Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generate Self Signed Cert w/Python
import sys, os
from OpenSSL import crypto, SSL
from socket import gethostname
from pprint import pprint
from time import gmtime, mktime
from os.path import exists, join
CERT_FILE = "apache.crt"
KEY_FILE = "apache.key"
REQ_FILE = "apache.csr"
def create_self_signed_cert(country, state, city, company, organization):
cert_dir="/home/chris/tmp/certs"
key = crypto.PKey()
key.generate_key(crypto.TYPE_RSA, 1024)
req = crypto.X509Req()
req.set_pubkey(key)
req.get_subject().C = "%s" % country
req.get_subject().ST = "%s" % state
req.get_subject().L = "%s" % city
req.get_subject().O = "%s" % company
req.get_subject().OU = "%s" % organization
req.get_subject().CN = gethostname()
req.sign(key,"sha1")
csr_file = join(cert_dir, REQ_FILE)
key_file = join(cert_dir, KEY_FILE)
cert_file = join(cert_dir, CERT_FILE)
open(csr_file, "wt").write(
crypto.dump_certificate_request(crypto.FILETYPE_PEM, req))
open(key_file, "wt").write(
crypto.dump_privatekey(crypto.FILETYPE_PEM, key))
cmd="openssl x509 -req -days 365 -in {} -signkey {} -out {}".format(csr_file, key_file, cert_file)
os.system(cmd)
if __name__ == "__main__":
create_self_signed_cert("US", "CO", "Boulder", "Alco", "Engineering")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment