
     h                     v    d Z ddlZddlmZmZmZ ddlmZ ddlm	Z	 ddl
mZmZmZmZ dgZ G d	 d          ZdS )
z
Module to handle the timestamping functionality in pyHanko.

Many PDF signature profiles require trusted timestamp tokens.
The tools in this module allow pyHanko to obtain such tokens from
:rfc:`3161`-compliant time stamping
authorities.
    N)algoscmstsp)CertificateValidator)SimpleCertificateStore   )dummy_digestextract_ts_certs	get_noncehandle_tsp_responseTimeStamperc                       e Zd ZdZddZd Zd Zd Zd Zde	j
        fd	Zd
ej        dej        fdZde	j
        fdZdS )r   z
    .. versionchanged:: 0.9.0
        Made API more asyncio-friendly _(breaking change)_

    Class to make :rfc:`3161` timestamp requests.
    Tc                 V    i | _         i | _        t                      | _        || _        d S N)_dummy_response_cache_certsr   cert_registryinclude_nonce)selfr   s     W/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/pyhanko/sign/timestamps/api.py__init__zTimeStamper.__init__!   s-    %'"355*    c                     dt          j        t          j        d|i          |d          dd}| j        r&t                      }t          j        |          |d<   nd}|t          j        |          fS )a&  
        Format the body of an :rfc:`3161` request as a CMS object.
        Subclasses with more specific needs may want to override this.

        :param message_digest:
            Message digest to which the timestamp will apply.
        :param md_algorithm:
            Message digest algorithm to use.

            .. note::
                As per :rfc:`8933`, ``md_algorithm`` should also be the
                algorithm used to compute ``message_digest``.
        :return:
            An :class:`.asn1crypto.tsp.TimeStampReq` object.
        r   	algorithm)hash_algorithmhashed_messageT)versionmessage_imprintcert_reqnonceN)	r   MessageImprintr   DigestAlgorithmr   r   r   IntegerTimeStampReq)r   message_digestmd_algorithmreqr    s        r   request_cmszTimeStamper.request_cms'   s    " "1&+&;$l3' ' '5	     
 
  	KKE;u--CLLEc&s++++r   c                    K                                      d{V   fd}t          | j                                                  }t	          j        |          D ]}| d{V W V  dS )a+  
        Produce validation paths for the certificates gathered by this
        :class:`.TimeStamper`.

        This is internal API.

        :param validation_context:
            The validation context to apply.
        :return:
            An asynchronous generator of validation paths.
        Nc                 x    t          | j                  }|                    t                      dh          S )N)intermediate_certsvalidation_contexttime_stamping)r   r   async_validate_usageset)cert	validatorr   r,   s     r   _validation_jobz5TimeStamper.validation_paths.<locals>._validation_jobY   sB    ,#'#5#5  I
 11#%%/9JKKKr   )_ensure_dummymapr   valuesasyncioas_completed)r   r,   r2   jobsjobs   ``   r   validation_pathszTimeStamper.validation_pathsK   s         """""""""	L 	L 	L 	L 	L 	L ?DK$6$6$8$899'-- 	 	C))))))OOOOO	 	r   c                 h    || j         |<   t          || j                  D ]}|| j        |j        <   d S r   )r   r
   r   r   issuer_serial)r   r&   dummyr0   s       r   _register_dummyzTimeStamper._register_dummyf   sG    38"<0$UD,>?? 	3 	3D.2DK*++	3 	3r   c                 ^   K   | j         s#ddlm} |                     |           d {V  d S d S )Nr   )
DEFAULT_MD)r   pyhanko.signr@   async_dummy_response)r   r@   s     r   r3   zTimeStamper._ensure_dummyk   sY      ) 	8//////++J77777777777	8 	8r   returnc                    K   	 | j         |         S # t          $ r, |                     t          |          |           d{V }Y nw xY w|                     ||           |S )a9  
        Return a dummy response for use in CMS object size estimation.

        For every new ``md_algorithm`` passed in, this method will call
        the :meth:`timestamp` method exactly once, with a dummy digest.
        The resulting object will be cached and reused for future invocations
        of :meth:`dummy_response` with the same ``md_algorithm`` value.

        :param md_algorithm:
            Message digest algorithm to use.
        :return:
            A timestamp token, encoded as an
            :class:`.asn1crypto.cms.ContentInfo` object.
        N)r   KeyErrorasync_timestampr	   r>   )r   r&   r=   s      r   rB   z TimeStamper.async_dummy_responser   s      $	-l;; 	 	 	..\**L       EEE	 	\5111s    3AAr'   c                    K   t           )aF  
        Submit the specified timestamp request to the server.

        :param req:
            Request body to submit.
        :return:
            A timestamp response from the server.
        :raises IOError:
            Raised in case of an I/O issue in the communication with the
            timestamping server.
        )NotImplementedError)r   r'   s     r   async_request_tsa_responsez&TimeStamper.async_request_tsa_response   s       "!r   c                    K   |                      ||          \  }}|                     |           d{V }t          ||          S )a+  
        Request a timestamp for the given message digest.

        :param message_digest:
            Message digest to which the timestamp will apply.
        :param md_algorithm:
            Message digest algorithm to use.

            .. note::
                As per :rfc:`8933`, ``md_algorithm`` should also be the
                algorithm used to compute ``message_digest``.
        :return:
            A timestamp token, encoded as an
            :class:`.asn1crypto.cms.ContentInfo` object.
        :raises IOError:
            Raised in case of an I/O issue in the communication with the
            timestamping server.
        :raises TimestampRequestError:
            Raised if the timestamp server did not return a success response,
            or if the server's response is invalid.
        N)r(   rI   r   )r   r%   r&   r    r'   ress         r   rF   zTimeStamper.async_timestamp   sV      2 %%nlCC
s33C88888888"3...r   N)T)__name__
__module____qualname____doc__r   r(   r:   r>   r3   r   ContentInforB   r   r$   TimeStampResprI   rF    r   r   r   r      s         + + + +", ", ",H  63 3 3
8 8 8#/    6"#"		" " " " /	/ / / / / /r   )rO   r6   
asn1cryptor   r   r   pyhanko_certvalidatorr   pyhanko_certvalidator.registryr   common_utilsr	   r
   r   r   __all__r   rR   r   r   <module>rX      s      & & & & & & & & & & 6 6 6 6 6 6 A A A A A A            /_/ _/ _/ _/ _/ _/ _/ _/ _/ _/r   