forked from ARMmbed/mbed-os
-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathTLSSocketWrapper.cpp
879 lines (751 loc) · 25.6 KB
/
TLSSocketWrapper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
/*
* Copyright (c) 2018 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "netsocket/TLSSocketWrapper.h"
#include <new>
#include "platform/Callback.h"
#include "drivers/Timer.h"
#include "events/mbed_events.h"
#define TRACE_GROUP "TLSW"
#include "mbed-trace/mbed_trace.h"
#include "mbedtls/debug.h"
#include "mbedtls/platform.h"
#include "mbed_error.h"
#include "rtos/Kernel.h"
#if defined(MBEDTLS_USE_PSA_CRYPTO)
#include "psa/crypto.h"
#endif
// This class requires Mbed TLS SSL/TLS client code
#if defined(MBEDTLS_SSL_CLI_C)
TLSSocketWrapper::TLSSocketWrapper(Socket *transport, const char *hostname, control_transport control) :
_transport(transport),
_connect_transport(control == TRANSPORT_CONNECT || control == TRANSPORT_CONNECT_AND_CLOSE),
_close_transport(control == TRANSPORT_CLOSE || control == TRANSPORT_CONNECT_AND_CLOSE),
_tls_initialized(false),
_handshake_completed(false),
_cacert_allocated(false),
_clicert_allocated(false),
_ssl_conf_allocated(false)
{
#if defined(MBEDTLS_USE_PSA_CRYPTO)
// It is safe to call psa_crypto_init() any number of times as
// defined by the PSA Crypto API. There is no standard "deinit"
// function.
psa_status_t status = psa_crypto_init();
if (status != PSA_SUCCESS) {
tr_err("psa_crypto_init() failed (" PRIu32 ")", status);
}
#endif /* MBEDTLS_USE_PSA_CRYPTO */
#if defined(MBEDTLS_PLATFORM_C)
int ret = mbedtls_platform_setup(nullptr);
if (ret != 0) {
print_mbedtls_error("mbedtls_platform_setup()", ret);
}
#endif /* MBEDTLS_PLATFORM_C */
mbedtls_entropy_init(&_entropy);
DRBG_INIT(&_drbg);
mbedtls_ssl_init(&_ssl);
#if defined(MBEDTLS_X509_CRT_PARSE_C)
mbedtls_pk_init(&_pkctx);
#endif
if (hostname) {
set_hostname(hostname);
}
}
TLSSocketWrapper::~TLSSocketWrapper()
{
if (_transport) {
close();
}
mbedtls_entropy_free(&_entropy);
DRBG_FREE(&_drbg);
mbedtls_ssl_free(&_ssl);
#if defined(MBEDTLS_X509_CRT_PARSE_C)
mbedtls_pk_free(&_pkctx);
set_own_cert(nullptr);
set_ca_chain(nullptr);
#endif
set_ssl_config(nullptr);
#if defined(MBEDTLS_PLATFORM_C)
mbedtls_platform_teardown(nullptr);
#endif /* MBEDTLS_PLATFORM_C */
}
void TLSSocketWrapper::set_hostname(const char *hostname)
{
#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
mbedtls_ssl_set_hostname(&_ssl, hostname);
#endif
}
nsapi_error_t TLSSocketWrapper::set_root_ca_cert(const void *root_ca, size_t len)
{
#if !defined(MBEDTLS_X509_CRT_PARSE_C)
return NSAPI_ERROR_UNSUPPORTED;
#else
mbedtls_x509_crt *crt;
crt = new (std::nothrow) mbedtls_x509_crt;
if (!crt) {
return NSAPI_ERROR_NO_MEMORY;
}
mbedtls_x509_crt_init(crt);
/* Parse CA certification */
int ret;
if ((ret = mbedtls_x509_crt_parse(crt, static_cast<const unsigned char *>(root_ca),
len)) != 0) {
print_mbedtls_error("mbedtls_x509_crt_parse", ret);
mbedtls_x509_crt_free(crt);
delete crt;
return NSAPI_ERROR_PARAMETER;
}
set_ca_chain(crt);
_cacert_allocated = true;
return NSAPI_ERROR_OK;
#endif
}
nsapi_error_t TLSSocketWrapper::set_root_ca_cert(const char *root_ca_pem)
{
return set_root_ca_cert(root_ca_pem, strlen(root_ca_pem) + 1);
}
nsapi_error_t TLSSocketWrapper::append_root_ca_cert(const void *root_ca, size_t len)
{
#if !defined(MBEDTLS_X509_CRT_PARSE_C)
return NSAPI_ERROR_UNSUPPORTED;
#else
mbedtls_x509_crt *crt;
crt = get_ca_chain();
if (!crt) {
/* In no chain is configured create a new one */
return set_root_ca_cert(root_ca, len);
}
/* Append root_ca to the crt chain */
int ret;
if ((ret = mbedtls_x509_crt_parse(crt, static_cast<const unsigned char *>(root_ca),
len)) != 0) {
print_mbedtls_error("mbedtls_x509_crt_parse", ret);
return NSAPI_ERROR_PARAMETER;
}
return NSAPI_ERROR_OK;
#endif
}
nsapi_error_t TLSSocketWrapper::append_root_ca_cert(const char *root_ca_pem)
{
return append_root_ca_cert(root_ca_pem, strlen(root_ca_pem) + 1);
}
nsapi_error_t TLSSocketWrapper::set_root_ca_cert_path(const char *root_ca)
{
#if !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO)
return NSAPI_ERROR_UNSUPPORTED;
#else
mbedtls_x509_crt *crt;
crt = new (std::nothrow) mbedtls_x509_crt;
if (!crt) {
return NSAPI_ERROR_NO_MEMORY;
}
mbedtls_x509_crt_init(crt);
/* Parse CA certification */
int ret = mbedtls_x509_crt_parse_path(crt, root_ca);
if (ret < 0) {
print_mbedtls_error("mbedtls_x509_crt_parse", ret);
mbedtls_x509_crt_free(crt);
delete crt;
return NSAPI_ERROR_PARAMETER;
}
set_ca_chain(crt);
_cacert_allocated = true;
return NSAPI_ERROR_OK;
#endif
}
nsapi_error_t TLSSocketWrapper::set_client_cert_key(const char *client_cert_pem, const char *client_private_key_pem)
{
return set_client_cert_key(client_cert_pem, strlen(client_cert_pem) + 1, client_private_key_pem, strlen(client_private_key_pem) + 1);
}
nsapi_error_t TLSSocketWrapper::set_client_cert_key(const void *client_cert, size_t client_cert_len,
const void *client_private_key_pem, size_t client_private_key_len)
{
#if !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_PK_C)
return NSAPI_ERROR_UNSUPPORTED;
#else
int ret;
mbedtls_x509_crt *crt = new (std::nothrow) mbedtls_x509_crt;
if (!crt) {
return NSAPI_ERROR_NO_MEMORY;
}
mbedtls_x509_crt_init(crt);
if ((ret = mbedtls_x509_crt_parse(crt, static_cast<const unsigned char *>(client_cert),
client_cert_len)) != 0) {
print_mbedtls_error("mbedtls_x509_crt_parse", ret);
mbedtls_x509_crt_free(crt);
delete crt;
return NSAPI_ERROR_PARAMETER;
}
mbedtls_pk_init(&_pkctx);
if ((ret = mbedtls_pk_parse_key(&_pkctx, static_cast<const unsigned char *>(client_private_key_pem),
client_private_key_len, nullptr, 0)) != 0) {
print_mbedtls_error("mbedtls_pk_parse_key", ret);
mbedtls_x509_crt_free(crt);
delete crt;
return NSAPI_ERROR_PARAMETER;
}
set_own_cert(crt);
_clicert_allocated = true;
return NSAPI_ERROR_OK;
#endif /* MBEDTLS_X509_CRT_PARSE_C */
}
#if defined(COMPONENT_SE050) && defined(MBEDTLS_ECDH_ALT) && SSS_HAVE_ALT_SSS
nsapi_error_t TLSSocketWrapper::set_client_cert_key(const void *client_cert, size_t client_cert_len,
sss_object_t *pkeyObject, ex_sss_boot_ctx_t *deviceCtx)
{
#if !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_PK_C)
return NSAPI_ERROR_UNSUPPORTED;
#else
int ret;
mbedtls_x509_crt *crt = new (std::nothrow) mbedtls_x509_crt;
if (!crt) {
return NSAPI_ERROR_NO_MEMORY;
}
mbedtls_x509_crt_init(crt);
if ((ret = mbedtls_x509_crt_parse(crt, static_cast<const unsigned char *>(client_cert),
client_cert_len)) != 0) {
print_mbedtls_error("mbedtls_x509_crt_parse", ret);
mbedtls_x509_crt_free(crt);
delete crt;
return NSAPI_ERROR_PARAMETER;
}
mbedtls_pk_init(&_pkctx);
if ((ret = sss_mbedtls_associate_keypair(&_pkctx, pkeyObject)) != 0) {
print_mbedtls_error("sss_mbedtls_associate_keypair", ret);
mbedtls_x509_crt_free(crt);
delete crt;
return NSAPI_ERROR_PARAMETER;
}
set_own_cert(crt);
_clicert_allocated = true;
static const int CIPHER_SUITES[] = { MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 };
mbedtls_ssl_conf_ciphersuites(get_ssl_config(), CIPHER_SUITES);
_sss_key_pair_ptr = pkeyObject;
_sss_ks_ptr = &deviceCtx->ks;
return NSAPI_ERROR_OK;
#endif /* MBEDTLS_X509_CRT_PARSE_C */
}
#endif
nsapi_error_t TLSSocketWrapper::start_handshake(bool first_call)
{
const char DRBG_PERS[] = "mbed TLS client";
int ret;
if (!_transport) {
return NSAPI_ERROR_NO_SOCKET;
}
if (_tls_initialized) {
return continue_handshake();
}
#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
tr_info("Starting TLS handshake with %s", _ssl.hostname);
#else
tr_info("Starting TLS handshake");
#endif
/*
* Initialize TLS-related stuf.
*/
#if defined(MBEDTLS_CTR_DRBG_C)
if ((ret = mbedtls_ctr_drbg_seed(&_drbg, mbedtls_entropy_func, &_entropy,
(const unsigned char *) DRBG_PERS,
sizeof(DRBG_PERS))) != 0) {
print_mbedtls_error("mbedtls_crt_drbg_init", ret);
return NSAPI_ERROR_AUTH_FAILURE;
}
#elif defined(MBEDTLS_HMAC_DRBG_C)
if ((ret = mbedtls_hmac_drbg_seed(&_drbg, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
mbedtls_entropy_func, &_entropy,
(const unsigned char *) DRBG_PERS,
sizeof(DRBG_PERS))) != 0) {
print_mbedtls_error("mbedtls_hmac_drbg_seed", ret);
return NSAPI_ERROR_AUTH_FAILURE;
}
#else
#error "CTR or HMAC must be defined for TLSSocketWrapper!"
#endif
#if !defined(MBEDTLS_SSL_CONF_RNG)
mbedtls_ssl_conf_rng(get_ssl_config(), DRBG_RANDOM, &_drbg);
#endif
#if MBED_CONF_TLS_SOCKET_DEBUG_LEVEL > 0
mbedtls_ssl_conf_verify(get_ssl_config(), my_verify, nullptr);
mbedtls_ssl_conf_dbg(get_ssl_config(), my_debug, nullptr);
mbedtls_debug_set_threshold(MBED_CONF_TLS_SOCKET_DEBUG_LEVEL);
#endif
tr_debug("mbedtls_ssl_setup()");
if ((ret = mbedtls_ssl_setup(&_ssl, get_ssl_config())) != 0) {
print_mbedtls_error("mbedtls_ssl_setup", ret);
return NSAPI_ERROR_AUTH_FAILURE;
}
_transport->set_blocking(false);
_transport->sigio(mbed::callback(this, &TLSSocketWrapper::event));
#if defined(COMPONENT_SE050) && defined(MBEDTLS_ECDH_ALT) && SSS_HAVE_ALT_SSS
if ((_sss_key_pair_ptr != nullptr) && (_sss_ks_ptr != nullptr)) {
sss_mbedtls_associate_ecdhctx(_ssl.handshake, _sss_key_pair_ptr, _sss_ks_ptr);
}
#endif
// Defines MBEDTLS_SSL_CONF_RECV/SEND/RECV_TIMEOUT define global functions which should be the same for all
// callers of mbedtls_ssl_set_bio_ctx and there should be only one ssl context. If these rules don't apply,
// these defines can't be used.
#if !defined(MBEDTLS_SSL_CONF_RECV) && !defined(MBEDTLS_SSL_CONF_SEND) && !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
mbedtls_ssl_set_bio(&_ssl, this, ssl_send, ssl_recv, nullptr);
#else
mbedtls_ssl_set_bio_ctx(&_ssl, this);
#endif /* !defined(MBEDTLS_SSL_CONF_RECV) && !defined(MBEDTLS_SSL_CONF_SEND) && !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT) */
_tls_initialized = true;
ret = continue_handshake();
if (first_call) {
if (ret == NSAPI_ERROR_ALREADY) {
ret = NSAPI_ERROR_IN_PROGRESS; // If first call should return IN_PROGRESS
}
if (ret == NSAPI_ERROR_IS_CONNECTED) {
ret = NSAPI_ERROR_OK; // If we happened to complete the request on the first call, return OK.
}
}
return ret;
}
nsapi_error_t TLSSocketWrapper::continue_handshake()
{
int ret;
if (_handshake_completed) {
return NSAPI_ERROR_IS_CONNECTED;
}
if (!_tls_initialized) {
return NSAPI_ERROR_NO_CONNECTION;
}
while (true) {
ret = mbedtls_ssl_handshake(&_ssl);
if (_timeout && (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE)) {
uint32_t flag;
flag = _event_flag.wait_any(1, _timeout);
if (flag & osFlagsError) {
break;
}
} else {
break;
}
}
if (ret < 0) {
if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
return NSAPI_ERROR_ALREADY;
} else {
print_mbedtls_error("mbedtls_ssl_handshake", ret);
return NSAPI_ERROR_AUTH_FAILURE;
}
}
#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
/* It also means the handshake is done, time to print info */
tr_info("TLS connection to %s established", _ssl.hostname);
#else
tr_info("TLS connection established");
#endif
#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(FEA_TRACE_SUPPORT) && !defined(MBEDTLS_X509_REMOVE_INFO)
/* Prints the server certificate and verify it. */
const size_t buf_size = 1024;
char *buf = new (std::nothrow) char[buf_size];
if (!buf) {
print_mbedtls_error("new (std::nothrow) char[buf_size] failed in continue_handshake", NSAPI_ERROR_NO_MEMORY);
return NSAPI_ERROR_NO_MEMORY;
}
mbedtls_x509_crt_info(buf, buf_size, "\r ",
mbedtls_ssl_get_peer_cert(&_ssl));
tr_debug("Server certificate:\r\n%s\r\n", buf);
uint32_t flags = mbedtls_ssl_get_verify_result(&_ssl);
if (flags != 0) {
/* Verification failed. */
mbedtls_x509_crt_verify_info(buf, buf_size, "\r ! ", flags);
tr_error("Certificate verification failed:\r\n%s", buf);
} else {
/* Verification succeeded. */
tr_info("Certificate verification passed");
}
delete[] buf;
#endif
_handshake_completed = true;
return NSAPI_ERROR_IS_CONNECTED;
}
nsapi_error_t TLSSocketWrapper::send(const void *data, nsapi_size_t size)
{
int ret;
if (!_transport) {
return NSAPI_ERROR_NO_SOCKET;
}
tr_debug("send %d", size);
while (true) {
if (!_handshake_completed) {
ret = continue_handshake();
if (ret != NSAPI_ERROR_IS_CONNECTED) {
if (ret == NSAPI_ERROR_ALREADY) {
ret = NSAPI_ERROR_WOULD_BLOCK;
}
return ret;
}
}
ret = mbedtls_ssl_write(&_ssl, (const unsigned char *) data, size);
if (_timeout == 0) {
break;
} else if (ret == MBEDTLS_ERR_SSL_WANT_WRITE || ret == MBEDTLS_ERR_SSL_WANT_READ) {
uint32_t flag;
flag = _event_flag.wait_any(1, _timeout);
if (flag & osFlagsError) {
// Timeout break
break;
}
} else {
break;
}
}
if (ret == MBEDTLS_ERR_SSL_WANT_WRITE ||
ret == MBEDTLS_ERR_SSL_WANT_READ) {
// translate to socket error
return NSAPI_ERROR_WOULD_BLOCK;
}
if (ret < 0) {
print_mbedtls_error("mbedtls_ssl_write", ret);
return NSAPI_ERROR_DEVICE_ERROR;
}
return ret; // Assume "non negative errorcode" to be propagated from Socket layer
}
nsapi_size_or_error_t TLSSocketWrapper::sendto(const SocketAddress &, const void *data, nsapi_size_t size)
{
// Ignore the SocketAddress
return send(data, size);
}
nsapi_size_or_error_t TLSSocketWrapper::sendto_control(const SocketAddress &address, const void *data,
nsapi_size_t size, nsapi_msghdr_t *control,
nsapi_size_t control_size)
{
return sendto(address, data, size);
}
nsapi_size_or_error_t TLSSocketWrapper::recv(void *data, nsapi_size_t size)
{
int ret;
if (!_transport) {
return NSAPI_ERROR_NO_SOCKET;
}
while (true) {
if (!_handshake_completed) {
ret = continue_handshake();
if (ret != NSAPI_ERROR_IS_CONNECTED) {
if (ret == NSAPI_ERROR_ALREADY) {
ret = NSAPI_ERROR_WOULD_BLOCK;
}
return ret;
}
}
ret = mbedtls_ssl_read(&_ssl, (unsigned char *) data, size);
if (_timeout == 0) {
break;
} else if (ret == MBEDTLS_ERR_SSL_WANT_WRITE || ret == MBEDTLS_ERR_SSL_WANT_READ) {
uint32_t flag;
flag = _event_flag.wait_any(1, _timeout);
if (flag & osFlagsError) {
// Timeout break
break;
}
} else {
break;
}
}
if (ret == MBEDTLS_ERR_SSL_WANT_WRITE ||
ret == MBEDTLS_ERR_SSL_WANT_READ) {
// translate to socket error
return NSAPI_ERROR_WOULD_BLOCK;
} else if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
/* MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY is not considered as error.
* Just ignore here. Once connection is closed, mbedtls_ssl_read()
* will return 0.
*/
return 0;
} else if (ret < 0) {
print_mbedtls_error("mbedtls_ssl_read", ret);
// There is no mapping of TLS error codes to Socket API so return most generic error to application
return NSAPI_ERROR_DEVICE_ERROR;
}
return ret;
}
nsapi_size_or_error_t TLSSocketWrapper::recvfrom(SocketAddress *address, void *data, nsapi_size_t size)
{
if (address) {
getpeername(address);
}
return recv(data, size);
}
nsapi_size_or_error_t TLSSocketWrapper::recvfrom_control(SocketAddress *address, void *data, nsapi_size_t size,
nsapi_msghdr_t *control, nsapi_size_t control_size)
{
return recvfrom(address, data, size);
}
void TLSSocketWrapper::print_mbedtls_error(MBED_UNUSED const char *name, MBED_UNUSED int err)
{
// Avoid pulling in mbedtls_strerror when trace is not enabled
#if defined FEA_TRACE_SUPPORT && defined MBEDTLS_ERROR_C
char buf[128];
mbedtls_strerror(err, buf, 128);
tr_err("%s() failed: -0x%04x (%d): %s", name, -err, err, buf);
#else
tr_err("%s() failed: -0x%04x (%d)", name, -err, err);
#endif
}
#if MBED_CONF_TLS_SOCKET_DEBUG_LEVEL > 0
void TLSSocketWrapper::my_debug(void *ctx, int level, const char *file, int line,
const char *str)
{
const char *p, *basename;
(void) ctx;
/* Extract basename from file */
for (p = basename = file; *p != '\0'; p++) {
if (*p == '/' || *p == '\\') {
basename = p + 1;
}
}
tr_debug("%s:%04d: |%d| %s", basename, line, level, str);
}
int TLSSocketWrapper::my_verify(void *data, mbedtls_x509_crt *crt, int depth, uint32_t *flags)
{
const uint32_t buf_size = 1024;
char *buf = new char[buf_size];
(void) data;
tr_debug("\nVerifying certificate at depth %d:\n", depth);
mbedtls_x509_crt_info(buf, buf_size - 1, " ", crt);
tr_debug("%s", buf);
if (*flags == 0) {
tr_info("No verification issue for this certificate\n");
} else {
mbedtls_x509_crt_verify_info(buf, buf_size, " ! ", *flags);
tr_info("%s\n", buf);
}
delete[] buf;
return 0;
}
#endif /* MBED_CONF_TLS_SOCKET_DEBUG_LEVEL > 0 */
int TLSSocketWrapper::ssl_recv(void *ctx, unsigned char *buf, size_t len)
{
int recv;
TLSSocketWrapper *my = static_cast<TLSSocketWrapper *>(ctx);
if (!my->_transport) {
return NSAPI_ERROR_NO_SOCKET;
}
recv = my->_transport->recv(buf, len);
if (NSAPI_ERROR_WOULD_BLOCK == recv) {
return MBEDTLS_ERR_SSL_WANT_READ;
} else if (recv < 0) {
tr_error("Socket recv error %d", recv);
}
// Propagate also Socket errors to SSL, it allows negative error codes to be returned here.
return recv;
}
int TLSSocketWrapper::ssl_send(void *ctx, const unsigned char *buf, size_t len)
{
int size = -1;
TLSSocketWrapper *my = static_cast<TLSSocketWrapper *>(ctx);
if (!my->_transport) {
return NSAPI_ERROR_NO_SOCKET;
}
size = my->_transport->send(buf, len);
if (NSAPI_ERROR_WOULD_BLOCK == size) {
return MBEDTLS_ERR_SSL_WANT_WRITE;
} else if (size < 0) {
tr_error("Socket send error %d", size);
}
// Propagate also Socket errors to SSL, it allows negative error codes to be returned here.
return size;
}
#if defined(MBEDTLS_X509_CRT_PARSE_C)
mbedtls_x509_crt *TLSSocketWrapper::get_own_cert()
{
return _clicert;
}
int TLSSocketWrapper::set_own_cert(mbedtls_x509_crt *crt)
{
int ret = 0;
if (_clicert && _clicert_allocated) {
mbedtls_x509_crt_free(_clicert);
delete _clicert;
_clicert_allocated = false;
}
_clicert = crt;
if (crt) {
if ((ret = mbedtls_ssl_conf_own_cert(get_ssl_config(), _clicert, &_pkctx)) != 0) {
print_mbedtls_error("mbedtls_ssl_conf_own_cert", ret);
}
}
return ret;
}
mbedtls_x509_crt *TLSSocketWrapper::get_ca_chain()
{
return _cacert;
}
void TLSSocketWrapper::set_ca_chain(mbedtls_x509_crt *crt)
{
if (_cacert && _cacert_allocated) {
mbedtls_x509_crt_free(_cacert);
delete _cacert;
_cacert_allocated = false;
}
_cacert = crt;
tr_debug("mbedtls_ssl_conf_ca_chain()");
mbedtls_ssl_conf_ca_chain(get_ssl_config(), _cacert, nullptr);
}
#endif /* MBEDTLS_X509_CRT_PARSE_C */
mbedtls_ssl_config *TLSSocketWrapper::get_ssl_config()
{
if (!_ssl_conf) {
int ret;
_ssl_conf = new (std::nothrow) mbedtls_ssl_config;
if (!_ssl_conf) {
return nullptr;
}
mbedtls_ssl_config_init(_ssl_conf);
_ssl_conf_allocated = true;
if ((ret = mbedtls_ssl_config_defaults(_ssl_conf,
MBEDTLS_SSL_IS_CLIENT,
MBEDTLS_SSL_TRANSPORT_STREAM,
MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
print_mbedtls_error("mbedtls_ssl_config_defaults", ret);
set_ssl_config(nullptr);
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_NETWORK_STACK, MBED_ERROR_CODE_OUT_OF_MEMORY), "mbedtls_ssl_config_defaults() failed");
return nullptr;
}
/* It is possible to disable authentication by passing
* MBEDTLS_SSL_VERIFY_NONE in the call to mbedtls_ssl_conf_authmode()
*/
mbedtls_ssl_conf_authmode(get_ssl_config(), MBEDTLS_SSL_VERIFY_REQUIRED);
}
return _ssl_conf;
}
void TLSSocketWrapper::set_ssl_config(mbedtls_ssl_config *conf)
{
if (_ssl_conf && _ssl_conf_allocated) {
mbedtls_ssl_config_free(_ssl_conf);
delete _ssl_conf;
_ssl_conf_allocated = false;
}
_ssl_conf = conf;
}
mbedtls_ssl_context *TLSSocketWrapper::get_ssl_context()
{
return &_ssl;
}
nsapi_error_t TLSSocketWrapper::close()
{
if (!_transport) {
return NSAPI_ERROR_NO_SOCKET;
}
tr_info("Closing TLS");
int ret = 0;
if (_handshake_completed) {
_transport->set_blocking(true);
ret = mbedtls_ssl_close_notify(&_ssl);
if (ret) {
print_mbedtls_error("mbedtls_ssl_close_notify", ret);
}
_handshake_completed = false;
}
if (_close_transport) {
int ret2 = _transport->close();
if (!ret) {
ret = ret2;
}
}
_transport = nullptr;
return ret;
}
nsapi_error_t TLSSocketWrapper::connect(const SocketAddress &address)
{
nsapi_error_t ret = NSAPI_ERROR_OK;
if (!_transport) {
return NSAPI_ERROR_NO_SOCKET;
}
if (!is_handshake_started() && _connect_transport) {
ret = _transport->connect(address);
if (ret && ret != NSAPI_ERROR_IS_CONNECTED) {
return ret;
}
}
return start_handshake(ret == NSAPI_ERROR_OK);
}
nsapi_error_t TLSSocketWrapper::bind(const SocketAddress &address)
{
if (!_transport) {
return NSAPI_ERROR_NO_SOCKET;
}
return _transport->bind(address);
}
void TLSSocketWrapper::set_blocking(bool blocking)
{
set_timeout(blocking ? -1 : 0);
}
void TLSSocketWrapper::set_timeout(int timeout)
{
_timeout = timeout;
if (!is_handshake_started() && timeout != -1 && _connect_transport) {
// If we have not yet connected the transport, we need to modify its blocking mode as well.
// After connection is initiated, it is already set to non blocking mode
_transport->set_timeout(timeout);
}
}
void TLSSocketWrapper::sigio(mbed::Callback<void()> func)
{
if (!_transport) {
return;
}
_sigio = func;
_transport->sigio(mbed::callback(this, &TLSSocketWrapper::event));
}
nsapi_error_t TLSSocketWrapper::setsockopt(int level, int optname, const void *optval, unsigned optlen)
{
if (!_transport) {
return NSAPI_ERROR_NO_SOCKET;
}
return _transport->setsockopt(level, optname, optval, optlen);
}
nsapi_error_t TLSSocketWrapper::getsockopt(int level, int optname, void *optval, unsigned *optlen)
{
if (!_transport) {
return NSAPI_ERROR_NO_SOCKET;
}
return _transport->getsockopt(level, optname, optval, optlen);
}
Socket *TLSSocketWrapper::accept(nsapi_error_t *err)
{
if (err) {
*err = NSAPI_ERROR_UNSUPPORTED;
}
return nullptr;
}
nsapi_error_t TLSSocketWrapper::listen(int)
{
return NSAPI_ERROR_UNSUPPORTED;
}
void TLSSocketWrapper::event()
{
_event_flag.set(1);
if (_sigio) {
_sigio();
}
}
bool TLSSocketWrapper::is_handshake_started() const
{
return _tls_initialized;
}
nsapi_error_t TLSSocketWrapper::getpeername(SocketAddress *address)
{
if (!_handshake_completed) {
return NSAPI_ERROR_NO_CONNECTION;
}
return _transport->getpeername(address);
}
#endif /* MBEDTLS_SSL_CLI_C */