-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-ca
executable file
·268 lines (214 loc) · 8.14 KB
/
create-ca
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
266
267
268
#!/usr/bin/env python
# Copyright 2017-2019 Fizyr B.V. - https://fizyr.com
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import os
import sys
from pathlib import Path
from argparse import ArgumentParser
from datetime import datetime, timedelta
import subprocess
from cryptography import x509
from typing import List, Optional
from message import msg, msg2, error
import util
import cert_tools
def parseArguments():
parser = ArgumentParser(description='Create new root CA key and certificate.')
parser.add_argument('CA_DIR', help='Root directory for the CA files.')
parser.add_argument('--csr', action='store_true', help='Create a CSR to be signed by the parent CA.')
parser.add_argument('--self-signed', action='store_true', help='Create a self signed certificate, useful for a root CA.')
parser.add_argument('--reuse-key', action='store_true', help='Re-use the existing CA key, useful for reneweing the certificate of a CA.')
parser.add_argument('--import-cert', help='Import an existing certificate as CA cert.')
parser.add_argument('--days', type=int, help='The validity period of the self signed certificate in days, starting today.')
parser.add_argument('--max-path-length', type=int, help='The maximum path of intermediate CA\'s following the newly created one.')
return parser.parse_args()
def create_csr(
root: Path,
dn: x509.Name,
dns_name,
reuse_key: bool,
max_path_length: Optional[int],
now: datetime,
) -> int:
key_link = root / 'ca.key.pem'
csr_file = root / 'ca-csr' / '{}-{:%Y-%m-%d-%H-%M-%S}.csr.pem'.format(dns_name, now)
csr_link = root / 'ca.csr.pem'
files = [csr_file]
links = [csr_link]
public_dirs = [root / 'signed', csr_file.parent]
# Make sure the files don't exist yet.
for file in files:
if file.exists():
error('File already exists: {}', file)
return 1
# Make sure the symlinks are non-existing or indeed symlinks.
for link in links:
if link.exists() and not link.is_symlink():
error('File exists but it not a symlink: {}', link)
return 1
# Create all public directories.
for dir in public_dirs:
os.makedirs(dir, exist_ok=True)
if reuse_key:
# Load existing private key.
key = cert_tools.load_key(key_link)
else:
key_file = root / 'ca-key' / '{}-{:%Y-%m-%d-%H-%M-%S}.key.pem'.format(dns_name, now)
msg('Generating key pair: {}', key_file)
# Create private directories with modified umask.
umask = util.or_umask(0o077)
os.makedirs(key_file.parent, exist_ok=True)
# Generate key with modified umask.
util.or_umask(0o277)
key = cert_tools.generate_rsa_key(key_file)
os.umask(umask)
files.append(key_file)
links.append(key_link)
# Create a CSR for the parent CA to sign.
msg('Making CSR: {}', csr_file)
cert_tools.make_csr(
csr_file,
key = key,
name = dn,
extensions = cert_tools.ca_extensions(dns_name, max_path_length),
)
# Create/update all the symlinks.
for file, link in zip(files, links):
util.force_relative_symlink(file, link);
return 0
def create_self_signed(
root: Path,
dn: x509.Name,
dns_name: str,
reuse_key: bool,
max_path_length: Optional[int],
days: int,
now: datetime,
) -> int:
key_link = root / 'ca.key.pem'
cert_file = root / 'ca-cert' / '{}-{:%Y-%m-%d-%H-%M-%S}.csr.pem'.format(dns_name, now)
cert_link = root / 'ca.cert.pem'
files = [cert_file]
links = [cert_link]
public_dirs = [root / 'signed', cert_file.parent]
private_dirs = [key_file.parent]
# Make sure the files don't exist yet.
for file in files:
if file.exists():
error('File already exists: {}', file)
return 1
# Make sure the symlinks are non-existing or indeed symlinks.
for link in links:
if link.exists() and not link.is_symlink():
error('File exists but it not a symlink: {}', link)
return 1
# Create all public directories.
for dir in public_dirs:
os.makedirs(dir, exist_ok=True)
if reuse_key:
# Load existing private key.
key = cert_tools.load_key(key_link)
else:
key_file = root / 'ca-key' / '{}-{:%Y-%m-%d-%H-%M-%S}.key.pem'.format(dns_name, now)
msg('Generating key pair: {}', key_file)
# Create private directories with modified umask.
umask = util.or_umask(0o077)
os.makedirs(key_file.parent, exist_ok=True)
# Generate key with modified umask.
util.or_umask(0o277)
key = cert_tools.generate_rsa_key(key_file)
os.umask(umask)
files.append(key_file)
links.append(key_link)
# Create and/or bump serial number.
serial = cert_tools.bump_serial(root / 'serial')
# Create self signed certificate.
msg('Making self signed certificate: {}', cert_file)
cert_tools.make_self_signed_cert(
cert_file,
key = key,
name = dn,
serial = serial,
extensions = cert_tools.ca_extensions(dns_name, max_path_length),
days = days,
now = now,
)
# Create/update all the symlinks.
for file, link in zip(files, links):
util.force_relative_symlink(file, link);
return 0
def import_certificate(root: Path, cert_in: Path):
cert_file = root / 'ca-cert' / cert_in.name
cert_link = root / 'ca.cert.pem'
files = [cert_file]
links = [cert_link]
public_dirs = [root / 'signed', cert_file.parent]
# Make sure the files don't exist yet.
for file in files:
if file.exists():
error('File already exists: {}', file)
return 1
# Make sure the symlinks are non-existing or indeed symlinks.
for link in links:
if link.exists() and not link.is_symlink():
error('File exists but it not a symlink: {}', link)
return 1
# Create all public directories.
for dir in public_dirs:
os.makedirs(dir, exist_ok=True)
# Copy the certificate.
util.write_file(cert_file, util.read_file(cert_in))
# Create/update all the symlinks.
for file, link in zip(files, links):
util.force_relative_symlink(file, link);
return 0
def count_bools(*args) -> int:
total = 0
for arg in args:
if arg: total += 1
return total
def main():
now = datetime.now().astimezone()
args = parseArguments()
root = Path(args.CA_DIR)
dn = cert_tools.parse_dn(util.read_file(root / 'dirname').decode('utf8'))
dns = util.read_file(root / 'domain').decode('utf8').strip()
if count_bools(args.csr, args.self_signed, args.import_cert is not None) != 1:
error('You must choose exactly one of --csr, --self-signed and --import-cert')
return 1
if args.csr:
return create_csr(root, dn, dns, args.reuse_key, args.max_path_length, now)
elif args.self_signed:
if args.days is None:
error('The options --days is required when using --self-signed')
return 1
return create_self_signed(root, dn, dns, args.reuse_key, args.max_path_length, args.days, now)
elif args.import_cert is not None:
return import_certificate(root, Path(args.import_cert))
return 0
if __name__ == '__main__':
sys.exit(main())