-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCertificateMetaData.py
77 lines (58 loc) · 3.35 KB
/
CertificateMetaData.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
import json
from datetime import datetime, timedelta
import os
class CertificateMetaData:
"""A class to manage and generate certificate metadata based on supplied information from JSON files."""
CLASS_VERSION = "0.01"
AUTHOR = "TheScriptGuy"
LAST_MODIFIED = "2024-01-03"
def __init__(self, **kwargs):
"""Initialize the class and attempt to load certificate information."""
self.company_name = kwargs.get('companyName', 'ACME Corp')
self.certificate_info = {}
try:
self.load_root_ca()
self.load_client_authentication()
self.add_not_before_and_after(self.certificate_info["RootCA"])
self.add_not_before_and_after(self.certificate_info["ClientAuthentication"])
except (json.JSONDecodeError, FileNotFoundError) as e:
print(f"Error loading JSON files: {e}")
exit(1)
def normalize_name(self, name: str) -> str:
"""
Normalize the company name to only have numbers, letters, and spaces.
Args:
name (str): The company name.
Returns:
str: A normalized version of the company name.
"""
return ''.join(filter(lambda x: x.isalpha() or x.isspace() or x.isdigit(), name))
def add_not_before_and_after(self, certificate_dict: dict) -> None:
"""
Add notBefore and notAfter fields to the certificate information.
Args:
certificate_dict (dict): The dictionary containing the certificate information.
"""
certificate_dict["notBefore"] = datetime.today()
certificate_dict["notAfter"] = datetime.today() + timedelta(seconds=31536000)
def load_root_ca(self) -> None:
"""Load the Root Certificate Authority information from a JSON file."""
with open('RootCertificateAuthority.json', 'r') as file:
self.certificate_info["RootCA"] = json.load(file)
rootCAFileName = "root-ca-" + self.normalize_name(self.company_name).replace(' ', '-').lower()
self.certificate_info["RootCA"]["rootCAFileName"] = rootCAFileName
self.certificate_info["RootCA"]["rootCAPublicKey"] = f"{rootCAFileName}.crt"
self.certificate_info["RootCA"]["rootCAPrivateKey"] = f"{rootCAFileName}.key"
self.certificate_info["RootCA"]["rootCAPKCS12"] = f"{rootCAFileName}.p12"
if self.certificate_info["RootCA"]["oid"]["CN"] is None:
self.certificate_info["RootCA"]["oid"]["CN"] = f"{self.company_name} + Root CA"
if self.certificate_info["RootCA"]["oid"]["companyName"] is None:
self.certificate_info["RootCA"]["oid"]["companyName"] = f"{self.company_name}"
def load_client_authentication(self) -> None:
"""Load the Client Authentication certificate information from a JSON file."""
with open('ClientCertificate.json', 'r') as file:
self.certificate_info["ClientAuthentication"] = json.load(file)
client_cert_file_name = "client-cert-" + self.normalize_name(self.company_name).replace(' ', '-').lower()
self.certificate_info["ClientAuthentication"]["clientCertificatePublicKey"] = f"{client_cert_file_name}.crt"
self.certificate_info["ClientAuthentication"]["clientCertificatePrivateKey"] = f"{client_cert_file_name}.key"
self.certificate_info["ClientAuthentication"]["clientCertificatePKCS12"] = f"{client_cert_file_name}.p12"