U
    gs                      @   s   d Z ddlmZ ddlmZ ddl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 dd
lmZ ddlmZ ddlmZmZ dddgZde
eeee ee dddZde
eeee ee dddZde
eejejee dddZdS )z<
This module defines pyHanko's high-level API entry points.
    )datetime)OptionalN)cms)embed)BasePdfFileWriter)SigFieldSpec)SigningError)TimeStamper   )Signer)PdfSignatureMetadata	PdfSignersign_pdfasync_sign_pdfembed_payload_with_cmsF)pdf_outsignature_metasignertimestampernew_field_specc	           
      C   s8   |dk	r|rt dt||||d}	|	j| ||||dS )a  
    Thin convenience wrapper around :meth:`.PdfSigner.sign_pdf`.

    :param pdf_out:
        An :class:`.IncrementalPdfFileWriter`.
    :param bytes_reserved:
        Bytes to reserve for the CMS object in the PDF file.
        If not specified, make an estimate based on a dummy signature.
    :param signature_meta:
        The specification of the signature to add.
    :param signer:
        :class:`.Signer` object to use to produce the signature object.
    :param timestamper:
        :class:`.TimeStamper` object to use to produce any time stamp tokens
        that might be required.
    :param in_place:
        Sign the input in-place. If ``False``, write output to a
        :class:`.BytesIO` object.
    :param existing_fields_only:
        If ``True``, never create a new empty signature field to contain
        the signature.
        If ``False``, a new field may be created if no field matching
        :attr:`~.PdfSignatureMetadata.field_name` exists.
    :param new_field_spec:
        If a new field is to be created, this parameter allows the caller
        to specify the field's properties in the form of a
        :class:`.SigFieldSpec`. This parameter is only meaningful if
        ``existing_fields_only`` is ``False``.
    :param output:
        Write the output to the specified output stream.
        If ``None``, write to a new :class:`.BytesIO` object.
        Default is ``None``.
    :return:
        The output stream containing the signed output.
    NSSpecifying a signature field spec is not meaningful when existing_fields_only=True.r   r   existing_fields_onlybytes_reservedin_placeoutput)r   r   r   
r   r   r   r   r   r   r   r   r   
pdf_signer r   B/tmp/pip-unpacked-wheel-owvgwkas/pyhanko/sign/signers/functions.pyr      s"    /c	           
         s>   |dk	r|rt dt||||d}	|	j| ||||dI dH S )a  
    Thin convenience wrapper around :meth:`.PdfSigner.async_sign_pdf`.

    :param pdf_out:
        An :class:`.IncrementalPdfFileWriter`.
    :param bytes_reserved:
        Bytes to reserve for the CMS object in the PDF file.
        If not specified, make an estimate based on a dummy signature.
    :param signature_meta:
        The specification of the signature to add.
    :param signer:
        :class:`.Signer` object to use to produce the signature object.
    :param timestamper:
        :class:`.TimeStamper` object to use to produce any time stamp tokens
        that might be required.
    :param in_place:
        Sign the input in-place. If ``False``, write output to a
        :class:`.BytesIO` object.
    :param existing_fields_only:
        If ``True``, never create a new empty signature field to contain
        the signature.
        If ``False``, a new field may be created if no field matching
        :attr:`~.PdfSignatureMetadata.field_name` exists.
    :param new_field_spec:
        If a new field is to be created, this parameter allows the caller
        to specify the field's properties in the form of a
        :class:`.SigFieldSpec`. This parameter is only meaningful if
        ``existing_fields_only`` is ``False``.
    :param output:
        Write the output to the specified output stream.
        If ``None``, write to a new :class:`.BytesIO` object.
        Default is ``None``.
    :return:
        The output stream containing the signed output.
    Nr   r   r   )r   r   r   r   r   r   r    r   [   s"    /.sig)
pdf_writerfile_spec_stringpayloadcms_obj	file_namec              	   C   s   t jt d}tjj| | ddtj||dd}	|	ddd | }
d	 }}|d	k	r||	ddd | }tj
||	d
g}tjf |||tj
|
|	d
g|d|pi }t| | tjf |
||	d|pi }t| | d	S )a  
    Embed some data as an embedded file stream into a PDF, and associate it
    with a CMS object.

    The resulting CMS object will also be turned into an embedded file, and
    associated with the original payload through a related file relationship.

    This can be used to bundle (non-PDF) detached signatures with PDF
    attachments, for example.

    .. versionadded:: 0.7.0

    :param pdf_writer:
        The PDF writer to use.
    :param file_spec_string:
        See :attr:`~pyhanko.pdf_utils.embed.FileSpec.file_spec_string` in
        :class:`~pyhanko.pdf_utils.embed.FileSpec`.
    :param payload:
        Payload object.
    :param cms_obj:
        CMS object pertaining to the payload.
    :param extension:
        File extension to use for the CMS attachment.
    :param file_name:
        See :attr:`~pyhanko.pdf_utils.embed.FileSpec.file_name` in
        :class:`~pyhanko.pdf_utils.embed.FileSpec`.
    :param file_spec_kwargs:
        Extra arguments to pass to the
        :class:`~pyhanko.pdf_utils.embed.FileSpec` constructor
        for the main attachment specification.
    :param cms_file_spec_kwargs:
        Extra arguments to pass to the
        :class:`~pyhanko.pdf_utils.embed.FileSpec` constructor
        for the CMS attachment specification.
    )tzFzapplication/pkcs7-mime)Zcreation_dateZmodification_date)r"   datacompressZ	mime_typeparams.r
   r   N)embedded_data)r#   r&   r,   Zf_related_filesuf_related_files)r#   r&   r,   )r   nowtzlocalZget_localzoner   EmbeddedFileObjectZfrom_file_datadumpZEmbeddedFileParamsrsplitZRelatedFileSpecZFileSpecZ
embed_file)r"   r#   r$   r%   	extensionr&   Zfile_spec_kwargsZcms_file_spec_kwargsr.   Z
cms_ef_objZ
cms_data_fZcms_data_ufr-   specZcms_specr   r   r    r      sH    / )NNFNFN)NNFNFN)r!   NNN)__doc__r   typingr   r/   Z
asn1cryptor   Zpyhanko.pdf_utilsr   Zpyhanko.pdf_utils.writerr   Zpyhanko.sign.fieldsr   Zpyhanko.sign.generalr   Zpyhanko.sign.timestampsr	   Zpdf_cmsr   r   r   r   __all__r   r   strr0   ZContentInfor   r   r   r   r    <module>   sb   
      H      I    