"""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//...). 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 inspect/back up. """ import json import os import threading import time STATE_PATH = "/data/state.json" _lock = threading.Lock() def _load(): if not os.path.exists(STATE_PATH): 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", {}) # 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): tmp_path = STATE_PATH + ".tmp" with open(tmp_path, "w") as f: json.dump(state, f, indent=2) os.replace(tmp_path, STATE_PATH) def add_pending(device_id: str, csr_pem: str, meta: dict): with _lock: state = _load() state["pending"][device_id] = { "csr": csr_pem, "meta": meta or {}, "requested_at": time.time(), } _save(state) def list_pending(): with _lock: return _load()["pending"] def pop_pending(device_id: str): with _lock: state = _load() return state["pending"].pop(device_id, None), state def list_issued(): with _lock: return _load()["issued"] def mark_issued(device_id: str, cert_pem: str, not_after: str, serial: int, state=None): with _lock: state = state or _load() state["issued"][device_id] = { "cert": cert_pem, "not_after": not_after, "issued_at": time.time(), "serial": serial, } _save(state) def reject_pending(device_id: str, reason: str): with _lock: state = _load() state["pending"].pop(device_id, None) _save(state) def get_broker_cert(): with _lock: return _load()["broker_cert"] def set_broker_cert(hostnames: list, not_after: str): with _lock: state = _load() state["broker_cert"] = { "hostnames": hostnames, "not_after": not_after, "issued_at": time.time(), } _save(state) def revoke_device(device_id: str, reason: str): """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 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(), } _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"]