From 769139999dd2d40358bc006f1e7d48d5e70f9939 Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Thu, 19 Nov 2026 17:09:12 +0100 Subject: [PATCH] Companion: the private key and the BLE PIN are again refused over USB, UART and WiFi -- only paired BLE gets them Co-Authored-By: Claude Opus 5.5 --- README.md | 17 ++- examples/companion_radio/MyMesh.cpp | 10 +- src/helpers/BaseSerialInterface.h | 6 + src/helpers/MultiSerialInterface.h | 34 +++++- test/test_multi_serial/test_multi_serial.cpp | 116 +++++++++++++++++++ 5 files changed, 173 insertions(+), 10 deletions(-) create mode 100644 test/test_multi_serial/test_multi_serial.cpp diff --git a/README.md b/README.md index bc2578d1..2c11f301 100644 --- a/README.md +++ b/README.md @@ -169,9 +169,12 @@ Repeater navíc: patch pro INA219: Alkel Brostl). Čipy INA se rozlišují podle ID, takže INA226 už se nedetekuje jako INA219. Odpor bočníku se nastavuje za běhu a ukládá se: `sensor set ina.shunt `. -* Companion: `gps duty` přežije reboot, export klíče a PIN jsou na - neověřeném USB spojení zamčené, kopie data sestavení má hlídanou délku. - KISS modem: oprava o jedna vedle při zkracování jména. +* Companion: `gps duty` přežije reboot, kopie data sestavení má hlídanou + délku. Export privátního klíče, čtení BLE PINu a jeho změna (i přes CLI + `set pin`) jdou jen přes spárované BLE, ne přes USB, UART ani WiFi/TCP, + a odpověď s klíčem či PINem jde jen tam, odkud přišel dotaz. (Merge v1.17 + tu kontrolu potichu vyřadil; build bez BLE bere své jediné rozhraní dál + jako důvěryhodné.) KISS modem: oprava o jedna vedle při zkracování jména. ### Veřejné buildy: výchozí nastavení pro český mesh @@ -485,8 +488,12 @@ The repeater also has: and first INA219 patch: Alkel Brostl). INA chips are told apart by their ID, so an INA226 is no longer taken for an INA219. The shunt is set at runtime and saved: `sensor set ina.shunt `. -* Companion: `gps duty` persists, key export / PIN gated on an unauthenticated - USB link, bounded build-date copy. KISS modem: off-by-one in the name clamp. +* Companion: `gps duty` persists, bounded build-date copy. Private key + export, reading the BLE PIN and changing it (CLI `set pin` included) work + only over paired BLE, never over USB, a UART or WiFi/TCP, and the reply that + carries the key or PIN goes back only where the request came from. (The + v1.17 merge had silently disabled that gate; a build without BLE still + trusts its one transport.) KISS modem: off-by-one in the name clamp. ### Public builds: Czech mesh defaults diff --git a/examples/companion_radio/MyMesh.cpp b/examples/companion_radio/MyMesh.cpp index 71686f2e..460455d8 100644 --- a/examples/companion_radio/MyMesh.cpp +++ b/examples/companion_radio/MyMesh.cpp @@ -1098,7 +1098,7 @@ void MyMesh::handleCmdFrame(size_t len) { i += 20; out_frame[i++] = _prefs.isRepeatEn() ? 1 : 0; // v9+ out_frame[i++] = _prefs.path_hash_mode; // v10+ - _serial->writeFrame(out_frame, i); + _serial->writeReplyFrame(out_frame, i); // carries the PIN: never to the other transports } else if (cmd_frame[0] == CMD_APP_START && len >= 8) { // sent when app establishes connection, respond with node ID // cmd_frame[1..7] reserved future @@ -1148,7 +1148,11 @@ void MyMesh::handleCmdFrame(size_t len) { text[tlen] = 0; // ensure null reply_buf[0] = 0; - if (!handleCommand(text, 0, reply_buf)) { + if (memcmp(text, "set pin ", 8) == 0 && !_serial->isConnectionSecure()) { + // CUSTOM (TeTeHacko): the same gate as CMD_SET_DEVICE_PIN -- through the + // CLI, USB set the PIN it may not set directly, then paired over BLE + strcpy(reply_buf, "Error: set pin only over paired BLE"); + } else if (!handleCommand(text, 0, reply_buf)) { strcat(reply_buf, "Unknown command"); // reply_buf may have cmd prefix from 'text' } out_frame[0] = RESP_CODE_CLI_REPLY; @@ -1569,7 +1573,7 @@ void MyMesh::handleCmdFrame(size_t len) { uint8_t reply[65]; reply[0] = RESP_CODE_PRIVATE_KEY; self_id.writeTo(&reply[1], 64); - _serial->writeFrame(reply, 65); + _serial->writeReplyFrame(reply, 65); // only back to the paired BLE peer, not out on USB too } #else writeDisabledFrame(); diff --git a/src/helpers/BaseSerialInterface.h b/src/helpers/BaseSerialInterface.h index 7e4c4b0a..8590a72e 100644 --- a/src/helpers/BaseSerialInterface.h +++ b/src/helpers/BaseSerialInterface.h @@ -27,4 +27,10 @@ public: // distinguish an unauthenticated local channel (USB) from a paired one, so // callers can gate sensitive commands (key export, PIN read/set). virtual bool isConnectionSecure() const { return true; } + + // CUSTOM (TeTeHacko): a reply that must go back only over the transport the + // request came on -- the private key, the BLE PIN. writeFrame() of a + // multi-transport wrapper sends to every enabled transport, so a key exported + // over paired BLE also went out on USB. Single transports: the same thing. + virtual size_t writeReplyFrame(const uint8_t src[], size_t len) { return writeFrame(src, len); } }; diff --git a/src/helpers/MultiSerialInterface.h b/src/helpers/MultiSerialInterface.h index f7742b24..1cd83824 100644 --- a/src/helpers/MultiSerialInterface.h +++ b/src/helpers/MultiSerialInterface.h @@ -26,6 +26,7 @@ private: bool _enabled = false; RegisteredInterface _interfaces[MAX_INTERFACES] = {}; + int _last_rx = -1; // CUSTOM (TeTeHacko): slot of the most recent frame's transport public: bool addInterface(InterfaceType type, BaseSerialInterface* iface) { @@ -57,6 +58,7 @@ public: for(int i = 0; i < MAX_INTERFACES; i++){ if(_interfaces[i].instance == iface){ _interfaces[i] = {}; + if (_last_rx == i) _last_rx = -1; return true; } } @@ -184,11 +186,13 @@ public: } // try to read a frame from any enabled interface - for(auto iface : _interfaces){ + for(int i = 0; i < MAX_INTERFACES; i++){ + auto& iface = _interfaces[i]; if(iface.instance && iface.instance->isEnabled()){ size_t frameSize = iface.instance->checkRecvFrame(dest); if(frameSize > 0){ - return frameSize; + _last_rx = i; + return frameSize; } } } @@ -197,4 +201,30 @@ public: return 0; } + // CUSTOM (TeTeHacko): only a frame that came over (MITM-paired) Bluetooth is + // authenticated. USB, a hardware UART, WiFi and Ethernet carry no pairing, so + // the companion's key-export / PIN gates (89eb71ab) refuse them. Without this + // override the base class answered `true` for everything: the gates written + // for SerialDualInterface went dead when the v1.17 merge replaced it with + // this class, and USB could export the key and read or set the BLE PIN. + // A build with no Bluetooth (USB-only companion) has nothing to pair over, so + // its one transport stays trusted, as BaseSerialInterface defines it. + bool isConnectionSecure() const override { + bool has_ble = false; + for (const auto& iface : _interfaces) { + if (iface.instance && iface.type == InterfaceType::Bluetooth) has_ble = true; + } + if (!has_ble) return true; + if (_last_rx < 0) return false; + const auto& iface = _interfaces[_last_rx]; + return iface.instance && iface.type == InterfaceType::Bluetooth && iface.instance->isConnectionSecure(); + } + + size_t writeReplyFrame(const uint8_t src[], size_t len) override { + if (!_enabled || len == 0 || _last_rx < 0) return 0; + auto* iface = _interfaces[_last_rx].instance; + if (!iface || !iface->isEnabled()) return 0; + return iface->writeFrame(src, len); + } + }; diff --git a/test/test_multi_serial/test_multi_serial.cpp b/test/test_multi_serial/test_multi_serial.cpp new file mode 100644 index 00000000..4b7117c0 --- /dev/null +++ b/test/test_multi_serial/test_multi_serial.cpp @@ -0,0 +1,116 @@ +// CUSTOM (TeTeHacko): which transport a companion frame came over, and where a +// sensitive reply goes. The v1.17 merge swapped SerialDualInterface for this +// class, whose isConnectionSecure() was the base class's `true`, so USB could +// export the private key and read or set the BLE PIN. +#include +#include +#include +#include +#include "helpers/MultiSerialInterface.h" + +class FakeIface : public BaseSerialInterface { +public: + bool enabled = false; + std::deque> inbox; + std::vector> sent; + + void enable() override { enabled = true; } + void disable() override { enabled = false; } + bool isEnabled() const override { return enabled; } + bool isConnected() const override { return true; } + bool isWriteBusy() const override { return false; } + size_t writeFrame(const uint8_t src[], size_t len) override { + sent.emplace_back(src, src + len); + return len; + } + size_t checkRecvFrame(uint8_t dest[]) override { + if (inbox.empty()) return 0; + auto f = inbox.front(); + inbox.pop_front(); + memcpy(dest, f.data(), f.size()); + return f.size(); + } +}; + +struct MultiSerial : ::testing::Test { + MultiSerialInterface multi; + FakeIface ble, usb; + uint8_t buf[MAX_FRAME_SIZE + 1]; + + void SetUp() override { + multi.addInterface(InterfaceType::Bluetooth, &ble); + multi.addInterface(InterfaceType::USB, &usb); + multi.enable(); + } +}; + +TEST_F(MultiSerial, NothingReceivedIsNotSecure) { + EXPECT_FALSE(multi.isConnectionSecure()); +} + +TEST_F(MultiSerial, UsbFrameIsNotSecure) { + usb.inbox.push_back({1, 2, 3}); + ASSERT_EQ(multi.checkRecvFrame(buf), 3u); + EXPECT_FALSE(multi.isConnectionSecure()); +} + +TEST_F(MultiSerial, BleFrameIsSecure) { + ble.inbox.push_back({1, 2, 3}); + ASSERT_EQ(multi.checkRecvFrame(buf), 3u); + EXPECT_TRUE(multi.isConnectionSecure()); +} + +TEST_F(MultiSerial, FollowsTheLatestFrame) { + ble.inbox.push_back({1}); + multi.checkRecvFrame(buf); + usb.inbox.push_back({2}); + multi.checkRecvFrame(buf); + EXPECT_FALSE(multi.isConnectionSecure()); // a BLE session does not bless a later USB frame +} + +TEST_F(MultiSerial, ReplyGoesOnlyToTheRequestingTransport) { + ble.inbox.push_back({1}); + multi.checkRecvFrame(buf); + const uint8_t key[] = {0xAA, 0xBB}; + EXPECT_EQ(multi.writeReplyFrame(key, 2), 2u); + EXPECT_EQ(ble.sent.size(), 1u); + EXPECT_TRUE(usb.sent.empty()); +} + +TEST_F(MultiSerial, PushStillGoesEverywhere) { + const uint8_t push[] = {0x83}; + multi.writeFrame(push, 1); + EXPECT_EQ(ble.sent.size(), 1u); + EXPECT_EQ(usb.sent.size(), 1u); +} + +TEST_F(MultiSerial, NoReplyBeforeAnyRequest) { + const uint8_t key[] = {0xAA}; + EXPECT_EQ(multi.writeReplyFrame(key, 1), 0u); + EXPECT_TRUE(ble.sent.empty()); + EXPECT_TRUE(usb.sent.empty()); +} + +TEST_F(MultiSerial, RemovedTransportForgetsTheSource) { + ble.inbox.push_back({1}); + multi.checkRecvFrame(buf); + multi.removeInterface(&ble); + const uint8_t key[] = {0xAA}; + EXPECT_EQ(multi.writeReplyFrame(key, 1), 0u); +} + +TEST(MultiSerialUsbOnly, SingleTransportStaysTrusted) { + MultiSerialInterface multi; + FakeIface usb; + multi.addInterface(InterfaceType::USB, &usb); + multi.enable(); + uint8_t buf[MAX_FRAME_SIZE + 1]; + usb.inbox.push_back({1}); + multi.checkRecvFrame(buf); + EXPECT_TRUE(multi.isConnectionSecure()); // nothing to pair over, as before v1.17 +} + +int main(int argc, char** argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}