Debian Codegraph @

crt/s2n/tests/unit/s2n_client_psk_extension_test.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include "s2n_test.h"
17 #include "testlib/s2n_testlib.h"
18
19 /* Include source to test static methods. */
20 #include "tls/extensions/s2n_client_psk.c"
21
22 #define TEST_BYTES 0x01, 0xFF, 0x23
23 #define TEST_BYTES_SIZE 0x00, 0x03
24
25 #define TEST_BYTES_2 0x0A, 0x0B, 0x0C
26 #define TEST_BYTES_SIZE_2 0x00, 0x03
27
28 #define MILLIS_TO_NANOS(millis) (millis * (uint64_t) ONE_MILLISEC_IN_NANOS)
29
30 #define BINDER_SIZE 32
31
32 struct s2n_psk_test_case {
33 s2n_hmac_algorithm hmac_alg;
34 uint8_t hash_size;
35 const uint8_t *identity;
36 size_t identity_size;
37 };
38
39 static int s2n_test_select_psk_identity_callback(struct s2n_connection *conn, void *context,
40 struct s2n_offered_psk_list *psk_identity_list)
41 {
42 uint16_t *wire_index_choice = (uint16_t *) context;
43
44 struct s2n_offered_psk offered_psk = { 0 };
45 uint16_t idx = 0;
46 while (s2n_offered_psk_list_has_next(psk_identity_list)) {
47 POSIX_GUARD(s2n_offered_psk_list_next(psk_identity_list, &offered_psk));
48 if (idx == *wire_index_choice) {
49 POSIX_GUARD(s2n_offered_psk_list_choose_psk(psk_identity_list, &offered_psk));
50 break;
51 }
52 idx++;
53 };
54 return S2N_SUCCESS;
55 }
56
57 static int s2n_test_error_select_psk_identity_callback(struct s2n_connection *conn, void *context,
58 struct s2n_offered_psk_list *psk_identity_list)
59 {
60 POSIX_BAIL(S2N_ERR_UNIMPLEMENTED);
61 }
62
63 static S2N_RESULT s2n_write_test_identity(struct s2n_stuffer *out, struct s2n_blob *identity)
64 {
65 RESULT_GUARD_POSIX(s2n_stuffer_write_uint16(out, identity->size));
66 RESULT_GUARD_POSIX(s2n_stuffer_write(out, identity));
67 RESULT_GUARD_POSIX(s2n_stuffer_write_uint32(out, 0));
68 return S2N_RESULT_OK;
69 }
70
71 static int mock_time(void *data, uint64_t *nanoseconds)
72 {
73 *nanoseconds = MILLIS_TO_NANOS(500);
74 return S2N_SUCCESS;
75 }
76
77 static S2N_RESULT s2n_setup_encrypted_ticket(struct s2n_connection *conn, struct s2n_stuffer *output)
78 {
79 conn->tls13_ticket_fields = (struct s2n_ticket_fields){ 0 };
80 uint8_t test_secret_data[] = "test secret";
81 RESULT_GUARD_POSIX(s2n_alloc(&conn->tls13_ticket_fields.session_secret, sizeof(test_secret_data)));
82 RESULT_CHECKED_MEMCPY(conn->tls13_ticket_fields.session_secret.data, test_secret_data, sizeof(test_secret_data));
83
84 struct s2n_ticket_key *key = s2n_get_ticket_encrypt_decrypt_key(conn->config);
85 RESULT_ENSURE(key != NULL, S2N_ERR_NO_TICKET_ENCRYPT_DECRYPT_KEY);
86
87 /* Create a valid resumption psk identity */
88 RESULT_GUARD(s2n_resume_encrypt_session_ticket(conn, key, output));
89
90 output->blob.size = s2n_stuffer_data_available(output);
91
92 return S2N_RESULT_OK;
93 }
94
95 int main(int argc, char **argv)
96 {
97 BEGIN_TEST();
98
99 uint8_t test_bytes_data[] = { TEST_BYTES };
100 uint8_t test_bytes_data_2[] = { TEST_BYTES_2 };
101 uint8_t test_identity[] = "test identity";
102 uint8_t test_identity_2[] = "another identity";
103 uint8_t test_identity_3[] = "identity 3";
104 uint8_t test_secret[] = "test secret";
105
106 uint8_t single_wire_identity[] = {
107 TEST_BYTES_SIZE, /* identity size */
108 TEST_BYTES, /* identity */
109 0x00, 0x00, 0x00, 0x00, /* ticket_age */
110 };
111
112 uint8_t wire_identities[] = {
113 0x00, 0x01, /* identity size */
114 0x01, /* identity */
115 0x00, 0x00, 0x00, 0x00, /* ticket_age */
116
117 0x00, 0x01, /* identity size */
118 0xFF, /* identity */
119 0x00, 0x00, 0x00, 0x00, /* ticket_age */
120
121 TEST_BYTES_SIZE, /* identity size */
122 TEST_BYTES, /* identity */
123 0x00, 0x00, 0x00, 0x00, /* ticket_age */
124
125 TEST_BYTES_SIZE, /* identity size */
126 TEST_BYTES, /* identity */
127 0x00, 0x00, 0x00, 0x00, /* ticket_age */
128
129 0x00, 0x02, /* identity size */
130 0x00, 0x01, /* identity */
131 0x00, 0x00, 0x00, 0x00, /* ticket_age */
132 };
133
134 /* Test: s2n_client_psk_is_missing */
135 {
136 struct s2n_connection *conn = NULL;
137 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
138
139 /* Okay if early data not requested */
140 conn->early_data_state = S2N_EARLY_DATA_NOT_REQUESTED;
141 EXPECT_SUCCESS(s2n_client_psk_extension.if_missing(conn));
142
143 /**
144 *= https://www.rfc-editor.org/rfc/rfc8446#section-4.2.10
145 *= type=test
146 *# When a PSK is used and early data is allowed for that PSK, the client
147 *# can send Application Data in its first flight of messages. If the
148 *# client opts to do so, it MUST supply both the "pre_shared_key" and
149 *# "early_data" extensions.
150 */
151 conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
152 EXPECT_FAILURE_WITH_ERRNO(s2n_client_psk_extension.if_missing(conn), S2N_ERR_UNSUPPORTED_EXTENSION);
153
154 EXPECT_SUCCESS(s2n_connection_free(conn));
155 };
156
157 /* Test: s2n_client_psk_should_send */
158 {
159 struct s2n_psk *psk = NULL;
160
161 struct s2n_connection *conn = NULL;
162 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
163
164 EXPECT_FALSE(s2n_client_psk_extension.should_send(NULL));
165
166 conn->actual_protocol_version = S2N_TLS12;
167 EXPECT_FALSE(s2n_client_psk_extension.should_send(conn));
168 conn->actual_protocol_version = S2N_TLS13;
169 EXPECT_FALSE(s2n_client_psk_extension.should_send(conn));
170
171 EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &psk));
172
173 /* If send is called with a NULL stuffer, it will fail.
174 * So a failure indicates that send was called.
175 */
176 conn->actual_protocol_version = S2N_TLS12;
177 EXPECT_SUCCESS(s2n_extension_send(&s2n_client_psk_extension, conn, NULL));
178 conn->actual_protocol_version = S2N_TLS13;
179 EXPECT_FAILURE(s2n_extension_send(&s2n_client_psk_extension, conn, NULL));
180
181 /* Only send the extension after a retry if at least one PSK matches the cipher suite */
182 {
183 conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
184 const s2n_hmac_algorithm matching_hmac_alg = conn->secure->cipher_suite->prf_alg;
185 const s2n_hmac_algorithm different_hmac_alg = conn->secure->cipher_suite->prf_alg + 1;
186
187 /* Do send if the PSK does NOT match the cipher suite, but this is NOT a retry */
188 conn->handshake.handshake_type = INITIAL;
189 psk->hmac_alg = different_hmac_alg;
190 EXPECT_TRUE(s2n_client_psk_extension.should_send(conn));
191
192 /* Do send if the PSK matches the cipher suite */
193 conn->handshake.handshake_type = HELLO_RETRY_REQUEST;
194 psk->hmac_alg = matching_hmac_alg;
195 EXPECT_TRUE(s2n_client_psk_extension.should_send(conn));
196
197 /* Do NOT send if the PSK does NOT match the cipher suite */
198 conn->handshake.handshake_type = HELLO_RETRY_REQUEST;
199 psk->hmac_alg = different_hmac_alg;
200 EXPECT_FALSE(s2n_client_psk_extension.should_send(conn));
201
202 /* Do send if there are two PSKs, and one matches the cipher suite */
203 conn->handshake.handshake_type = HELLO_RETRY_REQUEST;
204 psk->hmac_alg = different_hmac_alg;
205 EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &psk));
206 psk->hmac_alg = matching_hmac_alg;
207 EXPECT_TRUE(s2n_client_psk_extension.should_send(conn));
208 };
209
210 EXPECT_SUCCESS(s2n_connection_free(conn));
211 };
212
213 /* Test: s2n_generate_obfuscated_ticket_age */
214 {
215 uint32_t output = 0;
216 uint64_t current_time = 0;
217 struct s2n_psk psk = { 0 };
218 psk.type = S2N_PSK_TYPE_RESUMPTION;
219
220 /* Current time is smaller than ticket issue time */
221 {
222 current_time = 0;
223 psk.ticket_issue_time = 10;
224 EXPECT_ERROR_WITH_ERRNO(s2n_generate_obfuscated_ticket_age(&psk, current_time, &output), S2N_ERR_SAFETY);
225 };
226
227 /* Ticket age is too large to fit in a uint32_t */
228 {
229 current_time = UINT64_MAX;
230 psk.ticket_issue_time = 0;
231 EXPECT_ERROR_WITH_ERRNO(s2n_generate_obfuscated_ticket_age(&psk, current_time, &output), S2N_ERR_SAFETY);
232 };
233
234 /* External psk */
235 {
236 psk.type = S2N_PSK_TYPE_EXTERNAL;
237 EXPECT_OK(s2n_generate_obfuscated_ticket_age(&psk, current_time, &output));
238 EXPECT_EQUAL(output, 0);
239 };
240
241 struct {
242 uint64_t current_time;
243 uint64_t ticket_issue_time;
244 uint32_t ticket_age_add;
245 uint32_t expected_output;
246 } test_cases[] = {
247 { .current_time = MILLIS_TO_NANOS(50), .ticket_issue_time = 0, .ticket_age_add = 50, .expected_output = 100 },
248 { .current_time = MILLIS_TO_NANOS(500), .ticket_issue_time = 0, .ticket_age_add = 50, .expected_output = 550 },
249 { .current_time = MILLIS_TO_NANOS(UINT32_MAX), .ticket_issue_time = 0, .ticket_age_add = 1, .expected_output = 0 },
250 { .current_time = MILLIS_TO_NANOS(UINT32_MAX), .ticket_issue_time = 0, .ticket_age_add = 2, .expected_output = 1 },
251 { .current_time = MILLIS_TO_NANOS(UINT32_MAX), .ticket_issue_time = 0, .ticket_age_add = 50, .expected_output = 49 },
252 { .current_time = MILLIS_TO_NANOS(0), .ticket_issue_time = 0, .ticket_age_add = 50, .expected_output = 50 },
253 };
254
255 for (size_t i = 0; i < s2n_array_len(test_cases); i++) {
256 output = 0;
257 current_time = test_cases[i].current_time;
258 psk.ticket_issue_time = test_cases[i].ticket_issue_time;
259 psk.ticket_age_add = test_cases[i].ticket_age_add;
260 psk.type = S2N_PSK_TYPE_RESUMPTION;
261
262 EXPECT_OK(s2n_generate_obfuscated_ticket_age(&psk, current_time, &output));
263 EXPECT_EQUAL(output, test_cases[i].expected_output);
264 }
265 };
266
267 /* Test: s2n_client_psk_send */
268 {
269 /* Send a single PSK identity */
270 {
271 struct s2n_stuffer out = { 0 };
272 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&out, 0));
273
274 struct s2n_connection *conn = NULL;
275 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
276
277 struct s2n_psk *psk = NULL;
278 EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &psk));
279 EXPECT_OK(s2n_psk_init(psk, S2N_PSK_TYPE_EXTERNAL));
280 EXPECT_SUCCESS(s2n_psk_set_identity(psk, test_identity, sizeof(test_identity)));
281 psk->hmac_alg = S2N_HMAC_SHA384;
282
283 EXPECT_SUCCESS(s2n_client_psk_extension.send(conn, &out));
284
285 uint32_t offered_psks_size = 0;
286 EXPECT_OK(s2n_psk_parameters_offered_psks_size(&conn->psk_params, &offered_psks_size));
287 EXPECT_EQUAL(offered_psks_size, s2n_stuffer_data_available(&out));
288
289 uint16_t identity_list_size = 0;
290 EXPECT_SUCCESS(s2n_stuffer_read_uint16(&out, &identity_list_size));
291
292 uint16_t identity_size = 0;
293 EXPECT_SUCCESS(s2n_stuffer_read_uint16(&out, &identity_size));
294 EXPECT_EQUAL(identity_size, sizeof(test_identity));
295
296 uint8_t *identity_data = NULL;
297 EXPECT_NOT_NULL(identity_data = s2n_stuffer_raw_read(&out, identity_size));
298 EXPECT_BYTEARRAY_EQUAL(identity_data, test_identity, sizeof(test_identity));
299
300 uint32_t obfuscated_ticket_age = 0;
301 EXPECT_SUCCESS(s2n_stuffer_read_uint32(&out, &obfuscated_ticket_age));
302 EXPECT_EQUAL(obfuscated_ticket_age, 0);
303
304 EXPECT_EQUAL(s2n_stuffer_data_available(&out),
305 SHA384_DIGEST_LENGTH /* binder size */
306 + sizeof(uint8_t) /* size of binder size */
307 + sizeof(uint16_t)); /* size of binder list size */
308
309 EXPECT_SUCCESS(s2n_connection_free(conn));
310 EXPECT_SUCCESS(s2n_stuffer_free(&out));
311 };
312
313 /* Send multiple PSK identities */
314 {
315 struct s2n_stuffer out = { 0 };
316 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&out, 0));
317
318 struct s2n_connection *conn = NULL;
319 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
320
321 struct s2n_psk_test_case test_cases[] = {
322 { .hmac_alg = S2N_HMAC_SHA224, .hash_size = SHA224_DIGEST_LENGTH, .identity = test_identity, .identity_size = sizeof(test_identity) },
323 { .hmac_alg = S2N_HMAC_SHA384, .hash_size = SHA384_DIGEST_LENGTH, .identity = test_identity_2, .identity_size = sizeof(test_identity_2) },
324 };
325
326 uint16_t binder_list_size = 0;
327 for (size_t i = 0; i < s2n_array_len(test_cases); i++) {
328 struct s2n_psk *psk = NULL;
329 EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &psk));
330 EXPECT_OK(s2n_psk_init(psk, S2N_PSK_TYPE_EXTERNAL));
331 EXPECT_SUCCESS(s2n_psk_set_identity(psk, test_cases[i].identity, test_cases[i].identity_size));
332 psk->hmac_alg = test_cases[i].hmac_alg;
333
334 binder_list_size += test_cases[i].hash_size
335 + sizeof(uint8_t) /* size of binder size */;
336 }
337
338 EXPECT_SUCCESS(s2n_client_psk_extension.send(conn, &out));
339
340 uint32_t offered_psks_size = 0;
341 EXPECT_OK(s2n_psk_parameters_offered_psks_size(&conn->psk_params, &offered_psks_size));
342 EXPECT_EQUAL(offered_psks_size, s2n_stuffer_data_available(&out));
343
344 uint16_t identity_list_size = 0;
345 EXPECT_SUCCESS(s2n_stuffer_read_uint16(&out, &identity_list_size));
346
347 for (size_t i = 0; i < s2n_array_len(test_cases); i++) {
348 uint16_t identity_size = 0;
349 EXPECT_SUCCESS(s2n_stuffer_read_uint16(&out, &identity_size));
350 EXPECT_EQUAL(identity_size, test_cases[i].identity_size);
351
352 uint8_t *identity_data = NULL;
353 EXPECT_NOT_NULL(identity_data = s2n_stuffer_raw_read(&out, identity_size));
354 EXPECT_BYTEARRAY_EQUAL(identity_data, test_cases[i].identity, test_cases[i].identity_size);
355
356 uint32_t obfuscated_ticket_age = 0;
357 EXPECT_SUCCESS(s2n_stuffer_read_uint32(&out, &obfuscated_ticket_age));
358 EXPECT_EQUAL(obfuscated_ticket_age, 0);
359 }
360
361 EXPECT_EQUAL(s2n_stuffer_data_available(&out),
362 binder_list_size /* binder list size */
363 + sizeof(uint16_t)); /* size of binder list size */
364
365 EXPECT_SUCCESS(s2n_connection_free(conn));
366 EXPECT_SUCCESS(s2n_stuffer_free(&out));
367 };
368
369 /* Send a resumption psk identity */
370 {
371 struct s2n_stuffer out = { 0 };
372 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&out, 0));
373
374 struct s2n_connection *conn = NULL;
375 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
376
377 struct s2n_config *config = s2n_config_new();
378 EXPECT_NOT_NULL(config);
379 EXPECT_SUCCESS(s2n_config_set_wall_clock(config, mock_time, NULL));
380 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
381
382 struct s2n_psk *psk = NULL;
383 EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &psk));
384 EXPECT_OK(s2n_psk_init(psk, S2N_PSK_TYPE_RESUMPTION));
385 EXPECT_SUCCESS(s2n_psk_set_identity(psk, test_identity, sizeof(test_identity)));
386 psk->hmac_alg = S2N_HMAC_SHA384;
387 psk->ticket_age_add = 50;
388 psk->ticket_issue_time = 0;
389
390 EXPECT_SUCCESS(s2n_client_psk_extension.send(conn, &out));
391
392 EXPECT_SUCCESS(s2n_stuffer_skip_read(&out, sizeof(uint16_t) /* identity_list_size */ + sizeof(uint16_t) /* identity_size */ + sizeof(test_identity)));
393
394 uint32_t obfuscated_ticket_age = 0;
395 EXPECT_SUCCESS(s2n_stuffer_read_uint32(&out, &obfuscated_ticket_age));
396 EXPECT_TRUE(obfuscated_ticket_age == 550);
397
398 EXPECT_SUCCESS(s2n_connection_free(conn));
399 EXPECT_SUCCESS(s2n_config_free(config));
400 EXPECT_SUCCESS(s2n_stuffer_free(&out));
401 };
402
403 /* On the second ClientHello after a retry request,
404 * do not send any PSKs that do not match the cipher suite.
405 *
406 *= https://www.rfc-editor.org/rfc/rfc8446#section-4.1.4
407 *= type=test
408 *# In addition, in its updated ClientHello, the client SHOULD NOT offer
409 *# any pre-shared keys associated with a hash other than that of the
410 *# selected cipher suite.
411 */
412 {
413 struct s2n_stuffer out = { 0 };
414 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&out, 0));
415
416 const struct s2n_psk_test_case matching_psk = {
417 .hmac_alg = S2N_HMAC_SHA384,
418 .hash_size = SHA384_DIGEST_LENGTH,
419 .identity = test_identity,
420 .identity_size = sizeof(test_identity)
421 };
422 const struct s2n_psk_test_case non_matching_psk = {
423 .hmac_alg = S2N_HMAC_SHA224,
424 .hash_size = SHA224_DIGEST_LENGTH,
425 .identity = test_identity,
426 .identity_size = sizeof(test_identity)
427 };
428 struct s2n_psk_test_case test_cases[] = { matching_psk, non_matching_psk };
429
430 struct s2n_connection *conn = NULL;
431 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
432 conn->handshake.handshake_type = HELLO_RETRY_REQUEST;
433 conn->secure->cipher_suite = &s2n_tls13_aes_256_gcm_sha384;
434 EXPECT_EQUAL(conn->secure->cipher_suite->prf_alg, matching_psk.hmac_alg);
435
436 for (size_t i = 0; i < s2n_array_len(test_cases); i++) {
437 struct s2n_psk *psk = NULL;
438 EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &psk));
439 EXPECT_OK(s2n_psk_init(psk, S2N_PSK_TYPE_EXTERNAL));
440 EXPECT_SUCCESS(s2n_psk_set_identity(psk, test_cases[i].identity, test_cases[i].identity_size));
441 psk->hmac_alg = test_cases[i].hmac_alg;
442 }
443
444 EXPECT_SUCCESS(s2n_client_psk_extension.send(conn, &out));
445
446 /* The identity list should only contain the matching psk's identity.
447 * It should NOT contain the non-matching psk. */
448
449 uint16_t identity_list_size = 0;
450 uint16_t identity_size = 0;
451 uint8_t *identity_data = NULL;
452 uint32_t obfuscated_ticket_age = 0;
453
454 EXPECT_SUCCESS(s2n_stuffer_read_uint16(&out, &identity_list_size));
455 EXPECT_EQUAL(identity_list_size, sizeof(identity_size) + matching_psk.identity_size + sizeof(obfuscated_ticket_age));
456
457 EXPECT_SUCCESS(s2n_stuffer_read_uint16(&out, &identity_size));
458 EXPECT_EQUAL(identity_size, matching_psk.identity_size);
459 EXPECT_NOT_NULL(identity_data = s2n_stuffer_raw_read(&out, identity_size));
460 EXPECT_BYTEARRAY_EQUAL(identity_data, matching_psk.identity, matching_psk.identity_size);
461
462 EXPECT_SUCCESS(s2n_stuffer_read_uint32(&out, &obfuscated_ticket_age));
463
464 /* The binder list should only reserve space for the matching psk's binder.
465 * It should NOT reserve space for the binder for the non-matching psk. */
466 EXPECT_EQUAL(s2n_stuffer_data_available(&out),
467 matching_psk.hash_size
468 + sizeof(uint8_t) /* size of binder size */
469 + sizeof(uint16_t))
470 /* size of binder list size */;
471
472 EXPECT_SUCCESS(s2n_connection_free(conn));
473 EXPECT_SUCCESS(s2n_stuffer_free(&out));
474 }
475
476 /* On the second ClientHello after a retry request,
477 * do not send the PSK extension if no valid PSKs.
478 *
479 *= https://www.rfc-editor.org/rfc/rfc8446#section-4.1.4
480 *= type=test
481 *# In addition, in its updated ClientHello, the client SHOULD NOT offer
482 *# any pre-shared keys associated with a hash other than that of the
483 *# selected cipher suite.
484 */
485 if (s2n_is_tls13_fully_supported()) {
486 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL,
487 s2n_cert_chain_and_key_ptr_free);
488 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
489 S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY));
490
491 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(),
492 s2n_config_ptr_free);
493 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
494 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13"));
495 EXPECT_SUCCESS(s2n_config_disable_x509_verification(config));
496
497 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
498 s2n_connection_ptr_free);
499 EXPECT_NOT_NULL(server_conn);
500 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
501
502 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
503 s2n_connection_ptr_free);
504 EXPECT_NOT_NULL(server_conn);
505 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
506
507 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
508 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
509 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
510
511 /* Force the HRR path */
512 client_conn->security_policy_override = &security_policy_test_tls13_retry;
513
514 /* The server will choose the first cipher in the security policy,
515 * which uses SHA256 for its PRF. So setup a PSK that requires SHA384
516 * so that the algorithms will not match and the PSK will be discarded.
517 */
518 DEFER_CLEANUP(struct s2n_psk *psk = s2n_test_psk_new(client_conn), s2n_psk_free);
519 psk->hmac_alg = S2N_HMAC_SHA384;
520 EXPECT_SUCCESS(s2n_connection_append_psk(client_conn, psk));
521 EXPECT_TRUE(s2n_client_psk_extension.should_send(client_conn));
522
523 /* Send and receive ClientHello and RetryRequest */
524 s2n_blocked_status blocked = 0;
525 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
526 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, HELLO_RETRY_MSG));
527 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, CLIENT_HELLO));
528 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, CLIENT_HELLO));
529
530 /* Verify that the PSK extension will not be sent in the second hello */
531 EXPECT_FALSE(s2n_client_psk_extension.should_send(client_conn));
532
533 /* Verify the handshake could complete successfully */
534 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
535 }
536 };
537
538 /* Test: s2n_select_external_psk */
539 {
540 /* Safety checks */
541 {
542 struct s2n_connection *conn = NULL;
543 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
544
545 EXPECT_ERROR_WITH_ERRNO(s2n_select_external_psk(conn, NULL), S2N_ERR_NULL);
546
547 struct s2n_offered_psk_list wire_identity_list = { 0 };
548 EXPECT_ERROR_WITH_ERRNO(s2n_select_external_psk(NULL, &wire_identity_list), S2N_ERR_NULL);
549
550 EXPECT_SUCCESS(s2n_connection_free(conn));
551 };
552
553 struct s2n_blob identity_1 = { 0 };
554 EXPECT_SUCCESS(s2n_blob_init(&identity_1, test_identity, sizeof(test_identity)));
555 struct s2n_blob identity_2 = { 0 };
556 EXPECT_SUCCESS(s2n_blob_init(&identity_2, test_identity_2, sizeof(test_identity_2)));
557 struct s2n_blob identity_3 = { 0 };
558 EXPECT_SUCCESS(s2n_blob_init(&identity_3, test_identity_3, sizeof(test_identity_3)));
559
560 struct s2n_blob *all_psks_list[] = { &identity_1, &identity_2, &identity_3 };
561 struct s2n_blob *reverse_order_list[] = { &identity_3, &identity_2, &identity_1 };
562 struct s2n_blob *list_without_psk1[] = { &identity_2, &identity_3 };
563 struct s2n_blob *multiple_psk1[] = { &identity_1, &identity_1, &identity_1 };
564 struct s2n_blob *multiple_psk2[] = { &identity_2, &identity_2, &identity_2 };
565 struct s2n_blob *only_psk1[] = { &identity_1 };
566 struct s2n_blob *only_psk2[] = { &identity_2 };
567 struct s2n_blob *only_psk3[] = { &identity_3 };
568
569 struct {
570 bool match;
571 struct s2n_blob **wire_identities;
572 size_t wire_identities_len;
573 struct s2n_blob **local_identities;
574 size_t local_identities_len;
575 size_t wire_match_index;
576 size_t local_match_index;
577 } test_cases[] = {
578 #define WIRE_IDENTITIES(list) .wire_identities = list, .wire_identities_len = s2n_array_len(list)
579 #define LOCAL_IDENTITIES(list) .local_identities = list, .local_identities_len = s2n_array_len(list)
580 /* No wire or local identities */
581 { .match = false },
582
583 /* Only wire identities */
584 { .match = false, WIRE_IDENTITIES(only_psk1) },
585 { .match = false, WIRE_IDENTITIES(all_psks_list) },
586
587 /* Only local identities */
588 { .match = false, LOCAL_IDENTITIES(only_psk1) },
589 { .match = false, LOCAL_IDENTITIES(all_psks_list) },
590
591 /* No match with valid lists */
592 { .match = false, WIRE_IDENTITIES(only_psk1), LOCAL_IDENTITIES(only_psk2) },
593 { .match = false, WIRE_IDENTITIES(only_psk1), LOCAL_IDENTITIES(list_without_psk1) },
594 { .match = false, WIRE_IDENTITIES(list_without_psk1), LOCAL_IDENTITIES(only_psk1) },
595
596 /* Single option matches */
597 { .match = true, WIRE_IDENTITIES(only_psk1), LOCAL_IDENTITIES(only_psk1), .wire_match_index = 0, .local_match_index = 0 },
598
599 /* Single wire option matches */
600 { .match = true, WIRE_IDENTITIES(only_psk1), LOCAL_IDENTITIES(all_psks_list), .wire_match_index = 0, .local_match_index = 0 },
601 { .match = true, WIRE_IDENTITIES(only_psk2), LOCAL_IDENTITIES(all_psks_list), .wire_match_index = 0, .local_match_index = 1 },
602 { .match = true, WIRE_IDENTITIES(only_psk3), LOCAL_IDENTITIES(all_psks_list), .wire_match_index = 0, .local_match_index = 2 },
603
604 /* Single local option matches */
605 { .match = true, WIRE_IDENTITIES(all_psks_list), LOCAL_IDENTITIES(only_psk1), .wire_match_index = 0, .local_match_index = 0 },
606 { .match = true, WIRE_IDENTITIES(all_psks_list), LOCAL_IDENTITIES(only_psk2), .wire_match_index = 1, .local_match_index = 0 },
607 { .match = true, WIRE_IDENTITIES(all_psks_list), LOCAL_IDENTITIES(only_psk3), .wire_match_index = 2, .local_match_index = 0 },
608
609 /* Match with multiple wire and local options: choose first local */
610 { .match = true, WIRE_IDENTITIES(all_psks_list), LOCAL_IDENTITIES(all_psks_list), .wire_match_index = 0, .local_match_index = 0 },
611 { .match = true, WIRE_IDENTITIES(reverse_order_list), LOCAL_IDENTITIES(all_psks_list), .wire_match_index = 2, .local_match_index = 0 },
612 { .match = true, WIRE_IDENTITIES(all_psks_list), LOCAL_IDENTITIES(reverse_order_list), .wire_match_index = 2, .local_match_index = 0 },
613 { .match = true, WIRE_IDENTITIES(list_without_psk1), LOCAL_IDENTITIES(all_psks_list), .wire_match_index = 0, .local_match_index = 1 },
614 { .match = true, WIRE_IDENTITIES(all_psks_list), LOCAL_IDENTITIES(list_without_psk1), .wire_match_index = 1, .local_match_index = 0 },
615 { .match = true, WIRE_IDENTITIES(list_without_psk1), LOCAL_IDENTITIES(reverse_order_list), .wire_match_index = 1, .local_match_index = 0 },
616 { .match = true, WIRE_IDENTITIES(reverse_order_list), LOCAL_IDENTITIES(list_without_psk1), .wire_match_index = 1, .local_match_index = 0 },
617
618 /* Handle duplicates */
619 { .match = true, WIRE_IDENTITIES(multiple_psk1), LOCAL_IDENTITIES(multiple_psk1), .wire_match_index = 0, .local_match_index = 0 },
620 { .match = false, WIRE_IDENTITIES(multiple_psk1), LOCAL_IDENTITIES(multiple_psk2) },
621 { .match = true, WIRE_IDENTITIES(multiple_psk1), LOCAL_IDENTITIES(all_psks_list), .wire_match_index = 0, .local_match_index = 0 },
622 { .match = true, WIRE_IDENTITIES(all_psks_list), LOCAL_IDENTITIES(multiple_psk1), .wire_match_index = 0, .local_match_index = 0 },
623 { .match = true, WIRE_IDENTITIES(multiple_psk1), LOCAL_IDENTITIES(reverse_order_list), .wire_match_index = 0, .local_match_index = 2 },
624 { .match = true, WIRE_IDENTITIES(reverse_order_list), LOCAL_IDENTITIES(multiple_psk1), .wire_match_index = 2, .local_match_index = 0 },
625 };
626
627 for (size_t i = 0; i < s2n_array_len(test_cases); i++) {
628 struct s2n_connection *conn = NULL;
629 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
630 conn->psk_params.type = S2N_PSK_TYPE_EXTERNAL;
631
632 struct s2n_offered_psk_list client_identity_list = { .conn = conn };
633 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&client_identity_list.wire_data, 0));
634 for (size_t wire_i = 0; wire_i < test_cases[i].wire_identities_len; wire_i++) {
635 EXPECT_OK(s2n_write_test_identity(&client_identity_list.wire_data, test_cases[i].wire_identities[wire_i]));
636 }
637
638 struct s2n_psk *expected_chosen_psk = NULL;
639 for (size_t local_i = 0; local_i < test_cases[i].local_identities_len; local_i++) {
640 struct s2n_psk *server_psk = NULL;
641 EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &server_psk));
642 EXPECT_NOT_NULL(server_psk);
643 EXPECT_SUCCESS(s2n_psk_set_identity(server_psk, test_cases[i].local_identities[local_i]->data,
644 test_cases[i].local_identities[local_i]->size));
645
646 if (local_i == test_cases[i].local_match_index) {
647 expected_chosen_psk = server_psk;
648 }
649 }
650
651 if (test_cases[i].match) {
652 EXPECT_OK(s2n_select_external_psk(conn, &client_identity_list));
653 EXPECT_EQUAL(conn->psk_params.chosen_psk_wire_index, test_cases[i].wire_match_index);
654 EXPECT_EQUAL(conn->psk_params.chosen_psk, expected_chosen_psk);
655 } else {
656 EXPECT_ERROR_WITH_ERRNO(s2n_select_external_psk(conn, &client_identity_list), S2N_ERR_NULL);
657 EXPECT_NULL(conn->psk_params.chosen_psk);
658 }
659
660 EXPECT_SUCCESS(s2n_connection_free(conn));
661 EXPECT_SUCCESS(s2n_stuffer_free(&client_identity_list.wire_data));
662 }
663 };
664
665 /* Test: s2n_select_resumption_psk */
666 {
667 /* Safety checks */
668 {
669 struct s2n_offered_psk_list identity_list = { 0 };
670 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
671 EXPECT_NOT_NULL(conn);
672
673 EXPECT_ERROR_WITH_ERRNO(s2n_select_resumption_psk(NULL, &identity_list), S2N_ERR_NULL);
674 EXPECT_ERROR_WITH_ERRNO(s2n_select_resumption_psk(conn, NULL), S2N_ERR_NULL);
675
676 EXPECT_SUCCESS(s2n_connection_free(conn));
677 };
678
679 /* One valid resumption psk is received */
680 {
681 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
682 EXPECT_NOT_NULL(conn);
683
684 struct s2n_config *config = s2n_config_new();
685 EXPECT_NOT_NULL(config);
686 EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));
687 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
688
689 conn->actual_protocol_version = S2N_TLS13;
690 conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
691
692 DEFER_CLEANUP(struct s2n_stuffer psk_identity = { 0 }, s2n_stuffer_free);
693 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&psk_identity, 0));
694 EXPECT_OK(s2n_setup_encrypted_ticket(conn, &psk_identity));
695
696 struct s2n_offered_psk_list identity_list = { .conn = conn };
697 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&identity_list.wire_data, 0));
698
699 EXPECT_OK(s2n_write_test_identity(&identity_list.wire_data, &psk_identity.blob));
700
701 EXPECT_OK(s2n_select_resumption_psk(conn, &identity_list));
702
703 EXPECT_EQUAL(conn->psk_params.chosen_psk_wire_index, 0);
704 EXPECT_NOT_NULL(conn->psk_params.chosen_psk);
705
706 /* Sanity check psk creation is correct */
707 EXPECT_EQUAL(conn->psk_params.chosen_psk->hmac_alg, s2n_tls13_aes_128_gcm_sha256.prf_alg);
708
709 EXPECT_SUCCESS(s2n_stuffer_free(&identity_list.wire_data));
710 EXPECT_SUCCESS(s2n_connection_free(conn));
711 EXPECT_SUCCESS(s2n_config_free(config));
712 };
713
714 /* First valid resumption psk is chosen */
715 {
716 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
717 EXPECT_NOT_NULL(conn);
718
719 struct s2n_config *config = s2n_config_new();
720 EXPECT_NOT_NULL(config);
721 EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));
722 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
723
724 conn->actual_protocol_version = S2N_TLS13;
725 conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
726
727 DEFER_CLEANUP(struct s2n_stuffer psk_identity = { 0 }, s2n_stuffer_free);
728 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&psk_identity, 0));
729 EXPECT_OK(s2n_setup_encrypted_ticket(conn, &psk_identity));
730
731 struct s2n_offered_psk_list identity_list = { .conn = conn };
732 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&identity_list.wire_data, 0));
733
734 /* Write invalid resumption psk */
735 uint8_t bad_identity_data[] = "hello";
736 struct s2n_blob bad_identity = { 0 };
737 EXPECT_SUCCESS(s2n_blob_init(&bad_identity, bad_identity_data, sizeof(bad_identity_data)));
738
739 uint8_t psk_idx = MAX_REJECTED_TICKETS - 1;
740 for (size_t i = 0; i < psk_idx; i++) {
741 EXPECT_OK(s2n_write_test_identity(&identity_list.wire_data, &bad_identity));
742 }
743
744 /* Write valid resumption psk */
745 EXPECT_OK(s2n_write_test_identity(&identity_list.wire_data, &psk_identity.blob));
746
747 /* Write second valid resumption psk */
748 EXPECT_OK(s2n_write_test_identity(&identity_list.wire_data, &psk_identity.blob));
749
750 EXPECT_OK(s2n_select_resumption_psk(conn, &identity_list));
751
752 EXPECT_EQUAL(conn->psk_params.chosen_psk_wire_index, psk_idx);
753 EXPECT_NOT_NULL(conn->psk_params.chosen_psk);
754
755 /* Sanity check psk creation is correct */
756 EXPECT_EQUAL(conn->psk_params.chosen_psk->hmac_alg, s2n_tls13_aes_128_gcm_sha256.prf_alg);
757
758 EXPECT_SUCCESS(s2n_stuffer_free(&identity_list.wire_data));
759 EXPECT_SUCCESS(s2n_connection_free(conn));
760 EXPECT_SUCCESS(s2n_config_free(config));
761 };
762
763 /* No valid resumption psks */
764 {
765 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
766 EXPECT_NOT_NULL(conn);
767
768 conn->actual_protocol_version = S2N_TLS13;
769 conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
770
771 struct s2n_offered_psk_list identity_list = { .conn = conn };
772 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&identity_list.wire_data, 0));
773
774 /* Write invalid resumption psk */
775 uint8_t bad_identity_data[] = "hello";
776 struct s2n_blob bad_identity = { 0 };
777 EXPECT_SUCCESS(s2n_blob_init(&bad_identity, bad_identity_data, sizeof(bad_identity_data)));
778 EXPECT_OK(s2n_write_test_identity(&identity_list.wire_data, &bad_identity));
779
780 EXPECT_ERROR_WITH_ERRNO(s2n_select_resumption_psk(conn, &identity_list), S2N_ERR_INVALID_SESSION_TICKET);
781 EXPECT_NULL(conn->psk_params.chosen_psk);
782
783 EXPECT_SUCCESS(s2n_stuffer_free(&identity_list.wire_data));
784 EXPECT_SUCCESS(s2n_connection_free(conn));
785 };
786
787 /* Fail if too many invalid resumption psks */
788 {
789 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
790 EXPECT_NOT_NULL(conn);
791
792 struct s2n_config *config = s2n_config_new();
793 EXPECT_NOT_NULL(config);
794 EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));
795 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
796
797 conn->actual_protocol_version = S2N_TLS13;
798 conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
799
800 DEFER_CLEANUP(struct s2n_stuffer psk_identity = { 0 }, s2n_stuffer_free);
801 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&psk_identity, 0));
802 EXPECT_OK(s2n_setup_encrypted_ticket(conn, &psk_identity));
803
804 /* "hello" is mysteriously not a valid session ticket */
805 uint8_t bad_identity_data[] = "hello";
806 struct s2n_blob bad_identity = { 0 };
807 EXPECT_SUCCESS(s2n_blob_init(&bad_identity, bad_identity_data, sizeof(bad_identity_data)));
808
809 struct s2n_offered_psk_list identity_list = { .conn = conn };
810 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&identity_list.wire_data, 0));
811
812 uint8_t bad_psk_count = MAX_REJECTED_TICKETS;
813 for (size_t i = 0; i < bad_psk_count; i++) {
814 EXPECT_OK(s2n_write_test_identity(&identity_list.wire_data, &bad_identity));
815 }
816 EXPECT_OK(s2n_write_test_identity(&identity_list.wire_data, &psk_identity.blob));
817
818 EXPECT_ERROR_WITH_ERRNO(s2n_select_resumption_psk(conn, &identity_list), S2N_ERR_INVALID_SESSION_TICKET);
819 EXPECT_NULL(conn->psk_params.chosen_psk);
820
821 EXPECT_SUCCESS(s2n_stuffer_free(&identity_list.wire_data));
822 EXPECT_SUCCESS(s2n_connection_free(conn));
823 EXPECT_SUCCESS(s2n_config_free(config));
824 };
825 };
826
827 /* Test: s2n_client_psk_recv_identity_list */
828 {
829 /* Safety checks */
830 {
831 struct s2n_stuffer wire_identities_in = { 0 };
832
833 struct s2n_connection *conn = NULL;
834 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
835
836 EXPECT_ERROR_WITH_ERRNO(s2n_client_psk_recv_identity_list(conn, NULL), S2N_ERR_NULL);
837 EXPECT_ERROR_WITH_ERRNO(s2n_client_psk_recv_identity_list(NULL, &wire_identities_in), S2N_ERR_NULL);
838
839 EXPECT_SUCCESS(s2n_connection_free(conn));
840 };
841
842 /* Receive an empty list */
843 {
844 struct s2n_stuffer empty_wire_identities_in = { 0 };
845
846 struct s2n_connection *conn = NULL;
847 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
848 EXPECT_OK(s2n_connection_set_psk_type(conn, S2N_PSK_TYPE_EXTERNAL));
849
850 EXPECT_ERROR(s2n_client_psk_recv_identity_list(conn, &empty_wire_identities_in));
851 EXPECT_NULL(conn->psk_params.chosen_psk);
852
853 EXPECT_SUCCESS(s2n_connection_free(conn));
854 };
855
856 /* Default selection logic: receive a list without a match */
857 {
858 struct s2n_connection *conn = NULL;
859 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
860 EXPECT_OK(s2n_connection_set_psk_type(conn, S2N_PSK_TYPE_EXTERNAL));
861
862 struct s2n_stuffer wire_identities_in = { 0 };
863 EXPECT_SUCCESS(s2n_stuffer_alloc(&wire_identities_in, sizeof(wire_identities)));
864 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&wire_identities_in, wire_identities, sizeof(wire_identities)));
865
866 struct s2n_psk *no_match_psk = NULL;
867 EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &no_match_psk));
868 EXPECT_OK(s2n_psk_init(no_match_psk, S2N_PSK_TYPE_EXTERNAL));
869 EXPECT_SUCCESS(s2n_psk_set_identity(no_match_psk, test_bytes_data_2, sizeof(test_bytes_data_2)));
870
871 EXPECT_ERROR_WITH_ERRNO(s2n_client_psk_recv_identity_list(conn, &wire_identities_in), S2N_ERR_NULL);
872 EXPECT_NULL(conn->psk_params.chosen_psk);
873
874 EXPECT_SUCCESS(s2n_connection_free(conn));
875 EXPECT_SUCCESS(s2n_stuffer_free(&wire_identities_in));
876 };
877
878 /* Default selection logic: receive a list with a match */
879 {
880 struct s2n_connection *conn = NULL;
881 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
882 EXPECT_OK(s2n_connection_set_psk_type(conn, S2N_PSK_TYPE_EXTERNAL));
883
884 struct s2n_stuffer wire_identities_in = { 0 };
885 EXPECT_SUCCESS(s2n_stuffer_alloc(&wire_identities_in, sizeof(single_wire_identity)));
886 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&wire_identities_in, single_wire_identity, sizeof(single_wire_identity)));
887
888 struct s2n_psk *match_psk = NULL;
889 EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &match_psk));
890 EXPECT_OK(s2n_psk_init(match_psk, S2N_PSK_TYPE_EXTERNAL));
891 EXPECT_SUCCESS(s2n_psk_set_identity(match_psk, test_bytes_data, sizeof(test_bytes_data)));
892
893 EXPECT_OK(s2n_client_psk_recv_identity_list(conn, &wire_identities_in));
894 EXPECT_EQUAL(conn->psk_params.chosen_psk_wire_index, 0);
895 EXPECT_EQUAL(conn->psk_params.chosen_psk, match_psk);
896
897 EXPECT_SUCCESS(s2n_connection_free(conn));
898 EXPECT_SUCCESS(s2n_stuffer_free(&wire_identities_in));
899 };
900
901 /* Customer selection logic: customer rejects all identities */
902 {
903 struct s2n_config *config = s2n_config_new();
904 EXPECT_NOT_NULL(config);
905 EXPECT_SUCCESS(s2n_config_set_psk_selection_callback(config, s2n_test_error_select_psk_identity_callback, NULL));
906
907 struct s2n_connection *conn = NULL;
908 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
909 EXPECT_OK(s2n_connection_set_psk_type(conn, S2N_PSK_TYPE_EXTERNAL));
910 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
911
912 struct s2n_stuffer wire_identities_in = { 0 };
913 EXPECT_ERROR_WITH_ERRNO(s2n_client_psk_recv_identity_list(conn, &wire_identities_in),
914 S2N_ERR_UNIMPLEMENTED);
915 EXPECT_EQUAL(conn->psk_params.chosen_psk, NULL);
916
917 EXPECT_SUCCESS(s2n_connection_free(conn));
918 EXPECT_SUCCESS(s2n_config_free(config));
919 EXPECT_SUCCESS(s2n_stuffer_free(&wire_identities_in));
920 };
921
922 /* Customer selection logic: customer selects valid identity */
923 {
924 struct s2n_config *config = s2n_config_new();
925 EXPECT_NOT_NULL(config);
926
927 uint16_t expected_wire_choice = 0;
928 EXPECT_SUCCESS(s2n_config_set_psk_selection_callback(config, s2n_test_select_psk_identity_callback, &expected_wire_choice));
929
930 struct s2n_connection *conn = NULL;
931 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
932 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
933 EXPECT_OK(s2n_connection_set_psk_type(conn, S2N_PSK_TYPE_EXTERNAL));
934
935 struct s2n_psk *match_psk = NULL;
936 EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &match_psk));
937 EXPECT_OK(s2n_psk_init(match_psk, S2N_PSK_TYPE_EXTERNAL));
938 EXPECT_SUCCESS(s2n_psk_set_identity(match_psk, test_bytes_data, sizeof(test_bytes_data)));
939
940 struct s2n_stuffer wire_identities_in = { 0 };
941 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&wire_identities_in, 0));
942 EXPECT_OK(s2n_write_test_identity(&wire_identities_in, &match_psk->identity));
943
944 EXPECT_OK(s2n_client_psk_recv_identity_list(conn, &wire_identities_in));
945 EXPECT_EQUAL(conn->psk_params.chosen_psk, match_psk);
946 EXPECT_EQUAL(conn->psk_params.chosen_psk_wire_index, 0);
947
948 EXPECT_SUCCESS(s2n_connection_free(conn));
949 EXPECT_SUCCESS(s2n_config_free(config));
950 EXPECT_SUCCESS(s2n_stuffer_free(&wire_identities_in));
951 };
952
953 /* Customer selection logic: customer selects out of bounds index */
954 {
955 struct s2n_config *config = s2n_config_new();
956 EXPECT_NOT_NULL(config);
957
958 uint16_t expected_wire_choice = 10;
959 EXPECT_SUCCESS(s2n_config_set_psk_selection_callback(config, s2n_test_select_psk_identity_callback, &expected_wire_choice));
960
961 struct s2n_connection *conn = NULL;
962 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
963 EXPECT_OK(s2n_connection_set_psk_type(conn, S2N_PSK_TYPE_EXTERNAL));
964 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
965
966 struct s2n_psk *match_psk = NULL;
967 EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &match_psk));
968 EXPECT_OK(s2n_psk_init(match_psk, S2N_PSK_TYPE_EXTERNAL));
969 EXPECT_SUCCESS(s2n_psk_set_identity(match_psk, test_bytes_data, sizeof(test_bytes_data)));
970
971 struct s2n_stuffer wire_identities_in = { 0 };
972 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&wire_identities_in, 0));
973 EXPECT_OK(s2n_write_test_identity(&wire_identities_in, &match_psk->identity));
974
975 EXPECT_ERROR(s2n_client_psk_recv_identity_list(conn, &wire_identities_in));
976 EXPECT_EQUAL(conn->psk_params.chosen_psk, NULL);
977
978 EXPECT_SUCCESS(s2n_connection_free(conn));
979 EXPECT_SUCCESS(s2n_config_free(config));
980 EXPECT_SUCCESS(s2n_stuffer_free(&wire_identities_in));
981 };
982
983 /* Customer selection logic: customer selects index without a local match */
984 {
985 struct s2n_config *config = s2n_config_new();
986 EXPECT_NOT_NULL(config);
987
988 uint16_t expected_wire_choice = 0;
989 EXPECT_SUCCESS(s2n_config_set_psk_selection_callback(config, s2n_test_select_psk_identity_callback, &expected_wire_choice));
990
991 struct s2n_connection *conn = NULL;
992 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
993 EXPECT_OK(s2n_connection_set_psk_type(conn, S2N_PSK_TYPE_EXTERNAL));
994 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
995
996 struct s2n_psk *local_psk = NULL;
997 EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &local_psk));
998 EXPECT_OK(s2n_psk_init(local_psk, S2N_PSK_TYPE_EXTERNAL));
999 EXPECT_SUCCESS(s2n_psk_set_identity(local_psk, test_bytes_data, sizeof(test_bytes_data)));
1000
1001 struct s2n_blob wire_identity = { 0 };
1002 EXPECT_SUCCESS(s2n_blob_init(&wire_identity, test_bytes_data_2, sizeof(test_bytes_data_2)));
1003
1004 struct s2n_stuffer wire_identities_in = { 0 };
1005 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&wire_identities_in, 0));
1006 EXPECT_OK(s2n_write_test_identity(&wire_identities_in, &wire_identity));
1007
1008 EXPECT_ERROR(s2n_client_psk_recv_identity_list(conn, &wire_identities_in));
1009 EXPECT_EQUAL(conn->psk_params.chosen_psk, NULL);
1010
1011 EXPECT_SUCCESS(s2n_connection_free(conn));
1012 EXPECT_SUCCESS(s2n_config_free(config));
1013 EXPECT_SUCCESS(s2n_stuffer_free(&wire_identities_in));
1014 };
1015
1016 /* PSK type is resumption */
1017 {
1018 DEFER_CLEANUP(struct s2n_stuffer wire_identities_in = { 0 }, s2n_stuffer_free);
1019 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&wire_identities_in, 0));
1020
1021 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
1022 EXPECT_NOT_NULL(conn);
1023 EXPECT_OK(s2n_connection_set_psk_type(conn, S2N_PSK_TYPE_RESUMPTION));
1024
1025 struct s2n_config *config_with_cb = s2n_config_new();
1026 EXPECT_NOT_NULL(config_with_cb);
1027 uint16_t expected_wire_choice = 0;
1028 EXPECT_SUCCESS(s2n_config_set_psk_selection_callback(config_with_cb, s2n_test_select_psk_identity_callback, &expected_wire_choice));
1029 EXPECT_OK(s2n_resumption_test_ticket_key_setup(config_with_cb));
1030
1031 struct s2n_config *config = s2n_config_new();
1032 EXPECT_NOT_NULL(config);
1033 EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));
1034
1035 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
1036
1037 conn->actual_protocol_version = S2N_TLS13;
1038 conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
1039
1040 DEFER_CLEANUP(struct s2n_stuffer psk_identity = { 0 }, s2n_stuffer_free);
1041 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&psk_identity, 0));
1042 EXPECT_OK(s2n_setup_encrypted_ticket(conn, &psk_identity));
1043
1044 /* Resumption psk was selected */
1045 {
1046 /* Write invalid resumption PSK */
1047 struct s2n_blob wire_identity = { 0 };
1048 EXPECT_SUCCESS(s2n_blob_init(&wire_identity, test_bytes_data_2, sizeof(test_bytes_data_2)));
1049 EXPECT_OK(s2n_write_test_identity(&wire_identities_in, &wire_identity));
1050
1051 /* Write valid resumption PSK */
1052 EXPECT_OK(s2n_write_test_identity(&wire_identities_in, &psk_identity.blob));
1053
1054 EXPECT_OK(s2n_client_psk_recv_identity_list(conn, &wire_identities_in));
1055
1056 EXPECT_EQUAL(conn->psk_params.chosen_psk_wire_index, 1);
1057 EXPECT_NOT_NULL(conn->psk_params.chosen_psk);
1058 };
1059
1060 /* Customer selects a valid resumption psk with a callback */
1061 {
1062 conn->psk_params.chosen_psk = NULL;
1063 EXPECT_SUCCESS(s2n_stuffer_rewrite(&wire_identities_in));
1064 EXPECT_SUCCESS(s2n_connection_set_config(conn, config_with_cb));
1065 EXPECT_OK(s2n_write_test_identity(&wire_identities_in, &psk_identity.blob));
1066
1067 EXPECT_OK(s2n_client_psk_recv_identity_list(conn, &wire_identities_in));
1068
1069 EXPECT_EQUAL(conn->psk_params.chosen_psk_wire_index, 0);
1070 EXPECT_NOT_NULL(conn->psk_params.chosen_psk);
1071 };
1072
1073 /* Customer selects an invalid resumption psk with a callback */
1074 {
1075 conn->psk_params.chosen_psk = NULL;
1076 EXPECT_SUCCESS(s2n_stuffer_rewrite(&wire_identities_in));
1077 EXPECT_SUCCESS(s2n_connection_set_config(conn, config_with_cb));
1078
1079 struct s2n_blob wire_identity = { 0 };
1080 EXPECT_SUCCESS(s2n_blob_init(&wire_identity, test_bytes_data_2, sizeof(test_bytes_data_2)));
1081 EXPECT_OK(s2n_write_test_identity(&wire_identities_in, &wire_identity));
1082
1083 EXPECT_ERROR(s2n_client_psk_recv_identity_list(conn, &wire_identities_in));
1084
1085 EXPECT_EQUAL(conn->psk_params.chosen_psk_wire_index, 0);
1086 EXPECT_NULL(conn->psk_params.chosen_psk);
1087 };
1088
1089 EXPECT_SUCCESS(s2n_config_free(config_with_cb));
1090 EXPECT_SUCCESS(s2n_connection_free(conn));
1091 EXPECT_SUCCESS(s2n_config_free(config));
1092 };
1093 };
1094
1095 /* Test: s2n_client_psk_recv_binder_list */
1096 {
1097 const uint8_t filler_binders[] = {
1098 0x00, /* binder size */
1099
1100 0x01, /* binder size */
1101 0xFF, /* binder value */
1102 };
1103 const size_t zero_length_binder_index = 0;
1104 const size_t valid_binder_index = 2;
1105
1106 uint8_t partial_client_hello_data[] = "Hello";
1107 uint8_t secret_data[] = "Secret";
1108 uint8_t binder_hash_data[SHA256_DIGEST_LENGTH] = { 0 };
1109 uint8_t valid_binder_data[SHA256_DIGEST_LENGTH] = { 0 };
1110
1111 struct s2n_blob partial_client_hello = { 0 };
1112 EXPECT_SUCCESS(s2n_blob_init(&partial_client_hello,
1113 partial_client_hello_data, sizeof(partial_client_hello_data)));
1114
1115 struct s2n_blob binder_hash = { 0 };
1116 EXPECT_SUCCESS(s2n_blob_init(&binder_hash, binder_hash_data, sizeof(binder_hash_data)));
1117
1118 struct s2n_blob valid_binder = { 0 };
1119 EXPECT_SUCCESS(s2n_blob_init(&valid_binder, valid_binder_data, sizeof(valid_binder_data)));
1120
1121 struct s2n_connection *conn = NULL;
1122 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
1123
1124 DEFER_CLEANUP(struct s2n_psk psk = { 0 }, s2n_psk_wipe);
1125 EXPECT_OK(s2n_psk_init(&psk, S2N_PSK_TYPE_EXTERNAL));
1126 EXPECT_SUCCESS(s2n_psk_set_secret(&psk, secret_data, sizeof(secret_data)));
1127
1128 EXPECT_SUCCESS(s2n_psk_calculate_binder_hash(conn, psk.hmac_alg, &partial_client_hello, &binder_hash));
1129 EXPECT_SUCCESS(s2n_psk_calculate_binder(&psk, &binder_hash, &valid_binder));
1130
1131 struct s2n_stuffer wire_binders_in = { 0 };
1132 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&wire_binders_in, sizeof(filler_binders)));
1133 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&wire_binders_in, filler_binders, sizeof(filler_binders)));
1134 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&wire_binders_in, valid_binder.size));
1135 EXPECT_SUCCESS(s2n_stuffer_write(&wire_binders_in, &valid_binder));
1136
1137 /* Receive an empty list */
1138 {
1139 uint8_t empty_wire_binders[] = { 0 };
1140 conn->psk_params.chosen_psk = &psk;
1141 conn->psk_params.chosen_psk_wire_index = valid_binder_index;
1142
1143 struct s2n_stuffer empty_wire_binders_in = { 0 };
1144 EXPECT_SUCCESS(s2n_stuffer_alloc(&empty_wire_binders_in, sizeof(empty_wire_binders)));
1145 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&empty_wire_binders_in, empty_wire_binders, sizeof(empty_wire_binders)));
1146
1147 EXPECT_ERROR_WITH_ERRNO(s2n_client_psk_recv_binder_list(conn, &partial_client_hello, &empty_wire_binders_in),
1148 S2N_ERR_BAD_MESSAGE);
1149
1150 EXPECT_SUCCESS(s2n_stuffer_free(&empty_wire_binders_in));
1151 };
1152
1153 /* No chosen identity */
1154 {
1155 EXPECT_SUCCESS(s2n_stuffer_reread(&wire_binders_in));
1156 conn->psk_params.chosen_psk = NULL;
1157 conn->psk_params.chosen_psk_wire_index = 0;
1158
1159 EXPECT_ERROR_WITH_ERRNO(s2n_client_psk_recv_binder_list(conn, &partial_client_hello, &wire_binders_in),
1160 S2N_ERR_NULL);
1161 };
1162
1163 /* Binder list too short for chosen identity index */
1164 {
1165 EXPECT_SUCCESS(s2n_stuffer_reread(&wire_binders_in));
1166 conn->psk_params.chosen_psk = &psk;
1167 conn->psk_params.chosen_psk_wire_index = valid_binder_index + 10;
1168
1169 EXPECT_ERROR_WITH_ERRNO(s2n_client_psk_recv_binder_list(conn, &partial_client_hello, &wire_binders_in),
1170 S2N_ERR_BAD_MESSAGE);
1171 };
1172
1173 /* Binder for chosen identity is zero-length */
1174 {
1175 EXPECT_SUCCESS(s2n_stuffer_reread(&wire_binders_in));
1176 conn->psk_params.chosen_psk = &psk;
1177 conn->psk_params.chosen_psk_wire_index = zero_length_binder_index;
1178
1179 EXPECT_ERROR_WITH_ERRNO(s2n_client_psk_recv_binder_list(conn, &partial_client_hello, &wire_binders_in),
1180 S2N_ERR_SAFETY);
1181 };
1182
1183 /* Binder for chosen identity is invalid */
1184 {
1185 EXPECT_SUCCESS(s2n_stuffer_reread(&wire_binders_in));
1186 conn->psk_params.chosen_psk = &psk;
1187 conn->psk_params.chosen_psk_wire_index = valid_binder_index;
1188
1189 /* Using a different partial client hello produces a different binder */
1190 struct s2n_blob *different_partial_client_hello = &valid_binder;
1191
1192 EXPECT_ERROR_WITH_ERRNO(s2n_client_psk_recv_binder_list(conn, different_partial_client_hello, &wire_binders_in),
1193 S2N_ERR_BAD_MESSAGE);
1194 };
1195
1196 /* Binder for chosen identity is valid */
1197 {
1198 EXPECT_SUCCESS(s2n_stuffer_reread(&wire_binders_in));
1199 conn->psk_params.chosen_psk = &psk;
1200 conn->psk_params.chosen_psk_wire_index = valid_binder_index;
1201
1202 EXPECT_OK(s2n_client_psk_recv_binder_list(conn, &partial_client_hello, &wire_binders_in));
1203 };
1204
1205 EXPECT_SUCCESS(s2n_connection_free(conn));
1206 EXPECT_SUCCESS(s2n_stuffer_free(&wire_binders_in));
1207 };
1208
1209 /* Test: s2n_client_psk_recv */
1210 {
1211 const uint8_t client_hello_data[] = "ClientHello";
1212 s2n_extension_type_id key_share_id = 0;
1213 s2n_extension_type_id psk_ke_mode_id = 0;
1214 EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_PSK_KEY_EXCHANGE_MODES, &psk_ke_mode_id));
1215 EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_KEY_SHARE, &key_share_id));
1216
1217 /* Receive an extension with no valid identity */
1218 {
1219 const uint8_t extension_data[] = {
1220 0x00, 0x00, /* identity list size */
1221 0x00, 0x00, /* binder list size */
1222 };
1223
1224 struct s2n_connection *conn = NULL;
1225 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
1226 conn->client_hello.extensions.count = 1;
1227
1228 /* The psk key exchange modes and keyshare extensions need to be received to use a psk */
1229 conn->psk_params.psk_ke_mode = S2N_PSK_DHE_KE;
1230 S2N_CBIT_SET(conn->extension_requests_received, psk_ke_mode_id);
1231 S2N_CBIT_SET(conn->extension_requests_received, key_share_id);
1232
1233 /* Setup the ClientHello */
1234 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&conn->handshake.io, client_hello_data, sizeof(client_hello_data)));
1235 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&conn->handshake.io, extension_data, sizeof(extension_data)));
1236
1237 /* Setup the extension */
1238 struct s2n_stuffer extension = { 0 };
1239 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&extension, 0));
1240 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&extension, extension_data, sizeof(extension_data)));
1241
1242 /* Verify it is successful, but no PSK is chosen */
1243 EXPECT_SUCCESS(s2n_client_psk_recv(conn, &extension));
1244 EXPECT_EQUAL(conn->psk_params.chosen_psk_wire_index, 0);
1245 EXPECT_EQUAL(conn->psk_params.chosen_psk, NULL);
1246
1247 EXPECT_SUCCESS(s2n_connection_free(conn));
1248 EXPECT_SUCCESS(s2n_stuffer_free(&extension));
1249 };
1250
1251 /* Receive an extension with an invalid binder */
1252 /* Receive an extension when running with TLS1.2 */
1253 {
1254 const uint8_t identity = 0x12;
1255 const uint8_t identity_bytes[] = { identity };
1256 const uint8_t extension_data[] = {
1257 0x00, 0x07, /* identity list size */
1258 0x00, 0x01, /* identity size */
1259 identity, /* identity */
1260 0x00, 0x00, 0x00, 0x00, /* ticket_age */
1261 0x00, 0x00, /* binder list size */
1262 };
1263
1264 struct s2n_connection *conn = NULL;
1265 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
1266 EXPECT_OK(s2n_connection_set_psk_type(conn, S2N_PSK_TYPE_EXTERNAL));
1267 conn->client_hello.extensions.count = 1;
1268
1269 /* The psk_ke_modes and keyshare extensions need to be received to use a psk */
1270 conn->psk_params.psk_ke_mode = S2N_PSK_DHE_KE;
1271 S2N_CBIT_SET(conn->extension_requests_received, psk_ke_mode_id);
1272 S2N_CBIT_SET(conn->extension_requests_received, key_share_id);
1273
1274 /* Setup the ClientHello */
1275 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&conn->handshake.io, client_hello_data, sizeof(client_hello_data)));
1276 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&conn->handshake.io, extension_data, sizeof(extension_data)));
1277
1278 /* Setup the extension */
1279 struct s2n_stuffer extension = { 0 };
1280 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&extension, 0));
1281 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&extension, extension_data, sizeof(extension_data)));
1282
1283 struct s2n_psk *psk = NULL;
1284 EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &psk));
1285 EXPECT_OK(s2n_psk_init(psk, S2N_PSK_TYPE_EXTERNAL));
1286 EXPECT_SUCCESS(s2n_psk_set_identity(psk, identity_bytes, sizeof(identity_bytes)));
1287
1288 /* Should be a no-op if using TLS1.2 */
1289 conn->actual_protocol_version = S2N_TLS12;
1290 EXPECT_SUCCESS(s2n_extension_recv(&s2n_client_psk_extension, conn, &extension));
1291 EXPECT_NULL(conn->psk_params.chosen_psk);
1292 EXPECT_EQUAL(s2n_stuffer_data_available(&extension), sizeof(extension_data));
1293
1294 /* Should be a failure if using TLS1.3 */
1295 conn->actual_protocol_version = S2N_TLS13;
1296 EXPECT_FAILURE_WITH_ERRNO(s2n_client_psk_recv(conn, &extension), S2N_ERR_BAD_MESSAGE);
1297
1298 EXPECT_SUCCESS(s2n_connection_free(conn));
1299 EXPECT_SUCCESS(s2n_stuffer_free(&extension));
1300 };
1301
1302 /* Receive a psk extension with no psk_ke_modes extension */
1303 {
1304 const uint8_t extension_data[] = { 0 };
1305
1306 struct s2n_connection *conn = NULL;
1307 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
1308 conn->client_hello.extensions.count = 1;
1309
1310 /* Setup the extension */
1311 struct s2n_stuffer extension = { 0 };
1312 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&extension, 0));
1313 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&extension, extension_data, sizeof(extension_data)));
1314
1315 EXPECT_EQUAL(conn->psk_params.psk_ke_mode, S2N_PSK_KE_UNKNOWN);
1316
1317 EXPECT_FALSE(S2N_CBIT_TEST(conn->extension_requests_received, psk_ke_mode_id));
1318
1319 EXPECT_FAILURE_WITH_ERRNO(s2n_client_psk_recv(conn, &extension), S2N_ERR_MISSING_EXTENSION);
1320
1321 EXPECT_SUCCESS(s2n_connection_free(conn));
1322 EXPECT_SUCCESS(s2n_stuffer_free(&extension));
1323 };
1324
1325 /* Receive a psk extension with an unknown psk key exchange mode */
1326 {
1327 const uint8_t extension_data[] = { 0 };
1328 struct s2n_connection *conn = NULL;
1329 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
1330 conn->client_hello.extensions.count = 1;
1331
1332 /* Setup the extension */
1333 struct s2n_stuffer extension = { 0 };
1334 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&extension, 0));
1335 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&extension, extension_data, sizeof(extension_data)));
1336
1337 conn->psk_params.psk_ke_mode = S2N_PSK_KE_UNKNOWN;
1338
1339 S2N_CBIT_SET(conn->extension_requests_received, psk_ke_mode_id);
1340
1341 EXPECT_SUCCESS(s2n_client_psk_recv(conn, &extension));
1342 EXPECT_NULL(conn->psk_params.chosen_psk);
1343
1344 EXPECT_SUCCESS(s2n_connection_free(conn));
1345 EXPECT_SUCCESS(s2n_stuffer_free(&extension));
1346 };
1347
1348 /* Receive a psk extension and a psk key exchange extension with (EC)DHE key establishment but no
1349 * keyshare_extension */
1350 {
1351 const uint8_t extension_data[] = { 0 };
1352 struct s2n_connection *conn = NULL;
1353 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
1354 conn->client_hello.extensions.count = 1;
1355
1356 /* Setup the extension */
1357 struct s2n_stuffer extension = { 0 };
1358 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&extension, 0));
1359 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&extension, extension_data, sizeof(extension_data)));
1360
1361 conn->psk_params.psk_ke_mode = S2N_PSK_DHE_KE;
1362
1363 S2N_CBIT_SET(conn->extension_requests_received, psk_ke_mode_id);
1364 EXPECT_FALSE(S2N_CBIT_TEST(conn->extension_requests_received, key_share_id));
1365
1366 EXPECT_FAILURE_WITH_ERRNO(s2n_client_psk_recv(conn, &extension), S2N_ERR_MISSING_EXTENSION);
1367
1368 EXPECT_SUCCESS(s2n_connection_free(conn));
1369 EXPECT_SUCCESS(s2n_stuffer_free(&extension));
1370 };
1371
1372 /* The extension does not appear last in the extension list */
1373 {
1374 s2n_extension_type_id psk_ext_id = 0;
1375 EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_PRE_SHARED_KEY, &psk_ext_id));
1376
1377 struct s2n_stuffer extension = { 0 };
1378 struct s2n_connection *conn = NULL;
1379 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
1380
1381 conn->client_hello.extensions.count = 2;
1382 conn->client_hello.extensions.parsed_extensions[psk_ext_id].wire_index = 0;
1383 EXPECT_FAILURE_WITH_ERRNO(s2n_client_psk_recv(conn, &extension), S2N_ERR_UNSUPPORTED_EXTENSION);
1384
1385 conn->client_hello.extensions.count = 5;
1386 conn->client_hello.extensions.parsed_extensions[psk_ext_id].wire_index = 1;
1387 EXPECT_FAILURE_WITH_ERRNO(s2n_client_psk_recv(conn, &extension), S2N_ERR_UNSUPPORTED_EXTENSION);
1388
1389 EXPECT_SUCCESS(s2n_connection_free(conn));
1390 };
1391
1392 /* Receive a valid extension */
1393 {
1394 const uint8_t client_hello_prefix_data[] = {
1395 0x01, /* Message Type: ClientHello */
1396 0x00, 0x00, 0x00, /* Message size: not set yet */
1397 0x12, 0x34, 0x56, /* Message: random data */
1398 };
1399
1400 struct s2n_connection *client_conn = NULL;
1401 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
1402 struct s2n_stuffer *client_out = &client_conn->handshake.io;
1403
1404 struct s2n_connection *server_conn = NULL;
1405 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
1406 EXPECT_OK(s2n_connection_set_psk_type(server_conn, S2N_PSK_TYPE_EXTERNAL));
1407 struct s2n_stuffer *server_in = &server_conn->handshake.io;
1408 server_conn->client_hello.extensions.count = 1;
1409
1410 /* The psk key exchange modes and keyshare extensions need to be received to use a psk */
1411 server_conn->psk_params.psk_ke_mode = S2N_PSK_DHE_KE;
1412 S2N_CBIT_SET(server_conn->extension_requests_received, psk_ke_mode_id);
1413 S2N_CBIT_SET(server_conn->extension_requests_received, key_share_id);
1414
1415 struct s2n_psk *shared_psk = NULL;
1416 EXPECT_OK(s2n_array_pushback(&client_conn->psk_params.psk_list, (void **) &shared_psk));
1417 EXPECT_OK(s2n_psk_init(shared_psk, S2N_PSK_TYPE_EXTERNAL));
1418 EXPECT_SUCCESS(s2n_psk_set_identity(shared_psk, test_identity, sizeof(test_identity)));
1419 EXPECT_SUCCESS(s2n_psk_set_secret(shared_psk, test_secret, sizeof(test_secret)));
1420 EXPECT_OK(s2n_array_pushback(&server_conn->psk_params.psk_list, (void **) &shared_psk));
1421 EXPECT_OK(s2n_psk_init(shared_psk, S2N_PSK_TYPE_EXTERNAL));
1422 EXPECT_SUCCESS(s2n_psk_set_identity(shared_psk, test_identity, sizeof(test_identity)));
1423 EXPECT_SUCCESS(s2n_psk_set_secret(shared_psk, test_secret, sizeof(test_secret)));
1424
1425 struct s2n_psk *other_server_psk = NULL;
1426 EXPECT_OK(s2n_array_pushback(&server_conn->psk_params.psk_list, (void **) &other_server_psk));
1427 EXPECT_OK(s2n_psk_init(other_server_psk, S2N_PSK_TYPE_EXTERNAL));
1428 EXPECT_SUCCESS(s2n_psk_set_identity(other_server_psk, test_identity_2, sizeof(test_identity_2)));
1429
1430 /* Write the ClientHello prefix */
1431 EXPECT_SUCCESS(s2n_stuffer_write_bytes(client_out,
1432 client_hello_prefix_data, sizeof(client_hello_prefix_data)));
1433
1434 EXPECT_SUCCESS(s2n_client_psk_extension.send(client_conn, client_out));
1435 EXPECT_OK(s2n_finish_psk_extension(client_conn));
1436
1437 /* Copy the ClientHello over to the server's input buffer, but skip ClientHello prefix */
1438 EXPECT_SUCCESS(s2n_stuffer_copy(client_out, server_in,
1439 s2n_stuffer_data_available(client_out)));
1440 EXPECT_SUCCESS(s2n_stuffer_skip_read(server_in, sizeof(client_hello_prefix_data)));
1441
1442 EXPECT_SUCCESS(s2n_client_psk_recv(server_conn, server_in));
1443 EXPECT_EQUAL(server_conn->psk_params.chosen_psk_wire_index, 0);
1444 EXPECT_EQUAL(server_conn->psk_params.chosen_psk, shared_psk);
1445
1446 EXPECT_SUCCESS(s2n_connection_free(server_conn));
1447 EXPECT_SUCCESS(s2n_connection_free(client_conn));
1448 };
1449 };
1450
1451 /* Functional test */
1452 if (s2n_is_tls13_fully_supported()) {
1453 /* Setup connections */
1454 struct s2n_connection *client_conn = NULL, *server_conn = NULL;
1455 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
1456 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
1457 EXPECT_OK(s2n_connection_set_psk_type(server_conn, S2N_PSK_TYPE_EXTERNAL));
1458 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, "default_tls13"));
1459 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "default_tls13"));
1460
1461 /* Create nonblocking pipes */
1462 struct s2n_test_io_pair io_pair = { 0 };
1463 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
1464 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
1465 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
1466
1467 /* Setup other client PSK */
1468 uint8_t other_client_data[] = "other client data";
1469 struct s2n_psk *other_client_psk = NULL;
1470 EXPECT_OK(s2n_array_pushback(&client_conn->psk_params.psk_list, (void **) &other_client_psk));
1471 EXPECT_OK(s2n_psk_init(other_client_psk, S2N_PSK_TYPE_EXTERNAL));
1472 EXPECT_SUCCESS(s2n_psk_set_identity(other_client_psk, other_client_data, sizeof(other_client_data)));
1473 EXPECT_SUCCESS(s2n_psk_set_secret(other_client_psk, other_client_data, sizeof(other_client_data)));
1474
1475 /* Setup other server PSK */
1476 uint8_t other_server_data[] = "other server data";
1477 struct s2n_psk *other_server_psk = NULL;
1478 EXPECT_OK(s2n_array_pushback(&server_conn->psk_params.psk_list, (void **) &other_server_psk));
1479 EXPECT_OK(s2n_psk_init(other_server_psk, S2N_PSK_TYPE_EXTERNAL));
1480 EXPECT_SUCCESS(s2n_psk_set_identity(other_server_psk, other_server_data, sizeof(other_server_data)));
1481 EXPECT_SUCCESS(s2n_psk_set_secret(other_server_psk, other_server_data, sizeof(other_server_data)));
1482
1483 /* Setup shared PSK for client */
1484 struct s2n_psk *shared_psk = NULL;
1485 EXPECT_OK(s2n_array_pushback(&client_conn->psk_params.psk_list, (void **) &shared_psk));
1486 EXPECT_OK(s2n_psk_init(shared_psk, S2N_PSK_TYPE_EXTERNAL));
1487 EXPECT_SUCCESS(s2n_psk_set_identity(shared_psk, test_identity, sizeof(test_identity)));
1488 EXPECT_SUCCESS(s2n_psk_set_secret(shared_psk, test_secret, sizeof(test_secret)));
1489
1490 /* Setup shared PSK for server */
1491 EXPECT_OK(s2n_array_pushback(&server_conn->psk_params.psk_list, (void **) &shared_psk));
1492 EXPECT_OK(s2n_psk_init(shared_psk, S2N_PSK_TYPE_EXTERNAL));
1493 EXPECT_SUCCESS(s2n_psk_set_identity(shared_psk, test_identity, sizeof(test_identity)));
1494 EXPECT_SUCCESS(s2n_psk_set_secret(shared_psk, test_secret, sizeof(test_secret)));
1495
1496 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
1497 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
1498 s2n_stuffer_data_available(&client_conn->handshake.io)));
1499 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
1500
1501 /* Verify shared PSK chosen */
1502 EXPECT_EQUAL(server_conn->psk_params.chosen_psk_wire_index, 1);
1503 EXPECT_EQUAL(server_conn->psk_params.chosen_psk, shared_psk);
1504 EXPECT_EQUAL(shared_psk->secret.size, sizeof(test_secret));
1505 EXPECT_BYTEARRAY_EQUAL(shared_psk->secret.data, test_secret, sizeof(test_secret));
1506
1507 EXPECT_SUCCESS(s2n_connection_free(server_conn));
1508 EXPECT_SUCCESS(s2n_connection_free(client_conn));
1509 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
1510 }
1511
1512 /**
1513 * Ensure obfuscated_ticket_age and binder values are updated on a client hello after a HRR
1514 *
1515 *= https://www.rfc-editor.org/rfc/rfc8446#section-4.1.2
1516 *= type=test
1517 *# - Updating the "pre_shared_key" extension if present by recomputing
1518 *# the "obfuscated_ticket_age" and binder values and (optionally)
1519 *# removing any PSKs which are incompatible with the server's
1520 *# indicated cipher suite.
1521 **/
1522 if (s2n_is_tls13_fully_supported()) {
1523 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL,
1524 s2n_cert_chain_and_key_ptr_free);
1525 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
1526 S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN,
1527 S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY));
1528
1529 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(),
1530 s2n_config_ptr_free);
1531 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
1532 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13"));
1533 EXPECT_SUCCESS(s2n_config_disable_x509_verification(config));
1534 EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));
1535
1536 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
1537 s2n_connection_ptr_free);
1538 EXPECT_NOT_NULL(server_conn);
1539 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
1540
1541 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
1542 s2n_connection_ptr_free);
1543 EXPECT_NOT_NULL(client_conn);
1544 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
1545
1546 DEFER_CLEANUP(struct s2n_stuffer io_stuffer = { 0 }, s2n_stuffer_free);
1547 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&io_stuffer, 0));
1548 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&io_stuffer, &io_stuffer, client_conn));
1549 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&io_stuffer, &io_stuffer, server_conn));
1550
1551 /* Force the HRR path */
1552 client_conn->security_policy_override = &security_policy_test_tls13_retry;
1553
1554 client_conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
1555
1556 /* Set a resumption psk */
1557 DEFER_CLEANUP(struct s2n_stuffer psk_identity = { 0 }, s2n_stuffer_free);
1558 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&psk_identity, 0));
1559 EXPECT_OK(s2n_setup_encrypted_ticket(client_conn, &psk_identity));
1560 struct s2n_offered_psk_list identity_list = { .conn = client_conn };
1561 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&identity_list.wire_data, 0));
1562 EXPECT_OK(s2n_write_test_identity(&identity_list.wire_data, &psk_identity.blob));
1563 EXPECT_OK(s2n_select_resumption_psk(server_conn, &identity_list));
1564
1565 /* Set the ticket issue time to be 10 milliseconds ago, so the obfuscated ticket age will be non-zero */
1566 struct s2n_psk *psk = client_conn->psk_params.chosen_psk;
1567 psk->ticket_issue_time -= MILLIS_TO_NANOS(10);
1568
1569 /* Calculate the size of the identity without the key name. Used to first seek to the key name as the target for
1570 * s2n_stuffer_skip_read_until, and then seek to the end of the identity. */
1571 const unsigned long identity_data_size = psk->identity.size - strlen((char *) psk->identity.data);
1572
1573 s2n_blocked_status blocked = 0;
1574
1575 /* Client sends ClientHello 1 */
1576 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
1577
1578 /* Read the obfuscated ticket age from ClientHello 1 */
1579 EXPECT_SUCCESS(s2n_stuffer_skip_read_until(&io_stuffer, (char *) psk->identity.data));
1580 EXPECT_SUCCESS(s2n_stuffer_skip_read(&io_stuffer, identity_data_size));
1581 uint32_t obfuscated_ticket_age_1 = 0;
1582 EXPECT_SUCCESS(s2n_stuffer_read_uint32(&io_stuffer, &obfuscated_ticket_age_1));
1583
1584 /* Skip over the size of the binders list */
1585 EXPECT_SUCCESS(s2n_stuffer_skip_read(&io_stuffer, sizeof(uint16_t)));
1586
1587 /* Ensure the binder size is as expected */
1588 uint8_t binder_1_size = 0;
1589 EXPECT_SUCCESS(s2n_stuffer_read_uint8(&io_stuffer, &binder_1_size));
1590 EXPECT_TRUE(binder_1_size == BINDER_SIZE);
1591
1592 /* Read the binder from ClientHello 1 */
1593 uint8_t binder_1[BINDER_SIZE] = { 0 };
1594 EXPECT_SUCCESS(s2n_stuffer_read_bytes(&io_stuffer, binder_1, BINDER_SIZE));
1595 EXPECT_SUCCESS(s2n_stuffer_reread(&io_stuffer));
1596
1597 /* Ensure that the ticket age and binder are non-zero */
1598 EXPECT_TRUE(obfuscated_ticket_age_1 != 0);
1599 uint8_t zero_array[BINDER_SIZE] = { 0 };
1600 EXPECT_FALSE(s2n_constant_time_equals(binder_1, zero_array, BINDER_SIZE));
1601
1602 /* Skip to before the client sends ClientHello 2 */
1603 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, HELLO_RETRY_MSG));
1604 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, CLIENT_HELLO));
1605 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, CLIENT_HELLO));
1606
1607 /* Change the ticket issue time so a new obfuscated ticket age will change */
1608 psk->ticket_issue_time -= MILLIS_TO_NANOS(1);
1609
1610 /* Client sends ClientHello 2 */
1611 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
1612
1613 /* Read the obfuscated ticket age from ClientHello 2 */
1614 EXPECT_SUCCESS(s2n_stuffer_skip_read_until(&io_stuffer, (char *) psk->identity.data));
1615 EXPECT_SUCCESS(s2n_stuffer_skip_read(&io_stuffer, identity_data_size));
1616 uint32_t obfuscated_ticket_age_2 = 0;
1617 EXPECT_SUCCESS(s2n_stuffer_read_uint32(&io_stuffer, &obfuscated_ticket_age_2));
1618
1619 /* Skip over the size of the binders list */
1620 EXPECT_SUCCESS(s2n_stuffer_skip_read(&io_stuffer, sizeof(uint16_t)));
1621
1622 /* Ensure the binder size is as expected */
1623 uint8_t binder_2_size = 0;
1624 EXPECT_SUCCESS(s2n_stuffer_read_uint8(&io_stuffer, &binder_2_size));
1625 EXPECT_TRUE(binder_2_size == BINDER_SIZE);
1626
1627 /* Read the binder from ClientHello 2 */
1628 uint8_t binder_2[BINDER_SIZE] = { 0 };
1629 EXPECT_SUCCESS(s2n_stuffer_read_bytes(&io_stuffer, binder_2, BINDER_SIZE));
1630
1631 /* Ensure that the ticket age and binder were updated after ClientHello 2 */
1632 EXPECT_TRUE(obfuscated_ticket_age_1 != obfuscated_ticket_age_2);
1633 EXPECT_FALSE(s2n_constant_time_equals(binder_1, binder_2, BINDER_SIZE));
1634
1635 EXPECT_SUCCESS(s2n_stuffer_free(&identity_list.wire_data));
1636 }
1637
1638 /* Receive a valid extension with a large ClientHello (> UINT16_MAX) */
1639 {
1640 s2n_extension_type_id key_share_id = 0;
1641 s2n_extension_type_id psk_ke_mode_id = 0;
1642 EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_KEY_SHARE, &key_share_id));
1643 EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_PSK_KEY_EXCHANGE_MODES, &psk_ke_mode_id));
1644
1645 uint8_t large_test_identity[] = "test identity";
1646 uint8_t large_test_secret[] = "test secret";
1647
1648 struct s2n_connection *client_conn = NULL;
1649 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
1650 struct s2n_stuffer *client_out = &client_conn->handshake.io;
1651
1652 struct s2n_connection *server_conn = NULL;
1653 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
1654 EXPECT_OK(s2n_connection_set_psk_type(server_conn, S2N_PSK_TYPE_EXTERNAL));
1655 struct s2n_stuffer *server_in = &server_conn->handshake.io;
1656 /* s2n_client_psk_recv checks that extensions.count > 0 and that PSK is the last
1657 * extension. Set to 1 because we bypass normal ClientHello parsing. */
1658 server_conn->client_hello.extensions.count = 1;
1659
1660 server_conn->psk_params.psk_ke_mode = S2N_PSK_DHE_KE;
1661 S2N_CBIT_SET(server_conn->extension_requests_received, psk_ke_mode_id);
1662 S2N_CBIT_SET(server_conn->extension_requests_received, key_share_id);
1663
1664 struct s2n_psk *shared_psk = NULL;
1665 EXPECT_OK(s2n_array_pushback(&client_conn->psk_params.psk_list, (void **) &shared_psk));
1666 EXPECT_OK(s2n_psk_init(shared_psk, S2N_PSK_TYPE_EXTERNAL));
1667 EXPECT_SUCCESS(s2n_psk_set_identity(shared_psk, large_test_identity, sizeof(large_test_identity)));
1668 EXPECT_SUCCESS(s2n_psk_set_secret(shared_psk, large_test_secret, sizeof(large_test_secret)));
1669 EXPECT_OK(s2n_array_pushback(&server_conn->psk_params.psk_list, (void **) &shared_psk));
1670 EXPECT_OK(s2n_psk_init(shared_psk, S2N_PSK_TYPE_EXTERNAL));
1671 EXPECT_SUCCESS(s2n_psk_set_identity(shared_psk, large_test_identity, sizeof(large_test_identity)));
1672 EXPECT_SUCCESS(s2n_psk_set_secret(shared_psk, large_test_secret, sizeof(large_test_secret)));
1673
1674 /* Write a large ClientHello prefix (> UINT16_MAX) to trigger the truncation bug. */
1675 uint32_t large_prefix_size = UINT16_MAX + 100;
1676 EXPECT_SUCCESS(s2n_stuffer_skip_write(client_out, large_prefix_size));
1677
1678 EXPECT_SUCCESS(s2n_client_psk_extension.send(client_conn, client_out));
1679 EXPECT_OK(s2n_finish_psk_extension(client_conn));
1680
1681 /* Copy the ClientHello to the server, skipping the large prefix */
1682 EXPECT_SUCCESS(s2n_stuffer_copy(client_out, server_in,
1683 s2n_stuffer_data_available(client_out)));
1684 EXPECT_SUCCESS(s2n_stuffer_skip_read(server_in, large_prefix_size));
1685
1686 EXPECT_SUCCESS(s2n_client_psk_recv(server_conn, server_in));
1687 EXPECT_EQUAL(server_conn->psk_params.chosen_psk, shared_psk);
1688
1689 EXPECT_SUCCESS(s2n_connection_free(server_conn));
1690 EXPECT_SUCCESS(s2n_connection_free(client_conn));
1691 };
1692
1693 END_TEST();
1694 }