Debian Codegraph @

crt/s2n/tests/unit/s2n_post_handshake_recv_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/unstable/renegotiate.h"
17 #include "error/s2n_errno.h"
18 #include "s2n_test.h"
19 #include "testlib/s2n_mem_testlib.h"
20 #include "testlib/s2n_testlib.h"
21 #include "tls/s2n_connection.h"
22 #include "tls/s2n_key_update.h"
23 #include "tls/s2n_post_handshake.h"
24 #include "tls/s2n_tls.h"
25 #include "tls/s2n_tls13_handshake.h"
26 #include "utils/s2n_safety.h"
27
28 #define S2N_TEST_MESSAGE_COUNT 5
29
30 int s2n_key_update_write(struct s2n_blob *out);
31
32 size_t tickets_count = 0;
33 static int s2n_ticket_count_cb(struct s2n_connection *conn, void *ctx, struct s2n_session_ticket *ticket)
34 {
35 tickets_count++;
36 return S2N_SUCCESS;
37 }
38
39 size_t hello_request_count = 0;
40 static int s2n_hello_request_cb(struct s2n_connection *conn, void *ctx, s2n_renegotiate_response *response)
41 {
42 hello_request_count++;
43 *response = S2N_RENEGOTIATE_IGNORE;
44 return S2N_SUCCESS;
45 }
46
47 static S2N_RESULT s2n_test_send_records(struct s2n_connection *conn, struct s2n_stuffer messages, uint32_t fragment_size)
48 {
49 conn->max_outgoing_fragment_length = fragment_size;
50
51 DEFER_CLEANUP(struct s2n_blob record_data = { 0 }, s2n_free);
52 RESULT_GUARD_POSIX(s2n_alloc(&record_data, fragment_size));
53
54 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
55 uint32_t remaining = 0;
56 while ((remaining = s2n_stuffer_data_available(&messages)) > 0) {
57 record_data.size = S2N_MIN(record_data.size, remaining);
58 RESULT_GUARD_POSIX(s2n_stuffer_read(&messages, &record_data));
59 RESULT_GUARD(s2n_record_write(conn, TLS_HANDSHAKE, &record_data));
60 RESULT_GUARD_POSIX(s2n_flush(conn, &blocked));
61 };
62
63 return S2N_RESULT_OK;
64 }
65
66 /*
67 * Verify that the receiver can receive a byte sent by the sender.
68 * In the process, we also verify that the receiver can receive all previous
69 * data sent by the sender, since TCP / TLS messages have a guaranteed order.
70 */
71 static S2N_RESULT s2n_test_basic_recv(struct s2n_connection *sender, struct s2n_connection *receiver)
72 {
73 uint8_t app_data[1] = { 0 };
74 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
75
76 int send_ret = s2n_send(sender, app_data, sizeof(app_data), &blocked);
77 RESULT_GUARD_POSIX(send_ret);
78 RESULT_ENSURE_EQ(send_ret, sizeof(app_data));
79
80 /* Reset all counters */
81 RESULT_GUARD(s2n_mem_test_wipe_callbacks());
82 tickets_count = 0;
83 hello_request_count = 0;
84
85 int recv_ret = s2n_recv(receiver, app_data, sizeof(app_data), &blocked);
86 RESULT_GUARD_POSIX(recv_ret);
87 RESULT_ENSURE_EQ(recv_ret, sizeof(app_data));
88
89 return S2N_RESULT_OK;
90 }
91
92 /* Like s2n_test_basic_recv,
93 * but we make only one byte of data available at a time.
94 * This forces us to call s2n_recv repeatedly and verifies that s2n_recv
95 * can resume across s2n_recv calls while handling fragmented post-handshake messages.
96 */
97 static S2N_RESULT s2n_test_blocking_recv(struct s2n_connection *sender, struct s2n_connection *receiver,
98 struct s2n_test_io_stuffer_pair *io_pair)
99 {
100 uint8_t app_data[1] = { 0 };
101 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
102
103 int send_ret = s2n_send(sender, app_data, sizeof(app_data), &blocked);
104 RESULT_GUARD_POSIX(send_ret);
105 RESULT_ENSURE_EQ(send_ret, sizeof(app_data));
106
107 /* Reset all counters */
108 RESULT_GUARD(s2n_mem_test_wipe_callbacks());
109 tickets_count = 0;
110 hello_request_count = 0;
111
112 /* Modify the stuffer's write_cursor to make only one byte
113 * of the socket / input data available at a time.
114 */
115 struct s2n_stuffer *in = &io_pair->client_in;
116 if (receiver->mode == S2N_SERVER) {
117 in = &io_pair->server_in;
118 }
119 uint32_t *write_cursor = &in->write_cursor;
120 uint32_t *read_cursor = &in->read_cursor;
121 RESULT_ENSURE_GT(write_cursor, read_cursor);
122 uint32_t saved_write_cursor = *write_cursor;
123 RESULT_ENSURE_GT(saved_write_cursor, 0);
124 *write_cursor = *read_cursor + 1;
125
126 while (s2n_recv(receiver, app_data, sizeof(app_data), &blocked) < 0) {
127 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ);
128 EXPECT_EQUAL(s2n_errno, S2N_ERR_IO_BLOCKED);
129 (*write_cursor)++;
130 RESULT_ENSURE_LTE(*write_cursor, saved_write_cursor);
131 }
132 RESULT_ENSURE_EQ(*write_cursor, saved_write_cursor);
133
134 return S2N_RESULT_OK;
135 }
136
137 static S2N_RESULT s2n_test_init_sender_and_receiver(struct s2n_config *config,
138 struct s2n_connection *sender, struct s2n_connection *receiver,
139 struct s2n_test_io_stuffer_pair *io_pair)
140 {
141 RESULT_GUARD_POSIX(s2n_connection_set_config(sender, config));
142 RESULT_GUARD_POSIX(s2n_connection_set_all_protocol_versions(sender, S2N_TLS13));
143 RESULT_GUARD(s2n_connection_set_secrets(sender));
144 RESULT_GUARD_POSIX(s2n_connection_set_blinding(sender, S2N_SELF_SERVICE_BLINDING));
145
146 RESULT_GUARD_POSIX(s2n_connection_set_config(receiver, config));
147 RESULT_GUARD_POSIX(s2n_connection_set_all_protocol_versions(receiver, S2N_TLS13));
148 RESULT_GUARD(s2n_connection_set_secrets(receiver));
149 RESULT_GUARD_POSIX(s2n_connection_set_blinding(receiver, S2N_SELF_SERVICE_BLINDING));
150
151 RESULT_GUARD(s2n_io_stuffer_pair_init(io_pair));
152 if (sender->mode == S2N_SERVER) {
153 RESULT_GUARD(s2n_connections_set_io_stuffer_pair(receiver, sender, io_pair));
154 } else {
155 RESULT_GUARD(s2n_connections_set_io_stuffer_pair(sender, receiver, io_pair));
156 }
157
158 /* Send and receive to initialize io buffers */
159 EXPECT_OK(s2n_test_basic_recv(sender, receiver));
160
161 return S2N_RESULT_OK;
162 }
163
164 int main(int argc, char **argv)
165 {
166 BEGIN_TEST();
167
168 const uint8_t unknown_message_type = UINT8_MAX;
169 const uint32_t test_large_message_size = 3001;
170
171 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
172 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13"));
173 EXPECT_SUCCESS(s2n_config_set_session_ticket_cb(config, s2n_ticket_count_cb, NULL));
174 EXPECT_SUCCESS(s2n_config_set_renegotiate_request_cb(config, s2n_hello_request_cb, NULL));
175
176 /* Some tests require sending and receiving tickets.
177 * Setup the config to handle tickets, but don't send any by default.
178 */
179 uint8_t ticket_key_name[16] = "key name";
180 uint8_t ticket_key[] = "key data";
181 uint64_t current_time = 0;
182 EXPECT_SUCCESS(config->wall_clock(config->sys_clock_ctx, &current_time));
183 EXPECT_SUCCESS(s2n_config_set_session_tickets_onoff(config, 1));
184 EXPECT_SUCCESS(s2n_config_add_ticket_crypto_key(config, ticket_key_name, sizeof(ticket_key_name),
185 ticket_key, sizeof(ticket_key), current_time / ONE_SEC_IN_NANOS));
186 config->initial_tickets_to_send = 0;
187
188 const uint32_t fragment_sizes[] = {
189 1,
190 2,
191 S2N_MIN_SEND_BUFFER_FRAGMENT_SIZE,
192 TLS_HANDSHAKE_HEADER_LENGTH,
193 TLS_HANDSHAKE_HEADER_LENGTH + 1,
194 S2N_DEFAULT_FRAGMENT_LENGTH,
195 S2N_TLS_MAXIMUM_FRAGMENT_LENGTH,
196 };
197 const uint8_t modes[] = { S2N_CLIENT, S2N_SERVER };
198
199 /* Test: client and server receive small post-handshake messages (KeyUpdates) */
200 for (size_t frag_i = 0; frag_i < s2n_array_len(fragment_sizes); frag_i++) {
201 uint32_t fragment_size = fragment_sizes[frag_i];
202
203 for (size_t mode_i = 0; mode_i < s2n_array_len(modes); mode_i++) {
204 uint8_t mode = modes[mode_i];
205
206 DEFER_CLEANUP(struct s2n_connection *receiver = s2n_connection_new(mode), s2n_connection_ptr_free);
207 DEFER_CLEANUP(struct s2n_connection *sender = s2n_connection_new(S2N_PEER_MODE(mode)), s2n_connection_ptr_free);
208 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
209 EXPECT_OK(s2n_test_init_sender_and_receiver(config, sender, receiver, &io_pair));
210
211 /* Write KeyUpdate message */
212 struct s2n_stuffer message = { 0 };
213 DEFER_CLEANUP(struct s2n_blob message_blob = { 0 }, s2n_free);
214 EXPECT_SUCCESS(s2n_alloc(&message_blob, S2N_KEY_UPDATE_MESSAGE_SIZE));
215 for (size_t i = 0; i < S2N_TEST_MESSAGE_COUNT; i++) {
216 EXPECT_SUCCESS(s2n_key_update_write(&message_blob));
217 EXPECT_SUCCESS(s2n_stuffer_init(&message, &message_blob));
218 EXPECT_SUCCESS(s2n_stuffer_skip_write(&message, S2N_KEY_UPDATE_MESSAGE_SIZE));
219
220 /* The TLS1.3 RFC says "Handshake messages MUST NOT span key changes".
221 * Because KeyUpdate messages trigger key changes, we cannot include multiple in one record.
222 * We must send individual KeyUpdate messages.
223 */
224 EXPECT_OK(s2n_test_send_records(sender, message, fragment_size));
225
226 /* Update the traffic keys for the next records */
227 EXPECT_SUCCESS(s2n_update_application_traffic_keys(sender, sender->mode, SENDING));
228 }
229
230 /*
231 * We have no mechanism to count KeyUpdates, but we can assume they are processed
232 * if we successfully decrypt all records. If they were not processed,
233 * then we would try to use the wrong key to decrypt the next record.
234 */
235 DEFER_CLEANUP(struct s2n_mem_test_cb_scope mem_ctx = { 0 }, s2n_mem_test_free_callbacks);
236 EXPECT_OK(s2n_mem_test_init_callbacks(&mem_ctx));
237 EXPECT_OK(s2n_test_basic_recv(sender, receiver));
238 EXPECT_OK(s2n_mem_test_assert_malloc_count(0));
239 }
240 }
241
242 /* Test: client receives large post-handshake messages (NewSessionTickets)
243 *
244 * There is no server version of this test because there are no large post-handshake messages
245 * valid for the server to accept.
246 */
247 for (size_t frag_i = 0; frag_i < s2n_array_len(fragment_sizes); frag_i++) {
248 uint32_t fragment_size = fragment_sizes[frag_i];
249
250 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
251 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
252 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
253 EXPECT_OK(s2n_test_init_sender_and_receiver(config, server, client, &io_pair));
254
255 /* Write NewSessionTicket message */
256 DEFER_CLEANUP(struct s2n_stuffer messages = { 0 }, s2n_stuffer_free);
257 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&messages, 0));
258 for (size_t i = 0; i < S2N_TEST_MESSAGE_COUNT; i++) {
259 server->tickets_to_send++;
260 EXPECT_OK(s2n_tls13_server_nst_write(server, &messages));
261 }
262
263 EXPECT_OK(s2n_test_send_records(server, messages, fragment_size));
264 EXPECT_OK(s2n_test_basic_recv(server, client));
265 EXPECT_EQUAL(tickets_count, S2N_TEST_MESSAGE_COUNT);
266
267 EXPECT_OK(s2n_test_send_records(server, messages, fragment_size));
268 EXPECT_OK(s2n_test_blocking_recv(server, client, &io_pair));
269 EXPECT_EQUAL(tickets_count, S2N_TEST_MESSAGE_COUNT);
270 }
271
272 /* Test: client receives large post-handshake messages of different sizes (NewSessionTickets)
273 *
274 * There is no server version of this test because there are no large post-handshake messages
275 * valid for the server to accept.
276 */
277 for (size_t frag_i = 0; frag_i < s2n_array_len(fragment_sizes); frag_i++) {
278 uint32_t fragment_size = fragment_sizes[frag_i];
279
280 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
281 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
282 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
283 EXPECT_OK(s2n_test_init_sender_and_receiver(config, server, client, &io_pair));
284 server->server_max_early_data_size = 10;
285
286 size_t total_size = 0;
287 DEFER_CLEANUP(struct s2n_stuffer messages = { 0 }, s2n_stuffer_free);
288 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&messages, 0));
289 for (size_t i = 0; i < 3; i++) {
290 /* Write a basic NewSessionTicket */
291 server->server_max_early_data_size_overridden = false;
292 server->tickets_to_send++;
293 EXPECT_OK(s2n_tls13_server_nst_write(server, &messages));
294 size_t min_length = s2n_stuffer_data_available(&messages) - total_size;
295 total_size += min_length;
296
297 /* Write a NewSesionTicket with early data enabled
298 * so that the early_data_indication extension is included
299 * and the message is therefore longer.
300 */
301 server->server_max_early_data_size_overridden = true;
302 server->tickets_to_send++;
303 EXPECT_OK(s2n_tls13_server_nst_write(server, &messages));
304 size_t max_length = s2n_stuffer_data_available(&messages) - total_size;
305 EXPECT_TRUE(max_length > min_length);
306 total_size += max_length;
307 }
308
309 EXPECT_OK(s2n_test_send_records(server, messages, fragment_size));
310 EXPECT_OK(s2n_test_basic_recv(server, client));
311 EXPECT_EQUAL(tickets_count, server->tickets_to_send);
312
313 EXPECT_OK(s2n_test_send_records(server, messages, fragment_size));
314 EXPECT_OK(s2n_test_blocking_recv(server, client, &io_pair));
315 EXPECT_EQUAL(tickets_count, server->tickets_to_send);
316 }
317
318 /* Test: server rejects known, invalid post-handshake messages (NewSessionTickets)
319 *
320 * There is no client version of this test because the client accepts all supported
321 * post-handshake messages.
322 */
323 for (size_t frag_i = 0; frag_i < s2n_array_len(fragment_sizes); frag_i++) {
324 uint32_t fragment_size = fragment_sizes[frag_i];
325
326 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
327 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
328 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
329 EXPECT_OK(s2n_test_init_sender_and_receiver(config, client, server, &io_pair));
330
331 /* Send NewSessionTicket message */
332 DEFER_CLEANUP(struct s2n_stuffer messages = { 0 }, s2n_stuffer_free);
333 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&messages, 0));
334 client->tickets_to_send = 1;
335 EXPECT_OK(s2n_tls13_server_nst_write(client, &messages));
336 EXPECT_OK(s2n_test_send_records(client, messages, fragment_size));
337
338 DEFER_CLEANUP(struct s2n_mem_test_cb_scope mem_ctx = { 0 }, s2n_mem_test_free_callbacks);
339 EXPECT_OK(s2n_mem_test_init_callbacks(&mem_ctx));
340 EXPECT_ERROR_WITH_ERRNO(s2n_test_basic_recv(client, server), S2N_ERR_BAD_MESSAGE);
341 EXPECT_EQUAL(tickets_count, 0);
342 EXPECT_OK(s2n_mem_test_assert_malloc_count(0));
343 }
344
345 /* Test: server rejects fragmented post-handshake message (KeyUpdate) with an invalid size
346 *
347 * This response is unique to the server because we want to prevent a malicious
348 * client from forcing the server to allocate large amounts of memory.
349 *
350 * While we could extend the same validation to the client, the client accepts
351 * a variable-sized message (NewSessionTicket) so can't really be protected.
352 */
353 {
354 /* This test is only interesting if the message is fragmented */
355 uint32_t fragment_size = 2;
356
357 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
358 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
359 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
360 EXPECT_OK(s2n_test_init_sender_and_receiver(config, client, server, &io_pair));
361
362 /* Write large KeyUpdate messages */
363 DEFER_CLEANUP(struct s2n_stuffer message = { 0 }, s2n_stuffer_free);
364 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&message, 0));
365 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&message, TLS_KEY_UPDATE));
366 EXPECT_SUCCESS(s2n_stuffer_write_uint24(&message, test_large_message_size));
367 EXPECT_SUCCESS(s2n_stuffer_skip_write(&message, test_large_message_size));
368 EXPECT_OK(s2n_test_send_records(client, message, fragment_size));
369 EXPECT_SUCCESS(s2n_update_application_traffic_keys(client, client->mode, SENDING));
370
371 DEFER_CLEANUP(struct s2n_mem_test_cb_scope mem_ctx = { 0 }, s2n_mem_test_free_callbacks);
372 EXPECT_OK(s2n_mem_test_init_callbacks(&mem_ctx));
373 EXPECT_ERROR_WITH_ERRNO(s2n_test_basic_recv(client, server), S2N_ERR_BAD_MESSAGE);
374
375 /* No post-handshake message should trigger the server to allocate memory */
376 EXPECT_OK(s2n_mem_test_assert_malloc_count(0));
377 };
378
379 /* Test: client receives empty post-handshake messages (HelloRequests)
380 *
381 * There is no server version of this test because there are no empty post-handshake messages
382 * valid for the server to accept.
383 */
384 for (size_t frag_i = 0; frag_i < s2n_array_len(fragment_sizes); frag_i++) {
385 uint32_t fragment_size = fragment_sizes[frag_i];
386
387 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
388 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
389 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
390 EXPECT_OK(s2n_test_init_sender_and_receiver(config, server, client, &io_pair));
391
392 /* HelloRequests are ignored if secure_renegotiation isn't set */
393 client->secure_renegotiation = true;
394
395 /* Write HelloRequest messages */
396 DEFER_CLEANUP(struct s2n_stuffer messages = { 0 }, s2n_stuffer_free);
397 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&messages, 0));
398 for (size_t i = 0; i < S2N_TEST_MESSAGE_COUNT; i++) {
399 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&messages, TLS_HELLO_REQUEST));
400 EXPECT_SUCCESS(s2n_stuffer_write_uint24(&messages, 0));
401 }
402
403 DEFER_CLEANUP(struct s2n_mem_test_cb_scope mem_ctx = { 0 }, s2n_mem_test_free_callbacks);
404 EXPECT_OK(s2n_mem_test_init_callbacks(&mem_ctx));
405
406 EXPECT_OK(s2n_test_send_records(server, messages, fragment_size));
407 EXPECT_OK(s2n_test_basic_recv(server, client));
408 EXPECT_EQUAL(hello_request_count, S2N_TEST_MESSAGE_COUNT);
409 EXPECT_OK(s2n_mem_test_assert_malloc_count(0));
410
411 EXPECT_OK(s2n_test_send_records(server, messages, fragment_size));
412 EXPECT_OK(s2n_test_blocking_recv(server, client, &io_pair));
413 EXPECT_EQUAL(hello_request_count, S2N_TEST_MESSAGE_COUNT);
414 EXPECT_OK(s2n_mem_test_assert_malloc_count(0));
415 }
416
417 /* Test: client and server reject known, invalid messages (ClientHellos) */
418 for (size_t frag_i = 0; frag_i < s2n_array_len(fragment_sizes); frag_i++) {
419 uint32_t fragment_size = fragment_sizes[frag_i];
420
421 for (size_t mode_i = 0; mode_i < s2n_array_len(modes); mode_i++) {
422 uint8_t mode = modes[mode_i];
423
424 DEFER_CLEANUP(struct s2n_connection *receiver = s2n_connection_new(mode), s2n_connection_ptr_free);
425 DEFER_CLEANUP(struct s2n_connection *sender = s2n_connection_new(S2N_PEER_MODE(mode)), s2n_connection_ptr_free);
426 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
427 EXPECT_OK(s2n_test_init_sender_and_receiver(config, sender, receiver, &io_pair));
428
429 /* Send fake ClientHello messages */
430 DEFER_CLEANUP(struct s2n_stuffer message = { 0 }, s2n_stuffer_free);
431 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&message, TLS_HANDSHAKE_HEADER_LENGTH));
432 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&message, TLS_CLIENT_HELLO));
433 EXPECT_SUCCESS(s2n_stuffer_write_uint24(&message, test_large_message_size));
434 EXPECT_SUCCESS(s2n_stuffer_skip_write(&message, test_large_message_size));
435 EXPECT_OK(s2n_test_send_records(sender, message, fragment_size));
436
437 DEFER_CLEANUP(struct s2n_mem_test_cb_scope mem_ctx = { 0 }, s2n_mem_test_free_callbacks);
438 EXPECT_OK(s2n_mem_test_init_callbacks(&mem_ctx));
439 EXPECT_ERROR_WITH_ERRNO(s2n_test_basic_recv(sender, receiver), S2N_ERR_BAD_MESSAGE);
440
441 /* No post-handshake message should trigger the server to allocate memory */
442 if (mode == S2N_SERVER) {
443 EXPECT_OK(s2n_mem_test_assert_malloc_count(0));
444 }
445 }
446 }
447
448 /* Test: client and server reject unknown messages */
449 for (size_t frag_i = 0; frag_i < s2n_array_len(fragment_sizes); frag_i++) {
450 uint32_t fragment_size = fragment_sizes[frag_i];
451
452 for (size_t mode_i = 0; mode_i < s2n_array_len(modes); mode_i++) {
453 uint8_t mode = modes[mode_i];
454
455 DEFER_CLEANUP(struct s2n_connection *receiver = s2n_connection_new(mode), s2n_connection_ptr_free);
456 DEFER_CLEANUP(struct s2n_connection *sender = s2n_connection_new(S2N_PEER_MODE(mode)), s2n_connection_ptr_free);
457 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
458 EXPECT_OK(s2n_test_init_sender_and_receiver(config, sender, receiver, &io_pair));
459
460 /* Send unknown message */
461 DEFER_CLEANUP(struct s2n_stuffer message = { 0 }, s2n_stuffer_free);
462 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&message, 0));
463 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&message, unknown_message_type));
464 EXPECT_SUCCESS(s2n_stuffer_write_uint24(&message, test_large_message_size));
465 EXPECT_SUCCESS(s2n_stuffer_skip_write(&message, test_large_message_size));
466 EXPECT_OK(s2n_test_send_records(sender, message, fragment_size));
467
468 DEFER_CLEANUP(struct s2n_mem_test_cb_scope mem_ctx = { 0 }, s2n_mem_test_free_callbacks);
469 EXPECT_OK(s2n_mem_test_init_callbacks(&mem_ctx));
470 EXPECT_ERROR_WITH_ERRNO(s2n_test_basic_recv(sender, receiver), S2N_ERR_BAD_MESSAGE);
471
472 /* No post-handshake message should trigger the server to allocate memory */
473 if (mode == S2N_SERVER) {
474 EXPECT_OK(s2n_mem_test_assert_malloc_count(0));
475 }
476 }
477 }
478
479 /**
480 *= https://www.rfc-editor.org/rfc/rfc8446#section-5.1
481 *= type=test
482 *# - Handshake messages MUST NOT be interleaved with other record
483 *# types. That is, if a handshake message is split over two or more
484 *# records, there MUST NOT be any other records between them.
485 */
486 for (size_t mode_i = 0; mode_i < s2n_array_len(modes); mode_i++) {
487 uint8_t mode = modes[mode_i];
488
489 /* This test is only interesting if the message is fragmented */
490 uint32_t fragment_size = 2;
491
492 DEFER_CLEANUP(struct s2n_connection *receiver = s2n_connection_new(mode), s2n_connection_ptr_free);
493 DEFER_CLEANUP(struct s2n_connection *sender = s2n_connection_new(S2N_PEER_MODE(mode)), s2n_connection_ptr_free);
494 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
495 EXPECT_OK(s2n_test_init_sender_and_receiver(config, sender, receiver, &io_pair));
496
497 /* Write a partial message */
498 DEFER_CLEANUP(struct s2n_stuffer message = { 0 }, s2n_stuffer_free);
499 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&message, 0));
500 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&message, TLS_KEY_UPDATE));
501 EXPECT_SUCCESS(s2n_stuffer_write_uint24(&message, S2N_KEY_UPDATE_LENGTH));
502 /* Don't write the actual message body -- we want the message to be incomplete */
503
504 /* Verify we can't receive the records: s2n_test_send_records does not send
505 * the complete handshake message, so we receive the application data sent by
506 * s2n_test_basic_recv in the middle of the handshake message.
507 */
508 EXPECT_OK(s2n_test_send_records(sender, message, fragment_size));
509 EXPECT_ERROR_WITH_ERRNO(s2n_test_basic_recv(sender, receiver), S2N_ERR_BAD_MESSAGE);
510 }
511
512 END_TEST();
513 }