
    Wwg/                         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
j0                  f   Z G d d      Zy)    N)	dataclass)	FrozenSetIterableIteratorOptionalUnion)cmsx509   )
AAControls)	AuthorityAuthorityWithCertCertTrustAnchorTrustAnchor)get_ac_extension_valueget_issuer_dnT)frozenc                   0    e Zd ZU eed<   	 eed<   	 eed<   y)QualifiedPolicyissuer_domain_policy_iduser_domain_policy_id
qualifiersN)__name__
__module____qualname__str__annotations__	frozenset     Q/var/www/horilla/myenv/lib/python3.12/site-packages/pyhanko_certvalidator/path.pyr   r      s'        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!jD                  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+y)#ValidationPathza
    Represents a path going towards an end-entity certificate or attribute
    certificate.
    N_qualified_policiestrust_anchorintermleafc                 ^    |r|st        d      t        |      | _        || _        || _        y )Nz-Leafless paths cannot have intermediate certs)
ValueErrorlist_interm_root_leaf)selfr%   r&   r'   s       r!   __init__zValidationPath.__init__1   s.     $LMMF|!

r    returnc                     | j                   S N)r,   r.   s    r!   r%   zValidationPath.trust_anchor=   s    zzr    c                     | j                   j                  }t        |t              r|j                  S | j
                  r| j
                  d   S t        | j                  t        j                        r| j                  S y)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   sb     zz##d-.###\\<<?"

D$4$45:: 6r    c                     | j                   | j                   S | j                  s0t        | j                  t              r| j                  j
                  S y)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   s?     ::!::*TZZ"I::)))r    c                     | j                   }t        |t        j                        r|j                  j
                  S t        |t        j                        ryy )Nz<Attribute certificate>)r'   r6   r
   r8   subjecthuman_friendlyr	   AttributeCertificateV2r.   r'   s     r!   describe_leafzValidationPath.describe_leafi   sA    yydD,,-<<...c889,r    c                 T    | j                   }t        |t        j                        r|S y)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%     yydD,,-Kr    c                 6    | j                         }|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              #   x   K   | j                   j                   | j                  D ]  }t        |        yw)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   s6      jj"""LL 	*D#D))	*s   8:rG   c                 6   t        |      }t        |t        j                        r|j                  }nt        |d      }|r|d   j                  nd}| j                         D ]+  }|j                  |k(  s|j                  }|r|r||k7  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,,-//C,T3MNG6='*+224C..0 	!I~~,!((SUc\  	! D
 	
r    new_leafc                    t        | j                  t              rE| j                  j                  j                  |j                  k(  rt        | j                  g |      S | j                  }d}t        |      D ]"  \  }}|j                  |j                  k(  s |} 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    ( djj/2zz%%33t7I7II%djj(KK
%e, 	LE5""d&8&88"
	
 HIIJJu%5zA~6X
 	
r    c                    d}| j                   j                  j                  |      r?|j                  dk(  rt	        | j
                  g d      S t	        | j
                  g |      S | j                  }t        |      D ]X  \  }}|j                  |j                  k(  s |j                  r*|j                  r|j                  |j                  k(  sR|} n|} 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7* &djj$GG%djj$GG %e, 	LE5}}+''D,I,I++t/L/LL',#(L	 H  djj%0B,2B*C$OOr    c                     | j                   d d  }| j                  r|j                  | j                         t        | j                  ||      S )Nr%   r&   r'   )r+   r-   appendr#   r,   )r.   rG   	new_certss      r!   copy_and_appendzValidationPath.copy_and_append  s@    LLO	::TZZ(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  sP     t||!#||CR0$,,r2BH
JX
 	
r    c                     || _         y 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 cg c]  }t        j                  |       }}t        d |D              }|syt        fd|D              S c c}w )Nc              3   $   K   | ]  }|d u 
 y wr2   r   ).0xs     r!   	<genexpr>z2ValidationPath.aa_attr_in_scope.<locals>.<genexpr>+  s     Mq}Ms   Tc              3   D   K   | ]  }||j                          y wr2   )accept)r|   ctrlry   s     r!   r~   z2ValidationPath.aa_attr_in_scope.<locals>.<genexpr>5  s*       #	 G$s    )r   read_extension_valueanyall)r.   ry   rG   aa_controls_extensionsaa_controls_useds    `   r!   aa_attr_in_scopezValidationPath.aa_attr_in_scope'  sd    >B"
6:J++D1"
 "
 M6LMM  2  "
s   Ac                 R    t        | j                        | j                  rdz   S dz   S )Nr   r   )ro   r+   r-   r3   s    r!   pkix_lenzValidationPath.pkix_len=  s$    4<< A;;;;r    c                      d| j                   z   S )Nr   )r   r3   s    r!   __len__zValidationPath.__len__A  s    4==  r    c                    |dkD  rGt        | 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  su    7$,,'!+Gg~$**"8zz!<<a((

O4::))) 7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  sr     zz## 
41B C  	
 zz)$0@0@ATGr	y$,,	BBr    c                 &    | j                  d      S )NT)r   )r   r3   s    r!   __iter__zValidationPath.__iter__g  s     D11r    c                     t        |t              sy| j                  |j                  k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S )NF)r6   r#   r%   r+   r-   )r.   others     r!   __eq__zValidationPath.__eq__l  sR    %0!3!33 *-*

ekk)	
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
!
 ))*
 tn	
 k    , htn   x} (4+;+;"<  d&&  *(9"5 *
4 
@#
4+;+; #
t #
L.P$2B2B .P`
D 

,(HY-G$H ((@(@ T , < <!9Ct C9I9I0J C&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      sm     ! A A   "  8 $  " Ts999:L
 L
r    