1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825
|
/*
* ProFTPD - mod_proxy TLS implementation
* Copyright (c) 2015-2024 TJ Saunders
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*
* As a special exemption, TJ Saunders and other respective copyright holders
* give permission to link this program with OpenSSL, and distribute the
* resulting executable, without including the source code for OpenSSL in the
* source distribution.
*/
#include "mod_proxy.h"
#include "proxy/conn.h"
#include "proxy/netio.h"
#include "proxy/session.h"
#include "proxy/tls.h"
#include "proxy/tls/db.h"
#include "proxy/tls/redis.h"
/* Define if you have the LibreSSL library. */
#if defined(LIBRESSL_VERSION_NUMBER)
# define HAVE_LIBRESSL 1
#endif
static const char *trace_channel = "proxy.tls";
#if defined(PR_USE_OPENSSL)
extern xaset_t *server_list;
static unsigned long tls_opts = 0UL;
static const char *tls_tables_path = NULL;
static struct proxy_tls_datastore tls_ds;
static int tls_engine = PROXY_TLS_ENGINE_AUTO;
static int tls_need_data_prot = TRUE;
static int tls_verify_server = TRUE;
#if defined(PSK_MAX_PSK_LEN)
static const char *tls_psk_name = NULL;
static BIGNUM *tls_psk_bn = NULL;
static int tls_psk_used = FALSE;
# define PROXY_TLS_MIN_PSK_LEN 20
#endif /* PSK support */
/* The SSL_set_session_ticket_ext() API was not fixed until OpenSSL-1.0.2e.
* Thus mod_proxy's caching/reuse of session tickets will not work properly
* before that version.
*/
#if defined(SSL_CTRL_SET_TLSEXT_TICKET_KEYS)
# define PROXY_TLS_USE_SESSION_TICKETS 1
#endif
static const char *timing_channel = "timing";
#define PROXY_TLS_DEFAULT_CIPHER_SUITE "DEFAULT:!ADH:!EXPORT:!DES"
#define PROXY_TLS_NEXT_PROTO "ftp"
/* SSL record/buffer sizes */
#define PROXY_TLS_HANDSHAKE_WRITE_BUFFER_SIZE 1400
/* SSL adaptive buffer sizes/values */
#define PROXY_TLS_DATA_ADAPTIVE_WRITE_MIN_BUFFER_SIZE (4 * 1024)
#define PROXY_TLS_DATA_ADAPTIVE_WRITE_MAX_BUFFER_SIZE (16 * 1024)
#define PROXY_TLS_DATA_ADAPTIVE_WRITE_BOOST_THRESHOLD (1024 * 1024)
#define PROXY_TLS_DATA_ADAPTIVE_WRITE_BOOST_INTERVAL_MS 1000
#if defined(SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)
static int tls_ssl_opts = (SSL_OP_ALL|SSL_OP_NO_SSLv2|SSL_OP_SINGLE_DH_USE)^SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS;
#else
/* OpenSSL-0.9.6 and earlier (yes, it appears people still have these versions
* installed) does not define the DONT_INSERT_EMPTY_FRAGMENTS option.
*/
static int tls_ssl_opts = SSL_OP_ALL|SSL_OP_NO_SSLv2|SSL_OP_SINGLE_DH_USE;
#endif
static const char *tls_cipher_suite = NULL;
#if OPENSSL_VERSION_NUMBER >= 0x10101000L && \
defined(TLS1_3_VERSION)
static const char *tlsv13_cipher_suite = NULL;
#endif /* TLS1_3_VERSION */
#define PROXY_TLS_VERIFY_DEPTH 9
/* ProxyTLSTimeoutHandshake */
static unsigned int handshake_timeout = 30;
static int handshake_timer_id = -1;
static int handshake_timed_out = FALSE;
#define PROXY_TLS_SHUTDOWN_BIDIRECTIONAL 0x001
/* Stream notes */
#define PROXY_TLS_NETIO_NOTE "mod_proxy.SSL"
#define PROXY_TLS_ADAPTIVE_BYTES_COUNT_KEY "mod_proxy.SSL.adaptive.bytes"
#define PROXY_TLS_ADAPTIVE_BYTES_MS_KEY "mod_proxy.SSL.adaptive.ms"
/* Session caching */
#define PROXY_TLS_MAX_SESSION_AGE 86400
#define PROXY_TLS_MAX_SESSION_COUNT 1000
static SSL_CTX *ssl_ctx = NULL;
static pr_netio_t *tls_ctrl_netio = NULL;
static pr_netio_t *tls_data_netio = NULL;
static SSL *tls_ctrl_ssl = NULL;
static int netio_install_ctrl(void);
static int netio_install_data(void);
/* Indices for data stashed in SSL objects */
#define PROXY_TLS_IDX_TICKET_KEY 2
#define PROXY_TLS_IDX_HAD_TICKET 3
#if !defined(OPENSSL_NO_TLSEXT)
static void tls_tlsext_cb(SSL *, int, int, unsigned char *, int, void *);
#endif /* !OPENSSL_NO_TLSEXT */
static int handshake_timeout_cb(CALLBACK_FRAME) {
handshake_timed_out = TRUE;
return 0;
}
const char *proxy_tls_get_errors(void) {
unsigned int count = 0;
unsigned long error_code;
BIO *bio = NULL;
char *data = NULL;
long datalen;
const char *error_data = NULL, *str = "(unknown)";
int error_flags = 0;
/* Use ERR_print_errors() and a memory BIO to build up a string with
* all of the error messages from the error queue.
*/
error_code = ERR_get_error_line_data(NULL, NULL, &error_data, &error_flags);
if (error_code) {
bio = BIO_new(BIO_s_mem());
}
while (error_code) {
pr_signals_handle();
if (error_flags & ERR_TXT_STRING) {
BIO_printf(bio, "\n (%u) %s [%s]", ++count,
ERR_error_string(error_code, NULL), error_data);
} else {
BIO_printf(bio, "\n (%u) %s", ++count,
ERR_error_string(error_code, NULL));
}
error_data = NULL;
error_flags = 0;
error_code = ERR_get_error_line_data(NULL, NULL, &error_data, &error_flags);
}
datalen = BIO_get_mem_data(bio, &data);
if (data) {
data[datalen] = '\0';
str = pstrdup(session.pool, data);
}
if (bio != NULL) {
BIO_free(bio);
}
return str;
}
static char *tls_x509_name_oneline(X509_NAME *x509_name) {
static char buf[1024] = {'\0'};
/* If we are using OpenSSL 0.9.6 or newer, we want to use
* X509_NAME_print_ex() instead of X509_NAME_oneline().
*/
#if OPENSSL_VERSION_NUMBER < 0x000906000L
memset(&buf, '\0', sizeof(buf));
return X509_NAME_oneline(x509_name, buf, sizeof(buf)-1);
#else
/* Sigh...do it the hard way. */
BIO *mem = BIO_new(BIO_s_mem());
char *data = NULL;
long datalen = 0;
int ok;
ok = X509_NAME_print_ex(mem, x509_name, 0, XN_FLAG_ONELINE);
if (ok) {
datalen = BIO_get_mem_data(mem, &data);
if (data != NULL) {
memset(&buf, '\0', sizeof(buf));
if ((size_t) datalen >= sizeof(buf)) {
datalen = sizeof(buf)-1;
}
memcpy(buf, data, datalen);
buf[datalen] = '\0';
buf[sizeof(buf)-1] = '\0';
BIO_free(mem);
return buf;
}
}
BIO_free(mem);
return NULL;
#endif /* OPENSSL_VERSION_NUMBER >= 0x000906000 */
}
static char *tls_get_subj_name(SSL *ssl) {
X509 *cert;
cert = SSL_get_peer_certificate(ssl);
if (cert != NULL) {
char *subj_name;
subj_name = tls_x509_name_oneline(X509_get_subject_name(cert));
X509_free(cert);
return subj_name;
}
errno = ENOENT;
return NULL;
}
static int tls_get_block(conn_t *conn) {
int flags;
flags = fcntl(conn->rfd, F_GETFL);
if (flags & O_NONBLOCK) {
return FALSE;
}
return TRUE;
}
static void tls_fatal(long error, int lineno) {
switch (error) {
case SSL_ERROR_NONE:
return;
case SSL_ERROR_SSL:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"panic: SSL_ERROR_SSL, line %d: %s", lineno, proxy_tls_get_errors());
break;
case SSL_ERROR_WANT_READ:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"panic: SSL_ERROR_WANT_READ, line %d", lineno);
break;
case SSL_ERROR_WANT_WRITE:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"panic: SSL_ERROR_WANT_WRITE, line %d", lineno);
break;
case SSL_ERROR_WANT_X509_LOOKUP:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"panic: SSL_ERROR_WANT_X509_LOOKUP, line %d", lineno);
break;
case SSL_ERROR_SYSCALL: {
long xerrcode = ERR_get_error();
if (errno == ECONNRESET) {
pr_trace_msg(trace_channel, 17,
"SSL_ERROR_SYSCALL error (errcode %ld) occurred on line %d; ignoring "
"ECONNRESET (%s)", xerrcode, lineno, strerror(errno));
return;
}
/* Check to see if the OpenSSL error queue has info about this. */
if (xerrcode == 0) {
/* The OpenSSL error queue doesn't have any more info, so we'll
* examine the error value itself.
*/
if (errno == EOF) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"panic: SSL_ERROR_SYSCALL, line %d: EOF that violates protocol",
lineno);
} else {
/* Check errno */
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"panic: SSL_ERROR_SYSCALL, line %d: system error: %s", lineno,
strerror(errno));
}
} else {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"panic: SSL_ERROR_SYSCALL, line %d: %s", lineno,
proxy_tls_get_errors());
}
break;
}
case SSL_ERROR_ZERO_RETURN:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"panic: SSL_ERROR_ZERO_RETURN, line %d", lineno);
break;
case SSL_ERROR_WANT_CONNECT:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"panic: SSL_ERROR_WANT_CONNECT, line %d", lineno);
break;
default:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"panic: SSL_ERROR %ld, line %d", error, lineno);
break;
}
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"unexpected OpenSSL error, disconnecting");
pr_log_pri(PR_LOG_WARNING, "%s", MOD_PROXY_VERSION
": unexpected OpenSSL error, disconnecting");
pr_session_disconnect(&proxy_module, PR_SESS_DISCONNECT_BY_APPLICATION, NULL);
}
static void tls_end_sess(SSL *ssl, int strms, int flags) {
int lineno = 0, res = 0;
int shutdown_state;
BIO *rbio, *wbio;
int bread, bwritten;
unsigned long rbio_rbytes, rbio_wbytes, wbio_rbytes, wbio_wbytes;
if (ssl == NULL) {
return;
}
rbio = SSL_get_rbio(ssl);
rbio_rbytes = BIO_number_read(rbio);
rbio_wbytes = BIO_number_written(rbio);
wbio = SSL_get_wbio(ssl);
wbio_rbytes = BIO_number_read(wbio);
wbio_wbytes = BIO_number_written(wbio);
/* A 'close_notify' alert (SSL shutdown message) may have been previously
* sent to the server via netio_shutdown_cb().
*/
shutdown_state = SSL_get_shutdown(ssl);
if (!(shutdown_state & SSL_SENT_SHUTDOWN)) {
errno = 0;
/* 'close_notify' not already sent; send it now. */
pr_trace_msg(trace_channel, 17,
"shutting down TLS session, 'close_notify' not already sent; "
"sending now");
lineno = __LINE__ + 1;
res = SSL_shutdown(ssl);
}
if (res == 0) {
/* Now call SSL_shutdown() again, but only if necessary. */
if (flags & PROXY_TLS_SHUTDOWN_BIDIRECTIONAL) {
shutdown_state = SSL_get_shutdown(ssl);
res = 1;
if (!(shutdown_state & SSL_RECEIVED_SHUTDOWN)) {
errno = 0;
pr_trace_msg(trace_channel, 17,
"shutting down TLS session, 'close_notify' not received; try again");
lineno = __LINE__ + 1;
res = SSL_shutdown(ssl);
}
}
/* If SSL_shutdown() returned -1 here, an error occurred during the
* shutdown.
*/
if (res < 0) {
long err_code;
err_code = SSL_get_error(ssl, res);
switch (err_code) {
case SSL_ERROR_WANT_READ:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"SSL_shutdown error: WANT_READ");
pr_log_debug(DEBUG0, MOD_PROXY_VERSION
": SSL_shutdown error: WANT_READ");
break;
case SSL_ERROR_WANT_WRITE:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"SSL_shutdown error: WANT_WRITE");
pr_log_debug(DEBUG0, MOD_PROXY_VERSION
": SSL_shutdown error: WANT_WRITE");
break;
case SSL_ERROR_ZERO_RETURN:
/* Clean shutdown, nothing we need to do. */
break;
case SSL_ERROR_SYSCALL:
if (errno != 0 &&
errno != EOF &&
errno != EBADF &&
errno != EPIPE &&
errno != EPERM &&
errno != ENOSYS) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"SSL_shutdown syscall error: %s", strerror(errno));
}
break;
default: {
const char *errors;
errors = proxy_tls_get_errors();
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"SSL_shutdown error [%ld]: %s", err_code, errors);
pr_log_debug(DEBUG0, MOD_PROXY_VERSION
": SSL_shutdown error [%ld]: %s", err_code, errors);
break;
}
}
}
} else if (res < 0) {
long err_code;
err_code = SSL_get_error(ssl, res);
switch (err_code) {
case SSL_ERROR_WANT_READ:
case SSL_ERROR_WANT_WRITE:
case SSL_ERROR_ZERO_RETURN:
/* Clean shutdown, nothing we need to do. The WANT_READ/WANT_WRITE
* error codes crept into OpenSSL 0.9.8m, with changes to make
* SSL_shutdown() work properly for non-blocking sockets. And
* handling these error codes for older OpenSSL versions won't break
* things.
*/
break;
case SSL_ERROR_SYSCALL:
if (errno != 0 &&
errno != EOF &&
errno != EBADF &&
errno != EPIPE &&
errno != EPERM &&
errno != ENOSYS) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"SSL_shutdown syscall error: %s", strerror(errno));
}
break;
default:
if (lineno == 0) {
lineno = __LINE__;
}
tls_fatal(err_code, lineno);
break;
}
}
bread = (BIO_number_read(rbio) - rbio_rbytes) +
(BIO_number_read(wbio) - wbio_rbytes);
bwritten = (BIO_number_written(rbio) - rbio_wbytes) +
(BIO_number_written(wbio) - wbio_wbytes);
/* Manually update session.total_raw_in/out, in order to have %I/%O be
* accurately represented for the raw traffic.
*/
if (bread > 0) {
session.total_raw_in += bread;
}
if (bwritten > 0) {
session.total_raw_out += bwritten;
}
SSL_free(ssl);
if (res >= 0) {
pr_trace_msg(trace_channel, 17, "TLS session cleanly shut down");
}
}
static int tls_readmore(int rfd) {
fd_set rfds;
struct timeval tv;
FD_ZERO(&rfds);
FD_SET(rfd, &rfds);
/* Use a timeout of 15 seconds */
tv.tv_sec = 15;
tv.tv_usec = 0;
return select(rfd + 1, &rfds, NULL, NULL, &tv);
}
static int tls_writemore(int wfd) {
fd_set wfds;
struct timeval tv;
FD_ZERO(&wfds);
FD_SET(wfd, &wfds);
/* Use a timeout of 15 seconds */
tv.tv_sec = 15;
tv.tv_usec = 0;
return select(wfd + 1, NULL, &wfds, NULL, &tv);
}
static ssize_t tls_read(SSL *ssl, void *buf, size_t len,
int nstrm_type, pr_table_t *notes) {
int lineno;
ssize_t count;
read_retry:
pr_signals_handle();
lineno = __LINE__ + 1;
count = SSL_read(ssl, buf, len);
if (count < 0) {
long err;
int fd;
err = SSL_get_error(ssl, count);
fd = SSL_get_fd(ssl);
/* read(2) returns only the generic error number -1 */
count = -1;
switch (err) {
case SSL_ERROR_WANT_READ:
/* OpenSSL needs more data from the wire to finish the current block,
* so we wait a little while for it.
*/
pr_trace_msg(trace_channel, 17,
"WANT_READ encountered while reading SSL data on fd %d, "
"waiting to read data", fd);
err = tls_readmore(fd);
if (err > 0) {
goto read_retry;
} else if (err == 0) {
/* Still missing data after timeout. Simulate an EINTR and return.
*/
errno = EINTR;
/* If err < 0, i.e. some error from the select(), everything is
* already in place; errno is properly set and this function
* returns -1.
*/
break;
}
case SSL_ERROR_WANT_WRITE:
/* OpenSSL needs to write more data to the wire to finish the current
* block, so we wait a little while for it.
*/
pr_trace_msg(trace_channel, 17,
"WANT_WRITE encountered while writing SSL data on fd %d, "
"waiting to send data", fd);
err = tls_writemore(fd);
if (err > 0) {
goto read_retry;
} else if (err == 0) {
/* Still missing data after timeout. Simulate an EINTR and return.
*/
errno = EINTR;
/* If err < 0, i.e. some error from the select(), everything is
* already in place; errno is properly set and this function
* returns -1.
*/
break;
}
case SSL_ERROR_ZERO_RETURN:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"read EOF from client during TLS");
break;
default:
tls_fatal(err, lineno);
break;
}
}
return count;
}
static ssize_t tls_write(SSL *ssl, const void *buf, size_t len,
int nstrm_type, pr_table_t *notes) {
ssize_t count;
int lineno, xerrno = 0;
/* Help ensure we have a clean/trustable errno value. */
errno = 0;
lineno = __LINE__ + 1;
count = SSL_write(ssl, buf, len);
xerrno = errno;
if (count < 0) {
long err = SSL_get_error(ssl, count);
/* write(2) returns only the generic error number -1 */
count = -1;
switch (err) {
case SSL_ERROR_WANT_READ:
case SSL_ERROR_WANT_WRITE:
/* Simulate an EINTR in case OpenSSL wants to write more. */
xerrno = EINTR;
break;
default:
tls_fatal(err, lineno);
break;
}
}
if (nstrm_type == PR_NETIO_STRM_DATA) {
BIO *wbio;
uint64_t *adaptive_bytes_written_ms = NULL, now;
off_t *adaptive_bytes_written_count = NULL;
const void *v;
v = pr_table_get(notes, PROXY_TLS_ADAPTIVE_BYTES_COUNT_KEY, NULL);
if (v != NULL) {
adaptive_bytes_written_count = (off_t *) v;
}
v = pr_table_get(notes, PROXY_TLS_ADAPTIVE_BYTES_MS_KEY, NULL);
if (v != NULL) {
adaptive_bytes_written_ms = (uint64_t *) v;
}
(void) pr_gettimeofday_millis(&now);
wbio = SSL_get_wbio(ssl);
if (adaptive_bytes_written_count != NULL) {
(*adaptive_bytes_written_count) += count;
if (*adaptive_bytes_written_count >= PROXY_TLS_DATA_ADAPTIVE_WRITE_BOOST_THRESHOLD) {
/* Boost the buffer size if we've written more than the "boost"
* threshold.
*/
(void) BIO_set_write_buf_size(wbio,
PROXY_TLS_DATA_ADAPTIVE_WRITE_MAX_BUFFER_SIZE);
}
if (now > (*adaptive_bytes_written_ms + PROXY_TLS_DATA_ADAPTIVE_WRITE_BOOST_INTERVAL_MS)) {
/* If it's been longer than the boost interval since our last write,
* then reset the buffer size to the smaller version, assuming
* congestion (and thus closing of the TCP congestion window).
*/
(void) BIO_set_write_buf_size(wbio,
PROXY_TLS_DATA_ADAPTIVE_WRITE_MIN_BUFFER_SIZE);
*adaptive_bytes_written_count = 0;
}
*adaptive_bytes_written_ms = now;
}
}
errno = xerrno;
return count;
}
static const char *get_printable_san(pool *p, const char *data,
size_t datalen) {
register unsigned int i;
char *ptr, *res;
size_t reslen = 0;
/* First, calculate the length of the resulting printable string we'll
* be generating.
*/
for (i = 0; i < datalen; i++) {
if (PR_ISPRINT(data[i])) {
reslen++;
} else {
reslen += 4;
}
}
/* Leave one space in the allocated string for the terminating NUL. */
ptr = res = pcalloc(p, reslen + 1);
for (i = 0; i < datalen; i++) {
if (PR_ISPRINT(data[i])) {
*(ptr++) = data[i];
} else {
snprintf(ptr, reslen - (ptr - res), "\\x%02x", data[i]);
ptr += 4;
}
}
return res;
}
static int cert_match_wildcard(pool *p, const char *host_name,
const char *cert_name, size_t cert_namelen) {
register unsigned int i;
char *ptr, *ptr2;
unsigned int host_label_count = 1, cert_label_count = 1;
int res;
size_t host_namelen;
/* To be a valid wildcard pattern, we need a '*', a '.', and a top-level
* domain. Thus if the length is less than 4 characters, it CANNOT be
* a wildcard match.
*/
if (cert_namelen < 4) {
return FALSE;
}
/* If no '.', FALSE. */
ptr = strchr(cert_name, '.');
if (ptr == NULL) {
return FALSE;
}
/* If no '*', FALSE. */
ptr2 = strchr(cert_name, '*');
if (ptr2 == NULL) {
return FALSE;
}
/* If more than one '*', FALSE. */
if (strchr(ptr2 + 1, '*') != NULL) {
pr_trace_msg(trace_channel, 17,
"multiple '*' characters found in '%s', unable to use for wildcard "
"matching", cert_name);
return FALSE;
}
/* If not in leftmost label, FALSE. */
if (ptr2 > ptr) {
pr_trace_msg(trace_channel, 17,
"wildcard character in '%s' is NOT in the leftmost label", cert_name);
return FALSE;
}
/* If not the same number of labels, FALSE */
host_namelen = strlen(host_name);
for (i = 0; i < host_namelen; i++) {
if (host_name[i] == '.') {
host_label_count++;
}
}
for (i = 0; i < cert_namelen; i++) {
if (cert_name[i] == '.') {
cert_label_count++;
}
}
if (host_label_count != cert_label_count) {
pr_trace_msg(trace_channel, 17,
"cert name '%s' label count (%d) does not match host name '%s' "
"label count (%d)", cert_name,
cert_label_count, host_name, host_label_count);
return FALSE;
}
res = pr_fnmatch(cert_name, host_name, PR_FNM_NOESCAPE);
if (res == 0) {
return TRUE;
}
pr_trace_msg(trace_channel, 17,
"certificate name with wildcard '%s' did not match host name '%s'",
cert_name, host_name);
return FALSE;
}
static int cert_match_dns_san(pool *p, X509 *cert, const char *dns_name,
int allow_wildcards) {
int matched = FALSE;
#if OPENSSL_VERSION_NUMBER > 0x000907000L
STACK_OF(GENERAL_NAME) *sans;
sans = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
if (sans != NULL) {
register int i;
int nsans = sk_GENERAL_NAME_num(sans);
for (i = 0; i < nsans; i++) {
GENERAL_NAME *alt_name = sk_GENERAL_NAME_value(sans, i);
if (alt_name->type == GEN_DNS) {
char *dns_san;
size_t dns_sanlen;
dns_san = (char *) ASN1_STRING_data(alt_name->d.ia5);
dns_sanlen = strlen(dns_san);
/* Check for subjectAltName values which contain embedded NULs.
* This can cause verification problems (spoofing), e.g. if the
* string is "www.goodguy.com\0www.badguy.com"; the use of strcmp()
* only checks "www.goodguy.com".
*/
if ((size_t) ASN1_STRING_length(alt_name->d.ia5) != dns_sanlen) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"cert dNSName SAN contains embedded NULs, "
"rejecting as possible spoof attempt");
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"suspicious dNSName SAN value: '%s'",
get_printable_san(p, dns_san,
ASN1_STRING_length(alt_name->d.dNSName)));
GENERAL_NAME_free(alt_name);
sk_GENERAL_NAME_free(sans);
return 0;
}
if (strncasecmp(dns_name, dns_san, dns_sanlen + 1) == 0) {
pr_trace_msg(trace_channel, 8,
"found cert dNSName SAN matching '%s'", dns_name);
matched = TRUE;
} else {
if (allow_wildcards) {
matched = cert_match_wildcard(p, dns_name, dns_san, dns_sanlen);
}
}
if (matched == FALSE) {
pr_trace_msg(trace_channel, 9,
"cert dNSName SAN '%s' did not match '%s'", dns_san, dns_name);
}
}
GENERAL_NAME_free(alt_name);
if (matched == TRUE) {
break;
}
}
sk_GENERAL_NAME_free(sans);
}
#endif /* OpenSSL-0.9.7 or later */
return matched;
}
static int cert_match_ip_san(pool *p, X509 *cert, const char *ipstr) {
int matched = FALSE;
STACK_OF(GENERAL_NAME) *sans;
sans = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
if (sans != NULL) {
register int i;
int nsans = sk_GENERAL_NAME_num(sans);
for (i = 0; i < nsans; i++) {
GENERAL_NAME *alt_name = sk_GENERAL_NAME_value(sans, i);
if (alt_name->type == GEN_IPADD) {
unsigned char *san_data = NULL;
int have_ipstr = FALSE, san_datalen;
#if defined(PR_USE_IPV6)
char san_ipstr[INET6_ADDRSTRLEN + 1] = {'\0'};
#else
char san_ipstr[INET_ADDRSTRLEN + 1] = {'\0'};
#endif /* PR_USE_IPV6 */
san_data = ASN1_STRING_data(alt_name->d.ip);
memset(san_ipstr, '\0', sizeof(san_ipstr));
san_datalen = ASN1_STRING_length(alt_name->d.ip);
if (san_datalen == 4) {
/* IPv4 address */
snprintf(san_ipstr, sizeof(san_ipstr)-1, "%u.%u.%u.%u",
san_data[0], san_data[1], san_data[2], san_data[3]);
have_ipstr = TRUE;
#if defined(PR_USE_IPV6)
} else if (san_datalen == 16) {
/* IPv6 address */
if (pr_inet_ntop(AF_INET6, san_data, san_ipstr,
sizeof(san_ipstr)-1) == NULL) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"unable to convert cert iPAddress SAN value (length %d) "
"to IPv6 representation: %s", san_datalen, strerror(errno));
} else {
have_ipstr = TRUE;
}
#endif /* PR_USE_IPV6 */
} else {
pr_trace_msg(trace_channel, 3,
"unsupported cert SAN ipAddress length (%d), ignoring",
san_datalen);
continue;
}
if (have_ipstr) {
size_t san_ipstrlen;
san_ipstrlen = strlen(san_ipstr);
if (strncmp(ipstr, san_ipstr, san_ipstrlen + 1) == 0) {
pr_trace_msg(trace_channel, 8,
"found cert iPAddress SAN matching '%s'", ipstr);
matched = TRUE;
} else {
if (san_datalen == 16) {
/* We need to handle the case where the iPAddress SAN might
* have contained an IPv4-mapped IPv6 adress, and we're
* comparing against an IPv4 address.
*/
if (san_ipstrlen > 7 &&
strncasecmp(san_ipstr, "::ffff:", 7) == 0) {
if (strncmp(ipstr, san_ipstr + 7, san_ipstrlen - 6) == 0) {
pr_trace_msg(trace_channel, 8,
"found cert iPAddress SAN '%s' matching '%s'",
san_ipstr, ipstr);
matched = TRUE;
}
}
} else {
pr_trace_msg(trace_channel, 9,
"cert iPAddress SAN '%s' did not match '%s'", san_ipstr, ipstr);
}
}
}
}
GENERAL_NAME_free(alt_name);
if (matched == TRUE) {
break;
}
}
sk_GENERAL_NAME_free(sans);
}
return matched;
}
static int cert_match_cn(pool *p, X509 *cert, const char *name,
int allow_wildcards) {
int matched = FALSE, idx = -1;
X509_NAME *subj_name = NULL;
X509_NAME_ENTRY *cn_entry = NULL;
ASN1_STRING *cn_asn1 = NULL;
char *cn_str = NULL;
size_t cn_len = 0;
/* Find the position of the CommonName (CN) field within the Subject of
* the cert.
*/
subj_name = X509_get_subject_name(cert);
if (subj_name == NULL) {
pr_trace_msg(trace_channel, 12,
"unable to check certificate CommonName against '%s': "
"unable to get Subject", name);
return 0;
}
idx = X509_NAME_get_index_by_NID(subj_name, NID_commonName, -1);
if (idx < 0) {
pr_trace_msg(trace_channel, 12,
"unable to check certificate CommonName against '%s': "
"no CommonName attribute found", name);
return 0;
}
cn_entry = X509_NAME_get_entry(subj_name, idx);
if (cn_entry == NULL) {
pr_trace_msg(trace_channel, 12,
"unable to check certificate CommonName against '%s': "
"error obtaining CommonName attribute found: %s", name,
proxy_tls_get_errors());
return 0;
}
/* Convert the CN field to a string, by way of an ASN1 object. */
cn_asn1 = X509_NAME_ENTRY_get_data(cn_entry);
if (cn_asn1 == NULL) {
pr_trace_msg(trace_channel, 12,
"unable to check certificate CommonName against '%s': "
"error converting CommonName attribute to ASN.1: %s", name,
proxy_tls_get_errors());
return 0;
}
cn_str = (char *) ASN1_STRING_data(cn_asn1);
/* Check for CommonName values which contain embedded NULs. This can cause
* verification problems (spoofing), e.g. if the string is
* "www.goodguy.com\0www.badguy.com"; the use of strcmp() only checks
* "www.goodguy.com".
*/
cn_len = strlen(cn_str);
if ((size_t) ASN1_STRING_length(cn_asn1) != cn_len) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"cert CommonName contains embedded NULs, rejecting as possible spoof "
"attempt");
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"suspicious CommonName value: '%s'",
get_printable_san(p, (const char *) cn_str, ASN1_STRING_length(cn_asn1)));
return 0;
}
/* Yes, this is deliberately a case-insensitive comparison. Most CNs
* contain a hostname (case-insensitive); if they contain an IP address,
* the case-insensitivity won't hurt anything. In fact, it's needed for
* e.g. IPv6 addresses.
*/
if (strncasecmp(name, cn_str, cn_len + 1) == 0) {
pr_trace_msg(trace_channel, 12, "cert CommonName '%s' matches '%s'", cn_str,
name);
matched = TRUE;
} else {
if (allow_wildcards) {
matched = cert_match_wildcard(p, name, cn_str, cn_len);
}
}
if (matched == FALSE) {
pr_trace_msg(trace_channel, 12, "cert CommonName '%s' does NOT match '%s'",
cn_str, name);
}
return matched;
}
static int check_server_cert(SSL *ssl, conn_t *conn, const char *host_name) {
X509 *cert = NULL;
int ok = -1;
long verify_result;
/* Only perform these more stringent checks if asked to verify servers. */
if (tls_verify_server == FALSE) {
return 0;
}
/* Check SSL_get_verify_result. */
verify_result = SSL_get_verify_result(ssl);
if (verify_result != X509_V_OK) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"unable to verify '%s' server certificate: %s",
conn->remote_name, X509_verify_cert_error_string(verify_result));
return -1;
}
cert = SSL_get_peer_certificate(ssl);
if (cert == NULL) {
/* This can be null in the case where some anonymous (insecure)
* cipher suite was used.
*/
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"unable to verify '%s': server did not provide certificate",
conn->remote_name);
# if defined(PSK_MAX_PSK_LEN)
if (tls_psk_used) {
return 0;
}
# endif /* PSK support */
return -1;
}
/* XXX If using OpenSSL-1.0.2/1.1.0, we might be able to use:
* X509_match_host() and X509_match_ip()/X509_match_ip_asc().
*/
ok = cert_match_ip_san(conn->pool, cert,
pr_netaddr_get_ipstr(conn->remote_addr));
if (ok == 0) {
ok = cert_match_cn(conn->pool, cert,
pr_netaddr_get_ipstr(conn->remote_addr), FALSE);
}
if (ok == 0) {
ok = cert_match_dns_san(conn->pool, cert, host_name, TRUE);
if (ok == 0) {
ok = cert_match_cn(conn->pool, cert, host_name, TRUE);
}
}
X509_free(cert);
return ok;
}
static void stash_stream_ssl(pr_netio_stream_t *nstrm, SSL *ssl) {
if (pr_table_add(nstrm->notes,
pstrdup(nstrm->strm_pool, PROXY_TLS_NETIO_NOTE), ssl,
sizeof(SSL *)) < 0) {
if (errno != EEXIST) {
pr_trace_msg(trace_channel, 4,
"error stashing '%s' note on %s %s stream: %s",
PROXY_TLS_NETIO_NOTE,
nstrm->strm_type == PR_NETIO_STRM_CTRL ? "control" : "data",
nstrm->strm_mode == PR_NETIO_IO_RD ? "read" : "write",
strerror(errno));
}
}
}
static int tls_verify_cb(int ok, X509_STORE_CTX *ctx) {
X509 *cert;
cert = X509_STORE_CTX_get_current_cert(ctx);
if (!ok) {
int verify_depth, verify_error;
verify_depth = X509_STORE_CTX_get_error_depth(ctx);
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"error: unable to verify server certificate at depth %d",
verify_depth);
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"error: cert subject: %s",
tls_x509_name_oneline(X509_get_subject_name(cert)));
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"error: cert issuer: %s",
tls_x509_name_oneline(X509_get_issuer_name(cert)));
/* Catch a too long certificate chain here. */
if (verify_depth > PROXY_TLS_VERIFY_DEPTH) {
X509_STORE_CTX_set_error(ctx, X509_V_ERR_CERT_CHAIN_TOO_LONG);
}
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
verify_error = X509_STORE_CTX_get_error(ctx);
#else
verify_error = ctx->error;
#endif /* OpenSSL-1.1.x and later */
switch (verify_error) {
case X509_V_ERR_CERT_CHAIN_TOO_LONG:
case X509_V_ERR_CERT_NOT_YET_VALID:
case X509_V_ERR_CERT_HAS_EXPIRED:
case X509_V_ERR_CERT_REVOKED:
case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
case X509_V_ERR_APPLICATION_VERIFICATION:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"server certificate failed verification: %s",
X509_verify_cert_error_string(verify_error));
ok = 0;
break;
case X509_V_ERR_INVALID_PURPOSE: {
register int i;
int count = X509_PURPOSE_get_count();
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"server certificate failed verification: %s",
X509_verify_cert_error_string(verify_error));
for (i = 0; i < count; i++) {
X509_PURPOSE *purp = X509_PURPOSE_get0(i);
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
" purpose #%d: %s", i+1, X509_PURPOSE_get0_name(purp));
}
ok = 0;
break;
}
default:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"error verifying server certificate: [%d] %s",
verify_error, X509_verify_cert_error_string(verify_error));
ok = 0;
break;
}
if (tls_verify_server == FALSE) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"ProxyTLSVerifyServer off, ignoring failed certificate verification");
ok = 1;
}
} else {
if (tls_opts & PROXY_TLS_OPT_ENABLE_DIAGS) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.verify]: cert subject: %s",
tls_x509_name_oneline(X509_get_subject_name(cert)));
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.verify]: cert issuer: %s",
tls_x509_name_oneline(X509_get_issuer_name(cert)));
}
}
return ok;
}
static int tls_get_cached_sess(pool *p, SSL *ssl, const char *host, int port) {
char port_str[32], *sess_key = NULL;
SSL_SESSION *sess = NULL;
if (tls_opts & PROXY_TLS_OPT_NO_SESSION_CACHE) {
if (tls_opts & PROXY_TLS_OPT_NO_SESSION_TICKETS) {
pr_trace_msg(trace_channel, 19,
"NoSessionCache and NoSessionTickets ProxyTLSOptions in effect, "
"not using cached SSL sessions");
SSL_set_options(ssl, SSL_OP_NO_TICKET);
return 0;
}
}
memset(port_str, '\0', sizeof(port_str));
snprintf(port_str, sizeof(port_str)-1, "%d", port);
sess_key = pstrcat(p, "ftp://", host, ":", port_str, NULL);
pr_trace_msg(trace_channel, 19,
"looking for cached SSL session using key '%s'", sess_key);
sess = (tls_ds.get_sess)(p, tls_ds.dsh, sess_key);
if (sess != NULL) {
long sess_age;
time_t now;
now = time(NULL);
sess_age = now - SSL_SESSION_get_time(sess);
if (sess_age >= PROXY_TLS_MAX_SESSION_AGE) {
pr_trace_msg(trace_channel, 9, "cached SSL session expired, removing");
(void) (tls_ds.remove_sess)(p, tls_ds.dsh, sess_key);
SSL_SESSION_free(sess);
errno = ENOENT;
sess = NULL;
}
}
if (sess == NULL) {
if (errno == ENOENT) {
pr_trace_msg(trace_channel, 19,
"no cached sessions found for key '%s'", sess_key);
} else {
pr_trace_msg(trace_channel, 9,
"error getting cached session using key '%s': %s", sess_key,
strerror(errno));
}
return 0;
}
pr_trace_msg(trace_channel, 12,
"found cached SSL session using key '%s'", sess_key);
SSL_set_session(ssl, sess);
SSL_SESSION_free(sess);
return 0;
}
static int tls_add_cached_sess(pool *p, SSL *ssl, const char *host, int port) {
char port_str[32], *sess_key = NULL;
SSL_SESSION *sess = NULL;
int res, sess_count, xerrno = 0;
time_t now, sess_age;
if (tls_opts & PROXY_TLS_OPT_NO_SESSION_CACHE) {
if (tls_opts & PROXY_TLS_OPT_NO_SESSION_TICKETS) {
pr_trace_msg(trace_channel, 19,
"NoSessionCache and NoSessionTickets ProxyTLSOptions in effect, "
"not caching SSL sessions");
return 0;
}
}
sess_count = (tls_ds.count_sess)(p, tls_ds.dsh);
if (sess_count < 0) {
return -1;
}
if (sess_count >= PROXY_TLS_MAX_SESSION_COUNT) {
pr_trace_msg(trace_channel, 14,
"Maximum number of cached sessions (%d) reached, not caching SSL session",
PROXY_TLS_MAX_SESSION_COUNT);
return 0;
}
sess = SSL_get1_session(ssl);
/* If this session is already past our expiration policy, ignore it. */
now = time(NULL);
sess_age = now - SSL_SESSION_get_time(sess);
if (sess_age >= PROXY_TLS_MAX_SESSION_AGE) {
pr_trace_msg(trace_channel, 9,
"SSL session has already expired, not caching");
SSL_SESSION_free(sess);
return 0;
}
memset(port_str, '\0', sizeof(port_str));
snprintf(port_str, sizeof(port_str)-1, "%d", port);
sess_key = pstrcat(p, "ftp://", host, ":", port_str, NULL);
pr_trace_msg(trace_channel, 19,
"caching SSL session using key '%s'", sess_key);
res = (tls_ds.add_sess)(p, tls_ds.dsh, sess_key, sess);
xerrno = errno;
SSL_SESSION_free(sess);
if (res < 0) {
pr_trace_msg(trace_channel, 9,
"error storing cached SSL session using key '%s': %s", sess_key,
strerror(xerrno));
} else {
pr_trace_msg(trace_channel, 19,
"successfully cached SSL session using key '%s'", sess_key);
}
return 0;
}
static int tls_connect(conn_t *conn, const char *host_name,
pr_netio_stream_t *nstrm) {
int blocking, res = 0, xerrno = 0;
char *subj = NULL;
SSL *ssl = NULL;
BIO *rbio = NULL, *wbio = NULL;
if (ssl_ctx == NULL) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"unable to start session: null SSL_CTX");
errno = EPERM;
return -1;
}
ssl = SSL_new(ssl_ctx);
if (ssl == NULL) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"error: unable to allocate SSL session: %s", proxy_tls_get_errors());
return -2;
}
SSL_set_verify(ssl, SSL_VERIFY_PEER, tls_verify_cb);
/* This works with either rfd or wfd (I hope). */
rbio = BIO_new_socket(conn->rfd, FALSE);
wbio = BIO_new_socket(conn->rfd, FALSE);
SSL_set_bio(ssl, rbio, wbio);
#if !defined(OPENSSL_NO_TLSEXT)
SSL_set_tlsext_debug_callback(ssl, tls_tlsext_cb);
/* We should be a well-behaved TLS client, and NOT send an SNI value
* that is an IP address. Per RFC 6066, literal IPv4/IPv6 addresses are NOT
* permitted in the SNI.
*/
if (pr_netaddr_is_v4(host_name) != TRUE &&
pr_netaddr_is_v6(host_name) != TRUE) {
pr_trace_msg(trace_channel, 9, "sending SNI '%s'", host_name);
SSL_set_tlsext_host_name(ssl, host_name);
} else {
pr_trace_msg(trace_channel, 9, "skipping sending of IP address SNI '%s'",
host_name);
}
# if defined(TLSEXT_STATUSTYPE_ocsp)
pr_trace_msg(trace_channel, 9, "requesting stapled OCSP response");
SSL_set_tlsext_status_type(ssl, TLSEXT_STATUSTYPE_ocsp);
# endif /* OCSP support */
#endif /* OPENSSL_NO_TLSEXT */
if (nstrm->strm_type == PR_NETIO_STRM_DATA) {
/* If we're opening a data connection, reuse the SSL data from the
* session on the control connection (including any session IDs and/or
* tickets).
*/
SSL_copy_session_id(ssl, tls_ctrl_ssl);
} else if (nstrm->strm_type == PR_NETIO_STRM_CTRL) {
tls_get_cached_sess(nstrm->strm_pool, ssl, host_name, conn->remote_port);
# if !defined(PROXY_TLS_USE_SESSION_TICKETS)
SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TICKET);
# endif /* Session ticket support */
}
/* If configured, set a timer for the handshake. */
if (handshake_timeout) {
handshake_timer_id = pr_timer_add(handshake_timeout, -1,
&proxy_module, handshake_timeout_cb, "SSL/TLS handshake");
}
/* Make sure that TCP_NODELAY is enabled for the handshake. */
(void) pr_inet_set_proto_nodelay(conn->pool, conn, 1);
/* Make sure that TCP_CORK (aka TCP_NOPUSH) is DISABLED for the handshake. */
if (pr_inet_set_proto_cork(conn->wfd, 0) < 0) {
pr_trace_msg(trace_channel, 9,
"error disabling TCP_CORK on %s conn: %s",
nstrm->strm_type == PR_NETIO_STRM_CTRL ? "control" : "data",
strerror(errno));
}
connect_retry:
blocking = tls_get_block(conn);
if (blocking) {
/* Put the connection in non-blocking mode for the duration of the
* SSL handshake. This lets us handle EAGAIN/retries better (i.e.
* without spinning in a tight loop and consuming the CPU).
*/
if (pr_inet_set_nonblock(conn->pool, conn) < 0) {
pr_trace_msg(trace_channel, 3,
"error making connection nonblocking: %s", strerror(errno));
}
}
pr_signals_handle();
res = SSL_connect(ssl);
if (res == -1) {
xerrno = errno;
}
if (blocking) {
/* Return the connection to blocking mode. */
if (pr_inet_set_block(conn->pool, conn) < 0) {
pr_trace_msg(trace_channel, 3,
"error making connection blocking: %s", strerror(errno));
}
}
if (res < 1) {
const char *msg = "unable to connect using TLS";
int errcode, shutdown_ssl;
errcode = SSL_get_error(ssl, res);
shutdown_ssl = TRUE;
pr_signals_handle();
if (handshake_timed_out) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"TLS negotiation timed out (%u seconds)", handshake_timeout);
tls_end_sess(ssl, nstrm->strm_type, 0);
return -4;
}
switch (errcode) {
case SSL_ERROR_WANT_READ:
pr_trace_msg(trace_channel, 17,
"WANT_READ encountered while connecting on fd %d, "
"waiting to read data", conn->rfd);
tls_readmore(conn->rfd);
goto connect_retry;
case SSL_ERROR_WANT_WRITE:
pr_trace_msg(trace_channel, 17,
"WANT_WRITE encountered while connecting on fd %d, "
"waiting to read data", conn->rfd);
tls_writemore(conn->rfd);
goto connect_retry;
case SSL_ERROR_ZERO_RETURN:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"%s: TLS connection closed", msg);
break;
case SSL_ERROR_WANT_X509_LOOKUP:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"%s: needs X509 lookup", msg);
break;
case SSL_ERROR_SYSCALL: {
/* Check to see if the OpenSSL error queue has info about this. */
int xerrcode = ERR_get_error();
if (xerrcode == 0) {
/* The OpenSSL error queue doesn't have any more info, so we'll
* examine the SSL_connect() return value itself.
*/
if (res == 0) {
/* EOF */
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"%s: received EOF that violates protocol", msg);
} else if (res == -1) {
/* Check errno */
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"%s: system call error: [%d] %s", msg, xerrno,
strerror(xerrno));
}
} else {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"%s: system call error: %s", msg, proxy_tls_get_errors());
}
shutdown_ssl = FALSE;
break;
}
case SSL_ERROR_SSL:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"%s: protocol error: %s", msg, proxy_tls_get_errors());
shutdown_ssl = FALSE;
break;
}
if (nstrm->strm_type == PR_NETIO_STRM_CTRL) {
pr_event_generate("mod_proxy.tls-ctrl-handshake-failed", &errcode);
} else if (nstrm->strm_type == PR_NETIO_STRM_DATA) {
pr_event_generate("mod_proxy.tls-data-handshake-failed", &errcode);
}
if (shutdown_ssl == TRUE) {
tls_end_sess(ssl, nstrm->strm_type, 0);
}
return -3;
}
/* Disable the handshake timer. */
pr_timer_remove(handshake_timer_id, &proxy_module);
/* Disable TCP_NODELAY, now that the handshake is done. */
(void) pr_inet_set_proto_nodelay(conn->pool, conn, 0);
if (nstrm->strm_type == PR_NETIO_STRM_DATA) {
/* Reenable TCP_CORK (aka TCP_NOPUSH), now that the handshake is done. */
if (pr_inet_set_proto_cork(conn->wfd, 1) < 0) {
pr_trace_msg(trace_channel, 9,
"error re-enabling TCP_CORK on data conn: %s", strerror(errno));
}
} else if (nstrm->strm_type == PR_NETIO_STRM_CTRL) {
int reused;
/* Only try to cache the new SSL session if we actually did create a
* new session. Otherwise, leave the previously cached session as is.
*/
reused = SSL_session_reused(ssl);
if (reused == 0) {
tls_add_cached_sess(nstrm->strm_pool, ssl, host_name, conn->remote_port);
}
}
/* Manually update the raw bytes counters with the network IO from the
* SSL handshake.
*/
session.total_raw_in += (BIO_number_read(rbio) +
BIO_number_read(wbio));
session.total_raw_out += (BIO_number_written(rbio) +
BIO_number_written(wbio));
/* Stash the SSL pointer in BOTH input and output streams for this
* connection.
*/
stash_stream_ssl(conn->instrm, ssl);
stash_stream_ssl(conn->outstrm, ssl);
if (nstrm->strm_type == PR_NETIO_STRM_DATA) {
pr_buffer_t *strm_buf;
/* Clear any data from the NetIO stream buffers which may have been read
* in before the SSL/TLS handshake occurred (Bug#3624).
*/
strm_buf = nstrm->strm_buf;
if (strm_buf != NULL) {
strm_buf->current = NULL;
strm_buf->remaining = strm_buf->buflen;
}
} else {
/* Stash a pointer to the control connection SSL object. */
tls_ctrl_ssl = ssl;
}
subj = tls_get_subj_name(ssl);
if (subj != NULL) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"Server: %s", subj);
}
if (check_server_cert(ssl, conn, host_name) < 0) {
tls_end_sess(ssl, nstrm->strm_type, 0);
return -1;
}
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"%s connection created, using cipher %s (%d bits)",
SSL_get_version(ssl), SSL_get_cipher_name(ssl),
SSL_get_cipher_bits(ssl, NULL));
return 0;
}
static int tls_seed_prng(void) {
char *heapdata, stackdata[1024];
FILE *fp = NULL;
pid_t pid;
struct timeval tv;
#if OPENSSL_VERSION_NUMBER >= 0x00905100L
if (RAND_status() == 1) {
/* PRNG already well-seeded. */
return 0;
}
#endif
pr_log_debug(DEBUG9, MOD_PROXY_VERSION
": PRNG not seeded with enough data, looking for entropy sources");
/* If the device '/dev/urandom' is present, OpenSSL uses it by default.
* Check if it's present, else we have to make random data ourselves.
*/
fp = fopen("/dev/urandom", "r");
if (fp != NULL) {
fclose(fp);
pr_log_debug(DEBUG9, MOD_PROXY_VERSION
": device /dev/urandom is present, assuming OpenSSL will use that "
"for PRNG data");
return 0;
}
/* Not enough entropy; trying providing some. */
gettimeofday(&tv, NULL);
RAND_seed(&(tv.tv_sec), sizeof(tv.tv_sec));
RAND_seed(&(tv.tv_usec), sizeof(tv.tv_usec));
pid = getpid();
RAND_seed(&pid, sizeof(pid_t));
RAND_seed(stackdata, sizeof(stackdata));
heapdata = malloc(sizeof(stackdata));
if (heapdata != NULL) {
RAND_seed(heapdata, sizeof(stackdata));
free(heapdata);
}
#if OPENSSL_VERSION_NUMBER >= 0x00905100L
if (RAND_status() == 0) {
/* PRNG still badly seeded. */
errno = EPERM;
return -1;
}
#endif
return 0;
}
/* NetIO callbacks */
static void netio_abort_cb(pr_netio_stream_t *nstrm) {
nstrm->strm_flags |= PR_NETIO_SESS_ABORT;
}
static int netio_close_cb(pr_netio_stream_t *nstrm) {
int res = 0;
SSL *ssl = NULL;
ssl = (SSL *) pr_table_get(nstrm->notes, PROXY_TLS_NETIO_NOTE, NULL);
if (ssl != NULL) {
if (nstrm->strm_type == PR_NETIO_STRM_CTRL &&
nstrm->strm_mode == PR_NETIO_IO_WR) {
const struct proxy_session *proxy_sess;
const char *host_name;
int remote_port;
proxy_sess = pr_table_get(session.notes, "mod_proxy.proxy-session", NULL);
if (proxy_sess == NULL) {
/* Unlikely to occur. */
pr_trace_msg(trace_channel, 1, "missing proxy session unexpectedly");
errno = EINVAL;
return -1;
}
host_name = proxy_conn_get_host(proxy_sess->dst_pconn);
remote_port = proxy_conn_get_port(proxy_sess->dst_pconn);
/* Cache the SSL session here, as it may have changed (e.g. due to
* renegotiations) during the lifetime of the control connection.
*/
tls_add_cached_sess(nstrm->strm_pool, ssl, host_name, remote_port);
pr_table_remove(nstrm->notes, PROXY_TLS_NETIO_NOTE, NULL);
tls_end_sess(ssl, nstrm->strm_type, 0);
proxy_sess_state &= ~PROXY_SESS_STATE_BACKEND_HAS_CTRL_TLS;
}
if (nstrm->strm_type == PR_NETIO_STRM_DATA &&
nstrm->strm_mode == PR_NETIO_IO_WR) {
pr_table_remove(nstrm->notes, PROXY_TLS_NETIO_NOTE, NULL);
}
}
res = close(nstrm->strm_fd);
nstrm->strm_fd = -1;
return res;
}
static pr_netio_stream_t *netio_open_cb(pr_netio_stream_t *nstrm, int fd,
int mode) {
nstrm->strm_fd = fd;
nstrm->strm_mode = mode;
return nstrm;
}
static int netio_poll_cb(pr_netio_stream_t *nstrm) {
fd_set rfds, wfds;
struct timeval tval;
FD_ZERO(&rfds);
FD_ZERO(&wfds);
if (nstrm->strm_mode == PR_NETIO_IO_RD) {
FD_SET(nstrm->strm_fd, &rfds);
} else {
FD_SET(nstrm->strm_fd, &wfds);
}
tval.tv_sec = (nstrm->strm_flags & PR_NETIO_SESS_INTR) ?
nstrm->strm_interval : 10;
tval.tv_usec = 0;
return select(nstrm->strm_fd + 1, &rfds, &wfds, NULL, &tval);
}
static int netio_postopen_cb(pr_netio_stream_t *nstrm) {
/* If this stream is for writing, and TLS is wanted/required, then perform
* a TLS handshake.
*/
if (tls_engine == PROXY_TLS_ENGINE_OFF) {
return 0;
}
if (nstrm->strm_mode == PR_NETIO_IO_WR) {
const struct proxy_session *proxy_sess;
uint64_t *adaptive_ms = NULL, start_ms;
off_t *adaptive_bytes = NULL;
conn_t *conn = NULL;
const char *host_name;
if (nstrm->strm_type == PR_NETIO_STRM_DATA) {
if (tls_need_data_prot == FALSE) {
pr_trace_msg(trace_channel, 17,
"data connection does not require SSL/TLS, skipping handshake");
return 0;
}
/* This callback may be invoked by `pr_netio_postopen` for a frontend
* data conn, as when we already have a backend data conn using TLS
* already established.
*
* This happens when the frontend is NOT using TLS, and is using passive
* data transfers, but the backed IS using TLS, and is using active
* data transfers.
*
* NOTE: This is still a bug; mod_proxy is calling pr_netio_postopen(),
* which should not be invoking this callback.
*/
if (proxy_sess_state & PROXY_SESS_STATE_BACKEND_HAS_DATA_TLS) {
pr_trace_msg(trace_channel, 27,
"data connection already using SSL/TLS, skipping handshake");
return 0;
}
}
proxy_sess = pr_table_get(session.notes, "mod_proxy.proxy-session", NULL);
if (proxy_sess == NULL) {
/* Unlikely to occur. */
pr_trace_msg(trace_channel, 1, "missing proxy session unexpectedly");
errno = EINVAL;
return -1;
}
/* TODO: Handle SSCN_MODE CLIENT (connect), SERVER (accept) */
pr_gettimeofday_millis(&start_ms);
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"starting TLS negotiation on %s connection",
nstrm->strm_type == PR_NETIO_STRM_CTRL ? "control" : "data");
if (nstrm->strm_type == PR_NETIO_STRM_CTRL) {
conn = proxy_sess->backend_ctrl_conn;
} else {
conn = proxy_sess->backend_data_conn;
}
host_name = proxy_conn_get_host(proxy_sess->dst_pconn);
if (tls_connect(conn, host_name, nstrm) < 0) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"unable to open %s connection to %s: TLS negotiation failed",
nstrm->strm_type == PR_NETIO_STRM_CTRL ? "control" : "data", host_name);
errno = EPERM;
return -1;
}
if (pr_trace_get_level(timing_channel) >= 4) {
unsigned long elapsed_ms;
uint64_t finish_ms;
pr_gettimeofday_millis(&finish_ms);
elapsed_ms = (unsigned long) (finish_ms - start_ms);
pr_trace_msg(timing_channel, 4,
"TLS %s handshake duration: %lu ms",
nstrm->strm_type == PR_NETIO_STRM_CTRL ? "control" : "data",
elapsed_ms);
}
adaptive_ms = pcalloc(nstrm->strm_pool, sizeof(uint64_t));
if (pr_table_add(nstrm->notes, PROXY_TLS_ADAPTIVE_BYTES_MS_KEY,
adaptive_ms, sizeof(uint64_t)) < 0) {
pr_trace_msg(trace_channel, 3,
"error stashing '%s' stream note: %s", PROXY_TLS_ADAPTIVE_BYTES_MS_KEY,
strerror(errno));
}
adaptive_bytes = pcalloc(nstrm->strm_pool, sizeof(off_t));
if (pr_table_add(nstrm->notes, PROXY_TLS_ADAPTIVE_BYTES_COUNT_KEY,
adaptive_bytes, sizeof(off_t)) < 0) {
pr_trace_msg(trace_channel, 3,
"error stashing '%s' stream note: %s",
PROXY_TLS_ADAPTIVE_BYTES_COUNT_KEY, strerror(errno));
}
if (netio_install_data() < 0) {
pr_trace_msg(trace_channel, 1,
"error installing data connection NetIO: %s", strerror(errno));
}
if (nstrm->strm_type == PR_NETIO_STRM_CTRL) {
proxy_sess_state |= PROXY_SESS_STATE_BACKEND_HAS_CTRL_TLS;
} else if (nstrm->strm_type == PR_NETIO_STRM_DATA) {
proxy_sess_state |= PROXY_SESS_STATE_BACKEND_HAS_DATA_TLS;
}
}
return 0;
}
static int netio_read_cb(pr_netio_stream_t *nstrm, char *buf, size_t buflen) {
SSL *ssl = NULL;
ssl = (SSL *) pr_table_get(nstrm->notes, PROXY_TLS_NETIO_NOTE, NULL);
if (ssl != NULL) {
BIO *rbio, *wbio;
int bread = 0, bwritten = 0;
ssize_t res = 0;
unsigned long rbio_rbytes, rbio_wbytes, wbio_rbytes, wbio_wbytes;
rbio = SSL_get_rbio(ssl);
rbio_rbytes = BIO_number_read(rbio);
rbio_wbytes = BIO_number_written(rbio);
wbio = SSL_get_wbio(ssl);
wbio_rbytes = BIO_number_read(wbio);
wbio_wbytes = BIO_number_written(wbio);
res = tls_read(ssl, buf, buflen, nstrm->strm_type, nstrm->notes);
bread = (BIO_number_read(rbio) - rbio_rbytes) +
(BIO_number_read(wbio) - wbio_rbytes);
bwritten = (BIO_number_written(rbio) - rbio_wbytes) +
(BIO_number_written(wbio) - wbio_wbytes);
/* Manually update session.total_raw_in with the difference between
* the raw bytes read in versus the non-SSL bytes read in, in order to
* have %I be accurately represented for the raw traffic.
*/
if (res > 0) {
session.total_raw_in += (bread - res);
}
/* Manually update session.total_raw_out, in order to have %O be
* accurately represented for the raw traffic.
*/
if (bwritten > 0) {
session.total_raw_out += bwritten;
}
return res;
}
return read(nstrm->strm_fd, buf, buflen);
}
static pr_netio_stream_t *netio_reopen_cb(pr_netio_stream_t *nstrm, int fd,
int mode) {
if (nstrm->strm_fd != -1) {
(void) close(nstrm->strm_fd);
}
nstrm->strm_fd = fd;
nstrm->strm_mode = mode;
return nstrm;
}
static int netio_shutdown_cb(pr_netio_stream_t *nstrm, int how) {
if (how == 1 ||
how == 2) {
/* Closing this stream for writing; we need to send the 'close_notify'
* alert first, so that the client knows, at the application layer,
* that the SSL/TLS session is shutting down.
*/
if (nstrm->strm_mode == PR_NETIO_IO_WR &&
(nstrm->strm_type == PR_NETIO_STRM_CTRL ||
nstrm->strm_type == PR_NETIO_STRM_DATA)) {
SSL *ssl;
/* If we do not have TLS on this connection, then don't try to do
* a TLS shutdown.
*/
if (nstrm->strm_type == PR_NETIO_STRM_CTRL) {
if (!(proxy_sess_state & PROXY_SESS_STATE_BACKEND_HAS_CTRL_TLS)) {
return shutdown(nstrm->strm_fd, how);
}
} else if (nstrm->strm_type == PR_NETIO_STRM_DATA) {
if (!(proxy_sess_state & PROXY_SESS_STATE_BACKEND_HAS_DATA_TLS)) {
return shutdown(nstrm->strm_fd, how);
}
}
ssl = (SSL *) pr_table_get(nstrm->notes, PROXY_TLS_NETIO_NOTE, NULL);
if (ssl != NULL) {
BIO *rbio, *wbio;
int bread = 0, bwritten = 0;
unsigned long rbio_rbytes, rbio_wbytes, wbio_rbytes, wbio_wbytes;
rbio = SSL_get_rbio(ssl);
rbio_rbytes = BIO_number_read(rbio);
rbio_wbytes = BIO_number_written(rbio);
wbio = SSL_get_wbio(ssl);
wbio_rbytes = BIO_number_read(wbio);
wbio_wbytes = BIO_number_written(wbio);
if (!(SSL_get_shutdown(ssl) & SSL_SENT_SHUTDOWN)) {
/* We haven't sent a 'close_notify' alert yet; do so now. */
SSL_shutdown(ssl);
}
if (nstrm->strm_type == PR_NETIO_STRM_DATA) {
proxy_sess_state &= ~PROXY_SESS_STATE_BACKEND_HAS_DATA_TLS;
}
bread = (BIO_number_read(rbio) - rbio_rbytes) +
(BIO_number_read(wbio) - wbio_rbytes);
bwritten = (BIO_number_written(rbio) - rbio_wbytes) +
(BIO_number_written(wbio) - wbio_wbytes);
/* Manually update session.total_raw_in/out, in order to have %I/%O be
* accurately represented for the raw traffic.
*/
if (bread > 0) {
session.total_raw_in += bread;
}
if (bwritten > 0) {
session.total_raw_out += bwritten;
}
}
}
}
return shutdown(nstrm->strm_fd, how);
}
static int netio_write_cb(pr_netio_stream_t *nstrm, char *buf, size_t buflen) {
SSL *ssl;
ssl = (SSL *) pr_table_get(nstrm->notes, PROXY_TLS_NETIO_NOTE, NULL);
if (ssl != NULL) {
BIO *rbio, *wbio;
int bread = 0, bwritten = 0;
ssize_t res = 0;
unsigned long rbio_rbytes, rbio_wbytes, wbio_rbytes, wbio_wbytes;
rbio = SSL_get_rbio(ssl);
rbio_rbytes = BIO_number_read(rbio);
rbio_wbytes = BIO_number_written(rbio);
wbio = SSL_get_wbio(ssl);
wbio_rbytes = BIO_number_read(wbio);
wbio_wbytes = BIO_number_written(wbio);
res = tls_write(ssl, buf, buflen, nstrm->strm_type, nstrm->notes);
bread = (BIO_number_read(rbio) - rbio_rbytes) +
(BIO_number_read(wbio) - wbio_rbytes);
bwritten = (BIO_number_written(rbio) - rbio_wbytes) +
(BIO_number_written(wbio) - wbio_wbytes);
/* Manually update session.total_raw_in, in order to have %I be
* accurately represented for the raw traffic.
*/
if (bread > 0) {
session.total_raw_in += bread;
}
/* Manually update session.total_raw_out with the difference between
* the raw bytes written out versus the non-SSL bytes written out,
* in order to have %O be accurately represented for the raw traffic.
*/
if (res > 0) {
session.total_raw_out += (bwritten - res);
}
return res;
}
return write(nstrm->strm_fd, buf, buflen);
}
static int netio_install_ctrl(void) {
pr_netio_t *netio;
if (tls_ctrl_netio != NULL) {
/* If we already have our ctrl netio, then it's been registered, and
* we don't need to do anything more.
*/
return 0;
}
netio = pr_alloc_netio2(permanent_pool, &proxy_module, "proxy.tls");
netio->abort = netio_abort_cb;
netio->close = netio_close_cb;
netio->open = netio_open_cb;
netio->poll = netio_poll_cb;
netio->postopen = netio_postopen_cb;
netio->read = netio_read_cb;
netio->reopen = netio_reopen_cb;
netio->shutdown = netio_shutdown_cb;
netio->write = netio_write_cb;
if (proxy_netio_use(PR_NETIO_STRM_CTRL, netio) < 0) {
return -1;
}
tls_ctrl_netio = netio;
return 0;
}
static int netio_install_data(void) {
pr_netio_t *netio;
if (tls_data_netio != NULL) {
pr_netio_t *using_netio = NULL;
if (proxy_netio_using(PR_NETIO_STRM_DATA, &using_netio) == 0) {
if (using_netio == NULL) {
if (proxy_netio_use(PR_NETIO_STRM_DATA, tls_data_netio) < 0) {
return -1;
}
}
}
return 0;
}
netio = pr_alloc_netio2(permanent_pool, &proxy_module, "proxy.tls");
netio->abort = netio_abort_cb;
netio->close = netio_close_cb;
netio->open = netio_open_cb;
netio->poll = netio_poll_cb;
netio->postopen = netio_postopen_cb;
netio->read = netio_read_cb;
netio->reopen = netio_reopen_cb;
netio->shutdown = netio_shutdown_cb;
netio->write = netio_write_cb;
if (proxy_netio_use(PR_NETIO_STRM_DATA, netio) < 0) {
return -1;
}
tls_data_netio = netio;
return 0;
}
/* Initialization routines */
#if defined(PSK_MAX_PSK_LEN)
static unsigned int tls_psk_cb(SSL *ssl, const char *psk_hint, char *identity,
unsigned int max_identity_len,
unsigned char *psk, unsigned int max_psklen) {
int res, bn_len;
unsigned int psklen;
if (psk_hint != NULL) {
pr_trace_msg(trace_channel, 7, "received PSK identity hint: '%s'",
psk_hint);
} else {
pr_trace_msg(trace_channel, 17, "received no PSK identity hint");
}
res = snprintf(identity, max_identity_len, "%s", tls_psk_name);
if (res < 0 ||
res > (int) max_identity_len) {
pr_trace_msg(trace_channel, 6,
"error setting PSK identity to '%s'", tls_psk_name);
return 0;
}
bn_len = BN_num_bytes(tls_psk_bn);
if (bn_len > (int) max_psklen) {
pr_trace_msg(trace_channel, 6,
"warning: unable to use '%s' PSK: max buffer size (%u bytes) "
"too small for key (%d bytes)", tls_psk_name, max_psklen,
bn_len);
return 0;
}
psklen = BN_bn2bin(tls_psk_bn, psk);
if (psklen == 0) {
pr_trace_msg(trace_channel, 6,
"error converting '%s' PSK to binary: %s", tls_psk_name,
proxy_tls_get_errors());
return 0;
}
if (tls_opts & PROXY_TLS_OPT_ENABLE_DIAGS) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.psk] used PSK identity '%s'", tls_psk_name);
}
tls_psk_used = TRUE;
return psklen;
}
#endif /* PSK support */
#if !defined(OPENSSL_NO_TLSEXT) && defined(TLSEXT_STATUSTYPE_ocsp)
/* TODO: Do more than just log the received stapled OCSP response. */
static int tls_ocsp_response_cb(SSL *ssl, void *user_data) {
BIO *bio = NULL;
char *data = NULL;
long datalen;
const unsigned char *ptr;
int len, res = 1;
bio = BIO_new(BIO_s_mem());
len = SSL_get_tlsext_status_ocsp_resp(ssl, &ptr);
BIO_puts(bio, "OCSP response: ");
if (ptr == NULL) {
BIO_puts(bio, "no response sent\n");
} else {
OCSP_RESPONSE *resp;
resp = d2i_OCSP_RESPONSE(NULL, &ptr, len);
if (resp == NULL) {
BIO_puts(bio, "response parse error\n");
BIO_dump_indent(bio, (char *) ptr, len, 4);
res = 0;
} else {
BIO_puts(bio, "\n======================================\n");
OCSP_RESPONSE_print(bio, resp, 0);
BIO_puts(bio, "======================================\n");
OCSP_RESPONSE_free(resp);
}
}
datalen = BIO_get_mem_data(bio, &data);
if (data) {
data[datalen] = '\0';
}
pr_trace_msg(trace_channel, 1, "%s", "stapled OCSP response:");
pr_trace_msg(trace_channel, 1, "%s", data);
BIO_free(bio);
return res;
}
#endif /* OCSP support */
#if !defined(OPENSSL_NO_TLSEXT)
struct proxy_tls_next_proto {
const char *proto;
unsigned char *encoded_proto;
unsigned int encoded_protolen;
};
#if defined(PR_USE_OPENSSL_NPN)
static int tls_npn_cb(SSL *ssl,
unsigned char **npn_out, unsigned char *npn_outlen,
const unsigned char *npn_in, unsigned int npn_inlen,
void *data) {
struct proxy_tls_next_proto *next_proto;
next_proto = data;
if (pr_trace_get_level(trace_channel) >= 12) {
register unsigned int i;
int res;
pr_trace_msg(trace_channel, 12,
"NPN protocols advertised by server:");
for (i = 0; i < npn_inlen; i++) {
pr_trace_msg(trace_channel, 12,
" %.*s", (int) npn_in[i], (char *) &(npn_in[i+1]));
i += npn_in[i];
}
res = SSL_select_next_proto(npn_out, npn_outlen, npn_in, npn_inlen,
next_proto->encoded_proto, next_proto->encoded_protolen);
if (res != OPENSSL_NPN_NEGOTIATED) {
pr_trace_msg(trace_channel, 12,
"failed to negotiate NPN protocol '%s': %s", PROXY_TLS_NEXT_PROTO,
res == OPENSSL_NPN_UNSUPPORTED ? "NPN unsupported by server" :
"No overlap with server protocols");
}
}
return SSL_TLSEXT_ERR_OK;
}
#endif /* PR_USE_OPENSSL_NPN */
static int set_next_protocol(SSL_CTX *ctx) {
register unsigned int i;
const char *proto = PROXY_TLS_NEXT_PROTO;
struct proxy_tls_next_proto *next_proto;
unsigned char *encoded_proto;
size_t encoded_protolen, proto_len;
proto_len = strlen(proto);
encoded_protolen = proto_len + 1;
encoded_proto = palloc(proxy_pool, encoded_protolen);
encoded_proto[0] = proto_len;
for (i = 0; i < proto_len; i++) {
encoded_proto[i+1] = proto[i];
}
next_proto = palloc(proxy_pool, sizeof(struct proxy_tls_next_proto));
next_proto->proto = pstrdup(proxy_pool, proto);
next_proto->encoded_proto = encoded_proto;
next_proto->encoded_protolen = encoded_protolen;
# if defined(PR_USE_OPENSSL_NPN)
SSL_CTX_set_next_proto_select_cb(ctx, tls_npn_cb, next_proto);
# endif /* NPN */
# if defined(PR_USE_OPENSSL_ALPN)
SSL_CTX_set_alpn_protos(ctx, next_proto->encoded_proto,
next_proto->encoded_protolen);
# endif /* ALPN */
return 0;
}
#endif /* OPENSSL_NO_TLSEXT */
static int init_ssl_ctx(void) {
long ssl_mode = 0;
int ssl_opts = tls_ssl_opts;
if (ssl_ctx != NULL) {
SSL_CTX_free(ssl_ctx);
ssl_ctx = NULL;
}
ssl_ctx = SSL_CTX_new(SSLv23_client_method());
if (ssl_ctx == NULL) {
pr_log_debug(DEBUG0, MOD_PROXY_VERSION
": error creating SSL_CTX: %s", proxy_tls_get_errors());
errno = EPERM;
return -1;
}
/* Note that we explicitly do NOT use OpenSSL's internal cache for
* client session caching; we'll use our own.
*/
SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_OFF);
#if OPENSSL_VERSION_NUMBER > 0x000906000L
/* The SSL_MODE_AUTO_RETRY mode was added in 0.9.6. */
ssl_mode |= SSL_MODE_AUTO_RETRY;
#endif
#if OPENSSL_VERSION_NUMBER >= 0x1000001fL
/* The SSL_MODE_RELEASE_BUFFERS mode was added in 1.0.0a. */
ssl_mode |= SSL_MODE_RELEASE_BUFFERS;
#endif
if (ssl_mode != 0) {
SSL_CTX_set_mode(ssl_ctx, ssl_mode);
}
/* If using OpenSSL-0.9.7 or greater, prevent session resumptions on
* renegotiations (more secure).
*/
#if OPENSSL_VERSION_NUMBER > 0x000907000L
ssl_opts |= SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION;
#endif
/* Disable SSL compression. */
#if defined(SSL_OP_NO_COMPRESSION)
ssl_opts |= SSL_OP_NO_COMPRESSION;
#endif /* SSL_OP_NO_COMPRESSION */
#if defined(PR_USE_OPENSSL_ECC)
# if defined(SSL_OP_SINGLE_ECDH_USE)
ssl_opts |= SSL_OP_SINGLE_ECDH_USE;
# endif
# if defined(SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
ssl_opts |= SSL_OP_SAFARI_ECDHE_ECDSA_BUG;
# endif
#endif /* ECC support */
SSL_CTX_set_options(ssl_ctx, ssl_opts);
#if !defined(OPENSSL_NO_TLSEXT)
if (set_next_protocol(ssl_ctx) < 0) {
pr_trace_msg(trace_channel, 4,
"error setting TLS next protocol: %s", strerror(errno));
}
#endif /* OPENSSL_NO_TLSEXT */
if (tls_seed_prng() < 0) {
pr_log_debug(DEBUG1, MOD_PROXY_VERSION ": unable to properly seed PRNG");
}
return 0;
}
/* Event listeners */
static void proxy_tls_shutdown_ev(const void *event_data, void *user_data) {
RAND_cleanup();
}
#endif /* PR_USE_OPENSSL */
int proxy_tls_set_data_prot(int data_prot) {
int old_data_prot;
#if defined(PR_USE_OPENSSL)
old_data_prot = tls_need_data_prot;
tls_need_data_prot = data_prot;
#else
old_data_prot = FALSE;
#endif /* PR_USE_OPENSSL */
return old_data_prot;
}
int proxy_tls_set_tls(int engine) {
#if defined(PR_USE_OPENSSL)
if (engine != PROXY_TLS_ENGINE_ON &&
engine != PROXY_TLS_ENGINE_OFF &&
engine != PROXY_TLS_ENGINE_AUTO &&
engine != PROXY_TLS_ENGINE_IMPLICIT) {
errno = EINVAL;
return -1;
}
tls_engine = engine;
#endif /* PR_USE_OPENSSL */
return 0;
}
int proxy_tls_using_tls(void) {
#if defined(PR_USE_OPENSSL)
return tls_engine;
#else
return PROXY_TLS_ENGINE_OFF;
#endif /* PR_USE_OPENSSL */
}
int proxy_tls_match_client_tls(void) {
int client_using_tls = FALSE, res = 0;
/* Is the client using TLS right now? */
if (session.rfc2228_mech != NULL &&
strcasecmp(session.rfc2228_mech, "TLS") == 0) {
client_using_tls = TRUE;
}
if (client_using_tls == TRUE) {
int client_using_implicit_ftps = FALSE;
config_rec *c;
unsigned long mod_tls_opts = 0UL;
/* Is the client using implicit FTPS? */
c = find_config(main_server->conf, CONF_PARAM, "TLSOptions", FALSE);
while (c != NULL) {
unsigned long opts;
pr_signals_handle();
opts = *((unsigned long *) c->argv[0]);
mod_tls_opts |= opts;
c = find_config_next(c, c->next, CONF_PARAM, "TLSOptions", FALSE);
}
/* We cheat, and duplicate/check for the UseImplicitTLS TLSOption
* (TLS_OPT_USE_IMPLICIT_SSL) from the mod_tls.c implementation.
*/
if (mod_tls_opts & 0x0200) {
client_using_implicit_ftps = TRUE;
}
if (client_using_implicit_ftps == TRUE) {
pr_trace_msg(trace_channel, 17,
"setting implicit FTPS due to ProxyTLSEngine MatchClient");
res = proxy_tls_set_tls(PROXY_TLS_ENGINE_IMPLICIT);
} else {
/* Using explicit FTPS. */
pr_trace_msg(trace_channel, 17,
"setting explicit FTPS due to ProxyTLSEngine MatchClient");
res = proxy_tls_set_tls(PROXY_TLS_ENGINE_ON);
}
} else {
pr_trace_msg(trace_channel, 17,
"disabling FTPS due to ProxyTLSEngine MatchClient");
res = proxy_tls_set_tls(PROXY_TLS_ENGINE_OFF);
}
return res;
}
int proxy_tls_init(pool *p, const char *tables_path, int flags) {
#if defined(PR_USE_OPENSSL)
int res;
memset(&tls_ds, 0, sizeof(tls_ds));
switch (proxy_datastore) {
case PROXY_DATASTORE_SQLITE:
res = proxy_tls_db_as_datastore(&tls_ds, proxy_datastore_data,
proxy_datastore_datasz);
break;
case PROXY_DATASTORE_REDIS:
res = proxy_tls_redis_as_datastore(&tls_ds, proxy_datastore_data,
proxy_datastore_datasz);
break;
default:
res = -1;
errno = EINVAL;
break;
}
if (res < 0) {
return -1;
}
res = (tls_ds.init)(p, tables_path, flags);
if (res < 0) {
return -1;
}
if (pr_module_exists("mod_sftp.c") == FALSE &&
pr_module_exists("mod_tls.c") == FALSE) {
#if OPENSSL_VERSION_NUMBER < 0x10100000L
OPENSSL_config(NULL);
#endif /* prior to OpenSSL-1.1.x */
SSL_load_error_strings();
SSL_library_init();
ERR_load_crypto_strings();
OpenSSL_add_all_algorithms();
}
res = init_ssl_ctx();
if (res < 0) {
return -1;
}
tls_tables_path = pstrdup(proxy_pool, tables_path);
pr_event_register(&proxy_module, "core.shutdown", proxy_tls_shutdown_ev,
NULL);
#endif /* PR_USE_OPENSSL */
return 0;
}
int proxy_tls_free(pool *p) {
if (p == NULL) {
errno = EINVAL;
return -1;
}
#if defined(PR_USE_OPENSSL)
if (ssl_ctx != NULL) {
SSL_CTX_free(ssl_ctx);
ssl_ctx = NULL;
}
if (tls_ds.dsh != NULL) {
int res;
res = (tls_ds.close)(p, tls_ds.dsh);
if (res < 0) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"error closing datastore: %s", strerror(errno));
}
tls_ds.dsh = NULL;
}
#endif /* PR_USE_OPENSSL */
return 0;
}
#if defined(PR_USE_OPENSSL)
/* Construct the options value that disables all unsupported protocols. */
static int get_disabled_protocols(unsigned int supported_protocols) {
int disabled_protocols;
/* First, create an options value where ALL protocols are disabled. */
disabled_protocols = (SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1);
# if defined(SSL_OP_NO_TLSv1_1)
disabled_protocols |= SSL_OP_NO_TLSv1_1;
# endif
# if defined(SSL_OP_NO_TLSv1_2)
disabled_protocols |= SSL_OP_NO_TLSv1_2;
# endif
# if defined(SSL_OP_NO_TLSv1_3)
disabled_protocols |= SSL_OP_NO_TLSv1_3;
# endif
/* Now, based on the given bitset of supported protocols, clear the
* necessary bits.
*/
if (supported_protocols & PROXY_TLS_PROTO_SSL_V3) {
disabled_protocols &= ~SSL_OP_NO_SSLv3;
}
if (supported_protocols & PROXY_TLS_PROTO_TLS_V1) {
disabled_protocols &= ~SSL_OP_NO_TLSv1;
}
# if OPENSSL_VERSION_NUMBER >= 0x10001000L
if (supported_protocols & PROXY_TLS_PROTO_TLS_V1_1) {
disabled_protocols &= ~SSL_OP_NO_TLSv1_1;
}
if (supported_protocols & PROXY_TLS_PROTO_TLS_V1_2) {
disabled_protocols &= ~SSL_OP_NO_TLSv1_2;
}
# endif /* OpenSSL-1.0.1 or later */
#if defined(SSL_OP_NO_TLSv1_3)
if (supported_protocols & PROXY_TLS_PROTO_TLS_V1_3) {
disabled_protocols &= ~SSL_OP_NO_TLSv1_3;
}
#endif /* OpenSSL 1.1.1 or later */
return disabled_protocols;
}
static const char *get_enabled_protocols_str(pool *p, unsigned int protos,
unsigned int *count) {
char *proto_str = "";
unsigned int nproto = 0;
if (protos & PROXY_TLS_PROTO_SSL_V3) {
proto_str = pstrcat(p, proto_str, *proto_str ? ", " : "",
"SSLv3", NULL);
nproto++;
}
if (protos & PROXY_TLS_PROTO_TLS_V1) {
proto_str = pstrcat(p, proto_str, *proto_str ? ", " : "",
"TLSv1", NULL);
nproto++;
}
if (protos & PROXY_TLS_PROTO_TLS_V1_1) {
proto_str = pstrcat(p, proto_str, *proto_str ? ", " : "",
"TLSv1.1", NULL);
nproto++;
}
if (protos & PROXY_TLS_PROTO_TLS_V1_2) {
proto_str = pstrcat(p, proto_str, *proto_str ? ", " : "",
"TLSv1.2", NULL);
nproto++;
}
*count = nproto;
return proto_str;
}
# if defined(PSK_MAX_PSK_LEN)
static int tls_load_psk(const char *identity, const char *path) {
register int i;
char key_buf[PR_TUNABLE_BUFFER_SIZE];
int fd, key_len, valid_hex = TRUE, res, xerrno;
struct stat st;
BIGNUM *bn = NULL;
PRIVS_ROOT
fd = open(path, O_RDONLY);
xerrno = errno;
PRIVS_RELINQUISH
if (fd < 0) {
pr_trace_msg(trace_channel, 6,
"error opening ProxyTLSPreSharedKey file '%s': %s", path,
strerror(xerrno));
errno = xerrno;
return -1;
}
if (fstat(fd, &st) < 0) {
xerrno = errno;
pr_trace_msg(trace_channel, 6,
"error checking ProxyTLSPreSharedKey file '%s': %s", path,
strerror(xerrno));
(void) close(fd);
errno = xerrno;
return -1;
}
/* Check on the permissions of the file; skip it if the permissions
* are too permissive, e.g. file is world-read/writable.
*/
if (st.st_mode & S_IROTH) {
pr_trace_msg(trace_channel, 6,
"unable to use ProxyTLSPreSharedKey file '%s': "
"file is world-readable", path);
(void) close(fd);
errno = EPERM;
return -1;
}
if (st.st_mode & S_IWOTH) {
pr_trace_msg(trace_channel, 6,
"unable to use ProxyTLSPreSharedKey file '%s': "
"file is world-writable", path);
(void) close(fd);
errno = EPERM;
return -1;
}
if (st.st_size == 0) {
pr_trace_msg(trace_channel, 6,
"unable to use ProxyTLSPreSharedKey file '%s': "
"file is zero length", path);
(void) close(fd);
errno = ENOENT;
return -1;
}
/* Read the entire key into memory. */
memset(key_buf, '\0', sizeof(key_buf));
key_len = read(fd, key_buf, sizeof(key_buf)-1);
xerrno = errno;
(void) close(fd);
if (key_len < 0) {
pr_trace_msg(trace_channel, 6,
": error reading ProxyTLSPreSharedKey file '%s': %s", path,
strerror(xerrno));
errno = xerrno;
return -1;
}
if (key_len < PROXY_TLS_MIN_PSK_LEN) {
pr_trace_msg(trace_channel, 6,
"read %d bytes from ProxyTLSPreSharedKey file '%s', need at least %d "
"bytes of key data, ignoring", key_len, path, PROXY_TLS_MIN_PSK_LEN);
errno = ENOENT;
return -1;
}
key_buf[key_len] = '\0';
key_buf[sizeof(key_buf)-1] = '\0';
/* Ignore any trailing newlines. */
if (key_buf[key_len-1] == '\n') {
key_buf[key_len-1] = '\0';
key_len--;
}
if (key_buf[key_len-1] == '\r') {
key_buf[key_len-1] = '\0';
key_len--;
}
/* Ensure that it is all hex encoded data */
for (i = 0; i < key_len; i++) {
if (isxdigit((int) key_buf[i]) == 0) {
valid_hex = FALSE;
break;
}
}
if (valid_hex == FALSE) {
pr_trace_msg(trace_channel, 6,
"unable to use '%s' data from ProxyTLSPreSharedKey file '%s': "
"not a hex number", key_buf, path);
errno = EINVAL;
return -1;
}
res = BN_hex2bn(&bn, key_buf);
if (res == 0) {
pr_trace_msg(trace_channel, 6,
"failed to convert '%s' data from ProxyTLSPreSharedKey file '%s' "
"to BIGNUM: %s", key_buf, path, proxy_tls_get_errors());
if (bn != NULL) {
BN_free(bn);
}
errno = EINVAL;
return -1;
}
tls_psk_name = identity;
tls_psk_bn = bn;
return 0;
}
# endif /* PSK support */
static void tls_info_cb(const SSL *ssl, int where, int ret) {
const char *str = "(unknown)";
int w;
pr_signals_handle();
w = where & ~SSL_ST_MASK;
if (w & SSL_ST_CONNECT) {
str = "connecting";
} else if (w & SSL_ST_ACCEPT) {
str = "accepting";
} else {
int ssl_state;
ssl_state = SSL_get_state(ssl);
switch (ssl_state) {
# if defined(SSL_ST_BEFORE)
case SSL_ST_BEFORE:
str = "before";
break;
# endif
#if OPENSSL_VERSION_NUMBER >= 0x10100000L && \
!defined(HAVE_LIBRESSL)
case TLS_ST_OK:
#else
case SSL_ST_OK:
#endif /* OpenSSL-1.1.x and later */
str = "ok";
break;
# if defined(SSL_ST_RENEGOTIATE)
case SSL_ST_RENEGOTIATE:
str = "renegotiating";
break;
# endif
default:
break;
}
}
if (where & SSL_CB_CONNECT_LOOP) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.info] %s: %s", str, SSL_state_string_long(ssl));
} else if (where & SSL_CB_HANDSHAKE_START) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.info] %s: %s", str, SSL_state_string_long(ssl));
} else if (where & SSL_CB_HANDSHAKE_DONE) {
if (pr_trace_get_level(trace_channel) >= 9) {
int reused;
reused = SSL_session_reused((SSL *) ssl);
if (reused > 0) {
pr_trace_msg(trace_channel, 9,
"RESUMED SSL/TLS session: %s using cipher %s (%d bits)",
SSL_get_version(ssl), SSL_get_cipher_name(ssl),
SSL_get_cipher_bits(ssl, NULL));
} else {
pr_trace_msg(trace_channel, 9,
"negotiated NEW SSL/TLS session");
}
}
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.info] %s: %s", str, SSL_state_string_long(ssl));
} else if (where & SSL_CB_LOOP) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.info] %s: %s", str, SSL_state_string_long(ssl));
} else if (where & SSL_CB_ALERT) {
str = (where & SSL_CB_READ) ? "reading" : "writing";
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.info] %s: SSL/TLS alert %s: %s", str,
SSL_alert_type_string_long(ret), SSL_alert_desc_string_long(ret));
} else if (where & SSL_CB_EXIT) {
if (ret == 0) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.info] %s: failed in %s: %s", str, SSL_state_string_long(ssl),
proxy_tls_get_errors());
} else if (ret < 0 &&
errno != 0 &&
errno != EAGAIN) {
/* Ignore EAGAIN errors */
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.info] %s: error in %s (errno %d: %s)", str,
SSL_state_string_long(ssl), errno, strerror(errno));
}
}
}
# if OPENSSL_VERSION_NUMBER > 0x000907000L
/* Borrowed from mod_tls. Would be nice to share this code somehow. */
struct tls_label {
int labelno;
const char *label_name;
};
/* SSL record types */
static struct tls_label tls_record_type_labels[] = {
{ 20, "ChangeCipherSpec" },
{ 21, "Alert" },
{ 22, "Handshake" },
{ 23, "ApplicationData" },
{ 0, NULL }
};
/* SSL versions */
static struct tls_label tls_version_labels[] = {
{ 0x0002, "SSL 2.0" },
{ 0x0300, "SSL 3.0" },
{ 0x0301, "TLS 1.0" },
{ 0x0302, "TLS 1.1" },
{ 0x0303, "TLS 1.2" },
{ 0x0304, "TLS 1.3" },
{ 0, NULL }
};
/* Cipher suites. These values come from:
* http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-4
*/
static struct tls_label tls_ciphersuite_labels[] = {
{ 0x0000, "SSL_NULL_WITH_NULL_NULL" },
{ 0x0001, "SSL_RSA_WITH_NULL_MD5" },
{ 0x0002, "SSL_RSA_WITH_NULL_SHA" },
{ 0x0003, "SSL_RSA_EXPORT_WITH_RC4_40_MD5" },
{ 0x0004, "SSL_RSA_WITH_RC4_128_MD5" },
{ 0x0005, "SSL_RSA_WITH_RC4_128_SHA" },
{ 0x0006, "SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5" },
{ 0x0007, "SSL_RSA_WITH_IDEA_CBC_SHA" },
{ 0x0008, "SSL_RSA_EXPORT_WITH_DES40_CBC_SHA" },
{ 0x0009, "SSL_RSA_WITH_DES_CBC_SHA" },
{ 0x000A, "SSL_RSA_WITH_3DES_EDE_CBC_SHA" },
{ 0x000B, "SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA" },
{ 0x000C, "SSL_DH_DSS_WITH_DES_CBC_SHA" },
{ 0x000D, "SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA" },
{ 0x000E, "SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA" },
{ 0x000F, "SSL_DH_RSA_WITH_DES_CBC_SHA" },
{ 0x0010, "SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA" },
{ 0x0011, "SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA" },
{ 0x0012, "SSL_DHE_DSS_WITH_DES_CBC_SHA" },
{ 0x0013, "SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA" },
{ 0x0014, "SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA" },
{ 0x0015, "SSL_DHE_RSA_WITH_DES_CBC_SHA" },
{ 0x0016, "SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA" },
{ 0x0017, "SSL_DH_anon_EXPORT_WITH_RC4_40_MD5" },
{ 0x0018, "SSL_DH_anon_WITH_RC4_128_MD5" },
{ 0x0019, "SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA" },
{ 0x001A, "SSL_DH_anon_WITH_DES_CBC_SHA" },
{ 0x001B, "SSL_DH_anon_WITH_3DES_EDE_CBC_SHA" },
{ 0x001D, "SSL_FORTEZZA_KEA_WITH_FORTEZZA_CBC_SHA" },
{ 0x001E, "SSL_FORTEZZA_KEA_WITH_RC4_128_SHA" },
{ 0x001F, "TLS_KRB5_WITH_3DES_EDE_CBC_SHA" },
{ 0x0020, "TLS_KRB5_WITH_RC4_128_SHA" },
{ 0x0021, "TLS_KRB5_WITH_IDEA_CBC_SHA" },
{ 0x0022, "TLS_KRB5_WITH_DES_CBC_MD5" },
{ 0x0023, "TLS_KRB5_WITH_3DES_EDE_CBC_MD5" },
{ 0x0024, "TLS_KRB5_WITH_RC4_128_MD5" },
{ 0x0025, "TLS_KRB5_WITH_IDEA_CBC_MD5" },
{ 0x0026, "TLS_KRB5_EXPORT_WITH_DES_CBC_40_SHA" },
{ 0x0027, "TLS_KRB5_EXPORT_WITH_RC2_CBC_40_SHA" },
{ 0x0028, "TLS_KRB5_EXPORT_WITH_RC4_40_SHA" },
{ 0x0029, "TLS_KRB5_EXPORT_WITH_DES_CBC_40_MD5" },
{ 0x002A, "TLS_KRB5_EXPORT_WITH_RC2_CBC_40_MD5" },
{ 0x002B, "TLS_KRB5_EXPORT_WITH_RC4_40_MD5" },
{ 0x002F, "TLS_RSA_WITH_AES_128_CBC_SHA" },
{ 0x0030, "TLS_DH_DSS_WITH_AES_128_CBC_SHA" },
{ 0x0031, "TLS_DH_RSA_WITH_AES_128_CBC_SHA" },
{ 0x0032, "TLS_DHE_DSS_WITH_AES_128_CBC_SHA" },
{ 0x0033, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA" },
{ 0x0034, "TLS_DH_anon_WITH_AES_128_CBC_SHA" },
{ 0x0035, "TLS_RSA_WITH_AES_256_CBC_SHA" },
{ 0x0036, "TLS_DH_DSS_WITH_AES_256_CBC_SHA" },
{ 0x0037, "TLS_DH_RSA_WITH_AES_256_CBC_SHA" },
{ 0x0038, "TLS_DHE_DSS_WITH_AES_256_CBC_SHA" },
{ 0x0039, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA" },
{ 0x003A, "TLS_DH_anon_WITH_AES_256_CBC_SHA" },
{ 0x003B, "TLS_RSA_WITH_NULL_SHA256" },
{ 0x003C, "TLS_RSA_WITH_AES_128_CBC_SHA256" },
{ 0x003D, "TLS_RSA_WITH_AES_256_CBC_SHA256" },
{ 0x003E, "TLS_DH_DSS_WITH_AES_128_CBC_SHA256" },
{ 0x003F, "TLS_DH_RSA_WITH_AES_128_CBC_SHA256" },
{ 0x0040, "TLS_DHE_DSS_WITH_AES_128_CBC_SHA256" },
{ 0x0041, "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA" },
{ 0x0042, "TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA" },
{ 0x0043, "TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA" },
{ 0x0044, "TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA" },
{ 0x0045, "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA" },
{ 0x0046, "TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA" },
{ 0x0067, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256" },
{ 0x0068, "TLS_DH_DSS_WITH_AES_256_CBC_SHA256" },
{ 0x0069, "TLS_DH_RSA_WITH_AES_256_CBC_SHA256" },
{ 0x006A, "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256" },
{ 0x006B, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256" },
{ 0x006C, "TLS_DH_anon_WITH_AES_128_CBC_SHA256" },
{ 0x006D, "TLS_DH_anon_WITH_AES_256_CBC_SHA256" },
{ 0x0084, "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA" },
{ 0x0085, "TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA" },
{ 0x0086, "TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA" },
{ 0x0087, "TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA" },
{ 0x0088, "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA" },
{ 0x0089, "TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA" },
{ 0x008A, "TLS_PSK_WITH_RC4_128_SHA" },
{ 0x008B, "TLS_PSK_WITH_3DES_EDE_CBC_SHA" },
{ 0x008C, "TLS_PSK_WITH_AES_128_CBC_SHA" },
{ 0x008D, "TLS_PSK_WITH_AES_256_CBC_SHA" },
{ 0x008E, "TLS_DHE_PSK_WITH_RC4_128_SHA" },
{ 0x008F, "TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA" },
{ 0x0090, "TLS_DHE_PSK_WITH_AES_128_CBC_SHA" },
{ 0x0091, "TLS_DHE_PSK_WITH_AES_256_CBC_SHA" },
{ 0x0092, "TLS_RSA_PSK_WITH_RC4_128_SHA" },
{ 0x0093, "TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA" },
{ 0x0094, "TLS_RSA_PSK_WITH_AES_128_CBC_SHA" },
{ 0x0095, "TLS_RSA_PSK_WITH_AES_256_CBC_SHA" },
{ 0x0096, "TLS_RSA_WITH_SEED_CBC_SHA" },
{ 0x0097, "TLS_DH_DSS_WITH_SEED_CBC_SHA" },
{ 0x0098, "TLS_DH_RSA_WITH_SEED_CBC_SHA" },
{ 0x0099, "TLS_DHE_DSS_WITH_SEED_CBC_SHA" },
{ 0x009A, "TLS_DHE_RSA_WITH_SEED_CBC_SHA" },
{ 0x009B, "TLS_DH_anon_WITH_SEED_CBC_SHA" },
{ 0x009C, "TLS_RSA_WITH_AES_128_GCM_SHA256" },
{ 0x009D, "TLS_RSA_WITH_AES_256_GCM_SHA384" },
{ 0x009E, "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256" },
{ 0x009F, "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384" },
{ 0x00A0, "TLS_DH_RSA_WITH_AES_128_GCM_SHA256" },
{ 0x00A1, "TLS_DH_RSA_WITH_AES_256_GCM_SHA384" },
{ 0x00A2, "TLS_DHE_DSS_WITH_AES_128_GCM_SHA256" },
{ 0x00A3, "TLS_DHE_DSS_WITH_AES_256_GCM_SHA384" },
{ 0x00A4, "TLS_DH_DSS_WITH_AES_128_GCM_SHA256" },
{ 0x00A5, "TLS_DH_DSS_WITH_AES_256_GCM_SHA384" },
{ 0x00A6, "TLS_DH_anon_WITH_AES_128_GCM_SHA256" },
{ 0x00A7, "TLS_DH_anon_WITH_AES_256_GCM_SHA384" },
{ 0x00A8, "TLS_PSK_WITH_AES_128_GCM_SHA256" },
{ 0x00A9, "TLS_PSK_WITH_AES_256_GCM_SHA384" },
{ 0x00AA, "TLS_DHE_PSK_WITH_AES_128_GCM_SHA256" },
{ 0x00AB, "TLS_DHE_PSK_WITH_AES_256_GCM_SHA384" },
{ 0x00AC, "TLS_RSA_PSK_WITH_AES_128_GCM_SHA256" },
{ 0x00AD, "TLS_RSA_PSK_WITH_AES_256_GCM_SHA384" },
{ 0x00AE, "TLS_PSK_WITH_AES_128_CBC_SHA256" },
{ 0x00AF, "TLS_PSK_WITH_AES_256_CBC_SHA384" },
{ 0x00B0, "TLS_PSK_WITH_NULL_SHA256" },
{ 0x00B1, "TLS_PSK_WITH_NULL_SHA384" },
{ 0x00B2, "TLS_DHE_PSK_WITH_AES_128_CBC_SHA256" },
{ 0x00B3, "TLS_DHE_PSK_WITH_AES_256_CBC_SHA384"},
{ 0x00B4, "TLS_DHE_PSK_WITH_NULL_SHA256" },
{ 0x00B5, "TLS_DHE_PSK_WITH_NULL_SHA384" },
{ 0x00B6, "TLS_RSA_PSK_WITH_AES_128_CBC_SHA256" },
{ 0x00B7, "TLS_RSA_PSK_WITH_AES_256_CBC_SHA384" },
{ 0x00B8, "TLS_RSA_PSK_WITH_NULL_SHA256" },
{ 0x00B9, "TLS_RSA_PSK_WITH_NULL_SHA384" },
{ 0x00BA, "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256" },
{ 0x00BB, "TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA256" },
{ 0x00BC, "TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA256" },
{ 0x00BD, "TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA256" },
{ 0x00BE, "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256" },
{ 0x00BF, "TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA256" },
{ 0x00C0, "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256" },
{ 0x00C1, "TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA256" },
{ 0x00C2, "TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA256" },
{ 0x00C3, "TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256" },
{ 0x00C4, "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256" },
{ 0x00C5, "TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA256" },
{ 0x00FF, "TLS_EMPTY_RENEGOTIATION_INFO_SCSV" },
{ 0x1301, "TLS_AES_128_GCM_SHA256" },
{ 0x1302, "TLS_AES_256_GCM_SHA384" },
{ 0x1303, "TLS_CHACHA20_POLY1305_SHA256" },
{ 0xC001, "TLS_ECDH_ECDSA_WITH_NULL_SHA" },
{ 0xC002, "TLS_ECDH_ECDSA_WITH_RC4_128_SHA" },
{ 0xC003, "TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA" },
{ 0xC004, "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA" },
{ 0xC005, "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA" },
{ 0xC006, "TLS_ECDHE_ECDSA_WITH_NULL_SHA" },
{ 0xC007, "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA" },
{ 0xC008, "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA" },
{ 0xC009, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA" },
{ 0xC00A, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA" },
{ 0xC00B, "TLS_ECDH_RSA_WITH_NULL_SHA" },
{ 0xC00C, "TLS_ECDH_RSA_WITH_RC4_128_SHA" },
{ 0xC00D, "TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA" },
{ 0xC00E, "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA" },
{ 0xC00F, "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA" },
{ 0xC010, "TLS_ECDHE_RSA_WITH_NULL_SHA" },
{ 0xC011, "TLS_ECDHE_RSA_WITH_RC4_128_SHA" },
{ 0xC012, "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA" },
{ 0xC013, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA" },
{ 0xC014, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA" },
{ 0xC015, "TLS_ECDH_anon_WITH_NULL_SHA" },
{ 0xC016, "TLS_ECDH_anon_WITH_RC4_128_SHA" },
{ 0xC017, "TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA" },
{ 0xC018, "TLS_ECDH_anon_WITH_AES_128_CBC_SHA" },
{ 0xC019, "TLS_ECDH_anon_WITH_AES_256_CBC_SHA" },
{ 0xC01A, "TLS_SRP_SHA_WITH_3DES_EDE_CBC_SHA" },
{ 0xC01B, "TLS_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA" },
{ 0xC01C, "TLS_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA" },
{ 0xC01D, "TLS_SRP_SHA_WITH_AES_128_CBC_SHA" },
{ 0xC01E, "TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA" },
{ 0xC01F, "TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA" },
{ 0xC020, "TLS_SRP_SHA_WITH_AES_256_CBC_SHA" },
{ 0xC021, "TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA" },
{ 0xC022, "TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA" },
{ 0xC023, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256" },
{ 0xC024, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384" },
{ 0xC025, "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256" },
{ 0xC026, "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384" },
{ 0xC027, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256" },
{ 0xC028, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384" },
{ 0xC029, "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256" },
{ 0xC02A, "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384" },
{ 0xC02B, "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" },
{ 0xC02C, "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384" },
{ 0xC02D, "TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256" },
{ 0xC02E, "TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384" },
{ 0xC02F, "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256" },
{ 0xC030, "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384" },
{ 0xC031, "TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256" },
{ 0xC032, "TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384" },
{ 0xC07A, "TLS_RSA_WITH_CAMELLIA_128_GCM_SHA256" },
{ 0xC07B, "TLS_RSA_WITH_CAMELLIA_256_GCM_SHA384" },
{ 0xC086, "TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_GCM_SHA256" },
{ 0xC087, "TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_GCM_SHA384" },
{ 0xC08A, "TLS_ECDHE_RSA_WITH_CAMELLIA_128_GCM_SHA256" },
{ 0xC08B, "TLS_ECDHE_RSA_WITH_CAMELLIA_256_GCM_SHA384" },
{ 0xC09C, "TLS_RSA_WITH_AES_128_CCM" },
{ 0xC09D, "TLS_RSA_WITH_AES_256_CCM" },
{ 0xC0AC, "TLS_ECDHE_ECDSA_WITH_AES_128_CCM" },
{ 0xC0AD, "TLS_ECDHE_ECDSA_WITH_AES_256_CCM" },
{ 0xC0AE, "TLS_DHE_RSA_WITH_AES_128_CCM" },
{ 0xC0AF, "TLS_DHE_RSA_WITH_AES_256_CCM" },
{ 0xCCA8, "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256" },
{ 0xCCA9, "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256" },
{ 0xCCAA, "TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256" },
{ 0xCCAB, "TLS_PSK_WITH_CHACHA20_POLY1305_SHA256" },
{ 0xCCAC, "TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256" },
{ 0xCCAD, "TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256" },
{ 0xCCAE, "TLS_RSA_PSK_WITH_CHACHA20_POLY1305_SHA256" },
{ 0xFEFE, "SSL_RSA_FIPS_WITH_DES_CBC_SHA" },
{ 0xFEFF, "SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA" },
{ 0, NULL }
};
/* Compressions */
static struct tls_label tls_compression_labels[] = {
{ 0x0000, "None" },
{ 0x0001, "Zlib" },
{ 0, NULL }
};
/* Extensions */
static struct tls_label tls_extension_labels[] = {
{ 0, "server_name" },
{ 1, "max_fragment_length" },
{ 2, "client_certificate_url" },
{ 3, "trusted_ca_keys" },
{ 4, "truncated_hmac" },
{ 5, "status_request" },
{ 6, "user_mapping" },
{ 7, "client_authz" },
{ 8, "server_authz" },
{ 9, "cert_type" },
{ 10, "elliptic_curves" },
{ 11, "ec_point_formats" },
{ 12, "srp" },
{ 13, "signature_algorithms" },
{ 14, "use_srtp" },
{ 15, "heartbeat" },
{ 16, "application_layer_protocol_negotiation" },
{ 18, "signed_certificate_timestamp" },
{ 21, "padding" },
{ 22, "encrypt_then_mac" },
{ 23, "extended_master_secret" },
{ 35, "session_ticket" },
{ 41, "psk" },
{ 42, "early_data" },
{ 43, "supported_versions" },
{ 44, "cookie" },
{ 45, "psk_kex_modes" },
{ 47, "certificate_authorities" },
{ 49, "post_handshake_auth" },
{ 50, "signature_algorithms_cert" },
{ 51, "key_share" },
{ 0xFF01, "renegotiate" },
{ 13172, "next_proto_neg" },
{ 0, NULL }
};
# if defined(TLSEXT_TYPE_signature_algorithms)
/* Signature Algorithms. These values come from:
* https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-16
*/
static struct tls_label tls_sigalgo_labels[] = {
{ 0x0201, "rsa_pkcs1_sha1" },
{ 0x0202, "dsa_sha1" },
{ 0x0203, "ecdsa_sha1" },
{ 0x0301, "rsa_pkcs1_sha224" },
{ 0x0302, "dsa_sha224" },
{ 0x0303, "ecdsa_sha224" },
{ 0x0401, "rsa_pkcs1_sha256" },
{ 0x0402, "dsa_sha256" },
{ 0x0403, "ecdsa_secp256r1_sha256" },
{ 0x0501, "rsa_pkcs1_sha384" },
{ 0x0502, "dsa_sha384" },
{ 0x0503, "ecdsa_secp384r1_sha384" },
{ 0x0601, "rsa_pkcs1_sha512" },
{ 0x0602, "dsa_sha512" },
{ 0x0603, "ecdsa_secp521r1_sha512" },
{ 0x0804, "rsa_pss_rsae_sha256" },
{ 0x0805, "rsa_pss_rsae_sha384" },
{ 0x0806, "rsa_pss_rsae_sha512" },
{ 0x0807, "ed25519" },
{ 0x0808, "ed448" },
{ 0x0809, "rsa_pss_pss_sha256" },
{ 0x080A, "rsa_pss_pss_sha384" },
{ 0x080B, "rsa_pss_pss_sha512" },
{ 0, NULL }
};
# endif /* TLSEXT_TYPE_signature_algorithms */
# if defined(TLSEXT_TYPE_psk_kex_modes)
/* PSK KEX modes. These values come from:
* https://tools.ietf.org/html/rfc8446#section-4.2.9
*/
static struct tls_label tls_psk_kex_labels[] = {
{ 0, "psk_ke" },
{ 1, "psk_dhe_key" },
{ 0, NULL }
};
# endif /* TLSEXT_TYPE_psk_kex_modes */
static const char *tls_get_label(int labelno, struct tls_label *labels) {
register unsigned int i;
for (i = 0; labels[i].label_name != NULL; i++) {
if (labels[i].labelno == labelno) {
return labels[i].label_name;
}
}
return "[unknown/unsupported]";
}
static void tls_print_ssl_version(BIO *bio, const char *name,
const unsigned char **msg, size_t *msglen, int *pversion) {
int version;
if (*msglen < 2) {
return;
}
version = ((*msg)[0] << 8) | (*msg)[1];
BIO_printf(bio, " %s = %s\n", name,
tls_get_label(version, tls_version_labels));
*msg += 2;
*msglen -= 2;
if (pversion != NULL) {
*pversion = version;
}
}
static void tls_print_hex(BIO *bio, const char *indent, const char *name,
const unsigned char *msg, size_t msglen) {
BIO_printf(bio, "%s (%lu %s)\n", name, (unsigned long) msglen,
msglen != 1 ? "bytes" : "byte");
if (msglen > 0) {
register unsigned int i;
BIO_puts(bio, indent);
for (i = 0; i < msglen; i++) {
BIO_printf(bio, "%02x", msg[i]);
}
BIO_puts(bio, "\n");
}
}
static void tls_print_hexbuf(BIO *bio, const char *indent, const char *name,
size_t namelen, const unsigned char **msg, size_t *msglen) {
size_t buflen;
const unsigned char *ptr;
if (*msglen < namelen) {
return;
}
ptr = *msg;
buflen = ptr[0];
if (namelen > 1) {
buflen = (buflen << 8) | ptr[1];
}
if (*msglen < namelen + buflen) {
return;
}
ptr += namelen;
tls_print_hex(bio, indent, name, ptr, buflen);
*msg += (buflen + namelen);
*msglen -= (buflen + namelen);
}
static void tls_print_random(BIO *bio, const unsigned char **msg,
size_t *msglen) {
time_t ts;
const unsigned char *ptr;
if (*msglen < 32) {
return;
}
ptr = *msg;
ts = ((ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3]);
ptr += 4;
BIO_puts(bio, " random:\n");
BIO_printf(bio, " gmt_unix_time = %s (not guaranteed to be accurate)\n",
pr_strtime2(ts, TRUE));
tls_print_hex(bio, " ", " random_bytes", ptr, 28);
*msg += 32;
*msglen -= 32;
}
static void tls_print_session_id(BIO *bio, const unsigned char **msg,
size_t *msglen) {
tls_print_hexbuf(bio, " ", " session_id", 1, msg, msglen);
}
static void tls_print_ciphersuites(BIO *bio, const char *name,
const unsigned char **msg, size_t *msglen) {
size_t len;
len = ((*msg[0]) << 8) | (*msg)[1];
*msg += 2;
*msglen -= 2;
BIO_printf(bio, " %s (%lu %s)\n", name, (unsigned long) len,
len != 1 ? "bytes" : "byte");
if (*msglen < len ||
(len & 1)) {
return;
}
while (len > 0) {
unsigned int suiteno;
pr_signals_handle();
suiteno = ((*msg[0]) << 8) | (*msg)[1];
BIO_printf(bio, " %s (0x%x)\n",
tls_get_label(suiteno, tls_ciphersuite_labels), suiteno);
*msg += 2;
*msglen -= 2;
len -= 2;
}
}
static void tls_print_compressions(BIO *bio, const char *name,
const unsigned char **msg, size_t *msglen) {
size_t len;
len = (*msg)[0];
*msg += 1;
*msglen -= 1;
if (*msglen < len) {
return;
}
BIO_printf(bio, " %s (%lu %s)\n", name, (unsigned long) len,
len != 1 ? "bytes" : "byte");
while (len > 0) {
int comp_type;
pr_signals_handle();
comp_type = (*msg)[0];
BIO_printf(bio, " %s\n",
tls_get_label(comp_type, tls_compression_labels));
*msg += 1;
*msglen -= 1;
len -= 1;
}
}
static void tls_print_extension(BIO *bio, const char *indent, int server,
int ext_type, const unsigned char *ext, size_t extlen) {
BIO_printf(bio, "%sextension_type = %s (%lu %s)\n", indent,
tls_get_label(ext_type, tls_extension_labels), (unsigned long) extlen,
extlen != 1 ? "bytes" : "byte");
}
static void tls_print_extensions(BIO *bio, const char *name, int server,
const unsigned char **msg, size_t *msglen) {
size_t len;
if (*msglen == 0) {
BIO_printf(bio, "%s: None\n", name);
return;
}
len = ((*msg)[0] << 8) | (*msg)[1];
if (len != (*msglen - 2)) {
return;
}
*msg += 2;
*msglen -= 2;
BIO_printf(bio, " %s (%lu %s)\n", name, (unsigned long) len,
len != 1 ? "bytes" : "byte");
while (len > 0) {
int ext_type;
size_t ext_len;
pr_signals_handle();
if (*msglen < 4) {
break;
}
ext_type = ((*msg)[0] << 8) | (*msg)[1];
ext_len = ((*msg)[2] << 8) | (*msg)[3];
if (*msglen < (ext_len + 4)) {
break;
}
*msg += 4;
tls_print_extension(bio, " ", server, ext_type, *msg, ext_len);
*msg += ext_len;
*msglen -= (ext_len + 4);
}
}
static void tls_print_client_hello(int io_flag, int version, int content_type,
const unsigned char *buf, size_t buflen, SSL *ssl, void *arg) {
BIO *bio;
char *data = NULL;
long datalen;
bio = BIO_new(BIO_s_mem());
BIO_puts(bio, "\nClientHello:\n");
tls_print_ssl_version(bio, "client_version", &buf, &buflen, NULL);
tls_print_random(bio, &buf, &buflen);
tls_print_session_id(bio, &buf, &buflen);
if (buflen < 2) {
BIO_free(bio);
return;
}
tls_print_ciphersuites(bio, "cipher_suites", &buf, &buflen);
if (buflen < 1) {
BIO_free(bio);
return;
}
tls_print_compressions(bio, "compression_methods", &buf, &buflen);
tls_print_extensions(bio, "extensions", FALSE, &buf, &buflen);
datalen = BIO_get_mem_data(bio, &data);
if (data != NULL) {
data[datalen] = '\0';
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION, "[tls.msg] %.*s",
(int) datalen, data);
}
BIO_free(bio);
}
static void tls_print_server_hello(int io_flag, int version, int content_type,
const unsigned char *buf, size_t buflen, SSL *ssl, void *arg) {
BIO *bio;
char *data = NULL;
long datalen;
int print_session_id = TRUE, print_compressions = TRUE, server_version;
unsigned int suiteno;
bio = BIO_new(BIO_s_mem());
BIO_puts(bio, "\nServerHello:\n");
tls_print_ssl_version(bio, "server_version", &buf, &buflen, &server_version);
#if defined(TLS1_3_VERSION)
if (server_version == TLS1_3_VERSION) {
print_session_id = FALSE;
print_compressions = FALSE;
}
#endif /* TLS1_3_VERSION */
tls_print_random(bio, &buf, &buflen);
if (print_session_id == TRUE) {
tls_print_session_id(bio, &buf, &buflen);
}
if (buflen < 2) {
BIO_free(bio);
return;
}
/* Print the selected ciphersuite. */
BIO_printf(bio, " cipher_suites (2 bytes)\n");
suiteno = (buf[0] << 8) | buf[1];
BIO_printf(bio, " %s (0x%x)\n",
tls_get_label(suiteno, tls_ciphersuite_labels), suiteno);
buf += 2;
buflen -= 2;
if (print_compressions == TRUE) {
int comp_type;
if (buflen < 1) {
BIO_free(bio);
return;
}
/* Print the selected compression. */
BIO_printf(bio, " compression_methods (1 byte)\n");
comp_type = *buf;
BIO_printf(bio, " %s\n",
tls_get_label(comp_type, tls_compression_labels));
buf += 1;
buflen -= 1;
}
tls_print_extensions(bio, "extensions", TRUE, &buf, &buflen);
datalen = BIO_get_mem_data(bio, &data);
if (data != NULL) {
data[datalen] = '\0';
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION, "[tls.msg] %.*s",
(int) datalen, data);
}
BIO_free(bio);
}
# if defined(SSL3_MT_NEWSESSION_TICKET)
static void tls_print_ticket(int io_flag, int version, int content_type,
const unsigned char *buf, size_t buflen, SSL *ssl, void *arg) {
BIO *bio;
char *data = NULL;
long datalen;
bio = BIO_new(BIO_s_mem());
BIO_puts(bio, "\nNewSessionTicket:\n");
if (buflen != 0) {
unsigned int ticket_lifetime;
int print_ticket_age = FALSE, print_extensions = FALSE;
ticket_lifetime = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
buf += 4;
buflen -= 4;
BIO_printf(bio, " ticket_lifetime_hint\n %u (sec)\n", ticket_lifetime);
# if defined(TLS1_3_VERSION)
if (SSL_version(ssl) == TLS1_3_VERSION) {
print_ticket_age = TRUE;
print_extensions = TRUE;
}
# endif /* TLS1_3_VERSION */
if (print_ticket_age == TRUE) {
unsigned int ticket_age_add;
ticket_age_add = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
buf += 4;
buflen -= 4;
BIO_printf(bio, " ticket_age_add\n %u (sec)\n", ticket_age_add);
tls_print_hexbuf(bio, " ", " ticket_nonce", 1, &buf, &buflen);
}
tls_print_hexbuf(bio, " ", " ticket", 2, &buf, &buflen);
if (print_extensions == TRUE) {
tls_print_extensions(bio, "extensions", TRUE, &buf, &buflen);
}
} else {
BIO_puts(bio, " <no ticket>\n");
}
datalen = BIO_get_mem_data(bio, &data);
if (data != NULL) {
data[datalen] = '\0';
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION, "[tls.msg] %.*s",
(int) datalen, data);
}
BIO_free(bio);
}
# endif /* SSL3_MT_NEWSESSION_TICKET */
#if !defined(OPENSSL_NO_TLSEXT)
static void tls_tlsext_cb(SSL *ssl, int server, int type,
unsigned char *tlsext_data, int tlsext_datalen, void *user_data) {
char *extension_name = "(unknown)";
int print_basic_info = TRUE;
/* Note: OpenSSL does not implement all possible extensions. For the
* "(unknown)" extensions, see:
*
* http://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#tls-extensiontype-values-1
*/
switch (type) {
# if defined(TLSEXT_TYPE_server_name)
case TLSEXT_TYPE_server_name: {
BIO *bio = NULL;
char *ext_info = "";
long ext_infolen = 0;
extension_name = "server name";
if (pr_trace_get_level(trace_channel) >= 21 &&
tlsext_datalen >= 2) {
/* We read 2 bytes for the extension length, 1 byte for the
* name type, and 2 bytes for the name length. For the SNI
* format specification, see:
* https://tools.ietf.org/html/rfc6066#section-3
*/
int ext_len;
ext_len = (tlsext_data[0] << 8) | tlsext_data[1];
if (tlsext_datalen == ext_len + 2 &&
(ext_len & 1) == 0) {
int name_type;
tlsext_data += 2;
name_type = tlsext_data[0];
tlsext_data += 1;
if (name_type == TLSEXT_NAMETYPE_host_name) {
size_t name_len;
name_len = (tlsext_data[0] << 8) | tlsext_data[1];
tlsext_data += 2;
bio = BIO_new(BIO_s_mem());
BIO_printf(bio, "\n %.*s (%lu)", (int) name_len, tlsext_data,
(unsigned long) name_len);
ext_infolen = BIO_get_mem_data(bio, &ext_info);
if (ext_info != NULL) {
ext_info[ext_infolen] = '\0';
}
}
}
}
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.tlsext] TLS %s extension \"%s\" (ID %d, %d %s)%.*s",
server ? "server" : "client", extension_name, type, tlsext_datalen,
tlsext_datalen != 1 ? "bytes" : "byte", (int) ext_infolen,
ext_info != NULL ? ext_info : "");
if (bio != NULL) {
BIO_free(bio);
}
print_basic_info = FALSE;
break;
}
# endif /* TLSEXT_TYPE_server_name */
# if defined(TLSEXT_TYPE_max_fragment_length)
case TLSEXT_TYPE_max_fragment_length:
extension_name = "max fragment length";
break;
# endif /* TLSEXT_TYPE_max_fragment_length */
# if defined(TLSEXT_TYPE_client_certificate_url)
case TLSEXT_TYPE_client_certificate_url:
extension_name = "client certificate URL";
break;
# endif /* TLSEXT_TYPE_client_certificate_url */
# if defined(TLSEXT_TYPE_trusted_ca_keys)
case TLSEXT_TYPE_trusted_ca_keys:
extension_name = "trusted CA keys";
break;
# endif /* TLSEXT_TYPE_trusted_ca_keys */
# if defined(TLSEXT_TYPE_truncated_hmac)
case TLSEXT_TYPE_truncated_hmac:
extension_name = "truncated HMAC";
break;
# endif /* TLSEXT_TYPE_truncated_hmac */
# if defined(TLSEXT_TYPE_status_request)
case TLSEXT_TYPE_status_request:
extension_name = "status request";
break;
# endif /* TLSEXT_TYPE_status_request */
# if defined(TLSEXT_TYPE_user_mapping)
case TLSEXT_TYPE_user_mapping:
extension_name = "user mapping";
break;
# endif /* TLSEXT_TYPE_user_mapping */
# if defined(TLSEXT_TYPE_client_authz)
case TLSEXT_TYPE_client_authz:
extension_name = "client authz";
break;
# endif /* TLSEXT_TYPE_client_authz */
# if defined(TLSEXT_TYPE_server_authz)
case TLSEXT_TYPE_server_authz:
extension_name = "server authz";
break;
# endif /* TLSEXT_TYPE_server_authz */
# if defined(TLSEXT_TYPE_cert_type)
case TLSEXT_TYPE_cert_type:
extension_name = "cert type";
break;
# endif /* TLSEXT_TYPE_cert_type */
# if defined(TLSEXT_TYPE_elliptic_curves)
case TLSEXT_TYPE_elliptic_curves:
extension_name = "elliptic curves";
break;
# endif /* TLSEXT_TYPE_elliptic_curves */
# if defined(TLSEXT_TYPE_ec_point_formats)
case TLSEXT_TYPE_ec_point_formats:
extension_name = "EC point formats";
break;
# endif /* TLSEXT_TYPE_ec_point_formats */
# if defined(TLSEXT_TYPE_srp)
case TLSEXT_TYPE_srp:
extension_name = "SRP";
break;
# endif /* TLSEXT_TYPE_srp */
# if defined(TLSEXT_TYPE_signature_algorithms)
case TLSEXT_TYPE_signature_algorithms: {
BIO *bio = NULL;
char *ext_info = "";
long ext_infolen = 0;
extension_name = "signature algorithms";
if (pr_trace_get_level(trace_channel) >= 21 &&
tlsext_datalen >= 2) {
int len;
len = (tlsext_data[0] << 8) | tlsext_data[1];
if (tlsext_datalen == len + 2 &&
(len & 1) == 0) {
tlsext_data += 2;
bio = BIO_new(BIO_s_mem());
BIO_puts(bio, "\n");
while (len > 0) {
unsigned int sig_algo;
pr_signals_handle();
sig_algo = (tlsext_data[0] << 8) | tlsext_data[1];
BIO_printf(bio, " %s (0x%x)\n",
tls_get_label(sig_algo, tls_sigalgo_labels), sig_algo);
len -= 2;
tlsext_data += 2;
}
ext_infolen = BIO_get_mem_data(bio, &ext_info);
if (ext_info != NULL) {
ext_info[ext_infolen] = '\0';
}
}
}
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.tlsext] TLS %s extension \"%s\" (ID %d, %d %s)%.*s",
server ? "server" : "client", extension_name, type, tlsext_datalen,
tlsext_datalen != 1 ? "bytes" : "byte", (int) ext_infolen,
ext_info != NULL ? ext_info : "");
if (bio != NULL) {
BIO_free(bio);
}
print_basic_info = FALSE;
break;
}
# endif /* TLSEXT_TYPE_signature_algorithms */
# if defined(TLSEXT_TYPE_use_srtp)
case TLSEXT_TYPE_use_srtp:
extension_name = "use SRTP";
break;
# endif /* TLSEXT_TYPE_use_srtp */
# if defined(TLSEXT_TYPE_heartbeat)
case TLSEXT_TYPE_heartbeat:
extension_name = "heartbeat";
break;
# endif /* TLSEXT_TYPE_heartbeat */
# if defined(TLSEXT_TYPE_signed_certificate_timestamp)
case TLSEXT_TYPE_signed_certificate_timestamp:
extension_name = "signed certificate timestamp";
break;
# endif /* TLSEXT_TYPE_signed_certificate_timestamp */
# if defined(TLSEXT_TYPE_encrypt_then_mac)
case TLSEXT_TYPE_encrypt_then_mac:
extension_name = "encrypt then mac";
break;
# endif /* TLSEXT_TYPE_encrypt_then_mac */
# if defined(TLSEXT_TYPE_extended_master_secret)
case TLSEXT_TYPE_extended_master_secret:
extension_name = "extended master secret";
break;
# endif /* TLSEXT_TYPE_extended_master_secret */
# if defined(TLSEXT_TYPE_session_ticket)
case TLSEXT_TYPE_session_ticket:
extension_name = "session ticket";
break;
# endif /* TLSEXT_TYPE_session_ticket */
# if defined(TLSEXT_TYPE_psk)
case TLSEXT_TYPE_psk:
extension_name = "PSK";
break;
# endif /* TLSEXT_TYPE_psk */
# if defined(TLSEXT_TYPE_supported_versions)
case TLSEXT_TYPE_supported_versions: {
BIO *bio = NULL;
char *ext_info = NULL;
long ext_infolen = 0;
/* If we are the server responding, we only indicate the selected
* protocol version. Otherwise, we are a client indicating the range
* of versions supported.
*/
extension_name = "supported versions";
if (pr_trace_get_level(trace_channel) >= 21) {
bio = BIO_new(BIO_s_mem());
if (server) {
if (tlsext_datalen == 2) {
int version;
version = (tlsext_data[0] << 8) | tlsext_data[1];
BIO_printf(bio, "\n %s (0x%x)\n",
tls_get_label(version, tls_version_labels), version);
}
} else {
if (tlsext_datalen >= 1) {
int len;
len = tlsext_data[0];
if (tlsext_datalen == len + 1) {
tlsext_data += 1;
BIO_puts(bio, "\n");
while (len > 0) {
int version;
pr_signals_handle();
version = (tlsext_data[0] << 8) | tlsext_data[1];
BIO_printf(bio, " %s (0x%x)\n",
tls_get_label(version, tls_version_labels), version);
len -= 2;
tlsext_data += 2;
}
}
}
}
ext_infolen = BIO_get_mem_data(bio, &ext_info);
if (ext_info != NULL) {
ext_info[ext_infolen] = '\0';
}
}
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.tlsext] TLS %s extension \"%s\" (ID %d, %d %s)%.*s",
server ? "server" : "client", extension_name, type, tlsext_datalen,
tlsext_datalen != 1 ? "bytes" : "byte", (int) ext_infolen,
ext_info != NULL ? ext_info : "");
if (bio != NULL) {
BIO_free(bio);
}
print_basic_info = FALSE;
break;
}
# endif /* TLSEXT_TYPE_supported_versions */
# if defined(TLSEXT_TYPE_psk_kex_modes)
case TLSEXT_TYPE_psk_kex_modes: {
BIO *bio = NULL;
char *ext_info = NULL;
long ext_infolen = 0;
extension_name = "PSK KEX modes";
if (pr_trace_get_level(trace_channel) >= 19) {
if (tlsext_datalen >= 1) {
int len;
bio = BIO_new(BIO_s_mem());
len = tlsext_data[0];
if (tlsext_datalen == len + 1) {
tlsext_data += 1;
BIO_puts(bio, "\n");
while (len > 0) {
int kex_mode;
pr_signals_handle();
kex_mode = tlsext_data[0];
BIO_printf(bio, " %s (%d)\n",
tls_get_label(kex_mode, tls_psk_kex_labels), kex_mode);
len -= 1;
tlsext_data += 1;
}
}
}
ext_infolen = BIO_get_mem_data(bio, &ext_info);
if (ext_info != NULL) {
ext_info[ext_infolen] = '\0';
}
}
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.tlsext] TLS %s extension \"%s\" (ID %d, %d %s)%.*s",
server ? "server" : "client", extension_name, type, tlsext_datalen,
tlsext_datalen != 1 ? "bytes" : "byte", (int) ext_infolen,
ext_info != NULL ? ext_info : "");
if (bio != NULL) {
BIO_free(bio);
}
print_basic_info = FALSE;
break;
}
# endif /* TLSEXT_TYPE_psk_kex_modes */
# if defined(TLSEXT_TYPE_renegotiate)
case TLSEXT_TYPE_renegotiate:
extension_name = "renegotiation info";
break;
# endif /* TLSEXT_TYPE_renegotiate */
# if defined(TLSEXT_TYPE_opaque_prf_input)
case TLSEXT_TYPE_opaque_prf_input:
extension_name = "opaque PRF input";
break;
# endif /* TLSEXT_TYPE_opaque_prf_input */
# if defined(TLSEXT_TYPE_next_proto_neg)
case TLSEXT_TYPE_next_proto_neg:
extension_name = "next protocol";
break;
# endif /* TLSEXT_TYPE_next_proto_neg */
# if defined(TLSEXT_TYPE_application_layer_protocol_negotiation)
case TLSEXT_TYPE_application_layer_protocol_negotiation:
extension_name = "application layer protocol";
break;
# endif /* TLSEXT_TYPE_application_layer_protocol_negotiation */
# if defined(TLSEXT_TYPE_padding)
case TLSEXT_TYPE_padding:
extension_name = "TLS padding";
break;
# endif /* TLSEXT_TYPE_padding */
# if defined(TLSEXT_TYPE_early_data)
case TLSEXT_TYPE_early_data:
extension_name = "early data";
break;
# endif /* TLSEXT_TYPE_early_data */
# if defined(TLSEXT_TYPE_post_handshake_auth)
case TLSEXT_TYPE_post_handshake_auth:
extension_name = "post handshake auth";
break;
# endif /* TLSEXT_TYPE_post_handshake_auth */
default:
print_basic_info = TRUE;
break;
}
if (print_basic_info == TRUE) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.tlsext] TLS %s extension \"%s\" (ID %d, %d %s)",
server ? "server" : "client", extension_name, type, tlsext_datalen,
tlsext_datalen != 1 ? "bytes" : "byte");
}
}
#endif /* OPENSSL_NO_TLSEXT */
static void tls_msg_cb(int io_flag, int version, int content_type,
const void *buf, size_t buflen, SSL *ssl, void *arg) {
char *action_str = NULL;
char *version_str = NULL;
char *bytes_str = buflen != 1 ? "bytes" : "byte";
if (io_flag == 0) {
action_str = "received";
} else if (io_flag == 1) {
action_str = "sent";
}
switch (version) {
case SSL2_VERSION:
version_str = "SSLv2";
break;
case SSL3_VERSION:
version_str = "SSLv3";
break;
case TLS1_VERSION:
version_str = "TLSv1";
break;
# if defined(TLS1_1_VERSION)
case TLS1_1_VERSION:
version_str = "TLSv1.1";
break;
# endif /* TLS1_1_VERSION */
# if defined(TLS1_2_VERSION)
case TLS1_2_VERSION:
version_str = "TLSv1.2";
break;
# endif /* TLS1_2_VERSION */
# if defined(TLS1_3_VERSION)
case TLS1_3_VERSION:
version_str = "TLSv1.3";
break;
# endif /* TLS1_3_VERSION */
default:
# if defined(SSL3_RT_HEADER)
/* OpenSSL calls this callback for SSL records received; filter those
* from true "unknowns".
*/
if (version == 0 &&
(content_type != SSL3_RT_HEADER ||
buflen != SSL3_RT_HEADER_LENGTH)) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] unknown/unsupported version: %d", version);
}
# else
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] unknown/unsupported version: %d", version);
# endif
break;
}
if (version == SSL3_VERSION ||
# if defined(TLS1_1_VERSION)
version == TLS1_1_VERSION ||
# endif /* TLS1_1_VERSION */
# if defined(TLS1_2_VERSION)
version == TLS1_2_VERSION ||
# endif /* TLS1_2_VERSION */
# if defined(TLS1_3_VERSION)
version == TLS1_3_VERSION ||
# endif /* TLS1_3_VERSION */
version == TLS1_VERSION) {
switch (content_type) {
case SSL3_RT_CHANGE_CIPHER_SPEC:
/* ChangeCipherSpec message */
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s %s ChangeCipherSpec message (%u %s)",
action_str, version_str, (unsigned int) buflen, bytes_str);
break;
case SSL3_RT_ALERT: {
/* Alert messages */
pr_trace_msg(trace_channel, 27,
"%s %s Alert (%u %s)", action_str, version_str,
(unsigned int) buflen, bytes_str);
if (buflen == 2) {
char *severity_str = NULL;
/* Peek naughtily into the buffer. */
switch (((const unsigned char *) buf)[0]) {
case SSL3_AL_WARNING:
severity_str = "warning";
break;
case SSL3_AL_FATAL:
severity_str = "fatal";
break;
}
switch (((const unsigned char *) buf)[1]) {
case SSL3_AD_CLOSE_NOTIFY:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s %s %s 'close_notify' Alert message (%u %s)",
action_str, version_str, severity_str, (unsigned int) buflen,
bytes_str);
break;
case SSL3_AD_UNEXPECTED_MESSAGE:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s %s %s 'unexpected_message' Alert message "
"(%u %s)", action_str, version_str, severity_str,
(unsigned int) buflen, bytes_str);
break;
case SSL3_AD_BAD_RECORD_MAC:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s %s %s 'bad_record_mac' Alert message (%u %s)",
action_str, version_str, severity_str, (unsigned int) buflen,
bytes_str);
break;
case 21:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s %s %s 'decryption_failed' Alert message "
"(%u %s)", action_str, version_str, severity_str,
(unsigned int) buflen, bytes_str);
break;
case 22:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s %s %s 'record_overflow' Alert message (%u %s)",
action_str, version_str, severity_str, (unsigned int) buflen,
bytes_str);
break;
case 30:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s %s %s 'decompression_failure' Alert message "
"(%u %s)", action_str, version_str, severity_str,
(unsigned int) buflen, bytes_str);
break;
case SSL3_AD_HANDSHAKE_FAILURE:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s %s %s 'handshake_failure' Alert message "
"(%u %s)", action_str, version_str, severity_str,
(unsigned int) buflen, bytes_str);
break;
# if defined(SSL3_AD_BAD_CERTIFICATE)
case SSL3_AD_BAD_CERTIFICATE:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s %s %s 'bad_certificate' Alert message "
"(%u %s)", action_str, version_str, severity_str,
(unsigned int) buflen, bytes_str);
break;
# endif /* SSL3_AD_BAD_CERTIFICATE */
# if defined(SSL3_AD_CERTIFICATE_REVOKED)
case SSL3_AD_CERTIFICATE_REVOKED:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s %s %s 'certificate_revoked' Alert message "
"(%u %s)", action_str, version_str, severity_str,
(unsigned int) buflen, bytes_str);
break;
# endif /* SSL3_AD_CERTIFICATE_REVOKED */
# if defined(SSL3_AD_CERTIFICATE_EXPIRED)
case SSL3_AD_CERTIFICATE_EXPIRED:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s %s %s 'certificate_expired' Alert message "
"(%u %s)", action_str, version_str, severity_str,
(unsigned int) buflen, bytes_str);
break;
# endif /* SSL3_AD_CERTIFICATE_EXPIRED */
# if defined(SSL3_AD_CERTIFICATE_UNKNOWN)
case SSL3_AD_CERTIFICATE_UNKNOWN:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s %s %s 'certificate_unknown' Alert message "
"(%u %s)", action_str, version_str, severity_str,
(unsigned int) buflen, bytes_str);
break;
# endif /* SSL3_AD_CERTIFICATE_UNKNOWN */
# if defined(SSL3_AD_ILLEGAL_PARAMETER)
case SSL3_AD_ILLEGAL_PARAMETER:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s %s %s 'illegal_parameter' Alert message "
"(%u %s)", action_str, version_str, severity_str,
(unsigned int) buflen, bytes_str);
break;
# endif /* SSL3_AD_ILLEGAL_PARAMETER */
}
} else {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s %s Alert message, unknown type (%u %s)", action_str,
version_str, (unsigned int) buflen, bytes_str);
}
break;
}
case SSL3_RT_HANDSHAKE: {
/* Handshake messages */
pr_trace_msg(trace_channel, 27,
"%s %s Handshake (%u %s)", action_str, version_str,
(unsigned int) buflen, bytes_str);
if (buflen > 0) {
/* Peek naughtily into the buffer. */
switch (((const unsigned char *) buf)[0]) {
case SSL3_MT_HELLO_REQUEST:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s %s 'HelloRequest' Handshake message (%u %s)",
action_str, version_str, (unsigned int) buflen, bytes_str);
break;
case SSL3_MT_CLIENT_HELLO: {
const unsigned char *msg;
size_t msglen;
msg = buf;
msglen = buflen;
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s %s 'ClientHello' Handshake message (%u %s)",
action_str, version_str, (unsigned int) buflen, bytes_str);
tls_print_client_hello(io_flag, version, content_type, msg + 4,
msglen - 4, ssl, arg);
break;
}
case SSL3_MT_SERVER_HELLO: {
const unsigned char *msg;
size_t msglen;
msg = buf;
msglen = buflen;
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s %s 'ServerHello' Handshake message (%u %s)",
action_str, version_str, (unsigned int) buflen, bytes_str);
tls_print_server_hello(io_flag, version, content_type, msg + 4,
msglen - 4, ssl, arg);
break;
}
# if defined(SSL3_MT_NEWSESSION_TICKET)
case SSL3_MT_NEWSESSION_TICKET: {
const unsigned char *msg;
size_t msglen;
msg = buf;
msglen = buflen;
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s %s 'NewSessionTicket' Handshake message (%u %s)",
action_str, version_str, (unsigned int) buflen, bytes_str);
tls_print_ticket(io_flag, version, content_type, msg + 4,
msglen - 4, ssl, arg);
break;
}
# endif /* SSL3_MT_NEWSESSION_TICKET */
case SSL3_MT_CERTIFICATE:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s %s 'Certificate' Handshake message (%u %s)",
action_str, version_str, (unsigned int) buflen, bytes_str);
break;
case SSL3_MT_SERVER_KEY_EXCHANGE:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s %s 'ServerKeyExchange' Handshake message "
"(%u %s)", action_str, version_str, (unsigned int) buflen,
bytes_str);
break;
case SSL3_MT_CERTIFICATE_REQUEST:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s %s 'CertificateRequest' Handshake message "
"(%u %s)", action_str, version_str, (unsigned int) buflen,
bytes_str);
break;
case SSL3_MT_SERVER_DONE:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s %s 'ServerHelloDone' Handshake message (%u %s)",
action_str, version_str, (unsigned int) buflen, bytes_str);
break;
case SSL3_MT_CERTIFICATE_VERIFY:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s %s 'CertificateVerify' Handshake message "
"(%u %s)", action_str, version_str, (unsigned int) buflen,
bytes_str);
break;
case SSL3_MT_CLIENT_KEY_EXCHANGE:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s %s 'ClientKeyExchange' Handshake message "
"(%u %s)", action_str, version_str, (unsigned int) buflen,
bytes_str);
break;
case SSL3_MT_FINISHED:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s %s 'Finished' Handshake message (%u %s)",
action_str, version_str, (unsigned int) buflen, bytes_str);
break;
# if defined(SSL3_MT_CERTIFICATE_STATUS)
case SSL3_MT_CERTIFICATE_STATUS:
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s %s 'CertificateStatus' Handshake message (%u %s)",
action_str, version_str, (unsigned int) buflen, bytes_str);
break;
# endif /* SSL3_MT_CERTIFICATE_STATUS */
# if defined(SSL3_MT_ENCRYPTED_EXTENSIONS)
case SSL3_MT_ENCRYPTED_EXTENSIONS: {
const unsigned char *msg;
size_t msglen;
BIO *bio;
char *data = NULL;
long datalen;
msg = buf;
msglen = buflen;
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s %s 'EncryptedExtensions' Handshake message "
"(%u %s)", action_str, version_str, (unsigned int) buflen,
bytes_str);
bio = BIO_new(BIO_s_mem());
msg += 4;
msglen -= 4;
BIO_puts(bio, "\nEncryptedExtensions:\n");
tls_print_extensions(bio, "extensions", TRUE, &msg, &msglen);
datalen = BIO_get_mem_data(bio, &data);
if (data != NULL) {
data[datalen] = '\0';
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %.*s", (int) datalen, data);
}
BIO_free(bio);
break;
}
# endif /* SSL3_MT_ENCRYPTED_EXTENSIONS */
}
} else {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s %s Handshake message, unknown type %d (%u %s)",
action_str, version_str, content_type, (unsigned int) buflen,
bytes_str);
}
break;
}
}
# if defined(SSL3_RT_HEADER)
} else if (version == 0 &&
content_type == SSL3_RT_HEADER &&
buflen == SSL3_RT_HEADER_LENGTH) {
const unsigned char *msg;
const char *record_type;
unsigned int msg_len;
msg = buf;
record_type = tls_get_label(msg[0], tls_record_type_labels);
msg_len = msg[buflen - 2] << 8 | msg[buflen - 1];
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s protocol record message (content_type = %s, len = %u)",
action_str, record_type, msg_len);
# endif
} else {
/* This case might indicate an issue with OpenSSL itself; the version
* given to the msg_callback function was not initialized, or not set to
* one of the recognized SSL/TLS versions. Weird.
*/
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"[tls.msg] %s message of unknown version %d, type %d (%u %s)",
action_str, version, content_type, (unsigned int) buflen, bytes_str);
}
}
# endif /* OpenSSL-0.9.7 or later */
#endif /* PR_USE_OPENSSL */
int proxy_tls_sess_init(pool *p, struct proxy_session *proxy_sess, int flags) {
#if defined(PR_USE_OPENSSL)
config_rec *c;
unsigned int enabled_proto_count = 0, tls_protocol = PROXY_TLS_PROTO_DEFAULT;
int disabled_proto, res, xerrno = 0;
const char *enabled_proto_str = NULL;
char *ca_file = NULL, *ca_path = NULL, *cert_file = NULL, *key_file = NULL,
*crl_file = NULL, *crl_path = NULL;
if (proxy_sess == NULL) {
errno = EINVAL;
return -1;
}
if (proxy_sess->use_ftp == FALSE) {
return 0;
}
c = find_config(main_server->conf, CONF_PARAM, "ProxyTLSEngine", FALSE);
if (c != NULL) {
tls_engine = *((int *) c->argv[0]);
}
if (tls_engine == PROXY_TLS_ENGINE_OFF) {
return 0;
}
if (p == NULL) {
errno = EINVAL;
return -1;
}
if (ssl_ctx == NULL) {
/* We haven't been initialized properly! */
pr_trace_msg(trace_channel, 2, "%s",
"missing required SSL_CTX initialization!");
errno = EPERM;
return -1;
}
c = find_config(main_server->conf, CONF_PARAM, "ProxyTLSOptions", FALSE);
while (c != NULL) {
unsigned long opts = 0;
pr_signals_handle();
opts = *((unsigned long *) c->argv[0]);
tls_opts |= opts;
c = find_config_next(c, c->next, CONF_PARAM, "ProxyTLSOptions", FALSE);
}
PRIVS_ROOT
tls_ds.dsh = (tls_ds.open)(proxy_pool, tls_tables_path, tls_opts);
xerrno = errno;
PRIVS_RELINQUISH
if (tls_ds.dsh == NULL) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"error opening TLS datastore: %s", strerror(xerrno));
errno = xerrno;
return -1;
}
c = find_config(main_server->conf, CONF_PARAM, "ProxyTLSProtocol", FALSE);
if (c != NULL) {
tls_protocol = *((unsigned int *) c->argv[0]);
}
disabled_proto = get_disabled_protocols(tls_protocol);
/* Per the comments in <ssl/ssl.h>, SSL_CTX_set_options() uses |= on
* the previous value. This means we can easily OR in our new option
* values with any previously set values.
*/
enabled_proto_str = get_enabled_protocols_str(main_server->pool,
tls_protocol, &enabled_proto_count);
pr_log_debug(DEBUG8, MOD_PROXY_VERSION ": supporting %s %s",
enabled_proto_str,
enabled_proto_count != 1 ? "protocols" : "protocol only");
SSL_CTX_set_options(ssl_ctx, disabled_proto);
c = find_config(main_server->conf, CONF_PARAM, "ProxyTLSCipherSuite", FALSE);
while (c != NULL) {
int protocol;
pr_signals_handle();
protocol = *((int *) c->argv[1]);
#if OPENSSL_VERSION_NUMBER >= 0x10101000L && \
defined(TLS1_3_VERSION)
if (protocol == PROXY_TLS_PROTO_TLS_V1_3) {
tlsv13_cipher_suite = c->argv[0];
} else {
tls_cipher_suite = c->argv[0];
}
#else
tls_cipher_suite = c->argv[0];
#endif /* TLS1_3_VERSION */
c = find_config_next(c, c->next, CONF_PARAM, "ProxyTLSCipherSuite", FALSE);
}
if (tls_cipher_suite == NULL) {
tls_cipher_suite = PROXY_TLS_DEFAULT_CIPHER_SUITE;
}
SSL_CTX_set_cipher_list(ssl_ctx, tls_cipher_suite);
#if OPENSSL_VERSION_NUMBER >= 0x10101000L && \
defined(TLS1_3_VERSION)
if (tlsv13_cipher_suite != NULL) {
SSL_CTX_set_ciphersuites(ssl_ctx, tlsv13_cipher_suite);
}
#endif /* TLS1_3_VERSION */
c = find_config(main_server->conf, CONF_PARAM, "ProxyTLSTimeoutHandshake",
FALSE);
if (c != NULL) {
handshake_timeout = *((unsigned int *) c->argv[0]);
}
c = find_config(main_server->conf, CONF_PARAM, "ProxyTLSCACertificateFile",
FALSE);
if (c != NULL) {
ca_file = c->argv[0];
} else {
ca_file = PR_CONFIG_DIR "/cacerts.pem";
if (!file_exists2(p, ca_file)) {
pr_trace_msg(trace_channel, 9,
"warning: no default ProxyTLSCACertificateFile found at '%s'", ca_file);
ca_file = NULL;
}
}
c = find_config(main_server->conf, CONF_PARAM, "ProxyTLSCACertificatePath",
FALSE);
if (c != NULL) {
ca_path = c->argv[0];
}
if (ca_file != NULL ||
ca_path != NULL) {
long verify_flags = 0;
X509_VERIFY_PARAM *verify_param;
/* Set the locations used for verifying certificates. */
PRIVS_ROOT
if (SSL_CTX_load_verify_locations(ssl_ctx, ca_file, ca_path) != 1) {
PRIVS_RELINQUISH
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"unable to set CA verification using file '%s' or "
"directory '%s': %s", ca_file ? ca_file : "(none)",
ca_path ? ca_path : "(none)", proxy_tls_get_errors());
errno = EPERM;
return -1;
}
PRIVS_RELINQUISH
verify_param = X509_VERIFY_PARAM_new();
#if 0
/* NOTE: Many server certs may not have a CRL provider configured; such certs
* would be deemed invalid/unusable by these CRL_CHECK flags. So they are
* disabled, for now.
*/
# if defined(X509_V_FLAG_CRL_CHECK)
verify_flags |= X509_V_FLAG_CRL_CHECK;
# endif
# if defined(X509_V_FLAG_CRL_CHECK_ALL)
verify_flags |= X509_V_FLAG_CRL_CHECK_ALL;
# endif
#endif
# if defined(X509_V_FLAG_TRUSTED_FIRST)
verify_flags |= X509_V_FLAG_TRUSTED_FIRST;
# endif
# if defined(X509_V_FLAG_PARTIAL_CHAIN)
verify_flags |= X509_V_FLAG_PARTIAL_CHAIN;
# endif
if (X509_VERIFY_PARAM_set_flags(verify_param, verify_flags) != 1) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"error preparing X509 verification parameters: %s",
proxy_tls_get_errors());
} else {
if (SSL_CTX_set1_param(ssl_ctx, verify_param) != 1) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"error setting X509 verification parameters: %s",
proxy_tls_get_errors());
}
}
} else {
/* Default to using locations set in the OpenSSL config file. */
pr_trace_msg(trace_channel, 9,
"using default OpenSSL CA verification locations (see $SSL_CERT_DIR "
"environment variable)");
if (SSL_CTX_set_default_verify_paths(ssl_ctx) != 1) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"error setting default CA verification locations: %s",
proxy_tls_get_errors());
}
}
c = find_config(main_server->conf, CONF_PARAM, "ProxyTLSCARevocationFile",
FALSE);
if (c != NULL) {
crl_file = c->argv[0];
}
c = find_config(main_server->conf, CONF_PARAM, "ProxyTLSCARevocationPath",
FALSE);
if (c != NULL) {
crl_path = c->argv[0];
}
if (crl_file != NULL ||
crl_path != NULL) {
X509_STORE *crl_store;
crl_store = X509_STORE_new();
if (crl_store == NULL) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"error allocating CRL store: %s", proxy_tls_get_errors());
errno = EPERM;
return -1;
}
if (X509_STORE_load_locations(crl_store, crl_file, crl_path) != 1) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"error loading ProxyTLSCARevocation files: %s", proxy_tls_get_errors());
} else {
SSL_CTX_set_cert_store(ssl_ctx, crl_store);
}
}
c = find_config(main_server->conf, CONF_PARAM, "ProxyTLSVerifyServer", FALSE);
if (c != NULL) {
tls_verify_server = *((int *) c->argv[0]);
}
c = find_config(main_server->conf, CONF_PARAM, "ProxyTLSCertificateFile",
FALSE);
if (c != NULL) {
cert_file = c->argv[0];
}
c = find_config(main_server->conf, CONF_PARAM, "ProxyTLSCertificateKeyFile",
FALSE);
if (c != NULL) {
key_file = c->argv[0];
} else {
key_file = cert_file;
}
if (cert_file != NULL) {
int ok = TRUE;
PRIVS_ROOT
res = SSL_CTX_use_certificate_file(ssl_ctx, cert_file, SSL_FILETYPE_PEM);
PRIVS_RELINQUISH
if (res != 1) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"error loading certificate from ProxyTLSCertificateFile '%s': %s",
cert_file, proxy_tls_get_errors());
ok = FALSE;
}
if (ok) {
PRIVS_ROOT
res = SSL_CTX_use_PrivateKey_file(ssl_ctx, key_file, SSL_FILETYPE_PEM);
PRIVS_RELINQUISH
if (res != 1) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"error loading private key from ProxyTLSCertificateKeyFile '%s': %s",
key_file, proxy_tls_get_errors());
ok = FALSE;
}
}
if (ok) {
res = SSL_CTX_check_private_key(ssl_ctx);
if (res != 1) {
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"warning: ProxyTLSCertificateKeyFile '%s' private key does not "
"match ProxyTLSCertificateFile '%s' certificate", key_file,
cert_file);
}
}
}
# if defined(PSK_MAX_PSK_LEN)
c = find_config(main_server->conf, CONF_PARAM, "ProxyTLSPreSharedKey", FALSE);
if (c != NULL) {
const char *identity, *path;
pr_signals_handle();
identity = c->argv[0];
path = c->argv[1];
/* Advance past the "hex:" format prefix. */
path += 4;
res = tls_load_psk(identity, path);
if (res < 0) {
pr_log_debug(DEBUG2, MOD_PROXY_VERSION
": error loading ProxyTLSPreSharedKey file '%s': %s", path,
strerror(errno));
}
}
if (tls_psk_name != NULL) {
pr_trace_msg(trace_channel, 9,
"enabling support for PSK identities");
SSL_CTX_set_psk_client_callback(ssl_ctx, tls_psk_cb);
}
# endif /* PSK support */
# if !defined(OPENSSL_NO_TLSEXT) && defined(TLSEXT_STATUSTYPE_ocsp)
SSL_CTX_set_tlsext_status_cb(ssl_ctx, tls_ocsp_response_cb);
# endif /* OCSP support */
if (tls_opts & PROXY_TLS_OPT_ALLOW_WEAK_SECURITY) {
# if OPENSSL_VERSION_NUMBER >= 0x10100000L
SSL_CTX_set_security_level(ssl_ctx, 0);
# endif /* OpenSSL-1.1.0 and later */
}
if (tls_opts & PROXY_TLS_OPT_ENABLE_DIAGS) {
SSL_CTX_set_info_callback(ssl_ctx, tls_info_cb);
# if OPENSSL_VERSION_NUMBER > 0x000907000L
SSL_CTX_set_msg_callback(ssl_ctx, tls_msg_cb);
# endif
}
if (netio_install_ctrl() < 0) {
xerrno = errno;
(void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
"error installing control connection proxy NetIO: %s", strerror(xerrno));
errno = xerrno;
return -1;
}
#endif /* PR_USE_OPENSSL */
return 0;
}
int proxy_tls_sess_free(pool *p) {
if (p == NULL) {
errno = EINVAL;
return -1;
}
#if defined(PR_USE_OPENSSL)
/* Reset any state, but only if we have not already negotiated an SSL
* session.
*/
if (session.rfc2228_mech == NULL) {
handshake_timeout = 30;
tls_opts = 0UL;
tls_engine = PROXY_TLS_ENGINE_AUTO;
tls_verify_server = TRUE;
tls_cipher_suite = NULL;
# if OPENSSL_VERSION_NUMBER >= 0x10101000L && \
defined(TLS1_3_VERSION)
tlsv13_cipher_suite = NULL;
# endif /* TLS1_3_VERSION */
# if defined(PSK_MAX_PSK_LEN)
tls_psk_name = NULL;
tls_psk_bn = NULL;
tls_psk_used = FALSE;
# endif /* PSK support */
if (tls_ds.dsh != NULL) {
(void) (tls_ds.close)(p, tls_ds.dsh);
tls_ds.dsh = NULL;
}
if (ssl_ctx != NULL) {
if (init_ssl_ctx() < 0) {
return -1;
}
}
if (tls_ctrl_netio != NULL) {
pr_netio_t *using_netio = NULL;
if (proxy_netio_using(PR_NETIO_STRM_CTRL, &using_netio) == 0) {
if (using_netio == tls_ctrl_netio) {
proxy_netio_use(PR_NETIO_STRM_CTRL, NULL);
}
}
destroy_pool(tls_ctrl_netio->pool);
tls_ctrl_netio = NULL;
}
}
#endif /* PR_USE_OPENSSL */
return 0;
}
|