Debian Codegraph @

crt/s2n/tests/unit/s2n_alerts_protocol_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 "s2n_test.h"
17 #include "testlib/s2n_testlib.h"
18 #include "tls/s2n_alerts.h"
19 #include "tls/s2n_tls.h"
20
21 #define ERROR_ALERTS_COUNT (UINT16_MAX)
22 #define END_OF_DATA 0
23 #define MODE_COUNT 2
24
25 int s2n_test_ch_cb(struct s2n_connection *conn, void *context)
26 {
27 return S2N_FAILURE;
28 }
29
30 typedef enum {
31 S2N_TEST_DURING_HANDSHAKE,
32 S2N_TEST_AFTER_HANDSHAKE,
33 S2N_TEST_TYPE_COUNT
34 } s2n_test_type;
35
36 int main(int argc, char **argv)
37 {
38 BEGIN_TEST();
39
40 if (!s2n_is_tls13_fully_supported()) {
41 return 0;
42 }
43
44 uint8_t alert_header[] = {
45 /* type */
46 TLS_ALERT,
47 /* legacy_record_version */
48 0x03, 0x03,
49 /* length */
50 0x00, 0x02
51 };
52 uint8_t close_notify[] = { 1, S2N_TLS_ALERT_CLOSE_NOTIFY };
53 uint8_t data[] = "hello";
54
55 /**
56 *= https://www.rfc-editor.org/rfc/rfc8446#section-6
57 *= type=test
58 *# Unknown Alert types MUST be treated as error alerts.
59 */
60 uint8_t test_alert_levels[] = { 0, 1, 2, 3, 10, UINT8_MAX };
61
62 /**
63 *= https://www.rfc-editor.org/rfc/rfc8446#section-6
64 *= type=test
65 *# All the alerts listed in Section 6.2 MUST be sent with
66 *# AlertLevel=fatal and MUST be treated as error alerts when received
67 *# regardless of the AlertLevel in the message.
68 */
69 uint8_t error_alerts[ERROR_ALERTS_COUNT][2] = { 0 };
70 size_t error_alerts_count = 0;
71 for (size_t level_i = 0; level_i < s2n_array_len(test_alert_levels); level_i++) {
72 for (size_t alert_code = 0; alert_code <= UINT8_MAX; alert_code++) {
73 /* Skip closure alerts */
74 if (alert_code == S2N_TLS_ALERT_CLOSE_NOTIFY) {
75 continue;
76 }
77 if (alert_code == S2N_TLS_ALERT_USER_CANCELED) {
78 continue;
79 }
80
81 /* To speed up the test, let's exclude a chunk of the unassigned values. */
82 if (alert_code > 120 && alert_code < 250) {
83 continue;
84 }
85
86 EXPECT_TRUE(error_alerts_count < ERROR_ALERTS_COUNT);
87 error_alerts[error_alerts_count][0] = test_alert_levels[level_i];
88 error_alerts[error_alerts_count][1] = alert_code;
89 error_alerts_count++;
90 }
91 }
92
93 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL,
94 s2n_cert_chain_and_key_ptr_free);
95 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
96 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
97
98 DEFER_CLEANUP(struct s2n_cert_chain_and_key *ecdsa_chain_and_key = NULL,
99 s2n_cert_chain_and_key_ptr_free);
100 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&ecdsa_chain_and_key,
101 S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY));
102
103 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(),
104 s2n_config_ptr_free);
105 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config));
106 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13"));
107 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
108
109 DEFER_CLEANUP(struct s2n_config *ecdsa_config = s2n_config_new(),
110 s2n_config_ptr_free);
111 EXPECT_OK(s2n_config_set_tls12_security_policy(ecdsa_config));
112 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(ecdsa_config));
113 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(ecdsa_config, ecdsa_chain_and_key));
114
115 /* Test: Receiving an error alert */
116 for (size_t i = 0; i < error_alerts_count; i++) {
117 for (s2n_test_type type = 0; type < S2N_TEST_TYPE_COUNT; type++) {
118 for (uint8_t mode = 0; mode < MODE_COUNT; mode++) {
119 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
120 s2n_connection_ptr_free);
121 EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING));
122 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
123
124 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
125 s2n_connection_ptr_free);
126 EXPECT_SUCCESS(s2n_connection_set_blinding(client, S2N_SELF_SERVICE_BLINDING));
127 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
128
129 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
130 EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
131 EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair));
132
133 if (type == S2N_TEST_DURING_HANDSHAKE) {
134 /* Partially perform handshake */
135 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server, client, SERVER_CERT));
136 } else {
137 /* Complete handshake */
138 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));
139 }
140 EXPECT_EQUAL(server->actual_protocol_version, S2N_TLS13);
141
142 struct s2n_connection *receiver = server;
143 struct s2n_connection *sender = client;
144 if (mode == S2N_CLIENT) {
145 receiver = client;
146 sender = server;
147 }
148
149 /* Send alert */
150 struct s2n_blob alert = { 0 };
151 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
152 EXPECT_SUCCESS(s2n_blob_init(&alert, error_alerts[i], sizeof(error_alerts[0])));
153 EXPECT_OK(s2n_record_write(sender, TLS_ALERT, &alert));
154 EXPECT_SUCCESS(s2n_flush(sender, &blocked));
155
156 /**
157 *= https://www.rfc-editor.org/rfc/rfc8446#section-6
158 *= type=test
159 *# Upon receiving an error alert, the TLS implementation
160 *# SHOULD indicate an error to the application and MUST NOT allow any
161 *# further data to be sent or received on the connection.
162 */
163 if (type == S2N_TEST_DURING_HANDSHAKE) {
164 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate(receiver, &blocked), S2N_ERR_ALERT);
165 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate(receiver, &blocked), S2N_ERR_CLOSED);
166 } else {
167 EXPECT_FAILURE_WITH_ERRNO(s2n_recv(receiver, data, sizeof(data), &blocked), S2N_ERR_ALERT);
168 EXPECT_FAILURE_WITH_ERRNO(s2n_recv(receiver, data, sizeof(data), &blocked), S2N_ERR_CLOSED);
169 EXPECT_FAILURE_WITH_ERRNO(s2n_send(receiver, data, sizeof(data), &blocked), S2N_ERR_CLOSED);
170 }
171
172 /**
173 *= https://www.rfc-editor.org/rfc/rfc8446#section-6.2
174 *= type=test
175 *# Upon transmission or
176 *# receipt of a fatal alert message, both parties MUST immediately close
177 *# the connection.
178 */
179 EXPECT_TRUE(s2n_connection_check_io_status(receiver, S2N_IO_CLOSED));
180
181 /**
182 *= https://www.rfc-editor.org/rfc/rfc8446#section-6.2
183 *= type=test
184 *# The implementation SHOULD provide a way to facilitate logging the sending
185 *# and receiving of alerts.
186 */
187 EXPECT_EQUAL(error_alerts[i][1], s2n_connection_get_alert(receiver));
188 }
189 }
190 };
191
192 /* Test: Sending an error alert */
193 {
194 /* Testing a variety of alerts is more difficult sending than receiving.
195 * We can't trigger every possible fatal error, so just choose some common ones.
196 */
197 int test_errors[] = {
198 /* handshake errors without blinding */
199 S2N_ERR_CANCELLED,
200 S2N_ERR_CIPHER_NOT_SUPPORTED,
201 /* handshake errors with blinding */
202 S2N_ERR_PROTOCOL_DOWNGRADE_DETECTED,
203 S2N_ERR_CERT_INVALID_HOSTNAME,
204 /* application data error */
205 S2N_ERR_DECRYPT,
206 };
207
208 DEFER_CLEANUP(struct s2n_config *bad_cb_config = s2n_config_new(),
209 s2n_config_ptr_free);
210 EXPECT_OK(s2n_config_set_tls12_security_policy(bad_cb_config));
211 EXPECT_SUCCESS(s2n_config_set_client_hello_cb(bad_cb_config, s2n_test_ch_cb, NULL));
212
213 DEFER_CLEANUP(struct s2n_config *untrusted_config = s2n_config_new(),
214 s2n_config_ptr_free);
215 EXPECT_OK(s2n_config_set_tls12_security_policy(untrusted_config));
216
217 for (size_t i = 0; i < s2n_array_len(test_errors); i++) {
218 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
219 s2n_connection_ptr_free);
220 EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING));
221 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
222
223 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
224 s2n_connection_ptr_free);
225 EXPECT_SUCCESS(s2n_connection_set_blinding(client, S2N_SELF_SERVICE_BLINDING));
226 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
227
228 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
229 EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
230 EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair));
231
232 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
233 uint8_t expected_alert = S2N_TLS_ALERT_CLOSE_NOTIFY;
234 struct s2n_connection *failed_conn = server;
235 struct s2n_connection *closed_conn = client;
236
237 switch (test_errors[i]) {
238 case S2N_ERR_CANCELLED:
239 /* Error triggered by callback failure during handshake */
240 EXPECT_SUCCESS(s2n_connection_set_config(server, bad_cb_config));
241 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server, client),
242 S2N_ERR_CANCELLED);
243
244 expected_alert = S2N_TLS_ALERT_HANDSHAKE_FAILURE;
245 break;
246 case S2N_ERR_CIPHER_NOT_SUPPORTED:
247 /* Error triggered if no valid cipher suites.
248 * Use a security policy that only supports RSA certs for the client,
249 * and only set an EC cert for the server.
250 */
251 EXPECT_SUCCESS(s2n_connection_set_config(server, ecdsa_config));
252 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client, "20170210"));
253
254 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server, client),
255 S2N_ERR_CIPHER_NOT_SUPPORTED);
256 break;
257 case S2N_ERR_PROTOCOL_DOWNGRADE_DETECTED:
258 /* Remove TLS1.3 support before the ClientHello by setting
259 * a security policy that doesn't support TLS1.3, then add
260 * TLS1.3 support back after the ClientHello.
261 */
262 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client, "20170210"));
263 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate(client, &blocked), S2N_ERR_IO_BLOCKED);
264 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate(server, &blocked), S2N_ERR_IO_BLOCKED);
265 client->client_protocol_version = S2N_TLS13;
266
267 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server, client),
268 S2N_ERR_PROTOCOL_DOWNGRADE_DETECTED);
269
270 failed_conn = client;
271 closed_conn = server;
272 break;
273 case S2N_ERR_CERT_INVALID_HOSTNAME:
274 EXPECT_SUCCESS(s2n_connection_set_config(client, untrusted_config));
275
276 EXPECT_FAILURE_WITH_ALERT(s2n_negotiate_test_server_and_client(server, client),
277 S2N_ERR_CERT_INVALID_HOSTNAME, S2N_TLS_ALERT_CERTIFICATE_UNKNOWN);
278
279 failed_conn = client;
280 closed_conn = server;
281 break;
282 case S2N_ERR_DECRYPT:
283 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));
284
285 /* The server will expect the original sequence number,
286 * not our updated sequence number, so fail to decrypt.
287 */
288 client->secure->client_sequence_number[0] = 0xFF;
289 EXPECT_EQUAL(s2n_send(client, data, sizeof(data), &blocked), sizeof(data));
290 EXPECT_FAILURE_WITH_ERRNO(s2n_recv(server, data, sizeof(data), &blocked),
291 S2N_ERR_DECRYPT);
292 break;
293 default:
294 FAIL_MSG("Fatal error not implemented for test");
295 }
296
297 /* Remove any blinding so that we can immediately call shutdown */
298 failed_conn->delay = 0;
299
300 /* The alert will not be sent until we attempt to shutdown */
301 EXPECT_SUCCESS(s2n_shutdown_send(failed_conn, &blocked));
302 EXPECT_TRUE(failed_conn->alert_sent);
303
304 /**
305 *= https://www.rfc-editor.org/rfc/rfc8446#section-6.2
306 *= type=test
307 *# Upon transmission or
308 *# receipt of a fatal alert message, both parties MUST immediately close
309 *# the connection.
310 */
311 EXPECT_TRUE(s2n_connection_check_io_status(failed_conn, S2N_IO_CLOSED));
312
313 /**
314 *= https://www.rfc-editor.org/rfc/rfc8446#section-6.2
315 *= type=test
316 *# Whenever an implementation encounters a fatal error condition, it
317 *# SHOULD send an appropriate fatal alert
318 */
319 if (expected_alert == S2N_TLS_ALERT_CLOSE_NOTIFY) {
320 EXPECT_EQUAL(s2n_recv(closed_conn, data, sizeof(data), &blocked), END_OF_DATA);
321 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
322 } else {
323 EXPECT_FAILURE_WITH_ERRNO(s2n_recv(closed_conn, data, sizeof(data), &blocked), S2N_ERR_ALERT);
324 }
325 EXPECT_EQUAL(expected_alert, s2n_connection_get_alert(closed_conn));
326 /**
327 *= https://www.rfc-editor.org/rfc/rfc8446#section-6.2
328 *= type=test
329 *# and MUST close the connection
330 *# without sending or receiving any additional data.
331 */
332 EXPECT_FAILURE_WITH_ERRNO(s2n_send(failed_conn, data, sizeof(data), &blocked),
333 S2N_ERR_CLOSED);
334 EXPECT_FAILURE_WITH_ERRNO(s2n_recv(failed_conn, data, sizeof(data), &blocked), S2N_ERR_CLOSED);
335 }
336 };
337
338 /* Test: Receiving a closure alert
339 *
340 *= https://www.rfc-editor.org/rfc/rfc8446#section-6.1
341 *= type=test
342 *# Either party MAY initiate a close of its write side of the connection
343 *# by sending a "close_notify" alert. Any data received after a closure
344 *# alert has been received MUST be ignored.
345 */
346 for (s2n_test_type type = 0; type < S2N_TEST_TYPE_COUNT; type++) {
347 for (uint8_t mode = 0; mode < MODE_COUNT; mode++) {
348 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
349 s2n_connection_ptr_free);
350 EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING));
351 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
352
353 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
354 s2n_connection_ptr_free);
355 EXPECT_SUCCESS(s2n_connection_set_blinding(client, S2N_SELF_SERVICE_BLINDING));
356 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
357
358 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
359 EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
360 EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair));
361
362 if (type == S2N_TEST_DURING_HANDSHAKE) {
363 /* Partially perform handshake */
364 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server, client, SERVER_CERT));
365 } else {
366 /* Complete handshake */
367 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));
368 }
369
370 struct s2n_connection *receiver = server;
371 struct s2n_stuffer *input = &io_pair.server_in;
372 if (mode == S2N_CLIENT) {
373 receiver = client;
374 input = &io_pair.client_in;
375 }
376
377 /* Send close_notify */
378 EXPECT_SUCCESS(s2n_stuffer_write_bytes(input, alert_header, sizeof(alert_header)));
379 EXPECT_SUCCESS(s2n_stuffer_write_bytes(input, close_notify, sizeof(close_notify)));
380
381 /* Receive close_notify */
382 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
383 if (type == S2N_TEST_DURING_HANDSHAKE) {
384 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate(receiver, &blocked), S2N_ERR_CLOSED);
385 } else {
386 EXPECT_EQUAL(s2n_recv(receiver, data, sizeof(data), &blocked), END_OF_DATA);
387 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
388 }
389 EXPECT_FALSE(s2n_connection_check_io_status(receiver, S2N_IO_READABLE));
390
391 /*
392 *= https://www.rfc-editor.org/rfc/rfc8446#section-6.1
393 *= type=test
394 *# Any data received after a closure alert has been received MUST be ignored.
395 */
396 EXPECT_SUCCESS(s2n_stuffer_write_bytes(input, data, sizeof(data)));
397 if (type == S2N_TEST_DURING_HANDSHAKE) {
398 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate(receiver, &blocked), S2N_ERR_CLOSED);
399 } else {
400 EXPECT_EQUAL(s2n_recv(receiver, data, sizeof(data), &blocked), END_OF_DATA);
401 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
402 }
403 };
404 };
405
406 /* Test: Sending a closure alert
407 *
408 *= https://www.rfc-editor.org/rfc/rfc8446#section-6.1
409 *= type=test
410 *# Each party MUST send a "close_notify" alert before closing its write
411 *# side of the connection, unless it has already sent some error alert.
412 *# This does not have any effect on its read side of the connection.
413 */
414 for (uint8_t mode = 0; mode < MODE_COUNT; mode++) {
415 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
416 s2n_connection_ptr_free);
417 EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING));
418 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
419
420 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
421 s2n_connection_ptr_free);
422 EXPECT_SUCCESS(s2n_connection_set_blinding(client, S2N_SELF_SERVICE_BLINDING));
423 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
424
425 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
426 EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
427 EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair));
428
429 /* Perform handshake */
430 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));
431 EXPECT_EQUAL(server->actual_protocol_version, S2N_TLS13);
432
433 struct s2n_connection *sender = server;
434 struct s2n_connection *receiver = client;
435 if (mode == S2N_CLIENT) {
436 sender = client;
437 receiver = server;
438 }
439
440 /* Send close_notify */
441 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
442 EXPECT_SUCCESS(s2n_shutdown_send(sender, &blocked));
443 EXPECT_FALSE(s2n_connection_check_io_status(sender, S2N_IO_WRITABLE));
444
445 /* Receive close_notify
446 *
447 *= https://www.rfc-editor.org/rfc/rfc8446#section-6
448 *= type=test
449 *# The "close_notify" alert is used to indicate orderly closure of one
450 *# direction of the connection. Upon receiving such an alert, the TLS
451 *# implementation SHOULD indicate end-of-data to the application.
452 */
453 EXPECT_EQUAL(s2n_recv(receiver, data, sizeof(data), &blocked), END_OF_DATA);
454 EXPECT_FALSE(s2n_connection_check_io_status(receiver, S2N_IO_READABLE));
455 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
456
457 /* Read side is NOT affected.
458 * The receiver of the close_notify can continue to send, and the sender
459 * should continue to read.
460 */
461 EXPECT_EQUAL(s2n_send(receiver, data, sizeof(data), &blocked), sizeof(data));
462 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
463 EXPECT_EQUAL(s2n_recv(sender, data, sizeof(data), &blocked), sizeof(data));
464 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
465
466 /* Respond with close_notify */
467 EXPECT_SUCCESS(s2n_shutdown(receiver, &blocked));
468 EXPECT_SUCCESS(s2n_shutdown(sender, &blocked));
469 };
470
471 /* Test: Closure alerts in TLS1.2
472 *
473 *= https://www.rfc-editor.org/rfc/rfc8446#section-6.1
474 *= type=test
475 *# Note that this is a change from versions of TLS prior to TLS 1.3 in
476 *# which implementations were required to react to a "close_notify" by
477 *# discarding pending writes and sending an immediate "close_notify"
478 *# alert of their own.
479 */
480 for (uint8_t mode = 0; mode < MODE_COUNT; mode++) {
481 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
482 s2n_connection_ptr_free);
483 EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING));
484 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
485 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server, "20240501"));
486
487 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
488 s2n_connection_ptr_free);
489 EXPECT_SUCCESS(s2n_connection_set_blinding(client, S2N_SELF_SERVICE_BLINDING));
490 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
491 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client, "20240501"));
492
493 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
494 EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
495 EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair));
496
497 /* Perform handshake */
498 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));
499 EXPECT_EQUAL(server->actual_protocol_version, S2N_TLS12);
500
501 struct s2n_connection *sender = server;
502 struct s2n_connection *receiver = client;
503 if (mode == S2N_CLIENT) {
504 sender = client;
505 receiver = server;
506 }
507
508 /* Send close_notify */
509 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
510 EXPECT_SUCCESS(s2n_shutdown_send(sender, &blocked));
511 EXPECT_TRUE(s2n_connection_check_io_status(sender, S2N_IO_CLOSED));
512
513 /* Receive close_notify */
514 EXPECT_EQUAL(s2n_recv(receiver, data, sizeof(data), &blocked), END_OF_DATA);
515 EXPECT_TRUE(s2n_connection_check_io_status(receiver, S2N_IO_CLOSED));
516 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
517
518 /* Read side is affected.
519 * The receiver should discard writes and just send a close_notify.
520 */
521 EXPECT_FAILURE_WITH_ERRNO(s2n_send(receiver, data, sizeof(data), &blocked),
522 S2N_ERR_CLOSED);
523
524 /* Respond with close_notify */
525 EXPECT_SUCCESS(s2n_shutdown(receiver, &blocked));
526 EXPECT_SUCCESS(s2n_shutdown(sender, &blocked));
527 };
528
529 /* Test: End-of-Data
530 *
531 *= https://www.rfc-editor.org/rfc/rfc8446#6.1
532 *= type=test
533 *# If a transport-level close
534 *# is received prior to a "close_notify", the receiver cannot know that
535 *# all the data that was sent has been received.
536 *
537 *= https://www.rfc-editor.org/rfc/rfc8446#6.1
538 *= type=test
539 *# If the application protocol using TLS provides that any data may be
540 *# carried over the underlying transport after the TLS connection is
541 *# closed, the TLS implementation MUST receive a "close_notify" alert
542 *# before indicating end-of-data to the application layer.
543 */
544 for (uint8_t mode = 0; mode < MODE_COUNT; mode++) {
545 /* Test: Without partial read */
546 {
547 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
548 s2n_connection_ptr_free);
549 EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING));
550 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
551
552 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
553 s2n_connection_ptr_free);
554 EXPECT_SUCCESS(s2n_connection_set_blinding(client, S2N_SELF_SERVICE_BLINDING));
555 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
556
557 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
558 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
559 EXPECT_SUCCESS(s2n_connections_set_io_pair(client, server, &io_pair));
560
561 /* Perform handshake */
562 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));
563
564 /* Close one end of pipe */
565 EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, mode));
566
567 struct s2n_connection *receiver = client;
568 if (mode == S2N_CLIENT) {
569 receiver = server;
570 }
571
572 /* Subsequent reads should NOT report END_OF_DATA, but instead an error */
573 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
574 for (size_t i = 0; i < 5; i++) {
575 EXPECT_FAILURE_WITH_ERRNO(s2n_recv(receiver, data, sizeof(data), &blocked),
576 S2N_ERR_CLOSED);
577 EXPECT_FALSE(s2n_connection_check_io_status(receiver, S2N_IO_READABLE));
578 }
579 };
580
581 /* Test: With partial read */
582 {
583 /* In order to trigger a partial read, we need to encounter end-of-data
584 * when some data has already been successfully read. For that to be true,
585 * we need to read multiple records (one successfully, then end-of-data).
586 */
587 struct s2n_config partial_write_config_copy = *config;
588 partial_write_config_copy.recv_multi_record = true;
589
590 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
591 s2n_connection_ptr_free);
592 EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING));
593 EXPECT_SUCCESS(s2n_connection_set_config(server, &partial_write_config_copy));
594
595 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
596 s2n_connection_ptr_free);
597 EXPECT_SUCCESS(s2n_connection_set_blinding(client, S2N_SELF_SERVICE_BLINDING));
598 EXPECT_SUCCESS(s2n_connection_set_config(client, &partial_write_config_copy));
599
600 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
601 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
602 EXPECT_SUCCESS(s2n_connections_set_io_pair(client, server, &io_pair));
603
604 /* Perform handshake */
605 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));
606
607 struct s2n_connection *sender = server;
608 struct s2n_connection *receiver = client;
609 if (mode == S2N_CLIENT) {
610 sender = client;
611 receiver = server;
612 }
613
614 /* Send some data */
615 const uint8_t partial_write = sizeof(data) / 2;
616 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
617 EXPECT_EQUAL(s2n_send(sender, data, partial_write, &blocked), partial_write);
618 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
619
620 /* Close one end of pipe */
621 EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, mode));
622
623 /* First read should report a partial read, but also close the connection */
624 EXPECT_EQUAL(s2n_recv(receiver, data, sizeof(data), &blocked), partial_write);
625 EXPECT_FALSE(s2n_connection_check_io_status(receiver, S2N_IO_READABLE));
626
627 /* Since we read at least one byte, the blocked status should be S2N_NOT_BLOCKED */
628 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
629
630 /* Subsequent reads should NOT report END_OF_DATA, but instead an error */
631 for (size_t i = 0; i < 5; i++) {
632 EXPECT_FAILURE_WITH_ERRNO(s2n_recv(receiver, data, sizeof(data), &blocked),
633 S2N_ERR_CLOSED);
634 }
635 };
636 };
637
638 /* Test: s2n_connection_get_alert is idempotent */
639 {
640 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
641 s2n_connection_ptr_free);
642 EXPECT_NOT_NULL(conn);
643
644 /* Write a fatal alert directly into alert_in */
645 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&conn->alert_in, S2N_TLS_ALERT_LEVEL_FATAL));
646 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&conn->alert_in, S2N_TLS_ALERT_HANDSHAKE_FAILURE));
647
648 /* Repeated calls should all return the same alert */
649 for (size_t i = 0; i < 5; i++) {
650 EXPECT_EQUAL(s2n_connection_get_alert(conn), S2N_TLS_ALERT_HANDSHAKE_FAILURE);
651 }
652
653 /* The stuffer should still have the original data */
654 EXPECT_EQUAL(s2n_stuffer_data_available(&conn->alert_in), 2);
655 };
656
657 END_TEST();
658 }