Files
2026-10-31 14:07:20 +03:30

7.6 KiB

Device enrollment protocol

This is the contract a node's firmware needs to implement to get a client certificate from the add-on. It assumes the node can already reach the MQTT broker (unauthenticated, or with a shared bootstrap username/password — see "Security notes" below).

Default topic prefix is cert/enroll, configurable in the add-on options. Replace <device_id> with a stable, unique identifier for the node (e.g. its MAC address with colons stripped, or a serial number) — this MUST be unique per physical device, even if many devices share the same MQTT login for connecting to the broker. It's what topic routing and this add-on's own record-keeping are both keyed on; two devices sharing a device_id will collide with each other.

The cert's Common Name (CN) is a separate, independent concept — see step 2.

1. Generate a key pair and CSR (on-device, once)

The private key never leaves the device. Generate an EC P-256 (or RSA-2048 which is not recommended) key pair and a PKCS#10 CSR with:

  • Common Name = whatever identity you want written into the issued certificate. This have to be some device_id.
  • No need for other extensions in the CSR itself; the add-on sets the ones that matter (basic constraints, key usage, SAN) when it signs.

Persist the key pair to flash. If a cert is later lost or expires, reuse the same key and just use auto renewal procedure (or repeat step 2 which is not recommended) — no need to regenerate the key.

2. Publish the request (port = 1993)

Topic: cert/enroll/<device_id>/request QoS: 1, retain: false

Payload (JSON):

{
  "csr": "-----BEGIN CERTIFICATE REQUEST-----\n...\n-----END CERTIFICATE REQUEST-----\n",
  "meta": {
    "model": "esp32-sensor-v2",
    "fw_version": "1.4.0"
  }
}

meta is optional and only shown to the human approving the request — put anything there that helps you recognize the device in the UI.

The issued certificate's CN is taken directly from your CSR's own CN (step 1) — there's no separate field for it here. If your CSR has no CN at all, the add-on falls back to using device_id as the CN, but that's a safety net for malformed requests, not something to rely on.

This does NOT apply to renewal (§5) — a renewed cert always keeps the CN of the device's current, still-valid cert, regardless of what a renewal request's CSR claims. Changing identity requires a fresh, human-approved enrollment, not a renewal.

3. Subscribe for the result, before or immediately after publishing (port = 1993)

Subscribe to both of these (QoS 1) so you catch whichever response comes. Since device_id is unique per device (per the requirement above), each device has its own dedicated topics here — no filtering needed, unlike setups where many devices might share one topic.

  • cert/enroll/<device_id>/issued — retained. Payload:

    {
      "cert": "-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----\n",
      "ca": "-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----\n"
    }
    

    Because this is retained, if the device subscribes after a cert was already issued (e.g. after a reboot), it will receive it immediately without needing to re-request.

  • cert/enroll/<device_id>/rejected — not retained. Payload:

    { "reason": "human-readable reason" }
    
  • cert/enroll/<device_id>/revoked — not retained, best-effort only. Payload:

    { "reason": "human-readable reason" }
    

    Sent if you revoke a device's cert after it was issued. This is informational — actual enforcement happens at the TLS layer via Mosquitto's CRL check, which a revoked device will hit on its next connection attempt regardless of whether it saw this message. Devices aren't required to act on it, but it's a convenient hook if you want firmware to, say, stop operating or blink an error LED immediately rather than waiting for the next failed connection.

4. Install and reconnect (port = 9993)

Store cert and ca to flash alongside the existing private key. Reconnect to the broker using mutual TLS (client cert + key, broker CA trust anchor = the same ca). Once confirmed working, you can stop listening on the unauthenticated bootstrap connection for that device.

5. Renewal (port = 9993)

Devices are trusted to renew themselves automatically once they're within renewal_window_days (default 30) of not_after — no human approval needed — but only by proving they still hold the private key matching their currently valid cert. Anything that fails that proof, or is requested too early, silently falls back to the normal manual-approval queue (same as a first-time device), rather than being rejected outright.

Topic: cert/enroll/<device_id>/renew QoS: 1, retain: false

Payload (JSON):

{
  "csr": "-----BEGIN CERTIFICATE REQUEST-----\n...\n-----END CERTIFICATE REQUEST-----\n",
  "timestamp": 1785400000,
  "signature": "<base64>",
  "meta": { "...": "optional, same as the request topic" }
}
  • csr — a fresh CSR. Reusing the same key pair is fine and simplest; generating a new key pair here is also fine (and arguably better hygiene) since this CSR only needs to be valid, not signed by anything — the proof of identity comes from signature below, which is signed by the current, not-yet-expired key, not the new one.
  • timestamp — current Unix time (seconds), integer. Must be within 5 minutes of the add-on's clock or the renewal is rejected as stale (replay protection) — keep device clocks roughly in sync (SNTP).
  • signature — base64-encoded signature over the exact ASCII string "<device_id>|<timestamp>|renew" (e.g. "sensor-01|1785400000|renew"), produced with the device's current private key (the one matching its currently valid, not-yet-expired cert — not the new key in csr, if you generated one).
    • RSA keys: PKCS#1 v1.5 padding, SHA-256.
    • EC keys: ECDSA, SHA-256.

On success, the response arrives on the same .../issued topic as a normal enrollment (retained, same payload shape) — no separate topic to watch for renewals.

If nothing on the device changed, the simplest implementation is: every boot, or on a periodic timer, check how close not_after (which you should have stored from the last issued payload) is to now; if within the renewal window, sign and publish a renewal request; watch .../issued for the (possibly unretained, freshly-published) response same as usual.

Security notes

  • The bootstrap request (step 2) travels over an unauthenticated or broker-credentialed-but-unencrypted connection — that's inherent to any "first contact" enrollment flow. The mitigating control is that a human must explicitly approve each device in the add-on UI before anything is issued; nothing is auto-approved on first enrollment.
  • Renewal (step 5) is the one place a cert gets issued without a human in the loop, and it's gated entirely on a working proof-of-possession signature — a device (or attacker) that doesn't already hold the current private key cannot get a free renewal no matter what it sends; it's routed to the manual queue exactly like a stranger would be.
  • If you want defense in depth beyond human approval on first enrollment, consider adding a shared enrollment secret that devices include in meta and the add-on checks before showing a request as approvable (not implemented in this scaffold, but ca.py/main.py are small — this is a natural next add).
  • Because the device generates its own key pair and only ever sends a public-key CSR over the wire, the private key is never exposed to the network or to the add-on, even to a passive eavesdropper on the bootstrap connection.