Debian Codegraph @

crt/s2n/tests/unit/s2n_send_multirecord_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 <math.h>
17 #include <pthread.h>
18
19 #include "api/s2n.h"
20 #include "s2n_test.h"
21 #include "testlib/s2n_testlib.h"
22 #include "tls/s2n_post_handshake.h"
23 #include "tls/s2n_record.h"
24 #include "tls/s2n_tls.h"
25 #include "utils/s2n_random.h"
26
27 /* clang-format off */
28 #define CLOSED_SEND_RESULT { .result = -1, .error = EPIPE }
29 #define BLOCK_SEND_RESULT { .result = -1, .error = EAGAIN }
30 #define PARTIAL_SEND_RESULT(bytes) { .result = bytes, .error = EAGAIN }
31 #define EXPECTED_SEND_RESULT(bytes) { .result = bytes, .assert_result = true }
32 #define OK_SEND_RESULT { .result = INT_MAX }
33 /* clang-format on */
34
35 int s2n_check_record_limit(struct s2n_connection *conn, struct s2n_blob *sequence_number);
36 bool s2n_should_flush(struct s2n_connection *conn, ssize_t total_message_size);
37
38 struct s2n_send_result {
39 int result;
40 int error;
41 bool assert_result;
42 };
43
44 struct s2n_send_context {
45 size_t calls;
46 size_t bytes_sent;
47 const struct s2n_send_result *results;
48 const size_t results_len;
49 };
50
51 static int s2n_test_send_cb(void *io_context, const uint8_t *buf, uint32_t len)
52 {
53 struct s2n_send_context *context = (struct s2n_send_context *) io_context;
54 POSIX_ENSURE_REF(context);
55
56 POSIX_ENSURE_LT(context->calls, context->results_len);
57 const struct s2n_send_result *result = &context->results[context->calls];
58
59 int retval = S2N_MIN((int) len, result->result);
60 if (result->assert_result) {
61 POSIX_ENSURE_EQ(retval, len);
62 }
63
64 context->calls++;
65 if (retval > 0) {
66 context->bytes_sent += retval;
67 }
68
69 errno = result->error;
70 return retval;
71 }
72
73 int main(int argc, char **argv)
74 {
75 BEGIN_TEST();
76
77 uint8_t test_data[] = "hello world";
78
79 uint8_t large_test_data[S2N_TLS_MAXIMUM_FRAGMENT_LENGTH + 10] = { 0 };
80 struct s2n_blob large_data_blob = { 0 };
81 EXPECT_SUCCESS(s2n_blob_init(&large_data_blob, large_test_data, sizeof(large_test_data)));
82 EXPECT_OK(s2n_get_public_random_data(&large_data_blob));
83
84 /* Small record sizes will require a LOT of calls to s2n_send.
85 * Use this context when they should all succeed.
86 */
87 struct s2n_send_result results_all_ok[50] = { 0 };
88 for (size_t i = 0; i < s2n_array_len(results_all_ok); i++) {
89 results_all_ok[i] = (struct s2n_send_result) OK_SEND_RESULT;
90 }
91 const struct s2n_send_context context_all_ok = {
92 .results = results_all_ok,
93 .results_len = s2n_array_len(results_all_ok)
94 };
95
96 /* Setup a large output buffer that can contain all of large_test_data */
97 const size_t buffer_size = 20000;
98 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
99 EXPECT_NOT_NULL(config);
100 EXPECT_SUCCESS(s2n_config_set_send_buffer_size(config, buffer_size));
101
102 /* Setup an output buffer that is slightly too small for large_test_data */
103 const uint32_t smaller_buffer_size = 17300;
104 DEFER_CLEANUP(struct s2n_config *smaller_buffer_config = s2n_config_new(), s2n_config_ptr_free);
105 EXPECT_NOT_NULL(smaller_buffer_config);
106 EXPECT_SUCCESS(s2n_config_set_send_buffer_size(smaller_buffer_config, smaller_buffer_size));
107
108 /* Test s2n_should_flush */
109 {
110 /* Flush if multirecord send not enabled */
111 {
112 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
113 s2n_connection_ptr_free);
114
115 /* Multirecord send not enabled */
116 EXPECT_FALSE(conn->multirecord_send);
117 EXPECT_TRUE(s2n_should_flush(conn, buffer_size));
118
119 /* Multirecord send enabled */
120 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
121 EXPECT_TRUE(conn->multirecord_send);
122 EXPECT_FALSE(s2n_should_flush(conn, buffer_size));
123 };
124
125 /* Flush if all data sent */
126 {
127 ssize_t send_size = 100;
128
129 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
130 s2n_connection_ptr_free);
131 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
132
133 /* No data sent */
134 EXPECT_FALSE(s2n_should_flush(conn, send_size));
135
136 /* Some data sent */
137 conn->current_user_data_consumed = send_size / 2;
138 EXPECT_FALSE(s2n_should_flush(conn, send_size));
139
140 /* All data sent */
141 conn->current_user_data_consumed = send_size;
142 EXPECT_TRUE(s2n_should_flush(conn, send_size));
143 };
144
145 /* Flush if buffer can't hold max size record */
146 {
147 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
148 s2n_connection_ptr_free);
149 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
150
151 /* Uninitialized buffer */
152 EXPECT_FALSE(s2n_should_flush(conn, buffer_size));
153
154 /* Empty buffer */
155 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&conn->out, buffer_size));
156 EXPECT_FALSE(s2n_should_flush(conn, buffer_size));
157
158 /* Buffer not empty, but sufficient space remains */
159 size_t max_record_size = S2N_TLS_MAX_RECORD_LEN_FOR(conn->max_outgoing_fragment_length);
160 EXPECT_SUCCESS(s2n_stuffer_skip_write(&conn->out, buffer_size - max_record_size));
161 EXPECT_FALSE(s2n_should_flush(conn, buffer_size));
162
163 /* Insufficient space in buffer */
164 EXPECT_SUCCESS(s2n_stuffer_skip_write(&conn->out, 1));
165 EXPECT_TRUE(s2n_should_flush(conn, buffer_size));
166 };
167 };
168
169 /* Total data fits in a single record.
170 * Equivalent to not using multirecord.
171 */
172 {
173 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
174 s2n_connection_ptr_free);
175 EXPECT_NOT_NULL(conn);
176 EXPECT_OK(s2n_connection_set_secrets(conn));
177 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
178
179 struct s2n_send_context context = context_all_ok;
180 EXPECT_SUCCESS(s2n_connection_set_send_cb(conn, s2n_test_send_cb));
181 EXPECT_SUCCESS(s2n_connection_set_send_ctx(conn, (void *) &context));
182
183 s2n_blocked_status blocked = 0;
184 EXPECT_EQUAL(s2n_send(conn, test_data, sizeof(test_data), &blocked), sizeof(test_data));
185
186 EXPECT_EQUAL(context.calls, 1);
187 EXPECT_EQUAL(context.bytes_sent, conn->wire_bytes_out);
188 EXPECT_TRUE(context.bytes_sent > sizeof(test_data));
189
190 /* Verify output buffer */
191 EXPECT_EQUAL(conn->out.blob.size, buffer_size);
192 };
193
194 /* Send buffer was configured too small for even a single record.
195 * Send smaller records.
196 *
197 * The minimum buffer size we allow generates a fragment size of 5, to prevent
198 * fragmenting KeyUpdate messages, which are always 5 bytes. At this minimum size,
199 * application data is also fragmented into 5 byte chunks, which is pretty silly,
200 * but is an edge case.
201 */
202 {
203 DEFER_CLEANUP(struct s2n_config *min_buffer_config = s2n_config_new(), s2n_config_ptr_free);
204 EXPECT_NOT_NULL(min_buffer_config);
205 EXPECT_SUCCESS(s2n_config_set_send_buffer_size(min_buffer_config, S2N_MIN_SEND_BUFFER_SIZE));
206
207 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
208 s2n_connection_ptr_free);
209 EXPECT_NOT_NULL(conn);
210 EXPECT_OK(s2n_connection_set_secrets(conn));
211 EXPECT_SUCCESS(s2n_connection_set_config(conn, min_buffer_config));
212
213 struct s2n_send_context context = context_all_ok;
214 EXPECT_SUCCESS(s2n_connection_set_send_cb(conn, s2n_test_send_cb));
215 EXPECT_SUCCESS(s2n_connection_set_send_ctx(conn, (void *) &context));
216
217 ssize_t send_size = sizeof(test_data);
218 s2n_blocked_status blocked = 0;
219 EXPECT_EQUAL(s2n_send(conn, test_data, send_size, &blocked), send_size);
220
221 /* Since each record only contains two bytes of payload,
222 * we need to send a number of records equal to our total send ceil(size / 2).
223 */
224 uint8_t remainder = (send_size % S2N_MIN_SEND_BUFFER_FRAGMENT_SIZE) ? 1 : 0;
225 EXPECT_EQUAL(context.calls, (send_size / S2N_MIN_SEND_BUFFER_FRAGMENT_SIZE) + remainder);
226 EXPECT_EQUAL(context.bytes_sent, conn->wire_bytes_out);
227 EXPECT_TRUE(context.bytes_sent > send_size);
228
229 /* Verify output buffer */
230 EXPECT_EQUAL(conn->out.blob.size, S2N_MIN_SEND_BUFFER_SIZE);
231 };
232
233 /* Total data fits in multiple records.
234 * Without multirecord, this would result in multiple calls to send.
235 */
236 uint16_t large_test_data_send_size = 0;
237 {
238 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
239 s2n_connection_ptr_free);
240 EXPECT_NOT_NULL(conn);
241 EXPECT_OK(s2n_connection_set_secrets(conn));
242 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
243
244 struct s2n_send_context context = context_all_ok;
245 EXPECT_SUCCESS(s2n_connection_set_send_cb(conn, s2n_test_send_cb));
246 EXPECT_SUCCESS(s2n_connection_set_send_ctx(conn, (void *) &context));
247
248 s2n_blocked_status blocked = 0;
249 EXPECT_EQUAL(s2n_send(conn, large_test_data, sizeof(large_test_data), &blocked), sizeof(large_test_data));
250
251 EXPECT_EQUAL(context.calls, 1);
252 EXPECT_EQUAL(context.bytes_sent, conn->wire_bytes_out);
253 EXPECT_TRUE(context.bytes_sent > sizeof(large_test_data));
254 large_test_data_send_size = context.bytes_sent;
255
256 /* Verify output buffer */
257 EXPECT_EQUAL(conn->out.blob.size, buffer_size);
258 };
259
260 /* Total data with multiple records too large for the send buffer.
261 * Call send multiple times.
262 */
263 {
264 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
265 s2n_connection_ptr_free);
266 EXPECT_NOT_NULL(conn);
267 EXPECT_OK(s2n_connection_set_secrets(conn));
268 EXPECT_SUCCESS(s2n_connection_set_config(conn, smaller_buffer_config));
269
270 struct s2n_send_context context = context_all_ok;
271 EXPECT_SUCCESS(s2n_connection_set_send_cb(conn, s2n_test_send_cb));
272 EXPECT_SUCCESS(s2n_connection_set_send_ctx(conn, (void *) &context));
273
274 s2n_blocked_status blocked = 0;
275 EXPECT_EQUAL(s2n_send(conn, large_test_data, sizeof(large_test_data), &blocked), sizeof(large_test_data));
276
277 EXPECT_EQUAL(context.calls, 2);
278 EXPECT_EQUAL(context.bytes_sent, conn->wire_bytes_out);
279 /* Even though it took more send calls,
280 * we still sent the same number of records with the same overhead.
281 */
282 EXPECT_EQUAL(context.bytes_sent, large_test_data_send_size);
283
284 /* Verify output buffer */
285 EXPECT_EQUAL(conn->out.blob.size, smaller_buffer_size);
286 };
287
288 /* Block while buffering multiple records.
289 * Send blocks until all buffered data is sent.
290 */
291 {
292 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
293 s2n_connection_ptr_free);
294 EXPECT_NOT_NULL(conn);
295 EXPECT_OK(s2n_connection_set_secrets(conn));
296 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
297
298 const uint32_t partial_send = 10;
299 const uint32_t at_least_one_record = large_test_data_send_size - partial_send - 1;
300 const struct s2n_send_result results[] = {
301 /* First send writes less than one record before blocking */
302 PARTIAL_SEND_RESULT(partial_send), BLOCK_SEND_RESULT,
303 /* Second send writes at least one record before blocking */
304 PARTIAL_SEND_RESULT(at_least_one_record), BLOCK_SEND_RESULT,
305 /* Third send completes */
306 OK_SEND_RESULT
307 };
308 struct s2n_send_context context = { .results = results, .results_len = s2n_array_len(results) };
309 EXPECT_SUCCESS(s2n_connection_set_send_cb(conn, s2n_test_send_cb));
310 EXPECT_SUCCESS(s2n_connection_set_send_ctx(conn, (void *) &context));
311
312 s2n_blocked_status blocked = 0;
313
314 EXPECT_FAILURE_WITH_ERRNO(s2n_send(conn, large_test_data, sizeof(large_test_data), &blocked), S2N_ERR_IO_BLOCKED);
315 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE);
316 EXPECT_EQUAL(context.bytes_sent, partial_send);
317
318 /* Unlike when we buffer a single record at a time, s2n_send does not report each fragment / record flushed.
319 * Instead, it won't report any data as sent until all buffered data is flushed.
320 */
321 EXPECT_FAILURE_WITH_ERRNO(s2n_send(conn, large_test_data, sizeof(large_test_data), &blocked), S2N_ERR_IO_BLOCKED);
322 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE);
323 EXPECT_EQUAL(context.bytes_sent, at_least_one_record + partial_send);
324
325 EXPECT_EQUAL(s2n_send(conn, large_test_data, sizeof(large_test_data), &blocked), sizeof(large_test_data));
326 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
327 EXPECT_EQUAL(context.bytes_sent, large_test_data_send_size);
328
329 /* Verify output buffer */
330 EXPECT_EQUAL(conn->out.blob.size, buffer_size);
331 };
332
333 /* Block while buffering multiple records across multiple send calls.
334 * Each send blocks until all buffered data is flushed.
335 */
336 {
337 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
338 s2n_connection_ptr_free);
339 EXPECT_NOT_NULL(conn);
340 EXPECT_OK(s2n_connection_set_secrets(conn));
341 EXPECT_SUCCESS(s2n_connection_set_config(conn, smaller_buffer_config));
342
343 const uint32_t partial_send = 10;
344 const uint32_t at_least_one_flush = smaller_buffer_size - partial_send;
345 const struct s2n_send_result results[] = {
346 /* First send writes less than one record before blocking */
347 PARTIAL_SEND_RESULT(partial_send), BLOCK_SEND_RESULT,
348 /* Second send flushes the output buffer before blocking */
349 PARTIAL_SEND_RESULT(at_least_one_flush), BLOCK_SEND_RESULT,
350 /* Third send completes */
351 OK_SEND_RESULT
352 };
353 struct s2n_send_context context = { .results = results, .results_len = s2n_array_len(results) };
354 EXPECT_SUCCESS(s2n_connection_set_send_cb(conn, s2n_test_send_cb));
355 EXPECT_SUCCESS(s2n_connection_set_send_ctx(conn, (void *) &context));
356
357 s2n_blocked_status blocked = 0;
358
359 EXPECT_FAILURE_WITH_ERRNO(s2n_send(conn, large_test_data, sizeof(large_test_data), &blocked), S2N_ERR_IO_BLOCKED);
360 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE);
361 EXPECT_EQUAL(context.bytes_sent, partial_send);
362
363 /* Write the buffer, which contains two records. */
364 ssize_t expected_sent = S2N_DEFAULT_FRAGMENT_LENGTH * 2;
365 EXPECT_EQUAL(s2n_send(conn, large_test_data, sizeof(large_test_data), &blocked), expected_sent);
366 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE);
367
368 size_t offset = expected_sent;
369 expected_sent = sizeof(large_test_data) - offset;
370 EXPECT_EQUAL(s2n_send(conn, large_test_data + offset, sizeof(large_test_data) - offset, &blocked), expected_sent);
371 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
372 EXPECT_EQUAL(context.bytes_sent, large_test_data_send_size);
373
374 /* Verify output buffer */
375 EXPECT_EQUAL(conn->out.blob.size, smaller_buffer_size);
376 };
377
378 /* Send a post-handshake message when records are buffered.
379 *
380 * We only test the KeyUpdate post-handshake message. That can trigger
381 * part way through a call to s2n_send if the encryption limit is reached.
382 *
383 * We can't reliably test NewSessionTicket post-handshake messages.
384 * Those could only trigger if an application called s2n_connection_add_new_tickets_to_send
385 * part way through a call to s2n_send, which requires calling from another thread
386 * at just the right (wrong?) time.
387 */
388 {
389 s2n_blocked_status blocked = 0;
390
391 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
392 s2n_connection_ptr_free);
393 EXPECT_NOT_NULL(conn);
394 EXPECT_OK(s2n_connection_set_secrets(conn));
395 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
396 EXPECT_SUCCESS(s2n_connection_set_send_cb(conn, s2n_test_send_cb));
397
398 /* Find size of a KeyUpdate record */
399 size_t key_update_size = 0;
400 {
401 struct s2n_send_context context = context_all_ok;
402 EXPECT_SUCCESS(s2n_connection_set_send_ctx(conn, (void *) &context));
403
404 s2n_atomic_flag_set(&conn->key_update_pending);
405 EXPECT_SUCCESS(s2n_post_handshake_send(conn, &blocked));
406 EXPECT_FALSE(s2n_atomic_flag_test(&conn->key_update_pending));
407 key_update_size = context.bytes_sent;
408 };
409 EXPECT_TRUE(key_update_size > 0);
410
411 const struct s2n_send_result results[] = {
412 /* We expect the buffer to be flushed before the post handshake message */
413 OK_SEND_RESULT,
414 /* We expect the buffer to be flushed again after the post handshake message */
415 EXPECTED_SEND_RESULT(key_update_size),
416 OK_SEND_RESULT
417 };
418 struct s2n_send_context context = { .results = results, .results_len = s2n_array_len(results) };
419 EXPECT_SUCCESS(s2n_connection_set_send_ctx(conn, (void *) &context));
420
421 /* Find record limit */
422 uint64_t limit = conn->secure->cipher_suite->record_alg->encryption_limit;
423 EXPECT_TRUE(limit > 0);
424
425 /* Initialize sequence number */
426 struct s2n_blob seq_num_blob = { 0 };
427 struct s2n_stuffer seq_num_stuffer = { 0 };
428 EXPECT_SUCCESS(s2n_blob_init(&seq_num_blob, conn->secure->client_sequence_number, S2N_TLS_SEQUENCE_NUM_LEN));
429 EXPECT_SUCCESS(s2n_stuffer_init(&seq_num_stuffer, &seq_num_blob));
430
431 /* Set the sequence number so that a KeyUpdate triggers after one more record. */
432 uint64_t initial_seq_num = limit - 1;
433 EXPECT_SUCCESS(s2n_stuffer_write_uint64(&seq_num_stuffer, initial_seq_num));
434 EXPECT_SUCCESS(s2n_check_record_limit(conn, &seq_num_blob));
435 EXPECT_FALSE(s2n_atomic_flag_test(&conn->key_update_pending));
436
437 /* Send */
438 EXPECT_EQUAL(s2n_send(conn, large_test_data, sizeof(large_test_data), &blocked), sizeof(large_test_data));
439
440 /* Verify KeyUpdate happened: the sequence number was reset */
441 uint64_t final_seq_num = 0;
442 EXPECT_SUCCESS(s2n_stuffer_read_uint64(&seq_num_stuffer, &final_seq_num));
443 EXPECT_TRUE(final_seq_num < initial_seq_num);
444
445 /* Verify expected send behavior */
446 size_t expected_calls = 1 /* first record */ + 1 /* KeyUpdate */ + 1 /* remaining records */;
447 EXPECT_EQUAL(context.calls, expected_calls);
448 EXPECT_EQUAL(context.bytes_sent, large_test_data_send_size + key_update_size);
449
450 /* Verify output buffer */
451 EXPECT_EQUAL(conn->out.blob.size, buffer_size);
452 };
453
454 /* Test: Alert records are not buffered with ApplicationData records
455 *
456 * We flush before sending an alert, even if there is sufficient
457 * space for the alert record in the send buffer.
458 *
459 * If this behavior changed, then s2n_should_flush would need to consider
460 * the size of a possible alert.
461 */
462 {
463 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
464 s2n_connection_ptr_free);
465 EXPECT_NOT_NULL(conn);
466 EXPECT_OK(s2n_connection_set_secrets(conn));
467 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
468
469 struct s2n_send_context context = context_all_ok;
470 EXPECT_SUCCESS(s2n_connection_set_send_ctx(conn, (void *) &context));
471 EXPECT_SUCCESS(s2n_connection_set_send_cb(conn, s2n_test_send_cb));
472
473 const uint32_t send_size = 10;
474 const uint32_t max_app_data_record_size = S2N_TLS_MAX_RECORD_LEN_FOR(send_size);
475 const uint32_t max_alert_record_size = S2N_TLS_MAX_RECORD_LEN_FOR(S2N_ALERT_LENGTH);
476 const uint32_t min_send_buffer_size = max_app_data_record_size + max_alert_record_size;
477 EXPECT_TRUE(min_send_buffer_size <= buffer_size);
478
479 /* Queue the alert */
480 EXPECT_OK(s2n_queue_reader_no_renegotiation_alert(conn));
481
482 /* Send the Application Data and Alert */
483 s2n_blocked_status blocked = 0;
484 EXPECT_EQUAL(s2n_send(conn, large_test_data, send_size, &blocked), send_size);
485
486 /* We expect two separate send calls: one for the application data record
487 * and one for the alert record.
488 */
489 EXPECT_EQUAL(context.calls, 2);
490
491 /* We expect that the output buffer never contained all data sent,
492 * since that data was split between two records.
493 */
494 EXPECT_TRUE(conn->out.high_water_mark < context.bytes_sent);
495 };
496
497 /* Send a post-handshake message when records are buffered, and IO blocks */
498 {
499 s2n_blocked_status blocked = 0;
500
501 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
502 s2n_connection_ptr_free);
503 EXPECT_NOT_NULL(conn);
504 EXPECT_OK(s2n_connection_set_secrets(conn));
505 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
506 EXPECT_SUCCESS(s2n_connection_set_send_cb(conn, s2n_test_send_cb));
507
508 /* Find size of a KeyUpdate record */
509 size_t key_update_size = 0;
510 {
511 struct s2n_send_context context = context_all_ok;
512 EXPECT_SUCCESS(s2n_connection_set_send_ctx(conn, (void *) &context));
513
514 s2n_atomic_flag_set(&conn->key_update_pending);
515 EXPECT_SUCCESS(s2n_post_handshake_send(conn, &blocked));
516 EXPECT_FALSE(s2n_atomic_flag_test(&conn->key_update_pending));
517 key_update_size = context.bytes_sent;
518 }
519 EXPECT_TRUE(key_update_size > 0);
520
521 /* Block the first two calls to send, only allowing the third to succeed. */
522 const struct s2n_send_result results[] = {
523 /* Initial ApplicationData records */
524 BLOCK_SEND_RESULT,
525 BLOCK_SEND_RESULT,
526 OK_SEND_RESULT,
527 /* KeyUpdate record */
528 BLOCK_SEND_RESULT,
529 BLOCK_SEND_RESULT,
530 EXPECTED_SEND_RESULT(key_update_size),
531 /* Remaining ApplicationData records */
532 BLOCK_SEND_RESULT,
533 BLOCK_SEND_RESULT,
534 OK_SEND_RESULT,
535 };
536 struct s2n_send_context context = { .results = results, .results_len = s2n_array_len(results) };
537 EXPECT_SUCCESS(s2n_connection_set_send_ctx(conn, (void *) &context));
538
539 /* Find record limit */
540 uint64_t limit = conn->secure->cipher_suite->record_alg->encryption_limit;
541 EXPECT_TRUE(limit > 0);
542
543 /* Initialize sequence number */
544 struct s2n_blob seq_num_blob = { 0 };
545 struct s2n_stuffer seq_num_stuffer = { 0 };
546 EXPECT_SUCCESS(s2n_blob_init(&seq_num_blob, conn->secure->client_sequence_number, S2N_TLS_SEQUENCE_NUM_LEN));
547 EXPECT_SUCCESS(s2n_stuffer_init(&seq_num_stuffer, &seq_num_blob));
548
549 /* Set the sequence number so that a KeyUpdate triggers after one more record. */
550 uint64_t initial_seq_num = limit - 1;
551 EXPECT_SUCCESS(s2n_stuffer_write_uint64(&seq_num_stuffer, initial_seq_num));
552 EXPECT_SUCCESS(s2n_check_record_limit(conn, &seq_num_blob));
553 EXPECT_FALSE(s2n_atomic_flag_test(&conn->key_update_pending));
554
555 /* Send until all data written */
556 size_t total = 0;
557 while (total < sizeof(large_test_data)) {
558 ssize_t sent = s2n_send(conn, large_test_data + total, sizeof(large_test_data) - total, &blocked);
559 if (sent >= S2N_SUCCESS) {
560 total += sent;
561 } else {
562 EXPECT_EQUAL(s2n_errno, S2N_ERR_IO_BLOCKED);
563 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE);
564 }
565 }
566
567 /* Verify KeyUpdate happened: the sequence number was reset */
568 uint64_t final_seq_num = 0;
569 EXPECT_SUCCESS(s2n_stuffer_read_uint64(&seq_num_stuffer, &final_seq_num));
570 EXPECT_TRUE(final_seq_num < initial_seq_num);
571
572 /* Verify expected send behavior */
573 size_t expected_calls = s2n_array_len(results);
574 EXPECT_EQUAL(context.calls, expected_calls);
575 EXPECT_EQUAL(context.bytes_sent, large_test_data_send_size + key_update_size);
576
577 /* Verify output buffer */
578 EXPECT_EQUAL(conn->out.blob.size, buffer_size);
579 };
580
581 END_TEST();
582 }