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 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728
|
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Netscape security libraries.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1994-2000
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Kai Engert (kengert@redhat.com)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
* Implementation of OCSP services, for both client and server.
* (XXX, really, mostly just for client right now, but intended to do both.)
*
* $Id: ocsp.c,v 1.65.2.1 2011/07/13 11:13:55 kaie%kuix.de Exp $
*/
#include "prerror.h"
#include "prprf.h"
#include "plarena.h"
#include "prnetdb.h"
#include "seccomon.h"
#include "secitem.h"
#include "secoidt.h"
#include "secasn1.h"
#include "secder.h"
#include "cert.h"
#include "xconst.h"
#include "secerr.h"
#include "secoid.h"
#include "hasht.h"
#include "sechash.h"
#include "secasn1.h"
#include "keyhi.h"
#include "cryptohi.h"
#include "ocsp.h"
#include "ocspti.h"
#include "ocspi.h"
#include "genname.h"
#include "certxutl.h"
#include "pk11func.h" /* for PK11_HashBuf */
#include <stdarg.h>
#include <plhash.h>
#define DEFAULT_OCSP_CACHE_SIZE 1000
#define DEFAULT_MINIMUM_SECONDS_TO_NEXT_OCSP_FETCH_ATTEMPT 1*60*60L
#define DEFAULT_MAXIMUM_SECONDS_TO_NEXT_OCSP_FETCH_ATTEMPT 24*60*60L
#define DEFAULT_OSCP_TIMEOUT_SECONDS 60
#define MICROSECONDS_PER_SECOND 1000000L
typedef struct OCSPCacheItemStr OCSPCacheItem;
typedef struct OCSPCacheDataStr OCSPCacheData;
struct OCSPCacheItemStr {
/* LRU linking */
OCSPCacheItem *moreRecent;
OCSPCacheItem *lessRecent;
/* key */
CERTOCSPCertID *certID;
/* CertID's arena also used to allocate "this" cache item */
/* cache control information */
PRTime nextFetchAttemptTime;
/* Cached contents. Use a separate arena, because lifetime is different */
PRArenaPool *certStatusArena; /* NULL means: no cert status cached */
ocspCertStatus certStatus;
/* This may contain an error code when no OCSP response is available. */
SECErrorCodes missingResponseError;
PRPackedBool haveThisUpdate;
PRPackedBool haveNextUpdate;
PRTime thisUpdate;
PRTime nextUpdate;
};
struct OCSPCacheDataStr {
PLHashTable *entries;
PRUint32 numberOfEntries;
OCSPCacheItem *MRUitem; /* most recently used cache item */
OCSPCacheItem *LRUitem; /* least recently used cache item */
};
static struct OCSPGlobalStruct {
PRMonitor *monitor;
const SEC_HttpClientFcn *defaultHttpClientFcn;
PRInt32 maxCacheEntries;
PRUint32 minimumSecondsToNextFetchAttempt;
PRUint32 maximumSecondsToNextFetchAttempt;
PRUint32 timeoutSeconds;
OCSPCacheData cache;
SEC_OcspFailureMode ocspFailureMode;
CERT_StringFromCertFcn alternateOCSPAIAFcn;
} OCSP_Global = { NULL,
NULL,
DEFAULT_OCSP_CACHE_SIZE,
DEFAULT_MINIMUM_SECONDS_TO_NEXT_OCSP_FETCH_ATTEMPT,
DEFAULT_MAXIMUM_SECONDS_TO_NEXT_OCSP_FETCH_ATTEMPT,
DEFAULT_OSCP_TIMEOUT_SECONDS,
{NULL, 0, NULL, NULL},
ocspMode_FailureIsVerificationFailure,
NULL
};
/* Forward declarations */
static SECItem *
ocsp_GetEncodedOCSPResponseFromRequest(PRArenaPool *arena,
CERTOCSPRequest *request,
char *location, int64 time,
PRBool addServiceLocator,
void *pwArg,
CERTOCSPRequest **pRequest);
static SECStatus
ocsp_GetOCSPStatusFromNetwork(CERTCertDBHandle *handle,
CERTOCSPCertID *certID,
CERTCertificate *cert,
int64 time,
void *pwArg,
PRBool *certIDWasConsumed,
SECStatus *rv_ocsp);
static SECStatus
ocsp_CacheEncodedOCSPResponse(CERTCertDBHandle *handle,
CERTOCSPCertID *certID,
CERTCertificate *cert,
int64 time,
void *pwArg,
SECItem *encodedResponse,
PRBool *certIDWasConsumed,
PRBool cacheNegative,
SECStatus *rv_ocsp);
static SECStatus
ocsp_GetVerifiedSingleResponseForCertID(CERTCertDBHandle *handle,
CERTOCSPResponse *response,
CERTOCSPCertID *certID,
CERTCertificate *signerCert,
int64 time,
CERTOCSPSingleResponse **pSingleResponse);
static SECStatus
ocsp_CertRevokedAfter(ocspRevokedInfo *revokedInfo, int64 time);
#ifndef DEBUG
#define OCSP_TRACE(msg)
#define OCSP_TRACE_TIME(msg, time)
#define OCSP_TRACE_CERT(cert)
#define OCSP_TRACE_CERTID(certid)
#else
#define OCSP_TRACE(msg) ocsp_Trace msg
#define OCSP_TRACE_TIME(msg, time) ocsp_dumpStringWithTime(msg, time)
#define OCSP_TRACE_CERT(cert) dumpCertificate(cert)
#define OCSP_TRACE_CERTID(certid) dumpCertID(certid)
#if (defined(XP_UNIX) || defined(XP_WIN32) || defined(XP_BEOS) \
|| defined(XP_MACOSX)) && !defined(_WIN32_WCE)
#define NSS_HAVE_GETENV 1
#endif
static PRBool wantOcspTrace()
{
static PRBool firstTime = PR_TRUE;
static PRBool wantTrace = PR_FALSE;
#ifdef NSS_HAVE_GETENV
if (firstTime) {
char *ev = getenv("NSS_TRACE_OCSP");
if (ev && ev[0]) {
wantTrace = PR_TRUE;
}
firstTime = PR_FALSE;
}
#endif
return wantTrace;
}
static void
ocsp_Trace(const char *format, ...)
{
char buf[2000];
va_list args;
if (!wantOcspTrace())
return;
va_start(args, format);
PR_vsnprintf(buf, sizeof(buf), format, args);
va_end(args);
PR_LogPrint("%s", buf);
}
static void
ocsp_dumpStringWithTime(const char *str, int64 time)
{
PRExplodedTime timePrintable;
char timestr[256];
if (!wantOcspTrace())
return;
PR_ExplodeTime(time, PR_GMTParameters, &timePrintable);
if (PR_FormatTime(timestr, 256, "%a %b %d %H:%M:%S %Y", &timePrintable)) {
ocsp_Trace("OCSP %s %s\n", str, timestr);
}
}
static void
printHexString(const char *prefix, SECItem *hexval)
{
unsigned int i;
char *hexbuf = NULL;
for (i = 0; i < hexval->len; i++) {
if (i != hexval->len - 1) {
hexbuf = PR_sprintf_append(hexbuf, "%02x:", hexval->data[i]);
} else {
hexbuf = PR_sprintf_append(hexbuf, "%02x", hexval->data[i]);
}
}
if (hexbuf) {
ocsp_Trace("%s %s\n", prefix, hexbuf);
PR_smprintf_free(hexbuf);
}
}
static void
dumpCertificate(CERTCertificate *cert)
{
if (!wantOcspTrace())
return;
ocsp_Trace("OCSP ----------------\n");
ocsp_Trace("OCSP ## SUBJECT: %s\n", cert->subjectName);
{
int64 timeBefore, timeAfter;
PRExplodedTime beforePrintable, afterPrintable;
char beforestr[256], afterstr[256];
PRStatus rv1, rv2;
DER_DecodeTimeChoice(&timeBefore, &cert->validity.notBefore);
DER_DecodeTimeChoice(&timeAfter, &cert->validity.notAfter);
PR_ExplodeTime(timeBefore, PR_GMTParameters, &beforePrintable);
PR_ExplodeTime(timeAfter, PR_GMTParameters, &afterPrintable);
rv1 = PR_FormatTime(beforestr, 256, "%a %b %d %H:%M:%S %Y",
&beforePrintable);
rv2 = PR_FormatTime(afterstr, 256, "%a %b %d %H:%M:%S %Y",
&afterPrintable);
ocsp_Trace("OCSP ## VALIDITY: %s to %s\n", rv1 ? beforestr : "",
rv2 ? afterstr : "");
}
ocsp_Trace("OCSP ## ISSUER: %s\n", cert->issuerName);
printHexString("OCSP ## SERIAL NUMBER:", &cert->serialNumber);
}
static void
dumpCertID(CERTOCSPCertID *certID)
{
if (!wantOcspTrace())
return;
printHexString("OCSP certID issuer", &certID->issuerNameHash);
printHexString("OCSP certID serial", &certID->serialNumber);
}
#endif
SECStatus
SEC_RegisterDefaultHttpClient(const SEC_HttpClientFcn *fcnTable)
{
if (!OCSP_Global.monitor) {
PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
return SECFailure;
}
PR_EnterMonitor(OCSP_Global.monitor);
OCSP_Global.defaultHttpClientFcn = fcnTable;
PR_ExitMonitor(OCSP_Global.monitor);
return SECSuccess;
}
SECStatus
CERT_RegisterAlternateOCSPAIAInfoCallBack(
CERT_StringFromCertFcn newCallback,
CERT_StringFromCertFcn * oldCallback)
{
CERT_StringFromCertFcn old;
if (!OCSP_Global.monitor) {
PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
return SECFailure;
}
PR_EnterMonitor(OCSP_Global.monitor);
old = OCSP_Global.alternateOCSPAIAFcn;
OCSP_Global.alternateOCSPAIAFcn = newCallback;
PR_ExitMonitor(OCSP_Global.monitor);
if (oldCallback)
*oldCallback = old;
return SECSuccess;
}
static PLHashNumber PR_CALLBACK
ocsp_CacheKeyHashFunction(const void *key)
{
CERTOCSPCertID *cid = (CERTOCSPCertID *)key;
PLHashNumber hash = 0;
unsigned int i;
unsigned char *walk;
/* a very simple hash calculation for the initial coding phase */
walk = (unsigned char*)cid->issuerNameHash.data;
for (i=0; i < cid->issuerNameHash.len; ++i, ++walk) {
hash += *walk;
}
walk = (unsigned char*)cid->issuerKeyHash.data;
for (i=0; i < cid->issuerKeyHash.len; ++i, ++walk) {
hash += *walk;
}
walk = (unsigned char*)cid->serialNumber.data;
for (i=0; i < cid->serialNumber.len; ++i, ++walk) {
hash += *walk;
}
return hash;
}
static PRIntn PR_CALLBACK
ocsp_CacheKeyCompareFunction(const void *v1, const void *v2)
{
CERTOCSPCertID *cid1 = (CERTOCSPCertID *)v1;
CERTOCSPCertID *cid2 = (CERTOCSPCertID *)v2;
return (SECEqual == SECITEM_CompareItem(&cid1->issuerNameHash,
&cid2->issuerNameHash)
&& SECEqual == SECITEM_CompareItem(&cid1->issuerKeyHash,
&cid2->issuerKeyHash)
&& SECEqual == SECITEM_CompareItem(&cid1->serialNumber,
&cid2->serialNumber));
}
static SECStatus
ocsp_CopyRevokedInfo(PRArenaPool *arena, ocspCertStatus *dest,
ocspRevokedInfo *src)
{
SECStatus rv = SECFailure;
void *mark;
mark = PORT_ArenaMark(arena);
dest->certStatusInfo.revokedInfo =
(ocspRevokedInfo *) PORT_ArenaZAlloc(arena, sizeof(ocspRevokedInfo));
if (!dest->certStatusInfo.revokedInfo) {
goto loser;
}
rv = SECITEM_CopyItem(arena,
&dest->certStatusInfo.revokedInfo->revocationTime,
&src->revocationTime);
if (rv != SECSuccess) {
goto loser;
}
if (src->revocationReason) {
dest->certStatusInfo.revokedInfo->revocationReason =
SECITEM_ArenaDupItem(arena, src->revocationReason);
if (!dest->certStatusInfo.revokedInfo->revocationReason) {
goto loser;
}
} else {
dest->certStatusInfo.revokedInfo->revocationReason = NULL;
}
PORT_ArenaUnmark(arena, mark);
return SECSuccess;
loser:
PORT_ArenaRelease(arena, mark);
return SECFailure;
}
static SECStatus
ocsp_CopyCertStatus(PRArenaPool *arena, ocspCertStatus *dest,
ocspCertStatus*src)
{
SECStatus rv = SECFailure;
dest->certStatusType = src->certStatusType;
switch (src->certStatusType) {
case ocspCertStatus_good:
dest->certStatusInfo.goodInfo =
SECITEM_ArenaDupItem(arena, src->certStatusInfo.goodInfo);
if (dest->certStatusInfo.goodInfo != NULL) {
rv = SECSuccess;
}
break;
case ocspCertStatus_revoked:
rv = ocsp_CopyRevokedInfo(arena, dest,
src->certStatusInfo.revokedInfo);
break;
case ocspCertStatus_unknown:
dest->certStatusInfo.unknownInfo =
SECITEM_ArenaDupItem(arena, src->certStatusInfo.unknownInfo);
if (dest->certStatusInfo.unknownInfo != NULL) {
rv = SECSuccess;
}
break;
case ocspCertStatus_other:
default:
PORT_Assert(src->certStatusType == ocspCertStatus_other);
dest->certStatusInfo.otherInfo =
SECITEM_ArenaDupItem(arena, src->certStatusInfo.otherInfo);
if (dest->certStatusInfo.otherInfo != NULL) {
rv = SECSuccess;
}
break;
}
return rv;
}
static void
ocsp_AddCacheItemToLinkedList(OCSPCacheData *cache, OCSPCacheItem *new_most_recent)
{
PR_EnterMonitor(OCSP_Global.monitor);
if (!cache->LRUitem) {
cache->LRUitem = new_most_recent;
}
new_most_recent->lessRecent = cache->MRUitem;
new_most_recent->moreRecent = NULL;
if (cache->MRUitem) {
cache->MRUitem->moreRecent = new_most_recent;
}
cache->MRUitem = new_most_recent;
PR_ExitMonitor(OCSP_Global.monitor);
}
static void
ocsp_RemoveCacheItemFromLinkedList(OCSPCacheData *cache, OCSPCacheItem *item)
{
PR_EnterMonitor(OCSP_Global.monitor);
if (!item->lessRecent && !item->moreRecent) {
/*
* Fail gracefully on attempts to remove an item from the list,
* which is currently not part of the list.
* But check for the edge case it is the single entry in the list.
*/
if (item == cache->LRUitem &&
item == cache->MRUitem) {
/* remove the single entry */
PORT_Assert(cache->numberOfEntries == 1);
PORT_Assert(item->moreRecent == NULL);
cache->MRUitem = NULL;
cache->LRUitem = NULL;
}
PR_ExitMonitor(OCSP_Global.monitor);
return;
}
PORT_Assert(cache->numberOfEntries > 1);
if (item == cache->LRUitem) {
PORT_Assert(item != cache->MRUitem);
PORT_Assert(item->lessRecent == NULL);
PORT_Assert(item->moreRecent != NULL);
PORT_Assert(item->moreRecent->lessRecent == item);
cache->LRUitem = item->moreRecent;
cache->LRUitem->lessRecent = NULL;
}
else if (item == cache->MRUitem) {
PORT_Assert(item->moreRecent == NULL);
PORT_Assert(item->lessRecent != NULL);
PORT_Assert(item->lessRecent->moreRecent == item);
cache->MRUitem = item->lessRecent;
cache->MRUitem->moreRecent = NULL;
} else {
/* remove an entry in the middle of the list */
PORT_Assert(item->moreRecent != NULL);
PORT_Assert(item->lessRecent != NULL);
PORT_Assert(item->lessRecent->moreRecent == item);
PORT_Assert(item->moreRecent->lessRecent == item);
item->moreRecent->lessRecent = item->lessRecent;
item->lessRecent->moreRecent = item->moreRecent;
}
item->lessRecent = NULL;
item->moreRecent = NULL;
PR_ExitMonitor(OCSP_Global.monitor);
}
static void
ocsp_MakeCacheEntryMostRecent(OCSPCacheData *cache, OCSPCacheItem *new_most_recent)
{
OCSP_TRACE(("OCSP ocsp_MakeCacheEntryMostRecent THREADID %p\n",
PR_GetCurrentThread()));
PR_EnterMonitor(OCSP_Global.monitor);
if (cache->MRUitem == new_most_recent) {
OCSP_TRACE(("OCSP ocsp_MakeCacheEntryMostRecent ALREADY MOST\n"));
PR_ExitMonitor(OCSP_Global.monitor);
return;
}
OCSP_TRACE(("OCSP ocsp_MakeCacheEntryMostRecent NEW entry\n"));
ocsp_RemoveCacheItemFromLinkedList(cache, new_most_recent);
ocsp_AddCacheItemToLinkedList(cache, new_most_recent);
PR_ExitMonitor(OCSP_Global.monitor);
}
static PRBool
ocsp_IsCacheDisabled()
{
/*
* maxCacheEntries == 0 means unlimited cache entries
* maxCacheEntries < 0 means cache is disabled
*/
PRBool retval;
PR_EnterMonitor(OCSP_Global.monitor);
retval = (OCSP_Global.maxCacheEntries < 0);
PR_ExitMonitor(OCSP_Global.monitor);
return retval;
}
static OCSPCacheItem *
ocsp_FindCacheEntry(OCSPCacheData *cache, CERTOCSPCertID *certID)
{
OCSPCacheItem *found_ocsp_item = NULL;
OCSP_TRACE(("OCSP ocsp_FindCacheEntry\n"));
OCSP_TRACE_CERTID(certID);
PR_EnterMonitor(OCSP_Global.monitor);
if (ocsp_IsCacheDisabled())
goto loser;
found_ocsp_item = (OCSPCacheItem *)PL_HashTableLookup(
cache->entries, certID);
if (!found_ocsp_item)
goto loser;
OCSP_TRACE(("OCSP ocsp_FindCacheEntry FOUND!\n"));
ocsp_MakeCacheEntryMostRecent(cache, found_ocsp_item);
loser:
PR_ExitMonitor(OCSP_Global.monitor);
return found_ocsp_item;
}
static void
ocsp_FreeCacheItem(OCSPCacheItem *item)
{
OCSP_TRACE(("OCSP ocsp_FreeCacheItem\n"));
if (item->certStatusArena) {
PORT_FreeArena(item->certStatusArena, PR_FALSE);
}
if (item->certID->poolp) {
/* freeing this poolp arena will also free item */
PORT_FreeArena(item->certID->poolp, PR_FALSE);
}
}
static void
ocsp_RemoveCacheItem(OCSPCacheData *cache, OCSPCacheItem *item)
{
/* The item we're removing could be either the least recently used item,
* or it could be an item that couldn't get updated with newer status info
* because of an allocation failure, or it could get removed because we're
* cleaning up.
*/
PRBool couldRemoveFromHashTable;
OCSP_TRACE(("OCSP ocsp_RemoveCacheItem, THREADID %p\n", PR_GetCurrentThread()));
PR_EnterMonitor(OCSP_Global.monitor);
ocsp_RemoveCacheItemFromLinkedList(cache, item);
couldRemoveFromHashTable = PL_HashTableRemove(cache->entries,
item->certID);
PORT_Assert(couldRemoveFromHashTable);
--cache->numberOfEntries;
ocsp_FreeCacheItem(item);
PR_ExitMonitor(OCSP_Global.monitor);
}
static void
ocsp_CheckCacheSize(OCSPCacheData *cache)
{
OCSP_TRACE(("OCSP ocsp_CheckCacheSize\n"));
PR_EnterMonitor(OCSP_Global.monitor);
if (OCSP_Global.maxCacheEntries > 0) {
/* Cache is not disabled. Number of cache entries is limited.
* The monitor ensures that maxCacheEntries remains positive.
*/
while (cache->numberOfEntries >
(PRUint32)OCSP_Global.maxCacheEntries) {
ocsp_RemoveCacheItem(cache, cache->LRUitem);
}
}
PR_ExitMonitor(OCSP_Global.monitor);
}
SECStatus
CERT_ClearOCSPCache()
{
OCSP_TRACE(("OCSP CERT_ClearOCSPCache\n"));
PR_EnterMonitor(OCSP_Global.monitor);
while (OCSP_Global.cache.numberOfEntries > 0) {
ocsp_RemoveCacheItem(&OCSP_Global.cache,
OCSP_Global.cache.LRUitem);
}
PR_ExitMonitor(OCSP_Global.monitor);
return SECSuccess;
}
static SECStatus
ocsp_CreateCacheItemAndConsumeCertID(OCSPCacheData *cache,
CERTOCSPCertID *certID,
OCSPCacheItem **pCacheItem)
{
PRArenaPool *arena;
void *mark;
PLHashEntry *new_hash_entry;
OCSPCacheItem *item;
PORT_Assert(pCacheItem != NULL);
*pCacheItem = NULL;
PR_EnterMonitor(OCSP_Global.monitor);
arena = certID->poolp;
mark = PORT_ArenaMark(arena);
/* ZAlloc will init all Bools to False and all Pointers to NULL
and all error codes to zero/good. */
item = (OCSPCacheItem *)PORT_ArenaZAlloc(certID->poolp,
sizeof(OCSPCacheItem));
if (!item) {
goto loser;
}
item->certID = certID;
new_hash_entry = PL_HashTableAdd(cache->entries, item->certID,
item);
if (!new_hash_entry) {
goto loser;
}
++cache->numberOfEntries;
PORT_ArenaUnmark(arena, mark);
ocsp_AddCacheItemToLinkedList(cache, item);
*pCacheItem = item;
PR_ExitMonitor(OCSP_Global.monitor);
return SECSuccess;
loser:
PORT_ArenaRelease(arena, mark);
PR_ExitMonitor(OCSP_Global.monitor);
return SECFailure;
}
static SECStatus
ocsp_SetCacheItemResponse(OCSPCacheItem *item,
const CERTOCSPSingleResponse *response)
{
if (item->certStatusArena) {
PORT_FreeArena(item->certStatusArena, PR_FALSE);
item->certStatusArena = NULL;
}
item->haveThisUpdate = item->haveNextUpdate = PR_FALSE;
if (response) {
SECStatus rv;
item->certStatusArena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
if (item->certStatusArena == NULL) {
return SECFailure;
}
rv = ocsp_CopyCertStatus(item->certStatusArena, &item->certStatus,
response->certStatus);
if (rv != SECSuccess) {
PORT_FreeArena(item->certStatusArena, PR_FALSE);
item->certStatusArena = NULL;
return rv;
}
item->missingResponseError = 0;
rv = DER_GeneralizedTimeToTime(&item->thisUpdate,
&response->thisUpdate);
item->haveThisUpdate = (rv == SECSuccess);
if (response->nextUpdate) {
rv = DER_GeneralizedTimeToTime(&item->nextUpdate,
response->nextUpdate);
item->haveNextUpdate = (rv == SECSuccess);
} else {
item->haveNextUpdate = PR_FALSE;
}
}
return SECSuccess;
}
static void
ocsp_FreshenCacheItemNextFetchAttemptTime(OCSPCacheItem *cacheItem)
{
PRTime now;
PRTime earliestAllowedNextFetchAttemptTime;
PRTime latestTimeWhenResponseIsConsideredFresh;
OCSP_TRACE(("OCSP ocsp_FreshenCacheItemNextFetchAttemptTime\n"));
PR_EnterMonitor(OCSP_Global.monitor);
now = PR_Now();
OCSP_TRACE_TIME("now:", now);
if (cacheItem->haveThisUpdate) {
OCSP_TRACE_TIME("thisUpdate:", cacheItem->thisUpdate);
latestTimeWhenResponseIsConsideredFresh = cacheItem->thisUpdate +
OCSP_Global.maximumSecondsToNextFetchAttempt *
MICROSECONDS_PER_SECOND;
OCSP_TRACE_TIME("latestTimeWhenResponseIsConsideredFresh:",
latestTimeWhenResponseIsConsideredFresh);
} else {
latestTimeWhenResponseIsConsideredFresh = now +
OCSP_Global.minimumSecondsToNextFetchAttempt *
MICROSECONDS_PER_SECOND;
OCSP_TRACE_TIME("no thisUpdate, "
"latestTimeWhenResponseIsConsideredFresh:",
latestTimeWhenResponseIsConsideredFresh);
}
if (cacheItem->haveNextUpdate) {
OCSP_TRACE_TIME("have nextUpdate:", cacheItem->nextUpdate);
}
if (cacheItem->haveNextUpdate &&
cacheItem->nextUpdate < latestTimeWhenResponseIsConsideredFresh) {
latestTimeWhenResponseIsConsideredFresh = cacheItem->nextUpdate;
OCSP_TRACE_TIME("nextUpdate is smaller than latestFresh, setting "
"latestTimeWhenResponseIsConsideredFresh:",
latestTimeWhenResponseIsConsideredFresh);
}
earliestAllowedNextFetchAttemptTime = now +
OCSP_Global.minimumSecondsToNextFetchAttempt *
MICROSECONDS_PER_SECOND;
OCSP_TRACE_TIME("earliestAllowedNextFetchAttemptTime:",
earliestAllowedNextFetchAttemptTime);
if (latestTimeWhenResponseIsConsideredFresh <
earliestAllowedNextFetchAttemptTime) {
latestTimeWhenResponseIsConsideredFresh =
earliestAllowedNextFetchAttemptTime;
OCSP_TRACE_TIME("latest < earliest, setting latest to:",
latestTimeWhenResponseIsConsideredFresh);
}
cacheItem->nextFetchAttemptTime =
latestTimeWhenResponseIsConsideredFresh;
OCSP_TRACE_TIME("nextFetchAttemptTime",
latestTimeWhenResponseIsConsideredFresh);
PR_ExitMonitor(OCSP_Global.monitor);
}
static PRBool
ocsp_IsCacheItemFresh(OCSPCacheItem *cacheItem)
{
PRTime now;
PRBool retval;
PR_EnterMonitor(OCSP_Global.monitor);
now = PR_Now();
retval = (cacheItem->nextFetchAttemptTime > now);
OCSP_TRACE(("OCSP ocsp_IsCacheItemFresh: %d\n", retval));
PR_ExitMonitor(OCSP_Global.monitor);
return retval;
}
/*
* Status in *certIDWasConsumed will always be correct, regardless of
* return value.
*/
static SECStatus
ocsp_CreateOrUpdateCacheEntry(OCSPCacheData *cache,
CERTOCSPCertID *certID,
CERTOCSPSingleResponse *single,
PRBool *certIDWasConsumed)
{
SECStatus rv;
OCSPCacheItem *cacheItem;
OCSP_TRACE(("OCSP ocsp_CreateOrUpdateCacheEntry\n"));
if (!certIDWasConsumed) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
*certIDWasConsumed = PR_FALSE;
PR_EnterMonitor(OCSP_Global.monitor);
PORT_Assert(OCSP_Global.maxCacheEntries >= 0);
cacheItem = ocsp_FindCacheEntry(cache, certID);
if (!cacheItem) {
rv = ocsp_CreateCacheItemAndConsumeCertID(cache, certID,
&cacheItem);
if (rv != SECSuccess) {
PR_ExitMonitor(OCSP_Global.monitor);
return rv;
}
*certIDWasConsumed = PR_TRUE;
}
if (single) {
rv = ocsp_SetCacheItemResponse(cacheItem, single);
if (rv != SECSuccess) {
ocsp_RemoveCacheItem(cache, cacheItem);
PR_ExitMonitor(OCSP_Global.monitor);
return rv;
}
} else {
cacheItem->missingResponseError = PORT_GetError();
}
ocsp_FreshenCacheItemNextFetchAttemptTime(cacheItem);
ocsp_CheckCacheSize(cache);
PR_ExitMonitor(OCSP_Global.monitor);
return SECSuccess;
}
extern SECStatus
CERT_SetOCSPFailureMode(SEC_OcspFailureMode ocspFailureMode)
{
switch (ocspFailureMode) {
case ocspMode_FailureIsVerificationFailure:
case ocspMode_FailureIsNotAVerificationFailure:
break;
default:
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
PR_EnterMonitor(OCSP_Global.monitor);
OCSP_Global.ocspFailureMode = ocspFailureMode;
PR_ExitMonitor(OCSP_Global.monitor);
return SECSuccess;
}
SECStatus
CERT_OCSPCacheSettings(PRInt32 maxCacheEntries,
PRUint32 minimumSecondsToNextFetchAttempt,
PRUint32 maximumSecondsToNextFetchAttempt)
{
if (minimumSecondsToNextFetchAttempt > maximumSecondsToNextFetchAttempt
|| maxCacheEntries < -1) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
PR_EnterMonitor(OCSP_Global.monitor);
if (maxCacheEntries < 0) {
OCSP_Global.maxCacheEntries = -1; /* disable cache */
} else if (maxCacheEntries == 0) {
OCSP_Global.maxCacheEntries = 0; /* unlimited cache entries */
} else {
OCSP_Global.maxCacheEntries = maxCacheEntries;
}
if (minimumSecondsToNextFetchAttempt <
OCSP_Global.minimumSecondsToNextFetchAttempt
|| maximumSecondsToNextFetchAttempt <
OCSP_Global.maximumSecondsToNextFetchAttempt) {
/*
* Ensure our existing cache entries are not used longer than the
* new settings allow, we're lazy and just clear the cache
*/
CERT_ClearOCSPCache();
}
OCSP_Global.minimumSecondsToNextFetchAttempt =
minimumSecondsToNextFetchAttempt;
OCSP_Global.maximumSecondsToNextFetchAttempt =
maximumSecondsToNextFetchAttempt;
ocsp_CheckCacheSize(&OCSP_Global.cache);
PR_ExitMonitor(OCSP_Global.monitor);
return SECSuccess;
}
SECStatus
CERT_SetOCSPTimeout(PRUint32 seconds)
{
/* no locking, see bug 406120 */
OCSP_Global.timeoutSeconds = seconds;
return SECSuccess;
}
/* this function is called at NSS initialization time */
SECStatus OCSP_InitGlobal(void)
{
SECStatus rv = SECFailure;
if (OCSP_Global.monitor == NULL) {
OCSP_Global.monitor = PR_NewMonitor();
}
if (!OCSP_Global.monitor)
return SECFailure;
PR_EnterMonitor(OCSP_Global.monitor);
if (!OCSP_Global.cache.entries) {
OCSP_Global.cache.entries =
PL_NewHashTable(0,
ocsp_CacheKeyHashFunction,
ocsp_CacheKeyCompareFunction,
PL_CompareValues,
NULL,
NULL);
OCSP_Global.ocspFailureMode = ocspMode_FailureIsVerificationFailure;
OCSP_Global.cache.numberOfEntries = 0;
OCSP_Global.cache.MRUitem = NULL;
OCSP_Global.cache.LRUitem = NULL;
} else {
/*
* NSS might call this function twice while attempting to init.
* But it's not allowed to call this again after any activity.
*/
PORT_Assert(OCSP_Global.cache.numberOfEntries == 0);
PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
}
if (OCSP_Global.cache.entries)
rv = SECSuccess;
PR_ExitMonitor(OCSP_Global.monitor);
return rv;
}
SECStatus OCSP_ShutdownGlobal(void)
{
if (!OCSP_Global.monitor)
return SECSuccess;
PR_EnterMonitor(OCSP_Global.monitor);
if (OCSP_Global.cache.entries) {
CERT_ClearOCSPCache();
PL_HashTableDestroy(OCSP_Global.cache.entries);
OCSP_Global.cache.entries = NULL;
}
PORT_Assert(OCSP_Global.cache.numberOfEntries == 0);
OCSP_Global.cache.MRUitem = NULL;
OCSP_Global.cache.LRUitem = NULL;
OCSP_Global.defaultHttpClientFcn = NULL;
OCSP_Global.maxCacheEntries = DEFAULT_OCSP_CACHE_SIZE;
OCSP_Global.minimumSecondsToNextFetchAttempt =
DEFAULT_MINIMUM_SECONDS_TO_NEXT_OCSP_FETCH_ATTEMPT;
OCSP_Global.maximumSecondsToNextFetchAttempt =
DEFAULT_MAXIMUM_SECONDS_TO_NEXT_OCSP_FETCH_ATTEMPT;
OCSP_Global.ocspFailureMode =
ocspMode_FailureIsVerificationFailure;
PR_ExitMonitor(OCSP_Global.monitor);
PR_DestroyMonitor(OCSP_Global.monitor);
OCSP_Global.monitor = NULL;
return SECSuccess;
}
/*
* A return value of NULL means:
* The application did not register it's own HTTP client.
*/
const SEC_HttpClientFcn *SEC_GetRegisteredHttpClient()
{
const SEC_HttpClientFcn *retval;
if (!OCSP_Global.monitor) {
PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
return NULL;
}
PR_EnterMonitor(OCSP_Global.monitor);
retval = OCSP_Global.defaultHttpClientFcn;
PR_ExitMonitor(OCSP_Global.monitor);
return retval;
}
/*
* The following structure is only used internally. It is allocated when
* someone turns on OCSP checking, and hangs off of the status-configuration
* structure in the certdb structure. We use it to keep configuration
* information specific to OCSP checking.
*/
typedef struct ocspCheckingContextStr {
PRBool useDefaultResponder;
char *defaultResponderURI;
char *defaultResponderNickname;
CERTCertificate *defaultResponderCert;
} ocspCheckingContext;
SEC_ASN1_MKSUB(SEC_AnyTemplate)
SEC_ASN1_MKSUB(SEC_IntegerTemplate)
SEC_ASN1_MKSUB(SEC_NullTemplate)
SEC_ASN1_MKSUB(SEC_OctetStringTemplate)
SEC_ASN1_MKSUB(SEC_PointerToAnyTemplate)
SEC_ASN1_MKSUB(SECOID_AlgorithmIDTemplate)
SEC_ASN1_MKSUB(SEC_SequenceOfAnyTemplate)
SEC_ASN1_MKSUB(SEC_PointerToGeneralizedTimeTemplate)
SEC_ASN1_MKSUB(SEC_PointerToEnumeratedTemplate)
/*
* Forward declarations of sub-types, so I can lay out the types in the
* same order as the ASN.1 is laid out in the OCSP spec itself.
*
* These are in alphabetical order (case-insensitive); please keep it that way!
*/
extern const SEC_ASN1Template ocsp_CertIDTemplate[];
extern const SEC_ASN1Template ocsp_PointerToSignatureTemplate[];
extern const SEC_ASN1Template ocsp_PointerToResponseBytesTemplate[];
extern const SEC_ASN1Template ocsp_ResponseDataTemplate[];
extern const SEC_ASN1Template ocsp_RevokedInfoTemplate[];
extern const SEC_ASN1Template ocsp_SingleRequestTemplate[];
extern const SEC_ASN1Template ocsp_SingleResponseTemplate[];
extern const SEC_ASN1Template ocsp_TBSRequestTemplate[];
/*
* Request-related templates...
*/
/*
* OCSPRequest ::= SEQUENCE {
* tbsRequest TBSRequest,
* optionalSignature [0] EXPLICIT Signature OPTIONAL }
*/
static const SEC_ASN1Template ocsp_OCSPRequestTemplate[] = {
{ SEC_ASN1_SEQUENCE,
0, NULL, sizeof(CERTOCSPRequest) },
{ SEC_ASN1_POINTER,
offsetof(CERTOCSPRequest, tbsRequest),
ocsp_TBSRequestTemplate },
{ SEC_ASN1_OPTIONAL | SEC_ASN1_EXPLICIT |
SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 0,
offsetof(CERTOCSPRequest, optionalSignature),
ocsp_PointerToSignatureTemplate },
{ 0 }
};
/*
* TBSRequest ::= SEQUENCE {
* version [0] EXPLICIT Version DEFAULT v1,
* requestorName [1] EXPLICIT GeneralName OPTIONAL,
* requestList SEQUENCE OF Request,
* requestExtensions [2] EXPLICIT Extensions OPTIONAL }
*
* Version ::= INTEGER { v1(0) }
*
* Note: this should be static but the AIX compiler doesn't like it (because it
* was forward-declared above); it is not meant to be exported, but this
* is the only way it will compile.
*/
const SEC_ASN1Template ocsp_TBSRequestTemplate[] = {
{ SEC_ASN1_SEQUENCE,
0, NULL, sizeof(ocspTBSRequest) },
{ SEC_ASN1_OPTIONAL | SEC_ASN1_EXPLICIT | /* XXX DER_DEFAULT */
SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 0,
offsetof(ocspTBSRequest, version),
SEC_ASN1_SUB(SEC_IntegerTemplate) },
{ SEC_ASN1_OPTIONAL | SEC_ASN1_EXPLICIT |
SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 1,
offsetof(ocspTBSRequest, derRequestorName),
SEC_ASN1_SUB(SEC_PointerToAnyTemplate) },
{ SEC_ASN1_SEQUENCE_OF,
offsetof(ocspTBSRequest, requestList),
ocsp_SingleRequestTemplate },
{ SEC_ASN1_OPTIONAL | SEC_ASN1_EXPLICIT |
SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 2,
offsetof(ocspTBSRequest, requestExtensions),
CERT_SequenceOfCertExtensionTemplate },
{ 0 }
};
/*
* Signature ::= SEQUENCE {
* signatureAlgorithm AlgorithmIdentifier,
* signature BIT STRING,
* certs [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL }
*/
static const SEC_ASN1Template ocsp_SignatureTemplate[] = {
{ SEC_ASN1_SEQUENCE,
0, NULL, sizeof(ocspSignature) },
{ SEC_ASN1_INLINE | SEC_ASN1_XTRN,
offsetof(ocspSignature, signatureAlgorithm),
SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
{ SEC_ASN1_BIT_STRING,
offsetof(ocspSignature, signature) },
{ SEC_ASN1_OPTIONAL | SEC_ASN1_EXPLICIT |
SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 0,
offsetof(ocspSignature, derCerts),
SEC_ASN1_SUB(SEC_SequenceOfAnyTemplate) },
{ 0 }
};
/*
* This template is just an extra level to use in an explicitly-tagged
* reference to a Signature.
*
* Note: this should be static but the AIX compiler doesn't like it (because it
* was forward-declared above); it is not meant to be exported, but this
* is the only way it will compile.
*/
const SEC_ASN1Template ocsp_PointerToSignatureTemplate[] = {
{ SEC_ASN1_POINTER, 0, ocsp_SignatureTemplate }
};
/*
* Request ::= SEQUENCE {
* reqCert CertID,
* singleRequestExtensions [0] EXPLICIT Extensions OPTIONAL }
*
* Note: this should be static but the AIX compiler doesn't like it (because it
* was forward-declared above); it is not meant to be exported, but this
* is the only way it will compile.
*/
const SEC_ASN1Template ocsp_SingleRequestTemplate[] = {
{ SEC_ASN1_SEQUENCE,
0, NULL, sizeof(ocspSingleRequest) },
{ SEC_ASN1_POINTER,
offsetof(ocspSingleRequest, reqCert),
ocsp_CertIDTemplate },
{ SEC_ASN1_OPTIONAL | SEC_ASN1_EXPLICIT |
SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 0,
offsetof(ocspSingleRequest, singleRequestExtensions),
CERT_SequenceOfCertExtensionTemplate },
{ 0 }
};
/*
* This data structure and template (CertID) is used by both OCSP
* requests and responses. It is the only one that is shared.
*
* CertID ::= SEQUENCE {
* hashAlgorithm AlgorithmIdentifier,
* issuerNameHash OCTET STRING, -- Hash of Issuer DN
* issuerKeyHash OCTET STRING, -- Hash of Issuer public key
* serialNumber CertificateSerialNumber }
*
* CertificateSerialNumber ::= INTEGER
*
* Note: this should be static but the AIX compiler doesn't like it (because it
* was forward-declared above); it is not meant to be exported, but this
* is the only way it will compile.
*/
const SEC_ASN1Template ocsp_CertIDTemplate[] = {
{ SEC_ASN1_SEQUENCE,
0, NULL, sizeof(CERTOCSPCertID) },
{ SEC_ASN1_INLINE | SEC_ASN1_XTRN,
offsetof(CERTOCSPCertID, hashAlgorithm),
SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
{ SEC_ASN1_OCTET_STRING,
offsetof(CERTOCSPCertID, issuerNameHash) },
{ SEC_ASN1_OCTET_STRING,
offsetof(CERTOCSPCertID, issuerKeyHash) },
{ SEC_ASN1_INTEGER,
offsetof(CERTOCSPCertID, serialNumber) },
{ 0 }
};
/*
* Response-related templates...
*/
/*
* OCSPResponse ::= SEQUENCE {
* responseStatus OCSPResponseStatus,
* responseBytes [0] EXPLICIT ResponseBytes OPTIONAL }
*/
static const SEC_ASN1Template ocsp_OCSPResponseTemplate[] = {
{ SEC_ASN1_SEQUENCE,
0, NULL, sizeof(CERTOCSPResponse) },
{ SEC_ASN1_ENUMERATED,
offsetof(CERTOCSPResponse, responseStatus) },
{ SEC_ASN1_OPTIONAL | SEC_ASN1_EXPLICIT |
SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 0,
offsetof(CERTOCSPResponse, responseBytes),
ocsp_PointerToResponseBytesTemplate },
{ 0 }
};
/*
* ResponseBytes ::= SEQUENCE {
* responseType OBJECT IDENTIFIER,
* response OCTET STRING }
*/
static const SEC_ASN1Template ocsp_ResponseBytesTemplate[] = {
{ SEC_ASN1_SEQUENCE,
0, NULL, sizeof(ocspResponseBytes) },
{ SEC_ASN1_OBJECT_ID,
offsetof(ocspResponseBytes, responseType) },
{ SEC_ASN1_OCTET_STRING,
offsetof(ocspResponseBytes, response) },
{ 0 }
};
/*
* This template is just an extra level to use in an explicitly-tagged
* reference to a ResponseBytes.
*
* Note: this should be static but the AIX compiler doesn't like it (because it
* was forward-declared above); it is not meant to be exported, but this
* is the only way it will compile.
*/
const SEC_ASN1Template ocsp_PointerToResponseBytesTemplate[] = {
{ SEC_ASN1_POINTER, 0, ocsp_ResponseBytesTemplate }
};
/*
* BasicOCSPResponse ::= SEQUENCE {
* tbsResponseData ResponseData,
* signatureAlgorithm AlgorithmIdentifier,
* signature BIT STRING,
* certs [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL }
*/
static const SEC_ASN1Template ocsp_BasicOCSPResponseTemplate[] = {
{ SEC_ASN1_SEQUENCE,
0, NULL, sizeof(ocspBasicOCSPResponse) },
{ SEC_ASN1_ANY | SEC_ASN1_SAVE,
offsetof(ocspBasicOCSPResponse, tbsResponseDataDER) },
{ SEC_ASN1_POINTER,
offsetof(ocspBasicOCSPResponse, tbsResponseData),
ocsp_ResponseDataTemplate },
{ SEC_ASN1_INLINE | SEC_ASN1_XTRN,
offsetof(ocspBasicOCSPResponse, responseSignature.signatureAlgorithm),
SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
{ SEC_ASN1_BIT_STRING,
offsetof(ocspBasicOCSPResponse, responseSignature.signature) },
{ SEC_ASN1_OPTIONAL | SEC_ASN1_EXPLICIT |
SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 0,
offsetof(ocspBasicOCSPResponse, responseSignature.derCerts),
SEC_ASN1_SUB(SEC_SequenceOfAnyTemplate) },
{ 0 }
};
/*
* ResponseData ::= SEQUENCE {
* version [0] EXPLICIT Version DEFAULT v1,
* responderID ResponderID,
* producedAt GeneralizedTime,
* responses SEQUENCE OF SingleResponse,
* responseExtensions [1] EXPLICIT Extensions OPTIONAL }
*
* Note: this should be static but the AIX compiler doesn't like it (because it
* was forward-declared above); it is not meant to be exported, but this
* is the only way it will compile.
*/
const SEC_ASN1Template ocsp_ResponseDataTemplate[] = {
{ SEC_ASN1_SEQUENCE,
0, NULL, sizeof(ocspResponseData) },
{ SEC_ASN1_OPTIONAL | SEC_ASN1_EXPLICIT | /* XXX DER_DEFAULT */
SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 0,
offsetof(ocspResponseData, version),
SEC_ASN1_SUB(SEC_IntegerTemplate) },
{ SEC_ASN1_ANY,
offsetof(ocspResponseData, derResponderID) },
{ SEC_ASN1_GENERALIZED_TIME,
offsetof(ocspResponseData, producedAt) },
{ SEC_ASN1_SEQUENCE_OF,
offsetof(ocspResponseData, responses),
ocsp_SingleResponseTemplate },
{ SEC_ASN1_OPTIONAL | SEC_ASN1_EXPLICIT |
SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 1,
offsetof(ocspResponseData, responseExtensions),
CERT_SequenceOfCertExtensionTemplate },
{ 0 }
};
/*
* ResponderID ::= CHOICE {
* byName [1] EXPLICIT Name,
* byKey [2] EXPLICIT KeyHash }
*
* KeyHash ::= OCTET STRING -- SHA-1 hash of responder's public key
* (excluding the tag and length fields)
*
* XXX Because the ASN.1 encoder and decoder currently do not provide
* a way to automatically handle a CHOICE, we need to do it in two
* steps, looking at the type tag and feeding the exact choice back
* to the ASN.1 code. Hopefully that will change someday and this
* can all be simplified down into a single template. Anyway, for
* now we list each choice as its own template:
*/
static const SEC_ASN1Template ocsp_ResponderIDByNameTemplate[] = {
{ SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 1,
offsetof(ocspResponderID, responderIDValue.name),
CERT_NameTemplate }
};
static const SEC_ASN1Template ocsp_ResponderIDByKeyTemplate[] = {
{ SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC |
SEC_ASN1_XTRN | 2,
offsetof(ocspResponderID, responderIDValue.keyHash),
SEC_ASN1_SUB(SEC_OctetStringTemplate) }
};
static const SEC_ASN1Template ocsp_ResponderIDOtherTemplate[] = {
{ SEC_ASN1_ANY,
offsetof(ocspResponderID, responderIDValue.other) }
};
/* Decode choice container, but leave x509 name object encoded */
static const SEC_ASN1Template ocsp_ResponderIDDerNameTemplate[] = {
{ SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC |
SEC_ASN1_XTRN | 1, 0, SEC_ASN1_SUB(SEC_AnyTemplate) }
};
/*
* SingleResponse ::= SEQUENCE {
* certID CertID,
* certStatus CertStatus,
* thisUpdate GeneralizedTime,
* nextUpdate [0] EXPLICIT GeneralizedTime OPTIONAL,
* singleExtensions [1] EXPLICIT Extensions OPTIONAL }
*
* Note: this should be static but the AIX compiler doesn't like it (because it
* was forward-declared above); it is not meant to be exported, but this
* is the only way it will compile.
*/
const SEC_ASN1Template ocsp_SingleResponseTemplate[] = {
{ SEC_ASN1_SEQUENCE,
0, NULL, sizeof(CERTOCSPSingleResponse) },
{ SEC_ASN1_POINTER,
offsetof(CERTOCSPSingleResponse, certID),
ocsp_CertIDTemplate },
{ SEC_ASN1_ANY,
offsetof(CERTOCSPSingleResponse, derCertStatus) },
{ SEC_ASN1_GENERALIZED_TIME,
offsetof(CERTOCSPSingleResponse, thisUpdate) },
{ SEC_ASN1_OPTIONAL | SEC_ASN1_EXPLICIT |
SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 0,
offsetof(CERTOCSPSingleResponse, nextUpdate),
SEC_ASN1_SUB(SEC_PointerToGeneralizedTimeTemplate) },
{ SEC_ASN1_OPTIONAL | SEC_ASN1_EXPLICIT |
SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 1,
offsetof(CERTOCSPSingleResponse, singleExtensions),
CERT_SequenceOfCertExtensionTemplate },
{ 0 }
};
/*
* CertStatus ::= CHOICE {
* good [0] IMPLICIT NULL,
* revoked [1] IMPLICIT RevokedInfo,
* unknown [2] IMPLICIT UnknownInfo }
*
* Because the ASN.1 encoder and decoder currently do not provide
* a way to automatically handle a CHOICE, we need to do it in two
* steps, looking at the type tag and feeding the exact choice back
* to the ASN.1 code. Hopefully that will change someday and this
* can all be simplified down into a single template. Anyway, for
* now we list each choice as its own template:
*/
static const SEC_ASN1Template ocsp_CertStatusGoodTemplate[] = {
{ SEC_ASN1_POINTER | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 0,
offsetof(ocspCertStatus, certStatusInfo.goodInfo),
SEC_ASN1_SUB(SEC_NullTemplate) }
};
static const SEC_ASN1Template ocsp_CertStatusRevokedTemplate[] = {
{ SEC_ASN1_POINTER | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 1,
offsetof(ocspCertStatus, certStatusInfo.revokedInfo),
ocsp_RevokedInfoTemplate }
};
static const SEC_ASN1Template ocsp_CertStatusUnknownTemplate[] = {
{ SEC_ASN1_POINTER | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 2,
offsetof(ocspCertStatus, certStatusInfo.unknownInfo),
SEC_ASN1_SUB(SEC_NullTemplate) }
};
static const SEC_ASN1Template ocsp_CertStatusOtherTemplate[] = {
{ SEC_ASN1_POINTER | SEC_ASN1_XTRN,
offsetof(ocspCertStatus, certStatusInfo.otherInfo),
SEC_ASN1_SUB(SEC_AnyTemplate) }
};
/*
* RevokedInfo ::= SEQUENCE {
* revocationTime GeneralizedTime,
* revocationReason [0] EXPLICIT CRLReason OPTIONAL }
*
* Note: this should be static but the AIX compiler doesn't like it (because it
* was forward-declared above); it is not meant to be exported, but this
* is the only way it will compile.
*/
const SEC_ASN1Template ocsp_RevokedInfoTemplate[] = {
{ SEC_ASN1_SEQUENCE,
0, NULL, sizeof(ocspRevokedInfo) },
{ SEC_ASN1_GENERALIZED_TIME,
offsetof(ocspRevokedInfo, revocationTime) },
{ SEC_ASN1_OPTIONAL | SEC_ASN1_EXPLICIT |
SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC |
SEC_ASN1_XTRN | 0,
offsetof(ocspRevokedInfo, revocationReason),
SEC_ASN1_SUB(SEC_PointerToEnumeratedTemplate) },
{ 0 }
};
/*
* OCSP-specific extension templates:
*/
/*
* ServiceLocator ::= SEQUENCE {
* issuer Name,
* locator AuthorityInfoAccessSyntax OPTIONAL }
*/
static const SEC_ASN1Template ocsp_ServiceLocatorTemplate[] = {
{ SEC_ASN1_SEQUENCE,
0, NULL, sizeof(ocspServiceLocator) },
{ SEC_ASN1_POINTER,
offsetof(ocspServiceLocator, issuer),
CERT_NameTemplate },
{ SEC_ASN1_OPTIONAL | SEC_ASN1_ANY,
offsetof(ocspServiceLocator, locator) },
{ 0 }
};
/*
* REQUEST SUPPORT FUNCTIONS (encode/create/decode/destroy):
*/
/*
* FUNCTION: CERT_EncodeOCSPRequest
* DER encodes an OCSP Request, possibly adding a signature as well.
* XXX Signing is not yet supported, however; see comments in code.
* INPUTS:
* PRArenaPool *arena
* The return value is allocated from here.
* If a NULL is passed in, allocation is done from the heap instead.
* CERTOCSPRequest *request
* The request to be encoded.
* void *pwArg
* Pointer to argument for password prompting, if needed. (Definitely
* not needed if not signing.)
* RETURN:
* Returns a NULL on error and a pointer to the SECItem with the
* encoded value otherwise. Any error is likely to be low-level
* (e.g. no memory).
*/
SECItem *
CERT_EncodeOCSPRequest(PRArenaPool *arena, CERTOCSPRequest *request,
void *pwArg)
{
ocspTBSRequest *tbsRequest;
SECStatus rv;
/* XXX All of these should generate errors if they fail. */
PORT_Assert(request);
PORT_Assert(request->tbsRequest);
tbsRequest = request->tbsRequest;
if (request->tbsRequest->extensionHandle != NULL) {
rv = CERT_FinishExtensions(request->tbsRequest->extensionHandle);
request->tbsRequest->extensionHandle = NULL;
if (rv != SECSuccess)
return NULL;
}
/*
* XXX When signed requests are supported and request->optionalSignature
* is not NULL:
* - need to encode tbsRequest->requestorName
* - need to encode tbsRequest
* - need to sign that encoded result (using cert in sig), filling in the
* request->optionalSignature structure with the result, the signing
* algorithm and (perhaps?) the cert (and its chain?) in derCerts
*/
return SEC_ASN1EncodeItem(arena, NULL, request, ocsp_OCSPRequestTemplate);
}
/*
* FUNCTION: CERT_DecodeOCSPRequest
* Decode a DER encoded OCSP Request.
* INPUTS:
* SECItem *src
* Pointer to a SECItem holding DER encoded OCSP Request.
* RETURN:
* Returns a pointer to a CERTOCSPRequest containing the decoded request.
* On error, returns NULL. Most likely error is trouble decoding
* (SEC_ERROR_OCSP_MALFORMED_REQUEST), or low-level problem (no memory).
*/
CERTOCSPRequest *
CERT_DecodeOCSPRequest(SECItem *src)
{
PRArenaPool *arena = NULL;
SECStatus rv = SECFailure;
CERTOCSPRequest *dest = NULL;
int i;
SECItem newSrc;
arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
if (arena == NULL) {
goto loser;
}
dest = (CERTOCSPRequest *) PORT_ArenaZAlloc(arena,
sizeof(CERTOCSPRequest));
if (dest == NULL) {
goto loser;
}
dest->arena = arena;
/* copy the DER into the arena, since Quick DER returns data that points
into the DER input, which may get freed by the caller */
rv = SECITEM_CopyItem(arena, &newSrc, src);
if ( rv != SECSuccess ) {
goto loser;
}
rv = SEC_QuickDERDecodeItem(arena, dest, ocsp_OCSPRequestTemplate, &newSrc);
if (rv != SECSuccess) {
if (PORT_GetError() == SEC_ERROR_BAD_DER)
PORT_SetError(SEC_ERROR_OCSP_MALFORMED_REQUEST);
goto loser;
}
/*
* XXX I would like to find a way to get rid of the necessity
* of doing this copying of the arena pointer.
*/
for (i = 0; dest->tbsRequest->requestList[i] != NULL; i++) {
dest->tbsRequest->requestList[i]->arena = arena;
}
return dest;
loser:
if (arena != NULL) {
PORT_FreeArena(arena, PR_FALSE);
}
return NULL;
}
SECStatus
CERT_DestroyOCSPCertID(CERTOCSPCertID* certID)
{
if (certID && certID->poolp) {
PORT_FreeArena(certID->poolp, PR_FALSE);
return SECSuccess;
}
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
/*
* Digest data using the specified algorithm.
* The necessary storage for the digest data is allocated. If "fill" is
* non-null, the data is put there, otherwise a SECItem is allocated.
* Allocation from "arena" if it is non-null, heap otherwise. Any problem
* results in a NULL being returned (and an appropriate error set).
*/
static SECItem *
ocsp_DigestValue(PRArenaPool *arena, SECOidTag digestAlg,
SECItem *fill, const SECItem *src)
{
const SECHashObject *digestObject;
SECItem *result = NULL;
void *mark = NULL;
void *digestBuff = NULL;
if ( arena != NULL ) {
mark = PORT_ArenaMark(arena);
}
digestObject = HASH_GetHashObjectByOidTag(digestAlg);
if ( digestObject == NULL ) {
goto loser;
}
if (fill == NULL || fill->data == NULL) {
result = SECITEM_AllocItem(arena, fill, digestObject->length);
if ( result == NULL ) {
goto loser;
}
digestBuff = result->data;
} else {
if (fill->len < digestObject->length) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
goto loser;
}
digestBuff = fill->data;
}
if (PK11_HashBuf(digestAlg, digestBuff,
src->data, src->len) != SECSuccess) {
goto loser;
}
if ( arena != NULL ) {
PORT_ArenaUnmark(arena, mark);
}
if (result == NULL) {
result = fill;
}
return result;
loser:
if (arena != NULL) {
PORT_ArenaRelease(arena, mark);
} else {
if (result != NULL) {
SECITEM_FreeItem(result, (fill == NULL) ? PR_TRUE : PR_FALSE);
}
}
return(NULL);
}
/*
* Digest the cert's subject public key using the specified algorithm.
* The necessary storage for the digest data is allocated. If "fill" is
* non-null, the data is put there, otherwise a SECItem is allocated.
* Allocation from "arena" if it is non-null, heap otherwise. Any problem
* results in a NULL being returned (and an appropriate error set).
*/
SECItem *
CERT_GetSPKIDigest(PRArenaPool *arena, const CERTCertificate *cert,
SECOidTag digestAlg, SECItem *fill)
{
SECItem spk;
/*
* Copy just the length and data pointer (nothing needs to be freed)
* of the subject public key so we can convert the length from bits
* to bytes, which is what the digest function expects.
*/
spk = cert->subjectPublicKeyInfo.subjectPublicKey;
DER_ConvertBitString(&spk);
return ocsp_DigestValue(arena, digestAlg, fill, &spk);
}
/*
* Digest the cert's subject name using the specified algorithm.
*/
static SECItem *
cert_GetSubjectNameDigest(PRArenaPool *arena, const CERTCertificate *cert,
SECOidTag digestAlg, SECItem *fill)
{
SECItem name;
/*
* Copy just the length and data pointer (nothing needs to be freed)
* of the subject name
*/
name = cert->derSubject;
return ocsp_DigestValue(arena, digestAlg, fill, &name);
}
/*
* Create and fill-in a CertID. This function fills in the hash values
* (issuerNameHash and issuerKeyHash), and is hardwired to use SHA1.
* Someday it might need to be more flexible about hash algorithm, but
* for now we have no intention/need to create anything else.
*
* Error causes a null to be returned; most likely cause is trouble
* finding the certificate issuer (SEC_ERROR_UNKNOWN_ISSUER).
* Other errors are low-level problems (no memory, bad database, etc.).
*/
static CERTOCSPCertID *
ocsp_CreateCertID(PRArenaPool *arena, CERTCertificate *cert, int64 time)
{
CERTOCSPCertID *certID;
CERTCertificate *issuerCert = NULL;
void *mark = PORT_ArenaMark(arena);
SECStatus rv;
PORT_Assert(arena != NULL);
certID = PORT_ArenaZNew(arena, CERTOCSPCertID);
if (certID == NULL) {
goto loser;
}
rv = SECOID_SetAlgorithmID(arena, &certID->hashAlgorithm, SEC_OID_SHA1,
NULL);
if (rv != SECSuccess) {
goto loser;
}
issuerCert = CERT_FindCertIssuer(cert, time, certUsageAnyCA);
if (issuerCert == NULL) {
goto loser;
}
if (cert_GetSubjectNameDigest(arena, issuerCert, SEC_OID_SHA1,
&(certID->issuerNameHash)) == NULL) {
goto loser;
}
certID->issuerSHA1NameHash.data = certID->issuerNameHash.data;
certID->issuerSHA1NameHash.len = certID->issuerNameHash.len;
if (cert_GetSubjectNameDigest(arena, issuerCert, SEC_OID_MD5,
&(certID->issuerMD5NameHash)) == NULL) {
goto loser;
}
if (cert_GetSubjectNameDigest(arena, issuerCert, SEC_OID_MD2,
&(certID->issuerMD2NameHash)) == NULL) {
goto loser;
}
if (CERT_GetSPKIDigest(arena, issuerCert, SEC_OID_SHA1,
&(certID->issuerKeyHash)) == NULL) {
goto loser;
}
certID->issuerSHA1KeyHash.data = certID->issuerKeyHash.data;
certID->issuerSHA1KeyHash.len = certID->issuerKeyHash.len;
/* cache the other two hash algorithms as well */
if (CERT_GetSPKIDigest(arena, issuerCert, SEC_OID_MD5,
&(certID->issuerMD5KeyHash)) == NULL) {
goto loser;
}
if (CERT_GetSPKIDigest(arena, issuerCert, SEC_OID_MD2,
&(certID->issuerMD2KeyHash)) == NULL) {
goto loser;
}
/* now we are done with issuerCert */
CERT_DestroyCertificate(issuerCert);
issuerCert = NULL;
rv = SECITEM_CopyItem(arena, &certID->serialNumber, &cert->serialNumber);
if (rv != SECSuccess) {
goto loser;
}
PORT_ArenaUnmark(arena, mark);
return certID;
loser:
if (issuerCert != NULL) {
CERT_DestroyCertificate(issuerCert);
}
PORT_ArenaRelease(arena, mark);
return NULL;
}
CERTOCSPCertID*
CERT_CreateOCSPCertID(CERTCertificate *cert, int64 time)
{
PRArenaPool *arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
CERTOCSPCertID *certID;
PORT_Assert(arena != NULL);
if (!arena)
return NULL;
certID = ocsp_CreateCertID(arena, cert, time);
if (!certID) {
PORT_FreeArena(arena, PR_FALSE);
return NULL;
}
certID->poolp = arena;
return certID;
}
/*
* Callback to set Extensions in request object
*/
void SetSingleReqExts(void *object, CERTCertExtension **exts)
{
ocspSingleRequest *singleRequest =
(ocspSingleRequest *)object;
singleRequest->singleRequestExtensions = exts;
}
/*
* Add the Service Locator extension to the singleRequestExtensions
* for the given singleRequest.
*
* All errors are internal or low-level problems (e.g. no memory).
*/
static SECStatus
ocsp_AddServiceLocatorExtension(ocspSingleRequest *singleRequest,
CERTCertificate *cert)
{
ocspServiceLocator *serviceLocator = NULL;
void *extensionHandle = NULL;
SECStatus rv = SECFailure;
serviceLocator = PORT_ZNew(ocspServiceLocator);
if (serviceLocator == NULL)
goto loser;
/*
* Normally it would be a bad idea to do a direct reference like
* this rather than allocate and copy the name *or* at least dup
* a reference of the cert. But all we need is to be able to read
* the issuer name during the encoding we are about to do, so a
* copy is just a waste of time.
*/
serviceLocator->issuer = &cert->issuer;
rv = CERT_FindCertExtension(cert, SEC_OID_X509_AUTH_INFO_ACCESS,
&serviceLocator->locator);
if (rv != SECSuccess) {
if (PORT_GetError() != SEC_ERROR_EXTENSION_NOT_FOUND)
goto loser;
}
/* prepare for following loser gotos */
rv = SECFailure;
PORT_SetError(0);
extensionHandle = cert_StartExtensions(singleRequest,
singleRequest->arena, SetSingleReqExts);
if (extensionHandle == NULL)
goto loser;
rv = CERT_EncodeAndAddExtension(extensionHandle,
SEC_OID_PKIX_OCSP_SERVICE_LOCATOR,
serviceLocator, PR_FALSE,
ocsp_ServiceLocatorTemplate);
loser:
if (extensionHandle != NULL) {
/*
* Either way we have to finish out the extension context (so it gets
* freed). But careful not to override any already-set bad status.
*/
SECStatus tmprv = CERT_FinishExtensions(extensionHandle);
if (rv == SECSuccess)
rv = tmprv;
}
/*
* Finally, free the serviceLocator structure itself and we are done.
*/
if (serviceLocator != NULL) {
if (serviceLocator->locator.data != NULL)
SECITEM_FreeItem(&serviceLocator->locator, PR_FALSE);
PORT_Free(serviceLocator);
}
return rv;
}
/*
* Creates an array of ocspSingleRequest based on a list of certs.
* Note that the code which later compares the request list with the
* response expects this array to be in the exact same order as the
* certs are found in the list. It would be harder to change that
* order than preserve it, but since the requirement is not obvious,
* it deserves to be mentioned.
*
* Any problem causes a null return and error set:
* SEC_ERROR_UNKNOWN_ISSUER
* Other errors are low-level problems (no memory, bad database, etc.).
*/
static ocspSingleRequest **
ocsp_CreateSingleRequestList(PRArenaPool *arena, CERTCertList *certList,
int64 time, PRBool includeLocator)
{
ocspSingleRequest **requestList = NULL;
CERTCertListNode *node = NULL;
int i, count;
void *mark = PORT_ArenaMark(arena);
node = CERT_LIST_HEAD(certList);
for (count = 0; !CERT_LIST_END(node, certList); count++) {
node = CERT_LIST_NEXT(node);
}
if (count == 0)
goto loser;
requestList = PORT_ArenaNewArray(arena, ocspSingleRequest *, count + 1);
if (requestList == NULL)
goto loser;
node = CERT_LIST_HEAD(certList);
for (i = 0; !CERT_LIST_END(node, certList); i++) {
requestList[i] = PORT_ArenaZNew(arena, ocspSingleRequest);
if (requestList[i] == NULL)
goto loser;
OCSP_TRACE(("OCSP CERT_CreateOCSPRequest %s\n", node->cert->subjectName));
requestList[i]->arena = arena;
requestList[i]->reqCert = ocsp_CreateCertID(arena, node->cert, time);
if (requestList[i]->reqCert == NULL)
goto loser;
if (includeLocator == PR_TRUE) {
SECStatus rv;
rv = ocsp_AddServiceLocatorExtension(requestList[i], node->cert);
if (rv != SECSuccess)
goto loser;
}
node = CERT_LIST_NEXT(node);
}
PORT_Assert(i == count);
PORT_ArenaUnmark(arena, mark);
requestList[i] = NULL;
return requestList;
loser:
PORT_ArenaRelease(arena, mark);
return NULL;
}
static ocspSingleRequest **
ocsp_CreateRequestFromCert(PRArenaPool *arena,
CERTOCSPCertID *certID,
CERTCertificate *singleCert,
int64 time,
PRBool includeLocator)
{
ocspSingleRequest **requestList = NULL;
void *mark = PORT_ArenaMark(arena);
PORT_Assert(certID != NULL && singleCert != NULL);
/* meaning of value 2: one entry + one end marker */
requestList = PORT_ArenaNewArray(arena, ocspSingleRequest *, 2);
if (requestList == NULL)
goto loser;
requestList[0] = PORT_ArenaZNew(arena, ocspSingleRequest);
if (requestList[0] == NULL)
goto loser;
requestList[0]->arena = arena;
/* certID will live longer than the request */
requestList[0]->reqCert = certID;
if (includeLocator == PR_TRUE) {
SECStatus rv;
rv = ocsp_AddServiceLocatorExtension(requestList[0], singleCert);
if (rv != SECSuccess)
goto loser;
}
PORT_ArenaUnmark(arena, mark);
requestList[1] = NULL;
return requestList;
loser:
PORT_ArenaRelease(arena, mark);
return NULL;
}
static CERTOCSPRequest *
ocsp_prepareEmptyOCSPRequest()
{
PRArenaPool *arena = NULL;
CERTOCSPRequest *request = NULL;
ocspTBSRequest *tbsRequest = NULL;
arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
if (arena == NULL) {
goto loser;
}
request = PORT_ArenaZNew(arena, CERTOCSPRequest);
if (request == NULL) {
goto loser;
}
request->arena = arena;
tbsRequest = PORT_ArenaZNew(arena, ocspTBSRequest);
if (tbsRequest == NULL) {
goto loser;
}
request->tbsRequest = tbsRequest;
/* version 1 is the default, so we need not fill in a version number */
return request;
loser:
if (arena != NULL) {
PORT_FreeArena(arena, PR_FALSE);
}
return NULL;
}
CERTOCSPRequest *
cert_CreateSingleCertOCSPRequest(CERTOCSPCertID *certID,
CERTCertificate *singleCert,
int64 time,
PRBool addServiceLocator,
CERTCertificate *signerCert)
{
CERTOCSPRequest *request;
OCSP_TRACE(("OCSP cert_CreateSingleCertOCSPRequest %s\n", singleCert->subjectName));
/* XXX Support for signerCert may be implemented later,
* see also the comment in CERT_CreateOCSPRequest.
*/
if (signerCert != NULL) {
PORT_SetError(PR_NOT_IMPLEMENTED_ERROR);
return NULL;
}
request = ocsp_prepareEmptyOCSPRequest();
if (!request)
return NULL;
/*
* Version 1 is the default, so we need not fill in a version number.
* Now create the list of single requests, one for each cert.
*/
request->tbsRequest->requestList =
ocsp_CreateRequestFromCert(request->arena,
certID,
singleCert,
time,
addServiceLocator);
if (request->tbsRequest->requestList == NULL) {
PORT_FreeArena(request->arena, PR_FALSE);
return NULL;
}
return request;
}
/*
* FUNCTION: CERT_CreateOCSPRequest
* Creates a CERTOCSPRequest, requesting the status of the certs in
* the given list.
* INPUTS:
* CERTCertList *certList
* A list of certs for which status will be requested.
* Note that all of these certificates should have the same issuer,
* or it's expected the response will be signed by a trusted responder.
* If the certs need to be broken up into multiple requests, that
* must be handled by the caller (and thus by having multiple calls
* to this routine), who knows about where the request(s) are being
* sent and whether there are any trusted responders in place.
* int64 time
* Indicates the time for which the certificate status is to be
* determined -- this may be used in the search for the cert's issuer
* but has no effect on the request itself.
* PRBool addServiceLocator
* If true, the Service Locator extension should be added to the
* single request(s) for each cert.
* CERTCertificate *signerCert
* If non-NULL, means sign the request using this cert. Otherwise,
* do not sign.
* XXX note that request signing is not yet supported; see comment in code
* RETURN:
* A pointer to a CERTOCSPRequest structure containing an OCSP request
* for the cert list. On error, null is returned, with an error set
* indicating the reason. This is likely SEC_ERROR_UNKNOWN_ISSUER.
* (The issuer is needed to create a request for the certificate.)
* Other errors are low-level problems (no memory, bad database, etc.).
*/
CERTOCSPRequest *
CERT_CreateOCSPRequest(CERTCertList *certList, int64 time,
PRBool addServiceLocator,
CERTCertificate *signerCert)
{
CERTOCSPRequest *request = NULL;
if (!certList) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return NULL;
}
/*
* XXX When we are prepared to put signing of requests back in,
* we will need to allocate a signature
* structure for the request, fill in the "derCerts" field in it,
* save the signerCert there, as well as fill in the "requestorName"
* field of the tbsRequest.
*/
if (signerCert != NULL) {
PORT_SetError(PR_NOT_IMPLEMENTED_ERROR);
return NULL;
}
request = ocsp_prepareEmptyOCSPRequest();
if (!request)
return NULL;
/*
* Now create the list of single requests, one for each cert.
*/
request->tbsRequest->requestList =
ocsp_CreateSingleRequestList(request->arena,
certList,
time,
addServiceLocator);
if (request->tbsRequest->requestList == NULL) {
PORT_FreeArena(request->arena, PR_FALSE);
return NULL;
}
return request;
}
/*
* FUNCTION: CERT_AddOCSPAcceptableResponses
* Add the AcceptableResponses extension to an OCSP Request.
* INPUTS:
* CERTOCSPRequest *request
* The request to which the extension should be added.
* ...
* A list (of one or more) of SECOidTag -- each of the response types
* to be added. The last OID *must* be SEC_OID_PKIX_OCSP_BASIC_RESPONSE.
* (This marks the end of the list, and it must be specified because a
* client conforming to the OCSP standard is required to handle the basic
* response type.) The OIDs are not checked in any way.
* RETURN:
* SECSuccess if the extension is added; SECFailure if anything goes wrong.
* All errors are internal or low-level problems (e.g. no memory).
*/
void SetRequestExts(void *object, CERTCertExtension **exts)
{
CERTOCSPRequest *request = (CERTOCSPRequest *)object;
request->tbsRequest->requestExtensions = exts;
}
SECStatus
CERT_AddOCSPAcceptableResponses(CERTOCSPRequest *request,
SECOidTag responseType0, ...)
{
void *extHandle;
va_list ap;
int i, count;
SECOidTag responseType;
SECOidData *responseOid;
SECItem **acceptableResponses = NULL;
SECStatus rv = SECFailure;
extHandle = request->tbsRequest->extensionHandle;
if (extHandle == NULL) {
extHandle = cert_StartExtensions(request, request->arena, SetRequestExts);
if (extHandle == NULL)
goto loser;
}
/* Count number of OIDS going into the extension value. */
count = 1;
if (responseType0 != SEC_OID_PKIX_OCSP_BASIC_RESPONSE) {
va_start(ap, responseType0);
do {
count++;
responseType = va_arg(ap, SECOidTag);
} while (responseType != SEC_OID_PKIX_OCSP_BASIC_RESPONSE);
va_end(ap);
}
acceptableResponses = PORT_NewArray(SECItem *, count + 1);
if (acceptableResponses == NULL)
goto loser;
i = 0;
responseOid = SECOID_FindOIDByTag(responseType0);
acceptableResponses[i++] = &(responseOid->oid);
if (count > 1) {
va_start(ap, responseType0);
for ( ; i < count; i++) {
responseType = va_arg(ap, SECOidTag);
responseOid = SECOID_FindOIDByTag(responseType);
acceptableResponses[i] = &(responseOid->oid);
}
va_end(ap);
}
acceptableResponses[i] = NULL;
rv = CERT_EncodeAndAddExtension(extHandle, SEC_OID_PKIX_OCSP_RESPONSE,
&acceptableResponses, PR_FALSE,
SEC_ASN1_GET(SEC_SequenceOfObjectIDTemplate));
if (rv != SECSuccess)
goto loser;
PORT_Free(acceptableResponses);
if (request->tbsRequest->extensionHandle == NULL)
request->tbsRequest->extensionHandle = extHandle;
return SECSuccess;
loser:
if (acceptableResponses != NULL)
PORT_Free(acceptableResponses);
if (extHandle != NULL)
(void) CERT_FinishExtensions(extHandle);
return rv;
}
/*
* FUNCTION: CERT_DestroyOCSPRequest
* Frees an OCSP Request structure.
* INPUTS:
* CERTOCSPRequest *request
* Pointer to CERTOCSPRequest to be freed.
* RETURN:
* No return value; no errors.
*/
void
CERT_DestroyOCSPRequest(CERTOCSPRequest *request)
{
if (request == NULL)
return;
if (request->tbsRequest != NULL) {
if (request->tbsRequest->requestorName != NULL)
CERT_DestroyGeneralNameList(request->tbsRequest->requestorName);
if (request->tbsRequest->extensionHandle != NULL)
(void) CERT_FinishExtensions(request->tbsRequest->extensionHandle);
}
if (request->optionalSignature != NULL) {
if (request->optionalSignature->cert != NULL)
CERT_DestroyCertificate(request->optionalSignature->cert);
/*
* XXX Need to free derCerts? Or do they come out of arena?
* (Currently we never fill in derCerts, which is why the
* answer is not obvious. Once we do, add any necessary code
* here and remove this comment.)
*/
}
/*
* We should actually never have a request without an arena,
* but check just in case. (If there isn't one, there is not
* much we can do about it...)
*/
PORT_Assert(request->arena != NULL);
if (request->arena != NULL)
PORT_FreeArena(request->arena, PR_FALSE);
}
/*
* RESPONSE SUPPORT FUNCTIONS (encode/create/decode/destroy):
*/
/*
* Helper function for encoding or decoding a ResponderID -- based on the
* given type, return the associated template for that choice.
*/
static const SEC_ASN1Template *
ocsp_ResponderIDTemplateByType(ocspResponderIDType responderIDType)
{
const SEC_ASN1Template *responderIDTemplate;
switch (responderIDType) {
case ocspResponderID_byName:
responderIDTemplate = ocsp_ResponderIDByNameTemplate;
break;
case ocspResponderID_byKey:
responderIDTemplate = ocsp_ResponderIDByKeyTemplate;
break;
case ocspResponderID_other:
default:
PORT_Assert(responderIDType == ocspResponderID_other);
responderIDTemplate = ocsp_ResponderIDOtherTemplate;
break;
}
return responderIDTemplate;
}
/*
* Helper function for encoding or decoding a CertStatus -- based on the
* given type, return the associated template for that choice.
*/
static const SEC_ASN1Template *
ocsp_CertStatusTemplateByType(ocspCertStatusType certStatusType)
{
const SEC_ASN1Template *certStatusTemplate;
switch (certStatusType) {
case ocspCertStatus_good:
certStatusTemplate = ocsp_CertStatusGoodTemplate;
break;
case ocspCertStatus_revoked:
certStatusTemplate = ocsp_CertStatusRevokedTemplate;
break;
case ocspCertStatus_unknown:
certStatusTemplate = ocsp_CertStatusUnknownTemplate;
break;
case ocspCertStatus_other:
default:
PORT_Assert(certStatusType == ocspCertStatus_other);
certStatusTemplate = ocsp_CertStatusOtherTemplate;
break;
}
return certStatusTemplate;
}
/*
* Helper function for decoding a certStatus -- turn the actual DER tag
* into our local translation.
*/
static ocspCertStatusType
ocsp_CertStatusTypeByTag(int derTag)
{
ocspCertStatusType certStatusType;
switch (derTag) {
case 0:
certStatusType = ocspCertStatus_good;
break;
case 1:
certStatusType = ocspCertStatus_revoked;
break;
case 2:
certStatusType = ocspCertStatus_unknown;
break;
default:
certStatusType = ocspCertStatus_other;
break;
}
return certStatusType;
}
/*
* Helper function for decoding SingleResponses -- they each contain
* a status which is encoded as CHOICE, which needs to be decoded "by hand".
*
* Note -- on error, this routine does not release the memory it may
* have allocated; it expects its caller to do that.
*/
static SECStatus
ocsp_FinishDecodingSingleResponses(PRArenaPool *reqArena,
CERTOCSPSingleResponse **responses)
{
ocspCertStatus *certStatus;
ocspCertStatusType certStatusType;
const SEC_ASN1Template *certStatusTemplate;
int derTag;
int i;
SECStatus rv = SECFailure;
if (!reqArena) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
if (responses == NULL) /* nothing to do */
return SECSuccess;
for (i = 0; responses[i] != NULL; i++) {
SECItem* newStatus;
/*
* The following assert points out internal errors (problems in
* the template definitions or in the ASN.1 decoder itself, etc.).
*/
PORT_Assert(responses[i]->derCertStatus.data != NULL);
derTag = responses[i]->derCertStatus.data[0] & SEC_ASN1_TAGNUM_MASK;
certStatusType = ocsp_CertStatusTypeByTag(derTag);
certStatusTemplate = ocsp_CertStatusTemplateByType(certStatusType);
certStatus = PORT_ArenaZAlloc(reqArena, sizeof(ocspCertStatus));
if (certStatus == NULL) {
goto loser;
}
newStatus = SECITEM_ArenaDupItem(reqArena, &responses[i]->derCertStatus);
if (!newStatus) {
goto loser;
}
rv = SEC_QuickDERDecodeItem(reqArena, certStatus, certStatusTemplate,
newStatus);
if (rv != SECSuccess) {
if (PORT_GetError() == SEC_ERROR_BAD_DER)
PORT_SetError(SEC_ERROR_OCSP_MALFORMED_RESPONSE);
goto loser;
}
certStatus->certStatusType = certStatusType;
responses[i]->certStatus = certStatus;
}
return SECSuccess;
loser:
return rv;
}
/*
* Helper function for decoding a responderID -- turn the actual DER tag
* into our local translation.
*/
static ocspResponderIDType
ocsp_ResponderIDTypeByTag(int derTag)
{
ocspResponderIDType responderIDType;
switch (derTag) {
case 1:
responderIDType = ocspResponderID_byName;
break;
case 2:
responderIDType = ocspResponderID_byKey;
break;
default:
responderIDType = ocspResponderID_other;
break;
}
return responderIDType;
}
/*
* Decode "src" as a BasicOCSPResponse, returning the result.
*/
static ocspBasicOCSPResponse *
ocsp_DecodeBasicOCSPResponse(PRArenaPool *arena, SECItem *src)
{
void *mark;
ocspBasicOCSPResponse *basicResponse;
ocspResponseData *responseData;
ocspResponderID *responderID;
ocspResponderIDType responderIDType;
const SEC_ASN1Template *responderIDTemplate;
int derTag;
SECStatus rv;
SECItem newsrc;
mark = PORT_ArenaMark(arena);
basicResponse = PORT_ArenaZAlloc(arena, sizeof(ocspBasicOCSPResponse));
if (basicResponse == NULL) {
goto loser;
}
/* copy the DER into the arena, since Quick DER returns data that points
into the DER input, which may get freed by the caller */
rv = SECITEM_CopyItem(arena, &newsrc, src);
if ( rv != SECSuccess ) {
goto loser;
}
rv = SEC_QuickDERDecodeItem(arena, basicResponse,
ocsp_BasicOCSPResponseTemplate, &newsrc);
if (rv != SECSuccess) {
if (PORT_GetError() == SEC_ERROR_BAD_DER)
PORT_SetError(SEC_ERROR_OCSP_MALFORMED_RESPONSE);
goto loser;
}
responseData = basicResponse->tbsResponseData;
/*
* The following asserts point out internal errors (problems in
* the template definitions or in the ASN.1 decoder itself, etc.).
*/
PORT_Assert(responseData != NULL);
PORT_Assert(responseData->derResponderID.data != NULL);
/*
* XXX Because responderID is a CHOICE, which is not currently handled
* by our ASN.1 decoder, we have to decode it "by hand".
*/
derTag = responseData->derResponderID.data[0] & SEC_ASN1_TAGNUM_MASK;
responderIDType = ocsp_ResponderIDTypeByTag(derTag);
responderIDTemplate = ocsp_ResponderIDTemplateByType(responderIDType);
responderID = PORT_ArenaZAlloc(arena, sizeof(ocspResponderID));
if (responderID == NULL) {
goto loser;
}
rv = SEC_QuickDERDecodeItem(arena, responderID, responderIDTemplate,
&responseData->derResponderID);
if (rv != SECSuccess) {
if (PORT_GetError() == SEC_ERROR_BAD_DER)
PORT_SetError(SEC_ERROR_OCSP_MALFORMED_RESPONSE);
goto loser;
}
responderID->responderIDType = responderIDType;
responseData->responderID = responderID;
/*
* XXX Each SingleResponse also contains a CHOICE, which has to be
* fixed up by hand.
*/
rv = ocsp_FinishDecodingSingleResponses(arena, responseData->responses);
if (rv != SECSuccess) {
goto loser;
}
PORT_ArenaUnmark(arena, mark);
return basicResponse;
loser:
PORT_ArenaRelease(arena, mark);
return NULL;
}
/*
* Decode the responseBytes based on the responseType found in "rbytes",
* leaving the resulting translated/decoded information in there as well.
*/
static SECStatus
ocsp_DecodeResponseBytes(PRArenaPool *arena, ocspResponseBytes *rbytes)
{
PORT_Assert(rbytes != NULL); /* internal error, really */
if (rbytes == NULL) {
PORT_SetError(SEC_ERROR_INVALID_ARGS); /* XXX set better error? */
return SECFailure;
}
rbytes->responseTypeTag = SECOID_FindOIDTag(&rbytes->responseType);
switch (rbytes->responseTypeTag) {
case SEC_OID_PKIX_OCSP_BASIC_RESPONSE:
{
ocspBasicOCSPResponse *basicResponse;
basicResponse = ocsp_DecodeBasicOCSPResponse(arena,
&rbytes->response);
if (basicResponse == NULL)
return SECFailure;
rbytes->decodedResponse.basic = basicResponse;
}
break;
/*
* Add new/future response types here.
*/
default:
PORT_SetError(SEC_ERROR_OCSP_UNKNOWN_RESPONSE_TYPE);
return SECFailure;
}
return SECSuccess;
}
/*
* FUNCTION: CERT_DecodeOCSPResponse
* Decode a DER encoded OCSP Response.
* INPUTS:
* SECItem *src
* Pointer to a SECItem holding DER encoded OCSP Response.
* RETURN:
* Returns a pointer to a CERTOCSPResponse (the decoded OCSP Response);
* the caller is responsible for destroying it. Or NULL if error (either
* response could not be decoded (SEC_ERROR_OCSP_MALFORMED_RESPONSE),
* it was of an unexpected type (SEC_ERROR_OCSP_UNKNOWN_RESPONSE_TYPE),
* or a low-level or internal error occurred).
*/
CERTOCSPResponse *
CERT_DecodeOCSPResponse(SECItem *src)
{
PRArenaPool *arena = NULL;
CERTOCSPResponse *response = NULL;
SECStatus rv = SECFailure;
ocspResponseStatus sv;
SECItem newSrc;
arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
if (arena == NULL) {
goto loser;
}
response = (CERTOCSPResponse *) PORT_ArenaZAlloc(arena,
sizeof(CERTOCSPResponse));
if (response == NULL) {
goto loser;
}
response->arena = arena;
/* copy the DER into the arena, since Quick DER returns data that points
into the DER input, which may get freed by the caller */
rv = SECITEM_CopyItem(arena, &newSrc, src);
if ( rv != SECSuccess ) {
goto loser;
}
rv = SEC_QuickDERDecodeItem(arena, response, ocsp_OCSPResponseTemplate, &newSrc);
if (rv != SECSuccess) {
if (PORT_GetError() == SEC_ERROR_BAD_DER)
PORT_SetError(SEC_ERROR_OCSP_MALFORMED_RESPONSE);
goto loser;
}
sv = (ocspResponseStatus) DER_GetInteger(&response->responseStatus);
response->statusValue = sv;
if (sv != ocspResponse_successful) {
/*
* If the response status is anything but successful, then we
* are all done with decoding; the status is all there is.
*/
return response;
}
/*
* A successful response contains much more information, still encoded.
* Now we need to decode that.
*/
rv = ocsp_DecodeResponseBytes(arena, response->responseBytes);
if (rv != SECSuccess) {
goto loser;
}
return response;
loser:
if (arena != NULL) {
PORT_FreeArena(arena, PR_FALSE);
}
return NULL;
}
/*
* The way an OCSPResponse is defined, there are many levels to descend
* before getting to the actual response information. And along the way
* we need to check that the response *type* is recognizable, which for
* now means that it is a BasicOCSPResponse, because that is the only
* type currently defined. Rather than force all routines to perform
* a bunch of sanity checking every time they want to work on a response,
* this function isolates that and gives back the interesting part.
* Note that no copying is done, this just returns a pointer into the
* substructure of the response which is passed in.
*
* XXX This routine only works when a valid response structure is passed
* into it; this is checked with many assertions. Assuming the response
* was creating by decoding, it wouldn't make it this far without being
* okay. That is a sufficient assumption since the entire OCSP interface
* is only used internally. When this interface is officially exported,
* each assertion below will need to be followed-up with setting an error
* and returning (null).
*
* FUNCTION: ocsp_GetResponseData
* Returns ocspResponseData structure and a pointer to tbs response
* data DER from a valid ocsp response.
* INPUTS:
* CERTOCSPResponse *response
* structure of a valid ocsp response
* RETURN:
* Returns a pointer to ocspResponseData structure: decoded OCSP response
* data, and a pointer(tbsResponseDataDER) to its undecoded data DER.
*/
ocspResponseData *
ocsp_GetResponseData(CERTOCSPResponse *response, SECItem **tbsResponseDataDER)
{
ocspBasicOCSPResponse *basic;
ocspResponseData *responseData;
PORT_Assert(response != NULL);
PORT_Assert(response->responseBytes != NULL);
PORT_Assert(response->responseBytes->responseTypeTag
== SEC_OID_PKIX_OCSP_BASIC_RESPONSE);
basic = response->responseBytes->decodedResponse.basic;
PORT_Assert(basic != NULL);
responseData = basic->tbsResponseData;
PORT_Assert(responseData != NULL);
if (tbsResponseDataDER) {
*tbsResponseDataDER = &basic->tbsResponseDataDER;
PORT_Assert((*tbsResponseDataDER)->data != NULL);
PORT_Assert((*tbsResponseDataDER)->len != 0);
}
return responseData;
}
/*
* Much like the routine above, except it returns the response signature.
* Again, no copy is done.
*/
ocspSignature *
ocsp_GetResponseSignature(CERTOCSPResponse *response)
{
ocspBasicOCSPResponse *basic;
PORT_Assert(response != NULL);
if (NULL == response->responseBytes) {
return NULL;
}
PORT_Assert(response->responseBytes != NULL);
PORT_Assert(response->responseBytes->responseTypeTag
== SEC_OID_PKIX_OCSP_BASIC_RESPONSE);
basic = response->responseBytes->decodedResponse.basic;
PORT_Assert(basic != NULL);
return &(basic->responseSignature);
}
/*
* FUNCTION: CERT_DestroyOCSPResponse
* Frees an OCSP Response structure.
* INPUTS:
* CERTOCSPResponse *request
* Pointer to CERTOCSPResponse to be freed.
* RETURN:
* No return value; no errors.
*/
void
CERT_DestroyOCSPResponse(CERTOCSPResponse *response)
{
if (response != NULL) {
ocspSignature *signature = ocsp_GetResponseSignature(response);
if (signature && signature->cert != NULL)
CERT_DestroyCertificate(signature->cert);
/*
* We should actually never have a response without an arena,
* but check just in case. (If there isn't one, there is not
* much we can do about it...)
*/
PORT_Assert(response->arena != NULL);
if (response->arena != NULL) {
PORT_FreeArena(response->arena, PR_FALSE);
}
}
}
/*
* OVERALL OCSP CLIENT SUPPORT (make and send a request, verify a response):
*/
/*
* Pick apart a URL, saving the important things in the passed-in pointers.
*
* We expect to find "http://<hostname>[:<port>]/[path]", though we will
* tolerate that final slash character missing, as well as beginning and
* trailing whitespace, and any-case-characters for "http". All of that
* tolerance is what complicates this routine. What we want is just to
* pick out the hostname, the port, and the path.
*
* On a successful return, the caller will need to free the output pieces
* of hostname and path, which are copies of the values found in the url.
*/
static SECStatus
ocsp_ParseURL(const char *url, char **pHostname, PRUint16 *pPort, char **pPath)
{
unsigned short port = 80; /* default, in case not in url */
char *hostname = NULL;
char *path = NULL;
const char *save;
char c;
int len;
if (url == NULL)
goto loser;
/*
* Skip beginning whitespace.
*/
c = *url;
while ((c == ' ' || c == '\t') && c != '\0') {
url++;
c = *url;
}
if (c == '\0')
goto loser;
/*
* Confirm, then skip, protocol. (Since we only know how to do http,
* that is all we will accept).
*/
if (PORT_Strncasecmp(url, "http://", 7) != 0)
goto loser;
url += 7;
/*
* Whatever comes next is the hostname (or host IP address). We just
* save it aside and then search for its end so we can determine its
* length and copy it.
*
* XXX Note that because we treat a ':' as a terminator character
* (and below, we expect that to mean there is a port specification
* immediately following), we will not handle IPv6 addresses. That is
* apparently an acceptable limitation, for the time being. Some day,
* when there is a clear way to specify a URL with an IPv6 address that
* can be parsed unambiguously, this code should be made to do that.
*/
save = url;
c = *url;
while (c != '/' && c != ':' && c != '\0' && c != ' ' && c != '\t') {
url++;
c = *url;
}
len = url - save;
hostname = PORT_Alloc(len + 1);
if (hostname == NULL)
goto loser;
PORT_Memcpy(hostname, save, len);
hostname[len] = '\0';
/*
* Now we figure out if there was a port specified or not.
* If so, we need to parse it (as a number) and skip it.
*/
if (c == ':') {
url++;
port = (unsigned short) PORT_Atoi(url);
c = *url;
while (c != '/' && c != '\0' && c != ' ' && c != '\t') {
if (c < '0' || c > '9')
goto loser;
url++;
c = *url;
}
}
/*
* Last thing to find is a path. There *should* be a slash,
* if nothing else -- but if there is not we provide one.
*/
if (c == '/') {
save = url;
while (c != '\0' && c != ' ' && c != '\t') {
url++;
c = *url;
}
len = url - save;
path = PORT_Alloc(len + 1);
if (path == NULL)
goto loser;
PORT_Memcpy(path, save, len);
path[len] = '\0';
} else {
path = PORT_Strdup("/");
if (path == NULL)
goto loser;
}
*pHostname = hostname;
*pPort = port;
*pPath = path;
return SECSuccess;
loser:
if (hostname != NULL)
PORT_Free(hostname);
PORT_SetError(SEC_ERROR_CERT_BAD_ACCESS_LOCATION);
return SECFailure;
}
/*
* Open a socket to the specified host on the specified port, and return it.
* The host is either a hostname or an IP address.
*/
static PRFileDesc *
ocsp_ConnectToHost(const char *host, PRUint16 port)
{
PRFileDesc *sock = NULL;
PRIntervalTime timeout;
PRNetAddr addr;
char *netdbbuf = NULL;
sock = PR_NewTCPSocket();
if (sock == NULL)
goto loser;
/* XXX Some day need a way to set (and get?) the following value */
timeout = PR_SecondsToInterval(30);
/*
* If the following converts an IP address string in "dot notation"
* into a PRNetAddr. If it fails, we assume that is because we do not
* have such an address, but instead a host *name*. In that case we
* then lookup the host by name. Using the NSPR function this way
* means we do not have to have our own logic for distinguishing a
* valid numerical IP address from a hostname.
*/
if (PR_StringToNetAddr(host, &addr) != PR_SUCCESS) {
PRIntn hostIndex;
PRHostEnt hostEntry;
netdbbuf = PORT_Alloc(PR_NETDB_BUF_SIZE);
if (netdbbuf == NULL)
goto loser;
if (PR_GetHostByName(host, netdbbuf, PR_NETDB_BUF_SIZE,
&hostEntry) != PR_SUCCESS)
goto loser;
hostIndex = 0;
do {
hostIndex = PR_EnumerateHostEnt(hostIndex, &hostEntry, port, &addr);
if (hostIndex <= 0)
goto loser;
} while (PR_Connect(sock, &addr, timeout) != PR_SUCCESS);
PORT_Free(netdbbuf);
} else {
/*
* First put the port into the address, then connect.
*/
if (PR_InitializeNetAddr(PR_IpAddrNull, port, &addr) != PR_SUCCESS)
goto loser;
if (PR_Connect(sock, &addr, timeout) != PR_SUCCESS)
goto loser;
}
return sock;
loser:
if (sock != NULL)
PR_Close(sock);
if (netdbbuf != NULL)
PORT_Free(netdbbuf);
return NULL;
}
/*
* Sends an encoded OCSP request to the server identified by "location",
* and returns the socket on which it was sent (so can listen for the reply).
* "location" is expected to be a valid URL -- an error parsing it produces
* SEC_ERROR_CERT_BAD_ACCESS_LOCATION. Other errors are likely problems
* connecting to it, or writing to it, or allocating memory, and the low-level
* errors appropriate to the problem will be set.
*/
static PRFileDesc *
ocsp_SendEncodedRequest(char *location, SECItem *encodedRequest)
{
char *hostname = NULL;
char *path = NULL;
PRUint16 port;
SECStatus rv;
PRFileDesc *sock = NULL;
PRFileDesc *returnSock = NULL;
char *header = NULL;
/*
* Take apart the location, getting the hostname, port, and path.
*/
rv = ocsp_ParseURL(location, &hostname, &port, &path);
if (rv != SECSuccess)
goto loser;
PORT_Assert(hostname != NULL);
PORT_Assert(path != NULL);
sock = ocsp_ConnectToHost(hostname, port);
if (sock == NULL)
goto loser;
header = PR_smprintf("POST %s HTTP/1.0\r\n"
"Host: %s:%d\r\n"
"Content-Type: application/ocsp-request\r\n"
"Content-Length: %u\r\n\r\n",
path, hostname, port, encodedRequest->len);
if (header == NULL)
goto loser;
/*
* The NSPR documentation promises that if it can, it will write the full
* amount; this will not return a partial value expecting us to loop.
*/
if (PR_Write(sock, header, (PRInt32) PORT_Strlen(header)) < 0)
goto loser;
if (PR_Write(sock, encodedRequest->data,
(PRInt32) encodedRequest->len) < 0)
goto loser;
returnSock = sock;
sock = NULL;
loser:
if (header != NULL)
PORT_Free(header);
if (sock != NULL)
PR_Close(sock);
if (path != NULL)
PORT_Free(path);
if (hostname != NULL)
PORT_Free(hostname);
return returnSock;
}
/*
* Read from "fd" into "buf" -- expect/attempt to read a given number of bytes
* Obviously, stop if hit end-of-stream. Timeout is passed in.
*/
static int
ocsp_read(PRFileDesc *fd, char *buf, int toread, PRIntervalTime timeout)
{
int total = 0;
while (total < toread)
{
PRInt32 got;
got = PR_Recv(fd, buf + total, (PRInt32) (toread - total), 0, timeout);
if (got < 0)
{
if (0 == total)
{
total = -1; /* report the error if we didn't read anything yet */
}
break;
}
else
if (got == 0)
{ /* EOS */
break;
}
total += got;
}
return total;
}
#define OCSP_BUFSIZE 1024
#define AbortHttpDecode(error) \
{ \
if (inBuffer) \
PORT_Free(inBuffer); \
PORT_SetError(error); \
return NULL; \
}
/*
* Reads on the given socket and returns an encoded response when received.
* Properly formatted HTTP/1.0 response headers are expected to be read
* from the socket, preceding a binary-encoded OCSP response. Problems
* with parsing cause the error SEC_ERROR_OCSP_BAD_HTTP_RESPONSE to be
* set; any other problems are likely low-level i/o or memory allocation
* errors.
*/
static SECItem *
ocsp_GetEncodedResponse(PRArenaPool *arena, PRFileDesc *sock)
{
/* first read HTTP status line and headers */
char* inBuffer = NULL;
PRInt32 offset = 0;
PRInt32 inBufsize = 0;
const PRInt32 bufSizeIncrement = OCSP_BUFSIZE; /* 1 KB at a time */
const PRInt32 maxBufSize = 8 * bufSizeIncrement ; /* 8 KB max */
const char* CRLF = "\r\n";
const PRInt32 CRLFlen = strlen(CRLF);
const char* headerEndMark = "\r\n\r\n";
const PRInt32 markLen = strlen(headerEndMark);
const PRIntervalTime ocsptimeout =
PR_SecondsToInterval(30); /* hardcoded to 30s for now */
char* headerEnd = NULL;
PRBool EOS = PR_FALSE;
const char* httpprotocol = "HTTP/";
const PRInt32 httplen = strlen(httpprotocol);
const char* httpcode = NULL;
const char* contenttype = NULL;
PRInt32 contentlength = 0;
PRInt32 bytesRead = 0;
char* statusLineEnd = NULL;
char* space = NULL;
char* nextHeader = NULL;
SECItem* result = NULL;
/* read up to at least the end of the HTTP headers */
do
{
inBufsize += bufSizeIncrement;
inBuffer = PORT_Realloc(inBuffer, inBufsize+1);
if (NULL == inBuffer)
{
AbortHttpDecode(SEC_ERROR_NO_MEMORY);
}
bytesRead = ocsp_read(sock, inBuffer + offset, bufSizeIncrement,
ocsptimeout);
if (bytesRead > 0)
{
PRInt32 searchOffset = (offset - markLen) >0 ? offset-markLen : 0;
offset += bytesRead;
*(inBuffer + offset) = '\0'; /* NULL termination */
headerEnd = strstr((const char*)inBuffer + searchOffset, headerEndMark);
if (bytesRead < bufSizeIncrement)
{
/* we read less data than requested, therefore we are at
EOS or there was a read error */
EOS = PR_TRUE;
}
}
else
{
/* recv error or EOS */
EOS = PR_TRUE;
}
} while ( (!headerEnd) && (PR_FALSE == EOS) &&
(inBufsize < maxBufSize) );
if (!headerEnd)
{
AbortHttpDecode(SEC_ERROR_OCSP_BAD_HTTP_RESPONSE);
}
/* parse the HTTP status line */
statusLineEnd = strstr((const char*)inBuffer, CRLF);
if (!statusLineEnd)
{
AbortHttpDecode(SEC_ERROR_OCSP_BAD_HTTP_RESPONSE);
}
*statusLineEnd = '\0';
/* check for HTTP/ response */
space = strchr((const char*)inBuffer, ' ');
if (!space || PORT_Strncasecmp((const char*)inBuffer, httpprotocol, httplen) != 0 )
{
AbortHttpDecode(SEC_ERROR_OCSP_BAD_HTTP_RESPONSE);
}
/* check the HTTP status code of 200 */
httpcode = space +1;
space = strchr(httpcode, ' ');
if (!space)
{
AbortHttpDecode(SEC_ERROR_OCSP_BAD_HTTP_RESPONSE);
}
*space = 0;
if (0 != strcmp(httpcode, "200"))
{
AbortHttpDecode(SEC_ERROR_OCSP_BAD_HTTP_RESPONSE);
}
/* parse the HTTP headers in the buffer . We only care about
content-type and content-length
*/
nextHeader = statusLineEnd + CRLFlen;
*headerEnd = '\0'; /* terminate */
do
{
char* thisHeaderEnd = NULL;
char* value = NULL;
char* colon = strchr(nextHeader, ':');
if (!colon)
{
AbortHttpDecode(SEC_ERROR_OCSP_BAD_HTTP_RESPONSE);
}
*colon = '\0';
value = colon + 1;
/* jpierre - note : the following code will only handle the basic form
of HTTP/1.0 response headers, of the form "name: value" . Headers
split among multiple lines are not supported. This is not common
and should not be an issue, but it could become one in the
future */
if (*value != ' ')
{
AbortHttpDecode(SEC_ERROR_OCSP_BAD_HTTP_RESPONSE);
}
value++;
thisHeaderEnd = strstr(value, CRLF);
if (thisHeaderEnd )
{
*thisHeaderEnd = '\0';
}
if (0 == PORT_Strcasecmp(nextHeader, "content-type"))
{
contenttype = value;
}
else
if (0 == PORT_Strcasecmp(nextHeader, "content-length"))
{
contentlength = atoi(value);
}
if (thisHeaderEnd )
{
nextHeader = thisHeaderEnd + CRLFlen;
}
else
{
nextHeader = NULL;
}
} while (nextHeader && (nextHeader < (headerEnd + CRLFlen) ) );
/* check content-type */
if (!contenttype ||
(0 != PORT_Strcasecmp(contenttype, "application/ocsp-response")) )
{
AbortHttpDecode(SEC_ERROR_OCSP_BAD_HTTP_RESPONSE);
}
/* read the body of the OCSP response */
offset = offset - (PRInt32) (headerEnd - (const char*)inBuffer) - markLen;
if (offset)
{
/* move all data to the beginning of the buffer */
PORT_Memmove(inBuffer, headerEnd + markLen, offset);
}
/* resize buffer to only what's needed to hold the current response */
inBufsize = (1 + (offset-1) / bufSizeIncrement ) * bufSizeIncrement ;
while ( (PR_FALSE == EOS) &&
( (contentlength == 0) || (offset < contentlength) ) &&
(inBufsize < maxBufSize)
)
{
/* we still need to receive more body data */
inBufsize += bufSizeIncrement;
inBuffer = PORT_Realloc(inBuffer, inBufsize+1);
if (NULL == inBuffer)
{
AbortHttpDecode(SEC_ERROR_NO_MEMORY);
}
bytesRead = ocsp_read(sock, inBuffer + offset, bufSizeIncrement,
ocsptimeout);
if (bytesRead > 0)
{
offset += bytesRead;
if (bytesRead < bufSizeIncrement)
{
/* we read less data than requested, therefore we are at
EOS or there was a read error */
EOS = PR_TRUE;
}
}
else
{
/* recv error or EOS */
EOS = PR_TRUE;
}
}
if (0 == offset)
{
AbortHttpDecode(SEC_ERROR_OCSP_BAD_HTTP_RESPONSE);
}
/*
* Now allocate the item to hold the data.
*/
result = SECITEM_AllocItem(arena, NULL, offset);
if (NULL == result)
{
AbortHttpDecode(SEC_ERROR_NO_MEMORY);
}
/*
* And copy the data left in the buffer.
*/
PORT_Memcpy(result->data, inBuffer, offset);
/* and free the temporary buffer */
PORT_Free(inBuffer);
return result;
}
SECStatus
CERT_ParseURL(const char *url, char **pHostname, PRUint16 *pPort, char **pPath)
{
return ocsp_ParseURL(url, pHostname, pPort, pPath);
}
/*
* Limit the size of http responses we are willing to accept.
*/
#define MAX_WANTED_OCSP_RESPONSE_LEN 64*1024
static SECItem *
fetchOcspHttpClientV1(PRArenaPool *arena,
const SEC_HttpClientFcnV1 *hcv1,
char *location,
SECItem *encodedRequest)
{
char *hostname = NULL;
char *path = NULL;
PRUint16 port;
SECItem *encodedResponse = NULL;
SEC_HTTP_SERVER_SESSION pServerSession = NULL;
SEC_HTTP_REQUEST_SESSION pRequestSession = NULL;
PRUint16 myHttpResponseCode;
const char *myHttpResponseData;
PRUint32 myHttpResponseDataLen;
if (ocsp_ParseURL(location, &hostname, &port, &path) == SECFailure) {
PORT_SetError(SEC_ERROR_OCSP_MALFORMED_REQUEST);
goto loser;
}
PORT_Assert(hostname != NULL);
PORT_Assert(path != NULL);
if ((*hcv1->createSessionFcn)(
hostname,
port,
&pServerSession) != SECSuccess) {
PORT_SetError(SEC_ERROR_OCSP_SERVER_ERROR);
goto loser;
}
/* We use a non-zero timeout, which means:
- the client will use blocking I/O
- TryFcn will not return WOULD_BLOCK nor a poll descriptor
- it's sufficient to call TryFcn once
No lock for accessing OCSP_Global.timeoutSeconds, bug 406120
*/
if ((*hcv1->createFcn)(
pServerSession,
"http",
path,
"POST",
PR_TicksPerSecond() * OCSP_Global.timeoutSeconds,
&pRequestSession) != SECSuccess) {
PORT_SetError(SEC_ERROR_OCSP_SERVER_ERROR);
goto loser;
}
if ((*hcv1->setPostDataFcn)(
pRequestSession,
(char*)encodedRequest->data,
encodedRequest->len,
"application/ocsp-request") != SECSuccess) {
PORT_SetError(SEC_ERROR_OCSP_SERVER_ERROR);
goto loser;
}
/* we don't want result objects larger than this: */
myHttpResponseDataLen = MAX_WANTED_OCSP_RESPONSE_LEN;
OCSP_TRACE(("OCSP trySendAndReceive %s\n", location));
if ((*hcv1->trySendAndReceiveFcn)(
pRequestSession,
NULL,
&myHttpResponseCode,
NULL,
NULL,
&myHttpResponseData,
&myHttpResponseDataLen) != SECSuccess) {
PORT_SetError(SEC_ERROR_OCSP_SERVER_ERROR);
goto loser;
}
OCSP_TRACE(("OCSP trySendAndReceive result http %d\n", myHttpResponseCode));
if (myHttpResponseCode != 200) {
PORT_SetError(SEC_ERROR_OCSP_BAD_HTTP_RESPONSE);
goto loser;
}
encodedResponse = SECITEM_AllocItem(arena, NULL, myHttpResponseDataLen);
if (!encodedResponse) {
PORT_SetError(SEC_ERROR_NO_MEMORY);
goto loser;
}
PORT_Memcpy(encodedResponse->data, myHttpResponseData, myHttpResponseDataLen);
loser:
if (pRequestSession != NULL)
(*hcv1->freeFcn)(pRequestSession);
if (pServerSession != NULL)
(*hcv1->freeSessionFcn)(pServerSession);
if (path != NULL)
PORT_Free(path);
if (hostname != NULL)
PORT_Free(hostname);
return encodedResponse;
}
/*
* FUNCTION: CERT_GetEncodedOCSPResponse
* Creates and sends a request to an OCSP responder, then reads and
* returns the (encoded) response.
* INPUTS:
* PRArenaPool *arena
* Pointer to arena from which return value will be allocated.
* If NULL, result will be allocated from the heap (and thus should
* be freed via SECITEM_FreeItem).
* CERTCertList *certList
* A list of certs for which status will be requested.
* Note that all of these certificates should have the same issuer,
* or it's expected the response will be signed by a trusted responder.
* If the certs need to be broken up into multiple requests, that
* must be handled by the caller (and thus by having multiple calls
* to this routine), who knows about where the request(s) are being
* sent and whether there are any trusted responders in place.
* char *location
* The location of the OCSP responder (a URL).
* int64 time
* Indicates the time for which the certificate status is to be
* determined -- this may be used in the search for the cert's issuer
* but has no other bearing on the operation.
* PRBool addServiceLocator
* If true, the Service Locator extension should be added to the
* single request(s) for each cert.
* CERTCertificate *signerCert
* If non-NULL, means sign the request using this cert. Otherwise,
* do not sign.
* void *pwArg
* Pointer to argument for password prompting, if needed. (Definitely
* not needed if not signing.)
* OUTPUTS:
* CERTOCSPRequest **pRequest
* Pointer in which to store the OCSP request created for the given
* list of certificates. It is only filled in if the entire operation
* is successful and the pointer is not null -- and in that case the
* caller is then reponsible for destroying it.
* RETURN:
* Returns a pointer to the SECItem holding the response.
* On error, returns null with error set describing the reason:
* SEC_ERROR_UNKNOWN_ISSUER
* SEC_ERROR_CERT_BAD_ACCESS_LOCATION
* SEC_ERROR_OCSP_BAD_HTTP_RESPONSE
* Other errors are low-level problems (no memory, bad database, etc.).
*/
SECItem *
CERT_GetEncodedOCSPResponse(PRArenaPool *arena, CERTCertList *certList,
char *location, int64 time,
PRBool addServiceLocator,
CERTCertificate *signerCert, void *pwArg,
CERTOCSPRequest **pRequest)
{
CERTOCSPRequest *request;
request = CERT_CreateOCSPRequest(certList, time, addServiceLocator,
signerCert);
if (!request)
return NULL;
return ocsp_GetEncodedOCSPResponseFromRequest(arena, request, location,
time, addServiceLocator,
pwArg, pRequest);
}
static SECItem *
ocsp_GetEncodedOCSPResponseFromRequest(PRArenaPool *arena,
CERTOCSPRequest *request,
char *location, int64 time,
PRBool addServiceLocator,
void *pwArg,
CERTOCSPRequest **pRequest)
{
SECItem *encodedRequest = NULL;
SECItem *encodedResponse = NULL;
PRFileDesc *sock = NULL;
SECStatus rv;
const SEC_HttpClientFcn *registeredHttpClient = NULL;
rv = CERT_AddOCSPAcceptableResponses(request,
SEC_OID_PKIX_OCSP_BASIC_RESPONSE);
if (rv != SECSuccess)
goto loser;
encodedRequest = CERT_EncodeOCSPRequest(NULL, request, pwArg);
if (encodedRequest == NULL)
goto loser;
registeredHttpClient = SEC_GetRegisteredHttpClient();
if (registeredHttpClient
&&
registeredHttpClient->version == 1) {
encodedResponse = fetchOcspHttpClientV1(
arena,
®isteredHttpClient->fcnTable.ftable1,
location,
encodedRequest);
}
else {
/* use internal http client */
sock = ocsp_SendEncodedRequest(location, encodedRequest);
if (sock == NULL)
goto loser;
encodedResponse = ocsp_GetEncodedResponse(arena, sock);
}
if (encodedResponse != NULL && pRequest != NULL) {
*pRequest = request;
request = NULL; /* avoid destroying below */
}
loser:
if (request != NULL)
CERT_DestroyOCSPRequest(request);
if (encodedRequest != NULL)
SECITEM_FreeItem(encodedRequest, PR_TRUE);
if (sock != NULL)
PR_Close(sock);
return encodedResponse;
}
static SECItem *
ocsp_GetEncodedOCSPResponseForSingleCert(PRArenaPool *arena,
CERTOCSPCertID *certID,
CERTCertificate *singleCert,
char *location, int64 time,
PRBool addServiceLocator,
void *pwArg,
CERTOCSPRequest **pRequest)
{
CERTOCSPRequest *request;
request = cert_CreateSingleCertOCSPRequest(certID, singleCert, time,
addServiceLocator, NULL);
if (!request)
return NULL;
return ocsp_GetEncodedOCSPResponseFromRequest(arena, request, location,
time, addServiceLocator,
pwArg, pRequest);
}
/* Checks a certificate for the key usage extension of OCSP signer. */
static PRBool
ocsp_CertIsOCSPDesignatedResponder(CERTCertificate *cert)
{
SECStatus rv;
SECItem extItem;
SECItem **oids;
SECItem *oid;
SECOidTag oidTag;
PRBool retval;
CERTOidSequence *oidSeq = NULL;
extItem.data = NULL;
rv = CERT_FindCertExtension(cert, SEC_OID_X509_EXT_KEY_USAGE, &extItem);
if ( rv != SECSuccess ) {
goto loser;
}
oidSeq = CERT_DecodeOidSequence(&extItem);
if ( oidSeq == NULL ) {
goto loser;
}
oids = oidSeq->oids;
while ( *oids != NULL ) {
oid = *oids;
oidTag = SECOID_FindOIDTag(oid);
if ( oidTag == SEC_OID_OCSP_RESPONDER ) {
goto success;
}
oids++;
}
loser:
retval = PR_FALSE;
PORT_SetError(SEC_ERROR_OCSP_INVALID_SIGNING_CERT);
goto done;
success:
retval = PR_TRUE;
done:
if ( extItem.data != NULL ) {
PORT_Free(extItem.data);
}
if ( oidSeq != NULL ) {
CERT_DestroyOidSequence(oidSeq);
}
return(retval);
}
#ifdef LATER /*
* XXX This function is not currently used, but will
* be needed later when we do revocation checking of
* the responder certificate. Of course, it may need
* revising then, if the cert extension interface has
* changed. (Hopefully it will!)
*/
/* Checks a certificate to see if it has the OCSP no check extension. */
static PRBool
ocsp_CertHasNoCheckExtension(CERTCertificate *cert)
{
SECStatus rv;
rv = CERT_FindCertExtension(cert, SEC_OID_PKIX_OCSP_NO_CHECK,
NULL);
if (rv == SECSuccess) {
return PR_TRUE;
}
return PR_FALSE;
}
#endif /* LATER */
static PRBool
ocsp_matchcert(SECItem *certIndex,CERTCertificate *testCert)
{
SECItem item;
unsigned char buf[HASH_LENGTH_MAX];
item.data = buf;
item.len = SHA1_LENGTH;
if (CERT_GetSPKIDigest(NULL,testCert,SEC_OID_SHA1, &item) == NULL) {
return PR_FALSE;
}
if (SECITEM_ItemsAreEqual(certIndex,&item)) {
return PR_TRUE;
}
if (CERT_GetSPKIDigest(NULL,testCert,SEC_OID_MD5, &item) == NULL) {
return PR_FALSE;
}
if (SECITEM_ItemsAreEqual(certIndex,&item)) {
return PR_TRUE;
}
if (CERT_GetSPKIDigest(NULL,testCert,SEC_OID_MD2, &item) == NULL) {
return PR_FALSE;
}
if (SECITEM_ItemsAreEqual(certIndex,&item)) {
return PR_TRUE;
}
return PR_FALSE;
}
static CERTCertificate *
ocsp_CertGetDefaultResponder(CERTCertDBHandle *handle,CERTOCSPCertID *certID);
CERTCertificate *
ocsp_GetSignerCertificate(CERTCertDBHandle *handle, ocspResponseData *tbsData,
ocspSignature *signature, CERTCertificate *issuer)
{
CERTCertificate **certs = NULL;
CERTCertificate *signerCert = NULL;
SECStatus rv = SECFailure;
PRBool lookupByName = PR_TRUE;
void *certIndex = NULL;
int certCount = 0;
PORT_Assert(tbsData->responderID != NULL);
switch (tbsData->responderID->responderIDType) {
case ocspResponderID_byName:
lookupByName = PR_TRUE;
certIndex = &tbsData->derResponderID;
break;
case ocspResponderID_byKey:
lookupByName = PR_FALSE;
certIndex = &tbsData->responderID->responderIDValue.keyHash;
break;
case ocspResponderID_other:
default:
PORT_Assert(0);
PORT_SetError(SEC_ERROR_OCSP_MALFORMED_RESPONSE);
return NULL;
}
/*
* If the signature contains some certificates as well, temporarily
* import them in case they are needed for verification.
*
* Note that the result of this is that each cert in "certs" needs
* to be destroyed.
*/
if (signature->derCerts != NULL) {
for (; signature->derCerts[certCount] != NULL; certCount++) {
/* just counting */
}
rv = CERT_ImportCerts(handle, certUsageStatusResponder, certCount,
signature->derCerts, &certs,
PR_FALSE, PR_FALSE, NULL);
if (rv != SECSuccess)
goto finish;
}
/*
* Now look up the certificate that did the signing.
* The signer can be specified either by name or by key hash.
*/
if (lookupByName) {
SECItem *crIndex = (SECItem*)certIndex;
SECItem encodedName;
PLArenaPool *arena;
arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
if (arena != NULL) {
rv = SEC_QuickDERDecodeItem(arena, &encodedName,
ocsp_ResponderIDDerNameTemplate,
crIndex);
if (rv != SECSuccess) {
if (PORT_GetError() == SEC_ERROR_BAD_DER)
PORT_SetError(SEC_ERROR_OCSP_MALFORMED_RESPONSE);
} else {
signerCert = CERT_FindCertByName(handle, &encodedName);
}
PORT_FreeArena(arena, PR_FALSE);
}
} else {
/*
* The signer is either 1) a known issuer CA we passed in,
* 2) the default OCSP responder, or 3) an intermediate CA
* passed in the cert list to use. Figure out which it is.
*/
int i;
CERTCertificate *responder =
ocsp_CertGetDefaultResponder(handle, NULL);
if (responder && ocsp_matchcert(certIndex,responder)) {
signerCert = CERT_DupCertificate(responder);
} else if (issuer && ocsp_matchcert(certIndex,issuer)) {
signerCert = CERT_DupCertificate(issuer);
}
for (i=0; (signerCert == NULL) && (i < certCount); i++) {
if (ocsp_matchcert(certIndex,certs[i])) {
signerCert = CERT_DupCertificate(certs[i]);
}
}
}
finish:
if (certs != NULL) {
CERT_DestroyCertArray(certs, certCount);
}
return signerCert;
}
SECStatus
ocsp_VerifyResponseSignature(CERTCertificate *signerCert,
ocspSignature *signature,
SECItem *tbsResponseDataDER,
void *pwArg)
{
SECItem rawSignature;
SECKEYPublicKey *signerKey = NULL;
SECStatus rv = SECFailure;
/*
* Now get the public key from the signer's certificate; we need
* it to perform the verification.
*/
signerKey = CERT_ExtractPublicKey(signerCert);
if (signerKey == NULL)
return SECFailure;
/*
* We copy the signature data *pointer* and length, so that we can
* modify the length without damaging the original copy. This is a
* simple copy, not a dup, so no destroy/free is necessary.
*/
rawSignature = signature->signature;
/*
* The raw signature is a bit string, but we need to represent its
* length in bytes, because that is what the verify function expects.
*/
DER_ConvertBitString(&rawSignature);
rv = VFY_VerifyDataWithAlgorithmID(tbsResponseDataDER->data,
tbsResponseDataDER->len,
signerKey, &rawSignature,
&signature->signatureAlgorithm,
NULL, pwArg);
if (rv != SECSuccess && PORT_GetError() == SEC_ERROR_BAD_SIGNATURE) {
PORT_SetError(SEC_ERROR_OCSP_BAD_SIGNATURE);
}
if (signerKey != NULL) {
SECKEY_DestroyPublicKey(signerKey);
}
return rv;
}
/*
* FUNCTION: CERT_VerifyOCSPResponseSignature
* Check the signature on an OCSP Response. Will also perform a
* verification of the signer's certificate. Note, however, that a
* successful verification does not make any statement about the
* signer's *authority* to provide status for the certificate(s),
* that must be checked individually for each certificate.
* INPUTS:
* CERTOCSPResponse *response
* Pointer to response structure with signature to be checked.
* CERTCertDBHandle *handle
* Pointer to CERTCertDBHandle for certificate DB to use for verification.
* void *pwArg
* Pointer to argument for password prompting, if needed.
* OUTPUTS:
* CERTCertificate **pSignerCert
* Pointer in which to store signer's certificate; only filled-in if
* non-null.
* RETURN:
* Returns SECSuccess when signature is valid, anything else means invalid.
* Possible errors set:
* SEC_ERROR_OCSP_MALFORMED_RESPONSE - unknown type of ResponderID
* SEC_ERROR_INVALID_TIME - bad format of "ProducedAt" time
* SEC_ERROR_UNKNOWN_SIGNER - signer's cert could not be found
* SEC_ERROR_BAD_SIGNATURE - the signature did not verify
* Other errors are any of the many possible failures in cert verification
* (e.g. SEC_ERROR_REVOKED_CERTIFICATE, SEC_ERROR_UNTRUSTED_ISSUER) when
* verifying the signer's cert, or low-level problems (no memory, etc.)
*/
SECStatus
CERT_VerifyOCSPResponseSignature(CERTOCSPResponse *response,
CERTCertDBHandle *handle, void *pwArg,
CERTCertificate **pSignerCert,
CERTCertificate *issuer)
{
SECItem *tbsResponseDataDER;
CERTCertificate *signerCert = NULL;
SECStatus rv = SECFailure;
int64 producedAt;
/* ocsp_DecodeBasicOCSPResponse will fail if asn1 decoder is unable
* to properly decode tbsData (see the function and
* ocsp_BasicOCSPResponseTemplate). Thus, tbsData can not be
* equal to null */
ocspResponseData *tbsData = ocsp_GetResponseData(response,
&tbsResponseDataDER);
ocspSignature *signature = ocsp_GetResponseSignature(response);
if (!signature) {
PORT_SetError(SEC_ERROR_OCSP_BAD_SIGNATURE);
return SECFailure;
}
/*
* If this signature has already gone through verification, just
* return the cached result.
*/
if (signature->wasChecked) {
if (signature->status == SECSuccess) {
if (pSignerCert != NULL)
*pSignerCert = CERT_DupCertificate(signature->cert);
} else {
PORT_SetError(signature->failureReason);
}
return signature->status;
}
signerCert = ocsp_GetSignerCertificate(handle, tbsData,
signature, issuer);
if (signerCert == NULL) {
rv = SECFailure;
if (PORT_GetError() == SEC_ERROR_UNKNOWN_CERT) {
/* Make the error a little more specific. */
PORT_SetError(SEC_ERROR_OCSP_INVALID_SIGNING_CERT);
}
goto finish;
}
/*
* We could mark this true at the top of this function, or always
* below at "finish", but if the problem was just that we could not
* find the signer's cert, leave that as if the signature hasn't
* been checked in case a subsequent call might have better luck.
*/
signature->wasChecked = PR_TRUE;
/*
* The function will also verify the signer certificate; we
* need to tell it *when* that certificate must be valid -- for our
* purposes we expect it to be valid when the response was signed.
* The value of "producedAt" is the signing time.
*/
rv = DER_GeneralizedTimeToTime(&producedAt, &tbsData->producedAt);
if (rv != SECSuccess)
goto finish;
/*
* Just because we have a cert does not mean it is any good; check
* it for validity, trust and usage.
*/
if (ocsp_CertIsOCSPDefaultResponder(handle, signerCert)) {
rv = SECSuccess;
} else {
SECCertUsage certUsage;
if (CERT_IsCACert(signerCert, NULL)) {
certUsage = certUsageAnyCA;
} else {
certUsage = certUsageStatusResponder;
}
rv = CERT_VerifyCert(handle, signerCert, PR_TRUE,
certUsage, producedAt, pwArg, NULL);
if (rv != SECSuccess) {
PORT_SetError(SEC_ERROR_OCSP_INVALID_SIGNING_CERT);
goto finish;
}
}
rv = ocsp_VerifyResponseSignature(signerCert, signature,
tbsResponseDataDER,
pwArg);
finish:
if (signature->wasChecked)
signature->status = rv;
if (rv != SECSuccess) {
signature->failureReason = PORT_GetError();
if (signerCert != NULL)
CERT_DestroyCertificate(signerCert);
} else {
/*
* Save signer's certificate in signature.
*/
signature->cert = signerCert;
if (pSignerCert != NULL) {
/*
* Pass pointer to signer's certificate back to our caller,
* who is also now responsible for destroying it.
*/
*pSignerCert = CERT_DupCertificate(signerCert);
}
}
return rv;
}
/*
* See if the request's certID and the single response's certID match.
* This can be easy or difficult, depending on whether the same hash
* algorithm was used.
*/
static PRBool
ocsp_CertIDsMatch(CERTCertDBHandle *handle,
CERTOCSPCertID *requestCertID,
CERTOCSPCertID *responseCertID)
{
PRBool match = PR_FALSE;
SECOidTag hashAlg;
SECItem *keyHash = NULL;
SECItem *nameHash = NULL;
/*
* In order to match, they must have the same issuer and the same
* serial number.
*
* We just compare the easier things first.
*/
if (SECITEM_CompareItem(&requestCertID->serialNumber,
&responseCertID->serialNumber) != SECEqual) {
goto done;
}
/*
* Make sure the "parameters" are not too bogus. Since we encoded
* requestCertID->hashAlgorithm, we don't need to check it.
*/
if (responseCertID->hashAlgorithm.parameters.len > 2) {
goto done;
}
if (SECITEM_CompareItem(&requestCertID->hashAlgorithm.algorithm,
&responseCertID->hashAlgorithm.algorithm) == SECEqual) {
/*
* If the hash algorithms match then we can do a simple compare
* of the hash values themselves.
*/
if ((SECITEM_CompareItem(&requestCertID->issuerNameHash,
&responseCertID->issuerNameHash) == SECEqual)
&& (SECITEM_CompareItem(&requestCertID->issuerKeyHash,
&responseCertID->issuerKeyHash) == SECEqual)) {
match = PR_TRUE;
}
goto done;
}
hashAlg = SECOID_FindOIDTag(&responseCertID->hashAlgorithm.algorithm);
switch (hashAlg) {
case SEC_OID_SHA1:
keyHash = &requestCertID->issuerSHA1KeyHash;
nameHash = &requestCertID->issuerSHA1NameHash;
break;
case SEC_OID_MD5:
keyHash = &requestCertID->issuerMD5KeyHash;
nameHash = &requestCertID->issuerMD5NameHash;
break;
case SEC_OID_MD2:
keyHash = &requestCertID->issuerMD2KeyHash;
nameHash = &requestCertID->issuerMD2NameHash;
break;
default:
PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);
return SECFailure;
}
if ((keyHash != NULL)
&& (SECITEM_CompareItem(nameHash,
&responseCertID->issuerNameHash) == SECEqual)
&& (SECITEM_CompareItem(keyHash,
&responseCertID->issuerKeyHash) == SECEqual)) {
match = PR_TRUE;
}
done:
return match;
}
/*
* Find the single response for the cert specified by certID.
* No copying is done; this just returns a pointer to the appropriate
* response within responses, if it is found (and null otherwise).
* This is fine, of course, since this function is internal-use only.
*/
static CERTOCSPSingleResponse *
ocsp_GetSingleResponseForCertID(CERTOCSPSingleResponse **responses,
CERTCertDBHandle *handle,
CERTOCSPCertID *certID)
{
CERTOCSPSingleResponse *single;
int i;
if (responses == NULL)
return NULL;
for (i = 0; responses[i] != NULL; i++) {
single = responses[i];
if (ocsp_CertIDsMatch(handle, certID, single->certID)) {
return single;
}
}
/*
* The OCSP server should have included a response even if it knew
* nothing about the certificate in question. Since it did not,
* this will make it look as if it had.
*
* XXX Should we make this a separate error to notice the server's
* bad behavior?
*/
PORT_SetError(SEC_ERROR_OCSP_UNKNOWN_CERT);
return NULL;
}
static ocspCheckingContext *
ocsp_GetCheckingContext(CERTCertDBHandle *handle)
{
CERTStatusConfig *statusConfig;
ocspCheckingContext *ocspcx = NULL;
statusConfig = CERT_GetStatusConfig(handle);
if (statusConfig != NULL) {
ocspcx = statusConfig->statusContext;
/*
* This is actually an internal error, because we should never
* have a good statusConfig without a good statusContext, too.
* For lack of anything better, though, we just assert and use
* the same error as if there were no statusConfig (set below).
*/
PORT_Assert(ocspcx != NULL);
}
if (ocspcx == NULL)
PORT_SetError(SEC_ERROR_OCSP_NOT_ENABLED);
return ocspcx;
}
/*
* Return cert reference if the given signerCert is the default responder for
* the given certID. If not, or if any error, return NULL.
*/
static CERTCertificate *
ocsp_CertGetDefaultResponder(CERTCertDBHandle *handle, CERTOCSPCertID *certID)
{
ocspCheckingContext *ocspcx;
ocspcx = ocsp_GetCheckingContext(handle);
if (ocspcx == NULL)
goto loser;
/*
* Right now we have only one default responder. It applies to
* all certs when it is used, so the check is simple and certID
* has no bearing on the answer. Someday in the future we may
* allow configuration of different responders for different
* issuers, and then we would have to use the issuer specified
* in certID to determine if signerCert is the right one.
*/
if (ocspcx->useDefaultResponder) {
PORT_Assert(ocspcx->defaultResponderCert != NULL);
return ocspcx->defaultResponderCert;
}
loser:
return NULL;
}
/*
* Return true if the cert is one of the default responders configured for
* ocsp context. If not, or if any error, return false.
*/
PRBool
ocsp_CertIsOCSPDefaultResponder(CERTCertDBHandle *handle, CERTCertificate *cert)
{
ocspCheckingContext *ocspcx;
ocspcx = ocsp_GetCheckingContext(handle);
if (ocspcx == NULL)
return PR_FALSE;
/*
* Right now we have only one default responder. It applies to
* all certs when it is used, so the check is simple and certID
* has no bearing on the answer. Someday in the future we may
* allow configuration of different responders for different
* issuers, and then we would have to use the issuer specified
* in certID to determine if signerCert is the right one.
*/
if (ocspcx->useDefaultResponder &&
CERT_CompareCerts(ocspcx->defaultResponderCert, cert)) {
return PR_TRUE;
}
return PR_FALSE;
}
/*
* Check that the given signer certificate is authorized to sign status
* information for the given certID. Return true if it is, false if not
* (or if there is any error along the way). If false is returned because
* the signer is not authorized, the following error will be set:
* SEC_ERROR_OCSP_UNAUTHORIZED_RESPONSE
* Other errors are low-level problems (no memory, bad database, etc.).
*
* There are three ways to be authorized. In the order in which we check,
* using the terms used in the OCSP spec, the signer must be one of:
* 1. A "trusted responder" -- it matches a local configuration
* of OCSP signing authority for the certificate in question.
* 2. The CA who issued the certificate in question.
* 3. A "CA designated responder", aka an "authorized responder" -- it
* must be represented by a special cert issued by the CA who issued
* the certificate in question.
*/
static PRBool
ocsp_AuthorizedResponderForCertID(CERTCertDBHandle *handle,
CERTCertificate *signerCert,
CERTOCSPCertID *certID,
int64 thisUpdate)
{
CERTCertificate *issuerCert = NULL, *defRespCert;
SECItem *keyHash = NULL;
SECItem *nameHash = NULL;
SECOidTag hashAlg;
PRBool keyHashEQ = PR_FALSE, nameHashEQ = PR_FALSE;
/*
* Check first for a trusted responder, which overrides everything else.
*/
if ((defRespCert = ocsp_CertGetDefaultResponder(handle, certID)) &&
CERT_CompareCerts(defRespCert, signerCert)) {
return PR_TRUE;
}
/*
* In the other two cases, we need to do an issuer comparison.
* How we do it depends on whether the signer certificate has the
* special extension (for a designated responder) or not.
*
* First, lets check if signer of the response is the actual issuer
* of the cert. For that we will use signer cert key hash and cert subj
* name hash and will compare them with already calculated issuer key
* hash and issuer name hash. The hash algorithm is picked from response
* certID hash to avoid second hash calculation.
*/
hashAlg = SECOID_FindOIDTag(&certID->hashAlgorithm.algorithm);
keyHash = CERT_GetSPKIDigest(NULL, signerCert, hashAlg, NULL);
if (keyHash != NULL) {
keyHashEQ =
(SECITEM_CompareItem(keyHash,
&certID->issuerKeyHash) == SECEqual);
SECITEM_FreeItem(keyHash, PR_TRUE);
}
if (keyHashEQ &&
(nameHash = cert_GetSubjectNameDigest(NULL, signerCert,
hashAlg, NULL))) {
nameHashEQ =
(SECITEM_CompareItem(nameHash,
&certID->issuerNameHash) == SECEqual);
SECITEM_FreeItem(nameHash, PR_TRUE);
if (nameHashEQ) {
/* The issuer of the cert is the the signer of the response */
return PR_TRUE;
}
}
keyHashEQ = PR_FALSE;
nameHashEQ = PR_FALSE;
if (!ocsp_CertIsOCSPDesignatedResponder(signerCert)) {
PORT_SetError(SEC_ERROR_OCSP_UNAUTHORIZED_RESPONSE);
return PR_FALSE;
}
/*
* The signer is a designated responder. Its issuer must match
* the issuer of the cert being checked.
*/
issuerCert = CERT_FindCertIssuer(signerCert, thisUpdate,
certUsageAnyCA);
if (issuerCert == NULL) {
/*
* We could leave the SEC_ERROR_UNKNOWN_ISSUER error alone,
* but the following will give slightly more information.
* Once we have an error stack, things will be much better.
*/
PORT_SetError(SEC_ERROR_OCSP_UNAUTHORIZED_RESPONSE);
return PR_FALSE;
}
keyHash = CERT_GetSPKIDigest(NULL, issuerCert, hashAlg, NULL);
nameHash = cert_GetSubjectNameDigest(NULL, issuerCert, hashAlg, NULL);
CERT_DestroyCertificate(issuerCert);
if (keyHash != NULL && nameHash != NULL) {
keyHashEQ =
(SECITEM_CompareItem(keyHash,
&certID->issuerKeyHash) == SECEqual);
nameHashEQ =
(SECITEM_CompareItem(nameHash,
&certID->issuerNameHash) == SECEqual);
}
if (keyHash) {
SECITEM_FreeItem(keyHash, PR_TRUE);
}
if (nameHash) {
SECITEM_FreeItem(nameHash, PR_TRUE);
}
if (keyHashEQ && nameHashEQ) {
return PR_TRUE;
}
PORT_SetError(SEC_ERROR_OCSP_UNAUTHORIZED_RESPONSE);
return PR_FALSE;
}
/*
* We need to check that a responder gives us "recent" information.
* Since a responder can pre-package responses, we need to pick an amount
* of time that is acceptable to us, and reject any response that is
* older than that.
*
* XXX This *should* be based on some configuration parameter, so that
* different usages could specify exactly what constitutes "sufficiently
* recent". But that is not going to happen right away. For now, we
* want something from within the last 24 hours. This macro defines that
* number in seconds.
*/
#define OCSP_ALLOWABLE_LAPSE_SECONDS (24L * 60L * 60L)
static PRBool
ocsp_TimeIsRecent(int64 checkTime)
{
int64 now = PR_Now();
int64 lapse, tmp;
LL_I2L(lapse, OCSP_ALLOWABLE_LAPSE_SECONDS);
LL_I2L(tmp, PR_USEC_PER_SEC);
LL_MUL(lapse, lapse, tmp); /* allowable lapse in microseconds */
LL_ADD(checkTime, checkTime, lapse);
if (LL_CMP(now, >, checkTime))
return PR_FALSE;
return PR_TRUE;
}
#define OCSP_SLOP (5L*60L) /* OCSP responses are allowed to be 5 minutes
in the future by default */
static PRUint32 ocspsloptime = OCSP_SLOP; /* seconds */
/*
* If an old response contains the revoked certificate status, we want
* to return SECSuccess so the response will be used.
*/
static SECStatus
ocsp_HandleOldSingleResponse(CERTOCSPSingleResponse *single, PRTime time)
{
SECStatus rv;
ocspCertStatus *status = single->certStatus;
if (status->certStatusType == ocspCertStatus_revoked) {
rv = ocsp_CertRevokedAfter(status->certStatusInfo.revokedInfo, time);
if (rv != SECSuccess &&
PORT_GetError() == SEC_ERROR_REVOKED_CERTIFICATE) {
/*
* Return SECSuccess now. The subsequent ocsp_CertRevokedAfter
* call in ocsp_CertHasGoodStatus will cause
* ocsp_CertHasGoodStatus to fail with
* SEC_ERROR_REVOKED_CERTIFICATE.
*/
return SECSuccess;
}
}
PORT_SetError(SEC_ERROR_OCSP_OLD_RESPONSE);
return SECFailure;
}
/*
* Check that this single response is okay. A return of SECSuccess means:
* 1. The signer (represented by "signerCert") is authorized to give status
* for the cert represented by the individual response in "single".
* 2. The value of thisUpdate is earlier than now.
* 3. The value of producedAt is later than or the same as thisUpdate.
* 4. If nextUpdate is given:
* - The value of nextUpdate is later than now.
* - The value of producedAt is earlier than nextUpdate.
* Else if no nextUpdate:
* - The value of thisUpdate is fairly recent.
* - The value of producedAt is fairly recent.
* However we do not need to perform an explicit check for this last
* constraint because it is already guaranteed by checking that
* producedAt is later than thisUpdate and thisUpdate is recent.
* Oh, and any responder is "authorized" to say that a cert is unknown to it.
*
* If any of those checks fail, SECFailure is returned and an error is set:
* SEC_ERROR_OCSP_FUTURE_RESPONSE
* SEC_ERROR_OCSP_OLD_RESPONSE
* SEC_ERROR_OCSP_UNAUTHORIZED_RESPONSE
* Other errors are low-level problems (no memory, bad database, etc.).
*/
static SECStatus
ocsp_VerifySingleResponse(CERTOCSPSingleResponse *single,
CERTCertDBHandle *handle,
CERTCertificate *signerCert,
int64 producedAt)
{
CERTOCSPCertID *certID = single->certID;
int64 now, thisUpdate, nextUpdate, tmstamp, tmp;
SECStatus rv;
OCSP_TRACE(("OCSP ocsp_VerifySingleResponse, nextUpdate: %d\n",
((single->nextUpdate) != 0)));
/*
* If all the responder said was that the given cert was unknown to it,
* that is a valid response. Not very interesting to us, of course,
* but all this function is concerned with is validity of the response,
* not the status of the cert.
*/
PORT_Assert(single->certStatus != NULL);
if (single->certStatus->certStatusType == ocspCertStatus_unknown)
return SECSuccess;
/*
* We need to extract "thisUpdate" for use below and to pass along
* to AuthorizedResponderForCertID in case it needs it for doing an
* issuer look-up.
*/
rv = DER_GeneralizedTimeToTime(&thisUpdate, &single->thisUpdate);
if (rv != SECSuccess)
return rv;
/*
* First confirm that signerCert is authorized to give this status.
*/
if (ocsp_AuthorizedResponderForCertID(handle, signerCert, certID,
thisUpdate) != PR_TRUE)
return SECFailure;
/*
* Now check the time stuff, as described above.
*/
now = PR_Now();
/* allow slop time for future response */
LL_UI2L(tmstamp, ocspsloptime); /* get slop time in seconds */
LL_UI2L(tmp, PR_USEC_PER_SEC);
LL_MUL(tmp, tmstamp, tmp); /* convert the slop time to PRTime */
LL_ADD(tmstamp, tmp, now); /* add current time to it */
if (LL_CMP(thisUpdate, >, tmstamp) || LL_CMP(producedAt, <, thisUpdate)) {
PORT_SetError(SEC_ERROR_OCSP_FUTURE_RESPONSE);
return SECFailure;
}
if (single->nextUpdate != NULL) {
rv = DER_GeneralizedTimeToTime(&nextUpdate, single->nextUpdate);
if (rv != SECSuccess)
return rv;
LL_ADD(tmp, tmp, nextUpdate);
if (LL_CMP(tmp, <, now) || LL_CMP(producedAt, >, nextUpdate))
return ocsp_HandleOldSingleResponse(single, now);
} else if (ocsp_TimeIsRecent(thisUpdate) != PR_TRUE) {
return ocsp_HandleOldSingleResponse(single, now);
}
return SECSuccess;
}
/*
* FUNCTION: CERT_GetOCSPAuthorityInfoAccessLocation
* Get the value of the URI of the OCSP responder for the given cert.
* This is found in the (optional) Authority Information Access extension
* in the cert.
* INPUTS:
* CERTCertificate *cert
* The certificate being examined.
* RETURN:
* char *
* A copy of the URI for the OCSP method, if found. If either the
* extension is not present or it does not contain an entry for OCSP,
* SEC_ERROR_CERT_BAD_ACCESS_LOCATION will be set and a NULL returned.
* Any other error will also result in a NULL being returned.
*
* This result should be freed (via PORT_Free) when no longer in use.
*/
char *
CERT_GetOCSPAuthorityInfoAccessLocation(CERTCertificate *cert)
{
CERTGeneralName *locname = NULL;
SECItem *location = NULL;
SECItem *encodedAuthInfoAccess = NULL;
CERTAuthInfoAccess **authInfoAccess = NULL;
char *locURI = NULL;
PRArenaPool *arena = NULL;
SECStatus rv;
int i;
/*
* Allocate this one from the heap because it will get filled in
* by CERT_FindCertExtension which will also allocate from the heap,
* and we can free the entire thing on our way out.
*/
encodedAuthInfoAccess = SECITEM_AllocItem(NULL, NULL, 0);
if (encodedAuthInfoAccess == NULL)
goto loser;
rv = CERT_FindCertExtension(cert, SEC_OID_X509_AUTH_INFO_ACCESS,
encodedAuthInfoAccess);
if (rv == SECFailure) {
PORT_SetError(SEC_ERROR_CERT_BAD_ACCESS_LOCATION);
goto loser;
}
/*
* The rest of the things allocated in the routine will come out of
* this arena, which is temporary just for us to decode and get at the
* AIA extension. The whole thing will be destroyed on our way out,
* after we have copied the location string (url) itself (if found).
*/
arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
if (arena == NULL)
goto loser;
authInfoAccess = CERT_DecodeAuthInfoAccessExtension(arena,
encodedAuthInfoAccess);
if (authInfoAccess == NULL)
goto loser;
for (i = 0; authInfoAccess[i] != NULL; i++) {
if (SECOID_FindOIDTag(&authInfoAccess[i]->method) == SEC_OID_PKIX_OCSP)
locname = authInfoAccess[i]->location;
}
/*
* If we found an AIA extension, but it did not include an OCSP method,
* that should look to our caller as if we did not find the extension
* at all, because it is only an OCSP method that we care about.
* So set the same error that would be set if the AIA extension was
* not there at all.
*/
if (locname == NULL) {
PORT_SetError(SEC_ERROR_CERT_BAD_ACCESS_LOCATION);
goto loser;
}
/*
* The following is just a pointer back into locname (i.e. not a copy);
* thus it should not be freed.
*/
location = CERT_GetGeneralNameByType(locname, certURI, PR_FALSE);
if (location == NULL) {
/*
* XXX Appears that CERT_GetGeneralNameByType does not set an
* error if there is no name by that type. For lack of anything
* better, act as if the extension was not found. In the future
* this should probably be something more like the extension was
* badly formed.
*/
PORT_SetError(SEC_ERROR_CERT_BAD_ACCESS_LOCATION);
goto loser;
}
/*
* That location is really a string, but it has a specified length
* without a null-terminator. We need a real string that does have
* a null-terminator, and we need a copy of it anyway to return to
* our caller -- so allocate and copy.
*/
locURI = PORT_Alloc(location->len + 1);
if (locURI == NULL) {
goto loser;
}
PORT_Memcpy(locURI, location->data, location->len);
locURI[location->len] = '\0';
loser:
if (arena != NULL)
PORT_FreeArena(arena, PR_FALSE);
if (encodedAuthInfoAccess != NULL)
SECITEM_FreeItem(encodedAuthInfoAccess, PR_TRUE);
return locURI;
}
/*
* Figure out where we should go to find out the status of the given cert
* via OCSP. If allowed to use a default responder uri and a default
* responder is set up, then that is our answer.
* If not, see if the certificate has an Authority Information Access (AIA)
* extension for OCSP, and return the value of that. Otherwise return NULL.
* We also let our caller know whether or not the responder chosen was
* a default responder or not through the output variable isDefault;
* its value has no meaning unless a good (non-null) value is returned
* for the location.
*
* The result needs to be freed (PORT_Free) when no longer in use.
*/
char *
ocsp_GetResponderLocation(CERTCertDBHandle *handle, CERTCertificate *cert,
PRBool canUseDefault, PRBool *isDefault)
{
ocspCheckingContext *ocspcx = NULL;
char *ocspUrl = NULL;
if (canUseDefault) {
ocspcx = ocsp_GetCheckingContext(handle);
}
if (ocspcx != NULL && ocspcx->useDefaultResponder) {
/*
* A default responder wins out, if specified.
* XXX Someday this may be a more complicated determination based
* on the cert's issuer. (That is, we could have different default
* responders configured for different issuers.)
*/
PORT_Assert(ocspcx->defaultResponderURI != NULL);
*isDefault = PR_TRUE;
return (PORT_Strdup(ocspcx->defaultResponderURI));
}
/*
* No default responder set up, so go see if we can find an AIA
* extension that has a value for OCSP, and get the url from that.
*/
*isDefault = PR_FALSE;
ocspUrl = CERT_GetOCSPAuthorityInfoAccessLocation(cert);
if (!ocspUrl) {
CERT_StringFromCertFcn altFcn;
PR_EnterMonitor(OCSP_Global.monitor);
altFcn = OCSP_Global.alternateOCSPAIAFcn;
PR_ExitMonitor(OCSP_Global.monitor);
if (altFcn) {
ocspUrl = (*altFcn)(cert);
if (ocspUrl)
*isDefault = PR_TRUE;
}
}
return ocspUrl;
}
/*
* Return SECSuccess if the cert was revoked *after* "time",
* SECFailure otherwise.
*/
static SECStatus
ocsp_CertRevokedAfter(ocspRevokedInfo *revokedInfo, int64 time)
{
int64 revokedTime;
SECStatus rv;
rv = DER_GeneralizedTimeToTime(&revokedTime, &revokedInfo->revocationTime);
if (rv != SECSuccess)
return rv;
/*
* Set the error even if we will return success; someone might care.
*/
PORT_SetError(SEC_ERROR_REVOKED_CERTIFICATE);
if (LL_CMP(revokedTime, >, time))
return SECSuccess;
return SECFailure;
}
/*
* See if the cert represented in the single response had a good status
* at the specified time.
*/
static SECStatus
ocsp_CertHasGoodStatus(ocspCertStatus *status, int64 time)
{
SECStatus rv;
switch (status->certStatusType) {
case ocspCertStatus_good:
rv = SECSuccess;
break;
case ocspCertStatus_revoked:
rv = ocsp_CertRevokedAfter(status->certStatusInfo.revokedInfo, time);
break;
case ocspCertStatus_unknown:
PORT_SetError(SEC_ERROR_OCSP_UNKNOWN_CERT);
rv = SECFailure;
break;
case ocspCertStatus_other:
default:
PORT_Assert(0);
PORT_SetError(SEC_ERROR_OCSP_MALFORMED_RESPONSE);
rv = SECFailure;
break;
}
return rv;
}
static SECStatus
ocsp_SingleResponseCertHasGoodStatus(CERTOCSPSingleResponse *single,
int64 time)
{
return ocsp_CertHasGoodStatus(single->certStatus, time);
}
/* Return value SECFailure means: not found or not fresh.
* On SECSuccess, the out parameters contain the OCSP status.
* rvOcsp contains the overall result of the OCSP operation.
* Depending on input parameter ignoreGlobalOcspFailureSetting,
* a soft failure might be converted into *rvOcsp=SECSuccess.
* If the cached attempt to obtain OCSP information had resulted
* in a failure, missingResponseError shows the error code of
* that failure.
*/
SECStatus
ocsp_GetCachedOCSPResponseStatusIfFresh(CERTOCSPCertID *certID,
int64 time,
PRBool ignoreGlobalOcspFailureSetting,
SECStatus *rvOcsp,
SECErrorCodes *missingResponseError)
{
OCSPCacheItem *cacheItem = NULL;
SECStatus rv = SECFailure;
if (!certID || !missingResponseError || !rvOcsp) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
*rvOcsp = SECFailure;
*missingResponseError = 0;
PR_EnterMonitor(OCSP_Global.monitor);
cacheItem = ocsp_FindCacheEntry(&OCSP_Global.cache, certID);
if (cacheItem && ocsp_IsCacheItemFresh(cacheItem)) {
/* having an arena means, we have a cached certStatus */
if (cacheItem->certStatusArena) {
*rvOcsp = ocsp_CertHasGoodStatus(&cacheItem->certStatus, time);
if (*rvOcsp != SECSuccess) {
*missingResponseError = PORT_GetError();
}
rv = SECSuccess;
} else {
/*
* No status cached, the previous attempt failed.
* If OCSP is required, we never decide based on a failed attempt
* However, if OCSP is optional, a recent OCSP failure is
* an allowed good state.
*/
if (!ignoreGlobalOcspFailureSetting &&
OCSP_Global.ocspFailureMode ==
ocspMode_FailureIsNotAVerificationFailure) {
rv = SECSuccess;
*rvOcsp = SECSuccess;
}
*missingResponseError = cacheItem->missingResponseError;
}
}
PR_ExitMonitor(OCSP_Global.monitor);
return rv;
}
PRBool
ocsp_FetchingFailureIsVerificationFailure()
{
PRBool isFailure;
PR_EnterMonitor(OCSP_Global.monitor);
isFailure =
OCSP_Global.ocspFailureMode == ocspMode_FailureIsVerificationFailure;
PR_ExitMonitor(OCSP_Global.monitor);
return isFailure;
}
/*
* FUNCTION: CERT_CheckOCSPStatus
* Checks the status of a certificate via OCSP. Will only check status for
* a certificate that has an AIA (Authority Information Access) extension
* for OCSP *or* when a "default responder" is specified and enabled.
* (If no AIA extension for OCSP and no default responder in place, the
* cert is considered to have a good status and SECSuccess is returned.)
* INPUTS:
* CERTCertDBHandle *handle
* certificate DB of the cert that is being checked
* CERTCertificate *cert
* the certificate being checked
* XXX in the long term also need a boolean parameter that specifies
* whether to check the cert chain, as well; for now we check only
* the leaf (the specified certificate)
* int64 time
* time for which status is to be determined
* void *pwArg
* argument for password prompting, if needed
* RETURN:
* Returns SECSuccess if an approved OCSP responder "knows" the cert
* *and* returns a non-revoked status for it; SECFailure otherwise,
* with an error set describing the reason:
*
* SEC_ERROR_OCSP_BAD_HTTP_RESPONSE
* SEC_ERROR_OCSP_FUTURE_RESPONSE
* SEC_ERROR_OCSP_MALFORMED_REQUEST
* SEC_ERROR_OCSP_MALFORMED_RESPONSE
* SEC_ERROR_OCSP_OLD_RESPONSE
* SEC_ERROR_OCSP_REQUEST_NEEDS_SIG
* SEC_ERROR_OCSP_SERVER_ERROR
* SEC_ERROR_OCSP_TRY_SERVER_LATER
* SEC_ERROR_OCSP_UNAUTHORIZED_REQUEST
* SEC_ERROR_OCSP_UNAUTHORIZED_RESPONSE
* SEC_ERROR_OCSP_UNKNOWN_CERT
* SEC_ERROR_OCSP_UNKNOWN_RESPONSE_STATUS
* SEC_ERROR_OCSP_UNKNOWN_RESPONSE_TYPE
*
* SEC_ERROR_BAD_SIGNATURE
* SEC_ERROR_CERT_BAD_ACCESS_LOCATION
* SEC_ERROR_INVALID_TIME
* SEC_ERROR_REVOKED_CERTIFICATE
* SEC_ERROR_UNKNOWN_ISSUER
* SEC_ERROR_UNKNOWN_SIGNER
*
* Other errors are any of the many possible failures in cert verification
* (e.g. SEC_ERROR_REVOKED_CERTIFICATE, SEC_ERROR_UNTRUSTED_ISSUER) when
* verifying the signer's cert, or low-level problems (error allocating
* memory, error performing ASN.1 decoding, etc.).
*/
SECStatus
CERT_CheckOCSPStatus(CERTCertDBHandle *handle, CERTCertificate *cert,
int64 time, void *pwArg)
{
CERTOCSPCertID *certID;
PRBool certIDWasConsumed = PR_FALSE;
SECStatus rv = SECFailure;
SECStatus rvOcsp;
SECErrorCodes dummy_error_code; /* we ignore this */
OCSP_TRACE_CERT(cert);
OCSP_TRACE_TIME("## requested validity time:", time);
certID = CERT_CreateOCSPCertID(cert, time);
if (!certID)
return SECFailure;
rv = ocsp_GetCachedOCSPResponseStatusIfFresh(
certID, time, PR_FALSE, /* ignoreGlobalOcspFailureSetting */
&rvOcsp, &dummy_error_code);
if (rv == SECSuccess) {
CERT_DestroyOCSPCertID(certID);
return rvOcsp;
}
rv = ocsp_GetOCSPStatusFromNetwork(handle, certID, cert, time, pwArg,
&certIDWasConsumed,
&rvOcsp);
if (rv != SECSuccess) {
/* we were unable to obtain ocsp status. Check if we should
* return cert status revoked. */
rvOcsp = ocsp_FetchingFailureIsVerificationFailure() ?
SECFailure : SECSuccess;
}
if (!certIDWasConsumed) {
CERT_DestroyOCSPCertID(certID);
}
return rvOcsp;
}
/*
* FUNCTION: CERT_CacheOCSPResponseFromSideChannel
* First, this function checks the OCSP cache to see if a good response
* for the given certificate already exists. If it does, then the function
* returns successfully.
*
* If not, then it validates that the given OCSP response is a valid,
* good response for the given certificate and inserts it into the
* cache.
*
* This function is intended for use when OCSP responses are provided via a
* side-channel, i.e. TLS OCSP stapling (a.k.a. the status_request extension).
*
* INPUTS:
* CERTCertDBHandle *handle
* certificate DB of the cert that is being checked
* CERTCertificate *cert
* the certificate being checked
* int64 time
* time for which status is to be determined
* SECItem *encodedResponse
* the DER encoded bytes of the OCSP response
* void *pwArg
* argument for password prompting, if needed
* RETURN:
* SECSuccess if the cert was found in the cache, or if the OCSP response was
* found to be valid and inserted into the cache. SECFailure otherwise.
*/
SECStatus
CERT_CacheOCSPResponseFromSideChannel(CERTCertDBHandle *handle,
CERTCertificate *cert,
int64 time,
SECItem *encodedResponse,
void *pwArg)
{
CERTOCSPCertID *certID;
PRBool certIDWasConsumed = PR_FALSE;
SECStatus rv = SECFailure;
SECStatus rvOcsp;
SECErrorCodes dummy_error_code; /* we ignore this */
certID = CERT_CreateOCSPCertID(cert, time);
if (!certID)
return SECFailure;
rv = ocsp_GetCachedOCSPResponseStatusIfFresh(
certID, time, PR_FALSE, /* ignoreGlobalOcspFailureSetting */
&rvOcsp, &dummy_error_code);
if (rv == SECSuccess && rvOcsp == SECSuccess) {
/* The cached value is good. We don't want to waste time validating
* this OCSP response. */
CERT_DestroyOCSPCertID(certID);
return rv;
}
/* Since the OCSP response came from a side channel it is attacker
* controlled. The attacker can have chosen any valid OCSP response,
* including responses from the past. In this case,
* ocsp_GetVerifiedSingleResponseForCertID will fail. If we recorded a
* negative cache entry in this case, then the attacker would have
* 'poisoned' our cache (denial of service), so we don't record negative
* results. */
rv = ocsp_CacheEncodedOCSPResponse(handle, certID, cert, time, pwArg,
encodedResponse, &certIDWasConsumed,
PR_FALSE /* don't cache failures */,
&rvOcsp);
if (!certIDWasConsumed) {
CERT_DestroyOCSPCertID(certID);
}
return rv == SECSuccess ? rvOcsp : rv;
}
/*
* Status in *certIDWasConsumed will always be correct, regardless of
* return value.
*/
static SECStatus
ocsp_GetOCSPStatusFromNetwork(CERTCertDBHandle *handle,
CERTOCSPCertID *certID,
CERTCertificate *cert,
int64 time,
void *pwArg,
PRBool *certIDWasConsumed,
SECStatus *rv_ocsp)
{
char *location = NULL;
PRBool locationIsDefault;
SECItem *encodedResponse = NULL;
CERTOCSPRequest *request = NULL;
SECStatus rv = SECFailure;
if (!certIDWasConsumed || !rv_ocsp) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
*certIDWasConsumed = PR_FALSE;
*rv_ocsp = SECFailure;
/*
* The first thing we need to do is find the location of the responder.
* This will be the value of the default responder (if enabled), else
* it will come out of the AIA extension in the cert (if present).
* If we have no such location, then this cert does not "deserve" to
* be checked -- that is, we consider it a success and just return.
* The way we tell that is by looking at the error number to see if
* the problem was no AIA extension was found; any other error was
* a true failure that we unfortunately have to treat as an overall
* failure here.
*/
location = ocsp_GetResponderLocation(handle, cert, PR_TRUE,
&locationIsDefault);
if (location == NULL) {
int err = PORT_GetError();
if (err == SEC_ERROR_EXTENSION_NOT_FOUND ||
err == SEC_ERROR_CERT_BAD_ACCESS_LOCATION) {
PORT_SetError(0);
*rv_ocsp = SECSuccess;
return SECSuccess;
}
return SECFailure;
}
/*
* XXX In the fullness of time, we will want/need to handle a
* certificate chain. This will be done either when a new parameter
* tells us to, or some configuration variable tells us to. In any
* case, handling it is complicated because we may need to send as
* many requests (and receive as many responses) as we have certs
* in the chain. If we are going to talk to a default responder,
* and we only support one default responder, we can put all of the
* certs together into one request. Otherwise, we must break them up
* into multiple requests. (Even if all of the requests will go to
* the same location, the signature on each response will be different,
* because each issuer is different. Carefully read the OCSP spec
* if you do not understand this.)
*/
/*
* XXX If/when signing of requests is supported, that second NULL
* should be changed to be the signer certificate. Not sure if that
* should be passed into this function or retrieved via some operation
* on the handle/context.
*/
encodedResponse =
ocsp_GetEncodedOCSPResponseForSingleCert(NULL, certID, cert, location,
time, locationIsDefault,
pwArg, &request);
if (encodedResponse == NULL) {
goto loser;
}
rv = ocsp_CacheEncodedOCSPResponse(handle, certID, cert, time, pwArg,
encodedResponse, certIDWasConsumed,
PR_TRUE /* cache failures */, rv_ocsp);
loser:
if (request != NULL)
CERT_DestroyOCSPRequest(request);
if (encodedResponse != NULL)
SECITEM_FreeItem(encodedResponse, PR_TRUE);
if (location != NULL)
PORT_Free(location);
return rv;
}
/*
* FUNCTION: ocsp_CacheEncodedOCSPResponse
* This function decodes an OCSP response and checks for a valid response
* concerning the given certificate. If such a response is not found
* then nothing is cached. Otherwise, if it is a good response, or if
* cacheNegative is true, the results are stored in the OCSP cache.
*
* Note: a 'valid' response is one that parses successfully, is not an OCSP
* exception (see RFC 2560 Section 2.3), is correctly signed and is current.
* A 'good' response is a valid response that attests that the certificate
* is not currently revoked (see RFC 2560 Section 2.2).
*
* INPUTS:
* CERTCertDBHandle *handle
* certificate DB of the cert that is being checked
* CERTOCSPCertID *certID
* the cert ID corresponding to |cert|
* CERTCertificate *cert
* the certificate being checked
* int64 time
* time for which status is to be determined
* void *pwArg
* the opaque argument to the password prompting function.
* SECItem *encodedResponse
* the DER encoded bytes of the OCSP response
* PRBool *certIDWasConsumed
* (output) on return, this is true iff |certID| was consumed by this
* function.
* SECStatus *rv_ocsp
* (output) on return, this is SECSuccess iff the response is good (see
* definition of 'good' above).
* RETURN:
* SECSuccess iff the response is valid.
*/
static SECStatus
ocsp_CacheEncodedOCSPResponse(CERTCertDBHandle *handle,
CERTOCSPCertID *certID,
CERTCertificate *cert,
int64 time,
void *pwArg,
SECItem *encodedResponse,
PRBool *certIDWasConsumed,
PRBool cacheNegative,
SECStatus *rv_ocsp)
{
CERTOCSPResponse *response = NULL;
CERTCertificate *signerCert = NULL;
CERTCertificate *issuerCert = NULL;
CERTOCSPSingleResponse *single = NULL;
SECStatus rv = SECFailure;
*certIDWasConsumed = PR_FALSE;
*rv_ocsp = SECFailure;
response = CERT_DecodeOCSPResponse(encodedResponse);
if (response == NULL) {
goto loser;
}
/*
* Okay, we at least have a response that *looks* like a response!
* Now see if the overall response status value is good or not.
* If not, we set an error and give up. (It means that either the
* server had a problem, or it didn't like something about our
* request. Either way there is nothing to do but give up.)
* Otherwise, we continue to find the actual per-cert status
* in the response.
*/
if (CERT_GetOCSPResponseStatus(response) != SECSuccess) {
goto loser;
}
/*
* If we've made it this far, we expect a response with a good signature.
* So, check for that.
*/
issuerCert = CERT_FindCertIssuer(cert, time, certUsageAnyCA);
rv = CERT_VerifyOCSPResponseSignature(response, handle, pwArg, &signerCert,
issuerCert);
if (rv != SECSuccess)
goto loser;
PORT_Assert(signerCert != NULL); /* internal consistency check */
/* XXX probably should set error, return failure if signerCert is null */
/*
* Again, we are only doing one request for one cert.
* XXX When we handle cert chains, the following code will obviously
* have to be modified, in coordation with the code above that will
* have to determine how to make multiple requests, etc.
*/
rv = ocsp_GetVerifiedSingleResponseForCertID(handle, response, certID,
signerCert, time, &single);
if (rv != SECSuccess)
goto loser;
*rv_ocsp = ocsp_SingleResponseCertHasGoodStatus(single, time);
loser:
if (cacheNegative || *rv_ocsp == SECSuccess) {
PR_EnterMonitor(OCSP_Global.monitor);
if (OCSP_Global.maxCacheEntries >= 0) {
/* single == NULL means: remember response failure */
ocsp_CreateOrUpdateCacheEntry(&OCSP_Global.cache, certID, single,
certIDWasConsumed);
/* ignore cache update failures */
}
PR_ExitMonitor(OCSP_Global.monitor);
}
/* 'single' points within the response so there's no need to free it. */
if (issuerCert != NULL)
CERT_DestroyCertificate(issuerCert);
if (signerCert != NULL)
CERT_DestroyCertificate(signerCert);
if (response != NULL)
CERT_DestroyOCSPResponse(response);
return rv;
}
static SECStatus
ocsp_GetVerifiedSingleResponseForCertID(CERTCertDBHandle *handle,
CERTOCSPResponse *response,
CERTOCSPCertID *certID,
CERTCertificate *signerCert,
int64 time,
CERTOCSPSingleResponse
**pSingleResponse)
{
SECStatus rv;
ocspResponseData *responseData;
int64 producedAt;
CERTOCSPSingleResponse *single;
/*
* The ResponseData part is the real guts of the response.
*/
responseData = ocsp_GetResponseData(response, NULL);
if (responseData == NULL) {
rv = SECFailure;
goto loser;
}
/*
* There is one producedAt time for the entire response (and a separate
* thisUpdate time for each individual single response). We need to
* compare them, so get the overall time to pass into the check of each
* single response.
*/
rv = DER_GeneralizedTimeToTime(&producedAt, &responseData->producedAt);
if (rv != SECSuccess)
goto loser;
single = ocsp_GetSingleResponseForCertID(responseData->responses,
handle, certID);
if (single == NULL) {
rv = SECFailure;
goto loser;
}
rv = ocsp_VerifySingleResponse(single, handle, signerCert, producedAt);
if (rv != SECSuccess)
goto loser;
*pSingleResponse = single;
loser:
return rv;
}
SECStatus
CERT_GetOCSPStatusForCertID(CERTCertDBHandle *handle,
CERTOCSPResponse *response,
CERTOCSPCertID *certID,
CERTCertificate *signerCert,
int64 time)
{
/*
* We do not update the cache, because:
*
* CERT_GetOCSPStatusForCertID is an old exported API that was introduced
* before the OCSP cache got implemented.
*
* The implementation of helper function cert_ProcessOCSPResponse
* requires the ability to transfer ownership of the the given certID to
* the cache. The external API doesn't allow us to prevent the caller from
* destroying the certID. We don't have the original certificate available,
* therefore we are unable to produce another certID object (that could
* be stored in the cache).
*
* Should we ever implement code to produce a deep copy of certID,
* then this could be changed to allow updating the cache.
* The duplication would have to be done in
* cert_ProcessOCSPResponse, if the out parameter to indicate
* a transfer of ownership is NULL.
*/
return cert_ProcessOCSPResponse(handle, response, certID,
signerCert, time,
NULL, NULL);
}
/*
* The first 5 parameters match the definition of CERT_GetOCSPStatusForCertID.
*/
SECStatus
cert_ProcessOCSPResponse(CERTCertDBHandle *handle,
CERTOCSPResponse *response,
CERTOCSPCertID *certID,
CERTCertificate *signerCert,
int64 time,
PRBool *certIDWasConsumed,
SECStatus *cacheUpdateStatus)
{
SECStatus rv;
SECStatus rv_cache = SECSuccess;
CERTOCSPSingleResponse *single = NULL;
rv = ocsp_GetVerifiedSingleResponseForCertID(handle, response, certID,
signerCert, time, &single);
if (rv == SECSuccess) {
/*
* Check whether the status says revoked, and if so
* how that compares to the time value passed into this routine.
*/
rv = ocsp_SingleResponseCertHasGoodStatus(single, time);
}
if (certIDWasConsumed) {
/*
* We don't have copy-of-certid implemented. In order to update
* the cache, the caller must supply an out variable
* certIDWasConsumed, allowing us to return ownership status.
*/
PR_EnterMonitor(OCSP_Global.monitor);
if (OCSP_Global.maxCacheEntries >= 0) {
/* single == NULL means: remember response failure */
rv_cache =
ocsp_CreateOrUpdateCacheEntry(&OCSP_Global.cache, certID,
single, certIDWasConsumed);
}
PR_ExitMonitor(OCSP_Global.monitor);
if (cacheUpdateStatus) {
*cacheUpdateStatus = rv_cache;
}
}
return rv;
}
SECStatus
cert_RememberOCSPProcessingFailure(CERTOCSPCertID *certID,
PRBool *certIDWasConsumed)
{
SECStatus rv = SECSuccess;
PR_EnterMonitor(OCSP_Global.monitor);
if (OCSP_Global.maxCacheEntries >= 0) {
rv = ocsp_CreateOrUpdateCacheEntry(&OCSP_Global.cache, certID, NULL,
certIDWasConsumed);
}
PR_ExitMonitor(OCSP_Global.monitor);
return rv;
}
/*
* Disable status checking and destroy related structures/data.
*/
static SECStatus
ocsp_DestroyStatusChecking(CERTStatusConfig *statusConfig)
{
ocspCheckingContext *statusContext;
/*
* Disable OCSP checking
*/
statusConfig->statusChecker = NULL;
statusContext = statusConfig->statusContext;
PORT_Assert(statusContext != NULL);
if (statusContext == NULL)
return SECFailure;
if (statusContext->defaultResponderURI != NULL)
PORT_Free(statusContext->defaultResponderURI);
if (statusContext->defaultResponderNickname != NULL)
PORT_Free(statusContext->defaultResponderNickname);
PORT_Free(statusContext);
statusConfig->statusContext = NULL;
PORT_Free(statusConfig);
return SECSuccess;
}
/*
* FUNCTION: CERT_DisableOCSPChecking
* Turns off OCSP checking for the given certificate database.
* This routine disables OCSP checking. Though it will return
* SECFailure if OCSP checking is not enabled, it is "safe" to
* call it that way and just ignore the return value, if it is
* easier to just call it than to "remember" whether it is enabled.
* INPUTS:
* CERTCertDBHandle *handle
* Certificate database for which OCSP checking will be disabled.
* RETURN:
* Returns SECFailure if an error occurred (usually means that OCSP
* checking was not enabled or status contexts were not initialized --
* error set will be SEC_ERROR_OCSP_NOT_ENABLED); SECSuccess otherwise.
*/
SECStatus
CERT_DisableOCSPChecking(CERTCertDBHandle *handle)
{
CERTStatusConfig *statusConfig;
ocspCheckingContext *statusContext;
if (handle == NULL) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
statusConfig = CERT_GetStatusConfig(handle);
statusContext = ocsp_GetCheckingContext(handle);
if (statusContext == NULL)
return SECFailure;
if (statusConfig->statusChecker != CERT_CheckOCSPStatus) {
/*
* Status configuration is present, but either not currently
* enabled or not for OCSP.
*/
PORT_SetError(SEC_ERROR_OCSP_NOT_ENABLED);
return SECFailure;
}
/* cache no longer necessary */
CERT_ClearOCSPCache();
/*
* This is how we disable status checking. Everything else remains
* in place in case we are enabled again.
*/
statusConfig->statusChecker = NULL;
return SECSuccess;
}
/*
* Allocate and initialize the informational structures for status checking.
* This is done when some configuration of OCSP is being done or when OCSP
* checking is being turned on, whichever comes first.
*/
static SECStatus
ocsp_InitStatusChecking(CERTCertDBHandle *handle)
{
CERTStatusConfig *statusConfig = NULL;
ocspCheckingContext *statusContext = NULL;
PORT_Assert(CERT_GetStatusConfig(handle) == NULL);
if (CERT_GetStatusConfig(handle) != NULL) {
/* XXX or call statusConfig->statusDestroy and continue? */
return SECFailure;
}
statusConfig = PORT_ZNew(CERTStatusConfig);
if (statusConfig == NULL)
goto loser;
statusContext = PORT_ZNew(ocspCheckingContext);
if (statusContext == NULL)
goto loser;
statusConfig->statusDestroy = ocsp_DestroyStatusChecking;
statusConfig->statusContext = statusContext;
CERT_SetStatusConfig(handle, statusConfig);
return SECSuccess;
loser:
if (statusConfig != NULL)
PORT_Free(statusConfig);
return SECFailure;
}
/*
* FUNCTION: CERT_EnableOCSPChecking
* Turns on OCSP checking for the given certificate database.
* INPUTS:
* CERTCertDBHandle *handle
* Certificate database for which OCSP checking will be enabled.
* RETURN:
* Returns SECFailure if an error occurred (likely only problem
* allocating memory); SECSuccess otherwise.
*/
SECStatus
CERT_EnableOCSPChecking(CERTCertDBHandle *handle)
{
CERTStatusConfig *statusConfig;
SECStatus rv;
if (handle == NULL) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
statusConfig = CERT_GetStatusConfig(handle);
if (statusConfig == NULL) {
rv = ocsp_InitStatusChecking(handle);
if (rv != SECSuccess)
return rv;
/* Get newly established value */
statusConfig = CERT_GetStatusConfig(handle);
PORT_Assert(statusConfig != NULL);
}
/*
* Setting the checker function is what really enables the checking
* when each cert verification is done.
*/
statusConfig->statusChecker = CERT_CheckOCSPStatus;
return SECSuccess;
}
/*
* FUNCTION: CERT_SetOCSPDefaultResponder
* Specify the location and cert of the default responder.
* If OCSP checking is already enabled *and* use of a default responder
* is also already enabled, all OCSP checking from now on will go directly
* to the specified responder. If OCSP checking is not enabled, or if
* it is but use of a default responder is not enabled, the information
* will be recorded and take effect whenever both are enabled.
* INPUTS:
* CERTCertDBHandle *handle
* Cert database on which OCSP checking should use the default responder.
* char *url
* The location of the default responder (e.g. "http://foo.com:80/ocsp")
* Note that the location will not be tested until the first attempt
* to send a request there.
* char *name
* The nickname of the cert to trust (expected) to sign the OCSP responses.
* If the corresponding cert cannot be found, SECFailure is returned.
* RETURN:
* Returns SECFailure if an error occurred; SECSuccess otherwise.
* The most likely error is that the cert for "name" could not be found
* (probably SEC_ERROR_UNKNOWN_CERT). Other errors are low-level (no memory,
* bad database, etc.).
*/
SECStatus
CERT_SetOCSPDefaultResponder(CERTCertDBHandle *handle,
const char *url, const char *name)
{
CERTCertificate *cert;
ocspCheckingContext *statusContext;
char *url_copy = NULL;
char *name_copy = NULL;
SECStatus rv;
if (handle == NULL || url == NULL || name == NULL) {
/*
* XXX When interface is exported, probably want better errors;
* perhaps different one for each parameter.
*/
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
/*
* Find the certificate for the specified nickname. Do this first
* because it seems the most likely to fail.
*
* XXX Shouldn't need that cast if the FindCertByNickname interface
* used const to convey that it does not modify the name. Maybe someday.
*/
cert = CERT_FindCertByNickname(handle, (char *) name);
if (cert == NULL) {
/*
* look for the cert on an external token.
*/
cert = PK11_FindCertFromNickname((char *)name, NULL);
}
if (cert == NULL)
return SECFailure;
/*
* Make a copy of the url and nickname.
*/
url_copy = PORT_Strdup(url);
name_copy = PORT_Strdup(name);
if (url_copy == NULL || name_copy == NULL) {
rv = SECFailure;
goto loser;
}
statusContext = ocsp_GetCheckingContext(handle);
/*
* Allocate and init the context if it doesn't already exist.
*/
if (statusContext == NULL) {
rv = ocsp_InitStatusChecking(handle);
if (rv != SECSuccess)
goto loser;
statusContext = ocsp_GetCheckingContext(handle);
PORT_Assert(statusContext != NULL); /* extreme paranoia */
}
/*
* Note -- we do not touch the status context until after all of
* the steps which could cause errors. If something goes wrong,
* we want to leave things as they were.
*/
/*
* Get rid of old url and name if there.
*/
if (statusContext->defaultResponderNickname != NULL)
PORT_Free(statusContext->defaultResponderNickname);
if (statusContext->defaultResponderURI != NULL)
PORT_Free(statusContext->defaultResponderURI);
/*
* And replace them with the new ones.
*/
statusContext->defaultResponderURI = url_copy;
statusContext->defaultResponderNickname = name_copy;
/*
* If there was already a cert in place, get rid of it and replace it.
* Otherwise, we are not currently enabled, so we don't want to save it;
* it will get re-found and set whenever use of a default responder is
* enabled.
*/
if (statusContext->defaultResponderCert != NULL) {
CERT_DestroyCertificate(statusContext->defaultResponderCert);
statusContext->defaultResponderCert = cert;
/*OCSP enabled, switching responder: clear cache*/
CERT_ClearOCSPCache();
} else {
PORT_Assert(statusContext->useDefaultResponder == PR_FALSE);
CERT_DestroyCertificate(cert);
/*OCSP currently not enabled, no need to clear cache*/
}
return SECSuccess;
loser:
CERT_DestroyCertificate(cert);
if (url_copy != NULL)
PORT_Free(url_copy);
if (name_copy != NULL)
PORT_Free(name_copy);
return rv;
}
/*
* FUNCTION: CERT_EnableOCSPDefaultResponder
* Turns on use of a default responder when OCSP checking.
* If OCSP checking is already enabled, this will make subsequent checks
* go directly to the default responder. (The location of the responder
* and the nickname of the responder cert must already be specified.)
* If OCSP checking is not enabled, this will be recorded and take effect
* whenever it is enabled.
* INPUTS:
* CERTCertDBHandle *handle
* Cert database on which OCSP checking should use the default responder.
* RETURN:
* Returns SECFailure if an error occurred; SECSuccess otherwise.
* No errors are especially likely unless the caller did not previously
* perform a successful call to SetOCSPDefaultResponder (in which case
* the error set will be SEC_ERROR_OCSP_NO_DEFAULT_RESPONDER).
*/
SECStatus
CERT_EnableOCSPDefaultResponder(CERTCertDBHandle *handle)
{
ocspCheckingContext *statusContext;
CERTCertificate *cert;
SECStatus rv;
SECCertificateUsage usage;
if (handle == NULL) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
statusContext = ocsp_GetCheckingContext(handle);
if (statusContext == NULL) {
/*
* Strictly speaking, the error already set is "correct",
* but cover over it with one more helpful in this context.
*/
PORT_SetError(SEC_ERROR_OCSP_NO_DEFAULT_RESPONDER);
return SECFailure;
}
if (statusContext->defaultResponderURI == NULL) {
PORT_SetError(SEC_ERROR_OCSP_NO_DEFAULT_RESPONDER);
return SECFailure;
}
if (statusContext->defaultResponderNickname == NULL) {
PORT_SetError(SEC_ERROR_OCSP_NO_DEFAULT_RESPONDER);
return SECFailure;
}
/*
* Find the cert for the nickname.
*/
cert = CERT_FindCertByNickname(handle,
statusContext->defaultResponderNickname);
if (cert == NULL) {
cert = PK11_FindCertFromNickname(statusContext->defaultResponderNickname,
NULL);
}
/*
* We should never have trouble finding the cert, because its
* existence should have been proven by SetOCSPDefaultResponder.
*/
PORT_Assert(cert != NULL);
if (cert == NULL)
return SECFailure;
/*
* Supplied cert should at least have a signing capability in order for us
* to use it as a trusted responder cert. Ability to sign is guaranteed if
* cert is validated to have any set of the usages below.
*/
rv = CERT_VerifyCertificateNow(handle, cert, PR_TRUE,
certificateUsageCheckAllUsages,
NULL, &usage);
if (rv != SECSuccess || (usage & (certificateUsageSSLClient |
certificateUsageSSLServer |
certificateUsageSSLServerWithStepUp |
certificateUsageEmailSigner |
certificateUsageObjectSigner |
certificateUsageStatusResponder |
certificateUsageSSLCA)) == 0) {
PORT_SetError(SEC_ERROR_OCSP_RESPONDER_CERT_INVALID);
return SECFailure;
}
/*
* And hang onto it.
*/
statusContext->defaultResponderCert = cert;
/* we don't allow a mix of cache entries from different responders */
CERT_ClearOCSPCache();
/*
* Finally, record the fact that we now have a default responder enabled.
*/
statusContext->useDefaultResponder = PR_TRUE;
return SECSuccess;
}
/*
* FUNCTION: CERT_DisableOCSPDefaultResponder
* Turns off use of a default responder when OCSP checking.
* (Does nothing if use of a default responder is not enabled.)
* INPUTS:
* CERTCertDBHandle *handle
* Cert database on which OCSP checking should stop using a default
* responder.
* RETURN:
* Returns SECFailure if an error occurred; SECSuccess otherwise.
* Errors very unlikely (like random memory corruption...).
*/
SECStatus
CERT_DisableOCSPDefaultResponder(CERTCertDBHandle *handle)
{
CERTStatusConfig *statusConfig;
ocspCheckingContext *statusContext;
CERTCertificate *tmpCert;
if (handle == NULL) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
statusConfig = CERT_GetStatusConfig(handle);
if (statusConfig == NULL)
return SECSuccess;
statusContext = ocsp_GetCheckingContext(handle);
PORT_Assert(statusContext != NULL);
if (statusContext == NULL)
return SECFailure;
tmpCert = statusContext->defaultResponderCert;
if (tmpCert) {
statusContext->defaultResponderCert = NULL;
CERT_DestroyCertificate(tmpCert);
/* we don't allow a mix of cache entries from different responders */
CERT_ClearOCSPCache();
}
/*
* Finally, record the fact.
*/
statusContext->useDefaultResponder = PR_FALSE;
return SECSuccess;
}
SECStatus
CERT_GetOCSPResponseStatus(CERTOCSPResponse *response)
{
PORT_Assert(response);
if (response->statusValue == ocspResponse_successful)
return SECSuccess;
switch (response->statusValue) {
case ocspResponse_malformedRequest:
PORT_SetError(SEC_ERROR_OCSP_MALFORMED_REQUEST);
break;
case ocspResponse_internalError:
PORT_SetError(SEC_ERROR_OCSP_SERVER_ERROR);
break;
case ocspResponse_tryLater:
PORT_SetError(SEC_ERROR_OCSP_TRY_SERVER_LATER);
break;
case ocspResponse_sigRequired:
/* XXX We *should* retry with a signature, if possible. */
PORT_SetError(SEC_ERROR_OCSP_REQUEST_NEEDS_SIG);
break;
case ocspResponse_unauthorized:
PORT_SetError(SEC_ERROR_OCSP_UNAUTHORIZED_REQUEST);
break;
case ocspResponse_other:
case ocspResponse_unused:
default:
PORT_SetError(SEC_ERROR_OCSP_UNKNOWN_RESPONSE_STATUS);
break;
}
return SECFailure;
}
|