| 1 |
#!/usr/bin/env python3 |
| 2 |
# ========================================================================== |
| 3 |
# ____ _ _____ _ |
| 4 |
# / ___| _ _ ___| |_ ___ _ __ ___ |_ _|__ ___ | |___ |
| 5 |
# \___ \| | | / __| __/ _ \ '_ ` _ \ _____| |/ _ \ / _ \| / __| |
| 6 |
# ___) | |_| \__ \ || __/ | | | | |_____| | (_) | (_) | \__ \ |
| 7 |
# |____/ \__, |___/\__\___|_| |_| |_| |_|\___/ \___/|_|___/ |
| 8 |
# |___/ |
| 9 |
# --- System-Tools --- |
| 10 |
# https://www.nntb.no/~dreibh/system-tools/ |
| 11 |
# ========================================================================== |
| 12 |
# |
| 13 |
# X.509 CA and Certificate Test Setup Script |
| 14 |
# Copyright (C) 2015-2026 by Thomas Dreibholz |
| 15 |
# |
| 16 |
# This program is free software: you can redistribute it and/or modify |
| 17 |
# it under the terms of the GNU General Public License as published by |
| 18 |
# the Free Software Foundation, either version 3 of the License, or |
| 19 |
# (at your option) any later version. |
| 20 |
# |
| 21 |
# This program is distributed in the hope that it will be useful, |
| 22 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 |
# GNU General Public License for more details. |
| 25 |
# |
| 26 |
# You should have received a copy of the GNU General Public License |
| 27 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 28 |
# |
| 29 |
# Contact: [email protected] |
| 30 |
|
| 31 |
import argparse |
| 32 |
import sys |
| 33 |
|
| 34 |
if sys.version_info < (3, 10): |
| 35 |
sys.stderr.write('ERROR: ' + sys.argv[0] + ' requires Python 3.10 or later!\n') |
| 36 |
sys.exit(1) |
| 37 |
|
| 38 |
from typing import Any, Final |
| 39 |
from CertificateHelper import * |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
# ###### Main program ####################################################### |
| 44 |
|
| 45 |
# ====== Handle command-line parameters ===================================== |
| 46 |
subjectAltName : str | None = None |
| 47 |
certType : CertificateType = CertificateType.Server |
| 48 |
revokeIfExisting : bool = True |
| 49 |
revoke : bool = False |
| 50 |
|
| 51 |
# ====== Handle arguments =================================================== |
| 52 |
parser : Final[argparse.ArgumentParser] = argparse.ArgumentParser( |
| 53 |
description = 'X.509 CA and Certificate Test Setup Script', |
| 54 |
formatter_class = argparse.ArgumentDefaultsHelpFormatter |
| 55 |
) |
| 56 |
|
| 57 |
# Positional arguments: |
| 58 |
parser.add_argument('ssl_directory', help = 'The main directory for SSL certificates') |
| 59 |
parser.add_argument('names', nargs = '+', help='One or more names for the certificates') |
| 60 |
|
| 61 |
# Certificate Type (mutually exclusive): |
| 62 |
type_group = parser.add_mutually_exclusive_group() |
| 63 |
type_group.add_argument('-s', '--server', action = 'store_const', dest = 'cert_type', |
| 64 |
const=CertificateType.Server, help = 'Create a server certificate') |
| 65 |
type_group.add_argument('-c', '--client', action = 'store_const', dest = 'cert_type', |
| 66 |
const=CertificateType.Client, help = 'Create a client certificate') |
| 67 |
type_group.add_argument('-u', '--user', action = 'store_const', dest = 'cert_type', |
| 68 |
const=CertificateType.User, help = 'Create a user certificate') |
| 69 |
parser.set_defaults(cert_type= CertificateType.Server) |
| 70 |
|
| 71 |
# Existing Certificate Behavior (mutually exclusive): |
| 72 |
existing_group = parser.add_mutually_exclusive_group() |
| 73 |
existing_group.add_argument('-x', '--revoke-if-existing', action = 'store_true', default = True, |
| 74 |
help = 'Revoke the certificate if it already exists') |
| 75 |
existing_group.add_argument('-i', '--ignore-if-existing', action = 'store_false', |
| 76 |
dest = 'revoke_if_existing', |
| 77 |
help = 'Ignore and do not revoke if the certificate exists') |
| 78 |
|
| 79 |
# Other Options: |
| 80 |
parser.add_argument('-r', '--revoke', action = 'store_true', |
| 81 |
help = 'Revoke the generated certificate(s) immediately') |
| 82 |
parser.add_argument('-S', '--san', '--subjectAltName', dest = 'subjectAltName', default=None, |
| 83 |
help = 'Subject Alternative Name (subjectAltName|LOCAL|LOOKUP)') |
| 84 |
|
| 85 |
parser.add_argument('-A', '--algorithm', type = str, default = 'RSA', |
| 86 |
choices = [ 'RSA', 'EC', 'ED25519', 'ED448' ], |
| 87 |
help = 'The key algorithm to use') |
| 88 |
parser.add_argument('-C', '--curve', type=str, default=DefaultECCurve, |
| 89 |
help = 'The elliptic curve to use (only applicable if algorithm is EC)') |
| 90 |
parser.add_argument('-K', '--keylen-ca', type = int, default = DefaultCAKeyLength, |
| 91 |
help = 'Key length for the CA (only applicable if algorithm is RSA)') |
| 92 |
parser.add_argument('-k', '--keylen-cert', type = int, default = DefaultCertKeyLength, |
| 93 |
help = 'Key length for the certificates (only applicable if algorithm is RSA)') |
| 94 |
|
| 95 |
args = parser.parse_args() |
| 96 |
|
| 97 |
keyAlgorithm : Final[KeyAlgorithm] = KeyAlgorithm[args.algorithm] |
| 98 |
caKeyLength : Final[int] = args.keylen_ca |
| 99 |
certKeyLength : Final[int] = args.keylen_cert |
| 100 |
ecCurve : Final[str] = args.curve |
| 101 |
mainDirectory : Final[str] = args.ssl_directory |
| 102 |
certificatesToCreate : list[dict[str, Any]] = [ ] |
| 103 |
name : str |
| 104 |
for name in args.names: |
| 105 |
parsedName : str |
| 106 |
san : str |
| 107 |
( parsedName, san ) = prepareSubjectAltName(args.cert_type, name, args.subjectAltName) |
| 108 |
certificatesToCreate.append({ 'name' : parsedName, |
| 109 |
'type' : args.cert_type, |
| 110 |
'subjectAltName' : san, |
| 111 |
'revokeIfExisting' : args.revoke_if_existing }) |
| 112 |
# print(certificatesToCreate) |
| 113 |
|
| 114 |
|
| 115 |
# ====== Create CA hierarchy, if not existing =============================== |
| 116 |
|
| 117 |
# =========================================================================== |
| 118 |
# Hierarchy to be created: |
| 119 |
# TestLevel1 |
| 120 |
# - TestLevel2 |
| 121 |
# - TestIntermediate1 |
| 122 |
# - TestIntermediate2 |
| 123 |
# - TestLeaf |
| 124 |
# - Servers ... |
| 125 |
# - Clients ... |
| 126 |
# - Users ... |
| 127 |
# =========================================================================== |
| 128 |
|
| 129 |
|
| 130 |
# ====== Create Test CAs ==================================================== |
| 131 |
globalCRLFileName : Final[str] = "TestGlobal.crl" |
| 132 |
|
| 133 |
# Create Test Root Level-1 CA certificate (self-signed): |
| 134 |
TestLevel1 : Final[CA] = \ |
| 135 |
CA(mainDirectory, 'TestLevel1', |
| 136 |
parentCA = None, |
| 137 |
subject = '/C=NO/ST=Oslo/L=Oslo/O=Simula Research Laboratory/streetAddress=Kristian Augusts gate 23/postalCode=0164/CN=Test Level-1 CA Certificate', |
| 138 |
certType = CertificateType.RootCA, |
| 139 |
keyAlgorithm = keyAlgorithm, keyLength = caKeyLength, ecCurve = ecCurve, |
| 140 |
globalCRLFileName = globalCRLFileName) |
| 141 |
|
| 142 |
# Create Test Root Level-2 CA certificate (signed by Test Root Level-1): |
| 143 |
TestLevel2 : Final[CA] = \ |
| 144 |
CA(mainDirectory, 'TestLevel2', |
| 145 |
parentCA = TestLevel1, |
| 146 |
subject = '/C=NO/ST=Oslo/L=Oslo/O=Simula Research Laboratory/streetAddress=Kristian Augusts gate 23/postalCode=0164/CN=Test Level-2 CA Certificate', |
| 147 |
certType = CertificateType.IntermediateCA, |
| 148 |
keyAlgorithm = keyAlgorithm, keyLength = caKeyLength, ecCurve = ecCurve, |
| 149 |
globalCRLFileName = globalCRLFileName) |
| 150 |
|
| 151 |
# Create Test Intermediate CA certificate (signed by Test Root Level-2): |
| 152 |
TestIntermediate1 : Final[CA] = \ |
| 153 |
CA(mainDirectory, 'TestIntermediate1', |
| 154 |
parentCA = TestLevel2, |
| 155 |
subject = '/C=NO/ST=Oslo/L=Oslo/O=Simula Metropolitan Centre for Digital Engineering (SimulaMet)/streetAddress=Stensberggata 27/postalCode=0170/CN=*', |
| 156 |
certType = CertificateType.IntermediateCA, |
| 157 |
keyAlgorithm = keyAlgorithm, keyLength = caKeyLength, ecCurve = ecCurve, |
| 158 |
globalCRLFileName = globalCRLFileName) |
| 159 |
|
| 160 |
# Create Test Intermediate CA certificate (signed by Test Root Level-2): |
| 161 |
TestIntermediate2 : Final[CA] = \ |
| 162 |
CA(mainDirectory, 'TestIntermediate2', |
| 163 |
parentCA = TestIntermediate1, |
| 164 |
subject = '/C=NO/ST=Oslo/L=Oslo/O=Simula Metropolitan Centre for Digital Engineering (SimulaMet)/OU=Centre for Resilient Networks and Applications (CRNA)/streetAddress=Stensberggata 27/postalCode=0170/CN=*', |
| 165 |
certType = CertificateType.IntermediateCA, |
| 166 |
keyAlgorithm = keyAlgorithm, keyLength = caKeyLength, ecCurve = ecCurve, |
| 167 |
globalCRLFileName = globalCRLFileName) |
| 168 |
|
| 169 |
# Create Test Leaf CA certificate (signed by Test Intermediate): |
| 170 |
TestLeaf : Final[CA] = \ |
| 171 |
CA(mainDirectory, 'TestLeaf', |
| 172 |
parentCA = TestIntermediate2, |
| 173 |
subject = '/C=NO/ST=Oslo/L=Oslo/O=Simula Metropolitan Centre for Digital Engineering (SimulaMet)/OU=SimulaMet Interoperability Lab (SMIL)/streetAddress=Stensberggata 27/postalCode=0170/CN=*/', |
| 174 |
certType = CertificateType.LeafCA, |
| 175 |
keyAlgorithm = keyAlgorithm, keyLength = caKeyLength, ecCurve = ecCurve, |
| 176 |
globalCRLFileName = globalCRLFileName) |
| 177 |
|
| 178 |
|
| 179 |
# ====== Create Test Servers ================================================ |
| 180 |
defaultSubjectWithoutCN = '/C=NO/ST=Oslo/L=Oslo/O=Simula Metropolitan Centre for Digital Engineering (SimulaMet)/OU=SimulaMet Interoperability Lab (SMIL)/streetAddress=Stensberggata 27/postalCode=0170' |
| 181 |
|
| 182 |
certificates : dict[str, Certificate] = {} |
| 183 |
certificateToCreate : dict[str, Any] |
| 184 |
for certificateToCreate in certificatesToCreate: |
| 185 |
# print(certificateToCreate) |
| 186 |
|
| 187 |
certificateName = certificateToCreate['name'] |
| 188 |
subjectWithoutCN : str |
| 189 |
if certificateToCreate['type'] != CertificateType.User: |
| 190 |
subjectWithoutCN = defaultSubjectWithoutCN |
| 191 |
else: |
| 192 |
subjectWithoutCN = defaultSubjectWithoutCN + prepareUserSubject(certificateName) |
| 193 |
certificates[certificateName] = \ |
| 194 |
Certificate(mainDirectory, certificateName, TestLeaf, |
| 195 |
subjectWithoutCN, certificateToCreate['subjectAltName'], |
| 196 |
certificateToCreate['type'], |
| 197 |
keyAlgorithm = keyAlgorithm, keyLength = certKeyLength, ecCurve = ecCurve, |
| 198 |
revokeIfExisting = revokeIfExisting) |
| 199 |
|
| 200 |
if revoke: |
| 201 |
certificates[certificateName].revoke() |
| 202 |
|
| 203 |
|
| 204 |
# ====== Finally, force regeneration of the CRL to ensure it is up-to-date == |
| 205 |
TestLevel1.generateCRL() |