esp_https_server.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 Indicates the state at which the user callback is executed,
  21. * i.e at session creation or session close
  22. */
  23. typedef enum {
  24. HTTPD_SSL_USER_CB_SESS_CREATE,
  25. HTTPD_SSL_USER_CB_SESS_CLOSE
  26. } httpd_ssl_user_cb_state_t;
  27. /**
  28. * @brief Callback data struct, contains the ESP-TLS connection handle
  29. * and the connection state at which the callback is executed
  30. */
  31. typedef struct esp_https_server_user_cb_arg {
  32. httpd_ssl_user_cb_state_t user_cb_state; /*!< State of user callback */
  33. esp_tls_t *tls; /*!< ESP-TLS connection handle */
  34. } esp_https_server_user_cb_arg_t;
  35. /**
  36. * @brief Callback function prototype
  37. * Can be used to get connection or client information (SSL context)
  38. * E.g. Client certificate, Socket FD, Connection state, etc.
  39. *
  40. * @param user_cb Callback data struct
  41. */
  42. typedef void esp_https_server_user_cb(esp_https_server_user_cb_arg_t *user_cb);
  43. /**
  44. * HTTPS server config struct
  45. *
  46. * Please use HTTPD_SSL_CONFIG_DEFAULT() to initialize it.
  47. */
  48. struct httpd_ssl_config {
  49. /**
  50. * Underlying HTTPD server config
  51. *
  52. * Parameters like task stack size and priority can be adjusted here.
  53. */
  54. httpd_config_t httpd;
  55. /** Server certificate */
  56. const uint8_t *servercert;
  57. /** Server certificate byte length */
  58. size_t servercert_len;
  59. /** CA certificate ((CA used to sign clients, or client cert itself) */
  60. const uint8_t *cacert_pem;
  61. /** CA certificate byte length */
  62. size_t cacert_len;
  63. /** Private key */
  64. const uint8_t *prvtkey_pem;
  65. /** Private key byte length */
  66. size_t prvtkey_len;
  67. /** Transport Mode (default secure) */
  68. httpd_ssl_transport_mode_t transport_mode;
  69. /** Port used when transport mode is secure (default 443) */
  70. uint16_t port_secure;
  71. /** Port used when transport mode is insecure (default 80) */
  72. uint16_t port_insecure;
  73. /** Enable tls session tickets */
  74. bool session_tickets;
  75. /** Enable secure element for server session */
  76. bool use_secure_element;
  77. /** User callback for esp_https_server */
  78. esp_https_server_user_cb *user_cb;
  79. void *ssl_userdata; /*!< user data to add to the ssl context */
  80. esp_tls_handshake_callback cert_select_cb; /*!< Certificate selection callback to use */
  81. const char** alpn_protos; /*!< Application protocols the server supports in order of prefernece. Used for negotiating during the TLS handshake, first one the client supports is selected. The data structure must live as long as the https server itself! */
  82. };
  83. typedef struct httpd_ssl_config httpd_ssl_config_t;
  84. /**
  85. * Default config struct init
  86. *
  87. * (http_server default config had to be copied for customization)
  88. *
  89. * Notes:
  90. * - port is set when starting the server, according to 'transport_mode'
  91. * - one socket uses ~ 40kB RAM with SSL, we reduce the default socket count to 4
  92. * - SSL sockets are usually long-lived, closing LRU prevents pool exhaustion DOS
  93. * - Stack size may need adjustments depending on the user application
  94. */
  95. #define HTTPD_SSL_CONFIG_DEFAULT() { \
  96. .httpd = { \
  97. .task_priority = tskIDLE_PRIORITY+5, \
  98. .stack_size = 10240, \
  99. .core_id = tskNO_AFFINITY, \
  100. .server_port = 0, \
  101. .ctrl_port = ESP_HTTPD_DEF_CTRL_PORT+1, \
  102. .max_open_sockets = 4, \
  103. .max_uri_handlers = 8, \
  104. .max_resp_headers = 8, \
  105. .backlog_conn = 5, \
  106. .lru_purge_enable = true, \
  107. .recv_wait_timeout = 5, \
  108. .send_wait_timeout = 5, \
  109. .global_user_ctx = NULL, \
  110. .global_user_ctx_free_fn = NULL, \
  111. .global_transport_ctx = NULL, \
  112. .global_transport_ctx_free_fn = NULL, \
  113. .enable_so_linger = false, \
  114. .linger_timeout = 0, \
  115. .keep_alive_enable = false, \
  116. .keep_alive_idle = 0, \
  117. .keep_alive_interval = 0, \
  118. .keep_alive_count = 0, \
  119. .open_fn = NULL, \
  120. .close_fn = NULL, \
  121. .uri_match_fn = NULL \
  122. }, \
  123. .servercert = NULL, \
  124. .servercert_len = 0, \
  125. .cacert_pem = NULL, \
  126. .cacert_len = 0, \
  127. .prvtkey_pem = NULL, \
  128. .prvtkey_len = 0, \
  129. .transport_mode = HTTPD_SSL_TRANSPORT_SECURE, \
  130. .port_secure = 443, \
  131. .port_insecure = 80, \
  132. .session_tickets = false, \
  133. .use_secure_element = false, \
  134. .user_cb = NULL, \
  135. .ssl_userdata = NULL, \
  136. .cert_select_cb = NULL, \
  137. .alpn_protos = NULL, \
  138. }
  139. /**
  140. * Create a SSL capable HTTP server (secure mode may be disabled in config)
  141. *
  142. * @param[in,out] config - server config, must not be const. Does not have to stay valid after
  143. * calling this function.
  144. * @param[out] handle - storage for the server handle, must be a valid pointer
  145. * @return success
  146. */
  147. esp_err_t httpd_ssl_start(httpd_handle_t *handle, httpd_ssl_config_t *config);
  148. /**
  149. * Stop the server. Blocks until the server is shut down.
  150. *
  151. * @param[in] handle
  152. * @return
  153. * - ESP_OK: Server stopped successfully
  154. * - ESP_ERR_INVALID_ARG: Invalid argument
  155. * - ESP_FAIL: Failure to shut down server
  156. */
  157. esp_err_t httpd_ssl_stop(httpd_handle_t handle);
  158. #ifdef __cplusplus
  159. }
  160. #endif
  161. #endif // _ESP_HTTPS_SERVER_H_