Explorar el Código

Web: enable TLS session tickets for faster session resumption

Rename arch/esp32/tls_cipher_restrict.c → arch/esp32/tls_server_config.c.
The file now handles two aspects of TLS server configuration via the
existing __wrap_mbedtls_ssl_config_defaults() intercept, so the old name
no longer described its full scope.

Add TLS session ticket support (RFC 5077).  Browsers can now resume
HTTPS sessions without a full RSA handshake, reducing connection latency
on repeated connections.

A static mbedtls_ssl_ticket_context is initialized once on first server
start using the ESP32 hardware RNG (esp_fill_random), then registered via
mbedtls_ssl_conf_session_tickets_cb().  The context must be static because
mbedTLS stores the pointer for the lifetime of the server, analogous to
kServerOnlyCipherSuites.  If mbedtls_ssl_ticket_setup() fails, the flag
remains unset and session tickets are silently skipped rather than
registering callbacks against an uninitialized context.

CONFIG_MBEDTLS_SSL_SESSION_TICKETS is enabled by default in the pre-built
arduino-esp32 framework, so no build system changes are required beyond
the filename update in platformio.ini.
Valentin V. Bartenev hace 3 meses
padre
commit
89a59d5a2e
Se han modificado 2 ficheros con 45 adiciones y 6 borrados
  1. 44 5
      arch/esp32/tls_server_config.c
  2. 1 1
      platformio.ini

+ 44 - 5
arch/esp32/tls_cipher_restrict.c → arch/esp32/tls_server_config.c

@@ -1,11 +1,16 @@
 /*
- * arch/esp32/tls_cipher_restrict.c
+ * arch/esp32/tls_server_config.c
  *
- * Restricts TLS server cipher suites to RSA key exchange, eliminating
- * the ECDH computation that starves IDLE0 and triggers the task watchdog
- * during HTTPS handshakes.
+ * Configures the TLS server via a linker-wrap of mbedtls_ssl_config_defaults():
  *
- * Background:
+ *  1. Restricts cipher suites to RSA key exchange, eliminating the ECDH
+ *     computation that starves IDLE0 and triggers the task watchdog during
+ *     HTTPS handshakes.
+ *
+ *  2. Enables TLS session tickets so that browsers can resume sessions
+ *     without a full RSA handshake.
+ *
+ * Background (cipher suite restriction):
  *   Browsers negotiate ECDHE cipher suites by default. On ESP32-S3 the
  *   hardware RSA accelerator handles RSA key exchange efficiently, but
  *   there is no ECP hardware accelerator. ECDHE requires the server to
@@ -38,9 +43,13 @@
  *   MBEDTLS_SSL_IS_SERVER — this is the discriminator.
  *
  * Note: this file is compiled only for [esp32_base] targets (IDF v4).
+ * CONFIG_MBEDTLS_SSL_SESSION_TICKETS is enabled by default in the
+ * arduino-esp32 2.x pre-built framework, so no sdkconfig changes are needed.
  */
 
 #include "mbedtls/ssl.h"
+#include "mbedtls/ssl_ticket.h"
+#include "esp_random.h"
 
 /*
  * RSA key exchange cipher suites only.
@@ -55,6 +64,21 @@ static const int kServerOnlyCipherSuites[] = {
   0
 };
 
+/*
+ * Session ticket context. Must be static — mbedTLS stores the pointer for
+ * the lifetime of the server, analogous to kServerOnlyCipherSuites.
+ * Initialized once on first server start.
+ */
+static mbedtls_ssl_ticket_context s_ticket_ctx;
+static int s_ticket_ctx_ready = 0;
+
+/* Thin wrapper around the ESP32 hardware RNG for mbedtls_ssl_ticket_setup(). */
+static int esp_rng(void *ctx, unsigned char *buf, size_t len) {
+  (void)ctx;
+  esp_fill_random(buf, len);
+  return 0;
+}
+
 extern int __real_mbedtls_ssl_config_defaults(
   mbedtls_ssl_config *conf,
   int endpoint,
@@ -71,6 +95,21 @@ int __wrap_mbedtls_ssl_config_defaults(
   int ret = __real_mbedtls_ssl_config_defaults(conf, endpoint, transport, preset);
   if (ret == 0 && endpoint == MBEDTLS_SSL_IS_SERVER) {
     mbedtls_ssl_conf_ciphersuites(conf, kServerOnlyCipherSuites);
+
+    if (!s_ticket_ctx_ready) {
+      mbedtls_ssl_ticket_init(&s_ticket_ctx);
+      int ticket_ret = mbedtls_ssl_ticket_setup(&s_ticket_ctx, esp_rng, NULL,
+                                                MBEDTLS_CIPHER_AES_256_GCM, 86400);
+      if (ticket_ret == 0) {
+        s_ticket_ctx_ready = 1;
+      }
+    }
+    if (s_ticket_ctx_ready) {
+      mbedtls_ssl_conf_session_tickets_cb(conf,
+        mbedtls_ssl_ticket_write,
+        mbedtls_ssl_ticket_parse,
+        &s_ticket_ctx);
+    }
   }
   return ret;
 }

+ 1 - 1
platformio.ini

@@ -68,7 +68,7 @@ build_flags = ${arduino_base.build_flags}
   -Wl,--wrap=mbedtls_ssl_config_defaults
 build_src_filter = ${arduino_base.build_src_filter}
   +<../arch/esp32/task_pinning.c>
-  +<../arch/esp32/tls_cipher_restrict.c>
+  +<../arch/esp32/tls_server_config.c>
   +<../arch/esp32/CPUUsageTracker.cpp>
 
 [esp32_ota]