How to get public key using PyOpenSSL?

I am trying to create a python script that will accept PKCS # 12 package and print some information contained in x509 certificate and use PyOpenSSL purpouses for this module. For now, I want to get a certificate from the public key. But the PKey object does not have a corresponding method. Where can I leave here? Any ideas on how to get the public key?

pfx=open('./1.p12','rb').read()
PKCS=crypto.load_pkcs12(pfx)
cert=PKCS.get_certificate()
PKey=cert.get_pubkey()

print PKey
<OpenSSL.crypto.PKey object at 0x012432D8>

Thank.

+5
source share
3 answers

First you can download the certificate as follows

from OpenSSL import crypto

#cert is the encrypted certificate int this format -----BEGIN -----END    
crtObj = crypto.load_certificate(crypto.FILETYPE_PEM, cert)
pubKeyObject = crtObj.get_pubkey()
pubKeyString = crypto.dump_publickey(crypto.FILETYPE_PEM,pubKeyObject)
print pubKeyString

you will see something like

-----BEGIN PUBLIC KEY----- 
....
....
-----END PUBLIC KEY-----
0
source

Will this work?

print PKey
<OpenSSL.crypto.PKey object at 0x012432D8>

from OpenSSL import crypto

crypto.dump_privatekey(PKey)
-2
source

Use instead:

c.dump_privatekey(c.FILETYPE_TEXT,pubkey)
-2
source

All Articles