esp_https_server.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. };
  53. typedef struct httpd_ssl_config httpd_ssl_config_t;
  54. /**
  55. * Default config struct init
  56. *
  57. * (http_server default config had to be copied for customization)
  58. *
  59. * Notes:
  60. * - port is set when starting the server, according to 'transport_mode'
  61. * - one socket uses ~ 40kB RAM with SSL, we reduce the default socket count to 4
  62. * - SSL sockets are usually long-lived, closing LRU prevents pool exhaustion DOS
  63. * - Stack size may need adjustments depending on the user application
  64. */
  65. #define HTTPD_SSL_CONFIG_DEFAULT() { \
  66. .httpd = { \
  67. .task_priority = tskIDLE_PRIORITY+5, \
  68. .stack_size = 10240, \
  69. .core_id = tskNO_AFFINITY, \
  70. .server_port = 0, \
  71. .ctrl_port = 32768, \
  72. .max_open_sockets = 4, \
  73. .max_uri_handlers = 8, \
  74. .max_resp_headers = 8, \
  75. .backlog_conn = 5, \
  76. .lru_purge_enable = true, \
  77. .recv_wait_timeout = 5, \
  78. .send_wait_timeout = 5, \
  79. .global_user_ctx = NULL, \
  80. .global_user_ctx_free_fn = NULL, \
  81. .global_transport_ctx = NULL, \
  82. .global_transport_ctx_free_fn = NULL, \
  83. .open_fn = NULL, \
  84. .close_fn = NULL, \
  85. .uri_match_fn = NULL \
  86. }, \
  87. .cacert_pem = NULL, \
  88. .cacert_len = 0, \
  89. .client_verify_cert_pem = NULL, \
  90. .client_verify_cert_len = 0, \
  91. .prvtkey_pem = NULL, \
  92. .prvtkey_len = 0, \
  93. .transport_mode = HTTPD_SSL_TRANSPORT_SECURE, \
  94. .port_secure = 443, \
  95. .port_insecure = 80, \
  96. }
  97. /**
  98. * Create a SSL capable HTTP server (secure mode may be disabled in config)
  99. *
  100. * @param[in,out] config - server config, must not be const. Does not have to stay valid after
  101. * calling this function.
  102. * @param[out] handle - storage for the server handle, must be a valid pointer
  103. * @return success
  104. */
  105. esp_err_t httpd_ssl_start(httpd_handle_t *handle, httpd_ssl_config_t *config);
  106. /**
  107. * Stop the server. Blocks until the server is shut down.
  108. *
  109. * @param[in] handle
  110. */
  111. void httpd_ssl_stop(httpd_handle_t handle);
  112. #ifdef __cplusplus
  113. }
  114. #endif
  115. #endif // _ESP_HTTPS_SERVER_H_