esp_https_server.h 5.1 KB

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