esp_https_server.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * SPDX-FileCopyrightText: 2018-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef _ESP_HTTPS_SERVER_H_
  7. #define _ESP_HTTPS_SERVER_H_
  8. #include <stdbool.h>
  9. #include "esp_err.h"
  10. #include "esp_http_server.h"
  11. #include "esp_tls.h"
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. typedef enum {
  16. HTTPD_SSL_TRANSPORT_SECURE, // SSL Enabled
  17. HTTPD_SSL_TRANSPORT_INSECURE // SSL disabled
  18. } httpd_ssl_transport_mode_t;
  19. /**
  20. * @brief Callback data struct, contains the ESP-TLS connection handle
  21. */
  22. typedef struct esp_https_server_user_cb_arg {
  23. const esp_tls_t *tls;
  24. } esp_https_server_user_cb_arg_t;
  25. /**
  26. * @brief Callback function prototype
  27. * Can be used to get connection or client information (SSL context)
  28. * E.g. Client certificate, Socket FD, Connection state, etc.
  29. *
  30. * @param user_cb Callback data struct
  31. */
  32. typedef void esp_https_server_user_cb(esp_https_server_user_cb_arg_t *user_cb);
  33. /**
  34. * HTTPS server config struct
  35. *
  36. * Please use HTTPD_SSL_CONFIG_DEFAULT() to initialize it.
  37. */
  38. struct httpd_ssl_config {
  39. /**
  40. * Underlying HTTPD server config
  41. *
  42. * Parameters like task stack size and priority can be adjusted here.
  43. */
  44. httpd_config_t httpd;
  45. /** Server certificate */
  46. const uint8_t *servercert;
  47. /** Server certificate byte length */
  48. size_t servercert_len;
  49. /** CA certificate ((CA used to sign clients, or client cert itself) */
  50. const uint8_t *cacert_pem;
  51. /** CA certificate byte length */
  52. size_t cacert_len;
  53. /** Private key */
  54. const uint8_t *prvtkey_pem;
  55. /** Private key byte length */
  56. size_t prvtkey_len;
  57. /** Transport Mode (default secure) */
  58. httpd_ssl_transport_mode_t transport_mode;
  59. /** Port used when transport mode is secure (default 443) */
  60. uint16_t port_secure;
  61. /** Port used when transport mode is insecure (default 80) */
  62. uint16_t port_insecure;
  63. /** Enable tls session tickets */
  64. bool session_tickets;
  65. /** User callback for esp_https_server */
  66. esp_https_server_user_cb *user_cb;
  67. };
  68. typedef struct httpd_ssl_config httpd_ssl_config_t;
  69. /**
  70. * Default config struct init
  71. *
  72. * (http_server default config had to be copied for customization)
  73. *
  74. * Notes:
  75. * - port is set when starting the server, according to 'transport_mode'
  76. * - one socket uses ~ 40kB RAM with SSL, we reduce the default socket count to 4
  77. * - SSL sockets are usually long-lived, closing LRU prevents pool exhaustion DOS
  78. * - Stack size may need adjustments depending on the user application
  79. */
  80. #define HTTPD_SSL_CONFIG_DEFAULT() { \
  81. .httpd = { \
  82. .task_priority = tskIDLE_PRIORITY+5, \
  83. .stack_size = 10240, \
  84. .core_id = tskNO_AFFINITY, \
  85. .server_port = 0, \
  86. .ctrl_port = 32768, \
  87. .max_open_sockets = 4, \
  88. .max_uri_handlers = 8, \
  89. .max_resp_headers = 8, \
  90. .backlog_conn = 5, \
  91. .lru_purge_enable = true, \
  92. .recv_wait_timeout = 5, \
  93. .send_wait_timeout = 5, \
  94. .global_user_ctx = NULL, \
  95. .global_user_ctx_free_fn = NULL, \
  96. .global_transport_ctx = NULL, \
  97. .global_transport_ctx_free_fn = NULL, \
  98. .open_fn = NULL, \
  99. .close_fn = NULL, \
  100. .uri_match_fn = NULL \
  101. }, \
  102. .servercert = NULL, \
  103. .servercert_len = 0, \
  104. .cacert_pem = NULL, \
  105. .cacert_len = 0, \
  106. .prvtkey_pem = NULL, \
  107. .prvtkey_len = 0, \
  108. .transport_mode = HTTPD_SSL_TRANSPORT_SECURE, \
  109. .port_secure = 443, \
  110. .port_insecure = 80, \
  111. .session_tickets = false, \
  112. .user_cb = NULL, \
  113. }
  114. /**
  115. * Create a SSL capable HTTP server (secure mode may be disabled in config)
  116. *
  117. * @param[in,out] config - server config, must not be const. Does not have to stay valid after
  118. * calling this function.
  119. * @param[out] handle - storage for the server handle, must be a valid pointer
  120. * @return success
  121. */
  122. esp_err_t httpd_ssl_start(httpd_handle_t *handle, httpd_ssl_config_t *config);
  123. /**
  124. * Stop the server. Blocks until the server is shut down.
  125. *
  126. * @param[in] handle
  127. */
  128. void httpd_ssl_stop(httpd_handle_t handle);
  129. #ifdef __cplusplus
  130. }
  131. #endif
  132. #endif // _ESP_HTTPS_SERVER_H_