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
|
/*
* gensio - A library for abstracting stream I/O
* Copyright (C) 2019 Corey Minyard <minyard@acm.org>
*
* SPDX-License-Identifier: LGPL-2.1-only
*/
#include "config.h"
#include <gensio/gensio_class.h>
#include "gensio_filter_certauth.h"
#include "gensio_filter_ssl.h"
struct gensio_certauth_filter_data {
struct gensio_os_funcs *o;
bool is_client;
char *CAfilepath;
char *keyfile;
char *certfile;
char *username;
char *password;
char *service;
bool allow_authfail;
bool use_child_auth;
bool enable_password;
/*
* The following is only used for testing. so certauth can be run
* over stdio for fuzz testing. Do not document.
*/
bool allow_unencrypted;
};
#if HAVE_OPENSSL
#include <assert.h>
#include <string.h>
#include <openssl/x509.h>
#include <openssl/x509_vfy.h>
#include <openssl/bio.h>
#include <openssl/pem.h>
#include <openssl/rand.h>
#include <openssl/err.h>
/* In gensio_filter_ssl.c, semi-private. */
int gensio_cert_get_name(X509 *cert, char *data, gensiods *datalen);
int gensio_cert_to_buf(X509 *cert, char *buf, gensiods *datalen);
int gensio_cert_fingerprint(X509 *cert, char *buf, gensiods *buflen);
#if OPENSSL_VERSION_NUMBER < 0x10100000L
#define X509_up_ref(x) CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509)
static EVP_MD_CTX *EVP_MD_CTX_new(void)
{
EVP_MD_CTX *c = OPENSSL_malloc(sizeof(*c));
if (c)
memset(c, 0, sizeof(*c));
return c;
}
static void EVP_MD_CTX_free(EVP_MD_CTX *c)
{
OPENSSL_free(c);
}
#endif
#define GENSIO_CERTAUTH_DATA_SIZE 2048
#define GENSIO_CERTAUTH_CHALLENGE_SIZE 32
#define GENSIO_CERTAUTH_VERSION 1
/*
* Passwords are always sent in this size buffer to keep an attacker
* from getting the actual password length.
*/
#define GENSIO_CERTAUTH_PASSWORD_LEN 100
/*
* A message consists of the following:
*
* <message number> [ <element number> <element length> <element data>
* [ <element number> ...] <end element>
*
* The message number is the same as the states below, it is one byte
* long. Message elements may come in any order. The element number
* is one byte and the element length is a two byte network order
* unsigned integer, giving the maximum element data length of 65535
* bytes.
*/
/*
* State machines for both the client and the server, also message
* numbers (except for CLIENT_START).
*/
enum certauth_state {
/*
* Client first sends the hello (containing the version, username,
* and optional service and options) and goes into SERVERHELLO.
*/
CERTAUTH_CLIENT_START = 0,
/*
* Server waits for CLIENTHELLO.
*
* The app is called to check the username. If the app says
* verification is done, send a SERVERDONE with error result
* from the app.
*
* If apps says to continue verification, send the SERVERHELLO
* (containing the version, random challenge, and optional
* options) and goes into CHALLENGE_RESPONSE.
*/
CERTAUTH_CLIENTHELLO = 1,
/*
* Client waits for SERVERHELLO and uses the random challenge to
* generate a challenge response and sends challenge response
* (containing certificate and challenge response) and goes into
* PASSWORD_REQUEST.
*
* Client may also receive a SERVERDONE in this state if the
* authorization is rejected or authorized on username alone.
*/
CERTAUTH_SERVERHELLO = 2,
/*
* Server receives the challenge response verifies the reponse and
* the certificate against the CA.
*
* The app is called before the verification so it can do things
* based on certificate data. If the app says
* verification is done, send a SERVERDONE with error result
* from the app.
*
* Otherwise the certificate is verified and the challenge
* response is checked. If the certificate verifies but the
* challenge response fails, fail the connection. If both pass
* verification, send a SERVERDONE giving the result and go into
* passthrough mode. If the certificate does not verify, send
* a PASSWORD_REQUEST (no data) and go into PASSWORD mode.
*/
CERTAUTH_CHALLENGE_RESPONSE = 3,
/*
* Client waits for PASSWORD_REQUEST. One byte, after the request
* tells whether to actually send the password (1) or send a dummy (2).
*
* Client may also receive a SERVERDONE in this state if the
* authorization is rejected or authorized by the certificate.
*/
CERTAUTH_PASSWORD_REQUEST = 4,
/*
* Server waits for a password. When received. the application is
* notified of the password. Send a SERVERDONE with error result
* from the app.
*/
CERTAUTH_PASSWORD = 5,
/*
* Client waits for SERVERDONE and goes into passthrough mode if
* successful, contains the result.
*/
CERTAUTH_SERVERDONE = 6,
/*
* We are done, just waiting for the data to be written from the
* buffer before reporting so.
*/
CERTAUTH_WAIT_WRITE_DONE = 107,
/*
* Just pass all the data through.
*/
CERTAUTH_PASSTHROUGH = 108,
/*
* Something went wrong, abort.
*/
CERTAUTH_ERR = 109
};
#define CERTAUTH_STATE_MAX CERTAUTH_SERVERDONE
/*
* Various message components.
*
* The contents are given for each one.
*/
enum certauth_elements {
/*
* 100 2 <2 byte version number>
*/
CERTAUTH_VERSION = 100,
/*
* 101 <n> <n byte string>
*/
CERTAUTH_USERNAME = 101,
/*
* Currently not used.
*/
CERTAUTH_OPTIONS = 102,
/*
* 103 32 <32 bytes of random data>
*/
CERTAUTH_CHALLENGE_DATA = 103,
/*
* 104 <n> <signature of random data plus service id signed by the
* client private key>
*/
CERTAUTH_CHALLENGE_RSP = 104,
/*
* 105 <n> <X509 certificate in PEM format>
*/
CERTAUTH_CERTIFICATE = 105,
/*
* 106 2 1|2
*
* The challenge response is verified, 1 is for success, 2 is for
* failure.
*/
CERTAUTH_RESULT = 106,
/*
* 107 <n> <n-byte string with service name>
*
* The service is used to tell the server what service the client
* wishes to run. It is optional and may be ignored.
*/
CERTAUTH_SERVICE = 107,
/*
* 108 100 <100-byte string with password>
*
* The service is used to transfer a password.
*/
CERTAUTH_PASSWORD_DATA = 108,
/* 109 n <n zeros>
*
* Dummy data to mask the fact that we are not sending certs or
* passwords.
*/
CERTAUTH_DUMMY_DATA = 109,
/*
* 110 <n>
* What password data we are asking for.
*/
CERTAUTH_PASSWORD_TYPE = 110,
/*
* 200
*
* This is the last thing in the message.
*/
CERTAUTH_END = 200
};
#define CERTAUTH_MIN_ELEMENT CERTAUTH_VERSION
#define CERTAUTH_MAX_ELEMENT CERTAUTH_PASSWORD_TYPE
#define CERTAUTH_RESULT_SUCCESS 1
#define CERTAUTH_RESULT_FAILURE 2
#define CERTAUTH_RESULT_ERR 3
#define CERTAUTH_PASSWORD_TYPE_REQ 1
#define CERTAUTH_PASSWORD_TYPE_DUMMY 2
struct certauth_filter {
struct gensio_filter *filter;
struct gensio_os_funcs *o;
bool is_client;
enum certauth_state state;
struct gensio_lock *lock;
/*
* If we get an error while reading, hold it here until the try
* connect is called.
*/
int pending_err;
/* Version number from the remote end. */
unsigned int version;
/* Result from the server or local verification. */
unsigned int result;
/* Result from the response check. */
unsigned int response_result;
/* Certificate verification result, server only. */
bool verified;
/* Use authenticated from the child gensio to skip this layer. */
bool use_child_auth;
/* Enable password authentication. */
bool enable_password;
char *username;
size_t username_len;
unsigned int password_req_val;
char *password;
size_t password_len;
char *service;
size_t service_len;
unsigned char *challenge_data;
gensiods challenge_data_size;
X509 *cert;
STACK_OF(X509) *sk_ca;
EVP_PKEY *pkey;
X509_STORE *verify_store;
bool allow_authfail;
BUF_MEM cert_buf_mem;
BIO *cert_bio;
const EVP_MD *rsa_md5;
unsigned char *read_buf;
gensiods read_buf_len;
gensiods max_read_size;
unsigned char *write_buf;
gensiods write_buf_len;
gensiods write_buf_pos;
gensiods max_write_size;
/*
* Processing for incoming messages.
*/
unsigned char curr_msg_type;
unsigned char curr_elem;
unsigned int curr_elem_len;
bool curr_elem_len_b1;
bool curr_elem_len_b2;
bool got_msg;
};
#define filter_to_certauth(v) ((struct certauth_filter *) \
gensio_filter_get_user_data(v))
static void
gca_vlog(struct certauth_filter *f, enum gensio_log_levels l,
bool do_ssl_err, char *fmt, va_list ap)
{
if (do_ssl_err) {
char buf[256], buf2[200];
unsigned long ssl_err = ERR_get_error();
if (!ssl_err)
goto no_ssl_err;
ERR_error_string_n(ssl_err, buf2, sizeof(buf2));
snprintf(buf, sizeof(buf), "certauth: %s: %s", fmt, buf2);
gensio_vlog(f->o, l, buf, ap);
} else {
no_ssl_err:
gensio_vlog(f->o, l, fmt, ap);
}
}
static void
gca_log_info(struct certauth_filter *f, char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
gca_vlog(f, GENSIO_LOG_INFO, false, fmt, ap);
va_end(ap);
}
static void
gca_log_err(struct certauth_filter *f, char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
gca_vlog(f, GENSIO_LOG_ERR, false, fmt, ap);
va_end(ap);
}
static void
gca_logs_info(struct certauth_filter *f, char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
gca_vlog(f, GENSIO_LOG_INFO, true, fmt, ap);
va_end(ap);
}
static void
gca_logs_err(struct certauth_filter *f, char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
gca_vlog(f, GENSIO_LOG_ERR, true, fmt, ap);
va_end(ap);
}
static void
certauth_lock(struct certauth_filter *sfilter)
{
sfilter->o->lock(sfilter->lock);
}
static void
certauth_unlock(struct certauth_filter *sfilter)
{
sfilter->o->unlock(sfilter->lock);
}
static void
certauth_set_callbacks(struct gensio_filter *filter,
gensio_filter_cb cb, void *cb_data)
{
/* We don't currently use callbacks. */
}
static bool
certauth_ul_read_pending(struct gensio_filter *filter)
{
return false; /* We never have data pending to the upper layer. */
}
static bool
certauth_ll_write_pending(struct gensio_filter *filter)
{
struct certauth_filter *sfilter = filter_to_certauth(filter);
return sfilter->write_buf_len > 0;
}
static bool
certauth_ll_read_needed(struct gensio_filter *filter)
{
struct certauth_filter *sfilter = filter_to_certauth(filter);
/*
* Turn off read when we have a message to process.
*/
return !sfilter->got_msg;
}
static int
certauth_check_open_done(struct gensio_filter *filter, struct gensio *io)
{
struct certauth_filter *sfilter = filter_to_certauth(filter);
int rv = 0;
certauth_lock(sfilter);
if (!sfilter->result)
sfilter->result = CERTAUTH_RESULT_FAILURE;
if (sfilter->result == CERTAUTH_RESULT_SUCCESS)
gensio_set_is_authenticated(io, true);
else if (sfilter->result == CERTAUTH_RESULT_ERR)
rv = GE_AUTHREJECT;
else if (sfilter->is_client || !sfilter->allow_authfail)
rv = GE_AUTHREJECT;
certauth_unlock(sfilter);
return rv;
}
static void
certauth_write(struct certauth_filter *sfilter, void *data, unsigned int len)
{
if (len + sfilter->write_buf_len > sfilter->max_write_size) {
gca_log_err(sfilter, "Unable to write data to network");
sfilter->pending_err = GE_TOOBIG;
return;
}
memcpy(sfilter->write_buf + sfilter->write_buf_len, data, len);
sfilter->write_buf_len += len;
}
static void
certauth_write_zeros(struct certauth_filter *sfilter, unsigned int len)
{
if (len + sfilter->write_buf_len > sfilter->max_write_size) {
gca_log_err(sfilter, "Unable to write data to network");
sfilter->pending_err = GE_TOOBIG;
return;
}
memset(sfilter->write_buf + sfilter->write_buf_len, 0, len);
sfilter->write_buf_len += len;
}
static void
certauth_u16_to_buf(unsigned char *buf, unsigned int v)
{
buf[0] = (v >> 8) & 0xff;
buf[1] = v & 0xff;
}
static unsigned int
certauth_buf_to_u16(unsigned char *buf)
{
return (((unsigned int) buf[0]) << 8) | buf[1];
}
static void
certauth_write_byte(struct certauth_filter *sfilter, unsigned char b)
{
certauth_write(sfilter, &b, 1);
}
static void
certauth_write_u16(struct certauth_filter *sfilter, unsigned int v)
{
unsigned char d[2];
certauth_u16_to_buf(d, v);
certauth_write(sfilter, d, 2);
}
static gensiods
certauth_writeleft(struct certauth_filter *sfilter)
{
return sfilter->max_write_size - sfilter->write_buf_len;
}
static void *
certauth_writepos(struct certauth_filter *sfilter)
{
return sfilter->write_buf + sfilter->write_buf_len;
}
static int
certauth_verify_cert(struct certauth_filter *sfilter)
{
X509_STORE_CTX *cert_store_ctx = NULL;
int rv = 0, verify_err;
const char *auxdata[] = { NULL, NULL };
cert_store_ctx = X509_STORE_CTX_new();
if (!cert_store_ctx) {
rv = GE_NOMEM;
goto out_err;
}
if (!X509_STORE_CTX_init(cert_store_ctx, sfilter->verify_store,
sfilter->cert, sfilter->sk_ca)) {
rv = GE_NOMEM;
goto out_err;
}
verify_err = X509_verify_cert(cert_store_ctx);
if (verify_err <= 0) {
verify_err = X509_STORE_CTX_get_error(cert_store_ctx);
if (verify_err == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY ||
verify_err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
rv = GE_CERTNOTFOUND;
else if (verify_err == X509_V_ERR_CERT_REVOKED)
rv = GE_CERTREVOKED;
else if (verify_err == X509_V_ERR_CERT_HAS_EXPIRED ||
verify_err == X509_V_ERR_CRL_HAS_EXPIRED)
rv = GE_CERTEXPIRED;
else
rv = GE_CERTINVALID;
} else {
verify_err = X509_V_OK;
}
certauth_unlock(sfilter);
if (rv)
auxdata[0] = X509_verify_cert_error_string(verify_err);
rv = gensio_filter_do_event(sfilter->filter, GENSIO_EVENT_POSTCERT_VERIFY,
rv, NULL, NULL, auxdata);
certauth_lock(sfilter);
if (rv == GE_NOTSUP) {
if (verify_err != X509_V_OK) {
gca_logs_info(sfilter,
"Remote peer certificate verify failed: %s",
X509_verify_cert_error_string(verify_err));
rv = GE_NOTSUP;
} else {
rv = 0;
}
}
if (rv == 0)
sfilter->verified = true;
out_err:
if (cert_store_ctx)
X509_STORE_CTX_free(cert_store_ctx);
return rv;
}
static int
certauth_add_cert(struct certauth_filter *sfilter)
{
unsigned int lenpos;
certauth_write_byte(sfilter, CERTAUTH_CERTIFICATE);
lenpos = sfilter->write_buf_len;
sfilter->write_buf_len += 2;
sfilter->cert_buf_mem.length = 0;
sfilter->cert_buf_mem.data = certauth_writepos(sfilter);
sfilter->cert_buf_mem.max = certauth_writeleft(sfilter);
BIO_set_mem_buf(sfilter->cert_bio, &sfilter->cert_buf_mem, BIO_NOCLOSE);
BIO_set_flags(sfilter->cert_bio, 0);
if (PEM_write_bio_X509(sfilter->cert_bio, sfilter->cert) == 0) {
gca_logs_err(sfilter, "Failure writing cert to network");
return GE_TOOBIG;
}
sfilter->write_buf_len += sfilter->cert_buf_mem.length;
certauth_u16_to_buf(sfilter->write_buf + lenpos,
sfilter->cert_buf_mem.length);
return 0;
}
static int
certauth_get_cert(struct certauth_filter *sfilter)
{
sfilter->cert_buf_mem.length = sfilter->read_buf_len;
sfilter->cert_buf_mem.data = (char *) sfilter->read_buf;
sfilter->cert_buf_mem.max = sfilter->read_buf_len;
BIO_set_mem_buf(sfilter->cert_bio, &sfilter->cert_buf_mem,
BIO_NOCLOSE);
BIO_set_flags(sfilter->cert_bio, BIO_FLAGS_MEM_RDONLY);
sfilter->cert = PEM_read_bio_X509(sfilter->cert_bio, NULL, NULL, NULL);
if (!sfilter->cert) {
gca_logs_err(sfilter, "Failure reading cert from network");
return GE_NOCERT;
}
sfilter->write_buf_len += sfilter->cert_buf_mem.length;
sfilter->sk_ca = sk_X509_new_null();
if (!sfilter->sk_ca) {
gca_log_err(sfilter, "Failure allocating CA stack");
return GE_NOMEM;
}
if (!sk_X509_push(sfilter->sk_ca, sfilter->cert)) {
gca_log_err(sfilter, "Failure pushing to CA stack");
return GE_NOMEM;
}
/* cert is in the stack and held by the user. */
X509_up_ref(sfilter->cert);
return 0;
}
static int
certauth_add_challenge_rsp(struct certauth_filter *sfilter)
{
EVP_MD_CTX *sign_ctx;
unsigned int lenpos, len;
int rv = 0;
certauth_write_byte(sfilter, CERTAUTH_CHALLENGE_RSP);
lenpos = sfilter->write_buf_len;
sfilter->write_buf_len += 2;
if (certauth_writeleft(sfilter) < EVP_PKEY_size(sfilter->pkey)) {
gca_log_err(sfilter, "Key too large to fit in the data");
return GE_TOOBIG;
}
sign_ctx = EVP_MD_CTX_new();
if (!sign_ctx) {
gca_log_err(sfilter, "Unable to allocate signature context");
return GE_NOMEM;
}
if (!EVP_SignInit(sign_ctx, sfilter->rsa_md5)) {
gca_logs_err(sfilter, "Signature init failed");
goto out_nomem;
}
if (!EVP_SignUpdate(sign_ctx, sfilter->challenge_data,
sfilter->challenge_data_size)) {
gca_logs_err(sfilter, "Signature update failed");
goto out_nomem;
}
if (!EVP_SignUpdate(sign_ctx, sfilter->service, sfilter->service_len)) {
gca_logs_err(sfilter, "Signature update (service) failed");
goto out_nomem;
}
if (!EVP_SignFinal(sign_ctx, certauth_writepos(sfilter), &len,
sfilter->pkey)) {
gca_logs_err(sfilter, "Signature final failed");
goto out_nomem;
}
sfilter->write_buf_len += len;
certauth_u16_to_buf(sfilter->write_buf + lenpos, len);
out:
EVP_MD_CTX_free(sign_ctx);
return rv;
out_nomem:
rv = GE_NOMEM;
goto out;
}
static int
certauth_check_challenge(struct certauth_filter *sfilter)
{
EVP_MD_CTX *sign_ctx;
int rv = 0;
EVP_PKEY *pkey;
sign_ctx = EVP_MD_CTX_new();
if (!sign_ctx) {
gca_log_err(sfilter, "Unable to allocate verify context");
return GE_NOMEM;
}
if (!EVP_VerifyInit(sign_ctx, sfilter->rsa_md5)) {
gca_logs_err(sfilter, "Verify init failed");
goto out_nomem;
}
if (!EVP_VerifyUpdate(sign_ctx, sfilter->challenge_data,
sfilter->challenge_data_size)) {
gca_logs_err(sfilter, "Verify update failed");
goto out_nomem;
}
if (!EVP_VerifyUpdate(sign_ctx, sfilter->service, sfilter->service_len)) {
gca_logs_err(sfilter, "Verify update (service) failed");
goto out_nomem;
}
pkey = X509_get_pubkey(sfilter->cert);
if (!pkey) {
gca_logs_err(sfilter, "Getting public key failed");
goto out_nomem;
}
rv = EVP_VerifyFinal(sign_ctx, sfilter->read_buf, sfilter->read_buf_len,
pkey);
EVP_PKEY_free(pkey);
if (rv < 0) {
gca_logs_err(sfilter, "Verify final failed");
goto out_nomem;
}
if (rv) {
sfilter->response_result = CERTAUTH_RESULT_SUCCESS;
} else {
sfilter->response_result = CERTAUTH_RESULT_FAILURE;
gca_logs_info(sfilter, "Challenge verify failed");
}
rv = 0;
out:
EVP_MD_CTX_free(sign_ctx);
return rv;
out_nomem:
rv = GE_NOMEM;
goto out;
}
static void
certauth_add_dummy(struct certauth_filter *sfilter, unsigned int len)
{
certauth_write_byte(sfilter, CERTAUTH_DUMMY_DATA);
certauth_write_u16(sfilter, len);
certauth_write_zeros(sfilter, len);
}
static void
certauth_send_server_done(struct certauth_filter *sfilter)
{
int result = CERTAUTH_RESULT_SUCCESS;
if (!sfilter->result)
sfilter->result = CERTAUTH_RESULT_FAILURE;
if (!sfilter->allow_authfail)
result = sfilter->result;
if (sfilter->result == CERTAUTH_RESULT_ERR)
result = CERTAUTH_RESULT_FAILURE;
sfilter->write_buf_len = 0;
certauth_write_byte(sfilter, CERTAUTH_SERVERDONE);
certauth_write_byte(sfilter, CERTAUTH_RESULT);
certauth_write_u16(sfilter, 2);
certauth_write_u16(sfilter, result);
certauth_write_byte(sfilter, CERTAUTH_END);
}
static int
certauth_try_connect(struct gensio_filter *filter, gensio_time *timeout)
{
struct certauth_filter *sfilter = filter_to_certauth(filter);
struct gensio *io;
bool password_requested = false;
int err, rv;
certauth_lock(sfilter);
if (sfilter->pending_err)
goto out_finish;
if (!sfilter->got_msg)
goto out_inprogress;
switch (sfilter->state) {
case CERTAUTH_CLIENT_START:
sfilter->write_buf_len = 0;
certauth_write_byte(sfilter, CERTAUTH_CLIENTHELLO);
certauth_write_byte(sfilter, CERTAUTH_VERSION);
certauth_write_u16(sfilter, 2);
certauth_write_u16(sfilter, GENSIO_CERTAUTH_VERSION);
if (sfilter->username && sfilter->username_len) {
certauth_write_byte(sfilter, CERTAUTH_USERNAME);
certauth_write_u16(sfilter, sfilter->username_len);
certauth_write(sfilter, sfilter->username, sfilter->username_len);
}
if (sfilter->service && sfilter->service_len) {
certauth_write_byte(sfilter, CERTAUTH_SERVICE);
certauth_write_u16(sfilter, sfilter->service_len);
certauth_write(sfilter, sfilter->service, sfilter->service_len);
}
certauth_write_byte(sfilter, CERTAUTH_END);
sfilter->state = CERTAUTH_SERVERHELLO;
break;
case CERTAUTH_CLIENTHELLO:
if (!sfilter->version) {
/* Remote end didn't send version. */
gca_log_err(sfilter,
"Remote client didn't send version");
sfilter->pending_err = GE_DATAMISSING;
break;
}
io = gensio_filter_get_gensio(filter);
if (sfilter->use_child_auth) {
if (gensio_is_authenticated(io)) {
/*
* A lower layer has already authenticated, just skip this.
*/
gca_log_info(sfilter, "Using lower layer authentication");
sfilter->result = CERTAUTH_RESULT_SUCCESS;
/* Go ahead and go through the motions. */
}
} else {
/* Override child setting. */
gensio_set_is_authenticated(io, false);
}
certauth_unlock(sfilter);
err = gensio_filter_do_event(sfilter->filter,
GENSIO_EVENT_AUTH_BEGIN, 0,
NULL, NULL, NULL);
certauth_lock(sfilter);
if (!err)
/*
* Note that we go ahead and do the rest of the messages
* even though they may fail, because otherwise we are
* broadcasting to the world that we have a login with
* no credentials.
*/
sfilter->result = CERTAUTH_RESULT_SUCCESS;
else if (err == GE_AUTHREJECT) {
sfilter->result = CERTAUTH_RESULT_ERR;
gca_log_err(sfilter, "auth begin rejected connection");
} else if (err != GE_NOTSUP) {
gca_log_err(sfilter, "Error from application at auth begin: %s",
gensio_err_to_str(err));
sfilter->pending_err = err;
goto finish_result;
}
sfilter->write_buf_len = 0;
certauth_write_byte(sfilter, CERTAUTH_SERVERHELLO);
certauth_write_byte(sfilter, CERTAUTH_VERSION);
certauth_write_u16(sfilter, 2);
certauth_write_u16(sfilter, GENSIO_CERTAUTH_VERSION);
certauth_write_byte(sfilter, CERTAUTH_CHALLENGE_DATA);
certauth_write_u16(sfilter, sfilter->challenge_data_size);
if (!RAND_bytes(sfilter->challenge_data,
sfilter->challenge_data_size)) {
gca_log_err(sfilter, "Unable to get random data");
sfilter->pending_err = GE_IOERR;
break;
}
certauth_write(sfilter, sfilter->challenge_data,
sfilter->challenge_data_size);
certauth_write_byte(sfilter, CERTAUTH_END);
sfilter->state = CERTAUTH_CHALLENGE_RESPONSE;
break;
case CERTAUTH_SERVERHELLO:
if (sfilter->result)
/* We got a server done with result, just go on. */
goto handle_server_done;
if (!sfilter->challenge_data || !sfilter->version) {
gca_log_err(sfilter,
"Remote server didn't send challenge data or version");
sfilter->pending_err = GE_DATAMISSING;
break;
}
sfilter->write_buf_len = 0;
certauth_write_byte(sfilter, CERTAUTH_CHALLENGE_RESPONSE);
if (sfilter->cert) {
sfilter->pending_err = certauth_add_cert(sfilter);
if (sfilter->pending_err)
goto finish_result;
} else {
/* Mask the fact we are not sending a cert. */
certauth_add_dummy(sfilter, 1265);
}
if (sfilter->pkey) {
sfilter->pending_err = certauth_add_challenge_rsp(sfilter);
if (sfilter->pending_err)
goto finish_result;
} else {
/* Mask the fact we are not sending a response. */
certauth_add_dummy(sfilter, 256);
}
certauth_write_byte(sfilter, CERTAUTH_END);
sfilter->state = CERTAUTH_PASSWORD_REQUEST;
break;
case CERTAUTH_CHALLENGE_RESPONSE:
if (!sfilter->cert || !sfilter->response_result) {
/*
* Remote end didn't send certificate and/or challenge
* response, try password.
*/
goto try_password;
}
if (!!sfilter->cert != !!sfilter->response_result) {
gca_log_err(sfilter, "Remote end did not send cert and response");
sfilter->pending_err = GE_PROTOERR;
goto finish_result;
}
if (!sfilter->result) {
certauth_unlock(sfilter);
err = gensio_filter_do_event(sfilter->filter,
GENSIO_EVENT_PRECERT_VERIFY, 0,
NULL, NULL, NULL);
certauth_lock(sfilter);
if (!err) {
sfilter->result = CERTAUTH_RESULT_SUCCESS;
} else if (err == GE_AUTHREJECT) {
gca_log_err(sfilter, "precert verify rejected connection");
sfilter->result = CERTAUTH_RESULT_ERR;
} else if (err != GE_NOTSUP) {
gca_log_err(sfilter, "Error from application at precert: %s",
gensio_err_to_str(err));
sfilter->pending_err = err;
goto finish_result;
}
}
err = certauth_verify_cert(sfilter);
if (!sfilter->result) {
if (err == GE_AUTHREJECT) {
gca_log_err(sfilter, "precert verify rejected connection");
sfilter->result = CERTAUTH_RESULT_ERR;
} else if (err && err != GE_NOTSUP) {
gca_log_err(sfilter, "Error from application at precert: %s",
gensio_err_to_str(err));
sfilter->pending_err = err;
goto finish_result;
}
if (sfilter->verified &&
sfilter->response_result == CERTAUTH_RESULT_SUCCESS) {
sfilter->result = CERTAUTH_RESULT_SUCCESS;
}
}
/*
* We may mark it as authenticated, but go through the
* password request so an attacker can't tell how the
* authentication was done.
*/
try_password:
sfilter->write_buf_len = 0;
certauth_write_byte(sfilter, CERTAUTH_PASSWORD_REQUEST);
certauth_write_byte(sfilter, CERTAUTH_PASSWORD_TYPE);
certauth_write_u16(sfilter, 2);
certauth_write_u16(sfilter,
sfilter->result || !sfilter->enable_password ?
CERTAUTH_PASSWORD_TYPE_DUMMY :
CERTAUTH_PASSWORD_TYPE_REQ);
certauth_write_byte(sfilter, CERTAUTH_END);
sfilter->state = CERTAUTH_PASSWORD;
break;
case CERTAUTH_PASSWORD_REQUEST:
if (!sfilter->password_req_val) {
gca_log_err(sfilter,
"Remote client didn't send request value");
sfilter->pending_err = GE_DATAMISSING;
goto finish_result;
}
sfilter->write_buf_len = 0;
if (sfilter->password_req_val != CERTAUTH_PASSWORD_TYPE_REQ) {
certauth_write_byte(sfilter, CERTAUTH_PASSWORD);
certauth_add_dummy(sfilter, sfilter->password_len);
goto password_done;
}
if (!sfilter->enable_password) {
certauth_write_byte(sfilter, CERTAUTH_PASSWORD);
certauth_write_byte(sfilter, CERTAUTH_PASSWORD_DATA);
certauth_write_u16(sfilter, sfilter->password_len);
if (sfilter->password_len)
certauth_write_zeros(sfilter, sfilter->password_len);
goto password_done;
}
if (!*sfilter->password) {
/* Empty password, ask the user. */
gensiods dummy_len = sfilter->password_len;
certauth_unlock(sfilter);
err = gensio_filter_do_event(sfilter->filter,
GENSIO_EVENT_REQUEST_PASSWORD, 0,
(unsigned char *) sfilter->password,
&dummy_len, NULL);
certauth_lock(sfilter);
if (err) {
memset(sfilter->password, 0, sfilter->password_len);
if (err != GE_NOTSUP) {
gca_log_err(sfilter, "Error fetching password: %s",
gensio_err_to_str(err));
sfilter->pending_err = err;
goto finish_result;
}
}
password_requested = true;
}
certauth_write_byte(sfilter, CERTAUTH_PASSWORD);
certauth_write_byte(sfilter, CERTAUTH_PASSWORD_DATA);
certauth_write_u16(sfilter, sfilter->password_len);
if (sfilter->password_len)
certauth_write(sfilter, sfilter->password, sfilter->password_len);
if (password_requested)
memset(sfilter->password, 0, sfilter->password_len);
password_done:
certauth_write_byte(sfilter, CERTAUTH_END);
sfilter->state = CERTAUTH_SERVERDONE;
break;
case CERTAUTH_PASSWORD:
if (sfilter->result)
/* Already verified, the rest was for show. */
goto finish_result;
if (sfilter->enable_password && !sfilter->password) {
/* Remote end didn't send a password and we requested one. */
gca_log_err(sfilter, "Remote client didn't send password");
sfilter->pending_err = GE_DATAMISSING;
goto finish_result;
}
if (!sfilter->password || !*sfilter->password)
goto finish_result;
certauth_unlock(sfilter);
err = gensio_filter_do_event(sfilter->filter,
GENSIO_EVENT_PASSWORD_VERIFY, 0,
(unsigned char *) sfilter->password,
NULL, NULL);
certauth_lock(sfilter);
memset(sfilter->password, 0, sfilter->password_len);
if (!err) {
sfilter->result = CERTAUTH_RESULT_SUCCESS;
} else if (err == GE_CERTINVALID) {
gca_log_err(sfilter, "Application rejected password");
} else if (err != GE_NOTSUP) {
gca_log_err(sfilter, "Error from application at password: %s",
gensio_err_to_str(err));
sfilter->pending_err = err;
}
finish_result:
certauth_send_server_done(sfilter);
if (sfilter->pending_err) {
sfilter->state = CERTAUTH_ERR;
goto out_finish;
}
sfilter->state = CERTAUTH_WAIT_WRITE_DONE;
/*
* Leave got_msg enabled so we won't read any more until we go
* to passthrough state.
*/
goto out_inprogress;
case CERTAUTH_SERVERDONE:
if (!sfilter->result) {
/* Remote end didn't send result. */
gca_log_err(sfilter, "Remote server didn't send result");
sfilter->pending_err = GE_DATAMISSING;
goto out_finish;
}
handle_server_done:
if (sfilter->result != CERTAUTH_RESULT_SUCCESS) {
sfilter->pending_err = GE_AUTHREJECT;
goto out_finish;
}
sfilter->state = CERTAUTH_PASSTHROUGH;
goto out_finish;
case CERTAUTH_WAIT_WRITE_DONE:
if (sfilter->write_buf_len == 0) {
sfilter->state = CERTAUTH_PASSTHROUGH;
goto out_finish;
}
goto out_inprogress;
default:
assert(false);
}
sfilter->got_msg = false;
out_inprogress:
rv = GE_INPROGRESS;
goto out;
out_finish:
rv = sfilter->pending_err;
out:
certauth_unlock(sfilter);
return rv;
}
static int
certauth_try_disconnect(struct gensio_filter *filter, gensio_time *timeout)
{
return 0;
}
static int
certauth_ul_write(struct gensio_filter *filter,
gensio_ul_filter_data_handler handler, void *cb_data,
gensiods *rcount,
const struct gensio_sg *sg, gensiods sglen,
const char *const *auxdata)
{
struct certauth_filter *sfilter = filter_to_certauth(filter);
int rv = 0;
certauth_lock(sfilter);
if (sg) {
if (sfilter->state != CERTAUTH_PASSTHROUGH || sfilter->pending_err)
rv = GE_NOTREADY;
else
rv = handler(cb_data, rcount, sg, sglen, auxdata);
if (rv)
goto out_unlock;
}
if (sfilter->write_buf_len) {
gensiods count = 0;
struct gensio_sg sg = { sfilter->write_buf + sfilter->write_buf_pos,
sfilter->write_buf_len - sfilter->write_buf_pos };
rv = handler(cb_data, &count, &sg, 1, auxdata);
if (rv)
goto out_unlock;
if (count + sfilter->write_buf_pos >= sfilter->write_buf_len) {
sfilter->write_buf_len = 0;
sfilter->write_buf_pos = 0;
} else {
sfilter->write_buf_pos += count;
}
}
out_unlock:
certauth_unlock(sfilter);
return rv;
}
static unsigned int
limited_strlen(const char *str, unsigned int max)
{
unsigned int i;
for (i = 0; i < max; i++) {
if (!*str)
return i;
}
return i;
}
static void
certauth_handle_new_element(struct certauth_filter *sfilter)
{
struct gensio_os_funcs *o = sfilter->o;
switch (sfilter->curr_elem) {
case CERTAUTH_VERSION:
if (sfilter->version) {
gca_log_err(sfilter, "Version received when already set");
sfilter->pending_err = GE_PROTOERR;
break;
}
if (sfilter->curr_elem_len != 2) {
gca_log_err(sfilter, "Version size not 2");
sfilter->pending_err = GE_PROTOERR;
} else {
sfilter->version = certauth_buf_to_u16(sfilter->read_buf);
if (!sfilter->version) {
gca_log_err(sfilter, "Version was zero");
sfilter->pending_err = GE_PROTOERR;
}
}
break;
case CERTAUTH_USERNAME:
if (sfilter->username) {
gca_log_err(sfilter, "Username received when already set");
sfilter->pending_err = GE_PROTOERR;
break;
}
sfilter->username = o->zalloc(0, sfilter->curr_elem_len + 1);
sfilter->username_len = sfilter->curr_elem_len;
if (!sfilter->username) {
gca_log_err(sfilter, "Unable to allocate memory for username");
sfilter->pending_err = GE_NOMEM;
} else {
memcpy(sfilter->username, sfilter->read_buf,
sfilter->curr_elem_len);
}
break;
case CERTAUTH_SERVICE:
if (sfilter->service) {
gca_log_err(sfilter, "Service received when already set");
sfilter->pending_err = GE_PROTOERR;
break;
}
sfilter->service = o->zalloc(0, sfilter->curr_elem_len + 1);
sfilter->service_len = sfilter->curr_elem_len;
if (!sfilter->service) {
gca_log_err(sfilter, "Unable to allocate memory for service");
sfilter->pending_err = GE_NOMEM;
} else {
memcpy(sfilter->service, sfilter->read_buf,
sfilter->curr_elem_len);
}
break;
case CERTAUTH_PASSWORD_TYPE:
if (sfilter->password_req_val) {
gca_log_err(sfilter, "password req received when already set");
sfilter->pending_err = GE_PROTOERR;
break;
}
if (sfilter->curr_elem_len != 2) {
gca_log_err(sfilter, "password req size not 2");
sfilter->pending_err = GE_PROTOERR;
} else {
sfilter->password_req_val = certauth_buf_to_u16(sfilter->read_buf);
if (!sfilter->password_req_val) {
gca_log_err(sfilter, "password req was zero");
sfilter->pending_err = GE_PROTOERR;
}
}
break;
case CERTAUTH_PASSWORD_DATA:
if (sfilter->password) {
gca_log_err(sfilter, "Password received when already set");
sfilter->pending_err = GE_PROTOERR;
break;
}
sfilter->password_len = limited_strlen((char *) sfilter->read_buf,
sfilter->curr_elem_len);
sfilter->password = o->zalloc(0, sfilter->password_len + 1);
if (!sfilter->password) {
gca_log_err(sfilter, "Unable to allocate memory for password");
sfilter->pending_err = GE_NOMEM;
} else {
memcpy(sfilter->password, sfilter->read_buf,
sfilter->password_len);
}
break;
case CERTAUTH_OPTIONS:
break;
case CERTAUTH_CHALLENGE_DATA:
if (sfilter->challenge_data) {
gca_log_err(sfilter, "Challenge data received when already set");
sfilter->pending_err = GE_PROTOERR;
break;
}
sfilter->challenge_data = o->zalloc(0, sfilter->curr_elem_len);
sfilter->challenge_data_size = sfilter->curr_elem_len;
if (!sfilter->challenge_data) {
gca_log_err(sfilter, "Unable to allocate memory for challenge");
sfilter->pending_err = GE_NOMEM;
} else {
memcpy(sfilter->challenge_data, sfilter->read_buf,
sfilter->curr_elem_len);
}
break;
case CERTAUTH_CHALLENGE_RSP:
if (sfilter->response_result) {
gca_log_err(sfilter,
"Challenge response received when already set");
sfilter->pending_err = GE_PROTOERR;
break;
}
sfilter->pending_err = certauth_check_challenge(sfilter);
break;
case CERTAUTH_CERTIFICATE:
if (sfilter->cert) {
gca_log_err(sfilter, "Certificate received when already set");
sfilter->pending_err = GE_PROTOERR;
break;
}
sfilter->pending_err = certauth_get_cert(sfilter);
break;
case CERTAUTH_RESULT:
if (sfilter->result) {
gca_log_err(sfilter, "Result received when already set");
sfilter->pending_err = GE_PROTOERR;
break;
}
if (sfilter->curr_elem_len != 2) {
gca_log_err(sfilter, "Result size not 2");
sfilter->pending_err = GE_PROTOERR;
} else {
sfilter->result = certauth_buf_to_u16(sfilter->read_buf);
if (!sfilter->result) {
gca_log_err(sfilter, "Result value was zero");
sfilter->pending_err = GE_PROTOERR;
}
}
break;
case CERTAUTH_DUMMY_DATA:
/* Just ignore it. */
break;
default:
break;
}
}
static int
certauth_ll_write(struct gensio_filter *filter,
gensio_ll_filter_data_handler handler, void *cb_data,
gensiods *rcount,
unsigned char *buf, gensiods buflen,
const char *const *auxdata)
{
struct certauth_filter *sfilter = filter_to_certauth(filter);
int err = 0;
unsigned char *obuf = buf;
gensiods elemleft;
if (buflen == 0)
goto out;
certauth_lock(sfilter);
if (sfilter->state == CERTAUTH_PASSTHROUGH) {
certauth_unlock(sfilter);
err = gensio_filter_do_event(sfilter->filter, GENSIO_EVENT_READ, 0,
buf, &buflen, auxdata);
if (rcount)
*rcount = buflen;
return err;
}
if (gensio_str_in_auxdata(auxdata, "oob")) {
/* Ignore oob data. */
if (rcount)
*rcount = buflen;
goto out_unlock;
}
if (sfilter->pending_err) {
if (rcount)
*rcount = buflen;
goto out_unlock;
}
if (!sfilter->curr_msg_type) {
sfilter->curr_msg_type = *buf;
buf++;
buflen--;
if (sfilter->curr_msg_type > CERTAUTH_STATE_MAX) {
gca_log_err(sfilter, "Invalid message type: %d",
sfilter->curr_msg_type);
sfilter->pending_err = GE_PROTOERR;
goto out_unlock;
}
sfilter->curr_elem = 0;
sfilter->curr_elem_len_b1 = false;
sfilter->curr_elem_len_b2 = false;
sfilter->read_buf_len = 0;
if (sfilter->is_client &&
sfilter->curr_msg_type == CERTAUTH_SERVERDONE) {
/* We allow server done in any state. */
sfilter->state = CERTAUTH_SERVERDONE;
} else if (sfilter->curr_msg_type != sfilter->state) {
gca_log_err(sfilter, "Expected message type %d, got %d",
sfilter->curr_msg_type, sfilter->state);
sfilter->pending_err = GE_PROTOERR;
goto out_unlock;
}
}
if (buflen == 0)
goto out_unlock;
restart:
if (!sfilter->curr_elem) {
sfilter->curr_elem = *buf;
buf++;
buflen--;
if (sfilter->curr_elem == CERTAUTH_END) {
sfilter->curr_msg_type = 0;
sfilter->got_msg = true;
goto out_unlock;
}
if (sfilter->curr_elem > CERTAUTH_MAX_ELEMENT ||
sfilter->curr_elem < CERTAUTH_MIN_ELEMENT) {
gca_log_err(sfilter, "Invalid message element: %d",
sfilter->curr_elem);
sfilter->pending_err = GE_PROTOERR;
goto out_unlock;
}
}
if (buflen == 0)
goto out_unlock;
if (!sfilter->curr_elem_len_b1) {
sfilter->curr_elem_len_b1 = true;
sfilter->curr_elem_len = ((unsigned int) (*buf)) << 8;
buf++;
buflen--;
}
if (buflen == 0)
goto out_unlock;
if (!sfilter->curr_elem_len_b2) {
sfilter->curr_elem_len_b2 = true;
sfilter->curr_elem_len |= *buf;
if (!sfilter->curr_elem_len ||
sfilter->curr_elem_len > sfilter->max_read_size) {
gca_log_err(sfilter, "Element type %d was too large: %d",
sfilter->curr_elem, sfilter->curr_elem_len);
sfilter->pending_err = GE_PROTOERR;
goto out_unlock;
}
buf++;
buflen--;
}
elemleft = sfilter->curr_elem_len - sfilter->read_buf_len;
if (buflen >= elemleft) {
memcpy(sfilter->read_buf + sfilter->read_buf_len, buf, elemleft);
buflen -= elemleft;
buf += elemleft;
sfilter->read_buf_len += elemleft;
certauth_handle_new_element(sfilter);
sfilter->curr_elem = 0;
sfilter->curr_elem_len_b1 = false;
sfilter->curr_elem_len_b2 = false;
sfilter->read_buf_len = 0;
goto restart;
} else {
memcpy(sfilter->read_buf + sfilter->read_buf_len, buf, buflen);
sfilter->read_buf_len += buflen;
buf += buflen;
buflen = 0;
}
out_unlock:
err = sfilter->pending_err;
certauth_unlock(sfilter);
out:
if (rcount)
*rcount = buf - obuf;
return err;
}
static int
certauth_setup(struct gensio_filter *filter)
{
struct certauth_filter *sfilter = filter_to_certauth(filter);
gensio_time tv_rand;
/* Make sure the random number generator is seeded. */
sfilter->o->get_monotonic_time(sfilter->o, &tv_rand);
tv_rand.secs += tv_rand.nsecs;
RAND_add(&tv_rand.secs, sizeof(tv_rand.secs), 0);
return 0;
}
static void
certauth_cleanup(struct gensio_filter *filter)
{
struct certauth_filter *sfilter = filter_to_certauth(filter);
if (sfilter->is_client) {
if (sfilter->challenge_data)
sfilter->o->free(sfilter->o, sfilter->challenge_data);
sfilter->challenge_data = NULL;
memset(sfilter->password, 0, sfilter->password_len);
} else {
if (sfilter->cert)
X509_free(sfilter->cert);
if (sfilter->sk_ca)
sk_X509_pop_free(sfilter->sk_ca, X509_free);
sfilter->cert = NULL;
sfilter->sk_ca = NULL;
if (sfilter->password)
memset(sfilter->password, 0, sfilter->password_len);
if (!sfilter->is_client && sfilter->password) {
sfilter->o->free(sfilter->o, sfilter->password);
sfilter->password = NULL;
sfilter->password_len = 0;
}
if (sfilter->username)
sfilter->o->free(sfilter->o, sfilter->username);
sfilter->username = NULL;
sfilter->username_len = 0;
if (sfilter->service)
sfilter->o->free(sfilter->o, sfilter->service);
sfilter->service = NULL;
sfilter->service_len = 0;
}
sfilter->pending_err = 0;
sfilter->password_req_val = 0;
sfilter->read_buf_len = 0;
sfilter->write_buf_len = 0;
sfilter->write_buf_pos = 0;
sfilter->version = 0;
sfilter->result = 0;
sfilter->response_result = 0;
sfilter->verified = false;
}
static void
sfilter_free(struct certauth_filter *sfilter)
{
if (sfilter->cert)
X509_free(sfilter->cert);
if (sfilter->sk_ca)
sk_X509_pop_free(sfilter->sk_ca, X509_free);
if (sfilter->cert_bio)
BIO_free(sfilter->cert_bio);
if (sfilter->lock)
sfilter->o->free_lock(sfilter->lock);
if (sfilter->read_buf) {
memset(sfilter->read_buf, 0, sfilter->max_read_size);
sfilter->o->free(sfilter->o, sfilter->read_buf);
}
if (sfilter->write_buf)
sfilter->o->free(sfilter->o, sfilter->write_buf);
if (sfilter->pkey)
EVP_PKEY_free(sfilter->pkey);
if (sfilter->password) {
memset(sfilter->password, 0, sfilter->password_len);
sfilter->o->free(sfilter->o, sfilter->password);
}
if (sfilter->username)
sfilter->o->free(sfilter->o, sfilter->username);
if (sfilter->service)
sfilter->o->free(sfilter->o, sfilter->service);
if (sfilter->challenge_data)
sfilter->o->free(sfilter->o, sfilter->challenge_data);
if (sfilter->filter)
gensio_filter_free_data(sfilter->filter);
if (sfilter->verify_store)
X509_STORE_free(sfilter->verify_store);
sfilter->o->free(sfilter->o, sfilter);
}
static void
certauth_free(struct gensio_filter *filter)
{
struct certauth_filter *sfilter = filter_to_certauth(filter);
sfilter_free(sfilter);
}
static int
certauth_filter_control(struct gensio_filter *filter, bool get, int op,
char *data, gensiods *datalen)
{
struct certauth_filter *sfilter = filter_to_certauth(filter);
X509_STORE *store;
char *CApath = NULL, *CAfile = NULL;
switch (op) {
case GENSIO_CONTROL_GET_PEER_CERT_NAME:
if (!get)
return GE_NOTSUP;
return gensio_cert_get_name(sfilter->cert, data, datalen);
case GENSIO_CONTROL_CERT:
if (!get)
return GE_NOTSUP;
if (!sfilter->cert)
return GE_NOTFOUND;
return gensio_cert_to_buf(sfilter->cert, data, datalen);
case GENSIO_CONTROL_USERNAME: {
int rv = 0;
certauth_lock(sfilter);
if (get) {
if (!sfilter->username) {
rv = GE_DATAMISSING;
goto out_username;
}
*datalen = snprintf(data, *datalen, "%s", sfilter->username);
} else {
char *newusername = NULL;
if (data) {
newusername = gensio_strdup(sfilter->o, data);
if (!newusername) {
rv = GE_NOMEM;
goto out_username;
}
}
if (sfilter->username)
sfilter->o->free(sfilter->o, sfilter->username);
sfilter->username = data;
}
out_username:
certauth_unlock(sfilter);
return rv;
}
case GENSIO_CONTROL_SERVICE:
if (get) {
gensiods to_copy;
if (!sfilter->service)
return GE_DATAMISSING;
to_copy = sfilter->service_len;
if (to_copy > *datalen)
to_copy = *datalen;
memcpy(data, sfilter->service, to_copy);
*datalen = sfilter->service_len;
} else {
char *new_service = sfilter->o->zalloc(sfilter->o, *datalen);
if (!new_service)
return GE_NOMEM;
memcpy(new_service, data, *datalen);
if (sfilter->service)
sfilter->o->free(sfilter->o, sfilter->service);
sfilter->service = new_service;
sfilter->service_len = *datalen;
}
return 0;
case GENSIO_CONTROL_CERT_AUTH:
if (get)
return GE_NOTSUP;
store = X509_STORE_new();
if (!store)
return GE_NOMEM;
if (data[strlen(data) - 1] == '/')
CApath = data;
else
CAfile = data;
if (!X509_STORE_load_locations(store, CAfile, CApath)) {
X509_STORE_free(store);
return GE_CERTNOTFOUND;
}
certauth_lock(sfilter);
if (sfilter->verify_store)
X509_STORE_free(sfilter->verify_store);
sfilter->verify_store = store;
certauth_unlock(sfilter);
return 0;
case GENSIO_CONTROL_CERT_FINGERPRINT:
if (!get)
return GE_NOTSUP;
if (!sfilter->cert)
return GE_NOTFOUND;
return gensio_cert_fingerprint(sfilter->cert, data, datalen);
default:
return GE_NOTSUP;
}
}
static
int gensio_certauth_filter_func(struct gensio_filter *filter, int op,
const void *func, void *data,
gensiods *count,
void *buf, const void *cbuf,
gensiods buflen,
const char *const *auxdata)
{
switch (op) {
case GENSIO_FILTER_FUNC_SET_CALLBACK:
certauth_set_callbacks(filter, func, data);
return 0;
case GENSIO_FILTER_FUNC_UL_READ_PENDING:
return certauth_ul_read_pending(filter);
case GENSIO_FILTER_FUNC_LL_WRITE_PENDING:
return certauth_ll_write_pending(filter);
case GENSIO_FILTER_FUNC_LL_READ_NEEDED:
return certauth_ll_read_needed(filter);
case GENSIO_FILTER_FUNC_CHECK_OPEN_DONE:
return certauth_check_open_done(filter, data);
case GENSIO_FILTER_FUNC_TRY_CONNECT:
return certauth_try_connect(filter, data);
case GENSIO_FILTER_FUNC_TRY_DISCONNECT:
return certauth_try_disconnect(filter, data);
case GENSIO_FILTER_FUNC_UL_WRITE_SG:
return certauth_ul_write(filter, func, data, count, cbuf, buflen, buf);
case GENSIO_FILTER_FUNC_LL_WRITE:
return certauth_ll_write(filter, func, data, count, buf, buflen, NULL);
case GENSIO_FILTER_FUNC_SETUP:
return certauth_setup(filter);
case GENSIO_FILTER_FUNC_CLEANUP:
certauth_cleanup(filter);
return 0;
case GENSIO_FILTER_FUNC_FREE:
certauth_free(filter);
return 0;
case GENSIO_FILTER_FUNC_CONTROL:
return certauth_filter_control(filter, *((bool *) cbuf), buflen, data,
count);
case GENSIO_FILTER_FUNC_TIMEOUT:
default:
return GE_NOTSUP;
}
}
static int
gensio_certauth_filter_raw_alloc(struct gensio_os_funcs *o,
bool is_client, X509_STORE *store,
X509 *cert, STACK_OF(X509) *sk_ca,
EVP_PKEY *pkey,
const char *username, const char *password,
const char *service,
bool allow_authfail, bool use_child_auth,
bool enable_password,
struct gensio_filter **rfilter)
{
struct certauth_filter *sfilter;
int rv;
sfilter = o->zalloc(o, sizeof(*sfilter));
if (!sfilter)
return GE_NOMEM;
sfilter->o = o;
sfilter->is_client = is_client;
sfilter->allow_authfail = allow_authfail;
sfilter->use_child_auth = use_child_auth;
sfilter->enable_password = enable_password,
sfilter->rsa_md5 = EVP_get_digestbyname("ssl3-md5");
if (!sfilter->rsa_md5) {
rv = GE_IOERR;
goto out_err;
}
if (is_client) {
/* Extra byte at the end so it's always nil terminated. */
sfilter->password = o->zalloc(o, GENSIO_CERTAUTH_PASSWORD_LEN + 1);
if (!sfilter->password) {
rv = GE_NOMEM;
goto out_err;
}
sfilter->password_len = GENSIO_CERTAUTH_PASSWORD_LEN;
if (password) {
size_t pwlen = strlen(password);
if (pwlen > GENSIO_CERTAUTH_PASSWORD_LEN) {
rv = GE_TOOBIG;
goto out_err;
}
strncpy(sfilter->password, password, GENSIO_CERTAUTH_PASSWORD_LEN);
}
}
sfilter->lock = o->alloc_lock(o);
if (!sfilter->lock)
goto out_nomem;
sfilter->cert_bio = BIO_new(BIO_s_mem());
if (!sfilter->cert_bio)
goto out_nomem;
if (username) {
sfilter->username = gensio_strdup(o, username);
if (!sfilter->username)
goto out_nomem;
sfilter->username_len = strlen(username);
}
if (service) {
sfilter->service = gensio_strdup(o, service);
if (!sfilter->service)
goto out_nomem;
sfilter->service_len = strlen(service);
}
if (is_client) {
sfilter->state = CERTAUTH_CLIENT_START;
sfilter->got_msg = true; /* Go ahead and run the state machine. */
} else {
sfilter->state = CERTAUTH_CLIENTHELLO;
sfilter->challenge_data = o->zalloc(o, GENSIO_CERTAUTH_CHALLENGE_SIZE);
if (!sfilter->challenge_data)
goto out_nomem;
sfilter->challenge_data_size = GENSIO_CERTAUTH_CHALLENGE_SIZE;
}
sfilter->read_buf = o->zalloc(o, GENSIO_CERTAUTH_DATA_SIZE);
if (!sfilter->read_buf)
goto out_nomem;
sfilter->max_read_size = GENSIO_CERTAUTH_DATA_SIZE;
sfilter->write_buf = o->zalloc(o, GENSIO_CERTAUTH_DATA_SIZE);
if (!sfilter->write_buf)
goto out_nomem;
sfilter->max_write_size = GENSIO_CERTAUTH_DATA_SIZE;
sfilter->filter = gensio_filter_alloc_data(o, gensio_certauth_filter_func,
sfilter);
if (!sfilter->filter)
goto out_nomem;
/* Don't set these until here so sfilter_free() doesn't free them on err. */
sfilter->cert = cert;
sfilter->sk_ca = sk_ca;
sfilter->pkey = pkey;
sfilter->verify_store = store;
*rfilter = sfilter->filter;
return 0;
out_nomem:
rv = GE_NOMEM;
out_err:
sfilter_free(sfilter);
return rv;
}
void
gensio_certauth_filter_config_free(struct gensio_certauth_filter_data *data)
{
struct gensio_os_funcs *o;
if (!data)
return;
o = data->o;
if (data->CAfilepath)
o->free(o, data->CAfilepath);
if (data->keyfile)
o->free(o, data->keyfile);
if (data->certfile)
o->free(o, data->certfile);
if (data->password) {
memset(data->password, 0, strlen(data->password));
o->free(o, data->password);
}
if (data->username)
o->free(o, data->username);
if (data->service)
o->free(o, data->service);
o->free(o, data);
}
int
gensio_certauth_filter_config(struct gensio_os_funcs *o,
const char * const args[],
bool default_is_client,
struct gensio_certauth_filter_data **rdata)
{
unsigned int i;
struct gensio_certauth_filter_data *data = o->zalloc(o, sizeof(*data));
int rv = GE_NOMEM, ival;
const char *str;
char *fstr;
if (!data)
return GE_NOMEM;
data->o = o;
data->is_client = default_is_client;
rv = gensio_get_default(o, "certauth", "allow-authfail", false,
GENSIO_DEFAULT_BOOL, NULL, &ival);
if (rv)
return rv;
data->allow_authfail = ival;
rv = gensio_get_default(o, "certauth", "use-child-auth", false,
GENSIO_DEFAULT_BOOL, NULL, &ival);
if (rv)
return rv;
data->use_child_auth = ival;
rv = gensio_get_default(o, "certauth", "enable-password", false,
GENSIO_DEFAULT_BOOL, NULL, &ival);
if (rv)
return rv;
data->enable_password = ival;
rv = gensio_get_default(o, "certauth", "mode", false,
GENSIO_DEFAULT_STR, &fstr, NULL);
if (rv) {
gensio_log(o, GENSIO_LOG_ERR,
"Failed getting certauth mode: %s",
gensio_err_to_str(rv));
return rv;
}
if (fstr) {
if (strcasecmp(fstr, "client") == 0)
data->is_client = true;
else if (strcasecmp(fstr, "server") == 0)
data->is_client = false;
else {
gensio_log(o, GENSIO_LOG_ERR,
"Unknown default certauth mode (%s), ignoring", str);
}
o->free(o, fstr);
}
rv = GE_NOMEM;
for (i = 0; args && args[i]; i++) {
if (gensio_check_keyvalue(args[i], "CA", &str) > 0) {
data->CAfilepath = gensio_strdup(o, str);
if (!data->CAfilepath)
goto out_err;
continue;
}
if (gensio_check_keyvalue(args[i], "key", &str) > 0) {
data->keyfile = gensio_strdup(o, str);
if (!data->keyfile)
goto out_err;
continue;
}
if (gensio_check_keyvalue(args[i], "cert", &str) > 0) {
data->certfile = gensio_strdup(o, str);
if (!data->certfile)
goto out_err;
continue;
}
if (gensio_check_keyvalue(args[i], "username", &str) > 0) {
data->username = gensio_strdup(o, str);
if (!data->username)
goto out_err;
continue;
}
if (gensio_check_keyvalue(args[i], "password", &str) > 0) {
data->password = gensio_strdup(o, str);
if (!data->password)
goto out_err;
continue;
}
if (gensio_check_keyvalue(args[i], "service", &str) > 0) {
data->service = gensio_strdup(o, str);
if (!data->service)
goto out_err;
continue;
}
if (gensio_check_keyboolv(args[i], "mode", "client", "server",
&data->is_client) > 0)
continue;
if (gensio_check_keybool(args[i], "allow-authfail",
&data->allow_authfail) > 0)
continue;
if (gensio_check_keybool(args[i], "use-child-auth",
&data->use_child_auth) > 0)
continue;
if (gensio_check_keybool(args[i], "enable-password",
&data->enable_password) > 0)
continue;
if (gensio_check_keybool(args[i], "allow-unencrypted",
&data->allow_unencrypted) > 0)
continue;
rv = GE_INVAL;
goto out_err;
}
if (!data->keyfile) {
rv = gensio_get_default(o, "certauth", "key", false, GENSIO_DEFAULT_STR,
&data->keyfile, NULL);
if (rv) {
gensio_log(o, GENSIO_LOG_ERR,
"Unable to get default key for certauth: %s",
gensio_err_to_str(rv));
goto out_err;
}
}
if (!data->certfile) {
rv = gensio_get_default(o, "certauth", "cert", false,
GENSIO_DEFAULT_STR, &data->certfile, NULL);
if (rv) {
gensio_log(o, GENSIO_LOG_ERR,
"Unable to get default cert for certauth: %s",
gensio_err_to_str(rv));
goto out_err;
}
}
if (!data->CAfilepath) {
rv = gensio_get_default(o, "certauth", "CA", false, GENSIO_DEFAULT_STR,
&data->CAfilepath, NULL);
if (rv) {
gensio_log(o, GENSIO_LOG_ERR,
"Unable to get default CA for certauth: %s",
gensio_err_to_str(rv));
goto out_err;
}
}
if (!data->username) {
rv = gensio_get_default(o, "certauth", "username", false,
GENSIO_DEFAULT_STR, &data->username, NULL);
if (rv) {
gensio_log(o, GENSIO_LOG_ERR,
"Unable to get default username for certauth: %s",
gensio_err_to_str(rv));
goto out_err;
}
}
if (!data->password) {
rv = gensio_get_default(o, "certauth", "password", false,
GENSIO_DEFAULT_STR, &data->password, NULL);
if (rv) {
gensio_log(o, GENSIO_LOG_ERR,
"Unable to get default password for certauth: %s",
gensio_err_to_str(rv));
goto out_err;
}
}
if (!data->service) {
gensio_get_default(o, "certauth", "service", false, GENSIO_DEFAULT_STR,
&data->service, NULL);
if (rv) {
gensio_log(o, GENSIO_LOG_ERR,
"Unable to get default service for certauth: %s",
gensio_err_to_str(rv));
goto out_err;
}
}
if (!data->keyfile && data->certfile) {
data->keyfile = gensio_strdup(o, data->certfile);
if (!data->keyfile) {
rv = GE_NOMEM;
goto out_err;
}
}
if (data->is_client) {
if (data->CAfilepath) {
rv = GE_INVAL;
goto out_err;
}
} else {
if (data->keyfile || data->username) {
rv = GE_INVAL;
goto out_err;
}
}
*rdata = data;
return 0;
out_err:
gensio_certauth_filter_config_free(data);
return rv;
}
bool gensio_certauth_filter_config_allow_unencrypted(
struct gensio_certauth_filter_data *data)
{
return data->allow_unencrypted;
}
static int
read_certificate_chain(const char *file, X509 **rcert, STACK_OF(X509) **rca)
{
BIO *in;
int rv = 0;
X509 *cert = NULL, *ca = NULL;
STACK_OF(X509) *sk_ca = NULL;
ERR_clear_error();
in = BIO_new(BIO_s_file());
if (!in)
return GE_NOMEM;
if (BIO_read_filename(in, file) <= 0) {
rv = GE_CERTNOTFOUND;
goto out_err;
}
cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
if (!cert) {
rv = GE_CERTINVALID;
goto out_err;
}
sk_ca = sk_X509_new_null();
if (!sk_ca) {
rv = GE_NOMEM;
goto out_err;
}
if (!sk_X509_push(sk_ca, cert)) {
rv = GE_NOMEM;
goto out_err;
}
X509_up_ref(cert);
while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
if (!sk_X509_push(sk_ca, ca)) {
X509_free(ca);
rv = GE_NOMEM;
goto out_err;
}
}
*rcert = cert;
*rca = sk_ca;
goto out;
out_err:
if (sk_ca)
sk_X509_pop_free(sk_ca, X509_free);
if (cert)
X509_free(cert);
out:
BIO_free(in);
return rv;
}
static int
read_private_key(const char *file, EVP_PKEY **rpkey)
{
BIO *in;
EVP_PKEY *pkey;
ERR_clear_error();
in = BIO_new(BIO_s_file());
if (!in)
return GE_NOMEM;
if (BIO_read_filename(in, file) <= 0) {
BIO_free(in);
return GE_KEYNOTFOUND;
}
pkey = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
BIO_free(in);
if (!pkey)
return GE_KEYINVALID;
*rpkey = pkey;
return 0;
}
int
gensio_certauth_filter_alloc(struct gensio_certauth_filter_data *data,
struct gensio_filter **rfilter)
{
struct gensio_os_funcs *o = data->o;
struct gensio_filter *filter;
X509_STORE *store = NULL;
X509 *cert = NULL;
EVP_PKEY *pkey = NULL;
STACK_OF(X509) *sk_ca = NULL;
int rv = GE_INVAL;
store = X509_STORE_new();
if (!store) {
rv = GE_NOMEM;
goto err;
}
if (data->CAfilepath && data->CAfilepath[0]) {
char *CAfile = NULL, *CApath = NULL;
if (data->CAfilepath[strlen(data->CAfilepath) - 1] == '/')
CApath = data->CAfilepath;
else
CAfile = data->CAfilepath;
if (!X509_STORE_load_locations(store, CAfile, CApath)) {
rv = GE_CERTNOTFOUND;
goto err;
}
}
if (data->certfile && data->certfile[0]) {
rv = read_certificate_chain(data->certfile, &cert, &sk_ca);
if (rv)
goto err;
rv = read_private_key(data->keyfile, &pkey);
if (rv)
goto err;
}
rv = gensio_certauth_filter_raw_alloc(o, data->is_client, store,
cert, sk_ca, pkey,
data->username, data->password,
data->service,
data->allow_authfail,
data->use_child_auth,
data->enable_password,
&filter);
if (rv)
goto err;
*rfilter = filter;
return 0;
err:
if (sk_ca)
sk_X509_pop_free(sk_ca, X509_free);
if (cert)
X509_free(cert);
if (pkey)
EVP_PKEY_free(pkey);
if (store)
X509_STORE_free(store);
return rv;
}
#else /* HAVE_OPENSSL */
int
gensio_certauth_filter_config(struct gensio_os_funcs *o,
const char * const args[],
bool default_is_client,
struct gensio_certauth_filter_data **rdata)
{
return GE_NOTSUP;
}
void
gensio_certauth_filter_config_free(struct gensio_certauth_filter_data *data)
{
}
int
gensio_certauth_filter_alloc(struct gensio_certauth_filter_data *data,
struct gensio_filter **rfilter)
{
return GE_NOTSUP;
}
#endif /* HAVE_OPENSSL */
|