
     f                        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	e      Z	 d	 	 	 	 	 	 	 dd
ZddZ	 d	 	 	 	 	 	 	 ddZy)zHThe match_hostname() function from Python 3.5, essential when using SSL.    )annotationsN)IPv4AddressIPv6Address   )_TYPE_PEER_CERT_RET_DICTz3.5.0.1c                      e Zd Zy)CertificateErrorN)__name__
__module____qualname__     W/home/Script/Script_env/lib/python3.12/site-packages/urllib3/util/ssl_match_hostname.pyr	   r	      s    r   r	   c                   g }| sy| j                  d      }|d   }|dd }|j                  d      }||kD  rt        dt        |       z         |s*t	        | j                         |j                         k(        S |dk(  r|j                  d       n{|j                  d	      s|j                  d	      r%|j                  t        j                  |             n4|j                  t        j                  |      j                  d
d             |D ]&  }|j                  t        j                  |             ( t        j                  ddj                  |      z   dz   t        j                        }	|	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)
dnhostnamemax_wildcardspatspartsleftmost	remainder	wildcardsfragpats
             r   _dnsname_matchr+      sO    D HHTNEQxHab	Is#I= 
 :T"XE
 	

 BHHJ(.."2233
 3 	G			V	$(;(;F(C
 	BIIh'( 	BIIh'//w?@  %BIIdO$% **UUZZ--5r}}
EC99Xr   c                    t        j                  | j                               }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)ipnamehost_ipips      r   _ipaddress_matchr4   P   s2     
		fmmo	.B		W^^+,,r   c                   | st        d      	 d|v r(t        j                  |d|j                  d             }nt        j                  |      }g }| j	                  dd      }|D ]S  \  }}|dk(  r"|t        ||      r y|j                  |       -|dk(  s3|t        ||      r y|j                  |       U |rK|I|sG| j	                  dd      D ]2  }|D ]+  \  }}|d	k(  st        ||      r  y|j                  |       - 4 t        |      d
kD  r.t        d|ddj                  t        t        |                  t        |      d
k(  rt        d|d|d         t        d      # t         $ r d}Y 0w xY w)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 z doesn't match either of z, z doesn't match r   z/no appropriate subjectAltName fields were found)
ValueErrorr-   r.   rfindgetr+   r   r4   lenr	   r   mapr   )	certr"   hostname_checks_common_namer2   dnsnamessankeyvaluesubs	            r   match_hostnamerG   _   s    -
 	

 (?**84IhnnS6I+JKG**84G
 H'+xx0@"'EC  #
U%<>%#BOOE"L "'7w'GOOE"# #wx88Ir* 	+C! +
U,&%eX6OOE*	+	+ 8}q,4diiD(@S6TV
 	
 
X!	8,ohqk_UVVPQQG  s   AE2 2F F)r   )r!   z
typing.Anyr"   strr#   intreturnztyping.Match[str] | None | bool)r1   rH   r2   zIPv4Address | IPv6AddressrJ   r   )F)r@   z_TYPE_PEER_CERT_RET_DICT | Noner"   rH   rA   r   rJ   None)__doc__
__future__r   r-   r   typingr   r   TYPE_CHECKINGssl_r   __version__r;   r	   r+   r4   rG   r   r   r   <module>rR      s    N #  	  .	.	z 	
 9:55!5255$5p-$ ).@R
)@R@R "&@R 
	@Rr   