3 §˜‚]3ã @stdZddlZddlZy ddlZWnek r8dZYnXdZGdd„deƒZddd„Zd d „Z d d „Z d d„Z dS)zJThe match_hostname() function from Python 3.3.3, essential when using SSL.éNz3.5.0.1c@s eZdZdS)ÚCertificateErrorN)Ú__name__Ú __module__Ú __qualname__©rrúV/tmp/pip-build-el9acr48/urllib3/urllib3/packages/ssl_match_hostname/_implementation.pyrsréc Csög}|s dS|jdƒ}|d}|dd…}|jdƒ}||krLtdt|ƒƒ‚|s`|jƒ|jƒkS|dkrt|jdƒn>|jd ƒsˆ|jd ƒrš|jtj|ƒƒn|jtj|ƒj d d ƒƒx|D]}|jtj|ƒƒq¸Wtj 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)ÚsplitÚcountrÚreprÚlowerÚappendÚ startswithÚreÚescapeÚreplaceÚcompileÚjoinÚ IGNORECASEÚmatch) ÚdnÚhostnameÚ max_wildcardsÚpatsÚpartsÚleftmostÚ remainderÚ wildcardsÚfragÚpatrrrÚ_dnsname_matchs*     r"cCs&t|tƒr"tjdkr"t|ddd�}|S)NéÚasciiÚstrict)ÚencodingÚerrors)r#)Ú isinstanceÚstrÚsysÚ version_infoÚunicode)ÚobjrrrÚ _to_unicodeNsr.cCstjt|ƒjƒƒ}||kS)zˆExact 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)ÚipnameÚhost_ipÚiprrrÚ_ipaddress_matchSsr5cCs”|s tdƒ‚ytjt|ƒƒ}WnPtk r6d}Yn:tk rLd}Yn$tk rntdkrhd}n‚YnXg}|jdfƒ}xb|D]Z\}}|dkr¸|dkr¬t||ƒr¬dS|j|ƒq†|dkr†|dk rÖt ||ƒrÖdS|j|ƒq†W|�s8xL|jdfƒD]<}x6|D].\}}|dk�rt||ƒ�r$dS|j|ƒ�qWqøWt |ƒdk�rdt d |d j t t|ƒƒfƒ‚n,t |ƒdk�rˆt d ||d fƒ‚nt 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_REQUIREDNÚsubjectAltNameÚDNSz IP AddressÚsubjectÚ 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.Ú UnicodeErrorÚAttributeErrorÚgetr"rr5ÚlenrrÚmapr )Úcertrr3ÚdnsnamesÚsanÚkeyÚvalueÚsubrrrÚmatch_hostname_sJ    rF)r) Ú__doc__rr*r/Ú ImportErrorÚ __version__r:rr"r.r5rFrrrrÚs   5