X.509
Parse, build and verify X.509 certificates, PKCS#10 certificate signing requests and CRLs.
import datetime as dt
from oxcrypto.pki.x509 import X509
from oxcrypto.signature.ecdsa import ECDSA
from oxcrypto.signature.ed25519 import ED25519
X = X509.RFC5280
start = dt.datetime(2026, 1, 1, tzinfo=dt.timezone.utc)
end = dt.datetime(2036, 1, 1, tzinfo=dt.timezone.utc)
ca_key = ECDSA.P256.generate()
ca = X.build_certificate(
ca_key.public_key(), ca_key, subject="CN=Example CA", serial_number=1,
not_valid_before=start, not_valid_after=end, ca=True,
)
leaf_key = ED25519.ED25519.generate()
csr = X.build_csr(leaf_key, subject="CN=leaf.example", dns_names=["leaf.example"])
assert csr.verify()
leaf = X.build_certificate(
csr.public_key(), ca_key, subject=csr.subject, issuer=ca, serial_number=2,
not_valid_before=start, not_valid_after=end, dns_names=["leaf.example"],
)
assert leaf.verify(ca.public_key())
crl = X.build_crl(ca, ca_key, crl_number=1, this_update=start, next_update=end,
revoked=[(leaf.serial_number, start)])
assert crl.verify(ca.public_key())Loading and encoding
X509.RFC5280 loads DER and PEM with load_der_certificate,
load_pem_certificate, load_der_csr, load_pem_csr, load_der_crl and
load_pem_crl. PEM input must carry the CERTIFICATE,
CERTIFICATE REQUEST or X509 CRL label. Every object has to_der() and
to_pem(), compares equal by its DER encoding and is hashable.
Certificates expose subject and issuer (RFC 4514 strings),
serial_number, not_valid_before, not_valid_after (UTC datetime),
signature_algorithm (dotted OID) and extensions, a list of
(oid, critical, der_value) tuples. Requests expose subject,
signature_algorithm and the requested extensions. CRLs expose issuer,
this_update, next_update and revoked_certificates, a list of
(serial_number, revocation_date) tuples.
Verification
certificate.verify(public_key) and crl.verify(public_key) check the
signature with the issuer's public key. csr.verify() checks the request
against its own public key. They return False for a wrong key or a bad
signature and check nothing else: validity dates, extensions and the issuer
name are left to path validation.
Building
build_certificate(public_key, issuer_private_key, *, subject, serial_number, not_valid_before, not_valid_after, issuer=None, ca=False, path_length=None, dns_names=[], algorithm=None) builds a version 3 certificate. With
issuer=None it is self-signed; otherwise issuer is the issuer's
certificate and issuer_private_key must match its public key. Builders add
Subject Key Identifier, Authority Key Identifier, Basic Constraints, Key Usage
(digitalSignature, plus keyCertSign and cRLSign for CAs) and, for
dns_names, Subject Alternative Name.
build_csr(private_key, *, subject, dns_names=[], algorithm=None) builds a
PKCS#10 request, with an extension request for dns_names.
build_crl(issuer, issuer_private_key, *, crl_number, this_update, next_update=None, revoked=[], algorithm=None) builds a version 2 CRL. RFC 5280
requires next_update, and rustls-webpki rejects CRLs without it.
Times must be timezone-aware and between 1970 and 9999, and serial numbers
must be positive and below 2**159. algorithm selects the RSA scheme or the
ECDSA hash; see the algorithm table.
Upstream
| Crate | Standard | Audit |
|---|---|---|
x509-cert 0.3.1, der 0.8, spki 0.8 | RFC 5280, RFC 2986, RFC 4055, RFC 5480, RFC 8410, RFC 9881 | No published audit |
Tests use certificates and CRLs from the NIST PKITS 2011 suite (tests 4.1.1
to 4.1.3 and 4.4.3) and the RustCrypto x509-cert RSA CSR fixture.