Skip to content

Instantly share code, notes, and snippets.

@wsgac
Last active November 23, 2023 15:54
Show Gist options
  • Save wsgac/59a19e8af972eb228136b19dfd7cada0 to your computer and use it in GitHub Desktop.
Save wsgac/59a19e8af972eb228136b19dfd7cada0 to your computer and use it in GitHub Desktop.
Parse, modify and write an x509 certificate with Python (WIP)
from cryptography import x509
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives import serialization
import os
# Read cert from file
with open("cert.pem", "rb") as f:
cert= x509.load_pem_x509_certificate(f.read(), default_backend())
# This causes problems due to `subject` attribute not being writable
# Edit subject entry of the certificate
cert.subject = x509.Name([x509.NameAttribute(x509.NameOID.COMMON_NAME, "example.com")])
# Write cert to file
with open("cert.crt.bak", "wb") as f:
f.write(cert.public_bytes(encoding=serialization.Encoding.PEM))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment