0
#!/usr/bin/python
# -*- coding: utf-8 -*-
#coding:utf8
from OpenSSL import crypto

class  CertToString(object):
    def __init__(self):
        pass

    def dump_certificate(self):
        p12 = crypto.load_pkcs12(file(r'cert.p12','rb').read(), '')
        p12_certificate = p12.get_certificate()
        p12_privatekey = p12.get_privatekey()

        certificate = crypto.dump_certificate(crypto.FILETYPE_PEM, p12_certificate)
        private_key = crypto.dump_privatekey(crypto.FILETYPE_PEM, p12_privatekey,)

        print certificate
        print private_key

if __name__ == "__main__":

     certToStr = CertToString()
     certToStr.dump_certificate()

print out result is:

 -----BEGIN CERTIFICATE-----

  xxxxxxxxxxx

  -----END CERTIFICATE-----


  -----BEGIN RSA PRIVATE KEY-----

  xxxxxxxxxx

  -----END RSA PRIVATE KEY-----


  openssl convert apple push notification cert.p12 to cert.pem in command:

    $)openssl pkcs12 -in cert.p12 -out cert.pem -nodes

    $)cat cert.pem


Bag Attributes

   friendlyName: xxxxxxxxx

   localKeyID: xxxxxxxxx

subject=xxxxxx

issuer=xxxxxxx

-----BEGIN CERTIFICATE-----

xxxxxxxxxxx

-----END CERTIFICATE-----

Bag Attributes

   friendlyName: xxxx

   localKeyID: xxxxxxx

Key Attributes: <No Attributes>

-----BEGIN PRIVATE KEY-----

xxxxxxxxxxx

-----END PRIVATE KEY-----

the pem file have "-----BEGIN CERTIFICATE-----xxxx-----END CERTIFICATE-----"and"-----BEGIN PRIVATE KEY-----xxxxxxxxxxx-----END PRIVATE KEY-----", the private key and python dump_privatekey is not same, but aws sns need "PRIVATE KEY", is not the "RSA PRIVATE KEY", how can get cert.p12 "PRIVATE KEY" in python?

llrs
  • 3,308
  • 35
  • 68
mackjoner
  • 59
  • 1
  • 8
  • I think this is what you are asking for: possible duplicate of [How to encode an RSA key using PKCS12 in Python?](http://stackoverflow.com/questions/1647568/how-to-encode-an-rsa-key-using-pkcs12-in-python). There's also http://stackoverflow.com/questions/6345786/python-reading-a-pkcs12-certificate-with-pyopenssl-crypto. – jww Feb 14 '14 at 11:41
  • I want to get what don't encrypt private keys in python, the "`OpenSSL.crypto.dump_privatekey()`" function return result is rsa encrypt private key. – mackjoner Feb 15 '14 at 01:33
  • @mackjoner Did you success sending out `push notification` based on `.p12` file? – joe Feb 10 '20 at 17:03

0 Answers0