
     h/                         d dl Z d dlmZ d dlmZmZmZmZmZ d dl	m
Z
mZ ddlmZ ddlmZmZmZmZ ddlmZmZ  ed	
           G d d                      Zeej        e
j        f         Z G d d          ZdS )    N)	dataclass)	FrozenSetIterableIteratorOptionalUnion)cmsx509   )
AAControls)	AuthorityAuthorityWithCertCertTrustAnchorTrustAnchor)get_ac_extension_valueget_issuer_dnT)frozenc                   2    e Zd ZU eed<   	 eed<   	 eed<   dS )QualifiedPolicyissuer_domain_policy_iduser_domain_policy_id
qualifiersN)__name__
__module____qualname__str__annotations__	frozenset     V/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/pyhanko_certvalidator/path.pyr   r      sH                r    r   c                   .   e Zd ZU dZdZeee                  ed<   dZ	de
deej                 dee         fdZede
fd	            Zed
             Zedee         fd            Zdee         fdZdeej                 fdZedej        fd            Zdee         fdZdefdZdej        defdZdej        fdZdefdZd"dZd Zdeee                  fdZ de!j"        de#fdZ$ed             Z%d Z&d Z'de#de(ej                 fdZ)d  Z*d! Z+dS )#ValidationPathza
    Represents a path going towards an end-entity certificate or attribute
    certificate.
    N_qualified_policiestrust_anchorintermleafc                 p    |r|st          d          t          |          | _        || _        || _        d S )Nz-Leafless paths cannot have intermediate certs)
ValueErrorlist_interm_root_leaf)selfr%   r&   r'   s       r!   __init__zValidationPath.__init__1   sC      	N$ 	NLMMMF||!



r    returnc                     | j         S N)r,   r.   s    r!   r%   zValidationPath.trust_anchor=   s
    zr    c                     | j         j        }t          |t                    r|j        S | j        r| j        d         S t          | j        t          j                  r| j        S dS )a  
        Returns the current beginning of the path - for a path to be complete,
        this certificate should be a trust root

        .. warning::
            This is a compatibility property, and will return the first non-root
            certificate if the trust root is not provisioned as a certificate.
            If you want the trust root itself (even when it doesn't have a
            certificate), use :attr:`trust_anchor`.

        :return:
            The first asn1crypto.x509.Certificate object in the path
        r   N)	r,   	authority
isinstancer   certificater+   r-   r
   Certificate)r.   roots     r!   firstzValidationPath.firstA   si     z#d-.. 	##\ 	<?"
D$455 	:	 	r    c                 |    | j         | j         S | j        s&t          | j        t                    r| j        j        S dS )a<  
        Returns the current leaf certificate (AC or public-key).
        The trust root's certificate will be returned if there is one and
        there are no other certificates in the path.

        If the trust root is certificate-less and there are no certificates,
        the result will be ``None``.
        N)r-   r+   r6   r,   r   r7   r3   s    r!   r'   zValidationPath.leafX   sA     :!: 	**TZ"I"I 	*:))tr    c                     | j         }t          |t          j                  r|j        j        S t          |t          j                  rdS d S )Nz<Attribute certificate>)r'   r6   r
   r8   subjecthuman_friendlyr	   AttributeCertificateV2r.   r'   s     r!   describe_leafzValidationPath.describe_leafi   sI    ydD,-- 	<..c899 	,,4r    c                 L    | j         }t          |t          j                  r|S dS )z
        Returns the current leaf certificate if it is an X.509 public-key
        certificate, and ``None`` otherwise.
        :return:
        N)r'   r6   r
   r8   r@   s     r!   get_ee_cert_safezValidationPath.get_ee_cert_safer   s*     ydD,-- 	K4r    c                 @    |                                  }|r|S t          )z
        Returns the last certificate in the path if it is an X.509 public-key
        certificate, and throws an error otherwise.

        :return:
            The last asn1crypto.x509.Certificate object in the path
        )rC   LookupErrorr.   certs     r!   lastzValidationPath.last   s(     $$&& 	Kr    c              #   \   K   | j         j        V  | j        D ]}t          |          V  dS )zU
        Iterate over all authorities in the path, including the trust root.
        N)r,   r5   r+   r   rF   s     r!   iter_authoritieszValidationPath.iter_authorities   sK       j""""L 	* 	*D#D))))))	* 	*r    rG   c                 4   t          |          }t          |t          j                  r|j        }n!t          |d          }|r|d         j        nd}|                                 D ]#}|j        |k    r|j	        }|r	|r||k    r|c S $t          d          )aK  
        Return the issuer of the cert specified, as defined by this path

        :param cert:
            A certificate to get the issuer of

        :raises:
            LookupError - when the issuer of the certificate could not be found

        :return:
            An asn1crypto.x509.Certificate object of the issuer
        authority_key_identifierkey_identifierN6Unable to find the issuer of the certificate specified)r   r6   r
   r8   rL   r   nativerJ   namekey_idrE   )r.   rG   issuer_nameakiaki_extr5   keyids          r!   find_issuing_authorityz%ValidationPath.find_issuing_authority   s     $D))dD,-- 	H/CC,T3MNNG6=G'*+224C..00 	! 	!I~,,!( S Uc\\    	 - D
 
 	
r    new_leafc                 b   t          | j        t                    r1| j        j        j        |j        k    rt          | j        g |          S | j        }d}t          |          D ]\  }}|j        |j        k    r|} n|t          d          t          | j        |d|dz            |          S )a  
        Remove all certificates in the path after the cert specified and return
        them in a new path.

        Internal API.

        :param cert:
            An asn1crypto.x509.Certificate object to find

        :param new_leaf:
            A new leaf certificate to append.

        :raises:
            LookupError - when the certificate could not be found

        :return:
            The current ValidationPath object, for chaining
        r&   r'   Nz(Unable to find the certificate specifiedr   )	r6   r,   r   r7   issuer_serialr#   r+   	enumeraterE   )r.   rG   rW   certs
cert_indexindexentrys          r!   truncate_to_and_appendz%ValidationPath.truncate_to_and_append   s    ( dj/22 	Lz%3t7III%dj(KKKK
%e,, 	 	LE5"d&888"
 9 HIIIJu%5zA~%56X
 
 
 	
r    c                    d}| j         j                            |          r9|j        dk    rt	          | j        g d          S t	          | j        g |          S | j        }t          |          D ]<\  }}|j        |j	        k    r'|j
        r|j        r|j
        |j        k    r|} n8|} n=|t          d          t	          | j        |d|dz            |          S )a  
        Remove all certificates in the path after the issuer of the cert
        specified, as defined by this path, and append a new one.

        Internal API.

        :param cert:
            A new leaf certificate to append.

        :raises:
            LookupError - when the issuer of the certificate could not be found

        :return:
            The current ValidationPath object, for chaining
        NmayberY   rN   r   )r'   )r%   r5   is_potential_issuer_ofself_signedr#   r,   r+   r[   r=   issuerrM   rL   rE   )r.   rG   issuer_indexr\   r^   r_   s         r!   truncate_to_issuer_and_appendz,ValidationPath.truncate_to_issuer_and_append   s&   "  &==dCC 	H7** &dj$GGGG%dj$GGGG %e,, 	 	LE5}++' D,I +t/LLL', M $)LE , H   dj%0B,2B0B*C$OOOOr    c                     | j         d d          }| j        r|                    | j                   t          | j        ||          S )Nr%   r&   r'   )r+   r-   appendr#   r,   )r.   rG   	new_certss      r!   copy_and_appendzValidationPath.copy_and_append  sQ    LO	: 	)TZ(((ID
 
 
 	
r    c                     t          | j                  dk    rt          | j        dd         | j        d         }}t          | j        ||          S )z
        Drop the leaf cert from this path and return a new path with the
        last intermediate certificate set as the leaf.
        r   Nri   )lenr+   
IndexErrorr#   r,   )r.   
new_intermrW   s      r!   copy_and_drop_leafz!ValidationPath.copy_and_drop_leaf  sZ     t|!!#|CRC0$,r2BH
JX
 
 
 	
r    c                     || _         d S r2   r$   )r.   policiess     r!   _set_qualified_policiesz&ValidationPath._set_qualified_policies!  s    #+   r    c                     | j         S r2   rt   r3   s    r!   qualified_policiesz!ValidationPath.qualified_policies$  s    ''r    attr_idc                     d | D             }t          d |D                       }|sdS t          fd|D                       S )Nc                 6    g | ]}t          j        |          S r   )r   read_extension_value).0rG   s     r!   
<listcomp>z3ValidationPath.aa_attr_in_scope.<locals>.<listcomp>(  s0     "
 "
 "
6:J+D11"
 "
 "
r    c              3      K   | ]}|d uV  	d S r2   r   )r}   xs     r!   	<genexpr>z2ValidationPath.aa_attr_in_scope.<locals>.<genexpr>+  s&      MMq}MMMMMMr    Tc              3   F   K   | ]}||                               V  d S r2   )accept)r}   ctrlry   s     r!   r   z2ValidationPath.aa_attr_in_scope.<locals>.<genexpr>5  sE         #	 G$$ $### r    )anyall)r.   ry   aa_controls_extensionsaa_controls_useds    `  r!   aa_attr_in_scopezValidationPath.aa_attr_in_scope'  s    "
 "
>B"
 "
 "
 MM6LMMMMM 	4     2     r    c                 B    t          | j                  | j        rdndz   S )Nr   r   )ro   r+   r-   r3   s    r!   pkix_lenzValidationPath.pkix_len=  s"    4<  $:AA;;r    c                     d| j         z   S )Nr   )r   r3   s    r!   __len__zValidationPath.__len__A  s    4=  r    c                     |dk    r;t          | j                  dz   }||k    r| j        | j        S | j        |dz
           S t          | j        t
                    r| j        j        S t          d          )Nr   r   zRoot has no certificate)ro   r+   r-   r6   r,   r   r7   rE   )r.   keyleaf_ixs      r!   __getitem__zValidationPath.__getitem__E  sx    77$,''!+Gg~~$*"8z!<a((
O44 	9:)) 7888r    include_rootc                     | j         j        }|rt          |t                    r|j        fnd}| j        }t          |t          j                  r|fnd}t          j	        || j
        |          S )z
        Iterate over the certificates in the path.

        :param include_root:
            Include the root (if it is supplied as a certificate)
        :return:
            An iterator.
        r   )r,   r5   r6   r   r7   r-   r
   r8   	itertoolschainr+   )r.   r   r9   	from_rootr'   	from_leafs         r!   
iter_certszValidationPath.iter_certsT  sz     z#  *41B C CT 	
 z)$0@AAITGGr	y$,	BBBr    c                 .    |                      d          S )NT)r   )r   r3   s    r!   __iter__zValidationPath.__iter__g  s     D111r    c                     t          |t                    sdS | j        |j        k    o| j        |j        k    o| j        |j        k    S )NF)r6   r#   r%   r+   r-   )r.   others     r!   __eq__zValidationPath.__eq__l  sN    %00 	5!33 *-*
ek)	
r    )r0   r#   ),r   r   r   __doc__r$   r   r   r   r   _path_aa_controlsr   r   r
   r8   Leafr/   propertyr%   r:   r'   r   rA   rC   rH   r   rJ   rV   r`   rg   rl   rr   rv   rx   r	   AttCertAttributeTypeboolr   r   r   r   r   r   r   r   r   r    r!   r#   r#   '   s         
 AE)O"<=DDD
!
 )*
 tn	
 
 
 
 k    X   X, htn    X x}    (4+;"<     d&    X*(9"5 * * * *
4 
 
 
 
@#
4+; #
t #
 #
 #
 #
L.P$2B .P .P .P .P`
D 
 
 
 

 
 
 
, , ,(HY-G$H ( ( ( ((@ T    , < < X<! ! !9 9 9Ct C9I0J C C C C&2 2 2

 
 
 
 
r    r#   )r   dataclassesr   typingr   r   r   r   r   
asn1cryptor	   r
   
asn1_typesr   r5   r   r   r   r   utilr   r   r   r8   r?   r   r#   r   r    r!   <module>r      sb       ! ! ! ! ! ! A A A A A A A A A A A A A A                 " " " " " "            8 7 7 7 7 7 7 7 $       " Ts99:L
 L
 L
 L
 L
 L
 L
 L
 L
 L
r    