Utilities
aia_chaser.utils.cert_utils
¶
AiaInformation
¶
Bases: NamedTuple
Authority Information Access (AIA) values.
build_certificate_chain(leaf_cert, certs_map)
¶
Builds a certificate chain from the leaf_cert to the root CA.
| PARAMETER | DESCRIPTION |
|---|---|
leaf_cert
|
Leaf certificate of the chain.
TYPE:
|
certs_map
|
Mapping from
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Certificate]
|
A certificate chain starting at |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
An issuer is not found in |
build_certificate_chains(certificates)
¶
Builds all certificate chains found in certificates.
First it looks for all leaf certificates using
find_leaf_certificates
and then builds the chains starting at each leaf using
build_certificate_chain.
| RETURNS | DESCRIPTION |
|---|---|
list[list[Certificate]]
|
All certificate chains found in |
certificates_to_der(certificates)
¶
DER representation of the given certificates.
| RETURNS | DESCRIPTION |
|---|---|
bytes
|
A bytes object with the DER content of |
certificates_to_pem(certificates)
¶
PEM representation of the given certificates.
| RETURNS | DESCRIPTION |
|---|---|
str
|
A string with the PEM content of |
extract_aia_information(certificate)
¶
Extract authority information access (AIA) from a certificate.
| PARAMETER | DESCRIPTION |
|---|---|
certificate
|
Certificate from which extract AIA information.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
AiaInformation
|
The extracted CA issues and OCSP servers. |
Note
If the certificate does not have the AIA extension this function does not fail, it fallbacks to returning empty sequences of data.
extract_crl_urls(certificate)
¶
Extract CRL distribution points from a certificate.
find_leaf_certificates(certificates)
¶
Finds leaf certificates.
A certificate is considered a leaf certificate if its subject is not found as the issuer of another certificate from the list.
| RETURNS | DESCRIPTION |
|---|---|
list[Certificate]
|
List with the certificates that are not issuers of any of the other provided certificates. |
force_load_default_verify_certificates(context)
¶
Forcefully load default verify certificates into the SSL context.
Certificates in CA path directory are not loaded unless they have been used at leas one by the SSL context.
This function loads all files located in CA path, except those that are considered hidden files (start with a ‘.’).
| PARAMETER | DESCRIPTION |
|---|---|
context
|
The SSL context to load the verify certificates into.
TYPE:
|
load_ssl_ca_certificates(context=None, *, force_load=True)
¶
Load CA certificates available to Python’s ssl.
| PARAMETER | DESCRIPTION |
|---|---|
context
|
The SSL context used to get the default certificates.
If not provided a default context is created with
TYPE:
|
force_load
|
Forcefully load default certificates into the SSL context. Certificates in CA path directory are not loaded unless they have been used at leas one by the SSL context. For more information see
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Certificate]
|
A list with the CA certificates from |
select_rsa_padding_for_signature_algorithm_oid(signature_alg_oid, signature_hash_alg)
¶
Select padding for a given signature algorithm OID.
temp_pem_file(certificates)
¶
Context manager that writes certificates to a temporary PEM file.
Creates a temporary directory containing a PEM file with the provided certificates. The file path is yielded for use with libraries that require a file path (e.g., pycurl).
Note
This uses a TemporaryDirectory instead of NamedTemporaryFile because on Windows, a file opened by one process cannot be accessed by another process. By writing to a file in a temp directory and closing it before yielding, we ensure cross-platform compatibility.
| PARAMETER | DESCRIPTION |
|---|---|
certificates
|
Sequence of certificates to write to the PEM file.
TYPE:
|
| YIELDS | DESCRIPTION |
|---|---|
Path
|
Path to the temporary PEM file. |
Example
chaser = AiaChaser()
ca_chain = chaser.fetch_ca_chain_for_url(url)
with temp_pem_file(ca_chain) as pem_path:
response = requests.get(url, verify=str(pem_path))
aia_chaser.utils.url
¶
extract_host_port_from_url(url_string)
¶
Extract host and port from a URL string.
If the port is not explicitly specified it will be inferred from the scheme.
| PARAMETER | DESCRIPTION |
|---|---|
url_string
|
URL from which to extract the host.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[str, int]
|
The host (netloc) of |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the host or port cannot be extracted from the given URL. It may happen with a seemingly correct URL if it is missing the scheme component. |