summaryrefslogtreecommitdiffstats
path: root/src/VBox/Runtime/common/crypto/x509-certpaths.cpp
blob: 1ef8cc5c879af222c4a779a19e95da24a9118f1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
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
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
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
1974
1975
1976
1977
1978
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
2016
2017
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
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
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
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
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
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
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
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
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
2832
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
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
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
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
/* $Id: x509-certpaths.cpp $ */
/** @file
 * IPRT - Crypto - X.509, Simple Certificate Path Builder & Validator.
 */

/*
 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
 * in the VirtualBox distribution, in which case the provisions of the
 * CDDL are applicable instead of those of the GPL.
 *
 * You may elect to license modified versions of this file under the
 * terms and conditions of either the GPL or the CDDL or both.
 *
 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#define LOG_GROUP RTLOGGROUP_CRYPTO
#include "internal/iprt.h"
#include <iprt/crypto/x509.h>

#include <iprt/asm.h>
#include <iprt/ctype.h>
#include <iprt/err.h>
#include <iprt/mem.h>
#include <iprt/string.h>
#include <iprt/list.h>
#include <iprt/log.h>
#include <iprt/time.h>
#include <iprt/crypto/applecodesign.h> /* critical extension OIDs */
#include <iprt/crypto/pkcs7.h> /* PCRTCRPKCS7SETOFCERTS */
#include <iprt/crypto/store.h>

#include "x509-internal.h"


/*********************************************************************************************************************************
*   Structures and Typedefs                                                                                                      *
*********************************************************************************************************************************/
/**
 * X.509 certificate path node.
 */
typedef struct RTCRX509CERTPATHNODE
{
    /** Sibling list entry. */
    RTLISTNODE                      SiblingEntry;
    /** List of children or leaf list entry. */
    RTLISTANCHOR                    ChildListOrLeafEntry;
    /** Pointer to the parent node.  NULL for root. */
    struct RTCRX509CERTPATHNODE    *pParent;

    /** The distance between this node and the target.  */
    uint32_t                        uDepth : 8;
    /** Indicates the source of this certificate.  */
    uint32_t                        uSrc : 3;
    /** Set if this is a leaf node. */
    uint32_t                        fLeaf : 1;
    /** Makes sure it's a 32-bit bitfield. */
    uint32_t                        uReserved : 20;

    /** Leaf only: The result of the last path vertification. */
    int                             rcVerify;

    /** Pointer to the certificate.  This can be NULL only for trust anchors. */
    PCRTCRX509CERTIFICATE           pCert;

    /** If the certificate or trust anchor was obtained from a store, this is the
     * associated certificate context (referenced of course).  This is used to
     * access the trust anchor information, if present.
     *
     * (If this is NULL it's from a certificate array or some such given directly to
     * the path building code.  It's assumed the caller doesn't free these until the
     * path validation/whatever is done with and the paths destroyed.) */
    PCRTCRCERTCTX                   pCertCtx;
} RTCRX509CERTPATHNODE;
/** Pointer to a X.509 path node. */
typedef RTCRX509CERTPATHNODE *PRTCRX509CERTPATHNODE;

/** @name RTCRX509CERTPATHNODE::uSrc values.
 * The trusted and untrusted sources ordered in priority order, where higher
 * number means high priority in case of duplicates.
 * @{ */
#define RTCRX509CERTPATHNODE_SRC_NONE               0
#define RTCRX509CERTPATHNODE_SRC_TARGET             1
#define RTCRX509CERTPATHNODE_SRC_UNTRUSTED_SET      2
#define RTCRX509CERTPATHNODE_SRC_UNTRUSTED_ARRAY    3
#define RTCRX509CERTPATHNODE_SRC_UNTRUSTED_STORE    4
#define RTCRX509CERTPATHNODE_SRC_TRUSTED_STORE      5
#define RTCRX509CERTPATHNODE_SRC_TRUSTED_CERT       6
#define RTCRX509CERTPATHNODE_SRC_IS_TRUSTED(uSrc)   ((uSrc) >= RTCRX509CERTPATHNODE_SRC_TRUSTED_STORE)
/** @} */


/**
 * Policy tree node.
 */
typedef struct RTCRX509CERTPATHSPOLICYNODE
{
    /** Sibling list entry. */
    RTLISTNODE                      SiblingEntry;
    /** Tree depth list entry. */
    RTLISTNODE                      DepthEntry;
    /** List of children or leaf list entry. */
    RTLISTANCHOR                    ChildList;
    /** Pointer to the parent. */
    struct RTCRX509CERTPATHSPOLICYNODE *pParent;

    /** The policy object ID. */
    PCRTASN1OBJID                   pValidPolicy;

    /** Optional sequence of policy qualifiers. */
    PCRTCRX509POLICYQUALIFIERINFOS  pPolicyQualifiers;

    /** The first policy ID in the exepcted policy set. */
    PCRTASN1OBJID                   pExpectedPolicyFirst;
    /** Set if we've already mapped pExpectedPolicyFirst. */
    bool                            fAlreadyMapped;
    /** Number of additional items in the expected policy set. */
    uint32_t                        cMoreExpectedPolicySet;
    /** Additional items in the expected policy set.  */
    PCRTASN1OBJID                  *papMoreExpectedPolicySet;
} RTCRX509CERTPATHSPOLICYNODE;
/** Pointer to a policy tree node. */
typedef RTCRX509CERTPATHSPOLICYNODE *PRTCRX509CERTPATHSPOLICYNODE;


/**
 * Path builder and validator instance.
 *
 * The path builder creates a tree of certificates by forward searching from the
 * end-entity towards a trusted source.  The leaf nodes are inserted into list
 * ordered by the source of the leaf certificate and the path length (i.e. tree
 * depth).
 *
 * The path validator works the tree from the leaf end and validates each
 * potential path found by the builder.  It is generally happy with one working
 * path, but may be told to verify all of them.
 */
typedef struct RTCRX509CERTPATHSINT
{
    /** Magic number. */
    uint32_t                        u32Magic;
    /** Reference counter. */
    uint32_t volatile               cRefs;

    /** @name Input
     * @{ */
    /** The target certificate (end entity) to build a trusted path for. */
    PCRTCRX509CERTIFICATE           pTarget;

    /** Lone trusted certificate.  */
    PCRTCRX509CERTIFICATE           pTrustedCert;
    /** Store of trusted certificates. */
    RTCRSTORE                       hTrustedStore;

    /** Store of untrusted certificates. */
    RTCRSTORE                       hUntrustedStore;
    /** Array of untrusted certificates, typically from the protocol. */
    PCRTCRX509CERTIFICATE           paUntrustedCerts;
    /** Number of entries in paUntrusted. */
    uint32_t                        cUntrustedCerts;
    /** Set of untrusted PKCS \#7 / CMS certificatess. */
    PCRTCRPKCS7SETOFCERTS           pUntrustedCertsSet;

    /** UTC time we're going to validate the path at, requires
     *  RTCRX509CERTPATHSINT_F_VALID_TIME to be set. */
    RTTIMESPEC                      ValidTime;
    /** Number of policy OIDs in the user initial policy set, 0 means anyPolicy. */
    uint32_t                        cInitialUserPolicySet;
    /** The user initial policy set.  As with all other user provided data, we
     * assume it's immutable and remains valid for the usage period of the path
     * builder & validator. */
    PCRTASN1OBJID                  *papInitialUserPolicySet;
    /** Number of certificates before the user wants an explicit policy result.
     * Set to UINT32_MAX no explicit policy restriction required by the user. */
    uint32_t                        cInitialExplicitPolicy;
    /** Number of certificates before the user wants policy mapping to be
     * inhibited.  Set to UINT32_MAX if no initial policy mapping inhibition
     * desired by the user. */
    uint32_t                        cInitialPolicyMappingInhibit;
    /** Number of certificates before the user wants the anyPolicy to be rejected.
     * Set to UINT32_MAX no explicit policy restriction required by the user. */
    uint32_t                        cInitialInhibitAnyPolicy;
    /** Initial name restriction: Permitted subtrees.  */
    PCRTCRX509GENERALSUBTREES       pInitialPermittedSubtrees;
    /** Initial name restriction: Excluded subtrees.  */
    PCRTCRX509GENERALSUBTREES       pInitialExcludedSubtrees;

    /** Flags RTCRX509CERTPATHSINT_F_XXX. */
    uint32_t                        fFlags;
    /** @} */

    /** Sticky status for remembering allocation errors and the like.  */
    int32_t                         rc;
    /** Where to store extended error info (optional). */
    PRTERRINFO                      pErrInfo;

    /** @name Path Builder Output
     * @{  */
    /** Pointer to the root of the tree.  This will always be non-NULL after path
     *  building and thus can be reliably used to tell if path building has taken
     *  place or not. */
    PRTCRX509CERTPATHNODE           pRoot;
    /** List of working leaf tree nodes. */
    RTLISTANCHOR                    LeafList;
    /** The number of paths (leafs). */
    uint32_t                        cPaths;
    /** @} */

    /** Path Validator State. */
    struct
    {
        /** Number of nodes in the certificate path we're validating (aka 'n'). */
        uint32_t                        cNodes;
        /** The current node (0 being the trust anchor). */
        uint32_t                        iNode;

        /** The root node of the valid policy tree. */
        PRTCRX509CERTPATHSPOLICYNODE    pValidPolicyTree;
        /** An array of length cNodes + 1 which tracks all nodes at the given (index)
         *  tree depth via the RTCRX509CERTPATHSPOLICYNODE::DepthEntry member. */
        PRTLISTANCHOR                   paValidPolicyDepthLists;

        /** Number of entries in paPermittedSubtrees (name constraints).
         * If zero, no permitted name constrains currently in effect.  */
        uint32_t                        cPermittedSubtrees;
        /** The allocated size of papExcludedSubtrees */
        uint32_t                        cPermittedSubtreesAlloc;
        /** Array of permitted subtrees we've collected so far (name constraints). */
        PCRTCRX509GENERALSUBTREE       *papPermittedSubtrees;
        /** Set if we end up with an empty set after calculating a name constraints
         * union.  */
        bool                            fNoPermittedSubtrees;

        /** Number of entries in paExcludedSubtrees (name constraints).
         * If zero, no excluded name constrains currently in effect.  */
        uint32_t                        cExcludedSubtrees;
        /** Array of excluded subtrees we've collected so far (name constraints). */
        PCRTCRX509GENERALSUBTREES      *papExcludedSubtrees;

        /** Number of non-self-issued certificates to be processed before a non-NULL
         * paValidPolicyTree is required. */
        uint32_t                        cExplicitPolicy;
        /** Number of non-self-issued certificates to be processed we stop processing
         * policy mapping extensions. */
        uint32_t                        cInhibitPolicyMapping;
        /** Number of non-self-issued certificates to be processed before a the
         * anyPolicy is rejected. */
        uint32_t                        cInhibitAnyPolicy;
        /** Number of non-self-issued certificates we're allowed to process. */
        uint32_t                        cMaxPathLength;

        /** The working issuer name. */
        PCRTCRX509NAME                  pWorkingIssuer;
        /** The working public key algorithm ID. */
        PCRTASN1OBJID                   pWorkingPublicKeyAlgorithm;
        /** The working public key algorithm parameters. */
        PCRTASN1DYNTYPE                 pWorkingPublicKeyParameters;
        /** A bit string containing the public key. */
        PCRTASN1BITSTRING               pWorkingPublicKey;
    } v;

    /** An object identifier initialized to anyPolicy. */
    RTASN1OBJID                         AnyPolicyObjId;

    /** Temporary scratch space. */
    char                                szTmp[1024];
} RTCRX509CERTPATHSINT;
typedef RTCRX509CERTPATHSINT *PRTCRX509CERTPATHSINT;

/** Magic value for RTCRX509CERTPATHSINT::u32Magic (Bruce Schneier). */
#define RTCRX509CERTPATHSINT_MAGIC                          UINT32_C(0x19630115)

/** @name RTCRX509CERTPATHSINT_F_XXX - Certificate path build flags.
 * @{ */
#define RTCRX509CERTPATHSINT_F_VALID_TIME                   RT_BIT_32(0)
#define RTCRX509CERTPATHSINT_F_ELIMINATE_UNTRUSTED_PATHS    RT_BIT_32(1)
/** Whether checking the trust anchor signature (if self signed) and
 * that it is valid at the verification time, also require it to be a CA if not
 * leaf node. */
#define RTCRX509CERTPATHSINT_F_CHECK_TRUST_ANCHOR           RT_BIT_32(2)
#define RTCRX509CERTPATHSINT_F_VALID_MASK                   UINT32_C(0x00000007)
/** @} */


/*********************************************************************************************************************************
*   Internal Functions                                                                                                           *
*********************************************************************************************************************************/
static void rtCrX509CertPathsDestroyTree(PRTCRX509CERTPATHSINT pThis);
static void rtCrX509CpvCleanup(PRTCRX509CERTPATHSINT pThis);


/** @name Path Builder and Validator Config APIs
 * @{
 */

RTDECL(int) RTCrX509CertPathsCreate(PRTCRX509CERTPATHS phCertPaths, PCRTCRX509CERTIFICATE pTarget)
{
    AssertPtrReturn(phCertPaths, VERR_INVALID_POINTER);

    PRTCRX509CERTPATHSINT pThis = (PRTCRX509CERTPATHSINT)RTMemAllocZ(sizeof(*pThis));
    if (pThis)
    {
        int rc = RTAsn1ObjId_InitFromString(&pThis->AnyPolicyObjId, RTCRX509_ID_CE_CP_ANY_POLICY_OID, &g_RTAsn1DefaultAllocator);
        if (RT_SUCCESS(rc))
        {
            pThis->u32Magic                     = RTCRX509CERTPATHSINT_MAGIC;
            pThis->cRefs                        = 1;
            pThis->pTarget                      = pTarget;
            pThis->hTrustedStore                = NIL_RTCRSTORE;
            pThis->hUntrustedStore              = NIL_RTCRSTORE;
            pThis->cInitialExplicitPolicy       = UINT32_MAX;
            pThis->cInitialPolicyMappingInhibit = UINT32_MAX;
            pThis->cInitialInhibitAnyPolicy     = UINT32_MAX;
            pThis->rc                           = VINF_SUCCESS;
            RTListInit(&pThis->LeafList);
            *phCertPaths = pThis;
            return VINF_SUCCESS;
        }
        return rc;
    }
    return VERR_NO_MEMORY;
}


RTDECL(uint32_t) RTCrX509CertPathsRetain(RTCRX509CERTPATHS hCertPaths)
{
    PRTCRX509CERTPATHSINT pThis = hCertPaths;
    AssertPtrReturn(pThis, UINT32_MAX);

    uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
    Assert(cRefs > 0 && cRefs < 64);
    return cRefs;
}


RTDECL(uint32_t) RTCrX509CertPathsRelease(RTCRX509CERTPATHS hCertPaths)
{
    uint32_t cRefs;
    if (hCertPaths != NIL_RTCRX509CERTPATHS)
    {
        PRTCRX509CERTPATHSINT pThis = hCertPaths;
        AssertPtrReturn(pThis, UINT32_MAX);
        AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, UINT32_MAX);

        cRefs = ASMAtomicDecU32(&pThis->cRefs);
        Assert(cRefs < 64);
        if (!cRefs)
        {
            /*
             * No more references, destroy the whole thing.
             */
            ASMAtomicWriteU32(&pThis->u32Magic, ~RTCRX509CERTPATHSINT_MAGIC);

            /* config */
            pThis->pTarget                      = NULL; /* Referencing user memory. */
            pThis->pTrustedCert                 = NULL; /* Referencing user memory. */
            RTCrStoreRelease(pThis->hTrustedStore);
            pThis->hTrustedStore                = NIL_RTCRSTORE;
            RTCrStoreRelease(pThis->hUntrustedStore);
            pThis->hUntrustedStore              = NIL_RTCRSTORE;
            pThis->paUntrustedCerts             = NULL; /* Referencing user memory. */
            pThis->pUntrustedCertsSet           = NULL; /* Referencing user memory. */
            pThis->papInitialUserPolicySet      = NULL; /* Referencing user memory. */
            pThis->pInitialPermittedSubtrees    = NULL; /* Referencing user memory. */
            pThis->pInitialExcludedSubtrees     = NULL; /* Referencing user memory. */

            /* builder */
            rtCrX509CertPathsDestroyTree(pThis);

            /* validator */
            rtCrX509CpvCleanup(pThis);

            /* misc */
            RTAsn1VtDelete(&pThis->AnyPolicyObjId.Asn1Core);

            /* Finally, the instance itself. */
            RTMemFree(pThis);
        }
    }
    else
        cRefs = 0;
    return cRefs;
}



RTDECL(int) RTCrX509CertPathsSetTrustedStore(RTCRX509CERTPATHS hCertPaths, RTCRSTORE hTrustedStore)
{
    PRTCRX509CERTPATHSINT pThis = hCertPaths;
    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, VERR_INVALID_HANDLE);
    AssertReturn(pThis->pRoot == NULL, VERR_WRONG_ORDER);

    if (pThis->hTrustedStore != NIL_RTCRSTORE)
    {
        RTCrStoreRelease(pThis->hTrustedStore);
        pThis->hTrustedStore = NIL_RTCRSTORE;
    }
    if (hTrustedStore != NIL_RTCRSTORE)
    {
        AssertReturn(RTCrStoreRetain(hTrustedStore) != UINT32_MAX, VERR_INVALID_HANDLE);
        pThis->hTrustedStore = hTrustedStore;
    }
    return VINF_SUCCESS;
}


RTDECL(int) RTCrX509CertPathsSetUntrustedStore(RTCRX509CERTPATHS hCertPaths, RTCRSTORE hUntrustedStore)
{
    PRTCRX509CERTPATHSINT pThis = hCertPaths;
    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, VERR_INVALID_HANDLE);
    AssertReturn(pThis->pRoot == NULL, VERR_WRONG_ORDER);

    if (pThis->hUntrustedStore != NIL_RTCRSTORE)
    {
        RTCrStoreRelease(pThis->hUntrustedStore);
        pThis->hUntrustedStore = NIL_RTCRSTORE;
    }
    if (hUntrustedStore != NIL_RTCRSTORE)
    {
        AssertReturn(RTCrStoreRetain(hUntrustedStore) != UINT32_MAX, VERR_INVALID_HANDLE);
        pThis->hUntrustedStore = hUntrustedStore;
    }
    return VINF_SUCCESS;
}


RTDECL(int) RTCrX509CertPathsSetUntrustedArray(RTCRX509CERTPATHS hCertPaths, PCRTCRX509CERTIFICATE paCerts, uint32_t cCerts)
{
    PRTCRX509CERTPATHSINT pThis = hCertPaths;
    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, VERR_INVALID_HANDLE);

    pThis->paUntrustedCerts = paCerts;
    pThis->cUntrustedCerts  = cCerts;
    return VINF_SUCCESS;
}


RTDECL(int) RTCrX509CertPathsSetUntrustedSet(RTCRX509CERTPATHS hCertPaths, PCRTCRPKCS7SETOFCERTS pSetOfCerts)
{
    PRTCRX509CERTPATHSINT pThis = hCertPaths;
    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, VERR_INVALID_HANDLE);

    pThis->pUntrustedCertsSet = pSetOfCerts;
    return VINF_SUCCESS;
}


RTDECL(int) RTCrX509CertPathsSetValidTime(RTCRX509CERTPATHS hCertPaths, PCRTTIME pTime)
{
    PRTCRX509CERTPATHSINT pThis = hCertPaths;
    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, VERR_INVALID_HANDLE);

    /* Allow this after building paths, as it's only used during verification. */

    if (pTime)
    {
        if (RTTimeImplode(&pThis->ValidTime, pTime))
            return VERR_INVALID_PARAMETER;
        pThis->fFlags |= RTCRX509CERTPATHSINT_F_VALID_TIME;
    }
    else
        pThis->fFlags &= ~RTCRX509CERTPATHSINT_F_VALID_TIME;
    return VINF_SUCCESS;
}


RTDECL(int) RTCrX509CertPathsSetValidTimeSpec(RTCRX509CERTPATHS hCertPaths, PCRTTIMESPEC pTimeSpec)
{
    PRTCRX509CERTPATHSINT pThis = hCertPaths;
    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, VERR_INVALID_HANDLE);

    /* Allow this after building paths, as it's only used during verification. */

    if (pTimeSpec)
    {
        pThis->ValidTime = *pTimeSpec;
        pThis->fFlags |= RTCRX509CERTPATHSINT_F_VALID_TIME;
    }
    else
        pThis->fFlags &= ~RTCRX509CERTPATHSINT_F_VALID_TIME;
    return VINF_SUCCESS;
}


RTDECL(int) RTCrX509CertPathsSetTrustAnchorChecks(RTCRX509CERTPATHS hCertPaths, bool fEnable)
{
    PRTCRX509CERTPATHSINT pThis = hCertPaths;
    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, VERR_INVALID_HANDLE);

    if (fEnable)
        pThis->fFlags |= RTCRX509CERTPATHSINT_F_CHECK_TRUST_ANCHOR;
    else
        pThis->fFlags &= ~RTCRX509CERTPATHSINT_F_CHECK_TRUST_ANCHOR;
    return VINF_SUCCESS;
}


RTDECL(int) RTCrX509CertPathsCreateEx(PRTCRX509CERTPATHS phCertPaths, PCRTCRX509CERTIFICATE pTarget, RTCRSTORE hTrustedStore,
                                      RTCRSTORE hUntrustedStore, PCRTCRX509CERTIFICATE paUntrustedCerts, uint32_t cUntrustedCerts,
                                      PCRTTIMESPEC pValidTime)
{
    int rc = RTCrX509CertPathsCreate(phCertPaths, pTarget);
    if (RT_SUCCESS(rc))
    {
        PRTCRX509CERTPATHSINT pThis = *phCertPaths;

        rc = RTCrX509CertPathsSetTrustedStore(pThis, hTrustedStore);
        if (RT_SUCCESS(rc))
        {
            rc = RTCrX509CertPathsSetUntrustedStore(pThis, hUntrustedStore);
            if (RT_SUCCESS(rc))
            {
                rc = RTCrX509CertPathsSetUntrustedArray(pThis, paUntrustedCerts, cUntrustedCerts);
                if (RT_SUCCESS(rc))
                {
                    rc = RTCrX509CertPathsSetValidTimeSpec(pThis, pValidTime);
                    if (RT_SUCCESS(rc))
                    {
                        return VINF_SUCCESS;
                    }
                }
                RTCrStoreRelease(pThis->hUntrustedStore);
            }
            RTCrStoreRelease(pThis->hTrustedStore);
        }
        RTMemFree(pThis);
        *phCertPaths = NIL_RTCRX509CERTPATHS;
    }
    return rc;
}

/** @} */



/** @name Path Builder and Validator Common Utility Functions.
 * @{
 */

/**
 * Checks if the certificate is self-issued.
 *
 * @returns true / false.
 * @param   pNode               The path node to check..
 */
static bool rtCrX509CertPathsIsSelfIssued(PRTCRX509CERTPATHNODE pNode)
{
    return pNode->pCert
        && RTCrX509Name_MatchByRfc5280(&pNode->pCert->TbsCertificate.Subject, &pNode->pCert->TbsCertificate.Issuer);
}

/**
 * Helper for checking whether a certificate is in the trusted store or not.
 */
static bool rtCrX509CertPathsIsCertInStore(PRTCRX509CERTPATHNODE pNode, RTCRSTORE hStore)
{
    bool fRc = false;
    PCRTCRCERTCTX pCertCtx = RTCrStoreCertByIssuerAndSerialNo(hStore, &pNode->pCert->TbsCertificate.Issuer,
                                                              &pNode->pCert->TbsCertificate.SerialNumber);
    if (pCertCtx)
    {
        if (pCertCtx->pCert)
            fRc = RTCrX509Certificate_Compare(pCertCtx->pCert, pNode->pCert) == 0;
        RTCrCertCtxRelease(pCertCtx);
    }
    return fRc;
}

/** @}  */



/** @name Path Builder Functions.
 * @{
 */

static PRTCRX509CERTPATHNODE rtCrX509CertPathsNewNode(PRTCRX509CERTPATHSINT pThis)
{
    PRTCRX509CERTPATHNODE pNode = (PRTCRX509CERTPATHNODE)RTMemAllocZ(sizeof(*pNode));
    if (RT_LIKELY(pNode))
    {
        RTListInit(&pNode->SiblingEntry);
        RTListInit(&pNode->ChildListOrLeafEntry);
        pNode->rcVerify = VERR_CR_X509_NOT_VERIFIED;

        return pNode;
    }

    pThis->rc = RTErrInfoSet(pThis->pErrInfo, VERR_NO_MEMORY, "No memory for path node");
    return NULL;
}


static void rtCrX509CertPathsDestroyNode(PRTCRX509CERTPATHNODE pNode)
{
    if (pNode->pCertCtx)
    {
        RTCrCertCtxRelease(pNode->pCertCtx);
        pNode->pCertCtx = NULL;
    }
    RT_ZERO(*pNode);
    RTMemFree(pNode);
}


static void rtCrX509CertPathsAddIssuer(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHNODE pParent,
                                       PCRTCRX509CERTIFICATE pCert, PCRTCRCERTCTX pCertCtx, uint8_t uSrc)
{
    /*
     * Check if we've seen this certificate already in the current path or
     * among the already gathered issuers.
     */
    if (pCert)
    {
        /* No duplicate certificates in the path. */
        PRTCRX509CERTPATHNODE pTmpNode = pParent;
        while (pTmpNode)
        {
            Assert(pTmpNode->pCert);
            if (   pTmpNode->pCert == pCert
                || RTCrX509Certificate_Compare(pTmpNode->pCert, pCert) == 0)
            {
                /* If target and the source it trusted, upgrade the source so we can successfully verify single node 'paths'. */
                if (   RTCRX509CERTPATHNODE_SRC_IS_TRUSTED(uSrc)
                    && pTmpNode == pParent
                    && pTmpNode->uSrc == RTCRX509CERTPATHNODE_SRC_TARGET)
                {
                    AssertReturnVoid(!pTmpNode->pParent);
                    pTmpNode->uSrc = uSrc;
                }
                return;
            }
            pTmpNode = pTmpNode->pParent;
        }

        /* No duplicate tree branches. */
        RTListForEach(&pParent->ChildListOrLeafEntry, pTmpNode, RTCRX509CERTPATHNODE, SiblingEntry)
        {
            if (RTCrX509Certificate_Compare(pTmpNode->pCert, pCert) == 0)
                return;
        }
    }
    else
        Assert(pCertCtx);

    /*
     * Reference the context core before making the allocation.
     */
    if (pCertCtx)
        AssertReturnVoidStmt(RTCrCertCtxRetain(pCertCtx) != UINT32_MAX,
                             pThis->rc = RTErrInfoSetF(pThis->pErrInfo, VERR_CR_X509_CPB_BAD_CERT_CTX,
                                                       "Bad pCertCtx=%p", pCertCtx));

    /*
     * We haven't see it, append it as a child.
     */
    PRTCRX509CERTPATHNODE pNew = rtCrX509CertPathsNewNode(pThis);
    if (pNew)
    {
        pNew->pParent  = pParent;
        pNew->pCert    = pCert;
        pNew->pCertCtx = pCertCtx;
        pNew->uSrc     = uSrc;
        pNew->uDepth   = pParent->uDepth + 1;
        RTListAppend(&pParent->ChildListOrLeafEntry, &pNew->SiblingEntry);
        Log2Func(("pNew=%p uSrc=%u uDepth=%u\n", pNew, uSrc, pNew->uDepth));
    }
    else
        RTCrCertCtxRelease(pCertCtx);
}


static void rtCrX509CertPathsGetIssuersFromStore(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHNODE pNode,
                                                 PCRTCRX509NAME pIssuer, RTCRSTORE hStore, uint8_t uSrc)
{
    RTCRSTORECERTSEARCH Search;
    int rc = RTCrStoreCertFindBySubjectOrAltSubjectByRfc5280(hStore, pIssuer, &Search);
    if (RT_SUCCESS(rc))
    {
        PCRTCRCERTCTX pCertCtx;
        while ((pCertCtx = RTCrStoreCertSearchNext(hStore, &Search)) != NULL)
        {
            if (   pCertCtx->pCert
                || (   RTCRX509CERTPATHNODE_SRC_IS_TRUSTED(uSrc)
                    && pCertCtx->pTaInfo) )
                rtCrX509CertPathsAddIssuer(pThis, pNode, pCertCtx->pCert, pCertCtx, uSrc);
            RTCrCertCtxRelease(pCertCtx);
        }
        RTCrStoreCertSearchDestroy(hStore, &Search);
    }
}


static void rtCrX509CertPathsGetIssuers(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHNODE pNode)
{
    Assert(RTListIsEmpty(&pNode->ChildListOrLeafEntry));
    Assert(!pNode->fLeaf);
    Assert(pNode->pCert);

    /*
     * Don't recurse infintely.
     */
    if (RT_UNLIKELY(pNode->uDepth >= 50))
        return;

    PCRTCRX509NAME const pIssuer = &pNode->pCert->TbsCertificate.Issuer;
#if defined(LOG_ENABLED) && defined(IN_RING3)
    if (LogIs2Enabled())
    {
        char szIssuer[128] = {0};
        RTCrX509Name_FormatAsString(pIssuer, szIssuer, sizeof(szIssuer), NULL);
        char szSubject[128] = {0};
        RTCrX509Name_FormatAsString(&pNode->pCert->TbsCertificate.Subject, szSubject, sizeof(szSubject), NULL);
        Log2Func(("pNode=%p uSrc=%u uDepth=%u Issuer='%s' (Subject='%s')\n", pNode, pNode->uSrc, pNode->uDepth, szIssuer, szSubject));
    }
#endif

    /*
     * Trusted certificate.
     */
    if (   pThis->pTrustedCert
        && RTCrX509Certificate_MatchSubjectOrAltSubjectByRfc5280(pThis->pTrustedCert, pIssuer))
        rtCrX509CertPathsAddIssuer(pThis, pNode, pThis->pTrustedCert, NULL, RTCRX509CERTPATHNODE_SRC_TRUSTED_CERT);

    /*
     * Trusted certificate store.
     */
    if (pThis->hTrustedStore != NIL_RTCRSTORE)
        rtCrX509CertPathsGetIssuersFromStore(pThis, pNode, pIssuer, pThis->hTrustedStore,
                                             RTCRX509CERTPATHNODE_SRC_TRUSTED_STORE);

    /*
     * Untrusted store.
     */
    if (pThis->hUntrustedStore != NIL_RTCRSTORE)
        rtCrX509CertPathsGetIssuersFromStore(pThis, pNode, pIssuer, pThis->hTrustedStore,
                                             RTCRX509CERTPATHNODE_SRC_UNTRUSTED_STORE);

    /*
     * Untrusted array.
     */
    if (pThis->paUntrustedCerts)
        for (uint32_t i = 0; i < pThis->cUntrustedCerts; i++)
            if (RTCrX509Certificate_MatchSubjectOrAltSubjectByRfc5280(&pThis->paUntrustedCerts[i], pIssuer))
                rtCrX509CertPathsAddIssuer(pThis, pNode, &pThis->paUntrustedCerts[i], NULL,
                                           RTCRX509CERTPATHNODE_SRC_UNTRUSTED_ARRAY);

    /** @todo Rainy day: Should abstract the untrusted array and set so we don't get
     *        unnecessary PKCS7/CMS header dependencies. */

    /*
     * Untrusted set.
     */
    if (pThis->pUntrustedCertsSet)
    {
        uint32_t const        cCerts   = pThis->pUntrustedCertsSet->cItems;
        PRTCRPKCS7CERT const *papCerts = pThis->pUntrustedCertsSet->papItems;
        for (uint32_t i = 0; i < cCerts; i++)
        {
            PCRTCRPKCS7CERT pCert = papCerts[i];
            if (   pCert->enmChoice == RTCRPKCS7CERTCHOICE_X509
                && RTCrX509Certificate_MatchSubjectOrAltSubjectByRfc5280(pCert->u.pX509Cert, pIssuer))
                rtCrX509CertPathsAddIssuer(pThis, pNode, pCert->u.pX509Cert, NULL, RTCRX509CERTPATHNODE_SRC_UNTRUSTED_SET);
        }
    }
}


static PRTCRX509CERTPATHNODE rtCrX509CertPathsGetNextRightUp(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHNODE pNode)
{
    for (;;)
    {
        /* The root node has no siblings. */
        PRTCRX509CERTPATHNODE pParent = pNode->pParent;
        if (!pNode->pParent)
            return NULL;

        /* Try go to the right. */
        PRTCRX509CERTPATHNODE pNext = RTListGetNext(&pParent->ChildListOrLeafEntry, pNode, RTCRX509CERTPATHNODE, SiblingEntry);
        if (pNext)
            return pNext;

        /* Up. */
        pNode = pParent;
    }

    RT_NOREF_PV(pThis);
}


static PRTCRX509CERTPATHNODE rtCrX509CertPathsEliminatePath(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHNODE pNode)
{
    for (;;)
    {
        Assert(RTListIsEmpty(&pNode->ChildListOrLeafEntry));

        /* Don't remove the root node. */
        PRTCRX509CERTPATHNODE pParent = pNode->pParent;
        if (!pParent)
            return NULL;

        /* Before removing and deleting the node check if there is sibling
           right to it that we should continue processing from. */
        PRTCRX509CERTPATHNODE pNext = RTListGetNext(&pParent->ChildListOrLeafEntry, pNode, RTCRX509CERTPATHNODE, SiblingEntry);
        RTListNodeRemove(&pNode->SiblingEntry);
        rtCrX509CertPathsDestroyNode(pNode);

        if (pNext)
            return pNext;

        /* If the parent node cannot be removed, do a normal get-next-rigth-up
           to find the continuation point for the tree loop. */
        if (!RTListIsEmpty(&pParent->ChildListOrLeafEntry))
            return rtCrX509CertPathsGetNextRightUp(pThis, pParent);

        pNode = pParent;
    }
}


/**
 * Destroys the whole path tree.
 *
 * @param   pThis       The path builder and verifier instance.
 */
static void rtCrX509CertPathsDestroyTree(PRTCRX509CERTPATHSINT pThis)
{
    PRTCRX509CERTPATHNODE pNode, pNextLeaf;
    RTListForEachSafe(&pThis->LeafList, pNode, pNextLeaf, RTCRX509CERTPATHNODE, ChildListOrLeafEntry)
    {
        RTListNodeRemove(&pNode->ChildListOrLeafEntry);
        RTListInit(&pNode->ChildListOrLeafEntry);

        for (;;)
        {
            PRTCRX509CERTPATHNODE pParent = pNode->pParent;

            RTListNodeRemove(&pNode->SiblingEntry);
            rtCrX509CertPathsDestroyNode(pNode);

            if (!pParent)
            {
                pThis->pRoot = NULL;
                break;
            }

            if (!RTListIsEmpty(&pParent->ChildListOrLeafEntry))
                break;

            pNode = pParent;
        }
    }
    Assert(!pThis->pRoot);
}


/**
 * Adds a leaf node.
 *
 * This should normally be a trusted certificate, but the caller can also
 * request the incomplete paths, in which case this will be an untrusted
 * certificate.
 *
 * @returns Pointer to the next node in the tree to process.
 * @param   pThis               The path builder instance.
 * @param   pNode               The leaf node.
 */
static PRTCRX509CERTPATHNODE rtCrX509CertPathsAddLeaf(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHNODE pNode)
{
    pNode->fLeaf = true;

    /*
     * Priority insert by source and depth.
     */
    PRTCRX509CERTPATHNODE pCurLeaf;
    RTListForEach(&pThis->LeafList, pCurLeaf, RTCRX509CERTPATHNODE, ChildListOrLeafEntry)
    {
        if (   pNode->uSrc > pCurLeaf->uSrc
            || (   pNode->uSrc == pCurLeaf->uSrc
                && pNode->uDepth < pCurLeaf->uDepth) )
        {
            RTListNodeInsertBefore(&pCurLeaf->ChildListOrLeafEntry, &pNode->ChildListOrLeafEntry);
            pThis->cPaths++;
            return rtCrX509CertPathsGetNextRightUp(pThis, pNode);
        }
    }

    RTListAppend(&pThis->LeafList, &pNode->ChildListOrLeafEntry);
    pThis->cPaths++;
    return rtCrX509CertPathsGetNextRightUp(pThis, pNode);
}



RTDECL(int) RTCrX509CertPathsBuild(RTCRX509CERTPATHS hCertPaths, PRTERRINFO pErrInfo)
{
    /*
     * Validate the input.
     */
    PRTCRX509CERTPATHSINT pThis = hCertPaths;
    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, VERR_INVALID_HANDLE);
    AssertReturn(!(pThis->fFlags & ~RTCRX509CERTPATHSINT_F_VALID_MASK), VERR_INVALID_PARAMETER);
    AssertReturn(   (pThis->paUntrustedCerts == NULL && pThis->cUntrustedCerts == 0)
                 || (pThis->paUntrustedCerts != NULL && pThis->cUntrustedCerts > 0),
                 VERR_INVALID_PARAMETER);
    AssertReturn(RTListIsEmpty(&pThis->LeafList), VERR_INVALID_PARAMETER);
    AssertReturn(pThis->pRoot == NULL, VERR_INVALID_PARAMETER);
    AssertReturn(pThis->rc == VINF_SUCCESS, pThis->rc);
    AssertPtrReturn(pThis->pTarget, VERR_INVALID_PARAMETER);
    Assert(RT_SUCCESS(RTCrX509Certificate_CheckSanity(pThis->pTarget, 0, NULL, NULL)));

    /*
     * Set up the target.
     */
    PRTCRX509CERTPATHNODE pCur;
    pThis->pRoot = pCur = rtCrX509CertPathsNewNode(pThis);
    if (pThis->pRoot)
    {
        pCur->pCert  = pThis->pTarget;
        pCur->uDepth = 0;
        pCur->uSrc   = RTCRX509CERTPATHNODE_SRC_TARGET;

        /* Check if the target is trusted and do the upgrade (this is outside the RFC,
           but this simplifies the path validator usage a lot (less work for the caller)). */
        if (   pThis->pTrustedCert
            && RTCrX509Certificate_Compare(pThis->pTrustedCert, pCur->pCert) == 0)
            pCur->uSrc = RTCRX509CERTPATHNODE_SRC_TRUSTED_CERT;
        else if (   pThis->hTrustedStore != NIL_RTCRSTORE
                 && rtCrX509CertPathsIsCertInStore(pCur, pThis->hTrustedStore))
            pCur->uSrc = RTCRX509CERTPATHNODE_SRC_TRUSTED_STORE;

        pThis->pErrInfo = pErrInfo;

        /*
         * The tree construction loop.
         * Walks down, up, and right as the tree is constructed.
         */
        do
        {
            /*
             * Check for the two leaf cases first.
             */
            if (RTCRX509CERTPATHNODE_SRC_IS_TRUSTED(pCur->uSrc))
                pCur = rtCrX509CertPathsAddLeaf(pThis, pCur);
#if 0 /* This isn't right.*/
            else if (rtCrX509CertPathsIsSelfIssued(pCur))
            {
                if (pThis->fFlags & RTCRX509CERTPATHSINT_F_ELIMINATE_UNTRUSTED_PATHS)
                    pCur = rtCrX509CertPathsEliminatePath(pThis, pCur);
                else
                    pCur = rtCrX509CertPathsAddLeaf(pThis, pCur);
            }
#endif
            /*
             * Not a leaf, find all potential issuers and decend into these.
             */
            else
            {
                rtCrX509CertPathsGetIssuers(pThis, pCur);
                if (RT_FAILURE(pThis->rc))
                    break;

                if (!RTListIsEmpty(&pCur->ChildListOrLeafEntry))
                    pCur = RTListGetFirst(&pCur->ChildListOrLeafEntry, RTCRX509CERTPATHNODE, SiblingEntry);
                else if (pThis->fFlags & RTCRX509CERTPATHSINT_F_ELIMINATE_UNTRUSTED_PATHS)
                    pCur = rtCrX509CertPathsEliminatePath(pThis, pCur);
                else
                    pCur = rtCrX509CertPathsAddLeaf(pThis, pCur);
            }
            if (pCur)
                Log2(("RTCrX509CertPathsBuild: pCur=%p fLeaf=%d pParent=%p pNext=%p pPrev=%p\n",
                      pCur, pCur->fLeaf, pCur->pParent,
                      pCur->pParent ? RTListGetNext(&pCur->pParent->ChildListOrLeafEntry, pCur, RTCRX509CERTPATHNODE, SiblingEntry) : NULL,
                      pCur->pParent ? RTListGetPrev(&pCur->pParent->ChildListOrLeafEntry, pCur, RTCRX509CERTPATHNODE, SiblingEntry) : NULL));
        } while (pCur);

        pThis->pErrInfo = NULL;
        if (RT_SUCCESS(pThis->rc))
            return VINF_SUCCESS;
    }
    else
        Assert(RT_FAILURE_NP(pThis->rc));
    return pThis->rc;
}


/**
 * Looks up path by leaf/path index.
 *
 * @returns Pointer to the leaf node of the path.
 * @param   pThis           The path builder & validator instance.
 * @param   iPath           The oridnal of the path to get.
 */
static PRTCRX509CERTPATHNODE rtCrX509CertPathsGetLeafByIndex(PRTCRX509CERTPATHSINT pThis, uint32_t iPath)
{
    Assert(iPath < pThis->cPaths);

    uint32_t                iCurPath = 0;
    PRTCRX509CERTPATHNODE   pCurLeaf;
    RTListForEach(&pThis->LeafList, pCurLeaf, RTCRX509CERTPATHNODE, ChildListOrLeafEntry)
    {
        if (iCurPath == iPath)
            return pCurLeaf;
        iCurPath++;
    }

    AssertFailedReturn(NULL);
}


static void rtDumpPrintf(PFNRTDUMPPRINTFV pfnPrintfV, void *pvUser, const char *pszFormat, ...)
{
    va_list va;
    va_start(va, pszFormat);
    pfnPrintfV(pvUser, pszFormat, va);
    va_end(va);
}


static void rtDumpIndent(PFNRTDUMPPRINTFV pfnPrintfV, void *pvUser, uint32_t cchSpaces, const char *pszFormat, ...)
{
    static const char s_szSpaces[] = "                          ";
    while (cchSpaces > 0)
    {
        uint32_t cchBurst = RT_MIN(sizeof(s_szSpaces) - 1, cchSpaces);
        rtDumpPrintf(pfnPrintfV, pvUser, &s_szSpaces[sizeof(s_szSpaces) - cchBurst - 1]);
        cchSpaces -= cchBurst;
    }

    va_list va;
    va_start(va, pszFormat);
    pfnPrintfV(pvUser, pszFormat, va);
    va_end(va);
}

/** @name X.500 attribute types
 * See RFC-4519 among others.
 * @{ */
#define RTCRX500_ID_AT_OBJECT_CLASS_OID                     "2.5.4.0"
#define RTCRX500_ID_AT_ALIASED_ENTRY_NAME_OID               "2.5.4.1"
#define RTCRX500_ID_AT_KNOWLDGEINFORMATION_OID              "2.5.4.2"
#define RTCRX500_ID_AT_COMMON_NAME_OID                      "2.5.4.3"
#define RTCRX500_ID_AT_SURNAME_OID                          "2.5.4.4"
#define RTCRX500_ID_AT_SERIAL_NUMBER_OID                    "2.5.4.5"
#define RTCRX500_ID_AT_COUNTRY_NAME_OID                     "2.5.4.6"
#define RTCRX500_ID_AT_LOCALITY_NAME_OID                    "2.5.4.7"
#define RTCRX500_ID_AT_STATE_OR_PROVINCE_NAME_OID           "2.5.4.8"
#define RTCRX500_ID_AT_STREET_ADDRESS_OID                   "2.5.4.9"
#define RTCRX500_ID_AT_ORGANIZATION_NAME_OID                "2.5.4.10"
#define RTCRX500_ID_AT_ORGANIZATION_UNIT_NAME_OID           "2.5.4.11"
#define RTCRX500_ID_AT_TITLE_OID                            "2.5.4.12"
#define RTCRX500_ID_AT_DESCRIPTION_OID                      "2.5.4.13"
#define RTCRX500_ID_AT_SEARCH_GUIDE_OID                     "2.5.4.14"
#define RTCRX500_ID_AT_BUSINESS_CATEGORY_OID                "2.5.4.15"
#define RTCRX500_ID_AT_POSTAL_ADDRESS_OID                   "2.5.4.16"
#define RTCRX500_ID_AT_POSTAL_CODE_OID                      "2.5.4.17"
#define RTCRX500_ID_AT_POST_OFFICE_BOX_OID                  "2.5.4.18"
#define RTCRX500_ID_AT_PHYSICAL_DELIVERY_OFFICE_NAME_OID    "2.5.4.19"
#define RTCRX500_ID_AT_TELEPHONE_NUMBER_OID                 "2.5.4.20"
#define RTCRX500_ID_AT_TELEX_NUMBER_OID                     "2.5.4.21"
#define RTCRX500_ID_AT_TELETEX_TERMINAL_IDENTIFIER_OID      "2.5.4.22"
#define RTCRX500_ID_AT_FACIMILE_TELEPHONE_NUMBER_OID        "2.5.4.23"
#define RTCRX500_ID_AT_X121_ADDRESS_OID                     "2.5.4.24"
#define RTCRX500_ID_AT_INTERNATIONAL_ISDN_NUMBER_OID        "2.5.4.25"
#define RTCRX500_ID_AT_REGISTERED_ADDRESS_OID               "2.5.4.26"
#define RTCRX500_ID_AT_DESTINATION_INDICATOR_OID            "2.5.4.27"
#define RTCRX500_ID_AT_PREFERRED_DELIVERY_METHOD_OID        "2.5.4.28"
#define RTCRX500_ID_AT_PRESENTATION_ADDRESS_OID             "2.5.4.29"
#define RTCRX500_ID_AT_SUPPORTED_APPLICATION_CONTEXT_OID    "2.5.4.30"
#define RTCRX500_ID_AT_MEMBER_OID                           "2.5.4.31"
#define RTCRX500_ID_AT_OWNER_OID                            "2.5.4.32"
#define RTCRX500_ID_AT_ROLE_OCCUPANT_OID                    "2.5.4.33"
#define RTCRX500_ID_AT_SEE_ALSO_OID                         "2.5.4.34"
#define RTCRX500_ID_AT_USER_PASSWORD_OID                    "2.5.4.35"
#define RTCRX500_ID_AT_USER_CERTIFICATE_OID                 "2.5.4.36"
#define RTCRX500_ID_AT_CA_CERTIFICATE_OID                   "2.5.4.37"
#define RTCRX500_ID_AT_AUTHORITY_REVOCATION_LIST_OID        "2.5.4.38"
#define RTCRX500_ID_AT_CERTIFICATE_REVOCATION_LIST_OID      "2.5.4.39"
#define RTCRX500_ID_AT_CROSS_CERTIFICATE_PAIR_OID           "2.5.4.40"
#define RTCRX500_ID_AT_NAME_OID                             "2.5.4.41"
#define RTCRX500_ID_AT_GIVEN_NAME_OID                       "2.5.4.42"
#define RTCRX500_ID_AT_INITIALS_OID                         "2.5.4.43"
#define RTCRX500_ID_AT_GENERATION_QUALIFIER_OID             "2.5.4.44"
#define RTCRX500_ID_AT_UNIQUE_IDENTIFIER_OID                "2.5.4.45"
#define RTCRX500_ID_AT_DN_QUALIFIER_OID                     "2.5.4.46"
#define RTCRX500_ID_AT_ENHANCHED_SEARCH_GUIDE_OID           "2.5.4.47"
#define RTCRX500_ID_AT_PROTOCOL_INFORMATION_OID             "2.5.4.48"
#define RTCRX500_ID_AT_DISTINGUISHED_NAME_OID               "2.5.4.49"
#define RTCRX500_ID_AT_UNIQUE_MEMBER_OID                    "2.5.4.50"
#define RTCRX500_ID_AT_HOUSE_IDENTIFIER_OID                 "2.5.4.51"
#define RTCRX500_ID_AT_SUPPORTED_ALGORITHMS_OID             "2.5.4.52"
#define RTCRX500_ID_AT_DELTA_REVOCATION_LIST_OID            "2.5.4.53"
#define RTCRX500_ID_AT_ATTRIBUTE_CERTIFICATE_OID            "2.5.4.58"
#define RTCRX500_ID_AT_PSEUDONYM_OID                        "2.5.4.65"
/** @} */


static void rtCrX509NameDump(PCRTCRX509NAME pName, PFNRTDUMPPRINTFV pfnPrintfV, void *pvUser)
{
    for (uint32_t i = 0; i < pName->cItems; i++)
    {
        PCRTCRX509RELATIVEDISTINGUISHEDNAME const pRdn = pName->papItems[i];
        for (uint32_t j = 0; j < pRdn->cItems; j++)
        {
            PRTCRX509ATTRIBUTETYPEANDVALUE pAttrib = pRdn->papItems[j];

            const char *pszType = RTCrX509Name_GetShortRdn(&pAttrib->Type);
            if (!pszType)
                pszType = pAttrib->Type.szObjId;
            rtDumpPrintf(pfnPrintfV, pvUser, "/%s=", pszType);
            if (pAttrib->Value.enmType == RTASN1TYPE_STRING)
            {
                if (pAttrib->Value.u.String.pszUtf8)
                    rtDumpPrintf(pfnPrintfV, pvUser, "%s", pAttrib->Value.u.String.pszUtf8);
                else
                {
                    const char *pch = pAttrib->Value.u.String.Asn1Core.uData.pch;
                    uint32_t    cch = pAttrib->Value.u.String.Asn1Core.cb;
                    int rc = RTStrValidateEncodingEx(pch, cch, 0);
                    if (RT_SUCCESS(rc) && cch)
                        rtDumpPrintf(pfnPrintfV, pvUser, "%.*s", (size_t)cch, pch);
                    else
                        while (cch > 0)
                        {
                            if (RT_C_IS_PRINT(*pch))
                                rtDumpPrintf(pfnPrintfV, pvUser, "%c", *pch);
                            else
                                rtDumpPrintf(pfnPrintfV, pvUser, "\\x%02x", *pch);
                            cch--;
                            pch++;
                        }
                }
            }
            else
                rtDumpPrintf(pfnPrintfV, pvUser, "<not-string: uTag=%#x>", pAttrib->Value.u.Core.uTag);
        }
    }
}


static const char *rtCrX509CertPathsNodeGetSourceName(PRTCRX509CERTPATHNODE pNode)
{
    switch (pNode->uSrc)
    {
        case RTCRX509CERTPATHNODE_SRC_TARGET:           return "target";
        case RTCRX509CERTPATHNODE_SRC_UNTRUSTED_SET:    return "untrusted_set";
        case RTCRX509CERTPATHNODE_SRC_UNTRUSTED_ARRAY:  return "untrusted_array";
        case RTCRX509CERTPATHNODE_SRC_UNTRUSTED_STORE:  return "untrusted_store";
        case RTCRX509CERTPATHNODE_SRC_TRUSTED_STORE:    return "trusted_store";
        case RTCRX509CERTPATHNODE_SRC_TRUSTED_CERT:     return "trusted_cert";
        default:                                        return "invalid";
    }
}


static void rtCrX509CertPathsDumpOneWorker(PRTCRX509CERTPATHSINT pThis, uint32_t iPath, PRTCRX509CERTPATHNODE pCurLeaf,
                                           uint32_t uVerbosity, PFNRTDUMPPRINTFV pfnPrintfV, void *pvUser)
{
    RT_NOREF_PV(pThis);
    rtDumpPrintf(pfnPrintfV, pvUser, "Path #%u: %s, %u deep, rcVerify=%Rrc\n",
                 iPath, RTCRX509CERTPATHNODE_SRC_IS_TRUSTED(pCurLeaf->uSrc) ? "trusted" : "untrusted", pCurLeaf->uDepth,
                 pCurLeaf->rcVerify);

    for (uint32_t iIndent = 2; pCurLeaf; iIndent += 2, pCurLeaf = pCurLeaf->pParent)
    {
        if (pCurLeaf->pCert)
        {
            rtDumpIndent(pfnPrintfV, pvUser, iIndent, "Issuer : ");
            rtCrX509NameDump(&pCurLeaf->pCert->TbsCertificate.Issuer, pfnPrintfV, pvUser);
            rtDumpPrintf(pfnPrintfV, pvUser, "\n");

            rtDumpIndent(pfnPrintfV, pvUser, iIndent, "Subject: ");
            rtCrX509NameDump(&pCurLeaf->pCert->TbsCertificate.Subject, pfnPrintfV, pvUser);
            rtDumpPrintf(pfnPrintfV, pvUser, "\n");

            if (uVerbosity >= 4)
                RTAsn1Dump(&pCurLeaf->pCert->SeqCore.Asn1Core, 0, iIndent, pfnPrintfV, pvUser);
            else if (uVerbosity >= 3)
                RTAsn1Dump(&pCurLeaf->pCert->TbsCertificate.T3.Extensions.SeqCore.Asn1Core, 0, iIndent, pfnPrintfV, pvUser);

            rtDumpIndent(pfnPrintfV, pvUser, iIndent, "Valid  : %s thru %s\n",
                         RTTimeToString(&pCurLeaf->pCert->TbsCertificate.Validity.NotBefore.Time,
                                        pThis->szTmp, sizeof(pThis->szTmp) / 2),
                         RTTimeToString(&pCurLeaf->pCert->TbsCertificate.Validity.NotAfter.Time,
                                        &pThis->szTmp[sizeof(pThis->szTmp) / 2], sizeof(pThis->szTmp) / 2) );
        }
        else
        {
            Assert(pCurLeaf->pCertCtx); Assert(pCurLeaf->pCertCtx->pTaInfo);
            rtDumpIndent(pfnPrintfV, pvUser, iIndent, "Subject: ");
            rtCrX509NameDump(&pCurLeaf->pCertCtx->pTaInfo->CertPath.TaName, pfnPrintfV, pvUser);

            if (uVerbosity >= 4)
                RTAsn1Dump(&pCurLeaf->pCertCtx->pTaInfo->SeqCore.Asn1Core, 0, iIndent, pfnPrintfV, pvUser);
        }

        const char *pszSrc = rtCrX509CertPathsNodeGetSourceName(pCurLeaf);
        rtDumpIndent(pfnPrintfV, pvUser, iIndent, "Source : %s\n", pszSrc);
    }
}


RTDECL(int) RTCrX509CertPathsDumpOne(RTCRX509CERTPATHS hCertPaths, uint32_t iPath, uint32_t uVerbosity,
                                     PFNRTDUMPPRINTFV pfnPrintfV, void *pvUser)
{
    /*
     * Validate the input.
     */
    PRTCRX509CERTPATHSINT pThis = hCertPaths;
    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, VERR_INVALID_HANDLE);
    AssertPtrReturn(pfnPrintfV, VERR_INVALID_POINTER);
    int rc;
    if (iPath < pThis->cPaths)
    {
        PRTCRX509CERTPATHNODE pLeaf = rtCrX509CertPathsGetLeafByIndex(pThis, iPath);
        if (pLeaf)
        {
            rtCrX509CertPathsDumpOneWorker(pThis, iPath, pLeaf, uVerbosity, pfnPrintfV, pvUser);
            rc = VINF_SUCCESS;
        }
        else
            rc = VERR_CR_X509_CERTPATHS_INTERNAL_ERROR;
    }
    else
        rc = VERR_NOT_FOUND;
    return rc;
}


RTDECL(int) RTCrX509CertPathsDumpAll(RTCRX509CERTPATHS hCertPaths, uint32_t uVerbosity, PFNRTDUMPPRINTFV pfnPrintfV, void *pvUser)
{
    /*
     * Validate the input.
     */
    PRTCRX509CERTPATHSINT pThis = hCertPaths;
    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, VERR_INVALID_HANDLE);
    AssertPtrReturn(pfnPrintfV, VERR_INVALID_POINTER);

    /*
     * Dump all the paths.
     */
    rtDumpPrintf(pfnPrintfV, pvUser, "%u paths, rc=%Rrc\n", pThis->cPaths, pThis->rc);
    uint32_t iPath = 0;
    PRTCRX509CERTPATHNODE pCurLeaf, pNextLeaf;
    RTListForEachSafe(&pThis->LeafList, pCurLeaf, pNextLeaf, RTCRX509CERTPATHNODE, ChildListOrLeafEntry)
    {
        rtCrX509CertPathsDumpOneWorker(pThis, iPath, pCurLeaf, uVerbosity, pfnPrintfV, pvUser);
        iPath++;
    }

    return VINF_SUCCESS;
}


/** @} */


/** @name Path Validator Functions.
 * @{
 */


static void *rtCrX509CpvAllocZ(PRTCRX509CERTPATHSINT pThis, size_t cb, const char *pszWhat)
{
    void *pv = RTMemAllocZ(cb);
    if (!pv)
        pThis->rc = RTErrInfoSetF(pThis->pErrInfo, VERR_NO_MEMORY, "Failed to allocate %zu bytes for %s", cb, pszWhat);
    return pv;
}


DECL_NO_INLINE(static, bool) rtCrX509CpvFailed(PRTCRX509CERTPATHSINT pThis, int rc, const char *pszFormat, ...)
{
    va_list va;
    va_start(va, pszFormat);
    pThis->rc = RTErrInfoSetV(pThis->pErrInfo, rc, pszFormat, va);
    va_end(va);
    return false;
}


/**
 * Adds a sequence of excluded sub-trees.
 *
 * Don't waste time optimizing the output if this is supposed to be a union.
 * Unless the path is very long, it's a lot more work to optimize and the result
 * will be the same anyway.
 *
 * @returns success indicator.
 * @param   pThis               The validator instance.
 * @param   pSubtrees           The sequence of sub-trees to add.
 */
static bool rtCrX509CpvAddExcludedSubtrees(PRTCRX509CERTPATHSINT pThis, PCRTCRX509GENERALSUBTREES pSubtrees)
{
    if (((pThis->v.cExcludedSubtrees + 1) & 0xf) == 0)
    {
        void *pvNew = RTMemRealloc(pThis->v.papExcludedSubtrees,
                                   (pThis->v.cExcludedSubtrees + 16) * sizeof(pThis->v.papExcludedSubtrees[0]));
        if (RT_UNLIKELY(!pvNew))
            return rtCrX509CpvFailed(pThis, VERR_NO_MEMORY, "Error growing subtrees pointer array to %u elements",
                                     pThis->v.cExcludedSubtrees + 16);
        pThis->v.papExcludedSubtrees = (PCRTCRX509GENERALSUBTREES *)pvNew;
    }
    pThis->v.papExcludedSubtrees[pThis->v.cExcludedSubtrees] = pSubtrees;
    pThis->v.cExcludedSubtrees++;
    return true;
}


/**
 * Checks if a sub-tree is according to RFC-5280.
 *
 * @returns Success indiciator.
 * @param   pThis               The validator instance.
 * @param   pSubtree            The subtree to check.
 */
static bool rtCrX509CpvCheckSubtreeValidity(PRTCRX509CERTPATHSINT pThis, PCRTCRX509GENERALSUBTREE pSubtree)
{
    if (   pSubtree->Base.enmChoice <= RTCRX509GENERALNAMECHOICE_INVALID
        || pSubtree->Base.enmChoice >= RTCRX509GENERALNAMECHOICE_END)
        return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_UNEXP_GENERAL_SUBTREE_CHOICE,
                                 "Unexpected GeneralSubtree choice %#x", pSubtree->Base.enmChoice);

    if (RTAsn1Integer_UnsignedCompareWithU32(&pSubtree->Minimum, 0) != 0)
        return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_UNEXP_GENERAL_SUBTREE_MIN,
                                 "Unexpected GeneralSubtree Minimum value: %#llx",
                                 pSubtree->Minimum.uValue);

    if (RTAsn1Integer_IsPresent(&pSubtree->Maximum))
        return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_UNEXP_GENERAL_SUBTREE_MAX,
                                 "Unexpected GeneralSubtree Maximum value: %#llx",
                                 pSubtree->Maximum.uValue);

    return true;
}


/**
 * Grows the array of permitted sub-trees.
 *
 * @returns success indiciator.
 * @param   pThis               The validator instance.
 * @param   cAdding             The number of subtrees we should grow by
 *                              (relative to the current number of valid
 *                              entries).
 */
static bool rtCrX509CpvGrowPermittedSubtrees(PRTCRX509CERTPATHSINT pThis, uint32_t cAdding)
{
    uint32_t cNew = RT_ALIGN_32(pThis->v.cPermittedSubtrees + cAdding, 16);
    if (cNew > pThis->v.cPermittedSubtreesAlloc)
    {
        if (cNew >= _4K)
            return rtCrX509CpvFailed(pThis, VERR_NO_MEMORY, "Too many permitted subtrees: %u (cur %u)",
                                     cNew, pThis->v.cPermittedSubtrees);
        void *pvNew = RTMemRealloc(pThis->v.papPermittedSubtrees, cNew * sizeof(pThis->v.papPermittedSubtrees[0]));
        if (RT_UNLIKELY(!pvNew))
            return rtCrX509CpvFailed(pThis, VERR_NO_MEMORY, "Error growing subtrees pointer array from %u to %u elements",
                                     pThis->v.cPermittedSubtreesAlloc, cNew);
        pThis->v.papPermittedSubtrees = (PCRTCRX509GENERALSUBTREE *)pvNew;
    }
    return true;
}


/**
 * Adds a sequence of permitted sub-trees.
 *
 * We store reference to each individual sub-tree because we must support
 * intersection calculation.
 *
 * @returns success indiciator.
 * @param   pThis               The validator instance.
 * @param   cSubtrees           The number of sub-trees to add.
 * @param   papSubtrees         Array of sub-trees to add.
 */
static bool rtCrX509CpvAddPermittedSubtrees(PRTCRX509CERTPATHSINT pThis, uint32_t cSubtrees,
                                            PRTCRX509GENERALSUBTREE const *papSubtrees)
{
    /*
     * If the array is empty, assume no permitted names.
     */
    if (!cSubtrees)
    {
        pThis->v.fNoPermittedSubtrees = true;
        return true;
    }

    /*
     * Grow the array if necessary.
     */
    if (!rtCrX509CpvGrowPermittedSubtrees(pThis, cSubtrees))
        return false;

    /*
     * Append each subtree to the array.
     */
    uint32_t iDst = pThis->v.cPermittedSubtrees;
    for (uint32_t iSrc = 0; iSrc < cSubtrees; iSrc++)
    {
        if (!rtCrX509CpvCheckSubtreeValidity(pThis, papSubtrees[iSrc]))
            return false;
        pThis->v.papPermittedSubtrees[iDst] = papSubtrees[iSrc];
        iDst++;
    }
    pThis->v.cPermittedSubtrees = iDst;

    return true;
}


/**
 * Adds a one permitted sub-tree.
 *
 * We store reference to each individual sub-tree because we must support
 * intersection calculation.
 *
 * @returns success indiciator.
 * @param   pThis               The validator instance.
 * @param   pSubtree            Array of sub-trees to add.
 */
static bool rtCrX509CpvAddPermittedSubtree(PRTCRX509CERTPATHSINT pThis, PCRTCRX509GENERALSUBTREE pSubtree)
{
    return rtCrX509CpvAddPermittedSubtrees(pThis, 1, (PRTCRX509GENERALSUBTREE const *)&pSubtree);
}


/**
 * Calculates the intersection between @a pSubtrees and the current permitted
 * sub-trees.
 *
 * @returns Success indicator.
 * @param   pThis               The validator instance.
 * @param   pSubtrees           The sub-tree sequence to intersect with.
 */
static bool rtCrX509CpvIntersectionPermittedSubtrees(PRTCRX509CERTPATHSINT pThis, PCRTCRX509GENERALSUBTREES pSubtrees)
{
    /*
     * Deal with special cases first.
     */
    if (pThis->v.fNoPermittedSubtrees)
    {
        Assert(pThis->v.cPermittedSubtrees == 0);
        return true;
    }

    uint32_t                       cRight   = pSubtrees->cItems;
    PRTCRX509GENERALSUBTREE const *papRight = pSubtrees->papItems;
    if (cRight == 0)
    {
        pThis->v.cPermittedSubtrees = 0;
        pThis->v.fNoPermittedSubtrees = true;
        return true;
    }

    uint32_t                    cLeft   = pThis->v.cPermittedSubtrees;
    PCRTCRX509GENERALSUBTREE   *papLeft = pThis->v.papPermittedSubtrees;
    if (!cLeft) /* first name constraint, no initial constraint */
        return rtCrX509CpvAddPermittedSubtrees(pThis, cRight, papRight);

    /*
     * Create a new array with the intersection, freeing the old (left) array
     * once we're done.
     */
    bool afRightTags[RTCRX509GENERALNAMECHOICE_END] = { 0, 0, 0, 0,  0, 0, 0, 0,  0 };

    pThis->v.cPermittedSubtrees      = 0;
    pThis->v.cPermittedSubtreesAlloc = 0;
    pThis->v.papPermittedSubtrees    = NULL;

    for (uint32_t iRight = 0; iRight < cRight; iRight++)
    {
        if (!rtCrX509CpvCheckSubtreeValidity(pThis, papRight[iRight]))
            return false;

        RTCRX509GENERALNAMECHOICE const enmRightChoice = papRight[iRight]->Base.enmChoice;
        afRightTags[enmRightChoice] = true;

        bool fHaveRight = false;
        for (uint32_t iLeft = 0; iLeft < cLeft; iLeft++)
            if (papLeft[iLeft]->Base.enmChoice == enmRightChoice)
            {
                if (RTCrX509GeneralSubtree_Compare(papLeft[iLeft], papRight[iRight]) == 0)
                {
                    if (!fHaveRight)
                    {
                        fHaveRight = true;
                        rtCrX509CpvAddPermittedSubtree(pThis, papLeft[iLeft]);
                    }
                }
                else if (RTCrX509GeneralSubtree_ConstraintMatch(papLeft[iLeft], papRight[iRight]))
                {
                    if (!fHaveRight)
                    {
                        fHaveRight = true;
                        rtCrX509CpvAddPermittedSubtree(pThis, papRight[iRight]);
                    }
                }
                else if (RTCrX509GeneralSubtree_ConstraintMatch(papRight[iRight], papLeft[iLeft]))
                    rtCrX509CpvAddPermittedSubtree(pThis, papLeft[iLeft]);
            }
    }

    /*
     * Add missing types not specified in the right set.
     */
    for (uint32_t iLeft = 0; iLeft < cLeft; iLeft++)
        if (!afRightTags[papLeft[iLeft]->Base.enmChoice])
            rtCrX509CpvAddPermittedSubtree(pThis, papLeft[iLeft]);

    /*
     * If we ended up with an empty set, no names are permitted any more.
     */
    if (pThis->v.cPermittedSubtrees == 0)
        pThis->v.fNoPermittedSubtrees = true;

    RTMemFree(papLeft);
    return RT_SUCCESS(pThis->rc);
}


/**
 * Check if the given X.509 name is permitted by current name constraints.
 *
 * @returns true is permitteded, false if not (caller set error info).
 * @param   pThis       The validator instance.
 * @param   pName       The name to match.
 */
static bool rtCrX509CpvIsNamePermitted(PRTCRX509CERTPATHSINT pThis, PCRTCRX509NAME pName)
{
    uint32_t i = pThis->v.cPermittedSubtrees;
    if (i == 0)
        return !pThis->v.fNoPermittedSubtrees;

    while (i-- > 0)
    {
        PCRTCRX509GENERALSUBTREE pConstraint = pThis->v.papPermittedSubtrees[i];
        if (   RTCRX509GENERALNAME_IS_DIRECTORY_NAME(&pConstraint->Base)
            && RTCrX509Name_ConstraintMatch(&pConstraint->Base.u.pT4->DirectoryName, pName))
            return true;
    }
    return false;
}


/**
 * Check if the given X.509 general name is permitted by current name
 * constraints.
 *
 * @returns true is permitteded, false if not (caller sets error info).
 * @param   pThis           The validator instance.
 * @param   pGeneralName    The name to match.
 */
static bool rtCrX509CpvIsGeneralNamePermitted(PRTCRX509CERTPATHSINT pThis, PCRTCRX509GENERALNAME pGeneralName)
{
    uint32_t i = pThis->v.cPermittedSubtrees;
    if (i == 0)
        return !pThis->v.fNoPermittedSubtrees;

    while (i-- > 0)
        if (RTCrX509GeneralName_ConstraintMatch(&pThis->v.papPermittedSubtrees[i]->Base, pGeneralName))
            return true;
    return false;
}


/**
 * Check if the given X.509 name is excluded by current name constraints.
 *
 * @returns true if excluded (caller sets error info), false if not explicitly
 *          excluded.
 * @param   pThis       The validator instance.
 * @param   pName       The name to match.
 */
static bool rtCrX509CpvIsNameExcluded(PRTCRX509CERTPATHSINT pThis, PCRTCRX509NAME pName)
{
    uint32_t i = pThis->v.cExcludedSubtrees;
    while (i-- > 0)
    {
        PCRTCRX509GENERALSUBTREES pSubTrees = pThis->v.papExcludedSubtrees[i];
        uint32_t j = pSubTrees->cItems;
        while (j-- > 0)
        {
            PCRTCRX509GENERALSUBTREE const pSubTree = pSubTrees->papItems[j];
            if (   RTCRX509GENERALNAME_IS_DIRECTORY_NAME(&pSubTree->Base)
                && RTCrX509Name_ConstraintMatch(&pSubTree->Base.u.pT4->DirectoryName, pName))
                return true;
        }
    }
    return false;
}


/**
 * Check if the given X.509 general name is excluded by current name
 * constraints.
 *
 * @returns true if excluded (caller sets error info), false if not explicitly
 *          excluded.
 * @param   pThis           The validator instance.
 * @param   pGeneralName    The name to match.
 */
static bool rtCrX509CpvIsGeneralNameExcluded(PRTCRX509CERTPATHSINT pThis, PCRTCRX509GENERALNAME pGeneralName)
{
    uint32_t i = pThis->v.cExcludedSubtrees;
    while (i-- > 0)
    {
        PCRTCRX509GENERALSUBTREES pSubTrees = pThis->v.papExcludedSubtrees[i];
        uint32_t j = pSubTrees->cItems;
        while (j-- > 0)
            if (RTCrX509GeneralName_ConstraintMatch(&pSubTrees->papItems[j]->Base, pGeneralName))
                return true;
    }
    return false;
}


/**
 * Creates a new node and inserts it.
 *
 * @param   pThis           The path builder & validator instance.
 * @param   pParent         The parent node. NULL for the root node.
 * @param   iDepth          The tree depth to insert at.
 * @param   pValidPolicy    The valid policy of the new node.
 * @param   pQualifiers     The qualifiers of the new node.
 * @param   pExpectedPolicy The (first) expected polcy of the new node.
 */
static bool rtCrX509CpvPolicyTreeInsertNew(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHSPOLICYNODE pParent, uint32_t iDepth,
                                           PCRTASN1OBJID pValidPolicy, PCRTCRX509POLICYQUALIFIERINFOS pQualifiers,
                                           PCRTASN1OBJID pExpectedPolicy)
{
    Assert(iDepth <= pThis->v.cNodes);

    PRTCRX509CERTPATHSPOLICYNODE pNode;
    pNode = (PRTCRX509CERTPATHSPOLICYNODE)rtCrX509CpvAllocZ(pThis, sizeof(*pNode), "policy tree node");
    if (pNode)
    {
        pNode->pParent = pParent;
        if (pParent)
            RTListAppend(&pParent->ChildList, &pNode->SiblingEntry);
        else
        {
            Assert(pThis->v.pValidPolicyTree == NULL);
            pThis->v.pValidPolicyTree = pNode;
            RTListInit(&pNode->SiblingEntry);
        }
        RTListInit(&pNode->ChildList);
        RTListAppend(&pThis->v.paValidPolicyDepthLists[iDepth], &pNode->DepthEntry);

        pNode->pValidPolicy = pValidPolicy;
        pNode->pPolicyQualifiers = pQualifiers;
        pNode->pExpectedPolicyFirst = pExpectedPolicy;
        pNode->cMoreExpectedPolicySet = 0;
        pNode->papMoreExpectedPolicySet = NULL;
        return true;
    }
    return false;
}


/**
 * Unlinks and frees a node in the valid policy tree.
 *
 * @param   pThis           The path builder & validator instance.
 * @param   pNode           The node to destroy.
 */
static void rtCrX509CpvPolicyTreeDestroyNode(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHSPOLICYNODE pNode)
{
    Assert(RTListIsEmpty(&pNode->ChildList));
    if (pNode->pParent)
        RTListNodeRemove(&pNode->SiblingEntry);
    else
        pThis->v.pValidPolicyTree = NULL;
    RTListNodeRemove(&pNode->DepthEntry);
    pNode->pParent = NULL;

    if (pNode->papMoreExpectedPolicySet)
    {
        RTMemFree(pNode->papMoreExpectedPolicySet);
        pNode->papMoreExpectedPolicySet = NULL;
    }
    RTMemFree(pNode);
}


/**
 * Unlinks and frees a sub-tree in the valid policy tree.
 *
 * @param   pThis           The path builder & validator instance.
 * @param   pNode           The node that is the root of the subtree.
 */
static void rtCrX509CpvPolicyTreeDestroySubtree(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHSPOLICYNODE pNode)
{
    if (!RTListIsEmpty(&pNode->ChildList))
    {
        PRTCRX509CERTPATHSPOLICYNODE pCur = pNode;
        do
        {
            Assert(!RTListIsEmpty(&pCur->ChildList));

            /* Decend until we find a leaf. */
            do
                pCur = RTListGetFirst(&pCur->ChildList, RTCRX509CERTPATHSPOLICYNODE, SiblingEntry);
            while (!RTListIsEmpty(&pCur->ChildList));

            /* Remove it and all leafy siblings. */
            PRTCRX509CERTPATHSPOLICYNODE pParent = pCur->pParent;
            do
            {
                Assert(pCur != pNode);
                rtCrX509CpvPolicyTreeDestroyNode(pThis, pCur);
                pCur = RTListGetFirst(&pParent->ChildList, RTCRX509CERTPATHSPOLICYNODE, SiblingEntry);
                if (!pCur)
                {
                    pCur = pParent;
                    pParent = pParent->pParent;
                }
            } while (RTListIsEmpty(&pCur->ChildList) && pCur != pNode);
        } while (pCur != pNode);
    }

    rtCrX509CpvPolicyTreeDestroyNode(pThis, pNode);
}



/**
 * Destroys the entire policy tree.
 *
 * @param   pThis           The path builder & validator instance.
 */
static void rtCrX509CpvPolicyTreeDestroy(PRTCRX509CERTPATHSINT pThis)
{
    uint32_t i = pThis->v.cNodes + 1;
    while (i-- > 0)
    {
        PRTCRX509CERTPATHSPOLICYNODE pCur, pNext;
        RTListForEachSafe(&pThis->v.paValidPolicyDepthLists[i], pCur, pNext, RTCRX509CERTPATHSPOLICYNODE, DepthEntry)
        {
            rtCrX509CpvPolicyTreeDestroyNode(pThis, pCur);
        }
    }
}


/**
 * Removes all leaf nodes at level @a iDepth and above.
 *
 * @param   pThis           The path builder & validator instance.
 * @param   iDepth          The depth to start pruning at.
 */
static void rtCrX509CpvPolicyTreePrune(PRTCRX509CERTPATHSINT pThis, uint32_t iDepth)
{
    do
    {
        PRTLISTANCHOR pList = &pThis->v.paValidPolicyDepthLists[iDepth];
        PRTCRX509CERTPATHSPOLICYNODE pCur, pNext;
        RTListForEachSafe(pList, pCur, pNext, RTCRX509CERTPATHSPOLICYNODE, DepthEntry)
        {
            if (RTListIsEmpty(&pCur->ChildList))
                rtCrX509CpvPolicyTreeDestroyNode(pThis, pCur);
        }

    } while (iDepth-- > 0);
}


/**
 * Checks if @a pPolicy is the valid policy of a child of @a pNode.
 *
 * @returns true if in child node, false if not.
 * @param   pNode           The node which children to check.
 * @param   pPolicy         The valid policy to look for among the children.
 */
static bool rtCrX509CpvPolicyTreeIsChild(PRTCRX509CERTPATHSPOLICYNODE pNode, PCRTASN1OBJID pPolicy)
{
    PRTCRX509CERTPATHSPOLICYNODE pChild;
    RTListForEach(&pNode->ChildList, pChild, RTCRX509CERTPATHSPOLICYNODE, SiblingEntry)
    {
        if (RTAsn1ObjId_Compare(pChild->pValidPolicy, pPolicy) == 0)
            return true;
    }
    return true;
}


/**
 * Prunes the valid policy tree according to the specified user policy set.
 *
 * @returns Pointer to the policy object from @a papPolicies if found, NULL if
 *          no match.
 * @param   pObjId          The object ID to locate at match in the set.
 * @param   cPolicies       The number of policies in @a papPolicies.
 * @param   papPolicies     The policy set to search.
 */
static PCRTASN1OBJID rtCrX509CpvFindObjIdInPolicySet(PCRTASN1OBJID pObjId, uint32_t cPolicies, PCRTASN1OBJID *papPolicies)
{
    uint32_t i = cPolicies;
    while (i-- > 0)
        if (RTAsn1ObjId_Compare(pObjId, papPolicies[i]) == 0)
            return papPolicies[i];
    return NULL;
}


/**
 * Prunes the valid policy tree according to the specified user policy set.
 *
 * @returns success indicator (allocates memory)
 * @param   pThis           The path builder & validator instance.
 * @param   cPolicies       The number of policies in @a papPolicies.
 * @param   papPolicies     The user initial policies.
 */
static bool rtCrX509CpvPolicyTreeIntersect(PRTCRX509CERTPATHSINT pThis, uint32_t cPolicies, PCRTASN1OBJID *papPolicies)
{
    /*
     * 4.1.6.g.i - NULL tree remains NULL.
     */
    if (!pThis->v.pValidPolicyTree)
        return true;

    /*
     * 4.1.6.g.ii - If the user set includes anyPolicy, the whole tree is the
     *              result of the intersection.
     */
    uint32_t i = cPolicies;
    while (i-- > 0)
        if (RTAsn1ObjId_CompareWithString(papPolicies[i], RTCRX509_ID_CE_CP_ANY_POLICY_OID) == 0)
            return true;

    /*
     * 4.1.6.g.iii - Complicated.
     */
    PRTCRX509CERTPATHSPOLICYNODE pCur, pNext;
    PRTLISTANCHOR pList;

    /* 1 & 2: Delete nodes which parent has valid policy == anyPolicy and which
              valid policy is neither anyPolicy nor a member of papszPolicies.
              While doing so, construct a set of unused user policies that
              we'll replace anyPolicy nodes with in step 3. */
    uint32_t        cPoliciesLeft   = 0;
    PCRTASN1OBJID  *papPoliciesLeft = NULL;
    if (cPolicies)
    {
        papPoliciesLeft = (PCRTASN1OBJID *)rtCrX509CpvAllocZ(pThis, cPolicies * sizeof(papPoliciesLeft[0]), "papPoliciesLeft");
        if (!papPoliciesLeft)
            return false;
        for (i = 0; i < cPolicies; i++)
            papPoliciesLeft[i] = papPolicies[i];
    }

    for (uint32_t iDepth = 1; iDepth <= pThis->v.cNodes; iDepth++)
    {
        pList = &pThis->v.paValidPolicyDepthLists[iDepth];
        RTListForEachSafe(pList, pCur, pNext, RTCRX509CERTPATHSPOLICYNODE, DepthEntry)
        {
            Assert(pCur->pParent);
            if (   RTAsn1ObjId_CompareWithString(pCur->pParent->pValidPolicy, RTCRX509_ID_CE_CP_ANY_POLICY_OID) == 0
                && RTAsn1ObjId_CompareWithString(pCur->pValidPolicy, RTCRX509_ID_CE_CP_ANY_POLICY_OID) != 0)
            {
                PCRTASN1OBJID pFound = rtCrX509CpvFindObjIdInPolicySet(pCur->pValidPolicy, cPolicies, papPolicies);
                if (!pFound)
                    rtCrX509CpvPolicyTreeDestroySubtree(pThis, pCur);
                else
                    for (i = 0; i < cPoliciesLeft; i++)
                        if (papPoliciesLeft[i] == pFound)
                        {
                            cPoliciesLeft--;
                            if (i < cPoliciesLeft)
                                papPoliciesLeft[i] = papPoliciesLeft[cPoliciesLeft];
                            papPoliciesLeft[cPoliciesLeft] = NULL;
                            break;
                        }
            }
        }
    }

    /*
     * 4.1.5.g.iii.3 - Replace anyPolicy nodes on the final tree depth with
     *                 the policies in papPoliciesLeft.
     */
    pList = &pThis->v.paValidPolicyDepthLists[pThis->v.cNodes];
    RTListForEachSafe(pList, pCur, pNext, RTCRX509CERTPATHSPOLICYNODE, DepthEntry)
    {
        if (RTAsn1ObjId_CompareWithString(pCur->pValidPolicy, RTCRX509_ID_CE_CP_ANY_POLICY_OID) == 0)
        {
            for (i = 0; i < cPoliciesLeft; i++)
                rtCrX509CpvPolicyTreeInsertNew(pThis, pCur->pParent, pThis->v.cNodes - 1,
                                               papPoliciesLeft[i], pCur->pPolicyQualifiers, papPoliciesLeft[i]);
            rtCrX509CpvPolicyTreeDestroyNode(pThis, pCur);
        }
    }

    RTMemFree(papPoliciesLeft);

    /*
     * 4.1.5.g.iii.4 - Prune the tree
     */
    rtCrX509CpvPolicyTreePrune(pThis, pThis->v.cNodes - 1);

    return RT_SUCCESS(pThis->rc);
}



/**
 * Frees the path validator state.
 *
 * @param   pThis           The path builder & validator instance.
 */
static void rtCrX509CpvCleanup(PRTCRX509CERTPATHSINT pThis)
{
    /*
     * Destroy the policy tree and all its nodes.  We do this from the bottom
     * up via the depth lists, saving annoying tree traversal.
     */
    if (pThis->v.paValidPolicyDepthLists)
    {
        rtCrX509CpvPolicyTreeDestroy(pThis);

        RTMemFree(pThis->v.paValidPolicyDepthLists);
        pThis->v.paValidPolicyDepthLists = NULL;
    }

    Assert(pThis->v.pValidPolicyTree == NULL);
    pThis->v.pValidPolicyTree = NULL;

    /*
     * Destroy the name constraint arrays.
     */
    if (pThis->v.papPermittedSubtrees)
    {
        RTMemFree(pThis->v.papPermittedSubtrees);
        pThis->v.papPermittedSubtrees = NULL;
    }
    pThis->v.cPermittedSubtrees = 0;
    pThis->v.cPermittedSubtreesAlloc = 0;
    pThis->v.fNoPermittedSubtrees = false;

    if (pThis->v.papExcludedSubtrees)
    {
        RTMemFree(pThis->v.papExcludedSubtrees);
        pThis->v.papExcludedSubtrees = NULL;
    }
    pThis->v.cExcludedSubtrees = 0;

    /*
     * Clear other pointers.
     */
    pThis->v.pWorkingIssuer              = NULL;
    pThis->v.pWorkingPublicKey           = NULL;
    pThis->v.pWorkingPublicKeyAlgorithm  = NULL;
    pThis->v.pWorkingPublicKeyParameters = NULL;
}


/**
 * Initializes the state.
 *
 * Caller must check pThis->rc.
 *
 * @param   pThis           The path builder & validator instance.
 * @param   pTrustAnchor    The trust anchor node for the path that we're about
 *                          to validate.
 */
static void rtCrX509CpvInit(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHNODE pTrustAnchor)
{
    rtCrX509CpvCleanup(pThis);

    /*
     * The node count does not include the trust anchor.
     */
    pThis->v.cNodes = pTrustAnchor->uDepth;

    /*
     * Valid policy tree starts with an anyPolicy node.
     */
    uint32_t i = pThis->v.cNodes + 1;
    pThis->v.paValidPolicyDepthLists = (PRTLISTANCHOR)rtCrX509CpvAllocZ(pThis, i * sizeof(RTLISTANCHOR),
                                                                        "paValidPolicyDepthLists");
    if (RT_UNLIKELY(!pThis->v.paValidPolicyDepthLists))
        return;
    while (i-- > 0)
        RTListInit(&pThis->v.paValidPolicyDepthLists[i]);

    if (!rtCrX509CpvPolicyTreeInsertNew(pThis, NULL, 0 /* iDepth*/, &pThis->AnyPolicyObjId, NULL, &pThis->AnyPolicyObjId))
        return;
    Assert(!RTListIsEmpty(&pThis->v.paValidPolicyDepthLists[0])); Assert(pThis->v.pValidPolicyTree);

    /*
     * Name constrains.
     */
    if (pThis->pInitialPermittedSubtrees)
        rtCrX509CpvAddPermittedSubtrees(pThis, pThis->pInitialPermittedSubtrees->cItems,
                                        pThis->pInitialPermittedSubtrees->papItems);
    if (pThis->pInitialExcludedSubtrees)
        rtCrX509CpvAddExcludedSubtrees(pThis, pThis->pInitialExcludedSubtrees);

    /*
     * Counters.
     */
    pThis->v.cExplicitPolicy        = pThis->cInitialExplicitPolicy;
    pThis->v.cInhibitPolicyMapping  = pThis->cInitialPolicyMappingInhibit;
    pThis->v.cInhibitAnyPolicy      = pThis->cInitialInhibitAnyPolicy;
    pThis->v.cMaxPathLength         = pThis->v.cNodes;

    /*
     * Certificate info from the trust anchor.
     */
    if (pTrustAnchor->pCert)
    {
        PCRTCRX509TBSCERTIFICATE const pTbsCert = &pTrustAnchor->pCert->TbsCertificate;
        pThis->v.pWorkingIssuer                 = &pTbsCert->Subject;
        pThis->v.pWorkingPublicKey              = &pTbsCert->SubjectPublicKeyInfo.SubjectPublicKey;
        pThis->v.pWorkingPublicKeyAlgorithm     = &pTbsCert->SubjectPublicKeyInfo.Algorithm.Algorithm;
        pThis->v.pWorkingPublicKeyParameters    = &pTbsCert->SubjectPublicKeyInfo.Algorithm.Parameters;
    }
    else
    {
        Assert(pTrustAnchor->pCertCtx); Assert(pTrustAnchor->pCertCtx->pTaInfo);

        PCRTCRTAFTRUSTANCHORINFO const  pTaInfo = pTrustAnchor->pCertCtx->pTaInfo;
        pThis->v.pWorkingIssuer                 = &pTaInfo->CertPath.TaName;
        pThis->v.pWorkingPublicKey              = &pTaInfo->PubKey.SubjectPublicKey;
        pThis->v.pWorkingPublicKeyAlgorithm     = &pTaInfo->PubKey.Algorithm.Algorithm;
        pThis->v.pWorkingPublicKeyParameters    = &pTaInfo->PubKey.Algorithm.Parameters;
    }
    if (   !RTASN1CORE_IS_PRESENT(&pThis->v.pWorkingPublicKeyParameters->u.Core)
        || pThis->v.pWorkingPublicKeyParameters->enmType == RTASN1TYPE_NULL)
        pThis->v.pWorkingPublicKeyParameters = NULL;
}


/**
 * This does basic trust anchor checks (similar to 6.1.3.a) before starting on
 * the RFC-5280 algorithm.
 */
static bool rtCrX509CpvMaybeCheckTrustAnchor(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHNODE pTrustAnchor)
{
    /*
     * This is optional (not part of RFC-5280) and we need a full certificate
     * structure to do it.
     */
    if (!(pThis->fFlags & RTCRX509CERTPATHSINT_F_CHECK_TRUST_ANCHOR))
        return true;

    PCRTCRX509CERTIFICATE const pCert = pTrustAnchor->pCert;
    if (!pCert)
        return true;

    /*
     * Verify the certificate signature if self-signed.
     */
    if (RTCrX509Certificate_IsSelfSigned(pCert))
    {
        int rc = RTCrX509Certificate_VerifySignature(pCert, pThis->v.pWorkingPublicKeyAlgorithm,
                                                     pThis->v.pWorkingPublicKeyParameters, pThis->v.pWorkingPublicKey,
                                                     pThis->pErrInfo);
        if (RT_FAILURE(rc))
        {
            pThis->rc = rc;
            return false;
        }
    }

    /*
     * Verify that the certificate is valid at the specified time.
     */
    AssertCompile(sizeof(pThis->szTmp) >= 36 * 3);
    if (   (pThis->fFlags & RTCRX509CERTPATHSINT_F_VALID_TIME)
        && !RTCrX509Validity_IsValidAtTimeSpec(&pCert->TbsCertificate.Validity, &pThis->ValidTime))
        return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_NOT_VALID_AT_TIME,
                                 "Certificate is not valid (ValidTime=%s Validity=[%s...%s])",
                                 RTTimeSpecToString(&pThis->ValidTime, &pThis->szTmp[0], 36),
                                 RTTimeToString(&pCert->TbsCertificate.Validity.NotBefore.Time, &pThis->szTmp[36], 36),
                                 RTTimeToString(&pCert->TbsCertificate.Validity.NotAfter.Time,  &pThis->szTmp[2*36], 36) );

    /*
     * Verified that the certficiate is not revoked.
     */
    /** @todo rainy day. */

    /*
     * If non-leaf certificate CA must be set, if basic constraints are present.
     */
    if (pTrustAnchor->pParent)
    {
        if (RTAsn1Integer_UnsignedCompareWithU32(&pTrustAnchor->pCert->TbsCertificate.T0.Version, RTCRX509TBSCERTIFICATE_V3) != 0)
            return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_NOT_V3_CERT,
                                     "Only version 3 TA certificates are supported (Version=%llu)",
                                     pTrustAnchor->pCert->TbsCertificate.T0.Version.uValue);
        PCRTCRX509BASICCONSTRAINTS pBasicConstraints = pTrustAnchor->pCert->TbsCertificate.T3.pBasicConstraints;
        if (pBasicConstraints && !pBasicConstraints->CA.fValue)
            return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_NOT_CA_CERT,
                                     "Trust anchor certificate is not marked as a CA");
    }

    return true;
}


/**
 * Step 6.1.3.a.
 */
static bool rtCrX509CpvCheckBasicCertInfo(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHNODE pNode)
{
    /*
     * 6.1.3.a.1 - Verify the certificate signature.
     */
    int rc = RTCrX509Certificate_VerifySignature(pNode->pCert, pThis->v.pWorkingPublicKeyAlgorithm,
                                                 pThis->v.pWorkingPublicKeyParameters, pThis->v.pWorkingPublicKey,
                                                 pThis->pErrInfo);
    if (RT_FAILURE(rc))
    {
        pThis->rc = rc;
        return false;
    }

    /*
     * 6.1.3.a.2 - Verify that the certificate is valid at the specified time.
     */
    AssertCompile(sizeof(pThis->szTmp) >= 36 * 3);
    if (   (pThis->fFlags & RTCRX509CERTPATHSINT_F_VALID_TIME)
        && !RTCrX509Validity_IsValidAtTimeSpec(&pNode->pCert->TbsCertificate.Validity, &pThis->ValidTime))
        return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_NOT_VALID_AT_TIME,
                                 "Certificate is not valid (ValidTime=%s Validity=[%s...%s])",
                                 RTTimeSpecToString(&pThis->ValidTime, &pThis->szTmp[0], 36),
                                 RTTimeToString(&pNode->pCert->TbsCertificate.Validity.NotBefore.Time, &pThis->szTmp[36], 36),
                                 RTTimeToString(&pNode->pCert->TbsCertificate.Validity.NotAfter.Time,  &pThis->szTmp[2*36], 36) );

    /*
     * 6.1.3.a.3 - Verified that the certficiate is not revoked.
     */
    /** @todo rainy day. */

    /*
     * 6.1.3.a.4 - Check the issuer name.
     */
    if (!RTCrX509Name_MatchByRfc5280(&pNode->pCert->TbsCertificate.Issuer, pThis->v.pWorkingIssuer))
        return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_ISSUER_MISMATCH, "Issuer mismatch");

    return true;
}


/**
 * Step 6.1.3.b-c.
 */
static bool rtCrX509CpvCheckNameConstraints(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHNODE pNode)
{
    if (pThis->v.fNoPermittedSubtrees)
        return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_NO_PERMITTED_NAMES, "No permitted subtrees");

    if (   pNode->pCert->TbsCertificate.Subject.cItems > 0
        && (   !rtCrX509CpvIsNamePermitted(pThis, &pNode->pCert->TbsCertificate.Subject)
            || rtCrX509CpvIsNameExcluded(pThis, &pNode->pCert->TbsCertificate.Subject)) )
        return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_NAME_NOT_PERMITTED,
                                 "Subject name is not permitted by current name constraints");

    PCRTCRX509GENERALNAMES pAltSubjectName = pNode->pCert->TbsCertificate.T3.pAltSubjectName;
    if (pAltSubjectName)
    {
        uint32_t i = pAltSubjectName->cItems;
        while (i-- > 0)
            if (   !rtCrX509CpvIsGeneralNamePermitted(pThis, pAltSubjectName->papItems[i])
                || rtCrX509CpvIsGeneralNameExcluded(pThis, pAltSubjectName->papItems[i]))
                return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_ALT_NAME_NOT_PERMITTED,
                                         "Alternative name #%u is is not permitted by current name constraints", i);
    }

    return true;
}


/**
 * Step 6.1.3.d-f.
 */
static bool rtCrX509CpvWorkValidPolicyTree(PRTCRX509CERTPATHSINT pThis, uint32_t iDepth, PRTCRX509CERTPATHNODE pNode,
                                           bool fSelfIssued)
{
    PCRTCRX509CERTIFICATEPOLICIES pPolicies = pNode->pCert->TbsCertificate.T3.pCertificatePolicies;
    if (pPolicies)
    {
        /*
         * 6.1.3.d.1 - Work the certiciate policies into the tree.
         */
        PRTCRX509CERTPATHSPOLICYNODE    pCur;
        PRTLISTANCHOR                   pListAbove  = &pThis->v.paValidPolicyDepthLists[iDepth - 1];
        uint32_t                        iAnyPolicy  = UINT32_MAX;
        uint32_t                        i           = pPolicies->cItems;
        while (i-- > 0)
        {
            PCRTCRX509POLICYQUALIFIERINFOS const    pQualifiers = &pPolicies->papItems[i]->PolicyQualifiers;
            PCRTASN1OBJID const                     pIdP        = &pPolicies->papItems[i]->PolicyIdentifier;
            if (RTAsn1ObjId_CompareWithString(pIdP, RTCRX509_ID_CE_CP_ANY_POLICY_OID) == 0)
            {
                iAnyPolicy++;
                continue;
            }

            /*
             * 6.1.3.d.1.i - Create children for matching policies.
             */
            uint32_t cMatches = 0;
            RTListForEach(pListAbove, pCur, RTCRX509CERTPATHSPOLICYNODE, DepthEntry)
            {
                bool fMatch = RTAsn1ObjId_Compare(pCur->pExpectedPolicyFirst, pIdP) == 0;
                if (!fMatch && pCur->cMoreExpectedPolicySet)
                    for (uint32_t j = 0; !fMatch && j < pCur->cMoreExpectedPolicySet; j++)
                        fMatch = RTAsn1ObjId_Compare(pCur->papMoreExpectedPolicySet[j], pIdP) == 0;
                if (fMatch)
                {
                    if (!rtCrX509CpvPolicyTreeInsertNew(pThis, pCur, iDepth, pIdP, pQualifiers, pIdP))
                        return false;
                    cMatches++;
                }
            }

            /*
             * 6.1.3.d.1.ii - If no matches above do the same for anyPolicy
             *                nodes, only match with valid policy this time.
             */
            if (cMatches == 0)
            {
                RTListForEach(pListAbove, pCur, RTCRX509CERTPATHSPOLICYNODE, DepthEntry)
                {
                    if (RTAsn1ObjId_CompareWithString(pCur->pExpectedPolicyFirst, RTCRX509_ID_CE_CP_ANY_POLICY_OID) == 0)
                    {
                        if (!rtCrX509CpvPolicyTreeInsertNew(pThis, pCur, iDepth, pIdP, pQualifiers, pIdP))
                            return false;
                    }
                }
            }
        }

        /*
         * 6.1.3.d.2 - If anyPolicy present, make sure all expected policies
         *             are propagated to the current depth.
         */
        if (   iAnyPolicy < pPolicies->cItems
            && (   pThis->v.cInhibitAnyPolicy > 0
                || (pNode->pParent && fSelfIssued) ) )
        {
            PCRTCRX509POLICYQUALIFIERINFOS pApQ = &pPolicies->papItems[iAnyPolicy]->PolicyQualifiers;
            RTListForEach(pListAbove, pCur, RTCRX509CERTPATHSPOLICYNODE, DepthEntry)
            {
                if (!rtCrX509CpvPolicyTreeIsChild(pCur, pCur->pExpectedPolicyFirst))
                    rtCrX509CpvPolicyTreeInsertNew(pThis, pCur, iDepth, pCur->pExpectedPolicyFirst, pApQ,
                                                   pCur->pExpectedPolicyFirst);
                for (uint32_t j = 0; j < pCur->cMoreExpectedPolicySet; j++)
                    if (!rtCrX509CpvPolicyTreeIsChild(pCur, pCur->papMoreExpectedPolicySet[j]))
                        rtCrX509CpvPolicyTreeInsertNew(pThis, pCur, iDepth, pCur->papMoreExpectedPolicySet[j], pApQ,
                                                       pCur->papMoreExpectedPolicySet[j]);
            }
        }
        /*
         * 6.1.3.d.3 - Prune the tree.
         */
        else
            rtCrX509CpvPolicyTreePrune(pThis, iDepth - 1);
    }
    else
    {
        /*
         * 6.1.3.e - No policy extension present, set tree to NULL.
         */
        rtCrX509CpvPolicyTreeDestroy(pThis);
    }

    /*
     * 6.1.3.f - NULL tree check.
     */
    if (   pThis->v.pValidPolicyTree == NULL
        && pThis->v.cExplicitPolicy == 0)
        return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_NO_VALID_POLICY,
                                 "An explicit policy is called for but the valid policy tree is NULL.");
    return RT_SUCCESS(pThis->rc);
}


/**
 * Step 6.1.4.a-b.
 */
static bool rtCrX509CpvSoakUpPolicyMappings(PRTCRX509CERTPATHSINT pThis, uint32_t iDepth,
                                            PCRTCRX509POLICYMAPPINGS pPolicyMappings)
{
    /*
     * 6.1.4.a - The anyPolicy is not allowed in policy mappings as it would
     *           allow an evil intermediate certificate to expand the policy
     *           scope of a certiciate chain without regard to upstream.
     */
    uint32_t i = pPolicyMappings->cItems;
    while (i-- > 0)
    {
        PCRTCRX509POLICYMAPPING const pOne = pPolicyMappings->papItems[i];
        if (RTAsn1ObjId_CompareWithString(&pOne->IssuerDomainPolicy, RTCRX509_ID_CE_CP_ANY_POLICY_OID) == 0)
            return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_INVALID_POLICY_MAPPING,
                                     "Invalid policy mapping %#u: IssuerDomainPolicy is anyPolicy.", i);

        if (RTAsn1ObjId_CompareWithString(&pOne->SubjectDomainPolicy, RTCRX509_ID_CE_CP_ANY_POLICY_OID) == 0)
            return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_INVALID_POLICY_MAPPING,
                                     "Invalid policy mapping %#u: SubjectDomainPolicy is anyPolicy.", i);
    }

    PRTCRX509CERTPATHSPOLICYNODE pCur, pNext;
    if (pThis->v.cInhibitPolicyMapping > 0)
    {
        /*
         * 6.1.4.b.1 - Do the policy mapping.
         */
        i = pPolicyMappings->cItems;
        while (i-- > 0)
        {
            PCRTCRX509POLICYMAPPING const pOne = pPolicyMappings->papItems[i];

            uint32_t cFound = 0;
            RTListForEach(&pThis->v.paValidPolicyDepthLists[iDepth], pCur, RTCRX509CERTPATHSPOLICYNODE, DepthEntry)
            {
                if (RTAsn1ObjId_Compare(pCur->pValidPolicy, &pOne->IssuerDomainPolicy))
                {
                    if (!pCur->fAlreadyMapped)
                    {
                        pCur->fAlreadyMapped = true;
                        pCur->pExpectedPolicyFirst = &pOne->SubjectDomainPolicy;
                    }
                    else
                    {
                        uint32_t iExpected = pCur->cMoreExpectedPolicySet;
                        void *pvNew = RTMemRealloc(pCur->papMoreExpectedPolicySet,
                                                   sizeof(pCur->papMoreExpectedPolicySet[0]) * (iExpected + 1));
                        if (!pvNew)
                            return rtCrX509CpvFailed(pThis, VERR_NO_MEMORY,
                                                     "Error growing papMoreExpectedPolicySet array (cur %u, depth %u)",
                                                     pCur->cMoreExpectedPolicySet, iDepth);
                        pCur->papMoreExpectedPolicySet = (PCRTASN1OBJID *)pvNew;
                        pCur->papMoreExpectedPolicySet[iExpected] = &pOne->SubjectDomainPolicy;
                        pCur->cMoreExpectedPolicySet = iExpected  + 1;
                    }
                    cFound++;
                }
            }

            /*
             * If no mapping took place, look for an anyPolicy node.
             */
            if (!cFound)
            {
                RTListForEach(&pThis->v.paValidPolicyDepthLists[iDepth], pCur, RTCRX509CERTPATHSPOLICYNODE, DepthEntry)
                {
                    if (RTAsn1ObjId_CompareWithString(pCur->pValidPolicy, RTCRX509_ID_CE_CP_ANY_POLICY_OID) == 0)
                    {
                        if (!rtCrX509CpvPolicyTreeInsertNew(pThis, pCur->pParent, iDepth,
                                                            &pOne->IssuerDomainPolicy,
                                                            pCur->pPolicyQualifiers,
                                                            &pOne->SubjectDomainPolicy))
                            return false;
                        break;
                    }
                }
            }
        }
    }
    else
    {
        /*
         * 6.1.4.b.2 - Remove matching policies from the tree if mapping is
         *             inhibited and prune the tree.
         */
        uint32_t cRemoved = 0;
        i = pPolicyMappings->cItems;
        while (i-- > 0)
        {
            PCRTCRX509POLICYMAPPING const pOne = pPolicyMappings->papItems[i];
            RTListForEachSafe(&pThis->v.paValidPolicyDepthLists[iDepth], pCur, pNext, RTCRX509CERTPATHSPOLICYNODE, DepthEntry)
            {
                if (RTAsn1ObjId_Compare(pCur->pValidPolicy, &pOne->IssuerDomainPolicy))
                {
                    rtCrX509CpvPolicyTreeDestroyNode(pThis, pCur);
                    cRemoved++;
                }
            }
        }
        if (cRemoved)
            rtCrX509CpvPolicyTreePrune(pThis, iDepth - 1);
    }

    return true;
}


/**
 * Step 6.1.4.d-f & 6.1.5.c-e.
 */
static void rtCrX509CpvSetWorkingPublicKeyInfo(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHNODE pNode)
{
    PCRTCRX509TBSCERTIFICATE const pTbsCert = &pNode->pCert->TbsCertificate;

    /*
     * 6.1.4.d - The public key.
     */
    pThis->v.pWorkingPublicKey = &pTbsCert->SubjectPublicKeyInfo.SubjectPublicKey;

    /*
     * 6.1.4.e - The public key parameters.  Use new ones if present, keep old
     *           if the algorithm remains the same.
     */
    if (   RTASN1CORE_IS_PRESENT(&pTbsCert->SubjectPublicKeyInfo.Algorithm.Parameters.u.Core)
        && pTbsCert->SubjectPublicKeyInfo.Algorithm.Parameters.enmType != RTASN1TYPE_NULL)
        pThis->v.pWorkingPublicKeyParameters = &pTbsCert->SubjectPublicKeyInfo.Algorithm.Parameters;
    else if (   pThis->v.pWorkingPublicKeyParameters
             && RTAsn1ObjId_Compare(pThis->v.pWorkingPublicKeyAlgorithm, &pTbsCert->SubjectPublicKeyInfo.Algorithm.Algorithm) != 0)
        pThis->v.pWorkingPublicKeyParameters = NULL;

    /*
     * 6.1.4.f - The public algorithm.
     */
    pThis->v.pWorkingPublicKeyAlgorithm = &pTbsCert->SubjectPublicKeyInfo.Algorithm.Algorithm;
}


/**
 * Step 6.1.4.g.
 */
static bool rtCrX509CpvSoakUpNameConstraints(PRTCRX509CERTPATHSINT pThis, PCRTCRX509NAMECONSTRAINTS pNameConstraints)
{
    if (pNameConstraints->T0.PermittedSubtrees.cItems > 0)
        if (!rtCrX509CpvIntersectionPermittedSubtrees(pThis, &pNameConstraints->T0.PermittedSubtrees))
            return false;

    if (pNameConstraints->T1.ExcludedSubtrees.cItems > 0)
        if (!rtCrX509CpvAddExcludedSubtrees(pThis, &pNameConstraints->T1.ExcludedSubtrees))
            return false;

    return true;
}


/**
 * Step 6.1.4.i.
 */
static bool rtCrX509CpvSoakUpPolicyConstraints(PRTCRX509CERTPATHSINT pThis, PCRTCRX509POLICYCONSTRAINTS pPolicyConstraints)
{
    if (RTAsn1Integer_IsPresent(&pPolicyConstraints->RequireExplicitPolicy))
    {
        if (RTAsn1Integer_UnsignedCompareWithU32(&pPolicyConstraints->RequireExplicitPolicy, pThis->v.cExplicitPolicy) < 0)
            pThis->v.cExplicitPolicy = pPolicyConstraints->RequireExplicitPolicy.uValue.s.Lo;
    }

    if (RTAsn1Integer_IsPresent(&pPolicyConstraints->InhibitPolicyMapping))
    {
        if (RTAsn1Integer_UnsignedCompareWithU32(&pPolicyConstraints->InhibitPolicyMapping, pThis->v.cInhibitPolicyMapping) < 0)
            pThis->v.cInhibitPolicyMapping = pPolicyConstraints->InhibitPolicyMapping.uValue.s.Lo;
    }
    return true;
}


/**
 * Step 6.1.4.j.
 */
static bool rtCrX509CpvSoakUpInhibitAnyPolicy(PRTCRX509CERTPATHSINT pThis, PCRTASN1INTEGER pInhibitAnyPolicy)
{
    if (RTAsn1Integer_UnsignedCompareWithU32(pInhibitAnyPolicy, pThis->v.cInhibitAnyPolicy) < 0)
        pThis->v.cInhibitAnyPolicy = pInhibitAnyPolicy->uValue.s.Lo;
    return true;
}


/**
 * Steps 6.1.4.k, 6.1.4.l, 6.1.4.m, and 6.1.4.n.
 */
static bool rtCrX509CpvCheckAndSoakUpBasicConstraintsAndKeyUsage(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHNODE pNode,
                                                                 bool fSelfIssued)
{
    /* 6.1.4.k - If basic constraints present, CA must be set. */
    if (RTAsn1Integer_UnsignedCompareWithU32(&pNode->pCert->TbsCertificate.T0.Version, RTCRX509TBSCERTIFICATE_V3) != 0)
    {
        /* Note! Add flags if support for older certificates is needed later. */
        return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_NOT_V3_CERT,
                                 "Only version 3 certificates are supported (Version=%llu)",
                                 pNode->pCert->TbsCertificate.T0.Version.uValue);
    }
    PCRTCRX509BASICCONSTRAINTS pBasicConstraints = pNode->pCert->TbsCertificate.T3.pBasicConstraints;
    if (pBasicConstraints)
    {
        if (!pBasicConstraints->CA.fValue)
            return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_NOT_CA_CERT,
                                     "Intermediate certificate (#%u) is not marked as a CA", pThis->v.iNode);
    }

    /* 6.1.4.l - Work cMaxPathLength. */
    if (!fSelfIssued)
    {
        if (pThis->v.cMaxPathLength > 0)
            pThis->v.cMaxPathLength--;
        else
            return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_MAX_PATH_LENGTH,
                                     "Hit max path length at node #%u", pThis->v.iNode);
    }

    /* 6.1.4.m - Update cMaxPathLength if basic constrain field is present and smaller. */
    if (pBasicConstraints)
    {
        if (RTAsn1Integer_IsPresent(&pBasicConstraints->PathLenConstraint))
            if (RTAsn1Integer_UnsignedCompareWithU32(&pBasicConstraints->PathLenConstraint, pThis->v.cMaxPathLength) < 0)
                pThis->v.cMaxPathLength = pBasicConstraints->PathLenConstraint.uValue.s.Lo;
    }

    /* 6.1.4.n - Require keyCertSign in key usage if the extension is present. */
    PCRTCRX509TBSCERTIFICATE const pTbsCert = &pNode->pCert->TbsCertificate;
    if (   (pTbsCert->T3.fFlags     & RTCRX509TBSCERTIFICATE_F_PRESENT_KEY_USAGE)
        && !(pTbsCert->T3.fKeyUsage & RTCRX509CERT_KEY_USAGE_F_KEY_CERT_SIGN))
        return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_MISSING_KEY_CERT_SIGN,
                                 "Node #%u does not have KeyCertSign set (keyUsage=%#x)",
                                 pThis->v.iNode, pTbsCert->T3.fKeyUsage);

    return true;
}


/**
 * Step 6.1.4.o - check out critical extensions.
 */
static bool rtCrX509CpvCheckCriticalExtensions(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHNODE pNode)
{
    uint32_t                  cLeft = pNode->pCert->TbsCertificate.T3.Extensions.cItems;
    PRTCRX509EXTENSION const *ppCur = pNode->pCert->TbsCertificate.T3.Extensions.papItems;
    while (cLeft-- > 0)
    {
        PCRTCRX509EXTENSION const pCur = *ppCur;
        if (pCur->Critical.fValue)
        {
            if (   RTAsn1ObjId_CompareWithString(&pCur->ExtnId, RTCRX509_ID_CE_KEY_USAGE_OID) != 0
                && RTAsn1ObjId_CompareWithString(&pCur->ExtnId, RTCRX509_ID_CE_SUBJECT_ALT_NAME_OID) != 0
                && RTAsn1ObjId_CompareWithString(&pCur->ExtnId, RTCRX509_ID_CE_ISSUER_ALT_NAME_OID) != 0
                && RTAsn1ObjId_CompareWithString(&pCur->ExtnId, RTCRX509_ID_CE_BASIC_CONSTRAINTS_OID) != 0
                && RTAsn1ObjId_CompareWithString(&pCur->ExtnId, RTCRX509_ID_CE_NAME_CONSTRAINTS_OID) != 0
                && RTAsn1ObjId_CompareWithString(&pCur->ExtnId, RTCRX509_ID_CE_CERTIFICATE_POLICIES_OID) != 0
                && RTAsn1ObjId_CompareWithString(&pCur->ExtnId, RTCRX509_ID_CE_POLICY_MAPPINGS_OID) != 0
                && RTAsn1ObjId_CompareWithString(&pCur->ExtnId, RTCRX509_ID_CE_POLICY_CONSTRAINTS_OID) != 0
                && RTAsn1ObjId_CompareWithString(&pCur->ExtnId, RTCRX509_ID_CE_EXT_KEY_USAGE_OID) != 0
                && RTAsn1ObjId_CompareWithString(&pCur->ExtnId, RTCRX509_ID_CE_INHIBIT_ANY_POLICY_OID) != 0
                && RTAsn1ObjId_CompareWithString(&pCur->ExtnId, RTCR_APPLE_CS_DEVID_APPLICATION_OID) != 0
                && RTAsn1ObjId_CompareWithString(&pCur->ExtnId, RTCR_APPLE_CS_DEVID_INSTALLER_OID) != 0
                && RTAsn1ObjId_CompareWithString(&pCur->ExtnId, RTCR_APPLE_CS_DEVID_KEXT_OID) != 0
                && RTAsn1ObjId_CompareWithString(&pCur->ExtnId, RTCR_APPLE_CS_DEVID_IPHONE_SW_DEV_OID) != 0
                && RTAsn1ObjId_CompareWithString(&pCur->ExtnId, RTCR_APPLE_CS_DEVID_MAC_SW_DEV_OID) != 0
               )
            {
                /* @bugref{10130}: An IntelGraphicsPE2021 cert issued by iKG_AZSKGFDCS has a critical subjectKeyIdentifier
                                   which we quietly ignore here. RFC-5280 conforming CAs should not mark this as critical.
                                   On an end entity this extension can have relevance to path construction. */
                if (   pNode->uSrc == RTCRX509CERTPATHNODE_SRC_TARGET
                    && RTAsn1ObjId_CompareWithString(&pCur->ExtnId, RTCRX509_ID_CE_SUBJECT_KEY_IDENTIFIER_OID) == 0)
                    LogFunc(("Ignoring non-standard subjectKeyIdentifier on target certificate.\n"));
                else
                    return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_UNKNOWN_CRITICAL_EXTENSION,
                                             "Node #%u has an unknown critical extension: %s",
                                             pThis->v.iNode, pCur->ExtnId.szObjId);
            }
        }

        ppCur++;
    }

    return true;
}


/**
 * Step 6.1.5 - The wrapping up.
 */
static bool rtCrX509CpvWrapUp(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHNODE pNode)
{
    Assert(!pNode->pParent); Assert(pThis->pTarget == pNode->pCert);

    /*
     * 6.1.5.a - Decrement explicit policy.
     */
    if (pThis->v.cExplicitPolicy > 0)
        pThis->v.cExplicitPolicy--;

    /*
     * 6.1.5.b - Policy constraints and explicit policy.
     */
    PCRTCRX509POLICYCONSTRAINTS pPolicyConstraints = pNode->pCert->TbsCertificate.T3.pPolicyConstraints;
    if (   pPolicyConstraints
        && RTAsn1Integer_IsPresent(&pPolicyConstraints->RequireExplicitPolicy)
        && RTAsn1Integer_UnsignedCompareWithU32(&pPolicyConstraints->RequireExplicitPolicy, 0) == 0)
        pThis->v.cExplicitPolicy = 0;

    /*
     * 6.1.5.c-e - Update working public key info.
     */
    rtCrX509CpvSetWorkingPublicKeyInfo(pThis, pNode);

    /*
     * 6.1.5.f - Critical extensions.
     */
    if (!rtCrX509CpvCheckCriticalExtensions(pThis, pNode))
        return false;

    /*
     * 6.1.5.g - Calculate the intersection between the user initial policy set
     *           and the valid policy tree.
     */
    rtCrX509CpvPolicyTreeIntersect(pThis, pThis->cInitialUserPolicySet, pThis->papInitialUserPolicySet);

    if (   pThis->v.cExplicitPolicy == 0
        && pThis->v.pValidPolicyTree == NULL)
        return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_NO_VALID_POLICY, "No valid policy (wrap-up).");

    return true;
}


/**
 * Worker that validates one path.
 *
 * This implements the algorithm in RFC-5280, section 6.1, with exception of
 * the CRL checks in 6.1.3.a.3.
 *
 * @returns success indicator.
 * @param   pThis           The path builder & validator instance.
 * @param   pTrustAnchor    The trust anchor node.
 */
static bool rtCrX509CpvOneWorker(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHNODE pTrustAnchor)
{
    /*
     * Init.
     */
    rtCrX509CpvInit(pThis, pTrustAnchor);
    if (RT_SUCCESS(pThis->rc))
    {
        /*
         * Maybe do some trust anchor checks.
         */
        if (!rtCrX509CpvMaybeCheckTrustAnchor(pThis, pTrustAnchor))
        {
            AssertStmt(RT_FAILURE_NP(pThis->rc), pThis->rc = VERR_CR_X509_CERTPATHS_INTERNAL_ERROR);
            return false;
        }

        /*
         * Special case, target certificate is trusted.
         */
        if (!pTrustAnchor->pParent)
            return true; /* rtCrX509CpvWrapUp should not be needed here. */

        /*
         * Normal processing.
         */
        PRTCRX509CERTPATHNODE   pNode = pTrustAnchor->pParent;
        uint32_t                iNode = pThis->v.iNode = 1; /* We count to cNode (inclusive).  Same a validation tree depth. */
        while (pNode && RT_SUCCESS(pThis->rc))
        {
            /*
             * Basic certificate processing.
             */
            if (!rtCrX509CpvCheckBasicCertInfo(pThis, pNode))                                           /* Step 6.1.3.a */
                break;

            bool const fSelfIssued = rtCrX509CertPathsIsSelfIssued(pNode);
            if (!fSelfIssued || !pNode->pParent)                                                        /* Step 6.1.3.b-c */
                if (!rtCrX509CpvCheckNameConstraints(pThis, pNode))
                    break;

            if (!rtCrX509CpvWorkValidPolicyTree(pThis, iNode, pNode, fSelfIssued))                      /* Step 6.1.3.d-f */
                break;

            /*
             * If it's the last certificate in the path, do wrap-ups.
             */
            if (!pNode->pParent)                                                                         /* Step 6.1.5 */
            {
                Assert(iNode == pThis->v.cNodes);
                if (!rtCrX509CpvWrapUp(pThis, pNode))
                    break;
                AssertRCBreak(pThis->rc);
                return true;
            }

            /*
             * Preparations for the next certificate.
             */
            PCRTCRX509TBSCERTIFICATE const pTbsCert = &pNode->pCert->TbsCertificate;
            if (   pTbsCert->T3.pPolicyMappings
                && !rtCrX509CpvSoakUpPolicyMappings(pThis, iNode, pTbsCert->T3.pPolicyMappings))        /* Step 6.1.4.a-b */
                break;

            pThis->v.pWorkingIssuer = &pTbsCert->Subject;                                               /* Step 6.1.4.c */

            rtCrX509CpvSetWorkingPublicKeyInfo(pThis, pNode);                                           /* Step 6.1.4.d-f */

            if (   pTbsCert->T3.pNameConstraints                                                        /* Step 6.1.4.g */
                && !rtCrX509CpvSoakUpNameConstraints(pThis, pTbsCert->T3.pNameConstraints))
                break;

            if (!fSelfIssued)                                                                           /* Step 6.1.4.h */
            {
                if (pThis->v.cExplicitPolicy > 0)
                    pThis->v.cExplicitPolicy--;
                if (pThis->v.cInhibitPolicyMapping > 0)
                    pThis->v.cInhibitPolicyMapping--;
                if (pThis->v.cInhibitAnyPolicy > 0)
                    pThis->v.cInhibitAnyPolicy--;
            }

            if (   pTbsCert->T3.pPolicyConstraints                                                      /* Step 6.1.4.j */
                && !rtCrX509CpvSoakUpPolicyConstraints(pThis, pTbsCert->T3.pPolicyConstraints))
                break;

            if (   pTbsCert->T3.pInhibitAnyPolicy                                                       /* Step 6.1.4.j */
                && !rtCrX509CpvSoakUpInhibitAnyPolicy(pThis, pTbsCert->T3.pInhibitAnyPolicy))
                break;

            if (!rtCrX509CpvCheckAndSoakUpBasicConstraintsAndKeyUsage(pThis, pNode, fSelfIssued))       /* Step 6.1.4.k-n */
                break;

            if (!rtCrX509CpvCheckCriticalExtensions(pThis, pNode))                                      /* Step 6.1.4.o */
                break;

            /*
             * Advance to the next certificate.
             */
            pNode = pNode->pParent;
            pThis->v.iNode = ++iNode;
        }
        AssertStmt(RT_FAILURE_NP(pThis->rc), pThis->rc = VERR_CR_X509_CERTPATHS_INTERNAL_ERROR);
    }
    return false;
}


RTDECL(int) RTCrX509CertPathsValidateOne(RTCRX509CERTPATHS hCertPaths, uint32_t iPath, PRTERRINFO pErrInfo)
{
    /*
     * Validate the input.
     */
    PRTCRX509CERTPATHSINT pThis = hCertPaths;
    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, VERR_INVALID_HANDLE);
    AssertReturn(!(pThis->fFlags & ~RTCRX509CERTPATHSINT_F_VALID_MASK), VERR_INVALID_PARAMETER);
    AssertPtrReturn(pThis->pTarget, VERR_INVALID_PARAMETER);
    AssertPtrReturn(pThis->pRoot, VERR_INVALID_PARAMETER);
    AssertReturn(pThis->rc == VINF_SUCCESS, VERR_INVALID_PARAMETER);

    /*
     * Locate the path and validate it.
     */
    int rc;
    if (iPath < pThis->cPaths)
    {
        PRTCRX509CERTPATHNODE pLeaf = rtCrX509CertPathsGetLeafByIndex(pThis, iPath);
        if (pLeaf)
        {
            if (RTCRX509CERTPATHNODE_SRC_IS_TRUSTED(pLeaf->uSrc))
            {
                pThis->pErrInfo = pErrInfo;
                rtCrX509CpvOneWorker(pThis, pLeaf);
                pThis->pErrInfo = NULL;
                rc = pThis->rc;
                pThis->rc = VINF_SUCCESS;
            }
            else
                rc = RTErrInfoSetF(pErrInfo, VERR_CR_X509_NO_TRUST_ANCHOR, "Path #%u is does not have a trust anchor: uSrc=%s",
                                   iPath, rtCrX509CertPathsNodeGetSourceName(pLeaf));
            pLeaf->rcVerify = rc;
        }
        else
            rc = VERR_CR_X509_CERTPATHS_INTERNAL_ERROR;
    }
    else
        rc = VERR_NOT_FOUND;
    return rc;
}


RTDECL(int) RTCrX509CertPathsValidateAll(RTCRX509CERTPATHS hCertPaths, uint32_t *pcValidPaths, PRTERRINFO pErrInfo)
{
    /*
     * Validate the input.
     */
    PRTCRX509CERTPATHSINT pThis = hCertPaths;
    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, VERR_INVALID_HANDLE);
    AssertReturn(!(pThis->fFlags & ~RTCRX509CERTPATHSINT_F_VALID_MASK), VERR_INVALID_PARAMETER);
    AssertPtrReturn(pThis->pTarget, VERR_INVALID_PARAMETER);
    AssertPtrReturn(pThis->pRoot, VERR_INVALID_PARAMETER);
    AssertReturn(pThis->rc == VINF_SUCCESS, VERR_INVALID_PARAMETER);
    AssertPtrNullReturn(pcValidPaths, VERR_INVALID_POINTER);

    /*
     * Validate the paths.
     */
    pThis->pErrInfo = pErrInfo;

    int      rcLastFailure = VINF_SUCCESS;
    uint32_t cValidPaths   = 0;
    PRTCRX509CERTPATHNODE pCurLeaf;
    RTListForEach(&pThis->LeafList, pCurLeaf, RTCRX509CERTPATHNODE, ChildListOrLeafEntry)
    {
        if (RTCRX509CERTPATHNODE_SRC_IS_TRUSTED(pCurLeaf->uSrc))
        {
            rtCrX509CpvOneWorker(hCertPaths, pCurLeaf);
            if (RT_SUCCESS(pThis->rc))
                cValidPaths++;
            else
                rcLastFailure = pThis->rc;
            pCurLeaf->rcVerify = pThis->rc;
            pThis->rc = VINF_SUCCESS;
        }
        else
            pCurLeaf->rcVerify = VERR_CR_X509_NO_TRUST_ANCHOR;
    }

    pThis->pErrInfo = NULL;

    if (pcValidPaths)
        *pcValidPaths = cValidPaths;
    if (cValidPaths > 0)
        return VINF_SUCCESS;
    if (RT_SUCCESS_NP(rcLastFailure))
        return RTErrInfoSetF(pErrInfo, VERR_CR_X509_CPV_NO_TRUSTED_PATHS,
                             "None of the %u path(s) have a trust anchor.", pThis->cPaths);
    return rcLastFailure;
}


RTDECL(uint32_t) RTCrX509CertPathsGetPathCount(RTCRX509CERTPATHS hCertPaths)
{
    /*
     * Validate the input.
     */
    PRTCRX509CERTPATHSINT pThis = hCertPaths;
    AssertPtrReturn(pThis, UINT32_MAX);
    AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, UINT32_MAX);
    AssertPtrReturn(pThis->pRoot, UINT32_MAX);

    /*
     * Return data.
     */
    return pThis->cPaths;
}


RTDECL(int)  RTCrX509CertPathsQueryPathInfo(RTCRX509CERTPATHS hCertPaths, uint32_t iPath,
                                            bool *pfTrusted, uint32_t *pcNodes, PCRTCRX509NAME *ppSubject,
                                            PCRTCRX509SUBJECTPUBLICKEYINFO *ppPublicKeyInfo,
                                            PCRTCRX509CERTIFICATE *ppCert, PCRTCRCERTCTX *ppCertCtx,
                                            int *prcVerify)
{
    /*
     * Validate the input.
     */
    PRTCRX509CERTPATHSINT pThis = hCertPaths;
    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, VERR_INVALID_HANDLE);
    AssertPtrReturn(pThis->pRoot, VERR_WRONG_ORDER);
    AssertReturn(iPath < pThis->cPaths, VERR_NOT_FOUND);

    /*
     * Get the data.
     */
    PRTCRX509CERTPATHNODE pLeaf = rtCrX509CertPathsGetLeafByIndex(pThis, iPath);
    AssertReturn(pLeaf, VERR_CR_X509_INTERNAL_ERROR);

    if (pfTrusted)
        *pfTrusted = RTCRX509CERTPATHNODE_SRC_IS_TRUSTED(pLeaf->uSrc);

    if (pcNodes)
        *pcNodes = pLeaf->uDepth + 1; /* Includes both trust anchor and target. */

    if (ppSubject)
        *ppSubject = pLeaf->pCert ? &pLeaf->pCert->TbsCertificate.Subject : &pLeaf->pCertCtx->pTaInfo->CertPath.TaName;

    if (ppPublicKeyInfo)
        *ppPublicKeyInfo = pLeaf->pCert ? &pLeaf->pCert->TbsCertificate.SubjectPublicKeyInfo : &pLeaf->pCertCtx->pTaInfo->PubKey;

    if (ppCert)
        *ppCert = pLeaf->pCert;

    if (ppCertCtx)
    {
        if (pLeaf->pCertCtx)
        {
            uint32_t cRefs = RTCrCertCtxRetain(pLeaf->pCertCtx);
            AssertReturn(cRefs != UINT32_MAX, VERR_CR_X509_INTERNAL_ERROR);
        }
        *ppCertCtx = pLeaf->pCertCtx;
    }

    if (prcVerify)
        *prcVerify = pLeaf->rcVerify;

    return VINF_SUCCESS;
}


RTDECL(uint32_t) RTCrX509CertPathsGetPathLength(RTCRX509CERTPATHS hCertPaths, uint32_t iPath)
{
    /*
     * Validate the input.
     */
    PRTCRX509CERTPATHSINT pThis = hCertPaths;
    AssertPtrReturn(pThis, UINT32_MAX);
    AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, UINT32_MAX);
    AssertPtrReturn(pThis->pRoot, UINT32_MAX);
    AssertReturn(iPath < pThis->cPaths, UINT32_MAX);

    /*
     * Get the data.
     */
    PRTCRX509CERTPATHNODE pLeaf = rtCrX509CertPathsGetLeafByIndex(pThis, iPath);
    AssertReturn(pLeaf, UINT32_MAX);
    return pLeaf->uDepth + 1;
}


RTDECL(int) RTCrX509CertPathsGetPathVerifyResult(RTCRX509CERTPATHS hCertPaths, uint32_t iPath)
{
    /*
     * Validate the input.
     */
    PRTCRX509CERTPATHSINT pThis = hCertPaths;
    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, VERR_INVALID_HANDLE);
    AssertPtrReturn(pThis->pRoot, VERR_WRONG_ORDER);
    AssertReturn(iPath < pThis->cPaths, VERR_NOT_FOUND);

    /*
     * Get the data.
     */
    PRTCRX509CERTPATHNODE pLeaf = rtCrX509CertPathsGetLeafByIndex(pThis, iPath);
    AssertReturn(pLeaf, VERR_CR_X509_INTERNAL_ERROR);

    return pLeaf->rcVerify;
}


static PRTCRX509CERTPATHNODE rtCrX509CertPathsGetPathNodeByIndexes(PRTCRX509CERTPATHSINT pThis, uint32_t iPath, uint32_t iNode)
{
    PRTCRX509CERTPATHNODE pNode = rtCrX509CertPathsGetLeafByIndex(pThis, iPath);
    Assert(pNode);
    if (pNode)
    {
        if (iNode <= pNode->uDepth)
        {
            uint32_t uCertDepth = pNode->uDepth - iNode;
            while (pNode->uDepth > uCertDepth)
                pNode = pNode->pParent;
            Assert(pNode);
            Assert(pNode && pNode->uDepth == uCertDepth);
            return pNode;
        }
    }

    return NULL;
}


RTDECL(PCRTCRX509CERTIFICATE) RTCrX509CertPathsGetPathNodeCert(RTCRX509CERTPATHS hCertPaths, uint32_t iPath, uint32_t iNode)
{
    /*
     * Validate the input.
     */
    PRTCRX509CERTPATHSINT pThis = hCertPaths;
    AssertPtrReturn(pThis, NULL);
    AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, NULL);
    AssertPtrReturn(pThis->pRoot, NULL);
    AssertReturn(iPath < pThis->cPaths, NULL);

    /*
     * Get the data.
     */
    PRTCRX509CERTPATHNODE pNode = rtCrX509CertPathsGetPathNodeByIndexes(pThis, iPath, iNode);
    if (pNode)
        return pNode->pCert;
    return NULL;
}


/** @} */