U
    ;g^"                     @   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ejed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                   @   sB   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   C/tmp/pip-unpacked-wheel-hgp_x7fx/pyhanko_certvalidator/authority.pyr
      s
   
r
   c                   @   s|   e Zd ZdZeejdddZeej	dddZ
edd Zd	d
 Zdd Zeee dddZejedddZdS )	AuthorityzM
    .. versionadded:: 0.20.0

    Abstract authority, i.e. a named key.
    returnc                 C   s   t dS )z'
        The authority's name.
        NNotImplementedErrorselfr   r   r   name/   s    zAuthority.namec                 C   s   t dS )z-
        The authority's public key.
        Nr   r   r   r   r   
public_key6   s    zAuthority.public_keyc                 C   s   t dS )zm
        A hashable unique identifier of the authority, used in ``__eq__``
        and ``__hash__``.
        Nr   r   r   r   r   hashable=   s    zAuthority.hashablec                 C   s
   t | jS N)hashr   r   r   r   r   __hash__E   s    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   s   t dS )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.
        Nr   r   r   r   r   key_idN   s    zAuthority.key_idcertr   c                 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   Z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boolr,   r   r   r   r   r   (   s   
r   c                   @   sZ   e Zd ZdZdeee dddZeedddZ	eed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   s   || _ || _d S r    )
_authority_quals)r   r4   r5   r   r   r   __init__n   s    zTrustAnchor.__init__r   c                 C   s   | j S r    )r6   r   r   r   r   r4   t   s    zTrustAnchor.authorityc                 C   s   | j p
t S )z0
        Qualifiers for the trust root.
        )r7   r
   r   r   r   r   trust_qualifiersx   s    zTrustAnchor.trust_qualifiersc                 C   s   t |to|j| jkS r    )r#   r3   r6   r$   r   r   r   r&      s    

zTrustAnchor.__eq__c                 C   s
   t | jS r    )r!   r6   r   r   r   r   r"      s    zTrustAnchor.__hash__)N)r   r   r   r   r   r   r
   r8   r-   r4   r9   r&   r"   r   r   r   r   r3   h   s     r3   r(   c           
      C   s   d}d }}| j dk	rXd}| j }|d }t|tjr<t|}|d }t|tjrXt|}d}| jdk	rd}| j}tdd |D }d}	|rt|ptdg|dk	||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)Zdotted).0Zpol_infor   r   r   
<listcomp>   s     z*derive_quals_from_cert.<locals>.<listcomp>Z
any_policy)Zuser_initial_policy_setZinitial_explicit_policyZinitial_permitted_subtreesZinitial_excluded_subtrees)r   r   )
Zname_constraints_valuer#   r   ZGeneralSubtreesr   Zcertificate_policies_value	frozensetr   r
   r   )
r)   Z	ext_foundr:   r;   Znc_extZpermitted_valZexcluded_valZacceptable_policiesZpolicies_valparamsr   r   r   derive_quals_from_cert   s<    

 rA   c                       s   e Zd ZdZejdddZeejdddZ	edd	 Z
ed
d Zeee dddZeejdddZejd 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   r8      s    zAuthorityWithCert.__init__r   c                 C   s   | j jS r    )rD   subjectr   r   r   r   r      s    zAuthorityWithCert.namec                 C   s   | j jS r    )rD   r   r   r   r   r   r      s    zAuthorityWithCert.public_keyc                 C   s   | j }|jj|j fS r    )rD   rE   r   r   dumpr+   r   r   r   r      s    zAuthorityWithCert.hashablec                 C   s   | j jS r    )rD   key_identifierr   r   r   r   r'      s    zAuthorityWithCert.key_idc                 C   s   | j S r    rC   r   r   r   r   certificate   s    zAuthorityWithCert.certificatec                    s,   t  |sdS |jr(|j| jjkr(dS dS )NFT)superr,   Zauthority_issuer_serialrD   Zissuer_serialr+   	__class__r   r   r,      s    z(AuthorityWithCert.is_potential_issuer_of)r   r   r   r   r   r1   r8   r-   r.   r   r   r   r   r0   r'   rH   r,   __classcell__r   r   rJ   r   rB      s   	

rB   c                       sX   e Zd ZdZdejee ed fddZ	e
ejddd	Ze
ed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``.
    NF)r)   r5   derive_default_quals_from_certc                    s&   t |}|| _t || || _d S r    )rB   rD   rI   r8   _derive)r   r)   r5   rN   r4   rJ   r   r   r8      s    zCertTrustAnchor.__init__r   c                 C   s   | j S r    rC   r   r   r   r   rH     s    zCertTrustAnchor.certificatec                 C   s4   | j d k	r| j S | jr*t| j | _ }|S t S d S r    )r7   rO   rA   rD   r
   )r   r5   r   r   r   r9     s    
z CertTrustAnchor.trust_qualifiers)NF)r   r   r   r   r   r1   r   r
   r2   r8   r-   rH   r9   rL   r   r   rJ   r   rM      s     rM   c                   @   sf   e Zd ZdZejejdddZe	ejdddZ
e	dd	 Ze	ee d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   s   || _ || _d S r    )_name_public_key)r   rQ   r   r   r   r   r8      s    zNamedKeyAuthority.__init__r   c                 C   s   | j S r    )rR   r   r   r   r   r   $  s    zNamedKeyAuthority.namec                 C   s   | j S r    )rS   r   r   r   r   r   (  s    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    )rR   r   rS   rF   r   r   r   r   r   0  s    zNamedKeyAuthority.hashableN)r   r   r   r   r   r.   r   r/   r8   r-   r   r   r   r0   r'   r   r   r   r   r   rP     s   	
rP   )abcdataclassesr   typingr   Z
asn1cryptor   r   Z
name_treesr   Zpolicy_declr   r
   ABCr   r3   r1   rA   rB   rM   rP   r   r   r   r   <module>   s   @!9+)