initial commit
build-and-push / build (aarch64, linux/arm64) (push) Has been cancelled
build-and-push / build (amd64, linux/amd64) (push) Has been cancelled
build-and-push / build (armhf, linux/arm/v6) (push) Has been cancelled
build-and-push / build (armv7, linux/arm/v7) (push) Has been cancelled
build-and-push / build (i386, linux/386) (push) Has been cancelled

This commit is contained in:
2026-08-02 15:35:21 +03:30
commit 0e084adf8d
19 changed files with 1547 additions and 0 deletions
+151
View File
@@ -0,0 +1,151 @@
# 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 value is
used as both the MQTT topic segment and the certificate's Common Name /
SAN, so keep it short, DNS-safe, and unique on your network.
## 1. Generate a key pair and CSR (on-device, once)
The private key never leaves the device. Generate an RSA-2048 (or EC
P-256) key pair and a PKCS#10 CSR with:
- Common Name = `<device_id>`
- No need for 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 repeat step 2 — no need to regenerate the key.
## 2. Publish the request
Topic: `cert/enroll/<device_id>/request`
QoS: 1, retain: false
Payload (JSON):
```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.
## 3. Subscribe for the result, before or immediately after publishing
Subscribe to both of these (QoS 1) so you catch whichever response comes:
- `cert/enroll/<device_id>/issued` — retained. Payload:
```json
{
"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:
```json
{ "reason": "human-readable reason" }
```
- `cert/enroll/<device_id>/revoked` — not retained, best-effort only.
Payload:
```json
{ "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
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
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):
```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.