U
    [e.                     @   s  d Z ddlmZmZmZ ddlZddlZddlZddlm	Z	m
Z
 ddlmZmZmZmZmZmZ zddlZW n ek
r   dZY nX ejddG d	d
 d
eZdd Zdd Zdd Zdd ZejdddG dd deZejddG dd deZejdddG dd deZejdddG dd deZejdddG dd deZejddG dd  d eZ ejdddG d!d" d"eZ!ejdddG d#d$ d$eZ"d%d& Z#d'd( Z$e	d)d*Z%dS )+z
Common verification code.
    )absolute_importdivisionprint_functionN   )	maketrans	text_type)CertificateErrorDNSMismatchIPAddressMismatchSRVMismatchURIMismatchVerificationErrorT)slotsc                   @   s    e Zd ZdZe Ze ZdS )ServiceMatchz<
    A match of a service id and a certificate pattern.
    N)__name__
__module____qualname____doc__attrib
service_idcert_pattern r   r   Z/var/www/html/services/stratfitenv/lib/python3.8/site-packages/service_identity/_common.pyr      s   r   c                 C   s   g }t | |t | | }dd |D }|D ]}||kr*||j|d q*|D ]*}||krNt| |jrN||j|d qN|rt|d|S )z
    Verify whether *cert_patterns* are valid for *obligatory_ids* and
    *optional_ids*.

    *obligatory_ids* must be both present and match.  *optional_ids* must match
    if a pattern of the respective type is present.
    c                 S   s   g | ]
}|j qS r   )r   ).0matchr   r   r   
<listcomp>4   s     z+verify_service_identity.<locals>.<listcomp>)Zmismatched_id)errors)_find_matchesappenderror_on_mismatch_contains_instance_ofpattern_classr   )cert_patternsZobligatory_idsZoptional_idsr   matchesZmatched_idsir   r   r   verify_service_identity'   s$    
 
 
r&   c                 C   s8   g }|D ]*}| D ] }| |r|t||d qq|S )a  
    Search for matching certificate patterns and service_ids.

    :param cert_ids: List certificate IDs like DNSPattern.
    :type cert_ids: `list`

    :param service_ids: List of service IDs like DNS_ID.
    :type service_ids: `list`

    :rtype: `list` of `ServiceMatch`
    )r   r   )verifyr   r   )r#   Zservice_idsr$   ZsidZcidr   r   r   r   I   s    
r   c                 C   s   | D ]}t ||r dS qdS )zB
    :type seq: iterable
    :type cl: type

    :rtype: bool
    TF)
isinstance)seqcler   r   r   r!   ]   s    
r!   c                 C   s   t | tr0z| d} W n tk
r.   Y dS X zt|  W dS  tk
rR   Y nX zt| dd W n tk
r   Y dS X dS )z
    Check whether *pattern* could be/match an IP address.

    :param pattern: A pattern for a host name.
    :type pattern: `bytes` or `unicode`

    :return: `True` if *pattern* could be an IP address, else `False`.
    :rtype: bool
    asciiFT*1)	r(   bytesdecodeUnicodeErrorint
ValueError	ipaddress
ip_addressreplacepatternr   r   r   _is_ip_addressj   s    

r9   F)initr   c                   @   s*   e Zd ZdZe ZedZ	dd Z
dS )
DNSPatternz7
    A DNS pattern as extracted from certificates.
       ^[a-z0-9\-_.]+$c                 C   sd   t |tstd| }|dks2t|s2d|kr@td||t| _	d| j	kr`t
| j	 dS )(
        :type pattern: `bytes`
        z'The DNS pattern must be a bytes string.        zInvalid DNS pattern {0!r}.   *N)r(   r/   	TypeErrorstripr9   r   format	translate_TRANS_TO_LOWERr8   _validate_patternselfr8   r   r   r   __init__   s    

zDNSPattern.__init__N)r   r   r   r   r   r   r8   recompile_RE_LEGAL_CHARSrI   r   r   r   r   r;      s   
r;   c                   @   s$   e Zd ZdZe Zedd ZdS )IPAddressPatternz?
    An IP address pattern as extracted from certificates.
    c                 C   s:   z| t |dW S  tk
r4   td|Y nX d S )Nr7   z Invalid IP address pattern {!r}.)r4   r5   r3   r   rC   )clsbsr   r   r   
from_bytes   s    zIPAddressPattern.from_bytesN)	r   r   r   r   r   r   r8   classmethodrP   r   r   r   r   rM      s   rM   c                   @   s(   e Zd ZdZe Ze Zdd ZdS )
URIPatternz8
    An URI pattern as extracted from certificates.
    c                 C   sd   t |tstd| t}d|ks8d|ks8t|rFtd||	d\| _
}t|| _dS )r=   z'The URI pattern must be a bytes string.   :r@   zInvalid URI pattern {0!r}.N)r(   r/   rA   rB   rD   rE   r9   r   rC   splitprotocol_patternr;   dns_pattern)rH   r8   hostnamer   r   r   rI      s    
zURIPattern.__init__N)	r   r   r   r   r   r   rU   rV   rI   r   r   r   r   rR      s   rR   c                   @   s(   e Zd ZdZe Ze Zdd ZdS )
SRVPatternz8
    An SRV pattern as extracted from certificates.
    c                 C   s~   t |tstd| t}|d dksDd|ksDd|ksDt|rRtd||	dd\}}|dd | _
t|| _dS )	r=   z'The SRV pattern must be a bytes string.r   _      .r@   zInvalid SRV pattern {0!r}.r   N)r(   r/   rA   rB   rD   rE   r9   r   rC   rT   name_patternr;   rV   )rH   r8   namerW   r   r   r   rI      s"    

zSRVPattern.__init__N)	r   r   r   r   r   r   r[   rV   rI   r   r   r   r   rX      s   rX   c                   @   s:   e Zd ZdZe ZedZ	e
ZeZdd Zdd ZdS )DNS_IDz)
    A DNS service ID, aka hostname.
    r<   c                 C   s   t |tstd| }|dks*t|r2tdtdd |D r^trTt|}qht	dn
|d}|
t| _| j| jdkrtddS )	z+
        :type hostname: `unicode`
        z DNS-ID must be a unicode string. zInvalid DNS-ID.c                 s   s   | ]}t |d kV  qdS )   N)ord)r   cr   r   r   	<genexpr>	  s     z"DNS_ID.__init__.<locals>.<genexpr>z+idna library is required for non-ASCII IDs.r,   N)r(   r   rA   rB   r9   r3   anyidnaencodeImportErrorrD   rE   rW   rL   r   )rH   rW   Zascii_idr   r   r   rI      s    

zDNS_ID.__init__c                 C   s"   t || jrt|j| jS dS dS )zC
        https://tools.ietf.org/search/rfc6125#section-6.4
        FN)r(   r"   _hostname_matchesr8   rW   rG   r   r   r   r'     s    zDNS_ID.verifyN)r   r   r   r   r   r   rW   rJ   rK   rL   r;   r"   r	   r    rI   r'   r   r   r   r   r]      s   
r]   c                   @   s.   e Zd ZdZejejdZe	Z
eZdd ZdS )IPAddress_IDz#
    An IP address service ID.
    )	converterc                 C   s   | j |jkS )zC
        https://tools.ietf.org/search/rfc2818#section-3.1
        )ipr8   rG   r   r   r   r'   ,  s    zIPAddress_ID.verifyN)r   r   r   r   r   r   r4   r5   rj   rM   r"   r
   r    r'   r   r   r   r   rh   !  s
   rh   c                   @   s8   e Zd ZdZe Ze ZeZ	e
Zdd Zdd ZdS )URI_IDz
    An URI service ID.
    c                 C   sf   t |tstd| }d|ks*t|r2td|d\}}|dt	| _
t|d| _dS )z&
        :type uri: `unicode`
        z URI-ID must be a unicode string.:zInvalid URI-ID.r,   /N)r(   r   rA   rB   r9   r3   rT   re   rD   rE   protocolr]   dns_id)rH   uriZprotrW   r   r   r   rI   ?  s    
zURI_ID.__init__c                 C   s.   t || jr&|j| jko$| j|jS dS dS )zE
        https://tools.ietf.org/search/rfc6125#section-6.5.2
        FN)r(   r"   rU   rn   ro   r'   rV   rG   r   r   r   r'   O  s
    zURI_ID.verifyN)r   r   r   r   r   r   rn   ro   rR   r"   r   r    rI   r'   r   r   r   r   rk   3  s   rk   c                   @   s8   e Zd ZdZe Ze ZeZ	e
Zdd Zdd ZdS )SRV_IDz
    An SRV service ID.
    c                 C   sv   t |tstd| }d|ks6t|s6|d dkr>td|dd\}}|dd dt	| _
t|| _dS )	z&
        :type srv: `unicode`
        z SRV-ID must be a unicode string..r   _zInvalid SRV-ID.r   Nr,   )r(   r   rA   rB   r9   r3   rT   re   rD   rE   r\   r]   ro   )rH   Zsrvr\   rW   r   r   r   rI   h  s    
zSRV_ID.__init__c                 C   s.   t || jr&| j|jko$| j|jS dS dS )zE
        https://tools.ietf.org/search/rfc6125#section-6.5.1
        FN)r(   r"   r\   r[   ro   r'   rV   rG   r   r   r   r'   x  s
    zSRV_ID.verifyN)r   r   r   r   r   r   r\   ro   rX   r"   r   r    rI   r'   r   r   r   r   rq   \  s   rq   c                 C   s^   d| krR|  dd\}}| dd\}}||kr4dS |drBdS |dkpP||kS | |kS dS )z
    :type cert_pattern: `bytes`
    :type actual_hostname: `bytes`

    :return: `True` if *cert_pattern* matches *actual_hostname*, else `False`.
    :rtype: `bool`
    r@   rZ   r   Fs   xn--N)rT   
startswith)r   Zactual_hostnameZ	cert_headZ	cert_tailZactual_headZactual_tailr   r   r   rg     s    
rg   c                 C   s   |  d}|dkr td| | d}t|dk rDtd| d|d kr^td| td	d
 |D r~td| dS )z
    Check whether the usage of wildcards within *cert_pattern* conforms with
    our expectations.

    :type hostname: `bytes`

    :return: None
    r@   r   z7Certificate's DNS-ID {0!r} contains too many wildcards.rZ      zJCertificate's DNS-ID {0!r} has too few host components for wildcard usage.r   zECertificate's DNS-ID {0!r} has a wildcard outside the left-most part.c                 s   s   | ]}t | V  qd S )N)len)r   pr   r   r   rb     s     z$_validate_pattern.<locals>.<genexpr>z0Certificate's DNS-ID {0!r} contains empty parts.N)countr   rC   rT   rv   rc   )r   Zcntpartsr   r   r   rF     s4    	

rF   s   ABCDEFGHIJKLMNOPQRSTUVWXYZs   abcdefghijklmnopqrstuvwxyz)&r   
__future__r   r   r   r4   rJ   r   Z_compatr   r   
exceptionsr   r	   r
   r   r   r   rd   rf   sobjectr   r&   r   r!   r9   r;   rM   rR   rX   r]   rh   rk   rq   rg   rF   rE   r   r   r   r   <module>   sL    


	"
/
('% 