mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-04 21:24:32 +02:00
Merge branch 'feature/esp_tls_unit_tests' into 'master'
Feature/esp tls unit tests Closes IDF-1332 See merge request espressif/esp-idf!11930
This commit is contained in:
@@ -604,6 +604,7 @@ void esp_mbedtls_server_session_delete(esp_tls_t *tls)
|
||||
{
|
||||
if (tls != NULL) {
|
||||
esp_mbedtls_cleanup(tls);
|
||||
esp_tls_internal_event_tracker_destroy(tls->error_handle);
|
||||
free(tls);
|
||||
}
|
||||
};
|
||||
@@ -639,6 +640,7 @@ esp_err_t esp_mbedtls_set_global_ca_store(const unsigned char *cacert_pem_buf, c
|
||||
if (ret < 0) {
|
||||
ESP_LOGE(TAG, "mbedtls_x509_crt_parse returned -0x%x", -ret);
|
||||
mbedtls_x509_crt_free(global_cacert);
|
||||
free(global_cacert);
|
||||
global_cacert = NULL;
|
||||
return ESP_FAIL;
|
||||
} else if (ret > 0) {
|
||||
@@ -657,6 +659,7 @@ void esp_mbedtls_free_global_ca_store(void)
|
||||
{
|
||||
if (global_cacert) {
|
||||
mbedtls_x509_crt_free(global_cacert);
|
||||
free(global_cacert);
|
||||
global_cacert = NULL;
|
||||
}
|
||||
}
|
||||
|
@@ -157,6 +157,11 @@ static esp_err_t set_client_config(const char *hostname, size_t hostlen, esp_tls
|
||||
return ESP_ERR_WOLFSSL_CTX_SETUP_FAILED;
|
||||
}
|
||||
|
||||
if (cfg->crt_bundle_attach != NULL) {
|
||||
ESP_LOGE(TAG,"use_crt_bundle not supported in wolfssl");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
if (cfg->use_global_ca_store == true) {
|
||||
if ((esp_load_wolfssl_verify_buffer(tls, global_cacert, global_cacert_pem_bytes, FILE_TYPE_CA_CERT, &ret)) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Error in loading certificate verify buffer, returned %d", ret);
|
||||
@@ -223,11 +228,6 @@ static esp_err_t set_client_config(const char *hostname, size_t hostlen, esp_tls
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
if (cfg->crt_bundle_attach != NULL) {
|
||||
ESP_LOGE(TAG,"use_crt_bundle not supported in wolfssl");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
tls->priv_ssl =(void *)wolfSSL_new( (WOLFSSL_CTX *)tls->priv_ctx);
|
||||
if (!tls->priv_ssl) {
|
||||
ESP_LOGE(TAG, "Create wolfSSL failed");
|
||||
@@ -464,6 +464,7 @@ void esp_wolfssl_server_session_delete(esp_tls_t *tls)
|
||||
{
|
||||
if (tls != NULL) {
|
||||
esp_wolfssl_cleanup(tls);
|
||||
esp_tls_internal_event_tracker_destroy(tls->error_handle);
|
||||
free(tls);
|
||||
}
|
||||
}
|
||||
|
2
components/esp-tls/test/CMakeLists.txt
Normal file
2
components/esp-tls/test/CMakeLists.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
idf_component_register(SRC_DIRS "."
|
||||
PRIV_REQUIRES test_utils esp-tls)
|
4
components/esp-tls/test/component.mk
Normal file
4
components/esp-tls/test/component.mk
Normal file
@@ -0,0 +1,4 @@
|
||||
#
|
||||
#Component Makefile
|
||||
#
|
||||
COMPONENT_ADD_LDFLAGS = -Wl,--whole-archive -l$(COMPONENT_NAME) -Wl,--no-whole-archive
|
128
components/esp-tls/test/test_esp_tls.c
Normal file
128
components/esp-tls/test/test_esp_tls.c
Normal file
@@ -0,0 +1,128 @@
|
||||
// Copyright 2021 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "test_utils.h"
|
||||
#include "esp_tls.h"
|
||||
#include "unity.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
#if SOC_SHA_SUPPORT_PARALLEL_ENG
|
||||
#include "sha/sha_parallel_engine.h"
|
||||
#elif SOC_SHA_SUPPORT_DMA
|
||||
#include "sha/sha_dma.h"
|
||||
#endif
|
||||
|
||||
const char *test_cert_pem = "-----BEGIN CERTIFICATE-----\n"\
|
||||
"MIICrDCCAZQCCQD88gCs5AFs/jANBgkqhkiG9w0BAQsFADAYMRYwFAYDVQQDDA1F\n"\
|
||||
"U1AtVExTIFRlc3RzMB4XDTIxMDEwNzAxMTc1OVoXDTMxMDEwNTAxMTc1OVowGDEW\n"\
|
||||
"MBQGA1UEAwwNRVNQLVRMUyBUZXN0czCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC\n"\
|
||||
"AQoCggEBAMn4KrV9M40PcxDNdmwfFMXI5cydrV3nm1o1QzcDDuYn1YfxoCCcaIc0\n"\
|
||||
"JgHox948u94z32vnJU5qXVgEvwkD3nWGzWqjkwB1NlnIuF10XQFLa8cIas7Db/hd\n"\
|
||||
"KygbIbYwhtwQAQFEt0/MiUbA+m+rjzvffV9pDcPRlCOE6pC+X3iC7Ft3aevkEBtR\n"\
|
||||
"EPPN0sxGx0iFRIsglpDcEKUONYjF46q90LDmjA4EbqvK7XBuXjZ1QRGcQ871/8Ht\n"\
|
||||
"LzHjZKc5IcIxBVoVNJo0sgHHBOaG7J8x2eAJX8DfK6GseGZDbuptiJyZoj4nmOXx\n"\
|
||||
"iToEPG08DPlMHy90TDHXZbVeXNHzFV8CAwEAATANBgkqhkiG9w0BAQsFAAOCAQEA\n"\
|
||||
"n3oD/VONY9WIf0jvlAn62+VheABHFQqMrH02ucDxj0lRlgS4Z6+gRN2ITkzs8eNN\n"\
|
||||
"UglqVZCjU1mstL10tD8mwH8ShelxTWzHavc+GmAiEQx9C9zpMa7Oa+t6xFZCF6mi\n"\
|
||||
"c9h+3oNeql7zugcHjFXnlPd+oFhDTzH8R4Y+drZyR5LZsT5SNeEvLs7DEBMHNdKC\n"\
|
||||
"GOuWb7JNNhZKvNTymfCtM98s+7cbukwPogfWbaI+CjT49v4NOQlHNSN2Ldxpg9eh\n"\
|
||||
"fDgV8LqpUghnYjpe5lHgEqSZxV57z+amv5DRJNsH5+hB4k6HnRjfjQZqHUPlQ7Bk\n"\
|
||||
"pC4dhDzYsU8L1MN9e2i5Ig==\n"\
|
||||
"-----END CERTIFICATE-----\n";
|
||||
|
||||
const char *test_key_pem = "-----BEGIN PRIVATE KEY-----\n"\
|
||||
"MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDJ+Cq1fTOND3MQ\n"\
|
||||
"zXZsHxTFyOXMna1d55taNUM3Aw7mJ9WH8aAgnGiHNCYB6MfePLveM99r5yVOal1Y\n"\
|
||||
"BL8JA951hs1qo5MAdTZZyLhddF0BS2vHCGrOw2/4XSsoGyG2MIbcEAEBRLdPzIlG\n"\
|
||||
"wPpvq487331faQ3D0ZQjhOqQvl94guxbd2nr5BAbURDzzdLMRsdIhUSLIJaQ3BCl\n"\
|
||||
"DjWIxeOqvdCw5owOBG6ryu1wbl42dUERnEPO9f/B7S8x42SnOSHCMQVaFTSaNLIB\n"\
|
||||
"xwTmhuyfMdngCV/A3yuhrHhmQ27qbYicmaI+J5jl8Yk6BDxtPAz5TB8vdEwx12W1\n"\
|
||||
"XlzR8xVfAgMBAAECggEAMPHNF9JRjduynN3N1wHpPM0ks2N6wgkAounJcilxkOES\n"\
|
||||
"ZDFwBrcgtbMzir7owLijJoQXnsSTS9+G42+zA/JGFOEoIrGlTDUbiMKVu0AZ0dh/\n"\
|
||||
"M+qcR6MhHWUmsj9w+f/kjqvqMm5+pf4BPn5BspI20eGlNlv1FAFUDw+E8LLR3Bgf\n"\
|
||||
"6Wl1BfuKHvdIRUd8nnJlDd0eeFap7yI/b2RDdvkOl4ounlKQhwpyJue2fbYJ2Z2v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PfkbRJuY5DHfTdRhq+Tik5uYdRthlWM92Sf\n"\
|
||||
"Aogx44Fozd1t2hYcozPuZD4s\n"\
|
||||
"-----END PRIVATE KEY-----\n";
|
||||
|
||||
|
||||
static void test_leak_setup(const char *file, long line)
|
||||
{
|
||||
uint8_t mac[6];
|
||||
struct timeval te;
|
||||
gettimeofday(&te, NULL); // get current time
|
||||
esp_read_mac(mac, ESP_MAC_WIFI_STA);
|
||||
printf("%s:%ld: time=%ld.%lds, mac:" MACSTR "\n", file, line, te.tv_sec, te.tv_usec, MAC2STR(mac));
|
||||
// Execute esp_sha operation to allocate internal SHA semaphore memory
|
||||
// which is considered as leaked otherwise
|
||||
const uint8_t input_buffer[64];
|
||||
uint8_t output_buffer[64];
|
||||
esp_sha(SHA2_512, input_buffer, sizeof(input_buffer), output_buffer);
|
||||
unity_reset_leak_checks();
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("esp-tls init deinit", "[esp-tls][leaks=0]")
|
||||
{
|
||||
test_leak_setup(__FILE__, __LINE__);
|
||||
struct esp_tls *tls = esp_tls_init();
|
||||
TEST_ASSERT_NOT_NULL(tls);
|
||||
int ret = esp_tls_conn_destroy(tls);
|
||||
TEST_ASSERT_EQUAL(0, ret);
|
||||
}
|
||||
|
||||
TEST_CASE("esp-tls global_ca_store set free", "[esp-tls][leaks=0]")
|
||||
{
|
||||
test_leak_setup(__FILE__, __LINE__);
|
||||
esp_err_t ret = esp_tls_init_global_ca_store();
|
||||
TEST_ASSERT_EQUAL(ESP_OK, ret);
|
||||
ret = esp_tls_set_global_ca_store((const unsigned char *)test_cert_pem, strlen(test_cert_pem) + 1);
|
||||
TEST_ASSERT_EQUAL(ESP_OK, ret);
|
||||
esp_tls_free_global_ca_store();
|
||||
}
|
||||
|
||||
#ifdef CONFIG_ESP_TLS_SERVER
|
||||
TEST_CASE("esp_tls_server session create delete", "[esp-tls][leaks=0]")
|
||||
{
|
||||
test_leak_setup(__FILE__, __LINE__);
|
||||
struct esp_tls *tls = esp_tls_init();
|
||||
TEST_ASSERT_NOT_NULL(tls);
|
||||
esp_tls_cfg_server_t cfg = {
|
||||
.servercert_buf = (const unsigned char *)test_cert_pem,
|
||||
.servercert_bytes = strlen(test_cert_pem) + 1,
|
||||
.serverkey_buf = (const unsigned char *)test_key_pem,
|
||||
.serverkey_bytes = strlen(test_key_pem) + 1,
|
||||
};
|
||||
// The call is going to fail at the time of TLS handshake (no internet),
|
||||
// but the internal memory for the TLS connection will be allocated.
|
||||
int ret = esp_tls_server_session_create(&cfg, -1, tls);
|
||||
TEST_ASSERT_LESS_THAN_INT(0, ret);
|
||||
// free the allocated memory.
|
||||
esp_tls_server_session_delete(tls);
|
||||
}
|
||||
#endif
|
@@ -1,7 +1,7 @@
|
||||
import re
|
||||
import os
|
||||
|
||||
import re
|
||||
import ttfw_idf
|
||||
from tiny_test_fw import Utility
|
||||
|
||||
|
||||
@ttfw_idf.idf_example_test(env_tag="Example_EthKitV1")
|
||||
@@ -9,7 +9,8 @@ def test_examples_protocol_https_request(env, extra_data):
|
||||
"""
|
||||
steps: |
|
||||
1. join AP
|
||||
2. connect to www.howsmyssl.com:443
|
||||
2. establish TLS connection to www.howsmyssl.com:443 with multiple
|
||||
certificate verification options
|
||||
3. send http request
|
||||
"""
|
||||
dut1 = env.get_dut("https_request", "examples/protocols/https_request", dut_class=ttfw_idf.ESP32DUT)
|
||||
@@ -17,13 +18,51 @@ def test_examples_protocol_https_request(env, extra_data):
|
||||
binary_file = os.path.join(dut1.app.binary_path, "https_request.bin")
|
||||
bin_size = os.path.getsize(binary_file)
|
||||
ttfw_idf.log_performance("https_request_bin_size", "{}KB".format(bin_size // 1024))
|
||||
# start test
|
||||
# start tes
|
||||
Utility.console_log("Starting https_request simple test app")
|
||||
dut1.start_app()
|
||||
dut1.expect("Connection established...", timeout=30)
|
||||
dut1.expect("Reading HTTP response...")
|
||||
dut1.expect(re.compile(r"Completed (\d) requests"))
|
||||
|
||||
# test mbedtls dynamic resource
|
||||
# Check for connection using crt bundle
|
||||
Utility.console_log("Testing for \"https_request using crt bundle\"")
|
||||
try:
|
||||
dut1.expect(re.compile("https_request using crt bundle"), timeout=30)
|
||||
dut1.expect_all("Certificate validated",
|
||||
"Connection established...",
|
||||
"Reading HTTP response...",
|
||||
"HTTP/1.1 200 OK",
|
||||
re.compile("connection closed"))
|
||||
except Exception:
|
||||
Utility.console_log("Failed the test for \"https_request using crt bundle\"")
|
||||
raise
|
||||
Utility.console_log("Passed the test for \"https_request using crt bundle\"")
|
||||
|
||||
# Check for connection using cacert_buf
|
||||
Utility.console_log("Testing for \"https_request using cacert_buf\"")
|
||||
try:
|
||||
dut1.expect(re.compile("https_request using cacert_buf"), timeout=20)
|
||||
dut1.expect_all("Connection established...",
|
||||
"Reading HTTP response...",
|
||||
"HTTP/1.1 200 OK",
|
||||
re.compile("connection closed"))
|
||||
except Exception:
|
||||
Utility.console_log("Passed the test for \"https_request using cacert_buf\"")
|
||||
raise
|
||||
Utility.console_log("Passed the test for \"https_request using cacert_buf\"")
|
||||
|
||||
# Check for connection using global ca_store
|
||||
Utility.console_log("Testing for \"https_request using global ca_store\"")
|
||||
try:
|
||||
dut1.expect(re.compile("https_request using global ca_store"), timeout=20)
|
||||
dut1.expect_all("Connection established...",
|
||||
"Reading HTTP response...",
|
||||
"HTTP/1.1 200 OK",
|
||||
re.compile("connection closed"))
|
||||
except Exception:
|
||||
Utility.console_log("Failed the test for \"https_request using global ca_store\"")
|
||||
raise
|
||||
Utility.console_log("Passed the test for \"https_request using global ca_store\"")
|
||||
|
||||
# Check for connection using crt bundle with mbedtls dynamic resource enabled
|
||||
dut1 = env.get_dut("https_request", "examples/protocols/https_request", dut_class=ttfw_idf.ESP32DUT, app_config_name='ssldyn')
|
||||
# check and log bin size
|
||||
binary_file = os.path.join(dut1.app.binary_path, "https_request.bin")
|
||||
@@ -31,9 +70,18 @@ def test_examples_protocol_https_request(env, extra_data):
|
||||
ttfw_idf.log_performance("https_request_bin_size", "{}KB".format(bin_size // 1024))
|
||||
# start test
|
||||
dut1.start_app()
|
||||
dut1.expect("Connection established...", timeout=30)
|
||||
dut1.expect("Reading HTTP response...")
|
||||
dut1.expect(re.compile(r"Completed (\d) requests"))
|
||||
# only check if one connection is established
|
||||
Utility.console_log("Testing for \"https_request using crt bundle\" with mbedtls dynamic resource enabled")
|
||||
try:
|
||||
dut1.expect(re.compile("https_request using crt bundle"), timeout=30)
|
||||
dut1.expect_all("Connection established...",
|
||||
"Reading HTTP response...",
|
||||
"HTTP/1.1 200 OK",
|
||||
re.compile("connection closed"))
|
||||
except Exception:
|
||||
Utility.console_log("Failed the test for \"https_request using crt bundle\" when mbedtls dynamic resource was enabled")
|
||||
raise
|
||||
Utility.console_log("Passed the test for \"https_request using crt bundle\" when mbedtls dynamic resource was enabled")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
@@ -2,4 +2,5 @@
|
||||
#
|
||||
# (If this was a component, we would set COMPONENT_EMBED_TXTFILES here.)
|
||||
idf_component_register(SRCS "https_request_example_main.c"
|
||||
INCLUDE_DIRS ".")
|
||||
INCLUDE_DIRS "."
|
||||
EMBED_TXTFILES server_root_cert.pem)
|
||||
|
@@ -2,3 +2,4 @@
|
||||
# "main" pseudo-component makefile.
|
||||
#
|
||||
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
|
||||
COMPONENT_EMBED_TXTFILES := server_root_cert.pem
|
||||
|
@@ -50,21 +50,29 @@
|
||||
|
||||
static const char *TAG = "example";
|
||||
|
||||
static const char *REQUEST = "GET " WEB_URL " HTTP/1.1\r\n"
|
||||
static const char 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";
|
||||
|
||||
static void https_get_task(void *pvParameters)
|
||||
/* Root cert for howsmyssl.com, taken from server_root_cert.pem
|
||||
|
||||
The PEM file was extracted from the output of this command:
|
||||
openssl s_client -showcerts -connect www.howsmyssl.com:443 </dev/null
|
||||
|
||||
The CA root cert is the last cert given in the chain of certs.
|
||||
|
||||
To embed it in the app binary, the PEM file is named
|
||||
in the component.mk COMPONENT_EMBED_TXTFILES variable.
|
||||
*/
|
||||
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");
|
||||
|
||||
static void https_get_request(esp_tls_cfg_t cfg)
|
||||
{
|
||||
char buf[512];
|
||||
int ret, len;
|
||||
|
||||
while(1) {
|
||||
esp_tls_cfg_t cfg = {
|
||||
.crt_bundle_attach = esp_crt_bundle_attach,
|
||||
};
|
||||
|
||||
struct esp_tls *tls = esp_tls_conn_http_new(WEB_URL, &cfg);
|
||||
|
||||
if (tls != NULL) {
|
||||
@@ -78,35 +86,33 @@ static void https_get_task(void *pvParameters)
|
||||
do {
|
||||
ret = esp_tls_conn_write(tls,
|
||||
REQUEST + written_bytes,
|
||||
strlen(REQUEST) - written_bytes);
|
||||
sizeof(REQUEST) - written_bytes);
|
||||
if (ret >= 0) {
|
||||
ESP_LOGI(TAG, "%d bytes written", ret);
|
||||
written_bytes += ret;
|
||||
} else if (ret != ESP_TLS_ERR_SSL_WANT_READ && ret != ESP_TLS_ERR_SSL_WANT_WRITE) {
|
||||
ESP_LOGE(TAG, "esp_tls_conn_write returned 0x%x", ret);
|
||||
ESP_LOGE(TAG, "esp_tls_conn_write returned: [0x%02X](%s)", ret, esp_err_to_name(ret));
|
||||
goto exit;
|
||||
}
|
||||
} while(written_bytes < strlen(REQUEST));
|
||||
} while (written_bytes < sizeof(REQUEST));
|
||||
|
||||
ESP_LOGI(TAG, "Reading HTTP response...");
|
||||
|
||||
do
|
||||
{
|
||||
do {
|
||||
len = sizeof(buf) - 1;
|
||||
bzero(buf, sizeof(buf));
|
||||
ret = esp_tls_conn_read(tls, (char *)buf, len);
|
||||
|
||||
if(ret == ESP_TLS_ERR_SSL_WANT_WRITE || ret == ESP_TLS_ERR_SSL_WANT_READ)
|
||||
if (ret == ESP_TLS_ERR_SSL_WANT_WRITE || ret == ESP_TLS_ERR_SSL_WANT_READ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if(ret < 0)
|
||||
{
|
||||
ESP_LOGE(TAG, "esp_tls_conn_read returned -0x%x", -ret);
|
||||
if (ret < 0) {
|
||||
ESP_LOGE(TAG, "esp_tls_conn_read returned [-0x%02X](%s)", -ret, esp_err_to_name(ret));
|
||||
break;
|
||||
}
|
||||
|
||||
if(ret == 0)
|
||||
{
|
||||
if (ret == 0) {
|
||||
ESP_LOGI(TAG, "connection closed");
|
||||
break;
|
||||
}
|
||||
@@ -117,21 +123,62 @@ static void https_get_task(void *pvParameters)
|
||||
for (int i = 0; i < len; i++) {
|
||||
putchar(buf[i]);
|
||||
}
|
||||
putchar('\n'); // JSON output doesn't have a newline at end
|
||||
} while (1);
|
||||
|
||||
exit:
|
||||
esp_tls_conn_delete(tls);
|
||||
putchar('\n'); // JSON output doesn't have a newline at end
|
||||
|
||||
static int request_count;
|
||||
ESP_LOGI(TAG, "Completed %d requests", ++request_count);
|
||||
|
||||
for (int countdown = 10; countdown >= 0; countdown--) {
|
||||
ESP_LOGI(TAG, "%d...", countdown);
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
ESP_LOGI(TAG, "Starting again!");
|
||||
}
|
||||
|
||||
static void https_get_request_using_crt_bundle(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "https_request using crt bundle");
|
||||
esp_tls_cfg_t cfg = {
|
||||
.crt_bundle_attach = esp_crt_bundle_attach,
|
||||
};
|
||||
https_get_request(cfg);
|
||||
}
|
||||
|
||||
static void https_get_request_using_cacert_buf(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "https_request using cacert_buf");
|
||||
esp_tls_cfg_t cfg = {
|
||||
.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);
|
||||
}
|
||||
|
||||
static void https_get_request_using_global_ca_store(void)
|
||||
{
|
||||
esp_err_t esp_ret = ESP_FAIL;
|
||||
ESP_LOGI(TAG, "https_request using global ca_store");
|
||||
esp_ret = esp_tls_set_global_ca_store(server_root_cert_pem_start, server_root_cert_pem_end - server_root_cert_pem_start);
|
||||
if (esp_ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Error in setting the global ca store: [%02X] (%s),could not complete the https_request using global_ca_store", esp_ret, esp_err_to_name(esp_ret));
|
||||
return;
|
||||
}
|
||||
esp_tls_cfg_t cfg = {
|
||||
.use_global_ca_store = true,
|
||||
};
|
||||
https_get_request(cfg);
|
||||
esp_tls_free_global_ca_store();
|
||||
}
|
||||
|
||||
static void https_request_task(void *pvparameters)
|
||||
{
|
||||
ESP_LOGI(TAG, "Start https_request example");
|
||||
|
||||
https_get_request_using_crt_bundle();
|
||||
https_get_request_using_cacert_buf();
|
||||
https_get_request_using_global_ca_store();
|
||||
|
||||
ESP_LOGI(TAG, "Finish https_request example");
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
@@ -146,5 +193,5 @@ void app_main(void)
|
||||
*/
|
||||
ESP_ERROR_CHECK(example_connect());
|
||||
|
||||
xTaskCreate(&https_get_task, "https_get_task", 8192, NULL, 5, NULL);
|
||||
xTaskCreate(&https_request_task, "https_get_task", 8192, NULL, 5, NULL);
|
||||
}
|
||||
|
27
examples/protocols/https_request/main/server_root_cert.pem
Normal file
27
examples/protocols/https_request/main/server_root_cert.pem
Normal file
@@ -0,0 +1,27 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/
|
||||
MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT
|
||||
DkRTVCBSb290IENBI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C5jb20vRFNUUk9PVENBWDNDUkwu
|
||||
Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF
|
||||
AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo
|
||||
uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/
|
||||
wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu
|
||||
X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG
|
||||
PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6
|
||||
KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==
|
||||
-----END CERTIFICATE-----
|
@@ -1,3 +1,3 @@
|
||||
# This config is split between targets since different component needs to be excluded (esp32, esp32s2)
|
||||
CONFIG_IDF_TARGET="esp32"
|
||||
TEST_EXCLUDE_COMPONENTS=libsodium bt app_update freertos esp32 esp_ipc esp_pm esp_system esp_timer driver heap pthread soc spi_flash vfs test_utils experimental_cpp_component
|
||||
TEST_EXCLUDE_COMPONENTS=libsodium bt app_update freertos esp32 esp_ipc esp_pm esp_system esp_timer driver heap pthread soc spi_flash vfs test_utils experimental_cpp_component esp-tls
|
||||
|
@@ -1,3 +1,3 @@
|
||||
# This config is split between targets since different component needs to be excluded (esp32, esp32s2)
|
||||
CONFIG_IDF_TARGET="esp32c3"
|
||||
TEST_EXCLUDE_COMPONENTS=libsodium bt app_update freertos esp32c3 esp_ipc esp_pm esp_system esp_timer driver heap pthread soc spi_flash vfs experimental_cpp_component ulp perfmon
|
||||
TEST_EXCLUDE_COMPONENTS=libsodium bt app_update freertos esp32c3 esp_ipc esp_pm esp_system esp_timer driver heap pthread soc spi_flash vfs experimental_cpp_component ulp perfmon esp-tls
|
||||
|
@@ -1,3 +1,3 @@
|
||||
# This config is split between targets since different component needs to be excluded (esp32, esp32s2)
|
||||
CONFIG_IDF_TARGET="esp32s2"
|
||||
TEST_EXCLUDE_COMPONENTS=libsodium bt app_update freertos esp32s2 esp_ipc esp_pm esp_system esp_timer driver heap pthread soc spi_flash vfs experimental_cpp_component
|
||||
TEST_EXCLUDE_COMPONENTS=libsodium bt app_update freertos esp32s2 esp_ipc esp_pm esp_system esp_timer driver heap pthread soc spi_flash vfs experimental_cpp_component esp-tls
|
||||
|
5
tools/unit-test-app/configs/esp_tls
Normal file
5
tools/unit-test-app/configs/esp_tls
Normal file
@@ -0,0 +1,5 @@
|
||||
# As this is protocol specific, only test for one target.
|
||||
CONFIG_IDF_TARGET="esp32"
|
||||
TEST_COMPONENTS=esp-tls
|
||||
TEST_EXCLUDE_COMPONENTS=libsodium bt
|
||||
CONFIG_ESP_TLS_SERVER=y
|
@@ -1,5 +1,5 @@
|
||||
CONFIG_IDF_TARGET="esp32"
|
||||
TEST_EXCLUDE_COMPONENTS=libsodium bt app_update driver esp32 esp_ipc esp_pm esp_system esp_timer mbedtls spi_flash test_utils heap pthread soc experimental_cpp_component
|
||||
TEST_EXCLUDE_COMPONENTS=libsodium bt app_update driver esp32 esp_ipc esp_pm esp_system esp_timer mbedtls spi_flash test_utils heap pthread soc experimental_cpp_component esp-tls
|
||||
CONFIG_ESP32_SPIRAM_SUPPORT=y
|
||||
CONFIG_ESP_INT_WDT_TIMEOUT_MS=800
|
||||
CONFIG_SPIRAM_OCCUPY_NO_HOST=y
|
||||
|
@@ -1,6 +1,6 @@
|
||||
# This config is split between targets since different component needs to be included (esp32, esp32s2)
|
||||
CONFIG_IDF_TARGET="esp32"
|
||||
TEST_EXCLUDE_COMPONENTS=libsodium bt app_update freertos esp32 esp_ipc esp_pm esp_system esp_timer driver heap pthread soc spi_flash vfs test_utils experimental_cpp_component
|
||||
TEST_EXCLUDE_COMPONENTS=libsodium bt app_update freertos esp32 esp_ipc esp_pm esp_system esp_timer driver heap pthread soc spi_flash vfs test_utils experimental_cpp_component esp-tls
|
||||
CONFIG_COMPILER_OPTIMIZATION_SIZE=y
|
||||
CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y
|
||||
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y
|
||||
|
@@ -1,6 +1,6 @@
|
||||
# This config is split between targets since different component needs to be excluded (esp32, esp32s2)
|
||||
CONFIG_IDF_TARGET="esp32"
|
||||
TEST_EXCLUDE_COMPONENTS=libsodium bt app_update freertos esp32 esp_system esp_pm esp_ipc esp_timer driver heap pthread soc spi_flash vfs test_utils experimental_cpp_component
|
||||
TEST_EXCLUDE_COMPONENTS=libsodium bt app_update freertos esp32 esp_system esp_pm esp_ipc esp_timer driver heap pthread soc spi_flash vfs test_utils experimental_cpp_component esp-tls
|
||||
CONFIG_MEMMAP_SMP=n
|
||||
CONFIG_FREERTOS_UNICORE=y
|
||||
CONFIG_ESP32_RTCDATA_IN_FAST_MEM=y
|
||||
|
Reference in New Issue
Block a user