o
    vhn3                     @   s  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eZG dd deZd	ed
edefddZdd ZdededefddZdedefddZdedefddZdejdejfddZ G dd de j!Z"e"j#e e"j$ee"j%ee"j&eiZ'G dd de(Z)dej*fdd Z+d!ej,fd"d#Z-G d$d% d%Z.ed&d'G d(d) d)Z/e	e"ee/ f Z0d*e
ej de0fd+d,Z1d-e
e/ de0fd.d/Z2d0ej3de0fd1d2Z4G d3d4 d4Z5G d5d6 d6Z6G d7d8 d8Z7de0fd9d:Z8de0fd;d<Z9dS )=    N)	dataclass)IPv4AddressIPv6Address)CallableDictIterableListOptionalSetUnion)x509)urisplitc                   @   s   e Zd ZdS )NameConstraintErrorN)__name__
__module____qualname__ r   r   ^/var/www/html/hyperkenya/venv/lib/python3.10/site-packages/pyhanko_certvalidator/name_trees.pyr      s    r   	base_host
other_hostreturnc                 C   s6   | d dkr| | \}}}t|ot| S || kS )Nr   .)
rpartitionbool)r   r   pre_postr   r   r   host_tree_contains   s   r   c                 C   s^   t |  }|rt|ttfr-|d u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msgr   r   r   _host_regname   s   
r&   baseotherc                 C   s   t |}t| |S N)r&   r   )r'   r(   r   r   r   r   uri_tree_contains,   s   
r*   c                 C   sX   |  d}| d}t|t|k rdS t|t|ko+tdd tt|t|D S )Nr   Fc                 s       | ]	\}}||kV  qd S r)   r   .0xyr   r   r   	<genexpr>9       
z$dns_tree_contains.<locals>.<genexpr>)splitlenallzipreversed)r'   r(   base_labelsother_labelsr   r   r   dns_tree_contains2   s   

r9   c                 C   s6   |  d\}}}| d\}}}|r| |kS t||S )N@)r   r   )r'   r(   base_mailboxr   base_host_or_domainother_mailboxother_host_or_domainr   r   r   email_tree_contains>   s
   
r?   c                 C   s4   | j }|j }t|t|kotdd t||D S )Nc                 s   r+   r)   r   r,   r   r   r   r0   O   r1   z(dirname_tree_contains.<locals>.<genexpr>)chosenr3   r4   r5   )r'   r(   base_rdn_sequenceother_rdn_sequencer   r   r   dirname_tree_containsK   s
   rC   c                   @   s   e Zd Ze Ze Ze Ze Ze Z	e Z
e Ze Ze Zedeeeeejf eeejf gef  fddZedddZdS )GeneralNameTyper   c                 C   s   t | d S r)   )_name_type_checkersgetselfr   r   r   check_membershipb   s   z GeneralNameType.check_membershipc                 C   s   t | | S r)   )getattrupper)clschoicer   r   r   from_choicej   s   zGeneralNameType.from_choiceN)r   rD   )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   rI   classmethodrN   r   r   r   r   rD   W   s$    "rD   c                       s"   e Zd Zdef fddZ  ZS )UnsupportedNameTypeError	name_typec                    s   t  |j  d S r)   )super__init__namelower)rH   r_   	__class__r   r   ra   x      z!UnsupportedNameTypeError.__init__)r   r   r   rD   ra   __classcell__r   r   rd   r   r^   w   s    r^   gnamec                 C   s*   t | j}| j}|t jkr|j}||fS r)   )rD   rN   rb   r@   rU   native)rh   
gname_typevaluer   r   r   _interpret_general_name|   s
   
rl   certc                 c   s~    t | jjrtj| jfV  | j}|d u r3| jjD ]}|D ]}|d jdkr/tj|d jfV  qqd S |D ]}t|V  q5d S )Ntypeemail_addressrk   )	r3   subjectr@   rD   rU   subject_alt_name_valueri   rR   rl   )rm   subject_alt_namesrdn	name_pairrb   r   r   r   _enumerate_names_in_cert   s   ru   c                   @   s@   e Zd Zdeeejf fddZedd Z	dd Z
dd	 Zd
S )_StringOrNamerk   c                 C   s
   || _ d S r)   )rk   )rH   rk   r   r   r   ra         
z_StringOrName.__init__c                 C   s&   | j }t|tjrd| fS d|fS )Nr      )rk   r   r   r\   dump)rH   valr   r   r   _code   s   z_StringOrName._codec                 C   s
   t | jS r)   )hashr{   rG   r   r   r   __hash__   rw   z_StringOrName.__hash__c                 C   s   t |to
| j|jkS r)   )r   rv   r{   )rH   r(   r   r   r   __eq__   rf   z_StringOrName.__eq__N)r   r   r   r   r[   r   r\   ra   rZ   r{   r}   r~   r   r   r   r   rv      s    
rv   T)frozenc                   @   s   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	d
Zedede
eejf fddZedddZededd fddZdS )NameSubtreer_   	tree_baser   minNmaxitemr   c                 C   sX   | j d u rdS | jdks| jd urtd| jj}|d u r%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   NotImplementedErrorr_   rI   rk   )rH   r   checkerr   r   r   __contains__   s   

zNameSubtree.__contains__rb   c                 C   s   t |t|dS )Nr_   r   )r   rv   )rL   r_   rb   r   r   r   	from_name   s   zNameSubtree.from_namec                 C   s4   |d }t |\}}t|t||d j|d jdS )Nr'   minimummaximum)r   r   )rl   r   rv   ri   )rL   subtreerh   r_   name_objr   r   r   from_general_subtree   s   z NameSubtree.from_general_subtreec                 C   s   t |ddS )z
        Tree that contains all names of a given type.

        :param name_type:
            The name type to use.
        :return:
        Nr   )r   )rL   r_   r   r   r   universal_tree   s   	zNameSubtree.universal_tree)r   r   )r   r   r   rD   __annotations__r	   rv   r   intr   r   r[   r   r\   r   r   r]   r   r   r   r   r   r   r   r      s   
 
r   namesc                    s(   dt jfdd tj fdd| D iS )Nrb   c                 S   s   t jtj| dS )N)r_   rb   )r   r   rD   rU   rb   r   r   r   _subtree   s   z(x509_names_to_subtrees.<locals>._subtreec                    s   h | ]} |qS r   r   )r-   nr   r   r   	<setcomp>       z)x509_names_to_subtrees.<locals>.<setcomp>)r   r\   rD   rU   )r   r   r   r   x509_names_to_subtrees   s   r   treesc              	   C   sD   i }| D ]}z
||j  | W q ty   |h||j < Y qw |S r)   )r_   addKeyError)r   resulttreer   r   r   _group_subtrees   s   r   subtreesc                 C   s   t dd | D S )Nc                 s   s    | ]}t |V  qd S r)   )r   r   )r-   r   r   r   r   r0      s    

z+process_general_subtrees.<locals>.<genexpr>)r   )r   r   r   r   process_general_subtrees   s   r   c                   @   sH   e Zd Z		d
dee deeejdf fddZ	dd Z
edd	 ZdS )NameConstraintValidationResultNfailing_name_typefailing_namec                 C   s   || _ || _d S r)   r   r   )rH   r   r   r   r   r   ra     s   
z'NameConstraintValidationResult.__init__c                 C   s
   | j d u S r)   )r   rG   r   r   r   __bool__  rw   z'NameConstraintValidationResult.__bool__c                 C   sD   | j d us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   r\   human_friendlyrb   rc   )rH   name_strr_   r   r   r   error_message  s   z,NameConstraintValidationResult.error_message)NN)r   r   r   r	   rD   r   r[   r   r\   ra   r   rZ   r   r   r   r   r   r     s    
r   c                   @   N   e Zd ZdefddZdefddZdedefd	d
Zde	j
defddZdS )PermittedSubtreesinitial_permitted_subtreesc                    s    fddt D }|| _d S )Nc                    s    i | ]}|t  |d gqS )r   )setrF   r-   r_   r   r   r   
<dictcomp>'  s    z.PermittedSubtrees.__init__.<locals>.<dictcomp>)rD   _trees)rH   r   r   r   r   r   ra     s   

zPermittedSubtrees.__init__r   c                 C   &   |  D ]\}}| j| | qd S r)   )itemsr   append)rH   r   r_   new_permittedr   r   r   intersect_with-     z PermittedSubtrees.intersect_withr_   r   c                    s8   zt  fddt| j| D W S  ty   Y dS w )Nc                 3   s&    | ]}t  fd d|D V  qdS )c                 3       | ]} |v V  qd S r)   r   r-   r   r   r   r   r0   9      z:PermittedSubtrees.accept_name.<locals>.<genexpr>.<genexpr>N)any)r-   trees_in_generationr   r   r   r0   8  s
    
z0PermittedSubtrees.accept_name.<locals>.<genexpr>F)r4   r6   r   r   rH   r_   rb   r   r   r   accept_name2  s   
zPermittedSubtrees.accept_namerm   c                    F   zt  fddt|D \}}t||dW S  ty"   t  Y S w )Nc                 3   s(    | ]\}}  ||s||fV  qd S r)   )r   r-   r_   rb   rG   r   r   r0   C      

z0PermittedSubtrees.accept_cert.<locals>.<genexpr>r   nextru   r   StopIterationrH   rm   r   r   r   rG   r   accept_cert?     
zPermittedSubtrees.accept_certN)r   r   r   PKIXSubtreesra   r   rD   r   r   r   Certificater   r   r   r   r   r   r     s    r   c                   @   r   )ExcludedSubtreesinitial_excluded_subtreesc                 C   s   dd |  D | _d S )Nc                 S   s   i | ]	\}}|t |qS r   r   )r-   r_   tree_setr   r   r   r   U  s    z-ExcludedSubtrees.__init__.<locals>.<dictcomp>)r   r   )rH   r   r   r   r   ra   P  s   zExcludedSubtrees.__init__r   c                 C   r   r)   )r   r   update)rH   r   r_   new_excludedr   r   r   
union_withZ  r   zExcludedSubtrees.union_withr_   r   c                    s4   zt  fdd| j| D W S  ty   Y dS w )Nc                 3   r   r)   r   r   r   r   r   r0   a  r   z/ExcludedSubtrees.reject_name.<locals>.<genexpr>T)r   r   r   r   r   r   r   reject_name_  s
   zExcludedSubtrees.reject_namerm   c                    r   )Nc                 3   s(    | ]\}}  ||r||fV  qd S r)   )r   r   rG   r   r   r0   i  r   z/ExcludedSubtrees.accept_cert.<locals>.<genexpr>r   r   r   r   rG   r   r   e  r   zExcludedSubtrees.accept_certN)r   r   r   r   ra   r   rD   r   r   r   r   r   r   r   r   r   r   r   O  s    
r   c                   C      dd t D S )Nc                 S   s   i | ]	}|t |hqS r   )r   r   r   r   r   r   r   v  s    z.default_permitted_subtrees.<locals>.<dictcomp>rD   r   r   r   r   default_permitted_subtreesu  s   r   c                   C   r   )Nc                 S   s   i | ]}|t  qS r   r   r   r   r   r   r   }  r   z-default_excluded_subtrees.<locals>.<dictcomp>r   r   r   r   r   default_excluded_subtrees|  s   r   ):rO   loggingdataclassesr   	ipaddressr   r   typingr   r   r   r   r	   r
   r   
asn1cryptor   uritoolsr   	getLoggerr   r    
ValueErrorr   r[   r   r   r&   r*   r9   r?   r\   rC   EnumrD   rU   rR   rS   rW   rE   r   r^   GeneralNamerl   r   ru   rv   r   r   r   r   GeneralSubtreesr   r   r   r   r   r   r   r   r   r   <module>   sH    $


3	6&