Index
๐ค AI-Generated Content
This documentation was generated with AI assistance and is still being audited. Some, or potentially a lot, of this information may be inaccurate. Learn more.
provide.foundation.crypto.certificates
¶
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.
CertificateBase
¶
Immutable base certificate data.
Functions¶
create
classmethod
¶
Create a new certificate base and private key.
Source code in provide/foundation/crypto/certificates/base.py
CertificateError
¶
Bases: ValidationError
Certificate-related errors.
Source code in provide/foundation/crypto/certificates/base.py
Functions¶
create_ca
¶
create_ca(
common_name: str,
organization: str = "Default CA Organization",
validity_days: int = DEFAULT_CERTIFICATE_VALIDITY_DAYS
* 2,
key_type: str = DEFAULT_CERTIFICATE_KEY_TYPE,
) -> Certificate
Create a CA certificate (convenience function).
Source code in provide/foundation/crypto/certificates/factory.py
create_self_signed
¶
create_self_signed(
common_name: str = "localhost",
alt_names: list[str] | None = None,
organization: str = "Default Organization",
validity_days: int = DEFAULT_CERTIFICATE_VALIDITY_DAYS,
key_type: str = DEFAULT_CERTIFICATE_KEY_TYPE,
) -> Certificate
Create a self-signed certificate (convenience function).
Source code in provide/foundation/crypto/certificates/factory.py
create_x509_certificate
¶
create_x509_certificate(
base: CertificateBase,
private_key: KeyPair,
alt_names: list[str] | None = None,
issuer_name_override: Name | None = None,
signing_key_override: KeyPair | None = None,
is_ca: bool = False,
is_client_cert: bool = False,
) -> X509Certificate
Internal helper to build and sign the X.509 certificate object.
Source code in provide/foundation/crypto/certificates/operations.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
validate_signature
¶
validate_signature(
signed_cert_obj: Certificate,
signing_cert_obj: Certificate,
signing_public_key: PublicKey,
) -> bool
Internal helper: Validates signature and issuer/subject match.