Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define NX_SECURE_SOURCE_CODE
#include "nx_secure_x509.h"
...
...
_nx_secure_x509_certificate_parse(const UCHAR *, UINT, UINT *, NX_SECURE_X509_CERT *)
...
...
_nx_secure_x509_extract_oid_data(const UCHAR *, UINT, UINT, ULONG, UINT *, NX_SECURE_X509_CERT *)
...
...
_nx_secure_x509_parse_cert_data(const UCHAR *, ULONG, UINT *, NX_SECURE_X509_CERT *)
...
...
_nx_secure_x509_parse_version(const UCHAR *, ULONG, UINT *, NX_SECURE_X509_CERT *)
...
...
_nx_secure_x509_parse_serial_num(const UCHAR *, ULONG, UINT *, NX_SECURE_X509_CERT *)
...
...
_nx_secure_x509_parse_signature_algorithm(const UCHAR *, ULONG, UINT *, NX_SECURE_X509_CERT *)
...
...
_nx_secure_x509_parse_issuer(const UCHAR *, ULONG, UINT *, NX_SECURE_X509_CERT *)
...
...
_nx_secure_x509_parse_validity(const UCHAR *, ULONG, UINT *, NX_SECURE_X509_CERT *)
...
...
_nx_secure_x509_parse_subject(const UCHAR *, ULONG, UINT *, NX_SECURE_X509_CERT *)
...
...
_nx_secure_x509_parse_public_key(const UCHAR *, ULONG, UINT *, NX_SECURE_X509_CERT *)
...
...
_nx_secure_x509_parse_unique_ids(const UCHAR *, ULONG, UINT *, NX_SECURE_X509_CERT *)
...
...
_nx_secure_x509_parse_extensions(const UCHAR *, ULONG, UINT *, NX_SECURE_X509_CERT *)
...
...
_nx_secure_x509_parse_signature_data(const UCHAR *, ULONG, UINT *, NX_SECURE_X509_CERT *)
Files
loading...
CodeScopeSTM32 Libraries and Samplesnetxduonx_secure/src/nx_secure_x509.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
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* 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 */ /** */ /** X.509 Digital Certificates */ /** */... /**************************************************************************/ /**************************************************************************/ #define NX_SECURE_SOURCE_CODE #include "nx_secure_x509.h" static UINT _nx_secure_x509_parse_cert_data(const UCHAR *buffer, ULONG length, UINT *bytes_processed, NX_SECURE_X509_CERT *cert); static UINT _nx_secure_x509_parse_version(const UCHAR *buffer, ULONG length, UINT *bytes_processed, NX_SECURE_X509_CERT *cert); static UINT _nx_secure_x509_parse_serial_num(const UCHAR *buffer, ULONG length, UINT *bytes_processed, NX_SECURE_X509_CERT *cert); static UINT _nx_secure_x509_parse_signature_algorithm(const UCHAR *buffer, ULONG length, UINT *bytes_processed, NX_SECURE_X509_CERT *cert); static UINT _nx_secure_x509_parse_issuer(const UCHAR *buffer, ULONG length, UINT *bytes_processed, NX_SECURE_X509_CERT *cert); static UINT _nx_secure_x509_parse_validity(const UCHAR *buffer, ULONG length, UINT *bytes_processed, NX_SECURE_X509_CERT *cert); static UINT _nx_secure_x509_parse_subject(const UCHAR *buffer, ULONG length, UINT *bytes_processed, NX_SECURE_X509_CERT *cert); static UINT _nx_secure_x509_parse_public_key(const UCHAR *buffer, ULONG length, UINT *bytes_processed, NX_SECURE_X509_CERT *cert); static UINT _nx_secure_x509_parse_unique_ids(const UCHAR *buffer, ULONG length, UINT *bytes_processed, NX_SECURE_X509_CERT *cert); static UINT _nx_secure_x509_parse_extensions(const UCHAR *buffer, ULONG length, UINT *bytes_processed, NX_SECURE_X509_CERT *cert); static UINT _nx_secure_x509_parse_signature_data(const UCHAR *buffer, ULONG length, UINT *bytes_processed, NX_SECURE_X509_CERT *cert); static UINT _nx_secure_x509_extract_oid_data(const UCHAR *buffer, UINT oid, UINT oid_param, ULONG length, UINT *bytes_processed, NX_SECURE_X509_CERT *cert); ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_x509_certificate_parse PORTABLE C */ /* 6.1.6 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function parses a DER-encoded X509 digital certificate and */ /* up pointers in the NX_SECURE_X509_CERT structure to the various */ /* fields needed by applications such as TLS. Note that to save memory */ /* the data is left in-place and not copied out into separate buffers. */ /* */ /* INPUT */ /* */ /* buffer Pointer data to be parsed */ /* length Length of data in buffer */ /* bytes_processed Return bytes processed */ /* cert Return X509 structure */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* _nx_secure_x509_asn1_tlv_block_parse Parse ASN.1 block */ /* _nx_secure_x509_parse_cert_data Parse certificate */ /* _nx_secure_x509_parse_signature_algorithm */ /* Parse signature algorithm in */ /* certificate */ /* _nx_secure_x509_parse_signature_data Parse signature data in */ /* certificate */ /* */ /* CALLED BY */ /* */ /* _nx_secure_tls_process_remote_certificate */ /* Process server certificate */ /* _nx_secure_x509_certificate_initialize */ /* Initialize certificate */ /* */ /* 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 */ /* 04-02-2021 Timothy Stapko Modified comment(s), */ /* removed dependency on TLS, */ /* resulting in version 6.1.6 */ /* */... /**************************************************************************/ UINT _nx_secure_x509_certificate_parse(const UCHAR *buffer, UINT length, UINT *bytes_processed, NX_SECURE_X509_CERT *cert) { USHORT tlv_type; USHORT tlv_type_class; ULONG tlv_length; UINT bytes; const UCHAR *tlv_data; ULONG header_length; UINT status; /* X509 Certificate structure: * ASN.1 sequence: Certificate * Sequence: Certificate data * Version (e.g. X509v3) * Serial Number * Sequence: Signature algorithm * Signature Algorithms (e.g. RSA_SHA-256) * NULL * Sequence: Issuer * Set: Issuer information (multiple sets here - one per OID) * Sequence: Information (multiple sequences here?) * ASN.1 OID (e.g. Country/Region) * ASN.1 String (e.g. "US") * ... * Sequence: Validity Period * UTC Time: Not before (ASN.1 time value) * UTC Time: Not After * Sequence: Subject info (Common Name, etc. for this certificate) * Set: Subject information (multiple sets here - one per OID) * Sequence: Information (multiple sequences here?) * ASN.1 OID (e.g. Country/Region) * ASN.1 String (e.g. "US") * ... * Sequence: Public Key * Sequence: Public key data * ASN.1 OID (e.g. RSA) * NULL * ASN.1 Bit string - contains embedded key data in ASN.1 format: * { * 1 byte: padding (always 0?) * ASN.1 Key information - sequence of 2 integers for RSA: * Public Modulus followed by Public Exponent * } * ASN.1 Bit String: Extensions * { * ASN.1-encoded data for extensions * } * Sequence: Signature algorithm data * ASN.1 OID (E.g RSA_SHA256) * NULL * ASN.1 Bit string: Signature data * * Parsing of this structure is as follows: * - Each sequence is parsed by it's own function * - The structure of each recursive sequence/set is encapsulated in the function that parses it * - Some functions may be used in multiple places (e.g. subject info parsing) * - At the lowest level, all parsing will be done by the ASN.1 TLV block parser. *//* ... */ /* Parse a TLV block and get information to continue parsing. */ status = _nx_secure_x509_asn1_tlv_block_parse(buffer, (ULONG *)&length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length); /* Make sure we parsed the block alright. */ if (status != 0) { return(status); }if (status != 0) { ... } if (tlv_type != NX_SECURE_ASN_TAG_SEQUENCE || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { return(NX_SECURE_X509_INVALID_CERTIFICATE_SEQUENCE); }if (tlv_type != NX_SECURE_ASN_TAG_SEQUENCE || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { ... } *bytes_processed = header_length; length = tlv_length; /* Next block should be the certificate data. */ status = _nx_secure_x509_parse_cert_data(tlv_data, tlv_length, &bytes, cert); if (status != 0) { return(status); }if (status != 0) { ... } *bytes_processed += bytes; /* Following the certificate data is the signature algorithm data. */ tlv_data = &tlv_data[bytes]; length -= bytes; status = _nx_secure_x509_parse_signature_algorithm(tlv_data, length, &bytes, cert); if (status != 0) { return(status); }if (status != 0) { ... } *bytes_processed += bytes; /* Finally, the signature itself is at the end of the certificate. */ tlv_data = &tlv_data[bytes]; length -= bytes; status = _nx_secure_x509_parse_signature_data(tlv_data, length, &bytes, cert); if (status != 0) { return(status); }if (status != 0) { ... } /* Return the number of bytes we processed. */ *bytes_processed += bytes; if (cert -> nx_secure_x509_public_algorithm != NX_SECURE_TLS_X509_TYPE_RSA #ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE && cert -> nx_secure_x509_public_algorithm != NX_SECURE_TLS_X509_TYPE_EC #endif /* NX_SECURE_ENABLE_ECC_CIPHERSUITE */ ) { return(NX_SECURE_X509_UNSUPPORTED_PUBLIC_CIPHER); }if (cert -> nx_secure_x509_public_algorithm != NX_SECURE_TLS_X509_TYPE_RSA #ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE && cert -> nx_secure_x509_public_algorithm != NX_SECURE_TLS_X509_TYPE_EC #endif /* NX_SECURE_ENABLE_ECC_CIPHERSUITE */) { ... } /* Successfully parsed an X509 certificate. */ return(NX_SECURE_X509_SUCCESS); }{ ... } /* --- Helper functions. --- */ ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_x509_extract_oid_data PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function extracts data from the certificate buffer associated */ /* with an OID, but not part of a Distinguished Name Field or */ /* extensions. This generally includes public keys and algorithm */ /* identifiers. */ /* */ /* INPUT */ /* */ /* buffer Pointer data to be parsed */ /* oid Data type in OID */ /* oid_param Public key parameter OID */ /* length Length of data in buffer */ /* bytes_processed Number of bytes being */ /* consumed by the oid */ /* cert The certificate */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* _nx_secure_x509_asn1_tlv_block_parse Parse ASN.1 block */ /* */ /* CALLED BY */ /* */ /* _nx_secure_x509_parse_public_key Parse public key in */ /* */ /* 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_x509_extract_oid_data(const UCHAR *buffer, UINT oid, UINT oid_param, ULONG length, UINT *bytes_processed, NX_SECURE_X509_CERT *cert) { USHORT tlv_type; USHORT tlv_type_class; ULONG tlv_length; const UCHAR *tlv_data; const UCHAR *sequence_data; ULONG header_length; UINT status; #ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE NX_SECURE_EC_PUBLIC_KEY *ec_pubkey; #else NX_CRYPTO_PARAMETER_NOT_USED(oid_param); #endif /* NX_SECURE_ENABLE_ECC_CIPHERSUITE */ #ifdef NX_CRYPTO_STANDALONE_ENABLE if (cert == NX_CRYPTO_NULL) { return(NX_CRYPTO_PTR_ERROR); }if (cert == NX_CRYPTO_NULL) { ... } /* ... */#else NX_ASSERT(cert != NX_CRYPTO_NULL); #endif /* NX_CRYPTO_STANDALONE_ENABLE */ /* IMPORTANT NOTE: This function MUST handle a NULL value for the "cert" parameter - we need to parse the * certificate data no matter what, and in some cases we might want to parse past the data rather than saving * it off in the cert structure. By passing the structure as NULL, this function should just pass over the parsed data. *//* ... */ status = _nx_secure_x509_asn1_tlv_block_parse(buffer, &length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length); if (status != 0) { *bytes_processed = 0; return(status); }if (status != 0) { ... } *bytes_processed = (header_length + tlv_length); /* The RSA OID represents an ASN.1 structure rather than a simple field so handle it differently. */ if (oid == NX_SECURE_TLS_X509_TYPE_RSA) { /* RSA public key. ASN.1 formatted string with a leading 0 byte. */ if (tlv_length < 1) { return(NX_SECURE_X509_ASN1_LENGTH_TOO_LONG); }if (tlv_length < 1) { ... } /* Parse the RSA bitstring - it is preceeded by a byte indicating the number of padding bytes * added. Should always be 0. *//* ... */ if (tlv_data[0] != 0) { return(NX_SECURE_X509_FOUND_NON_ZERO_PADDING); }if (tlv_data[0] != 0) { ... } length = tlv_length - 1; /* First is a sequence. */ status = _nx_secure_x509_asn1_tlv_block_parse(&tlv_data[1], &length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length); if (status || tlv_type != NX_SECURE_ASN_TAG_SEQUENCE || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { return(NX_SECURE_X509_MISSING_PUBLIC_KEY); }if (status || tlv_type != NX_SECURE_ASN_TAG_SEQUENCE || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { ... } sequence_data = tlv_data; length = tlv_length; /* Inside is the public modulus. */ status = _nx_secure_x509_asn1_tlv_block_parse(sequence_data, &length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length); if (status || tlv_type != NX_SECURE_ASN_TAG_INTEGER || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { return(NX_SECURE_X509_INVALID_PUBLIC_KEY); }if (status || tlv_type != NX_SECURE_ASN_TAG_INTEGER || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { ... } /* The modulus has a 0 byte at the front we need to skip. * This is due to the modulus being encoded as an ASN.1 bit string, which may * require padding bits to get to a multiple of 8 for byte alignment. The byte * represents the number of padding bits, but in X509 it should always be 0. *//* ... */ cert -> nx_secure_x509_public_key.rsa_public_key.nx_secure_rsa_public_modulus = &tlv_data[1]; cert -> nx_secure_x509_public_key.rsa_public_key.nx_secure_rsa_public_modulus_length = (USHORT)(tlv_length - 1); /* Finally the public exponent. */ status = _nx_secure_x509_asn1_tlv_block_parse(&sequence_data[header_length + tlv_length], &length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length); if (status || tlv_type != NX_SECURE_ASN_TAG_INTEGER || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { return(NX_SECURE_X509_INVALID_PUBLIC_KEY); }if (status || tlv_type != NX_SECURE_ASN_TAG_INTEGER || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { ... } /* Extract a pointer to the public exponent. */ cert -> nx_secure_x509_public_key.rsa_public_key.nx_secure_rsa_public_exponent = tlv_data; cert -> nx_secure_x509_public_key.rsa_public_key.nx_secure_rsa_public_exponent_length = (USHORT)tlv_length; }if (oid == NX_SECURE_TLS_X509_TYPE_RSA) { ... } #ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE else if (oid == NX_SECURE_TLS_X509_TYPE_EC) { /* EC public key. */ /* Parse the EC bitstring - it is preceeded by a byte indicating the number of padding bytes * added. Should always be 0. *//* ... */ if (tlv_data[0] != 0) { return(NX_SECURE_X509_FOUND_NON_ZERO_PADDING); }if (tlv_data[0] != 0) { ... } ec_pubkey = &cert -> nx_secure_x509_public_key.ec_public_key; ec_pubkey -> nx_secure_ec_public_key = &tlv_data[1]; ec_pubkey -> nx_secure_ec_public_key_length = (USHORT)(tlv_length - 1); ec_pubkey -> nx_secure_ec_named_curve = oid_param; }else if (oid == NX_SECURE_TLS_X509_TYPE_EC) { ... } /* ... */#endif /* NX_SECURE_ENABLE_ECC_CIPHERSUITE */ /* Any additional special-case handling should go here. */ switch (oid) { case NX_SECURE_TLS_X509_TYPE_RSA_MD5: case NX_SECURE_TLS_X509_TYPE_RSA_SHA_1: case NX_SECURE_TLS_X509_TYPE_RSA_SHA_256: case NX_SECURE_TLS_X509_TYPE_RSA_SHA_384: case NX_SECURE_TLS_X509_TYPE_RSA_SHA_512: case NX_SECURE_TLS_X509_TYPE_DSS_SHA_1: case NX_SECURE_TLS_X509_TYPE_DH: case NX_SECURE_TLS_X509_TYPE_RSA: /*Public keys require additional parsing, handled above. */case NX_SECURE_TLS_X509_TYPE_RSA: default: break;default }switch (oid) { ... } /* We have successfully extracted our data based on the OID. */ return(NX_SECURE_X509_SUCCESS); }{ ... } /* Parse the sequence that contains all the certificate data (e.g. Common Name and public key). */ /* Version (e.g. X509v3) * Serial Number * Sequence: Signature algorithm (NULL-terminated) * Sequence: Issuer * Sequence: Validity Period * Sequence: Subject info (Common Name, etc. for this certificate) * Sequence: Public Key * ASN.1 Bit String: Extensions *//* ... */ .../**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_x509_parse_cert_data PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function parses the ASN.1 sequence in an X.509 certificate that */ /* contains the identity information for the certificate. This is the */ /* high-level container for all the certificate information. */ /* */ /* INPUT */ /* */ /* buffer Pointer data to be parsed */ /* length Return bytes processed */ /* bytes_processed Number of bytes being */ /* consumed by the oid */ /* cert The certificate */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* _nx_secure_x509_asn1_tlv_block_parse Parse ASN.1 block */ /* _nx_secure_x509_parse_extensions Parse extension in certificate*/ /* _nx_secure_x509_parse_issuer Parse issuer in certificate */ /* _nx_secure_x509_parse_public_key Parse public key in */ /* certificate */ /* _nx_secure_x509_parse_serial_num Parse serial number in */ /* certificate */ /* _nx_secure_x509_parse_signature_algorithm */ /* Parse signature algorithm in */ /* certificate */ /* _nx_secure_x509_parse_subject Parse subject in certificate */ /* _nx_secure_x509_parse_unique_ids Parse unique IDs in */ /* certificate */ /* _nx_secure_x509_parse_validity Parse validity in certificate */ /* _nx_secure_x509_parse_version Parse version in certificate */ /* */ /* CALLED BY */ /* */ /* _nx_secure_x509_certificate_parse Extract public key data */ /* */ /* 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_x509_parse_cert_data(const UCHAR *buffer, ULONG length, UINT *bytes_processed, NX_SECURE_X509_CERT *cert) { USHORT tlv_type; USHORT tlv_type_class; ULONG tlv_length; UINT bytes; UINT cur_length; const UCHAR *tlv_data; ULONG header_length; UINT status; /* Parse a TLV block and get information to continue parsing. */ status = _nx_secure_x509_asn1_tlv_block_parse(buffer, &length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length); /* Make sure we parsed the block alright. */ if (status != 0) { return(status); }if (status != 0) { ... } /* Buffer should contain the certificate data sequence. */ if (tlv_type != NX_SECURE_ASN_TAG_SEQUENCE || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { return(NX_SECURE_X509_INVALID_CERTIFICATE_DATA); }if (tlv_type != NX_SECURE_ASN_TAG_SEQUENCE || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { ... } /* We need to save off a pointer to the certificate data and its length for signature generation and verification later on. The certificate data for verification includes the encapsulating ASN.1 sequence header. *//* ... */ cert -> nx_secure_x509_certificate_data = buffer; cert -> nx_secure_x509_certificate_data_length = tlv_length + header_length; *bytes_processed = header_length; /* First up is the version data. */ status = _nx_secure_x509_parse_version(tlv_data, tlv_length, &bytes, cert); if (status != 0) { return(status); }if (status != 0) { ... } cur_length = tlv_length; /* Next is the serial number. */ tlv_data = &tlv_data[bytes]; cur_length -= bytes; *bytes_processed += bytes; status = _nx_secure_x509_parse_serial_num(tlv_data, cur_length, &bytes, cert); if (status != 0) { return(status); }if (status != 0) { ... } /* Signature algorithm (embedded in the certificate information). */ tlv_data = &tlv_data[bytes]; cur_length -= bytes; *bytes_processed += bytes; status = _nx_secure_x509_parse_signature_algorithm(tlv_data, cur_length, &bytes, cert); if (status != 0) { return(status); }if (status != 0) { ... } /* Issuer data. */ tlv_data = &tlv_data[bytes]; cur_length -= bytes; *bytes_processed += bytes; status = _nx_secure_x509_parse_issuer(tlv_data, cur_length, &bytes, cert); if (status != 0) { return(status); }if (status != 0) { ... } /* Validity period. */ tlv_data = &tlv_data[bytes]; cur_length -= bytes; *bytes_processed += bytes; status = _nx_secure_x509_parse_validity(tlv_data, cur_length, &bytes, cert); if (status != 0) { return(status); }if (status != 0) { ... } /* Subject information. */ tlv_data = &tlv_data[bytes]; cur_length -= bytes; *bytes_processed += bytes; status = _nx_secure_x509_parse_subject(tlv_data, cur_length, &bytes, cert); if (status != 0) { return(status); }if (status != 0) { ... } /* Public key. */ tlv_data = &tlv_data[bytes]; cur_length -= bytes; *bytes_processed += bytes; status = _nx_secure_x509_parse_public_key(tlv_data, cur_length, &bytes, cert); if (status != 0) { return(status); }if (status != 0) { ... } /* Optional Issuer and Subject Unique IDs. */ tlv_data = &tlv_data[bytes]; cur_length -= bytes; *bytes_processed += bytes; /* The following fields are all optional, so stop parsing with success if they were not included. *//* ... */ if (cur_length == 0) { return(NX_SECURE_X509_SUCCESS); }if (cur_length == 0) { ... } status = _nx_secure_x509_parse_unique_ids(tlv_data, cur_length, &bytes, cert); if (status != 0) { return(status); }if (status != 0) { ... } *bytes_processed += bytes; /* Parse extensions. */ tlv_data = &tlv_data[bytes]; cur_length -= bytes; *bytes_processed += bytes; /* The following fields are all optional, so stop parsing with success if they were not included. *//* ... */ if (cur_length == 0) { return(NX_SECURE_X509_SUCCESS); }if (cur_length == 0) { ... } status = _nx_secure_x509_parse_extensions(tlv_data, cur_length, &bytes, cert); if (status != 0) { return(status); }if (status != 0) { ... } *bytes_processed += bytes; /* Parsed X509 certificate data successfully. */ return(NX_SECURE_X509_SUCCESS); }{ ... } /* X509 certificate version parsing. NOTE: X509v1 certificates do NOT have a version field! */... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_x509_parse_version PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function parses the optional version field in an X.509 */ /* certificate. If the field is omitted, the certificate must be a */ /* version 1 X.509 certificate. */ /* */ /* INPUT */ /* */ /* buffer Pointer data to be parsed */ /* length Return bytes processed */ /* bytes_processed Number of bytes being */ /* consumed by the oid */ /* cert The certificate */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* _nx_secure_x509_asn1_tlv_block_parse Parse ASN.1 block */ /* */ /* CALLED BY */ /* */ /* _nx_secure_x509_parse_cert_data Parse certificate */ /* */ /* 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_x509_parse_version(const UCHAR *buffer, ULONG length, UINT *bytes_processed, NX_SECURE_X509_CERT *cert) { USHORT tlv_type; USHORT tlv_type_class; ULONG tlv_length; const UCHAR *tlv_data; ULONG header_length; UINT status; NX_CRYPTO_PARAMETER_NOT_USED(cert); /* Parse a TLV block and get information to continue parsing. */ status = _nx_secure_x509_asn1_tlv_block_parse(buffer, &length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length); /* Make sure we parsed the block alright. */ if (status != 0) { return(status); }if (status != 0) { ... } /* Check to see if version exists. If the version is present it will have a type * of NX_SECURE_ASN_TAG_BER. Otherwise it is an X509v1 certificate or invalid * (determined higher up the call stack). *//* ... */ if (tlv_type == NX_SECURE_ASN_TAG_BER && tlv_type_class == NX_SECURE_ASN_TAG_CLASS_CONTEXT) { /* Version is the payload byte. */ cert -> nx_secure_x509_version = (USHORT)tlv_data[0]; /* Return the number of bytes we processed. */ *bytes_processed = header_length + tlv_length; }if (tlv_type == NX_SECURE_ASN_TAG_BER && tlv_type_class == NX_SECURE_ASN_TAG_CLASS_CONTEXT) { ... } else { /* No Version field found, assume X509v1. */ cert -> nx_secure_x509_version = NX_SECURE_X509_VERSION_1; /* No bytes processed since the field was omitted. */ *bytes_processed = 0; }else { ... } /* Always return success - this field is optional. */ return(NX_SECURE_X509_SUCCESS); }{ ... } /* X509 certificate serial number parsing. */... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_x509_parse_serial_num PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function parses the serial number field of an X.509 */ /* certificate. */ /* */ /* INPUT */ /* */ /* buffer Pointer data to be parsed */ /* length Return bytes processed */ /* bytes_processed Number of bytes being */ /* consumed by the oid */ /* cert The certificate */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* _nx_secure_x509_asn1_tlv_block_parse Parse ASN.1 block */ /* */ /* CALLED BY */ /* */ /* _nx_secure_x509_parse_cert_data Parse certificate */ /* */ /* 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_x509_parse_serial_num(const UCHAR *buffer, ULONG length, UINT *bytes_processed, NX_SECURE_X509_CERT *cert) { USHORT tlv_type; USHORT tlv_type_class; ULONG tlv_length; const UCHAR *tlv_data; ULONG header_length; UINT status; NX_CRYPTO_PARAMETER_NOT_USED(cert); /* Parse a TLV block and get information to continue parsing. */ status = _nx_secure_x509_asn1_tlv_block_parse(buffer, &length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length); /* Make sure we parsed the block alright. */ if (status != 0) { return(status); }if (status != 0) { ... } /* Check to see if version exists. If the version is present it will have a type * of NX_SECURE_ASN_TAG_BER. Otherwise it is an X509v1 certificate or invalid * (determined higher up the call stack). *//* ... */ if (tlv_type != NX_SECURE_ASN_TAG_INTEGER || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { return(NX_SECURE_X509_UNEXPECTED_ASN1_TAG); }if (tlv_type != NX_SECURE_ASN_TAG_INTEGER || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { ... } /* Save off the serial number in case someone wants it. */ cert -> nx_secure_x509_serial_number = tlv_data; cert -> nx_secure_x509_serial_number_length = (USHORT)tlv_length; /* Return the number of bytes we processed. */ *bytes_processed = header_length + tlv_length; return(NX_SECURE_X509_SUCCESS); }{ ... } /* X509 certificate signature algorithm parsing. Used at both the top level and in the certificate sequence. */... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_x509_parse_signature_algorithm PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function parses the signature algorithm field in an X.509 */ /* certificate. The signature algorithm is found both in certificate */ /* data sequence and with the public key information. */ /* */ /* INPUT */ /* */ /* buffer Pointer data to be parsed */ /* length Return bytes processed */ /* bytes_processed Number of bytes being */ /* consumed by the oid */ /* cert The certificate */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* _nx_secure_x509_asn1_tlv_block_parse Parse ASN.1 block */ /* _nx_secure_x509_oid_parse Parse OID in certificate */ /* */ /* CALLED BY */ /* */ /* _nx_secure_x509_parse_cert_data Parse certificate */ /* _nx_secure_x509_certificate_parse Extract public key data */ /* */ /* 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_x509_parse_signature_algorithm(const UCHAR *buffer, ULONG length, UINT *bytes_processed, NX_SECURE_X509_CERT *cert) { USHORT tlv_type; USHORT tlv_type_class; ULONG tlv_length; const UCHAR *tlv_data; UINT oid; ULONG header_length; UINT status; UCHAR oid_found = NX_CRYPTO_FALSE; /* The signature algorithm is an OID and has optionally associated parameters. */ *bytes_processed = 0; /* First, parse the sequence. */ status = _nx_secure_x509_asn1_tlv_block_parse(buffer, &length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length); /* Make sure we parsed the block alright. */ if (status != 0) { return(status); }if (status != 0) { ... } if (tlv_type != NX_SECURE_ASN_TAG_SEQUENCE || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { return(NX_SECURE_X509_UNEXPECTED_ASN1_TAG); }if (tlv_type != NX_SECURE_ASN_TAG_SEQUENCE || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { ... } /* Limit the length to the sequence length. */ length = tlv_length; /* While the tag is not NULL and we don't run out of buffer, keep parsing OIDs. */ *bytes_processed += header_length; while (length > 0) { status = _nx_secure_x509_asn1_tlv_block_parse(tlv_data, &length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length); if (status != 0) { return status; }if (status != 0) { ... } /* Update bytes processed. */ *bytes_processed += (header_length + tlv_length); /* Now check out what we got. */ if (tlv_type == NX_SECURE_ASN_TAG_OID) { _nx_secure_x509_oid_parse(tlv_data, tlv_length, &oid); cert -> nx_secure_x509_signature_algorithm = oid; oid_found = NX_CRYPTO_TRUE; }if (tlv_type == NX_SECURE_ASN_TAG_OID) { ... } else if (tlv_type == NX_SECURE_ASN_TAG_NULL) { break; }else if (tlv_type == NX_SECURE_ASN_TAG_NULL) { ... } else { return(NX_SECURE_X509_UNEXPECTED_ASN1_TAG); }else { ... } /* Advance the data pointer. */ tlv_data = &tlv_data[tlv_length]; }while (length > 0) { ... } if (oid_found == NX_CRYPTO_TRUE) { return(NX_SECURE_X509_SUCCESS); }if (oid_found == NX_CRYPTO_TRUE) { ... } /* We were expecting a signature algorithm OID but didn't find one. */ return(NX_SECURE_X509_MISSING_SIGNATURE_ALGORITHM); }{ ... } ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_x509_parse_issuer PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function parses the issuer distinguished name in an X.509 */ /* certificate. The issuer distinguished name is used to link the */ /* certificate to the CA certificate that signed it. */ /* */ /* INPUT */ /* */ /* buffer Pointer data to be parsed */ /* length Return bytes processed */ /* bytes_processed Number of bytes being */ /* consumed by the oid */ /* cert The certificate */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* _nx_secure_x509_asn1_tlv_block_parse Parse ASN.1 block */ /* _nx_secure_x509_distinguished_name_parse */ /* Parse Distinguished Name */ /* */ /* CALLED BY */ /* */ /* _nx_secure_x509_parse_cert_data Parse certificate */ /* */ /* 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_x509_parse_issuer(const UCHAR *buffer, ULONG length, UINT *bytes_processed, NX_SECURE_X509_CERT *cert) { USHORT tlv_type; USHORT tlv_type_class; ULONG tlv_length; UINT bytes; const UCHAR *tlv_data; ULONG header_length; UINT status; /* First, parse the sequence. */ status = _nx_secure_x509_asn1_tlv_block_parse(buffer, &length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length); /* Make sure we parsed the block alright. */ if (status != 0) { return(status); }if (status != 0) { ... } if (tlv_type != NX_SECURE_ASN_TAG_SEQUENCE || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { return(NX_SECURE_X509_UNEXPECTED_ASN1_TAG); }if (tlv_type != NX_SECURE_ASN_TAG_SEQUENCE || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { ... } /* Now, pass the issuer certificate to populate it with the id info. Note that * even if the issuer cert is NULL, we still need to parse the information - * the parse_id function handles NULL appropriately. *//* ... */ status = _nx_secure_x509_distinguished_name_parse(tlv_data, tlv_length, &bytes, &cert -> nx_secure_x509_issuer); /* Return the number of bytes we processed. */ *bytes_processed = bytes + header_length; return status; }{ ... } .../**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_x509_parse_validity PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function parses the validity period fields in an X.509 */ /* certificate. The fields "Not Before" and "Not After" define an */ /* explicit time period during which the certificate is valid. The the */ /* current date and time fall outside that period, the certificate is */ /* considered invalid and should not be trusted. */ /* */ /* INPUT */ /* */ /* buffer Pointer data to be parsed */ /* length Return bytes processed */ /* bytes_processed Number of bytes being */ /* consumed by the oid */ /* cert The certificate */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* _nx_secure_x509_asn1_tlv_block_parse Parse ASN.1 block */ /* */ /* CALLED BY */ /* */ /* _nx_secure_x509_parse_cert_data Parse certificate */ /* */ /* 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_x509_parse_validity(const UCHAR *buffer, ULONG length, UINT *bytes_processed, NX_SECURE_X509_CERT *cert) { USHORT tlv_type; USHORT tlv_type_class; ULONG tlv_length; const UCHAR *tlv_data; ULONG header_length; UINT status; const UCHAR *current_buffer; NX_CRYPTO_PARAMETER_NOT_USED(cert); /* First, parse the sequence. */ status = _nx_secure_x509_asn1_tlv_block_parse(buffer, &length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length); /* Make sure we parsed the block alright. */ if (status != 0) { return(status); }if (status != 0) { ... } if (tlv_type != NX_SECURE_ASN_TAG_SEQUENCE || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { return(NX_SECURE_X509_UNEXPECTED_ASN1_TAG); }if (tlv_type != NX_SECURE_ASN_TAG_SEQUENCE || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { ... } /* Return the number of bytes we processed. */ *bytes_processed = header_length + tlv_length; length = tlv_length; /* Advance to our next item. */ current_buffer = &buffer[0] + header_length; /* Parse the "not before" field. */ status = _nx_secure_x509_asn1_tlv_block_parse(current_buffer, &length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length); /* Make sure we parsed the block alright. */ if (status != 0) { return(status); }if (status != 0) { ... } if ((tlv_type != NX_SECURE_ASN_TAG_UTC_TIME && tlv_type != NX_SECURE_ASN_TAG_GENERALIZED_TIME) || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { return(NX_SECURE_X509_UNEXPECTED_ASN1_TAG); }if ((tlv_type != NX_SECURE_ASN_TAG_UTC_TIME && tlv_type != NX_SECURE_ASN_TAG_GENERALIZED_TIME) || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { ... } cert -> nx_secure_x509_validity_format = tlv_type; cert -> nx_secure_x509_not_before = tlv_data; cert -> nx_secure_x509_not_before_length = (USHORT)tlv_length; /* Advance to our next item. */ current_buffer = current_buffer + (tlv_length + header_length); /* Parse the "not after" field. */ status = _nx_secure_x509_asn1_tlv_block_parse(current_buffer, &length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length); /* Make sure we parsed the block alright. */ if (status != 0) { return(status); }if (status != 0) { ... } if ((tlv_type != NX_SECURE_ASN_TAG_UTC_TIME && tlv_type != NX_SECURE_ASN_TAG_GENERALIZED_TIME) || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { return(NX_SECURE_X509_UNEXPECTED_ASN1_TAG); }if ((tlv_type != NX_SECURE_ASN_TAG_UTC_TIME && tlv_type != NX_SECURE_ASN_TAG_GENERALIZED_TIME) || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { ... } cert -> nx_secure_x509_not_after = tlv_data; cert -> nx_secure_x509_not_after_length = (USHORT)tlv_length; return(NX_SECURE_X509_SUCCESS); }{ ... } ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_x509_parse_subject PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function parses the subject field of an X.509 certificate. This */ /* field consists of an X.509 Distinguished Name that provides the */ /* identity information for the certificate. */ /* */ /* INPUT */ /* */ /* buffer Pointer data to be parsed */ /* length Return bytes processed */ /* bytes_processed Number of bytes being */ /* consumed by the oid */ /* cert The certificate */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* _nx_secure_x509_asn1_tlv_block_parse Parse ASN.1 block */ /* _nx_secure_x509_distinguished_name_parse */ /* Parse Distinguished Name */ /* */ /* CALLED BY */ /* */ /* _nx_secure_x509_parse_cert_data Parse certificate */ /* */ /* 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_x509_parse_subject(const UCHAR *buffer, ULONG length, UINT *bytes_processed, NX_SECURE_X509_CERT *cert) { USHORT tlv_type; USHORT tlv_type_class; ULONG tlv_length; UINT bytes; const UCHAR *tlv_data; ULONG header_length; UINT status; /* First, parse the sequence. */ status = _nx_secure_x509_asn1_tlv_block_parse(buffer, &length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length); /* Make sure we parsed the block alright. */ if (status != 0) { return(status); }if (status != 0) { ... } if (tlv_type != NX_SECURE_ASN_TAG_SEQUENCE || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { return(NX_SECURE_X509_UNEXPECTED_ASN1_TAG); }if (tlv_type != NX_SECURE_ASN_TAG_SEQUENCE || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { ... } /* Now, pass the issuer certificate to populate it with the id info. Note that * even if the issuer cert is NULL, we still need to parse the information - * the parse_id function handles NULL appropriately. *//* ... */ status = _nx_secure_x509_distinguished_name_parse(tlv_data, tlv_length, &bytes, &cert -> nx_secure_x509_distinguished_name); /* Return the number of bytes we processed. */ *bytes_processed = bytes + header_length; return status; }{ ... } ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_x509_parse_public_key PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function parses the public key field of an X.509 certificate. */ /* The field consists of an OID indicating the algorithm (e.g. RSA) and */ /* an ASN.1 bit string containing a DER-encoded public key, the format */ /* of which varies with the algorithm. */ /* */ /* INPUT */ /* */ /* buffer Pointer data to be parsed */ /* length Return bytes processed */ /* bytes_processed Number of bytes being */ /* consumed by the oid */ /* cert The certificate */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* _nx_secure_x509_asn1_tlv_block_parse Parse ASN.1 block */ /* _nx_secure_x509_extract_oid_data Extract OID data */ /* _nx_secure_x509_oid_parse Parse OID in certificate */ /* */ /* CALLED BY */ /* */ /* _nx_secure_x509_parse_cert_data Parse certificate */ /* */ /* 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_x509_parse_public_key(const UCHAR *buffer, ULONG length, UINT *bytes_processed, NX_SECURE_X509_CERT *cert) { USHORT tlv_type; USHORT tlv_type_class; ULONG tlv_length; UINT bytes = 0; const UCHAR *tlv_data; const UCHAR *bitstring_ptr; ULONG bitstring_len; ULONG header_length; UINT oid; UINT oid_parameter; UINT status; /* Extract public key information, encoded as ASN.1 in an ASN.1 bitstring, and populate the certificate. */ /* Sequence: Public Key * Sequence: Public key data * ASN.1 OID (e.g. RSA) * NULL or parameters * ASN.1 Bit string - contains embedded key data in ASN.1 format: * { * 1 byte: padding (always 0?) * ASN.1 Key information - sequence of 2 integers for RSA: * Public Modulus followed by Public Exponent * } *//* ... */ /* The public key is a sequence of OIDs that is terminated by a NULL ASN.1 tag. */ *bytes_processed = 0; /* First, parse the outermost sequence. */ status = _nx_secure_x509_asn1_tlv_block_parse(buffer, &length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length); /* Make sure we parsed the block alright. */ if (status != 0) { return(status); }if (status != 0) { ... } if (tlv_type != NX_SECURE_ASN_TAG_SEQUENCE || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { return(NX_SECURE_X509_UNEXPECTED_ASN1_TAG); }if (tlv_type != NX_SECURE_ASN_TAG_SEQUENCE || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { ... } /* Save off a pointer into our sequence - we will need it to jump to the public key data, * which follows the public key id in the outermost sequence. *//* ... */ bitstring_ptr = tlv_data; *bytes_processed += header_length; length = tlv_length; /* Next, parse the OID sequence. */ status = _nx_secure_x509_asn1_tlv_block_parse(tlv_data, &length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length); /* Make sure we parsed the block alright. */ if (status != 0) { return(status); }if (status != 0) { ... } if (tlv_type != NX_SECURE_ASN_TAG_SEQUENCE || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { return(NX_SECURE_X509_UNEXPECTED_ASN1_TAG); }if (tlv_type != NX_SECURE_ASN_TAG_SEQUENCE || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { ... } /* Update byte count. */ *bytes_processed += header_length; /* Advance sequence pointer to the next item in the outermost sequence (following the id sequence) */ bitstring_ptr = &bitstring_ptr[(header_length + tlv_length)]; /* The length we use to parse the bitstring should be equal to the length of the entire buffer * minus the length of the OID sequence. *//* ... */ bitstring_len = length; length = tlv_length; /* Next in the buffer should be an OID to id the public key algorithm. */ status = _nx_secure_x509_asn1_tlv_block_parse(tlv_data, &length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length); if (status != 0) { return status; }if (status != 0) { ... } /* Update bytes processed. */ *bytes_processed += (header_length + tlv_length); /* Now check out what we got. Should be an OID... */ if (tlv_type == NX_SECURE_ASN_TAG_OID) { /* The OID is in the data we extracted. */ _nx_secure_x509_oid_parse(tlv_data, tlv_length, &oid); cert -> nx_secure_x509_public_algorithm = oid; /* The OID is followed by a NULL or a parameter OID. */ tlv_data = &tlv_data[tlv_length]; status = _nx_secure_x509_asn1_tlv_block_parse(tlv_data, &length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length); if (status != 0) { return status; }if (status != 0) { ... } oid_parameter = 0; if (tlv_type == NX_SECURE_ASN_TAG_OID) { /* The OID is in the data we extracted. */ _nx_secure_x509_oid_parse(tlv_data, tlv_length, &oid_parameter); }if (tlv_type == NX_SECURE_ASN_TAG_OID) { ... } else if (tlv_type != NX_SECURE_ASN_TAG_NULL || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { return(NX_SECURE_X509_UNEXPECTED_ASN1_TAG); }else if (tlv_type != NX_SECURE_ASN_TAG_NULL || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { ... } /* Update our bytes count. */ *bytes_processed += (header_length + tlv_length); /* Extract the data in the block following the OID sequence. Use the calculated remaining * length for the length of the data going in. *//* ... */ status = _nx_secure_x509_extract_oid_data(bitstring_ptr, oid, oid_parameter, bitstring_len, &bytes, cert); if (status != 0) { return status; }if (status != 0) { ... } /* Update the processed byte count with the extracted OID data. */ *bytes_processed += (bytes); }if (tlv_type == NX_SECURE_ASN_TAG_OID) { ... } else { return(NX_SECURE_X509_UNEXPECTED_ASN1_TAG); }else { ... } /* All good! */ return(NX_SECURE_X509_SUCCESS); }{ ... } ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_x509_parse_unique_ids PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function parses the optional isserUniqueID and subjectUniqueID */ /* bit strings in an X.509 certificate. */ /* */ /* INPUT */ /* */ /* buffer Pointer data to be parsed */ /* length Return bytes processed */ /* bytes_processed Number of bytes being */ /* consumed by the oid */ /* cert The certificate */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* _nx_secure_x509_asn1_tlv_block_parse Parse ASN.1 block */ /* */ /* CALLED BY */ /* */ /* _nx_secure_x509_parse_cert_data Parse certificate */ /* */ /* 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_x509_parse_unique_ids(const UCHAR *buffer, ULONG length, UINT *bytes_processed, NX_SECURE_X509_CERT *cert) { USHORT tlv_type; USHORT tlv_type_class; ULONG tlv_length; const UCHAR *tlv_data; ULONG header_length; UINT status; const UCHAR *current_buffer; UINT processed_id; NX_CRYPTO_PARAMETER_NOT_USED(cert); /* Extract unique identifiers for issuer and subject, if present. */ /* issuerUniqueID ::= ASN.1 Bit String OPTIONAL * subjectUniqueID ::= ASN.1 Bit String OPTIONAL *//* ... */ /* First, parse the next item which may be an ID or the item following the IDs in the X.509 spec if they were omitted. */ status = _nx_secure_x509_asn1_tlv_block_parse(buffer, &length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length); /* Make sure we parsed the block alright. */ if (status != 0) { return(status); }if (status != 0) { ... } *bytes_processed = 0; current_buffer = buffer; /* If we process either ID, then we need to do a version check (v2 or v3 only). If neither is encountered skip the version check. *//* ... */ processed_id = NX_CRYPTO_FALSE; /* Check for the OPTIONAL issuer unique ID. */ if (tlv_type_class == NX_SECURE_ASN_TAG_CLASS_CONTEXT && tlv_type == NX_SECURE_X509_TAG_ISSUER_UNIQUE_ID) { /* We processed an ID, mark for version check. */ processed_id = NX_CRYPTO_TRUE; /* The field is an IMPLICIT bit string, so the data just follows the context-specific tag. */ /* Save off a pointer to the issuer unique identifier data and its length. */ cert -> nx_secure_x509_issuer_identifier = &tlv_data[1]; cert -> nx_secure_x509_issuer_identifier_length = (USHORT)(tlv_length - 1); /* Return the number of bytes we processed. */ *bytes_processed += (header_length + tlv_length); /* Advance to our next item. */ current_buffer = &current_buffer[header_length + tlv_length]; /* Padding data? */ if (length > 0) { /* Parse the next item which might be another ID or perhaps the extensions. */ status = _nx_secure_x509_asn1_tlv_block_parse(current_buffer, &length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length); /* Make sure we parsed the block alright. */ if (status != 0) { return(status); }if (status != 0) { ... } }if (length > 0) { ... } }if (tlv_type_class == NX_SECURE_ASN_TAG_CLASS_CONTEXT && tlv_type == NX_SECURE_X509_TAG_ISSUER_UNIQUE_ID) { ... } /* Check for the OPTIONAL subject unique id field. */ if (tlv_type_class == NX_SECURE_ASN_TAG_CLASS_CONTEXT && tlv_type == NX_SECURE_X509_TAG_SUBJECT_UNIQUE_ID) { /* We processed an ID, mark for version check. */ processed_id = NX_CRYPTO_TRUE; /* The field is an IMPLICIT bit string, so the data just follows the context-specific tag. */ /* Save off a pointer to the issuer unique identifier data and its length. */ cert -> nx_secure_x509_subject_identifier = &tlv_data[1]; cert -> nx_secure_x509_subject_identifier_length = (USHORT)(tlv_length - 1); /* Return the number of bytes we processed. */ *bytes_processed += (header_length + tlv_length); }if (tlv_type_class == NX_SECURE_ASN_TAG_CLASS_CONTEXT && tlv_type == NX_SECURE_X509_TAG_SUBJECT_UNIQUE_ID) { ... } /* If we processed either field, make sure this is version 2 or 3. */ if(processed_id) { if (cert -> nx_secure_x509_version != NX_SECURE_X509_VERSION_2 && cert -> nx_secure_x509_version != NX_SECURE_X509_VERSION_3) { return(NX_SECURE_X509_INVALID_VERSION); }if (cert -> nx_secure_x509_version != NX_SECURE_X509_VERSION_2 && cert -> nx_secure_x509_version != NX_SECURE_X509_VERSION_3) { ... } }if (processed_id) { ... } /* If neither ID field was parsed, bytes_processed should be 0. */ return(NX_SECURE_X509_SUCCESS); }{ ... } ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_x509_parse_extensions PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function parses the extensions sequence in an X.509 */ /* certificate. The extensions themselves are not parsed here, but a */ /* pointer to the beginning of the DER-encoded sequence is saved in */ /* the certificate structure so that the extension-specific helper */ /* functions can easily find and parse the extensions later. */ /* */ /* INPUT */ /* */ /* buffer Pointer data to be parsed */ /* length Return bytes processed */ /* bytes_processed Number of bytes being */ /* consumed by the oid */ /* cert The certificate */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* _nx_secure_x509_asn1_tlv_block_parse Parse ASN.1 block */ /* */ /* CALLED BY */ /* */ /* _nx_secure_x509_parse_cert_data Parse certificate */ /* */ /* 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_x509_parse_extensions(const UCHAR *buffer, ULONG length, UINT *bytes_processed, NX_SECURE_X509_CERT *cert) { USHORT tlv_type; USHORT tlv_type_class; ULONG tlv_length; const UCHAR *tlv_data; const UCHAR *current_buffer; ULONG header_length; UINT status; /* Extract any extension information present in the certificate buffer and populate the certificate structure. * Extensions are encoded as ASN.1 in a sequence, so extract and parse that separately. *//* ... */ /* ASN.1 Sequence: Extensions * { * ASN.1-encoded data for extensions * } *//* ... */ /* First, parse the context-specific tag (if it exists). */ status = _nx_secure_x509_asn1_tlv_block_parse(buffer, &length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length); /* Make sure we parsed the block alright. */ if (status != 0) { return(status); }if (status != 0) { ... } /* If the next item up is not a sequence, then it isn't an extensions block. */ if (!(tlv_type_class == NX_SECURE_ASN_TAG_CLASS_CONTEXT && tlv_type == NX_SECURE_X509_TAG_EXTENSIONS)) { /* No extensions block is OK because it is non-existent in v1 and v2, and OPTIONAL in v3. *//* ... */ return(NX_SECURE_X509_SUCCESS); }if (!(tlv_type_class == NX_SECURE_ASN_TAG_CLASS_CONTEXT && tlv_type == NX_SECURE_X509_TAG_EXTENSIONS)) { ... } /* We have an extensions sequence, this MUST be a version 3 certificate, or we have an error. *//* ... */ if (cert -> nx_secure_x509_version != NX_SECURE_X509_VERSION_3) { return(NX_SECURE_X509_INVALID_VERSION); }if (cert -> nx_secure_x509_version != NX_SECURE_X509_VERSION_3) { ... } *bytes_processed = header_length + tlv_length; current_buffer = tlv_data; length = tlv_length; /* Next, parse the extensions sequence. */ status = _nx_secure_x509_asn1_tlv_block_parse(current_buffer, &length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length); /* Make sure we parsed the block alright. */ if (status != 0) { return(status); }if (status != 0) { ... } /* If the next item up is not a sequence, then it isn't an extensions block. */ if (!(tlv_type_class == NX_SECURE_ASN_TAG_CLASS_UNIVERSAL && tlv_type == NX_SECURE_ASN_TAG_SEQUENCE)) { /* No extensions block is OK because it is non-existent in v1 and v2, and OPTIONAL in v3. *//* ... */ return(NX_SECURE_X509_SUCCESS); }if (!(tlv_type_class == NX_SECURE_ASN_TAG_CLASS_UNIVERSAL && tlv_type == NX_SECURE_ASN_TAG_SEQUENCE)) { ... } /* Save off the start of the extensions sequence so we can parse it later when needed. */ cert -> nx_secure_x509_extensions_data = tlv_data; cert -> nx_secure_x509_extensions_data_length = tlv_length; /* We have saved off the extensions data successfully. */ return(NX_SECURE_X509_SUCCESS); }{ ... } ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_x509_parse_signature_data PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function parses the certificate signature field in an X.509 */ /* certificate. The signature data consists of a hash of the */ /* certificate data that is encrypted using the issuer's private key. */ /* Decrypting the hash with the issuer's public key and checking the */ /* result against a locally-computed hash cryptographically ties a */ /* certificate to its issuer. */ /* */ /* INPUT */ /* */ /* buffer Pointer data to be parsed */ /* length Return bytes processed */ /* bytes_processed Number of bytes being */ /* consumed by the oid */ /* cert The certificate */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* _nx_secure_x509_asn1_tlv_block_parse Parse ASN.1 block */ /* */ /* CALLED BY */ /* */ /* _nx_secure_x509_certificate_parse Extract public key data */ /* */ /* 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_x509_parse_signature_data(const UCHAR *buffer, ULONG length, UINT *bytes_processed, NX_SECURE_X509_CERT *cert) { USHORT tlv_type; USHORT tlv_type_class; ULONG tlv_length; const UCHAR *tlv_data; ULONG header_length; UINT status; /* Extract the signature data, which is a hash of the certificate data which is then encrypted using the issuer's * private key. This function extracts the encrypted hash but decryption/authentication is done elsewhere. *//* ... */ /* ASN.1 Bit string: Signature data */ /* First, parse the sequence. */ status = _nx_secure_x509_asn1_tlv_block_parse(buffer, &length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length); /* Make sure we parsed the block alright. */ if (status != 0) { return(status); }if (status != 0) { ... } if (tlv_type != NX_SECURE_ASN_TAG_BIT_STRING || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { return(NX_SECURE_X509_UNEXPECTED_ASN1_TAG); }if (tlv_type != NX_SECURE_ASN_TAG_BIT_STRING || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL) { ... } /* Save off a pointer to the signature data and its length. */ /* Signature has a 0 byte at the front we need to skip. * This is due to the data being encoded as an ASN.1 bit string, which may * require padding bits to get to a multiple of 8 for byte alignment. The byte * represents the number of padding bits, but in X509 it should always be 0. *//* ... */ cert -> nx_secure_x509_signature_data = &tlv_data[1]; cert -> nx_secure_x509_signature_data_length = tlv_length - 1; /* Return the number of bytes we processed. */ *bytes_processed = header_length + tlv_length; return(NX_SECURE_X509_SUCCESS); }{ ... }