1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535
|
/* crypt-gpgme.c - GPGME based crypto operations
* Copyright (C) 1996-7,2007 Michael R. Elkins <me@cs.hmc.edu>
* Copyright (C) 1998,1999,2000 Thomas Roessler <roessler@does-not-exist.org>
* Copyright (C) 2001 Thomas Roessler <roessler@does-not-exist.org>
* Oliver Ehli <elmy@acm.org>
* Copyright (C) 2002, 2003, 2004 g10 Code GmbH
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef CRYPT_BACKEND_GPGME
#include "mutt.h"
#include "mutt_crypt.h"
#include "mutt_menu.h"
#include "mutt_curses.h"
#include "mime.h"
#include "copy.h"
#include "pager.h"
#include "sort.h"
#include <sys/wait.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <ctype.h>
#include <gpgme.h>
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
#ifdef HAVE_LANGINFO_D_T_FMT
#include <langinfo.h>
#endif
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#endif
#ifdef HAVE_SYS_RESOURCE_H
# include <sys/resource.h>
#endif
/*
* Helper macros.
*/
#define digitp(p) (*(p) >= '0' && *(p) <= '9')
#define hexdigitp(a) (digitp (a) \
|| (*(a) >= 'A' && *(a) <= 'F') \
|| (*(a) >= 'a' && *(a) <= 'f'))
#define xtoi_1(p) (*(p) <= '9'? (*(p)- '0'): \
*(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
#define xtoi_2(p) ((xtoi_1(p) * 16) + xtoi_1((p)+1))
#define PKA_NOTATION_NAME "pka-address@gnupg.org"
#define is_pka_notation(notation) (! strcmp ((notation)->name, \
PKA_NOTATION_NAME))
/* Values used for comparing addresses. */
#define CRYPT_KV_VALID 1
#define CRYPT_KV_ADDR 2
#define CRYPT_KV_STRING 4
#define CRYPT_KV_STRONGID 8
#define CRYPT_KV_MATCH (CRYPT_KV_ADDR|CRYPT_KV_STRING)
/* static local variables */
static int GpgmeLocaleSet = 0;
/*
* Type definitions.
*/
struct crypt_cache
{
char *what;
char *dflt;
struct crypt_cache *next;
};
struct dn_array_s
{
char *key;
char *value;
};
/* We work based on user IDs, getting from a user ID to the key is
check and does not need any memory (gpgme uses reference counting). */
typedef struct crypt_keyinfo
{
struct crypt_keyinfo *next;
gpgme_key_t kobj;
int idx; /* and the user ID at this index */
const char *uid; /* and for convenience point to this user ID */
unsigned int flags; /* global and per uid flags (for convenience)*/
} crypt_key_t;
typedef struct crypt_entry
{
size_t num;
crypt_key_t *key;
} crypt_entry_t;
static struct crypt_cache *id_defaults = NULL;
static gpgme_key_t signature_key = NULL;
static char *current_sender = NULL;
/*
* General helper functions.
*/
/* return true when S pints to a didgit or letter. */
static int
digit_or_letter (const unsigned char *s)
{
return ( (*s >= '0' && *s < '9')
|| (*s >= 'A' && *s <= 'Z')
|| (*s >= 'a' && *s <= 'z'));
}
/* Print the utf-8 encoded string BUF of length LEN bytes to stream
FP. Convert the character set. */
static void
print_utf8 (FILE *fp, const char *buf, size_t len)
{
char *tstr;
tstr = safe_malloc (len+1);
memcpy (tstr, buf, len);
tstr[len] = 0;
/* fromcode "utf-8" is sure, so we don't want
* charset-hook corrections: flags must be 0.
*/
mutt_convert_string (&tstr, "utf-8", Charset, 0);
fputs (tstr, fp);
FREE (&tstr);
}
/*
* Key management.
*/
/* Return the keyID for the key K. Note that this string is valid as
long as K is valid */
static const char *crypt_keyid (crypt_key_t *k)
{
const char *s = "????????";
if (k->kobj && k->kobj->subkeys)
{
s = k->kobj->subkeys->keyid;
if ((! option (OPTPGPLONGIDS)) && (strlen (s) == 16))
/* Return only the short keyID. */
s += 8;
}
return s;
}
/* Return the hexstring fingerprint from the key K. */
static const char *crypt_fpr (crypt_key_t *k)
{
const char *s = "";
if (k->kobj && k->kobj->subkeys)
s = k->kobj->subkeys->fpr;
return s;
}
/* Parse FLAGS and return a statically allocated(!) string with them. */
static char *crypt_key_abilities (int flags)
{
static char buff[3];
if (!(flags & KEYFLAG_CANENCRYPT))
buff[0] = '-';
else if (flags & KEYFLAG_PREFER_SIGNING)
buff[0] = '.';
else
buff[0] = 'e';
if (!(flags & KEYFLAG_CANSIGN))
buff[1] = '-';
else if (flags & KEYFLAG_PREFER_ENCRYPTION)
buff[1] = '.';
else
buff[1] = 's';
buff[2] = '\0';
return buff;
}
/* Parse FLAGS and return a character describing the most important flag. */
static char crypt_flags (int flags)
{
if (flags & KEYFLAG_REVOKED)
return 'R';
else if (flags & KEYFLAG_EXPIRED)
return 'X';
else if (flags & KEYFLAG_DISABLED)
return 'd';
else if (flags & KEYFLAG_CRITICAL)
return 'c';
else
return ' ';
}
/* Return a copy of KEY. */
static crypt_key_t *crypt_copy_key (crypt_key_t *key)
{
crypt_key_t *k;
k = safe_calloc (1, sizeof *k);
k->kobj = key->kobj;
gpgme_key_ref (key->kobj);
k->idx = key->idx;
k->uid = key->uid;
k->flags = key->flags;
return k;
}
/* Release all the keys at the address of KEYLIST and set the address
to NULL. */
static void crypt_free_key (crypt_key_t **keylist)
{
while (*keylist)
{
crypt_key_t *k = (*keylist)->next;
FREE (&k);
*keylist = k;
}
}
/* Return trute when key K is valid. */
static int crypt_key_is_valid (crypt_key_t *k)
{
if (k->flags & KEYFLAG_CANTUSE)
return 0;
return 1;
}
/* Return true whe validity of KEY is sufficient. */
static int crypt_id_is_strong (crypt_key_t *key)
{
gpgme_validity_t val = GPGME_VALIDITY_UNKNOWN;
gpgme_user_id_t uid = NULL;
unsigned int is_strong = 0;
unsigned int i = 0;
if ((key->flags & KEYFLAG_ISX509))
return 1;
for (i = 0, uid = key->kobj->uids; (i < key->idx) && uid;
i++, uid = uid->next)
;
if (uid)
val = uid->validity;
switch (val)
{
case GPGME_VALIDITY_UNKNOWN:
case GPGME_VALIDITY_UNDEFINED:
case GPGME_VALIDITY_NEVER:
case GPGME_VALIDITY_MARGINAL:
is_strong = 0;
break;
case GPGME_VALIDITY_FULL:
case GPGME_VALIDITY_ULTIMATE:
is_strong = 1;
break;
}
return is_strong;
}
/* Return true when the KEY is valid, i.e. not marked as unusable. */
static int crypt_id_is_valid (crypt_key_t *key)
{
return ! (key->flags & KEYFLAG_CANTUSE);
}
/* Return a bit vector describing how well the addresses ADDR and
U_ADDR match and whether KEY is valid. */
static int crypt_id_matches_addr (ADDRESS *addr, ADDRESS *u_addr,
crypt_key_t *key)
{
int rv = 0;
if (crypt_id_is_valid (key))
rv |= CRYPT_KV_VALID;
if (crypt_id_is_strong (key))
rv |= CRYPT_KV_STRONGID;
if (addr->mailbox && u_addr->mailbox
&& mutt_strcasecmp (addr->mailbox, u_addr->mailbox) == 0)
rv |= CRYPT_KV_ADDR;
if (addr->personal && u_addr->personal
&& mutt_strcasecmp (addr->personal, u_addr->personal) == 0)
rv |= CRYPT_KV_STRING;
return rv;
}
/*
* GPGME convenient functions.
*/
/* Create a new gpgme context and return it. With FOR_SMIME set to
true, the protocol of the context is set to CMS. */
static gpgme_ctx_t create_gpgme_context (int for_smime)
{
gpgme_error_t err;
gpgme_ctx_t ctx;
if (!GpgmeLocaleSet)
{
gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL));
#ifdef ENABLE_NLS
gpgme_set_locale (NULL, LC_MESSAGES, setlocale (LC_MESSAGES, NULL));
#endif
GpgmeLocaleSet = 1;
}
err = gpgme_new (&ctx);
if (err)
{
mutt_error (_("error creating gpgme context: %s\n"), gpgme_strerror (err));
sleep (2);
mutt_exit (1);
}
if (for_smime)
{
err = gpgme_set_protocol (ctx, GPGME_PROTOCOL_CMS);
if (err)
{
mutt_error (_("error enabling CMS protocol: %s\n"),
gpgme_strerror (err));
sleep (2);
mutt_exit (1);
}
}
return ctx;
}
/* Create a new gpgme data object. This is a wrapper to die on
error. */
static gpgme_data_t create_gpgme_data (void)
{
gpgme_error_t err;
gpgme_data_t data;
err = gpgme_data_new (&data);
if (err)
{
mutt_error (_("error creating gpgme data object: %s\n"),
gpgme_strerror (err));
sleep (2);
mutt_exit (1);
}
return data;
}
/* Create a new GPGME Data object from the mail body A. With CONVERT
passed as true, the lines are converted to CR,LF if required.
Return NULL on error or the gpgme_data_t object on success. */
static gpgme_data_t body_to_data_object (BODY *a, int convert)
{
char tempfile[_POSIX_PATH_MAX];
FILE *fptmp;
int err = 0;
gpgme_data_t data;
mutt_mktemp (tempfile);
fptmp = safe_fopen (tempfile, "w+");
if (!fptmp)
{
mutt_perror (tempfile);
return NULL;
}
mutt_write_mime_header (a, fptmp);
fputc ('\n', fptmp);
mutt_write_mime_body (a, fptmp);
if (convert)
{
int c, hadcr = 0;
unsigned char buf[1];
data = create_gpgme_data ();
rewind (fptmp);
while ((c = fgetc (fptmp)) != EOF)
{
if (c == '\r')
hadcr = 1;
else
{
if (c == '\n' && !hadcr)
{
buf[0] = '\r';
gpgme_data_write (data, buf, 1);
}
hadcr = 0;
}
/* FIXME: This is quite suboptimal */
buf[0] = c;
gpgme_data_write (data, buf, 1);
}
safe_fclose (&fptmp);
gpgme_data_seek (data, 0, SEEK_SET);
}
else
{
safe_fclose (&fptmp);
err = gpgme_data_new_from_file (&data, tempfile, 1);
}
unlink (tempfile);
if (err)
{
mutt_error (_("error allocating data object: %s\n"), gpgme_strerror (err));
return NULL;
}
return data;
}
/* Create a GPGME data object from the stream FP but limit the object
to LENGTH bytes starting at OFFSET bytes from the beginning of the
file. */
static gpgme_data_t file_to_data_object (FILE *fp, long offset, long length)
{
int err = 0;
gpgme_data_t data;
err = gpgme_data_new_from_filepart (&data, NULL, fp, offset, length);
if (err)
{
mutt_error (_("error allocating data object: %s\n"), gpgme_strerror (err));
return NULL;
}
return data;
}
/* Write a GPGME data object to the stream FP. */
static int data_object_to_stream (gpgme_data_t data, FILE *fp)
{
int err;
char buf[4096], *p;
ssize_t nread;
err = ((gpgme_data_seek (data, 0, SEEK_SET) == -1)
? gpgme_error_from_errno (errno) : 0);
if (err)
{
mutt_error (_("error rewinding data object: %s\n"), gpgme_strerror (err));
return -1;
}
while ((nread = gpgme_data_read (data, buf, sizeof (buf))))
{
/* fixme: we are not really converting CRLF to LF but just
skipping CR. Doing it correctly needs a more complex logic */
for (p=buf; nread; p++, nread--)
{
if (*p != '\r')
putc (*p, fp);
}
if (ferror (fp))
{
mutt_perror ("[tempfile]");
return -1;
}
}
if (nread == -1)
{
mutt_error (_("error reading data object: %s\n"), strerror (errno));
return -1;
}
return 0;
}
/* Copy a data object to a newly created temporay file and return that
filename. Caller must free. With RET_FP not NULL, don't close the
stream but return it there. */
static char *data_object_to_tempfile (gpgme_data_t data, FILE **ret_fp)
{
int err;
char tempfile[_POSIX_PATH_MAX];
FILE *fp;
size_t nread = 0;
mutt_mktemp (tempfile);
fp = safe_fopen (tempfile, "w+");
if (!fp)
{
mutt_perror (tempfile);
return NULL;
}
err = ((gpgme_data_seek (data, 0, SEEK_SET) == -1)
? gpgme_error_from_errno (errno) : 0);
if (!err)
{
char buf[4096];
while ((nread = gpgme_data_read (data, buf, sizeof (buf))))
{
if (fwrite (buf, nread, 1, fp) != 1)
{
mutt_perror (tempfile);
safe_fclose (&fp);
unlink (tempfile);
return NULL;
}
}
}
if (ret_fp)
rewind (fp);
else
safe_fclose (&fp);
if (nread == -1)
{
mutt_error (_("error reading data object: %s\n"), gpgme_strerror (err));
unlink (tempfile);
safe_fclose (&fp);
return NULL;
}
if (ret_fp)
*ret_fp = fp;
return safe_strdup (tempfile);
}
/* Create a GpgmeRecipientSet from the keys in the string KEYLIST.
The keys must be space delimited. */
static gpgme_key_t *create_recipient_set (const char *keylist,
gpgme_protocol_t protocol)
{
int err;
const char *s;
char buf[100];
int i;
gpgme_key_t *rset = NULL;
unsigned int rset_n = 0;
gpgme_key_t key = NULL;
gpgme_ctx_t context = NULL;
err = gpgme_new (&context);
if (! err)
err = gpgme_set_protocol (context, protocol);
if (! err)
{
s = keylist;
do {
while (*s == ' ')
s++;
for (i=0; *s && *s != ' ' && i < sizeof(buf)-1;)
buf[i++] = *s++;
buf[i] = 0;
if (*buf)
{
if (i>1 && buf[i-1] == '!')
{
/* The user selected to override the valididy of that
key. */
buf[i-1] = 0;
err = gpgme_get_key (context, buf, &key, 0);
if (! err)
key->uids->validity = GPGME_VALIDITY_FULL;
buf[i-1] = '!';
}
else
err = gpgme_get_key (context, buf, &key, 0);
if (! err)
{
safe_realloc (&rset, sizeof (*rset) * (rset_n + 1));
rset[rset_n++] = key;
}
else
{
mutt_error (_("error adding recipient `%s': %s\n"),
buf, gpgme_strerror (err));
FREE (&rset);
return NULL;
}
}
} while (*s);
}
/* NULL terminate. */
safe_realloc (&rset, sizeof (*rset) * (rset_n + 1));
rset[rset_n++] = NULL;
if (context)
gpgme_release (context);
return rset;
}
/* Make sure that the correct signer is set. Returns 0 on success. */
static int set_signer (gpgme_ctx_t ctx, int for_smime)
{
char *signid = for_smime ? SmimeDefaultKey: PgpSignAs;
gpgme_error_t err;
gpgme_ctx_t listctx;
gpgme_key_t key, key2;
if (!signid || !*signid)
return 0;
listctx = create_gpgme_context (for_smime);
err = gpgme_op_keylist_start (listctx, signid, 1);
if (!err)
err = gpgme_op_keylist_next (listctx, &key);
if (err)
{
gpgme_release (listctx);
mutt_error (_("secret key `%s' not found: %s\n"),
signid, gpgme_strerror (err));
return -1;
}
err = gpgme_op_keylist_next (listctx, &key2);
if (!err)
{
gpgme_key_release (key);
gpgme_key_release (key2);
gpgme_release (listctx);
mutt_error (_("ambiguous specification of secret key `%s'\n"),
signid);
return -1;
}
gpgme_op_keylist_end (listctx);
gpgme_release (listctx);
gpgme_signers_clear (ctx);
err = gpgme_signers_add (ctx, key);
gpgme_key_release (key);
if (err)
{
mutt_error (_("error setting secret key `%s': %s\n"),
signid, gpgme_strerror (err));
return -1;
}
return 0;
}
static gpgme_error_t
set_pka_sig_notation (gpgme_ctx_t ctx)
{
gpgme_error_t err;
err = gpgme_sig_notation_add (ctx,
PKA_NOTATION_NAME, current_sender, 0);
if (err)
{
mutt_error (_("error setting PKA signature notation: %s\n"),
gpgme_strerror (err));
mutt_sleep (2);
}
return err;
}
/* Encrypt the gpgme data object PLAINTEXT to the recipients in RSET
and return an allocated filename to a temporary file containing the
enciphered text. With USE_SMIME set to true, the smime backend is
used. With COMBINED_SIGNED a PGP message is signed and
encrypted. Returns NULL in case of error */
static char *encrypt_gpgme_object (gpgme_data_t plaintext, gpgme_key_t *rset,
int use_smime, int combined_signed)
{
gpgme_error_t err;
gpgme_ctx_t ctx;
gpgme_data_t ciphertext;
char *outfile;
ctx = create_gpgme_context (use_smime);
if (!use_smime)
gpgme_set_armor (ctx, 1);
ciphertext = create_gpgme_data ();
if (combined_signed)
{
if (set_signer (ctx, use_smime))
{
gpgme_data_release (ciphertext);
gpgme_release (ctx);
return NULL;
}
if (option (OPTCRYPTUSEPKA))
{
err = set_pka_sig_notation (ctx);
if (err)
{
gpgme_data_release (ciphertext);
gpgme_release (ctx);
return NULL;
}
}
err = gpgme_op_encrypt_sign (ctx, rset, GPGME_ENCRYPT_ALWAYS_TRUST,
plaintext, ciphertext);
}
else
err = gpgme_op_encrypt (ctx, rset, GPGME_ENCRYPT_ALWAYS_TRUST,
plaintext, ciphertext);
mutt_need_hard_redraw ();
if (err)
{
mutt_error (_("error encrypting data: %s\n"), gpgme_strerror (err));
gpgme_data_release (ciphertext);
gpgme_release (ctx);
return NULL;
}
gpgme_release (ctx);
outfile = data_object_to_tempfile (ciphertext, NULL);
gpgme_data_release (ciphertext);
return outfile;
}
/* Find the "micalg" parameter from the last Gpgme operation on
context CTX. It is expected that this operation was a sign
operation. Return the algorithm name as a C string in buffer BUF
which must have been allocated by the caller with size BUFLEN.
Returns 0 on success or -1 in case of an error. The return string
is truncted to BUFLEN - 1. */
static int get_micalg (gpgme_ctx_t ctx, int use_smime, char *buf, size_t buflen)
{
gpgme_sign_result_t result = NULL;
const char *algorithm_name = NULL;
if (buflen < 5)
return -1;
*buf = 0;
result = gpgme_op_sign_result (ctx);
if (result && result->signatures)
{
algorithm_name = gpgme_hash_algo_name (result->signatures->hash_algo);
if (algorithm_name)
{
if (use_smime)
{
/* convert GPGME raw hash name to RFC 2633 format */
snprintf (buf, buflen, "%s", algorithm_name);
ascii_strlower (buf);
} else {
/* convert GPGME raw hash name to RFC 3156 format */
snprintf (buf, buflen, "pgp-%s", algorithm_name);
ascii_strlower (buf + 4);
}
}
}
return *buf? 0:-1;
}
static void print_time(time_t t, STATE *s)
{
char p[STRING];
setlocale (LC_TIME, "");
#ifdef HAVE_LANGINFO_D_T_FMT
strftime (p, sizeof (p), nl_langinfo (D_T_FMT), localtime (&t));
#else
strftime (p, sizeof (p), "%c", localtime (&t));
#endif
setlocale (LC_TIME, "C");
state_attach_puts (p, s);
}
/*
* Implementation of `sign_message'.
*/
/* Sign the MESSAGE in body A either using OpenPGP or S/MIME when
USE_SMIME is passed as true. Returns the new body or NULL on
error. */
static BODY *sign_message (BODY *a, int use_smime)
{
BODY *t;
char *sigfile;
int err = 0;
char buf[100];
gpgme_ctx_t ctx;
gpgme_data_t message, signature;
convert_to_7bit (a); /* Signed data _must_ be in 7-bit format. */
message = body_to_data_object (a, 1);
if (!message)
return NULL;
signature = create_gpgme_data ();
ctx = create_gpgme_context (use_smime);
if (!use_smime)
gpgme_set_armor (ctx, 1);
if (set_signer (ctx, use_smime))
{
gpgme_data_release (signature);
gpgme_release (ctx);
return NULL;
}
if (option (OPTCRYPTUSEPKA))
{
err = set_pka_sig_notation (ctx);
if (err)
{
gpgme_data_release (signature);
gpgme_data_release (message);
gpgme_release (ctx);
return NULL;
}
}
err = gpgme_op_sign (ctx, message, signature, GPGME_SIG_MODE_DETACH );
mutt_need_hard_redraw ();
gpgme_data_release (message);
if (err)
{
gpgme_data_release (signature);
gpgme_release (ctx);
mutt_error (_("error signing data: %s\n"), gpgme_strerror (err));
return NULL;
}
sigfile = data_object_to_tempfile (signature, NULL);
gpgme_data_release (signature);
if (!sigfile)
{
gpgme_release (ctx);
return NULL;
}
t = mutt_new_body ();
t->type = TYPEMULTIPART;
t->subtype = safe_strdup ("signed");
t->encoding = ENC7BIT;
t->use_disp = 0;
t->disposition = DISPINLINE;
mutt_generate_boundary (&t->parameter);
mutt_set_parameter ("protocol",
use_smime? "application/pkcs7-signature"
: "application/pgp-signature",
&t->parameter);
/* Get the micalg from gpgme. Old gpgme versions don't support this
for S/MIME so we assume sha-1 in this case. */
if (!get_micalg (ctx, use_smime, buf, sizeof buf))
mutt_set_parameter ("micalg", buf, &t->parameter);
else if (use_smime)
mutt_set_parameter ("micalg", "sha1", &t->parameter);
gpgme_release (ctx);
t->parts = a;
a = t;
t->parts->next = mutt_new_body ();
t = t->parts->next;
t->type = TYPEAPPLICATION;
if (use_smime)
{
t->subtype = safe_strdup ("pkcs7-signature");
mutt_set_parameter ("name", "smime.p7s", &t->parameter);
t->encoding = ENCBASE64;
t->use_disp = 1;
t->disposition = DISPATTACH;
t->d_filename = safe_strdup ("smime.p7s");
}
else
{
t->subtype = safe_strdup ("pgp-signature");
t->use_disp = 0;
t->disposition = DISPINLINE;
t->encoding = ENC7BIT;
}
t->filename = sigfile;
t->unlink = 1; /* ok to remove this file after sending. */
return a;
}
BODY *pgp_gpgme_sign_message (BODY *a)
{
return sign_message (a, 0);
}
BODY *smime_gpgme_sign_message (BODY *a)
{
return sign_message (a, 1);
}
/*
* Implementation of `encrypt_message'.
*/
/* Encrypt the mail body A to all keys given as space separated keyids
or fingerprints in KEYLIST and return the encrypted body. */
BODY *pgp_gpgme_encrypt_message (BODY *a, char *keylist, int sign)
{
char *outfile = NULL;
BODY *t;
gpgme_key_t *rset = NULL;
gpgme_data_t plaintext;
rset = create_recipient_set (keylist, GPGME_PROTOCOL_OpenPGP);
if (!rset)
return NULL;
if (sign)
convert_to_7bit (a);
plaintext = body_to_data_object (a, 0);
if (!plaintext)
{
FREE (&rset);
return NULL;
}
outfile = encrypt_gpgme_object (plaintext, rset, 0, sign);
gpgme_data_release (plaintext);
FREE (&rset);
if (!outfile)
return NULL;
t = mutt_new_body ();
t->type = TYPEMULTIPART;
t->subtype = safe_strdup ("encrypted");
t->encoding = ENC7BIT;
t->use_disp = 0;
t->disposition = DISPINLINE;
mutt_generate_boundary(&t->parameter);
mutt_set_parameter("protocol", "application/pgp-encrypted", &t->parameter);
t->parts = mutt_new_body ();
t->parts->type = TYPEAPPLICATION;
t->parts->subtype = safe_strdup ("pgp-encrypted");
t->parts->encoding = ENC7BIT;
t->parts->next = mutt_new_body ();
t->parts->next->type = TYPEAPPLICATION;
t->parts->next->subtype = safe_strdup ("octet-stream");
t->parts->next->encoding = ENC7BIT;
t->parts->next->filename = outfile;
t->parts->next->use_disp = 1;
t->parts->next->disposition = DISPINLINE;
t->parts->next->unlink = 1; /* delete after sending the message */
t->parts->next->d_filename = safe_strdup ("msg.asc"); /* non pgp/mime
can save */
return t;
}
/*
* Implementation of `smime_build_smime_entity'.
*/
/* Encrypt the mail body A to all keys given as space separated
fingerprints in KEYLIST and return the S/MIME encrypted body. */
BODY *smime_gpgme_build_smime_entity (BODY *a, char *keylist)
{
char *outfile = NULL;
BODY *t;
gpgme_key_t *rset = NULL;
gpgme_data_t plaintext;
rset = create_recipient_set (keylist, GPGME_PROTOCOL_CMS);
if (!rset)
return NULL;
plaintext = body_to_data_object (a, 0);
if (!plaintext)
{
FREE (&rset);
return NULL;
}
outfile = encrypt_gpgme_object (plaintext, rset, 1, 0);
gpgme_data_release (plaintext);
FREE (&rset);
if (!outfile)
return NULL;
t = mutt_new_body ();
t->type = TYPEAPPLICATION;
t->subtype = safe_strdup ("pkcs7-mime");
mutt_set_parameter ("name", "smime.p7m", &t->parameter);
mutt_set_parameter ("smime-type", "enveloped-data", &t->parameter);
t->encoding = ENCBASE64; /* The output of OpenSSL SHOULD be binary */
t->use_disp = 1;
t->disposition = DISPATTACH;
t->d_filename = safe_strdup ("smime.p7m");
t->filename = outfile;
t->unlink = 1; /*delete after sending the message */
t->parts=0;
t->next=0;
return t;
}
/*
* Implementation of `verify_one'.
*/
/* Display the common attributes of the signature summary SUM.
Return 1 if there is is a severe warning.
*/
static int show_sig_summary (unsigned long sum,
gpgme_ctx_t ctx, gpgme_key_t key, int idx,
STATE *s, gpgme_signature_t sig)
{
int severe = 0;
if ((sum & GPGME_SIGSUM_KEY_REVOKED))
{
state_attach_puts (_("Warning: One of the keys has been revoked\n"),s);
severe = 1;
}
if ((sum & GPGME_SIGSUM_KEY_EXPIRED))
{
time_t at = key->subkeys->expires ? key->subkeys->expires : 0;
if (at)
{
state_attach_puts (_("Warning: The key used to create the "
"signature expired at: "), s);
print_time (at , s);
state_attach_puts ("\n", s);
}
else
state_attach_puts (_("Warning: At least one certification key "
"has expired\n"), s);
}
if ((sum & GPGME_SIGSUM_SIG_EXPIRED))
{
gpgme_verify_result_t result;
gpgme_signature_t sig;
unsigned int i;
result = gpgme_op_verify_result (ctx);
for (sig = result->signatures, i = 0; sig && (i < idx);
sig = sig->next, i++)
;
state_attach_puts (_("Warning: The signature expired at: "), s);
print_time (sig ? sig->exp_timestamp : 0, s);
state_attach_puts ("\n", s);
}
if ((sum & GPGME_SIGSUM_KEY_MISSING))
state_attach_puts (_("Can't verify due to a missing "
"key or certificate\n"), s);
if ((sum & GPGME_SIGSUM_CRL_MISSING))
{
state_attach_puts (_("The CRL is not available\n"), s);
severe = 1;
}
if ((sum & GPGME_SIGSUM_CRL_TOO_OLD))
{
state_attach_puts (_("Available CRL is too old\n"), s);
severe = 1;
}
if ((sum & GPGME_SIGSUM_BAD_POLICY))
state_attach_puts (_("A policy requirement was not met\n"), s);
if ((sum & GPGME_SIGSUM_SYS_ERROR))
{
const char *t0 = NULL, *t1 = NULL;
gpgme_verify_result_t result;
gpgme_signature_t sig;
unsigned int i;
state_attach_puts (_("A system error occurred"), s );
/* Try to figure out some more detailed system error information. */
result = gpgme_op_verify_result (ctx);
for (sig = result->signatures, i = 0; sig && (i < idx);
sig = sig->next, i++)
;
if (sig)
{
t0 = "";
t1 = sig->wrong_key_usage ? "Wrong_Key_Usage" : "";
}
if (t0 || t1)
{
state_attach_puts (": ", s);
if (t0)
state_attach_puts (t0, s);
if (t1 && !(t0 && !strcmp (t0, t1)))
{
if (t0)
state_attach_puts (",", s);
state_attach_puts (t1, s);
}
}
state_attach_puts ("\n", s);
}
#ifdef HAVE_GPGME_PKA_TRUST
if (option (OPTCRYPTUSEPKA))
{
if (sig->pka_trust == 1 && sig->pka_address)
{
state_attach_puts (_("WARNING: PKA entry does not match "
"signer's address: "), s);
state_attach_puts (sig->pka_address, s);
state_attach_puts ("\n", s);
}
else if (sig->pka_trust == 2 && sig->pka_address)
{
state_attach_puts (_("PKA verified signer's address is: "), s);
state_attach_puts (sig->pka_address, s);
state_attach_puts ("\n", s);
}
}
#endif
return severe;
}
static void show_fingerprint (gpgme_key_t key, STATE *state)
{
const char *s;
int i, is_pgp;
char *buf, *p;
const char *prefix = _("Fingerprint: ");
if (!key)
return;
s = key->subkeys ? key->subkeys->fpr : NULL;
if (!s)
return;
is_pgp = (key->protocol == GPGME_PROTOCOL_OpenPGP);
buf = safe_malloc ( strlen (prefix) + strlen(s) * 4 + 2 );
strcpy (buf, prefix); /* __STRCPY_CHECKED__ */
p = buf + strlen (buf);
if (is_pgp && strlen (s) == 40)
{ /* PGP v4 style formatted. */
for (i=0; *s && s[1] && s[2] && s[3] && s[4]; s += 4, i++)
{
*p++ = s[0];
*p++ = s[1];
*p++ = s[2];
*p++ = s[3];
*p++ = ' ';
if (i == 4)
*p++ = ' ';
}
}
else
{
for (i=0; *s && s[1] && s[2]; s += 2, i++)
{
*p++ = s[0];
*p++ = s[1];
*p++ = is_pgp? ' ':':';
if (is_pgp && i == 7)
*p++ = ' ';
}
}
/* just in case print remaining odd digits */
for (; *s; s++)
*p++ = *s;
*p++ = '\n';
*p = 0;
state_attach_puts (buf, state);
FREE (&buf);
}
/* Show the valididy of a key used for one signature. */
static void show_one_sig_validity (gpgme_ctx_t ctx, int idx, STATE *s)
{
gpgme_verify_result_t result = NULL;
gpgme_signature_t sig = NULL;
const char *txt = NULL;
result = gpgme_op_verify_result (ctx);
if (result)
for (sig = result->signatures; sig && (idx > 0); sig = sig->next, idx--);
switch (sig ? sig->validity : 0)
{
case GPGME_VALIDITY_UNKNOWN:
txt = _("WARNING: We have NO indication whether "
"the key belongs to the person named "
"as shown above\n");
break;
case GPGME_VALIDITY_UNDEFINED:
break;
case GPGME_VALIDITY_NEVER:
txt = _("WARNING: The key does NOT BELONG to "
"the person named as shown above\n");
break;
case GPGME_VALIDITY_MARGINAL:
txt = _("WARNING: It is NOT certain that the key "
"belongs to the person named as shown above\n");
break;
case GPGME_VALIDITY_FULL:
case GPGME_VALIDITY_ULTIMATE:
txt = NULL;
break;
}
if (txt)
state_attach_puts (txt, s);
}
static void print_smime_keyinfo (const char* msg, gpgme_signature_t sig,
gpgme_key_t key, STATE *s)
{
size_t msglen;
gpgme_user_id_t uids = NULL;
int i, aka = 0;
state_attach_puts (msg, s);
state_attach_puts (" ", s);
for (uids = key->uids; uids; uids = uids->next)
{
if (uids->revoked)
continue;
if (aka)
{
msglen = mutt_strlen (msg) - 4;
for (i = 0; i < msglen; i++)
state_attach_puts(" ", s);
state_attach_puts(_("aka: "), s);
}
state_attach_puts (uids->uid, s);
state_attach_puts ("\n", s);
aka = 1;
}
msglen = mutt_strlen (msg) - 8;
for (i = 0; i < msglen; i++)
state_attach_puts(" ", s);
state_attach_puts (_("created: "), s);
print_time (sig->timestamp, s);
state_attach_puts ("\n", s);
}
/* Show information about one signature. This fucntion is called with
the context CTX of a sucessful verification operation and the
enumerator IDX which should start at 0 and incremete for each
call/signature.
Return values are: 0 for normal procession, 1 for a bad signature,
2 for a signature with a warning or -1 for no more signature. */
static int show_one_sig_status (gpgme_ctx_t ctx, int idx, STATE *s)
{
const char *fpr, *uid;
gpgme_key_t key = NULL;
int i, anybad = 0, anywarn = 0;
unsigned int sum;
gpgme_verify_result_t result;
gpgme_signature_t sig;
gpgme_error_t err = GPG_ERR_NO_ERROR;
result = gpgme_op_verify_result (ctx);
if (result)
{
/* FIXME: this code should use a static variable and remember
the current position in the list of signatures, IMHO.
-moritz. */
for (i = 0, sig = result->signatures; sig && (i < idx);
i++, sig = sig->next)
;
if (! sig)
return -1; /* Signature not found. */
if (signature_key)
{
gpgme_key_release (signature_key);
signature_key = NULL;
}
fpr = sig->fpr;
sum = sig->summary;
if (gpg_err_code (sig->status) != GPG_ERR_NO_ERROR)
anybad = 1;
err = gpgme_get_key (ctx, fpr, &key, 0); /* secret key? */
if (! err)
{
uid = (key->uids && key->uids->uid) ? key->uids->uid : "[?]";
if (! signature_key)
signature_key = key;
}
else
{
key = NULL; /* Old gpgme versions did not set KEY to NULL on
error. Do it here to avoid a double free. */
uid = "[?]";
}
if (!s || !s->fpout || !(s->flags & M_DISPLAY))
; /* No state information so no way to print anything. */
else if (err)
{
state_attach_puts (_("Error getting key information: "), s);
state_attach_puts ( gpg_strerror (err), s );
state_attach_puts ("\n", s);
anybad = 1;
}
else if ((sum & GPGME_SIGSUM_GREEN))
{
print_smime_keyinfo (_("Good signature from:"), sig, key, s);
if (show_sig_summary (sum, ctx, key, idx, s, sig))
anywarn = 1;
show_one_sig_validity (ctx, idx, s);
}
else if ((sum & GPGME_SIGSUM_RED))
{
print_smime_keyinfo (_("*BAD* signature from:"), sig, key, s);
show_sig_summary (sum, ctx, key, idx, s, sig);
}
else if (!anybad && key && (key->protocol == GPGME_PROTOCOL_OpenPGP))
{ /* We can't decide (yellow) but this is a PGP key with a good
signature, so we display what a PGP user expects: The name,
fingerprint and the key validity (which is neither fully or
ultimate). */
print_smime_keyinfo (_("Good signature from:"), sig, key, s);
show_one_sig_validity (ctx, idx, s);
show_fingerprint (key,s);
if (show_sig_summary (sum, ctx, key, idx, s, sig))
anywarn = 1;
}
else /* can't decide (yellow) */
{
print_smime_keyinfo (_("Problem signature from:"), sig, key, s);
state_attach_puts (_(" expires: "), s);
print_time (sig->exp_timestamp, s);
state_attach_puts ("\n", s);
show_sig_summary (sum, ctx, key, idx, s, sig);
anywarn = 1;
}
if (key != signature_key)
gpgme_key_release (key);
}
return anybad ? 1 : anywarn ? 2 : 0;
}
/* Do the actual verification step. With IS_SMIME set to true we
assume S/MIME (surprise!) */
static int verify_one (BODY *sigbdy, STATE *s,
const char *tempfile, int is_smime)
{
int badsig = -1;
int anywarn = 0;
int err;
gpgme_ctx_t ctx;
gpgme_data_t signature, message;
signature = file_to_data_object (s->fpin, sigbdy->offset, sigbdy->length);
if (!signature)
return -1;
/* We need to tell gpgme about the encoding because the backend can't
auto-detect plain base-64 encoding which is used by S/MIME. */
if (is_smime)
gpgme_data_set_encoding (signature, GPGME_DATA_ENCODING_BASE64);
err = gpgme_data_new_from_file (&message, tempfile, 1);
if (err)
{
gpgme_data_release (signature);
mutt_error (_("error allocating data object: %s\n"), gpgme_strerror (err));
return -1;
}
ctx = create_gpgme_context (is_smime);
/* Note: We don't need a current time output because GPGME avoids
such an attack by separating the meta information from the
data. */
state_attach_puts (_("[-- Begin signature information --]\n"), s);
err = gpgme_op_verify (ctx, signature, message, NULL);
mutt_need_hard_redraw ();
if (err)
{
char buf[200];
snprintf (buf, sizeof(buf)-1,
_("Error: verification failed: %s\n"),
gpgme_strerror (err));
state_attach_puts (buf, s);
}
else
{ /* Verification succeeded, see what the result is. */
int res, idx;
int anybad = 0;
if (signature_key)
{
gpgme_key_release (signature_key);
signature_key = NULL;
}
for(idx=0; (res = show_one_sig_status (ctx, idx, s)) != -1; idx++)
{
if (res == 1)
anybad = 1;
else if (res == 2)
anywarn = 2;
}
if (!anybad)
badsig = 0;
}
if (!badsig)
{
gpgme_verify_result_t result;
gpgme_sig_notation_t notation;
gpgme_signature_t signature;
int non_pka_notations;
result = gpgme_op_verify_result (ctx);
if (result)
{
for (signature = result->signatures; signature;
signature = signature->next)
{
non_pka_notations = 0;
for (notation = signature->notations; notation;
notation = notation->next)
if (! is_pka_notation (notation))
non_pka_notations++;
if (non_pka_notations)
{
char buf[SHORT_STRING];
snprintf (buf, sizeof (buf),
_("*** Begin Notation (signature by: %s) ***\n"),
signature->fpr);
state_attach_puts (buf, s);
for (notation = signature->notations; notation;
notation = notation->next)
{
if (is_pka_notation (notation))
continue;
if (notation->name)
{
state_attach_puts (notation->name, s);
state_attach_puts ("=", s);
}
if (notation->value)
{
state_attach_puts (notation->value, s);
if (!(*notation->value
&& (notation->value[strlen (notation->value)-1]=='\n')))
state_attach_puts ("\n", s);
}
}
state_attach_puts (_("*** End Notation ***\n"), s);
}
}
}
}
gpgme_release (ctx);
state_attach_puts (_("[-- End signature information --]\n\n"), s);
dprint (1, (debugfile, "verify_one: returning %d.\n", badsig));
return badsig? 1: anywarn? 2 : 0;
}
int pgp_gpgme_verify_one (BODY *sigbdy, STATE *s, const char *tempfile)
{
return verify_one (sigbdy, s, tempfile, 0);
}
int smime_gpgme_verify_one (BODY *sigbdy, STATE *s, const char *tempfile)
{
return verify_one (sigbdy, s, tempfile, 1);
}
/*
* Implementation of `decrypt_part'.
*/
/* Decrypt a PGP or SMIME message (depending on the boolean flag
IS_SMIME) with body A described further by state S. Write
plaintext out to file FPOUT and return a new body. For PGP returns
a flag in R_IS_SIGNED to indicate whether this is a combined
encrypted and signed message, for S/MIME it returns true when it is
not a encrypted but a signed message. */
static BODY *decrypt_part (BODY *a, STATE *s, FILE *fpout, int is_smime,
int *r_is_signed)
{
struct stat info;
BODY *tattach;
int err = 0;
gpgme_ctx_t ctx;
gpgme_data_t ciphertext, plaintext;
int maybe_signed = 0;
int anywarn = 0;
int sig_stat = 0;
if (r_is_signed)
*r_is_signed = 0;
ctx = create_gpgme_context (is_smime);
restart:
/* Make a data object from the body, create context etc. */
ciphertext = file_to_data_object (s->fpin, a->offset, a->length);
if (!ciphertext)
return NULL;
plaintext = create_gpgme_data ();
/* Do the decryption or the verification in case of the S/MIME hack. */
if ((! is_smime) || maybe_signed)
{
if (! is_smime)
err = gpgme_op_decrypt_verify (ctx, ciphertext, plaintext);
else if (maybe_signed)
err = gpgme_op_verify (ctx, ciphertext, NULL, plaintext);
if (err == GPG_ERR_NO_ERROR)
{
/* Check whether signatures have been verified. */
gpgme_verify_result_t verify_result = gpgme_op_verify_result (ctx);
if (verify_result->signatures)
sig_stat = 1;
}
}
else
err = gpgme_op_decrypt (ctx, ciphertext, plaintext);
gpgme_data_release (ciphertext);
if (err)
{
if (is_smime && !maybe_signed
&& gpg_err_code (err) == GPG_ERR_NO_DATA)
{
/* Check whether this might be a signed message despite what
the mime header told us. Retry then. gpgsm returns the
error information "unsupported Algorithm '?'" but gpgme
will not store this unknown algorithm, thus we test that
it has not been set. */
gpgme_decrypt_result_t result;
result = gpgme_op_decrypt_result (ctx);
if (!result->unsupported_algorithm)
{
maybe_signed = 1;
gpgme_data_release (plaintext);
goto restart;
}
}
mutt_need_hard_redraw ();
if ((s->flags & M_DISPLAY))
{
char buf[200];
snprintf (buf, sizeof(buf)-1,
_("[-- Error: decryption failed: %s --]\n\n"),
gpgme_strerror (err));
state_attach_puts (buf, s);
}
gpgme_data_release (plaintext);
gpgme_release (ctx);
return NULL;
}
mutt_need_hard_redraw ();
/* Read the output from GPGME, and make sure to change CRLF to LF,
otherwise read_mime_header has a hard time parsing the message. */
if (data_object_to_stream (plaintext, fpout))
{
gpgme_data_release (plaintext);
gpgme_release (ctx);
return NULL;
}
gpgme_data_release (plaintext);
a->is_signed_data = 0;
if (sig_stat)
{
int res, idx;
int anybad = 0;
if (maybe_signed)
a->is_signed_data = 1;
if(r_is_signed)
*r_is_signed = -1; /* A signature exists. */
if ((s->flags & M_DISPLAY))
state_attach_puts (_("[-- Begin signature "
"information --]\n"), s);
for(idx = 0; (res = show_one_sig_status (ctx, idx, s)) != -1; idx++)
{
if (res == 1)
anybad = 1;
else if (res == 2)
anywarn = 1;
}
if (!anybad && idx && r_is_signed && *r_is_signed)
*r_is_signed = anywarn? 2:1; /* Good signature. */
if ((s->flags & M_DISPLAY))
state_attach_puts (_("[-- End signature "
"information --]\n\n"), s);
}
gpgme_release (ctx); ctx = NULL;
fflush (fpout);
rewind (fpout);
tattach = mutt_read_mime_header (fpout, 0);
if (tattach)
{
/*
* Need to set the length of this body part.
*/
fstat (fileno (fpout), &info);
tattach->length = info.st_size - tattach->offset;
tattach->warnsig = anywarn;
/* See if we need to recurse on this MIME part. */
mutt_parse_part (fpout, tattach);
}
return tattach;
}
/* Decrypt a PGP/MIME message in FPIN and B and return a new body and
the stream in CUR and FPOUT. Returns 0 on success. */
int pgp_gpgme_decrypt_mime (FILE *fpin, FILE **fpout, BODY *b, BODY **cur)
{
char tempfile[_POSIX_PATH_MAX];
STATE s;
BODY *first_part = b;
int is_signed;
first_part->goodsig = 0;
first_part->warnsig = 0;
if(!mutt_is_multipart_encrypted(b))
return -1;
if(!b->parts || !b->parts->next)
return -1;
b = b->parts->next;
memset (&s, 0, sizeof (s));
s.fpin = fpin;
mutt_mktemp (tempfile);
if (!(*fpout = safe_fopen (tempfile, "w+")))
{
mutt_perror (tempfile);
return -1;
}
unlink (tempfile);
*cur = decrypt_part (b, &s, *fpout, 0, &is_signed);
rewind (*fpout);
if (is_signed > 0)
first_part->goodsig = 1;
return *cur? 0:-1;
}
/* Decrypt a S/MIME message in FPIN and B and return a new body and
the stream in CUR and FPOUT. Returns 0 on success. */
int smime_gpgme_decrypt_mime (FILE *fpin, FILE **fpout, BODY *b, BODY **cur)
{
char tempfile[_POSIX_PATH_MAX];
STATE s;
FILE *tmpfp=NULL;
int is_signed;
LOFF_T saved_b_offset;
size_t saved_b_length;
int saved_b_type;
if (!mutt_is_application_smime (b))
return -1;
if (b->parts)
return -1;
/* Decode the body - we need to pass binary CMS to the
backend. The backend allows for Base64 encoded data but it does
not allow for QP which I have seen in some messages. So better
do it here. */
saved_b_type = b->type;
saved_b_offset = b->offset;
saved_b_length = b->length;
memset (&s, 0, sizeof (s));
s.fpin = fpin;
fseeko (s.fpin, b->offset, 0);
mutt_mktemp (tempfile);
if (!(tmpfp = safe_fopen (tempfile, "w+")))
{
mutt_perror (tempfile);
return -1;
}
mutt_unlink (tempfile);
s.fpout = tmpfp;
mutt_decode_attachment (b, &s);
fflush (tmpfp);
b->length = ftello (s.fpout);
b->offset = 0;
rewind (tmpfp);
memset (&s, 0, sizeof (s));
s.fpin = tmpfp;
s.fpout = 0;
mutt_mktemp (tempfile);
if (!(*fpout = safe_fopen (tempfile, "w+")))
{
mutt_perror (tempfile);
return -1;
}
mutt_unlink (tempfile);
*cur = decrypt_part (b, &s, *fpout, 1, &is_signed);
if (*cur)
(*cur)->goodsig = is_signed > 0;
b->type = saved_b_type;
b->length = saved_b_length;
b->offset = saved_b_offset;
safe_fclose (&tmpfp);
rewind (*fpout);
if (*cur && !is_signed && !(*cur)->parts && mutt_is_application_smime (*cur))
{
/* Assume that this is a opaque signed s/mime message. This is
an ugly way of doing it but we have anyway a problem with
arbitrary encoded S/MIME messages: Only the outer part may be
encrypted. The entire mime parsing should be revamped,
probably by keeping the temportary files so that we don't
need to decrypt them all the time. Inner parts of an
encrypted part can then pint into this file and tehre won't
never be a need to decrypt again. This needs a partial
rewrite of the MIME engine. */
BODY *bb = *cur;
BODY *tmp_b;
saved_b_type = bb->type;
saved_b_offset = bb->offset;
saved_b_length = bb->length;
memset (&s, 0, sizeof (s));
s.fpin = *fpout;
fseeko (s.fpin, bb->offset, 0);
mutt_mktemp (tempfile);
if (!(tmpfp = safe_fopen (tempfile, "w+")))
{
mutt_perror (tempfile);
return -1;
}
mutt_unlink (tempfile);
s.fpout = tmpfp;
mutt_decode_attachment (bb, &s);
fflush (tmpfp);
bb->length = ftello (s.fpout);
bb->offset = 0;
rewind (tmpfp);
safe_fclose (fpout);
memset (&s, 0, sizeof (s));
s.fpin = tmpfp;
s.fpout = 0;
mutt_mktemp (tempfile);
if (!(*fpout = safe_fopen (tempfile, "w+")))
{
mutt_perror (tempfile);
return -1;
}
mutt_unlink (tempfile);
tmp_b = decrypt_part (bb, &s, *fpout, 1, &is_signed);
if (tmp_b)
tmp_b->goodsig = is_signed > 0;
bb->type = saved_b_type;
bb->length = saved_b_length;
bb->offset = saved_b_offset;
safe_fclose (&tmpfp);
rewind (*fpout);
mutt_free_body (cur);
*cur = tmp_b;
}
return *cur? 0:-1;
}
static int pgp_gpgme_extract_keys (gpgme_data_t keydata, FILE** fp, int dryrun)
{
/* there's no side-effect free way to view key data in GPGME,
* so we import the key into a temporary keyring */
char tmpdir[_POSIX_PATH_MAX];
char tmpfile[_POSIX_PATH_MAX];
gpgme_ctx_t tmpctx;
gpgme_error_t err;
gpgme_engine_info_t engineinfo;
gpgme_key_t key;
gpgme_user_id_t uid;
gpgme_subkey_t subkey;
const char* shortid;
int len;
char date[STRING];
int more;
int rc = -1;
if ((err = gpgme_new (&tmpctx)) != GPG_ERR_NO_ERROR)
{
dprint (1, (debugfile, "Error creating GPGME context\n"));
return rc;
}
if (dryrun)
{
snprintf (tmpdir, sizeof(tmpdir), "%s/mutt-gpgme-XXXXXX", Tempdir);
if (!mkdtemp (tmpdir))
{
dprint (1, (debugfile, "Error creating temporary GPGME home\n"));
goto err_ctx;
}
engineinfo = gpgme_ctx_get_engine_info (tmpctx);
while (engineinfo && engineinfo->protocol != GPGME_PROTOCOL_OpenPGP)
engineinfo = engineinfo->next;
if (!engineinfo)
{
dprint (1, (debugfile, "Error finding GPGME PGP engine\n"));
goto err_tmpdir;
}
err = gpgme_ctx_set_engine_info (tmpctx, GPGME_PROTOCOL_OpenPGP,
engineinfo->file_name, tmpdir);
if (err != GPG_ERR_NO_ERROR)
{
dprint (1, (debugfile, "Error setting GPGME context home\n"));
goto err_tmpdir;
}
}
if ((err = gpgme_op_import (tmpctx, keydata)) != GPG_ERR_NO_ERROR)
{
dprint (1, (debugfile, "Error importing key\n"));
goto err_tmpdir;
}
mutt_mktemp (tmpfile);
*fp = safe_fopen (tmpfile, "w+");
if (!*fp)
{
mutt_perror (tmpfile);
goto err_tmpdir;
}
unlink (tmpfile);
err = gpgme_op_keylist_start (tmpctx, NULL, 0);
while (!err)
{
if ((err = gpgme_op_keylist_next (tmpctx, &key)))
break;
uid = key->uids;
subkey = key->subkeys;
more = 0;
while (subkey)
{
shortid = subkey->keyid;
len = mutt_strlen (subkey->keyid);
if (len > 8)
shortid += len - 8;
strftime (date, sizeof (date), "%Y-%m-%d", localtime (&subkey->timestamp));
if (!more)
fprintf (*fp, "%s %5.5s %d/%8s %s %s\n", more ? "sub" : "pub",
gpgme_pubkey_algo_name (subkey->pubkey_algo), subkey->length,
shortid, date, uid->uid);
else
fprintf (*fp, "%s %5.5s %d/%8s %s\n", more ? "sub" : "pub",
gpgme_pubkey_algo_name (subkey->pubkey_algo), subkey->length,
shortid, date);
subkey = subkey->next;
more = 1;
}
gpgme_key_release (key);
}
if (gpg_err_code (err) != GPG_ERR_EOF)
{
dprint (1, (debugfile, "Error listing keys\n"));
goto err_fp;
}
rc = 0;
err_fp:
if (rc)
safe_fclose (fp);
err_tmpdir:
if (dryrun)
mutt_rmtree (tmpdir);
err_ctx:
gpgme_release (tmpctx);
return rc;
}
/*
* Implementation of `pgp_check_traditional'.
*/
static int pgp_check_traditional_one_body (FILE *fp, BODY *b, int tagged_only)
{
char tempfile[_POSIX_PATH_MAX];
char buf[HUGE_STRING];
FILE *tfp;
short sgn = 0;
short enc = 0;
if (b->type != TYPETEXT)
return 0;
if (tagged_only && !b->tagged)
return 0;
mutt_mktemp (tempfile);
if (mutt_decode_save_attachment (fp, b, tempfile, 0, 0) != 0)
{
unlink (tempfile);
return 0;
}
if ((tfp = fopen (tempfile, "r")) == NULL)
{
unlink (tempfile);
return 0;
}
while (fgets (buf, sizeof (buf), tfp))
{
if (!mutt_strncmp ("-----BEGIN PGP ", buf, 15))
{
if (!mutt_strcmp ("MESSAGE-----\n", buf + 15))
{
enc = 1;
break;
}
else if (!mutt_strcmp ("SIGNED MESSAGE-----\n", buf + 15))
{
sgn = 1;
break;
}
}
}
safe_fclose (&tfp);
unlink (tempfile);
if (!enc && !sgn)
return 0;
/* fix the content type */
mutt_set_parameter ("format", "fixed", &b->parameter);
mutt_set_parameter ("x-action", enc ? "pgp-encrypted" : "pgp-signed",
&b->parameter);
return 1;
}
int pgp_gpgme_check_traditional (FILE *fp, BODY *b, int tagged_only)
{
int rv = 0;
int r;
for (; b; b = b->next)
{
if (is_multipart (b))
rv = (pgp_gpgme_check_traditional (fp, b->parts, tagged_only) || rv);
else if (b->type == TYPETEXT)
{
if ((r = mutt_is_application_pgp (b)))
rv = (rv || r);
else
rv = (pgp_check_traditional_one_body (fp, b, tagged_only) || rv);
}
}
return rv;
}
/* TODO: looks like this won't work and we'll have to fully parse the
* message file. GPGME makes life hard yet again. */
void pgp_gpgme_invoke_import (const char *fname)
{
gpgme_data_t keydata;
gpgme_error_t err;
FILE* in;
FILE* out;
long outlen;
if (!(in = safe_fopen (fname, "r")))
return;
if ((err = gpgme_data_new_from_stream (&keydata, in)) != GPG_ERR_NO_ERROR)
{
dprint (1, (debugfile, "error converting key file into data object\n"));
return;
}
safe_fclose (&in);
if (!pgp_gpgme_extract_keys (keydata, &out, 0))
{
/* display import results */
outlen = ftell (out);
fseek (out, 0, SEEK_SET);
mutt_copy_bytes (out, stdout, outlen);
safe_fclose (&out);
}
else
printf (_("Error extracting key data!\n"));
}
/*
* Implementation of `application_handler'.
*/
/*
Copy a clearsigned message, and strip the signature and PGP's
dash-escaping.
XXX - charset handling: We assume that it is safe to do
character set decoding first, dash decoding second here, while
we do it the other way around in the main handler.
(Note that we aren't worse than Outlook & Cie in this, and also
note that we can successfully handle anything produced by any
existing versions of mutt.) */
static void copy_clearsigned (gpgme_data_t data, STATE *s, char *charset)
{
char buf[HUGE_STRING];
short complete, armor_header;
FGETCONV *fc;
char *fname;
FILE *fp;
fname = data_object_to_tempfile (data, &fp);
if (!fname)
return;
unlink (fname);
FREE (&fname);
/* fromcode comes from the MIME Content-Type charset label. It might
* be a wrong label, so we want the ability to do corrections via
* charset-hooks. Therefore we set flags to M_ICONV_HOOK_FROM.
*/
fc = fgetconv_open (fp, charset, Charset, M_ICONV_HOOK_FROM);
for (complete = 1, armor_header = 1;
fgetconvs (buf, sizeof (buf), fc) != NULL;
complete = strchr (buf, '\n') != NULL)
{
if (!complete)
{
if (!armor_header)
state_puts (buf, s);
continue;
}
if (!mutt_strcmp (buf, "-----BEGIN PGP SIGNATURE-----\n"))
break;
if (armor_header)
{
if (buf[0] == '\n')
armor_header = 0;
continue;
}
if (s->prefix)
state_puts (s->prefix, s);
if (buf[0] == '-' && buf[1] == ' ')
state_puts (buf + 2, s);
else
state_puts (buf, s);
}
fgetconv_close (&fc);
safe_fclose (&fp);
}
/* Support for classic_application/pgp */
int pgp_gpgme_application_handler (BODY *m, STATE *s)
{
int needpass = -1, pgp_keyblock = 0;
int clearsign = 0;
long start_pos = 0;
long bytes;
LOFF_T last_pos, offset;
char buf[HUGE_STRING];
FILE *pgpout = NULL;
gpgme_error_t err = 0;
gpgme_data_t armored_data = NULL;
short maybe_goodsig = 1;
short have_any_sigs = 0;
char body_charset[STRING]; /* Only used for clearsigned messages. */
dprint (2, (debugfile, "Entering pgp_application_pgp handler\n"));
/* For clearsigned messages we won't be able to get a character set
but we know that this may only be text thus we assume Latin-1
here. */
if (!mutt_get_body_charset (body_charset, sizeof (body_charset), m))
strfcpy (body_charset, "iso-8859-1", sizeof body_charset);
fseeko (s->fpin, m->offset, 0);
last_pos = m->offset;
for (bytes = m->length; bytes > 0;)
{
if (fgets (buf, sizeof (buf), s->fpin) == NULL)
break;
offset = ftello (s->fpin);
bytes -= (offset - last_pos); /* don't rely on mutt_strlen(buf) */
last_pos = offset;
if (!mutt_strncmp ("-----BEGIN PGP ", buf, 15))
{
clearsign = 0;
start_pos = last_pos;
if (!mutt_strcmp ("MESSAGE-----\n", buf + 15))
needpass = 1;
else if (!mutt_strcmp ("SIGNED MESSAGE-----\n", buf + 15))
{
clearsign = 1;
needpass = 0;
}
else if (!mutt_strcmp ("PUBLIC KEY BLOCK-----\n", buf + 15))
{
needpass = 0;
pgp_keyblock = 1;
}
else
{
/* XXX - we may wish to recode here */
if (s->prefix)
state_puts (s->prefix, s);
state_puts (buf, s);
continue;
}
have_any_sigs = (have_any_sigs
|| (clearsign && (s->flags & M_VERIFY)));
/* Copy PGP material to an data container */
armored_data = file_to_data_object (s->fpin, m->offset, m->length);
/* Invoke PGP if needed */
if (pgp_keyblock)
{
pgp_gpgme_extract_keys (armored_data, &pgpout, 1);
}
else if (!clearsign || (s->flags & M_VERIFY))
{
unsigned int sig_stat = 0;
gpgme_data_t plaintext;
gpgme_ctx_t ctx;
plaintext = create_gpgme_data ();
ctx = create_gpgme_context (0);
if (clearsign)
err = gpgme_op_verify (ctx, armored_data, NULL, plaintext);
else
{
err = gpgme_op_decrypt_verify (ctx, armored_data, plaintext);
if (gpg_err_code (err) == GPG_ERR_NO_DATA)
{
/* Decrypt verify can't handle signed only messages. */
err = (gpgme_data_seek (armored_data, 0, SEEK_SET) == -1)
? gpgme_error_from_errno (errno) : 0;
/* Must release plaintext so that we supply an
uninitialized object. */
gpgme_data_release (plaintext);
plaintext = create_gpgme_data ();
err = gpgme_op_verify (ctx, armored_data,
NULL, plaintext);
}
}
if (err)
{
char errbuf[200];
snprintf (errbuf, sizeof(errbuf)-1,
_("Error: decryption/verification failed: %s\n"),
gpgme_strerror (err));
state_attach_puts (errbuf, s);
}
else
{ /* Decryption/Verification succeeded */
char *tmpfname;
{
/* Check whether signatures have been verified. */
gpgme_verify_result_t verify_result;
verify_result = gpgme_op_verify_result (ctx);
if (verify_result->signatures)
sig_stat = 1;
}
have_any_sigs = 0;
maybe_goodsig = 0;
if ((s->flags & M_DISPLAY) && sig_stat)
{
int res, idx;
int anybad = 0;
int anywarn = 0;
state_attach_puts (_("[-- Begin signature "
"information --]\n"), s);
have_any_sigs = 1;
for(idx=0;
(res = show_one_sig_status (ctx, idx, s)) != -1;
idx++)
{
if (res == 1)
anybad = 1;
else if (res == 2)
anywarn = 1;
}
if (!anybad && idx)
maybe_goodsig = 1;
state_attach_puts (_("[-- End signature "
"information --]\n\n"), s);
}
tmpfname = data_object_to_tempfile (plaintext, &pgpout);
if (!tmpfname)
{
pgpout = NULL;
state_attach_puts (_("Error: copy data failed\n"), s);
}
else
{
unlink (tmpfname);
FREE (&tmpfname);
}
}
gpgme_release (ctx);
}
/*
* Now, copy cleartext to the screen. NOTE - we expect that PGP
* outputs utf-8 cleartext. This may not always be true, but it
* seems to be a reasonable guess.
*/
if(s->flags & M_DISPLAY)
{
if (needpass)
state_attach_puts (_("[-- BEGIN PGP MESSAGE --]\n\n"), s);
else if (pgp_keyblock)
state_attach_puts (_("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"),
s);
else
state_attach_puts (_("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"),
s);
}
if (clearsign)
{
copy_clearsigned (armored_data, s, body_charset);
}
else if (pgpout)
{
FGETCONV *fc;
int c;
rewind (pgpout);
fc = fgetconv_open (pgpout, "utf-8", Charset, 0);
while ((c = fgetconv (fc)) != EOF)
{
state_putc (c, s);
if (c == '\n' && s->prefix)
state_puts (s->prefix, s);
}
fgetconv_close (&fc);
}
if (s->flags & M_DISPLAY)
{
state_putc ('\n', s);
if (needpass)
state_attach_puts (_("[-- END PGP MESSAGE --]\n"), s);
else if (pgp_keyblock)
state_attach_puts (_("[-- END PGP PUBLIC KEY BLOCK --]\n"), s);
else
state_attach_puts (_("[-- END PGP SIGNED MESSAGE --]\n"), s);
}
if (pgpout)
{
safe_fclose (&pgpout);
}
}
#if 0
else
{
/* why would we want to display this at all? */
/* XXX - we may wish to recode here */
if (s->prefix)
state_puts (s->prefix, s);
state_puts (buf, s);
}
#endif
}
m->goodsig = (maybe_goodsig && have_any_sigs);
if (needpass == -1)
{
state_attach_puts (_("[-- Error: could not find beginning"
" of PGP message! --]\n\n"), s);
return 1;
}
dprint (2, (debugfile, "Leaving pgp_application_pgp handler\n"));
return err;
}
/*
* Implementation of `encrypted_handler'.
*/
/* MIME handler for pgp/mime encrypted messages. */
int pgp_gpgme_encrypted_handler (BODY *a, STATE *s)
{
char tempfile[_POSIX_PATH_MAX];
FILE *fpout;
BODY *tattach;
BODY *orig_body = a;
int is_signed;
int rc = 0;
dprint (2, (debugfile, "Entering pgp_encrypted handler\n"));
a = a->parts;
if (!a || a->type != TYPEAPPLICATION || !a->subtype
|| ascii_strcasecmp ("pgp-encrypted", a->subtype)
|| !a->next || a->next->type != TYPEAPPLICATION || !a->next->subtype
|| ascii_strcasecmp ("octet-stream", a->next->subtype) )
{
if (s->flags & M_DISPLAY)
state_attach_puts (_("[-- Error: malformed PGP/MIME message! --]\n\n"),
s);
return -1;
}
/* Move forward to the application/pgp-encrypted body. */
a = a->next;
mutt_mktemp (tempfile);
if (!(fpout = safe_fopen (tempfile, "w+")))
{
if (s->flags & M_DISPLAY)
state_attach_puts (_("[-- Error: could not create temporary file! "
"--]\n"), s);
return -1;
}
tattach = decrypt_part (a, s, fpout, 0, &is_signed);
if (tattach)
{
tattach->goodsig = is_signed > 0;
if (s->flags & M_DISPLAY)
state_attach_puts (is_signed?
_("[-- The following data is PGP/MIME signed and encrypted --]\n\n"):
_("[-- The following data is PGP/MIME encrypted --]\n\n"),
s);
{
FILE *savefp = s->fpin;
s->fpin = fpout;
rc = mutt_body_handler (tattach, s);
s->fpin = savefp;
}
/*
* if a multipart/signed is the _only_ sub-part of a
* multipart/encrypted, cache signature verification
* status.
*/
if (mutt_is_multipart_signed (tattach) && !tattach->next)
orig_body->goodsig |= tattach->goodsig;
if (s->flags & M_DISPLAY)
{
state_puts ("\n", s);
state_attach_puts (is_signed?
_("[-- End of PGP/MIME signed and encrypted data --]\n"):
_("[-- End of PGP/MIME encrypted data --]\n"),
s);
}
mutt_free_body (&tattach);
}
safe_fclose (&fpout);
mutt_unlink(tempfile);
dprint (2, (debugfile, "Leaving pgp_encrypted handler\n"));
return rc;
}
/* Support for application/smime */
int smime_gpgme_application_handler (BODY *a, STATE *s)
{
char tempfile[_POSIX_PATH_MAX];
FILE *fpout;
BODY *tattach;
int is_signed;
int rc = 0;
dprint (2, (debugfile, "Entering smime_encrypted handler\n"));
a->warnsig = 0;
mutt_mktemp (tempfile);
if (!(fpout = safe_fopen (tempfile, "w+")))
{
if (s->flags & M_DISPLAY)
state_attach_puts (_("[-- Error: could not create temporary file! "
"--]\n"), s);
return -1;
}
tattach = decrypt_part (a, s, fpout, 1, &is_signed);
if (tattach)
{
tattach->goodsig = is_signed > 0;
if (s->flags & M_DISPLAY)
state_attach_puts (is_signed?
_("[-- The following data is S/MIME signed --]\n\n"):
_("[-- The following data is S/MIME encrypted --]\n\n"),
s);
{
FILE *savefp = s->fpin;
s->fpin = fpout;
rc = mutt_body_handler (tattach, s);
s->fpin = savefp;
}
/*
* if a multipart/signed is the _only_ sub-part of a
* multipart/encrypted, cache signature verification
* status.
*/
if (mutt_is_multipart_signed (tattach) && !tattach->next)
{
if (!(a->goodsig = tattach->goodsig))
a->warnsig = tattach->warnsig;
}
else if (tattach->goodsig)
{
a->goodsig = 1;
a->warnsig = tattach->warnsig;
}
if (s->flags & M_DISPLAY)
{
state_puts ("\n", s);
state_attach_puts (is_signed?
_("[-- End of S/MIME signed data --]\n"):
_("[-- End of S/MIME encrypted data --]\n"),
s);
}
mutt_free_body (&tattach);
}
safe_fclose (&fpout);
mutt_unlink(tempfile);
dprint (2, (debugfile, "Leaving smime_encrypted handler\n"));
return rc;
}
/*
* Format an entry on the CRYPT key selection menu.
*
* %n number
* %k key id %K key id of the principal key
* %u user id
* %a algorithm %A algorithm of the princ. key
* %l length %L length of the princ. key
* %f flags %F flags of the princ. key
* %c capabilities %C capabilities of the princ. key
* %t trust/validity of the key-uid association
* %p protocol
* %[...] date of key using strftime(3)
*/
static const char *crypt_entry_fmt (char *dest,
size_t destlen,
size_t col,
char op,
const char *src,
const char *prefix,
const char *ifstring,
const char *elsestring,
unsigned long data,
format_flag flags)
{
char fmt[16];
crypt_entry_t *entry;
crypt_key_t *key;
int kflags = 0;
int optional = (flags & M_FORMAT_OPTIONAL);
const char *s = NULL;
unsigned long val;
entry = (crypt_entry_t *) data;
key = entry->key;
/* if (isupper ((unsigned char) op)) */
/* key = pkey; */
kflags = (key->flags /*| (pkey->flags & KEYFLAG_RESTRICTIONS)
| uid->flags*/);
switch (ascii_tolower (op))
{
case '[':
{
const char *cp;
char buf2[SHORT_STRING], *p;
int do_locales;
struct tm *tm;
size_t len;
p = dest;
cp = src;
if (*cp == '!')
{
do_locales = 0;
cp++;
}
else
do_locales = 1;
len = destlen - 1;
while (len > 0 && *cp != ']')
{
if (*cp == '%')
{
cp++;
if (len >= 2)
{
*p++ = '%';
*p++ = *cp;
len -= 2;
}
else
break; /* not enough space */
cp++;
}
else
{
*p++ = *cp++;
len--;
}
}
*p = 0;
if (do_locales && Locale)
setlocale (LC_TIME, Locale);
{
time_t tt = 0;
if (key->kobj->subkeys && (key->kobj->subkeys->timestamp > 0))
tt = key->kobj->subkeys->timestamp;
tm = localtime (&tt);
}
strftime (buf2, sizeof (buf2), dest, tm);
if (do_locales)
setlocale (LC_TIME, "C");
snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
snprintf (dest, destlen, fmt, buf2);
if (len > 0)
src = cp + 1;
}
break;
case 'n':
if (!optional)
{
snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
snprintf (dest, destlen, fmt, entry->num);
}
break;
case 'k':
if (!optional)
{
/* fixme: we need a way to distinguish between main and subkeys.
Store the idx in entry? */
snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
snprintf (dest, destlen, fmt, crypt_keyid (key));
}
break;
case 'u':
if (!optional)
{
snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
snprintf (dest, destlen, fmt, key->uid);
}
break;
case 'a':
if (!optional)
{
snprintf (fmt, sizeof (fmt), "%%%s.3s", prefix);
if (key->kobj->subkeys)
s = gpgme_pubkey_algo_name (key->kobj->subkeys->pubkey_algo);
else
s = "?";
snprintf (dest, destlen, fmt, s);
}
break;
case 'l':
if (!optional)
{
snprintf (fmt, sizeof (fmt), "%%%slu", prefix);
if (key->kobj->subkeys)
val = key->kobj->subkeys->length;
else
val = 0;
snprintf (dest, destlen, fmt, val);
}
break;
case 'f':
if (!optional)
{
snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
snprintf (dest, destlen, fmt, crypt_flags (kflags));
}
else if (!(kflags & (KEYFLAG_RESTRICTIONS)))
optional = 0;
break;
case 'c':
if (!optional)
{
snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
snprintf (dest, destlen, fmt, crypt_key_abilities (kflags));
}
else if (!(kflags & (KEYFLAG_ABILITIES)))
optional = 0;
break;
case 't':
if ((kflags & KEYFLAG_ISX509))
s = "x";
else
{
gpgme_user_id_t uid = NULL;
unsigned int i = 0;
for (i = 0, uid = key->kobj->uids; uid && (i < key->idx);
i++, uid = uid->next)
;
if (uid)
switch (uid->validity)
{
case GPGME_VALIDITY_UNDEFINED:
s = "q";
break;
case GPGME_VALIDITY_NEVER:
s = "n";
break;
case GPGME_VALIDITY_MARGINAL:
s = "m";
break;
case GPGME_VALIDITY_FULL:
s = "f";
break;
case GPGME_VALIDITY_ULTIMATE:
s = "u";
break;
case GPGME_VALIDITY_UNKNOWN:
default:
s = "?";
break;
}
}
snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
snprintf (dest, destlen, fmt, s? *s: 'B');
break;
case 'p':
snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
snprintf (dest, destlen, fmt,
gpgme_get_protocol_name (key->kobj->protocol));
break;
default:
*dest = '\0';
}
if (optional)
mutt_FormatString (dest, destlen, col, ifstring, mutt_attach_fmt, data, 0);
else if (flags & M_FORMAT_OPTIONAL)
mutt_FormatString (dest, destlen, col, elsestring, mutt_attach_fmt, data, 0);
return (src);
}
/* Used by the display fucntion to format a line. */
static void crypt_entry (char *s, size_t l, MUTTMENU * menu, int num)
{
crypt_key_t **key_table = (crypt_key_t **) menu->data;
crypt_entry_t entry;
entry.key = key_table[num];
entry.num = num + 1;
mutt_FormatString (s, l, 0, NONULL (PgpEntryFormat), crypt_entry_fmt,
(unsigned long) &entry, M_FORMAT_ARROWCURSOR);
}
/* Compare two addresses and the keyid to be used for sorting. */
static int _crypt_compare_address (const void *a, const void *b)
{
crypt_key_t **s = (crypt_key_t **) a;
crypt_key_t **t = (crypt_key_t **) b;
int r;
if ((r = mutt_strcasecmp ((*s)->uid, (*t)->uid)))
return r > 0;
else
return mutt_strcasecmp (crypt_keyid (*s), crypt_keyid (*t)) > 0;
}
static int crypt_compare_address (const void *a, const void *b)
{
return ((PgpSortKeys & SORT_REVERSE) ? !_crypt_compare_address (a, b)
: _crypt_compare_address (a, b));
}
/* Compare two key IDs and the addresses to be used for sorting. */
static int _crypt_compare_keyid (const void *a, const void *b)
{
crypt_key_t **s = (crypt_key_t **) a;
crypt_key_t **t = (crypt_key_t **) b;
int r;
if ((r = mutt_strcasecmp (crypt_keyid (*s), crypt_keyid (*t))))
return r > 0;
else
return mutt_strcasecmp ((*s)->uid, (*t)->uid) > 0;
}
static int crypt_compare_keyid (const void *a, const void *b)
{
return ((PgpSortKeys & SORT_REVERSE) ? !_crypt_compare_keyid (a, b)
: _crypt_compare_keyid (a, b));
}
/* Compare 2 creation dates and the addresses. For sorting. */
static int _crypt_compare_date (const void *a, const void *b)
{
crypt_key_t **s = (crypt_key_t **) a;
crypt_key_t **t = (crypt_key_t **) b;
unsigned long ts = 0, tt = 0;
if ((*s)->kobj->subkeys && ((*s)->kobj->subkeys->timestamp > 0))
ts = (*s)->kobj->subkeys->timestamp;
if ((*t)->kobj->subkeys && ((*t)->kobj->subkeys->timestamp > 0))
tt = (*t)->kobj->subkeys->timestamp;
if (ts > tt)
return 1;
if (ts < tt)
return 0;
return mutt_strcasecmp ((*s)->uid, (*t)->uid) > 0;
}
static int crypt_compare_date (const void *a, const void *b)
{
return ((PgpSortKeys & SORT_REVERSE) ? !_crypt_compare_date (a, b)
: _crypt_compare_date (a, b));
}
/* Compare two trust values, the key length, the creation dates. the
addresses and the key IDs. For sorting. */
static int _crypt_compare_trust (const void *a, const void *b)
{
crypt_key_t **s = (crypt_key_t **) a;
crypt_key_t **t = (crypt_key_t **) b;
unsigned long ts = 0, tt = 0;
int r;
if ((r = (((*s)->flags & (KEYFLAG_RESTRICTIONS))
- ((*t)->flags & (KEYFLAG_RESTRICTIONS)))))
return r > 0;
if ((*s)->kobj->uids)
ts = (*s)->kobj->uids->validity;
if ((*t)->kobj->uids)
tt = (*t)->kobj->uids->validity;
if ((r = (tt - ts)))
return r < 0;
if ((*s)->kobj->subkeys)
ts = (*s)->kobj->subkeys->length;
if ((*t)->kobj->subkeys)
tt = (*t)->kobj->subkeys->length;
if (ts != tt)
return ts > tt;
if ((*s)->kobj->subkeys && ((*s)->kobj->subkeys->timestamp > 0))
ts = (*s)->kobj->subkeys->timestamp;
if ((*t)->kobj->subkeys && ((*t)->kobj->subkeys->timestamp > 0))
tt = (*t)->kobj->subkeys->timestamp;
if (ts > tt)
return 1;
if (ts < tt)
return 0;
if ((r = mutt_strcasecmp ((*s)->uid, (*t)->uid)))
return r > 0;
return (mutt_strcasecmp (crypt_keyid ((*s)), crypt_keyid ((*t)))) > 0;
}
static int crypt_compare_trust (const void *a, const void *b)
{
return ((PgpSortKeys & SORT_REVERSE) ? !_crypt_compare_trust (a, b)
: _crypt_compare_trust (a, b));
}
/* Print the X.500 Distinguished Name part KEY from the array of parts
DN to FP. */
static int
print_dn_part (FILE *fp, struct dn_array_s *dn, const char *key)
{
int any = 0;
for (; dn->key; dn++)
{
if (!strcmp (dn->key, key))
{
if (any)
fputs (" + ", fp);
print_utf8 (fp, dn->value, strlen (dn->value));
any = 1;
}
}
return any;
}
/* Print all parts of a DN in a standard sequence. */
static void
print_dn_parts (FILE *fp, struct dn_array_s *dn)
{
const char *stdpart[] = {
"CN", "OU", "O", "STREET", "L", "ST", "C", NULL
};
int any=0, any2=0, i;
for (i=0; stdpart[i]; i++)
{
if (any)
fputs (", ", fp);
any = print_dn_part (fp, dn, stdpart[i]);
}
/* now print the rest without any specific ordering */
for (; dn->key; dn++)
{
for (i=0; stdpart[i]; i++)
{
if (!strcmp (dn->key, stdpart[i]))
break;
}
if (!stdpart[i])
{
if (any)
fputs (", ", fp);
if (!any2)
fputs ("(", fp);
any = print_dn_part (fp, dn, dn->key);
any2 = 1;
}
}
if (any2)
fputs (")", fp);
}
/* Parse an RDN; this is a helper to parse_dn(). */
static const unsigned char *
parse_dn_part (struct dn_array_s *array, const unsigned char *string)
{
const unsigned char *s, *s1;
size_t n;
unsigned char *p;
/* parse attributeType */
for (s = string+1; *s && *s != '='; s++)
;
if (!*s)
return NULL; /* error */
n = s - string;
if (!n)
return NULL; /* empty key */
array->key = safe_malloc (n+1);
p = (unsigned char *)array->key;
memcpy (p, string, n); /* fixme: trim trailing spaces */
p[n] = 0;
string = s + 1;
if (*string == '#')
{ /* hexstring */
string++;
for (s=string; hexdigitp (s); s++)
s++;
n = s - string;
if (!n || (n & 1))
return NULL; /* empty or odd number of digits */
n /= 2;
p = safe_malloc (n+1);
array->value = (char*)p;
for (s1=string; n; s1 += 2, n--)
*p++ = xtoi_2 (s1);
*p = 0;
}
else
{ /* regular v3 quoted string */
for (n=0, s=string; *s; s++)
{
if (*s == '\\')
{ /* pair */
s++;
if (*s == ',' || *s == '=' || *s == '+'
|| *s == '<' || *s == '>' || *s == '#' || *s == ';'
|| *s == '\\' || *s == '\"' || *s == ' ')
n++;
else if (hexdigitp (s) && hexdigitp (s+1))
{
s++;
n++;
}
else
return NULL; /* invalid escape sequence */
}
else if (*s == '\"')
return NULL; /* invalid encoding */
else if (*s == ',' || *s == '=' || *s == '+'
|| *s == '<' || *s == '>' || *s == '#' || *s == ';' )
break;
else
n++;
}
p = safe_malloc (n+1);
array->value = (char*)p;
for (s=string; n; s++, n--)
{
if (*s == '\\')
{
s++;
if (hexdigitp (s))
{
*p++ = xtoi_2 (s);
s++;
}
else
*p++ = *s;
}
else
*p++ = *s;
}
*p = 0;
}
return s;
}
/* Parse a DN and return an array-ized one. This is not a validating
parser and it does not support any old-stylish syntax; gpgme is
expected to return only rfc2253 compatible strings. */
static struct dn_array_s *
parse_dn (const unsigned char *string)
{
struct dn_array_s *array;
size_t arrayidx, arraysize;
int i;
arraysize = 7; /* C,ST,L,O,OU,CN,email */
array = safe_malloc ((arraysize+1) * sizeof *array);
arrayidx = 0;
while (*string)
{
while (*string == ' ')
string++;
if (!*string)
break; /* ready */
if (arrayidx >= arraysize)
{ /* mutt lacks a real safe_realoc - so we need to copy */
struct dn_array_s *a2;
arraysize += 5;
a2 = safe_malloc ((arraysize+1) * sizeof *array);
for (i=0; i < arrayidx; i++)
{
a2[i].key = array[i].key;
a2[i].value = array[i].value;
}
FREE (&array);
array = a2;
}
array[arrayidx].key = NULL;
array[arrayidx].value = NULL;
string = parse_dn_part (array+arrayidx, string);
arrayidx++;
if (!string)
goto failure;
while (*string == ' ')
string++;
if (*string && *string != ',' && *string != ';' && *string != '+')
goto failure; /* invalid delimiter */
if (*string)
string++;
}
array[arrayidx].key = NULL;
array[arrayidx].value = NULL;
return array;
failure:
for (i=0; i < arrayidx; i++)
{
FREE (&array[i].key);
FREE (&array[i].value);
}
FREE (&array);
return NULL;
}
/* Print a nice representation of the USERID and make sure it is
displayed in a proper way, which does mean to reorder some parts
for S/MIME's DNs. USERID is a string as returned by the gpgme key
functions. It is utf-8 encoded. */
static void
parse_and_print_user_id (FILE *fp, const char *userid)
{
const char *s;
int i;
if (*userid == '<')
{
s = strchr (userid+1, '>');
if (s)
print_utf8 (fp, userid+1, s-userid-1);
}
else if (*userid == '(')
fputs (_("[Can't display this user ID (unknown encoding)]"), fp);
else if (!digit_or_letter ((const unsigned char *)userid))
fputs (_("[Can't display this user ID (invalid encoding)]"), fp);
else
{
struct dn_array_s *dn = parse_dn ((const unsigned char *)userid);
if (!dn)
fputs (_("[Can't display this user ID (invalid DN)]"), fp);
else
{
print_dn_parts (fp, dn);
for (i=0; dn[i].key; i++)
{
FREE (&dn[i].key);
FREE (&dn[i].value);
}
FREE (&dn);
}
}
}
typedef enum
{
KEY_CAP_CAN_ENCRYPT,
KEY_CAP_CAN_SIGN,
KEY_CAP_CAN_CERTIFY
}
key_cap_t;
static unsigned int
key_check_cap (gpgme_key_t key, key_cap_t cap)
{
gpgme_subkey_t subkey = NULL;
unsigned int ret = 0;
switch (cap)
{
case KEY_CAP_CAN_ENCRYPT:
if (! (ret = key->can_encrypt))
for (subkey = key->subkeys; subkey; subkey = subkey->next)
if ((ret = subkey->can_encrypt))
break;
break;
case KEY_CAP_CAN_SIGN:
if (! (ret = key->can_sign))
for (subkey = key->subkeys; subkey; subkey = subkey->next)
if ((ret = subkey->can_sign))
break;
break;
case KEY_CAP_CAN_CERTIFY:
if (! (ret = key->can_certify))
for (subkey = key->subkeys; subkey; subkey = subkey->next)
if ((ret = subkey->can_certify))
break;
break;
}
return ret;
}
/* Print verbose information about a key or certificate to FP. */
static void print_key_info (gpgme_key_t key, FILE *fp)
{
int idx;
const char *s = NULL, *s2 = NULL;
time_t tt = 0;
struct tm *tm;
char shortbuf[SHORT_STRING];
unsigned long aval = 0;
const char *delim;
int is_pgp = 0;
int i;
gpgme_user_id_t uid = NULL;
if (Locale)
setlocale (LC_TIME, Locale);
is_pgp = key->protocol == GPGME_PROTOCOL_OpenPGP;
for (idx = 0, uid = key->uids; uid; idx++, uid = uid->next)
{
if (uid->revoked)
continue;
s = uid->uid;
fputs (idx ? _(" aka ......: ") :_("Name ......: "), fp);
if (uid->invalid)
{
fputs (_("[Invalid]"), fp);
putc (' ', fp);
}
if (is_pgp)
print_utf8 (fp, s, strlen(s));
else
parse_and_print_user_id (fp, s);
putc ('\n', fp);
}
if (key->subkeys && (key->subkeys->timestamp > 0))
{
tt = key->subkeys->timestamp;
tm = localtime (&tt);
#ifdef HAVE_LANGINFO_D_T_FMT
strftime (shortbuf, sizeof shortbuf, nl_langinfo (D_T_FMT), tm);
#else
strftime (shortbuf, sizeof shortbuf, "%c", tm);
#endif
fprintf (fp, _("Valid From : %s\n"), shortbuf);
}
if (key->subkeys && (key->subkeys->expires > 0))
{
tt = key->subkeys->expires;
tm = localtime (&tt);
#ifdef HAVE_LANGINFO_D_T_FMT
strftime (shortbuf, sizeof shortbuf, nl_langinfo (D_T_FMT), tm);
#else
strftime (shortbuf, sizeof shortbuf, "%c", tm);
#endif
fprintf (fp, _("Valid To ..: %s\n"), shortbuf);
}
if (key->subkeys)
s = gpgme_pubkey_algo_name (key->subkeys->pubkey_algo);
else
s = "?";
s2 = is_pgp ? "PGP" : "X.509";
if (key->subkeys)
aval = key->subkeys->length;
fprintf (fp, _("Key Type ..: %s, %lu bit %s\n"), s2, aval, s);
fprintf (fp, _("Key Usage .: "));
delim = "";
if (key_check_cap (key, KEY_CAP_CAN_ENCRYPT))
{
fprintf (fp, "%s%s", delim, _("encryption"));
delim = _(", ");
}
if (key_check_cap (key, KEY_CAP_CAN_SIGN))
{
fprintf (fp, "%s%s", delim, _("signing"));
delim = _(", ");
}
if (key_check_cap (key, KEY_CAP_CAN_CERTIFY))
{
fprintf (fp, "%s%s", delim, _("certification"));
delim = _(", ");
}
putc ('\n', fp);
if (key->subkeys)
{
s = key->subkeys->fpr;
fputs (_("Fingerprint: "), fp);
if (is_pgp && strlen (s) == 40)
{
for (i=0; *s && s[1] && s[2] && s[3] && s[4]; s += 4, i++)
{
putc (*s, fp);
putc (s[1], fp);
putc (s[2], fp);
putc (s[3], fp);
putc (is_pgp? ' ':':', fp);
if (is_pgp && i == 4)
putc (' ', fp);
}
}
else
{
for (i=0; *s && s[1] && s[2]; s += 2, i++)
{
putc (*s, fp);
putc (s[1], fp);
putc (is_pgp? ' ':':', fp);
if (is_pgp && i == 7)
putc (' ', fp);
}
}
fprintf (fp, "%s\n", s);
}
if (key->issuer_serial)
{
s = key->issuer_serial;
if (s)
fprintf (fp, _("Serial-No .: 0x%s\n"), s);
}
if (key->issuer_name)
{
s = key->issuer_name;
if (s)
{
fprintf (fp, _("Issued By .: "));
parse_and_print_user_id (fp, s);
putc ('\n', fp);
}
}
/* For PGP we list all subkeys. */
if (is_pgp)
{
gpgme_subkey_t subkey = NULL;
for (idx = 1, subkey = key->subkeys; subkey;
idx++, subkey = subkey->next)
{
s = subkey->keyid;
putc ('\n', fp);
if ( strlen (s) == 16)
s += 8; /* display only the short keyID */
fprintf (fp, _("Subkey ....: 0x%s"), s);
if (subkey->revoked)
{
putc (' ', fp);
fputs (_("[Revoked]"), fp);
}
if (subkey->invalid)
{
putc (' ', fp);
fputs (_("[Invalid]"), fp);
}
if (subkey->expired)
{
putc (' ', fp);
fputs (_("[Expired]"), fp);
}
if (subkey->disabled)
{
putc (' ', fp);
fputs (_("[Disabled]"), fp);
}
putc ('\n', fp);
if (subkey->timestamp > 0)
{
tt = subkey->timestamp;
tm = localtime (&tt);
#ifdef HAVE_LANGINFO_D_T_FMT
strftime (shortbuf, sizeof shortbuf, nl_langinfo (D_T_FMT), tm);
#else
strftime (shortbuf, sizeof shortbuf, "%c", tm);
#endif
fprintf (fp, _("Valid From : %s\n"), shortbuf);
}
if (subkey->expires > 0)
{
tt = subkey->expires;
tm = localtime (&tt);
#ifdef HAVE_LANGINFO_D_T_FMT
strftime (shortbuf, sizeof shortbuf, nl_langinfo (D_T_FMT), tm);
#else
strftime (shortbuf, sizeof shortbuf, "%c", tm);
#endif
fprintf (fp, _("Valid To ..: %s\n"), shortbuf);
}
if (subkey)
s = gpgme_pubkey_algo_name (subkey->pubkey_algo);
else
s = "?";
if (subkey)
aval = subkey->length;
else
aval = 0;
fprintf (fp, _("Key Type ..: %s, %lu bit %s\n"), "PGP", aval, s);
fprintf (fp, _("Key Usage .: "));
delim = "";
if (subkey->can_encrypt)
{
fprintf (fp, "%s%s", delim, _("encryption"));
delim = _(", ");
}
if (subkey->can_sign)
{
fprintf (fp, "%s%s", delim, _("signing"));
delim = _(", ");
}
if (subkey->can_certify)
{
fprintf (fp, "%s%s", delim, _("certification"));
delim = _(", ");
}
putc ('\n', fp);
}
}
if (Locale)
setlocale (LC_TIME, "C");
}
/* Show detailed information about the selected key */
static void
verify_key (crypt_key_t *key)
{
FILE *fp;
char cmd[LONG_STRING], tempfile[_POSIX_PATH_MAX];
const char *s;
gpgme_ctx_t listctx = NULL;
gpgme_error_t err;
gpgme_key_t k = NULL;
int maxdepth = 100;
mutt_mktemp (tempfile);
if (!(fp = safe_fopen (tempfile, "w")))
{
mutt_perror _("Can't create temporary file");
return;
}
mutt_message _("Collecting data...");
print_key_info (key->kobj, fp);
err = gpgme_new (&listctx);
if (err)
{
fprintf (fp, "Internal error: can't create gpgme context: %s\n",
gpgme_strerror (err));
goto leave;
}
if ((key->flags & KEYFLAG_ISX509))
gpgme_set_protocol (listctx, GPGME_PROTOCOL_CMS);
k = key->kobj;
gpgme_key_ref (k);
while ((s = k->chain_id) && k->subkeys && strcmp (s, k->subkeys->fpr) )
{
putc ('\n', fp);
err = gpgme_op_keylist_start (listctx, s, 0);
gpgme_key_release (k);
k = NULL;
if (!err)
err = gpgme_op_keylist_next (listctx, &k);
if (err)
{
fprintf (fp, _("Error finding issuer key: %s\n"),
gpgme_strerror (err));
goto leave;
}
gpgme_op_keylist_end (listctx);
print_key_info (k, fp);
if (!--maxdepth)
{
putc ('\n', fp);
fputs (_("Error: certification chain to long - stopping here\n"),
fp);
break;
}
}
leave:
gpgme_key_release (k);
gpgme_release (listctx);
safe_fclose (&fp);
mutt_clear_error ();
snprintf (cmd, sizeof (cmd), _("Key ID: 0x%s"), crypt_keyid (key));
mutt_do_pager (cmd, tempfile, 0, NULL);
}
/*
* Implementation of `findkeys'.
*/
/* Convert LIST into a pattern string suitable to be passed to GPGME.
We need to convert spaces in an item into a '+' and '%' into
"%25". */
static char *list_to_pattern (LIST *list)
{
LIST *l;
char *pattern, *p;
const char *s;
size_t n;
n = 0;
for(l=list; l; l = l->next)
{
for(s = l->data; *s; s++)
{
if (*s == '%')
n += 2;
n++;
}
n++; /* delimiter or end of string */
}
n++; /* make sure to allocate at least one byte */
pattern = p = safe_calloc (1,n);
for(l=list; l; l = l->next)
{
s = l->data;
if (*s)
{
if (l != list)
*p++ = ' ';
for(s = l->data; *s; s++)
{
if (*s == '%')
{
*p++ = '%';
*p++ = '2';
*p++ = '5';
}
else if (*s == '+')
{
*p++ = '%';
*p++ = '2';
*p++ = 'B';
}
else if (*s == ' ')
*p++ = '+';
else
*p++ = *s;
}
}
}
*p = 0;
return pattern;
}
/* Return a list of keys which are candidates for the selection.
Select by looking at the HINTS list. */
static crypt_key_t *get_candidates (LIST * hints, unsigned int app, int secret)
{
crypt_key_t *db, *k, **kend;
char *pattern;
gpgme_error_t err;
gpgme_ctx_t ctx;
gpgme_key_t key;
int idx;
gpgme_user_id_t uid = NULL;
pattern = list_to_pattern (hints);
if (!pattern)
return NULL;
err = gpgme_new (&ctx);
if (err)
{
mutt_error (_("gpgme_new failed: %s"), gpgme_strerror (err));
FREE (&pattern);
return NULL;
}
db = NULL;
kend = &db;
if ((app & APPLICATION_PGP))
{
/* Its all a mess. That old GPGME expects different things
depending on the protocol. For gpg we don' t need percent
escaped pappert but simple strings passed in an array to the
keylist_ext_start function. */
LIST *l;
size_t n;
char **patarr;
for(l=hints, n=0; l; l = l->next)
{
if (l->data && *l->data)
n++;
}
if (!n)
goto no_pgphints;
patarr = safe_calloc (n+1, sizeof *patarr);
for(l=hints, n=0; l; l = l->next)
{
if (l->data && *l->data)
patarr[n++] = safe_strdup (l->data);
}
patarr[n] = NULL;
err = gpgme_op_keylist_ext_start (ctx, (const char**)patarr, secret, 0);
for (n=0; patarr[n]; n++)
FREE (&patarr[n]);
FREE (&patarr);
if (err)
{
mutt_error (_("gpgme_op_keylist_start failed: %s"),
gpgme_strerror (err));
gpgme_release (ctx);
FREE (&pattern);
return NULL;
}
while (!(err = gpgme_op_keylist_next (ctx, &key)) )
{
unsigned int flags = 0;
if (key_check_cap (key, KEY_CAP_CAN_ENCRYPT))
flags |= KEYFLAG_CANENCRYPT;
if (key_check_cap (key, KEY_CAP_CAN_SIGN))
flags |= KEYFLAG_CANSIGN;
#if 0 /* DISABLED code */
if (!flags)
{
/* Bug in gpg. Capabilities are not listed for secret
keys. Try to deduce them from the algorithm. */
switch (key->subkeys[0].pubkey_algo)
{
case GPGME_PK_RSA:
flags |= KEYFLAG_CANENCRYPT;
flags |= KEYFLAG_CANSIGN;
break;
case GPGME_PK_ELG_E:
flags |= KEYFLAG_CANENCRYPT;
break;
case GPGME_PK_DSA:
flags |= KEYFLAG_CANSIGN;
break;
}
}
#endif /* DISABLED code */
for (idx = 0, uid = key->uids; uid; idx++, uid = uid->next)
{
k = safe_calloc (1, sizeof *k);
k->kobj = key;
k->idx = idx;
k->uid = uid->uid;
k->flags = flags;
*kend = k;
kend = &k->next;
}
}
if (gpg_err_code (err) != GPG_ERR_EOF)
mutt_error (_("gpgme_op_keylist_next failed: %s"), gpgme_strerror (err));
gpgme_op_keylist_end (ctx);
no_pgphints:
;
}
if ((app & APPLICATION_SMIME))
{
/* and now look for x509 certificates */
gpgme_set_protocol (ctx, GPGME_PROTOCOL_CMS);
err = gpgme_op_keylist_start (ctx, pattern, 0);
if (err)
{
mutt_error (_("gpgme_op_keylist_start failed: %s"),
gpgme_strerror (err));
gpgme_release (ctx);
FREE (&pattern);
return NULL;
}
while (!(err = gpgme_op_keylist_next (ctx, &key)) )
{
unsigned int flags = KEYFLAG_ISX509;
if (key_check_cap (key, KEY_CAP_CAN_ENCRYPT))
flags |= KEYFLAG_CANENCRYPT;
if (key_check_cap (key, KEY_CAP_CAN_SIGN))
flags |= KEYFLAG_CANSIGN;
for (idx = 0, uid = key->uids; uid; idx++, uid = uid->next)
{
k = safe_calloc (1, sizeof *k);
k->kobj = key;
k->idx = idx;
k->uid = uid->uid;
k->flags = flags;
*kend = k;
kend = &k->next;
}
}
if (gpg_err_code (err) != GPG_ERR_EOF)
mutt_error (_("gpgme_op_keylist_next failed: %s"), gpgme_strerror (err));
gpgme_op_keylist_end (ctx);
}
gpgme_release (ctx);
FREE (&pattern);
return db;
}
/* Add the string STR to the list HINTS. This list is later used to
match addresses. */
static LIST *crypt_add_string_to_hints (LIST *hints, const char *str)
{
char *scratch;
char *t;
if ((scratch = safe_strdup (str)) == NULL)
return hints;
for (t = strtok (scratch, " ,.:\"()<>\n"); t;
t = strtok (NULL, " ,.:\"()<>\n"))
{
if (strlen (t) > 3)
hints = mutt_add_list (hints, t);
}
FREE (&scratch);
return hints;
}
/* Display a menu to select a key from the array KEYS. FORCED_VALID
will be set to true on return if the user did override the the
key's validity. */
static crypt_key_t *crypt_select_key (crypt_key_t *keys,
ADDRESS * p, const char *s,
unsigned int app, int *forced_valid)
{
int keymax;
crypt_key_t **key_table;
MUTTMENU *menu;
int i, done = 0;
char helpstr[LONG_STRING], buf[LONG_STRING];
crypt_key_t *k;
int (*f) (const void *, const void *);
int menu_to_use = 0;
int unusable = 0;
*forced_valid = 0;
/* build the key table */
keymax = i = 0;
key_table = NULL;
for (k = keys; k; k = k->next)
{
if (!option (OPTPGPSHOWUNUSABLE) && (k->flags & KEYFLAG_CANTUSE))
{
unusable = 1;
continue;
}
if (i == keymax)
{
keymax += 20;
safe_realloc (&key_table, sizeof (crypt_key_t*)*keymax);
}
key_table[i++] = k;
}
if (!i && unusable)
{
mutt_error _("All matching keys are marked expired/revoked.");
mutt_sleep (1);
return NULL;
}
switch (PgpSortKeys & SORT_MASK)
{
case SORT_DATE:
f = crypt_compare_date;
break;
case SORT_KEYID:
f = crypt_compare_keyid;
break;
case SORT_ADDRESS:
f = crypt_compare_address;
break;
case SORT_TRUST:
default:
f = crypt_compare_trust;
break;
}
qsort (key_table, i, sizeof (crypt_key_t*), f);
if (app & APPLICATION_PGP)
menu_to_use = MENU_KEY_SELECT_PGP;
else if (app & APPLICATION_SMIME)
menu_to_use = MENU_KEY_SELECT_SMIME;
helpstr[0] = 0;
mutt_make_help (buf, sizeof (buf), _("Exit "), menu_to_use, OP_EXIT);
strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
mutt_make_help (buf, sizeof (buf), _("Select "), menu_to_use,
OP_GENERIC_SELECT_ENTRY);
strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
mutt_make_help (buf, sizeof (buf), _("Check key "),
menu_to_use, OP_VERIFY_KEY);
strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
mutt_make_help (buf, sizeof (buf), _("Help"), menu_to_use, OP_HELP);
strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
menu = mutt_new_menu (menu_to_use);
menu->max = i;
menu->make_entry = crypt_entry;
menu->help = helpstr;
menu->data = key_table;
{
const char *ts;
if ((app & APPLICATION_PGP) && (app & APPLICATION_SMIME))
ts = _("PGP and S/MIME keys matching");
else if ((app & APPLICATION_PGP))
ts = _("PGP keys matching");
else if ((app & APPLICATION_SMIME))
ts = _("S/MIME keys matching");
else
ts = _("keys matching");
if (p)
snprintf (buf, sizeof (buf), _("%s <%s>."), ts, p->mailbox);
else
snprintf (buf, sizeof (buf), _("%s \"%s\"."), ts, s);
menu->title = buf;
}
mutt_clear_error ();
k = NULL;
while (!done)
{
*forced_valid = 0;
switch (mutt_menuLoop (menu))
{
case OP_VERIFY_KEY:
verify_key (key_table[menu->current]);
menu->redraw = REDRAW_FULL;
break;
case OP_VIEW_ID:
mutt_message ("%s", key_table[menu->current]->uid);
break;
case OP_GENERIC_SELECT_ENTRY:
/* FIXME make error reporting more verbose - this should be
easy because gpgme provides more information */
if (option (OPTPGPCHECKTRUST))
{
if (!crypt_key_is_valid (key_table[menu->current]))
{
mutt_error _("This key can't be used: "
"expired/disabled/revoked.");
break;
}
}
if (option (OPTPGPCHECKTRUST) &&
(!crypt_id_is_valid (key_table[menu->current])
|| !crypt_id_is_strong (key_table[menu->current])))
{
const char *warn_s;
char buff[LONG_STRING];
if (key_table[menu->current]->flags & KEYFLAG_CANTUSE)
s = N_("ID is expired/disabled/revoked.");
else
{
gpgme_validity_t val = GPGME_VALIDITY_UNKNOWN;
gpgme_user_id_t uid = NULL;
unsigned int j = 0;
warn_s = "??";
uid = key_table[menu->current]->kobj->uids;
for (j = 0; (j < key_table[menu->current]->idx) && uid;
j++, uid = uid->next)
;
if (uid)
val = uid->validity;
switch (val)
{
case GPGME_VALIDITY_UNKNOWN:
case GPGME_VALIDITY_UNDEFINED:
warn_s = N_("ID has undefined validity.");
break;
case GPGME_VALIDITY_NEVER:
warn_s = N_("ID is not valid.");
break;
case GPGME_VALIDITY_MARGINAL:
warn_s = N_("ID is only marginally valid.");
break;
case GPGME_VALIDITY_FULL:
case GPGME_VALIDITY_ULTIMATE:
break;
}
snprintf (buff, sizeof (buff),
_("%s Do you really want to use the key?"),
_(warn_s));
if (mutt_yesorno (buff, 0) != 1)
{
mutt_clear_error ();
break;
}
*forced_valid = 1;
}
}
k = crypt_copy_key (key_table[menu->current]);
done = 1;
break;
case OP_EXIT:
k = NULL;
done = 1;
break;
}
}
mutt_menuDestroy (&menu);
FREE (&key_table);
set_option (OPTNEEDREDRAW);
return k;
}
static crypt_key_t *crypt_getkeybyaddr (ADDRESS * a, short abilities,
unsigned int app, int *forced_valid)
{
ADDRESS *r, *p;
LIST *hints = NULL;
int weak = 0;
int invalid = 0;
int multi = 0;
int this_key_has_strong;
int this_key_has_weak;
int this_key_has_invalid;
int match;
crypt_key_t *keys, *k;
crypt_key_t *the_valid_key = NULL;
crypt_key_t *matches = NULL;
crypt_key_t **matches_endp = &matches;
*forced_valid = 0;
if (a && a->mailbox)
hints = crypt_add_string_to_hints (hints, a->mailbox);
if (a && a->personal)
hints = crypt_add_string_to_hints (hints, a->personal);
mutt_message (_("Looking for keys matching \"%s\"..."), a->mailbox);
keys = get_candidates (hints, app, (abilities & KEYFLAG_CANSIGN) );
mutt_free_list (&hints);
if (!keys)
return NULL;
dprint (5, (debugfile, "crypt_getkeybyaddr: looking for %s <%s>.",
a->personal, a->mailbox));
for (k = keys; k; k = k->next)
{
dprint (5, (debugfile, " looking at key: %s `%.15s'\n",
crypt_keyid (k), k->uid));
if (abilities && !(k->flags & abilities))
{
dprint (5, (debugfile, " insufficient abilities: Has %x, want %x\n",
k->flags, abilities));
continue;
}
this_key_has_weak = 0; /* weak but valid match */
this_key_has_invalid = 0; /* invalid match */
this_key_has_strong = 0; /* strong and valid match */
match = 0; /* any match */
r = rfc822_parse_adrlist (NULL, k->uid);
for (p = r; p; p = p->next)
{
int validity = crypt_id_matches_addr (a, p, k);
if (validity & CRYPT_KV_MATCH) /* something matches */
match = 1;
/* is this key a strong candidate? */
if ((validity & CRYPT_KV_VALID)
&& (validity & CRYPT_KV_STRONGID)
&& (validity & CRYPT_KV_ADDR))
{
if (the_valid_key && the_valid_key != k)
multi = 1;
the_valid_key = k;
this_key_has_strong = 1;
}
else if ((validity & CRYPT_KV_MATCH)
&& !(validity & CRYPT_KV_VALID))
this_key_has_invalid = 1;
else if ((validity & CRYPT_KV_MATCH)
&& (!(validity & CRYPT_KV_STRONGID)
|| !(validity & CRYPT_KV_ADDR)))
this_key_has_weak = 1;
}
rfc822_free_address (&r);
if (match)
{
crypt_key_t *tmp;
if (!this_key_has_strong && this_key_has_invalid)
invalid = 1;
if (!this_key_has_strong && this_key_has_weak)
weak = 1;
*matches_endp = tmp = crypt_copy_key (k);
matches_endp = &tmp->next;
the_valid_key = tmp;
}
}
crypt_free_key (&keys);
if (matches)
{
if (the_valid_key && !multi && !weak
&& !(invalid && option (OPTPGPSHOWUNUSABLE)))
{
/*
* There was precisely one strong match on a valid ID, there
* were no valid keys with weak matches, and we aren't
* interested in seeing invalid keys.
*
* Proceed without asking the user.
*/
k = crypt_copy_key (the_valid_key);
}
else
{
/*
* Else: Ask the user.
*/
k = crypt_select_key (matches, a, NULL, app, forced_valid);
}
crypt_free_key (&matches);
}
else
k = NULL;
return k;
}
static crypt_key_t *crypt_getkeybystr (char *p, short abilities,
unsigned int app, int *forced_valid)
{
LIST *hints = NULL;
crypt_key_t *keys;
crypt_key_t *matches = NULL;
crypt_key_t **matches_endp = &matches;
crypt_key_t *k;
int match;
mutt_message (_("Looking for keys matching \"%s\"..."), p);
*forced_valid = 0;
hints = crypt_add_string_to_hints (hints, p);
keys = get_candidates (hints, app, (abilities & KEYFLAG_CANSIGN));
mutt_free_list (&hints);
if (!keys)
return NULL;
for (k = keys; k; k = k->next)
{
if (abilities && !(k->flags & abilities))
continue;
match = 0;
dprint (5, (debugfile, "crypt_getkeybystr: matching \"%s\" against "
"key %s, \"%s\": ", p, crypt_keyid (k), k->uid));
if (!*p
|| !mutt_strcasecmp (p, crypt_keyid (k))
|| (!mutt_strncasecmp (p, "0x", 2)
&& !mutt_strcasecmp (p + 2, crypt_keyid (k)))
|| (option (OPTPGPLONGIDS)
&& !mutt_strncasecmp (p, "0x", 2)
&& !mutt_strcasecmp (p + 2, crypt_keyid (k) + 8))
|| mutt_stristr (k->uid, p))
{
crypt_key_t *tmp;
dprint (5, (debugfile, "match.\n"));
*matches_endp = tmp = crypt_copy_key (k);
matches_endp = &tmp->next;
}
}
crypt_free_key (&keys);
if (matches)
{
k = crypt_select_key (matches, NULL, p, app, forced_valid);
crypt_free_key (&matches);
return k;
}
return NULL;
}
/* Display TAG as a prompt to ask for a key. If WHATFOR is not null
use it as default and store it under that label as the next
default. ABILITIES describe the required key abilities (sign,
encrypt) and APP the type of the requested key; ether S/MIME or
PGP. Return a copy of the key or NULL if not found. */
static crypt_key_t *crypt_ask_for_key (char *tag,
char *whatfor,
short abilities,
unsigned int app,
int *forced_valid)
{
crypt_key_t *key;
char resp[SHORT_STRING];
struct crypt_cache *l = NULL;
int dummy;
if (!forced_valid)
forced_valid = &dummy;
mutt_clear_error ();
*forced_valid = 0;
resp[0] = 0;
if (whatfor)
{
for (l = id_defaults; l; l = l->next)
if (!mutt_strcasecmp (whatfor, l->what))
{
strfcpy (resp, NONULL (l->dflt), sizeof (resp));
break;
}
}
for (;;)
{
resp[0] = 0;
if (mutt_get_field (tag, resp, sizeof (resp), M_CLEAR) != 0)
return NULL;
if (whatfor)
{
if (l)
mutt_str_replace (&l->dflt, resp);
else
{
l = safe_malloc (sizeof (struct crypt_cache));
l->next = id_defaults;
id_defaults = l;
l->what = safe_strdup (whatfor);
l->dflt = safe_strdup (resp);
}
}
if ((key = crypt_getkeybystr (resp, abilities, app, forced_valid)))
return key;
BEEP ();
}
/* not reached */
}
/* This routine attempts to find the keyids of the recipients of a
message. It returns NULL if any of the keys can not be found. */
static char *find_keys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc,
unsigned int app)
{
char *keyID, *keylist = NULL, *t;
size_t keylist_size = 0;
size_t keylist_used = 0;
ADDRESS *tmp = NULL, *addr = NULL;
ADDRESS **last = &tmp;
ADDRESS *p, *q;
int i;
crypt_key_t *k_info, *key;
const char *fqdn = mutt_fqdn (1);
#if 0
*r_application = APPLICATION_PGP|APPLICATION_SMIME;
#endif
for (i = 0; i < 3; i++)
{
switch (i)
{
case 0: p = to; break;
case 1: p = cc; break;
case 2: p = bcc; break;
default: abort ();
}
*last = rfc822_cpy_adr (p, 0);
while (*last)
last = &((*last)->next);
}
if (fqdn)
rfc822_qualify (tmp, fqdn);
tmp = mutt_remove_duplicates (tmp);
for (p = tmp; p ; p = p->next)
{
char buf[LONG_STRING];
int forced_valid = 0;
q = p;
k_info = NULL;
if ((keyID = mutt_crypt_hook (p)) != NULL)
{
int r;
snprintf (buf, sizeof (buf), _("Use keyID = \"%s\" for %s?"),
keyID, p->mailbox);
if ((r = mutt_yesorno (buf, M_YES)) == M_YES)
{
/* check for e-mail address */
if ((t = strchr (keyID, '@')) &&
(addr = rfc822_parse_adrlist (NULL, keyID)))
{
if (fqdn)
rfc822_qualify (addr, fqdn);
q = addr;
}
else
{
#if 0
k_info = crypt_getkeybystr (keyID, KEYFLAG_CANENCRYPT,
*r_application, &forced_valid);
#else
k_info = crypt_getkeybystr (keyID, KEYFLAG_CANENCRYPT,
app, &forced_valid);
#endif
}
}
else if (r == -1)
{
FREE (&keylist);
rfc822_free_address (&tmp);
rfc822_free_address (&addr);
return NULL;
}
}
if (k_info == NULL
&& (k_info = crypt_getkeybyaddr (q, KEYFLAG_CANENCRYPT,
app, &forced_valid)) == NULL)
{
snprintf (buf, sizeof (buf), _("Enter keyID for %s: "), q->mailbox);
if ((key = crypt_ask_for_key (buf, q->mailbox,
KEYFLAG_CANENCRYPT,
#if 0
*r_application,
#else
app,
#endif
&forced_valid)) == NULL)
{
FREE (&keylist);
rfc822_free_address (&tmp);
rfc822_free_address (&addr);
return NULL;
}
}
else
key = k_info;
{
const char *s = crypt_fpr (key);
#if 0
if (key->flags & KEYFLAG_ISX509)
*r_application &= ~APPLICATION_PGP;
if (!(key->flags & KEYFLAG_ISX509))
*r_application &= ~APPLICATION_SMIME;
#endif
keylist_size += mutt_strlen (s) + 4 + 1;
safe_realloc (&keylist, keylist_size);
sprintf (keylist + keylist_used, "%s0x%s%s", /* __SPRINTF_CHECKED__ */
keylist_used ? " " : "", s,
forced_valid? "!":"");
}
keylist_used = mutt_strlen (keylist);
crypt_free_key (&key);
rfc822_free_address (&addr);
}
rfc822_free_address (&tmp);
return (keylist);
}
char *pgp_gpgme_findkeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc)
{
return find_keys (to, cc, bcc, APPLICATION_PGP);
}
char *smime_gpgme_findkeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc)
{
return find_keys (to, cc, bcc, APPLICATION_SMIME);
}
/*
* Implementation of `init'.
*/
/* Initialization. */
static void init_gpgme (void)
{
/* Make sure that gpg-agent is running. */
if (! getenv ("GPG_AGENT_INFO"))
{
mutt_error (_("\nUsing GPGME backend, although no gpg-agent is running"));
if (mutt_any_key_to_continue (NULL) == -1)
mutt_exit(1);
}
}
void pgp_gpgme_init (void)
{
init_gpgme ();
}
void smime_gpgme_init (void)
{
}
static int gpgme_send_menu (HEADER *msg, int *redraw, int is_smime)
{
crypt_key_t *p;
char input_signas[SHORT_STRING];
int choice;
if (msg->security & APPLICATION_PGP)
is_smime = 0;
else if (msg->security & APPLICATION_SMIME)
is_smime = 1;
if (is_smime)
choice = mutt_multi_choice (
_("S/MIME (e)ncrypt, (s)ign, sign (a)s, (b)oth, (p)gp or (c)lear?"),
_("esabpfc"));
else
choice = mutt_multi_choice (
_("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, s/(m)ime or (c)lear?"),
_("esabmfc"));
switch (choice)
{
case 1: /* (e)ncrypt */
msg->security |= (is_smime ? SMIMEENCRYPT : PGPENCRYPT);
msg->security &= ~(is_smime ? SMIMESIGN : PGPSIGN);
break;
case 2: /* (s)ign */
msg->security |= (is_smime? SMIMESIGN :PGPSIGN);
msg->security &= ~(is_smime ? SMIMEENCRYPT : PGPENCRYPT);
break;
case 3: /* sign (a)s */
/* unset_option(OPTCRYPTCHECKTRUST); */
if ((p = crypt_ask_for_key (_("Sign as: "), NULL, KEYFLAG_CANSIGN,
is_smime? APPLICATION_SMIME:APPLICATION_PGP,
NULL)))
{
snprintf (input_signas, sizeof (input_signas), "0x%s", crypt_keyid (p));
mutt_str_replace (is_smime? &SmimeDefaultKey : &PgpSignAs, input_signas);
crypt_free_key (&p);
msg->security |= (is_smime? SMIMESIGN:PGPSIGN);
}
#if 0
else
{
msg->security &= (is_smime? ~SMIMESIGN : ~PGPSIGN);
}
#endif
*redraw = REDRAW_FULL;
break;
case 4: /* (b)oth */
msg->security = (is_smime? (SMIMEENCRYPT|SMIMESIGN):(PGPENCRYPT|PGPSIGN));
break;
case 5: /* (p)gp or s/(m)ime */
is_smime = !is_smime;
break;
case 6: /* (f)orget it */
case 7: /* (c)lear */
msg->security = 0;
break;
}
if (choice == 6 || choice == 7)
;
else if (is_smime)
{
msg->security &= ~APPLICATION_PGP;
msg->security |= APPLICATION_SMIME;
}
else
{
msg->security &= ~APPLICATION_SMIME;
msg->security |= APPLICATION_PGP;
}
return (msg->security);
}
int pgp_gpgme_send_menu (HEADER *msg, int *redraw)
{
return gpgme_send_menu (msg, redraw, 0);
}
int smime_gpgme_send_menu (HEADER *msg, int *redraw)
{
return gpgme_send_menu (msg, redraw, 1);
}
static int verify_sender (HEADER *h, gpgme_protocol_t protocol)
{
ADDRESS *sender = NULL;
unsigned int ret = 1;
if (h->env->from)
{
h->env->from = mutt_expand_aliases (h->env->from);
sender = h->env->from;
}
else if (h->env->sender)
{
h->env->sender = mutt_expand_aliases (h->env->sender);
sender = h->env->sender;
}
if (sender)
{
if (signature_key)
{
gpgme_key_t key = signature_key;
gpgme_user_id_t uid = NULL;
int sender_length = 0;
int uid_length = 0;
sender_length = strlen (sender->mailbox);
for (uid = key->uids; uid && ret; uid = uid->next)
{
uid_length = strlen (uid->email);
if (1
&& (uid->email[0] == '<')
&& (uid->email[uid_length - 1] == '>')
&& (uid_length == sender_length + 2)
&& (! strncmp (uid->email + 1, sender->mailbox, sender_length)))
ret = 0;
}
}
else
mutt_any_key_to_continue (_("Failed to verify sender"));
}
else
mutt_any_key_to_continue (_("Failed to figure out sender"));
if (signature_key)
{
gpgme_key_release (signature_key);
signature_key = NULL;
}
return ret;
}
int smime_gpgme_verify_sender (HEADER *h)
{
return verify_sender (h, GPGME_PROTOCOL_CMS);
}
void gpgme_set_sender (const char *sender)
{
mutt_error ("[setting sender] mailbox: %s\n", sender);
FREE (¤t_sender);
current_sender = safe_strdup (sender);
}
#endif
|