1 // Copyright 2019 Espressif Systems (Shanghai) PTE LTD
2 //
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
6
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14 #include <stdio.h>
15 #include <string.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netdb.h>
22
23 #include <http_parser.h>
24 #include "esp_tls.h"
25 #include "esp_tls_error_capture_internal.h"
26 #include <errno.h>
27 static const char *TAG = "esp-tls";
28
29 #ifdef CONFIG_ESP_TLS_USING_MBEDTLS
30 #include "esp_tls_mbedtls.h"
31 #elif CONFIG_ESP_TLS_USING_WOLFSSL
32 #include "esp_tls_wolfssl.h"
33 #endif
34
35 #ifdef ESP_PLATFORM
36 #include <esp_log.h>
37 #else
38 #define ESP_LOGD(TAG, ...) //printf(__VA_ARGS__);
39 #define ESP_LOGE(TAG, ...) printf(__VA_ARGS__);
40 #endif
41
42 #ifdef CONFIG_ESP_TLS_USING_MBEDTLS
43 #define _esp_create_ssl_handle esp_create_mbedtls_handle
44 #define _esp_tls_handshake esp_mbedtls_handshake
45 #define _esp_tls_read esp_mbedtls_read
46 #define _esp_tls_write esp_mbedtls_write
47 #define _esp_tls_conn_delete esp_mbedtls_conn_delete
48 #ifdef CONFIG_ESP_TLS_SERVER
49 #define _esp_tls_server_session_create esp_mbedtls_server_session_create
50 #define _esp_tls_server_session_delete esp_mbedtls_server_session_delete
51 #endif /* CONFIG_ESP_TLS_SERVER */
52 #define _esp_tls_get_bytes_avail esp_mbedtls_get_bytes_avail
53 #define _esp_tls_init_global_ca_store esp_mbedtls_init_global_ca_store
54 #define _esp_tls_set_global_ca_store esp_mbedtls_set_global_ca_store /*!< Callback function for setting global CA store data for TLS/SSL */
55 #define _esp_tls_get_global_ca_store esp_mbedtls_get_global_ca_store
56 #define _esp_tls_free_global_ca_store esp_mbedtls_free_global_ca_store /*!< Callback function for freeing global ca store for TLS/SSL */
57 #elif CONFIG_ESP_TLS_USING_WOLFSSL /* CONFIG_ESP_TLS_USING_MBEDTLS */
58 #define _esp_create_ssl_handle esp_create_wolfssl_handle
59 #define _esp_tls_handshake esp_wolfssl_handshake
60 #define _esp_tls_read esp_wolfssl_read
61 #define _esp_tls_write esp_wolfssl_write
62 #define _esp_tls_conn_delete esp_wolfssl_conn_delete
63 #ifdef CONFIG_ESP_TLS_SERVER
64 #define _esp_tls_server_session_create esp_wolfssl_server_session_create
65 #define _esp_tls_server_session_delete esp_wolfssl_server_session_delete
66 #endif /* CONFIG_ESP_TLS_SERVER */
67 #define _esp_tls_get_bytes_avail esp_wolfssl_get_bytes_avail
68 #define _esp_tls_init_global_ca_store esp_wolfssl_init_global_ca_store
69 #define _esp_tls_set_global_ca_store esp_wolfssl_set_global_ca_store /*!< Callback function for setting global CA store data for TLS/SSL */
70 #define _esp_tls_free_global_ca_store esp_wolfssl_free_global_ca_store /*!< Callback function for freeing global ca store for TLS/SSL */
71 #else /* ESP_TLS_USING_WOLFSSL */
72 #error "No TLS stack configured"
73 #endif
74
create_ssl_handle(const char * hostname,size_t hostlen,const void * cfg,esp_tls_t * tls)75 static esp_err_t create_ssl_handle(const char *hostname, size_t hostlen, const void *cfg, esp_tls_t *tls)
76 {
77 return _esp_create_ssl_handle(hostname, hostlen, cfg, tls);
78 }
79
esp_tls_handshake(esp_tls_t * tls,const esp_tls_cfg_t * cfg)80 static esp_err_t esp_tls_handshake(esp_tls_t *tls, const esp_tls_cfg_t *cfg)
81 {
82 return _esp_tls_handshake(tls, cfg);
83 }
84
tcp_read(esp_tls_t * tls,char * data,size_t datalen)85 static ssize_t tcp_read(esp_tls_t *tls, char *data, size_t datalen)
86 {
87 return recv(tls->sockfd, data, datalen, 0);
88 }
89
tcp_write(esp_tls_t * tls,const char * data,size_t datalen)90 static ssize_t tcp_write(esp_tls_t *tls, const char *data, size_t datalen)
91 {
92 return send(tls->sockfd, data, datalen, 0);
93 }
94
95 /**
96 * @brief Close the TLS connection and free any allocated resources.
97 */
esp_tls_conn_delete(esp_tls_t * tls)98 void esp_tls_conn_delete(esp_tls_t *tls)
99 {
100 esp_tls_conn_destroy(tls);
101 }
102
esp_tls_conn_destroy(esp_tls_t * tls)103 int esp_tls_conn_destroy(esp_tls_t *tls)
104 {
105 if (tls != NULL) {
106 int ret = 0;
107 _esp_tls_conn_delete(tls);
108 if (tls->sockfd >= 0) {
109 ret = close(tls->sockfd);
110 }
111 esp_tls_internal_event_tracker_destroy(tls->error_handle);
112 free(tls);
113 return ret;
114 }
115 return -1; // invalid argument
116 }
117
esp_tls_init(void)118 esp_tls_t *esp_tls_init(void)
119 {
120 esp_tls_t *tls = (esp_tls_t *)calloc(1, sizeof(esp_tls_t));
121 if (!tls) {
122 return NULL;
123 }
124 tls->error_handle = esp_tls_internal_event_tracker_create();
125 if (!tls->error_handle) {
126 free(tls);
127 return NULL;
128 }
129 #ifdef CONFIG_ESP_TLS_USING_MBEDTLS
130 tls->server_fd.fd = -1;
131 #endif
132 tls->sockfd = -1;
133 return tls;
134 }
135
resolve_host_name(const char * host,size_t hostlen,struct addrinfo ** address_info)136 static esp_err_t resolve_host_name(const char *host, size_t hostlen, struct addrinfo **address_info)
137 {
138 struct addrinfo hints;
139 memset(&hints, 0, sizeof(hints));
140 hints.ai_family = AF_UNSPEC;
141 hints.ai_socktype = SOCK_STREAM;
142
143 char *use_host = strndup(host, hostlen);
144 if (!use_host) {
145 return ESP_ERR_NO_MEM;
146 }
147
148 ESP_LOGD(TAG, "host:%s: strlen %lu", use_host, (unsigned long)hostlen);
149 if (getaddrinfo(use_host, NULL, &hints, address_info)) {
150 ESP_LOGE(TAG, "couldn't get hostname for :%s:", use_host);
151 free(use_host);
152 return ESP_ERR_ESP_TLS_CANNOT_RESOLVE_HOSTNAME;
153 }
154 free(use_host);
155 return ESP_OK;
156 }
157
ms_to_timeval(int timeout_ms,struct timeval * tv)158 static void ms_to_timeval(int timeout_ms, struct timeval *tv)
159 {
160 tv->tv_sec = timeout_ms / 1000;
161 tv->tv_usec = (timeout_ms % 1000) * 1000;
162 }
163
esp_tls_tcp_enable_keep_alive(int fd,tls_keep_alive_cfg_t * cfg)164 static int esp_tls_tcp_enable_keep_alive(int fd, tls_keep_alive_cfg_t *cfg)
165 {
166 int keep_alive_enable = 1;
167 int keep_alive_idle = cfg->keep_alive_idle;
168 int keep_alive_interval = cfg->keep_alive_interval;
169 int keep_alive_count = cfg->keep_alive_count;
170
171 ESP_LOGD(TAG, "Enable TCP keep alive. idle: %d, interval: %d, count: %d", keep_alive_idle, keep_alive_interval, keep_alive_count);
172 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &keep_alive_enable, sizeof(keep_alive_enable)) != 0) {
173 ESP_LOGE(TAG, "Fail to setsockopt SO_KEEPALIVE");
174 return -1;
175 }
176 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &keep_alive_idle, sizeof(keep_alive_idle)) != 0) {
177 ESP_LOGE(TAG, "Fail to setsockopt TCP_KEEPIDLE");
178 return -1;
179 }
180 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &keep_alive_interval, sizeof(keep_alive_interval)) != 0) {
181 ESP_LOGE(TAG, "Fail to setsockopt TCP_KEEPINTVL");
182 return -1;
183 }
184 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &keep_alive_count, sizeof(keep_alive_count)) != 0) {
185 ESP_LOGE(TAG, "Fail to setsockopt TCP_KEEPCNT");
186 return -1;
187 }
188
189 return 0;
190 }
191
esp_tcp_connect(const char * host,int hostlen,int port,int * sockfd,const esp_tls_t * tls,const esp_tls_cfg_t * cfg)192 static esp_err_t esp_tcp_connect(const char *host, int hostlen, int port, int *sockfd, const esp_tls_t *tls, const esp_tls_cfg_t *cfg)
193 {
194 esp_err_t ret;
195 struct addrinfo *addrinfo;
196 if ((ret = resolve_host_name(host, hostlen, &addrinfo)) != ESP_OK) {
197 return ret;
198 }
199
200 int fd = socket(addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol);
201 if (fd < 0) {
202 ESP_LOGE(TAG, "Failed to create socket (family %d socktype %d protocol %d)", addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol);
203 ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ESP_TLS_ERR_TYPE_SYSTEM, errno);
204 ret = ESP_ERR_ESP_TLS_CANNOT_CREATE_SOCKET;
205 goto err_freeaddr;
206 }
207
208 void *addr_ptr;
209 if (addrinfo->ai_family == AF_INET) {
210 struct sockaddr_in *p = (struct sockaddr_in *)addrinfo->ai_addr;
211 p->sin_port = htons(port);
212 addr_ptr = p;
213 }
214 #if CONFIG_LWIP_IPV6
215 else if (addrinfo->ai_family == AF_INET6) {
216 struct sockaddr_in6 *p = (struct sockaddr_in6 *)addrinfo->ai_addr;
217 p->sin6_port = htons(port);
218 p->sin6_family = AF_INET6;
219 addr_ptr = p;
220 }
221 #endif
222 else {
223 ESP_LOGE(TAG, "Unsupported protocol family %d", addrinfo->ai_family);
224 ret = ESP_ERR_ESP_TLS_UNSUPPORTED_PROTOCOL_FAMILY;
225 goto err_freesocket;
226 }
227
228 if (cfg) {
229 if (cfg->timeout_ms >= 0) {
230 struct timeval tv;
231 ms_to_timeval(cfg->timeout_ms, &tv);
232 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
233 setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
234 if (cfg->keep_alive_cfg && cfg->keep_alive_cfg->keep_alive_enable) {
235 if (esp_tls_tcp_enable_keep_alive(fd, cfg->keep_alive_cfg) < 0) {
236 ESP_LOGE(TAG, "Error setting keep-alive");
237 goto err_freesocket;
238 }
239 }
240 }
241 if (cfg->non_block) {
242 int flags = fcntl(fd, F_GETFL, 0);
243 ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
244 if (ret < 0) {
245 ESP_LOGE(TAG, "Failed to configure the socket as non-blocking (errno %d)", errno);
246 goto err_freesocket;
247 }
248 }
249 }
250
251 ret = connect(fd, addr_ptr, addrinfo->ai_addrlen);
252 if (ret < 0 && !(errno == EINPROGRESS && cfg && cfg->non_block)) {
253
254 ESP_LOGE(TAG, "Failed to connnect to host (errno %d)", errno);
255 ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ESP_TLS_ERR_TYPE_SYSTEM, errno);
256 ret = ESP_ERR_ESP_TLS_FAILED_CONNECT_TO_HOST;
257 goto err_freesocket;
258 }
259
260 *sockfd = fd;
261 freeaddrinfo(addrinfo);
262 return ESP_OK;
263
264 err_freesocket:
265 close(fd);
266 err_freeaddr:
267 freeaddrinfo(addrinfo);
268 return ret;
269 }
270
esp_tls_low_level_conn(const char * hostname,int hostlen,int port,const esp_tls_cfg_t * cfg,esp_tls_t * tls)271 static int esp_tls_low_level_conn(const char *hostname, int hostlen, int port, const esp_tls_cfg_t *cfg, esp_tls_t *tls)
272 {
273 if (!tls) {
274 ESP_LOGE(TAG, "empty esp_tls parameter");
275 return -1;
276 }
277 esp_err_t esp_ret;
278 /* These states are used to keep a tab on connection progress in case of non-blocking connect,
279 and in case of blocking connect these cases will get executed one after the other */
280 switch (tls->conn_state) {
281 case ESP_TLS_INIT:
282 tls->sockfd = -1;
283 if (cfg != NULL) {
284 #ifdef CONFIG_ESP_TLS_USING_MBEDTLS
285 mbedtls_net_init(&tls->server_fd);
286 #endif
287 tls->is_tls = true;
288 }
289 if ((esp_ret = esp_tcp_connect(hostname, hostlen, port, &tls->sockfd, tls, cfg)) != ESP_OK) {
290 ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ESP_TLS_ERR_TYPE_ESP, esp_ret);
291 return -1;
292 }
293 if (!cfg) {
294 tls->read = tcp_read;
295 tls->write = tcp_write;
296 ESP_LOGD(TAG, "non-tls connection established");
297 return 1;
298 }
299 if (cfg->non_block) {
300 FD_ZERO(&tls->rset);
301 FD_SET(tls->sockfd, &tls->rset);
302 tls->wset = tls->rset;
303 }
304 tls->conn_state = ESP_TLS_CONNECTING;
305 /* falls through */
306 case ESP_TLS_CONNECTING:
307 if (cfg->non_block) {
308 ESP_LOGD(TAG, "connecting...");
309 struct timeval tv;
310 ms_to_timeval(cfg->timeout_ms, &tv);
311
312 /* In case of non-blocking I/O, we use the select() API to check whether
313 connection has been established or not*/
314 if (select(tls->sockfd + 1, &tls->rset, &tls->wset, NULL,
315 cfg->timeout_ms>0 ? &tv : NULL) == 0) {
316 ESP_LOGD(TAG, "select() timed out");
317 return 0;
318 }
319 if (FD_ISSET(tls->sockfd, &tls->rset) || FD_ISSET(tls->sockfd, &tls->wset)) {
320 int error;
321 socklen_t len = sizeof(error);
322 /* pending error check */
323 if (getsockopt(tls->sockfd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
324 ESP_LOGD(TAG, "Non blocking connect failed");
325 ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ESP_TLS_ERR_TYPE_SYSTEM, errno);
326 ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ESP_TLS_ERR_TYPE_ESP, ESP_ERR_ESP_TLS_SOCKET_SETOPT_FAILED);
327 tls->conn_state = ESP_TLS_FAIL;
328 return -1;
329 }
330 }
331 }
332 /* By now, the connection has been established */
333 esp_ret = create_ssl_handle(hostname, hostlen, cfg, tls);
334 if (esp_ret != ESP_OK) {
335 ESP_LOGE(TAG, "create_ssl_handle failed");
336 ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ESP_TLS_ERR_TYPE_ESP, esp_ret);
337 tls->conn_state = ESP_TLS_FAIL;
338 return -1;
339 }
340 tls->read = _esp_tls_read;
341 tls->write = _esp_tls_write;
342 tls->conn_state = ESP_TLS_HANDSHAKE;
343 /* falls through */
344 case ESP_TLS_HANDSHAKE:
345 ESP_LOGD(TAG, "handshake in progress...");
346 return esp_tls_handshake(tls, cfg);
347 break;
348 case ESP_TLS_FAIL:
349 ESP_LOGE(TAG, "failed to open a new connection");;
350 break;
351 default:
352 ESP_LOGE(TAG, "invalid esp-tls state");
353 break;
354 }
355 return -1;
356 }
357
358 /**
359 * @brief Create a new TLS/SSL connection
360 */
esp_tls_conn_new(const char * hostname,int hostlen,int port,const esp_tls_cfg_t * cfg)361 esp_tls_t *esp_tls_conn_new(const char *hostname, int hostlen, int port, const esp_tls_cfg_t *cfg)
362 {
363 esp_tls_t *tls = esp_tls_init();
364 if (!tls) {
365 return NULL;
366 }
367 /* esp_tls_conn_new() API establishes connection in a blocking manner thus this loop ensures that esp_tls_conn_new()
368 API returns only after connection is established unless there is an error*/
369 size_t start = xTaskGetTickCount();
370 while (1) {
371 int ret = esp_tls_low_level_conn(hostname, hostlen, port, cfg, tls);
372 if (ret == 1) {
373 return tls;
374 } else if (ret == -1) {
375 esp_tls_conn_delete(tls);
376 ESP_LOGE(TAG, "Failed to open new connection");
377 return NULL;
378 } else if (ret == 0 && cfg->timeout_ms >= 0) {
379 size_t timeout_ticks = pdMS_TO_TICKS(cfg->timeout_ms);
380 uint32_t expired = xTaskGetTickCount() - start;
381 if (expired >= timeout_ticks) {
382 esp_tls_conn_delete(tls);
383 ESP_LOGE(TAG, "Failed to open new connection in specified timeout");
384 return NULL;
385 }
386 }
387 }
388 return NULL;
389 }
390
esp_tls_conn_new_sync(const char * hostname,int hostlen,int port,const esp_tls_cfg_t * cfg,esp_tls_t * tls)391 int esp_tls_conn_new_sync(const char *hostname, int hostlen, int port, const esp_tls_cfg_t *cfg, esp_tls_t *tls)
392 {
393 /* esp_tls_conn_new_sync() is a sync alternative to esp_tls_conn_new_async() with symmetric function prototype
394 it is an alternative to esp_tls_conn_new() which is left for compatibility reasons */
395 size_t start = xTaskGetTickCount();
396 while (1) {
397 int ret = esp_tls_low_level_conn(hostname, hostlen, port, cfg, tls);
398 if (ret == 1) {
399 return ret;
400 } else if (ret == -1) {
401 ESP_LOGE(TAG, "Failed to open new connection");
402 return -1;
403 } else if (ret == 0 && cfg->timeout_ms >= 0) {
404 size_t timeout_ticks = pdMS_TO_TICKS(cfg->timeout_ms);
405 uint32_t expired = xTaskGetTickCount() - start;
406 if (expired >= timeout_ticks) {
407 ESP_LOGW(TAG, "Failed to open new connection in specified timeout");
408 ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ESP_TLS_ERR_TYPE_ESP, ESP_ERR_ESP_TLS_CONNECTION_TIMEOUT);
409 return 0;
410 }
411 }
412 }
413 return 0;
414 }
415
416 /*
417 * @brief Create a new TLS/SSL non-blocking connection
418 */
esp_tls_conn_new_async(const char * hostname,int hostlen,int port,const esp_tls_cfg_t * cfg,esp_tls_t * tls)419 int esp_tls_conn_new_async(const char *hostname, int hostlen, int port, const esp_tls_cfg_t *cfg, esp_tls_t *tls)
420 {
421 return esp_tls_low_level_conn(hostname, hostlen, port, cfg, tls);
422 }
423
get_port(const char * url,struct http_parser_url * u)424 static int get_port(const char *url, struct http_parser_url *u)
425 {
426 if (u->field_data[UF_PORT].len) {
427 return strtol(&url[u->field_data[UF_PORT].off], NULL, 10);
428 } else {
429 if (strncasecmp(&url[u->field_data[UF_SCHEMA].off], "http", u->field_data[UF_SCHEMA].len) == 0) {
430 return 80;
431 } else if (strncasecmp(&url[u->field_data[UF_SCHEMA].off], "https", u->field_data[UF_SCHEMA].len) == 0) {
432 return 443;
433 }
434 }
435 return 0;
436 }
437
438 /**
439 * @brief Create a new TLS/SSL connection with a given "HTTP" url
440 */
esp_tls_conn_http_new(const char * url,const esp_tls_cfg_t * cfg)441 esp_tls_t *esp_tls_conn_http_new(const char *url, const esp_tls_cfg_t *cfg)
442 {
443 /* Parse URI */
444 struct http_parser_url u;
445 http_parser_url_init(&u);
446 http_parser_parse_url(url, strlen(url), 0, &u);
447 esp_tls_t *tls = esp_tls_init();
448 if (!tls) {
449 return NULL;
450 }
451 /* Connect to host */
452 if (esp_tls_conn_new_sync(&url[u.field_data[UF_HOST].off], u.field_data[UF_HOST].len,
453 get_port(url, &u), cfg, tls) == 1) {
454 return tls;
455 }
456 esp_tls_conn_delete(tls);
457 return NULL;
458 }
459
460 /**
461 * @brief Create a new non-blocking TLS/SSL connection with a given "HTTP" url
462 */
esp_tls_conn_http_new_async(const char * url,const esp_tls_cfg_t * cfg,esp_tls_t * tls)463 int esp_tls_conn_http_new_async(const char *url, const esp_tls_cfg_t *cfg, esp_tls_t *tls)
464 {
465 /* Parse URI */
466 struct http_parser_url u;
467 http_parser_url_init(&u);
468 http_parser_parse_url(url, strlen(url), 0, &u);
469
470 /* Connect to host */
471 return esp_tls_conn_new_async(&url[u.field_data[UF_HOST].off], u.field_data[UF_HOST].len,
472 get_port(url, &u), cfg, tls);
473 }
474
475 #ifdef CONFIG_ESP_TLS_USING_MBEDTLS
476
esp_tls_get_global_ca_store(void)477 mbedtls_x509_crt *esp_tls_get_global_ca_store(void)
478 {
479 return _esp_tls_get_global_ca_store();
480 }
481
482 #endif /* CONFIG_ESP_TLS_USING_MBEDTLS */
483 #ifdef CONFIG_ESP_TLS_SERVER
484 /**
485 * @brief Create a server side TLS/SSL connection
486 */
esp_tls_server_session_create(esp_tls_cfg_server_t * cfg,int sockfd,esp_tls_t * tls)487 int esp_tls_server_session_create(esp_tls_cfg_server_t *cfg, int sockfd, esp_tls_t *tls)
488 {
489 return _esp_tls_server_session_create(cfg, sockfd, tls);
490 }
491 /**
492 * @brief Close the server side TLS/SSL connection and free any allocated resources.
493 */
esp_tls_server_session_delete(esp_tls_t * tls)494 void esp_tls_server_session_delete(esp_tls_t *tls)
495 {
496 return _esp_tls_server_session_delete(tls);
497 }
498 #endif /* CONFIG_ESP_TLS_SERVER */
499
esp_tls_get_bytes_avail(esp_tls_t * tls)500 ssize_t esp_tls_get_bytes_avail(esp_tls_t *tls)
501 {
502 return _esp_tls_get_bytes_avail(tls);
503 }
504
esp_tls_get_conn_sockfd(esp_tls_t * tls,int * sockfd)505 esp_err_t esp_tls_get_conn_sockfd(esp_tls_t *tls, int *sockfd)
506 {
507 if (!tls || !sockfd) {
508 ESP_LOGE(TAG, "Invalid arguments passed");
509 return ESP_ERR_INVALID_ARG;
510 }
511 *sockfd = tls->sockfd;
512 return ESP_OK;
513 }
514
esp_tls_get_and_clear_last_error(esp_tls_error_handle_t h,int * esp_tls_code,int * esp_tls_flags)515 esp_err_t esp_tls_get_and_clear_last_error(esp_tls_error_handle_t h, int *esp_tls_code, int *esp_tls_flags)
516 {
517 if (!h) {
518 return ESP_ERR_INVALID_STATE;
519 }
520 esp_err_t last_err = h->last_error;
521 if (esp_tls_code) {
522 *esp_tls_code = h->esp_tls_error_code;
523 }
524 if (esp_tls_flags) {
525 *esp_tls_flags = h->esp_tls_flags;
526 }
527 memset(h, 0, sizeof(esp_tls_last_error_t));
528 return last_err;
529 }
530
esp_tls_init_global_ca_store(void)531 esp_err_t esp_tls_init_global_ca_store(void)
532 {
533 return _esp_tls_init_global_ca_store();
534 }
535
esp_tls_set_global_ca_store(const unsigned char * cacert_pem_buf,const unsigned int cacert_pem_bytes)536 esp_err_t esp_tls_set_global_ca_store(const unsigned char *cacert_pem_buf, const unsigned int cacert_pem_bytes)
537 {
538 return _esp_tls_set_global_ca_store(cacert_pem_buf, cacert_pem_bytes);
539 }
540
esp_tls_free_global_ca_store(void)541 void esp_tls_free_global_ca_store(void)
542 {
543 return _esp_tls_free_global_ca_store();
544 }
545