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
|
/** Implementation for GSTLS classes for GNUStep
Copyright (C) 2012 Free Software Foundation, Inc.
Written by: Richard Frith-Macdonald <rfm@gnu.org>
Date: 2101
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110 USA.
*/
#import "common.h"
#import "Foundation/NSArray.h"
#import "Foundation/NSBundle.h"
#import "Foundation/NSData.h"
#import "Foundation/NSDictionary.h"
#import "Foundation/NSEnumerator.h"
#import "Foundation/NSFileManager.h"
#import "Foundation/NSHost.h"
#import "Foundation/NSException.h"
#import "Foundation/NSLock.h"
#import "Foundation/NSNotification.h"
#import "Foundation/NSProcessInfo.h"
#import "Foundation/NSStream.h"
#import "Foundation/NSThread.h"
#import "Foundation/NSUserDefaults.h"
#import "GNUstepBase/GSTLS.h"
#import "GSPrivate.h"
@interface NSString(gnutlsFileSystemRepresentation)
- (const char*) gnutlsFileSystemRepresentation;
@end
@implementation NSString(gnutlsFileSystemRepresentation)
- (const char*) gnutlsFileSystemRepresentation
{
#if defined(_WIN32)
const unichar *buf = (const unichar*)[self fileSystemRepresentation];
int len = 0;
NSString *str;
const char *result;
while (buf[len] > 0)
{
len++;
}
str = [[NSString alloc] initWithBytes: buf
length: len * 2
encoding: NSUnicodeStringEncoding];
result = [str UTF8String];
RELEASE(str);
return result;
#else
return [self fileSystemRepresentation];
#endif
}
@end
#if defined(HAVE_GNUTLS)
static NSString *
standardizedPath(NSString *path)
{
if (0 == [path length])
{
return nil; // Not a path
}
if (NO == [path isAbsolutePath])
{
path = [[[NSFileManager defaultManager] currentDirectoryPath]
stringByAppendingPathComponent: path];
}
return [path stringByStandardizingPath];
}
#if GNUTLS_VERSION_NUMBER <= 0x020b00
/* Set up locking callbacks for gcrypt so that it will be thread-safe.
*/
static int gcry_mutex_init(void **priv)
{
NSLock *lock = [NSLock new];
*priv = (void*)lock;
return 0;
}
static int gcry_mutex_destroy(void **lock)
{
[((NSLock*)*lock) release];
return 0;
}
static int gcry_mutex_lock(void **lock)
{
[((NSLock*)*lock) lock];
return 0;
}
static int gcry_mutex_unlock(void **lock)
{
[((NSLock*)*lock) unlock];
return 0;
}
static struct gcry_thread_cbs gcry_threads_other = {
GCRY_THREAD_OPTION_DEFAULT,
NULL,
gcry_mutex_init,
gcry_mutex_destroy,
gcry_mutex_lock,
gcry_mutex_unlock
};
#endif
static void
GSTLSLog(int level, const char *msg)
{
NSLog(@"%s", msg);
}
/* The caFile variable holds the location of the file containing the default
* certificate authorities to be used by our system.
* The hard-coded value is a file in the GSTLS folder of the base library
* resource bundle, but this can be overridden by the GS_TLS_CA_FILE
* environment variable, which in turn will be overridden by the GSTLSCAFile
* user default string.
*/
static NSString *caFile = nil; // GSTLS/ca-certificates.crt
/* The caRevoke variable holds the location of the file containing the default
* certificate revocation list to be used by our system.
* The hard-coded value is a file in the GSTLS folder of the base library
* resource bundle, but this can be overridden by the GS_TLS_REVOKE
* environment variable, which in turn will be overridden by the GSTLSRevokeFile
* user default string.
*/
static NSString *revokeFile = nil; // GSTLS/revoke.crl
/* The verifyClient variable tells us if connections from a remote server
* should (by default) provide a client certificate which we verify against
* our trusted authorities.
* The hard-coded value can be overridden by the GS_TLS_VERIFY_C environment
* variable, which in turn will be overridden by the GSTLSVerifyClient user
* default string.
* A GSTLSVerify option set for a specific session overrides this default
*/
static BOOL verifyClient = NO;
/* The verifyServer variable tells us if outgoing connections (as a client)
* to a remote server should (by default) verify that server's certificate
* against our trusted authorities.
* The hard-coded value can be overridden by the GS_TLS_VERIFY_S environment
* variable, which in turn will be overridden by the GSTLSVerifyServer user
* default string.
* A GSTLSVerify option set for a specific session overrides this default
*/
static BOOL verifyServer = NO;
/* The globalDebug variable turns on gnutls debug. The hard-code value is
* overridden by GS_TLS_DEBUG, which in turn can be overridden by the
* GSTLSDebug user default. This is an integer debug level with higher
* values producing more debug output. Usually levels above 1 are too
* verbose and not useful unless you have the gnutls source code to hand.
* NB. The GSTLSDebug session option is a boolean to turn on extra debug for
* a particular session to be produced on verification failure.
*/
static int globalDebug = 0;
/* Defines the default priority list.
*/
static NSString *priority = nil;
static gnutls_anon_client_credentials_t anoncred;
/* This class is used to ensure that the GNUTLS system is initialised
* and thread-safe.
*/
@implementation GSTLSObject
static NSLock *fileLock = nil;
static NSMutableDictionary *fileMap = nil;
+ (void) _defaultsChanged: (NSNotification*)n
{
NSBundle *bundle;
NSUserDefaults *defs;
NSDictionary *env;
NSString *str;
bundle = [NSBundle bundleForClass: [NSObject class]];
defs = [NSUserDefaults standardUserDefaults];
env = [[NSProcessInfo processInfo] environment];
str = [defs stringForKey: @"GSCipherList"];
if (nil != str)
{
GSOnceMLog(@"GSCipherList is no longer used, please try GSTLSPriority");
}
str = [defs stringForKey: GSTLSPriority];
if (0 == [str length])
{
str = nil; // nil or empty string resets to default
}
ASSIGN(priority, str);
/* The GSTLSCAFile user default overrides the builtin value or the
* GS_TLS_CA_FILE environment variable.
*/
str = [defs stringForKey: GSTLSCAFile];
if (nil == str)
{
/* Let the GS_TLS_CA_FILE environment variable override the
* default certificate authority location.
*/
str = [env objectForKey: @"GS_TLS_CA_FILE"];
if (nil == str)
{
str = [bundle pathForResource: @"ca-certificates"
ofType: @"crt"
inDirectory: @"GSTLS"];
}
}
str = standardizedPath(str);
ASSIGN(caFile, str);
/* The GSTLSRevokeFile user default overrides the builtin value or the
* GS_TLS_REVOKE environment variable.
*/
str = [defs stringForKey: GSTLSRevokeFile];
if (nil == str)
{
/* Let the GS_TLS_REVOKE environment variable override the
* default revocation list location.
*/
str = [env objectForKey: @"GS_TLS_REVOKE"];
if (nil == str)
{
str = [bundle pathForResource: @"revoke"
ofType: @"crl"
inDirectory: @"GSTLS"];
}
}
str = standardizedPath(str);
ASSIGN(revokeFile, str);
str = [defs stringForKey: @"GSTLSVerifyClient"];
if (nil == str)
{
str = [env objectForKey: @"GS_TLS_VERIFY_C"];
}
verifyClient = [str boolValue];
str = [defs stringForKey: @"GSTLSVerifyServer"];
if (nil == str)
{
str = [env objectForKey: @"GS_TLS_VERIFY_S"];
}
verifyServer = [str boolValue];
str = [defs stringForKey: GSTLSDebug];
if (nil == str)
{
str = [env objectForKey: @"GS_TLS_DEBUG"];
}
globalDebug = [str intValue];
if (globalDebug < 0)
{
globalDebug = 0;
}
gnutls_global_set_log_level(globalDebug);
}
+ (NSData*) dataForTLSFile: (NSString*)fileName
{
NSData *result;
if (NO == [fileName isKindOfClass: [NSString class]])
{
[NSException raise: NSInvalidArgumentException
format: @"[GSTLS+dataForTLSFile:] called with bad file name"];
}
[fileLock lock];
NS_DURING
{
result = [[fileMap objectForKey: fileName] retain];
}
NS_HANDLER
{
[fileLock unlock];
result = nil;
[localException raise];
}
NS_ENDHANDLER
[fileLock unlock];
if (nil == result)
{
return [NSData dataWithContentsOfFile: fileName];
}
return [result autorelease];
}
+ (void) initialize
{
if ([GSTLSObject class] == self)
{
static BOOL beenHere = NO;
if (beenHere == NO)
{
beenHere = YES;
fileLock = [NSLock new];
fileMap = [NSMutableDictionary new];
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(_defaultsChanged:)
name: NSUserDefaultsDidChangeNotification
object: nil];
#if GNUTLS_VERSION_NUMBER <= 0x020b00
/* Make gcrypt thread-safe
*/
gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_other);
#endif
/* Initialise gnutls
*/
gnutls_global_init();
/* Allocate global credential information for anonymous tls
*/
gnutls_anon_allocate_client_credentials(&anoncred);
/* Enable gnutls logging via NSLog
*/
gnutls_global_set_log_function(GSTLSLog);
[self _defaultsChanged: nil];
}
}
}
+ (void) setData: (NSData*)data forTLSFile: (NSString*)fileName
{
fileName = standardizedPath(fileName);
if (nil != data && NO == [data isKindOfClass: [NSData class]])
{
[NSException raise: NSInvalidArgumentException
format: @"[GSTLS+setData:forTLSFile:] called with bad data"];
}
if (NO == [fileName isKindOfClass: [NSString class]])
{
[NSException raise: NSInvalidArgumentException
format: @"[GSTLS+setData:forTLSFile:] called with bad file"];
}
[fileLock lock];
NS_DURING
{
if (data == nil)
{
[fileMap removeObjectForKey: fileName];
}
else
{
[fileMap setObject: data forKey: fileName];
}
}
NS_HANDLER
{
[fileLock unlock];
[localException raise];
}
NS_ENDHANDLER
[fileLock unlock];
}
@end
@implementation GSTLSDHParams
static NSLock *paramsLock = nil;
static NSMutableDictionary *paramsCache = nil;
static NSTimeInterval paramsWhen = 0.0;
static BOOL paramsGenerating = NO;
static GSTLSDHParams *paramsCurrent = nil;
+ (GSTLSDHParams*) current
{
GSTLSDHParams *p;
[paramsLock lock];
if (nil == paramsCurrent)
{
if (NO == paramsGenerating)
{
[paramsLock unlock];
[self generate];
[paramsLock lock];
}
while (nil == paramsCurrent)
{
[paramsLock unlock];
[NSThread sleepForTimeInterval: 0.2];
[paramsLock lock];
}
}
p = [paramsCurrent retain];
[paramsLock unlock];
return [p autorelease];
}
+ (void) generate
{
GSTLSDHParams *p;
[paramsLock lock];
if (YES == paramsGenerating)
{
[paramsLock unlock];
return;
}
paramsGenerating = YES;
[paramsLock unlock];
p = [GSTLSDHParams new];
/* Generate Diffie-Hellman parameters - for use with DHE
* kx algorithms. When short bit length is used, it might
* be wise to regenerate parameters often.
*/
gnutls_dh_params_init(&p->params);
gnutls_dh_params_generate2 (p->params, 2048);
[paramsLock lock];
[paramsCurrent release];
paramsCurrent = p;
paramsWhen = [NSDate timeIntervalSinceReferenceDate];
paramsGenerating = NO;
[paramsLock unlock];
}
+ (void) housekeeping: (NSNotification*)n
{
NSEnumerator *enumerator;
NSString *key;
NSTimeInterval now;
now = [NSDate timeIntervalSinceReferenceDate];
[paramsLock lock];
enumerator = [[paramsCache allKeys] objectEnumerator];
while (nil != (key = [enumerator nextObject]))
{
GSTLSDHParams *p;
p = [paramsCache objectForKey: key];
if (now - p->when > 300.0)
{
[paramsCache removeObjectForKey: key];
}
}
/* Regenerate DH params once per day, perfoming generation in another
* thread since it's likely to be rather slow.
*/
if (nil != paramsCurrent && NO == paramsGenerating
&& (now = paramsWhen) > 24.0 * 60.0 * 60.0)
{
[NSThread detachNewThreadSelector: @selector(generate)
toTarget: self
withObject: nil];
}
[paramsLock unlock];
}
+ (void) initialize
{
if (nil == paramsLock)
{
paramsLock = [NSLock new];
[[NSObject leakAt: ¶msLock] release];
paramsWhen = [NSDate timeIntervalSinceReferenceDate];
paramsCache = [NSMutableDictionary new];
[[NSObject leakAt: ¶msCache] release];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(housekeeping:)
name: @"GSHousekeeping" object: nil];
}
}
+ (GSTLSDHParams*) paramsFromFile: (NSString*)f
{
GSTLSDHParams *p;
if (nil == f)
{
return nil;
}
f = standardizedPath(f);
[paramsLock lock];
p = [[paramsCache objectForKey: f] retain];
[paramsLock unlock];
if (nil == p)
{
NSData *data;
int ret;
gnutls_datum_t datum;
data = [[self class] dataForTLSFile: f];
if (nil == data)
{
NSLog(@"Unable to read DF params file '%@'", f);
return nil;
}
datum.data = (unsigned char*)[data bytes];
datum.size = (unsigned int)[data length];
p = [self alloc];
p->when = [NSDate timeIntervalSinceReferenceDate];
p->path = [f copy];
gnutls_dh_params_init(&p->params);
ret = gnutls_dh_params_import_pkcs3(p->params, &datum,
GNUTLS_X509_FMT_PEM);
if (ret < 0)
{
NSLog(@"Unable to parse DH params file '%@': %s",
p->path, gnutls_strerror(ret));
[p release];
return nil;
}
[paramsLock lock];
[paramsCache setObject: p forKey: p->path];
[paramsLock unlock];
}
return [p autorelease];
}
- (void) dealloc
{
gnutls_dh_params_deinit(params);
[super dealloc];
}
- (gnutls_dh_params_t) params
{
return params;
}
@end
@implementation GSTLSCertificateList
static NSLock *certificateListLock = nil;
static NSMutableDictionary *certificateListCache = nil;
+ (void) certInfo: (gnutls_x509_crt_t)cert to: (NSMutableString*)str
{
char dn[1024];
size_t dn_size = sizeof(dn);
char serial[40];
size_t serial_size = sizeof(serial);
time_t expiret;
time_t activet;
int algo;
unsigned int bits;
int i;
[str appendFormat: _(@"- Certificate version: #%d\n"),
gnutls_x509_crt_get_version(cert)];
dn_size = sizeof(dn);
gnutls_x509_crt_get_dn(cert, dn, &dn_size);
dn[dn_size - 1] = '\0';
[str appendFormat: @"- Certificate DN: %@\n",
[NSString stringWithUTF8String: dn]];
dn_size = sizeof(dn);
gnutls_x509_crt_get_issuer_dn(cert, dn, &dn_size);
dn[dn_size - 1] = '\0';
[str appendFormat: _(@"- Certificate Issuer's DN: %@\n"),
[NSString stringWithUTF8String: dn]];
activet = gnutls_x509_crt_get_activation_time(cert);
[str appendFormat: _(@"- Certificate is valid since: %s"),
ctime(&activet)];
expiret = gnutls_x509_crt_get_expiration_time(cert);
[str appendFormat: _(@"- Certificate expires: %s"),
ctime (&expiret)];
#if 0
{
char digest[20];
size_t digest_size = sizeof(digest);
if (gnutls_x509_fingerprint(GNUTLS_DIG_MD5,
&cert_list[0], digest, &digest_size) >= 0)
{
[str appendString: _(@"- Certificate fingerprint: ")];
for (i = 0; i < digest_size; i++)
{
[str appendFormat: @"%.2x ", (unsigned char)digest[i]];
}
[str appendString: @"\n"];
}
}
#endif
if (gnutls_x509_crt_get_serial(cert, serial, &serial_size) >= 0)
{
[str appendString: _(@"- Certificate serial number: ")];
for (i = 0; i < serial_size; i++)
{
[str appendFormat: @"%.2x ", (unsigned char)serial[i]];
}
[str appendString: @"\n"];
}
[str appendString: _(@"- Certificate public key: ")];
algo = gnutls_x509_crt_get_pk_algorithm(cert, &bits);
if (GNUTLS_PK_RSA == algo)
{
[str appendFormat: _(@"RSA - Modulus: %d bits\n"), bits];
}
else if (GNUTLS_PK_DSA == algo)
{
[str appendFormat: _(@"DSA - Exponent: %d bits\n"), bits];
}
else
{
[str appendString: _(@"UNKNOWN\n")];
}
}
/* Method to purge older lists from cache.
*/
+ (void) housekeeping: (NSNotification*)n
{
NSEnumerator *enumerator;
NSString *key;
NSTimeInterval now;
now = [NSDate timeIntervalSinceReferenceDate];
[certificateListLock lock];
enumerator = [[certificateListCache allKeys] objectEnumerator];
while (nil != (key = [enumerator nextObject]))
{
GSTLSCertificateList *list;
list = [certificateListCache objectForKey: key];
if (now - list->when > 300.0)
{
[certificateListCache removeObjectForKey: key];
}
}
[certificateListLock unlock];
}
+ (void) initialize
{
if (nil == certificateListLock)
{
certificateListLock = [NSLock new];
[[NSObject leakAt: &certificateListLock] release];
certificateListCache = [NSMutableDictionary new];
[[NSObject leakAt: &certificateListCache] release];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(housekeeping:)
name: @"GSHousekeeping" object: nil];
}
}
+ (GSTLSCertificateList*) listFromFile: (NSString*)f
{
GSTLSCertificateList *l;
if (nil == f)
{
return nil;
}
f = standardizedPath(f);
[certificateListLock lock];
l = [[certificateListCache objectForKey: f] retain];
[certificateListLock unlock];
if (nil == l)
{
NSData *data;
int ret;
gnutls_datum_t datum;
unsigned int count = 100;
gnutls_x509_crt_t crts[count];
data = [[self class] dataForTLSFile: f];
if (nil == data)
{
NSLog(@"Unable to read certificate file '%@'", f);
return nil;
}
datum.data = (unsigned char*)[data bytes];
datum.size = (unsigned int)[data length];
l = [self alloc];
l->when = [NSDate timeIntervalSinceReferenceDate];
l->path = [f copy];
ret = gnutls_x509_crt_list_import(crts, &count, &datum,
GNUTLS_X509_FMT_PEM,
// GNUTLS_X509_CRT_LIST_FAIL_IF_UNSORTED |
GNUTLS_X509_CRT_LIST_IMPORT_FAIL_IF_EXCEED);
if (ret < 0)
{
NSLog(@"Unable to parse certificate file '%@': %s",
l->path, gnutls_strerror(ret));
[l release];
return nil;
}
if (count > 0)
{
time_t now = (time_t)[[NSDate date] timeIntervalSince1970];
unsigned int i = count;
l->crts = malloc(sizeof(gnutls_x509_crt_t) * count);
memcpy(l->crts, crts, sizeof(gnutls_x509_crt_t) * count);
l->count = count;
for (i = 0; i < count; i++)
{
time_t expiret = gnutls_x509_crt_get_expiration_time(crts[i]);
time_t activet = gnutls_x509_crt_get_activation_time(crts[i]);
if (expiret <= now)
{
NSLog(@"WARNING: at index %u in %@ ... expired at %s",
i, l->path, ctime(&activet));
}
if (activet > now)
{
NSLog(@"WARNING: at index %u in %@ ... not valid until %s",
i, l->path, ctime(&activet));
}
if (expiret <= now || activet > now)
{
NSMutableString *m;
m = [NSMutableString stringWithCapacity: 2000];
[self certInfo: crts[i] to: m];
NSLog(@"%@", m);
}
}
}
[certificateListLock lock];
[certificateListCache setObject: l forKey: l->path];
[certificateListLock unlock];
}
return [l autorelease];
}
- (gnutls_x509_crt_t*) certificateList
{
return crts;
}
- (unsigned int) count
{
return count;
}
- (NSDate*) expiresAt
{
unsigned index = count;
time_t expiret;
if (index-- == 0)
{
return nil;
}
expiret = gnutls_x509_crt_get_expiration_time(crts[index]);
if (expiret < 0)
{
return nil;
}
while (index > 0)
{
time_t t = gnutls_x509_crt_get_expiration_time(crts[--index]);
if (t < 0)
{
return nil;
}
if (t < expiret)
{
expiret = t;
}
}
return [NSDate dateWithTimeIntervalSince1970: expiret];
}
- (NSDate*) expiresAt: (unsigned)index
{
time_t expiret;
if (count == 0 || index > count - 1)
{
return nil;
}
expiret = gnutls_x509_crt_get_expiration_time(crts[index]);
if (expiret < 0)
{
return nil;
}
else
{
return [NSDate dateWithTimeIntervalSince1970: expiret];
}
}
- (void) dealloc
{
if (nil != path)
{
DESTROY(path);
if (count > 0)
{
while (count-- > 0)
{
if (crts) gnutls_x509_crt_deinit(crts[count]);
}
if (crts) free(crts);
}
}
[super dealloc];
}
@end
@implementation GSTLSPrivateKey
static NSLock *privateKeyLock = nil;
static NSMutableDictionary *privateKeyCache0 = nil;
static NSMutableDictionary *privateKeyCache1 = nil;
/* Method to purge older keys from cache.
*/
+ (void) housekeeping: (NSNotification*)n
{
NSEnumerator *outer;
NSString *oKey;
NSTimeInterval now;
now = [NSDate timeIntervalSinceReferenceDate];
[privateKeyLock lock];
outer = [[privateKeyCache0 allKeys] objectEnumerator];
while (nil != (oKey = [outer nextObject]))
{
GSTLSPrivateKey *key;
key = [privateKeyCache0 objectForKey: oKey];
if (now - key->when > 300.0)
{
[privateKeyCache0 removeObjectForKey: oKey];
}
}
outer = [[privateKeyCache1 allKeys] objectEnumerator];
while (nil != (oKey = [outer nextObject]))
{
NSMutableDictionary *m;
NSEnumerator *inner;
NSString *iKey;
m = [privateKeyCache1 objectForKey: oKey];
inner = [[m allKeys] objectEnumerator];
while (nil != (iKey = [inner nextObject]))
{
GSTLSPrivateKey *key = [m objectForKey: iKey];
if (now - key->when > 300.0)
{
[m removeObjectForKey: iKey];
if (0 == [m count])
{
[privateKeyCache1 removeObjectForKey: oKey];
}
}
}
}
[privateKeyLock unlock];
}
+ (void) initialize
{
if (nil == privateKeyLock)
{
privateKeyLock = [NSLock new];
[[NSObject leakAt: &privateKeyLock] release];
privateKeyCache0 = [NSMutableDictionary new];
[[NSObject leakAt: &privateKeyCache0] release];
privateKeyCache1 = [NSMutableDictionary new];
[[NSObject leakAt: &privateKeyCache1] release];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(housekeeping:)
name: @"GSHousekeeping" object: nil];
}
}
+ (GSTLSPrivateKey*) keyFromFile: (NSString*)f withPassword: (NSString*)p
{
GSTLSPrivateKey *k;
if (nil == f)
{
return nil;
}
f = standardizedPath(f);
[privateKeyLock lock];
if (nil == p)
{
k = [privateKeyCache0 objectForKey: f];
}
else
{
NSMutableDictionary *m;
m = [privateKeyCache1 objectForKey: f];
if (nil == m)
{
k = nil;
}
else
{
k = [m objectForKey: p];
}
}
[k retain];
[privateKeyLock unlock];
if (nil == k)
{
NSData *data;
int ret;
gnutls_datum_t datum;
data = [[self class] dataForTLSFile: f];
if (nil == data)
{
NSLog(@"Unable to read private key file '%@'", f);
return nil;
}
datum.data = (unsigned char*)[data bytes];
datum.size = (unsigned int)[data length];
k = [self alloc];
k->when = [NSDate timeIntervalSinceReferenceDate];
k->path = [f copy];
k->password = [p copy];
gnutls_x509_privkey_init(&k->key);
#ifdef HAVE_GNUTLS_X509_PRIVKEY_IMPORT2
/* This function can read openssl proprietory key format,
* and uses the password if supplied.
*/
ret = gnutls_x509_privkey_import2(k->key, &datum,
GNUTLS_X509_FMT_PEM, [k->password UTF8String], 0);
#else
if (nil == k->password)
{
ret = gnutls_x509_privkey_import(k->key, &datum,
GNUTLS_X509_FMT_PEM);
}
else
{
ret = gnutls_x509_privkey_import_pkcs8(k->key, &datum,
GNUTLS_X509_FMT_PEM, [k->password UTF8String], 0);
}
#endif
if (ret < 0)
{
NSLog(@"Unable to parse private key file '%@': %s",
k->path, gnutls_strerror(ret));
[k release];
return nil;
}
[privateKeyLock lock];
if (nil == k->password)
{
[privateKeyCache0 setObject: k forKey: k->path];
}
else
{
NSMutableDictionary *m;
m = [privateKeyCache1 objectForKey: f];
if (nil == m)
{
m = [NSMutableDictionary new];
[privateKeyCache1 setObject: m forKey: f];
[m release];
}
[m setObject: k forKey: p];
}
[privateKeyLock unlock];
}
return [k autorelease];
}
- (void) dealloc
{
if (nil != path)
{
DESTROY(path);
DESTROY(password);
gnutls_x509_privkey_deinit(key);
}
[super dealloc];
}
- (gnutls_x509_privkey_t) key
{
return key;
}
@end
@implementation GSTLSCredentials
static NSLock *credentialsLock = nil;
static NSMutableDictionary *credentialsCache = nil;
/* Method to purge older credentials from cache.
*/
+ (void) housekeeping: (NSNotification*)n
{
NSEnumerator *enumerator;
NSDictionary *key;
NSTimeInterval now;
now = [NSDate timeIntervalSinceReferenceDate];
[credentialsLock lock];
enumerator = [[credentialsCache allKeys] objectEnumerator];
while (nil != (key = [enumerator nextObject]))
{
GSTLSCredentials *cred;
cred = [credentialsCache objectForKey: key];
if (now - cred->when > 300.0)
{
[credentialsCache removeObjectForKey: key];
}
}
[credentialsLock unlock];
}
+ (void) initialize
{
if (nil == credentialsLock)
{
credentialsLock = [NSLock new];
[[NSObject leakAt: &credentialsLock] release];
credentialsCache = [NSMutableDictionary new];
[[NSObject leakAt: &credentialsCache] release];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(housekeeping:)
name: @"GSHousekeeping" object: nil];
}
}
+ (GSTLSCredentials*) credentialsFromCAFile: (NSString*)ca
defaultCAFile: (NSString*)dca
revokeFile: (NSString*)rv
defaultRevokeFile: (NSString*)drv
certificateFile: (NSString*)cf
certificateKeyFile: (NSString*)ck
certificateKeyPassword: (NSString*)cp
asClient: (BOOL)client
debug: (BOOL)debug
{
GSTLSCredentials *c;
NSMutableString *k;
/* Build a unique key for the credentials based on all the
* information used to build them (apart from password used
* to load the key).
*/
k = [NSMutableString stringWithCapacity: 1024];
ca = standardizedPath(ca);
if (nil != ca) [k appendString: ca];
[k appendString: @":"];
if (nil != dca) [k appendString: dca];
[k appendString: @":"];
rv = standardizedPath(rv);
if (nil != rv) [k appendString: rv];
[k appendString: @":"];
if (nil != drv) [k appendString: drv];
[k appendString: @":"];
if (nil != cf) [k appendString: cf];
[k appendString: @":"];
if (nil != ck) [k appendString: ck];
[credentialsLock lock];
c = [credentialsCache objectForKey: k];
if (nil != c)
{
[c retain];
if (YES == debug)
{
NSLog(@"Re-used credentials %p for '%@'", c, k);
}
}
[credentialsLock unlock];
if (nil == c)
{
c = [self new];
c->name = [k copy];
c->when = [NSDate timeIntervalSinceReferenceDate];
gnutls_certificate_allocate_credentials(&c->certcred);
c->freeCred = YES; // Need to free on dealloc
/* Set the default trusted authority certificates.
*/
if ([dca length] > 0)
{
const char *path;
int ret;
path = [dca gnutlsFileSystemRepresentation];
ret = gnutls_certificate_set_x509_trust_file(c->certcred,
path, GNUTLS_X509_FMT_PEM);
if (ret < 0)
{
NSLog(@"Problem loading trusted authorities from %@: %s",
dca, gnutls_strerror(ret));
}
else
{
if (ret > 0)
{
c->trust = YES; // Loaded at least one trusted CA
}
if (YES == debug)
{
NSLog(@"Default trusted authorities (from %@): %d",
dca, ret);
}
}
}
/* Load any specified trusted authority certificates.
*/
if ([ca length] > 0)
{
const char *path;
int ret;
path = [dca gnutlsFileSystemRepresentation];
ret = gnutls_certificate_set_x509_trust_file(c->certcred,
path, GNUTLS_X509_FMT_PEM);
if (ret < 0)
{
NSLog(@"Problem loading trusted authorities from %@: %s",
ca, gnutls_strerror(ret));
}
else
{
if (ret > 0)
{
c->trust = YES;
}
else if (0 == ret)
{
NSLog(@"No certificates processed from %@", ca);
}
if (YES == debug)
{
NSLog(@"Trusted authorities (from %@): %d", ca, ret);
}
}
}
/* Load default revocation list.
*/
if ([drv length] > 0)
{
const char *path;
int ret;
path = [drv gnutlsFileSystemRepresentation];
ret = gnutls_certificate_set_x509_crl_file(c->certcred,
path, GNUTLS_X509_FMT_PEM);
if (ret < 0)
{
NSLog(@"Problem loading default revocation list from %@: %s",
drv, gnutls_strerror(ret));
}
else
{
if (YES == debug)
{
NSLog(@"Default revocations (from %@): %d", drv, ret);
}
}
}
/* Load any specified revocation list.
*/
if ([rv length] > 0)
{
const char *path;
int ret;
path = [rv gnutlsFileSystemRepresentation];
ret = gnutls_certificate_set_x509_crl_file(c->certcred,
path, GNUTLS_X509_FMT_PEM);
if (ret < 0)
{
NSLog(@"Problem loading revocation list from %@: %s",
rv, gnutls_strerror(ret));
}
else
{
if (0 == ret)
{
NSLog(@"No revocations processed from %@", rv);
}
if (YES == debug)
{
NSLog(@"Revocations (from %@): %d", rv, ret);
}
}
}
/* Get the key for our certificate .. if one is specified.
*/
if (nil != ck)
{
c->key = [[GSTLSPrivateKey keyFromFile: ck
withPassword: cp] retain];
if (nil == c->key)
{
[c release];
return nil;
}
}
/* Load our certificate (may be a list) if the file is specified.
*/
if (nil != cf)
{
c->list = [[GSTLSCertificateList listFromFile: cf] retain];
if (nil == c->list)
{
[c release];
return nil;
}
}
/* If we have loaded a certificate, we add it to the credentials
* using the certificate key so we can use it.
*/
if (nil != c->list)
{
int ret;
ret = gnutls_certificate_set_x509_key(c->certcred,
[c->list certificateList], [c->list count], [c->key key]);
if (ret < 0)
{
c->freeCred = NO; // Already freed
NSLog(@"Unable to set certificate for session: %s",
gnutls_strerror(ret));
[c release];
return nil;
}
/*
if (NO == client)
{
// FIXME ... if the server certificate required DH params ...
c->dhParams = [[GSTLSDHParams current] retain];
gnutls_certificate_set_dh_params(c->certcred,
[c->dhParams params]);
}
*/
}
if (YES == debug)
{
NSLog(@"%@ created credentials %p for '%@'", self, c, k);
}
[credentialsLock lock];
[credentialsCache setObject: c forKey: c->name];
[credentialsLock unlock];
}
return [c autorelease];
}
- (void) dealloc
{
if (nil != name)
{
if (YES == freeCred)
{
gnutls_certificate_free_credentials(certcred);
}
DESTROY(key);
DESTROY(list);
DESTROY(dhParams);
DESTROY(name);
}
[super dealloc];
}
- (gnutls_certificate_credentials_t) credentials
{
return certcred;
}
- (GSTLSPrivateKey*) key
{
return key;
}
- (GSTLSCertificateList*) list
{
return list;
}
- (BOOL) trust
{
return trust;
}
@end
#if GNUTLS_VERSION_NUMBER >= 0x020C00
/* Callback used only when debug is enabled, to print the request for a
* certificate and the response to that request.
* NB. This function always returns the certificate set for the session
* even if that certificate does not match the CAs or algorithms requested
* by the server. This differs from the default behavior which is for the
* library code to only return a certificate matching the request.
* So, the logging of a returned certificate does not guarantee that the
* certificate is acceptable to the server.
*/
static int
retrieve_callback(gnutls_session_t session,
const gnutls_datum_t *req_ca_rdn,
int nreqs,
const gnutls_pk_algorithm_t *sign_algos,
int sign_algos_length,
gnutls_retr2_st *st)
{
GSTLSSession *s = gnutls_session_get_ptr(session);
char issuer_dn[256];
int i;
int ret;
size_t len;
/* Print the server's trusted CAs
*/
if (nreqs > 0)
NSLog(@"- Server's trusted authorities:");
else
NSLog(@"- Server did not send us any trusted authorities names.");
/* print the names (if any) */
for (i = 0; i < nreqs; i++)
{
len = sizeof(issuer_dn);
ret = gnutls_x509_rdn_get(&req_ca_rdn[i], issuer_dn, &len);
if (ret >= 0)
{
NSLog(@" [%d]: %s", i, issuer_dn);
}
}
/* Select a certificate and return it.
* The certificate must be of any of the "sign algorithms"
* supported by the server.
*/
if (gnutls_certificate_type_get(session) == GNUTLS_CRT_X509)
{
GSTLSCredentials *credentials = [s credentials];
GSTLSPrivateKey *key = [credentials key];
GSTLSCertificateList *list = [credentials list];
int count = (int)[list count];
gnutls_x509_crt_t *crts = [list certificateList];
NSMutableString *m;
m = [NSMutableString stringWithCapacity: 2000];
for (i = 0; i < count; i++)
{
[GSTLSCertificateList certInfo: crts[i]
to: m];
}
if (0 == count)
{
[m appendString: @"None."];
}
NSLog(@"Certificates retrieved for sending to peer -\n%@", m);
st->cert_type = GNUTLS_CRT_X509;
st->ncerts = count;
st->cert.x509 = crts;
st->key.x509 = [key key];
return 0;
}
else
{
return -1;
}
}
#endif
@implementation GSTLSSession
+ (GSTLSSession*) sessionWithOptions: (NSDictionary*)options
direction: (BOOL)isOutgoing
transport: (void*)handle
push: (GSTLSIOW)pushFunc
pull: (GSTLSIOR)pullFunc
{
GSTLSSession *sess;
sess = [[self alloc] initWithOptions: options
direction: isOutgoing
transport: handle
push: pushFunc
pull: pullFunc];
return [sess autorelease];
}
- (BOOL) active
{
return active;
}
- (NSTimeInterval) age
{
return [NSDate timeIntervalSinceReferenceDate] - created;
}
- (GSTLSCredentials*) credentials
{
return credentials;
}
- (void) dealloc
{
[self finalize];
DESTROY(opts);
DESTROY(credentials);
DESTROY(problem);
DESTROY(issuer);
DESTROY(owner);
[super dealloc];
}
- (BOOL) debug
{
return debug;
}
- (BOOL) disconnect: (BOOL)reusable
{
BOOL ok = YES;
if (YES == active || YES == handshake)
{
active = NO;
handshake = NO;
if (NO == reusable)
{
gnutls_bye(session, GNUTLS_SHUT_WR);
}
else
{
int result;
do
{
result = gnutls_bye(session, GNUTLS_SHUT_RDWR);
}
while (GNUTLS_E_AGAIN == result || GNUTLS_E_INTERRUPTED == result);
if (result < 0)
{
ok = NO;
}
}
}
if (YES == setup)
{
setup = NO;
gnutls_db_remove_session(session);
gnutls_deinit(session);
}
return ok;
}
- (void) finalize
{
[self disconnect: NO];
[super finalize];
}
- (id) initWithOptions: (NSDictionary*)options
direction: (BOOL)isOutgoing
transport: (void*)handle
push: (GSTLSIOW)pushFunc
pull: (GSTLSIOR)pullFunc
{
if (nil != (self = [super init]))
{
NSString *ca;
NSString *dca;
NSString *rv;
NSString *drv;
NSString *cf;
NSString *ck;
NSString *cp;
NSString *pri;
NSString *str;
BOOL trust;
BOOL verify;
created = [NSDate timeIntervalSinceReferenceDate];
opts = [options copy];
outgoing = isOutgoing ? YES : NO;
if (YES == outgoing)
{
verify = verifyServer; // Verify connection to remote server
}
else
{
verify = verifyClient; // Verify certificate of remote client
}
str = [opts objectForKey: GSTLSVerify];
if (nil != str)
{
verify = [str boolValue];
}
debug = (globalDebug > 0) ? YES : NO;
if (NO == debug)
{
debug = [[opts objectForKey: GSTLSDebug] boolValue];
}
/* Now initialise session and set it up. It's simplest to always
* allocate a credentials structure at this point (and get rid of
* it when the session is disconnected) too.
*/
if (YES == outgoing)
{
gnutls_init(&session, GNUTLS_CLIENT);
str = [opts objectForKey: GSTLSServerName];
if ([str length] > 0)
{
const char *ptr = [str UTF8String];
unsigned len = strlen(ptr);
int ret;
ret = gnutls_server_name_set(session, GNUTLS_NAME_DNS, ptr, len);
if (YES == debug)
{
if (ret < 0)
{
NSLog(@"%@ %@: failed '%s'", self, GSTLSServerName,
gnutls_strerror(ret));
}
else
{
NSLog(@"%@ %@: set to '%s'", self, GSTLSServerName, ptr);
}
}
}
else if (YES == debug)
{
NSLog(@"%@ %@: not set", self, GSTLSServerName);
}
}
else
{
gnutls_init(&session, GNUTLS_SERVER);
if (NO == verify)
{
/* We don't want to demand/verify the client certificate,
* but we still ask the other end to send it so that higher
* level code can see what distinguished names are in it.
*/
gnutls_certificate_server_set_request(session,
GNUTLS_CERT_REQUEST);
}
else
{
/* We request the client certificate and require them client
* end to send it (if not, we don't allow the session).
*/
gnutls_certificate_server_set_request(session,
GNUTLS_CERT_REQUIRE);
}
}
setup = YES;
ca = [opts objectForKey: GSTLSCAFile];
dca = caFile;
rv = [opts objectForKey: GSTLSRevokeFile];
drv = revokeFile;
cf = [opts objectForKey: GSTLSCertificateFile];
ck = [opts objectForKey: GSTLSCertificateKeyFile];
cp = [opts objectForKey: GSTLSCertificateKeyPassword];
credentials = [[GSTLSCredentials credentialsFromCAFile: ca
defaultCAFile: dca
revokeFile: rv
defaultRevokeFile: drv
certificateFile: cf
certificateKeyFile: ck
certificateKeyPassword: cp
asClient: outgoing
debug: debug] retain];
if (nil == credentials)
{
[self release];
return nil;
}
trust = [credentials trust];
if (YES == verify && NO == trust)
{
NSLog(@"You have requested that a TLS/SSL connection be to a remote"
@" system with a verified certificate, but have provided no trusted"
@" certificate authorities.");
NSLog(@"If you did not use the GSTLSCAFile option to specify a file"
@" containing certificate authorities for a session, and did not"
@" specify a default file using the GSTLSCAFile user default or"
@" the GS_TLS_CA_FILE environment variable, then the system will"
@" have attempted to use the GSTLS/ca-certificates.crt file in the"
@" gnustep-base resource bundle. Unfortunately, it has not been"
@" possible to read any trusted certificate authorities from"
@" these locations.");
}
pri = [opts objectForKey: NSStreamSocketSecurityLevelKey];
str = [opts objectForKey: GSTLSPriority];
if (nil == pri && nil == str)
{
str = priority; // Default setting
}
if (YES == [str isEqual: @"SSLv3"])
{
pri = NSStreamSocketSecurityLevelSSLv3;
str = nil;
}
else if (YES == [str isEqual: @"TLSv1"])
{
pri = NSStreamSocketSecurityLevelTLSv1;
str = nil;
}
if (nil == str)
{
if ([pri isEqual: NSStreamSocketSecurityLevelNone] == YES)
{
// pri = NSStreamSocketSecurityLevelNone;
GSOnceMLog(@"NSStreamSocketSecurityLevelNone is insecure ..."
@" not implemented");
DESTROY(self);
return nil;
}
else if ([pri isEqual: NSStreamSocketSecurityLevelSSLv2] == YES)
{
// pri = NSStreamSocketSecurityLevelSSLv2;
GSOnceMLog(@"NSStreamSocketSecurityLevelSSLv2 is insecure ..."
@" not implemented");
DESTROY(self);
return nil;
}
else if ([pri isEqual: NSStreamSocketSecurityLevelSSLv3] == YES)
{
#if GNUTLS_VERSION_NUMBER < 0x020C00
const int proto_prio[2] = {
GNUTLS_SSL3,
0 };
gnutls_protocol_set_priority(session, proto_prio);
#else
gnutls_priority_set_direct(session,
"NORMAL:-VERS-TLS-ALL:+VERS-SSL3.0", NULL);
#endif
GSOnceMLog(@"NSStreamSocketSecurityLevelSSLv3 is insecure ..."
@" please change your code to stop using it");
}
else if ([pri isEqual: NSStreamSocketSecurityLevelTLSv1] == YES)
{
#if GNUTLS_VERSION_NUMBER < 0x020C00
const int proto_prio[4] = {
#if defined(GNUTLS_TLS1_2)
GNUTLS_TLS1_2,
#endif
GNUTLS_TLS1_1,
GNUTLS_TLS1_0,
0 };
gnutls_protocol_set_priority(session, proto_prio);
#else
gnutls_priority_set_direct(session,
"NORMAL:-VERS-SSL3.0:+VERS-TLS-ALL", NULL);
#endif
}
else
{
#if GNUTLS_VERSION_NUMBER < 0x020C00
gnutls_set_default_priority(session);
#else
/* By default we disable SSL3.0 as the 'POODLE' attack (Oct 2014)
* renders it insecure.
*/
gnutls_priority_set_direct(session, "NORMAL:-VERS-SSL3.0", NULL);
#endif
}
}
else
{
#if GNUTLS_VERSION_NUMBER < 0x020C00
gnutls_set_default_priority(session);
#else
/* By default we disable SSL3.0 as the 'POODLE' attack (Oct 2014)
* renders it insecure.
*/
const char *err_pos;
if (gnutls_priority_set_direct(session, [str UTF8String], &err_pos))
{
NSLog(@"Invalid GSTLSPriority: %s", err_pos);
NSLog(@"Falling back to NORMAL:-VERS-SSL3.0");
gnutls_priority_set_direct(session, "NORMAL:-VERS-SSL3.0", NULL);
}
#endif
}
/* Set certificate credentials for this session.
*/
gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE,
[credentials credentials]);
#if GNUTLS_VERSION_NUMBER >= 0x020C00
if (YES == outgoing && YES == debug)
{
/* Set a callback to log handling of a request (from the server)
* for the client certificate. The callback always returns the
* certificate set for this session, even if that does not match
* the server's request.
*/
gnutls_certificate_set_retrieve_function(
[credentials credentials], retrieve_callback);
}
#endif
/* Set transport layer to use
*/
#if GNUTLS_VERSION_NUMBER < 0x020C00
gnutls_transport_set_lowat(session, 0);
#endif
gnutls_transport_set_pull_function(session, pullFunc);
gnutls_transport_set_push_function(session, pushFunc);
gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t)handle);
gnutls_session_set_ptr(session, (void*)self);
}
return self;
}
- (BOOL) handshake
{
int ret;
if (YES == active || NO == setup)
{
return YES; // Handshake completed or impossible.
}
handshake = YES;
ret = gnutls_handshake(session);
if (ret < 0)
{
if (gnutls_error_is_fatal(ret))
{
NSString *p;
p = [NSString stringWithFormat: @"%s", gnutls_strerror(ret)];
/* We want to differentiate between errors which are usually
* due to the remote end not expecting to be using TLS/SSL,
* and errors which are caused by other interoperability
* issues. The first sort are not normally worth reporting.
*/
if (ret == GNUTLS_E_UNEXPECTED_PACKET_LENGTH
|| ret == GNUTLS_E_FATAL_ALERT_RECEIVED
|| ret == GNUTLS_E_DECRYPTION_FAILED
#ifdef GNUTLS_E_PREMATURE_TERMINATION
|| ret == GNUTLS_E_PREMATURE_TERMINATION
#endif
|| ret == GNUTLS_E_UNSUPPORTED_VERSION_PACKET)
{
p = [p stringByAppendingString:
@"\nmost often due to the remote end not expecting TLS/SSL"];
ASSIGN(problem, p);
if (YES == debug)
{
NSLog(@"%@ in handshake: %@", self, p);
}
}
else
{
ASSIGN(problem, p);
NSLog(@"%@ in handshake: %@", self, p);
}
[self disconnect: NO];
return YES; // Failed ... not active.
}
else
{
return NO; // Non-fatal error needs a retry.
}
}
else
{
NSString *str;
BOOL shouldVerify = NO;
active = YES; // The TLS session is now active.
handshake = NO; // Handshake is over.
if (YES == outgoing)
{
shouldVerify = verifyServer; // Verify remote server certificate?
}
else
{
shouldVerify = verifyClient; // Verify remote client certificate?
}
str = [opts objectForKey: GSTLSVerify];
if (nil != str)
{
shouldVerify = [str boolValue];
}
if (globalDebug > 1)
{
NSLog(@"%@ trying verify:\n%@", self, [self sessionInfo]);
}
ret = [self verify];
if (ret < 0)
{
if (globalDebug > 1 || (YES == shouldVerify && globalDebug > 0)
|| YES == [[opts objectForKey: GSTLSDebug] boolValue])
{
NSLog(@"%@ unable to verify SSL connection - %s",
self, gnutls_strerror(ret));
NSLog(@"%@ %@", self, [self sessionInfo]);
}
if (YES == shouldVerify)
{
[self disconnect: NO];
}
}
else
{
if (globalDebug > 1)
{
NSLog(@"%@ succeeded verify:\n%@", self, [self sessionInfo]);
}
}
return YES; // Handshake complete
}
}
- (NSString*) issuer
{
return issuer;
}
- (NSString*) owner
{
return owner;
}
- (NSString*) problem
{
return problem;
}
- (NSInteger) read: (void*)buf length: (NSUInteger)len
{
int result = gnutls_record_recv(session, buf, len);
if (result < 0)
{
NSString *p;
if (GNUTLS_E_AGAIN == result)
{
errno = EAGAIN; // Need to retry.
}
else if (GNUTLS_E_INTERRUPTED == result)
{
errno = EINTR; // Need to retry
}
else if (gnutls_error_is_fatal(result))
{
p = [NSString stringWithFormat: @"%s", gnutls_strerror(result)];
ASSIGN(problem, p);
if (YES == debug)
{
NSLog(@"%@ in read: %@", self, p);
}
if (EAGAIN == errno || EINTR == errno)
{
errno = EBADF; // Fatal ... don't retry
}
}
else
{
if (GNUTLS_E_WARNING_ALERT_RECEIVED == result)
{
if (YES == debug)
{
p = [NSString stringWithFormat: @"%s",
gnutls_alert_get_name(gnutls_alert_get(session))];
NSLog(@"%@ in read: %@", self, p);
}
}
errno = EAGAIN; // Need to retry.
}
result = -1;
#if defined(_WIN32)
/* Windows specific code expects to use winsock functions for error
* codes rather than looking at errno, so we must translate a few.
*/
if (EAGAIN == errno)
WSASetLastError(WSAEWOULDBLOCK);
else if (EINTR == errno)
WSASetLastError(WSAEINTR);
else
WSASetLastError(errno);
#endif
}
return result;
}
- (NSInteger) write: (const void*)buf length: (NSUInteger)len
{
int result = gnutls_record_send(session, buf, len);
if (result < 0)
{
if (GNUTLS_E_AGAIN == result)
{
errno = EAGAIN; // Need to retry.
}
else if (GNUTLS_E_INTERRUPTED == result)
{
errno = EINTR; // Need to retry
}
else if (gnutls_error_is_fatal(result))
{
NSString *p;
p = [NSString stringWithFormat: @"%s", gnutls_strerror(result)];
ASSIGN(problem, p);
if (YES == debug)
{
NSLog(@"%@ in write: %@", self, p);
}
if (EAGAIN == errno || EINTR == errno)
{
errno = EBADF; // Fatal ... don't retry
}
}
else
{
errno = EAGAIN; // Need to retry.
}
result = -1;
#if defined(_WIN32)
/* Windows specific code expects to use winsock functions for error
* codes rather than looking at errno, so we must translate a few.
*/
if (EAGAIN == errno)
WSASetLastError(WSAEWOULDBLOCK);
else if (EINTR == errno)
WSASetLastError(WSAEINTR);
else
WSASetLastError(errno);
#endif
}
return result;
}
/* Copied/based on the public domain code provided by gnutls
* to print the session ... I've left in details for features
* we don't yet support.
*/
- (NSString*) sessionInfo
{
NSMutableString *str;
const char *tmp;
gnutls_credentials_type_t cred;
gnutls_kx_algorithm_t kx;
int dhe = 0;
#if defined(XXX_ECDH)
/* At some point we may want to implement ecdh */
int ecdh = 0;
#endif
str = [NSMutableString stringWithCapacity: 2000];
/* get the key exchange's algorithm name
*/
kx = gnutls_kx_get(session);
tmp = gnutls_kx_get_name(kx);
[str appendFormat: _(@"- Key Exchange: %s\n"), tmp];
/* Check the authentication type used and switch to the appropriate.
*/
cred = gnutls_auth_get_type(session);
switch (cred)
{
case GNUTLS_CRD_IA:
[str appendString: _(@"- TLS/IA session\n")];
break;
case GNUTLS_CRD_SRP:
#ifdef ENABLE_SRP
[str appendFormat: _(@"- SRP session with username %s\n"),
gnutls_srp_server_get_username(session)];
#endif
break;
case GNUTLS_CRD_PSK:
#if 0
/* This returns NULL in server side.
*/
if (gnutls_psk_client_get_hint(session) != NULL)
{
[str appendFormat: _(@"- PSK authentication. PSK hint '%s'\n"),
gnutls_psk_client_get_hint(session)];
}
/* This returns NULL in client side.
*/
if (gnutls_psk_server_get_username(session) != NULL)
{
[str appendFormat: _(@"- PSK authentication. Connected as '%s'\n"),
gnutls_psk_server_get_username(session)];
}
if (GNUTLS_KX_ECDHE_PSK == kx)
{
ecdh = 1;
}
else if (GNUTLS_KX_DHE_PSK == kx)
{
dhe = 1;
}
#endif
break;
case GNUTLS_CRD_ANON: /* anonymous authentication */
#if 0
[str appendFormat: _(@"- Anonymous authentication.\n")];
if (GNUTLS_KX_ANON_ECDH == kx)
{
ecdh = 1;
}
else if (GNUTLS_KX_ANON_DH == kx)
{
dhe = 1;
}
#endif
break;
case GNUTLS_CRD_CERTIFICATE: /* certificate authentication */
/* Check if we have been using ephemeral Diffie-Hellman.
*/
if (GNUTLS_KX_DHE_RSA == kx || GNUTLS_KX_DHE_DSS == kx)
{
dhe = 1;
}
#if defined(XXX_ECDH)
if (GNUTLS_KX_ECDHE_RSA == kx || GNUTLS_KX_ECDHE_ECDSA == kx)
{
dhe = 0;
ecdh = 1;
}
#endif
tmp = gnutls_certificate_type_get_name(
gnutls_certificate_type_get(session));
[str appendFormat: _(@"- Authentication using certificate type: %s\n"),
tmp];
break;
} /* switch */
#if defined(XXXECDH)
if (ecdh != 0)
{
[str appendFormat: _(@"- Ephemeral ECDH using curve %s\n"),
gnutls_ecc_curve_get_name(gnutls_ecc_curve_get(session))];
}
#endif
if (dhe != 0)
{
[str appendFormat: _(@"- Ephemeral DH using prime of %d bits\n"),
gnutls_dh_get_prime_bits(session)];
}
/* print the protocol's name (eg TLS 1.0)
*/
tmp = gnutls_protocol_get_name(gnutls_protocol_get_version(session));
[str appendFormat: _(@"- Protocol: %s\n"), tmp];
/* print the certificates of the peer.
*/
if (gnutls_certificate_type_get(session) == GNUTLS_CRT_X509)
{
unsigned int cert_list_size = 0;
const gnutls_datum_t *cert_list;
gnutls_x509_crt_t cert;
cert_list = gnutls_certificate_get_peers(session, &cert_list_size);
if (0 == cert_list_size)
{
[str appendString: _(@"- Peer provided no certificate.\n")];
}
else
{
int cert_num;
for (cert_num = 0; cert_num < cert_list_size; cert_num++)
{
gnutls_x509_crt_init(&cert);
/* NB. the list of peer certificate is in memory in native
* format (DER) rather than the normal file format (PEM).
*/
gnutls_x509_crt_import(cert,
&cert_list[cert_num], GNUTLS_X509_FMT_DER);
[str appendFormat: _(@"- Certificate %d info:\n"), cert_num];
[GSTLSCertificateList certInfo: cert to: str];
gnutls_x509_crt_deinit(cert);
}
}
}
else
{
tmp = gnutls_certificate_type_get_name(
gnutls_certificate_type_get(session));
[str appendFormat: _(@"- Certificate Type: %s\n"), tmp];
}
/* print the name of the cipher used.
* eg 3DES.
*/
tmp = gnutls_cipher_get_name(gnutls_cipher_get(session));
[str appendFormat: _(@"- Cipher: %s\n"), tmp];
/* Print the MAC algorithms name.
* eg SHA1
*/
tmp = gnutls_mac_get_name(gnutls_mac_get(session));
[str appendFormat: _(@"- MAC: %s\n"), tmp];
return str;
}
- (int) verify
{
NSArray *names;
NSString *str;
unsigned int status;
const gnutls_datum_t *cert_list;
unsigned int cert_list_size;
int ret;
gnutls_x509_crt_t cert;
/* This verification function uses the trusted CAs in the credentials
* structure. So you must have installed one or more CA certificates.
*/
ret = gnutls_certificate_verify_peers2(session, &status);
if (ret < 0)
{
str = [NSString stringWithFormat:
@"TLS verification: error %s", gnutls_strerror(ret)];
ASSIGN(problem, str);
if (YES == debug) NSLog(@"%@ %@", self, problem);
return GNUTLS_E_CERTIFICATE_ERROR;
}
if (YES == debug)
{
if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
NSLog(@"%@ TLS verification: certificate hasn't got a known issuer.",
self);
if (status & GNUTLS_CERT_REVOKED)
NSLog(@"%@ TLS verification: certificate has been revoked.", self);
#if defined(GNUTLS_CERT_EXPIRED)
if (status & GNUTLS_CERT_EXPIRED)
NSLog(@"%@ TLS verification: certificate has expired", self);
#endif
#if defined(GNUTLS_CERT_NOT_ACTIVATED)
if (status & GNUTLS_CERT_NOT_ACTIVATED)
NSLog(@"%@ TLS verification: certificate is not yet activated", self);
#endif
}
if (status & GNUTLS_CERT_INVALID)
{
ASSIGN(problem,
@"TLS verification: remote certificate is not trusted.");
if (YES == debug) NSLog(@"%@ %@", self, problem);
return GNUTLS_E_CERTIFICATE_ERROR;
}
/* Up to here the process is the same for X.509 certificates and
* OpenPGP keys. From now on X.509 certificates are assumed. This can
* be easily extended to work with openpgp keys as well.
*/
if (gnutls_certificate_type_get(session) != GNUTLS_CRT_X509)
{
ASSIGN(problem,
@"TLS verification: remote certificate not of the X509 type.");
if (YES == debug) NSLog(@"%@ %@", self, problem);
return GNUTLS_E_CERTIFICATE_ERROR;
}
if (gnutls_x509_crt_init(&cert) < 0)
{
ASSIGN(problem, @"TLS verification: error in certificate initialization");
gnutls_x509_crt_deinit(cert);
if (YES == debug) NSLog(@"%@ %@", self, problem);
return GNUTLS_E_CERTIFICATE_ERROR;
}
cert_list = gnutls_certificate_get_peers(session, &cert_list_size);
if (cert_list == NULL)
{
ASSIGN(problem, @"TLS verification: no certificate from remote end!");
gnutls_x509_crt_deinit(cert);
if (YES == debug) NSLog(@"%@ %@", self, problem);
return GNUTLS_E_CERTIFICATE_ERROR;
}
if (gnutls_x509_crt_import(cert, &cert_list[0], GNUTLS_X509_FMT_DER) < 0)
{
ASSIGN(problem, @"TLS verification: error parsing certificate");
gnutls_x509_crt_deinit(cert);
if (YES == debug) NSLog(@"%@ %@", self, problem);
return GNUTLS_E_CERTIFICATE_ERROR;
}
else
{
char dn[1024];
size_t dn_size;
/* Get certificate owner and issuer
*/
dn_size = sizeof(dn)-1;
gnutls_x509_crt_get_dn(cert, dn, &dn_size);
dn[dn_size] = '\0';
ASSIGN(owner, [NSString stringWithUTF8String: dn]);
dn_size = sizeof(dn)-1;
gnutls_x509_crt_get_issuer_dn(cert, dn, &dn_size);
dn[dn_size] = '\0';
ASSIGN(issuer, [NSString stringWithUTF8String: dn]);
}
str = [opts objectForKey: GSTLSRemoteHosts];
if (nil == str)
{
names = nil;
}
else
{
/* The string is a comma separated list of permitted host names.
*/
names = [str componentsSeparatedByString: @","];
}
if (nil != names)
{
NSEnumerator *enumerator = [names objectEnumerator];
BOOL found = NO;
NSString *name;
while (nil != (name = [enumerator nextObject]))
{
if (0 == gnutls_x509_crt_check_hostname(cert, [name UTF8String]))
{
found = YES;
break;
}
}
if (NO == found)
{
str = [NSString stringWithFormat:
@"TLS verification: certificate's owner does not match '%@'",
names];
ASSIGN(problem, str);
gnutls_x509_crt_deinit(cert);
if (YES == debug) NSLog(@"%@ %@", self, problem);
return GNUTLS_E_CERTIFICATE_ERROR;
}
}
gnutls_x509_crt_deinit(cert);
return 0; // Verified
}
@end
#endif
|