cert config
- Changed SAN default for generating server certificate - Added two new options for organizations
This commit is contained in:
@@ -24,19 +24,35 @@ CA_VALIDITY_YEARS = 20
|
||||
|
||||
|
||||
class CertificateAuthority:
|
||||
def __init__(self):
|
||||
def __init__(self, organization: str = "", organizational_unit: str = ""):
|
||||
# Not hardcoded: these come from add-on options (default empty,
|
||||
# meaning "omit entirely" — the subject stays CN-only, same as
|
||||
# before this existed). If set, they're applied consistently to
|
||||
# every cert this CA issues: the root CA itself, the broker cert,
|
||||
# and every device client cert.
|
||||
self.organization = organization
|
||||
self.organizational_unit = organizational_unit
|
||||
os.makedirs(CA_DIR, exist_ok=True)
|
||||
if not (os.path.exists(CA_KEY_PATH) and os.path.exists(CA_CERT_PATH)):
|
||||
self._generate_root_ca()
|
||||
self._load_root_ca()
|
||||
|
||||
def _build_name(self, common_name: str) -> x509.Name:
|
||||
attrs = []
|
||||
if self.organization:
|
||||
attrs.append(x509.NameAttribute(NameOID.ORGANIZATION_NAME, self.organization))
|
||||
if self.organizational_unit:
|
||||
attrs.append(
|
||||
x509.NameAttribute(NameOID.ORGANIZATIONAL_UNIT_NAME, self.organizational_unit)
|
||||
)
|
||||
attrs.append(x509.NameAttribute(NameOID.COMMON_NAME, common_name))
|
||||
return x509.Name(attrs)
|
||||
|
||||
# -- setup ---------------------------------------------------------
|
||||
|
||||
def _generate_root_ca(self):
|
||||
key = ec.generate_private_key(ec.SECP256R1())
|
||||
subject = issuer = x509.Name(
|
||||
[x509.NameAttribute(NameOID.COMMON_NAME, CA_COMMON_NAME)]
|
||||
)
|
||||
subject = issuer = self._build_name(CA_COMMON_NAME)
|
||||
now = datetime.datetime.now(datetime.timezone.utc)
|
||||
cert = (
|
||||
x509.CertificateBuilder()
|
||||
@@ -109,7 +125,7 @@ class CertificateAuthority:
|
||||
raise ValueError("CSR signature is invalid")
|
||||
|
||||
now = datetime.datetime.now(datetime.timezone.utc)
|
||||
subject = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, device_id)])
|
||||
subject = self._build_name(device_id)
|
||||
|
||||
builder = (
|
||||
x509.CertificateBuilder()
|
||||
@@ -159,7 +175,7 @@ class CertificateAuthority:
|
||||
|
||||
key = ec.generate_private_key(ec.SECP256R1())
|
||||
now = datetime.datetime.now(datetime.timezone.utc)
|
||||
subject = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, hostnames[0])])
|
||||
subject = self._build_name(hostnames[0])
|
||||
|
||||
san_entries = []
|
||||
for name in hostnames:
|
||||
@@ -261,4 +277,4 @@ class CertificateAuthority:
|
||||
return False
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user