tls_server_config.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * arch/esp32/tls_server_config.c
  3. *
  4. * Configures the TLS server via a linker-wrap of mbedtls_ssl_config_defaults():
  5. *
  6. * 1. Restricts cipher suites to RSA key exchange, eliminating the ECDH
  7. * computation that starves IDLE0 and triggers the task watchdog during
  8. * HTTPS handshakes.
  9. *
  10. * 2. Enables TLS session tickets so that browsers can resume sessions
  11. * without a full RSA handshake.
  12. *
  13. * Background (cipher suite restriction):
  14. * Browsers negotiate ECDHE cipher suites by default. On ESP32-S3 the
  15. * hardware RSA accelerator handles RSA key exchange efficiently, but
  16. * there is no ECP hardware accelerator. ECDHE requires the server to
  17. * compute an ephemeral key pair: ecp_precompute_comb() builds a comb
  18. * table through many sequential ECP point doublings, each dispatched
  19. * to the hardware bignum unit (esp_bignum.c), but the ECP layer has
  20. * no RTOS yield points between iterations. The entire computation runs
  21. * to completion on CPU 0 without ever resetting the task watchdog.
  22. *
  23. * A single handshake does not exceed the watchdog timeout on its own,
  24. * but two consecutive handshakes (e.g. a browser retry after a failed
  25. * attempt) accumulate enough uninterrupted runtime to starve IDLE0:
  26. *
  27. * E (54924) esp-tls-mbedtls: mbedtls_ssl_handshake returned -0x0050
  28. * E (57208) esp-tls-mbedtls: mbedtls_ssl_handshake returned -0x7280
  29. * E (57638) task_wdt: Task watchdog got triggered.
  30. * E (57638) task_wdt: - IDLE0 (CPU 0)
  31. * E (57638) task_wdt: Tasks currently running:
  32. * E (57638) task_wdt: CPU 0: httpd
  33. *
  34. * The crash occurs in ecp_precompute_comb() → ecp_double_jac() →
  35. * mbedtls_mpi_mul_mpi() during the ServerKeyExchange step.
  36. *
  37. * There is no sdkconfig knob accessible at runtime through
  38. * esp_https_server. The user_cb hook fires after the handshake, too
  39. * late to change cipher suites. The only pre-handshake intercept point
  40. * is mbedtls_ssl_config_defaults(), called once per ssl_config init.
  41. *
  42. * MQTT uses MBEDTLS_SSL_IS_CLIENT; the HTTPS server uses
  43. * MBEDTLS_SSL_IS_SERVER — this is the discriminator.
  44. *
  45. * Note: this file is compiled only for [esp32_base] targets (IDF v4).
  46. * CONFIG_MBEDTLS_SSL_SESSION_TICKETS is enabled by default in the
  47. * arduino-esp32 2.x pre-built framework, so no sdkconfig changes are needed.
  48. */
  49. #include "mbedtls/ssl.h"
  50. #include "mbedtls/ssl_ticket.h"
  51. #include "esp_random.h"
  52. /*
  53. * RSA key exchange cipher suites only.
  54. * Must be static — mbedTLS stores the pointer, does not copy the array.
  55. * Terminated with 0.
  56. */
  57. static const int kServerOnlyCipherSuites[] = {
  58. MBEDTLS_TLS_RSA_WITH_AES_128_GCM_SHA256,
  59. MBEDTLS_TLS_RSA_WITH_AES_256_GCM_SHA384,
  60. MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA256,
  61. MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA256,
  62. 0
  63. };
  64. /*
  65. * Session ticket context. Must be static — mbedTLS stores the pointer for
  66. * the lifetime of the server, analogous to kServerOnlyCipherSuites.
  67. * Initialized once on first server start.
  68. */
  69. static mbedtls_ssl_ticket_context s_ticket_ctx;
  70. static int s_ticket_ctx_ready = 0;
  71. /* Thin wrapper around the ESP32 hardware RNG for mbedtls_ssl_ticket_setup(). */
  72. static int esp_rng(void *ctx, unsigned char *buf, size_t len) {
  73. (void)ctx;
  74. esp_fill_random(buf, len);
  75. return 0;
  76. }
  77. extern int __real_mbedtls_ssl_config_defaults(
  78. mbedtls_ssl_config *conf,
  79. int endpoint,
  80. int transport,
  81. int preset
  82. );
  83. int __wrap_mbedtls_ssl_config_defaults(
  84. mbedtls_ssl_config *conf,
  85. int endpoint,
  86. int transport,
  87. int preset
  88. ) {
  89. int ret = __real_mbedtls_ssl_config_defaults(conf, endpoint, transport, preset);
  90. if (ret == 0 && endpoint == MBEDTLS_SSL_IS_SERVER) {
  91. mbedtls_ssl_conf_ciphersuites(conf, kServerOnlyCipherSuites);
  92. if (!s_ticket_ctx_ready) {
  93. mbedtls_ssl_ticket_init(&s_ticket_ctx);
  94. int ticket_ret = mbedtls_ssl_ticket_setup(&s_ticket_ctx, esp_rng, NULL,
  95. MBEDTLS_CIPHER_AES_256_GCM, 86400);
  96. if (ticket_ret == 0) {
  97. s_ticket_ctx_ready = 1;
  98. }
  99. }
  100. if (s_ticket_ctx_ready) {
  101. mbedtls_ssl_conf_session_tickets_cb(conf,
  102. mbedtls_ssl_ticket_write,
  103. mbedtls_ssl_ticket_parse,
  104. &s_ticket_ctx);
  105. }
  106. }
  107. return ret;
  108. }