Skip to content

Commit 44b3d6b

Browse files
robert-alfarotve
authored andcommitted
mbed-tls-try2 updates (#3)
* Fix LoadProhibited (me-no-dev#73) * Use sizeof instead of strlen for const char[] * Add Kconfig option to control ASYNC_TCP_SSL_ENABLED * Optionally include ssl header files * Add null check for psk_ident and pskey * Do not default to PSK when root_ca is not explcitly set. tcp_ssl_new_client() has a case to handle this. * Move psk null checks to top of function, remove unneeded include, syntax cleanup. Authored-by: Bob <[email protected]>
1 parent abdd496 commit 44b3d6b

File tree

5 files changed

+26
-7
lines changed

5 files changed

+26
-7
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ set(COMPONENT_REQUIRES
1313
register_component()
1414

1515
target_compile_options(${COMPONENT_TARGET} PRIVATE -fno-rtti)
16+
17+
if(CONFIG_ASYNC_TCP_SSL_ENABLED)
18+
target_compile_options(${COMPONENT_TARGET} PRIVATE -DASYNC_TCP_SSL_ENABLED)
19+
endif()

Kconfig.projbuild

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,10 @@ config ASYNC_TCP_USE_WDT
2727
help
2828
Enable WDT for the AsyncTCP task, so it will trigger if a handler is locking the thread.
2929

30+
config ASYNC_TCP_SSL_ENABLED
31+
bool "Enable SSL for AsyncTCP client"
32+
default "n"
33+
help
34+
Enables mbedTLS support for AsyncTCP clients.
35+
3036
endmenu

src/AsyncTCP.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,10 @@ static bool _remove_events_with_arg(void * arg){
157157
}
158158

159159
static void _handle_async_event(lwip_event_packet_t * e){
160-
//ets_printf("T %s- ", pcTaskGetTaskName(xTaskGetCurrentTaskHandle()));
161-
if(e->event == LWIP_TCP_CLEAR){
160+
if(e->arg == NULL){
161+
// do nothing when arg is NULL
162+
//ets_printf("event arg == NULL: 0x%08x\n", e->recv.pcb);
163+
} else if(e->event == LWIP_TCP_CLEAR){
162164
_remove_events_with_arg(e->arg);
163165
} else if(e->event == LWIP_TCP_RECV){
164166
//ets_printf("-R: 0x%08x\n", e->recv.pcb);
@@ -972,11 +974,11 @@ int8_t AsyncClient::_connected(void* pcb, int8_t err){
972974
#if ASYNC_TCP_SSL_ENABLED
973975
if(_pcb_secure){
974976
bool err = false;
975-
if(_root_ca) {
977+
if (_psk_ident != NULL and _psk != NULL) {
978+
err = tcp_ssl_new_psk_client(_pcb, this, _psk_ident, _psk) < 0;
979+
} else {
976980
err = tcp_ssl_new_client(_pcb, this, _hostname.empty() ? NULL : _hostname.c_str(),
977981
_root_ca, _root_ca_len) < 0;
978-
} else {
979-
err = tcp_ssl_new_psk_client(_pcb, this, _psk_ident, _psk) < 0;
980982
}
981983
if (err) {
982984
log_e("closing....");

src/AsyncTCP.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
#include "sdkconfig.h"
2727
#include <functional>
2828
#include <string>
29+
#if ASYNC_TCP_SSL_ENABLED
2930
#include <ssl_client.h>
3031
#include "tcp_mbedtls.h"
32+
#endif
3133
extern "C" {
3234
#include "freertos/semphr.h"
3335
#include "lwip/pbuf.h"

src/tcp_mbedtls.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ int tcp_ssl_new_client(struct tcp_pcb *tcp, void *arg, const char* hostname, con
231231
mbedtls_ssl_config_init(&tcp_ssl->ssl_conf);
232232

233233
mbedtls_ctr_drbg_seed(&tcp_ssl->drbg_ctx, mbedtls_entropy_func,
234-
&tcp_ssl->entropy_ctx, (const unsigned char*)pers, strlen(pers));
234+
&tcp_ssl->entropy_ctx, (const unsigned char*)pers, sizeof(pers));
235235

236236
if(mbedtls_ssl_config_defaults(&tcp_ssl->ssl_conf,
237237
MBEDTLS_SSL_IS_CLIENT,
@@ -297,6 +297,11 @@ int tcp_ssl_new_client(struct tcp_pcb *tcp, void *arg, const char* hostname, con
297297
int tcp_ssl_new_psk_client(struct tcp_pcb *tcp, void *arg, const char* psk_ident, const char* pskey) {
298298
tcp_ssl_t* tcp_ssl;
299299

300+
if (pskey == NULL || psk_ident == NULL) {
301+
TCP_SSL_DEBUG(" failed\n ! pre-shared key or identity is NULL\n\n");
302+
return -1;
303+
}
304+
300305
if(tcp == NULL) return -1;
301306
if(tcp_ssl_get(tcp) != NULL) return -1;
302307

@@ -309,7 +314,7 @@ int tcp_ssl_new_psk_client(struct tcp_pcb *tcp, void *arg, const char* psk_ident
309314
mbedtls_ssl_config_init(&tcp_ssl->ssl_conf);
310315

311316
mbedtls_ctr_drbg_seed(&tcp_ssl->drbg_ctx, mbedtls_entropy_func,
312-
&tcp_ssl->entropy_ctx, (const uint8_t*)pers, strlen(pers));
317+
&tcp_ssl->entropy_ctx, (const uint8_t*)pers, sizeof(pers));
313318

314319
if(mbedtls_ssl_config_defaults(&tcp_ssl->ssl_conf,
315320
MBEDTLS_SSL_IS_CLIENT,

0 commit comments

Comments
 (0)