Certificate
provide.foundation.crypto.certificates.certificate
¶
TODO: Add module docstring.
Classes¶
Certificate
¶
X.509 certificate management using attrs.
This class should be instantiated via factory methods: - Certificate.from_pem() - Load from PEM strings - Certificate.generate() - Generate new certificate - Certificate.create_ca() - Generate CA certificate - Certificate.create_signed_certificate() - Generate signed certificate - Certificate.create_self_signed_server_cert() - Generate self-signed server cert - Certificate.create_self_signed_client_cert() - Generate self-signed client cert
Attributes¶
is_ca
property
¶
Checks if the certificate has the Basic Constraints CA flag set to True.
is_valid
cached
property
¶
Checks if the certificate is currently valid based on its dates.
public_key
property
¶
Returns the public key object from the certificate.
trust_chain
property
writable
¶
Returns the list of trusted certificates associated with this one.
Functions¶
__eq__
¶
Custom equality based on subject and serial number.
Source code in provide/foundation/crypto/certificates/certificate.py
__hash__
¶
Custom hash based on subject and serial number.
Source code in provide/foundation/crypto/certificates/certificate.py
create_ca
classmethod
¶
create_ca(
common_name: str,
organization_name: str,
validity_days: int,
key_type: str = DEFAULT_CERTIFICATE_KEY_TYPE,
key_size: int = DEFAULT_RSA_KEY_SIZE,
ecdsa_curve: str = DEFAULT_CERTIFICATE_CURVE,
) -> Certificate
Creates a new self-signed CA certificate.
Source code in provide/foundation/crypto/certificates/certificate.py
create_self_signed_client_cert
classmethod
¶
create_self_signed_client_cert(
common_name: str,
organization_name: str,
validity_days: int,
alt_names: list[str] | None = None,
key_type: str = DEFAULT_CERTIFICATE_KEY_TYPE,
key_size: int = DEFAULT_RSA_KEY_SIZE,
ecdsa_curve: str = DEFAULT_CERTIFICATE_CURVE,
) -> Certificate
Creates a new self-signed end-entity certificate suitable for a client.
Source code in provide/foundation/crypto/certificates/certificate.py
create_self_signed_server_cert
classmethod
¶
create_self_signed_server_cert(
common_name: str,
organization_name: str,
validity_days: int,
alt_names: list[str] | None = None,
key_type: str = DEFAULT_CERTIFICATE_KEY_TYPE,
key_size: int = DEFAULT_RSA_KEY_SIZE,
ecdsa_curve: str = DEFAULT_CERTIFICATE_CURVE,
) -> Certificate
Creates a new self-signed end-entity certificate suitable for a server.
Source code in provide/foundation/crypto/certificates/certificate.py
create_signed_certificate
classmethod
¶
create_signed_certificate(
ca_certificate: Certificate,
common_name: str,
organization_name: str,
validity_days: int,
alt_names: list[str] | None = None,
key_type: str = DEFAULT_CERTIFICATE_KEY_TYPE,
key_size: int = DEFAULT_RSA_KEY_SIZE,
ecdsa_curve: str = DEFAULT_CERTIFICATE_CURVE,
is_client_cert: bool = False,
) -> Certificate
Creates a new certificate signed by the provided CA certificate.
Source code in provide/foundation/crypto/certificates/certificate.py
from_pem
classmethod
¶
Load certificate from PEM strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cert_pem
|
str
|
Certificate in PEM format (string or URI) |
required |
key_pem
|
str | None
|
Optional private key in PEM format (string or URI) |
None
|
Returns:
| Type | Description |
|---|---|
Certificate
|
Certificate instance |
Raises:
| Type | Description |
|---|---|
CertificateError
|
If loading fails |
Example
cert = Certificate.from_pem(cert_pem_string, key_pem_string) assert cert.is_valid
Source code in provide/foundation/crypto/certificates/certificate.py
generate
classmethod
¶
generate(
common_name: str = DEFAULT_CERTIFICATE_COMMON_NAME,
organization_name: str = DEFAULT_CERTIFICATE_ORGANIZATION_NAME,
validity_days: int = DEFAULT_CERTIFICATE_VALIDITY_DAYS,
key_type: str = DEFAULT_CERTIFICATE_KEY_TYPE,
key_size: int = DEFAULT_RSA_KEY_SIZE,
ecdsa_curve: str = DEFAULT_CERTIFICATE_CURVE,
alt_names: list[str] | None = None,
is_ca: bool = False,
is_client_cert: bool = True,
) -> Certificate
Generate a new certificate with a new keypair.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
common_name
|
str
|
Certificate common name |
DEFAULT_CERTIFICATE_COMMON_NAME
|
organization_name
|
str
|
Organization name |
DEFAULT_CERTIFICATE_ORGANIZATION_NAME
|
validity_days
|
int
|
Number of days certificate is valid |
DEFAULT_CERTIFICATE_VALIDITY_DAYS
|
key_type
|
str
|
Key type ("rsa" or "ecdsa") |
DEFAULT_CERTIFICATE_KEY_TYPE
|
key_size
|
int
|
RSA key size in bits |
DEFAULT_RSA_KEY_SIZE
|
ecdsa_curve
|
str
|
ECDSA curve name |
DEFAULT_CERTIFICATE_CURVE
|
alt_names
|
list[str] | None
|
Subject alternative names |
None
|
is_ca
|
bool
|
Whether this is a CA certificate |
False
|
is_client_cert
|
bool
|
Whether this is a client certificate |
True
|
Returns:
| Type | Description |
|---|---|
Certificate
|
New Certificate instance |
Example
cert = Certificate.generate( ... common_name="example.com", ... organization_name="Example Corp", ... ) assert cert._private_key is not None
Source code in provide/foundation/crypto/certificates/certificate.py
verify_trust
¶
Verifies if the other_cert is trusted based on this certificate's trust chain.