esp_https_server.h 5.4 KB

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