https_request_example:

Use local python server to test client session tickets
Update example test to test the client session tickets
Check for minimum heap info in the example test.
This commit is contained in:
Aditya Patwardhan
2021-11-12 15:48:53 +05:30
committed by bot
parent 4949f6b720
commit 36bb817b80
8 changed files with 329 additions and 54 deletions

View File

@@ -53,16 +53,25 @@
#define WEB_PORT "443"
#define WEB_URL "https://www.howsmyssl.com/a/check"
#define SERVER_URL_MAX_SZ 256
static const char *TAG = "example";
/* Timer interval once every day (24 Hours) */
#define TIME_PERIOD (86400000000ULL)
static const char REQUEST[] = "GET " WEB_URL " HTTP/1.1\r\n"
static const char HOWSMYSSL_REQUEST[] = "GET " WEB_URL " HTTP/1.1\r\n"
"Host: "WEB_SERVER"\r\n"
"User-Agent: esp-idf/1.0 esp32\r\n"
"\r\n";
#ifdef CONFIG_EXAMPLE_CLIENT_SESSION_TICKETS
static const char LOCAL_SRV_REQUEST[] = "GET " CONFIG_EXAMPLE_LOCAL_SERVER_URL " HTTP/1.1\r\n"
"Host: "WEB_SERVER"\r\n"
"User-Agent: esp-idf/1.0 esp32\r\n"
"\r\n";
#endif
/* Root cert for howsmyssl.com, taken from server_root_cert.pem
The PEM file was extracted from the output of this command:
@@ -75,15 +84,21 @@ static const char REQUEST[] = "GET " WEB_URL " HTTP/1.1\r\n"
*/
extern const uint8_t server_root_cert_pem_start[] asm("_binary_server_root_cert_pem_start");
extern const uint8_t server_root_cert_pem_end[] asm("_binary_server_root_cert_pem_end");
#ifdef CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS
extern const uint8_t local_server_cert_pem_start[] asm("_binary_local_server_cert_pem_start");
extern const uint8_t local_server_cert_pem_end[] asm("_binary_local_server_cert_pem_end");
#ifdef CONFIG_EXAMPLE_CLIENT_SESSION_TICKETS
esp_tls_client_session_t *tls_client_session = NULL;
static bool save_client_session = false;
#endif
static void https_get_request(esp_tls_cfg_t cfg)
static void https_get_request(esp_tls_cfg_t cfg, const char *WEB_SERVER_URL, const char *REQUEST)
{
char buf[512];
int ret, len;
struct esp_tls *tls = esp_tls_conn_http_new(WEB_URL, &cfg);
struct esp_tls *tls = esp_tls_conn_http_new(WEB_SERVER_URL, &cfg);
if (tls != NULL) {
ESP_LOGI(TAG, "Connection established...");
@@ -92,9 +107,10 @@ static void https_get_request(esp_tls_cfg_t cfg)
goto exit;
}
#ifdef CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS
#ifdef CONFIG_EXAMPLE_CLIENT_SESSION_TICKETS
/* The TLS session is successfully established, now saving the session ctx for reuse */
if (tls_client_session == NULL) {
if (save_client_session) {
free(tls_client_session);
tls_client_session = esp_tls_get_client_session(tls);
}
#endif
@@ -102,7 +118,7 @@ static void https_get_request(esp_tls_cfg_t cfg)
do {
ret = esp_tls_conn_write(tls,
REQUEST + written_bytes,
sizeof(REQUEST) - written_bytes);
strlen(REQUEST) - written_bytes);
if (ret >= 0) {
ESP_LOGI(TAG, "%d bytes written", ret);
written_bytes += ret;
@@ -110,7 +126,7 @@ static void https_get_request(esp_tls_cfg_t cfg)
ESP_LOGE(TAG, "esp_tls_conn_write returned: [0x%02X](%s)", ret, esp_err_to_name(ret));
goto exit;
}
} while (written_bytes < sizeof(REQUEST));
} while (written_bytes < strlen(REQUEST));
ESP_LOGI(TAG, "Reading HTTP response...");
@@ -156,7 +172,7 @@ static void https_get_request_using_crt_bundle(void)
esp_tls_cfg_t cfg = {
.crt_bundle_attach = esp_crt_bundle_attach,
};
https_get_request(cfg);
https_get_request(cfg, WEB_URL, HOWSMYSSL_REQUEST);
}
@@ -168,7 +184,7 @@ static void https_get_request_using_cacert_buf(void)
.cacert_buf = (const unsigned char *) server_root_cert_pem_start,
.cacert_bytes = server_root_cert_pem_end - server_root_cert_pem_start,
};
https_get_request(cfg);
https_get_request(cfg, WEB_URL, HOWSMYSSL_REQUEST);
}
static void https_get_request_using_global_ca_store(void)
@@ -183,19 +199,32 @@ static void https_get_request_using_global_ca_store(void)
esp_tls_cfg_t cfg = {
.use_global_ca_store = true,
};
https_get_request(cfg);
https_get_request(cfg, WEB_URL, HOWSMYSSL_REQUEST);
esp_tls_free_global_ca_store();
}
#ifdef CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS
static void https_get_request_using_already_saved_session(void)
#ifdef CONFIG_EXAMPLE_CLIENT_SESSION_TICKETS
static void https_get_request_to_local_server(const char* url)
{
ESP_LOGI(TAG, "https_request to local server");
esp_tls_cfg_t cfg = {
.cacert_buf = (const unsigned char *) local_server_cert_pem_start,
.cacert_bytes = local_server_cert_pem_end - local_server_cert_pem_start,
.skip_common_name = true,
};
save_client_session = true;
https_get_request(cfg, url, LOCAL_SRV_REQUEST);
}
static void https_get_request_using_already_saved_session(const char *url)
{
ESP_LOGI(TAG, "https_request using saved client session");
esp_tls_cfg_t cfg = {
.client_session = tls_client_session,
};
https_get_request(cfg);
https_get_request(cfg, url, LOCAL_SRV_REQUEST);
free(tls_client_session);
save_client_session = false;
tls_client_session = NULL;
}
#endif
@@ -204,12 +233,32 @@ static void https_request_task(void *pvparameters)
{
ESP_LOGI(TAG, "Start https_request example");
#ifdef CONFIG_EXAMPLE_CLIENT_SESSION_TICKETS
char *server_url = NULL;
#ifdef CONFIG_EXAMPLE_LOCAL_SERVER_URL_FROM_STDIN
char url_buf[SERVER_URL_MAX_SZ];
if (strcmp(CONFIG_EXAMPLE_LOCAL_SERVER_URL, "FROM_STDIN") == 0) {
example_configure_stdin_stdout();
fgets(url_buf, SERVER_URL_MAX_SZ, stdin);
int len = strlen(url_buf);
url_buf[len - 1] = '\0';
server_url = url_buf;
} else {
ESP_LOGE(TAG, "Configuration mismatch: invalid url for local server");
abort();
}
printf("\nServer URL obtained is %s\n", url_buf);
#else
server_url = CONFIG_EXAMPLE_LOCAL_SERVER_URL;
#endif /* CONFIG_EXAMPLE_LOCAL_SERVER_URL_FROM_STDIN */
https_get_request_to_local_server(server_url);
https_get_request_using_already_saved_session(server_url);
#endif
https_get_request_using_crt_bundle();
printf("Minimum free heap size: %d bytes\n", esp_get_minimum_free_heap_size());
https_get_request_using_cacert_buf();
https_get_request_using_global_ca_store();
#ifdef CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS
https_get_request_using_already_saved_session();
#endif
ESP_LOGI(TAG, "Finish https_request example");
vTaskDelete(NULL);
}