esp_https_server.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. typedef enum {
  15. HTTPD_SSL_TRANSPORT_SECURE, // SSL Enabled
  16. HTTPD_SSL_TRANSPORT_INSECURE // SSL disabled
  17. } httpd_ssl_transport_mode_t;
  18. /**
  19. * HTTPS server config struct
  20. *
  21. * Please use HTTPD_SSL_CONFIG_DEFAULT() to initialize it.
  22. */
  23. struct httpd_ssl_config {
  24. /**
  25. * Underlying HTTPD server config
  26. *
  27. * Parameters like task stack size and priority can be adjusted here.
  28. */
  29. httpd_config_t httpd;
  30. /** CA certificate (here it is treated as server cert)
  31. * Todo: Fix this change in release/v5.0 as it would be a breaking change
  32. * i.e. Rename the nomenclature of variables holding different certs in https_server component as well as example
  33. * 1)The cacert variable should hold the CA which is used to authenticate clients (should inherit current role of client_verify_cert_pem var)
  34. * 2)There should be another variable servercert which whould hold servers own certificate (should inherit current role of cacert var) */
  35. const uint8_t *cacert_pem;
  36. /** CA certificate byte length */
  37. size_t cacert_len;
  38. /** Client verify authority certificate (CA used to sign clients, or client cert itself */
  39. const uint8_t *client_verify_cert_pem;
  40. /** Client verify authority cert len */
  41. size_t client_verify_cert_len;
  42. /** Private key */
  43. const uint8_t *prvtkey_pem;
  44. /** Private key byte length */
  45. size_t prvtkey_len;
  46. /** Transport Mode (default secure) */
  47. httpd_ssl_transport_mode_t transport_mode;
  48. /** Port used when transport mode is secure (default 443) */
  49. uint16_t port_secure;
  50. /** Port used when transport mode is insecure (default 80) */
  51. uint16_t port_insecure;
  52. /** Enable tls session tickets */
  53. bool session_tickets;
  54. };
  55. typedef struct httpd_ssl_config httpd_ssl_config_t;
  56. /**
  57. * Default config struct init
  58. *
  59. * (http_server default config had to be copied for customization)
  60. *
  61. * Notes:
  62. * - port is set when starting the server, according to 'transport_mode'
  63. * - one socket uses ~ 40kB RAM with SSL, we reduce the default socket count to 4
  64. * - SSL sockets are usually long-lived, closing LRU prevents pool exhaustion DOS
  65. * - Stack size may need adjustments depending on the user application
  66. */
  67. #define HTTPD_SSL_CONFIG_DEFAULT() { \
  68. .httpd = { \
  69. .task_priority = tskIDLE_PRIORITY+5, \
  70. .stack_size = 10240, \
  71. .core_id = tskNO_AFFINITY, \
  72. .server_port = 0, \
  73. .ctrl_port = 32768, \
  74. .max_open_sockets = 4, \
  75. .max_uri_handlers = 8, \
  76. .max_resp_headers = 8, \
  77. .backlog_conn = 5, \
  78. .lru_purge_enable = true, \
  79. .recv_wait_timeout = 5, \
  80. .send_wait_timeout = 5, \
  81. .global_user_ctx = NULL, \
  82. .global_user_ctx_free_fn = NULL, \
  83. .global_transport_ctx = NULL, \
  84. .global_transport_ctx_free_fn = NULL, \
  85. .open_fn = NULL, \
  86. .close_fn = NULL, \
  87. .uri_match_fn = NULL \
  88. }, \
  89. .cacert_pem = NULL, \
  90. .cacert_len = 0, \
  91. .client_verify_cert_pem = NULL, \
  92. .client_verify_cert_len = 0, \
  93. .prvtkey_pem = NULL, \
  94. .prvtkey_len = 0, \
  95. .transport_mode = HTTPD_SSL_TRANSPORT_SECURE, \
  96. .port_secure = 443, \
  97. .port_insecure = 80, \
  98. .session_tickets = false, \
  99. }
  100. /**
  101. * Create a SSL capable HTTP server (secure mode may be disabled in config)
  102. *
  103. * @param[in,out] config - server config, must not be const. Does not have to stay valid after
  104. * calling this function.
  105. * @param[out] handle - storage for the server handle, must be a valid pointer
  106. * @return success
  107. */
  108. esp_err_t httpd_ssl_start(httpd_handle_t *handle, httpd_ssl_config_t *config);
  109. /**
  110. * Stop the server. Blocks until the server is shut down.
  111. *
  112. * @param[in] handle
  113. */
  114. void httpd_ssl_stop(httpd_handle_t handle);
  115. #ifdef __cplusplus
  116. }
  117. #endif
  118. #endif // _ESP_HTTPS_SERVER_H_