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
|
char *cksslv = "SSL/TLS support, 9.0.227, 04 Aug 2010";
/*
C K _ S S L . C -- OpenSSL Interface for C-Kermit
Copyright (C) 1985, 2010,
Trustees of Columbia University in the City of New York.
All rights reserved. See the C-Kermit COPYING.TXT file or the
copyright text in the ckcmai.c module for disclaimer and permissions.
Author: Jeffrey E Altman (jaltman@secure-endpoints.com)
Secure Endpoints Inc., New York City
Provides:
. Telnet Auth SSL option compatible with Tim Hudson's hack.
. Telnet START_TLS option
. Configuration of certificate and key files
. Certificate verification and revocation list checks
. Client certificate to user id routine
Note: This code is written to be compatible with OpenSSL 0.9.6[abcdefgh]
and 0.9.7 beta 5 (and, presumably, later).
It will also compile with version 0.9.5 although that is discouraged
due to security weaknesses in that release.
*/
#include "ckcsym.h"
#include "ckcdeb.h"
#ifdef CK_SSL
#include "ckcnet.h"
#include "ckuath.h"
#include <stdlib.h>
#include <string.h>
#ifdef UNIX
#include <netinet/in.h>
#ifndef FREEBSD4
#include <arpa/inet.h>
#endif /* FREEBSD4 */
#endif /* UNIX */
#ifdef DEC_TCPIP
#include <time.h>
#include <inet.h>
#endif /* DEC_TCPIP */
#ifdef OS2
extern char exedir[];
#ifdef NT
char * GetAppData(int);
#endif
#endif /* OS2 */
extern int quiet; /* fdc - Mon Nov 28 11:44:15 2005 */
static int ssl_installed = 1;
#endif /* CK_SSL */
int
ck_ssh_is_installed()
{
#ifdef SSHBUILTIN
#ifdef SSLDLL
#ifdef NT
extern HINSTANCE hCRYPTO;
#else /* NT */
extern HMODULE hCRYPTO;
#endif /* NT */
debug(F111,"ck_ssh_is_installed","hCRYPTO",hCRYPTO);
return(ssl_installed && (hCRYPTO != NULL));
#else /* SSLDLL */
return(ssl_installed);
#endif /* SSLDLL */
#else
return 0;
#endif
}
int
#ifdef CK_ANSIC
ck_ssleay_is_installed(void)
#else
ck_ssleay_is_installed()
#endif
{
#ifdef CK_SSL
#ifdef SSLDLL
#ifdef NT
extern HINSTANCE hSSL, hCRYPTO;
#else /* NT */
extern HMODULE hSSL, hCRYPTO;
#endif /* NT */
debug(F111,"ck_ssleay_is_installed","hSSL",hSSL);
debug(F111,"ck_ssleay_is_installed","hCRYPTO",hCRYPTO);
return(ssl_installed && (hSSL != NULL) && (hCRYPTO != NULL));
#else /* SSLDLL */
return(ssl_installed);
#endif /* SSLDLL */
#else /* CK_SSL */
return(0);
#endif /* CK_SSL */
}
#ifdef CK_SSL
#include "ckcker.h"
#include "ckucmd.h" /* For struct keytab */
#include "ckctel.h"
#include "ck_ssl.h"
#ifdef UNIX
#include <pwd.h> /* Password file for home directory */
#endif /* UNIX */
#ifdef OS2
#include <process.h>
#endif /* OS2 */
#ifdef OS2ONLY
#include "ckotcp.h"
#endif /* OS2ONLY */
#ifdef SSLDLL
int ssl_finished_messages = 0;
#else /* SSLDLL */
#ifdef OPENSSL_VERSION_NUMBER
int ssl_finished_messages = (OPENSSL_VERSION_NUMBER >= 0x0090581fL);
#else
!ERROR This module requires OpenSSL 0.9.5a or higher
#endif /* OPENSSL_VERSION_NUMBER */
#endif /* SSLDLL */
static int auth_ssl_valid = 0;
static char *auth_ssl_name = 0; /* this holds the oneline name */
char ssl_err[SSL_ERR_BFSZ]="";
BIO *bio_err=NULL;
X509_STORE *crl_store = NULL;
#ifndef NOFTP
#ifndef SYSFTP
SSL *ssl_ftp_con = NULL;
SSL_CTX *ssl_ftp_ctx = NULL;
SSL *ssl_ftp_data_con = NULL;
int ssl_ftp_active_flag = 0;
int ssl_ftp_data_active_flag = 0;
#endif /* SYSFTP */
#endif /* NOFTP */
#ifndef NOHTTP
SSL *tls_http_con = NULL;
SSL_CTX *tls_http_ctx = NULL;
int tls_http_active_flag = 0;
int ssl_http_initialized = 0;
#endif /* NOHTTP */
SSL_CTX *ssl_ctx = NULL;
SSL *ssl_con = NULL;
int ssl_debug_flag = 0;
int ssl_verbose_flag = 0;
int ssl_only_flag = 0;
int ssl_raw_flag = 0;
int ssl_active_flag = 0;
int ssl_verify_flag = SSL_VERIFY_PEER;
int ssl_certsok_flag = 0;
char *ssl_rsa_cert_file = NULL;
char *ssl_rsa_cert_chain_file = NULL;
char *ssl_rsa_key_file = NULL;
char *ssl_dsa_cert_file = NULL;
char *ssl_dsa_cert_chain_file = NULL;
char *ssl_dh_key_file = NULL;
char *ssl_crl_file = NULL;
char *ssl_crl_dir = NULL;
char *ssl_verify_file = NULL;
char *ssl_verify_dir = NULL;
char *ssl_dh_param_file = NULL;
char *ssl_cipher_list = NULL;
char *ssl_rnd_file = NULL;
SSL_CTX *tls_ctx = NULL;
SSL *tls_con = NULL;
int tls_only_flag = 0;
int tls_raw_flag = 0;
int tls_active_flag = 0;
int ssl_initialized = 0;
int ssl_verify_depth = -1; /* used to track depth in verify routines */
/* compile this set to 1 to negotiate SSL/TLS but not actually start it */
int ssl_dummy_flag=0;
extern int inserver;
extern int debses;
extern int accept_complete;
extern char szHostName[], szUserNameRequested[], szUserNameAuthenticated[];
_PROTOTYP(int X509_to_user,(X509 *, char *, int));
static int verbosity = 0; /* Message control */
static VOID
setverbosity() {
verbosity = ssl_verbose_flag;
if (quiet) verbosity = 0;
}
int
#ifdef CK_ANSIC
ssl_server_verify_callback(int ok, X509_STORE_CTX * ctx)
#else /* CK_ANSIC */
ssl_server_verify_callback(ok, ctx)
int ok;
X509_STORE_CTX *ctx;
#endif /* CK_ANSIC */
{
static char *saved_subject=NULL;
char *subject=NULL, *issuer=NULL;
int depth,error;
X509 *xs = NULL;
if ( ssl_certsok_flag )
return(1);
setverbosity();
error=X509_STORE_CTX_get_error(ctx);
depth=X509_STORE_CTX_get_error_depth(ctx);
xs=X509_STORE_CTX_get_current_cert(ctx);
if (depth==0) {
/* clear things */
if (saved_subject!=NULL) {
free(saved_subject);
saved_subject=NULL;
}
if (auth_ssl_name!=NULL) {
free(auth_ssl_name);
auth_ssl_name=NULL;
}
}
if (ssl_debug_flag && !inserver) {
printf("ssl:server_verify_callback:depth=%d ok=%d err=%d-%s\r\n",
depth,ok,error,X509_verify_cert_error_string(error));
}
/* first thing is to have a meaningful name for the current
* certificate that is being verified ... and if we cannot
* determine that then something is seriously wrong!
*/
makestr(&subject,
(char *)X509_NAME_oneline(X509_get_subject_name(xs),NULL,0));
makestr(&issuer,
(char *)X509_NAME_oneline(X509_get_issuer_name(xs),NULL,0));
if (!subject || !subject[0] || !issuer || !issuer[0]) {
ok = 0;
goto return_time;
}
if (verbosity && !inserver && depth != ssl_verify_depth) {
printf("[%d] Certificate Subject:\r\n%s\r\n",depth,subject);
printf("[%d] Certificate Issuer:\r\n%s\r\n",depth,issuer);
ssl_verify_depth = depth;
}
/* make sure that the certificate that has been presented */
/* has not been revoked (if we have been given a CRL. */
ok = ssl_verify_crl(ok, ctx);
/* if we have any form of error in secure mode we reject the connection */
if (error!=X509_V_OK) {
if (inserver) {
#ifdef CKSYSLOG
if (ckxsyslog >= SYSLG_LI && ckxlogging) {
cksyslog(SYSLG_LI, 0,
"X.509 Certificate verify failure",
(char *) subject,
(char *)X509_verify_cert_error_string(error)
);
}
#endif /* CKSYSLOG */
} else {
if ( ssl_verify_flag &
(SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
printf("Error: ");
else
printf("Warning: ");
switch (error) {
case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
printf("Certificate is self signed.\r\n");
break;
case X509_V_ERR_CERT_HAS_EXPIRED:
printf("Certificate has expired.\r\n");
break;
case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
printf(
"Certificate issuer's certificate isn't available locally.\r\n");
break;
case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
printf("Unable to verify leaf signature.\r\n");
break;
case X509_V_ERR_CERT_REVOKED:
printf("Certificate revoked.\r\n");
break;
default:
printf("Error %d while verifying certificate.\r\n",
ctx->error);
break;
}
}
ok = !(ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
} else {
/* if we got all the way to the top of the tree then
* we *can* use this certificate for a username to
* match ... in all other cases we must not!
*/
auth_ssl_name = saved_subject;
saved_subject = NULL;
}
return_time:
/* save the name if at least the first level is okay */
if (depth == 0 && ok)
makestr(&saved_subject,subject);
/* clean up things */
if (subject!=NULL)
free(subject);
if (issuer!=NULL)
free(issuer);
return ok;
}
int
#ifdef CK_ANSIC
ssl_client_verify_callback(int ok, X509_STORE_CTX * ctx)
#else
ssl_client_verify_callback(ok, ctx)
int ok;
X509_STORE_CTX *ctx;
#endif
{
char subject[256]="", issuer[256]="";
int depth, error, len;
X509 *xs;
setverbosity();
xs=X509_STORE_CTX_get_current_cert(ctx);
error=X509_STORE_CTX_get_error(ctx);
depth=X509_STORE_CTX_get_error_depth(ctx);
if ( ssl_debug_flag )
printf("ssl:client_verify_callback:depth=%d ok=%d err=%d-%s\r\n",
depth,ok,error,X509_verify_cert_error_string(error));
if ( ssl_certsok_flag ) {
ok = 1;
}
/* first thing is to have a meaningful name for the current
* certificate that is being verified ... and if we cannot
* determine that then something is seriously wrong!
*/
#ifdef XN_FLAG_SEP_MULTILINE
X509_NAME_print_ex(bio_err,X509_get_subject_name(xs),4,
XN_FLAG_SEP_MULTILINE);
len = BIO_read(bio_err,subject,256);
subject[len < 256 ? len : 255] = '\0';
if (!subject[0]) {
ERR_print_errors(bio_err);
len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
uq_ok("X.509 Subject Name unavailable", ssl_err, 1, NULL, 0);
ok=0;
goto return_time;
}
X509_NAME_print_ex(bio_err,X509_get_issuer_name(xs),4,
XN_FLAG_SEP_MULTILINE);
len = BIO_read(bio_err,issuer,256);
issuer[len < 256 ? len : 255] = '\0';
if (!issuer[0]) {
ERR_print_errors(bio_err);
len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
uq_ok("X.509 Issuer Name unavailable", ssl_err, 1, NULL, 0);
ok=0;
goto return_time;
}
#else /* XN_FLAG_SEP_MULTILINE */
X509_NAME_oneline(X509_get_subject_name(xs),subject,256);
if (!subject[0]) {
int len;
ERR_print_errors(bio_err);
len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
uq_ok("X.509 Subject Name unavailable", ssl_err, 1, NULL, 0);
ok=0;
goto return_time;
}
X509_NAME_oneline(X509_get_issuer_name(xs),issuer,256);
if (!issuer[0]) {
int len;
ERR_print_errors(bio_err);
len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
uq_ok("X.509 Issuer Name unavailable", ssl_err, 1, NULL, 0);
ok=0;
goto return_time;
}
#endif /* XN_FLAG_SEP_MULTILINE */
if (verbosity && depth != ssl_verify_depth) {
printf("[%d] Certificate Subject:\r\n%s\r\n",depth,subject);
printf("[%d] Certificate Issuer:\r\n%s\r\n",depth,issuer);
ssl_verify_depth = depth;
}
ok = ssl_verify_crl(ok, ctx);
if ( !ok ) {
char prefix[1024];
/* if the server is using a self signed certificate then
* we need to decide if that is good enough for us to
* accept ...
*/
switch ( error ) {
case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: {
if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
/* make 100% sure that in secure more we drop the
* connection if the server does not have a
* real certificate!
*/
ckmakxmsg(prefix,1024,
"Error: Server has a self-signed certificate\n",
"[",ckitoa(depth),"] Certificate Subject=\n",subject,
"\n[",ckitoa(depth),"] Certificate Issuer=\n",issuer,
NULL,NULL,NULL);
uq_ok(prefix, "Rejecting Connection", 1, NULL, 0);
/* sometimes it is really handy to be able to debug things
* and still get a connection!
*/
if (ssl_debug_flag) {
printf("SSL: debug -> ignoring cert required!\r\n");
ok=1;
} else {
ok=0;
}
goto return_time;
} else if (ssl_verify_flag != SSL_VERIFY_NONE) {
ckmakxmsg(prefix,1024,
"Warning: Server has a self-signed certificate\n",
"[",ckitoa(depth),"] Certificate Subject=\n",subject,
"\n[",ckitoa(depth),"] Certificate Issuer=\n",issuer,
NULL,NULL,NULL);
ok = uq_ok(prefix,
"Continue? (Y/N) ",
3, NULL, 0);
if ( ok < 0 )
ok = 0;
goto return_time;
}
}
case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
/* make 100% sure that in secure more we drop the
* connection if the server does not have a
* real certificate!
*/
ckmakxmsg(prefix,1024,
"Error: ",
(char *)X509_verify_cert_error_string(error),
"\nCertificate Issuer=\n",issuer,
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
uq_ok(prefix, "Rejecting Connection", 1, NULL, 0);
/* sometimes it is really handy to be able to debug things
* and still get a connection!
*/
if (ssl_debug_flag) {
printf("SSL: debug -> ignoring cert required!\r\n");
ok=1;
} else {
ok=0;
}
goto return_time;
} else if (ssl_verify_flag != SSL_VERIFY_NONE) {
ckmakxmsg(prefix,1024,
"Warning: ",
(char *)X509_verify_cert_error_string(error),
"\nCertificate Issuer=\n",issuer,
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
ok = uq_ok(prefix, "Continue (Y/N)", 3, NULL, 0);
goto return_time;
}
break;
case X509_V_ERR_CERT_NOT_YET_VALID:
case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
int len;
/* make 100% sure that in secure more we drop the
* connection if the server does not have a
* real certificate!
*/
ASN1_TIME_print(bio_err,X509_get_notBefore(xs));
len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
ckmakxmsg(prefix,1024,
"Error: ",
(char *)X509_verify_cert_error_string(error),
"\nCertificate Subject=\n",subject,
"\nnotBefore=",ssl_err,
NULL,NULL,NULL,NULL,NULL,NULL);
uq_ok(prefix, "Rejecting Connection", 1, NULL, 0);
/* sometimes it is really handy to be able to debug things
* and still get a connection!
*/
if (ssl_debug_flag) {
printf("SSL: debug -> ignoring cert required!\r\n");
ok=1;
} else {
ok=0;
}
goto return_time;
} else if (ssl_verify_flag != SSL_VERIFY_NONE) {
int len;
ASN1_TIME_print(bio_err,X509_get_notBefore(xs));
len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
ckmakxmsg(prefix,1024,
"Warning: ",
(char *)X509_verify_cert_error_string(error),
"\nCertificate Subject=\n",subject,
"\n notBefore=",ssl_err,
NULL,NULL,NULL,NULL,NULL,NULL);
ok = uq_ok(prefix, "Continue (Y/N)", 3, NULL, 0);
}
break;
case X509_V_ERR_CERT_HAS_EXPIRED:
case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
int len;
/* make 100% sure that in secure more we drop the
* connection if the server does not have a
* real certificate!
*/
ASN1_TIME_print(bio_err,X509_get_notAfter(xs));
len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
ckmakxmsg(prefix,1024,
"Error: ",
(char *)X509_verify_cert_error_string(error),
"\nCertificate Subject=\n",subject,
"\n notAfter=",ssl_err,
NULL,NULL,NULL,NULL,NULL,NULL);
uq_ok(prefix, "Rejecting Connection", 1, NULL, 0);
/* sometimes it is really handy to be able to debug things
* and still get a connection!
*/
if (ssl_debug_flag) {
printf("SSL: debug -> ignoring cert required!\r\n");
ok=1;
} else {
ok=0;
}
goto return_time;
} else if (ssl_verify_flag != SSL_VERIFY_NONE) {
int len;
ASN1_TIME_print(bio_err,X509_get_notAfter(xs));
len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
ckmakxmsg(prefix,1024,
"Warning: ",
(char *)X509_verify_cert_error_string(error),
"\nCertificate Subject=\n",subject,
"\n notAfter=",ssl_err,
NULL,NULL,NULL,NULL,NULL,NULL);
ok = uq_ok(prefix, "Continue (Y/N)", 3, NULL, 0);
}
break;
case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
/*
* When an SSL server sends its certificates to the client there
* are two" conventions": one is to send the complete certificate
* chain and the other is to send the whole chain apart from the
* root.
*
* You don't usually need the root because the root is normally
* stored and trusted locally.
*
* So if you get the whole chain it will complain about the self
* signed certificate whereas if the root is missing it says it
* can't find the issuer certificate.
*/
if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
/* make 100% sure that in secure more we drop the
* connection if the server does not have a
* real certificate!
*/
ckmakxmsg(prefix,1024,
"Error: ",
(char *)X509_verify_cert_error_string(error),
"\nCertificate Issuer=\n",issuer,
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
uq_ok(prefix, "Rejecting Connection", 1, NULL, 0);
/* sometimes it is really handy to be able to debug things
* and still get a connection!
*/
if (ssl_debug_flag) {
printf("SSL: debug -> ignoring cert required!\r\n");
ok=1;
} else {
ok=0;
}
goto return_time;
} else if (ssl_verify_flag != SSL_VERIFY_NONE) {
ckmakxmsg(prefix,1024,
"Warning: ",
(char *)X509_verify_cert_error_string(error),
"\nCertificate Issuer=\n",issuer,
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
ok = uq_ok(prefix, "Continue (Y/N)", 3, NULL, 0);
#ifdef NT
if (ok) {
/* if the user decides to accept the certificate
* offer to store it for future connections in
* the user's private store
*/
ok = uq_ok(
"Do you wish to store the certificate to verify future connections?",
"Continue (Y/N)", 3, NULL, 0);
if (ok)
ck_X509_save_cert_to_user_store(xs);
}
#endif /* NT */
}
break;
case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
case X509_V_ERR_UNABLE_TO_GET_CRL:
case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
case X509_V_ERR_CERT_SIGNATURE_FAILURE:
case X509_V_ERR_CRL_SIGNATURE_FAILURE:
case X509_V_ERR_CRL_NOT_YET_VALID:
case X509_V_ERR_CRL_HAS_EXPIRED:
case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
case X509_V_ERR_OUT_OF_MEM:
case X509_V_ERR_CERT_CHAIN_TOO_LONG:
case X509_V_ERR_CERT_REVOKED:
case X509_V_ERR_APPLICATION_VERIFICATION:
default:
if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
/* make 100% sure that in secure mode we drop the
* connection if the server does not have a
* real certificate!
*/
ckmakxmsg(prefix,1024,
"Error: ",
(char *)X509_verify_cert_error_string(error),
"\nCertificate Subject=\n",subject,
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
uq_ok(prefix, "Rejecting Connection", 1, NULL, 0);
/* sometimes it is really handy to be able to debug things
* and still get a connection!
*/
if (ssl_debug_flag) {
printf("SSL: debug -> ignoring cert required!\r\n");
ok=1;
} else {
ok=0;
}
goto return_time;
} else if (ssl_verify_flag != SSL_VERIFY_NONE) {
ckmakxmsg(prefix,1024,
"Warning: ",
(char *)X509_verify_cert_error_string(error),
"\nCertificate Subject=\n",subject,
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
ok = uq_ok(prefix, "Continue (Y/N)", 3, NULL, 0);
}
break;
}
}
return_time:
if ( ssl_debug_flag )
printf("ssl:client_verify_callback => ok: %d\r\n",ok);
return ok;
}
VOID
#ifdef CK_ANSIC
ssl_client_info_callback(const SSL *s, int where, int ret)
#else
ssl_client_info_callback(s,where,ret)
const SSL *s;
int where;
int ret;
#endif /* CK_ANSIC */
{
if (inserver || !ssl_debug_flag)
return;
setverbosity();
switch ( where ) {
case SSL_CB_CONNECT_LOOP:
printf("SSL_connect:%s %s\r\n",
SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
break;
case SSL_CB_CONNECT_EXIT:
if (ret == 0) {
printf("SSL_connect:failed in %s %s\r\n",
SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
} else if (ret < 0) {
printf("SSL_connect:error in %s %s\r\n",
SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
}
break;
case SSL_CB_ACCEPT_LOOP:
printf("SSL_accept:%s %s\r\n",
SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
break;
case SSL_CB_ACCEPT_EXIT:
if (ret == 0) {
printf("SSL_accept:failed in %s %s\r\n",
SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
} else if (ret < 0) {
printf("SSL_accept:error in %s %s\r\n",
SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
}
break;
case SSL_CB_READ_ALERT:
printf("SSL_read_alert\r\n");
break;
case SSL_CB_WRITE_ALERT:
printf("SSL_write_alert\r\n");
break;
case SSL_CB_HANDSHAKE_START:
printf("SSL_handshake:%s %s\r\n",
SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
break;
case SSL_CB_HANDSHAKE_DONE:
printf("SSL_handshake:%s %s\r\n",
SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
break;
}
}
#ifdef USE_CERT_CB
/* Return 1, client cert is available */
/* Return 0, no client cert is available */
/* Return -1, callback must be called again. SSL_want_x509_lookup() == 1 */
int
#ifdef CK_ANSIC
ssl_client_cert_callback(SSL * s, X509 ** x509, EVP_PKEY ** pkey)
#else /* CK_ANSIC */
ssl_client_cert_callback(s, x509, pkey)
SSL * s;
X509 ** x509;
EVP_PKEY ** pkey;
#endif /* CK_ANSIC */
{
setverbosity();
if ( ssl_debug_flag ) {
const char * cipher_list=SSL_get_cipher(s);
printf("ssl_client_cert_callback called (%s)\r\n",
cipher_list?cipher_list:"UNKNOWN");
}
#ifdef COMMENT
if ( s == tls_con ) {
if (tls_load_certs(tls_cts,tls_con,0)) {
*x509 = SSL_get_certificate(s);
*pkey = SSL_get_privatekey(s);
return(1);
}
} else if ( s == ssl_con ) {
if (tls_load_certs(ssl_ctx,ssl_con,0)) {
*x509 = SSL_get_certificate(s);
*pkey = SSL_get_privatekey(s);
return(1);
}
}
return(0);
#else /* COMMENT */
return(0);
#endif /* COMMENT */
}
#endif /* USE_CERT_CB */
#ifndef MS_CALLBACK
#define MS_CALLBACK
#endif /* MS_CALLBACK */
static RSA MS_CALLBACK *
#ifdef CK_ANSIC
tmp_rsa_cb(SSL * s, int export, int keylength)
#else /* CK_ANSIC */
tmp_rsa_cb(s,export,keylength)
SSL *s;
int export;
int keylength;
#endif /* CK_ANSIC */
{
static RSA *rsa_tmp=NULL;
#ifndef NO_RSA
if (rsa_tmp == NULL)
{
if (ssl_debug_flag)
printf("Generating temporary (%d bit) RSA key...\r\n",keylength);
rsa_tmp=RSA_generate_key(keylength,RSA_F4,NULL,NULL);
if (ssl_debug_flag)
printf("\r\n");
}
#else /* NO_RSA */
if (ssl_debug_flag)
printf("Unable to generate temporary RSA key...\r\n");
#endif
return(rsa_tmp);
}
#ifndef NO_DH
static unsigned char dh512_p[]={
0xE9,0x4E,0x3A,0x64,0xFA,0x65,0x5F,0xA6,0x44,0xC7,0xFC,0xF1,
0x16,0x8B,0x11,0x11,0x7A,0xF0,0xB2,0x49,0x80,0x56,0xA3,0xF8,
0x0F,0x7D,0x01,0x68,0x5D,0xF6,0x8A,0xEA,0x8C,0xDD,0x01,0xDC,
0x43,0x18,0xE0,0xC4,0x89,0x80,0xE6,0x2D,0x44,0x77,0x45,0xFD,
0xBA,0xFC,0x43,0x35,0x12,0xC0,0xED,0x32,0xD3,0x16,0xEF,0x51,
0x09,0x44,0xA2,0xDB,
};
static unsigned char dh512_g[]={
0x05,
};
static unsigned char dh768_p[]={
0x8B,0x2A,0x8C,0x6C,0x0F,0x87,0xC7,0x34,0xEE,0x2E,0xFB,0x60,
0x94,0xB3,0xBF,0x95,0xBA,0x84,0x74,0x86,0xEA,0xE0,0xA4,0x33,
0xE0,0x8F,0x7C,0x79,0x5C,0x62,0xE2,0x91,0xC5,0x6D,0x68,0xB9,
0x6C,0x5E,0x4E,0x94,0x0C,0x8E,0x56,0x8E,0xEB,0x98,0x7C,0x6E,
0x0E,0xF2,0xD5,0xAA,0x22,0x27,0x3F,0x0F,0xAF,0x10,0xB5,0x0B,
0x16,0xCC,0x05,0x27,0xBB,0x58,0x6D,0x61,0x4B,0x2B,0xAB,0xDC,
0x6A,0x15,0xBC,0x36,0x75,0x4D,0xEC,0xAB,0xFA,0xB6,0xE1,0xB1,
0x13,0x70,0xD8,0x77,0xCD,0x5E,0x51,0x77,0x81,0x0D,0x77,0x43,
};
static unsigned char dh768_g[]={
0x05,
};
static unsigned char dh1024_p[]={
0xA4,0x75,0xCF,0x35,0x00,0xAF,0x3C,0x17,0xCE,0xB0,0xD0,0x52,
0x43,0xA0,0x0E,0xFA,0xA2,0xC9,0xBE,0x0B,0x76,0x7A,0xD9,0x2E,
0xF4,0x97,0xAC,0x02,0x24,0x69,0xF6,0x36,0x4F,0xAB,0xCC,0x43,
0xC1,0x74,0xFF,0xA3,0xD4,0x04,0x0F,0x11,0x2B,0x6D,0x8C,0x47,
0xC9,0xCF,0x40,0x93,0x9B,0x7D,0x1E,0x52,0x85,0xB2,0x17,0x55,
0x9C,0xF2,0x41,0x02,0x2A,0x9D,0x5F,0x24,0x22,0xC6,0x04,0xC4,
0xAB,0x92,0x6D,0xC7,0xC8,0xF3,0x41,0x58,0x6C,0x86,0xFD,0xB8,
0x0F,0x2D,0xDD,0xBF,0xA8,0x40,0x0C,0x58,0xC8,0xF2,0x3F,0x18,
0xEF,0xF1,0x93,0x3E,0xBA,0x16,0x41,0xBE,0x32,0x6C,0xC5,0x63,
0xFF,0x8A,0x02,0x3D,0xAC,0xD5,0x5A,0x49,0x64,0x34,0x14,0x2E,
0xFB,0x2E,0xE7,0x39,0x1A,0x0F,0x3C,0x33,
};
static unsigned char dh1024_g[]={
0x05,
};
static unsigned char dh1536_p[]={
0xA3,0x2B,0x75,0x0E,0x7B,0x31,0x82,0xCA,0xF2,0xFC,0xF3,0x3D,
0xCE,0x5F,0xCD,0x5B,0x95,0xF6,0x2F,0xA4,0x5D,0x08,0x26,0xD2,
0x5F,0xC0,0x3F,0xC5,0xD8,0xA2,0xFE,0x83,0x26,0xBC,0xEB,0x7D,
0xF0,0x4E,0xD2,0xA6,0xBB,0x3C,0x88,0x63,0xCE,0x98,0xDE,0x08,
0xE2,0xE1,0xAF,0xE2,0x38,0xA8,0xFA,0x68,0x76,0x8D,0xBF,0xDF,
0xBB,0x30,0x15,0xFE,0xBD,0x22,0xCC,0x03,0x4E,0x5E,0x33,0xA3,
0x6D,0xD6,0x68,0x12,0x97,0x17,0x4B,0xB5,0x84,0x5F,0x5F,0xA3,
0x5C,0x2F,0xA4,0x10,0xC1,0xAD,0xBF,0xAC,0x30,0xCA,0x47,0x64,
0x63,0xFE,0xEE,0xEE,0xA1,0x64,0x73,0x70,0xAA,0xF9,0xFE,0xC6,
0xAD,0x5E,0xF6,0xF3,0x9C,0xDF,0x34,0x53,0x34,0x72,0xA6,0xA4,
0xBB,0x81,0x5A,0x43,0x41,0xFD,0x41,0x05,0x5B,0x77,0x7B,0x84,
0x03,0xFA,0x8A,0xFA,0xF7,0x8E,0x0F,0xCB,0x51,0xA2,0xB8,0x45,
0xFF,0x59,0x42,0xEF,0xCF,0xF6,0x25,0x37,0xE2,0x6D,0xFF,0x69,
0x11,0xF5,0x77,0x59,0x79,0x1C,0x5F,0x05,0xFC,0x7A,0x65,0x81,
0x03,0x4A,0x78,0xC6,0xE9,0x48,0x73,0xF6,0x10,0xBC,0x99,0x1C,
0xEE,0x44,0x2F,0x8B,0x70,0xCA,0xA8,0xB6,0x02,0x83,0x3E,0x0B,
};
static unsigned char dh1536_g[]={
0x05,
};
static unsigned char dh2048_p[]={
0xFA,0x4E,0xE4,0x3B,0xFA,0xC1,0x87,0xDD,0xE7,0xC6,0x8B,0xE6,
0x13,0x85,0xBC,0x9B,0x2B,0x8B,0x5B,0x46,0xBB,0x8B,0x86,0x6D,
0xD7,0xB6,0xD5,0x49,0xC5,0x54,0xF2,0x3E,0xD2,0x39,0x64,0x9B,
0x0E,0x33,0x39,0x8F,0xFA,0xFA,0xD9,0x78,0xED,0x34,0x82,0x29,
0x37,0x58,0x4D,0x5D,0x40,0xCB,0x69,0xE3,0x8A,0x9F,0x17,0x0C,
0x01,0x23,0x6B,0x05,0x01,0xAF,0x33,0xDE,0xDF,0x1A,0xBB,0x7B,
0x6A,0x9F,0xD8,0xED,0x8D,0x5E,0x44,0x19,0x5B,0xE0,0xB6,0x23,
0xF9,0x7A,0x96,0x6E,0x94,0x33,0x31,0x49,0xBA,0x84,0xD5,0x12,
0xD7,0x6D,0xDC,0x35,0x54,0x64,0xA3,0xD8,0x04,0x26,0xC5,0xAF,
0x7F,0xE3,0xFE,0x6F,0xBE,0xD5,0x17,0x72,0x4B,0xA6,0xD0,0xA7,
0x5F,0x18,0xF5,0xF0,0x2D,0x11,0x9A,0xF6,0xD5,0x3B,0x6C,0x61,
0x3C,0x6F,0x8E,0x09,0x4F,0x2C,0xE1,0x26,0x06,0x51,0xB3,0x19,
0x85,0x85,0x13,0xF9,0xC2,0x6E,0x80,0x28,0x9E,0x8A,0xA0,0x01,
0x46,0xD1,0x85,0x44,0x8C,0xE6,0xEE,0x7E,0x1E,0x17,0x3D,0xBA,
0x54,0xFF,0xE8,0x0E,0xDD,0x51,0xF3,0x74,0x7F,0x0D,0x0B,0xAB,
0xCA,0x84,0x8D,0x24,0x5D,0x56,0xD4,0x47,0x02,0xFC,0x93,0x9F,
0xAE,0x9B,0x5C,0xDB,0x63,0xEB,0x65,0x01,0x38,0xC2,0x7B,0x30,
0x1E,0x17,0x1C,0x75,0xF5,0x16,0x3B,0x4F,0x5F,0x41,0x32,0xB5,
0xFF,0x9E,0x61,0xFD,0xD2,0x62,0x6E,0xFD,0x8A,0x28,0x93,0x59,
0x2D,0x70,0x14,0x4D,0xE1,0x86,0xD5,0x90,0xB4,0xDF,0x72,0x71,
0xE0,0xB4,0xD0,0xD6,0x82,0x3A,0x4A,0x04,0x58,0x32,0x0B,0xD3,
0x51,0x13,0x32,0x63,
};
static unsigned char dh2048_g[]={
0x02,
};
static DH *
get_dh512()
{
DH *dh=NULL;
if ((dh=DH_new()) == NULL)
return(NULL);
dh->p=BN_bin2bn(dh512_p,sizeof(dh512_p),NULL);
dh->g=BN_bin2bn(dh512_g,sizeof(dh512_g),NULL);
if ((dh->p == NULL) || (dh->g == NULL))
return(NULL);
return(dh);
}
static DH *
get_dh768()
{
DH *dh=NULL;
if ((dh=DH_new()) == NULL)
return(NULL);
dh->p=BN_bin2bn(dh768_p,sizeof(dh768_p),NULL);
dh->g=BN_bin2bn(dh768_g,sizeof(dh768_g),NULL);
if ((dh->p == NULL) || (dh->g == NULL))
return(NULL);
return(dh);
}
static DH *
get_dh1024()
{
DH *dh=NULL;
if ((dh=DH_new()) == NULL)
return(NULL);
dh->p=BN_bin2bn(dh1024_p,sizeof(dh1024_p),NULL);
dh->g=BN_bin2bn(dh1024_g,sizeof(dh1024_g),NULL);
if ((dh->p == NULL) || (dh->g == NULL))
return(NULL);
return(dh);
}
static DH *
get_dh1536()
{
DH *dh=NULL;
if ((dh=DH_new()) == NULL)
return(NULL);
dh->p=BN_bin2bn(dh1536_p,sizeof(dh1536_p),NULL);
dh->g=BN_bin2bn(dh1536_g,sizeof(dh1536_g),NULL);
if ((dh->p == NULL) || (dh->g == NULL))
return(NULL);
return(dh);
}
static DH *
get_dh2048()
{
DH *dh=NULL;
if ((dh=DH_new()) == NULL)
return(NULL);
dh->p=BN_bin2bn(dh2048_p,sizeof(dh2048_p),NULL);
dh->g=BN_bin2bn(dh2048_g,sizeof(dh2048_g),NULL);
if ((dh->p == NULL) || (dh->g == NULL))
return(NULL);
return(dh);
}
#endif /* NO_DH */
static DH MS_CALLBACK *
#ifdef CK_ANSIC
tmp_dh_cb(SSL * s, int export, int keylength)
#else /* CK_ANSIC */
tmp_dh_cb(s,export,keylength)
SSL *s;
int export;
int keylength;
#endif /* CK_ANSIC */
{
static DH *dh_tmp=NULL;
BIO *bio=NULL;
#ifndef NO_DH
if (dh_tmp == NULL)
{
if (ssl_dh_param_file &&
(bio=BIO_new_file(ssl_dh_param_file,"r")) != NULL)
dh_tmp=PEM_read_bio_DHparams(bio,NULL,NULL,NULL);
if (bio != NULL)
BIO_free(bio);
if ( dh_tmp == NULL ) {
if ( keylength < 768 )
dh_tmp = get_dh512();
else if ( keylength < 1024 )
dh_tmp = get_dh768();
else if ( keylength < 1536 )
dh_tmp = get_dh1024();
else if ( keylength < 2048 )
dh_tmp = get_dh1536();
else
dh_tmp = get_dh2048();
}
}
#else /* NO_DH */
if (ssl_debug_flag)
printf("DH not supported...\r\n");
#endif /* NO_DH */
return(dh_tmp);
}
static void
ssl_display_comp(SSL * ssl)
{
if ( quiet ) /* fdc - Mon Nov 28 11:44:15 2005 */
return;
if ( !ck_ssleay_is_installed() )
return;
if (ssl == NULL)
return;
if (ssl->expand == NULL || ssl->expand->meth == NULL)
printf("Compression: None\r\n");
else {
printf("Compression: %s\r\n",ssl->expand->meth->name);
}
}
int
#ifdef CK_ANSIC
ssl_display_connect_details(SSL * ssl_con, int server, int verbose)
#else /* CK_ANSIC */
ssl_display_connect_details(ssl_con,server,verbose)
SSL *ssl_con;
int server;
int verbose;
#endif /* CK_ANSIC */
{
X509 *peer;
SSL_CIPHER * cipher;
const char *cipher_list;
char buf[512]="";
if ( quiet ) /* fdc - Mon Nov 28 11:44:15 2005 */
return(0);
if ( !ck_ssleay_is_installed() )
return(0);
if ( inserver && !tn_deb )
return(0);
/* the cipher list *can* be NULL ... useless but it happens! */
cipher = SSL_get_current_cipher(ssl_con);
cipher_list = SSL_CIPHER_get_name(cipher);
SSL_CIPHER_description(cipher,buf,sizeof(buf));
if (cipher_list==NULL)
cipher_list="<NULL>";
printf("[TLS - %s",buf);
ssl_display_comp(ssl_con);
if ( server ) {
cipher_list=SSL_get_shared_ciphers(ssl_con,buf,512);
if (cipher_list==NULL)
cipher_list="<NULL>";
printf("[TLS - shared ciphers=%s]\r\n",
cipher_list);
}
if ( server || tn_deb ) {
peer=SSL_get_peer_certificate(ssl_con);
if (peer != NULL) {
X509_NAME_oneline(X509_get_subject_name(peer),buf,512);
printf("[TLS - subject=%s]\r\n",buf);
X509_NAME_oneline(X509_get_issuer_name(peer),buf,512);
printf("[TLS - issuer=%s]\r\n",buf);
/* X509_free(peer); */
} else if (!tls_is_krb5(0)) {
if ( !sstelnet && !tcp_incoming ) {
printf("[TLS - No certificate provided.]\r\n");
printf(
"[TLS - The identity of the host could not be verified.]\r\n");
}
}
}
return(0);
}
/*
* Use SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *, void * userdata)
* to set the value of the userdata. We are going to use it to store the
* prompt.
*/
int
#ifdef CK_ANSIC
ssl_passwd_callback(char *buf, int len, int rwflag, VOID * userdata)
#else /* CK_ANSIC */
ssl_passwd_callback(buf,len,rwflag,userdata)
char * buf; int len; int rwflag; VOID *userdata;
#endif /* CK_ANSIC */
{
extern char pwbuf[];
extern int pwflg, pwcrypt;
int ok;
char *prompt=NULL;
if ( pwbuf[0] && pwflg ) {
int n;
n = ckstrncpy(buf,pwbuf,len);
#ifdef OS2
if ( pwcrypt )
ck_encrypt((char *)buf);
#endif /* OS2 */
return(n);
}
if ( userdata == NULL )
prompt="Enter certificate passphrase: ";
else
prompt=(char*)userdata;
ok = uq_txt(NULL,prompt,2,NULL,buf,len,NULL,DEFAULT_UQ_TIMEOUT);
return(ok > 0 ? strlen(buf) : 0);
}
/* Attempts to load certificate data into the TLS context structures */
/* Returns 1 on success; 0 on failure */
int
tls_load_certs(SSL_CTX * ctx, SSL * con, int server)
{
int rc = 1;
if ( !ck_ssleay_is_installed() )
return(0);
debug(F110,"tls_load_certs","SSL_CTX",0);
debug(F110,"tls_load_certs","SSL",0);
debug(F110,"tls_load_certs","server",0);
if ( con ) {
if (ssl_rsa_cert_file) {
if ( ssl_debug_flag )
printf("Loading RSA certificate into SSL\r\n");
rc = SSL_use_certificate_file(con, ssl_rsa_cert_file,
X509_FILETYPE_PEM);
if (!rc)
{
if ( !quiet || ssl_debug_flag )
printf("Error loading certificate from %s\r\n",
ssl_rsa_cert_file);
} else {
if (!ssl_rsa_key_file || !ssl_rsa_key_file[0])
makestr(&ssl_rsa_key_file,ssl_rsa_cert_file);
rc = SSL_use_PrivateKey_file(con, ssl_rsa_key_file,
X509_FILETYPE_PEM);
if (!rc)
rc = SSL_use_PrivateKey_file(con, ssl_rsa_cert_file,
X509_FILETYPE_PEM);
if (!rc)
{
if ( !quiet || ssl_debug_flag )
printf("Error loading key from %s\r\n",
ssl_rsa_key_file);
} else {
rc = SSL_check_private_key(con);
if (!rc)
{
if ( ssl_debug_flag )
printf(
"Private key does not match the certificate public key\r\n");
}
}
}
}
if (ssl_dsa_cert_file) {
if ( ssl_debug_flag )
printf("Loading DSA certificate into SSL\r\n");
rc = SSL_use_certificate_file(con, ssl_dsa_cert_file,
X509_FILETYPE_PEM);
if (!rc)
{
if ( ssl_debug_flag ) {
printf("Error loading certificate from %s\r\n",
ssl_dsa_cert_file);
}
} else {
if (!ssl_dh_key_file || !ssl_dh_key_file[0])
makestr(&ssl_dh_key_file,ssl_dsa_cert_file);
rc = SSL_use_PrivateKey_file(con, ssl_dh_key_file,
X509_FILETYPE_PEM);
if (!rc)
rc = SSL_use_PrivateKey_file(con, ssl_dsa_cert_file,
X509_FILETYPE_PEM);
if (!rc)
{
if ( !quiet || ssl_debug_flag ) {
printf("Error loading key from %s\r\n",
ssl_dh_key_file);
}
} else {
rc = SSL_check_private_key(con);
if (!rc)
{
if ( ssl_debug_flag )
printf(
"Private key does not match the certificate public key\n");
}
}
}
}
} else {
if (ssl_rsa_cert_file) {
if ( ssl_debug_flag )
printf("Loading RSA certificate into SSL\r\n");
rc = SSL_CTX_use_certificate_file(ctx, ssl_rsa_cert_file,
X509_FILETYPE_PEM);
if (!rc)
{
if ( !quiet || ssl_debug_flag )
printf("Error loading certificate from %s\r\n",
ssl_rsa_cert_file);
} else {
if (!ssl_rsa_key_file || !ssl_rsa_key_file[0])
makestr(&ssl_rsa_key_file,ssl_rsa_cert_file);
rc = SSL_CTX_use_PrivateKey_file(ctx, ssl_rsa_key_file,
X509_FILETYPE_PEM);
if (!rc)
rc = SSL_CTX_use_PrivateKey_file(ctx, ssl_rsa_cert_file,
X509_FILETYPE_PEM);
if (!rc) {
if ( ssl_debug_flag )
printf("Error loading key from %s\r\n",ssl_rsa_key_file);
} else {
rc = SSL_CTX_check_private_key(ctx);
if (!rc) {
if ( ssl_debug_flag )
printf(
"Private key does not match the certificate public key\r\n");
}
}
}
}
if (ssl_dsa_cert_file) {
if ( ssl_debug_flag )
printf("Loading DSA certificate into SSL\r\n");
rc = SSL_CTX_use_certificate_file(ctx, ssl_dsa_cert_file,
X509_FILETYPE_PEM);
if (!rc) {
if ( ssl_debug_flag ) {
printf("Error loading certificate from %s\r\n",
ssl_dsa_cert_file);
}
} else {
if (!ssl_dh_key_file || !ssl_dh_key_file[0])
makestr(&ssl_dh_key_file,ssl_dsa_cert_file);
rc = SSL_CTX_use_PrivateKey_file(ctx, ssl_dh_key_file,
X509_FILETYPE_PEM);
if (!rc)
rc = SSL_CTX_use_PrivateKey_file(ctx, ssl_dsa_cert_file,
X509_FILETYPE_PEM);
if (!rc) {
if ( ssl_debug_flag )
printf("Error loading key from %s\r\n",ssl_dh_key_file);
} else {
rc = SSL_CTX_check_private_key(ctx);
if (!rc) {
if ( ssl_debug_flag )
printf(
"Private key does not match the certificate public key\n");
}
}
}
}
}
if (ssl_rsa_cert_chain_file && server) {
int skip1st = 0;
if (ssl_debug_flag)
printf("Loading RSA Certificate Chain into SSL\r\n");
if (!ckstrcmp(ssl_rsa_cert_chain_file,ssl_rsa_cert_file,-1,
#ifdef OS2
0
#else
1
#endif /* OS2 */
))
skip1st = 1;
rc = SSL_CTX_use_certificate_chain_file(ctx,ssl_rsa_cert_chain_file);
if (!rc && ssl_debug_flag)
printf("Error loading RSA Certificate Chain into SSL\r\n");
}
if (ssl_dsa_cert_chain_file && server) {
int skip1st = 0;
if (ssl_debug_flag)
printf("Loading DSA Certificate Chain into SSL\r\n");
if (!ckstrcmp(ssl_dsa_cert_chain_file,ssl_dsa_cert_file,-1,
#ifdef OS2
0
#else
1
#endif /* OS2 */
))
skip1st = 1;
rc = SSL_CTX_use_certificate_chain_file(ctx,ssl_dsa_cert_chain_file);
if (!rc && ssl_debug_flag)
printf("Error loading DSA Certificate Chain into SSL\r\n");
}
return(rc);
}
VOID
#ifdef CK_ANSIC
ssl_once_init(void)
#else
ssl_once_init()
#endif /* CK_ANSIC */
{
COMP_METHOD * cm;
char * s;
if ( !ck_ssleay_is_installed() )
return;
/*
OpenSSL does not provide for ABI compatibility between releases prior
to version 1.0.0. If the version does not match, it is not safe to
assume that any function you call takes the same parameters or does
the same thing with them. Removing this test prior to the OpenSSL 1.0.0
release will result in an increase in unexplained or incorrect behaviors.
The test should be revised once OpenSSL 1.0.0 is released and we see what
its claims are as to ABI compatibility.
*/
debug(F111,"Kermit built for OpenSSL",OPENSSL_VERSION_TEXT,SSLEAY_VERSION_NUMBER);
#ifndef OS2ONLY
debug(F111,"OpenSSL Library",SSLeay_version(SSLEAY_VERSION),
SSLeay());
debug(F110,"OpenSSL Library",SSLeay_version(SSLEAY_BUILT_ON),0);
debug(F110,"OpenSSL Library",SSLeay_version(SSLEAY_CFLAGS),0);
debug(F110,"OpenSSL Library",SSLeay_version(SSLEAY_PLATFORM),0);
/* The following test is suggested by Richard Levitte */
if (((OPENSSL_VERSION_NUMBER ^ SSLeay()) & 0xffffff0f)
#ifdef OS2
|| ckstrcmp(OPENSSL_VERSION_TEXT,(char *)SSLeay_version(SSLEAY_VERSION),-1,1)
#endif /* OS2 */
) {
ssl_installed = 0;
debug(F111,"OpenSSL Version does not match. Built with",
SSLeay_version(SSLEAY_VERSION),SSLEAY_VERSION_NUMBER);
printf("?OpenSSL libraries do not match required version:\r\n");
printf(" . C-Kermit built with %s\r\n",OPENSSL_VERSION_TEXT);
printf(" . Version found %s\r\n",SSLeay_version(SSLEAY_VERSION));
printf(" OpenSSL versions prior to 1.0.0 must be the same.\r\n");
s = "R";
#ifdef SOLARIS
printf(" Set CD_LIBRARY_PATH for %s.\r\n",OPENSSL_VERSION_TEXT);
s = " Or r";
#endif /* SOLARIS */
#ifdef HPUX
printf(" Set SHLIB_PATH for %s.\r\n",OPENSSL_VERSION_TEXT);
s = " Or r";
#endif /* HPUX */
#ifdef AIX
printf(" Set LIBPATH for %s.\r\n",OPENSSL_VERSION_TEXT);
s = " Or r";
#endif /* AIX */
#ifdef LINUX
printf(" Set LD_LIBRARY_PATH for %s.\r\n",OPENSSL_VERSION_TEXT);
s = " Or r";
#endif /* LINUX */
printf(" %sebuild C-Kermit from source on this computer to make \
versions agree.\r\n",s);
#ifdef KTARGET
{
char * s;
s = KTARGET;
if (!s) s = "";
if (!*s) s = "(unknown)";
printf(" C-Kermit makefile target: %s\r\n",s);
}
#endif /* KTARGET */
printf(" Or if that is what you did then try to find out why\r\n");
printf(" the program loader (image activator) is choosing a\r\n");
printf(" different OpenSSL library than the one specified in \
the build.\r\n\r\n");
printf(" All SSL/TLS features disabled.\r\n\r\n");
bleep(BP_FAIL);
#ifdef SSLDLL
ck_ssl_unloaddll();
ck_crypto_unloaddll();
#endif /* SSLDLL */
return;
}
#endif /* OS2ONLY */
/* init things so we will get meaningful error messages
* rather than numbers
*/
SSL_load_error_strings();
#ifdef SSHBUILTIN
OPENSSL_add_all_algorithms_noconf();
#else
/* SSL_library_init() only loads those ciphers needs for SSL */
/* These happen to be a similar set to those required for SSH */
/* but they are not a complete set of ciphers provided by the */
/* crypto library. */
SSL_library_init();
#endif /* SSHBUILTIN */
#ifdef ZLIB
cm = COMP_zlib();
if (cm != NULL && cm->type != NID_undef) {
SSL_COMP_add_compression_method(0xe0, cm); /* EAY's ZLIB ID */
}
#endif /* ZLIB */
cm = COMP_rle();
if (cm != NULL && cm->type != NID_undef)
SSL_COMP_add_compression_method(0xe1, cm); /* EAY's RLE ID */
/* Ensure the Random number generator has enough entropy */
if ( !RAND_status() ) {
char buffer[256]="";
char randombytes[256];
int rc1 = -1, rc2 = 1; /* assume failure and success */
debug(F110,"ssl_once_init","!RAND_status()",0);
if ( ssl_rnd_file == NULL ) {
debug(F110,"ssl_rnd_file","ssl_rnd_file is NULL",0);
RAND_file_name(buffer,256);
if ( buffer[0] )
makestr(&ssl_rnd_file, buffer);
else
makestr(&ssl_rnd_file,".rnd");
}
debug(F110,"ssl_rnd_file",ssl_rnd_file,0);
rc1 = RAND_egd(ssl_rnd_file);
debug(F111,"ssl_once_init","RAND_egd()",rc1);
if ( rc1 <= 0 ) {
rc2 = RAND_load_file(ssl_rnd_file, -1);
debug(F111,"ssl_once_init","RAND_load_file()",rc1);
}
if ( rc1 <= 0 && !rc2 )
{
time_t t = time(NULL);
int tlen = sizeof(time_t);
int pid = getpid();
int plen = sizeof(int);
int n;
#ifndef RAND_MAX
#define RAND_MAX 0x7FFF
#endif
debug(F110,"ssl_once_init","calling RAND_seed()",0);
RAND_seed((unsigned char *)&t, tlen);
RAND_seed((unsigned char *)&pid, plen);
srand((unsigned int)t);
sprintf(buffer, "%.0f", (((double)(rand()%RAND_MAX)/RAND_MAX)*
(sizeof(randombytes)-128-1)));
n = (atoi(buffer)+1)%(sizeof(randombytes)-128-1);
RAND_seed(randombytes, 128);
}
if ( !RAND_status() ) {
debug(F110,"ssl_once_init","Unable to initialize PRNG",0);
printf(" Unable to load 'random state'\n");
printf(" SSL and TLS are unavailble.\n");
printf(" Use SET AUTH SSL RANDOM-FILE <file> command to provide random data.\n");
printf(" Specified file will be overwritten with new random data after use.\n");
return;
}
if ( ssl_rnd_file ) {
int rc = RAND_write_file(ssl_rnd_file);
debug(F111,"ssl_once_init","RAND_write_file()",rc);
}
}
#ifdef NT
// Initialize additional OID types for use when saving certs to a file
OBJ_create("2.99999.3","SET.ex3","SET x509v3 extension 3");
#endif /* NT */
/* make sure we have somewhere we can log errors to */
bio_err=BIO_new(BIO_s_mem());
debug(F100,"ssl_once_init() complete","",0);
}
int
#ifdef CK_ANSIC
ssl_tn_init(int mode)
#else
ssl_tn_init(mode) int mode;
#endif /* CK_ANSIC */
{
#ifdef KRB5
extern char * k5_keytab;
extern char * krb5_d_srv;
#endif /* KRB5 */
static int last_ssl_mode = -1;
SSL * ssl_conx=NULL, * tls_conx=NULL;
ssl_initialized = 0;
if ( !ck_ssleay_is_installed() )
return(0);
debug(F111,"ssl_tn_init","mode",mode);
if (ssl_debug_flag)
printf("SSL_DEBUG_FLAG on\r\n");
if (last_ssl_mode != mode) {
if (ssl_ctx) {
SSL_CTX_free(ssl_ctx);
ssl_ctx = NULL;
}
if (tls_ctx) {
SSL_CTX_free(tls_ctx);
tls_ctx = NULL;
}
}
if ( (last_ssl_mode != mode) || !ssl_ctx || !tls_ctx ) {
if ( mode == SSL_CLIENT ) {
ssl_ctx=(SSL_CTX *)SSL_CTX_new(SSLv23_client_method());
/* This can fail because we do not have RSA available */
if ( !ssl_ctx ) {
debug(F110,"ssl_tn_init","SSLv23_client_method failed",0);
ssl_ctx=(SSL_CTX *)SSL_CTX_new(SSLv3_client_method());
}
if ( !ssl_ctx ) {
debug(F110,"ssl_tn_init","SSLv3_client_method failed",0);
last_ssl_mode = -1;
return(0);
}
#ifndef COMMENT
tls_ctx=(SSL_CTX *)SSL_CTX_new(TLSv1_client_method());
#else /* COMMENT */
tls_ctx=(SSL_CTX *)SSL_CTX_new(SSLv23_client_method());
/* This can fail because we do not have RSA available */
if ( !tls_ctx ) {
debug(F110,"ssl_tn_init","SSLv23_client_method failed",0);
tls_ctx=(SSL_CTX *)SSL_CTX_new(SSLv3_client_method());
}
#endif /* COMMENT */
if ( !tls_ctx ) {
debug(F110,"ssl_tn_init","TLSv1_client_method failed",0);
last_ssl_mode = -1;
return(0);
}
#ifdef USE_CERT_CB
SSL_CTX_set_client_cert_cb(ssl_ctx,ssl_client_cert_callback);
SSL_CTX_set_client_cert_cb(tls_ctx,ssl_client_cert_callback);
#endif /* USE_CERT_CB */
} else if (mode == SSL_SERVER) {
/* We are a server */
ssl_ctx=(SSL_CTX *)SSL_CTX_new(SSLv23_server_method());
/* This can fail because we do not have RSA available */
if ( !ssl_ctx ) {
debug(F110,"ssl_tn_init","SSLv23_server_method failed",0);
ssl_ctx=(SSL_CTX *)SSL_CTX_new(SSLv3_server_method());
}
if ( !ssl_ctx ) {
debug(F110,"ssl_tn_init","SSLv3_server_method failed",0);
last_ssl_mode = -1;
return(0);
}
#ifdef COMMENT
tls_ctx=(SSL_CTX *)SSL_CTX_new(TLSv1_server_method());
#else /* COMMENT */
tls_ctx=(SSL_CTX *)SSL_CTX_new(SSLv23_server_method());
/* This can fail because we do not have RSA available */
if ( !tls_ctx ) {
debug(F110,"ssl_tn_init","SSLv23_server_method failed",0);
tls_ctx=(SSL_CTX *)SSL_CTX_new(TLSv1_server_method());
}
#endif /* COMMENT */
if ( !tls_ctx ) {
debug(F110,"ssl_tn_init","TLSv1_server_method failed",0);
last_ssl_mode = -1;
return(0);
}
} else /* Unknown mode */
return(0);
if ( !inserver ) {
SSL_CTX_set_default_passwd_cb(ssl_ctx,
(pem_password_cb *)ssl_passwd_callback);
SSL_CTX_set_default_passwd_cb(tls_ctx,
(pem_password_cb *)ssl_passwd_callback);
}
/* for SSL switch on all the interoperability and bug
* workarounds so that we will communicate with people
* that cannot read poorly written specs :-)
* for TLS be sure to prevent use of SSLv2
*/
SSL_CTX_set_options(ssl_ctx,SSL_OP_ALL|SSL_OP_NO_SSLv2);
SSL_CTX_set_options(tls_ctx,
SSL_OP_NO_SSLv2|SSL_OP_SINGLE_DH_USE|SSL_OP_EPHEMERAL_RSA);
SSL_CTX_set_info_callback(ssl_ctx,ssl_client_info_callback);
SSL_CTX_set_info_callback(tls_ctx,ssl_client_info_callback);
#ifndef COMMENT
/* Set the proper caching mode */
if ( mode == SSL_SERVER ) {
SSL_CTX_set_session_cache_mode(ssl_ctx,SSL_SESS_CACHE_SERVER);
SSL_CTX_set_session_cache_mode(tls_ctx,SSL_SESS_CACHE_SERVER);
} else {
SSL_CTX_set_session_cache_mode(ssl_ctx,SSL_SESS_CACHE_CLIENT);
SSL_CTX_set_session_cache_mode(tls_ctx,SSL_SESS_CACHE_CLIENT);
}
SSL_CTX_set_session_id_context(ssl_ctx,(CHAR *)"1",1);
SSL_CTX_set_session_id_context(tls_ctx,(CHAR *)"2",1);
#else /* COMMENT */
SSL_CTX_set_session_cache_mode(ssl_ctx,SSL_SESS_CACHE_OFF);
SSL_CTX_set_session_cache_mode(tls_ctx,SSL_SESS_CACHE_OFF);
#endif /* COMMENT */
}
/* The server uses defaults for the certificate files. */
/* The client does not. */
if (mode == SSL_SERVER) {
char cert_filepath[1024];
const char * defdir = NULL;
DH * dh = NULL;
defdir = getenv("SSL_CERT_DIR");
if ( !defdir ) {
#ifdef OS2
defdir = exedir;
#else /* OS2 */
defdir = X509_get_default_cert_dir();
#endif /* OS2 */
debug(F110,"ssl_tn_init - setting default directory to",defdir,0);
}
if ( !defdir )
defdir = "";
if (!ssl_rsa_cert_file) {
/* we need to know the fullpath to the location of the
* certificate that we will be running with as we cannot
* be sure of the cwd when we are launched
*/
sprintf(cert_filepath,"%s/%s",defdir,"telnetd-rsa.pem");
if (zchki(cert_filepath) > 0)
makestr(&ssl_rsa_cert_file,cert_filepath);
}
if (ssl_rsa_cert_file && !ssl_rsa_key_file) {
/* we need to know the fullpath to the location of the
* certificate that we will be running with as we cannot
* be sure of the cwd when we are launched
*/
sprintf(cert_filepath,"%s/%s",defdir,"telnetd-rsa-key.pem");
if (zchki(cert_filepath) > 0)
makestr(&ssl_rsa_key_file,cert_filepath);
}
if (!ssl_dsa_cert_file) {
/* we need to know the fullpath to the location of the
* certificate that we will be running with as we cannot
* be sure of the cwd when we are launched
*/
sprintf(cert_filepath,"%s/%s",defdir,"telnetd-dsa.pem");
if (zchki(cert_filepath) > 0)
makestr(&ssl_dsa_cert_file,cert_filepath);
}
if (ssl_dsa_cert_file && !ssl_dh_key_file) {
/* we need to know the fullpath to the location of the
* certificate that we will be running with as we cannot
* be sure of the cwd when we are launched
*/
sprintf(cert_filepath,"%s/%s",defdir,"telnetd-dsa-key.pem");
if (zchki(cert_filepath) > 0)
makestr(&ssl_dh_key_file,cert_filepath);
}
if (!ssl_crl_dir) {
/* we need to know the fullpath to the location of the
* certificate that we will be running with as we cannot
* be sure of the cwd when we are launched
*/
sprintf(cert_filepath,"%s/crl",defdir);
if (zchki(cert_filepath) > 0)
makestr(&ssl_crl_dir,cert_filepath);
}
if (ssl_only_flag && !tls_load_certs(ssl_ctx,ssl_con,1)) {
debug(F110,"ssl_tn_init","Unable to load SSL certs",0);
last_ssl_mode = -1;
return(0);
}
if (tls_only_flag && !tls_load_certs(tls_ctx,tls_con,1)) {
debug(F110,"ssl_tn_init","Unable to load TLS certs",0);
last_ssl_mode = -1;
return(0);
}
if ( (last_ssl_mode != mode) || !ssl_ctx || !tls_ctx ) {
/* we may require a temp 512 bit RSA key because of the
* wonderful way export things work ... if so we generate
* one now!
*/
SSL_CTX_set_tmp_rsa_callback(ssl_ctx, tmp_rsa_cb);
SSL_CTX_set_tmp_dh_callback( ssl_ctx, tmp_dh_cb);
SSL_CTX_set_tmp_rsa_callback(tls_ctx, tmp_rsa_cb);
SSL_CTX_set_tmp_dh_callback( tls_ctx, tmp_dh_cb);
dh = tmp_dh_cb(NULL,0,512);
SSL_CTX_set_tmp_dh(ssl_ctx,dh);
SSL_CTX_set_tmp_dh(tls_ctx,dh);
/* The following code is only called if we are using a
* certificate with an RSA public key and where the
* certificate has a key length less than 512 bits or is
* marked for signing only. This is so we can support
* the greatest legal privacy level with exportable clients.
*/
if (SSL_CTX_need_tmp_RSA(ssl_ctx) ||
SSL_CTX_need_tmp_RSA(tls_ctx))
{
RSA *rsa;
if ( ssl_debug_flag )
printf("Generating temp (512 bit) RSA key ...\r\n");
rsa=RSA_generate_key(512,RSA_F4,NULL,NULL);
if ( ssl_debug_flag )
printf("Generation of temp (512 bit) RSA key done\r\n");
if (SSL_CTX_need_tmp_RSA(ssl_ctx)) {
if (!SSL_CTX_set_tmp_rsa(ssl_ctx,rsa)) {
if ( ssl_debug_flag )
printf(
"Failed to assign generated temp RSA key to SSL!\r\n");
}
}
if (SSL_CTX_need_tmp_RSA(tls_ctx)) {
if (!SSL_CTX_set_tmp_rsa(tls_ctx,rsa)) {
if ( ssl_debug_flag )
printf(
"Failed to assign generated temp RSA key to TLS!\r\n");
}
}
RSA_free(rsa);
if ( ssl_debug_flag )
printf("Assigned temp (512 bit) RSA key\r\n");
}
}
}
/* make sure we will find certificates in the standard
* location ... otherwise we don't look anywhere for
* these things which is going to make client certificate
* exchange rather useless :-)
* In OS2, default values for ssl_verify_file and ssl_verify_path.
*/
#ifdef OS2
#ifdef NT
{
/* The defaults in the SSL crypto library are not appropriate for OS/2 */
char path[CKMAXPATH];
ckmakmsg(path,CKMAXPATH,exedir,"certs",NULL,NULL);
if (isdir(path) &&
SSL_CTX_load_verify_locations(tls_ctx,NULL,path) == 1) {
debug(F110,"ssl_tn_init certificate verify dir",path,0);
if (ssl_debug_flag)
printf(" Certificate Verification Directory: %s\r\n",path);
SSL_CTX_load_verify_locations(ssl_ctx,NULL,path);
}
ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/certs",NULL,NULL);
if (isdir(path) &&
SSL_CTX_load_verify_locations(tls_ctx,NULL,path) == 1) {
debug(F110,"ssl_tn_init certificate verify dir",path,0);
if (ssl_debug_flag)
printf(" Certificate Verification Directory: %s\r\n",path);
SSL_CTX_load_verify_locations(ssl_ctx,NULL,path);
}
ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/certs",NULL,NULL);
if (isdir(path) &&
SSL_CTX_load_verify_locations(tls_ctx,NULL,path) == 1) {
debug(F110,"ssl_tn_init certificate verify dir",path,0);
if (ssl_debug_flag)
printf(" Certificate Verification Directory: %s\r\n",path);
SSL_CTX_load_verify_locations(ssl_ctx,NULL,path);
}
ckmakmsg(path,CKMAXPATH,exedir,"ca_certs.pem",NULL,NULL);
if (zchki(path) > 0 &&
SSL_CTX_load_verify_locations(tls_ctx,path,NULL) == 1) {
debug(F110,"ssl_tn_init certificate verify file",path,0);
if (ssl_debug_flag)
printf(" Certificate Verification File: %s\r\n",path);
SSL_CTX_load_verify_locations(ssl_ctx,path,NULL);
}
ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/ca_certs.pem",NULL,NULL);
if (zchki(path) > 0 &&
SSL_CTX_load_verify_locations(tls_ctx,path,NULL) == 1) {
debug(F110,"ssl_tn_init certificate verify file",path,0);
if (ssl_debug_flag)
printf(" Certificate Verification File: %s\r\n",path);
SSL_CTX_load_verify_locations(ssl_ctx,path,NULL);
}
ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/ca_certs.pem",NULL,NULL);
if (zchki(path) > 0 &&
SSL_CTX_load_verify_locations(tls_ctx,path,NULL) == 1) {
debug(F110,"ssl_tn_init certificate verify file",path,0);
if (ssl_debug_flag)
printf(" Certificate Verification File: %s\r\n",path);
SSL_CTX_load_verify_locations(ssl_ctx,path,NULL);
}
}
#else /* NT */
{
/* The defaults in the SSL crypto library are not appropriate for OS/2 */
char path[CKMAXPATH];
ckmakmsg(path,CKMAXPATH,exedir,"certs",NULL,NULL);
if (isdir(path) &&
SSL_CTX_load_verify_locations(tls_ctx,NULL,path) == 1) {
debug(F110,"ssl_tn_init certificate verify dir",path,0);
if (ssl_debug_flag)
printf(" Certificate Verification Directory: %s\r\n",path);
SSL_CTX_load_verify_locations(ssl_ctx,NULL,path);
}
ckmakmsg(path,CKMAXPATH,exedir,"ca_certs.pem",NULL,NULL);
if (zchki(path) > 0 &&
SSL_CTX_load_verify_locations(tls_ctx,path,NULL) == 1) {
debug(F110,"ssl_tn_init certificate verify file",path,0);
if (ssl_debug_flag)
printf(" Certificate Verification File: %s\r\n",path);
SSL_CTX_load_verify_locations(ssl_ctx,path,NULL);
}
}
#endif /* NT */
#else /* OS2 */
SSL_CTX_set_default_verify_paths(ssl_ctx);
SSL_CTX_set_default_verify_paths(tls_ctx);
#endif /* OS2 */
if (ssl_verify_file) {
if (zchki(ssl_verify_file) > 0 &&
SSL_CTX_load_verify_locations(tls_ctx,ssl_verify_file,NULL) == 1) {
debug(F110,"ssl_tn_init certificate verify file",ssl_verify_file,0);
if (ssl_debug_flag)
printf(" Certificate Verification File: %s\r\n",ssl_verify_file);
SSL_CTX_load_verify_locations(ssl_ctx,ssl_verify_file,NULL);
}
}
if (ssl_verify_dir && isdir(ssl_verify_dir)) {
if (SSL_CTX_load_verify_locations(tls_ctx,NULL,ssl_verify_dir) == 1) {
debug(F110,"ssl_tn_init certificate verify dir",ssl_verify_dir,0);
if (ssl_debug_flag)
printf(" Certificate Verification Directory: %s\r\n",ssl_verify_dir);
SSL_CTX_load_verify_locations(ssl_ctx,NULL,ssl_verify_dir);
}
}
if (mode == SSL_SERVER) {
SSL_CTX_set_verify(ssl_ctx,
ssl_verify_flag?ssl_verify_flag|SSL_VERIFY_CLIENT_ONCE:0,
ssl_server_verify_callback);
SSL_CTX_set_verify(tls_ctx,
ssl_verify_flag?ssl_verify_flag|SSL_VERIFY_CLIENT_ONCE:0,
ssl_server_verify_callback);
} else {
SSL_CTX_set_verify(ssl_ctx,ssl_verify_flag,
ssl_client_verify_callback);
SSL_CTX_set_verify(tls_ctx,ssl_verify_flag,
ssl_client_verify_callback);
}
/* Free the existing CRL Store */
if (crl_store) {
X509_STORE_free(crl_store);
crl_store = NULL;
}
/* set up the new CRL Store */
crl_store = X509_STORE_new();
if (crl_store) {
#ifdef OS2
char path[CKMAXPATH];
ckmakmsg(path,CKMAXPATH,exedir,"crls",NULL,NULL);
if (isdir(path) &&
X509_STORE_load_locations(crl_store,NULL,path) == 1) {
debug(F110,"ssl_tn_init crl dir",path,0);
if (ssl_debug_flag)
printf(" CRL Directory: %s\r\n",path);
}
#ifdef NT
ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/crls",NULL,NULL);
if (isdir(path) &&
X509_STORE_load_locations(crl_store,NULL,path) == 1) {
debug(F110,"ssl_tn_init crl dir",path,0);
if (ssl_debug_flag)
printf(" CRL Directory: %s\r\n",path);
}
ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/crls",NULL,NULL);
if (isdir(path) &&
X509_STORE_load_locations(crl_store,NULL,path) == 1) {
debug(F110,"ssl_tn_init crl dir",path,0);
if (ssl_debug_flag)
printf(" CRL Directory: %s\r\n",path);
}
#endif /* NT */
ckmakmsg(path,CKMAXPATH,exedir,"ca_crls.pem",NULL,NULL);
if (zchki(path) > 0 &&
X509_STORE_load_locations(crl_store,path,NULL) == 1) {
debug(F110,"ssl_tn_init crl file",path,0);
if (ssl_debug_flag)
printf(" CRL File: %s\r\n",path);
}
#ifdef NT
ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/ca_crls.pem",NULL,NULL);
if (zchki(path) > 0 &&
X509_STORE_load_locations(crl_store,path,NULL) == 1) {
debug(F110,"ssl_tn_init crl file",path,0);
if (ssl_debug_flag)
printf(" CRL File: %s\r\n",path);
}
ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/ca_crls.pem",NULL,NULL);
if (zchki(path) > 0 &&
X509_STORE_load_locations(crl_store,path,NULL) == 1) {
debug(F110,"ssl_tn_init crl file",path,0);
if (ssl_debug_flag)
printf(" CRL File: %s\r\n",path);
}
#endif /* NT */
#endif /* OS2 */
if (ssl_crl_file || ssl_crl_dir) {
if (ssl_crl_file && zchki(ssl_crl_file) > 0 &&
X509_STORE_load_locations(crl_store,ssl_crl_file,NULL) == 1) {
debug(F110,"ssl_tn_init crl file",ssl_crl_file,0);
if (ssl_debug_flag)
printf(" CRL File: %s\r\n",ssl_crl_file);
}
if (ssl_crl_dir && isdir(ssl_crl_dir) &&
X509_STORE_load_locations(crl_store,NULL,ssl_crl_dir) == 1) {
debug(F110,"ssl_tn_init crl dir",ssl_crl_dir,0);
if (ssl_debug_flag)
printf(" CRL Directory: %s\r\n",ssl_crl_dir);
}
}
#ifndef OS2
else {
X509_STORE_set_default_paths(crl_store);
}
#endif /* OS2 */
}
#ifndef COMMENT
ssl_conx = ssl_con;
ssl_con=(SSL *)SSL_new(ssl_ctx);
if ( !ssl_con ) {
debug(F110,"ssl_tn_init","SSL_new(ssl_con) failed",0);
last_ssl_mode = -1;
ssl_con = ssl_conx;
return(0);
}
if (ssl_conx) {
if ( mode == SSL_CLIENT ) {
SSL_set_session(ssl_con, SSL_get_session(ssl_conx));
}
#ifdef SSL_KRB5
if (ssl_conx->kssl_ctx) {
kssl_ctx_free(ssl_conx->kssl_ctx);
ssl_conx->kssl_ctx = NULL;
}
#endif /* SSL_KRB5 */
SSL_free(ssl_conx);
ssl_conx = NULL;
}
tls_conx = tls_con;
tls_con=(SSL *)SSL_new(tls_ctx);
if ( !tls_con ) {
debug(F110,"ssl_tn_init","SSL_new(tls_con) failed",0);
last_ssl_mode = -1;
tls_con = tls_conx;
return(0);
}
if (tls_conx) {
if ( mode == SSL_CLIENT )
SSL_set_session(tls_con, SSL_get_session(tls_conx));
#ifdef SSL_KRB5
if (tls_conx->kssl_ctx) {
kssl_ctx_free(tls_conx->kssl_ctx);
tls_conx->kssl_ctx = NULL;
}
#endif /* SSL_KRB5 */
SSL_free(tls_conx);
tls_conx = NULL;
}
#else /* COMMENT */
/* I don't know why this does not work to reuse the connection. */
if ( ssl_con ) {
SSL_clear(ssl_con);
SSL_set_session(ssl_con,NULL);
SSL_set_accept_state(ssl_con) ;
} else {
ssl_con=(SSL *)SSL_new(ssl_ctx);
if (!ssl_con) {
debug(F110,"ssl_tn_init","SSL_new(ssl_ctx) failed",0);
last_ssl_mode = -1;
ssl_con = ssl_conx;
return(0);
}
}
if ( tls_con ) {
SSL_clear(tls_con);
SSL_set_session(tls_con,NULL);
SSL_set_accept_state(tls_con) ;
} else {
tls_con=(SSL *)SSL_new(tls_ctx);
if ( !tls_con ) {
debug(F110,"ssl_tn_init","SSL_new(tls_ctx) failed",0);
last_ssl_mode = -1;
tls_con = tls_conx;
return(0);
}
}
#endif /* COMMENT */
#ifdef SSL_KRB5
#ifndef KRB5_SERVICE_NAME
#define KRB5_SERVICE_NAME "host"
#endif
if (ssl_con->kssl_ctx == NULL)
ssl_con->kssl_ctx = kssl_ctx_new();
if (tls_con->kssl_ctx == NULL)
tls_con->kssl_ctx = kssl_ctx_new();
if (mode == SSL_SERVER) {
if (ssl_con->kssl_ctx != NULL)
kssl_ctx_setstring(ssl_con->kssl_ctx, KSSL_KEYTAB, k5_keytab);
if (tls_con->kssl_ctx != NULL)
kssl_ctx_setstring(tls_con->kssl_ctx, KSSL_KEYTAB, k5_keytab);
} else {
if (ssl_con->kssl_ctx != NULL)
kssl_ctx_setstring(ssl_con->kssl_ctx, KSSL_SERVER, szHostName);
if (tls_con->kssl_ctx != NULL)
kssl_ctx_setstring(tls_con->kssl_ctx, KSSL_SERVER, szHostName);
}
kssl_ctx_setstring(ssl_con->kssl_ctx, KSSL_SERVICE,
krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME);
kssl_ctx_setstring(tls_con->kssl_ctx, KSSL_SERVICE,
krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME);
#endif /* SSL_KRB5 */
if (ssl_cipher_list) {
SSL_set_cipher_list(ssl_con,ssl_cipher_list);
SSL_set_cipher_list(tls_con,ssl_cipher_list);
} else {
char * p;
if (p = getenv("SSL_CIPHER")) {
SSL_set_cipher_list(ssl_con,p);
SSL_set_cipher_list(tls_con,p);
} else {
SSL_set_cipher_list(ssl_con,DEFAULT_CIPHER_LIST);
SSL_set_cipher_list(tls_con,DEFAULT_CIPHER_LIST);
}
}
ssl_verify_depth = -1;
if ( ssl_debug_flag )
printf("SSL/TLS init done!\r\n");
ssl_initialized = 1;
last_ssl_mode = mode;
debug(F110,"ssl_tn_init","done",0);
return(1);
}
#ifndef NOHTTP
int
#ifdef CK_ANSIC
ssl_http_init(char * hostname)
#else
ssl_http_init(hostname) char * hostname;
#endif /* CK_ANSIC */
{
#ifdef KRB5
extern char * k5_keytab;
extern char * krb5_d_srv;
#endif /* KRB5 */
SSL * tls_conx=NULL;
ssl_http_initialized = 0;
if ( !ck_ssleay_is_installed() )
return(0);
debug(F110,"ssl_http_init",hostname,0);
if (ssl_debug_flag)
printf("SSL_DEBUG_FLAG on\r\n");
if (!tls_http_ctx ) {
#ifdef COMMENT
/* too many web servers still do not support TLSv1 */
tls_http_ctx=(SSL_CTX *)SSL_CTX_new(TLSv1_client_method());
#else /* COMMENT */
tls_http_ctx=(SSL_CTX *)SSL_CTX_new(SSLv23_client_method());
/* This can fail because we do not have RSA available */
if ( !tls_http_ctx ) {
debug(F110,"ssl_http_init","SSLv23_client_method failed",0);
tls_http_ctx=(SSL_CTX *)SSL_CTX_new(SSLv3_client_method());
}
#endif /* COMMENT */
if ( !tls_http_ctx ) {
debug(F110,"ssl_http_init","TLSv1_client_method failed",0);
return(0);
}
#ifdef USE_CERT_CB
SSL_CTX_set_client_cert_cb(tls_http_ctx,ssl_client_cert_callback);
#endif /* USE_CERT_CB */
}
SSL_CTX_set_default_passwd_cb(tls_http_ctx,
(pem_password_cb *)ssl_passwd_callback);
/* for SSL switch on all the interoperability and bug
* workarounds so that we will communicate with people
* that cannot read poorly written specs :-)
* for TLS be sure to prevent use of SSLv2
*/
SSL_CTX_set_options(tls_http_ctx,
SSL_OP_NO_SSLv2|SSL_OP_SINGLE_DH_USE|SSL_OP_EPHEMERAL_RSA);
SSL_CTX_set_info_callback(tls_http_ctx,ssl_client_info_callback);
#ifndef COMMENT
SSL_CTX_set_session_cache_mode(tls_http_ctx,SSL_SESS_CACHE_CLIENT);
SSL_CTX_set_session_id_context(tls_http_ctx,(CHAR *)"3",1);
#else /* COMMENT */
SSL_CTX_set_session_cache_mode(tls_http_ctx,SSL_SESS_CACHE_OFF);
#endif /* COMMENT */
/* make sure we will find certificates in the standard
* location ... otherwise we don't look anywhere for
* these things which is going to make client certificate
* exchange rather useless :-)
*/
#ifdef OS2
#ifdef NT
{
/* The defaults in the SSL crypto library are not appropriate for OS/2 */
char path[CKMAXPATH];
ckmakmsg(path,CKMAXPATH,exedir,"certs",NULL,NULL);
if (SSL_CTX_load_verify_locations(tls_http_ctx,NULL,path) == 0) {
debug(F110,"ssl_http_init unable to load path",path,0);
if (ssl_debug_flag)
printf("?Unable to load verify-dir: %s\r\n",path);
}
ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/certs",NULL,NULL);
if (SSL_CTX_load_verify_locations(tls_http_ctx,NULL,path) == 0) {
debug(F110,"ssl_http_init unable to load path",path,0);
if (ssl_debug_flag)
printf("?Unable to load verify-dir: %s\r\n",path);
}
ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/certs",NULL,NULL);
if (SSL_CTX_load_verify_locations(tls_http_ctx,NULL,path) == 0) {
debug(F110,"ssl_http_init unable to load path",path,0);
if (ssl_debug_flag)
printf("?Unable to load verify-dir: %s\r\n",path);
}
ckmakmsg(path,CKMAXPATH,exedir,"ca_certs.pem",NULL,NULL);
if (SSL_CTX_load_verify_locations(tls_http_ctx,path,NULL) == 0) {
debug(F110,"ssl_http_init unable to load path",path,0);
if (ssl_debug_flag)
printf("?Unable to load verify-file: %s\r\n",path);
}
ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/ca_certs.pem",NULL,NULL);
if (SSL_CTX_load_verify_locations(tls_http_ctx,path,NULL) == 0) {
debug(F110,"ssl_http_init unable to load path",path,0);
if (ssl_debug_flag)
printf("?Unable to load verify-file: %s\r\n",path);
}
ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/ca_certs.pem",NULL,NULL);
if (SSL_CTX_load_verify_locations(tls_http_ctx,path,NULL) == 0) {
debug(F110,"ssl_http_init unable to load path",path,0);
if (ssl_debug_flag)
printf("?Unable to load verify-file: %s\r\n",path);
}
}
#else /* NT */
{
/* The defaults in the SSL crypto library are not appropriate for OS/2 */
char path[CKMAXPATH];
ckmakmsg(path,CKMAXPATH,exedir,"certs",NULL,NULL);
if (SSL_CTX_load_verify_locations(tls_http_ctx,NULL,path) == 0) {
debug(F110,"ssl_http_init unable to load path",path,0);
if (ssl_debug_flag)
printf("?Unable to load verify-dir: %s\r\n",path);
}
ckmakmsg(path,CKMAXPATH,exedir,"ca_certs.pem",NULL,NULL);
if (SSL_CTX_load_verify_locations(tls_http_ctx,path,NULL) == 0) {
debug(F110,"ssl_http_init unable to load path",path,0);
if (ssl_debug_flag)
printf("?Unable to load verify-file: %s\r\n",path);
}
}
#endif /* NT */
#else /* OS2 */
SSL_CTX_set_default_verify_paths(tls_http_ctx);
#endif /* OS2 */
if (ssl_verify_file &&
SSL_CTX_load_verify_locations(tls_http_ctx,ssl_verify_file,NULL) == 0) {
debug(F110,"ssl_http_init unable to load ssl_verify_file",ssl_verify_file,0);
if (ssl_debug_flag)
printf("?Unable to load verify-file: %s\r\n",ssl_verify_file);
}
if (ssl_verify_dir &&
SSL_CTX_load_verify_locations(tls_http_ctx,NULL,ssl_verify_dir) == 0) {
debug(F110,"ssl_http_init unable to load ssl_verify_dir",ssl_verify_dir,0);
if (ssl_debug_flag)
printf("?Unable to load verify-dir: %s\r\n",ssl_verify_dir);
}
SSL_CTX_set_verify(tls_http_ctx,ssl_verify_flag,
ssl_client_verify_callback);
/* Free the existing CRL Store */
if (crl_store) {
X509_STORE_free(crl_store);
crl_store = NULL;
}
/* set up the new CRL Store */
crl_store = X509_STORE_new();
if (crl_store) {
#ifdef OS2
char path[CKMAXPATH];
ckmakmsg(path,CKMAXPATH,exedir,"crls",NULL,NULL);
if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
debug(F110,"ssl_http_init unable to load dir",path,0);
if (ssl_debug_flag)
printf("?Unable to load crl-dir: %s\r\n",path);
}
#ifdef NT
ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/crls",NULL,NULL);
if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
debug(F110,"ssl_http_init unable to load dir",path,0);
if (ssl_debug_flag)
printf("?Unable to load crl-dir: %s\r\n",path);
}
ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/crls",NULL,NULL);
if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
debug(F110,"ssl_http_init unable to load dir",path,0);
if (ssl_debug_flag)
printf("?Unable to load crl-dir: %s\r\n",path);
}
#endif /* NT */
ckmakmsg(path,CKMAXPATH,exedir,"ca_crls.pem",NULL,NULL);
if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
debug(F110,"ssl_http_init unable to load file",path,0);
if (ssl_debug_flag)
printf("?Unable to load crl-file: %s\r\n",path);
}
#ifdef NT
ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/ca_crls.pem",NULL,NULL);
if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
debug(F110,"ssl_http_init unable to load file",path,0);
if (ssl_debug_flag)
printf("?Unable to load crl-file: %s\r\n",path);
}
ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/ca_crls.pem",NULL,NULL);
if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
debug(F110,"ssl_http_init unable to load file",path,0);
if (ssl_debug_flag)
printf("?Unable to load crl-file: %s\r\n",path);
}
#endif /* NT */
#endif /* OS2 */
if (ssl_crl_file || ssl_crl_dir) {
if (ssl_crl_file &&
X509_STORE_load_locations(crl_store,ssl_crl_file,NULL) == 0) {
debug(F110,"ssl_http_init unable to load ssl_crl_file",ssl_crl_file,0);
if (ssl_debug_flag)
printf("?Unable to load crl-file: %s\r\n",ssl_crl_file);
}
if (ssl_crl_dir &&
X509_STORE_load_locations(crl_store,NULL,ssl_crl_dir) == 0) {
debug(F110,"ssl_http_init unable to load ssl_crl_dir",ssl_crl_dir,0);
if (ssl_debug_flag)
printf("?Unable to load crl-dir: %s\r\n",ssl_crl_dir);
}
} else {
X509_STORE_set_default_paths(crl_store);
}
}
#ifndef COMMENT
tls_conx = tls_http_con;
tls_http_con=(SSL *)SSL_new(tls_http_ctx);
if ( !tls_http_con ) {
debug(F110,"ssl_http_init","SSL_new(tls_http_con) failed",0);
tls_http_con = tls_conx;
return(0);
}
if (tls_conx) {
SSL_set_session(tls_http_con, SSL_get_session(tls_conx));
#ifdef SSL_KRB5
if (tls_conx->kssl_ctx) {
kssl_ctx_free(tls_conx->kssl_ctx);
tls_conx->kssl_ctx = NULL;
}
#endif /* SSL_KRB5 */
SSL_free(tls_conx);
tls_conx = NULL;
}
#else /* COMMENT */
/* I don't know why this does not work to reuse the connection. */
if ( tls_http_con ) {
SSL_clear(tls_http_con);
SSL_set_session(tls_http_con,NULL);
SSL_set_accept_state(tls_http_con) ;
} else {
tls_http_con=(SSL *)SSL_new(tls_http_ctx);
if ( !tls_http_con ) {
debug(F110,"ssl_http_init","SSL_new(tls_http_ctx) failed",0);
tls_http_con = tls_conx;
return(0);
}
}
#endif /* COMMENT */
#ifdef SSL_KRB5
#ifndef KRB5_SERVICE_NAME
#define KRB5_SERVICE_NAME "host"
#endif
if (tls_http_con->kssl_ctx == NULL)
tls_http_con->kssl_ctx = kssl_ctx_new();
if (tls_http_con->kssl_ctx != NULL)
kssl_ctx_setstring(tls_http_con->kssl_ctx, KSSL_SERVER, hostname);
kssl_ctx_setstring(tls_http_con->kssl_ctx, KSSL_SERVICE,
krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME);
#endif /* SSL_KRB5 */
if (ssl_cipher_list)
SSL_set_cipher_list(tls_http_con,ssl_cipher_list);
else {
char * p;
if (p = getenv("SSL_CIPHER")) {
SSL_set_cipher_list(tls_http_con,p);
} else {
SSL_set_cipher_list(tls_http_con,DEFAULT_CIPHER_LIST);
}
}
ssl_verify_depth = -1;
if ( ssl_debug_flag )
printf("SSL/TLS init done!\r\n");
ssl_http_initialized = 1;
return(1);
}
#endif /* NOHTTP */
char *
ssl_get_dNSName(ssl) SSL * ssl;
{
static char *dns = NULL;
X509 *server_cert = NULL;
int i;
X509_EXTENSION *ext = NULL;
STACK_OF(GENERAL_NAME) *ialt = NULL;
GENERAL_NAME *gen = NULL;
if ( dns ) {
free(dns);
dns = NULL;
}
if (server_cert = SSL_get_peer_certificate(ssl)) {
if ((i = X509_get_ext_by_NID(server_cert, NID_subject_alt_name, -1))<0)
return NULL;
if (!(ext = X509_get_ext(server_cert, i)))
return NULL;
X509V3_add_standard_extensions();
if (!(ialt = X509V3_EXT_d2i(ext)))
return NULL;
for (i = 0; i < sk_GENERAL_NAME_num(ialt); i++) {
gen = sk_GENERAL_NAME_value(ialt, i);
if (gen->type == GEN_DNS) {
if (!gen->d.ia5 || !gen->d.ia5->length)
break;
if (strlen((char *)gen->d.ia5->data) != gen->d.ia5->length) {
/* Ignoring IA5String containing null character */
continue;
}
dns = malloc(gen->d.ia5->length + 1);
if (dns) {
memcpy(dns, gen->d.ia5->data, gen->d.ia5->length);
dns[gen->d.ia5->length] = 0;
}
break;
}
}
X509V3_EXT_cleanup();
}
cleanup:
if (ialt) sk_GENERAL_NAME_free(ialt);
if (server_cert) X509_free(server_cert);
return dns;
}
char *
ssl_get_commonName(ssl) SSL * ssl; {
static char name[256];
int name_text_len;
int err;
X509 *server_cert;
name_text_len = 0;
if (server_cert = SSL_get_peer_certificate(ssl)) {
name_text_len =
X509_NAME_get_text_by_NID(X509_get_subject_name(server_cert),
NID_commonName, name, sizeof(name));
X509_free(server_cert);
}
if (name_text_len <= 0) {
/* Common Name was empty or not retrieved */
err = 0;
} else if (strlen(name) != name_text_len) {
/* Ignoring Common Name containing null character */
err = 0;
} else {
err = 1;
}
if (err > 0)
return name;
else
return NULL;
}
char *
ssl_get_issuer_name(ssl) SSL * ssl;
{
static char name[256];
X509 *server_cert;
name[0] = '\0';
if (server_cert = SSL_get_peer_certificate(ssl)) {
X509_NAME_oneline(X509_get_issuer_name(server_cert),name,sizeof(name));
X509_free(server_cert);
return name;
}
else {
#ifdef COMMENT
fprintf(stderr, "Warning: No certificate from server!\r\n");
#endif /* COMMENT */
return NULL;
}
}
char *
ssl_get_subject_name(ssl) SSL * ssl;
{
static char name[256];
X509 *server_cert;
name[0] = '\0';
if (server_cert = SSL_get_peer_certificate(ssl)) {
X509_NAME_oneline(X509_get_subject_name(server_cert),name,sizeof(name));
X509_free(server_cert);
return name;
}
else
return NULL;
}
#ifdef COMMENT
#ifdef CK_SSL
&& !(ck_ssleay_is_installed() &&
(tls_active_flag || ssl_active_flag) &&
ssl_anonymous_cipher(tls_active_flag?tls_con:ssl_con))
#endif /* CK_SSL */
int
ssl_anonymous_cipher(ssl) SSL * ssl;
{
X509 * cert;
if (sstelnet)
cert = SSL_get_certificate(ssl);
else
cert = SSL_get_peer_certificate(ssl);
if ( cert ) {
X509_free(cert);
return 0;
}
return 1;
}
#endif /* COMMENT */
/*
This one is (very much!) based on work by
Ralf S. Engelschall <rse@engelschall.com>.
Comments by Ralf.
*/
int
ssl_verify_crl(int ok, X509_STORE_CTX *ctx)
{
X509_OBJECT obj;
X509_NAME *subject = NULL;
X509_NAME *issuer = NULL;
X509 *xs = NULL;
X509_CRL *crl = NULL;
X509_REVOKED *revoked = NULL;
X509_STORE_CTX * store_ctx = NULL;
long serial;
BIO *bio = NULL;
int i, n, rc;
char *cp;
char *cp2;
/*
* Unless a revocation store for CRLs was created we
* cannot do any CRL-based verification, of course.
*/
if (!crl_store)
return ok;
store_ctx = X509_STORE_CTX_new();
if ( !store_ctx )
return(ok);
/*
* Determine certificate ingredients in advance
*/
xs = X509_STORE_CTX_get_current_cert(ctx);
subject = X509_get_subject_name(xs);
issuer = X509_get_issuer_name(xs);
/*
* OpenSSL provides the general mechanism to deal with CRLs but does not
* use them automatically when verifying certificates, so we do it
* explicitly here. We will check the CRL for the currently checked
* certificate, if there is such a CRL in the store.
*
* We come through this procedure for each certificate in the certificate
* chain, starting with the root-CA's certificate. At each step we've to
* both verify the signature on the CRL (to make sure it's a valid CRL)
* and it's revocation list (to make sure the current certificate isn't
* revoked). But because to check the signature on the CRL we need the
* public key of the issuing CA certificate (which was already processed
* one round before), we've a little problem. But we can both solve it and
* at the same time optimize the processing by using the following
* verification scheme (idea and code snippets borrowed from the GLOBUS
* project):
*
* 1. We'll check the signature of a CRL in each step when we find a CRL
* through the _subject_ name of the current certificate. This CRL
* itself will be needed the first time in the next round, of course.
* But we do the signature processing one round before this where the
* public key of the CA is available.
*
* 2. We'll check the revocation list of a CRL in each step when
* we find a CRL through the _issuer_ name of the current certificate.
* This CRLs signature was then already verified one round before.
*
* This verification scheme allows a CA to revoke its own certificate as
* well, of course.
*/
/*
* Try to retrieve a CRL corresponding to the _subject_ of
* the current certificate in order to verify it's integrity.
*/
memset((char *)&obj, 0, sizeof(obj));
X509_STORE_CTX_init(store_ctx, crl_store, NULL, NULL);
rc = X509_STORE_get_by_subject(store_ctx, X509_LU_CRL, subject, &obj);
X509_STORE_CTX_cleanup(store_ctx);
crl = obj.data.crl;
if (rc > 0 && crl != NULL) {
/*
* Verify the signature on this CRL
*/
if (X509_CRL_verify(crl, X509_get_pubkey(xs)) <= 0) {
fprintf(stderr, "Invalid signature on CRL!\n");
X509_STORE_CTX_set_error(ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE);
X509_OBJECT_free_contents(&obj);
X509_STORE_CTX_free(store_ctx);
return 0;
}
/*
* Check date of CRL to make sure it's not expired
*/
i = X509_cmp_current_time(X509_CRL_get_nextUpdate(crl));
if (i == 0) {
fprintf(stderr, "Found CRL has invalid nextUpdate field.\n");
X509_STORE_CTX_set_error(ctx,
X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD);
X509_OBJECT_free_contents(&obj);
X509_STORE_CTX_free(store_ctx);
return 0;
}
if (i < 0) {
fprintf(stderr,
"Found CRL is expired - revoking all certificates until you get updated CRL.\n"
);
X509_STORE_CTX_set_error(ctx, X509_V_ERR_CRL_HAS_EXPIRED);
X509_OBJECT_free_contents(&obj);
X509_STORE_CTX_free(store_ctx);
return 0;
}
X509_OBJECT_free_contents(&obj);
}
/*
* Try to retrieve a CRL corresponding to the _issuer_ of
* the current certificate in order to check for revocation.
*/
memset((char *)&obj, 0, sizeof(obj));
X509_STORE_CTX_init(store_ctx, crl_store, NULL, NULL);
rc = X509_STORE_get_by_subject(store_ctx, X509_LU_CRL, issuer, &obj);
X509_STORE_CTX_free(store_ctx); /* calls X509_STORE_CTX_cleanup() */
crl = obj.data.crl;
if (rc > 0 && crl != NULL) {
/*
* Check if the current certificate is revoked by this CRL
*/
n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl));
for (i = 0; i < n; i++) {
revoked = sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i);
if (ASN1_INTEGER_cmp(revoked->serialNumber,
X509_get_serialNumber(xs)) == 0) {
serial = ASN1_INTEGER_get(revoked->serialNumber);
cp = X509_NAME_oneline(issuer, NULL, 0);
free(cp);
X509_STORE_CTX_set_error(ctx, X509_V_ERR_CERT_REVOKED);
X509_OBJECT_free_contents(&obj);
return 0;
}
}
X509_OBJECT_free_contents(&obj);
}
return ok;
}
char *
tls_userid_from_client_cert(ssl) SSL * ssl;
{
static char cn[256];
static char *r = cn;
int err;
X509 *client_cert;
if (client_cert = SSL_get_peer_certificate(ssl)) {
/* call the custom function */
err = X509_to_user(client_cert, cn, sizeof(cn));
X509_free(client_cert);
if (err)
return r = NULL;
else
return r;
}
else
return r = NULL;
}
unsigned char **
tls_get_SAN_objs(SSL * ssl, int type)
/* returns NULL or an array of malloc'ed objects of type `type' from the server's
* subjectAltName, remember to free() them all!
*/
{
#define NUM_SAN_OBJS 64
static unsigned char *objs[NUM_SAN_OBJS];
unsigned char **rv = NULL;
X509 *server_cert = NULL;
int i, j;
X509_EXTENSION *ext = NULL;
STACK_OF(GENERAL_NAME) *ialt = NULL;
GENERAL_NAME *gen = NULL;
memset(objs, 0, sizeof(objs));
if (server_cert = SSL_get_peer_certificate(ssl)) {
if ((i = X509_get_ext_by_NID(server_cert, NID_subject_alt_name, -1)) < 0)
goto eject;
if (!(ext = X509_get_ext(server_cert, i)))
goto eject;
X509V3_add_standard_extensions();
if (!(ialt = X509V3_EXT_d2i(ext)))
goto eject;
rv = objs;
for (i = 0, j = 0; i < sk_GENERAL_NAME_num(ialt) && j < NUM_SAN_OBJS - 2; i++) {
gen = sk_GENERAL_NAME_value(ialt, i);
/* The use of V_ASN1_CONTEXT_SPECIFIC is because OpenSSL 0.9.6 defined its
* types | V_ASN1_CONTEXT_SPECIFIC. 0.9.7 does not. In case, we are built
* with one and linked to the other we use this hack.
*/
if ((gen->type | V_ASN1_CONTEXT_SPECIFIC) == (type | V_ASN1_CONTEXT_SPECIFIC)) {
if (!gen->d.ia5 || !gen->d.ia5->length)
break;
if (strlen((char *)gen->d.ia5->data) != gen->d.ia5->length) {
/* Ignoring IA5String containing null character */
continue;
}
objs[j] = malloc(gen->d.ia5->length + 1);
if (objs[j]) {
memcpy(objs[j], gen->d.ia5->data, gen->d.ia5->length);
objs[j][gen->d.ia5->length] = 0;
j++;
}
}
}
X509V3_EXT_cleanup();
}
eject:
if (ialt) sk_GENERAL_NAME_free(ialt);
if (server_cert) X509_free(server_cert);
return rv;
}
static int
dNSName_cmp(const char *host, const char *dNSName)
{
int c1 = 1, c2 = 1, num_comp, rv = -1;
char *p, *p1, *p2, *host_copy=NULL, *dNSName_copy=NULL;
/* first we count the number of domain name components in both parameters.
* they should be equal many, or it's not a match
*/
p = (char *) host;
while (p = strstr(p, ".")) {
c1++;
p++;
}
p = (char *) dNSName;
while (p = strstr(p, ".")) {
c2++;
p++;
}
if (c1 != c2)
return -1;
num_comp = c1;
makestr(&host_copy,host);
makestr(&dNSName_copy,dNSName);
if (host_copy == NULL || dNSName_copy == NULL)
goto eject;
/* make substrings by replacing '.' with '\0' */
p = dNSName_copy;
while (p = strstr(p, ".")) {
*p = '\0';
p++;
}
p = host_copy;
while (p = strstr(p, ".")) {
*p = '\0';
p++;
}
/* compare each component */
p1 = host_copy;
p2 = dNSName_copy;
for (; num_comp; num_comp--) {
if (!ckmatch(p2, p1,0,1))
/* failed match */
goto eject;
p1 += strlen(p1) + 1;
p2 += strlen(p2) + 1;
}
/* match ok */
rv = 0;
eject:
if (dNSName_copy) free(dNSName_copy);
if (host_copy) free(host_copy);
return rv;
}
static int
show_hostname_warning(char *s1, char *s2)
{
char prefix[1024];
int ok = 1;
setverbosity();
ckmakxmsg(prefix,1024,
"Warning: Hostname (\"", s1,
"\") does not match server's certificate (\"", s2, "\")",
NULL,NULL,NULL,NULL,NULL,NULL,NULL);
if (ssl_verify_flag)
ok = uq_ok(prefix,
"Continue? (Y/N) ",
3, NULL, 0);
else if (verbosity)
printf(prefix);
return(ok);
}
#ifndef OSF50
#ifndef HPUX10
#ifndef HPUX1100
#ifndef SCO_OSR505
#ifndef OpenBSD
#ifndef FREEBSD4
#ifndef NETBSD15
#ifndef LINUX
#ifndef AIX41
#ifndef UW7
#ifndef IRIX65
#ifndef SOLARIS9
#ifndef SOLARIS8
#ifndef SOLARIS7
#ifndef MACOSX
#ifdef DEC_TCPIP
#define inet_aton INET_ATON
#endif /* DEC_TCPIP */
#ifndef NO_DCL_INET_ATON
static int
inet_aton(char * ipaddress, struct in_addr * ia) {
struct stringarray * q;
union {
unsigned long l;
unsigned char b[4];
} dummy;
q = cksplit(1,0,ipaddress,".","0123456789abcdefACDEF",8,0,0);
if (q->a_size == 4) {
dummy.b[0] = atoi(q->a_head[1]);
dummy.b[1] = atoi(q->a_head[2]);
dummy.b[2] = atoi(q->a_head[3]);
dummy.b[3] = atoi(q->a_head[4]);
ia->s_addr = dummy.l;
return(ia->s_addr != 0);
}
return(0);
}
#endif /* NO_DCL_INET_ATON */
#endif /* MACOSX */
#endif /* SOLARIS7 */
#endif /* SOLARIS8 */
#endif /* SOLARIS9 */
#endif /* IRIX65 */
#endif /* UW7 */
#endif /* AIX41 */
#endif /* LINUX */
#endif /* NETBSD15 */
#endif /* FREEBSD4 */
#endif /* OpenBSD */
#endif /* SCO_OSR505 */
#endif /* HPUX1100 */
#endif /* HPUX10 */
#endif /* OSF50 */
int
ssl_check_server_name(SSL * ssl, char * hostname)
/* returns 0 if hostname and server's cert matches, else -1 */
{
char * commonName;
unsigned char ** dNSName;
unsigned char ** ipAddress;
struct in_addr ia;
int rv;
setverbosity();
if (verbosity && !inserver) {
if (dNSName = tls_get_SAN_objs(ssl,GEN_DNS)) {
int i = 0;
for (i = 0; dNSName[i]; i++) {
printf("Certificate[0] altSubjectName DNS=%s\r\n",dNSName[i]);
free(dNSName[i]);
}
}
if (ipAddress = tls_get_SAN_objs(ssl,GEN_IPADD)) {
int i = 0;
char *server_ip;
struct in_addr ia;
for (i = 0; ipAddress[i]; i++) {
if (ipAddress[i]) {
ia.s_addr = *(unsigned long *)ipAddress[i];
server_ip = inet_ntoa(ia);
printf("Certificate[0] altSubjectName IPAddr=%s\r\n",server_ip);
}
free(ipAddress[i]);
}
/* ipAddress points to a static - don't free */
}
if (dNSName = tls_get_SAN_objs(ssl,GEN_EMAIL)) {
int i = 0;
for (i = 0; dNSName[i]; i++) {
printf("Certificate[0] altSubjectName Email=%s\r\n",dNSName[i]);
free(dNSName[i]);
}
}
if (dNSName = tls_get_SAN_objs(ssl,GEN_URI)) {
int i = 0;
for (i = 0; dNSName[i]; i++) {
printf("Certificate[0] altSubjectName URI=%s\r\n",dNSName[i]);
free(dNSName[i]);
}
}
if (dNSName = tls_get_SAN_objs(ssl,GEN_OTHERNAME)) {
int i = 0;
for (i = 0; dNSName[i]; i++) {
printf("Certificate[0] altSubjectName Other=%s\r\n",dNSName[i]);
free(dNSName[i]);
}
}
}
/* first we check if `hostname' is in fact an ip address */
if (inet_aton(hostname, &ia)) {
ipAddress = tls_get_SAN_objs(ssl,GEN_IPADD);
if (ipAddress) {
int i = 0;
char *server_ip = "UNKNOWN";
for (i = 0; ipAddress[i]; i++)
if (*(unsigned long *)ipAddress[i] == ia.s_addr)
return 0;
if (ipAddress[i - 1]) {
ia.s_addr = *(unsigned long *)ipAddress[i - 1];
server_ip = inet_ntoa(ia);
}
rv = show_hostname_warning(hostname, server_ip) ? 0 : -1;
for (i = 0; ipAddress[i]; i++)
free(ipAddress[i]);
} else {
rv = show_hostname_warning(hostname, "NO IP IN CERT") ? 0 : -1;
}
return(rv);
}
/* look for dNSName(s) in subjectAltName in the server's certificate */
dNSName = tls_get_SAN_objs(ssl,GEN_DNS);
if (dNSName) {
int i = 0;
for (i = 0; dNSName[i]; i++) {
if (!dNSName_cmp(hostname,(char *)dNSName[i]))
return 0;
}
rv = show_hostname_warning(hostname,
(char *)((dNSName[i - 1] == NULL) ?
(char *)"UNKNOWN" : (char *)dNSName[i - 1]))
? 0 : -1;
for (i = 0; dNSName[i]; i++)
free(dNSName[i]);
return rv;
} else if ((commonName = ssl_get_commonName(ssl))) {
/* so the server didn't have any dNSName's, check the commonName */
if (!dNSName_cmp(hostname, commonName))
return 0;
else
return (show_hostname_warning(hostname, commonName) ? 0 : -1);
}
return -1;
}
/* Is 'user' authorized to access the system without a login */
int
tls_is_user_valid(SSL * ssl, const char *user)
{
X509 *client_cert;
int r = 0;
if ( !ssl || !user || !user[0] )
return(0);
if (!(client_cert = SSL_get_peer_certificate(ssl)))
return 0;
/* Use user supplied function */
r = X509_userok(client_cert,user);
X509_free(client_cert);
return r;
}
int
tls_is_anon(int x)
{
char buf[128];
SSL_CIPHER * cipher;
SSL * ssl = NULL;
switch ( x ) {
#ifndef NOFTP
#ifndef SYSFTP
case 1: /* ftp command */
if ( ssl_ftp_active_flag )
ssl = ssl_ftp_con;
else
return(0);
break;
case 2: /* ftp data */
if ( ssl_ftp_data_active_flag )
ssl = ssl_ftp_data_con;
else
return(0);
break;
#endif /* SYSFTP */
#endif /* NOFTP */
default:
if (tls_active_flag)
ssl = tls_con;
else if (ssl_active_flag)
ssl = ssl_con;
else
return(0);
}
cipher = SSL_get_current_cipher(ssl);
if (SSL_CIPHER_description(cipher,buf,sizeof(buf))) {
if (ckindex("Au=None",buf,0,0,0) != 0)
return(1); /* anonymous */
return(0); /* known */
} else {
/* could not get cipher description. Assume anonymous */
return(1);
}
}
int
tls_is_krb5(int x)
{
char buf[128];
SSL_CIPHER * cipher;
SSL * ssl = NULL;
switch ( x ) {
#ifndef NOFTP
#ifndef SYSFTP
case 1: /* ftp command */
if ( ssl_ftp_active_flag )
ssl = ssl_ftp_con;
else
return(0);
break;
case 2: /* ftp data */
if ( ssl_ftp_data_active_flag )
ssl = ssl_ftp_data_con;
else
return(0);
break;
#endif /* SYSFTP */
#endif /* NOFTP */
#ifndef NOHTTP
case 3:
if ( tls_http_active_flag )
ssl = tls_http_con;
break;
#endif /* NOHTTP */
default:
if (tls_active_flag)
ssl = tls_con;
else if (ssl_active_flag)
ssl = ssl_con;
else
return(0);
}
cipher = SSL_get_current_cipher(ssl);
if (cipher && SSL_CIPHER_description(cipher,buf,sizeof(buf))) {
if (ckindex("Au=KRB5",buf,0,0,0) != 0)
return(1); /* krb5 */
}
return(0); /* not */
}
int
ssl_get_client_finished(char *buf, int count)
{
#ifdef NO_GET_FINISHED
return(0);
#else
if (sstelnet || tcp_incoming) {
return(SSL_get_peer_finished(ssl_active_flag?ssl_con:tls_con,
buf,count));
} else {
return(SSL_get_finished(ssl_active_flag?ssl_con:tls_con,
buf,count));
}
#endif /* NO_GET_FINISHED */
}
int
ssl_get_server_finished(char *buf, int count)
{
#ifdef NO_GET_FINISHED
return(0);
#else
if (sstelnet || tcp_incoming) {
return(SSL_get_finished(ssl_active_flag?ssl_con:tls_con,
buf,count));
} else {
return(SSL_get_peer_finished(ssl_active_flag?ssl_con:tls_con,
buf,count));
}
#endif /* NO_GET_FINISHED */
}
#ifdef CK_AUTHENTICATION
int
#ifdef CK_ANSIC
ssl_reply(int how, unsigned char *data, int cnt)
#else
ssl_reply(how,data,cnt) int how; unsigned char *data; int cnt;
#endif
{
char * str=NULL;
setverbosity();
data += 4; /* Point to status byte */
cnt -= 4;
if(cnt-- < 1) {
auth_finished(AUTH_REJECT);
return AUTH_FAILURE;
}
switch(*data++) {
case SSL_ACCEPT:
if (tn_deb || debses)
tn_debug("[SSL - handshake starting]");
else if ( verbosity )
printf("[SSL - handshake starting]\r\n");
debug(F110,"ssl_reply","[SSL - handshake starting]",0);
/* right ... now we drop into the SSL library */
if (!ssl_only_flag) {
if (ssl_dummy_flag) {
if (tn_deb || debses)
tn_debug("[SSL - Dummy Connected]");
else if ( verbosity ) {
printf("[SSL - Dummy Connected]\r\n");
}
debug(F110,"ssl_reply","[SSL - Dummy Connected]",0);
auth_finished(AUTH_UNKNOWN);
accept_complete = 1;
return AUTH_SUCCESS;
}
if (SSL_connect(ssl_con) <= 0) {
int len;
if (tn_deb || debses) {
tn_debug("[SSL - FAILED]");
ERR_print_errors(bio_err);
len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
printf(ssl_err);
} else if ( verbosity ) {
printf("[SSL - FAILED]\r\n");
ERR_print_errors(bio_err);
len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
printf(ssl_err);
}
debug(F110,"ssl_reply","[SSL - FAILED]",0);
auth_finished(AUTH_REJECT);
ttclos(0);
return AUTH_FAILURE;
} else {
if (tn_deb || debses)
tn_debug("[SSL - OK]");
else if ( verbosity ) {
printf("[SSL - OK]\r\n");
}
debug(F110,"ssl_reply","[SSL - OK]",0);
ssl_active_flag = 1;
ssl_display_connect_details(ssl_con,0,verbosity);
}
}
auth_finished(AUTH_UNKNOWN);
accept_complete = 1;
break;
case SSL_REJECT:
if (tn_deb || debses) {
tn_debug(
"[SSL - failed to switch on SSL - trying plaintext login]");
} else if ( verbosity ) {
printf("[SSL - failed to switch on SSL]\r\n");
printf("Trying plaintext login:\r\n");
}
debug(F110,"ssl_reply","[SSL - failed to switch on SSL]",0);
auth_finished(AUTH_REJECT);
return AUTH_FAILURE;
default:
return AUTH_FAILURE;
}
return AUTH_SUCCESS;
}
int
#ifdef CK_ANSIC
ssl_is(unsigned char *data, int cnt)
#else
ssl_is(data,cnt) unsigned char *data; int cnt;
#endif
{
if ((cnt -= 4) < 1)
return AUTH_FAILURE;
setverbosity();
data += 4;
switch(*data++) {
case SSL_START:
/* server starts the SSL stuff now ... */
if (!ssl_only_flag) {
if ( !tls_load_certs(ssl_ctx,ssl_con,1) ) {
auth_finished(AUTH_REJECT);
return AUTH_FAILURE;
}
if (tn_deb || debses)
tn_debug("[SSL - handshake starting]");
else if ( verbosity )
printf("[SSL - handshake starting]\r\n");
debug(F110,"ssl_is","[SSL - handshake starting]",0);
SendSSLAuthSB(SSL_ACCEPT, (void *)0, 0);
auth_ssl_valid = 1;
if (ssl_dummy_flag) {
if (tn_deb || debses)
tn_debug("[SSL - Dummy Connected]");
else if ( verbosity ) {
printf("[SSL - Dummy Connected]\r\n");
}
debug(F110,"ssl_is","[SSL - Dummy Connected]",0);
accept_complete = 1;
auth_finished(AUTH_UNKNOWN);
return AUTH_SUCCESS;
}
if (SSL_accept(ssl_con) <= 0) {
char errbuf[1024];
sprintf(errbuf,"[SSL - SSL_accept error: %s",
ERR_error_string(ERR_get_error(),NULL));
if (tn_deb || debses)
tn_debug(errbuf);
else if ( ssl_debug_flag )
printf("%s\r\n",errbuf);
else if ( verbosity )
printf("[SSL - SSL_accept error]\r\n");
debug(F110,"ssl_is",errbuf,0);
auth_finished(AUTH_REJECT);
ttclos(0);
return AUTH_FAILURE;
}
if (tn_deb || debses)
tn_debug("[SSL - OK]");
else if ( verbosity ) {
printf("[SSL - OK]\r\n");
}
debug(F110,"ssl_is","[SSL - OK]",0);
ssl_active_flag = 1;
ssl_display_connect_details(ssl_con,1,verbosity);
/* now check to see that we got exactly what we
* wanted from the caller ... if a certificate is
* required then we make 100% sure that we were
* given one during the handshake (as it is an optional
* part of SSL)
*/
#ifdef SSL_KRB5
if ( tls_is_krb5(0) ) {
if (ssl_con->kssl_ctx->client_princ)
debug(F110,"ssl_is KRB5",ssl_con->kssl_ctx->client_princ,0);
} else
#endif /* SSL_KRB5 */
if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
X509 * peer = SSL_get_peer_certificate(ssl_con);
if (peer == NULL) {
if (tn_deb || debses)
tn_debug("[SSL - peer check failed]");
else if (ssl_debug_flag)
printf("[SSL - peer check failed]\r\n");
debug(F110,"ssl_is","[SSL - peer check failed]",0);
/* LOGGING REQUIRED HERE! */
auth_finished(AUTH_REJECT);
return AUTH_FAILURE;
}
}
auth_finished(AUTH_UNKNOWN);
accept_complete = 1;
}
break;
default:
SendSSLAuthSB(SSL_REJECT, (void *) "Unknown option received", -1);
if (tn_deb || debses)
tn_debug("[SSL - Unknown option received]");
else
printf("Unknown SSL option %d\r\n", data[-1]);
debug(F111,"ssl_is","[SSL - Unknown option received]",data[-1]);
auth_ssl_valid = 0;
auth_finished(AUTH_REJECT);
return(AUTH_FAILURE);
}
return AUTH_SUCCESS;
}
#endif /* CK_AUTHENTICATION */
int
ck_tn_tls_negotiate(VOID)
{
X509 * peer = NULL;
char str[256], *uid=NULL;
extern int sstelnet;
if ( !ck_ssleay_is_installed() )
return(-1);
setverbosity();
if (sstelnet) {
/* server starts the TLS stuff now ... */
if (!tls_only_flag) {
if ( !tls_load_certs(tls_ctx,tls_con,1) ) {
auth_finished(AUTH_REJECT);
return -1;
}
if (tn_deb || debses)
tn_debug("[TLS - handshake starting]");
else if ( verbosity )
printf("[TLS - handshake starting]\r\n");
debug(F110,"ck_tn_tls_negotiate","[TLS - handshake starting]",0);
if (ssl_dummy_flag) {
if (tn_deb || debses)
tn_debug("[TLS - Dummy Connected]");
else if ( verbosity ) {
printf("[TLS - Dummy Connected]\r\n");
}
debug(F110,"ck_tn_tls_negotiate","[TLS - Dummy Connected]",0);
accept_complete = 1;
auth_finished(AUTH_REJECT);
return 0;
}
if (SSL_accept(tls_con) <= 0) {
char errbuf[1024];
sprintf(errbuf,"[TLS - SSL_accept error: %s",
ERR_error_string(ERR_get_error(),NULL));
if (tn_deb || debses)
tn_debug(errbuf);
else if ( ssl_debug_flag )
printf("%s\r\n",errbuf);
else if ( verbosity )
printf("[TLS - SSL_accept error]\r\n");
debug(F110,"ck_tn_tls_negotiate",errbuf,0);
auth_finished(AUTH_REJECT);
return -1;
}
if (tn_deb || debses)
tn_debug("[TLS - OK]");
else if ( verbosity ) {
printf("[TLS - OK]\r\n");
}
debug(F110,"ck_tn_tls_negotiate","[TLS - OK]",0);
tls_active_flag = 1;
ssl_display_connect_details(tls_con,1,verbosity);
#ifdef SSL_KRB5
if ( tls_is_krb5(0) ) {
if (tls_con->kssl_ctx->client_princ) {
char *p;
ckstrncpy(szUserNameAuthenticated,
tls_con->kssl_ctx->client_princ,
UIDBUFLEN);
ckstrncpy(szUserNameRequested,
tls_con->kssl_ctx->client_princ,
UIDBUFLEN);
for ( p = szUserNameRequested; *p ; p++ ) {
if ( *p == '@' || *p == '/' ) {
*p = '\0';
break;
}
}
} else {
szUserNameRequested[0] = '\0';
szUserNameAuthenticated[0] = '\0';
}
#ifdef CK_LOGIN
if (zvuser(szUserNameRequested))
auth_finished(AUTH_VALID);
else
#endif /* CK_LOGIN */
auth_finished(AUTH_USER);
} else
#endif /* SSL_KRB5 */
{
/* now check to see that we got exactly what we
* wanted from the caller ... if a certificate is
* required then we make 100% sure that we were
* given one during the handshake (as it is an optional
* part of TLS)
*/
peer=SSL_get_peer_certificate(tls_con);
if (peer == NULL) {
debug(F100,"SSL_get_peer_certificate() == NULL","",0);
auth_finished(AUTH_REJECT);
if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
if (tn_deb || debses)
tn_debug("[TLS - peer check failed]");
else if (ssl_debug_flag) {
printf("[TLS - peer check failed]\r\n");
}
debug(F110,
"ck_tn_tls_negotiate",
"[TLS - peer check failed]",
0
);
/* LOGGING REQUIRED HERE! */
return -1;
}
} else {
debug(F100,"SSL_get_peer_certificate() != NULL","",0);
X509_NAME_get_text_by_NID(X509_get_subject_name(peer),
NID_commonName,str,
256
);
if ( verbosity )
printf("[TLS - commonName=%s]\r\n",str);
X509_NAME_get_text_by_NID(X509_get_subject_name(peer),
#ifndef NID_x500UniqueIdentifier
NID_uniqueIdentifier,
#else
NID_x500UniqueIdentifier,
#endif
str,
256
);
if ( verbosity )
printf("[TLS - uniqueIdentifier=%s]\r\n",str);
/* Try to determine user name */
uid = tls_userid_from_client_cert(tls_con);
if ( uid ) {
/* This code is very questionable.
* How should it behave?
* The client has presented a certificate that
* contains a username. We have validated the
* certificate but we do not automatically
* log the user in unless there is a .tlslogin
* file.
*/
ckstrncpy(szUserNameRequested,uid,UIDBUFLEN);
#ifdef CK_LOGIN
if (zvuser(uid))
auth_finished(AUTH_VALID);
else
#endif /* CK_LOGIN */
auth_finished(AUTH_USER);
}
else {
szUserNameRequested[0] = '\0';
auth_finished(AUTH_REJECT);
}
}
}
}
} else {
char * str=NULL;
if (tn_deb || debses)
tn_debug("[TLS - handshake starting]");
else if ( verbosity )
printf("[TLS - handshake starting]\r\n");
debug(F110,"ck_tn_tls_negotiate","[TLS - handshake starting]",0);
/* right ... now we drop into the SSL library */
if (!tls_only_flag) {
char *subject=NULL, *issuer=NULL, *commonName=NULL, *dNSName=NULL;
if (ssl_dummy_flag) {
if (tn_deb || debses)
tn_debug("[TLS - Dummy Connected]");
else if ( verbosity ) {
printf("[TLS - Dummy Connected]\r\n");
}
debug(F110,"ck_tn_tls_negotiate","[TLS - Dummy Connected]",0);
auth_finished(AUTH_REJECT);
accept_complete = 1;
return 0;
}
#ifndef USE_CERT_CB
if (!tls_load_certs(tls_ctx,tls_con,0))
return(-1);
#endif /* USE_CERT_CB */
if (SSL_connect(tls_con) <= 0) {
int len;
if (tn_deb || debses) {
tn_debug("[TLS - FAILED]");
ERR_print_errors(bio_err);
len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
printf(ssl_err);
} else if ( verbosity ) {
printf("[TLS - FAILED]\r\n");
ERR_print_errors(bio_err);
len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
printf(ssl_err);
}
debug(F110,"ck_tn_tls_negotiate","[TLS - FAILED]",0);
auth_finished(AUTH_REJECT);
return -1;
}
tls_active_flag = 1;
if ( !ssl_certsok_flag && (ssl_verify_flag & SSL_VERIFY_PEER)
&& !tls_is_krb5(0)) {
char prmpt[1024];
subject = ssl_get_subject_name(tls_con);
if (!subject) {
if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
{
if (tn_deb || debses)
tn_debug("[TLS - FAILED]");
else if ( verbosity )
printf("[TLS - FAILED]\r\n");
debug(F110,"ck_tn_tls_negotiate","[TLS - FAILED]",0);
auth_finished(AUTH_REJECT);
return -1;
} else {
int ok;
ok = uq_ok("Warning: Server didn't provide a certificate",
"Continue? (Y/N)", 3, NULL, 0);
if (!ok) {
if (tn_deb || debses)
tn_debug("[TLS - FAILED]");
else if ( verbosity )
printf("[TLS - FAILED]\r\n");
debug(F110,
"ck_tn_tls_negotiate","[TLS - FAILED]",0);
auth_finished(AUTH_REJECT);
return -1;
}
}
} else if (ssl_check_server_name(tls_con, szHostName)) {
if (tn_deb || debses)
tn_debug("[TLS - FAILED]");
else if ( verbosity )
printf("[TLS - FAILED]\r\n");
debug(F110,
"ck_tn_tls_negotiate","[TLS - FAILED]",0);
auth_finished(AUTH_REJECT);
return -1;
}
}
if ( ssl_debug_flag && ssl_finished_messages) {
char msg[32];
int i, len=32;
extern char tn_msg[], hexbuf[];
tn_msg[0] = '\0';
len = ssl_get_client_finished(msg,len);
if ( len > 0 ) {
for ( i=0;i<len;i++ ) {
sprintf(hexbuf,"%02X ",msg[i]);
ckstrncat(tn_msg,hexbuf,TN_MSG_LEN);
}
printf("TLS client finished: %s\r\n",tn_msg);
}
tn_msg[0] = '\0';
len = ssl_get_server_finished(msg,len);
if ( len > 0 ) {
for ( i=0;i<len;i++ ) {
sprintf(hexbuf,"%02X ",msg[i]);
ckstrncat(tn_msg,hexbuf,TN_MSG_LEN);
}
printf("TLS server finished: %s\r\n",tn_msg);
}
}
if (tn_deb || debses)
tn_debug("[TLS - OK]");
else if ( verbosity )
printf("[TLS - OK]\r\n");
debug(F110,"ck_tn_tls_negotiate","[TLS - OK]",0);
ssl_display_connect_details(tls_con,0,verbosity);
}
auth_finished(AUTH_REJECT);
}
accept_complete = 1;
auth_ssl_valid = 1;
return(0);
}
int
ck_ssl_incoming(fd) int fd;
{
/* if we are not running in debug then any error
* stuff from SSL debug *must* not go down
* the socket (which 0,1,2 are all pointing to by
* default)
*/
int timo = 2000;
setverbosity();
if ( !ck_ssleay_is_installed() )
return(-1);
/* do the SSL stuff now ... before we play with pty's */
SSL_set_fd(ssl_con,fd);
SSL_set_fd(tls_con,fd);
if (tls_only_flag) {
if (tn_deb || debses)
tn_debug("[TLS - handshake starting]");
else if ( verbosity )
printf("[TLS - handshake starting]\r\n");
debug(F110,"ck_ssl_incoming","[TLS - handshake starting]",0);
/* hmm ... only when running talking to things like
* https servers should we hit this code and then
* we really don't care *who* we talk to :-)
*/
if (SSL_accept(tls_con) <= 0) {
char errbuf[1024];
sprintf(errbuf,"[TLS - SSL_accept error: %s",
ERR_error_string(ERR_get_error(),NULL));
if (tn_deb || debses)
tn_debug(errbuf);
else if ( ssl_debug_flag )
printf("%s\r\n",errbuf);
else if ( verbosity )
printf("[TLS - SSL_accept error]\r\n");
debug(F110,"ck_ssl_incoming",errbuf,0);
return(-1);
} else {
if (tn_deb || debses)
tn_debug("[TLS - OK]");
else if ( verbosity )
printf("[TLS - OK]\r\n");
debug(F110,"ck_ssl_incoming","[TLS - OK]",0);
tls_active_flag = 1;
}
} else if (ssl_only_flag) {
if (tn_deb || debses)
tn_debug("[SSL - handshake starting]");
else if ( verbosity )
printf("[SSL - handshake starting]\r\n");
debug(F110,"ck_ssl_incoming","[SSL - handshake starting]",0);
/* hmm ... only when running talking to things like
* https servers should we hit this code and then
* we really don't care *who* we talk to :-)
*/
if (SSL_accept(ssl_con) <= 0) {
char errbuf[1024];
sprintf(errbuf,"[SSL - SSL_accept error: %s",
ERR_error_string(ERR_get_error(),NULL));
if (tn_deb || debses)
tn_debug(errbuf);
else if ( ssl_debug_flag )
printf("%s\r\n",errbuf);
else if ( verbosity )
printf("[SSL - SSL_accept error]\r\n");
debug(F110,"ck_ssl_incoming",errbuf,0);
return(-1);
} else {
if (tn_deb || debses)
tn_debug("[SSL - OK]");
else if ( verbosity )
printf("[SSL - OK]\r\n");
debug(F110,"ssl_is","[SSL - OK]",0);
ssl_active_flag = 1;
}
}
if (ssl_active_flag || tls_active_flag) {
X509 *peer;
char str[256], *uid=NULL;
/* now check to see that we got exactly what we
* wanted from the caller ... if a certificate is
* required then we make 100% sure that we were
* given on during the handshake (as it is an optional
* part of SSL and TLS)
*/
if ( tls_active_flag ) {
peer=SSL_get_peer_certificate(tls_con);
} else if ( ssl_active_flag ) {
peer=SSL_get_peer_certificate(ssl_con);
}
if (peer == NULL) {
debug(F100,"SSL_get_peer_certificate() == NULL","",0);
auth_finished(AUTH_REJECT);
if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
if (tn_deb || debses)
tn_debug("[SSL/TLS - peer check failed]");
else if (ssl_debug_flag) {
printf("[SSL/TLS - peer check failed]\r\n");
}
debug(F110,
"ck_tn_tls_negotiate",
"[SSL/TLS - peer check failed]",
0
);
/* LOGGING REQUIRED HERE! */
return -1;
}
} else {
debug(F100,"SSL_get_peer_certificate() != NULL","",0);
X509_NAME_get_text_by_NID(X509_get_subject_name(peer),
NID_commonName,str,
256
);
printf("[TLS - commonName=%s]\r\n",str);
X509_NAME_get_text_by_NID(X509_get_subject_name(peer),
#ifndef NID_x500UniqueIdentifier
NID_uniqueIdentifier,
#else
NID_x500UniqueIdentifier,
#endif
str,256
);
printf("[TLS - uniqueIdentifier=%s]\r\n",str);
/* Try to determine user name */
uid = tls_userid_from_client_cert(tls_con);
if ( uid ) {
/* This code is very questionable.
* How should it behave?
* The client has presented a certificate that
* contains a username. We have validated the
* certificate but we do not automatically
* log the user in unless there is a .tlslogin
* file.
*/
ckstrncpy(szUserNameRequested,uid,UIDBUFLEN);
#ifdef CK_LOGIN
if (zvuser(uid))
auth_finished(AUTH_VALID);
else
#endif /* CK_LOGIN */
auth_finished(AUTH_USER);
}
else {
szUserNameRequested[0] = '\0';
auth_finished(AUTH_REJECT);
}
}
}
return(0); /* success */
}
int
ck_ssl_outgoing(fd) int fd;
{
int timo = 2000;
setverbosity();
if ( !ck_ssleay_is_installed() )
return(-1);
/* bind in the network descriptor */
SSL_set_fd(ssl_con,fd);
SSL_set_fd(tls_con,fd);
/* If we are doing raw TLS then start it now ... */
if (tls_only_flag) {
#ifndef USE_CERT_CB
if (!tls_load_certs(tls_ctx,tls_con,0)) {
debug(F110,"ck_ssl_outgoing","tls_load_certs() failed",0);
return(-1);
}
#endif /* USE_CERT_CB */
if (tn_deb || debses)
tn_debug("[TLS - handshake starting]");
else if (verbosity)
printf("[TLS - handshake starting]\r\n");
debug(F110,"ck_ssl_outgoing","[TLS - handshake starting]",0);
if (SSL_connect(tls_con) <= 0) {
char errbuf[1024];
sprintf(errbuf,"[TLS - SSL_connect error: %s",
ERR_error_string(ERR_get_error(),NULL));
if (tn_deb || debses)
tn_debug(errbuf);
else if ( ssl_debug_flag )
printf("%s\r\n",errbuf);
if (tn_deb || debses)
tn_debug("[TLS - FAILED]");
else if ( verbosity )
printf("[TLS - FAILED]\r\n");
debug(F110,"ck_ssl_outgoing","[TLS - FAILED]",0);
netclos();
return(-1);
} else {
tls_active_flag = 1;
if ( !ssl_certsok_flag && (ssl_verify_flag & SSL_VERIFY_PEER) &&
!tls_is_krb5(0) ) {
char *subject = ssl_get_subject_name(tls_con);
if (!subject) {
if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
{
if (tn_deb || debses)
tn_debug("[TLS - FAILED]");
else if ( verbosity )
printf("[TLS - FAILED]\r\n");
debug(F110,"ck_tn_tls_negotiate","[TLS - FAILED]",0);
auth_finished(AUTH_REJECT);
return -1;
} else {
char prmpt[1024];
int ok;
ok = uq_ok("Warning: Server didn't provide a certificate",
"Continue? (Y/N)", 3, NULL, 0);
if (!ok) {
if (tn_deb || debses)
tn_debug("[TLS - FAILED]");
else if ( verbosity )
printf("[TLS - FAILED]\r\n");
debug(F110,
"ck_tn_tls_negotiate","[TLS - FAILED]",0);
auth_finished(AUTH_REJECT);
return -1;
}
}
} else if (ssl_check_server_name(tls_con, szHostName)) {
if (tn_deb || debses)
tn_debug("[TLS - FAILED]");
else if ( verbosity )
printf("[TLS - FAILED]\r\n");
debug(F110,
"ck_tn_tls_negotiate","[TLS - FAILED]",0);
auth_finished(AUTH_REJECT);
return -1;
}
}
if (tn_deb || debses)
tn_debug("[TLS - OK]");
else if (!quiet)
printf("[TLS - OK]\r\n");
debug(F110,"ck_ssl_outgoing","[TLS - OK]",0);
ssl_display_connect_details(tls_con,0,verbosity);
}
}
/* if we are doing raw SSL then start it now ... */
else if (ssl_only_flag) {
#ifndef USE_CERT_CB
if (!tls_load_certs(ssl_ctx,ssl_con,0))
return(-1);
#endif /* USE_CERT_CB */
if (tn_deb || debses)
tn_debug("[SSL - handshake starting]");
else if ( verbosity )
printf("[SSL - handshake starting]\r\n");
debug(F110,"ck_ssl_outgoing","[SSL - handshake starting]",0);
if (SSL_connect(ssl_con) <= 0) {
if ( ssl_debug_flag ) {
char errbuf[1024];
sprintf(errbuf,"[SSL - SSL_connect error: %s",
ERR_error_string(ERR_get_error(),NULL));
printf("%s\r\n",errbuf);
}
if (tn_deb || debses)
tn_debug("[SSL - FAILED]");
else if ( verbosity )
printf("[SSL - FAILED]\r\n");
debug(F110,"ck_ssl_outgoing","[SSL - FAILED]",0);
return(-1);
} else {
ssl_active_flag = 1;
if ( !ssl_certsok_flag && (ssl_verify_flag & SSL_VERIFY_PEER) &&
!tls_is_krb5(0)) {
char *subject = ssl_get_subject_name(ssl_con);
if (!subject) {
if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
{
if (tn_deb || debses)
tn_debug("[SSL - FAILED]");
else if ( verbosity )
printf("[SSL - FAILED]\r\n");
debug(F110,"ck_tn_tls_negotiate","[SSL - FAILED]",0);
auth_finished(AUTH_REJECT);
return -1;
} else {
char prmpt[1024];
int ok;
ok = uq_ok("Warning: Server didn't provide a certificate",
"Continue? (Y/N)", 3, NULL, 0);
if (!ok) {
if (tn_deb || debses)
tn_debug("[SSL - FAILED]");
else if ( verbosity )
printf("[SSL - FAILED]\r\n");
debug(F110,
"ck_tn_tls_negotiate","[SSL - FAILED]",0);
auth_finished(AUTH_REJECT);
return -1;
}
}
} else if (ssl_check_server_name(ssl_con, szHostName)) {
if (tn_deb || debses)
tn_debug("[SSL - FAILED]");
else if ( verbosity )
printf("[SSL - FAILED]\r\n");
debug(F110, "ck_tn_tls_negotiate","[SSL - FAILED]",0);
auth_finished(AUTH_REJECT);
return -1;
}
}
if (tn_deb || debses)
tn_debug("[SSL - OK]");
else if (!quiet)
printf("[SSL - OK]\r\n");
debug(F110,"ck_ssl_outgoing","[SSL - OK]",0);
ssl_display_connect_details(ssl_con,0,verbosity);
}
}
return(0); /* success */
}
#ifndef NOHTTP
int
ck_ssl_http_client(fd, hostname) int fd; char * hostname;
{
int timo = 2000;
if ( !ck_ssleay_is_installed() )
return(-1);
setverbosity();
/* bind in the network descriptor */
SSL_set_fd(tls_http_con,fd);
/* If we are doing raw TLS then start it now ... */
if (1) {
#ifndef USE_CERT_CB
if (!tls_load_certs(tls_http_ctx,tls_http_con,0)) {
debug(F110,"ck_ssl_http_client","tls_load_certs() failed",0);
return(-1);
}
#endif /* USE_CERT_CB */
if (tn_deb || debses)
tn_debug("[TLS - handshake starting]");
else if (verbosity)
printf("[TLS - handshake starting]\r\n");
debug(F110,"ck_ssl_outgoing","[TLS - handshake starting]",0);
if (SSL_connect(tls_http_con) <= 0) {
char errbuf[1024];
sprintf(errbuf,"[TLS - SSL_connect error: %s",
ERR_error_string(ERR_get_error(),NULL));
if (tn_deb || debses)
tn_debug(errbuf);
else if ( ssl_debug_flag )
printf("%s\r\n",errbuf);
if (tn_deb || debses)
tn_debug("[TLS - FAILED]");
else if ( verbosity )
printf("[TLS - FAILED]\r\n");
debug(F110,"ck_ssl_http_client","[TLS - FAILED]",0);
http_close();
return(-1);
} else {
tls_http_active_flag = 1;
if ( !ssl_certsok_flag && (ssl_verify_flag & SSL_VERIFY_PEER) &&
!tls_is_krb5(3) ) {
char *subject = ssl_get_subject_name(tls_http_con);
if (!subject) {
if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
{
if (tn_deb || debses)
tn_debug("[TLS - FAILED]");
else if ( verbosity )
printf("[TLS - FAILED]\r\n");
debug(F110,"ck_tn_tls_negotiate","[TLS - FAILED]",0);
return -1;
} else {
char prmpt[1024];
int ok;
ok = uq_ok("Warning: Server didn't provide a certificate",
"Continue? (Y/N)", 3, NULL, 0);
if (!ok) {
if (tn_deb || debses)
tn_debug("[TLS - FAILED]");
else if ( verbosity )
printf("[TLS - FAILED]\r\n");
debug(F110,
"ck_tn_tls_negotiate","[TLS - FAILED]",0);
return -1;
}
}
} else if (ssl_check_server_name(tls_http_con, hostname)) {
if (tn_deb || debses)
tn_debug("[TLS - FAILED]");
else if ( verbosity )
printf("[TLS - FAILED]\r\n");
debug(F110,
"ck_tn_tls_negotiate","[TLS - FAILED]",0);
return -1;
}
}
printf("[TLS - OK]\r\n");
if (tn_deb || debses)
tn_debug("[TLS - OK]");
debug(F110,"ck_ssl_outgoing","[TLS - OK]",0);
ssl_display_connect_details(tls_http_con,0,verbosity);
}
}
return(0); /* success */
}
#endif /* NOHTTP */
int
ck_ssl_renegotiate_ciphers()
{
if ( !ck_ssleay_is_installed() )
return(0);
if ( !sstelnet )
return(0);
if ( ssl_active_flag )
return SSL_renegotiate(ssl_con);
else if ( tls_active_flag )
return SSL_renegotiate(tls_con);
return(0);
}
#ifdef NT
int
ck_X509_save_cert_to_user_store(X509 *cert)
{
#ifdef X509V3_EXT_DUMP_UNKNOWN
char path[CKMAXPATH];
char hash[16];
char * GetAppData(int);
BIO * out=NULL;
if ( cert == NULL )
return(0);
sprintf(hash,"%08lx",X509_subject_name_hash(cert));
ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/certs/",
hash,".0");
out=BIO_new(BIO_s_file());
if (out == NULL)
{
ERR_print_errors(bio_err);
return(0);
}
if (BIO_write_filename(out,path) <= 0) {
perror(path);
return(0);
}
X509_print_ex(out, cert, XN_FLAG_SEP_MULTILINE, X509V3_EXT_DUMP_UNKNOWN);
if (!PEM_write_bio_X509(out,cert)) {
BIO_printf(bio_err,"unable to write certificate\n");
ERR_print_errors(bio_err);
BIO_free_all(out);
return(0);
}
BIO_free_all(out);
return(1);
#else /* X509V3_EXT_DUMP_UNKNOWN */
return(0);
#endif /* X509V3_EXT_DUMP_UNKNOWN */
}
#endif /* NT */
#ifndef OS2
/* The following function should be replaced by institution specific */
/* code that will convert an X509 cert structure to a userid for the */
/* purposes of client to host login. The example code included */
/* simply returns the UID field of the Subject if it exists. */
/* X509_to_user() returns 0 if valid userid in 'userid', else -1 */
int
X509_to_user(X509 *peer_cert, char *userid, int len)
{
#ifdef X509_UID_TO_USER
/* BEGIN EXAMPLE */
int err;
if (!(peer_cert && userid) || len <= 0)
return -1;
userid[0] = '\0';
debug(F110,"X509_to_user() subject",
X509_NAME_oneline(X509_get_subject_name(peer_cert),NULL,0),0);
/* userid is in cert subject /UID */
err = X509_NAME_get_text_by_NID(X509_get_subject_name(peer_cert),
#ifndef NID_x500UniqueIdentifier
NID_uniqueIdentifier,
#else
NID_x500UniqueIdentifier,
#endif
userid, len);
debug(F111,"X509_to_user() userid",userid,err);
if (err > 0)
return 0;
/* END EXAMPLE */
#else /* X509_UID_TO_USER */
#ifdef X509_SUBJECT_ALT_NAME_TO_USER
/* BEGIN EXAMPLE */
int i;
X509_EXTENSION *ext = NULL;
STACK_OF(GENERAL_NAME) *ialt = NULL;
GENERAL_NAME *gen = NULL;
char email[256];
if (!(peer_cert && userid) || len <= 0)
return -1;
userid[0] = '\0';
email[0] = '\0';
debug(F110,"X509_to_user() subject",
X509_NAME_oneline(X509_get_subject_name(peer_cert),NULL,0),0);
if ((i = X509_get_ext_by_NID(peer_cert, NID_subject_alt_name, -1))<0)
return -1;
if (!(ext = X509_get_ext(peer_cert, i)))
return -1;
X509V3_add_standard_extensions();
if (!(ialt = X509V3_EXT_d2i(ext)))
return -1;
for (i = 0; i < sk_GENERAL_NAME_num(ialt); i++) {
gen = sk_GENERAL_NAME_value(ialt, i);
if (gen->type == GEN_DNS) {
if (!gen->d.ia5 || !gen->d.ia5->length)
break;
if (strlen(gen->d.ia5->data) != gen->d.ia5->length) {
/* Ignoring IA5String containing null character */
continue;
}
if ( gen->d.ia5->length + 1 > sizeof(email) ) {
goto cleanup;
}
memcpy(email, gen->d.ia5->data, gen->d.ia5->length);
email[gen->d.ia5->length] = 0;
break;
}
}
cleanup:
X509V3_EXT_cleanup();
if (ialt)
sk_GENERAL_NAME_free(ialt);
debug(F110,"X509_to_user() email",email,0);
if ( email[0] ) {
char * domain = NULL;
/* Find domain */
for ( i=0 ; email[i] ; i++ ) {
if ( email[i] == '@' ) {
email[i] = '\0';
domain = &email[i+1];
break;
}
}
if ( domain ) {
/* XXX - Put code to Verify domain here */
if ( /* domain is okay */ 1 )
ckstrncpy(userid,email,len);
}
}
return(userid[0] ? 0 : -1);
/* END EXAMPLE */
#endif /* X509_SUBJECT_ALT_NAME_TO_USER */
#endif /* X509_UID_TO_USER */
return -1;
}
/* The following function should be replaced by institution specific */
/* code that will determine whether or not the combination of the */
/* provided X509 certificate and username is valid for automatic */
/* login. Whereas X509_to_user() is used to provide authentication */
/* of the user, the X509_userok() function is used to provide */
/* authorization. The certificate passed into X509_userok() does */
/* need to map to a userid; nor would the userid it would map to */
/* need to match the userid provided to the function. There are */
/* numerous circumstances in which it is beneficial to have the ability */
/* for multiple users to gain access to a common account such as */
/* 'root' on Unix; or a class account on a web server. In Unix we */
/* implement this capability with the ~userid/.tlslogin file which */
/* a list of X509 certificates which may be used to access the */
/* account 'userid'. */
/* X509_to_user() returns 0 if access is denied; 1 is access is permitted */
int
X509_userok(X509 * peer_cert, const char * userid)
{
#ifndef VMS
/* check if clients cert is in "user"'s ~/.tlslogin file */
char buf[512];
int r = 0;
FILE *fp;
struct passwd *pwd;
X509 *file_cert;
if ( peer_cert == NULL )
return(0);
if (!(pwd = getpwnam(userid)))
return 0;
if (strlen(pwd->pw_dir) > 500)
return(0);
sprintf(buf, "%s/.tlslogin", pwd->pw_dir);
if (!(fp = fopen(buf, "r")))
return 0;
while (!r && (file_cert = PEM_read_X509(fp, NULL, NULL, NULL))) {
if (!ASN1_STRING_cmp(peer_cert->signature, file_cert->signature))
r = 1;
X509_free(file_cert);
}
fclose(fp);
return(r);
#else /* VMS */
/* Need to implement an appropriate function for VMS */
return(0);
#endif /* VMS */
}
#endif /* OS2 */
#endif /* CK_SSL */
|