pyhanko_certvalidator.fetchers package

Subpackages

Submodules

pyhanko_certvalidator.fetchers.api module

Asynchronous API for fetching OCSP responses, CRLs and certificates.

class pyhanko_certvalidator.fetchers.api.CRLFetcher

Bases: ABC

Utility interface to fetch and cache CRLs.

async fetch(cert: Certificate | AttributeCertificateV2, *, use_deltas=None) → Iterable[CertificateList]

Fetches the CRLs for a certificate.

Parameters:
  • cert – An asn1crypto.x509.Certificate object to get the CRL for

  • use_deltas – A boolean indicating if delta CRLs should be fetched

Raises:

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

Returns:

An iterable of CRLs fetched.

fetched_crls() → Iterable[CertificateList]

Return all CRLs fetched by this CRL fetcher.

fetched_crls_for_cert(cert: Certificate | AttributeCertificateV2) → Iterable[CertificateList]

Return all relevant fetched CRLs for the given certificate

Parameters:

cert – A certificate.

Returns:

An iterable of CRLs

Raises:

KeyError – if no fetch operations have been performed for this certificate

class pyhanko_certvalidator.fetchers.api.CertificateFetcher

Bases: ABC

Utility interface to fetch and cache certificates.

fetch_cert_issuers(cert: Certificate | AttributeCertificateV2) → AsyncGenerator[Certificate, None]

Fetches certificates from the authority information access extension of a certificate.

Parameters:

cert – A certificate

Raises:

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

Returns:

An asynchronous generator yielding asn1crypto.x509.Certificate objects that were fetched.

fetch_crl_issuers(certificate_list) → AsyncGenerator[Certificate, None]

Fetches certificates from the authority information access extension of an asn1crypto.crl.CertificateList.

Parameters:

certificate_list – An asn1crypto.crl.CertificateList object

Raises:

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

Returns:

An asynchronous generator yielding asn1crypto.x509.Certificate objects that were fetched.

fetched_certs() → Iterable[Certificate]

Return all certificates retrieved by this certificate fetcher.

class pyhanko_certvalidator.fetchers.api.FetchLimits(max_requests: int | None = 1000, max_cert_requests: int | None = 200, max_aia_urls_per_object: int | None = 10)

Bases: object

Added in version 0.33.0.

Bounds on the amount of fetching that the built-in fetcher backends perform on behalf of a single Fetchers instance (which, in practice, means a single validation context).

URLs to fetch are taken from the certificates and CRLs being processed, so without bounds, a single hostile input can trigger an arbitrary amount of network activity. The defaults are far above what any real PKI needs. Set a field to None to disable that particular limit.

Requests that are served from a fetcher’s cache are not counted. When a limit is hit, the request is not made and the fetch fails with the usual error for its type (e.g. CertificateFetchError).

Note

Only the built-in backends enforce these limits. Custom FetcherBackend implementations are responsible for their own limits.

max_requests: int | None = 1000

Maximum number of network requests of any type.

max_cert_requests: int | None = 200

Maximum number of requests for issuer certificates, whether the URL came from a certificate or from a CRL. These requests also count towards max_requests. Since every step of chasing down a certificate chain costs at least one such request, this also bounds the chain length.

max_aia_urls_per_object: int | None = 10

Maximum number of caIssuers URLs to follow for any single certificate or CRL. Surplus URLs are ignored.

class pyhanko_certvalidator.fetchers.api.FetcherBackend

Bases: ABC

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 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 Fetchers objects to the validation context yourself, and forgo use of the FetcherBackend API altogether.

get_fetchers() → Fetchers

Set up fetchers synchronously.

Note

This is a synchronous method

async close()

Clean up the resources associated with this fetcher backend, asynchronously.

class pyhanko_certvalidator.fetchers.api.Fetchers(ocsp_fetcher: OCSPFetcher, crl_fetcher: CRLFetcher, cert_fetcher: CertificateFetcher)

Bases: object

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 FetcherBackend.

ocsp_fetcher: OCSPFetcher
crl_fetcher: CRLFetcher
cert_fetcher: CertificateFetcher
class pyhanko_certvalidator.fetchers.api.OCSPFetcher

Bases: ABC

Utility interface to fetch and cache OCSP responses.

async fetch(cert: Certificate | AttributeCertificateV2, authority: Authority) → OCSPResponse

Fetch an OCSP response for a certificate.

Parameters:
  • cert – The certificate for which an OCSP response has to be fetched.

  • authority – The issuing authority.

Raises:

OCSPFetchError - Raised if an OCSP response could not be obtained.

Returns:

An OCSP response.

fetched_responses() → Iterable[OCSPResponse]

Return all responses fetched by this OCSP fetcher.

fetched_responses_for_cert(cert: Certificate | AttributeCertificateV2) → Iterable[OCSPResponse]

Return all responses fetched by this OCSP fetcher that are relevant to determine the revocation status of the given certificate.

class pyhanko_certvalidator.fetchers.api.OwnedFetcherBackend(backend: FetcherBackend)

Bases: object

Added in version 0.32.0.

Wrapper handing a backend’s lifetime over to whoever it is passed to.

Backends are ordinarily the caller’s to close, so passing one to a validation context leaves it untouched. Wrap it in this to say the opposite: the recipient provisioned it in all but name, and releases it when the operation it was provisioned for is over.

backend: FetcherBackend

The backend whose lifetime is being handed over.

pyhanko_certvalidator.fetchers.common_utils module

Internal backend-agnostic utilities to help process fetched certificates, CRLs and OCSP responses.

async pyhanko_certvalidator.fetchers.common_utils.complete_certificate_fetch_jobs(fetch_jobs)
pyhanko_certvalidator.fetchers.common_utils.crl_fetch_url_groups(cert: Certificate | AttributeCertificateV2, *, use_deltas: bool) → list[list[str]]

Group the HTTP(S) URLs from which CRLs for a certificate can be fetched into sets of alternatives.

URLs listed under the same distribution point are always alternatives. URLs from different distribution points are only considered alternatives if neither distribution point is limited to a subset of revocation reasons or delegates to a different CRL issuer. Complete CRLs and delta CRLs are never mixed in the same group.

Parameters:
  • cert – The certificate (or attribute certificate) of interest.

  • use_deltas – Whether to include delta CRL distribution points.

Returns:

A list of groups of URLs. Fetching one CRL from each group should be sufficient.

async pyhanko_certvalidator.fetchers.common_utils.crl_job_results_as_completed(jobs)
pyhanko_certvalidator.fetchers.common_utils.enumerate_delivery_point_urls(distribution_point: DistributionPoint)
async pyhanko_certvalidator.fetchers.common_utils.first_successful_fetch(jobs: Iterable[Awaitable[R]], kind: FetchKind) → R

Run fetch jobs concurrently, and return the result of the first one to complete successfully. The remaining jobs are then cancelled.

Parameters:
  • jobs – The jobs to run. These must be interchangeable.

  • kind – The kind of fetch being performed.

Returns:

The first successful result.

Raises:

The fetch error type matching kind if all jobs failed (or if there were no jobs at all).

pyhanko_certvalidator.fetchers.common_utils.format_ocsp_request(cert: Certificate, authority: Authority, *, certid_hash_algo: str, request_nonces: bool)
pyhanko_certvalidator.fetchers.common_utils.gather_aia_issuer_urls(cert: Certificate | AttributeCertificateV2)
async pyhanko_certvalidator.fetchers.common_utils.ocsp_job_get_earliest(jobs)
pyhanko_certvalidator.fetchers.common_utils.process_ocsp_response_data(response_data: bytes, *, ocsp_request: OCSPRequest, ocsp_url: str)
async pyhanko_certvalidator.fetchers.common_utils.queue_fetch_task(results: dict[T, R | Exception], running_jobs: dict[T, Event], tag: T, async_fun: Callable[[], Awaitable[R]]) → R | Exception
pyhanko_certvalidator.fetchers.common_utils.unpack_cert_content(response_data: bytes, content_type: str | None, url: str, permit_pem: bool)

Module contents

class pyhanko_certvalidator.fetchers.CRLFetcher

Bases: ABC

Utility interface to fetch and cache CRLs.

async fetch(cert: Certificate | AttributeCertificateV2, *, use_deltas=None) → Iterable[CertificateList]

Fetches the CRLs for a certificate.

Parameters:
  • cert – An asn1crypto.x509.Certificate object to get the CRL for

  • use_deltas – A boolean indicating if delta CRLs should be fetched

Raises:

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

Returns:

An iterable of CRLs fetched.

fetched_crls() → Iterable[CertificateList]

Return all CRLs fetched by this CRL fetcher.

fetched_crls_for_cert(cert: Certificate | AttributeCertificateV2) → Iterable[CertificateList]

Return all relevant fetched CRLs for the given certificate

Parameters:

cert – A certificate.

Returns:

An iterable of CRLs

Raises:

KeyError – if no fetch operations have been performed for this certificate

class pyhanko_certvalidator.fetchers.CertificateFetcher

Bases: ABC

Utility interface to fetch and cache certificates.

fetch_cert_issuers(cert: Certificate | AttributeCertificateV2) → AsyncGenerator[Certificate, None]

Fetches certificates from the authority information access extension of a certificate.

Parameters:

cert – A certificate

Raises:

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

Returns:

An asynchronous generator yielding asn1crypto.x509.Certificate objects that were fetched.

fetch_crl_issuers(certificate_list) → AsyncGenerator[Certificate, None]

Fetches certificates from the authority information access extension of an asn1crypto.crl.CertificateList.

Parameters:

certificate_list – An asn1crypto.crl.CertificateList object

Raises:

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

Returns:

An asynchronous generator yielding asn1crypto.x509.Certificate objects that were fetched.

fetched_certs() → Iterable[Certificate]

Return all certificates retrieved by this certificate fetcher.

class pyhanko_certvalidator.fetchers.FetcherBackend

Bases: ABC

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 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 Fetchers objects to the validation context yourself, and forgo use of the FetcherBackend API altogether.

get_fetchers() → Fetchers

Set up fetchers synchronously.

Note

This is a synchronous method

async close()

Clean up the resources associated with this fetcher backend, asynchronously.

class pyhanko_certvalidator.fetchers.Fetchers(ocsp_fetcher: OCSPFetcher, crl_fetcher: CRLFetcher, cert_fetcher: CertificateFetcher)

Bases: object

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 FetcherBackend.

ocsp_fetcher: OCSPFetcher
crl_fetcher: CRLFetcher
cert_fetcher: CertificateFetcher
class pyhanko_certvalidator.fetchers.OCSPFetcher

Bases: ABC

Utility interface to fetch and cache OCSP responses.

async fetch(cert: Certificate | AttributeCertificateV2, authority: Authority) → OCSPResponse

Fetch an OCSP response for a certificate.

Parameters:
  • cert – The certificate for which an OCSP response has to be fetched.

  • authority – The issuing authority.

Raises:

OCSPFetchError - Raised if an OCSP response could not be obtained.

Returns:

An OCSP response.

fetched_responses() → Iterable[OCSPResponse]

Return all responses fetched by this OCSP fetcher.

fetched_responses_for_cert(cert: Certificate | AttributeCertificateV2) → Iterable[OCSPResponse]

Return all responses fetched by this OCSP fetcher that are relevant to determine the revocation status of the given certificate.

class pyhanko_certvalidator.fetchers.OwnedFetcherBackend(backend: FetcherBackend)

Bases: object

Added in version 0.32.0.

Wrapper handing a backend’s lifetime over to whoever it is passed to.

Backends are ordinarily the caller’s to close, so passing one to a validation context leaves it untouched. Wrap it in this to say the opposite: the recipient provisioned it in all but name, and releases it when the operation it was provisioned for is over.

backend: FetcherBackend

The backend whose lifetime is being handed over.