-
Notifications
You must be signed in to change notification settings - Fork 0
/
certgen.py
265 lines (204 loc) · 7.8 KB
/
certgen.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#
# Corda Certificate Generator
# requires Java Keytool to be installed
# see also requirements.txt for Python dependencies
#
import sys
import re
import argparse
import os
import subprocess
from ruamel.yaml import YAML
yaml = YAML()
from munch import Munch
from colorama import Fore, Back, Style
CERT_ROLES = {
"DOORMAN_CA": "020101",
"NETWORK_MAP": "020102",
"SERVICE_IDENTITY": "020103",
"NODE_CA": "020104",
"TLS": "020105",
"LEGAL_IDENTITY": "020106",
"CONFIDENTIAL_IDENTITY": "020107",
"NETWORK_PARAMETERS": "020108"
}
def cert_role_string(cr):
return f'1.3.6.1.4.1.50530.1.1:non-critical={CERT_ROLES[cr]}'
def execute_verbose(cmd):
print(Fore.CYAN + cmd, flush=True)
print(Style.RESET_ALL)
try:
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)#stdout=subprocess.PIPE)
except:
pass
print(result.stdout.decode('utf-8'))
print(result.stderr.decode('utf-8'))
def execute(cmd):
try:
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)#stdout=subprocess.PIPE)
except:
pass
#print(result.stdout.decode('utf-8'))
#print(result.stderr.decode('utf-8'))
class CertGenerator:
def __init__(self, workdir = 'work'):
self.WORK_DIR = workdir
os.makedirs(workdir, exist_ok=True)
def __mkpath(self, file):
file = os.path.normpath(file)
parts = os.path.split(file)
if len(parts) == 2 and parts[0]:
os.makedirs(parts[0], exist_ok=True)
return file
#
# http://openssl.cs.utah.edu/docs/apps/x509v3_config.html
#def x509_to_openssl(dn):
# change from "CN=Corda, OU=Blah" to "/CN=Corda/OU=Blah"
# return re.sub(r'(^|[,]\s*)([A-Z]+)=', lambda x:'/'+x.group(2)+'=',dn)
#
# Format the certificate extensions required by keytool
#
def __extensions_str(self, cert):
exts = ''
# cert role
if hasattr(cert, 'role'):
exts = exts + f' -ext {cert_role_string(cert.role)}'
# add extensions
for extn in cert.extensions:
extv = cert.extensions[extn]
if extv.startswith('critical'):
extn += ':critical'
extv = extv[9:]
exts = exts + f' -ext "{extn}={extv}"'
# expiration
if hasattr(cert, 'expires'):
exts = exts + f' -validity {cert.expires}'
return exts
#
# load YAML config; return as nested python object
#
def load_config(self, config_file, params):
with open(config_file) as f:
p = re.compile('.*\".*{.*}.*\"')
conf = ""
for line in f:
if p.match(line):
try:
line = line.format(**params)
except KeyError as e:
print(f'{e} must be defined: --param {str(e)[1:-1]}:<value> ')
exit(1)
conf = conf + line
#print(conf)
#exit(0)
y = yaml.load(conf)
m = Munch.fromDict(y)
#print(m)
#exit(0)
return m
def create_cert(self, config, cert, executor):
""" create certificate based on the specified config """
store = config.stores[cert.store]
outfile = self.__mkpath(store.file)
keyfile = self.__mkpath(f'{self.WORK_DIR}/{cert.alias}.key')
csrfile = self.__mkpath(f'{self.WORK_DIR}/{cert.alias}.csr')
crtfile = self.__mkpath(f'{self.WORK_DIR}/{cert.alias}.crt')
rootfile = self.__mkpath(f'{self.WORK_DIR}/root.crt')
# delete the existing keypair
cmd0 = f'keytool -delete -alias {cert.alias} -keystore {outfile} -storepass {store.password}'
executor(cmd0)
# create self-signed keypair ( & cert)
cmd1 = f'keytool -genkeypair -dname "{cert.subject}" -alias {cert.alias} -keyalg EC -keysize 256 -keystore {outfile} -storepass {store.password} -keypass {cert.key.password} -v'
cmd1 = cmd1 + self.__extensions_str(cert)
executor(cmd1)
if cert.issuer:
# find the signer. If it is not explicitly in config
if '.' in cert.issuer:
castore = cert.issuer.split('.')[0]
castore = config.stores[castore]
issuer = cert.issuer.split('.')[1]
else:
castore = store
issuer = cert.issuer
if ':' in issuer:
issuerpass = issuer.split(':')[1]
issuer = issuer.split(':')[0]
else:
issuerpass = issuer.key.password
cmd0 = f'keytool -exportcert -alias cordarootca -keystore {castore.file} -file {rootfile} -storepass {castore.password} -v'
executor(cmd0)
# create CSR for issuer to sign
cmd2 = f'keytool -certreq -alias {cert.alias} -file {csrfile} -keystore {outfile} -storepass {store.password} -keypass {cert.key.password} -v'
executor(cmd2)
# sign the CSR
cmd3 = f'keytool -gencert -alias {issuer} -ext honored=all -infile {csrfile} -outfile {crtfile} -keystore {castore.file} -storepass {castore.password} -keypass {issuerpass} -v'
cmd3 = cmd3 + self.__extensions_str(cert)
executor(cmd3)
# update with the signed copy
cmd4 = f'keytool -importcert -alias {cert.alias} -keystore {outfile} -storepass {store.password} -noprompt -trustcacerts -keypass {cert.key.password} -v'
cat = 'type' if os.name == 'nt' else 'cat'
cmd4 = f'{cat} {crtfile} {rootfile} | {cmd4}'
executor(cmd4)
else:
# self-signed - nothing else to do
pass
print(f' --> {store.file}')
def __apply_store_defaults( store, alias):
try:
store.__getattr__('password')
except AttributeError:
store['password'] = 'password'
pass
def __apply_cert_defaults(cert, alias):
if not hasattr(cert, 'alias'):
cert.alias = alias
if not hasattr(cert, 'issuer'):
cert.issuer = None
if not hasattr(cert, 'password'):
cert.password = 'password'
def generate(self, config, verbose=False, executor=execute):
# preprocess the stores section
for store in config.stores:
store = config.stores[store]
CertGenerator.__apply_store_defaults(store, 0)
# preprocess the certificates section
for alias in config.certificates:
cert = config.certificates[alias]
CertGenerator.__apply_cert_defaults(cert, alias)
# generate the certificates
for alias in config.certificates:
cert = config.certificates[alias]
if verbose:
print(Fore.BLACK + Style.BRIGHT + f'@echo ' + '-'*100)
print(Fore.BLACK + Style.BRIGHT + f'@echo generating: {cert.alias} - "{cert.subject}"')
print(Fore.BLACK + Style.BRIGHT + f'@echo ' + '-'*100)
print(Style.RESET_ALL)
else:
print(f'generating: {cert.alias} - "{cert.subject}"')
self.create_cert(config, cert, executor)
def main(args):
from colorama import init
init(strip=False)
# build diction of parameter substitutions
args.params = [y for x in args.params for y in x]
params = {}
for p in args.params:
nv = p.split(':')
params[nv[0]] = nv[1]
certgen = CertGenerator(args.workdir)
print(f'Parameter substitutions: {params}')
cert_config = certgen.load_config(args.config, params)
if args.execute:
executor = execute
else:
executor = print
certgen.generate(cert_config, True, executor)
if __name__ == "__main__":
parser = argparse.ArgumentParser('Certificate hierarchy generator')
parser.add_argument('--config', help = 'configuration file in YAML format', required=True)
parser.add_argument('--workdir', help = 'working directory', dest='workdir', default='work')
parser.add_argument('--execute', help = 'working directory', dest='execute', default=False, action='store_true')
parser.add_argument('--param', help = 'parameter substitutions. [A:B]', dest='params', nargs='+', action='append', default=[])
#parser.add_argument('--render', help = 'render config and exit', default=False, action='store_true')
args = parser.parse_args()
main(args)