o
    	c~                     @   sr   d Z ddlZddlZzddlZW n ey   dZY nw dZG dd deZdddZd	d
 Z	dd Z
dd ZdS )zJThe match_hostname() function from Python 3.3.3, essential when using SSL.    Nz3.5.0.1c                   @   s   e Zd ZdS )CertificateErrorN)__name__
__module____qualname__ r   r   L/opt/certbot/lib/python3.10/site-packages/urllib3/util/ssl_match_hostname.pyr      s    r      c           
      C   s   g }| sdS |  d}|d }|dd }|d}||kr&tdt|  |s0|  | kS |dkr:|d n|d	sD|d	rM|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lowerappend
startswithreescapereplacecompilejoin
IGNORECASEmatch)
dnhostnamemax_wildcardspatspartsleftmost	remainder	wildcardsfragpatr   r   r   _dnsname_match   s,   



r"   c                 C   s&   t | trtjdk rt| ddd} | S )N)   asciistrict)encodingerrors)
isinstancestrsysversion_infounicode)objr   r   r   _to_unicodeO   s   r.   c                 C   s   t t|  }||kS )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)ipnamehost_ipipr   r   r   _ipaddress_matchV   s   r5   c              	   C   sf  | st dz	tt|}W n tt fy   d}Y n ty,   tdu r)d}n Y nw g }| dd}|D ]/\}}|dkrQ|du rKt||rK dS || q7|dkrf|durat	||ra dS || q7|s| ddD ]}|D ]\}}|dkrt||r  dS || qsqot
|d	krtd
|dtt|f t
|d	krtd||d f 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_REQUIREDNsubjectAltNamer   DNSz
IP Addresssubject
commonNamer   z&hostname %r doesn't match either of %sz, zhostname %r doesn't match %rr   z=no appropriate commonName or subjectAltName fields were found)
ValueErrorr/   r0   r.   UnicodeErrorAttributeErrorgetr"   r   r5   lenr   r   mapr   )certr   r3   dnsnamessankeyvaluesubr   r   r   match_hostnameb   s\   


rF   )r   )__doc__r   r*   r/   ImportError__version__r:   r   r"   r.   r5   rF   r   r   r   r   <module>   s    
6