Enhance MQTT implementation with updated partition table guidance and merged firmware flashing instructions. Add support for new SSL certificate sources in the generate_cert_bundle script, and integrate embedded CA bundle handling in MQTTBridge and CommonCLI. Update platformio.ini files across multiple variants to include new certificate bundle configurations and scripts for improved SSL support.

This commit is contained in:
agessaman committed 2026-03-30 17:05:01 -07:00
1 parent 384b80fc05
commit da775bb281
16 files changed
+3881 -3390

No files matched your search

Binary file not shown.
+93
View File
@@ -9,6 +9,7 @@
#endif
#ifdef ESP_PLATFORM
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <esp_wifi.h>
#endif
#ifdef WITH_MQTT_BRIDGE
@@ -47,6 +48,59 @@ static bool isValidName(const char *n) {
return true;
}
#ifdef ESP_PLATFORM
// Optional embedded CA bundle symbols produced by board_build.embed_files.
// Weak linkage keeps non-bundle builds linkable.
extern const uint8_t rootca_crt_bundle_start[] asm("_binary_src_certs_x509_crt_bundle_bin_start") __attribute__((weak));
extern const uint8_t rootca_crt_bundle_end[] asm("_binary_src_certs_x509_crt_bundle_bin_end") __attribute__((weak));
static bool parseTlsBundleTarget(const char* input, char* host_out, size_t host_out_size, uint16_t* port_out) {
if (!input || !host_out || host_out_size == 0 || !port_out) return false;
while (*input == ' ') input++;
if (*input == '\0') return false;
const char* start = input;
const char* scheme = strstr(input, "://");
if (scheme) start = scheme + 3;
const char* end = start;
while (*end && *end != '/' && *end != '?' && *end != '#') end++;
if (end <= start) return false;
uint16_t port = 443;
const char* host_start = start;
const char* host_end = end;
if (*host_start == '[') {
const char* close = (const char*)memchr(host_start, ']', host_end - host_start);
if (!close) return false;
if ((close + 1) < host_end && *(close + 1) == ':') {
int p = atoi(close + 2);
if (p <= 0 || p > 65535) return false;
port = (uint16_t)p;
}
host_start++;
host_end = close;
} else {
const char* colon = (const char*)memchr(host_start, ':', host_end - host_start);
if (colon) {
int p = atoi(colon + 1);
if (p <= 0 || p > 65535) return false;
port = (uint16_t)p;
host_end = colon;
}
}
size_t host_len = (size_t)(host_end - host_start);
if (host_len == 0 || host_len >= host_out_size) return false;
memcpy(host_out, host_start, host_len);
host_out[host_len] = '\0';
*port_out = port;
return true;
}
#endif
void CommonCLI::loadPrefs(FILESYSTEM* fs) {
bool is_fresh_install = false;
bool is_upgrade = false;
@@ -584,6 +638,45 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
sprintf(reply, "Free: %d, Min: %d, Max: %d, Queue: %d",
ESP.getFreeHeap(), ESP.getMinFreeHeap(), ESP.getMaxAllocHeap(),
_callbacks->getQueueSize());
} else if (memcmp(command, "tls.bundletest ", 15) == 0) {
#ifdef ESP_PLATFORM
if (WiFi.status() != WL_CONNECTED) {
strcpy(reply, "ERR: WiFi not connected");
} else {
size_t bundle_len = 0;
if (rootca_crt_bundle_start != nullptr &&
rootca_crt_bundle_end != nullptr &&
rootca_crt_bundle_end > rootca_crt_bundle_start) {
bundle_len = static_cast<size_t>(rootca_crt_bundle_end - rootca_crt_bundle_start);
}
if (bundle_len == 0) {
strcpy(reply, "ERR: no embedded cert bundle");
} else {
char host[96];
uint16_t port = 443;
if (!parseTlsBundleTarget(command + 15, host, sizeof(host), &port)) {
strcpy(reply, "ERR: usage tls.bundletest <host[:port]|url>");
} else {
WiFiClientSecure client;
#if ESP_ARDUINO_VERSION_MAJOR >= 3
client.setCACertBundle(rootca_crt_bundle_start, bundle_len);
#else
client.setCACertBundle(rootca_crt_bundle_start);
#endif
client.setTimeout(8000);
bool ok = client.connect(host, port);
if (ok) {
client.stop();
snprintf(reply, 160, "OK: TLS bundle verified %s:%u", host, (unsigned)port);
} else {
snprintf(reply, 160, "ERR: TLS bundle failed %s:%u", host, (unsigned)port);
}
}
}
}
#else
strcpy(reply, "ERR: unsupported on this platform");
#endif
} else if (memcmp(command, "start ota", 9) == 0) {
if (!_board->startOTAUpdate(_prefs->node_name, reply)) {
strcpy(reply, "Error");
+76 -4
View File
@@ -51,6 +51,16 @@ static bool isWiFiConfigValid(const NodePrefs* prefs) {
#ifdef WITH_MQTT_BRIDGE
// Optional embedded CA bundle symbols produced by board_build.embed_files.
// Weak linkage keeps non-bundle builds linkable and allows runtime fallback.
extern const uint8_t rootca_crt_bundle_start[] asm("_binary_src_certs_x509_crt_bundle_bin_start") __attribute__((weak));
extern const uint8_t rootca_crt_bundle_end[] asm("_binary_src_certs_x509_crt_bundle_bin_end") __attribute__((weak));
// Track whether the global cert bundle has been loaded into s_crt_bundle.
// Loading must happen exactly once to avoid a use-after-free race when multiple
// TLS slots are set up in sequence (each connect() launches an async task).
static bool s_ca_bundle_loaded = false;
// PSRAM-aware allocation: prefer PSRAM on ESP32 when BOARD_HAS_PSRAM, fallback to internal heap or malloc.
// Use psram_free() for any pointer returned by psram_malloc().
static void* psram_malloc(size_t size) {
@@ -870,14 +880,45 @@ void MQTTBridge::setupSlot(int index) {
}
} else {
// Custom broker slot — build persistent URI
// If host already has a scheme (mqtt://, mqtts://, ws://, wss://), use as-is with port.
// If host already has a scheme (mqtt://, mqtts://, ws://, wss://), preserve the full URI
// (including optional path/query) and only inject :port when the authority has no explicit port.
// Otherwise, infer protocol from port number.
bool has_scheme = (strncmp(slot.host, "mqtt://", 7) == 0 ||
strncmp(slot.host, "mqtts://", 8) == 0 ||
strncmp(slot.host, "ws://", 5) == 0 ||
strncmp(slot.host, "wss://", 6) == 0);
if (has_scheme) {
snprintf(slot.broker_uri, sizeof(slot.broker_uri), "%s:%d", slot.host, slot.port);
const char* authority = strstr(slot.host, "://");
authority = authority ? authority + 3 : slot.host;
const char* path = strchr(authority, '/');
const char* authority_end = path ? path : slot.host + strlen(slot.host);
bool has_explicit_port = false;
// Detect host:port in URI authority (IPv6 literals in [addr]:port are supported).
if (authority < authority_end) {
if (*authority == '[') {
const char* close = (const char*)memchr(authority, ']', authority_end - authority);
if (close && (close + 1) < authority_end && *(close + 1) == ':') {
has_explicit_port = true;
}
} else {
const char* colon = (const char*)memchr(authority, ':', authority_end - authority);
if (colon != nullptr) {
has_explicit_port = true;
}
}
}
if (has_explicit_port || slot.port == 0) {
snprintf(slot.broker_uri, sizeof(slot.broker_uri), "%s", slot.host);
} else {
const size_t authority_len = (size_t)(authority_end - slot.host);
snprintf(slot.broker_uri, sizeof(slot.broker_uri), "%.*s:%u%s",
(int)authority_len,
slot.host,
(unsigned)slot.port,
path ? path : "");
}
} else {
const char* proto = "mqtt";
if (slot.port == 8883) {
@@ -888,12 +929,43 @@ void MQTTBridge::setupSlot(int index) {
snprintf(slot.broker_uri, sizeof(slot.broker_uri), "%s://%s:%d", proto, slot.host, slot.port);
}
slot.client->setServer(slot.broker_uri);
MQTT_DEBUG_PRINTLN("MQTT%d custom broker URI: %s (host='%s', port=%u)",
index + 1, slot.broker_uri, slot.host, (unsigned)slot.port);
// Custom TLS/WSS slots need the system CA cert bundle for server verification
// Custom TLS/WSS slots need a CA bundle for server verification.
// The bundle is loaded into the global s_crt_bundle exactly once to avoid
// a use-after-free race: connect() launches an async FreeRTOS task, and
// calling setCACertBundle() again from a later slot would free the global
// crts array while a prior slot's TLS handshake may still be reading it.
bool needs_tls = (strncmp(slot.broker_uri, "mqtts://", 8) == 0 ||
strncmp(slot.broker_uri, "wss://", 6) == 0);
if (needs_tls) {
slot.client->attachArduinoCACertBundle(true);
if (!s_ca_bundle_loaded) {
size_t bundle_len = 0;
if (rootca_crt_bundle_start != nullptr &&
rootca_crt_bundle_end != nullptr &&
rootca_crt_bundle_end > rootca_crt_bundle_start) {
bundle_len = static_cast<size_t>(rootca_crt_bundle_end - rootca_crt_bundle_start);
}
if (bundle_len > 0) {
MQTT_DEBUG_PRINTLN("MQTT global CA bundle init: embedded bundle (%u bytes)",
(unsigned)bundle_len);
// Load the bundle into the global s_crt_bundle via the first client.
// This is a one-time operation; subsequent clients reuse via attachArduinoCACertBundle.
slot.client->setCACertBundle(rootca_crt_bundle_start, bundle_len);
s_ca_bundle_loaded = true;
} else {
MQTT_DEBUG_PRINTLN("MQTT%d TLS: no embedded cert bundle available", index + 1);
}
} else {
// Global bundle already loaded — just attach the callback for this client.
slot.client->attachArduinoCACertBundle(true);
}
MQTT_DEBUG_PRINTLN("MQTT%d TLS verify: CA bundle %s", index + 1,
s_ca_bundle_loaded ? "active" : "unavailable");
} else {
MQTT_DEBUG_PRINTLN("MQTT%d custom broker uses non-TLS transport", index + 1);
}
if (strlen(slot.username) > 0) {