Select one of the symbols to view example projects that use it.
 
Outline
#include "utils/includes.h"
#include "utils/common.h"
#include "crypto/tls.h"
#include "crypto/sha1.h"
#include "crypto/md5.h"
#include "crypto/sha256.h"
#include "crypto/sha384.h"
#define MBEDTLS_ALLOW_PRIVATE_ACCESS
#include "ssl_misc.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/entropy.h"
#include "mbedtls/debug.h"
#include "mbedtls/oid.h"
#include "mbedtls/esp_debug.h"
#include "mbedtls/esp_config.h"
#include "mbedtls/config.h"
#include "mbedtls/platform.h"
#include "eap_peer/eap.h"
#include "psa/crypto.h"
#include "md_psa.h"
#include "ssl_tls13_keys.h"
#define TLS_RANDOM_LEN
#define TLS_HASH_MAX_SIZE
#define TLS_MASTER_SECRET_LEN
#define MAX_CIPHERSUITE
#define MAX_EXPORTER_CONTEXT_LEN
tls_instance_count
tls_data
tls_context
tls_connection
tls_mbedtls_cleanup(tls_context_t *)
tls_mbedtls_conn_delete(tls_context_t *)
tls_mbedtls_write(void *, const unsigned char *, size_t)
tls_mbedtls_read(void *, unsigned char *, size_t)
set_pki_context(tls_context_t *, const struct tls_connection_params *)
set_ca_cert(tls_context_t *, const unsigned char *, size_t)
tls_sig_algs_for_eap
eap_mbedtls_x509_crt_profile
tls_enable_sha1_config(tls_context_t *)
tls_set_ciphersuite(const struct tls_connection_params *, tls_context_t *)
set_client_config(const struct tls_connection_params *, tls_context_t *)
tls_key_derivation(void *, mbedtls_ssl_key_export_type, const unsigned char *, size_t, const unsigned char *, const unsigned char *, mbedtls_tls_prf_types)
tls_create_mbedtls_handle(struct tls_connection *, const struct tls_connection_params *, tls_context_t *)
tls_init(const struct tls_config *)
tls_deinit(void *)
tls_connection_init(void *)
tls_connection_deinit(void *, struct tls_connection *)
tls_get_errors(void *)
tls_connection_established(void *, struct tls_connection *)
tls_global_set_verify(void *, int, int)
tls_connection_set_verify(void *, struct tls_connection *, int, unsigned int, const u8 *, size_t)
tls_connection_handshake(void *, struct tls_connection *, const struct wpabuf *, struct wpabuf **)
tls_connection_server_handshake(void *, struct tls_connection *, const struct wpabuf *, struct wpabuf **)
tls_connection_encrypt(void *, struct tls_connection *, const struct wpabuf *)
tls_connection_decrypt(void *, struct tls_connection *, const struct wpabuf *)
tls_connection_resumed(void *, struct tls_connection *)
tls_connection_set_cipher_list(void *, struct tls_connection *, u8 *)
tls_get_version(void *, struct tls_connection *, char *, size_t)
tls_get_cipher(void *, struct tls_connection *, char *, size_t)
tls_connection_enable_workaround(void *, struct tls_connection *)
tls_connection_get_failed(void *, struct tls_connection *)
tls_connection_get_read_alerts(void *, struct tls_connection *)
tls_connection_get_write_alerts(void *, struct tls_connection *)
tls_connection_set_params(void *, struct tls_connection *, const struct tls_connection_params *)
tls_global_set_params(void *, const struct tls_connection_params *)
tls_connection_set_session_ticket_cb(void *, struct tls_connection *, tls_session_ticket_cb, void *)
tls_connection_prf(void *, struct tls_connection *, const char *, const u8 *, size_t, int, u8 *, size_t)
tls_connection_export_key(void *, struct tls_connection *, const char *, const u8 *, size_t, u8 *, size_t)
tls_connection_get_eap_fast_key(void *, struct tls_connection *, u8 *, size_t)
tls_connection_client_hello_ext(void *, struct tls_connection *, int, const u8 *, size_t)
tls_connection_shutdown(void *, struct tls_connection *)
tls_connection_get_random(void *, struct tls_connection *, struct tls_random *)
Files
loading (4/5)...
SourceVuESP-IDF Framework and ExamplesESP-IDFcomponents/wpa_supplicant/esp_supplicant/src/crypto/tls_mbedtls.c
 
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
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 *//* ... */ #include "utils/includes.h" #include "utils/common.h" #include "crypto/tls.h" #include "crypto/sha1.h" #include "crypto/md5.h" #include "crypto/sha256.h" #include "crypto/sha384.h"7 includes /* TODO: Remove this once the appropriate solution is found * * ssl_misc.h header uses private elements from * mbedtls, which become undefined if the following flag * is not defined *//* ... */ #define MBEDTLS_ALLOW_PRIVATE_ACCESS // located at mbedtls/library/ssl_misc.h #include "ssl_misc.h" #include "mbedtls/ctr_drbg.h" #include "mbedtls/entropy.h" #include "mbedtls/debug.h" #include "mbedtls/oid.h"5 includes #ifdef ESPRESSIF_USE #include "mbedtls/esp_debug.h" #include "mbedtls/esp_config.h"/* ... */ #else #include "mbedtls/config.h" #endif #include "mbedtls/platform.h" #include "eap_peer/eap.h" #ifdef CONFIG_TLSV13 #include "psa/crypto.h" #include "md_psa.h" #include "ssl_tls13_keys.h" #define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \ psa_to_ssl_errors, \ psa_generic_status_to_mbedtls)... /* ... */#endif /* CONFIG_TLSV13 */ #define TLS_RANDOM_LEN 32 #define TLS_HASH_MAX_SIZE 64 #define TLS_MASTER_SECRET_LEN 48 #define MAX_CIPHERSUITE 32 #define MAX_EXPORTER_CONTEXT_LEN 655355 defines /* Throw a compilation error if basic requirements in mbedtls are not enabled */ #if !defined(MBEDTLS_SSL_TLS_C) #error "TLS not enabled in mbedtls config" #endif #if !defined(MBEDTLS_SHA256_C) #error "SHA256 is disabled in mbedtls config" #endif #if !defined(MBEDTLS_AES_C) #error "AES support is disabled in mbedtls config" #endif uint32_t tls_instance_count; struct tls_data { /* Data for mbedlts */ struct wpabuf *in_data; /* Data from mbedtls */ struct wpabuf *out_data; }{ ... }; typedef struct tls_context { mbedtls_ssl_context ssl; /*!< TLS/SSL context */ mbedtls_entropy_context entropy; /*!< mbedTLS entropy context structure */ mbedtls_ctr_drbg_context ctr_drbg; /*!< mbedTLS ctr drbg context structure */ mbedtls_ssl_config conf; /*!< TLS/SSL config to be shared structures */ mbedtls_x509_crt cacert; /*!< Container for X.509 CA certificate */ mbedtls_x509_crt *cacert_ptr; /*!< Pointer to the cacert being used. */ mbedtls_x509_crt clientcert; /*!< Container for X.509 client certificate */ mbedtls_pk_context clientkey; /*!< Private key of client certificate */ int ciphersuite[MAX_CIPHERSUITE]; }{ ... } tls_context_t; struct tls_connection { tls_context_t *tls; struct tls_data tls_io_data; unsigned char master_secret[TLS_MASTER_SECRET_LEN]; unsigned char randbytes[2 * TLS_RANDOM_LEN]; mbedtls_tls_prf_types tls_prf_type; #ifdef CONFIG_TLSV13 unsigned char exporter_master_secret[TLS_HASH_MAX_SIZE]; #endif /* CONFIG_TLSV13 */ }{ ... }; static void tls_mbedtls_cleanup(tls_context_t *tls) { if (!tls) { return; }{...} tls->cacert_ptr = NULL; mbedtls_x509_crt_free(&tls->cacert); mbedtls_x509_crt_free(&tls->clientcert); mbedtls_pk_free(&tls->clientkey); mbedtls_entropy_free(&tls->entropy); mbedtls_ssl_config_free(&tls->conf); mbedtls_ctr_drbg_free(&tls->ctr_drbg); mbedtls_ssl_free(&tls->ssl); }{ ... } static void tls_mbedtls_conn_delete(tls_context_t *tls) { if (tls != NULL) { tls_mbedtls_cleanup(tls); }{...} }{ ... } static int tls_mbedtls_write(void *ctx, const unsigned char *buf, size_t len) { struct tls_connection *conn = (struct tls_connection *)ctx; struct tls_data *data = &conn->tls_io_data; if (wpabuf_resize(&data->out_data, len) < 0) { return 0; }{...} wpabuf_put_data(data->out_data, buf, len); return len; }{ ... } static int tls_mbedtls_read(void *ctx, unsigned char *buf, size_t len) { struct tls_connection *conn = (struct tls_connection *)ctx; struct tls_data *data = &conn->tls_io_data; struct wpabuf *local_buf; if (data->in_data == NULL || len > wpabuf_len(data->in_data)) { /* We don't have suffient buffer available for read */ wpa_printf(MSG_INFO, "len=%zu not available in input", len); return MBEDTLS_ERR_SSL_WANT_READ; }{...} os_memcpy(buf, wpabuf_head(data->in_data), len); /* adjust buffer */ if (len < wpabuf_len(data->in_data)) { /* TODO optimize this operation */ local_buf = wpabuf_alloc_copy(wpabuf_mhead_u8(data->in_data) + len, wpabuf_len(data->in_data) - len); wpabuf_free(data->in_data); data->in_data = local_buf; }{...} else { wpabuf_free(data->in_data); data->in_data = NULL; }{...} return len; }{ ... } static int set_pki_context(tls_context_t *tls, const struct tls_connection_params *cfg) { int ret; if (cfg->client_cert_blob == NULL || cfg->private_key_blob == NULL) { wpa_printf(MSG_ERROR, "%s: config not correct", __func__); return -1; }{...} mbedtls_x509_crt_init(&tls->clientcert); mbedtls_pk_init(&tls->clientkey); ret = mbedtls_x509_crt_parse(&tls->clientcert, cfg->client_cert_blob, cfg->client_cert_blob_len); if (ret < 0) { wpa_printf(MSG_ERROR, "mbedtls_x509_crt_parse returned -0x%x", -ret); return ret; }{...} ret = mbedtls_pk_parse_key(&tls->clientkey, cfg->private_key_blob, cfg->private_key_blob_len, (const unsigned char *)cfg->private_key_passwd, cfg->private_key_passwd ? os_strlen(cfg->private_key_passwd) : 0, mbedtls_ctr_drbg_random, &tls->ctr_drbg); if (ret < 0) { wpa_printf(MSG_ERROR, "mbedtls_pk_parse_keyfile returned -0x%x", -ret); return ret; }{...} ret = mbedtls_ssl_conf_own_cert(&tls->conf, &tls->clientcert, &tls->clientkey); if (ret < 0) { wpa_printf(MSG_ERROR, "mbedtls_ssl_conf_own_cert returned -0x%x", -ret); return ret; }{...} return 0; }{ ... } static int set_ca_cert(tls_context_t *tls, const unsigned char *cacert, size_t cacert_len) { tls->cacert_ptr = &tls->cacert; mbedtls_x509_crt_init(tls->cacert_ptr); int ret = mbedtls_x509_crt_parse(tls->cacert_ptr, cacert, cacert_len); if (ret < 0) { wpa_printf(MSG_ERROR, "mbedtls_x509_crt_parse returned -0x%x", -ret); return ret; }{...} mbedtls_ssl_conf_authmode(&tls->conf, MBEDTLS_SSL_VERIFY_REQUIRED); mbedtls_ssl_conf_ca_chain(&tls->conf, tls->cacert_ptr, NULL); return 0; }{ ... } #ifdef CONFIG_SUITEB192 static uint16_t tls_sig_algs_for_suiteb[] = { #ifdef CONFIG_TLSV13 #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \ defined(MBEDTLS_MD_CAN_SHA384) && \ defined(PSA_WANT_ECC_SECP_R1_384) MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384)/* ... */ #endif #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \ defined(MBEDTLS_MD_CAN_SHA512) && \ defined(PSA_WANT_ECC_SECP_R1_521) MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512, // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512)/* ... */ #endif #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \ defined(MBEDTLS_MD_CAN_SHA512) MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, #endif \ /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_MD_CAN_SHA512 */ #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \ defined(MBEDTLS_MD_CAN_SHA384) MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, #endif \ /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_MD_CAN_SHA384 */ #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA512) MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512, #endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA512 */ #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA384) MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384, #endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA384 *//* ... */ #endif /* CONFIG_TLSV13 */ #if defined(MBEDTLS_SSL_PROTO_TLS1_2) #if defined(MBEDTLS_SHA512_C) #if defined(MBEDTLS_ECDSA_C) MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512), MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),/* ... */ #endif #if defined(MBEDTLS_RSA_C) MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512), MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),/* ... */ #endif/* ... */ #endif /* MBEDTLS_SHA512_C *//* ... */ #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ MBEDTLS_TLS_SIG_NONE }{...}; const mbedtls_x509_crt_profile suiteb_mbedtls_x509_crt_profile = { #if defined(MBEDTLS_SHA512_C) MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512) |/* ... */ #endif 0, 0xFFFFFFF, /* Any PK alg */ MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1), 1024, }{...}; static void tls_set_suiteb_config(tls_context_t *tls) { const mbedtls_x509_crt_profile *crt_profile = &suiteb_mbedtls_x509_crt_profile; mbedtls_ssl_conf_cert_profile(&tls->conf, crt_profile); mbedtls_ssl_conf_sig_algs(&tls->conf, tls_sig_algs_for_suiteb); }{...} /* ... */#endif static uint16_t tls_sig_algs_for_eap[] = { #ifdef CONFIG_TLSV13 #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \ defined(MBEDTLS_MD_CAN_SHA256) && \ defined(PSA_WANT_ECC_SECP_R1_256) MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256)/* ... */ #endif #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \ defined(MBEDTLS_MD_CAN_SHA384) && \ defined(PSA_WANT_ECC_SECP_R1_384) MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384)/* ... */ #endif #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \ defined(MBEDTLS_MD_CAN_SHA512) && \ defined(PSA_WANT_ECC_SECP_R1_521) MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512, // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512)/* ... */ #endif #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \ defined(MBEDTLS_MD_CAN_SHA512) MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, #endif \ /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_MD_CAN_SHA512 */ #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \ defined(MBEDTLS_MD_CAN_SHA384) MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, #endif \ /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_MD_CAN_SHA384 */ #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \ defined(MBEDTLS_MD_CAN_SHA256) MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, #endif \ /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_MD_CAN_SHA256 */ #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA512) MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512, #endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA512 */ #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA384) MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384, #endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA384 */ #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA256) MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, #endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256 *//* ... */ #endif /* CONFIG_TLSV13 */ #if defined(MBEDTLS_SSL_PROTO_TLS1_2) #if defined(MBEDTLS_SHA512_C) #if defined(MBEDTLS_ECDSA_C) MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512), MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),/* ... */ #endif #if defined(MBEDTLS_RSA_C) MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512), MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),/* ... */ #endif/* ... */ #endif /* MBEDTLS_SHA512_C */ #if defined(MBEDTLS_SHA256_C) #if defined(MBEDTLS_ECDSA_C) MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256), MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA224),/* ... */ #endif #if defined(MBEDTLS_RSA_C) MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256), MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA224),/* ... */ #endif/* ... */ #endif /* MBEDTLS_SHA256_C */ #if defined(MBEDTLS_SHA1_C) #if defined(MBEDTLS_ECDSA_C) MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA1), #endif #if defined(MBEDTLS_RSA_C) MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA1), #endif/* ... */ #endif /* MBEDTLS_SHA1_C *//* ... */ #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ MBEDTLS_TLS_SIG_NONE }{...}; const mbedtls_x509_crt_profile eap_mbedtls_x509_crt_profile = { #if defined(MBEDTLS_SHA1_C) MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1) | #endif #if defined(MBEDTLS_SHA256_C) MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224) | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |/* ... */ #endif #if defined(MBEDTLS_SHA512_C) MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512) |/* ... */ #endif 0, 0xFFFFFFF, /* Any PK alg */ 0xFFFFFFF, /* Any curve */ 1024, }{...}; static void tls_enable_sha1_config(tls_context_t *tls) { const mbedtls_x509_crt_profile *crt_profile = &eap_mbedtls_x509_crt_profile; mbedtls_ssl_conf_cert_profile(&tls->conf, crt_profile); mbedtls_ssl_conf_sig_algs(&tls->conf, tls_sig_algs_for_eap); }{ ... } #ifdef CONFIG_ESP_WIFI_DISABLE_KEY_USAGE_CHECK static int tls_disable_key_usages(void *data, mbedtls_x509_crt *cert, int depth, uint32_t *flags) { cert->MBEDTLS_PRIVATE(ext_types) &= ~MBEDTLS_X509_EXT_KEY_USAGE; cert->MBEDTLS_PRIVATE(ext_types) &= ~MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE; return 0; }{...} /* ... */#endif /*CONFIG_ESP_WIFI_DISABLE_KEY_USAGE_CHECK*/ #ifdef CONFIG_SUITEB192 static const int suiteb_rsa_ciphersuite_preference[] = { #if defined(CONFIG_ESP_WIFI_EAP_TLS1_3) MBEDTLS_TLS1_3_AES_256_GCM_SHA384, #endif /* CONFIG_ESP_WIFI_EAP_TLS1_3 */ #if defined(MBEDTLS_GCM_C) #if defined(MBEDTLS_SHA512_C) MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, MBEDTLS_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384,/* ... */ #endif/* ... */ #endif 0 }{...}; static const int suiteb_ecc_ciphersuite_preference[] = { #if defined(CONFIG_ESP_WIFI_EAP_TLS1_3) MBEDTLS_TLS1_3_AES_256_GCM_SHA384, #endif /* CONFIG_ESP_WIFI_EAP_TLS1_3 */ #if defined(MBEDTLS_GCM_C) #if defined(MBEDTLS_SHA512_C) MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, #endif/* ... */ #endif 0 }{...}; static const int suiteb_ciphersuite_preference[] = { #if defined(CONFIG_ESP_WIFI_EAP_TLS1_3) MBEDTLS_TLS1_3_AES_256_GCM_SHA384, #endif /* CONFIG_ESP_WIFI_EAP_TLS1_3 */ #if defined(MBEDTLS_GCM_C) #if defined(MBEDTLS_SHA512_C) MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, MBEDTLS_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384,/* ... */ #endif/* ... */ #endif 0 }{...};/* ... */ #endif static void tls_set_ciphersuite(const struct tls_connection_params *cfg, tls_context_t *tls) { /* Only set ciphersuite if cert's key length is high or ciphersuites are set by user */ #ifdef CONFIG_SUITEB192 if (cfg->flags & TLS_CONN_SUITEB) { /* cipher suites will be set based on certificate */ mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(&tls->clientkey); if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) { mbedtls_ssl_conf_ciphersuites(&tls->conf, suiteb_rsa_ciphersuite_preference); }{...} else if (pk_alg == MBEDTLS_PK_ECDSA || pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) { mbedtls_ssl_conf_ciphersuites(&tls->conf, suiteb_ecc_ciphersuite_preference); }{...} else { mbedtls_ssl_conf_ciphersuites(&tls->conf, suiteb_ciphersuite_preference); }{...} }{...} else/* ... */ #endif /* Set cipher suites if User has explicitly set those * TODO: public API to set EAP ciphers *//* ... */ if (tls->ciphersuite[0]) { mbedtls_ssl_conf_ciphersuites(&tls->conf, tls->ciphersuite); }{...} }{ ... } static int set_client_config(const struct tls_connection_params *cfg, tls_context_t *tls) { int ret; int preset = MBEDTLS_SSL_PRESET_DEFAULT; assert(cfg != NULL); assert(tls != NULL); #ifdef CONFIG_SUITEB192 if (cfg->flags & TLS_CONN_SUITEB) { preset = MBEDTLS_SSL_PRESET_SUITEB; }{...} #endif/* ... */ ret = mbedtls_ssl_config_defaults(&tls->conf, MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM, preset); if (ret != 0) { wpa_printf(MSG_ERROR, "mbedtls_ssl_config_defaults returned -0x%x", -ret); return ret; }{...} if (preset != MBEDTLS_SSL_PRESET_SUITEB) { /* Enable SHA1 support since it's not enabled by default in mbedtls */ tls_enable_sha1_config(tls); #ifdef CONFIG_SUITEB192 }{...} else { tls_set_suiteb_config(tls); #endif }{...} if (cfg->ca_cert_blob != NULL) { ret = set_ca_cert(tls, cfg->ca_cert_blob, cfg->ca_cert_blob_len); if (ret != 0) { return ret; }{...} }{...} else { mbedtls_ssl_conf_authmode(&tls->conf, MBEDTLS_SSL_VERIFY_NONE); }{...} if (cfg->client_cert_blob != NULL && cfg->private_key_blob != NULL) { ret = set_pki_context(tls, cfg); if (ret != 0) { wpa_printf(MSG_ERROR, "Failed to set client pki context"); return ret; }{...} }{...} /* Usages of default ciphersuites can take a lot of time on low end device * and can cause watchdog. Enabling the ciphers which are secured enough * but doesn't take that much processing power *//* ... */ tls_set_ciphersuite(cfg, tls); #ifdef CONFIG_ESP_WIFI_DISABLE_KEY_USAGE_CHECK mbedtls_ssl_set_verify(&tls->ssl, tls_disable_key_usages, NULL); #endif /*CONFIG_ESP_WIFI_DISABLE_KEY_USAGE_CHECK*/ #ifdef CONFIG_MBEDTLS_CERTIFICATE_BUNDLE if (cfg->flags & TLS_CONN_USE_DEFAULT_CERT_BUNDLE) { wpa_printf(MSG_INFO, "Using default cert bundle"); if (esp_crt_bundle_attach_fn) { ret = (*esp_crt_bundle_attach_fn)(&tls->conf); }{...} if (ret != 0) { wpa_printf(MSG_ERROR, "Failed to set default cert bundle"); return ret; }{...} }{...} #endif/* ... */ return 0; }{ ... } #ifdef CONFIG_TLSV13 static void tls13_extract_exporter_master_secret(struct tls_connection *conn) { mbedtls_ssl_context *ssl = &conn->tls->ssl; const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info; psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(ciphersuite_info->mac); size_t hash_len = PSA_HASH_LENGTH(hash_alg); assert(hash_len != 0); mbedtls_ssl_tls13_application_secrets *app_secrets = &ssl->session_negotiate->app_secrets; os_memcpy(conn->exporter_master_secret, app_secrets->exporter_master_secret, hash_len); }{...} /* ... */#endif /* CONFIG_TLSV13 */ static void tls_key_derivation(void *ctx, mbedtls_ssl_key_export_type secret_type, const unsigned char *secret, size_t secret_len, const unsigned char client_random[TLS_RANDOM_LEN], const unsigned char server_random[TLS_RANDOM_LEN], mbedtls_tls_prf_types tls_prf_type) { struct tls_connection *conn = (struct tls_connection *)ctx; os_memcpy(conn->master_secret, secret, sizeof(conn->master_secret)); os_memcpy(conn->randbytes, client_random, TLS_RANDOM_LEN); os_memcpy(conn->randbytes + 32, server_random, TLS_RANDOM_LEN); conn->tls_prf_type = tls_prf_type; #ifdef CONFIG_TLSV13 if (secret_type == MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_APPLICATION_TRAFFIC_SECRET) { tls13_extract_exporter_master_secret(conn); }{...} #endif/* ... */ /* CONFIG_TLSV13 */ }{ ... } static int tls_create_mbedtls_handle(struct tls_connection *conn, const struct tls_connection_params *params, tls_context_t *tls) { int ret; assert(params != NULL); assert(tls != NULL); mbedtls_ssl_init(&tls->ssl); mbedtls_ctr_drbg_init(&tls->ctr_drbg); mbedtls_ssl_config_init(&tls->conf); mbedtls_entropy_init(&tls->entropy); ret = set_client_config(params, tls); if (ret != 0) { wpa_printf(MSG_ERROR, "Failed to set client configurations"); goto exit; }{...} ret = mbedtls_ctr_drbg_seed(&tls->ctr_drbg, mbedtls_entropy_func, &tls->entropy, NULL, 0); if (ret != 0) { wpa_printf(MSG_ERROR, "mbedtls_ctr_drbg_seed returned -0x%x", -ret); goto exit; }{...} mbedtls_ssl_conf_rng(&tls->conf, mbedtls_ctr_drbg_random, &tls->ctr_drbg); #if defined(CONFIG_MBEDTLS_SSL_PROTO_TLS1_3) && !defined(CONFIG_TLSV13) /* Disable TLSv1.3 even when enabled in MbedTLS and not enabled in WiFi config. * TODO: Remove Kconfig option for TLSv1.3 when it is matured enough *//* ... */ mbedtls_ssl_conf_max_tls_version(&tls->conf, MBEDTLS_SSL_VERSION_TLS1_2);/* ... */ #endif /* CONFIG_MBEDTLS_SSL_PROTO_TLS1_3 && !CONFIG_TLSV13 */ ret = mbedtls_ssl_setup(&tls->ssl, &tls->conf); if (ret != 0) { wpa_printf(MSG_ERROR, "mbedtls_ssl_setup returned -0x%x", -ret); goto exit; }{...} mbedtls_ssl_set_export_keys_cb(&tls->ssl, tls_key_derivation, conn); #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) /* Disable BEAST attack countermeasures for Windows 2008 interoperability */ mbedtls_ssl_conf_cbc_record_splitting(&tls->conf, MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED);/* ... */ #endif /* Enable debug prints in case supplicant's prints are enabled */ #if defined(CONFIG_MBEDTLS_DEBUG) && defined(ESPRESSIF_USE) mbedtls_esp_enable_debug_log(&tls->conf, 2); #endif return 0; exit: tls_mbedtls_cleanup(tls); return ret; }{ ... } void *tls_init(const struct tls_config *conf) { tls_instance_count++; return &tls_instance_count; }{ ... } void tls_deinit(void *tls_ctx) { tls_instance_count--; }{ ... } struct tls_connection * tls_connection_init(void *tls_ctx) { struct tls_connection *conn = os_zalloc(sizeof(*conn)); if (!conn) { wpa_printf(MSG_ERROR, "TLS: Failed to allocate connection memory"); return NULL; }{...} #ifdef CONFIG_TLSV13 psa_status_t status = psa_crypto_init(); if (status != PSA_SUCCESS) { wpa_printf(MSG_ERROR, "Failed to initialize PSA crypto, returned %d", (int) status); return NULL; }{...} #endif/* ... */ /* CONFIG_TLSV13 */ return conn; }{ ... } void tls_connection_deinit(void *tls_ctx, struct tls_connection *conn) { /* case: tls init failed */ if (!conn) { return; }{...} /* Free ssl ctx and data */ tls_mbedtls_conn_delete((tls_context_t *) conn->tls); os_free(conn->tls); conn->tls = NULL; /* Data in in ssl ctx, free connection */ os_free(conn); }{ ... } int tls_get_errors(void *tls_ctx) { return 0; }{ ... } int tls_connection_established(void *tls_ctx, struct tls_connection *conn) { mbedtls_ssl_context *ssl = &conn->tls->ssl; return mbedtls_ssl_is_handshake_over(ssl); }{ ... } int tls_global_set_verify(void *tls_ctx, int check_crl, int strict) { wpa_printf(MSG_INFO, "TLS: global settings are not supported"); return -1; }{ ... } int tls_connection_set_verify(void *tls_ctx, struct tls_connection *conn, int verify_peer, unsigned int flags, const u8 *session_ctx, size_t session_ctx_len) { wpa_printf(MSG_INFO, "TLS: tls_connection_set_verify not supported"); return -1; }{ ... } #ifdef CONFIG_ESP_WIFI_ENT_FREE_DYNAMIC_BUFFER static void esp_mbedtls_free_dhm(mbedtls_ssl_context *ssl) { #ifdef CONFIG_MBEDTLS_DHM_C const mbedtls_ssl_config *conf = mbedtls_ssl_context_get_config(ssl); mbedtls_mpi_free((mbedtls_mpi *)&conf->MBEDTLS_PRIVATE(dhm_P)); mbedtls_mpi_free((mbedtls_mpi *)&conf->MBEDTLS_PRIVATE(dhm_G));/* ... */ #endif /* CONFIG_MBEDTLS_DHM_C */ }{...} static void esp_mbedtls_free_keycert(mbedtls_ssl_context *ssl) { mbedtls_ssl_config *conf = (mbedtls_ssl_config *)mbedtls_ssl_context_get_config(ssl); mbedtls_ssl_key_cert *keycert = conf->MBEDTLS_PRIVATE(key_cert), *next; while (keycert) { next = keycert->next; if (keycert) { mbedtls_free(keycert); }{...} keycert = next; }{...} conf->MBEDTLS_PRIVATE(key_cert) = NULL; }{...} static void esp_mbedtls_free_keycert_key(mbedtls_ssl_context *ssl) { const mbedtls_ssl_config *conf = mbedtls_ssl_context_get_config(ssl); mbedtls_ssl_key_cert *keycert = conf->MBEDTLS_PRIVATE(key_cert); while (keycert) { if (keycert->key) { mbedtls_pk_free(keycert->key); keycert->key = NULL; }{...} keycert = keycert->next; }{...} }{...} static void esp_mbedtls_free_cacert(mbedtls_ssl_context *ssl) { if (ssl->MBEDTLS_PRIVATE(conf)->MBEDTLS_PRIVATE(ca_chain)) { mbedtls_ssl_config *conf = (mbedtls_ssl_config *)mbedtls_ssl_context_get_config(ssl); mbedtls_x509_crt_free(conf->MBEDTLS_PRIVATE(ca_chain)); conf->MBEDTLS_PRIVATE(ca_chain) = NULL; }{...} }{...} /* ... */#endif struct wpabuf * tls_connection_handshake(void *tls_ctx, struct tls_connection *conn, const struct wpabuf *in_data, struct wpabuf **appl_data) { tls_context_t *tls = conn->tls; int ret = 0; struct wpabuf *resp; /* data freed by sender */ conn->tls_io_data.out_data = NULL; if (wpabuf_len(in_data)) { conn->tls_io_data.in_data = wpabuf_dup(in_data); }{...} /* Multiple reads */ while (!mbedtls_ssl_is_handshake_over(&tls->ssl)) { #ifdef CONFIG_ESP_WIFI_ENT_FREE_DYNAMIC_BUFFER int cli_state = tls->ssl.MBEDTLS_PRIVATE(state); #endif /* CONFIG_ESP_WIFI_ENT_FREE_DYNAMIC_BUFFER */ ret = mbedtls_ssl_handshake_step(&tls->ssl); if (ret < 0) { break; }{...} #ifdef CONFIG_ESP_WIFI_ENT_FREE_DYNAMIC_BUFFER if (mbedtls_ssl_get_version_number(&tls->ssl) == MBEDTLS_SSL_VERSION_TLS1_2) { if (cli_state == MBEDTLS_SSL_SERVER_CERTIFICATE) { esp_mbedtls_free_cacert(&tls->ssl); }{...} else if (cli_state == MBEDTLS_SSL_CERTIFICATE_VERIFY) { esp_mbedtls_free_dhm(&tls->ssl); esp_mbedtls_free_keycert_key(&tls->ssl); esp_mbedtls_free_keycert(&tls->ssl); }{...} }{...} #endif/* ... */ }{...} if (ret < 0 && ret != MBEDTLS_ERR_SSL_WANT_READ) { wpa_printf(MSG_INFO, "%s: ret is %d line:%d", __func__, ret, __LINE__); goto end; }{...} if (!conn->tls_io_data.out_data) { wpa_printf(MSG_INFO, "application data is null, adding one byte for ack"); u8 *dummy = os_zalloc(1); if (dummy == NULL) { wpa_printf(MSG_INFO, "%s: memory allocation failure. line:%d", __func__, __LINE__); goto end; }{...} #ifdef CONFIG_TLSV13 if (mbedtls_ssl_get_version_number(&conn->tls->ssl) == MBEDTLS_SSL_VERSION_TLS1_3) { *appl_data = wpabuf_alloc_ext_data(dummy, 1); if (appl_data == NULL) { wpa_printf(MSG_INFO, "%s: memory allocation failure. line:%d", __func__, __LINE__); os_free(dummy); goto end; }{...} return NULL; }{...} #endif/* ... */ /* CONFIG_TLSV13 */ conn->tls_io_data.out_data = wpabuf_alloc_ext_data(dummy, 0); if (conn->tls_io_data.out_data == NULL) { wpa_printf(MSG_INFO, "%s: memory allocation failure. line:%d", __func__, __LINE__); os_free(dummy); goto end; }{...} }{...} end: resp = conn->tls_io_data.out_data; conn->tls_io_data.out_data = NULL; return resp; }{ ... } struct wpabuf * tls_connection_server_handshake(void *tls_ctx, struct tls_connection *conn, const struct wpabuf *in_data, struct wpabuf **appl_data) { wpa_printf(MSG_ERROR, "%s: not supported %d", __func__, __LINE__); return NULL; }{ ... } struct wpabuf * tls_connection_encrypt(void *tls_ctx, struct tls_connection *conn, const struct wpabuf *in_data) { struct wpabuf *resp; size_t ret; /* Reset dangling pointer */ conn->tls_io_data.out_data = NULL; ret = mbedtls_ssl_write(&conn->tls->ssl, (unsigned char*) wpabuf_head(in_data), wpabuf_len(in_data)); if (ret < wpabuf_len(in_data)) { wpa_printf(MSG_ERROR, "%s:%d, not able to write whole data", __func__, __LINE__); }{...} resp = conn->tls_io_data.out_data; conn->tls_io_data.out_data = NULL; return resp; }{ ... } struct wpabuf *tls_connection_decrypt(void *tls_ctx, struct tls_connection *conn, const struct wpabuf *in_data) { #define MAX_PHASE2_BUFFER 1536 struct wpabuf *out = NULL; int ret; unsigned char *buf = os_malloc(MAX_PHASE2_BUFFER); if (!buf) { return NULL; }{...} /* Reset dangling output buffer before setting data, data was freed by caller */ conn->tls_io_data.out_data = NULL; conn->tls_io_data.in_data = wpabuf_dup(in_data); if (!conn->tls_io_data.in_data) { goto cleanup; }{...} ret = mbedtls_ssl_read(&conn->tls->ssl, buf, MAX_PHASE2_BUFFER); if (ret < 0) { wpa_printf(MSG_ERROR, "%s:%d, not able to read data", __func__, __LINE__); goto cleanup; }{...} out = wpabuf_alloc_copy(buf, ret); cleanup: /* there may be some error written in output buffer */ if (conn->tls_io_data.out_data) { os_free(conn->tls_io_data.out_data); conn->tls_io_data.out_data = NULL; }{...} os_free(buf); return out; #undef MAX_PHASE2_BUFFER }{ ... } int tls_connection_resumed(void *tls_ctx, struct tls_connection *conn) { if (conn && conn->tls && conn->tls->ssl.MBEDTLS_PRIVATE(handshake)) { return conn->tls->ssl.MBEDTLS_PRIVATE(handshake)->resume; }{...} return 0; }{ ... } /* cipher array should contain cipher number in mbedtls num as per IANA * Please see cipherlist is u8, therefore only initial ones are supported *//* ... */ int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn, u8 *ciphers) { int i = 0; while (*ciphers != 0 && i < MAX_CIPHERSUITE) { conn->tls->ciphersuite[i] = ciphers[i]; i++; }{...} return 0; }{ ... } int tls_get_version(void *tls_ctx, struct tls_connection *conn, char *buf, size_t buflen) { const char *name; if (conn == NULL) { return -1; }{...} name = mbedtls_ssl_get_version(&conn->tls->ssl); if (name == NULL) { return -1; }{...} os_strlcpy(buf, name, buflen); return 0; }{ ... } int tls_get_cipher(void *tls_ctx, struct tls_connection *conn, char *buf, size_t buflen) { const char *name; if (conn == NULL) { return -1; }{...} name = mbedtls_ssl_get_ciphersuite(&conn->tls->ssl); if (name == NULL) { return -1; }{...} os_strlcpy(buf, name, buflen); return 0; }{ ... } int tls_connection_enable_workaround(void *tls_ctx, struct tls_connection *conn) { wpa_printf(MSG_ERROR, "%s: not supported %d", __func__, __LINE__); return -1; }{ ... } int tls_connection_get_failed(void *tls_ctx, struct tls_connection *conn) { return 0; }{ ... } int tls_connection_get_read_alerts(void *tls_ctx, struct tls_connection *conn) { wpa_printf(MSG_ERROR, "%s: not supported %d", __func__, __LINE__); return 0; }{ ... } int tls_connection_get_write_alerts(void *tls_ctx, struct tls_connection *conn) { wpa_printf(MSG_ERROR, "%s: not supported %d", __func__, __LINE__); return 0; }{ ... } int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn, const struct tls_connection_params *params) { int ret = 0; tls_context_t *tls = (tls_context_t *)os_zalloc(sizeof(tls_context_t)); if (!tls) { wpa_printf(MSG_ERROR, "failed to allocate tls context"); return -1; }{...} if (!params) { wpa_printf(MSG_ERROR, "configuration is null"); ret = -1; goto err; }{...} ret = tls_create_mbedtls_handle(conn, params, tls); if (ret < 0) { wpa_printf(MSG_ERROR, "failed to create ssl handle"); goto err; }{...} mbedtls_ssl_set_bio(&tls->ssl, conn, tls_mbedtls_write, tls_mbedtls_read, NULL); conn->tls = (tls_context_t *)tls; return ret; err: os_free(tls); return ret; }{ ... } int tls_global_set_params(void *tls_ctx, const struct tls_connection_params *params) { wpa_printf(MSG_INFO, "TLS: Global parameters not supported"); return -1; }{ ... } int tls_connection_set_session_ticket_cb(void *tls_ctx, struct tls_connection *conn, tls_session_ticket_cb cb, void *ctx) { wpa_printf(MSG_ERROR, "TLS: %s not supported", __func__); return -1; }{ ... } static int tls_connection_prf(void *tls_ctx, struct tls_connection *conn, const char *label, const u8 *context, size_t context_len, int server_random_first, u8 *out, size_t out_len) { int ret; u8 *seed, *pos; size_t seed_len = 2 * TLS_RANDOM_LEN; mbedtls_ssl_context *ssl = &conn->tls->ssl; if (context_len > MAX_EXPORTER_CONTEXT_LEN) { return -1; }{...} if (context) { // The magic value 2 represents the memory required to store the context length. seed_len += 2 + context_len; }{...} if (!ssl) { wpa_printf(MSG_ERROR, "TLS: %s, session ingo is null", __func__); return -1; }{...} if (!mbedtls_ssl_is_handshake_over(ssl)) { wpa_printf(MSG_ERROR, "TLS: %s, incorrect tls state=%d", __func__, ssl->MBEDTLS_PRIVATE(state)); return -1; }{...} seed = os_malloc(seed_len); if (!seed) { return -1; }{...} if (server_random_first) { os_memcpy(seed, conn->randbytes + TLS_RANDOM_LEN, TLS_RANDOM_LEN); os_memcpy(seed + TLS_RANDOM_LEN, conn->randbytes, TLS_RANDOM_LEN); }{...} else { os_memcpy(seed, conn->randbytes, 2 * TLS_RANDOM_LEN); }{...} if (context) { pos = seed + 2 * TLS_RANDOM_LEN; WPA_PUT_BE16(pos, context_len); pos += 2; os_memcpy(pos, context, context_len); }{...} wpa_hexdump_key(MSG_MSGDUMP, "random", seed, 2 * TLS_RANDOM_LEN); wpa_hexdump_key(MSG_MSGDUMP, "master", ssl->MBEDTLS_PRIVATE(session)->MBEDTLS_PRIVATE(master), TLS_MASTER_SECRET_LEN); ret = mbedtls_ssl_tls_prf(conn->tls_prf_type, conn->master_secret, TLS_MASTER_SECRET_LEN, label, seed, seed_len, out, out_len); os_free(seed); if (ret < 0) { wpa_printf(MSG_ERROR, "prf failed, ret=%d", ret); return -1; }{...} wpa_hexdump_key(MSG_MSGDUMP, "key", out, out_len); return ret; }{ ... } #ifdef CONFIG_TLSV13 /* RFC 8446 Section 7.5 */ static int tls13_connection_export_key(void *tls_ctx, struct tls_connection *conn, const char *label, const u8 *context, size_t context_len, u8 *out, size_t out_len) { int ret; mbedtls_ssl_context *ssl = &conn->tls->ssl; psa_algorithm_t hash_alg; size_t hash_len; unsigned char tmp_secret[PSA_MAC_MAX_SIZE] = { 0 }; unsigned char hashed_context[PSA_HASH_MAX_SIZE] = { 0 }; psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info; hash_alg = mbedtls_md_psa_alg_from_type(ciphersuite_info->mac); hash_len = PSA_HASH_LENGTH(hash_alg); ret = mbedtls_ssl_tls13_derive_secret(hash_alg, conn->exporter_master_secret, hash_len, (unsigned char const *) label, (size_t) strlen(label), NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED, tmp_secret, hash_len); if (ret != 0) { wpa_printf(MSG_ERROR, "%s(): mbedtls_ssl_tls13_derive_secret() failed", __func__); return ret; }{...} status = psa_hash_compute(hash_alg, context, context_len, hashed_context, PSA_HASH_LENGTH(hash_alg), &context_len); if (status != PSA_SUCCESS) { wpa_printf(MSG_ERROR, "%s(): psa_hash_compute() failed", __func__); ret = PSA_TO_MBEDTLS_ERR(status); return ret; }{...} ret = mbedtls_ssl_tls13_hkdf_expand_label(hash_alg, tmp_secret, hash_len, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(exporter), hashed_context, context_len, out, out_len); if (ret != 0) { wpa_printf(MSG_ERROR, "%s(): psa_hash_compute() failed", __func__); return ret; }{...} return 0; }{...} /* ... */#endif /* CONFIG_TLSV13 */ int tls_connection_export_key(void *tls_ctx, struct tls_connection *conn, const char *label, const u8 *context, size_t context_len, u8 *out, size_t out_len) { #ifdef CONFIG_TLSV13 if (mbedtls_ssl_get_version_number(&conn->tls->ssl) == MBEDTLS_SSL_VERSION_TLS1_3) return tls13_connection_export_key(tls_ctx, conn, label, context, context_len, out, out_len);/* ... */ #endif /* CONFIG_TLSV13 */ return tls_connection_prf(tls_ctx, conn, label, context, context_len, 0, out, out_len); }{ ... } int tls_connection_get_eap_fast_key(void *tls_ctx, struct tls_connection *conn, u8 *out, size_t out_len) { wpa_printf(MSG_INFO, "TLS: tls_connection_get_eap_fast_key not supported, please unset mbedtls crypto and try again"); return -1; }{ ... } int tls_connection_client_hello_ext(void *tls_ctx, struct tls_connection *conn, int ext_type, const u8 *data, size_t data_len) { wpa_printf(MSG_INFO, "TLS: tls_connection_client_hello_ext not supported, please unset mbedtls crypto and try again"); return -1; }{ ... } int tls_connection_shutdown(void *tls_ctx, struct tls_connection *conn) { if (conn->tls_io_data.in_data) { wpabuf_free(conn->tls_io_data.in_data); }{...} conn->tls_io_data.in_data = NULL; /* outdata may have dangling pointer */ conn->tls_io_data.out_data = NULL; return mbedtls_ssl_session_reset(&conn->tls->ssl); }{ ... } int tls_connection_get_random(void *tls_ctx, struct tls_connection *conn, struct tls_random *data) { mbedtls_ssl_context *ssl = &conn->tls->ssl; os_memset(data, 0, sizeof(*data)); if (ssl->MBEDTLS_PRIVATE(state) == MBEDTLS_SSL_CLIENT_HELLO) { return -1; }{...} data->client_random = conn->randbytes; data->client_random_len = TLS_RANDOM_LEN; if (ssl->MBEDTLS_PRIVATE(state) != MBEDTLS_SSL_SERVER_HELLO) { data->server_random = conn->randbytes + TLS_RANDOM_LEN; data->server_random_len = TLS_RANDOM_LEN; }{...} return 0; }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.