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
|
# Copyright (c) 2013-2024 by Ron Frederick <ronf@timeheart.net> and others.
#
# This program and the accompanying materials are made available under
# the terms of the Eclipse Public License v2.0 which accompanies this
# distribution and is available at:
#
# http://www.eclipse.org/legal/epl-2.0/
#
# This program may also be made available under the following secondary
# licenses when the conditions for such availability set forth in the
# Eclipse Public License v2.0 are satisfied:
#
# GNU General Public License, Version 2.0, or any later versions of
# that license
#
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
#
# Contributors:
# Ron Frederick - initial implementation, API, and documentation
"""SSH asymmetric encryption handlers"""
import asyncio
import binascii
import inspect
import os
import re
import time
from datetime import datetime
from hashlib import md5, sha1, sha256, sha384, sha512
from pathlib import Path, PurePath
from typing import Callable, Dict, List, Mapping, Optional, Sequence, Set
from typing import Tuple, Type, Union, cast
from typing_extensions import Protocol
from .crypto import ed25519_available, ed448_available
from .encryption import Encryption
from .sk import sk_available
try:
# pylint: disable=unused-import
from .crypto import X509Certificate
from .crypto import generate_x509_certificate, import_x509_certificate
_x509_available = True
except ImportError: # pragma: no cover
_x509_available = False
try:
import bcrypt
_bcrypt_available = hasattr(bcrypt, 'kdf')
except ImportError: # pragma: no cover
_bcrypt_available = False
from .asn1 import ASN1DecodeError, BitString, ObjectIdentifier
from .asn1 import der_encode, der_decode, der_decode_partial
from .crypto import CryptoKey, PyCAKey
from .encryption import get_encryption_params, get_encryption
from .misc import BytesOrStr, DefTuple, FilePath, IPNetwork
from .misc import ip_network, read_file, write_file, parse_time_interval
from .packet import NameList, String, UInt32, UInt64
from .packet import PacketDecodeError, SSHPacket
from .pbe import KeyEncryptionError, pkcs1_encrypt, pkcs8_encrypt
from .pbe import pkcs1_decrypt, pkcs8_decrypt
from .sk import SSH_SK_USER_PRESENCE_REQD, sk_get_resident
_Comment = Optional[BytesOrStr]
_CertPrincipals = Union[str, Sequence[str]]
_Time = Union[int, float, datetime, str]
_PubKeyAlgMap = Dict[bytes, Type['SSHKey']]
_CertAlgMap = Dict[bytes, Tuple[Optional[Type['SSHKey']],
Type['SSHCertificate']]]
_CertSigAlgMap = Dict[bytes, bytes]
_CertVersionMap = Dict[Tuple[bytes, int],
Tuple[bytes, Type['SSHOpenSSHCertificate']]]
_PEMMap = Dict[bytes, Type['SSHKey']]
_PKCS8OIDMap = Dict[ObjectIdentifier, Type['SSHKey']]
_SKAlgMap = Dict[int, Tuple[Type['SSHKey'], Tuple[object, ...]]]
_OpenSSHCertOptions = Dict[str, object]
_OpenSSHCertParams = Tuple[object, int, int, bytes, bytes,
int, int, bytes, bytes]
_OpenSSHCertEncoders = Sequence[Tuple[str, Callable[[object], bytes]]]
_OpenSSHCertDecoders = Dict[bytes, Callable[[SSHPacket], object]]
X509CertPurposes = Union[None, str, Sequence[str]]
_IdentityArg = Union[bytes, FilePath, 'SSHKey', 'SSHCertificate']
IdentityListArg = Union[_IdentityArg, Sequence[_IdentityArg]]
_KeyArg = Union[bytes, FilePath, 'SSHKey']
KeyListArg = Union[FilePath, Sequence[_KeyArg]]
_CertArg = Union[bytes, FilePath, 'SSHCertificate']
CertListArg = Union[_CertArg, Sequence[_CertArg]]
_KeyPairArg = Union['SSHKeyPair', _KeyArg, Tuple[_KeyArg, _CertArg]]
KeyPairListArg = Union[_KeyPairArg, Sequence[_KeyPairArg]]
# Default file names in .ssh directory to read private keys from
_DEFAULT_KEY_FILES = (
('id_ed25519_sk', ed25519_available and sk_available),
('id_ecdsa_sk', sk_available),
('id_ed448', ed448_available),
('id_ed25519', ed25519_available),
('id_ecdsa', True),
('id_rsa', True),
('id_dsa', True)
)
# Default directories and file names to read host keys from
_DEFAULT_HOST_KEY_DIRS = ('/opt/local/etc', '/opt/local/etc/ssh',
'/usr/local/etc', '/usr/local/etc/ssh',
'/etc', '/etc/ssh')
_DEFAULT_HOST_KEY_FILES = ('ssh_host_ed448_key', 'ssh_host_ed25519_key',
'ssh_host_ecdsa_key', 'ssh_host_rsa_key',
'ssh_host_dsa_key')
_hashes = {'md5': md5, 'sha1': sha1, 'sha256': sha256,
'sha384': sha384, 'sha512': sha512}
_public_key_algs: List[bytes] = []
_default_public_key_algs: List[bytes] = []
_certificate_algs: List[bytes] = []
_default_certificate_algs: List[bytes] = []
_x509_certificate_algs: List[bytes] = []
_default_x509_certificate_algs: List[bytes] = []
_public_key_alg_map: _PubKeyAlgMap = {}
_certificate_alg_map: _CertAlgMap = {}
_certificate_sig_alg_map: _CertSigAlgMap = {}
_certificate_version_map: _CertVersionMap = {}
_pem_map: _PEMMap = {}
_pkcs8_oid_map: _PKCS8OIDMap = {}
_sk_alg_map: _SKAlgMap = {}
_abs_date_pattern = re.compile(r'\d{8}')
_abs_time_pattern = re.compile(r'\d{14}')
_subject_pattern = re.compile(r'(?:Distinguished[ -_]?Name|Subject|DN)[=:]?\s?',
re.IGNORECASE)
# SSH certificate types
CERT_TYPE_USER = 1
CERT_TYPE_HOST = 2
# Flag to omit second argument in alg_params
OMIT = object()
_OPENSSH_KEY_V1 = b'openssh-key-v1\0'
_OPENSSH_SALT_LEN = 16
_OPENSSH_WRAP_LEN = 70
def _parse_time(t: _Time) -> int:
"""Parse a time value"""
if isinstance(t, int):
return t
elif isinstance(t, float):
return int(t)
elif isinstance(t, datetime):
return int(t.timestamp())
elif isinstance(t, str):
if t == 'now':
return int(time.time())
match = _abs_date_pattern.fullmatch(t)
if match:
return int(datetime.strptime(t, '%Y%m%d').timestamp())
match = _abs_time_pattern.fullmatch(t)
if match:
return int(datetime.strptime(t, '%Y%m%d%H%M%S').timestamp())
try:
return int(time.time() + parse_time_interval(t))
except ValueError:
pass
raise ValueError('Unrecognized time value')
def _wrap_base64(data: bytes, wrap: int = 64) -> bytes:
"""Break a Base64 value into multiple lines."""
data = binascii.b2a_base64(data)[:-1]
return b'\n'.join(data[i:i+wrap]
for i in range(0, len(data), wrap)) + b'\n'
class KeyGenerationError(ValueError):
"""Key generation error
This exception is raised by :func:`generate_private_key`,
:meth:`generate_user_certificate() <SSHKey.generate_user_certificate>`
or :meth:`generate_host_certificate()
<SSHKey.generate_host_certificate>` when the requested parameters are
unsupported.
"""
class KeyImportError(ValueError):
"""Key import error
This exception is raised by key import functions when the
data provided cannot be imported as a valid key.
"""
class KeyExportError(ValueError):
"""Key export error
This exception is raised by key export functions when the
requested format is unknown or encryption is requested for a
format which doesn't support it.
"""
class SigningKey(Protocol):
"""Protocol for signing a block of data"""
def sign(self, data: bytes) -> bytes:
"""Sign a block of data with a private key"""
class VerifyingKey(Protocol):
"""Protocol for verifying a signature on a block of data"""
def verify(self, data: bytes, sig: bytes) -> bool:
"""Verify a signature on a block of data with a public key"""
class SSHKey:
"""Parent class which holds an asymmetric encryption key"""
algorithm: bytes = b''
sig_algorithms: Sequence[bytes] = ()
cert_algorithms: Sequence[bytes] = ()
x509_algorithms: Sequence[bytes] = ()
all_sig_algorithms: Set[bytes] = set()
default_x509_hash: str = ''
pem_name: bytes = b''
pkcs8_oid: Optional[ObjectIdentifier] = None
use_executor: bool = False
use_webauthn: bool = False
def __init__(self, key: Optional[CryptoKey] = None):
self._key = key
self._comment: Optional[bytes] = None
self._filename: Optional[bytes] = None
self._touch_required = False
@classmethod
def generate(cls, algorithm: bytes, **kwargs) -> 'SSHKey':
"""Generate a new SSH private key"""
raise NotImplementedError
@classmethod
def make_private(cls, key_params: object) -> 'SSHKey':
"""Construct a private key"""
raise NotImplementedError
@classmethod
def make_public(cls, key_params: object) -> 'SSHKey':
"""Construct a public key"""
raise NotImplementedError
@classmethod
def decode_pkcs1_private(cls, key_data: object) -> object:
"""Decode a PKCS#1 format private key"""
@classmethod
def decode_pkcs1_public(cls, key_data: object) -> object:
"""Decode a PKCS#1 format public key"""
@classmethod
def decode_pkcs8_private(cls, alg_params: object, data: bytes) -> object:
"""Decode a PKCS#8 format private key"""
@classmethod
def decode_pkcs8_public(cls, alg_params: object, data: bytes) -> object:
"""Decode a PKCS#8 format public key"""
@classmethod
def decode_ssh_private(cls, packet: SSHPacket) -> object:
"""Decode an SSH format private key"""
@classmethod
def decode_ssh_public(cls, packet: SSHPacket) -> object:
"""Decode an SSH format public key"""
@property
def private_data(self) -> bytes:
"""Return private key data in OpenSSH binary format"""
return String(self.algorithm) + self.encode_ssh_private()
@property
def public_data(self) -> bytes:
"""Return public key data in OpenSSH binary format"""
return String(self.algorithm) + self.encode_ssh_public()
@property
def pyca_key(self) -> PyCAKey:
"""Return PyCA key for use in X.509 module"""
assert self._key is not None
return self._key.pyca_key
def _generate_certificate(self, key: 'SSHKey', version: int, serial: int,
cert_type: int, key_id: str,
principals: _CertPrincipals,
valid_after: _Time, valid_before: _Time,
cert_options: _OpenSSHCertOptions,
sig_alg_name: DefTuple[str],
comment: DefTuple[_Comment]) -> \
'SSHOpenSSHCertificate':
"""Generate a new SSH certificate"""
if isinstance(principals, str):
principals = [p.strip() for p in principals.split(',')]
else:
principals = list(principals)
valid_after = _parse_time(valid_after)
valid_before = _parse_time(valid_before)
if valid_before <= valid_after:
raise ValueError('Valid before time must be later than '
'valid after time')
if sig_alg_name == ():
sig_alg = self.sig_algorithms[0]
else:
sig_alg = cast(str, sig_alg_name).encode()
if comment == ():
comment = key.get_comment_bytes()
comment: _Comment
try:
algorithm, cert_handler = _certificate_version_map[key.algorithm,
version]
except KeyError:
raise KeyGenerationError('Unknown certificate version') from None
return cert_handler.generate(self, algorithm, key, serial, cert_type,
key_id, principals, valid_after,
valid_before, cert_options,
sig_alg, comment)
def _generate_x509_certificate(self, key: 'SSHKey', subject: str,
issuer: Optional[str],
serial: Optional[int],
valid_after: _Time, valid_before: _Time,
ca: bool, ca_path_len: Optional[int],
purposes: X509CertPurposes,
user_principals: _CertPrincipals,
host_principals: _CertPrincipals,
hash_name: DefTuple[str],
comment: DefTuple[_Comment]) -> \
'SSHX509Certificate':
"""Generate a new X.509 certificate"""
if not _x509_available: # pragma: no cover
raise KeyGenerationError('X.509 certificate generation '
'requires PyOpenSSL')
if not self.x509_algorithms:
raise KeyGenerationError('X.509 certificate generation not '
'supported for ' + self.get_algorithm() +
' keys')
valid_after = _parse_time(valid_after)
valid_before = _parse_time(valid_before)
if valid_before <= valid_after:
raise ValueError('Valid before time must be later than '
'valid after time')
if hash_name == ():
hash_name = key.default_x509_hash
if comment == ():
comment = key.get_comment_bytes()
hash_name: str
comment: _Comment
return SSHX509Certificate.generate(self, key, subject, issuer,
serial, valid_after, valid_before,
ca, ca_path_len, purposes,
user_principals, host_principals,
hash_name, comment)
def get_algorithm(self) -> str:
"""Return the algorithm associated with this key"""
return self.algorithm.decode('ascii')
def has_comment(self) -> bool:
"""Return whether a comment is set for this key
:returns: `bool`
"""
return bool(self._comment)
def get_comment_bytes(self) -> Optional[bytes]:
"""Return the comment associated with this key as a byte string
:returns: `bytes` or `None`
"""
return self._comment or self._filename
def get_comment(self, encoding: str = 'utf-8',
errors: str = 'strict') -> Optional[str]:
"""Return the comment associated with this key as a Unicode string
:param encoding:
The encoding to use to decode the comment as a Unicode
string, defaulting to UTF-8
:param errors:
The error handling scheme to use for Unicode decode errors
:type encoding: `str`
:type errors: `str`
:returns: `str` or `None`
:raises: :exc:`UnicodeDecodeError` if the comment cannot be
decoded using the specified encoding
"""
comment = self.get_comment_bytes()
return comment.decode(encoding, errors) if comment else None
def set_comment(self, comment: _Comment, encoding: str = 'utf-8',
errors: str = 'strict') -> None:
"""Set the comment associated with this key
:param comment:
The new comment to associate with this key
:param encoding:
The Unicode encoding to use to encode the comment,
defaulting to UTF-8
:param errors:
The error handling scheme to use for Unicode encode errors
:type comment: `str`, `bytes`, or `None`
:type encoding: `str`
:type errors: `str`
:raises: :exc:`UnicodeEncodeError` if the comment cannot be
encoded using the specified encoding
"""
if isinstance(comment, str):
comment = comment.encode(encoding, errors)
self._comment = comment or None
def get_filename(self) -> Optional[bytes]:
"""Return the filename associated with this key
:returns: `bytes` or `None`
"""
return self._filename
def set_filename(self, filename: Union[None, bytes, FilePath]) -> None:
"""Set the filename associated with this key
:param filename:
The new filename to associate with this key
:type filename: `PurePath`, `str`, `bytes`, or `None`
"""
if isinstance(filename, PurePath):
filename = str(filename)
if isinstance(filename, str):
filename = filename.encode('utf-8')
self._filename = filename or None
def get_fingerprint(self, hash_name: str = 'sha256') -> str:
"""Get the fingerprint of this key
Available hashes include:
md5, sha1, sha256, sha384, sha512
:param hash_name: (optional)
The hash algorithm to use to construct the fingerprint.
:type hash_name: `str`
:returns: `str`
:raises: :exc:`ValueError` if the hash name is invalid
"""
try:
hash_alg = _hashes[hash_name]
except KeyError:
raise ValueError('Unknown hash algorithm') from None
h = hash_alg(self.public_data)
if hash_name == 'md5':
fp = h.hexdigest()
fp_text = ':'.join(fp[i:i+2] for i in range(0, len(fp), 2))
else:
fpb = h.digest()
fp_text = binascii.b2a_base64(fpb).decode('ascii')[:-1].strip('=')
return hash_name.upper() + ':' + fp_text
def set_touch_required(self, touch_required: bool) -> None:
"""Set whether touch is required when using a security key"""
self._touch_required = touch_required
def sign_raw(self, data: bytes, hash_name: str) -> bytes:
"""Return a raw signature of the specified data"""
assert self._key is not None
return self._key.sign(data, hash_name)
def sign_ssh(self, data: bytes, sig_algorithm: bytes) -> bytes:
"""Abstract method to compute an SSH-encoded signature"""
raise NotImplementedError
def verify_ssh(self, data: bytes, sig_algorithm: bytes,
packet: SSHPacket) -> bool:
"""Abstract method to verify an SSH-encoded signature"""
raise NotImplementedError
def sign(self, data: bytes, sig_algorithm: bytes) -> bytes:
"""Return an SSH-encoded signature of the specified data"""
if sig_algorithm.startswith(b'x509v3-'):
sig_algorithm = sig_algorithm[7:]
if sig_algorithm not in self.all_sig_algorithms:
raise ValueError('Unrecognized signature algorithm')
return b''.join((String(sig_algorithm),
self.sign_ssh(data, sig_algorithm)))
def verify(self, data: bytes, sig: bytes) -> bool:
"""Verify an SSH signature of the specified data using this key"""
try:
packet = SSHPacket(sig)
sig_algorithm = packet.get_string()
if sig_algorithm not in self.all_sig_algorithms:
return False
return self.verify_ssh(data, sig_algorithm, packet)
except PacketDecodeError:
return False
def encode_pkcs1_private(self) -> object:
"""Export parameters associated with a PKCS#1 private key"""
# pylint: disable=no-self-use
raise KeyExportError('PKCS#1 private key export not supported')
def encode_pkcs1_public(self) -> object:
"""Export parameters associated with a PKCS#1 public key"""
# pylint: disable=no-self-use
raise KeyExportError('PKCS#1 public key export not supported')
def encode_pkcs8_private(self) -> Tuple[object, object]:
"""Export parameters associated with a PKCS#8 private key"""
# pylint: disable=no-self-use
raise KeyExportError('PKCS#8 private key export not supported')
def encode_pkcs8_public(self) -> Tuple[object, object]:
"""Export parameters associated with a PKCS#8 public key"""
# pylint: disable=no-self-use
raise KeyExportError('PKCS#8 public key export not supported')
def encode_ssh_private(self) -> bytes:
"""Export parameters associated with an OpenSSH private key"""
# pylint: disable=no-self-use
raise KeyExportError('OpenSSH private key export not supported')
def encode_ssh_public(self) -> bytes:
"""Export parameters associated with an OpenSSH public key"""
# pylint: disable=no-self-use
raise KeyExportError('OpenSSH public key export not supported')
def encode_agent_cert_private(self) -> bytes:
"""Encode certificate private key data for agent"""
raise NotImplementedError
def convert_to_public(self) -> 'SSHKey':
"""Return public key corresponding to this key
This method converts an :class:`SSHKey` object which contains
a private key into one which contains only the corresponding
public key. If it is called on something which is already
a public key, it has no effect.
"""
result = decode_ssh_public_key(self.public_data)
result.set_comment(self._comment)
result.set_filename(self._filename)
return result
def generate_user_certificate(
self, user_key: 'SSHKey', key_id: str, version: int = 1,
serial: int = 0, principals: _CertPrincipals = (),
valid_after: _Time = 0, valid_before: _Time = 0xffffffffffffffff,
force_command: Optional[str] = None,
source_address: Optional[Sequence[str]] = None,
permit_x11_forwarding: bool = True,
permit_agent_forwarding: bool = True,
permit_port_forwarding: bool = True, permit_pty: bool = True,
permit_user_rc: bool = True, touch_required: bool = True,
sig_alg: DefTuple[str] = (),
comment: DefTuple[_Comment] = ()) -> 'SSHOpenSSHCertificate':
"""Generate a new SSH user certificate
This method returns an SSH user certificate with the requested
attributes signed by this private key.
:param user_key:
The user's public key.
:param key_id:
The key identifier associated with this certificate.
:param version: (optional)
The version of certificate to create, defaulting to 1.
:param serial: (optional)
The serial number of the certificate, defaulting to 0.
:param principals: (optional)
The user names this certificate is valid for. By default,
it can be used with any user name.
:param valid_after: (optional)
The earliest time the certificate is valid for, defaulting to
no restriction on when the certificate starts being valid.
See :ref:`SpecifyingTimeValues` for allowed time specifications.
:param valid_before: (optional)
The latest time the certificate is valid for, defaulting to
no restriction on when the certificate stops being valid.
See :ref:`SpecifyingTimeValues` for allowed time specifications.
:param force_command: (optional)
The command (if any) to force a session to run when this
certificate is used.
:param source_address: (optional)
A list of source addresses and networks for which the
certificate is valid, defaulting to all addresses.
:param permit_x11_forwarding: (optional)
Whether or not to allow this user to use X11 forwarding,
defaulting to `True`.
:param permit_agent_forwarding: (optional)
Whether or not to allow this user to use agent forwarding,
defaulting to `True`.
:param permit_port_forwarding: (optional)
Whether or not to allow this user to use port forwarding,
defaulting to `True`.
:param permit_pty: (optional)
Whether or not to allow this user to allocate a
pseudo-terminal, defaulting to `True`.
:param permit_user_rc: (optional)
Whether or not to run the user rc file when this certificate
is used, defaulting to `True`.
:param touch_required: (optional)
Whether or not to require the user to touch the security key
when authenticating with it, defaulting to `True`.
:param sig_alg: (optional)
The algorithm to use when signing the new certificate.
:param comment:
The comment to associate with this certificate. By default,
the comment will be set to the comment currently set on
user_key.
:type user_key: :class:`SSHKey`
:type key_id: `str`
:type version: `int`
:type serial: `int`
:type principals: `str` or `list` of `str`
:type force_command: `str` or `None`
:type source_address: list of ip_address and ip_network values
:type permit_x11_forwarding: `bool`
:type permit_agent_forwarding: `bool`
:type permit_port_forwarding: `bool`
:type permit_pty: `bool`
:type permit_user_rc: `bool`
:type touch_required: `bool`
:type sig_alg: `str`
:type comment: `str`, `bytes`, or `None`
:returns: :class:`SSHCertificate`
:raises: | :exc:`ValueError` if the validity times are invalid
| :exc:`KeyGenerationError` if the requested certificate
parameters are unsupported
"""
cert_options: _OpenSSHCertOptions = {}
if force_command:
cert_options['force-command'] = force_command
if source_address:
cert_options['source-address'] = [ip_network(addr)
for addr in source_address]
if permit_x11_forwarding:
cert_options['permit-X11-forwarding'] = True
if permit_agent_forwarding:
cert_options['permit-agent-forwarding'] = True
if permit_port_forwarding:
cert_options['permit-port-forwarding'] = True
if permit_pty:
cert_options['permit-pty'] = True
if permit_user_rc:
cert_options['permit-user-rc'] = True
if not touch_required:
cert_options['no-touch-required'] = True
return self._generate_certificate(user_key, version, serial,
CERT_TYPE_USER, key_id,
principals, valid_after,
valid_before, cert_options,
sig_alg, comment)
def generate_host_certificate(self, host_key: 'SSHKey', key_id: str,
version: int = 1, serial: int = 0,
principals: _CertPrincipals = (),
valid_after: _Time = 0,
valid_before: _Time = 0xffffffffffffffff,
sig_alg: DefTuple[str] = (),
comment: DefTuple[_Comment] = ()) -> \
'SSHOpenSSHCertificate':
"""Generate a new SSH host certificate
This method returns an SSH host certificate with the requested
attributes signed by this private key.
:param host_key:
The host's public key.
:param key_id:
The key identifier associated with this certificate.
:param version: (optional)
The version of certificate to create, defaulting to 1.
:param serial: (optional)
The serial number of the certificate, defaulting to 0.
:param principals: (optional)
The host names this certificate is valid for. By default,
it can be used with any host name.
:param valid_after: (optional)
The earliest time the certificate is valid for, defaulting to
no restriction on when the certificate starts being valid.
See :ref:`SpecifyingTimeValues` for allowed time specifications.
:param valid_before: (optional)
The latest time the certificate is valid for, defaulting to
no restriction on when the certificate stops being valid.
See :ref:`SpecifyingTimeValues` for allowed time specifications.
:param sig_alg: (optional)
The algorithm to use when signing the new certificate.
:param comment:
The comment to associate with this certificate. By default,
the comment will be set to the comment currently set on
host_key.
:type host_key: :class:`SSHKey`
:type key_id: `str`
:type version: `int`
:type serial: `int`
:type principals: `str` or `list` of `str`
:type sig_alg: `str`
:type comment: `str`, `bytes`, or `None`
:returns: :class:`SSHCertificate`
:raises: | :exc:`ValueError` if the validity times are invalid
| :exc:`KeyGenerationError` if the requested certificate
parameters are unsupported
"""
if comment == ():
comment = host_key.get_comment_bytes()
return self._generate_certificate(host_key, version, serial,
CERT_TYPE_HOST, key_id,
principals, valid_after,
valid_before, {}, sig_alg, comment)
def generate_x509_user_certificate(
self, user_key: 'SSHKey', subject: str,
issuer: Optional[str] = None, serial: Optional[int] = None,
principals: _CertPrincipals = (), valid_after: _Time = 0,
valid_before: _Time = 0xffffffffffffffff,
purposes: X509CertPurposes = 'secureShellClient',
hash_alg: DefTuple[str] = (),
comment: DefTuple[_Comment] = ()) -> 'SSHX509Certificate':
"""Generate a new X.509 user certificate
This method returns an X.509 user certificate with the requested
attributes signed by this private key.
:param user_key:
The user's public key.
:param subject:
The subject name in the certificate, expresed as a
comma-separated list of X.509 `name=value` pairs.
:param issuer: (optional)
The issuer name in the certificate, expresed as a
comma-separated list of X.509 `name=value` pairs. If
not specified, the subject name will be used, creating
a self-signed certificate.
:param serial: (optional)
The serial number of the certificate, defaulting to a random
64-bit value.
:param principals: (optional)
The user names this certificate is valid for. By default,
it can be used with any user name.
:param valid_after: (optional)
The earliest time the certificate is valid for, defaulting to
no restriction on when the certificate starts being valid.
See :ref:`SpecifyingTimeValues` for allowed time specifications.
:param valid_before: (optional)
The latest time the certificate is valid for, defaulting to
no restriction on when the certificate stops being valid.
See :ref:`SpecifyingTimeValues` for allowed time specifications.
:param purposes: (optional)
The allowed purposes for this certificate or `None` to
not restrict the certificate's purpose, defaulting to
'secureShellClient'
:param hash_alg: (optional)
The hash algorithm to use when signing the new certificate,
defaulting to SHA256.
:param comment: (optional)
The comment to associate with this certificate. By default,
the comment will be set to the comment currently set on
user_key.
:type user_key: :class:`SSHKey`
:type subject: `str`
:type issuer: `str`
:type serial: `int`
:type principals: `str` or `list` of `str`
:type purposes: `str`, `list` of `str`, or `None`
:type hash_alg: `str`
:type comment: `str`, `bytes`, or `None`
:returns: :class:`SSHCertificate`
:raises: | :exc:`ValueError` if the validity times are invalid
| :exc:`KeyGenerationError` if the requested certificate
parameters are unsupported
"""
return self._generate_x509_certificate(user_key, subject, issuer,
serial, valid_after,
valid_before, False, None,
purposes, principals, (),
hash_alg, comment)
def generate_x509_host_certificate(
self, host_key: 'SSHKey', subject: str,
issuer: Optional[str] = None, serial: Optional[int] = None,
principals: _CertPrincipals = (), valid_after: _Time = 0,
valid_before: _Time = 0xffffffffffffffff,
purposes: X509CertPurposes = 'secureShellServer',
hash_alg: DefTuple[str] = (),
comment: DefTuple[_Comment] = ()) -> 'SSHX509Certificate':
"""Generate a new X.509 host certificate
This method returns an X.509 host certificate with the requested
attributes signed by this private key.
:param host_key:
The host's public key.
:param subject:
The subject name in the certificate, expresed as a
comma-separated list of X.509 `name=value` pairs.
:param issuer: (optional)
The issuer name in the certificate, expresed as a
comma-separated list of X.509 `name=value` pairs. If
not specified, the subject name will be used, creating
a self-signed certificate.
:param serial: (optional)
The serial number of the certificate, defaulting to a random
64-bit value.
:param principals: (optional)
The host names this certificate is valid for. By default,
it can be used with any host name.
:param valid_after: (optional)
The earliest time the certificate is valid for, defaulting to
no restriction on when the certificate starts being valid.
See :ref:`SpecifyingTimeValues` for allowed time specifications.
:param valid_before: (optional)
The latest time the certificate is valid for, defaulting to
no restriction on when the certificate stops being valid.
See :ref:`SpecifyingTimeValues` for allowed time specifications.
:param purposes: (optional)
The allowed purposes for this certificate or `None` to
not restrict the certificate's purpose, defaulting to
'secureShellServer'
:param hash_alg: (optional)
The hash algorithm to use when signing the new certificate,
defaulting to SHA256.
:param comment: (optional)
The comment to associate with this certificate. By default,
the comment will be set to the comment currently set on
host_key.
:type host_key: :class:`SSHKey`
:type subject: `str`
:type issuer: `str`
:type serial: `int`
:type principals: `str` or `list` of `str`
:type purposes: `str`, `list` of `str`, or `None`
:type hash_alg: `str`
:type comment: `str`, `bytes`, or `None`
:returns: :class:`SSHCertificate`
:raises: | :exc:`ValueError` if the validity times are invalid
| :exc:`KeyGenerationError` if the requested certificate
parameters are unsupported
"""
return self._generate_x509_certificate(host_key, subject, issuer,
serial, valid_after,
valid_before, False, None,
purposes, (), principals,
hash_alg, comment)
def generate_x509_ca_certificate(self, ca_key: 'SSHKey', subject: str,
issuer: Optional[str] = None,
serial: Optional[int] = None,
valid_after: _Time = 0,
valid_before: _Time = 0xffffffffffffffff,
ca_path_len: Optional[int] = None,
hash_alg: DefTuple[str] = (),
comment: DefTuple[_Comment] = ()) -> \
'SSHX509Certificate':
"""Generate a new X.509 CA certificate
This method returns an X.509 CA certificate with the requested
attributes signed by this private key.
:param ca_key:
The new CA's public key.
:param subject:
The subject name in the certificate, expresed as a
comma-separated list of X.509 `name=value` pairs.
:param issuer: (optional)
The issuer name in the certificate, expresed as a
comma-separated list of X.509 `name=value` pairs. If
not specified, the subject name will be used, creating
a self-signed certificate.
:param serial: (optional)
The serial number of the certificate, defaulting to a random
64-bit value.
:param valid_after: (optional)
The earliest time the certificate is valid for, defaulting to
no restriction on when the certificate starts being valid.
See :ref:`SpecifyingTimeValues` for allowed time specifications.
:param valid_before: (optional)
The latest time the certificate is valid for, defaulting to
no restriction on when the certificate stops being valid.
See :ref:`SpecifyingTimeValues` for allowed time specifications.
:param ca_path_len: (optional)
The maximum number of levels of intermediate CAs allowed
below this new CA or `None` to not enforce a limit,
defaulting to no limit.
:param hash_alg: (optional)
The hash algorithm to use when signing the new certificate,
defaulting to SHA256.
:param comment: (optional)
The comment to associate with this certificate. By default,
the comment will be set to the comment currently set on
ca_key.
:type ca_key: :class:`SSHKey`
:type subject: `str`
:type issuer: `str`
:type serial: `int`
:type ca_path_len: `int` or `None`
:type hash_alg: `str`
:type comment: `str`, `bytes`, or `None`
:returns: :class:`SSHCertificate`
:raises: | :exc:`ValueError` if the validity times are invalid
| :exc:`KeyGenerationError` if the requested certificate
parameters are unsupported
"""
return self._generate_x509_certificate(ca_key, subject, issuer,
serial, valid_after,
valid_before, True,
ca_path_len, None, (), (),
hash_alg, comment)
def export_private_key(self, format_name: str = 'openssh',
passphrase: Optional[BytesOrStr] = None,
cipher_name: str = 'aes256-cbc',
hash_name: str = 'sha256',
pbe_version: int = 2, rounds: int = 128,
ignore_few_rounds: bool = False) -> bytes:
"""Export a private key in the requested format
This method returns this object's private key encoded in the
requested format. If a passphrase is specified, the key will
be exported in encrypted form.
Available formats include:
pkcs1-der, pkcs1-pem, pkcs8-der, pkcs8-pem, openssh
By default, openssh format will be used.
Encryption is supported in pkcs1-pem, pkcs8-der, pkcs8-pem,
and openssh formats. For pkcs1-pem, only the cipher can be
specified. For pkcs8-der and pkcs-8, cipher, hash and PBE
version can be specified. For openssh, cipher and rounds
can be specified.
Available ciphers for pkcs1-pem are:
aes128-cbc, aes192-cbc, aes256-cbc, des-cbc, des3-cbc
Available ciphers for pkcs8-der and pkcs8-pem are:
aes128-cbc, aes192-cbc, aes256-cbc, blowfish-cbc,
cast128-cbc, des-cbc, des2-cbc, des3-cbc, rc4-40, rc4-128
Available ciphers for openssh format include the following
:ref:`encryption algorithms <EncryptionAlgs>`.
Available hashes include:
md5, sha1, sha256, sha384, sha512
Available PBE versions include 1 for PBES1 and 2 for PBES2.
Not all combinations of cipher, hash, and version are supported.
The default cipher is aes256. In the pkcs8 formats, the default
hash is sha256 and default version is PBES2.
In openssh format, the default number of rounds is 128.
.. note:: The openssh format uses bcrypt for encryption, but
unlike the traditional bcrypt cost factor used in
password hashing which scales logarithmically, the
encryption strength here scales linearly with the
rounds value. Since the cipher is rekeyed 64 times
per round, the default rounds value of 128 corresponds
to 8192 total iterations, which is the equivalent of
a bcrypt cost factor of 13.
:param format_name: (optional)
The format to export the key in.
:param passphrase: (optional)
A passphrase to encrypt the private key with.
:param cipher_name: (optional)
The cipher to use for private key encryption.
:param hash_name: (optional)
The hash to use for private key encryption.
:param pbe_version: (optional)
The PBE version to use for private key encryption.
:param rounds: (optional)
The number of KDF rounds to apply to the passphrase.
:type format_name: `str`
:type passphrase: `str` or `bytes`
:type cipher_name: `str`
:type hash_name: `str`
:type pbe_version: `int`
:type rounds: `int`
:returns: `bytes` representing the exported private key
"""
if format_name in ('pkcs1-der', 'pkcs1-pem'):
data = der_encode(self.encode_pkcs1_private())
if passphrase is not None:
if format_name == 'pkcs1-der':
raise KeyExportError('PKCS#1 DER format does not support '
'private key encryption')
alg, iv, data = pkcs1_encrypt(data, cipher_name, passphrase)
headers = (b'Proc-Type: 4,ENCRYPTED\n' +
b'DEK-Info: ' + alg + b',' +
binascii.b2a_hex(iv).upper() + b'\n\n')
else:
headers = b''
if format_name == 'pkcs1-pem':
keytype = self.pem_name + b' PRIVATE KEY'
data = (b'-----BEGIN ' + keytype + b'-----\n' +
headers + _wrap_base64(data) +
b'-----END ' + keytype + b'-----\n')
return data
elif format_name in ('pkcs8-der', 'pkcs8-pem'):
alg_params, pkcs8_data = self.encode_pkcs8_private()
if alg_params is OMIT:
data = der_encode((0, (self.pkcs8_oid,), pkcs8_data))
else:
data = der_encode((0, (self.pkcs8_oid, alg_params), pkcs8_data))
if passphrase is not None:
data = pkcs8_encrypt(data, cipher_name, hash_name,
pbe_version, passphrase)
if format_name == 'pkcs8-pem':
if passphrase is not None:
keytype = b'ENCRYPTED PRIVATE KEY'
else:
keytype = b'PRIVATE KEY'
data = (b'-----BEGIN ' + keytype + b'-----\n' +
_wrap_base64(data) +
b'-----END ' + keytype + b'-----\n')
return data
elif format_name == 'openssh':
check = os.urandom(4)
nkeys = 1
data = b''.join((check, check, self.private_data,
String(self._comment or b'')))
cipher: Optional[Encryption]
if passphrase is not None:
try:
alg = cipher_name.encode('ascii')
key_size, iv_size, block_size, _, _, _ = \
get_encryption_params(alg)
except (KeyError, UnicodeEncodeError):
raise KeyEncryptionError('Unknown cipher: ' +
cipher_name) from None
if not _bcrypt_available: # pragma: no cover
raise KeyExportError('OpenSSH private key encryption '
'requires bcrypt with KDF support')
kdf = b'bcrypt'
salt = os.urandom(_OPENSSH_SALT_LEN)
kdf_data = b''.join((String(salt), UInt32(rounds)))
if isinstance(passphrase, str):
passphrase = passphrase.encode('utf-8')
key = bcrypt.kdf(passphrase, salt, key_size + iv_size,
rounds, ignore_few_rounds)
cipher = get_encryption(alg, key[:key_size], key[key_size:])
block_size = max(block_size, 8)
else:
cipher = None
alg = b'none'
kdf = b'none'
kdf_data = b''
block_size = 8
mac = b''
pad = len(data) % block_size
if pad: # pragma: no branch
data = data + bytes(range(1, block_size + 1 - pad))
if cipher:
data, mac = cipher.encrypt_packet(0, b'', data)
else:
mac = b''
data = b''.join((_OPENSSH_KEY_V1, String(alg), String(kdf),
String(kdf_data), UInt32(nkeys),
String(self.public_data), String(data), mac))
return (b'-----BEGIN OPENSSH PRIVATE KEY-----\n' +
_wrap_base64(data, _OPENSSH_WRAP_LEN) +
b'-----END OPENSSH PRIVATE KEY-----\n')
else:
raise KeyExportError('Unknown export format')
def export_public_key(self, format_name: str = 'openssh') -> bytes:
"""Export a public key in the requested format
This method returns this object's public key encoded in the
requested format. Available formats include:
pkcs1-der, pkcs1-pem, pkcs8-der, pkcs8-pem, openssh, rfc4716
By default, openssh format will be used.
:param format_name: (optional)
The format to export the key in.
:type format_name: `str`
:returns: `bytes` representing the exported public key
"""
if format_name in ('pkcs1-der', 'pkcs1-pem'):
data = der_encode(self.encode_pkcs1_public())
if format_name == 'pkcs1-pem':
keytype = self.pem_name + b' PUBLIC KEY'
data = (b'-----BEGIN ' + keytype + b'-----\n' +
_wrap_base64(data) +
b'-----END ' + keytype + b'-----\n')
return data
elif format_name in ('pkcs8-der', 'pkcs8-pem'):
alg_params, pkcs8_data = self.encode_pkcs8_public()
pkcs8_data = BitString(pkcs8_data)
if alg_params is OMIT:
data = der_encode(((self.pkcs8_oid,), pkcs8_data))
else:
data = der_encode(((self.pkcs8_oid, alg_params), pkcs8_data))
if format_name == 'pkcs8-pem':
data = (b'-----BEGIN PUBLIC KEY-----\n' +
_wrap_base64(data) +
b'-----END PUBLIC KEY-----\n')
return data
elif format_name == 'openssh':
if self._comment:
comment = b' ' + self._comment
else:
comment = b''
return (self.algorithm + b' ' +
binascii.b2a_base64(self.public_data)[:-1] +
comment + b'\n')
elif format_name == 'rfc4716':
if self._comment:
comment = (b'Comment: "' + self._comment + b'"\n')
else:
comment = b''
return (b'---- BEGIN SSH2 PUBLIC KEY ----\n' +
comment + _wrap_base64(self.public_data) +
b'---- END SSH2 PUBLIC KEY ----\n')
else:
raise KeyExportError('Unknown export format')
def write_private_key(self, filename: FilePath, *args, **kwargs) -> None:
"""Write a private key to a file in the requested format
This method is a simple wrapper around :meth:`export_private_key`
which writes the exported key data to a file.
:param filename:
The filename to write the private key to.
:param \\*args,\\ \\*\\*kwargs:
Additional arguments to pass through to
:meth:`export_private_key`.
:type filename: :class:`PurePath <pathlib.PurePath>` or `str`
"""
write_file(filename, self.export_private_key(*args, **kwargs))
def write_public_key(self, filename: FilePath, *args, **kwargs) -> None:
"""Write a public key to a file in the requested format
This method is a simple wrapper around :meth:`export_public_key`
which writes the exported key data to a file.
:param filename:
The filename to write the public key to.
:param \\*args,\\ \\*\\*kwargs:
Additional arguments to pass through to
:meth:`export_public_key`.
:type filename: :class:`PurePath <pathlib.PurePath>` or `str`
"""
write_file(filename, self.export_public_key(*args, **kwargs))
def append_private_key(self, filename: FilePath, *args, **kwargs) -> None:
"""Append a private key to a file in the requested format
This method is a simple wrapper around :meth:`export_private_key`
which appends the exported key data to an existing file.
:param filename:
The filename to append the private key to.
:param \\*args,\\ \\*\\*kwargs:
Additional arguments to pass through to
:meth:`export_private_key`.
:type filename: :class:`PurePath <pathlib.PurePath>` or `str`
"""
write_file(filename, self.export_private_key(*args, **kwargs), 'ab')
def append_public_key(self, filename: FilePath, *args, **kwargs) -> None:
"""Append a public key to a file in the requested format
This method is a simple wrapper around :meth:`export_public_key`
which appends the exported key data to an existing file.
:param filename:
The filename to append the public key to.
:param \\*args,\\ \\*\\*kwargs:
Additional arguments to pass through to
:meth:`export_public_key`.
:type filename: :class:`PurePath <pathlib.PurePath>` or `str`
"""
write_file(filename, self.export_public_key(*args, **kwargs), 'ab')
class SSHCertificate:
"""Parent class which holds an SSH certificate"""
is_x509 = False
is_x509_chain = False
def __init__(self, algorithm: bytes, sig_algorithms: Sequence[bytes],
host_key_algorithms: Sequence[bytes], key: SSHKey,
public_data: bytes, comment: _Comment):
self.algorithm = algorithm
self.sig_algorithms = sig_algorithms
self.host_key_algorithms = host_key_algorithms
self.key = key
self.public_data = public_data
self.set_comment(comment)
@classmethod
def construct(cls, packet: SSHPacket, algorithm: bytes,
key_handler: Optional[Type[SSHKey]],
comment: _Comment) -> 'SSHCertificate':
"""Construct an SSH certificate from packetized data"""
raise NotImplementedError
def __eq__(self, other: object) -> bool:
return (isinstance(other, type(self)) and
self.public_data == other.public_data)
def __hash__(self) -> int:
return hash(self.public_data)
def get_algorithm(self) -> str:
"""Return the algorithm associated with this certificate"""
return self.algorithm.decode('ascii')
def has_comment(self) -> bool:
"""Return whether a comment is set for this certificate
:returns: `bool`
"""
return bool(self._comment)
def get_comment_bytes(self) -> Optional[bytes]:
"""Return the comment associated with this certificate as a
byte string
:returns: `bytes` or `None`
"""
return self._comment
def get_comment(self, encoding: str = 'utf-8',
errors: str = 'strict') -> Optional[str]:
"""Return the comment associated with this certificate as a
Unicode string
:param encoding:
The encoding to use to decode the comment as a Unicode
string, defaulting to UTF-8
:param errors:
The error handling scheme to use for Unicode decode errors
:type encoding: `str`
:type errors: `str`
:returns: `str` or `None`
:raises: :exc:`UnicodeDecodeError` if the comment cannot be
decoded using the specified encoding
"""
return self._comment.decode(encoding, errors) if self._comment else None
def set_comment(self, comment: _Comment, encoding: str = 'utf-8',
errors: str = 'strict') -> None:
"""Set the comment associated with this certificate
:param comment:
The new comment to associate with this key
:param encoding:
The Unicode encoding to use to encode the comment,
defaulting to UTF-8
:param errors:
The error handling scheme to use for Unicode encode errors
:type comment: `str`, `bytes`, or `None`
:type encoding: `str`
:type errors: `str`
:raises: :exc:`UnicodeEncodeError` if the comment cannot be
encoded using the specified encoding
"""
if isinstance(comment, str):
comment = comment.encode(encoding, errors)
self._comment = comment or None
def export_certificate(self, format_name: str = 'openssh') -> bytes:
"""Export a certificate in the requested format
This function returns this certificate encoded in the requested
format. Available formats include:
der, pem, openssh, rfc4716
By default, OpenSSH format will be used.
:param format_name: (optional)
The format to export the certificate in.
:type format_name: `str`
:returns: `bytes` representing the exported certificate
"""
if self.is_x509:
if format_name == 'rfc4716':
raise KeyExportError('RFC4716 format is not supported for '
'X.509 certificates')
else:
if format_name in ('der', 'pem'):
raise KeyExportError('DER and PEM formats are not supported '
'for OpenSSH certificates')
if format_name == 'der':
return self.public_data
elif format_name == 'pem':
return (b'-----BEGIN CERTIFICATE-----\n' +
_wrap_base64(self.public_data) +
b'-----END CERTIFICATE-----\n')
elif format_name == 'openssh':
if self._comment:
comment = b' ' + self._comment
else:
comment = b''
return (self.algorithm + b' ' +
binascii.b2a_base64(self.public_data)[:-1] +
comment + b'\n')
elif format_name == 'rfc4716':
if self._comment:
comment = (b'Comment: "' + self._comment + b'"\n')
else:
comment = b''
return (b'---- BEGIN SSH2 PUBLIC KEY ----\n' +
comment + _wrap_base64(self.public_data) +
b'---- END SSH2 PUBLIC KEY ----\n')
else:
raise KeyExportError('Unknown export format')
def write_certificate(self, filename: FilePath,
format_name: str = 'openssh') -> None:
"""Write a certificate to a file in the requested format
This function is a simple wrapper around export_certificate
which writes the exported certificate to a file.
:param filename:
The filename to write the certificate to.
:param format_name: (optional)
The format to export the certificate in.
:type filename: :class:`PurePath <pathlib.PurePath>` or `str`
:type format_name: `str`
"""
write_file(filename, self.export_certificate(format_name))
def append_certificate(self, filename: FilePath,
format_name: str = 'openssh') -> None:
"""Append a certificate to a file in the requested format
This function is a simple wrapper around export_certificate
which appends the exported certificate to an existing file.
:param filename:
The filename to append the certificate to.
:param format_name: (optional)
The format to export the certificate in.
:type filename: :class:`PurePath <pathlib.PurePath>` or `str`
:type format_name: `str`
"""
write_file(filename, self.export_certificate(format_name), 'ab')
class SSHOpenSSHCertificate(SSHCertificate):
"""Class which holds an OpenSSH certificate"""
_user_option_encoders: _OpenSSHCertEncoders = ()
_user_extension_encoders: _OpenSSHCertEncoders = ()
_host_option_encoders: _OpenSSHCertEncoders = ()
_host_extension_encoders: _OpenSSHCertEncoders = ()
_user_option_decoders: _OpenSSHCertDecoders = {}
_user_extension_decoders: _OpenSSHCertDecoders = {}
_host_option_decoders: _OpenSSHCertDecoders = {}
_host_extension_decoders: _OpenSSHCertDecoders = {}
def __init__(self, algorithm: bytes, key: SSHKey, data: bytes,
principals: Sequence[str], options: _OpenSSHCertOptions,
signing_key: SSHKey, serial: int, cert_type: int,
key_id: str, valid_after: int, valid_before: int,
comment: _Comment):
super().__init__(algorithm, key.sig_algorithms,
key.cert_algorithms or (algorithm,),
key, data, comment)
self.principals = principals
self.options = options
self.signing_key = signing_key
self._serial = serial
self._cert_type = cert_type
self._key_id = key_id
self._valid_after = valid_after
self._valid_before = valid_before
@classmethod
def generate(cls, signing_key: 'SSHKey', algorithm: bytes, key: 'SSHKey',
serial: int, cert_type: int, key_id: str,
principals: Sequence[str], valid_after: int,
valid_before: int, options: _OpenSSHCertOptions,
sig_alg: bytes, comment: _Comment) -> 'SSHOpenSSHCertificate':
"""Generate a new SSH certificate"""
principal_bytes = b''.join(String(p) for p in principals)
if cert_type == CERT_TYPE_USER:
cert_options = cls._encode_options(options,
cls._user_option_encoders)
cert_extensions = cls._encode_options(options,
cls._user_extension_encoders)
else:
cert_options = cls._encode_options(options,
cls._host_option_encoders)
cert_extensions = cls._encode_options(options,
cls._host_extension_encoders)
key = key.convert_to_public()
data = b''.join((String(algorithm),
cls._encode(key, serial, cert_type, key_id,
principal_bytes, valid_after,
valid_before, cert_options,
cert_extensions),
String(signing_key.public_data)))
data += String(signing_key.sign(data, sig_alg))
signing_key = signing_key.convert_to_public()
return cls(algorithm, key, data, principals, options, signing_key,
serial, cert_type, key_id, valid_after, valid_before,
comment)
@classmethod
def construct(cls, packet: SSHPacket, algorithm: bytes,
key_handler: Optional[Type[SSHKey]],
comment: _Comment) -> 'SSHOpenSSHCertificate':
"""Construct an SSH certificate from packetized data"""
assert key_handler is not None
key_params, serial, cert_type, key_id, \
principals, valid_after, valid_before, \
options, extensions = cls._decode(packet, key_handler)
signing_key = decode_ssh_public_key(packet.get_string())
data = packet.get_consumed_payload()
signature = packet.get_string()
packet.check_end()
if not signing_key.verify(data, signature):
raise KeyImportError('Invalid certificate signature')
key = key_handler.make_public(key_params)
data = packet.get_consumed_payload()
try:
key_id_bytes = key_id.decode('utf-8')
except UnicodeDecodeError:
raise KeyImportError('Invalid characters in key ID') from None
packet = SSHPacket(principals)
principals: List[str] = []
while packet:
try:
principal = packet.get_string().decode('utf-8')
except UnicodeDecodeError:
raise KeyImportError('Invalid characters in principal '
'name') from None
principals.append(principal)
if cert_type == CERT_TYPE_USER:
cert_options = cls._decode_options(
options, cls._user_option_decoders, True)
cert_options.update(cls._decode_options(
extensions, cls._user_extension_decoders, False))
elif cert_type == CERT_TYPE_HOST:
cert_options = cls._decode_options(
options, cls._host_option_decoders, True)
cert_options.update(cls._decode_options(
extensions, cls._host_extension_decoders, False))
else:
raise KeyImportError('Unknown certificate type')
return cls(algorithm, key, data, principals, cert_options, signing_key,
serial, cert_type, key_id_bytes, valid_after, valid_before,
comment)
@classmethod
def _encode(cls, key: SSHKey, serial: int, cert_type: int, key_id: str,
principals: bytes, valid_after: int, valid_before: int,
options: bytes, extensions: bytes) -> bytes:
"""Encode an SSH certificate"""
raise NotImplementedError
@classmethod
def _decode(cls, packet: SSHPacket,
key_handler: Type[SSHKey]) -> _OpenSSHCertParams:
"""Decode an SSH certificate"""
raise NotImplementedError
@staticmethod
def _encode_options(options: _OpenSSHCertOptions,
encoders: _OpenSSHCertEncoders) -> bytes:
"""Encode options found in this certificate"""
result = []
for name, encoder in encoders:
value = options.get(name)
if value:
result.append(String(name) + String(encoder(value)))
return b''.join(result)
@staticmethod
def _encode_bool(_value: object) -> bytes:
"""Encode a boolean option value"""
return b''
@staticmethod
def _encode_force_cmd(force_command: object) -> bytes:
"""Encode a force-command option"""
return String(cast(BytesOrStr, force_command))
@staticmethod
def _encode_source_addr(source_address: object) -> bytes:
"""Encode a source-address option"""
return NameList(str(addr).encode('ascii')
for addr in cast(Sequence[IPNetwork], source_address))
@staticmethod
def _decode_bool(_packet: SSHPacket) -> bool:
"""Decode a boolean option value"""
return True
@staticmethod
def _decode_force_cmd(packet: SSHPacket) -> str:
"""Decode a force-command option"""
try:
return packet.get_string().decode('utf-8')
except UnicodeDecodeError:
raise KeyImportError('Invalid characters in command') from None
@staticmethod
def _decode_source_addr(packet: SSHPacket) -> Sequence[IPNetwork]:
"""Decode a source-address option"""
try:
return [ip_network(addr.decode('ascii'))
for addr in packet.get_namelist()]
except (UnicodeDecodeError, ValueError):
raise KeyImportError('Invalid source address') from None
@staticmethod
def _decode_options(options: bytes, decoders: _OpenSSHCertDecoders,
critical: bool = True) -> _OpenSSHCertOptions:
"""Decode options found in this certificate"""
packet = SSHPacket(options)
result: _OpenSSHCertOptions = {}
while packet:
name = packet.get_string()
decoder = decoders.get(name)
if decoder:
data_packet = SSHPacket(packet.get_string())
result[name.decode('ascii')] = decoder(data_packet)
data_packet.check_end()
elif critical:
raise KeyImportError('Unrecognized critical option: ' +
name.decode('ascii', errors='replace'))
return result
def validate(self, cert_type: int, principal: Optional[str]) -> None:
"""Validate an OpenSSH certificate"""
if self._cert_type != cert_type:
raise ValueError('Invalid certificate type')
now = time.time()
if now < self._valid_after:
raise ValueError('Certificate not yet valid')
if now >= self._valid_before:
raise ValueError('Certificate expired')
if principal is not None and self.principals and \
principal not in self.principals:
raise ValueError('Certificate principal mismatch')
class SSHOpenSSHCertificateV01(SSHOpenSSHCertificate):
"""Encoder/decoder class for version 01 OpenSSH certificates"""
_user_option_encoders = (
('force-command', SSHOpenSSHCertificate._encode_force_cmd),
('source-address', SSHOpenSSHCertificate._encode_source_addr)
)
_user_extension_encoders = (
('permit-X11-forwarding', SSHOpenSSHCertificate._encode_bool),
('permit-agent-forwarding', SSHOpenSSHCertificate._encode_bool),
('permit-port-forwarding', SSHOpenSSHCertificate._encode_bool),
('permit-pty', SSHOpenSSHCertificate._encode_bool),
('permit-user-rc', SSHOpenSSHCertificate._encode_bool),
('no-touch-required', SSHOpenSSHCertificate._encode_bool)
)
_user_option_decoders = {
b'force-command': SSHOpenSSHCertificate._decode_force_cmd,
b'source-address': SSHOpenSSHCertificate._decode_source_addr
}
_user_extension_decoders = {
b'permit-X11-forwarding': SSHOpenSSHCertificate._decode_bool,
b'permit-agent-forwarding': SSHOpenSSHCertificate._decode_bool,
b'permit-port-forwarding': SSHOpenSSHCertificate._decode_bool,
b'permit-pty': SSHOpenSSHCertificate._decode_bool,
b'permit-user-rc': SSHOpenSSHCertificate._decode_bool,
b'no-touch-required': SSHOpenSSHCertificate._decode_bool
}
@classmethod
def _encode(cls, key: SSHKey, serial: int, cert_type: int, key_id: str,
principals: bytes, valid_after: int, valid_before: int,
options: bytes, extensions: bytes) -> bytes:
"""Encode a version 01 SSH certificate"""
return b''.join((String(os.urandom(32)), key.encode_ssh_public(),
UInt64(serial), UInt32(cert_type), String(key_id),
String(principals), UInt64(valid_after),
UInt64(valid_before), String(options),
String(extensions), String('')))
@classmethod
def _decode(cls, packet: SSHPacket,
key_handler: Type[SSHKey]) -> _OpenSSHCertParams:
"""Decode a version 01 SSH certificate"""
_ = packet.get_string() # nonce
key_params = key_handler.decode_ssh_public(packet)
serial = packet.get_uint64()
cert_type = packet.get_uint32()
key_id = packet.get_string()
principals = packet.get_string()
valid_after = packet.get_uint64()
valid_before = packet.get_uint64()
options = packet.get_string()
extensions = packet.get_string()
_ = packet.get_string() # reserved
return (key_params, serial, cert_type, key_id, principals,
valid_after, valid_before, options, extensions)
class SSHX509Certificate(SSHCertificate):
"""Encoder/decoder class for SSH X.509 certificates"""
is_x509 = True
def __init__(self, key: SSHKey, x509_cert: 'X509Certificate',
comment: _Comment = None):
super().__init__(b'x509v3-' + key.algorithm, key.x509_algorithms,
key.x509_algorithms, key, x509_cert.data,
x509_cert.comment or comment)
self.subject = x509_cert.subject
self.issuer = x509_cert.issuer
self.issuer_hash = x509_cert.issuer_hash
self.user_principals = x509_cert.user_principals
self.x509_cert = x509_cert
def _expand_trust_store(self, cert: 'SSHX509Certificate',
trusted_cert_paths: Sequence[FilePath],
trust_store: Set['SSHX509Certificate']) -> None:
"""Look up certificates by issuer hash to build a trust store"""
issuer_hash = cert.issuer_hash
for path in trusted_cert_paths:
idx = 0
try:
while True:
cert_path = Path(path, issuer_hash + '.' + str(idx))
idx += 1
c = cast('SSHX509Certificate', read_certificate(cert_path))
if c.subject != cert.issuer or c in trust_store:
continue
trust_store.add(c)
self._expand_trust_store(c, trusted_cert_paths, trust_store)
except (OSError, KeyImportError):
pass
@classmethod
def construct(cls, packet: SSHPacket, algorithm: bytes,
key_handler: Optional[Type[SSHKey]],
comment: _Comment) -> 'SSHX509Certificate':
"""Construct an SSH X.509 certificate from packetized data"""
raise RuntimeError # pragma: no cover
@classmethod
def generate(cls, signing_key: 'SSHKey', key: 'SSHKey', subject: str,
issuer: Optional[str], serial: Optional[int],
valid_after: int, valid_before: int, ca: bool,
ca_path_len: Optional[int], purposes: X509CertPurposes,
user_principals: _CertPrincipals,
host_principals: _CertPrincipals, hash_name: str,
comment: _Comment) -> 'SSHX509Certificate':
"""Generate a new X.509 certificate"""
key = key.convert_to_public()
x509_cert = generate_x509_certificate(signing_key.pyca_key,
key.pyca_key, subject, issuer,
serial, valid_after, valid_before,
ca, ca_path_len, purposes,
user_principals, host_principals,
hash_name, comment)
return cls(key, x509_cert)
@classmethod
def construct_from_der(cls, data: bytes,
comment: _Comment = None) -> 'SSHX509Certificate':
"""Construct an SSH X.509 certificate from DER data"""
try:
x509_cert = import_x509_certificate(data)
key = import_public_key(x509_cert.key_data)
except ValueError as exc:
raise KeyImportError(str(exc)) from None
return cls(key, x509_cert, comment)
def validate_chain(self, trust_chain: Sequence['SSHX509Certificate'],
trusted_certs: Sequence['SSHX509Certificate'],
trusted_cert_paths: Sequence[FilePath],
purposes: X509CertPurposes, user_principal: str = '',
host_principal: str = '') -> None:
"""Validate an X.509 certificate chain"""
trust_store = {c for c in trust_chain if c.subject != c.issuer} | \
set(trusted_certs)
if trusted_cert_paths:
self._expand_trust_store(self, trusted_cert_paths, trust_store)
for c in trust_chain:
self._expand_trust_store(c, trusted_cert_paths, trust_store)
self.x509_cert.validate([c.x509_cert for c in trust_store],
purposes, user_principal, host_principal)
class SSHX509CertificateChain(SSHCertificate):
"""Encoder/decoder class for an SSH X.509 certificate chain"""
is_x509_chain = True
def __init__(self, algorithm: bytes, certs: Sequence[SSHCertificate],
ocsp_responses: Sequence[bytes], comment: _Comment):
key = certs[0].key
data = self._public_data(algorithm, certs, ocsp_responses)
super().__init__(algorithm, key.x509_algorithms, key.x509_algorithms,
key, data, comment)
x509_certs = cast(Sequence[SSHX509Certificate], certs)
first_cert = x509_certs[0]
last_cert = x509_certs[-1]
self.subject = first_cert.subject
self.issuer = last_cert.issuer
self.user_principals = first_cert.user_principals
self._certs = x509_certs
self._ocsp_responses = ocsp_responses
@staticmethod
def _public_data(algorithm: bytes, certs: Sequence[SSHCertificate],
ocsp_responses: Sequence[bytes]) -> bytes:
"""Return the X509 chain public data"""
return (String(algorithm) + UInt32(len(certs)) +
b''.join(String(c.public_data) for c in certs) +
UInt32(len(ocsp_responses)) +
b''.join(String(resp) for resp in ocsp_responses))
@classmethod
def construct(cls, packet: SSHPacket, algorithm: bytes,
key_handler: Optional[Type[SSHKey]],
comment: _Comment) -> 'SSHX509CertificateChain':
"""Construct an SSH X.509 certificate from packetized data"""
cert_count = packet.get_uint32()
certs = [import_certificate(packet.get_string())
for _ in range(cert_count)]
ocsp_resp_count = packet.get_uint32()
ocsp_responses = [packet.get_string() for _ in range(ocsp_resp_count)]
packet.check_end()
if not certs:
raise KeyImportError('No certificates present')
return cls(algorithm, certs, ocsp_responses, comment)
@classmethod
def construct_from_certs(cls, certs: Sequence['SSHCertificate']) -> \
'SSHX509CertificateChain':
"""Construct an SSH X.509 certificate chain from certificates"""
cert = certs[0]
return cls(cert.algorithm, certs, (), cert.get_comment_bytes())
def adjust_public_data(self, algorithm: bytes) -> bytes:
"""Adjust public data to reflect chosen signature algorithm"""
return self._public_data(algorithm, self._certs, self._ocsp_responses)
def validate_chain(self, trusted_certs: Sequence[SSHX509Certificate],
trusted_cert_paths: Sequence[FilePath],
revoked_certs: Set[SSHX509Certificate],
purposes: X509CertPurposes, user_principal: str = '',
host_principal: str = '') -> None:
"""Validate an X.509 certificate chain"""
if revoked_certs:
for cert in self._certs:
if cert in revoked_certs:
raise ValueError('Revoked X.509 certificate in '
'certificate chain')
self._certs[0].validate_chain(self._certs[1:], trusted_certs,
trusted_cert_paths, purposes,
user_principal, host_principal)
class SSHKeyPair:
"""Parent class which represents an asymmetric key pair
This is an abstract class which provides a method to sign data
with a private key and members to access the corresponding
algorithm and public key or certificate information needed to
identify what key was used for signing.
"""
_key_type = 'unknown'
def __init__(self, algorithm: bytes, sig_algorithm: bytes,
sig_algorithms: Sequence[bytes],
host_key_algorithms: Sequence[bytes],
public_data: bytes, comment: _Comment,
cert: Optional[SSHCertificate] = None,
filename: Optional[bytes] = None,
use_executor: bool = False,
use_webauthn: bool = False):
self.key_algorithm = algorithm
self.key_public_data = public_data
self.set_comment(comment)
self._cert = cert
self._filename = filename
self.use_executor = use_executor
self.use_webauthn = use_webauthn
if cert:
if cert.key.public_data != self.key_public_data:
raise ValueError('Certificate key mismatch')
self.algorithm = cert.algorithm
if cert.is_x509_chain:
self.sig_algorithm = cert.algorithm
else:
self.sig_algorithm = sig_algorithm
self.sig_algorithms = cert.sig_algorithms
self.host_key_algorithms = cert.host_key_algorithms
self.public_data = cert.public_data
else:
self.algorithm = algorithm
self.sig_algorithm = algorithm
self.sig_algorithms = sig_algorithms
self.host_key_algorithms = host_key_algorithms
self.public_data = public_data
def get_key_type(self) -> str:
"""Return what type of key pair this is
This method returns 'local' for locally loaded keys, and
'agent' for keys managed by an SSH agent.
"""
return self._key_type
@property
def has_cert(self) -> bool:
""" Return if this key pair has an associated cert"""
return bool(self._cert)
@property
def has_x509_chain(self) -> bool:
""" Return if this key pair has an associated X.509 cert chain"""
return self._cert.is_x509_chain if self._cert else False
def get_algorithm(self) -> str:
"""Return the algorithm associated with this key pair"""
return self.algorithm.decode('ascii')
def get_agent_private_key(self) -> bytes:
"""Return binary encoding of keypair for upload to SSH agent"""
# pylint: disable=no-self-use
raise KeyImportError('Private key export to agent not supported')
def get_comment_bytes(self) -> Optional[bytes]:
"""Return the comment associated with this key pair as a
byte string
:returns: `bytes` or `None`
"""
return self._comment or self._filename
def get_comment(self, encoding: str = 'utf-8',
errors: str = 'strict') -> Optional[str]:
"""Return the comment associated with this key pair as a
Unicode string
:param encoding:
The encoding to use to decode the comment as a Unicode
string, defaulting to UTF-8
:param errors:
The error handling scheme to use for Unicode decode errors
:type encoding: `str`
:type errors: `str`
:returns: `str` or `None`
:raises: :exc:`UnicodeDecodeError` if the comment cannot be
decoded using the specified encoding
"""
comment = self.get_comment_bytes()
return comment.decode(encoding, errors) if comment else None
def set_comment(self, comment: _Comment, encoding: str = 'utf-8',
errors: str = 'strict') -> None:
"""Set the comment associated with this key pair
:param comment:
The new comment to associate with this key
:param encoding:
The Unicode encoding to use to encode the comment,
defaulting to UTF-8
:param errors:
The error handling scheme to use for Unicode encode errors
:type comment: `str`, `bytes`, or `None`
:type encoding: `str`
:type errors: `str`
:raises: :exc:`UnicodeEncodeError` if the comment cannot be
encoded using the specified encoding
"""
if isinstance(comment, str):
comment = comment.encode(encoding, errors)
self._comment = comment or None
def set_certificate(self, cert: SSHCertificate) -> None:
"""Set certificate to use with this key"""
if cert.key.public_data != self.key_public_data:
raise ValueError('Certificate key mismatch')
self._cert = cert
self.algorithm = cert.algorithm
if cert.is_x509_chain:
self.sig_algorithm = cert.algorithm
else:
self.sig_algorithm = self.key_algorithm
self.sig_algorithms = cert.sig_algorithms
self.host_key_algorithms = cert.host_key_algorithms
self.public_data = cert.public_data
def set_sig_algorithm(self, sig_algorithm: bytes) -> None:
"""Set the signature algorithm to use when signing data"""
try:
sig_algorithm = _certificate_sig_alg_map[sig_algorithm]
except KeyError:
pass
self.sig_algorithm = sig_algorithm
if not self.has_cert:
self.algorithm = sig_algorithm
elif self.has_x509_chain:
self.algorithm = sig_algorithm
cert = cast('SSHX509CertificateChain', self._cert)
self.public_data = cert.adjust_public_data(sig_algorithm)
def sign(self, data: bytes) -> bytes:
"""Sign a block of data with this private key"""
# pylint: disable=no-self-use
raise RuntimeError # pragma: no cover
class SSHLocalKeyPair(SSHKeyPair):
"""Class which holds a local asymmetric key pair
This class holds a private key and associated public data
which can either be the matching public key or a certificate
which has signed that public key.
"""
_key_type = 'local'
def __init__(self, key: SSHKey, pubkey: Optional[SSHKey] = None,
cert: Optional[SSHCertificate] = None):
if pubkey and pubkey.public_data != key.public_data:
raise ValueError('Public key mismatch')
if key.has_comment():
comment = key.get_comment_bytes()
elif cert and cert.has_comment():
comment = cert.get_comment_bytes()
elif pubkey and pubkey.has_comment():
comment = pubkey.get_comment_bytes()
else:
comment = None
super().__init__(key.algorithm, key.algorithm, key.sig_algorithms,
key.sig_algorithms, key.public_data, comment,
cert, key.get_filename(), key.use_executor,
key.use_webauthn)
self._key = key
def get_agent_private_key(self) -> bytes:
"""Return binary encoding of keypair for upload to SSH agent"""
if self._cert:
data = String(self.public_data) + \
self._key.encode_agent_cert_private()
else:
data = self._key.encode_ssh_private()
return String(self.algorithm) + data
def sign(self, data: bytes) -> bytes:
"""Sign a block of data with this private key"""
return self._key.sign(data, self.sig_algorithm)
def _parse_openssh(data: bytes) -> Tuple[bytes, Optional[bytes], bytes]:
"""Parse an OpenSSH format public key or certificate"""
line = data.split(None, 2)
if len(line) < 2:
raise KeyImportError('Invalid OpenSSH public key or certificate')
elif len(line) == 2:
comment = None
else:
comment = line[2]
if (line[0] not in _public_key_alg_map and
line[0] not in _certificate_alg_map):
raise KeyImportError('Unknown OpenSSH public key algorithm')
try:
return line[0], comment, binascii.a2b_base64(line[1])
except binascii.Error:
raise KeyImportError('Invalid OpenSSH public key '
'or certificate') from None
def _parse_pem(data: bytes) -> Tuple[Mapping[bytes, bytes], bytes]:
"""Parse a PEM data block"""
start = 0
end: Optional[int] = None
headers: Dict[bytes, bytes] = {}
while True:
end = data.find(b'\n', start) + 1
line = data[start:end] if end else data[start:]
line = line.rstrip()
if b':' in line:
hdr, value = line.split(b':', 1)
headers[hdr.strip()] = value.strip()
else:
break
start = end if end != 0 else len(data)
try:
return headers, binascii.a2b_base64(data[start:])
except binascii.Error:
raise KeyImportError('Invalid PEM data') from None
def _parse_rfc4716(data: bytes) -> Tuple[Optional[bytes], bytes]:
"""Parse an RFC 4716 data block"""
start = 0
end = None
hdr = b''
comment = None
while True:
end = data.find(b'\n', start) + 1
line = data[start:end] if end else data[start:]
line = line.rstrip()
if line[-1:] == b'\\':
hdr += line[:-1]
else:
hdr += line
if b':' in hdr:
hdr, value = hdr.split(b':', 1)
if hdr.strip() == b'Comment':
comment = value.strip()
if comment[:1] == b'"' and comment[-1:] == b'"':
comment = comment[1:-1]
hdr = b''
else:
break
start = end if end != 0 else len(data)
try:
return comment, binascii.a2b_base64(data[start:])
except binascii.Error:
raise KeyImportError('Invalid RFC 4716 data') from None
def _match_block(data: bytes, start: int, header: bytes,
fmt: str) -> Tuple[bytes, int]:
"""Match a block of data wrapped in a header/footer"""
match = re.compile(b'^' + header[:5] + b'END' + header[10:] +
rb'[ \t\r\f\v]*$', re.M).search(data, start)
if not match:
raise KeyImportError(f'Missing {fmt} footer')
return data[start:match.start()], match.end()
def _match_next(data: bytes, keytype: bytes, public: bool = False) -> \
Tuple[Optional[str], Tuple, Optional[int]]:
"""Find the next key/certificate and call the appropriate decode"""
end: Optional[int]
if data.startswith(b'\x30'):
try:
key_data, end = der_decode_partial(data)
return 'der', (key_data,), end
except ASN1DecodeError:
pass
start = 0
end = None
while end != 0:
end = data.find(b'\n', start) + 1
line = data[start:end] if end else data[start:]
line = line.rstrip()
if (line.startswith(b'-----BEGIN ') and
line.endswith(b' ' + keytype + b'-----')):
pem_name = line[11:-(6+len(keytype))].strip()
data, end = _match_block(data, end, line, 'PEM')
headers, data = _parse_pem(data)
return 'pem', (pem_name, headers, data), end
elif public:
if line == b'---- BEGIN SSH2 PUBLIC KEY ----':
data, end = _match_block(data, end, line, 'RFC 4716')
return 'rfc4716', _parse_rfc4716(data), end
else:
try:
cert = _parse_openssh(line)
except KeyImportError:
pass
else:
return 'openssh', cert, (end if end else len(data))
start = end
return None, (), len(data)
def _decode_pkcs1_private(
pem_name: bytes, key_data: object,
unsafe_skip_rsa_key_validation: Optional[bool]) -> SSHKey:
"""Decode a PKCS#1 format private key"""
handler = _pem_map.get(pem_name)
if handler is None:
raise KeyImportError('Unknown PEM key type: ' +
pem_name.decode('ascii'))
key_params = handler.decode_pkcs1_private(key_data)
if key_params is None:
raise KeyImportError(
f'Invalid {pem_name.decode("ascii")} private key')
if pem_name == b'RSA':
key_params = cast(Tuple, key_params) + \
(unsafe_skip_rsa_key_validation,)
return handler.make_private(key_params)
def _decode_pkcs1_public(pem_name: bytes, key_data: object) -> SSHKey:
"""Decode a PKCS#1 format public key"""
handler = _pem_map.get(pem_name)
if handler is None:
raise KeyImportError('Unknown PEM key type: ' +
pem_name.decode('ascii'))
key_params = handler.decode_pkcs1_public(key_data)
if key_params is None:
raise KeyImportError(f'Invalid {pem_name.decode("ascii")} public key')
return handler.make_public(key_params)
def _decode_pkcs8_private(
key_data: object,
unsafe_skip_rsa_key_validation: Optional[bool]) -> SSHKey:
"""Decode a PKCS#8 format private key"""
if (isinstance(key_data, tuple) and len(key_data) >= 3 and
key_data[0] in (0, 1) and isinstance(key_data[1], tuple) and
1 <= len(key_data[1]) <= 2 and isinstance(key_data[2], bytes)):
if len(key_data[1]) == 2:
alg, alg_params = key_data[1]
else:
alg, alg_params = key_data[1][0], OMIT
handler = _pkcs8_oid_map.get(alg)
if handler is None:
raise KeyImportError('Unknown PKCS#8 algorithm')
key_params = handler.decode_pkcs8_private(alg_params, key_data[2])
if key_params is None:
key_type = handler.pem_name.decode('ascii') if \
handler.pem_name else 'PKCS#8'
raise KeyImportError(f'Invalid {key_type} private key')
if alg == ObjectIdentifier('1.2.840.113549.1.1.1'):
key_params = cast(Tuple, key_params) + \
(unsafe_skip_rsa_key_validation,)
return handler.make_private(key_params)
else:
raise KeyImportError('Invalid PKCS#8 private key')
def _decode_pkcs8_public(key_data: object) -> SSHKey:
"""Decode a PKCS#8 format public key"""
if (isinstance(key_data, tuple) and len(key_data) == 2 and
isinstance(key_data[0], tuple) and 1 <= len(key_data[0]) <= 2 and
isinstance(key_data[1], BitString) and key_data[1].unused == 0):
if len(key_data[0]) == 2:
alg, alg_params = key_data[0]
else:
alg, alg_params = key_data[0][0], OMIT
handler = _pkcs8_oid_map.get(alg)
if handler is None:
raise KeyImportError('Unknown PKCS#8 algorithm')
key_params = handler.decode_pkcs8_public(alg_params, key_data[1].value)
if key_params is None:
key_type = handler.pem_name.decode('ascii') if \
handler.pem_name else 'PKCS#8'
raise KeyImportError(f'Invalid {key_type} public key')
return handler.make_public(key_params)
else:
raise KeyImportError('Invalid PKCS#8 public key')
def _decode_openssh_private(
data: bytes, passphrase: Optional[BytesOrStr],
unsafe_skip_rsa_key_validation: Optional[bool]) -> SSHKey:
"""Decode an OpenSSH format private key"""
try:
if not data.startswith(_OPENSSH_KEY_V1):
raise KeyImportError('Unrecognized OpenSSH private key type')
data = data[len(_OPENSSH_KEY_V1):]
packet = SSHPacket(data)
cipher_name = packet.get_string()
kdf = packet.get_string()
kdf_data = packet.get_string()
nkeys = packet.get_uint32()
_ = packet.get_string() # public_key
key_data = packet.get_string()
mac = packet.get_remaining_payload()
if nkeys != 1:
raise KeyImportError('Invalid OpenSSH private key')
if cipher_name != b'none':
if passphrase is None:
raise KeyImportError('Passphrase must be specified to import '
'encrypted private keys')
try:
key_size, iv_size, _, _, _, _ = \
get_encryption_params(cipher_name)
except KeyError:
raise KeyEncryptionError('Unknown cipher: ' +
cipher_name.decode('ascii')) from None
if kdf != b'bcrypt':
raise KeyEncryptionError('Unknown kdf: ' + kdf.decode('ascii'))
if not _bcrypt_available: # pragma: no cover
raise KeyEncryptionError('OpenSSH private key encryption '
'requires bcrypt with KDF support')
packet = SSHPacket(kdf_data)
salt = packet.get_string()
rounds = packet.get_uint32()
packet.check_end()
if isinstance(passphrase, str):
passphrase = passphrase.encode('utf-8')
try:
bcrypt_key = bcrypt.kdf(passphrase, salt, key_size + iv_size,
rounds, ignore_few_rounds=True)
except ValueError:
raise KeyEncryptionError('Invalid OpenSSH '
'private key') from None
cipher = get_encryption(cipher_name, bcrypt_key[:key_size],
bcrypt_key[key_size:])
decrypted_key = cipher.decrypt_packet(0, b'', key_data, 0, mac)
if decrypted_key is None:
raise KeyEncryptionError('Incorrect passphrase')
key_data = decrypted_key
packet = SSHPacket(key_data)
check1 = packet.get_uint32()
check2 = packet.get_uint32()
if check1 != check2:
if cipher_name != b'none':
raise KeyEncryptionError('Incorrect passphrase') from None
else:
raise KeyImportError('Invalid OpenSSH private key')
alg = packet.get_string()
handler = _public_key_alg_map.get(alg)
if not handler:
raise KeyImportError('Unknown OpenSSH private key algorithm')
key_params = handler.decode_ssh_private(packet)
comment = packet.get_string()
pad = packet.get_remaining_payload()
if len(pad) >= 256 or pad != bytes(range(1, len(pad) + 1)):
raise KeyImportError('Invalid OpenSSH private key')
if alg == b'ssh-rsa':
key_params = cast(Tuple, key_params) + \
(unsafe_skip_rsa_key_validation,)
key = handler.make_private(key_params)
key.set_comment(comment)
return key
except PacketDecodeError:
raise KeyImportError('Invalid OpenSSH private key') from None
def _decode_openssh_public(data: bytes) -> SSHKey:
"""Decode public key within OpenSSH format private key"""
try:
if not data.startswith(_OPENSSH_KEY_V1):
raise KeyImportError('Unrecognized OpenSSH private key type')
data = data[len(_OPENSSH_KEY_V1):]
packet = SSHPacket(data)
_ = packet.get_string() # cipher_name
_ = packet.get_string() # kdf
_ = packet.get_string() # kdf_data
nkeys = packet.get_uint32()
pubkey = packet.get_string()
if nkeys != 1:
raise KeyImportError('Invalid OpenSSH private key')
return decode_ssh_public_key(pubkey)
except PacketDecodeError:
raise KeyImportError('Invalid OpenSSH private key') from None
def _decode_der_private(
key_data: object, passphrase: Optional[BytesOrStr],
unsafe_skip_rsa_key_validation: Optional[bool]) -> SSHKey:
"""Decode a DER format private key"""
# First, if there's a passphrase, try to decrypt PKCS#8
if passphrase is not None:
try:
key_data = pkcs8_decrypt(key_data, passphrase)
except KeyEncryptionError:
# Decryption failed - try decoding it as unencrypted
pass
# Then, try to decode PKCS#8
try:
return _decode_pkcs8_private(key_data, unsafe_skip_rsa_key_validation)
except KeyImportError:
# PKCS#8 failed - try PKCS#1 instead
pass
# If that fails, try each of the possible PKCS#1 encodings
for pem_name in _pem_map:
try:
return _decode_pkcs1_private(pem_name, key_data,
unsafe_skip_rsa_key_validation)
except KeyImportError:
# Try the next PKCS#1 encoding
pass
raise KeyImportError('Invalid DER private key')
def _decode_der_public(key_data: object) -> SSHKey:
"""Decode a DER format public key"""
# First, try to decode PKCS#8
try:
return _decode_pkcs8_public(key_data)
except KeyImportError:
# PKCS#8 failed - try PKCS#1 instead
pass
# If that fails, try each of the possible PKCS#1 encodings
for pem_name in _pem_map:
try:
return _decode_pkcs1_public(pem_name, key_data)
except KeyImportError:
# Try the next PKCS#1 encoding
pass
raise KeyImportError('Invalid DER public key')
def _decode_der_certificate(data: bytes,
comment: _Comment = None) -> SSHCertificate:
"""Decode a DER format X.509 certificate"""
return SSHX509Certificate.construct_from_der(data, comment)
def _decode_pem_private(
pem_name: bytes, headers: Mapping[bytes, bytes],
data: bytes, passphrase: Optional[BytesOrStr],
unsafe_skip_rsa_key_validation: Optional[bool]) -> SSHKey:
"""Decode a PEM format private key"""
if pem_name == b'OPENSSH':
return _decode_openssh_private(data, passphrase,
unsafe_skip_rsa_key_validation)
if headers.get(b'Proc-Type') == b'4,ENCRYPTED':
if passphrase is None:
raise KeyImportError('Passphrase must be specified to import '
'encrypted private keys')
dek_info = headers.get(b'DEK-Info', b'').split(b',')
if len(dek_info) != 2:
raise KeyImportError('Invalid PEM encryption params')
alg, iv = dek_info
try:
iv = binascii.a2b_hex(iv)
except binascii.Error:
raise KeyImportError('Invalid PEM encryption params') from None
try:
data = pkcs1_decrypt(data, alg, iv, passphrase)
except KeyEncryptionError:
raise KeyImportError('Unable to decrypt PKCS#1 '
'private key') from None
try:
key_data = der_decode(data)
except ASN1DecodeError:
raise KeyImportError('Invalid PEM private key') from None
if pem_name == b'ENCRYPTED':
if passphrase is None:
raise KeyImportError('Passphrase must be specified to import '
'encrypted private keys')
pem_name = b''
try:
key_data = pkcs8_decrypt(key_data, passphrase)
except KeyEncryptionError:
raise KeyImportError('Unable to decrypt PKCS#8 '
'private key') from None
if pem_name:
return _decode_pkcs1_private(pem_name, key_data,
unsafe_skip_rsa_key_validation)
else:
return _decode_pkcs8_private(key_data, unsafe_skip_rsa_key_validation)
def _decode_pem_public(pem_name: bytes, data: bytes) -> SSHKey:
"""Decode a PEM format public key"""
try:
key_data = der_decode(data)
except ASN1DecodeError:
raise KeyImportError('Invalid PEM public key') from None
if pem_name:
return _decode_pkcs1_public(pem_name, key_data)
else:
return _decode_pkcs8_public(key_data)
def _decode_pem_certificate(pem_name: bytes, data: bytes) -> SSHCertificate:
"""Decode a PEM format X.509 certificate"""
if pem_name == b'TRUSTED':
# Strip off OpenSSL trust information
try:
_, end = der_decode_partial(data)
data = data[:end]
except ASN1DecodeError:
raise KeyImportError('Invalid PEM trusted certificate') from None
elif pem_name:
raise KeyImportError('Invalid PEM certificate')
return SSHX509Certificate.construct_from_der(data)
def _decode_private(
data: bytes, passphrase: Optional[BytesOrStr],
unsafe_skip_rsa_key_validation: Optional[bool]) -> \
Tuple[Optional[SSHKey], Optional[int]]:
"""Decode a private key"""
fmt, key_info, end = _match_next(data, b'PRIVATE KEY')
key: Optional[SSHKey]
if fmt == 'der':
key = _decode_der_private(key_info[0], passphrase,
unsafe_skip_rsa_key_validation)
elif fmt == 'pem':
pem_name, headers, data = key_info
key = _decode_pem_private(pem_name, headers, data, passphrase,
unsafe_skip_rsa_key_validation)
else:
key = None
return key, end
def _decode_public(data: bytes) -> Tuple[Optional[SSHKey], Optional[int]]:
"""Decode a public key"""
fmt, key_info, end = _match_next(data, b'PUBLIC KEY', public=True)
key: Optional[SSHKey]
if fmt == 'der':
key = _decode_der_public(key_info[0])
elif fmt == 'pem':
pem_name, _, data = key_info
key = _decode_pem_public(pem_name, data)
elif fmt == 'openssh':
algorithm, comment, data = key_info
key = decode_ssh_public_key(data)
if algorithm != key.algorithm:
raise KeyImportError('Public key algorithm mismatch')
key.set_comment(comment)
elif fmt == 'rfc4716':
comment, data = key_info
key = decode_ssh_public_key(data)
key.set_comment(comment)
else:
fmt, key_info, end = _match_next(data, b'PRIVATE KEY')
if fmt == 'pem' and key_info[0] == b'OPENSSH':
key = _decode_openssh_public(key_info[2])
else:
key, _ = _decode_private(data, None, False)
if key:
key = key.convert_to_public()
return key, end
def _decode_certificate(data: bytes) -> \
Tuple[Optional[SSHCertificate], Optional[int]]:
"""Decode a certificate"""
fmt, key_info, end = _match_next(data, b'CERTIFICATE', public=True)
cert: Optional[SSHCertificate]
if fmt == 'der':
cert = _decode_der_certificate(data[:end])
elif fmt == 'pem':
pem_name, _, data = key_info
cert = _decode_pem_certificate(pem_name, data)
elif fmt == 'openssh':
algorithm, comment, data = key_info
if algorithm.startswith(b'x509v3-'):
cert = _decode_der_certificate(data, comment)
else:
cert = decode_ssh_certificate(data, comment)
elif fmt == 'rfc4716':
comment, data = key_info
cert = decode_ssh_certificate(data, comment)
else:
cert = None
return cert, end
def _decode_private_list(
data: bytes, passphrase: Optional[BytesOrStr],
unsafe_skip_rsa_key_validation: Optional[bool]) -> Sequence[SSHKey]:
"""Decode a private key list"""
keys: List[SSHKey] = []
while data:
key, end = _decode_private(data, passphrase,
unsafe_skip_rsa_key_validation)
if key:
keys.append(key)
data = data[end:]
return keys
def _decode_public_list(data: bytes) -> Sequence[SSHKey]:
"""Decode a public key list"""
keys: List[SSHKey] = []
while data:
key, end = _decode_public(data)
if key:
keys.append(key)
data = data[end:]
return keys
def _decode_certificate_list(data: bytes) -> Sequence[SSHCertificate]:
"""Decode a certificate list"""
certs: List[SSHCertificate] = []
while data:
cert, end = _decode_certificate(data)
if cert:
certs.append(cert)
data = data[end:]
return certs
def register_sk_alg(sk_alg: int, handler: Type[SSHKey], *args: object) -> None:
"""Register a new security key algorithm"""
_sk_alg_map[sk_alg] = handler, args
def register_public_key_alg(algorithm: bytes, handler: Type[SSHKey],
default: bool,
sig_algorithms: Optional[Sequence[bytes]] = \
None) -> None:
"""Register a new public key algorithm"""
if not sig_algorithms:
sig_algorithms = handler.sig_algorithms
_public_key_algs.extend(sig_algorithms)
if default:
_default_public_key_algs.extend(sig_algorithms)
_public_key_alg_map[algorithm] = handler
if handler.pem_name:
_pem_map[handler.pem_name] = handler
if handler.pkcs8_oid: # pragma: no branch
_pkcs8_oid_map[handler.pkcs8_oid] = handler
def register_certificate_alg(version: int, algorithm: bytes,
cert_algorithm: bytes,
key_handler: Type[SSHKey],
cert_handler: Type[SSHOpenSSHCertificate],
default: bool) -> None:
"""Register a new certificate algorithm"""
_certificate_algs.append(cert_algorithm)
if default:
_default_certificate_algs.append(cert_algorithm)
_certificate_alg_map[cert_algorithm] = (key_handler, cert_handler)
_certificate_sig_alg_map[cert_algorithm] = algorithm
_certificate_version_map[algorithm, version] = \
(cert_algorithm, cert_handler)
def register_x509_certificate_alg(cert_algorithm: bytes, default: bool) -> None:
"""Register a new X.509 certificate algorithm"""
if _x509_available: # pragma: no branch
_x509_certificate_algs.append(cert_algorithm)
if default:
_default_x509_certificate_algs.append(cert_algorithm)
_certificate_alg_map[cert_algorithm] = (None, SSHX509CertificateChain)
def get_public_key_algs() -> List[bytes]:
"""Return supported public key algorithms"""
return _public_key_algs
def get_default_public_key_algs() -> List[bytes]:
"""Return default public key algorithms"""
return _default_public_key_algs
def get_certificate_algs() -> List[bytes]:
"""Return supported certificate-based public key algorithms"""
return _certificate_algs
def get_default_certificate_algs() -> List[bytes]:
"""Return default certificate-based public key algorithms"""
return _default_certificate_algs
def get_x509_certificate_algs() -> List[bytes]:
"""Return supported X.509 certificate-based public key algorithms"""
return _x509_certificate_algs
def get_default_x509_certificate_algs() -> List[bytes]:
"""Return default X.509 certificate-based public key algorithms"""
return _default_x509_certificate_algs
def decode_ssh_public_key(data: bytes) -> SSHKey:
"""Decode a packetized SSH public key"""
try:
packet = SSHPacket(data)
alg = packet.get_string()
handler = _public_key_alg_map.get(alg)
if handler:
key_params = handler.decode_ssh_public(packet)
packet.check_end()
key = handler.make_public(key_params)
key.algorithm = alg
return key
else:
raise KeyImportError('Unknown key algorithm: ' +
alg.decode('ascii', errors='replace'))
except PacketDecodeError:
raise KeyImportError('Invalid public key') from None
def decode_ssh_certificate(data: bytes,
comment: _Comment = None) -> SSHCertificate:
"""Decode a packetized SSH certificate"""
try:
packet = SSHPacket(data)
alg = packet.get_string()
key_handler, cert_handler = _certificate_alg_map.get(alg, (None, None))
if cert_handler:
return cert_handler.construct(packet, alg, key_handler, comment)
else:
raise KeyImportError('Unknown certificate algorithm: ' +
alg.decode('ascii', errors='replace'))
except (PacketDecodeError, ValueError):
raise KeyImportError('Invalid OpenSSH certificate') from None
def generate_private_key(alg_name: str, comment: _Comment = None,
**kwargs) -> SSHKey:
"""Generate a new private key
This function generates a new private key of a type matching
the requested SSH algorithm. Depending on the algorithm, additional
parameters can be passed which affect the generated key.
Available algorithms include:
ssh-dss, ssh-rsa, ecdsa-sha2-nistp256, ecdsa-sha2-nistp384,
ecdsa-sha2-nistp521, ecdsa-sha2-1.3.132.0.10, ssh-ed25519,
ssh-ed448, sk-ecdsa-sha2-nistp256\\@openssh.com,
sk-ssh-ed25519\\@openssh.com
For dss keys, no parameters are supported. The key size is fixed at
1024 bits due to the use of SHA1 signatures.
For rsa keys, the key size can be specified using the `key_size`
parameter, and the RSA public exponent can be changed using the
`exponent` parameter. By default, generated keys are 2048 bits
with a public exponent of 65537.
For ecdsa keys, the curve to use is part of the SSH algorithm name
and that determines the key size. No other parameters are supported.
For ed25519 and ed448 keys, no parameters are supported. The key size
is fixed by the algorithms at 256 bits and 448 bits, respectively.
For sk keys, the application name to associate with the generated
key can be specified using the `application` parameter. It defaults
to `'ssh:'`. The user name to associate with the generated key can
be specified using the `user` parameter. It defaults to `'AsyncSSH'`.
When generating an sk key, a PIN can be provided via the `pin`
parameter if the security key requires it.
The `resident` parameter can be set to `True` to request that a
resident key be created on the security key. This allows the key
handle and public key information to later be retrieved so that
the generated key can be used without having to store any
information on the client system. It defaults to `False`.
You can enable or disable the security key touch requirement by
setting the `touch_required` parameter. It defaults to `True`,
requiring that the user confirm their presence by touching the
security key each time they use it to authenticate.
:param alg_name:
The SSH algorithm name corresponding to the desired type of key.
:param comment: (optional)
A comment to associate with this key.
:param key_size: (optional)
The key size in bits for RSA keys.
:param exponent: (optional)
The public exponent for RSA keys.
:param application: (optional)
The application name to associate with the generated SK key,
defaulting to `'ssh:'`.
:param user: (optional)
The user name to associate with the generated SK key, defaulting
to `'AsyncSSH'`.
:param pin: (optional)
The PIN to use to access the security key, defaulting to `None`.
:param resident: (optional)
Whether or not to create a resident key on the security key,
defaulting to `False`.
:param touch_required: (optional)
Whether or not to require the user to touch the security key
when authenticating with it, defaulting to `True`.
:type alg_name: `str`
:type comment: `str`, `bytes`, or `None`
:type key_size: `int`
:type exponent: `int`
:type application: `str`
:type user: `str`
:type pin: `str`
:type resident: `bool`
:type touch_required: `bool`
:returns: An :class:`SSHKey` private key
:raises: :exc:`KeyGenerationError` if the requested key parameters
are unsupported
"""
algorithm = alg_name.encode('utf-8')
handler = _public_key_alg_map.get(algorithm)
if handler:
try:
key = handler.generate(algorithm, **kwargs)
except (TypeError, ValueError) as exc:
raise KeyGenerationError(str(exc)) from None
else:
raise KeyGenerationError('Unknown algorithm: ' + alg_name)
key.set_comment(comment)
return key
def import_private_key(
data: BytesOrStr, passphrase: Optional[BytesOrStr] = None,
unsafe_skip_rsa_key_validation: Optional[bool] = None) -> SSHKey:
"""Import a private key
This function imports a private key encoded in PKCS#1 or PKCS#8 DER
or PEM format or OpenSSH format. Encrypted private keys can be
imported by specifying the passphrase needed to decrypt them.
:param data:
The data to import.
:param passphrase: (optional)
The passphrase to use to decrypt the key.
:param unsafe_skip_rsa_key_validation: (optional)
Whether or not to skip key validation when loading RSA private
keys, defaulting to performing these checks unless changed by
calling :func:`set_default_skip_rsa_key_validation`.
:type data: `bytes` or ASCII `str`
:type passphrase: `str` or `bytes`
:type unsafe_skip_rsa_key_validation: bool
:returns: An :class:`SSHKey` private key
"""
if isinstance(data, str):
try:
data = data.encode('ascii')
except UnicodeEncodeError:
raise KeyImportError('Invalid encoding for key') from None
key, _ = _decode_private(data, passphrase, unsafe_skip_rsa_key_validation)
if key:
return key
else:
raise KeyImportError('Invalid private key')
def import_private_key_and_certs(
data: bytes, passphrase: Optional[BytesOrStr] = None,
unsafe_skip_rsa_key_validation: Optional[bool] = None) -> \
Tuple[SSHKey, Optional[SSHX509CertificateChain]]:
"""Import a private key and optional certificate chain"""
key, end = _decode_private(data, passphrase,
unsafe_skip_rsa_key_validation)
if key:
return key, import_certificate_chain(data[end:])
else:
raise KeyImportError('Invalid private key')
def import_public_key(data: BytesOrStr) -> SSHKey:
"""Import a public key
This function imports a public key encoded in OpenSSH, RFC4716, or
PKCS#1 or PKCS#8 DER or PEM format.
:param data:
The data to import.
:type data: `bytes` or ASCII `str`
:returns: An :class:`SSHKey` public key
"""
if isinstance(data, str):
try:
data = data.encode('ascii')
except UnicodeEncodeError:
raise KeyImportError('Invalid encoding for key') from None
key, _ = _decode_public(data)
if key:
return key
else:
raise KeyImportError('Invalid public key')
def import_certificate(data: BytesOrStr) -> SSHCertificate:
"""Import a certificate
This function imports an SSH certificate in DER, PEM, OpenSSH, or
RFC4716 format.
:param data:
The data to import.
:type data: `bytes` or ASCII `str`
:returns: An :class:`SSHCertificate` object
"""
if isinstance(data, str):
try:
data = data.encode('ascii')
except UnicodeEncodeError:
raise KeyImportError('Invalid encoding for key') from None
cert, _ = _decode_certificate(data)
if cert:
return cert
else:
raise KeyImportError('Invalid certificate')
def import_certificate_chain(data: bytes) -> Optional[SSHX509CertificateChain]:
"""Import an X.509 certificate chain"""
certs = _decode_certificate_list(data)
chain: Optional[SSHX509CertificateChain]
if certs:
chain = SSHX509CertificateChain.construct_from_certs(certs)
else:
chain = None
return chain
def import_certificate_subject(data: str) -> str:
"""Import an X.509 certificate subject name"""
try:
algorithm, data = data.strip().split(None, 1)
except ValueError:
raise KeyImportError('Missing certificate subject algorithm') from None
if algorithm.startswith('x509v3-'):
match = _subject_pattern.match(data)
if match:
return data[match.end():]
raise KeyImportError('Invalid certificate subject')
def read_private_key(
filename: FilePath, passphrase: Optional[BytesOrStr] = None,
unsafe_skip_rsa_key_validation: Optional[bool] = None) -> SSHKey:
"""Read a private key from a file
This function reads a private key from a file. See the function
:func:`import_private_key` for information about the formats
supported.
:param filename:
The file to read the key from.
:param passphrase: (optional)
The passphrase to use to decrypt the key.
:param unsafe_skip_rsa_key_validation: (optional)
Whether or not to skip key validation when loading RSA private
keys, defaulting to performing these checks unless changed by
calling :func:`set_default_skip_rsa_key_validation`.
:type filename: :class:`PurePath <pathlib.PurePath>` or `str`
:type passphrase: `str` or `bytes`
:type unsafe_skip_rsa_key_validation: bool
:returns: An :class:`SSHKey` private key
"""
key = import_private_key(read_file(filename), passphrase,
unsafe_skip_rsa_key_validation)
key.set_filename(filename)
return key
def read_private_key_and_certs(
filename: FilePath, passphrase: Optional[BytesOrStr] = None,
unsafe_skip_rsa_key_validation: Optional[bool] = None) -> \
Tuple[SSHKey, Optional[SSHX509CertificateChain]]:
"""Read a private key and optional certificate chain from a file"""
key, cert = import_private_key_and_certs(read_file(filename), passphrase,
unsafe_skip_rsa_key_validation)
key.set_filename(filename)
return key, cert
def read_public_key(filename: FilePath) -> SSHKey:
"""Read a public key from a file
This function reads a public key from a file. See the function
:func:`import_public_key` for information about the formats
supported.
:param filename:
The file to read the key from.
:type filename: :class:`PurePath <pathlib.PurePath>` or `str`
:returns: An :class:`SSHKey` public key
"""
key = import_public_key(read_file(filename))
key.set_filename(filename)
return key
def read_certificate(filename: FilePath) -> SSHCertificate:
"""Read a certificate from a file
This function reads an SSH certificate from a file. See the
function :func:`import_certificate` for information about the
formats supported.
:param filename:
The file to read the certificate from.
:type filename: :class:`PurePath <pathlib.PurePath>` or `str`
:returns: An :class:`SSHCertificate` object
"""
return import_certificate(read_file(filename))
def read_private_key_list(
filename: FilePath, passphrase: Optional[BytesOrStr] = None,
unsafe_skip_rsa_key_validation: Optional[bool] = None) -> \
Sequence[SSHKey]:
"""Read a list of private keys from a file
This function reads a list of private keys from a file. See the
function :func:`import_private_key` for information about the
formats supported. If any of the keys are encrypted, they must
all be encrypted with the same passphrase.
:param filename:
The file to read the keys from.
:param passphrase: (optional)
The passphrase to use to decrypt the keys.
:param unsafe_skip_rsa_key_validation: (optional)
Whether or not to skip key validation when loading RSA private
keys, defaulting to performing these checks unless changed by
calling :func:`set_default_skip_rsa_key_validation`.
:type filename: :class:`PurePath <pathlib.PurePath>` or `str`
:type passphrase: `str` or `bytes`
:type unsafe_skip_rsa_key_validation: bool
:returns: A list of :class:`SSHKey` private keys
"""
keys = _decode_private_list(read_file(filename), passphrase,
unsafe_skip_rsa_key_validation)
for key in keys:
key.set_filename(filename)
return keys
def read_public_key_list(filename: FilePath) -> Sequence[SSHKey]:
"""Read a list of public keys from a file
This function reads a list of public keys from a file. See the
function :func:`import_public_key` for information about the
formats supported.
:param filename:
The file to read the keys from.
:type filename: :class:`PurePath <pathlib.PurePath>` or `str`
:returns: A list of :class:`SSHKey` public keys
"""
keys = _decode_public_list(read_file(filename))
for key in keys:
key.set_filename(filename)
return keys
def read_certificate_list(filename: FilePath) -> Sequence[SSHCertificate]:
"""Read a list of certificates from a file
This function reads a list of SSH certificates from a file. See
the function :func:`import_certificate` for information about
the formats supported.
:param filename:
The file to read the certificates from.
:type filename: :class:`PurePath <pathlib.PurePath>` or `str`
:returns: A list of :class:`SSHCertificate` certificates
"""
return _decode_certificate_list(read_file(filename))
def load_keypairs(
keylist: KeyPairListArg, passphrase: Optional[BytesOrStr] = None,
certlist: CertListArg = (), skip_public: bool = False,
ignore_encrypted: bool = False,
unsafe_skip_rsa_key_validation: Optional[bool] = None,
loop: Optional[asyncio.AbstractEventLoop] = None) -> \
Sequence[SSHKeyPair]:
"""Load SSH private keys and optional matching certificates
This function loads a list of SSH keys and optional matching
certificates.
When certificates are specified, the private key is added to
the list both with and without the certificate.
:param keylist:
The list of private keys and certificates to load.
:param passphrase: (optional)
The passphrase to use to decrypt the keys, or a `callable` which
takes a filename and returns the passphrase to decrypt it.
:param certlist: (optional)
A list of certificates to attempt to pair with the provided
list of private keys.
:param skip_public: (optional)
An internal parameter used to skip public keys and certificates
when IdentitiesOnly and IdentityFile are used to specify a
mixture of private and public keys.
:param unsafe_skip_rsa_key_validation: (optional)
Whether or not to skip key validation when loading RSA private
keys, defaulting to performing these checks unless changed by
calling :func:`set_default_skip_rsa_key_validation`.
:type keylist: *see* :ref:`SpecifyingPrivateKeys`
:type passphrase: `str` or `bytes`
:type certlist: *see* :ref:`SpecifyingCertificates`
:type skip_public: `bool`
:type unsafe_skip_rsa_key_validation: bool
:returns: A list of :class:`SSHKeyPair` objects
"""
keys_to_load: Sequence[_KeyPairArg]
result: List[SSHKeyPair] = []
certlist = load_certificates(certlist)
certdict = {cert.key.public_data: cert for cert in certlist}
if isinstance(keylist, (PurePath, str)):
try:
if callable(passphrase):
resolved_passphrase = passphrase(str(keylist))
else:
resolved_passphrase = passphrase
if loop and inspect.isawaitable(resolved_passphrase):
resolved_passphrase = asyncio.run_coroutine_threadsafe(
resolved_passphrase, loop).result()
priv_keys = read_private_key_list(keylist, resolved_passphrase,
unsafe_skip_rsa_key_validation)
if len(priv_keys) <= 1:
keys_to_load = [keylist]
passphrase = resolved_passphrase
else:
keys_to_load = priv_keys
except KeyImportError:
keys_to_load = [keylist]
elif isinstance(keylist, (tuple, bytes, SSHKey, SSHKeyPair)):
keys_to_load = [cast(_KeyPairArg, keylist)]
else:
keys_to_load = keylist if keylist else []
for key_to_load in keys_to_load:
allow_certs = False
key_prefix = None
saved_exc = None
pubkey_or_certs = None
pubkey_to_load: Optional[_KeyArg] = None
certs_to_load: Optional[_CertArg] = None
key: Union['SSHKey', 'SSHKeyPair']
if isinstance(key_to_load, (PurePath, str, bytes)):
allow_certs = True
elif isinstance(key_to_load, tuple):
key_to_load, pubkey_or_certs = key_to_load
try:
if isinstance(key_to_load, (PurePath, str)):
key_prefix = str(key_to_load)
if callable(passphrase):
resolved_passphrase = passphrase(key_prefix)
else:
resolved_passphrase = passphrase
if loop and inspect.isawaitable(resolved_passphrase):
resolved_passphrase = asyncio.run_coroutine_threadsafe(
resolved_passphrase, loop).result()
if allow_certs:
key, certs_to_load = read_private_key_and_certs(
key_to_load, resolved_passphrase,
unsafe_skip_rsa_key_validation)
if not certs_to_load:
certs_to_load = key_prefix + '-cert.pub'
else:
key = read_private_key(key_to_load, resolved_passphrase,
unsafe_skip_rsa_key_validation)
pubkey_to_load = key_prefix + '.pub'
elif isinstance(key_to_load, bytes):
if allow_certs:
key, certs_to_load = import_private_key_and_certs(
key_to_load, passphrase,
unsafe_skip_rsa_key_validation)
else:
key = import_private_key(key_to_load, passphrase,
unsafe_skip_rsa_key_validation)
else:
key = key_to_load
except KeyImportError as exc:
if skip_public or \
(ignore_encrypted and str(exc).startswith('Passphrase')):
continue
raise
certs: Optional[Sequence[SSHCertificate]]
if pubkey_or_certs:
try:
certs = load_certificates(pubkey_or_certs)
except (TypeError, OSError, KeyImportError) as exc:
saved_exc = exc
certs = None
if not certs:
pubkey_to_load = cast(_KeyArg, pubkey_or_certs)
elif certs_to_load:
try:
certs = load_certificates(certs_to_load)
except (OSError, KeyImportError):
certs = None
else:
certs = None
pubkey: Optional[SSHKey]
if pubkey_to_load:
try:
if isinstance(pubkey_to_load, (PurePath, str)):
pubkey = read_public_key(pubkey_to_load)
elif isinstance(pubkey_to_load, bytes):
pubkey = import_public_key(pubkey_to_load)
else:
pubkey = pubkey_to_load
except (OSError, KeyImportError):
pubkey = None
else:
saved_exc = None
else:
pubkey = None
if saved_exc:
raise saved_exc # pylint: disable=raising-bad-type
if not certs:
if isinstance(key, SSHKeyPair):
pubdata = key.key_public_data
else:
pubdata = key.public_data
cert = certdict.get(pubdata)
if cert and cert.is_x509:
cert = SSHX509CertificateChain.construct_from_certs(certlist)
elif len(certs) == 1 and not certs[0].is_x509:
cert = certs[0]
else:
cert = SSHX509CertificateChain.construct_from_certs(certs)
if isinstance(key, SSHKeyPair):
if cert:
key.set_certificate(cert)
result.append(key)
else:
if cert:
result.append(SSHLocalKeyPair(key, pubkey, cert))
result.append(SSHLocalKeyPair(key, pubkey))
return result
def load_default_keypairs(passphrase: Optional[BytesOrStr] = None,
certlist: CertListArg = ()) -> \
Sequence[SSHKeyPair]:
"""Return a list of default keys from the user's home directory"""
result: List[SSHKeyPair] = []
for file, condition in _DEFAULT_KEY_FILES:
if condition: # pragma: no branch
try:
path = Path('~', '.ssh', file).expanduser()
result.extend(load_keypairs(path, passphrase, certlist,
ignore_encrypted=True))
except OSError:
pass
return result
def load_public_keys(keylist: KeyListArg) -> Sequence[SSHKey]:
"""Load public keys
This function loads a list of SSH public keys.
:param keylist:
The list of public keys to load.
:type keylist: *see* :ref:`SpecifyingPublicKeys`
:returns: A list of :class:`SSHKey` objects
"""
if isinstance(keylist, (PurePath, str)):
return read_public_key_list(keylist)
else:
result: List[SSHKey] = []
for key in keylist:
if isinstance(key, (PurePath, str)):
key = read_public_key(key)
elif isinstance(key, bytes):
key = import_public_key(key)
result.append(key)
return result
def load_default_host_public_keys() -> Sequence[Union[SSHKey, SSHCertificate]]:
"""Return a list of default host public keys or certificates"""
result: List[Union[SSHKey, SSHCertificate]] = []
for host_key_dir in _DEFAULT_HOST_KEY_DIRS:
for file in _DEFAULT_HOST_KEY_FILES:
try:
cert = read_certificate(Path(host_key_dir, file + '-cert.pub'))
except (OSError, KeyImportError):
pass
else:
result.append(cert)
for host_key_dir in _DEFAULT_HOST_KEY_DIRS:
for file in _DEFAULT_HOST_KEY_FILES:
try:
key = read_public_key(Path(host_key_dir, file + '.pub'))
except (OSError, KeyImportError):
pass
else:
result.append(key)
return result
def load_certificates(certlist: CertListArg) -> Sequence[SSHCertificate]:
"""Load certificates
This function loads a list of OpenSSH or X.509 certificates.
:param certlist:
The list of certificates to load.
:type certlist: *see* :ref:`SpecifyingCertificates`
:returns: A list of :class:`SSHCertificate` objects
"""
if isinstance(certlist, SSHCertificate):
return [certlist]
elif isinstance(certlist, (PurePath, str, bytes)):
certlist = [certlist]
result: List[SSHCertificate] = []
for cert in certlist:
if isinstance(cert, (PurePath, str)):
certs = read_certificate_list(cert)
elif isinstance(cert, bytes):
certs = _decode_certificate_list(cert)
elif isinstance(cert, SSHCertificate):
certs = [cert]
else:
certs = cert
result.extend(certs)
return result
def load_identities(keylist: IdentityListArg,
skip_private: bool = False) -> Sequence[bytes]:
"""Load public key and certificate identities"""
if isinstance(keylist, (bytes, str, PurePath, SSHKey, SSHCertificate)):
identities: Sequence[_IdentityArg] = [keylist]
else:
identities = keylist
result = []
for identity in identities:
if isinstance(identity, (PurePath, str)):
try:
pubdata = read_certificate(identity).public_data
except KeyImportError:
try:
pubdata = read_public_key(identity).public_data
except KeyImportError:
if skip_private:
continue
raise
elif isinstance(identity, (SSHKey, SSHCertificate)):
pubdata = identity.public_data
else:
pubdata = identity
result.append(pubdata)
return result
def load_default_identities() -> Sequence[bytes]:
"""Return a list of default public key and certificate identities"""
result: List[bytes] = []
for file, condition in _DEFAULT_KEY_FILES:
if condition: # pragma: no branch
try:
cert = read_certificate(Path('~', '.ssh', file + '-cert.pub'))
except (OSError, KeyImportError):
pass
else:
result.append(cert.public_data)
try:
key = read_public_key(Path('~', '.ssh', file + '.pub'))
except (OSError, KeyImportError):
pass
else:
result.append(key.public_data)
return result
def load_resident_keys(pin: str, *, application: str = 'ssh:',
user: Optional[str] = None,
touch_required: bool = True) -> Sequence[SSHKey]:
"""Load keys resident on attached FIDO2 security keys
This function loads keys resident on any FIDO2 security keys
currently attached to the system. The user name associated
with each key is returned in the key's comment field.
:param pin:
The PIN to use to access the security keys, defaulting to `None`.
:param application: (optional)
The application name associated with the keys to load,
defaulting to `'ssh:'`.
:param user: (optional)
The user name associated with the keys to load. By default,
keys for all users are loaded.
:param touch_required: (optional)
Whether or not to require the user to touch the security key
when authenticating with it, defaulting to `True`.
:type application: `str`
:type user: `str`
:type pin: `str`
:type touch_required: `bool`
"""
flags = SSH_SK_USER_PRESENCE_REQD if touch_required else 0
reserved = b''
try:
resident_keys = sk_get_resident(application, user, pin)
except ValueError as exc:
raise KeyImportError(str(exc)) from None
result: List[SSHKey] = []
for sk_alg, name, public_value, key_handle in resident_keys:
handler, key_params = _sk_alg_map[sk_alg]
key_params += (public_value, application, flags, key_handle, reserved)
key = handler.make_private(key_params)
key.set_comment(name)
result.append(key)
return result
|