Skip to main content

x509-utils

x509-utils is a Python library for parsing and normalizing X.509 certificates in Base64 DER format. It extracts key certificate details such as the serial number, subject DN, issuer DN, and validity periods while providing support for consistent DN normalization.

Features

  • Parse Base64-encoded X.509 certificates in DER format.
  • Extract certificate details including:
    • Serial Number
    • Subject Distinguished Name (DN)
    • Issuer Distinguished Name (DN)
    • Validity Period (Not Before and Not After)
  • Normalize Distinguished Names for consistent formatting.
  • Lightweight and easy to integrate into cryptographic and PKI workflows.

Installation

Install the library via pip:

pip install x509-utils

Usage

Here's an example of how to use x509-utils to parse an X.509 certificate:

from x509_utils import parse_x509_certificate

# Base64-encoded DER certificate
client_cert: str = """
<insert your Base64-encoded certificate here>
"""

# Parse the certificate
decoded_cert = parse_x509_certificate(client_cert)

# Access certificate details
print("Serial Number:", decoded_cert["serial"])
print("Subject DN (raw):", decoded_cert["subject_dn"]["raw"])
print("Subject DN (normalized):", decoded_cert["subject_dn"]["normalized"])
print("Issuer DN (raw):", decoded_cert["issuer_dn"]["raw"])
print("Issuer DN (normalized):", decoded_cert["issuer_dn"]["normalized"])
print("Validity Period (Not Before):", decoded_cert["not_valid"]["before"])
print("Validity Period (Not After):", decoded_cert["not_valid"]["after"])

API Reference

parse_x509_certificate(cert_base64: str) -> dict

Parses a Base64-encoded X.509 certificate in DER format.

Arguments

  • cert_base64 (str): Base64-encoded DER-formatted X.509 certificate.

Returns

A dictionary with the following keys:

  • serial: The certificate serial number.
  • subject_dn: A dictionary containing the raw and normalized subject DN.
  • issuer_dn: A dictionary containing the raw and normalized issuer DN.
  • not_valid: A dictionary with before and after datetime objects indicating the validity period.

Raises

  • ValueError: If the certificate is invalid or parsing fails.

safe_dn(dn: str) -> str

Normalizes a Distinguished Name (DN) string by sorting and formatting components consistently.

Arguments

  • dn (str): The DN string to normalize.

Returns

  • A normalized DN string.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit issues or pull requests to help improve this library.

Generating a self-signed certificate

# Generate a Private Key
openssl genrsa -out tmp/test.key 2048

# Generate a Certificate Signing Request (CSR)
openssl req -new -key tmp/test.key -out tmp/test.csr -subj "/C=US/ST=Virginia/L=McLean/O=Test Company/OU=Engineering/CN=example.com"

# Generate a Self-Signed Certificate
openssl x509 -req -days 365 -in tmp/test.csr -signkey tmp/test.key -out tmp/test.crt

# Convert the Certificate to DER Format
openssl x509 -in tmp/test.crt -outform DER -out tmp/test.der

# Base64 Encode the DER File
base64 tmp/test.der > tmp/test_cert.base64

Metadata

Release files for x509-toolkit 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for x509-toolkit 0.0.1
File Size Uploaded
x509_toolkit-0.0.1.tar.gz 8.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for x509-toolkit 0.0.1
File Interpreter ABI Platform
x509_toolkit-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 17.5 kB

Release files / x509_toolkit-0.0.1.tar.gz

Download URL x509_toolkit-0.0.1.tar.gz
Size 8.5 kB
Tags Source
SHA-256 checksum
How to use checksums
5a29630f0ed6d1e4237c81ede9d97ada34369af1f2ea168c48198ddc54c03a00
BLAKE2b-256 checksum
How to use checksums
01144ffe9342261bc5ae44ee38007b4e7c89ed0af30d975f1d6c77e729f2a6ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.10.12 Linux/6.5.0-1025-azure

Release files / x509_toolkit-0.0.1-py3-none-any.whl

Download URL x509_toolkit-0.0.1-py3-none-any.whl
Size 9.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
45e5819866100998a4af23c24b541e808938f0cb051e224443d9e8bcc75c1371
BLAKE2b-256 checksum
How to use checksums
89b16472bf69d38f41f9767c44362bff3a8510bb7bb0a318e29e8fb3128fe257
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.10.12 Linux/6.5.0-1025-azure

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page