
     h*              	           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dS )    )defaultdict)IterableOptionalSet)x509   )ValProcState)PathValidationErrorvalid_policy_treePolicyTreeRootdepthany_policy_uninhibitedreturnc                 *   d}t                      }| D ]}|d         j        }|dk    r|}|                    |           |d         }d}	d}
|                    |dz
            D ]3}|j        dk    r|}
||j        vrd}	|                    |||h           4|	s|
r|
                    |||h           |rI|rG|                    |dz
            D ].}|j        D ]$}||vr|                    ||d         |h           %/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                ]/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/pyhanko_certvalidator/policy_tree.pyupdate_policy_treer%   
   s    O!ee '  "#67>,,$O##$5666"#67  &..uqy99 	 	D L00$(! (@@@"ONN!#47H6I   
  	#4 	''!#47H6I  
  1 %..uqy99 	 	D.2.F  *-5LLLNN2'(;<34   ++<eaiHH    c                     |                      |          D ]#}|j        s|j                            |           $| j        sd } | S N)walk_upchildrenparentremove_child)r   r   r"   s      r$   r   r   G   sW    !))%00 + +} 	+K$$T***% ! r&   mappings
proc_statec                    t          t                    }| D ]n}|d         j        }|d         j        }||                             |           |dk    s|dk    r+t	          j        d|                                 d|          o|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 '(? @ G'(,,-BCCC !L00$44%0;++--; ; ; 	   5 r&   policy_mapping_uninhibitedc                    |                                  D ]\  }}|rcd}d}|                    |          D ]#}|j        dk    r|}|j        |k    r	d}||_        $|s#|r!|j                            ||j        |           j|                    |          D ]'}|j        |k    r|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9I9I9K9K Q Q55% 	Q).&"O)22599 G G$44&*O$(<<<15./FD,- / &00(#1+   *22599 3 3$(<<<K,,T222 23Deai P Pr&   c                   	 t          |                                          		fd}t           |                      }	 t          d |                    |           D                       }|j        }|J |j        }|z
  D ]}|                    |||h           |                    |           n# t          $ r Y nw xY wt          || dz
            S )Nc               3   v   K   D ]2} | j         }|dk    s|v r|V  | j                            |            3d S )Nr   )r   r+   r,   )policy_node	policy_idacceptable_policiesvalid_policy_node_sets     r$   _filter_acceptablez7prune_unacceptable_policies.<locals>._filter_acceptable   sc      0 	= 	=K#0IL((I9L,L,L"//<<<<	= 	=r&   c              3   0   K   | ]}|j         d k    |V  dS )r   N)r   ).0r@   s     r$   	<genexpr>z.prune_unacceptable_policies.<locals>.<genexpr>   s<       0
 0
'<77 77770
 0
r&   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      sE      1 I I K KLL= = = = = = 113344
+/ 0
 0
099+FF0
 0
 0
 ,
 ,

 +1***)7!47K!K 	 	%%!>4E3F    	$$%56666    /qAAAs   A3B6 6
CCc                   p    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dS )r   zH
    A generic policy tree node, used for the root node in the tree
    c                 P    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   r:   r   roots        r$   init_policy_treezPolicyTreeRoot.init_policy_tree   s+     |]4GHHHr&   c                 "    d | _         g | _        d S r(   )r+   r*   )selfs    r$   __init__zPolicyTreeRoot.__init__   s    r&   c                 j    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)rX   r   r:   r   childs        r$   r   zPolicyTreeRoot.add_child   s9     |]<OPPU#####r&   c                 :    | j                             |           dS )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|V  |                    |dz
            D ]}|V  -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   rX   r   r]   
grandchilds       r$   r   zPolicyTreeRoot.at_depth   sr       $-(( 	% 	%Ezz"'..";"; % %J$$$$$%		% 	%r&   c              #      K   t          | j                  D ]+}|dk    r|                    |dz
            D ]}|V  |V  ,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)rb   r*   r)   rc   s       r$   r)   zPolicyTreeRoot.walk_up  sm       $-(( 	 	Ezz"'--	":": % %J$$$$$KKKK		 	r&   c              #   p   K   | j         D ]+}|V  |j        dk    r|                                E d{V  ,dS )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!  s_       ] 	; 	;EKKK!\11 88:::::::::	; 	;r&   N)__name__
__module____qualname____doc__classmethodrV   rY   r   r,   r   r   r)   rH    r&   r$   r   r      s            ["  $ $ $$$ $ $%*:!; % % % %$  &	;2B)C 	; 	; 	; 	; 	; 	;r&   c                   L     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                 r    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)superrY   r   r:   r   )rX   r   r:   r   	__class__s       r$   rY   zPolicyTreeNode.__init__2  s9      	(*#6   r&   c              #   0   K   | }||V  |j         }|d S d S r(   )r+   )rX   r"   s     r$   path_to_rootzPolicyTreeNode.path_to_rootH  s5      JJJ;D r&   )rg   rh   ri   rj   strr   PolicyQualifierInfosr   rY   rr   __classcell__)rp   s   @r$   r[   r[   -  sy         77 07 !X	7 7 7 7 7 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 /B /B /Bde; e; e; e; e; e; e; e;P    ^     r&   