Skip to content

feat: enroll the app's client key once the phone is validated - #133

Merged
Tichael merged 4 commits into
mainfrom
feat/client-key-enrollment
Dec 5, 2026
Merged

Tichael merged 4 commits into
mainfrom
feat/client-key-enrollment

Conversation

@Tichael

@Tichael Tichael commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Requested by Michaël · project thread

What and why

Closes #128. Phase 1 of OpenCarControls/contracts#44: the app gets its own client key and enrolls it with the device over Bluetooth once the phone is validated, and pins the device's identity per vehicle. Nothing uses the keys for a connection yet; the Dart secure channel session (#129) does. Needs OpenCarControls/contracts#50 (the submodule bump is its own commit and points at that PR's branch until it merges) and the firmware side, OpenCarControls/firmware#124.

  • Client key (lib/secure/client_key.dart): one X25519 key pair per install, made on first use, kept in flutter_secure_storage (Android Keystore), never in SharedPreferences. New dependencies: cryptography (pure Dart X25519/BLAKE2s/ChaCha20-Poly1305, also used by Secure channel session in the app #129), flutter_secure_storage, and device_info_plus for the label.
  • Device identity (lib/secure/device_identity.dart): only a 32-byte key whose key ID matches it is accepted. Pinned in PairedVehicleConfig (devicePublicKey, deviceKeyId), with the client key the device last accepted (enrolledClientKey), so unpairing forgets them.
  • Enrollment (clientKeyEnrollmentProvider): on each Bluetooth connection (and the debug HTTP transport), once the device reports the phone validated, it lists the paired phones and sends EnrollClientKey (kind phone, label = phone maker and model cut to 32 UTF-8 bytes) when the device doesn't list this phone with a key, no identity is pinned, or the device last accepted another key. It pins the identity from the answer. A failure waits for the next connection.
  • Identity change: a reported identity other than the pinned one is never pinned silently. The dashboard shows "The device has a new identity" with Unpair or Trust new identity (which pins it and enrolls again).
  • Paired phones: a phone with a key shows "Remote access set up (label)".
  • Generated constants: a new SecureChannelConstantsBuilder generates lib/secure/secure_channel_constants.g.dart from [secure_channel] in transport.toml, so the key ID length and label limit are imported, not copied.
  • Docs: code-generation.md, architecture.md (new "Secure channel keys" section and providers), ble.md, ui.md (banner order), ui-screens.md.

How it was tested

  • New test/client_key_enrollment_test.dart: key made once and kept, unreadable stored key replaced, identity validation, UTF-8 label cut, enrollment after validation (and pinned identity persisted), nothing while only bonded, no re-enrollment when already enrolled, re-enrollment when the device lost the key, a different device key flagged and then trusted, a different identity in the system state, unpair forgets the identity.
  • dart format, flutter analyze --fatal-infos and the full flutter test pass.
  • Not run: a device build, or anything against a real controller. Locally, flutter test needed thermion_dart's native download skipped (blocked in my sandbox); that workaround is not committed.

Checklist

  • PR title uses conventional-commit style (feat:, fix:, docs:, chore:); CI validates it
  • dart format, flutter analyze --fatal-infos and flutter test pass
  • Docs (docs/ or README) updated if behaviour changed
  • No config.toml or credentials committed; generated files not hand-edited
  • If the wire format changed: the contracts PR is linked and the submodule bump is its own commit

Safety

  • This change does not touch BLE pairing/bonding or how commands are sent to the vehicle
  • It does (explain below)

It adds one system command sent over Bluetooth (EnrollClientKey), only after the device reports the phone validated; the device checks validation itself too. Bonding, source_device_id and vehicle commands are unchanged. The private key never leaves the phone.

https://claude.ai/code/session_01XkZEiWM4SEVB1xBTaoykRa


Generated by Claude Code

Each install makes an X25519 client key, kept in flutter_secure_storage.
Over Bluetooth, once the device reports the phone validated, the app
enrolls the key with EnrollClientKey and pins the device identity per
vehicle. A different identity is never pinned silently: the dashboard
shows a banner to trust it or unpair. The paired phones screen shows
which phones have remote access set up.

Closes #128

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XkZEiWM4SEVB1xBTaoykRa
@Tichael Tichael self-assigned this Dec 1, 2026
Adds a builder for [secure_channel] in transport.toml, so the key ID
length and the client label limit are imported instead of copied.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XkZEiWM4SEVB1xBTaoykRa
@Tichael

Tichael commented Dec 5, 2026

Copy link
Copy Markdown
Collaborator Author

Hardware test: WiCAN (ESP32-C3) + virtual-car

Tested with contracts#50, firmware#124, and the stacked firmware#125 / mobile-app#134. Tests 2, 3 and 5 cover this PR.

Setup

  • Board: WiCAN (ESP32-C3, 4 MB), platform virtual-car. Flash erased before stage A.
  • Stage A: firmware#124 765697e + mobile-app#133 7dd1ecb (release APK, app data cleared) + contracts#50 4ce23a0.
  • Stage B: firmware#125 16d5284, flashed over stage A without erasing. The phone kept the mobile-app#133 APK, because nothing in mobile-app#134 is called yet. mobile-app#134 0eb773e was tested off-device (test 6).
  • Phone: Pixel 9 Pro Fold (Android), one phone.
  • All four code PRs pin contracts 4ce23a0.

Tests

1. [Analysis] The identity is made on the ESP: pass

After the erase, the board created an identity. On boot it logged Device identity: key ID 9b4174fd65e9e12b71a9, and no none, the secure channel is off. The created line itself came on the first boot after flashing, before the logger was running.

2. [PR] Pairing, validation and key enrollment: pass

Fresh pairing of the Virtual car, then validation with the cluster code. After that, Paired phones showed "Remote access set up (…)". No identity banner.

Time (UTC) Event
17:52:52.073 Phone connected (pairing window open)
17:52:59.079 Link encrypted, MTU 251, bond stored
17:52:59.581 App subscribed to TX
17:53:25.264 Validation code shown; phone validated

3. [PR]/[Analysis] Reset with the bond kept: pass

The board was reset with espflash reset --after watchdog-reset. The key ID stayed the same, the client keys came back from flash, and the app still showed the label, with no banner.

Time (UTC) Event
17:55:36.209 Phone validations: restored from flash
17:55:36.213 Client keys: restored from flash
17:55:36.232 Device identity: key ID 9b4174fd65e9e12b71a9 (unchanged)
17:56:10.565 Phone reconnected (is_known=true) after the app's 30 s retry gap, encrypted, subscribed 1 s later

4. [Analysis] firmware#125 flashed over #124 without erasing: pass

  • The build was 1,348,960 bytes, 32.67% of the app partition, about the same as 3D hero: skin accent on the charge glow and cable #124.
  • No ESP transport calls the secure channel yet, so the linker leaves its code out. Its flash, RAM and handshake cost on the C3 still has to be measured once the LAN or MQTT transport lands.
  • On boot, the identity, client keys, validation and bond were all restored, with the same key ID.
  • The phone reconnected at 17:58:08. Live state and several lock/unlock commands over BLE worked, which goes through the new dispatch, routing and dedupe code.
  • No warnings, no TX dropped and no dropped chunks in the log.

5. [Analysis] New device identity: pass

I erased only the access flash range (0x3FC000–0x3FDFFF), which holds the approval, validations, client keys and identity, and kept the bond.

Time (UTC) Event
18:07:26.098 Device identity: created
18:07:26.105 Device identity: key ID ceade7dcc2033c4cbc38
18:07:59.208 Phone reconnected with its bond, encrypted
  • The app showed "The device has a new identity" with the new key ID, plus Unpair and Trust new identity.
  • The tester tapped Trust new identity while the phone wasn't validated yet. The app then asked to allow control again, which is expected: the CAN approval was erased too. After that it ran the normal validation.
  • After validating, Paired phones showed "Remote access set up (…)" again, and the banner was gone.

6. [Analysis] Live interop: the Dart SecureChannel (mobile-app#134) against firmware#125's channel, off-device: pass

A throwaway, uncommitted harness:

  • A temporary core-interface integration test serves on_frame, handle_secure_message and seal over localhost TCP, with the 2-byte length framing.
  • Its setup: random OS keys, one owner phone enrolled and validated, and a second phone enrolled but not validated.
  • A temporary flutter test ran the Dart SecureChannel against it.
Case Result
Owner: handshake, 51 commands (each echoed back on the same session), a keepalive 1 handshake. Every command reached BASIC_CMD_CHANNEL as Transport::Secure with the owner's ble: ID and an authorization. The keepalive was dropped silently
Device restart (sessions forgotten) while the client keeps its session The next frame got the unknown-session reply (type 5). The client handshook again, and the next command went through. The command sent during the restart is lost, as designed
Second phone, enrolled but not validated RejectedNotValidated from the dispatcher, sealed back on its session and decrypted by the client
Unknown client key / wrong pinned device key No answer. The client failed with SecureHandshakeTimeoutException

Host checks for the stacked PRs (CI only ran the title check, since their base isn't main)

  • firmware#125: cargo xtask test, cargo fmt --check, and cargo xtask clippy for pc and esp × virtual-car and egmp all pass. The only warning is a rustls deprecation in board-pc, which also appears on main.
  • mobile-app#134: build_runner, dart format (non-generated files), flutter analyze --fatal-infos and flutter test (557 tests) all pass.

Not tested

  • E-GMP in the car.
  • iOS.
  • A second phone: a stranger trying to enroll, or a key already enrolled by another phone.
  • Xtensa/S3 flash cost.
  • The channel over a real transport (LAN firmware#120, MQTT firmware#122) and its handshake time and memory on the C3.
  • Re-enrolling after a lost key on the device. The unit tests cover it, but it can't be triggered on this board without also changing the identity.

@Tichael
Tichael merged commit 17c0a56 into main Dec 5, 2026
2 checks passed
@Tichael
Tichael deleted the feat/client-key-enrollment branch December 5, 2026 18:28
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.

Create the app's client key and enroll it after validation

2 participants