a
    H¤XhÕ  ã                   @  s    d Z ddlmZ ddlZddlZddlZddlmZmZ ejrJddl	m
Z
 dZG dd	„ d	eƒZdd
ddddœdd„Zddddœdd„Zddddddœdd„ZdS )zHThe match_hostname() function from Python 3.5, essential when using SSL.é    )ÚannotationsN)ÚIPv4AddressÚIPv6Addressé   )Ú_TYPE_PEER_CERT_RET_DICTz3.5.0.1c                   @  s   e Zd ZdS )ÚCertificateErrorN)Ú__name__Ú
__module__Ú__qualname__© r   r   úd/var/www/viveiro_mudafortebrasil/venv/lib/python3.9/site-packages/urllib3/util/ssl_match_hostname.pyr      s   r   z
typing.AnyÚstrÚintztyping.Match[str] | None | bool)ÚdnÚhostnameÚmax_wildcardsÚreturnc           
      C  sö   g }| sdS |   d¡}|d }|dd… }| d¡}||krLtdt| ƒ ƒ‚|sdt|  ¡ | ¡ kƒS |dkrx| d¡ n>| d	¡sŒ| d	¡rž| t 	|¡¡ n| t 	|¡ 
d
d¡¡ |D ]}| t 	|¡¡ qºt dd |¡ d tj¡}	|	 |¡S )zhMatching according to RFC 6125, section 6.4.3

    http://tools.ietf.org/html/rfc6125#section-6.4.3
    FÚ.r   r   NÚ*z,too many wildcards in certificate DNS name: z[^.]+zxn--z\*z[^.]*z\Az\.z\Z)ÚsplitÚcountr   ÚreprÚboolÚlowerÚappendÚ
startswithÚreÚescapeÚreplaceÚcompileÚjoinÚ
IGNORECASEÚmatch)
r   r   r   ÚpatsÚpartsÚleftmostÚ	remainderÚ	wildcardsÚfragÚpatr   r   r   Ú_dnsname_match   s,    


ÿr*   zIPv4Address | IPv6Addressr   )ÚipnameÚhost_ipr   c                 C  s   t  |  ¡ ¡}t|j|jkƒS )a…  Exact matching of IP addresses.

    RFC 9110 section 4.3.5: "A reference identity of IP-ID contains the decoded
    bytes of the IP address. An IP version 4 address is 4 octets, and an IP
    version 6 address is 16 octets. [...] A reference identity of type IP-ID
    matches if the address is identical to an iPAddress value of the
    subjectAltName extension of the certificate."
    )Ú	ipaddressÚ
ip_addressÚrstripr   Úpacked)r+   r,   Úipr   r   r   Ú_ipaddress_matchP   s    r2   Fz_TYPE_PEER_CERT_RET_DICT | NoneÚNone)Úcertr   Úhostname_checks_common_namer   c           	      C  s„  | st dƒ‚z0d|v r0t |d| d¡… ¡}n
t |¡}W n t yR   d}Y n0 g }|  dd¡}|D ]^\}}|dkrœ|du rt||ƒr dS | |¡ qh|dkrh|dur¼t||ƒr¼ dS | |¡ qh|r$|du r$|s$|  dd¡D ]8}|D ].\}}|d	kròt||ƒr  dS | |¡ qòqêt|ƒd
krPt	d|d 
tt|ƒ¡f ƒ‚n0t|ƒd
krxt	d|›d|d ›ƒ‚nt	dƒ‚dS )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_REQUIREDú%NÚsubjectAltNamer   ÚDNSz
IP AddressÚsubjectÚ
commonNamer   z&hostname %r doesn't match either of %sz, z	hostname z doesn't match r   z/no appropriate subjectAltName fields were found)Ú
ValueErrorr-   r.   ÚrfindÚgetr*   r   r2   Úlenr   r    Úmapr   )	r4   r   r5   r,   ÚdnsnamesÚsanÚkeyÚvalueÚsubr   r   r   Úmatch_hostname_   sJ    ÿ
ÿÿrE   )r   )F)Ú__doc__Ú
__future__r   r-   r   Útypingr   r   ÚTYPE_CHECKINGÚssl_r   Ú__version__r;   r   r*   r2   rE   r   r   r   r   Ú<module>   s    ÿ8 ý