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
|
/**
* @file certificate.c Public-Key Certificate API
* @ingroup core
*/
/*
*
* purple
*
* Purple is the legal property of its developers, whose names are too numerous
* to list here. Please refer to the COPYRIGHT file distributed with this
* source distribution.
*
* 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 02111-1301 USA
*/
#include "internal.h"
#include "certificate.h"
#include "dbus-maybe.h"
#include "debug.h"
#include "request.h"
#include "signals.h"
#include "util.h"
/** List holding pointers to all registered certificate schemes */
static GList *cert_schemes = NULL;
/** List of registered Verifiers */
static GList *cert_verifiers = NULL;
/** List of registered Pools */
static GList *cert_pools = NULL;
static const gchar *
invalidity_reason_to_string(PurpleCertificateInvalidityFlags flag)
{
switch (flag) {
case PURPLE_CERTIFICATE_SELF_SIGNED:
return _("The certificate is self-signed and cannot be "
"automatically checked.");
break;
case PURPLE_CERTIFICATE_CA_UNKNOWN:
return _("The certificate is not trusted because no certificate "
"that can verify it is currently trusted.");
break;
case PURPLE_CERTIFICATE_NOT_ACTIVATED:
return _("The certificate is not valid yet. Check that your "
"computer's date and time are accurate.");
break;
case PURPLE_CERTIFICATE_EXPIRED:
return _("The certificate has expired and should not be "
"considered valid. Check that your computer's date "
"and time are accurate.");
break;
case PURPLE_CERTIFICATE_NAME_MISMATCH:
/* Translators: "domain" refers to a DNS domain (e.g. talk.google.com) */
return _("The certificate presented is not issued to this domain.");
break;
case PURPLE_CERTIFICATE_NO_CA_POOL:
return _("You have no database of root certificates, so "
"this certificate cannot be validated.");
break;
case PURPLE_CERTIFICATE_INVALID_CHAIN:
return _("The certificate chain presented is invalid.");
break;
case PURPLE_CERTIFICATE_REVOKED:
return _("The certificate has been revoked.");
break;
case PURPLE_CERTIFICATE_UNKNOWN_ERROR:
default:
return _("An unknown certificate error occurred.");
break;
}
}
void
purple_certificate_verify (PurpleCertificateVerifier *verifier,
const gchar *subject_name, GList *cert_chain,
PurpleCertificateVerifiedCallback cb,
gpointer cb_data)
{
PurpleCertificateVerificationRequest *vrq;
PurpleCertificateScheme *scheme;
g_return_if_fail(subject_name != NULL);
/* If you don't have a cert to check, why are you requesting that it
be verified? */
g_return_if_fail(cert_chain != NULL);
g_return_if_fail(cb != NULL);
/* Look up the CertificateScheme */
scheme = purple_certificate_find_scheme(verifier->scheme_name);
g_return_if_fail(scheme);
/* Check that at least the first cert in the chain matches the
Verifier scheme */
g_return_if_fail(scheme ==
((PurpleCertificate *) (cert_chain->data))->scheme);
/* Construct and fill in the request fields */
vrq = g_new0(PurpleCertificateVerificationRequest, 1);
vrq->verifier = verifier;
vrq->scheme = scheme;
vrq->subject_name = g_strdup(subject_name);
vrq->cert_chain = purple_certificate_copy_list(cert_chain);
vrq->cb = cb;
vrq->cb_data = cb_data;
/* Initiate verification */
(verifier->start_verification)(vrq);
}
void
purple_certificate_verify_complete(PurpleCertificateVerificationRequest *vrq,
PurpleCertificateVerificationStatus st)
{
PurpleCertificateVerifier *vr;
g_return_if_fail(vrq);
if (st == PURPLE_CERTIFICATE_VALID) {
purple_debug_info("certificate",
"Successfully verified certificate for %s\n",
vrq->subject_name);
} else {
purple_debug_error("certificate",
"Failed to verify certificate for %s\n",
vrq->subject_name);
}
/* Pass the results on to the request's callback */
(vrq->cb)(st, vrq->cb_data);
/* And now to eliminate the request */
/* Fetch the Verifier responsible... */
vr = vrq->verifier;
/* ...and order it to KILL */
(vr->destroy_request)(vrq);
/* Now the internals have been cleaned up, so clean up the libpurple-
created elements */
g_free(vrq->subject_name);
purple_certificate_destroy_list(vrq->cert_chain);
/* A structure born
* to much ado
* and with so much within.
* It reaches now
* its quiet end. */
g_free(vrq);
}
PurpleCertificate *
purple_certificate_copy(PurpleCertificate *crt)
{
g_return_val_if_fail(crt, NULL);
g_return_val_if_fail(crt->scheme, NULL);
g_return_val_if_fail(crt->scheme->copy_certificate, NULL);
return (crt->scheme->copy_certificate)(crt);
}
GList *
purple_certificate_copy_list(GList *crt_list)
{
GList *new_l, *l;
/* First, make a shallow copy of the list */
new_l = g_list_copy(crt_list);
/* Now go through and actually duplicate each certificate */
for (l = new_l; l; l = l->next) {
l->data = purple_certificate_copy(l->data);
}
return new_l;
}
void
purple_certificate_destroy (PurpleCertificate *crt)
{
PurpleCertificateScheme *scheme;
if (NULL == crt) return;
scheme = crt->scheme;
(scheme->destroy_certificate)(crt);
}
void
purple_certificate_destroy_list (GList * crt_list)
{
PurpleCertificate *crt;
GList *l;
for (l=crt_list; l; l = l->next) {
crt = (PurpleCertificate *) l->data;
purple_certificate_destroy(crt);
}
g_list_free(crt_list);
}
gboolean
purple_certificate_signed_by(PurpleCertificate *crt, PurpleCertificate *issuer)
{
PurpleCertificateScheme *scheme;
g_return_val_if_fail(crt, FALSE);
g_return_val_if_fail(issuer, FALSE);
scheme = crt->scheme;
g_return_val_if_fail(scheme, FALSE);
/* We can't compare two certs of unrelated schemes, obviously */
g_return_val_if_fail(issuer->scheme == scheme, FALSE);
return (scheme->signed_by)(crt, issuer);
}
gboolean
purple_certificate_check_signature_chain_with_failing(GList *chain,
PurpleCertificate **failing)
{
GList *cur;
PurpleCertificate *crt, *issuer;
gchar *uid;
time_t now, activation, expiration;
gboolean ret;
g_return_val_if_fail(chain, FALSE);
if (failing)
*failing = NULL;
uid = purple_certificate_get_unique_id((PurpleCertificate *) chain->data);
purple_debug_info("certificate",
"Checking signature chain for uid=%s\n",
uid);
g_free(uid);
/* If this is a single-certificate chain, say that it is valid */
if (chain->next == NULL) {
purple_debug_info("certificate",
"...Singleton. We'll say it's valid.\n");
return TRUE;
}
now = time(NULL);
/* Load crt with the first certificate */
crt = (PurpleCertificate *)(chain->data);
/* And start with the second certificate in the chain */
for ( cur = chain->next; cur; cur = cur->next ) {
issuer = (PurpleCertificate *)(cur->data);
uid = purple_certificate_get_unique_id(issuer);
ret = purple_certificate_get_times(issuer, &activation, &expiration);
if (!ret || now < activation || now > expiration) {
if (!ret)
purple_debug_error("certificate",
"...Failed to get validity times for certificate %s\n"
"Chain is INVALID\n", uid);
else if (now > expiration)
purple_debug_error("certificate",
"...Issuer %s expired at %s\nChain is INVALID\n",
uid, ctime(&expiration));
else
purple_debug_error("certificate",
"...Not-yet-activated issuer %s will be valid at %s\n"
"Chain is INVALID\n", uid, ctime(&activation));
if (failing)
*failing = crt;
g_free(uid);
return FALSE;
}
/* Check the signature for this link */
if (! purple_certificate_signed_by(crt, issuer) ) {
purple_debug_error("certificate",
"...Bad or missing signature by %s\nChain is INVALID\n",
uid);
g_free(uid);
if (failing)
*failing = crt;
return FALSE;
}
purple_debug_info("certificate",
"...Good signature by %s\n",
uid);
g_free(uid);
/* The issuer is now the next crt whose signature is to be
checked */
crt = issuer;
}
/* If control reaches this point, the chain is valid */
purple_debug_info("certificate", "Chain is VALID\n");
return TRUE;
}
gboolean
purple_certificate_check_signature_chain(GList *chain)
{
return purple_certificate_check_signature_chain_with_failing(chain, NULL);
}
PurpleCertificate *
purple_certificate_import(PurpleCertificateScheme *scheme, const gchar *filename)
{
g_return_val_if_fail(scheme, NULL);
g_return_val_if_fail(scheme->import_certificate, NULL);
g_return_val_if_fail(filename, NULL);
return (scheme->import_certificate)(filename);
}
GSList *
purple_certificates_import(PurpleCertificateScheme *scheme, const gchar *filename)
{
g_return_val_if_fail(scheme, NULL);
g_return_val_if_fail(scheme->import_certificates, NULL);
g_return_val_if_fail(filename, NULL);
return (scheme->import_certificates)(filename);
}
gboolean
purple_certificate_export(const gchar *filename, PurpleCertificate *crt)
{
PurpleCertificateScheme *scheme;
g_return_val_if_fail(filename, FALSE);
g_return_val_if_fail(crt, FALSE);
g_return_val_if_fail(crt->scheme, FALSE);
scheme = crt->scheme;
g_return_val_if_fail(scheme->export_certificate, FALSE);
return (scheme->export_certificate)(filename, crt);
}
static gboolean
byte_arrays_equal(const GByteArray *array1, const GByteArray *array2)
{
g_return_val_if_fail(array1 != NULL, FALSE);
g_return_val_if_fail(array2 != NULL, FALSE);
return (array1->len == array2->len) &&
(0 == memcmp(array1->data, array2->data, array1->len));
}
GByteArray *
purple_certificate_get_fingerprint_sha1(PurpleCertificate *crt)
{
PurpleCertificateScheme *scheme;
GByteArray *fpr;
g_return_val_if_fail(crt, NULL);
g_return_val_if_fail(crt->scheme, NULL);
scheme = crt->scheme;
g_return_val_if_fail(scheme->get_fingerprint_sha1, NULL);
fpr = (scheme->get_fingerprint_sha1)(crt);
return fpr;
}
gchar *
purple_certificate_get_unique_id(PurpleCertificate *crt)
{
g_return_val_if_fail(crt, NULL);
g_return_val_if_fail(crt->scheme, NULL);
g_return_val_if_fail(crt->scheme->get_unique_id, NULL);
return (crt->scheme->get_unique_id)(crt);
}
gchar *
purple_certificate_get_issuer_unique_id(PurpleCertificate *crt)
{
g_return_val_if_fail(crt, NULL);
g_return_val_if_fail(crt->scheme, NULL);
g_return_val_if_fail(crt->scheme->get_issuer_unique_id, NULL);
return (crt->scheme->get_issuer_unique_id)(crt);
}
gchar *
purple_certificate_get_subject_name(PurpleCertificate *crt)
{
PurpleCertificateScheme *scheme;
gchar *subject_name;
g_return_val_if_fail(crt, NULL);
g_return_val_if_fail(crt->scheme, NULL);
scheme = crt->scheme;
g_return_val_if_fail(scheme->get_subject_name, NULL);
subject_name = (scheme->get_subject_name)(crt);
return subject_name;
}
gboolean
purple_certificate_check_subject_name(PurpleCertificate *crt, const gchar *name)
{
PurpleCertificateScheme *scheme;
g_return_val_if_fail(crt, FALSE);
g_return_val_if_fail(crt->scheme, FALSE);
g_return_val_if_fail(name, FALSE);
scheme = crt->scheme;
g_return_val_if_fail(scheme->check_subject_name, FALSE);
return (scheme->check_subject_name)(crt, name);
}
gboolean
purple_certificate_get_times(PurpleCertificate *crt, time_t *activation, time_t *expiration)
{
PurpleCertificateScheme *scheme;
g_return_val_if_fail(crt, FALSE);
scheme = crt->scheme;
g_return_val_if_fail(scheme, FALSE);
/* If both provided references are NULL, what are you doing calling
this? */
g_return_val_if_fail( (activation != NULL) || (expiration != NULL), FALSE);
/* Throw the request on down to the certscheme */
return (scheme->get_times)(crt, activation, expiration);
}
gchar *
purple_certificate_pool_mkpath(PurpleCertificatePool *pool, const gchar *id)
{
gchar *path;
gchar *esc_scheme_name, *esc_name, *esc_id;
g_return_val_if_fail(pool, NULL);
g_return_val_if_fail(pool->scheme_name, NULL);
g_return_val_if_fail(pool->name, NULL);
/* Escape all the elements for filesystem-friendliness */
esc_scheme_name = g_strdup(purple_escape_filename(pool->scheme_name));
esc_name = g_strdup(purple_escape_filename(pool->name));
esc_id = id ? g_strdup(purple_escape_filename(id)) : NULL;
path = g_build_filename(purple_user_dir(),
"certificates", /* TODO: constantize this? */
esc_scheme_name,
esc_name,
esc_id,
NULL);
g_free(esc_scheme_name);
g_free(esc_name);
g_free(esc_id);
return path;
}
gboolean
purple_certificate_pool_usable(PurpleCertificatePool *pool)
{
g_return_val_if_fail(pool, FALSE);
g_return_val_if_fail(pool->scheme_name, FALSE);
/* Check that the pool's scheme is loaded */
if (purple_certificate_find_scheme(pool->scheme_name) == NULL) {
return FALSE;
}
return TRUE;
}
PurpleCertificateScheme *
purple_certificate_pool_get_scheme(PurpleCertificatePool *pool)
{
g_return_val_if_fail(pool, NULL);
g_return_val_if_fail(pool->scheme_name, NULL);
return purple_certificate_find_scheme(pool->scheme_name);
}
gboolean
purple_certificate_pool_contains(PurpleCertificatePool *pool, const gchar *id)
{
g_return_val_if_fail(pool, FALSE);
g_return_val_if_fail(id, FALSE);
g_return_val_if_fail(pool->cert_in_pool, FALSE);
return (pool->cert_in_pool)(id);
}
PurpleCertificate *
purple_certificate_pool_retrieve(PurpleCertificatePool *pool, const gchar *id)
{
g_return_val_if_fail(pool, NULL);
g_return_val_if_fail(id, NULL);
g_return_val_if_fail(pool->get_cert, NULL);
return (pool->get_cert)(id);
}
gboolean
purple_certificate_pool_store(PurpleCertificatePool *pool, const gchar *id, PurpleCertificate *crt)
{
gboolean ret = FALSE;
g_return_val_if_fail(pool, FALSE);
g_return_val_if_fail(id, FALSE);
g_return_val_if_fail(pool->put_cert, FALSE);
/* Whether crt->scheme matches find_scheme(pool->scheme_name) is not
relevant... I think... */
g_return_val_if_fail(
g_ascii_strcasecmp(pool->scheme_name, crt->scheme->name) == 0,
FALSE);
ret = (pool->put_cert)(id, crt);
/* Signal that the certificate was stored if success*/
if (ret) {
purple_signal_emit(pool, "certificate-stored",
pool, id);
}
return ret;
}
gboolean
purple_certificate_pool_delete(PurpleCertificatePool *pool, const gchar *id)
{
gboolean ret = FALSE;
g_return_val_if_fail(pool, FALSE);
g_return_val_if_fail(id, FALSE);
g_return_val_if_fail(pool->delete_cert, FALSE);
ret = (pool->delete_cert)(id);
/* Signal that the certificate was deleted if success */
if (ret) {
purple_signal_emit(pool, "certificate-deleted",
pool, id);
}
return ret;
}
GList *
purple_certificate_pool_get_idlist(PurpleCertificatePool *pool)
{
g_return_val_if_fail(pool, NULL);
g_return_val_if_fail(pool->get_idlist, NULL);
return (pool->get_idlist)();
}
void
purple_certificate_pool_destroy_idlist(GList *idlist)
{
GList *l;
/* Iterate through and free them strings */
for ( l = idlist; l; l = l->next ) {
g_free(l->data);
}
g_list_free(idlist);
}
/****************************************************************************/
/* Builtin Verifiers, Pools, etc. */
/****************************************************************************/
static void
x509_singleuse_verify_cb (PurpleCertificateVerificationRequest *vrq, gint id)
{
g_return_if_fail(vrq);
purple_debug_info("certificate/x509_singleuse",
"VRQ on cert from %s gave %d\n",
vrq->subject_name, id);
/* Signal what happened back to the caller */
if (1 == id) {
/* Accepted! */
purple_certificate_verify_complete(vrq,
PURPLE_CERTIFICATE_VALID);
} else {
/* Not accepted */
purple_certificate_verify_complete(vrq,
PURPLE_CERTIFICATE_INVALID);
}
}
static void
x509_singleuse_start_verify (PurpleCertificateVerificationRequest *vrq)
{
gchar *sha_asc;
GByteArray *sha_bin;
gchar *cn;
const gchar *cn_match;
gchar *primary, *secondary;
PurpleCertificate *crt = (PurpleCertificate *) vrq->cert_chain->data;
/* Pull out the SHA1 checksum */
sha_bin = purple_certificate_get_fingerprint_sha1(crt);
/* Now decode it for display */
sha_asc = purple_base16_encode_chunked(sha_bin->data,
sha_bin->len);
/* Get the cert Common Name */
cn = purple_certificate_get_subject_name(crt);
/* Determine whether the name matches */
if (purple_certificate_check_subject_name(crt, vrq->subject_name)) {
cn_match = "";
} else {
cn_match = _("(DOES NOT MATCH)");
}
/* Make messages */
primary = g_strdup_printf(_("%s has presented the following certificate for just-this-once use:"), vrq->subject_name);
secondary = g_strdup_printf(_("Common name: %s %s\nFingerprint (SHA1): %s"), cn, cn_match, sha_asc);
/* Make a semi-pretty display */
purple_request_accept_cancel(
vrq->cb_data, /* TODO: Find what the handle ought to be */
_("Single-use Certificate Verification"),
primary,
secondary,
0, /* Accept by default */
NULL, /* No account */
NULL, /* No other user */
NULL, /* No associated conversation */
vrq,
x509_singleuse_verify_cb,
x509_singleuse_verify_cb );
/* Cleanup */
g_free(cn);
g_free(primary);
g_free(secondary);
g_free(sha_asc);
g_byte_array_free(sha_bin, TRUE);
}
static void
x509_singleuse_destroy_request (PurpleCertificateVerificationRequest *vrq)
{
/* I don't do anything! */
}
static PurpleCertificateVerifier x509_singleuse = {
"x509", /* Scheme name */
"singleuse", /* Verifier name */
x509_singleuse_start_verify, /* start_verification function */
x509_singleuse_destroy_request, /* Request cleanup operation */
NULL,
NULL,
NULL,
NULL
};
/***** X.509 Certificate Authority pool, keyed by Distinguished Name *****/
/* This is implemented in what may be the most inefficient and bugprone way
possible; however, future optimizations should not be difficult. */
static PurpleCertificatePool x509_ca;
/** Holds a key-value pair for quickish certificate lookup */
typedef struct {
gchar *dn;
PurpleCertificate *crt;
} x509_ca_element;
static void
x509_ca_element_free(x509_ca_element *el)
{
if (NULL == el) return;
g_free(el->dn);
purple_certificate_destroy(el->crt);
g_free(el);
}
/** System directory to probe for CA certificates */
/* This is set in the lazy_init function */
static GList *x509_ca_paths = NULL;
/** A list of loaded CAs, populated from the above path whenever the lazy_init
happens. Contains pointers to x509_ca_elements */
static GList *x509_ca_certs = NULL;
/** Used for lazy initialization purposes. */
static gboolean x509_ca_initialized = FALSE;
/** Adds a certificate to the in-memory cache, and mark it as trusted */
static gboolean
x509_ca_quiet_put_cert(PurpleCertificate *crt)
{
gboolean ret;
x509_ca_element *el;
/* lazy_init calls this function, so calling lazy_init here is a
Bad Thing */
g_return_val_if_fail(crt, FALSE);
g_return_val_if_fail(crt->scheme, FALSE);
/* Make sure that this is some kind of X.509 certificate */
/* TODO: Perhaps just check crt->scheme->name instead? */
g_return_val_if_fail(crt->scheme == purple_certificate_find_scheme(x509_ca.scheme_name), FALSE);
ret = TRUE;
if (crt->scheme->register_trusted_tls_cert) {
ret = (crt->scheme->register_trusted_tls_cert)(crt, TRUE);
}
if (ret) {
el = g_new0(x509_ca_element, 1);
el->dn = purple_certificate_get_unique_id(crt);
el->crt = purple_certificate_copy(crt);
x509_ca_certs = g_list_prepend(x509_ca_certs, el);
}
return ret;
}
/* Since the libpurple CertificatePools get registered before plugins are
loaded, an X.509 Scheme is generally not available when x509_ca_init is
called, but x509_ca requires X.509 operations in order to properly load.
To solve this, I present the lazy_init function. It attempts to finish
initialization of the Pool, but it usually fails when it is called from
x509_ca_init. However, this is OK; initialization is then simply deferred
until someone tries to use functions from the pool. */
static gboolean
x509_ca_lazy_init(void)
{
PurpleCertificateScheme *x509;
GDir *certdir;
const gchar *entry;
GPatternSpec *pempat, *crtpat;
GList *iter = NULL;
GSList *crts = NULL;
if (x509_ca_initialized) return TRUE;
/* Check that X.509 is registered */
x509 = purple_certificate_find_scheme(x509_ca.scheme_name);
if ( !x509 ) {
purple_debug_warning("certificate/x509/ca",
"Lazy init failed because an X.509 Scheme "
"is not yet registered. Maybe it will be "
"better later.\n");
return FALSE;
}
/* Use a glob to only read .pem files */
pempat = g_pattern_spec_new("*.pem");
crtpat = g_pattern_spec_new("*.crt");
/* Populate the certificates pool from the search path(s) */
for (iter = x509_ca_paths; iter; iter = iter->next) {
certdir = g_dir_open(iter->data, 0, NULL);
if (!certdir) {
purple_debug_error("certificate/x509/ca", "Couldn't open location '%s'\n", (const char *)iter->data);
continue;
}
while ( (entry = g_dir_read_name(certdir)) ) {
gchar *fullpath;
PurpleCertificate *crt;
if (!g_pattern_match_string(pempat, entry) && !g_pattern_match_string(crtpat, entry)) {
continue;
}
fullpath = g_build_filename(iter->data, entry, NULL);
/* TODO: Respond to a failure in the following? */
crts = purple_certificates_import(x509, fullpath);
while (crts && crts->data) {
crt = crts->data;
if (x509_ca_quiet_put_cert(crt)) {
gchar *name;
name = purple_certificate_get_subject_name(crt);
purple_debug_info("certificate/x509/ca",
"Loaded %s from %s\n",
name ? name : "(unknown)", fullpath);
g_free(name);
} else {
purple_debug_error("certificate/x509/ca",
"Failed to load certificate from %s\n",
fullpath);
}
purple_certificate_destroy(crt);
crts = g_slist_delete_link(crts, crts);
}
g_free(fullpath);
}
g_dir_close(certdir);
}
g_pattern_spec_free(pempat);
g_pattern_spec_free(crtpat);
purple_debug_info("certificate/x509/ca",
"Lazy init completed.\n");
x509_ca_initialized = TRUE;
return TRUE;
}
static gboolean
x509_ca_init(void)
{
/* Attempt to point at the appropriate system path */
if (NULL == x509_ca_paths) {
#ifdef _WIN32
x509_ca_paths = g_list_append(NULL, g_build_filename(DATADIR,
"ca-certs", NULL));
#else
# ifdef SSL_CERTIFICATES_DIR
x509_ca_paths = g_list_append(NULL, g_strdup(SSL_CERTIFICATES_DIR));
# endif
x509_ca_paths = g_list_append(x509_ca_paths,
g_build_filename(DATADIR, "purple", "ca-certs", NULL));
#endif
}
/* Attempt to initialize now, but if it doesn't work, that's OK;
it will get done later */
if ( ! x509_ca_lazy_init()) {
purple_debug_info("certificate/x509/ca",
"Init failed, probably because a "
"dependency is not yet registered. "
"It has been deferred to later.\n");
}
return TRUE;
}
static void
x509_ca_uninit(void)
{
GList *l;
for (l = x509_ca_certs; l; l = l->next) {
x509_ca_element *el = l->data;
x509_ca_element_free(el);
}
g_list_free(x509_ca_certs);
x509_ca_certs = NULL;
x509_ca_initialized = FALSE;
/** TODO: the cert store in the SSL implementation wouldn't be cleared by this */
g_list_foreach(x509_ca_paths, (GFunc)g_free, NULL);
g_list_free(x509_ca_paths);
x509_ca_paths = NULL;
}
/** Look up a ca_element by dn */
static x509_ca_element *
x509_ca_locate_cert(GList *lst, const gchar *dn)
{
GList *cur;
for (cur = lst; cur; cur = cur->next) {
x509_ca_element *el = cur->data;
if (purple_strequal(dn, el->dn)) {
return el;
}
}
return NULL;
}
static GSList *
x509_ca_locate_certs(GList *lst, const gchar *dn)
{
GList *cur;
GSList *crts = NULL;
for (cur = lst; cur; cur = cur->next) {
x509_ca_element *el = cur->data;
if (purple_strequal(dn, el->dn)) {
crts = g_slist_prepend(crts, el);
}
}
return crts;
}
static gboolean
x509_ca_cert_in_pool(const gchar *id)
{
g_return_val_if_fail(x509_ca_lazy_init(), FALSE);
g_return_val_if_fail(id, FALSE);
if (x509_ca_locate_cert(x509_ca_certs, id) != NULL) {
return TRUE;
} else {
return FALSE;
}
return FALSE;
}
static PurpleCertificate *
x509_ca_get_cert(const gchar *id)
{
PurpleCertificate *crt = NULL;
x509_ca_element *el;
g_return_val_if_fail(x509_ca_lazy_init(), NULL);
g_return_val_if_fail(id, NULL);
/* Search the memory-cached pool */
el = x509_ca_locate_cert(x509_ca_certs, id);
if (el != NULL) {
/* Make a copy of the memcached one for the function caller
to play with */
crt = purple_certificate_copy(el->crt);
} else {
crt = NULL;
}
return crt;
}
static GSList *
x509_ca_get_certs(const gchar *id)
{
GSList *crts = NULL, *els = NULL;
g_return_val_if_fail(x509_ca_lazy_init(), NULL);
g_return_val_if_fail(id, NULL);
/* Search the memory-cached pool */
els = x509_ca_locate_certs(x509_ca_certs, id);
if (els != NULL) {
GSList *cur;
/* Make a copy of the memcached ones for the function caller
to play with */
for (cur = els; cur; cur = cur->next) {
x509_ca_element *el = cur->data;
crts = g_slist_prepend(crts, purple_certificate_copy(el->crt));
}
g_slist_free(els);
}
return crts;
}
static gboolean
x509_ca_put_cert(const gchar *id, PurpleCertificate *crt)
{
gboolean ret = FALSE;
g_return_val_if_fail(x509_ca_lazy_init(), FALSE);
/* TODO: This is a quick way of doing this. At some point the change
ought to be flushed to disk somehow. */
ret = x509_ca_quiet_put_cert(crt);
return ret;
}
static gboolean
x509_ca_delete_cert(const gchar *id)
{
x509_ca_element *el;
g_return_val_if_fail(x509_ca_lazy_init(), FALSE);
g_return_val_if_fail(id, FALSE);
/* Is the id even in the pool? */
el = x509_ca_locate_cert(x509_ca_certs, id);
if ( el == NULL ) {
purple_debug_warning("certificate/x509/ca",
"Id %s wasn't in the pool\n",
id);
return FALSE;
}
/* Unlink it from the memory cache and destroy it */
x509_ca_certs = g_list_remove(x509_ca_certs, el);
x509_ca_element_free(el);
return TRUE;
}
static GList *
x509_ca_get_idlist(void)
{
GList *l, *idlist;
g_return_val_if_fail(x509_ca_lazy_init(), NULL);
idlist = NULL;
for (l = x509_ca_certs; l; l = l->next) {
x509_ca_element *el = l->data;
idlist = g_list_prepend(idlist, g_strdup(el->dn));
}
return idlist;
}
static PurpleCertificatePool x509_ca = {
"x509", /* Scheme name */
"ca", /* Pool name */
N_("Certificate Authorities"),/* User-friendly name */
NULL, /* Internal data */
x509_ca_init, /* init */
x509_ca_uninit, /* uninit */
x509_ca_cert_in_pool, /* Certificate exists? */
x509_ca_get_cert, /* Cert retriever */
x509_ca_put_cert, /* Cert writer */
x509_ca_delete_cert, /* Cert remover */
x509_ca_get_idlist, /* idlist retriever */
NULL,
NULL,
NULL,
NULL
};
/***** Cache of certificates given by TLS/SSL peers *****/
static PurpleCertificatePool x509_tls_peers;
static gboolean
x509_tls_peers_init(void)
{
gchar *poolpath;
int ret;
/* Set up key cache here if it isn't already done */
poolpath = purple_certificate_pool_mkpath(&x509_tls_peers, NULL);
ret = purple_build_dir(poolpath, 0700); /* Make it this user only */
if (ret != 0)
purple_debug_info("certificate/tls_peers",
"Could not create %s. Certificates will not be cached.\n",
poolpath);
g_free(poolpath);
return TRUE;
}
static gboolean
x509_tls_peers_cert_in_pool(const gchar *id)
{
gchar *keypath;
gboolean ret = FALSE;
g_return_val_if_fail(id, FALSE);
keypath = purple_certificate_pool_mkpath(&x509_tls_peers, id);
ret = g_file_test(keypath, G_FILE_TEST_IS_REGULAR);
g_free(keypath);
return ret;
}
static PurpleCertificate *
x509_tls_peers_get_cert(const gchar *id)
{
PurpleCertificateScheme *x509;
PurpleCertificate *crt;
gchar *keypath;
g_return_val_if_fail(id, NULL);
/* Is it in the pool? */
if ( !x509_tls_peers_cert_in_pool(id) ) {
return NULL;
}
/* Look up the X.509 scheme */
x509 = purple_certificate_find_scheme("x509");
g_return_val_if_fail(x509, NULL);
/* Okay, now find and load that key */
keypath = purple_certificate_pool_mkpath(&x509_tls_peers, id);
crt = purple_certificate_import(x509, keypath);
g_free(keypath);
return crt;
}
static gboolean
x509_tls_peers_put_cert(const gchar *id, PurpleCertificate *crt)
{
gboolean ret = FALSE;
gchar *keypath;
g_return_val_if_fail(crt, FALSE);
g_return_val_if_fail(crt->scheme, FALSE);
/* Make sure that this is some kind of X.509 certificate */
/* TODO: Perhaps just check crt->scheme->name instead? */
g_return_val_if_fail(crt->scheme == purple_certificate_find_scheme(x509_tls_peers.scheme_name), FALSE);
/* Work out the filename and export */
keypath = purple_certificate_pool_mkpath(&x509_tls_peers, id);
ret = purple_certificate_export(keypath, crt);
if (crt->scheme->register_trusted_tls_cert) {
ret = (crt->scheme->register_trusted_tls_cert)(crt, FALSE);
}
g_free(keypath);
return ret;
}
static gboolean
x509_tls_peers_delete_cert(const gchar *id)
{
gboolean ret = FALSE;
gchar *keypath;
g_return_val_if_fail(id, FALSE);
/* Is the id even in the pool? */
if (!x509_tls_peers_cert_in_pool(id)) {
purple_debug_warning("certificate/tls_peers",
"Id %s wasn't in the pool\n",
id);
return FALSE;
}
/* OK, so work out the keypath and delete the thing */
keypath = purple_certificate_pool_mkpath(&x509_tls_peers, id);
if ( unlink(keypath) != 0 ) {
purple_debug_error("certificate/tls_peers",
"Unlink of %s failed!\n",
keypath);
ret = FALSE;
} else {
ret = TRUE;
}
g_free(keypath);
return ret;
}
static GList *
x509_tls_peers_get_idlist(void)
{
GList *idlist = NULL;
GDir *dir;
const gchar *entry;
gchar *poolpath;
/* Get a handle on the pool directory */
poolpath = purple_certificate_pool_mkpath(&x509_tls_peers, NULL);
dir = g_dir_open(poolpath,
0, /* No flags */
NULL); /* Not interested in what the error is */
g_free(poolpath);
g_return_val_if_fail(dir, NULL);
/* Traverse the directory listing and create an idlist */
while ( (entry = g_dir_read_name(dir)) != NULL ) {
/* Unescape the filename */
const char *unescaped = purple_unescape_filename(entry);
/* Copy the entry name into our list (GLib owns the original
string) */
idlist = g_list_prepend(idlist, g_strdup(unescaped));
}
/* Release the directory */
g_dir_close(dir);
return idlist;
}
static PurpleCertificatePool x509_tls_peers = {
"x509", /* Scheme name */
"tls_peers", /* Pool name */
N_("SSL Peers Cache"), /* User-friendly name */
NULL, /* Internal data */
x509_tls_peers_init, /* init */
NULL, /* uninit not required */
x509_tls_peers_cert_in_pool, /* Certificate exists? */
x509_tls_peers_get_cert, /* Cert retriever */
x509_tls_peers_put_cert, /* Cert writer */
x509_tls_peers_delete_cert, /* Cert remover */
x509_tls_peers_get_idlist, /* idlist retriever */
NULL,
NULL,
NULL,
NULL
};
/***** A Verifier that uses the tls_peers cache and the CA pool to validate certificates *****/
static PurpleCertificateVerifier x509_tls_cached;
/* The following is several hacks piled together and needs to be fixed.
* It exists because show_cert (see its comments) needs the original reason
* given to user_auth in order to rebuild the dialog.
*/
/* TODO: This will cause a ua_ctx to become memleaked if the request(s) get
closed by handle or otherwise abnormally. */
typedef struct {
PurpleCertificateVerificationRequest *vrq;
gchar *reason;
} x509_tls_cached_ua_ctx;
static x509_tls_cached_ua_ctx *
x509_tls_cached_ua_ctx_new(PurpleCertificateVerificationRequest *vrq,
const gchar *reason)
{
x509_tls_cached_ua_ctx *c;
c = g_new0(x509_tls_cached_ua_ctx, 1);
c->vrq = vrq;
c->reason = g_strdup(reason);
return c;
}
static void
x509_tls_cached_ua_ctx_free(x509_tls_cached_ua_ctx *c)
{
g_return_if_fail(c);
g_free(c->reason);
g_free(c);
}
static void
x509_tls_cached_user_auth(PurpleCertificateVerificationRequest *vrq,
const gchar *reason);
static void
x509_tls_cached_show_cert(x509_tls_cached_ua_ctx *c, gint id)
{
PurpleCertificate *disp_crt = c->vrq->cert_chain->data;
/* Since clicking a button closes the request, show it again */
x509_tls_cached_user_auth(c->vrq, c->reason);
/* Show the certificate AFTER re-opening the dialog so that this
appears above the other */
purple_certificate_display_x509(disp_crt);
x509_tls_cached_ua_ctx_free(c);
}
static void
x509_tls_cached_user_auth_cb (x509_tls_cached_ua_ctx *c, gint id)
{
PurpleCertificateVerificationRequest *vrq;
PurpleCertificatePool *tls_peers;
g_return_if_fail(c);
g_return_if_fail(c->vrq);
vrq = c->vrq;
x509_tls_cached_ua_ctx_free(c);
tls_peers = purple_certificate_find_pool("x509","tls_peers");
if (2 == id) {
gchar *cache_id = vrq->subject_name;
purple_debug_info("certificate/x509/tls_cached",
"User ACCEPTED cert\nCaching first in chain for future use as %s...\n",
cache_id);
purple_certificate_pool_store(tls_peers, cache_id,
vrq->cert_chain->data);
purple_certificate_verify_complete(vrq,
PURPLE_CERTIFICATE_VALID);
} else {
purple_debug_warning("certificate/x509/tls_cached",
"User REJECTED cert\n");
purple_certificate_verify_complete(vrq,
PURPLE_CERTIFICATE_INVALID);
}
}
static void
x509_tls_cached_user_auth_accept_cb(x509_tls_cached_ua_ctx *c, gint ignore)
{
x509_tls_cached_user_auth_cb(c, 2);
}
static void
x509_tls_cached_user_auth_reject_cb(x509_tls_cached_ua_ctx *c, gint ignore)
{
x509_tls_cached_user_auth_cb(c, 1);
}
/** Validates a certificate by asking the user
* @param reason String to explain why the user needs to accept/refuse the
* certificate.
* @todo Needs a handle argument
*/
static void
x509_tls_cached_user_auth(PurpleCertificateVerificationRequest *vrq,
const gchar *reason)
{
gchar *primary;
/* Make messages */
primary = g_strdup_printf(_("Accept certificate for %s?"),
vrq->subject_name);
/* Make a semi-pretty display */
purple_request_action(
vrq->cb_data, /* TODO: Find what the handle ought to be */
_("SSL Certificate Verification"),
primary,
reason,
0, /* Accept by default */
NULL, /* No account */
NULL, /* No other user */
NULL, /* No associated conversation */
x509_tls_cached_ua_ctx_new(vrq, reason),
3, /* Number of actions */
_("Accept"), x509_tls_cached_user_auth_accept_cb,
_("Reject"), x509_tls_cached_user_auth_reject_cb,
_("_View Certificate..."), x509_tls_cached_show_cert);
/* Cleanup */
g_free(primary);
}
static void
x509_tls_cached_unknown_peer(PurpleCertificateVerificationRequest *vrq,
PurpleCertificateInvalidityFlags flags);
static void
x509_tls_cached_complete(PurpleCertificateVerificationRequest *vrq,
PurpleCertificateInvalidityFlags flags)
{
PurpleCertificatePool *tls_peers;
PurpleCertificate *peer_crt = vrq->cert_chain->data;
if (flags & PURPLE_CERTIFICATE_FATALS_MASK) {
/* TODO: Also print any other warnings? */
const gchar *error;
gchar *tmp, *secondary;
if (flags & PURPLE_CERTIFICATE_INVALID_CHAIN)
error = invalidity_reason_to_string(PURPLE_CERTIFICATE_INVALID_CHAIN);
else if (flags & PURPLE_CERTIFICATE_REVOKED)
error = invalidity_reason_to_string(PURPLE_CERTIFICATE_REVOKED);
else
error = invalidity_reason_to_string(PURPLE_CERTIFICATE_UNKNOWN_ERROR);
tmp = g_strdup_printf(_("The certificate for %s could not be validated."),
vrq->subject_name);
secondary = g_strconcat(tmp, " ", error, NULL);
g_free(tmp);
purple_notify_error(NULL, /* TODO: Probably wrong. */
_("SSL Certificate Error"),
_("Unable to validate certificate"),
secondary);
g_free(secondary);
purple_certificate_verify_complete(vrq, PURPLE_CERTIFICATE_INVALID);
return;
} else if (flags & PURPLE_CERTIFICATE_NON_FATALS_MASK) {
/* Non-fatal error. Prompt the user. */
gchar *tmp;
GString *errors;
guint32 i = 1;
tmp = g_strdup_printf(_("The certificate for %s could not be validated."),
vrq->subject_name);
errors = g_string_new(tmp);
g_free(tmp);
errors = g_string_append_c(errors, '\n');
/* Special case a name mismatch because we want to display the two names... */
if (flags & PURPLE_CERTIFICATE_NAME_MISMATCH) {
gchar *sn = purple_certificate_get_subject_name(peer_crt);
if (sn) {
g_string_append_printf(errors, _("The certificate claims to be "
"from \"%s\" instead. This could mean that you are "
"not connecting to the service you believe you are."),
sn);
g_free(sn);
flags &= ~PURPLE_CERTIFICATE_NAME_MISMATCH;
}
}
while (i != PURPLE_CERTIFICATE_LAST) {
if (flags & i) {
errors = g_string_append_c(errors, '\n');
g_string_append(errors, invalidity_reason_to_string(i));
}
i <<= 1;
}
x509_tls_cached_user_auth(vrq, errors->str);
g_string_free(errors, TRUE);
return;
}
/* If we reach this point, the certificate is good. */
/* Look up the local cache and store it there for future use */
tls_peers = purple_certificate_find_pool(x509_tls_cached.scheme_name,
"tls_peers");
if (tls_peers) {
if (!purple_certificate_pool_store(tls_peers,vrq->subject_name,
peer_crt)) {
purple_debug_error("certificate/x509/tls_cached",
"FAILED to cache peer certificate\n");
}
} else {
purple_debug_error("certificate/x509/tls_cached",
"Unable to locate tls_peers certificate cache.\n");
}
purple_certificate_verify_complete(vrq, PURPLE_CERTIFICATE_VALID);
}
static void
x509_tls_cached_cert_in_cache(PurpleCertificateVerificationRequest *vrq,
PurpleCertificateInvalidityFlags flags)
{
/* TODO: Looking this up by name over and over is expensive.
Fix, please! */
PurpleCertificatePool *tls_peers =
purple_certificate_find_pool(x509_tls_cached.scheme_name,
"tls_peers");
/* The peer's certificate should be the first in the list */
PurpleCertificate *peer_crt =
(PurpleCertificate *) vrq->cert_chain->data;
PurpleCertificate *cached_crt;
GByteArray *peer_fpr, *cached_fpr;
/* Load up the cached certificate */
cached_crt = purple_certificate_pool_retrieve(
tls_peers, vrq->subject_name);
if ( !cached_crt ) {
purple_debug_warning("certificate/x509/tls_cached",
"Lookup failed on cached certificate!\n"
"Falling back to full verification.\n");
/* vrq now becomes the problem of unknown_peer */
x509_tls_cached_unknown_peer(vrq, flags);
return;
}
/* Now get SHA1 sums for both and compare them */
/* TODO: This is not an elegant way to compare certs */
peer_fpr = purple_certificate_get_fingerprint_sha1(peer_crt);
cached_fpr = purple_certificate_get_fingerprint_sha1(cached_crt);
if (!memcmp(peer_fpr->data, cached_fpr->data, peer_fpr->len)) {
purple_debug_info("certificate/x509/tls_cached",
"Peer cert matched cached\n");
x509_tls_cached_complete(vrq, flags);
} else {
purple_debug_error("certificate/x509/tls_cached",
"Peer cert did NOT match cached\n");
/* vrq now becomes the problem of the user */
x509_tls_cached_unknown_peer(vrq, flags);
}
purple_certificate_destroy(cached_crt);
g_byte_array_free(peer_fpr, TRUE);
g_byte_array_free(cached_fpr, TRUE);
}
/*
* This is called from two points in x509_tls_cached_unknown_peer below
* once we've verified the signature chain is valid. Now we need to verify
* the subject name of the certificate.
*/
static void
x509_tls_cached_check_subject_name(PurpleCertificateVerificationRequest *vrq,
PurpleCertificateInvalidityFlags flags)
{
PurpleCertificate *peer_crt;
GList *chain = vrq->cert_chain;
peer_crt = (PurpleCertificate *) chain->data;
/* Last, check that the hostname matches */
if ( ! purple_certificate_check_subject_name(peer_crt,
vrq->subject_name) ) {
gchar *sn = purple_certificate_get_subject_name(peer_crt);
flags |= PURPLE_CERTIFICATE_NAME_MISMATCH;
purple_debug_error("certificate/x509/tls_cached",
"Name mismatch: Certificate given for %s "
"has a name of %s\n",
vrq->subject_name, sn);
g_free(sn);
}
x509_tls_cached_complete(vrq, flags);
}
/* For when we've never communicated with this party before */
/* TODO: Need ways to specify possibly multiple problems with a cert, or at
least reprioritize them.
*/
static void
x509_tls_cached_unknown_peer(PurpleCertificateVerificationRequest *vrq,
PurpleCertificateInvalidityFlags flags)
{
PurpleCertificatePool *ca;
PurpleCertificate *peer_crt;
PurpleCertificate *ca_crt, *end_crt;
PurpleCertificate *failing_crt;
GList *chain = vrq->cert_chain;
GSList *ca_crts, *cur;
GByteArray *last_fpr, *ca_fpr;
gboolean valid = FALSE;
gchar *ca_id, *ca2_id;
peer_crt = (PurpleCertificate *) chain->data;
if (peer_crt->scheme->verify_cert) {
/** Make sure we've loaded the CA certs (which causes NSS to trust them) */
g_return_if_fail(x509_ca_lazy_init());
peer_crt->scheme->verify_cert(vrq, &flags);
x509_tls_cached_complete(vrq, flags);
return;
}
/* TODO: Figure out a way to check for a bad signature, as opposed to
"not self-signed" */
if ( purple_certificate_signed_by(peer_crt, peer_crt) ) {
flags |= PURPLE_CERTIFICATE_SELF_SIGNED;
purple_debug_info("certificate/x509/tls_cached",
"Certificate for %s is self-signed.\n",
vrq->subject_name);
x509_tls_cached_check_subject_name(vrq, flags);
return;
} /* if (self signed) */
ca = purple_certificate_find_pool(x509_tls_cached.scheme_name, "ca");
/* Next, check that the certificate chain is valid */
if (!purple_certificate_check_signature_chain_with_failing(chain,
&failing_crt))
{
gboolean chain_validated = FALSE;
/*
* Check if the failing certificate is in the CA store. If it is, then
* consider this fully validated. This works around issues with some
* prominent intermediate CAs whose signature is md5WithRSAEncryption.
* I'm looking at CACert Class 3 here. See #4458 for details.
*/
if (ca) {
gchar *uid = purple_certificate_get_unique_id(failing_crt);
PurpleCertificate *ca_crt = purple_certificate_pool_retrieve(ca, uid);
if (ca_crt != NULL) {
GByteArray *failing_fpr;
GByteArray *ca_fpr;
failing_fpr = purple_certificate_get_fingerprint_sha1(failing_crt);
ca_fpr = purple_certificate_get_fingerprint_sha1(ca_crt);
if (byte_arrays_equal(failing_fpr, ca_fpr)) {
purple_debug_info("certificate/x509/tls_cached",
"Full chain verification failed (probably a bad "
"signature algorithm), but found the last "
"certificate %s in the CA pool.\n", uid);
chain_validated = TRUE;
}
g_byte_array_free(failing_fpr, TRUE);
g_byte_array_free(ca_fpr, TRUE);
}
purple_certificate_destroy(ca_crt);
g_free(uid);
}
/*
* If we get here, either the cert matched the stuff right above
* or it didn't, in which case we give up and complain to the user.
*/
if (!chain_validated)
/* TODO: Tell the user where the chain broke? */
flags |= PURPLE_CERTIFICATE_INVALID_CHAIN;
x509_tls_cached_check_subject_name(vrq, flags);
return;
} /* if (signature chain not good) */
/* Next, attempt to verify the last certificate is signed by a trusted
* CA, or is a trusted CA (based on fingerprint).
*/
/* If, for whatever reason, there is no Certificate Authority pool
loaded, we'll verify the subject name and then warn about thsi. */
if ( !ca ) {
purple_debug_error("certificate/x509/tls_cached",
"No X.509 Certificate Authority pool "
"could be found!\n");
flags |= PURPLE_CERTIFICATE_NO_CA_POOL;
x509_tls_cached_check_subject_name(vrq, flags);
return;
}
end_crt = g_list_last(chain)->data;
/* Attempt to look up the last certificate, and the last certificate's
* issuer.
*/
ca_id = purple_certificate_get_issuer_unique_id(end_crt);
ca2_id = purple_certificate_get_unique_id(end_crt);
purple_debug_info("certificate/x509/tls_cached",
"Checking for a CA with DN=%s\n",
ca_id);
purple_debug_info("certificate/x509/tls_cached",
"Also checking for a CA with DN=%s\n",
ca2_id);
ca_crts = g_slist_concat(x509_ca_get_certs(ca_id), x509_ca_get_certs(ca2_id));
g_free(ca_id);
g_free(ca2_id);
if ( NULL == ca_crts ) {
flags |= PURPLE_CERTIFICATE_CA_UNKNOWN;
purple_debug_warning("certificate/x509/tls_cached",
"No Certificate Authorities with either DN found "
"found. I'll prompt the user, I guess.\n");
x509_tls_cached_check_subject_name(vrq, flags);
return;
}
/*
* Check the fingerprints; if they match, then this certificate *is* one
* of the designated "trusted roots", and we don't need to verify the
* signature. This is good because some of the older roots are self-signed
* with bad hash algorithms that we don't want to allow in any other
* circumstances (one of Verisign's root CAs is self-signed with MD2).
*
* If the fingerprints don't match, we'll fall back to checking the
* signature.
*/
last_fpr = purple_certificate_get_fingerprint_sha1(end_crt);
for (cur = ca_crts; cur; cur = cur->next) {
ca_crt = cur->data;
ca_fpr = purple_certificate_get_fingerprint_sha1(ca_crt);
if ( byte_arrays_equal(last_fpr, ca_fpr) ||
purple_certificate_signed_by(end_crt, ca_crt) )
{
/* TODO: If signed_by ever returns a reason, maybe mention
that, too. */
/* TODO: Also mention the CA involved. While I could do this
now, a full DN is a little much with which to assault the
user's poor, leaky eyes. */
valid = TRUE;
g_byte_array_free(ca_fpr, TRUE);
break;
}
g_byte_array_free(ca_fpr, TRUE);
}
if (valid == FALSE)
flags |= PURPLE_CERTIFICATE_INVALID_CHAIN;
g_slist_foreach(ca_crts, (GFunc)purple_certificate_destroy, NULL);
g_slist_free(ca_crts);
g_byte_array_free(last_fpr, TRUE);
x509_tls_cached_check_subject_name(vrq, flags);
}
static void
x509_tls_cached_start_verify(PurpleCertificateVerificationRequest *vrq)
{
const gchar *tls_peers_name = "tls_peers"; /* Name of local cache */
PurpleCertificatePool *tls_peers;
time_t now, activation, expiration;
PurpleCertificateInvalidityFlags flags = PURPLE_CERTIFICATE_NO_PROBLEMS;
gboolean ret;
g_return_if_fail(vrq);
purple_debug_info("certificate/x509/tls_cached",
"Starting verify for %s\n",
vrq->subject_name);
/*
* Verify the first certificate (the main one) has been activated and
* isn't expired, i.e. activation < now < expiration.
*/
now = time(NULL);
ret = purple_certificate_get_times(vrq->cert_chain->data, &activation,
&expiration);
if (!ret) {
flags |= PURPLE_CERTIFICATE_EXPIRED | PURPLE_CERTIFICATE_NOT_ACTIVATED;
purple_debug_error("certificate/x509/tls_cached",
"Failed to get validity times for certificate %s\n",
vrq->subject_name);
} else if (now > expiration) {
flags |= PURPLE_CERTIFICATE_EXPIRED;
purple_debug_error("certificate/x509/tls_cached",
"Certificate %s expired at %s\n",
vrq->subject_name, ctime(&expiration));
} else if (now < activation) {
flags |= PURPLE_CERTIFICATE_NOT_ACTIVATED;
purple_debug_error("certificate/x509/tls_cached",
"Certificate %s is not yet valid, will be at %s\n",
vrq->subject_name, ctime(&activation));
}
tls_peers = purple_certificate_find_pool(x509_tls_cached.scheme_name,tls_peers_name);
if (!tls_peers) {
purple_debug_error("certificate/x509/tls_cached",
"Couldn't find local peers cache %s\n",
tls_peers_name);
/* vrq now becomes the problem of unknown_peer */
x509_tls_cached_unknown_peer(vrq, flags);
return;
}
/* Check if the peer has a certificate cached already */
purple_debug_info("certificate/x509/tls_cached",
"Checking for cached cert...\n");
if (purple_certificate_pool_contains(tls_peers, vrq->subject_name)) {
purple_debug_info("certificate/x509/tls_cached",
"...Found cached cert\n");
/* vrq is now the responsibility of cert_in_cache */
x509_tls_cached_cert_in_cache(vrq, flags);
} else {
purple_debug_warning("certificate/x509/tls_cached",
"...Not in cache\n");
/* vrq now becomes the problem of unknown_peer */
x509_tls_cached_unknown_peer(vrq, flags);
}
}
static void
x509_tls_cached_destroy_request(PurpleCertificateVerificationRequest *vrq)
{
g_return_if_fail(vrq);
}
static PurpleCertificateVerifier x509_tls_cached = {
"x509", /* Scheme name */
"tls_cached", /* Verifier name */
x509_tls_cached_start_verify, /* Verification begin */
x509_tls_cached_destroy_request,/* Request cleanup */
NULL,
NULL,
NULL,
NULL
};
/****************************************************************************/
/* Subsystem */
/****************************************************************************/
void
purple_certificate_init(void)
{
/* Register builtins */
purple_certificate_register_verifier(&x509_singleuse);
purple_certificate_register_pool(&x509_ca);
purple_certificate_register_pool(&x509_tls_peers);
purple_certificate_register_verifier(&x509_tls_cached);
}
void
purple_certificate_uninit(void)
{
/* Unregister all Verifiers */
g_list_foreach(cert_verifiers, (GFunc)purple_certificate_unregister_verifier, NULL);
/* Unregister all Pools */
g_list_foreach(cert_pools, (GFunc)purple_certificate_unregister_pool, NULL);
}
gpointer
purple_certificate_get_handle(void)
{
static gint handle;
return &handle;
}
PurpleCertificateScheme *
purple_certificate_find_scheme(const gchar *name)
{
PurpleCertificateScheme *scheme = NULL;
GList *l;
g_return_val_if_fail(name, NULL);
/* Traverse the list of registered schemes and locate the
one whose name matches */
for(l = cert_schemes; l; l = l->next) {
scheme = (PurpleCertificateScheme *)(l->data);
/* Name matches? that's our man */
if(!g_ascii_strcasecmp(scheme->name, name))
return scheme;
}
purple_debug_warning("certificate",
"CertificateScheme %s requested but not found.\n",
name);
/* TODO: Signalling and such? */
return NULL;
}
GList *
purple_certificate_get_schemes(void)
{
return cert_schemes;
}
gboolean
purple_certificate_register_scheme(PurpleCertificateScheme *scheme)
{
g_return_val_if_fail(scheme != NULL, FALSE);
/* Make sure no scheme is registered with the same name */
if (purple_certificate_find_scheme(scheme->name) != NULL) {
return FALSE;
}
/* Okay, we're golden. Register it. */
cert_schemes = g_list_prepend(cert_schemes, scheme);
/* TODO: Signalling and such? */
purple_debug_info("certificate",
"CertificateScheme %s registered\n",
scheme->name);
return TRUE;
}
gboolean
purple_certificate_unregister_scheme(PurpleCertificateScheme *scheme)
{
if (NULL == scheme) {
purple_debug_warning("certificate",
"Attempting to unregister NULL scheme\n");
return FALSE;
}
/* TODO: signalling? */
/* TODO: unregister all CertificateVerifiers for this scheme?*/
/* TODO: unregister all CertificatePools for this scheme? */
/* Neither of the above should be necessary, though */
cert_schemes = g_list_remove(cert_schemes, scheme);
purple_debug_info("certificate",
"CertificateScheme %s unregistered\n",
scheme->name);
return TRUE;
}
PurpleCertificateVerifier *
purple_certificate_find_verifier(const gchar *scheme_name, const gchar *ver_name)
{
PurpleCertificateVerifier *vr = NULL;
GList *l;
g_return_val_if_fail(scheme_name, NULL);
g_return_val_if_fail(ver_name, NULL);
/* Traverse the list of registered verifiers and locate the
one whose name matches */
for(l = cert_verifiers; l; l = l->next) {
vr = (PurpleCertificateVerifier *)(l->data);
/* Scheme and name match? */
if(!g_ascii_strcasecmp(vr->scheme_name, scheme_name) &&
!g_ascii_strcasecmp(vr->name, ver_name))
return vr;
}
purple_debug_warning("certificate",
"CertificateVerifier %s, %s requested but not found.\n",
scheme_name, ver_name);
/* TODO: Signalling and such? */
return NULL;
}
GList *
purple_certificate_get_verifiers(void)
{
return cert_verifiers;
}
gboolean
purple_certificate_register_verifier(PurpleCertificateVerifier *vr)
{
g_return_val_if_fail(vr != NULL, FALSE);
/* Make sure no verifier is registered with the same scheme/name */
if (purple_certificate_find_verifier(vr->scheme_name, vr->name) != NULL) {
return FALSE;
}
/* Okay, we're golden. Register it. */
cert_verifiers = g_list_prepend(cert_verifiers, vr);
/* TODO: Signalling and such? */
purple_debug_info("certificate",
"CertificateVerifier %s registered\n",
vr->name);
return TRUE;
}
gboolean
purple_certificate_unregister_verifier(PurpleCertificateVerifier *vr)
{
if (NULL == vr) {
purple_debug_warning("certificate",
"Attempting to unregister NULL verifier\n");
return FALSE;
}
/* TODO: signalling? */
cert_verifiers = g_list_remove(cert_verifiers, vr);
purple_debug_info("certificate",
"CertificateVerifier %s unregistered\n",
vr->name);
return TRUE;
}
PurpleCertificatePool *
purple_certificate_find_pool(const gchar *scheme_name, const gchar *pool_name)
{
PurpleCertificatePool *pool = NULL;
GList *l;
g_return_val_if_fail(scheme_name, NULL);
g_return_val_if_fail(pool_name, NULL);
/* Traverse the list of registered pools and locate the
one whose name matches */
for(l = cert_pools; l; l = l->next) {
pool = (PurpleCertificatePool *)(l->data);
/* Scheme and name match? */
if(!g_ascii_strcasecmp(pool->scheme_name, scheme_name) &&
!g_ascii_strcasecmp(pool->name, pool_name))
return pool;
}
purple_debug_warning("certificate",
"CertificatePool %s, %s requested but not found.\n",
scheme_name, pool_name);
/* TODO: Signalling and such? */
return NULL;
}
GList *
purple_certificate_get_pools(void)
{
return cert_pools;
}
gboolean
purple_certificate_register_pool(PurpleCertificatePool *pool)
{
g_return_val_if_fail(pool, FALSE);
g_return_val_if_fail(pool->scheme_name, FALSE);
g_return_val_if_fail(pool->name, FALSE);
g_return_val_if_fail(pool->fullname, FALSE);
/* Make sure no pools are registered under this name */
if (purple_certificate_find_pool(pool->scheme_name, pool->name)) {
return FALSE;
}
/* Initialize the pool if needed */
if (pool->init) {
gboolean success;
success = pool->init();
if (!success)
return FALSE;
}
/* Register the Pool */
cert_pools = g_list_prepend(cert_pools, pool);
/* TODO: Emit a signal that the pool got registered */
PURPLE_DBUS_REGISTER_POINTER(pool, PurpleCertificatePool);
purple_signal_register(pool, /* Signals emitted from pool */
"certificate-stored",
purple_marshal_VOID__POINTER_POINTER,
NULL, /* No callback return value */
2, /* Two non-data arguments */
purple_value_new(PURPLE_TYPE_SUBTYPE,
PURPLE_SUBTYPE_CERTIFICATEPOOL),
purple_value_new(PURPLE_TYPE_STRING));
purple_signal_register(pool, /* Signals emitted from pool */
"certificate-deleted",
purple_marshal_VOID__POINTER_POINTER,
NULL, /* No callback return value */
2, /* Two non-data arguments */
purple_value_new(PURPLE_TYPE_SUBTYPE,
PURPLE_SUBTYPE_CERTIFICATEPOOL),
purple_value_new(PURPLE_TYPE_STRING));
purple_debug_info("certificate",
"CertificatePool %s registered\n",
pool->name);
return TRUE;
}
gboolean
purple_certificate_unregister_pool(PurpleCertificatePool *pool)
{
if (NULL == pool) {
purple_debug_warning("certificate",
"Attempting to unregister NULL pool\n");
return FALSE;
}
/* Check that the pool is registered */
if (!g_list_find(cert_pools, pool)) {
purple_debug_warning("certificate",
"Pool to unregister isn't registered!\n");
return FALSE;
}
/* Uninit the pool if needed */
PURPLE_DBUS_UNREGISTER_POINTER(pool);
if (pool->uninit) {
pool->uninit();
}
cert_pools = g_list_remove(cert_pools, pool);
/* TODO: Signalling? */
purple_signal_unregister(pool, "certificate-stored");
purple_signal_unregister(pool, "certificate-deleted");
purple_debug_info("certificate",
"CertificatePool %s unregistered\n",
pool->name);
return TRUE;
}
/****************************************************************************/
/* Scheme-specific functions */
/****************************************************************************/
static void display_x509_issuer(gchar *issuer_id) {
PurpleCertificate *issuer_crt;
issuer_crt = x509_ca_get_cert(issuer_id);
if (issuer_crt) {
purple_certificate_display_x509(issuer_crt);
purple_certificate_destroy(issuer_crt);
} else {
purple_notify_info(NULL, /* TODO: Find what the handle ought to be */
_("Certificate Information"),
"",
_("Unable to find Issuer Certificate"));
}
g_free(issuer_id);
}
void
purple_certificate_display_x509(PurpleCertificate *crt)
{
gchar *sha_asc;
GByteArray *sha_bin;
gchar *cn, *issuer_id;
time_t activation, expiration;
gchar *activ_str, *expir_str;
gchar *secondary;
gboolean self_signed;
/* Pull out the SHA1 checksum */
sha_bin = purple_certificate_get_fingerprint_sha1(crt);
/* Now decode it for display */
sha_asc = purple_base16_encode_chunked(sha_bin->data,
sha_bin->len);
/* Get the cert Common Name */
/* TODO: Will break on CA certs */
cn = purple_certificate_get_subject_name(crt);
issuer_id = purple_certificate_get_issuer_unique_id(crt);
/* Get the certificate times */
/* TODO: Check the times against localtime */
/* TODO: errorcheck? */
if (!purple_certificate_get_times(crt, &activation, &expiration)) {
purple_debug_error("certificate",
"Failed to get certificate times!\n");
activation = expiration = 0;
}
activ_str = g_strdup(ctime(&activation));
expir_str = g_strdup(ctime(&expiration));
self_signed = purple_certificate_signed_by(crt, crt);
/* Make messages */
secondary = g_strdup_printf(_("Common name: %s\n\n"
"Issued By: %s\n\n"
"Fingerprint (SHA1): %s\n\n"
"Activation date: %s\n"
"Expiration date: %s\n"),
cn ? cn : "(null)",
self_signed ? _("(self-signed)") : (issuer_id ? issuer_id : "(null)"),
sha_asc ? sha_asc : "(null)",
activ_str ? activ_str : "(null)",
expir_str ? expir_str : "(null)");
/* Make a semi-pretty display */
if (self_signed) {
purple_notify_info(NULL, /* TODO: Find what the handle ought to be */
_("Certificate Information"),
"",
secondary);
} else {
purple_request_action(NULL, /* TODO: Find what the handle ought to be */
_("Certificate Information"), _("Certificate Information"),
secondary, 2, NULL, NULL, NULL,
issuer_id, 2,
_("View Issuer Certificate"), PURPLE_CALLBACK(display_x509_issuer),
_("Close"), PURPLE_CALLBACK(g_free));
/* purple_request_action has taken ownership of issuer_id */
issuer_id = NULL;
}
/* Cleanup */
g_free(cn);
g_free(issuer_id);
g_free(secondary);
g_free(sha_asc);
g_free(activ_str);
g_free(expir_str);
g_byte_array_free(sha_bin, TRUE);
}
void purple_certificate_add_ca_search_path(const char *path)
{
if (g_list_find_custom(x509_ca_paths, path, (GCompareFunc)strcmp))
return;
x509_ca_paths = g_list_append(x509_ca_paths, g_strdup(path));
}
|