U
    ;g                     @   s   d Z ddlZddlmZ ddlmZmZmZ ddlm	Z	m
Z
mZmZ ddlmZ ddlmZ dd	d
dddgZde ZG dd dejZG dd	 d	ejZG dd
 d
ejZeddG dd dZG dd dejZdS )zF
Asynchronous API for fetching OCSP responses, CRLs and certificates.
    N)	dataclass)AsyncGeneratorIterableUnion)cmscrlocspx509)	Authority)__version__OCSPFetcher
CRLFetcherCertificateFetcherFetchersFetcherBackendDEFAULT_USER_AGENTzpyhanko_certvalidator %sc                   @   sf   e Zd ZdZeejejf e	e
jdddZee
j dddZeejejf ee
j dd	d
ZdS )r   z4Utility interface to fetch and cache OCSP responses.)cert	authorityreturnc                    s   t dS )ag  
        Fetch an OCSP response for a certificate.

        :param cert:
            The certificate for which an OCSP response has to be fetched.
        :param authority:
            The issuing authority.
        :raises:
            OCSPFetchError - Raised if an OCSP response could not be obtained.
        :return:
            An OCSP response.
        NNotImplementedError)selfr   r    r   F/tmp/pip-unpacked-wheel-hgp_x7fx/pyhanko_certvalidator/fetchers/api.pyfetch   s    zOCSPFetcher.fetchr   c                 C   s   t dS )zD
        Return all responses fetched by this OCSP fetcher.
        Nr   r   r   r   r   fetched_responses0   s    zOCSPFetcher.fetched_responsesr   r   c                 C   s   t dS )z
        Return all responses fetched by this OCSP fetcher that are relevant
        to determine the revocation status of the given certificate.
        Nr   r   r   r   r   r   fetched_responses_for_cert6   s    z&OCSPFetcher.fetched_responses_for_certN)__name__
__module____qualname____doc__r   r	   Certificater   AttributeCertificateV2r
   r   OCSPResponser   r   r   r    r   r   r   r   r      s   c                   @   sn   e Zd ZdZddeejejf e	e
j dddZe	e
j ddd	Zeejejf e	e
j dd
dZdS )r   z*Utility interface to fetch and cache CRLs.N)
use_deltasr   c                   s   t dS )a  
        Fetches the CRLs for a certificate.

        :param cert:
            An asn1crypto.x509.Certificate object to get the CRL for

        :param use_deltas:
            A boolean indicating if delta CRLs should be fetched

        :raises:
            CRLFetchError - when a network/IO error or decoding error occurs

        :return:
            An iterable of CRLs fetched.
        Nr   )r   r   r(   r   r   r   r   C   s    zCRLFetcher.fetchr   c                 C   s   t dS )z>
        Return all CRLs fetched by this CRL fetcher.
        Nr   r   r   r   r   fetched_crls_   s    zCRLFetcher.fetched_crlsc                 C   s   t dS )a  
        Return all relevant fetched CRLs for the given certificate

        :param cert:
            A certificate.
        :return:
            An iterable of CRLs
        :raise KeyError:
            if no fetch operations have been performed for this certificate
        Nr   r   r   r   r   fetched_crls_for_certe   s    z CRLFetcher.fetched_crls_for_cert)r!   r"   r#   r$   r   r	   r%   r   r&   r   r   ZCertificateListr   r)   r*   r   r   r   r   r   @   s   c                   @   sb   e Zd ZdZeejejf e	ejdf dddZ
e	ejdf dddZeej dd	d
ZdS )r   z2Utility interface to fetch and cache certificates.Nr   c                 C   s   t dS )a  
        Fetches certificates from the authority information access extension of
        a certificate.

        :param cert:
            A certificate

        :raises:
            CertificateFetchError - when a network I/O or decoding error occurs

        :return:
            An asynchronous generator yielding asn1crypto.x509.Certificate
            objects that were fetched.
        Nr   r   r   r   r   fetch_cert_issuersx   s    z%CertificateFetcher.fetch_cert_issuersr   c                 C   s   t dS )a  
        Fetches certificates from the authority information access extension of
        an asn1crypto.crl.CertificateList.

        :param certificate_list:
            An asn1crypto.crl.CertificateList object

        :raises:
            CertificateFetchError - when a network I/O or decoding error occurs

        :return:
            An asynchronous generator yielding asn1crypto.x509.Certificate
            objects that were fetched.
        Nr   )r   Zcertificate_listr   r   r   fetch_crl_issuers   s    z$CertificateFetcher.fetch_crl_issuersc                 C   s   t dS )zP
        Return all certificates retrieved by this certificate fetcher.
        Nr   r   r   r   r   fetched_certs   s    z CertificateFetcher.fetched_certs)r!   r"   r#   r$   r   r	   r%   r   r&   r   r+   r,   r   r-   r   r   r   r   r   u   s   T)frozenc                   @   s*   e Zd ZU dZeed< eed< eed< dS )r   z
    Models a collection of fetchers to be used by a validation context.

    The intention is that these can share resources (like a connection pool)
    in a unified, controlled manner. See also :class:`.FetcherBackend`.
    Zocsp_fetcherZcrl_fetcherZcert_fetcherN)r!   r"   r#   r$   r   __annotations__r   r   r   r   r   r   r      s   
c                   @   s<   e Zd ZdZedddZdd ZedddZd	d
 ZdS )r   a  
    Generic, bare-bones interface to help abstract away instantiation logic for
    fetcher implementations.

    Intended to operate as an asynchronous context manager, with
    `async with backend_obj as fetchers: ...` putting the resulting
    :class:`.Fetchers` object in to the variable named `fetchers`.

    .. note::
        The initialisation part of the API is necessarily synchronous,
        for backwards compatibility with the old ``ValidationContext`` API.
        If you need asynchronous resource management, handle it elsewhere,
        or use some form of lazy resource provisioning.

        Alternatively, you can pass :class:`Fetchers` objects to the validation
        context yourself, and forgo use of the :class:`.FetcherBackend`
        API altogether.
    r   c                 C   s   t dS )zl
        Set up fetchers synchronously.

        .. note::
            This is a synchronous method
        Nr   r   r   r   r   get_fetchers   s    zFetcherBackend.get_fetchersc                    s   dS )zf
        Clean up the resources associated with this fetcher backend,
        asynchronously.
        Nr   r   r   r   r   close   s    zFetcherBackend.closec                    s   |   S N)r0   r   r   r   r   
__aenter__   s    zFetcherBackend.__aenter__c                    s   |   I d H S r2   )r1   )r   exc_typeexc_valexc_tbr   r   r   	__aexit__   s    zFetcherBackend.__aexit__N)	r!   r"   r#   r$   r   r0   r1   r3   r7   r   r   r   r   r      s
   	)r$   abcdataclassesr   typingr   r   r   Z
asn1cryptor   r   r   r	   Zpyhanko_certvalidator.authorityr
   Zpyhanko_certvalidator.versionr   __all__r   ABCr   r   r   r   r   r   r   r   r   <module>   s(   	&50