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
|
#include <u.h>
#include <libc.h>
#include <mp.h>
#include <libsec.h>
/*=============================================================*/
/* general ASN1 declarations and parsing
*
* For now, this is used only for extracting the key from an
* X509 certificate, so the entire collection is hidden. But
* someday we should probably make the functions visible and
* give them their own man page.
*/
typedef struct Elem Elem;
typedef struct Tag Tag;
typedef struct Value Value;
typedef struct Bytes Bytes;
typedef struct Ints Ints;
typedef struct Bits Bits;
typedef struct Elist Elist;
/* tag classes */
#define Universal 0
#define Context 0x80
/* universal tags */
#define BOOLEAN 1
#define INTEGER 2
#define BIT_STRING 3
#define OCTET_STRING 4
#define NULLTAG 5
#define OBJECT_ID 6
#define ObjectDescriptor 7
#define EXTERNAL 8
#define REAL 9
#define ENUMERATED 10
#define EMBEDDED_PDV 11
#define UTF8String 12
#define SEQUENCE 16 /* also SEQUENCE OF */
#define SETOF 17 /* also SETOF OF */
#define NumericString 18
#define PrintableString 19
#define TeletexString 20
#define VideotexString 21
#define IA5String 22
#define UTCTime 23
#define GeneralizedTime 24
#define GraphicString 25
#define VisibleString 26
#define GeneralString 27
#define UniversalString 28
#define BMPString 30
struct Bytes {
int len;
uchar data[];
};
struct Ints {
int len;
int data[];
};
struct Bits {
int len; /* number of bytes */
int unusedbits; /* unused bits in last byte */
uchar data[]; /* most-significant bit first */
};
struct Tag {
int class;
int num;
};
enum { VBool, VInt, VOctets, VBigInt, VReal, VOther,
VBitString, VNull, VEOC, VObjId, VString, VSeq, VSet };
struct Value {
int tag; /* VBool, etc. */
union {
int boolval;
int intval;
Bytes* octetsval;
Bytes* bigintval;
Bytes* realval; /* undecoded; hardly ever used */
Bytes* otherval;
Bits* bitstringval;
Ints* objidval;
char* stringval;
Elist* seqval;
Elist* setval;
} u; /* (Don't use anonymous unions, for ease of porting) */
};
struct Elem {
Tag tag;
Value val;
};
struct Elist {
Elist* tl;
Elem hd;
};
/* decoding errors */
enum { ASN_OK, ASN_ESHORT, ASN_ETOOBIG, ASN_EVALLEN,
ASN_ECONSTR, ASN_EPRIM, ASN_EINVAL, ASN_EUNIMPL };
/* here are the functions to consider making extern someday */
static Bytes* newbytes(int len);
static Bytes* makebytes(uchar* buf, int len);
static void freebytes(Bytes* b);
static Bytes* catbytes(Bytes* b1, Bytes* b2);
static Ints* newints(int len);
static Ints* makeints(int* buf, int len);
static void freeints(Ints* b);
static Bits* newbits(int len);
static Bits* makebits(uchar* buf, int len, int unusedbits);
static void freebits(Bits* b);
static Elist* mkel(Elem e, Elist* tail);
static void freeelist(Elist* el);
static int elistlen(Elist* el);
static int is_seq(Elem* pe, Elist** pseq);
static int is_set(Elem* pe, Elist** pset);
static int is_int(Elem* pe, int* pint);
static int is_bigint(Elem* pe, Bytes** pbigint);
static int is_bitstring(Elem* pe, Bits** pbits);
static int is_octetstring(Elem* pe, Bytes** poctets);
static int is_oid(Elem* pe, Ints** poid);
static int is_string(Elem* pe, char** pstring);
static int is_time(Elem* pe, char** ptime);
static int decode(uchar* a, int alen, Elem* pelem);
static int encode(Elem e, Bytes** pbytes);
static int oid_lookup(Ints* o, Ints** tab);
static void freevalfields(Value* v);
static mpint *asn1mpint(Elem *e);
static void edump(Elem);
#define TAG_MASK 0x1F
#define CONSTR_MASK 0x20
#define CLASS_MASK 0xC0
#define MAXOBJIDLEN 20
static int ber_decode(uchar** pp, uchar* pend, Elem* pelem);
static int tag_decode(uchar** pp, uchar* pend, Tag* ptag, int* pisconstr);
static int length_decode(uchar** pp, uchar* pend, int* plength);
static int value_decode(uchar** pp, uchar* pend, int length, int kind, int isconstr, Value* pval);
static int int_decode(uchar** pp, uchar* pend, int count, int unsgned, int* pint);
static int uint7_decode(uchar** pp, uchar* pend, int* pint);
static int octet_decode(uchar** pp, uchar* pend, int length, int isconstr, Bytes** pbytes);
static int seq_decode(uchar** pp, uchar* pend, int length, int isconstr, Elist** pelist);
static int enc(uchar** pp, Elem e, int lenonly);
static int val_enc(uchar** pp, Elem e, int *pconstr, int lenonly);
static void uint7_enc(uchar** pp, int num, int lenonly);
static void int_enc(uchar** pp, int num, int unsgned, int lenonly);
static void *
emalloc(int n)
{
void *p;
if(n==0)
n=1;
p = malloc(n);
if(p == nil)
sysfatal("out of memory");
memset(p, 0, n);
setmalloctag(p, getcallerpc(&n));
return p;
}
static char*
estrdup(char *s)
{
char *d;
int n;
n = strlen(s)+1;
d = emalloc(n);
memmove(d, s, n);
return d;
}
/*
* Decode a[0..len] as a BER encoding of an ASN1 type.
* The return value is one of ASN_OK, etc.
* Depending on the error, the returned elem may or may not
* be nil.
*/
static int
decode(uchar* a, int alen, Elem* pelem)
{
uchar* p = a;
int err;
err = ber_decode(&p, &a[alen], pelem);
if(err == ASN_OK && p != &a[alen])
err = ASN_EVALLEN;
return err;
}
/*
* All of the following decoding routines take arguments:
* uchar **pp;
* uchar *pend;
* Where parsing is supposed to start at **pp, and when parsing
* is done, *pp is updated to point at next char to be parsed.
* The pend pointer is just past end of string; an error should
* be returned parsing hasn't finished by then.
*
* The returned int is ASN_OK if all went fine, else ASN_ESHORT, etc.
* The remaining argument(s) are pointers to where parsed entity goes.
*/
/* Decode an ASN1 'Elem' (tag, length, value) */
static int
ber_decode(uchar** pp, uchar* pend, Elem* pelem)
{
int err;
int isconstr;
int length;
Tag tag;
Value val;
memset(pelem, 0, sizeof(*pelem));
err = tag_decode(pp, pend, &tag, &isconstr);
if(err == ASN_OK) {
err = length_decode(pp, pend, &length);
if(err == ASN_OK) {
if(tag.class == Universal)
err = value_decode(pp, pend, length, tag.num, isconstr, &val);
else
err = value_decode(pp, pend, length, OCTET_STRING, 0, &val);
if(err == ASN_OK) {
pelem->tag = tag;
pelem->val = val;
}
}
}
return err;
}
/* Decode a tag field */
static int
tag_decode(uchar** pp, uchar* pend, Tag* ptag, int* pisconstr)
{
int err;
int v;
uchar* p;
err = ASN_OK;
p = *pp;
if(pend-p >= 2) {
v = *p++;
ptag->class = v&CLASS_MASK;
if(v&CONSTR_MASK)
*pisconstr = 1;
else
*pisconstr = 0;
v &= TAG_MASK;
if(v == TAG_MASK)
err = uint7_decode(&p, pend, &v);
ptag->num = v;
}
else
err = ASN_ESHORT;
*pp = p;
return err;
}
/* Decode a length field */
static int
length_decode(uchar** pp, uchar* pend, int* plength)
{
int err;
int num;
int v;
uchar* p;
err = ASN_OK;
num = 0;
p = *pp;
if(p < pend) {
v = *p++;
if(v&0x80)
err = int_decode(&p, pend, v&0x7F, 1, &num);
else
num = v;
}
else
err = ASN_ESHORT;
*pp = p;
*plength = num;
return err;
}
/* Decode a value field */
static int
value_decode(uchar** pp, uchar* pend, int length, int kind, int isconstr, Value* pval)
{
int err;
Bytes* va;
int num;
int bitsunused;
int subids[MAXOBJIDLEN];
int isubid;
Elist* vl;
uchar* p;
uchar* pe;
err = ASN_OK;
p = *pp;
if(length == -1) { /* "indefinite" length spec */
if(!isconstr)
err = ASN_EINVAL;
}
else if(p + length > pend)
err = ASN_EVALLEN;
if(err != ASN_OK)
return err;
switch(kind) {
case 0:
/* marker for end of indefinite constructions */
if(length == 0)
pval->tag = VNull;
else
err = ASN_EINVAL;
break;
case BOOLEAN:
if(isconstr)
err = ASN_ECONSTR;
else if(length != 1)
err = ASN_EVALLEN;
else {
pval->tag = VBool;
pval->u.boolval = (*p++ != 0);
}
break;
case INTEGER:
case ENUMERATED:
if(isconstr)
err = ASN_ECONSTR;
else if(length <= 4) {
err = int_decode(&p, pend, length, 0, &num);
if(err == ASN_OK) {
pval->tag = VInt;
pval->u.intval = num;
}
}
else {
pval->tag = VBigInt;
pval->u.bigintval = makebytes(p, length);
p += length;
}
break;
case BIT_STRING:
pval->tag = VBitString;
if(isconstr) {
if(length == -1 && p + 2 <= pend && *p == 0 && *(p+1) ==0) {
pval->u.bitstringval = makebits(0, 0, 0);
p += 2;
}
else /* TODO: recurse and concat results */
err = ASN_EUNIMPL;
}
else {
if(length < 2) {
if(length == 1 && *p == 0) {
pval->u.bitstringval = makebits(0, 0, 0);
p++;
}
else
err = ASN_EINVAL;
}
else {
bitsunused = *p;
if(bitsunused > 7)
err = ASN_EINVAL;
else if(length > 0x0FFFFFFF)
err = ASN_ETOOBIG;
else {
pval->u.bitstringval = makebits(p+1, length-1, bitsunused);
p += length;
}
}
}
break;
case OCTET_STRING:
case ObjectDescriptor:
err = octet_decode(&p, pend, length, isconstr, &va);
if(err == ASN_OK) {
pval->tag = VOctets;
pval->u.octetsval = va;
}
break;
case NULLTAG:
if(isconstr)
err = ASN_ECONSTR;
else if(length != 0)
err = ASN_EVALLEN;
else
pval->tag = VNull;
break;
case OBJECT_ID:
if(isconstr)
err = ASN_ECONSTR;
else if(length == 0)
err = ASN_EVALLEN;
else {
isubid = 0;
pe = p+length;
while(p < pe && isubid < MAXOBJIDLEN) {
err = uint7_decode(&p, pend, &num);
if(err != ASN_OK)
break;
if(isubid == 0) {
subids[isubid++] = num / 40;
subids[isubid++] = num % 40;
}
else
subids[isubid++] = num;
}
if(err == ASN_OK) {
if(p != pe)
err = ASN_EVALLEN;
else {
pval->tag = VObjId;
pval->u.objidval = makeints(subids, isubid);
}
}
}
break;
case EXTERNAL:
case EMBEDDED_PDV:
/* TODO: parse this internally */
if(p+length > pend)
err = ASN_EVALLEN;
else {
pval->tag = VOther;
pval->u.otherval = makebytes(p, length);
p += length;
}
break;
case REAL:
/* Let the application decode */
if(isconstr)
err = ASN_ECONSTR;
else if(p+length > pend)
err = ASN_EVALLEN;
else {
pval->tag = VReal;
pval->u.realval = makebytes(p, length);
p += length;
}
break;
case SEQUENCE:
err = seq_decode(&p, pend, length, isconstr, &vl);
if(err == ASN_OK) {
pval->tag = VSeq ;
pval->u.seqval = vl;
}
break;
case SETOF:
err = seq_decode(&p, pend, length, isconstr, &vl);
if(err == ASN_OK) {
pval->tag = VSet;
pval->u.setval = vl;
}
break;
case UTF8String:
case NumericString:
case PrintableString:
case TeletexString:
case VideotexString:
case IA5String:
case UTCTime:
case GeneralizedTime:
case GraphicString:
case VisibleString:
case GeneralString:
case UniversalString:
case BMPString:
err = octet_decode(&p, pend, length, isconstr, &va);
if(err == ASN_OK) {
uchar *s;
char *d;
Rune r;
int n;
switch(kind){
case UniversalString:
n = va->len / 4;
d = emalloc(n*UTFmax+1);
pval->u.stringval = d;
s = va->data;
while(n > 0){
r = s[0]<<24 | s[1]<<16 | s[2]<<8 | s[3];
if(r == 0)
break;
n--;
s += 4;
d += runetochar(d, &r);
}
*d = 0;
break;
case BMPString:
n = va->len / 2;
d = emalloc(n*UTFmax+1);
pval->u.stringval = d;
s = va->data;
while(n > 0){
r = s[0]<<8 | s[1];
if(r == 0)
break;
n--;
s += 2;
d += runetochar(d, &r);
}
*d = 0;
break;
default:
n = va->len;
d = emalloc(n+1);
pval->u.stringval = d;
s = va->data;
while(n > 0){
if((*d = *s) == 0)
break;
n--;
s++;
d++;
}
*d = 0;
break;
}
if(n != 0){
err = ASN_EINVAL;
free(pval->u.stringval);
} else
pval->tag = VString;
free(va);
}
break;
default:
if(p+length > pend)
err = ASN_EVALLEN;
else {
pval->tag = VOther;
pval->u.otherval = makebytes(p, length);
p += length;
}
break;
}
*pp = p;
return err;
}
/*
* Decode an int in format where count bytes are
* concatenated to form value.
* Although ASN1 allows any size integer, we return
* an error if the result doesn't fit in a 32-bit int.
* If unsgned is not set, make sure to propagate sign bit.
*/
static int
int_decode(uchar** pp, uchar* pend, int count, int unsgned, int* pint)
{
int err;
int num;
uchar* p;
p = *pp;
err = ASN_OK;
num = 0;
if(p+count <= pend) {
if((count > 4) || (unsgned && count == 4 && (*p&0x80)))
err = ASN_ETOOBIG;
else {
if(!unsgned && count > 0 && count < 4 && (*p&0x80))
num = -1; /* set all bits, initially */
while(count--)
num = (num << 8)|(*p++);
}
}
else
err = ASN_ESHORT;
*pint = num;
*pp = p;
return err;
}
/*
* Decode an unsigned int in format where each
* byte except last has high bit set, and remaining
* seven bits of each byte are concatenated to form value.
* Although ASN1 allows any size integer, we return
* an error if the result doesn't fit in a 32 bit int.
*/
static int
uint7_decode(uchar** pp, uchar* pend, int* pint)
{
int err;
int num;
int more;
int v;
uchar* p;
p = *pp;
err = ASN_OK;
num = 0;
more = 1;
while(more && p < pend) {
v = *p++;
if(num&0x7F000000) {
err = ASN_ETOOBIG;
break;
}
num <<= 7;
more = v&0x80;
num |= (v&0x7F);
}
if(p == pend)
err = ASN_ESHORT;
*pint = num;
*pp = p;
return err;
}
/*
* Decode an octet string, recursively if isconstr.
* We've already checked that length==-1 implies isconstr==1,
* and otherwise that specified length fits within (*pp..pend)
*/
static int
octet_decode(uchar** pp, uchar* pend, int length, int isconstr, Bytes** pbytes)
{
int err;
uchar* p;
Bytes* ans;
Bytes* newans;
uchar* pstart;
uchar* pold;
Elem elem;
err = ASN_OK;
p = *pp;
ans = nil;
if(length >= 0 && !isconstr) {
ans = makebytes(p, length);
p += length;
}
else {
/* constructed, either definite or indefinite length */
pstart = p;
for(;;) {
if(length >= 0 && p >= pstart + length) {
if(p != pstart + length)
err = ASN_EVALLEN;
break;
}
pold = p;
err = ber_decode(&p, pend, &elem);
if(err != ASN_OK)
break;
switch(elem.val.tag) {
case VOctets:
newans = catbytes(ans, elem.val.u.octetsval);
freevalfields(&elem.val);
freebytes(ans);
ans = newans;
break;
case VEOC:
if(length == -1)
goto cloop_done;
/* no break */
default:
freevalfields(&elem.val);
p = pold;
err = ASN_EINVAL;
goto cloop_done;
}
}
cloop_done:
if(err != ASN_OK){
freebytes(ans);
ans = nil;
}
}
*pp = p;
*pbytes = ans;
return err;
}
/*
* Decode a sequence or set.
* We've already checked that length==-1 implies isconstr==1,
* and otherwise that specified length fits within (*p..pend)
*/
static int
seq_decode(uchar** pp, uchar* pend, int length, int isconstr, Elist** pelist)
{
int err;
uchar* p;
uchar* pstart;
uchar* pold;
Elist* ans;
Elem elem;
Elist* lve;
Elist* lveold;
err = ASN_OK;
ans = nil;
p = *pp;
if(!isconstr)
err = ASN_EPRIM;
else {
/* constructed, either definite or indefinite length */
lve = nil;
pstart = p;
for(;;) {
if(length >= 0 && p >= pstart + length) {
if(p != pstart + length)
err = ASN_EVALLEN;
break;
}
pold = p;
err = ber_decode(&p, pend, &elem);
if(err != ASN_OK)
break;
if(elem.val.tag == VEOC) {
if(length != -1) {
p = pold;
err = ASN_EINVAL;
}
break;
}
else
lve = mkel(elem, lve);
}
if(err != ASN_OK)
freeelist(lve);
else {
/* reverse back to original order */
while(lve != nil) {
lveold = lve;
lve = lve->tl;
lveold->tl = ans;
ans = lveold;
}
}
}
*pp = p;
*pelist = ans;
return err;
}
/*
* Encode e by BER rules, putting answer in *pbytes.
* This is done by first calling enc with lenonly==1
* to get the length of the needed buffer,
* then allocating the buffer and using enc again to fill it up.
*/
static int
encode(Elem e, Bytes** pbytes)
{
uchar* p;
Bytes* ans;
int err;
uchar uc;
p = &uc;
err = enc(&p, e, 1);
if(err == ASN_OK) {
ans = newbytes(p-&uc);
p = ans->data;
err = enc(&p, e, 0);
*pbytes = ans;
}
return err;
}
/*
* The various enc functions take a pointer to a pointer
* into a buffer, and encode their entity starting there,
* updating the pointer afterwards.
* If lenonly is 1, only the pointer update is done,
* allowing enc to be called first to calculate the needed
* buffer length.
* If lenonly is 0, it is assumed that the answer will fit.
*/
static int
enc(uchar** pp, Elem e, int lenonly)
{
int err;
int vlen;
int constr;
Tag tag;
int v;
int ilen;
uchar* p;
uchar* psave;
p = *pp;
err = val_enc(&p, e, &constr, 1);
if(err != ASN_OK)
return err;
vlen = p - *pp;
p = *pp;
tag = e.tag;
v = tag.class|constr;
if(tag.num < 31) {
if(!lenonly)
*p = (v|tag.num);
p++;
}
else {
if(!lenonly)
*p = (v|31);
p++;
if(tag.num < 0)
return ASN_EINVAL;
uint7_enc(&p, tag.num, lenonly);
}
if(vlen < 0x80) {
if(!lenonly)
*p = vlen;
p++;
}
else {
psave = p;
int_enc(&p, vlen, 1, 1);
ilen = p-psave;
p = psave;
if(!lenonly) {
*p++ = (0x80 | ilen);
int_enc(&p, vlen, 1, 0);
}
else
p += 1 + ilen;
}
if(!lenonly)
val_enc(&p, e, &constr, 0);
else
p += vlen;
*pp = p;
return err;
}
static int
val_enc(uchar** pp, Elem e, int *pconstr, int lenonly)
{
int err;
uchar* p;
int kind;
int cl;
int v;
Bytes* bb = nil;
Bits* bits;
Ints* oid;
int k;
Elist* el;
char* s;
p = *pp;
err = ASN_OK;
kind = e.tag.num;
cl = e.tag.class;
*pconstr = 0;
if(cl != Universal) {
switch(e.val.tag) {
case VBool:
kind = BOOLEAN;
break;
case VInt:
kind = INTEGER;
break;
case VBigInt:
kind = INTEGER;
break;
case VOctets:
kind = OCTET_STRING;
break;
case VReal:
kind = REAL;
break;
case VOther:
kind = OCTET_STRING;
break;
case VBitString:
kind = BIT_STRING;
break;
case VNull:
kind = NULLTAG;
break;
case VObjId:
kind = OBJECT_ID;
break;
case VString:
kind = UniversalString;
break;
case VSeq:
kind = SEQUENCE;
break;
case VSet:
kind = SETOF;
break;
}
}
switch(kind) {
case BOOLEAN:
if(is_int(&e, &v)) {
if(v != 0)
v = 255;
int_enc(&p, v, 1, lenonly);
}
else
err = ASN_EINVAL;
break;
case INTEGER:
case ENUMERATED:
if(is_int(&e, &v))
int_enc(&p, v, 0, lenonly);
else {
if(is_bigint(&e, &bb)) {
if(!lenonly)
memmove(p, bb->data, bb->len);
p += bb->len;
}
else
err = ASN_EINVAL;
}
break;
case BIT_STRING:
if(is_bitstring(&e, &bits)) {
if(bits->len == 0) {
if(!lenonly)
*p = 0;
p++;
}
else {
v = bits->unusedbits;
if(v < 0 || v > 7)
err = ASN_EINVAL;
else {
if(!lenonly) {
*p = v;
memmove(p+1, bits->data, bits->len);
}
p += 1 + bits->len;
}
}
}
else
err = ASN_EINVAL;
break;
case OCTET_STRING:
case ObjectDescriptor:
case EXTERNAL:
case REAL:
case EMBEDDED_PDV:
bb = nil;
switch(e.val.tag) {
case VOctets:
bb = e.val.u.octetsval;
break;
case VReal:
bb = e.val.u.realval;
break;
case VOther:
bb = e.val.u.otherval;
break;
}
if(bb != nil) {
if(!lenonly)
memmove(p, bb->data, bb->len);
p += bb->len;
}
else
err = ASN_EINVAL;
break;
case NULLTAG:
break;
case OBJECT_ID:
if(is_oid(&e, &oid)) {
for(k = 0; k < oid->len; k++) {
v = oid->data[k];
if(k == 0) {
v *= 40;
if(oid->len > 1)
v += oid->data[++k];
}
uint7_enc(&p, v, lenonly);
}
}
else
err = ASN_EINVAL;
break;
case SEQUENCE:
case SETOF:
el = nil;
if(e.val.tag == VSeq)
el = e.val.u.seqval;
else if(e.val.tag == VSet)
el = e.val.u.setval;
else
err = ASN_EINVAL;
if(el != nil) {
*pconstr = CONSTR_MASK;
for(; el != nil; el = el->tl) {
err = enc(&p, el->hd, lenonly);
if(err != ASN_OK)
break;
}
}
break;
case UTF8String:
case NumericString:
case PrintableString:
case TeletexString:
case VideotexString:
case IA5String:
case UTCTime:
case GeneralizedTime:
case GraphicString:
case VisibleString:
case GeneralString:
case UniversalString:
case BMPString:
if(e.val.tag == VString) {
s = e.val.u.stringval;
if(s != nil) {
v = strlen(s);
if(!lenonly)
memmove(p, s, v);
p += v;
}
}
else
err = ASN_EINVAL;
break;
default:
err = ASN_EINVAL;
}
*pp = p;
return err;
}
/*
* Encode num as unsigned 7 bit values with top bit 1 on all bytes
* except last, only putting in bytes if !lenonly.
*/
static void
uint7_enc(uchar** pp, int num, int lenonly)
{
int n;
int v;
int k;
uchar* p;
p = *pp;
n = 1;
v = num >> 7;
while(v > 0) {
v >>= 7;
n++;
}
if(lenonly)
p += n;
else {
for(k = (n - 1)*7; k > 0; k -= 7)
*p++= ((num >> k)|0x80);
*p++ = (num&0x7F);
}
*pp = p;
}
/*
* Encode num as unsigned or signed integer,
* only putting in bytes if !lenonly.
* Encoding is length followed by bytes to concatenate.
*/
static void
int_enc(uchar** pp, int num, int unsgned, int lenonly)
{
int v;
int n;
int prevv;
int k;
uchar* p;
p = *pp;
v = num;
if(v < 0)
v = -(v + 1);
n = 1;
prevv = v;
v >>= 8;
while(v > 0) {
prevv = v;
v >>= 8;
n++;
}
if(!unsgned && (prevv&0x80))
n++;
if(lenonly)
p += n;
else {
for(k = (n - 1)*8; k >= 0; k -= 8)
*p++ = (num >> k);
}
*pp = p;
}
static int
ints_eq(Ints* a, Ints* b)
{
int alen;
int i;
alen = a->len;
if(alen != b->len)
return 0;
for(i = 0; i < alen; i++)
if(a->data[i] != b->data[i])
return 0;
return 1;
}
/*
* Look up o in tab (which must have nil entry to terminate).
* Return index of matching entry, or -1 if none.
*/
static int
oid_lookup(Ints* o, Ints** tab)
{
int i;
for(i = 0; tab[i] != nil; i++)
if(ints_eq(o, tab[i]))
return i;
return -1;
}
/*
* Return true if *pe is a SEQUENCE, and set *pseq to
* the value of the sequence if so.
*/
static int
is_seq(Elem* pe, Elist** pseq)
{
if(pe->tag.class == Universal && pe->tag.num == SEQUENCE && pe->val.tag == VSeq) {
*pseq = pe->val.u.seqval;
return 1;
}
return 0;
}
static int
is_set(Elem* pe, Elist** pset)
{
if(pe->tag.class == Universal && pe->tag.num == SETOF && pe->val.tag == VSet) {
*pset = pe->val.u.setval;
return 1;
}
return 0;
}
static int
is_int(Elem* pe, int* pint)
{
if(pe->tag.class == Universal) {
if(pe->tag.num == INTEGER && pe->val.tag == VInt) {
*pint = pe->val.u.intval;
return 1;
}
else if(pe->tag.num == BOOLEAN && pe->val.tag == VBool) {
*pint = pe->val.u.boolval;
return 1;
}
}
return 0;
}
/*
* for convience, all VInt's are readable via this routine,
* as well as all VBigInt's
*/
static int
is_bigint(Elem* pe, Bytes** pbigint)
{
if(pe->tag.class == Universal && pe->tag.num == INTEGER && pe->val.tag == VBigInt) {
*pbigint = pe->val.u.bigintval;
return 1;
}
return 0;
}
static int
is_bitstring(Elem* pe, Bits** pbits)
{
if(pe->tag.class == Universal && pe->tag.num == BIT_STRING && pe->val.tag == VBitString) {
*pbits = pe->val.u.bitstringval;
return 1;
}
return 0;
}
static int
is_octetstring(Elem* pe, Bytes** poctets)
{
if(pe->tag.class == Universal && pe->tag.num == OCTET_STRING && pe->val.tag == VOctets) {
*poctets = pe->val.u.octetsval;
return 1;
}
return 0;
}
static int
is_oid(Elem* pe, Ints** poid)
{
if(pe->tag.class == Universal && pe->tag.num == OBJECT_ID && pe->val.tag == VObjId) {
*poid = pe->val.u.objidval;
return 1;
}
return 0;
}
static int
is_string(Elem* pe, char** pstring)
{
if(pe->tag.class == Universal) {
switch(pe->tag.num) {
case UTF8String:
case NumericString:
case PrintableString:
case TeletexString:
case VideotexString:
case IA5String:
case GraphicString:
case VisibleString:
case GeneralString:
case UniversalString:
case BMPString:
if(pe->val.tag == VString) {
*pstring = pe->val.u.stringval;
return 1;
}
}
}
return 0;
}
static int
is_time(Elem* pe, char** ptime)
{
if(pe->tag.class == Universal
&& (pe->tag.num == UTCTime || pe->tag.num == GeneralizedTime)
&& pe->val.tag == VString) {
*ptime = pe->val.u.stringval;
return 1;
}
return 0;
}
/*
* malloc and return a new Bytes structure capable of
* holding len bytes. (len >= 0)
*/
static Bytes*
newbytes(int len)
{
Bytes* ans;
if(len < 0)
abort();
ans = emalloc(sizeof(Bytes) + len);
ans->len = len;
return ans;
}
/*
* newbytes(len), with data initialized from buf
*/
static Bytes*
makebytes(uchar* buf, int len)
{
Bytes* ans;
ans = newbytes(len);
memmove(ans->data, buf, len);
return ans;
}
static void
freebytes(Bytes* b)
{
free(b);
}
/*
* Make a new Bytes, containing bytes of b1 followed by those of b2.
* Either b1 or b2 or both can be nil.
*/
static Bytes*
catbytes(Bytes* b1, Bytes* b2)
{
Bytes* ans;
int n;
if(b1 == nil) {
if(b2 == nil)
ans = newbytes(0);
else
ans = makebytes(b2->data, b2->len);
}
else if(b2 == nil) {
ans = makebytes(b1->data, b1->len);
}
else {
n = b1->len + b2->len;
ans = newbytes(n);
ans->len = n;
memmove(ans->data, b1->data, b1->len);
memmove(ans->data+b1->len, b2->data, b2->len);
}
return ans;
}
/* len is number of ints */
static Ints*
newints(int len)
{
Ints* ans;
if(len < 0 || len > ((uint)-1>>1)/sizeof(int))
abort();
ans = emalloc(sizeof(Ints) + len*sizeof(int));
ans->len = len;
return ans;
}
static Ints*
makeints(int* buf, int len)
{
Ints* ans;
ans = newints(len);
memmove(ans->data, buf, len*sizeof(int));
return ans;
}
static void
freeints(Ints* b)
{
free(b);
}
/* len is number of bytes */
static Bits*
newbits(int len)
{
Bits* ans;
if(len < 0)
abort();
ans = emalloc(sizeof(Bits) + len);
ans->len = len;
ans->unusedbits = 0;
return ans;
}
static Bits*
makebits(uchar* buf, int len, int unusedbits)
{
Bits* ans;
ans = newbits(len);
memmove(ans->data, buf, len);
ans->unusedbits = unusedbits;
return ans;
}
static void
freebits(Bits* b)
{
free(b);
}
static Elist*
mkel(Elem e, Elist* tail)
{
Elist* el;
el = (Elist*)emalloc(sizeof(Elist));
setmalloctag(el, getcallerpc(&e));
el->hd = e;
el->tl = tail;
return el;
}
static int
elistlen(Elist* el)
{
int ans = 0;
while(el != nil) {
ans++;
el = el->tl;
}
return ans;
}
/* Frees elist, but not fields inside values of constituent elems */
static void
freeelist(Elist* el)
{
Elist* next;
while(el != nil) {
next = el->tl;
free(el);
el = next;
}
}
/* free any allocated structures inside v (recursively freeing Elists) */
static void
freevalfields(Value* v)
{
Elist* el;
Elist* l;
if(v == nil)
return;
switch(v->tag) {
case VOctets:
freebytes(v->u.octetsval);
break;
case VBigInt:
freebytes(v->u.bigintval);
break;
case VReal:
freebytes(v->u.realval);
break;
case VOther:
freebytes(v->u.otherval);
break;
case VBitString:
freebits(v->u.bitstringval);
break;
case VObjId:
freeints(v->u.objidval);
break;
case VString:
free(v->u.stringval);
break;
case VSeq:
el = v->u.seqval;
for(l = el; l != nil; l = l->tl)
freevalfields(&l->hd.val);
freeelist(el);
break;
case VSet:
el = v->u.setval;
for(l = el; l != nil; l = l->tl)
freevalfields(&l->hd.val);
freeelist(el);
break;
}
memset(v, 0, sizeof(*v));
}
static mpint*
asn1mpint(Elem *e)
{
Bytes *b;
int v;
if(is_int(e, &v))
return itomp(v, nil);
if(is_bigint(e, &b))
return betomp(b->data, b->len, nil);
return nil;
}
/* end of general ASN1 functions */
/*=============================================================*/
/*
* Decode and parse an X.509 Certificate, defined by this ASN1:
* Certificate ::= SEQUENCE {
* certificateInfo CertificateInfo,
* signatureAlgorithm AlgorithmIdentifier,
* signature BIT STRING }
*
* CertificateInfo ::= SEQUENCE {
* version [0] INTEGER DEFAULT v1 (0),
* serialNumber INTEGER,
* signature AlgorithmIdentifier,
* issuer Name,
* validity Validity,
* subject Name,
* subjectPublicKeyInfo SubjectPublicKeyInfo }
* (version v2 has two more fields, optional unique identifiers for
* issuer and subject; since we ignore these anyway, we won't parse them)
*
* Validity ::= SEQUENCE {
* notBefore UTCTime,
* notAfter UTCTime }
*
* SubjectPublicKeyInfo ::= SEQUENCE {
* algorithm AlgorithmIdentifier,
* subjectPublicKey BIT STRING }
*
* AlgorithmIdentifier ::= SEQUENCE {
* algorithm OBJECT IDENTIFER,
* parameters ANY DEFINED BY ALGORITHM OPTIONAL }
*
* Name ::= SEQUENCE OF RelativeDistinguishedName
*
* RelativeDistinguishedName ::= SETOF SIZE(1..MAX) OF AttributeTypeAndValue
*
* AttributeTypeAndValue ::= SEQUENCE {
* type OBJECT IDENTIFER,
* value DirectoryString }
* (selected attributes have these Object Ids:
* commonName {2 5 4 3}
* countryName {2 5 4 6}
* localityName {2 5 4 7}
* stateOrProvinceName {2 5 4 8}
* organizationName {2 5 4 10}
* organizationalUnitName {2 5 4 11}
* )
*
* DirectoryString ::= CHOICE {
* teletexString TeletexString,
* printableString PrintableString,
* universalString UniversalString }
*
* See rfc1423, rfc2437 for AlgorithmIdentifier, subjectPublicKeyInfo, signature.
*
* Not yet implemented:
* CertificateRevocationList ::= SIGNED SEQUENCE{
* signature AlgorithmIdentifier,
* issuer Name,
* lastUpdate UTCTime,
* nextUpdate UTCTime,
* revokedCertificates
* SEQUENCE OF CRLEntry OPTIONAL}
* CRLEntry ::= SEQUENCE{
* userCertificate SerialNumber,
* revocationDate UTCTime}
*/
typedef struct CertX509 {
int serial;
char* issuer;
char* validity_start;
char* validity_end;
char* subject;
int publickey_alg;
Bits* publickey;
int signature_alg;
Bits* signature;
int curve;
} CertX509;
/* Algorithm object-ids */
enum {
ALG_rsaEncryption,
ALG_md2WithRSAEncryption,
ALG_md4WithRSAEncryption,
ALG_md5WithRSAEncryption,
ALG_sha1WithRSAEncryption,
ALG_sha1WithRSAEncryptionOiw,
ALG_sha256WithRSAEncryption,
ALG_sha384WithRSAEncryption,
ALG_sha512WithRSAEncryption,
ALG_sha224WithRSAEncryption,
ALG_ecPublicKey,
ALG_sha1WithECDSA,
ALG_sha256WithECDSA,
ALG_sha384WithECDSA,
ALG_sha512WithECDSA,
ALG_md5,
ALG_sha1,
ALG_sha256,
ALG_sha384,
ALG_sha512,
ALG_sha224,
NUMALGS
};
typedef struct Ints15 {
int len;
int data[15];
} Ints15;
typedef struct DigestAlg {
int alg;
DigestState* (*fun)(uchar*,ulong,uchar*,DigestState*);
int len;
} DigestAlg;
static DigestAlg alg_md5 = { ALG_md5, md5, MD5dlen};
static DigestAlg alg_sha1 = { ALG_sha1, sha1, SHA1dlen };
static DigestAlg alg_sha256 = { ALG_sha256, sha2_256, SHA2_256dlen };
static DigestAlg alg_sha384 = { ALG_sha384, sha2_384, SHA2_384dlen };
static DigestAlg alg_sha512 = { ALG_sha512, sha2_512, SHA2_512dlen };
static DigestAlg alg_sha224 = { ALG_sha224, sha2_224, SHA2_224dlen };
/* maximum length of digest output of the digest algs above */
enum {
MAXdlen = SHA2_512dlen,
};
static Ints15 oid_rsaEncryption = {7, 1, 2, 840, 113549, 1, 1, 1 };
static Ints15 oid_md2WithRSAEncryption = {7, 1, 2, 840, 113549, 1, 1, 2 };
static Ints15 oid_md4WithRSAEncryption = {7, 1, 2, 840, 113549, 1, 1, 3 };
static Ints15 oid_md5WithRSAEncryption = {7, 1, 2, 840, 113549, 1, 1, 4 };
static Ints15 oid_sha1WithRSAEncryption ={7, 1, 2, 840, 113549, 1, 1, 5 };
static Ints15 oid_sha1WithRSAEncryptionOiw ={6, 1, 3, 14, 3, 2, 29 };
static Ints15 oid_sha256WithRSAEncryption = {7, 1, 2, 840, 113549, 1, 1, 11 };
static Ints15 oid_sha384WithRSAEncryption = {7, 1, 2, 840, 113549, 1, 1, 12 };
static Ints15 oid_sha512WithRSAEncryption = {7, 1, 2, 840, 113549, 1, 1, 13 };
static Ints15 oid_sha224WithRSAEncryption = {7, 1, 2, 840, 113549, 1, 1, 14 };
static Ints15 oid_ecPublicKey = {6, 1, 2, 840, 10045, 2, 1 };
static Ints15 oid_sha1WithECDSA = {6, 1, 2, 840, 10045, 4, 1 };
static Ints15 oid_sha256WithECDSA = {7, 1, 2, 840, 10045, 4, 3, 2 };
static Ints15 oid_sha384WithECDSA = {7, 1, 2, 840, 10045, 4, 3, 3 };
static Ints15 oid_sha512WithECDSA = {7, 1, 2, 840, 10045, 4, 3, 4 };
static Ints15 oid_md5 = {6, 1, 2, 840, 113549, 2, 5 };
static Ints15 oid_sha1 = {6, 1, 3, 14, 3, 2, 26 };
static Ints15 oid_sha256= {9, 2, 16, 840, 1, 101, 3, 4, 2, 1 };
static Ints15 oid_sha384= {9, 2, 16, 840, 1, 101, 3, 4, 2, 2 };
static Ints15 oid_sha512= {9, 2, 16, 840, 1, 101, 3, 4, 2, 3 };
static Ints15 oid_sha224= {9, 2, 16, 840, 1, 101, 3, 4, 2, 4 };
static Ints *alg_oid_tab[NUMALGS+1] = {
(Ints*)&oid_rsaEncryption,
(Ints*)&oid_md2WithRSAEncryption,
(Ints*)&oid_md4WithRSAEncryption,
(Ints*)&oid_md5WithRSAEncryption,
(Ints*)&oid_sha1WithRSAEncryption,
(Ints*)&oid_sha1WithRSAEncryptionOiw,
(Ints*)&oid_sha256WithRSAEncryption,
(Ints*)&oid_sha384WithRSAEncryption,
(Ints*)&oid_sha512WithRSAEncryption,
(Ints*)&oid_sha224WithRSAEncryption,
(Ints*)&oid_ecPublicKey,
(Ints*)&oid_sha1WithECDSA,
(Ints*)&oid_sha256WithECDSA,
(Ints*)&oid_sha384WithECDSA,
(Ints*)&oid_sha512WithECDSA,
(Ints*)&oid_md5,
(Ints*)&oid_sha1,
(Ints*)&oid_sha256,
(Ints*)&oid_sha384,
(Ints*)&oid_sha512,
(Ints*)&oid_sha224,
nil
};
static DigestAlg *digestalg[NUMALGS+1] = {
&alg_md5, &alg_md5, &alg_md5, &alg_md5,
&alg_sha1, &alg_sha1,
&alg_sha256, &alg_sha384, &alg_sha512, &alg_sha224,
&alg_sha256, &alg_sha1, &alg_sha256, &alg_sha384, &alg_sha512,
&alg_md5, &alg_sha1, &alg_sha256, &alg_sha384, &alg_sha512, &alg_sha224,
nil
};
static Bytes* encode_digest(DigestAlg *da, uchar *digest);
static Ints15 oid_secp256r1 = {7, 1, 2, 840, 10045, 3, 1, 7};
static Ints15 oid_secp384r1 = {5, 1, 3, 132, 0, 34};
static Ints *namedcurves_oid_tab[] = {
(Ints*)&oid_secp256r1,
(Ints*)&oid_secp384r1,
nil,
};
static void (*namedcurves[])(mpint *p, mpint *a, mpint *b, mpint *x, mpint *y, mpint *n, mpint *h) = {
secp256r1,
secp384r1,
nil,
};
static void
freecert(CertX509* c)
{
if(c == nil)
return;
free(c->issuer);
free(c->validity_start);
free(c->validity_end);
free(c->subject);
freebits(c->publickey);
freebits(c->signature);
free(c);
}
/*
* Parse the Name ASN1 type.
* The sequence of RelativeDistinguishedName's gives a sort of pathname,
* from most general to most specific. Each element of the path can be
* one or more (but usually just one) attribute-value pair, such as
* countryName="US".
* We'll just form a "postal-style" address string by concatenating the elements
* from most specific to least specific, separated by commas.
* Return name-as-string (which must be freed by caller).
*/
static char*
parse_name(Elem* e)
{
Elist* el;
Elem* es;
Elist* esetl;
Elem* eat;
Elist* eatl;
char* s;
enum { MAXPARTS = 100 };
char* parts[MAXPARTS];
int i;
int plen;
char* ans = nil;
if(!is_seq(e, &el))
goto errret;
i = 0;
plen = 0;
while(el != nil) {
es = &el->hd;
if(!is_set(es, &esetl))
goto errret;
while(esetl != nil) {
eat = &esetl->hd;
if(!is_seq(eat, &eatl) || elistlen(eatl) != 2)
goto errret;
if(!is_string(&eatl->tl->hd, &s) || i>=MAXPARTS)
goto errret;
parts[i++] = s;
plen += strlen(s) + 2; /* room for ", " after */
esetl = esetl->tl;
}
el = el->tl;
}
if(i > 0) {
ans = (char*)emalloc(plen);
*ans = '\0';
while(--i >= 0) {
s = parts[i];
strcat(ans, s);
if(i > 0)
strcat(ans, ", ");
}
}
errret:
return ans;
}
/*
* Parse an AlgorithmIdentifer ASN1 type.
* Look up the oid in oid_tab and return one of OID_rsaEncryption, etc..,
* or -1 if not found.
* For now, ignore parameters, since none of our algorithms need them.
*/
static int
parse_alg(Elem* e)
{
Elist* el;
Ints* oid;
if(!is_seq(e, &el) || el == nil || !is_oid(&el->hd, &oid))
return -1;
return oid_lookup(oid, alg_oid_tab);
}
static int
parse_curve(Elem* e)
{
Elist* el;
Ints* oid;
if(!is_seq(e, &el) || elistlen(el)<2 || !is_oid(&el->tl->hd, &oid))
return -1;
return oid_lookup(oid, namedcurves_oid_tab);
}
static CertX509*
decode_cert(uchar *buf, int len)
{
int ok = 0;
int n;
Elem ecert;
Elem* ecertinfo;
Elem* esigalg;
Elem* esig;
Elem* eserial;
Elem* eissuer;
Elem* evalidity;
Elem* esubj;
Elem* epubkey;
Elist* el;
Elist* elcert = nil;
Elist* elcertinfo = nil;
Elist* elvalidity = nil;
Elist* elpubkey = nil;
Bits* bits = nil;
Bytes* b;
Elem* e;
CertX509* c = nil;
if(decode(buf, len, &ecert) != ASN_OK)
goto errret;
c = (CertX509*)emalloc(sizeof(CertX509));
c->serial = -1;
c->issuer = nil;
c->validity_start = nil;
c->validity_end = nil;
c->subject = nil;
c->publickey_alg = -1;
c->publickey = nil;
c->signature_alg = -1;
c->signature = nil;
/* Certificate */
if(!is_seq(&ecert, &elcert) || elistlen(elcert) !=3)
goto errret;
ecertinfo = &elcert->hd;
el = elcert->tl;
esigalg = &el->hd;
c->signature_alg = parse_alg(esigalg);
el = el->tl;
esig = &el->hd;
/* Certificate Info */
if(!is_seq(ecertinfo, &elcertinfo))
goto errret;
n = elistlen(elcertinfo);
if(n < 6)
goto errret;
eserial =&elcertinfo->hd;
el = elcertinfo->tl;
/* check for optional version, marked by explicit context tag 0 */
if(eserial->tag.class == Context && eserial->tag.num == 0) {
eserial = &el->hd;
if(n < 7)
goto errret;
el = el->tl;
}
if(parse_alg(&el->hd) != c->signature_alg)
goto errret;
el = el->tl;
eissuer = &el->hd;
el = el->tl;
evalidity = &el->hd;
el = el->tl;
esubj = &el->hd;
el = el->tl;
epubkey = &el->hd;
if(!is_int(eserial, &c->serial)) {
if(!is_bigint(eserial, &b))
goto errret;
c->serial = -1; /* else we have to change cert struct */
}
c->issuer = parse_name(eissuer);
if(c->issuer == nil)
goto errret;
/* Validity */
if(!is_seq(evalidity, &elvalidity))
goto errret;
if(elistlen(elvalidity) != 2)
goto errret;
e = &elvalidity->hd;
if(!is_time(e, &c->validity_start))
goto errret;
e->val.u.stringval = nil; /* string ownership transfer */
e = &elvalidity->tl->hd;
if(!is_time(e, &c->validity_end))
goto errret;
e->val.u.stringval = nil; /* string ownership transfer */
/* resume CertificateInfo */
c->subject = parse_name(esubj);
if(c->subject == nil)
goto errret;
/* SubjectPublicKeyInfo */
if(!is_seq(epubkey, &elpubkey))
goto errret;
if(elistlen(elpubkey) != 2)
goto errret;
c->publickey_alg = parse_alg(&elpubkey->hd);
if(c->publickey_alg < 0)
goto errret;
c->curve = -1;
if(c->publickey_alg == ALG_ecPublicKey){
c->curve = parse_curve(&elpubkey->hd);
if(c->curve < 0)
goto errret;
}
elpubkey = elpubkey->tl;
if(!is_bitstring(&elpubkey->hd, &bits))
goto errret;
elpubkey->hd.val.u.bitstringval = nil; /* transfer ownership */
c->publickey = bits;
/*resume Certificate */
if(c->signature_alg < 0)
goto errret;
if(!is_bitstring(esig, &bits))
goto errret;
esig->val.u.bitstringval = nil; /* transfer ownership */
c->signature = bits;
ok = 1;
errret:
freevalfields(&ecert.val); /* recurses through lists, too */
if(!ok){
freecert(c);
c = nil;
}
return c;
}
/*
* RSAPublickKey ::= SEQUENCE {
* modulus INTEGER,
* publicExponent INTEGER
* }
*/
RSApub*
asn1toRSApub(uchar *buf, int len)
{
Elem e;
Elist *el;
RSApub* key;
key = nil;
if(decode(buf, len, &e) != ASN_OK)
goto errret;
if(!is_seq(&e, &el) || elistlen(el) != 2)
goto errret;
key = rsapuballoc();
if((key->n = asn1mpint(&el->hd)) == nil)
goto errret;
el = el->tl;
if((key->ek = asn1mpint(&el->hd)) == nil)
goto errret;
freevalfields(&e.val);
return key;
errret:
freevalfields(&e.val);
rsapubfree(key);
return nil;
}
/*
* RSAPrivateKey ::= SEQUENCE {
* version Version,
* modulus INTEGER, -- n
* publicExponent INTEGER, -- e
* privateExponent INTEGER, -- d
* prime1 INTEGER, -- p
* prime2 INTEGER, -- q
* exponent1 INTEGER, -- d mod (p-1)
* exponent2 INTEGER, -- d mod (q-1)
* coefficient INTEGER -- (inverse of q) mod p }
*/
RSApriv*
asn1toRSApriv(uchar *buf, int len)
{
int version;
Elem e;
Elist *el;
Bytes *b;
RSApriv* key = nil;
if(decode(buf, len, &e) != ASN_OK)
goto errret;
if(!is_seq(&e, &el))
goto errret;
if(!is_int(&el->hd, &version) || version != 0)
goto errret;
if(elistlen(el) != 9){
if(elistlen(el) == 3
&& parse_alg(&el->tl->hd) == ALG_rsaEncryption
&& is_octetstring(&el->tl->tl->hd, &b)){
key = asn1toRSApriv(b->data, b->len);
if(key != nil)
goto done;
}
goto errret;
}
key = rsaprivalloc();
el = el->tl;
if((key->pub.n = asn1mpint(&el->hd)) == nil)
goto errret;
el = el->tl;
if((key->pub.ek = asn1mpint(&el->hd)) == nil)
goto errret;
el = el->tl;
if((key->dk = asn1mpint(&el->hd)) == nil)
goto errret;
el = el->tl;
if((key->q = asn1mpint(&el->hd)) == nil)
goto errret;
el = el->tl;
if((key->p = asn1mpint(&el->hd)) == nil)
goto errret;
el = el->tl;
if((key->kq = asn1mpint(&el->hd)) == nil)
goto errret;
el = el->tl;
if((key->kp = asn1mpint(&el->hd)) == nil)
goto errret;
el = el->tl;
if((key->c2 = asn1mpint(&el->hd)) == nil)
goto errret;
done:
freevalfields(&e.val);
return key;
errret:
freevalfields(&e.val);
rsaprivfree(key);
return nil;
}
/*
* digest(CertificateInfo)
* Our ASN.1 library doesn't return pointers into the original
* data array, so we need to do a little hand decoding.
*/
static int
digest_certinfo(uchar *cert, int ncert, DigestAlg *da, uchar *digest)
{
uchar *info, *p, *pend;
int isconstr, length;
Tag tag;
Elem elem;
p = cert;
pend = cert + ncert;
if(tag_decode(&p, pend, &tag, &isconstr) != ASN_OK ||
tag.class != Universal || tag.num != SEQUENCE ||
length_decode(&p, pend, &length) != ASN_OK ||
p+length > pend ||
p+length < p)
return -1;
info = p;
if(ber_decode(&p, pend, &elem) != ASN_OK)
return -1;
freevalfields(&elem.val);
if(elem.tag.num != SEQUENCE)
return -1;
(*da->fun)(info, p - info, digest, nil);
return da->len;
}
mpint*
pkcs1padbuf(uchar *buf, int len, mpint *modulus, int blocktype)
{
int i, n = (mpsignif(modulus)-1)/8;
int pad = n - 2 - len;
uchar *p;
mpint *mp;
if(pad < 8){
werrstr("rsa modulus too small");
return nil;
}
if((p = malloc(n)) == nil)
return nil;
p[0] = blocktype;
switch(blocktype){
default:
case 1:
memset(p+1, 0xFF, pad);
break;
case 2:
for(i=1; i <= pad; i++)
p[i] = 1 + nfastrand(255);
break;
}
p[1+pad] = 0;
memmove(p+2+pad, buf, len);
mp = betomp(p, n, nil);
free(p);
return mp;
}
int
pkcs1unpadbuf(uchar *buf, int len, mpint *modulus, int blocktype)
{
uchar *p = buf + 1, *e = buf + len;
if(len < 1 || len != (mpsignif(modulus)-1)/8 || buf[0] != blocktype)
return -1;
switch(blocktype){
default:
case 1:
while(p < e && *p == 0xFF)
p++;
break;
case 2:
while(p < e && *p != 0x00)
p++;
break;
}
if(p - buf <= 8 || p >= e || *p++ != 0x00)
return -1;
memmove(buf, p, len = e - p);
return len;
}
static char Ebadsig[] = "bad signature";
char*
X509rsaverifydigest(uchar *sig, int siglen, uchar *edigest, int edigestlen, RSApub *pk)
{
mpint *x, *y;
DigestAlg **dp;
Bytes *digest;
uchar *buf;
int len;
char *err;
x = betomp(sig, siglen, nil);
y = rsaencrypt(pk, x, nil);
mpfree(x);
len = mptobe(y, nil, 0, &buf);
mpfree(y);
err = Ebadsig;
len = pkcs1unpadbuf(buf, len, pk->n, 1);
if(len == edigestlen && tsmemcmp(buf, edigest, edigestlen) == 0)
err = nil;
for(dp = digestalg; err != nil && *dp != nil; dp++){
if((*dp)->len != edigestlen)
continue;
digest = encode_digest(*dp, edigest);
if(digest->len == len && tsmemcmp(digest->data, buf, len) == 0)
err = nil;
freebytes(digest);
}
free(buf);
return err;
}
char*
X509ecdsaverifydigest(uchar *sig, int siglen, uchar *edigest, int edigestlen, ECdomain *dom, ECpub *pub)
{
Elem e;
Elist *el;
mpint *r, *s;
char *err;
r = s = nil;
err = Ebadsig;
if(decode(sig, siglen, &e) != ASN_OK)
goto end;
if(!is_seq(&e, &el) || elistlen(el) != 2)
goto end;
r = asn1mpint(&el->hd);
if(r == nil)
goto end;
el = el->tl;
s = asn1mpint(&el->hd);
if(s == nil)
goto end;
if(ecdsaverify(dom, pub, edigest, edigestlen, r, s))
err = nil;
end:
freevalfields(&e.val);
mpfree(s);
mpfree(r);
return err;
}
static void
copysubject(char *name, int nname, char *subject)
{
char *e;
if(name == nil)
return;
memset(name, 0, nname);
if(subject == nil)
return;
strncpy(name, subject, nname-1);
e = strchr(name, ',');
if(e != nil)
*e = 0; /* take just CN part of Distinguished Name */
}
ECpub*
X509toECpub(uchar *cert, int ncert, char *name, int nname, ECdomain *dom)
{
CertX509 *c;
ECpub *pub;
c = decode_cert(cert, ncert);
if(c == nil)
return nil;
copysubject(name, nname, c->subject);
pub = nil;
if(c->publickey_alg == ALG_ecPublicKey){
ecdominit(dom, namedcurves[c->curve]);
pub = ecdecodepub(dom, c->publickey->data, c->publickey->len);
if(pub == nil)
ecdomfree(dom);
}
freecert(c);
return pub;
}
char*
X509ecdsaverify(uchar *cert, int ncert, ECdomain *dom, ECpub *pk)
{
char *e;
CertX509 *c;
int digestlen;
uchar digest[MAXdlen];
c = decode_cert(cert, ncert);
if(c == nil)
return "cannot decode cert";
digestlen = digest_certinfo(cert, ncert, digestalg[c->signature_alg], digest);
if(digestlen <= 0){
freecert(c);
return "cannot decode certinfo";
}
e = X509ecdsaverifydigest(c->signature->data, c->signature->len, digest, digestlen, dom, pk);
freecert(c);
return e;
}
RSApub*
X509toRSApub(uchar *cert, int ncert, char *name, int nname)
{
CertX509 *c;
RSApub *pub;
c = decode_cert(cert, ncert);
if(c == nil)
return nil;
copysubject(name, nname, c->subject);
pub = nil;
if(c->publickey_alg == ALG_rsaEncryption)
pub = asn1toRSApub(c->publickey->data, c->publickey->len);
freecert(c);
return pub;
}
char*
X509rsaverify(uchar *cert, int ncert, RSApub *pk)
{
char *e;
CertX509 *c;
int digestlen;
uchar digest[MAXdlen];
c = decode_cert(cert, ncert);
if(c == nil)
return "cannot decode cert";
digestlen = digest_certinfo(cert, ncert, digestalg[c->signature_alg], digest);
if(digestlen <= 0){
freecert(c);
return "cannot decode certinfo";
}
e = X509rsaverifydigest(c->signature->data, c->signature->len, digest, digestlen, pk);
freecert(c);
return e;
}
/* ------- Elem constructors ---------- */
static Elem
Null(void)
{
Elem e;
e.tag.class = Universal;
e.tag.num = NULLTAG;
e.val.tag = VNull;
return e;
}
static Elem
mkint(int j)
{
Elem e;
e.tag.class = Universal;
e.tag.num = INTEGER;
e.val.tag = VInt;
e.val.u.intval = j;
return e;
}
static Elem
mkbigint(mpint *p)
{
Elem e;
e.tag.class = Universal;
e.tag.num = INTEGER;
e.val.tag = VBigInt;
e.val.u.bigintval = newbytes((mpsignif(p)+8)/8);
if(p->sign < 0){
mpint *s = mpnew(e.val.u.bigintval->len*8+1);
mpleft(mpone, e.val.u.bigintval->len*8, s);
mpadd(p, s, s);
mptober(s, e.val.u.bigintval->data, e.val.u.bigintval->len);
mpfree(s);
} else {
mptober(p, e.val.u.bigintval->data, e.val.u.bigintval->len);
}
return e;
}
static int
printable(char *s)
{
int c;
while((c = (uchar)*s++) != 0){
if((c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z')
|| (c >= '0' && c <= '9')
|| strchr("'=()+,-./:? ", c) != nil)
continue;
return 0;
}
return 1;
}
#define DirectoryString 0
static Elem
mkstring(char *s, int t)
{
Elem e;
if(t == DirectoryString)
t = printable(s) ? PrintableString : UTF8String;
e.tag.class = Universal;
e.tag.num = t;
e.val.tag = VString;
e.val.u.stringval = estrdup(s);
return e;
}
static Elem
mkoctet(uchar *buf, int buflen)
{
Elem e;
e.tag.class = Universal;
e.tag.num = OCTET_STRING;
e.val.tag = VOctets;
e.val.u.octetsval = makebytes(buf, buflen);
return e;
}
static Elem
mkbits(uchar *buf, int buflen)
{
Elem e;
e.tag.class = Universal;
e.tag.num = BIT_STRING;
e.val.tag = VBitString;
e.val.u.bitstringval = makebits(buf, buflen, 0);
return e;
}
static Elem
mkutc(long t)
{
Elem e;
char utc[50];
Tm *tm = gmtime(t);
e.tag.class = Universal;
e.tag.num = UTCTime;
e.val.tag = VString;
snprint(utc, sizeof(utc), "%.2d%.2d%.2d%.2d%.2d%.2dZ",
tm->year % 100, tm->mon+1, tm->mday, tm->hour, tm->min, tm->sec);
e.val.u.stringval = estrdup(utc);
return e;
}
static Elem
mkoid(Ints *oid)
{
Elem e;
e.tag.class = Universal;
e.tag.num = OBJECT_ID;
e.val.tag = VObjId;
e.val.u.objidval = makeints(oid->data, oid->len);
return e;
}
static Elem
mkseq(Elist *el)
{
Elem e;
e.tag.class = Universal;
e.tag.num = SEQUENCE;
e.val.tag = VSeq;
e.val.u.seqval = el;
return e;
}
static Elem
mkset(Elist *el)
{
Elem e;
e.tag.class = Universal;
e.tag.num = SETOF;
e.val.tag = VSet;
e.val.u.setval = el;
return e;
}
static Elem
mkalg(int alg)
{
return mkseq(mkel(mkoid(alg_oid_tab[alg]), mkel(Null(), nil)));
}
typedef struct Ints7pref {
int len;
int data[7];
char prefix[4];
int stype;
} Ints7pref;
Ints7pref DN_oid[] = {
{4, 2, 5, 4, 6, 0, 0, 0, "C=", PrintableString},
{4, 2, 5, 4, 8, 0, 0, 0, "ST=",DirectoryString},
{4, 2, 5, 4, 7, 0, 0, 0, "L=", DirectoryString},
{4, 2, 5, 4, 10, 0, 0, 0, "O=", DirectoryString},
{4, 2, 5, 4, 11, 0, 0, 0, "OU=",DirectoryString},
{4, 2, 5, 4, 3, 0, 0, 0, "CN=",DirectoryString},
{7, 1,2,840,113549,1,9,1, "E=", IA5String},
{7, 0,9,2342,19200300,100,1,25, "DC=",IA5String},
};
static Elem
mkname(Ints7pref *oid, char *subj)
{
return mkset(mkel(mkseq(mkel(mkoid((Ints*)oid), mkel(mkstring(subj, oid->stype), nil))), nil));
}
static Elem
mkDN(char *dn)
{
int i, j, nf;
char *f[20], *prefix, *d2 = estrdup(dn);
Elist* el = nil;
nf = tokenize(d2, f, nelem(f));
for(i=nf-1; i>=0; i--){
for(j=0; j<nelem(DN_oid); j++){
prefix = DN_oid[j].prefix;
if(strncmp(f[i],prefix,strlen(prefix))==0){
el = mkel(mkname(&DN_oid[j],f[i]+strlen(prefix)), el);
break;
}
}
}
free(d2);
return mkseq(el);
}
/*
* DigestInfo ::= SEQUENCE {
* digestAlgorithm AlgorithmIdentifier,
* digest OCTET STRING }
*/
static Bytes*
encode_digest(DigestAlg *da, uchar *digest)
{
Bytes *b = nil;
Elem e = mkseq(
mkel(mkalg(da->alg),
mkel(mkoctet(digest, da->len),
nil)));
encode(e, &b);
freevalfields(&e.val);
return b;
}
int
asn1encodedigest(DigestState* (*fun)(uchar*, ulong, uchar*, DigestState*), uchar *digest, uchar *buf, int len)
{
Bytes *bytes;
DigestAlg **dp;
for(dp = digestalg; *dp != nil; dp++){
if((*dp)->fun != fun)
continue;
bytes = encode_digest(*dp, digest);
if(bytes == nil)
break;
if(bytes->len > len){
freebytes(bytes);
break;
}
len = bytes->len;
memmove(buf, bytes->data, len);
freebytes(bytes);
return len;
}
return -1;
}
static Elem
mkcont(Elem e, int num)
{
e = mkseq(mkel(e, nil));
e.tag.class = Context;
e.tag.num = num;
return e;
}
static Elem
mkaltname(char *s)
{
Elem e;
int i;
for(i=0; i<nelem(DN_oid); i++){
if(strstr(s, DN_oid[i].prefix) != nil)
return mkcont(mkDN(s), 4); /* DN */
}
e = mkstring(s, IA5String);
e.tag.class = Context;
e.tag.num = strchr(s, '@') != nil ? 1 : 2; /* email : DNS */
return e;
}
static Elist*
mkaltnames(char *alts)
{
Elist *el;
char *s, *p;
if(alts == nil)
return nil;
el = nil;
alts = estrdup(alts);
for(s = alts; s != nil; s = p){
while(*s == ' ')
s++;
if(*s == '\0')
break;
if((p = strchr(s, ',')) != nil)
*p++ = 0;
el = mkel(mkaltname(s), el);
}
free(alts);
return el;
}
static Elist*
mkextel(Elem e, Ints *oid, Elist *el)
{
Bytes *b = nil;
if(encode(e, &b) == ASN_OK){
el = mkel(mkseq(
mkel(mkoid(oid),
mkel(mkoctet(b->data, b->len),
nil))), el);
freebytes(b);
}
freevalfields(&e.val);
return el;
}
static Ints15 oid_subjectAltName = {4, 2, 5, 29, 17 };
static Ints15 oid_extensionRequest = { 7, 1, 2, 840, 113549, 1, 9, 14};
static Elist*
mkextensions(char *alts, int req)
{
Elist *sl, *xl;
xl = nil;
if((sl = mkaltnames(alts)) != nil)
xl = mkextel(mkseq(sl), (Ints*)&oid_subjectAltName, xl);
if(xl != nil){
if(req) return mkel(mkcont(mkseq(
mkel(mkoid((Ints*)&oid_extensionRequest),
mkel(mkset(mkel(mkseq(xl), nil)), nil))), 0), nil);
return mkel(mkcont(mkseq(xl), 3), nil);
}
return nil;
}
static char*
splitalts(char *s)
{
int q;
for(q = 0; *s != '\0'; s++){
if(*s == '\'')
q ^= 1;
else if(q == 0 && *s == ','){
*s++ = 0;
return s;
}
}
return nil;
}
static Bytes*
encode_rsapubkey(RSApub *pk)
{
Bytes *b = nil;
Elem e = mkseq(
mkel(mkbigint(pk->n),
mkel(mpsignif(pk->ek)<32 ? mkint(mptoi(pk->ek)) : mkbigint(pk->ek),
nil)));
encode(e, &b);
freevalfields(&e.val);
return b;
}
int
asn1encodeRSApub(RSApub *pk, uchar *buf, int len)
{
Bytes *b = encode_rsapubkey(pk);
if(b == nil)
return -1;
if(b->len > len){
freebytes(b);
werrstr("buffer too small");
return -1;
}
memmove(buf, b->data, len = b->len);
freebytes(b);
return len;
}
uchar*
X509rsagen(RSApriv *priv, char *subj, ulong valid[2], int *certlen)
{
int serial = 0, sigalg = ALG_sha256WithRSAEncryption;
uchar *cert = nil;
Bytes *certbytes, *pkbytes, *certinfobytes, *sigbytes;
Elem e, certinfo;
DigestAlg *da;
uchar digest[MAXdlen], *buf;
int buflen;
mpint *pkcs1;
char *alts;
if((pkbytes = encode_rsapubkey(&priv->pub)) == nil)
return nil;
subj = estrdup(subj);
alts = splitalts(subj);
e = mkseq(
mkel(mkcont(mkint(2), 0),
mkel(mkint(serial),
mkel(mkalg(sigalg),
mkel(mkDN(subj),
mkel(mkseq(
mkel(mkutc(valid[0]),
mkel(mkutc(valid[1]),
nil))),
mkel(mkDN(subj),
mkel(mkseq(
mkel(mkalg(ALG_rsaEncryption),
mkel(mkbits(pkbytes->data, pkbytes->len),
nil))),
mkextensions(alts, 0)))))))));
freebytes(pkbytes);
if(encode(e, &certinfobytes) != ASN_OK)
goto errret;
da = digestalg[sigalg];
(*da->fun)(certinfobytes->data, certinfobytes->len, digest, 0);
freebytes(certinfobytes);
certinfo = e;
sigbytes = encode_digest(da, digest);
if(sigbytes == nil)
goto errret;
pkcs1 = pkcs1padbuf(sigbytes->data, sigbytes->len, priv->pub.n, 1);
freebytes(sigbytes);
if(pkcs1 == nil)
goto errret;
rsadecrypt(priv, pkcs1, pkcs1);
buflen = mptobe(pkcs1, nil, 0, &buf);
mpfree(pkcs1);
e = mkseq(
mkel(certinfo,
mkel(mkalg(sigalg),
mkel(mkbits(buf, buflen),
nil))));
free(buf);
if(encode(e, &certbytes) != ASN_OK)
goto errret;
if(certlen != nil)
*certlen = certbytes->len;
cert = (uchar*)certbytes;
memmove(cert, certbytes->data, certbytes->len);
errret:
freevalfields(&e.val);
free(subj);
return cert;
}
uchar*
X509rsareq(RSApriv *priv, char *subj, int *certlen)
{
/* RFC 2314, PKCS #10 Certification Request Syntax */
int version = 0, sigalg = ALG_sha256WithRSAEncryption;
uchar *cert = nil;
Bytes *certbytes, *pkbytes, *certinfobytes, *sigbytes;
Elem e, certinfo;
DigestAlg *da;
uchar digest[MAXdlen], *buf;
int buflen;
mpint *pkcs1;
char *alts;
if((pkbytes = encode_rsapubkey(&priv->pub)) == nil)
return nil;
subj = estrdup(subj);
alts = splitalts(subj);
e = mkseq(
mkel(mkint(version),
mkel(mkDN(subj),
mkel(mkseq(
mkel(mkalg(ALG_rsaEncryption),
mkel(mkbits(pkbytes->data, pkbytes->len),
nil))),
mkextensions(alts, 1)))));
freebytes(pkbytes);
if(encode(e, &certinfobytes) != ASN_OK)
goto errret;
da = digestalg[sigalg];
(*da->fun)(certinfobytes->data, certinfobytes->len, digest, 0);
freebytes(certinfobytes);
certinfo = e;
sigbytes = encode_digest(da, digest);
if(sigbytes == nil)
goto errret;
pkcs1 = pkcs1padbuf(sigbytes->data, sigbytes->len, priv->pub.n, 1);
freebytes(sigbytes);
if(pkcs1 == nil)
goto errret;
rsadecrypt(priv, pkcs1, pkcs1);
buflen = mptobe(pkcs1, nil, 0, &buf);
mpfree(pkcs1);
e = mkseq(
mkel(certinfo,
mkel(mkalg(sigalg),
mkel(mkbits(buf, buflen),
nil))));
free(buf);
if(encode(e, &certbytes) != ASN_OK)
goto errret;
if(certlen != nil)
*certlen = certbytes->len;
cert = (uchar*)certbytes;
memmove(cert, certbytes->data, certbytes->len);
errret:
freevalfields(&e.val);
free(subj);
return cert;
}
static void
digestSPKI(int alg, uchar *pubkey, int npubkey, DigestState* (*fun)(uchar*, ulong, uchar*, DigestState*), uchar *digest)
{
Bytes *b = nil;
Elem e = mkseq(mkel(mkalg(alg), mkel(mkbits(pubkey, npubkey), nil)));
encode(e, &b);
freevalfields(&e.val);
(*fun)(b->data, b->len, digest, nil);
freebytes(b);
}
int
X509digestSPKI(uchar *cert, int ncert, DigestState* (*fun)(uchar*, ulong, uchar*, DigestState*), uchar *digest)
{
CertX509 *c;
c = decode_cert(cert, ncert);
if(c == nil){
werrstr("cannot decode cert");
return -1;
}
digestSPKI(c->publickey_alg, c->publickey->data, c->publickey->len, fun, digest);
freecert(c);
return 0;
}
static char*
tagdump(Tag tag)
{
static char buf[32];
if(tag.class != Universal){
snprint(buf, sizeof(buf), "class%d,num%d", tag.class, tag.num);
return buf;
}
switch(tag.num){
case BOOLEAN: return "BOOLEAN";
case INTEGER: return "INTEGER";
case BIT_STRING: return "BIT STRING";
case OCTET_STRING: return "OCTET STRING";
case NULLTAG: return "NULLTAG";
case OBJECT_ID: return "OID";
case ObjectDescriptor: return "OBJECT_DES";
case EXTERNAL: return "EXTERNAL";
case REAL: return "REAL";
case ENUMERATED: return "ENUMERATED";
case EMBEDDED_PDV: return "EMBEDDED PDV";
case SEQUENCE: return "SEQUENCE";
case SETOF: return "SETOF";
case UTF8String: return "UTF8String";
case NumericString: return "NumericString";
case PrintableString: return "PrintableString";
case TeletexString: return "TeletexString";
case VideotexString: return "VideotexString";
case IA5String: return "IA5String";
case UTCTime: return "UTCTime";
case GeneralizedTime: return "GeneralizedTime";
case GraphicString: return "GraphicString";
case VisibleString: return "VisibleString";
case GeneralString: return "GeneralString";
case UniversalString: return "UniversalString";
case BMPString: return "BMPString";
default:
snprint(buf, sizeof(buf), "Universal,num%d", tag.num);
return buf;
}
}
static void
edump(Elem e)
{
Value v;
Elist *el;
int i;
print("%s{", tagdump(e.tag));
v = e.val;
switch(v.tag){
case VBool: print("Bool %d",v.u.boolval); break;
case VInt: print("Int %d",v.u.intval); break;
case VOctets: print("Octets[%d] %.2x%.2x...",v.u.octetsval->len,v.u.octetsval->data[0],v.u.octetsval->data[1]); break;
case VBigInt: print("BigInt[%d] %.2x%.2x...",v.u.bigintval->len,v.u.bigintval->data[0],v.u.bigintval->data[1]); break;
case VReal: print("Real..."); break;
case VOther: print("Other..."); break;
case VBitString: print("BitString[%d]...", v.u.bitstringval->len*8 - v.u.bitstringval->unusedbits); break;
case VNull: print("Null"); break;
case VEOC: print("EOC..."); break;
case VObjId: print("ObjId");
for(i = 0; i<v.u.objidval->len; i++)
print(" %d", v.u.objidval->data[i]);
break;
case VString: print("String \"%s\"",v.u.stringval); break;
case VSeq: print("Seq\n");
for(el = v.u.seqval; el!=nil; el = el->tl)
edump(el->hd);
break;
case VSet: print("Set\n");
for(el = v.u.setval; el!=nil; el = el->tl)
edump(el->hd);
break;
}
print("}\n");
}
void
asn1dump(uchar *der, int len)
{
Elem e;
if(decode(der, len, &e) != ASN_OK){
print("didn't parse\n");
exits("didn't parse");
}
edump(e);
}
void
X509dump(uchar *cert, int ncert)
{
char *e;
CertX509 *c;
RSApub *rsapub;
ECpub *ecpub;
ECdomain ecdom;
int digestlen;
uchar digest[MAXdlen];
print("begin X509dump\n");
c = decode_cert(cert, ncert);
if(c == nil){
print("cannot decode cert\n");
return;
}
digestlen = digest_certinfo(cert, ncert, digestalg[c->signature_alg], digest);
if(digestlen <= 0){
freecert(c);
print("cannot decode certinfo\n");
return;
}
print("serial %d\n", c->serial);
print("issuer %s\n", c->issuer);
print("validity %s %s\n", c->validity_start, c->validity_end);
print("subject %s\n", c->subject);
print("sigalg=%d digest=%.*H\n", c->signature_alg, digestlen, digest);
print("publickey_alg=%d pubkey[%d] %.*H\n", c->publickey_alg, c->publickey->len,
c->publickey->len, c->publickey->data);
switch(c->publickey_alg){
case ALG_rsaEncryption:
rsapub = asn1toRSApub(c->publickey->data, c->publickey->len);
if(rsapub != nil){
print("rsa pubkey e=%B n(%d)=%B\n", rsapub->ek, mpsignif(rsapub->n), rsapub->n);
e = X509rsaverifydigest(c->signature->data, c->signature->len,
digest, digestlen, rsapub);
if(e==nil)
e = "nil (meaning ok)";
print("self-signed X509rsaverifydigest returns: %s\n", e);
rsapubfree(rsapub);
}
break;
case ALG_ecPublicKey:
ecdominit(&ecdom, namedcurves[c->curve]);
ecpub = ecdecodepub(&ecdom, c->publickey->data, c->publickey->len);
if(ecpub != nil){
e = X509ecdsaverifydigest(c->signature->data, c->signature->len,
digest, digestlen, &ecdom, ecpub);
if(e==nil)
e = "nil (meaning ok)";
print("self-signed X509ecdsaverifydigest returns: %s\n", e);
ecpubfree(ecpub);
}
ecdomfree(&ecdom);
break;
}
digestSPKI(c->publickey_alg, c->publickey->data, c->publickey->len, sha2_256, digest);
print("publickey_thumbprint sha256=%.*[\n", SHA2_256dlen, digest);
sha2_256(cert, ncert, digest, nil);
print("cert_thumbprint sha256=%.*[\n", SHA2_256dlen, digest);
sha1(cert, ncert, digest, nil);
print("cert_thumbprint sha1=%.*H\n", SHA1dlen, digest);
freecert(c);
print("end X509dump\n");
}
|