esp_https_server.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. };
  82. typedef struct httpd_ssl_config httpd_ssl_config_t;
  83. /**
  84. * Default config struct init
  85. *
  86. * (http_server default config had to be copied for customization)
  87. *
  88. * Notes:
  89. * - port is set when starting the server, according to 'transport_mode'
  90. * - one socket uses ~ 40kB RAM with SSL, we reduce the default socket count to 4
  91. * - SSL sockets are usually long-lived, closing LRU prevents pool exhaustion DOS
  92. * - Stack size may need adjustments depending on the user application
  93. */
  94. #define HTTPD_SSL_CONFIG_DEFAULT() { \
  95. .httpd = { \
  96. .task_priority = tskIDLE_PRIORITY+5, \
  97. .stack_size = 10240, \
  98. .core_id = tskNO_AFFINITY, \
  99. .server_port = 0, \
  100. .ctrl_port = 32768, \
  101. .max_open_sockets = 4, \
  102. .max_uri_handlers = 8, \
  103. .max_resp_headers = 8, \
  104. .backlog_conn = 5, \
  105. .lru_purge_enable = true, \
  106. .recv_wait_timeout = 5, \
  107. .send_wait_timeout = 5, \
  108. .global_user_ctx = NULL, \
  109. .global_user_ctx_free_fn = NULL, \
  110. .global_transport_ctx = NULL, \
  111. .global_transport_ctx_free_fn = NULL, \
  112. .open_fn = NULL, \
  113. .close_fn = NULL, \
  114. .uri_match_fn = NULL \
  115. }, \
  116. .servercert = NULL, \
  117. .servercert_len = 0, \
  118. .cacert_pem = NULL, \
  119. .cacert_len = 0, \
  120. .prvtkey_pem = NULL, \
  121. .prvtkey_len = 0, \
  122. .transport_mode = HTTPD_SSL_TRANSPORT_SECURE, \
  123. .port_secure = 443, \
  124. .port_insecure = 80, \
  125. .session_tickets = false, \
  126. .use_secure_element = false, \
  127. .user_cb = NULL, \
  128. .ssl_userdata = NULL, \
  129. .cert_select_cb = NULL \
  130. }
  131. /**
  132. * Create a SSL capable HTTP server (secure mode may be disabled in config)
  133. *
  134. * @param[in,out] config - server config, must not be const. Does not have to stay valid after
  135. * calling this function.
  136. * @param[out] handle - storage for the server handle, must be a valid pointer
  137. * @return success
  138. */
  139. esp_err_t httpd_ssl_start(httpd_handle_t *handle, httpd_ssl_config_t *config);
  140. /**
  141. * Stop the server. Blocks until the server is shut down.
  142. *
  143. * @param[in] handle
  144. * @return
  145. * - ESP_OK: Server stopped successfully
  146. * - ESP_ERR_INVALID_ARG: Invalid argument
  147. * - ESP_FAIL: Failure to shut down server
  148. */
  149. esp_err_t httpd_ssl_stop(httpd_handle_t handle);
  150. #ifdef __cplusplus
  151. }
  152. #endif
  153. #endif // _ESP_HTTPS_SERVER_H_