Skip to content

Instantly share code, notes, and snippets.

@chris-piekarski
Created May 7, 2014 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-piekarski/732a75262669ea0f22b0 to your computer and use it in GitHub Desktop.
Save chris-piekarski/732a75262669ea0f22b0 to your computer and use it in GitHub Desktop.
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