-
Notifications
You must be signed in to change notification settings - Fork 0
/
openssl.py
96 lines (86 loc) · 2.93 KB
/
openssl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
__author__ = 'Martijn Braam <[email protected]>'
from subprocess import call
from opensslconfig import OpenSSLConfig
import os
from collections import OrderedDict
def generate_config(fp):
config = OpenSSLConfig()
config["__global__"] = {"distinguished_name": "req_distinguished_name"}
config["req_distinguished_name"] = {}
config["v3_req"] = {}
config["v3_ca"] = OrderedDict([
("subjectKeyIdentifier", "hash"),
("authorityKeyIdentifier", "keyid:always,issuer"),
("basicConstraints", "CA:true"),
("keyUsage", "cRLSign, keyCertSign")
])
config["usr_cert"] = {
"basicConstraints": "CA:false",
"keyUsage": "nonRepudiation, digitalSignature, keyEncipherment",
"nsComment": "OpenSSL Generated Certificate",
"subjectKeyIdentifier": "hash",
"authorityKeyIdentifier": "keyid,issuer"
}
config["ca"] = {
"default_ca": "CA_default"
}
config["CA_default"] = OrderedDict([
("dir", os.getcwd()),
("certs", "$dir/certs"),
("crl_dir", "$dir/crl"),
("database", "$dir/index.txt"),
("new_certs_dir", "$dir/newcerts"),
("certificate", "$dir/certs/ca.cert.pem"),
("private_key", "$dir/private/ca.key.pem"),
("serial", "$dir/serial"),
("RANDFILE", "$dir/private/.rand"),
("x509_extensions", "usr_cert"),
("name_opt", "ca_default"),
("cert_opt", "ca_default"),
("default_days", "3650"),
("default_md", "rsa256"),
("policy", "policy_match")
])
config["policy_match"] = {
"countryName": "supplied",
"stateOrProvinceName": "supplied",
"organizationName": "supplied",
"organizationalUnitName": "optional",
"commonName": "supplied",
"emailAddress": "optional"
}
config["req"] = {
"default_bits": "4096",
"distinguished_name": "req_distinguished_name",
"attributes": "req_attributes",
"x509_extensions": "v3_ca",
"req_extensions": "v3_req",
"string_mask": "utf8only"
}
config.write(fp)
def generate_private_key(path, size=4096, encrypt=True):
if encrypt:
return call(["openssl", "genrsa", "-aes256", "-out", path, str(size)])
else:
return call(["openssl", "genrsa", "-out", path, str(size)])
def generate_root_cert(path, keyfile, cert):
return call(
["openssl", "req", "-new", "-x509",
"-key", keyfile,
"-extensions", "v3_ca",
"-subj", cert.get_dn(),
"-out", path])
def generate_csr(path, keyfile, cert):
return call(
["openssl", "req", "-new",
"-key", keyfile,
"-subj", cert.get_dn(),
"-out", path])
def sign_csr(csrfile, outputfile):
return call(
["openssl", "ca", "-notext",
"-extensions", "usr_cert",
"-md", "sha256",
"-config", "openssl.cnf",
"-in", csrfile,
"-out", outputfile])