Create self signed root CA certificate with the help of python using openssl

You need to install openssl package for python. Use following command to install openssl for python:

pip install pyopenssl
#Following script will create a self signed root ca cert.
from OpenSSL import crypto, SSL
from os.path import join
import random

CN = input("Enter the common name of the certificate you want: ")
pubkey = "%s.crt" % CN #replace %s with CN
privkey = "%s.key" % CN # replcate %s with CN

pubkey = join(".", pubkey)
privkey = join(".", privkey)

k = crypto.PKey()
k.generate_key(crypto.TYPE_RSA, 2048)

# create a self-signed cert
cert = crypto.X509()
cert.get_subject().C = input("Country: ")
cert.get_subject().ST = input("State: ")
cert.get_subject().L = input("City: ")
cert.get_subject().O = input("Organization: ")
cert.get_subject().OU = input("Organizational Unit: ")
cert.get_subject().CN = CN
cert.gmtime_adj_notAfter(31536000)#315360000 is in seconds.
cert.sign(k, 'sha512')
pub=crypto.dump_certificate(crypto.FILETYPE_PEM, cert)
priv=crypto.dump_privatekey(crypto.FILETYPE_PEM, k)
open(privkey, "wt").write(priv.decode("utf-8") )
This entry was posted in Python and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s