Debian Codegraph @

crt/s2n/tests/unit/s2n_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 "tls/s2n_early_data.h"
17
18 #include "s2n_test.h"
19 #include "testlib/s2n_testlib.h"
20
21 #define TEST_SIZE 10
22
23 typedef struct {
24 const s2n_early_data_state *states;
25 size_t len;
26 } s2n_early_state_sequence;
27
28 /* We want to verify that applying s2n_connection_set_early_data_state to the current state
29 * can only produce valid state sequences.
30 *
31 * We check every possible next state and verify that the only transitions that
32 * s2n_connection_set_early_data_state allows are those in the valid state sequences. Then, for
33 * every valid transition, we call this method again recursively. The recursion ends when we either
34 * reach the end of a valid state sequence or encounter an invalid state sequence.
35 */
36 static S2N_RESULT s2n_test_all_early_data_sequences(struct s2n_connection *conn, size_t i,
37 const s2n_early_state_sequence *valid_early_state_sequences, size_t valid_early_state_sequences_len)
38 {
39 s2n_early_data_state current_state = conn->early_data_state;
40 for (s2n_early_data_state next_state = 0; next_state < S2N_EARLY_DATA_STATES_COUNT; next_state++) {
41 /* We always allow no-op transitions, so ignore them */
42 if (next_state == current_state) {
43 continue;
44 }
45
46 conn->early_data_state = current_state;
47
48 bool actual_valid = s2n_result_is_ok(s2n_connection_set_early_data_state(conn, next_state));
49 bool expected_valid = false;
50
51 size_t next_i = i + 1;
52 for (size_t j = 0; j < valid_early_state_sequences_len; j++) {
53 if (next_i < valid_early_state_sequences[j].len) {
54 expected_valid |= (valid_early_state_sequences[j].states[i] == current_state)
55 && (valid_early_state_sequences[j].states[next_i] == next_state);
56 }
57 }
58
59 RESULT_ENSURE_EQ(actual_valid, expected_valid);
60
61 if (expected_valid) {
62 RESULT_GUARD(s2n_test_all_early_data_sequences(conn, i + 1,
63 valid_early_state_sequences, valid_early_state_sequences_len));
64 }
65 }
66 return S2N_RESULT_OK;
67 }
68
69 static S2N_RESULT s2n_alloc_test_config_buffers(struct s2n_early_data_config *config)
70 {
71 RESULT_GUARD_POSIX(s2n_alloc(&config->application_protocol, TEST_SIZE));
72 RESULT_ENSURE_NE(config->application_protocol.size, 0);
73 RESULT_GUARD_POSIX(s2n_alloc(&config->context, TEST_SIZE));
74 RESULT_ENSURE_NE(config->context.size, 0);
75 return S2N_RESULT_OK;
76 }
77
78 static S2N_RESULT s2n_test_config_buffers_freed(struct s2n_early_data_config *config)
79 {
80 RESULT_ENSURE_EQ(config->application_protocol.data, NULL);
81 RESULT_ENSURE_EQ(config->application_protocol.size, 0);
82 RESULT_ENSURE_EQ(config->context.data, NULL);
83 RESULT_ENSURE_EQ(config->context.size, 0);
84 return S2N_RESULT_OK;
85 }
86
87 static int s2n_test_early_data_cb(struct s2n_connection *conn, struct s2n_offered_early_data *early_data)
88 {
89 POSIX_ENSURE_REF(conn);
90
91 uint16_t context_len = 0;
92 POSIX_GUARD(s2n_offered_early_data_get_context_length(early_data, &context_len));
93 POSIX_ENSURE_EQ(context_len, 1);
94
95 uint8_t context = 0;
96 POSIX_GUARD(s2n_offered_early_data_get_context(early_data, &context, 1));
97
98 if (context) {
99 POSIX_GUARD(s2n_offered_early_data_accept(early_data));
100 } else {
101 POSIX_GUARD(s2n_offered_early_data_reject(early_data));
102 }
103 return S2N_SUCCESS;
104 }
105
106 struct s2n_offered_early_data *async_early_data = NULL;
107 static int s2n_test_async_early_data_cb(struct s2n_connection *conn, struct s2n_offered_early_data *early_data)
108 {
109 POSIX_ENSURE_REF(conn);
110 async_early_data = early_data;
111 return S2N_SUCCESS;
112 }
113
114 int main(int argc, char **argv)
115 {
116 BEGIN_TEST();
117 const uint8_t test_value[] = "test value";
118 const uint8_t test_value_2[] = "more test data";
119
120 const uint32_t nonzero_max_early_data = 10;
121
122 /* Test s2n_connection_set_early_data_state */
123 {
124 /* Safety check */
125 EXPECT_ERROR_WITH_ERRNO(s2n_connection_set_early_data_state(NULL, 0), S2N_ERR_NULL);
126
127 const s2n_early_data_state early_data_not_requested_seq[] = {
128 S2N_UNKNOWN_EARLY_DATA_STATE, S2N_EARLY_DATA_NOT_REQUESTED
129 };
130 const s2n_early_data_state early_data_rejected_seq[] = {
131 S2N_UNKNOWN_EARLY_DATA_STATE, S2N_EARLY_DATA_REQUESTED, S2N_EARLY_DATA_REJECTED
132 };
133 const s2n_early_data_state early_data_success_seq[] = {
134 S2N_UNKNOWN_EARLY_DATA_STATE, S2N_EARLY_DATA_REQUESTED, S2N_EARLY_DATA_ACCEPTED, S2N_END_OF_EARLY_DATA
135 };
136
137 /* Test known valid / invalid transitions */
138 {
139 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
140 EXPECT_NOT_NULL(conn);
141
142 conn->early_data_state = 0;
143 EXPECT_OK(s2n_connection_set_early_data_state(conn, S2N_UNKNOWN_EARLY_DATA_STATE));
144
145 conn->early_data_state = 0;
146 EXPECT_ERROR_WITH_ERRNO(s2n_connection_set_early_data_state(conn, S2N_EARLY_DATA_STATES_COUNT),
147 S2N_ERR_INVALID_EARLY_DATA_STATE);
148
149 conn->early_data_state = 0;
150 EXPECT_ERROR_WITH_ERRNO(s2n_connection_set_early_data_state(conn, S2N_EARLY_DATA_STATES_COUNT + 1),
151 S2N_ERR_INVALID_EARLY_DATA_STATE);
152
153 conn->early_data_state = 0;
154 EXPECT_OK(s2n_connection_set_early_data_state(conn, S2N_EARLY_DATA_REQUESTED));
155
156 conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
157 EXPECT_OK(s2n_connection_set_early_data_state(conn, S2N_EARLY_DATA_REJECTED));
158
159 conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
160 EXPECT_ERROR_WITH_ERRNO(s2n_connection_set_early_data_state(conn, S2N_END_OF_EARLY_DATA),
161 S2N_ERR_INVALID_EARLY_DATA_STATE);
162
163 EXPECT_SUCCESS(s2n_connection_free(conn));
164 };
165
166 /* Test that only the expected sequences of states are possible.
167 * Given every possible sequence of early data states, test that s2n_connection_set_early_data_state
168 * can only be used to iterate through the known valid sequences. */
169 {
170 /* Test with the correct expected sequences */
171 {
172 const s2n_early_state_sequence valid_early_state_sequences[] = {
173 { .states = early_data_not_requested_seq, .len = s2n_array_len(early_data_not_requested_seq) },
174 { .states = early_data_rejected_seq, .len = s2n_array_len(early_data_rejected_seq) },
175 { .states = early_data_success_seq, .len = s2n_array_len(early_data_success_seq) },
176 };
177
178 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
179 EXPECT_NOT_NULL(conn);
180 EXPECT_OK(s2n_test_all_early_data_sequences(conn, 0,
181 valid_early_state_sequences, s2n_array_len(valid_early_state_sequences)));
182 EXPECT_SUCCESS(s2n_connection_free(conn));
183 };
184
185 /* Sanity check: adding an invalid expected sequence causes test to fail */
186 {
187 const s2n_early_data_state invalid_seq[] = {
188 S2N_UNKNOWN_EARLY_DATA_STATE, S2N_EARLY_DATA_ACCEPTED, S2N_END_OF_EARLY_DATA
189 };
190 const s2n_early_state_sequence test_early_state_sequences[] = {
191 { .states = early_data_not_requested_seq, .len = s2n_array_len(early_data_not_requested_seq) },
192 { .states = early_data_rejected_seq, .len = s2n_array_len(early_data_rejected_seq) },
193 { .states = early_data_success_seq, .len = s2n_array_len(early_data_success_seq) },
194 { .states = invalid_seq, .len = s2n_array_len(invalid_seq) },
195 };
196
197 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
198 EXPECT_NOT_NULL(conn);
199 EXPECT_ERROR(s2n_test_all_early_data_sequences(conn, 0,
200 test_early_state_sequences, s2n_array_len(test_early_state_sequences)));
201 EXPECT_SUCCESS(s2n_connection_free(conn));
202 };
203
204 /* Sanity check: removing one of the expected sequences causes test to fail */
205 {
206 const s2n_early_state_sequence test_early_state_sequences[] = {
207 { .states = early_data_not_requested_seq, .len = s2n_array_len(early_data_not_requested_seq) },
208 { .states = early_data_success_seq, .len = s2n_array_len(early_data_success_seq) },
209 };
210
211 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
212 EXPECT_NOT_NULL(conn);
213 EXPECT_ERROR(s2n_test_all_early_data_sequences(conn, 0,
214 test_early_state_sequences, s2n_array_len(test_early_state_sequences)));
215 EXPECT_SUCCESS(s2n_connection_free(conn));
216 };
217 };
218 };
219
220 /* Test s2n_early_data_config_free */
221 {
222 /* Safety check */
223 EXPECT_OK(s2n_early_data_config_free(NULL));
224
225 /* Resets everything */
226 {
227 struct s2n_early_data_config config = { 0 };
228 EXPECT_OK(s2n_alloc_test_config_buffers(&config));
229
230 EXPECT_OK(s2n_early_data_config_free(&config));
231 EXPECT_OK(s2n_test_config_buffers_freed(&config));
232 };
233
234 /* Called by s2n_psk_wipe */
235 {
236 struct s2n_psk psk = { 0 };
237 EXPECT_OK(s2n_alloc_test_config_buffers(&psk.early_data_config));
238
239 EXPECT_OK(s2n_psk_wipe(&psk));
240 EXPECT_OK(s2n_test_config_buffers_freed(&psk.early_data_config));
241 };
242
243 /* Called by s2n_psk_free */
244 {
245 struct s2n_psk *psk = s2n_external_psk_new();
246 EXPECT_OK(s2n_alloc_test_config_buffers(&psk->early_data_config));
247
248 EXPECT_SUCCESS(s2n_psk_free(&psk));
249 /* A memory leak in this test would indicate that s2n_psk_free isn't freeing the buffers. */
250 };
251 };
252
253 /* Test s2n_psk_configure_early_data */
254 {
255 /* Safety */
256 EXPECT_FAILURE_WITH_ERRNO(s2n_psk_configure_early_data(NULL, nonzero_max_early_data, 1, 1), S2N_ERR_NULL);
257
258 /* Set valid configuration */
259 {
260 uint32_t expected_max_early_data_size = 1000;
261 const struct s2n_cipher_suite *expected_cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
262
263 DEFER_CLEANUP(struct s2n_psk *psk = s2n_external_psk_new(), s2n_psk_free);
264 EXPECT_SUCCESS(s2n_psk_configure_early_data(psk, expected_max_early_data_size,
265 expected_cipher_suite->iana_value[0], expected_cipher_suite->iana_value[1]));
266
267 EXPECT_EQUAL(psk->early_data_config.max_early_data_size, expected_max_early_data_size);
268 EXPECT_EQUAL(psk->early_data_config.protocol_version, S2N_TLS13);
269 EXPECT_EQUAL(psk->early_data_config.cipher_suite, expected_cipher_suite);
270 };
271
272 /* Set cipher suite must match hmac algorithm */
273 {
274 DEFER_CLEANUP(struct s2n_psk *psk = s2n_external_psk_new(), s2n_psk_free);
275 const struct s2n_cipher_suite *cipher_suite = &s2n_tls13_aes_256_gcm_sha384;
276
277 psk->hmac_alg = cipher_suite->prf_alg + 1;
278 EXPECT_FAILURE_WITH_ERRNO(s2n_psk_configure_early_data(psk, nonzero_max_early_data,
279 cipher_suite->iana_value[0], cipher_suite->iana_value[1]),
280 S2N_ERR_INVALID_ARGUMENT);
281
282 psk->hmac_alg = cipher_suite->prf_alg;
283 EXPECT_SUCCESS(s2n_psk_configure_early_data(psk, nonzero_max_early_data,
284 cipher_suite->iana_value[0], cipher_suite->iana_value[1]));
285 EXPECT_EQUAL(psk->early_data_config.cipher_suite, cipher_suite);
286 };
287 };
288
289 /* Test s2n_psk_set_application_protocol */
290 {
291 /* Safety checks */
292 {
293 struct s2n_psk psk = { 0 };
294 EXPECT_FAILURE_WITH_ERRNO(s2n_psk_set_application_protocol(&psk, NULL, 1), S2N_ERR_NULL);
295 EXPECT_FAILURE_WITH_ERRNO(s2n_psk_set_application_protocol(NULL, test_value, 1), S2N_ERR_NULL);
296 };
297
298 DEFER_CLEANUP(struct s2n_psk *psk = s2n_external_psk_new(), s2n_psk_free);
299 EXPECT_EQUAL(psk->early_data_config.application_protocol.size, 0);
300 EXPECT_EQUAL(psk->early_data_config.application_protocol.allocated, 0);
301
302 /* Set empty value as no-op */
303 EXPECT_SUCCESS(s2n_psk_set_application_protocol(psk, test_value, 0));
304 EXPECT_EQUAL(psk->early_data_config.application_protocol.size, 0);
305 EXPECT_EQUAL(psk->early_data_config.application_protocol.allocated, 0);
306
307 /* Set valid value */
308 EXPECT_SUCCESS(s2n_psk_set_application_protocol(psk, test_value, sizeof(test_value)));
309 EXPECT_EQUAL(psk->early_data_config.application_protocol.size, sizeof(test_value));
310 EXPECT_BYTEARRAY_EQUAL(psk->early_data_config.application_protocol.data, test_value, sizeof(test_value));
311
312 /* Replace previous value */
313 EXPECT_SUCCESS(s2n_psk_set_application_protocol(psk, test_value_2, sizeof(test_value_2)));
314 EXPECT_EQUAL(psk->early_data_config.application_protocol.size, sizeof(test_value_2));
315 EXPECT_BYTEARRAY_EQUAL(psk->early_data_config.application_protocol.data, test_value_2, sizeof(test_value_2));
316
317 /* Clear with empty value */
318 EXPECT_SUCCESS(s2n_psk_set_application_protocol(psk, test_value, 0));
319 EXPECT_EQUAL(psk->early_data_config.application_protocol.size, 0);
320 EXPECT_EQUAL(psk->early_data_config.application_protocol.allocated, 0);
321
322 /* Repeat clear */
323 EXPECT_SUCCESS(s2n_psk_set_application_protocol(psk, test_value, 0));
324 EXPECT_EQUAL(psk->early_data_config.application_protocol.size, 0);
325 EXPECT_EQUAL(psk->early_data_config.application_protocol.allocated, 0);
326 };
327
328 /* Test s2n_psk_set_early_data_context */
329 {
330 /* Safety checks */
331 {
332 struct s2n_psk psk = { 0 };
333 EXPECT_FAILURE_WITH_ERRNO(s2n_psk_set_early_data_context(&psk, NULL, 1), S2N_ERR_NULL);
334 EXPECT_FAILURE_WITH_ERRNO(s2n_psk_set_early_data_context(NULL, test_value, 1), S2N_ERR_NULL);
335 };
336
337 DEFER_CLEANUP(struct s2n_psk *psk = s2n_external_psk_new(), s2n_psk_free);
338 EXPECT_EQUAL(psk->early_data_config.context.size, 0);
339 EXPECT_EQUAL(psk->early_data_config.context.allocated, 0);
340
341 /* Set empty value as no-op */
342 EXPECT_SUCCESS(s2n_psk_set_early_data_context(psk, test_value, 0));
343 EXPECT_EQUAL(psk->early_data_config.context.size, 0);
344 EXPECT_EQUAL(psk->early_data_config.context.allocated, 0);
345
346 /* Set valid value */
347 EXPECT_SUCCESS(s2n_psk_set_early_data_context(psk, test_value, sizeof(test_value)));
348 EXPECT_EQUAL(psk->early_data_config.context.size, sizeof(test_value));
349 EXPECT_BYTEARRAY_EQUAL(psk->early_data_config.context.data, test_value, sizeof(test_value));
350
351 /* Replace previous value */
352 EXPECT_SUCCESS(s2n_psk_set_early_data_context(psk, test_value_2, sizeof(test_value_2)));
353 EXPECT_EQUAL(psk->early_data_config.context.size, sizeof(test_value_2));
354 EXPECT_BYTEARRAY_EQUAL(psk->early_data_config.context.data, test_value_2, sizeof(test_value_2));
355
356 /* Clear with empty value */
357 EXPECT_SUCCESS(s2n_psk_set_early_data_context(psk, test_value, 0));
358 EXPECT_EQUAL(psk->early_data_config.context.size, 0);
359 EXPECT_EQUAL(psk->early_data_config.context.allocated, 0);
360
361 /* Repeat clear */
362 EXPECT_SUCCESS(s2n_psk_set_early_data_context(psk, test_value, 0));
363 EXPECT_EQUAL(psk->early_data_config.context.size, 0);
364 EXPECT_EQUAL(psk->early_data_config.context.allocated, 0);
365 };
366
367 /* Test s2n_early_data_config_clone */
368 {
369 const uint8_t test_bad_value[] = "wrong";
370 const uint8_t test_apln[] = "protocol";
371 const uint8_t test_context[] = "context";
372 const uint8_t test_version = UINT8_MAX;
373 const uint32_t test_max_early_data = 10;
374 const struct s2n_cipher_suite *test_cipher_suite = &s2n_tls13_chacha20_poly1305_sha256;
375
376 for (size_t called_directly = 0; called_directly < 2; called_directly++) {
377 struct s2n_psk *original = s2n_external_psk_new();
378 EXPECT_NOT_NULL(original);
379 EXPECT_SUCCESS(s2n_psk_configure_early_data(original, test_max_early_data,
380 test_cipher_suite->iana_value[0], test_cipher_suite->iana_value[1]));
381 EXPECT_SUCCESS(s2n_psk_set_application_protocol(original, test_apln, sizeof(test_apln)));
382 EXPECT_SUCCESS(s2n_psk_set_early_data_context(original, test_context, sizeof(test_context)));
383 original->early_data_config.protocol_version = test_version;
384
385 DEFER_CLEANUP(struct s2n_psk *clone = s2n_external_psk_new(), s2n_psk_free);
386 EXPECT_SUCCESS(s2n_psk_set_application_protocol(clone, test_bad_value, sizeof(test_bad_value)));
387 EXPECT_NOT_NULL(clone);
388
389 if (called_directly) {
390 EXPECT_OK(s2n_early_data_config_clone(clone, &original->early_data_config));
391 } else {
392 EXPECT_SUCCESS(s2n_psk_set_identity(original, test_bad_value, sizeof(test_bad_value)));
393 EXPECT_SUCCESS(s2n_psk_set_secret(original, test_bad_value, sizeof(test_bad_value)));
394 EXPECT_OK(s2n_psk_clone(clone, original));
395 }
396
397 /* Check that the blobs weren't shallow copied */
398 EXPECT_NOT_EQUAL(original->early_data_config.application_protocol.data,
399 clone->early_data_config.application_protocol.data);
400 EXPECT_NOT_EQUAL(original->early_data_config.context.data, clone->early_data_config.context.data);
401
402 /* Free the original to ensure they share no memory */
403 EXPECT_SUCCESS(s2n_psk_free(&original));
404
405 /* existing alpn is replaced by original's alpn */
406 EXPECT_EQUAL(clone->early_data_config.application_protocol.size, sizeof(test_apln));
407 EXPECT_BYTEARRAY_EQUAL(clone->early_data_config.application_protocol.data, test_apln, sizeof(test_apln));
408
409 /* new context is allocated for original's context */
410 EXPECT_EQUAL(clone->early_data_config.context.size, sizeof(test_context));
411 EXPECT_BYTEARRAY_EQUAL(clone->early_data_config.context.data, test_context, sizeof(test_context));
412
413 /* other values are copied */
414 EXPECT_EQUAL(clone->early_data_config.max_early_data_size, test_max_early_data);
415 EXPECT_EQUAL(clone->early_data_config.cipher_suite, test_cipher_suite);
416 EXPECT_EQUAL(clone->early_data_config.protocol_version, test_version);
417 }
418 };
419
420 /* Test s2n_early_data_is_valid_for_connection */
421 {
422 /* Safety check */
423 EXPECT_FALSE(s2n_early_data_is_valid_for_connection(NULL));
424
425 /* Not valid if the first wire PSK was not chosen
426 *
427 *= https://www.rfc-editor.org/rfc/rfc8446#section-4.2.10
428 *= type=test
429 *# In order to accept early data, the server MUST have accepted a PSK
430 *# cipher suite and selected the first key offered in the client's
431 *# "pre_shared_key" extension.
432 **/
433 {
434 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
435 EXPECT_NOT_NULL(conn);
436 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "default_tls13"));
437 conn->secure->cipher_suite = &s2n_tls13_aes_256_gcm_sha384;
438 EXPECT_OK(s2n_conn_choose_state_machine(conn, S2N_TLS13));
439 conn->actual_protocol_version = S2N_TLS13;
440 conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
441
442 /* No chosen PSK */
443 EXPECT_NULL(conn->psk_params.chosen_psk);
444 EXPECT_FALSE(s2n_early_data_is_valid_for_connection(conn));
445
446 EXPECT_OK(s2n_append_test_chosen_psk_with_early_data(conn, nonzero_max_early_data, &s2n_tls13_aes_256_gcm_sha384));
447
448 /* PSK chosen, but not first PSK */
449 conn->psk_params.chosen_psk_wire_index = 5;
450 EXPECT_FALSE(s2n_early_data_is_valid_for_connection(conn));
451
452 /* First PSK chosen */
453 conn->psk_params.chosen_psk_wire_index = 0;
454 EXPECT_TRUE(s2n_early_data_is_valid_for_connection(conn));
455
456 EXPECT_SUCCESS(s2n_connection_free(conn));
457 };
458
459 /**
460 *= https://www.rfc-editor.org/rfc/rfc8446#section-4.2.10
461 *= type=test
462 *# In addition, it MUST verify that the
463 *# following values are the same as those associated with the
464 *# selected PSK:
465 **/
466 {
467 /**
468 *= https://www.rfc-editor.org/rfc/rfc8446#section-4.2.10
469 *= type=test
470 *# - The TLS version number
471 **/
472 {
473 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
474 EXPECT_NOT_NULL(conn);
475 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "default_tls13"));
476 EXPECT_OK(s2n_append_test_chosen_psk_with_early_data(conn, nonzero_max_early_data, &s2n_tls13_aes_256_gcm_sha384));
477 conn->secure->cipher_suite = &s2n_tls13_aes_256_gcm_sha384;
478 conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
479
480 conn->actual_protocol_version = S2N_TLS12;
481 EXPECT_OK(s2n_conn_choose_state_machine(conn, S2N_TLS12));
482 EXPECT_FALSE(s2n_early_data_is_valid_for_connection(conn));
483
484 /* Reset state machine */
485 conn->handshake.state_machine = S2N_STATE_MACHINE_INITIAL;
486
487 conn->actual_protocol_version = S2N_TLS13;
488 EXPECT_OK(s2n_conn_choose_state_machine(conn, S2N_TLS13));
489 EXPECT_TRUE(s2n_early_data_is_valid_for_connection(conn));
490
491 EXPECT_SUCCESS(s2n_connection_free(conn));
492 };
493
494 /**
495 *= https://www.rfc-editor.org/rfc/rfc8446#section-4.2.10
496 *= type=test
497 *# - The selected cipher suite
498 **/
499 {
500 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
501 EXPECT_NOT_NULL(conn);
502 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "default_tls13"));
503 EXPECT_OK(s2n_append_test_chosen_psk_with_early_data(conn, nonzero_max_early_data, &s2n_tls13_aes_256_gcm_sha384));
504 EXPECT_OK(s2n_conn_choose_state_machine(conn, S2N_TLS13));
505 conn->actual_protocol_version = S2N_TLS13;
506 conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
507
508 conn->secure->cipher_suite = &s2n_tls13_chacha20_poly1305_sha256;
509 EXPECT_FALSE(s2n_early_data_is_valid_for_connection(conn));
510
511 conn->secure->cipher_suite = &s2n_tls13_aes_256_gcm_sha384;
512 EXPECT_TRUE(s2n_early_data_is_valid_for_connection(conn));
513
514 EXPECT_SUCCESS(s2n_connection_free(conn));
515 };
516
517 /**
518 *= https://www.rfc-editor.org/rfc/rfc8446#section-4.2.10
519 *= type=test
520 *# - The selected ALPN [RFC7301] protocol, if any
521 **/
522 {
523 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
524 EXPECT_NOT_NULL(conn);
525 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "default_tls13"));
526 EXPECT_OK(s2n_append_test_chosen_psk_with_early_data(conn, nonzero_max_early_data, &s2n_tls13_aes_256_gcm_sha384));
527 conn->secure->cipher_suite = &s2n_tls13_aes_256_gcm_sha384;
528 conn->actual_protocol_version = S2N_TLS13;
529 EXPECT_OK(s2n_conn_choose_state_machine(conn, S2N_TLS13));
530 conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
531
532 const uint8_t empty_protocol[] = "";
533 const uint8_t required_protocol[] = "required_protocol";
534 const uint8_t wrong_protocol[] = "wrong protocol";
535
536 /* No early data alpn set, no alpn negotiated */
537 EXPECT_TRUE(s2n_early_data_is_valid_for_connection(conn));
538
539 /* No early data alpn set, but alpn negotiated */
540 EXPECT_MEMCPY_SUCCESS(conn->application_protocol, required_protocol, sizeof(required_protocol));
541 EXPECT_FALSE(s2n_early_data_is_valid_for_connection(conn));
542
543 EXPECT_SUCCESS(s2n_psk_set_application_protocol(conn->psk_params.chosen_psk,
544 required_protocol, sizeof(required_protocol)));
545
546 /* Early data alpn set, but no alpn negotiated */
547 EXPECT_MEMCPY_SUCCESS(conn->application_protocol, empty_protocol, sizeof(empty_protocol));
548 EXPECT_FALSE(s2n_early_data_is_valid_for_connection(conn));
549
550 /* Early data alpn does NOT match negotiated alpn */
551 EXPECT_MEMCPY_SUCCESS(conn->application_protocol, wrong_protocol, sizeof(wrong_protocol));
552 EXPECT_FALSE(s2n_early_data_is_valid_for_connection(conn));
553
554 /* Early data alpn matches negotiated alpn */
555 EXPECT_MEMCPY_SUCCESS(conn->application_protocol, required_protocol, sizeof(required_protocol));
556 EXPECT_TRUE(s2n_early_data_is_valid_for_connection(conn));
557
558 EXPECT_SUCCESS(s2n_connection_free(conn));
559 };
560 };
561 };
562
563 /* Test s2n_early_data_accept_or_reject */
564 {
565 /* Safety check */
566 EXPECT_ERROR_WITH_ERRNO(s2n_early_data_accept_or_reject(NULL), S2N_ERR_NULL);
567
568 /* Server */
569 {
570 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
571 EXPECT_NOT_NULL(conn);
572 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "default_tls13"));
573 EXPECT_OK(s2n_append_test_chosen_psk_with_early_data(conn, nonzero_max_early_data, &s2n_tls13_aes_256_gcm_sha384));
574 conn->secure->cipher_suite = &s2n_tls13_aes_256_gcm_sha384;
575
576 /* Early data not enabled */
577 conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
578 conn->actual_protocol_version = S2N_TLS13;
579 EXPECT_OK(s2n_early_data_accept_or_reject(conn));
580 EXPECT_EQUAL(conn->early_data_state, S2N_EARLY_DATA_REJECTED);
581
582 EXPECT_SUCCESS(s2n_connection_set_early_data_expected(conn));
583
584 /* Early data not requested */
585 conn->early_data_state = S2N_EARLY_DATA_NOT_REQUESTED;
586 conn->actual_protocol_version = S2N_TLS13;
587 EXPECT_OK(s2n_early_data_accept_or_reject(conn));
588 EXPECT_EQUAL(conn->early_data_state, S2N_EARLY_DATA_NOT_REQUESTED);
589
590 /* Set wrong protocol version */
591 conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
592 conn->actual_protocol_version = S2N_TLS12;
593 EXPECT_OK(s2n_early_data_accept_or_reject(conn));
594 EXPECT_EQUAL(conn->early_data_state, S2N_EARLY_DATA_REJECTED);
595
596 conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
597 conn->actual_protocol_version = S2N_TLS13;
598 EXPECT_OK(s2n_early_data_accept_or_reject(conn));
599 EXPECT_EQUAL(conn->early_data_state, S2N_EARLY_DATA_ACCEPTED);
600
601 EXPECT_SUCCESS(s2n_connection_free(conn));
602 };
603
604 /* Client */
605 {
606 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
607 EXPECT_NOT_NULL(conn);
608 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "default_tls13"));
609 EXPECT_OK(s2n_append_test_chosen_psk_with_early_data(conn, nonzero_max_early_data, &s2n_tls13_aes_256_gcm_sha384));
610 conn->secure->cipher_suite = &s2n_tls13_aes_256_gcm_sha384;
611
612 /* Early data not enabled */
613 conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
614 conn->actual_protocol_version = S2N_TLS13;
615 EXPECT_OK(s2n_early_data_accept_or_reject(conn));
616 EXPECT_EQUAL(conn->early_data_state, S2N_EARLY_DATA_REQUESTED);
617
618 EXPECT_SUCCESS(s2n_connection_set_early_data_expected(conn));
619
620 /* Early data not requested */
621 conn->early_data_state = S2N_EARLY_DATA_NOT_REQUESTED;
622 conn->actual_protocol_version = S2N_TLS13;
623 EXPECT_OK(s2n_early_data_accept_or_reject(conn));
624 EXPECT_EQUAL(conn->early_data_state, S2N_EARLY_DATA_NOT_REQUESTED);
625
626 /* Set wrong protocol version */
627 conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
628 conn->actual_protocol_version = S2N_TLS12;
629 EXPECT_OK(s2n_early_data_accept_or_reject(conn));
630 EXPECT_EQUAL(conn->early_data_state, S2N_EARLY_DATA_REJECTED);
631
632 conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
633 conn->actual_protocol_version = S2N_TLS13;
634 EXPECT_OK(s2n_early_data_accept_or_reject(conn));
635 EXPECT_EQUAL(conn->early_data_state, S2N_EARLY_DATA_REQUESTED);
636
637 EXPECT_SUCCESS(s2n_connection_free(conn));
638 };
639
640 /* Triggers callback to let application reject early data */
641 {
642 struct s2n_config *config = s2n_config_new();
643 EXPECT_NOT_NULL(config);
644
645 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
646 EXPECT_NOT_NULL(conn);
647 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
648 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "default_tls13"));
649 EXPECT_OK(s2n_append_test_chosen_psk_with_early_data(conn, nonzero_max_early_data, &s2n_tls13_aes_256_gcm_sha384));
650 EXPECT_SUCCESS(s2n_connection_set_early_data_expected(conn));
651 conn->secure->cipher_suite = &s2n_tls13_aes_256_gcm_sha384;
652 conn->actual_protocol_version = S2N_TLS13;
653
654 /* Without callback set, accepts early data */
655 conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
656 EXPECT_OK(s2n_early_data_accept_or_reject(conn));
657 EXPECT_EQUAL(conn->early_data_state, S2N_EARLY_DATA_ACCEPTED);
658
659 uint8_t accept_early_data = true;
660 EXPECT_SUCCESS(s2n_config_set_early_data_cb(config, s2n_test_early_data_cb));
661
662 /* With callback set, may still accept early data */
663 conn->handshake.early_data_async_state = (struct s2n_offered_early_data){ 0 };
664 conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
665 EXPECT_SUCCESS(s2n_psk_set_early_data_context(conn->psk_params.chosen_psk, &accept_early_data, sizeof(accept_early_data)));
666 EXPECT_OK(s2n_early_data_accept_or_reject(conn));
667 EXPECT_EQUAL(conn->early_data_state, S2N_EARLY_DATA_ACCEPTED);
668
669 /* With callback set, may reject early data */
670 accept_early_data = false;
671 conn->handshake.early_data_async_state = (struct s2n_offered_early_data){ 0 };
672 conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
673 EXPECT_SUCCESS(s2n_psk_set_early_data_context(conn->psk_params.chosen_psk, &accept_early_data, sizeof(accept_early_data)));
674 EXPECT_OK(s2n_early_data_accept_or_reject(conn));
675 EXPECT_EQUAL(conn->early_data_state, S2N_EARLY_DATA_REJECTED);
676
677 EXPECT_SUCCESS(s2n_connection_free(conn));
678 EXPECT_SUCCESS(s2n_config_free(config));
679 };
680
681 /* Application rejects early data asynchronously */
682 {
683 struct s2n_config *config = s2n_config_new();
684 EXPECT_SUCCESS(s2n_config_set_early_data_cb(config, s2n_test_async_early_data_cb));
685 EXPECT_NOT_NULL(config);
686
687 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
688 EXPECT_NOT_NULL(conn);
689 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
690 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "default_tls13"));
691 EXPECT_OK(s2n_append_test_chosen_psk_with_early_data(conn, nonzero_max_early_data, &s2n_tls13_aes_256_gcm_sha384));
692 EXPECT_SUCCESS(s2n_connection_set_early_data_expected(conn));
693 conn->secure->cipher_suite = &s2n_tls13_aes_256_gcm_sha384;
694 EXPECT_OK(s2n_conn_choose_state_machine(conn, S2N_TLS13));
695 conn->actual_protocol_version = S2N_TLS13;
696 conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
697
698 /* No decision yet: blocked */
699 EXPECT_ERROR_WITH_ERRNO(s2n_early_data_accept_or_reject(conn), S2N_ERR_ASYNC_BLOCKED);
700 EXPECT_EQUAL(conn->early_data_state, S2N_EARLY_DATA_REQUESTED);
701
702 /* If called again, still blocked */
703 EXPECT_ERROR_WITH_ERRNO(s2n_early_data_accept_or_reject(conn), S2N_ERR_ASYNC_BLOCKED);
704 EXPECT_EQUAL(conn->early_data_state, S2N_EARLY_DATA_REQUESTED);
705
706 /* Make decision */
707 EXPECT_SUCCESS(s2n_offered_early_data_reject(async_early_data));
708 EXPECT_EQUAL(conn->early_data_state, S2N_EARLY_DATA_REJECTED);
709
710 /* Complete */
711 EXPECT_OK(s2n_early_data_accept_or_reject(conn));
712 EXPECT_EQUAL(conn->early_data_state, S2N_EARLY_DATA_REJECTED);
713
714 EXPECT_SUCCESS(s2n_connection_free(conn));
715 EXPECT_SUCCESS(s2n_config_free(config));
716 };
717 };
718
719 /* Test s2n_connection_get_early_data_status */
720 {
721 const uint32_t limit = 10;
722
723 /* Safety */
724 {
725 struct s2n_connection conn = { 0 };
726 s2n_early_data_status_t status = 0;
727 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_early_data_status(NULL, &status), S2N_ERR_NULL);
728 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_early_data_status(&conn, NULL), S2N_ERR_NULL);
729
730 conn.early_data_state = S2N_EARLY_DATA_STATES_COUNT;
731 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_early_data_status(&conn, &status), S2N_ERR_INVALID_EARLY_DATA_STATE);
732 };
733
734 /* Correct status returned for current early data state */
735 {
736 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
737 EXPECT_NOT_NULL(conn);
738 EXPECT_OK(s2n_append_test_psk_with_early_data(conn, limit, &s2n_tls13_aes_256_gcm_sha384));
739
740 s2n_early_data_status_t status = 0;
741
742 EXPECT_SUCCESS(s2n_connection_get_early_data_status(conn, &status));
743 EXPECT_EQUAL(status, S2N_EARLY_DATA_STATUS_OK);
744
745 conn->early_data_state = S2N_EARLY_DATA_NOT_REQUESTED;
746 EXPECT_SUCCESS(s2n_connection_get_early_data_status(conn, &status));
747 EXPECT_EQUAL(status, S2N_EARLY_DATA_STATUS_NOT_REQUESTED);
748
749 conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
750 EXPECT_SUCCESS(s2n_connection_get_early_data_status(conn, &status));
751 EXPECT_EQUAL(status, S2N_EARLY_DATA_STATUS_OK);
752
753 conn->early_data_state = S2N_EARLY_DATA_REJECTED;
754 EXPECT_SUCCESS(s2n_connection_get_early_data_status(conn, &status));
755 EXPECT_EQUAL(status, S2N_EARLY_DATA_STATUS_REJECTED);
756
757 conn->early_data_state = S2N_EARLY_DATA_ACCEPTED;
758 EXPECT_SUCCESS(s2n_connection_get_early_data_status(conn, &status));
759 EXPECT_EQUAL(status, S2N_EARLY_DATA_STATUS_OK);
760
761 conn->early_data_state = S2N_END_OF_EARLY_DATA;
762 EXPECT_SUCCESS(s2n_connection_get_early_data_status(conn, &status));
763 EXPECT_EQUAL(status, S2N_EARLY_DATA_STATUS_END);
764
765 EXPECT_SUCCESS(s2n_connection_free(conn));
766 };
767
768 /* Sanity check that all valid early data states successfully report a status */
769 {
770 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
771 EXPECT_NOT_NULL(conn);
772 EXPECT_OK(s2n_append_test_psk_with_early_data(conn, limit, &s2n_tls13_aes_256_gcm_sha384));
773
774 s2n_early_data_status_t status = 0;
775 for (s2n_early_data_state state = 1; state < S2N_EARLY_DATA_STATES_COUNT; state++) {
776 conn->early_data_state = state;
777 EXPECT_SUCCESS(s2n_connection_get_early_data_status(conn, &status));
778 }
779
780 EXPECT_SUCCESS(s2n_connection_free(conn));
781 };
782 };
783
784 /* Test s2n_connection_get_remaining_early_data_size */
785 {
786 const uint32_t limit = 10;
787
788 /* Safety */
789 {
790 struct s2n_connection conn = { 0 };
791 uint32_t bytes = 0;
792 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_remaining_early_data_size(NULL, &bytes), S2N_ERR_NULL);
793 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_remaining_early_data_size(&conn, NULL), S2N_ERR_NULL);
794 };
795
796 /* If early data allowed, return the remaining bytes allowed */
797 {
798 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
799 EXPECT_NOT_NULL(conn);
800 EXPECT_OK(s2n_append_test_psk_with_early_data(conn, limit, &s2n_tls13_aes_256_gcm_sha384));
801
802 uint32_t bytes = 0;
803
804 EXPECT_SUCCESS(s2n_connection_get_remaining_early_data_size(conn, &bytes));
805 EXPECT_EQUAL(bytes, limit);
806
807 conn->early_data_state = S2N_EARLY_DATA_NOT_REQUESTED;
808 EXPECT_SUCCESS(s2n_connection_get_remaining_early_data_size(conn, &bytes));
809 EXPECT_EQUAL(bytes, 0);
810
811 conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
812 EXPECT_SUCCESS(s2n_connection_get_remaining_early_data_size(conn, &bytes));
813 EXPECT_EQUAL(bytes, limit);
814
815 conn->early_data_bytes = limit - 1;
816 conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
817 EXPECT_SUCCESS(s2n_connection_get_remaining_early_data_size(conn, &bytes));
818 EXPECT_EQUAL(bytes, 1);
819
820 conn->early_data_bytes = limit;
821 conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
822 EXPECT_SUCCESS(s2n_connection_get_remaining_early_data_size(conn, &bytes));
823 EXPECT_EQUAL(bytes, 0);
824
825 conn->early_data_bytes = limit + 1;
826 conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
827 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_remaining_early_data_size(conn, &bytes),
828 S2N_ERR_MAX_EARLY_DATA_SIZE);
829 EXPECT_EQUAL(bytes, 0);
830
831 EXPECT_SUCCESS(s2n_connection_free(conn));
832 };
833
834 /* Test that all valid early data states successfully report a zero size */
835 {
836 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
837 EXPECT_NOT_NULL(conn);
838
839 uint32_t size = 0;
840 for (s2n_early_data_state state = 0; state < S2N_EARLY_DATA_STATES_COUNT; state++) {
841 conn->early_data_state = state;
842 EXPECT_SUCCESS(s2n_connection_get_remaining_early_data_size(conn, &size));
843 EXPECT_EQUAL(size, 0);
844 }
845
846 EXPECT_SUCCESS(s2n_connection_free(conn));
847 };
848
849 /* Test that if S2N_EARLY_DATA_STATUS_OK, then non-zero size reported */
850 {
851 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
852 EXPECT_NOT_NULL(conn);
853 EXPECT_OK(s2n_append_test_psk_with_early_data(conn, limit, &s2n_tls13_aes_256_gcm_sha384));
854
855 s2n_early_data_status_t reason = 0;
856 uint32_t size = 0;
857 for (s2n_early_data_state state = 0; state < S2N_EARLY_DATA_STATES_COUNT; state++) {
858 conn->early_data_state = state;
859
860 EXPECT_SUCCESS(s2n_connection_get_early_data_status(conn, &reason));
861 EXPECT_SUCCESS(s2n_connection_get_remaining_early_data_size(conn, &size));
862
863 if (reason == S2N_EARLY_DATA_STATUS_OK) {
864 EXPECT_EQUAL(size, limit);
865 } else {
866 EXPECT_EQUAL(size, 0);
867 }
868 }
869
870 EXPECT_SUCCESS(s2n_connection_free(conn));
871 };
872 };
873
874 /* Test s2n_connection_get_max_early_data_size */
875 {
876 /* Safety */
877 {
878 struct s2n_connection conn = { 0 };
879 uint32_t bytes = 0;
880 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_max_early_data_size(&conn, NULL), S2N_ERR_NULL);
881 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_max_early_data_size(NULL, &bytes), S2N_ERR_NULL);
882 EXPECT_EQUAL(bytes, 0);
883 };
884
885 /* Retrieve the limit from the first PSK, or return 0 */
886 {
887 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
888 EXPECT_NOT_NULL(conn);
889
890 const uint32_t limit = 10;
891 uint32_t actual_bytes = limit;
892
893 /* No PSKs: limit is zero */
894 EXPECT_SUCCESS(s2n_connection_get_max_early_data_size(conn, &actual_bytes));
895 EXPECT_EQUAL(actual_bytes, 0);
896
897 /* PSK with zero limit: limit is zero */
898 EXPECT_OK(s2n_append_test_psk_with_early_data(conn, 0, &s2n_tls13_aes_256_gcm_sha384));
899 EXPECT_SUCCESS(s2n_connection_get_max_early_data_size(conn, &actual_bytes));
900 EXPECT_EQUAL(actual_bytes, 0);
901
902 /* Second PSK with non-zero limit: limit is still zero */
903 EXPECT_OK(s2n_append_test_psk_with_early_data(conn, limit, &s2n_tls13_aes_256_gcm_sha384));
904 EXPECT_SUCCESS(s2n_connection_get_max_early_data_size(conn, &actual_bytes));
905 EXPECT_EQUAL(actual_bytes, 0);
906
907 /* First PSK with non-zero limit: limit is non-zero */
908 EXPECT_OK(s2n_psk_parameters_wipe(&conn->psk_params));
909 EXPECT_OK(s2n_append_test_psk_with_early_data(conn, limit, &s2n_tls13_aes_256_gcm_sha384));
910 EXPECT_SUCCESS(s2n_connection_get_max_early_data_size(conn, &actual_bytes));
911 EXPECT_EQUAL(actual_bytes, limit);
912
913 EXPECT_SUCCESS(s2n_connection_free(conn));
914 };
915
916 /* If in server mode, apply the server limit */
917 {
918 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
919 EXPECT_NOT_NULL(conn);
920
921 const uint32_t psk_limit = 10;
922 EXPECT_OK(s2n_append_test_chosen_psk_with_early_data(conn, psk_limit, &s2n_tls13_aes_256_gcm_sha384));
923
924 uint32_t actual_bytes = 0;
925
926 /* server limit is lower, but PSK is external: use PSK limit */
927 EXPECT_EQUAL(conn->psk_params.chosen_psk->type, S2N_PSK_TYPE_EXTERNAL);
928 uint32_t server_limit = psk_limit - 1;
929 EXPECT_SUCCESS(s2n_connection_set_server_max_early_data_size(conn, server_limit));
930 EXPECT_SUCCESS(s2n_connection_get_max_early_data_size(conn, &actual_bytes));
931 EXPECT_EQUAL(actual_bytes, psk_limit);
932
933 conn->psk_params.chosen_psk->type = S2N_PSK_TYPE_RESUMPTION;
934
935 /* server limit is higher: use PSK limit */
936 server_limit = psk_limit + 1;
937 EXPECT_SUCCESS(s2n_connection_set_server_max_early_data_size(conn, server_limit));
938 EXPECT_SUCCESS(s2n_connection_get_max_early_data_size(conn, &actual_bytes));
939 EXPECT_EQUAL(actual_bytes, psk_limit);
940
941 /* server limit is lower and PSK is resumption: use server limit */
942 server_limit = psk_limit - 1;
943 EXPECT_SUCCESS(s2n_connection_set_server_max_early_data_size(conn, server_limit));
944 EXPECT_SUCCESS(s2n_connection_get_max_early_data_size(conn, &actual_bytes));
945 EXPECT_EQUAL(actual_bytes, server_limit);
946
947 EXPECT_SUCCESS(s2n_connection_free(conn));
948 };
949
950 /* If in server mode, fall back to the server limit */
951 {
952 const uint32_t server_limit = 15;
953 uint32_t actual_bytes = 0;
954
955 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
956 EXPECT_NOT_NULL(server_conn);
957
958 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
959 EXPECT_NOT_NULL(client_conn);
960
961 /* No PSKs: use server limit for initial connection */
962 EXPECT_SUCCESS(s2n_connection_set_server_max_early_data_size(server_conn, server_limit));
963 EXPECT_SUCCESS(s2n_connection_get_max_early_data_size(server_conn, &actual_bytes));
964 EXPECT_EQUAL(actual_bytes, server_limit);
965
966 /* Client mode: don't use server limit for initial connection */
967 EXPECT_SUCCESS(s2n_connection_set_server_max_early_data_size(client_conn, server_limit));
968 EXPECT_SUCCESS(s2n_connection_get_max_early_data_size(client_conn, &actual_bytes));
969 EXPECT_EQUAL(actual_bytes, 0);
970
971 /* Negotiated connection: once a connection is negotiated, no PSKs means no early data */
972 server_conn->handshake.handshake_type = NEGOTIATED;
973 EXPECT_SUCCESS(s2n_connection_get_max_early_data_size(server_conn, &actual_bytes));
974 EXPECT_EQUAL(actual_bytes, 0);
975
976 EXPECT_SUCCESS(s2n_connection_free(client_conn));
977 EXPECT_SUCCESS(s2n_connection_free(server_conn));
978 };
979 };
980
981 /* Test s2n_config_set_server_max_early_data_size */
982 {
983 /* Safety */
984 EXPECT_FAILURE_WITH_ERRNO(s2n_config_set_server_max_early_data_size(NULL, 1), S2N_ERR_NULL);
985
986 struct s2n_config *config = s2n_config_new();
987 EXPECT_NOT_NULL(config);
988
989 EXPECT_EQUAL(config->server_max_early_data_size, 0);
990
991 EXPECT_SUCCESS(s2n_config_set_server_max_early_data_size(config, 1));
992 EXPECT_EQUAL(config->server_max_early_data_size, 1);
993
994 EXPECT_SUCCESS(s2n_config_set_server_max_early_data_size(config, UINT32_MAX));
995 EXPECT_EQUAL(config->server_max_early_data_size, UINT32_MAX);
996
997 EXPECT_SUCCESS(s2n_config_free(config));
998 };
999
1000 /* Test s2n_connection_set_server_max_early_data_size */
1001 {
1002 /* Safety */
1003 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_set_server_max_early_data_size(NULL, 1), S2N_ERR_NULL);
1004
1005 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
1006 EXPECT_NOT_NULL(conn);
1007
1008 EXPECT_EQUAL(conn->server_max_early_data_size, 0);
1009 EXPECT_FALSE(conn->server_max_early_data_size_overridden);
1010
1011 EXPECT_SUCCESS(s2n_connection_set_server_max_early_data_size(conn, 1));
1012 EXPECT_EQUAL(conn->server_max_early_data_size, 1);
1013 EXPECT_TRUE(conn->server_max_early_data_size_overridden);
1014
1015 conn->server_max_early_data_size_overridden = false;
1016
1017 EXPECT_SUCCESS(s2n_connection_set_server_max_early_data_size(conn, UINT32_MAX));
1018 EXPECT_EQUAL(conn->server_max_early_data_size, UINT32_MAX);
1019 EXPECT_TRUE(conn->server_max_early_data_size_overridden);
1020
1021 EXPECT_SUCCESS(s2n_connection_free(conn));
1022 };
1023
1024 /* Test s2n_early_data_get_server_max_size */
1025 {
1026 uint32_t result_size = 0;
1027 const uint32_t connection_value = 10;
1028 const uint32_t config_value = 5;
1029
1030 struct s2n_config *config = s2n_config_new();
1031 EXPECT_NOT_NULL(config);
1032 EXPECT_SUCCESS(s2n_config_set_server_max_early_data_size(config, config_value));
1033
1034 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
1035 EXPECT_NOT_NULL(conn);
1036 EXPECT_SUCCESS(s2n_connection_set_server_max_early_data_size(conn, connection_value));
1037
1038 /* Safety */
1039 EXPECT_ERROR_WITH_ERRNO(s2n_early_data_get_server_max_size(NULL, &result_size), S2N_ERR_NULL);
1040 EXPECT_ERROR_WITH_ERRNO(s2n_early_data_get_server_max_size(conn, NULL), S2N_ERR_NULL);
1041
1042 /* No config */
1043 conn->config = NULL;
1044 conn->server_max_early_data_size_overridden = false;
1045 EXPECT_ERROR_WITH_ERRNO(s2n_early_data_get_server_max_size(conn, &result_size), S2N_ERR_NULL);
1046
1047 /* No config, but connection override set */
1048 EXPECT_NULL(conn->config);
1049 conn->server_max_early_data_size_overridden = true;
1050 EXPECT_SUCCESS(s2n_connection_set_server_max_early_data_size(conn, connection_value));
1051 EXPECT_OK(s2n_early_data_get_server_max_size(conn, &result_size));
1052 EXPECT_EQUAL(result_size, connection_value);
1053
1054 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
1055
1056 /* Return config version if override not set */
1057 conn->server_max_early_data_size_overridden = false;
1058 EXPECT_OK(s2n_early_data_get_server_max_size(conn, &result_size));
1059 EXPECT_EQUAL(result_size, config_value);
1060
1061 /* Return connection version if set */
1062 conn->server_max_early_data_size_overridden = true;
1063 EXPECT_OK(s2n_early_data_get_server_max_size(conn, &result_size));
1064 EXPECT_EQUAL(result_size, connection_value);
1065
1066 /* Connection can override with a zero value */
1067 EXPECT_SUCCESS(s2n_connection_set_server_max_early_data_size(conn, 0));
1068 EXPECT_OK(s2n_early_data_get_server_max_size(conn, &result_size));
1069 EXPECT_EQUAL(result_size, 0);
1070
1071 EXPECT_SUCCESS(s2n_config_free(config));
1072 EXPECT_SUCCESS(s2n_connection_free(conn));
1073 };
1074
1075 /* Test s2n_connection_set_server_early_data_context */
1076 {
1077 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
1078 const uint8_t data[] = "hello world";
1079
1080 /* Safety */
1081 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_set_server_early_data_context(NULL, data, 1), S2N_ERR_NULL);
1082 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_set_server_early_data_context(conn, NULL, 1), S2N_ERR_NULL);
1083 EXPECT_EQUAL(conn->server_early_data_context.size, 0);
1084 EXPECT_EQUAL(conn->server_early_data_context.allocated, 0);
1085
1086 /* Set context */
1087 EXPECT_SUCCESS(s2n_connection_set_server_early_data_context(conn, data, sizeof(data)));
1088 EXPECT_EQUAL(conn->server_early_data_context.size, sizeof(data));
1089 EXPECT_BYTEARRAY_EQUAL(conn->server_early_data_context.data, data, sizeof(data));
1090
1091 /* Clear context */
1092 EXPECT_SUCCESS(s2n_connection_set_server_early_data_context(conn, NULL, 0));
1093 EXPECT_EQUAL(conn->server_early_data_context.size, 0);
1094
1095 /* Set context again */
1096 EXPECT_SUCCESS(s2n_connection_set_server_early_data_context(conn, data, 1));
1097 EXPECT_EQUAL(conn->server_early_data_context.size, 1);
1098 EXPECT_BYTEARRAY_EQUAL(conn->server_early_data_context.data, data, 1);
1099
1100 EXPECT_SUCCESS(s2n_connection_free(conn));
1101 };
1102
1103 /* Test s2n_early_data_record_bytes */
1104 {
1105 /* Safety check */
1106 EXPECT_ERROR_WITH_ERRNO(s2n_early_data_record_bytes(NULL, 1), S2N_ERR_NULL);
1107
1108 const uint32_t limit = 10;
1109
1110 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
1111 EXPECT_NOT_NULL(conn);
1112 EXPECT_OK(s2n_append_test_psk_with_early_data(conn, limit, &s2n_tls13_aes_256_gcm_sha384));
1113
1114 /* If early data not expected, bytes are not recorded */
1115 conn->early_data_bytes = 0;
1116 EXPECT_OK(s2n_early_data_record_bytes(conn, limit + 1));
1117 EXPECT_EQUAL(conn->early_data_bytes, 0);
1118
1119 EXPECT_SUCCESS(s2n_connection_set_early_data_expected(conn));
1120
1121 /* Bytes are recorded */
1122 {
1123 conn->early_data_bytes = 0;
1124 EXPECT_OK(s2n_early_data_record_bytes(conn, 1));
1125 EXPECT_EQUAL(conn->early_data_bytes, 1);
1126
1127 conn->early_data_bytes = 1;
1128 EXPECT_OK(s2n_early_data_record_bytes(conn, 1));
1129 EXPECT_EQUAL(conn->early_data_bytes, 2);
1130 };
1131
1132 /* Error if exceeds max_early_data_size */
1133 {
1134 conn->early_data_bytes = 0;
1135 EXPECT_OK(s2n_early_data_record_bytes(conn, limit));
1136 EXPECT_EQUAL(conn->early_data_bytes, limit);
1137
1138 conn->early_data_bytes = 1;
1139 EXPECT_ERROR_WITH_ERRNO(s2n_early_data_record_bytes(conn, limit), S2N_ERR_MAX_EARLY_DATA_SIZE);
1140 EXPECT_EQUAL(conn->early_data_bytes, limit + 1);
1141 };
1142
1143 /* Prevents early_data_bytes from overflowing */
1144 {
1145 conn->early_data_bytes = UINT64_MAX - 2;
1146 EXPECT_ERROR_WITH_ERRNO(s2n_early_data_record_bytes(conn, 1), S2N_ERR_MAX_EARLY_DATA_SIZE);
1147 EXPECT_EQUAL(conn->early_data_bytes, UINT64_MAX - 1);
1148
1149 conn->early_data_bytes = UINT64_MAX - 1;
1150 EXPECT_ERROR_WITH_ERRNO(s2n_early_data_record_bytes(conn, 1), S2N_ERR_MAX_EARLY_DATA_SIZE);
1151 EXPECT_EQUAL(conn->early_data_bytes, UINT64_MAX);
1152
1153 conn->early_data_bytes = UINT64_MAX;
1154 EXPECT_ERROR_WITH_ERRNO(s2n_early_data_record_bytes(conn, 1), S2N_ERR_INTEGER_OVERFLOW);
1155 EXPECT_EQUAL(conn->early_data_bytes, UINT64_MAX);
1156 };
1157
1158 /* Zero bytes are "recorded" */
1159 {
1160 conn->early_data_bytes = 0;
1161 EXPECT_OK(s2n_early_data_record_bytes(conn, 0));
1162 EXPECT_EQUAL(conn->early_data_bytes, 0);
1163
1164 conn->early_data_bytes = limit;
1165 EXPECT_OK(s2n_early_data_record_bytes(conn, 0));
1166 EXPECT_EQUAL(conn->early_data_bytes, limit);
1167
1168 conn->early_data_bytes = limit + 1;
1169 EXPECT_ERROR_WITH_ERRNO(s2n_early_data_record_bytes(conn, 0), S2N_ERR_MAX_EARLY_DATA_SIZE);
1170 EXPECT_EQUAL(conn->early_data_bytes, limit + 1);
1171 };
1172
1173 /* Negative bytes are "recorded" */
1174 {
1175 conn->early_data_bytes = 0;
1176 EXPECT_OK(s2n_early_data_record_bytes(conn, -1));
1177 EXPECT_EQUAL(conn->early_data_bytes, 0);
1178
1179 conn->early_data_bytes = limit / 2;
1180 EXPECT_OK(s2n_early_data_record_bytes(conn, -1));
1181 EXPECT_EQUAL(conn->early_data_bytes, limit / 2);
1182
1183 conn->early_data_bytes = limit;
1184 EXPECT_OK(s2n_early_data_record_bytes(conn, -1));
1185 EXPECT_EQUAL(conn->early_data_bytes, limit);
1186
1187 /* Unlike with other inputs, does not return an error and set S2N_ERR_MAX_EARLY_DATA_SIZE.
1188 * That would overwrite whatever send error caused the -1 result.
1189 */
1190 conn->early_data_bytes = limit + 1;
1191 EXPECT_OK(s2n_early_data_record_bytes(conn, -1));
1192 EXPECT_EQUAL(conn->early_data_bytes, limit + 1);
1193 };
1194
1195 EXPECT_SUCCESS(s2n_connection_free(conn));
1196 };
1197
1198 /* Test s2n_early_data_validate_send */
1199 {
1200 /* Safety check */
1201 EXPECT_ERROR_WITH_ERRNO(s2n_early_data_validate_send(NULL, 1), S2N_ERR_NULL);
1202
1203 const uint32_t limit = 10;
1204 struct s2n_connection *valid_connection = s2n_connection_new(S2N_CLIENT);
1205 EXPECT_NOT_NULL(valid_connection);
1206 EXPECT_OK(s2n_append_test_psk_with_early_data(valid_connection, limit, &s2n_tls13_aes_256_gcm_sha384));
1207 EXPECT_SUCCESS(s2n_connection_set_early_data_expected(valid_connection));
1208 valid_connection->mode = S2N_CLIENT;
1209 valid_connection->early_data_state = S2N_EARLY_DATA_REQUESTED;
1210 valid_connection->early_data_bytes = 0;
1211
1212 /* Fails if server */
1213 struct s2n_connection conn = *valid_connection;
1214 conn.mode = S2N_SERVER;
1215 EXPECT_ERROR_WITH_ERRNO(s2n_early_data_validate_send(&conn, 1), S2N_ERR_EARLY_DATA_NOT_ALLOWED);
1216
1217 /* Fails if wrong state */
1218 conn = *valid_connection;
1219 conn.early_data_state = S2N_END_OF_EARLY_DATA;
1220 EXPECT_ERROR_WITH_ERRNO(s2n_early_data_validate_send(&conn, 1), S2N_ERR_EARLY_DATA_NOT_ALLOWED);
1221
1222 /* Passes for S2N_EARLY_DATA_REQUESTED */
1223 conn = *valid_connection;
1224 conn.early_data_state = S2N_EARLY_DATA_REQUESTED;
1225 EXPECT_OK(s2n_early_data_validate_send(&conn, 1));
1226
1227 /* Passes for S2N_EARLY_DATA_ACCEPTED */
1228 conn = *valid_connection;
1229 conn.early_data_state = S2N_EARLY_DATA_ACCEPTED;
1230 EXPECT_OK(s2n_early_data_validate_send(&conn, 1));
1231
1232 /* Fails if too much data */
1233 conn = *valid_connection;
1234 conn.early_data_bytes = limit;
1235 EXPECT_ERROR_WITH_ERRNO(s2n_early_data_validate_send(&conn, 1), S2N_ERR_MAX_EARLY_DATA_SIZE);
1236
1237 /* No-op if actually Application Data */
1238 conn = *valid_connection;
1239 conn.early_data_bytes = limit;
1240 conn.handshake.handshake_type = NEGOTIATED;
1241 while (s2n_conn_get_current_message_type(&conn) != APPLICATION_DATA) {
1242 conn.handshake.message_number++;
1243 }
1244 EXPECT_OK(s2n_early_data_validate_send(&conn, 1));
1245
1246 EXPECT_SUCCESS(s2n_connection_free(valid_connection));
1247 };
1248
1249 /* Test s2n_early_data_validate_recv */
1250 {
1251 /* Safety check */
1252 EXPECT_ERROR_WITH_ERRNO(s2n_early_data_validate_recv(NULL), S2N_ERR_NULL);
1253
1254 const uint32_t limit = 10;
1255 struct s2n_connection *valid_connection = s2n_connection_new(S2N_SERVER);
1256 EXPECT_NOT_NULL(valid_connection);
1257 EXPECT_OK(s2n_append_test_psk_with_early_data(valid_connection, limit, &s2n_tls13_aes_256_gcm_sha384));
1258 EXPECT_SUCCESS(s2n_connection_set_early_data_expected(valid_connection));
1259 valid_connection->early_data_state = S2N_EARLY_DATA_ACCEPTED;
1260 valid_connection->early_data_bytes = 0;
1261 valid_connection->actual_protocol_version = S2N_TLS13;
1262 EXPECT_OK(s2n_conn_choose_state_machine(valid_connection, S2N_TLS13));
1263 valid_connection->handshake.handshake_type = NEGOTIATED | WITH_EARLY_DATA;
1264 while (s2n_conn_get_current_message_type(valid_connection) != END_OF_EARLY_DATA) {
1265 valid_connection->handshake.message_number++;
1266 }
1267
1268 /* Passes if everything valid */
1269 struct s2n_connection conn = *valid_connection;
1270 EXPECT_OK(s2n_early_data_validate_recv(&conn));
1271
1272 /* Fails if client */
1273 conn = *valid_connection;
1274 conn.mode = S2N_CLIENT;
1275 EXPECT_ERROR_WITH_ERRNO(s2n_early_data_validate_recv(&conn), S2N_ERR_EARLY_DATA_NOT_ALLOWED);
1276
1277 /* Fails if wrong state */
1278 conn = *valid_connection;
1279 conn.early_data_state = S2N_END_OF_EARLY_DATA;
1280 EXPECT_ERROR_WITH_ERRNO(s2n_early_data_validate_recv(&conn), S2N_ERR_EARLY_DATA_NOT_ALLOWED);
1281
1282 /* Fails if wrong handshake message */
1283 conn = *valid_connection;
1284 conn.handshake.message_number--;
1285 EXPECT_ERROR_WITH_ERRNO(s2n_early_data_validate_recv(&conn), S2N_ERR_EARLY_DATA_NOT_ALLOWED);
1286
1287 /* Fails for S2N_EARLY_DATA_REQUESTED */
1288 conn = *valid_connection;
1289 conn.early_data_state = S2N_EARLY_DATA_REQUESTED;
1290 EXPECT_ERROR_WITH_ERRNO(s2n_early_data_validate_recv(&conn), S2N_ERR_EARLY_DATA_NOT_ALLOWED);
1291
1292 /* Passes for S2N_EARLY_DATA_ACCEPTED */
1293 conn = *valid_connection;
1294 conn.early_data_state = S2N_EARLY_DATA_ACCEPTED;
1295 EXPECT_OK(s2n_early_data_validate_recv(&conn));
1296
1297 /* No-op if actually Application Data */
1298 conn = *valid_connection;
1299 conn.early_data_bytes = limit;
1300 conn.handshake.handshake_type = NEGOTIATED;
1301 while (s2n_conn_get_current_message_type(&conn) != APPLICATION_DATA) {
1302 conn.handshake.message_number++;
1303 }
1304 EXPECT_OK(s2n_early_data_validate_recv(&conn));
1305
1306 EXPECT_SUCCESS(s2n_connection_free(valid_connection));
1307 };
1308
1309 /* Test s2n_config_set_early_data_cb */
1310 {
1311 struct s2n_config *config = s2n_config_new();
1312
1313 /* Safety */
1314 EXPECT_FAILURE_WITH_ERRNO(s2n_config_set_early_data_cb(NULL, s2n_test_early_data_cb), S2N_ERR_NULL);
1315 EXPECT_EQUAL(config->early_data_cb, 0);
1316
1317 /* Set callback */
1318 EXPECT_SUCCESS(s2n_config_set_early_data_cb(config, s2n_test_early_data_cb));
1319 EXPECT_EQUAL(config->early_data_cb, s2n_test_early_data_cb);
1320
1321 /* Clear callback */
1322 EXPECT_SUCCESS(s2n_config_set_early_data_cb(config, NULL));
1323 EXPECT_EQUAL(config->early_data_cb, NULL);
1324
1325 EXPECT_SUCCESS(s2n_config_free(config));
1326 };
1327
1328 /* Test s2n_offered_early_data_get_context and s2n_offered_early_data_get_context_length */
1329 {
1330 struct s2n_offered_early_data early_data = { 0 };
1331 const uint8_t context[] = "psk context";
1332 const uint8_t empty_context[sizeof(context)] = { 0 };
1333 uint8_t actual_context[sizeof(context)] = { 0 };
1334 uint16_t length = 1;
1335
1336 /* Safety */
1337 {
1338 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_early_data_get_context_length(NULL, &length), S2N_ERR_NULL);
1339 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_early_data_get_context(NULL, actual_context, 1), S2N_ERR_NULL);
1340
1341 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_early_data_get_context_length(&early_data, NULL), S2N_ERR_NULL);
1342 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_early_data_get_context(&early_data, NULL, 1), S2N_ERR_NULL);
1343
1344 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_early_data_get_context_length(&early_data, &length), S2N_ERR_NULL);
1345 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_early_data_get_context(&early_data, actual_context, 1), S2N_ERR_NULL);
1346
1347 early_data.conn = s2n_connection_new(S2N_SERVER);
1348 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_early_data_get_context_length(&early_data, &length), S2N_ERR_NULL);
1349 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_early_data_get_context(&early_data, actual_context, 1), S2N_ERR_NULL);
1350 EXPECT_SUCCESS(s2n_connection_free(early_data.conn));
1351 };
1352
1353 /* No context */
1354 {
1355 early_data.conn = s2n_connection_new(S2N_SERVER);
1356 DEFER_CLEANUP(struct s2n_psk *test_psk = s2n_test_psk_new(early_data.conn), s2n_psk_free);
1357 early_data.conn->psk_params.chosen_psk = test_psk;
1358
1359 EXPECT_SUCCESS(s2n_offered_early_data_get_context_length(&early_data, &length));
1360 EXPECT_EQUAL(length, 0);
1361
1362 EXPECT_SUCCESS(s2n_offered_early_data_get_context(&early_data, actual_context, 0));
1363 EXPECT_BYTEARRAY_EQUAL(actual_context, empty_context, sizeof(empty_context));
1364
1365 EXPECT_SUCCESS(s2n_offered_early_data_get_context(&early_data, actual_context, sizeof(actual_context)));
1366 EXPECT_BYTEARRAY_EQUAL(actual_context, empty_context, sizeof(empty_context));
1367
1368 EXPECT_SUCCESS(s2n_connection_free(early_data.conn));
1369 };
1370
1371 /* Context */
1372 {
1373 early_data.conn = s2n_connection_new(S2N_SERVER);
1374 DEFER_CLEANUP(struct s2n_psk *test_psk = s2n_test_psk_new(early_data.conn), s2n_psk_free);
1375 EXPECT_SUCCESS(s2n_psk_set_early_data_context(test_psk, context, sizeof(context)));
1376 early_data.conn->psk_params.chosen_psk = test_psk;
1377
1378 EXPECT_SUCCESS(s2n_offered_early_data_get_context_length(&early_data, &length));
1379 EXPECT_EQUAL(length, sizeof(context));
1380
1381 EXPECT_SUCCESS(s2n_offered_early_data_get_context(&early_data, actual_context, sizeof(actual_context)));
1382 EXPECT_BYTEARRAY_EQUAL(actual_context, context, sizeof(context));
1383
1384 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_early_data_get_context(&early_data, actual_context, 1),
1385 S2N_ERR_INSUFFICIENT_MEM_SIZE);
1386
1387 EXPECT_SUCCESS(s2n_connection_free(early_data.conn));
1388 };
1389 };
1390
1391 /* Test s2n_offered_early_data_reject */
1392 {
1393 struct s2n_offered_early_data early_data = { .conn = NULL };
1394
1395 /* Safety */
1396 {
1397 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_early_data_reject(NULL), S2N_ERR_NULL);
1398 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_early_data_reject(&early_data), S2N_ERR_NULL);
1399 };
1400
1401 /* Reject early data */
1402 {
1403 early_data.conn = s2n_connection_new(S2N_SERVER);
1404 early_data.conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
1405 EXPECT_SUCCESS(s2n_offered_early_data_reject(&early_data));
1406 EXPECT_EQUAL(early_data.conn->early_data_state, S2N_EARLY_DATA_REJECTED);
1407 EXPECT_SUCCESS(s2n_connection_free(early_data.conn));
1408 };
1409 };
1410
1411 /* Test s2n_offered_early_data_accept */
1412 {
1413 struct s2n_offered_early_data early_data = { .conn = NULL };
1414
1415 /* Safety */
1416 {
1417 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_early_data_accept(NULL), S2N_ERR_NULL);
1418 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_early_data_accept(&early_data), S2N_ERR_NULL);
1419 };
1420
1421 /* Accept early data */
1422 {
1423 early_data.conn = s2n_connection_new(S2N_SERVER);
1424 early_data.conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
1425 EXPECT_SUCCESS(s2n_offered_early_data_accept(&early_data));
1426 EXPECT_EQUAL(early_data.conn->early_data_state, S2N_EARLY_DATA_ACCEPTED);
1427 EXPECT_SUCCESS(s2n_connection_free(early_data.conn));
1428 };
1429 };
1430
1431 END_TEST();
1432 }