Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyOpenSSL reading certificate/pkey file

That's how i create certificate

  from OpenSSL import crypto

  cert = crypto.X509()
  cert.get_subject().C            = countryName
  cert.get_subject().ST           = stateOrProvinceName
  ...


Here what generation looks like.
Now, how do I extract those values fomr certificate using PyOpenSSL backwards from plain files?

So here's what I cameup with

def certext(certstr):
  p1 = Popen(['printf', certstr], stdout=PIPE)
  p2 = Popen(['openssl', 'x509', '-text'], stdin=p1.stdout, stdout=PIPE)
  p1.stdout.close()
  output = p2.communicate()[0]
  return output
like image 686
user2013697 Avatar asked Jan 28 '13 15:01

user2013697


1 Answers

You can load a PEM certificate as follows:

import OpenSSL.crypto

st_cert=open(certfile, 'rt').read()

c=OpenSSL.crypto
cert=c.load_certificate(c.FILETYPE_PEM, st_cert)

and a private key with:

st_key=open(keyfile, 'rt').read()
key=c.load_privatekey(c.FILETYPE_PEM, st_key)

where certfile and keyfile are the filenames.

like image 171
V13 Avatar answered Sep 18 '22 17:09

V13