Skip to content

Commit 546ce38

Browse files
authored
fix(tls): do not attach bundle from runtime (#9763)
* fix(tls): do not attach bundle from runtime * fix(ssl): Make the bundle callback per context
1 parent de2fc25 commit 546ce38

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

Diff for: libraries/NetworkClientSecure/src/NetworkClientSecure.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,11 @@ void NetworkClientSecure::setCACert(const char *rootCA) {
317317
void NetworkClientSecure::setCACertBundle(const uint8_t *bundle) {
318318
if (bundle != NULL) {
319319
esp_crt_bundle_set(bundle, sizeof(bundle));
320+
attach_ssl_certificate_bundle(sslclient.get(), true);
320321
_use_ca_bundle = true;
321322
} else {
322323
esp_crt_bundle_detach(NULL);
324+
attach_ssl_certificate_bundle(sslclient.get(), false);
323325
_use_ca_bundle = false;
324326
}
325327
}

Diff for: libraries/NetworkClientSecure/src/ssl_client.cpp

+16-5
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ void ssl_init(sslclient_context *ssl_client) {
5151
ssl_client->peek_buf = -1;
5252
}
5353

54+
void attach_ssl_certificate_bundle(sslclient_context *ssl_client, bool att) {
55+
if (att) {
56+
ssl_client->bundle_attach_cb = &esp_crt_bundle_attach;
57+
} else {
58+
ssl_client->bundle_attach_cb = NULL;
59+
}
60+
}
61+
5462
int start_ssl_client(
5563
sslclient_context *ssl_client, const IPAddress &ip, uint32_t port, const char *hostname, int timeout, const char *rootCABuff, bool useRootCABundle,
5664
const char *cli_cert, const char *cli_key, const char *pskIdent, const char *psKey, bool insecure, const char **alpn_protos
@@ -195,11 +203,14 @@ int start_ssl_client(
195203
return handle_error(ret);
196204
}
197205
} else if (useRootCABundle) {
198-
log_v("Attaching root CA cert bundle");
199-
ret = esp_crt_bundle_attach(&ssl_client->ssl_conf);
200-
201-
if (ret < 0) {
202-
return handle_error(ret);
206+
if (ssl_client->bundle_attach_cb != NULL) {
207+
log_v("Attaching root CA cert bundle");
208+
ret = ssl_client->bundle_attach_cb(&ssl_client->ssl_conf);
209+
if (ret < 0) {
210+
return handle_error(ret);
211+
}
212+
} else {
213+
log_e("useRootCABundle is set, but attach_ssl_certificate_bundle(ssl, true); was not called!");
203214
}
204215
} else if (pskIdent != NULL && psKey != NULL) {
205216
log_v("Setting up PSK");

Diff for: libraries/NetworkClientSecure/src/ssl_client.h

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include "mbedtls/ctr_drbg.h"
1313
#include "mbedtls/error.h"
1414

15+
typedef esp_err_t (*crt_bundle_attach_cb)(void *conf);
16+
1517
typedef struct sslclient_context {
1618
int socket;
1719
mbedtls_ssl_context ssl_ctx;
@@ -24,6 +26,8 @@ typedef struct sslclient_context {
2426
mbedtls_x509_crt client_cert;
2527
mbedtls_pk_context client_key;
2628

29+
crt_bundle_attach_cb bundle_attach_cb;
30+
2731
unsigned long socket_timeout;
2832
unsigned long handshake_timeout;
2933

@@ -37,6 +41,7 @@ int start_ssl_client(
3741
sslclient_context *ssl_client, const IPAddress &ip, uint32_t port, const char *hostname, int timeout, const char *rootCABuff, bool useRootCABundle,
3842
const char *cli_cert, const char *cli_key, const char *pskIdent, const char *psKey, bool insecure, const char **alpn_protos
3943
);
44+
void attach_ssl_certificate_bundle(sslclient_context *ssl_client, bool att);
4045
int ssl_starttls_handshake(sslclient_context *ssl_client);
4146
void stop_ssl_socket(sslclient_context *ssl_client);
4247
int data_to_read(sslclient_context *ssl_client);

0 commit comments

Comments
 (0)