1
1
# Description: Create a Root CA with a client Authentication certificate that's signed by the Root CA.
2
2
# Author: TheScriptGuy
3
- # Last modified: 2023-05-21
4
- # Version: 1.06
3
+ # Last modified: 2023-05-31
4
+ # Version: 1.07
5
5
6
6
from cryptography import x509
7
7
17
17
import argparse
18
18
import random
19
19
20
- scriptVersion = "1.06 "
20
+ scriptVersion = "1.07 "
21
21
22
22
23
23
def certificateMetaData ():
@@ -60,7 +60,8 @@ def certificateMetaData():
60
60
"digest" : "sha512"
61
61
},
62
62
"extensions" : {
63
- "keyUsage" : "digitalSignature, nonRepudiation, keyCertSign" ,
63
+ "keyUsage" : ["digitalSignature" , "nonRepudiation" , "keyCertSign" ],
64
+ "extendedKeyUsage" : ["clientAuth" ]
64
65
}
65
66
}
66
67
@@ -89,8 +90,8 @@ def certificateMetaData():
89
90
"digest" : "sha256"
90
91
},
91
92
"extensions" : {
92
- "keyUsage" : "digitalSignature, nonRepudiation" ,
93
- "extendedKeyUsage" : "clientAuth"
93
+ "keyUsage" : [ "digitalSignature" , " nonRepudiation"] ,
94
+ "extendedKeyUsage" : [ "clientAuth" ]
94
95
}
95
96
}
96
97
@@ -153,20 +154,43 @@ def printWindowsInstallationInstructions(
153
154
print (f"C:\\ >certutil -importpfx -f -Enterprise -p { __p12Password } { __certificateInfo ['ClientAuthentication' ]['clientCertificatePKCS12' ]} NoExport" )
154
155
155
156
156
- def createRootCA (__certificateMetaData : dict ) -> None :
157
- """Create a Root CA with the information from the --companyName argument ."""
157
+ def create_root_private_keys (__certificateMetaData : dict ) -> CryptographySupport . CryptographySupport . PRIVATE_KEY_TYPES :
158
+ """Create a private key ."""
158
159
# First check to see if the --ecc argument was passed. If passed, generate ECC key.
159
160
if args .ecc :
160
- rootCAPrivateKey = ec .generate_private_key (
161
+ __private_key = ec .generate_private_key (
161
162
curve = CryptographySupport .CryptographySupport .generate_curve (__certificateMetaData ["RootCA" ]["ecc" ]["curve" ]),
162
163
backend = default_backend ()
163
164
)
164
165
else :
165
- rootCAPrivateKey = rsa .generate_private_key (
166
+ __private_key = rsa .generate_private_key (
166
167
public_exponent = 65537 ,
167
168
key_size = __certificateMetaData ["RootCA" ]["rsa" ]["rsa_bits" ],
168
169
backend = default_backend ()
169
170
)
171
+ return __private_key
172
+
173
+
174
+ def create_client_private_keys (__certificateMetaData : dict ) -> CryptographySupport .CryptographySupport .PRIVATE_KEY_TYPES :
175
+ """Create a private key."""
176
+ # First check to see if the --ecc argument was passed. If passed, generate ECC key.
177
+ if args .ecc :
178
+ __private_key = ec .generate_private_key (
179
+ curve = CryptographySupport .CryptographySupport .generate_curve (__certificateMetaData ["ClientAuthentication" ]["ecc" ]["curve" ]),
180
+ backend = default_backend ()
181
+ )
182
+ else :
183
+ __private_key = rsa .generate_private_key (
184
+ public_exponent = 65537 ,
185
+ key_size = __certificateMetaData ["ClientAuthentication" ]["rsa" ]["rsa_bits" ],
186
+ backend = default_backend ()
187
+ )
188
+ return __private_key
189
+
190
+
191
+ def createRootCA (__certificateMetaData : dict ) -> None :
192
+ """Create a Root CA with the information from the --companyName argument."""
193
+ rootCAPrivateKey = create_root_private_keys (__certificateMetaData )
170
194
171
195
rootCAPublicKey = rootCAPrivateKey .public_key ()
172
196
rootCACertificateBuilder = x509 .CertificateBuilder ()
@@ -208,9 +232,12 @@ def createRootCA(__certificateMetaData: dict) -> None:
208
232
rootCAKeyUsage , True
209
233
)
210
234
235
+ # Create the ExtendedKeyUsage list
236
+ rootCAExtendedKeyUsage = CryptographySupport .CryptographySupport .build_extended_key_usage (__certificateMetaData ['RootCA' ])
237
+
211
238
# Add extension for only allowing CA to do Client Authentication
212
239
rootCACertificateBuilder = rootCACertificateBuilder .add_extension (
213
- x509 .ExtendedKeyUsage ([ x509 . OID_CLIENT_AUTH ] ), critical = True
240
+ x509 .ExtendedKeyUsage (rootCAExtendedKeyUsage ), critical = True
214
241
)
215
242
216
243
# Apply basic constraints to certificate.
@@ -271,22 +298,12 @@ def create_client_certificate(__certificateMetaData: dict) -> None:
271
298
"""Create the client certificate and sign it from the root CA created from createRootCA()"""
272
299
check_root_ca_files_exist (__certificateMetaData )
273
300
274
- # First check to see if the --ecc argument was passed. If passed, generate ECC key.
275
- if args .ecc :
276
- clientPrivateKey = ec .generate_private_key (
277
- curve = CryptographySupport .CryptographySupport .generate_curve (__certificateMetaData ["ClientAuthentication" ]["ecc" ]["curve" ]),
278
- backend = default_backend ()
279
- )
280
- else :
281
- clientPrivateKey = rsa .generate_private_key (
282
- public_exponent = 65537 ,
283
- key_size = __certificateMetaData ["ClientAuthentication" ]["rsa" ]["rsa_bits" ],
284
- backend = default_backend ()
285
- )
301
+ clientPrivateKey = create_client_private_keys (__certificateMetaData )
286
302
287
303
clientPublicKey = clientPrivateKey .public_key ()
288
304
289
305
clientNameAttributes = CryptographySupport .CryptographySupport .build_name_attribute (__certificateMetaData ['ClientAuthentication' ])
306
+
290
307
clientCertificateBuilder = x509 .CertificateBuilder ()
291
308
clientCertificateBuilder = clientCertificateBuilder .subject_name (x509 .Name (clientNameAttributes ))
292
309
@@ -305,8 +322,11 @@ def create_client_certificate(__certificateMetaData: dict) -> None:
305
322
clientCertificateBuilder = clientCertificateBuilder .add_extension (
306
323
x509 .BasicConstraints (ca = True , path_length = 0 ), critical = True
307
324
)
325
+
326
+ # Add extended key usage extensions to the certificate
327
+ clientCertificateExtendedKeyUsage = CryptographySupport .CryptographySupport .build_extended_key_usage (__certificateMetaData ['ClientAuthentication' ])
308
328
clientCertificateBuilder = clientCertificateBuilder .add_extension (
309
- x509 .ExtendedKeyUsage ([ x509 . OID_CLIENT_AUTH ] ), critical = True
329
+ x509 .ExtendedKeyUsage (clientCertificateExtendedKeyUsage ), critical = True
310
330
)
311
331
312
332
# Load Root CA Key
0 commit comments