1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572
|
/*
* IKEv2 parent SA creation routines, for Libreswan
*
* Copyright (C) 2007-2008 Michael Richardson <mcr@xelerance.com>
* Copyright (C) 2008-2011 Paul Wouters <paul@xelerance.com>
* Copyright (C) 2008 Antony Antony <antony@xelerance.com>
* Copyright (C) 2008-2009 David McCullough <david_mccullough@securecomputing.com>
* Copyright (C) 2010,2012 Avesh Agarwal <avagarwa@redhat.com>
* Copyright (C) 2010-2019 Tuomo Soini <tis@foobar.fi
* Copyright (C) 2012-2019 Paul Wouters <pwouters@redhat.com>
* Copyright (C) 2012-2018 Antony Antony <antony@phenome.org>
* Copyright (C) 2013-2019 D. Hugh Redelmeier <hugh@mimosa.com>
* Copyright (C) 2013 David McCullough <ucdevel@gmail.com>
* Copyright (C) 2013 Matt Rogers <mrogers@redhat.com>
* Copyright (C) 2015-2019 Andrew Cagney <cagney@gnu.org>
* Copyright (C) 2017-2018 Sahana Prasad <sahana.prasad07@gmail.com>
* Copyright (C) 2017-2018 Vukasin Karadzic <vukasin.karadzic@gmail.com>
* Copyright (C) 2017 Mayank Totale <mtotale@gmail.com>
* Copyright (C) 2020 Yulia Kuzovkova <ukuzovkova@gmail.com>
*
* 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. See <https://www.gnu.org/licenses/gpl2.txt>.
*
* 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.
*
*/
#include <unistd.h>
#include "sysdep.h"
#include "constants.h"
#include "defs.h"
#include "state.h"
#include "keys.h" /* needs state.h */
#include "id.h"
#include "connections.h"
#include "crypt_prf.h"
#include "crypto.h"
#include "x509.h"
#include "pluto_x509.h"
#include "ike_alg.h"
#include "ike_alg_hash.h"
#include "ike_alg_dh.h"
#include "kernel_alg.h"
#include "plutoalg.h"
#include "packet.h"
#include "demux.h"
#include "ikev2.h"
#include "log.h"
#include "ipsec_doi.h"
#include "vendor.h"
#include "timer.h"
#include "ike_spi.h"
#include "rnd.h"
#include "pending.h"
#include "kernel.h"
#include "nat_traversal.h"
#include "keyhi.h" /* for SECKEY_DestroyPublicKey */
#include "vendor.h"
#include "crypt_hash.h"
#include "ikev2_ipseckey.h"
#include "ikev2_ppk.h"
#include "ikev2_redirect.h"
#include "pam_auth.h"
#include "crypt_dh.h"
#include "crypt_prf.h"
#include "ietf_constants.h"
#include "ip_address.h"
#include "hostpair.h"
#include "send.h"
#include "ikev2_send.h"
#include "pluto_stats.h"
#include "retry.h"
#include "ipsecconf/confread.h" /* for struct starter_end */
#include "addr_lookup.h"
#include "impair.h"
#include "ikev2_message.h"
#include "ikev2_notify.h"
#include "ikev2_ts.h"
#include "ikev2_msgid.h"
#include "state_db.h"
#ifdef USE_XFRM_INTERFACE
# include "kernel_xfrm_interface.h"
#endif
#include "crypt_ke.h"
#include "crypt_symkey.h" /* for release_symkey */
#include "ip_info.h"
#include "iface.h"
#include "ikev2_auth.h"
#include "secrets.h"
#include "cert_decode_helper.h"
#include "addresspool.h"
#include "unpack.h"
struct mobike {
ip_endpoint remote;
const struct iface_endpoint *interface;
};
static stf_status ikev2_parent_inI2outR2_auth_tail(struct state *st,
struct msg_digest *md,
bool pam_status);
static stf_status ikev2_child_out_tail(struct ike_sa *ike,
struct child_sa *child,
struct msg_digest *request_md);
static bool accept_v2_nonce(struct logger *logger, struct msg_digest *md,
chunk_t *dest, const char *name)
{
/*
* note ISAKMP_NEXT_v2Ni == ISAKMP_NEXT_v2Nr
* so when we refer to ISAKMP_NEXT_v2Ni, it might be ISAKMP_NEXT_v2Nr
*/
pb_stream *nonce_pbs = &md->chain[ISAKMP_NEXT_v2Ni]->pbs;
shunk_t nonce = pbs_in_left_as_shunk(nonce_pbs);
/*
* RFC 7296 Section 2.10:
* Nonces used in IKEv2 MUST be randomly chosen, MUST be at least 128
* bits in size, and MUST be at least half the key size of the
* negotiated pseudorandom function (PRF). However, the initiator
* chooses the nonce before the outcome of the negotiation is known.
* Because of that, the nonce has to be long enough for all the PRFs
* being proposed.
*
* We will check for a minimum/maximum here - not meeting that
* requirement is a syntax error(?). Once the PRF is
* selected, we verify the nonce is big enough.
*/
if (nonce.len < IKEv2_MINIMUM_NONCE_SIZE || nonce.len > IKEv2_MAXIMUM_NONCE_SIZE) {
llog(RC_LOG_SERIOUS, logger, "%s length %zu not between %d and %d",
name, nonce.len, IKEv2_MINIMUM_NONCE_SIZE, IKEv2_MAXIMUM_NONCE_SIZE);
return false;
}
free_chunk_content(dest);
*dest = clone_hunk(nonce, name);
return true;
}
static bool negotiate_hash_algo_from_notification(const struct pbs_in *payload_pbs,
struct ike_sa *ike)
{
lset_t sighash_policy = ike->sa.st_connection->sighash_policy;
struct pbs_in pbs = *payload_pbs;
while (pbs_left(&pbs) > 0) {
uint16_t nh_value;
passert(sizeof(nh_value) == RFC_7427_HASH_ALGORITHM_IDENTIFIER_SIZE);
diag_t d = pbs_in_raw(&pbs, &nh_value, sizeof(nh_value),
"hash algorithm identifier (network ordered)");
if (d != NULL) {
log_diag(RC_LOG_SERIOUS, ike->sa.st_logger, &d, "%s", "");
return false;
}
uint16_t h_value = ntohs(nh_value);
switch (h_value) {
/* We no longer support SHA1 (as per RFC 8247) */
case IKEv2_HASH_ALGORITHM_SHA2_256:
if (sighash_policy & POL_SIGHASH_SHA2_256) {
ike->sa.st_hash_negotiated |= NEGOTIATE_AUTH_HASH_SHA2_256;
dbg("received HASH_ALGORITHM_SHA2_256 which is allowed by local policy");
}
break;
case IKEv2_HASH_ALGORITHM_SHA2_384:
if (sighash_policy & POL_SIGHASH_SHA2_384) {
ike->sa.st_hash_negotiated |= NEGOTIATE_AUTH_HASH_SHA2_384;
dbg("received HASH_ALGORITHM_SHA2_384 which is allowed by local policy");
}
break;
case IKEv2_HASH_ALGORITHM_SHA2_512:
if (sighash_policy & POL_SIGHASH_SHA2_512) {
ike->sa.st_hash_negotiated |= NEGOTIATE_AUTH_HASH_SHA2_512;
dbg("received HASH_ALGORITHM_SHA2_512 which is allowed by local policy");
}
break;
case IKEv2_HASH_ALGORITHM_SHA1:
dbg("received and ignored IKEv2_HASH_ALGORITHM_SHA1 - it is no longer allowed as per RFC 8247");
break;
case IKEv2_HASH_ALGORITHM_IDENTITY:
/* ike->sa.st_hash_negotiated |= NEGOTIATE_HASH_ALGORITHM_IDENTITY; */
dbg("received unsupported HASH_ALGORITHM_IDENTITY - ignored");
break;
default:
log_state(RC_LOG, &ike->sa, "received and ignored unknown hash algorithm %d", h_value);
}
}
return true;
}
/* check for ASN.1 blob; if found, consume it */
static bool ikev2_try_asn1_hash_blob(const struct hash_desc *hash_algo,
pb_stream *a_pbs,
enum keyword_authby authby)
{
shunk_t b = authby_asn1_hash_blob(hash_algo, authby);
uint8_t in_blob[ASN1_LEN_ALGO_IDENTIFIER +
PMAX(ASN1_SHA1_ECDSA_SIZE,
PMAX(ASN1_SHA2_RSA_PSS_SIZE, ASN1_SHA2_ECDSA_SIZE))];
dbg("looking for ASN.1 blob for method %s for hash_algo %s",
enum_name(&keyword_authby_names, authby), hash_algo->common.fqn);
return
pexpect(b.ptr != NULL) && /* we know this hash */
pbs_left(a_pbs) >= b.len && /* the stream has enough octets */
memeq(a_pbs->cur, b.ptr, b.len) && /* they are the right octets */
pexpect(b.len <= sizeof(in_blob)) && /* enough space in in_blob[] */
pexpect(pbs_in_raw(a_pbs, in_blob, b.len, "ASN.1 blob for hash algo") == NULL); /* can eat octets */
}
void ikev2_ike_sa_established(struct ike_sa *ike,
const struct state_v2_microcode *svm,
enum state_kind new_state)
{
struct connection *c = ike->sa.st_connection;
/*
* Taking it (what???) current from current state I2/R1.
* The parent has advanced but not the svm???
* Ideally this should be timeout of I3/R2 state svm.
* How to find that svm???
* I wonder what this comment means? Needs rewording.
*
* XXX: .timeout_event is tied to a state transition. Does
* that mean it applies to the transition or to the final
* state? It is kind of treated as all three (the third case
* is where a transition gets shared between the parent and
* child).
*/
pexpect(svm->timeout_event == EVENT_SA_REPLACE);
/*
* update the parent state to make sure that it knows we have
* authenticated properly.
*/
change_state(&ike->sa, new_state);
c->newest_isakmp_sa = ike->sa.st_serialno;
v2_schedule_replace_event(&ike->sa);
ike->sa.st_viable_parent = TRUE;
linux_audit_conn(&ike->sa, LAK_PARENT_START);
pstat_sa_established(&ike->sa);
}
/*
* Check that the bundled keying material (KE) matches the accepted
* proposal and if it doesn't record a response and return false.
*/
static bool v2_accept_ke_for_proposal(struct ike_sa *ike,
struct state *st,
struct msg_digest *md,
const struct dh_desc *accepted_dh,
enum payload_security security)
{
passert(md->chain[ISAKMP_NEXT_v2KE] != NULL);
int ke_group = md->chain[ISAKMP_NEXT_v2KE]->payload.v2ke.isak_group;
if (accepted_dh->common.id[IKEv2_ALG_ID] == ke_group) {
return true;
}
struct esb_buf ke_esb;
llog(RC_LOG, st->st_logger,
"initiator guessed wrong keying material group (%s); responding with INVALID_KE_PAYLOAD requesting %s",
enum_show_shortb(&oakley_group_names, ke_group, &ke_esb),
accepted_dh->common.fqn);
pstats(invalidke_sent_u, ke_group);
pstats(invalidke_sent_s, accepted_dh->common.id[IKEv2_ALG_ID]);
/* convert group to a raw buffer */
uint16_t gr = htons(accepted_dh->group);
chunk_t nd = THING_AS_CHUNK(gr);
record_v2N_response(st->st_logger, ike, md,
v2N_INVALID_KE_PAYLOAD, &nd,
security);
return false;
}
/*
* Called by ikev2_parent_inI2outR2_tail() and ikev2_parent_inR2()
* Do the actual AUTH payload verification
*/
/*
* ??? Several verify routines return an stf_status and yet we just return a bool.
* We perhaps should return an stf_status so distinctions don't get lost.
*
* XXX: this is answering a simple yes/no question. Did auth succeed.
* Caller needs to decide what response is appropriate.
*/
static bool v2_check_auth(enum ikev2_auth_method recv_auth,
struct ike_sa *ike,
const struct crypt_mac *idhash_in,
pb_stream *pbs,
const enum keyword_authby that_authby,
const char *context)
{
switch (recv_auth) {
case IKEv2_AUTH_RSA:
{
if (that_authby != AUTHBY_RSASIG) {
log_state(RC_LOG, &ike->sa,
"peer attempted RSA authentication but we want %s in %s",
enum_name(&keyword_authby_names, that_authby),
context);
return false;
}
stf_status authstat = ikev2_verify_rsa_hash(ike, idhash_in, pbs,
&ike_alg_hash_sha1);
if (authstat != STF_OK) {
log_state(RC_LOG, &ike->sa,
"RSA authentication of %s failed", context);
return false;
}
return true;
}
case IKEv2_AUTH_PSK:
{
if (that_authby != AUTHBY_PSK) {
log_state(RC_LOG, &ike->sa,
"peer attempted PSK authentication but we want %s in %s",
enum_name(&keyword_authby_names, that_authby),
context);
return FALSE;
}
if (!ikev2_verify_psk_auth(AUTHBY_PSK, ike, idhash_in, pbs)) {
log_state(RC_LOG, &ike->sa,
"PSK Authentication failed: AUTH mismatch in %s!",
context);
return FALSE;
}
return TRUE;
}
case IKEv2_AUTH_NULL:
{
if (!(that_authby == AUTHBY_NULL ||
(that_authby == AUTHBY_RSASIG && LIN(POLICY_AUTH_NULL, ike->sa.st_connection->policy)))) {
log_state(RC_LOG, &ike->sa,
"peer attempted NULL authentication but we want %s in %s",
enum_name(&keyword_authby_names, that_authby),
context);
return FALSE;
}
if (!ikev2_verify_psk_auth(AUTHBY_NULL, ike, idhash_in, pbs)) {
log_state(RC_LOG, &ike->sa,
"NULL authentication failed: AUTH mismatch in %s! (implementation bug?)",
context);
return FALSE;
}
ike->sa.st_ikev2_anon = TRUE;
return TRUE;
}
case IKEv2_AUTH_DIGSIG:
{
if (that_authby != AUTHBY_ECDSA && that_authby != AUTHBY_RSASIG) {
log_state(RC_LOG, &ike->sa,
"peer attempted Authentication through Digital Signature but we want %s in %s",
enum_name(&keyword_authby_names, that_authby),
context);
return FALSE;
}
/* try to match ASN.1 blob designating the hash algorithm */
lset_t hn = ike->sa.st_hash_negotiated;
struct hash_alts {
lset_t neg;
const struct hash_desc *algo;
};
static const struct hash_alts ha[] = {
{ NEGOTIATE_AUTH_HASH_SHA2_512, &ike_alg_hash_sha2_512 },
{ NEGOTIATE_AUTH_HASH_SHA2_384, &ike_alg_hash_sha2_384 },
{ NEGOTIATE_AUTH_HASH_SHA2_256, &ike_alg_hash_sha2_256 },
/* { NEGOTIATE_AUTH_HASH_IDENTITY, IKEv2_HASH_ALGORITHM_IDENTITY }, */
};
const struct hash_alts *hap;
for (hap = ha; ; hap++) {
if (hap == &ha[elemsof(ha)]) {
log_state(RC_LOG, &ike->sa,
"no acceptable ECDSA/RSA-PSS ASN.1 signature hash proposal included for %s in %s",
enum_name(&keyword_authby_names, that_authby), context);
if (DBGP(DBG_BASE)) {
size_t dl = min(pbs_left(pbs),
(size_t) (ASN1_LEN_ALGO_IDENTIFIER +
PMAX(ASN1_SHA1_ECDSA_SIZE,
PMAX(ASN1_SHA2_RSA_PSS_SIZE,
ASN1_SHA2_ECDSA_SIZE))));
DBG_dump("offered blob", pbs->cur, dl);
}
return FALSE; /* none recognized */
}
if ((hn & hap->neg) && ikev2_try_asn1_hash_blob(hap->algo, pbs, that_authby))
break;
dbg("st_hash_negotiated policy does not match hash algorithm %s",
hap->algo->common.fqn);
}
/* try to match the hash */
stf_status authstat;
switch (that_authby) {
case AUTHBY_RSASIG:
authstat = ikev2_verify_rsa_hash(ike, idhash_in, pbs,
hap->algo);
break;
case AUTHBY_ECDSA:
authstat = ikev2_verify_ecdsa_hash(ike, idhash_in, pbs,
hap->algo);
break;
default:
bad_case(that_authby);
}
if (authstat != STF_OK) {
log_state(RC_LOG, &ike->sa,
"Digital Signature authentication using %s failed in %s",
enum_name(&keyword_authby_names, that_authby),
context);
return FALSE;
}
return TRUE;
}
default:
log_state(RC_LOG, &ike->sa,
"authentication method: %s not supported in %s",
enum_name(&ikev2_auth_names, recv_auth),
context);
return FALSE;
}
}
static bool id_ipseckey_allowed(struct state *st, enum ikev2_auth_method atype)
{
const struct connection *c = st->st_connection;
struct id id = st->st_connection->spd.that.id;
if (!c->spd.that.key_from_DNS_on_demand)
return FALSE;
if (c->spd.that.authby == AUTHBY_RSASIG &&
(id.kind == ID_FQDN || id_is_ipaddr(&id)))
{
switch (atype) {
case IKEv2_AUTH_RESERVED:
case IKEv2_AUTH_DIGSIG:
case IKEv2_AUTH_RSA:
return TRUE; /* success */
default:
break; /* failure */
}
}
if (DBGP(DBG_BASE)) {
const char *err1 = "%dnsondemand";
const char *err2 = "";
if (atype != IKEv2_AUTH_RESERVED && !(atype == IKEv2_AUTH_RSA ||
atype == IKEv2_AUTH_DIGSIG)) {
err1 = " initiator IKEv2 Auth Method mismatched ";
err2 = enum_name(&ikev2_auth_names, atype);
}
if (id.kind != ID_FQDN &&
id.kind != ID_IPV4_ADDR &&
id.kind != ID_IPV6_ADDR) {
err1 = " mismatched ID type, that ID is not a FQDN, IPV4_ADDR, or IPV6_ADDR id type=";
err2 = enum_show(&ike_idtype_names, id.kind);
}
id_buf thatid;
endpoint_buf ra;
DBG_log("%s #%lu not fetching ipseckey %s%s remote=%s thatid=%s",
c->name, st->st_serialno,
err1, err2,
str_endpoint(&st->st_remote_endpoint, &ra),
str_id(&id, &thatid));
}
return FALSE;
}
/*
*
***************************************************************
***** PARENT_OUTI1 *****
***************************************************************
*
*
* Initiate an Oakley Main Mode exchange.
* HDR, SAi1, KEi, Ni -->
*
* Note: this is not called from demux.c, but from ipsecdoi_initiate().
*
*/
static ke_and_nonce_cb ikev2_parent_outI1_continue;
/* extern initiator_function ikev2_parent_outI1; */ /* type assertion */
void ikev2_parent_outI1(struct fd *whack_sock,
struct connection *c,
struct state *predecessor,
lset_t policy,
unsigned long try,
const threadtime_t *inception,
chunk_t sec_label)
{
if (drop_new_exchanges()) {
/* Only drop outgoing opportunistic connections */
if (c->policy & POLICY_OPPORTUNISTIC) {
return;
}
}
const struct finite_state *fs = finite_states[STATE_PARENT_I0];
pexpect(fs->nr_transitions == 1);
const struct state_v2_microcode *transition = &fs->v2_transitions[0];
struct ike_sa *ike = new_v2_ike_state(c, transition, SA_INITIATOR,
ike_initiator_spi(), zero_ike_spi,
policy, try, whack_sock);
statetime_t start = statetime_backdate(&ike->sa, inception);
/* set up new state */
struct state *st = &ike->sa;
passert(st->st_ike_version == IKEv2);
passert(st->st_state->kind == STATE_PARENT_I0);
passert(st->st_sa_role == SA_INITIATOR);
st->st_try = try;
if (sec_label.len != 0) {
dbg("%s: received security label from acquire: \"%.*s\"", __FUNCTION__,
(int)sec_label.len, sec_label.ptr);
dbg("%s: connection security label: \"%.*s\"", __FUNCTION__,
(int)c->spd.this.sec_label.len, c->spd.this.sec_label.ptr);
/*
* Should we have a within_range() check here? In theory, the ACQUIRE came
* from a policy we gave the kernel, so it _should_ be within our range?
*/
st->st_acquired_sec_label = clone_hunk(sec_label, "st_acquired_sec_label");
}
if ((c->iketcp == IKE_TCP_ONLY) || (try > 1 && c->iketcp != IKE_TCP_NO)) {
dbg("TCP: forcing #%lu remote endpoint port to %d",
st->st_serialno, c->remote_tcpport);
update_endpoint_port(&st->st_remote_endpoint, ip_hport(c->remote_tcpport));
stf_status ret = create_tcp_interface(st);
if (ret != STF_OK) {
/* TCP: already logged? */
delete_state(st);
return;
}
}
if (HAS_IPSEC_POLICY(policy)) {
if (DBGP(DBG_BASE)) {
st->st_ts_this = ikev2_end_to_ts(&c->spd.this, st->st_acquired_sec_label);
st->st_ts_that = ikev2_end_to_ts(&c->spd.that, st->st_seen_sec_label);
ikev2_print_ts(&st->st_ts_this);
ikev2_print_ts(&st->st_ts_that);
}
add_pending(whack_sock, ike, c, policy, 1,
predecessor == NULL ? SOS_NOBODY : predecessor->st_serialno,
sec_label, true /*part of initiate*/);
}
/*
* XXX: why limit this log line to whack when opportunistic?
* This was, after all, triggered by something that happened
* at this end.
*/
enum stream logger = ((c->policy & POLICY_OPPORTUNISTIC) == LEMPTY) ? ALL_STREAMS : WHACK_STREAM;
if (predecessor != NULL) {
/*
* XXX: can PREDECESSOR be a child? Idle speculation
* would suggest it can: perhaps it's a state that
* hasn't yet emancipated, or the child from a must
* remain up connection.
*/
dbg("predecessor #%lu: %s SA; %s %s; %s",
predecessor->st_serialno,
IS_CHILD_SA(predecessor) ? "CHILD" : "IKE",
IS_V2_ESTABLISHED(predecessor->st_state) ? "established" : "establishing?",
enum_enum_name(&sa_type_names, predecessor->st_ike_version,
predecessor->st_establishing_sa),
predecessor->st_state->name);
log_state(logger | (RC_NEW_V2_STATE + STATE_PARENT_I1), &ike->sa,
"initiating IKEv2 connection to replace #%lu",
predecessor->st_serialno);
if (IS_V2_ESTABLISHED(predecessor->st_state)) {
if (IS_CHILD_SA(st))
st->st_ipsec_pred = predecessor->st_serialno;
else
st->st_ike_pred = predecessor->st_serialno;
}
update_pending(ike_sa(predecessor, HERE), pexpect_ike_sa(st));
} else {
log_state(logger | (RC_NEW_V2_STATE + STATE_PARENT_I1), &ike->sa,
"initiating IKEv2 connection");
}
if (IS_LIBUNBOUND && id_ipseckey_allowed(st, IKEv2_AUTH_RESERVED)) {
stf_status ret = idr_ipseckey_fetch(st);
if (ret != STF_OK) {
return;
}
}
/*
* Initialize st->st_oakley, including the group number.
* Grab the DH group from the first configured proposal and build KE.
*/
struct ikev2_proposals *ike_proposals =
get_v2_ike_proposals(c, "IKE SA initiator selecting KE", ike->sa.st_logger);
st->st_oakley.ta_dh = ikev2_proposals_first_dh(ike_proposals, ike->sa.st_logger);
if (st->st_oakley.ta_dh == NULL) {
log_state(RC_LOG, st, "proposals do not contain a valid DH");
delete_state(st); /* pops state? */
return;
}
/*
* Calculate KE and Nonce.
*/
submit_ke_and_nonce(st, st->st_oakley.ta_dh,
ikev2_parent_outI1_continue,
"ikev2_outI1 KE");
statetime_stop(&start, "%s()", __func__);
}
/*
* package up the calculated KE value, and emit it as a KE payload.
* used by IKEv2: parent, child (PFS)
*/
bool emit_v2KE(chunk_t *g, const struct dh_desc *group,
pb_stream *outs)
{
if (impair.ke_payload == IMPAIR_EMIT_OMIT) {
llog(RC_LOG, outs->outs_logger, "IMPAIR: omitting KE payload");
return true;
}
pb_stream kepbs;
struct ikev2_ke v2ke = {
.isak_group = group->common.id[IKEv2_ALG_ID],
};
if (!out_struct(&v2ke, &ikev2_ke_desc, outs, &kepbs))
return FALSE;
if (impair.ke_payload >= IMPAIR_EMIT_ROOF) {
uint8_t byte = impair.ke_payload - IMPAIR_EMIT_ROOF;
llog(RC_LOG, outs->outs_logger,
"IMPAIR: sending bogus KE (g^x) == %u value to break DH calculations", byte);
/* Only used to test sending/receiving bogus g^x */
diag_t d = pbs_out_repeated_byte(&kepbs, byte, g->len, "ikev2 impair KE (g^x) == 0");
if (d != NULL) {
log_diag(RC_LOG_SERIOUS, outs->outs_logger, &d, "%s", "");
return false;
}
} else if (impair.ke_payload == IMPAIR_EMIT_EMPTY) {
llog(RC_LOG, outs->outs_logger, "IMPAIR: sending an empty KE value");
diag_t d = pbs_out_zero(&kepbs, 0, "ikev2 impair KE (g^x) == empty");
if (d != NULL) {
log_diag(RC_LOG_SERIOUS, outs->outs_logger, &d, "%s", "");
return false;
}
} else {
if (!pbs_out_hunk(*g, &kepbs, "ikev2 g^x"))
return FALSE;
}
close_output_pbs(&kepbs);
return TRUE;
}
stf_status ikev2_parent_outI1_continue(struct state *st,
struct msg_digest *unused_md,
struct dh_local_secret *local_secret,
chunk_t *nonce)
{
dbg("%s() for #%lu %s",
__func__, st->st_serialno, st->st_state->name);
pexpect(unused_md == NULL);
struct ike_sa *ike = pexpect_ike_sa(st);
pexpect(ike->sa.st_sa_role == SA_INITIATOR);
/* I1 is from INVALID KE */
pexpect(st->st_state->kind == STATE_PARENT_I0 ||
st->st_state->kind == STATE_PARENT_I1);
unpack_KE_from_helper(st, local_secret, &st->st_gi);
unpack_nonce(&st->st_ni, nonce);
return record_v2_IKE_SA_INIT_request(ike) ? STF_OK : STF_INTERNAL_ERROR;
}
bool record_v2_IKE_SA_INIT_request(struct ike_sa *ike)
{
struct connection *c = ike->sa.st_connection;
/* set up reply */
struct pbs_out reply_stream = open_pbs_out("reply packet",
reply_buffer, sizeof(reply_buffer),
ike->sa.st_logger);
if (impair.send_bogus_dcookie) {
/* add or mangle a dcookie so what we will send is bogus */
DBG_log("Mangling dcookie because --impair-send-bogus-dcookie is set");
free_chunk_content(&ike->sa.st_dcookie);
ike->sa.st_dcookie.ptr = alloc_bytes(1, "mangled dcookie");
ike->sa.st_dcookie.len = 1;
messupn(ike->sa.st_dcookie.ptr, 1);
}
/* HDR out */
pb_stream rbody = open_v2_message(&reply_stream, ike, NULL /* request */,
ISAKMP_v2_IKE_SA_INIT);
if (!pbs_ok(&rbody)) {
return false;
}
/*
* https://tools.ietf.org/html/rfc5996#section-2.6
* reply with the anti DDOS cookie if we received one (remote is under attack)
*/
if (ike->sa.st_dcookie.ptr != NULL) {
/* In v2, for parent, protoid must be 0 and SPI must be empty */
if (!emit_v2N_hunk(v2N_COOKIE, ike->sa.st_dcookie, &rbody)) {
return false;
}
}
/* SA out */
struct ikev2_proposals *ike_proposals =
get_v2_ike_proposals(c, "IKE SA initiator emitting local proposals", ike->sa.st_logger);
if (!ikev2_emit_sa_proposals(&rbody, ike_proposals,
(chunk_t*)NULL /* IKE - no CHILD SPI */)) {
return false;
}
/* ??? from here on, this looks a lot like the end of ikev2_parent_inI1outR1_tail */
/* send KE */
if (!emit_v2KE(&ike->sa.st_gi, ike->sa.st_oakley.ta_dh, &rbody))
return false;
/* send NONCE */
{
pb_stream pb;
struct ikev2_generic in = {
.isag_critical = build_ikev2_critical(false, ike->sa.st_logger),
};
if (!out_struct(&in, &ikev2_nonce_desc, &rbody, &pb) ||
!pbs_out_hunk(ike->sa.st_ni, &pb, "IKEv2 nonce"))
return false;
close_output_pbs(&pb);
}
/* Send fragmentation support notification */
if (c->policy & POLICY_IKE_FRAG_ALLOW) {
if (!emit_v2N(v2N_IKEV2_FRAGMENTATION_SUPPORTED, &rbody))
return false;
}
/* Send USE_PPK Notify payload */
if (LIN(POLICY_PPK_ALLOW, c->policy)) {
if (!emit_v2N(v2N_USE_PPK, &rbody))
return false;
}
/* Send INTERMEDIATE_EXCHANGE_SUPPORTED Notify payload */
if (c->policy & POLICY_INTERMEDIATE) {
if (!emit_v2N(v2N_INTERMEDIATE_EXCHANGE_SUPPORTED, &rbody))
return STF_INTERNAL_ERROR;
}
/* first check if this IKE_SA_INIT came from redirect
* instruction.
* - if yes, send the v2N_REDIRECTED_FROM
* with the identity of previous gateway
* - if not, check if we support redirect mechanism
* and send v2N_REDIRECT_SUPPORTED if we do
*/
if (address_is_specified(&c->temp_vars.redirect_ip)) {
if (!emit_redirected_from_notification(&c->temp_vars.old_gw_address, &rbody))
return false;
} else if (LIN(POLICY_ACCEPT_REDIRECT_YES, c->policy)) {
if (!emit_v2N(v2N_REDIRECT_SUPPORTED, &rbody))
return false;
}
/* Send SIGNATURE_HASH_ALGORITHMS Notify payload */
if (!impair.omit_hash_notify_request) {
if (((c->policy & POLICY_RSASIG) || (c->policy & POLICY_ECDSA))
&& (c->sighash_policy != LEMPTY)) {
if (!emit_v2N_signature_hash_algorithms(c->sighash_policy, &rbody))
return false;
}
} else {
log_state(RC_LOG, &ike->sa,
"Impair: Skipping the Signature hash notify in IKE_SA_INIT Request");
}
/* Send NAT-T Notify payloads */
if (!ikev2_out_nat_v2n(&rbody, &ike->sa, &zero_ike_spi/*responder unknown*/))
return false;
/* From here on, only payloads left are Vendor IDs */
if (c->send_vendorid) {
if (!emit_v2V(pluto_vendorid, &rbody))
return false;
}
if (c->fake_strongswan) {
if (!emit_v2V("strongSwan", &rbody))
return false;
}
if (c->policy & POLICY_AUTH_NULL) {
if (!emit_v2V("Opportunistic IPsec", &rbody))
return STF_INTERNAL_ERROR;
}
close_output_pbs(&rbody);
close_output_pbs(&reply_stream);
/* save packet for later signing */
free_chunk_content(&ike->sa.st_firstpacket_me);
ike->sa.st_firstpacket_me = clone_out_pbs_as_chunk(&reply_stream,
"saved first packet");
/* Transmit */
record_v2_message(ike, &reply_stream, "IKE_SA_INIT request",
MESSAGE_REQUEST);
return true;
}
/*
*
***************************************************************
* PARENT_INI1 *****
***************************************************************
* -
*
*
*/
/* no state: none I1 --> R1
* <-- HDR, SAi1, KEi, Ni
* HDR, SAr1, KEr, Nr, [CERTREQ] -->
*/
static ke_and_nonce_cb ikev2_parent_inI1outR1_continue; /* forward decl and type assertion */
stf_status ikev2_parent_inI1outR1(struct ike_sa *ike,
struct child_sa *child,
struct msg_digest *md)
{
pexpect(child == NULL);
struct connection *c = ike->sa.st_connection;
/* set up new state */
update_ike_endpoints(ike, md);
passert(ike->sa.st_ike_version == IKEv2);
passert(ike->sa.st_state->kind == STATE_PARENT_R0);
passert(ike->sa.st_sa_role == SA_RESPONDER);
/* set by caller */
pexpect(md->svm == finite_states[STATE_PARENT_R0]->v2_transitions);
pexpect(md->svm->state == STATE_PARENT_R0);
/* Vendor ID processing */
for (struct payload_digest *v = md->chain[ISAKMP_NEXT_v2V]; v != NULL; v = v->next) {
handle_vendorid(md, (char *)v->pbs.cur, pbs_left(&v->pbs), TRUE, ike->sa.st_logger);
}
/* Get the proposals ready. */
struct ikev2_proposals *ike_proposals =
get_v2_ike_proposals(c, "IKE SA responder matching remote proposals", ike->sa.st_logger);
/*
* Select the proposal.
*/
stf_status ret = ikev2_process_sa_payload("IKE responder",
&md->chain[ISAKMP_NEXT_v2SA]->pbs,
/*expect_ike*/ TRUE,
/*expect_spi*/ FALSE,
/*expect_accepted*/ FALSE,
LIN(POLICY_OPPORTUNISTIC, c->policy),
&ike->sa.st_accepted_ike_proposal,
ike_proposals, ike->sa.st_logger);
if (ret != STF_OK) {
pexpect(ike->sa.st_sa_role == SA_RESPONDER);
pexpect(ret > STF_FAIL);
record_v2N_response(ike->sa.st_logger, ike, md,
ret - STF_FAIL, NULL,
UNENCRYPTED_PAYLOAD);
return STF_FAIL;
}
if (DBGP(DBG_BASE)) {
DBG_log_ikev2_proposal("accepted IKE proposal",
ike->sa.st_accepted_ike_proposal);
}
/*
* Convert what was accepted to internal form and apply some
* basic validation. If this somehow fails (it shouldn't but
* ...), drop everything.
*/
if (!ikev2_proposal_to_trans_attrs(ike->sa.st_accepted_ike_proposal,
&ike->sa.st_oakley, ike->sa.st_logger)) {
log_state(RC_LOG_SERIOUS, &ike->sa, "IKE responder accepted an unsupported algorithm");
/* STF_INTERNAL_ERROR doesn't delete ST */
return STF_FATAL;
}
/*
* Check the MODP group in the payload matches the accepted
* proposal.
*/
if (!v2_accept_ke_for_proposal(ike, &ike->sa, md,
ike->sa.st_oakley.ta_dh,
UNENCRYPTED_PAYLOAD)) {
/* pexpect(reply-recorded) */
return STF_FAIL;
}
/*
* Check and read the KE contents.
*/
/* note: v1 notification! */
if (!unpack_KE(&ike->sa.st_gi, "Gi", ike->sa.st_oakley.ta_dh,
md->chain[ISAKMP_NEXT_v2KE], ike->sa.st_logger)) {
send_v2N_response_from_md(md, v2N_INVALID_SYNTAX, NULL);
return STF_FATAL;
}
/* extract results */
ike->sa.st_seen_fragmentation_supported = md->pbs[PBS_v2N_IKEV2_FRAGMENTATION_SUPPORTED] != NULL;
ike->sa.st_seen_ppk = md->pbs[PBS_v2N_USE_PPK] != NULL;
ike->sa.st_seen_intermediate = md->pbs[PBS_v2N_INTERMEDIATE_EXCHANGE_SUPPORTED] != NULL;
ike->sa.st_seen_redirect_sup = (md->pbs[PBS_v2N_REDIRECTED_FROM] != NULL ||
md->pbs[PBS_v2N_REDIRECT_SUPPORTED] != NULL);
/*
* Responder: check v2N_NAT_DETECTION_DESTINATION_IP or/and
* v2N_NAT_DETECTION_SOURCE_IP.
*
* 2.23. NAT Traversal
*
* The IKE initiator MUST check the NAT_DETECTION_SOURCE_IP
* or NAT_DETECTION_DESTINATION_IP payloads if present, and
* if they do not match the addresses in the outer packet,
* MUST tunnel all future IKE and ESP packets associated
* with this IKE SA over UDP port 4500.
*
* Since this is the responder, there's really not much to do.
* It is the initiator that will switch to port 4500 (float
* away) when necessary.
*/
if (v2_nat_detected(ike, md)) {
dbg("NAT: responder so initiator gets to switch ports");
/* should this check that a port is available? */
}
if (md->pbs[PBS_v2N_SIGNATURE_HASH_ALGORITHMS] != NULL) {
if (impair.ignore_hash_notify_response) {
log_state(RC_LOG, &ike->sa, "IMPAIR: ignoring the hash notify in IKE_SA_INIT request");
} else if (!negotiate_hash_algo_from_notification(md->pbs[PBS_v2N_SIGNATURE_HASH_ALGORITHMS], ike)) {
return STF_FATAL;
}
ike->sa.st_seen_hashnotify = true;
}
/* calculate the nonce and the KE */
submit_ke_and_nonce(&ike->sa,
ike->sa.st_oakley.ta_dh,
ikev2_parent_inI1outR1_continue,
"ikev2_inI1outR1 KE");
return STF_SUSPEND;
}
static stf_status ikev2_parent_inI1outR1_continue(struct state *st,
struct msg_digest *md,
struct dh_local_secret *local_secret,
chunk_t *nonce)
{
dbg("%s() for #%lu %s: calculated ke+nonce, sending R1",
__func__, st->st_serialno, st->st_state->name);
pexpect(v2_msg_role(md) == MESSAGE_REQUEST); /* i.e., MD!=NULL */
pexpect(md->st == NULL || md->st == st);
struct ike_sa *ike = pexpect_ike_sa(st);
pexpect(ike->sa.st_sa_role == SA_RESPONDER);
pexpect(st->st_state->kind == STATE_PARENT_R0);
/*
* XXX: sanity check that this call does not screw around with
* MD.ST (it isn't creating a child, and can return STF_FATAL
* et.al.)
*/
md->st = st;
struct connection *c = st->st_connection;
bool send_certreq = FALSE;
/* note that we don't update the state here yet */
/*
* XXX:
*
* Should this code use clone_in_pbs_as_chunk() which uses
* pbs_room() (.roof-.start)? The original code:
*
* clonetochunk(st->st_firstpacket_peer, md->message_pbs.start,
* pbs_offset(&md->message_pbs),
* "saved first received packet");
*
* and clone_out_pbs_as_chunk() both use pbs_offset()
* (.cur-.start).
*
* Suspect it doesn't matter as the code initializing
* .message_pbs forces .roof==.cur - look for the comment
* "trim padding (not actually legit)".
*/
/* record first packet for later checking of signature */
free_chunk_content(&st->st_firstpacket_peer);
st->st_firstpacket_peer = clone_out_pbs_as_chunk(&md->message_pbs,
"saved first received packet in inI1outR1_continue_tail");
/* make sure HDR is at start of a clean buffer */
struct pbs_out reply_stream = open_pbs_out("reply packet",
reply_buffer, sizeof(reply_buffer),
ike->sa.st_logger);
/* HDR out */
pb_stream rbody = open_v2_message(&reply_stream, ike_sa(st, HERE),
md /* response */,
ISAKMP_v2_IKE_SA_INIT);
if (!pbs_ok(&rbody)) {
return STF_INTERNAL_ERROR;
}
/* start of SA out */
{
/*
* Since this is the initial IKE exchange, the SPI is
* emitted as part of the packet header and not as
* part of the proposal. Hence the NULL SPI.
*/
passert(st->st_accepted_ike_proposal != NULL);
if (!ikev2_emit_sa_proposal(&rbody, st->st_accepted_ike_proposal, NULL)) {
dbg("problem emitting accepted proposal");
return STF_INTERNAL_ERROR;
}
}
/* Ni in */
if (!accept_v2_nonce(st->st_logger, md, &st->st_ni, "Ni")) {
/*
* Presumably not our fault. Syntax errors kill the
* family, hence FATAL.
*/
record_v2N_response(ike->sa.st_logger, ike, md,
v2N_INVALID_SYNTAX, NULL/*no-data*/,
UNENCRYPTED_PAYLOAD);
return STF_FATAL;
}
/* ??? from here on, this looks a lot like the end of ikev2_parent_outI1_common */
/*
* Unpack and send KE
*
* Pass the crypto helper's oakley group so that it is
* consistent with what was unpacked.
*
* IKEv2 code (arguably, incorrectly) uses st_oakley.ta_dh to
* track the most recent KE sent out. It should instead be
* maintaining a list of KEs sent out (so that they can be
* reused should the initial responder flip-flop) and only set
* st_oakley.ta_dh once the proposal has been accepted.
*/
pexpect(st->st_oakley.ta_dh == dh_local_secret_desc(local_secret));
unpack_KE_from_helper(st, local_secret, &st->st_gr);
if (!emit_v2KE(&st->st_gr, dh_local_secret_desc(local_secret), &rbody)) {
return STF_INTERNAL_ERROR;
}
/* send NONCE */
unpack_nonce(&st->st_nr, nonce);
{
pb_stream pb;
struct ikev2_generic in = {
.isag_critical = build_ikev2_critical(false, st->st_logger),
};
if (!out_struct(&in, &ikev2_nonce_desc, &rbody, &pb) ||
!pbs_out_hunk(st->st_nr, &pb, "IKEv2 nonce"))
return STF_INTERNAL_ERROR;
close_output_pbs(&pb);
}
/* decide to send a CERTREQ - for RSASIG or GSSAPI */
send_certreq = (((c->policy & POLICY_RSASIG) &&
!has_preloaded_public_key(st))
);
/* Send fragmentation support notification */
if (c->policy & POLICY_IKE_FRAG_ALLOW) {
if (!emit_v2N(v2N_IKEV2_FRAGMENTATION_SUPPORTED, &rbody))
return STF_INTERNAL_ERROR;
}
/* Send USE_PPK Notify payload */
if (st->st_seen_ppk) {
if (!emit_v2N(v2N_USE_PPK, &rbody))
return STF_INTERNAL_ERROR;
}
/* Send INTERMEDIATE_EXCHANGE_SUPPORTED Notify payload */
if ((c->policy & POLICY_INTERMEDIATE) && ike->sa.st_seen_intermediate) {
if (!emit_v2N(v2N_INTERMEDIATE_EXCHANGE_SUPPORTED, &rbody))
return STF_INTERNAL_ERROR;
ike->sa.st_intermediate_used = true;
}
/* Send SIGNATURE_HASH_ALGORITHMS notification only if we received one */
if (!impair.ignore_hash_notify_request) {
if (st->st_seen_hashnotify && ((c->policy & POLICY_RSASIG) || (c->policy & POLICY_ECDSA))
&& (c->sighash_policy != LEMPTY)) {
if (!emit_v2N_signature_hash_algorithms(c->sighash_policy, &rbody))
return STF_INTERNAL_ERROR;
}
} else {
log_state(RC_LOG, st, "Impair: Not sending out signature hash notify");
}
/* Send NAT-T Notify payloads */
if (!ikev2_out_nat_v2n(&rbody, st, &st->st_ike_spis.responder)) {
return STF_INTERNAL_ERROR;
}
/* something the other end won't like */
/* send CERTREQ */
if (send_certreq) {
dbg("going to send a certreq");
ikev2_send_certreq(st, md, &rbody);
}
if (c->send_vendorid) {
if (!emit_v2V(pluto_vendorid, &rbody))
return STF_INTERNAL_ERROR;
}
if (c->fake_strongswan) {
if (!emit_v2V("strongSwan", &rbody))
return STF_INTERNAL_ERROR;
}
if (c->policy & POLICY_AUTH_NULL) {
if (!emit_v2V("Opportunistic IPsec", &rbody))
return STF_INTERNAL_ERROR;
}
close_output_pbs(&rbody);
close_output_pbs(&reply_stream);
record_v2_message(ike, &reply_stream,
"reply packet for ikev2_parent_inI1outR1_tail",
MESSAGE_RESPONSE);
/* save packet for later signing */
free_chunk_content(&st->st_firstpacket_me);
st->st_firstpacket_me = clone_out_pbs_as_chunk(&reply_stream,
"saved first packet");
/*
* sanity check nothing has screwed around with md.st.
*/
if (!pexpect(md->st == st)) {
st = md->st;
}
return STF_OK;
}
/*
*
***************************************************************
* PARENT_inR1 *****
***************************************************************
* -
*
*
*/
/* STATE_PARENT_I1: R1B --> I1B
* <-- HDR, N
* HDR, N(COOKIE), SAi1, KEi, Ni -->
*/
static stf_status resubmit_ke_and_nonce(struct ike_sa *ike)
{
submit_ke_and_nonce(&ike->sa, ike->sa.st_oakley.ta_dh,
ikev2_parent_outI1_continue,
"rekey outI");
return STF_SUSPEND;
}
stf_status process_IKE_SA_INIT_v2N_INVALID_KE_PAYLOAD_response(struct ike_sa *ike,
struct child_sa *child,
struct msg_digest *md)
{
struct connection *c = ike->sa.st_connection;
pexpect(child == NULL);
if (!pexpect(md->pbs[PBS_v2N_INVALID_KE_PAYLOAD] != NULL)) {
return STF_INTERNAL_ERROR;
}
struct pbs_in invalid_ke_pbs = *md->pbs[PBS_v2N_INVALID_KE_PAYLOAD];
/* careful of DDOS, only log with debugging on? */
/* we treat this as a "retransmit" event to rate limit these */
if (!count_duplicate(&ike->sa, MAXIMUM_INVALID_KE_RETRANS)) {
dbg("ignoring received INVALID_KE packets - received too many (DoS?)");
return STF_IGNORE;
}
/*
* There's at least this notify payload, is there more than
* one?
*/
if (md->chain[ISAKMP_NEXT_v2N]->next != NULL) {
dbg("ignoring other notify payloads");
}
struct suggested_group sg;
diag_t d = pbs_in_struct(&invalid_ke_pbs, &suggested_group_desc,
&sg, sizeof(sg), NULL);
if (d != NULL) {
log_diag(RC_LOG, ike->sa.st_logger, &d, "%s", "");
return STF_IGNORE;
}
pstats(invalidke_recv_s, sg.sg_group);
pstats(invalidke_recv_u, ike->sa.st_oakley.ta_dh->group);
struct ikev2_proposals *ike_proposals =
get_v2_ike_proposals(c, "IKE SA initiator validating remote's suggested KE", ike->sa.st_logger);
if (!ikev2_proposals_include_modp(ike_proposals, sg.sg_group)) {
struct esb_buf esb;
log_state(RC_LOG, &ike->sa,
"Discarding unauthenticated INVALID_KE_PAYLOAD response to DH %s; suggested DH %s is not acceptable",
ike->sa.st_oakley.ta_dh->common.fqn,
enum_show_shortb(&oakley_group_names,
sg.sg_group, &esb));
return STF_IGNORE;
}
dbg("Suggested modp group is acceptable");
/*
* Since there must be a group object for every local
* proposal, and sg.sg_group matches one of the local proposal
* groups, a lookup of sg.sg_group must succeed.
*/
const struct dh_desc *new_group = ikev2_get_dh_desc(sg.sg_group);
passert(new_group != NULL);
log_state(RC_LOG, &ike->sa,
"Received unauthenticated INVALID_KE_PAYLOAD response to DH %s; resending with suggested DH %s",
ike->sa.st_oakley.ta_dh->common.fqn,
new_group->common.fqn);
ike->sa.st_oakley.ta_dh = new_group;
/* wipe our mismatched KE */
dh_local_secret_delref(&ike->sa.st_dh_local_secret, HERE);
/*
* get a new KE
*/
schedule_reinitiate_v2_ike_sa_init(ike, resubmit_ke_and_nonce);
return STF_OK;
}
stf_status ikev2_auth_initiator_process_failure_notification(struct ike_sa *ike,
struct child_sa *child,
struct msg_digest *md)
{
/*
* XXX: ST here should be the IKE SA. The state machine,
* however, directs the AUTH response to the CHILD!
*/
pexpect(child != NULL);
struct state *st = &child->sa;
v2_notification_t n = md->svm->encrypted_payloads.notification;
pstat(ikev2_recv_notifies_e, n);
/*
* Always log the notification error and fail;
* but do it in slightly different ways so it
* is possible to figure out which code path
* was taken.
*/
log_state(RC_LOG, &ike->sa, "IKE SA authentication request rejected by peer: %s",
enum_short_name(&ikev2_notify_names, n));
/*
* XXX: ST here should be the IKE SA. The state machine,
* however, directs the AUTH response to the CHILD! Find the
* IKE SA and mark it as failing.
*/
pstat_sa_failed(&ike->sa, REASON_AUTH_FAILED);
/*
* 2.21.2. Error Handling in IKE_AUTH
*
* ... If the error occurred on the responder, the
* notification is returned in the protected response, and is usually
* the only payload in that response. Although the IKE_AUTH messages
* are encrypted and integrity protected, if the peer receiving this
* notification has not authenticated the other end yet, that peer needs
* to treat the information with caution.
*
* So assume MITM and schedule a retry.
*/
if (ikev2_schedule_retry(st)) {
return STF_IGNORE; /* drop packet */
} else {
return STF_FATAL;
}
}
stf_status ikev2_auth_initiator_process_unknown_notification(struct ike_sa *unused_ike UNUSED,
struct child_sa *child,
struct msg_digest *md)
{
/*
* XXX: ST here should be the IKE SA. The state machine,
* however, directs the AUTH response to the CHILD!
*/
pexpect(child != NULL);
struct state *st = &child->sa;
/*
* 3.10.1. Notify Message Types:
*
* Types in the range 0 - 16383 are intended for reporting errors. An
* implementation receiving a Notify payload with one of these types
* that it does not recognize in a response MUST assume that the
* corresponding request has failed entirely. Unrecognized error types
* in a request and status types in a request or response MUST be
* ignored, and they should be logged.
*/
bool ignore = true;
for (struct payload_digest *ntfy = md->chain[ISAKMP_NEXT_v2N]; ntfy != NULL; ntfy = ntfy->next) {
v2_notification_t n = ntfy->payload.v2n.isan_type;
const char *name = enum_short_name(&ikev2_notify_names, n);
if (ntfy->payload.v2n.isan_spisize != 0) {
/* invalid-syntax, but can't do anything about it */
log_state(RC_LOG, st, "received an encrypted %s notification with an unexpected non-empty SPI; deleting IKE SA",
name);
return STF_FATAL;
}
if (n >= v2N_STATUS_FLOOR) {
/* just log */
pstat(ikev2_recv_notifies_s, n);
if (name == NULL) {
log_state(RC_LOG, st, "IKE_AUTH response contained an unknown status notification (%d)", n);
} else {
log_state(RC_LOG, st, "IKE_AUTH response contained the status notification %s", name);
}
} else {
pstat(ikev2_recv_notifies_e, n);
ignore = false;
if (name == NULL) {
log_state(RC_LOG, st, "IKE_AUTH response contained an unknown error notification (%d)", n);
} else {
log_state(RC_LOG, st, "IKE_AUTH response contained the error notification %s", name);
/*
* There won't be a child state transition, so log if error is child related.
* see RFC 7296 Section 1.2
*/
switch(n) {
case v2N_NO_PROPOSAL_CHOSEN:
case v2N_SINGLE_PAIR_REQUIRED:
case v2N_NO_ADDITIONAL_SAS:
case v2N_INTERNAL_ADDRESS_FAILURE:
case v2N_FAILED_CP_REQUIRED:
case v2N_TS_UNACCEPTABLE:
case v2N_INVALID_SELECTORS:
/* fallthrough */
linux_audit_conn(st, LAK_CHILD_FAIL);
break;
default:
break;
}
}
}
}
if (ignore) {
return STF_IGNORE;
}
/*
* 2.21.2. Error Handling in IKE_AUTH
*
* ... If the error occurred on the responder, the
* notification is returned in the protected response, and is usually
* the only payload in that response. Although the IKE_AUTH messages
* are encrypted and integrity protected, if the peer receiving this
* notification has not authenticated the other end yet, that peer needs
* to treat the information with caution.
*
* So assume MITM and schedule a retry.
*/
if (ikev2_schedule_retry(st)) {
return STF_IGNORE; /* drop packet */
} else {
return STF_FATAL;
}
}
/* STATE_PARENT_I1: R1 --> I2
* <-- HDR, SAr1, KEr, Nr, [CERTREQ]
* HDR, SK {IDi, [CERT,] [CERTREQ,]
* [IDr,] AUTH, SAi2,
* TSi, TSr} -->
*/
static dh_shared_secret_cb ikev2_parent_inR1outI2_continue; /* forward decl and type assertion */
static dh_shared_secret_cb ikev2_parent_inR1out_intermediate; /* forward decl and type assertion */
stf_status ikev2_parent_inR1outI2(struct ike_sa *ike,
struct child_sa *unused_child UNUSED,
struct msg_digest *md)
{
struct state *st = &ike->sa;
struct connection *c = st->st_connection;
/* for testing only */
if (impair.send_no_ikev2_auth) {
log_state(RC_LOG, &ike->sa,
"IMPAIR_SEND_NO_IKEV2_AUTH set - not sending IKE_AUTH packet");
return STF_IGNORE;
}
/*
* if this connection has a newer Child SA than this state
* this negotiation is not relevant any more. would this
* cover if there are multiple CREATE_CHILD_SA pending on this
* IKE negotiation ???
*
* XXX: this is testing for an IKE SA that's been superseed by
* a newer IKE SA (not child). Suspect this is to handle a
* race where the other end brings up the IKE SA first? For
* that case, shouldn't this state have been deleted?
*/
if (c->newest_ipsec_sa > st->st_serialno) {
log_state(RC_LOG, &ike->sa,
"state superseded by #%lu try=%lu, drop this negotiation",
c->newest_ipsec_sa, st->st_try);
return STF_FATAL;
}
if (md->hdr.isa_xchg != ISAKMP_v2_IKE_INTERMEDIATE) {
/*
* XXX: this iteration over the notifies modifies state
* _before_ the code's committed to creating an SA. Hack this
* by resetting any flags that might be set.
*/
ike->sa.st_seen_fragmentation_supported = false;
ike->sa.st_seen_ppk = false;
ike->sa.st_seen_intermediate = false;
ike->sa.st_seen_fragmentation_supported = md->pbs[PBS_v2N_IKEV2_FRAGMENTATION_SUPPORTED] != NULL;
ike->sa.st_seen_ppk = md->pbs[PBS_v2N_USE_PPK] != NULL;
ike->sa.st_seen_intermediate = md->pbs[PBS_v2N_INTERMEDIATE_EXCHANGE_SUPPORTED] != NULL;
if (md->pbs[PBS_v2N_SIGNATURE_HASH_ALGORITHMS] != NULL) {
if (impair.ignore_hash_notify_request) {
log_state(RC_LOG, &ike->sa,
"IMPAIR: ignoring the Signature hash notify in IKE_SA_INIT response");
} else if (!negotiate_hash_algo_from_notification(md->pbs[PBS_v2N_SIGNATURE_HASH_ALGORITHMS], ike)) {
return STF_FATAL;
}
ike->sa.st_seen_hashnotify = true;
}
/*
* the responder sent us back KE, Gr, Nr, and it's our time to calculate
* the shared key values.
*/
dbg("ikev2 parent inR1: calculating g^{xy} in order to send I2");
/* KE in */
if (!unpack_KE(&st->st_gr, "Gr", st->st_oakley.ta_dh,
md->chain[ISAKMP_NEXT_v2KE], st->st_logger)) {
/*
* XXX: Initiator - so this code will not trigger a
* notify. Since packet isn't trusted, should it be
* ignored?
*/
return STF_FAIL + v2N_INVALID_SYNTAX;
}
/* Ni in */
if (!accept_v2_nonce(st->st_logger, md, &st->st_nr, "Nr")) {
/*
* Presumably not our fault. Syntax errors in a
* response kill the family (and trigger no further
* exchange).
*/
return STF_FATAL;
}
/* We're missing processing a CERTREQ in here */
/* process and confirm the SA selected */
{
/* SA body in and out */
struct payload_digest *const sa_pd =
md->chain[ISAKMP_NEXT_v2SA];
struct ikev2_proposals *ike_proposals =
get_v2_ike_proposals(c, "IKE SA initiator accepting remote proposal", ike->sa.st_logger);
stf_status ret = ikev2_process_sa_payload("IKE initiator (accepting)",
&sa_pd->pbs,
/*expect_ike*/ TRUE,
/*expect_spi*/ FALSE,
/*expect_accepted*/ TRUE,
LIN(POLICY_OPPORTUNISTIC, c->policy),
&st->st_accepted_ike_proposal,
ike_proposals, ike->sa.st_logger);
if (ret != STF_OK) {
dbg("ikev2_parse_parent_sa_body() failed in ikev2_parent_inR1outI2()");
return ret; /* initiator; no response */
}
if (!ikev2_proposal_to_trans_attrs(st->st_accepted_ike_proposal,
&st->st_oakley, ike->sa.st_logger)) {
log_state(RC_LOG_SERIOUS, st, "IKE initiator proposed an unsupported algorithm");
free_ikev2_proposal(&st->st_accepted_ike_proposal);
passert(st->st_accepted_ike_proposal == NULL);
/*
* Assume caller et.al. will clean up the
* reset of the mess?
*/
return STF_FAIL;
}
}
free_chunk_content(&st->st_firstpacket_peer);
st->st_firstpacket_peer = clone_out_pbs_as_chunk(&md->message_pbs,
"saved first received packet in inR1outI2");
} else {
dbg("No KE payload in INTERMEDIATE RESPONSE, not calculating keys, going to AUTH by completing state transition");
}
/*
* Initiator: check v2N_NAT_DETECTION_DESTINATION_IP or/and
* v2N_NAT_DETECTION_SOURCE_IP.
*
* 2.23. NAT Traversal
*
* The IKE initiator MUST check the NAT_DETECTION_SOURCE_IP
* or NAT_DETECTION_DESTINATION_IP payloads if present, and
* if they do not match the addresses in the outer packet,
* MUST tunnel all future IKE and ESP packets associated
* with this IKE SA over UDP port 4500.
*
* When detected, float to the NAT port as needed (*ikeport
* can't float but already supports NAT). When the ports
* can't support NAT, give up.
*/
if (v2_nat_detected(ike, md)) {
pexpect(ike->sa.hidden_variables.st_nat_traversal & NAT_T_DETECTED);
if (!v2_natify_initiator_endpoints(ike, HERE)) {
/* already logged */
return STF_FATAL;
}
}
/*
* Initiate the calculation of g^xy.
*
* Form and pass in the full SPI[ir] that will eventually be
* used by this IKE SA. Only once DH has been computed and
* the SA is secure (but not authenticated) should the state's
* IKE SPIr be updated.
*/
if (!(md->hdr.isa_xchg == ISAKMP_v2_IKE_INTERMEDIATE)){
pexpect(ike_spi_is_zero(&ike->sa.st_ike_spis.responder));
}
ike->sa.st_ike_rekey_spis = (ike_spis_t) {
.initiator = ike->sa.st_ike_spis.initiator,
.responder = md->hdr.isa_ike_responder_spi,
};
/* If we seen the intermediate AND we are configured to use intermediate */
/* for now, do only one Intermediate Exchange round and proceed with IKE_AUTH */
dh_shared_secret_cb (*pcrc_func) = (ike->sa.st_seen_intermediate && (md->pbs[PBS_v2N_INTERMEDIATE_EXCHANGE_SUPPORTED] != NULL) && !(md->hdr.isa_xchg == ISAKMP_v2_IKE_INTERMEDIATE)) ?
ikev2_parent_inR1out_intermediate : ikev2_parent_inR1outI2_continue;
submit_dh_shared_secret(st, st->st_gr/*initiator needs responder KE*/,
pcrc_func, HERE);
return STF_SUSPEND;
}
static stf_status ikev2_parent_inR1out_intermediate(struct state *st,
struct msg_digest *mdp)
{
dbg("%s() for #%lu %s: g^{xy} calculated, sending INTERMEDIATE",
__func__, st->st_serialno, st->st_state->name);
pexpect(v2_msg_role(mdp) == MESSAGE_RESPONSE); /* i.e., MD!=NULL */
pexpect(mdp->st == NULL || mdp->st == st);
struct ike_sa *ike = pexpect_ike_sa(st);
pexpect(ike->sa.st_sa_role == SA_INITIATOR);
ike->sa.st_intermediate_used = true;
if (st->st_dh_shared_secret == NULL) {
/*
* XXX: this is the initiator so returning a
* notification is kind of useless.
*/
pstat_sa_failed(st, REASON_CRYPTO_FAILED);
return STF_FAIL;
}
calc_v2_keymat(st,
NULL, NULL, /*previous keymat*/
&st->st_ike_rekey_spis);
/*
* All systems are go.
*
* Since DH succeeded, a secure (but unauthenticated) SA
* (channel) is available. From this point on, should things
* go south, the state needs to be abandoned (but it shouldn't
* happen).
*/
/*
* Since systems are go, start updating the state, starting
* with SPIr.
*/
rehash_state(&ike->sa, &mdp->hdr.isa_ike_responder_spi);
/* beginning of data going out */
/* make sure HDR is at start of a clean buffer */
struct pbs_out reply_stream = open_pbs_out("reply packet",
reply_buffer, sizeof(reply_buffer),
ike->sa.st_logger);
/* HDR out */
pb_stream rbody = open_v2_message(&reply_stream, ike_sa(st, HERE),
NULL /* request */,
ISAKMP_v2_IKE_INTERMEDIATE);
if (!pbs_ok(&rbody)) {
return STF_INTERNAL_ERROR;
}
/* insert an Encryption payload header (SK) */
v2SK_payload_t sk = open_v2SK_payload(ike->sa.st_logger, &rbody, ike_sa(st, HERE));
if (!pbs_ok(&sk.pbs)) {
return STF_INTERNAL_ERROR;
}
/* send NOTIFY payload */
if (st->st_seen_intermediate) {
if (!emit_v2N(v2N_INTERMEDIATE_EXCHANGE_SUPPORTED, &sk.pbs))
return STF_INTERNAL_ERROR;
}
if (!close_v2SK_payload(&sk)) {
return STF_INTERNAL_ERROR;
}
close_output_pbs(&rbody);
close_output_pbs(&reply_stream);
stf_status ret = encrypt_v2SK_payload(&sk);
if (ret != STF_OK) {
return ret;
}
record_v2_message(ike, &reply_stream, "reply packet for intermediate exchange",
MESSAGE_REQUEST);
dbg_v2_msgid(ike, st, "XXX: in %s() hacking around record'n'send bypassing send queue",
__func__);
return STF_OK;
}
/* Misleading name, also used for NULL sized type's */
static stf_status ikev2_ship_cp_attr_ip(uint16_t type, ip_address *ip,
const char *story, pb_stream *outpbs)
{
pb_stream a_pbs;
struct ikev2_cp_attribute attr;
attr.type = type;
if (ip == NULL) {
attr.len = 0;
} else {
if (address_type(ip)->af == AF_INET)
attr.len = address_type(ip)->ip_size;
else
attr.len = INTERNAL_IP6_ADDRESS_SIZE; /* RFC hack to append IPv6 prefix len */
}
if (!out_struct(&attr, &ikev2_cp_attribute_desc, outpbs,
&a_pbs))
return STF_INTERNAL_ERROR;
if (attr.len > 0) {
diag_t d = pbs_out_address(&a_pbs, ip, story);
if (d != NULL) {
log_diag(RC_LOG_SERIOUS, a_pbs.outs_logger, &d, "%s", "");
return STF_INTERNAL_ERROR;
}
}
if (attr.len == INTERNAL_IP6_ADDRESS_SIZE) { /* IPv6 address add prefix */
uint8_t ipv6_prefix_len = INTERNL_IP6_PREFIX_LEN;
diag_t d = pbs_out_raw(&a_pbs, &ipv6_prefix_len, sizeof(uint8_t), "INTERNL_IP6_PREFIX_LEN");
if (d != NULL) {
log_diag(RC_LOG_SERIOUS, outpbs->outs_logger, &d, "%s", "");
return STF_INTERNAL_ERROR;
}
}
close_output_pbs(&a_pbs);
return STF_OK;
}
static stf_status ikev2_ship_cp_attr_str(uint16_t type, char *str,
const char *story, pb_stream *outpbs)
{
pb_stream a_pbs;
struct ikev2_cp_attribute attr = {
.type = type,
.len = (str == NULL) ? 0 : strlen(str),
};
if (!out_struct(&attr, &ikev2_cp_attribute_desc, outpbs,
&a_pbs))
return STF_INTERNAL_ERROR;
if (attr.len > 0) {
diag_t d = pbs_out_raw(&a_pbs, str, attr.len, story);
if (d != NULL) {
log_diag(RC_LOG_SERIOUS, outpbs->outs_logger, &d, "%s", "");
return STF_INTERNAL_ERROR;
}
}
close_output_pbs(&a_pbs);
return STF_OK;
}
/*
* CHILD is asking for configuration; hence log against child.
*/
bool emit_v2_child_configuration_payload(struct connection *c,
struct child_sa *child,
pb_stream *outpbs)
{
pb_stream cp_pbs;
bool cfg_reply = c->spd.that.has_lease;
struct ikev2_cp cp = {
.isacp_critical = ISAKMP_PAYLOAD_NONCRITICAL,
.isacp_type = cfg_reply ? IKEv2_CP_CFG_REPLY : IKEv2_CP_CFG_REQUEST,
};
dbg("Send Configuration Payload %s ",
cfg_reply ? "reply" : "request");
if (!out_struct(&cp, &ikev2_cp_desc, outpbs, &cp_pbs))
return false;
if (cfg_reply) {
ip_address that_client_address = subnet_prefix(&c->spd.that.client);
ikev2_ship_cp_attr_ip(subnet_type(&c->spd.that.client) == &ipv4_info ?
IKEv2_INTERNAL_IP4_ADDRESS : IKEv2_INTERNAL_IP6_ADDRESS,
&that_client_address, "Internal IP Address", &cp_pbs);
if (c->modecfg_dns != NULL) {
char *ipstr;
ipstr = strtok(c->modecfg_dns, ", ");
while (ipstr != NULL) {
if (strchr(ipstr, '.') != NULL) {
ip_address ip;
err_t e = ttoaddr_num(ipstr, 0, AF_INET, &ip);
if (e != NULL) {
log_state(RC_LOG_SERIOUS, &child->sa,
"Ignored bogus DNS IP address '%s'", ipstr);
} else {
if (ikev2_ship_cp_attr_ip(IKEv2_INTERNAL_IP4_DNS, &ip,
"IP4_DNS", &cp_pbs) != STF_OK)
return false;
}
} else if (strchr(ipstr, ':') != NULL) {
ip_address ip;
err_t e = ttoaddr_num(ipstr, 0, AF_INET6, &ip);
if (e != NULL) {
log_state(RC_LOG_SERIOUS, &child->sa,
"Ignored bogus DNS IP address '%s'", ipstr);
} else {
if (ikev2_ship_cp_attr_ip(IKEv2_INTERNAL_IP6_DNS, &ip,
"IP6_DNS", &cp_pbs) != STF_OK)
return false;
}
} else {
log_state(RC_LOG_SERIOUS, &child->sa,
"Ignored bogus DNS IP address '%s'", ipstr);
}
ipstr = strtok(NULL, ", ");
}
}
if (c->modecfg_domains != NULL) {
char *domain;
domain = strtok(c->modecfg_domains, ", ");
while (domain != NULL) {
if (ikev2_ship_cp_attr_str(IKEv2_INTERNAL_DNS_DOMAIN, domain,
"IKEv2_INTERNAL_DNS_DOMAIN", &cp_pbs) != STF_OK)
return false;
domain = strtok(NULL, ", ");
}
}
} else { /* cfg request */
ikev2_ship_cp_attr_ip(IKEv2_INTERNAL_IP4_ADDRESS, NULL, "IPV4 Address", &cp_pbs);
ikev2_ship_cp_attr_ip(IKEv2_INTERNAL_IP4_DNS, NULL, "DNSv4", &cp_pbs);
ikev2_ship_cp_attr_ip(IKEv2_INTERNAL_IP6_ADDRESS, NULL, "IPV6 Address", &cp_pbs);
ikev2_ship_cp_attr_ip(IKEv2_INTERNAL_IP6_DNS, NULL, "DNSv6", &cp_pbs);
ikev2_ship_cp_attr_ip(IKEv2_INTERNAL_DNS_DOMAIN, NULL, "Domain", &cp_pbs);
}
close_output_pbs(&cp_pbs);
return true;
}
static bool need_configuration_payload(const struct connection *const pc,
const lset_t st_nat_traversal)
{
return (pc->spd.this.modecfg_client &&
(!pc->spd.this.cat || LHAS(st_nat_traversal, NATED_HOST)));
}
static struct crypt_mac v2_hash_id_payload(const char *id_name, struct ike_sa *ike,
const char *key_name, PK11SymKey *key)
{
/*
* InitiatorIDPayload = PayloadHeader | RestOfInitIDPayload
* RestOfInitIDPayload = IDType | RESERVED | InitIDData
* MACedIDForR = prf(SK_pr, RestOfInitIDPayload)
*/
struct crypt_prf *id_ctx = crypt_prf_init_symkey(id_name, ike->sa.st_oakley.ta_prf,
key_name, key, ike->sa.st_logger);
/* skip PayloadHeader; hash: IDType | RESERVED */
crypt_prf_update_bytes(id_ctx, "IDType", &ike->sa.st_v2_id_payload.header.isai_type,
sizeof(ike->sa.st_v2_id_payload.header.isai_type));
/* note that res1+res2 is 3 zero bytes */
crypt_prf_update_byte(id_ctx, "RESERVED 1", ike->sa.st_v2_id_payload.header.isai_res1);
crypt_prf_update_byte(id_ctx, "RESERVED 2", ike->sa.st_v2_id_payload.header.isai_res2);
crypt_prf_update_byte(id_ctx, "RESERVED 3", ike->sa.st_v2_id_payload.header.isai_res3);
/* hash: InitIDData */
crypt_prf_update_hunk(id_ctx, "InitIDData", ike->sa.st_v2_id_payload.data);
return crypt_prf_final_mac(&id_ctx, NULL/*no-truncation*/);
}
static struct crypt_mac v2_id_hash(struct ike_sa *ike, const char *why,
const char *id_name, shunk_t id_payload,
const char *key_name, PK11SymKey *key)
{
const uint8_t *id_start = id_payload.ptr;
size_t id_size = id_payload.len;
/* HASH of ID is not done over common header */
id_start += NSIZEOF_isakmp_generic;
id_size -= NSIZEOF_isakmp_generic;
struct crypt_prf *id_ctx = crypt_prf_init_symkey(why, ike->sa.st_oakley.ta_prf,
key_name, key, ike->sa.st_logger);
crypt_prf_update_bytes(id_ctx, id_name, id_start, id_size);
return crypt_prf_final_mac(&id_ctx, NULL/*no-truncation*/);
}
static stf_status ikev2_parent_inR1outI2_auth_signature_continue(struct ike_sa *ike,
struct msg_digest *md,
const struct hash_signature *sig);
static stf_status ikev2_parent_inR1outI2_continue(struct state *st,
struct msg_digest *md)
{
dbg("%s() for #%lu %s: g^{xy} calculated, sending I2",
__func__, st->st_serialno, st->st_state->name);
pexpect(v2_msg_role(md) == MESSAGE_RESPONSE); /* i.e., MD!=NULL */
pexpect(md->st == NULL || md->st == st);
struct ike_sa *ike = pexpect_ike_sa(st);
pexpect(ike->sa.st_sa_role == SA_INITIATOR);
struct state *pst = &ike->sa; /* TBD: hack-to-avoid-code-churn */
struct connection *const pc = pst->st_connection; /* parent connection */
if (!(md->hdr.isa_xchg == ISAKMP_v2_IKE_INTERMEDIATE)) {
if (st->st_dh_shared_secret == NULL) {
/*
* XXX: this is the initiator so returning a
* notification is kind of useless.
*/
pstat_sa_failed(pst, REASON_CRYPTO_FAILED);
return STF_FAIL;
}
calc_v2_keymat(st,
NULL, NULL, /*no old keymat*/
&st->st_ike_rekey_spis);
}
/*
* All systems are go.
*
* Since DH succeeded, a secure (but unauthenticated) SA
* (channel) is available. From this point on, should things
* go south, the state needs to be abandoned (but it shouldn't
* happen).
*/
/*
* Since systems are go, start updating the state, starting
* with SPIr.
*/
rehash_state(&ike->sa, &md->hdr.isa_ike_responder_spi);
/*
* If we and responder are willing to use a PPK, we need to
* generate NO_PPK_AUTH as well as PPK-based AUTH payload.
*
* Stash the no-ppk keys in st_skey_*_no_ppk, and then
* scramble the st_skey_* keys with PPK.
*/
if (LIN(POLICY_PPK_ALLOW, pc->policy) && ike->sa.st_seen_ppk) {
chunk_t *ppk_id;
chunk_t *ppk = get_connection_ppk(ike->sa.st_connection, &ppk_id,
ike->sa.st_logger);
if (ppk != NULL) {
dbg("found PPK and PPK_ID for our connection");
pexpect(ike->sa.st_sk_d_no_ppk == NULL);
ike->sa.st_sk_d_no_ppk = reference_symkey(__func__, "sk_d_no_ppk", ike->sa.st_skey_d_nss);
pexpect(ike->sa.st_sk_pi_no_ppk == NULL);
ike->sa.st_sk_pi_no_ppk = reference_symkey(__func__, "sk_pi_no_ppk", ike->sa.st_skey_pi_nss);
pexpect(ike->sa.st_sk_pr_no_ppk == NULL);
ike->sa.st_sk_pr_no_ppk = reference_symkey(__func__, "sk_pr_no_ppk", ike->sa.st_skey_pr_nss);
ppk_recalculate(ppk, ike->sa.st_oakley.ta_prf,
&ike->sa.st_skey_d_nss,
&ike->sa.st_skey_pi_nss,
&ike->sa.st_skey_pr_nss,
ike->sa.st_logger);
log_state(RC_LOG, st, "PPK AUTH calculated as initiator");
} else {
if (pc->policy & POLICY_PPK_INSIST) {
log_state(RC_LOG_SERIOUS, &ike->sa,
"connection requires PPK, but we didn't find one");
return STF_FATAL;
} else {
log_state(RC_LOG, &ike->sa,
"failed to find PPK and PPK_ID, continuing without PPK");
/* we should omit sending any PPK Identity, so we pretend we didn't see USE_PPK */
ike->sa.st_seen_ppk = FALSE;
}
}
}
/*
* Construct the IDi payload and store it in state so that it
* can be emitted later. Then use that to construct the
* "MACedIDFor[I]".
*
* Code assumes that struct ikev2_id's "IDType|RESERVED" is
* laid out the same as the packet.
*/
{
shunk_t data;
ike->sa.st_v2_id_payload.header = build_v2_id_payload(&pc->spd.this, &data,
"my IDi", ike->sa.st_logger);
ike->sa.st_v2_id_payload.data = clone_hunk(data, "my IDi");
}
ike->sa.st_v2_id_payload.mac = v2_hash_id_payload("IDi", ike,
"st_skey_pi_nss",
ike->sa.st_skey_pi_nss);
if (pst->st_seen_ppk && !LIN(POLICY_PPK_INSIST, pc->policy)) {
/* ID payload that we've build is the same */
ike->sa.st_v2_id_payload.mac_no_ppk_auth =
v2_hash_id_payload("IDi (no-PPK)", ike,
"sk_pi_no_pkk",
ike->sa.st_sk_pi_no_ppk);
}
{
enum keyword_authby authby = v2_auth_by(ike);
enum ikev2_auth_method auth_method = v2_auth_method(ike, authby);
switch (auth_method) {
case IKEv2_AUTH_RSA:
{
const struct hash_desc *hash_algo = &ike_alg_hash_sha1;
struct crypt_mac hash_to_sign =
v2_calculate_sighash(ike, &ike->sa.st_v2_id_payload.mac,
hash_algo, LOCAL_PERSPECTIVE);
if (!submit_v2_auth_signature(ike, &hash_to_sign, hash_algo,
authby, auth_method,
ikev2_parent_inR1outI2_auth_signature_continue)) {
dbg("submit_v2_auth_signature() died, fatal");
return STF_FATAL;
}
return STF_SUSPEND;
}
case IKEv2_AUTH_DIGSIG:
{
const struct hash_desc *hash_algo = v2_auth_negotiated_signature_hash(ike);
if (hash_algo == NULL) {
return STF_FATAL;
}
struct crypt_mac hash_to_sign =
v2_calculate_sighash(ike, &ike->sa.st_v2_id_payload.mac,
hash_algo, LOCAL_PERSPECTIVE);
if (!submit_v2_auth_signature(ike, &hash_to_sign, hash_algo,
authby, auth_method,
ikev2_parent_inR1outI2_auth_signature_continue)) {
dbg("submit_v2_auth_signature() died, fatal");
return STF_FATAL;
}
return STF_SUSPEND;
}
case IKEv2_AUTH_PSK:
case IKEv2_AUTH_NULL:
{
struct hash_signature sig = { .len = 0, };
return ikev2_parent_inR1outI2_auth_signature_continue(ike, md, &sig);
}
default:
log_state(RC_LOG, &ike->sa,
"authentication method %s not supported",
enum_name(&ikev2_auth_names, auth_method));
return STF_FATAL;
}
}
}
static stf_status ikev2_parent_inR1outI2_auth_signature_continue(struct ike_sa *ike,
struct msg_digest *md,
const struct hash_signature *auth_sig)
{
struct connection *const pc = ike->sa.st_connection; /* parent connection */
ikev2_log_parentSA(&ike->sa);
/*
* XXX This is too early and many failures could lead to not
* needing a child state.
*
* XXX: The problem isn't so much that the child state is
* created - it provides somewhere to store all the child's
* state - but that things switch to the child before the IKE
* SA is finished. Consequently, code is forced to switch
* back to the IKE SA.
*
* Start with the CHILD SA bound to the same whackfd as it IKE
* SA. It might later change when its discovered that the
* child is for something pending?
*/
struct child_sa *child = new_v2_child_state(ike->sa.st_connection,
ike, IPSEC_SA,
SA_INITIATOR,
STATE_V2_IKE_AUTH_CHILD_I0,
ike->sa.st_logger->object_whackfd);
/* XXX because the early child state ends up with the try counter check, we need to copy it */
child->sa.st_try = ike->sa.st_try;
/*
* XXX: This is so lame. Need to move the current initiator
* from IKE to the CHILD so that the post processor doesn't
* get confused. If the IKE->CHILD switch didn't happen this
* wouldn't be needed.
*/
v2_msgid_switch_initiator(ike, child, md);
binlog_refresh_state(&child->sa);
switch_md_st(md, &child->sa, HERE);
/*
* XXX: Danger!
*
* Because the code above has blatted MD->ST with the child
* state (CST) and this function's caller is going to try to
* complete the V2 state transition on MD->ST (i.e., CST) and
* using the state-transition MD->SVM the IKE SA (PST) will
* never get to complete its state transition.
*
* Get around this by forcing the state transition here.
*
* But what should happen? A guess is to just leave MD->ST
* alone. The CHILD SA doesn't really exist until after the
* IKE SA has processed and approved of the response to this
* IKE_AUTH request.
*
* XXX: Danger!
*
* Set the replace timeout but ensure it is larger than the
* retransmit timeout (the default for both is 60-seconds and
* it would appear that libevent can sometimes deliver the
* retransmit before the replay). This way the retransmit
* will timeout and initiate the replace (but if things really
* really screw up the replace will kick in).
*
* XXX: Danger:
*
* In success_v2_state_transition() there's a call to
* clear_retransmits() however, because of the IKE->CHILD
* switch it ends up clearing the CHILD letting the retransmit
* timer expire. Making things worse, the retransmit code
* doesn't know how to properly replace an IKE family -
* flush_incomplete_child() schedules replace events for the
* CHILD states that trigger _after_ the IKE SA has been
* deleted leaving them orphaned.
*/
pexpect(md->svm->timeout_event == EVENT_RETRANSMIT); /* for CST */
delete_event(&ike->sa);
clear_retransmits(&ike->sa);
deltatime_t halfopen = deltatime_max(deltatime_mulu(ike->sa.st_connection->r_timeout, 2),
deltatime(PLUTO_HALFOPEN_SA_LIFE));
event_schedule(EVENT_SA_REPLACE, halfopen, &ike->sa);
change_state(&ike->sa, STATE_PARENT_I2);
/*
* XXX:
*
* Should this code use clone_in_pbs_as_chunk() which uses
* pbs_room() (.roof-.start)? The original code:
*
* clonetochunk(st->st_firstpacket_peer, md->message_pbs.start,
* pbs_offset(&md->message_pbs),
* "saved first received packet");
*
* and clone_out_pbs_as_chunk() both use pbs_offset()
* (.cur-.start).
*
* Suspect it doesn't matter as the code initializing
* .message_pbs forces .roof==.cur - look for the comment
* "trim padding (not actually legit)".
*/
/* record first packet for later checking of signature */
if (md->hdr.isa_xchg != ISAKMP_v2_IKE_INTERMEDIATE){
free_chunk_content(&ike->sa.st_firstpacket_peer);
ike->sa.st_firstpacket_peer = clone_out_pbs_as_chunk(&md->message_pbs,
"saved first received non-intermediate packet");
}
/* beginning of data going out */
/* make sure HDR is at start of a clean buffer */
struct pbs_out reply_stream = open_pbs_out("reply packet",
reply_buffer, sizeof(reply_buffer),
ike->sa.st_logger);
/* HDR out */
pb_stream rbody = open_v2_message(&reply_stream, ike,
NULL /* request */,
ISAKMP_v2_IKE_AUTH);
if (!pbs_ok(&rbody)) {
return STF_INTERNAL_ERROR;
}
/* insert an Encryption payload header (SK) */
v2SK_payload_t sk = open_v2SK_payload(child->sa.st_logger, &rbody, ike);
if (!pbs_ok(&sk.pbs)) {
return STF_INTERNAL_ERROR;
}
/* actual data */
/* decide whether to send CERT payload */
/* it should use parent not child state */
bool send_cert = ikev2_send_cert_decision(&child->sa);
bool ic = pc->initial_contact && (ike->sa.st_ike_pred == SOS_NOBODY);
bool send_idr = ((pc->spd.that.id.kind != ID_NULL && pc->spd.that.id.name.len != 0) ||
pc->spd.that.id.kind == ID_NULL); /* me tarzan, you jane */
if (impair.send_no_idr) {
log_state(RC_LOG, &ike->sa, "IMPAIR: omitting IDr payload");
send_idr = false;
}
dbg("IDr payload will %sbe sent", send_idr ? "" : "NOT ");
/* send out the IDi payload */
{
pb_stream i_id_pbs;
if (!out_struct(&ike->sa.st_v2_id_payload.header,
&ikev2_id_i_desc,
&sk.pbs,
&i_id_pbs) ||
!pbs_out_hunk(ike->sa.st_v2_id_payload.data, &i_id_pbs, "my identity"))
return STF_INTERNAL_ERROR;
close_output_pbs(&i_id_pbs);
}
if (impair.add_unknown_v2_payload_to_sk == ISAKMP_v2_IKE_AUTH) {
if (!emit_v2UNKNOWN("SK request",
impair.add_unknown_v2_payload_to_sk,
&sk.pbs)) {
return STF_INTERNAL_ERROR;
}
}
/* send [CERT,] payload RFC 4306 3.6, 1.2) */
if (send_cert) {
stf_status certstat = ikev2_send_cert(child->sa.st_connection, &sk.pbs);
if (certstat != STF_OK)
return certstat;
/* send CERTREQ */
bool send_certreq = ikev2_send_certreq_INIT_decision(&child->sa, SA_INITIATOR);
if (send_certreq) {
if (DBGP(DBG_BASE)) {
dn_buf buf;
DBG_log("Sending [CERTREQ] of %s",
str_dn(child->sa.st_connection->spd.that.ca, &buf));
}
ikev2_send_certreq(&child->sa, md, &sk.pbs);
}
}
/* you Tarzan, me Jane support */
if (send_idr) {
switch (pc->spd.that.id.kind) {
case ID_DER_ASN1_DN:
case ID_FQDN:
case ID_USER_FQDN:
case ID_KEY_ID:
case ID_NULL:
{
shunk_t id_b;
struct ikev2_id r_id = build_v2_id_payload(&pc->spd.that, &id_b,
"their IDr",
ike->sa.st_logger);
pb_stream r_id_pbs;
if (!out_struct(&r_id, &ikev2_id_r_desc, &sk.pbs,
&r_id_pbs) ||
!pbs_out_hunk(id_b, &r_id_pbs, "their IDr"))
return STF_INTERNAL_ERROR;
close_output_pbs(&r_id_pbs);
break;
}
default:
dbg("Not sending IDr payload for remote ID type %s",
enum_show(&ike_idtype_names, pc->spd.that.id.kind));
break;
}
}
if (ic) {
log_state(RC_LOG, &ike->sa, "sending INITIAL_CONTACT");
if (!emit_v2N(v2N_INITIAL_CONTACT, &sk.pbs))
return STF_INTERNAL_ERROR;
} else {
dbg("not sending INITIAL_CONTACT");
}
/* send out the AUTH payload */
if (!emit_v2_auth(ike, auth_sig, &ike->sa.st_v2_id_payload.mac, &sk.pbs)) {
v2_msgid_switch_responder_from_aborted_child(ike, &child, md, HERE);
return STF_INTERNAL_ERROR;
}
if (need_configuration_payload(pc, ike->sa.hidden_variables.st_nat_traversal)) {
/*
* XXX: should this be passed the CHILD SA's
* .st_connection? Here CHILD and IKE SAs share a
* connection?
*/
if (!emit_v2_child_configuration_payload(ike->sa.st_connection,
child, &sk.pbs)) {
return STF_INTERNAL_ERROR;
}
}
/*
* Switch to first pending child request for this host pair.
* ??? Why so late in this game?
*
* Then emit SA2i, TSi and TSr and NOTIFY payloads related
* to the IPsec SA.
*/
/* so far child's connection is same as parent's */
passert(pc == child->sa.st_connection);
lset_t policy = pc->policy;
/* child connection */
struct connection *cc = first_pending(ike, &policy, &child->sa.st_logger->object_whackfd);
if (cc == NULL) {
cc = pc;
dbg("no pending CHILD SAs found for %s Reauthentication so use the original policy",
cc->name);
} else if (cc != child->sa.st_connection) {
connection_buf cib;
log_state(RC_LOG, &ike->sa,
"switching CHILD #%lu to pending connection "PRI_CONNECTION,
child->sa.st_serialno, pri_connection(cc, &cib));
/* ??? this seems very late to change the connection */
update_state_connection(&child->sa, cc);
}
/* code does not support AH+ESP, which not recommended as per RFC 8247 */
struct ipsec_proto_info *proto_info = ikev2_child_sa_proto_info(child, cc->policy);
proto_info->our_spi = ikev2_child_sa_spi(&cc->spd, cc->policy, child->sa.st_logger);
const chunk_t local_spi = THING_AS_CHUNK(proto_info->our_spi);
/*
* A CHILD_SA established during an AUTH exchange does
* not propose DH - the IKE SA's SKEYSEED is always
* used.
*/
struct ikev2_proposals *child_proposals =
get_v2_ike_auth_child_proposals(cc, "IKE SA initiator emitting ESP/AH proposals",
child->sa.st_logger);
if (!ikev2_emit_sa_proposals(&sk.pbs, child_proposals, &local_spi)) {
return STF_INTERNAL_ERROR;
}
child->sa.st_ts_this = ikev2_end_to_ts(&cc->spd.this, child->sa.st_acquired_sec_label);
child->sa.st_ts_that = ikev2_end_to_ts(&cc->spd.that, child->sa.st_seen_sec_label);
v2_emit_ts_payloads(child, &sk.pbs, cc);
if ((cc->policy & POLICY_TUNNEL) == LEMPTY) {
dbg("Initiator child policy is transport mode, sending v2N_USE_TRANSPORT_MODE");
/* In v2, for parent, protoid must be 0 and SPI must be empty */
if (!emit_v2N(v2N_USE_TRANSPORT_MODE, &sk.pbs)) {
return STF_INTERNAL_ERROR;
}
} else {
dbg("Initiator child policy is tunnel mode, NOT sending v2N_USE_TRANSPORT_MODE");
}
if (!emit_v2N_compression(&child->sa, true, &sk.pbs)) {
return STF_INTERNAL_ERROR;
}
if (cc->send_no_esp_tfc) {
if (!emit_v2N(v2N_ESP_TFC_PADDING_NOT_SUPPORTED, &sk.pbs)) {
return STF_INTERNAL_ERROR;
}
}
if (LIN(POLICY_MOBIKE, cc->policy)) {
child->sa.st_sent_mobike = ike->sa.st_sent_mobike = TRUE;
if (!emit_v2N(v2N_MOBIKE_SUPPORTED, &sk.pbs)) {
return STF_INTERNAL_ERROR;
}
}
/*
* If we and responder are willing to use a PPK, we need to
* generate NO_PPK_AUTH as well as PPK-based AUTH payload
*/
if (ike->sa.st_seen_ppk) {
chunk_t *ppk_id;
get_connection_ppk(ike->sa.st_connection, &ppk_id,
ike->sa.st_logger);
struct ppk_id_payload ppk_id_p = { .type = 0, };
create_ppk_id_payload(ppk_id, &ppk_id_p);
if (DBGP(DBG_BASE)) {
DBG_log("ppk type: %d", (int) ppk_id_p.type);
DBG_dump_hunk("ppk_id from payload:", ppk_id_p.ppk_id);
}
pb_stream ppks;
if (!emit_v2Npl(v2N_PPK_IDENTITY, &sk.pbs, &ppks) ||
!emit_unified_ppk_id(&ppk_id_p, &ppks)) {
return STF_INTERNAL_ERROR;
}
close_output_pbs(&ppks);
if (!LIN(POLICY_PPK_INSIST, cc->policy)) {
if (!ikev2_calc_no_ppk_auth(ike, &ike->sa.st_v2_id_payload.mac_no_ppk_auth,
&ike->sa.st_no_ppk_auth)) {
dbg("ikev2_calc_no_ppk_auth() failed dying");
return STF_FATAL;
}
if (!emit_v2N_hunk(v2N_NO_PPK_AUTH,
ike->sa.st_no_ppk_auth, &sk.pbs)) {
return STF_INTERNAL_ERROR;
}
}
}
/*
* The initiator:
*
* We sent normal IKEv2_AUTH_RSA but if the policy also allows
* AUTH_NULL, we will send a Notify with NULL_AUTH in separate
* chunk. This is only done on the initiator in IKE_AUTH, and
* not repeated in rekeys.
*/
if (v2_auth_by(ike) == AUTHBY_RSASIG && pc->policy & POLICY_AUTH_NULL) {
/* store in null_auth */
chunk_t null_auth = NULL_HUNK;
if (!ikev2_create_psk_auth(AUTHBY_NULL, ike,
&ike->sa.st_v2_id_payload.mac,
&null_auth)) {
log_state(RC_LOG_SERIOUS, &ike->sa,
"Failed to calculate additional NULL_AUTH");
return STF_FATAL;
}
ike->sa.st_intermediate_used = false;
if (!emit_v2N_hunk(v2N_NULL_AUTH, null_auth, &sk.pbs)) {
free_chunk_content(&null_auth);
return STF_INTERNAL_ERROR;
}
free_chunk_content(&null_auth);
}
/* send CP payloads */
if (pc->modecfg_domains != NULL || pc->modecfg_dns != NULL) {
/*
* XXX: should this be passed the CHILD SA's
* .st_connection? Here IKE and CHILD SAs share a
* connection?
*/
if (!emit_v2_child_configuration_payload(ike->sa.st_connection,
child, &sk.pbs)) {
return STF_INTERNAL_ERROR;
}
}
if (!close_v2SK_payload(&sk)) {
return STF_INTERNAL_ERROR;
}
close_output_pbs(&rbody);
close_output_pbs(&reply_stream);
/*
* For AUTH exchange, store the message in the IKE SA. The
* attempt to create the CHILD SA could have failed.
*/
return record_v2SK_message(&reply_stream, &sk,
"sending IKE_AUTH request",
MESSAGE_REQUEST);
}
#ifdef AUTH_HAVE_PAM
static pamauth_callback_t ikev2_pam_continue; /* type assertion */
static void ikev2_pam_continue(struct state *st,
struct msg_digest *md,
const char *name UNUSED,
bool success)
{
dbg("%s() for #%lu %s",
__func__, st->st_serialno, st->st_state->name);
pexpect(v2_msg_role(md) == MESSAGE_REQUEST); /* i.e., MD!=NULL */
pexpect(md->st == NULL || md->st == st);
struct ike_sa *ike = pexpect_ike_sa(st);
pexpect(ike->sa.st_sa_role == SA_RESPONDER);
pexpect(st->st_state->kind == STATE_PARENT_R1);
stf_status stf;
if (success) {
stf = ikev2_parent_inI2outR2_auth_tail(&ike->sa, md, success);
} else {
/*
* XXX: better would be to record the message and
* return STF_ZOMBIFY.
*
* That way compute_v2_state_transition() could send
* the recorded message and then transition the state
* to ZOMBIE (aka *_DEL*). There it can linger while
* dealing with any duplicate IKE_AUTH requests.
*/
record_v2N_response(ike->sa.st_logger, ike, md,
v2N_AUTHENTICATION_FAILED, NULL/*no-data*/,
ENCRYPTED_PAYLOAD);
pstat_sa_failed(&ike->sa, REASON_AUTH_FAILED);
stf = STF_FATAL; /* STF_ZOMBIFY */
}
/* replace (*mdp)->st with st ... */
complete_v2_state_transition(md->st, md, stf);
}
/*
* In the middle of IKEv2 AUTH exchange, the AUTH payload is verified succsfully.
* Now invoke the PAM helper to authorize connection (based on name only, not password)
* When pam helper is done state will be woken up and continue.
*
* This routine "suspends" MD/ST; once PAM finishes it will be
* unsuspended.
*/
static stf_status ikev2_start_pam_authorize(struct state *st)
{
id_buf thatidb;
const char *thatid = str_id(&st->st_connection->spd.that.id, &thatidb);
log_state(RC_LOG, st,
"IKEv2: [XAUTH]PAM method requested to authorize '%s'",
thatid);
auth_fork_pam_process(st,
thatid, "password",
"IKEv2",
ikev2_pam_continue);
return STF_SUSPEND;
}
#endif /* AUTH_HAVE_PAM */
/*
*
***************************************************************
* PARENT_inI2 *****
***************************************************************
* -
*
*
*/
/* STATE_PARENT_R1: I2 --> R2
* <-- HDR, SK {IDi, [CERT,] [CERTREQ,]
* [IDr,] AUTH, SAi2,
* TSi, TSr}
* HDR, SK {IDr, [CERT,] AUTH,
* SAr2, TSi, TSr} -->
*
* [Parent SA established]
*/
static dh_shared_secret_cb ikev2_ike_sa_process_auth_request_no_keymat_continue; /* type assertion */
stf_status ikev2_ike_sa_process_auth_request_no_skeyid(struct ike_sa *ike,
struct child_sa *child,
struct msg_digest *md UNUSED)
{
pexpect(child == NULL);
struct state *st = &ike->sa;
/*
* the initiator sent us an encrypted payload. We need to calculate
* our g^xy, and skeyseed values, and then decrypt the payload.
*/
dbg("ikev2 parent %s(): calculating g^{xy} in order to decrypt I2", __func__);
/* initiate calculation of g^xy */
submit_dh_shared_secret(st, st->st_gi/*responder needs initiator KE*/,
ikev2_ike_sa_process_auth_request_no_keymat_continue,
HERE);
return STF_SUSPEND;
}
static stf_status ikev2_ike_sa_process_auth_request_no_keymat_continue(struct state *st,
struct msg_digest *md)
{
dbg("%s() for #%lu %s: calculating g^{xy}, sending R2",
__func__, st->st_serialno, st->st_state->name);
pexpect(v2_msg_role(md) == MESSAGE_REQUEST); /* i.e., MD!=NULL */
pexpect(md->st == NULL || md->st == st);
struct ike_sa *ike = pexpect_ike_sa(st);
pexpect(ike->sa.st_sa_role == SA_RESPONDER);
pexpect(st->st_state->kind == STATE_PARENT_R1);
/* extract calculated values from r */
if (st->st_dh_shared_secret == NULL) {
/*
* Since dh failed, the channel isn't end-to-end
* encrypted. Send back a clear text notify and then
* abandon the connection.
*/
dbg("aborting IKE SA: DH failed");
send_v2N_response_from_md(md, v2N_INVALID_SYNTAX, NULL);
return STF_FATAL;
}
calc_v2_keymat(st,
NULL/*old_skey_d*/,
NULL/*old_prf*/,
&st->st_ike_spis/*new SPIs*/);
ikev2_process_state_packet(pexpect_ike_sa(st), st, md);
/* above does complete state transition */
return STF_SKIP_COMPLETE_STATE_TRANSITION;
}
static dh_shared_secret_cb ikev2_ike_sa_process_intermediate_request_no_skeyid_continue; /* type assertion */
stf_status ikev2_ike_sa_process_intermediate_request_no_skeyid(struct ike_sa *ike,
struct child_sa *child,
struct msg_digest *md UNUSED)
{
pexpect(child == NULL);
struct state *st = &ike->sa;
/*
* the initiator sent us an encrypted payload. We need to calculate
* our g^xy, and skeyseed values, and then decrypt the payload.
*/
dbg("ikev2 parent %s(): calculating g^{xy} in order to decrypt I2", __func__);
/* initiate calculation of g^xy */
submit_dh_shared_secret(st, st->st_gi/*responder needs initiator KE*/,
ikev2_ike_sa_process_intermediate_request_no_skeyid_continue,
HERE);
return STF_SUSPEND;
}
static stf_status ikev2_ike_sa_process_intermediate_request_no_skeyid_continue(struct state *st,
struct msg_digest *md)
{
dbg("%s() for #%lu %s: calculating g^{xy}, sending R2",
__func__, st->st_serialno, st->st_state->name);
pexpect(v2_msg_role(md) == MESSAGE_REQUEST); /* i.e., MD!=NULL */
pexpect(md->st == NULL || md->st == st);
struct ike_sa *ike = pexpect_ike_sa(st);
pexpect(ike->sa.st_sa_role == SA_RESPONDER);
pexpect(st->st_state->kind == STATE_PARENT_R1);
if (st->st_dh_shared_secret == NULL) {
/*
* Since dh failed, the channel isn't end-to-end
* encrypted. Send back a clear text notify and then
* abandon the connection.
*/
dbg("aborting IKE SA: DH failed");
send_v2N_response_from_md(md, v2N_INVALID_SYNTAX, NULL);
return STF_FATAL;
}
calc_v2_keymat(st,
NULL, NULL, /* no old keymat */
&st->st_ike_spis);
ikev2_process_state_packet(pexpect_ike_sa(st), st, md);
return STF_SKIP_COMPLETE_STATE_TRANSITION;
}
static stf_status ikev2_parent_inI2outR2_continue_tail(struct state *st,
struct msg_digest *md);
stf_status ikev2_ike_sa_process_auth_request(struct ike_sa *ike,
struct child_sa *child,
struct msg_digest *md)
{
if (md->hdr.isa_xchg == ISAKMP_v2_IKE_INTERMEDIATE) {
struct state *st = &ike->sa;
/*
* All systems are go.
*
* Since DH succeeded, a secure (but unauthenticated) SA
* (channel) is available. From this point on, should things
* go south, the state needs to be abandoned (but it shouldn't
* happen).
*/
/*
* Since systems are go, start updating the state, starting
* with SPIr.
*/
rehash_state(&ike->sa, &md->hdr.isa_ike_responder_spi);
/* send Intermediate Exchange response packet */
/* beginning of data going out */
/* make sure HDR is at start of a clean buffer */
struct pbs_out reply_stream = open_pbs_out("reply packet",
reply_buffer, sizeof(reply_buffer),
ike->sa.st_logger);
/* HDR out */
pb_stream rbody = open_v2_message(&reply_stream, ike_sa(st, HERE),
md /* response */,
ISAKMP_v2_IKE_INTERMEDIATE);
if (!pbs_ok(&rbody)) {
return STF_INTERNAL_ERROR;
}
/* insert an Encryption payload header (SK) */
v2SK_payload_t sk = open_v2SK_payload(ike->sa.st_logger, &rbody, ike_sa(st, HERE));
if (!pbs_ok(&sk.pbs)) {
return STF_INTERNAL_ERROR;
}
/* send NOTIFY payload */
if (st->st_seen_intermediate) {
if (!emit_v2N(v2N_INTERMEDIATE_EXCHANGE_SUPPORTED, &sk.pbs))
return STF_INTERNAL_ERROR;
}
if (!close_v2SK_payload(&sk)) {
return STF_INTERNAL_ERROR;
}
close_output_pbs(&rbody);
close_output_pbs(&reply_stream);
stf_status ret = encrypt_v2SK_payload(&sk);
if (ret != STF_OK) {
return ret;
}
record_v2_message(ike, &reply_stream, "reply packet for intermediate exchange",
MESSAGE_RESPONSE);
dbg_v2_msgid(ike, st, "XXX: in %s() hacking around record'n'send bypassing send queue",
__func__);
return STF_OK;
}
/* The connection is "up", start authenticating it */
pexpect(child == NULL);
pexpect(md->st == NULL || md->st == &ike->sa);
/* for testing only */
if (impair.send_no_ikev2_auth) {
log_state(RC_LOG, &ike->sa,
"IMPAIR_SEND_NO_IKEV2_AUTH set - not sending IKE_AUTH packet");
return STF_IGNORE;
}
/*
* This log line establishes that the packet's been decrypted
* and now it is being processed for real.
*
* XXX: move this into ikev2.c?
*/
LLOG_JAMBUF(RC_LOG, ike->sa.st_logger, buf) {
jam(buf, "processing decrypted ");
lswlog_msg_digest(buf, md);
}
stf_status e = ikev2_parent_inI2outR2_continue_tail(&ike->sa, md);
LSWDBGP(DBG_BASE, buf) {
jam(buf, "ikev2_parent_inI2outR2_continue_tail returned ");
jam_v2_stf_status(buf, e);
}
/*
* if failed OE, delete state completely, no create_child_sa
* allowed so childless parent makes no sense. That is also
* the reason why we send v2N_AUTHENTICATION_FAILED, even
* though authenticated succeeded. It shows the remote end
* we have deleted the SA from our end.
*/
if (e >= STF_FAIL &&
(ike->sa.st_connection->policy & POLICY_OPPORTUNISTIC)) {
dbg("deleting opportunistic IKE SA with no Child SA");
pexpect(md->st == &ike->sa);
record_v2N_response(ike->sa.st_logger, ike, md,
v2N_AUTHENTICATION_FAILED, NULL/*no data*/,
ENCRYPTED_PAYLOAD);
return STF_FATAL; /* STF_ZOMBIFY */
}
return e;
}
static stf_status v2_inI2outR2_post_cert_decode(struct state *st,
struct msg_digest *md);
static stf_status ikev2_parent_inI2outR2_continue_tail(struct state *st,
struct msg_digest *md)
{
struct ike_sa *ike = ike_sa(st, HERE);
struct payload_digest *cert_payloads = md->chain[ISAKMP_NEXT_v2CERT];
if (cert_payloads != NULL) {
submit_cert_decode(ike, st, md, cert_payloads,
v2_inI2outR2_post_cert_decode,
"responder decoding certificates");
return STF_SUSPEND;
} else {
dbg("no certs to decode");
ike->sa.st_remote_certs.processed = true;
ike->sa.st_remote_certs.harmless = true;
}
return v2_inI2outR2_post_cert_decode(st, md);
}
static stf_status v2_inI2outR2_post_cert_decode(struct state *st,
struct msg_digest *md)
{
struct ike_sa *ike = ike_sa(st, HERE);
ikev2_log_parentSA(st);
/* going to switch to child st. before that update parent */
if (!LHAS(ike->sa.hidden_variables.st_nat_traversal, NATED_HOST))
update_ike_endpoints(ike, md);
nat_traversal_change_port_lookup(md, st); /* shouldn't this be ike? */
/* this call might update connection in md->st */
if (!ikev2_decode_peer_id(md)) {
event_force(EVENT_SA_EXPIRE, st);
pstat_sa_failed(&ike->sa, REASON_AUTH_FAILED);
release_pending_whacks(st, "Authentication failed");
record_v2N_response(ike->sa.st_logger, ike, md,
v2N_AUTHENTICATION_FAILED, NULL/*no-data*/,
ENCRYPTED_PAYLOAD);
return STF_FATAL;
}
enum ikev2_auth_method atype = md->chain[ISAKMP_NEXT_v2AUTH]->payload.v2auth.isaa_auth_method;
if (IS_LIBUNBOUND && id_ipseckey_allowed(st, atype)) {
stf_status ret = idi_ipseckey_fetch(md);
if (ret != STF_OK) {
log_state(RC_LOG_SERIOUS, st, "DNS: IPSECKEY not found or usable");
return ret;
}
}
return ikev2_parent_inI2outR2_id_tail(md);
}
stf_status ikev2_parent_inI2outR2_id_tail(struct msg_digest *md)
{
struct state *const st = md->st;
struct ike_sa *ike = pexpect_ike_sa(st);
lset_t policy = st->st_connection->policy;
bool found_ppk = FALSE;
chunk_t null_auth = EMPTY_CHUNK;
/*
* The NOTIFY payloads we receive in the IKE_AUTH request are
* either related to the IKE SA, or the Child SA. Here we only
* process the ones related to the IKE SA.
*/
if (md->pbs[PBS_v2N_PPK_IDENTITY] != NULL) {
dbg("received PPK_IDENTITY");
struct ppk_id_payload payl;
if (!extract_v2N_ppk_identity(md->pbs[PBS_v2N_PPK_IDENTITY], &payl, ike)) {
dbg("failed to extract PPK_ID from PPK_IDENTITY payload. Abort!");
return STF_FATAL;
}
const chunk_t *ppk = get_ppk_by_id(&payl.ppk_id);
free_chunk_content(&payl.ppk_id);
if (ppk != NULL) {
found_ppk = TRUE;
}
if (found_ppk && LIN(POLICY_PPK_ALLOW, policy)) {
ppk_recalculate(ppk, st->st_oakley.ta_prf,
&st->st_skey_d_nss,
&st->st_skey_pi_nss,
&st->st_skey_pr_nss,
st->st_logger);
st->st_ppk_used = TRUE;
log_state(RC_LOG, st, "PPK AUTH calculated as responder");
} else {
log_state(RC_LOG, st, "ignored received PPK_IDENTITY - connection does not require PPK or PPKID not found");
}
}
if (md->pbs[PBS_v2N_NO_PPK_AUTH] != NULL) {
pb_stream pbs = *md->pbs[PBS_v2N_NO_PPK_AUTH];
size_t len = pbs_left(&pbs);
dbg("received NO_PPK_AUTH");
if (LIN(POLICY_PPK_INSIST, policy)) {
dbg("Ignored NO_PPK_AUTH data - connection insists on PPK");
} else {
chunk_t no_ppk_auth = alloc_chunk(len, "NO_PPK_AUTH");
diag_t d = pbs_in_raw(&pbs, no_ppk_auth.ptr, len, "NO_PPK_AUTH extract");
if (d != NULL) {
log_diag(RC_LOG_SERIOUS, st->st_logger, &d,
"failed to extract %zd bytes of NO_PPK_AUTH from Notify payload", len);
free_chunk_content(&no_ppk_auth);
return STF_FATAL;
}
free_chunk_content(&st->st_no_ppk_auth); /* in case this was already occupied */
st->st_no_ppk_auth = no_ppk_auth;
}
}
if (md->pbs[PBS_v2N_MOBIKE_SUPPORTED] != NULL) {
dbg("received v2N_MOBIKE_SUPPORTED %s",
st->st_sent_mobike ?
"and sent" : "while it did not sent");
st->st_seen_mobike = true;
}
if (md->pbs[PBS_v2N_NULL_AUTH] != NULL) {
pb_stream pbs = *md->pbs[PBS_v2N_NULL_AUTH];
size_t len = pbs_left(&pbs);
dbg("received v2N_NULL_AUTH");
null_auth = alloc_chunk(len, "NULL_AUTH");
diag_t d = pbs_in_raw(&pbs, null_auth.ptr, len, "NULL_AUTH extract");
if (d != NULL) {
log_diag(RC_LOG_SERIOUS, ike->sa.st_logger, &d,
"failed to extract %zd bytes of NULL_AUTH from Notify payload: ", len);
free_chunk_content(&null_auth);
return STF_FATAL;
}
}
st->st_seen_initialc = md->pbs[PBS_v2N_INITIAL_CONTACT] != NULL;
/*
* If we found proper PPK ID and policy allows PPK, use that.
* Otherwise use NO_PPK_AUTH
*/
if (found_ppk && LIN(POLICY_PPK_ALLOW, policy))
free_chunk_content(&st->st_no_ppk_auth);
if (!found_ppk && LIN(POLICY_PPK_INSIST, policy)) {
log_state(RC_LOG_SERIOUS, &ike->sa, "Requested PPK_ID not found and connection requires a valid PPK");
free_chunk_content(&null_auth);
record_v2N_response(ike->sa.st_logger, ike, md,
v2N_AUTHENTICATION_FAILED, NULL/*no data*/,
ENCRYPTED_PAYLOAD);
return STF_FATAL;
}
/* calculate hash of IDi for AUTH below */
struct crypt_mac idhash_in = v2_id_hash(ike, "IDi verify hash",
"IDi", pbs_in_as_shunk(&md->chain[ISAKMP_NEXT_v2IDi]->pbs),
"skey_pi", st->st_skey_pi_nss);
/* process CERTREQ payload */
if (md->chain[ISAKMP_NEXT_v2CERTREQ] != NULL) {
dbg("received CERTREQ payload; going to decode it");
ikev2_decode_cr(md, ike->sa.st_logger);
}
/* process AUTH payload */
enum keyword_authby that_authby = st->st_connection->spd.that.authby;
passert(that_authby != AUTHBY_NEVER && that_authby != AUTHBY_UNSET);
if (!ike->sa.st_ppk_used && ike->sa.st_no_ppk_auth.ptr != NULL) {
/*
* we didn't recalculate keys with PPK, but we found NO_PPK_AUTH
* (meaning that initiator did use PPK) so we try to verify NO_PPK_AUTH.
*/
dbg("going to try to verify NO_PPK_AUTH.");
/* making a dummy pb_stream so we could pass it to v2_check_auth */
pb_stream pbs_no_ppk_auth;
pb_stream pbs = md->chain[ISAKMP_NEXT_v2AUTH]->pbs;
size_t len = pbs_left(&pbs);
init_pbs(&pbs_no_ppk_auth, ike->sa.st_no_ppk_auth.ptr, len, "pb_stream for verifying NO_PPK_AUTH");
if (!v2_check_auth(md->chain[ISAKMP_NEXT_v2AUTH]->payload.v2auth.isaa_auth_method,
ike, &idhash_in, &pbs_no_ppk_auth,
ike->sa.st_connection->spd.that.authby, "no-PPK-auth")) {
record_v2N_response(ike->sa.st_logger, ike, md,
v2N_AUTHENTICATION_FAILED, NULL/*no data*/,
ENCRYPTED_PAYLOAD);
free_chunk_content(&null_auth); /* ??? necessary? */
pstat_sa_failed(&ike->sa, REASON_AUTH_FAILED);
return STF_FATAL;
}
dbg("NO_PPK_AUTH verified");
} else {
bool policy_null = LIN(POLICY_AUTH_NULL, st->st_connection->policy);
bool policy_rsasig = LIN(POLICY_RSASIG, st->st_connection->policy);
/*
* if received NULL_AUTH in Notify payload and we only allow NULL Authentication,
* proceed with verifying that payload, else verify AUTH normally
*/
if (null_auth.ptr != NULL && policy_null && !policy_rsasig) {
/* making a dummy pb_stream so we could pass it to v2_check_auth */
pb_stream pbs_null_auth;
size_t len = null_auth.len;
dbg("going to try to verify NULL_AUTH from Notify payload");
init_pbs(&pbs_null_auth, null_auth.ptr, len, "pb_stream for verifying NULL_AUTH");
if (!v2_check_auth(IKEv2_AUTH_NULL, ike, &idhash_in,
&pbs_null_auth, AUTHBY_NULL, "NULL_auth from Notify Payload")) {
record_v2N_response(ike->sa.st_logger, ike, md,
v2N_AUTHENTICATION_FAILED, NULL/*no data*/,
ENCRYPTED_PAYLOAD);
free_chunk_content(&null_auth);
pstat_sa_failed(&ike->sa, REASON_AUTH_FAILED);
return STF_FATAL;
}
dbg("NULL_AUTH verified");
} else {
dbg("verifying AUTH payload");
if (!v2_check_auth(md->chain[ISAKMP_NEXT_v2AUTH]->payload.v2auth.isaa_auth_method,
ike, &idhash_in, &md->chain[ISAKMP_NEXT_v2AUTH]->pbs,
st->st_connection->spd.that.authby, "I2 Auth Payload")) {
record_v2N_response(ike->sa.st_logger, ike, md,
v2N_AUTHENTICATION_FAILED, NULL/*no data*/,
ENCRYPTED_PAYLOAD);
free_chunk_content(&null_auth);
pstat_sa_failed(&ike->sa, REASON_AUTH_FAILED);
return STF_FATAL;
}
}
}
/* AUTH succeeded */
free_chunk_content(&null_auth);
#ifdef AUTH_HAVE_PAM
if (st->st_connection->policy & POLICY_IKEV2_PAM_AUTHORIZE)
return ikev2_start_pam_authorize(st);
#endif
return ikev2_parent_inI2outR2_auth_tail(st, md, TRUE);
}
static v2_auth_signature_cb ikev2_parent_inI2outR2_auth_signature_continue; /* type check */
static stf_status ikev2_parent_inI2outR2_auth_tail(struct state *st,
struct msg_digest *md,
bool pam_status)
{
struct connection *const c = st->st_connection;
struct ike_sa *ike = pexpect_ike_sa(st);
if (!pam_status) {
/*
* TBD: send this notification encrypted because the
* AUTH payload succeed
*/
record_v2N_response(ike->sa.st_logger, ike, md,
v2N_AUTHENTICATION_FAILED, NULL/*no data*/,
ENCRYPTED_PAYLOAD);
return STF_FATAL;
}
/*
* Construct the IDr payload and store it in state so that it
* can be emitted later. Then use that to construct the
* "MACedIDFor[R]".
*
* Code assumes that struct ikev2_id's "IDType|RESERVED" is
* laid out the same as the packet.
*/
if (ike->sa.st_peer_wants_null) {
/* make it the Null ID */
ike->sa.st_v2_id_payload.header.isai_type = ID_NULL;
ike->sa.st_v2_id_payload.data = empty_chunk;
} else {
shunk_t data;
ike->sa.st_v2_id_payload.header = build_v2_id_payload(&c->spd.this, &data,
"my IDr",
ike->sa.st_logger);
ike->sa.st_v2_id_payload.data = clone_hunk(data, "my IDr");
}
/* will be signed in auth payload */
ike->sa.st_v2_id_payload.mac = v2_hash_id_payload("IDr", ike, "st_skey_pr_nss",
ike->sa.st_skey_pr_nss);
{
enum keyword_authby authby = v2_auth_by(ike);
enum ikev2_auth_method auth_method = v2_auth_method(ike, authby);
switch (auth_method) {
case IKEv2_AUTH_RSA:
{
const struct hash_desc *hash_algo = &ike_alg_hash_sha1;
struct crypt_mac hash_to_sign =
v2_calculate_sighash(ike, &ike->sa.st_v2_id_payload.mac,
hash_algo, LOCAL_PERSPECTIVE);
ike->sa.st_intermediate_used = false;
if (!submit_v2_auth_signature(ike, &hash_to_sign, hash_algo,
authby, auth_method,
ikev2_parent_inI2outR2_auth_signature_continue)) {
dbg("submit_v2_auth_signature() died, fatal");
record_v2N_response(ike->sa.st_logger, ike, md,
v2N_AUTHENTICATION_FAILED, NULL/*no data*/,
ENCRYPTED_PAYLOAD);
return STF_FATAL;
}
return STF_SUSPEND;
}
case IKEv2_AUTH_DIGSIG:
{
const struct hash_desc *hash_algo = v2_auth_negotiated_signature_hash(ike);
if (hash_algo == NULL) {
record_v2N_response(ike->sa.st_logger, ike, md,
v2N_AUTHENTICATION_FAILED, NULL/*no data*/,
ENCRYPTED_PAYLOAD);
return STF_FATAL;
}
struct crypt_mac hash_to_sign =
v2_calculate_sighash(ike, &ike->sa.st_v2_id_payload.mac,
hash_algo, LOCAL_PERSPECTIVE);
ike->sa.st_intermediate_used = false;
if (!submit_v2_auth_signature(ike, &hash_to_sign, hash_algo,
authby, auth_method,
ikev2_parent_inI2outR2_auth_signature_continue)) {
dbg("submit_v2_auth_signature() died, fatal");
record_v2N_response(ike->sa.st_logger, ike, md,
v2N_AUTHENTICATION_FAILED, NULL/*no data*/,
ENCRYPTED_PAYLOAD);
return STF_FATAL;
}
return STF_SUSPEND;
}
case IKEv2_AUTH_PSK:
case IKEv2_AUTH_NULL:
{
struct hash_signature sig = { .len = 0, };
return ikev2_parent_inI2outR2_auth_signature_continue(ike, md, &sig);
}
default:
log_state(RC_LOG, st,
"authentication method %s not supported",
enum_name(&ikev2_auth_names, auth_method));
return STF_FATAL;
}
}
}
/*
* Deal with either CP or TS.
*
* A CREATE_CHILD_SA can, technically, include a CP (Configuration)
* payload. However no one does it. Allow it here so that the code
* paths are consistent (and it seems that pluto has supported it).
*/
static bool assign_child_responder_client(struct ike_sa *ike,
struct child_sa *child,
struct msg_digest *md)
{
pexpect(md->st == &child->sa);
struct connection *c = child->sa.st_connection;
if (c->pool != NULL && md->chain[ISAKMP_NEXT_v2CP] != NULL) {
struct spd_route *spd = &child->sa.st_connection->spd;
/*
* See ikev2-hostpair-02 where the connection is
* constantly clawed back as the SA keeps trying to
* establish / replace / rekey.
*/
err_t e = lease_that_address(c, md->st);
if (e != NULL) {
log_state(RC_LOG, &child->sa, "ikev2 lease_an_address failure %s", e);
/* XXX: record what? */
record_v2N_response(child->sa.st_logger, ike, md,
v2N_INTERNAL_ADDRESS_FAILURE, NULL/*no data*/,
ENCRYPTED_PAYLOAD);
return false;
}
child->sa.st_ts_this = ikev2_end_to_ts(&spd->this, child->sa.st_acquired_sec_label);
child->sa.st_ts_that = ikev2_end_to_ts(&spd->that, child->sa.st_seen_sec_label);
} else {
if (!v2_process_ts_request(child, md)) {
/* already logged? */
record_v2N_response(child->sa.st_logger, ike, md,
v2N_TS_UNACCEPTABLE, NULL/*no data*/,
ENCRYPTED_PAYLOAD);
return false;
}
}
return true;
}
/*
* The caller could have done the linux_audit_conn() call, except one case
* here deletes the state before returning an STF error
*/
static stf_status ike_auth_child_responder(struct ike_sa *ike,
struct child_sa **child_out,
struct msg_digest *md)
{
pexpect(md->st != NULL);
pexpect(md->st == &ike->sa); /* passed in parent */
struct connection *c = md->st->st_connection;
pexpect(md->hdr.isa_xchg == ISAKMP_v2_IKE_AUTH); /* redundant */
struct child_sa *child = new_v2_child_state(c, ike, IPSEC_SA, SA_RESPONDER,
STATE_V2_IKE_AUTH_CHILD_R0,
null_fd);
binlog_refresh_state(&child->sa);
/*
* XXX: This is to hack around the broken responder code that
* switches from the IKE SA to the CHILD SA before sending the
* reply. Instead, because the CHILD SA can fail, the IKE SA
* should be the one processing the message?
*/
v2_msgid_switch_responder_to_child(ike, child, md, HERE);
if (!assign_child_responder_client(ike, child, md)) {
/* already logged; already recorded */
/*
* XXX: while the CHILD SA failed, the IKE SA should
* continue to exist. This STF_FAIL will blame MD->ST
* aka the IKE SA.
*/
v2_msgid_switch_responder_from_aborted_child(ike, &child, md, HERE);
return STF_FAIL; /* XXX: better? */
}
*child_out = child;
return STF_OK;
}
static stf_status ikev2_parent_inI2outR2_auth_signature_continue(struct ike_sa *ike,
struct msg_digest *md,
const struct hash_signature *auth_sig)
{
struct connection *c = ike->sa.st_connection;
struct state *st = &ike->sa; /* avoid rename for now */
/*
* Now create child state.
* As we will switch to child state, force the parent to the
* new state now.
*
* XXX: Danger! md->svm points to a state transition that
* mashes the IKE SA's initial state in and the CHILD SA's
* final state. Hence, the need to explicitly force the final
* IKE SA state. There should instead be separate state
* transitions for the IKE and CHILD SAs and then have the IKE
* SA invoke the CHILD SA's transition.
*/
pexpect(md->svm->next_state == STATE_V2_ESTABLISHED_CHILD_SA);
ikev2_ike_sa_established(ike, md->svm, STATE_V2_ESTABLISHED_IKE_SA);
if (LHAS(st->hidden_variables.st_nat_traversal, NATED_HOST)) {
/* ensure we run keepalives if needed */
if (c->nat_keepalive) {
/* XXX: just trigger this event? */
nat_traversal_ka_event(ike->sa.st_logger);
}
}
/* send response */
if (LIN(POLICY_MOBIKE, c->policy) && st->st_seen_mobike) {
if (c->spd.that.host_type == KH_ANY) {
/* only allow %any connection to mobike */
st->st_sent_mobike = TRUE;
} else {
log_state(RC_LOG, st, "not responding with v2N_MOBIKE_SUPPORTED, that end is not %%any");
}
}
bool send_redirect = FALSE;
if (st->st_seen_redirect_sup &&
(LIN(POLICY_SEND_REDIRECT_ALWAYS, c->policy) ||
(!LIN(POLICY_SEND_REDIRECT_NEVER, c->policy) &&
require_ddos_cookies()))) {
if (c->redirect_to == NULL) {
log_state(RC_LOG_SERIOUS, st, "redirect-to is not specified, can't redirect requests");
} else {
send_redirect = TRUE;
}
}
/* make sure HDR is at start of a clean buffer */
struct pbs_out reply_stream = open_pbs_out("reply packet",
reply_buffer, sizeof(reply_buffer),
ike->sa.st_logger);
/* HDR out */
pb_stream rbody = open_v2_message(&reply_stream, ike_sa(st, HERE),
md /* response */,
ISAKMP_v2_IKE_AUTH);
/* decide to send CERT payload before we generate IDr */
bool send_cert = ikev2_send_cert_decision(st);
/* insert an Encryption payload header */
v2SK_payload_t sk = open_v2SK_payload(st->st_logger, &rbody, ike);
if (!pbs_ok(&sk.pbs)) {
return STF_INTERNAL_ERROR;
}
if (impair.add_unknown_v2_payload_to_sk == ISAKMP_v2_IKE_AUTH) {
if (!emit_v2UNKNOWN("SK reply",
impair.add_unknown_v2_payload_to_sk,
&sk.pbs)) {
return STF_INTERNAL_ERROR;
}
}
/* send any NOTIFY payloads */
if (st->st_sent_mobike) {
if (!emit_v2N(v2N_MOBIKE_SUPPORTED, &sk.pbs))
return STF_INTERNAL_ERROR;
}
if (st->st_ppk_used) {
if (!emit_v2N(v2N_PPK_IDENTITY, &sk.pbs))
return STF_INTERNAL_ERROR;
}
if (send_redirect) {
if (!emit_redirect_notification(shunk1(c->redirect_to), &sk.pbs))
return STF_INTERNAL_ERROR;
st->st_sent_redirect = TRUE; /* mark that we have sent REDIRECT in IKE_AUTH */
}
if (LIN(POLICY_TUNNEL, c->policy) == LEMPTY && st->st_seen_use_transport) {
if (!emit_v2N(v2N_USE_TRANSPORT_MODE, &sk.pbs))
return STF_INTERNAL_ERROR;
}
if (!emit_v2N_compression(st, st->st_seen_use_ipcomp, &sk.pbs))
return STF_INTERNAL_ERROR;
if (c->send_no_esp_tfc) {
if (!emit_v2N(v2N_ESP_TFC_PADDING_NOT_SUPPORTED, &sk.pbs))
return STF_INTERNAL_ERROR;
}
/* send out the IDr payload */
{
pb_stream r_id_pbs;
if (!out_struct(&ike->sa.st_v2_id_payload.header,
&ikev2_id_r_desc, &sk.pbs, &r_id_pbs) ||
!pbs_out_hunk(ike->sa.st_v2_id_payload.data,
&r_id_pbs, "my identity"))
return STF_INTERNAL_ERROR;
close_output_pbs(&r_id_pbs);
dbg("added IDr payload to packet");
}
/*
* send CERT payload RFC 4306 3.6, 1.2:([CERT,] )
* upon which our received I2 CERTREQ is ignored,
* but ultimately should go into the CERT decision
*/
if (send_cert) {
stf_status certstat = ikev2_send_cert(st->st_connection, &sk.pbs);
if (certstat != STF_OK)
return certstat;
}
/* authentication good, see if there is a child SA being proposed */
unsigned int auth_np;
if (md->chain[ISAKMP_NEXT_v2SA] == NULL ||
md->chain[ISAKMP_NEXT_v2TSi] == NULL ||
md->chain[ISAKMP_NEXT_v2TSr] == NULL) {
/* initiator didn't propose anything. Weird. Try unpending our end. */
/* UNPEND XXX */
if ((c->policy & POLICY_OPPORTUNISTIC) == LEMPTY) {
log_state(RC_LOG, st, "No CHILD SA proposals received.");
} else {
dbg("no CHILD SA proposals received");
}
auth_np = ISAKMP_NEXT_v2NONE;
} else {
dbg("CHILD SA proposals received");
auth_np = (c->pool != NULL && md->chain[ISAKMP_NEXT_v2CP] != NULL) ?
ISAKMP_NEXT_v2CP : ISAKMP_NEXT_v2SA;
}
dbg("going to assemble AUTH payload");
/* now send AUTH payload */
if (!emit_v2_auth(ike, auth_sig, &ike->sa.st_v2_id_payload.mac, &sk.pbs)) {
return STF_INTERNAL_ERROR;
}
ike->sa.st_intermediate_used = false;
if (auth_np == ISAKMP_NEXT_v2SA || auth_np == ISAKMP_NEXT_v2CP) {
/* must have enough to build an CHILD_SA */
struct child_sa *child = NULL;
stf_status ret;
ret = ike_auth_child_responder(ike, &child, md);
if (ret != STF_OK) {
pexpect(child == NULL);
LSWDBGP(DBG_BASE, buf) {
jam(buf, "ike_auth_child_responder() returned ");
jam_v2_stf_status(buf, ret);
}
return ret; /* we should continue building a valid reply packet */
}
pexpect(child != NULL);
ret = ikev2_child_sa_respond(ike, child, md, &sk.pbs,
ISAKMP_v2_IKE_AUTH);
/* note: st: parent; md->st: child */
if (ret != STF_OK) {
LSWDBGP(DBG_BASE, buf) {
jam(buf, "ikev2_child_sa_respond returned ");
jam_v2_stf_status(buf, ret);
}
return ret; /* we should continue building a valid reply packet */
}
}
if (!close_v2SK_payload(&sk)) {
return STF_INTERNAL_ERROR;
}
close_output_pbs(&rbody);
close_output_pbs(&reply_stream);
/*
* For AUTH exchange, store the message in the IKE SA.
* The attempt to create the CHILD SA could have
* failed.
*/
return record_v2SK_message(&reply_stream, &sk,
"replying to IKE_AUTH request",
MESSAGE_RESPONSE);
}
stf_status ikev2_process_child_sa_pl(struct ike_sa *ike, struct child_sa *child,
struct msg_digest *md, bool expect_accepted_proposal)
{
struct connection *c = child->sa.st_connection;
struct payload_digest *const sa_pd = md->chain[ISAKMP_NEXT_v2SA];
enum isakmp_xchg_types isa_xchg = md->hdr.isa_xchg;
struct ipsec_proto_info *proto_info =
ikev2_child_sa_proto_info(child, c->policy);
stf_status ret;
const char *what;
struct ikev2_proposals *child_proposals;
if (isa_xchg == ISAKMP_v2_CREATE_CHILD_SA) {
if (child->sa.st_state->kind == STATE_V2_NEW_CHILD_I1) {
what = "CREATE_CHILD_SA initiator accepting remote ESP/AH proposal";
} else {
what = "CREATE_CHILD_SA responder matching remote ESP/AH proposals";
}
const struct dh_desc *default_dh = (c->policy & POLICY_PFS) != LEMPTY
? ike->sa.st_oakley.ta_dh
: &ike_alg_dh_none;
child_proposals = get_v2_create_child_proposals(c, what, default_dh,
child->sa.st_logger);
} else if (expect_accepted_proposal) {
what = "IKE_AUTH initiator accepting remote ESP/AH proposal";
child_proposals = get_v2_ike_auth_child_proposals(c, what,
child->sa.st_logger);
} else {
what = "IKE_AUTH responder matching remote ESP/AH proposals";
child_proposals = get_v2_ike_auth_child_proposals(c, what,
child->sa.st_logger);
}
ret = ikev2_process_sa_payload(what,
&sa_pd->pbs,
/*expect_ike*/ FALSE,
/*expect_spi*/ TRUE,
expect_accepted_proposal,
LIN(POLICY_OPPORTUNISTIC, c->policy),
&child->sa.st_accepted_esp_or_ah_proposal,
child_proposals, child->sa.st_logger);
if (ret != STF_OK) {
LLOG_JAMBUF(RC_LOG_SERIOUS, child->sa.st_logger, buf) {
jam_string(buf, what);
jam(buf, " failed, responder SA processing returned ");
jam_v2_stf_status(buf, ret);
}
if (child->sa.st_sa_role == SA_RESPONDER) {
pexpect(ret > STF_FAIL);
record_v2N_response(child->sa.st_logger, ike, md,
ret - STF_FAIL, NULL,
ENCRYPTED_PAYLOAD);
return STF_FAIL;
}
/* XXX: return RET? */
return STF_FAIL + v2N_NO_PROPOSAL_CHOSEN;
}
if (DBGP(DBG_BASE)) {
DBG_log_ikev2_proposal(what, child->sa.st_accepted_esp_or_ah_proposal);
}
if (!ikev2_proposal_to_proto_info(child->sa.st_accepted_esp_or_ah_proposal, proto_info,
child->sa.st_logger)) {
log_state(RC_LOG_SERIOUS, &child->sa,
"%s proposed/accepted a proposal we don't actually support!", what);
return STF_FAIL + v2N_NO_PROPOSAL_CHOSEN;
}
/*
* Update/check the PFS.
*
* For the responder, go with what ever was negotiated. For
* the initiator, check what was negotiated against what was
* sent.
*
* Because code expects .st_pfs_group to use NULL, and not
* &ike_alg_dh_none, to indicate no-DH algorithm, the value
* returned by the proposal parser needs to be patched up.
*/
const struct dh_desc *accepted_dh =
proto_info->attrs.transattrs.ta_dh == &ike_alg_dh_none ? NULL
: proto_info->attrs.transattrs.ta_dh;
switch (child->sa.st_sa_role) {
case SA_INITIATOR:
pexpect(expect_accepted_proposal);
if (accepted_dh != NULL && accepted_dh != child->sa.st_pfs_group) {
log_state(RC_LOG_SERIOUS, &child->sa,
"expecting %s but remote's accepted proposal includes %s",
child->sa.st_pfs_group == NULL ? "no DH" : child->sa.st_pfs_group->common.fqn,
accepted_dh->common.fqn);
return STF_FAIL + v2N_NO_PROPOSAL_CHOSEN;
}
child->sa.st_pfs_group = accepted_dh;
break;
case SA_RESPONDER:
pexpect(!expect_accepted_proposal);
pexpect(child->sa.st_sa_role == SA_RESPONDER);
pexpect(child->sa.st_pfs_group == NULL);
child->sa.st_pfs_group = accepted_dh;
break;
default:
bad_case(child->sa.st_sa_role);
}
/*
* Update the state's st_oakley parameters from the proposal,
* but retain the previous PRF. A CHILD_SA always uses the
* PRF negotiated when creating initial IKE SA.
*
* XXX: The mystery is, why is .st_oakley even being updated?
* Perhaps it is to prop up code getting the CHILD_SA's PRF
* from the child when that code should use the CHILD_SA's IKE
* SA; or perhaps it is getting things ready for an IKE SA
* re-key?
*/
if (isa_xchg == ISAKMP_v2_CREATE_CHILD_SA && child->sa.st_pfs_group != NULL) {
dbg("updating #%lu's .st_oakley with preserved PRF, but why update?",
child->sa.st_serialno);
struct trans_attrs accepted_oakley = proto_info->attrs.transattrs;
pexpect(accepted_oakley.ta_prf == NULL);
accepted_oakley.ta_prf = child->sa.st_oakley.ta_prf;
child->sa.st_oakley = accepted_oakley;
}
return STF_OK;
}
static stf_status ikev2_process_cp_respnse(struct msg_digest *md)
{
struct state *st = md->st;
struct connection *c = st->st_connection;
/* IP parameters on rekey MUST be identical, so CP payloads not needed */
if (st->st_state->kind == STATE_V2_REKEY_CHILD_I1 ||
st->st_state->kind == STATE_V2_NEW_CHILD_I1)
return STF_OK; /* CP is not required in an IPsec REKEY exchange */
if (need_configuration_payload(c, st->hidden_variables.st_nat_traversal)) {
if (md->chain[ISAKMP_NEXT_v2CP] == NULL) {
/* not really anything to here... but it would be worth unpending again */
log_state(RC_LOG_SERIOUS, st, "missing v2CP reply, not attempting to setup child SA");
/*
* ??? this isn't really a failure, is it?
* If none of those payloads appeared, isn't this is a
* legitimate negotiation of a parent?
*/
return STF_FAIL + v2N_NO_PROPOSAL_CHOSEN;
}
if (!ikev2_parse_cp_r_body(md->chain[ISAKMP_NEXT_v2CP], st))
{
return STF_FAIL + v2N_NO_PROPOSAL_CHOSEN;
}
}
return STF_OK;
}
static void ikev2_rekey_expire_pred(const struct state *st, so_serial_t pred)
{
struct state *rst = state_with_serialno(pred);
deltatime_t lifetime = deltatime(0); /* .lt. EXPIRE_OLD_SA_DELAY */
if (rst != NULL && IS_V2_ESTABLISHED(rst->st_state)) {
/* on initiator, delete st_ipsec_pred. The responder should not */
monotime_t now = mononow();
const struct pluto_event *ev = rst->st_event;
if (ev != NULL)
lifetime = monotimediff(ev->ev_time, now);
}
deltatime_buf lb;
log_state(RC_LOG, st, "rekeyed #%lu %s %s remaining life %ss", pred,
st->st_state->name,
rst == NULL ? "and the state is gone" : "and expire it",
str_deltatime(lifetime, &lb));
if (deltatime_cmp(lifetime, >, EXPIRE_OLD_SA_DELAY)) {
delete_event(rst);
event_schedule(EVENT_SA_EXPIRE, EXPIRE_OLD_SA_DELAY, rst);
}
/* else it should be on its way to expire no need to kick dead state */
}
static stf_status ikev2_process_ts_and_rest(struct msg_digest *md)
{
struct child_sa *child = pexpect_child_sa(md->st);
struct state *st = &child->sa;
struct connection *c = st->st_connection;
struct ike_sa *ike = ike_sa(&child->sa, HERE);
RETURN_STF_FAILURE_STATUS(ikev2_process_cp_respnse(md));
if (!v2_process_ts_response(child, md)) {
/*
* XXX: will this will cause the state machine to
* overwrite the AUTH part of the message - which is
* wrong. XXX: does this delete the child state?
*/
return STF_FAIL + v2N_TS_UNACCEPTABLE;
}
/* examine and accept SA ESP/AH proposals */
if (md->hdr.isa_xchg != ISAKMP_v2_CREATE_CHILD_SA)
RETURN_STF_FAILURE_STATUS(ikev2_process_child_sa_pl(ike, child, md, TRUE));
/*
* examine notification payloads for Child SA errors
* (presumably any error reaching this point is for the
* child?).
*
* https://tools.ietf.org/html/rfc7296#section-3.10.1
*
* Types in the range 0 - 16383 are intended for reporting
* errors. An implementation receiving a Notify payload
* with one of these types that it does not recognize in a
* response MUST assume that the corresponding request has
* failed entirely. Unrecognized error types in a request
* and status types in a request or response MUST be
* ignored, and they should be logged.
*/
if (md->v2N_error != v2N_NOTHING_WRONG) {
struct esb_buf esb;
log_state(RC_LOG_SERIOUS, &child->sa, "received ERROR NOTIFY (%d): %s ",
md->v2N_error,
enum_showb(&ikev2_notify_names, md->v2N_error, &esb));
return STF_FATAL;
}
/* check for Child SA related NOTIFY payloads */
if (md->pbs[PBS_v2N_USE_TRANSPORT_MODE] != NULL) {
if (c->policy & POLICY_TUNNEL) {
/* This means we did not send v2N_USE_TRANSPORT, however responder is sending it in now, seems incorrect */
dbg("Initiator policy is tunnel, responder sends v2N_USE_TRANSPORT_MODE notification in inR2, ignoring it");
} else {
dbg("Initiator policy is transport, responder sends v2N_USE_TRANSPORT_MODE, setting CHILD SA to transport mode");
if (st->st_esp.present) {
st->st_esp.attrs.mode = ENCAPSULATION_MODE_TRANSPORT;
}
if (st->st_ah.present) {
st->st_ah.attrs.mode = ENCAPSULATION_MODE_TRANSPORT;
}
}
}
st->st_seen_no_tfc = md->pbs[PBS_v2N_ESP_TFC_PADDING_NOT_SUPPORTED] != NULL;
if (md->pbs[PBS_v2N_IPCOMP_SUPPORTED] != NULL) {
pb_stream pbs = *md->pbs[PBS_v2N_IPCOMP_SUPPORTED];
size_t len = pbs_left(&pbs);
struct ikev2_notify_ipcomp_data n_ipcomp;
dbg("received v2N_IPCOMP_SUPPORTED of length %zd", len);
if ((c->policy & POLICY_COMPRESS) == LEMPTY) {
log_state(RC_LOG_SERIOUS, st,
"Unexpected IPCOMP request as our connection policy did not indicate support for it");
return STF_FAIL + v2N_NO_PROPOSAL_CHOSEN;
}
diag_t d = pbs_in_struct(&pbs, &ikev2notify_ipcomp_data_desc,
&n_ipcomp, sizeof(n_ipcomp), NULL);
if (d != NULL) {
log_diag(RC_LOG, st->st_logger, &d, "%s", "");
return STF_FATAL;
}
if (n_ipcomp.ikev2_notify_ipcomp_trans != IPCOMP_DEFLATE) {
log_state(RC_LOG_SERIOUS, st,
"Unsupported IPCOMP compression method %d",
n_ipcomp.ikev2_notify_ipcomp_trans); /* enum_name this later */
return STF_FATAL;
}
if (n_ipcomp.ikev2_cpi < IPCOMP_FIRST_NEGOTIATED) {
log_state(RC_LOG_SERIOUS, st,
"Illegal IPCOMP CPI %d", n_ipcomp.ikev2_cpi);
return STF_FATAL;
}
dbg("Received compression CPI=%d", n_ipcomp.ikev2_cpi);
//st->st_ipcomp.attrs.spi = uniquify_peer_cpi((ipsec_spi_t)htonl(n_ipcomp.ikev2_cpi), st, 0);
st->st_ipcomp.attrs.spi = htonl((ipsec_spi_t)n_ipcomp.ikev2_cpi);
st->st_ipcomp.attrs.transattrs.ta_comp = n_ipcomp.ikev2_notify_ipcomp_trans;
st->st_ipcomp.attrs.mode = ENCAPSULATION_MODE_TUNNEL; /* always? */
st->st_ipcomp.present = TRUE;
st->st_seen_use_ipcomp = TRUE;
}
ikev2_derive_child_keys(child);
#ifdef USE_XFRM_INTERFACE
/* before calling do_command() */
if (st->st_state->kind != STATE_V2_REKEY_CHILD_I1)
if (c->xfrmi != NULL &&
c->xfrmi->if_id != 0)
if (add_xfrmi(c, child->sa.st_logger))
return STF_FATAL;
#endif
/* now install child SAs */
if (!install_ipsec_sa(st, TRUE))
return STF_FATAL; /* does this affect/kill the IKE SA ? */
set_newest_ipsec_sa("inR2", st);
if (st->st_state->kind == STATE_V2_REKEY_CHILD_I1)
ikev2_rekey_expire_pred(st, st->st_ipsec_pred);
return STF_OK;
}
/*
s
***************************************************************
* PARENT_inR2 (I3 state) *****
***************************************************************
* - there are no cryptographic continuations, but be certain
* that there will have to be DNS continuations, but they
* just aren't implemented yet.
*
*/
/* STATE_PARENT_I2: R2 --> I3
* <-- HDR, SK {IDr, [CERT,] AUTH,
* SAr2, TSi, TSr}
* [Parent SA established]
*
* For error handling in this function, please read:
* https://tools.ietf.org/html/rfc7296#section-2.21.2
*/
static stf_status v2_inR2_post_cert_decode(struct state *st, struct msg_digest *md);
stf_status ikev2_parent_inR2(struct ike_sa *ike, struct child_sa *child, struct msg_digest *md)
{
pexpect(child != NULL);
struct state *st = &child->sa;
struct state *pst = &ike->sa;
if (md->pbs[PBS_v2N_MOBIKE_SUPPORTED] != NULL) {
dbg("received v2N_MOBIKE_SUPPORTED %s",
pst->st_sent_mobike ? "and sent" : "while it did not sent");
st->st_seen_mobike = pst->st_seen_mobike = true;
}
if (md->pbs[PBS_v2N_REDIRECT] != NULL) {
dbg("received v2N_REDIRECT in IKE_AUTH reply");
if (!LIN(POLICY_ACCEPT_REDIRECT_YES, st->st_connection->policy)) {
dbg("ignoring v2N_REDIRECT, we don't accept being redirected");
} else {
ip_address redirect_ip;
err_t err = parse_redirect_payload(md->pbs[PBS_v2N_REDIRECT],
st->st_connection->accept_redirect_to,
NULL,
&redirect_ip,
ike->sa.st_logger);
if (err != NULL) {
dbg("warning: parsing of v2N_REDIRECT payload failed: %s", err);
} else {
/* initiate later, because we need to wait for AUTH success */
st->st_connection->temp_vars.redirect_ip = redirect_ip;
}
}
}
st->st_seen_no_tfc = md->pbs[PBS_v2N_ESP_TFC_PADDING_NOT_SUPPORTED] != NULL; /* Technically, this should be only on the child state */
/*
* On the initiator, we can STF_FATAL on IKE SA errors, because no
* packet needs to be sent anymore. And we cannot recover. Unlike
* IKEv1, we cannot send an updated IKE_AUTH request that would use
* different credentials.
*
* On responder (code elsewhere), we have to STF_FAIL to get out
* the response packet (we need a zombie state for these)
*
* Note: once AUTH succeeds, we can still return STF_FAIL's because
* those apply to the Child SA and should not tear down the IKE SA.
*/
struct payload_digest *cert_payloads = md->chain[ISAKMP_NEXT_v2CERT];
if (cert_payloads != NULL) {
submit_cert_decode(ike, st, md, cert_payloads,
v2_inR2_post_cert_decode,
"initiator decoding certificates");
return STF_SUSPEND;
} else {
dbg("no certs to decode");
ike->sa.st_remote_certs.processed = true;
ike->sa.st_remote_certs.harmless = true;
return v2_inR2_post_cert_decode(st, md);
}
}
static stf_status v2_inR2_post_cert_decode(struct state *st, struct msg_digest *md)
{
passert(md != NULL);
struct ike_sa *ike = ike_sa(st, HERE);
struct state *pst = &ike->sa;
if (!ikev2_decode_peer_id(md)) {
event_force(EVENT_SA_EXPIRE, st);
pstat_sa_failed(&ike->sa, REASON_AUTH_FAILED);
release_pending_whacks(st, "Authentication failed");
return STF_FATAL;
}
struct connection *c = st->st_connection;
enum keyword_authby that_authby = c->spd.that.authby;
passert(that_authby != AUTHBY_NEVER && that_authby != AUTHBY_UNSET);
if (md->pbs[PBS_v2N_PPK_IDENTITY] != NULL) {
if (!LIN(POLICY_PPK_ALLOW, c->policy)) {
log_state(RC_LOG_SERIOUS, st,
"Received PPK_IDENTITY but connection does not allow PPK");
return STF_FATAL;
}
} else {
if (LIN(POLICY_PPK_INSIST, c->policy)) {
log_state(RC_LOG_SERIOUS, st,
"failed to receive PPK confirmation and connection has ppk=insist");
dbg("should be initiating a notify that kills the state");
pstat_sa_failed(&ike->sa, REASON_AUTH_FAILED);
return STF_FATAL;
}
}
/*
* If we sent USE_PPK and we did not receive a PPK_IDENTITY,
* it means the responder failed to find our PPK ID, but allowed
* the connection to continue without PPK by using our NO_PPK_AUTH
* payload. We should revert our key material to NO_PPK versions.
*/
if (ike->sa.st_seen_ppk &&
md->pbs[PBS_v2N_PPK_IDENTITY] == NULL &&
LIN(POLICY_PPK_ALLOW, c->policy)) {
/* discard the PPK based calculations */
log_state(RC_LOG, st, "Peer wants to continue without PPK - switching to NO_PPK");
release_symkey(__func__, "st_skey_d_nss", &pst->st_skey_d_nss);
pst->st_skey_d_nss = reference_symkey(__func__, "used sk_d from no ppk", pst->st_sk_d_no_ppk);
release_symkey(__func__, "st_skey_pi_nss", &pst->st_skey_pi_nss);
pst->st_skey_pi_nss = reference_symkey(__func__, "used sk_pi from no ppk", pst->st_sk_pi_no_ppk);
release_symkey(__func__, "st_skey_pr_nss", &pst->st_skey_pr_nss);
pst->st_skey_pr_nss = reference_symkey(__func__, "used sk_pr from no ppk", pst->st_sk_pr_no_ppk);
if (pst != st) {
release_symkey(__func__, "st_skey_d_nss", &st->st_skey_d_nss);
st->st_skey_d_nss = reference_symkey(__func__, "used sk_d from no ppk", st->st_sk_d_no_ppk);
release_symkey(__func__, "st_skey_pi_nss", &st->st_skey_pi_nss);
st->st_skey_pi_nss = reference_symkey(__func__, "used sk_pi from no ppk", st->st_sk_pi_no_ppk);
release_symkey(__func__, "st_skey_pr_nss", &st->st_skey_pr_nss);
st->st_skey_pr_nss = reference_symkey(__func__, "used sk_pr from no ppk", st->st_sk_pr_no_ppk);
}
}
struct crypt_mac idhash_in = v2_id_hash(ike, "idhash auth R2",
"IDr", pbs_in_as_shunk(&md->chain[ISAKMP_NEXT_v2IDr]->pbs),
"skey_pr", pst->st_skey_pr_nss);
/* process AUTH payload */
dbg("verifying AUTH payload");
if (!v2_check_auth(md->chain[ISAKMP_NEXT_v2AUTH]->payload.v2auth.isaa_auth_method,
ike, &idhash_in, &md->chain[ISAKMP_NEXT_v2AUTH]->pbs,
that_authby, "R2 Auth Payload"))
{
/*
* We cannot send a response as we are processing IKE_AUTH reply
* the RFC states we should pretend IKE_AUTH was okay, and then
* send an INFORMATIONAL DELETE IKE SA but we have not implemented
* that yet.
*/
return STF_FATAL;
}
st->st_ikev2_anon = pst->st_ikev2_anon; /* was set after duplicate_state() */
/* AUTH succeeded */
/*
* update the parent state to make sure that it knows we have
* authenticated properly.
*
* XXX: Danger! md->svm points to a state transition that
* mashes the IKE SA's initial state in and the CHILD SA's
* final state. Hence, the need to explicitly force the final
* IKE SA state. There should instead be separate state
* transitions for the IKE and CHILD SAs and then have the IKE
* SA invoke the CHILD SA's transition.
*/
pexpect(md->svm->next_state == STATE_V2_ESTABLISHED_CHILD_SA);
ikev2_ike_sa_established(pexpect_ike_sa(pst), md->svm, STATE_V2_ESTABLISHED_IKE_SA);
if (LHAS(st->hidden_variables.st_nat_traversal, NATED_HOST)) {
/* ensure we run keepalives if needed */
if (c->nat_keepalive) {
/* XXX: just trigger this event */
nat_traversal_ka_event(ike->sa.st_logger);
}
}
/* AUTH is ok, we can trust the notify payloads */
if (md->pbs[PBS_v2N_USE_TRANSPORT_MODE] != NULL) { /* FIXME: use new RFC logic turning this into a request, not requirement */
if (LIN(POLICY_TUNNEL, st->st_connection->policy)) {
log_state(RC_LOG_SERIOUS, st, "local policy requires Tunnel Mode but peer requires required Transport Mode");
return STF_V2_DELETE_EXCHANGE_INITIATOR_IKE_SA; /* should just delete child */
}
} else {
if (!LIN(POLICY_TUNNEL, st->st_connection->policy)) {
log_state(RC_LOG_SERIOUS, st, "local policy requires Transport Mode but peer requires required Tunnel Mode");
return STF_V2_DELETE_EXCHANGE_INITIATOR_IKE_SA; /* should just delete child */
}
}
if (md->pbs[PBS_v2N_REDIRECT] != NULL) {
st->st_redirected_in_auth = true;
event_force(EVENT_v2_REDIRECT, st);
return STF_SUSPEND;
}
/* See if there is a child SA available */
if (md->chain[ISAKMP_NEXT_v2SA] == NULL ||
md->chain[ISAKMP_NEXT_v2TSi] == NULL ||
md->chain[ISAKMP_NEXT_v2TSr] == NULL) {
/* not really anything to here... but it would be worth unpending again */
log_state(RC_LOG_SERIOUS, st,
"missing v2SA, v2TSi or v2TSr: not attempting to setup child SA");
/*
* ??? this isn't really a failure, is it?
* If none of those payloads appeared, isn't this is a
* legitimate negotiation of a parent?
* Paul: this notify is never sent because w
*/
return STF_FAIL + v2N_NO_PROPOSAL_CHOSEN;
}
return ikev2_process_ts_and_rest(md);
}
static bool ikev2_rekey_child_req(struct child_sa *child,
enum ikev2_sec_proto_id *rekey_protoid,
ipsec_spi_t *rekey_spi)
{
if (!pexpect(child->sa.st_establishing_sa == IPSEC_SA) ||
!pexpect(child->sa.st_ipsec_pred != SOS_NOBODY) ||
!pexpect(child->sa.st_state->kind == STATE_V2_REKEY_CHILD_I0)) {
return false;
}
struct state *rst = state_with_serialno(child->sa.st_ipsec_pred);
if (rst == NULL) {
/*
* XXX: For instance:
*
* - the old child initiated this replacement
*
* - this child wondered off to perform DH
*
* - the old child expires itself (or it gets sent a
* delete)
*
* - this child finds it has no older sibling
*
* The older child should have discarded this state.
*/
log_state(LOG_STREAM/*not-whack*/, &child->sa,
"CHILD SA to rekey #%lu vanished abort this exchange",
child->sa.st_ipsec_pred);
return false;
}
/*
* 1.3.3. Rekeying Child SAs with the CREATE_CHILD_SA
* Exchange: The SA being rekeyed is identified by the SPI
* field in the Notify payload; this is the SPI the exchange
* initiator would expect in inbound ESP or AH packets.
*/
if (rst->st_esp.present) {
*rekey_spi = rst->st_esp.our_spi;
*rekey_protoid = PROTO_IPSEC_ESP;
} else if (rst->st_ah.present) {
*rekey_spi = rst->st_ah.our_spi;
*rekey_protoid = PROTO_IPSEC_AH;
} else {
pexpect_fail(child->sa.st_logger, HERE,
"CHILD SA to rekey #%lu is not ESP/AH",
child->sa.st_ipsec_pred);
return false;
}
child->sa.st_ts_this = rst->st_ts_this;
child->sa.st_ts_that = rst->st_ts_that;
connection_buf cib;
dbg("#%lu initiate rekey request for "PRI_CONNECTION" #%lu SPI 0x%x TSi TSr",
child->sa.st_serialno,
pri_connection(rst->st_connection, &cib),
rst->st_serialno, ntohl(*rekey_spi));
ikev2_print_ts(&child->sa.st_ts_this);
ikev2_print_ts(&child->sa.st_ts_that);
return true;
}
static bool ikev2_rekey_child_resp(struct ike_sa *ike, struct child_sa *child,
struct msg_digest *md)
{
struct payload_digest *rekey_sa_payload = NULL;
for (struct payload_digest *ntfy = md->chain[ISAKMP_NEXT_v2N]; ntfy != NULL; ntfy = ntfy->next) {
switch (ntfy->payload.v2n.isan_type) {
case v2N_REKEY_SA:
if (rekey_sa_payload != NULL) {
/* will tolerate multiple */
log_state(RC_LOG_SERIOUS, &child->sa,
"ignoring duplicate v2N_REKEY_SA in exchange");
break;
}
dbg("received v2N_REKEY_SA");
rekey_sa_payload = ntfy;
break;
default:
/*
* there is another pass of notify payloads
* after this that will handle all other but
* REKEY
*/
break;
}
}
if (rekey_sa_payload == NULL) {
pexpect_fail(child->sa.st_logger, HERE,
"rekey child can't find its rekey_sa payload");
return STF_INTERNAL_ERROR;
}
struct ikev2_notify *rekey_notify = &rekey_sa_payload->payload.v2n;
/*
* find old state to rekey
*/
dbg("CREATE_CHILD_SA IPsec SA rekey Protocol %s",
enum_show(&ikev2_notify_protocol_id_names, rekey_notify->isan_protoid));
if (rekey_notify->isan_spisize != sizeof(ipsec_spi_t)) {
log_state(RC_LOG, &child->sa,
"CREATE_CHILD_SA IPsec SA rekey invalid spi size %u",
rekey_notify->isan_spisize);
record_v2N_response(child->sa.st_logger, ike, md, v2N_INVALID_SYNTAX,
NULL/*empty data*/, ENCRYPTED_PAYLOAD);
return false;
}
ipsec_spi_t spi = 0;
diag_t d = pbs_in_raw(&rekey_sa_payload->pbs, &spi, sizeof(spi), "SPI");
if (d != NULL) {
log_diag(RC_LOG, child->sa.st_logger, &d, "%s", "");
record_v2N_response(child->sa.st_logger, ike, md, v2N_INVALID_SYNTAX,
NULL/*empty data*/, ENCRYPTED_PAYLOAD);
return false; /* cannot happen; XXX: why? */
}
if (spi == 0) {
log_state(RC_LOG, &child->sa,
"CREATE_CHILD_SA IPsec SA rekey contains zero SPI");
record_v2N_response(child->sa.st_logger, ike, md, v2N_INVALID_SYNTAX,
NULL/*empty data*/, ENCRYPTED_PAYLOAD);
return false;
}
if (rekey_notify->isan_protoid != PROTO_IPSEC_ESP &&
rekey_notify->isan_protoid != PROTO_IPSEC_AH) {
log_state(RC_LOG, &child->sa,
"CREATE_CHILD_SA IPsec SA rekey invalid Protocol ID %s",
enum_show(&ikev2_notify_protocol_id_names, rekey_notify->isan_protoid));
record_v2N_spi_response(child->sa.st_logger, ike, md,
rekey_notify->isan_protoid, &spi,
v2N_CHILD_SA_NOT_FOUND,
NULL/*empty data*/, ENCRYPTED_PAYLOAD);
return false;
}
dbg("CREATE_CHILD_S to rekey IPsec SA(0x%08" PRIx32 ") Protocol %s",
ntohl((uint32_t) spi),
enum_show(&ikev2_notify_protocol_id_names, rekey_notify->isan_protoid));
/*
* From 1.3.3. Rekeying Child SAs with the CREATE_CHILD_SA
* Exchange: The SA being rekeyed is identified by the SPI
* field in the [REKEY_SA] Notify payload; this is the SPI the
* exchange initiator would expect in inbound ESP or AH
* packets.
*
* From our POV, that's the outbound SPI.
*/
struct child_sa *replaced_child = find_v2_child_sa_by_outbound_spi(ike, rekey_notify->isan_protoid, spi);
if (replaced_child == NULL) {
log_state(RC_LOG, &child->sa,
"CREATE_CHILD_SA no such IPsec SA to rekey SA(0x%08" PRIx32 ") Protocol %s",
ntohl((uint32_t) spi),
enum_show(&ikev2_notify_protocol_id_names, rekey_notify->isan_protoid));
record_v2N_spi_response(child->sa.st_logger, ike, md,
rekey_notify->isan_protoid, &spi,
v2N_CHILD_SA_NOT_FOUND,
NULL/*empty data*/, ENCRYPTED_PAYLOAD);
return false;
}
child->sa.st_ipsec_pred = replaced_child->sa.st_serialno;
connection_buf cb;
dbg("#%lu rekey request for "PRI_CONNECTION" #%lu TSi TSr",
child->sa.st_serialno,
pri_connection(replaced_child->sa.st_connection, &cb),
replaced_child->sa.st_serialno);
ikev2_print_ts(&replaced_child->sa.st_ts_this);
ikev2_print_ts(&replaced_child->sa.st_ts_that);
update_state_connection(&child->sa, replaced_child->sa.st_connection);
return true;
}
static bool ikev2_rekey_child_copy_ts(struct child_sa *child)
{
passert(child->sa.st_ipsec_pred != SOS_NOBODY);
/* old child state being rekeyed */
struct child_sa *rchild = child_sa_by_serialno(child->sa.st_ipsec_pred);
if (!pexpect(rchild != NULL)) {
/*
* Something screwed up - can't even start to rekey a
* CHILD SA when there's no predicessor.
*/
return false;
}
/*
* RFC 7296 #2.9.2 the exact or the superset.
* exact is a should. Here libreswan only allow the exact.
* Inherit the TSi TSr from old state, IPsec SA.
*/
connection_buf cib;
dbg("#%lu inherit spd, TSi TSr, from "PRI_CONNECTION" #%lu",
child->sa.st_serialno,
pri_connection(rchild->sa.st_connection, &cib),
rchild->sa.st_serialno);
struct spd_route *spd = &rchild->sa.st_connection->spd;
child->sa.st_ts_this = ikev2_end_to_ts(&spd->this, child->sa.st_acquired_sec_label);
child->sa.st_ts_that = ikev2_end_to_ts(&spd->that, child->sa.st_seen_sec_label);
ikev2_print_ts(&child->sa.st_ts_this);
ikev2_print_ts(&child->sa.st_ts_that);
return true;
}
/* once done use the same function in ikev2_parent_inR1outI2_tail too */
static stf_status ikev2_child_add_ipsec_payloads(struct child_sa *child,
pb_stream *outpbs)
{
if (!pexpect(child->sa.st_establishing_sa == IPSEC_SA)) {
return STF_INTERNAL_ERROR;
}
struct connection *cc = child->sa.st_connection;
bool send_use_transport = (cc->policy & POLICY_TUNNEL) == LEMPTY;
/* ??? this code won't support AH + ESP */
struct ipsec_proto_info *proto_info
= ikev2_child_sa_proto_info(child, cc->policy);
proto_info->our_spi = ikev2_child_sa_spi(&cc->spd, cc->policy, child->sa.st_logger);
chunk_t local_spi = THING_AS_CHUNK(proto_info->our_spi);
/*
* HACK: Use the CREATE_CHILD_SA proposal suite hopefully
* generated during the CHILD SA's initiation.
*
* XXX: this code should be either using get_v2...() (hard to
* figure out what DEFAULT_DH is) or saving the proposal in
* the state.
*/
passert(cc->v2_create_child_proposals != NULL);
if (!ikev2_emit_sa_proposals(outpbs, cc->v2_create_child_proposals, &local_spi))
return STF_INTERNAL_ERROR;
/*
* If rekeying, get the old SPI and protocol.
*/
ipsec_spi_t rekey_spi = 0;
enum ikev2_sec_proto_id rekey_protoid = PROTO_v2_RESERVED;
if (child->sa.st_ipsec_pred != SOS_NOBODY) {
if (!ikev2_rekey_child_req(child, &rekey_protoid, &rekey_spi)) {
/*
* XXX: For instance:
*
* - the old child initiated this replacement
*
* - this child wondered off to perform DH
*
* - the old child expires itself (or it gets
* sent a delete)
*
* - this child finds it has no older sibling
*
* The older child should have discarded this
* state.
*/
return STF_INTERNAL_ERROR;
}
}
struct ikev2_generic in = {
.isag_critical = build_ikev2_critical(false, child->sa.st_logger),
};
pb_stream pb_nr;
if (!out_struct(&in, &ikev2_nonce_desc, outpbs, &pb_nr) ||
!pbs_out_hunk(child->sa.st_ni, &pb_nr, "IKEv2 nonce"))
return STF_INTERNAL_ERROR;
close_output_pbs(&pb_nr);
if (child->sa.st_pfs_group != NULL) {
if (!emit_v2KE(&child->sa.st_gi, child->sa.st_pfs_group, outpbs)) {
return STF_INTERNAL_ERROR;
}
}
if (rekey_spi != 0) {
if (!emit_v2Nsa_pl(v2N_REKEY_SA,
rekey_protoid, &rekey_spi,
outpbs, NULL))
return STF_INTERNAL_ERROR;
}
if (rekey_spi == 0) {
/* not rekey */
child->sa.st_ts_this = ikev2_end_to_ts(&cc->spd.this, child->sa.st_acquired_sec_label);
child->sa.st_ts_that = ikev2_end_to_ts(&cc->spd.that, child->sa.st_seen_sec_label);
}
v2_emit_ts_payloads(child, outpbs, cc);
if (send_use_transport) {
dbg("Initiator child policy is transport mode, sending v2N_USE_TRANSPORT_MODE");
if (!emit_v2N(v2N_USE_TRANSPORT_MODE, outpbs))
return STF_INTERNAL_ERROR;
} else {
dbg("Initiator child policy is tunnel mode, NOT sending v2N_USE_TRANSPORT_MODE");
}
if (cc->send_no_esp_tfc) {
if (!emit_v2N(v2N_ESP_TFC_PADDING_NOT_SUPPORTED, outpbs))
return STF_INTERNAL_ERROR;
}
return STF_OK;
}
static stf_status ikev2_child_add_ike_payloads(struct child_sa *child,
pb_stream *outpbs)
{
struct state *st = &child->sa;
struct connection *c = st->st_connection;
chunk_t local_nonce;
chunk_t *local_g;
switch (st->st_state->kind) {
case STATE_V2_REKEY_IKE_R0:
{
local_g = &st->st_gr;
local_nonce = st->st_nr;
chunk_t local_spi = THING_AS_CHUNK(st->st_ike_rekey_spis.responder);
/* send selected v2 IKE SA */
if (!ikev2_emit_sa_proposal(outpbs, st->st_accepted_ike_proposal,
&local_spi)) {
dbg("problem emitting accepted ike proposal in CREATE_CHILD_SA");
return STF_INTERNAL_ERROR;
}
break;
}
case STATE_V2_REKEY_IKE_I0:
{
local_g = &st->st_gi;
local_nonce = st->st_ni;
chunk_t local_spi = THING_AS_CHUNK(st->st_ike_rekey_spis.initiator);
struct ikev2_proposals *ike_proposals =
get_v2_ike_proposals(c, "IKE SA initiating rekey",
child->sa.st_logger);
/* send v2 IKE SAs*/
if (!ikev2_emit_sa_proposals(outpbs, ike_proposals,
&local_spi)) {
log_state(RC_LOG, st, "outsa fail");
dbg("problem emitting connection ike proposals in CREATE_CHILD_SA");
return STF_INTERNAL_ERROR;
}
break;
}
default:
bad_case(st->st_state->kind);
}
/* send NONCE */
{
struct ikev2_generic in = {
.isag_critical = build_ikev2_critical(false, child->sa.st_logger),
};
pb_stream nr_pbs;
if (!out_struct(&in, &ikev2_nonce_desc, outpbs, &nr_pbs) ||
!pbs_out_hunk(local_nonce, &nr_pbs, "IKEv2 nonce"))
return STF_INTERNAL_ERROR;
close_output_pbs(&nr_pbs);
}
if (!emit_v2KE(local_g, st->st_oakley.ta_dh, outpbs))
return STF_INTERNAL_ERROR;
return STF_OK;
}
/*
* initiator received Rekey IKE SA (RFC 7296 1.3.3) response
*/
static dh_shared_secret_cb ikev2_child_ike_inR_continue;
stf_status ikev2_child_ike_inR(struct ike_sa *ike,
struct child_sa *child,
struct msg_digest *md)
{
pexpect(child != NULL);
struct state *st = &child->sa;
pexpect(ike != NULL);
pexpect(ike->sa.st_serialno == st->st_clonedfrom);
struct connection *c = st->st_connection;
/* Ni in */
if (!accept_v2_nonce(st->st_logger, md, &st->st_nr, "Nr")) {
/*
* Presumably not our fault. Syntax errors in a
* response kill the family and trigger no further
* exchange.
*/
return STF_FATAL; /* NEED RESTART? */
}
/* Get the proposals ready. */
struct ikev2_proposals *ike_proposals =
get_v2_ike_proposals(c, "IKE SA accept response to rekey",
child->sa.st_logger);
struct payload_digest *const sa_pd = md->chain[ISAKMP_NEXT_v2SA];
stf_status ret = ikev2_process_sa_payload("IKE initiator (accepting)",
&sa_pd->pbs,
/*expect_ike*/ TRUE,
/*expect_spi*/ TRUE,
/*expect_accepted*/ TRUE,
LIN(POLICY_OPPORTUNISTIC, c->policy),
&st->st_accepted_ike_proposal,
ike_proposals, child->sa.st_logger);
if (ret != STF_OK) {
dbg("failed to accept IKE SA, REKEY, response, in ikev2_child_ike_inR");
return ret; /* initiator; no response */
}
if (DBGP(DBG_BASE)) {
DBG_log_ikev2_proposal("accepted IKE proposal",
st->st_accepted_ike_proposal);
}
if (!ikev2_proposal_to_trans_attrs(st->st_accepted_ike_proposal,
&st->st_oakley, st->st_logger)) {
log_state(RC_LOG_SERIOUS, st, "IKE responder accepted an unsupported algorithm");
/* free early return items */
free_ikev2_proposal(&st->st_accepted_ike_proposal);
passert(st->st_accepted_ike_proposal == NULL);
switch_md_st(md, &ike->sa, HERE);
return STF_FAIL;
}
/* KE in */
if (!unpack_KE(&st->st_gr, "Gr", st->st_oakley.ta_dh,
md->chain[ISAKMP_NEXT_v2KE], st->st_logger)) {
/*
* XXX: Initiator so returning this notification will
* go no where. Need to check RFC for what to do
* next. The packet is trusted but the re-key has
* failed.
*/
return STF_FAIL + v2N_INVALID_SYNTAX;
}
/* fill in the missing responder SPI */
passert(!ike_spi_is_zero(&st->st_ike_rekey_spis.initiator));
passert(ike_spi_is_zero(&st->st_ike_rekey_spis.responder));
ikev2_copy_cookie_from_sa(st->st_accepted_ike_proposal,
&st->st_ike_rekey_spis.responder);
/* initiate calculation of g^xy for rekey */
submit_dh_shared_secret(st, st->st_gr/*initiator needs responder's KE*/,
ikev2_child_ike_inR_continue,
HERE);
return STF_SUSPEND;
}
static stf_status ikev2_child_ike_inR_continue(struct state *st,
struct msg_digest *md)
{
dbg("%s() for #%lu %s",
__func__, st->st_serialno, st->st_state->name);
pexpect(v2_msg_role(md) == MESSAGE_RESPONSE); /* i.e., MD!=NULL */
pexpect(md->st == NULL || md->st == st);
struct ike_sa *ike = ike_sa(st, HERE);
struct child_sa *child = pexpect_child_sa(st); /* not yet emancipated */
pexpect(child->sa.st_sa_role == SA_INITIATOR);
pexpect(st->st_state->kind == STATE_V2_REKEY_IKE_I1);
/* and a parent? */
if (ike == NULL) {
pexpect_fail(st->st_logger, HERE,
"sponsoring child state #%lu has no parent state #%lu",
st->st_serialno, st->st_clonedfrom);
/* XXX: release what? */
return STF_INTERNAL_ERROR;
}
if (st->st_dh_shared_secret == NULL) {
/*
* XXX: this is the initiator so returning a
* notification is kind of useless.
*/
return STF_FAIL + v2N_INVALID_SYNTAX;
}
calc_v2_keymat(st,
ike->sa.st_skey_d_nss, /* only IKE has SK_d */
ike->sa.st_oakley.ta_prf, /* for IKE/ESP/AH */
&child->sa.st_ike_rekey_spis/* new SPIs */);
ikev2_rekey_expire_pred(st, st->st_ike_pred);
return STF_OK;
}
/*
* initiator received a create Child SA Response (RFC 7296 1.3.1, 1.3.2)
*
* Note: "when rekeying, the new Child SA SHOULD NOT have different Traffic
* Selectors and algorithms than the old one."
*/
static dh_shared_secret_cb ikev2_child_inR_continue;
stf_status ikev2_child_inR(struct ike_sa *ike,
struct child_sa *child, struct msg_digest *md)
{
pexpect(child != NULL);
struct state *st = &child->sa;
/* Ni in */
if (!accept_v2_nonce(st->st_logger, md, &st->st_nr, "Nr")) {
/*
* Presumably not our fault. Syntax errors in a
* response kill the family (and trigger no further
* exchange).
*/
return STF_FATAL;
}
RETURN_STF_FAILURE_STATUS(ikev2_process_child_sa_pl(ike, child, md, TRUE));
/* XXX: only for rekey child? */
if (st->st_pfs_group == NULL)
return ikev2_process_ts_and_rest(md);
/*
* This is the initiator, accept responder's KE.
*
* XXX: Above checks st_pfs_group but this uses
* st_oakley.ta_dh, presumably they are the same? Lets find
* out.
*/
pexpect(st->st_oakley.ta_dh == st->st_pfs_group);
if (!unpack_KE(&st->st_gr, "Gr", st->st_oakley.ta_dh,
md->chain[ISAKMP_NEXT_v2KE], st->st_logger)) {
/*
* XXX: Initiator so this notification result is going
* no where. What should happen?
*/
return STF_FAIL + v2N_INVALID_SYNTAX; /* XXX: STF_FATAL? */
}
chunk_t remote_ke = st->st_gr;
submit_dh_shared_secret(st, remote_ke, ikev2_child_inR_continue, HERE);
return STF_SUSPEND;
}
static stf_status ikev2_child_inR_continue(struct state *st,
struct msg_digest *md)
{
dbg("%s() for #%lu %s",
__func__, st->st_serialno, st->st_state->name);
/* initiator getting back an answer */
pexpect(v2_msg_role(md) == MESSAGE_RESPONSE); /* i.e., MD!=NULL */
pexpect(md->st == NULL || md->st == st);
struct ike_sa *ike = ike_sa(st, HERE);
struct child_sa *child = pexpect_child_sa(st);
pexpect(child->sa.st_sa_role == SA_INITIATOR);
/*
* XXX: Should this routine be split so that each instance
* handles only one state transition. If there's commonality
* then the per-transition functions can all call common code.
*/
pexpect(st->st_state->kind == STATE_V2_NEW_CHILD_I1 ||
st->st_state->kind == STATE_V2_REKEY_CHILD_I1);
/* and a parent? */
if (ike == NULL) {
pexpect_fail(st->st_logger, HERE,
"sponsoring child state #%lu has no parent state #%lu",
st->st_serialno, st->st_clonedfrom);
/* XXX: release what? */
return STF_FATAL;
}
if (st->st_dh_shared_secret == NULL) {
/*
* XXX: this is the initiator so returning a
* notification is kind of useless.
*/
return STF_FAIL + v2N_INVALID_SYNTAX;
}
return ikev2_process_ts_and_rest(md);
}
/*
* processing a new Child SA (RFC 7296 1.3.1 or 1.3.3) request
*/
static ke_and_nonce_cb ikev2_child_inIoutR_continue;
stf_status ikev2_child_inIoutR(struct ike_sa *ike,
struct child_sa *child,
struct msg_digest *md)
{
stf_status status;
pexpect(child != NULL);
free_chunk_content(&child->sa.st_ni); /* this is from the parent. */
free_chunk_content(&child->sa.st_nr); /* this is from the parent. */
/* Ni in */
if (!accept_v2_nonce(child->sa.st_logger, md, &child->sa.st_ni, "Ni")) {
/*
* Presumably not our fault. Syntax error response
* impicitly kills the family.
*/
record_v2N_response(ike->sa.st_logger, ike, md,
v2N_INVALID_SYNTAX, NULL/*no-data*/,
ENCRYPTED_PAYLOAD);
return STF_FATAL; /* invalid syntax means we're dead */
}
status = ikev2_process_child_sa_pl(ike, child, md, FALSE);
if (status != STF_OK) {
return status;
}
/*
* KE in with old(pst) and matching accepted_oakley from
* proposals
*
* XXX: does this code need to insist that the IKE SA
* replacement has KE or has SA processor handled that by only
* accepting a proposal with KE?
*/
if (child->sa.st_pfs_group != NULL) {
pexpect(child->sa.st_oakley.ta_dh == child->sa.st_pfs_group);
if (!unpack_KE(&child->sa.st_gi, "Gi", child->sa.st_oakley.ta_dh,
md->chain[ISAKMP_NEXT_v2KE], child->sa.st_logger)) {
record_v2N_response(child->sa.st_logger, ike, md, v2N_INVALID_SYNTAX,
NULL/*no data*/, ENCRYPTED_PAYLOAD);
return STF_FAIL;
}
}
/* check N_REKEY_SA in the negotiation */
switch (child->sa.st_state->kind) {
case STATE_V2_REKEY_CHILD_R0:
if (!ikev2_rekey_child_resp(ike, child, md)) {
/* already logged; already recorded */
return STF_FAIL;
}
if (!child_rekey_responder_ts_verify(child, md)) {
record_v2N_response(ike->sa.st_logger, ike, md,
v2N_TS_UNACCEPTABLE, NULL/*no data*/,
ENCRYPTED_PAYLOAD);
return STF_FAIL;
}
pexpect(child->sa.st_ipsec_pred != SOS_NOBODY);
break;
case STATE_V2_NEW_CHILD_R0:
/* state m/c created CHILD SA */
pexpect(child->sa.st_ipsec_pred == SOS_NOBODY);
if (!assign_child_responder_client(ike, child, md)) {
/* already logged; already recorded */
return STF_FAIL;
}
break;
default:
bad_case(child->sa.st_state->kind);
}
/*
* XXX: a quick eyeball suggests that the only difference
* between these two cases is the description.
*
* ??? if we don't have an md (see above) why are we referencing it?
* ??? clang 6.0.0 warns md might be NULL
*
* XXX: 'see above' is lost; this is a responder state
* which _always_ has an MD.
*/
switch (child->sa.st_state->kind) {
case STATE_V2_NEW_CHILD_R0:
/*
* XXX: note the .st_pfs_group vs .st_oakley.ta_dh
* switch-a-roo. Is this because .st_pfs_group is
* acting more like a flag or perhaps, even though DH
* was negotiated it can be ignored?
*/
submit_ke_and_nonce(&child->sa,
child->sa.st_pfs_group != NULL ? child->sa.st_oakley.ta_dh : NULL,
ikev2_child_inIoutR_continue,
"Child Responder KE and nonce nr");
return STF_SUSPEND;
case STATE_V2_REKEY_CHILD_R0:
/*
* XXX: note the .st_pfs_group vs .st_oakley.ta_dh
* switch-a-roo. Is this because .st_pfs_group is
* acting more like a flag or perhaps, even though DH
* was negotiated it can be ignored?
*/
submit_ke_and_nonce(&child->sa,
child->sa.st_pfs_group != NULL ? child->sa.st_oakley.ta_dh : NULL,
ikev2_child_inIoutR_continue,
"Child Rekey Responder KE and nonce nr");
return STF_SUSPEND;
default:
bad_case(child->sa.st_state->kind);
}
}
static dh_shared_secret_cb ikev2_child_inIoutR_continue_continue;
static stf_status ikev2_child_inIoutR_continue(struct state *st,
struct msg_digest *md,
struct dh_local_secret *local_secret,
chunk_t *nonce)
{
dbg("%s() for #%lu %s",
__func__, st->st_serialno, st->st_state->name);
/* responder processing request */
pexpect(v2_msg_role(md) == MESSAGE_REQUEST); /* i.e., MD!=NULL */
pexpect(md->st == NULL || md->st == st);
struct ike_sa *ike = ike_sa(st, HERE);
struct child_sa *child = pexpect_child_sa(st);
pexpect(child->sa.st_sa_role == SA_RESPONDER);
/*
* XXX: Should this routine be split so that each instance
* handles only one state transition. If there's commonality
* then the per-transition functions can all call common code.
*
* Instead of computing the entire DH as a single crypto task,
* does a second continue. Yuck!
*/
pexpect(st->st_state->kind == STATE_V2_NEW_CHILD_R0 ||
st->st_state->kind == STATE_V2_REKEY_CHILD_R0);
/* and a parent? */
if (ike == NULL) {
pexpect_fail(st->st_logger, HERE,
"sponsoring child state #%lu has no parent state #%lu",
st->st_serialno, st->st_clonedfrom);
/* XXX: release what? */
return STF_INTERNAL_ERROR;
}
unpack_nonce(&st->st_nr, nonce);
if (local_secret != NULL) {
unpack_KE_from_helper(st, local_secret, &st->st_gr);
/* initiate calculation of g^xy */
submit_dh_shared_secret(st, st->st_gi, ikev2_child_inIoutR_continue_continue,
HERE);
return STF_SUSPEND;
} else {
return ikev2_child_out_tail(ike, child, md);
}
}
static stf_status ikev2_child_inIoutR_continue_continue(struct state *st,
struct msg_digest *md)
{
dbg("%s() for #%lu %s",
__func__, st->st_serialno, st->st_state->name);
/* 'child' responding to request */
passert(v2_msg_role(md) == MESSAGE_REQUEST); /* i.e., MD!=NULL */
pexpect(md->st == NULL || md->st == st);
struct ike_sa *ike = ike_sa(st, HERE);
struct child_sa *child = pexpect_child_sa(st);
passert(child->sa.st_sa_role == SA_RESPONDER);
/*
* XXX: Should this routine be split so that each instance
* handles only one state transition. If there's commonality
* then the per-transition functions can all call common code.
*/
pexpect(child->sa.st_state->kind == STATE_V2_NEW_CHILD_R0 ||
child->sa.st_state->kind == STATE_V2_REKEY_CHILD_R0);
/* didn't loose parent? */
if (ike == NULL) {
pexpect_fail(st->st_logger, HERE,
"sponsoring child state #%lu has no parent state #%lu",
st->st_serialno, st->st_clonedfrom);
/* XXX: release child? */
return STF_FATAL;
}
if (st->st_dh_shared_secret == NULL) {
log_state(RC_LOG, &child->sa, "DH failed");
record_v2N_response(child->sa.st_logger, ike, md,
v2N_INVALID_SYNTAX, NULL,
ENCRYPTED_PAYLOAD);
return STF_FATAL; /* kill family */
}
return ikev2_child_out_tail(ike, child, md);
}
/*
* processing a new Rekey IKE SA (RFC 7296 1.3.2) request
*/
static ke_and_nonce_cb ikev2_child_ike_inIoutR_continue;
stf_status ikev2_child_ike_inIoutR(struct ike_sa *ike,
struct child_sa *child,
struct msg_digest *md)
{
pexpect(child != NULL); /* not yet emancipated */
struct state *st = &child->sa;
pexpect(ike != NULL);
struct connection *c = st->st_connection;
free_chunk_content(&st->st_ni); /* this is from the parent. */
free_chunk_content(&st->st_nr); /* this is from the parent. */
/* Ni in */
if (!accept_v2_nonce(st->st_logger, md, &st->st_ni, "Ni")) {
/*
* Presumably not our fault. A syntax error response
* implicitly kills the entire family.
*/
record_v2N_response(ike->sa.st_logger, ike, md,
v2N_INVALID_SYNTAX, NULL/*no-data*/,
ENCRYPTED_PAYLOAD);
return STF_FATAL; /* we're doomed */
}
/* Get the proposals ready. */
struct ikev2_proposals *ike_proposals =
get_v2_ike_proposals(c, "IKE SA responding to rekey", ike->sa.st_logger);
struct payload_digest *const sa_pd = md->chain[ISAKMP_NEXT_v2SA];
stf_status ret = ikev2_process_sa_payload("IKE Rekey responder child",
&sa_pd->pbs,
/*expect_ike*/ TRUE,
/*expect_spi*/ TRUE,
/*expect_accepted*/ FALSE,
LIN(POLICY_OPPORTUNISTIC, c->policy),
&st->st_accepted_ike_proposal,
ike_proposals, child->sa.st_logger);
if (ret != STF_OK) {
pexpect(child->sa.st_sa_role == SA_RESPONDER);
pexpect(ret > STF_FAIL);
record_v2N_response(child->sa.st_logger, ike, md, ret - STF_FAIL, NULL,
ENCRYPTED_PAYLOAD);
return STF_FAIL;
}
if (DBGP(DBG_BASE)) {
DBG_log_ikev2_proposal("accepted IKE proposal",
st->st_accepted_ike_proposal);
}
if (!ikev2_proposal_to_trans_attrs(st->st_accepted_ike_proposal,
&st->st_oakley, st->st_logger)) {
log_state(RC_LOG_SERIOUS, st, "IKE responder accepted an unsupported algorithm");
/*
* XXX; where is 'st' freed? Should the code instead
* tunnel back md.st==st and return STF_FATAL which
* will delete the child state? Or perhaps there a
* lurking SO_DISPOSE to clean it up?
*/
switch_md_st(md, &ike->sa, HERE);
return STF_IGNORE;
}
if (!v2_accept_ke_for_proposal(ike, &child->sa, md,
st->st_oakley.ta_dh,
ENCRYPTED_PAYLOAD)) {
/* passert(reply-recorded) */
return STF_FAIL;
}
/*
* Check and read the KE contents.
*
* responder, so accept initiator's KE in with new
* accepted_oakley for IKE.
*/
pexpect(st->st_oakley.ta_dh != NULL);
pexpect(st->st_pfs_group == NULL);
if (!unpack_KE(&st->st_gi, "Gi", st->st_oakley.ta_dh,
md->chain[ISAKMP_NEXT_v2KE], st->st_logger)) {
record_v2N_response(ike->sa.st_logger, ike, md,
v2N_INVALID_SYNTAX, NULL/*no data*/,
ENCRYPTED_PAYLOAD);
return STF_FATAL; /* kill family */
}
submit_ke_and_nonce(st, st->st_oakley.ta_dh,
ikev2_child_ike_inIoutR_continue,
"IKE rekey KE response gir");
return STF_SUSPEND;
}
static dh_shared_secret_cb ikev2_child_ike_inIoutR_continue_continue; /* type assertion */
static stf_status ikev2_child_ike_inIoutR_continue(struct state *st,
struct msg_digest *md,
struct dh_local_secret *local_secret,
chunk_t *nonce)
{
dbg("%s() for #%lu %s",
__func__, st->st_serialno, st->st_state->name);
/* responder processing request */
pexpect(v2_msg_role(md) == MESSAGE_REQUEST); /* i.e., MD!=NULL */
pexpect(md->st == NULL || md->st == st);
struct ike_sa *ike = ike_sa(st, HERE);
struct child_sa *child = pexpect_child_sa(st); /* not yet emancipated */
pexpect(child->sa.st_sa_role == SA_RESPONDER);
pexpect(st->st_state->kind == STATE_V2_REKEY_IKE_R0);
/* and a parent? */
if (ike == NULL) {
pexpect_fail(st->st_logger, HERE,
"sponsoring child state #%lu has no parent state #%lu",
st->st_serialno, st->st_clonedfrom);
/* XXX: release what? */
return STF_INTERNAL_ERROR;
}
pexpect(local_secret != NULL);
pexpect(md->chain[ISAKMP_NEXT_v2KE] != NULL);
unpack_nonce(&st->st_nr, nonce);
unpack_KE_from_helper(st, local_secret, &st->st_gr);
/* initiate calculation of g^xy */
passert(ike_spi_is_zero(&st->st_ike_rekey_spis.initiator));
passert(ike_spi_is_zero(&st->st_ike_rekey_spis.responder));
ikev2_copy_cookie_from_sa(st->st_accepted_ike_proposal,
&st->st_ike_rekey_spis.initiator);
st->st_ike_rekey_spis.responder = ike_responder_spi(&md->sender,
st->st_logger);
submit_dh_shared_secret(st, st->st_gi/*responder needs initiator KE*/,
ikev2_child_ike_inIoutR_continue_continue,
HERE);
return STF_SUSPEND;
}
static stf_status ikev2_child_ike_inIoutR_continue_continue(struct state *st,
struct msg_digest *md)
{
dbg("%s() for #%lu %s",
__func__, st->st_serialno, st->st_state->name);
/* 'child' responding to request */
passert(v2_msg_role(md) == MESSAGE_REQUEST); /* i.e., MD!=NULL */
pexpect(md->st == NULL || md->st == st);
struct ike_sa *ike = ike_sa(st, HERE);
struct child_sa *child = pexpect_child_sa(st); /* not yet emancipated */
passert(child->sa.st_sa_role == SA_RESPONDER);
pexpect(st->st_state->kind == STATE_V2_REKEY_IKE_R0);
/* didn't loose parent? */
if (ike == NULL) {
pexpect_fail(st->st_logger, HERE,
"sponsoring child state #%lu has no parent state #%lu",
st->st_serialno, st->st_clonedfrom);
/* XXX: release child? */
return STF_INTERNAL_ERROR;
}
if (st->st_dh_shared_secret == NULL) {
record_v2N_response(ike->sa.st_logger, ike, md,
v2N_INVALID_SYNTAX, NULL,
ENCRYPTED_PAYLOAD);
return STF_FATAL; /* kill family */
}
calc_v2_keymat(st,
ike->sa.st_skey_d_nss, /* only IKE has SK_d */
ike->sa.st_oakley.ta_prf, /* for IKE/ESP/AH */
&st->st_ike_rekey_spis);
return ikev2_child_out_tail(ike, child, md);
}
static stf_status ikev2_child_out_tail(struct ike_sa *ike, struct child_sa *child,
struct msg_digest *request_md)
{
stf_status ret;
passert(ike != NULL);
pexpect((request_md != NULL) == (child->sa.st_sa_role == SA_RESPONDER));
/* 3 initiator initiating states */
pexpect((request_md == NULL) == (child->sa.st_state->kind == STATE_V2_REKEY_IKE_I0 ||
child->sa.st_state->kind == STATE_V2_NEW_CHILD_I0 ||
child->sa.st_state->kind == STATE_V2_REKEY_CHILD_I0));
/* 3 responder replying states */
pexpect((request_md != NULL) == (child->sa.st_state->kind == STATE_V2_REKEY_IKE_R0 ||
child->sa.st_state->kind == STATE_V2_NEW_CHILD_R0 ||
child->sa.st_state->kind == STATE_V2_REKEY_CHILD_R0));
/* 3 initiator receiving; can't happen here */
pexpect(child->sa.st_state->kind != STATE_V2_REKEY_IKE_I1 &&
child->sa.st_state->kind != STATE_V2_NEW_CHILD_I1 &&
child->sa.st_state->kind != STATE_V2_REKEY_CHILD_I1);
ikev2_log_parentSA(&child->sa);
struct pbs_out reply_stream = open_pbs_out("reply packet",
reply_buffer, sizeof(reply_buffer),
child->sa.st_logger);
/* HDR out Start assembling respone message */
pb_stream rbody = open_v2_message(&reply_stream, ike, request_md,
ISAKMP_v2_CREATE_CHILD_SA);
/* insert an Encryption payload header */
v2SK_payload_t sk = open_v2SK_payload(child->sa.st_logger, &rbody, ike);
if (!pbs_ok(&sk.pbs)) {
return STF_INTERNAL_ERROR;
}
switch (child->sa.st_state->kind) {
case STATE_V2_REKEY_IKE_R0:
case STATE_V2_REKEY_IKE_I0:
ret = ikev2_child_add_ike_payloads(child, &sk.pbs);
break;
case STATE_V2_NEW_CHILD_I0:
case STATE_V2_REKEY_CHILD_I0:
ret = ikev2_child_add_ipsec_payloads(child, &sk.pbs);
break;
case STATE_V2_NEW_CHILD_R0:
if (!pexpect(child->sa.st_ipsec_pred == SOS_NOBODY)) {
return STF_INTERNAL_ERROR;
}
ret = ikev2_child_sa_respond(ike, child,
request_md, &sk.pbs,
ISAKMP_v2_CREATE_CHILD_SA);
break;
case STATE_V2_REKEY_CHILD_R0:
if (!pexpect(child->sa.st_ipsec_pred != SOS_NOBODY)) {
return STF_INTERNAL_ERROR;
}
if (!ikev2_rekey_child_copy_ts(child)) {
/* Should "just work", not working is a screw up */
return STF_INTERNAL_ERROR;
}
ret = ikev2_child_sa_respond(ike, child,
request_md, &sk.pbs,
ISAKMP_v2_CREATE_CHILD_SA);
break;
case STATE_V2_REKEY_IKE_I1:
case STATE_V2_NEW_CHILD_I1:
case STATE_V2_REKEY_CHILD_I1:
return STF_INTERNAL_ERROR;
default:
bad_case(child->sa.st_state->kind);
}
if (ret != STF_OK) {
LSWDBGP(DBG_BASE, buf) {
jam(buf, "ikev2_child_sa_respond returned ");
jam_v2_stf_status(buf, ret);
}
return ret; /* abort building the response message */
}
/* note: pst: parent; md->st: child */
/* const unsigned int len = pbs_offset(&sk.pbs); */
if (!close_v2SK_payload(&sk)) {
return STF_INTERNAL_ERROR;
}
close_output_pbs(&rbody);
close_output_pbs(&reply_stream);
ret = encrypt_v2SK_payload(&sk);
if (ret != STF_OK)
return ret;
/*
* CREATE_CHILD_SA request and response are small 300 - 750 bytes.
* ??? Should we support fragmenting? Maybe one day.
*/
record_v2_message(ike, &reply_stream,
"packet from ikev2_child_out_cont",
request_md != NULL ? MESSAGE_RESPONSE : MESSAGE_REQUEST);
if (child->sa.st_state->kind == STATE_V2_NEW_CHILD_R0 ||
child->sa.st_state->kind == STATE_V2_REKEY_CHILD_R0) {
log_ipsec_sa_established("negotiated new IPsec SA", &child->sa);
}
return STF_OK;
}
static stf_status ikev2_start_new_exchange(struct ike_sa *ike,
struct child_sa *child)
{
switch (child->sa.st_establishing_sa) { /* where we're going */
case IKE_SA:
return STF_OK;
case IPSEC_SA: /* CHILD_SA */
if (!ike->sa.st_viable_parent) {
child->sa.st_policy = child->sa.st_connection->policy; /* for pick_initiator */
log_state(RC_LOG_SERIOUS, &child->sa,
"no viable to parent to initiate CREATE_CHILD_EXCHANGE %s; trying replace",
child->sa.st_state->name);
delete_event(&child->sa);
event_schedule(EVENT_SA_REPLACE, REPLACE_ORPHAN_DELAY, &child->sa);
/* ??? surely this isn't yet a failure or a success */
return STF_FAIL;
}
return STF_OK;
default:
bad_case(child->sa.st_establishing_sa);
}
}
static void delete_or_replace_child(struct ike_sa *ike, struct child_sa *child)
{
/* the CHILD's connection; not IKE's */
struct connection *c = child->sa.st_connection;
if (child->sa.st_event == NULL) {
/*
* ??? should this be an assert/expect?
*/
log_state(RC_LOG_SERIOUS, &ike->sa,
"received Delete SA payload: delete CHILD SA #%lu. st_event == NULL",
child->sa.st_serialno);
delete_state(&child->sa);
} else if (child->sa.st_event->ev_type == EVENT_SA_EXPIRE) {
/*
* this state was going to EXPIRE: hurry it along
*
* ??? why is this treated specially. Can we not
* delete_state()?
*/
log_state(RC_LOG_SERIOUS, &ike->sa,
"received Delete SA payload: expire CHILD SA #%lu now",
child->sa.st_serialno);
event_force(EVENT_SA_EXPIRE, &child->sa);
} else if (c->newest_ipsec_sa == child->sa.st_serialno &&
(c->policy & POLICY_UP)) {
/*
* CHILD SA for a permanent connection that we have
* initiated. Replace it now. Useful if the other
* peer is rebooting.
*/
log_state(RC_LOG_SERIOUS, &ike->sa,
"received Delete SA payload: replace CHILD SA #%lu now",
child->sa.st_serialno);
child->sa.st_replace_margin = deltatime(0);
event_force(EVENT_SA_REPLACE, &child->sa);
} else {
log_state(RC_LOG_SERIOUS, &ike->sa,
"received Delete SA payload: delete CHILD SA #%lu now",
child->sa.st_serialno);
delete_state(&child->sa);
}
}
/* can an established state initiate or respond to mobike probe */
static bool mobike_check_established(const struct state *st)
{
struct connection *c = st->st_connection;
/* notice tricky use of & on booleans */
bool ret = LIN(POLICY_MOBIKE, c->policy) &
st->st_seen_mobike & st->st_sent_mobike &
IS_ISAKMP_SA_ESTABLISHED(st->st_state);
return ret;
}
static bool process_mobike_resp(struct msg_digest *md)
{
struct state *st = md->st;
struct ike_sa *ike = ike_sa(st, HERE);
bool may_mobike = mobike_check_established(st);
/* ??? there is currently no need for separate natd_[sd] variables */
bool natd_s = FALSE;
bool natd_d = FALSE;
struct payload_digest *ntfy;
if (!may_mobike) {
return FALSE;
}
for (ntfy = md->chain[ISAKMP_NEXT_v2N]; ntfy != NULL; ntfy = ntfy->next) {
switch (ntfy->payload.v2n.isan_type) {
case v2N_NAT_DETECTION_DESTINATION_IP:
natd_d = TRUE;
dbg("TODO: process %s in MOBIKE response ",
enum_name(&ikev2_notify_names, ntfy->payload.v2n.isan_type));
break;
case v2N_NAT_DETECTION_SOURCE_IP:
natd_s = TRUE;
dbg("TODO: process %s in MOBIKE response ",
enum_name(&ikev2_notify_names, ntfy->payload.v2n.isan_type));
break;
}
}
/* use of bitwise & on bool values is correct but odd */
bool ret = natd_s & natd_d;
if (ret && !update_mobike_endpoints(ike, md)) {
/* IPs already updated from md */
return FALSE;
}
update_ike_endpoints(ike, md); /* update state sender so we can find it for IPsec SA */
return ret;
}
/* currently we support only MOBIKE notifies and v2N_REDIRECT notify */
static void process_informational_notify_req(struct msg_digest *md, bool *redirect, bool *ntfy_natd,
chunk_t *cookie2)
{
struct payload_digest *ntfy;
struct state *st = md->st;
struct ike_sa *ike = ike_sa(st, HERE);
bool may_mobike = mobike_check_established(st);
bool ntfy_update_sa = FALSE;
ip_address redirect_ip;
for (ntfy = md->chain[ISAKMP_NEXT_v2N]; ntfy != NULL; ntfy = ntfy->next) {
switch (ntfy->payload.v2n.isan_type) {
case v2N_REDIRECT:
dbg("received v2N_REDIRECT in informational");
err_t e = parse_redirect_payload(&ntfy->pbs,
st->st_connection->accept_redirect_to,
NULL,
&redirect_ip,
ike->sa.st_logger);
if (e != NULL) {
log_state(RC_LOG_SERIOUS, st,
"warning: parsing of v2N_REDIRECT payload failed: %s", e);
} else {
*redirect = TRUE;
st->st_connection->temp_vars.redirect_ip = redirect_ip;
}
return;
case v2N_UPDATE_SA_ADDRESSES:
if (may_mobike) {
ntfy_update_sa = TRUE;
dbg("Need to process v2N_UPDATE_SA_ADDRESSES");
} else {
log_state(RC_LOG, st, "Connection does not allow MOBIKE, ignoring UPDATE_SA_ADDRESSES");
}
break;
case v2N_NO_NATS_ALLOWED:
if (may_mobike)
st->st_seen_nonats = TRUE;
else
log_state(RC_LOG, st, "Connection does not allow MOBIKE, ignoring v2N_NO_NATS_ALLOWED");
break;
case v2N_NAT_DETECTION_DESTINATION_IP:
case v2N_NAT_DETECTION_SOURCE_IP:
*ntfy_natd = TRUE;
dbg("TODO: Need to process NAT DETECTION payload if we are initiator");
break;
case v2N_NO_ADDITIONAL_ADDRESSES:
if (may_mobike) {
dbg("Received NO_ADDITIONAL_ADDRESSES - no need to act on this");
} else {
log_state(RC_LOG, st, "Connection does not allow MOBIKE, ignoring NO_ADDITIONAL_ADDRESSES payload");
}
break;
case v2N_COOKIE2:
if (may_mobike) {
/* copy cookie */
if (ntfy->payload.v2n.isan_length > IKEv2_MAX_COOKIE_SIZE) {
dbg("MOBIKE COOKIE2 notify payload too big - ignored");
} else {
const pb_stream *dc_pbs = &ntfy->pbs;
*cookie2 = clone_bytes_as_chunk(dc_pbs->cur, pbs_left(dc_pbs),
"saved cookie2");
DBG_dump_hunk("MOBIKE COOKIE2 received:", *cookie2);
}
} else {
log_state(RC_LOG, st, "Connection does not allow MOBIKE, ignoring COOKIE2");
}
break;
case v2N_ADDITIONAL_IP4_ADDRESS:
dbg("ADDITIONAL_IP4_ADDRESS payload ignored (not yet supported)");
/* not supported yet */
break;
case v2N_ADDITIONAL_IP6_ADDRESS:
dbg("ADDITIONAL_IP6_ADDRESS payload ignored (not yet supported)");
/* not supported yet */
break;
default:
dbg("Received unexpected %s notify - ignored",
enum_name(&ikev2_notify_names, ntfy->payload.v2n.isan_type));
break;
}
}
if (ntfy_update_sa) {
if (LHAS(st->hidden_variables.st_nat_traversal, NATED_HOST)) {
log_state(RC_LOG, st, "Ignoring MOBIKE UPDATE_SA since we are behind NAT");
} else {
if (!update_mobike_endpoints(ike, md))
*ntfy_natd = FALSE;
update_ike_endpoints(ike, md); /* update state sender so we can find it for IPsec SA */
}
}
if (may_mobike && !ntfy_update_sa && *ntfy_natd &&
!LHAS(st->hidden_variables.st_nat_traversal, NATED_HOST)) {
/*
* If this is a MOBIKE probe, use the received IP:port
* for only this reply packet, without updating IKE
* endpoint and without UPDATE_SA.
*/
st->st_mobike_remote_endpoint = md->sender;
}
if (ntfy_update_sa)
log_state(RC_LOG, st, "MOBIKE request: updating IPsec SA by request");
else
dbg("MOBIKE request: not updating IPsec SA");
}
static void mobike_reset_remote(struct state *st, struct mobike *est_remote)
{
if (est_remote->interface == NULL)
return;
st->st_remote_endpoint = est_remote->remote;
st->st_interface = est_remote->interface;
pexpect_st_local_endpoint(st);
st->st_mobike_remote_endpoint = unset_endpoint;
}
/* MOBIKE liveness/update response. set temp remote address/interface */
static void mobike_switch_remote(struct msg_digest *md, struct mobike *est_remote)
{
struct state *st = md->st;
est_remote->interface = NULL;
if (mobike_check_established(st) &&
!LHAS(st->hidden_variables.st_nat_traversal, NATED_HOST) &&
!endpoint_eq(&md->sender, &st->st_remote_endpoint)) {
/* remember the established/old address and interface */
est_remote->remote = st->st_remote_endpoint;
est_remote->interface = st->st_interface;
/* set temp one and after the message sent reset it */
st->st_remote_endpoint = md->sender;
st->st_interface = md->iface;
pexpect_st_local_endpoint(st);
}
}
static stf_status add_mobike_response_payloads(
chunk_t *cookie2, /* freed by us */
struct msg_digest *md,
pb_stream *pbs)
{
dbg("adding NATD%s payloads to MOBIKE response",
cookie2->len != 0 ? " and cookie2" : "");
stf_status r = STF_INTERNAL_ERROR;
struct state *st = md->st;
/* assumptions from ikev2_out_nat_v2n() and caller */
pexpect(v2_msg_role(md) == MESSAGE_REQUEST);
pexpect(!ike_spi_is_zero(&st->st_ike_spis.responder));
if (ikev2_out_nat_v2n(pbs, st, &st->st_ike_spis.responder) &&
(cookie2->len == 0 || emit_v2N_hunk(v2N_COOKIE2, *cookie2, pbs)))
r = STF_OK;
free_chunk_content(cookie2);
return r;
}
/*
*
***************************************************************
* INFORMATIONAL *****
***************************************************************
* -
*
*
*/
/* RFC 5996 1.4 "The INFORMATIONAL Exchange"
*
* HDR, SK {[N,] [D,] [CP,] ...} -->
* <-- HDR, SK {[N,] [D,] [CP], ...}
*/
stf_status process_encrypted_informational_ikev2(struct ike_sa *ike,
struct child_sa *null_child,
struct msg_digest *md)
{
pexpect(null_child == NULL);
int ndp = 0; /* number Delete payloads for IPsec protocols */
bool del_ike = false; /* any IKE SA Deletions? */
bool seen_and_parsed_redirect = FALSE;
/*
* we need connection and boolean below
* in a separate variables because we
* do something with them after we delete
* the state.
*
* XXX: which is of course broken; code should return
* STF_ZOMBIFY and and let state machine clean things up.
*/
struct connection *c = ike->sa.st_connection;
bool do_unroute = ike->sa.st_sent_redirect && c->kind == CK_PERMANENT;
chunk_t cookie2 = empty_chunk;
/* Are we responding (as opposed to processing a response)? */
const bool responding = v2_msg_role(md) == MESSAGE_REQUEST;
dbg("an informational %s ", responding ? "request should send a response" : "response");
/*
* Process NOTIFY payloads - ignore MOBIKE when deleting
*/
bool send_mobike_resp = false; /* only if responding */
if (md->chain[ISAKMP_NEXT_v2D] == NULL) {
if (responding) {
process_informational_notify_req(md, &seen_and_parsed_redirect, &send_mobike_resp, &cookie2);
} else {
if (process_mobike_resp(md)) {
log_state(RC_LOG, &ike->sa,
"MOBIKE response: updating IPsec SA");
} else {
dbg("MOBIKE response: not updating IPsec SA");
}
}
} else {
/*
* RFC 7296 1.4.1 "Deleting an SA with INFORMATIONAL Exchanges"
*/
/*
* Pass 1 over Delete Payloads:
*
* - Count number of IPsec SA Delete Payloads
* - notice any IKE SA Delete Payload
* - sanity checking
*/
for (struct payload_digest *p = md->chain[ISAKMP_NEXT_v2D];
p != NULL; p = p->next) {
struct ikev2_delete *v2del = &p->payload.v2delete;
switch (v2del->isad_protoid) {
case PROTO_ISAKMP:
if (!responding) {
log_state(RC_LOG, &ike->sa,
"Response to Delete improperly includes IKE SA");
return STF_FAIL + v2N_INVALID_SYNTAX;
}
if (del_ike) {
log_state(RC_LOG, &ike->sa,
"Error: INFORMATIONAL Exchange with more than one Delete Payload for the IKE SA");
return STF_FAIL + v2N_INVALID_SYNTAX;
}
if (v2del->isad_nrspi != 0 || v2del->isad_spisize != 0) {
log_state(RC_LOG, &ike->sa,
"IKE SA Delete has non-zero SPI size or number of SPIs");
return STF_FAIL + v2N_INVALID_SYNTAX;
}
del_ike = true;
break;
case PROTO_IPSEC_AH:
case PROTO_IPSEC_ESP:
if (v2del->isad_spisize != sizeof(ipsec_spi_t)) {
log_state(RC_LOG, &ike->sa,
"IPsec Delete Notification has invalid SPI size %u",
v2del->isad_spisize);
return STF_FAIL + v2N_INVALID_SYNTAX;
}
if (v2del->isad_nrspi * v2del->isad_spisize != pbs_left(&p->pbs)) {
log_state(RC_LOG, &ike->sa,
"IPsec Delete Notification payload size is %zu but %u is required",
pbs_left(&p->pbs),
v2del->isad_nrspi * v2del->isad_spisize);
return STF_FAIL + v2N_INVALID_SYNTAX;
}
ndp++;
break;
default:
log_state(RC_LOG, &ike->sa,
"Ignored bogus delete protoid '%d'", v2del->isad_protoid);
}
}
if (del_ike && ndp != 0)
log_state(RC_LOG, &ike->sa,
"Odd: INFORMATIONAL Exchange deletes IKE SA and yet also deletes some IPsec SA");
}
/*
* response packet preparation: DELETE or non-delete (eg MOBIKE/keepalive/REDIRECT)
*
* There can be at most one Delete Payload for an IKE SA.
* It means that this very SA is to be deleted.
*
* For each non-IKE Delete Payload we receive,
* we respond with a corresponding Delete Payload.
* Note that that means we will have an empty response
* if no Delete Payloads came in or if the only
* Delete Payload is for an IKE SA.
*
* If we received NAT detection payloads as per MOBIKE, send answers
*/
/*
* Variables for generating response.
* NOTE: only meaningful if "responding" is true!
* These declarations must be placed so early because they must be in scope for
* all of the several chunks of code that handle responding.
*
* XXX: in terms of readability and reliability, this
* interleaving of initiator vs response code paths is pretty
* screwed up.
*/
struct pbs_out reply_stream;
pb_stream rbody;
v2SK_payload_t sk;
zero(&rbody);
zero(&sk);
if (responding) {
/* make sure HDR is at start of a clean buffer */
reply_stream = open_pbs_out("information exchange reply packet",
reply_buffer, sizeof(reply_buffer),
ike->sa.st_logger);
/* authenticated decrypted response - It's alive, alive! */
dbg("Received an INFORMATIONAL response, updating st_last_liveness, no pending_liveness");
ike->sa.st_last_liveness = mononow();
ike->sa.st_pend_liveness = false;
/* HDR out */
rbody = open_v2_message(&reply_stream, ike,
md /* response */,
ISAKMP_v2_INFORMATIONAL);
if (!pbs_ok(&rbody)) {
return STF_INTERNAL_ERROR;
}
/* insert an Encryption payload header */
sk = open_v2SK_payload(ike->sa.st_logger, &rbody, ike);
if (!pbs_ok(&sk.pbs)) {
return STF_INTERNAL_ERROR;
}
if (send_mobike_resp) {
stf_status e = add_mobike_response_payloads(
&cookie2, /* will be freed */
md, &sk.pbs);
if (e != STF_OK)
return e;
}
}
/*
* This happens when we are original initiator,
* and we received REDIRECT payload during the active
* session.
*/
if (seen_and_parsed_redirect)
event_force(EVENT_v2_REDIRECT, &ike->sa);
/*
* Do the actual deletion.
* If responding, build the body of the response.
*/
if (!responding && ike->sa.st_state->kind == STATE_IKESA_DEL) {
/*
* this must be a response to our IKE SA delete request
* Even if there are are other Delete Payloads,
* they cannot matter: we delete the family.
*/
delete_ike_family(ike, DONT_SEND_DELETE);
md->st = NULL;
ike = NULL;
} else if (!responding && md->chain[ISAKMP_NEXT_v2D] == NULL) {
/*
* A liveness update response is handled here
*/
dbg("Received an INFORMATIONAL non-delete request; updating liveness, no longer pending.");
ike->sa.st_last_liveness = mononow();
ike->sa.st_pend_liveness = false;
} else if (del_ike) {
/*
* If we are deleting the Parent SA, the Child SAs will be torn down as well,
* so no point processing the other Delete SA payloads.
* We won't catch nonsense in those payloads.
*
* But wait: we cannot delete the IKE SA until after
* we've sent the response packet. To be continued
* below ...
*/
passert(responding);
} else {
/*
* Pass 2 over the Delete Payloads:
* Actual IPsec SA deletion.
* If responding, build response Delete Payloads.
* If there is no payload, this loop is a no-op.
*/
for (struct payload_digest *p = md->chain[ISAKMP_NEXT_v2D];
p != NULL; p = p->next) {
struct ikev2_delete *v2del = &p->payload.v2delete;
switch (v2del->isad_protoid) {
case PROTO_ISAKMP:
passert_fail(ike->sa.st_logger, HERE, "unexpected IKE delete");
case PROTO_IPSEC_AH: /* Child SAs */
case PROTO_IPSEC_ESP: /* Child SAs */
{
/* stuff for responding */
ipsec_spi_t spi_buf[128];
uint16_t j = 0; /* number of SPIs in spi_buf */
uint16_t i;
for (i = 0; i < v2del->isad_nrspi; i++) {
ipsec_spi_t spi;
diag_t d = pbs_in_raw( &p->pbs, &spi, sizeof(spi),"SPI");
if (d != NULL) {
log_diag(RC_LOG, ike->sa.st_logger, &d, "%s", "");
return STF_INTERNAL_ERROR; /* cannot happen */
}
dbg("delete %s SA(0x%08" PRIx32 ")",
enum_show(&ikev2_delete_protocol_id_names,
v2del->isad_protoid),
ntohl((uint32_t) spi));
/*
* From 3.11. Delete Payload:
* [the delete payload will]
* contain the IPsec protocol
* ID of that protocol (2 for
* AH, 3 for ESP), and the SPI
* is the SPI the sending
* endpoint would expect in
* inbound ESP or AH packets.
*
* From our POV, that's the
* outbound SPI.
*/
struct child_sa *dst = find_v2_child_sa_by_outbound_spi(ike,
v2del->isad_protoid,
spi);
if (dst == NULL) {
log_state(RC_LOG, &ike->sa,
"received delete request for %s SA(0x%08" PRIx32 ") but corresponding state not found",
enum_show(&ikev2_delete_protocol_id_names,
v2del->isad_protoid),
ntohl((uint32_t)spi));
} else {
dbg("our side SPI that needs to be deleted: %s SA(0x%08" PRIx32 ")",
enum_show(&ikev2_delete_protocol_id_names,
v2del->isad_protoid), ntohl((uint32_t)spi));
/* we just received a delete, don't send another delete */
dst->sa.st_dont_send_delete = true;
/* st is a parent */
passert(&ike->sa != &dst->sa);
passert(ike->sa.st_serialno == dst->sa.st_clonedfrom);
if (!del_ike && responding) {
struct ipsec_proto_info *pr =
v2del->isad_protoid == PROTO_IPSEC_AH ?
&dst->sa.st_ah :
&dst->sa.st_esp;
if (j < elemsof(spi_buf)) {
spi_buf[j] = pr->our_spi;
j++;
} else {
log_state(RC_LOG, &ike->sa,
"too many SPIs in Delete Notification payload; ignoring 0x%08" PRIx32,
ntohl(spi));
}
}
delete_or_replace_child(ike, dst);
/* note: md->st != dst */
}
} /* for each spi */
if (!del_ike && responding) {
/* build output Delete Payload */
struct ikev2_delete v2del_tmp = {
.isad_protoid = v2del->isad_protoid,
.isad_spisize = v2del->isad_spisize,
.isad_nrspi = j,
};
/* Emit delete payload header and SPI values */
pb_stream del_pbs; /* output stream */
if (!out_struct(&v2del_tmp,
&ikev2_delete_desc,
&sk.pbs,
&del_pbs))
return false;
diag_t d = pbs_out_raw(&del_pbs,
spi_buf,
j * sizeof(spi_buf[0]),
"local SPIs");
if (d != NULL) {
log_diag(RC_LOG_SERIOUS, sk.logger, &d, "%s", "");
return STF_INTERNAL_ERROR;
}
close_output_pbs(&del_pbs);
}
}
break;
default:
/* ignore unrecognized protocol */
break;
}
} /* for each Delete Payload */
}
if (responding) {
/*
* We've now build up the content (if any) of the Response:
*
* - empty, if there were no Delete Payloads or if we are
* responding to v2N_REDIRECT payload (RFC 5685 Chapter 5).
* Treat as a check for liveness. Correct response is this
* empty Response.
*
* - if an ISAKMP SA is mentioned in input message,
* we are sending an empty Response, as per standard.
*
* - for IPsec SA mentioned, we are sending its mate.
*
* - for MOBIKE, we send NAT NOTIFY payloads and optionally a COOKIE2
*
* Close up the packet and send it.
*/
/* const size_t len = pbs_offset(&sk.pbs); */
if (!close_v2SK_payload(&sk)) {
return STF_INTERNAL_ERROR;
}
close_output_pbs(&rbody);
close_output_pbs(&reply_stream);
;
stf_status ret = encrypt_v2SK_payload(&sk);
if (ret != STF_OK)
return ret;
struct mobike mobike_remote;
mobike_switch_remote(md, &mobike_remote);
/* ??? should we support fragmenting? Maybe one day. */
record_v2_message(ike, &reply_stream, "reply packet for process_encrypted_informational_ikev2",
MESSAGE_RESPONSE);
send_recorded_v2_message(ike, "reply packet for process_encrypted_informational_ikev2",
MESSAGE_RESPONSE);
/*
* XXX: This code should be neither using record 'n'
* send (which leads to RFC violations because it
* doesn't wait for an ACK) and/or be deleting the
* state midway through a state transition.
*
* When DEL_IKE, the update isn't needed but what
* ever.
*/
dbg_v2_msgid(ike, &ike->sa, "XXX: in %s() hacking around record 'n' send bypassing send queue hacking around delete_ike_family()",
__func__);
v2_msgid_update_sent(ike, &ike->sa, md, MESSAGE_RESPONSE);
mobike_reset_remote(&ike->sa, &mobike_remote);
/*
* ... now we can delete the IKE SA if we want to.
*
* The response is hopefully empty.
*/
if (del_ike) {
delete_ike_family(ike, DONT_SEND_DELETE);
md->st = NULL;
ike = NULL;
}
}
/*
* This is a special case. When we have site to site connection
* and one site redirects other in IKE_AUTH reply, he doesn't
* unroute. It seems like it was easier to add here this part
* than in delete_ipsec_sa() in kernel.c where it should be
* (at least it seems like it should be there).
*
* The need for this special case was discovered by running
* various test cases.
*/
if (do_unroute) {
unroute_connection(c);
}
/* count as DPD/liveness only if there was no Delete */
if (!del_ike && ndp == 0) {
if (responding)
pstats_ike_dpd_replied++;
else
pstats_ike_dpd_recv++;
}
return STF_OK;
}
#ifdef XFRM_SUPPORT
static payload_emitter_fn add_mobike_payloads;
static bool add_mobike_payloads(struct state *st, pb_stream *pbs)
{
ip_endpoint local_endpoint = st->st_mobike_local_endpoint;
ip_endpoint remote_endpoint = st->st_remote_endpoint;
return emit_v2N(v2N_UPDATE_SA_ADDRESSES, pbs) &&
ikev2_out_natd(&local_endpoint, &remote_endpoint,
&st->st_ike_spis, pbs);
}
#endif
void ikev2_rekey_ike_start(struct ike_sa *ike)
{
struct pending p = {
.whack_sock = ike->sa.st_logger->object_whackfd,/*on-stack*/
.ike = ike,
.connection = ike->sa.st_connection,
.policy = LEMPTY,
.try = 1,
.replacing = ike->sa.st_serialno
};
ikev2_initiate_child_sa(&p);
}
void ikev2_initiate_child_sa(struct pending *p)
{
struct ike_sa *ike = p->ike;
struct connection *c = p->connection;
passert(c != NULL);
enum sa_type sa_type;
if (p->replacing == ike->sa.st_serialno) { /* IKE rekey exchange */
sa_type = IKE_SA;
ike->sa.st_viable_parent = FALSE;
} else {
if (find_pending_phase2(ike->sa.st_serialno,
c, IPSECSA_PENDING_STATES)) {
return;
}
sa_type = IPSEC_SA;
}
struct child_sa *child; /* to be determined */
const struct child_sa *child_being_replaced;
if (sa_type == IPSEC_SA) {
child_being_replaced = pexpect_child_sa(state_with_serialno(p->replacing));
if (child_being_replaced != NULL &&
!IS_CHILD_SA_ESTABLISHED(&child_being_replaced->sa)) {
/* can't replace a state that isn't established */
child_being_replaced = NULL;
}
child = new_v2_child_state(c, ike, IPSEC_SA,
SA_INITIATOR,
(child_being_replaced != NULL ? STATE_V2_REKEY_CHILD_I0 :
STATE_V2_NEW_CHILD_I0),
p->whack_sock);
if (p->sec_label.len != 0) {
dbg("%s: received security label from acquire via pending: \"%.*s\"", __FUNCTION__,
(int)p->sec_label.len, p->sec_label.ptr);
dbg("%s: connection security label: \"%.*s\"", __FUNCTION__,
(int)c->spd.this.sec_label.len, c->spd.this.sec_label.ptr);
/*
* Should we have a within_range() check here? In theory, the ACQUIRE came
* from a policy we gave the kernel, so it _should_ be within our range?
*/
child->sa.st_acquired_sec_label = clone_hunk(p->sec_label, "st_acquired_sec_label");
c->spd.this.sec_label = clone_hunk(p->sec_label, "updated conn label");
c->spd.that.sec_label = clone_hunk(p->sec_label, "updated conn label");
}
} else {
child_being_replaced = NULL; /* obviously the IKE SA */
child = new_v2_child_state(c, ike, IKE_SA,
SA_INITIATOR,
STATE_V2_REKEY_IKE_I0,
p->whack_sock);
child->sa.st_oakley = ike->sa.st_oakley;
child->sa.st_ike_rekey_spis.initiator = ike_initiator_spi();
child->sa.st_ike_pred = ike->sa.st_serialno;
}
child->sa.st_try = p->try;
free_chunk_content(&child->sa.st_ni); /* this is from the parent. */
free_chunk_content(&child->sa.st_nr); /* this is from the parent. */
if (child_being_replaced != NULL) {
pexpect(sa_type == IPSEC_SA);
pexpect(IS_CHILD_SA_ESTABLISHED(&child_being_replaced->sa));
child->sa.st_ipsec_pred = child_being_replaced->sa.st_serialno;
passert(child->sa.st_connection == child_being_replaced->sa.st_connection);
if (HAS_IPSEC_POLICY(child_being_replaced->sa.st_policy))
child->sa.st_policy = child_being_replaced->sa.st_policy;
else
p->policy = c->policy; /* where did child_being_replaced->sa.st_policy go? */
}
child->sa.st_policy = p->policy;
binlog_refresh_state(&child->sa);
char replacestr[256] = "";
if (p->replacing != SOS_NOBODY) {
snprintf(replacestr, sizeof(replacestr), " to replace #%lu",
p->replacing);
}
passert(child->sa.st_connection != NULL);
if (sa_type == IPSEC_SA) {
/*
* Use the CREATE_CHILD_SA proposal suite - the
* proposal generated during IKE_AUTH will have been
* stripped of DH.
*
* XXX: If the IKE SA's DH changes, then the child
* proposals will be re-generated. Should the child
* proposals instead be somehow stored in state and
* dragged around?
*/
const struct dh_desc *default_dh =
c->policy & POLICY_PFS ? ike->sa.st_oakley.ta_dh : NULL;
struct ikev2_proposals *child_proposals =
get_v2_create_child_proposals(c,
"ESP/AH initiator emitting proposals",
default_dh,
child->sa.st_logger);
/* see ikev2_child_add_ipsec_payloads */
passert(c->v2_create_child_proposals != NULL);
child->sa.st_pfs_group = ikev2_proposals_first_dh(child_proposals, child->sa.st_logger);
policy_buf pb;
dbg("#%lu schedule %s IPsec SA %s%s using IKE# %lu pfs=%s",
child->sa.st_serialno,
child_being_replaced != NULL ? "rekey initiate" : "initiate",
str_policy(p->policy, &pb),
replacestr,
ike->sa.st_serialno,
child->sa.st_pfs_group == NULL ? "no-pfs" : child->sa.st_pfs_group->common.fqn);
} else {
policy_buf pb;
dbg("#%lu schedule initiate IKE Rekey SA %s to replace IKE# %lu",
child->sa.st_serialno,
str_policy(p->policy, &pb),
ike->sa.st_serialno);
}
event_force(EVENT_v2_INITIATE_CHILD, &child->sa);
}
static ke_and_nonce_cb ikev2_child_outI_continue;
void ikev2_child_outI(struct state *st)
{
/*
* XXX: the combination of .st_pfs_group and .st_oakley.ta_dh
* is weird. Should this instead extract the tentative DH
* from the proposals (providing a default)?
*/
switch (st->st_state->kind) {
case STATE_V2_REKEY_CHILD_I0:
submit_ke_and_nonce(st, st->st_pfs_group,
ikev2_child_outI_continue /*possibly-null*/,
"Child Rekey Initiator KE and nonce ni");
break; /* return STF_SUSPEND; */
case STATE_V2_NEW_CHILD_I0:
submit_ke_and_nonce(st, st->st_pfs_group /*possibly-null*/,
ikev2_child_outI_continue,
"Child Initiator KE? and nonce");
break; /* return STF_SUSPEND; */
case STATE_V2_REKEY_IKE_I0:
submit_ke_and_nonce(st, st->st_oakley.ta_dh,
ikev2_child_outI_continue /*never-null?*/,
"IKE REKEY Initiator KE and nonce ni");
break; /* return STF_SUSPEND; */
default:
bad_case(st->st_state->kind);
}
}
static v2_msgid_pending_cb ikev2_child_outI_continue_2;
static stf_status ikev2_child_outI_continue(struct state *st,
struct msg_digest *unused_md,
struct dh_local_secret *local_secret,
chunk_t *nonce)
{
dbg("%s() for #%lu %s",
__func__, st->st_serialno, st->st_state->name);
/* child initiating exchange */
pexpect(unused_md == NULL);
struct ike_sa *ike = ike_sa(st, HERE);
struct child_sa *child = pexpect_child_sa(st);
pexpect(child->sa.st_sa_role == SA_INITIATOR);
/*
* XXX: Should this routine be split so that each instance
* handles only one state transition. If there's commonality
* then the per-transition functions can all call common code.
*/
pexpect(st->st_state->kind == STATE_V2_NEW_CHILD_I0 ||
st->st_state->kind == STATE_V2_REKEY_CHILD_I0 ||
st->st_state->kind == STATE_V2_REKEY_IKE_I0);
/* and a parent? */
if (ike == NULL) {
pexpect_fail(st->st_logger, HERE,
"sponsoring child state #%lu has no parent state #%lu",
st->st_serialno, st->st_clonedfrom);
/* XXX: release child? */
return STF_INTERNAL_ERROR;
}
/* IKE SA => DH */
pexpect(st->st_state->kind == STATE_V2_REKEY_IKE_I0 ? local_secret != NULL : true);
unpack_nonce(&st->st_ni, nonce);
if (local_secret != NULL) {
unpack_KE_from_helper(st, local_secret, &st->st_gi);
}
dbg("queueing child sa with acquired label %.*s",
(int)st->st_acquired_sec_label.len, st->st_acquired_sec_label.ptr);
dbg("adding CHILD SA #%lu to IKE SA #%lu message initiator queue",
child->sa.st_serialno, ike->sa.st_serialno);
v2_msgid_queue_initiator(ike, &child->sa, ISAKMP_v2_CREATE_CHILD_SA,
NULL, ikev2_child_outI_continue_2);
return STF_SUSPEND;
}
stf_status ikev2_child_outI_continue_2(struct ike_sa *ike, struct state *st,
struct msg_digest *md UNUSED)
{
struct child_sa *child = pexpect_child_sa(st);
stf_status e = ikev2_start_new_exchange(ike, child);
if (e != STF_OK) {
return e;
}
return ikev2_child_out_tail(ike, child, NULL);
}
void ikev2_record_newaddr(struct state *st, void *arg_ip)
{
ip_address *ip = arg_ip;
if (!mobike_check_established(st))
return;
if (address_is_specified(&st->st_deleted_local_addr)) {
/*
* A work around for delay between new address and new route
* A better fix would be listen to RTM_NEWROUTE, RTM_DELROUTE
*/
if (st->st_addr_change_event == NULL) {
event_schedule(EVENT_v2_ADDR_CHANGE,
RTM_NEWADDR_ROUTE_DELAY, st);
} else {
ipstr_buf b;
dbg("#%lu MOBIKE ignore address %s change pending previous",
st->st_serialno, sensitive_ipstr(ip, &b));
}
}
}
void ikev2_record_deladdr(struct state *st, void *arg_ip)
{
ip_address *ip = arg_ip;
if (!mobike_check_established(st))
return;
pexpect_st_local_endpoint(st);
ip_address local_address = endpoint_address(&st->st_interface->local_endpoint);
/* ignore port */
if (sameaddr(ip, &local_address)) {
ip_address ip_p = st->st_deleted_local_addr;
st->st_deleted_local_addr = local_address;
struct state *cst = state_with_serialno(st->st_connection->newest_ipsec_sa);
migration_down(cst->st_connection, cst);
unroute_connection(st->st_connection);
event_delete(EVENT_v2_LIVENESS, cst);
if (st->st_addr_change_event == NULL) {
event_schedule(EVENT_v2_ADDR_CHANGE, deltatime(0), st);
} else {
ipstr_buf o, n;
dbg("#%lu MOBIKE new RTM_DELADDR %s pending previous %s",
st->st_serialno, ipstr(ip, &n), ipstr(&ip_p, &o));
}
}
}
#ifdef XFRM_SUPPORT
static void initiate_mobike_probe(struct state *st, struct starter_end *this,
const struct iface_endpoint *iface)
{
struct ike_sa *ike = ike_sa(st, HERE);
/*
* caveat: could a CP initiator find an address received
* from the pool as a new source address?
*/
ipstr_buf s, g;
endpoint_buf b;
dbg("#%lu MOBIKE new source address %s remote %s and gateway %s",
st->st_serialno, ipstr(&this->addr, &s),
str_endpoint(&st->st_remote_endpoint, &b),
ipstr(&this->nexthop, &g));
pexpect_st_local_endpoint(st);
/*
* XXX: why not local_endpoint or is this redundant?
*
* The interface changed (new address in .address) but
* continue to use the existing port.
*/
ip_port port = endpoint_port(&st->st_interface->local_endpoint);
st->st_mobike_local_endpoint = endpoint3(st->st_interface->protocol,
&this->addr, port);
st->st_mobike_host_nexthop = this->nexthop; /* for updown, after xfrm migration */
const struct iface_endpoint *o_iface = st->st_interface;
/* notice how it gets set back below */
st->st_interface = iface;
stf_status e = record_v2_informational_request("mobike informational request",
ike, st/*sender*/,
add_mobike_payloads);
if (e == STF_OK) {
send_recorded_v2_message(ike, "mobike informational request",
MESSAGE_REQUEST);
/*
* XXX: record 'n' send violates the RFC. This code should
* instead let success_v2_state_transition() deal with things.
*/
dbg_v2_msgid(ike, st, "XXX: in %s() hacking around record'n'send bypassing send queue",
__func__);
v2_msgid_update_sent(ike, &ike->sa, NULL /* new exchange */, MESSAGE_REQUEST);
}
st->st_interface = o_iface;
pexpect_st_local_endpoint(st);
}
#endif
#ifdef XFRM_SUPPORT
static const struct iface_endpoint *ikev2_src_iface(struct state *st,
struct starter_end *this)
{
/* success found a new source address */
pexpect_st_local_endpoint(st);
ip_port port = endpoint_port(&st->st_interface->local_endpoint);
ip_endpoint local_endpoint = endpoint3(st->st_interface->protocol,
&this->addr, port);
const struct iface_endpoint *iface = find_iface_endpoint_by_local_endpoint(&local_endpoint);
if (iface == NULL) {
endpoint_buf b;
dbg("#%lu no interface for %s try to initialize",
st->st_serialno, str_endpoint(&local_endpoint, &b));
/* XXX: should this be building a global logger? */
struct logger global_logger[1] = { GLOBAL_LOGGER(whack_log_fd), };
find_ifaces(false, global_logger);
iface = find_iface_endpoint_by_local_endpoint(&local_endpoint);
if (iface == NULL) {
return NULL;
}
}
return iface;
}
#endif
void ikev2_addr_change(struct state *st)
{
if (!mobike_check_established(st))
return;
#ifdef XFRM_SUPPORT
/* let's re-discover local address */
struct starter_end this = {
.addrtype = KH_DEFAULTROUTE,
.nexttype = KH_DEFAULTROUTE,
.host_family = endpoint_type(&st->st_remote_endpoint),
};
struct starter_end that = {
.addrtype = KH_IPADDR,
.host_family = endpoint_type(&st->st_remote_endpoint),
.addr = endpoint_address(&st->st_remote_endpoint),
};
/*
* mobike need two lookups. one for the gateway and
* the one for the source address
*/
switch (resolve_defaultroute_one(&this, &that, true, st->st_logger)) {
case 0: /* success */
/* cannot happen */
/* ??? original code treated this as failure */
/* bad_case(0); */
log_state(RC_LOG, st, "unexpected SUCCESS from first resolve_defaultroute_one");
/* FALL THROUGH */
case -1: /* failure */
{
/* keep this DEBUG, if a libreswan log, too many false +ve */
address_buf b;
dbg("#%lu no local gateway to reach %s",
st->st_serialno, str_address(&that.addr, &b));
break;
}
case 1: /* please call again: more to do */
switch (resolve_defaultroute_one(&this, &that, true, st->st_logger)) {
case 1: /* please call again: more to do */
/* cannot happen */
/* ??? original code treated this as failure */
/* bad_case(1); */
log_state(RC_LOG, st, "unexpected TRY AGAIN from second resolve_defaultroute_one");
/* FALL THROUGH */
case -1: /* failure */
{
ipstr_buf g, b;
log_state(RC_LOG, st, "no local source address to reach remote %s, local gateway %s",
sensitive_ipstr(&that.addr, &b),
ipstr(&this.nexthop, &g));
break;
}
case 0: /* success */
{
const struct iface_endpoint *iface = ikev2_src_iface(st, &this);
if (iface != NULL)
initiate_mobike_probe(st, &this, iface);
break;
}
}
break;
}
#else /* !defined(XFRM_SUPPORT) */
log_state(RC_LOG, st, "without NETKEY we cannot ikev2_addr_change()");
#endif
}
/*
* For opportunistic IPsec, we want to delete idle connections, so we
* are not gaining an infinite amount of unused IPsec SAs.
*
* NOTE: Soon we will accept an idletime= configuration option that
* replaces this check.
*
* Only replace the SA when it's been in use (checking for in-use is a
* separate operation).
*/
static bool expire_ike_because_child_not_used(struct state *st)
{
if (!(IS_PARENT_SA_ESTABLISHED(st) ||
IS_CHILD_SA_ESTABLISHED(st))) {
/* for instance, too many retransmits trigger replace */
return false;
}
struct connection *c = st->st_connection;
if (!(c->policy & POLICY_OPPORTUNISTIC)) {
/* killing idle IPsec SA's is only for opportunistic SA's */
return false;
}
if (c->spd.that.has_lease) {
pexpect_fail(st->st_logger, HERE,
"#%lu has lease; should not be trying to replace",
st->st_serialno);
return true;
}
/* see of (most recent) child is busy */
struct state *cst;
struct ike_sa *ike;
if (IS_IKE_SA(st)) {
ike = pexpect_ike_sa(st);
cst = state_with_serialno(c->newest_ipsec_sa);
if (cst == NULL) {
pexpect_fail(st->st_logger, HERE,
"can't check usage as IKE SA #%lu has no newest child",
ike->sa.st_serialno);
return true;
}
} else {
cst = st;
ike = ike_sa(st, HERE);
}
dbg("#%lu check last used on newest CHILD SA #%lu",
ike->sa.st_serialno, cst->st_serialno);
/* not sure why idleness is set to rekey margin? */
if (was_eroute_idle(cst, c->sa_rekey_margin)) {
/* we observed no traffic, let IPSEC SA and IKE SA expire */
dbg("expiring IKE SA #%lu as CHILD SA #%lu has been idle for more than %jds",
ike->sa.st_serialno,
ike->sa.st_serialno,
deltasecs(c->sa_rekey_margin));
return true;
}
return false;
}
void v2_schedule_replace_event(struct state *st)
{
struct connection *c = st->st_connection;
/* unwrapped deltatime_t in seconds */
intmax_t delay = deltasecs(IS_IKE_SA(st) ? c->sa_ike_life_seconds
: c->sa_ipsec_life_seconds);
st->st_replace_by = monotime_add(mononow(), deltatime(delay));
/*
* Important policy lies buried here. For example, we favour
* the initiator over the responder by making the initiator
* start rekeying sooner. Also, fuzz is only added to the
* initiator's margin.
*/
enum event_type kind;
const char *story;
intmax_t marg;
if ((c->policy & POLICY_OPPORTUNISTIC) &&
st->st_connection->spd.that.has_lease) {
marg = 0;
kind = EVENT_SA_EXPIRE;
story = "always expire opportunistic SA with lease";
} else if (c->policy & POLICY_DONT_REKEY) {
marg = 0;
kind = EVENT_SA_EXPIRE;
story = "policy doesn't allow re-key";
} else if (IS_IKE_SA(st) && LIN(POLICY_REAUTH, st->st_connection->policy)) {
marg = 0;
kind = EVENT_SA_REPLACE;
story = "IKE SA with policy re-authenticate";
} else {
/* unwrapped deltatime_t in seconds */
marg = deltasecs(c->sa_rekey_margin);
switch (st->st_sa_role) {
case SA_INITIATOR:
marg += marg *
c->sa_rekey_fuzz / 100.E0 *
(rand() / (RAND_MAX + 1.E0));
break;
case SA_RESPONDER:
marg /= 2;
break;
default:
bad_case(st->st_sa_role);
}
if (delay > marg) {
delay -= marg;
kind = EVENT_SA_REKEY;
story = "attempting re-key";
} else {
marg = 0;
kind = EVENT_SA_REPLACE;
story = "margin to small for re-key";
}
}
st->st_replace_margin = deltatime(marg);
if (marg > 0) {
passert(kind == EVENT_SA_REKEY);
dbg("#%lu will start re-keying in %jd seconds with margin of %jd seconds (%s)",
st->st_serialno, delay, marg, story);
} else {
passert(kind == EVENT_SA_REPLACE || kind == EVENT_SA_EXPIRE);
dbg("#%lu will %s in %jd seconds (%s)",
st->st_serialno,
kind == EVENT_SA_EXPIRE ? "expire" : "be replaced",
delay, story);
}
delete_event(st);
event_schedule(kind, deltatime(delay), st);
}
void v2_event_sa_rekey(struct state *st)
{
monotime_t now = mononow();
const char *satype = IS_IKE_SA(st) ? "IKE" : "CHILD";
so_serial_t newer_sa = get_newer_sa_from_connection(st);
if (newer_sa != SOS_NOBODY) {
/* implies a double re-key? */
pexpect_fail(st->st_logger, HERE,
"not replacing stale %s SA #%lu; as already got a newer #%lu",
satype, st->st_serialno, newer_sa);
event_force(EVENT_SA_EXPIRE, st);
return;
}
if (expire_ike_because_child_not_used(st)) {
struct ike_sa *ike = ike_sa(st, HERE);
event_force(EVENT_SA_EXPIRE, &ike->sa);
return;
}
if (monobefore(st->st_replace_by, now)) {
dbg("#%lu has no time to re-key, will replace",
st->st_serialno);
event_force(EVENT_SA_REPLACE, st);
}
dbg("rekeying stale %s SA", satype);
if (IS_IKE_SA(st)) {
log_state(RC_LOG, st, "initiate rekey of IKEv2 CREATE_CHILD_SA IKE Rekey");
ikev2_rekey_ike_start(pexpect_ike_sa(st));
} else {
/*
* XXX: Don't be fooled, ipsecdoi_replace() is magic -
* if the old state still exists it morphs things into
* a child re-key.
*/
ipsecdoi_replace(st, 1);
}
/*
* Should the rekey go into the weeds this replace will kick
* in.
*
* XXX: should the next event be SA_EXPIRE instead of
* SA_REPLACE? For an IKE SA it breaks ikev2-32-nat-rw-rekey.
* For a CHILD SA perhaps - there is a mystery around what
* happens to the new child if the old one disappears.
*/
dbg("scheduling drop-dead replace event for #%lu", st->st_serialno);
event_delete(EVENT_v2_LIVENESS, st);
event_schedule(EVENT_SA_REPLACE, monotimediff(st->st_replace_by, now), st);
}
void v2_event_sa_replace(struct state *st)
{
const char *satype = IS_IKE_SA(st) ? "IKE" : "CHILD";
so_serial_t newer_sa = get_newer_sa_from_connection(st);
if (newer_sa != SOS_NOBODY) {
/*
* For some reason the rekey, above, hasn't completed.
* For an IKE SA blow away the entire family
* (including the in-progress rekey). For a CHILD SA
* this will delete the old SA but leave the rekey
* alone. Confusing.
*/
if (IS_IKE_SA(st)) {
dbg("replacing entire stale IKE SA #%lu family; rekey #%lu will be deleted",
st->st_serialno, newer_sa);
ipsecdoi_replace(st, 1);
} else {
dbg("expiring stale CHILD SA #%lu; newer #%lu will replace?",
st->st_serialno, newer_sa);
}
/* XXX: are these calls needed? it's about to die */
event_delete(EVENT_v2_LIVENESS, st);
event_force(EVENT_SA_EXPIRE, st);
return;
}
if (expire_ike_because_child_not_used(st)) {
struct ike_sa *ike = ike_sa(st, HERE);
event_force(EVENT_SA_EXPIRE, &ike->sa);
return;
}
/*
* XXX: For a CHILD SA, will this result in a re-key attempt?
*/
dbg("replacing stale %s SA", satype);
ipsecdoi_replace(st, 1);
event_delete(EVENT_v2_LIVENESS, st);
event_force(EVENT_SA_EXPIRE, st);
}
|