mbedtls_ssl_handshake returned -0x4290

Ritu21
Posts: 123
Joined: Sat Aug 04, 2018 9:58 am

mbedtls_ssl_handshake returned -0x4290

Postby Ritu21 » Mon Apr 29, 2019 11:37 am

Hi,

Getting the above error code very frequently after https calls which leads to the connection fail in http sockets. This continues for few calls then reboots the device.

Could please guide what does this error means and how to rectify it???.

Waiting for your early response.

Thanks
Ritu


Ritu21
Posts: 123
Joined: Sat Aug 04, 2018 9:58 am

Re: mbedtls_ssl_handshake returned -0x4290

Postby Ritu21 » Tue Apr 30, 2019 5:12 am

Hi Chegewara,

From the link you have sent, I could only make out that this error is due to memory leak in TLS library.

***It seems to be a memory leak into the mbedtls library. I am using xPortGetFreeHeapSize() to check the heap size and with each ssl communication the free heap size is lower.

Where to use xPortGetFreeHeapSize() in my code and how will that help in resolving the issue.???

Please suggest how to resolve this issue.

Waiting for your reply.

Thanks
Ritu

Ritu21
Posts: 123
Joined: Sat Aug 04, 2018 9:58 am

Re: mbedtls_ssl_handshake returned -0x4290

Postby Ritu21 » Tue Apr 30, 2019 5:21 am

After sometimes, it starts returning, mbedtls_ssl_setup returned -0x7f00.

What is this error for????

My system gets hanged after sometime.

Please help in this issue.

Thanks
Ritu.

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: mbedtls_ssl_handshake returned -0x4290

Postby WiFive » Tue Apr 30, 2019 5:31 am

https://github.com/espressif/mbedtls/bl ... ssl.h#L100

You're running out of memory, it could be memory leak from your code or just using too much.

https://docs.espressif.com/projects/esp ... nformation

Ritu21
Posts: 123
Joined: Sat Aug 04, 2018 9:58 am

Re: mbedtls_ssl_handshake returned -0x4290

Postby Ritu21 » Tue Apr 30, 2019 5:50 am

Hi WiFive,

If I follow the steps given under heap tracing & also a short example under this section.

https://docs.espressif.com/projects/esp ... ap-tracing

Thanks
Ritu

chegewara
Posts: 2378
Joined: Wed Jun 14, 2017 9:00 pm

Re: mbedtls_ssl_handshake returned -0x4290

Postby chegewara » Wed May 01, 2019 9:42 am

E (57867091) example: mbedtls_ssl_handshake returned -0x4290
E (57867101) example: Last error was: -0x4290 - RSA - The public key operation failed : BIGNUM - Memory allocation failed
#define MBEDTLS_ERR_SSL_ALLOC_FAILED -0x7F00 /**< Memory allocation failed */

Ritu21
Posts: 123
Joined: Sat Aug 04, 2018 9:58 am

Re: mbedtls_ssl_handshake returned -0x4290

Postby Ritu21 » Mon May 06, 2019 9:38 am

Hi Chegewara!!!

Below is the http task which sends data to the server.

void http_client(void *pvParam)
{
ESP_LOGI(HTTP_TAG,"http_client task started \n");

bzero(g_byNew_smartTagURLBuff, sizeof(g_byNew_smartTagURLBuff));
form_smartTagURL(g_byNew_smartTagURLBuff);

bzero(g_byNew_normalTagURLBuff, sizeof(g_byNew_normalTagURLBuff));
form_NormalTagURL(g_byNew_normalTagURLBuff);

cJSON *location = NULL;
cJSON *sourceId = NULL;
cJSON *rfidTag = NULL;
cJSON *dateTime = NULL;
cJSON *smart = NULL;
static uint8_t client_queue_buff[RFID_Q_BUFF_SIZE];
char time_buf[32];
uint8_t rfid_queue_buff[RFID_Q_BUFF_SIZE];
char *ipRecvdFromTcpClient;
unsigned short portRecvdFromTcpClient = 0;
char tcp_port_inAscii[6];
esp_http_client_config_t config = {};
xEventGroupWaitBits(wifi_event_group,WIFI_STA_CONNECTED_BIT,false,true,portMAX_DELAY);
//calculate_Time();
while(1)
{
if(xQueuePeek(rfid_queue, client_queue_buff, ( TickType_t ) 1 ))
{
if(xQueueReceive(rfid_queue, client_queue_buff, ( TickType_t ) 1 ) == pdTRUE)
{
//ESP_ERROR_CHECK( heap_trace_start(HEAP_TRACE_LEAKS) );
strcpy((char*)rfid_queue_buff, (char*)client_queue_buff);

ESP_LOG_BUFFER_HEXDUMP(HTTP_TAG, client_queue_buff, g_byRfid_byteCopy, ESP_LOG_INFO);
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
strftime(time_buf, sizeof(time_buf), "%d/%m/%Y %H:%M:%S", timeinfo);
ESP_LOGI(HTTP_TAG, "The current date/time in India is: %s",time_buf);
if(g_boolSmartAccessStatus_ReceivedFromServer == 1)
{
{
config.url = g_byNew_smartTagURLBuff,
config.event_handler = _http_event_handler;
//config.is_async = true;
};
}
else
{
{
config.url = g_byNew_normalTagURLBuff,
config.event_handler = _http_event_handler;
//config.is_async = true;
};
apiFailedState = LAST_FAILED_NORMALCARD;
}
esp_http_client_handle_t client = esp_http_client_init(&config);
//printf("Smart URL = %s\n", config.url);
cJSON *rfid_data_json = cJSON_CreateObject();
location = cJSON_CreateString(g_byDeviceIDBuff_ReceivedFromServer);
cJSON_AddItemToObject(rfid_data_json, "location", location);
sourceId = cJSON_CreateString(g_byFormattedMacID_Buff);
cJSON_AddItemToObject(rfid_data_json, "sourceId", sourceId);
rfidTag = cJSON_CreateString((char *)rfid_queue_buff);
cJSON_AddItemToObject(rfid_data_json, "rfidTag", rfidTag);
dateTime = cJSON_CreateString(time_buf);
cJSON_AddItemToObject(rfid_data_json, "dateTime", dateTime);
if(g_boolSmartAccessStatus_ReceivedFromServer == 1){
smart = cJSON_CreateString("true");
cJSON_AddItemToObject(rfid_data_json, "smartAccess", smart);
parseState = PARSE_SMARTACCESS_RESPONSE;
apiFailedState = LAST_FAILED_SMARTACCESS;
}
const char *post_data = cJSON_Print(rfid_data_json);
printf("MONITOR = %s\n", post_data);
g_byLastDataSentFailedBuff = cJSON_Print(rfid_data_json);
esp_http_client_set_post_field(client, post_data, strlen(post_data));
esp_http_client_set_method(client, HTTP_METHOD_POST);
esp_http_client_set_header(client, "Authorization", access_token);
//printf("Access_Token = %s\n", access_token);
esp_http_client_set_header(client, "Content-Type", "application/json");
clock_t current_time;
current_time = clock();
err = esp_http_client_perform(client);
current_time = clock() - current_time;
double time_taken = ((double)current_time)/CLOCKS_PER_SEC; // in seconds
printf("smartAccess_DataSend() took %f seconds to execute \n", time_taken);
//ESP_ERROR_CHECK( heap_trace_stop() );
//heap_trace_dump();
if (err == ESP_OK){
ESP_LOGI(HTTP_TAG, "RFID Data Post Status = %d, content_length = %d",
esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client));
}
else {
ESP_LOGE(HTTP_TAG, "RFID Data Post Status request failed: %s", esp_err_to_name(err));
}
if(esp_http_client_get_status_code(client) != 200){
err = esp_http_client_perform(client);
}
esp_http_client_close(client);
esp_http_client_cleanup(client);
cJSON_Delete(rfid_data_json);
}
/*uint32_t freeheap = xPortGetFreeHeapSize();
printf("xPortGetFreeHeapSize = %d bytes\n", freeheap);
ESP_ERROR_CHECK( heap_trace_stop() );
heap_trace_dump();
uint32_t freeheap = xPortGetFreeHeapSize();
printf("xPortGetFreeHeapSize1 = %d bytes\n", freeheap);*/
}
if(xQueuePeek(ip_PortQueue, g_byIP_Port_Buff, ( TickType_t ) 3 ))
{
xQueueReceive(ip_PortQueue, g_byIP_Port_Buff, ( TickType_t ) 5 );
g_byReceivedfromTCPClient_IP = *(uint32_t*)(g_byIP_Port_Buff+7);
ipRecvdFromTcpClient = inet_ntoa(g_byReceivedfromTCPClient_IP);
//printf("The IP address is %s\n", ipRecvdFromTcpClient);
err = nvs_set_str(ipPortHandler, "ip2", (char *)ipRecvdFromTcpClient);
if(err != ESP_OK)
{
printf("\nError in nvs_set_str ip2! (%04X)\n", err);
//return;
}
//printf("The IP address is %s\n", ipRecvdFromTcpClient);
//portRecvdFromTcpClient = ntohs(*(u_short *)(g_byIP_Port_Buff+11));
portRecvdFromTcpClient = *(u_short *)(g_byIP_Port_Buff+11);
int len = sizeof(portRecvdFromTcpClient);
printf("Port length = %d\n", len);
if(len > 0){
itoa(portRecvdFromTcpClient, tcp_port_inAscii, 10);
err = nvs_set_str(ipPortHandler, "port", tcp_port_inAscii);
if(err != ESP_OK)
{
printf("\nError in nvs_set_str port! (%04X)\n", err);
//return;
}
}
else{
portRecvdFromTcpClient = DEFAULT_HTTP_PORT; //443
printf("No port received from app, using %d port\n", portRecvdFromTcpClient);
itoa(portRecvdFromTcpClient, tcp_port_inAscii, 10);
err = nvs_set_str(ipPortHandler, "port", tcp_port_inAscii);
if(err != ESP_OK)
{
printf("\nError in nvs_set_str port! (%04X)\n", err);
//return;
}
}
strcpy((char*)(g_byReceivedfromTCPClient_URL),(char*)(g_byIP_Port_Buff+13));
printf(" URL = %s\n", g_byReceivedfromTCPClient_URL);
err = nvs_set_str(ipPortHandler, "url", (char *)g_byReceivedfromTCPClient_URL);
if(err != ESP_OK)
{
printf("\nError in nvs_set_str url! (%04X)\n", err);
//return;
}
err = nvs_commit(ipPortHandler);
if(err != ESP_OK)
{
printf("\nError in nvs_commit! (%04X)\n", err);
//return;
}
bzero(g_byServerCommonURL, sizeof(g_byServerCommonURL));
//formCommonURL_DataRecv_fromTCPClient(g_byServerCommonURL, ipRecvdFromTcpClient);
formCommonURL_DataRecv_fromTCPClient(g_byServerCommonURL, ipRecvdFromTcpClient, portRecvdFromTcpClient, g_byReceivedfromTCPClient_URL);
//printf("generating tokens again\n");
parseState = PARSE_GENERATE_TOKENS_RESPONSE;
generate_Tokens();
}
if(g_boolRebootStatus_ReceivedFromServer == 1){
g_boolRebootStatus_ReceivedFromServer = 0;
//printf("Restarting...\n");
gpio_set_level(SERVER_LED_OUTPUT_PIN, 0);
esp_restart();
}
while(g_boolDeviceNotInitiated == true)
{
//printf("INIT = TRUE\n");
parseState = PARSE_DEVICE_INIT_RESPONSE;
call_DeviceInit_Api();
}
TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
TIMERG0.wdt_feed=1;
TIMERG0.wdt_wprotect=0;
}
}

Below is the error I get, after sending few data to the server:

E (119654) esp-tls: mbedtls_ssl_handshake returned -0x4290
E (119654) esp-tls: Failed to open new connection
E (119654) TRANS_SSL: Failed to open a new connection
E (119654) HTTP_CLIENT: Connection failed, sock < 0
smartAccess_DataSend() took 0.450000 seconds to execute
E (119674) http_client: RFID Data Post Status request failed: ESP_ERR_HTTP_CONNECT


Can you make out why there is memory allocation failed error after sending around 40 data to the server??

Waiting for your early response.

Thanks
Ritu.

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: mbedtls_ssl_handshake returned -0x4290

Postby WiFive » Mon May 06, 2019 10:52 am


Ritu21
Posts: 123
Joined: Sat Aug 04, 2018 9:58 am

Re: mbedtls_ssl_handshake returned -0x4290

Postby Ritu21 » Mon May 06, 2019 1:14 pm

Hi WiFive,

Thanks for your response.

As suggested by you, I have freed const char *post_data = cJSON_Print(rfid_data_json); memory.

Again getting the same error, but after sending 170 data on server i.e the count has increased. Also, applied the same changes to other places wherever the memory was not freed.

Thanks
Ritu.

Who is online

Users browsing this forum: No registered users and 82 guests