p12 certs to pem etc

18 views
Skip to first unread message

MikeKJ

unread,
Apr 27, 2017, 9:07:20 AM4/27/17
to Django users
Using this snippet from The Random Engineer

from OpenSSL.crypto import load_pkcs12, FILETYPE_PEM, FILETYPE_ASN1
 
with open('cert.p12', 'rb') as f:
  c = f.read()
 
p = load_pkcs12(c, 'passphrase')
 
certificate = p.get_certificate()
private_key = p.get_privatekey()
 
# Where type is FILETYPE_PEM or FILETYPE_ASN1 (for DER).
type_ = FILETYPE_PEM
 
prvkey = OpenSSL.crypto.dump_privatekey(type_, private_key)
cert = OpenSSL.crypto.dump_certificate(type_, certificate)

and a little later I use

                headers = {"username": "password"}
                context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
                context.load_cert_chain(cert, prvkey, password=password)
                context.verify_mode = ssl.CERT_REQUIRED
                context.check_hostname = True
                conn = httplib.HTTPSConnection(target_url, port=442, context=oontext)
                conn.request("POST", "/to/this/place", xml, headers)
                response = conn.getresponse()
                data = response.read()

I can 'see' the content of prvkey and cert with a raise NameError BUT this line

context.load_cert_chain(cert, prvkey, password=password)

throws an I/O error ErrNo36 file name too long and I have no idea why can anyone shed some light on this please?
Reply all
Reply to author
Forward
0 new messages