forked from digicert/smbr-cert-factory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates.py
365 lines (261 loc) · 13 KB
/
templates.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
from typing import NamedTuple
from pyasn1.codec.der.encoder import encode
from pyasn1.type import char, univ
from pyasn1_alt_modules import rfc5280
import key
import tbs_builder
import test_keys
import smime_constants
_RANDOM_LEI = 'AEYE00EKXESVZUUEBP67'
_CRLDP_BASE_URI = 'http://crl.ca.example.com'
_OCSP_BASE_URI = 'http://ocsp.ca.example.com'
_CA_ISSUERS_BASE_URI = 'http://repository.ca.example.com'
def _build_uri(base_uri, path):
return f'{base_uri}/{path}'
class CertificateAndKeyPair(NamedTuple):
certificate: rfc5280.Certificate
key_pair: key.KeyPair
def _build_ca_rdns(common_name):
return tbs_builder.build_rdn_sequence([
(rfc5280.id_at_countryName, char.PrintableString('US')),
(rfc5280.id_at_organizationName, char.UTF8String('Foo Industries Limited')),
(rfc5280.id_at_commonName, char.UTF8String(common_name)),
])
def _build_ee_org_rdns(extra_rdns=None):
if extra_rdns is None:
extra_rdns = []
return tbs_builder.build_rdn_sequence([
(tbs_builder.id_at_organizationIdentifier,
char.PrintableString(f'LEIXG-{_RANDOM_LEI}')),
(rfc5280.id_at_organizationName,
char.PrintableString('Acme Industries, Ltd.')),
] + extra_rdns)
def _build_ee_ja_org_rdns(extra_rdns=None):
if extra_rdns is None:
extra_rdns = []
return tbs_builder.build_rdn_sequence([
(tbs_builder.id_at_organizationIdentifier,
char.PrintableString(f'LEIXG-{_RANDOM_LEI}')),
(rfc5280.id_at_organizationName, char.UTF8String('アクミ工業株式会社')),
] + extra_rdns)
def _build_ee_individual_rdns(extra_rdns=None, legacy_name=False):
if extra_rdns is None:
extra_rdns = []
rdns = []
if not legacy_name:
rdns.extend([
(rfc5280.id_at_surname, char.UTF8String('Yamada')),
(rfc5280.id_at_givenName, char.UTF8String('Hanako')),
])
rdns.append((rfc5280.id_at_commonName, char.UTF8String('YAMADA Hanako')))
rdns.extend(extra_rdns)
return tbs_builder.build_rdn_sequence(rdns)
def _build_ee_ja_individual_rdns(extra_rdns=None, legacy_name=False):
if extra_rdns is None:
extra_rdns = []
rdns = []
if not legacy_name:
rdns.extend([
(rfc5280.id_at_surname, char.UTF8String('山田')),
(rfc5280.id_at_givenName, char.UTF8String('花子')),
])
rdns.append((rfc5280.id_at_commonName, char.UTF8String('山田花子')))
rdns.extend(extra_rdns)
return tbs_builder.build_rdn_sequence(rdns)
def _build_ee_sponsored_rdns(extra_rdns=None):
if extra_rdns is None:
extra_rdns = []
return _build_ee_org_rdns([
(rfc5280.id_at_surname, char.UTF8String('Yamada')),
(rfc5280.id_at_givenName, char.UTF8String('Hanako')),
(rfc5280.id_at_commonName, char.UTF8String('YAMADA Hanako'))
] + extra_rdns)
def _build_ee_ja_sponsored_rdns(extra_rdns=None):
if extra_rdns is None:
extra_rdns = []
return _build_ee_ja_org_rdns([
(rfc5280.id_at_surname, char.UTF8String('山田')),
(rfc5280.id_at_givenName, char.UTF8String('花子')),
(rfc5280.id_at_commonName, char.UTF8String('山田花子'))
] + extra_rdns)
def sign_certificate(tbs_certificate: rfc5280.TBSCertificate, key_pair: key.KeyPair):
tbs_cert_der = encode(tbs_certificate)
cert = rfc5280.Certificate()
cert['tbsCertificate'] = tbs_certificate
cert['signatureAlgorithm'] = key_pair.public_key.signature_algorithm
cert['signature'] = univ.BitString(hexValue=key_pair.private_key.sign(tbs_cert_der).hex())
return cert
def sign_crl(tbs_certlist: rfc5280.TBSCertList, key_pair: key.KeyPair):
tbs_certlist_der = encode(tbs_certlist)
crl = rfc5280.CertificateList()
crl['tbsCertList'] = tbs_certlist
crl['signatureAlgorithm'] = key_pair.public_key.signature_algorithm
crl['signature'] = univ.BitString(hexValue=key_pair.private_key.sign(tbs_certlist_der).hex())
return crl
def create_root():
rsa_key_pair = test_keys.GUTMANN_TESTKEY_P256
rdns = _build_ca_rdns('Root CA')
tbs_cert = tbs_builder.build_root(rdns, rsa_key_pair.public_key)
cert = sign_certificate(tbs_cert, rsa_key_pair)
tbs_crl = tbs_builder.build_crl(rdns, rsa_key_pair.public_key)
crl = sign_crl(tbs_crl, rsa_key_pair)
return CertificateAndKeyPair(cert, rsa_key_pair), crl
def create_ica(root_ca: CertificateAndKeyPair):
rsa_key_pair = test_keys.GUTMANN_TESTKEY_RSA4096
rdns = _build_ca_rdns('Intermediate CA')
extensions = [tbs_builder.build_certificate_policies([rfc5280.anyPolicy]),
tbs_builder.build_crldp([_build_uri(_CRLDP_BASE_URI, 'root_ca_crl.crl')]),
tbs_builder.build_aia([], [_build_uri(_CA_ISSUERS_BASE_URI, 'root_ca.der')]),
tbs_builder.build_eku([rfc5280.id_kp_emailProtection, rfc5280.id_kp_clientAuth])]
tbs_cert = tbs_builder.build_ica(rdns, rsa_key_pair.public_key,
root_ca.certificate['tbsCertificate']['subject']['rdnSequence'],
root_ca.key_pair.public_key,
extensions)
cert = sign_certificate(tbs_cert, root_ca.key_pair)
tbs_crl = tbs_builder.build_crl(rdns, rsa_key_pair.public_key)
crl = sign_crl(tbs_crl, rsa_key_pair)
return CertificateAndKeyPair(cert, rsa_key_pair), crl
def _create_ee(ica: CertificateAndKeyPair, validation_level: smime_constants.ValidationLevel,
generation: smime_constants.Generation, subject_rdns, sans,
additional_extensions=None, additional_ekus=None, additional_dirname_rdns=None):
if additional_extensions is None:
additional_extensions = []
if additional_ekus is None:
additional_ekus = []
rsa_key_pair = test_keys.GUTMANN_TESTKEY_RSA2048
extensions = [
tbs_builder.build_certificate_policies(
[smime_constants.get_policy_oid(validation_level, generation)]),
tbs_builder.build_crldp([_build_uri(_CRLDP_BASE_URI, 'issuing_ca_crl.crl')]),
tbs_builder.build_aia([], [_build_uri(_CA_ISSUERS_BASE_URI, 'issuing_ca.der')]),
tbs_builder.build_eku([rfc5280.id_kp_emailProtection] + additional_ekus),
tbs_builder.build_san(sans, add_upn=(rfc5280.id_kp_clientAuth in additional_ekus),
dir_name_rdns=additional_dirname_rdns)
] + additional_extensions
tbs_cert = tbs_builder.build_ee(subject_rdns, rsa_key_pair.public_key,
ica.certificate['tbsCertificate']['subject']['rdnSequence'],
ica.key_pair.public_key,
extensions
)
cert = sign_certificate(tbs_cert, ica.key_pair)
return CertificateAndKeyPair(cert, rsa_key_pair)
def create_ee_mailbox_strict(ica: CertificateAndKeyPair):
email_address = '[email protected]'
email_addresses = [
email_address,
'山田花子@example.com',
]
rdns = tbs_builder.build_rdn_sequence([
(rfc5280.id_at_commonName, char.UTF8String(email_address)),
(rfc5280.id_emailAddress, char.IA5String(email_address))
])
return _create_ee(ica, smime_constants.ValidationLevel.MAILBOX, smime_constants.Generation.STRICT,
rdns, email_addresses)
def create_ee_mailbox_multipurpose(ica: CertificateAndKeyPair):
email_address = '[email protected]'
email_addresses = [
email_address,
'山田花子@example.com',
]
rdns = tbs_builder.build_rdn_sequence([
(rfc5280.id_at_commonName, char.UTF8String(email_address)),
(rfc5280.id_emailAddress, char.IA5String(email_address))
])
return _create_ee(ica, smime_constants.ValidationLevel.MAILBOX, smime_constants.Generation.MULTIPURPOSE,
rdns, email_addresses, additional_ekus=[rfc5280.id_kp_clientAuth])
def create_ee_org_strict(ica: CertificateAndKeyPair):
email_address = '[email protected]'
email_addresses = [
email_address,
'山田花子@example.com',
]
rdns = _build_ee_org_rdns(
[(rfc5280.id_emailAddress, char.IA5String(email_address))]
)
ja_rdns = _build_ee_ja_org_rdns()
lei_ext = tbs_builder.build_lei(_RANDOM_LEI)
return _create_ee(ica, smime_constants.ValidationLevel.ORGANIZATION, smime_constants.Generation.STRICT,
rdns, email_addresses, additional_extensions=[lei_ext], additional_dirname_rdns=ja_rdns)
def create_ee_org_multipurpose(ica: CertificateAndKeyPair):
email_address = '[email protected]'
email_addresses = [
email_address,
'山田花子@example.com',
]
rdns = _build_ee_org_rdns(
[(rfc5280.id_emailAddress, char.IA5String(email_address))]
)
ja_rdns = _build_ee_ja_org_rdns()
lei_ext = tbs_builder.build_lei(_RANDOM_LEI)
return _create_ee(ica, smime_constants.ValidationLevel.ORGANIZATION, smime_constants.Generation.MULTIPURPOSE,
rdns, email_addresses, additional_extensions=[lei_ext],
additional_ekus=[rfc5280.id_kp_clientAuth], additional_dirname_rdns=ja_rdns)
def create_ee_sponsored_strict(ica: CertificateAndKeyPair):
email_address = '[email protected]'
email_addresses = [
email_address,
'山田花子@example.com',
]
rdns = _build_ee_sponsored_rdns(
[(rfc5280.id_emailAddress, char.IA5String(email_address))]
)
ja_rdns = _build_ee_ja_sponsored_rdns()
lei_ext = tbs_builder.build_lei(_RANDOM_LEI)
lei_role_ext = tbs_builder.build_lei_role('CEO')
return _create_ee(ica, smime_constants.ValidationLevel.SPONSORED, smime_constants.Generation.STRICT,
rdns, email_addresses, additional_extensions=[lei_ext, lei_role_ext],
additional_dirname_rdns=ja_rdns)
def create_ee_sponsored_multipurpose(ica: CertificateAndKeyPair):
email_address = '[email protected]'
email_addresses = [
email_address,
'山田花子@example.com',
]
rdns = _build_ee_sponsored_rdns(
[(rfc5280.id_emailAddress, char.IA5String(email_address))]
)
ja_rdns = _build_ee_ja_sponsored_rdns()
lei_ext = tbs_builder.build_lei(_RANDOM_LEI)
lei_role_ext = tbs_builder.build_lei_role('CEO')
return _create_ee(ica, smime_constants.ValidationLevel.SPONSORED, smime_constants.Generation.MULTIPURPOSE,
rdns, email_addresses, additional_extensions=[lei_ext, lei_role_ext],
additional_ekus=[rfc5280.id_kp_clientAuth],
additional_dirname_rdns=ja_rdns)
def create_ee_individual_strict(ica: CertificateAndKeyPair):
email_address = '[email protected]'
email_addresses = [
email_address,
'山田花子@example.com',
]
rdns = _build_ee_individual_rdns(
[(rfc5280.id_emailAddress, char.IA5String(email_address))]
)
ja_rdns = _build_ee_ja_individual_rdns()
return _create_ee(ica, smime_constants.ValidationLevel.INDIVIDUAL, smime_constants.Generation.STRICT,
rdns, email_addresses, additional_dirname_rdns=ja_rdns)
def create_ee_individual_multipurpose(ica: CertificateAndKeyPair):
email_address = '[email protected]'
email_addresses = [
email_address,
'山田花子@example.com',
]
rdns = _build_ee_individual_rdns(
[(rfc5280.id_emailAddress, char.IA5String(email_address))]
)
ja_rdns = _build_ee_ja_individual_rdns()
return _create_ee(ica, smime_constants.ValidationLevel.INDIVIDUAL, smime_constants.Generation.MULTIPURPOSE,
rdns, email_addresses, additional_ekus=[rfc5280.id_kp_clientAuth],
additional_dirname_rdns=ja_rdns)
def create_ee_individual_legacy(ica: CertificateAndKeyPair):
email_address = '[email protected]'
email_addresses = [
email_address,
'山田花子@example.com',
]
rdns = _build_ee_individual_rdns(
[(rfc5280.id_emailAddress, char.IA5String(email_address))], legacy_name=True
)
ja_rdns = _build_ee_ja_individual_rdns(legacy_name=True)
return _create_ee(ica, smime_constants.ValidationLevel.INDIVIDUAL, smime_constants.Generation.LEGACY,
rdns, email_addresses, additional_ekus=[rfc5280.id_kp_clientAuth],
additional_dirname_rdns=ja_rdns)