
    #a.                     n   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 	 ddlZn# e$ r dZY nw xY w ej        d           G d	 d
e                      Zd Zd Zd Zd Z ej        dd           G d de                      Z ej        d           G d de                      Z ej        dd           G d de                      Z ej        dd           G d de                      Z ej        dd           G d de                      Z ej        d           G d de                      Z  ej        dd           G d de                      Z! ej        dd           G d d e                      Z"d! Z#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                   N    e Zd ZdZ ej                    Z ej                    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     8lib/python3.11/site-packages/service_identity/_common.pyr   r      s4          J4799LLLr   r   c                 z   g }t          | |          t          | |          z   }d |D             }|D ]/}||vr)|                    |                    |                     0|D ]D}||vr>t          | |j                  r)|                    |                    |                     E|rt          |          |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                     g | ]	}|j         
S r   )r   ).0matchs     r   
<listcomp>z+verify_service_identity.<locals>.<listcomp>4   s    9995#999r   )mismatched_id)errors)_find_matchesappenderror_on_mismatch_contains_instance_ofpattern_classr   )cert_patternsobligatory_idsoptional_idsr#   matchesmatched_idsis          r   verify_service_identityr/   '   s     FM>::]|> > G :9999K @ @KMM!--A->>??? @ @
 K$91?%
 %
 MM!--A->>??? /v....Nr   c                     g }|D ]@}| D ];}|                     |          r$|                    t          ||                     <A|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)   service_idsr,   sidcids        r   r$   r$   I   sk     G O O  	O 	OCzz# O|MMMNNN	O Nr   c                 6    | D ]}t          ||          r dS dS )zB
    :type seq: iterable
    :type cl: type

    :rtype: bool
    TF)
isinstance)seqcles      r   r'   r'   ]   s6       a 	44	5r   c                 :   t          | t                    r(	 |                     d          } n# t          $ r Y dS w xY w	 t	          |            dS # t
          $ r Y nw xY w	 t          j        |                     dd                     n# t
          $ r Y dS w xY w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)	r6   bytesdecodeUnicodeErrorint
ValueError	ipaddress
ip_addressreplacepatterns    r   _is_ip_addressrH   j   s     '5!! 	nnW--GG 	 	 	55	Gt   W__S#667777   uu 4s/   - 
;;A 
AA!(B
 

BBF)initr   c                   V    e Zd ZdZ ej                    Z ej        d          Z	d Z
dS )
DNSPatternz7
    A DNS pattern as extracted from certificates.
       ^[a-z0-9\-_.]+$c                 h   t          |t                    st          d          |                                }|dk    st	          |          sd|v r"t          d                    |                    |                    t                    | _	        d| j	        v rt          | j	                   dS dS )(
        :type pattern: `bytes`
        z'The DNS pattern must be a bytes string.r       zInvalid DNS pattern {0!r}.   *N)r6   r>   	TypeErrorstriprH   r	   format	translate_TRANS_TO_LOWERrG   _validate_patternselfrG   s     r   __init__zDNSPattern.__init__   s     '5)) 	GEFFF--//c>>^G44>8H8H",33G<<   ((994<dl+++++  r   N)r   r   r   r   r   r   rG   recompile_RE_LEGAL_CHARSrY   r   r   r   rK   rK      sN          dgiiG bj!455O, , , , ,r   rK   c                   F    e Zd ZdZ ej                    Zed             ZdS )IPAddressPatternz?
    An IP address pattern as extracted from certificates.
    c                     	  | t          j        |                    S # t          $ r# t          d                    |                    w xY w)NrF   z Invalid IP address pattern {!r}.)rC   rD   rB   r	   rS   )clsbss     r   
from_byteszIPAddressPattern.from_bytes   sc    	3y3B778888 	 	 	"299"==  	s	     -AN)	r   r   r   r   r   r   rG   classmethodrb   r   r   r   r^   r^      sF          dgiiG  [  r   r^   c                   T    e Zd ZdZ ej                    Z ej                    Zd ZdS )
URIPatternz8
    An URI pattern as extracted from certificates.
    c                 z   t          |t                    st          d          |                                                    t
                    }d|vsd|v st          |          r"t          d                    |                    |	                    d          \  | _
        }t          |          | _        dS )rN   z'The URI pattern must be a bytes string.   :rP   zInvalid URI pattern {0!r}.N)r6   r>   rQ   rR   rT   rU   rH   r	   rS   splitprotocol_patternrK   dns_pattern)rX   rG   hostnames      r   rY   zURIPattern.__init__   s     '5)) 	GEFFF--//++O<<w$'//^G5L5L/",33G<<   +2--*=*='x%h//r   N)	r   r   r   r   r   r   ri   rj   rY   r   r   r   re   re      sJ          twyy$'))K0 0 0 0 0r   re   c                   T    e Zd ZdZ ej                    Z ej                    Zd ZdS )
SRVPatternz8
    An SRV pattern as extracted from certificates.
    c                    t          |t                    st          d          |                                                    t
                    }|d         dk    sd|vsd|v st          |          r"t          d                    |                    |	                    dd          \  }}|dd         | _
        t          |          | _        dS )	rN   z'The SRV pattern must be a bytes string.r   _      .rP   zInvalid SRV pattern {0!r}.r   N)r6   r>   rQ   rR   rT   rU   rH   r	   rS   rh   name_patternrK   rj   )rX   rG   namerk   s       r   rY   zSRVPattern.__init__   s     '5)) 	GEFFF--//++O<< AJ'!!7""wg&&  #,33G<<   !tQ//h H%h//r   N)	r   r   r   r   r   r   rq   rj   rY   r   r   r   rm   rm      sI          4799L$'))K0 0 0 0 0r   rm   c                   d    e Zd ZdZ ej                    Z ej        d          Z	e
ZeZd Zd ZdS )DNS_IDz)
    A DNS service ID, aka hostname.
    rL   c                 
   t          |t                    st          d          |                                }|dk    st	          |          rt          d          t          d |D                       r+t          rt          j        |          }n$t          d          |                    d          }|
                    t                    | _        | j                            | j                  t          d          dS )z+
        :type hostname: `unicode`
        z DNS-ID must be a unicode string. zInvalid DNS-ID.c              3   <   K   | ]}t          |          d k    V  dS )   N)ord)r   cs     r   	<genexpr>z"DNS_ID.__init__.<locals>.<genexpr>	  s,      ..s1vv|......r   z+idna library is required for non-ASCII IDs.r;   N)r6   r   rQ   rR   rH   rB   anyidnaencodeImportErrorrT   rU   rk   r\   r    )rX   rk   ascii_ids      r   rY   zDNS_ID.__init__   s     (I.. 	@>???>>##s??nX66?.///..X..... 	0 ;x00!A    w//H **?;;%%dm44<./// =<r   c                 d    t          || j                  rt          |j        | j                  S dS )zC
        https://tools.ietf.org/search/rfc6125#section-6.4
        F)r6   r(   _hostname_matchesrG   rk   rW   s     r   r1   zDNS_ID.verify  s2     gt122 	$W_dmDDD5r   N)r   r   r   r   r   r   rk   rZ   r[   r\   rK   r(   r
   r&   rY   r1   r   r   r   rt   rt      sj          twyyH !bj!455OM#0 0 02    r   rt   c                   L    e Zd ZdZ ej        ej                  Ze	Z
eZd ZdS )IPAddress_IDz#
    An IP address service ID.
    )	converterc                 "    | j         |j        k    S )zC
        https://tools.ietf.org/search/rfc2818#section-3.1
        )iprG   rW   s     r   r1   zIPAddress_ID.verify,  s     w'/))r   N)r   r   r   r   r   r   rC   rD   r   r^   r(   r   r&   r1   r   r   r   r   r   !  sQ          
9/	0	0	0B$M)* * * * *r   r   c                   b    e Zd ZdZ ej                    Z ej                    ZeZ	e
Zd Zd ZdS )URI_IDz
    An URI service ID.
    c                    t          |t                    st          d          |                                }d|vst	          |          rt          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)r6   r   rQ   rR   rH   rB   rh   r~   rT   rU   protocolrt   dns_id)rX   uriprotrk   s       r   rY   zURI_ID.__init__?  s     #y)) 	@>???iikks??nS11?.///4hG,,66GGX^^D1122r   c                     t          || j                  r/|j        | j        k    o| j                            |j                  S dS )zE
        https://tools.ietf.org/search/rfc6125#section-6.5.2
        F)r6   r(   ri   r   r   r1   rj   rW   s     r   r1   zURI_ID.verifyO  sM     gt122 	(DM9 <K&&w':;;
 5r   N)r   r   r   r   r   r   r   r   re   r(   r   r&   rY   r1   r   r   r   r   r   3  sc          twyyHTWYYFM#3 3 3 
 
 
 
 
r   r   c                   b    e Zd ZdZ ej                    Z ej                    ZeZ	e
Zd Zd ZdS )SRV_IDz
    An SRV service ID.
    c                    t          |t                    st          d          |                                }d|vst	          |          s|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;   )r6   r   rQ   rR   rH   rB   rh   r~   rT   rU   rr   rt   r   )rX   srvrr   rk   s       r   rY   zSRV_ID.__init__h  s     #y)) 	@>???iikks??nS11?SVt^^.///4++hHOOG,,66GG	X&&r   c                     t          || j                  r/| j        |j        k    o| j                            |j                  S dS )zE
        https://tools.ietf.org/search/rfc6125#section-6.5.1
        F)r6   r(   rr   rq   r   r1   rj   rW   s     r   r1   zSRV_ID.verifyx  sQ     gt122 	9 44 9K9K#: :  5r   N)r   r   r   r   r   r   rr   r   rm   r(   r   r&   rY   r1   r   r   r   r   r   \  sc          4799DTWYYFM#' ' ' 	 	 	 	 	r   r   c                     d| v r]|                      dd          \  }}|                     dd          \  }}||k    rdS |                    d          rdS |dk    p||k    S | |k    S )z
    :type cert_pattern: `bytes`
    :type actual_hostname: `bytes`

    :return: `True` if *cert_pattern* matches *actual_hostname*, else `False`.
    :rtype: `bool`
    rP   rp   r   Fs   xn--)rh   
startswith)r   actual_hostname	cert_head	cert_tailactual_headactual_tails         r   r   r     s     |+11$::	9#2#8#8q#A#A [##5!!'** 	5D <I$<<..r   c                    |                      d          }|dk    r"t          d                    |                     |                     d          }t	          |          dk     r"t          d                    |                     d|d         vr"t          d                    |                     t          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
    rP   r   z7Certificate's DNS-ID {0!r} contains too many wildcards.rp      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              3   6   K   | ]}t          |           V  d S )N)len)r   ps     r   r{   z$_validate_pattern.<locals>.<genexpr>  s*      
%
%!s1vv:
%
%
%
%
%
%r   z0Certificate's DNS-ID {0!r} contains empty parts.N)countr	   rS   rh   r   r|   )r   cntpartss      r   rV   rV     s    

T
"
"C
QwwELL 
 
 	

 t$$E
5zzA~~$f\22
 
 	

 58F<((
 
 	
 
%
%u
%
%
%%% 
>EE 
 
 	

 
r   s   ABCDEFGHIJKLMNOPQRSTUVWXYZs   abcdefghijklmnopqrstuvwxyz)&r   
__future__r   r   r   rC   rZ   r   _compatr   r   
exceptionsr	   r
   r   r   r   r   r}   r   sobjectr   r/   r$   r'   rH   rK   r^   re   rm   rt   r   r   r   r   rV   rU   r   r   r   <module>r      s    A @ @ @ @ @ @ @ @ @     				  ) ) ) ) ) ) ) )               KKKK   DDD d    6     D  (
 
 
  < U$, , , , , , ,  ,8 d    v   " U$0 0 0 0 0 0 0  02 U$0 0 0 0 0 0 0  0> U$, , , , ,V , ,  ,^ d* * * * *6 * * *" U$% % % % %V % %  %P U$$ $ $ $ $V $ $  $N/ / /,!
 !
 !
J )!#@ s   7 A A