File: s2n_key_update_test.c

package info (click to toggle)
aws-crt-python 0.16.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 78,328 kB
  • sloc: ansic: 330,743; python: 18,949; makefile: 6,271; sh: 3,712; asm: 754; cpp: 699; ruby: 208; java: 77; perl: 73; javascript: 46; xml: 11
file content (517 lines) | stat: -rw-r--r-- 23,522 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

#include "tls/s2n_key_update.h"

#include "crypto/s2n_sequence.h"
#include "s2n_test.h"
#include "testlib/s2n_testlib.h"
#include "tls/s2n_cipher_suites.h"
#include "tls/s2n_connection.h"
#include "tls/s2n_post_handshake.h"
#include "tls/s2n_quic_support.h"
#include "tls/s2n_tls13_handshake.h"
#include "utils/s2n_blob.h"
#include "utils/s2n_safety.h"

#define LOWEST_BYTE (S2N_TLS_SEQUENCE_NUM_LEN - 1)

int s2n_key_update_write(struct s2n_blob *out);
int s2n_check_record_limit(struct s2n_connection *conn, struct s2n_blob *sequence_number);

static S2N_RESULT s2n_write_uint64(uint64_t input, uint8_t *output)
{
    struct s2n_blob blob = { 0 };
    struct s2n_stuffer stuffer = { 0 };
    EXPECT_SUCCESS(s2n_blob_init(&blob, output, S2N_TLS_SEQUENCE_NUM_LEN));
    EXPECT_SUCCESS(s2n_stuffer_init(&stuffer, &blob));
    EXPECT_SUCCESS(s2n_stuffer_write_uint64(&stuffer, input));
    return S2N_RESULT_OK;
}

int main(int argc, char **argv)
{
    BEGIN_TEST();
    EXPECT_SUCCESS(s2n_disable_tls13_in_test());

    S2N_BLOB_FROM_HEX(application_secret,
            "4bc28934ddd802b00f479e14a72d7725dab45d32b3b145f29"
            "e4c5b56677560eb5236b168c71c5c75aa52f3e20ee89bfb");

    struct s2n_cipher_suite *cipher_suite_with_limit = &s2n_tls13_aes_256_gcm_sha384;
    const uint64_t record_limit = cipher_suite_with_limit->record_alg->encryption_limit;
    struct s2n_cipher_suite *cipher_suite_without_limit = &s2n_tls13_chacha20_poly1305_sha256;

    /* We can use a TLS1.2 cipher suite if chacha20 isn't available,
     * since no TLS1.2 cipher suites have record limits.
     */
    if (!cipher_suite_without_limit->available) {
        cipher_suite_without_limit = &s2n_ecdhe_ecdsa_with_aes_256_gcm_sha384;
    }

    EXPECT_TRUE(cipher_suite_with_limit->available);
    EXPECT_TRUE(cipher_suite_without_limit->available);

    uint8_t zeroed_sequence_number[S2N_TLS_SEQUENCE_NUM_LEN] = { 0 };

    /* s2n_key_update_write */
    {
        /* Tests s2n_key_update_write writes as expected */
        {
            uint8_t key_update_data[S2N_KEY_UPDATE_MESSAGE_SIZE];
            struct s2n_blob key_update_blob = { 0 };
            struct s2n_stuffer key_update_stuffer = { 0 };
            EXPECT_SUCCESS(s2n_blob_init(&key_update_blob, key_update_data, sizeof(key_update_data)));
            EXPECT_SUCCESS(s2n_stuffer_init(&key_update_stuffer, &key_update_blob));

            /* Write key update message */
            EXPECT_SUCCESS(s2n_key_update_write(&key_update_blob));

            /* Move stuffer write cursor to correct position */
            EXPECT_SUCCESS(s2n_stuffer_skip_write(&key_update_stuffer, S2N_KEY_UPDATE_MESSAGE_SIZE));

            uint8_t post_handshake_id;
            EXPECT_SUCCESS(s2n_stuffer_read_uint8(&key_update_stuffer, &post_handshake_id));
            EXPECT_EQUAL(post_handshake_id, TLS_KEY_UPDATE);

            uint32_t request_length;
            EXPECT_SUCCESS(s2n_stuffer_read_uint24(&key_update_stuffer, &request_length));
            EXPECT_EQUAL(request_length, S2N_KEY_UPDATE_LENGTH);

            uint8_t key_update_request;
            EXPECT_SUCCESS(s2n_stuffer_read_uint8(&key_update_stuffer, &key_update_request));
            EXPECT_EQUAL(key_update_request, S2N_KEY_UPDATE_NOT_REQUESTED);
        };
    };

    /* s2n_key_update_recv */
    {
        /* Key update message not allowed when running with QUIC
         *
         *= https://tools.ietf.org/rfc/rfc9001.txt#6
         *= type=test
         *# Endpoints MUST treat the receipt of a TLS KeyUpdate message as a connection error
         *# of type 0x010a, equivalent to a fatal TLS alert of unexpected_message
         **/
        {
            const size_t test_data_len = 10;
            DEFER_CLEANUP(struct s2n_stuffer input, s2n_stuffer_free);
            EXPECT_SUCCESS(s2n_stuffer_alloc(&input, test_data_len));
            EXPECT_SUCCESS(s2n_stuffer_skip_write(&input, test_data_len));

            struct s2n_config *quic_config;
            EXPECT_NOT_NULL(quic_config = s2n_config_new());
            EXPECT_SUCCESS(s2n_config_enable_quic(quic_config));

            struct s2n_connection *conn;
            EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
            EXPECT_SUCCESS(s2n_connection_set_config(conn, quic_config));

            EXPECT_FAILURE_WITH_ALERT(s2n_key_update_recv(conn, &input),
                    S2N_ERR_BAD_MESSAGE, S2N_TLS_ALERT_UNEXPECTED_MESSAGE);

            /* Verify method was a no-op and the message was not read */
            EXPECT_EQUAL(s2n_stuffer_data_available(&input), test_data_len);

            EXPECT_SUCCESS(s2n_connection_free(conn));
            EXPECT_SUCCESS(s2n_config_free(quic_config));
        };

        /* Key update message not allowed in TLS1.2 */
        {
            const size_t test_data_len = 10;
            DEFER_CLEANUP(struct s2n_stuffer input, s2n_stuffer_free);
            EXPECT_SUCCESS(s2n_stuffer_alloc(&input, test_data_len));
            EXPECT_SUCCESS(s2n_stuffer_skip_write(&input, test_data_len));

            struct s2n_connection *conn;
            EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
            conn->actual_protocol_version = S2N_TLS12;

            EXPECT_FAILURE_WITH_ERRNO(s2n_key_update_recv(conn, &input), S2N_ERR_BAD_MESSAGE);

            /* Verify method was a no-op and the message was not read */
            EXPECT_EQUAL(s2n_stuffer_data_available(&input), test_data_len);

            EXPECT_SUCCESS(s2n_connection_free(conn));
        };

        /* Key update message received contains invalid key update request */
        {
            DEFER_CLEANUP(struct s2n_stuffer input, s2n_stuffer_free);
            EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));

            struct s2n_connection *conn;
            EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
            /* Write invalid value for key update request type */
            EXPECT_SUCCESS(s2n_stuffer_write_uint8(&input, -1));

            EXPECT_FAILURE_WITH_ERRNO(s2n_key_update_recv(conn, &input), S2N_ERR_BAD_MESSAGE);

            EXPECT_SUCCESS(s2n_connection_free(conn));
        };

        /* Server receives valid key update request */
        {
            DEFER_CLEANUP(struct s2n_stuffer input, s2n_stuffer_free);
            EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));

            struct s2n_connection *server_conn;
            EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
            server_conn->actual_protocol_version = S2N_TLS13;
            server_conn->secure->cipher_suite = cipher_suite_with_limit;
            POSIX_CHECKED_MEMCPY(server_conn->secrets.tls13.client_app_secret, application_secret.data, application_secret.size);

            server_conn->secure->client_sequence_number[0] = 1;
            /* Write the key update request to the correct stuffer */
            EXPECT_SUCCESS(s2n_stuffer_write_uint8(&input, S2N_KEY_UPDATE_NOT_REQUESTED));

            EXPECT_SUCCESS(s2n_key_update_recv(server_conn, &input));
            EXPECT_EQUAL(server_conn->secure->client_sequence_number[0], 0);
            EXPECT_EQUAL(server_conn->key_update_pending, S2N_KEY_UPDATE_NOT_REQUESTED);

            EXPECT_SUCCESS(s2n_connection_free(server_conn));
        };

        /* Client receives valid key update request */
        {
            DEFER_CLEANUP(struct s2n_stuffer input, s2n_stuffer_free);
            EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));

            struct s2n_connection *client_conn;
            EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
            client_conn->actual_protocol_version = S2N_TLS13;
            client_conn->secure->cipher_suite = cipher_suite_with_limit;
            POSIX_CHECKED_MEMCPY(client_conn->secrets.tls13.server_app_secret, application_secret.data, application_secret.size);

            client_conn->secure->server_sequence_number[0] = 1;
            /* Write the key update request to the correct stuffer */
            EXPECT_SUCCESS(s2n_stuffer_write_uint8(&input, S2N_KEY_UPDATE_NOT_REQUESTED));

            EXPECT_SUCCESS(s2n_key_update_recv(client_conn, &input));
            EXPECT_EQUAL(client_conn->secure->server_sequence_number[0], 0);
            EXPECT_EQUAL(client_conn->key_update_pending, S2N_KEY_UPDATE_NOT_REQUESTED);

            EXPECT_SUCCESS(s2n_connection_free(client_conn));
        };
    };

    /* s2n_key_update_send */
    {
        /* Key update has been requested */
        {
            struct s2n_connection *client_conn;
            EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
            client_conn->actual_protocol_version = S2N_TLS13;
            client_conn->secure->cipher_suite = cipher_suite_with_limit;
            POSIX_CHECKED_MEMCPY(client_conn->secrets.tls13.client_app_secret, application_secret.data, application_secret.size);

            /* Setup io */
            struct s2n_stuffer stuffer;
            EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0));
            EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&stuffer, &stuffer, client_conn));

            client_conn->key_update_pending = true;

            s2n_blocked_status blocked = 0;
            EXPECT_SUCCESS(s2n_key_update_send(client_conn, &blocked));

            EXPECT_EQUAL(client_conn->key_update_pending, false);
            EXPECT_BYTEARRAY_EQUAL(client_conn->secure->client_sequence_number, zeroed_sequence_number, S2N_TLS_SEQUENCE_NUM_LEN);
            EXPECT_TRUE(s2n_stuffer_data_available(&stuffer) > 0);

            EXPECT_SUCCESS(s2n_stuffer_free(&stuffer));
            EXPECT_SUCCESS(s2n_connection_free(client_conn));
        };

        /* Key update is triggered by encryption limits */
        {
            struct s2n_connection *client_conn;
            EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
            client_conn->actual_protocol_version = S2N_TLS13;
            client_conn->secure->cipher_suite = cipher_suite_with_limit;
            POSIX_CHECKED_MEMCPY(client_conn->secrets.tls13.client_app_secret, application_secret.data, application_secret.size);

            /* Setup io */
            struct s2n_stuffer stuffer;
            EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0));
            EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&stuffer, &stuffer, client_conn));

            client_conn->key_update_pending = false;
            EXPECT_OK(s2n_write_uint64(record_limit, client_conn->secure->client_sequence_number));

            s2n_blocked_status blocked = 0;
            EXPECT_SUCCESS(s2n_key_update_send(client_conn, &blocked));

            EXPECT_EQUAL(client_conn->key_update_pending, false);
            EXPECT_BYTEARRAY_EQUAL(client_conn->secure->client_sequence_number, zeroed_sequence_number, S2N_TLS_SEQUENCE_NUM_LEN);
            EXPECT_TRUE(s2n_stuffer_data_available(&stuffer) > 0);

            EXPECT_SUCCESS(s2n_stuffer_free(&stuffer));
            EXPECT_SUCCESS(s2n_connection_free(client_conn));
        };

        /* Key update is not triggered */
        {
            struct s2n_connection *client_conn;
            EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
            client_conn->actual_protocol_version = S2N_TLS13;
            client_conn->secure->cipher_suite = cipher_suite_with_limit;
            POSIX_CHECKED_MEMCPY(client_conn->secrets.tls13.client_app_secret, application_secret.data, application_secret.size);
            uint8_t expected_sequence_number[S2N_TLS_SEQUENCE_NUM_LEN] = { 0 };

            /* Setup io */
            struct s2n_stuffer stuffer;
            EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0));
            EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&stuffer, &stuffer, client_conn));

            client_conn->secure->client_sequence_number[LOWEST_BYTE] = 1;
            expected_sequence_number[LOWEST_BYTE] = 1;
            client_conn->key_update_pending = false;

            s2n_blocked_status blocked = 0;
            EXPECT_SUCCESS(s2n_key_update_send(client_conn, &blocked));

            EXPECT_EQUAL(client_conn->key_update_pending, false);
            EXPECT_BYTEARRAY_EQUAL(client_conn->secure->client_sequence_number, expected_sequence_number, S2N_TLS_SEQUENCE_NUM_LEN);
            EXPECT_TRUE(s2n_stuffer_data_available(&stuffer) == 0);

            EXPECT_SUCCESS(s2n_stuffer_free(&stuffer));
            EXPECT_SUCCESS(s2n_connection_free(client_conn));
        };

        /* Key update eventually occurs when record limit reached */
        {
            const uint64_t expected = record_limit;
            const uint64_t start = expected - 100;

            DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
                    s2n_connection_ptr_free);
            EXPECT_NOT_NULL(client_conn);
            client_conn->actual_protocol_version = S2N_TLS13;
            client_conn->secure->cipher_suite = cipher_suite_with_limit;
            POSIX_CHECKED_MEMCPY(client_conn->secrets.tls13.client_app_secret, application_secret.data, application_secret.size);

            /* Setup io */
            DEFER_CLEANUP(struct s2n_stuffer stuffer = { 0 }, s2n_stuffer_free);
            EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0));
            EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&stuffer, &stuffer, client_conn));

            struct s2n_blob sequence_number = { 0 };
            EXPECT_SUCCESS(s2n_blob_init(&sequence_number,
                    client_conn->secure->client_sequence_number, S2N_TLS_SEQUENCE_NUM_LEN));
            EXPECT_OK(s2n_write_uint64(start, client_conn->secure->client_sequence_number));

            uint64_t key_update_seq_num = 0;
            for (uint64_t i = start; i <= UINT64_MAX; i++) {
                s2n_blocked_status blocked = 0;
                EXPECT_SUCCESS(s2n_key_update_send(client_conn, &blocked));

                if (s2n_stuffer_data_available(&stuffer) > 0) {
                    key_update_seq_num = i;
                    break;
                }

                EXPECT_SUCCESS(s2n_increment_sequence_number(&sequence_number));
            }

            EXPECT_EQUAL(key_update_seq_num, expected);
        };

        /* Key update eventually occurs before we run out of sequence numbers */
        {
            const uint64_t expected = UINT64_MAX;
            const uint64_t start = expected - 100;

            DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
                    s2n_connection_ptr_free);
            EXPECT_NOT_NULL(client_conn);
            client_conn->actual_protocol_version = S2N_TLS13;
            client_conn->secure->cipher_suite = cipher_suite_without_limit;
            POSIX_CHECKED_MEMCPY(client_conn->secrets.tls13.client_app_secret, application_secret.data, application_secret.size);

            /* Setup io */
            DEFER_CLEANUP(struct s2n_stuffer stuffer = { 0 }, s2n_stuffer_free);
            EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0));
            EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&stuffer, &stuffer, client_conn));

            struct s2n_blob sequence_number = { 0 };
            EXPECT_SUCCESS(s2n_blob_init(&sequence_number,
                    client_conn->secure->client_sequence_number, S2N_TLS_SEQUENCE_NUM_LEN));
            EXPECT_OK(s2n_write_uint64(start, client_conn->secure->client_sequence_number));

            uint64_t key_update_seq_num = 0;
            for (uint64_t i = start; i <= UINT64_MAX; i++) {
                s2n_blocked_status blocked = 0;
                EXPECT_SUCCESS(s2n_key_update_send(client_conn, &blocked));

                if (s2n_stuffer_data_available(&stuffer) > 0) {
                    key_update_seq_num = i;
                    break;
                }

                EXPECT_SUCCESS(s2n_increment_sequence_number(&sequence_number));
            }

            EXPECT_EQUAL(key_update_seq_num, expected);
        };
    };

    /* s2n_check_record_limit */
    {
        /* Record encryption limit exists (AES-GCM) */
        {
            struct s2n_blob sequence_number = { 0 };
            uint8_t sequence_number_bytes[S2N_TLS_SEQUENCE_NUM_LEN] = { 0 };
            EXPECT_SUCCESS(s2n_blob_init(&sequence_number, sequence_number_bytes, sizeof(sequence_number_bytes)));

            DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
            EXPECT_NOT_NULL(conn);
            conn->secure->cipher_suite = cipher_suite_with_limit;

            /* Not at limit yet: no records sent */
            conn->key_update_pending = false;
            EXPECT_OK(s2n_write_uint64(0, sequence_number_bytes));
            EXPECT_SUCCESS(s2n_check_record_limit(conn, &sequence_number));
            EXPECT_FALSE(conn->key_update_pending);

            /* Not at limit yet: 2 records less than limit */
            conn->key_update_pending = false;
            EXPECT_OK(s2n_write_uint64(record_limit - 2, sequence_number_bytes));
            EXPECT_SUCCESS(s2n_check_record_limit(conn, &sequence_number));
            EXPECT_FALSE(conn->key_update_pending);

            /* 1 record less than limit */
            conn->key_update_pending = false;
            EXPECT_OK(s2n_write_uint64(record_limit - 1, sequence_number_bytes));
            EXPECT_SUCCESS(s2n_check_record_limit(conn, &sequence_number));
            EXPECT_FALSE(conn->key_update_pending);

            /* Limit */
            conn->key_update_pending = false;
            EXPECT_OK(s2n_write_uint64(record_limit, sequence_number_bytes));
            EXPECT_SUCCESS(s2n_check_record_limit(conn, &sequence_number));
            EXPECT_TRUE(conn->key_update_pending);

            /* Don't reset the key_update_pending flag if already set */
            EXPECT_OK(s2n_write_uint64(0, sequence_number_bytes));
            EXPECT_SUCCESS(s2n_check_record_limit(conn, &sequence_number));
            EXPECT_TRUE(conn->key_update_pending);

            /* Over limit */
            conn->key_update_pending = false;
            EXPECT_OK(s2n_write_uint64(record_limit + 1, sequence_number_bytes));
            EXPECT_SUCCESS(s2n_check_record_limit(conn, &sequence_number));
            EXPECT_TRUE(conn->key_update_pending);
        };

        /* No record encryption limit (CHACHA20) */
        {
            struct s2n_blob sequence_number = { 0 };
            uint8_t sequence_number_bytes[S2N_TLS_SEQUENCE_NUM_LEN] = { 0 };
            EXPECT_SUCCESS(s2n_blob_init(&sequence_number, sequence_number_bytes, sizeof(sequence_number_bytes)));

            DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
            EXPECT_NOT_NULL(conn);
            conn->secure->cipher_suite = cipher_suite_without_limit;

            /* Not at limit yet: no records sent */
            conn->key_update_pending = false;
            EXPECT_OK(s2n_write_uint64(0, sequence_number_bytes));
            EXPECT_SUCCESS(s2n_check_record_limit(conn, &sequence_number));
            EXPECT_FALSE(conn->key_update_pending);

            /* Not at limit yet: 2 records less than limit */
            conn->key_update_pending = false;
            EXPECT_OK(s2n_write_uint64(UINT64_MAX - 2, sequence_number_bytes));
            EXPECT_SUCCESS(s2n_check_record_limit(conn, &sequence_number));
            EXPECT_FALSE(conn->key_update_pending);

            /* 1 record less than limit */
            conn->key_update_pending = false;
            EXPECT_OK(s2n_write_uint64(UINT64_MAX - 1, sequence_number_bytes));
            EXPECT_SUCCESS(s2n_check_record_limit(conn, &sequence_number));
            EXPECT_FALSE(conn->key_update_pending);

            /* Limit */
            conn->key_update_pending = false;
            EXPECT_OK(s2n_write_uint64(UINT64_MAX, sequence_number_bytes));
            EXPECT_SUCCESS(s2n_check_record_limit(conn, &sequence_number));
            EXPECT_TRUE(conn->key_update_pending);

            /* Don't reset the key_update_pending flag if already set */
            EXPECT_OK(s2n_write_uint64(0, sequence_number_bytes));
            EXPECT_SUCCESS(s2n_check_record_limit(conn, &sequence_number));
            EXPECT_TRUE(conn->key_update_pending);

            /* Over limit not possible: limit is maximum value */
        };
    };

    /* Test: KeyUpdate fails if fragmentation required */
    {
        const size_t key_update_record_size = S2N_TLS_MAX_RECORD_LEN_FOR(S2N_KEY_UPDATE_MESSAGE_SIZE);

        /* Test: send buffer cannot be set smaller than a KeyUpdate record */
        {
            DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
            EXPECT_FAILURE_WITH_ERRNO(s2n_config_set_send_buffer_size(config, key_update_record_size - 1),
                    S2N_ERR_INVALID_ARGUMENT);
        };

        /* Test: send fails if send buffer is too small for a KeyUpdate record */
        {
            DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
            s2n_blocked_status blocked = S2N_NOT_BLOCKED;

            DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
            EXPECT_OK(s2n_connection_set_secrets(conn));

            DEFER_CLEANUP(struct s2n_stuffer stuffer = { 0 }, s2n_stuffer_free);
            EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0));
            EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&stuffer, &stuffer, conn));

            /* Sanity check: send buffer just large enough for KeyUpdate record */
            config->send_buffer_size_override = key_update_record_size;
            EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
            conn->key_update_pending = true;
            EXPECT_SUCCESS(s2n_key_update_send(conn, &blocked));

            EXPECT_SUCCESS(s2n_connection_release_buffers(conn));

            /* Test: send buffer too small for KeyUpdate record */
            config->send_buffer_size_override = key_update_record_size - 1;
            EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
            conn->key_update_pending = true;
            EXPECT_FAILURE_WITH_ERRNO(s2n_key_update_send(conn, &blocked), S2N_ERR_FRAGMENT_LENGTH_TOO_LARGE);
        };
    };

    /* Test: all cipher suites must have record limits set.
     *
     * If this ever changes, then s2n_check_record_limit needs to consider
     * the case where there is no record limit.
     */
    for (size_t i = 0; i < cipher_preferences_test_all.count; i++) {
        struct s2n_cipher_suite *cipher_suite = cipher_preferences_test_all.suites[i];
        EXPECT_NOT_NULL(cipher_suite);
        if (cipher_suite->available) {
            EXPECT_NOT_NULL(cipher_suite->record_alg);
            EXPECT_TRUE(cipher_suite->record_alg->encryption_limit > 0);
        }
    }

    END_TEST();
}