diff --git a/mqtt-cert-manager/CHANGELOG.md b/mqtt-cert-manager/CHANGELOG.md index 2a75d52..7092b47 100644 --- a/mqtt-cert-manager/CHANGELOG.md +++ b/mqtt-cert-manager/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 0.6.1 + +- Bug fix for revokation procedure +- Increased CRL expiration from 7 days to 365 + ## 0.6.0 - Changed certificate renwal procedure diff --git a/mqtt-cert-manager/app/ca.py b/mqtt-cert-manager/app/ca.py index d359b63..f4d6198 100644 --- a/mqtt-cert-manager/app/ca.py +++ b/mqtt-cert-manager/app/ca.py @@ -270,7 +270,7 @@ class CertificateAuthority: x509.CertificateRevocationListBuilder() .issuer_name(self.cert.subject) .last_update(now) - .next_update(now + datetime.timedelta(days=7)) + .next_update(now + datetime.timedelta(days=365)) ) for serial, revoked_at in revoked_entries: revoked_cert = ( diff --git a/mqtt-cert-manager/app/main.py b/mqtt-cert-manager/app/main.py index e555fe9..6502650 100644 --- a/mqtt-cert-manager/app/main.py +++ b/mqtt-cert-manager/app/main.py @@ -195,7 +195,7 @@ def handle_renew_begin(device_id): Only devices that already hold an issued, non-revoked cert get one, so strangers can't make the server accumulate nonces. A device that gets no answer should fall back to an ordinary enrollment request.""" - if device_id in store.list_revoked(): + if store.is_revoked(device_id): log.info("renewal challenge refused for %s: device is revoked", device_id) return if device_id not in store.list_issued(): @@ -237,7 +237,7 @@ def handle_renew(device_id, csr_pem, nonce, signature_b64, meta): log.info("renewal from %s falling back to manual approval: %s", device_id, why) store.add_pending(device_id, csr_pem, meta) - if device_id in store.list_revoked(): + if store.is_revoked(device_id): fall_back_to_manual("device is revoked") return diff --git a/mqtt-cert-manager/app/store.py b/mqtt-cert-manager/app/store.py index 35fbafe..59d2c6d 100644 --- a/mqtt-cert-manager/app/store.py +++ b/mqtt-cert-manager/app/store.py @@ -1,10 +1,7 @@ """Simple JSON-file store for pending requests and issued cert records. Keyed by device_id -- the unique per-device identifier from the MQTT -topic path (cert/enroll//...). CN is deliberately NOT unique -in this deployment (many devices share one CN per category, for ACL -grouping), so it can't be used as a storage key -- device_id is the -only thing guaranteed unique per physical device. +topic path (cert/enroll//...). A real deployment could swap this for sqlite, but request volume here is tiny (one entry per physical device) so a flat file is fine and easy to @@ -25,9 +22,17 @@ def _load(): return {"pending": {}, "issued": {}, "revoked": {}, "broker_cert": None} with open(STATE_PATH, "r") as f: state = json.load(f) - state.setdefault("broker_cert", None) - state.setdefault("revoked", {}) - return state + state.setdefault("broker_cert", None) + state.setdefault("revoked", {}) + # migrate old device_id-keyed revoked entries to serial-keyed + for key, entry in list(state["revoked"].items()): + if "device_id" not in entry: + del state["revoked"][key] + state["revoked"][str(entry["serial"])] = { + "device_id": key, + **entry, + } + return state def _save(state): @@ -100,16 +105,21 @@ def set_broker_cert(hostnames: list, not_after: str): def revoke_device(device_id: str, reason: str): - """Move a device from issued -> revoked. Keeps the issued record too - (for audit history) but also records it separately so the CRL builder - has a fast list to work from. Returns the serial number, or None if - the device has no issued cert on record.""" + """Revoke the device's current cert. Revocations are keyed by serial, + so re-issuing for the same device never un-revokes older certs and a + new cert can be revoked later. Returns the serial, or None if the + device has no issued cert or it is already revoked.""" with _lock: state = _load() issued = state["issued"].get(device_id) if not issued: return None - state["revoked"][device_id] = { + serial = str(issued["serial"]) # str: JSON keys must be strings, and + # these serials exceed safe int range elsewhere + if serial in state["revoked"]: + return None + state["revoked"][serial] = { + "device_id": device_id, "serial": issued["serial"], "reason": reason, "revoked_at": time.time(), @@ -117,7 +127,16 @@ def revoke_device(device_id: str, reason: str): _save(state) return issued["serial"] +def is_revoked(device_id: str) -> bool: + """True only if the device's *current* cert is revoked.""" + with _lock: + state = _load() + issued = state["issued"].get(device_id) + if not issued: + return False + return str(issued["serial"]) in state["revoked"] def list_revoked(): + """All revoked certs, keyed by serial (use for building the CRL).""" with _lock: return _load()["revoked"] diff --git a/mqtt-cert-manager/config.yaml b/mqtt-cert-manager/config.yaml index 69c8e0b..7c3dedc 100644 --- a/mqtt-cert-manager/config.yaml +++ b/mqtt-cert-manager/config.yaml @@ -1,5 +1,5 @@ name: "MQTT Certificate Manager" -version: "0.6.0" +version: "0.6.1" slug: "mqtt_cert_manager" description: "Local CA that issues and manages TLS client certificates for MQTT nodes" url: "https://gitea.viratak.com/bitopa/virarepo/src/branch/main/mqtt-cert-manager"