When it comes to working with certificates in Python, no one package has all of the answers. Without considering more advanced schemes (ECC), most of the key and certificate functionality will be in one of the following packages:
In general, ssl can handle SSL sockets and HTTPS connections, M2Crypto can handle RSA/DSA keys and certificates, and pyopenssl can handle P12 certificates. There is some role overlap:
- pyopenssl and M2Crypto both do X509 certificate deconstruction
- ssl does PEM/DER conversions
Since the reason that I’m doing this post is because of the obscureness of reading P12 certificates in Python, here’s an example of doing so:
from OpenSSL.crypto import load_pkcs12, FILETYPE_PEM, FILETYPE_ASN1 with open('cert.p12', 'rb') as f: c = f.read() p = load_pkcs12(c, 'passphrase') certificate = p.get_certificate() private_key = p.get_privatekey() # Where type is FILETYPE_PEM or FILETYPE_ASN1 (for DER). type_ = FILETYPE_PEM OpenSSL.crypto.dump_privatekey(type_, private_key) OpenSSL.crypto.dump_certificate(type_, certificate) # Get CSR fields (as a list of 2-tuples). fields = certificate.get_subject().get_components() print(fields)
Image may be NSFW.
Clik here to view.

Clik here to view.
