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
|
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#ifndef BYO_CRYPTO
# include <aws/io/channel_bootstrap.h>
# include <aws/io/event_loop.h>
# include <aws/io/file_utils.h>
# include <aws/io/host_resolver.h>
# include <aws/io/logging.h>
# include <aws/io/socket.h>
# include <aws/io/tls_channel_handler.h>
# include <aws/common/clock.h>
# include <aws/common/condition_variable.h>
# include <aws/common/thread.h>
# include <aws/testing/aws_test_harness.h>
# include <aws/common/string.h>
# include <read_write_test_handler.h>
# include <statistics_handler_test.h>
# ifdef _WIN32
# define LOCAL_SOCK_TEST_PATTERN "\\\\.\\pipe\\testsock%llu_%d"
# else
# define LOCAL_SOCK_TEST_PATTERN "testsock%llu_%d.sock"
# endif
struct tls_test_args {
struct aws_allocator *allocator;
struct aws_mutex *mutex;
struct aws_condition_variable *condition_variable;
struct aws_tls_connection_options *tls_options;
struct aws_channel *channel;
struct aws_channel_handler *rw_handler;
struct aws_channel_slot *rw_slot;
struct aws_byte_buf negotiated_protocol;
struct aws_byte_buf server_name;
int last_error_code;
uint32_t tls_levels_negotiated;
uint32_t desired_tls_levels;
bool listener_destroyed;
bool error_invoked;
bool expects_error;
bool server;
bool shutdown_finished;
bool setup_callback_invoked;
bool creation_callback_invoked;
};
/* common structure for tls options */
struct tls_opt_tester {
struct aws_tls_ctx_options ctx_options;
struct aws_tls_ctx *ctx;
struct aws_tls_connection_options opt;
};
static int s_tls_server_opt_tester_init(
struct aws_allocator *allocator,
struct tls_opt_tester *tester,
const char *cert_path,
const char *pkey_path) {
# ifdef __APPLE__
struct aws_byte_cursor pwd_cur = aws_byte_cursor_from_c_str("1234");
ASSERT_SUCCESS(
aws_tls_ctx_options_init_server_pkcs12_from_path(&tester->ctx_options, allocator, "unittests.p12", &pwd_cur));
# else
ASSERT_SUCCESS(
aws_tls_ctx_options_init_default_server_from_path(&tester->ctx_options, allocator, cert_path, pkey_path));
ASSERT_SUCCESS(
aws_tls_ctx_options_override_default_trust_store_from_path(&tester->ctx_options, NULL, "ca_root.crt"));
# endif /* __APPLE__ */
aws_tls_ctx_options_set_alpn_list(&tester->ctx_options, "h2;http/1.1");
tester->ctx = aws_tls_server_ctx_new(allocator, &tester->ctx_options);
ASSERT_NOT_NULL(tester->ctx);
aws_tls_connection_options_init_from_ctx(&tester->opt, tester->ctx);
return AWS_OP_SUCCESS;
}
static int s_tls_client_opt_tester_init(
struct aws_allocator *allocator,
struct tls_opt_tester *tester,
struct aws_byte_cursor server_name) {
aws_io_library_init(allocator);
aws_tls_ctx_options_init_default_client(&tester->ctx_options, allocator);
# ifdef __APPLE__
ASSERT_SUCCESS(
aws_tls_ctx_options_override_default_trust_store_from_path(&tester->ctx_options, NULL, "unittests.crt"));
# else
ASSERT_SUCCESS(
aws_tls_ctx_options_override_default_trust_store_from_path(&tester->ctx_options, NULL, "ca_root.crt"));
# endif /* __APPLE__ */
tester->ctx = aws_tls_client_ctx_new(allocator, &tester->ctx_options);
aws_tls_connection_options_init_from_ctx(&tester->opt, tester->ctx);
aws_tls_connection_options_set_alpn_list(&tester->opt, allocator, "h2;http/1.1");
aws_tls_connection_options_set_server_name(&tester->opt, allocator, &server_name);
return AWS_OP_SUCCESS;
}
static int s_tls_opt_tester_clean_up(struct tls_opt_tester *tester) {
aws_tls_connection_options_clean_up(&tester->opt);
aws_tls_ctx_options_clean_up(&tester->ctx_options);
aws_tls_ctx_release(tester->ctx);
return AWS_OP_SUCCESS;
}
/* common structure for test */
struct tls_common_tester {
struct aws_mutex mutex;
struct aws_condition_variable condition_variable;
struct aws_event_loop_group *el_group;
struct aws_host_resolver *resolver;
struct aws_atomic_var current_time_ns;
struct aws_atomic_var stats_handler;
};
static struct tls_common_tester c_tester;
/* common structure for a tls local server */
struct tls_local_server_tester {
struct aws_socket_options socket_options;
struct tls_opt_tester server_tls_opt_tester;
struct aws_socket_endpoint endpoint;
struct aws_server_bootstrap *server_bootstrap;
struct aws_socket *listener;
uint64_t timestamp;
};
static int s_tls_test_arg_init(
struct aws_allocator *allocator,
struct tls_test_args *test_arg,
bool server,
struct tls_common_tester *tls_c_tester) {
AWS_ZERO_STRUCT(*test_arg);
test_arg->mutex = &tls_c_tester->mutex;
test_arg->condition_variable = &tls_c_tester->condition_variable;
test_arg->allocator = allocator;
test_arg->server = server;
test_arg->desired_tls_levels = 1;
return AWS_OP_SUCCESS;
}
static int s_tls_common_tester_init(struct aws_allocator *allocator, struct tls_common_tester *tester) {
AWS_ZERO_STRUCT(*tester);
struct aws_mutex mutex = AWS_MUTEX_INIT;
struct aws_condition_variable condition_variable = AWS_CONDITION_VARIABLE_INIT;
tester->mutex = mutex;
tester->condition_variable = condition_variable;
aws_atomic_store_int(&tester->current_time_ns, 0);
aws_atomic_store_ptr(&tester->stats_handler, NULL);
tester->el_group = aws_event_loop_group_new_default(allocator, 0, NULL);
struct aws_host_resolver_default_options resolver_options = {
.el_group = tester->el_group,
.max_entries = 1,
};
tester->resolver = aws_host_resolver_new_default(allocator, &resolver_options);
return AWS_OP_SUCCESS;
}
static int s_tls_common_tester_clean_up(struct tls_common_tester *tester) {
aws_host_resolver_release(tester->resolver);
aws_event_loop_group_release(tester->el_group);
aws_io_library_clean_up();
aws_condition_variable_clean_up(&tester->condition_variable);
aws_mutex_clean_up(&tester->mutex);
return AWS_OP_SUCCESS;
}
static bool s_tls_channel_shutdown_predicate(void *user_data) {
struct tls_test_args *setup_test_args = user_data;
return setup_test_args->shutdown_finished || setup_test_args->last_error_code == AWS_IO_SOCKET_TIMEOUT ||
(setup_test_args->expects_error && setup_test_args->error_invoked);
}
static bool s_tls_listener_destroy_predicate(void *user_data) {
struct tls_test_args *setup_test_args = user_data;
return setup_test_args->listener_destroyed || setup_test_args->last_error_code == AWS_IO_SOCKET_TIMEOUT;
}
static bool s_tls_channel_setup_predicate(void *user_data) {
struct tls_test_args *setup_test_args = user_data;
return (setup_test_args->tls_levels_negotiated == setup_test_args->desired_tls_levels &&
setup_test_args->setup_callback_invoked) ||
setup_test_args->error_invoked;
}
/*
* test args mutex must be held before calling this function
*/
static void s_aws_check_for_user_handler_setup(struct tls_test_args *setup_test_args) {
if (setup_test_args->tls_levels_negotiated == setup_test_args->desired_tls_levels &&
setup_test_args->setup_callback_invoked) {
if (setup_test_args->rw_handler) {
struct aws_channel *channel = setup_test_args->channel;
struct aws_channel_slot *rw_slot = aws_channel_slot_new(channel);
aws_channel_slot_insert_end(channel, rw_slot);
aws_channel_slot_set_handler(rw_slot, setup_test_args->rw_handler);
setup_test_args->rw_slot = rw_slot;
}
}
}
static int s_add_tls_handler_to_end_of_channel(struct tls_test_args *setup_test_args) {
AWS_FATAL_ASSERT(setup_test_args->desired_tls_levels > 1);
AWS_FATAL_ASSERT(!setup_test_args->server);
struct aws_channel_slot *last_slot = aws_channel_get_first_slot(setup_test_args->channel);
while (last_slot->adj_right) {
last_slot = last_slot->adj_right;
}
return aws_channel_setup_client_tls(last_slot, setup_test_args->tls_options);
}
static int s_on_channel_setup_next_tls_handler(struct tls_test_args *setup_test_args) {
if (setup_test_args->tls_levels_negotiated < setup_test_args->desired_tls_levels) {
ASSERT_SUCCESS(s_add_tls_handler_to_end_of_channel(setup_test_args));
}
return AWS_OP_SUCCESS;
}
static int s_on_tls_negotiated_next_tls_handler(struct tls_test_args *setup_test_args) {
if (!setup_test_args->setup_callback_invoked) {
return AWS_OP_SUCCESS;
}
if (setup_test_args->tls_levels_negotiated < setup_test_args->desired_tls_levels) {
ASSERT_SUCCESS(s_add_tls_handler_to_end_of_channel(setup_test_args));
}
return AWS_OP_SUCCESS;
}
static void s_tls_handler_test_client_setup_callback(
struct aws_client_bootstrap *bootstrap,
int error_code,
struct aws_channel *channel,
void *user_data) {
(void)bootstrap;
struct tls_test_args *setup_test_args = user_data;
aws_mutex_lock(setup_test_args->mutex);
setup_test_args->setup_callback_invoked = true;
if (!error_code) {
setup_test_args->channel = channel;
s_aws_check_for_user_handler_setup(setup_test_args);
s_on_channel_setup_next_tls_handler(setup_test_args);
} else {
setup_test_args->error_invoked = true;
setup_test_args->last_error_code = error_code;
}
aws_mutex_unlock(setup_test_args->mutex);
aws_condition_variable_notify_one(setup_test_args->condition_variable);
}
static void s_tls_handler_test_server_setup_callback(
struct aws_server_bootstrap *bootstrap,
int error_code,
struct aws_channel *channel,
void *user_data) {
(void)bootstrap;
struct tls_test_args *setup_test_args = (struct tls_test_args *)user_data;
aws_mutex_lock(setup_test_args->mutex);
setup_test_args->setup_callback_invoked = true;
if (!error_code) {
setup_test_args->channel = channel;
} else {
setup_test_args->error_invoked = true;
setup_test_args->last_error_code = error_code;
}
s_aws_check_for_user_handler_setup(setup_test_args);
aws_mutex_unlock(setup_test_args->mutex);
aws_condition_variable_notify_one(setup_test_args->condition_variable);
}
static void s_tls_handler_test_client_shutdown_callback(
struct aws_client_bootstrap *bootstrap,
int error_code,
struct aws_channel *channel,
void *user_data) {
(void)bootstrap;
(void)error_code;
(void)channel;
struct tls_test_args *setup_test_args = (struct tls_test_args *)user_data;
aws_mutex_lock(setup_test_args->mutex);
setup_test_args->shutdown_finished = true;
aws_mutex_unlock(setup_test_args->mutex);
aws_condition_variable_notify_one(setup_test_args->condition_variable);
}
static void s_tls_handler_test_server_shutdown_callback(
struct aws_server_bootstrap *bootstrap,
int error_code,
struct aws_channel *channel,
void *user_data) {
(void)bootstrap;
(void)error_code;
(void)channel;
struct tls_test_args *setup_test_args = (struct tls_test_args *)user_data;
aws_mutex_lock(setup_test_args->mutex);
setup_test_args->shutdown_finished = true;
aws_mutex_unlock(setup_test_args->mutex);
aws_condition_variable_notify_one(setup_test_args->condition_variable);
}
static void s_tls_handler_test_server_listener_destroy_callback(
struct aws_server_bootstrap *bootstrap,
void *user_data) {
(void)bootstrap;
struct tls_test_args *setup_test_args = (struct tls_test_args *)user_data;
aws_mutex_lock(setup_test_args->mutex);
setup_test_args->listener_destroyed = true;
aws_mutex_unlock(setup_test_args->mutex);
aws_condition_variable_notify_one(setup_test_args->condition_variable);
}
static void s_tls_on_negotiated(
struct aws_channel_handler *handler,
struct aws_channel_slot *slot,
int err_code,
void *user_data) {
(void)slot;
struct tls_test_args *setup_test_args = (struct tls_test_args *)user_data;
if (!err_code) {
aws_mutex_lock(setup_test_args->mutex);
if (aws_tls_is_alpn_available()) {
setup_test_args->negotiated_protocol = aws_tls_handler_protocol(handler);
}
setup_test_args->server_name = aws_tls_handler_server_name(handler);
++setup_test_args->tls_levels_negotiated;
s_aws_check_for_user_handler_setup(setup_test_args);
s_on_tls_negotiated_next_tls_handler(setup_test_args);
aws_mutex_unlock(setup_test_args->mutex);
}
aws_condition_variable_notify_one(setup_test_args->condition_variable);
}
static int s_tls_local_server_tester_init(
struct aws_allocator *allocator,
struct tls_local_server_tester *tester,
struct tls_test_args *args,
struct tls_common_tester *tls_c_tester,
bool enable_back_pressure,
int server_index,
const char *cert_path,
const char *pkey_path) {
AWS_ZERO_STRUCT(*tester);
ASSERT_SUCCESS(s_tls_server_opt_tester_init(allocator, &tester->server_tls_opt_tester, cert_path, pkey_path));
aws_tls_connection_options_set_callbacks(&tester->server_tls_opt_tester.opt, s_tls_on_negotiated, NULL, NULL, args);
tester->socket_options.connect_timeout_ms = 3000;
tester->socket_options.type = AWS_SOCKET_STREAM;
tester->socket_options.domain = AWS_SOCKET_LOCAL;
ASSERT_SUCCESS(aws_sys_clock_get_ticks(&tester->timestamp));
snprintf(
tester->endpoint.address,
sizeof(tester->endpoint.address),
LOCAL_SOCK_TEST_PATTERN,
(long long unsigned)tester->timestamp,
server_index);
tester->server_bootstrap = aws_server_bootstrap_new(allocator, tls_c_tester->el_group);
ASSERT_NOT_NULL(tester->server_bootstrap);
struct aws_server_socket_channel_bootstrap_options bootstrap_options = {
.bootstrap = tester->server_bootstrap,
.enable_read_back_pressure = enable_back_pressure,
.port = tester->endpoint.port,
.host_name = tester->endpoint.address,
.socket_options = &tester->socket_options,
.incoming_callback = s_tls_handler_test_server_setup_callback,
.shutdown_callback = s_tls_handler_test_server_shutdown_callback,
.destroy_callback = s_tls_handler_test_server_listener_destroy_callback,
.tls_options = &tester->server_tls_opt_tester.opt,
.user_data = args,
};
tester->listener = aws_server_bootstrap_new_socket_listener(&bootstrap_options);
ASSERT_NOT_NULL(tester->listener);
return AWS_OP_SUCCESS;
}
static int s_tls_local_server_tester_clean_up(struct tls_local_server_tester *tester) {
ASSERT_SUCCESS(s_tls_opt_tester_clean_up(&tester->server_tls_opt_tester));
aws_server_bootstrap_release(tester->server_bootstrap);
return AWS_OP_SUCCESS;
}
struct tls_test_rw_args {
struct aws_mutex *mutex;
struct aws_condition_variable *condition_variable;
struct aws_byte_buf received_message;
int read_invocations;
bool invocation_happened;
};
static int s_tls_rw_args_init(
struct tls_test_rw_args *args,
struct tls_common_tester *tls_c_tester,
struct aws_byte_buf received_message) {
AWS_ZERO_STRUCT(*args);
args->mutex = &tls_c_tester->mutex;
args->condition_variable = &tls_c_tester->condition_variable;
args->received_message = received_message;
return AWS_OP_SUCCESS;
}
static bool s_tls_test_read_predicate(void *user_data) {
struct tls_test_rw_args *rw_args = (struct tls_test_rw_args *)user_data;
return rw_args->invocation_happened;
}
static struct aws_byte_buf s_tls_test_handle_read(
struct aws_channel_handler *handler,
struct aws_channel_slot *slot,
struct aws_byte_buf *data_read,
void *user_data) {
(void)handler;
(void)slot;
struct tls_test_rw_args *rw_args = (struct tls_test_rw_args *)user_data;
aws_mutex_lock(rw_args->mutex);
aws_byte_buf_write_from_whole_buffer(&rw_args->received_message, *data_read);
rw_args->read_invocations += 1;
rw_args->invocation_happened = true;
aws_mutex_unlock(rw_args->mutex);
aws_condition_variable_notify_one(rw_args->condition_variable);
return rw_args->received_message;
}
static struct aws_byte_buf s_tls_test_handle_write(
struct aws_channel_handler *handler,
struct aws_channel_slot *slot,
struct aws_byte_buf *data_read,
void *user_data) {
(void)handler;
(void)slot;
(void)data_read;
(void)user_data;
/*do nothing*/
return (struct aws_byte_buf){0};
}
static int s_tls_channel_echo_and_backpressure_test_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
aws_io_library_init(allocator);
ASSERT_SUCCESS(s_tls_common_tester_init(allocator, &c_tester));
struct aws_byte_buf read_tag = aws_byte_buf_from_c_str("I'm a little teapot.");
struct aws_byte_buf write_tag = aws_byte_buf_from_c_str("I'm a big teapot");
uint8_t incoming_received_message[128] = {0};
uint8_t outgoing_received_message[128] = {0};
struct tls_test_rw_args incoming_rw_args;
ASSERT_SUCCESS(s_tls_rw_args_init(
&incoming_rw_args,
&c_tester,
aws_byte_buf_from_empty_array(incoming_received_message, sizeof(incoming_received_message))));
struct tls_test_rw_args outgoing_rw_args;
ASSERT_SUCCESS(s_tls_rw_args_init(
&outgoing_rw_args,
&c_tester,
aws_byte_buf_from_empty_array(outgoing_received_message, sizeof(outgoing_received_message))));
struct tls_test_args outgoing_args;
ASSERT_SUCCESS(s_tls_test_arg_init(allocator, &outgoing_args, false, &c_tester));
struct tls_test_args incoming_args;
ASSERT_SUCCESS(s_tls_test_arg_init(allocator, &incoming_args, true, &c_tester));
struct tls_local_server_tester local_server_tester;
ASSERT_SUCCESS(s_tls_local_server_tester_init(
allocator, &local_server_tester, &incoming_args, &c_tester, true, 1, "server.crt", "server.key"));
/* make the windows small to make sure back pressure is honored. */
struct aws_channel_handler *outgoing_rw_handler = rw_handler_new(
allocator, s_tls_test_handle_read, s_tls_test_handle_write, true, write_tag.len / 2, &outgoing_rw_args);
ASSERT_NOT_NULL(outgoing_rw_handler);
struct aws_channel_handler *incoming_rw_handler = rw_handler_new(
allocator, s_tls_test_handle_read, s_tls_test_handle_write, true, read_tag.len / 2, &incoming_rw_args);
ASSERT_NOT_NULL(incoming_rw_handler);
incoming_args.rw_handler = incoming_rw_handler;
outgoing_args.rw_handler = outgoing_rw_handler;
g_aws_channel_max_fragment_size = 4096;
struct tls_opt_tester client_tls_opt_tester;
struct aws_byte_cursor server_name = aws_byte_cursor_from_c_str("localhost");
ASSERT_SUCCESS(s_tls_client_opt_tester_init(allocator, &client_tls_opt_tester, server_name));
aws_tls_connection_options_set_callbacks(
&client_tls_opt_tester.opt, s_tls_on_negotiated, NULL, NULL, &outgoing_args);
struct aws_client_bootstrap_options bootstrap_options = {
.event_loop_group = c_tester.el_group,
.host_resolver = c_tester.resolver,
};
struct aws_client_bootstrap *client_bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
struct aws_socket_channel_bootstrap_options channel_options;
AWS_ZERO_STRUCT(channel_options);
channel_options.bootstrap = client_bootstrap;
channel_options.host_name = local_server_tester.endpoint.address;
channel_options.port = 0;
channel_options.socket_options = &local_server_tester.socket_options;
channel_options.tls_options = &client_tls_opt_tester.opt;
channel_options.setup_callback = s_tls_handler_test_client_setup_callback;
channel_options.shutdown_callback = s_tls_handler_test_client_shutdown_callback;
channel_options.user_data = &outgoing_args;
channel_options.enable_read_back_pressure = true;
ASSERT_SUCCESS(aws_client_bootstrap_new_socket_channel(&channel_options));
/* put this here to verify ownership semantics are correct. This should NOT cause a segfault. If it does, ya
* done messed up. */
aws_tls_connection_options_clean_up(&client_tls_opt_tester.opt);
/* wait for both ends to setup */
ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_channel_setup_predicate, &incoming_args));
ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
ASSERT_FALSE(incoming_args.error_invoked);
/* currently it seems ALPN doesn't work in server mode. Just leaving this check out for now. */
# ifndef __APPLE__
struct aws_byte_buf expected_protocol = aws_byte_buf_from_c_str("h2");
/* check ALPN and SNI was properly negotiated */
if (aws_tls_is_alpn_available()) {
ASSERT_BIN_ARRAYS_EQUALS(
expected_protocol.buffer,
expected_protocol.len,
incoming_args.negotiated_protocol.buffer,
incoming_args.negotiated_protocol.len);
}
# endif
ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_channel_setup_predicate, &outgoing_args));
ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
ASSERT_FALSE(outgoing_args.error_invoked);
/* currently it seems ALPN doesn't work in server mode. Just leaving this check out for now. */
# ifndef __MACH__
if (aws_tls_is_alpn_available()) {
ASSERT_BIN_ARRAYS_EQUALS(
expected_protocol.buffer,
expected_protocol.len,
outgoing_args.negotiated_protocol.buffer,
outgoing_args.negotiated_protocol.len);
}
# endif
ASSERT_FALSE(outgoing_args.error_invoked);
/* Do the IO operations */
rw_handler_write(outgoing_args.rw_handler, outgoing_args.rw_slot, &write_tag);
rw_handler_write(incoming_args.rw_handler, incoming_args.rw_slot, &read_tag);
ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_test_read_predicate, &incoming_rw_args));
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_test_read_predicate, &outgoing_rw_args));
ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
incoming_rw_args.invocation_happened = false;
outgoing_rw_args.invocation_happened = false;
ASSERT_INT_EQUALS(1, outgoing_rw_args.read_invocations);
ASSERT_INT_EQUALS(1, incoming_rw_args.read_invocations);
/* Go ahead and verify back-pressure works*/
rw_handler_trigger_increment_read_window(incoming_args.rw_handler, incoming_args.rw_slot, 100);
rw_handler_trigger_increment_read_window(outgoing_args.rw_handler, outgoing_args.rw_slot, 100);
ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_test_read_predicate, &incoming_rw_args));
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_test_read_predicate, &outgoing_rw_args));
ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
ASSERT_INT_EQUALS(2, outgoing_rw_args.read_invocations);
ASSERT_INT_EQUALS(2, incoming_rw_args.read_invocations);
ASSERT_BIN_ARRAYS_EQUALS(
write_tag.buffer,
write_tag.len,
incoming_rw_args.received_message.buffer,
incoming_rw_args.received_message.len);
ASSERT_BIN_ARRAYS_EQUALS(
read_tag.buffer, read_tag.len, outgoing_rw_args.received_message.buffer, outgoing_rw_args.received_message.len);
aws_channel_shutdown(incoming_args.channel, AWS_OP_SUCCESS);
ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_channel_shutdown_predicate, &incoming_args));
ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
/*no shutdown on the client necessary here (it should have been triggered by shutting down the other side). just
* wait for the event to fire. */
ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_channel_shutdown_predicate, &outgoing_args));
aws_server_bootstrap_destroy_socket_listener(local_server_tester.server_bootstrap, local_server_tester.listener);
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_listener_destroy_predicate, &incoming_args));
aws_mutex_unlock(&c_tester.mutex);
/* clean up */
ASSERT_SUCCESS(s_tls_opt_tester_clean_up(&client_tls_opt_tester));
aws_client_bootstrap_release(client_bootstrap);
ASSERT_SUCCESS(s_tls_local_server_tester_clean_up(&local_server_tester));
ASSERT_SUCCESS(s_tls_common_tester_clean_up(&c_tester));
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(tls_channel_echo_and_backpressure_test, s_tls_channel_echo_and_backpressure_test_fn)
struct default_host_callback_data {
struct aws_host_address aaaa_address;
struct aws_host_address a_address;
bool has_aaaa_address;
bool has_a_address;
struct aws_condition_variable condition_variable;
bool invoked;
};
static int s_verify_negotiation_fails_helper(
struct aws_allocator *allocator,
const struct aws_string *host_name,
uint16_t port,
struct aws_tls_ctx_options *client_ctx_options) {
struct aws_tls_ctx *client_ctx = aws_tls_client_ctx_new(allocator, client_ctx_options);
struct aws_tls_connection_options tls_client_conn_options;
aws_tls_connection_options_init_from_ctx(&tls_client_conn_options, client_ctx);
aws_tls_connection_options_set_callbacks(&tls_client_conn_options, s_tls_on_negotiated, NULL, NULL, NULL);
struct aws_byte_cursor host_name_cur = aws_byte_cursor_from_string(host_name);
aws_tls_connection_options_set_server_name(&tls_client_conn_options, allocator, &host_name_cur);
struct tls_test_args outgoing_args = {
.mutex = &c_tester.mutex,
.allocator = allocator,
.condition_variable = &c_tester.condition_variable,
.error_invoked = false,
.expects_error = true,
.rw_handler = NULL,
.server = false,
.tls_levels_negotiated = 0,
.desired_tls_levels = 1,
.shutdown_finished = false,
};
tls_client_conn_options.user_data = &outgoing_args;
struct aws_socket_options options;
AWS_ZERO_STRUCT(options);
/* badssl.com is great but has occasional lags, make this timeout longer so we have a
higher chance of actually testing something. */
options.connect_timeout_ms = 10000;
options.type = AWS_SOCKET_STREAM;
options.domain = AWS_SOCKET_IPV4;
struct aws_client_bootstrap_options bootstrap_options = {
.event_loop_group = c_tester.el_group,
.host_resolver = c_tester.resolver,
};
struct aws_client_bootstrap *client_bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
ASSERT_NOT_NULL(client_bootstrap);
struct aws_socket_channel_bootstrap_options channel_options;
AWS_ZERO_STRUCT(channel_options);
channel_options.bootstrap = client_bootstrap;
channel_options.host_name = aws_string_c_str(host_name);
channel_options.port = port;
channel_options.socket_options = &options;
channel_options.tls_options = &tls_client_conn_options;
channel_options.setup_callback = s_tls_handler_test_client_setup_callback;
channel_options.shutdown_callback = s_tls_handler_test_client_shutdown_callback;
channel_options.user_data = &outgoing_args;
ASSERT_SUCCESS(aws_client_bootstrap_new_socket_channel(&channel_options));
/* put this here to verify ownership semantics are correct. This should NOT cause a segfault. If it does, ya
* done messed up. */
aws_tls_connection_options_clean_up(&tls_client_conn_options);
ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_channel_shutdown_predicate, &outgoing_args));
ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
ASSERT_TRUE(outgoing_args.error_invoked);
/* we're talking to an external internet endpoint, yeah this sucks... we don't know for sure that
this failed for the right reasons, but there's not much we can do about it.*/
if (outgoing_args.last_error_code != AWS_IO_SOCKET_TIMEOUT) {
ASSERT_INT_EQUALS(AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE, outgoing_args.last_error_code);
} else {
fprintf(
stderr,
"Warning: the connection timed out and we're not completely certain"
" that this fails for the right reasons. Maybe run the test again?\n");
}
aws_client_bootstrap_release(client_bootstrap);
aws_tls_ctx_release(client_ctx);
return AWS_OP_SUCCESS;
}
static int s_verify_negotiation_fails(
struct aws_allocator *allocator,
const struct aws_string *host_name,
uint16_t port,
void (*context_options_override_fn)(struct aws_tls_ctx_options *)) {
aws_io_library_init(allocator);
ASSERT_SUCCESS(s_tls_common_tester_init(allocator, &c_tester));
struct aws_tls_ctx_options client_ctx_options;
aws_tls_ctx_options_init_default_client(&client_ctx_options, allocator);
if (context_options_override_fn) {
(*context_options_override_fn)(&client_ctx_options);
}
ASSERT_SUCCESS(s_verify_negotiation_fails_helper(allocator, host_name, port, &client_ctx_options));
aws_tls_ctx_options_clean_up(&client_ctx_options);
ASSERT_SUCCESS(s_tls_common_tester_clean_up(&c_tester));
return AWS_OP_SUCCESS;
}
static int s_verify_negotiation_fails_with_ca_override(
struct aws_allocator *allocator,
const struct aws_string *host_name,
const char *root_ca_path) {
aws_io_library_init(allocator);
ASSERT_SUCCESS(s_tls_common_tester_init(allocator, &c_tester));
struct aws_tls_ctx_options client_ctx_options;
aws_tls_ctx_options_init_default_client(&client_ctx_options, allocator);
ASSERT_SUCCESS(aws_tls_ctx_options_override_default_trust_store_from_path(&client_ctx_options, NULL, root_ca_path));
ASSERT_SUCCESS(s_verify_negotiation_fails_helper(allocator, host_name, 443, &client_ctx_options));
ASSERT_SUCCESS(s_tls_common_tester_clean_up(&c_tester));
aws_tls_ctx_options_clean_up(&client_ctx_options);
return AWS_OP_SUCCESS;
}
AWS_STATIC_STRING_FROM_LITERAL(s_expired_host_name, "expired.badssl.com");
static int s_tls_client_channel_negotiation_error_expired_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_negotiation_fails(allocator, s_expired_host_name, 443, NULL);
}
AWS_TEST_CASE(tls_client_channel_negotiation_error_expired, s_tls_client_channel_negotiation_error_expired_fn)
AWS_STATIC_STRING_FROM_LITERAL(s_wrong_host_name, "wrong.host.badssl.com");
static int s_tls_client_channel_negotiation_error_wrong_host_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_negotiation_fails(allocator, s_wrong_host_name, 443, NULL);
}
AWS_TEST_CASE(tls_client_channel_negotiation_error_wrong_host, s_tls_client_channel_negotiation_error_wrong_host_fn)
static int s_tls_client_channel_negotiation_error_wrong_host_with_ca_override_fn(
struct aws_allocator *allocator,
void *ctx) {
(void)ctx;
return s_verify_negotiation_fails_with_ca_override(allocator, s_wrong_host_name, "DigiCertGlobalRootCA.crt.pem");
}
AWS_TEST_CASE(
tls_client_channel_negotiation_error_wrong_host_with_ca_override,
s_tls_client_channel_negotiation_error_wrong_host_with_ca_override_fn)
AWS_STATIC_STRING_FROM_LITERAL(s_self_signed_host_name, "self-signed.badssl.com");
static int s_tls_client_channel_negotiation_error_self_signed_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_negotiation_fails(allocator, s_self_signed_host_name, 443, NULL);
}
AWS_TEST_CASE(tls_client_channel_negotiation_error_self_signed, s_tls_client_channel_negotiation_error_self_signed_fn)
AWS_STATIC_STRING_FROM_LITERAL(s_untrusted_root_host_name, "untrusted-root.badssl.com");
static int s_tls_client_channel_negotiation_error_untrusted_root_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_negotiation_fails(allocator, s_untrusted_root_host_name, 443, NULL);
}
AWS_TEST_CASE(
tls_client_channel_negotiation_error_untrusted_root,
s_tls_client_channel_negotiation_error_untrusted_root_fn);
AWS_STATIC_STRING_FROM_LITERAL(s_amazon_host_name, "www.amazon.com");
/* negotiation should fail. www.amazon.com is obviously trusted by the default trust store,
* but we've overridden the default trust store */
static int s_tls_client_channel_negotiation_error_untrusted_root_due_to_ca_override_fn(
struct aws_allocator *allocator,
void *ctx) {
(void)ctx;
return s_verify_negotiation_fails_with_ca_override(allocator, s_amazon_host_name, "ca_root.crt");
}
AWS_TEST_CASE(
tls_client_channel_negotiation_error_untrusted_root_due_to_ca_override,
s_tls_client_channel_negotiation_error_untrusted_root_due_to_ca_override_fn)
AWS_STATIC_STRING_FROM_LITERAL(s_broken_crypto_rc4_host_name, "rc4.badssl.com");
static int s_tls_client_channel_negotiation_error_broken_crypto_rc4_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_negotiation_fails(allocator, s_broken_crypto_rc4_host_name, 443, NULL);
}
AWS_TEST_CASE(
tls_client_channel_negotiation_error_broken_crypto_rc4,
s_tls_client_channel_negotiation_error_broken_crypto_rc4_fn)
AWS_STATIC_STRING_FROM_LITERAL(s_broken_crypto_rc4_md5_host_name, "rc4-md5.badssl.com");
static int s_tls_client_channel_negotiation_error_broken_crypto_rc4_md5_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_negotiation_fails(allocator, s_broken_crypto_rc4_md5_host_name, 443, NULL);
}
AWS_TEST_CASE(
tls_client_channel_negotiation_error_broken_crypto_rc4_md5,
s_tls_client_channel_negotiation_error_broken_crypto_rc4_md5_fn)
AWS_STATIC_STRING_FROM_LITERAL(s_broken_crypto_dh480_host_name, "dh480.badssl.com");
static int s_tls_client_channel_negotiation_error_broken_crypto_dh480_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_negotiation_fails(allocator, s_broken_crypto_dh480_host_name, 443, NULL);
}
AWS_TEST_CASE(
tls_client_channel_negotiation_error_broken_crypto_dh480,
s_tls_client_channel_negotiation_error_broken_crypto_dh480_fn)
AWS_STATIC_STRING_FROM_LITERAL(s_broken_crypto_dh512_host_name, "dh512.badssl.com");
static int s_tls_client_channel_negotiation_error_broken_crypto_dh512_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_negotiation_fails(allocator, s_broken_crypto_dh512_host_name, 443, NULL);
}
AWS_TEST_CASE(
tls_client_channel_negotiation_error_broken_crypto_dh512,
s_tls_client_channel_negotiation_error_broken_crypto_dh512_fn)
AWS_STATIC_STRING_FROM_LITERAL(s_broken_crypto_dh1024_host_name, "dh1024.badssl.com");
static int s_tls_client_channel_negotiation_error_broken_crypto_dh1024_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_negotiation_fails(allocator, s_broken_crypto_dh1024_host_name, 443, NULL);
}
AWS_TEST_CASE(
tls_client_channel_negotiation_error_broken_crypto_dh1024,
s_tls_client_channel_negotiation_error_broken_crypto_dh1024_fn)
AWS_STATIC_STRING_FROM_LITERAL(s_broken_crypto_null_host_name, "null.badssl.com");
static int s_tls_client_channel_negotiation_error_broken_crypto_null_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_negotiation_fails(allocator, s_broken_crypto_null_host_name, 443, NULL);
}
AWS_TEST_CASE(
tls_client_channel_negotiation_error_broken_crypto_null,
s_tls_client_channel_negotiation_error_broken_crypto_null_fn)
AWS_STATIC_STRING_FROM_LITERAL(s_legacy_crypto_tls10_host_name, "tls-v1-0.badssl.com");
static void s_raise_tls_version_to_11(struct aws_tls_ctx_options *options) {
aws_tls_ctx_options_set_minimum_tls_version(options, AWS_IO_TLSv1_2);
}
static int s_tls_client_channel_negotiation_error_legacy_crypto_tls10_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_negotiation_fails(allocator, s_legacy_crypto_tls10_host_name, 1010, &s_raise_tls_version_to_11);
}
AWS_TEST_CASE(
tls_client_channel_negotiation_error_legacy_crypto_tls10,
s_tls_client_channel_negotiation_error_legacy_crypto_tls10_fn)
AWS_STATIC_STRING_FROM_LITERAL(s_legacy_crypto_tls11_host_name, "tls-v1-1.badssl.com");
static void s_raise_tls_version_to_12(struct aws_tls_ctx_options *options) {
aws_tls_ctx_options_set_minimum_tls_version(options, AWS_IO_TLSv1_2);
}
static int s_tls_client_channel_negotiation_error_override_legacy_crypto_tls11_fn(
struct aws_allocator *allocator,
void *ctx) {
(void)ctx;
return s_verify_negotiation_fails(allocator, s_legacy_crypto_tls11_host_name, 1011, &s_raise_tls_version_to_12);
}
AWS_TEST_CASE(
tls_client_channel_negotiation_error_override_legacy_crypto_tls11,
s_tls_client_channel_negotiation_error_override_legacy_crypto_tls11_fn)
AWS_STATIC_STRING_FROM_LITERAL(s_legacy_crypto_dh2048_host_name, "dh2048.badssl.com");
static int s_tls_client_channel_negotiation_error_legacy_crypto_dh2048_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_negotiation_fails(allocator, s_legacy_crypto_dh2048_host_name, 443, NULL);
}
AWS_TEST_CASE(
tls_client_channel_negotiation_error_legacy_crypto_dh2048,
s_tls_client_channel_negotiation_error_legacy_crypto_dh2048_fn)
AWS_STATIC_STRING_FROM_LITERAL(s_uncommon_no_subject_host_name, "no-subject.badssl.com");
static int s_tls_client_channel_negotiation_error_no_subject_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_negotiation_fails(allocator, s_uncommon_no_subject_host_name, 443, NULL);
}
AWS_TEST_CASE(tls_client_channel_negotiation_error_no_subject, s_tls_client_channel_negotiation_error_no_subject_fn)
AWS_STATIC_STRING_FROM_LITERAL(s_uncommon_no_common_name_host_name, "no-common-name.badssl.com");
static int s_tls_client_channel_negotiation_error_no_common_name_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_negotiation_fails(allocator, s_uncommon_no_common_name_host_name, 443, NULL);
}
AWS_TEST_CASE(
tls_client_channel_negotiation_error_no_common_name,
s_tls_client_channel_negotiation_error_no_common_name_fn)
/* Test that, if the channel shuts down unexpectedly during tls negotiation, that the user code is still notified.
* We make this happen by connecting to port 80 on s3 or amazon.com and attempting TLS,
* which gets you hung up on after a few seconds */
static int s_tls_client_channel_negotiation_error_socket_closed_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
const char *host_name = "aws-crt-test-stuff.s3.amazonaws.com";
uint16_t port = 80; /* Note: intentionally wrong and not 443 */
aws_io_library_init(allocator);
ASSERT_SUCCESS(s_tls_common_tester_init(allocator, &c_tester));
struct tls_opt_tester client_tls_opt_tester;
struct aws_byte_cursor server_name = aws_byte_cursor_from_c_str(host_name);
ASSERT_SUCCESS(s_tls_client_opt_tester_init(allocator, &client_tls_opt_tester, server_name));
client_tls_opt_tester.opt.timeout_ms = 0; /* disable negotiation timeout for this test */
struct tls_test_args outgoing_args;
ASSERT_SUCCESS(s_tls_test_arg_init(allocator, &outgoing_args, false, &c_tester));
struct aws_socket_options options = {
.connect_timeout_ms = 10000, .type = AWS_SOCKET_STREAM, .domain = AWS_SOCKET_IPV4};
struct aws_client_bootstrap_options bootstrap_options = {
.event_loop_group = c_tester.el_group,
.host_resolver = c_tester.resolver,
};
struct aws_client_bootstrap *client_bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
ASSERT_NOT_NULL(client_bootstrap);
struct aws_socket_channel_bootstrap_options channel_options;
AWS_ZERO_STRUCT(channel_options);
channel_options.bootstrap = client_bootstrap;
channel_options.host_name = host_name;
channel_options.port = port;
channel_options.socket_options = &options;
channel_options.tls_options = &client_tls_opt_tester.opt;
channel_options.setup_callback = s_tls_handler_test_client_setup_callback;
channel_options.shutdown_callback = s_tls_handler_test_client_shutdown_callback;
channel_options.user_data = &outgoing_args;
ASSERT_SUCCESS(aws_client_bootstrap_new_socket_channel(&channel_options));
/* Wait for setup to complete */
aws_mutex_lock(&c_tester.mutex);
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_channel_setup_predicate, &outgoing_args));
/* Assert that setup failed, and that it failed for reasons unrelated to the tls-handler. */
ASSERT_INT_EQUALS(0, outgoing_args.tls_levels_negotiated);
ASSERT_TRUE(outgoing_args.error_invoked);
ASSERT_INT_EQUALS(AWS_IO_SOCKET_CLOSED, outgoing_args.last_error_code);
aws_mutex_unlock(&c_tester.mutex);
/* Clean up */
aws_client_bootstrap_release(client_bootstrap);
s_tls_opt_tester_clean_up(&client_tls_opt_tester);
ASSERT_SUCCESS(s_tls_common_tester_clean_up(&c_tester));
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(
tls_client_channel_negotiation_error_socket_closed,
s_tls_client_channel_negotiation_error_socket_closed_fn);
static int s_verify_good_host(
struct aws_allocator *allocator,
const struct aws_string *host_name,
uint16_t port,
void (*override_tls_options_fn)(struct aws_tls_ctx_options *)) {
aws_io_library_init(allocator);
ASSERT_SUCCESS(s_tls_common_tester_init(allocator, &c_tester));
struct tls_test_args outgoing_args = {
.mutex = &c_tester.mutex,
.allocator = allocator,
.condition_variable = &c_tester.condition_variable,
.error_invoked = 0,
.rw_handler = NULL,
.server = false,
.tls_levels_negotiated = 0,
.desired_tls_levels = 1,
.shutdown_finished = false,
};
struct aws_tls_ctx_options client_ctx_options;
AWS_ZERO_STRUCT(client_ctx_options);
aws_tls_ctx_options_set_verify_peer(&client_ctx_options, true);
aws_tls_ctx_options_init_default_client(&client_ctx_options, allocator);
aws_tls_ctx_options_set_alpn_list(&client_ctx_options, "http/1.1");
if (override_tls_options_fn) {
(*override_tls_options_fn)(&client_ctx_options);
}
struct aws_tls_ctx *client_ctx = aws_tls_client_ctx_new(allocator, &client_ctx_options);
struct aws_tls_connection_options tls_client_conn_options;
aws_tls_connection_options_init_from_ctx(&tls_client_conn_options, client_ctx);
aws_tls_connection_options_set_callbacks(&tls_client_conn_options, s_tls_on_negotiated, NULL, NULL, &outgoing_args);
struct aws_byte_cursor host_name_cur = aws_byte_cursor_from_string(host_name);
aws_tls_connection_options_set_server_name(&tls_client_conn_options, allocator, &host_name_cur);
aws_tls_connection_options_set_alpn_list(&tls_client_conn_options, allocator, "http/1.1");
struct aws_socket_options options;
AWS_ZERO_STRUCT(options);
options.connect_timeout_ms = 10000;
options.type = AWS_SOCKET_STREAM;
options.domain = AWS_SOCKET_IPV4;
struct aws_client_bootstrap_options bootstrap_options = {
.event_loop_group = c_tester.el_group,
.host_resolver = c_tester.resolver,
};
struct aws_client_bootstrap *client_bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
ASSERT_NOT_NULL(client_bootstrap);
struct aws_socket_channel_bootstrap_options channel_options;
AWS_ZERO_STRUCT(channel_options);
channel_options.bootstrap = client_bootstrap;
channel_options.host_name = aws_string_c_str(host_name);
channel_options.port = port;
channel_options.socket_options = &options;
channel_options.tls_options = &tls_client_conn_options;
channel_options.setup_callback = s_tls_handler_test_client_setup_callback;
channel_options.shutdown_callback = s_tls_handler_test_client_shutdown_callback;
channel_options.user_data = &outgoing_args;
ASSERT_SUCCESS(aws_client_bootstrap_new_socket_channel(&channel_options));
/* put this here to verify ownership semantics are correct. This should NOT cause a segfault. If it does, ya
* done messed up. */
aws_tls_connection_options_clean_up(&tls_client_conn_options);
ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_channel_setup_predicate, &outgoing_args));
ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
ASSERT_FALSE(outgoing_args.error_invoked);
struct aws_byte_buf expected_protocol = aws_byte_buf_from_c_str("http/1.1");
/* check ALPN and SNI was properly negotiated */
if (aws_tls_is_alpn_available() && client_ctx_options.verify_peer) {
ASSERT_BIN_ARRAYS_EQUALS(
expected_protocol.buffer,
expected_protocol.len,
outgoing_args.negotiated_protocol.buffer,
outgoing_args.negotiated_protocol.len);
}
ASSERT_BIN_ARRAYS_EQUALS(
host_name->bytes, host_name->len, outgoing_args.server_name.buffer, outgoing_args.server_name.len);
ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
aws_channel_shutdown(outgoing_args.channel, AWS_OP_SUCCESS);
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_channel_shutdown_predicate, &outgoing_args));
ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
aws_client_bootstrap_release(client_bootstrap);
aws_tls_ctx_release(client_ctx);
aws_tls_ctx_options_clean_up(&client_ctx_options);
ASSERT_SUCCESS(s_tls_common_tester_clean_up(&c_tester));
return AWS_OP_SUCCESS;
}
static int s_tls_client_channel_negotiation_success_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_good_host(allocator, s_amazon_host_name, 443, NULL);
}
AWS_TEST_CASE(tls_client_channel_negotiation_success, s_tls_client_channel_negotiation_success_fn)
AWS_STATIC_STRING_FROM_LITERAL(s_badssl_ecc256_host_name, "ecc256.badssl.com");
static int s_tls_client_channel_negotiation_success_ecc256_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_good_host(allocator, s_badssl_ecc256_host_name, 443, NULL);
}
AWS_TEST_CASE(tls_client_channel_negotiation_success_ecc256, s_tls_client_channel_negotiation_success_ecc256_fn)
AWS_STATIC_STRING_FROM_LITERAL(s_badssl_ecc384_host_name, "ecc384.badssl.com");
static int s_tls_client_channel_negotiation_success_ecc384_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_good_host(allocator, s_badssl_ecc384_host_name, 443, NULL);
}
AWS_TEST_CASE(tls_client_channel_negotiation_success_ecc384, s_tls_client_channel_negotiation_success_ecc384_fn)
AWS_STATIC_STRING_FROM_LITERAL(s3_host_name, "s3.amazonaws.com");
static void s_disable_verify_peer(struct aws_tls_ctx_options *options) {
aws_tls_ctx_options_set_verify_peer(options, false);
}
/* prove that connections complete even when verify_peer is false */
static int s_tls_client_channel_no_verify_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_good_host(allocator, s3_host_name, 443, &s_disable_verify_peer);
}
AWS_TEST_CASE(tls_client_channel_no_verify, s_tls_client_channel_no_verify_fn)
/* Check all of the bad tls cases with verify_peer off. Now they should succeed. */
static int s_tls_client_channel_negotiation_no_verify_expired_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_good_host(allocator, s_expired_host_name, 443, &s_disable_verify_peer);
}
AWS_TEST_CASE(tls_client_channel_negotiation_no_verify_expired, s_tls_client_channel_negotiation_no_verify_expired_fn)
static int s_tls_client_channel_negotiation_no_verify_wrong_host_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_good_host(allocator, s_wrong_host_name, 443, &s_disable_verify_peer);
}
AWS_TEST_CASE(
tls_client_channel_negotiation_no_verify_wrong_host,
s_tls_client_channel_negotiation_no_verify_wrong_host_fn)
static int s_tls_client_channel_negotiation_no_verify_self_signed_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_good_host(allocator, s_self_signed_host_name, 443, &s_disable_verify_peer);
}
AWS_TEST_CASE(
tls_client_channel_negotiation_no_verify_self_signed,
s_tls_client_channel_negotiation_no_verify_self_signed_fn)
static int s_tls_client_channel_negotiation_no_verify_untrusted_root_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_good_host(allocator, s_untrusted_root_host_name, 443, &s_disable_verify_peer);
}
AWS_TEST_CASE(
tls_client_channel_negotiation_no_verify_untrusted_root,
s_tls_client_channel_negotiation_no_verify_untrusted_root_fn)
static void s_lower_tls_version(struct aws_tls_ctx_options *options) {
aws_tls_ctx_options_set_minimum_tls_version(options, AWS_IO_TLSv1);
}
static int s_tls_client_channel_negotiation_override_legacy_crypto_tls10_fn(
struct aws_allocator *allocator,
void *ctx) {
(void)ctx;
return s_verify_good_host(allocator, s_legacy_crypto_tls10_host_name, 1010, &s_lower_tls_version);
}
AWS_TEST_CASE(
tls_client_channel_negotiation_override_legacy_crypto_tls10,
s_tls_client_channel_negotiation_override_legacy_crypto_tls10_fn)
static int s_tls_client_channel_negotiation_success_legacy_crypto_tls11_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_good_host(allocator, s_legacy_crypto_tls11_host_name, 1011, NULL);
}
AWS_TEST_CASE(
tls_client_channel_negotiation_success_legacy_crypto_tls11,
s_tls_client_channel_negotiation_success_legacy_crypto_tls11_fn)
AWS_STATIC_STRING_FROM_LITERAL(s_uncommon_sha384_host_name, "sha384.badssl.com");
static int s_tls_client_channel_negotiation_success_sha384_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_good_host(allocator, s_uncommon_sha384_host_name, 443, NULL);
}
AWS_TEST_CASE(tls_client_channel_negotiation_success_sha384, s_tls_client_channel_negotiation_success_sha384_fn)
AWS_STATIC_STRING_FROM_LITERAL(s_uncommon_sha512_host_name, "sha512.badssl.com");
static int s_tls_client_channel_negotiation_success_sha512_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_good_host(allocator, s_uncommon_sha512_host_name, 443, NULL);
}
AWS_TEST_CASE(tls_client_channel_negotiation_success_sha512, s_tls_client_channel_negotiation_success_sha512_fn)
AWS_STATIC_STRING_FROM_LITERAL(s_uncommon_rsa8192_host_name, "rsa8192.badssl.com");
static int s_tls_client_channel_negotiation_success_rsa8192_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_good_host(allocator, s_uncommon_rsa8192_host_name, 443, NULL);
}
AWS_TEST_CASE(tls_client_channel_negotiation_success_rsa8192, s_tls_client_channel_negotiation_success_rsa8192_fn)
AWS_STATIC_STRING_FROM_LITERAL(s_uncommon_incomplete_chain_host_name, "incomplete-chain.badssl.com");
static int s_tls_client_channel_negotiation_success_no_verify_incomplete_chain_fn(
struct aws_allocator *allocator,
void *ctx) {
(void)ctx;
return s_verify_good_host(allocator, s_uncommon_incomplete_chain_host_name, 443, s_disable_verify_peer);
}
AWS_TEST_CASE(
tls_client_channel_negotiation_success_no_verify_incomplete_chain,
s_tls_client_channel_negotiation_success_no_verify_incomplete_chain_fn)
static int s_tls_client_channel_negotiation_success_no_verify_no_subject_fn(
struct aws_allocator *allocator,
void *ctx) {
(void)ctx;
return s_verify_good_host(allocator, s_uncommon_no_subject_host_name, 443, s_disable_verify_peer);
}
AWS_TEST_CASE(
tls_client_channel_negotiation_success_no_verify_no_subject,
s_tls_client_channel_negotiation_success_no_verify_no_subject_fn)
static int s_tls_client_channel_negotiation_success_no_verify_no_common_name_fn(
struct aws_allocator *allocator,
void *ctx) {
(void)ctx;
return s_verify_good_host(allocator, s_uncommon_no_common_name_host_name, 443, s_disable_verify_peer);
}
AWS_TEST_CASE(
tls_client_channel_negotiation_success_no_verify_no_common_name,
s_tls_client_channel_negotiation_success_no_verify_no_common_name_fn)
AWS_STATIC_STRING_FROM_LITERAL(s_common_tls12_host_name, "tls-v1-2.badssl.com");
static int s_tls_client_channel_negotiation_success_tls12_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_good_host(allocator, s_common_tls12_host_name, 1012, NULL);
}
AWS_TEST_CASE(tls_client_channel_negotiation_success_tls12, s_tls_client_channel_negotiation_success_tls12_fn)
AWS_STATIC_STRING_FROM_LITERAL(s_common_sha256_host_name, "sha256.badssl.com");
static int s_tls_client_channel_negotiation_success_sha256_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_good_host(allocator, s_common_sha256_host_name, 443, NULL);
}
AWS_TEST_CASE(tls_client_channel_negotiation_success_sha256, s_tls_client_channel_negotiation_success_sha256_fn)
AWS_STATIC_STRING_FROM_LITERAL(s_common_rsa2048_host_name, "rsa2048.badssl.com");
static int s_tls_client_channel_negotiation_success_rsa2048_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_good_host(allocator, s_common_rsa2048_host_name, 443, NULL);
}
AWS_TEST_CASE(tls_client_channel_negotiation_success_rsa2048, s_tls_client_channel_negotiation_success_rsa2048_fn)
AWS_STATIC_STRING_FROM_LITERAL(s_common_extended_validation_host_name, "extended-validation.badssl.com");
static int s_tls_client_channel_negotiation_success_extended_validation_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_good_host(allocator, s_common_extended_validation_host_name, 443, NULL);
}
AWS_TEST_CASE(
tls_client_channel_negotiation_success_extended_validation,
s_tls_client_channel_negotiation_success_extended_validation_fn)
AWS_STATIC_STRING_FROM_LITERAL(s_common_mozilla_modern_host_name, "mozilla-modern.badssl.com");
static int s_tls_client_channel_negotiation_success_mozilla_modern_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
return s_verify_good_host(allocator, s_common_mozilla_modern_host_name, 443, NULL);
}
AWS_TEST_CASE(
tls_client_channel_negotiation_success_mozilla_modern,
s_tls_client_channel_negotiation_success_mozilla_modern_fn)
static void s_reset_arg_state(struct tls_test_args *setup_test_args) {
setup_test_args->tls_levels_negotiated = 0;
setup_test_args->shutdown_finished = false;
setup_test_args->creation_callback_invoked = false;
setup_test_args->setup_callback_invoked = false;
}
static int s_tls_server_multiple_connections_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
aws_io_library_init(allocator);
ASSERT_SUCCESS(s_tls_common_tester_init(allocator, &c_tester));
struct tls_test_args outgoing_args;
ASSERT_SUCCESS(s_tls_test_arg_init(allocator, &outgoing_args, false, &c_tester));
struct tls_test_args incoming_args;
ASSERT_SUCCESS(s_tls_test_arg_init(allocator, &incoming_args, true, &c_tester));
struct tls_local_server_tester local_server_tester;
ASSERT_SUCCESS(s_tls_local_server_tester_init(
allocator, &local_server_tester, &incoming_args, &c_tester, false, 1, "server.crt", "server.key"));
struct tls_opt_tester client_tls_opt_tester;
struct aws_byte_cursor server_name = aws_byte_cursor_from_c_str("localhost");
ASSERT_SUCCESS(s_tls_client_opt_tester_init(allocator, &client_tls_opt_tester, server_name));
aws_tls_connection_options_set_callbacks(
&client_tls_opt_tester.opt, s_tls_on_negotiated, NULL, NULL, &outgoing_args);
struct aws_client_bootstrap_options bootstrap_options = {
.event_loop_group = c_tester.el_group,
.host_resolver = c_tester.resolver,
};
struct aws_client_bootstrap *client_bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
struct aws_socket_channel_bootstrap_options channel_options;
AWS_ZERO_STRUCT(channel_options);
channel_options.bootstrap = client_bootstrap;
channel_options.host_name = local_server_tester.endpoint.address;
channel_options.port = 0;
channel_options.socket_options = &local_server_tester.socket_options;
channel_options.tls_options = &client_tls_opt_tester.opt;
channel_options.setup_callback = s_tls_handler_test_client_setup_callback;
channel_options.shutdown_callback = s_tls_handler_test_client_shutdown_callback;
channel_options.user_data = &outgoing_args;
ASSERT_SUCCESS(aws_client_bootstrap_new_socket_channel(&channel_options));
/* wait for both ends to setup */
ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_channel_setup_predicate, &incoming_args));
ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
ASSERT_FALSE(incoming_args.error_invoked);
/* shut down */
aws_channel_shutdown(incoming_args.channel, AWS_OP_SUCCESS);
ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_channel_shutdown_predicate, &incoming_args));
ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
/* no shutdown on the client necessary here (it should have been triggered by shutting down the other side). just
* wait for the event to fire. */
ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_channel_shutdown_predicate, &outgoing_args));
ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
/* connect again! */
s_reset_arg_state(&outgoing_args);
s_reset_arg_state(&incoming_args);
ASSERT_SUCCESS(aws_client_bootstrap_new_socket_channel(&channel_options));
/* wait for both ends to setup */
ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_channel_setup_predicate, &incoming_args));
ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
ASSERT_FALSE(incoming_args.error_invoked);
/* shut down */
aws_channel_shutdown(incoming_args.channel, AWS_OP_SUCCESS);
ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_channel_shutdown_predicate, &incoming_args));
ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
/*no shutdown on the client necessary here (it should have been triggered by shutting down the other side). just
* wait for the event to fire. */
ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_channel_shutdown_predicate, &outgoing_args));
ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
aws_server_bootstrap_destroy_socket_listener(local_server_tester.server_bootstrap, local_server_tester.listener);
ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_listener_destroy_predicate, &incoming_args));
aws_mutex_unlock(&c_tester.mutex);
/* clean up */
ASSERT_SUCCESS(s_tls_opt_tester_clean_up(&client_tls_opt_tester));
aws_client_bootstrap_release(client_bootstrap);
ASSERT_SUCCESS(s_tls_local_server_tester_clean_up(&local_server_tester));
ASSERT_SUCCESS(s_tls_common_tester_clean_up(&c_tester));
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(tls_server_multiple_connections, s_tls_server_multiple_connections_fn)
struct shutdown_listener_tester {
struct aws_socket *listener;
struct aws_server_bootstrap *server_bootstrap;
struct tls_test_args *outgoing_args; /* client args */
struct aws_socket client_socket;
};
static bool s_client_socket_closed_predicate(void *user_data) {
struct tls_test_args *args = user_data;
return args->shutdown_finished;
}
static void s_close_client_socket_task(struct aws_task *task, void *arg, enum aws_task_status status) {
(void)status;
struct shutdown_listener_tester *tester = arg;
/* Free task memory */
aws_mem_release(tester->outgoing_args->allocator, task);
/* Close socket and notify */
AWS_FATAL_ASSERT(aws_socket_close(&tester->client_socket) == AWS_OP_SUCCESS);
AWS_FATAL_ASSERT(aws_mutex_lock(tester->outgoing_args->mutex) == AWS_OP_SUCCESS);
tester->outgoing_args->shutdown_finished = true;
AWS_FATAL_ASSERT(aws_mutex_unlock(tester->outgoing_args->mutex) == AWS_OP_SUCCESS);
AWS_FATAL_ASSERT(aws_condition_variable_notify_one(tester->outgoing_args->condition_variable) == AWS_OP_SUCCESS);
}
static void s_on_client_connected_do_hangup(struct aws_socket *socket, int error_code, void *user_data) {
AWS_FATAL_ASSERT(error_code == 0);
struct shutdown_listener_tester *tester = user_data;
tester->client_socket = *socket;
/* wait 1 sec so server side has time to setup the channel, then close the socket */
uint64_t run_at_ns;
aws_event_loop_current_clock_time(socket->event_loop, &run_at_ns);
run_at_ns += aws_timestamp_convert(1, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL);
struct aws_task *close_client_socket_task =
aws_mem_acquire(tester->outgoing_args->allocator, sizeof(struct aws_task));
aws_task_init(close_client_socket_task, s_close_client_socket_task, tester, "wait_close_client_socket");
aws_event_loop_schedule_task_future(socket->event_loop, close_client_socket_task, run_at_ns);
}
/* Test that server can handle a hangup in the middle of TLS negotiation */
static int s_tls_server_hangup_during_negotiation_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
aws_io_library_init(allocator);
ASSERT_SUCCESS(s_tls_common_tester_init(allocator, &c_tester));
struct tls_test_args outgoing_args;
ASSERT_SUCCESS(s_tls_test_arg_init(allocator, &outgoing_args, false, &c_tester));
struct tls_test_args incoming_args;
ASSERT_SUCCESS(s_tls_test_arg_init(allocator, &incoming_args, true, &c_tester));
struct tls_local_server_tester local_server_tester;
ASSERT_SUCCESS(s_tls_local_server_tester_init(
allocator, &local_server_tester, &incoming_args, &c_tester, false, 1, "server.crt", "server.key"));
ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
struct shutdown_listener_tester *shutdown_tester =
aws_mem_acquire(allocator, sizeof(struct shutdown_listener_tester));
shutdown_tester->server_bootstrap = local_server_tester.server_bootstrap;
shutdown_tester->listener = local_server_tester.listener;
shutdown_tester->outgoing_args = &outgoing_args;
/* Use a raw aws_socket for the client, instead of a full-blown TLS channel.
* This lets us hang up on the server, instead of automatically going through with proper TLS negotiation */
ASSERT_SUCCESS(aws_socket_init(&shutdown_tester->client_socket, allocator, &local_server_tester.socket_options));
/* Upon connecting, immediately close the socket */
ASSERT_SUCCESS(aws_socket_connect(
&shutdown_tester->client_socket,
&local_server_tester.endpoint,
aws_event_loop_group_get_next_loop(c_tester.el_group),
s_on_client_connected_do_hangup,
shutdown_tester));
/* Wait for client socket to close */
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_client_socket_closed_predicate, &outgoing_args));
/* Destroy listener socket and wait for shutdown to complete */
aws_server_bootstrap_destroy_socket_listener(shutdown_tester->server_bootstrap, shutdown_tester->listener);
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_listener_destroy_predicate, &incoming_args));
ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
/* clean up */
aws_socket_clean_up(&shutdown_tester->client_socket);
aws_mem_release(allocator, shutdown_tester);
/* cannot double free the listener */
ASSERT_SUCCESS(s_tls_opt_tester_clean_up(&local_server_tester.server_tls_opt_tester));
aws_server_bootstrap_release(local_server_tester.server_bootstrap);
ASSERT_SUCCESS(s_tls_common_tester_clean_up(&c_tester));
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(tls_server_hangup_during_negotiation, s_tls_server_hangup_during_negotiation_fn)
static void s_creation_callback_test_channel_creation_callback(
struct aws_client_bootstrap *bootstrap,
int error_code,
struct aws_channel *channel,
void *user_data) {
(void)bootstrap;
(void)error_code;
struct tls_test_args *setup_test_args = (struct tls_test_args *)user_data;
setup_test_args->creation_callback_invoked = true;
setup_test_args->channel = channel;
struct aws_crt_statistics_handler *stats_handler = aws_statistics_handler_new_test(bootstrap->allocator);
aws_atomic_store_ptr(&c_tester.stats_handler, stats_handler);
aws_channel_set_statistics_handler(channel, stats_handler);
}
static struct aws_event_loop *s_default_new_event_loop(
struct aws_allocator *allocator,
const struct aws_event_loop_options *options,
void *user_data) {
(void)user_data;
return aws_event_loop_new_default_with_options(allocator, options);
}
static int s_statistic_test_clock_fn(uint64_t *timestamp) {
*timestamp = aws_atomic_load_int(&c_tester.current_time_ns);
return AWS_OP_SUCCESS;
}
static int s_tls_common_tester_statistics_init(struct aws_allocator *allocator, struct tls_common_tester *tester) {
aws_io_library_init(allocator);
AWS_ZERO_STRUCT(*tester);
struct aws_mutex mutex = AWS_MUTEX_INIT;
struct aws_condition_variable condition_variable = AWS_CONDITION_VARIABLE_INIT;
tester->mutex = mutex;
tester->condition_variable = condition_variable;
aws_atomic_store_int(&tester->current_time_ns, 0);
aws_atomic_store_ptr(&tester->stats_handler, NULL);
tester->el_group =
aws_event_loop_group_new(allocator, s_statistic_test_clock_fn, 1, s_default_new_event_loop, NULL, NULL);
struct aws_host_resolver_default_options resolver_options = {
.el_group = tester->el_group,
.max_entries = 1,
};
tester->resolver = aws_host_resolver_new_default(allocator, &resolver_options);
return AWS_OP_SUCCESS;
}
static bool s_stats_processed_predicate(void *user_data) {
struct aws_crt_statistics_handler *stats_handler = user_data;
struct aws_statistics_handler_test_impl *stats_impl = stats_handler->impl;
return stats_impl->total_bytes_read > 0 && stats_impl->total_bytes_written > 0 &&
stats_impl->tls_status != AWS_TLS_NEGOTIATION_STATUS_NONE;
}
static int s_tls_channel_statistics_test(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
aws_io_library_init(allocator);
ASSERT_SUCCESS(s_tls_common_tester_statistics_init(allocator, &c_tester));
struct aws_byte_buf read_tag = aws_byte_buf_from_c_str("This is some data.");
struct aws_byte_buf write_tag = aws_byte_buf_from_c_str("Created from a blend of heirloom and cider apples");
uint8_t incoming_received_message[128] = {0};
uint8_t outgoing_received_message[128] = {0};
struct tls_test_rw_args incoming_rw_args;
ASSERT_SUCCESS(s_tls_rw_args_init(
&incoming_rw_args,
&c_tester,
aws_byte_buf_from_empty_array(incoming_received_message, sizeof(incoming_received_message))));
struct tls_test_rw_args outgoing_rw_args;
ASSERT_SUCCESS(s_tls_rw_args_init(
&outgoing_rw_args,
&c_tester,
aws_byte_buf_from_empty_array(outgoing_received_message, sizeof(outgoing_received_message))));
struct tls_test_args outgoing_args;
ASSERT_SUCCESS(s_tls_test_arg_init(allocator, &outgoing_args, false, &c_tester));
struct tls_test_args incoming_args;
ASSERT_SUCCESS(s_tls_test_arg_init(allocator, &incoming_args, true, &c_tester));
struct tls_local_server_tester local_server_tester;
ASSERT_SUCCESS(s_tls_local_server_tester_init(
allocator, &local_server_tester, &incoming_args, &c_tester, false, 1, "server.crt", "server.key"));
struct aws_channel_handler *outgoing_rw_handler =
rw_handler_new(allocator, s_tls_test_handle_read, s_tls_test_handle_write, true, 10000, &outgoing_rw_args);
ASSERT_NOT_NULL(outgoing_rw_handler);
struct aws_channel_handler *incoming_rw_handler =
rw_handler_new(allocator, s_tls_test_handle_read, s_tls_test_handle_write, true, 10000, &incoming_rw_args);
ASSERT_NOT_NULL(incoming_rw_handler);
incoming_args.rw_handler = incoming_rw_handler;
outgoing_args.rw_handler = outgoing_rw_handler;
struct tls_opt_tester client_tls_opt_tester;
struct aws_byte_cursor server_name = aws_byte_cursor_from_c_str("localhost");
ASSERT_SUCCESS(s_tls_client_opt_tester_init(allocator, &client_tls_opt_tester, server_name));
aws_tls_connection_options_set_callbacks(
&client_tls_opt_tester.opt, s_tls_on_negotiated, NULL, NULL, &outgoing_args);
struct aws_client_bootstrap_options bootstrap_options;
AWS_ZERO_STRUCT(bootstrap_options);
bootstrap_options.event_loop_group = c_tester.el_group;
bootstrap_options.host_resolver = c_tester.resolver;
struct aws_client_bootstrap *client_bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
struct aws_socket_channel_bootstrap_options channel_options;
AWS_ZERO_STRUCT(channel_options);
channel_options.bootstrap = client_bootstrap;
channel_options.host_name = local_server_tester.endpoint.address;
channel_options.port = 0;
channel_options.socket_options = &local_server_tester.socket_options;
channel_options.tls_options = &client_tls_opt_tester.opt;
channel_options.creation_callback = s_creation_callback_test_channel_creation_callback;
channel_options.setup_callback = s_tls_handler_test_client_setup_callback;
channel_options.shutdown_callback = s_tls_handler_test_client_shutdown_callback;
channel_options.user_data = &outgoing_args;
ASSERT_SUCCESS(aws_client_bootstrap_new_socket_channel(&channel_options));
/* put this here to verify ownership semantics are correct. This should NOT cause a segfault. If it does, ya
* done messed up. */
aws_tls_connection_options_clean_up(&client_tls_opt_tester.opt);
/* wait for both ends to setup */
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_channel_setup_predicate, &incoming_args));
ASSERT_FALSE(incoming_args.error_invoked);
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_channel_setup_predicate, &outgoing_args));
ASSERT_FALSE(outgoing_args.error_invoked);
ASSERT_TRUE(outgoing_args.creation_callback_invoked);
/* Do the IO operations */
rw_handler_write(outgoing_args.rw_handler, outgoing_args.rw_slot, &write_tag);
rw_handler_write(incoming_args.rw_handler, incoming_args.rw_slot, &read_tag);
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_test_read_predicate, &incoming_rw_args));
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_test_read_predicate, &outgoing_rw_args));
uint64_t ms_to_ns = aws_timestamp_convert(1, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL);
aws_atomic_store_int(&c_tester.current_time_ns, (size_t)ms_to_ns);
struct aws_crt_statistics_handler *stats_handler = aws_atomic_load_ptr(&c_tester.stats_handler);
struct aws_statistics_handler_test_impl *stats_impl = stats_handler->impl;
aws_mutex_lock(&stats_impl->lock);
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&stats_impl->signal, &stats_impl->lock, s_stats_processed_predicate, stats_handler));
ASSERT_TRUE(stats_impl->total_bytes_read >= read_tag.len);
ASSERT_TRUE(stats_impl->total_bytes_written >= write_tag.len);
ASSERT_TRUE(stats_impl->tls_status == AWS_TLS_NEGOTIATION_STATUS_SUCCESS);
aws_mutex_unlock(&stats_impl->lock);
aws_channel_shutdown(incoming_args.channel, AWS_OP_SUCCESS);
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_channel_shutdown_predicate, &incoming_args));
/*no shutdown on the client necessary here (it should have been triggered by shutting down the other side). just
* wait for the event to fire. */
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_channel_shutdown_predicate, &outgoing_args));
aws_server_bootstrap_destroy_socket_listener(local_server_tester.server_bootstrap, local_server_tester.listener);
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_listener_destroy_predicate, &incoming_args));
aws_mutex_unlock(&c_tester.mutex);
/* clean up */
ASSERT_SUCCESS(s_tls_opt_tester_clean_up(&client_tls_opt_tester));
ASSERT_SUCCESS(s_tls_local_server_tester_clean_up(&local_server_tester));
aws_client_bootstrap_release(client_bootstrap);
ASSERT_SUCCESS(s_tls_common_tester_clean_up(&c_tester));
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(tls_channel_statistics_test, s_tls_channel_statistics_test)
static int s_tls_certificate_chain_test(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
aws_io_library_init(allocator);
ASSERT_SUCCESS(s_tls_common_tester_init(allocator, &c_tester));
struct tls_test_args outgoing_args;
ASSERT_SUCCESS(s_tls_test_arg_init(allocator, &outgoing_args, false, &c_tester));
struct tls_test_args incoming_args;
ASSERT_SUCCESS(s_tls_test_arg_init(allocator, &incoming_args, true, &c_tester));
struct tls_local_server_tester local_server_tester;
ASSERT_SUCCESS(s_tls_local_server_tester_init(
allocator, &local_server_tester, &incoming_args, &c_tester, false, 1, "server_chain.crt", "server.key"));
struct tls_opt_tester client_tls_opt_tester;
struct aws_byte_cursor server_name = aws_byte_cursor_from_c_str("localhost");
ASSERT_SUCCESS(s_tls_client_opt_tester_init(allocator, &client_tls_opt_tester, server_name));
aws_tls_connection_options_set_callbacks(
&client_tls_opt_tester.opt, s_tls_on_negotiated, NULL, NULL, &outgoing_args);
struct aws_client_bootstrap_options bootstrap_options = {
.event_loop_group = c_tester.el_group,
.host_resolver = c_tester.resolver,
};
struct aws_client_bootstrap *client_bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
struct aws_socket_channel_bootstrap_options channel_options;
AWS_ZERO_STRUCT(channel_options);
channel_options.bootstrap = client_bootstrap;
channel_options.host_name = local_server_tester.endpoint.address;
channel_options.port = 0;
channel_options.socket_options = &local_server_tester.socket_options;
channel_options.tls_options = &client_tls_opt_tester.opt;
channel_options.setup_callback = s_tls_handler_test_client_setup_callback;
channel_options.shutdown_callback = s_tls_handler_test_client_shutdown_callback;
channel_options.user_data = &outgoing_args;
/* connect! */
ASSERT_SUCCESS(aws_client_bootstrap_new_socket_channel(&channel_options));
/* wait for both ends to setup */
ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_channel_setup_predicate, &incoming_args));
ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
ASSERT_FALSE(incoming_args.error_invoked);
/* shut down */
aws_channel_shutdown(incoming_args.channel, AWS_OP_SUCCESS);
ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_channel_shutdown_predicate, &incoming_args));
ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
/* no shutdown on the client necessary here (it should have been triggered by shutting down the other side). just
* wait for the event to fire. */
ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_channel_shutdown_predicate, &outgoing_args));
ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
/* clean up */
aws_server_bootstrap_destroy_socket_listener(local_server_tester.server_bootstrap, local_server_tester.listener);
ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&c_tester.condition_variable, &c_tester.mutex, s_tls_listener_destroy_predicate, &incoming_args));
aws_mutex_unlock(&c_tester.mutex);
ASSERT_SUCCESS(s_tls_opt_tester_clean_up(&client_tls_opt_tester));
aws_client_bootstrap_release(client_bootstrap);
ASSERT_SUCCESS(s_tls_local_server_tester_clean_up(&local_server_tester));
ASSERT_SUCCESS(s_tls_common_tester_clean_up(&c_tester));
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(tls_certificate_chain_test, s_tls_certificate_chain_test)
///////////////////////////////////////////////////////////////
struct channel_stat_test_context {
struct aws_allocator *allocator;
struct tls_opt_tester *tls_tester;
struct aws_mutex lock;
struct aws_condition_variable signal;
bool setup_completed;
bool shutdown_completed;
int error_code;
};
static void s_channel_setup_stat_test_context_init(
struct channel_stat_test_context *context,
struct aws_allocator *allocator,
struct tls_opt_tester *tls_tester) {
AWS_ZERO_STRUCT(*context);
aws_mutex_init(&context->lock);
aws_condition_variable_init(&context->signal);
context->allocator = allocator;
context->tls_tester = tls_tester;
}
static void s_channel_setup_stat_test_context_clean_up(struct channel_stat_test_context *context) {
aws_mutex_clean_up(&context->lock);
aws_condition_variable_clean_up(&context->signal);
}
static int s_dummy_process_message(
struct aws_channel_handler *handler,
struct aws_channel_slot *slot,
struct aws_io_message *message) {
(void)handler;
(void)slot;
aws_mem_release(message->allocator, message);
return AWS_OP_SUCCESS;
}
static int s_dummy_increment_read_window(
struct aws_channel_handler *handler,
struct aws_channel_slot *slot,
size_t size) {
(void)handler;
(void)slot;
(void)size;
return AWS_OP_SUCCESS;
}
static int s_dummy_shutdown(
struct aws_channel_handler *handler,
struct aws_channel_slot *slot,
enum aws_channel_direction dir,
int error_code,
bool free_scarce_resources_immediately) {
(void)handler;
return aws_channel_slot_on_handler_shutdown_complete(slot, dir, error_code, free_scarce_resources_immediately);
}
static size_t s_dummy_initial_window_size(struct aws_channel_handler *handler) {
(void)handler;
return 10000;
}
static size_t s_dummy_message_overhead(struct aws_channel_handler *handler) {
(void)handler;
return 0;
}
static void s_dummy_destroy(struct aws_channel_handler *handler) {
aws_mem_release(handler->alloc, handler);
}
static struct aws_channel_handler_vtable s_dummy_handler_vtable = {
.process_read_message = s_dummy_process_message,
.process_write_message = s_dummy_process_message,
.increment_read_window = s_dummy_increment_read_window,
.shutdown = s_dummy_shutdown,
.initial_window_size = s_dummy_initial_window_size,
.message_overhead = s_dummy_message_overhead,
.destroy = s_dummy_destroy,
};
static struct aws_channel_handler *aws_channel_handler_new_dummy(struct aws_allocator *allocator) {
struct aws_channel_handler *handler = aws_mem_acquire(allocator, sizeof(struct aws_channel_handler));
handler->alloc = allocator;
handler->vtable = &s_dummy_handler_vtable;
handler->impl = NULL;
return handler;
}
static bool s_setup_completed_predicate(void *arg) {
struct channel_stat_test_context *context = (struct channel_stat_test_context *)arg;
return context->setup_completed;
}
static bool s_shutdown_completed_predicate(void *arg) {
struct channel_stat_test_context *context = (struct channel_stat_test_context *)arg;
return context->shutdown_completed;
}
static void s_on_shutdown_completed(struct aws_channel *channel, int error_code, void *user_data) {
(void)channel;
struct channel_stat_test_context *context = (struct channel_stat_test_context *)user_data;
aws_mutex_lock(&context->lock);
context->shutdown_completed = true;
context->error_code = error_code;
aws_mutex_unlock(&context->lock);
aws_condition_variable_notify_one(&context->signal);
}
static const int s_tls_timeout_ms = 1000;
static void s_on_setup_completed(struct aws_channel *channel, int error_code, void *user_data) {
(void)channel;
struct channel_stat_test_context *context = (struct channel_stat_test_context *)user_data;
/* attach a dummy channel handler */
struct aws_channel_slot *dummy_slot = aws_channel_slot_new(channel);
struct aws_channel_handler *dummy_handler = aws_channel_handler_new_dummy(context->allocator);
aws_channel_slot_set_handler(dummy_slot, dummy_handler);
/* attach a tls channel handler and start negotiation */
aws_channel_setup_client_tls(dummy_slot, &context->tls_tester->opt);
aws_mutex_lock(&context->lock);
context->error_code = error_code;
context->setup_completed = true;
aws_mutex_unlock(&context->lock);
aws_condition_variable_notify_one(&context->signal);
}
static int s_test_tls_negotiation_timeout(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
aws_io_library_init(allocator);
struct aws_event_loop *event_loop = aws_event_loop_new_default(allocator, aws_high_res_clock_get_ticks);
ASSERT_SUCCESS(aws_event_loop_run(event_loop));
struct tls_opt_tester tls_test_context;
s_tls_client_opt_tester_init(allocator, &tls_test_context, aws_byte_cursor_from_c_str("derp.com"));
tls_test_context.opt.timeout_ms = s_tls_timeout_ms;
struct channel_stat_test_context channel_context;
s_channel_setup_stat_test_context_init(&channel_context, allocator, &tls_test_context);
struct aws_channel_options args = {
.on_setup_completed = s_on_setup_completed,
.setup_user_data = &channel_context,
.on_shutdown_completed = s_on_shutdown_completed,
.shutdown_user_data = &channel_context,
.event_loop = event_loop,
};
/* set up the channel */
ASSERT_SUCCESS(aws_mutex_lock(&channel_context.lock));
struct aws_channel *channel = aws_channel_new(allocator, &args);
ASSERT_NOT_NULL(channel);
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&channel_context.signal, &channel_context.lock, s_setup_completed_predicate, &channel_context));
aws_mutex_unlock(&channel_context.lock);
/* wait for the timeout */
aws_thread_current_sleep(aws_timestamp_convert(s_tls_timeout_ms, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL));
aws_mutex_lock(&channel_context.lock);
ASSERT_SUCCESS(aws_condition_variable_wait_pred(
&channel_context.signal, &channel_context.lock, s_shutdown_completed_predicate, &channel_context));
ASSERT_TRUE(channel_context.error_code == AWS_IO_TLS_NEGOTIATION_TIMEOUT);
aws_mutex_unlock(&channel_context.lock);
aws_channel_destroy(channel);
aws_event_loop_destroy(event_loop);
s_tls_opt_tester_clean_up(&tls_test_context);
s_channel_setup_stat_test_context_clean_up(&channel_context);
aws_io_library_clean_up();
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(test_tls_negotiation_timeout, s_test_tls_negotiation_timeout)
struct import_info {
struct aws_allocator *allocator;
struct aws_byte_buf cert_buf;
struct aws_byte_buf key_buf;
struct aws_thread thread;
struct aws_tls_ctx *tls;
};
static void s_import_cert(void *ctx) {
(void)ctx;
# if !defined(AWS_OS_IOS)
struct import_info *import = ctx;
struct aws_byte_cursor cert_cur = aws_byte_cursor_from_buf(&import->cert_buf);
struct aws_byte_cursor key_cur = aws_byte_cursor_from_buf(&import->key_buf);
struct aws_tls_ctx_options tls_options = {0};
AWS_FATAL_ASSERT(
AWS_OP_SUCCESS == aws_tls_ctx_options_init_client_mtls(&tls_options, import->allocator, &cert_cur, &key_cur));
/* import happens in here */
import->tls = aws_tls_client_ctx_new(import->allocator, &tls_options);
AWS_FATAL_ASSERT(import->tls);
aws_tls_ctx_options_clean_up(&tls_options);
# endif /* !AWS_OS_IOS */
}
# define NUM_PAIRS 1
static int s_test_concurrent_cert_import(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
/* temporarily disable this on apple until we can fix importing to be more robust */
/* temporarily disable this on linux until we can make CRYPTO_zalloc behave and stop angering ASan */
# if defined(__APPLE__) || defined(__linux__)
return AWS_OP_SUCCESS;
# endif
aws_io_library_init(allocator);
AWS_VARIABLE_LENGTH_ARRAY(struct import_info, imports, NUM_PAIRS);
/* setup, note that all I/O should be before the threads are launched */
for (size_t idx = 0; idx < NUM_PAIRS; ++idx) {
struct import_info *import = &imports[idx];
import->allocator = allocator;
char filename[1024];
snprintf(filename, sizeof(filename), "testcert%u.pem", (uint32_t)idx);
ASSERT_SUCCESS(aws_byte_buf_init_from_file(&import->cert_buf, import->allocator, filename));
snprintf(filename, sizeof(filename), "testkey.pem");
ASSERT_SUCCESS(aws_byte_buf_init_from_file(&import->key_buf, import->allocator, filename));
struct aws_thread *thread = &import->thread;
ASSERT_SUCCESS(aws_thread_init(thread, allocator));
}
/* run threads */
const struct aws_thread_options *options = aws_default_thread_options();
for (size_t idx = 0; idx < NUM_PAIRS; ++idx) {
struct import_info *import = &imports[idx];
struct aws_thread *thread = &import->thread;
ASSERT_SUCCESS(aws_thread_launch(thread, s_import_cert, import, options));
}
/* join and clean up */
for (size_t idx = 0; idx < NUM_PAIRS; ++idx) {
struct import_info *import = &imports[idx];
struct aws_thread *thread = &import->thread;
ASSERT_SUCCESS(aws_thread_join(thread));
aws_tls_ctx_release(import->tls);
aws_byte_buf_clean_up(&import->cert_buf);
aws_byte_buf_clean_up(&import->key_buf);
}
aws_io_library_clean_up();
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(test_concurrent_cert_import, s_test_concurrent_cert_import)
static int s_tls_destroy_null_context(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
(void)ctx;
struct aws_tls_ctx *null_context = NULL;
/* Verify that we don't crash. */
aws_tls_ctx_release(null_context);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(tls_destroy_null_context, s_tls_destroy_null_context);
static int s_test_ecc_cert_import(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
(void)allocator;
# ifndef AWS_OS_APPLE
aws_io_library_init(allocator);
struct aws_byte_buf cert_buf;
struct aws_byte_buf key_buf;
ASSERT_SUCCESS(aws_byte_buf_init_from_file(&cert_buf, allocator, "ecc-cert.pem"));
ASSERT_SUCCESS(aws_byte_buf_init_from_file(&key_buf, allocator, "ecc-key.pem"));
struct aws_byte_cursor cert_cur = aws_byte_cursor_from_buf(&cert_buf);
struct aws_byte_cursor key_cur = aws_byte_cursor_from_buf(&key_buf);
struct aws_tls_ctx_options tls_options = {0};
AWS_FATAL_ASSERT(
AWS_OP_SUCCESS == aws_tls_ctx_options_init_client_mtls(&tls_options, allocator, &cert_cur, &key_cur));
/* import happens in here */
struct aws_tls_ctx *tls_context = aws_tls_client_ctx_new(allocator, &tls_options);
ASSERT_NOT_NULL(tls_context);
aws_tls_ctx_release(tls_context);
aws_tls_ctx_options_clean_up(&tls_options);
aws_byte_buf_clean_up(&cert_buf);
aws_byte_buf_clean_up(&key_buf);
aws_io_library_clean_up();
# endif /* AWS_OS_APPLE */
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(test_ecc_cert_import, s_test_ecc_cert_import)
#endif /* BYO_CRYPTO */
|