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 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-11-19 17:09:12 +01:00
1 parent cac3249e41
commit 769139999d
5 files changed
+172 -9

No files matched your search

+12 -5
View File
@@ -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 <mΩ>`.
* 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 <mOhm>`.
* 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
+7 -3
View File
@@ -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();
+6
View File
@@ -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); }
};
+31 -1
View File
@@ -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,10 +186,12 @@ 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){
_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);
}
};
@@ -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 <gtest/gtest.h>
#include <string.h>
#include <deque>
#include <vector>
#include "helpers/MultiSerialInterface.h"
class FakeIface : public BaseSerialInterface {
public:
bool enabled = false;
std::deque<std::vector<uint8_t>> inbox;
std::vector<std::vector<uint8_t>> 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();
}