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
|
# BEGIN BPS TAGGED BLOCK {{{
#
# COPYRIGHT:
#
# This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC
# <sales@bestpractical.com>
#
# (Except where explicitly superseded by other copyright notices)
#
#
# LICENSE:
#
# This work is made available to you under the terms of Version 2 of
# the GNU General Public License. A copy of that license should have
# been provided with this software, but in any event can be snarfed
# from www.gnu.org.
#
# This work 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 or visit their web page on the internet at
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
#
#
# CONTRIBUTION SUBMISSION POLICY:
#
# (The following paragraph is not intended to limit the rights granted
# to you to modify and distribute this software under the terms of
# the GNU General Public License and is only of importance to you if
# you choose to contribute your changes and enhancements to the
# community by submitting them to Best Practical Solutions, LLC.)
#
# By intentionally submitting any modifications, corrections or
# derivatives to this work, or any other work intended for use with
# Request Tracker, to Best Practical Solutions, LLC, you confirm that
# you are the copyright holder for those contributions and you grant
# Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
# royalty-free, perpetual, license to use, copy, create derivative
# works based on those contributions, and sublicense and distribute
# those contributions and any derivatives thereof.
#
# END BPS TAGGED BLOCK }}}
use strict;
use warnings;
package RT::Crypt::GnuPG;
use IO::Handle;
use GnuPG::Interface;
use RT::EmailParser ();
use RT::Util 'safe_run_child', 'mime_recommended_filename';
=head1 NAME
RT::Crypt::GnuPG - encrypt/decrypt and sign/verify email messages with the GNU Privacy Guard (GPG)
=head1 DESCRIPTION
This module provides support for encryption and signing of outgoing messages,
as well as the decryption and verification of incoming email.
=head1 CONFIGURATION
You can control the configuration of this subsystem from RT's configuration file.
Some options are available via the web interface, but to enable this functionality, you
MUST start in the configuration file.
There are two hashes, GnuPG and GnuPGOptions in the configuration file. The
first one controls RT specific options. It enables you to enable/disable facility
or change the format of messages. The second one is a hash with options for the
'gnupg' utility. You can use it to define a keyserver, enable auto-retrieval keys
and set almost any option 'gnupg' supports on your system.
=head2 %GnuPG
=head3 Enabling GnuPG
Set to true value to enable this subsystem:
Set( %GnuPG,
Enable => 1,
... other options ...
);
However, note that you B<must> add the 'Auth::GnuPG' email filter to enable
the handling of incoming encrypted/signed messages.
=head3 Format of outgoing messages
Format of outgoing messages can be controlled using the 'OutgoingMessagesFormat'
option in the RT config:
Set( %GnuPG,
... other options ...
OutgoingMessagesFormat => 'RFC',
... other options ...
);
or
Set( %GnuPG,
... other options ...
OutgoingMessagesFormat => 'Inline',
... other options ...
);
This framework implements two formats of signing and encrypting of email messages:
=over
=item RFC
This format is also known as GPG/MIME and described in RFC3156 and RFC1847.
Technique described in these RFCs is well supported by many mail user
agents (MUA), but some MUAs support only inline signatures and encryption,
so it's possible to use inline format (see below).
=item Inline
This format doesn't take advantage of MIME, but some mail clients do
not support GPG/MIME.
We sign text parts using clear signatures. For each attachments another
attachment with a signature is added with '.sig' extension.
Encryption of text parts is implemented using inline format, other parts
are replaced with attachments with the filename extension '.pgp'.
This format is discouraged because modern mail clients typically don't support
it well.
=back
=head3 Encrypting data in the database
You can allow users to encrypt data in the database using
option C<AllowEncryptDataInDB>. By default it's disabled.
Users must have rights to see and modify tickets to use
this feature.
=head2 %GnuPGOptions
Use this hash to set options of the 'gnupg' program. You can define almost any
option you want which gnupg supports, but never try to set options which
change output format or gnupg's commands, such as --sign (command),
--list-options (option) and other.
Some GnuPG options take arguments while others take none. (Such as --use-agent).
For options without specific value use C<undef> as hash value.
To disable these option just comment them out or delete them from the hash
Set(%GnuPGOptions,
'option-with-value' => 'value',
'enabled-option-without-value' => undef,
# 'commented-option' => 'value or undef',
);
B<NOTE> that options may contain '-' character and such options B<MUST> be
quoted, otherwise you can see quite cryptic error 'gpg: Invalid option "--0"'.
=over
=item --homedir
The GnuPG home directory, by default it is set to F</opt/rt4/var/data/gpg>.
You can manage this data with the 'gpg' commandline utility
using the GNUPGHOME environment variable or --homedir option.
Other utilities may be used as well.
In a standard installation, access to this directory should be granted to
the web server user which is running RT's web interface, but if you're running
cronjobs or other utilities that access RT directly via API and may generate
encrypted/signed notifications then the users you execute these scripts under
must have access too.
However, granting access to the dir to many users makes your setup less secure,
some features, such as auto-import of keys, may not be available if you do not.
To enable this features and suppress warnings about permissions on
the dir use --no-permission-warning.
=item --digest-algo
This option is required in advance when RFC format for outgoing messages is
used. We can not get default algorithm from gpg program so RT uses 'SHA1' by
default. You may want to override it. You can use MD5, SHA1, RIPEMD160,
SHA256 or other, however use `gpg --version` command to get information about
supported algorithms by your gpg. These algorithms are listed as hash-functions.
=item --use-agent
This option lets you use GPG Agent to cache the passphrase of RT's key. See
L<http://www.gnupg.org/documentation/manuals/gnupg/Invoking-GPG_002dAGENT.html>
for information about GPG Agent.
=item --passphrase
This option lets you set the passphrase of RT's key directly. This option is
special in that it isn't passed directly to GPG, but is put into a file that
GPG then reads (which is more secure). The downside is that anyone who has read
access to your RT_SiteConfig.pm file can see the passphrase, thus we recommend
the --use-agent option instead.
=item other
Read `man gpg` to get list of all options this program support.
=back
=head2 Per-queue options
Using the web interface it's possible to enable signing and/or encrypting by
default. As an administrative user of RT, open 'Configuration' then 'Queues',
and select a queue. On the page you can see information about the queue's keys
at the bottom and two checkboxes to choose default actions.
As well, encryption is enabled for autoreplies and other notifications when
an encypted message enters system via mailgate interface even if queue's
option is disabled.
=head2 Handling incoming messages
To enable handling of encrypted and signed message in the RT you should add
'Auth::GnuPG' mail plugin.
Set(@MailPlugins, 'Auth::MailFrom', 'Auth::GnuPG', ...other filter...);
See also `perldoc lib/RT/Interface/Email/Auth/GnuPG.pm`.
=head2 Errors handling
There are several global templates created in the database by default. RT
uses these templates to send error messages to users or RT's owner. These
templates have 'Error:' or 'Error to RT owner:' prefix in the name. You can
adjust the text of the messages using the web interface.
Note that C<$TicketObj>, C<$TransactionObj> and other variable usually available
in RT's templates are not available in these templates, but each template
used for errors reporting has set of available data structures you can use to
build better messages. See default templates and descriptions below.
As well, you can disable particular notification by deleting content of
a template. You can delete a template too, but in this case you'll see
error messages in the logs when RT can not load template you've deleted.
=head3 Problems with public keys
Template 'Error: public key' is used to inform the user that RT has problems with
his public key and won't be able to send him encrypted content. There are several
reasons why RT can't use a key. However, the actual reason is not sent to the user,
but sent to RT owner using 'Error to RT owner: public key'.
The possible reasons: "Not Found", "Ambiguous specification", "Wrong
key usage", "Key revoked", "Key expired", "No CRL known", "CRL too
old", "Policy mismatch", "Not a secret key", "Key not trusted" or
"No specific reason given".
Due to limitations of GnuPG, it's impossible to encrypt to an untrusted key,
unless 'always trust' mode is enabled.
In the 'Error: public key' template there are a few additional variables available:
=over 4
=item $Message - user friendly error message
=item $Reason - short reason as listed above
=item $Recipient - recipient's identification
=item $AddressObj - L<Email::Address> object containing recipient's email address
=back
A message can have several invalid recipients, to avoid sending many emails
to the RT owner the system sends one message to the owner, grouped by
recipient. In the 'Error to RT owner: public key' template a C<@BadRecipients>
array is available where each element is a hash reference that describes one
recipient using the same fields as described above. So it's something like:
@BadRecipients = (
{ Message => '...', Reason => '...', Recipient => '...', ...},
{ Message => '...', Reason => '...', Recipient => '...', ...},
...
)
=head3 Private key doesn't exist
Template 'Error: no private key' is used to inform the user that
he sent an encrypted email, but we have no private key to decrypt
it.
In this template C<$Message> object of L<MIME::Entity> class
available. It's the message RT received.
=head3 Invalid data
Template 'Error: bad GnuPG data' used to inform the user that a
message he sent has invalid data and can not be handled.
There are several reasons for this error, but most of them are data
corruption or absence of expected information.
In this template C<@Messages> array is available and contains list
of error messages.
=head1 FOR DEVELOPERS
=head2 Documentation and references
* RFC1847 - Security Multiparts for MIME: Multipart/Signed and Multipart/Encrypted.
Describes generic MIME security framework, "mulitpart/signed" and "multipart/encrypted"
MIME types.
* RFC3156 - MIME Security with Pretty Good Privacy (PGP),
updates RFC2015.
=cut
# gnupg options supported by GnuPG::Interface
# other otions should be handled via extra_args argument
my %supported_opt = map { $_ => 1 } qw(
always_trust
armor
batch
comment
compress_algo
default_key
encrypt_to
extra_args
force_v3_sigs
homedir
logger_fd
no_greeting
no_options
no_verbose
openpgp
options
passphrase_fd
quiet
recipients
rfc1991
status_fd
textmode
verbose
);
our $RE_FILE_EXTENSIONS = qr/pgp|asc/i;
# DEV WARNING: always pass all STD* handles to GnuPG interface even if we don't
# need them, just pass 'IO::Handle->new()' and then close it after safe_run_child.
# we don't want to leak anything into FCGI/Apache/MP handles, this break things.
# So code should look like:
# my $handles = GnuPG::Handles->new(
# stdin => ($handle{'stdin'} = IO::Handle->new()),
# stdout => ($handle{'stdout'} = IO::Handle->new()),
# stderr => ($handle{'stderr'} = IO::Handle->new()),
# ...
# );
=head2 SignEncrypt Entity => MIME::Entity, [ Encrypt => 1, Sign => 1, ... ]
Signs and/or encrypts an email message with GnuPG utility.
=over
=item Signing
During signing you can pass C<Signer> argument to set key we sign with this option
overrides gnupg's C<default-key> option. If C<Signer> argument is not provided
then address of a message sender is used.
As well you can pass C<Passphrase>, but if value is undefined then L</GetPassphrase>
called to get it.
=item Encrypting
During encryption you can pass a C<Recipients> array, otherwise C<To>, C<Cc> and
C<Bcc> fields of the message are used to fetch the list.
=back
Returns a hash with the following keys:
* exit_code
* error
* logger
* status
* message
=cut
sub SignEncrypt {
my %args = (@_);
my $entity = $args{'Entity'};
if ( $args{'Sign'} && !defined $args{'Signer'} ) {
$args{'Signer'} = UseKeyForSigning()
|| (Email::Address->parse( $entity->head->get( 'From' ) ))[0]->address;
}
if ( $args{'Encrypt'} && !$args{'Recipients'} ) {
my %seen;
$args{'Recipients'} = [
grep $_ && !$seen{ $_ }++, map $_->address,
map Email::Address->parse( $entity->head->get( $_ ) ),
qw(To Cc Bcc)
];
}
my $format = lc RT->Config->Get('GnuPG')->{'OutgoingMessagesFormat'} || 'RFC';
if ( $format eq 'inline' ) {
return SignEncryptInline( %args );
} else {
return SignEncryptRFC3156( %args );
}
}
sub SignEncryptRFC3156 {
my %args = (
Entity => undef,
Sign => 1,
Signer => undef,
Passphrase => undef,
Encrypt => 1,
Recipients => undef,
@_
);
my $gnupg = GnuPG::Interface->new();
my %opt = RT->Config->Get('GnuPGOptions');
# handling passphrase in GnuPGOptions
$args{'Passphrase'} = delete $opt{'passphrase'}
if !defined $args{'Passphrase'};
$opt{'digest-algo'} ||= 'SHA1';
$opt{'default_key'} = $args{'Signer'}
if $args{'Sign'} && $args{'Signer'};
$gnupg->options->hash_init(
_PrepareGnuPGOptions( %opt ),
armor => 1,
meta_interactive => 0,
);
my $entity = $args{'Entity'};
if ( $args{'Sign'} && !defined $args{'Passphrase'} ) {
$args{'Passphrase'} = GetPassphrase( Address => $args{'Signer'} );
}
my %res;
if ( $args{'Sign'} && !$args{'Encrypt'} ) {
# required by RFC3156(Ch. 5) and RFC1847(Ch. 2.1)
foreach ( grep !$_->is_multipart, $entity->parts_DFS ) {
my $tenc = $_->head->mime_encoding;
unless ( $tenc =~ m/^(?:7bit|quoted-printable|base64)$/i ) {
$_->head->mime_attr( 'Content-Transfer-Encoding'
=> $_->effective_type =~ m{^text/}? 'quoted-printable': 'base64'
);
}
}
my ($handles, $handle_list) = _make_gpg_handles(stdin =>IO::Handle::CRLF->new );
my %handle = %$handle_list;
$gnupg->passphrase( $args{'Passphrase'} );
eval {
local $SIG{'CHLD'} = 'DEFAULT';
my $pid = safe_run_child { $gnupg->detach_sign( handles => $handles ) };
$entity->make_multipart( 'mixed', Force => 1 );
{
local $SIG{'PIPE'} = 'IGNORE';
$entity->parts(0)->print( $handle{'stdin'} );
close $handle{'stdin'};
}
waitpid $pid, 0;
};
my $err = $@;
my @signature = readline $handle{'stdout'};
close $handle{'stdout'};
$res{'exit_code'} = $?;
foreach ( qw(stderr logger status) ) {
$res{$_} = do { local $/; readline $handle{$_} };
delete $res{$_} unless $res{$_} && $res{$_} =~ /\S/s;
close $handle{$_};
}
$RT::Logger->debug( $res{'status'} ) if $res{'status'};
$RT::Logger->warning( $res{'stderr'} ) if $res{'stderr'};
$RT::Logger->error( $res{'logger'} ) if $res{'logger'} && $?;
if ( $err || $res{'exit_code'} ) {
$res{'message'} = $err? $err : "gpg exitted with error code ". ($res{'exit_code'} >> 8);
return %res;
}
# setup RFC1847(Ch.2.1) requirements
my $protocol = 'application/pgp-signature';
$entity->head->mime_attr( 'Content-Type' => 'multipart/signed' );
$entity->head->mime_attr( 'Content-Type.protocol' => $protocol );
$entity->head->mime_attr( 'Content-Type.micalg' => 'pgp-'. lc $opt{'digest-algo'} );
$entity->attach(
Type => $protocol,
Disposition => 'inline',
Data => \@signature,
Encoding => '7bit',
);
}
if ( $args{'Encrypt'} ) {
my %seen;
$gnupg->options->push_recipients( $_ ) foreach
map UseKeyForEncryption($_) || $_,
grep !$seen{ $_ }++, map $_->address,
map Email::Address->parse( $entity->head->get( $_ ) ),
qw(To Cc Bcc);
my ($tmp_fh, $tmp_fn) = File::Temp::tempfile( UNLINK => 1 );
binmode $tmp_fh, ':raw';
my ($handles, $handle_list) = _make_gpg_handles(stdout => $tmp_fh);
my %handle = %$handle_list;
$handles->options( 'stdout' )->{'direct'} = 1;
$gnupg->passphrase( $args{'Passphrase'} ) if $args{'Sign'};
eval {
local $SIG{'CHLD'} = 'DEFAULT';
my $pid = safe_run_child { $args{'Sign'}
? $gnupg->sign_and_encrypt( handles => $handles )
: $gnupg->encrypt( handles => $handles ) };
$entity->make_multipart( 'mixed', Force => 1 );
{
local $SIG{'PIPE'} = 'IGNORE';
$entity->parts(0)->print( $handle{'stdin'} );
close $handle{'stdin'};
}
waitpid $pid, 0;
};
$res{'exit_code'} = $?;
foreach ( qw(stderr logger status) ) {
$res{$_} = do { local $/; readline $handle{$_} };
delete $res{$_} unless $res{$_} && $res{$_} =~ /\S/s;
close $handle{$_};
}
$RT::Logger->debug( $res{'status'} ) if $res{'status'};
$RT::Logger->warning( $res{'stderr'} ) if $res{'stderr'};
$RT::Logger->error( $res{'logger'} ) if $res{'logger'} && $?;
if ( $@ || $? ) {
$res{'message'} = $@? $@: "gpg exited with error code ". ($? >> 8);
return %res;
}
my $protocol = 'application/pgp-encrypted';
$entity->parts([]);
$entity->head->mime_attr( 'Content-Type' => 'multipart/encrypted' );
$entity->head->mime_attr( 'Content-Type.protocol' => $protocol );
$entity->attach(
Type => $protocol,
Disposition => 'inline',
Data => ['Version: 1',''],
Encoding => '7bit',
);
$entity->attach(
Type => 'application/octet-stream',
Disposition => 'inline',
Path => $tmp_fn,
Filename => '',
Encoding => '7bit',
);
$entity->parts(-1)->bodyhandle->{'_dirty_hack_to_save_a_ref_tmp_fh'} = $tmp_fh;
}
return %res;
}
sub SignEncryptInline {
my %args = ( @_ );
my $entity = $args{'Entity'};
my %res;
$entity->make_singlepart;
if ( $entity->is_multipart ) {
foreach ( $entity->parts ) {
%res = SignEncryptInline( @_, Entity => $_ );
return %res if $res{'exit_code'};
}
return %res;
}
return _SignEncryptTextInline( @_ )
if $entity->effective_type =~ /^text\//i;
return _SignEncryptAttachmentInline( @_ );
}
sub _SignEncryptTextInline {
my %args = (
Entity => undef,
Sign => 1,
Signer => undef,
Passphrase => undef,
Encrypt => 1,
Recipients => undef,
@_
);
return unless $args{'Sign'} || $args{'Encrypt'};
my $gnupg = GnuPG::Interface->new();
my %opt = RT->Config->Get('GnuPGOptions');
# handling passphrase in GnupGOptions
$args{'Passphrase'} = delete $opt{'passphrase'}
if !defined($args{'Passphrase'});
$opt{'digest-algo'} ||= 'SHA1';
$opt{'default_key'} = $args{'Signer'}
if $args{'Sign'} && $args{'Signer'};
$gnupg->options->hash_init(
_PrepareGnuPGOptions( %opt ),
armor => 1,
meta_interactive => 0,
);
if ( $args{'Sign'} && !defined $args{'Passphrase'} ) {
$args{'Passphrase'} = GetPassphrase( Address => $args{'Signer'} );
}
if ( $args{'Encrypt'} ) {
$gnupg->options->push_recipients( $_ ) foreach
map UseKeyForEncryption($_) || $_,
@{ $args{'Recipients'} || [] };
}
my %res;
my ($tmp_fh, $tmp_fn) = File::Temp::tempfile( UNLINK => 1 );
binmode $tmp_fh, ':raw';
my ($handles, $handle_list) = _make_gpg_handles(stdout => $tmp_fh);
my %handle = %$handle_list;
$handles->options( 'stdout' )->{'direct'} = 1;
$gnupg->passphrase( $args{'Passphrase'} ) if $args{'Sign'};
my $entity = $args{'Entity'};
eval {
local $SIG{'CHLD'} = 'DEFAULT';
my $method = $args{'Sign'} && $args{'Encrypt'}
? 'sign_and_encrypt'
: ($args{'Sign'}? 'clearsign': 'encrypt');
my $pid = safe_run_child { $gnupg->$method( handles => $handles ) };
{
local $SIG{'PIPE'} = 'IGNORE';
$entity->bodyhandle->print( $handle{'stdin'} );
close $handle{'stdin'};
}
waitpid $pid, 0;
};
$res{'exit_code'} = $?;
my $err = $@;
foreach ( qw(stderr logger status) ) {
$res{$_} = do { local $/; readline $handle{$_} };
delete $res{$_} unless $res{$_} && $res{$_} =~ /\S/s;
close $handle{$_};
}
$RT::Logger->debug( $res{'status'} ) if $res{'status'};
$RT::Logger->warning( $res{'stderr'} ) if $res{'stderr'};
$RT::Logger->error( $res{'logger'} ) if $res{'logger'} && $?;
if ( $err || $res{'exit_code'} ) {
$res{'message'} = $err? $err : "gpg exitted with error code ". ($res{'exit_code'} >> 8);
return %res;
}
$entity->bodyhandle( MIME::Body::File->new( $tmp_fn) );
$entity->{'__store_tmp_handle_to_avoid_early_cleanup'} = $tmp_fh;
return %res;
}
sub _SignEncryptAttachmentInline {
my %args = (
Entity => undef,
Sign => 1,
Signer => undef,
Passphrase => undef,
Encrypt => 1,
Recipients => undef,
@_
);
return unless $args{'Sign'} || $args{'Encrypt'};
my $gnupg = GnuPG::Interface->new();
my %opt = RT->Config->Get('GnuPGOptions');
# handling passphrase in GnupGOptions
$args{'Passphrase'} = delete $opt{'passphrase'}
if !defined($args{'Passphrase'});
$opt{'digest-algo'} ||= 'SHA1';
$opt{'default_key'} = $args{'Signer'}
if $args{'Sign'} && $args{'Signer'};
$gnupg->options->hash_init(
_PrepareGnuPGOptions( %opt ),
armor => 1,
meta_interactive => 0,
);
if ( $args{'Sign'} && !defined $args{'Passphrase'} ) {
$args{'Passphrase'} = GetPassphrase( Address => $args{'Signer'} );
}
my $entity = $args{'Entity'};
if ( $args{'Encrypt'} ) {
$gnupg->options->push_recipients( $_ ) foreach
map UseKeyForEncryption($_) || $_,
@{ $args{'Recipients'} || [] };
}
my %res;
my ($tmp_fh, $tmp_fn) = File::Temp::tempfile( UNLINK => 1 );
binmode $tmp_fh, ':raw';
my ($handles, $handle_list) = _make_gpg_handles(stdout => $tmp_fh);
my %handle = %$handle_list;
$handles->options( 'stdout' )->{'direct'} = 1;
$gnupg->passphrase( $args{'Passphrase'} ) if $args{'Sign'};
eval {
local $SIG{'CHLD'} = 'DEFAULT';
my $method = $args{'Sign'} && $args{'Encrypt'}
? 'sign_and_encrypt'
: ($args{'Sign'}? 'detach_sign': 'encrypt');
my $pid = safe_run_child { $gnupg->$method( handles => $handles ) };
{
local $SIG{'PIPE'} = 'IGNORE';
$entity->bodyhandle->print( $handle{'stdin'} );
close $handle{'stdin'};
}
waitpid $pid, 0;
};
$res{'exit_code'} = $?;
my $err = $@;
foreach ( qw(stderr logger status) ) {
$res{$_} = do { local $/; readline $handle{$_} };
delete $res{$_} unless $res{$_} && $res{$_} =~ /\S/s;
close $handle{$_};
}
$RT::Logger->debug( $res{'status'} ) if $res{'status'};
$RT::Logger->warning( $res{'stderr'} ) if $res{'stderr'};
$RT::Logger->error( $res{'logger'} ) if $res{'logger'} && $?;
if ( $err || $res{'exit_code'} ) {
$res{'message'} = $err? $err : "gpg exitted with error code ". ($res{'exit_code'} >> 8);
return %res;
}
my $filename = mime_recommended_filename( $entity ) || 'no_name';
if ( $args{'Sign'} && !$args{'Encrypt'} ) {
$entity->make_multipart;
$entity->attach(
Type => 'application/octet-stream',
Path => $tmp_fn,
Filename => "$filename.sig",
Disposition => 'attachment',
);
} else {
$entity->bodyhandle(MIME::Body::File->new( $tmp_fn) );
$entity->effective_type('application/octet-stream');
$entity->head->mime_attr( $_ => "$filename.pgp" )
foreach (qw(Content-Type.name Content-Disposition.filename));
}
$entity->{'__store_tmp_handle_to_avoid_early_cleanup'} = $tmp_fh;
return %res;
}
sub SignEncryptContent {
my %args = (
Content => undef,
Sign => 1,
Signer => undef,
Passphrase => undef,
Encrypt => 1,
Recipients => undef,
@_
);
return unless $args{'Sign'} || $args{'Encrypt'};
my $gnupg = GnuPG::Interface->new();
my %opt = RT->Config->Get('GnuPGOptions');
# handling passphrase in GnupGOptions
$args{'Passphrase'} = delete $opt{'passphrase'}
if !defined($args{'Passphrase'});
$opt{'digest-algo'} ||= 'SHA1';
$opt{'default_key'} = $args{'Signer'}
if $args{'Sign'} && $args{'Signer'};
$gnupg->options->hash_init(
_PrepareGnuPGOptions( %opt ),
armor => 1,
meta_interactive => 0,
);
if ( $args{'Sign'} && !defined $args{'Passphrase'} ) {
$args{'Passphrase'} = GetPassphrase( Address => $args{'Signer'} );
}
if ( $args{'Encrypt'} ) {
$gnupg->options->push_recipients( $_ ) foreach
map UseKeyForEncryption($_) || $_,
@{ $args{'Recipients'} || [] };
}
my %res;
my ($tmp_fh, $tmp_fn) = File::Temp::tempfile( UNLINK => 1 );
binmode $tmp_fh, ':raw';
my ($handles, $handle_list) = _make_gpg_handles(stdout => $tmp_fh);
my %handle = %$handle_list;
$handles->options( 'stdout' )->{'direct'} = 1;
$gnupg->passphrase( $args{'Passphrase'} ) if $args{'Sign'};
eval {
local $SIG{'CHLD'} = 'DEFAULT';
my $method = $args{'Sign'} && $args{'Encrypt'}
? 'sign_and_encrypt'
: ($args{'Sign'}? 'clearsign': 'encrypt');
my $pid = safe_run_child { $gnupg->$method( handles => $handles ) };
{
local $SIG{'PIPE'} = 'IGNORE';
$handle{'stdin'}->print( ${ $args{'Content'} } );
close $handle{'stdin'};
}
waitpid $pid, 0;
};
$res{'exit_code'} = $?;
my $err = $@;
foreach ( qw(stderr logger status) ) {
$res{$_} = do { local $/; readline $handle{$_} };
delete $res{$_} unless $res{$_} && $res{$_} =~ /\S/s;
close $handle{$_};
}
$RT::Logger->debug( $res{'status'} ) if $res{'status'};
$RT::Logger->warning( $res{'stderr'} ) if $res{'stderr'};
$RT::Logger->error( $res{'logger'} ) if $res{'logger'} && $?;
if ( $err || $res{'exit_code'} ) {
$res{'message'} = $err? $err : "gpg exitted with error code ". ($res{'exit_code'} >> 8);
return %res;
}
${ $args{'Content'} } = '';
seek $tmp_fh, 0, 0;
while (1) {
my $status = read $tmp_fh, my $buf, 4*1024;
unless ( defined $status ) {
$RT::Logger->crit( "couldn't read message: $!" );
} elsif ( !$status ) {
last;
}
${ $args{'Content'} } .= $buf;
}
return %res;
}
sub FindProtectedParts {
my %args = ( Entity => undef, CheckBody => 1, @_ );
my $entity = $args{'Entity'};
# inline PGP block, only in singlepart
unless ( $entity->is_multipart ) {
my $file = ($entity->head->recommended_filename||'') =~ /\.${RE_FILE_EXTENSIONS}$/;
my $io = $entity->open('r');
unless ( $io ) {
$RT::Logger->warning( "Entity of type ". $entity->effective_type ." has no body" );
return ();
}
while ( defined($_ = $io->getline) ) {
next unless /^-----BEGIN PGP (SIGNED )?MESSAGE-----/;
my $type = $1? 'signed': 'encrypted';
$RT::Logger->debug("Found $type inline part");
return {
Type => $type,
Format => !$file || $type eq 'signed'? 'Inline' : 'Attachment',
Data => $entity,
};
}
$io->close;
return ();
}
# RFC3156, multipart/{signed,encrypted}
if ( ( my $type = $entity->effective_type ) =~ /^multipart\/(?:encrypted|signed)$/ ) {
unless ( $entity->parts == 2 ) {
$RT::Logger->error( "Encrypted or signed entity must has two subparts. Skipped" );
return ();
}
my $protocol = $entity->head->mime_attr( 'Content-Type.protocol' );
unless ( $protocol ) {
$RT::Logger->error( "Entity is '$type', but has no protocol defined. Skipped" );
return ();
}
if ( $type eq 'multipart/encrypted' ) {
unless ( $protocol eq 'application/pgp-encrypted' ) {
$RT::Logger->info( "Skipping protocol '$protocol', only 'application/pgp-encrypted' is supported" );
return ();
}
$RT::Logger->debug("Found encrypted according to RFC3156 part");
return {
Type => 'encrypted',
Format => 'RFC3156',
Top => $entity,
Data => $entity->parts(1),
Info => $entity->parts(0),
};
} else {
unless ( $protocol eq 'application/pgp-signature' ) {
$RT::Logger->info( "Skipping protocol '$protocol', only 'application/pgp-signature' is supported" );
return ();
}
$RT::Logger->debug("Found signed according to RFC3156 part");
return {
Type => 'signed',
Format => 'RFC3156',
Top => $entity,
Data => $entity->parts(0),
Signature => $entity->parts(1),
};
}
}
# attachments signed with signature in another part
my @file_indices;
foreach my $i ( 0 .. $entity->parts - 1 ) {
my $part = $entity->parts($i);
# we can not associate a signature within an attachment
# without file names
my $fname = $part->head->recommended_filename;
next unless $fname;
if ( $part->effective_type eq 'application/pgp-signature' ) {
push @file_indices, $i;
}
elsif ( $fname =~ /\.sig$/i && $part->effective_type eq 'application/octet-stream' ) {
push @file_indices, $i;
}
}
my (@res, %skip);
foreach my $i ( @file_indices ) {
my $sig_part = $entity->parts($i);
$skip{"$sig_part"}++;
my $sig_name = $sig_part->head->recommended_filename;
my ($file_name) = $sig_name =~ /^(.*?)(?:\.sig)?$/;
my ($data_part_idx) =
grep $file_name eq ($entity->parts($_)->head->recommended_filename||''),
grep $sig_part ne $entity->parts($_),
0 .. $entity->parts - 1;
unless ( defined $data_part_idx ) {
$RT::Logger->error("Found $sig_name attachment, but didn't find $file_name");
next;
}
my $data_part_in = $entity->parts($data_part_idx);
$skip{"$data_part_in"}++;
$RT::Logger->debug("Found signature (in '$sig_name') of attachment '$file_name'");
push @res, {
Type => 'signed',
Format => 'Attachment',
Top => $entity,
Data => $data_part_in,
Signature => $sig_part,
};
}
# attachments with inline encryption
my @encrypted_indices =
grep {($entity->parts($_)->head->recommended_filename || '') =~ /\.${RE_FILE_EXTENSIONS}$/}
0 .. $entity->parts - 1;
foreach my $i ( @encrypted_indices ) {
my $part = $entity->parts($i);
$skip{"$part"}++;
$RT::Logger->debug("Found encrypted attachment '". $part->head->recommended_filename ."'");
push @res, {
Type => 'encrypted',
Format => 'Attachment',
Top => $entity,
Data => $part,
};
}
push @res, FindProtectedParts( Entity => $_ )
foreach grep !$skip{"$_"}, $entity->parts;
return @res;
}
=head2 VerifyDecrypt Entity => undef, [ Detach => 1, Passphrase => undef, SetStatus => 1 ]
=cut
sub VerifyDecrypt {
my %args = (
Entity => undef,
Detach => 1,
SetStatus => 1,
AddStatus => 0,
@_
);
my @protected = FindProtectedParts( Entity => $args{'Entity'} );
my @res;
# XXX: detaching may brake nested signatures
foreach my $item( grep $_->{'Type'} eq 'signed', @protected ) {
my $status_on;
if ( $item->{'Format'} eq 'RFC3156' ) {
push @res, { VerifyRFC3156( %$item, SetStatus => $args{'SetStatus'} ) };
if ( $args{'Detach'} ) {
$item->{'Top'}->parts( [ $item->{'Data'} ] );
$item->{'Top'}->make_singlepart;
}
$status_on = $item->{'Top'};
} elsif ( $item->{'Format'} eq 'Inline' ) {
push @res, { VerifyInline( %$item ) };
$status_on = $item->{'Data'};
} elsif ( $item->{'Format'} eq 'Attachment' ) {
push @res, { VerifyAttachment( %$item ) };
if ( $args{'Detach'} ) {
$item->{'Top'}->parts( [
grep "$_" ne $item->{'Signature'}, $item->{'Top'}->parts
] );
$item->{'Top'}->make_singlepart;
}
$status_on = $item->{'Data'};
}
if ( $args{'SetStatus'} || $args{'AddStatus'} ) {
my $method = $args{'AddStatus'} ? 'add' : 'set';
# Let the header be modified so continuations are handled
my $modify = $status_on->head->modify;
$status_on->head->modify(1);
$status_on->head->$method(
'X-RT-GnuPG-Status' => $res[-1]->{'status'}
);
$status_on->head->modify($modify);
}
}
foreach my $item( grep $_->{'Type'} eq 'encrypted', @protected ) {
my $status_on;
if ( $item->{'Format'} eq 'RFC3156' ) {
push @res, { DecryptRFC3156( %$item ) };
$status_on = $item->{'Top'};
} elsif ( $item->{'Format'} eq 'Inline' ) {
push @res, { DecryptInline( %$item ) };
$status_on = $item->{'Data'};
} elsif ( $item->{'Format'} eq 'Attachment' ) {
push @res, { DecryptAttachment( %$item ) };
$status_on = $item->{'Data'};
}
if ( $args{'SetStatus'} || $args{'AddStatus'} ) {
my $method = $args{'AddStatus'} ? 'add' : 'set';
# Let the header be modified so continuations are handled
my $modify = $status_on->head->modify;
$status_on->head->modify(1);
$status_on->head->$method(
'X-RT-GnuPG-Status' => $res[-1]->{'status'}
);
$status_on->head->modify($modify);
}
}
return @res;
}
sub VerifyInline { return DecryptInline( @_ ) }
sub VerifyAttachment {
my %args = ( Data => undef, Signature => undef, Top => undef, @_ );
my $gnupg = GnuPG::Interface->new();
my %opt = RT->Config->Get('GnuPGOptions');
$opt{'digest-algo'} ||= 'SHA1';
$gnupg->options->hash_init(
_PrepareGnuPGOptions( %opt ),
meta_interactive => 0,
);
foreach ( $args{'Data'}, $args{'Signature'} ) {
next unless $_->bodyhandle->is_encoded;
require RT::EmailParser;
RT::EmailParser->_DecodeBody($_);
}
my ($tmp_fh, $tmp_fn) = File::Temp::tempfile( UNLINK => 1 );
binmode $tmp_fh, ':raw';
$args{'Data'}->bodyhandle->print( $tmp_fh );
$tmp_fh->flush;
my ($handles, $handle_list) = _make_gpg_handles();
my %handle = %$handle_list;
my %res;
eval {
local $SIG{'CHLD'} = 'DEFAULT';
my $pid = safe_run_child { $gnupg->verify(
handles => $handles, command_args => [ '-', $tmp_fn ]
) };
{
local $SIG{'PIPE'} = 'IGNORE';
$args{'Signature'}->bodyhandle->print( $handle{'stdin'} );
close $handle{'stdin'};
}
waitpid $pid, 0;
};
$res{'exit_code'} = $?;
foreach ( qw(stderr logger status) ) {
$res{$_} = do { local $/; readline $handle{$_} };
delete $res{$_} unless $res{$_} && $res{$_} =~ /\S/s;
close $handle{$_};
}
$RT::Logger->debug( $res{'status'} ) if $res{'status'};
$RT::Logger->warning( $res{'stderr'} ) if $res{'stderr'};
$RT::Logger->error( $res{'logger'} ) if $res{'logger'} && $?;
if ( $@ || $? ) {
$res{'message'} = $@? $@: "gpg exitted with error code ". ($? >> 8);
}
return %res;
}
sub VerifyRFC3156 {
my %args = ( Data => undef, Signature => undef, Top => undef, @_ );
my $gnupg = GnuPG::Interface->new();
my %opt = RT->Config->Get('GnuPGOptions');
$opt{'digest-algo'} ||= 'SHA1';
$gnupg->options->hash_init(
_PrepareGnuPGOptions( %opt ),
meta_interactive => 0,
);
my ($tmp_fh, $tmp_fn) = File::Temp::tempfile( UNLINK => 1 );
binmode $tmp_fh, ':raw:eol(CRLF?)';
$args{'Data'}->print( $tmp_fh );
$tmp_fh->flush;
my ($handles, $handle_list) = _make_gpg_handles();
my %handle = %$handle_list;
my %res;
eval {
local $SIG{'CHLD'} = 'DEFAULT';
my $pid = safe_run_child { $gnupg->verify(
handles => $handles, command_args => [ '-', $tmp_fn ]
) };
{
local $SIG{'PIPE'} = 'IGNORE';
$args{'Signature'}->bodyhandle->print( $handle{'stdin'} );
close $handle{'stdin'};
}
waitpid $pid, 0;
};
$res{'exit_code'} = $?;
foreach ( qw(stderr logger status) ) {
$res{$_} = do { local $/; readline $handle{$_} };
delete $res{$_} unless $res{$_} && $res{$_} =~ /\S/s;
close $handle{$_};
}
$RT::Logger->debug( $res{'status'} ) if $res{'status'};
$RT::Logger->warning( $res{'stderr'} ) if $res{'stderr'};
$RT::Logger->error( $res{'logger'} ) if $res{'logger'} && $?;
if ( $@ || $? ) {
$res{'message'} = $@? $@: "gpg exitted with error code ". ($? >> 8);
}
return %res;
}
sub DecryptRFC3156 {
my %args = (
Data => undef,
Info => undef,
Top => undef,
Passphrase => undef,
@_
);
my $gnupg = GnuPG::Interface->new();
my %opt = RT->Config->Get('GnuPGOptions');
# handling passphrase in GnupGOptions
$args{'Passphrase'} = delete $opt{'passphrase'}
if !defined($args{'Passphrase'});
$opt{'digest-algo'} ||= 'SHA1';
$gnupg->options->hash_init(
_PrepareGnuPGOptions( %opt ),
meta_interactive => 0,
);
if ( $args{'Data'}->bodyhandle->is_encoded ) {
require RT::EmailParser;
RT::EmailParser->_DecodeBody($args{'Data'});
}
$args{'Passphrase'} = GetPassphrase()
unless defined $args{'Passphrase'};
my ($tmp_fh, $tmp_fn) = File::Temp::tempfile( UNLINK => 1 );
binmode $tmp_fh, ':raw';
my ($handles, $handle_list) = _make_gpg_handles(stdout => $tmp_fh);
my %handle = %$handle_list;
$handles->options( 'stdout' )->{'direct'} = 1;
my %res;
eval {
local $SIG{'CHLD'} = 'DEFAULT';
$gnupg->passphrase( $args{'Passphrase'} );
my $pid = safe_run_child { $gnupg->decrypt( handles => $handles ) };
{
local $SIG{'PIPE'} = 'IGNORE';
$args{'Data'}->bodyhandle->print( $handle{'stdin'} );
close $handle{'stdin'}
}
waitpid $pid, 0;
};
$res{'exit_code'} = $?;
foreach ( qw(stderr logger status) ) {
$res{$_} = do { local $/; readline $handle{$_} };
delete $res{$_} unless $res{$_} && $res{$_} =~ /\S/s;
close $handle{$_};
}
$RT::Logger->debug( $res{'status'} ) if $res{'status'};
$RT::Logger->warning( $res{'stderr'} ) if $res{'stderr'};
$RT::Logger->error( $res{'logger'} ) if $res{'logger'} && $?;
# if the decryption is fine but the signature is bad, then without this
# status check we lose the decrypted text
# XXX: add argument to the function to control this check
if ( $res{'status'} !~ /DECRYPTION_OKAY/ ) {
if ( $@ || $? ) {
$res{'message'} = $@? $@: "gpg exitted with error code ". ($? >> 8);
return %res;
}
}
seek $tmp_fh, 0, 0;
my $parser = RT::EmailParser->new();
my $decrypted = $parser->ParseMIMEEntityFromFileHandle( $tmp_fh, 0 );
$decrypted->{'__store_link_to_object_to_avoid_early_cleanup'} = $parser;
$args{'Top'}->parts( [] );
$args{'Top'}->add_part( $decrypted );
$args{'Top'}->make_singlepart;
return %res;
}
sub DecryptInline {
my %args = (
Data => undef,
Passphrase => undef,
@_
);
my $gnupg = GnuPG::Interface->new();
my %opt = RT->Config->Get('GnuPGOptions');
# handling passphrase in GnuPGOptions
$args{'Passphrase'} = delete $opt{'passphrase'}
if !defined($args{'Passphrase'});
$opt{'digest-algo'} ||= 'SHA1';
$gnupg->options->hash_init(
_PrepareGnuPGOptions( %opt ),
meta_interactive => 0,
);
if ( $args{'Data'}->bodyhandle->is_encoded ) {
require RT::EmailParser;
RT::EmailParser->_DecodeBody($args{'Data'});
}
$args{'Passphrase'} = GetPassphrase()
unless defined $args{'Passphrase'};
my ($tmp_fh, $tmp_fn) = File::Temp::tempfile( UNLINK => 1 );
binmode $tmp_fh, ':raw';
my $io = $args{'Data'}->open('r');
unless ( $io ) {
die "Entity has no body, never should happen";
}
my %res;
my ($had_literal, $in_block) = ('', 0);
my ($block_fh, $block_fn) = File::Temp::tempfile( UNLINK => 1 );
binmode $block_fh, ':raw';
while ( defined(my $str = $io->getline) ) {
if ( $in_block && $str =~ /^-----END PGP (?:MESSAGE|SIGNATURE)-----/ ) {
print $block_fh $str;
$in_block--;
next if $in_block > 0;
seek $block_fh, 0, 0;
my ($res_fh, $res_fn);
($res_fh, $res_fn, %res) = _DecryptInlineBlock(
%args,
GnuPG => $gnupg,
BlockHandle => $block_fh,
);
return %res unless $res_fh;
print $tmp_fh "-----BEGIN OF PGP PROTECTED PART-----\n" if $had_literal;
while (my $buf = <$res_fh> ) {
print $tmp_fh $buf;
}
print $tmp_fh "-----END OF PART-----\n" if $had_literal;
($block_fh, $block_fn) = File::Temp::tempfile( UNLINK => 1 );
binmode $block_fh, ':raw';
$in_block = 0;
}
elsif ( $str =~ /^-----BEGIN PGP (SIGNED )?MESSAGE-----/ ) {
$in_block++;
print $block_fh $str;
}
elsif ( $in_block ) {
print $block_fh $str;
}
else {
print $tmp_fh $str;
$had_literal = 1 if /\S/s;
}
}
$io->close;
if ( $in_block ) {
# we're still in a block, this not bad not good. let's try to
# decrypt what we have, it can be just missing -----END PGP...
seek $block_fh, 0, 0;
my ($res_fh, $res_fn);
($res_fh, $res_fn, %res) = _DecryptInlineBlock(
%args,
GnuPG => $gnupg,
BlockHandle => $block_fh,
);
return %res unless $res_fh;
print $tmp_fh "-----BEGIN OF PGP PROTECTED PART-----\n" if $had_literal;
while (my $buf = <$res_fh> ) {
print $tmp_fh $buf;
}
print $tmp_fh "-----END OF PART-----\n" if $had_literal;
}
seek $tmp_fh, 0, 0;
$args{'Data'}->bodyhandle(MIME::Body::File->new( $tmp_fn ));
$args{'Data'}->{'__store_tmp_handle_to_avoid_early_cleanup'} = $tmp_fh;
return %res;
}
sub _DecryptInlineBlock {
my %args = (
GnuPG => undef,
BlockHandle => undef,
Passphrase => undef,
@_
);
my $gnupg = $args{'GnuPG'};
my ($tmp_fh, $tmp_fn) = File::Temp::tempfile( UNLINK => 1 );
binmode $tmp_fh, ':raw';
my ($handles, $handle_list) = _make_gpg_handles(
stdin => $args{'BlockHandle'},
stdout => $tmp_fh);
my %handle = %$handle_list;
$handles->options( 'stdout' )->{'direct'} = 1;
$handles->options( 'stdin' )->{'direct'} = 1;
my %res;
eval {
local $SIG{'CHLD'} = 'DEFAULT';
$gnupg->passphrase( $args{'Passphrase'} );
my $pid = safe_run_child { $gnupg->decrypt( handles => $handles ) };
waitpid $pid, 0;
};
$res{'exit_code'} = $?;
foreach ( qw(stderr logger status) ) {
$res{$_} = do { local $/; readline $handle{$_} };
delete $res{$_} unless $res{$_} && $res{$_} =~ /\S/s;
close $handle{$_};
}
$RT::Logger->debug( $res{'status'} ) if $res{'status'};
$RT::Logger->warning( $res{'stderr'} ) if $res{'stderr'};
$RT::Logger->error( $res{'logger'} ) if $res{'logger'} && $?;
# if the decryption is fine but the signature is bad, then without this
# status check we lose the decrypted text
# XXX: add argument to the function to control this check
if ( $res{'status'} !~ /DECRYPTION_OKAY/ ) {
if ( $@ || $? ) {
$res{'message'} = $@? $@: "gpg exitted with error code ". ($? >> 8);
return (undef, undef, %res);
}
}
seek $tmp_fh, 0, 0;
return ($tmp_fh, $tmp_fn, %res);
}
sub DecryptAttachment {
my %args = (
Top => undef,
Data => undef,
Passphrase => undef,
@_
);
my $gnupg = GnuPG::Interface->new();
my %opt = RT->Config->Get('GnuPGOptions');
# handling passphrase in GnuPGOptions
$args{'Passphrase'} = delete $opt{'passphrase'}
if !defined($args{'Passphrase'});
$opt{'digest-algo'} ||= 'SHA1';
$gnupg->options->hash_init(
_PrepareGnuPGOptions( %opt ),
meta_interactive => 0,
);
if ( $args{'Data'}->bodyhandle->is_encoded ) {
require RT::EmailParser;
RT::EmailParser->_DecodeBody($args{'Data'});
}
$args{'Passphrase'} = GetPassphrase()
unless defined $args{'Passphrase'};
my ($tmp_fh, $tmp_fn) = File::Temp::tempfile( UNLINK => 1 );
binmode $tmp_fh, ':raw';
$args{'Data'}->bodyhandle->print( $tmp_fh );
seek $tmp_fh, 0, 0;
my ($res_fh, $res_fn, %res) = _DecryptInlineBlock(
%args,
GnuPG => $gnupg,
BlockHandle => $tmp_fh,
);
return %res unless $res_fh;
$args{'Data'}->bodyhandle(MIME::Body::File->new($res_fn) );
$args{'Data'}->{'__store_tmp_handle_to_avoid_early_cleanup'} = $res_fh;
my $head = $args{'Data'}->head;
# we can not trust original content type
# TODO: and don't have way to detect, so we just use octet-stream
# some clients may send .asc files (encryped) as text/plain
$head->mime_attr( "Content-Type" => 'application/octet-stream' );
my $filename = $head->recommended_filename;
$filename =~ s/\.${RE_FILE_EXTENSIONS}$//i;
$head->mime_attr( $_ => $filename )
foreach (qw(Content-Type.name Content-Disposition.filename));
return %res;
}
sub DecryptContent {
my %args = (
Content => undef,
Passphrase => undef,
@_
);
my $gnupg = GnuPG::Interface->new();
my %opt = RT->Config->Get('GnuPGOptions');
# handling passphrase in GnupGOptions
$args{'Passphrase'} = delete $opt{'passphrase'}
if !defined($args{'Passphrase'});
$opt{'digest-algo'} ||= 'SHA1';
$gnupg->options->hash_init(
_PrepareGnuPGOptions( %opt ),
meta_interactive => 0,
);
$args{'Passphrase'} = GetPassphrase()
unless defined $args{'Passphrase'};
my ($tmp_fh, $tmp_fn) = File::Temp::tempfile( UNLINK => 1 );
binmode $tmp_fh, ':raw';
my ($handles, $handle_list) = _make_gpg_handles(
stdout => $tmp_fh);
my %handle = %$handle_list;
$handles->options( 'stdout' )->{'direct'} = 1;
my %res;
eval {
local $SIG{'CHLD'} = 'DEFAULT';
$gnupg->passphrase( $args{'Passphrase'} );
my $pid = safe_run_child { $gnupg->decrypt( handles => $handles ) };
{
local $SIG{'PIPE'} = 'IGNORE';
print { $handle{'stdin'} } ${ $args{'Content'} };
close $handle{'stdin'};
}
waitpid $pid, 0;
};
$res{'exit_code'} = $?;
foreach ( qw(stderr logger status) ) {
$res{$_} = do { local $/; readline $handle{$_} };
delete $res{$_} unless $res{$_} && $res{$_} =~ /\S/s;
close $handle{$_};
}
$RT::Logger->debug( $res{'status'} ) if $res{'status'};
$RT::Logger->warning( $res{'stderr'} ) if $res{'stderr'};
$RT::Logger->error( $res{'logger'} ) if $res{'logger'} && $?;
# if the decryption is fine but the signature is bad, then without this
# status check we lose the decrypted text
# XXX: add argument to the function to control this check
if ( $res{'status'} !~ /DECRYPTION_OKAY/ ) {
if ( $@ || $? ) {
$res{'message'} = $@? $@: "gpg exitted with error code ". ($? >> 8);
return %res;
}
}
${ $args{'Content'} } = '';
seek $tmp_fh, 0, 0;
while (1) {
my $status = read $tmp_fh, my $buf, 4*1024;
unless ( defined $status ) {
$RT::Logger->crit( "couldn't read message: $!" );
} elsif ( !$status ) {
last;
}
${ $args{'Content'} } .= $buf;
}
return %res;
}
=head2 GetPassphrase [ Address => undef ]
Returns passphrase, called whenever it's required with Address as a named argument.
=cut
sub GetPassphrase {
my %args = ( Address => undef, @_ );
return 'test';
}
=head2 ParseStatus
Takes a string containing output of gnupg status stream. Parses it and returns
array of hashes. Each element of array is a hash ref and represents line or
group of lines in the status message.
All hashes have Operation, Status and Message elements.
=over
=item Operation
Classification of operations gnupg performs. Now we have support
for Sign, Encrypt, Decrypt, Verify, PassphraseCheck, RecipientsCheck and Data
values.
=item Status
Informs about success. Value is 'DONE' on success, other values means that
an operation failed, for example 'ERROR', 'BAD', 'MISSING' and may be other.
=item Message
User friendly message.
=back
This parser is based on information from GnuPG distribution.
=cut
my %REASON_CODE_TO_TEXT = (
NODATA => {
1 => "No armored data",
2 => "Expected a packet, but did not found one",
3 => "Invalid packet found",
4 => "Signature expected, but not found",
},
INV_RECP => {
0 => "No specific reason given",
1 => "Not Found",
2 => "Ambigious specification",
3 => "Wrong key usage",
4 => "Key revoked",
5 => "Key expired",
6 => "No CRL known",
7 => "CRL too old",
8 => "Policy mismatch",
9 => "Not a secret key",
10 => "Key not trusted",
},
ERRSIG => {
0 => 'not specified',
4 => 'unknown algorithm',
9 => 'missing public key',
},
);
sub ReasonCodeToText {
my $keyword = shift;
my $code = shift;
return $REASON_CODE_TO_TEXT{ $keyword }{ $code }
if exists $REASON_CODE_TO_TEXT{ $keyword }{ $code };
return 'unknown';
}
my %simple_keyword = (
NO_RECP => {
Operation => 'RecipientsCheck',
Status => 'ERROR',
Message => 'No recipients',
},
UNEXPECTED => {
Operation => 'Data',
Status => 'ERROR',
Message => 'Unexpected data has been encountered',
},
BADARMOR => {
Operation => 'Data',
Status => 'ERROR',
Message => 'The ASCII armor is corrupted',
},
);
# keywords we parse
my %parse_keyword = map { $_ => 1 } qw(
USERID_HINT
SIG_CREATED GOODSIG BADSIG ERRSIG
END_ENCRYPTION
DECRYPTION_FAILED DECRYPTION_OKAY
BAD_PASSPHRASE GOOD_PASSPHRASE
NO_SECKEY NO_PUBKEY
NO_RECP INV_RECP NODATA UNEXPECTED
);
# keywords we ignore without any messages as we parse them using other
# keywords as starting point or just ignore as they are useless for us
my %ignore_keyword = map { $_ => 1 } qw(
NEED_PASSPHRASE MISSING_PASSPHRASE BEGIN_SIGNING PLAINTEXT PLAINTEXT_LENGTH
BEGIN_ENCRYPTION SIG_ID VALIDSIG
ENC_TO BEGIN_DECRYPTION END_DECRYPTION GOODMDC
TRUST_UNDEFINED TRUST_NEVER TRUST_MARGINAL TRUST_FULLY TRUST_ULTIMATE
DECRYPTION_INFO
);
sub ParseStatus {
my $status = shift;
return () unless $status;
my @status;
while ( $status =~ /\[GNUPG:\]\s*(.*?)(?=\[GNUPG:\]|\z)/igms ) {
push @status, $1; $status[-1] =~ s/\s+/ /g; $status[-1] =~ s/\s+$//;
}
$status = join "\n", @status;
study $status;
my @res;
my (%user_hint, $latest_user_main_key);
for ( my $i = 0; $i < @status; $i++ ) {
my $line = $status[$i];
my ($keyword, $args) = ($line =~ /^(\S+)\s*(.*)$/s);
if ( $simple_keyword{ $keyword } ) {
push @res, $simple_keyword{ $keyword };
$res[-1]->{'Keyword'} = $keyword;
next;
}
unless ( $parse_keyword{ $keyword } ) {
$RT::Logger->warning("Skipped $keyword") unless $ignore_keyword{ $keyword };
next;
}
if ( $keyword eq 'USERID_HINT' ) {
my %tmp = _ParseUserHint($status, $line);
$latest_user_main_key = $tmp{'MainKey'};
if ( $user_hint{ $tmp{'MainKey'} } ) {
while ( my ($k, $v) = each %tmp ) {
$user_hint{ $tmp{'MainKey'} }->{$k} = $v;
}
} else {
$user_hint{ $tmp{'MainKey'} } = \%tmp;
}
next;
}
elsif ( $keyword eq 'BAD_PASSPHRASE' || $keyword eq 'GOOD_PASSPHRASE' ) {
my $key_id = $args;
my %res = (
Operation => 'PassphraseCheck',
Status => $keyword eq 'BAD_PASSPHRASE'? 'BAD' : 'DONE',
Key => $key_id,
);
$res{'Status'} = 'MISSING' if $status[ $i - 1 ] =~ /^MISSING_PASSPHRASE/;
foreach my $line ( reverse @status[ 0 .. $i-1 ] ) {
next unless $line =~ /^NEED_PASSPHRASE\s+(\S+)\s+(\S+)\s+(\S+)/;
next if $key_id && $2 ne $key_id;
@res{'MainKey', 'Key', 'KeyType'} = ($1, $2, $3);
last;
}
$res{'Message'} = ucfirst( lc( $res{'Status'} eq 'DONE'? 'GOOD': $res{'Status'} ) ) .' passphrase';
$res{'User'} = ( $user_hint{ $res{'MainKey'} } ||= {} ) if $res{'MainKey'};
if ( exists $res{'User'}->{'EmailAddress'} ) {
$res{'Message'} .= ' for '. $res{'User'}->{'EmailAddress'};
} else {
$res{'Message'} .= " for '0x$key_id'";
}
push @res, \%res;
}
elsif ( $keyword eq 'END_ENCRYPTION' ) {
my %res = (
Operation => 'Encrypt',
Status => 'DONE',
Message => 'Data has been encrypted',
);
foreach my $line ( reverse @status[ 0 .. $i-1 ] ) {
next unless $line =~ /^BEGIN_ENCRYPTION\s+(\S+)\s+(\S+)/;
@res{'MdcMethod', 'SymAlgo'} = ($1, $2);
last;
}
push @res, \%res;
}
elsif ( $keyword eq 'DECRYPTION_FAILED' || $keyword eq 'DECRYPTION_OKAY' ) {
my %res = ( Operation => 'Decrypt' );
@res{'Status', 'Message'} =
$keyword eq 'DECRYPTION_FAILED'
? ('ERROR', 'Decryption failed')
: ('DONE', 'Decryption process succeeded');
foreach my $line ( reverse @status[ 0 .. $i-1 ] ) {
next unless $line =~ /^ENC_TO\s+(\S+)\s+(\S+)\s+(\S+)/;
my ($key, $alg, $key_length) = ($1, $2, $3);
my %encrypted_to = (
Message => "The message is encrypted to '0x$key'",
User => ( $user_hint{ $key } ||= {} ),
Key => $key,
KeyLength => $key_length,
Algorithm => $alg,
);
push @{ $res{'EncryptedTo'} ||= [] }, \%encrypted_to;
}
push @res, \%res;
}
elsif ( $keyword eq 'NO_SECKEY' || $keyword eq 'NO_PUBKEY' ) {
my ($key) = split /\s+/, $args;
my $type = $keyword eq 'NO_SECKEY'? 'secret': 'public';
my %res = (
Operation => 'KeyCheck',
Status => 'MISSING',
Message => ucfirst( $type ) ." key '0x$key' is not available",
Key => $key,
KeyType => $type,
);
$res{'User'} = ( $user_hint{ $key } ||= {} );
$res{'User'}{ ucfirst( $type ). 'KeyMissing' } = 1;
push @res, \%res;
}
# GOODSIG, BADSIG, VALIDSIG, TRUST_*
elsif ( $keyword eq 'GOODSIG' ) {
my %res = (
Operation => 'Verify',
Status => 'DONE',
Message => 'The signature is good',
);
@res{qw(Key UserString)} = split /\s+/, $args, 2;
$res{'Message'} .= ', signed by '. $res{'UserString'};
foreach my $line ( @status[ $i .. $#status ] ) {
next unless $line =~ /^TRUST_(\S+)/;
$res{'Trust'} = $1;
last;
}
$res{'Message'} .= ', trust level is '. lc( $res{'Trust'} || 'unknown');
foreach my $line ( @status[ $i .. $#status ] ) {
next unless $line =~ /^VALIDSIG\s+(.*)/;
@res{ qw(
Fingerprint
CreationDate
Timestamp
ExpireTimestamp
Version
Reserved
PubkeyAlgo
HashAlgo
Class
PKFingerprint
Other
) } = split /\s+/, $1, 10;
last;
}
push @res, \%res;
}
elsif ( $keyword eq 'BADSIG' ) {
my %res = (
Operation => 'Verify',
Status => 'BAD',
Message => 'The signature has not been verified okay',
);
@res{qw(Key UserString)} = split /\s+/, $args, 2;
push @res, \%res;
}
elsif ( $keyword eq 'ERRSIG' ) {
my %res = (
Operation => 'Verify',
Status => 'ERROR',
Message => 'Not possible to check the signature',
);
@res{qw(Key PubkeyAlgo HashAlgo Class Timestamp ReasonCode Other)}
= split /\s+/, $args, 7;
$res{'Reason'} = ReasonCodeToText( $keyword, $res{'ReasonCode'} );
$res{'Message'} .= ", the reason is ". $res{'Reason'};
push @res, \%res;
}
elsif ( $keyword eq 'SIG_CREATED' ) {
# SIG_CREATED <type> <pubkey algo> <hash algo> <class> <timestamp> <key fpr>
my @props = split /\s+/, $args;
push @res, {
Operation => 'Sign',
Status => 'DONE',
Message => "Signed message",
Type => $props[0],
PubKeyAlgo => $props[1],
HashKeyAlgo => $props[2],
Class => $props[3],
Timestamp => $props[4],
KeyFingerprint => $props[5],
User => $user_hint{ $latest_user_main_key },
};
$res[-1]->{Message} .= ' by '. $user_hint{ $latest_user_main_key }->{'EmailAddress'}
if $user_hint{ $latest_user_main_key };
}
elsif ( $keyword eq 'INV_RECP' ) {
my ($rcode, $recipient) = split /\s+/, $args, 2;
my $reason = ReasonCodeToText( $keyword, $rcode );
push @res, {
Operation => 'RecipientsCheck',
Status => 'ERROR',
Message => "Recipient '$recipient' is unusable, the reason is '$reason'",
Recipient => $recipient,
ReasonCode => $rcode,
Reason => $reason,
};
}
elsif ( $keyword eq 'NODATA' ) {
my $rcode = (split /\s+/, $args)[0];
my $reason = ReasonCodeToText( $keyword, $rcode );
push @res, {
Operation => 'Data',
Status => 'ERROR',
Message => "No data has been found. The reason is '$reason'",
ReasonCode => $rcode,
Reason => $reason,
};
}
else {
$RT::Logger->warning("Keyword $keyword is unknown");
next;
}
$res[-1]{'Keyword'} = $keyword if @res && !$res[-1]{'Keyword'};
}
return @res;
}
sub _ParseUserHint {
my ($status, $hint) = (@_);
my ($main_key_id, $user_str) = ($hint =~ /^USERID_HINT\s+(\S+)\s+(.*)$/);
return () unless $main_key_id;
return (
MainKey => $main_key_id,
String => $user_str,
EmailAddress => (map $_->address, Email::Address->parse( $user_str ))[0],
);
}
sub _PrepareGnuPGOptions {
my %opt = @_;
my %res = map { lc $_ => $opt{ $_ } } grep $supported_opt{ lc $_ }, keys %opt;
$res{'extra_args'} ||= [];
foreach my $o ( grep !$supported_opt{ lc $_ }, keys %opt ) {
push @{ $res{'extra_args'} }, '--'. lc $o;
push @{ $res{'extra_args'} }, $opt{ $o }
if defined $opt{ $o };
}
return %res;
}
{ my %key;
# no args -> clear
# one arg -> return preferred key
# many -> set
sub UseKeyForEncryption {
unless ( @_ ) {
%key = ();
} elsif ( @_ > 1 ) {
%key = (%key, @_);
$key{ lc($_) } = delete $key{ $_ } foreach grep lc ne $_, keys %key;
} else {
return $key{ $_[0] };
}
return ();
} }
=head2 UseKeyForSigning
Returns or sets identifier of the key that should be used for signing.
Returns the current value when called without arguments.
Sets new value when called with one argument and unsets if it's undef.
=cut
{ my $key;
sub UseKeyForSigning {
if ( @_ ) {
$key = $_[0];
}
return $key;
} }
=head2 GetKeysForEncryption
Takes identifier and returns keys suitable for encryption.
B<Note> that keys for which trust level is not set are
also listed.
=cut
sub GetKeysForEncryption {
my $key_id = shift;
my %res = GetKeysInfo( $key_id, 'public', @_ );
return %res if $res{'exit_code'};
return %res unless $res{'info'};
foreach my $key ( splice @{ $res{'info'} } ) {
# skip disabled keys
next if $key->{'Capabilities'} =~ /D/;
# skip keys not suitable for encryption
next unless $key->{'Capabilities'} =~ /e/i;
# skip disabled, expired, revoke and keys with no trust,
# but leave keys with unknown trust level
next if $key->{'TrustLevel'} < 0;
push @{ $res{'info'} }, $key;
}
delete $res{'info'} unless @{ $res{'info'} };
return %res;
}
sub GetKeysForSigning {
my $key_id = shift;
return GetKeysInfo( $key_id, 'private', @_ );
}
sub CheckRecipients {
my @recipients = (@_);
my ($status, @issues) = (1, ());
my %seen;
foreach my $address ( grep !$seen{ lc $_ }++, map $_->address, @recipients ) {
my %res = GetKeysForEncryption( $address );
if ( $res{'info'} && @{ $res{'info'} } == 1 && $res{'info'}[0]{'TrustLevel'} > 0 ) {
# good, one suitable and trusted key
next;
}
my $user = RT::User->new( RT->SystemUser );
$user->LoadByEmail( $address );
# it's possible that we have no User record with the email
$user = undef unless $user->id;
if ( my $fpr = UseKeyForEncryption( $address ) ) {
if ( $res{'info'} && @{ $res{'info'} } ) {
next if
grep lc $_->{'Fingerprint'} eq lc $fpr,
grep $_->{'TrustLevel'} > 0,
@{ $res{'info'} };
}
$status = 0;
my %issue = (
EmailAddress => $address,
$user? (User => $user) : (),
Keys => undef,
);
$issue{'Message'} = "Selected key either is not trusted or doesn't exist anymore."; #loc
push @issues, \%issue;
next;
}
my $prefered_key;
$prefered_key = $user->PreferredKey if $user;
#XXX: prefered key is not yet implemented...
# classify errors
$status = 0;
my %issue = (
EmailAddress => $address,
$user? (User => $user) : (),
Keys => undef,
);
unless ( $res{'info'} && @{ $res{'info'} } ) {
# no key
$issue{'Message'} = "There is no key suitable for encryption."; #loc
}
elsif ( @{ $res{'info'} } == 1 && !$res{'info'}[0]{'TrustLevel'} ) {
# trust is not set
$issue{'Message'} = "There is one suitable key, but trust level is not set."; #loc
}
else {
# multiple keys
$issue{'Message'} = "There are several keys suitable for encryption."; #loc
}
push @issues, \%issue;
}
return ($status, @issues);
}
sub GetPublicKeyInfo {
return GetKeyInfo( shift, 'public', @_ );
}
sub GetPrivateKeyInfo {
return GetKeyInfo( shift, 'private', @_ );
}
sub GetKeyInfo {
my %res = GetKeysInfo(@_);
$res{'info'} = $res{'info'}->[0];
return %res;
}
sub GetKeysInfo {
my $email = shift;
my $type = shift || 'public';
my $force = shift;
unless ( $email ) {
return (exit_code => 0) unless $force;
}
my $gnupg = GnuPG::Interface->new();
my %opt = RT->Config->Get('GnuPGOptions');
$opt{'digest-algo'} ||= 'SHA1';
$opt{'with-colons'} = undef; # parseable format
$opt{'fingerprint'} = undef; # show fingerprint
$opt{'fixed-list-mode'} = undef; # don't merge uid with keys
$gnupg->options->hash_init(
_PrepareGnuPGOptions( %opt ),
armor => 1,
meta_interactive => 0,
);
my %res;
my ($handles, $handle_list) = _make_gpg_handles();
my %handle = %$handle_list;
eval {
local $SIG{'CHLD'} = 'DEFAULT';
my $method = $type eq 'private'? 'list_secret_keys': 'list_public_keys';
my $pid = safe_run_child { $gnupg->$method( handles => $handles, $email
? (command_args => [ "--", $email])
: () ) };
close $handle{'stdin'};
waitpid $pid, 0;
};
my @info = readline $handle{'stdout'};
close $handle{'stdout'};
$res{'exit_code'} = $?;
foreach ( qw(stderr logger status) ) {
$res{$_} = do { local $/; readline $handle{$_} };
delete $res{$_} unless $res{$_} && $res{$_} =~ /\S/s;
close $handle{$_};
}
$RT::Logger->debug( $res{'status'} ) if $res{'status'};
$RT::Logger->warning( $res{'stderr'} ) if $res{'stderr'};
$RT::Logger->error( $res{'logger'} ) if $res{'logger'} && $?;
if ( $@ || $? ) {
$res{'message'} = $@? $@: "gpg exitted with error code ". ($? >> 8);
return %res;
}
@info = ParseKeysInfo( @info );
$res{'info'} = \@info;
return %res;
}
sub ParseKeysInfo {
my @lines = @_;
my %gpg_opt = RT->Config->Get('GnuPGOptions');
my @res = ();
foreach my $line( @lines ) {
chomp $line;
my $tag;
($tag, $line) = split /:/, $line, 2;
if ( $tag eq 'pub' ) {
my %info;
@info{ qw(
TrustChar KeyLength Algorithm Key
Created Expire Empty OwnerTrustChar
Empty Empty Capabilities Other
) } = split /:/, $line, 12;
# workaround gnupg's wierd behaviour, --list-keys command report calculated trust levels
# for any model except 'always', so you can change models and see changes, but not for 'always'
# we try to handle it in a simple way - we set ultimate trust for any key with trust
# level >= 0 if trust model is 'always'
my $always_trust;
$always_trust = 1 if exists $gpg_opt{'always-trust'};
$always_trust = 1 if exists $gpg_opt{'trust-model'} && $gpg_opt{'trust-model'} eq 'always';
@info{qw(Trust TrustTerse TrustLevel)} =
_ConvertTrustChar( $info{'TrustChar'} );
if ( $always_trust && $info{'TrustLevel'} >= 0 ) {
@info{qw(Trust TrustTerse TrustLevel)} =
_ConvertTrustChar( 'u' );
}
@info{qw(OwnerTrust OwnerTrustTerse OwnerTrustLevel)} =
_ConvertTrustChar( $info{'OwnerTrustChar'} );
$info{ $_ } = _ParseDate( $info{ $_ } )
foreach qw(Created Expire);
push @res, \%info;
}
elsif ( $tag eq 'sec' ) {
my %info;
@info{ qw(
Empty KeyLength Algorithm Key
Created Expire Empty OwnerTrustChar
Empty Empty Capabilities Other
) } = split /:/, $line, 12;
@info{qw(OwnerTrust OwnerTrustTerse OwnerTrustLevel)} =
_ConvertTrustChar( $info{'OwnerTrustChar'} );
$info{ $_ } = _ParseDate( $info{ $_ } )
foreach qw(Created Expire);
push @res, \%info;
}
elsif ( $tag eq 'uid' ) {
my %info;
@info{ qw(Trust Created Expire String) }
= (split /:/, $line)[0,4,5,8];
$info{ $_ } = _ParseDate( $info{ $_ } )
foreach qw(Created Expire);
push @{ $res[-1]{'User'} ||= [] }, \%info;
}
elsif ( $tag eq 'fpr' ) {
$res[-1]{'Fingerprint'} = (split /:/, $line, 10)[8];
}
}
return @res;
}
{
my %verbose = (
# deprecated
d => [
"The key has been disabled", #loc
"key disabled", #loc
"-2"
],
r => [
"The key has been revoked", #loc
"key revoked", #loc
-3,
],
e => [ "The key has expired", #loc
"key expired", #loc
'-4',
],
n => [ "Don't trust this key at all", #loc
'none', #loc
-1,
],
#gpupg docs says that '-' and 'q' may safely be treated as the same value
'-' => [
'Unknown (no trust value assigned)', #loc
'not set',
0,
],
q => [
'Unknown (no trust value assigned)', #loc
'not set',
0,
],
o => [
'Unknown (this value is new to the system)', #loc
'unknown',
0,
],
m => [
"There is marginal trust in this key", #loc
'marginal', #loc
1,
],
f => [
"The key is fully trusted", #loc
'full', #loc
2,
],
u => [
"The key is ultimately trusted", #loc
'ultimate', #loc
3,
],
);
sub _ConvertTrustChar {
my $value = shift;
return @{ $verbose{'-'} } unless $value;
$value = substr $value, 0, 1;
return @{ $verbose{ $value } || $verbose{'o'} };
}
}
sub _ParseDate {
my $value = shift;
# never
return $value unless $value;
require RT::Date;
my $obj = RT::Date->new( RT->SystemUser );
# unix time
if ( $value =~ /^\d+$/ ) {
$obj->Set( Value => $value );
} else {
$obj->Set( Format => 'unknown', Value => $value, Timezone => 'utc' );
}
return $obj;
}
sub DeleteKey {
my $key = shift;
my $gnupg = GnuPG::Interface->new();
my %opt = RT->Config->Get('GnuPGOptions');
$gnupg->options->hash_init(
_PrepareGnuPGOptions( %opt ),
meta_interactive => 0,
);
my ($handles, $handle_list) = _make_gpg_handles();
my %handle = %$handle_list;
eval {
local $SIG{'CHLD'} = 'DEFAULT';
my $pid = safe_run_child { $gnupg->wrap_call(
handles => $handles,
commands => ['--delete-secret-and-public-key'],
command_args => ["--", $key],
) };
close $handle{'stdin'};
while ( my $str = readline $handle{'status'} ) {
if ( $str =~ /^\[GNUPG:\]\s*GET_BOOL delete_key\..*/ ) {
print { $handle{'command'} } "y\n";
}
}
waitpid $pid, 0;
};
my $err = $@;
close $handle{'stdout'};
my %res;
$res{'exit_code'} = $?;
foreach ( qw(stderr logger status) ) {
$res{$_} = do { local $/; readline $handle{$_} };
delete $res{$_} unless $res{$_} && $res{$_} =~ /\S/s;
close $handle{$_};
}
$RT::Logger->debug( $res{'status'} ) if $res{'status'};
$RT::Logger->warning( $res{'stderr'} ) if $res{'stderr'};
$RT::Logger->error( $res{'logger'} ) if $res{'logger'} && $?;
if ( $err || $res{'exit_code'} ) {
$res{'message'} = $err? $err : "gpg exitted with error code ". ($res{'exit_code'} >> 8);
}
return %res;
}
sub ImportKey {
my $key = shift;
my $gnupg = GnuPG::Interface->new();
my %opt = RT->Config->Get('GnuPGOptions');
$gnupg->options->hash_init(
_PrepareGnuPGOptions( %opt ),
meta_interactive => 0,
);
my ($handles, $handle_list) = _make_gpg_handles();
my %handle = %$handle_list;
eval {
local $SIG{'CHLD'} = 'DEFAULT';
my $pid = safe_run_child { $gnupg->wrap_call(
handles => $handles,
commands => ['--import'],
) };
print { $handle{'stdin'} } $key;
close $handle{'stdin'};
waitpid $pid, 0;
};
my $err = $@;
close $handle{'stdout'};
my %res;
$res{'exit_code'} = $?;
foreach ( qw(stderr logger status) ) {
$res{$_} = do { local $/; readline $handle{$_} };
delete $res{$_} unless $res{$_} && $res{$_} =~ /\S/s;
close $handle{$_};
}
$RT::Logger->debug( $res{'status'} ) if $res{'status'};
$RT::Logger->warning( $res{'stderr'} ) if $res{'stderr'};
$RT::Logger->error( $res{'logger'} ) if $res{'logger'} && $?;
if ( $err || $res{'exit_code'} ) {
$res{'message'} = $err? $err : "gpg exitted with error code ". ($res{'exit_code'} >> 8);
}
return %res;
}
=head2 KEY
Signs a small message with the key, to make sure the key exists and
we have a useable passphrase. The first argument MUST be a key identifier
of the signer: either email address, key id or finger print.
Returns a true value if all went well.
=cut
sub DrySign {
my $from = shift;
my $mime = MIME::Entity->build(
Type => "text/plain",
From => 'nobody@localhost',
To => 'nobody@localhost',
Subject => "dry sign",
Data => ['t'],
);
my %res = SignEncrypt(
Sign => 1,
Encrypt => 0,
Entity => $mime,
Signer => $from,
);
return $res{exit_code} == 0;
}
1;
=head2 Probe
This routine returns true if RT's GnuPG support is configured and working
properly (and false otherwise).
=cut
sub Probe {
my $gnupg = GnuPG::Interface->new();
my %opt = RT->Config->Get('GnuPGOptions');
$gnupg->options->hash_init(
_PrepareGnuPGOptions( %opt ),
armor => 1,
meta_interactive => 0,
);
my ($handles, $handle_list) = _make_gpg_handles();
my %handle = %$handle_list;
local $@;
eval {
local $SIG{'CHLD'} = 'DEFAULT';
my $pid = safe_run_child { $gnupg->wrap_call( commands => ['--version' ], handles => $handles ) };
close $handle{'stdin'};
waitpid $pid, 0;
};
if ( $@ ) {
$RT::Logger->debug(
"Probe for GPG failed."
." Couldn't run `gpg --version`: ". $@
);
return 0;
}
# on some systems gpg exits with code 2, but still 100% functional,
# it's general error system error or incorrect command, command is correct,
# but there is no way to get actuall error
if ( $? && ($? >> 8) != 2 ) {
my $msg = "Probe for GPG failed."
." Process exitted with code ". ($? >> 8)
. ($? & 127 ? (" as recieved signal ". ($? & 127)) : '')
. ".";
foreach ( qw(stderr logger status) ) {
my $tmp = do { local $/; readline $handle{$_} };
next unless $tmp && $tmp =~ /\S/s;
close $handle{$_};
$msg .= "\n$_:\n$tmp\n";
}
$RT::Logger->debug( $msg );
return 0;
}
return 1;
}
sub _make_gpg_handles {
my %handle_map = (@_);
$handle_map{$_} = IO::Handle->new
foreach grep !defined $handle_map{$_},
qw(stdin stdout stderr logger status command);
my $handles = GnuPG::Handles->new(%handle_map);
return ($handles, \%handle_map);
}
RT::Base->_ImportOverlays();
# helper package to avoid using temp file
package IO::Handle::CRLF;
use base qw(IO::Handle);
sub print {
my ($self, @args) = (@_);
s/\r*\n/\x0D\x0A/g foreach @args;
return $self->SUPER::print( @args );
}
1;
|