esp_https_server.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. /** CA certificate (here it is treated as server cert)
  46. * Todo: Fix this change in release/v5.0 as it would be a breaking change
  47. * i.e. Rename the nomenclature of variables holding different certs in https_server component as well as example
  48. * 1)The cacert variable should hold the CA which is used to authenticate clients (should inherit current role of client_verify_cert_pem var)
  49. * 2)There should be another variable servercert which whould hold servers own certificate (should inherit current role of cacert var) */
  50. const uint8_t *cacert_pem;
  51. /** CA certificate byte length */
  52. size_t cacert_len;
  53. /** Client verify authority certificate (CA used to sign clients, or client cert itself */
  54. const uint8_t *client_verify_cert_pem;
  55. /** Client verify authority cert len */
  56. size_t client_verify_cert_len;
  57. /** Private key */
  58. const uint8_t *prvtkey_pem;
  59. /** Private key byte length */
  60. size_t prvtkey_len;
  61. /** Transport Mode (default secure) */
  62. httpd_ssl_transport_mode_t transport_mode;
  63. /** Port used when transport mode is secure (default 443) */
  64. uint16_t port_secure;
  65. /** Port used when transport mode is insecure (default 80) */
  66. uint16_t port_insecure;
  67. /** Enable tls session tickets */
  68. bool session_tickets;
  69. /** User callback for esp_https_server */
  70. esp_https_server_user_cb *user_cb;
  71. };
  72. typedef struct httpd_ssl_config httpd_ssl_config_t;
  73. /**
  74. * Default config struct init
  75. *
  76. * (http_server default config had to be copied for customization)
  77. *
  78. * Notes:
  79. * - port is set when starting the server, according to 'transport_mode'
  80. * - one socket uses ~ 40kB RAM with SSL, we reduce the default socket count to 4
  81. * - SSL sockets are usually long-lived, closing LRU prevents pool exhaustion DOS
  82. * - Stack size may need adjustments depending on the user application
  83. */
  84. #define HTTPD_SSL_CONFIG_DEFAULT() { \
  85. .httpd = { \
  86. .task_priority = tskIDLE_PRIORITY+5, \
  87. .stack_size = 10240, \
  88. .core_id = tskNO_AFFINITY, \
  89. .server_port = 0, \
  90. .ctrl_port = 32768, \
  91. .max_open_sockets = 4, \
  92. .max_uri_handlers = 8, \
  93. .max_resp_headers = 8, \
  94. .backlog_conn = 5, \
  95. .lru_purge_enable = true, \
  96. .recv_wait_timeout = 5, \
  97. .send_wait_timeout = 5, \
  98. .global_user_ctx = NULL, \
  99. .global_user_ctx_free_fn = NULL, \
  100. .global_transport_ctx = NULL, \
  101. .global_transport_ctx_free_fn = NULL, \
  102. .open_fn = NULL, \
  103. .close_fn = NULL, \
  104. .uri_match_fn = NULL \
  105. }, \
  106. .cacert_pem = NULL, \
  107. .cacert_len = 0, \
  108. .client_verify_cert_pem = NULL, \
  109. .client_verify_cert_len = 0, \
  110. .prvtkey_pem = NULL, \
  111. .prvtkey_len = 0, \
  112. .transport_mode = HTTPD_SSL_TRANSPORT_SECURE, \
  113. .port_secure = 443, \
  114. .port_insecure = 80, \
  115. .session_tickets = false, \
  116. .user_cb = NULL, \
  117. }
  118. /**
  119. * Create a SSL capable HTTP server (secure mode may be disabled in config)
  120. *
  121. * @param[in,out] config - server config, must not be const. Does not have to stay valid after
  122. * calling this function.
  123. * @param[out] handle - storage for the server handle, must be a valid pointer
  124. * @return success
  125. */
  126. esp_err_t httpd_ssl_start(httpd_handle_t *handle, httpd_ssl_config_t *config);
  127. /**
  128. * Stop the server. Blocks until the server is shut down.
  129. *
  130. * @param[in] handle
  131. */
  132. void httpd_ssl_stop(httpd_handle_t handle);
  133. #ifdef __cplusplus
  134. }
  135. #endif
  136. #endif // _ESP_HTTPS_SERVER_H_