6

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?

MattDMo
  • 100,794
  • 21
  • 241
  • 231
ab1127
  • 95
  • 1
  • 1
  • 6
  • I did similar task using command line, which I call from Python. Most of the time I spent learning, how to use the command line, you are done with that, so either calling it from Python (using `subprocess.call` or something similar), or `pyOpenSSL` as proposed by "ele" shall work. – Jan Vlcinsky May 08 '14 at 20:48
  • I too am using something like os.system() for creating the pem. But I run the command each time, I have to enter passphrase. – ab1127 May 08 '14 at 21:27
  • Better use `subprocess.call` and similar. `os.system` is deprecated. For instructions how to replace it see https://docs.python.org/2/library/subprocess.html#subprocess-replacements For the passphrase - check OpenSSL options, you can provide these values over command line too. – Jan Vlcinsky May 08 '14 at 21:30
  • possible duplicate of [Need help converting p12 certificate into PEM using OpenSSL](http://stackoverflow.com/questions/15144046/need-help-converting-p12-certificate-into-pem-using-openssl) – Jan Vlcinsky May 08 '14 at 21:36

1 Answers1

16

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.

Kondasamy Jayaraman
  • 1,802
  • 1
  • 20
  • 25
DIDoS
  • 812
  • 9
  • 23