U
    c                     @   s  d dl Z d dlmZ d dlmZmZ d dlmZmZmZ d dlm	Z
 d dlmZmZ d dlmZ dd	lmZmZmZ dd
lmZ ddddddddgZG dd dZG dd deZG dd deZG dd deZG dd deZG dd deZG dd de jZG dd de jZdS )    N)datetime)IterableOptional)algoscmscore)pdf)tspx509)hashes   )as_signing_certificate_v2get_pyca_cryptography_hashsimple_cms_attribute)TimeStamperSignedAttributeProviderSpecUnsignedAttributeProviderSpecCMSAttributeProviderSigningTimeProviderSigningCertificateV2ProviderAdobeRevinfoProviderCMSAlgorithmProtectionProviderTSTProviderc                   @   s:   e Zd ZU dZeed< d
ddZdeej	 dddZ
d	S )r   zB
    Base class to provide asynchronous CMS attribute values.
    attribute_typeFc                    s   t dS )a  
        Build the attribute value asynchronously.

        :param dry_run:
            ``True`` if the signer is operating in dry-run (size estimation)
            mode.
        :return:
            An attribute value appropriate for the attribute type.
        NNotImplementedErrorselfdry_run r   ;/tmp/pip-unpacked-wheel-0kb_yl26/pyhanko/sign/attributes.pybuild_attr_value(   s    
z%CMSAttributeProvider.build_attr_valuereturnc                    s.   | j |dI d H }|d k	r&t| j|S d S d S )N)r   )r!   r   r   )r   r   valuer   r   r    get_attribute4   s    z"CMSAttributeProvider.get_attributeN)F)F)__name__
__module____qualname____doc__str__annotations__r!   r   r   ZCMSAttributer%   r   r   r   r    r      s   

c                   @   s@   e Zd ZU dZdZeed< ejdddZ	de
jdd	d
ZdS )r   z
    Provide a value for the signing-certificate-v2 attribute.

    :param signing_cert:
        Certificate containing the signer's public key.
    Zsigning_certificate_v2r   signing_certc                 C   s
   || _ d S Nr,   )r   r-   r   r   r    __init__G   s    z%SigningCertificateV2Provider.__init__Fr"   c                    s
   t | jS r.   )r   r-   r   r   r   r    r!   J   s    z-SigningCertificateV2Provider.build_attr_valueN)F)r&   r'   r(   r)   r   r*   r+   r
   ZCertificater/   r	   ZSigningCertificateV2r!   r   r   r   r    r   =   s   
c                   @   s>   e Zd ZU dZdZeed< edddZde	j
dd	d
ZdS )r   z
    Provide a value for the signing-time attribute (i.e. an otherwise
    unauthenticated timestamp).

    :param timestamp:
        Datetime object to include.
    Zsigning_timer   	timestampc                 C   s
   || _ d S r.   r0   )r   r1   r   r   r    r/   Y   s    zSigningTimeProvider.__init__Fr"   c                    s   t dt| jiS )NZutc_time)r   Timer   ZUTCTimer1   r   r   r   r    r!   \   s    z$SigningTimeProvider.build_attr_valueN)F)r&   r'   r(   r)   r   r*   r+   r   r/   r   r2   r!   r   r   r   r    r   N   s   
c                   @   sD   e Zd ZU dZdZeed< ejdddZ	de
ej dd	d
ZdS )r   z
    Yield Adobe-style revocation information for inclusion into a CMS
    object.

    :param value:
        A (pre-formatted) RevocationInfoArchival object.
    Zadobe_revocation_info_archivalr   r$   c                 C   s
   || _ d S r.   r3   )r   r$   r   r   r    r/   q   s    zAdobeRevinfoProvider.__init__Fr"   c                    s   | j S r.   r3   r   r   r   r    r!   t   s    z%AdobeRevinfoProvider.build_attr_valueN)F)r&   r'   r(   r)   r   r*   r+   asn1_pdfZRevocationInfoArchivalr/   r   r!   r   r   r   r    r   f   s   
c                   @   s>   e Zd ZU dZeed< eejdddZde	j
ddd	Zd
S )r   Zcms_algorithm_protectionr   digest_algosignature_algoc                 C   s   || _ || _d S r.   r5   )r   r6   r7   r   r   r    r/   |   s    z'CMSAlgorithmProtectionProvider.__init__Fr"   c                    s   t td| ji| jdS )N	algorithm)digest_algorithmZsignature_algorithm)r   CMSAlgorithmProtectionr   ZDigestAlgorithmr6   r7   r   r   r   r    r!      s    z/CMSAlgorithmProtectionProvider.build_attr_valueN)F)r&   r'   r(   r   r*   r+   r   ZSignedDigestAlgorithmr/   r   r:   r!   r   r   r   r    r   y   s   
c                   @   s4   e Zd Zd
eeeedddZdejdddZ	d	S )r   signature_time_stamp_tokenF)r9   
data_to_tstimestamper	attr_typec                 C   s"   || _ || _|| _|| _|| _d S r.   )r   r9   r=   data	prehashed)r   r9   r<   r=   r>   r@   r   r   r    r/      s
    zTSTProvider.__init__r"   c                    sd   | j }| jr| j}n&t|}t|}|| j | }|rL| j	|}n| j
||}|I d H S r.   )r9   r@   r?   r   r   ZHashupdatefinalizer=   Zasync_dummy_responseZasync_timestamp)r   r   r9   digestZmd_specZmdZts_coror   r   r    r!      s    
zTSTProvider.build_attr_valueN)r;   F)F)
r&   r'   r(   r*   bytesr   r/   r   ZContentInfor!   r   r   r   r    r      s     
c                   @   s&   e Zd ZdZeeee dddZdS )r   z
    .. versionadded:: 0.14.0

    Interface for setting up signed attributes, independently of the
    :class:`~pyhanko.sign.signers.pdf_cms.Signer` hierarchy.
    )data_digestr9   r#   c                 C   s   t dS )z
        Lazily set up signed attribute providers.

        :param data_digest:
            The digest of the data to be signed.
        :param digest_algorithm:
            The digest algorithm used.
        Nr   )r   rE   r9   r   r   r    signed_attr_providers   s    
z1SignedAttributeProviderSpec.signed_attr_providersN)	r&   r'   r(   r)   rD   r*   r   r   rF   r   r   r   r    r      s   c                   @   s*   e Zd ZdZeejeee	 dddZ
dS )r   z
    .. versionadded:: 0.14.0

    Interface for setting up unsigned attributes, independently of the
    :class:`~pyhanko.sign.signers.pdf_cms.Signer` hierarchy.
    )	signaturesigned_attrsr9   r#   c                 C   s   t dS )a>  
        Lazily set up unsigned attribute providers.

        :param signature:
            The signature computed over the signed attributes.
        :param signed_attrs:
            Signed attributes over which the signature was taken.
        :param digest_algorithm:
            The digest algorithm used.
        Nr   )r   rG   rH   r9   r   r   r    unsigned_attr_providers   s    z5UnsignedAttributeProviderSpec.unsigned_attr_providersN)r&   r'   r(   r)   rD   r   ZCMSAttributesr*   r   r   rI   r   r   r   r    r      s     )abcr   typingr   r   Z
asn1cryptor   r   r   r   r4   r	   r
   Zcryptography.hazmat.primitivesr   Zgeneralr   r   r   Z
timestampsr   __all__r   r   r   r   r   r   ABCr   r   r   r   r   r    <module>   s2    