]> granicus.if.org Git - esp-idf/commitdiff
esp_http_client: Add esp-tls APIs in SSL transport of esp_http_client
authorJitin George <jitin@espressif.com>
Fri, 27 Jul 2018 12:48:26 +0000 (18:18 +0530)
committerbot <bot@espressif.com>
Tue, 21 Aug 2018 09:07:07 +0000 (09:07 +0000)
esp-tls APIs and SSL transport layer of esp_http_client try to
achieve similar things, so this is an effort to remove code redundancy.

components/esp_http_client/esp_http_client.c
components/esp_http_client/lib/transport_ssl.c

index e21b93da2f6228d7d01a150a56eff718946dfe31..99bf0ec73ced976c4a74a8f66d1bb6851f22c813 100644 (file)
@@ -878,7 +878,7 @@ esp_err_t esp_http_client_open(esp_http_client_handle_t client, int write_len)
             return ESP_ERR_HTTP_INVALID_TRANSPORT;
         }
         if (transport_connect(client->transport, client->connection_info.host, client->connection_info.port, client->timeout_ms) < 0) {
-            ESP_LOGE(TAG, "Connection failed, sock < 0");
+            ESP_LOGE(TAG, "Connection failed");
             return ESP_ERR_HTTP_CONNECT;
         }
         http_dispatch_event(client, HTTP_EVENT_ON_CONNECTED, NULL, 0);
index bb07c92455ec7ef9d2e05ae5a35cd9bb4c948b74..bd2260aaa9e024425bff09342c4a15c4022ca6cf 100644 (file)
 
 #include "freertos/FreeRTOS.h"
 #include "freertos/task.h"
-#include "lwip/err.h"
-#include "lwip/sockets.h"
-#include "lwip/sys.h"
-#include "lwip/netdb.h"
-#include "lwip/dns.h"
-
-#include "mbedtls/platform.h"
-#include "mbedtls/net_sockets.h"
-#include "mbedtls/esp_debug.h"
-#include "mbedtls/ssl.h"
-#include "mbedtls/entropy.h"
-#include "mbedtls/ctr_drbg.h"
-#include "mbedtls/error.h"
-#include "mbedtls/certs.h"
-
-
+#include "esp_tls.h"
 #include "esp_log.h"
 #include "esp_system.h"
 
@@ -45,12 +30,7 @@ static const char *TAG = "TRANS_SSL";
  *  mbedtls specific transport data
  */
 typedef struct {
-    mbedtls_entropy_context  entropy;
-    mbedtls_ctr_drbg_context ctr_drbg;
-    mbedtls_ssl_context      ctx;
-    mbedtls_x509_crt         cacert;
-    mbedtls_ssl_config       conf;
-    mbedtls_net_context      client_fd;
+    esp_tls_t                *tls;
     void                     *cert_pem_data;
     int                      cert_pem_len;
     bool                     ssl_initialized;
@@ -61,108 +41,21 @@ static int ssl_close(transport_handle_t t);
 
 static int ssl_connect(transport_handle_t t, const char *host, int port, int timeout_ms)
 {
-    int ret = -1, flags;
-    struct timeval tv;
     transport_ssl_t *ssl = transport_get_context_data(t);
-
-    if (!ssl) {
-        return -1;
-    }
-    ssl->ssl_initialized = true;
-    mbedtls_ssl_init(&ssl->ctx);
-    mbedtls_ctr_drbg_init(&ssl->ctr_drbg);
-    mbedtls_ssl_config_init(&ssl->conf);
-    mbedtls_entropy_init(&ssl->entropy);
-
-    if ((ret = mbedtls_ssl_config_defaults(&ssl->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;
-    }
-
-    if ((ret = mbedtls_ctr_drbg_seed(&ssl->ctr_drbg, mbedtls_entropy_func, &ssl->entropy, NULL, 0)) != 0) {
-        ESP_LOGE(TAG, "mbedtls_ctr_drbg_seed returned %d", ret);
-        goto exit;
-    }
-
+    esp_tls_cfg_t cfg = { 0 };
     if (ssl->cert_pem_data) {
-        mbedtls_x509_crt_init(&ssl->cacert);
         ssl->verify_server = true;
-        if ((ret = mbedtls_x509_crt_parse(&ssl->cacert, ssl->cert_pem_data, ssl->cert_pem_len + 1)) < 0) {
-            ESP_LOGE(TAG, "mbedtls_x509_crt_parse returned -0x%x\n\nDATA=%s,len=%d", -ret, (char*)ssl->cert_pem_data, ssl->cert_pem_len);
-            goto exit;
-        }
-        mbedtls_ssl_conf_ca_chain(&ssl->conf, &ssl->cacert, NULL);
-        mbedtls_ssl_conf_authmode(&ssl->conf, MBEDTLS_SSL_VERIFY_REQUIRED);
-
-        if ((ret = mbedtls_ssl_set_hostname(&ssl->ctx, host)) != 0) {
-            ESP_LOGE(TAG, "mbedtls_ssl_set_hostname returned -0x%x", -ret);
-            goto exit;
-        }
-    } else {
-        mbedtls_ssl_conf_authmode(&ssl->conf, MBEDTLS_SSL_VERIFY_NONE);
+        cfg.cacert_pem_buf = ssl->cert_pem_data;
+        cfg.cacert_pem_bytes = ssl->cert_pem_len + 1;
     }
-
-
-    mbedtls_ssl_conf_rng(&ssl->conf, mbedtls_ctr_drbg_random, &ssl->ctr_drbg);
-
-#ifdef CONFIG_MBEDTLS_DEBUG
-    mbedtls_esp_enable_debug_log(&ssl->conf, 4);
-#endif
-
-    if ((ret = mbedtls_ssl_setup(&ssl->ctx, &ssl->conf)) != 0) {
-        ESP_LOGE(TAG, "mbedtls_ssl_setup returned -0x%x\n\n", -ret);
-        goto exit;
-    }
-
-    mbedtls_net_init(&ssl->client_fd);
-
-    http_utils_ms_to_timeval(timeout_ms, &tv);
-
-    setsockopt(ssl->client_fd.fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
-    ESP_LOGD(TAG, "Connect to %s:%d", host, port);
-    char port_str[8] = {0};
-    sprintf(port_str, "%d", port);
-    if ((ret = mbedtls_net_connect(&ssl->client_fd, host, port_str, MBEDTLS_NET_PROTO_TCP)) != 0) {
-        ESP_LOGE(TAG, "mbedtls_net_connect returned -%x", -ret);
-        goto exit;
-    }
-
-    mbedtls_ssl_set_bio(&ssl->ctx, &ssl->client_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
-
-    if((ret = mbedtls_ssl_set_hostname(&ssl->ctx, host)) != 0) {
-        ESP_LOGE(TAG, " failed\n  ! mbedtls_ssl_set_hostname returned %d\n\n", ret);
-        goto exit;
-    }
-
-    ESP_LOGD(TAG, "Performing the SSL/TLS handshake...");
-
-    while ((ret = mbedtls_ssl_handshake(&ssl->ctx)) != 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_LOGD(TAG, "Verifying peer X.509 certificate...");
-
-    if ((flags = mbedtls_ssl_get_verify_result(&ssl->ctx)) != 0) {
-        /* In real life, we probably want to close connection if ret != 0 */
-        ESP_LOGW(TAG, "Failed to verify peer certificate!");
-        if (ssl->cert_pem_data) {
-            goto exit;
-        }
-    } else {
-        ESP_LOGD(TAG, "Certificate verified.");
+    cfg.timeout_ms = timeout_ms;
+    ssl->ssl_initialized = true;
+    ssl->tls = esp_tls_conn_new(host, strlen(host), port, &cfg);
+    if (!ssl->tls) {
+        ESP_LOGE(TAG, "Failed to open a new connection");
+        return -1;
     }
-
-    ESP_LOGD(TAG, "Cipher suite is %s", mbedtls_ssl_get_ciphersuite(&ssl->ctx));
-    return ret;
-exit:
-    ssl_close(t);
-    return ret;
+    return 0;
 }
 
 static int ssl_poll_read(transport_handle_t t, int timeout_ms)
@@ -170,11 +63,11 @@ static int ssl_poll_read(transport_handle_t t, int timeout_ms)
     transport_ssl_t *ssl = transport_get_context_data(t);
     fd_set readset;
     FD_ZERO(&readset);
-    FD_SET(ssl->client_fd.fd, &readset);
+    FD_SET(ssl->tls->sockfd, &readset);
     struct timeval timeout;
     http_utils_ms_to_timeval(timeout_ms, &timeout);
 
-    return select(ssl->client_fd.fd + 1, &readset, NULL, NULL, &timeout);
+    return select(ssl->tls->sockfd + 1, &readset, NULL, NULL, &timeout);
 }
 
 static int ssl_poll_write(transport_handle_t t, int timeout_ms)
@@ -182,10 +75,10 @@ static int ssl_poll_write(transport_handle_t t, int timeout_ms)
     transport_ssl_t *ssl = transport_get_context_data(t);
     fd_set writeset;
     FD_ZERO(&writeset);
-    FD_SET(ssl->client_fd.fd, &writeset);
+    FD_SET(ssl->tls->sockfd, &writeset);
     struct timeval timeout;
     http_utils_ms_to_timeval(timeout_ms, &timeout);
-    return select(ssl->client_fd.fd + 1, NULL, &writeset, NULL, &timeout);
+    return select(ssl->tls->sockfd + 1, NULL, &writeset, NULL, &timeout);
 }
 
 static int ssl_write(transport_handle_t t, const char *buffer, int len, int timeout_ms)
@@ -194,10 +87,10 @@ static int ssl_write(transport_handle_t t, const char *buffer, int len, int time
     transport_ssl_t *ssl = transport_get_context_data(t);
 
     if ((poll = transport_poll_write(t, timeout_ms)) <= 0) {
-        ESP_LOGW(TAG, "Poll timeout or error, errno=%s, fd=%d, timeout_ms=%d", strerror(errno), ssl->client_fd.fd, timeout_ms);
+        ESP_LOGW(TAG, "Poll timeout or error, errno=%s, fd=%d, timeout_ms=%d", strerror(errno), ssl->tls->sockfd, timeout_ms);
         return poll;
     }
-    ret = mbedtls_ssl_write(&ssl->ctx, (const unsigned char *) buffer, len);
+    ret = esp_tls_conn_write(ssl->tls, (const unsigned char *) buffer, len);
     if (ret <= 0) {
         ESP_LOGE(TAG, "mbedtls_ssl_write error, errno=%s", strerror(errno));
     }
@@ -206,14 +99,18 @@ static int ssl_write(transport_handle_t t, const char *buffer, int len, int time
 
 static int ssl_read(transport_handle_t t, char *buffer, int len, int timeout_ms)
 {
-    int poll = -1, ret;
+    int poll, ret;
     transport_ssl_t *ssl = transport_get_context_data(t);
-    if (mbedtls_ssl_get_bytes_avail(&ssl->ctx) <= 0) {
+
+    if (esp_tls_get_bytes_avail(ssl->tls) <= 0) {
         if ((poll = transport_poll_read(t, timeout_ms)) <= 0) {
             return poll;
         }
     }
-    ret = mbedtls_ssl_read(&ssl->ctx, (unsigned char *)buffer, len);
+    ret = esp_tls_conn_read(ssl->tls, (unsigned char *)buffer, len);
+    if (ret <= 0) {
+        ESP_LOGE(TAG, "mbedtls_ssl_read error, errno=%s", strerror(errno));
+    }
     return ret;
 }
 
@@ -222,17 +119,7 @@ static int ssl_close(transport_handle_t t)
     int ret = -1;
     transport_ssl_t *ssl = transport_get_context_data(t);
     if (ssl->ssl_initialized) {
-        ESP_LOGD(TAG, "Cleanup mbedtls");
-        mbedtls_ssl_close_notify(&ssl->ctx);
-        mbedtls_ssl_session_reset(&ssl->ctx);
-        mbedtls_net_free(&ssl->client_fd);
-        mbedtls_ssl_config_free(&ssl->conf);
-        if (ssl->verify_server) {
-            mbedtls_x509_crt_free(&ssl->cacert);
-        }
-        mbedtls_ctr_drbg_free(&ssl->ctr_drbg);
-        mbedtls_entropy_free(&ssl->entropy);
-        mbedtls_ssl_free(&ssl->ctx);
+        esp_tls_conn_delete(ssl->tls);
         ssl->ssl_initialized = false;
         ssl->verify_server = false;
     }
@@ -261,7 +148,6 @@ transport_handle_t transport_ssl_init()
     transport_handle_t t = transport_init();
     transport_ssl_t *ssl = calloc(1, sizeof(transport_ssl_t));
     HTTP_MEM_CHECK(TAG, ssl, return NULL);
-    mbedtls_net_init(&ssl->client_fd);
     transport_set_context_data(t, ssl);
     transport_set_func(t, ssl_connect, ssl_read, ssl_write, ssl_close, ssl_poll_read, ssl_poll_write, ssl_destroy);
     return t;