esp_https_server.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * SPDX-FileCopyrightText: 2018-2023 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. /** Use ECDSA peripheral to use private key */
  68. bool use_ecdsa_peripheral;
  69. /** The efuse block where ECDSA key is stored */
  70. uint8_t ecdsa_key_efuse_blk;
  71. /** Transport Mode (default secure) */
  72. httpd_ssl_transport_mode_t transport_mode;
  73. /** Port used when transport mode is secure (default 443) */
  74. uint16_t port_secure;
  75. /** Port used when transport mode is insecure (default 80) */
  76. uint16_t port_insecure;
  77. /** Enable tls session tickets */
  78. bool session_tickets;
  79. /** Enable secure element for server session */
  80. bool use_secure_element;
  81. /** User callback for esp_https_server */
  82. esp_https_server_user_cb *user_cb;
  83. void *ssl_userdata; /*!< user data to add to the ssl context */
  84. #if CONFIG_ESP_TLS_SERVER_CERT_SELECT_HOOK
  85. esp_tls_handshake_callback cert_select_cb; /*!< Certificate selection callback to use */
  86. #endif
  87. const char** alpn_protos; /*!< Application protocols the server supports in order of prefernece. Used for negotiating during the TLS handshake, first one the client supports is selected. The data structure must live as long as the https server itself! */
  88. };
  89. typedef struct httpd_ssl_config httpd_ssl_config_t;
  90. /* Macro kept for compatibility reasons */
  91. #define HTTPD_SSL_CONFIG_DEFAULT httpd_ssl_config_default
  92. /**
  93. * Returns the httpd config struct with default initialisation
  94. *
  95. * @return
  96. * httpd_ssl_config_t HTTPD ssl config struct
  97. * with default initialisation
  98. * Notes:
  99. * - port is set when starting the server, according to 'transport_mode'
  100. * - one socket uses ~ 40kB RAM with SSL, we reduce the default socket count to 4
  101. * - SSL sockets are usually long-lived, closing LRU prevents pool exhaustion DOS
  102. * - Stack size may need adjustments depending on the user application
  103. */
  104. httpd_ssl_config_t httpd_ssl_config_default(void);
  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. * @return
  119. * - ESP_OK: Server stopped successfully
  120. * - ESP_ERR_INVALID_ARG: Invalid argument
  121. * - ESP_FAIL: Failure to shut down server
  122. */
  123. esp_err_t httpd_ssl_stop(httpd_handle_t handle);
  124. #ifdef __cplusplus
  125. }
  126. #endif
  127. #endif // _ESP_HTTPS_SERVER_H_