
     hn3                         d dl Z d dlZd dlmZ d dlmZmZ d dlmZm	Z	m
Z
mZmZmZmZ d dlmZ d dlmZ  ej        e          Z G d de          Zd	ed
edefdZd ZdededefdZdedefdZdedefdZdej        dej        fdZ  G d de j!                  Z"e"j#        e e"j$        ee"j%        ee"j&        eiZ' G d de(          Z)dej*        fdZ+dej,        fdZ- G d d          Z. ed           G d  d!                      Z/e	e"ee/         f         Z0d"e
ej                 de0fd#Z1d$e
e/         de0fd%Z2d&ej3        de0fd'Z4 G d( d)          Z5 G d* d+          Z6 G d, d-          Z7de0fd.Z8de0fd/Z9dS )0    N)	dataclass)IPv4AddressIPv6Address)CallableDictIterableListOptionalSetUnion)x509)urisplitc                       e Zd ZdS )NameConstraintErrorN)__name__
__module____qualname__     \/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/pyhanko_certvalidator/name_trees.pyr   r      s        Dr   r   	base_host
other_hostreturnc                     | d         dk    r8|                     |           \  }}}t          |          ot          |           S || k    S )Nr   .)
rpartitionbool)r   r   pre_posts        r   host_tree_containsr!      sP     |s!,,Y77QCyy+d^+Y&&r   c                     t          |                                           }|rt          |t          t          f          r<|d| dnd}d|  d| d}t
                              |           t          |          |S )Nz	has host r   zis not a well-formed URI.zCURI constraints require URIs with a host specified as a FQDN; URI 'z' )r   gethost
isinstancer   r   loggerwarningr   )cand_uri	cand_hosthost_errmsgs       r   _host_regnamer+      s    ""**,,I '
9{K.HII ' $ %	$$$$- 	,, , (, , , 	 	s!#&&&r   baseotherc                 @    t          |          }t          | |          S N)r+   r!   )r,   r-   r   s      r   uri_tree_containsr0   ,   s    #E**JdJ///r   c           	      \   |                      d          }|                     d          }t          |          t          |          k     rdS t          |          t          |          k    o@t          d t          t	          |          t	          |                    D                       S )Nr   Fc              3   (   K   | ]\  }}||k    V  d S r/   r   .0xys      r   	<genexpr>z$dns_tree_contains.<locals>.<genexpr>9   s;       9 91aQ9 9 9 9 9 9r   )splitlenallzipreversed)r,   r-   base_labelsother_labelss       r   dns_tree_containsr?   2   s     **S//K;;s##L
<3{++++u|K 0 00 S 9 9x55x7L7LMM9 9 9 6 6 r   c                     |                      d          \  }}}|                     d          \  }}}|r| |k    S t          ||          S )N@)r   r!   )r,   r-   base_mailboxr   base_host_or_domainother_mailboxother_host_or_domains          r   email_tree_containsrF   >   s]     ,0??3+?+?(L!(-2-=-=c-B-B*M1* Mu}!"57KLLLr   c                     | j         }|j         }t          |          t          |          k    o&t          d t          ||          D                       S )Nc              3   (   K   | ]\  }}||k    V  d S r/   r   r3   s      r   r7   z(dirname_tree_contains.<locals>.<genexpr>O   sC       E E1aQE E E E E Er   )chosenr9   r:   r;   )r,   r-   base_rdn_sequenceother_rdn_sequences       r   dirname_tree_containsrL   K   sq    !""c*;&<&<<  E E02DEEE E E B B r   c                      e Zd Z ej                    Z ej                    Z ej                    Z ej                    Z ej                    Z	 ej                    Z
 ej                    Z ej                    Z ej                    Zedeeeeej        f         eeej        f         gef                  fd            Zedd            ZdS )GeneralNameTyper   c                 8    t                               | d           S r/   )_name_type_checkersgetselfs    r   check_membershipz GeneralNameType.check_membershipb   s     #&&tT222r   c                 F    t          | |                                          S r/   )getattrupper)clschoices     r   from_choicezGeneralNameType.from_choicej   s    sFLLNN+++r   N)r   rN   )r   r   r   enumauto
OTHER_NAMERFC822_NAMEDNS_NAMEX400_ADDRESSDIRECTORY_NAMEEDI_PARTY_NAMEUNIFORM_RESOURCE_IDENTIFIER
IP_ADDRESSREGISTERED_IDpropertyr
   r   r   strr   Namer   rT   classmethodrZ   r   r   r   rN   rN   W   s        J$)++Kty{{H49;;LTY[[NTY[[N"+$)++JDIKKM3	%TY'sDI~)>?EF
3 3 3 X3 , , , [, , ,r   rN   c                   $     e Zd Zdef fdZ xZS )UnsupportedNameTypeError	name_typec                 x    t                                          |j                                                   d S r/   )super__init__namelower)rS   rl   	__class__s     r   ro   z!UnsupportedNameTypeError.__init__x   s/    --//00000r   )r   r   r   rN   ro   __classcell__)rr   s   @r   rk   rk   w   sD        1/ 1 1 1 1 1 1 1 1 1 1r   rk   gnamec                     t                               | j                  }| j        }|t           j        k    r|j        }||fS r/   )rN   rZ   rp   rI   ra   native)rt   
gname_typevalues      r   _interpret_general_namery   |   s@     ,,UZ88JLE _333ur   certc              #   (  K   t          | j        j                  rt          j        | j        fV  | j        }|B| j        j        D ]3}|D ].}|d         j        dk    rt          j        |d         j        fV  /4d S |D ]}t          |          V  d S )Ntypeemail_addressrx   )	r9   subjectrI   rN   ra   subject_alt_name_valuerv   r^   ry   )rz   subject_alt_namesrdn	name_pairrp   s        r   _enumerate_names_in_certr      s      
4< ;,dl::::+/+F 
 <& 	Q 	QC  Q Q	V$+>>)5y7I7PPPPPQ	Q 	Q
 & 	0 	0D)$//////	0 	0r   c                   V    e Zd Zdeeej        f         fdZed             Z	d Z
d ZdS )_StringOrNamerx   c                     || _         d S r/   )rx   )rS   rx   s     r   ro   z_StringOrName.__init__   s    


r   c                 x    | j         }t          |t          j                  rd|                                fS d|fS )Nr      )rx   r$   r   rh   dump)rS   vals     r   _codez_StringOrName._code   s8    jc49%% 	chhjj= c6Mr   c                 *    t          | j                  S r/   )hashr   rR   s    r   __hash__z_StringOrName.__hash__   s    DJr   c                 L    t          |t                    o| j        |j        k    S r/   )r$   r   r   )rS   r-   s     r   __eq__z_StringOrName.__eq__   s     %//MDJ%+4MMr   N)r   r   r   r   rg   r   rh   ro   rf   r   r   r   r   r   r   r   r      sx        eCN3       X     N N N N Nr   r   T)frozenc                       e Zd ZU eed<   ee         ed<   dZeed<   dZ	ee         ed<   de
eej        f         defd	Zeded
e
eej        f         fd            Zedd            Zededd fd            ZdS )NameSubtreerl   	tree_baser   minNmaxitemr   c                     | j         dS | j        dk    s| j        t          d          | j        j        }|t          d| j                    || j         j        |          S )NTr   zuThe minimum/maximum fields on a name constraint are not meaningful in the PKIX (RFC 5280) profile --- not processing.z%No containment checker available for )r   r   r   NotImplementedErrorrl   rT   rx   )rS   r   checkers      r   __contains__zNameSubtree.__contains__   s    >!4 8q==DH0%P   .1?%HHH   wt~+T222r   rp   c                 >    t          |t          |                    S )Nrl   r   )r   r   )rX   rl   rp   s      r   	from_namezNameSubtree.from_name   s    Y-:M:MNNNNr   c                     |d         }t          |          \  }}t          |t          |          |d         j        |d         j                  S )Nr,   minimummaximum)r   r   )ry   r   r   rv   )rX   subtreert   rl   name_objs        r   from_general_subtreez NameSubtree.from_general_subtree   sX    5e<<	8(##	")	")	
 
 
 	
r   c                 $    t          |d          S )z
        Tree that contains all names of a given type.

        :param name_type:
            The name type to use.
        :return:
        Nr   )r   )rX   rl   s     r   universal_treezNameSubtree.universal_tree   s     Y$????r   )r   r   )r   r   r   rN   __annotations__r
   r   r   intr   r   rg   r   rh   r   r   ri   r   r   r   r   r   r   r   r      s        &&&&CLLLC#3sDI~!6 34 3 3 3 3" O/ OsDI~9N O O O [O 
 
 
 [
 @ @= @ @ @ [@ @ @r   r   namesc                 X    dt           j        fdt          j        fd| D             iS )Nrp   c                 N    t                               t          j        |           S )N)rl   rp   )r   r   rN   ra   )rp   s    r   _subtreez(x509_names_to_subtrees.<locals>._subtree   s'    $$%44 % 
 
 	
r   c                 &    h | ]} |          S r   r   )r4   nr   s     r   	<setcomp>z)x509_names_to_subtrees.<locals>.<setcomp>   s!    ,H,H,HQXXa[[,H,H,Hr   )r   rh   rN   ra   )r   r   s    @r   x509_names_to_subtreesr      sE    
ty 
 
 
 

 *,H,H,H,H%,H,H,HIIr   treesc                     i }| D ]>}	 ||j                                      |           $# t          $ r |h||j         <   Y ;w xY w|S r/   )rl   addKeyError)r   resulttrees      r   _group_subtreesr      so     F , ,	,4>"&&t,,,, 	, 	, 	,&*VF4>"""	,Ms    )A Asubtreesc                 4    t          d | D                       S )Nc              3   J   K   | ]}t                               |          V  d S r/   )r   r   )r4   r   s     r   r7   z+process_general_subtrees.<locals>.<genexpr>   sA        6=((11     r   )r   )r   s    r   process_general_subtreesr      s0      AI     r   c                   h    e Zd Z	 	 ddee         deeej        df         fdZ	d Z
ed             ZdS )NameConstraintValidationResultNfailing_name_typefailing_namec                 "    || _         || _        d S r/   r   r   )rS   r   r   s      r   ro   z'NameConstraintValidationResult.__init__  s    
 =N9Er   c                     | j         d u S r/   )r   rR   s    r   __bool__z'NameConstraintValidationResult.__bool__  s    %--r   c                     | j         J | j        }t          |t          j                  r|j        }| j         j                                        }d| d| dS )Nz
The name 'z
' of type z is not allowed.)r   r   r$   r   rh   human_friendlyrp   rq   )rS   name_strrl   s      r   error_messagez,NameConstraintValidationResult.error_message  se    %111$h	** 	/.H*/5577	KHKK	KKKKr   )NN)r   r   r   r
   rN   r   rg   r   rh   ro   r   rf   r   r   r   r   r   r     s         8<48F F#O4F CD01F F F F. . . L L XL L Lr   r   c                   P    e Zd ZdefdZdefdZdedefdZde	j
        defd	Zd
S )PermittedSubtreesinitial_permitted_subtreesc                 <    fdt           D             }|| _        d S )Nc           	      Z    i | ]'}|t                              |d                     g(S )r   )setrQ   )r4   rl   r   s     r   
<dictcomp>z.PermittedSubtrees.__init__.<locals>.<dictcomp>'  sM     @
 @
 @
 6::9bIIJJK@
 @
 @
r   )rN   _trees)rS   r   r   s    ` r   ro   zPermittedSubtrees.__init__  s?    @
 @
 @
 @
,@
 @
 @
 r   r   c                 z    |                                 D ]%\  }}| j        |                             |           &d S r/   )itemsr   append)rS   r   rl   new_permitteds       r   intersect_withz PermittedSubtrees.intersect_with-  sF    (- 	9 	9$I}K	"))-8888	9 	9r   rl   r   c                     	 t          fdt          | j        |                   D                       S # t          $ r Y dS w xY w)Nc              3   N   K   | ]}t          fd |D                       V   dS )c              3       K   | ]}|v V  	d S r/   r   r4   r   rp   s     r   r7   z:PermittedSubtrees.accept_name.<locals>.<genexpr>.<genexpr>9  s'      AATDDLAAAAAAr   N)any)r4   trees_in_generationrp   s     r   r7   z0PermittedSubtrees.accept_name.<locals>.<genexpr>8  sU        ' AAAA-@AAAAA     r   F)r:   r<   r   r   rS   rl   rp   s     `r   accept_namezPermittedSubtrees.accept_name2  ss    
	    +3DK	4J+K+K      # 	 	 	55	s   26 
AArz   c                      	 t           fdt          |          D                       \  }}t          ||          S # t          $ r t                      cY S w xY w)Nc              3   R   K   | ]!\  }}                     ||          ||fV  "d S r/   )r   r4   rl   rp   rS   s      r   r7   z0PermittedSubtrees.accept_cert.<locals>.<genexpr>C  sV       3 3#It''	4883D!3 3 3 3 3 3r   r   nextr   r   StopIterationrS   rz   r   r   s   `   r   accept_certzPermittedSubtrees.accept_cert?      
	4.2 3 3 3 3'?'E'E3 3 3 / /+|
 2"3,     	4 	4 	4133333	4   ;? AAN)r   r   r   PKIXSubtreesro   r   rN   r   r   r   Certificater   r   r   r   r   r   r     s        <    &9L 9 9 9 9
_ t    4$4	'4 4 4 4 4 4r   r   c                   P    e Zd ZdefdZdefdZdedefdZde	j
        defd	Zd
S )ExcludedSubtreesinitial_excluded_subtreesc                 L    d |                                 D             | _        d S )Nc                 4    i | ]\  }}|t          |          S r   r   )r4   rl   tree_sets      r   r   z-ExcludedSubtrees.__init__.<locals>.<dictcomp>U  s4     %
 %
 %
#	8 s8}}%
 %
 %
r   )r   r   )rS   r   s     r   ro   zExcludedSubtrees.__init__P  s2    
%
 %
'@'F'F'H'H%
 %
 %
r   r   c                 z    |                                 D ]%\  }}| j        |                             |           &d S r/   )r   r   update)rS   r   rl   new_excludeds       r   
union_withzExcludedSubtrees.union_withZ  sF    ',{{}} 	8 	8#I|K	")),7777	8 	8r   rl   r   c                 t    	 t          fd| j        |         D                       S # t          $ r Y dS w xY w)Nc              3       K   | ]}|v V  	d S r/   r   r   s     r   r7   z/ExcludedSubtrees.reject_name.<locals>.<genexpr>a  s'      GGtt|GGGGGGr   T)r   r   r   r   s     `r   reject_namezExcludedSubtrees.reject_name_  sT    	GGGGI0FGGGGGG" 	 	 	44	s   %) 
77rz   c                      	 t           fdt          |          D                       \  }}t          ||          S # t          $ r t                      cY S w xY w)Nc              3   R   K   | ]!\  }}                     ||          ||fV  "d S r/   )r   r   s      r   r7   z/ExcludedSubtrees.accept_cert.<locals>.<genexpr>i  sV       3 3#It##It443D!3 3 3 3 3 3r   r   r   r   s   `   r   r   zExcludedSubtrees.accept_certe  r   r   N)r   r   r   r   ro   r   rN   r   r   r   r   r   r   r   r   r   r   r   O  s        
, 
 
 
 
8 8 8 8 8
_ t    4$4	'4 4 4 4 4 4r   r   c                  $    d t           D             S )Nc                 F    i | ]}|t                               |          hS r   )r   r   r4   rl   s     r   r   z.default_permitted_subtrees.<locals>.<dictcomp>v  s;        	K..y99:  r   rN   r   r   r   default_permitted_subtreesr   u  s#     (   r   c                  $    d t           D             S )Nc                 ,    i | ]}|t                      S r   r   r   s     r   r   z-default_excluded_subtrees.<locals>.<dictcomp>}  s    >>>Isuu>>>r   r   r   r   r   default_excluded_subtreesr   |  s    >>o>>>>r   ):r[   loggingdataclassesr   	ipaddressr   r   typingr   r   r   r	   r
   r   r   
asn1cryptor   uritoolsr   	getLoggerr   r%   
ValueErrorr   rg   r   r!   r+   r0   r?   rF   rh   rL   EnumrN   ra   r^   r_   rc   rP   r   rk   GeneralNamery   r   r   r   r   r   r   r   GeneralSubtreesr   r   r   r   r   r   r   r   r   <module>r     s7     ! ! ! ! ! ! . . . . . . . . G G G G G G G G G G G G G G G G G G            		8	$	$	 	 	 	 	* 	 	 	'# '3 '4 ' ' ' '  "0C 0 0 0 0 0 0	C 	 	 	 	 	
Mc 
M# 
M 
M 
M 
M	 $)    , , , , ,di , , ,2 "$9!4//1B	 1 1 1 1 12 1 1 1
4#3    04#3 0 0 0 0(N N N N N N N N* $/@ /@ /@ /@ /@ /@ /@ /@f OS%556J(49"5 J, J J J J
8K0 
\ 
 
 
 
t';     L L L L L L L L.34 34 34 34 34 34 34 34l#4 #4 #4 #4 #4 #4 #4 #4LL    ?< ? ? ? ? ? ?r   