Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define NX_SECURE_SOURCE_CODE
#include "nx_secure_tls.h"
...
...
_nx_secure_tls_process_clienthello_extensions(NX_SECURE_TLS_SESSION *, UCHAR *, UINT, NX_SECURE_TLS_HELLO_EXTENSION *, UINT *, UCHAR *, UINT)
...
...
_nx_secure_tls_proc_clienthello_sec_reneg_extension(NX_SECURE_TLS_SESSION *, UCHAR *, UINT)
...
...
_nx_secure_tls_proc_clienthello_sec_sa_extension(NX_SECURE_TLS_SESSION *, NX_SECURE_TLS_HELLO_EXTENSION *, UINT, UINT *, USHORT, UINT *, USHORT *, NX_SECURE_X509_CERT *)
...
...
...
...
...
...
_nx_secure_tls_get_signature_algorithm_id(UINT, USHORT *)
...
...
Files
loading...
CodeScopeSTM32 Libraries and Samplesnetxduonx_secure/src/nx_secure_tls_process_clienthello_extensions.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
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** NetX Secure Component */ /** */ /** Transport Layer Security (TLS) */ /** */... /**************************************************************************/ /**************************************************************************/ #define NX_SECURE_SOURCE_CODE #include "nx_secure_tls.h" #if !defined(NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION) static UINT _nx_secure_tls_proc_clienthello_sec_reneg_extension(NX_SECURE_TLS_SESSION *tls_session, UCHAR *packet_buffer, UINT extension_length);/* ... */ #endif #if (NX_SECURE_TLS_TLS_1_3_ENABLED) && !defined(NX_SECURE_TLS_SERVER_DISABLED) static UINT _nx_secure_tls_proc_clienthello_keyshare_extension(NX_SECURE_TLS_SESSION *tls_session, UCHAR *packet_buffer, USHORT extension_length); static UINT _nx_secure_tls_proc_clienthello_supported_versions_extension(NX_SECURE_TLS_SESSION *tls_session, UCHAR *packet_buffer, USHORT *supported_version, USHORT extension_length); static VOID _nx_secure_tls_proc_clienthello_signature_algorithms_extension(NX_SECURE_TLS_SESSION *tls_session, const UCHAR *packet_buffer, USHORT extension_length); #ifdef NX_SECURE_ENABLE_PSK_CIPHERSUITES static UINT _nx_secure_tls_process_clienthello_psk_extension(NX_SECURE_TLS_SESSION *tls_session, const UCHAR *packet_buffer, USHORT extension_length, const UCHAR *client_hello_buffer, UINT client_hello_length);/* ... */ #endif/* ... */ #endif ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_tls_process_clienthello_extensions PORTABLE C */ /* 6.1.9 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function processes any extensions included in an incoming */ /* ClientHello message from a remote host. */ /* */ /* INPUT */ /* */ /* tls_session TLS control block */ /* packet_buffer Pointer to message data */ /* message_length Length of message data (bytes)*/ /* extensions Extensions for output */ /* num_extensions Number of extensions */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* _nx_secure_tls_proc_clienthello_sec_reneg_extension */ /* Process ClientHello */ /* Renegotiation extension */ /* _nx_secure_tls_proc_clienthello_ec_groups_extension */ /* Process ClientHello */ /* EC groups extension */ /* _nx_secure_tls_proc_clienthello_ec_point_formats_extension */ /* Process ClientHello */ /* EC point formats extension */ /* */ /* CALLED BY */ /* */ /* _nx_secure_tls_process_clienthello Process ClientHello */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* fixed renegotiation bug, */ /* resulting in version 6.1 */ /* 10-15-2021 Timothy Stapko Modified comment(s), added */ /* ability to disable client */ /* initiated renegotiation, */ /* resulting in version 6.1.9 */ /* */... /**************************************************************************/ UINT _nx_secure_tls_process_clienthello_extensions(NX_SECURE_TLS_SESSION *tls_session, UCHAR *packet_buffer, UINT message_length, NX_SECURE_TLS_HELLO_EXTENSION *extensions, UINT *num_extensions, UCHAR *client_hello_buffer, UINT client_hello_length) { UINT status = NX_SUCCESS; UINT offset; UINT max_extensions; USHORT extension_id; UINT extension_length; #if (NX_SECURE_TLS_TLS_1_3_ENABLED) && !defined(NX_SECURE_TLS_SERVER_DISABLED) USHORT supported_version = tls_session -> nx_secure_tls_protocol_version; #endif #if defined(NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION) || defined(NX_SECURE_TLS_DISABLE_CLIENT_INITIATED_RENEGOTIATION) NX_PARAMETER_NOT_USED(tls_session); #endif /* NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION */ #if !(NX_SECURE_TLS_TLS_1_3_ENABLED) || !defined(NX_SECURE_ENABLE_PSK_CIPHERSUITES) NX_PARAMETER_NOT_USED(client_hello_buffer); NX_PARAMETER_NOT_USED(client_hello_length);/* ... */ #endif max_extensions = *num_extensions; offset = 0; *num_extensions = 0; /* Process extensions until we run out. */ while (offset < message_length && *num_extensions < max_extensions) { /* Make sure there are at least 4 bytes available so we can read extension_id and extension_length. *//* ... */ if((offset + 4) > message_length) { return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH); }if ((offset + 4) > message_length) { ... } /* See what the extension is. */ extension_id = (USHORT)((packet_buffer[offset] << 8) + packet_buffer[offset + 1]); offset += 2; /* Get extension length. */ extension_length = (UINT)((packet_buffer[offset] << 8) + packet_buffer[offset + 1]); offset += 2; /* Verify the message_length is at least "extension_length". */ if((offset + extension_length) > message_length) { return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH); }if ((offset + extension_length) > message_length) { ... } /* Parse the extension. */ switch (extension_id) { #if !defined(NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION) case NX_SECURE_TLS_EXTENSION_SECURE_RENEGOTIATION: status = _nx_secure_tls_proc_clienthello_sec_reneg_extension(tls_session, &packet_buffer[offset], extension_length); if (status) { return(status); }if (status) { ... } break;/* ... */ #endif /* NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION */ #if (NX_SECURE_TLS_TLS_1_3_ENABLED) && !defined(NX_SECURE_TLS_SERVER_DISABLED)case NX_SECURE_TLS_EXTENSION_SECURE_RENEGOTIATION: case NX_SECURE_TLS_EXTENSION_KEY_SHARE: if(tls_session->nx_secure_tls_1_3) { /* If TLS 1.3, process the key share extension. */ status = _nx_secure_tls_proc_clienthello_keyshare_extension(tls_session, &packet_buffer[offset], (USHORT)extension_length); if (status) { return(status); }if (status) { ... } }if (tls_session->nx_secure_tls_1_3) { ... } /* TLS 1.2 and earlier: ignore extension. */ break;case NX_SECURE_TLS_EXTENSION_KEY_SHARE: case NX_SECURE_TLS_EXTENSION_SUPPORTED_VERSIONS: if(tls_session->nx_secure_tls_1_3) { /* Process the TLS 1.3 supported_versions extension. */ status = _nx_secure_tls_proc_clienthello_supported_versions_extension(tls_session, &packet_buffer[offset], &supported_version, (USHORT)extension_length); if (status) { return(status); }if (status) { ... } }if (tls_session->nx_secure_tls_1_3) { ... } /* Ignore if not TLS 1.3. */ break; #ifdef NX_SECURE_ENABLE_PSK_CIPHERSUITEScase NX_SECURE_TLS_EXTENSION_SUPPORTED_VERSIONS: case NX_SECURE_TLS_EXTENSION_PRE_SHARED_KEY: if(tls_session->nx_secure_tls_1_3) { /* Process the TLS 1.3 PSK extension. */ status = _nx_secure_tls_process_clienthello_psk_extension(tls_session, &packet_buffer[offset], (USHORT)extension_length, client_hello_buffer, client_hello_length); if (status) { return(status); }if (status) { ... } }if (tls_session->nx_secure_tls_1_3) { ... } break;/* ... */ #endif/* ... */ #endif case NX_SECURE_TLS_EXTENSION_SIGNATURE_ALGORITHMS: case NX_SECURE_TLS_EXTENSION_SERVER_NAME_INDICATION: case NX_SECURE_TLS_EXTENSION_MAX_FRAGMENT_LENGTH: case NX_SECURE_TLS_EXTENSION_CLIENT_CERTIFICATE_URL: case NX_SECURE_TLS_EXTENSION_TRUSTED_CA_INDICATION: case NX_SECURE_TLS_EXTENSION_CERTIFICATE_STATUS_REQUEST: case NX_SECURE_TLS_EXTENSION_EC_GROUPS: case NX_SECURE_TLS_EXTENSION_EC_POINT_FORMATS: case NX_SECURE_TLS_EXTENSION_ECJPAKE_KEY_KP_PAIR: /* These extensions require information to be passed to the application. Save off the extension data in our extensions array to pass along in the hello callback. *//* ... */ extensions[*num_extensions].nx_secure_tls_extension_id = extension_id; extensions[*num_extensions].nx_secure_tls_extension_data = &packet_buffer[offset]; extensions[*num_extensions].nx_secure_tls_extension_data_length = (USHORT)extension_length; /* Added another extension to the array. */ *num_extensions = *num_extensions + 1; break;case NX_SECURE_TLS_EXTENSION_ECJPAKE_KEY_KP_PAIR: case NX_SECURE_TLS_EXTENSION_TRUNCATED_HMAC: default: /* Unknown or unsupported extension, just ignore - TLS supports multiple extensions and the default behavior is to ignore any extensions that we don't know. *//* ... */ break;default }switch (extension_id) { ... } /* Adjust our offset with the length of the extension we just parsed. */ offset += extension_length; }while (offset < message_length && *num_extensions < max_extensions) { ... } #if (NX_SECURE_TLS_TLS_1_3_ENABLED) && !defined(NX_SECURE_TLS_SERVER_DISABLED) if((tls_session->nx_secure_tls_1_3) && (supported_version != NX_SECURE_TLS_VERSION_TLS_1_3)) { /* Negotiate a version of TLS prior to TLS 1.3. */ if (tls_session -> nx_secure_tls_protocol_version_override == 0) { tls_session -> nx_secure_tls_1_3 = NX_FALSE; #if !defined(NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION) tls_session -> nx_secure_tls_renegotation_enabled = NX_TRUE; #endif /* NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION */ tls_session -> nx_secure_tls_protocol_version = supported_version; }if (tls_session -> nx_secure_tls_protocol_version_override == 0) { ... } else { /* Protocol version is overridden to TLS 1.3. */ return(NX_SECURE_TLS_UNSUPPORTED_TLS_VERSION); }else { ... } }if ((tls_session->nx_secure_tls_1_3) && (supported_version != NX_SECURE_TLS_VERSION_TLS_1_3)) { ... } /* ... */#endif return(status); }{ ... } ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_tls_proc_clienthello_sec_reneg_extension PORTABLE C */ /* 6.1.9 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function parses the Secure Renegotiation Indication extension */ /* from an incoming ClientHello record.See RFC 5746 for more */ /* information. */ /* */ /* INPUT */ /* */ /* tls_session TLS control block */ /* packet_buffer Outgoing TLS packet buffer */ /* extension_length Length of extension data */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* _nx_secure_tls_process_clienthello_extensions */ /* Process ClientHello extensions*/ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* fixed renegotiation bug, */ /* resulting in version 6.1 */ /* 10-15-2021 Timothy Stapko Modified comment(s), added */ /* ability to disable client */ /* initiated renegotiation, */ /* resulting in version 6.1.9 */ /* */... /**************************************************************************/ #if !defined(NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION) static UINT _nx_secure_tls_proc_clienthello_sec_reneg_extension(NX_SECURE_TLS_SESSION *tls_session, UCHAR *packet_buffer, UINT extension_length) { ULONG offset = 0; UCHAR renegotiated_connection_length; INT compare_value; /* Secure Renegotiation Indication Extensions structure: * Initial ClientHello: * | 2 | 2 | 1 | * | Ext Type | Ext Len | Reneg Info Len | * | 0xff01 | 0x0001 | 0x00 | * * Renegotiating ClientHello: * | 2 | 2 | 1 | 12 | * | Ext Type | Ext Len | Reneg Info Len | client_verify_data | * | 0xff01 | 0x000d | 0x0c | | *//* ... */ /* From RFC 5746: struct { opaque renegotiated_connection<0..255>; } RenegotiationInfo; The contents of this extension are specified as follows. - If this is the initial handshake for a connection, then the "renegotiated_connection" field is of zero length in both the ClientHello and the ServerHello. Thus, the entire encoding of the extension is ff 01 00 01 00. The first two octets represent the extension type, the third and fourth octets the length of the extension itself, and the final octet the zero length byte for the "renegotiated_connection" field. - For ClientHellos that are renegotiating, this field contains the "client_verify_data" specified in Section 3.1. - For ServerHellos that are renegotiating, this field contains the concatenation of client_verify_data and server_verify_data. For current versions of TLS, this will be a 24-byte value (for SSLv3, it will be a 72-byte value). *//* ... */ if (extension_length < 1) { return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH); }if (extension_length < 1) { ... } /* Get the "renegotiated_connection" field. */ renegotiated_connection_length = packet_buffer[offset]; offset++; if (offset + renegotiated_connection_length > extension_length) { return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH); }if (offset + renegotiated_connection_length > extension_length) { ... } /* See if the client is attempting to renegotiate an established connection. */ if (renegotiated_connection_length) { /* The remote host is attempting a renegotiation - make sure our local session is active and renegotiation is OK. */ if (!tls_session -> nx_secure_tls_local_session_active || !tls_session -> nx_secure_tls_remote_session_active) { /* Remote host is attempting a renegotiation but the server is not currently in a session! */ return(NX_SECURE_TLS_RENEGOTIATION_SESSION_INACTIVE); }if (!tls_session -> nx_secure_tls_local_session_active || !tls_session -> nx_secure_tls_remote_session_active) { ... } if(!tls_session -> nx_secure_tls_secure_renegotiation) { return(NX_SECURE_TLS_RENEGOTIATION_FAILURE); }if (!tls_session -> nx_secure_tls_secure_renegotiation) { ... } if (renegotiated_connection_length != NX_SECURE_TLS_FINISHED_HASH_SIZE) { /* Do not have the right amount of data for comparison. */ return(NX_SECURE_TLS_RENEGOTIATION_EXTENSION_ERROR); }if (renegotiated_connection_length != NX_SECURE_TLS_FINISHED_HASH_SIZE) { ... } /* Compare the received verify data to our locally-stored version. */ compare_value = NX_SECURE_MEMCMP(&packet_buffer[offset], tls_session -> nx_secure_tls_remote_verify_data, NX_SECURE_TLS_FINISHED_HASH_SIZE); if (compare_value) { /* Session verify data did not match what we expect - error. */ return(NX_SECURE_TLS_RENEGOTIATION_EXTENSION_ERROR); }if (compare_value) { ... } tls_session -> nx_secure_tls_secure_renegotiation_verified = NX_TRUE; }if (renegotiated_connection_length) { ... } else { /* Verify that the extension contains only the initial handshake data - this is a new connection. */ if ((extension_length != 1) || (tls_session -> nx_secure_tls_local_session_active)) { /* Error - the provided extension length was not expected for an initial handshake. */ return(NX_SECURE_TLS_RENEGOTIATION_EXTENSION_ERROR); }if ((extension_length != 1) || (tls_session -> nx_secure_tls_local_session_active)) { ... } /* The remote host supports renegotiation. */ tls_session -> nx_secure_tls_secure_renegotiation = NX_TRUE; }else { ... } return(NX_SUCCESS); }{ ... } /* ... */#endif /* NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION */ ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_tls_proc_clienthello_sec_sa_extension PORTABLE C */ /* 6.1.9 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function processes the supported groups extensions included in */ /* an incoming ClientHello message from a remote host. */ /* */ /* INPUT */ /* */ /* tls_session TLS control block */ /* exts Parsed extensions */ /* num_extensions Number of extensions */ /* selected_curve Output selected ECDHE curve */ /* cert_curve Named curve of local cert */ /* cert_curve_supported Output whether curve used by */ /* local cert is supported */ /* ecdhe_signature_algorithm Output signature algorithm */ /* for ECDHE key exchange */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* _nx_secure_tls_find_curve_method Find named curve used by cert */ /* */ /* CALLED BY */ /* */ /* _nx_secure_tls_process_clienthello Process ClientHello */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), added */ /* curve priority logic, */ /* resulting in version 6.1 */ /* 10-15-2021 Timothy Stapko Modified comment(s), fixed */ /* compilation issue with */ /* TLS 1.3 and disabling TLS */ /* server, */ /* resulting in version 6.1.9 */ /* */... /**************************************************************************/ #ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE UINT _nx_secure_tls_proc_clienthello_sec_sa_extension(NX_SECURE_TLS_SESSION *tls_session, NX_SECURE_TLS_HELLO_EXTENSION *exts, UINT num_extensions, UINT *selected_curve, USHORT cert_curve, UINT *cert_curve_supported, USHORT *ecdhe_signature_algorithm, NX_SECURE_X509_CERT *cert) { UINT status = NX_SUCCESS; UINT i, j, k; const UCHAR *groups; USHORT group; USHORT groups_len; const NX_CRYPTO_METHOD *curve_method; UINT curve_priority; UINT new_curve_priority = 0; #if (NX_SECURE_TLS_TLS_1_3_ENABLED) && !defined(NX_SECURE_TLS_SERVER_DISABLED) NX_SECURE_TLS_ECC *ecc_info; UINT signature_algorithm_exist = NX_FALSE;/* ... */ #endif const UCHAR *signature_algorithms; USHORT signature_algorithms_len; USHORT signature_algorithm; USHORT signature_algorithm_id; UCHAR expected_signature = 0; /* Select SHA256 as the signature hash. */ *ecdhe_signature_algorithm = NX_SECURE_TLS_SIGNATURE_RSA_SHA256; *cert_curve_supported = NX_FALSE; *selected_curve = 0; if (cert_curve) { status = _nx_secure_tls_find_curve_method(tls_session, cert_curve, &curve_method, NX_NULL); if (status == NX_SUCCESS && curve_method != NX_NULL) { *cert_curve_supported = NX_TRUE; }if (status == NX_SUCCESS && curve_method != NX_NULL) { ... } }if (cert_curve) { ... } if (cert != NX_NULL) { if (cert -> nx_secure_x509_public_algorithm == NX_SECURE_TLS_X509_TYPE_EC) { expected_signature = NX_SECURE_TLS_SIGNATURE_ALGORITHM_ECDSA; }if (cert -> nx_secure_x509_public_algorithm == NX_SECURE_TLS_X509_TYPE_EC) { ... } else if (cert->nx_secure_x509_public_algorithm == NX_SECURE_TLS_X509_TYPE_RSA) { expected_signature = NX_SECURE_TLS_SIGNATURE_ALGORITHM_RSA; }else if (cert->nx_secure_x509_public_algorithm == NX_SECURE_TLS_X509_TYPE_RSA) { ... } #if (NX_SECURE_TLS_TLS_1_0_ENABLED || NX_SECURE_TLS_TLS_1_1_ENABLED) if (tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_0 || tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_1) { *ecdhe_signature_algorithm = (USHORT)((NX_SECURE_TLS_HASH_ALGORITHM_SHA1 << 8) | (expected_signature & 0xFF)); }if (tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_0 || tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_1) { ... } /* ... */#endif /* NX_SECURE_TLS_TLS_1_0_ENABLED || NX_SECURE_TLS_TLS_1_1_ENABLED */ }if (cert != NX_NULL) { ... } for (i = 0; i < num_extensions; i++) { switch (exts[i].nx_secure_tls_extension_id) { case NX_SECURE_TLS_EXTENSION_EC_GROUPS: groups = exts[i].nx_secure_tls_extension_data; groups_len = exts[i].nx_secure_tls_extension_data_length; /* Set our start priority to the size of our supported curves list (lowest priority). */ curve_priority = tls_session -> nx_secure_tls_ecc.nx_secure_tls_ecc_supported_groups_count; /* Loop through curves sent by client. */ for (j = 0; j < groups_len; j += 2) { group = (USHORT)((groups[j] << 8) + groups[j + 1]); status = _nx_secure_tls_find_curve_method(tls_session, group, &curve_method, &new_curve_priority); if ((status == NX_CRYTPO_MISSING_ECC_CURVE) || (new_curve_priority > curve_priority)) { /* Keep searching list. */ continue; }if ((status == NX_CRYTPO_MISSING_ECC_CURVE) || (new_curve_priority > curve_priority)) { ... } /* Found a higher-priority curve. */ if (status == NX_SUCCESS) { /* Found shared named curve. */ *selected_curve = group; curve_priority = new_curve_priority; #if (NX_SECURE_TLS_TLS_1_3_ENABLED) && !defined(NX_SECURE_TLS_SERVER_DISABLED) if (tls_session -> nx_secure_tls_1_3 && (tls_session -> nx_secure_tls_server_state == NX_SECURE_TLS_SERVER_STATE_SEND_HELLO_RETRY)) { /* Get our session ECC data. */ ecc_info = &(tls_session -> nx_secure_tls_ecc); /* Loop through all supported ECC curves in this session. */ for (k = 0; k < ecc_info -> nx_secure_tls_ecc_supported_groups_count; k++) { /* Find the matching group in the keys we generated and saved. */ if(group == tls_session->nx_secure_tls_key_material.nx_secure_tls_ecc_key_data[k].nx_secure_tls_ecdhe_named_curve) { /* Store selected ECDHE key data index. */ tls_session -> nx_secure_tls_key_material.nx_secure_tls_ecc_key_data_selected = k; }if (group == tls_session->nx_secure_tls_key_material.nx_secure_tls_ecc_key_data[k].nx_secure_tls_ecdhe_named_curve) { ... } }for (k = 0; k < ecc_info -> nx_secure_tls_ecc_supported_groups_count; k++) { ... } }if (tls_session -> nx_secure_tls_1_3 && (tls_session -> nx_secure_tls_server_state == NX_SECURE_TLS_SERVER_STATE_SEND_HELLO_RETRY)) { ... } /* ... */#endif }if (status == NX_SUCCESS) { ... } else { /* status is not NX_CRYTPO_MISSING_ECC_CURVE or NX_SUCCESS, return error. */ return(status); }else { ... } /* Continue searching our supported curves list until we find the highest-priority curve. */ }for (j = 0; j < groups_len; j += 2) { ... } /* Reset status as we do not return NX_CRYTPO_MISSING_ECC_CURVE. */ status = NX_SUCCESS; break; case NX_SECURE_TLS_EXTENSION_EC_GROUPS: case NX_SECURE_TLS_EXTENSION_SIGNATURE_ALGORITHMS: /* This extension is not meaningful for TLS versions prior to 1.2. */ #if (NX_SECURE_TLS_TLS_1_3_ENABLED) && !defined(NX_SECURE_TLS_SERVER_DISABLED) if (tls_session -> nx_secure_tls_1_3) { _nx_secure_tls_proc_clienthello_signature_algorithms_extension(tls_session, exts[i].nx_secure_tls_extension_data, exts[i].nx_secure_tls_extension_data_length); signature_algorithm_exist = NX_TRUE; }if (tls_session -> nx_secure_tls_1_3) { ... } else /* ... */ #endif #if (NX_SECURE_TLS_TLS_1_2_ENABLED) if (tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_2) { signature_algorithms = exts[i].nx_secure_tls_extension_data; signature_algorithms_len = (USHORT)exts[i].nx_secure_tls_extension_data_length; if (signature_algorithms_len < 2 || (((signature_algorithms[0] << 8) + signature_algorithms[1]) != (signature_algorithms_len - 2))) { return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH); }if (signature_algorithms_len < 2 || (((signature_algorithms[0] << 8) + signature_algorithms[1]) != (signature_algorithms_len - 2))) { ... } /* Loop the signature algorithms in the extension. */ for (j = 2; j < signature_algorithms_len; j += 2) { if (signature_algorithms[j + 1] != expected_signature) { continue; }if (signature_algorithms[j + 1] != expected_signature) { ... } signature_algorithm = (USHORT)((signature_algorithms[j] << 8) + signature_algorithms[j + 1]); /* Map signature algorithm to internal ID. */ _nx_secure_tls_get_signature_algorithm_id((UINT)signature_algorithm, &signature_algorithm_id); if (signature_algorithm_id == NX_SECURE_TLS_X509_TYPE_UNKNOWN) { continue; }if (signature_algorithm_id == NX_SECURE_TLS_X509_TYPE_UNKNOWN) { ... } /* Check if this signature algorithm is supported. */ for (k = 0; k < tls_session -> nx_secure_tls_crypto_table -> nx_secure_tls_x509_cipher_table_size; k++) { if (signature_algorithm_id == tls_session -> nx_secure_tls_crypto_table -> nx_secure_tls_x509_cipher_table[k].nx_secure_x509_crypto_identifier) { *ecdhe_signature_algorithm = signature_algorithm; break; }if (signature_algorithm_id == tls_session -> nx_secure_tls_crypto_table -> nx_secure_tls_x509_cipher_table[k].nx_secure_x509_crypto_identifier) { ... } }for (k = 0; k < tls_session -> nx_secure_tls_crypto_table -> nx_secure_tls_x509_cipher_table_size; k++) { ... } /* Signature algorithm is selected. */ if (*ecdhe_signature_algorithm == signature_algorithm) { break; }if (*ecdhe_signature_algorithm == signature_algorithm) { ... } }for (j = 2; j < signature_algorithms_len; j += 2) { ... } }if (tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_2) { ... } /* ... */#endif break;case NX_SECURE_TLS_EXTENSION_SIGNATURE_ALGORITHMS: }switch (exts[i].nx_secure_tls_extension_id) { ... } }for (i = 0; i < num_extensions; i++) { ... } #if (NX_SECURE_TLS_TLS_1_3_ENABLED) && !defined(NX_SECURE_TLS_SERVER_DISABLED) /* RFC8446 4.2.3: If a server is authenticating via a certificate and the client has not sent a "signature_algorithms" extension, then the server MUST abort the handshake with a "missing_extension" alert. *//* ... */ if ((cert != NX_NULL) && (tls_session -> nx_secure_tls_1_3)) { if (signature_algorithm_exist == NX_FALSE) { return(NX_SECURE_TLS_MISSING_EXTENSION); }if (signature_algorithm_exist == NX_FALSE) { ... } if (tls_session -> nx_secure_tls_signature_algorithm == 0) { return(NX_SECURE_TLS_UNKNOWN_CERT_SIG_ALGORITHM); }if (tls_session -> nx_secure_tls_signature_algorithm == 0) { ... } }if ((cert != NX_NULL) && (tls_session -> nx_secure_tls_1_3)) { ... } /* ... */#endif return(status); }{ ... } /* ... */#endif /* NX_SECURE_ENABLE_ECC_CIPHERSUITE */ ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_tls_proc_clienthello_keyshare_extension PORTABLE C */ /* 6.1.9 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function parses the Key Share extension introduced in TLS 1.3. */ /* */ /* INPUT */ /* */ /* tls_session TLS control block */ /* packet_buffer Outgoing TLS packet buffer */ /* extension_length Length of extension data */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* _nx_secure_tls_process_serverhello_extensions */ /* Process ServerHello extensions*/ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* resulting in version 6.1 */ /* 10-15-2021 Timothy Stapko Modified comment(s), fixed */ /* compilation issue with */ /* TLS 1.3 and disabling TLS */ /* server, */ /* resulting in version 6.1.9 */ /* */... /**************************************************************************/ #if (NX_SECURE_TLS_TLS_1_3_ENABLED) && !defined(NX_SECURE_TLS_SERVER_DISABLED) extern NX_CRYPTO_METHOD crypto_method_ecdhe; static UINT _nx_secure_tls_proc_clienthello_keyshare_extension(NX_SECURE_TLS_SESSION *tls_session, UCHAR *packet_buffer, USHORT extension_length) { UINT status; UINT i; USHORT offset; USHORT key_share_length; USHORT key_group; USHORT key_length; UCHAR legacy_form; NX_SECURE_TLS_ECDHE_HANDSHAKE_DATA *ecc_key_data; UCHAR *pubkey; UCHAR *private_key; UINT private_key_length; NX_CRYPTO_EXTENDED_OUTPUT extended_output; NX_CRYPTO_METHOD *ecdhe_method; const NX_CRYPTO_METHOD *curve_method; VOID *handler = NX_NULL; NX_SECURE_TLS_ECC *ecc_info; /* Key Share Extension structure (for clienthello): * * ClientHello KeyShare: * | <list length> | * | 2 | 2 | <| 2 | 2 | <Key len> |> | * | Ext Type | Extension length | <| Named Group | Key len | key_exchange data |> | *//* ... */ if (extension_length == 0) { /* The client_shares may be empty if the client is requesting a HelloRetryRequest. */ tls_session -> nx_secure_tls_server_state = NX_SECURE_TLS_SERVER_STATE_SEND_HELLO_RETRY; return(NX_SUCCESS); }if (extension_length == 0) { ... } offset = 0; /* ecc_key_data will hold the key information from the chosen named curve. */ ecc_key_data = NX_NULL; /* Get our session ECC data. */ ecc_info = &(tls_session -> nx_secure_tls_ecc); if (extension_length < 2) { return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH); }if (extension_length < 2) { ... } /* Get the key share length. */ key_share_length = (USHORT)((packet_buffer[offset] << 8) + packet_buffer[offset + 1]); offset = (USHORT)(offset + 2); if(offset + key_share_length > extension_length) { return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH); }if (offset + key_share_length > extension_length) { ... } /* Loop through the list of keys looking for a supported group. */ while(offset < extension_length && ecc_key_data == NX_NULL) { if (offset + 4 > extension_length) { return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH); }if (offset + 4 > extension_length) { ... } /* Get the key group. */ key_group = (USHORT)((packet_buffer[offset] << 8) + packet_buffer[offset + 1]); offset = (USHORT)(offset + 2); /* Get the key length. */ key_length = (USHORT)((packet_buffer[offset] << 8) + packet_buffer[offset + 1]); offset = (USHORT)(offset + 2); if (offset + key_length > extension_length) { return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH); }if (offset + key_length > extension_length) { ... } /* Check the key group. */ key_group = (USHORT)((ULONG)key_group | NX_CRYPTO_EC_MASK); /* Loop through all supported ECC curves in this session - see if we support the current group. */ for (i = 0; i < ecc_info -> nx_secure_tls_ecc_supported_groups_count; i++) { /* Find the matching group in the keys we generated and saved. */ if(key_group == tls_session->nx_secure_tls_key_material.nx_secure_tls_ecc_key_data[i].nx_secure_tls_ecdhe_named_curve) { /* Got a matching key/curve combo, get a pointer to the selected key data. */ ecc_key_data = &tls_session->nx_secure_tls_key_material.nx_secure_tls_ecc_key_data[i]; /* Extract the remote public key from our packet. */ pubkey = &packet_buffer[offset]; /* Store selected ECDHE key data index. */ tls_session -> nx_secure_tls_key_material.nx_secure_tls_ecc_key_data_selected = i; break; }if (key_group == tls_session->nx_secure_tls_key_material.nx_secure_tls_ecc_key_data[i].nx_secure_tls_ecdhe_named_curve) { ... } }for (i = 0; i < ecc_info -> nx_secure_tls_ecc_supported_groups_count; i++) { ... } /* Advance past the key data. */ offset = (USHORT)(offset + key_length); }while (offset < extension_length && ecc_key_data == NX_NULL) { ... } /* If we didn't find a matching curve, return error. */ if(ecc_key_data == NX_NULL || offset > extension_length) { tls_session -> nx_secure_tls_server_state = NX_SECURE_TLS_SERVER_STATE_SEND_HELLO_RETRY; return(NX_SUCCESS); }if (ecc_key_data == NX_NULL || offset > extension_length) { ... } /* Extract the legacy form value. NOTE: the form must be included in the ECC calculations below so don't advance the offset! *//* ... */ legacy_form = pubkey[0]; if(legacy_form != 0x4) { /* In TLS 1.3, the only valid form is 0x4. */ return(NX_SECURE_TLS_BAD_CLIENTHELLO_KEYSHARE); }if (legacy_form != 0x4) { ... } /* Get the curve method to initialize the remote public key data. */ _nx_secure_tls_find_curve_method(tls_session, key_group, &curve_method, NX_NULL); if (curve_method == NX_NULL) { return(NX_SECURE_TLS_MISSING_CRYPTO_ROUTINE); }if (curve_method == NX_NULL) { ... } /* Get the ECDHE method we are going to use. */ ecdhe_method = &crypto_method_ecdhe; if (ecdhe_method -> nx_crypto_operation == NX_NULL ) { return(NX_SECURE_TLS_MISSING_CRYPTO_ROUTINE); }if (ecdhe_method -> nx_crypto_operation == NX_NULL) { ... } /* Initialize the ECDHE method. */ if (ecdhe_method -> nx_crypto_init != NX_NULL) { status = ecdhe_method -> nx_crypto_init(ecdhe_method, NX_NULL, 0, &handler, tls_session -> nx_secure_public_cipher_metadata_area, tls_session -> nx_secure_public_cipher_metadata_size); if (status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } }if (ecdhe_method -> nx_crypto_init != NX_NULL) { ... } /* Set the curve we want to use. */ status = ecdhe_method -> nx_crypto_operation(NX_CRYPTO_EC_CURVE_SET, handler, ecdhe_method, NX_NULL, 0, (UCHAR *)curve_method, sizeof(NX_CRYPTO_METHOD *), NX_NULL, NX_NULL, 0, tls_session -> nx_secure_public_cipher_metadata_area, tls_session -> nx_secure_public_cipher_metadata_size, NX_NULL, NX_NULL); if (status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } /* Import the private key to the ECDH context. */ private_key = &ecc_key_data->nx_secure_tls_ecdhe_private_key[0]; private_key_length = ecc_key_data->nx_secure_tls_ecdhe_private_key_length; status = ecdhe_method -> nx_crypto_operation(NX_CRYPTO_DH_KEY_PAIR_IMPORT, handler, (NX_CRYPTO_METHOD*)ecdhe_method, private_key, private_key_length << 3, NX_NULL, 0, NX_NULL, NX_NULL, 0, tls_session -> nx_secure_public_cipher_metadata_area, tls_session -> nx_secure_public_cipher_metadata_size, NX_NULL, NX_NULL); if (status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } //tls_session -> nx_secure_tls_key_material.nx_secure_tls_new_key_material_data[0] = (UCHAR)extended_output.nx_crypto_extended_output_actual_size; /* Calculate the final pre_master_secret - the "private key" here is the Pre-Master Secret. */ extended_output.nx_crypto_extended_output_data = tls_session -> nx_secure_tls_key_material.nx_secure_tls_pre_master_secret; extended_output.nx_crypto_extended_output_length_in_byte = sizeof(tls_session -> nx_secure_tls_key_material.nx_secure_tls_pre_master_secret); extended_output.nx_crypto_extended_output_actual_size = 0; status = ecdhe_method -> nx_crypto_operation(NX_CRYPTO_DH_CALCULATE, handler, ecdhe_method, NX_NULL, 0, pubkey, key_length, NX_NULL, (UCHAR *)&extended_output, sizeof(extended_output), tls_session -> nx_secure_public_cipher_metadata_area, tls_session -> nx_secure_public_cipher_metadata_size, NX_NULL, NX_NULL); if (status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } tls_session -> nx_secure_tls_key_material.nx_secure_tls_pre_master_secret_size = extended_output.nx_crypto_extended_output_actual_size; if (ecdhe_method -> nx_crypto_cleanup) { status = ecdhe_method -> nx_crypto_cleanup(tls_session -> nx_secure_public_cipher_metadata_area); }if (ecdhe_method -> nx_crypto_cleanup) { ... } return(status); }_nx_secure_tls_proc_clienthello_keyshare_extension (NX_SECURE_TLS_SESSION *tls_session, UCHAR *packet_buffer, USHORT extension_length) { ... } /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_tls_proc_clienthello_supported_versions_extension */ /* PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function parses the supported versions extension introduced in */ /* TLS 1.3. */ /* */ /* INPUT */ /* */ /* tls_session TLS control block */ /* packet_buffer Outgoing TLS packet buffer */ /* supported_version Supported version */ /* extension_length Length of extension data */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* _nx_secure_tls_process_clienthello_extensions */ /* Process ClientHello extensions*/ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ static UINT _nx_secure_tls_proc_clienthello_supported_versions_extension(NX_SECURE_TLS_SESSION *tls_session, UCHAR *packet_buffer, USHORT *supported_version, USHORT extension_length) { UINT i; ULONG offset; NX_PARAMETER_NOT_USED(tls_session); /* Supported Versions Extension structure (for clienthello): * * ClientHello: * | 1 | <list length> | * | List Length | TLS Versions | *//* ... */ offset = 0; /* Extract the extension length. */ if ((extension_length) < 1 || (packet_buffer[0] != (extension_length - 1))) { /* Invalid Supported Versions Length. */ return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH); }if ((extension_length) < 1 || (packet_buffer[0] != (extension_length - 1))) { ... } offset = 1; /* Loop through all supported versions in this session. */ for (i = 0; i < (UINT)(extension_length - 1); i += 2) { /* Find the preferred protocol version. */ *supported_version = (USHORT)((packet_buffer[offset] << 8) + packet_buffer[offset + 1]); if (*supported_version == NX_SECURE_TLS_VERSION_TLS_1_3) { return(NX_SUCCESS); }if (*supported_version == NX_SECURE_TLS_VERSION_TLS_1_3) { ... } else if (_nx_secure_tls_check_protocol_version(tls_session, *supported_version, NX_SECURE_TLS) == NX_SUCCESS) { return(NX_SUCCESS); }else if (_nx_secure_tls_check_protocol_version(tls_session, *supported_version, NX_SECURE_TLS) == NX_SUCCESS) { ... } offset = (USHORT)(offset + 2); }for (i = 0; i < (UINT)(extension_length - 1); i += 2) { ... } return(NX_SECURE_TLS_UNSUPPORTED_TLS_VERSION); }_nx_secure_tls_proc_clienthello_supported_versions_extension (NX_SECURE_TLS_SESSION *tls_session, UCHAR *packet_buffer, USHORT *supported_version, USHORT extension_length) { ... } ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_tls_proc_clienthello_signature_algorithms_extension */ /* PORTABLE C */ /* 6.1.9 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function parses the siganture algorithms extension introduced */ /* in TLS 1.3. */ /* */ /* INPUT */ /* */ /* tls_session TLS control block */ /* packet_buffer Outgoing TLS packet buffer */ /* extension_length Length of extension data */ /* */ /* OUTPUT */ /* */ /* None */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* _nx_secure_tls_process_clienthello_extensions */ /* Process ClientHello extensions*/ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* resulting in version 6.1 */ /* 10-15-2021 Timothy Stapko Modified comment(s), fixed */ /* compilation issue with */ /* TLS 1.3 and disabling TLS */ /* server, */ /* resulting in version 6.1.9 */ /* */... /**************************************************************************/ static VOID _nx_secure_tls_proc_clienthello_signature_algorithms_extension(NX_SECURE_TLS_SESSION *tls_session, const UCHAR *packet_buffer, USHORT extension_length) { UINT i; USHORT offset; UINT status = 0; UINT expected_sign_alg = 0; UINT sign_alg = 0; NX_SECURE_X509_CERT *local_certificate = NX_NULL; /* Signature Algorithm Extension structure (for clienthello): * * ClientHello: * | 2 | <list length> | * | List Length | Signature Algorithems | *//* ... */ tls_session -> nx_secure_tls_signature_algorithm = 0; offset = 0; /* Extract the extension length. */ if ((extension_length < 2) || (((packet_buffer[0] << 8) + packet_buffer[1]) != (extension_length - 2))) { /* Invalid Supported Versions Length. */ return; }if ((extension_length < 2) || (((packet_buffer[0] << 8) + packet_buffer[1]) != (extension_length - 2))) { ... } offset = 2; /* Get the local certificate. */ if (tls_session -> nx_secure_tls_credentials.nx_secure_tls_active_certificate != NX_NULL) { local_certificate = tls_session -> nx_secure_tls_credentials.nx_secure_tls_active_certificate; }if (tls_session -> nx_secure_tls_credentials.nx_secure_tls_active_certificate != NX_NULL) { ... } else { /* Get reference to local device certificate. NX_NULL is passed for name to get default entry. */ status = _nx_secure_x509_local_device_certificate_get(&tls_session -> nx_secure_tls_credentials.nx_secure_tls_certificate_store, NX_NULL, &local_certificate); if (status != NX_SUCCESS) { local_certificate = NX_NULL; }if (status != NX_SUCCESS) { ... } }else { ... } if (local_certificate != NX_NULL) { if (local_certificate -> nx_secure_x509_public_algorithm == NX_SECURE_TLS_X509_TYPE_RSA) { return; }if (local_certificate -> nx_secure_x509_public_algorithm == NX_SECURE_TLS_X509_TYPE_RSA) { ... } #ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE else if (local_certificate -> nx_secure_x509_public_algorithm == NX_SECURE_TLS_X509_TYPE_EC) { switch (local_certificate -> nx_secure_x509_private_key.ec_private_key.nx_secure_ec_named_curve) { case NX_CRYPTO_EC_SECP256R1: expected_sign_alg = NX_SECURE_TLS_SIGNATURE_ECDSA_SHA256; break;case NX_CRYPTO_EC_SECP256R1: case NX_CRYPTO_EC_SECP384R1: expected_sign_alg = NX_SECURE_TLS_SIGNATURE_ECDSA_SHA384; break;case NX_CRYPTO_EC_SECP384R1: case NX_CRYPTO_EC_SECP521R1: expected_sign_alg = NX_SECURE_TLS_SIGNATURE_ECDSA_SHA512; break;case NX_CRYPTO_EC_SECP521R1: default: return;default }switch (local_certificate -> nx_secure_x509_private_key.ec_private_key.nx_secure_ec_named_curve) { ... } }else if (local_certificate -> nx_secure_x509_public_algorithm == NX_SECURE_TLS_X509_TYPE_EC) { ... } /* ... */#endif /* NX_SECURE_ENABLE_ECC_CIPHERSUITE */ }if (local_certificate != NX_NULL) { ... } /* Loop through all supported versions in this session. */ for (i = 0; i < (UINT)(extension_length - 2); i += 2) { /* Find the expected signature algorithm. */ sign_alg = (UINT)((packet_buffer[offset] << 8) + packet_buffer[offset + 1]); if (sign_alg == expected_sign_alg) { break; }if (sign_alg == expected_sign_alg) { ... } offset = (USHORT)(offset + 2); }for (i = 0; i < (UINT)(extension_length - 2); i += 2) { ... } /* Make sure we are using the right signature algorithm! */ if (sign_alg != expected_sign_alg) { return; }if (sign_alg != expected_sign_alg) { ... } tls_session -> nx_secure_tls_signature_algorithm = sign_alg; }_nx_secure_tls_proc_clienthello_signature_algorithms_extension (NX_SECURE_TLS_SESSION *tls_session, const UCHAR *packet_buffer, USHORT extension_length) { ... } /* ... */#endif /* NX_SECURE_TLS_TLS_1_3_ENABLED */ /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_tls_get_signature_algorithm_id PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* Get internal x509 ID of signature algorithm. */ /* */ /* INPUT */ /* */ /* signature_algorithm Signature algorithm */ /* signature_algorithm_id Internal ID */ /* */ /* OUTPUT */ /* */ /* None */ /* */ /* CALLS */ /* */ /* */ /* CALLED BY */ /* */ /* */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ VOID _nx_secure_tls_get_signature_algorithm_id(UINT signature_algorithm, USHORT *signature_algorithm_id) { *signature_algorithm_id = NX_SECURE_TLS_X509_TYPE_UNKNOWN; switch (signature_algorithm) { case NX_SECURE_TLS_SIGNATURE_RSA_MD5: *signature_algorithm_id = NX_SECURE_TLS_X509_TYPE_RSA_MD5; break;case NX_SECURE_TLS_SIGNATURE_RSA_MD5: case NX_SECURE_TLS_SIGNATURE_RSA_SHA1: *signature_algorithm_id = NX_SECURE_TLS_X509_TYPE_RSA_SHA_1; break;case NX_SECURE_TLS_SIGNATURE_RSA_SHA1: case NX_SECURE_TLS_SIGNATURE_RSA_SHA256: *signature_algorithm_id = NX_SECURE_TLS_X509_TYPE_RSA_SHA_256; break;case NX_SECURE_TLS_SIGNATURE_RSA_SHA256: case NX_SECURE_TLS_SIGNATURE_RSA_SHA384: *signature_algorithm_id = NX_SECURE_TLS_X509_TYPE_RSA_SHA_384; break;case NX_SECURE_TLS_SIGNATURE_RSA_SHA384: case NX_SECURE_TLS_SIGNATURE_RSA_SHA512: *signature_algorithm_id = NX_SECURE_TLS_X509_TYPE_RSA_SHA_512; break;case NX_SECURE_TLS_SIGNATURE_RSA_SHA512: case NX_SECURE_TLS_SIGNATURE_ECDSA_SHA1: *signature_algorithm_id = NX_SECURE_TLS_X509_TYPE_ECDSA_SHA_1; break;case NX_SECURE_TLS_SIGNATURE_ECDSA_SHA1: case NX_SECURE_TLS_SIGNATURE_ECDSA_SHA224: *signature_algorithm_id = NX_SECURE_TLS_X509_TYPE_ECDSA_SHA_224; break;case NX_SECURE_TLS_SIGNATURE_ECDSA_SHA224: case NX_SECURE_TLS_SIGNATURE_ECDSA_SHA256: *signature_algorithm_id = NX_SECURE_TLS_X509_TYPE_ECDSA_SHA_256; break;case NX_SECURE_TLS_SIGNATURE_ECDSA_SHA256: case NX_SECURE_TLS_SIGNATURE_ECDSA_SHA384: *signature_algorithm_id = NX_SECURE_TLS_X509_TYPE_ECDSA_SHA_384; break;case NX_SECURE_TLS_SIGNATURE_ECDSA_SHA384: case NX_SECURE_TLS_SIGNATURE_ECDSA_SHA512: *signature_algorithm_id = NX_SECURE_TLS_X509_TYPE_ECDSA_SHA_512; break;case NX_SECURE_TLS_SIGNATURE_ECDSA_SHA512: default: return;default }switch (signature_algorithm) { ... } }{ ... } ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_tls_process_clienthello_psk_extension PORTABLE C */ /* 6.1.8 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* Process an incoming TLS 1.3 PSK extension from a TLS Client. */ /* */ /* INPUT */ /* */ /* tls_session TLS control block */ /* packet_buffer Incoming packet data */ /* extension_length Length of PSK extension */ /* */ /* OUTPUT */ /* */ /* None */ /* */ /* CALLS */ /* */ /* */ /* CALLED BY */ /* */ /* */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* verified memcpy use cases, */ /* resulting in version 6.1 */ /* 08-02-2021 Timothy Stapko Modified comment(s), added */ /* hash clone and cleanup, */ /* resulting in version 6.1.8 */ /* */... /**************************************************************************/ #if (NX_SECURE_TLS_TLS_1_3_ENABLED) && defined(NX_SECURE_ENABLE_PSK_CIPHERSUITES) static UINT _nx_secure_tls_process_clienthello_psk_extension(NX_SECURE_TLS_SESSION *tls_session, const UCHAR *packet_buffer, USHORT extension_length, const UCHAR *client_hello_buffer, UINT client_hello_length) { UINT list_length; UCHAR *psk_data; UINT psk_length; UCHAR binder_len; UINT psk_index; UINT binder_index; UINT psk_store_index = 0; ULONG offset; UINT id_len; const UCHAR *id; const UCHAR *binder; INT binder_total; UINT status; UINT age; UINT psk_found = NX_FALSE; UINT partial_hello_length; NX_SECURE_TLS_PSK_STORE *psk_store; /* PSK Extension structure (From TLS 1.3 RFC 8446): struct { opaque identity<1..2^16-1>; uint32 obfuscated_ticket_age; } PskIdentity; opaque PskBinderEntry<32..255>; struct { PskIdentity identities<7..2^16-1>; PskBinderEntry binders<33..2^16-1>; } OfferedPsks; struct { select (Handshake.msg_type) { case client_hello: OfferedPsks; case server_hello: uint16 selected_identity; }; } PreSharedKeyExtension; identity: A label for a key. For instance, a ticket (as defined in Appendix B.3.4) or a label for a pre-shared key established externally. obfuscated_ticket_age: An obfuscated version of the age of the key. Section 4.2.11.1 describes how to form this value for identities established via the NewSessionTicket message. For identities established externally, an obfuscated_ticket_age of 0 SHOULD be used, and servers MUST ignore the value. identities: A list of the identities that the client is willing to negotiate with the server. If sent alongside the "early_data" extension (see Section 4.2.10), the first identity is the one used for 0-RTT data. binders: A series of HMAC values, one for each value in the identities list and in the same order, computed as described below. selected_identity: The server's chosen identity expressed as a (0-based) index into the identities in the client's list. ClientHello PSK extension layout: | 2 | <ID list len> | 2 | <binders len> | | ID list len | <|<ID len> | ID | age |> | binders len | <len> | binder | *//* ... */ if (tls_session == NX_NULL) { return(NX_PTR_ERROR); }if (tls_session == NX_NULL) { ... } offset = 0; if (extension_length < 2) { return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH); }if (extension_length < 2) { ... } /* Get the length of the ID list. (Extension id and length already removed by caller). */ list_length = (USHORT)((packet_buffer[offset] << 8) + packet_buffer[offset + 1]); offset += 2; /* Make sure the length is reasonable. */ if(list_length > extension_length) { return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH); }if (list_length > extension_length) { ... } /* Get our PSK store for easy access. */ psk_store = tls_session->nx_secure_tls_credentials.nx_secure_tls_psk_store; binder_total = 0; psk_index = 0; /* Loop through all IDs. */ while(list_length > 0) { if (list_length < 2) { return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH); }if (list_length < 2) { ... } /* Extract ID length. */ id_len = (USHORT)((packet_buffer[offset] << 8) + packet_buffer[offset + 1]); offset += 2; if(offset + id_len > extension_length || list_length < 2 + id_len + 4) { return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH); }if (offset + id_len > extension_length || list_length < 2 + id_len + 4) { ... } /* Reference to ID data. */ id = &packet_buffer[offset]; offset += id_len; /* Extract the age field. */ age = (UINT)((packet_buffer[offset] << 24) + (packet_buffer[offset + 1] << 16) + (packet_buffer[offset + 2] << 8) + packet_buffer[offset + 3]); offset += 4; /* Only support external PSKs (no session resumption). */ if(age != 0) { return(NX_SECURE_TLS_BAD_CLIENTHELLO_PSK_EXTENSION); }if (age != 0) { ... } /* Size of entry = 2 bytes id_len + <id_len> + 4 bytes age field. */ list_length -= (2 + id_len + 4); /* Check the ID list against our PSK store. */ status = _nx_secure_tls_psk_identity_find(tls_session, &psk_data, &psk_length, (UCHAR*)id, id_len, &psk_store_index); /* No match? Continue. */ if(status == NX_SECURE_TLS_NO_MATCHING_PSK) { /* Advance our selected PSK index. */ psk_index++; continue; }if (status == NX_SECURE_TLS_NO_MATCHING_PSK) { ... } else if(status != NX_SUCCESS) { return(status); }else if (status != NX_SUCCESS) { ... } /* At this point, we have a match - use this PSK if the age and binder check out. * Add the remaining list length to the offset to reach the binder list. *//* ... */ psk_found = NX_TRUE; offset += list_length; break; }while (list_length > 0) { ... } /* If we didn't find a match, return the error. */ if(!psk_found) { return(NX_SECURE_TLS_NO_MATCHING_PSK); }if (!psk_found) { ... } /* Now we can verify the age. */ if (extension_length < offset + 2) { return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH); }if (extension_length < offset + 2) { ... } /* Extract the total binder list length. */ binder_total = (USHORT)((packet_buffer[offset] << 8) + packet_buffer[offset + 1]); offset += 2; /* Make sure the length is reasonable. */ if((UINT)binder_total + offset > extension_length) { return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH); }if ((UINT)binder_total + offset > extension_length) { ... } /* We need to subtract the binders from the ClientHello for our binder generation since the hash for the binder is generated from a partial ClientHello before the binders are generated. Include 2 bytes for the length field. *//* ... */ partial_hello_length = client_hello_length - ((UINT)binder_total + 2); binder = NX_NULL; binder_index = 0; /* Loop through the binders to get the binder for our selected PSK. */ while(binder_total > 0) { /* Extract binder length. */ binder_len = packet_buffer[offset]; offset++; if (1 + binder_len > binder_total) { return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH); }if (1 + binder_len > binder_total) { ... } /* Subtract the binder length and field from the total. */ binder_total--; binder_total -= binder_len; /* Check our current binder index against the selected PSK index. */ if(binder_index == psk_index) { /* Extract binder and exit loop. */ binder = &packet_buffer[offset]; break; }if (binder_index == psk_index) { ... } /* Advance the binder index. */ binder_index++; }while (binder_total > 0) { ... } if(binder == NX_NULL) { /* Binder is missing! */ return(NX_SECURE_TLS_BAD_CLIENTHELLO_PSK_EXTENSION); }if (binder == NX_NULL) { ... } /* Check binder against locally-generated version. */ /* If nx_secure_tls_psk_data_size is zero, it means this is ClientHello1, need to initialize the handshake hash. If not, it means this is ClientHello2, need to save the metadata(ClientHello1 and HelloRetryRequest) to sratch buffer. *//* ... */ if (tls_session -> nx_secure_tls_credentials.nx_secure_tls_client_psk.nx_secure_tls_psk_data_size == 0) { /* Initialize the handshake hash to hash the ClientHello up to now. */ _nx_secure_tls_handshake_hash_init(tls_session); }if (tls_session -> nx_secure_tls_credentials.nx_secure_tls_client_psk.nx_secure_tls_psk_data_size == 0) { ... } else { /* Save the handshake hash state. */ NX_SECURE_HASH_METADATA_CLONE(tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_scratch, tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha256_metadata, tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha256_metadata_size); /* Use case of memcpy is verified. */ }else { ... } /* Hash the ClientHello, adding the TLS record header. */ UCHAR header[] = { 0x01, 0x00, 0x00, 0x00 }; header[2] = (UCHAR)((client_hello_length) >> 8); header[3] = (UCHAR)((client_hello_length) & 0xFF); _nx_secure_tls_handshake_hash_update(tls_session, header, sizeof(header)); _nx_secure_tls_handshake_hash_update(tls_session, (UCHAR*)client_hello_buffer, partial_hello_length); /* Save the transcript hash for the ClientHello, which is used in generating the PSK binders. */ status = _nx_secure_tls_1_3_transcript_hash_save(tls_session, NX_SECURE_TLS_TRANSCRIPT_IDX_CLIENTHELLO, NX_FALSE); if (status != NX_SUCCESS) { if (tls_session -> nx_secure_tls_credentials.nx_secure_tls_client_psk.nx_secure_tls_psk_data_size) { NX_SECURE_HASH_CLONE_CLEANUP(tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_scratch, tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha256_metadata_size); }if (tls_session -> nx_secure_tls_credentials.nx_secure_tls_client_psk.nx_secure_tls_psk_data_size) { ... } return(status); }if (status != NX_SUCCESS) { ... } /* Restore the original metadata(ClientHello1 and HelloRetryRequest) from scratch buffer. */ if (tls_session -> nx_secure_tls_credentials.nx_secure_tls_client_psk.nx_secure_tls_psk_data_size) { NX_SECURE_HASH_CLONE_CLEANUP(tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha256_metadata, tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha256_metadata_size); NX_SECURE_HASH_METADATA_CLONE(tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha256_metadata, tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_scratch, tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha256_metadata_size); /* Use case of memcpy is verified. */ NX_SECURE_HASH_CLONE_CLEANUP(tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_scratch, tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_sha256_metadata_size); }if (tls_session -> nx_secure_tls_credentials.nx_secure_tls_client_psk.nx_secure_tls_psk_data_size) { ... } /* Generate the binder for our selected PSK. */ _nx_secure_tls_psk_binder_generate(tls_session, &psk_store[psk_store_index]); /* Compare the generated binder to the received binder, using our generated length to avoid overflow from incoming length problems. *//* ... */ status = (UINT)NX_SECURE_MEMCMP(binder, psk_store[psk_store_index].nx_secure_tls_psk_binder, psk_store[psk_store_index].nx_secure_tls_psk_binder_size); /* Make sure the generated binder matches the one sent by the client. */ if((status != 0) || (binder_len != psk_store[psk_store_index].nx_secure_tls_psk_binder_size)) { return(NX_SECURE_TLS_PSK_BINDER_MISMATCH); }if ((status != 0) || (binder_len != psk_store[psk_store_index].nx_secure_tls_psk_binder_size)) { ... } /* The PSK is too big to save in our internal buffer. */ if(psk_length > NX_SECURE_TLS_MAX_PSK_SIZE) { return(NX_SECURE_TLS_NO_MORE_PSK_SPACE); }if (psk_length > NX_SECURE_TLS_MAX_PSK_SIZE) { ... } /* Make sure the Client PSK is initialized for later key generation. */ NX_SECURE_MEMCPY(tls_session->nx_secure_tls_credentials.nx_secure_tls_client_psk.nx_secure_tls_psk_data, psk_data, psk_length); /* Use case of memcpy is verified. */ tls_session->nx_secure_tls_credentials.nx_secure_tls_client_psk.nx_secure_tls_psk_data_size = psk_length; return(NX_SUCCESS); }_nx_secure_tls_process_clienthello_psk_extension (NX_SECURE_TLS_SESSION *tls_session, const UCHAR *packet_buffer, USHORT extension_length, const UCHAR *client_hello_buffer, UINT client_hello_length) { ... } /* ... */#endif ...