U
    ;gn3                     @   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eeed	d
dZdd ZeeedddZeedddZeedddZejejd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)ej*dddZ+ej,dd d!Z-G d"d# d#Z.ed$d%G d&d' d'Z/e	e"ee/ f Z0e
ej e0d(d)d*Z1e
e/ e0d+d,d-Z2ej3e0d.d/d0Z4G d1d2 d2Z5G d3d4 d4Z6G d5d6 d6Z7e0d7d8d9Z8e0d7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   D/tmp/pip-unpacked-wheel-hgp_x7fx/pyhanko_certvalidator/name_trees.pyr      s   r   )	base_host
other_hostreturnc                 C   s:   | d dkr.| | \}}}t|o,t| S || kS d S )Nr   .)
rpartitionbool)r   r   pre_postr   r   r   host_tree_contains   s    r   c                 C   s^   t |  }|rt|ttfrZ|d k	r2d| 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   Zgethost
isinstancer   r   loggerwarningr   )Zcand_uriZ	cand_hostZhost_errmsgr   r   r   _host_regname   s    
r"   )baseotherr   c                 C   s   t |}t| |S N)r"   r   )r#   r$   r   r   r   r   uri_tree_contains,   s    r&   )r#   r$   c                 C   sX   |  d}| d}t|t|k r(dS t|t|koVtdd tt|t|D S )Nr   Fc                 s   s   | ]\}}||kV  qd S r%   r   .0xyr   r   r   	<genexpr>9   s    z$dns_tree_contains.<locals>.<genexpr>)splitlenallzipreversed)r#   r$   Zbase_labelsZother_labelsr   r   r   dns_tree_contains2   s    

r1   c                 C   s:   |  d\}}}| d\}}}|r,| |kS t||S d S )N@)r   r   )r#   r$   Zbase_mailboxr   Zbase_host_or_domainZother_mailboxZother_host_or_domainr   r   r   email_tree_contains>   s
    r3   c                 C   s4   | j }|j }t|t|ko2tdd t||D S )Nc                 s   s   | ]\}}||kV  qd S r%   r   r'   r   r   r   r+   O   s    z(dirname_tree_contains.<locals>.<genexpr>)chosenr-   r.   r/   )r#   r$   Zbase_rdn_sequenceZother_rdn_sequencer   r   r   dirname_tree_containsK   s
    r5   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eeeeejf eeejf gef  dddZed dddZdS )GeneralNameTyper   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   r   r   enumautoZ
OTHER_NAMERFC822_NAMEDNS_NAMEZX400_ADDRESSDIRECTORY_NAMEZEDI_PARTY_NAMEUNIFORM_RESOURCE_IDENTIFIERZ
IP_ADDRESSZREGISTERED_IDpropertyr	   r   r   strr   Namer   r<   classmethodrA   r   r   r   r   r6   W   s    "r6   c                       s"   e Zd Zed fddZ  ZS )UnsupportedNameTypeError)	name_typec                    s   t  |j  d S r%   )super__init__namelower)r;   rM   	__class__r   r   rO   x   s    z!UnsupportedNameTypeError.__init__)r   r   r   r6   rO   __classcell__r   r   rR   r   rL   w   s   rL   )gnamec                 C   s*   t | j}| j}|t jkr"|j}||fS r%   )r6   rA   rP   r4   rF   native)rU   Z
gname_typevaluer   r   r   _interpret_general_name|   s
    
rX   )certc                 c   sz   t | jjrtj| jfV  | j}|d krb| jjD ].}|D ]$}|d jdkr8tj|d jfV  q8q0n|D ]}t|V  qfd S )NtypeZemail_addressrW   )	r-   subjectr4   r6   rF   Zsubject_alt_name_valuerV   rD   rX   )rY   Zsubject_alt_namesrdnZ	name_pairrP   r   r   r   _enumerate_names_in_cert   s    r]   c                   @   s@   e Zd Zeeejf dddZedd Z	dd Z
dd	 Zd
S )_StringOrNamerW   c                 C   s
   || _ d S r%   r_   )r;   rW   r   r   r   rO      s    z_StringOrName.__init__c                 C   s*   | j }t|tjrd| fS d|fS d S )Nr      )rW   r   r   rJ   dump)r;   valr   r   r   _code   s    z_StringOrName._codec                 C   s
   t | jS r%   )hashrc   r:   r   r   r   __hash__   s    z_StringOrName.__hash__c                 C   s   t |to| j|jkS r%   )r   r^   rc   )r;   r$   r   r   r   __eq__   s    z_StringOrName.__eq__N)r   r   r   r   rI   r   rJ   rO   rH   rc   re   rf   r   r   r   r   r^      s
   
r^   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< e
eejf eddd	Zeee
eejf d
ddZed dddZeed dddZdS )NameSubtreerM   	tree_baser   minNmax)itemr   c                 C   sX   | j d krdS | jdks"| jd k	r*td| jj}|d krJt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 )ri   rj   rk   NotImplementedErrorrM   r<   rW   )r;   rl   checkerr   r   r   __contains__   s    

zNameSubtree.__contains__rM   rP   c                 C   s   t |t|dS )NrM   ri   )rh   r^   )r?   rM   rP   r   r   r   	from_name   s    zNameSubtree.from_namer7   c                 C   s4   |d }t |\}}t|t||d j|d jdS )Nr#   Zminimummaximum)rj   rk   )rX   rh   r^   rV   )r?   subtreerU   rM   Zname_objr   r   r   from_general_subtree   s    z NameSubtree.from_general_subtreerM   r   c                 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:
        Nrq   )rh   )r?   rM   r   r   r   universal_tree   s    	zNameSubtree.universal_tree)r   r   r   r6   __annotations__r	   r^   rj   intrk   r   rI   r   rJ   r   ro   rK   rr   ru   rw   r   r   r   r   rh      s   

rh   )namesr   c                    s(   t jddd tj fdd| D iS )NrP   c                 S   s   t jtj| dS )Nrp   )rh   rr   r6   rF   r{   r   r   r   _subtree   s     z(x509_names_to_subtrees.<locals>._subtreec                    s   h | ]} |qS r   r   )r(   nr|   r   r   	<setcomp>   s     z)x509_names_to_subtrees.<locals>.<setcomp>)r   rJ   r6   rF   )rz   r   r~   r   x509_names_to_subtrees   s    r   )treesr   c              	   C   sH   i }| D ]:}z||j  | W q tk
r@   |h||j < Y qX q|S r%   )rM   addKeyError)r   resulttreer   r   r   _group_subtrees   s    r   )subtreesr   c                 C   s   t dd | D S )Nc                 s   s   | ]}t |V  qd S r%   )rh   ru   )r(   rt   r   r   r   r+      s    z+process_general_subtrees.<locals>.<genexpr>)r   )r   r   r   r   process_general_subtrees   s    r   c                   @   sB   e Zd Zd	ee eeejdf d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;   r   r   r   r   r   rO     s    z'NameConstraintValidationResult.__init__c                 C   s
   | j d kS r%   )r   r:   r   r   r   __bool__  s    z'NameConstraintValidationResult.__bool__c                 C   sD   | j d k	st| j}t|tjr&|j}| j j }d| d| dS )Nz
The name 'z
' of type z is not allowed.)	r   AssertionErrorr   r   r   rJ   Zhuman_friendlyrP   rQ   )r;   Zname_strrM   r   r   r   error_message  s    z,NameConstraintValidationResult.error_message)NN)r   r   r   r	   r6   r   rI   r   rJ   rO   r   rH   r   r   r   r   r   r     s     r   c                   @   sJ   e Zd ZedddZedddZeeddd	Ze	j
ed
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   )setr9   r(   rM   r   r   r   
<dictcomp>'  s    z.PermittedSubtrees.__init__.<locals>.<dictcomp>)r6   _trees)r;   r   r   r   r   r   rO     s    
zPermittedSubtrees.__init__r   c                 C   s&   |  D ]\}}| j| | qd S r%   )itemsr   append)r;   r   rM   Znew_permittedr   r   r   intersect_with-  s    z PermittedSubtrees.intersect_withrv   c                    s>   z"t  fddt| j| D W S  tk
r8   Y dS X d S )Nc                 3   s$   | ]}t  fd d|D V  qdS )c                 3   s   | ]} |kV  qd S r%   r   r(   r   r{   r   r   r+   9  s     z:PermittedSubtrees.accept_name.<locals>.<genexpr>.<genexpr>N)any)r(   Ztrees_in_generationr{   r   r   r+   8  s   z0PermittedSubtrees.accept_name.<locals>.<genexpr>F)r.   r0   r   r   r;   rM   rP   r   r{   r   accept_name2  s    
zPermittedSubtrees.accept_namerY   r   c                    sL   z,t  fddt|D \}}t||dW S  tk
rF   t  Y S X d S )Nc                 3   s&   | ]\}}  ||s||fV  qd S r%   )r   r(   rM   rP   r:   r   r   r+   C  s   z0PermittedSubtrees.accept_cert.<locals>.<genexpr>r   nextr]   r   StopIterationr;   rY   r   r   r   r:   r   accept_cert?  s     zPermittedSubtrees.accept_certN)r   r   r   PKIXSubtreesrO   r   r6   r   r   r   Certificater   r   r   r   r   r   r     s   r   c                   @   sJ   e Zd ZedddZedddZeeddd	Ze	j
ed
ddZdS )ExcludedSubtrees)initial_excluded_subtreesc                 C   s   dd |  D | _d S )Nc                 S   s   i | ]\}}|t |qS r   r   )r(   rM   Ztree_setr   r   r   r   U  s    z-ExcludedSubtrees.__init__.<locals>.<dictcomp>)r   r   )r;   r   r   r   r   rO   P  s    zExcludedSubtrees.__init__r   c                 C   s&   |  D ]\}}| j| | qd S r%   )r   r   update)r;   r   rM   Znew_excludedr   r   r   
union_withZ  s    zExcludedSubtrees.union_withrv   c                    s:   zt  fdd| j| D W S  tk
r4   Y dS X d S )Nc                 3   s   | ]} |kV  qd S r%   r   r   r{   r   r   r+   a  s     z/ExcludedSubtrees.reject_name.<locals>.<genexpr>T)r   r   r   r   r   r{   r   reject_name_  s    zExcludedSubtrees.reject_namer   c                    sL   z,t  fddt|D \}}t||dW S  tk
rF   t  Y S X d S )Nc                 3   s&   | ]\}}  ||r||fV  qd S r%   )r   r   r:   r   r   r+   i  s   z/ExcludedSubtrees.accept_cert.<locals>.<genexpr>r   r   r   r   r:   r   r   e  s     zExcludedSubtrees.accept_certN)r   r   r   r   rO   r   r6   r   r   r   r   r   r   r   r   r   r   r   O  s   
r   r7   c                   C   s   dd t D S )Nc                 S   s   i | ]}|t |hqS r   )rh   rw   r   r   r   r   r   v  s    z.default_permitted_subtrees.<locals>.<dictcomp>r6   r   r   r   r   default_permitted_subtreesu  s    r   c                   C   s   dd t D S )Nc                 S   s   i | ]}|t  qS r   r   r   r   r   r   r   }  s      z-default_excluded_subtrees.<locals>.<dictcomp>r   r   r   r   r   default_excluded_subtrees|  s    r   ):rB   loggingdataclassesr   	ipaddressr   r   typingr   r   r   r   r	   r
   r   Z
asn1cryptor   Zuritoolsr   	getLoggerr   r   
ValueErrorr   rI   r   r   r"   r&   r1   r3   rJ   r5   Enumr6   rF   rD   rE   rG   r8   rm   rL   GeneralNamerX   r   r]   r^   rh   r   r   r   ZGeneralSubtreesr   r   r   r   r   r   r   r   r   r   <module>   sN   $

    
3	6&