2021-05-31 08:06:09 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2018-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2018-10-31 18:17:00 -04:00
|
|
|
|
|
|
|
#ifndef _ESP_HTTPS_SERVER_H_
|
|
|
|
#define _ESP_HTTPS_SERVER_H_
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include "esp_err.h"
|
|
|
|
#include "esp_http_server.h"
|
2021-10-08 04:49:57 -04:00
|
|
|
#include "esp_tls.h"
|
2018-10-31 18:17:00 -04:00
|
|
|
|
2018-12-31 00:52:42 -05:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2018-11-12 03:49:20 -05:00
|
|
|
typedef enum {
|
|
|
|
HTTPD_SSL_TRANSPORT_SECURE, // SSL Enabled
|
|
|
|
HTTPD_SSL_TRANSPORT_INSECURE // SSL disabled
|
|
|
|
} httpd_ssl_transport_mode_t;
|
|
|
|
|
2021-10-08 04:49:57 -04:00
|
|
|
/**
|
|
|
|
* @brief Callback data struct, contains the ESP-TLS connection handle
|
|
|
|
*/
|
|
|
|
typedef struct esp_https_server_user_cb_arg {
|
2022-05-11 03:52:09 -04:00
|
|
|
const esp_tls_t *tls; /*!< ESP-TLS connection handle */
|
2021-10-08 04:49:57 -04:00
|
|
|
} esp_https_server_user_cb_arg_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Callback function prototype
|
|
|
|
* Can be used to get connection or client information (SSL context)
|
|
|
|
* E.g. Client certificate, Socket FD, Connection state, etc.
|
|
|
|
*
|
|
|
|
* @param user_cb Callback data struct
|
|
|
|
*/
|
|
|
|
typedef void esp_https_server_user_cb(esp_https_server_user_cb_arg_t *user_cb);
|
|
|
|
|
2018-10-31 18:17:00 -04:00
|
|
|
/**
|
|
|
|
* HTTPS server config struct
|
|
|
|
*
|
|
|
|
* Please use HTTPD_SSL_CONFIG_DEFAULT() to initialize it.
|
|
|
|
*/
|
|
|
|
struct httpd_ssl_config {
|
|
|
|
/**
|
|
|
|
* Underlying HTTPD server config
|
|
|
|
*
|
|
|
|
* Parameters like task stack size and priority can be adjusted here.
|
|
|
|
*/
|
|
|
|
httpd_config_t httpd;
|
|
|
|
|
2020-02-17 12:29:31 -05:00
|
|
|
/** CA certificate (here it is treated as server cert)
|
|
|
|
* Todo: Fix this change in release/v5.0 as it would be a breaking change
|
|
|
|
* i.e. Rename the nomenclature of variables holding different certs in https_server component as well as example
|
|
|
|
* 1)The cacert variable should hold the CA which is used to authenticate clients (should inherit current role of client_verify_cert_pem var)
|
|
|
|
* 2)There should be another variable servercert which whould hold servers own certificate (should inherit current role of cacert var) */
|
2018-10-31 18:17:00 -04:00
|
|
|
const uint8_t *cacert_pem;
|
|
|
|
|
|
|
|
/** CA certificate byte length */
|
|
|
|
size_t cacert_len;
|
|
|
|
|
2020-02-17 12:29:31 -05:00
|
|
|
/** Client verify authority certificate (CA used to sign clients, or client cert itself */
|
|
|
|
const uint8_t *client_verify_cert_pem;
|
2019-12-03 06:22:06 -05:00
|
|
|
|
2020-02-17 12:29:31 -05:00
|
|
|
/** Client verify authority cert len */
|
|
|
|
size_t client_verify_cert_len;
|
2019-12-03 06:22:06 -05:00
|
|
|
|
2018-10-31 18:17:00 -04:00
|
|
|
/** Private key */
|
|
|
|
const uint8_t *prvtkey_pem;
|
|
|
|
|
|
|
|
/** Private key byte length */
|
|
|
|
size_t prvtkey_len;
|
|
|
|
|
2018-11-12 03:49:20 -05:00
|
|
|
/** Transport Mode (default secure) */
|
|
|
|
httpd_ssl_transport_mode_t transport_mode;
|
2018-10-31 18:17:00 -04:00
|
|
|
|
2018-11-12 03:49:20 -05:00
|
|
|
/** Port used when transport mode is secure (default 443) */
|
2018-10-31 18:17:00 -04:00
|
|
|
uint16_t port_secure;
|
|
|
|
|
2018-11-12 03:49:20 -05:00
|
|
|
/** Port used when transport mode is insecure (default 80) */
|
2018-10-31 18:17:00 -04:00
|
|
|
uint16_t port_insecure;
|
2021-05-19 11:16:59 -04:00
|
|
|
|
|
|
|
/** Enable tls session tickets */
|
|
|
|
bool session_tickets;
|
2021-10-08 04:49:57 -04:00
|
|
|
|
|
|
|
/** User callback for esp_https_server */
|
|
|
|
esp_https_server_user_cb *user_cb;
|
2018-10-31 18:17:00 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct httpd_ssl_config httpd_ssl_config_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default config struct init
|
|
|
|
*
|
|
|
|
* (http_server default config had to be copied for customization)
|
|
|
|
*
|
|
|
|
* Notes:
|
2018-11-12 03:49:20 -05:00
|
|
|
* - port is set when starting the server, according to 'transport_mode'
|
2018-10-31 18:17:00 -04:00
|
|
|
* - one socket uses ~ 40kB RAM with SSL, we reduce the default socket count to 4
|
|
|
|
* - SSL sockets are usually long-lived, closing LRU prevents pool exhaustion DOS
|
|
|
|
* - Stack size may need adjustments depending on the user application
|
|
|
|
*/
|
|
|
|
#define HTTPD_SSL_CONFIG_DEFAULT() { \
|
|
|
|
.httpd = { \
|
|
|
|
.task_priority = tskIDLE_PRIORITY+5, \
|
|
|
|
.stack_size = 10240, \
|
2019-03-15 11:45:12 -04:00
|
|
|
.core_id = tskNO_AFFINITY, \
|
2018-10-31 18:17:00 -04:00
|
|
|
.server_port = 0, \
|
|
|
|
.ctrl_port = 32768, \
|
|
|
|
.max_open_sockets = 4, \
|
|
|
|
.max_uri_handlers = 8, \
|
|
|
|
.max_resp_headers = 8, \
|
|
|
|
.backlog_conn = 5, \
|
|
|
|
.lru_purge_enable = true, \
|
|
|
|
.recv_wait_timeout = 5, \
|
|
|
|
.send_wait_timeout = 5, \
|
|
|
|
.global_user_ctx = NULL, \
|
|
|
|
.global_user_ctx_free_fn = NULL, \
|
|
|
|
.global_transport_ctx = NULL, \
|
|
|
|
.global_transport_ctx_free_fn = NULL, \
|
2022-12-05 04:19:58 -05:00
|
|
|
.enable_so_linger = false, \
|
|
|
|
.linger_timeout = 0, \
|
2018-10-31 18:17:00 -04:00
|
|
|
.open_fn = NULL, \
|
|
|
|
.close_fn = NULL, \
|
2019-02-01 08:13:22 -05:00
|
|
|
.uri_match_fn = NULL \
|
2018-10-31 18:17:00 -04:00
|
|
|
}, \
|
2018-12-31 00:52:42 -05:00
|
|
|
.cacert_pem = NULL, \
|
|
|
|
.cacert_len = 0, \
|
2020-02-17 12:29:31 -05:00
|
|
|
.client_verify_cert_pem = NULL, \
|
|
|
|
.client_verify_cert_len = 0, \
|
2020-03-27 11:12:57 -04:00
|
|
|
.prvtkey_pem = NULL, \
|
|
|
|
.prvtkey_len = 0, \
|
2018-11-12 03:49:20 -05:00
|
|
|
.transport_mode = HTTPD_SSL_TRANSPORT_SECURE, \
|
2018-10-31 18:17:00 -04:00
|
|
|
.port_secure = 443, \
|
|
|
|
.port_insecure = 80, \
|
2021-05-19 11:16:59 -04:00
|
|
|
.session_tickets = false, \
|
2021-10-08 04:49:57 -04:00
|
|
|
.user_cb = NULL, \
|
2018-10-31 18:17:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a SSL capable HTTP server (secure mode may be disabled in config)
|
|
|
|
*
|
|
|
|
* @param[in,out] config - server config, must not be const. Does not have to stay valid after
|
|
|
|
* calling this function.
|
|
|
|
* @param[out] handle - storage for the server handle, must be a valid pointer
|
|
|
|
* @return success
|
|
|
|
*/
|
|
|
|
esp_err_t httpd_ssl_start(httpd_handle_t *handle, httpd_ssl_config_t *config);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop the server. Blocks until the server is shut down.
|
|
|
|
*
|
|
|
|
* @param[in] handle
|
|
|
|
*/
|
|
|
|
void httpd_ssl_stop(httpd_handle_t handle);
|
|
|
|
|
2018-12-31 00:52:42 -05:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-10-31 18:17:00 -04:00
|
|
|
#endif // _ESP_HTTPS_SERVER_H_
|