Skip to content

Instantly share code, notes, and snippets.

@akash0x53
Created December 24, 2014 09:27
Show Gist options
  • Save akash0x53/564ff30fd584145e5e7d to your computer and use it in GitHub Desktop.
Save akash0x53/564ff30fd584145e5e7d to your computer and use it in GitHub Desktop.
Extract key, certificate from pkcs12
def extract_cert_key(certpath=None):
"""
Extracts private key & certificate and returns as pem string.
params:
certpath = Valid P12 certificate file path.
returns:
(certificate, privatekey)
"""
import os
import getpass
import OpenSSL.crypto as crypto
assert certpath, None
certpath = os.path.realpath(certpath)
paswd = getpass.getpass()
pk12 = crypto.load_pkcs12(open(certpath).read(), paswd)
return crypto.dump_certificate(crypto.FILETYPE_PEM, pk12.get_certificate()),\
crypto.dump_privatekey(crypto.FILETYPE_PEM, pk12.get_privatekey())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment