3 ]3 @stdZddlZddlZy ddlZWnek r8dZYnXdZGdddeZdddZd d Z d d Z d dZ dS)zJThe match_hostname() function from Python 3.3.3, essential when using SSL.Nz3.5.0.1c@s eZdZdS)CertificateErrorN)__name__ __module__ __qualname__rrV/tmp/pip-build-el9acr48/urllib3/urllib3/packages/ssl_match_hostname/_implementation.pyrsrc Csg}|s dS|jd}|d}|dd}|jd}||krLtdt||s`|j|jkS|dkrt|jdn>|jd s|jd r|jtj|n|jtj|j d d x|D]}|jtj|qWtj d d j |dtj } | j |S)zhMatching according to RFC 6125, section 6.4.3 http://tools.ietf.org/html/rfc6125#section-6.4.3 F.rrN*z,too many wildcards in certificate DNS name: z[^.]+zxn--z\*z[^.]*z\Az\.z\Z)splitcountrreprlowerappend startswithreescapereplacecompilejoin IGNORECASEmatch) dnhostname max_wildcardspatspartsleftmost remainder wildcardsfragpatrrr_dnsname_matchs*     r"cCs&t|tr"tjdkr"t|ddd}|S)Nasciistrict)encodingerrors)r#) isinstancestrsys version_infounicode)objrrr _to_unicodeNsr.cCstjt|j}||kS)zExact matching of IP addresses. RFC 6125 explicitly doesn't define an algorithm for this (section 1.7.2 - "Out of Scope"). ) ipaddress ip_addressr.rstrip)ipnamehost_ipiprrr_ipaddress_matchSsr5cCs|s tdytjt|}WnPtk r6d}Yn:tk rLd}Yn$tk rntdkrhd}nYnXg}|jdf}xb|D]Z\}}|dkr|dkrt||rdS|j|q|dkr|dk rt ||rdS|j|qW|s8xL|jdfD]<}x6|D].\}}|dkrt||r$dS|j|qWqWt |dkrdt d |d j t t|fn,t |dkrt d ||d fnt d dS)a)Verify that *cert* (in decoded format as returned by SSLSocket.getpeercert()) matches the *hostname*. RFC 2818 and RFC 6125 rules are followed, but IP addresses are not accepted for *hostname*. CertificateError is raised on failure. On success, the function returns nothing. ztempty or no certificate, match_hostname needs a SSL socket or SSL context with either CERT_OPTIONAL or CERT_REQUIREDNsubjectAltNameDNSz IP Addresssubject commonNamerz&hostname %r doesn't match either of %sz, zhostname %r doesn't match %rrz=no appropriate commonName or subjectAltName fields were found) ValueErrorr/r0r. UnicodeErrorAttributeErrorgetr"rr5lenrrmapr )certrr3dnsnamessankeyvaluesubrrrmatch_hostname_sJ    rF)r) __doc__rr*r/ ImportError __version__r:rr"r.r5rFrrrrs   5