Debian Codegraph @

crt/s2n/tests/unit/s2n_psk_offered_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 #include "tls/extensions/s2n_client_psk.h"
19
20 /* Include source to test static methods. */
21 #include "tls/s2n_psk.c"
22
23 #define MILLIS_TO_NANOS(millis) (millis * (uint64_t) ONE_MILLISEC_IN_NANOS)
24
25 static S2N_RESULT s2n_setup_encrypted_ticket(struct s2n_connection *conn, struct s2n_stuffer *output)
26 {
27 conn->tls13_ticket_fields = (struct s2n_ticket_fields){ 0 };
28 uint8_t test_secret_data[] = "test secret";
29 RESULT_GUARD_POSIX(s2n_alloc(&conn->tls13_ticket_fields.session_secret, sizeof(test_secret_data)));
30 RESULT_CHECKED_MEMCPY(conn->tls13_ticket_fields.session_secret.data, test_secret_data, sizeof(test_secret_data));
31
32 struct s2n_ticket_key *key = s2n_get_ticket_encrypt_decrypt_key(conn->config);
33 RESULT_ENSURE(key != NULL, S2N_ERR_NO_TICKET_ENCRYPT_DECRYPT_KEY);
34
35 /* Create a valid resumption psk identity */
36 RESULT_GUARD(s2n_resume_encrypt_session_ticket(conn, key, output));
37 output->blob.size = s2n_stuffer_data_available(output);
38
39 return S2N_RESULT_OK;
40 }
41
42 static S2N_RESULT s2n_write_test_identity(struct s2n_stuffer *out, const uint8_t *identity, uint16_t identity_size)
43 {
44 RESULT_GUARD_POSIX(s2n_stuffer_write_uint16(out, identity_size));
45 RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(out, identity, identity_size));
46 RESULT_GUARD_POSIX(s2n_stuffer_write_uint32(out, 0));
47 return S2N_RESULT_OK;
48 }
49
50 int main(int argc, char **argv)
51 {
52 BEGIN_TEST();
53
54 const uint8_t wire_identity_1[] = { "one" };
55 const uint8_t wire_identity_2[] = { "two" };
56
57 /* Test s2n_offered_psk_list_has_next */
58 {
59 struct s2n_offered_psk_list psk_list = { 0 };
60
61 /* Safety check */
62 EXPECT_FALSE(s2n_offered_psk_list_has_next(NULL));
63
64 /* Empty list */
65 EXPECT_FALSE(s2n_offered_psk_list_has_next(&psk_list));
66
67 /* Contains data */
68 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&psk_list.wire_data, 0));
69 EXPECT_SUCCESS(s2n_stuffer_skip_write(&psk_list.wire_data, 1));
70 EXPECT_TRUE(s2n_offered_psk_list_has_next(&psk_list));
71
72 /* Out of data */
73 EXPECT_SUCCESS(s2n_stuffer_skip_read(&psk_list.wire_data, 1));
74 EXPECT_FALSE(s2n_offered_psk_list_has_next(&psk_list));
75
76 EXPECT_SUCCESS(s2n_stuffer_free(&psk_list.wire_data));
77 };
78
79 /* Test s2n_offered_psk_list_next */
80 {
81 /* Safety checks */
82 {
83 struct s2n_offered_psk_list psk_list = { 0 };
84 struct s2n_offered_psk psk = { 0 };
85 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_psk_list_next(&psk_list, NULL), S2N_ERR_NULL);
86 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_psk_list_next(NULL, &psk), S2N_ERR_NULL);
87 };
88
89 /* Empty list */
90 {
91 struct s2n_offered_psk_list psk_list = { 0 };
92 struct s2n_offered_psk psk = { 0 };
93
94 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_psk_list_next(&psk_list, &psk), S2N_ERR_STUFFER_OUT_OF_DATA);
95 EXPECT_EQUAL(psk.identity.size, 0);
96 EXPECT_EQUAL(psk.identity.data, NULL);
97
98 /* Calling again produces same result */
99 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_psk_list_next(&psk_list, &psk), S2N_ERR_STUFFER_OUT_OF_DATA);
100 EXPECT_EQUAL(psk.identity.size, 0);
101 EXPECT_EQUAL(psk.identity.data, NULL);
102 };
103
104 /* Parses only element in list */
105 {
106 struct s2n_offered_psk psk = { 0 };
107 struct s2n_offered_psk_list psk_list = { 0 };
108 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
109 EXPECT_NOT_NULL(conn);
110 psk_list.conn = conn;
111
112 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&psk_list.wire_data, 0));
113 EXPECT_OK(s2n_write_test_identity(&psk_list.wire_data, wire_identity_1, sizeof(wire_identity_1)));
114
115 EXPECT_SUCCESS(s2n_offered_psk_list_next(&psk_list, &psk));
116 EXPECT_EQUAL(psk.identity.size, sizeof(wire_identity_1));
117 EXPECT_BYTEARRAY_EQUAL(psk.identity.data, wire_identity_1, sizeof(wire_identity_1));
118
119 /* Trying to retrieve a second element fails */
120 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_psk_list_next(&psk_list, &psk), S2N_ERR_STUFFER_OUT_OF_DATA);
121 EXPECT_EQUAL(psk.identity.size, 0);
122 EXPECT_EQUAL(psk.identity.data, NULL);
123
124 EXPECT_SUCCESS(s2n_stuffer_free(&psk_list.wire_data));
125 EXPECT_SUCCESS(s2n_connection_free(conn));
126 };
127
128 /* Fails to parse zero-length identities */
129 {
130 struct s2n_offered_psk psk = { 0 };
131 struct s2n_offered_psk_list psk_list = { 0 };
132
133 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&psk_list.wire_data, 0));
134 EXPECT_OK(s2n_write_test_identity(&psk_list.wire_data, wire_identity_1, 0));
135
136 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_psk_list_next(&psk_list, &psk), S2N_ERR_BAD_MESSAGE);
137 EXPECT_EQUAL(psk.identity.size, 0);
138 EXPECT_EQUAL(psk.identity.data, NULL);
139
140 EXPECT_SUCCESS(s2n_stuffer_free(&psk_list.wire_data));
141 };
142
143 /* Fails to parse partial identities */
144 {
145 struct s2n_offered_psk psk = { 0 };
146 struct s2n_offered_psk_list psk_list = { 0 };
147
148 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&psk_list.wire_data, 0));
149 EXPECT_OK(s2n_write_test_identity(&psk_list.wire_data, wire_identity_1, 0));
150 EXPECT_SUCCESS(s2n_stuffer_wipe_n(&psk_list.wire_data, 1));
151
152 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_psk_list_next(&psk_list, &psk), S2N_ERR_BAD_MESSAGE);
153 EXPECT_EQUAL(psk.identity.size, 0);
154 EXPECT_EQUAL(psk.identity.data, NULL);
155
156 EXPECT_SUCCESS(s2n_stuffer_free(&psk_list.wire_data));
157 };
158
159 /* Parses multiple elements from list */
160 {
161 struct s2n_offered_psk psk = { 0 };
162 struct s2n_offered_psk_list psk_list = { 0 };
163 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
164 EXPECT_NOT_NULL(conn);
165 psk_list.conn = conn;
166
167 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&psk_list.wire_data, 0));
168 EXPECT_OK(s2n_write_test_identity(&psk_list.wire_data, wire_identity_1, sizeof(wire_identity_1)));
169 EXPECT_OK(s2n_write_test_identity(&psk_list.wire_data, wire_identity_2, sizeof(wire_identity_2)));
170
171 EXPECT_SUCCESS(s2n_offered_psk_list_next(&psk_list, &psk));
172 EXPECT_EQUAL(psk.identity.size, sizeof(wire_identity_1));
173 EXPECT_BYTEARRAY_EQUAL(psk.identity.data, wire_identity_1, sizeof(wire_identity_1));
174
175 EXPECT_SUCCESS(s2n_offered_psk_list_next(&psk_list, &psk));
176 EXPECT_EQUAL(psk.identity.size, sizeof(wire_identity_2));
177 EXPECT_BYTEARRAY_EQUAL(psk.identity.data, wire_identity_2, sizeof(wire_identity_2));
178
179 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_psk_list_next(&psk_list, &psk), S2N_ERR_STUFFER_OUT_OF_DATA);
180 EXPECT_EQUAL(psk.identity.size, 0);
181 EXPECT_EQUAL(psk.identity.data, NULL);
182
183 EXPECT_SUCCESS(s2n_stuffer_free(&psk_list.wire_data));
184 EXPECT_SUCCESS(s2n_connection_free(conn));
185 };
186 };
187
188 /* Test s2n_offered_psk_list_read_next */
189 {
190 /* Safety check */
191 {
192 struct s2n_offered_psk psk = { 0 };
193 struct s2n_offered_psk_list psk_list = { 0 };
194 EXPECT_ERROR_WITH_ERRNO(s2n_offered_psk_list_read_next(&psk_list, NULL), S2N_ERR_NULL);
195 EXPECT_ERROR_WITH_ERRNO(s2n_offered_psk_list_read_next(NULL, &psk), S2N_ERR_NULL);
196 };
197
198 /**
199 *= https://www.rfc-editor.org/rfc/rfc8446#section-4.2.11
200 *= type=test
201 *# For identities established externally, an obfuscated_ticket_age of 0 SHOULD be
202 *# used, and servers MUST ignore the value.
203 */
204 {
205 struct s2n_offered_psk psk = { 0 };
206 struct s2n_offered_psk_list psk_list = { 0 };
207 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
208 EXPECT_NOT_NULL(conn);
209 conn->psk_params.type = S2N_PSK_TYPE_EXTERNAL;
210 psk_list.conn = conn;
211 uint32_t obfuscated_ticket_age = 100;
212
213 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&psk_list.wire_data, 0));
214 EXPECT_SUCCESS(s2n_stuffer_write_uint16(&psk_list.wire_data, sizeof(wire_identity_1)));
215 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&psk_list.wire_data, wire_identity_1, sizeof(wire_identity_1)));
216 EXPECT_SUCCESS(s2n_stuffer_write_uint32(&psk_list.wire_data, obfuscated_ticket_age));
217
218 EXPECT_OK(s2n_offered_psk_list_read_next(&psk_list, &psk));
219
220 EXPECT_EQUAL(psk.obfuscated_ticket_age, 0);
221
222 EXPECT_SUCCESS(s2n_stuffer_free(&psk_list.wire_data));
223 EXPECT_SUCCESS(s2n_connection_free(conn));
224 };
225
226 /* Resumption PSKs do not skip obfuscated ticket age parsing */
227 {
228 struct s2n_offered_psk psk = { 0 };
229 struct s2n_offered_psk_list psk_list = { 0 };
230 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
231 EXPECT_NOT_NULL(conn);
232 conn->psk_params.type = S2N_PSK_TYPE_RESUMPTION;
233 psk_list.conn = conn;
234 uint32_t obfuscated_ticket_age = 100;
235
236 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&psk_list.wire_data, 0));
237 EXPECT_SUCCESS(s2n_stuffer_write_uint16(&psk_list.wire_data, sizeof(wire_identity_1)));
238 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&psk_list.wire_data, wire_identity_1, sizeof(wire_identity_1)));
239 EXPECT_SUCCESS(s2n_stuffer_write_uint32(&psk_list.wire_data, obfuscated_ticket_age));
240
241 EXPECT_OK(s2n_offered_psk_list_read_next(&psk_list, &psk));
242
243 EXPECT_EQUAL(psk.obfuscated_ticket_age, obfuscated_ticket_age);
244
245 EXPECT_SUCCESS(s2n_stuffer_free(&psk_list.wire_data));
246 EXPECT_SUCCESS(s2n_connection_free(conn));
247 }
248 };
249
250 /* Test s2n_offered_psk_list_reread */
251 {
252 /* Safety check */
253 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_psk_list_reread(NULL), S2N_ERR_NULL);
254
255 /* No-op on empty list */
256 {
257 struct s2n_offered_psk psk = { 0 };
258 struct s2n_offered_psk_list psk_list = { 0 };
259
260 EXPECT_SUCCESS(s2n_offered_psk_list_reread(&psk_list));
261 EXPECT_SUCCESS(s2n_offered_psk_list_reread(&psk_list));
262
263 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_psk_list_next(&psk_list, &psk), S2N_ERR_STUFFER_OUT_OF_DATA);
264 };
265
266 /* Resets non-empty list */
267 {
268 struct s2n_offered_psk psk = { 0 };
269 struct s2n_offered_psk_list psk_list = { 0 };
270 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
271 EXPECT_NOT_NULL(conn);
272 psk_list.conn = conn;
273
274 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&psk_list.wire_data, 0));
275 EXPECT_OK(s2n_write_test_identity(&psk_list.wire_data, wire_identity_1, sizeof(wire_identity_1)));
276
277 EXPECT_SUCCESS(s2n_offered_psk_list_next(&psk_list, &psk));
278 EXPECT_EQUAL(psk.identity.size, sizeof(wire_identity_1));
279 EXPECT_BYTEARRAY_EQUAL(psk.identity.data, wire_identity_1, sizeof(wire_identity_1));
280
281 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_psk_list_next(&psk_list, &psk), S2N_ERR_STUFFER_OUT_OF_DATA);
282
283 EXPECT_SUCCESS(s2n_offered_psk_list_reread(&psk_list));
284
285 EXPECT_SUCCESS(s2n_offered_psk_list_next(&psk_list, &psk));
286 EXPECT_EQUAL(psk.identity.size, sizeof(wire_identity_1));
287 EXPECT_BYTEARRAY_EQUAL(psk.identity.data, wire_identity_1, sizeof(wire_identity_1));
288
289 EXPECT_SUCCESS(s2n_stuffer_free(&psk_list.wire_data));
290 EXPECT_SUCCESS(s2n_connection_free(conn));
291 };
292 };
293
294 /* Test s2n_offered_psk_new */
295 {
296 struct s2n_offered_psk zeroed_psk = { 0 };
297 POSIX_CHECKED_MEMSET(&zeroed_psk, 0, sizeof(struct s2n_offered_psk));
298 DEFER_CLEANUP(struct s2n_offered_psk *new_psk = s2n_offered_psk_new(), s2n_offered_psk_free);
299 EXPECT_NOT_NULL(new_psk);
300
301 /* _new equivalent to a zero-inited structure */
302 EXPECT_BYTEARRAY_EQUAL(&zeroed_psk, new_psk, sizeof(struct s2n_offered_psk));
303 };
304
305 /* Test s2n_offered_psk_free */
306 {
307 EXPECT_SUCCESS(s2n_offered_psk_free(NULL));
308
309 struct s2n_offered_psk *new_psk = s2n_offered_psk_new();
310 EXPECT_NOT_NULL(new_psk);
311 EXPECT_SUCCESS(s2n_offered_psk_free(&new_psk));
312 EXPECT_NULL(new_psk);
313 };
314
315 /* Test s2n_offered_psk_get_identity */
316 {
317 /* Safety checks */
318 {
319 struct s2n_offered_psk psk = { 0 };
320 uint8_t *data = NULL;
321 uint16_t size = 0;
322 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_psk_get_identity(NULL, &data, &size), S2N_ERR_NULL);
323 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_psk_get_identity(&psk, NULL, &size), S2N_ERR_NULL);
324 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_psk_get_identity(&psk, &data, NULL), S2N_ERR_NULL);
325 };
326
327 /* Empty identity */
328 {
329 DEFER_CLEANUP(struct s2n_offered_psk *psk = s2n_offered_psk_new(), s2n_offered_psk_free);
330
331 uint8_t *data = NULL;
332 uint16_t size = 0;
333 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_psk_get_identity(psk, &data, &size), S2N_ERR_NULL);
334 };
335
336 /* Valid identity */
337 {
338 uint8_t wire_identity[] = "identity";
339 DEFER_CLEANUP(struct s2n_offered_psk *psk = s2n_offered_psk_new(), s2n_offered_psk_free);
340 EXPECT_SUCCESS(s2n_blob_init(&psk->identity, wire_identity, sizeof(wire_identity)));
341
342 uint8_t *data = NULL;
343 uint16_t size = 0;
344 EXPECT_SUCCESS(s2n_offered_psk_get_identity(psk, &data, &size));
345 EXPECT_EQUAL(size, sizeof(wire_identity));
346 EXPECT_BYTEARRAY_EQUAL(data, wire_identity, sizeof(wire_identity));
347 };
348 };
349
350 /* Test: s2n_validate_ticket_lifetime */
351 {
352 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
353 EXPECT_NOT_NULL(conn);
354 uint32_t ticket_age_add = 0;
355 uint32_t obfuscated_ticket_age = 0;
356
357 /* Ticket age is smaller than session lifetime */
358 conn->config->session_state_lifetime_in_nanos = MILLIS_TO_NANOS(100);
359 EXPECT_OK(s2n_validate_ticket_lifetime(conn, obfuscated_ticket_age, ticket_age_add));
360
361 /* Ticket age is greater than session lifetime */
362 obfuscated_ticket_age = 101;
363 conn->config->session_state_lifetime_in_nanos = MILLIS_TO_NANOS(100);
364 EXPECT_ERROR_WITH_ERRNO(s2n_validate_ticket_lifetime(conn, obfuscated_ticket_age, ticket_age_add), S2N_ERR_INVALID_SESSION_TICKET);
365
366 /* ticket_age_add is greater than obfuscated ticket age and ticket age
367 * is smaller than session lifetime */
368 obfuscated_ticket_age = 100;
369 ticket_age_add = 1000;
370 conn->config->session_state_lifetime_in_nanos = MILLIS_TO_NANOS(UINT32_MAX);
371 EXPECT_OK(s2n_validate_ticket_lifetime(conn, obfuscated_ticket_age, ticket_age_add));
372
373 EXPECT_SUCCESS(s2n_connection_free(conn));
374 };
375
376 /* Test: s2n_offered_psk_list_choose_psk */
377 {
378 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
379 EXPECT_NOT_NULL(conn);
380
381 struct s2n_array *known_psks = &conn->psk_params.psk_list;
382
383 const uint16_t wire_index = 5;
384 uint8_t wire_identity_bytes[] = "wire_identity_bytes";
385 struct s2n_blob wire_identity = { 0 };
386 EXPECT_SUCCESS(s2n_blob_init(&wire_identity, wire_identity_bytes, sizeof(wire_identity_bytes)));
387 struct s2n_offered_psk offered_psk = { .identity = wire_identity, .wire_index = wire_index };
388
389 struct s2n_offered_psk_list offered_psk_list = { .conn = conn };
390 conn->psk_params.type = S2N_PSK_TYPE_EXTERNAL;
391
392 /* Safety */
393 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_psk_list_choose_psk(NULL, &offered_psk), S2N_ERR_NULL);
394
395 /* Test: No known PSKs */
396 {
397 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_psk_list_choose_psk(&offered_psk_list, &offered_psk), S2N_ERR_NULL);
398 EXPECT_NULL(conn->psk_params.chosen_psk);
399 };
400
401 /* Test: No match exists */
402 {
403 struct s2n_psk *different_identity = NULL;
404 EXPECT_OK(s2n_array_pushback(known_psks, (void **) &different_identity));
405 EXPECT_OK(s2n_psk_init(different_identity, S2N_PSK_TYPE_EXTERNAL));
406 EXPECT_SUCCESS(s2n_psk_set_identity(different_identity, wire_identity_2, sizeof(wire_identity_2)));
407
408 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_psk_list_choose_psk(&offered_psk_list, &offered_psk), S2N_ERR_NULL);
409 EXPECT_NULL(conn->psk_params.chosen_psk);
410 };
411
412 struct s2n_psk *expected_match = NULL;
413
414 /* Test: Match exists */
415 {
416 EXPECT_OK(s2n_array_pushback(known_psks, (void **) &expected_match));
417 EXPECT_OK(s2n_psk_init(expected_match, S2N_PSK_TYPE_EXTERNAL));
418 EXPECT_SUCCESS(s2n_psk_set_identity(expected_match, wire_identity_bytes, sizeof(wire_identity_bytes)));
419
420 EXPECT_SUCCESS(s2n_offered_psk_list_choose_psk(&offered_psk_list, &offered_psk));
421 EXPECT_EQUAL(conn->psk_params.chosen_psk, expected_match);
422 EXPECT_EQUAL(conn->psk_params.chosen_psk_wire_index, wire_index);
423 };
424
425 /* Test: set NULL PSK */
426 {
427 EXPECT_SUCCESS(s2n_offered_psk_list_choose_psk(&offered_psk_list, NULL));
428 EXPECT_NULL(conn->psk_params.chosen_psk);
429 };
430
431 /* Test: Multiple matches exist */
432 {
433 struct s2n_psk *another_match = NULL;
434 EXPECT_OK(s2n_array_pushback(known_psks, (void **) &another_match));
435 EXPECT_OK(s2n_psk_init(another_match, S2N_PSK_TYPE_EXTERNAL));
436 EXPECT_SUCCESS(s2n_psk_set_identity(another_match, wire_identity_bytes, sizeof(wire_identity_bytes)));
437
438 EXPECT_SUCCESS(s2n_offered_psk_list_choose_psk(&offered_psk_list, &offered_psk));
439 EXPECT_EQUAL(conn->psk_params.chosen_psk, expected_match);
440 EXPECT_EQUAL(conn->psk_params.chosen_psk_wire_index, wire_index);
441 };
442
443 EXPECT_SUCCESS(s2n_connection_free(conn));
444
445 /* Test: Valid resumption psk is received */
446 {
447 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
448 EXPECT_NOT_NULL(server_conn);
449
450 struct s2n_config *config = s2n_config_new();
451 EXPECT_NOT_NULL(config);
452 EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));
453 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
454
455 server_conn->psk_params.type = S2N_PSK_TYPE_RESUMPTION;
456 server_conn->actual_protocol_version = S2N_TLS13;
457 server_conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
458
459 DEFER_CLEANUP(struct s2n_stuffer psk_identity = { 0 }, s2n_stuffer_free);
460 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&psk_identity, 0));
461 EXPECT_OK(s2n_setup_encrypted_ticket(server_conn, &psk_identity));
462
463 struct s2n_offered_psk_list identity_list = { .conn = server_conn };
464
465 DEFER_CLEANUP(struct s2n_stuffer test_stuffer = { 0 }, s2n_stuffer_free);
466 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&test_stuffer, 0));
467 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&test_stuffer, psk_identity.blob.data, psk_identity.blob.size));
468 test_stuffer.blob.size = s2n_stuffer_data_available(&psk_identity);
469
470 struct s2n_offered_psk client_psk = { .identity = test_stuffer.blob, .wire_index = wire_index };
471
472 EXPECT_SUCCESS(s2n_offered_psk_list_choose_psk(&identity_list, &client_psk));
473
474 EXPECT_EQUAL(server_conn->psk_params.chosen_psk_wire_index, wire_index);
475 EXPECT_NOT_NULL(server_conn->psk_params.chosen_psk);
476
477 /* Sanity check psk creation is correct */
478 EXPECT_EQUAL(server_conn->psk_params.chosen_psk->hmac_alg, s2n_tls13_aes_128_gcm_sha256.prf_alg);
479
480 EXPECT_SUCCESS(s2n_config_free(config));
481 EXPECT_SUCCESS(s2n_connection_free(server_conn));
482 };
483
484 /* Resumption psk has expired */
485 {
486 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
487 EXPECT_NOT_NULL(server_conn);
488
489 struct s2n_config *config = s2n_config_new();
490 EXPECT_NOT_NULL(config);
491 EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));
492 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
493
494 server_conn->psk_params.type = S2N_PSK_TYPE_RESUMPTION;
495 server_conn->actual_protocol_version = S2N_TLS13;
496 server_conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
497
498 DEFER_CLEANUP(struct s2n_stuffer psk_identity = { 0 }, s2n_stuffer_free);
499 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&psk_identity, 0));
500 EXPECT_OK(s2n_setup_encrypted_ticket(server_conn, &psk_identity));
501
502 struct s2n_offered_psk_list identity_list = { .conn = server_conn };
503
504 DEFER_CLEANUP(struct s2n_stuffer test_stuffer = { 0 }, s2n_stuffer_free);
505 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&test_stuffer, 0));
506 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&test_stuffer, psk_identity.blob.data, psk_identity.blob.size));
507 test_stuffer.blob.size = s2n_stuffer_data_available(&psk_identity);
508
509 /* Obfuscated ticket age is larger than the session lifetime */
510 struct s2n_offered_psk client_psk = { .identity = psk_identity.blob, .wire_index = wire_index, .obfuscated_ticket_age = 100 };
511 server_conn->config->session_state_lifetime_in_nanos = 0;
512
513 EXPECT_FAILURE_WITH_ERRNO(s2n_offered_psk_list_choose_psk(&identity_list, &client_psk), S2N_ERR_INVALID_SESSION_TICKET);
514
515 EXPECT_EQUAL(server_conn->psk_params.chosen_psk_wire_index, 0);
516 EXPECT_NULL(server_conn->psk_params.chosen_psk);
517
518 EXPECT_SUCCESS(s2n_config_free(config));
519 EXPECT_SUCCESS(s2n_connection_free(server_conn));
520 };
521
522 /* Invalid resumption psk is received */
523 {
524 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
525 EXPECT_NOT_NULL(server_conn);
526
527 server_conn->psk_params.type = S2N_PSK_TYPE_RESUMPTION;
528 server_conn->actual_protocol_version = S2N_TLS13;
529 server_conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
530
531 struct s2n_offered_psk_list identity_list = { .conn = server_conn };
532 struct s2n_offered_psk client_psk = { .identity = wire_identity, .wire_index = wire_index };
533
534 EXPECT_FAILURE(s2n_offered_psk_list_choose_psk(&identity_list, &client_psk));
535
536 EXPECT_EQUAL(server_conn->psk_params.chosen_psk_wire_index, 0);
537 EXPECT_NULL(server_conn->psk_params.chosen_psk);
538
539 EXPECT_SUCCESS(s2n_connection_free(server_conn));
540 };
541 };
542
543 /* Functional test: Process the output of sending the psk extension */
544 {
545 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
546 EXPECT_NOT_NULL(conn);
547 conn->psk_params.type = S2N_PSK_TYPE_EXTERNAL;
548
549 const uint8_t test_secret[] = "secret";
550
551 struct s2n_psk *psk1 = NULL;
552 EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &psk1));
553 EXPECT_OK(s2n_psk_init(psk1, S2N_PSK_TYPE_EXTERNAL));
554 EXPECT_SUCCESS(s2n_psk_set_identity(psk1, wire_identity_1, sizeof(wire_identity_1)));
555 EXPECT_SUCCESS(s2n_psk_set_secret(psk1, test_secret, sizeof(test_secret)));
556
557 struct s2n_psk *psk2 = NULL;
558 EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &psk2));
559 EXPECT_OK(s2n_psk_init(psk2, S2N_PSK_TYPE_EXTERNAL));
560 EXPECT_SUCCESS(s2n_psk_set_identity(psk2, wire_identity_2, sizeof(wire_identity_2)));
561 EXPECT_SUCCESS(s2n_psk_set_secret(psk2, test_secret, sizeof(test_secret)));
562
563 EXPECT_SUCCESS(s2n_client_psk_extension.send(conn, &conn->handshake.io));
564
565 /* Skip identity list size */
566 EXPECT_SUCCESS(s2n_stuffer_skip_read(&conn->handshake.io, sizeof(uint16_t)));
567
568 struct s2n_offered_psk_list identity_list = { .conn = conn };
569 const size_t size = s2n_stuffer_data_available(&conn->handshake.io);
570 EXPECT_SUCCESS(s2n_stuffer_alloc(&identity_list.wire_data, size));
571 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&identity_list.wire_data,
572 s2n_stuffer_raw_read(&conn->handshake.io, size), size));
573
574 uint8_t *data = NULL;
575 uint16_t data_size = 0;
576 struct s2n_offered_psk psk = { 0 };
577
578 EXPECT_TRUE(s2n_offered_psk_list_has_next(&identity_list));
579 EXPECT_SUCCESS(s2n_offered_psk_list_next(&identity_list, &psk));
580 EXPECT_SUCCESS(s2n_offered_psk_get_identity(&psk, &data, &data_size));
581 EXPECT_EQUAL(data_size, sizeof(wire_identity_1));
582 EXPECT_BYTEARRAY_EQUAL(data, wire_identity_1, sizeof(wire_identity_1));
583
584 EXPECT_TRUE(s2n_offered_psk_list_has_next(&identity_list));
585 EXPECT_SUCCESS(s2n_offered_psk_list_next(&identity_list, &psk));
586 EXPECT_SUCCESS(s2n_offered_psk_get_identity(&psk, &data, &data_size));
587 EXPECT_EQUAL(data_size, sizeof(wire_identity_2));
588 EXPECT_BYTEARRAY_EQUAL(data, wire_identity_2, sizeof(wire_identity_2));
589
590 EXPECT_SUCCESS(s2n_offered_psk_list_choose_psk(&identity_list, &psk));
591 EXPECT_EQUAL(conn->psk_params.chosen_psk, psk2);
592 EXPECT_EQUAL(conn->psk_params.chosen_psk_wire_index, 1);
593
594 EXPECT_SUCCESS(s2n_offered_psk_list_reread(&identity_list));
595
596 EXPECT_TRUE(s2n_offered_psk_list_has_next(&identity_list));
597 EXPECT_SUCCESS(s2n_offered_psk_list_next(&identity_list, &psk));
598 EXPECT_SUCCESS(s2n_offered_psk_get_identity(&psk, &data, &data_size));
599 EXPECT_EQUAL(data_size, sizeof(wire_identity_1));
600 EXPECT_BYTEARRAY_EQUAL(data, wire_identity_1, sizeof(wire_identity_1));
601
602 EXPECT_SUCCESS(s2n_offered_psk_list_choose_psk(&identity_list, &psk));
603 EXPECT_EQUAL(conn->psk_params.chosen_psk, psk1);
604 EXPECT_EQUAL(conn->psk_params.chosen_psk_wire_index, 0);
605
606 EXPECT_SUCCESS(s2n_connection_free(conn));
607 EXPECT_SUCCESS(s2n_stuffer_free(&identity_list.wire_data));
608 };
609
610 END_TEST();
611 }