U
    c5                     @   s  d dl Z d dlZd dlmZmZmZmZ d dlmZ d dl	m
Z
 d dl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 ddlmZmZ ddlmZmZmZmZ ddlm Z m!Z!m"Z"m#Z#m$Z$ ddl%m&Z& ddl'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z- ddddddddddddddddd d!d"d#d$d%d&d'd(gZ.ed)e,d*Z/e,ddddd+fej0ee/ e1e
e2e&d,d-dZ3dddej4dfee1eej5ej6f ej0e
e
e&e-d.d/d!Z7d9e!e
e
ee&e8e*d0d1dZ9d:e!e
ee8e(d2d3d#Z:d+d4d+dd+ej4fe!e
d5d6d(Z;d;e!eee&e8e*d7d8d%Z<dS )<    N)IOTypeTypeVarUnion)cms)ValidationContext)misc   )
DiffPolicy   )VRIDocumentSecurityStoreasync_add_validation_infocollect_validation_info)SigSeedValueValidationErrorValidationInfoReadingError)async_validate_cms_signatureasync_validate_detached_cms)RevocationInfoValidationTypeapply_adobe_revocation_info async_validate_pdf_ltv_signatureget_timestamp_chain)
DocMDPInfoEmbeddedPdfSignatureasync_validate_pdf_signatureasync_validate_pdf_timestampread_certification_data)KeyUsageConstraints)DocumentTimestampStatusModificationInfoPdfSignatureStatusSignatureCoverageLevelSignatureStatusStandardCMSSignatureStatusr!   r    r   r#   r   r   r   r   r   r   r   r   r   validate_pdf_signaturer   validate_cms_signaturer   validate_detached_cmsr   validate_pdf_timestampr   validate_pdf_ltv_signaturer   r   add_validation_info
StatusType)boundFsigned_data
status_cls
raw_digestvalidation_contextstatus_kwargskey_usage_settingsc                 C   s*   t dt t| |||||d}t|S )a^  
    .. deprecated:: 0.9.0
        Use :func:`~.generic_cms.async_validate_cms_signature` instead.

    .. versionchanged:: 0.7.0
        Now handles both detached and enveloping signatures.

    Validate a CMS signature (i.e. a ``SignedData`` object).

    :param signed_data:
        The :class:`.asn1crypto.cms.SignedData` object to validate.
    :param status_cls:
        Status class to use for the validation result.
    :param raw_digest:
        Raw digest, computed from context.
    :param validation_context:
        Validation context to validate the signer's certificate.
    :param status_kwargs:
        Other keyword arguments to pass to the ``status_class`` when reporting
        validation results.
    :param key_usage_settings:
        A :class:`.KeyUsageConstraints` object specifying which key usages
        must or must not be present in the signer's certificate.
    :param encap_data_invalid:
        If ``True``, the encapsulated data inside the CMS is invalid,
        but the remaining validation logic still has to be run (e.g. a
        timestamp token, which requires validation of the embedded message
        imprint).

        This option is considered internal API, the semantics of which may
        change without notice in the future.
    :return:
        A :class:`.SignatureStatus` object (or an instance of a proper subclass)
    zR'validate_cms_signature' is deprecated, use 'async_validate_cms_signature' insteadr,   )warningswarnDeprecationWarningr   asynciorun)r-   r.   r/   r0   r1   r2   Zencap_data_invalidcoro r9   D/tmp/pip-unpacked-wheel-0kb_yl26/pyhanko/sign/validation/__init__.pyr%   A   s    *   )
input_datar-   signer_validation_contextts_validation_contextr2   returnc              	   C   s,   t dt t| ||||||d}t|S )a  
    .. deprecated:: 0.9.0
        Use :func:`.generic_cms.async_validate_detached_cms` instead.

    .. versionadded: 0.7.0

    Validate a detached CMS signature.

    :param input_data:
        The input data to sign. This can be either a :class:`bytes` object,
        a file-like object or a :class:`cms.ContentInfo` /
        :class:`cms.EncapsulatedContentInfo` object.

        If a CMS content info object is passed in, the `content` field
        will be extracted.
    :param signed_data:
        The :class:`cms.SignedData` object containing the signature to verify.
    :param signer_validation_context:
        Validation context to use to verify the signer certificate's trust.
    :param ts_validation_context:
        Validation context to use to verify the TSA certificate's trust, if
        a timestamp token is present.
        By default, the same validation context as that of the signer is used.
    :param key_usage_settings:
        Key usage parameters for the signer.
    :param chunk_size:
        Chunk size to use when consuming input data.
    :param max_read:
        Maximal number of bytes to read from the input stream.
    :return:
        A description of the signature's status.
    zP'validate_detached_cms' is deprecated, use 'async_validate_detached_cms' instead)r;   r-   r<   r=   r2   
chunk_sizemax_read)r3   r4   r5   r   r6   r7   )r;   r-   r<   r=   r2   r?   r@   r8   r9   r9   r:   r&   y   s    *	)embedded_sigr<   r=   diff_policyr2   	skip_diffr>   c                 C   s   t | |||||d}t|S )a  
    .. versionchanged:: 0.9.0
        Wrapper around :func:`~.pdf_embedded.async_validate_pdf_signature`.

    Validate a PDF signature.

    :param embedded_sig:
        Embedded signature to evaluate.
    :param signer_validation_context:
        Validation context to use to validate the signature's chain of trust.
    :param ts_validation_context:
        Validation context to use to validate the timestamp's chain of trust
        (defaults to ``signer_validation_context``).
    :param diff_policy:
        Policy to evaluate potential incremental updates that were appended
        to the signed revision of the document.
        Defaults to
        :const:`~pyhanko.sign.diff_analysis.DEFAULT_DIFF_POLICY`.
    :param key_usage_settings:
        A :class:`.KeyUsageConstraints` object specifying which key usages
        must or must not be present in the signer's certificate.
    :param skip_diff:
        If ``True``, skip the difference analysis step entirely.
    :return:
        The status of the PDF signature in question.
    )rA   r<   r=   rB   r2   rC   )r   r6   r7   )rA   r<   r=   rB   r2   rC   r8   r9   r9   r:   r$      s      )rA   r0   rB   rC   r>   c                 C   s   t | |||d}t|S )a  
    .. versionchanged:: 0.9.0
        Wrapper around :func:`~.pdf_embedded.async_validate_pdf_timestamp`.

    Validate a PDF document timestamp.

    :param embedded_sig:
        Embedded signature to evaluate.
    :param validation_context:
        Validation context to use to validate the timestamp's chain of trust.
    :param diff_policy:
        Policy to evaluate potential incremental updates that were appended
        to the signed revision of the document.
        Defaults to
        :const:`~pyhanko.sign.diff_analysis.DEFAULT_DIFF_POLICY`.
    :param skip_diff:
        If ``True``, skip the difference analysis step entirely.
    :return:
        The status of the PDF timestamp in question.
    )rA   r0   rB   rC   )r   r6   r7   )rA   r0   rB   rC   r8   r9   r9   r:   r'      s     T)rA   r0   c           	   
   C   s"   t | |||||||d}t|S )al  
    .. versionchanged:: 0.9.0
        Wrapper around :func:`~.dss.async_add_validation_info`

    Add validation info (CRLs, OCSP responses, extra certificates) for a
    signature to the DSS of a document in an incremental update.

    :param embedded_sig:
        The signature for which the revocation information needs to be
        collected.
    :param validation_context:
        The validation context to use.
    :param skip_timestamp:
        If ``True``, do not attempt to validate the timestamp attached to
        the signature, if one is present.
    :param add_vri_entry:
        Add a ``/VRI`` entry for this signature to the document security store.
        Default is ``True``.
    :param output:
        Write the output to the specified output stream.
        If ``None``, write to a new :class:`.BytesIO` object.
        Default is ``None``.
    :param in_place:
        Sign the original input stream in-place.
        This parameter overrides ``output``.
    :param chunk_size:
        Chunk size parameter to use when copying output to a new stream
        (irrelevant if ``in_place`` is ``True``).
    :param force_write:
        Force a new revision to be written, even if not necessary (i.e.
        when all data in the validation context is already present in the DSS).
    :return:
        The (file-like) output object to which the result was written.
    )rA   r0   skip_timestampadd_vri_entryoutputin_placer?   force_write)r   r6   r7   )	rA   r0   rD   rE   rG   rF   rH   r?   r8   r9   r9   r:   r)      s    (    )rA   validation_typerB   r2   rC   r>   c           	   
   C   s"   t | |||||||d}t|S )a  
    .. versionchanged:: 0.9.0
        Wrapper around :func:`async_validate_pdf_ltv_signature`.

    Validate a PDF LTV signature according to a particular profile.

    :param embedded_sig:
        Embedded signature to evaluate.
    :param validation_type:
        Validation profile to use.
    :param validation_context_kwargs:
        Keyword args to instantiate
        :class:`.pyhanko_certvalidator.ValidationContext` objects needed over
        the course of the validation.
    :param bootstrap_validation_context:
        Validation context used to validate the current timestamp.
    :param force_revinfo:
        Require all certificates encountered to have some form of live
        revocation checking provisions.
    :param diff_policy:
        Policy to evaluate potential incremental updates that were appended
        to the signed revision of the document.
        Defaults to
        :const:`~pyhanko.sign.diff_analysis.DEFAULT_DIFF_POLICY`.
    :param key_usage_settings:
        A :class:`.KeyUsageConstraints` object specifying which key usages
        must or must not be present in the signer's certificate.
    :param skip_diff:
        If ``True``, skip the difference analysis step entirely.
    :return:
        The status of the signature.
    )rA   rI   validation_context_kwargsbootstrap_validation_contextforce_revinforB   r2   rC   )r   r6   r7   )	rA   rI   rJ   rK   rL   rB   r2   rC   r8   r9   r9   r:   r(   0  s    (
)NNNNF)NNF)NNFNNF)=r6   r3   typingr   r   r   r   Z
asn1cryptor   Zpyhanko_certvalidatorr   Zpyhanko.pdf_utilsr   Zdiff_analysisr
   Zdssr   r   r   r   errorsr   r   Zgeneric_cmsr   r   Zltvr   r   r   r   Zpdf_embeddedr   r   r   r   r   settingsr   statusr   r   r    r!   r"   r#   __all__r*   Z
SignedDatabytesdictr%   ZDEFAULT_CHUNK_SIZEZContentInfoZEncapsulatedContentInfor&   boolr$   r'   r)   r(   r9   r9   r9   r:   <module>   s    
            <=      +    "   3       