https_mbedtls_example yields error -0x50 on ssl_read

dmc7881
Posts: 1
Joined: Tue Mar 17, 2020 2:18 pm

https_mbedtls_example yields error -0x50 on ssl_read

Postby dmc7881 » Thu Jun 04, 2020 6:58 pm

I have been fighting this error for some time using http_client, esp-tls and now finally mbedtls directly while trying to hit an API endpoint used with azure blob storage, (because of the azure instance, we are not using azure-sdk, but rather esp-mqtt. I can connect to the endpoint without error through curl and POSTMAN.

I am using the sample code, slightly modified to also include a client certificate and client private key.

Code: Select all

#define WEB_SERVER "[iothub_address_removed].azure-devices.net"
#define WEB_PORT "443"
#define WEB_URL "https://[iothub_address_removed].azure-devices.net/devices/[devie_removed]/files/[file_removed]?api-version=2019-03-30"

static const char *TAG = "example";

static const char *AZURE_REQUEST = "GET " WEB_URL " HTTP/1.1\r\n"
    "Host: "WEB_SERVER"\r\n"
    "User-Agent: esp-idf/1.0 esp32\r\n"
    "Connection: keep-alive\r\n"
    "Content-Type: application/json\r\n"
    "Accept: */*\r\n"
    "\r\n";
    
    
extern const uint8_t server_root_cert_pem_start[] asm("_binary_server_root_cert_pem_start");
extern const uint8_t server_root_cert_pem_end[]   asm("_binary_server_root_cert_pem_end");

extern const uint8_t myuplink_cert_pem_start[] asm("_binary_myuplink_cert_pem_start");
extern const uint8_t myuplink_cert_pem_end[]   asm("_binary_myuplink_cert_pem_end");

extern const uint8_t client_cert_pem_start[] asm("_binary_client_cert_pem_start");
extern const uint8_t client_cert_pem_end[]   asm("_binary_client_cert_pem_end");

extern const uint8_t client_key_pem_start[] asm("_binary_client_key_pem_start");
extern const uint8_t client_key_pem_end[]   asm("_binary_client_key_pem_end");

static void https_get_task(void *pvParameters)
{
    char buf[512];
    int ret, flags, len;

    mbedtls_entropy_context entropy;
    mbedtls_ctr_drbg_context ctr_drbg;
    mbedtls_ssl_context ssl;
    mbedtls_x509_crt cacert;
    mbedtls_x509_crt publiccert;
    mbedtls_pk_context privkey;
    mbedtls_ssl_config conf;
    mbedtls_net_context server_fd;

    mbedtls_ssl_init(&ssl);
    mbedtls_x509_crt_init(&cacert);
    mbedtls_x509_crt_init(&publiccert);
    mbedtls_pk_init(&privkey);
    mbedtls_ctr_drbg_init(&ctr_drbg);
    ESP_LOGI(TAG, "Seeding the random number generator");

    mbedtls_ssl_config_init(&conf);

    mbedtls_entropy_init(&entropy);
    if((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
                                    NULL, 0)) != 0)
    {
        ESP_LOGE(TAG, "mbedtls_ctr_drbg_seed returned %d", ret);
        abort();
    }

    ESP_LOGI(TAG, "Loading the CA root certificate...");

    ret = mbedtls_x509_crt_parse(&cacert, myuplink_cert_pem_start,
                                 myuplink_cert_pem_end-myuplink_cert_pem_start);

    if(ret < 0)
    {
        ESP_LOGE(TAG, "mbedtls_x509_crt_parse returned -0x%x\n\n", -ret);
        abort();
    }
    
    ESP_LOGI(TAG, "Loading the Public certificate...");
    
    ret = mbedtls_x509_crt_parse(&publiccert, client_cert_pem_start,
				 client_cert_pem_end-client_cert_pem_start);
    
    if(ret < 0)
    {
        ESP_LOGE(TAG, "mbedtls_x509_crt_parse returned -0x%x\n\n", -ret);
        abort();
    }
    
    ESP_LOGI(TAG, "Loading the Private key...");
    
    ret = mbedtls_pk_parse_key(&privkey, client_key_pem_start,
			       client_key_pem_end-client_key_pem_start, NULL, 0);
    
    if(ret < 0)
    {
        ESP_LOGE(TAG, "mbedtls_pk_parse returned -0x%x\n\n", -ret);
        abort();
    }

    ESP_LOGI(TAG, "Setting hostname for TLS session...");

     /* Hostname set here should match CN in server certificate */
    if((ret = mbedtls_ssl_set_hostname(&ssl, WEB_SERVER)) != 0)
    {
        ESP_LOGE(TAG, "mbedtls_ssl_set_hostname returned -0x%x", -ret);
        abort();
    }

    ESP_LOGI(TAG, "Setting up the SSL/TLS structure...");

    if((ret = mbedtls_ssl_config_defaults(&conf,
                                          MBEDTLS_SSL_IS_CLIENT,
                                          MBEDTLS_SSL_TRANSPORT_STREAM,
                                          MBEDTLS_SSL_PRESET_DEFAULT)) != 0)
    {
        ESP_LOGE(TAG, "mbedtls_ssl_config_defaults returned %d", ret);
        goto exit;
    }

    /* MBEDTLS_SSL_VERIFY_OPTIONAL is bad for security, in this example it will print
       a warning if CA verification fails but it will continue to connect.

       You should consider using MBEDTLS_SSL_VERIFY_REQUIRED in your own code.
    */
    mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_NONE);
    mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
    mbedtls_ssl_conf_own_cert(&conf, &publiccert, &privkey);
    mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
#ifdef CONFIG_MBEDTLS_DEBUG
    mbedtls_esp_enable_debug_log(&conf, CONFIG_MBEDTLS_DEBUG_LEVEL);
#endif
    
    

    if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0)
    {
        ESP_LOGE(TAG, "mbedtls_ssl_setup returned -0x%x\n\n", -ret);
        goto exit;
    }

    while(1) {
        mbedtls_net_init(&server_fd);

        ESP_LOGI(TAG, "Connecting to %s:%s...", WEB_SERVER, WEB_PORT);

        if ((ret = mbedtls_net_connect(&server_fd, WEB_SERVER,
                                      WEB_PORT, MBEDTLS_NET_PROTO_TCP)) != 0)
        {
            ESP_LOGE(TAG, "mbedtls_net_connect returned -%x", -ret);
            goto exit;
        }

        ESP_LOGI(TAG, "Connected.");

        mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL);

        ESP_LOGI(TAG, "Performing the SSL/TLS handshake...");

        while ((ret = mbedtls_ssl_handshake(&ssl)) != 0)
        {
            if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE)
            {
                ESP_LOGE(TAG, "mbedtls_ssl_handshake returned -0x%x", -ret);
                goto exit;
            }
        }

        ESP_LOGI(TAG, "Verifying peer X.509 certificate...");

        if ((flags = mbedtls_ssl_get_verify_result(&ssl)) != 0)
        {
            /* In real life, we probably want to close connection if ret != 0 */
            ESP_LOGW(TAG, "Failed to verify peer certificate!");
            bzero(buf, sizeof(buf));
            mbedtls_x509_crt_verify_info(buf, sizeof(buf), "  ! ", flags);
            ESP_LOGW(TAG, "verification info: %s", buf);
        }
        else {
            ESP_LOGI(TAG, "Certificate verified.");
        }

        ESP_LOGI(TAG, "Cipher suite is %s", mbedtls_ssl_get_ciphersuite(&ssl));

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

        size_t written_bytes = 0;
        do {
            ret = mbedtls_ssl_write(&ssl,
                                    (const unsigned char *)AZURE_REQUEST + written_bytes,
                                    strlen(AZURE_REQUEST) - 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);
                goto exit;
            }
        } while(written_bytes < strlen(AZURE_REQUEST));

        ESP_LOGI(TAG, "Reading HTTP response...");

        do
        {
            len = sizeof(buf) - 1;
            bzero(buf, sizeof(buf));
            ret = mbedtls_ssl_read(&ssl, (unsigned char *)buf, len);

            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);
            /* Print response directly to stdout as it is read */
            for(int i = 0; i < len; i++) {
                putchar(buf[i]);
            }
        } while(1);

        mbedtls_ssl_close_notify(&ssl);

    exit:
        mbedtls_ssl_session_reset(&ssl);
        mbedtls_net_free(&server_fd);

        if(ret != 0)
        {
            mbedtls_strerror(ret, buf, 100);
            ESP_LOGE(TAG, "Last error was: -0x%x - %s", -ret, buf);
        }

        putchar('\n'); // JSON output doesn't have a newline at end

        static int request_count;
        ESP_LOGI(TAG, "Completed %d requests", ++request_count);

        for(int countdown = 10; countdown >= 0; countdown--) {
            ESP_LOGI(TAG, "%d...", countdown);
            vTaskDelay(1000 / portTICK_PERIOD_MS);
        }
        ESP_LOGI(TAG, "Starting again!");
    }
}
The log error:

Code: Select all

I (16398) mbedtls: ssl_tls.c:2722 ssl->f_recv(_timeout)() returned -80 (-0x0050)

W (16398) mbedtls: ssl_tls.c:4973 mbedtls_ssl_fetch_input() returned -80 (-0x0050)

W (16408) mbedtls: ssl_tls.c:4344 ssl_get_next_record() returned -80 (-0x0050)

W (16408) mbedtls: ssl_tls.c:8335 mbedtls_ssl_read_record() returned -80 (-0x0050)

E (16418) example: mbedtls_ssl_read returned -0x50
I (16428) mbedtls: ssl_tls.c:8725 => write close notify

I (16428) mbedtls: ssl_tls.c:5250 => send alert message

I (16438) mbedtls: ssl_tls.c:3343 => write record

I (16448) mbedtls: ssl_tls.c:1445 => encrypt buf

I (16448) mbedtls: ssl_tls.c:1781 <= encrypt buf

I (16458) mbedtls: ssl_tls.c:2755 => flush output

I (16458) mbedtls: ssl_tls.c:2774 message length: 69, out_left: 69

I (16468) mbedtls: ssl_tls.c:2779 ssl->f_send() returned -80 (-0x0050)

W (16478) mbedtls: ssl_tls.c:3472 mbedtls_ssl_flush_output() returned -80 (-0x0050)

W (16488) mbedtls: ssl_tls.c:5260 mbedtls_ssl_write_record() returned -80 (-0x0050)

W (16488) mbedtls: ssl_tls.c:8736 mbedtls_ssl_send_alert_message() returned -80 (-0x0050)

E (16508) example: Last error was: -0x50 - UNKNOWN ERROR CODE (0050)

I also have a full log at level 5 for both the application and mbedtls if that helps. Any assistance is appreciated.

Who is online

Users browsing this forum: No registered users and 123 guests