Debian Codegraph @

crt/s2n/tests/unit/s2n_shutdown_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 "tls/s2n_shutdown.c"
17
18 #include "s2n_test.h"
19 #ifndef _WIN32
20 #include "testlib/s2n_ktls_test_utils.h"
21 #endif
22 #include "testlib/s2n_testlib.h"
23 #include "tls/s2n_alerts.h"
24
25 #define ALERT_LEN (sizeof(uint16_t))
26
27 int main(int argc, char **argv)
28 {
29 BEGIN_TEST();
30
31 const uint8_t close_notify_alert[] = {
32 S2N_TLS_ALERT_LEVEL_WARNING,
33 S2N_TLS_ALERT_CLOSE_NOTIFY
34 };
35
36 const uint8_t alert_record_header[] = {
37 /* record type */
38 TLS_ALERT,
39 /* protocol version */
40 S2N_TLS12 / 10,
41 S2N_TLS12 % 10,
42 /* length */
43 0,
44 S2N_ALERT_LENGTH,
45 };
46
47 const uint8_t alert_record_size = sizeof(alert_record_header) + S2N_ALERT_LENGTH;
48
49 DEFER_CLEANUP(struct s2n_cert_chain_and_key * chain_and_key,
50 s2n_cert_chain_and_key_ptr_free);
51 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
52 S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY));
53
54 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(),
55 s2n_config_ptr_free);
56 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
57 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13"));
58 EXPECT_SUCCESS(s2n_config_disable_x509_verification(config));
59
60 /* Test: Do not send or await close_notify if reader alert already queued */
61 {
62 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
63 s2n_connection_ptr_free);
64 EXPECT_NOT_NULL(conn);
65 EXPECT_OK(s2n_skip_handshake(conn));
66
67 /* Setup output, but no input. We expect no reads. */
68 DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free);
69 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0));
70 EXPECT_SUCCESS(s2n_connection_set_send_io_stuffer(&output, conn));
71
72 /* Verify state prior to alert */
73 EXPECT_TRUE(s2n_handshake_is_complete(conn));
74 EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
75 EXPECT_FALSE(conn->alert_sent);
76 EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX));
77
78 /* Queue reader alert */
79 EXPECT_SUCCESS(s2n_queue_reader_handshake_failure_alert(conn));
80
81 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
82 EXPECT_SUCCESS(s2n_shutdown(conn, &blocked));
83
84 /* Verify state after shutdown attempt */
85 EXPECT_TRUE(s2n_handshake_is_complete(conn));
86 EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
87 EXPECT_TRUE(conn->alert_sent);
88 EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED));
89
90 /* Verify only one alert sent */
91 EXPECT_EQUAL(s2n_stuffer_data_available(&output), alert_record_size);
92
93 /* Verify that the single alert is a fatal error, not a close_notify */
94 uint8_t level = 0, code = 0;
95 EXPECT_SUCCESS(s2n_stuffer_skip_read(&output, sizeof(alert_record_header)));
96 EXPECT_SUCCESS(s2n_stuffer_read_uint8(&output, &level));
97 EXPECT_EQUAL(level, S2N_TLS_ALERT_LEVEL_FATAL);
98 EXPECT_SUCCESS(s2n_stuffer_read_uint8(&output, &code));
99 EXPECT_EQUAL(code, S2N_TLS_ALERT_HANDSHAKE_FAILURE);
100 };
101
102 /* Test: Send and await close_notify if a warning alert was sent */
103 {
104 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
105 s2n_connection_ptr_free);
106 EXPECT_NOT_NULL(conn);
107 EXPECT_OK(s2n_skip_handshake(conn));
108
109 DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free);
110 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));
111 DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free);
112 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0));
113 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, conn));
114
115 /* Verify state prior to alert */
116 EXPECT_TRUE(s2n_handshake_is_complete(conn));
117 EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
118 EXPECT_FALSE(conn->alert_sent);
119 EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX));
120
121 /* Queue reader warning */
122 EXPECT_OK(s2n_queue_reader_no_renegotiation_alert(conn));
123
124 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
125 EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown(conn, &blocked),
126 S2N_ERR_IO_BLOCKED);
127 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ);
128
129 /* Verify state after shutdown attempt */
130 EXPECT_TRUE(s2n_handshake_is_complete(conn));
131 EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
132 EXPECT_TRUE(conn->alert_sent);
133 EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE));
134
135 /* Verify two alerts sent: the warning + the close_notify */
136 EXPECT_EQUAL(s2n_stuffer_data_available(&output), alert_record_size * 2);
137 };
138
139 /* Test: Do not send or await close_notify if error alert was already received */
140 {
141 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
142 s2n_connection_ptr_free);
143 EXPECT_NOT_NULL(conn);
144 EXPECT_OK(s2n_skip_handshake(conn));
145
146 DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free);
147 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));
148 DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free);
149 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0));
150 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, conn));
151
152 /* Verify state prior to alert */
153 EXPECT_TRUE(s2n_handshake_is_complete(conn));
154 EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
155 EXPECT_FALSE(conn->alert_sent);
156 EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX));
157
158 /* Queue input alert */
159 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&input, alert_record_header,
160 sizeof(alert_record_header)));
161 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&input, S2N_TLS_ALERT_LEVEL_FATAL));
162 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&input, S2N_TLS_ALERT_INTERNAL_ERROR));
163
164 /* Receive alert */
165 uint8_t buffer[1] = { 0 };
166 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
167 EXPECT_FAILURE_WITH_ERRNO(s2n_recv(conn, buffer, sizeof(buffer), &blocked),
168 S2N_ERR_ALERT);
169
170 /* Call s2n_connection_get_alert(), to make sure that
171 * https://github.com/aws/s2n-tls/issues/3933 doesn't affect shutdown.
172 */
173 EXPECT_EQUAL(s2n_connection_get_alert(conn), S2N_TLS_ALERT_INTERNAL_ERROR);
174
175 /* Shutdown should succeed, since it's a no-op */
176 EXPECT_SUCCESS(s2n_shutdown(conn, &blocked));
177
178 /* Verify state after shutdown attempt */
179 EXPECT_TRUE(s2n_handshake_is_complete(conn));
180 EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
181 EXPECT_FALSE(conn->alert_sent);
182 EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED));
183
184 /* Verify no alerts sent */
185 EXPECT_EQUAL(s2n_stuffer_data_available(&output), 0);
186 };
187
188 /* Test: Do not wait for response close_notify if handshake not complete */
189 {
190 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
191 s2n_connection_ptr_free);
192 EXPECT_NOT_NULL(conn);
193
194 DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free);
195 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));
196 DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free);
197 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0));
198 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, conn));
199
200 /* Verify state prior to alert */
201 EXPECT_FALSE(s2n_handshake_is_complete(conn));
202 EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
203 EXPECT_FALSE(conn->alert_sent);
204 EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX));
205
206 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
207 EXPECT_SUCCESS(s2n_shutdown(conn, &blocked));
208
209 /* Verify state after shutdown */
210 EXPECT_FALSE(s2n_handshake_is_complete(conn));
211 EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
212 EXPECT_TRUE(conn->alert_sent);
213
214 /* Fully closed: we don't worry about truncating data */
215 EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED));
216 };
217
218 /* Test: Await close_notify if no close_notify received yet */
219 {
220 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
221 s2n_connection_ptr_free);
222 EXPECT_NOT_NULL(conn);
223 EXPECT_OK(s2n_skip_handshake(conn));
224
225 DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free);
226 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));
227 DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free);
228 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0));
229 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, conn));
230
231 /* Verify state prior to alert */
232 EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
233 EXPECT_FALSE(conn->alert_sent);
234 EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX));
235
236 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
237 EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown(conn, &blocked), S2N_ERR_IO_BLOCKED);
238 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ);
239
240 /* Verify state after shutdown attempt */
241 EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
242 EXPECT_TRUE(conn->alert_sent);
243
244 /* Half-close: only write closed */
245 EXPECT_EQUAL(s2n_connection_get_protocol_version(conn), S2N_TLS13);
246 EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE));
247 EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_READABLE));
248 };
249
250 /* Test: Do not await close_notify if close_notify already received */
251 {
252 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
253 s2n_connection_ptr_free);
254 EXPECT_NOT_NULL(conn);
255 EXPECT_OK(s2n_skip_handshake(conn));
256
257 DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free);
258 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));
259 DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free);
260 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0));
261 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, conn));
262
263 /* Verify state prior to alert */
264 EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
265 EXPECT_FALSE(conn->alert_sent);
266 EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX));
267
268 /* Write and process the alert */
269 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&conn->in, close_notify_alert, sizeof(close_notify_alert)));
270 EXPECT_SUCCESS(s2n_process_alert_fragment(conn));
271
272 /* Verify state after alert */
273 EXPECT_TRUE(s2n_atomic_flag_test(&conn->close_notify_received));
274 EXPECT_FALSE(conn->alert_sent);
275 EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE));
276 EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_READABLE));
277
278 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
279 EXPECT_SUCCESS(s2n_shutdown(conn, &blocked));
280 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
281
282 /* Verify state after shutdown attempt */
283 EXPECT_TRUE(s2n_atomic_flag_test(&conn->close_notify_received));
284 EXPECT_TRUE(conn->alert_sent);
285 EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED));
286 };
287
288 /* Test: s2n_shutdown reports alerts received after a close_notify is sent */
289 {
290 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
291 s2n_connection_ptr_free);
292 EXPECT_NOT_NULL(conn);
293 EXPECT_OK(s2n_skip_handshake(conn));
294
295 DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free);
296 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));
297 DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free);
298 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0));
299 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, conn));
300
301 /* Verify s2n_shutdown is waiting for a close_notify */
302 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
303 EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown(conn, &blocked), S2N_ERR_IO_BLOCKED);
304 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ);
305 EXPECT_TRUE(conn->alert_sent);
306
307 /* Queue an input error alert */
308 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&input, alert_record_header, sizeof(alert_record_header)));
309 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&input, 2));
310 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&input, S2N_TLS_ALERT_INTERNAL_ERROR));
311
312 /* Receive and report the error alert */
313 EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown(conn, &blocked), S2N_ERR_ALERT);
314
315 /* Verify state after shutdown attempt */
316 EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
317 EXPECT_TRUE(conn->alert_sent);
318 EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED));
319 EXPECT_EQUAL(s2n_stuffer_data_available(&output), alert_record_size);
320
321 /* Future calls are no-ops */
322 for (size_t i = 0; i < 5; i++) {
323 EXPECT_SUCCESS(s2n_shutdown(conn, &blocked));
324 EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
325 EXPECT_TRUE(conn->alert_sent);
326 }
327 };
328
329 /* Test: s2n_shutdown ignores data received after a close_notify is sent */
330 {
331 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
332 s2n_connection_ptr_free);
333 EXPECT_NOT_NULL(conn);
334 EXPECT_OK(s2n_skip_handshake(conn));
335
336 DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free);
337 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));
338 DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free);
339 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0));
340 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, conn));
341
342 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
343 EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown(conn, &blocked), S2N_ERR_IO_BLOCKED);
344 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ);
345 EXPECT_TRUE(conn->alert_sent);
346
347 /* Receive a non-alert record */
348 uint8_t record_bytes[] = {
349 /* record type */
350 TLS_HANDSHAKE,
351 /* protocol version */
352 S2N_TLS12 / 10,
353 S2N_TLS12 % 10,
354 /* length */
355 0,
356 1,
357 /* data */
358 'x'
359 };
360 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&input, record_bytes, sizeof(record_bytes)));
361 EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown(conn, &blocked), S2N_ERR_IO_BLOCKED);
362 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ);
363
364 /* Receive the response close_notify */
365 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&input, alert_record_header, sizeof(alert_record_header)));
366 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&input, close_notify_alert, sizeof(close_notify_alert)));
367 EXPECT_SUCCESS(s2n_shutdown(conn, &blocked));
368 };
369
370 /* Test: Do not await close_notify after reading malformed record */
371 {
372 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
373 s2n_connection_ptr_free);
374 EXPECT_NOT_NULL(conn);
375 EXPECT_OK(s2n_skip_handshake(conn));
376 EXPECT_SUCCESS(s2n_connection_set_blinding(conn, S2N_SELF_SERVICE_BLINDING));
377
378 /* Set the version so that a record header with the wrong version will
379 * be rejected as invalid.
380 */
381 conn->actual_protocol_version_established = true;
382 conn->actual_protocol_version = S2N_TLS13;
383
384 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
385 DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free);
386 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));
387 DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free);
388 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0));
389 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, conn));
390
391 /* Receive a malformed record.
392 * We want reading this record to leave our IO in a bad state.
393 */
394 uint8_t header_bytes[] = {
395 /* record type */
396 TLS_HANDSHAKE,
397 /* bad protocol version */
398 0,
399 0,
400 /* zero length */
401 0,
402 0,
403 };
404 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&input, header_bytes, sizeof(header_bytes)));
405 uint8_t recv_buffer[1] = { 0 };
406 EXPECT_FAILURE_WITH_ERRNO(
407 s2n_recv(conn, recv_buffer, sizeof(recv_buffer), &blocked),
408 S2N_ERR_BAD_MESSAGE);
409
410 /* Clear the blinding delay so that we can call s2n_shutdown */
411 EXPECT_TRUE(conn->delay > 0);
412 conn->write_timer.time = 0;
413
414 /* Successfully shutdown without waiting for more data. */
415 EXPECT_SUCCESS(s2n_shutdown(conn, &blocked));
416 EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
417 };
418
419 /* Test: s2n_shutdown successfully reads and skips a partial app data record */
420 {
421 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
422 s2n_connection_ptr_free);
423 EXPECT_NOT_NULL(client);
424 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
425
426 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
427 s2n_connection_ptr_free);
428 EXPECT_NOT_NULL(server);
429 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
430
431 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 },
432 s2n_io_stuffer_pair_free);
433 EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
434 EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair));
435 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));
436
437 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
438
439 /* Send some application data */
440 uint8_t test_data[] = "hello world";
441 EXPECT_EQUAL(s2n_send(client, test_data, sizeof(test_data), &blocked), sizeof(test_data));
442
443 /* Block reading application data */
444 io_pair.server_in.write_cursor--;
445 EXPECT_FAILURE_WITH_ERRNO(s2n_recv(server, test_data, sizeof(test_data), &blocked),
446 S2N_ERR_IO_BLOCKED);
447
448 /* Make the remainder of the application data + a close_notify available */
449 io_pair.server_in.write_cursor++;
450 EXPECT_SUCCESS(s2n_shutdown_send(client, &blocked));
451
452 /* Successfully shutdown.
453 * The application data is skipped.
454 */
455 EXPECT_SUCCESS(s2n_shutdown(server, &blocked));
456 EXPECT_TRUE(s2n_atomic_flag_test(&server->close_notify_received));
457 };
458
459 /* Test: s2n_shutdown successfully reads a partial close_notify record */
460 {
461 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
462 s2n_connection_ptr_free);
463 EXPECT_NOT_NULL(client);
464 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
465
466 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
467 s2n_connection_ptr_free);
468 EXPECT_NOT_NULL(server);
469 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
470
471 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 },
472 s2n_io_stuffer_pair_free);
473 EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
474 EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair));
475 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));
476
477 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
478
479 /* Send the close_notify */
480 EXPECT_SUCCESS(s2n_shutdown_send(client, &blocked));
481
482 /* Block reading the close_notify record */
483 uint8_t read = 0;
484 io_pair.server_in.write_cursor--;
485 EXPECT_FAILURE_WITH_ERRNO(s2n_recv(server, &read, 1, &blocked), S2N_ERR_IO_BLOCKED);
486 EXPECT_FALSE(s2n_atomic_flag_test(&server->close_notify_received));
487
488 /* Receive the rest of the close_notify record and successful shutdown */
489 io_pair.server_in.write_cursor++;
490 EXPECT_SUCCESS(s2n_shutdown(server, &blocked));
491 EXPECT_TRUE(s2n_atomic_flag_test(&server->close_notify_received));
492 };
493
494 /* Test: s2n_shutdown fails if a partial record is malformed */
495 {
496 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
497 s2n_connection_ptr_free);
498 EXPECT_NOT_NULL(client);
499 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
500
501 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
502 s2n_connection_ptr_free);
503 EXPECT_NOT_NULL(server);
504 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
505 EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING));
506
507 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 },
508 s2n_io_stuffer_pair_free);
509 EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
510 EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair));
511 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));
512
513 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
514
515 /* Send some invalid application data */
516 uint8_t test_data[] = "hello world";
517 EXPECT_EQUAL(s2n_send(client, test_data, sizeof(test_data), &blocked), sizeof(test_data));
518 const uint8_t overwrite_size = sizeof(test_data) / 2;
519 EXPECT_SUCCESS(s2n_stuffer_wipe_n(&io_pair.server_in, overwrite_size));
520 EXPECT_SUCCESS(s2n_stuffer_skip_write(&io_pair.server_in, overwrite_size));
521
522 /* Block reading application data */
523 io_pair.server_in.write_cursor--;
524 EXPECT_FAILURE_WITH_ERRNO(s2n_recv(server, test_data, sizeof(test_data), &blocked),
525 S2N_ERR_IO_BLOCKED);
526
527 /* Make the remainder of the application data + a close_notify available */
528 io_pair.server_in.write_cursor++;
529 EXPECT_SUCCESS(s2n_shutdown_send(client, &blocked));
530
531 /* Shutdown fails to decrypt bad application data record */
532 EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown(server, &blocked), S2N_ERR_DECRYPT);
533 EXPECT_FALSE(s2n_atomic_flag_test(&server->close_notify_received));
534 };
535
536 /* Test: s2n_shutdown skips partially drained / consumed app data
537 *
538 * This is different from handling a partial record. The record is complete
539 * and decrypted, but the application has not consumed the plaintext data yet.
540 */
541 {
542 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
543 s2n_connection_ptr_free);
544 EXPECT_NOT_NULL(client);
545 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
546
547 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
548 s2n_connection_ptr_free);
549 EXPECT_NOT_NULL(server);
550 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
551
552 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 },
553 s2n_io_stuffer_pair_free);
554 EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
555 EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair));
556 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));
557
558 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
559
560 /* Send some application data */
561 uint8_t test_data[] = "hello world";
562 EXPECT_EQUAL(s2n_send(client, test_data, sizeof(test_data), &blocked), sizeof(test_data));
563
564 /* Only read some of the application data */
565 EXPECT_EQUAL(s2n_recv(server, test_data, 1, &blocked), 1);
566
567 /* Make a close_notify available */
568 EXPECT_SUCCESS(s2n_shutdown_send(client, &blocked));
569
570 /* Successfully shutdown.
571 * The remaining application data is skipped.
572 */
573 EXPECT_SUCCESS(s2n_shutdown(server, &blocked));
574 EXPECT_TRUE(s2n_atomic_flag_test(&server->close_notify_received));
575 };
576
577 /* Test: s2n_shutdown with aggressive socket close */
578 {
579 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
580 s2n_connection_ptr_free);
581 EXPECT_NOT_NULL(server_conn);
582 EXPECT_OK(s2n_skip_handshake(server_conn));
583
584 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
585 s2n_connection_ptr_free);
586 EXPECT_NOT_NULL(client_conn);
587 EXPECT_OK(s2n_skip_handshake(client_conn));
588
589 struct s2n_test_io_pair io_pair = { 0 };
590 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
591 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
592
593 /* The client's first shutdown attempt blocks on the server's close_notify */
594 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
595 EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown(client_conn, &blocked), S2N_ERR_IO_BLOCKED);
596 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ);
597
598 /* The server's next shutdown succeeds.
599 * From the server's perspective the connection is now gracefully shutdown and
600 * the socket can be closed.
601 */
602 EXPECT_SUCCESS(s2n_shutdown(server_conn, &blocked));
603 EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_SERVER));
604
605 /* Even though the socket is now closed, we should be able to finish
606 * shutting down the client connection too.
607 */
608 EXPECT_SUCCESS(s2n_shutdown(client_conn, &blocked));
609 EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_CLIENT));
610 };
611
612 /* Test: Do not send or await close_notify if supporting QUIC */
613 if (s2n_is_tls13_fully_supported()) {
614 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
615 s2n_connection_ptr_free);
616 EXPECT_NOT_NULL(conn);
617 EXPECT_SUCCESS(s2n_connection_enable_quic(conn));
618
619 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
620 EXPECT_SUCCESS(s2n_shutdown(conn, &blocked));
621 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
622
623 /* Verify state after shutdown attempt */
624 EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
625 EXPECT_FALSE(conn->alert_sent);
626 EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED));
627 };
628
629 /* Test: s2n_shutdown_send */
630 {
631 /* Test: Safety */
632 {
633 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
634 s2n_connection_ptr_free);
635 EXPECT_NOT_NULL(conn);
636 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
637
638 EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown_send(NULL, &blocked), S2N_ERR_NULL);
639 EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown_send(conn, NULL), S2N_ERR_NULL);
640 }
641
642 /* Test: Basic successful call */
643 {
644 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
645 s2n_connection_ptr_free);
646 EXPECT_NOT_NULL(conn);
647
648 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
649
650 /* Only setup write IO.
651 * By not setting up read IO, we test that s2n_shutdown_send never
652 * attempts to read.
653 */
654 DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free);
655 EXPECT_SUCCESS(s2n_connection_set_send_io_stuffer(&output, conn));
656 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0));
657
658 /* s2n_shutdown also doesn't attempt to read unless we skip the
659 * handshake. s2n_shutdown_send doesn't care about the state of the
660 * handshake, but skip anyway to prove that.
661 */
662 EXPECT_OK(s2n_skip_handshake(conn));
663
664 /* Successful half-close */
665 EXPECT_SUCCESS(s2n_shutdown_send(conn, &blocked));
666 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
667 EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE));
668 EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_READABLE));
669 EXPECT_EQUAL(s2n_stuffer_data_available(&output), alert_record_size);
670 EXPECT_TRUE(conn->alert_sent);
671 };
672
673 /* Test: Handles blocking IO */
674 {
675 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
676 s2n_connection_ptr_free);
677 EXPECT_NOT_NULL(conn);
678
679 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
680
681 /* Do not initially allocate any memory for the output stuffer.
682 * That will cause writes to block.
683 */
684 DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free);
685 EXPECT_SUCCESS(s2n_connection_set_send_io_stuffer(&output, conn));
686
687 /* All attempts to shutdown should block */
688 for (size_t i = 0; i < 5; i++) {
689 EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown_send(conn, &blocked),
690 S2N_ERR_IO_BLOCKED);
691 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE);
692 EXPECT_EQUAL(s2n_stuffer_data_available(&output), 0);
693 }
694
695 /* Once we allocate memory for the output stuffer (by marking it
696 * growable here), writes should start succeeding.
697 */
698 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0));
699
700 /* Successful half-close */
701 EXPECT_SUCCESS(s2n_shutdown_send(conn, &blocked));
702 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
703 EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE));
704 EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_READABLE));
705 EXPECT_EQUAL(s2n_stuffer_data_available(&output), alert_record_size);
706 EXPECT_TRUE(conn->alert_sent);
707 };
708
709 /* Test: No-op on wipe */
710 {
711 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
712 s2n_connection_ptr_free);
713 EXPECT_NOT_NULL(conn);
714 EXPECT_SUCCESS(s2n_connection_wipe(conn));
715
716 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
717 EXPECT_SUCCESS(s2n_shutdown_send(conn, &blocked));
718 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
719 EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX));
720 EXPECT_FALSE(conn->alert_sent);
721 };
722
723 /* Test: Full close after half close */
724 {
725 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
726 s2n_connection_ptr_free);
727 EXPECT_NOT_NULL(conn);
728 EXPECT_OK(s2n_skip_handshake(conn));
729
730 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
731
732 DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free);
733 DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free);
734 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0));
735 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));
736 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, conn));
737
738 /* Successful half-close.
739 * Subsequent calls are no-ops.
740 */
741 for (size_t i = 0; i < 5; i++) {
742 EXPECT_SUCCESS(s2n_shutdown_send(conn, &blocked));
743 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
744 EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE));
745 EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_READABLE));
746 EXPECT_EQUAL(s2n_stuffer_data_available(&output), alert_record_size);
747 EXPECT_TRUE(conn->alert_sent);
748 }
749
750 /* Full close blocks on input */
751 for (size_t i = 0; i < 5; i++) {
752 EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown(conn, &blocked), S2N_ERR_IO_BLOCKED);
753 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ);
754 EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE));
755 EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_READABLE));
756 }
757
758 /* Copy alert from output to input */
759 EXPECT_SUCCESS(s2n_stuffer_copy(&output, &input, s2n_stuffer_data_available(&output)));
760
761 /* Full close succeeds.
762 * Subsequent calls are no-ops.
763 */
764 for (size_t i = 0; i < 5; i++) {
765 EXPECT_SUCCESS(s2n_shutdown(conn, &blocked));
766 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
767 EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED));
768 EXPECT_EQUAL(s2n_stuffer_data_available(&input), 0);
769 EXPECT_EQUAL(s2n_stuffer_data_available(&output), 0);
770 EXPECT_TRUE(conn->alert_sent);
771 }
772 };
773
774 /* Test: Half close, local alert, then full close */
775 {
776 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
777 s2n_connection_ptr_free);
778 EXPECT_NOT_NULL(conn);
779 EXPECT_OK(s2n_skip_handshake(conn));
780
781 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
782
783 DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free);
784 DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free);
785 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0));
786 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));
787 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, conn));
788
789 /* Successful half-close */
790 EXPECT_SUCCESS(s2n_shutdown_send(conn, &blocked));
791 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
792 EXPECT_TRUE(conn->alert_sent);
793
794 /* Queue a local fatal alert */
795 EXPECT_SUCCESS(s2n_queue_reader_handshake_failure_alert(conn));
796
797 /* Full close is no-op */
798 EXPECT_SUCCESS(s2n_shutdown(conn, &blocked));
799 EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED));
800 EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
801 };
802
803 /* Test: Half close, peer alert, then full close */
804 {
805 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
806 s2n_connection_ptr_free);
807 EXPECT_NOT_NULL(conn);
808 EXPECT_OK(s2n_skip_handshake(conn));
809
810 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
811
812 DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free);
813 DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free);
814 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0));
815 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));
816 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, conn));
817
818 /* Successful half-close */
819 EXPECT_SUCCESS(s2n_shutdown_send(conn, &blocked));
820 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
821 EXPECT_TRUE(conn->alert_sent);
822
823 /* Receive alert */
824 uint8_t buffer[1] = { 0 };
825 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&input, alert_record_header,
826 sizeof(alert_record_header)));
827 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&input, S2N_TLS_ALERT_LEVEL_FATAL));
828 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&input, S2N_TLS_ALERT_INTERNAL_ERROR));
829 EXPECT_FAILURE_WITH_ERRNO(s2n_recv(conn, buffer, sizeof(buffer), &blocked),
830 S2N_ERR_ALERT);
831
832 /* Full close is no-op */
833 EXPECT_SUCCESS(s2n_shutdown(conn, &blocked));
834 EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED));
835 EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
836 };
837 };
838
839 #ifndef _WIN32
840 /* Test: s2n_shutdown_send: kTLS enabled */
841 {
842 /* Test: Successfully send alert */
843 {
844 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
845 s2n_connection_ptr_free);
846 EXPECT_NOT_NULL(conn);
847 s2n_ktls_configure_connection(conn, S2N_KTLS_MODE_SEND);
848
849 DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer out = { 0 },
850 s2n_ktls_io_stuffer_free);
851 EXPECT_OK(s2n_test_init_ktls_io_stuffer_send(conn, &out));
852
853 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
854 EXPECT_SUCCESS(s2n_shutdown_send(conn, &blocked));
855 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
856 EXPECT_TRUE(conn->alert_sent);
857 EXPECT_EQUAL(out.sendmsg_invoked_count, 1);
858 EXPECT_OK(s2n_test_validate_ancillary(&out, TLS_ALERT, S2N_ALERT_LENGTH));
859 EXPECT_OK(s2n_test_validate_data(&out,
860 close_notify_alert, sizeof(close_notify_alert)));
861
862 /* Repeating the shutdown does not resend the alert */
863 for (size_t i = 0; i < 5; i++) {
864 EXPECT_SUCCESS(s2n_shutdown_send(conn, &blocked));
865 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
866 EXPECT_TRUE(conn->alert_sent);
867 EXPECT_EQUAL(out.sendmsg_invoked_count, 1);
868 }
869 };
870
871 /* Test: Successfully send alert after blocking */
872 {
873 /* One call does the partial write, the second blocks */
874 const size_t partial_write = 1;
875 const size_t second_write = sizeof(close_notify_alert) - partial_write;
876 EXPECT_TRUE(second_write > 0);
877
878 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
879 s2n_connection_ptr_free);
880 EXPECT_NOT_NULL(conn);
881 s2n_ktls_configure_connection(conn, S2N_KTLS_MODE_SEND);
882
883 DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer out = { 0 },
884 s2n_ktls_io_stuffer_free);
885 EXPECT_OK(s2n_test_init_ktls_io_stuffer_send(conn, &out));
886 EXPECT_SUCCESS(s2n_stuffer_free(&out.data_buffer));
887 EXPECT_SUCCESS(s2n_stuffer_alloc(&out.data_buffer, partial_write));
888
889 /* One call does the partial write, the second blocks */
890 size_t expected_calls = 2;
891
892 /* Initial shutdown blocks */
893 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
894 EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown_send(conn, &blocked),
895 S2N_ERR_IO_BLOCKED);
896 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE);
897 EXPECT_TRUE(conn->alert_sent);
898 EXPECT_EQUAL(out.sendmsg_invoked_count, expected_calls);
899 EXPECT_OK(s2n_test_validate_ancillary(&out, TLS_ALERT, partial_write));
900 EXPECT_OK(s2n_test_validate_data(&out, close_notify_alert, partial_write));
901
902 /* Unblock the output stuffer */
903 out.data_buffer.growable = true;
904 expected_calls++;
905 EXPECT_SUCCESS(s2n_stuffer_wipe(&out.ancillary_buffer));
906
907 /* Second shutdown succeeds */
908 EXPECT_SUCCESS(s2n_shutdown_send(conn, &blocked));
909 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
910 EXPECT_TRUE(conn->alert_sent);
911 EXPECT_EQUAL(out.sendmsg_invoked_count, expected_calls);
912 EXPECT_OK(s2n_test_validate_ancillary(&out, TLS_ALERT, second_write));
913 EXPECT_OK(s2n_test_validate_data(&out, close_notify_alert,
914 sizeof(close_notify_alert)));
915
916 /* Repeating the shutdown does not resend the alert */
917 for (size_t i = 0; i < 5; i++) {
918 EXPECT_SUCCESS(s2n_shutdown_send(conn, &blocked));
919 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
920 EXPECT_TRUE(conn->alert_sent);
921 EXPECT_EQUAL(out.sendmsg_invoked_count, expected_calls);
922 }
923 };
924 };
925
926 /* Test: ktls enabled */
927 {
928 /* Test: Successfully shutdown */
929 {
930 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
931 s2n_connection_ptr_free);
932 EXPECT_NOT_NULL(client);
933 s2n_ktls_configure_connection(client, S2N_KTLS_MODE_SEND);
934 s2n_ktls_configure_connection(client, S2N_KTLS_MODE_RECV);
935 EXPECT_OK(s2n_skip_handshake(client));
936
937 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
938 s2n_connection_ptr_free);
939 EXPECT_NOT_NULL(server);
940 s2n_ktls_configure_connection(server, S2N_KTLS_MODE_SEND);
941 s2n_ktls_configure_connection(server, S2N_KTLS_MODE_RECV);
942 EXPECT_OK(s2n_skip_handshake(server));
943
944 DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer_pair io_pair = { 0 },
945 s2n_ktls_io_stuffer_pair_free);
946 EXPECT_OK(s2n_test_init_ktls_io_stuffer(server, client, &io_pair));
947
948 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
949 EXPECT_SUCCESS(s2n_shutdown_send(client, &blocked));
950 EXPECT_TRUE(client->alert_sent);
951
952 EXPECT_SUCCESS(s2n_shutdown(server, &blocked));
953 EXPECT_TRUE(server->alert_sent);
954 EXPECT_TRUE(s2n_connection_check_io_status(server, S2N_IO_CLOSED));
955 };
956
957 /* Test: Successfully shutdown after blocking */
958 {
959 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
960 s2n_connection_ptr_free);
961 EXPECT_NOT_NULL(client);
962 s2n_ktls_configure_connection(client, S2N_KTLS_MODE_SEND);
963 s2n_ktls_configure_connection(client, S2N_KTLS_MODE_RECV);
964 EXPECT_OK(s2n_skip_handshake(client));
965
966 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
967 s2n_connection_ptr_free);
968 EXPECT_NOT_NULL(server);
969 s2n_ktls_configure_connection(server, S2N_KTLS_MODE_SEND);
970 s2n_ktls_configure_connection(server, S2N_KTLS_MODE_RECV);
971 EXPECT_OK(s2n_skip_handshake(server));
972
973 DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer_pair io_pair = { 0 },
974 s2n_ktls_io_stuffer_pair_free);
975 EXPECT_OK(s2n_test_init_ktls_io_stuffer(server, client, &io_pair));
976
977 /* Setup the client->server stuffer to not fit the entire close_notify */
978 EXPECT_SUCCESS(s2n_stuffer_free(&io_pair.server_in.data_buffer));
979 EXPECT_SUCCESS(s2n_stuffer_alloc(&io_pair.server_in.data_buffer, 1));
980
981 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
982 EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown(client, &blocked), S2N_ERR_IO_BLOCKED);
983 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE);
984 EXPECT_FALSE(s2n_connection_check_io_status(client, S2N_IO_WRITABLE));
985 EXPECT_TRUE(s2n_connection_check_io_status(client, S2N_IO_READABLE));
986
987 EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown(server, &blocked), S2N_ERR_IO_BLOCKED);
988 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ);
989 EXPECT_FALSE(s2n_connection_check_io_status(server, S2N_IO_WRITABLE));
990 EXPECT_TRUE(s2n_connection_check_io_status(server, S2N_IO_READABLE));
991
992 /* Reuse the client->server stuffer for the remaining close_notify */
993 EXPECT_SUCCESS(s2n_stuffer_wipe(&io_pair.server_in.data_buffer));
994
995 EXPECT_SUCCESS(s2n_shutdown(client, &blocked));
996 EXPECT_SUCCESS(s2n_shutdown(server, &blocked));
997 };
998
999 /* Test: Skip application data when waiting for close_notify */
1000 {
1001 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
1002 s2n_connection_ptr_free);
1003 EXPECT_NOT_NULL(client);
1004 s2n_ktls_configure_connection(client, S2N_KTLS_MODE_SEND);
1005 s2n_ktls_configure_connection(client, S2N_KTLS_MODE_RECV);
1006 EXPECT_OK(s2n_skip_handshake(client));
1007
1008 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
1009 s2n_connection_ptr_free);
1010 EXPECT_NOT_NULL(server);
1011 s2n_ktls_configure_connection(server, S2N_KTLS_MODE_SEND);
1012 s2n_ktls_configure_connection(server, S2N_KTLS_MODE_RECV);
1013 EXPECT_OK(s2n_skip_handshake(server));
1014
1015 DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer_pair io_pair = { 0 },
1016 s2n_ktls_io_stuffer_pair_free);
1017 EXPECT_OK(s2n_test_init_ktls_io_stuffer(server, client, &io_pair));
1018
1019 /* Send some application data for shutdown to skip */
1020 uint8_t app_data[] = "hello world";
1021 size_t app_data_size = sizeof(app_data);
1022 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
1023 size_t app_data_count = 5;
1024 for (size_t i = 0; i < app_data_count; i++) {
1025 EXPECT_SUCCESS(s2n_send(client, app_data, app_data_size, &blocked));
1026 EXPECT_SUCCESS(s2n_send(server, app_data, app_data_size, &blocked));
1027 }
1028 EXPECT_OK(s2n_test_validate_ancillary(&io_pair.client_in, TLS_APPLICATION_DATA, app_data_size));
1029 EXPECT_OK(s2n_test_validate_ancillary(&io_pair.server_in, TLS_APPLICATION_DATA, app_data_size));
1030 EXPECT_OK(s2n_test_records_in_ancillary(&io_pair.client_in, app_data_count));
1031 EXPECT_OK(s2n_test_records_in_ancillary(&io_pair.server_in, app_data_count));
1032
1033 /* Client's first shutdown blocks on reading the close_notify,
1034 * but successfully writes the close_notify and skips all the app data.*/
1035 EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown(client, &blocked), S2N_ERR_IO_BLOCKED);
1036 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ);
1037 EXPECT_FALSE(s2n_connection_check_io_status(client, S2N_IO_WRITABLE));
1038 EXPECT_TRUE(s2n_connection_check_io_status(client, S2N_IO_READABLE));
1039 EXPECT_TRUE(client->alert_sent);
1040 EXPECT_OK(s2n_test_records_in_ancillary(&io_pair.client_in, 0));
1041 EXPECT_OK(s2n_test_records_in_ancillary(&io_pair.server_in, app_data_count + 1));
1042
1043 /* Server's first shutdown successfully skips all the app data
1044 * and receives the close_notify */
1045 EXPECT_SUCCESS(s2n_shutdown(server, &blocked));
1046 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
1047 EXPECT_TRUE(s2n_connection_check_io_status(server, S2N_IO_CLOSED));
1048 EXPECT_TRUE(server->alert_sent);
1049 EXPECT_OK(s2n_test_records_in_ancillary(&io_pair.client_in, 1));
1050 EXPECT_OK(s2n_test_records_in_ancillary(&io_pair.server_in, 0));
1051
1052 /* Client's second shutdown successfully receives the close_notify */
1053 EXPECT_SUCCESS(s2n_shutdown(client, &blocked));
1054 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
1055 EXPECT_TRUE(s2n_connection_check_io_status(client, S2N_IO_CLOSED));
1056 EXPECT_OK(s2n_test_records_in_ancillary(&io_pair.client_in, 0));
1057 EXPECT_OK(s2n_test_records_in_ancillary(&io_pair.server_in, 0));
1058 };
1059 };
1060 #endif
1061
1062 END_TEST();
1063 }