Decouple certificate CN from device_id
build-and-push / check-branch (push) Successful in 11s
build-and-push / build (amd64, linux/amd64) (push) Successful in 10m57s

- ca.sign_csr's identity parameter renamed to `common_name`, fully
  decoupled from device_id.
- New resolve_common_name(): CN comes from the device's own CSR: falls
  back to device_id only if the CSR has no CN (malformed request).
- Renewal unaffected: a renewed cert always keeps the CN of the
  device's current, still-valid cert, regardless of what the renewal
  CSR claims (verified against an adversarial CSR asserting a
  different CN).
- device_id remains the sole storage key (pending/issued/revoked) and
  MQTT topic routing key, and must still be unique per physical
  device even when CN is deliberately shared across many.
- /api/pending and /api/issued now also expose the CSR-resolved CN for
  display, without changing what's actually stored.

No topic structure changes. No new MQTT payload fields.
This commit is contained in:
2026-08-27 16:34:42 +03:30
parent e23eb7498b
commit 9472cfff8d
9 changed files with 157 additions and 35 deletions
+32 -3
View File
@@ -23,6 +23,30 @@ CA_COMMON_NAME = "Home Assistant Local MQTT CA"
CA_VALIDITY_YEARS = 20
def extract_csr_common_name(csr_pem: str):
"""Return the CN the CSR itself requests, or None if it has none /
doesn't parse. Lets a device declare its own identity independent of
whatever MQTT topic segment the request arrived on."""
try:
csr = x509.load_pem_x509_csr(csr_pem.encode())
attrs = csr.subject.get_attributes_for_oid(NameOID.COMMON_NAME)
return attrs[0].value if attrs else None
except ValueError:
return None
def extract_cert_common_name(cert_pem: str):
"""Return the CN of an already-issued cert, or None if it has none /
doesn't parse. Used so renewal can keep a device's identity stable
across reissuance regardless of what the new request claims."""
try:
cert = x509.load_pem_x509_certificate(cert_pem.encode())
attrs = cert.subject.get_attributes_for_oid(NameOID.COMMON_NAME)
return attrs[0].value if attrs else None
except ValueError:
return None
class CertificateAuthority:
def __init__(self, organization: str = "", organizational_unit: str = ""):
# Not hardcoded: these come from add-on options (default empty,
@@ -111,9 +135,14 @@ class CertificateAuthority:
def ca_cert_pem(self) -> str:
return self.cert_pem.decode()
def sign_csr(self, csr_pem: str, device_id: str, validity_days: int) -> str:
def sign_csr(self, csr_pem: str, common_name: str, validity_days: int) -> str:
"""Validate and sign a device CSR. Returns the signed cert as PEM.
`common_name` is deliberately NOT tied to any MQTT topic/device_id
here -- callers decide where it comes from (see
extract_csr_common_name / extract_cert_common_name below, and
main.py's resolution order).
Raises ValueError on a malformed or untrustworthy CSR.
"""
try:
@@ -125,7 +154,7 @@ class CertificateAuthority:
raise ValueError("CSR signature is invalid")
now = datetime.datetime.now(datetime.timezone.utc)
subject = self._build_name(device_id)
subject = self._build_name(common_name)
builder = (
x509.CertificateBuilder()
@@ -155,7 +184,7 @@ class CertificateAuthority:
critical=False,
)
.add_extension(
x509.SubjectAlternativeName([x509.DNSName(device_id)]),
x509.SubjectAlternativeName([x509.DNSName(common_name)]),
critical=False,
)
)