Python p12 to pem

ab1127 picture ab1127 · May 8, 2014 · Viewed 10.4k times · Source

I am trying to understand about openssl and certificates and also Python.

So I have this .cert.p12 file. I would like to convert it to .pem format.

I use

openssl -in input.cert.p12 -out output.pem -nodes

This creates the pem file.

How would I do the same process in Python? Take in a p12 file and covert it to a pem format?

Answer

DIDoS picture DIDoS · May 8, 2014

Try using an OpenSSL for Python library like "pyOpenSSL"

https://pyopenssl.org/en/stable/api/crypto.html#pkcs12-objects

from OpenSSL import crypto
p12 = crypto.load_pkcs12(file("push.p12", 'rb').read(), [password])

# PEM formatted private key
print crypto.dump_privatekey(crypto.FILETYPE_PEM, p12.get_privatekey())

# PEM formatted certificate
print crypto.dump_certificate(crypto.FILETYPE_PEM, p12.get_certificate())

from here.