Skip to content

feat: TLS for cloud relay MQTT (port 8883, pinned CA, fail closed) - #3

Closed
johnsonflix wants to merge 19 commits into
evolights/mainfrom
feat/cloud-relay-tls
Closed

johnsonflix wants to merge 19 commits into
evolights/mainfrom
feat/cloud-relay-tls

Conversation

@johnsonflix

Copy link
Copy Markdown
Owner

Summary

Replaces the placeholder plain WiFiClient cloud-relay MQTT transport with WiFiClientSecure over port 8883, using the CA cert that the cloud delivers at pair time. The device fails closed if no CA cert is on file — no setInsecure() escape hatch — so a misconfigured device is loud rather than silently exposing credentials.

To make <WiFiClientSecure.h> resolvable from a usermod's compile scope (it isn't reachable through wled.h's transitive includes), pio-scripts/load_usermods.py now enumerates the active framework package's bundled libraries and prepends their src/ dirs to every usermod's CPPPATH. This unblocks any other usermod that needs a stock framework header in the future, not just cloud_relay.

What changed

Firmware

  • usermods/cloud_relay/cloud_relay.cpp — drop plain WiFiClient, swap in WiFiClientSecure, setCACert(g_caCertPem), fail closed when g_caCertPem is empty, reset the pinned-CA flag on re-pair / unpair so a fresh CA actually takes effect.
  • pio-scripts/load_usermods.py — new _framework_library_include_dirs(xenv) helper, wired into the existing wrapped_ConfigureProjectLibBuilder. Anchored under EVOLIGHTS-ANCHOR: usermod-framework-includes and usermod-framework-includes-apply.
  • tools/verify-evolights-integration.sh — new anchored invariants for every load-bearing piece: the build glue, the WiFiClientSecure declaration, setCACert(), new PubSubClient(g_tls), and a hard guard against setInsecure() or plain-WiFiClient regressions.

Cloud (separate repo, evolights-cloud, committed alongside)

  • mosquitto/mosquitto.conf — TLS listener on 8883 is now active, not commented out. Operator must drop fullchain.pem + privkey.pem into mosquitto/certs/.
  • mosquitto/certs/.gitkeep — README-style placeholder documenting the cert files the operator must provide and how to wire a live Let's Encrypt directory in via bind mount.
  • docker-compose.yml — port 1883 is now bound to 127.0.0.1 only (was world-exposed). Production deployments should drop the 1883 mapping entirely; localhost-only is the safe default for ops/debugging.

Test plan

  • tools/verify-evolights-integration.sh passes locally (36/36).
  • CI builds green for all three EvoLights envs (esp32dev_evolights, esp32_eth_evolights, esp32s3dev_8MB_qspi_evolights).
  • On a paired device, MQTT establishes on port 8883 and credentials are no longer visible to a passive listener on the wire.
  • On an unpaired device (or one whose CA was wiped), the relay logs "refusing to connect: no CA cert on file" and does not connect.

Notes

arjohnson15 and others added 15 commits May 2, 2026 12:13
…ken override

The previous firmware-build.yml wrote a platformio_override.ini that did:
    build_flags = ${env:esp32dev.build_flags} -D USERMOD_EVOLIGHTS_CLOUD_RELAY
which PlatformIO rejects with "Infinite recursion has been detected for
build_flags option in the env:esp32dev section" because a section can't
self-reference.

Replace the runtime override with three new envs appended to platformio.ini,
each extending its upstream counterpart:

    [env:esp32dev_evolights]
    extends = env:esp32dev
    custom_usermods = ${env:esp32dev.custom_usermods} cloud_relay
    build_flags     = ${env:esp32dev.build_flags} -D USERMOD_EVOLIGHTS_CLOUD_RELAY -D WLED_FORK_NAME=\"EvoLights\"

References from the new env into the old env are not self-referential and
PlatformIO resolves them cleanly. Same pattern for esp32_eth_evolights and
esp32s3dev_8MB_evolights.

The new env block is wrapped in EVOLIGHTS-ANCHOR markers; the verifier now
asserts all three envs still exist so an upstream merge that scrambles
platformio.ini is caught.

Workflow simplified: firmware-build.yml now just runs `pio run -e <env>`
against the *_evolights envs in its matrix, no override file generation.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
The previous commit referenced [env:esp32s3dev_8MB] which doesn't exist in
upstream WLED's platformio.ini — actual S3 envs are esp32s3dev_8MB_qspi
and esp32s3dev_8MB_opi. The bad extends made PlatformIO refuse to parse
the entire file, which is why all three matrix builds failed at config
load.

Renames everywhere: env definition, build matrix, verifier assertion.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…ubSubClient

WLED uses lib_compat_mode = strict in [env], which blocks auto-inclusion of
ESP32 framework libraries. cloud_relay.cpp #include <WiFiClientSecure.h>
fails with 'No such file or directory' as a result.

Adds an [evolights_common] section listing the framework libs + PubSubClient,
and references it from each *_evolights env's lib_deps alongside the env's
existing deps.
…k libs

WLED's [env] sets lib_compat_mode = strict, which prevents PIO from
auto-resolving framework libraries (WiFiClientSecure, HTTPClient, Update)
when they're transitively included from a symlinked usermod source.

Override per-env to lib_compat_mode=soft and lib_ldf_mode=chain+ so PIO
follows our usermod's #include directives and finds the framework headers.
Adding 'WiFiClientSecure' as a lib_deps entry doesn't help: framework libs
aren't in the package registry, they ship with the platform.
…solve

PIO's load_usermods.py adds usermods as symlink:// lib_deps entries.
When LDF scans the symlinked library it uses the LIBRARY's own settings,
not the env's, so env-level lib_compat_mode/lib_ldf_mode overrides don't
reach the usermod's #include scan.

Setting libLDFMode=deep+ and libCompatMode=soft inside library.json
configures the scan at the right level. PIO can then locate the
WiFiClientSecure framework library via deep #include analysis.
….json

Symlinked usermods get scanned as isolated libraries; PIO's LDF doesn't
walk back up to project lib_deps from inside that scan, so WiFiClientSecure
isn't found unless the library itself declares it.

Switched library.json's dependencies to the array form so we can scope per
platform (the framework libs only exist on espressif32). PubSubClient stays
as a regular external dep.
Six iterations of trying to make WLED's build system find WiFiClientSecure
from a usermod's compile scope: lib_deps entry, lib_compat_mode=soft,
lib_ldf_mode=chain+/deep+, library.json deps array — none worked. The
root cause appears to be how WLED's load_usermods.py wires symlinked
usermods as isolated library compile scopes that don't see framework
include paths.

Pragmatic call: ship the rest of the architecture on plain TCP; mark TLS
as a critical TODO with a long header comment in cloud_relay.cpp listing
mitigation options for the interim:
  - private network + VPN, OR
  - mqtt-over-WSS via a different MQTT client, OR
  - link mbedTLS at WLED-core level and expose a TLS client to usermods

Per-device mqtt_user/mqtt_pass auth still works, but a passive eavesdropper
sees credentials in clear. Acceptable for development; NOT acceptable for
production — that's the entire content of the TODO.
… deps

PIO compiles usermods in an isolated library scope that does not see WLED's
vendored ArduinoJson at wled00/src/dependencies/json/, nor the project-local
ESPAsyncWebServer headers, when included via standalone <Header.h>. wled.h
already pulls all of these in, so the redundant includes only cause 'No such
file or directory' errors.
…ncJson handler signature

Two compile errors after redundant includes were removed:

1. wled_cloud_auth.h forward-declared 'class JsonObject' but ArduinoJson v6
   makes JsonObject a typedef, not a class. Pull in the wled-vendored
   ArduinoJson header so the type is fully known to consumers.

2. cloud_relay's /cloud/pair handler used the upstream AsyncJson lambda
   signature (AsyncWebServerRequest*, JsonVariant&). WLED's vendored
   AsyncCallbackJsonWebHandler at wled00/src/dependencies/json/AsyncJson-v6.h
   passes only AsyncWebServerRequest*; the body sits in request->_tempObject.
   Deserialize it ourselves to reach the same JsonObject obj.

Verifier still 25/25.
…des it

Same root issue as cloud_relay.cpp: WLED vendors ArduinoJson at
wled00/src/dependencies/json/ArduinoJson-v6.h and includes it via that
relative path in wled.h and fcn_declare.h. The standalone <ArduinoJson.h>
isn't on the include path for any WLED source file.
…cert

Replaces the placeholder plain WiFiClient transport with WiFiClientSecure
backed by the CA cert that the cloud delivers at pair time. The device
refuses to connect without a CA on file (no setInsecure() fallback), so
a misconfigured device is loud rather than silently exposing credentials.

To make <WiFiClientSecure.h> resolvable from a usermod's compile scope
(it isn't through wled.h's transitive includes), pio-scripts/load_usermods.py
now enumerates the active framework package's bundled libraries and prepends
their src dirs to every usermod's CPPPATH. This unblocks any other usermod
that needs a stock framework header in the future.

Verifier gets new anchored invariants for every load-bearing piece: the
build glue, the WiFiClientSecure declaration, the setCACert() call, the
PubSubClient(g_tls) wiring, and a hard guard against setInsecure() or
plain-WiFiClient regressions.
…iFiClientSecure.h>

The per-lib hook in load_usermods.py was logging "exposing 25 framework
library include dir(s) to usermods" but the include still wasn't reachable
from cloud_relay.cpp's compile context. Adding -I<path> directly to the
env's CCFLAGS via a pre-script is unconditional: every compile in the env
sees the headers, with no dependency on PIO's per-lib CPPPATH propagation
order.

Both layers are kept (belt + suspenders): if either stops working after a
PlatformIO upgrade, the other is the fallback. Verifier gains anchors for
the new script and the platformio.ini extra_scripts wiring.
… log paths

Log line previously said "exposed 25 framework library include dir(s) ...
WiFiClientSecure: NO" — the standard libraries/<lib>/src walk simply was not
seeing WiFiClientSecure under tasmota's framework-arduinoespressif32 zipball.

Added a Tier B pass that rglob's the entire framework package for
REQUIRED_HEADERS (WiFiClientSecure, HTTPClient, Update, WiFi, WiFiClient) and
adds each hit's containing dir to the env's include path. Logs every found
path explicitly so we can see exactly where things resolved (or where they
went MISSING) on the next CI run.
…s it

Diagnostic logging from expose_framework_libs.py confirmed that the
framework-arduinoespressif32 v2.0.18 zipball used by these EvoLights envs
ships WITHOUT WiFiClientSecure (and HTTPClient ships SSL stubs that prevent
linker errors but do not implement TLS — see tasmota/arduino-esp32 wled#447).

Vendoring the upstream tasmota arduino-esp32 v2.0.18 WiFiClientSecure
sources (6 files, ~44 KB) under usermods/cloud_relay/vendor/WiFiClientSecure/
gives us a real, CA-pinned TLS stack on top of the framework's bundled
mbedTLS. library.json's srcFilter compiles them into the usermod and a -I
flag exposes the headers to cloud_relay.cpp. Verifier asserts every vendor
file is present and that cloud_relay still satisfies the TLS invariants
(setCACert, fail-closed, no setInsecure, no plain WiFiClient).

The expose_framework_libs env-scope script is kept: it still does its job
for the other framework headers we need (HTTPClient.h, Update.h, WiFi.h)
that DO live in the slim distribution. Diagnostic logging trimmed to one
line per header now that we've solved the mystery.
… ssl_client.cpp compiles its impl

Vendored ssl_client.cpp wraps the entire implementation (lines 23-554) in
'#if !defined(...PSK...) #else <impl> #endif'. Without the PSK macro defined,
all of ssl_init/start_ssl_client/etc. become empty, producing the link-time
'undefined reference to ssl_init(sslclient_context*)' chain we just hit.

We don't actually use PSK at runtime (CA cert mode only), but defining the
macro flips the impl on so the symbols exist. If mbedtls_ssl_conf_psk turns
out to be missing from the linker, the PSK call site is unreachable for our
config (pskIdent stays NULL) and we can stub it out as a follow-up.
mbedtls_ssl_conf_alpn_protocols isn't compiled into tasmota's slim mbedtls,
which made ssl_client.cpp fail to compile even though we never set ALPN.
Wrap the call site in a feature-flag #if so it's skipped when ALPN is not
available; runtime requests for ALPN log a warning and continue without it.
… mbedtls SSL

The vendored WiFiClientSecure approach hit a wall: tasmota's slim
framework-arduinoespressif32 v2.0.18 doesn't just strip WiFiClientSecure,
it strips the entire mbedtls SSL module too. Linking produces dozens of
undefined references to mbedtls_ssl_init/setup/handshake/etc — symbols
that simply aren't in the framework's libmbedtls.a.

To get a real TLS stack on the EvoLights builds, override the platform on
just the *_evolights envs to the stock PlatformIO espressif32 @ 6.7.0,
which retains the full mbedtls + the upstream WiFiClientSecure under
libraries/. Upstream WLED envs are unaffected.

This is a load-bearing change that may surface new compatibility issues
with WLED's vendored deps (NeoPixelBus, ESPAsyncWebServer fork, esp_dmx,
etc.) — pushing now to surface them in CI.
@johnsonflix johnsonflix closed this May 3, 2026
johnsonflix added a commit that referenced this pull request May 3, 2026
Resolves the TLS deferral that closed PR #3.

Root cause was Tasmota slim framework strips mbedtls SSL. Fix is per-env override of platform = espressif32 @ 6.7.0 ONLY on the *_evolights envs (upstream WLED envs untouched). Stock arduino-esp32 v2.0.x ships full mbedtls.

- WiFiClientSecure g_tls with setCACert pinning, fail-closed if no CA cert
- ota_verifier also on WiFiClientSecure (signature is trust anchor)
- Verifier extended to 59 invariants including 7 new TLS-positive guards
- 4MB envs need big_partitions (LittleFS shrinks 960KB -> 256KB) to fit mbedtls
- All three *_evolights envs build green; flash usage 83-97%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants