
    Wwg                     n    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y)
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y)r   z
    .. versionchanged:: 0.9.0
        Made API more asyncio-friendly _(breaking change)_

    Class to make :rfc:`3161` timestamp requests.
    c                 L    i | _         i | _        t               | _        || _        y N)_dummy_response_cache_certsr   cert_registryinclude_nonce)selfr   s     R/var/www/horilla/myenv/lib/python3.12/site-packages/pyhanko/sign/timestamps/api.py__init__zTimeStamper.__init__"   s$    %'"3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1&+&;&;$l3' '5	  
 KE;;u-CLEc&&s+++r   c                    K    j                          d{     fd}t        | j                  j                               }t	        j
                  |      D ]  }| d{     y7 V7 w)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                 j    t        | j                        }|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_jobZ   s6    ,#'#5#5#5I
 11#%/9JKKr   )_ensure_dummymapr   valuesasyncioas_completed)r   r,   r2   jobsjobs   ``   r   validation_pathszTimeStamper.validation_pathsL   sh        """	L ?DKK$6$6$89''- 	C)O	 	# s"   A3A/AA3$A1%A31A3c                     || j                   |<   t        || j                        D ]  }|| j                  |j                  <    y r   )r   r
   r   r   issuer_serial)r   r&   dummyr0   s       r   _register_dummyzTimeStamper._register_dummyg   sB    38""<0$UD,>,>? 	3D.2DKK**+	3r   c                 h   K   | j                   s ddlm} | j                  |       d {    y y 7 w)Nr   )
DEFAULT_MD)r   pyhanko.signr@   async_dummy_response)r   r@   s     r   r3   zTimeStamper._ensure_dummyl   s0     ))/++J777 * 8s   '202returnc                    K   	 | j                   |   S # t        $ r' | j                  t        |      |       d{  7  }Y nw xY w| j	                  ||       |S w)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_responses   sf     $	--l;; 	..\*L  E	 	\51s.   A A'A=A AAAr'   c                    K   t         w)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      "!s   	c                    K   | j                  ||      \  }}| j                  |       d{   }t        ||      S 7 w)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   sC     2 %%nlC
s33C88"3.. 9s   *?=?N)T)__name__
__module____qualname____doc__r   r(   r:   r>   r3   r   ContentInforB   r   r$   TimeStampResprI   rF    r   r   r   r      sc    +",H63
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 A  /_/ _/r   