Debian Codegraph @

crt/aws-c-io/tests/tls_handler_test.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #ifndef BYO_CRYPTO
7
8 # include <aws/io/channel_bootstrap.h>
9 # include <aws/io/event_loop.h>
10 # include <aws/io/file_utils.h>
11 # include <aws/io/host_resolver.h>
12 # include <aws/io/logging.h>
13 # include <aws/io/private/event_loop_impl.h>
14 # include <aws/io/socket.h>
15 # include <aws/io/tls_channel_handler.h>
16
17 # include <aws/common/clock.h>
18 # include <aws/common/condition_variable.h>
19 # include <aws/common/environment.h>
20 # include <aws/common/thread.h>
21
22 # include <aws/testing/aws_test_harness.h>
23
24 # include <aws/common/string.h>
25 # include <read_write_test_handler.h>
26 # include <statistics_handler_test.h>
27
28 # include <aws/io/private/pki_utils.h>
29 # include <aws/io/private/tls_channel_handler_private.h>
30
31 /* badssl.com has occasional lags, make this timeout longer so we have a
32 * higher chance of actually testing something. */
33 # define BADSSL_TIMEOUT_MS 10000
34
35 # define AWS_TEST_LOCAL_TLS13_PORT 59443
36 # define AWS_TEST_LOCAL_UNTRUSTED_TLS_PORT 60443
37
38 bool s_is_badssl_being_flaky(const struct aws_string *host_name, int error_code) {
39 if (strstr(aws_string_c_str(host_name), "badssl.com") != NULL) {
40 if (error_code == AWS_IO_SOCKET_TIMEOUT || error_code == AWS_IO_TLS_NEGOTIATION_TIMEOUT ||
41 error_code == AWS_IO_SOCKET_CLOSED) {
42 fprintf(AWS_TESTING_REPORT_FD, "Warning: badssl.com is flaky. Allow the transiet error to happen\n");
43 return true;
44 }
45 }
46 return false;
47 }
48
49 bool s_is_apple_with_secure_transport(struct aws_allocator *allocator) {
50 (void)allocator;
51 # if defined(__APPLE__) && defined(USE_S2N)
52 /* When s2n is compiled in, AWS_CRT_USE_NON_FIPS_TLS_13 selects s2n over Secure Transport. */
53 struct aws_string *use_non_fips_13 = aws_get_env_nonempty(allocator, "AWS_CRT_USE_NON_FIPS_TLS_13");
54 if (use_non_fips_13) {
55 aws_string_destroy(use_non_fips_13);
56 return false;
57 }
58 return true;
59 # elif defined(__APPLE__)
60 /* Without s2n compiled in, Secure Transport is always used and the env var has no effect. */
61 return true;
62 # else
63 return false;
64 # endif
65 }
66
67 struct tls_test_args {
68 struct aws_allocator *allocator;
69 struct aws_mutex *mutex;
70 struct aws_condition_variable *condition_variable;
71 struct aws_tls_connection_options *tls_options;
72 struct aws_channel *channel;
73 struct aws_channel_handler *rw_handler;
74 struct aws_channel_slot *rw_slot;
75 struct aws_byte_buf negotiated_protocol;
76 struct aws_byte_buf server_name;
77 int last_error_code;
78
79 uint32_t tls_levels_negotiated;
80 uint32_t desired_tls_levels;
81
82 bool listener_destroyed;
83 bool error_invoked;
84 bool expects_error;
85 bool server;
86 bool shutdown_finished;
87 bool socket_close_invoked;
88 bool setup_callback_invoked;
89 bool creation_callback_invoked;
90 };
91
92 /* common structure for tls options */
93 struct tls_opt_tester {
94 struct aws_tls_ctx_options ctx_options;
95 struct aws_tls_ctx *ctx;
96 struct aws_tls_connection_options opt;
97 };
98
99 static int s_tls_server_opt_tester_init(
100 struct aws_allocator *allocator,
101 struct tls_opt_tester *tester,
102 const char *cert_path,
103 const char *pkey_path) {
104
105 if (s_is_apple_with_secure_transport(allocator)) {
106 struct aws_byte_cursor pwd_cur = aws_byte_cursor_from_c_str("1234");
107 ASSERT_SUCCESS(aws_tls_ctx_options_init_server_pkcs12_from_path(
108 &tester->ctx_options, allocator, "unittests.p12", &pwd_cur));
109 } else {
110 ASSERT_SUCCESS(
111 aws_tls_ctx_options_init_default_server_from_path(&tester->ctx_options, allocator, cert_path, pkey_path));
112 ASSERT_SUCCESS(
113 aws_tls_ctx_options_override_default_trust_store_from_path(&tester->ctx_options, NULL, "ca_root.crt"));
114 }
115
116 aws_tls_ctx_options_set_alpn_list(&tester->ctx_options, "h2;http/1.1");
117 tester->ctx = aws_tls_server_ctx_new(allocator, &tester->ctx_options);
118 ASSERT_NOT_NULL(tester->ctx);
119
120 aws_tls_connection_options_init_from_ctx(&tester->opt, tester->ctx);
121 return AWS_OP_SUCCESS;
122 }
123
124 static int s_tls_client_opt_tester_init(
125 struct aws_allocator *allocator,
126 struct tls_opt_tester *tester,
127 struct aws_byte_cursor server_name) {
128
129 aws_io_library_init(allocator);
130
131 aws_tls_ctx_options_init_default_client(&tester->ctx_options, allocator);
132
133 if (s_is_apple_with_secure_transport(allocator)) {
134 ASSERT_SUCCESS(
135 aws_tls_ctx_options_override_default_trust_store_from_path(&tester->ctx_options, NULL, "unittests.crt"));
136 } else {
137 ASSERT_SUCCESS(
138 aws_tls_ctx_options_override_default_trust_store_from_path(&tester->ctx_options, NULL, "ca_root.crt"));
139 }
140
141 tester->ctx = aws_tls_client_ctx_new(allocator, &tester->ctx_options);
142 aws_tls_connection_options_init_from_ctx(&tester->opt, tester->ctx);
143 aws_tls_connection_options_set_alpn_list(&tester->opt, allocator, "h2;http/1.1");
144
145 aws_tls_connection_options_set_server_name(&tester->opt, allocator, &server_name);
146
147 return AWS_OP_SUCCESS;
148 }
149
150 static int s_tls_opt_tester_clean_up(struct tls_opt_tester *tester) {
151 aws_tls_connection_options_clean_up(&tester->opt);
152 aws_tls_ctx_options_clean_up(&tester->ctx_options);
153 aws_tls_ctx_release(tester->ctx);
154 return AWS_OP_SUCCESS;
155 }
156
157 /* common structure for test */
158 struct tls_common_tester {
159 struct aws_mutex mutex;
160 struct aws_condition_variable condition_variable;
161 struct aws_event_loop_group *el_group;
162 struct aws_host_resolver *resolver;
163 struct aws_atomic_var current_time_ns;
164 struct aws_atomic_var stats_handler;
165 };
166
167 static struct tls_common_tester c_tester;
168
169 /* common structure for a tls local server */
170 struct tls_local_server_tester {
171 struct aws_socket_options socket_options;
172 struct tls_opt_tester server_tls_opt_tester;
173 struct aws_socket_endpoint endpoint;
174 struct aws_server_bootstrap *server_bootstrap;
175 struct aws_socket *listener;
176 };
177
178 static int s_tls_test_arg_init(
179 struct aws_allocator *allocator,
180 struct tls_test_args *test_arg,
181 bool server,
182 struct tls_common_tester *tls_c_tester) {
183 AWS_ZERO_STRUCT(*test_arg);
184 test_arg->mutex = &tls_c_tester->mutex;
185 test_arg->condition_variable = &tls_c_tester->condition_variable;
186 test_arg->allocator = allocator;
187 test_arg->server = server;
188 test_arg->desired_tls_levels = 1;
189
190 return AWS_OP_SUCCESS;
191 }
192
193 static int s_tls_common_tester_init(struct aws_allocator *allocator, struct tls_common_tester *tester) {
194 aws_io_library_init(allocator);
195 AWS_ZERO_STRUCT(*tester);
196
197 struct aws_mutex mutex = AWS_MUTEX_INIT;
198 struct aws_condition_variable condition_variable = AWS_CONDITION_VARIABLE_INIT;
199 tester->mutex = mutex;
200 tester->condition_variable = condition_variable;
201 aws_atomic_store_int(&tester->current_time_ns, 0);
202 aws_atomic_store_ptr(&tester->stats_handler, NULL);
203
204 struct aws_event_loop_group_options elg_options = {
205 .loop_count = 0,
206 };
207 tester->el_group = aws_event_loop_group_new(allocator, &elg_options);
208
209 struct aws_host_resolver_default_options resolver_options = {
210 .el_group = tester->el_group,
211 .max_entries = 1,
212 };
213 tester->resolver = aws_host_resolver_new_default(allocator, &resolver_options);
214
215 return AWS_OP_SUCCESS;
216 }
217
218 static int s_tls_common_tester_clean_up(struct tls_common_tester *tester) {
219 aws_host_resolver_release(tester->resolver);
220 aws_event_loop_group_release(tester->el_group);
221
222 aws_io_library_clean_up();
223
224 aws_condition_variable_clean_up(&tester->condition_variable);
225 aws_mutex_clean_up(&tester->mutex);
226 return AWS_OP_SUCCESS;
227 }
228
229 static bool s_tls_channel_shutdown_predicate(void *user_data) {
230 struct tls_test_args *setup_test_args = user_data;
231 return setup_test_args->shutdown_finished || setup_test_args->last_error_code == AWS_IO_SOCKET_TIMEOUT ||
232 (setup_test_args->expects_error && setup_test_args->error_invoked);
233 }
234
235 static bool s_tls_listener_destroy_predicate(void *user_data) {
236 struct tls_test_args *setup_test_args = user_data;
237 return setup_test_args->listener_destroyed || setup_test_args->last_error_code == AWS_IO_SOCKET_TIMEOUT;
238 }
239
240 static bool s_tls_channel_setup_predicate(void *user_data) {
241 struct tls_test_args *setup_test_args = user_data;
242 return (setup_test_args->tls_levels_negotiated == setup_test_args->desired_tls_levels &&
243 setup_test_args->setup_callback_invoked) ||
244 setup_test_args->error_invoked;
245 }
246
247 /*
248 * test args mutex must be held before calling this function
249 */
250 static void s_aws_check_for_user_handler_setup(struct tls_test_args *setup_test_args) {
251 if (setup_test_args->tls_levels_negotiated == setup_test_args->desired_tls_levels &&
252 setup_test_args->setup_callback_invoked) {
253 if (setup_test_args->rw_handler) {
254 struct aws_channel *channel = setup_test_args->channel;
255 struct aws_channel_slot *rw_slot = aws_channel_slot_new(channel);
256 aws_channel_slot_insert_end(channel, rw_slot);
257 aws_channel_slot_set_handler(rw_slot, setup_test_args->rw_handler);
258 setup_test_args->rw_slot = rw_slot;
259 }
260 }
261 }
262
263 static int s_add_tls_handler_to_end_of_channel(struct tls_test_args *setup_test_args) {
264 AWS_FATAL_ASSERT(setup_test_args->desired_tls_levels > 1);
265 AWS_FATAL_ASSERT(!setup_test_args->server);
266
267 struct aws_channel_slot *last_slot = aws_channel_get_first_slot(setup_test_args->channel);
268 while (last_slot->adj_right) {
269 last_slot = last_slot->adj_right;
270 }
271
272 return aws_channel_setup_client_tls(last_slot, setup_test_args->tls_options);
273 }
274
275 static int s_on_channel_setup_next_tls_handler(struct tls_test_args *setup_test_args) {
276 if (setup_test_args->tls_levels_negotiated < setup_test_args->desired_tls_levels) {
277 ASSERT_SUCCESS(s_add_tls_handler_to_end_of_channel(setup_test_args));
278 }
279
280 return AWS_OP_SUCCESS;
281 }
282
283 static int s_on_tls_negotiated_next_tls_handler(struct tls_test_args *setup_test_args) {
284 if (!setup_test_args->setup_callback_invoked) {
285 return AWS_OP_SUCCESS;
286 }
287
288 if (setup_test_args->tls_levels_negotiated < setup_test_args->desired_tls_levels) {
289 ASSERT_SUCCESS(s_add_tls_handler_to_end_of_channel(setup_test_args));
290 }
291
292 return AWS_OP_SUCCESS;
293 }
294
295 static void s_tls_handler_test_client_setup_callback(
296 struct aws_client_bootstrap *bootstrap,
297 int error_code,
298 struct aws_channel *channel,
299 void *user_data) {
300
301 (void)bootstrap;
302
303 struct tls_test_args *setup_test_args = user_data;
304 aws_mutex_lock(setup_test_args->mutex);
305
306 setup_test_args->setup_callback_invoked = true;
307
308 if (!error_code) {
309 setup_test_args->channel = channel;
310 s_aws_check_for_user_handler_setup(setup_test_args);
311 s_on_channel_setup_next_tls_handler(setup_test_args);
312 } else {
313 setup_test_args->error_invoked = true;
314 setup_test_args->last_error_code = error_code;
315 }
316
317 aws_mutex_unlock(setup_test_args->mutex);
318 aws_condition_variable_notify_one(setup_test_args->condition_variable);
319 }
320
321 static void s_tls_handler_test_server_setup_callback(
322 struct aws_server_bootstrap *bootstrap,
323 int error_code,
324 struct aws_channel *channel,
325 void *user_data) {
326
327 (void)bootstrap;
328
329 struct tls_test_args *setup_test_args = (struct tls_test_args *)user_data;
330
331 aws_mutex_lock(setup_test_args->mutex);
332 setup_test_args->setup_callback_invoked = true;
333 if (!error_code) {
334 setup_test_args->channel = channel;
335 } else {
336 setup_test_args->error_invoked = true;
337 setup_test_args->last_error_code = error_code;
338 }
339
340 s_aws_check_for_user_handler_setup(setup_test_args);
341
342 aws_mutex_unlock(setup_test_args->mutex);
343 aws_condition_variable_notify_one(setup_test_args->condition_variable);
344 }
345
346 static void s_tls_handler_test_client_shutdown_callback(
347 struct aws_client_bootstrap *bootstrap,
348 int error_code,
349 struct aws_channel *channel,
350 void *user_data) {
351
352 (void)bootstrap;
353 (void)error_code;
354 (void)channel;
355
356 struct tls_test_args *setup_test_args = (struct tls_test_args *)user_data;
357
358 aws_mutex_lock(setup_test_args->mutex);
359 setup_test_args->shutdown_finished = true;
360 if (error_code) {
361 setup_test_args->last_error_code = error_code;
362 }
363 aws_mutex_unlock(setup_test_args->mutex);
364 aws_condition_variable_notify_one(setup_test_args->condition_variable);
365 }
366
367 static void s_tls_handler_test_server_shutdown_callback(
368 struct aws_server_bootstrap *bootstrap,
369 int error_code,
370 struct aws_channel *channel,
371 void *user_data) {
372
373 (void)bootstrap;
374 (void)error_code;
375 (void)channel;
376
377 struct tls_test_args *setup_test_args = (struct tls_test_args *)user_data;
378
379 aws_mutex_lock(setup_test_args->mutex);
380 setup_test_args->shutdown_finished = true;
381 if (error_code) {
382 setup_test_args->last_error_code = error_code;
383 }
384 aws_condition_variable_notify_one(setup_test_args->condition_variable);
385 aws_mutex_unlock(setup_test_args->mutex);
386 }
387
388 static void s_tls_handler_test_server_listener_destroy_callback(
389 struct aws_server_bootstrap *bootstrap,
390 void *user_data) {
391 (void)bootstrap;
392
393 struct tls_test_args *setup_test_args = (struct tls_test_args *)user_data;
394 aws_mutex_lock(setup_test_args->mutex);
395 setup_test_args->listener_destroyed = true;
396 aws_condition_variable_notify_all(setup_test_args->condition_variable);
397 aws_mutex_unlock(setup_test_args->mutex);
398 }
399
400 static void s_tls_on_negotiated(
401 struct aws_channel_handler *handler,
402 struct aws_channel_slot *slot,
403 int err_code,
404 void *user_data) {
405
406 (void)slot;
407 struct tls_test_args *setup_test_args = (struct tls_test_args *)user_data;
408
409 if (!err_code) {
410 aws_mutex_lock(setup_test_args->mutex);
411
412 if (aws_tls_is_alpn_available()) {
413 setup_test_args->negotiated_protocol = aws_tls_handler_protocol(handler);
414 }
415 setup_test_args->server_name = aws_tls_handler_server_name(handler);
416 ++setup_test_args->tls_levels_negotiated;
417
418 s_aws_check_for_user_handler_setup(setup_test_args);
419 s_on_tls_negotiated_next_tls_handler(setup_test_args);
420
421 aws_mutex_unlock(setup_test_args->mutex);
422 }
423
424 aws_condition_variable_notify_one(setup_test_args->condition_variable);
425 }
426
427 static int s_tls_local_server_tester_init(
428 struct aws_allocator *allocator,
429 struct tls_local_server_tester *tester,
430 struct tls_test_args *args,
431 struct tls_common_tester *tls_c_tester,
432 bool enable_back_pressure,
433 const char *cert_path,
434 const char *pkey_path) {
435 AWS_ZERO_STRUCT(*tester);
436 ASSERT_SUCCESS(s_tls_server_opt_tester_init(allocator, &tester->server_tls_opt_tester, cert_path, pkey_path));
437 aws_tls_connection_options_set_callbacks(&tester->server_tls_opt_tester.opt, s_tls_on_negotiated, NULL, NULL, args);
438 tester->socket_options.connect_timeout_ms = 3000;
439 tester->socket_options.type = AWS_SOCKET_STREAM;
440 tester->socket_options.domain = AWS_SOCKET_LOCAL;
441
442 aws_socket_endpoint_init_local_address_for_test(&tester->endpoint);
443
444 tester->server_bootstrap = aws_server_bootstrap_new(allocator, tls_c_tester->el_group);
445 ASSERT_NOT_NULL(tester->server_bootstrap);
446
447 struct aws_server_socket_channel_bootstrap_options bootstrap_options = {
448 .bootstrap = tester->server_bootstrap,
449 .enable_read_back_pressure = enable_back_pressure,
450 .port = tester->endpoint.port,
451 .host_name = tester->endpoint.address,
452 .socket_options = &tester->socket_options,
453 .incoming_callback = s_tls_handler_test_server_setup_callback,
454 .shutdown_callback = s_tls_handler_test_server_shutdown_callback,
455 .destroy_callback = s_tls_handler_test_server_listener_destroy_callback,
456 .tls_options = &tester->server_tls_opt_tester.opt,
457 .user_data = args,
458 };
459 tester->listener = aws_server_bootstrap_new_socket_listener(&bootstrap_options);
460 ASSERT_NOT_NULL(tester->listener);
461
462 return AWS_OP_SUCCESS;
463 }
464
465 static int s_tls_local_server_tester_clean_up(struct tls_local_server_tester *tester) {
466 ASSERT_SUCCESS(s_tls_opt_tester_clean_up(&tester->server_tls_opt_tester));
467 aws_server_bootstrap_release(tester->server_bootstrap);
468 return AWS_OP_SUCCESS;
469 }
470
471 struct tls_test_rw_args {
472 struct aws_mutex *mutex;
473 struct aws_condition_variable *condition_variable;
474 struct aws_byte_buf received_message;
475 int read_invocations;
476 bool invocation_happened;
477 };
478
479 static int s_tls_rw_args_init(
480 struct tls_test_rw_args *args,
481 struct tls_common_tester *tls_c_tester,
482 struct aws_byte_buf received_message) {
483 AWS_ZERO_STRUCT(*args);
484 args->mutex = &tls_c_tester->mutex;
485 args->condition_variable = &tls_c_tester->condition_variable;
486 args->received_message = received_message;
487 return AWS_OP_SUCCESS;
488 }
489
490 static bool s_tls_test_read_predicate(void *user_data) {
491 struct tls_test_rw_args *rw_args = (struct tls_test_rw_args *)user_data;
492
493 return rw_args->invocation_happened;
494 }
495
496 static struct aws_byte_buf s_tls_test_handle_read(
497 struct aws_channel_handler *handler,
498 struct aws_channel_slot *slot,
499 struct aws_byte_buf *data_read,
500 void *user_data) {
501
502 (void)handler;
503 (void)slot;
504
505 struct tls_test_rw_args *rw_args = (struct tls_test_rw_args *)user_data;
506 aws_mutex_lock(rw_args->mutex);
507
508 aws_byte_buf_write_from_whole_buffer(&rw_args->received_message, *data_read);
509 rw_args->read_invocations += 1;
510 rw_args->invocation_happened = true;
511
512 aws_mutex_unlock(rw_args->mutex);
513 aws_condition_variable_notify_one(rw_args->condition_variable);
514
515 return rw_args->received_message;
516 }
517
518 static struct aws_byte_buf s_tls_test_handle_write(
519 struct aws_channel_handler *handler,
520 struct aws_channel_slot *slot,
521 struct aws_byte_buf *data_read,
522 void *user_data) {
523
524 (void)handler;
525 (void)slot;
526 (void)data_read;
527 (void)user_data;
528
529 /*do nothing*/
530 return (struct aws_byte_buf){0};
531 }
532
533 static uint8_t s_server_received_message[128] = {0};
534 static uint8_t s_client_received_message[128] = {0};
535
536 /* common structure for test with self-initaizlied server and client */
537 struct tls_channel_server_client_tester {
538 struct tls_test_rw_args client_rw_args;
539 struct tls_test_rw_args server_rw_args;
540 struct tls_test_args client_args;
541 struct tls_test_args server_args;
542 struct aws_client_bootstrap *client_bootstrap;
543 struct tls_local_server_tester local_server_tester;
544
545 struct aws_mutex server_mutex;
546 struct aws_condition_variable server_condition_variable;
547
548 struct aws_atomic_var server_shutdown_invoked;
549 /* Make sure server and client doesn't use the same thread */
550 struct aws_event_loop_group *client_el_group;
551
552 bool window_update_after_shutdown;
553 };
554
555 static struct tls_channel_server_client_tester s_server_client_tester;
556
557 static int s_tls_channel_server_client_tester_init(struct aws_allocator *allocator) {
558 ASSERT_SUCCESS(s_tls_common_tester_init(allocator, &c_tester));
559 AWS_ZERO_STRUCT(s_server_client_tester);
560 ASSERT_SUCCESS(aws_mutex_init(&s_server_client_tester.server_mutex));
561 ASSERT_SUCCESS(aws_condition_variable_init(&s_server_client_tester.server_condition_variable));
562
563 struct aws_event_loop_group_options elg_options = {
564 .loop_count = 0,
565 };
566 s_server_client_tester.client_el_group = aws_event_loop_group_new(allocator, &elg_options);
567
568 ASSERT_SUCCESS(s_tls_rw_args_init(
569 &s_server_client_tester.server_rw_args,
570 &c_tester,
571 aws_byte_buf_from_empty_array(s_server_received_message, sizeof(s_server_received_message))));
572 s_server_client_tester.server_rw_args.mutex = &s_server_client_tester.server_mutex;
573 s_server_client_tester.server_rw_args.condition_variable = &s_server_client_tester.server_condition_variable;
574 ASSERT_SUCCESS(s_tls_rw_args_init(
575 &s_server_client_tester.client_rw_args,
576 &c_tester,
577 aws_byte_buf_from_empty_array(s_client_received_message, sizeof(s_client_received_message))));
578 ASSERT_SUCCESS(s_tls_test_arg_init(allocator, &s_server_client_tester.client_args, false, &c_tester));
579 ASSERT_SUCCESS(s_tls_test_arg_init(allocator, &s_server_client_tester.server_args, true, &c_tester));
580 s_server_client_tester.server_args.mutex = &s_server_client_tester.server_mutex;
581 s_server_client_tester.server_args.condition_variable = &s_server_client_tester.server_condition_variable;
582
583 ASSERT_SUCCESS(s_tls_local_server_tester_init(
584 allocator,
585 &s_server_client_tester.local_server_tester,
586 &s_server_client_tester.server_args,
587 &c_tester,
588 true,
589 "server.crt",
590 "server.key"));
591 struct aws_client_bootstrap_options bootstrap_options = {
592 .event_loop_group = s_server_client_tester.client_el_group,
593 .host_resolver = c_tester.resolver,
594 };
595 s_server_client_tester.client_bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
596
597 aws_atomic_store_int(&s_server_client_tester.server_shutdown_invoked, 0);
598 return AWS_OP_SUCCESS;
599 }
600
601 static int s_tls_channel_server_client_tester_cleanup(void) {
602 /* Make sure client and server all shutdown */
603 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
604 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
605 &c_tester.condition_variable,
606 &c_tester.mutex,
607 s_tls_channel_shutdown_predicate,
608 &s_server_client_tester.client_args));
609 aws_mutex_unlock(&c_tester.mutex);
610
611 aws_server_bootstrap_destroy_socket_listener(
612 s_server_client_tester.local_server_tester.server_bootstrap,
613 s_server_client_tester.local_server_tester.listener);
614 ASSERT_SUCCESS(s_tls_local_server_tester_clean_up(&s_server_client_tester.local_server_tester));
615 ASSERT_SUCCESS(aws_mutex_lock(&s_server_client_tester.server_mutex));
616 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
617 &s_server_client_tester.server_condition_variable,
618 &s_server_client_tester.server_mutex,
619 s_tls_listener_destroy_predicate,
620 &s_server_client_tester.server_args));
621 ASSERT_SUCCESS(aws_mutex_unlock(&s_server_client_tester.server_mutex));
622
623 /* Clean up */
624 aws_mutex_clean_up(&s_server_client_tester.server_mutex);
625 aws_condition_variable_clean_up(&s_server_client_tester.server_condition_variable);
626 aws_client_bootstrap_release(s_server_client_tester.client_bootstrap);
627 aws_event_loop_group_release(s_server_client_tester.client_el_group);
628 ASSERT_SUCCESS(s_tls_common_tester_clean_up(&c_tester));
629 return AWS_OP_SUCCESS;
630 }
631
632 static int s_set_socket_channel(struct tls_channel_server_client_tester *server_client_tester) {
633
634 struct tls_opt_tester client_tls_opt_tester;
635 struct aws_byte_cursor server_name = aws_byte_cursor_from_c_str("localhost");
636 ASSERT_SUCCESS(
637 s_tls_client_opt_tester_init(server_client_tester->client_args.allocator, &client_tls_opt_tester, server_name));
638 aws_tls_connection_options_set_callbacks(
639 &client_tls_opt_tester.opt, s_tls_on_negotiated, NULL, NULL, &server_client_tester->client_args);
640
641 struct aws_socket_channel_bootstrap_options channel_options;
642 AWS_ZERO_STRUCT(channel_options);
643 channel_options.bootstrap = server_client_tester->client_bootstrap;
644 channel_options.host_name = server_client_tester->local_server_tester.endpoint.address;
645 channel_options.port = 0;
646 channel_options.socket_options = &server_client_tester->local_server_tester.socket_options;
647 channel_options.tls_options = &client_tls_opt_tester.opt;
648 channel_options.setup_callback = s_tls_handler_test_client_setup_callback;
649 channel_options.shutdown_callback = s_tls_handler_test_client_shutdown_callback;
650 channel_options.user_data = &server_client_tester->client_args;
651 channel_options.enable_read_back_pressure = true;
652
653 ASSERT_SUCCESS(aws_client_bootstrap_new_socket_channel(&channel_options));
654
655 /* put this here to verify ownership semantics are correct. This should NOT cause a segfault. If it does, ya
656 * done messed up. */
657 aws_tls_connection_options_clean_up(&client_tls_opt_tester.opt);
658 /* wait for both ends to setup */
659 ASSERT_SUCCESS(aws_mutex_lock(&s_server_client_tester.server_mutex));
660 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
661 &s_server_client_tester.server_condition_variable,
662 &s_server_client_tester.server_mutex,
663 s_tls_channel_setup_predicate,
664 &server_client_tester->server_args));
665 ASSERT_SUCCESS(aws_mutex_unlock(&s_server_client_tester.server_mutex));
666 ASSERT_FALSE(server_client_tester->server_args.error_invoked);
667
668 /* currently it seems ALPN doesn't work in server mode. Just leaving this check out for now. */
669 # ifndef __APPLE__
670 struct aws_byte_buf expected_protocol = aws_byte_buf_from_c_str("h2");
671
672 /* check ALPN and SNI was properly negotiated */
673 if (aws_tls_is_alpn_available()) {
674 ASSERT_BIN_ARRAYS_EQUALS(
675 expected_protocol.buffer,
676 expected_protocol.len,
677 server_client_tester->server_args.negotiated_protocol.buffer,
678 server_client_tester->server_args.negotiated_protocol.len);
679 }
680 # endif
681
682 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
683 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
684 &c_tester.condition_variable,
685 &c_tester.mutex,
686 s_tls_channel_setup_predicate,
687 &server_client_tester->client_args));
688 ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
689 ASSERT_FALSE(server_client_tester->client_args.error_invoked);
690
691 /* currently it seems ALPN doesn't work in server mode. Just leaving this check out for now. */
692 # ifndef __MACH__
693 if (aws_tls_is_alpn_available()) {
694 ASSERT_BIN_ARRAYS_EQUALS(
695 expected_protocol.buffer,
696 expected_protocol.len,
697 server_client_tester->client_args.negotiated_protocol.buffer,
698 server_client_tester->client_args.negotiated_protocol.len);
699 }
700 # endif
701
702 ASSERT_SUCCESS(s_tls_opt_tester_clean_up(&client_tls_opt_tester));
703 return AWS_OP_SUCCESS;
704 }
705
706 static int s_tls_channel_echo_and_backpressure_test_fn(struct aws_allocator *allocator, void *ctx) {
707 (void)ctx;
708 ASSERT_SUCCESS(s_tls_channel_server_client_tester_init(allocator));
709 struct tls_test_rw_args *client_rw_args = &s_server_client_tester.client_rw_args;
710 struct tls_test_rw_args *server_rw_args = &s_server_client_tester.server_rw_args;
711 struct tls_test_args *client_args = &s_server_client_tester.client_args;
712 struct tls_test_args *server_args = &s_server_client_tester.server_args;
713
714 struct aws_byte_buf read_tag = aws_byte_buf_from_c_str("I'm a little teapot.");
715 struct aws_byte_buf write_tag = aws_byte_buf_from_c_str("I'm a big teapot");
716
717 /* make the windows small to make sure back pressure is honored. */
718 struct aws_channel_handler *client_rw_handler = rw_handler_new(
719 allocator, s_tls_test_handle_read, s_tls_test_handle_write, true, write_tag.len / 2, client_rw_args);
720 ASSERT_NOT_NULL(client_rw_handler);
721 struct aws_channel_handler *server_rw_handler = rw_handler_new(
722 allocator, s_tls_test_handle_read, s_tls_test_handle_write, true, read_tag.len / 2, server_rw_args);
723 ASSERT_NOT_NULL(server_rw_handler);
724 server_args->rw_handler = server_rw_handler;
725 client_args->rw_handler = client_rw_handler;
726
727 g_aws_channel_max_fragment_size = 4096;
728 ASSERT_SUCCESS(s_set_socket_channel(&s_server_client_tester));
729
730 /* Do the IO operations */
731 rw_handler_write(client_args->rw_handler, client_args->rw_slot, &write_tag);
732 rw_handler_write(server_args->rw_handler, server_args->rw_slot, &read_tag);
733 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
734 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
735 &c_tester.condition_variable, &c_tester.mutex, s_tls_test_read_predicate, client_rw_args));
736 ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
737
738 ASSERT_SUCCESS(aws_mutex_lock(&s_server_client_tester.server_mutex));
739 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
740 &s_server_client_tester.server_condition_variable,
741 &s_server_client_tester.server_mutex,
742 s_tls_test_read_predicate,
743 server_rw_args));
744 ASSERT_SUCCESS(aws_mutex_unlock(&s_server_client_tester.server_mutex));
745
746 server_rw_args->invocation_happened = false;
747 client_rw_args->invocation_happened = false;
748
749 ASSERT_INT_EQUALS(1, client_rw_args->read_invocations);
750 ASSERT_INT_EQUALS(1, server_rw_args->read_invocations);
751
752 /* Go ahead and verify back-pressure works*/
753 rw_handler_trigger_increment_read_window(server_args->rw_handler, server_args->rw_slot, 100);
754 rw_handler_trigger_increment_read_window(client_args->rw_handler, client_args->rw_slot, 100);
755
756 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
757 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
758 &c_tester.condition_variable, &c_tester.mutex, s_tls_test_read_predicate, client_rw_args));
759 ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
760
761 ASSERT_SUCCESS(aws_mutex_lock(&s_server_client_tester.server_mutex));
762 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
763 &s_server_client_tester.server_condition_variable,
764 &s_server_client_tester.server_mutex,
765 s_tls_test_read_predicate,
766 server_rw_args));
767 ASSERT_SUCCESS(aws_mutex_unlock(&s_server_client_tester.server_mutex));
768
769 ASSERT_INT_EQUALS(2, client_rw_args->read_invocations);
770 ASSERT_INT_EQUALS(2, server_rw_args->read_invocations);
771
772 ASSERT_BIN_ARRAYS_EQUALS(
773 write_tag.buffer, write_tag.len, server_rw_args->received_message.buffer, server_rw_args->received_message.len);
774 ASSERT_BIN_ARRAYS_EQUALS(
775 read_tag.buffer, read_tag.len, client_rw_args->received_message.buffer, client_rw_args->received_message.len);
776
777 aws_channel_shutdown(server_args->channel, AWS_OP_SUCCESS);
778 ASSERT_SUCCESS(aws_mutex_lock(&s_server_client_tester.server_mutex));
779 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
780 &s_server_client_tester.server_condition_variable,
781 &s_server_client_tester.server_mutex,
782 s_tls_channel_shutdown_predicate,
783 &s_server_client_tester.server_args));
784 ASSERT_SUCCESS(aws_mutex_unlock(&s_server_client_tester.server_mutex));
785
786 /*no shutdown on the client necessary here (it should have been triggered by shutting down the other side). just
787 * wait for the event to fire. */
788 ASSERT_SUCCESS(s_tls_channel_server_client_tester_cleanup());
789
790 return AWS_OP_SUCCESS;
791 }
792
793 AWS_TEST_CASE(tls_channel_echo_and_backpressure_test, s_tls_channel_echo_and_backpressure_test_fn)
794
795 static struct aws_byte_buf s_on_client_recive_shutdown_with_cache_data(
796 struct aws_channel_handler *handler,
797 struct aws_channel_slot *slot,
798 struct aws_byte_buf *data_read,
799 void *user_data) {
800
801 /**
802 * Client received the data from server, and it happens from the channel thread.
803 * Because of the limited window size, we also have more data cached in the TLS hanlder.
804 *
805 * Now:
806 * - Shutdown the server channel, and wait for it to finish, which will close the socket, and the socket will
807 * schedule the channel shutdown process when this function returns.
808 * - Update the window from this thread, it should schedule another task from channel thread to do so.
809 */
810 (void)slot;
811 (void)user_data;
812 struct tls_test_rw_args *client_rw_args = &s_server_client_tester.client_rw_args;
813
814 if (!rw_handler_shutdown_called(handler)) {
815
816 size_t shutdown_invoked = aws_atomic_load_int(&s_server_client_tester.server_shutdown_invoked);
817 if (shutdown_invoked == 0) {
818 aws_atomic_store_int(&s_server_client_tester.server_shutdown_invoked, 1);
819 if (!s_server_client_tester.window_update_after_shutdown) {
820 rw_handler_trigger_increment_read_window(
821 s_server_client_tester.client_args.rw_handler, s_server_client_tester.client_args.rw_slot, 100);
822 }
823 aws_channel_shutdown(s_server_client_tester.server_args.channel, AWS_OP_SUCCESS);
824
825 aws_mutex_lock(&s_server_client_tester.server_mutex);
826 aws_condition_variable_wait_pred(
827 &s_server_client_tester.server_condition_variable,
828 &s_server_client_tester.server_mutex,
829 s_tls_channel_shutdown_predicate,
830 &s_server_client_tester.server_args);
831 aws_mutex_unlock(&s_server_client_tester.server_mutex);
832 }
833 aws_mutex_lock(client_rw_args->mutex);
834
835 aws_byte_buf_write_from_whole_buffer(&client_rw_args->received_message, *data_read);
836 client_rw_args->read_invocations += 1;
837 client_rw_args->invocation_happened = true;
838
839 aws_mutex_unlock(client_rw_args->mutex);
840 aws_condition_variable_notify_one(client_rw_args->condition_variable);
841 } else {
842 AWS_FATAL_ASSERT(false && "The channel has already shutdown before process the message.");
843 }
844 return client_rw_args->received_message;
845 }
846
847 /**
848 * Test that when the socket initailize the shutdown process becasue of socket closed, we have a pending window update
849 * task to start the reading of the cached data in TLS handler. So, the channel will run the window update task and
850 * followed by a shutdown task immediately.
851 *
852 * Previously, the window update task will schedule read task if it opens the window back from close, but since the
853 * shutdown task already been scheluded, the read will happen after shutdown. So, it result in lost of data.
854 */
855 static int s_tls_channel_shutdown_with_cache_test_helper(struct aws_allocator *allocator, bool after_shutdown) {
856 ASSERT_SUCCESS(s_tls_channel_server_client_tester_init(allocator));
857 s_server_client_tester.window_update_after_shutdown = after_shutdown;
858
859 struct aws_byte_buf read_tag = aws_byte_buf_from_c_str("I'm a little teapot.");
860 struct aws_byte_buf write_tag = aws_byte_buf_from_c_str("I'm a big teapot");
861 /* Initialize the handler for client with small window, and shutdown the server */
862 struct aws_channel_handler *client_rw_handler = rw_handler_new(
863 allocator,
864 s_on_client_recive_shutdown_with_cache_data,
865 s_tls_test_handle_write,
866 true,
867 write_tag.len / 2,
868 &s_server_client_tester.client_rw_args);
869 ASSERT_NOT_NULL(client_rw_handler);
870
871 struct aws_channel_handler *server_rw_handler = rw_handler_new(
872 allocator,
873 s_tls_test_handle_read,
874 s_tls_test_handle_write,
875 true,
876 SIZE_MAX,
877 &s_server_client_tester.server_rw_args);
878 ASSERT_NOT_NULL(server_rw_handler);
879
880 s_server_client_tester.server_args.rw_handler = server_rw_handler;
881 s_server_client_tester.client_args.rw_handler = client_rw_handler;
882
883 g_aws_channel_max_fragment_size = 4096;
884 ASSERT_SUCCESS(s_set_socket_channel(&s_server_client_tester));
885
886 /* Server sends data to client */
887 rw_handler_write(
888 s_server_client_tester.server_args.rw_handler, s_server_client_tester.server_args.rw_slot, &read_tag);
889 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
890 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
891 &c_tester.condition_variable,
892 &c_tester.mutex,
893 s_tls_test_read_predicate,
894 &s_server_client_tester.client_rw_args));
895 ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
896
897 if (s_server_client_tester.window_update_after_shutdown) {
898 rw_handler_trigger_increment_read_window(
899 s_server_client_tester.client_args.rw_handler, s_server_client_tester.client_args.rw_slot, 100);
900 }
901
902 /* Make sure client also shutdown without error. */
903 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
904 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
905 &c_tester.condition_variable,
906 &c_tester.mutex,
907 s_tls_channel_shutdown_predicate,
908 &s_server_client_tester.client_args));
909 aws_mutex_unlock(&c_tester.mutex);
910
911 s_server_client_tester.client_rw_args.invocation_happened = false;
912
913 ASSERT_INT_EQUALS(2, s_server_client_tester.client_rw_args.read_invocations);
914
915 ASSERT_BIN_ARRAYS_EQUALS(
916 read_tag.buffer,
917 read_tag.len,
918 s_server_client_tester.client_rw_args.received_message.buffer,
919 s_server_client_tester.client_rw_args.received_message.len);
920
921 /* clean up */
922 /*no shutdown on the client necessary here (it should have been triggered by shutting down the other side). just
923 * wait for the event to fire. */
924 ASSERT_SUCCESS(s_tls_channel_server_client_tester_cleanup());
925
926 return AWS_OP_SUCCESS;
927 }
928
929 static int s_tls_channel_shutdown_with_cache_test_fn(struct aws_allocator *allocator, void *ctx) {
930 (void)ctx;
931 return s_tls_channel_shutdown_with_cache_test_helper(allocator, false);
932 }
933
934 AWS_TEST_CASE(tls_channel_shutdown_with_cache_test, s_tls_channel_shutdown_with_cache_test_fn)
935
936 static int s_tls_channel_shutdown_with_cache_window_update_after_shutdown_test_fn(
937 struct aws_allocator *allocator,
938 void *ctx) {
939 (void)ctx;
940 return s_tls_channel_shutdown_with_cache_test_helper(allocator, true);
941 }
942 AWS_TEST_CASE(
943 tls_channel_shutdown_with_cache_window_update_after_shutdown_test,
944 s_tls_channel_shutdown_with_cache_window_update_after_shutdown_test_fn)
945
946 struct default_host_callback_data {
947 struct aws_host_address aaaa_address;
948 struct aws_host_address a_address;
949 bool has_aaaa_address;
950 bool has_a_address;
951 struct aws_condition_variable condition_variable;
952 bool invoked;
953 };
954
955 static int s_verify_negotiation_fails_helper(
956 struct aws_allocator *allocator,
957 const struct aws_string *host_name,
958 uint32_t port,
959 struct aws_tls_ctx_options *client_ctx_options) {
960 struct aws_tls_ctx *client_ctx = aws_tls_client_ctx_new(allocator, client_ctx_options);
961
962 struct aws_tls_connection_options tls_client_conn_options;
963 aws_tls_connection_options_init_from_ctx(&tls_client_conn_options, client_ctx);
964 aws_tls_connection_options_set_callbacks(&tls_client_conn_options, s_tls_on_negotiated, NULL, NULL, NULL);
965 struct aws_byte_cursor host_name_cur = aws_byte_cursor_from_string(host_name);
966 aws_tls_connection_options_set_server_name(&tls_client_conn_options, allocator, &host_name_cur);
967
968 struct tls_test_args outgoing_args = {
969 .mutex = &c_tester.mutex,
970 .allocator = allocator,
971 .condition_variable = &c_tester.condition_variable,
972 .error_invoked = false,
973 .expects_error = true,
974 .rw_handler = NULL,
975 .server = false,
976 .tls_levels_negotiated = 0,
977 .desired_tls_levels = 1,
978 .shutdown_finished = false,
979 };
980
981 tls_client_conn_options.user_data = &outgoing_args;
982
983 struct aws_socket_options options;
984 AWS_ZERO_STRUCT(options);
985 options.connect_timeout_ms = BADSSL_TIMEOUT_MS;
986 options.type = AWS_SOCKET_STREAM;
987 options.domain = AWS_SOCKET_IPV4;
988
989 struct aws_client_bootstrap_options bootstrap_options = {
990 .event_loop_group = c_tester.el_group,
991 .host_resolver = c_tester.resolver,
992 };
993 struct aws_client_bootstrap *client_bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
994 ASSERT_NOT_NULL(client_bootstrap);
995
996 struct aws_socket_channel_bootstrap_options channel_options;
997 AWS_ZERO_STRUCT(channel_options);
998 channel_options.bootstrap = client_bootstrap;
999 channel_options.host_name = aws_string_c_str(host_name);
1000 channel_options.port = port;
1001 channel_options.socket_options = &options;
1002 channel_options.tls_options = &tls_client_conn_options;
1003 channel_options.setup_callback = s_tls_handler_test_client_setup_callback;
1004 channel_options.shutdown_callback = s_tls_handler_test_client_shutdown_callback;
1005 channel_options.user_data = &outgoing_args;
1006
1007 ASSERT_SUCCESS(aws_client_bootstrap_new_socket_channel(&channel_options));
1008
1009 /* put this here to verify ownership semantics are correct. This should NOT cause a segfault. If it does, ya
1010 * done messed up. */
1011 aws_tls_connection_options_clean_up(&tls_client_conn_options);
1012 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
1013 uint64_t wait_time = aws_timestamp_convert(60, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL);
1014 ASSERT_SUCCESS(aws_condition_variable_wait_for_pred(
1015 &c_tester.condition_variable, &c_tester.mutex, wait_time, s_tls_channel_shutdown_predicate, &outgoing_args));
1016 ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
1017
1018 ASSERT_TRUE(outgoing_args.error_invoked);
1019
1020 if (s_is_badssl_being_flaky(host_name, outgoing_args.last_error_code)) {
1021 return AWS_OP_SKIP;
1022 }
1023
1024 ASSERT_TRUE(aws_error_code_is_tls(outgoing_args.last_error_code));
1025
1026 aws_client_bootstrap_release(client_bootstrap);
1027
1028 aws_tls_ctx_release(client_ctx);
1029
1030 return AWS_OP_SUCCESS;
1031 }
1032
1033 static int s_verify_negotiation_fails(
1034 struct aws_allocator *allocator,
1035 const struct aws_string *host_name,
1036 uint32_t port,
1037 void (*context_options_override_fn)(struct aws_tls_ctx_options *)) {
1038
1039 ASSERT_SUCCESS(s_tls_common_tester_init(allocator, &c_tester));
1040
1041 struct aws_tls_ctx_options client_ctx_options;
1042 aws_tls_ctx_options_init_default_client(&client_ctx_options, allocator);
1043
1044 if (context_options_override_fn) {
1045 (*context_options_override_fn)(&client_ctx_options);
1046 }
1047
1048 int ret = s_verify_negotiation_fails_helper(allocator, host_name, port, &client_ctx_options);
1049 if (ret == AWS_OP_SUCCESS) {
1050 aws_tls_ctx_options_clean_up(&client_ctx_options);
1051 ASSERT_SUCCESS(s_tls_common_tester_clean_up(&c_tester));
1052
1053 return AWS_OP_SUCCESS;
1054 }
1055 return ret;
1056 }
1057
1058 static int s_verify_negotiation_fails_with_ca_override(
1059 struct aws_allocator *allocator,
1060 const struct aws_string *host_name,
1061 const char *root_ca_path) {
1062
1063 ASSERT_SUCCESS(s_tls_common_tester_init(allocator, &c_tester));
1064
1065 struct aws_tls_ctx_options client_ctx_options;
1066 aws_tls_ctx_options_init_default_client(&client_ctx_options, allocator);
1067
1068 ASSERT_SUCCESS(aws_tls_ctx_options_override_default_trust_store_from_path(&client_ctx_options, NULL, root_ca_path));
1069
1070 int ret = s_verify_negotiation_fails_helper(allocator, host_name, 443, &client_ctx_options);
1071 if (ret == AWS_OP_SUCCESS) {
1072 aws_tls_ctx_options_clean_up(&client_ctx_options);
1073 ASSERT_SUCCESS(s_tls_common_tester_clean_up(&c_tester));
1074
1075 return AWS_OP_SUCCESS;
1076 }
1077 return ret;
1078 }
1079
1080 # if defined(USE_S2N)
1081 static int s_default_pki_path_exists_fn(struct aws_allocator *allocator, void *ctx) {
1082 (void)ctx;
1083 (void)allocator;
1084
1085 ASSERT_TRUE(
1086 aws_determine_default_pki_dir() != NULL || aws_determine_default_pki_ca_file() != NULL,
1087 "Default TLS trust store not found on this system.");
1088 return AWS_OP_SUCCESS;
1089 }
1090
1091 AWS_TEST_CASE(default_pki_path_exists, s_default_pki_path_exists_fn)
1092 # endif /* defined(USE_S2N) */
1093
1094 AWS_STATIC_STRING_FROM_LITERAL(s_expired_host_name, "expired.badssl.com");
1095
1096 static int s_tls_client_channel_negotiation_error_expired_fn(struct aws_allocator *allocator, void *ctx) {
1097 (void)ctx;
1098
1099 return s_verify_negotiation_fails(allocator, s_expired_host_name, 443, NULL);
1100 }
1101
1102 AWS_TEST_CASE(tls_client_channel_negotiation_error_expired, s_tls_client_channel_negotiation_error_expired_fn)
1103
1104 AWS_STATIC_STRING_FROM_LITERAL(s_wrong_host_name, "wrong.host.badssl.com");
1105
1106 static int s_tls_client_channel_negotiation_error_wrong_host_fn(struct aws_allocator *allocator, void *ctx) {
1107 (void)ctx;
1108
1109 return s_verify_negotiation_fails(allocator, s_wrong_host_name, 443, NULL);
1110 }
1111
1112 AWS_TEST_CASE(tls_client_channel_negotiation_error_wrong_host, s_tls_client_channel_negotiation_error_wrong_host_fn)
1113
1114 static int s_tls_client_channel_negotiation_error_wrong_host_with_ca_override_fn(
1115 struct aws_allocator *allocator,
1116 void *ctx) {
1117 (void)ctx;
1118
1119 return s_verify_negotiation_fails_with_ca_override(allocator, s_wrong_host_name, "DigiCertGlobalRootCA.crt.pem");
1120 }
1121
1122 AWS_TEST_CASE(
1123 tls_client_channel_negotiation_error_wrong_host_with_ca_override,
1124 s_tls_client_channel_negotiation_error_wrong_host_with_ca_override_fn)
1125
1126 AWS_STATIC_STRING_FROM_LITERAL(s_self_signed_host_name, "self-signed.badssl.com");
1127
1128 static int s_tls_client_channel_negotiation_error_self_signed_fn(struct aws_allocator *allocator, void *ctx) {
1129 (void)ctx;
1130
1131 return s_verify_negotiation_fails(allocator, s_self_signed_host_name, 443, NULL);
1132 }
1133
1134 AWS_TEST_CASE(tls_client_channel_negotiation_error_self_signed, s_tls_client_channel_negotiation_error_self_signed_fn)
1135
1136 AWS_STATIC_STRING_FROM_LITERAL(s_untrusted_root_host_name, "untrusted-root.badssl.com");
1137
1138 static int s_tls_client_channel_negotiation_error_untrusted_root_fn(struct aws_allocator *allocator, void *ctx) {
1139 (void)ctx;
1140
1141 return s_verify_negotiation_fails(allocator, s_untrusted_root_host_name, 443, NULL);
1142 }
1143
1144 AWS_TEST_CASE(
1145 tls_client_channel_negotiation_error_untrusted_root,
1146 s_tls_client_channel_negotiation_error_untrusted_root_fn);
1147
1148 AWS_STATIC_STRING_FROM_LITERAL(s_amazon_host_name, "www.amazon.com");
1149
1150 /* negotiation should fail. www.amazon.com is obviously trusted by the default trust store,
1151 * but we've overridden the default trust store */
1152 static int s_tls_client_channel_negotiation_error_untrusted_root_due_to_ca_override_fn(
1153 struct aws_allocator *allocator,
1154 void *ctx) {
1155 (void)ctx;
1156
1157 return s_verify_negotiation_fails_with_ca_override(allocator, s_amazon_host_name, "ca_root.crt");
1158 }
1159
1160 AWS_TEST_CASE(
1161 tls_client_channel_negotiation_error_untrusted_root_due_to_ca_override,
1162 s_tls_client_channel_negotiation_error_untrusted_root_due_to_ca_override_fn)
1163
1164 AWS_STATIC_STRING_FROM_LITERAL(s_broken_crypto_rc4_host_name, "rc4.badssl.com");
1165
1166 static int s_tls_client_channel_negotiation_error_broken_crypto_rc4_fn(struct aws_allocator *allocator, void *ctx) {
1167 (void)ctx;
1168 return s_verify_negotiation_fails(allocator, s_broken_crypto_rc4_host_name, 443, NULL);
1169 }
1170
1171 AWS_TEST_CASE(
1172 tls_client_channel_negotiation_error_broken_crypto_rc4,
1173 s_tls_client_channel_negotiation_error_broken_crypto_rc4_fn)
1174
1175 AWS_STATIC_STRING_FROM_LITERAL(s_broken_crypto_rc4_md5_host_name, "rc4-md5.badssl.com");
1176
1177 static int s_tls_client_channel_negotiation_error_broken_crypto_rc4_md5_fn(struct aws_allocator *allocator, void *ctx) {
1178 (void)ctx;
1179 return s_verify_negotiation_fails(allocator, s_broken_crypto_rc4_md5_host_name, 443, NULL);
1180 }
1181
1182 AWS_TEST_CASE(
1183 tls_client_channel_negotiation_error_broken_crypto_rc4_md5,
1184 s_tls_client_channel_negotiation_error_broken_crypto_rc4_md5_fn)
1185
1186 AWS_STATIC_STRING_FROM_LITERAL(s_broken_crypto_dh480_host_name, "dh480.badssl.com");
1187
1188 static int s_tls_client_channel_negotiation_error_broken_crypto_dh480_fn(struct aws_allocator *allocator, void *ctx) {
1189 (void)ctx;
1190 return s_verify_negotiation_fails(allocator, s_broken_crypto_dh480_host_name, 443, NULL);
1191 }
1192
1193 AWS_TEST_CASE(
1194 tls_client_channel_negotiation_error_broken_crypto_dh480,
1195 s_tls_client_channel_negotiation_error_broken_crypto_dh480_fn)
1196
1197 AWS_STATIC_STRING_FROM_LITERAL(s_broken_crypto_dh512_host_name, "dh512.badssl.com");
1198
1199 static int s_tls_client_channel_negotiation_error_broken_crypto_dh512_fn(struct aws_allocator *allocator, void *ctx) {
1200 (void)ctx;
1201 return s_verify_negotiation_fails(allocator, s_broken_crypto_dh512_host_name, 443, NULL);
1202 }
1203
1204 AWS_TEST_CASE(
1205 tls_client_channel_negotiation_error_broken_crypto_dh512,
1206 s_tls_client_channel_negotiation_error_broken_crypto_dh512_fn)
1207
1208 AWS_STATIC_STRING_FROM_LITERAL(s_broken_crypto_dh1024_host_name, "dh1024.badssl.com");
1209
1210 static int s_tls_client_channel_negotiation_error_broken_crypto_dh1024_fn(struct aws_allocator *allocator, void *ctx) {
1211 (void)ctx;
1212 return s_verify_negotiation_fails(allocator, s_broken_crypto_dh1024_host_name, 443, NULL);
1213 }
1214
1215 AWS_TEST_CASE(
1216 tls_client_channel_negotiation_error_broken_crypto_dh1024,
1217 s_tls_client_channel_negotiation_error_broken_crypto_dh1024_fn)
1218
1219 AWS_STATIC_STRING_FROM_LITERAL(s_broken_crypto_null_host_name, "null.badssl.com");
1220
1221 static int s_tls_client_channel_negotiation_error_broken_crypto_null_fn(struct aws_allocator *allocator, void *ctx) {
1222 (void)ctx;
1223 return s_verify_negotiation_fails(allocator, s_broken_crypto_null_host_name, 443, NULL);
1224 }
1225
1226 AWS_TEST_CASE(
1227 tls_client_channel_negotiation_error_broken_crypto_null,
1228 s_tls_client_channel_negotiation_error_broken_crypto_null_fn)
1229
1230 AWS_STATIC_STRING_FROM_LITERAL(s_legacy_crypto_tls10_host_name, "tls-v1-0.badssl.com");
1231
1232 static void s_raise_tls_version_to_11(struct aws_tls_ctx_options *options) {
1233 aws_tls_ctx_options_set_minimum_tls_version(options, AWS_IO_TLSv1_2);
1234 }
1235
1236 static int s_tls_client_channel_negotiation_error_legacy_crypto_tls10_fn(struct aws_allocator *allocator, void *ctx) {
1237 (void)ctx;
1238 return s_verify_negotiation_fails(allocator, s_legacy_crypto_tls10_host_name, 1010, &s_raise_tls_version_to_11);
1239 }
1240
1241 AWS_TEST_CASE(
1242 tls_client_channel_negotiation_error_legacy_crypto_tls10,
1243 s_tls_client_channel_negotiation_error_legacy_crypto_tls10_fn)
1244
1245 AWS_STATIC_STRING_FROM_LITERAL(s_legacy_crypto_tls11_host_name, "tls-v1-1.badssl.com");
1246
1247 static void s_raise_tls_version_to_12(struct aws_tls_ctx_options *options) {
1248 aws_tls_ctx_options_set_minimum_tls_version(options, AWS_IO_TLSv1_2);
1249 }
1250
1251 static int s_tls_client_channel_negotiation_error_override_legacy_crypto_tls11_fn(
1252 struct aws_allocator *allocator,
1253 void *ctx) {
1254 (void)ctx;
1255 return s_verify_negotiation_fails(allocator, s_legacy_crypto_tls11_host_name, 1011, &s_raise_tls_version_to_12);
1256 }
1257
1258 AWS_TEST_CASE(
1259 tls_client_channel_negotiation_error_override_legacy_crypto_tls11,
1260 s_tls_client_channel_negotiation_error_override_legacy_crypto_tls11_fn)
1261
1262 AWS_STATIC_STRING_FROM_LITERAL(s_legacy_crypto_dh2048_host_name, "dh2048.badssl.com");
1263
1264 static int s_tls_client_channel_negotiation_error_legacy_crypto_dh2048_fn(struct aws_allocator *allocator, void *ctx) {
1265 (void)ctx;
1266 return s_verify_negotiation_fails(allocator, s_legacy_crypto_dh2048_host_name, 443, NULL);
1267 }
1268
1269 AWS_TEST_CASE(
1270 tls_client_channel_negotiation_error_legacy_crypto_dh2048,
1271 s_tls_client_channel_negotiation_error_legacy_crypto_dh2048_fn)
1272
1273 AWS_STATIC_STRING_FROM_LITERAL(s_uncommon_no_subject_host_name, "no-subject.badssl.com");
1274
1275 static int s_tls_client_channel_negotiation_error_no_subject_fn(struct aws_allocator *allocator, void *ctx) {
1276 (void)ctx;
1277 return s_verify_negotiation_fails(allocator, s_uncommon_no_subject_host_name, 443, NULL);
1278 }
1279
1280 AWS_TEST_CASE(tls_client_channel_negotiation_error_no_subject, s_tls_client_channel_negotiation_error_no_subject_fn)
1281
1282 AWS_STATIC_STRING_FROM_LITERAL(s_uncommon_no_common_name_host_name, "no-common-name.badssl.com");
1283
1284 static int s_tls_client_channel_negotiation_error_no_common_name_fn(struct aws_allocator *allocator, void *ctx) {
1285 (void)ctx;
1286 return s_verify_negotiation_fails(allocator, s_uncommon_no_common_name_host_name, 443, NULL);
1287 }
1288
1289 AWS_TEST_CASE(
1290 tls_client_channel_negotiation_error_no_common_name,
1291 s_tls_client_channel_negotiation_error_no_common_name_fn)
1292
1293 /* Test that, if the channel shuts down unexpectedly during tls negotiation, that the user code is still notified.
1294 * We make this happen by connecting to port 80 on s3 or amazon.com and attempting TLS,
1295 * which gets you hung up on after a few seconds */
1296 static int s_tls_client_channel_negotiation_error_socket_closed_fn(struct aws_allocator *allocator, void *ctx) {
1297 (void)ctx;
1298
1299 const char *host_name = "aws-crt-test-stuff.s3.amazonaws.com";
1300 uint32_t port = 80; /* Note: intentionally wrong and not 443 */
1301
1302 ASSERT_SUCCESS(s_tls_common_tester_init(allocator, &c_tester));
1303
1304 struct tls_opt_tester client_tls_opt_tester;
1305 struct aws_byte_cursor server_name = aws_byte_cursor_from_c_str(host_name);
1306 ASSERT_SUCCESS(s_tls_client_opt_tester_init(allocator, &client_tls_opt_tester, server_name));
1307 client_tls_opt_tester.opt.timeout_ms = 0; /* disable negotiation timeout for this test */
1308
1309 struct tls_test_args outgoing_args;
1310 ASSERT_SUCCESS(s_tls_test_arg_init(allocator, &outgoing_args, false, &c_tester));
1311
1312 struct aws_socket_options options = {
1313 .connect_timeout_ms = 10000, .type = AWS_SOCKET_STREAM, .domain = AWS_SOCKET_IPV4};
1314
1315 struct aws_client_bootstrap_options bootstrap_options = {
1316 .event_loop_group = c_tester.el_group,
1317 .host_resolver = c_tester.resolver,
1318 };
1319 struct aws_client_bootstrap *client_bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
1320 ASSERT_NOT_NULL(client_bootstrap);
1321
1322 struct aws_socket_channel_bootstrap_options channel_options;
1323 AWS_ZERO_STRUCT(channel_options);
1324 channel_options.bootstrap = client_bootstrap;
1325 channel_options.host_name = host_name;
1326 channel_options.port = port;
1327 channel_options.socket_options = &options;
1328 channel_options.tls_options = &client_tls_opt_tester.opt;
1329 channel_options.setup_callback = s_tls_handler_test_client_setup_callback;
1330 channel_options.shutdown_callback = s_tls_handler_test_client_shutdown_callback;
1331 channel_options.user_data = &outgoing_args;
1332
1333 ASSERT_SUCCESS(aws_client_bootstrap_new_socket_channel(&channel_options));
1334
1335 /* Wait for setup to complete */
1336 aws_mutex_lock(&c_tester.mutex);
1337 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
1338 &c_tester.condition_variable, &c_tester.mutex, s_tls_channel_setup_predicate, &outgoing_args));
1339
1340 /* Assert that setup failed, and that it failed for reasons unrelated to the tls-handler. */
1341 ASSERT_INT_EQUALS(0, outgoing_args.tls_levels_negotiated);
1342 ASSERT_TRUE(outgoing_args.error_invoked);
1343 ASSERT_INT_EQUALS(AWS_IO_SOCKET_CLOSED, outgoing_args.last_error_code);
1344
1345 aws_mutex_unlock(&c_tester.mutex);
1346
1347 /* Clean up */
1348 aws_client_bootstrap_release(client_bootstrap);
1349
1350 s_tls_opt_tester_clean_up(&client_tls_opt_tester);
1351 ASSERT_SUCCESS(s_tls_common_tester_clean_up(&c_tester));
1352
1353 return AWS_OP_SUCCESS;
1354 }
1355
1356 AWS_TEST_CASE(
1357 tls_client_channel_negotiation_error_socket_closed,
1358 s_tls_client_channel_negotiation_error_socket_closed_fn);
1359
1360 static int s_verify_good_host(
1361 struct aws_allocator *allocator,
1362 const struct aws_string *host_name,
1363 uint32_t port,
1364 void (*override_tls_options_fn)(struct aws_tls_ctx_options *)) {
1365
1366 ASSERT_SUCCESS(s_tls_common_tester_init(allocator, &c_tester));
1367
1368 struct tls_test_args outgoing_args = {
1369 .mutex = &c_tester.mutex,
1370 .allocator = allocator,
1371 .condition_variable = &c_tester.condition_variable,
1372 .error_invoked = 0,
1373 .rw_handler = NULL,
1374 .server = false,
1375 .tls_levels_negotiated = 0,
1376 .desired_tls_levels = 1,
1377 .shutdown_finished = false,
1378 };
1379
1380 struct aws_tls_ctx_options client_ctx_options;
1381 AWS_ZERO_STRUCT(client_ctx_options);
1382 aws_tls_ctx_options_set_verify_peer(&client_ctx_options, true);
1383 aws_tls_ctx_options_init_default_client(&client_ctx_options, allocator);
1384 aws_tls_ctx_options_set_alpn_list(&client_ctx_options, "http/1.1");
1385
1386 if (override_tls_options_fn) {
1387 (*override_tls_options_fn)(&client_ctx_options);
1388 }
1389
1390 struct aws_tls_ctx *client_ctx = aws_tls_client_ctx_new(allocator, &client_ctx_options);
1391 ASSERT_NOT_NULL(client_ctx);
1392
1393 struct aws_tls_connection_options tls_client_conn_options;
1394 aws_tls_connection_options_init_from_ctx(&tls_client_conn_options, client_ctx);
1395 aws_tls_connection_options_set_callbacks(&tls_client_conn_options, s_tls_on_negotiated, NULL, NULL, &outgoing_args);
1396
1397 struct aws_byte_cursor host_name_cur = aws_byte_cursor_from_string(host_name);
1398 aws_tls_connection_options_set_server_name(&tls_client_conn_options, allocator, &host_name_cur);
1399 aws_tls_connection_options_set_alpn_list(&tls_client_conn_options, allocator, "http/1.1");
1400
1401 struct aws_socket_options options;
1402 AWS_ZERO_STRUCT(options);
1403 options.connect_timeout_ms = BADSSL_TIMEOUT_MS;
1404 options.type = AWS_SOCKET_STREAM;
1405 options.domain = AWS_SOCKET_IPV4;
1406
1407 struct aws_client_bootstrap_options bootstrap_options = {
1408 .event_loop_group = c_tester.el_group,
1409 .host_resolver = c_tester.resolver,
1410 };
1411 struct aws_client_bootstrap *client_bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
1412 ASSERT_NOT_NULL(client_bootstrap);
1413
1414 struct aws_socket_channel_bootstrap_options channel_options;
1415 AWS_ZERO_STRUCT(channel_options);
1416 channel_options.bootstrap = client_bootstrap;
1417 channel_options.host_name = aws_string_c_str(host_name);
1418 channel_options.port = port;
1419 channel_options.socket_options = &options;
1420 channel_options.tls_options = &tls_client_conn_options;
1421 channel_options.setup_callback = s_tls_handler_test_client_setup_callback;
1422 channel_options.shutdown_callback = s_tls_handler_test_client_shutdown_callback;
1423 channel_options.user_data = &outgoing_args;
1424
1425 ASSERT_SUCCESS(aws_client_bootstrap_new_socket_channel(&channel_options));
1426
1427 /* put this here to verify ownership semantics are correct. This should NOT cause a segfault. If it does, ya
1428 * done messed up. */
1429 aws_tls_connection_options_clean_up(&tls_client_conn_options);
1430
1431 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
1432 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
1433 &c_tester.condition_variable, &c_tester.mutex, s_tls_channel_setup_predicate, &outgoing_args));
1434 ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
1435
1436 if (s_is_badssl_being_flaky(host_name, outgoing_args.last_error_code)) {
1437 return AWS_OP_SKIP;
1438 }
1439
1440 ASSERT_FALSE(outgoing_args.error_invoked);
1441 struct aws_byte_buf expected_protocol = aws_byte_buf_from_c_str("http/1.1");
1442 /* check ALPN and SNI was properly negotiated */
1443
1444 if (aws_tls_is_alpn_available() && client_ctx_options.verify_peer) {
1445 ASSERT_BIN_ARRAYS_EQUALS(
1446 expected_protocol.buffer,
1447 expected_protocol.len,
1448 outgoing_args.negotiated_protocol.buffer,
1449 outgoing_args.negotiated_protocol.len);
1450 }
1451
1452 ASSERT_BIN_ARRAYS_EQUALS(
1453 host_name->bytes, host_name->len, outgoing_args.server_name.buffer, outgoing_args.server_name.len);
1454
1455 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
1456 aws_channel_shutdown(outgoing_args.channel, AWS_OP_SUCCESS);
1457 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
1458 &c_tester.condition_variable, &c_tester.mutex, s_tls_channel_shutdown_predicate, &outgoing_args));
1459 ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
1460
1461 aws_client_bootstrap_release(client_bootstrap);
1462
1463 aws_tls_ctx_release(client_ctx);
1464 aws_tls_ctx_options_clean_up(&client_ctx_options);
1465 ASSERT_SUCCESS(s_tls_common_tester_clean_up(&c_tester));
1466
1467 return AWS_OP_SUCCESS;
1468 }
1469
1470 static int s_verify_good_host_mtls_connect(
1471 struct aws_allocator *allocator,
1472 const struct aws_string *host_name,
1473 uint32_t port,
1474 const char *ca_path,
1475 void (*override_tls_options_fn)(struct aws_tls_ctx_options *)) {
1476
1477 struct aws_byte_buf cert_buf = {0};
1478 struct aws_byte_buf key_buf = {0};
1479 struct aws_byte_buf ca_buf = {0};
1480
1481 ASSERT_SUCCESS(aws_byte_buf_init_from_file(&cert_buf, allocator, "mtls_device.pem.crt"));
1482 ASSERT_SUCCESS(aws_byte_buf_init_from_file(&key_buf, allocator, "mtls_device.key"));
1483 if (ca_path) {
1484 ASSERT_SUCCESS(aws_byte_buf_init_from_file(&ca_buf, allocator, ca_path));
1485 }
1486
1487 struct aws_byte_cursor cert_cur = aws_byte_cursor_from_buf(&cert_buf);
1488 struct aws_byte_cursor key_cur = aws_byte_cursor_from_buf(&key_buf);
1489
1490 aws_io_library_init(allocator);
1491
1492 ASSERT_SUCCESS(s_tls_common_tester_init(allocator, &c_tester));
1493
1494 uint8_t outgoing_received_message[128] = {0};
1495
1496 struct tls_test_rw_args outgoing_rw_args;
1497 ASSERT_SUCCESS(s_tls_rw_args_init(
1498 &outgoing_rw_args,
1499 &c_tester,
1500 aws_byte_buf_from_empty_array(outgoing_received_message, sizeof(outgoing_received_message))));
1501
1502 struct tls_test_args outgoing_args = {
1503 .mutex = &c_tester.mutex,
1504 .allocator = allocator,
1505 .condition_variable = &c_tester.condition_variable,
1506 .error_invoked = 0,
1507 .rw_handler = NULL,
1508 .server = false,
1509 .tls_levels_negotiated = 0,
1510 .desired_tls_levels = 1,
1511 .shutdown_finished = false,
1512 };
1513
1514 struct aws_tls_ctx_options tls_options = {0};
1515 AWS_ZERO_STRUCT(tls_options);
1516
1517 AWS_FATAL_ASSERT(
1518 AWS_OP_SUCCESS == aws_tls_ctx_options_init_client_mtls(&tls_options, allocator, &cert_cur, &key_cur));
1519
1520 aws_tls_ctx_options_set_verify_peer(&tls_options, true);
1521 if (ca_path) {
1522 struct aws_byte_cursor ca_cur = aws_byte_cursor_from_buf(&ca_buf);
1523 aws_tls_ctx_options_override_default_trust_store(&tls_options, &ca_cur);
1524 }
1525
1526 if (override_tls_options_fn) {
1527 (*override_tls_options_fn)(&tls_options);
1528 }
1529
1530 struct aws_tls_ctx *tls_context = aws_tls_client_ctx_new(allocator, &tls_options);
1531 ASSERT_NOT_NULL(tls_context);
1532
1533 struct aws_tls_connection_options tls_client_conn_options;
1534 aws_tls_connection_options_init_from_ctx(&tls_client_conn_options, tls_context);
1535 aws_tls_connection_options_set_callbacks(&tls_client_conn_options, s_tls_on_negotiated, NULL, NULL, &outgoing_args);
1536
1537 struct aws_byte_cursor host_name_cur = aws_byte_cursor_from_string(host_name);
1538 aws_tls_connection_options_set_server_name(&tls_client_conn_options, allocator, &host_name_cur);
1539 aws_tls_connection_options_set_alpn_list(&tls_client_conn_options, allocator, "x-amzn-mqtt-ca");
1540
1541 struct aws_socket_options options;
1542 AWS_ZERO_STRUCT(options);
1543 options.connect_timeout_ms = 10000;
1544 options.type = AWS_SOCKET_STREAM;
1545 options.domain = AWS_SOCKET_IPV4;
1546
1547 struct aws_client_bootstrap_options bootstrap_options = {
1548 .event_loop_group = c_tester.el_group,
1549 .host_resolver = c_tester.resolver,
1550 };
1551 struct aws_client_bootstrap *client_bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
1552 ASSERT_NOT_NULL(client_bootstrap);
1553
1554 struct aws_socket_channel_bootstrap_options channel_options;
1555 AWS_ZERO_STRUCT(channel_options);
1556 channel_options.bootstrap = client_bootstrap;
1557 channel_options.host_name = aws_string_c_str(host_name);
1558 channel_options.port = port;
1559 channel_options.socket_options = &options;
1560 channel_options.tls_options = &tls_client_conn_options;
1561 channel_options.setup_callback = s_tls_handler_test_client_setup_callback;
1562 channel_options.shutdown_callback = s_tls_handler_test_client_shutdown_callback;
1563 channel_options.user_data = &outgoing_args;
1564
1565 ASSERT_SUCCESS(aws_client_bootstrap_new_socket_channel(&channel_options));
1566
1567 /* put this here to verify ownership semantics are correct. This should NOT cause a segfault. If it does, ya
1568 * done messed up. */
1569 aws_tls_connection_options_clean_up(&tls_client_conn_options);
1570
1571 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
1572 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
1573 &c_tester.condition_variable, &c_tester.mutex, s_tls_channel_setup_predicate, &outgoing_args));
1574 ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
1575
1576 ASSERT_FALSE(outgoing_args.error_invoked);
1577
1578 /* check SNI was properly negotiated */
1579 ASSERT_BIN_ARRAYS_EQUALS(
1580 host_name->bytes, host_name->len, outgoing_args.server_name.buffer, outgoing_args.server_name.len);
1581
1582 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
1583 aws_channel_shutdown(outgoing_args.channel, AWS_OP_SUCCESS);
1584 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
1585 &c_tester.condition_variable, &c_tester.mutex, s_tls_channel_shutdown_predicate, &outgoing_args));
1586 ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
1587
1588 /* cleanups */
1589 aws_byte_buf_clean_up(&cert_buf);
1590 aws_byte_buf_clean_up(&key_buf);
1591 aws_byte_buf_clean_up(&ca_buf);
1592 aws_tls_ctx_release(tls_context);
1593 aws_tls_ctx_options_clean_up(&tls_options);
1594 aws_client_bootstrap_release(client_bootstrap);
1595 ASSERT_SUCCESS(s_tls_common_tester_clean_up(&c_tester));
1596
1597 return AWS_OP_SUCCESS;
1598 }
1599
1600 static int s_tls_client_channel_negotiation_success_fn(struct aws_allocator *allocator, void *ctx) {
1601 (void)ctx;
1602 return s_verify_good_host(allocator, s_amazon_host_name, 443, NULL);
1603 }
1604
1605 AWS_TEST_CASE(tls_client_channel_negotiation_success, s_tls_client_channel_negotiation_success_fn)
1606
1607 AWS_STATIC_STRING_FROM_LITERAL(s_badssl_ecc256_host_name, "ecc256.badssl.com");
1608
1609 static int s_tls_client_channel_negotiation_success_ecc256_fn(struct aws_allocator *allocator, void *ctx) {
1610 (void)ctx;
1611 return s_verify_good_host(allocator, s_badssl_ecc256_host_name, 443, NULL);
1612 }
1613
1614 AWS_TEST_CASE(tls_client_channel_negotiation_success_ecc256, s_tls_client_channel_negotiation_success_ecc256_fn)
1615
1616 AWS_STATIC_STRING_FROM_LITERAL(s_badssl_ecc384_host_name, "ecc384.badssl.com");
1617
1618 static int s_tls_client_channel_negotiation_success_ecc384_fn(struct aws_allocator *allocator, void *ctx) {
1619 (void)ctx;
1620 return s_verify_good_host(allocator, s_badssl_ecc384_host_name, 443, NULL);
1621 }
1622 AWS_TEST_CASE(tls_client_channel_negotiation_success_ecc384, s_tls_client_channel_negotiation_success_ecc384_fn)
1623
1624 # ifdef _WIN32
1625
1626 static int s_tls_client_channel_negotiation_success_ecc384_SCHANNEL_CREDS_fn(
1627 struct aws_allocator *allocator,
1628 void *ctx) {
1629 (void)ctx;
1630
1631 // Force using SCHANNEL_CREDS for testing
1632 aws_windows_force_schannel_creds(true);
1633 s_verify_good_host(allocator, s_badssl_ecc384_host_name, 443, NULL);
1634 aws_windows_force_schannel_creds(false); // reset
1635 return AWS_OP_SUCCESS;
1636 }
1637
1638 AWS_TEST_CASE(
1639 tls_client_channel_negotiation_success_ecc384_deprecated,
1640 s_tls_client_channel_negotiation_success_ecc384_SCHANNEL_CREDS_fn)
1641 # endif
1642
1643 static void s_raise_tls_version_to_13(struct aws_tls_ctx_options *options) {
1644 aws_tls_ctx_options_set_minimum_tls_version(options, AWS_IO_TLSv1_3);
1645 }
1646
1647 AWS_STATIC_STRING_FROM_LITERAL(s_aws_local_tls_server_host_name, "localhost");
1648 static int s_tls_client_channel_negotiation_success_mtls_tls13_fn(struct aws_allocator *allocator, void *ctx) {
1649 (void)ctx;
1650 /* macOS supports TLS 1.3 only when s2n-tls is used as TLS backend, which is controlled by the env var. */
1651 if (s_is_apple_with_secure_transport(allocator)) {
1652 return AWS_OP_SKIP;
1653 }
1654 return s_verify_good_host_mtls_connect(
1655 allocator,
1656 s_aws_local_tls_server_host_name,
1657 AWS_TEST_LOCAL_TLS13_PORT,
1658 "mtls_server_root_ca.pem.crt",
1659 s_raise_tls_version_to_13);
1660 }
1661
1662 AWS_TEST_CASE(tls_client_channel_negotiation_success_mtls_tls13, s_tls_client_channel_negotiation_success_mtls_tls13_fn)
1663
1664 /* macOS-specific mTLS over s2n-tls test: connects to the trusted local TLS server without explicitly setting the server
1665 * root CA. Instead, it relies on the root CA being imported into the macOS keychain as trusted. */
1666 static int s_tls_client_channel_negotiation_success_mtls_from_keychain_fn(struct aws_allocator *allocator, void *ctx) {
1667 (void)ctx;
1668 if (s_is_apple_with_secure_transport(allocator)) {
1669 return AWS_OP_SKIP;
1670 }
1671 return s_verify_good_host_mtls_connect(
1672 allocator, s_aws_local_tls_server_host_name, AWS_TEST_LOCAL_TLS13_PORT, NULL /* ca_path */, NULL);
1673 }
1674
1675 AWS_TEST_CASE(
1676 tls_client_channel_negotiation_success_mtls_from_keychain,
1677 s_tls_client_channel_negotiation_success_mtls_from_keychain_fn)
1678
1679 /* macOS-specific mTLS over s2n-tls test: connects to the untrusted local TLS server without explicitly setting the
1680 * server root CA. The untrusted server's root CA is imported into the keychain but NOT marked as trusted, so TLS
1681 * negotiation should fail. */
1682 static int s_tls_client_channel_negotiation_failure_mtls_untrusted_server_from_keychain_fn(
1683 struct aws_allocator *allocator,
1684 void *ctx) {
1685 (void)ctx;
1686 if (s_is_apple_with_secure_transport(allocator)) {
1687 return AWS_OP_SKIP;
1688 }
1689
1690 struct aws_byte_buf cert_buf = {0};
1691 struct aws_byte_buf key_buf = {0};
1692
1693 ASSERT_SUCCESS(aws_byte_buf_init_from_file(&cert_buf, allocator, "mtls_device.pem.crt"));
1694 ASSERT_SUCCESS(aws_byte_buf_init_from_file(&key_buf, allocator, "mtls_device.key"));
1695
1696 struct aws_byte_cursor cert_cur = aws_byte_cursor_from_buf(&cert_buf);
1697 struct aws_byte_cursor key_cur = aws_byte_cursor_from_buf(&key_buf);
1698
1699 aws_io_library_init(allocator);
1700
1701 ASSERT_SUCCESS(s_tls_common_tester_init(allocator, &c_tester));
1702
1703 struct aws_tls_ctx_options tls_options = {0};
1704 AWS_ZERO_STRUCT(tls_options);
1705
1706 AWS_FATAL_ASSERT(
1707 AWS_OP_SUCCESS == aws_tls_ctx_options_init_client_mtls(&tls_options, allocator, &cert_cur, &key_cur));
1708
1709 aws_tls_ctx_options_set_verify_peer(&tls_options, true);
1710
1711 ASSERT_SUCCESS(s_verify_negotiation_fails_helper(
1712 allocator, s_aws_local_tls_server_host_name, AWS_TEST_LOCAL_UNTRUSTED_TLS_PORT, &tls_options));
1713
1714 aws_byte_buf_clean_up(&cert_buf);
1715 aws_byte_buf_clean_up(&key_buf);
1716 aws_tls_ctx_options_clean_up(&tls_options);
1717 ASSERT_SUCCESS(s_tls_common_tester_clean_up(&c_tester));
1718
1719 return AWS_OP_SUCCESS;
1720 }
1721
1722 AWS_TEST_CASE(
1723 tls_client_channel_negotiation_failure_mtls_untrusted_server_from_keychain,
1724 s_tls_client_channel_negotiation_failure_mtls_untrusted_server_from_keychain_fn)
1725
1726 static int s_tls_client_channel_negotiation_failure_tls13_to_tls12_server_fn(
1727 struct aws_allocator *allocator,
1728 void *ctx) {
1729 (void)ctx;
1730 return s_verify_negotiation_fails(allocator, s_badssl_ecc256_host_name, 443, s_raise_tls_version_to_13);
1731 }
1732
1733 AWS_TEST_CASE(
1734 tls_client_channel_negotiation_failure_tls13_to_tls12_server,
1735 s_tls_client_channel_negotiation_failure_tls13_to_tls12_server_fn)
1736
1737 AWS_STATIC_STRING_FROM_LITERAL(s3_host_name, "s3.amazonaws.com");
1738
1739 static void s_disable_verify_peer(struct aws_tls_ctx_options *options) {
1740 aws_tls_ctx_options_set_verify_peer(options, false);
1741 }
1742
1743 /* prove that connections complete even when verify_peer is false */
1744 static int s_tls_client_channel_no_verify_fn(struct aws_allocator *allocator, void *ctx) {
1745 (void)ctx;
1746 return s_verify_good_host(allocator, s3_host_name, 443, &s_disable_verify_peer);
1747 }
1748 AWS_TEST_CASE(tls_client_channel_no_verify, s_tls_client_channel_no_verify_fn)
1749
1750 /* Check all of the bad tls cases with verify_peer off. Now they should succeed. */
1751
1752 static int s_tls_client_channel_negotiation_no_verify_expired_fn(struct aws_allocator *allocator, void *ctx) {
1753 (void)ctx;
1754 return s_verify_good_host(allocator, s_expired_host_name, 443, &s_disable_verify_peer);
1755 }
1756
1757 AWS_TEST_CASE(tls_client_channel_negotiation_no_verify_expired, s_tls_client_channel_negotiation_no_verify_expired_fn)
1758
1759 static int s_tls_client_channel_negotiation_no_verify_wrong_host_fn(struct aws_allocator *allocator, void *ctx) {
1760 (void)ctx;
1761 return s_verify_good_host(allocator, s_wrong_host_name, 443, &s_disable_verify_peer);
1762 }
1763
1764 AWS_TEST_CASE(
1765 tls_client_channel_negotiation_no_verify_wrong_host,
1766 s_tls_client_channel_negotiation_no_verify_wrong_host_fn)
1767
1768 static int s_tls_client_channel_negotiation_no_verify_self_signed_fn(struct aws_allocator *allocator, void *ctx) {
1769 (void)ctx;
1770 return s_verify_good_host(allocator, s_self_signed_host_name, 443, &s_disable_verify_peer);
1771 }
1772
1773 AWS_TEST_CASE(
1774 tls_client_channel_negotiation_no_verify_self_signed,
1775 s_tls_client_channel_negotiation_no_verify_self_signed_fn)
1776
1777 static int s_tls_client_channel_negotiation_no_verify_untrusted_root_fn(struct aws_allocator *allocator, void *ctx) {
1778 (void)ctx;
1779 return s_verify_good_host(allocator, s_untrusted_root_host_name, 443, &s_disable_verify_peer);
1780 }
1781
1782 AWS_TEST_CASE(
1783 tls_client_channel_negotiation_no_verify_untrusted_root,
1784 s_tls_client_channel_negotiation_no_verify_untrusted_root_fn)
1785
1786 AWS_STATIC_STRING_FROM_LITERAL(s_revoked_host_name, "revoked.badssl.com");
1787
1788 # ifdef _WIN32
1789 /* On Windows, connecting to a revoked cert should fail with default options (revocation check enabled) */
1790 static int s_tls_client_channel_negotiation_error_revoked_fn(struct aws_allocator *allocator, void *ctx) {
1791 (void)ctx;
1792 return s_verify_negotiation_fails(allocator, s_revoked_host_name, 443, NULL);
1793 }
1794
1795 AWS_TEST_CASE(tls_client_channel_negotiation_error_revoked, s_tls_client_channel_negotiation_error_revoked_fn)
1796 # endif /* _WIN32 */
1797
1798 static void s_disable_revocation_check(struct aws_tls_ctx_options *options) {
1799 aws_tls_ctx_options_set_no_certificate_revocation(options, true);
1800 }
1801
1802 /* On Windows, connecting to a revoked cert should succeed when revocation checking is disabled */
1803 static int s_tls_client_channel_negotiation_revoked_no_check_fn(struct aws_allocator *allocator, void *ctx) {
1804 (void)ctx;
1805 return s_verify_good_host(allocator, s_revoked_host_name, 443, &s_disable_revocation_check);
1806 }
1807
1808 AWS_TEST_CASE(tls_client_channel_negotiation_revoked_no_check, s_tls_client_channel_negotiation_revoked_no_check_fn)
1809
1810 static void s_lower_tls_version_to_tls10(struct aws_tls_ctx_options *options) {
1811 aws_tls_ctx_options_set_minimum_tls_version(options, AWS_IO_TLSv1);
1812 }
1813
1814 static int s_tls_client_channel_negotiation_override_legacy_crypto_tls10_fn(
1815 struct aws_allocator *allocator,
1816 void *ctx) {
1817 (void)ctx;
1818 return s_verify_good_host(allocator, s_legacy_crypto_tls10_host_name, 1010, &s_lower_tls_version_to_tls10);
1819 }
1820
1821 AWS_TEST_CASE(
1822 tls_client_channel_negotiation_override_legacy_crypto_tls10,
1823 s_tls_client_channel_negotiation_override_legacy_crypto_tls10_fn)
1824
1825 static int s_tls_client_channel_negotiation_success_legacy_crypto_tls11_fn(struct aws_allocator *allocator, void *ctx) {
1826 (void)ctx;
1827 return s_verify_good_host(allocator, s_legacy_crypto_tls11_host_name, 1011, NULL);
1828 }
1829
1830 AWS_TEST_CASE(
1831 tls_client_channel_negotiation_success_legacy_crypto_tls11,
1832 s_tls_client_channel_negotiation_success_legacy_crypto_tls11_fn)
1833
1834 AWS_STATIC_STRING_FROM_LITERAL(s_uncommon_sha384_host_name, "sha384.badssl.com");
1835
1836 static int s_tls_client_channel_negotiation_success_sha384_fn(struct aws_allocator *allocator, void *ctx) {
1837 (void)ctx;
1838 return s_verify_good_host(allocator, s_uncommon_sha384_host_name, 443, NULL);
1839 }
1840
1841 AWS_TEST_CASE(tls_client_channel_negotiation_success_sha384, s_tls_client_channel_negotiation_success_sha384_fn)
1842
1843 AWS_STATIC_STRING_FROM_LITERAL(s_uncommon_sha512_host_name, "sha512.badssl.com");
1844
1845 static int s_tls_client_channel_negotiation_success_sha512_fn(struct aws_allocator *allocator, void *ctx) {
1846 (void)ctx;
1847 return s_verify_good_host(allocator, s_uncommon_sha512_host_name, 443, NULL);
1848 }
1849
1850 AWS_TEST_CASE(tls_client_channel_negotiation_success_sha512, s_tls_client_channel_negotiation_success_sha512_fn)
1851
1852 AWS_STATIC_STRING_FROM_LITERAL(s_uncommon_rsa8192_host_name, "rsa8192.badssl.com");
1853
1854 static int s_tls_client_channel_negotiation_success_rsa8192_fn(struct aws_allocator *allocator, void *ctx) {
1855 (void)ctx;
1856 return s_verify_good_host(allocator, s_uncommon_rsa8192_host_name, 443, NULL);
1857 }
1858
1859 AWS_TEST_CASE(tls_client_channel_negotiation_success_rsa8192, s_tls_client_channel_negotiation_success_rsa8192_fn)
1860
1861 AWS_STATIC_STRING_FROM_LITERAL(s_uncommon_incomplete_chain_host_name, "incomplete-chain.badssl.com");
1862
1863 static int s_tls_client_channel_negotiation_success_no_verify_incomplete_chain_fn(
1864 struct aws_allocator *allocator,
1865 void *ctx) {
1866 (void)ctx;
1867 return s_verify_good_host(allocator, s_uncommon_incomplete_chain_host_name, 443, s_disable_verify_peer);
1868 }
1869
1870 AWS_TEST_CASE(
1871 tls_client_channel_negotiation_success_no_verify_incomplete_chain,
1872 s_tls_client_channel_negotiation_success_no_verify_incomplete_chain_fn)
1873
1874 static int s_tls_client_channel_negotiation_success_no_verify_no_subject_fn(
1875 struct aws_allocator *allocator,
1876 void *ctx) {
1877 (void)ctx;
1878 return s_verify_good_host(allocator, s_uncommon_no_subject_host_name, 443, s_disable_verify_peer);
1879 }
1880
1881 AWS_TEST_CASE(
1882 tls_client_channel_negotiation_success_no_verify_no_subject,
1883 s_tls_client_channel_negotiation_success_no_verify_no_subject_fn)
1884
1885 static int s_tls_client_channel_negotiation_success_no_verify_no_common_name_fn(
1886 struct aws_allocator *allocator,
1887 void *ctx) {
1888 (void)ctx;
1889 return s_verify_good_host(allocator, s_uncommon_no_common_name_host_name, 443, s_disable_verify_peer);
1890 }
1891
1892 AWS_TEST_CASE(
1893 tls_client_channel_negotiation_success_no_verify_no_common_name,
1894 s_tls_client_channel_negotiation_success_no_verify_no_common_name_fn)
1895
1896 AWS_STATIC_STRING_FROM_LITERAL(s_common_tls12_host_name, "tls-v1-2.badssl.com");
1897
1898 static int s_tls_client_channel_negotiation_success_tls12_fn(struct aws_allocator *allocator, void *ctx) {
1899 (void)ctx;
1900 return s_verify_good_host(allocator, s_common_tls12_host_name, 1012, NULL);
1901 }
1902
1903 AWS_TEST_CASE(tls_client_channel_negotiation_success_tls12, s_tls_client_channel_negotiation_success_tls12_fn)
1904
1905 AWS_STATIC_STRING_FROM_LITERAL(s_common_sha256_host_name, "sha256.badssl.com");
1906
1907 static int s_tls_client_channel_negotiation_success_sha256_fn(struct aws_allocator *allocator, void *ctx) {
1908 (void)ctx;
1909 return s_verify_good_host(allocator, s_common_sha256_host_name, 443, NULL);
1910 }
1911
1912 AWS_TEST_CASE(tls_client_channel_negotiation_success_sha256, s_tls_client_channel_negotiation_success_sha256_fn)
1913
1914 AWS_STATIC_STRING_FROM_LITERAL(s_common_rsa2048_host_name, "rsa2048.badssl.com");
1915
1916 static int s_tls_client_channel_negotiation_success_rsa2048_fn(struct aws_allocator *allocator, void *ctx) {
1917 (void)ctx;
1918 return s_verify_good_host(allocator, s_common_rsa2048_host_name, 443, NULL);
1919 }
1920
1921 AWS_TEST_CASE(tls_client_channel_negotiation_success_rsa2048, s_tls_client_channel_negotiation_success_rsa2048_fn)
1922
1923 AWS_STATIC_STRING_FROM_LITERAL(s_common_extended_validation_host_name, "extended-validation.badssl.com");
1924
1925 static int s_tls_client_channel_negotiation_success_extended_validation_fn(struct aws_allocator *allocator, void *ctx) {
1926 (void)ctx;
1927 return s_verify_good_host(allocator, s_common_extended_validation_host_name, 443, NULL);
1928 }
1929
1930 AWS_TEST_CASE(
1931 tls_client_channel_negotiation_success_extended_validation,
1932 s_tls_client_channel_negotiation_success_extended_validation_fn)
1933
1934 AWS_STATIC_STRING_FROM_LITERAL(s_common_mozilla_modern_host_name, "mozilla-modern.badssl.com");
1935
1936 static int s_tls_client_channel_negotiation_success_mozilla_modern_fn(struct aws_allocator *allocator, void *ctx) {
1937 (void)ctx;
1938 return s_verify_good_host(allocator, s_common_mozilla_modern_host_name, 443, NULL);
1939 }
1940
1941 AWS_TEST_CASE(
1942 tls_client_channel_negotiation_success_mozilla_modern,
1943 s_tls_client_channel_negotiation_success_mozilla_modern_fn)
1944
1945 static void s_reset_arg_state(struct tls_test_args *setup_test_args) {
1946 setup_test_args->tls_levels_negotiated = 0;
1947 setup_test_args->shutdown_finished = false;
1948 setup_test_args->creation_callback_invoked = false;
1949 setup_test_args->setup_callback_invoked = false;
1950 }
1951
1952 static int s_tls_server_multiple_connections_fn(struct aws_allocator *allocator, void *ctx) {
1953 (void)ctx;
1954
1955 ASSERT_SUCCESS(s_tls_common_tester_init(allocator, &c_tester));
1956
1957 struct tls_test_args outgoing_args;
1958 ASSERT_SUCCESS(s_tls_test_arg_init(allocator, &outgoing_args, false, &c_tester));
1959
1960 struct tls_test_args incoming_args;
1961 ASSERT_SUCCESS(s_tls_test_arg_init(allocator, &incoming_args, true, &c_tester));
1962
1963 struct tls_local_server_tester local_server_tester;
1964 ASSERT_SUCCESS(s_tls_local_server_tester_init(
1965 allocator, &local_server_tester, &incoming_args, &c_tester, false, "server.crt", "server.key"));
1966
1967 struct tls_opt_tester client_tls_opt_tester;
1968 struct aws_byte_cursor server_name = aws_byte_cursor_from_c_str("localhost");
1969 ASSERT_SUCCESS(s_tls_client_opt_tester_init(allocator, &client_tls_opt_tester, server_name));
1970 aws_tls_connection_options_set_callbacks(
1971 &client_tls_opt_tester.opt, s_tls_on_negotiated, NULL, NULL, &outgoing_args);
1972
1973 struct aws_client_bootstrap_options bootstrap_options = {
1974 .event_loop_group = c_tester.el_group,
1975 .host_resolver = c_tester.resolver,
1976 };
1977 struct aws_client_bootstrap *client_bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
1978
1979 struct aws_socket_channel_bootstrap_options channel_options;
1980 AWS_ZERO_STRUCT(channel_options);
1981 channel_options.bootstrap = client_bootstrap;
1982 channel_options.host_name = local_server_tester.endpoint.address;
1983 channel_options.port = 0;
1984 channel_options.socket_options = &local_server_tester.socket_options;
1985 channel_options.tls_options = &client_tls_opt_tester.opt;
1986 channel_options.setup_callback = s_tls_handler_test_client_setup_callback;
1987 channel_options.shutdown_callback = s_tls_handler_test_client_shutdown_callback;
1988 channel_options.user_data = &outgoing_args;
1989
1990 ASSERT_SUCCESS(aws_client_bootstrap_new_socket_channel(&channel_options));
1991
1992 /* wait for both ends to setup */
1993 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
1994 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
1995 &c_tester.condition_variable, &c_tester.mutex, s_tls_channel_setup_predicate, &incoming_args));
1996 ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
1997 ASSERT_FALSE(incoming_args.error_invoked);
1998
1999 /* shut down */
2000 aws_channel_shutdown(incoming_args.channel, AWS_OP_SUCCESS);
2001 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
2002 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
2003 &c_tester.condition_variable, &c_tester.mutex, s_tls_channel_shutdown_predicate, &incoming_args));
2004 ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
2005
2006 /* no shutdown on the client necessary here (it should have been triggered by shutting down the other side). just
2007 * wait for the event to fire. */
2008 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
2009 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
2010 &c_tester.condition_variable, &c_tester.mutex, s_tls_channel_shutdown_predicate, &outgoing_args));
2011 ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
2012
2013 /* connect again! */
2014 s_reset_arg_state(&outgoing_args);
2015 s_reset_arg_state(&incoming_args);
2016
2017 ASSERT_SUCCESS(aws_client_bootstrap_new_socket_channel(&channel_options));
2018
2019 /* wait for both ends to setup */
2020 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
2021 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
2022 &c_tester.condition_variable, &c_tester.mutex, s_tls_channel_setup_predicate, &incoming_args));
2023 ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
2024 ASSERT_FALSE(incoming_args.error_invoked);
2025
2026 /* shut down */
2027 aws_channel_shutdown(incoming_args.channel, AWS_OP_SUCCESS);
2028 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
2029 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
2030 &c_tester.condition_variable, &c_tester.mutex, s_tls_channel_shutdown_predicate, &incoming_args));
2031 ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
2032
2033 /*no shutdown on the client necessary here (it should have been triggered by shutting down the other side). just
2034 * wait for the event to fire. */
2035 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
2036 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
2037 &c_tester.condition_variable, &c_tester.mutex, s_tls_channel_shutdown_predicate, &outgoing_args));
2038 ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
2039 aws_server_bootstrap_destroy_socket_listener(local_server_tester.server_bootstrap, local_server_tester.listener);
2040 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
2041 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
2042 &c_tester.condition_variable, &c_tester.mutex, s_tls_listener_destroy_predicate, &incoming_args));
2043 aws_mutex_unlock(&c_tester.mutex);
2044
2045 /* clean up */
2046 ASSERT_SUCCESS(s_tls_opt_tester_clean_up(&client_tls_opt_tester));
2047 aws_client_bootstrap_release(client_bootstrap);
2048 ASSERT_SUCCESS(s_tls_local_server_tester_clean_up(&local_server_tester));
2049 ASSERT_SUCCESS(s_tls_common_tester_clean_up(&c_tester));
2050
2051 return AWS_OP_SUCCESS;
2052 }
2053 AWS_TEST_CASE(tls_server_multiple_connections, s_tls_server_multiple_connections_fn)
2054
2055 struct shutdown_listener_tester {
2056 struct aws_socket *listener;
2057 struct aws_server_bootstrap *server_bootstrap;
2058 struct tls_test_args *outgoing_args; /* client args */
2059 struct aws_socket client_socket;
2060 };
2061
2062 static bool s_client_socket_closed_predicate(void *user_data) {
2063 struct tls_test_args *args = user_data;
2064 return args->socket_close_invoked;
2065 }
2066
2067 static void s_close_client_socket_task(struct aws_task *task, void *arg, enum aws_task_status status) {
2068 (void)status;
2069 struct shutdown_listener_tester *tester = arg;
2070
2071 /* Free task memory */
2072 aws_mem_release(tester->outgoing_args->allocator, task);
2073
2074 /* Close socket and notify */
2075 AWS_FATAL_ASSERT(aws_socket_close(&tester->client_socket) == AWS_OP_SUCCESS);
2076
2077 AWS_FATAL_ASSERT(aws_mutex_lock(tester->outgoing_args->mutex) == AWS_OP_SUCCESS);
2078 tester->outgoing_args->socket_close_invoked = true;
2079 AWS_FATAL_ASSERT(aws_mutex_unlock(tester->outgoing_args->mutex) == AWS_OP_SUCCESS);
2080 AWS_FATAL_ASSERT(aws_condition_variable_notify_one(tester->outgoing_args->condition_variable) == AWS_OP_SUCCESS);
2081 }
2082
2083 static void s_on_client_connected_do_hangup(struct aws_socket *socket, int error_code, void *user_data) {
2084 AWS_FATAL_ASSERT(error_code == 0);
2085 struct shutdown_listener_tester *tester = user_data;
2086 tester->client_socket = *socket;
2087
2088 /* wait 1 sec so server side has time to setup the channel, then close the socket */
2089 uint64_t run_at_ns;
2090 aws_event_loop_current_clock_time(socket->event_loop, &run_at_ns);
2091 run_at_ns += aws_timestamp_convert(1, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL);
2092 struct aws_task *close_client_socket_task =
2093 aws_mem_acquire(tester->outgoing_args->allocator, sizeof(struct aws_task));
2094 aws_task_init(close_client_socket_task, s_close_client_socket_task, tester, "wait_close_client_socket");
2095 aws_event_loop_schedule_task_future(socket->event_loop, close_client_socket_task, run_at_ns);
2096 }
2097
2098 /* Test that server can handle a hangup in the middle of TLS negotiation */
2099 static int s_tls_server_hangup_during_negotiation_fn(struct aws_allocator *allocator, void *ctx) {
2100 (void)ctx;
2101
2102 ASSERT_SUCCESS(s_tls_common_tester_init(allocator, &c_tester));
2103
2104 struct tls_test_args outgoing_args;
2105 ASSERT_SUCCESS(s_tls_test_arg_init(allocator, &outgoing_args, false, &c_tester));
2106
2107 struct tls_test_args incoming_args;
2108 ASSERT_SUCCESS(s_tls_test_arg_init(allocator, &incoming_args, true, &c_tester));
2109
2110 struct tls_local_server_tester local_server_tester;
2111 ASSERT_SUCCESS(s_tls_local_server_tester_init(
2112 allocator, &local_server_tester, &incoming_args, &c_tester, false, "server.crt", "server.key"));
2113
2114 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
2115
2116 struct shutdown_listener_tester *shutdown_tester =
2117 aws_mem_acquire(allocator, sizeof(struct shutdown_listener_tester));
2118 shutdown_tester->server_bootstrap = local_server_tester.server_bootstrap;
2119 shutdown_tester->listener = local_server_tester.listener;
2120 shutdown_tester->outgoing_args = &outgoing_args;
2121
2122 /* Use a raw aws_socket for the client, instead of a full-blown TLS channel.
2123 * This lets us hang up on the server, instead of automatically going through with proper TLS negotiation */
2124 ASSERT_SUCCESS(aws_socket_init(&shutdown_tester->client_socket, allocator, &local_server_tester.socket_options));
2125
2126 struct aws_socket_connect_options connect_options = {
2127 .remote_endpoint = &local_server_tester.endpoint,
2128 .event_loop = aws_event_loop_group_get_next_loop(c_tester.el_group),
2129 .on_connection_result = s_on_client_connected_do_hangup,
2130 .user_data = shutdown_tester};
2131
2132 /* Upon connecting, immediately close the socket */
2133 ASSERT_SUCCESS(aws_socket_connect(&shutdown_tester->client_socket, &connect_options));
2134
2135 /* Wait for client socket to close */
2136 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
2137 &c_tester.condition_variable, &c_tester.mutex, s_client_socket_closed_predicate, &outgoing_args));
2138
2139 /* Destroy listener socket and wait for shutdown to complete */
2140 aws_server_bootstrap_destroy_socket_listener(shutdown_tester->server_bootstrap, shutdown_tester->listener);
2141
2142 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
2143 &c_tester.condition_variable, &c_tester.mutex, s_tls_listener_destroy_predicate, &incoming_args));
2144
2145 ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
2146 /* clean up */
2147 aws_socket_clean_up(&shutdown_tester->client_socket);
2148 aws_mem_release(allocator, shutdown_tester);
2149 /* cannot double free the listener */
2150 ASSERT_SUCCESS(s_tls_opt_tester_clean_up(&local_server_tester.server_tls_opt_tester));
2151 aws_server_bootstrap_release(local_server_tester.server_bootstrap);
2152 ASSERT_SUCCESS(s_tls_common_tester_clean_up(&c_tester));
2153
2154 // Should only be invoked on successful channel setup. Verify we did not get a shutdown callback.
2155 ASSERT_INT_EQUALS(false, outgoing_args.shutdown_finished);
2156
2157 return AWS_OP_SUCCESS;
2158 }
2159 AWS_TEST_CASE(tls_server_hangup_during_negotiation, s_tls_server_hangup_during_negotiation_fn)
2160
2161 static void s_creation_callback_test_channel_creation_callback(
2162 struct aws_client_bootstrap *bootstrap,
2163 int error_code,
2164 struct aws_channel *channel,
2165 void *user_data) {
2166
2167 (void)bootstrap;
2168 (void)error_code;
2169
2170 struct tls_test_args *setup_test_args = (struct tls_test_args *)user_data;
2171
2172 setup_test_args->creation_callback_invoked = true;
2173 setup_test_args->channel = channel;
2174
2175 struct aws_crt_statistics_handler *stats_handler = aws_statistics_handler_new_test(bootstrap->allocator);
2176 aws_atomic_store_ptr(&c_tester.stats_handler, stats_handler);
2177
2178 aws_channel_set_statistics_handler(channel, stats_handler);
2179 }
2180
2181 static struct aws_event_loop *s_default_new_event_loop(
2182 struct aws_allocator *allocator,
2183 const struct aws_event_loop_options *options,
2184 void *user_data) {
2185
2186 (void)user_data;
2187 return aws_event_loop_new(allocator, options);
2188 }
2189
2190 static int s_statistic_test_clock_fn(uint64_t *timestamp) {
2191 *timestamp = aws_atomic_load_int(&c_tester.current_time_ns);
2192
2193 return AWS_OP_SUCCESS;
2194 }
2195
2196 static int s_tls_common_tester_statistics_init(struct aws_allocator *allocator, struct tls_common_tester *tester) {
2197
2198 aws_io_library_init(allocator);
2199
2200 AWS_ZERO_STRUCT(*tester);
2201
2202 struct aws_mutex mutex = AWS_MUTEX_INIT;
2203 struct aws_condition_variable condition_variable = AWS_CONDITION_VARIABLE_INIT;
2204 tester->mutex = mutex;
2205 tester->condition_variable = condition_variable;
2206 aws_atomic_store_int(&tester->current_time_ns, 0);
2207 aws_atomic_store_ptr(&tester->stats_handler, NULL);
2208
2209 struct aws_event_loop_group_options elg_options = {
2210 .loop_count = 1,
2211 .clock_override = s_statistic_test_clock_fn,
2212 };
2213 tester->el_group = aws_event_loop_group_new_internal(allocator, &elg_options, s_default_new_event_loop, NULL);
2214
2215 struct aws_host_resolver_default_options resolver_options = {
2216 .el_group = tester->el_group,
2217 .max_entries = 1,
2218 };
2219 tester->resolver = aws_host_resolver_new_default(allocator, &resolver_options);
2220
2221 return AWS_OP_SUCCESS;
2222 }
2223
2224 static bool s_stats_processed_predicate(void *user_data) {
2225 struct aws_crt_statistics_handler *stats_handler = user_data;
2226 struct aws_statistics_handler_test_impl *stats_impl = stats_handler->impl;
2227
2228 return stats_impl->total_bytes_read > 0 && stats_impl->total_bytes_written > 0 &&
2229 stats_impl->tls_status != AWS_TLS_NEGOTIATION_STATUS_NONE;
2230 }
2231
2232 static int s_tls_channel_statistics_test(struct aws_allocator *allocator, void *ctx) {
2233 (void)ctx;
2234 aws_io_library_init(allocator);
2235
2236 ASSERT_SUCCESS(s_tls_common_tester_statistics_init(allocator, &c_tester));
2237
2238 struct aws_byte_buf read_tag = aws_byte_buf_from_c_str("This is some data.");
2239 struct aws_byte_buf write_tag = aws_byte_buf_from_c_str("Created from a blend of heirloom and cider apples");
2240
2241 uint8_t incoming_received_message[128] = {0};
2242 uint8_t outgoing_received_message[128] = {0};
2243
2244 struct tls_test_rw_args incoming_rw_args;
2245 ASSERT_SUCCESS(s_tls_rw_args_init(
2246 &incoming_rw_args,
2247 &c_tester,
2248 aws_byte_buf_from_empty_array(incoming_received_message, sizeof(incoming_received_message))));
2249
2250 struct tls_test_rw_args outgoing_rw_args;
2251 ASSERT_SUCCESS(s_tls_rw_args_init(
2252 &outgoing_rw_args,
2253 &c_tester,
2254 aws_byte_buf_from_empty_array(outgoing_received_message, sizeof(outgoing_received_message))));
2255
2256 struct tls_test_args outgoing_args;
2257 ASSERT_SUCCESS(s_tls_test_arg_init(allocator, &outgoing_args, false, &c_tester));
2258
2259 struct tls_test_args incoming_args;
2260 ASSERT_SUCCESS(s_tls_test_arg_init(allocator, &incoming_args, true, &c_tester));
2261
2262 struct tls_local_server_tester local_server_tester;
2263 ASSERT_SUCCESS(s_tls_local_server_tester_init(
2264 allocator, &local_server_tester, &incoming_args, &c_tester, false, "server.crt", "server.key"));
2265
2266 struct aws_channel_handler *outgoing_rw_handler =
2267 rw_handler_new(allocator, s_tls_test_handle_read, s_tls_test_handle_write, true, 10000, &outgoing_rw_args);
2268 ASSERT_NOT_NULL(outgoing_rw_handler);
2269
2270 struct aws_channel_handler *incoming_rw_handler =
2271 rw_handler_new(allocator, s_tls_test_handle_read, s_tls_test_handle_write, true, 10000, &incoming_rw_args);
2272 ASSERT_NOT_NULL(incoming_rw_handler);
2273
2274 incoming_args.rw_handler = incoming_rw_handler;
2275 outgoing_args.rw_handler = outgoing_rw_handler;
2276
2277 struct tls_opt_tester client_tls_opt_tester;
2278 struct aws_byte_cursor server_name = aws_byte_cursor_from_c_str("localhost");
2279 ASSERT_SUCCESS(s_tls_client_opt_tester_init(allocator, &client_tls_opt_tester, server_name));
2280 aws_tls_connection_options_set_callbacks(
2281 &client_tls_opt_tester.opt, s_tls_on_negotiated, NULL, NULL, &outgoing_args);
2282
2283 struct aws_client_bootstrap_options bootstrap_options;
2284 AWS_ZERO_STRUCT(bootstrap_options);
2285 bootstrap_options.event_loop_group = c_tester.el_group;
2286 bootstrap_options.host_resolver = c_tester.resolver;
2287
2288 struct aws_client_bootstrap *client_bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
2289
2290 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
2291
2292 struct aws_socket_channel_bootstrap_options channel_options;
2293 AWS_ZERO_STRUCT(channel_options);
2294 channel_options.bootstrap = client_bootstrap;
2295 channel_options.host_name = local_server_tester.endpoint.address;
2296 channel_options.port = 0;
2297 channel_options.socket_options = &local_server_tester.socket_options;
2298 channel_options.tls_options = &client_tls_opt_tester.opt;
2299 channel_options.creation_callback = s_creation_callback_test_channel_creation_callback;
2300 channel_options.setup_callback = s_tls_handler_test_client_setup_callback;
2301 channel_options.shutdown_callback = s_tls_handler_test_client_shutdown_callback;
2302 channel_options.user_data = &outgoing_args;
2303
2304 ASSERT_SUCCESS(aws_client_bootstrap_new_socket_channel(&channel_options));
2305
2306 /* put this here to verify ownership semantics are correct. This should NOT cause a segfault. If it does, ya
2307 * done messed up. */
2308 aws_tls_connection_options_clean_up(&client_tls_opt_tester.opt);
2309 /* wait for both ends to setup */
2310 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
2311 &c_tester.condition_variable, &c_tester.mutex, s_tls_channel_setup_predicate, &incoming_args));
2312 ASSERT_FALSE(incoming_args.error_invoked);
2313
2314 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
2315 &c_tester.condition_variable, &c_tester.mutex, s_tls_channel_setup_predicate, &outgoing_args));
2316 ASSERT_FALSE(outgoing_args.error_invoked);
2317
2318 ASSERT_TRUE(outgoing_args.creation_callback_invoked);
2319
2320 /* Do the IO operations */
2321 rw_handler_write(outgoing_args.rw_handler, outgoing_args.rw_slot, &write_tag);
2322 rw_handler_write(incoming_args.rw_handler, incoming_args.rw_slot, &read_tag);
2323
2324 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
2325 &c_tester.condition_variable, &c_tester.mutex, s_tls_test_read_predicate, &incoming_rw_args));
2326 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
2327 &c_tester.condition_variable, &c_tester.mutex, s_tls_test_read_predicate, &outgoing_rw_args));
2328
2329 uint64_t ms_to_ns = aws_timestamp_convert(1, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL);
2330
2331 aws_atomic_store_int(&c_tester.current_time_ns, (size_t)ms_to_ns);
2332
2333 struct aws_crt_statistics_handler *stats_handler = aws_atomic_load_ptr(&c_tester.stats_handler);
2334 struct aws_statistics_handler_test_impl *stats_impl = stats_handler->impl;
2335
2336 aws_mutex_lock(&stats_impl->lock);
2337
2338 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
2339 &stats_impl->signal, &stats_impl->lock, s_stats_processed_predicate, stats_handler));
2340
2341 ASSERT_TRUE(stats_impl->total_bytes_read >= read_tag.len);
2342 ASSERT_TRUE(stats_impl->total_bytes_written >= write_tag.len);
2343 ASSERT_TRUE(stats_impl->tls_status == AWS_TLS_NEGOTIATION_STATUS_SUCCESS);
2344
2345 aws_mutex_unlock(&stats_impl->lock);
2346
2347 aws_channel_shutdown(incoming_args.channel, AWS_OP_SUCCESS);
2348 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
2349 &c_tester.condition_variable, &c_tester.mutex, s_tls_channel_shutdown_predicate, &incoming_args));
2350
2351 /*no shutdown on the client necessary here (it should have been triggered by shutting down the other side). just
2352 * wait for the event to fire. */
2353 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
2354 &c_tester.condition_variable, &c_tester.mutex, s_tls_channel_shutdown_predicate, &outgoing_args));
2355 aws_server_bootstrap_destroy_socket_listener(local_server_tester.server_bootstrap, local_server_tester.listener);
2356 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
2357 &c_tester.condition_variable, &c_tester.mutex, s_tls_listener_destroy_predicate, &incoming_args));
2358 aws_mutex_unlock(&c_tester.mutex);
2359 /* clean up */
2360 ASSERT_SUCCESS(s_tls_opt_tester_clean_up(&client_tls_opt_tester));
2361 ASSERT_SUCCESS(s_tls_local_server_tester_clean_up(&local_server_tester));
2362 aws_client_bootstrap_release(client_bootstrap);
2363 ASSERT_SUCCESS(s_tls_common_tester_clean_up(&c_tester));
2364
2365 return AWS_OP_SUCCESS;
2366 }
2367
2368 AWS_TEST_CASE(tls_channel_statistics_test, s_tls_channel_statistics_test)
2369
2370 static int s_tls_certificate_chain_test(struct aws_allocator *allocator, void *ctx) {
2371 (void)ctx;
2372
2373 ASSERT_SUCCESS(s_tls_common_tester_init(allocator, &c_tester));
2374
2375 struct tls_test_args outgoing_args;
2376 ASSERT_SUCCESS(s_tls_test_arg_init(allocator, &outgoing_args, false, &c_tester));
2377
2378 struct tls_test_args incoming_args;
2379 ASSERT_SUCCESS(s_tls_test_arg_init(allocator, &incoming_args, true, &c_tester));
2380
2381 struct tls_local_server_tester local_server_tester;
2382 ASSERT_SUCCESS(s_tls_local_server_tester_init(
2383 allocator, &local_server_tester, &incoming_args, &c_tester, false, "server_chain.crt", "server.key"));
2384
2385 struct tls_opt_tester client_tls_opt_tester;
2386 struct aws_byte_cursor server_name = aws_byte_cursor_from_c_str("localhost");
2387 ASSERT_SUCCESS(s_tls_client_opt_tester_init(allocator, &client_tls_opt_tester, server_name));
2388 aws_tls_connection_options_set_callbacks(
2389 &client_tls_opt_tester.opt, s_tls_on_negotiated, NULL, NULL, &outgoing_args);
2390
2391 struct aws_client_bootstrap_options bootstrap_options = {
2392 .event_loop_group = c_tester.el_group,
2393 .host_resolver = c_tester.resolver,
2394 };
2395 struct aws_client_bootstrap *client_bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
2396
2397 struct aws_socket_channel_bootstrap_options channel_options;
2398 AWS_ZERO_STRUCT(channel_options);
2399 channel_options.bootstrap = client_bootstrap;
2400 channel_options.host_name = local_server_tester.endpoint.address;
2401 channel_options.port = 0;
2402 channel_options.socket_options = &local_server_tester.socket_options;
2403 channel_options.tls_options = &client_tls_opt_tester.opt;
2404 channel_options.setup_callback = s_tls_handler_test_client_setup_callback;
2405 channel_options.shutdown_callback = s_tls_handler_test_client_shutdown_callback;
2406 channel_options.user_data = &outgoing_args;
2407
2408 /* connect! */
2409 ASSERT_SUCCESS(aws_client_bootstrap_new_socket_channel(&channel_options));
2410
2411 /* wait for both ends to setup */
2412 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
2413 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
2414 &c_tester.condition_variable, &c_tester.mutex, s_tls_channel_setup_predicate, &incoming_args));
2415 ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
2416 ASSERT_FALSE(incoming_args.error_invoked);
2417
2418 /* shut down */
2419 aws_channel_shutdown(incoming_args.channel, AWS_OP_SUCCESS);
2420 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
2421 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
2422 &c_tester.condition_variable, &c_tester.mutex, s_tls_channel_shutdown_predicate, &incoming_args));
2423 ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
2424
2425 /* no shutdown on the client necessary here (it should have been triggered by shutting down the other side). just
2426 * wait for the event to fire. */
2427 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
2428 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
2429 &c_tester.condition_variable, &c_tester.mutex, s_tls_channel_shutdown_predicate, &outgoing_args));
2430 ASSERT_SUCCESS(aws_mutex_unlock(&c_tester.mutex));
2431
2432 /* clean up */
2433 aws_server_bootstrap_destroy_socket_listener(local_server_tester.server_bootstrap, local_server_tester.listener);
2434 ASSERT_SUCCESS(aws_mutex_lock(&c_tester.mutex));
2435 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
2436 &c_tester.condition_variable, &c_tester.mutex, s_tls_listener_destroy_predicate, &incoming_args));
2437 aws_mutex_unlock(&c_tester.mutex);
2438
2439 ASSERT_SUCCESS(s_tls_opt_tester_clean_up(&client_tls_opt_tester));
2440 aws_client_bootstrap_release(client_bootstrap);
2441 ASSERT_SUCCESS(s_tls_local_server_tester_clean_up(&local_server_tester));
2442 ASSERT_SUCCESS(s_tls_common_tester_clean_up(&c_tester));
2443
2444 return AWS_OP_SUCCESS;
2445 }
2446
2447 AWS_TEST_CASE(tls_certificate_chain_test, s_tls_certificate_chain_test)
2448
2449 ///////////////////////////////////////////////////////////////
2450
2451 struct channel_stat_test_context {
2452 struct aws_allocator *allocator;
2453 struct tls_opt_tester *tls_tester;
2454 struct aws_mutex lock;
2455 struct aws_condition_variable signal;
2456 bool setup_completed;
2457 bool shutdown_completed;
2458 int error_code;
2459 };
2460
2461 static void s_channel_setup_stat_test_context_init(
2462 struct channel_stat_test_context *context,
2463 struct aws_allocator *allocator,
2464 struct tls_opt_tester *tls_tester) {
2465
2466 AWS_ZERO_STRUCT(*context);
2467 aws_mutex_init(&context->lock);
2468 aws_condition_variable_init(&context->signal);
2469 context->allocator = allocator;
2470 context->tls_tester = tls_tester;
2471 }
2472
2473 static void s_channel_setup_stat_test_context_clean_up(struct channel_stat_test_context *context) {
2474 aws_mutex_clean_up(&context->lock);
2475 aws_condition_variable_clean_up(&context->signal);
2476 }
2477
2478 static int s_dummy_process_message(
2479 struct aws_channel_handler *handler,
2480 struct aws_channel_slot *slot,
2481 struct aws_io_message *message) {
2482 (void)handler;
2483 (void)slot;
2484
2485 aws_mem_release(message->allocator, message);
2486 return AWS_OP_SUCCESS;
2487 }
2488
2489 static int s_dummy_increment_read_window(
2490 struct aws_channel_handler *handler,
2491 struct aws_channel_slot *slot,
2492 size_t size) {
2493 (void)handler;
2494 (void)slot;
2495 (void)size;
2496
2497 return AWS_OP_SUCCESS;
2498 }
2499
2500 static int s_dummy_shutdown(
2501 struct aws_channel_handler *handler,
2502 struct aws_channel_slot *slot,
2503 enum aws_channel_direction dir,
2504 int error_code,
2505 bool free_scarce_resources_immediately) {
2506
2507 (void)handler;
2508 return aws_channel_slot_on_handler_shutdown_complete(slot, dir, error_code, free_scarce_resources_immediately);
2509 }
2510
2511 static size_t s_dummy_initial_window_size(struct aws_channel_handler *handler) {
2512 (void)handler;
2513
2514 return 10000;
2515 }
2516
2517 static size_t s_dummy_message_overhead(struct aws_channel_handler *handler) {
2518 (void)handler;
2519
2520 return 0;
2521 }
2522
2523 static void s_dummy_destroy(struct aws_channel_handler *handler) {
2524 aws_mem_release(handler->alloc, handler);
2525 }
2526
2527 static struct aws_channel_handler_vtable s_dummy_handler_vtable = {
2528 .process_read_message = s_dummy_process_message,
2529 .process_write_message = s_dummy_process_message,
2530 .increment_read_window = s_dummy_increment_read_window,
2531 .shutdown = s_dummy_shutdown,
2532 .initial_window_size = s_dummy_initial_window_size,
2533 .message_overhead = s_dummy_message_overhead,
2534 .destroy = s_dummy_destroy,
2535 };
2536
2537 static struct aws_channel_handler *aws_channel_handler_new_dummy(struct aws_allocator *allocator) {
2538 struct aws_channel_handler *handler = aws_mem_acquire(allocator, sizeof(struct aws_channel_handler));
2539 handler->alloc = allocator;
2540 handler->vtable = &s_dummy_handler_vtable;
2541 handler->impl = NULL;
2542
2543 return handler;
2544 }
2545
2546 static bool s_setup_completed_predicate(void *arg) {
2547 struct channel_stat_test_context *context = (struct channel_stat_test_context *)arg;
2548 return context->setup_completed;
2549 }
2550
2551 static bool s_shutdown_completed_predicate(void *arg) {
2552 struct channel_stat_test_context *context = (struct channel_stat_test_context *)arg;
2553 return context->shutdown_completed;
2554 }
2555
2556 static void s_on_shutdown_completed(struct aws_channel *channel, int error_code, void *user_data) {
2557 (void)channel;
2558 struct channel_stat_test_context *context = (struct channel_stat_test_context *)user_data;
2559
2560 aws_mutex_lock(&context->lock);
2561 context->shutdown_completed = true;
2562 context->error_code = error_code;
2563 aws_mutex_unlock(&context->lock);
2564
2565 aws_condition_variable_notify_one(&context->signal);
2566 }
2567
2568 static const int s_tls_timeout_ms = 1000;
2569
2570 static void s_on_setup_completed(struct aws_channel *channel, int error_code, void *user_data) {
2571 (void)channel;
2572 struct channel_stat_test_context *context = (struct channel_stat_test_context *)user_data;
2573
2574 /* attach a dummy channel handler */
2575 struct aws_channel_slot *dummy_slot = aws_channel_slot_new(channel);
2576
2577 struct aws_channel_handler *dummy_handler = aws_channel_handler_new_dummy(context->allocator);
2578 aws_channel_slot_set_handler(dummy_slot, dummy_handler);
2579
2580 /* attach a tls channel handler and start negotiation */
2581 aws_channel_setup_client_tls(dummy_slot, &context->tls_tester->opt);
2582
2583 aws_mutex_lock(&context->lock);
2584 context->error_code = error_code;
2585 context->setup_completed = true;
2586 aws_mutex_unlock(&context->lock);
2587 aws_condition_variable_notify_one(&context->signal);
2588 }
2589
2590 static int s_test_tls_negotiation_timeout(struct aws_allocator *allocator, void *ctx) {
2591 (void)ctx;
2592 aws_io_library_init(allocator);
2593
2594 struct aws_event_loop *event_loop = aws_event_loop_new_default(allocator, aws_high_res_clock_get_ticks);
2595
2596 ASSERT_SUCCESS(aws_event_loop_run(event_loop));
2597
2598 struct tls_opt_tester tls_test_context;
2599 s_tls_client_opt_tester_init(allocator, &tls_test_context, aws_byte_cursor_from_c_str("derp.com"));
2600 tls_test_context.opt.timeout_ms = s_tls_timeout_ms;
2601
2602 struct channel_stat_test_context channel_context;
2603 s_channel_setup_stat_test_context_init(&channel_context, allocator, &tls_test_context);
2604
2605 struct aws_channel_options args = {
2606 .on_setup_completed = s_on_setup_completed,
2607 .setup_user_data = &channel_context,
2608 .on_shutdown_completed = s_on_shutdown_completed,
2609 .shutdown_user_data = &channel_context,
2610 .event_loop = event_loop,
2611 };
2612
2613 /* set up the channel */
2614 ASSERT_SUCCESS(aws_mutex_lock(&channel_context.lock));
2615 struct aws_channel *channel = aws_channel_new(allocator, &args);
2616 ASSERT_NOT_NULL(channel);
2617 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
2618 &channel_context.signal, &channel_context.lock, s_setup_completed_predicate, &channel_context));
2619 aws_mutex_unlock(&channel_context.lock);
2620
2621 /* wait for the timeout */
2622 aws_thread_current_sleep(aws_timestamp_convert(s_tls_timeout_ms, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL));
2623
2624 aws_mutex_lock(&channel_context.lock);
2625 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
2626 &channel_context.signal, &channel_context.lock, s_shutdown_completed_predicate, &channel_context));
2627
2628 ASSERT_TRUE(channel_context.error_code == AWS_IO_TLS_NEGOTIATION_TIMEOUT);
2629
2630 aws_mutex_unlock(&channel_context.lock);
2631
2632 aws_channel_destroy(channel);
2633 aws_event_loop_destroy(event_loop);
2634
2635 s_tls_opt_tester_clean_up(&tls_test_context);
2636
2637 s_channel_setup_stat_test_context_clean_up(&channel_context);
2638
2639 aws_io_library_clean_up();
2640
2641 return AWS_OP_SUCCESS;
2642 }
2643
2644 AWS_TEST_CASE(test_tls_negotiation_timeout, s_test_tls_negotiation_timeout)
2645
2646 struct import_info {
2647 struct aws_allocator *allocator;
2648 struct aws_byte_buf cert_buf;
2649 struct aws_byte_buf key_buf;
2650 struct aws_thread thread;
2651 struct aws_tls_ctx *tls;
2652 };
2653
2654 static void s_import_cert(void *ctx) {
2655 (void)ctx;
2656 struct import_info *import = ctx;
2657 struct aws_byte_cursor cert_cur = aws_byte_cursor_from_buf(&import->cert_buf);
2658 struct aws_byte_cursor key_cur = aws_byte_cursor_from_buf(&import->key_buf);
2659 struct aws_tls_ctx_options tls_options = {0};
2660 AWS_FATAL_ASSERT(
2661 AWS_OP_SUCCESS == aws_tls_ctx_options_init_client_mtls(&tls_options, import->allocator, &cert_cur, &key_cur));
2662
2663 /* import happens in here */
2664 import->tls = aws_tls_client_ctx_new(import->allocator, &tls_options);
2665 AWS_FATAL_ASSERT(import->tls);
2666
2667 aws_tls_ctx_options_clean_up(&tls_options);
2668 }
2669
2670 # define NUM_PAIRS 2
2671 static int s_test_concurrent_cert_import(struct aws_allocator *allocator, void *ctx) {
2672 (void)ctx;
2673
2674 aws_io_library_init(allocator);
2675
2676 AWS_VARIABLE_LENGTH_ARRAY(struct import_info, imports, NUM_PAIRS);
2677
2678 /* setup, note that all I/O should be before the threads are launched */
2679 for (size_t idx = 0; idx < NUM_PAIRS; ++idx) {
2680 struct import_info *import = &imports[idx];
2681 import->allocator = allocator;
2682
2683 char filename[1024];
2684 snprintf(filename, sizeof(filename), "testcert%u.pem", (uint32_t)idx);
2685 ASSERT_SUCCESS(aws_byte_buf_init_from_file(&import->cert_buf, import->allocator, filename));
2686
2687 snprintf(filename, sizeof(filename), "testkey.pem");
2688 ASSERT_SUCCESS(aws_byte_buf_init_from_file(&import->key_buf, import->allocator, filename));
2689
2690 struct aws_thread *thread = &import->thread;
2691 ASSERT_SUCCESS(aws_thread_init(thread, allocator));
2692 }
2693
2694 /* run threads */
2695 const struct aws_thread_options *options = aws_default_thread_options();
2696 for (size_t idx = 0; idx < NUM_PAIRS; ++idx) {
2697 struct import_info *import = &imports[idx];
2698 struct aws_thread *thread = &import->thread;
2699 ASSERT_SUCCESS(aws_thread_launch(thread, s_import_cert, import, options));
2700 }
2701
2702 /* join and clean up */
2703 for (size_t idx = 0; idx < NUM_PAIRS; ++idx) {
2704 struct import_info *import = &imports[idx];
2705 struct aws_thread *thread = &import->thread;
2706 ASSERT_SUCCESS(aws_thread_join(thread));
2707 aws_tls_ctx_release(import->tls);
2708 aws_byte_buf_clean_up(&import->cert_buf);
2709 aws_byte_buf_clean_up(&import->key_buf);
2710 }
2711
2712 aws_io_library_clean_up();
2713
2714 return AWS_OP_SUCCESS;
2715 }
2716
2717 AWS_TEST_CASE(test_concurrent_cert_import, s_test_concurrent_cert_import)
2718
2719 static int s_test_duplicate_cert_import(struct aws_allocator *allocator, void *ctx) {
2720 (void)ctx;
2721
2722 aws_io_library_init(allocator);
2723 struct aws_byte_buf cert_buf = {0};
2724 struct aws_byte_buf key_buf = {0};
2725
2726 # if !defined(AWS_USE_SECITEM)
2727
2728 ASSERT_SUCCESS(aws_byte_buf_init_from_file(&cert_buf, allocator, "testcert0.pem"));
2729 ASSERT_SUCCESS(aws_byte_buf_init_from_file(&key_buf, allocator, "testkey.pem"));
2730 struct aws_byte_cursor cert_cur = aws_byte_cursor_from_buf(&cert_buf);
2731 struct aws_byte_cursor key_cur = aws_byte_cursor_from_buf(&key_buf);
2732 struct aws_tls_ctx_options tls_options = {0};
2733 AWS_FATAL_ASSERT(
2734 AWS_OP_SUCCESS == aws_tls_ctx_options_init_client_mtls(&tls_options, allocator, &cert_cur, &key_cur));
2735
2736 /* import happens in here */
2737 struct aws_tls_ctx *tls = aws_tls_client_ctx_new(allocator, &tls_options);
2738 AWS_FATAL_ASSERT(tls);
2739 aws_tls_ctx_release(tls);
2740 /* import the same certs twice */
2741 tls = aws_tls_client_ctx_new(allocator, &tls_options);
2742 AWS_FATAL_ASSERT(tls);
2743 aws_tls_ctx_release(tls);
2744
2745 aws_tls_ctx_options_clean_up(&tls_options);
2746 # endif /* !AWS_USE_SECITEM */
2747
2748 /* clean up */
2749 aws_byte_buf_clean_up(&cert_buf);
2750 aws_byte_buf_clean_up(&key_buf);
2751 aws_io_library_clean_up();
2752
2753 return AWS_OP_SUCCESS;
2754 }
2755
2756 AWS_TEST_CASE(test_duplicate_cert_import, s_test_duplicate_cert_import)
2757
2758 static int s_tls_destroy_null_context(struct aws_allocator *allocator, void *ctx) {
2759 (void)allocator;
2760 (void)ctx;
2761
2762 struct aws_tls_ctx *null_context = NULL;
2763
2764 /* Verify that we don't crash. */
2765 aws_tls_ctx_release(null_context);
2766
2767 return AWS_OP_SUCCESS;
2768 }
2769 AWS_TEST_CASE(tls_destroy_null_context, s_tls_destroy_null_context);
2770
2771 static int s_test_ecc_cert_import(struct aws_allocator *allocator, void *ctx) {
2772 (void)ctx;
2773 (void)allocator;
2774
2775 # ifndef AWS_OS_APPLE
2776 aws_io_library_init(allocator);
2777
2778 struct aws_byte_buf cert_buf;
2779 struct aws_byte_buf key_buf;
2780
2781 ASSERT_SUCCESS(aws_byte_buf_init_from_file(&cert_buf, allocator, "ecc-cert.pem"));
2782 ASSERT_SUCCESS(aws_byte_buf_init_from_file(&key_buf, allocator, "ecc-key.pem"));
2783
2784 struct aws_byte_cursor cert_cur = aws_byte_cursor_from_buf(&cert_buf);
2785 struct aws_byte_cursor key_cur = aws_byte_cursor_from_buf(&key_buf);
2786 struct aws_tls_ctx_options tls_options = {0};
2787 AWS_FATAL_ASSERT(
2788 AWS_OP_SUCCESS == aws_tls_ctx_options_init_client_mtls(&tls_options, allocator, &cert_cur, &key_cur));
2789
2790 /* import happens in here */
2791 struct aws_tls_ctx *tls_context = aws_tls_client_ctx_new(allocator, &tls_options);
2792 ASSERT_NOT_NULL(tls_context);
2793
2794 aws_tls_ctx_release(tls_context);
2795
2796 aws_tls_ctx_options_clean_up(&tls_options);
2797
2798 aws_byte_buf_clean_up(&cert_buf);
2799 aws_byte_buf_clean_up(&key_buf);
2800
2801 aws_io_library_clean_up();
2802 # endif /* AWS_OS_APPLE */
2803
2804 return AWS_OP_SUCCESS;
2805 }
2806
2807 AWS_TEST_CASE(test_ecc_cert_import, s_test_ecc_cert_import)
2808
2809 static int s_test_pkcs8_import(struct aws_allocator *allocator, void *ctx) {
2810 (void)ctx;
2811 (void)allocator;
2812
2813 aws_io_library_init(allocator);
2814
2815 struct aws_byte_buf cert_buf;
2816 struct aws_byte_buf key_buf;
2817
2818 ASSERT_SUCCESS(aws_byte_buf_init_from_file(&cert_buf, allocator, "unittests.crt"));
2819 ASSERT_SUCCESS(aws_byte_buf_init_from_file(&key_buf, allocator, "unittests.p8"));
2820
2821 struct aws_byte_cursor cert_cur = aws_byte_cursor_from_buf(&cert_buf);
2822 struct aws_byte_cursor key_cur = aws_byte_cursor_from_buf(&key_buf);
2823 struct aws_tls_ctx_options tls_options = {0};
2824 AWS_FATAL_ASSERT(
2825 AWS_OP_SUCCESS == aws_tls_ctx_options_init_client_mtls(&tls_options, allocator, &cert_cur, &key_cur));
2826
2827 /* import happens in here */
2828 struct aws_tls_ctx *tls_context = aws_tls_client_ctx_new(allocator, &tls_options);
2829 ASSERT_NOT_NULL(tls_context);
2830
2831 aws_tls_ctx_release(tls_context);
2832
2833 aws_tls_ctx_options_clean_up(&tls_options);
2834
2835 aws_byte_buf_clean_up(&cert_buf);
2836 aws_byte_buf_clean_up(&key_buf);
2837
2838 aws_io_library_clean_up();
2839
2840 return AWS_OP_SUCCESS;
2841 }
2842
2843 AWS_TEST_CASE(test_pkcs8_import, s_test_pkcs8_import)
2844
2845 static int s_test_tls_cipher_preference_fn(struct aws_allocator *allocator, void *ctx) {
2846 (void)ctx;
2847 aws_io_library_init(allocator);
2848
2849 struct aws_tls_ctx_options tls_options;
2850 aws_tls_ctx_options_init_default_client(&tls_options, allocator);
2851
2852 aws_tls_ctx_options_set_tls_cipher_preference(&tls_options, AWS_IO_TLS_CIPHER_PREF_TLSV1_2_2025_07);
2853 /* Creating tls context */
2854 struct aws_tls_ctx *tls_context = aws_tls_client_ctx_new(allocator, &tls_options);
2855 # ifdef USE_S2N
2856 if (s_is_apple_with_secure_transport(allocator)) {
2857 ASSERT_NULL(tls_context);
2858 ASSERT_INT_EQUALS(AWS_IO_TLS_CIPHER_PREF_UNSUPPORTED, aws_last_error());
2859 } else {
2860 ASSERT_NOT_NULL(tls_context);
2861 aws_tls_ctx_release(tls_context);
2862 }
2863 # else
2864 /* The cipher suite currently only available with S2N */
2865 ASSERT_NULL(tls_context);
2866 ASSERT_INT_EQUALS(AWS_IO_TLS_CIPHER_PREF_UNSUPPORTED, aws_last_error());
2867 # endif
2868
2869 aws_tls_ctx_options_clean_up(&tls_options);
2870 aws_io_library_clean_up();
2871
2872 return AWS_OP_SUCCESS;
2873 }
2874
2875 AWS_TEST_CASE(test_tls_cipher_preference, s_test_tls_cipher_preference_fn)
2876
2877 #endif /* BYO_CRYPTO */