From a5fd4b74881927e78463f7288316c5e02e4bb09e Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Sat, 11 Jul 2026 19:21:11 +0200 Subject: [PATCH] harden: gate key-export/PIN over unauthenticated USB companion Add virtual BaseSerialInterface::isConnectionSecure() (default true). On the dual interface it is true only for MITM-paired BLE. Gate CMD_EXPORT_PRIVATE_KEY and CMD_SET_DEVICE_PIN, and hide the BLE PIN in CMD_DEVICE_QUERY, when the frame arrived over unauthenticated USB. Co-Authored-By: Claude Opus 4.8 --- examples/companion_radio/MyMesh.cpp | 44 +++++++++++++++++++---------- src/helpers/BaseSerialInterface.h | 8 ++++++ 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/examples/companion_radio/MyMesh.cpp b/examples/companion_radio/MyMesh.cpp index 6fbb0f74..5f03b924 100644 --- a/examples/companion_radio/MyMesh.cpp +++ b/examples/companion_radio/MyMesh.cpp @@ -1014,7 +1014,11 @@ void MyMesh::handleCmdFrame(size_t len) { out_frame[i++] = FIRMWARE_VER_CODE; out_frame[i++] = MAX_CONTACTS / 2; // v3+ out_frame[i++] = MAX_GROUP_CHANNELS; // v3+ - memcpy(&out_frame[i], &_prefs.ble_pin, 4); + // Never reveal the BLE pairing PIN over an unauthenticated transport (USB): + // it would let a momentary physical connection learn the PIN and then pair + // over BLE remotely. Only report it to an already-paired BLE peer. + uint32_t reported_ble_pin = _serial->isConnectionSecure() ? _prefs.ble_pin : 0; + memcpy(&out_frame[i], &reported_ble_pin, 4); i += 4; memset(&out_frame[i], 0, 12); strcpy((char *)&out_frame[i], FIRMWARE_BUILD_DATE); @@ -1468,10 +1472,15 @@ void MyMesh::handleCmdFrame(size_t len) { _serial->writeFrame(reply, i); } else if (cmd_frame[0] == CMD_EXPORT_PRIVATE_KEY) { #if ENABLE_PRIVATE_KEY_EXPORT - uint8_t reply[65]; - reply[0] = RESP_CODE_PRIVATE_KEY; - self_id.writeTo(&reply[1], 64); - _serial->writeFrame(reply, 65); + if (!_serial->isConnectionSecure()) { + // never export the identity over an unauthenticated transport (USB) + writeDisabledFrame(); + } else { + uint8_t reply[65]; + reply[0] = RESP_CODE_PRIVATE_KEY; + self_id.writeTo(&reply[1], 64); + _serial->writeFrame(reply, 65); + } #else writeDisabledFrame(); #endif @@ -1769,17 +1778,22 @@ void MyMesh::handleCmdFrame(size_t len) { } } else if (cmd_frame[0] == CMD_SET_DEVICE_PIN && len >= 5) { - // get pin from command frame - uint32_t pin; - memcpy(&pin, &cmd_frame[1], 4); - - // ensure pin is zero, or a valid 6 digit pin - if (pin == 0 || (pin >= 100000 && pin <= 999999)) { - _prefs.ble_pin = pin; - savePrefs(); - writeOKFrame(); - } else { + if (!_serial->isConnectionSecure()) { + // don't let an unauthenticated transport (USB) change the BLE pairing PIN writeErrFrame(ERR_CODE_ILLEGAL_ARG); + } else { + // get pin from command frame + uint32_t pin; + memcpy(&pin, &cmd_frame[1], 4); + + // ensure pin is zero, or a valid 6 digit pin + if (pin == 0 || (pin >= 100000 && pin <= 999999)) { + _prefs.ble_pin = pin; + savePrefs(); + writeOKFrame(); + } else { + writeErrFrame(ERR_CODE_ILLEGAL_ARG); + } } } else if (cmd_frame[0] == CMD_GET_CUSTOM_VARS) { out_frame[0] = RESP_CODE_CUSTOM_VARS; diff --git a/src/helpers/BaseSerialInterface.h b/src/helpers/BaseSerialInterface.h index e9a3f2ab..1fc71abb 100644 --- a/src/helpers/BaseSerialInterface.h +++ b/src/helpers/BaseSerialInterface.h @@ -18,4 +18,12 @@ public: virtual bool isWriteBusy() const = 0; virtual size_t writeFrame(const uint8_t src[], size_t len) = 0; virtual size_t checkRecvFrame(uint8_t dest[]) = 0; + + // Whether the transport carrying the current / most-recent frame is + // authenticated. A single-transport interface is trusted by definition + // (physically-connected USB, or a MITM-paired BLE link), so the default is + // true. Multi-transport wrappers (e.g. SerialDualInterface) override this to + // 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; } };