Skip to content

feat(mqtt): carry the secure channel over the MQTT relay - #138

Open
Tichael wants to merge 3 commits into
mainfrom
feat/mqtt-relay
Open

Tichael wants to merge 3 commits into
mainfrom
feat/mqtt-relay

Conversation

@Tichael

@Tichael Tichael commented Dec 6, 2026 •

Copy link
Copy Markdown
Collaborator

Requested by Michaël · project thread

Closes #122. Part of OpenCarControls/contracts#44, phase 3. Needs OpenCarControls/contracts#53 (submodule pinned to its branch until it merges); ships with OpenCarControls/mobile-app#140. #139 (WiFi + LTE together) is stacked on this one.

What and why

Any MQTT broker becomes a relay for the secure channel, and plaintext MQTT is gone, so remote commands are as authenticated as on the LAN and the broker never sees state or commands.

  • core-interface/src/relay.rs (new): the relay's logic, host-tested. The device connects with its device key ID as client ID and subscribes to opencar/{device_key_id}/d/+; each client publishes on its own d/{client_key_id} topic and gets answers on c/{client_key_id}. A handshake whose client key doesn't hash to the topic's key ID is dropped. Per broker connection (relay_connect / relay_disconnect): one session per client key (newest wins), and a reconnect ends them all, so clients get an unknown-session frame and handshake again.
  • What goes out (relay_try_next_frame, in order): command responses, a system state when due (session start, availability or LAN address change, every 30 s), then the basic state, throttled per session: at most 1/s while the client sent something within 35 s (its 15 s heartbeat), otherwise every 30 s. Nothing is published without a session. QoS 1, no retain; a frame the client can't queue yet (in-flight limit) waits.
  • Remote tier like the LAN: basic commands from a validated phone, advanced commands answered REJECTED_UNAVAILABLE, system commands dropped.
  • Removed: MQTT_TX/RX/STATE_TX, Transport::Mqtt, the plaintext MQTT dispatcher, mqtt_client_id (config and SystemState), and the topic constants. [network.mqtt].client_id is ignored with a warning.
  • Boards: PC and ESP mqtt_driver_task run the relay loop on top of the existing TCP/TLS connection.
  • xtask: auth_mode = "none" for a broker without a password; basic stays the default. TLS remains optional hardening. Building without TLS saves about 540 KB of flash on the ESP.

Docs: docs/mqtt-tls.md rewritten as docs/mqtt-relay.md; architecture, README, ble, commands, lan, lte, testing and config.toml.example updated.

How it was tested

  • New core-interface/tests/relay.rs: topics, topic/key mismatch, nothing without a session, system and basic state on a new session, per-client topics, system state repeat, active and idle throttle, newer handshake replaces the older, basic command and its response, advanced refused, no plaintext accepted, broker reconnect gives an unknown session, stale broker connection. MQTT tests that tested plaintext were removed.
  • cargo test -p xtask locally; CI passes (tests, host lint, PC and ESP builds for both vehicles, ESP LTE mTLS build).
  • Not run: anything on hardware. Michaël tests on the WiCAN and the LilyGO with the app PR.

Checklist

  • PR title uses conventional-commit style (feat(scope):, fix(scope):, docs:, chore:)
  • cargo fmt --all -- --check, cargo xtask clippy and cargo xtask test pass (CI)
  • Docs (docs/ or README) updated if behaviour changed
  • No config.toml, credentials, build artifacts or .bin files committed
  • If the wire format changed: the contracts PR is linked and the submodule bump is its own commit

Safety

  • This change does not touch CAN TX, BLE pairing/encryption, or OTA
  • It does, and CAN TX is still gated by is_can_read_only() and BLE checks are intact (explain below)

Commands from the relay go through the same secure-channel dispatcher as the LAN: the validated-phone check, the CAN approval and CommandAuthorization are unchanged, and source_device_id comes from the session's client key, never from the app. Plaintext MQTT commands are no longer accepted at all.

https://claude.ai/code/session_01XkZEiWoJmJsqnYdysAR9wp


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Move the MQTT driver onto the secure channel and allow remote commands

2 participants