Skip to main content

A Python library for parsing and normalizing X.509 certificates in Base64 DER format. It extracts key details like serial number, subject DN, issuer DN, and validity periods, with support for consistent DN normalization.

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

x509_toolkit-0.0.1.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

x509_toolkit-0.0.1-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file x509_toolkit-0.0.1.tar.gz.

File metadata

  • Download URL: x509_toolkit-0.0.1.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for x509_toolkit-0.0.1.tar.gz
Algorithm Hash digest
SHA256 5a29630f0ed6d1e4237c81ede9d97ada34369af1f2ea168c48198ddc54c03a00
MD5 20f343b7ece1cc7875cd09c7c3d07673
BLAKE2b-256 01144ffe9342261bc5ae44ee38007b4e7c89ed0af30d975f1d6c77e729f2a6ee

See more details on using hashes here.

File details

Details for the file x509_toolkit-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: x509_toolkit-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for x509_toolkit-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 45e5819866100998a4af23c24b541e808938f0cb051e224443d9e8bcc75c1371
MD5 9c8c5e3f8992880714317d87d8d079ba
BLAKE2b-256 89b16472bf69d38f41f9767c44362bff3a8510bb7bb0a318e29e8fb3128fe257

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page