mcm renewal
- Changed certificate renwal procedure
This commit is contained in:
@@ -3,11 +3,18 @@ MQTT side of the enrollment protocol.
|
||||
|
||||
Topics (prefix is configurable, default "cert/enroll"):
|
||||
<prefix>/<device_id>/request node -> here {"csr": "<PEM>", "meta": {...}}
|
||||
<prefix>/<device_id>/renew node -> here {"csr": "<PEM>", "timestamp": <int>, "signature": "<base64>", "meta": {...}}
|
||||
<prefix>/<device_id>/renew-begin node -> here (payload ignored) asks for a renewal challenge
|
||||
<prefix>/<device_id>/challenge here -> node {"nonce": "<hex>", "expires_in": <seconds>} (not retained)
|
||||
<prefix>/<device_id>/renew node -> here {"csr": "<PEM>", "nonce": "<hex>", "signature": "<base64>", "meta": {...}}
|
||||
<prefix>/<device_id>/issued here -> node {"cert": "<PEM>", "ca": "<PEM>"} (retained)
|
||||
<prefix>/<device_id>/rejected here -> node {"reason": "..."}
|
||||
<prefix>/<device_id>/revoked here -> node {"reason": "..."} (best-effort notice; see DEVICE_PROTOCOL.md)
|
||||
|
||||
Renewal needs no clock on the node. The node asks for a challenge, gets a
|
||||
single-use nonce back, and signs "renew-v1|<device_id>|<nonce>|<sha256 of
|
||||
the CSR's DER>" with its CURRENT key. See challenges.py and
|
||||
RENEWAL_PROTOCOL.md.
|
||||
|
||||
device_id is expected to be unique per physical device (not a shared
|
||||
bootstrap identity) -- that's what makes per-device topics work without
|
||||
any extra filtering in the payload. The cert's CN is a separate concern,
|
||||
@@ -30,10 +37,11 @@ log = logging.getLogger("mqtt_worker")
|
||||
|
||||
|
||||
class MqttWorker:
|
||||
def __init__(self, host, port, username, password, topic_prefix, on_request, on_renew):
|
||||
def __init__(self, host, port, username, password, topic_prefix, on_request, on_renew, on_renew_begin):
|
||||
self.topic_prefix = topic_prefix.rstrip("/")
|
||||
self.on_request = on_request # callback(device_id, csr_pem, meta)
|
||||
self.on_renew = on_renew # callback(device_id, csr_pem, timestamp, signature_b64, meta)
|
||||
self.on_renew_begin = on_renew_begin # callback(device_id)
|
||||
self.on_renew = on_renew # callback(device_id, csr_pem, nonce, signature_b64, meta)
|
||||
|
||||
self.client = mqtt.Client(client_id="mqtt-cert-manager")
|
||||
if username:
|
||||
@@ -89,10 +97,15 @@ class MqttWorker:
|
||||
)
|
||||
return
|
||||
request_topic = f"{self.topic_prefix}/+/request"
|
||||
renew_begin_topic = f"{self.topic_prefix}/+/renew-begin"
|
||||
renew_topic = f"{self.topic_prefix}/+/renew"
|
||||
client.subscribe(request_topic, qos=1)
|
||||
client.subscribe(renew_begin_topic, qos=1)
|
||||
client.subscribe(renew_topic, qos=1)
|
||||
log.info("connected to broker, subscribed to %s and %s", request_topic, renew_topic)
|
||||
log.info(
|
||||
"connected to broker, subscribed to %s, %s and %s",
|
||||
request_topic, renew_begin_topic, renew_topic,
|
||||
)
|
||||
|
||||
def _on_disconnect(self, client, userdata, rc):
|
||||
if rc != 0:
|
||||
@@ -104,11 +117,21 @@ class MqttWorker:
|
||||
return
|
||||
device_id, kind = parts[-2], parts[-1]
|
||||
|
||||
if kind == "renew-begin":
|
||||
# Nothing in this payload is used, so don't insist on valid JSON
|
||||
# (a node may well send an empty message).
|
||||
log.info("renewal challenge requested by %s", device_id)
|
||||
self.on_renew_begin(device_id)
|
||||
return
|
||||
|
||||
try:
|
||||
payload = json.loads(msg.payload.decode())
|
||||
except ValueError as exc:
|
||||
log.warning("malformed payload from %s on %s: %s", device_id, msg.topic, exc)
|
||||
return
|
||||
if not isinstance(payload, dict):
|
||||
log.warning("malformed payload from %s on %s: expected a JSON object", device_id, msg.topic)
|
||||
return
|
||||
|
||||
if kind == "request":
|
||||
try:
|
||||
@@ -122,13 +145,29 @@ class MqttWorker:
|
||||
elif kind == "renew":
|
||||
try:
|
||||
csr_pem = payload["csr"]
|
||||
timestamp = payload["timestamp"]
|
||||
signature_b64 = payload["signature"]
|
||||
except KeyError as exc:
|
||||
log.warning("malformed renewal from %s: missing %s", device_id, exc)
|
||||
return
|
||||
if not isinstance(csr_pem, str):
|
||||
log.warning("malformed renewal from %s: csr must be a string", device_id)
|
||||
return
|
||||
nonce = payload.get("nonce")
|
||||
if nonce is None and "timestamp" in payload:
|
||||
log.warning(
|
||||
"renewal from %s uses the old timestamp format, which is no longer "
|
||||
"supported; it will go to manual approval", device_id,
|
||||
)
|
||||
log.info("renewal request received from %s", device_id)
|
||||
self.on_renew(device_id, csr_pem, timestamp, signature_b64, payload.get("meta", {}))
|
||||
self.on_renew(device_id, csr_pem, nonce, signature_b64, payload.get("meta", {}))
|
||||
|
||||
def publish_challenge(self, device_id: str, nonce: str, expires_in: int):
|
||||
# Deliberately NOT retained: a retained challenge would be handed to
|
||||
# any later subscriber long after it expired.
|
||||
topic = f"{self.topic_prefix}/{device_id}/challenge"
|
||||
payload = json.dumps({"nonce": nonce, "expires_in": expires_in})
|
||||
self.client.publish(topic, payload, qos=1, retain=False)
|
||||
log.info("published renewal challenge for %s (valid %ss)", device_id, expires_in)
|
||||
|
||||
def publish_issued(self, device_id: str, cert_pem: str, ca_pem: str):
|
||||
topic = f"{self.topic_prefix}/{device_id}/issued"
|
||||
@@ -156,4 +195,4 @@ class MqttWorker:
|
||||
# retained message on the broker (per MQTT spec).
|
||||
issued_topic = f"{self.topic_prefix}/{device_id}/issued"
|
||||
self.client.publish(issued_topic, payload=b"", qos=1, retain=True)
|
||||
log.info("published revocation for %s and cleared retained cert", device_id)
|
||||
log.info("published revocation for %s and cleared retained cert", device_id)
|
||||
Reference in New Issue
Block a user