1
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
31
32
33
34
35
36
39
40
41
42
43
44
45
46
47
52
53
54
55
62
63
64
67
71
72
75
76
77
79
80
81
82
83
84
85
86
87
88
89
90
91
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
119
120
121
122
123
124
125
126
127
128
131
132
133
134
135
136
137
141
142
143
144
147
148
151
152
153
154
155
159
160
161
162
165
166
169
170
171
172
173
177
178
179
180
181
182
183
191
192
193
194
195
196
197
198
201
202
205
206
207
208
209
210
211
212
213
216
217
218
219
220
221
222
223
224
225
228
229
234
235
236
237
238
239
240
241
244
245
246
247
248
249
250
253
254
260
261
264
265
269
270
271
272
273
279
280
281
286
287
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
314
315
316
321
322
323
326
327
328
331
332
333
334
335
336
337
338
339
340
343
344
345
346
347
348
352
353
354
355
356
357
358
359
360
363
364
365
366
367
368
369
370
380
381
382
383
386
387
391
392
393
394
395
400
401
402
403
404
405
406
407
408
412
413
414
415
418
419
422
423
427
428
429
430
431
434
435
436
437
438
439
440
443
444
445
446
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
475
476
477
478
482
483
486
487
500
501
504
505
508
509
513
514
516
520
521
522
523
524
525
526
527
528
529
530
531
532
533
536
537
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
558
559
563
564
565
566
569
570
571
572
573
578
579
580
581
582
583
584
587
588
589
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
637
638
642
643
644
645
646
647
648
649
650
651
654
655
656
657
658
663
664
667
668
672
685
686
687
688
691
692
693
694
698
699
700
701
702
703
704
705
706
707
708
709
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
781
782
786
787
790
794
795
796
797
798
799
802
806
807
808
809
813
814
815
816
817
818
821
829
830
831
832
835
836
837
838
842
843
844
845
846
847
848
849
850
851
852
853
857
868
869
873
874
875
876
877
878
882
883
884
885
886
890
891
892
893
894
895
896
897
898
899
900
903
904
907
908
909
910
916
917
918
919
920
924
925
926
927
928
932
933
934
935
936
937
938
942
943
944
948
949
950
951
952
956
957
960
961
962
963
964
972
973
974
975
976
977
983
984
985
986
987
990
991
992
993
994
1002
1010
1018
1026
1034
1035
1036
1037
1038
1040
1045
1046
1047
1050
1051
1057
1060
1061
1062
1063
1075
1076
1077
1078
1082
1083
1084
1085
1086
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1108
1111
1112
1113
1114
1115
1116
1117
1123
1128
1129
1130
1131
1132
1133
1134
1137
1138
1139
1140
1141
1142
1143
1147
1148
1151
1152
1153
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1181
1182
1183
1184
1190
1191
1194
1195
1196
1201
1202
1206
1207
1208
1209
1215
1220
1221
1224
1225
1226
1231
1232
1236
1237
1238
1239
1242
1243
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1264
1265
1272
1273
1274
1275
1279
1286
1287
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1321
1322
1327
1328
1329
1330
1331
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1348
1351
1352
1356
1357
1360
1361
1362
1363
1366
1367
1368
1369
1370
1371
1372
1373
1374
1377
1378
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1394
1395
1396
1397
1398
1399
1400
1404
1405
1406
1407
1411
1412
1416
1417
1418
1419
1420
1421
1423
1424
1425
1428
1431
1432
1436
1437
1438
1439
1440
1441
1442
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1474
1475
1476
1479
1480
1481
1482
1485
1486
1487
1488
1491
1492
1493
1494
1495
1496
1497
1500
1503
1504
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1577
1578
1579
1582
1583
1584
1585
1586
1587
1590
1591
1592
1593
1594
1595
1599
1600
1601
1606
1607
1608
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1625
1626
1627
1632
1633
1634
1635
1636
1637
1638
1639
1657
1658
1661
1662
1663
1664
1665
1670
1671
1672
1673
1674
1675
1676
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
1716
1717
1721
1722
1723
1724
1725
1726
1729
1732
1733
1734
1735
1740
1741
1745
1746
1750
1751
1755
1756
1759
1760
1761
1762
1763
1764
1765
1769
1770
1771
1772
1773
1774
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1808
1809
1810
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1851
1852
1853
1854
1855
1859
1860
1861
1862
1863
1864
1865
1866
1869
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1896
1897
1898
1899
1900
1901
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1930
1932
1933
1934
1935
1936
1942
1943
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2131
2132
2133
2134
2135
2136
2139
2140
2141
2142
2143
2144
2145
2149
2150
2151
2161
2162
2170
2171
2179
2180
2189
2190
2199
2200
2201
2202
2203
2204
2205
2206
2210
2211
2212
2213
2214
2216
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2231
2233
2234
2236
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2253
2254
2255
2256
2257
2258
2259
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2279
2280
2281
2282
2285
2286
2287
2288
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2307
2308
2311
2312
2313
2314
2318
2319
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2344
2345
2346
2347
2348
2352
2353
2354
2355
2356
2357
2358
2359
2360
2363
2364
2365
2370
2371
2374
2375
2381
2382
2385
2388
2389
2392
2393
2394
2399
2400
2403
2404
2410
2411
2414
2417
2418
2421
2422
2425
2429
2430
2431
2432
2433
2434
2435
2436
2437
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2455
2456
2457
2458
2459
2462
2463
2467
2468
2472
2473
2474
2477
2478
2479
2485
2486
2487
2488
2489
2490
2491
2492
2493
2499
2500
2501
2502
2503
2504
2505
2506
2509
2510
2511
2512
2513
2514
2517
2518
2521
2522
2523
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2615
2616
2617
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2645
2646
2647
2656
2657
2658
2659
2660
2661
2662
2663
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2731
2732
2733
2734
2735
2738
2739
2740
2741
2742
2743
2744
2748
2749
2750
2751
2752
2758
2759
2760
2761
2762
2763
2764
2765
2768
2769
2770
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2877
2878
2881
2882
2883
2886
2887
2888
2891
2892
2895
2896
2897
2901
2902
2903
2904
2905
2906
2908
2909
2910
2911
2912
2913
2914
2915
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2951
2952
2955
2959
2960
2962
2967
2968
2969
2972
2973
2974
2977
2978
2979
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2996
2997
2998
2999
3000
3004
3005
3006
3009
3010
3011
3012
3013
3016
3017
3018
3019
3020
3021
3022
3023
3026
3027
3028
3029
3030
3031
3032
3033
3036
3037
3038
3039
3040
3041
3042
3043
3044
3066
3067
3068
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3142
3143
3144
3147
3148
3149
3150
3151
3154
3155
3158
3159
3160
3161
3162
3163
3164
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3180
3181
3182
3183
3187
3188
3191
3194
3195
3199
3200
3203
3204
3205
3206
3207
3208
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3281
3285
3286
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3316
3317
3318
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3394
3395
3396
3399
3400
3401
3402
3403
3404
3405
/* ... */
/* ... */
#include "common.h"
#if defined(MBEDTLS_X509_CRT_PARSE_C)
#include "mbedtls/x509_crt.h"
#include "mbedtls/error.h"
#include "mbedtls/oid.h"
#include "mbedtls/platform_util.h"
#include <string.h>
5 includes
#if defined(MBEDTLS_PEM_PARSE_C)
#include "mbedtls/pem.h"
#endif
#if defined(MBEDTLS_USE_PSA_CRYPTO)
#include "psa/crypto.h"
#include "mbedtls/psa_util.h"
/* ... */#endif
#include "mbedtls/platform.h"
#if defined(MBEDTLS_THREADING_C)
#include "mbedtls/threading.h"
#endif
#if defined(MBEDTLS_HAVE_TIME)
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
#include <windows.h>
#else
#include <time.h>
#endif/* ... */
#endif
#if defined(MBEDTLS_FS_IO)
#include <stdio.h>
#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
/* ... */#endif /* ... */
#endif
/* ... */
typedef struct {
mbedtls_x509_crt *crt;
uint32_t flags;
...} x509_crt_verify_chain_item;
/* ... */
#define X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2)
/* ... */
const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
{
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224) |
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
0xFFFFFFF,
0xFFFFFFF,
2048,
...};
/* ... */
const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
{
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
0xFFFFFFF,
#if defined(MBEDTLS_ECP_C)
MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256K1),/* ... */
#else
0,
#endif
2048,
...};
/* ... */
const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
{
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384),
MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECDSA) |
MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECKEY),
#if defined(MBEDTLS_ECP_C)
MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1),/* ... */
#else
0,
#endif
0,
...};
/* ... */
static int x509_profile_check_md_alg(const mbedtls_x509_crt_profile *profile,
mbedtls_md_type_t md_alg)
{
if (md_alg == MBEDTLS_MD_NONE) {
return -1;
}if (md_alg == MBEDTLS_MD_NONE) { ... }
if ((profile->allowed_mds & MBEDTLS_X509_ID_FLAG(md_alg)) != 0) {
return 0;
}if ((profile->allowed_mds & MBEDTLS_X509_ID_FLAG(md_alg)) != 0) { ... }
return -1;
}{ ... }
/* ... */
static int x509_profile_check_pk_alg(const mbedtls_x509_crt_profile *profile,
mbedtls_pk_type_t pk_alg)
{
if (pk_alg == MBEDTLS_PK_NONE) {
return -1;
}if (pk_alg == MBEDTLS_PK_NONE) { ... }
if ((profile->allowed_pks & MBEDTLS_X509_ID_FLAG(pk_alg)) != 0) {
return 0;
}if ((profile->allowed_pks & MBEDTLS_X509_ID_FLAG(pk_alg)) != 0) { ... }
return -1;
}{ ... }
/* ... */
static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
const mbedtls_pk_context *pk)
{
const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(pk);
#if defined(MBEDTLS_RSA_C)
if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) {
if (mbedtls_pk_get_bitlen(pk) >= profile->rsa_min_bitlen) {
return 0;
}if (mbedtls_pk_get_bitlen(pk) >= profile->rsa_min_bitlen) { ... }
return -1;
}if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) { ... }
/* ... */#endif
#if defined(MBEDTLS_ECP_C)
if (pk_alg == MBEDTLS_PK_ECDSA ||
pk_alg == MBEDTLS_PK_ECKEY ||
pk_alg == MBEDTLS_PK_ECKEY_DH) {
const mbedtls_ecp_group_id gid = mbedtls_pk_ec(*pk)->grp.id;
if (gid == MBEDTLS_ECP_DP_NONE) {
return -1;
}if (gid == MBEDTLS_ECP_DP_NONE) { ... }
if ((profile->allowed_curves & MBEDTLS_X509_ID_FLAG(gid)) != 0) {
return 0;
}if ((profile->allowed_curves & MBEDTLS_X509_ID_FLAG(gid)) != 0) { ... }
return -1;
}if (pk_alg == MBEDTLS_PK_ECDSA || pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) { ... }
/* ... */#endif
return -1;
}{ ... }
/* ... */
static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
{
size_t i;
unsigned char diff;
const unsigned char *n1 = s1, *n2 = s2;
for (i = 0; i < len; i++) {
diff = n1[i] ^ n2[i];
if (diff == 0) {
continue;
}if (diff == 0) { ... }
if (diff == 32 &&
((n1[i] >= 'a' && n1[i] <= 'z') ||
(n1[i] >= 'A' && n1[i] <= 'Z'))) {
continue;
}if (diff == 32 && ((n1[i] >= 'a' && n1[i] <= 'z') || (n1[i] >= 'A' && n1[i] <= 'Z'))) { ... }
return -1;
}for (i = 0; i < len; i++) { ... }
return 0;
}{ ... }
/* ... */
static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
{
size_t i;
size_t cn_idx = 0, cn_len = strlen(cn);
if (name->len < 3 || name->p[0] != '*' || name->p[1] != '.') {
return -1;
}if (name->len < 3 || name->p[0] != '*' || name->p[1] != '.') { ... }
for (i = 0; i < cn_len; ++i) {
if (cn[i] == '.') {
cn_idx = i;
break;
}if (cn[i] == '.') { ... }
}for (i = 0; i < cn_len; ++i) { ... }
if (cn_idx == 0) {
return -1;
}if (cn_idx == 0) { ... }
if (cn_len - cn_idx == name->len - 1 &&
x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) {
return 0;
}if (cn_len - cn_idx == name->len - 1 && x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) { ... }
return -1;
}{ ... }
/* ... */
static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b)
{
if (a->tag == b->tag &&
a->len == b->len &&
memcmp(a->p, b->p, b->len) == 0) {
return 0;
}if (a->tag == b->tag && a->len == b->len && memcmp(a->p, b->p, b->len) == 0) { ... }
if ((a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
(b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
a->len == b->len &&
x509_memcasecmp(a->p, b->p, b->len) == 0) {
return 0;
}if ((a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING) && (b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) && a->len == b->len && x509_memcasecmp(a->p, b->p, b->len) == 0) { ... }
return -1;
}{ ... }
/* ... */
static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b)
{
while (a != NULL || b != NULL) {
if (a == NULL || b == NULL) {
return -1;
}if (a == NULL || b == NULL) { ... }
if (a->oid.tag != b->oid.tag ||
a->oid.len != b->oid.len ||
memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) {
return -1;
}if (a->oid.tag != b->oid.tag || a->oid.len != b->oid.len || memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) { ... }
if (x509_string_cmp(&a->val, &b->val) != 0) {
return -1;
}if (x509_string_cmp(&a->val, &b->val) != 0) { ... }
if (a->next_merged != b->next_merged) {
return -1;
}if (a->next_merged != b->next_merged) { ... }
a = a->next;
b = b->next;
}while (a != NULL || b != NULL) { ... }
return 0;
}{ ... }
/* ... */
static void x509_crt_verify_chain_reset(
mbedtls_x509_crt_verify_chain *ver_chain)
{
size_t i;
for (i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++) {
ver_chain->items[i].crt = NULL;
ver_chain->items[i].flags = (uint32_t) -1;
}for (i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++) { ... }
ver_chain->len = 0;
#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
ver_chain->trust_ca_cb_result = NULL;
#endif
}{ ... }
/* ... */
static int x509_get_version(unsigned char **p,
const unsigned char *end,
int *ver)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t len;
if ((ret = mbedtls_asn1_get_tag(p, end, &len,
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
0)) != 0) {
if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
*ver = 0;
return 0;
}if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { ... }
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
}if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0)) != 0) { ... }
end = *p + len;
if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
}if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) { ... }
if (*p != end) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION,
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
}if (*p != end) { ... }
return 0;
}{ ... }
/* ... */
static int x509_get_dates(unsigned char **p,
const unsigned char *end,
mbedtls_x509_time *from,
mbedtls_x509_time *to)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t len;
if ((ret = mbedtls_asn1_get_tag(p, end, &len,
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
}if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { ... }
end = *p + len;
if ((ret = mbedtls_x509_get_time(p, end, from)) != 0) {
return ret;
}if ((ret = mbedtls_x509_get_time(p, end, from)) != 0) { ... }
if ((ret = mbedtls_x509_get_time(p, end, to)) != 0) {
return ret;
}if ((ret = mbedtls_x509_get_time(p, end, to)) != 0) { ... }
if (*p != end) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
}if (*p != end) { ... }
return 0;
}{ ... }
/* ... */
static int x509_get_uid(unsigned char **p,
const unsigned char *end,
mbedtls_x509_buf *uid, int n)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
if (*p == end) {
return 0;
}if (*p == end) { ... }
uid->tag = **p;
if ((ret = mbedtls_asn1_get_tag(p, end, &uid->len,
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
n)) != 0) {
if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
return 0;
}if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { ... }
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
}if ((ret = mbedtls_asn1_get_tag(p, end, &uid->len, MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n)) != 0) { ... }
uid->p = *p;
*p += uid->len;
return 0;
}{ ... }
static int x509_get_basic_constraints(unsigned char **p,
const unsigned char *end,
int *ca_istrue,
int *max_pathlen)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t len;
/* ... */
*ca_istrue = 0;
*max_pathlen = 0;
if ((ret = mbedtls_asn1_get_tag(p, end, &len,
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
}if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { ... }
if (*p == end) {
return 0;
}if (*p == end) { ... }
if ((ret = mbedtls_asn1_get_bool(p, end, ca_istrue)) != 0) {
if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
ret = mbedtls_asn1_get_int(p, end, ca_istrue);
}if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { ... }
if (ret != 0) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
}if (ret != 0) { ... }
if (*ca_istrue != 0) {
*ca_istrue = 1;
}if (*ca_istrue != 0) { ... }
}if ((ret = mbedtls_asn1_get_bool(p, end, ca_istrue)) != 0) { ... }
if (*p == end) {
return 0;
}if (*p == end) { ... }
if ((ret = mbedtls_asn1_get_int(p, end, max_pathlen)) != 0) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
}if ((ret = mbedtls_asn1_get_int(p, end, max_pathlen)) != 0) { ... }
if (*p != end) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
}if (*p != end) { ... }
/* ... */
if (*max_pathlen == INT_MAX) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
MBEDTLS_ERR_ASN1_INVALID_LENGTH);
}if (*max_pathlen == INT_MAX) { ... }
(*max_pathlen)++;
return 0;
}{ ... }
static int x509_get_ns_cert_type(unsigned char **p,
const unsigned char *end,
unsigned char *ns_cert_type)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
mbedtls_x509_bitstring bs = { 0, 0, NULL };
if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
}if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) { ... }
if (bs.len != 1) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
MBEDTLS_ERR_ASN1_INVALID_LENGTH);
}if (bs.len != 1) { ... }
*ns_cert_type = *bs.p;
return 0;
}{ ... }
static int x509_get_key_usage(unsigned char **p,
const unsigned char *end,
unsigned int *key_usage)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t i;
mbedtls_x509_bitstring bs = { 0, 0, NULL };
if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
}if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) { ... }
if (bs.len < 1) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
MBEDTLS_ERR_ASN1_INVALID_LENGTH);
}if (bs.len < 1) { ... }
*key_usage = 0;
for (i = 0; i < bs.len && i < sizeof(unsigned int); i++) {
*key_usage |= (unsigned int) bs.p[i] << (8*i);
}for (i = 0; i < bs.len && i < sizeof(unsigned int); i++) { ... }
return 0;
}{ ... }
/* ... */
static int x509_get_ext_key_usage(unsigned char **p,
const unsigned char *end,
mbedtls_x509_sequence *ext_key_usage)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage, MBEDTLS_ASN1_OID)) != 0) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
}if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage, MBEDTLS_ASN1_OID)) != 0) { ... }
if (ext_key_usage->buf.p == NULL) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
MBEDTLS_ERR_ASN1_INVALID_LENGTH);
}if (ext_key_usage->buf.p == NULL) { ... }
return 0;
}{ ... }
/* ... */
static int x509_get_subject_alt_name(unsigned char **p,
const unsigned char *end,
mbedtls_x509_sequence *subject_alt_name)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t len, tag_len;
mbedtls_asn1_sequence *cur = subject_alt_name;
if ((ret = mbedtls_asn1_get_tag(p, end, &len,
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
}if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { ... }
if (*p + len != end) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
}if (*p + len != end) { ... }
while (*p < end) {
mbedtls_x509_subject_alternative_name dummy_san_buf;
mbedtls_x509_buf tmp_san_buf;
memset(&dummy_san_buf, 0, sizeof(dummy_san_buf));
tmp_san_buf.tag = **p;
(*p)++;
if ((ret = mbedtls_asn1_get_len(p, end, &tag_len)) != 0) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
}if ((ret = mbedtls_asn1_get_len(p, end, &tag_len)) != 0) { ... }
tmp_san_buf.p = *p;
tmp_san_buf.len = tag_len;
if ((tmp_san_buf.tag & MBEDTLS_ASN1_TAG_CLASS_MASK) !=
MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
}if ((tmp_san_buf.tag & MBEDTLS_ASN1_TAG_CLASS_MASK) != MBEDTLS_ASN1_CONTEXT_SPECIFIC) { ... }
/* ... */
ret = mbedtls_x509_parse_subject_alt_name(&tmp_san_buf, &dummy_san_buf);
/* ... */
if (ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
mbedtls_x509_sequence *seq_cur = subject_alt_name->next;
mbedtls_x509_sequence *seq_prv;
while (seq_cur != NULL) {
seq_prv = seq_cur;
seq_cur = seq_cur->next;
mbedtls_platform_zeroize(seq_prv,
sizeof(mbedtls_x509_sequence));
mbedtls_free(seq_prv);
}while (seq_cur != NULL) { ... }
subject_alt_name->next = NULL;
return ret;
}if (ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) { ... }
if (cur->buf.p != NULL) {
if (cur->next != NULL) {
return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
}if (cur->next != NULL) { ... }
cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
if (cur->next == NULL) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
MBEDTLS_ERR_ASN1_ALLOC_FAILED);
}if (cur->next == NULL) { ... }
cur = cur->next;
}if (cur->buf.p != NULL) { ... }
cur->buf = tmp_san_buf;
*p += tmp_san_buf.len;
}while (*p < end) { ... }
cur->next = NULL;
if (*p != end) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
}if (*p != end) { ... }
return 0;
}{ ... }
/* ... */
static int x509_get_certificate_policies(unsigned char **p,
const unsigned char *end,
mbedtls_x509_sequence *certificate_policies)
{
int ret, parse_ret = 0;
size_t len;
mbedtls_asn1_buf *buf;
mbedtls_asn1_sequence *cur = certificate_policies;
ret = mbedtls_asn1_get_tag(p, end, &len,
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
if (ret != 0) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
}if (ret != 0) { ... }
if (*p + len != end) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
}if (*p + len != end) { ... }
/* ... */
if (len == 0) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
}if (len == 0) { ... }
while (*p < end) {
mbedtls_x509_buf policy_oid;
const unsigned char *policy_end;
/* ... */
if ((ret = mbedtls_asn1_get_tag(p, end, &len,
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
}if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { ... }
policy_end = *p + len;
if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
MBEDTLS_ASN1_OID)) != 0) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
}if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len, MBEDTLS_ASN1_OID)) != 0) { ... }
policy_oid.tag = MBEDTLS_ASN1_OID;
policy_oid.len = len;
policy_oid.p = *p;
/* ... */
if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) {
/* ... */
parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
}if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) { ... }
if (cur->buf.p != NULL) {
if (cur->next != NULL) {
return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
}if (cur->next != NULL) { ... }
cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
if (cur->next == NULL) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
MBEDTLS_ERR_ASN1_ALLOC_FAILED);
}if (cur->next == NULL) { ... }
cur = cur->next;
}if (cur->buf.p != NULL) { ... }
buf = &(cur->buf);
buf->tag = policy_oid.tag;
buf->p = policy_oid.p;
buf->len = policy_oid.len;
*p += len;
/* ... */
if (*p < policy_end) {
if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
0) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
}if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { ... }
/* ... */
*p += len;
}if (*p < policy_end) { ... }
if (*p != policy_end) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
}if (*p != policy_end) { ... }
}while (*p < end) { ... }
cur->next = NULL;
if (*p != end) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
}if (*p != end) { ... }
return parse_ret;
}{ ... }
/* ... */
static int x509_get_crt_ext(unsigned char **p,
const unsigned char *end,
mbedtls_x509_crt *crt,
mbedtls_x509_crt_ext_cb_t cb,
void *p_ctx)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t len;
unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
if (*p == end) {
return 0;
}if (*p == end) { ... }
if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0) {
return ret;
}if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0) { ... }
end = crt->v3_ext.p + crt->v3_ext.len;
while (*p < end) {
/* ... */
mbedtls_x509_buf extn_oid = { 0, 0, NULL };
int is_critical = 0;
int ext_type = 0;
if ((ret = mbedtls_asn1_get_tag(p, end, &len,
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
}if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { ... }
end_ext_data = *p + len;
if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
MBEDTLS_ASN1_OID)) != 0) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
}if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len, MBEDTLS_ASN1_OID)) != 0) { ... }
extn_oid.tag = MBEDTLS_ASN1_OID;
extn_oid.p = *p;
*p += extn_oid.len;
if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
(ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
}if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 && (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) { ... }
if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
MBEDTLS_ASN1_OCTET_STRING)) != 0) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
}if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) { ... }
start_ext_octet = *p;
end_ext_octet = *p + len;
if (end_ext_octet != end_ext_data) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
}if (end_ext_octet != end_ext_data) { ... }
/* ... */
ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
if (ret != 0) {
if (cb != NULL) {
ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet);
if (ret != 0 && is_critical) {
return ret;
}if (ret != 0 && is_critical) { ... }
*p = end_ext_octet;
continue;
}if (cb != NULL) { ... }
*p = end_ext_octet;
#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
if (is_critical) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
}if (is_critical) { ... }
/* ... */#endif
continue;
}if (ret != 0) { ... }
if ((crt->ext_types & ext_type) != 0) {
return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
}if ((crt->ext_types & ext_type) != 0) { ... }
crt->ext_types |= ext_type;
switch (ext_type) {
case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
if ((ret = x509_get_basic_constraints(p, end_ext_octet,
&crt->ca_istrue, &crt->max_pathlen)) != 0) {
return ret;
}if ((ret = x509_get_basic_constraints(p, end_ext_octet, &crt->ca_istrue, &crt->max_pathlen)) != 0) { ... }
break;
case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
case MBEDTLS_X509_EXT_KEY_USAGE:
if ((ret = x509_get_key_usage(p, end_ext_octet,
&crt->key_usage)) != 0) {
return ret;
}if ((ret = x509_get_key_usage(p, end_ext_octet, &crt->key_usage)) != 0) { ... }
break;
case MBEDTLS_X509_EXT_KEY_USAGE:
case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
&crt->ext_key_usage)) != 0) {
return ret;
}if ((ret = x509_get_ext_key_usage(p, end_ext_octet, &crt->ext_key_usage)) != 0) { ... }
break;
case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
if ((ret = x509_get_subject_alt_name(p, end_ext_octet,
&crt->subject_alt_names)) != 0) {
return ret;
}if ((ret = x509_get_subject_alt_name(p, end_ext_octet, &crt->subject_alt_names)) != 0) { ... }
break;
case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
case MBEDTLS_X509_EXT_NS_CERT_TYPE:
if ((ret = x509_get_ns_cert_type(p, end_ext_octet,
&crt->ns_cert_type)) != 0) {
return ret;
}if ((ret = x509_get_ns_cert_type(p, end_ext_octet, &crt->ns_cert_type)) != 0) { ... }
break;
case MBEDTLS_X509_EXT_NS_CERT_TYPE:
case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
if ((ret = x509_get_certificate_policies(p, end_ext_octet,
&crt->certificate_policies)) != 0) {
/* ... */
if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
cb(p_ctx, crt, &extn_oid, is_critical,
start_ext_octet, end_ext_octet) == 0) {
break;
}if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL && cb(p_ctx, crt, &extn_oid, is_critical, start_ext_octet, end_ext_octet) == 0) { ... }
#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
if (is_critical) {
return ret;
}if (is_critical) { ... } else/* ... */
#endif
/* ... */
if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
return ret;
}else if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) { ... }
}if ((ret = x509_get_certificate_policies(p, end_ext_octet, &crt->certificate_policies)) != 0) { ... }
break;
case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
default:
/* ... */
#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
if (is_critical) {
return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
}if (is_critical) { ... } else/* ... */
#endif
*p = end_ext_octet;default
}switch (ext_type) { ... }
}while (*p < end) { ... }
if (*p != end) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
}if (*p != end) { ... }
return 0;
}{ ... }
/* ... */
static int x509_crt_parse_der_core(mbedtls_x509_crt *crt,
const unsigned char *buf,
size_t buflen,
int make_copy,
mbedtls_x509_crt_ext_cb_t cb,
void *p_ctx)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t len;
unsigned char *p, *end, *crt_end;
mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
/* ... */
if (crt == NULL || buf == NULL) {
return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
}if (crt == NULL || buf == NULL) { ... }
p = (unsigned char *) buf;
len = buflen;
end = p + len;
/* ... */
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
mbedtls_x509_crt_free(crt);
return MBEDTLS_ERR_X509_INVALID_FORMAT;
}if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { ... }
end = crt_end = p + len;
crt->raw.len = crt_end - buf;
if (make_copy != 0) {
crt->raw.p = p = mbedtls_calloc(1, crt->raw.len);
if (crt->raw.p == NULL) {
return MBEDTLS_ERR_X509_ALLOC_FAILED;
}if (crt->raw.p == NULL) { ... }
memcpy(crt->raw.p, buf, crt->raw.len);
crt->own_buffer = 1;
p += crt->raw.len - len;
end = crt_end = p + len;
}if (make_copy != 0) { ... } else {
crt->raw.p = (unsigned char *) buf;
crt->own_buffer = 0;
}else { ... }
/* ... */
crt->tbs.p = p;
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
mbedtls_x509_crt_free(crt);
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
}if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { ... }
end = p + len;
crt->tbs.len = end - crt->tbs.p;
/* ... */
if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
(ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
(ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
&sig_params1)) != 0) {
mbedtls_x509_crt_free(crt);
return ret;
}if ((ret = x509_get_version(&p, end, &crt->version)) != 0 || (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 || (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid, &sig_params1)) != 0) { ... }
if (crt->version < 0 || crt->version > 2) {
mbedtls_x509_crt_free(crt);
return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
}if (crt->version < 0 || crt->version > 2) { ... }
crt->version++;
if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1,
&crt->sig_md, &crt->sig_pk,
&crt->sig_opts)) != 0) {
mbedtls_x509_crt_free(crt);
return ret;
}if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1, &crt->sig_md, &crt->sig_pk, &crt->sig_opts)) != 0) { ... }
/* ... */
crt->issuer_raw.p = p;
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
mbedtls_x509_crt_free(crt);
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
}if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { ... }
if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
mbedtls_x509_crt_free(crt);
return ret;
}if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) { ... }
crt->issuer_raw.len = p - crt->issuer_raw.p;
/* ... */
if ((ret = x509_get_dates(&p, end, &crt->valid_from,
&crt->valid_to)) != 0) {
mbedtls_x509_crt_free(crt);
return ret;
}if ((ret = x509_get_dates(&p, end, &crt->valid_from, &crt->valid_to)) != 0) { ... }
/* ... */
crt->subject_raw.p = p;
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
mbedtls_x509_crt_free(crt);
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
}if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { ... }
if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
mbedtls_x509_crt_free(crt);
return ret;
}if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) { ... }
crt->subject_raw.len = p - crt->subject_raw.p;
/* ... */
crt->pk_raw.p = p;
if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
mbedtls_x509_crt_free(crt);
return ret;
}if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) { ... }
crt->pk_raw.len = p - crt->pk_raw.p;
/* ... */
if (crt->version == 2 || crt->version == 3) {
ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
if (ret != 0) {
mbedtls_x509_crt_free(crt);
return ret;
}if (ret != 0) { ... }
}if (crt->version == 2 || crt->version == 3) { ... }
if (crt->version == 2 || crt->version == 3) {
ret = x509_get_uid(&p, end, &crt->subject_id, 2);
if (ret != 0) {
mbedtls_x509_crt_free(crt);
return ret;
}if (ret != 0) { ... }
}if (crt->version == 2 || crt->version == 3) { ... }
int extensions_allowed = 1;
#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
if (crt->version != 3) {
extensions_allowed = 0;
}if (crt->version != 3) { ... }
/* ... */#endif
if (extensions_allowed) {
ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
if (ret != 0) {
mbedtls_x509_crt_free(crt);
return ret;
}if (ret != 0) { ... }
}if (extensions_allowed) { ... }
if (p != end) {
mbedtls_x509_crt_free(crt);
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
}if (p != end) { ... }
end = crt_end;
/* ... */
if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
mbedtls_x509_crt_free(crt);
return ret;
}if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) { ... }
if (crt->sig_oid.len != sig_oid2.len ||
memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
sig_params1.tag != sig_params2.tag ||
sig_params1.len != sig_params2.len ||
(sig_params1.len != 0 &&
memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
mbedtls_x509_crt_free(crt);
return MBEDTLS_ERR_X509_SIG_MISMATCH;
}if (crt->sig_oid.len != sig_oid2.len || memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 || sig_params1.tag != sig_params2.tag || sig_params1.len != sig_params2.len || (sig_params1.len != 0 && memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) { ... }
if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) {
mbedtls_x509_crt_free(crt);
return ret;
}if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) { ... }
if (p != end) {
mbedtls_x509_crt_free(crt);
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
}if (p != end) { ... }
return 0;
}{ ... }
/* ... */
static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
const unsigned char *buf,
size_t buflen,
int make_copy,
mbedtls_x509_crt_ext_cb_t cb,
void *p_ctx)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
mbedtls_x509_crt *crt = chain, *prev = NULL;
/* ... */
if (crt == NULL || buf == NULL) {
return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
}if (crt == NULL || buf == NULL) { ... }
while (crt->version != 0 && crt->next != NULL) {
prev = crt;
crt = crt->next;
}while (crt->version != 0 && crt->next != NULL) { ... }
/* ... */
if (crt->version != 0 && crt->next == NULL) {
crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
if (crt->next == NULL) {
return MBEDTLS_ERR_X509_ALLOC_FAILED;
}if (crt->next == NULL) { ... }
prev = crt;
mbedtls_x509_crt_init(crt->next);
crt = crt->next;
}if (crt->version != 0 && crt->next == NULL) { ... }
ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
if (ret != 0) {
if (prev) {
prev->next = NULL;
}if (prev) { ... }
if (crt != chain) {
mbedtls_free(crt);
}if (crt != chain) { ... }
return ret;
}if (ret != 0) { ... }
return 0;
}{ ... }
int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
const unsigned char *buf,
size_t buflen)
{
return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL);
}{ ... }
int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
const unsigned char *buf,
size_t buflen,
int make_copy,
mbedtls_x509_crt_ext_cb_t cb,
void *p_ctx)
{
return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx);
}{ ... }
int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
const unsigned char *buf,
size_t buflen)
{
return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL);
}{ ... }
/* ... */
int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
const unsigned char *buf,
size_t buflen)
{
#if defined(MBEDTLS_PEM_PARSE_C)
int success = 0, first_error = 0, total_failed = 0;
int buf_format = MBEDTLS_X509_FORMAT_DER;/* ... */
#endif
/* ... */
if (chain == NULL || buf == NULL) {
return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
}if (chain == NULL || buf == NULL) { ... }
/* ... */
#if defined(MBEDTLS_PEM_PARSE_C)
if (buflen != 0 && buf[buflen - 1] == '\0' &&
strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
buf_format = MBEDTLS_X509_FORMAT_PEM;
}if (buflen != 0 && buf[buflen - 1] == '\0' && strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) { ... }
if (buf_format == MBEDTLS_X509_FORMAT_DER) {
return mbedtls_x509_crt_parse_der(chain, buf, buflen);
}if (buf_format == MBEDTLS_X509_FORMAT_DER) { ... }
/* ... */#else
return mbedtls_x509_crt_parse_der(chain, buf, buflen);
#endif
#if defined(MBEDTLS_PEM_PARSE_C)
if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
mbedtls_pem_context pem;
while (buflen > 1) {
size_t use_len;
mbedtls_pem_init(&pem);
ret = mbedtls_pem_read_buffer(&pem,
"-----BEGIN CERTIFICATE-----",
"-----END CERTIFICATE-----",
buf, NULL, 0, &use_len);
if (ret == 0) {
/* ... */
buflen -= use_len;
buf += use_len;
}if (ret == 0) { ... } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
return ret;
}else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) { ... } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
mbedtls_pem_free(&pem);
/* ... */
buflen -= use_len;
buf += use_len;
if (first_error == 0) {
first_error = ret;
}if (first_error == 0) { ... }
total_failed++;
continue;
}else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) { ... } else {
break;
}else { ... }
ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
mbedtls_pem_free(&pem);
if (ret != 0) {
/* ... */
if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) {
return ret;
}if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) { ... }
if (first_error == 0) {
first_error = ret;
}if (first_error == 0) { ... }
total_failed++;
continue;
}if (ret != 0) { ... }
success = 1;
}while (buflen > 1) { ... }
}if (buf_format == MBEDTLS_X509_FORMAT_PEM) { ... }
if (success) {
return total_failed;
}if (success) { ... } else if (first_error) {
return first_error;
}else if (first_error) { ... } else {
return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT;
}else { ... }
/* ... */#endif
}{ ... }
#if defined(MBEDTLS_FS_IO)
/* ... */
int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t n;
unsigned char *buf;
if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
return ret;
}if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) { ... }
ret = mbedtls_x509_crt_parse(chain, buf, n);
mbedtls_platform_zeroize(buf, n);
mbedtls_free(buf);
return ret;
}mbedtls_x509_crt_parse_file (mbedtls_x509_crt *chain, const char *path) { ... }
int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
{
int ret = 0;
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
int w_ret;
WCHAR szDir[MAX_PATH];
char filename[MAX_PATH];
char *p;
size_t len = strlen(path);
WIN32_FIND_DATAW file_data;
HANDLE hFind;
if (len > MAX_PATH - 3) {
return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
}if (len > MAX_PATH - 3) { ... }
memset(szDir, 0, sizeof(szDir));
memset(filename, 0, MAX_PATH);
memcpy(filename, path, len);
filename[len++] = '\\';
p = filename + len;
filename[len++] = '*';
w_ret = MultiByteToWideChar(CP_ACP, 0, filename, (int) len, szDir,
MAX_PATH - 3);
if (w_ret == 0) {
return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
}if (w_ret == 0) { ... }
hFind = FindFirstFileW(szDir, &file_data);
if (hFind == INVALID_HANDLE_VALUE) {
return MBEDTLS_ERR_X509_FILE_IO_ERROR;
}if (hFind == INVALID_HANDLE_VALUE) { ... }
len = MAX_PATH - len;
do {
memset(p, 0, len);
if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
continue;
}if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { ... }
w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
-1,
p, (int) len,
NULL, NULL);
if (w_ret == 0) {
ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
goto cleanup;
}if (w_ret == 0) { ... }
w_ret = mbedtls_x509_crt_parse_file(chain, filename);
if (w_ret < 0) {
ret++;
}if (w_ret < 0) { ... } else {
ret += w_ret;
}else { ... }
...} while (FindNextFileW(hFind, &file_data) != 0);
if (GetLastError() != ERROR_NO_MORE_FILES) {
ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
}if (GetLastError() != ERROR_NO_MORE_FILES) { ... }
cleanup:
FindClose(hFind);/* ... */
#else
int t_ret;
int snp_ret;
struct stat sb;
struct dirent *entry;
char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
DIR *dir = opendir(path);
if (dir == NULL) {
return MBEDTLS_ERR_X509_FILE_IO_ERROR;
}if (dir == NULL) { ... }
#if defined(MBEDTLS_THREADING_C)
if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
closedir(dir);
return ret;
}if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) { ... }
/* ... */#endif
memset(&sb, 0, sizeof(sb));
while ((entry = readdir(dir)) != NULL) {
snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
"%s/%s", path, entry->d_name);
if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
goto cleanup;
}if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) { ... } else if (stat(entry_name, &sb) == -1) {
if (errno == ENOENT) {
/* ... */
continue;
}if (errno == ENOENT) { ... } else {
ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
goto cleanup;
}else { ... }
}else if (stat(entry_name, &sb) == -1) { ... }
if (!S_ISREG(sb.st_mode)) {
continue;
}if (!S_ISREG(sb.st_mode)) { ... }
t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
if (t_ret < 0) {
ret++;
}if (t_ret < 0) { ... } else {
ret += t_ret;
}else { ... }
}while ((entry = readdir(dir)) != NULL) { ... }
cleanup:
closedir(dir);
#if defined(MBEDTLS_THREADING_C)
if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
}if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) { ... }
/* ... */#endif
/* ... */
#endif
return ret;
}mbedtls_x509_crt_parse_path (mbedtls_x509_crt *chain, const char *path) { ... }
/* ... */#endif
/* ... */
static int x509_get_other_name(const mbedtls_x509_buf *subject_alt_name,
mbedtls_x509_san_other_name *other_name)
{
int ret = 0;
size_t len;
unsigned char *p = subject_alt_name->p;
const unsigned char *end = p + subject_alt_name->len;
mbedtls_x509_buf cur_oid;
if ((subject_alt_name->tag &
(MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK)) !=
(MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME)) {
/* ... */
return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
}if ((subject_alt_name->tag & (MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK)) != (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME)) { ... }
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
MBEDTLS_ASN1_OID)) != 0) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
}if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0) { ... }
cur_oid.tag = MBEDTLS_ASN1_OID;
cur_oid.p = p;
cur_oid.len = len;
/* ... */
if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid) != 0) {
return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
}if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid) != 0) { ... }
other_name->type_id = cur_oid;
p += len;
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
0) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
}if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) != 0) { ... }
if (end != p + len) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
}if (end != p + len) { ... }
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
}if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { ... }
if (end != p + len) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
}if (end != p + len) { ... }
if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
}if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0) { ... }
other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
other_name->value.hardware_module_name.oid.p = p;
other_name->value.hardware_module_name.oid.len = len;
p += len;
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
MBEDTLS_ASN1_OCTET_STRING)) != 0) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
}if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) { ... }
other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
other_name->value.hardware_module_name.val.p = p;
other_name->value.hardware_module_name.val.len = len;
p += len;
if (p != end) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
}if (p != end) { ... }
return 0;
}{ ... }
static int x509_info_subject_alt_name(char **buf, size_t *size,
const mbedtls_x509_sequence
*subject_alt_name,
const char *prefix)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t i;
size_t n = *size;
char *p = *buf;
const mbedtls_x509_sequence *cur = subject_alt_name;
mbedtls_x509_subject_alternative_name san;
int parse_ret;
while (cur != NULL) {
memset(&san, 0, sizeof(san));
parse_ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
if (parse_ret != 0) {
if (parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
MBEDTLS_X509_SAFE_SNPRINTF;
}if (parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) { ... } else {
ret = mbedtls_snprintf(p, n, "\n%s <malformed>", prefix);
MBEDTLS_X509_SAFE_SNPRINTF;
}else { ... }
cur = cur->next;
continue;
}if (parse_ret != 0) { ... }
switch (san.type) {
/* ... */
case MBEDTLS_X509_SAN_OTHER_NAME:
{
mbedtls_x509_san_other_name *other_name = &san.san.other_name;
ret = mbedtls_snprintf(p, n, "\n%s otherName :", prefix);
MBEDTLS_X509_SAFE_SNPRINTF;
if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
&other_name->type_id) == 0) {
ret = mbedtls_snprintf(p, n, "\n%s hardware module name :", prefix);
MBEDTLS_X509_SAFE_SNPRINTF;
ret =
mbedtls_snprintf(p, n, "\n%s hardware type : ", prefix);
MBEDTLS_X509_SAFE_SNPRINTF;
ret = mbedtls_oid_get_numeric_string(p,
n,
&other_name->value.hardware_module_name.oid);
MBEDTLS_X509_SAFE_SNPRINTF;
ret =
mbedtls_snprintf(p, n, "\n%s hardware serial number : ", prefix);
MBEDTLS_X509_SAFE_SNPRINTF;
for (i = 0; i < other_name->value.hardware_module_name.val.len; i++) {
ret = mbedtls_snprintf(p,
n,
"%02X",
other_name->value.hardware_module_name.val.p[i]);
MBEDTLS_X509_SAFE_SNPRINTF;
}for (i = 0; i < other_name->value.hardware_module_name.val.len; i++) { ... }
}if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, &other_name->type_id) == 0) { ... }
...}
break;
/* ... */
case MBEDTLS_X509_SAN_OTHER_NAME: case MBEDTLS_X509_SAN_DNS_NAME:
{
ret = mbedtls_snprintf(p, n, "\n%s dNSName : ", prefix);
MBEDTLS_X509_SAFE_SNPRINTF;
if (san.san.unstructured_name.len >= n) {
*p = '\0';
return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
}if (san.san.unstructured_name.len >= n) { ... }
memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
p += san.san.unstructured_name.len;
n -= san.san.unstructured_name.len;
...}
break;
/* ... */
case MBEDTLS_X509_SAN_DNS_NAME: default:
ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
MBEDTLS_X509_SAFE_SNPRINTF;
break;default
}switch (san.type) { ... }
cur = cur->next;
}while (cur != NULL) { ... }
*p = '\0';
*size = n;
*buf = p;
return 0;
}{ ... }
int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
mbedtls_x509_subject_alternative_name *san)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
switch (san_buf->tag &
(MBEDTLS_ASN1_TAG_CLASS_MASK |
MBEDTLS_ASN1_TAG_VALUE_MASK)) {
/* ... */
case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME):
{
mbedtls_x509_san_other_name other_name;
ret = x509_get_other_name(san_buf, &other_name);
if (ret != 0) {
return ret;
}if (ret != 0) { ... }
memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
san->type = MBEDTLS_X509_SAN_OTHER_NAME;
memcpy(&san->san.other_name,
&other_name, sizeof(other_name));
...}
break;
/* ... */
case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME): case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME):
{
memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
san->type = MBEDTLS_X509_SAN_DNS_NAME;
memcpy(&san->san.unstructured_name,
san_buf, sizeof(*san_buf));
...}
break;
/* ... */
case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME): default:
return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;default
}switch (san_buf->tag & (MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK)) { ... }
return 0;
}{ ... }
#define PRINT_ITEM(i) \
do { \
ret = mbedtls_snprintf(p, n, "%s" i, sep); \
MBEDTLS_X509_SAFE_SNPRINTF; \
sep = ", "; \
...} while (0)...
#define CERT_TYPE(type, name) \
do { \
if (ns_cert_type & (type)) { \
PRINT_ITEM(name); \
}if (ns_cert_type & (type)) { ... } \
...} while (0)...
static int x509_info_cert_type(char **buf, size_t *size,
unsigned char ns_cert_type)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t n = *size;
char *p = *buf;
const char *sep = "";
CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client");
CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server");
CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email");
CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing");
CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved");
CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA");
CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA");
CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA");
*size = n;
*buf = p;
return 0;
}{ ... }
#define KEY_USAGE(code, name) \
do { \
if (key_usage & (code)) { \
PRINT_ITEM(name); \
}if (key_usage & (code)) { ... } \
...} while (0)...
static int x509_info_key_usage(char **buf, size_t *size,
unsigned int key_usage)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t n = *size;
char *p = *buf;
const char *sep = "";
KEY_USAGE(MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature");
KEY_USAGE(MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation");
KEY_USAGE(MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment");
KEY_USAGE(MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment");
KEY_USAGE(MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement");
KEY_USAGE(MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign");
KEY_USAGE(MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign");
KEY_USAGE(MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only");
KEY_USAGE(MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only");
*size = n;
*buf = p;
return 0;
}{ ... }
static int x509_info_ext_key_usage(char **buf, size_t *size,
const mbedtls_x509_sequence *extended_key_usage)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
const char *desc;
size_t n = *size;
char *p = *buf;
const mbedtls_x509_sequence *cur = extended_key_usage;
const char *sep = "";
while (cur != NULL) {
if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
desc = "???";
}if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) { ... }
ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
MBEDTLS_X509_SAFE_SNPRINTF;
sep = ", ";
cur = cur->next;
}while (cur != NULL) { ... }
*size = n;
*buf = p;
return 0;
}{ ... }
static int x509_info_cert_policies(char **buf, size_t *size,
const mbedtls_x509_sequence *certificate_policies)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
const char *desc;
size_t n = *size;
char *p = *buf;
const mbedtls_x509_sequence *cur = certificate_policies;
const char *sep = "";
while (cur != NULL) {
if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
desc = "???";
}if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) { ... }
ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
MBEDTLS_X509_SAFE_SNPRINTF;
sep = ", ";
cur = cur->next;
}while (cur != NULL) { ... }
*size = n;
*buf = p;
return 0;
}{ ... }
/* ... */
#define BEFORE_COLON 18
#define BC "18"
int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
const mbedtls_x509_crt *crt)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t n;
char *p;
char key_size_str[BEFORE_COLON];
p = buf;
n = size;
if (NULL == crt) {
ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
MBEDTLS_X509_SAFE_SNPRINTF;
return (int) (size - n);
}if (NULL == crt) { ... }
ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
prefix, crt->version);
MBEDTLS_X509_SAFE_SNPRINTF;
ret = mbedtls_snprintf(p, n, "%sserial number : ",
prefix);
MBEDTLS_X509_SAFE_SNPRINTF;
ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
MBEDTLS_X509_SAFE_SNPRINTF;
ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
MBEDTLS_X509_SAFE_SNPRINTF;
ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
MBEDTLS_X509_SAFE_SNPRINTF;
ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
MBEDTLS_X509_SAFE_SNPRINTF;
ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
MBEDTLS_X509_SAFE_SNPRINTF;
ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
"%04d-%02d-%02d %02d:%02d:%02d", prefix,
crt->valid_from.year, crt->valid_from.mon,
crt->valid_from.day, crt->valid_from.hour,
crt->valid_from.min, crt->valid_from.sec);
MBEDTLS_X509_SAFE_SNPRINTF;
ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
"%04d-%02d-%02d %02d:%02d:%02d", prefix,
crt->valid_to.year, crt->valid_to.mon,
crt->valid_to.day, crt->valid_to.hour,
crt->valid_to.min, crt->valid_to.sec);
MBEDTLS_X509_SAFE_SNPRINTF;
ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
MBEDTLS_X509_SAFE_SNPRINTF;
ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
crt->sig_md, crt->sig_opts);
MBEDTLS_X509_SAFE_SNPRINTF;
if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
mbedtls_pk_get_name(&crt->pk))) != 0) {
return ret;
}if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON, mbedtls_pk_get_name(&crt->pk))) != 0) { ... }
ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
(int) mbedtls_pk_get_bitlen(&crt->pk));
MBEDTLS_X509_SAFE_SNPRINTF;
/* ... */
if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
crt->ca_istrue ? "true" : "false");
MBEDTLS_X509_SAFE_SNPRINTF;
if (crt->max_pathlen > 0) {
ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
MBEDTLS_X509_SAFE_SNPRINTF;
}if (crt->max_pathlen > 0) { ... }
}if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) { ... }
if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
MBEDTLS_X509_SAFE_SNPRINTF;
if ((ret = x509_info_subject_alt_name(&p, &n,
&crt->subject_alt_names,
prefix)) != 0) {
return ret;
}if ((ret = x509_info_subject_alt_name(&p, &n, &crt->subject_alt_names, prefix)) != 0) { ... }
}if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) { ... }
if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
MBEDTLS_X509_SAFE_SNPRINTF;
if ((ret = x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
return ret;
}if ((ret = x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) { ... }
}if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) { ... }
if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
MBEDTLS_X509_SAFE_SNPRINTF;
if ((ret = x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
return ret;
}if ((ret = x509_info_key_usage(&p, &n, crt->key_usage)) != 0) { ... }
}if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) { ... }
if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
MBEDTLS_X509_SAFE_SNPRINTF;
if ((ret = x509_info_ext_key_usage(&p, &n,
&crt->ext_key_usage)) != 0) {
return ret;
}if ((ret = x509_info_ext_key_usage(&p, &n, &crt->ext_key_usage)) != 0) { ... }
}if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) { ... }
if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
MBEDTLS_X509_SAFE_SNPRINTF;
if ((ret = x509_info_cert_policies(&p, &n,
&crt->certificate_policies)) != 0) {
return ret;
}if ((ret = x509_info_cert_policies(&p, &n, &crt->certificate_policies)) != 0) { ... }
}if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) { ... }
ret = mbedtls_snprintf(p, n, "\n");
MBEDTLS_X509_SAFE_SNPRINTF;
return (int) (size - n);
}{ ... }
struct x509_crt_verify_string {
int code;
const char *string;
...};
static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
{ MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
{ MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
{ MBEDTLS_X509_BADCERT_CN_MISMATCH,
"The certificate Common Name (CN) does not match with the expected CN" ...},
{ MBEDTLS_X509_BADCERT_NOT_TRUSTED,
"The certificate is not correctly signed by the trusted CA" ...},
{ MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
{ MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
{ MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
{ MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
{ MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
{ MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
{ MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
{ MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
{ MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
{ MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
{ MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
{ MBEDTLS_X509_BADCERT_BAD_PK,
"The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." ...},
{ MBEDTLS_X509_BADCERT_BAD_KEY,
"The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." ...},
{ MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
{ MBEDTLS_X509_BADCRL_BAD_PK,
"The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." ...},
{ MBEDTLS_X509_BADCRL_BAD_KEY,
"The CRL is signed with an unacceptable key (eg bad curve, RSA too short)." ...},
{ 0, NULL }
...};
int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
uint32_t flags)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
const struct x509_crt_verify_string *cur;
char *p = buf;
size_t n = size;
for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
if ((flags & cur->code) == 0) {
continue;
}if ((flags & cur->code) == 0) { ... }
ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
MBEDTLS_X509_SAFE_SNPRINTF;
flags ^= cur->code;
}for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) { ... }
if (flags != 0) {
ret = mbedtls_snprintf(p, n, "%sUnknown reason "
"(this should not happen)\n", prefix);
MBEDTLS_X509_SAFE_SNPRINTF;
}if (flags != 0) { ... }
return (int) (size - n);
}{ ... }
#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
unsigned int usage)
{
unsigned int usage_must, usage_may;
unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
| MBEDTLS_X509_KU_DECIPHER_ONLY;
if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
return 0;
}if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) { ... }
usage_must = usage & ~may_mask;
if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
}if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) { ... }
usage_may = usage & may_mask;
if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
}if (((crt->key_usage & may_mask) | usage_may) != usage_may) { ... }
return 0;
}mbedtls_x509_crt_check_key_usage (const mbedtls_x509_crt *crt, unsigned int usage) { ... }
/* ... */#endif
#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
const char *usage_oid,
size_t usage_len)
{
const mbedtls_x509_sequence *cur;
if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
return 0;
}if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) { ... }
/* ... */
for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
const mbedtls_x509_buf *cur_oid = &cur->buf;
if (cur_oid->len == usage_len &&
memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
return 0;
}if (cur_oid->len == usage_len && memcmp(cur_oid->p, usage_oid, usage_len) == 0) { ... }
if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
return 0;
}if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) { ... }
}for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) { ... }
return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
}mbedtls_x509_crt_check_extended_key_usage (const mbedtls_x509_crt *crt, const char *usage_oid, size_t usage_len) { ... }
/* ... */#endif
#if defined(MBEDTLS_X509_CRL_PARSE_C)
/* ... */
int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
{
const mbedtls_x509_crl_entry *cur = &crl->entry;
while (cur != NULL && cur->serial.len != 0) {
if (crt->serial.len == cur->serial.len &&
memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
return 1;
}if (crt->serial.len == cur->serial.len && memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) { ... }
cur = cur->next;
}while (cur != NULL && cur->serial.len != 0) { ... }
return 0;
}mbedtls_x509_crt_is_revoked (const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl) { ... }
/* ... */
static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
mbedtls_x509_crl *crl_list,
const mbedtls_x509_crt_profile *profile)
{
int flags = 0;
unsigned char hash[MBEDTLS_MD_MAX_SIZE];
const mbedtls_md_info_t *md_info;
if (ca == NULL) {
return flags;
}if (ca == NULL) { ... }
while (crl_list != NULL) {
if (crl_list->version == 0 ||
x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
crl_list = crl_list->next;
continue;
}if (crl_list->version == 0 || x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) { ... }
/* ... */
#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
if (mbedtls_x509_crt_check_key_usage(ca,
MBEDTLS_X509_KU_CRL_SIGN) != 0) {
flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
break;
}if (mbedtls_x509_crt_check_key_usage(ca, MBEDTLS_X509_KU_CRL_SIGN) != 0) { ... }
/* ... */#endif
/* ... */
if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
flags |= MBEDTLS_X509_BADCRL_BAD_MD;
}if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) { ... }
if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
flags |= MBEDTLS_X509_BADCRL_BAD_PK;
}if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) { ... }
md_info = mbedtls_md_info_from_type(crl_list->sig_md);
if (mbedtls_md(md_info, crl_list->tbs.p, crl_list->tbs.len, hash) != 0) {
flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
break;
}if (mbedtls_md(md_info, crl_list->tbs.p, crl_list->tbs.len, hash) != 0) { ... }
if (x509_profile_check_key(profile, &ca->pk) != 0) {
flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
}if (x509_profile_check_key(profile, &ca->pk) != 0) { ... }
if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
crl_list->sig_md, hash, mbedtls_md_get_size(md_info),
crl_list->sig.p, crl_list->sig.len) != 0) {
flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
break;
}if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk, crl_list->sig_md, hash, mbedtls_md_get_size(md_info), crl_list->sig.p, crl_list->sig.len) != 0) { ... }
/* ... */
if (mbedtls_x509_time_is_past(&crl_list->next_update)) {
flags |= MBEDTLS_X509_BADCRL_EXPIRED;
}if (mbedtls_x509_time_is_past(&crl_list->next_update)) { ... }
if (mbedtls_x509_time_is_future(&crl_list->this_update)) {
flags |= MBEDTLS_X509_BADCRL_FUTURE;
}if (mbedtls_x509_time_is_future(&crl_list->this_update)) { ... }
/* ... */
if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
flags |= MBEDTLS_X509_BADCERT_REVOKED;
break;
}if (mbedtls_x509_crt_is_revoked(crt, crl_list)) { ... }
crl_list = crl_list->next;
}while (crl_list != NULL) { ... }
return flags;
}x509_crt_verifycrl (mbedtls_x509_crt *crt, mbedtls_x509_crt *ca, mbedtls_x509_crl *crl_list, const mbedtls_x509_crt_profile *profile) { ... }
/* ... */#endif
/* ... */
static int x509_crt_check_signature(const mbedtls_x509_crt *child,
mbedtls_x509_crt *parent,
mbedtls_x509_crt_restart_ctx *rs_ctx)
{
unsigned char hash[MBEDTLS_MD_MAX_SIZE];
size_t hash_len;
#if !defined(MBEDTLS_USE_PSA_CRYPTO)
const mbedtls_md_info_t *md_info;
md_info = mbedtls_md_info_from_type(child->sig_md);
hash_len = mbedtls_md_get_size(md_info);
if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) {
return -1;
}if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) { ... }
/* ... */#else
psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
psa_algorithm_t hash_alg = mbedtls_psa_translate_md(child->sig_md);
if (psa_hash_setup(&hash_operation, hash_alg) != PSA_SUCCESS) {
return -1;
}if (psa_hash_setup(&hash_operation, hash_alg) != PSA_SUCCESS) { ... }
if (psa_hash_update(&hash_operation, child->tbs.p, child->tbs.len)
!= PSA_SUCCESS) {
return -1;
}if (psa_hash_update(&hash_operation, child->tbs.p, child->tbs.len) != PSA_SUCCESS) { ... }
if (psa_hash_finish(&hash_operation, hash, sizeof(hash), &hash_len)
!= PSA_SUCCESS) {
return -1;
}if (psa_hash_finish(&hash_operation, hash, sizeof(hash), &hash_len) != PSA_SUCCESS) { ... }
/* ... */#endif
if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
return -1;
}if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) { ... }
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
return mbedtls_pk_verify_restartable(&parent->pk,
child->sig_md, hash, hash_len,
child->sig.p, child->sig.len, &rs_ctx->pk);
}if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) { ... }
/* ... */#else
(void) rs_ctx;
#endif
return mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
child->sig_md, hash, hash_len,
child->sig.p, child->sig.len);
}{ ... }
/* ... */
static int x509_crt_check_parent(const mbedtls_x509_crt *child,
const mbedtls_x509_crt *parent,
int top)
{
int need_ca_bit;
if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
return -1;
}if (x509_name_cmp(&child->issuer, &parent->subject) != 0) { ... }
need_ca_bit = 1;
if (top && parent->version < 3) {
need_ca_bit = 0;
}if (top && parent->version < 3) { ... }
if (need_ca_bit && !parent->ca_istrue) {
return -1;
}if (need_ca_bit && !parent->ca_istrue) { ... }
#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
if (need_ca_bit &&
mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
return -1;
}if (need_ca_bit && mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) { ... }
/* ... */#endif
return 0;
}{ ... }
/* ... */
static int x509_crt_find_parent_in(
mbedtls_x509_crt *child,
mbedtls_x509_crt *candidates,
mbedtls_x509_crt **r_parent,
int *r_signature_is_good,
int top,
unsigned path_cnt,
unsigned self_cnt,
mbedtls_x509_crt_restart_ctx *rs_ctx)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
mbedtls_x509_crt *parent, *fallback_parent;
int signature_is_good = 0, fallback_signature_is_good;
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && rs_ctx->parent != NULL) {
parent = rs_ctx->parent;
fallback_parent = rs_ctx->fallback_parent;
fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
rs_ctx->parent = NULL;
rs_ctx->fallback_parent = NULL;
rs_ctx->fallback_signature_is_good = 0;
goto check_signature;
}if (rs_ctx != NULL && rs_ctx->parent != NULL) { ... }
/* ... */#endif
fallback_parent = NULL;
fallback_signature_is_good = 0;
for (parent = candidates; parent != NULL; parent = parent->next) {
if (x509_crt_check_parent(child, parent, top) != 0) {
continue;
}if (x509_crt_check_parent(child, parent, top) != 0) { ... }
if (parent->max_pathlen > 0 &&
(size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
continue;
}if (parent->max_pathlen > 0 && (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) { ... }
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
check_signature:
#endif
ret = x509_crt_check_signature(child, parent, rs_ctx);
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
rs_ctx->parent = parent;
rs_ctx->fallback_parent = fallback_parent;
rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
return ret;
}if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) { ... }
/* ... */#else
(void) ret;
#endif
signature_is_good = ret == 0;
if (top && !signature_is_good) {
continue;
}if (top && !signature_is_good) { ... }
if (mbedtls_x509_time_is_past(&parent->valid_to) ||
mbedtls_x509_time_is_future(&parent->valid_from)) {
if (fallback_parent == NULL) {
fallback_parent = parent;
fallback_signature_is_good = signature_is_good;
}if (fallback_parent == NULL) { ... }
continue;
}if (mbedtls_x509_time_is_past(&parent->valid_to) || mbedtls_x509_time_is_future(&parent->valid_from)) { ... }
*r_parent = parent;
*r_signature_is_good = signature_is_good;
break;
}for (parent = candidates; parent != NULL; parent = parent->next) { ... }
if (parent == NULL) {
*r_parent = fallback_parent;
*r_signature_is_good = fallback_signature_is_good;
}if (parent == NULL) { ... }
return 0;
}{ ... }
/* ... */
static int x509_crt_find_parent(
mbedtls_x509_crt *child,
mbedtls_x509_crt *trust_ca,
mbedtls_x509_crt **parent,
int *parent_is_trusted,
int *signature_is_good,
unsigned path_cnt,
unsigned self_cnt,
mbedtls_x509_crt_restart_ctx *rs_ctx)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
mbedtls_x509_crt *search_list;
*parent_is_trusted = 1;
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
*parent_is_trusted = rs_ctx->parent_is_trusted;
rs_ctx->parent_is_trusted = -1;
}if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) { ... }
/* ... */#endif
while (1) {
search_list = *parent_is_trusted ? trust_ca : child->next;
ret = x509_crt_find_parent_in(child, search_list,
parent, signature_is_good,
*parent_is_trusted,
path_cnt, self_cnt, rs_ctx);
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
rs_ctx->parent_is_trusted = *parent_is_trusted;
return ret;
}if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) { ... }
/* ... */#else
(void) ret;
#endif
if (*parent != NULL || *parent_is_trusted == 0) {
break;
}if (*parent != NULL || *parent_is_trusted == 0) { ... }
*parent_is_trusted = 0;
}while (1) { ... }
if (*parent == NULL) {
*parent_is_trusted = 0;
*signature_is_good = 0;
}if (*parent == NULL) { ... }
return 0;
}{ ... }
/* ... */
static int x509_crt_check_ee_locally_trusted(
mbedtls_x509_crt *crt,
mbedtls_x509_crt *trust_ca)
{
mbedtls_x509_crt *cur;
if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
return -1;
}if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) { ... }
for (cur = trust_ca; cur != NULL; cur = cur->next) {
if (crt->raw.len == cur->raw.len &&
memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
return 0;
}if (crt->raw.len == cur->raw.len && memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) { ... }
}for (cur = trust_ca; cur != NULL; cur = cur->next) { ... }
return -1;
}{ ... }
/* ... */
static int x509_crt_verify_chain(
mbedtls_x509_crt *crt,
mbedtls_x509_crt *trust_ca,
mbedtls_x509_crl *ca_crl,
mbedtls_x509_crt_ca_cb_t f_ca_cb,
void *p_ca_cb,
const mbedtls_x509_crt_profile *profile,
mbedtls_x509_crt_verify_chain *ver_chain,
mbedtls_x509_crt_restart_ctx *rs_ctx)
{
/* ... */
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
uint32_t *flags;
mbedtls_x509_crt_verify_chain_item *cur;
mbedtls_x509_crt *child;
mbedtls_x509_crt *parent;
int parent_is_trusted;
int child_is_trusted;
int signature_is_good;
unsigned self_cnt;
mbedtls_x509_crt *cur_trust_ca = NULL;
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
*ver_chain = rs_ctx->ver_chain;
self_cnt = rs_ctx->self_cnt;
cur = &ver_chain->items[ver_chain->len - 1];
child = cur->crt;
flags = &cur->flags;
goto find_parent;
}if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) { ... }
/* ... */#endif
child = crt;
self_cnt = 0;
parent_is_trusted = 0;
child_is_trusted = 0;
while (1) {
cur = &ver_chain->items[ver_chain->len];
cur->crt = child;
cur->flags = 0;
ver_chain->len++;
flags = &cur->flags;
if (mbedtls_x509_time_is_past(&child->valid_to)) {
*flags |= MBEDTLS_X509_BADCERT_EXPIRED;
}if (mbedtls_x509_time_is_past(&child->valid_to)) { ... }
if (mbedtls_x509_time_is_future(&child->valid_from)) {
*flags |= MBEDTLS_X509_BADCERT_FUTURE;
}if (mbedtls_x509_time_is_future(&child->valid_from)) { ... }
if (child_is_trusted) {
return 0;
}if (child_is_trusted) { ... }
if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
*flags |= MBEDTLS_X509_BADCERT_BAD_MD;
}if (x509_profile_check_md_alg(profile, child->sig_md) != 0) { ... }
if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
*flags |= MBEDTLS_X509_BADCERT_BAD_PK;
}if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) { ... }
if (ver_chain->len == 1 &&
x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
return 0;
}if (ver_chain->len == 1 && x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) { ... }
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
find_parent:
#endif
/* ... */
#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
if (f_ca_cb != NULL) {
mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
mbedtls_free(ver_chain->trust_ca_cb_result);
ver_chain->trust_ca_cb_result = NULL;
ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
if (ret != 0) {
return MBEDTLS_ERR_X509_FATAL_ERROR;
}if (ret != 0) { ... }
cur_trust_ca = ver_chain->trust_ca_cb_result;
}if (f_ca_cb != NULL) { ... } else
#endif
{
((void) f_ca_cb);
((void) p_ca_cb);
cur_trust_ca = trust_ca;
}else { ... }
ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
&parent_is_trusted, &signature_is_good,
ver_chain->len - 1, self_cnt, rs_ctx);
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
rs_ctx->in_progress = x509_crt_rs_find_parent;
rs_ctx->self_cnt = self_cnt;
rs_ctx->ver_chain = *ver_chain;
return ret;
}if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) { ... }
/* ... */#else
(void) ret;
#endif
if (parent == NULL) {
*flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
return 0;
}if (parent == NULL) { ... }
/* ... */
if (ver_chain->len != 1 &&
x509_name_cmp(&child->issuer, &child->subject) == 0) {
self_cnt++;
}if (ver_chain->len != 1 && x509_name_cmp(&child->issuer, &child->subject) == 0) { ... }
/* ... */
if (!parent_is_trusted &&
ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
return MBEDTLS_ERR_X509_FATAL_ERROR;
}if (!parent_is_trusted && ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) { ... }
if (!signature_is_good) {
*flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
}if (!signature_is_good) { ... }
if (x509_profile_check_key(profile, &parent->pk) != 0) {
*flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
}if (x509_profile_check_key(profile, &parent->pk) != 0) { ... }
#if defined(MBEDTLS_X509_CRL_PARSE_C)
*flags |= x509_crt_verifycrl(child, parent, ca_crl, profile);/* ... */
#else
(void) ca_crl;
#endif
child = parent;
parent = NULL;
child_is_trusted = parent_is_trusted;
signature_is_good = 0;
}while (1) { ... }
}{ ... }
/* ... */
static int x509_crt_check_cn(const mbedtls_x509_buf *name,
const char *cn, size_t cn_len)
{
if (name->len == cn_len &&
x509_memcasecmp(cn, name->p, cn_len) == 0) {
return 0;
}if (name->len == cn_len && x509_memcasecmp(cn, name->p, cn_len) == 0) { ... }
if (x509_check_wildcard(cn, name) == 0) {
return 0;
}if (x509_check_wildcard(cn, name) == 0) { ... }
return -1;
}{ ... }
/* ... */
static int x509_crt_check_san(const mbedtls_x509_buf *name,
const char *cn, size_t cn_len)
{
const unsigned char san_type = (unsigned char) name->tag &
MBEDTLS_ASN1_TAG_VALUE_MASK;
if (san_type == MBEDTLS_X509_SAN_DNS_NAME) {
return x509_crt_check_cn(name, cn, cn_len);
}if (san_type == MBEDTLS_X509_SAN_DNS_NAME) { ... }
return -1;
}{ ... }
/* ... */
static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
const char *cn,
uint32_t *flags)
{
const mbedtls_x509_name *name;
const mbedtls_x509_sequence *cur;
size_t cn_len = strlen(cn);
if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
for (cur = &crt->subject_alt_names; cur != NULL; cur = cur->next) {
if (x509_crt_check_san(&cur->buf, cn, cn_len) == 0) {
break;
}if (x509_crt_check_san(&cur->buf, cn, cn_len) == 0) { ... }
}for (cur = &crt->subject_alt_names; cur != NULL; cur = cur->next) { ... }
if (cur == NULL) {
*flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
}if (cur == NULL) { ... }
}if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) { ... } else {
for (name = &crt->subject; name != NULL; name = name->next) {
if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
break;
}if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 && x509_crt_check_cn(&name->val, cn, cn_len) == 0) { ... }
}for (name = &crt->subject; name != NULL; name = name->next) { ... }
if (name == NULL) {
*flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
}if (name == NULL) { ... }
}else { ... }
}{ ... }
/* ... */
static int x509_crt_merge_flags_with_cb(
uint32_t *flags,
const mbedtls_x509_crt_verify_chain *ver_chain,
int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
void *p_vrfy)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
unsigned i;
uint32_t cur_flags;
const mbedtls_x509_crt_verify_chain_item *cur;
for (i = ver_chain->len; i != 0; --i) {
cur = &ver_chain->items[i-1];
cur_flags = cur->flags;
if (NULL != f_vrfy) {
if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
return ret;
}if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) { ... }
}if (NULL != f_vrfy) { ... }
*flags |= cur_flags;
}for (i = ver_chain->len; i != 0; --i) { ... }
return 0;
}{ ... }
/* ... */
static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
mbedtls_x509_crt *trust_ca,
mbedtls_x509_crl *ca_crl,
mbedtls_x509_crt_ca_cb_t f_ca_cb,
void *p_ca_cb,
const mbedtls_x509_crt_profile *profile,
const char *cn, uint32_t *flags,
int (*f_vrfy)(void *,
mbedtls_x509_crt *,
int,
uint32_t *),
void *p_vrfy,
mbedtls_x509_crt_restart_ctx *rs_ctx)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
mbedtls_pk_type_t pk_type;
mbedtls_x509_crt_verify_chain ver_chain;
uint32_t ee_flags;
*flags = 0;
ee_flags = 0;
x509_crt_verify_chain_reset(&ver_chain);
if (profile == NULL) {
ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
goto exit;
}if (profile == NULL) { ... }
if (cn != NULL) {
x509_crt_verify_name(crt, cn, &ee_flags);
}if (cn != NULL) { ... }
pk_type = mbedtls_pk_get_type(&crt->pk);
if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
}if (x509_profile_check_pk_alg(profile, pk_type) != 0) { ... }
if (x509_profile_check_key(profile, &crt->pk) != 0) {
ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
}if (x509_profile_check_key(profile, &crt->pk) != 0) { ... }
ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
f_ca_cb, p_ca_cb, profile,
&ver_chain, rs_ctx);
if (ret != 0) {
goto exit;
}if (ret != 0) { ... }
ver_chain.items[0].flags |= ee_flags;
ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
exit:
#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
mbedtls_free(ver_chain.trust_ca_cb_result);
ver_chain.trust_ca_cb_result = NULL;/* ... */
#endif
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
mbedtls_x509_crt_restart_free(rs_ctx);
}if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) { ... }
/* ... */#endif
/* ... */
if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
ret = MBEDTLS_ERR_X509_FATAL_ERROR;
}if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) { ... }
if (ret != 0) {
*flags = (uint32_t) -1;
return ret;
}if (ret != 0) { ... }
if (*flags != 0) {
return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
}if (*flags != 0) { ... }
return 0;
}{ ... }
/* ... */
int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
mbedtls_x509_crt *trust_ca,
mbedtls_x509_crl *ca_crl,
const char *cn, uint32_t *flags,
int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
void *p_vrfy)
{
return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
NULL, NULL,
&mbedtls_x509_crt_profile_default,
cn, flags,
f_vrfy, p_vrfy, NULL);
}{ ... }
/* ... */
int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
mbedtls_x509_crt *trust_ca,
mbedtls_x509_crl *ca_crl,
const mbedtls_x509_crt_profile *profile,
const char *cn, uint32_t *flags,
int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
void *p_vrfy)
{
return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
NULL, NULL,
profile, cn, flags,
f_vrfy, p_vrfy, NULL);
}{ ... }
#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
/* ... */
int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
mbedtls_x509_crt_ca_cb_t f_ca_cb,
void *p_ca_cb,
const mbedtls_x509_crt_profile *profile,
const char *cn, uint32_t *flags,
int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
void *p_vrfy)
{
return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
f_ca_cb, p_ca_cb,
profile, cn, flags,
f_vrfy, p_vrfy, NULL);
}mbedtls_x509_crt_verify_with_ca_cb (mbedtls_x509_crt *crt, mbedtls_x509_crt_ca_cb_t f_ca_cb, void *p_ca_cb, const mbedtls_x509_crt_profile *profile, const char *cn, uint32_t *flags, int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), void *p_vrfy) { ... }
/* ... */#endif
int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
mbedtls_x509_crt *trust_ca,
mbedtls_x509_crl *ca_crl,
const mbedtls_x509_crt_profile *profile,
const char *cn, uint32_t *flags,
int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
void *p_vrfy,
mbedtls_x509_crt_restart_ctx *rs_ctx)
{
return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
NULL, NULL,
profile, cn, flags,
f_vrfy, p_vrfy, rs_ctx);
}{ ... }
/* ... */
void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
{
memset(crt, 0, sizeof(mbedtls_x509_crt));
}{ ... }
/* ... */
void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
{
mbedtls_x509_crt *cert_cur = crt;
mbedtls_x509_crt *cert_prv;
mbedtls_x509_name *name_cur;
mbedtls_x509_name *name_prv;
mbedtls_x509_sequence *seq_cur;
mbedtls_x509_sequence *seq_prv;
if (crt == NULL) {
return;
}if (crt == NULL) { ... }
do {
mbedtls_pk_free(&cert_cur->pk);
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
mbedtls_free(cert_cur->sig_opts);
#endif
name_cur = cert_cur->issuer.next;
while (name_cur != NULL) {
name_prv = name_cur;
name_cur = name_cur->next;
mbedtls_platform_zeroize(name_prv, sizeof(mbedtls_x509_name));
mbedtls_free(name_prv);
}while (name_cur != NULL) { ... }
name_cur = cert_cur->subject.next;
while (name_cur != NULL) {
name_prv = name_cur;
name_cur = name_cur->next;
mbedtls_platform_zeroize(name_prv, sizeof(mbedtls_x509_name));
mbedtls_free(name_prv);
}while (name_cur != NULL) { ... }
seq_cur = cert_cur->ext_key_usage.next;
while (seq_cur != NULL) {
seq_prv = seq_cur;
seq_cur = seq_cur->next;
mbedtls_platform_zeroize(seq_prv,
sizeof(mbedtls_x509_sequence));
mbedtls_free(seq_prv);
}while (seq_cur != NULL) { ... }
seq_cur = cert_cur->subject_alt_names.next;
while (seq_cur != NULL) {
seq_prv = seq_cur;
seq_cur = seq_cur->next;
mbedtls_platform_zeroize(seq_prv,
sizeof(mbedtls_x509_sequence));
mbedtls_free(seq_prv);
}while (seq_cur != NULL) { ... }
seq_cur = cert_cur->certificate_policies.next;
while (seq_cur != NULL) {
seq_prv = seq_cur;
seq_cur = seq_cur->next;
mbedtls_platform_zeroize(seq_prv,
sizeof(mbedtls_x509_sequence));
mbedtls_free(seq_prv);
}while (seq_cur != NULL) { ... }
if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
mbedtls_platform_zeroize(cert_cur->raw.p, cert_cur->raw.len);
mbedtls_free(cert_cur->raw.p);
}if (cert_cur->raw.p != NULL && cert_cur->own_buffer) { ... }
cert_cur = cert_cur->next;
...} while (cert_cur != NULL);
cert_cur = crt;
do {
cert_prv = cert_cur;
cert_cur = cert_cur->next;
mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
if (cert_prv != crt) {
mbedtls_free(cert_prv);
}if (cert_prv != crt) { ... }
...} while (cert_cur != NULL);
}{ ... }
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
/* ... */
void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
{
mbedtls_pk_restart_init(&ctx->pk);
ctx->parent = NULL;
ctx->fallback_parent = NULL;
ctx->fallback_signature_is_good = 0;
ctx->parent_is_trusted = -1;
ctx->in_progress = x509_crt_rs_none;
ctx->self_cnt = 0;
x509_crt_verify_chain_reset(&ctx->ver_chain);
}mbedtls_x509_crt_restart_init (mbedtls_x509_crt_restart_ctx *ctx) { ... }
/* ... */
void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
{
if (ctx == NULL) {
return;
}if (ctx == NULL) { ... }
mbedtls_pk_restart_free(&ctx->pk);
mbedtls_x509_crt_restart_init(ctx);
}mbedtls_x509_crt_restart_free (mbedtls_x509_crt_restart_ctx *ctx) { ... }
/* ... */#endif
/* ... */
#endif