1 // Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
18 #include "freertos/FreeRTOS.h"
19 #include "freertos/task.h"
22 #include "esp_system.h"
24 #include "transport.h"
25 #include "transport_ssl.h"
26 #include "transport_utils.h"
28 static const char *TAG = "TRANS_SSL";
30 * mbedtls specific transport data
40 transport_handle_t transport_get_handle(transport_handle_t t);
42 static int ssl_close(transport_handle_t t);
44 static int ssl_connect(transport_handle_t t, const char *host, int port, int timeout_ms)
46 transport_ssl_t *ssl = transport_get_context_data(t);
47 esp_tls_cfg_t cfg = { 0 };
48 if (ssl->cert_pem_data) {
49 ssl->verify_server = true;
50 cfg.cacert_pem_buf = ssl->cert_pem_data;
51 cfg.cacert_pem_bytes = ssl->cert_pem_len + 1;
53 cfg.timeout_ms = timeout_ms;
54 ssl->ssl_initialized = true;
55 ssl->tls = esp_tls_conn_new(host, strlen(host), port, &cfg);
57 ESP_LOGE(TAG, "Failed to open a new connection");
63 static int ssl_poll_read(transport_handle_t t, int timeout_ms)
65 transport_ssl_t *ssl = transport_get_context_data(t);
68 FD_SET(ssl->tls->sockfd, &readset);
69 struct timeval timeout;
70 transport_utils_ms_to_timeval(timeout_ms, &timeout);
72 return select(ssl->tls->sockfd + 1, &readset, NULL, NULL, &timeout);
75 static int ssl_poll_write(transport_handle_t t, int timeout_ms)
77 transport_ssl_t *ssl = transport_get_context_data(t);
80 FD_SET(ssl->tls->sockfd, &writeset);
81 struct timeval timeout;
82 transport_utils_ms_to_timeval(timeout_ms, &timeout);
83 return select(ssl->tls->sockfd + 1, NULL, &writeset, NULL, &timeout);
86 static int ssl_write(transport_handle_t t, const char *buffer, int len, int timeout_ms)
89 transport_ssl_t *ssl = transport_get_context_data(t);
91 if ((poll = transport_poll_write(t, timeout_ms)) <= 0) {
92 ESP_LOGW(TAG, "Poll timeout or error, errno=%s, fd=%d, timeout_ms=%d", strerror(errno), ssl->tls->sockfd, timeout_ms);
95 ret = esp_tls_conn_write(ssl->tls, (const unsigned char *) buffer, len);
97 ESP_LOGE(TAG, "mbedtls_ssl_write error, errno=%s", strerror(errno));
102 static int ssl_read(transport_handle_t t, char *buffer, int len, int timeout_ms)
105 transport_ssl_t *ssl = transport_get_context_data(t);
107 if (esp_tls_get_bytes_avail(ssl->tls) <= 0) {
108 if ((poll = transport_poll_read(t, timeout_ms)) <= 0) {
112 ret = esp_tls_conn_read(ssl->tls, (unsigned char *)buffer, len);
114 ESP_LOGE(TAG, "mbedtls_ssl_read error, errno=%s", strerror(errno));
119 static int ssl_close(transport_handle_t t)
122 transport_ssl_t *ssl = transport_get_context_data(t);
123 if (ssl->ssl_initialized) {
124 esp_tls_conn_delete(ssl->tls);
125 ssl->ssl_initialized = false;
126 ssl->verify_server = false;
131 static int ssl_destroy(transport_handle_t t)
133 transport_ssl_t *ssl = transport_get_context_data(t);
139 void transport_ssl_set_cert_data(transport_handle_t t, const char *data, int len)
141 transport_ssl_t *ssl = transport_get_context_data(t);
143 ssl->cert_pem_data = (void *)data;
144 ssl->cert_pem_len = len;
148 transport_handle_t transport_ssl_init()
150 transport_handle_t t = transport_init();
151 transport_ssl_t *ssl = calloc(1, sizeof(transport_ssl_t));
152 TRANSPORT_MEM_CHECK(TAG, ssl, return NULL);
153 transport_set_context_data(t, ssl);
154 transport_set_func(t, ssl_connect, ssl_read, ssl_write, ssl_close, ssl_poll_read, ssl_poll_write, ssl_destroy, transport_get_handle);