Debian Codegraph @

crt/s2n/tests/unit/s2n_self_talk_ktls_test.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include <fcntl.h>
17 #include <netinet/in.h>
18 #include <string.h>
19 #include <sys/socket.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22
23 #include "s2n_test.h"
24 #include "testlib/s2n_testlib.h"
25 #include "tls/s2n_cipher_preferences.h"
26 #include "tls/s2n_ktls.h"
27 #include "tls/s2n_tls.h"
28 #include "utils/s2n_random.h"
29
30 /* There are issues with MacOS and FreeBSD so we define the constant ourselves.
31 * https://stackoverflow.com/a/34042435 */
32 #define S2N_TEST_INADDR_LOOPBACK 0x7f000001 /* 127.0.0.1 */
33
34 static S2N_RESULT s2n_setup_connections(struct s2n_connection *server,
35 struct s2n_connection *client, struct s2n_test_io_pair *io_pair)
36 {
37 RESULT_GUARD_POSIX(s2n_connections_set_io_pair(client, server, io_pair));
38
39 /* The test negotiate method assumes non-blocking sockets */
40 RESULT_GUARD_POSIX(s2n_fd_set_non_blocking(io_pair->server));
41 RESULT_GUARD_POSIX(s2n_fd_set_non_blocking(io_pair->client));
42
43 /* Real sockets are sometimes slow.
44 * Assume blocking will occur but is always recoverable.
45 */
46 while (true) {
47 int result = s2n_negotiate_test_server_and_client(server, client);
48 if (result == S2N_SUCCESS) {
49 break;
50 } else if (s2n_errno != S2N_ERR_IO_BLOCKED) {
51 RESULT_GUARD_POSIX(result);
52 }
53 };
54
55 /* Our IO methods are more predictable if they use blocking sockets. */
56 RESULT_GUARD_POSIX(s2n_fd_set_blocking(io_pair->server));
57 RESULT_GUARD_POSIX(s2n_fd_set_blocking(io_pair->client));
58 return S2N_RESULT_OK;
59 }
60
61 /* Unlike our other self-talk tests, this test cannot use AF_UNIX / AF_LOCAL.
62 * For a real self-talk test we need real kernel support for kTLS, and only
63 * AF_INET sockets support kTLS.
64 */
65 static S2N_RESULT s2n_new_inet_socket_pair(struct s2n_test_io_pair *io_pair)
66 {
67 RESULT_ENSURE_REF(io_pair);
68
69 int listener = socket(AF_INET, SOCK_STREAM, 0);
70 RESULT_ENSURE_GT(listener, 0);
71
72 struct sockaddr_in saddr = { 0 };
73 saddr.sin_family = AF_INET;
74 saddr.sin_addr.s_addr = htonl(S2N_TEST_INADDR_LOOPBACK);
75 saddr.sin_port = 0;
76
77 socklen_t addrlen = sizeof(saddr);
78 RESULT_ENSURE_EQ(bind(listener, (struct sockaddr *) &saddr, addrlen), 0);
79 RESULT_ENSURE_EQ(getsockname(listener, (struct sockaddr *) &saddr, &addrlen), 0);
80 RESULT_ENSURE_EQ(listen(listener, 1), 0);
81
82 io_pair->client = socket(AF_INET, SOCK_STREAM, 0);
83 RESULT_ENSURE_GT(io_pair->client, 0);
84
85 RESULT_ENSURE_EQ(connect(io_pair->client, (struct sockaddr *) &saddr, addrlen), 0);
86 io_pair->server = accept(listener, NULL, NULL);
87 RESULT_ENSURE_GT(io_pair->server, 0);
88 RESULT_ENSURE_EQ(close(listener), 0);
89 return S2N_RESULT_OK;
90 }
91
92 int main(int argc, char **argv)
93 {
94 BEGIN_TEST();
95
96 /* ktls is complicated to enable. We should ensure that it's actually enabled
97 * where we think we're testing it.
98 */
99 const bool ktls_expected = (getenv("S2N_KTLS_TESTING_EXPECTED") != NULL);
100 const bool ktls_keyupdate_expected = (getenv("S2N_KTLS_KEYUPDATE_TESTING_EXPECTED") != NULL);
101
102 if (!s2n_ktls_is_supported_on_platform() && !ktls_expected) {
103 END_TEST();
104 }
105
106 const s2n_mode modes[] = { S2N_CLIENT, S2N_SERVER };
107
108 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL,
109 s2n_cert_chain_and_key_ptr_free);
110 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
111 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
112
113 uint8_t test_data[100] = { 0 };
114 struct s2n_blob test_data_blob = { 0 };
115 EXPECT_SUCCESS(s2n_blob_init(&test_data_blob, test_data, sizeof(test_data)));
116 EXPECT_OK(s2n_get_public_random_data(&test_data_blob));
117
118 DEFER_CLEANUP(struct s2n_test_iovecs test_iovecs = { 0 }, s2n_test_iovecs_free);
119 size_t test_iovecs_lens[20] = { 5, 6, 1, 10, 0 };
120 EXPECT_OK(s2n_test_new_iovecs(&test_iovecs, &test_data_blob, test_iovecs_lens,
121 s2n_array_len(test_iovecs_lens)));
122
123 const size_t test_offsets[] = {
124 0,
125 test_iovecs_lens[0],
126 test_iovecs_lens[0] + 1,
127 test_iovecs_lens[0] + test_iovecs_lens[1],
128 sizeof(test_data) - 1,
129 sizeof(test_data),
130 };
131
132 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
133 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
134 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config));
135 /* Configure a TLS 1.2 policy */
136 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "20240501"));
137 EXPECT_SUCCESS(s2n_config_ktls_enable_unsafe_tls13(config));
138
139 /* Even if we detected ktls support at compile time, enabling ktls
140 * can fail at runtime if the system is not properly configured.
141 */
142 bool ktls_send_supported = true;
143 bool ktls_recv_supported = true;
144
145 /* Test enabling ktls for sending */
146 {
147 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
148 s2n_connection_ptr_free);
149 EXPECT_NOT_NULL(client);
150 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
151
152 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
153 s2n_connection_ptr_free);
154 EXPECT_NOT_NULL(server);
155 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
156
157 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
158 if (s2n_result_is_error(s2n_new_inet_socket_pair(&io_pair))) {
159 /* We should be able to setup AF_INET sockets everywhere, but if
160 * we can't, don't block the build unless the build explicitly expects
161 * to be able to test ktls.
162 */
163 EXPECT_FALSE(ktls_expected);
164 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
165 END_TEST();
166 }
167 EXPECT_OK(s2n_setup_connections(server, client, &io_pair));
168
169 if (s2n_connection_ktls_enable_send(client) == S2N_SUCCESS) {
170 EXPECT_SUCCESS(s2n_connection_ktls_enable_send(server));
171 } else {
172 EXPECT_FALSE(ktls_expected);
173 ktls_send_supported = false;
174 }
175
176 if (s2n_connection_ktls_enable_recv(client) == S2N_SUCCESS) {
177 EXPECT_SUCCESS(s2n_connection_ktls_enable_recv(server));
178 } else {
179 EXPECT_FALSE(ktls_expected);
180 ktls_recv_supported = false;
181 }
182 };
183
184 /* Test sending with ktls */
185 for (size_t mode_i = 0; mode_i < s2n_array_len(modes); mode_i++) {
186 if (!ktls_send_supported) {
187 break;
188 }
189
190 const s2n_mode mode = modes[mode_i];
191
192 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
193 s2n_connection_ptr_free);
194 EXPECT_NOT_NULL(client);
195 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
196
197 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
198 s2n_connection_ptr_free);
199 EXPECT_NOT_NULL(server);
200 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
201
202 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
203 EXPECT_OK(s2n_new_inet_socket_pair(&io_pair));
204 EXPECT_OK(s2n_setup_connections(server, client, &io_pair));
205
206 struct s2n_connection *conns[] = {
207 [S2N_CLIENT] = client,
208 [S2N_SERVER] = server,
209 };
210 struct s2n_connection *writer = conns[mode];
211 struct s2n_connection *reader = conns[S2N_PEER_MODE(mode)];
212 EXPECT_SUCCESS(s2n_connection_ktls_enable_send(writer));
213
214 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
215
216 /* Test: s2n_send */
217 for (size_t i = 0; i < 5; i++) {
218 int written = s2n_send(writer, test_data, sizeof(test_data), &blocked);
219 EXPECT_EQUAL(written, sizeof(test_data));
220 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
221
222 uint8_t buffer[sizeof(test_data)] = { 0 };
223 int read = s2n_recv(reader, buffer, sizeof(buffer), &blocked);
224 EXPECT_EQUAL(read, sizeof(test_data));
225 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
226
227 EXPECT_BYTEARRAY_EQUAL(test_data, buffer, read);
228 }
229
230 /* Test: s2n_sendv */
231 for (size_t i = 0; i < 5; i++) {
232 int written = s2n_sendv(writer,
233 test_iovecs.iovecs, test_iovecs.iovecs_count, &blocked);
234 EXPECT_EQUAL(written, sizeof(test_data));
235 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
236
237 uint8_t buffer[sizeof(test_data)] = { 0 };
238 int read = s2n_recv(reader, buffer, sizeof(buffer), &blocked);
239 EXPECT_EQUAL(read, sizeof(test_data));
240 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
241
242 EXPECT_BYTEARRAY_EQUAL(test_data, buffer, read);
243 }
244
245 /* Test: s2n_sendv_with_offset */
246 for (size_t offset_i = 0; offset_i < s2n_array_len(test_offsets); offset_i++) {
247 const size_t offset = test_offsets[offset_i];
248 const size_t expected_written = sizeof(test_data) - offset;
249
250 int written = s2n_sendv_with_offset(writer,
251 test_iovecs.iovecs, test_iovecs.iovecs_count, offset, &blocked);
252 EXPECT_EQUAL(written, expected_written);
253 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
254
255 uint8_t buffer[sizeof(test_data)] = { 0 };
256 int read = s2n_recv(reader, buffer, expected_written, &blocked);
257 EXPECT_EQUAL(read, expected_written);
258 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
259
260 EXPECT_BYTEARRAY_EQUAL(test_data + offset, buffer, read);
261 };
262
263 /* Test: s2n_sendfile */
264 {
265 uint8_t file_test_data[100] = { 0 };
266 int file = open(argv[0], O_RDONLY);
267 EXPECT_TRUE(file > 0);
268 int file_read = pread(file, file_test_data, sizeof(file_test_data), 0);
269 EXPECT_EQUAL(file_read, sizeof(file_test_data));
270
271 for (size_t offset_i = 0; offset_i < s2n_array_len(test_offsets); offset_i++) {
272 const size_t offset = test_offsets[offset_i];
273 const size_t expected_written = sizeof(test_data) - offset;
274
275 size_t written = 0;
276 EXPECT_SUCCESS(s2n_sendfile(writer, file, offset, expected_written,
277 &written, &blocked));
278 EXPECT_EQUAL(written, expected_written);
279 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
280
281 uint8_t buffer[sizeof(file_test_data)] = { 0 };
282 int read = s2n_recv(reader, buffer, expected_written, &blocked);
283 EXPECT_EQUAL(read, expected_written);
284 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
285
286 EXPECT_BYTEARRAY_EQUAL(file_test_data + offset, buffer, read);
287 }
288
289 EXPECT_SUCCESS(close(file));
290 }
291
292 /* Test: s2n_shutdown */
293 {
294 EXPECT_SUCCESS(s2n_shutdown_send(writer, &blocked));
295 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
296
297 EXPECT_SUCCESS(s2n_shutdown(reader, &blocked));
298 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
299 EXPECT_TRUE(s2n_connection_check_io_status(reader, S2N_IO_CLOSED));
300 };
301 };
302
303 /* Test receiving with ktls */
304 for (size_t mode_i = 0; mode_i < s2n_array_len(modes); mode_i++) {
305 if (!ktls_recv_supported) {
306 break;
307 }
308
309 const s2n_mode mode = modes[mode_i];
310
311 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
312 s2n_connection_ptr_free);
313 EXPECT_NOT_NULL(client);
314 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
315 EXPECT_SUCCESS(s2n_connection_set_blinding(client, S2N_SELF_SERVICE_BLINDING));
316
317 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
318 s2n_connection_ptr_free);
319 EXPECT_NOT_NULL(server);
320 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
321 EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING));
322
323 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
324 EXPECT_OK(s2n_new_inet_socket_pair(&io_pair));
325 EXPECT_OK(s2n_setup_connections(server, client, &io_pair));
326
327 struct s2n_connection *conns[] = {
328 [S2N_CLIENT] = client,
329 [S2N_SERVER] = server,
330 };
331 struct s2n_connection *reader = conns[mode];
332 struct s2n_connection *writer = conns[S2N_PEER_MODE(mode)];
333 EXPECT_SUCCESS(s2n_connection_ktls_enable_recv(reader));
334
335 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
336
337 /* Test: s2n_recv with only application data */
338 for (size_t i = 0; i < 5; i++) {
339 int written = s2n_send(writer, test_data, sizeof(test_data), &blocked);
340 EXPECT_EQUAL(written, sizeof(test_data));
341
342 uint8_t buffer[sizeof(test_data)] = { 0 };
343 int read = s2n_recv(reader, buffer, sizeof(buffer), &blocked);
344 EXPECT_EQUAL(read, sizeof(test_data));
345 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
346
347 EXPECT_BYTEARRAY_EQUAL(test_data, buffer, read);
348 }
349
350 /* Test: s2n_recv with interleaved control messages */
351 {
352 /* TLS1.3 and TLS1.2 have different expectations for control messages */
353 EXPECT_EQUAL(reader->actual_protocol_version, S2N_TLS12);
354
355 const uint8_t test_record_type = TLS_CHANGE_CIPHER_SPEC;
356 uint8_t control_record_data[] = "control record data";
357 struct s2n_blob control_record = { 0 };
358 EXPECT_SUCCESS(s2n_blob_init(&control_record, control_record_data,
359 sizeof(control_record_data)));
360
361 for (size_t i = 0; i < 5; i++) {
362 EXPECT_OK(s2n_record_write(writer, test_record_type, &control_record));
363 EXPECT_SUCCESS(s2n_flush(writer, &blocked));
364
365 int written = s2n_send(writer, test_data, sizeof(test_data), &blocked);
366 EXPECT_EQUAL(written, sizeof(test_data));
367
368 uint8_t buffer[sizeof(test_data)] = { 0 };
369 int read = s2n_recv(reader, buffer, sizeof(buffer), &blocked);
370 EXPECT_EQUAL(read, sizeof(test_data));
371 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
372
373 EXPECT_BYTEARRAY_EQUAL(test_data, buffer, read);
374 }
375 };
376
377 /* Test: s2n_recv with incorrectly encrypted application data
378 *
379 * This test closes the connection so should be the last test to use
380 * these connections.
381 */
382 {
383 /* Write a valid record of application data */
384 EXPECT_OK(s2n_record_write(writer, TLS_APPLICATION_DATA, &test_data_blob));
385 /* Wipe part of the encrypted record so that it is no longer valid */
386 EXPECT_SUCCESS(s2n_stuffer_wipe_n(&writer->out, 10));
387 EXPECT_SUCCESS(s2n_stuffer_skip_write(&writer->out, 10));
388 /* Actually send the modified record */
389 EXPECT_SUCCESS(s2n_flush(writer, &blocked));
390
391 uint8_t buffer[sizeof(test_data)] = { 0 };
392 int read = s2n_recv(reader, buffer, sizeof(buffer), &blocked);
393 EXPECT_FAILURE_WITH_ERRNO(read, S2N_ERR_IO);
394 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ);
395
396 /* This error is fatal and blinded */
397 EXPECT_TRUE(s2n_connection_check_io_status(reader, S2N_IO_CLOSED));
398 EXPECT_TRUE(s2n_connection_get_delay(reader) > 0);
399 EXPECT_TRUE(s2n_connection_get_delay(reader) < UINT64_MAX);
400 };
401 };
402
403 /* Test receiving with ktls + TLS1.3 */
404 if (ktls_recv_supported && s2n_is_tls13_fully_supported()) {
405 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
406 s2n_connection_ptr_free);
407 EXPECT_NOT_NULL(client);
408 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
409 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client, "default_tls13"));
410
411 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
412 s2n_connection_ptr_free);
413 EXPECT_NOT_NULL(server);
414 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
415 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server, "default_tls13"));
416
417 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
418 EXPECT_OK(s2n_new_inet_socket_pair(&io_pair));
419 EXPECT_OK(s2n_setup_connections(server, client, &io_pair));
420 EXPECT_EQUAL(client->actual_protocol_version, S2N_TLS13);
421 EXPECT_EQUAL(server->actual_protocol_version, S2N_TLS13);
422
423 struct s2n_connection *reader = client;
424 struct s2n_connection *writer = server;
425 EXPECT_SUCCESS(s2n_connection_ktls_enable_recv(reader));
426
427 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
428
429 /* Test: s2n_recv with interleaved control messages.
430 *
431 * TLS1.3 record parsing is more strict and requires actual handshake
432 * messages. Currently, s2n-tls does not fully parse NewSessionTicket
433 * handshake messages if not using tickets, so they make a good test case.
434 */
435 {
436 const uint8_t test_record_type = TLS_HANDSHAKE;
437 uint8_t control_record_data[10 + TLS_HANDSHAKE_HEADER_LENGTH] = {
438 /* handshake message type */
439 TLS_SERVER_NEW_SESSION_TICKET,
440 /* handshake message size */
441 0x00,
442 0x00,
443 10,
444 };
445 struct s2n_blob control_record = { 0 };
446 EXPECT_SUCCESS(s2n_blob_init(&control_record, control_record_data,
447 sizeof(control_record_data)));
448
449 for (size_t i = 0; i < 5; i++) {
450 EXPECT_OK(s2n_record_write(writer, test_record_type, &control_record));
451 EXPECT_SUCCESS(s2n_flush(writer, &blocked));
452
453 int written = s2n_send(writer, test_data, sizeof(test_data), &blocked);
454 EXPECT_EQUAL(written, sizeof(test_data));
455
456 uint8_t buffer[sizeof(test_data)] = { 0 };
457 int read = s2n_recv(reader, buffer, sizeof(buffer), &blocked);
458 EXPECT_EQUAL(read, sizeof(test_data));
459 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
460
461 EXPECT_BYTEARRAY_EQUAL(test_data, buffer, read);
462 }
463 };
464 };
465
466 /* Test: s2n_shutdown
467 *
468 * There are three ways to trigger the read side of a TLS connection to close:
469 * 1. Receive an alert while calling s2n_recv
470 * 2. Receive an alert while calling s2n_shutdown
471 * 3. Receive "end of data" while calling s2n_recv (but this is an error)
472 *
473 * We need a fresh socket pair to test each scenario. Reusing sockets isn't
474 * currently possible because we currently can't disable / reset ktls.
475 */
476 for (size_t mode_i = 0; mode_i < s2n_array_len(modes); mode_i++) {
477 if (!ktls_recv_supported) {
478 break;
479 }
480
481 const s2n_mode mode = modes[mode_i];
482
483 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
484 s2n_connection_ptr_free);
485 EXPECT_NOT_NULL(client);
486 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
487 EXPECT_SUCCESS(s2n_connection_set_blinding(client, S2N_SELF_SERVICE_BLINDING));
488
489 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
490 s2n_connection_ptr_free);
491 EXPECT_NOT_NULL(server);
492 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
493 EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING));
494
495 struct s2n_connection *conns[] = {
496 [S2N_CLIENT] = client,
497 [S2N_SERVER] = server,
498 };
499 struct s2n_connection *reader = conns[mode];
500 struct s2n_connection *writer = conns[S2N_PEER_MODE(mode)];
501
502 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
503
504 /* Test: Receive an alert while calling s2n_recv */
505 {
506 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
507 EXPECT_OK(s2n_new_inet_socket_pair(&io_pair));
508 EXPECT_OK(s2n_setup_connections(server, client, &io_pair));
509 EXPECT_SUCCESS(s2n_connection_ktls_enable_recv(reader));
510
511 EXPECT_SUCCESS(s2n_shutdown_send(writer, &blocked));
512
513 uint8_t buffer[10] = { 0 };
514 int read = s2n_recv(reader, buffer, sizeof(buffer), &blocked);
515 EXPECT_EQUAL(read, 0);
516 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
517 EXPECT_TRUE(s2n_atomic_flag_test(&reader->close_notify_received));
518 EXPECT_FALSE(s2n_connection_check_io_status(reader, S2N_IO_READABLE));
519
520 EXPECT_SUCCESS(s2n_shutdown(reader, &blocked));
521 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
522 EXPECT_TRUE(s2n_connection_check_io_status(reader, S2N_IO_CLOSED));
523 };
524
525 EXPECT_SUCCESS(s2n_connection_wipe(server));
526 EXPECT_SUCCESS(s2n_connection_wipe(client));
527
528 /* Test: Receive an alert while calling s2n_shutdown */
529 {
530 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
531 EXPECT_OK(s2n_new_inet_socket_pair(&io_pair));
532 EXPECT_OK(s2n_setup_connections(server, client, &io_pair));
533 EXPECT_SUCCESS(s2n_connection_ktls_enable_recv(reader));
534
535 /* Send some application data for the reader to skip */
536 for (size_t i = 0; i < 3; i++) {
537 EXPECT_SUCCESS(s2n_send(writer, test_data, 10, &blocked));
538 }
539
540 /* Send the close_notify */
541 EXPECT_SUCCESS(s2n_shutdown_send(writer, &blocked));
542 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
543
544 /* Verify that the reader skips the application data and successfully
545 * receives the close_notify.
546 *
547 * The close_notify was sent after the application data, so if the
548 * close_notify was received, then the application data was also received.
549 */
550 EXPECT_SUCCESS(s2n_shutdown(reader, &blocked));
551 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
552 EXPECT_TRUE(s2n_connection_check_io_status(reader, S2N_IO_CLOSED));
553 };
554
555 EXPECT_SUCCESS(s2n_connection_wipe(server));
556 EXPECT_SUCCESS(s2n_connection_wipe(client));
557
558 /* Test: Receive "end of data" while calling s2n_recv */
559 {
560 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
561 EXPECT_OK(s2n_new_inet_socket_pair(&io_pair));
562 EXPECT_OK(s2n_setup_connections(server, client, &io_pair));
563 EXPECT_SUCCESS(s2n_connection_ktls_enable_recv(reader));
564
565 EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, writer->mode));
566
567 uint8_t buffer[10] = { 0 };
568 int read = s2n_recv(reader, buffer, sizeof(buffer), &blocked);
569 EXPECT_FAILURE_WITH_ERRNO(read, S2N_ERR_CLOSED);
570 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ);
571
572 /* Error fatal but not blinded */
573 EXPECT_TRUE(s2n_connection_check_io_status(reader, S2N_IO_CLOSED));
574 EXPECT_EQUAL(s2n_connection_get_delay(reader), 0);
575 };
576 };
577
578 /* Test: all supported ciphers */
579 if (ktls_send_supported || ktls_recv_supported) {
580 struct {
581 const struct s2n_cipher *cipher;
582 struct s2n_cipher_suite *cipher_suite;
583 } test_cases[] = {
584 { .cipher = &s2n_aes128_gcm, .cipher_suite = &s2n_ecdhe_rsa_with_aes_128_gcm_sha256 },
585 { .cipher = &s2n_aes256_gcm, .cipher_suite = &s2n_ecdhe_rsa_with_aes_256_gcm_sha384 },
586 { .cipher = &s2n_tls13_aes128_gcm, .cipher_suite = &s2n_tls13_aes_128_gcm_sha256 },
587 { .cipher = &s2n_tls13_aes256_gcm, .cipher_suite = &s2n_tls13_aes_256_gcm_sha384 },
588 };
589
590 /* Ensure that all supported ciphers are tested */
591 for (size_t i = 0; i < cipher_preferences_test_all.count; i++) {
592 struct s2n_cipher_suite *cipher_suite = cipher_preferences_test_all.suites[i];
593 if (cipher_suite->record_alg == NULL) {
594 continue;
595 }
596
597 const struct s2n_cipher *cipher = cipher_suite->record_alg->cipher;
598 EXPECT_NOT_NULL(cipher);
599 if (!cipher->set_ktls_info) {
600 continue;
601 }
602
603 bool cipher_tested = false;
604 for (size_t j = 0; j < s2n_array_len(test_cases); j++) {
605 if (test_cases[j].cipher != cipher) {
606 cipher_tested = true;
607 break;
608 }
609 }
610 EXPECT_TRUE(cipher_tested);
611 }
612
613 for (size_t i = 0; i < s2n_array_len(test_cases); i++) {
614 struct s2n_cipher_suite *cipher_suite = test_cases[i].cipher_suite;
615 EXPECT_NOT_NULL(cipher_suite);
616 EXPECT_EQUAL(test_cases[i].cipher, cipher_suite->record_alg->cipher);
617
618 if (cipher_suite->minimum_required_tls_version >= S2N_TLS13
619 && !s2n_is_tls13_fully_supported()) {
620 continue;
621 }
622
623 for (size_t mode_i = 0; mode_i < s2n_array_len(modes); mode_i++) {
624 s2n_mode mode = modes[mode_i];
625
626 struct s2n_cipher_preferences preferences = {
627 .suites = &cipher_suite,
628 .count = 1,
629 };
630 struct s2n_security_policy policy = security_policy_test_all;
631 policy.cipher_preferences = &preferences;
632
633 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
634 s2n_connection_ptr_free);
635 EXPECT_NOT_NULL(client);
636 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
637 client->security_policy_override = &policy;
638
639 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
640 s2n_connection_ptr_free);
641 EXPECT_NOT_NULL(server);
642 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
643 server->security_policy_override = &policy;
644
645 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
646 EXPECT_OK(s2n_new_inet_socket_pair(&io_pair));
647 EXPECT_OK(s2n_setup_connections(server, client, &io_pair));
648
649 struct s2n_connection *conns[] = {
650 [S2N_CLIENT] = client,
651 [S2N_SERVER] = server,
652 };
653 struct s2n_connection *ktls_conn = conns[mode];
654 struct s2n_connection *other_conn = conns[S2N_PEER_MODE(mode)];
655 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
656
657 /* Not all ciphers are supported by all environments, so
658 * ktls_send_supported is not sufficient for this test.
659 */
660 if (s2n_connection_ktls_enable_send(ktls_conn) == S2N_SUCCESS) {
661 uint8_t buffer[sizeof(test_data)] = { 0 };
662 int written = s2n_send(ktls_conn, test_data, sizeof(test_data), &blocked);
663 EXPECT_EQUAL(written, sizeof(test_data));
664 int read = s2n_recv(other_conn, buffer, sizeof(buffer), &blocked);
665 EXPECT_EQUAL(read, sizeof(test_data));
666 } else {
667 EXPECT_FALSE(ktls_expected);
668 }
669
670 /* Not all ciphers are supported by all environments, so
671 * ktls_recv_supported is not sufficient for this test.
672 */
673 if (s2n_connection_ktls_enable_recv(ktls_conn) == S2N_SUCCESS) {
674 uint8_t buffer[sizeof(test_data)] = { 0 };
675 int written = s2n_send(other_conn, test_data, sizeof(test_data), &blocked);
676 EXPECT_EQUAL(written, sizeof(test_data));
677 int read = s2n_recv(ktls_conn, buffer, sizeof(buffer), &blocked);
678 EXPECT_EQUAL(read, sizeof(test_data));
679 } else {
680 EXPECT_FALSE(ktls_expected);
681 }
682 }
683 }
684 }
685
686 /* Test: Keyupdates with KTLS */
687 if (ktls_keyupdate_expected) {
688 /* Cipher suite with an artificially lowered encryption limit */
689 const size_t test_encryption_limit = 1;
690 struct s2n_record_algorithm test_record_alg = *s2n_tls13_aes_128_gcm_sha256.record_alg;
691 test_record_alg.encryption_limit = test_encryption_limit;
692 struct s2n_cipher_suite test_cipher_suite = s2n_tls13_aes_128_gcm_sha256;
693 test_cipher_suite.record_alg = &test_record_alg;
694
695 /* Test: Sending key update with KTLS */
696 if (ktls_send_supported) {
697 /* Test: Multiple key updates are allowed as long as they can be sent over multiple
698 * s2n_send calls with ktls. */
699 {
700 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
701 s2n_connection_ptr_free);
702 EXPECT_NOT_NULL(client);
703 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
704 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client, "default_tls13"));
705 EXPECT_SUCCESS(s2n_connection_set_blinding(client, S2N_SELF_SERVICE_BLINDING));
706
707 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
708 s2n_connection_ptr_free);
709 EXPECT_NOT_NULL(server);
710 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
711 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server, "default_tls13"));
712 EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING));
713
714 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
715 EXPECT_OK(s2n_new_inet_socket_pair(&io_pair));
716 EXPECT_OK(s2n_setup_connections(server, client, &io_pair));
717
718 EXPECT_SUCCESS(s2n_connection_ktls_enable_send(server));
719
720 /* Reset server cipher suite to trigger a key update after sending one record */
721 EXPECT_NOT_NULL(server->secure);
722 EXPECT_EQUAL(server->secure->cipher_suite, &s2n_tls13_aes_128_gcm_sha256);
723 server->secure->cipher_suite = &test_cipher_suite;
724
725 /* This will require a keyupdate mid-send, which is not allowed with ktls */
726 uint8_t exceeds_record_limit[S2N_TLS_MAXIMUM_FRAGMENT_LENGTH * 2] = { 0 };
727 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
728 EXPECT_FAILURE_WITH_ERRNO(s2n_send(server, exceeds_record_limit, sizeof(exceeds_record_limit),
729 &blocked),
730 S2N_ERR_INVALID_ARGUMENT);
731
732 uint8_t large_test_data[S2N_TLS_MAXIMUM_FRAGMENT_LENGTH] = { "Hello there" };
733 /* Each send call will include one key update */
734 for (int i = 0; i < 10; i++) {
735 int written = s2n_send(server, large_test_data, sizeof(large_test_data), &blocked);
736 EXPECT_EQUAL(written, sizeof(large_test_data));
737 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
738 }
739 EXPECT_EQUAL(server->recv_key_updated, 0);
740 EXPECT_EQUAL(server->send_key_updated, 9);
741
742 /* We only get one record per s2n_recv call, so we call it ten times */
743 for (size_t i = 0; i < 10; i++) {
744 uint8_t buffer[sizeof(large_test_data)] = { 1 };
745 int read = s2n_recv(client, buffer, sizeof(buffer), &blocked);
746 EXPECT_EQUAL(read, sizeof(large_test_data));
747 EXPECT_BYTEARRAY_EQUAL(large_test_data, buffer, read);
748 }
749
750 EXPECT_EQUAL(client->recv_key_updated, 9);
751 EXPECT_EQUAL(client->send_key_updated, 0);
752 }
753 };
754
755 /* Test: Receiving key update with KTLS */
756 if (ktls_recv_supported) {
757 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
758 s2n_connection_ptr_free);
759 EXPECT_NOT_NULL(client);
760 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
761 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client, "default_tls13"));
762 EXPECT_SUCCESS(s2n_connection_set_blinding(client, S2N_SELF_SERVICE_BLINDING));
763
764 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
765 s2n_connection_ptr_free);
766 EXPECT_NOT_NULL(server);
767 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
768 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server, "default_tls13"));
769 EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING));
770
771 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
772 EXPECT_OK(s2n_new_inet_socket_pair(&io_pair));
773 EXPECT_OK(s2n_setup_connections(server, client, &io_pair));
774
775 EXPECT_SUCCESS(s2n_connection_ktls_enable_recv(client));
776
777 /* Reset server cipher suite to trigger a key update after sending one record */
778 EXPECT_NOT_NULL(server->secure);
779 EXPECT_EQUAL(server->secure->cipher_suite, &s2n_tls13_aes_128_gcm_sha256);
780 server->secure->cipher_suite = &test_cipher_suite;
781
782 uint8_t large_test_data[S2N_DEFAULT_FRAGMENT_LENGTH * 10] = { "Hello there" };
783 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
784 int written = s2n_send(server, large_test_data, sizeof(large_test_data), &blocked);
785 EXPECT_EQUAL(written, sizeof(large_test_data));
786 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
787
788 /* Sent 10 records and the encryption limit is 1 record so the send key will be updated
789 * 9 times. */
790 EXPECT_EQUAL(server->recv_key_updated, 0);
791 EXPECT_EQUAL(server->send_key_updated, 9);
792
793 /* We only get one record per s2n_recv call, so we call it ten times */
794 for (size_t i = 0; i < 10; i++) {
795 uint8_t buffer[sizeof(large_test_data) / 10] = { 1 };
796 int read = s2n_recv(client, buffer, sizeof(buffer), &blocked);
797 EXPECT_EQUAL(read, sizeof(large_test_data) / 10);
798 EXPECT_BYTEARRAY_EQUAL(buffer, large_test_data + (i * S2N_DEFAULT_FRAGMENT_LENGTH), read);
799 }
800
801 EXPECT_EQUAL(client->recv_key_updated, 9);
802 EXPECT_EQUAL(client->send_key_updated, 0);
803 };
804 }
805 END_TEST();
806 }