From a8668840594b351e355183489ab3c2216a48d429 Mon Sep 17 00:00:00 2001 From: agessaman Date: Tue, 12 May 2026 21:18:58 -0700 Subject: [PATCH] Refactor alert PSK handling to exclusively use hex format Updated the alert PSK implementation to remove base64 support, now requiring a 32-character hex format for private channel secrets. Adjusted related CLI commands, error messages, and internal handling to ensure consistency with the new format. This change enhances clarity and aligns with the mobile app's "Share Channel" output. Relevant updates made across multiple files, including documentation and preference handling. --- MQTT_IMPLEMENTATION.md | 10 +- examples/simple_repeater/MyMesh.cpp | 2 +- examples/simple_room_server/MyMesh.cpp | 2 +- src/helpers/AlertReporter.cpp | 93 ++++------------- src/helpers/AlertReporter.h | 14 +-- src/helpers/CommonCLI.cpp | 138 +++++++++---------------- src/helpers/CommonCLI.h | 4 +- 7 files changed, 86 insertions(+), 177 deletions(-) diff --git a/MQTT_IMPLEMENTATION.md b/MQTT_IMPLEMENTATION.md index 7c1d7880e..d575ea93e 100644 --- a/MQTT_IMPLEMENTATION.md +++ b/MQTT_IMPLEMENTATION.md @@ -714,7 +714,7 @@ A "recovered" message is sent once when the underlying connection comes back. Af | Setting | Default | Notes | |---------|---------|-------| | `alert` | `off` | Master enable for automatic fault alerts | -| `alert.psk` | *(unset)* | Private channel secret. Accepts either base64 (24 or 44 chars, like `BaseChatMesh::addChannel`) or hex (32 or 64 chars, like the mobile app's "Share Channel" output). Stored internally as base64. | +| `alert.psk` | *(unset)* | Private channel secret as **32 hex chars** (16-byte channel key) — the same format the mobile app's "Share Channel" emits, and what every other secret-shaped CLI command (e.g. `prv.key`) uses. | | `alert.hashtag` | *(unset)* | Informational only; set via `set alert.hashtag` to pre-derive `alert.psk` from `sha256("#name")[0..15]`. Cleared when `alert.psk` is set directly. | | `alert.region` | *(unset)* | Optional region name; overrides the repeater's `default_scope` for alert sends only. Empty = use `default_scope`. Looked up lazily via `RegionMap`; unknown names silently fall back to `default_scope`. | | `alert.wifi` | `30` (min) | 0 disables WiFi alerts | @@ -727,14 +727,14 @@ A "recovered" message is sent once when the underlying connection comes back. Af Get: - `get alert` — master on/off -- `get alert.psk` — the active base64 PSK (or `(unset)`) +- `get alert.psk` — the active 32-hex-char PSK (or `(unset)`) - `get alert.hashtag` — the originating hashtag (or `(unset)`, e.g. after `set alert.psk` overrides the hashtag-derived key) - `get alert.region` — alert-only scope override (or `(unset, using default scope)`) - `get alert.wifi` / `get alert.mqtt` / `get alert.interval` Set: - `set alert on` / `set alert off` -- `set alert.psk ` — 24-/44-char base64 **or** 32-/64-char hex (16- or 32-byte secret); rejects banned channels (Public, `#test`, `#bot`). Clears `alert.hashtag` since the new key is operator-supplied. The mobile app's "share" output is hex; either format is accepted. +- `set alert.psk ` — 32 hex chars (16-byte channel secret); rejects banned channels (Public, `#test`, `#bot`). Paste the mobile app's "Share Channel" output as-is. Clears `alert.hashtag` since the new key is operator-supplied. - `set alert.psk` (no argument) — clears both `alert.psk` and `alert.hashtag` - `set alert.hashtag ` — derives the 16-byte key from `sha256("#name")` *once*, stores it as `alert.psk`, and remembers the hashtag for `get alert.hashtag`. `#` prefix is added if omitted (so `alerts` and `#alerts` are equivalent). Refuses banned hashtag names. - `set alert.hashtag` (no argument) — clears both `alert.psk` and `alert.hashtag` @@ -762,10 +762,10 @@ Anyone running a companion app and subscribed to the `#ops-alerts` hashtag chann ### Example: dedicated alerts channel with a private PSK -Generate a 16-byte random PSK and base64-encode it (24 chars), or use the companion app's "Add channel" feature to create one and copy the secret. Then: +Generate a 16-byte random PSK as 32 hex chars (`openssl rand -hex 16`), or use the companion app's "Add channel" feature and copy the "Share Channel" output. Then: ```bash -set alert.psk # base64 (24/44 chars) or hex (32/64 chars) — mobile "share" works as-is +set alert.psk <32_hex_chars> # 16-byte channel secret; mobile "Share Channel" pastes in directly set alert.wifi 10 set alert.mqtt 60 set alert on diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index 37d1da5ce..0153ed9ed 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -954,7 +954,7 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc // can fire. The sender prefix on outgoing alert messages is always the // node name (`set name ...`), so there's no separate `alert.name`. _prefs.alert_enabled = 0; - _prefs.alert_psk_b64[0] = '\0'; + _prefs.alert_psk_hex[0] = '\0'; _prefs.alert_hashtag[0] = '\0'; _prefs.alert_region[0] = '\0'; // empty = use default_scope _prefs.alert_wifi_minutes = 30; // 30 minutes diff --git a/examples/simple_room_server/MyMesh.cpp b/examples/simple_room_server/MyMesh.cpp index 34c4258b5..de7e3e111 100644 --- a/examples/simple_room_server/MyMesh.cpp +++ b/examples/simple_room_server/MyMesh.cpp @@ -678,7 +678,7 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc // Alert channel defaults (same as repeater; off by default and unconfigured). // Operator must pick `set alert.psk` or `set alert.hashtag` before alerts fire. _prefs.alert_enabled = 0; - _prefs.alert_psk_b64[0] = '\0'; + _prefs.alert_psk_hex[0] = '\0'; _prefs.alert_hashtag[0] = '\0'; _prefs.alert_region[0] = '\0'; _prefs.alert_wifi_minutes = 30; diff --git a/src/helpers/AlertReporter.cpp b/src/helpers/AlertReporter.cpp index 356c7aacd..df38d6fc5 100644 --- a/src/helpers/AlertReporter.cpp +++ b/src/helpers/AlertReporter.cpp @@ -5,45 +5,6 @@ #include #include -// Minimal base64 decoder — kept local to avoid dragging the densaugeo/base64 -// PlatformIO dependency into every repeater env that doesn't otherwise need -// it (only chat builds with MAX_GROUP_CHANNELS pulled it in via BaseChatMesh). -// Returns the number of decoded bytes, or 0 on error. Output buffer must be -// at least (in_len * 3 / 4) bytes. -static int alert_decode_base64(const char* in, size_t in_len, uint8_t* out) { - static const int8_t TBL[128] = { - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, - 52,53,54,55,56,57,58,59,60,61,-1,-1,-1, 0,-1,-1, - -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, - 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, - -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, - 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1 - }; - size_t pad = 0; - while (in_len > 0 && in[in_len - 1] == '=') { in_len--; pad++; } - if ((in_len + pad) % 4 != 0) return 0; - if (pad > 2) return 0; - - size_t out_pos = 0; - uint32_t buffer = 0; - int bits = 0; - for (size_t i = 0; i < in_len; i++) { - unsigned char c = (unsigned char)in[i]; - if (c >= 128) return 0; - int v = TBL[c]; - if (v < 0) return 0; - buffer = (buffer << 6) | (uint32_t)v; - bits += 6; - if (bits >= 8) { - bits -= 8; - out[out_pos++] = (uint8_t)((buffer >> bits) & 0xFF); - } - } - return (int)out_pos; -} - // Header layout for PAYLOAD_TYPE_GRP_TXT before encryption: // [0..3] timestamp (uint32_t LE) — also helps make packet_hash unique // [4] TXT_TYPE_PLAIN @@ -98,12 +59,12 @@ void AlertReporter::setBridge(MQTTBridge* bridge) { // // Provenance for each row can be re-derived with: // printf '#name' | openssl dgst -sha256 | cut -c1-32 -// or for raw b64 PSKs: -// echo 'izOH6cXN6mrJ5e26oRXNcg==' | base64 -d | xxd -p +// or for the Public PSK: +// echo 'izOH6cXN6mrJ5e26oRXNcg==' | base64 -d | xxd -p -c 16 // // To ban an additional channel: append one new row; no other code changes -// required. The matcher converts the candidate's 16-byte secret to a -// lowercase hex string and does a linear strcmp — N is tiny. +// required. Both the table entries and `alert_psk_hex` are 32 lowercase hex +// chars (16-byte secret), so the matcher is a direct strcmp. struct BannedAlertChannel { const char* label; const char* secret_hex; // 32 lowercase hex chars (no 0x, no separators) @@ -120,12 +81,7 @@ static const BannedAlertChannel BANNED_ALERT_CHANNELS[] = { const char* alertReporterBannedChannelMatch(const uint8_t* secret16) { char hex[33]; - static const char* H = "0123456789abcdef"; - for (int i = 0; i < 16; i++) { - hex[i*2] = H[(secret16[i] >> 4) & 0xF]; - hex[i*2+1] = H[secret16[i] & 0xF]; - } - hex[32] = '\0'; + mesh::Utils::toHex(hex, secret16, 16); for (size_t i = 0; i < sizeof(BANNED_ALERT_CHANNELS) / sizeof(BANNED_ALERT_CHANNELS[0]); i++) { if (strcmp(hex, BANNED_ALERT_CHANNELS[i].secret_hex) == 0) { return BANNED_ALERT_CHANNELS[i].label; @@ -134,42 +90,37 @@ const char* alertReporterBannedChannelMatch(const uint8_t* secret16) { return nullptr; } -const char* alertReporterBannedChannelMatchB64(const char* psk_b64) { - if (!psk_b64) return nullptr; - size_t len_in = strlen(psk_b64); - if (len_in == 0) return nullptr; - uint8_t secret[32]; - int n = alert_decode_base64(psk_b64, len_in, secret); - if (n != 16) return nullptr; // banned table only contains 16-byte secrets +const char* alertReporterBannedChannelMatchHex(const char* psk_hex) { + if (!psk_hex || strlen(psk_hex) != 32) return nullptr; + uint8_t secret[16]; + if (!mesh::Utils::fromHex(secret, 16, psk_hex)) return nullptr; return alertReporterBannedChannelMatch(secret); } bool AlertReporter::resolveChannel(mesh::GroupChannel& out) const { if (!_prefs) return false; - // alert_psk_b64 is the single source of truth — `set alert.hashtag` - // pre-derives the base64 PSK from sha256("#name")[0..15] at CLI time. - const char* psk = _prefs->alert_psk_b64; - size_t psk_len = strlen(psk); - if (psk_len == 0 || psk_len >= sizeof(_prefs->alert_psk_b64)) return false; + // alert_psk_hex is the single source of truth — `set alert.hashtag` + // pre-derives the hex-encoded PSK from sha256("#name")[0..15] at CLI time. + // Only 16-byte secrets (32 hex chars) are supported; 32-byte channel keys + // are not used anywhere in MeshCore practice and not represented in the + // banned table either. + const char* psk = _prefs->alert_psk_hex; + if (strlen(psk) != 32) return false; memset(out.secret, 0, sizeof(out.secret)); - int len = alert_decode_base64(psk, psk_len, out.secret); - if (len != 32 && len != 16) return false; + if (!mesh::Utils::fromHex(out.secret, 16, psk)) return false; // Belt-and-suspenders against an operator pasting a banned PSK directly // into alert.psk, or a hashtag whose hash somehow collides with one of the // banned 16-byte secrets (astronomically improbable, but free to check). - if (len == 16) { - const char* banned = alertReporterBannedChannelMatch(out.secret); - if (banned) { - ALERT_DEBUG_PRINTLN("refused banned channel '%s' for alert", banned); - return false; - } + const char* banned = alertReporterBannedChannelMatch(out.secret); + if (banned) { + ALERT_DEBUG_PRINTLN("refused banned channel '%s' for alert", banned); + return false; } - // PATH_HASH_SIZE bytes — same scheme used by addChannel(). - mesh::Utils::sha256(out.hash, sizeof(out.hash), out.secret, len); + mesh::Utils::sha256(out.hash, sizeof(out.hash), out.secret, 16); return true; } diff --git a/src/helpers/AlertReporter.h b/src/helpers/AlertReporter.h index eec488020..dbbd154f6 100644 --- a/src/helpers/AlertReporter.h +++ b/src/helpers/AlertReporter.h @@ -18,12 +18,12 @@ const char* alertReporterBannedChannelMatch(const uint8_t* secret16); /** - * Convenience: base64-decodes \a psk_b64 and forwards to - * alertReporterBannedChannelMatch. Returns nullptr if not banned (or if - * the input doesn't decode to a 16-byte key — non-16-byte keys cannot - * match any banned entry anyway). + * Convenience: hex-decodes \a psk_hex (32 lowercase/uppercase hex chars) and + * forwards to alertReporterBannedChannelMatch. Returns nullptr if not banned + * (or if the input isn't a valid 32-char hex string — only 16-byte secrets + * are present in the banned table). */ -const char* alertReporterBannedChannelMatchB64(const char* psk_b64); +const char* alertReporterBannedChannelMatchHex(const char* psk_hex); /** * \brief Send-only group-channel "fault alert" reporter for repeater/observer @@ -34,7 +34,7 @@ const char* alertReporterBannedChannelMatchB64(const char* psk_b64); * message on the configured alert channel ("WiFi down 47m — MyObserver"), * then arms a "recovered" message for the next state transition. * - * The alert channel must be explicitly configured to either a private base64 + * The alert channel must be explicitly configured to either a private hex * PSK (`set alert.psk`) or a hashtag name (`set alert.hashtag`); the * well-known PUBLIC group key (and a small list of other auto-responder * channels — see BANNED_ALERT_CHANNELS in AlertReporter.cpp) are rejected on @@ -67,7 +67,7 @@ public: #endif /** - * Re-derive the cached GroupChannel from \a alert_psk_b64. Call from the + * Re-derive the cached GroupChannel from \a alert_psk_hex. Call from the * CLI hot-reload hook after `set alert.psk` / `set alert.hashtag` / `set alert on|off`. */ void onConfigChanged(); diff --git a/src/helpers/CommonCLI.cpp b/src/helpers/CommonCLI.cpp index b327a3bcb..b6ac08904 100644 --- a/src/helpers/CommonCLI.cpp +++ b/src/helpers/CommonCLI.cpp @@ -6,28 +6,6 @@ #include #include -// Tiny base64 encoder used by `set alert.hashtag` to render a derived 16-byte -// key into NodePrefs::alert_psk_b64. Kept inline so we don't drag the -// densaugeo/base64 PlatformIO dep into every CLI-using build. -static size_t alert_encode_base64(const uint8_t* in, size_t in_len, char* out, size_t out_size) { - static const char TBL[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - size_t needed = ((in_len + 2) / 3) * 4 + 1; - if (out_size < needed) return 0; - size_t o = 0; - for (size_t i = 0; i < in_len; i += 3) { - uint32_t v = (uint32_t)in[i] << 16; - int rem = (int)(in_len - i); - if (rem > 1) v |= (uint32_t)in[i + 1] << 8; - if (rem > 2) v |= (uint32_t)in[i + 2]; - out[o++] = TBL[(v >> 18) & 0x3F]; - out[o++] = TBL[(v >> 12) & 0x3F]; - out[o++] = (rem > 1) ? TBL[(v >> 6) & 0x3F] : '='; - out[o++] = (rem > 2) ? TBL[v & 0x3F] : '='; - } - out[o] = '\0'; - return o; -} - #ifndef BRIDGE_MAX_BAUD #define BRIDGE_MAX_BAUD 115200 #endif @@ -306,8 +284,8 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) { if (file.available() >= (int)sizeof(_prefs->alert_enabled)) { file.read((uint8_t *)&_prefs->alert_enabled, sizeof(_prefs->alert_enabled)); } - if (file.available() >= (int)sizeof(_prefs->alert_psk_b64)) { - file.read((uint8_t *)&_prefs->alert_psk_b64, sizeof(_prefs->alert_psk_b64)); + if (file.available() >= (int)sizeof(_prefs->alert_psk_hex)) { + file.read((uint8_t *)&_prefs->alert_psk_hex, sizeof(_prefs->alert_psk_hex)); } if (file.available() >= (int)sizeof(_prefs->alert_wifi_minutes)) { file.read((uint8_t *)&_prefs->alert_wifi_minutes, sizeof(_prefs->alert_wifi_minutes)); @@ -325,7 +303,7 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) { file.read((uint8_t *)&_prefs->alert_region, sizeof(_prefs->alert_region)); } // ensure null termination after raw read - _prefs->alert_psk_b64[sizeof(_prefs->alert_psk_b64) - 1] = '\0'; + _prefs->alert_psk_hex[sizeof(_prefs->alert_psk_hex) - 1] = '\0'; _prefs->alert_hashtag[sizeof(_prefs->alert_hashtag) - 1] = '\0'; _prefs->alert_region[sizeof(_prefs->alert_region) - 1] = '\0'; @@ -452,7 +430,7 @@ void CommonCLI::savePrefs(FILESYSTEM* fs) { file.write((uint8_t *)&_prefs->radio_watchdog_minutes, sizeof(_prefs->radio_watchdog_minutes)); // 316 // Alert channel fields (appended) file.write((uint8_t *)&_prefs->alert_enabled, sizeof(_prefs->alert_enabled)); - file.write((uint8_t *)&_prefs->alert_psk_b64, sizeof(_prefs->alert_psk_b64)); + file.write((uint8_t *)&_prefs->alert_psk_hex, sizeof(_prefs->alert_psk_hex)); file.write((uint8_t *)&_prefs->alert_wifi_minutes, sizeof(_prefs->alert_wifi_minutes)); file.write((uint8_t *)&_prefs->alert_mqtt_minutes, sizeof(_prefs->alert_mqtt_minutes)); file.write((uint8_t *)&_prefs->alert_min_interval_min, sizeof(_prefs->alert_min_interval_min)); @@ -873,7 +851,7 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re } else { strcpy(text, "[test] alert channel ok"); } - if (!_prefs->alert_psk_b64[0]) { + if (!_prefs->alert_psk_hex[0]) { strcpy(reply, "Error: alert channel not configured (set alert.psk or set alert.hashtag)"); } else { bool ok = _callbacks->sendAlertText(text); @@ -1622,62 +1600,46 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep while (*val == ' ') val++; size_t len = strlen(val); if (len == 0) { - _prefs->alert_psk_b64[0] = '\0'; + _prefs->alert_psk_hex[0] = '\0'; _prefs->alert_hashtag[0] = '\0'; savePrefs(); _callbacks->onAlertConfigChanged(); strcpy(reply, "OK - alert.psk cleared (alerts disabled until configured)"); - } else if (len >= sizeof(_prefs->alert_psk_b64)) { - strcpy(reply, "Error: PSK too long (max 47 chars)"); } else if (val[0] == '#') { strcpy(reply, "Error: use 'set alert.hashtag' for hashtag channels"); + } else if (len != 32) { + // 16-byte channel secret = 32 hex chars. This is what the mobile app's + // "Share Channel" emits, what `set alert.hashtag` derives, and what the + // BANNED_ALERT_CHANNELS table holds. 32-byte channels aren't used + // anywhere in MeshCore practice. + strcpy(reply, "Error: PSK must be 32 hex chars (16-byte channel secret)"); } else { - // Accept either: - // - base64: 24 chars (16-byte secret) or 44 chars (32-byte secret) — - // the format `BaseChatMesh::addChannel` already expects. - // - hex: 32 chars (16-byte secret) or 64 chars (32-byte secret) — - // what the MeshCore mobile app's "share" button emits for both - // private channels and hashtag channels. - // Hex input is converted to base64 once at CLI time so the on-disk - // representation (and AlertReporter's decode path) stays unchanged. - char canonical_b64[48]; - const char* store = nullptr; - - if (len == 32 || len == 64) { - bool all_hex = true; - for (size_t i = 0; i < len; i++) { - if (!mesh::Utils::isHexChar(val[i])) { all_hex = false; break; } - } - if (all_hex) { - uint8_t raw[32]; - int raw_len = (int)(len / 2); - if (mesh::Utils::fromHex(raw, raw_len, val)) { - size_t b64_len = alert_encode_base64(raw, (size_t)raw_len, canonical_b64, sizeof(canonical_b64)); - if (b64_len > 0 && b64_len < sizeof(_prefs->alert_psk_b64)) { - store = canonical_b64; - } - } - } + // Validate all-hex, then normalize via fromHex/toHex so the stored + // form is always lowercase regardless of input case. + uint8_t raw[16]; + bool all_hex = true; + for (size_t i = 0; i < len; i++) { + if (!mesh::Utils::isHexChar(val[i])) { all_hex = false; break; } } - if (!store && (len == 24 || len == 44)) { - store = val; - } - - if (!store) { - strcpy(reply, "Error: PSK must be 32/64 hex chars or 24/44 chars base64 (16- or 32-byte secret)"); - } else if (const char* banned = alertReporterBannedChannelMatchB64(store)) { - // Refuse any key on the banned channel list (Public PSK, well-known - // auto-responder hashtags like #test/#bot, etc.). Fault alerts on - // those channels would spam every node in the area. - sprintf(reply, "Error: refusing banned channel '%s'; pick a private key or hashtag", banned); + if (!all_hex || !mesh::Utils::fromHex(raw, 16, val)) { + strcpy(reply, "Error: PSK must be 32 hex chars (16-byte channel secret)"); } else { - StrHelper::strncpy(_prefs->alert_psk_b64, store, sizeof(_prefs->alert_psk_b64)); - // The new PSK is operator-supplied, so any previously-derived hashtag - // name is no longer accurate provenance — drop it. - _prefs->alert_hashtag[0] = '\0'; - savePrefs(); - _callbacks->onAlertConfigChanged(); - strcpy(reply, "OK - alert.psk updated"); + char normalized[33]; + mesh::Utils::toHex(normalized, raw, 16); + if (const char* banned = alertReporterBannedChannelMatchHex(normalized)) { + // Refuse any key on the banned channel list (Public PSK, well-known + // auto-responder hashtags like #test/#bot, etc.). Fault alerts on + // those channels would spam every node in the area. + sprintf(reply, "Error: refusing banned channel '%s'; pick a private key or hashtag", banned); + } else { + StrHelper::strncpy(_prefs->alert_psk_hex, normalized, sizeof(_prefs->alert_psk_hex)); + // The new PSK is operator-supplied, so any previously-derived + // hashtag name is no longer accurate provenance — drop it. + _prefs->alert_hashtag[0] = '\0'; + savePrefs(); + _callbacks->onAlertConfigChanged(); + strcpy(reply, "OK - alert.psk updated"); + } } } } else if (memcmp(config, "alert.hashtag", 13) == 0 && (config[13] == 0 || config[13] == ' ')) { @@ -1685,7 +1647,7 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep while (*val == ' ') val++; size_t in_len = strlen(val); if (in_len == 0) { - _prefs->alert_psk_b64[0] = '\0'; + _prefs->alert_psk_hex[0] = '\0'; _prefs->alert_hashtag[0] = '\0'; savePrefs(); _callbacks->onAlertConfigChanged(); @@ -1708,9 +1670,9 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep } // Derive the channel key once: first 16 bytes of sha256("#name"), - // then base64-encode and store in alert_psk_b64. We don't re-derive - // on every send — operators can later override with `set alert.psk` - // without leaving stale hashtag text behind. + // store hex-encoded in alert_psk_hex. We don't re-derive on every + // send — operators can later override with `set alert.psk` without + // leaving stale hashtag text behind. uint8_t digest[32]; mesh::Utils::sha256(digest, sizeof(digest), (const uint8_t*)hashtag, (int)strlen(hashtag)); @@ -1719,17 +1681,13 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep // hits the #test entry). Refuse before clobbering existing config. sprintf(reply, "Error: refusing banned channel '%s'", banned); } else { - char b64[48]; - size_t b64_len = alert_encode_base64(digest, 16, b64, sizeof(b64)); - if (b64_len == 0 || b64_len >= sizeof(_prefs->alert_psk_b64)) { - strcpy(reply, "Error: failed to derive PSK from hashtag"); - } else { - StrHelper::strncpy(_prefs->alert_hashtag, hashtag, sizeof(_prefs->alert_hashtag)); - StrHelper::strncpy(_prefs->alert_psk_b64, b64, sizeof(_prefs->alert_psk_b64)); - savePrefs(); - _callbacks->onAlertConfigChanged(); - sprintf(reply, "OK - alert.hashtag: %s", _prefs->alert_hashtag); - } + char hex[33]; + mesh::Utils::toHex(hex, digest, 16); + StrHelper::strncpy(_prefs->alert_hashtag, hashtag, sizeof(_prefs->alert_hashtag)); + StrHelper::strncpy(_prefs->alert_psk_hex, hex, sizeof(_prefs->alert_psk_hex)); + savePrefs(); + _callbacks->onAlertConfigChanged(); + sprintf(reply, "OK - alert.hashtag: %s", _prefs->alert_hashtag); } } } @@ -2096,7 +2054,7 @@ void CommonCLI::handleGetCmd(uint32_t sender_timestamp, char* command, char* rep } else if (memcmp(config, "alert.hashtag", 13) == 0) { sprintf(reply, "> %s", _prefs->alert_hashtag[0] ? _prefs->alert_hashtag : "(unset)"); } else if (memcmp(config, "alert.psk", 9) == 0) { - sprintf(reply, "> %s", _prefs->alert_psk_b64[0] ? _prefs->alert_psk_b64 : "(unset)"); + sprintf(reply, "> %s", _prefs->alert_psk_hex[0] ? _prefs->alert_psk_hex : "(unset)"); } else if (memcmp(config, "alert.region", 12) == 0) { sprintf(reply, "> %s", _prefs->alert_region[0] ? _prefs->alert_region : "(unset, using default scope)"); } else if (memcmp(config, "alert.wifi", 10) == 0) { diff --git a/src/helpers/CommonCLI.h b/src/helpers/CommonCLI.h index e20ba9384..0d50aa790 100644 --- a/src/helpers/CommonCLI.h +++ b/src/helpers/CommonCLI.h @@ -109,12 +109,12 @@ struct NodePrefs { // persisted to file // Sent over the radio (NOT over MQTT) so the alert still works while the MQTT path is broken. // All fields are appended at the end of NodePrefs for binary-compatible upgrades. uint8_t alert_enabled; // 0 = off (default), 1 = on - char alert_psk_b64[48]; // base64 PSK; empty = alerts disabled. PUBLIC_GROUP_PSK is rejected. + char alert_psk_hex[33]; // 32 lowercase hex chars (16-byte channel secret) + null; empty = alerts disabled. Banned keys (Public/#test/#bot) are rejected. uint16_t alert_wifi_minutes; // WiFi-down threshold in minutes (0 = disabled), default 30 uint16_t alert_mqtt_minutes; // MQTT-down threshold in minutes (0 = disabled), default 240 (4 h) uint16_t alert_min_interval_min; // min minutes between alerts for the same fault, default 60, floor 60 // When the operator configures via `set alert.hashtag `, we derive - // alert_psk_b64 from sha256("#name")[0..15] once and remember the hashtag + // alert_psk_hex from sha256("#name")[0..15] once and remember the hashtag // text here purely for `get alert.hashtag` readback. A subsequent // `set alert.psk` clears this field so it doesn't lie about provenance. char alert_hashtag[24];