o
    vh                     @  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r%ddl	m
Z
 dZG dd	 d	eZ	d!d"ddZd#ddZ	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   ]/var/www/html/hyperkenya/venv/lib/python3.10/site-packages/urllib3/util/ssl_match_hostname.pyr      s    r   dn
typing.Anyhostnamestrmax_wildcardsintreturntyping.Match[str] | None | boolc           
      C  s   g }| sdS |  d}|d }|dd }|d}||kr&tdt|  |s2t|  | kS |dkr<|d n|d	sF|d	rO|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,   ipnamehost_ipIPv4Address | IPv6Addressr   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   r5   Fcert_TYPE_PEER_CERT_RET_DICT | Nonehostname_checks_common_nameNonec           	      C  sp  | st dzd|v rt|d|d }nt|}W n t y)   d}Y nw g }| dd}|D ]/\}}|dkrN|du rHt||rH dS || q4|dkrc|dur^t||r^ dS || q4|r|du r|s| ddD ]}|D ]\}}|d	krt||r  dS || qvqrt|d
krt	d|d
tt|f t|d
krt	d|d|d t	d)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)
ValueErrorr0   r1   rfindgetr,   r   r5   lenr   r"   mapr   )	r6   r   r8   r.   dnsnamessankeyvaluesubr   r   r   match_hostname_   sT   



rI   )r   )r   r   r   r   r   r   r   r   )r-   r   r.   r/   r   r   )F)r6   r7   r   r   r8   r   r   r9   )__doc__
__future__r   r0   r   typingr   r   TYPE_CHECKINGssl_r   __version__r?   r   r,   r5   rI   r   r   r   r   <module>   s    
8