It's fairly straight-forward to use. This isn't tested, but should work:
# load OpenSSL.crypto
from OpenSSL import crypto
# open it, using password. Supply/read your own from stdin.
p12 = crypto.load_pkcs12(open("/path/to/cert.p12", 'rb').read(), passwd)
# get various properties of said file.
# note these are PyOpenSSL objects, not strings although you
# can convert them to PEM-encoded strings.
p12.get_certificate() # (signed) certificate object
p12.get_privatekey() # private key.
p12.get_ca_certificates() # ca chain.
For more examples, have a look through the unit test code of pyopenssl. Pretty much every way you might want to use the library is there
See also here or without adverts here.
Answer from user257111 on Stack OverflowIt's fairly straight-forward to use. This isn't tested, but should work:
# load OpenSSL.crypto
from OpenSSL import crypto
# open it, using password. Supply/read your own from stdin.
p12 = crypto.load_pkcs12(open("/path/to/cert.p12", 'rb').read(), passwd)
# get various properties of said file.
# note these are PyOpenSSL objects, not strings although you
# can convert them to PEM-encoded strings.
p12.get_certificate() # (signed) certificate object
p12.get_privatekey() # private key.
p12.get_ca_certificates() # ca chain.
For more examples, have a look through the unit test code of pyopenssl. Pretty much every way you might want to use the library is there
See also here or without adverts here.
As pyOpenSSL.crypto.load_pkcs12 is now deprecated, here is the equivalent solution using cryptography, with loading inside a requests Session as a bonus.
from cryptography.hazmat.primitives import serialization
from requests import Session
with open("./cert.p12", "rb") as f:
(
private_key,
certificate,
additional_certificates,
) = serialization.pkcs12.load_key_and_certificates(
f.read(), CLIENT_CERT_KEY.encode()
)
# key will be available in user readable temporary file for the time of the
# program run (until key and cert get gc'ed)
key = tempfile.NamedTemporaryFile()
cert = tempfile.NamedTemporaryFile()
key.write(
private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption(),
)
)
key.flush()
cert.write(
certificate.public_bytes(serialization.Encoding.PEM),
)
cert.flush()
session = Session()
session.cert = (cert.name, key.name)
`AttributeError: module 'OpenSSL.crypto' has no attribute 'PKCS12'` under f41
PyOpenSSL has removed deprecated PKCS12 breaking --shadow-credentials in ntlmrelayx.py
pyopenssl - python crypto.sign not found, though it's in the module - Stack Overflow
Exception: module 'OpenSSL.crypto' has no attribute 'PKCS12Type'
» pip install pyOpenSSL
For people not using openssl who have this problem (missing sign method) there is another solution.
Check the import section on the top of your code, you need to have this import :
from Crypto.Signature import PKCS1_v1_5
and not the Crypto.Cipher implementation:
from Crypto.Cipher import PKCS1_v1_5
This class doesn't have the sign method: https://www.dlitz.net/software/pycrypto/api/2.6/Crypto.Cipher.PKCS1_v1_5.PKCS115_Cipher-class.html
The Crypto.Signature PKCS1_v1_5 class has the sign method :https://www.dlitz.net/software/pycrypto/api/2.6/Crypto.Signature.PKCS1_v1_5.PKCS115_SigScheme-class.html
I apparently had some conflicting installations - I removed a couple of yum python-crypto packages, then pip uninstalled openssl and there was still stuff in /usr/lib64/python2.6/site-packages/OpenSSL/ (including crypto.so), so I manually removed that directory and then pip installed pyopenssl and that solved the problem. Thanks for the pointers...