o
    vh*                  	   @   s   d dl mZ d dlmZmZmZ d dlmZ ddlm	Z	 ddl
mZ ddd	ed
eded fddZdd Zdeej de	fddZd	edefddZded fddZG dd dZG dd deZdS )    )defaultdict)IterableOptionalSet)x509   )ValProcState)PathValidationErrorvalid_policy_treePolicyTreeRootdepthany_policy_uninhibitedreturnc                 C   s   d}t  }| D ]H}|d j}|dkr|}q|| |d }d}	d}
||d D ]}|jdkr2|}
||jvr8q)d}	||||h q)|	sO|
rO|
|||h q|rr|rr||d D ]}|jD ]}||vrp|||d |h q`q[t||d }|S )zO
    Internal method to update the policy tree during RFC 5280 validation.
    Npolicy_identifier
any_policypolicy_qualifiersFr   T)setnativeaddat_depthvalid_policyexpected_policy_set	add_child_prune_policy_tree)certificate_policiesr
   r   r   cert_any_policycert_policy_identifierspolicyr   r   policy_id_matchparent_any_policynodeexpected_policy_identifier r"   _/var/www/html/hyperkenya/venv/lib/python3.10/site-packages/pyhanko_certvalidator/policy_tree.pyupdate_policy_tree
   sL   





	r$   c                 C   s0   |  |D ]}|js|j| q| jsd } | S N)walk_upchildrenparentremove_child)r
   r   r    r"   r"   r#   r   G   s   r   mappings
proc_statec                 C   s`   t t}| D ]'}|d j}|d j}|| | |dks!|dkr-td|  d|q|S )z
    Internal function to process policy mapping extension values into
    a Python dictionary mapping issuer domain policies to the corresponding
    policies in the subject policy domain.
    issuer_domain_policysubject_domain_policyr   z(The path could not be validated because z/ contains a policy mapping for the "any policy")r   r   r   r   r	   
from_statedescribe_cert)r*   r+   
policy_mapmappingr,   r-   r"   r"   r#   enumerate_policy_mappingsP   s   

	r2   policy_mapping_uninhibitedc           	      C   s   |   D ]K\}}|r5d}d}||D ]}|jdkr|}|j|kr&d}||_q|s4|r4|j||j| q||D ]}|j|krG|j| q:t||d }q|S )z
    Internal function to apply the policy mapping to the current policy tree
    in accordance with the algorithm in RFC 5280.
    FNr   Tr   )	itemsr   r   r   r(   r   qualifier_setr)   r   )	r0   r
   r   r3   r,   subject_domain_policiesissuer_domain_policy_matchr   r    r"   r"   r#   apply_policy_mappingn   s0   


r8   c           	         s   t |  fdd}t | }z.tdd || D }|j}|d us(J |j} | D ]
}||||h q/|| W n	 tyI   Y nw t	|| d S )Nc                  3   s:    D ]} | j }|dks| v r|V  q| j|  qd S )Nr   )r   r(   r)   )policy_node	policy_idacceptable_policiesvalid_policy_node_setr"   r#   _filter_acceptable   s   z7prune_unacceptable_policies.<locals>._filter_acceptablec                 s   s    | ]
}|j d kr|V  qdS )r   N)r   ).0r9   r"   r"   r#   	<genexpr>   s    
z.prune_unacceptable_policies.<locals>.<genexpr>r   )
r   nodes_in_current_domainnextr   r(   r5   r   r)   StopIterationr   )	path_lengthr
   r<   r>   valid_and_acceptablefinal_any_policywildcard_parentwildcard_qualsacceptable_policyr"   r;   r#   prune_unacceptable_policies   s&   
	rJ   c                   @   s`   e Zd ZdZedd Zdd Zdd Zdd	 Zd
e	d fddZ
dd Zd
e	d fddZdS )r   zH
    A generic policy tree node, used for the root node in the tree
    c                 C   s   t  }|||| |S )aq  
        Accepts values for a PolicyTreeNode that will be created at depth 0

        :param valid_policy:
            A unicode string of a policy name or OID

        :param qualifier_set:
            An instance of asn1crypto.x509.PolicyQualifierInfos

        :param expected_policy_set:
            A set of unicode strings containing policy names or OIDs
        )r   r   )clsr   r5   r   rootr"   r"   r#   init_policy_tree   s   zPolicyTreeRoot.init_policy_treec                 C   s   d | _ g | _d S r%   )r(   r'   )selfr"   r"   r#   __init__   s   
zPolicyTreeRoot.__init__c                 C   s"   t |||}| |_| j| dS )ab  
        Creates a new PolicyTreeNode as a child of this node

        :param valid_policy:
            A unicode string of a policy name or OID

        :param qualifier_set:
            An instance of asn1crypto.x509.PolicyQualifierInfos

        :param expected_policy_set:
            A set of unicode strings containing policy names or OIDs
        N)PolicyTreeNoder(   r'   append)rN   r   r5   r   childr"   r"   r#   r      s   zPolicyTreeRoot.add_childc                 C   s   | j | dS )zq
        Removes a child from this node

        :param child:
            An instance of PolicyTreeNode
        N)r'   removerN   rR   r"   r"   r#   r)      s   zPolicyTreeRoot.remove_childr   rP   c                 c   s@    t | jD ]}|dkr|V  q||d D ]}|V  qqdS )z
        Returns a generator yielding all nodes in the tree at a specific depth

        :param depth:
            An integer >= 0 of the depth of nodes to yield

        :return:
            A generator yielding PolicyTreeNode objects
        r   r   N)listr'   r   rN   r   rR   
grandchildr"   r"   r#   r      s   zPolicyTreeRoot.at_depthc                 c   s>    t | jD ]}|dkr||d D ]}|V  q|V  qdS )aW  
        Returns a generator yielding all nodes in the tree at a specific depth,
        or above. Yields nodes starting with leaves and traversing up to the
        root.

        :param depth:
            An integer >= 0 of the depth of nodes to walk up from

        :return:
            A generator yielding PolicyTreeNode objects
        r   r   N)rU   r'   r&   rV   r"   r"   r#   r&     s   zPolicyTreeRoot.walk_upc                 c   s0    | j D ]}|V  |jdkr| E dH  qdS )zy
        Returns a generator yielding all nodes in the tree that are children
        of an ``any_policy`` node.
        r   N)r'   r   rA   rT   r"   r"   r#   rA   !  s   

z&PolicyTreeRoot.nodes_in_current_domainN)__name__
__module____qualname____doc__classmethodrM   rO   r   r)   r   r   r&   rA   r"   r"   r"   r#   r      s    

c                       s<   e Zd ZdZdedejdee f fddZdd Z	  Z
S )	rP   zD
    A policy tree node that is used for all nodes but the root
    r   r5   r   c                    s    t    || _|| _|| _dS )a$  
        :param valid_policy:
            A unicode string of a policy name or OID

        :param qualifier_set:
            An instance of asn1crypto.x509.PolicyQualifierInfos

        :param expected_policy_set:
            A set of unicode strings containing policy names or OIDs
        N)superrO   r   r5   r   )rN   r   r5   r   	__class__r"   r#   rO   2  s   

zPolicyTreeNode.__init__c                 c   s*    | }|d ur|V  |j }|d usd S d S r%   )r(   )rN   r    r"   r"   r#   path_to_rootH  s   zPolicyTreeNode.path_to_root)rX   rY   rZ   r[   strr   PolicyQualifierInfosr   rO   r`   __classcell__r"   r"   r^   r#   rP   -  s    rP   N)collectionsr   typingr   r   r   
asn1cryptor   _stater   errorsr	   intboolr$   r   PolicyMappingr2   r8   rJ   r   rP   r"   r"   r"   r#   <module>   s<    
=	

%
2h