mbedtls_ssl_read returned -0x7100 or -0x4C

elDi@mond
Posts: 19
Joined: Fri Feb 04, 2022 10:42 am

mbedtls_ssl_read returned -0x7100 or -0x4C

Postby elDi@mond » Thu Apr 18, 2024 8:02 am

I have a trouble with load image file from my web server.
My code is based on
\examples\protocols\https_mbedtls
code example

Code: Select all

ESP_LOGI(TAG, "Writing HTTP request...");

    size_t written_bytes = 0;
    size_t request_size = strlen(https_request.request);
do {
        ret = mbedtls_ssl_write(&ssl,
            (const unsigned char *)https_request.request + written_bytes,
            request_size - written_bytes
        );
        if (ret >= 0) {
            ESP_LOGI(TAG, "%d bytes written", ret);
            written_bytes += ret;
        } else if (ret != MBEDTLS_ERR_SSL_WANT_WRITE && 
                    ret != MBEDTLS_ERR_SSL_WANT_READ) {
            ESP_LOGE(TAG, "mbedtls_ssl_write returned -0x%x", -ret);
            ret = HTTPS_SOCK_ERR;
            goto exit;
        }
    } while(written_bytes < request_size);

    ESP_LOGI(TAG, "Reading HTTP response...");
    
    char *rcv_buf = malloc(WEB_RECEIVE_BUF_MAX);
    https_responce_t rcv_data;

    do {
        
        ret = mbedtls_ssl_read(
            &ssl, 
            (unsigned char *)rcv_buf, 
            https_request.responce_max_len
        );
        if(ret > 0) {
            ESP_LOGI(TAG, "recieved %d bytes", ret);
            memcpy(rcv_data.responce, rcv_buf, ret);
            rcv_data.lenght = ret;

            if(xQueueSend(https_responce_queue, &rcv_data,
                            500 / portTICK_PERIOD_MS) == pdFALSE) {
                break;
            }
        }

        if(ret == MBEDTLS_ERR_SSL_WANT_READ || 
            ret == MBEDTLS_ERR_SSL_WANT_WRITE
        )
            continue;

        if(ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
            ret = 0;
            break;
        }

        if(ret < 0)
        {
            ESP_LOGE(TAG, "mbedtls_ssl_read returned -0x%x", -ret);
            break;
        }

        if(ret == 0)
        {
            ESP_LOGI(TAG, "connection closed");
            break;
        }

        len = ret;
        ESP_LOGD(TAG, "%d bytes read", len);
        
    } while(1);
It's making 3 requests, first 2 going well and catch data as expected. It's doing some GET/POST request and write data, and catch it in several block of data, i received up to 7,5Kb of data

Request is
"GET /test_img.png HTTP/1.1\r\nHost: [project_name].herokuapp.com\r\nConnection: keep-alive\r\n\r\n"
But 3th request - image download (it's about 70Kb size, i even try to smaller pictures - 4Kb), catch only first packets of data and returns -0x7100 or -0x4C error

Code: Select all

I (325397) HTTPS: SSL Seting hostname: [project_name].herokuapp.com
I (325404) HTTPS: Connecting to [project_name].herokuapp.com:443...
I (325600) HTTPS: Connected.
I (325601) HTTPS: Performing the SSL/TLS handshake...
I (327144) HTTPS: Verifying peer X.509 certificate...
I (327144) HTTPS: Certificate verified.
I (327145) HTTPS: Cipher suite is TLS-ECDHE-RSA-WITH-AES-128-GCM-SHA256
I (327151) HTTPS: heap = 71728
I (327154) HTTPS: Writing HTTP request...
I (327162) HTTPS: 92 bytes written
I (327163) HTTPS: Reading HTTP response...
I (327351) HTTPS: recieved 1022 bytes
I (327352) HTTPS: recieved 9 bytes
E (327546) HTTPS: mbedtls_ssl_read returned -0x7100
I (328102) REQUESTS: total recieved 1031 bytes
I (328103) WIFI_CONF: disconnect
I managed to find out that
0x7100 SSL - Bad input parameters to function
0x004C NET - Reading information from the socket failed

And even no data recieved error

Code: Select all

I (25061) HTTPS: heap = 72192
I (25064) HTTPS: Writing HTTP request...
I (25070) HTTPS: 92 bytes written
I (25073) HTTPS: Reading HTTP response...
E (25352) HTTPS: mbedtls_ssl_read returned -0x7100
E (33235) REQUESTS: failed to receive first packet
I (33235) WIFI_CONF: disconnect
So the question is, how to fix that problem or what im doing wrong?

elDi@mond
Posts: 19
Joined: Fri Feb 04, 2022 10:42 am

Re: mbedtls_ssl_read returned -0x7100 or -0x4C

Postby elDi@mond » Mon Apr 22, 2024 5:29 am

The problem is solved!

0x7100 SSL - Bad input parameters to function
Solution: Missed "\r\n" at the end of request
0x004C NET - Reading information from the socket failed
Soulion: Marks end of data stream

Who is online

Users browsing this forum: No registered users and 105 guests