Debian Codegraph @

crt/s2n/tests/unit/s2n_handshake_io_early_data_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 "api/s2n.h"
17 #include "s2n_test.h"
18 #include "testlib/s2n_testlib.h"
19 #include "utils/s2n_result.h"
20
21 /* Get access to s2n_handshake_read_io */
22 #include "tls/s2n_handshake_io.c"
23
24 int main(int argc, char **argv)
25 {
26 BEGIN_TEST();
27
28 uint8_t bad_record[] = {
29 0x17, /* ContentType opaque_type = application_data */
30 0x03, 0x03, /* ProtocolVersion legacy_record_version = 0x0303 */
31 0x00, 0x10, /* uint16 length */
32 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
33 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, /* opaque encrypted_record[TLSCiphertext.length] */
34 };
35
36 struct s2n_cipher_suite *test_cipher_suite = &s2n_tls13_aes_256_gcm_sha384;
37 uint8_t test_key_bytes[S2N_TLS13_SECRET_MAX_LEN] = "gibberish key";
38 struct s2n_blob test_key = { 0 };
39 EXPECT_SUCCESS(s2n_blob_init(&test_key, test_key_bytes,
40 test_cipher_suite->record_alg->cipher->key_material_size));
41
42 /**
43 *= https://www.rfc-editor.org/rfc/rfc8446#section-4.2.10
44 *= type=test
45 *# If the client attempts a 0-RTT handshake but the server
46 *# rejects it, the server will generally not have the 0-RTT record
47 *# protection keys and must instead use trial decryption (either with
48 *# the 1-RTT handshake keys or by looking for a cleartext ClientHello in
49 *# the case of a HelloRetryRequest) to find the first non-0-RTT message.
50 *#
51 *# If the server chooses to accept the "early_data" extension, then it
52 *# MUST comply with the same error-handling requirements specified for
53 *# all records when processing early data records. Specifically, if the
54 *# server fails to decrypt a 0-RTT record following an accepted
55 *# "early_data" extension, it MUST terminate the connection with a
56 *# "bad_record_mac" alert as per Section 5.2.
57 */
58 {
59 /* Server */
60 {
61 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
62 EXPECT_NOT_NULL(server_conn);
63 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
64
65 server_conn->secure->cipher_suite = test_cipher_suite;
66 POSIX_GUARD_RESULT(server_conn->secure->cipher_suite->record_alg->cipher->init(&server_conn->secure->client_key));
67 POSIX_GUARD_RESULT(server_conn->secure->cipher_suite->record_alg->cipher->set_decryption_key(&server_conn->secure->client_key, &test_key));
68 server_conn->client = server_conn->secure;
69
70 DEFER_CLEANUP(struct s2n_stuffer io_stuffer = { 0 }, s2n_stuffer_free);
71 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&io_stuffer, S2N_DEFAULT_RECORD_LENGTH));
72 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&io_stuffer, &io_stuffer, server_conn));
73
74 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&io_stuffer, bad_record, sizeof(bad_record)));
75
76 /* Fail for bad record if early data was not requested */
77 {
78 EXPECT_SUCCESS(s2n_stuffer_reread(&io_stuffer));
79 server_conn->early_data_state = S2N_EARLY_DATA_NOT_REQUESTED;
80 EXPECT_FAILURE_WITH_ERRNO(s2n_handshake_read_io(server_conn), S2N_ERR_DECRYPT);
81 };
82
83 /* Fail for bad record if early data was accepted */
84 {
85 EXPECT_SUCCESS(s2n_stuffer_reread(&io_stuffer));
86 server_conn->early_data_state = S2N_EARLY_DATA_ACCEPTED;
87 EXPECT_FAILURE_WITH_ERRNO(s2n_handshake_read_io(server_conn), S2N_ERR_DECRYPT);
88 };
89
90 /* Succeed for bad record if early data was rejected */
91 {
92 EXPECT_SUCCESS(s2n_stuffer_reread(&io_stuffer));
93 server_conn->early_data_state = S2N_EARLY_DATA_REJECTED;
94 EXPECT_SUCCESS(s2n_handshake_read_io(server_conn));
95 };
96
97 EXPECT_SUCCESS(s2n_connection_free(server_conn));
98 };
99
100 /* Client */
101 {
102 /* Fail for bad record if early data was rejected.
103 * Clients send early data but do not receive it, so a bad record is still an error. */
104 {
105 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
106 EXPECT_NOT_NULL(client_conn);
107 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
108
109 client_conn->secure->cipher_suite = test_cipher_suite;
110 POSIX_GUARD_RESULT(client_conn->secure->cipher_suite->record_alg->cipher->init(&client_conn->secure->server_key));
111 POSIX_GUARD_RESULT(client_conn->secure->cipher_suite->record_alg->cipher->set_decryption_key(&client_conn->secure->server_key, &test_key));
112 client_conn->server = client_conn->secure;
113
114 DEFER_CLEANUP(struct s2n_stuffer io_stuffer = { 0 }, s2n_stuffer_free);
115 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&io_stuffer, S2N_DEFAULT_RECORD_LENGTH));
116 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&io_stuffer, &io_stuffer, client_conn));
117
118 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&io_stuffer, bad_record, sizeof(bad_record)));
119
120 client_conn->early_data_state = S2N_EARLY_DATA_REJECTED;
121 EXPECT_FAILURE_WITH_ERRNO(s2n_handshake_read_io(client_conn), S2N_ERR_DECRYPT);
122
123 EXPECT_SUCCESS(s2n_connection_free(client_conn));
124 };
125 };
126 };
127
128 END_TEST();
129 }