Debian Codegraph @

crt/s2n/tests/unit/s2n_record_read_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_record.h"
19 #include "tls/s2n_tls.h"
20 #include "utils/s2n_safety.h"
21
22 #define SSLV2_MIN_SIZE 3
23
24 int main(int argc, char *argv[])
25 {
26 BEGIN_TEST();
27
28 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL,
29 s2n_cert_chain_and_key_ptr_free);
30 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
31 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
32
33 /* Test s2n_sslv2_record_header_parse */
34 {
35 const struct {
36 uint8_t bytes[S2N_TLS_RECORD_HEADER_LENGTH];
37 uint16_t length;
38 uint8_t type;
39 uint8_t version;
40 } test_cases[] = {
41 {
42 .bytes = { S2N_TLS_SSLV2_HEADER_FLAG, SSLV2_MIN_SIZE, TLS_CLIENT_HELLO, 0x03, 0x03 },
43 .length = 0,
44 .type = TLS_CLIENT_HELLO,
45 .version = S2N_TLS12,
46 },
47 {
48 .bytes = { S2N_TLS_SSLV2_HEADER_FLAG, SSLV2_MIN_SIZE + 1, TLS_CLIENT_HELLO, 0x03, 0x03 },
49 .length = 1,
50 .type = TLS_CLIENT_HELLO,
51 .version = S2N_TLS12,
52 },
53 {
54 .bytes = { S2N_TLS_SSLV2_HEADER_FLAG, 0xFF, TLS_CLIENT_HELLO, 0x03, 0x03 },
55 .length = 0xFF - SSLV2_MIN_SIZE,
56 .type = TLS_CLIENT_HELLO,
57 .version = S2N_TLS12,
58 },
59 {
60 .bytes = { S2N_TLS_SSLV2_HEADER_FLAG, 0x84, TLS_CLIENT_HELLO, 0x03, 0x03 },
61 .length = 0x84 - SSLV2_MIN_SIZE,
62 .type = TLS_CLIENT_HELLO,
63 .version = S2N_TLS12,
64 },
65 {
66 .bytes = { 0x84, 0x84, TLS_CLIENT_HELLO, 0x03, 0x03 },
67 .length = 0x484 - SSLV2_MIN_SIZE,
68 .type = TLS_CLIENT_HELLO,
69 .version = S2N_TLS12,
70 },
71 {
72 .bytes = { 0xFF, 0xFF, TLS_CLIENT_HELLO, 0x03, 0x03 },
73 .length = 0x7FFF - SSLV2_MIN_SIZE,
74 .type = TLS_CLIENT_HELLO,
75 .version = S2N_TLS12,
76 },
77 {
78 .bytes = { S2N_TLS_SSLV2_HEADER_FLAG, SSLV2_MIN_SIZE, 0, 0x03, 0x03 },
79 .length = 0,
80 .type = 0,
81 .version = S2N_TLS12,
82 },
83 {
84 .bytes = { S2N_TLS_SSLV2_HEADER_FLAG, SSLV2_MIN_SIZE, 77, 0x03, 0x03 },
85 .length = 0,
86 .type = 77,
87 .version = S2N_TLS12,
88 },
89 {
90 .bytes = { S2N_TLS_SSLV2_HEADER_FLAG, SSLV2_MIN_SIZE, TLS_SERVER_HELLO, 0x03, 0x03 },
91 .length = 0,
92 .type = TLS_SERVER_HELLO,
93 .version = S2N_TLS12,
94 },
95 {
96 .bytes = { S2N_TLS_SSLV2_HEADER_FLAG, SSLV2_MIN_SIZE, TLS_CLIENT_HELLO, 0x03, 0x04 },
97 .length = 0,
98 .type = TLS_CLIENT_HELLO,
99 .version = S2N_TLS13,
100 },
101 {
102 .bytes = { S2N_TLS_SSLV2_HEADER_FLAG, SSLV2_MIN_SIZE, TLS_CLIENT_HELLO, 0, 0 },
103 .length = 0,
104 .type = TLS_CLIENT_HELLO,
105 .version = 0,
106 },
107 };
108
109 /* Test: parse valid record headers */
110 for (size_t i = 0; i < s2n_array_len(test_cases); i++) {
111 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
112 s2n_connection_ptr_free);
113 EXPECT_NOT_NULL(conn);
114
115 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&conn->header_in,
116 test_cases[i].bytes, sizeof(test_cases[i].bytes)));
117
118 uint8_t type = 0, version = 0;
119 uint16_t length = 0;
120 EXPECT_SUCCESS(s2n_sslv2_record_header_parse(conn, &type, &version, &length));
121 EXPECT_EQUAL(test_cases[i].type, type);
122 EXPECT_EQUAL(test_cases[i].version, version);
123 EXPECT_EQUAL(test_cases[i].length, length);
124
125 EXPECT_BYTEARRAY_EQUAL(conn->header_in_data,
126 test_cases[i].bytes, sizeof(test_cases[i].bytes));
127
128 EXPECT_EQUAL(s2n_stuffer_data_available(&conn->header_in), S2N_TLS_RECORD_HEADER_LENGTH);
129 }
130
131 /* Test: parse header with bad length
132 *
133 * We already read 3 bytes by reading the header, so logically the message
134 * is at least 3 bytes long.
135 */
136 {
137 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
138 s2n_connection_ptr_free);
139 EXPECT_NOT_NULL(conn);
140 EXPECT_SUCCESS(s2n_stuffer_skip_write(&conn->header_in, S2N_TLS_RECORD_HEADER_LENGTH));
141 conn->header_in_data[0] = S2N_TLS_SSLV2_HEADER_FLAG;
142
143 uint8_t type = 0, version = 0;
144 uint16_t length = 0;
145
146 conn->header_in_data[1] = SSLV2_MIN_SIZE - 1;
147 EXPECT_FAILURE_WITH_ERRNO(
148 s2n_sslv2_record_header_parse(conn, &type, &version, &length),
149 S2N_ERR_BAD_MESSAGE);
150 EXPECT_EQUAL(s2n_stuffer_data_available(&conn->header_in), 3);
151 EXPECT_SUCCESS(s2n_stuffer_reread(&conn->header_in));
152
153 conn->header_in_data[1] = SSLV2_MIN_SIZE;
154 EXPECT_SUCCESS(s2n_sslv2_record_header_parse(conn, &type, &version, &length));
155 EXPECT_EQUAL(s2n_stuffer_data_available(&conn->header_in), S2N_TLS_RECORD_HEADER_LENGTH);
156 EXPECT_SUCCESS(s2n_stuffer_reread(&conn->header_in));
157
158 conn->header_in_data[1] = 0;
159 EXPECT_FAILURE_WITH_ERRNO(
160 s2n_sslv2_record_header_parse(conn, &type, &version, &length),
161 S2N_ERR_BAD_MESSAGE);
162 EXPECT_EQUAL(s2n_stuffer_data_available(&conn->header_in), 3);
163 EXPECT_SUCCESS(s2n_stuffer_reread(&conn->header_in));
164 };
165 };
166
167 struct {
168 const char *policy;
169 uint8_t version;
170 } policy_test_cases[] = {
171 { .policy = "20240503", .version = S2N_TLS13 },
172 { .policy = "20240501", .version = S2N_TLS12 }
173 };
174
175 /* Ensure that the input buffer is wiped after failing to read a record */
176 for (size_t i = 0; i < s2n_array_len(policy_test_cases); i++) {
177 if (policy_test_cases[i].version == S2N_TLS13 && !s2n_is_tls13_fully_supported()) {
178 continue;
179 }
180
181 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new_minimal(), s2n_config_ptr_free);
182 EXPECT_NOT_NULL(config);
183 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
184 EXPECT_SUCCESS(s2n_config_disable_x509_verification(config));
185 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, policy_test_cases[i].policy));
186
187 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
188 s2n_connection_ptr_free);
189 EXPECT_NOT_NULL(client);
190 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
191 s2n_connection_ptr_free);
192 EXPECT_NOT_NULL(server);
193
194 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
195 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
196
197 EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING));
198
199 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair stuffer_pair = { 0 },
200 s2n_io_stuffer_pair_free);
201 EXPECT_OK(s2n_io_stuffer_pair_init(&stuffer_pair));
202 EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &stuffer_pair));
203
204 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));
205 EXPECT_EQUAL(server->actual_protocol_version, policy_test_cases[i].version);
206
207 /* Send some test data to the server. */
208 uint8_t test_data[] = "hello world";
209 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
210 ssize_t send_size = s2n_send(client, test_data, sizeof(test_data), &blocked);
211 EXPECT_EQUAL(send_size, sizeof(test_data));
212
213 /* Invalidate an encrypted byte to cause decryption to fail. */
214 struct s2n_stuffer invalidation_stuffer = stuffer_pair.server_in;
215 /* Skip the TLS Record Header content, since its not used for decryption in TLS 1.3. */
216 EXPECT_SUCCESS(s2n_stuffer_skip_read(&invalidation_stuffer, S2N_TLS_RECORD_HEADER_LENGTH));
217 uint8_t *corrupt_byte = s2n_stuffer_raw_read(&invalidation_stuffer, 1);
218 EXPECT_NOT_NULL(corrupt_byte);
219 *corrupt_byte += 1;
220
221 /* Receive the invalid data. */
222 uint8_t buffer[sizeof(test_data)] = { 0 };
223 ssize_t ret = s2n_recv(server, buffer, sizeof(buffer), &blocked);
224 EXPECT_FAILURE_WITH_ERRNO(ret, S2N_ERR_DECRYPT);
225
226 /* Ensure that the invalid data has been wiped from the input buffer. */
227 EXPECT_TRUE(s2n_stuffer_is_wiped(&server->in));
228 }
229
230 END_TEST();
231 }