Skip to content

mbed-tls-try2 updates #3

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

Merged
merged 8 commits into from
Jan 22, 2020
Merged
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
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ set(COMPONENT_REQUIRES
register_component()

target_compile_options(${COMPONENT_TARGET} PRIVATE -fno-rtti)

if(CONFIG_ASYNC_TCP_SSL_ENABLED)
target_compile_options(${COMPONENT_TARGET} PRIVATE -DASYNC_TCP_SSL_ENABLED)
endif()
6 changes: 6 additions & 0 deletions Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ config ASYNC_TCP_USE_WDT
help
Enable WDT for the AsyncTCP task, so it will trigger if a handler is locking the thread.

config ASYNC_TCP_SSL_ENABLED
bool "Enable SSL for AsyncTCP client"
default "n"
help
Enables mbedTLS support for AsyncTCP clients.

endmenu
12 changes: 7 additions & 5 deletions src/AsyncTCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,10 @@ static bool _remove_events_with_arg(void * arg){
}

static void _handle_async_event(lwip_event_packet_t * e){
//ets_printf("T %s- ", pcTaskGetTaskName(xTaskGetCurrentTaskHandle()));
if(e->event == LWIP_TCP_CLEAR){
if(e->arg == NULL){
// do nothing when arg is NULL
//ets_printf("event arg == NULL: 0x%08x\n", e->recv.pcb);
} else if(e->event == LWIP_TCP_CLEAR){
_remove_events_with_arg(e->arg);
} else if(e->event == LWIP_TCP_RECV){
//ets_printf("-R: 0x%08x\n", e->recv.pcb);
Expand Down Expand Up @@ -972,11 +974,11 @@ int8_t AsyncClient::_connected(void* pcb, int8_t err){
#if ASYNC_TCP_SSL_ENABLED
if(_pcb_secure){
bool err = false;
if(_root_ca) {
if (_psk_ident != NULL and _psk != NULL) {
err = tcp_ssl_new_psk_client(_pcb, this, _psk_ident, _psk) < 0;
} else {
err = tcp_ssl_new_client(_pcb, this, _hostname.empty() ? NULL : _hostname.c_str(),
_root_ca, _root_ca_len) < 0;
} else {
err = tcp_ssl_new_psk_client(_pcb, this, _psk_ident, _psk) < 0;
}
if (err) {
log_e("closing....");
Expand Down
2 changes: 2 additions & 0 deletions src/AsyncTCP.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
#include "sdkconfig.h"
#include <functional>
#include <string>
#if ASYNC_TCP_SSL_ENABLED
#include <ssl_client.h>
#include "tcp_mbedtls.h"
#endif
extern "C" {
#include "freertos/semphr.h"
#include "lwip/pbuf.h"
Expand Down
9 changes: 7 additions & 2 deletions src/tcp_mbedtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ int tcp_ssl_new_client(struct tcp_pcb *tcp, void *arg, const char* hostname, con
mbedtls_ssl_config_init(&tcp_ssl->ssl_conf);

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

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

if (pskey == NULL || psk_ident == NULL) {
TCP_SSL_DEBUG(" failed\n ! pre-shared key or identity is NULL\n\n");
return -1;
}

if(tcp == NULL) return -1;
if(tcp_ssl_get(tcp) != NULL) return -1;

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

mbedtls_ctr_drbg_seed(&tcp_ssl->drbg_ctx, mbedtls_entropy_func,
&tcp_ssl->entropy_ctx, (const uint8_t*)pers, strlen(pers));
&tcp_ssl->entropy_ctx, (const uint8_t*)pers, sizeof(pers));

if(mbedtls_ssl_config_defaults(&tcp_ssl->ssl_conf,
MBEDTLS_SSL_IS_CLIENT,
Expand Down