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
|
/*
* The IKE Scanner (ike-scan) is Copyright (C) 2003-2007 Roy Hills,
* NTA Monitor Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library, and distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version.
*
* If this license is unacceptable to you, I may be willing to negotiate
* alternative licenses (contact ike-scan@nta-monitor.com).
*
* You are encouraged to send comments, improvements or suggestions to
* me at ike-scan@nta-monitor.com.
*
* $Id$
*
* Author: Roy Hills
* Date: 7 November 2003
*
* Functions to construct ISAKMP headers and payloads.
*
*/
#include "ike-scan.h"
static char rcsid[] = "$Id$"; /* RCS ID for ident(1) */
const id_name_map notification_map[] = { /* From RFC 2408 3.14.1 */
{0, "UNSPECIFIED"},
{1, "INVALID-PAYLOAD-TYPE"},
{2, "DOI-NOT-SUPPORTED"},
{3, "SITUATION-NOT-SUPPORTED"},
{4, "INVALID-COOKIE"},
{5, "INVALID-MAJOR-VERSION"},
{6, "INVALID-MINOR-VERSION"},
{7, "INVALID-EXCHANGE-TYPE"},
{8, "INVALID-FLAGS"},
{9, "INVALID-MESSAGE-ID"},
{10, "INVALID-PROTOCOL-ID"},
{11, "INVALID-SPI"},
{12, "INVALID-TRANSFORM-ID"},
{13, "ATTRIBUTES-NOT-SUPPORTED"},
{14, "NO-PROPOSAL-CHOSEN"},
{15, "BAD-PROPOSAL-SYNTAX"},
{16, "PAYLOAD-MALFORMED"},
{17, "INVALID-KEY-INFORMATION"},
{18, "INVALID-ID-INFORMATION"},
{19, "INVALID-CERT-ENCODING"},
{20, "INVALID-CERTIFICATE"},
{21, "CERT-TYPE-UNSUPPORTED"},
{22, "INVALID-CERT-AUTHORITY"},
{23, "INVALID-HASH-INFORMATION"},
{24, "AUTHENTICATION-FAILED"},
{25, "INVALID-SIGNATURE"},
{26, "ADDRESS-NOTIFICATION"},
{27, "NOTIFY-SA-LIFETIME"},
{28, "CERTIFICATE-UNAVAILABLE"},
{29, "UNSUPPORTED-EXCHANGE-TYPE"},
{30, "UNEQUAL-PAYLOAD-LENGTHS"},
{9101, "Checkpoint-Firewall-1"},
{9110, "Checkpoint-Firewall-1"},
{24576, "RESPONDER-LIFETIME"}, /* Next 3 are from RFC 2407 4.6.3 */
{24577, "REPLAY-STATUS"},
{24578, "INITIAL-CONTACT"},
{-1, NULL}
};
const id_name_map notification_map2[] = { /* From RFC 4306 3.10.1 */
{0, "RESERVED"},
{1, "UNSUPPORTED_CRITICAL_PAYLOAD"},
{4, "INVALID_IKE_SPI"},
{5, "INVALID_MAJOR_VERSION"},
{7, "INVALID_SYNTAX"},
{9, "INVALID_MESSAGE_ID"},
{11, "INVALID_SPI"},
{14, "NO_PROPOSAL_CHOSEN"},
{17, "INVALID_KE_PAYLOAD"},
{24, "AUTHENTICATION_FAILED"},
{34, "SINGLE_PAIR_REQUIRED"},
{35, "NO_ADDITIONAL_SAS"},
{36, "INTERNAL_ADDRESS_FAILURE"},
{37, "FAILED_CP_REQUIRED"},
{38, "TS_UNACCEPTABLE"},
{39, "INVALID_SELECTORS"},
{9101, "Checkpoint-Firewall-1"},
{9110, "Checkpoint-Firewall-1"},
{16384, "INITIAL_CONTACT"},
{16385, "SET_WINDOW_SIZE"},
{16386, "ADDITIONAL_TS_POSSIBLE"},
{16387, "IPCOMP_SUPPORTED"},
{16388, "NAT_DETECTION_SOURCE_IP"},
{16389, "NAT_DETECTION_DESTINATION_IP"},
{16390, "COOKIE"},
{16391, "USE_TRANSPORT_MODE"},
{16392, "HTTP_CERT_LOOKUP_SUPPORTED"},
{16393, "REKEY_SA"},
{16394, "ESP_TFC_PADDING_NOT_SUPPORTED"},
{16395, "NON_FIRST_FRAGMENTS_ALSO"},
{-1, NULL}
};
const id_name_map attr_map[] = { /* From RFC 2409 App. A and */
{1, "Enc"}, /* draft-ietf-ipsec-isakmp-gss-auth */
{2, "Hash"},
{3, "Auth"},
{4, "Group"},
{5, "GroupType"},
{6, "GroupPrime/IrreduciblePolynomial"},
{7, "GroupGeneratorOne"},
{8, "GroupGeneratorTwo"},
{9, "GroupCurve A"},
{10, "GroupCurve B"},
{11, "LifeType"},
{12, "LifeDuration"},
{13, "PRF"},
{14, "KeyLength"},
{15, "FieldSize"},
{16, "GroupOrder"},
{16384, "GSSIdentityName"},
{-1, NULL}
};
const id_name_map trans_type_map[] = { /* From RFC 4306 3.3.2 */
{1, "Encr"},
{2, "Prf"},
{3, "Integ"},
{4, "DH_Group"},
{5, "ESN"},
{-1, NULL}
};
const id_name_map enc_map[] = { /* From RFC 2409 App. A */
{1, "DES"},
{2, "IDEA"},
{3, "Blowfish"},
{4, "RC5"},
{5, "3DES"},
{6, "CAST"},
{7, "AES"}, /* RFC 3602 */
{8, "Camellia"}, /* draft-kato-ipsec-ciph-camellia-01.txt */
{65001, "Mars"}, /* Defined in strongSwan constants.h */
{65002, "RC6"}, /* Defined in strongSwan constants.h */
{65003, "ID_65003"}, /* Defined in strongSwan constants.h */
{65004, "Serpent"}, /* Defined in strongSwan constants.h */
{65005, "Twofish"}, /* Defined in strongSwan constants.h */
{-1, NULL}
};
const id_name_map encr_map[] = { /* From RFC 4306 3.3.2 */
{1, "DES_IV64"},
{2, "DES"},
{3, "3DES"},
{4, "RC5"},
{5, "IDEA"},
{6, "CAST"},
{7, "Blowfish"},
{8, "3IDEA"},
{9, "DES_IV32"},
{11, "NULL"},
{12, "AES_CBC"},
{13, "AES_CTR"},
{-1, NULL}
};
const id_name_map hash_map[] = { /* From RFC 2409 App. A */
{1, "MD5"},
{2, "SHA1"},
{3, "Tiger"},
{4, "SHA2-256"},
{5, "SHA2-384"},
{6, "SHA2-512"},
{-1, NULL}
};
const id_name_map prf_map[] = { /* From RFC 4306 3.3.2 */
{1, "HMAC_MD5"},
{2, "HMAC_SHA1"},
{3, "HMAC_TIGER"},
{4, "AES128_XCBC"},
{-1, NULL}
};
const id_name_map auth_map[] = { /* From RFC 2409 App. A */
{1, "PSK"},
{2, "DSS"},
{3, "RSA_Sig"},
{4, "RSA_Enc"},
{5, "RSA_RevEnc"},
{6, "ElGamel_Enc"},
{7, "ElGamel_RevEnc"},
{8, "ECDSA_Sig"},
{128, "CRACK"}, /* From draft-harkins-ipsra-crack-00 */
{64221, "Hybrid"},
{65001, "XAUTH_PSK"},
{65003, "XAUTH_DSS"},
{65005, "XAUTH_RSA"},
{65007, "XAUTH_RSA_Enc"},
{65009, "XAUTH_RSA_RevEnc"},
{-1, NULL}
};
const id_name_map integ_map[] = { /* From RFC 4306 3.3.2 */
{1, "HMAC_MD5_96"},
{2, "HMAC_SHA1_96"},
{3, "DES_MAC"},
{4, "KPDK_MD5"},
{5, "AES_XCBC_96"},
{-1, NULL}
};
const id_name_map dh_map[] = { /* From RFC 2409 App. A */
{1, "1:modp768"}, /* and RFC 3526 */
{2, "2:modp1024"},
{3, "3:ec2n155"},
{4, "4:ec2n185"},
{5, "5:modp1536"},
{6, "6:ec2n163"},
{7, "7:ec2n163"},
{8, "8:ec2n283"},
{9, "9:ec2n283"},
{10, "10:ec2n409"},
{11, "11:ec2n409"},
{12, "12:ec2n571"},
{13, "13:ec2n571"},
{14, "14:modp2048"},
{15, "15:modp3072"},
{16, "16:modp4096"},
{17, "17:modp6144"},
{18, "18:modp8192"},
{-1, NULL}
};
const id_name_map life_map[] = { /* From RFC 2409 App. A */
{1, "Seconds"},
{2, "Kilobytes"},
{-1, NULL}
};
const id_name_map payload_map[] = { /* Payload types from RFC 2408 3.1 */
{1, "SecurityAssociation"}, /* and RFC 4306 3.2 */
{2, "Proposal"},
{3, "Transform"},
{4, "KeyExchange"},
{5, "Identification"},
{6, "Certificate"},
{7, "CertificateRequest"},
{8, "Hash"},
{9, "Signature"},
{10, "Nonce"},
{11, "Notification"},
{12, "Delete"},
{13, "VendorID"},
{20, "NAT-D"}, /* RFC 3947 NAT Discovery */
{33, "SecurityAssociation"},
{34, "KeyExchange"},
{35, "IDI"},
{36, "IDR"},
{37, "Certificate"},
{38, "CertificateRequest"},
{39, "AUTH"},
{40, "Nonce"},
{41, "Notification"},
{42, "Delete"},
{43, "VendorID"},
{44, "TSI"},
{45, "TSR"},
{46, "Encrypted"},
{47, "Configuration"},
{48, "EAP"},
{-1, NULL}
};
const id_name_map doi_map[] = {
{0, "ISAKMP"},
{1, "IPsec"},
{-1, NULL}
};
const id_name_map protocol_map[] = {
{1, "PROTO_ISAKMP"},
{2, "PROTO_IPSEC_AH"},
{3, "PROTO_IPSEC_ESP"},
{4, "PROTO_IPSEC_COMP"},
{-1, NULL}
};
const id_name_map id_map[] = { /* From RFC 2407 4.6.2.1 */
{1, "ID_IPV4_ADDR"},
{2, "ID_FQDN"},
{3, "ID_USER_FQDN"},
{4, "ID_IPV4_ADDR_SUBNET"},
{5, "ID_IPV6_ADDR"},
{6, "ID_IPV6_ADDR_SUBNET"},
{7, "ID_IPV4_ADDR_RANGE"},
{8, "ID_IPV6_ADDR_RANGE"},
{9, "ID_DER_ASN1_DN"},
{10, "ID_DER_ASN1_GN"},
{11, "ID_KEY_ID"},
};
const id_name_map cert_map[] = { /* From RFC 2408 Sec. 3.9 */
{1, "PKCS #7 wrapped X.509 certificate"},
{2, "PGP Certificate"},
{3, "DNS Signed Key"},
{4, "X.509 Certificate - Signature"},
{5, "X.509 Certificate - Key Exchange"},
{6, "Kerberos Tokens"},
{7, "Certificate Revocation List (CRL)"},
{8, "Authority Revocation List (ARL)"},
{9, "SPKI Certificate"},
{10, "X.509 Certificate - Attribute"},
{-1, NULL}
};
extern psk_crack psk_values;
extern int mbz_value;
/*
* make_isakmp_hdr -- Construct an ISAKMP Header
*
* Inputs:
*
* xchg Exchange Type (e.g. ISAKMP_XCHG_IDPROT for main mode)
* next Next Payload Type
* length ISAKMP Message total length
* header_version Version number to put in the header
* hdr_flags Flags to put in the header
* hdr_msgid Message ID to put in the header
* rcookie_data Responder cookie data, or NULL for default
* rcookie_data_len Length of responder cookie data (<=8)
*
* Returns:
*
* Pointer to created ISAKMP Header.
*
* This constructs an ISAKMP header. It fills in the static values.
* The initiator cookie should be changed to a unique per-host value
* before the packet is sent.
*/
unsigned char*
make_isakmp_hdr(unsigned xchg, unsigned next, unsigned length,
int header_version, int hdr_flags, unsigned hdr_msgid,
unsigned char *rcookie_data, size_t rcookie_data_len) {
unsigned char *payload;
struct isakmp_hdr* hdr;
payload = Malloc(sizeof(struct isakmp_hdr));
hdr = (struct isakmp_hdr*) payload; /* Overlay header struct on payload */
memset(hdr, mbz_value, sizeof(struct isakmp_hdr));
hdr->isa_icookie[0] = 0xdeadbeef; /* Initiator cookie */
hdr->isa_icookie[1] = 0xdeadbeef;
hdr->isa_rcookie[0] = 0; /* Set responder cookie to 0 */
hdr->isa_rcookie[1] = 0;
if (rcookie_data) {
memcpy(hdr->isa_rcookie, rcookie_data, rcookie_data_len);
}
hdr->isa_np = next; /* Next Payload Type */
hdr->isa_version = header_version; /* v1.0 by default */
hdr->isa_xchg = xchg; /* Exchange type */
hdr->isa_flags = hdr_flags; /* Flags */
hdr->isa_msgid = htonl(hdr_msgid); /* Message ID */
hdr->isa_length = htonl(length); /* Total ISAKMP message length */
return payload;
}
/*
* make_sa -- Construct an SA payload
*
* Inputs:
*
* outlen (output) length of SA payload
* next Next Payload Type
* doi Domain of interpretation
* situation Situation
* proposals Pointer to list of proposals
* proposal_len length of proposal list
*
* Returns:
*
* Pointer to the SA payload.
*
* This constructs an SA payload.
*/
unsigned char*
make_sa(size_t *outlen, unsigned next, unsigned doi, unsigned situation,
unsigned char *proposals, size_t proposal_len) {
unsigned char *payload;
struct isakmp_sa* hdr;
unsigned char *cp;
size_t len;
hdr = Malloc(sizeof(struct isakmp_sa));
memset(hdr, mbz_value, sizeof(struct isakmp_sa));
hdr->isasa_np = next; /* Next Payload Type */
hdr->isasa_doi = htonl(doi); /* Default is IPsec DOI */
hdr->isasa_situation = htonl(situation); /* Default SIT_IDENTITY_ONLY */
len = sizeof(struct isakmp_sa) + proposal_len;
hdr->isasa_length = htons(len); /* SA Payload length */
payload = Malloc(len);
cp = payload;
memcpy(cp, hdr, sizeof(struct isakmp_sa));
cp += sizeof(struct isakmp_sa);
memcpy(cp, proposals, proposal_len);
*outlen = len;
return payload;
}
/*
* make_sa2 -- Construct an IKEv2 SA payload
*
* Inputs:
*
* outlen (output) length of SA payload
* next Next Payload Type
* proposals Pointer to list of proposals
* proposal_len length of proposal list
*
* Returns:
*
* Pointer to the SA payload.
*
* This constructs an IKEv2 SA payload.
*/
unsigned char*
make_sa2(size_t *outlen, unsigned next,
unsigned char *proposals, size_t proposal_len) {
unsigned char *payload;
struct isakmp_sa2* hdr;
unsigned char *cp;
size_t len;
hdr = Malloc(sizeof(struct isakmp_sa2));
memset(hdr, mbz_value, sizeof(struct isakmp_sa2));
hdr->isasa2_np = next; /* Next Payload Type */
len = sizeof(struct isakmp_sa2) + proposal_len;
hdr->isasa2_length = htons(len); /* SA Payload length */
payload = Malloc(len);
cp = payload;
memcpy(cp, hdr, sizeof(struct isakmp_sa2));
cp += sizeof(struct isakmp_sa2);
memcpy(cp, proposals, proposal_len);
*outlen = len;
return payload;
}
/*
* add_prop -- Add a proposal payload to the list of proposals
*
* Inputs:
*
* outlen (output) Proposal payload length
* notrans Number of transforms in this proposal
* protocol Protocol
* spi_size SPI Size
* transforms Pointer to transform list
* transform_len Length of transform list
*
* Returns:
*
* Pointer to proposal payload.
*
* This function can either be called with finished = 0, in which case
* notrans, protocol, spi_size, transforms and transform_len must be
* specified, and the function will return NULL, OR it can be called with
* finished = 1 in which case notrans, protocol, spi_size, transforms and
* transform_len are ignored and the function will return a pointer to the
* finished payload and will set *length to the length of this payload.
*
* ISAKMP SAs are only allowed to contain one proposal, RFC 2409 section 5
* states:
*
* "To put it another way, for phase 1 exchanges there MUST NOT be
* multiple Proposal Payloads for a single SA payload and there MUST NOT
* be multiple SA payloads."
*
* However, this function does not enforce this restriction.
*/
unsigned char*
add_prop(int finished, size_t *outlen,
unsigned notrans, unsigned protocol, unsigned spi_size,
unsigned char *transforms, size_t transform_len) {
static int first_proposal = 1;
static unsigned char *prop_start=NULL; /* Start of set of proposals */
static size_t cur_offset; /* Start of current proposal */
static size_t end_offset; /* End of proposals */
static unsigned prop_no=1;
unsigned char *prop; /* Proposal payload */
size_t len; /* Proposal length */
/*
* Construct a proposal if we are not finalising.
* Set next to ISAKMP_NEXT_P (more proposals), and increment prop_no for next
* time round.
*/
if (!finished) {
prop = make_prop(&len, ISAKMP_NEXT_P, prop_no, notrans, protocol,
spi_size, transforms, transform_len);
prop_no++;
if (first_proposal) {
cur_offset = 0;
end_offset = len;
prop_start = Malloc(end_offset);
memcpy(prop_start, prop, len);
first_proposal = 0;
} else {
cur_offset = end_offset;
end_offset += len;
prop_start = Realloc(prop_start, end_offset);
memcpy(prop_start+cur_offset, prop, len);
}
free(prop);
return NULL;
} else {
struct isakmp_proposal* hdr =
(struct isakmp_proposal*) (prop_start+cur_offset); /* Overlay */
first_proposal = 1;
hdr->isap_np = ISAKMP_NEXT_NONE; /* No more proposals */
*outlen = end_offset;
return prop_start;
}
}
/*
* make_prop -- Construct a proposal payload
*
* Inputs:
*
* outlen (output) Proposal payload length
* next next payload (2=more props, 0=no more props)
* number proposal number
* notrans Number of transforms in this proposal
* protocol Protocol
* spi_size SPI Size
* transforms Pointer to transform list
* transform_len Length of transform list
*
* Returns:
*
* Pointer to proposal payload.
*
* This constructs a single proposal payload.
*/
unsigned char*
make_prop(size_t *outlen, unsigned next, unsigned number, unsigned notrans,
unsigned protocol, unsigned spi_size, unsigned char *transforms,
size_t transform_len) {
unsigned char *payload;
struct isakmp_proposal* hdr;
unsigned char *cp;
size_t len;
/* Allocate and initialise the proposal header */
hdr = Malloc(sizeof(struct isakmp_proposal));
memset(hdr, mbz_value, sizeof(struct isakmp_proposal));
hdr->isap_np = next;
hdr->isap_proposal = number;
hdr->isap_protoid = protocol;
hdr->isap_spisize = spi_size; /* SPI Size */
hdr->isap_notrans = notrans; /* Number of transforms */
/* Determine total SA length and allocate payload memory */
len = sizeof(struct isakmp_proposal) + spi_size + transform_len;
hdr->isap_length = htons(len); /* Proposal payload length */
payload = Malloc(len);
cp = payload;
/* Copy the proposal header to the payload */
memcpy(cp, hdr, sizeof(struct isakmp_proposal));
cp += sizeof(struct isakmp_proposal);
free(hdr);
/* If the SPI size is non-zero, add a random SPI of the specified length */
if (spi_size > 0) {
unsigned i;
for (i=0; i<spi_size; i++)
*(cp++) = (unsigned char) random_byte();
}
/* Add the transforms */
memcpy(cp, transforms, transform_len);
*outlen = len;
return payload;
}
/*
* make_trans_simple -- Construct a single simple transform payload
*
* Inputs:
*
* length (output) length of entire transform payload.
* next Next Payload Type (3 = More transforms; 0=No more transforms)
* number Transform number
* cipher The encryption algorithm
* keylen Key length for variable length keys (0=fixed key length)
* hash Hash algorithm
* auth Authentication method
* group DH Group number
* lifetime Lifetime in seconds (0=no lifetime)
* lifesize Life in kilobytes (0=no life)
*
* Returns:
*
* Pointer to transform payload.
*
* This constructs a single simple transform payload.
* Most of the values are defined in RFC 2409 Appendix A.
*
* This function can only create a transform with a restricted set of
* attributes in a defined order. To create a transform with an arbitrary
* set of attributes in any order, use the make_transform function
* instead.
*/
unsigned char*
make_trans_simple(size_t *length, unsigned next, unsigned number,
unsigned cipher, unsigned keylen, unsigned hash, unsigned auth,
unsigned group, unsigned char *lifetime_data,
size_t lifetime_data_len, unsigned char *lifesize_data,
size_t lifesize_data_len, int gss_id_flag, unsigned char *gss_data,
size_t gss_data_len, unsigned trans_id) {
unsigned char *payload;
unsigned char *attr;
size_t attr_len; /* Attribute Length */
/* Allocate and initialise the mandatory attributes */
add_attr(0, NULL, 'B', OAKLEY_ENCRYPTION_ALGORITHM, 0, cipher, NULL);
add_attr(0, NULL, 'B', OAKLEY_HASH_ALGORITHM, 0, hash, NULL);
add_attr(0, NULL, 'B', OAKLEY_AUTHENTICATION_METHOD, 0, auth, NULL);
add_attr(0, NULL, 'B', OAKLEY_GROUP_DESCRIPTION, 0, group, NULL);
/* Allocate and initialise the optional attributes */
if (keylen)
add_attr(0, NULL, 'B', OAKLEY_KEY_LENGTH, 0, keylen, NULL);
if (lifetime_data_len) {
add_attr(0, NULL, 'B', OAKLEY_LIFE_TYPE, 0, SA_LIFE_TYPE_SECONDS, NULL);
add_attr(0, NULL, 'V', OAKLEY_LIFE_DURATION, lifetime_data_len, 0,
lifetime_data);
}
if (lifesize_data_len) {
add_attr(0, NULL, 'B', OAKLEY_LIFE_TYPE, 0, SA_LIFE_TYPE_KBYTES, NULL);
add_attr(0, NULL, 'V', OAKLEY_LIFE_DURATION, lifesize_data_len, 0,
lifesize_data);
}
if (gss_id_flag)
add_attr(0, NULL, 'V', OAKLEY_GSS_ID, gss_data_len, 0, gss_data);
/* Finalise attributes and fill in length value */
attr = add_attr(1, &attr_len, '\0', 0, 0, 0, NULL);
/* Create transform */
payload = make_transform(length, next, number, trans_id, attr, attr_len);
free(attr);
return payload;
}
/*
* add_trans_simple -- Add a simple transform payload to set of transforms.
*
* Inputs:
*
* finished 0 if adding a new transform; 1 if finalising.
* length (output) length of entire transform payload.
* cipher The encryption algorithm
* keylen Key length for variable length keys (0=fixed key length)
* hash Hash algorithm
* auth Authentication method
* group DH Group number
* lifetime Lifetime in seconds (0=no lifetime)
*
* Returns:
*
* Pointer to new set of transform payloads.
*
* This function can either be called with finished = 0, in which case
* cipher, keylen, hash, auth, group and lifetime must be specified, and
* the function will return NULL, OR it can be called with finished = 1
* in which case cipher, keylen, hash, auth, group and lifetime are
* ignored and the function will return a pointer to the finished
* payload and will set *length to the length of this payload.
*
* This function can only create transforms with a restricted set of
* attributes in a defined order. To create transforms with an arbitrary
* set of attributes in any order, use the add_transform function
* instead.
*/
unsigned char*
add_trans_simple(int finished, size_t *length, unsigned cipher,
unsigned keylen, unsigned hash, unsigned auth,
unsigned group, unsigned char *lifetime_data,
size_t lifetime_data_len, unsigned char *lifesize_data,
size_t lifesize_data_len, int gss_id_flag,
unsigned char *gss_data, size_t gss_data_len,
unsigned trans_id) {
static int first_transform = 1;
static unsigned char *trans_start=NULL; /* Start of set of transforms */
static size_t cur_offset; /* Start of current transform */
static size_t end_offset; /* End of transforms */
static unsigned trans_no=1;
unsigned char *trans; /* Transform payload */
size_t len; /* Transform length */
/*
* Construct a transform if we are not finalising.
* Set next to ISAKMP_NEXT_T (more transforms), and increment trans_no for
* next time round.
*/
if (!finished) {
trans = make_trans_simple(&len, ISAKMP_NEXT_T, trans_no, cipher, keylen,
hash, auth, group, lifetime_data,
lifetime_data_len, lifesize_data,
lifesize_data_len, gss_id_flag, gss_data,
gss_data_len, trans_id);
trans_no++;
if (first_transform) {
cur_offset = 0;
end_offset = len;
trans_start = Malloc(end_offset);
memcpy(trans_start, trans, len);
first_transform = 0;
} else {
cur_offset = end_offset;
end_offset += len;
trans_start = Realloc(trans_start, end_offset);
memcpy(trans_start+cur_offset, trans, len);
}
free(trans);
return NULL;
} else {
struct isakmp_transform* hdr =
(struct isakmp_transform*) (trans_start+cur_offset); /* Overlay */
first_transform = 1;
hdr->isat_np = 0; /* No more transforms */
*length = end_offset;
return trans_start;
}
}
/*
* make_transform -- Construct a single transform payload
*
* Inputs:
*
* length (output) length of entire transform payload.
* next Next Payload Type (3 = More transforms; 0=No more transforms)
* number Transform number
* trans_id Transform ID (generally KEY_IKE)
* attr Pointer to list of attributes
* attr_len Attribute length in bytes
*
* Returns:
*
* Pointer to transform payload.
*
* This constructs a single transform payload.
* Most of the values are defined in RFC 2409 Appendix A.
*/
unsigned char*
make_transform(size_t *length, unsigned next, unsigned number,
unsigned trans_id, unsigned char *attr, size_t attr_len) {
struct isakmp_transform* hdr; /* Transform header */
unsigned char *payload;
unsigned char *cp;
size_t len; /* Payload Length */
/* Allocate and initialise the transform header */
hdr = Malloc(sizeof(struct isakmp_transform));
memset(hdr, mbz_value, sizeof(struct isakmp_transform));
hdr->isat_np = next; /* Next payload type */
hdr->isat_transnum = number; /* Transform Number */
hdr->isat_transid = trans_id;
len = attr_len + sizeof(struct isakmp_transform);
hdr->isat_length = htons(len); /* Transform length */
*length = len;
/* Allocate memory for payload and copy structures to payload */
payload = Malloc(len);
cp = payload;
memcpy(cp, hdr, sizeof(struct isakmp_transform));
free(hdr);
cp += sizeof(struct isakmp_transform);
memcpy(cp, attr, attr_len);
return payload;
}
/*
* add_transform -- Add a transform payload to set of transforms.
*
* Inputs:
*
* finished 0 if adding a new transform; 1 if finalising.
* length (output) length of entire transform payload.
* trans_id Transform ID
* attr Pointer to list of attributes
* attr_len Length of attribute list
*
* Returns:
*
* Pointer to new set of transform payloads.
*
* This function can either be called with finished = 0, in which case
* attr and attr_len must be specified, and the function will return NULL,
* OR it can be called with finished = 1 in which case attr and attr_len
* are ignored and the function will return a pointer to the finished
* payload and will set *length to the length of this payload.
*/
unsigned char*
add_transform(int finished, size_t *length, unsigned trans_id,
unsigned char *attr, size_t attr_len) {
static int first_transform = 1;
static unsigned char *trans_start=NULL; /* Start of set of transforms */
static size_t cur_offset; /* Start of current transform */
static size_t end_offset; /* End of transforms */
static unsigned trans_no=1;
unsigned char *trans; /* Transform payload */
size_t len; /* Transform length */
/*
* Construct a transform if we are not finalising.
* Set next to ISAKMP_NEXT_T (more transforms), and increment trans_no for
* next time round.
*/
if (!finished) {
trans = make_transform(&len, ISAKMP_NEXT_T, trans_no, trans_id, attr,
attr_len);
trans_no++;
if (first_transform) {
cur_offset = 0;
end_offset = len;
trans_start = Malloc(end_offset);
memcpy(trans_start, trans, len);
first_transform = 0;
} else {
cur_offset = end_offset;
end_offset += len;
trans_start = Realloc(trans_start, end_offset);
memcpy(trans_start+cur_offset, trans, len);
}
free(trans);
return NULL;
} else {
struct isakmp_transform* hdr =
(struct isakmp_transform*) (trans_start+cur_offset); /* Overlay */
first_transform = 1;
hdr->isat_np = ISAKMP_NEXT_NONE; /* No more transforms */
*length = end_offset;
return trans_start;
}
}
/*
* make_transform2 -- Construct a single IKEv2 transform payload
*
* Inputs:
*
* length (output) length of entire transform payload.
* next Next Payload Type (3 = More transforms; 0=No more transforms)
* trans_type Transform type
* trans_id Transform ID
* attr Pointer to list of attributes, or NULL for no attributes
* attr_len Attribute length in bytes. Zero if no attributes.
*
* Returns:
*
* Pointer to transform payload.
*
* This constructs a single IKEv2 transform payload.
* Most of the values are defined in RFC 4306 Section 3.3.
*/
unsigned char*
make_transform2(size_t *length, unsigned next, unsigned trans_type,
unsigned trans_id, unsigned char *attr, size_t attr_len) {
struct isakmp_transform2* hdr; /* Transform header */
unsigned char *payload;
unsigned char *cp;
size_t len; /* Payload Length */
/* Allocate and initialise the transform header */
hdr = Malloc(sizeof(struct isakmp_transform2));
memset(hdr, mbz_value, sizeof(struct isakmp_transform2));
hdr->isat2_np = next; /* Next payload type */
hdr->isat2_transtype = trans_type; /* Transform Type */
hdr->isat2_transid = htons(trans_id); /* Transform ID */
len = attr_len + sizeof(struct isakmp_transform2);
hdr->isat2_length = htons(len); /* Transform length */
*length = len;
/* Allocate memory for payload and copy structures to payload */
payload = Malloc(len);
cp = payload;
memcpy(cp, hdr, sizeof(struct isakmp_transform2));
free(hdr);
cp += sizeof(struct isakmp_transform2);
memcpy(cp, attr, attr_len);
return payload;
}
/*
* add_transform2 -- Add a transform payload to set of transforms.
*
* Inputs:
*
* finished 0 if adding a new transform; 1 if finalising.
* length (output) length of entire transform payload.
* trans_type Transform type
* trans_id Transform ID
* attr Pointer to list of attributes
* attr_len Length of attribute list
*
* Returns:
*
* Pointer to new set of transform payloads.
*
* This function can either be called with finished = 0, in which case
* attr and attr_len must be specified, and the function will return NULL,
* OR it can be called with finished = 1 in which case attr and attr_len
* are ignored and the function will return a pointer to the finished
* payload and will set *length to the length of this payload.
*/
unsigned char*
add_transform2(int finished, size_t *length, unsigned trans_type,
unsigned trans_id, unsigned char *attr, size_t attr_len) {
static int first_transform = 1;
static unsigned char *trans_start=NULL; /* Start of set of transforms */
static size_t cur_offset; /* Start of current transform */
static size_t end_offset; /* End of transforms */
unsigned char *trans; /* Transform payload */
size_t len; /* Transform length */
/*
* Construct a transform if we are not finalising.
* Set next to ISAKMP_NEXT_T (more transforms)
*/
if (!finished) {
trans = make_transform2(&len, ISAKMP_NEXT_T, trans_type, trans_id, attr,
attr_len);
if (first_transform) {
cur_offset = 0;
end_offset = len;
trans_start = Malloc(end_offset);
memcpy(trans_start, trans, len);
first_transform = 0;
} else {
cur_offset = end_offset;
end_offset += len;
trans_start = Realloc(trans_start, end_offset);
memcpy(trans_start+cur_offset, trans, len);
}
free(trans);
return NULL;
} else {
struct isakmp_transform2* hdr =
(struct isakmp_transform2*) (trans_start+cur_offset); /* Overlay */
first_transform = 1;
hdr->isat2_np = ISAKMP_NEXT_NONE; /* No more transforms */
*length = end_offset;
return trans_start;
}
}
/*
* make_attr -- Construct a transform attribute
*
* Inputs:
*
* outlen (output) Total length of transform attribute.
* type Attribute Type. 'B' = basic, 'V' = variable.
* class Attribute Class
* length Attribute data length for variable type (ignored for basic).
* b_value Basic Attribute Value
* v_value Pointer to Variable Attribute Value
*
* Returns:
*
* Pointer to transform attribute.
*
* For variable attribute types, the data must be in network byte
* order, and its length should be a multiple of 4 bytes to avoid
* alignment issues.
*
* If type is "B", then length and v_value are ignored. If type is "V",
* then b_value is ignored.
*/
unsigned char *
make_attr(size_t *outlen, int type, unsigned class, size_t length,
unsigned b_value, void *v_value) {
struct isakmp_attribute *hdr;
unsigned char *cp;
size_t total_len;
total_len = sizeof(struct isakmp_attribute);
if (type == 'V')
total_len += length;
cp = Malloc(total_len);
hdr = (struct isakmp_attribute *) cp;
memset(hdr, mbz_value, sizeof(struct isakmp_attribute));
if (type == 'B') { /* Basic Attribute */
hdr->isaat_af_type = htons(class | 0x8000);
hdr->isaat_lv = htons(b_value);
} else { /* Variable Attribute */
hdr->isaat_af_type = htons(class);
hdr->isaat_lv = htons(length);
memcpy(cp+sizeof(struct isakmp_attribute), v_value, length);
}
*outlen = total_len;
return cp;
}
/*
* add_attr -- Add a new attribute onto the list of attributes
*
* Inputs:
*
* finished 0 if adding a new attribute; 1 if finalising.
* outlen (output) Total length of attribute list.
* type Attribute Type. 'B' = basic, 'V' = variable.
* class Attribute Class
* length Attribute data length for variable type (ignored for basic).
* b_value Basic Attribute Value
* v_value Pointer to Variable Attribute Value
*
* Returns:
*
* Pointer to attribute list
*
* This function can either be called with finished = 0, in which case
* type, class, length and either b_value or v_value must be specified,
* and the function will return NULL; or it can be called with
* finished = 1 in which case type, class, length, b_value and v_value
* are ignored and the function will return a pointer to the finished
* attribute list and will set *outlen to the length of the attribute
* list.
*/
unsigned char *
add_attr(int finished, size_t *outlen, int type, unsigned class, size_t length,
unsigned b_value, void *v_value) {
static int first_attr=1;
unsigned char *attr;
static unsigned char *attr_start=NULL; /* Start of attr list */
static size_t cur_offset; /* Start of current attr */
static size_t end_offset; /* End of attr list */
size_t len; /* Attr length */
/*
* Construct a new attribute if we are not finalising.
*/
if (!finished) {
attr = make_attr(&len, type, class, length, b_value, v_value);
if (first_attr) {
cur_offset = 0;
end_offset = len;
attr_start = Malloc(end_offset);
memcpy(attr_start, attr, len);
first_attr = 0;
} else {
cur_offset = end_offset;
end_offset += len;
attr_start = Realloc(attr_start, end_offset);
memcpy(attr_start+cur_offset, attr, len);
}
return NULL;
} else {
first_attr = 1;
*outlen = end_offset;
return attr_start;
}
}
/*
* make_vid -- Construct a vendor id payload
*
* Inputs:
*
* length (output) length of Vendor ID payload.
* next Next Payload Type
* vid_data Vendor ID data
* vid_data_len Vendor ID data length
*
* Returns:
*
* Pointer to vendor id payload.
*
* This constructs a vendor id payload. It fills in the static values.
* The next pointer value must be filled in later.
*/
unsigned char*
make_vid(size_t *length, unsigned next, unsigned char *vid_data,
size_t vid_data_len) {
unsigned char *payload;
struct isakmp_vid* hdr;
payload = Malloc(sizeof(struct isakmp_vid)+vid_data_len);
hdr = (struct isakmp_vid*) payload; /* Overlay vid struct on payload */
memset(hdr, mbz_value, sizeof(struct isakmp_vid));
hdr->isavid_np = next; /* Next payload type */
hdr->isavid_length = htons(sizeof(struct isakmp_vid)+vid_data_len);
memcpy(payload+sizeof(struct isakmp_vid), vid_data, vid_data_len);
*length = sizeof(struct isakmp_vid) + vid_data_len;
return payload;
}
/*
* add_vid -- Add a vendor ID payload to the set of VIDs.
*
* Inputs:
*
* finished 0 if adding a new VIDs; 1 if finalising.
* length (output) length of entire VID payload set.
* vid_data Vendor ID data
* vid_data_len Vendor ID data length
* next Next payload type (only when finished == 1)
*
* Returns:
*
* Pointer to the VID payload.
*
* This function can either be called with finished = 0, in which case
* vid_data and vid_data_len must be specified, and
* the function will return NULL, OR it can be called with finished = 1
* in which case vid_data and vid_data_len are
* ignored and the function will return a pointer to the finished
* payload and will set *length to the length of this payload.
*/
unsigned char*
add_vid(int finished, size_t *length, unsigned char *vid_data,
size_t vid_data_len, unsigned next) {
static int first_vid = 1;
static unsigned char *vid_start=NULL; /* Start of set of VIDs */
static size_t cur_offset; /* Start of current VID */
static size_t end_offset; /* End of VIDs */
unsigned char *vid; /* VID payload */
size_t len; /* VID length */
/*
* Construct a VID if we are not finalising.
*/
if (!finished) {
vid = make_vid(&len, ISAKMP_NEXT_VID, vid_data, vid_data_len);
if (first_vid) {
cur_offset = 0;
end_offset = len;
vid_start = Malloc(end_offset);
memcpy(vid_start, vid, len);
first_vid = 0;
} else {
cur_offset = end_offset;
end_offset += len;
vid_start = Realloc(vid_start, end_offset);
memcpy(vid_start+cur_offset, vid, len);
}
return NULL;
} else {
struct isakmp_vid* hdr =
(struct isakmp_vid*) (vid_start+cur_offset); /* Overlay */
hdr->isavid_np = next;
*length = end_offset;
return vid_start;
}
}
/*
* make_ke -- Make a Key Exchange payload
*
* Inputs:
*
* length (output) length of key exchange payload.
* next Next Payload Type
* kx_data_len Key exchange data length
*
* Returns:
*
* Pointer to key exchange payload.
*
* A real implementation would fill in the key exchange payload with the
* Diffie Hellman public value. However, we just use random data.
*/
unsigned char*
make_ke(size_t *length, unsigned next, size_t kx_data_len) {
unsigned char *payload;
struct isakmp_kx* hdr;
unsigned char *kx_data;
unsigned i;
if (kx_data_len % 4)
err_msg("Key exchange data length %d is not a multiple of 4",
kx_data_len);
payload = Malloc(sizeof(struct isakmp_kx)+kx_data_len);
hdr = (struct isakmp_kx*) payload; /* Overlay kx struct on payload */
memset(hdr, mbz_value, sizeof(struct isakmp_kx));
kx_data = payload + sizeof(struct isakmp_kx);
for (i=0; i<kx_data_len; i++)
*(kx_data++) = (unsigned char) random_byte();
hdr->isakx_np = next; /* Next payload type */
hdr->isakx_length = htons(sizeof(struct isakmp_kx)+kx_data_len);
*length = sizeof(struct isakmp_kx) + kx_data_len;
return payload;
}
/*
* make_ke2 -- Make an IKEv2 Key Exchange payload
*
* Inputs:
*
* length (output) length of key exchange payload.
* next Next Payload Type
* dh_group Diffie Hellman group number
* kx_data_len Key exchange data length
*
* Returns:
*
* Pointer to key exchange payload.
*
* A real implementation would fill in the key exchange payload with the
* Diffie Hellman public value. However, we just use random data.
*/
unsigned char*
make_ke2(size_t *length, unsigned next, unsigned dh_group, size_t kx_data_len) {
unsigned char *payload;
struct isakmp_kx2* hdr;
unsigned char *kx_data;
unsigned i;
if (kx_data_len % 4)
err_msg("Key exchange data length %d is not a multiple of 4",
kx_data_len);
payload = Malloc(sizeof(struct isakmp_kx2)+kx_data_len);
hdr = (struct isakmp_kx2*) payload; /* Overlay kx struct on payload */
memset(hdr, mbz_value, sizeof(struct isakmp_kx2));
kx_data = payload + sizeof(struct isakmp_kx2);
for (i=0; i<kx_data_len; i++)
*(kx_data++) = (unsigned char) random_byte();
hdr->isakx2_np = next; /* Next payload type */
hdr->isakx2_length = htons(sizeof(struct isakmp_kx2)+kx_data_len);
hdr->isakx2_dhgroup = htons(dh_group);
*length = sizeof(struct isakmp_kx2) + kx_data_len;
return payload;
}
/*
* make_nonce -- Make a Nonce payload
*
* Inputs:
*
* length (output) length of nonce payload.
* next Next Payload Type
* nonce_len Length of nonce data.
*
* Returns:
*
* Pointer to nonce payload.
*
* RFC 2409 states that: "The length of nonce payload MUST be between 8
* and 256 bytes inclusive". However, this function doesn't enforce the
* restriction.
*/
unsigned char*
make_nonce(size_t *length, unsigned next, size_t nonce_len) {
unsigned char *payload;
struct isakmp_nonce* hdr;
unsigned char *cp;
unsigned i;
payload = Malloc(sizeof(struct isakmp_nonce)+nonce_len);
hdr = (struct isakmp_nonce*) payload; /* Overlay nonce struct on payload */
memset(hdr, mbz_value, sizeof(struct isakmp_nonce));
hdr->isanonce_np = next; /* Next payload type */
hdr->isanonce_length = htons(sizeof(struct isakmp_nonce)+nonce_len);
cp = payload+sizeof(struct isakmp_vid);
for (i=0; i<nonce_len; i++)
*(cp++) = (unsigned char) random_byte();
*length = sizeof(struct isakmp_nonce)+nonce_len;
return payload;
}
/*
* make_id -- Make an Identification payload
*
* Inputs:
*
* length (output) length of ID payload.
* next Next Payload Type
* idtype Identification Type
* id_data ID data
* id_data_len ID data length
*
*/
unsigned char*
make_id(size_t *length, unsigned next, unsigned idtype, unsigned char *id_data,
size_t id_data_len) {
unsigned char *payload;
struct isakmp_id* hdr;
payload = Malloc(sizeof(struct isakmp_id)+id_data_len);
hdr = (struct isakmp_id*) payload; /* Overlay ID struct on payload */
memset(hdr, mbz_value, sizeof(struct isakmp_id));
hdr->isaid_np = next; /* Next payload type */
hdr->isaid_length = htons(sizeof(struct isakmp_id)+id_data_len);
hdr->isaid_idtype = idtype;
/*
* RFC 2407 4.6.2: "During Phase I negotiations, the ID port and protocol
* fields MUST be set to zero or to UDP port 500"
*/
hdr->isaid_doi_specific_a = 17; /* Protocol: UDP */
hdr->isaid_doi_specific_b = htons(500); /* Port: 500 */
memcpy(payload+sizeof(struct isakmp_id), id_data, id_data_len);
*length = sizeof(struct isakmp_id) + id_data_len;
return payload;
}
/*
* make_udphdr -- Construct a UDP header for encapsulated IKE
*
* Inputs:
*
* length (output) length of UDP header
* sport UDP source port
* dport UDP destination port
* udplen UDP length
*
* Returns:
*
* Pointer to constructed UDP header
*
* This constructs a UDP header which is used for IKE
* encapsulated within TCP.
*/
unsigned char*
make_udphdr(size_t *length, unsigned sport, unsigned dport, unsigned udplen) {
unsigned char *payload;
ike_udphdr *hdr;
payload = Malloc(sizeof(ike_udphdr));
hdr = (ike_udphdr*) payload; /* Overlay UDP hdr on payload */
hdr->source = htons(sport);
hdr->dest = htons(dport);
hdr->len = htons(udplen);
hdr->check = 0; /* should use in_cksum() */
*length = sizeof(ike_udphdr);
return payload;
}
/*
* make_cr -- Construct a certificate request payload
*
* Inputs:
*
* length (output) length of certificate request payload.
* next Next Payload Type
* cr_data Certificate request data
* cr_data_len Certificate request data length
*
* Returns:
*
* Pointer to certificate request payload.
*
* This constructs a certificate request payload.
*/
unsigned char*
make_cr(size_t *length, unsigned next, unsigned char *cr_data,
size_t cr_data_len) {
unsigned char *payload;
struct isakmp_generic* hdr;
payload = Malloc(sizeof(struct isakmp_generic)+cr_data_len);
hdr = (struct isakmp_generic*) payload;
memset(hdr, mbz_value, sizeof(struct isakmp_generic));
hdr->isag_np = next; /* Next payload type */
hdr->isag_length = htons(sizeof(struct isakmp_generic)+cr_data_len);
memcpy(payload+sizeof(struct isakmp_generic), cr_data, cr_data_len);
*length = sizeof(struct isakmp_generic) + cr_data_len;
return payload;
}
/*
* skip_payload -- Skip an ISAMKP payload
*
* Inputs:
*
* cp Pointer to start of payload to skip
* len Packet length remaining
* next Next payload type.
*
* Returns:
*
* Pointer to start of next payload, or NULL if no next payload.
*/
unsigned char *
skip_payload(unsigned char *cp, size_t *len, unsigned *next) {
struct isakmp_generic hdr;
/*
* Signal no more payloads by setting length to zero, next
* payload to none and returning NULL if the packet length is
* less that the ISAKMP generic header size.
*/
if (*len < sizeof(struct isakmp_generic)) {
*len=0;
*next=ISAKMP_NEXT_NONE;
return NULL;
}
/*
* Fill in the generic header from the packet. We must do this
* by copying rather than overlaying because we cannot be sure
* that "cp" is suitably aligned.
*/
memcpy(&hdr, cp, sizeof(hdr));
/*
* Signal no more payloads if:
*
* The payload length is greater than the packet length; or
* The payload length is less than the size of the generic header; or
* There is no next payload.
*
* Also set *next to none and return null.
*/
if (ntohs(hdr.isag_length) >= *len ||
ntohs(hdr.isag_length) < sizeof(struct isakmp_generic) ||
hdr.isag_np == ISAKMP_NEXT_NONE) {
*len=0;
*next=ISAKMP_NEXT_NONE;
return NULL;
}
/*
* There is another payload after this one, so adjust length and
* return pointer to next payload.
*/
*len = *len - ntohs(hdr.isag_length);
*next = hdr.isag_np;
return cp + ntohs(hdr.isag_length);
}
/*
* process_isakmp_hdr -- Process ISAKMP header
*
* Inputs:
*
* cp Pointer to start of ISAKMP header
* len Packet length remaining
* next Next payload type.
* type Exchange type
* hdr_descr ISAKMP Header description string
*
* Returns:
*
* Pointer to start of next payload, or NULL if no next payload.
*/
unsigned char *
process_isakmp_hdr(unsigned char *cp, size_t *len, unsigned *next,
unsigned *type, char **hdr_descr) {
struct isakmp_hdr *hdr = (struct isakmp_hdr *) cp;
char *msg;
char *msg2;
/*
* Signal no more payloads by setting length to zero if:
*
* The packet length is less than the ISAKMP header size; or
* The payload length is less than the size of the header; or
* There is no next payload.
*
* Also set *next to none and return null.
*/
if (*len < sizeof(struct isakmp_hdr) ||
ntohl(hdr->isa_length) < sizeof(struct isakmp_hdr) ||
hdr->isa_np == ISAKMP_NEXT_NONE) {
*hdr_descr = NULL;
*len=0;
*next=ISAKMP_NEXT_NONE;
*type=ISAKMP_XCHG_NONE;
return NULL;
}
/*
* Create ISAKMP header description string.
*/
msg2 = hexstring((unsigned char *)hdr->isa_rcookie, 8);
msg = make_message("HDR=(CKY-R=%s", msg2);
free(msg2);
if (hdr->isa_version != 0x10) { /* Version not 1.0 */
msg2 = msg;
if (hdr->isa_version == 0x20) {
msg = make_message("%s, IKEv2", msg2);
} else {
msg = make_message("%s, version=0x%.2x", msg2,
hdr->isa_version);
}
free(msg2);
}
if ((hdr->isa_version==0x10 && hdr->isa_flags != 0) ||
(hdr->isa_version==0x20 && hdr->isa_flags != 0x20)) {
msg2 = msg;
msg = make_message("%s, flags=0x%.2x", msg2, hdr->isa_flags);
free(msg2);
}
if (hdr->isa_msgid != 0) { /* Non-Zero msgid - shouldn't happen */
msg2 = msg;
msg = make_message("%s, msgid=%.8x", msg2, ntohl(hdr->isa_msgid));
free(msg2);
}
msg2 = msg;
msg = make_message("%s)", msg2);
free(msg2);
/*
* There is another payload after this one, so adjust length and
* return pointer to next payload.
*/
*hdr_descr = msg;
*len = *len - sizeof(struct isakmp_hdr);
*next = hdr->isa_np;
*type = hdr->isa_xchg;
return cp + sizeof(struct isakmp_hdr);
}
/*
* process_sa -- Process SA Payload
*
* Inputs:
*
* cp Pointer to start of SA payload
* len Packet length remaining
* type Exchange type.
* quiet Only print the basic info if nonzero
* multiline Split decodes across lines if nonzero
* hdr_descr ISAKMP Header description string
*
* Returns:
*
* Pointer to SA description string.
*
* The description string pointer returned points to malloc'ed storage
* which should be free'ed by the caller when it's no longer needed.
*/
char *
process_sa(unsigned char *cp, size_t len, unsigned type, int quiet,
int multiline, char *hdr_descr) {
struct isakmp_sa *sa_hdr = (struct isakmp_sa *) cp;
struct isakmp_proposal *prop_hdr =
(struct isakmp_proposal *) (cp + sizeof(struct isakmp_sa));
char *msg;
char *msg2;
char *msg3;
unsigned char *attr_ptr;
size_t safelen; /* Shorter of actual and claimed length */
safelen = (ntohs(sa_hdr->isasa_length)<len)?ntohs(sa_hdr->isasa_length):len;
/*
* Return with a "too short to decode" message if either the remaining
* packet length or the claimed payload length is less than the combined
* size of the SA, Proposal, and transform headers.
*/
if (safelen < sizeof(struct isakmp_sa) + sizeof(struct isakmp_proposal) +
sizeof(struct isakmp_transform))
return make_message("IKE Handshake returned (packet too short to decode)");
/*
* Build the first part of the message based on the exchange type.
*/
if (type == ISAKMP_XCHG_IDPROT) { /* Main Mode */
msg = make_message("Main Mode Handshake returned");
} else if (type == ISAKMP_XCHG_AGGR) { /* Aggressive Mode */
msg = make_message("Aggressive Mode Handshake returned");
} else {
msg = make_message("UNKNOWN Mode Handshake returned (%u)", type);
}
/*
* If quiet is not in effect, add the ISAKMP header details to the message.
*/
if (!quiet) {
msg2 = msg;
msg = make_message("%s%s%s", msg2, multiline?"\n\t":" ", hdr_descr);
free(msg2);
}
/*
* We should have exactly one transform in the server's response.
* If there is not exactly one transform, then add this fact to the
* message. This normally means that we've received our own output.
*/
if (prop_hdr->isap_notrans != 1) {
msg2 = msg;
msg = make_message("%s (%d transforms)", msg2, prop_hdr->isap_notrans);
free(msg2);
}
/*
* If quiet is not in effect, and we have exactly one transform, add the
* transform details to the message.
*/
if (!quiet && prop_hdr->isap_notrans==1) {
int firstloop=1;
msg2 = msg;
msg = make_message("%s%sSA=(", msg2, multiline?"\n\t":" ");
free(msg2);
if (prop_hdr->isap_spisize != 0) { /* Non-Zero SPI */
msg2 = msg;
msg3 = hexstring(cp + sizeof(struct isakmp_sa) +
sizeof(struct isakmp_proposal),
prop_hdr->isap_spisize);
msg = make_message("%sSPI=%s ", msg2, msg3);
free(msg2);
free(msg3);
}
attr_ptr = (cp + sizeof(struct isakmp_sa) +
sizeof(struct isakmp_proposal) + prop_hdr->isap_spisize +
sizeof(struct isakmp_transform));
safelen -= sizeof(struct isakmp_sa) +
sizeof(struct isakmp_proposal) + prop_hdr->isap_spisize +
sizeof(struct isakmp_transform);
while (safelen) {
msg2 = msg;
msg3 = process_attr(&attr_ptr, &safelen);
if (firstloop) { /* Don't need leading space for 1st attr */
msg = make_message("%s%s", msg2, msg3);
firstloop=0;
} else {
msg = make_message("%s %s", msg2, msg3);
}
free(msg2);
free(msg3);
}
msg2 = msg;
msg = make_message("%s)", msg2);
free(msg2);
}
return msg;
}
/*
* process_sa2 -- Process an IKEv2 SA Payload
*
* Inputs:
*
* cp Pointer to start of SA payload
* len Packet length remaining
* type Exchange type.
* quiet Only print the basic info if nonzero
* multiline Split decodes across lines if nonzero
* hdr_descr ISAKMP Header description string
*
* Returns:
*
* Pointer to SA description string.
*
* The description string pointer returned points to malloc'ed storage
* which should be free'ed by the caller when it's no longer needed.
*/
char *
process_sa2(unsigned char *cp, size_t len, unsigned type, int quiet,
int multiline, char *hdr_descr) {
struct isakmp_sa2 *sa_hdr = (struct isakmp_sa2 *) cp;
struct isakmp_proposal *prop_hdr =
(struct isakmp_proposal *) (cp + sizeof(struct isakmp_sa2));
char *msg;
char *msg2;
char *msg3;
unsigned char *trans_ptr;
size_t safelen; /* Shorter of actual and claimed length */
safelen = (ntohs(sa_hdr->isasa2_length)<len)?ntohs(sa_hdr->isasa2_length):len;
/*
* Return with a "too short to decode" message if either the remaining
* packet length or the claimed payload length is less than the combined
* size of the SA, Proposal, and transform headers.
*/
if (safelen < sizeof(struct isakmp_sa2) + sizeof(struct isakmp_proposal) +
sizeof(struct isakmp_transform2))
return make_message("IKEv2 Handshake returned (packet too short to decode)");
/*
* Build the first part of the message based on the exchange type.
*/
if (type == ISAKMP_XCHG_IKE_SA_INIT) {
msg = make_message("IKEv2 SA_INIT Handshake returned");
} else {
msg = make_message("UNKNOWN Mode Handshake returned (%u)", type);
}
/*
* If quiet is not in effect, add the ISAKMP header details to the message.
*/
if (!quiet) {
msg2 = msg;
msg = make_message("%s%s%s", msg2, multiline?"\n\t":" ", hdr_descr);
free(msg2);
}
/*
* We should have exactly one proposal in the server's response.
* If there is not exactly one proposal, then add this fact to the
* message. This normally means that we've received our own output.
*/
if (prop_hdr->isap_np != ISAKMP_NEXT_NONE) {
msg2 = msg;
msg = make_message("%s (multiple proposals)", msg2);
free(msg2);
}
/*
* If quiet is not in effect, and we have exactly one proposal, add the
* proposal details to the message.
*/
if (!quiet && prop_hdr->isap_np == ISAKMP_NEXT_NONE) {
int firstloop=1;
msg2 = msg;
msg = make_message("%s%sSA=(", msg2, multiline?"\n\t":" ");
free(msg2);
if (prop_hdr->isap_spisize != 0) { /* Non-Zero SPI */
msg2 = msg;
msg3 = hexstring(cp + sizeof(struct isakmp_sa2) +
sizeof(struct isakmp_proposal),
prop_hdr->isap_spisize);
msg = make_message("%sSPI=%s ", msg2, msg3);
free(msg2);
free(msg3);
}
trans_ptr = cp + sizeof(struct isakmp_sa2) +
sizeof(struct isakmp_proposal) + prop_hdr->isap_spisize;
safelen -= sizeof(struct isakmp_sa2) +
sizeof(struct isakmp_proposal) + prop_hdr->isap_spisize;
while (safelen) {
msg2 = msg;
msg3 = process_transform2(&trans_ptr, &safelen);
if (firstloop) { /* Don't need leading space for 1st attr */
msg = make_message("%s%s", msg2, msg3);
firstloop=0;
} else {
msg = make_message("%s %s", msg2, msg3);
}
free(msg2);
free(msg3);
}
msg2 = msg;
msg = make_message("%s)", msg2);
free(msg2);
}
return msg;
}
/*
* process_attr -- Process transform attribute
*
* Inputs:
*
* cp Pointer to start of attribute
* len Packet length remaining
*
* Returns:
*
* Pointer to attribute description string.
*
* The description string pointer returned points to malloc'ed storage
* which should be free'ed by the caller when it's no longer needed.
*/
char *
process_attr(unsigned char **cp, size_t *len) {
char *msg;
struct isakmp_attribute *attr_hdr = (struct isakmp_attribute *) *cp;
char attr_type; /* B=Basic, V=Variable */
unsigned attr_class;
unsigned attr_value=0;
char *attr_class_str;
char *attr_value_str;
size_t value_len;
size_t size;
if (ntohs(attr_hdr->isaat_af_type) & 0x8000) { /* Basic attribute */
attr_type = 'B';
attr_class = ntohs (attr_hdr->isaat_af_type) & 0x7fff;
attr_value = ntohs (attr_hdr->isaat_lv);
value_len = 0; /* Value is in length field */
} else { /* Variable attribute */
attr_type = 'V';
attr_class = ntohs (attr_hdr->isaat_af_type);
value_len = ntohs (attr_hdr->isaat_lv);
}
attr_class_str = make_message("%s", id_to_name(attr_class, attr_map));
if (attr_type == 'B') {
switch (attr_class) {
case 1: /* Encryption Algorithm */
attr_value_str = make_message("%s", id_to_name(attr_value, enc_map));
break;
case 2: /* Hash Algorithm */
attr_value_str = make_message("%s", id_to_name(attr_value, hash_map));
break;
case 3: /* Authentication Method */
attr_value_str = make_message("%s", id_to_name(attr_value, auth_map));
break;
case 4: /* Group Description */
attr_value_str = make_message("%s", id_to_name(attr_value, dh_map));
break;
case 11: /* Life Type */
attr_value_str = make_message("%s", id_to_name(attr_value, life_map));
break;
default:
attr_value_str = make_message("%u", attr_value);
break;
}
} else {
attr_value_str = hexstring((*cp) + sizeof (struct isakmp_attribute),
value_len);
}
if (attr_type == 'B')
msg = make_message("%s=%s", attr_class_str, attr_value_str);
else
msg = make_message("%s(%u)=0x%s", attr_class_str, value_len,
attr_value_str);
free(attr_class_str);
free(attr_value_str);
size=sizeof (struct isakmp_attribute) + value_len;
if (size >= *len) {
*len=0;
} else {
*len -= size;
(*cp) += size;
}
return msg;
}
/*
* process_transform2 -- Process IKEv2 transforms
*
* Inputs:
*
* cp Pointer to start of transform
* len Packet length remaining
*
* Returns:
*
* Pointer to transform description string.
*
* The description string pointer returned points to malloc'ed storage
* which should be free'ed by the caller when it's no longer needed.
*/
char *
process_transform2(unsigned char **cp, size_t *len) {
char *msg;
struct isakmp_transform2 *trans_hdr = (struct isakmp_transform2 *) *cp;
unsigned trans_type;
unsigned trans_id;
char *trans_type_str;
char *trans_id_str;
size_t size;
trans_type = trans_hdr->isat2_transtype;
trans_id = ntohs(trans_hdr->isat2_transid);
trans_type_str = make_message("%s", id_to_name(trans_type, trans_type_map));
switch (trans_type) {
case 1: /* Encryption Algorithm */
trans_id_str = make_message("%s", id_to_name(trans_id, encr_map));
break;
case 2: /* Pseudo-random Function */
trans_id_str = make_message("%s", id_to_name(trans_id, prf_map));
break;
case 3: /* Integrity Algorithm */
trans_id_str = make_message("%s", id_to_name(trans_id, integ_map));
break;
case 4: /* Diffie-Hellman Group */
trans_id_str = make_message("%s", id_to_name(trans_id, dh_map));
break;
default:
trans_id_str = make_message("%u", trans_id);
break;
}
size=ntohs(trans_hdr->isat2_length);
if (size > sizeof(struct isakmp_transform2)) { /* Attributes present */
unsigned char *attr_ptr = (*cp) + sizeof(struct isakmp_transform2);
struct isakmp_attribute *attr_hdr = (struct isakmp_attribute *) attr_ptr;
unsigned attr_class=0;
unsigned attr_value=0;
if (ntohs(attr_hdr->isaat_af_type) & 0x8000) { /* Basic attribute */
attr_class = ntohs (attr_hdr->isaat_af_type) & 0x7fff;
attr_value = ntohs (attr_hdr->isaat_lv);
} else { /* Variable attribute */
warn_msg("WARNING: Ignoring IKEv2 variable length transform attribute");
}
msg = make_message("%s=%s,%s=%u", trans_type_str, trans_id_str,
id_to_name(attr_class, attr_map), attr_value);
} else { /* No attributes */
msg = make_message("%s=%s", trans_type_str, trans_id_str);
}
free(trans_type_str);
free(trans_id_str);
if (size >= *len) {
*len=0;
} else {
*len -= size;
(*cp) += size;
}
return msg;
}
/*
* process_vid -- Process Vendor ID Payload
*
* Inputs:
*
* cp Pointer to start of Vendor ID payload
* len Packet length remaining
* vidlist List of Vendor ID patterns.
*
* Returns:
*
* Pointer to Vendor ID description string.
*
* The description string pointer returned points to malloc'ed storage
* which should be free'ed by the caller when it's no longer needed.
*/
char *
process_vid(unsigned char *cp, size_t len, vid_pattern_list *vidlist) {
struct isakmp_vid *hdr = (struct isakmp_vid *) cp;
char *msg;
char *hexvid;
char *p;
unsigned char *vid_data;
size_t data_len;
vid_pattern_list *ve;
if (len < sizeof(struct isakmp_vid) ||
ntohs(hdr->isavid_length) < sizeof(struct isakmp_vid))
return make_message("VID (packet too short to decode)");
vid_data = cp + sizeof(struct isakmp_vid); /* Points to start of VID data */
data_len = ntohs(hdr->isavid_length) < len ? ntohs(hdr->isavid_length) : len;
data_len -= sizeof(struct isakmp_vid);
hexvid = hexstring(vid_data, data_len);
msg=make_message("VID=%s", hexvid);
/*
* Try to find a match in the Vendor ID pattern list.
*/
ve = vidlist;
while(ve != NULL) {
if (!(regexec(ve->regex, hexvid, 0, NULL, 0))) {
p=msg;
msg=make_message("%s (%s)", p, ve->name);
free(p);
break; /* Stop looking after first match */
}
ve=ve->next;
}
free(hexvid);
return msg;
}
/*
* process_notify -- Process notify Payload
*
* Inputs:
*
* cp Pointer to start of notify payload
* len Packet length remaining
* quiet Only print the basic info if nonzero
* multiline Split decodes across lines if nonzero
* hdr_descr ISAKMP Header description string
*
* Returns:
*
* Pointer to notify description string.
*
* This function is only used for notification messages that are part
* of an informational exchange. Notification messages that are part
* of another exchange type are handled with process_notification()
* instead. This is an ugly hack.
*
* The description string pointer returned points to malloc'ed storage
* which should be free'ed by the caller when it's no longer needed.
*/
char *
process_notify(unsigned char *cp, size_t len, int quiet, int multiline,
char *hdr_descr) {
struct isakmp_notification *hdr = (struct isakmp_notification *) cp;
char *msg;
char *msg2;
unsigned msg_type;
size_t msg_len;
unsigned char *msg_data;
char *notify_msg;
if (len < sizeof(struct isakmp_notification) ||
ntohs(hdr->isan_length) < sizeof(struct isakmp_notification))
return make_message("Notify message (packet too short to decode)");
msg_type = ntohs(hdr->isan_type);
msg_len = ntohs(hdr->isan_length) - sizeof(struct isakmp_notification);
msg_data = cp + sizeof(struct isakmp_notification);
if (msg_type == 9101 || msg_type == 9110) { /* Firewall-1 message types */
notify_msg = printable(msg_data, msg_len);
msg=make_message("Notify message %u (Firewall-1) Message=\"%s\"",
msg_type, notify_msg);
free(notify_msg);
} else { /* All other Message Types */
msg=make_message("Notify message %u (%s)", msg_type,
id_to_name(msg_type, notification_map));
}
/*
* If quiet is not in effect, add the ISAKMP header details to the message.
*/
if (!quiet) {
msg2 = msg;
msg = make_message("%s%s%s", msg2, multiline?"\n\t":" ", hdr_descr);
free(msg2);
}
return msg;
}
/*
* process_notify2 -- Process IKEv2 notify Payload
*
* Inputs:
*
* cp Pointer to start of notify payload
* len Packet length remaining
* quiet Only print the basic info if nonzero
* multiline Split decodes across lines if nonzero
* hdr_descr ISAKMP Header description string
*
* Returns:
*
* Pointer to notify description string.
*
* This function is only used for notification messages that are part
* of an informational exchange. Notification messages that are part
* of another exchange type are handled with process_notification()
* instead. This is an ugly hack.
*
* The description string pointer returned points to malloc'ed storage
* which should be free'ed by the caller when it's no longer needed.
*/
char *
process_notify2(unsigned char *cp, size_t len, int quiet, int multiline,
char *hdr_descr) {
struct isakmp_notification2 *hdr = (struct isakmp_notification2 *) cp;
char *msg;
char *msg2;
unsigned msg_type;
size_t msg_len;
unsigned char *msg_data;
char *notify_msg;
if (len < sizeof(struct isakmp_notification2) ||
ntohs(hdr->isan2_length) < sizeof(struct isakmp_notification2))
return make_message("Notify message (packet too short to decode)");
msg_type = ntohs(hdr->isan2_type);
msg_len = ntohs(hdr->isan2_length) - sizeof(struct isakmp_notification2);
msg_data = cp + sizeof(struct isakmp_notification2);
if (msg_type == 9101 || msg_type == 9110) { /* Firewall-1 message types */
notify_msg = printable(msg_data, msg_len);
msg=make_message("Notify message %u (Firewall-1) Message=\"%s\"",
msg_type, notify_msg);
free(notify_msg);
} else { /* All other Message Types */
msg=make_message("Notify message %u (%s)", msg_type,
id_to_name(msg_type, notification_map2));
}
/*
* If quiet is not in effect, add the ISAKMP header details to the message.
*/
if (!quiet) {
msg2 = msg;
msg = make_message("%s%s%s", msg2, multiline?"\n\t":" ", hdr_descr);
free(msg2);
}
return msg;
}
/*
* process_notification -- Process notification Payload
*
* Inputs:
*
* cp Pointer to start of notify payload
* len Packet length remaining
*
* Returns:
*
* Pointer to notify description string.
*
* The description string pointer returned points to malloc'ed storage
* which should be free'ed by the caller when it's no longer needed.
*/
char *
process_notification(unsigned char *cp, size_t len) {
struct isakmp_notification *hdr = (struct isakmp_notification *) cp;
char *msg;
char *msg2;
unsigned msg_type;
size_t msg_len;
unsigned char *msg_data;
char *hex_spi;
unsigned char *notification_spi;
char *hex_data;
size_t spi_len;
uint32_t doi;
unsigned proto_id;
if (len < sizeof(struct isakmp_notification) ||
ntohs(hdr->isan_length) < sizeof(struct isakmp_notification))
return make_message("Notification (packet too short to decode)");
doi = ntohl(hdr->isan_doi);
proto_id = hdr->isan_protoid;
msg_type = ntohs(hdr->isan_type);
notification_spi = cp + sizeof(struct isakmp_notification);
spi_len = hdr->isan_spisize;
hex_spi = hexstring(notification_spi, spi_len);
msg_len = ntohs(hdr->isan_length) - sizeof(struct isakmp_notification) -
spi_len;
msg_data = cp + sizeof(struct isakmp_notification) + spi_len;
hex_data = hexstring(msg_data, msg_len);
msg=make_message("Notification=(");
if (doi != 1) { /* DOI not IPsec */
msg2 = msg;
msg = make_message("%sDOI=%s, ", msg2, id_to_name(doi, doi_map));
free(msg2);
}
if (proto_id != 1) { /* Protocol ID not ISAKMP */
msg2 = msg;
msg = make_message("%sProto_ID=%s, ", msg2,
id_to_name(proto_id, protocol_map));
free(msg2);
}
msg2 = msg;
msg=make_message("%sType=%s, SPI=%s, Data=%s)", msg2,
id_to_name(msg_type, notification_map),
hex_spi, hex_data);
free(msg2);
free(hex_spi);
free(hex_data);
return msg;
}
/*
* process_id -- Process identification Payload
*
* Inputs:
*
* cp Pointer to start of identification payload
* len Packet length remaining
*
* Returns:
*
* Pointer to identification description string.
*
* The description string pointer returned points to malloc'ed storage
* which should be free'ed by the caller when it's no longer needed.
*/
char *
process_id(unsigned char *cp, size_t len) {
struct isakmp_id *hdr = (struct isakmp_id *) cp;
unsigned idtype;
char *msg;
char *msg2;
unsigned char *id_data;
size_t data_len;
if (len < sizeof(struct isakmp_id) ||
ntohs(hdr->isaid_length) < sizeof(struct isakmp_id))
return make_message("ID (packet too short to decode)");
id_data = cp + sizeof(struct isakmp_id); /* Points to start of ID data */
data_len = ntohs(hdr->isaid_length) < len ? ntohs(hdr->isaid_length) : len;
data_len -= sizeof(struct isakmp_id);
idtype = hdr->isaid_idtype;
switch(idtype) {
char *id; /* Printable ID */
struct in_addr in; /* IPv4 Address */
struct in_addr in2; /* IPv4 Address */
unsigned char *mask; /* Netmask */
case ID_IPV4_ADDR:
if (data_len >= sizeof(struct in_addr)) {
memcpy(&in, id_data, sizeof(struct in_addr));
msg=make_message("Value=%s", inet_ntoa(in));
} else {
msg=make_message("Value too short to decode");
}
break;
case ID_IPV4_ADDR_SUBNET:
if (data_len >= sizeof(struct in_addr) + 4) {
memcpy(&in, id_data, sizeof(struct in_addr));
mask = id_data + sizeof(struct in_addr);
msg=make_message("Value=%s/%u.%u.%u.%u", inet_ntoa(in),
mask[0], mask[1], mask[2], mask[3]);
} else {
msg=make_message("Value too short to decode");
}
break;
case ID_IPV4_ADDR_RANGE:
if (data_len >= 2 * sizeof(struct in_addr)) {
memcpy(&in, id_data, sizeof(struct in_addr));
memcpy(&in2, id_data+sizeof(struct in_addr),
sizeof(struct in_addr));
msg=make_message("Value=%s-%s", inet_ntoa(in), inet_ntoa(in2));
} else {
msg=make_message("Value too short to decode");
}
break;
case ID_FQDN:
case ID_USER_FQDN:
id = printable(id_data, data_len);
msg=make_message("Value=%s", id);
free(id);
break;
case ID_KEY_ID:
id = hexstring(id_data, data_len);
msg = make_message("Value=%s", id);
free(id);
break;
case ID_IPV6_ADDR:
case ID_IPV6_ADDR_SUBNET:
case ID_IPV6_ADDR_RANGE:
case ID_DER_ASN1_DN:
case ID_DER_ASN1_GN:
msg=make_message("Decode not supported for this type");
break;
default:
msg = make_message("Unknown ID Type");
break;
}
msg2=msg;
msg=make_message("ID(Type=%s, %s)", id_to_name(idtype,id_map), msg2);
free(msg2);
return msg;
}
/*
* process_cert -- Process Certificate Payload
*
* Inputs:
*
* cp Pointer to start of certificate payload
* len Packet length remaining
* next The previous next payload type
*
* Returns:
*
* Pointer to certificate description string.
*
* The description string pointer returned points to malloc'ed storage
* which should be free'ed by the caller when it's no longer needed.
*/
char *
process_cert(unsigned char *cp, size_t len, unsigned next) {
struct isakmp_generic *hdr = (struct isakmp_generic *) cp;
char *msg;
unsigned char cert_type;
unsigned char *cert_data;
size_t data_len;
if (len < sizeof(struct isakmp_generic) + 1 ||
ntohs(hdr->isag_length) < sizeof(struct isakmp_generic) + 1)
return make_message("Certificate (packet too short to decode)");
cert_data = cp + sizeof(struct isakmp_generic);
cert_type = *cert_data++;
data_len = ntohs(hdr->isag_length) < len ? ntohs(hdr->isag_length) : len;
data_len -= sizeof(struct isakmp_generic) + 1;
msg=make_message("%s(Type=%s, Length=%u bytes)",
id_to_name(next, payload_map),
id_to_name(cert_type, cert_map), data_len);
return msg;
}
/*
* process_delete -- Process Delete Payload
*
* Inputs:
*
* cp Pointer to start of Delete payload
* len Packet length remaining
*
* Returns:
*
* Pointer to Delete description string.
*
* The description string pointer returned points to malloc'ed storage
* which should be free'ed by the caller when it's no longer needed.
*/
char *
process_delete(unsigned char *cp, size_t len) {
struct isakmp_delete *hdr = (struct isakmp_delete *) cp;
char *msg;
char *hex_spi;
unsigned char *delete_spi;
size_t spi_len;
if (len < sizeof(struct isakmp_delete) ||
ntohs(hdr->isad_length) < sizeof(struct isakmp_delete))
return make_message("Delete (packet too short to decode)");
delete_spi = cp + sizeof(struct isakmp_delete);
spi_len = ntohs(hdr->isad_length) < len ? ntohs(hdr->isad_length) : len;
spi_len -= sizeof(struct isakmp_delete);
hex_spi = hexstring(delete_spi, spi_len);
msg=make_message("Delete=(SPI_Size=%u, SPI_Count=%u, SPI_Data=%s)",
hdr->isad_spisize, ntohs(hdr->isad_nospi), hex_spi);
free(hex_spi);
return msg;
}
/*
* process_generic -- Process Generic ISAKMP Payload
*
* Inputs:
*
* cp Pointer to start of Delete payload
* len Packet length remaining
*
* Returns:
*
* Pointer to payload description string.
*
* The description string pointer returned points to malloc'ed storage
* which should be free'ed by the caller when it's no longer needed.
*/
char *
process_generic(unsigned char *cp, size_t len, unsigned next) {
struct isakmp_generic *hdr = (struct isakmp_generic *) cp;
char *msg;
if (len < sizeof(struct isakmp_generic) ||
ntohs(hdr->isag_length) < sizeof(struct isakmp_generic)) {
msg = make_message("%s (packet too short to decode)",
id_to_name(next, payload_map));
return msg;
}
msg=make_message("%s(%u bytes)", id_to_name(next, payload_map),
ntohs(hdr->isag_length) -
sizeof(struct isakmp_generic));
return msg;
}
/*
* add_isakmp_payload -- Add an ISAKMP payload to the current packet
*
* Inputs:
*
* payload Pointer to the payload to add
* payload_len Length of the payload
* new_payload Pointer to the new payload within the packet
*
* Returns:
*
* A pointer to the ISAKMP packet.
*
* This function assumes that "payload" is a pointer to malloc'ed
* storage, and will free it after use.
*/
unsigned char *
add_isakmp_payload(unsigned char *payload, size_t payload_len,
unsigned char **new_payload) {
static unsigned char *isakmp_packet = NULL;
static size_t offset = 0;
unsigned char *payload_ptr;
/*
* Allocate memory for the packet on the first call.
*/
if (isakmp_packet == NULL) {
isakmp_packet = Malloc(MAXUDP);
}
/*
* Calculate position within the packet to add the new payload.
* Copy the new payload starting at this position, then free the
* payload memory.
*/
payload_ptr = isakmp_packet+offset;
memcpy(payload_ptr, payload, payload_len);
free(payload);
/*
* Set the new_payload argument to the position of the newly added
* payload within the packet, and return the address of the start
* of the packet.
*/
*new_payload = payload_ptr;
return isakmp_packet;
}
/*
* print_payload -- Print an ISAKMP payload in hex
*
* Inputs:
*
* cp Pointer to start of ISAKMP payload
* payload Numeric value of this payload type, 0 = ISAKMP header
* dir Direction: 'I' for initiator or 'R' for responder
*
* Returns:
*
* None
*
* This function is used for debugging. It trusts the length in the
* generic ISAKMP header, and so could misbehave with corrupted packets.
*/
void
print_payload(unsigned char *cp, unsigned payload, int dir) {
struct isakmp_generic *hdr = (struct isakmp_generic *) cp;
struct isakmp_hdr *ihdr = (struct isakmp_hdr *) cp;
char *hexdata;
unsigned char *data;
size_t data_len;
if (payload) { /* Some other payload */
data = cp + sizeof(struct isakmp_generic); /* Points to start of data */
data_len = ntohs(hdr->isag_length);
data_len -= sizeof(struct isakmp_generic);
hexdata = hexstring(data, data_len);
switch (payload) {
case ISAKMP_NEXT_SA:
printf("sa%c_b_hex=\"%s\"\n", (dir=='I')?'i':'r', hexdata);
break;
case ISAKMP_NEXT_KE:
printf("g_x%c_hex=\"%s\"\n", (dir=='I')?'i':'r', hexdata);
break;
case ISAKMP_NEXT_ID:
printf("idi%c_b_hex=\"%s\"\n", (dir=='I')?'i':'r', hexdata);
break;
case ISAKMP_NEXT_HASH:
printf("expected_hash_%c_hex=\"%s\"\n", (dir=='I')?'i':'r', hexdata);
break;
case ISAKMP_NEXT_NONCE:
printf("n%c_b_hex=\"%s\"\n", (dir=='I')?'i':'r', hexdata);
break;
default:
printf("UNKNOWN PAYLOAD TYPE: %d\n", payload);
break;
}
free(hexdata);
} else { /* ISAKMP Header */
hexdata = hexstring((unsigned char *)ihdr->isa_icookie, 8);
printf("cky_i_hex=\"%s\"\n", hexdata);
free(hexdata);
hexdata = hexstring((unsigned char *)ihdr->isa_rcookie, 8);
printf("cky_r_hex=\"%s\"\n", hexdata);
free(hexdata);
}
}
/*
* add_psk_crack_payload -- Add an ISAKMP payload to PSK crack structure
*
* Inputs:
*
* cp Pointer to start of ISAKMP payload
* payload Numeric value of this payload type, 0 = ISAKMP header
* dir Direction: 'I' for initiator or 'R' for responder
*
* Returns:
*
* None
*
* This function trusts the length in the generic ISAKMP header, so
* could misbehave with corrupted packets.
*/
void
add_psk_crack_payload(unsigned char *cp, unsigned payload, int dir) {
struct isakmp_generic *hdr = (struct isakmp_generic *) cp;
struct isakmp_hdr *ihdr = (struct isakmp_hdr *) cp;
unsigned char *data;
size_t data_len;
if (payload) { /* Normal ISAKMP payload */
data_len = ntohs(hdr->isag_length) - sizeof(struct isakmp_generic);
data = Malloc(data_len);
memcpy(data, cp + sizeof(struct isakmp_generic), data_len);
switch (payload) {
case ISAKMP_NEXT_SA:
if (dir == 'I') {
psk_values.sai_b = data;
psk_values.sai_b_len = data_len;
}
break;
case ISAKMP_NEXT_KE:
if (dir == 'I') {
psk_values.g_xi = data;
psk_values.g_xi_len = data_len;
} else {
psk_values.g_xr = data;
psk_values.g_xr_len = data_len;
}
break;
case ISAKMP_NEXT_ID:
if (dir == 'R') {
psk_values.idir_b = data;
psk_values.idir_b_len = data_len;
}
break;
case ISAKMP_NEXT_HASH:
if (dir == 'R') {
psk_values.hash_r = data;
psk_values.hash_r_len = data_len;
}
break;
case ISAKMP_NEXT_NONCE:
if (dir == 'I') {
psk_values.ni_b = data;
psk_values.ni_b_len = data_len;
} else {
psk_values.nr_b = data;
psk_values.nr_b_len = data_len;
}
break;
default:
warn_msg("add_psk_crack_payload: UNKNOWN PAYLOAD TYPE: %d\n",
payload);
break;
}
} else { /* ISAKMP Header */
data_len=8; /* ISAKMP cookies are 8 bytes long */
data=Malloc(data_len);
memcpy(data, (unsigned char *)ihdr->isa_rcookie, 8);
psk_values.cky_r = data;
psk_values.cky_r_len = data_len;
data=Malloc(data_len);
memcpy(data, (unsigned char *)ihdr->isa_icookie, 8);
psk_values.cky_i = data;
psk_values.cky_i_len = data_len;
}
}
/*
* print_psk_crack_values -- Display the PSK crack values
*
* Inputs:
*
* psk_crack_file Name of PSK data output file, or NULL for stdout
*
* Returns:
*
* None
*
* This function prints the PSK crack values in the following format:
*
* g_xr:g_xi:cky_r:cky_i:sai_b:idir_b:ni_b:nr_b:hash_r
*/
void
print_psk_crack_values(const char *psk_crack_file) {
char *hexdata;
FILE *fp;
if (psk_crack_file[0]) {
if ((fp = fopen(psk_crack_file, "w")) == NULL) {
err_sys("ERROR: fopen");
}
} else {
fp = stdout;
printf("IKE PSK parameters (g_xr:g_xi:cky_r:cky_i:sai_b:idir_b:ni_b:nr_b:hash_r):\n");
}
hexdata=hexstring(psk_values.g_xr, psk_values.g_xr_len);
fprintf(fp, "%s:", hexdata);
free(hexdata);
hexdata=hexstring(psk_values.g_xi, psk_values.g_xi_len);
fprintf(fp, "%s:", hexdata);
free(hexdata);
hexdata=hexstring(psk_values.cky_r, psk_values.cky_r_len);
fprintf(fp, "%s:", hexdata);
free(hexdata);
hexdata=hexstring(psk_values.cky_i, psk_values.cky_i_len);
fprintf(fp, "%s:", hexdata);
free(hexdata);
hexdata=hexstring(psk_values.sai_b, psk_values.sai_b_len);
fprintf(fp, "%s:", hexdata);
free(hexdata);
hexdata=hexstring(psk_values.idir_b, psk_values.idir_b_len);
fprintf(fp, "%s:", hexdata);
free(hexdata);
hexdata=hexstring(psk_values.ni_b, psk_values.ni_b_len);
fprintf(fp, "%s:", hexdata);
free(hexdata);
hexdata=hexstring(psk_values.nr_b, psk_values.nr_b_len);
fprintf(fp, "%s:", hexdata);
free(hexdata);
hexdata=hexstring(psk_values.hash_r, psk_values.hash_r_len);
fprintf(fp, "%s\n", hexdata);
free(hexdata);
}
/*
* clone_payload -- Clone the ISAKMP payload
*
* Inputs:
*
* pkt_ptr Pointer to the payload to clone
* bytes_left Number of bytes remaining in the packet
*
* Returns:
*
* Pointer to the cloned payload or NULL if no payload.
*
* This function clones the ISAKMP payload starting at pkt_ptr, with
* a maximum size of bytes_left. It copies the payload to a newly
* allocated memory block to ensure that it is suitably aligned for
* those CPUs that have alignment restrictions.
*
* The return value points to Malloc'ed memory, which should be
* free'ed when it is no longer required.
*/
unsigned char *
clone_payload(const unsigned char *pkt_ptr, size_t bytes_left) {
struct isakmp_generic hdr;
unsigned char *clone_ptr;
size_t payload_len;
/*
* Ensure that there is sufficient data to fill the generic
* header.
*/
if (bytes_left < sizeof(struct isakmp_generic)) {
return NULL;
}
/*
* Fill in the generic header from the packet. We must do this
* by copying rather than overlaying because we cannot be sure
* that "pkt_ptr" is suitably aligned.
*/
memcpy(&hdr, pkt_ptr, sizeof(hdr));
/*
* Determine the length of the payload.
*/
payload_len = ntohs(hdr.isag_length);
if (payload_len > bytes_left)
payload_len = bytes_left;
/*
* Allocate memory and copy payload.
*/
clone_ptr = Malloc(payload_len);
memcpy(clone_ptr, pkt_ptr, payload_len);
return clone_ptr;
}
void
isakmp_use_rcsid(void) {
fprintf(stderr, "%s\n", rcsid); /* Use rcsid to stop compiler optimising away */
}
|