28
28
#endif
29
29
30
30
static const char * TAG = "esp-tls" ;
31
+ #if CONFIG_SSL_USING_MBEDTLS
31
32
static mbedtls_x509_crt * global_cacert = NULL ;
33
+ #else
34
+ static unsigned char * global_cacert = NULL ;
35
+ static unsigned int global_cacert_pem_bytes = 0 ;
36
+ #endif
32
37
33
38
#ifdef ESP_PLATFORM
34
39
#include <esp_log.h>
@@ -67,6 +72,7 @@ static ssize_t tcp_read(esp_tls_t *tls, char *data, size_t datalen)
67
72
68
73
static ssize_t tls_read (esp_tls_t * tls , char * data , size_t datalen )
69
74
{
75
+ #if CONFIG_SSL_USING_MBEDTLS
70
76
ssize_t ret = mbedtls_ssl_read (& tls -> ssl , (unsigned char * )data , datalen );
71
77
if (ret < 0 ) {
72
78
if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY ) {
@@ -76,6 +82,20 @@ static ssize_t tls_read(esp_tls_t *tls, char *data, size_t datalen)
76
82
ESP_LOGE (TAG , "read error :%d:" , ret );
77
83
}
78
84
}
85
+ #else
86
+ size_t ret = wolfSSL_read (tls -> ssl , (unsigned char * )data , datalen );
87
+ if (ret < 0 ) {
88
+ ret = wolfSSL_get_error (tls -> ssl , ret );
89
+ /* peer sent close notify */
90
+ if (ret == WOLFSSL_ERROR_ZERO_RETURN ) {
91
+ return 0 ;
92
+ }
93
+
94
+ if (ret != WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_ERROR_WANT_WRITE ) {
95
+ ESP_LOGE (TAG , "read error :%d:" , ret );
96
+ }
97
+ }
98
+ #endif
79
99
return ret ;
80
100
}
81
101
@@ -146,12 +166,14 @@ static int esp_tcp_connect(const char *host, int hostlen, int port, int *sockfd,
146
166
return ret ;
147
167
}
148
168
169
+
149
170
esp_err_t esp_tls_set_global_ca_store (const unsigned char * cacert_pem_buf , const unsigned int cacert_pem_bytes )
150
171
{
151
172
if (cacert_pem_buf == NULL ) {
152
173
ESP_LOGE (TAG , "cacert_pem_buf is null" );
153
174
return ESP_ERR_INVALID_ARG ;
154
175
}
176
+ #if CONFIG_SSL_USING_MBEDTLS
155
177
if (global_cacert != NULL ) {
156
178
mbedtls_x509_crt_free (global_cacert );
157
179
}
@@ -171,23 +193,43 @@ esp_err_t esp_tls_set_global_ca_store(const unsigned char *cacert_pem_buf, const
171
193
ESP_LOGE (TAG , "mbedtls_x509_crt_parse was partly successful. No. of failed certificates: %d" , ret );
172
194
}
173
195
return ESP_OK ;
196
+ #else
197
+ if (global_cacert != NULL ) {
198
+ esp_tls_free_global_ca_store (global_cacert );
199
+ }
200
+
201
+ global_cacert = (unsigned char * )strndup ((const char * )cacert_pem_buf , cacert_pem_bytes );
202
+ if (!global_cacert )
203
+ return ESP_FAIL ;
204
+
205
+ global_cacert_pem_bytes = cacert_pem_bytes ;
206
+
207
+ return ESP_OK ;
208
+ #endif
174
209
}
175
210
176
- mbedtls_x509_crt * esp_tls_get_global_ca_store ()
211
+ void * esp_tls_get_global_ca_store ()
177
212
{
178
- return global_cacert ;
213
+ return ( void * ) global_cacert ;
179
214
}
180
215
181
216
void esp_tls_free_global_ca_store ()
182
217
{
183
218
if (global_cacert ) {
219
+ #if CONFIG_SSL_USING_MBEDTLS
184
220
mbedtls_x509_crt_free (global_cacert );
185
221
global_cacert = NULL ;
222
+ #else
223
+ free (global_cacert );
224
+ global_cacert = NULL ;
225
+ global_cacert_pem_bytes = 0 ;
226
+ #endif
186
227
}
187
228
}
188
229
189
230
static void verify_certificate (esp_tls_t * tls )
190
231
{
232
+ #if CONFIG_SSL_USING_MBEDTLS
191
233
int flags ;
192
234
char buf [100 ];
193
235
if ((flags = mbedtls_ssl_get_verify_result (& tls -> ssl )) != 0 ) {
@@ -198,13 +240,22 @@ static void verify_certificate(esp_tls_t *tls)
198
240
} else {
199
241
ESP_LOGI (TAG , "Certificate verified." );
200
242
}
243
+ #else
244
+ int flags ;
245
+ if ((flags = wolfSSL_get_verify_result (tls -> ssl )) != WOLFSSL_SUCCESS ) {
246
+ ESP_LOGE (TAG , "Failed to verify peer certificate %d!" , flags );
247
+ } else {
248
+ ESP_LOGI (TAG , "Certificate verified." );
249
+ }
250
+ #endif
201
251
}
202
252
203
- static void mbedtls_cleanup (esp_tls_t * tls )
253
+ static void esp_tls_cleanup (esp_tls_t * tls )
204
254
{
205
255
if (!tls ) {
206
256
return ;
207
257
}
258
+ #if CONFIG_SSL_USING_MBEDTLS
208
259
if (tls -> cacert_ptr != global_cacert ) {
209
260
mbedtls_x509_crt_free (tls -> cacert_ptr );
210
261
}
@@ -217,12 +268,19 @@ static void mbedtls_cleanup(esp_tls_t *tls)
217
268
mbedtls_ctr_drbg_free (& tls -> ctr_drbg );
218
269
mbedtls_ssl_free (& tls -> ssl );
219
270
mbedtls_net_free (& tls -> server_fd );
271
+ #else
272
+ wolfSSL_shutdown (tls -> ssl );
273
+ wolfSSL_free (tls -> ssl );
274
+ close (tls -> sockfd );
275
+ wolfSSL_CTX_free (tls -> ctx );
276
+ wolfSSL_Cleanup ();
277
+ #endif
220
278
}
221
279
222
280
static int create_ssl_handle (esp_tls_t * tls , const char * hostname , size_t hostlen , const esp_tls_cfg_t * cfg )
223
281
{
224
282
int ret ;
225
-
283
+ #if CONFIG_SSL_USING_MBEDTLS
226
284
mbedtls_net_init (& tls -> server_fd );
227
285
tls -> server_fd .fd = tls -> sockfd ;
228
286
mbedtls_ssl_init (& tls -> ssl );
@@ -326,8 +384,74 @@ static int create_ssl_handle(esp_tls_t *tls, const char *hostname, size_t hostle
326
384
327
385
return 0 ;
328
386
exit :
329
- mbedtls_cleanup (tls );
387
+ esp_tls_cleanup (tls );
388
+ return -1 ;
389
+ #else
390
+ ret = wolfSSL_Init ();
391
+ if (ret != WOLFSSL_SUCCESS ) {
392
+ ESP_LOGE (TAG , "Init wolfSSL failed: %d" , ret );
393
+ goto exit ;
394
+ }
395
+
396
+ tls -> ctx = wolfSSL_CTX_new (wolfTLSv1_2_client_method ());
397
+ if (!tls -> ctx ) {
398
+ ESP_LOGE (TAG , "Set wolfSSL ctx failed" );
399
+ goto exit ;
400
+ }
401
+
402
+ #ifdef HAVE_ALPN
403
+ if (cfg -> alpn_protos ) {
404
+ char * * alpn_list = (char * * )cfg -> alpn_protos ;
405
+ for (; * alpn_list != NULL ; alpn_list ++ ) {
406
+ if (wolfSSL_UseALPN (tls -> ssl , * alpn_list , strlen (* alpn_list ), WOLFSSL_ALPN_FAILED_ON_MISMATCH ) != WOLFSSL_SUCCESS ) {
407
+ ESP_LOGE (TAG , "Use wolfSSL ALPN failed" );
408
+ goto exit ;
409
+ }
410
+ }
411
+ }
412
+ #endif
413
+
414
+ if (cfg -> use_global_ca_store == true) {
415
+ wolfSSL_CTX_load_verify_buffer (tls -> ctx , global_cacert , global_cacert_pem_bytes , WOLFSSL_FILETYPE_PEM );
416
+ wolfSSL_CTX_set_verify (tls -> ctx , SSL_VERIFY_PEER , NULL );
417
+ } else if (cfg -> cacert_pem_buf != NULL ) {
418
+ wolfSSL_CTX_load_verify_buffer (tls -> ctx , cfg -> cacert_pem_buf , cfg -> cacert_pem_bytes , WOLFSSL_FILETYPE_PEM );
419
+ wolfSSL_CTX_set_verify (tls -> ctx , SSL_VERIFY_PEER , NULL );
420
+ } else {
421
+ wolfSSL_CTX_set_verify (tls -> ctx , SSL_VERIFY_NONE , NULL );
422
+ }
423
+
424
+ if (cfg -> clientcert_pem_buf != NULL && cfg -> clientkey_pem_buf != NULL ) {
425
+ wolfSSL_CTX_use_certificate_buffer (tls -> ctx , cfg -> clientcert_pem_buf , cfg -> clientcert_pem_bytes , WOLFSSL_FILETYPE_PEM );
426
+ wolfSSL_CTX_use_PrivateKey_buffer (tls -> ctx , cfg -> clientkey_pem_buf , cfg -> clientkey_pem_bytes , WOLFSSL_FILETYPE_PEM );
427
+ } else if (cfg -> clientcert_pem_buf != NULL || cfg -> clientkey_pem_buf != NULL ) {
428
+ ESP_LOGE (TAG , "You have to provide both clientcert_pem_buf and clientkey_pem_buf for mutual authentication\n\n" );
429
+ goto exit ;
430
+ }
431
+
432
+ tls -> ssl = wolfSSL_new (tls -> ctx );
433
+ if (!tls -> ssl ) {
434
+ ESP_LOGE (TAG , "Create wolfSSL failed" );
435
+ goto exit ;
436
+ }
437
+
438
+ #ifdef HAVE_SNI
439
+ /* Hostname set here should match CN in server certificate */
440
+ char * use_host = strndup (hostname , hostlen );
441
+ if (!use_host ) {
442
+ goto exit ;
443
+ }
444
+ wolfSSL_set_tlsext_host_name (tls -> ssl , use_host );
445
+ free (use_host );
446
+ #endif
447
+
448
+ wolfSSL_set_fd (tls -> ssl , tls -> sockfd );
449
+
450
+ return 0 ;
451
+ exit :
452
+ esp_tls_cleanup (tls );
330
453
return -1 ;
454
+ #endif
331
455
}
332
456
333
457
/**
@@ -336,7 +460,7 @@ static int create_ssl_handle(esp_tls_t *tls, const char *hostname, size_t hostle
336
460
void esp_tls_conn_delete (esp_tls_t * tls )
337
461
{
338
462
if (tls != NULL ) {
339
- mbedtls_cleanup (tls );
463
+ esp_tls_cleanup (tls );
340
464
if (tls -> sockfd ) {
341
465
close (tls -> sockfd );
342
466
}
@@ -351,13 +475,23 @@ static ssize_t tcp_write(esp_tls_t *tls, const char *data, size_t datalen)
351
475
352
476
static ssize_t tls_write (esp_tls_t * tls , const char * data , size_t datalen )
353
477
{
478
+ #if CONFIG_SSL_USING_MBEDTLS
354
479
ssize_t ret = mbedtls_ssl_write (& tls -> ssl , (unsigned char * ) data , datalen );
355
480
if (ret < 0 ) {
356
481
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE ) {
357
482
ESP_LOGE (TAG , "write error :%d:" , ret );
358
483
}
359
484
}
360
485
return ret ;
486
+ #else
487
+ ssize_t ret = wolfSSL_write (tls -> ssl , (unsigned char * ) data , datalen );
488
+ if (ret < 0 ) {
489
+ if (ret != WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_ERROR_WANT_WRITE ) {
490
+ ESP_LOGE (TAG , "write error :%d:" , ret );
491
+ }
492
+ }
493
+ return ret ;
494
+ #endif
361
495
}
362
496
363
497
static int esp_tls_low_level_conn (const char * hostname , int hostlen , int port , const esp_tls_cfg_t * cfg , esp_tls_t * tls )
@@ -427,6 +561,7 @@ static int esp_tls_low_level_conn(const char *hostname, int hostlen, int port, c
427
561
/* falls through */
428
562
case ESP_TLS_HANDSHAKE :
429
563
ESP_LOGD (TAG , "handshake in progress..." );
564
+ #if CONFIG_SSL_USING_MBEDTLS
430
565
ret = mbedtls_ssl_handshake (& tls -> ssl );
431
566
if (ret == 0 ) {
432
567
tls -> conn_state = ESP_TLS_DONE ;
@@ -445,6 +580,26 @@ static int esp_tls_low_level_conn(const char *hostname, int hostlen, int port, c
445
580
or MBEDTLS_ERR_SSL_WANT_WRITE during handshake */
446
581
return 0 ;
447
582
}
583
+ #else
584
+ ret = wolfSSL_connect (tls -> ssl );
585
+ if (ret == WOLFSSL_SUCCESS ) {
586
+ tls -> conn_state = ESP_TLS_DONE ;
587
+ return 1 ;
588
+ } else {
589
+ if (ret != WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_ERROR_WANT_WRITE ) {
590
+ ESP_LOGE (TAG , "wolfSSL_connect returned -0x%x" , - ret );
591
+ if (cfg -> cacert_pem_buf != NULL || cfg -> use_global_ca_store == true) {
592
+ /* This is to check whether handshake failed due to invalid certificate*/
593
+ verify_certificate (tls );
594
+ }
595
+ tls -> conn_state = ESP_TLS_FAIL ;
596
+ return -1 ;
597
+ }
598
+ /* Irrespective of blocking or non-blocking I/O, we return on getting wolfSSL_want_read
599
+ or wolfSSL_want_write during handshake */
600
+ return 0 ;
601
+ }
602
+ #endif
448
603
break ;
449
604
case ESP_TLS_FAIL :
450
605
ESP_LOGE (TAG , "failed to open a new connection" );;
@@ -490,9 +645,13 @@ int esp_tls_conn_new_async(const char *hostname, int hostlen, int port, const es
490
645
491
646
size_t esp_tls_get_bytes_avail (esp_tls_t * tls )
492
647
{
648
+ #if CONFIG_SSL_USING_MBEDTLS
493
649
if (!tls ) {
494
650
ESP_LOGE (TAG , "empty arg passed to esp_tls_get_bytes_avail()" );
495
651
return ESP_FAIL ;
496
652
}
497
653
return mbedtls_ssl_get_bytes_avail (& tls -> ssl );
654
+ #else
655
+ return 0 ;
656
+ #endif
498
657
}
0 commit comments