o
    vh^"                     @   s   d dl Z d dlmZ d dlmZ d dlmZmZ ddlm	Z	 ddl
mZ edd	G d
d dZG dd de jZG dd dZdejdefddZG dd deZG dd deZG dd deZdS )    N)	dataclass)Optional)keysx509   )process_general_subtrees)PKIXValidationParamsT)frozenc                   @   sF   e Zd ZU dZdZed ed< 	 dZee ed< 	 dZ	ee ed< dS )TrustQualifierszY
    .. versionadded 0.20.0

    Parameters that allow a trust root to be qualified.
    Nr   standard_parametersmax_path_lengthmax_aa_path_length)
__name__
__module____qualname____doc__r   r   __annotations__r   intr    r   r   ]/var/www/html/hyperkenya/venv/lib/python3.10/site-packages/pyhanko_certvalidator/authority.pyr
      s   
 r
   c                   @   s~   e Zd ZdZedejfddZedej	fddZ
edd Zd	d
 Zdd Zedee fddZdejdefddZdS )	AuthorityzM
    .. versionadded:: 0.20.0

    Abstract authority, i.e. a named key.
    returnc                 C      t )z'
        The authority's name.
        NotImplementedErrorselfr   r   r   name/      zAuthority.namec                 C   r   )z-
        The authority's public key.
        r   r   r   r   r   
public_key6   r   zAuthority.public_keyc                 C   r   )zm
        A hashable unique identifier of the authority, used in ``__eq__``
        and ``__hash__``.
        r   r   r   r   r   hashable=   s   zAuthority.hashablec                 C   
   t | jS N)hashr    r   r   r   r   __hash__E      
zAuthority.__hash__c                 C   s   t |tsdS | j|jkS NF)
isinstancer   r    r   otherr   r   r   __eq__H   s   
zAuthority.__eq__c                 C   r   )z
        Key ID as (potentially) referenced in an authorityKeyIdentifier
        extension. Only used to eliminate non-matching trust anchors,
        never to retrieve keys or to definitively identify trust anchors.
        r   r   r   r   r   key_idN   s   zAuthority.key_idcertc                 C   s0   |j | jkrdS |jr| jr|j| jkrdS dS )z
        Function to determine whether this trust root could potentially be an
        issuer of a given certificate.
        This function is used during path building.

        :param cert:
            The certificate to evaluate.
        FT)issuerr   authority_key_identifierr+   r   r,   r   r   r   is_potential_issuer_ofW   s   	z Authority.is_potential_issuer_ofN)r   r   r   r   propertyr   Namer   r   PublicKeyInfor   r    r$   r*   r   bytesr+   Certificateboolr0   r   r   r   r   r   (   s    
r   c                   @   s^   e Zd ZdZ	ddedee fddZedefdd	Z	edefd
dZ
dd Zdd ZdS )TrustAnchorz
    Abstract trust root. A trust root is an authority with trust qualifiers.
    Equality of trust roots reduces to equality of authorities.
    N	authorityqualsc                 C      || _ || _d S r"   )
_authority_quals)r   r8   r9   r   r   r   __init__n   s   
zTrustAnchor.__init__r   c                 C      | j S r"   )r;   r   r   r   r   r8   t      zTrustAnchor.authorityc                 C   s   | j pt S )z0
        Qualifiers for the trust root.
        )r<   r
   r   r   r   r   trust_qualifiersx   s   zTrustAnchor.trust_qualifiersc                 C   s   t |to
|j| jkS r"   )r'   r7   r;   r(   r   r   r   r*      s   

zTrustAnchor.__eq__c                 C   r!   r"   )r#   r;   r   r   r   r   r$      r%   zTrustAnchor.__hash__r"   )r   r   r   r   r   r   r
   r=   r1   r8   r@   r*   r$   r   r   r   r   r7   h   s    
r7   r,   r   c           
      C   s   d}d }}| j dur,d}| j }|d }t|tjrt|}|d }t|tjr,t|}d}| jdurAd}| j}tdd |D }d}	|rTt|pLtdg|du||d	}	t| j	|	d
S )a  
    Extract trust qualifiers from data and extensions of a certificate.

    .. note::
        Recall that any property of a trust root other than its name and public
        key are in principle irrelevant to the PKIX validation algorithm
        itself.
        This function is merely a helper function that allows the certificate's
        other data to be conveniently gathered to populate the default
        validation parameters for paths deriving from that trust root.

    :param cert:
        The certificate from which to extract qualifiers (usually a
        self-signed one)
    :return:
        A :class:`TrustQualifiers` object with the extracted qualifiers.
    FNTpermitted_subtreesexcluded_subtreesc                 S   s   g | ]}|d  j qS )policy_identifier)dotted).0pol_infor   r   r   
<listcomp>   s    z*derive_quals_from_cert.<locals>.<listcomp>
any_policy)user_initial_policy_setinitial_explicit_policyinitial_permitted_subtreesinitial_excluded_subtrees)r   r   )
name_constraints_valuer'   r   GeneralSubtreesr   certificate_policies_value	frozensetr   r
   r   )
r,   	ext_foundrA   rB   nc_extpermitted_valexcluded_valacceptable_policiespolicies_valparamsr   r   r   derive_quals_from_cert   s:   

rX   c                       s   e Zd ZdZdejfddZedejfddZ	edd	 Z
ed
d Zedee fddZedejfddZdejf fddZ  ZS )AuthorityWithCertzz
    .. versionadded:: 0.20.0

    Authority provisioned as a certificate.

    :param cert:
        The certificate.
    r,   c                 C   s
   || _ d S r"   _certr/   r   r   r   r=      r%   zAuthorityWithCert.__init__r   c                 C      | j jS r"   )r[   subjectr   r   r   r   r         zAuthorityWithCert.namec                 C   r\   r"   )r[   r   r   r   r   r   r      r^   zAuthorityWithCert.public_keyc                 C   s   | j }|jj|j fS r"   )r[   r]   r    r   dumpr/   r   r   r   r       s   zAuthorityWithCert.hashablec                 C   r\   r"   )r[   key_identifierr   r   r   r   r+      r^   zAuthorityWithCert.key_idc                 C   r>   r"   rZ   r   r   r   r   certificate   r?   zAuthorityWithCert.certificatec                    s,   t  |sdS |jr|j| jjkrdS dS )NFT)superr0   authority_issuer_serialr[   issuer_serialr/   	__class__r   r   r0      s   z(AuthorityWithCert.is_potential_issuer_of)r   r   r   r   r   r5   r=   r1   r2   r   r   r    r   r4   r+   ra   r0   __classcell__r   r   re   r   rY      s    	

rY   c                       s`   e Zd ZdZ		ddejdee def fddZ	e
d	ejfd
dZe
d	efddZ  ZS )CertTrustAnchora  
    .. versionadded:: 0.20.0

    Trust anchor provisioned as a certificate.

    :param cert:
        The certificate, usually self-signed.
    :param quals:
        Explicit trust qualifiers.
    :param derive_default_quals_from_cert:
        Flag indicating to derive default trust qualifiers from the certificate
        content if explicit ones are not provided. Defaults to ``False``.
    NFr,   r9   derive_default_quals_from_certc                    s&   t |}|| _t || || _d S r"   )rY   r[   rb   r=   _derive)r   r,   r9   ri   r8   re   r   r   r=      s   
zCertTrustAnchor.__init__r   c                 C   r>   r"   rZ   r   r   r   r   ra     r?   zCertTrustAnchor.certificatec                 C   s0   | j d ur| j S | jrt| j | _ }|S t S r"   )r<   rj   rX   r[   r
   )r   r9   r   r   r   r@     s   
z CertTrustAnchor.trust_qualifiersr&   )r   r   r   r   r   r5   r   r
   r6   r=   r1   ra   r@   rg   r   r   re   r   rh      s    rh   c                   @   sh   e Zd ZdZdejdejfddZe	dejfddZ
e	d	d
 Ze	dee fddZe	dd ZdS )NamedKeyAuthorityz
    Authority provisioned as a named key.

    :param entity_name:
        The name of the entity that controls the private key of the trust root.
    :param public_key:
        The trust root's public key.
    entity_namer   c                 C   r:   r"   )_name_public_key)r   rl   r   r   r   r   r=      s   
zNamedKeyAuthority.__init__r   c                 C   r>   r"   )rm   r   r   r   r   r   $  r?   zNamedKeyAuthority.namec                 C   r>   r"   )rn   r   r   r   r   r   (  r?   zNamedKeyAuthority.public_keyc                 C   s   d S r"   r   r   r   r   r   r+   ,  s   zNamedKeyAuthority.key_idc                 C   s   | j j| j fS r"   )rm   r    rn   r_   r   r   r   r   r    0  s   zNamedKeyAuthority.hashableN)r   r   r   r   r   r2   r   r3   r=   r1   r   r   r   r4   r+   r    r   r   r   r   rk     s    	
rk   )abcdataclassesr   typingr   
asn1cryptor   r   
name_treesr   policy_declr   r
   ABCr   r7   r5   rX   rY   rh   rk   r   r   r   r   <module>   s    @!9+)