Skip to content

TLS: restore upload of SSL Root Certificate from IDE #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 3.x.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions arduino/libraries/WiFi/src/WiFiSSLClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,37 @@ int WiFiSSLClient::connect(const char* host, uint16_t port, bool sni)
return 0;
}

ret = esp_crt_bundle_attach(&_sslConfig);
if (ret != ESP_OK) {
return 0;
}
/* Starting from firmware 3.x.x cert partition starts at address 0xA000.
* The arduino-fw-uploader tool uploads user certificates in PEM format to a
* fixed flash address 0x10000. If you change again the partition table make
* sure 0x10000 is in the range of your certs partition.
* To make fimrware 3.x.x compatible with arduino-fw-uploader we first check
* for PEM certificate at address 0x10000, if no valid certificate is found
* we try to load the default certificate bundle from address 0xA000
*/
const bool hasPEM = (memcmp(&certs_data[0x6000], "-----BEGIN CERTIFICATE-----", 26) == 0);

if (hasPEM) {
ets_printf("Using provided certificate in pem format\n");
ret = mbedtls_x509_crt_parse(&_caCrt, &certs_data[0x6000], strlen((const char*)&certs_data[0x6000]) + 1);
if (ret < 0) {
ets_printf("mbedtls_x509_crt_parse failed: %d\n", ret);
stop();
}
mbedtls_ssl_conf_ca_chain(&_sslConfig, &_caCrt, NULL);
} else {
ets_printf("Using bundled certificate\n");
ret = esp_crt_bundle_attach(&_sslConfig);
if (ret != ESP_OK) {
stop();
return 0;
}

ret = esp_crt_bundle_set(certs_data, CRT_BUNDLE_SIZE);
if (ret != ESP_OK) {
return 0;
ret = esp_crt_bundle_set(certs_data, CRT_BUNDLE_SIZE);
if (ret != ESP_OK) {
stop();
return 0;
}
}

mbedtls_ssl_conf_rng(&_sslConfig, mbedtls_ctr_drbg_random, &_ctrDrbgContext);
Expand Down
32 changes: 23 additions & 9 deletions main/http_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ int downloadAndSaveFile(char * url, FILE * f, const char * cert_pem)
if(cert_pem != NULL) {
config.cert_pem = cert_pem;
} else {
config.crt_bundle_attach = esp_crt_bundle_attach;

const esp_partition_t* part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "certs");
if (part == NULL) {
return 0;
Expand All @@ -41,14 +39,30 @@ int downloadAndSaveFile(char * url, FILE * f, const char * cert_pem)
return 0;
}

ret = esp_crt_bundle_attach(&config);
if (ret != ESP_OK) {
return 0;
}
/* Starting from firmware 3.x.x cert partition starts at address 0xA000.
* The arduino-fw-uploader tool uploads user certificates in PEM format to a
* fixed flash address 0x10000. If you change again the partition table make
* sure 0x10000 is in the range of your certs partition.
* To make fimrware 3.x.x compatible with arduino-fw-uploader we first check
* for PEM certificate at address 0x10000, if no valid certificate is found
* we try to load the default certificate bundle from address 0xA000
*/
const bool hasPEM = (memcmp(&certs_data[0x6000], "-----BEGIN CERTIFICATE-----", 26) == 0);

ret = esp_crt_bundle_set(certs_data, CRT_BUNDLE_SIZE);
if (ret != ESP_OK) {
return 0;
if (hasPEM) {
config.cert_pem = (const char*)&certs_data[0x6000];
} else {
config.crt_bundle_attach = esp_crt_bundle_attach;

ret = esp_crt_bundle_attach(&config);
if (ret != ESP_OK) {
return 0;
}

ret = esp_crt_bundle_set(certs_data, CRT_BUNDLE_SIZE);
if (ret != ESP_OK) {
return 0;
}
}
}

Expand Down