
    Wwg*              	           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Zd Zdeej                      de	fdZd	edefdZded   fdZ G d d      Z G d de      Zy)    )defaultdict)IterableOptionalSet)x509   )ValProcState)PathValidationErrorvalid_policy_treePolicyTreeRootdepthany_policy_uninhibitedreturnc                    d}t               }| D ]  }|d   j                  }|dk(  r|}|j                  |       |d   }d}	d}
|j                  |dz
        D ]8  }|j                  dk(  r|}
||j
                  vr#d}	|j                  |||h       : |	r|
s|
j                  |||h        |rH|rF|j                  |dz
        D ]/  }|j
                  D ]  }||vs|j                  ||d   |h         1 t        ||dz
        }|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_identifiers                X/var/www/horilla/myenv/lib/python3.12/site-packages/pyhanko_certvalidator/policy_tree.pyupdate_policy_treer%   
   sl    O!e ' "#67>>,$O##$56"#67  &..uqy9 	D  L0$(! (@(@@"ONN!#47H6I	 #4''!#47H6I7@ 1%..uqy9 	D.2.F.F *-5LLNN2'(;<34	 ++<eaiH    c                     | j                  |      D ]*  }|j                  r|j                  j                  |       , | j                  sd } | S N)walk_upchildrenparentremove_child)r   r   r"   s      r$   r   r   G   sL    !))%0 +}}KK$$T*+ %% r&   mappings
proc_statec                     t        t              }| D ]f  }|d   j                  }|d   j                  }||   j                  |       |dk(  s|dk(  s@t	        j
                  d|j                          d|       |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mappingr0   r1   s         r$   enumerate_policy_mappingsr6   P   s     S!J &'=>EE '(? @ G G'(,,-BC !L0$4%00:++-. /:; 	 $ r&   policy_mapping_uninhibitedc                    | j                         D ]  \  }}|rrd}d}|j                  |      D ],  }|j                  dk(  r|}|j                  |k(  s$d}||_        . |rO|sR|j                  j                  ||j                  |       z|j                  |      D ]-  }|j                  |k(  s|j                  j                  |       / t        ||dz
        } |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   )	r4   r   r   r7   r0   subject_domain_policiesissuer_domain_policy_matchr   r"   s	            r$   apply_policy_mappingr=   n   s    :D9I9I9K Q55%).&"O)2259 G$$4&*O$$(<<15./FD,G ./&&00(#11+ *2259 3$$(<<KK,,T23 !33Deai P3Q4 r&   c                 z  	 t        |j                               		fd}t         |             }	 t        d |j                  |       D              }|j                  }|J |j
                  }|z
  D ]  }|j                  |||h        |j                  |       t        || dz
        S # t        $ r Y w xY w)Nc               3      K   D ]7  } | j                   }|dk(  s|v r| | j                  j                  |        9 y w)Nr   )r   r+   r,   )policy_node	policy_idacceptable_policiesvalid_policy_node_sets     r$   _filter_acceptablez7prune_unacceptable_policies.<locals>._filter_acceptable   sJ     0 	=K#00IL(I9L,L""//<	=s   >Ac              3   >   K   | ]  }|j                   d k(  r|  yw)r   N)r   ).0r@   s     r$   	<genexpr>z.prune_unacceptable_policies.<locals>.<genexpr>   s&      0
''<7 0
s   r   )
r   nodes_in_current_domainnextr   r+   r:   r   r,   StopIterationr   )
path_lengthr   rB   rD   valid_and_acceptablefinal_any_policywildcard_parentwildcard_qualsacceptable_policyrC   s
     `      @r$   prune_unacceptable_policiesrQ      s       1 I I KL= 134
+/ 0
099+F0
 ,

 +11***)77!47K!K 	%%!>4E3F	
 	$$%56
 /qAA	  s   A,B. .	B:9B:c                   \    e Zd ZdZed        Zd Zd Zd Zde	d   fdZ
d	 Zde	d   fd
Zy)r   zH
    A generic policy tree node, used for the root node in the tree
    c                 @    t               }|j                  |||       |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   r:   r   roots        r$   init_policy_treezPolicyTreeRoot.init_policy_tree   s"     |]4GHr&   c                      d | _         g | _        y r(   )r+   r*   )selfs    r$   __init__zPolicyTreeRoot.__init__   s    r&   c                 b    t        |||      }| |_        | j                  j                  |       y)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)rX   r   r:   r   childs        r$   r   zPolicyTreeRoot.add_child   s,     |]<OPU#r&   c                 :    | j                   j                  |       y)zq
        Removes a child from this node

        :param child:
            An instance of PolicyTreeNode
        N)r*   removerX   r]   s     r$   r,   zPolicyTreeRoot.remove_child   s     	U#r&   r   r[   c              #      K   t        | j                        D ])  }|dk(  r| |j                  |dz
        D ]  }|  + yw)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   rX   r   r]   
grandchilds       r$   r   zPolicyTreeRoot.at_depth   sN      $--( 	%Ez"'.."; %J$$%		%s   AAc              #      K   t        | j                        D ](  }|dk7  r|j                  |dz
        D ]  }|  | * yw)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)rb   r*   r)   rc   s       r$   r)   zPolicyTreeRoot.walk_up  sN      $--( 	Ez"'--	": %J$$%K		s   AAc              #      K   | j                   D ].  }| |j                  dk(  s|j                         E d{    0 y7 w)zy
        Returns a generator yielding all nodes in the tree that are children
        of an ``any_policy`` node.
        r   N)r*   r   rH   r`   s     r$   rH   z&PolicyTreeRoot.nodes_in_current_domain!  sE      ]] 	;EK!!\1 88:::	; ;s   #AAAAN)__name__
__module____qualname____doc__classmethodrV   rY   r   r,   r   r   r)   rH    r&   r$   r   r      sS      "$$$%*:!; %$&	;2B)C 	;r&   c                   P     e Zd ZdZdedej                  dee   f fdZd Z	 xZ
S )r[   zD
    A policy tree node that is used for all nodes but the root
    r   r:   r   c                 L    t         |           || _        || _        || _        y)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)superrY   r   r:   r   )rX   r   r:   r   	__class__s       r$   rY   zPolicyTreeNode.__init__2  s(      	(*#6 r&   c              #   <   K   | }|| |j                   }|y y wr(   )r+   )rX   r"   s     r$   path_to_rootzPolicyTreeNode.path_to_rootH  s'     J;;D s   )rg   rh   ri   rj   strr   PolicyQualifierInfosr   rY   rr   __classcell__)rp   s   @r$   r[   r[   -  s8    77 007 !X	7,r&   r[   N)collectionsr   typingr   r   r   
asn1cryptor   _stater	   errorsr
   intboolr%   r   PolicyMappingr6   r=   rQ   r   r[   rl   r&   r$   <module>r~      s    # * *    ':': : !	:
 :zt))*8D<"*-"KO"J/B/Bde; e;P^ r&   