Debian Codegraph @

crt/s2n/tests/unit/s2n_psk_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 intermediate functions */
20 #include "tls/s2n_psk.c"
21
22 #define TEST_VALUE_1 "test value"
23 #define TEST_VALUE_2 "another"
24
25 int main(int argc, char **argv)
26 {
27 BEGIN_TEST();
28
29 /* Test s2n_psk_init */
30 {
31 EXPECT_ERROR_WITH_ERRNO(s2n_psk_init(NULL, S2N_PSK_TYPE_EXTERNAL),
32 S2N_ERR_NULL);
33
34 DEFER_CLEANUP(struct s2n_psk psk, s2n_psk_wipe);
35
36 EXPECT_OK(s2n_psk_init(&psk, S2N_PSK_TYPE_EXTERNAL));
37 EXPECT_EQUAL(psk.type, S2N_PSK_TYPE_EXTERNAL);
38 EXPECT_EQUAL(psk.hmac_alg, S2N_HMAC_SHA256);
39 EXPECT_EQUAL(psk.ticket_age_add, 0);
40 EXPECT_EQUAL(psk.ticket_issue_time, 0);
41 EXPECT_EQUAL(psk.early_data_config.max_early_data_size, 0);
42
43 EXPECT_OK(s2n_psk_init(&psk, S2N_PSK_TYPE_RESUMPTION));
44 EXPECT_EQUAL(psk.type, S2N_PSK_TYPE_RESUMPTION);
45 EXPECT_EQUAL(psk.hmac_alg, S2N_HMAC_SHA256);
46 EXPECT_EQUAL(psk.ticket_age_add, 0);
47 EXPECT_EQUAL(psk.ticket_issue_time, 0);
48 EXPECT_EQUAL(psk.early_data_config.max_early_data_size, 0);
49 };
50
51 /* Test s2n_external_psk_new */
52 {
53 DEFER_CLEANUP(struct s2n_psk inited_psk, s2n_psk_wipe);
54 EXPECT_OK(s2n_psk_init(&inited_psk, S2N_PSK_TYPE_EXTERNAL));
55
56 DEFER_CLEANUP(struct s2n_psk *new_psk = s2n_external_psk_new(), s2n_psk_free);
57 EXPECT_NOT_NULL(new_psk);
58
59 EXPECT_BYTEARRAY_EQUAL(new_psk, &inited_psk, sizeof(struct s2n_psk));
60 };
61
62 /* Test s2n_psk_set_identity */
63 {
64 DEFER_CLEANUP(struct s2n_psk psk, s2n_psk_wipe);
65 EXPECT_OK(s2n_psk_init(&psk, S2N_PSK_TYPE_EXTERNAL));
66
67 uint8_t test_value_1[] = TEST_VALUE_1;
68
69 EXPECT_FAILURE_WITH_ERRNO(s2n_psk_set_identity(NULL, test_value_1, 1),
70 S2N_ERR_NULL);
71 EXPECT_FAILURE_WITH_ERRNO(s2n_psk_set_identity(&psk, NULL, 1),
72 S2N_ERR_NULL);
73 EXPECT_FAILURE_WITH_ERRNO(s2n_psk_set_identity(&psk, test_value_1, 0),
74 S2N_ERR_INVALID_ARGUMENT);
75
76 EXPECT_SUCCESS(s2n_psk_set_identity(&psk, test_value_1, sizeof(test_value_1)));
77 EXPECT_EQUAL(psk.identity.size, sizeof(TEST_VALUE_1));
78 EXPECT_BYTEARRAY_EQUAL(psk.identity.data, TEST_VALUE_1, sizeof(TEST_VALUE_1));
79
80 /* source can be modified without affecting psk */
81 test_value_1[0] = 'A';
82 EXPECT_BYTEARRAY_EQUAL(psk.identity.data, TEST_VALUE_1, sizeof(TEST_VALUE_1));
83
84 /* method can be called again to replace identity */
85 uint8_t test_value_2[] = TEST_VALUE_2;
86 EXPECT_SUCCESS(s2n_psk_set_identity(&psk, test_value_2, sizeof(test_value_2)));
87 EXPECT_EQUAL(psk.identity.size, sizeof(TEST_VALUE_2));
88 EXPECT_BYTEARRAY_EQUAL(psk.identity.data, TEST_VALUE_2, sizeof(TEST_VALUE_2));
89 };
90
91 /* Test s2n_psk_set_secret */
92 {
93 DEFER_CLEANUP(struct s2n_psk psk, s2n_psk_wipe);
94 EXPECT_OK(s2n_psk_init(&psk, S2N_PSK_TYPE_EXTERNAL));
95
96 uint8_t test_value_1[] = TEST_VALUE_1;
97 uint8_t all_zero_value[5] = { 0 };
98
99 EXPECT_FAILURE_WITH_ERRNO(s2n_psk_set_secret(NULL, test_value_1, 1),
100 S2N_ERR_NULL);
101 EXPECT_FAILURE_WITH_ERRNO(s2n_psk_set_secret(&psk, NULL, 1),
102 S2N_ERR_NULL);
103 EXPECT_FAILURE_WITH_ERRNO(s2n_psk_set_secret(&psk, test_value_1, 0),
104 S2N_ERR_INVALID_ARGUMENT);
105 EXPECT_FAILURE_WITH_ERRNO(s2n_psk_set_secret(&psk, all_zero_value, s2n_array_len(all_zero_value)),
106 S2N_ERR_INVALID_ARGUMENT);
107
108 EXPECT_SUCCESS(s2n_psk_set_secret(&psk, test_value_1, sizeof(test_value_1)));
109 EXPECT_EQUAL(psk.secret.size, sizeof(TEST_VALUE_1));
110 EXPECT_BYTEARRAY_EQUAL(psk.secret.data, TEST_VALUE_1, sizeof(TEST_VALUE_1));
111
112 /* source identity can be modified without affecting psk */
113 test_value_1[0] = 'A';
114 EXPECT_BYTEARRAY_EQUAL(psk.secret.data, TEST_VALUE_1, sizeof(TEST_VALUE_1));
115
116 /* method can be called again to replace secret */
117 uint8_t test_value_2[] = TEST_VALUE_2;
118 EXPECT_SUCCESS(s2n_psk_set_secret(&psk, test_value_2, sizeof(test_value_2)));
119 EXPECT_EQUAL(psk.secret.size, sizeof(TEST_VALUE_2));
120 EXPECT_BYTEARRAY_EQUAL(psk.secret.data, TEST_VALUE_2, sizeof(TEST_VALUE_2));
121 };
122
123 /* Test s2n_psk_clone */
124 {
125 const uint8_t test_bad_value[] = "wrong";
126 const uint8_t test_identity[] = "identity";
127 const uint8_t test_secret[] = "secret";
128 const uint8_t test_early_secret[] = "early_secret";
129 const s2n_hmac_algorithm test_hmac = S2N_HMAC_SHA384;
130
131 struct s2n_psk *original = s2n_external_psk_new();
132 EXPECT_NOT_NULL(original);
133 EXPECT_SUCCESS(s2n_psk_set_identity(original, test_identity, sizeof(test_identity)));
134 EXPECT_SUCCESS(s2n_psk_set_secret(original, test_secret, sizeof(test_secret)));
135 EXPECT_SUCCESS(s2n_alloc(&original->early_secret, sizeof(test_early_secret)));
136 EXPECT_MEMCPY_SUCCESS(original->early_secret.data, test_early_secret, original->early_secret.size);
137 original->hmac_alg = test_hmac;
138
139 DEFER_CLEANUP(struct s2n_psk *clone = s2n_external_psk_new(), s2n_psk_free);
140 EXPECT_SUCCESS(s2n_psk_set_identity(clone, test_bad_value, sizeof(test_bad_value)));
141 EXPECT_SUCCESS(s2n_psk_set_secret(clone, test_bad_value, sizeof(test_bad_value)));
142 EXPECT_NOT_NULL(clone);
143
144 /* Check that the blobs weren't shallow copied */
145 EXPECT_NOT_EQUAL(original->identity.data, clone->identity.data);
146 EXPECT_NOT_EQUAL(original->secret.data, clone->secret.data);
147 EXPECT_NOT_EQUAL(original->early_secret.data, clone->early_secret.data);
148
149 EXPECT_OK(s2n_psk_clone(clone, original));
150
151 /* Free the original to ensure they share no memory */
152 EXPECT_SUCCESS(s2n_psk_free(&original));
153
154 /* existing identity is replaced by original's identity */
155 EXPECT_EQUAL(clone->identity.size, sizeof(test_identity));
156 EXPECT_BYTEARRAY_EQUAL(clone->identity.data, test_identity, sizeof(test_identity));
157
158 /* new secret is replaced by original's secret */
159 EXPECT_EQUAL(clone->secret.size, sizeof(test_secret));
160 EXPECT_BYTEARRAY_EQUAL(clone->secret.data, test_secret, sizeof(test_secret));
161
162 /* early secret is allocated for original's early secret */
163 EXPECT_EQUAL(clone->early_secret.size, sizeof(test_early_secret));
164 EXPECT_BYTEARRAY_EQUAL(clone->early_secret.data, test_early_secret, sizeof(test_early_secret));
165
166 /* other values are copied */
167 EXPECT_EQUAL(clone->hmac_alg, test_hmac);
168 };
169
170 /* Test s2n_psk_wipe */
171 {
172 const uint8_t test_value[] = TEST_VALUE_1;
173 struct s2n_psk psk = { 0 };
174 EXPECT_OK(s2n_psk_init(&psk, S2N_PSK_TYPE_EXTERNAL));
175
176 /* No-op if blobs not allocated yet */
177 EXPECT_OK(s2n_psk_wipe(&psk));
178
179 EXPECT_SUCCESS(s2n_psk_set_identity(&psk, test_value, sizeof(test_value)));
180 EXPECT_NOT_EQUAL(psk.identity.size, 0);
181 EXPECT_SUCCESS(s2n_psk_set_secret(&psk, test_value, sizeof(test_value)));
182 EXPECT_NOT_EQUAL(psk.secret.size, 0);
183 EXPECT_SUCCESS(s2n_alloc(&psk.early_secret, sizeof(test_value)));
184 EXPECT_NOT_EQUAL(psk.early_secret.size, 0);
185
186 /* Frees all blobs */
187 EXPECT_OK(s2n_psk_wipe(&psk));
188 EXPECT_EQUAL(psk.identity.data, NULL);
189 EXPECT_EQUAL(psk.identity.size, 0);
190 EXPECT_EQUAL(psk.secret.data, NULL);
191 EXPECT_EQUAL(psk.secret.size, 0);
192 EXPECT_EQUAL(psk.early_secret.data, NULL);
193 EXPECT_EQUAL(psk.early_secret.size, 0);
194
195 /* No-op if already freed */
196 EXPECT_OK(s2n_psk_wipe(&psk));
197 };
198
199 /* Test s2n_psk_free */
200 {
201 EXPECT_SUCCESS(s2n_psk_free(NULL));
202
203 struct s2n_psk *new_psk = s2n_external_psk_new();
204 EXPECT_NOT_NULL(new_psk);
205 EXPECT_SUCCESS(s2n_psk_free(&new_psk));
206 EXPECT_NULL(new_psk);
207 };
208
209 /* Test s2n_psk_parameters_init */
210 {
211 DEFER_CLEANUP(struct s2n_psk_parameters params, s2n_psk_parameters_wipe);
212
213 EXPECT_ERROR_WITH_ERRNO(s2n_psk_parameters_init(NULL), S2N_ERR_NULL);
214
215 EXPECT_OK(s2n_psk_parameters_init(&params));
216
217 /* Verify params are initialized.
218 * Only element_size should be set.
219 *
220 * NOTE: check individual components to accommodate 32-bit system.
221 */
222 EXPECT_EQUAL(params.type, S2N_PSK_TYPE_RESUMPTION);
223 EXPECT_EQUAL(params.psk_list.mem.data, NULL);
224 EXPECT_EQUAL(params.psk_list.mem.size, 0);
225 EXPECT_EQUAL(params.psk_list.mem.allocated, 0);
226 EXPECT_EQUAL(params.psk_list.mem.growable, 0);
227 EXPECT_EQUAL(params.psk_list.len, 0);
228 EXPECT_EQUAL(params.psk_list.element_size, sizeof(struct s2n_psk));
229 EXPECT_EQUAL(params.binder_list_size, 0);
230 EXPECT_EQUAL(params.chosen_psk_wire_index, 0);
231 EXPECT_NULL(params.chosen_psk);
232 EXPECT_EQUAL(params.psk_ke_mode, S2N_PSK_KE_UNKNOWN);
233 };
234
235 /* Test s2n_psk_parameters_wipe */
236 {
237 uint8_t test_value[] = TEST_VALUE_1;
238
239 DEFER_CLEANUP(struct s2n_psk_parameters params = { 0 }, s2n_psk_parameters_wipe);
240 EXPECT_OK(s2n_psk_parameters_init(&params));
241 params.binder_list_size = 1;
242 params.chosen_psk_wire_index = 1;
243
244 struct s2n_psk *chosen_psk = NULL;
245 EXPECT_OK(s2n_array_pushback(&params.psk_list, (void **) &chosen_psk));
246 EXPECT_OK(s2n_psk_init(chosen_psk, S2N_PSK_TYPE_EXTERNAL));
247 EXPECT_SUCCESS(s2n_psk_set_identity(chosen_psk, test_value, sizeof(test_value)));
248 params.chosen_psk = chosen_psk;
249
250 struct s2n_psk *other_psk = NULL;
251 EXPECT_OK(s2n_array_pushback(&params.psk_list, (void **) &other_psk));
252 EXPECT_OK(s2n_psk_init(other_psk, S2N_PSK_TYPE_EXTERNAL));
253 EXPECT_SUCCESS(s2n_psk_set_identity(other_psk, test_value, sizeof(test_value)));
254
255 EXPECT_ERROR_WITH_ERRNO(s2n_psk_parameters_wipe(NULL), S2N_ERR_NULL);
256 EXPECT_OK(s2n_psk_parameters_wipe(&params));
257
258 /* Verify params are wiped.
259 * The params should be back to their initial state. */
260 struct s2n_psk_parameters expected_params = { 0 };
261 EXPECT_OK(s2n_psk_parameters_init(&expected_params));
262 EXPECT_BYTEARRAY_EQUAL(&expected_params, &params, sizeof(struct s2n_psk_parameters));
263 };
264
265 /* Test s2n_psk_parameters_wipe_secrets */
266 {
267 /* Safety Check */
268 EXPECT_ERROR_WITH_ERRNO(s2n_psk_parameters_wipe_secrets(NULL), S2N_ERR_NULL);
269
270 uint8_t test_identity_data[] = "test identity data";
271 uint8_t test_secret_data[] = "test secret data";
272 struct s2n_psk_parameters params = { 0 };
273 EXPECT_OK(s2n_psk_parameters_init(&params));
274 params.binder_list_size = 1;
275 params.chosen_psk_wire_index = 1;
276
277 struct s2n_psk *chosen_psk = NULL;
278 EXPECT_OK(s2n_array_pushback(&params.psk_list, (void **) &chosen_psk));
279 EXPECT_OK(s2n_psk_init(chosen_psk, S2N_PSK_TYPE_EXTERNAL));
280 EXPECT_SUCCESS(s2n_psk_set_identity(chosen_psk, test_identity_data, sizeof(test_identity_data)));
281 EXPECT_SUCCESS(s2n_psk_set_secret(chosen_psk, test_secret_data, sizeof(test_secret_data)));
282 params.chosen_psk = chosen_psk;
283
284 struct s2n_psk *other_psk = NULL;
285 EXPECT_OK(s2n_array_pushback(&params.psk_list, (void **) &other_psk));
286 EXPECT_OK(s2n_psk_init(other_psk, S2N_PSK_TYPE_EXTERNAL));
287 EXPECT_SUCCESS(s2n_psk_set_identity(other_psk, test_identity_data, sizeof(test_identity_data)));
288 EXPECT_SUCCESS(s2n_psk_set_secret(other_psk, test_secret_data, sizeof(test_secret_data)));
289
290 EXPECT_OK(s2n_psk_parameters_wipe_secrets(&params));
291
292 /* Verify secrets are wiped */
293 for (size_t i = 0; i < params.psk_list.len; i++) {
294 struct s2n_psk *psk = NULL;
295 EXPECT_OK(s2n_array_get(&params.psk_list, i, (void **) &psk));
296 EXPECT_NOT_NULL(psk->identity.data);
297 EXPECT_NULL(psk->secret.data);
298 EXPECT_EQUAL(psk->secret.size, 0);
299 EXPECT_NULL(psk->early_secret.data);
300 EXPECT_EQUAL(psk->early_secret.size, 0);
301 }
302
303 EXPECT_OK(s2n_psk_parameters_wipe(&params));
304 };
305
306 /* Test s2n_connection psk_parameters lifecycle.
307 * This test mostly exists to check for memory leaks. */
308 {
309 uint8_t test_value[] = TEST_VALUE_1;
310 struct s2n_psk_parameters empty_psk_params = { 0 };
311 EXPECT_OK(s2n_psk_parameters_init(&empty_psk_params));
312
313 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
314 conn->psk_params.binder_list_size = 1;
315 conn->psk_params.chosen_psk_wire_index = 1;
316
317 struct s2n_psk *first_psk = NULL;
318 EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &first_psk));
319 EXPECT_OK(s2n_psk_init(first_psk, S2N_PSK_TYPE_EXTERNAL));
320 EXPECT_SUCCESS(s2n_psk_set_identity(first_psk, test_value, sizeof(test_value)));
321 conn->psk_params.chosen_psk = first_psk;
322
323 EXPECT_SUCCESS(s2n_connection_wipe(conn));
324 EXPECT_BYTEARRAY_EQUAL(&empty_psk_params, &conn->psk_params, sizeof(struct s2n_psk_parameters));
325
326 struct s2n_psk *second_psk = NULL;
327 EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &second_psk));
328 EXPECT_OK(s2n_psk_init(second_psk, S2N_PSK_TYPE_EXTERNAL));
329 EXPECT_SUCCESS(s2n_psk_set_identity(second_psk, test_value, sizeof(test_value)));
330 conn->psk_params.chosen_psk = second_psk;
331
332 EXPECT_SUCCESS(s2n_connection_wipe(conn));
333 EXPECT_BYTEARRAY_EQUAL(&empty_psk_params, &conn->psk_params, sizeof(struct s2n_psk_parameters));
334
335 struct s2n_psk *third_psk = NULL;
336 EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &third_psk));
337 EXPECT_OK(s2n_psk_init(third_psk, S2N_PSK_TYPE_EXTERNAL));
338 EXPECT_SUCCESS(s2n_psk_set_identity(third_psk, test_value, sizeof(test_value)));
339 conn->psk_params.chosen_psk = third_psk;
340
341 EXPECT_SUCCESS(s2n_connection_free(conn));
342 };
343
344 /* Test s2n_psk_write_binder_list */
345 {
346 uint8_t test_value[] = TEST_VALUE_1;
347
348 struct s2n_blob client_hello_prefix = { 0 };
349 EXPECT_SUCCESS(s2n_blob_init(&client_hello_prefix, test_value, sizeof(test_value)));
350
351 /* Write two binders.
352 * There are no available test vectors for multiple PSKs, but we should at least
353 * verify that we write something relatively sane for this use case. */
354 {
355 struct s2n_connection *conn = NULL;
356 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
357
358 struct s2n_stuffer out = { 0 };
359 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&out, 0));
360
361 struct s2n_psk_parameters *params = &conn->psk_params;
362
363 struct s2n_psk *first_psk = NULL;
364 EXPECT_OK(s2n_array_pushback(&params->psk_list, (void **) &first_psk));
365 EXPECT_OK(s2n_psk_init(first_psk, S2N_PSK_TYPE_EXTERNAL));
366 EXPECT_SUCCESS(s2n_psk_set_secret(first_psk, test_value, sizeof(test_value)));
367
368 struct s2n_psk *second_psk = NULL;
369 EXPECT_OK(s2n_array_pushback(&params->psk_list, (void **) &second_psk));
370 EXPECT_OK(s2n_psk_init(second_psk, S2N_PSK_TYPE_EXTERNAL));
371 EXPECT_SUCCESS(s2n_psk_set_secret(second_psk, test_value, sizeof(test_value)));
372
373 EXPECT_OK(s2n_psk_write_binder_list(conn, &client_hello_prefix, &out));
374
375 uint16_t binder_list_size = 0;
376 EXPECT_SUCCESS(s2n_stuffer_read_uint16(&out, &binder_list_size));
377 EXPECT_EQUAL(binder_list_size, s2n_stuffer_data_available(&out));
378
379 /* After reading both binders, the buffer should be empty. */
380 for (int i = 0; i < 2; i++) {
381 uint8_t binder_size = 0;
382 EXPECT_SUCCESS(s2n_stuffer_read_uint8(&out, &binder_size));
383 EXPECT_SUCCESS(s2n_stuffer_skip_read(&out, binder_size));
384 }
385 EXPECT_EQUAL(s2n_stuffer_data_available(&out), 0);
386
387 EXPECT_SUCCESS(s2n_connection_free(conn));
388 EXPECT_SUCCESS(s2n_stuffer_free(&out));
389 };
390
391 /* On a retry, do not write binders for PSKs that do not match the cipher suite.
392 *
393 *= https://www.rfc-editor.org/rfc/rfc8446#section-4.1.4
394 *= type=test
395 *# In addition, in its updated ClientHello, the client SHOULD NOT offer
396 *# any pre-shared keys associated with a hash other than that of the
397 *# selected cipher suite. This allows the client to avoid having to
398 *# compute partial hash transcripts for multiple hashes in the second
399 *# ClientHello.
400 */
401 {
402 struct s2n_connection *conn = NULL;
403 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
404 conn->handshake.handshake_type = HELLO_RETRY_REQUEST;
405 conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
406
407 struct s2n_stuffer out = { 0 };
408 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&out, 0));
409
410 struct s2n_psk_parameters *params = &conn->psk_params;
411
412 struct s2n_psk *other_psk = NULL;
413 EXPECT_OK(s2n_array_pushback(&params->psk_list, (void **) &other_psk));
414 EXPECT_OK(s2n_psk_init(other_psk, S2N_PSK_TYPE_EXTERNAL));
415 EXPECT_SUCCESS(s2n_psk_set_secret(other_psk, test_value, sizeof(test_value)));
416 other_psk->hmac_alg = conn->secure->cipher_suite->prf_alg - 1;
417
418 struct s2n_psk *matching_psk = NULL;
419 EXPECT_OK(s2n_array_pushback(&params->psk_list, (void **) &matching_psk));
420 EXPECT_OK(s2n_psk_init(matching_psk, S2N_PSK_TYPE_EXTERNAL));
421 EXPECT_SUCCESS(s2n_psk_set_secret(matching_psk, test_value, sizeof(test_value)));
422 matching_psk->hmac_alg = conn->secure->cipher_suite->prf_alg;
423
424 EXPECT_OK(s2n_psk_write_binder_list(conn, &client_hello_prefix, &out));
425
426 uint16_t binder_list_size = 0;
427 EXPECT_SUCCESS(s2n_stuffer_read_uint16(&out, &binder_list_size));
428 EXPECT_EQUAL(binder_list_size, s2n_stuffer_data_available(&out));
429
430 /* There should only be one binder in the list
431 * (the other PSK was ignored because it didn't match the cipher suite)
432 * so that one binder should fill the rest of the stuffer. */
433 uint8_t binder_size = 0;
434 EXPECT_SUCCESS(s2n_stuffer_read_uint8(&out, &binder_size));
435 EXPECT_EQUAL(binder_size, s2n_stuffer_data_available(&out));
436
437 EXPECT_SUCCESS(s2n_connection_free(conn));
438 EXPECT_SUCCESS(s2n_stuffer_free(&out));
439 }
440 };
441
442 /* Test binder calculations with known values */
443 {
444 /* Test Vectors from https://tools.ietf.org/html/rfc8448#section-4 */
445 S2N_BLOB_FROM_HEX(identity,
446 "2c035d829359ee5ff7af4ec900000000262a6494dc486d2c8a34cb33fa90bf1b00"
447 "70ad3c498883c9367c09a2be785abc55cd226097a3a982117283f82a03a143efd3"
448 "ff5dd36d64e861be7fd61d2827db279cce145077d454a3664d4e6da4d29ee03725"
449 "a6a4dafcd0fc67d2aea70529513e3da2677fa5906c5b3f7d8f92f228bda40dda72"
450 "1470f9fbf297b5aea617646fac5c03272e970727c621a79141ef5f7de6505e5bfb"
451 "c388e93343694093934ae4d357fad6aacb");
452 S2N_BLOB_FROM_HEX(client_hello_prefix,
453 "010001fc03031bc3ceb6bbe39cff938355b5a50adb6db21b7a6af649d7b4bc419d"
454 "7876487d95000006130113031302010001cd0000000b0009000006736572766572"
455 "ff01000100000a00140012001d0017001800190100010101020103010400330026"
456 "0024001d0020e4ffb68ac05f8d96c99da26698346c6be16482badddafe051a66b4"
457 "f18d668f0b002a0000002b0003020304000d0020001e0403050306030203080408"
458 "05080604010501060102010402050206020202002d00020101001c000240010015"
459 "005700000000000000000000000000000000000000000000000000000000000000"
460 "000000000000000000000000000000000000000000000000000000000000000000"
461 "0000000000000000000000000000000000000000000000002900dd00b800b22c03"
462 "5d829359ee5ff7af4ec900000000262a6494dc486d2c8a34cb33fa90bf1b0070ad"
463 "3c498883c9367c09a2be785abc55cd226097a3a982117283f82a03a143efd3ff5d"
464 "d36d64e861be7fd61d2827db279cce145077d454a3664d4e6da4d29ee03725a6a4"
465 "dafcd0fc67d2aea70529513e3da2677fa5906c5b3f7d8f92f228bda40dda721470"
466 "f9fbf297b5aea617646fac5c03272e970727c621a79141ef5f7de6505e5bfbc388"
467 "e93343694093934ae4d357fad6aacb");
468 S2N_BLOB_FROM_HEX(full_client_hello,
469 "010001fc03031bc3ceb6bbe39cff938355b5a50adb6db21b7a6af649d7b4bc419d"
470 "7876487d95000006130113031302010001cd0000000b0009000006736572766572"
471 "ff01000100000a00140012001d0017001800190100010101020103010400330026"
472 "0024001d0020e4ffb68ac05f8d96c99da26698346c6be16482badddafe051a66b4"
473 "f18d668f0b002a0000002b0003020304000d0020001e0403050306030203080408"
474 "05080604010501060102010402050206020202002d00020101001c000240010015"
475 "005700000000000000000000000000000000000000000000000000000000000000"
476 "000000000000000000000000000000000000000000000000000000000000000000"
477 "0000000000000000000000000000000000000000000000002900dd00b800b22c03"
478 "5d829359ee5ff7af4ec900000000262a6494dc486d2c8a34cb33fa90bf1b0070ad"
479 "3c498883c9367c09a2be785abc55cd226097a3a982117283f82a03a143efd3ff5d"
480 "d36d64e861be7fd61d2827db279cce145077d454a3664d4e6da4d29ee03725a6a4"
481 "dafcd0fc67d2aea70529513e3da2677fa5906c5b3f7d8f92f228bda40dda721470"
482 "f9fbf297b5aea617646fac5c03272e970727c621a79141ef5f7de6505e5bfbc388"
483 "e93343694093934ae4d357fad6aacb0021203add4fb2d8fdf822a0ca3cf7678ef5"
484 "e88dae990141c5924d57bb6fa31b9e5f9d");
485 S2N_BLOB_FROM_HEX(resumption_secret,
486 "4ecd0eb6ec3b4d87f5d6028f922ca4c5851a277fd41311c9e62d2c9492e1c4f3");
487 S2N_BLOB_FROM_HEX(binder_hash,
488 "63224b2e4573f2d3454ca84b9d009a04f6be9e05711a8396473aefa01e924a14");
489 S2N_BLOB_FROM_HEX(early_secret,
490 "9b2188e9b2fc6d64d71dc329900e20bb41915000f678aa839cbb797cb7d8332c");
491 S2N_BLOB_FROM_HEX(finished_binder,
492 "3add4fb2d8fdf822a0ca3cf7678ef5e88dae990141c5924d57bb6fa31b9e5f9d");
493
494 /* Test s2n_psk_calculate_binder_hash with known values */
495 {
496 struct s2n_connection *conn = NULL;
497 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
498
499 struct s2n_blob hash_value = { 0 };
500 uint8_t hash_value_data[SHA256_DIGEST_LENGTH];
501 EXPECT_SUCCESS(s2n_blob_init(&hash_value, hash_value_data, sizeof(hash_value_data)));
502
503 EXPECT_SUCCESS(s2n_psk_calculate_binder_hash(conn, S2N_HMAC_SHA256, &client_hello_prefix, &hash_value));
504 S2N_BLOB_EXPECT_EQUAL(hash_value, binder_hash);
505
506 EXPECT_SUCCESS(s2n_connection_free(conn));
507 };
508
509 /* Test s2n_psk_calculate_binder with known values */
510 {
511 DEFER_CLEANUP(struct s2n_psk test_psk, s2n_psk_wipe);
512 EXPECT_OK(s2n_psk_init(&test_psk, S2N_PSK_TYPE_RESUMPTION));
513 EXPECT_SUCCESS(s2n_psk_set_secret(&test_psk, resumption_secret.data, resumption_secret.size));
514
515 struct s2n_blob binder_value = { 0 };
516 uint8_t binder_value_data[SHA256_DIGEST_LENGTH];
517 EXPECT_SUCCESS(s2n_blob_init(&binder_value, binder_value_data, sizeof(binder_value_data)));
518
519 EXPECT_SUCCESS(s2n_psk_calculate_binder(&test_psk, &binder_hash, &binder_value));
520 S2N_BLOB_EXPECT_EQUAL(test_psk.early_secret, early_secret);
521 S2N_BLOB_EXPECT_EQUAL(binder_value, finished_binder);
522 };
523
524 /* Test s2n_psk_verify_binder with known values */
525 {
526 struct s2n_connection *conn = NULL;
527 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
528
529 DEFER_CLEANUP(struct s2n_psk test_psk, s2n_psk_wipe);
530 EXPECT_OK(s2n_psk_init(&test_psk, S2N_PSK_TYPE_RESUMPTION));
531 EXPECT_SUCCESS(s2n_psk_set_secret(&test_psk, resumption_secret.data, resumption_secret.size));
532
533 struct s2n_blob binder_value = { 0 };
534 uint8_t binder_value_data[SHA256_DIGEST_LENGTH];
535 EXPECT_SUCCESS(s2n_blob_init(&binder_value, binder_value_data, sizeof(binder_value_data)));
536
537 EXPECT_SUCCESS(s2n_psk_verify_binder(conn, &test_psk, &client_hello_prefix, &finished_binder));
538 S2N_BLOB_EXPECT_EQUAL(test_psk.early_secret, early_secret);
539
540 EXPECT_SUCCESS(s2n_connection_free(conn));
541 };
542
543 /* Test s2n_psk_verify_binder with incorrect binder */
544 {
545 struct s2n_connection *conn = NULL;
546 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
547
548 DEFER_CLEANUP(struct s2n_psk test_psk, s2n_psk_wipe);
549 EXPECT_OK(s2n_psk_init(&test_psk, S2N_PSK_TYPE_RESUMPTION));
550 EXPECT_SUCCESS(s2n_psk_set_secret(&test_psk, resumption_secret.data, resumption_secret.size));
551
552 struct s2n_blob *incorrect_binder_value = &resumption_secret;
553
554 struct s2n_blob binder_value = { 0 };
555 uint8_t binder_value_data[SHA256_DIGEST_LENGTH];
556 EXPECT_SUCCESS(s2n_blob_init(&binder_value, binder_value_data, sizeof(binder_value_data)));
557
558 EXPECT_FAILURE(s2n_psk_verify_binder(conn, &test_psk, &client_hello_prefix, incorrect_binder_value));
559 S2N_BLOB_EXPECT_EQUAL(test_psk.early_secret, early_secret);
560
561 EXPECT_SUCCESS(s2n_connection_free(conn));
562 };
563
564 /* Test s2n_psk_write_binder with known values */
565 {
566 struct s2n_connection *conn = NULL;
567 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
568
569 DEFER_CLEANUP(struct s2n_psk psk = { 0 }, s2n_psk_wipe);
570 EXPECT_OK(s2n_psk_init(&psk, S2N_PSK_TYPE_RESUMPTION));
571 EXPECT_SUCCESS(s2n_psk_set_secret(&psk, resumption_secret.data, resumption_secret.size));
572
573 struct s2n_stuffer out = { 0 };
574 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&out, 0));
575
576 EXPECT_OK(s2n_psk_write_binder(conn, &psk, &binder_hash, &out));
577
578 uint8_t binder_size = 0;
579 EXPECT_SUCCESS(s2n_stuffer_read_uint8(&out, &binder_size));
580 EXPECT_EQUAL(binder_size, s2n_stuffer_data_available(&out));
581 EXPECT_EQUAL(binder_size, finished_binder.size);
582
583 uint8_t *binder_data = NULL;
584 EXPECT_NOT_NULL(binder_data = s2n_stuffer_raw_read(&out, binder_size));
585 EXPECT_BYTEARRAY_EQUAL(binder_data, finished_binder.data, binder_size);
586
587 EXPECT_SUCCESS(s2n_connection_free(conn));
588 EXPECT_SUCCESS(s2n_stuffer_free(&out));
589 };
590
591 /* Test s2n_psk_write_binder_list with known values */
592 {
593 struct s2n_connection *conn = NULL;
594 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
595
596 struct s2n_psk *psk = NULL;
597 EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &psk));
598 EXPECT_OK(s2n_psk_init(psk, S2N_PSK_TYPE_RESUMPTION));
599 EXPECT_SUCCESS(s2n_psk_set_identity(psk, identity.data, identity.size));
600 EXPECT_SUCCESS(s2n_psk_set_secret(psk, resumption_secret.data, resumption_secret.size));
601
602 struct s2n_stuffer out = { 0 };
603 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&out, 0));
604
605 EXPECT_OK(s2n_psk_write_binder_list(conn, &client_hello_prefix, &out));
606
607 uint16_t binder_list_size = 0;
608 EXPECT_SUCCESS(s2n_stuffer_read_uint16(&out, &binder_list_size));
609 EXPECT_EQUAL(binder_list_size, s2n_stuffer_data_available(&out));
610
611 uint8_t binder_size = 0;
612 EXPECT_SUCCESS(s2n_stuffer_read_uint8(&out, &binder_size));
613 EXPECT_EQUAL(binder_size, s2n_stuffer_data_available(&out));
614 EXPECT_EQUAL(binder_size, finished_binder.size);
615
616 uint8_t *binder_data = NULL;
617 EXPECT_NOT_NULL(binder_data = s2n_stuffer_raw_read(&out, binder_size));
618 EXPECT_BYTEARRAY_EQUAL(binder_data, finished_binder.data, binder_size);
619
620 EXPECT_SUCCESS(s2n_connection_free(conn));
621 EXPECT_SUCCESS(s2n_stuffer_free(&out));
622 };
623
624 /* Test s2n_psk_write_binder_list with multiple PSKs */
625 {
626 const uint8_t psk_count = 5;
627
628 struct s2n_connection *conn = NULL;
629 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
630
631 for (uint8_t i = 0; i < psk_count; i++) {
632 struct s2n_psk *psk = NULL;
633 EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &psk));
634 EXPECT_OK(s2n_psk_init(psk, S2N_PSK_TYPE_RESUMPTION));
635 EXPECT_SUCCESS(s2n_psk_set_identity(psk, identity.data, identity.size));
636 EXPECT_SUCCESS(s2n_psk_set_secret(psk, resumption_secret.data, resumption_secret.size));
637 }
638
639 struct s2n_stuffer out = { 0 };
640 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&out, 0));
641
642 EXPECT_OK(s2n_psk_write_binder_list(conn, &client_hello_prefix, &out));
643
644 uint16_t binder_list_size = 0;
645 EXPECT_SUCCESS(s2n_stuffer_read_uint16(&out, &binder_list_size));
646 EXPECT_EQUAL(binder_list_size, s2n_stuffer_data_available(&out));
647
648 for (uint8_t i = 0; i < psk_count; i++) {
649 uint8_t binder_size = 0;
650 EXPECT_SUCCESS(s2n_stuffer_read_uint8(&out, &binder_size));
651 EXPECT_EQUAL(binder_size, finished_binder.size);
652
653 uint8_t *binder_data = NULL;
654 EXPECT_NOT_NULL(binder_data = s2n_stuffer_raw_read(&out, binder_size));
655 EXPECT_BYTEARRAY_EQUAL(binder_data, finished_binder.data, binder_size);
656 }
657
658 EXPECT_EQUAL(s2n_stuffer_data_available(&out), 0);
659
660 EXPECT_SUCCESS(s2n_connection_free(conn));
661 EXPECT_SUCCESS(s2n_stuffer_free(&out));
662 };
663
664 /* Test s2n_psk_write_binder_list with multiple hash algs */
665 {
666 struct s2n_connection *conn = NULL;
667 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
668
669 for (s2n_hmac_algorithm hmac_alg = S2N_HMAC_SHA256; hmac_alg <= S2N_HMAC_SHA384; hmac_alg++) {
670 struct s2n_psk *psk = NULL;
671 EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &psk));
672 EXPECT_OK(s2n_psk_init(psk, S2N_PSK_TYPE_RESUMPTION));
673 EXPECT_SUCCESS(s2n_psk_set_identity(psk, identity.data, identity.size));
674 EXPECT_SUCCESS(s2n_psk_set_secret(psk, resumption_secret.data, resumption_secret.size));
675 psk->hmac_alg = hmac_alg;
676 }
677
678 struct s2n_stuffer out = { 0 };
679 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&out, 0));
680
681 EXPECT_OK(s2n_psk_write_binder_list(conn, &client_hello_prefix, &out));
682
683 uint16_t binder_list_size = 0;
684 EXPECT_SUCCESS(s2n_stuffer_read_uint16(&out, &binder_list_size));
685 EXPECT_EQUAL(binder_list_size, s2n_stuffer_data_available(&out));
686
687 for (s2n_hmac_algorithm hmac_alg = S2N_HMAC_SHA256; hmac_alg <= S2N_HMAC_SHA384; hmac_alg++) {
688 uint8_t hash_size = 0;
689 POSIX_GUARD(s2n_hmac_digest_size(hmac_alg, &hash_size));
690
691 uint8_t binder_size = 0;
692 EXPECT_SUCCESS(s2n_stuffer_read_uint8(&out, &binder_size));
693 EXPECT_EQUAL(binder_size, hash_size);
694
695 uint8_t *binder_data = NULL;
696 EXPECT_NOT_NULL(binder_data = s2n_stuffer_raw_read(&out, binder_size));
697 /* We can only actually verify the result for SHA256; we don't have known
698 * values for any other hash. */
699 if (hmac_alg == S2N_HMAC_SHA256) {
700 EXPECT_BYTEARRAY_EQUAL(binder_data, finished_binder.data, binder_size);
701 }
702 }
703
704 EXPECT_EQUAL(s2n_stuffer_data_available(&out), 0);
705
706 EXPECT_SUCCESS(s2n_connection_free(conn));
707 EXPECT_SUCCESS(s2n_stuffer_free(&out));
708 };
709
710 /* Test s2n_finish_psk_extension with known values */
711 {
712 struct s2n_connection *conn = NULL;
713 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
714
715 EXPECT_SUCCESS(s2n_stuffer_write(&conn->handshake.io, &client_hello_prefix));
716 conn->psk_params.binder_list_size = full_client_hello.size - client_hello_prefix.size;
717 EXPECT_SUCCESS(s2n_stuffer_skip_write(&conn->handshake.io, conn->psk_params.binder_list_size));
718
719 struct s2n_psk *psk = NULL;
720 EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &psk));
721 EXPECT_OK(s2n_psk_init(psk, S2N_PSK_TYPE_RESUMPTION));
722 EXPECT_SUCCESS(s2n_psk_set_identity(psk, identity.data, identity.size));
723 EXPECT_SUCCESS(s2n_psk_set_secret(psk, resumption_secret.data, resumption_secret.size));
724
725 EXPECT_OK(s2n_finish_psk_extension(conn));
726 EXPECT_EQUAL(s2n_stuffer_data_available(&conn->handshake.io), full_client_hello.size);
727 EXPECT_BYTEARRAY_EQUAL(conn->handshake.io.blob.data, full_client_hello.data, full_client_hello.size);
728
729 EXPECT_SUCCESS(s2n_connection_free(conn));
730 };
731 };
732
733 /* Test s2n_connection_append_psk */
734 {
735 uint8_t identity_0[] = "identity";
736 uint8_t secret_0[] = "secret";
737
738 uint8_t identity_1[] = "identity 1";
739
740 uint8_t huge_identity[UINT16_MAX] = { 0 };
741
742 DEFER_CLEANUP(struct s2n_psk *input_psk = s2n_external_psk_new(), s2n_psk_free);
743 EXPECT_SUCCESS(s2n_psk_set_identity(input_psk, identity_0, sizeof(identity_0)));
744 EXPECT_SUCCESS(s2n_psk_set_secret(input_psk, secret_0, sizeof(secret_0)));
745 EXPECT_SUCCESS(s2n_psk_set_hmac(input_psk, S2N_PSK_HMAC_SHA384));
746
747 /* Safety checks */
748 {
749 struct s2n_connection *conn = NULL;
750 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
751
752 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_append_psk(NULL, input_psk), S2N_ERR_NULL);
753 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_append_psk(conn, NULL), S2N_ERR_NULL);
754
755 EXPECT_SUCCESS(s2n_connection_free(conn));
756 };
757
758 /* Append valid PSK to empty list */
759 {
760 struct s2n_connection *conn = NULL;
761 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
762
763 EXPECT_SUCCESS(s2n_connection_append_psk(conn, input_psk));
764
765 struct s2n_psk *actual_psk = NULL;
766 EXPECT_OK(s2n_array_get(&conn->psk_params.psk_list, 0, (void **) &actual_psk));
767 EXPECT_NOT_NULL(actual_psk);
768
769 EXPECT_EQUAL(actual_psk->type, S2N_PSK_TYPE_EXTERNAL);
770 EXPECT_EQUAL(actual_psk->identity.size, input_psk->identity.size);
771 EXPECT_BYTEARRAY_EQUAL(actual_psk->identity.data, input_psk->identity.data, input_psk->identity.size);
772 EXPECT_EQUAL(actual_psk->secret.size, input_psk->secret.size);
773 EXPECT_BYTEARRAY_EQUAL(actual_psk->secret.data, input_psk->secret.data, input_psk->secret.size);
774 EXPECT_EQUAL(actual_psk->hmac_alg, S2N_HMAC_SHA384);
775 EXPECT_EQUAL(actual_psk->ticket_age_add, 0);
776 EXPECT_EQUAL(actual_psk->ticket_issue_time, 0);
777
778 EXPECT_SUCCESS(s2n_connection_free(conn));
779 };
780
781 /* Original PSK can be safely freed after being added to a connection */
782 {
783 struct s2n_connection *conn = NULL;
784 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
785
786 struct s2n_psk *original_psk = s2n_external_psk_new();
787 EXPECT_SUCCESS(s2n_psk_set_identity(original_psk, identity_0, sizeof(identity_0)));
788 EXPECT_SUCCESS(s2n_psk_set_secret(original_psk, secret_0, sizeof(secret_0)));
789 EXPECT_SUCCESS(s2n_psk_set_hmac(original_psk, S2N_PSK_HMAC_SHA384));
790
791 EXPECT_SUCCESS(s2n_connection_append_psk(conn, original_psk));
792
793 /* Original PSK freed */
794 EXPECT_SUCCESS(s2n_psk_free(&original_psk));
795 EXPECT_NULL(original_psk);
796
797 /* PSK on connection not freed */
798 struct s2n_psk *expected_psk = NULL;
799 EXPECT_OK(s2n_array_get(&conn->psk_params.psk_list, 0, (void **) &expected_psk));
800 EXPECT_NOT_NULL(expected_psk);
801
802 /* PSK on connection's buffers not freed */
803 EXPECT_EQUAL(expected_psk->identity.size, input_psk->identity.size);
804 EXPECT_BYTEARRAY_EQUAL(expected_psk->identity.data, input_psk->identity.data, input_psk->identity.size);
805
806 EXPECT_SUCCESS(s2n_connection_free(conn));
807 };
808
809 /* Invalid PSK not added to connection */
810 {
811 /* PSK is invalid because it has no identity */
812 {
813 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
814 EXPECT_NOT_NULL(conn);
815
816 DEFER_CLEANUP(struct s2n_psk *invalid_psk = s2n_external_psk_new(), s2n_psk_free);
817 EXPECT_SUCCESS(s2n_psk_set_secret(invalid_psk, secret_0, sizeof(secret_0)));
818
819 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_append_psk(conn, invalid_psk),
820 S2N_ERR_INVALID_ARGUMENT);
821 EXPECT_EQUAL(conn->psk_params.psk_list.len, 0);
822
823 /* Successful if identity added to PSK, making it valid */
824 EXPECT_SUCCESS(s2n_psk_set_identity(invalid_psk, identity_0, sizeof(identity_0)));
825 EXPECT_SUCCESS(s2n_connection_append_psk(conn, invalid_psk));
826 };
827
828 /* PSK is invalid because it has no secret */
829 {
830 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
831 EXPECT_NOT_NULL(conn);
832
833 DEFER_CLEANUP(struct s2n_psk *invalid_psk = s2n_external_psk_new(), s2n_psk_free);
834 EXPECT_SUCCESS(s2n_psk_set_identity(invalid_psk, identity_0, sizeof(identity_0)));
835
836 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_append_psk(conn, invalid_psk),
837 S2N_ERR_INVALID_ARGUMENT);
838 EXPECT_EQUAL(conn->psk_params.psk_list.len, 0);
839
840 /* Successful if secret added to PSK, making it valid */
841 EXPECT_SUCCESS(s2n_psk_set_secret(invalid_psk, secret_0, sizeof(secret_0)));
842 EXPECT_SUCCESS(s2n_connection_append_psk(conn, invalid_psk));
843 };
844 };
845
846 /* Huge PSK not added to client connection */
847 {
848 struct s2n_connection *conn = NULL;
849 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
850
851 DEFER_CLEANUP(struct s2n_psk *invalid_psk = s2n_external_psk_new(), s2n_psk_free);
852 EXPECT_SUCCESS(s2n_psk_set_secret(invalid_psk, secret_0, sizeof(secret_0)));
853
854 /* PSK is invalid because it will not fit in the PSK extension */
855 uint16_t max_identity_size = UINT16_MAX
856 - S2N_EXTENSION_HEADER_LENGTH
857 - sizeof(uint16_t) /* identity list size */
858 - sizeof(uint16_t) /* identity size */
859 - sizeof(uint32_t) /* obfuscated age add */
860 - sizeof(uint16_t) /* binder list size */
861 - sizeof(uint8_t) /* binder size */
862 - SHA256_DIGEST_LENGTH; /* binder */
863 EXPECT_SUCCESS(s2n_psk_set_identity(invalid_psk, huge_identity, max_identity_size + 1));
864
865 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_append_psk(conn, invalid_psk),
866 S2N_ERR_OFFERED_PSKS_TOO_LONG);
867 EXPECT_EQUAL(conn->psk_params.psk_list.len, 0);
868
869 /* Successful if smaller identity used */
870 EXPECT_SUCCESS(s2n_psk_set_identity(invalid_psk, huge_identity, max_identity_size));
871 EXPECT_SUCCESS(s2n_connection_append_psk(conn, invalid_psk));
872
873 EXPECT_SUCCESS(s2n_connection_free(conn));
874 };
875
876 /* Huge PSK added to server connection */
877 {
878 struct s2n_connection *conn = NULL;
879 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
880
881 DEFER_CLEANUP(struct s2n_psk *invalid_psk = s2n_external_psk_new(), s2n_psk_free);
882 EXPECT_SUCCESS(s2n_psk_set_secret(invalid_psk, secret_0, sizeof(secret_0)));
883 EXPECT_EQUAL(sizeof(huge_identity), UINT16_MAX);
884
885 EXPECT_SUCCESS(s2n_psk_set_identity(invalid_psk, huge_identity, sizeof(huge_identity)));
886
887 EXPECT_SUCCESS(s2n_connection_append_psk(conn, invalid_psk));
888 EXPECT_EQUAL(conn->psk_params.psk_list.len, 1);
889
890 EXPECT_SUCCESS(s2n_connection_free(conn));
891 };
892
893 /* New PSK would make existing list too long for client */
894 {
895 struct s2n_connection *conn = NULL;
896 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
897
898 uint32_t offered_psks_size = 0;
899 struct s2n_psk *test_psk = NULL;
900 EXPECT_SUCCESS(s2n_connection_set_psk_mode(conn, S2N_PSK_MODE_EXTERNAL));
901 while (offered_psks_size < UINT16_MAX) {
902 EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &test_psk));
903 EXPECT_NOT_NULL(test_psk);
904
905 EXPECT_OK(s2n_psk_init(test_psk, S2N_PSK_TYPE_EXTERNAL));
906 EXPECT_SUCCESS(s2n_psk_set_identity(test_psk, identity_1, sizeof(identity_1)));
907 EXPECT_OK(s2n_psk_parameters_offered_psks_size(&conn->psk_params, &offered_psks_size));
908 }
909
910 /* Delete the last PSK that caused the list to exceed the allowed size */
911 EXPECT_OK(s2n_psk_wipe(test_psk));
912 EXPECT_OK(s2n_array_remove(&conn->psk_params.psk_list, conn->psk_params.psk_list.len - 1));
913 EXPECT_OK(s2n_psk_parameters_offered_psks_size(&conn->psk_params, &offered_psks_size));
914 EXPECT_TRUE(offered_psks_size < UINT16_MAX);
915
916 uint32_t original_psk_count = conn->psk_params.psk_list.len;
917 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_append_psk(conn, input_psk),
918 S2N_ERR_OFFERED_PSKS_TOO_LONG);
919 EXPECT_EQUAL(conn->psk_params.psk_list.len, original_psk_count);
920
921 /* Server allows an arbitrarily long list */
922 conn->mode = S2N_SERVER;
923 EXPECT_SUCCESS(s2n_connection_append_psk(conn, input_psk));
924 EXPECT_EQUAL(conn->psk_params.psk_list.len, original_psk_count + 1);
925
926 EXPECT_SUCCESS(s2n_connection_free(conn));
927 };
928
929 /* PSK matches existing external PSK */
930 {
931 struct s2n_connection *conn = NULL;
932 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
933
934 EXPECT_SUCCESS(s2n_connection_append_psk(conn, input_psk));
935 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_append_psk(conn, input_psk),
936 S2N_ERR_DUPLICATE_PSK_IDENTITIES);
937
938 EXPECT_SUCCESS(s2n_connection_free(conn));
939 };
940
941 /* Can't mix resumption and external PSKs */
942 {
943 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
944 EXPECT_NOT_NULL(conn);
945
946 DEFER_CLEANUP(struct s2n_psk *test_external_psk = s2n_test_psk_new(conn), s2n_psk_free);
947 test_external_psk->type = S2N_PSK_TYPE_EXTERNAL;
948 DEFER_CLEANUP(struct s2n_psk *test_resumption_psk = s2n_test_psk_new(conn), s2n_psk_free);
949 test_resumption_psk->type = S2N_PSK_TYPE_RESUMPTION;
950
951 /* Add resumption to list that contains external */
952 {
953 EXPECT_SUCCESS(s2n_connection_append_psk(conn, test_external_psk));
954 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_append_psk(conn, test_resumption_psk),
955 S2N_ERR_PSK_MODE);
956 };
957
958 EXPECT_SUCCESS(s2n_connection_wipe(conn));
959
960 /* Add external to a list that contains resumption */
961 {
962 EXPECT_SUCCESS(s2n_connection_append_psk(conn, test_resumption_psk));
963 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_append_psk(conn, test_external_psk),
964 S2N_ERR_PSK_MODE);
965 };
966
967 EXPECT_SUCCESS(s2n_connection_free(conn));
968 };
969 };
970
971 /* Test: s2n_psk_set_hmac */
972 {
973 EXPECT_FAILURE_WITH_ERRNO(s2n_psk_set_hmac(NULL, S2N_PSK_HMAC_SHA256), S2N_ERR_NULL);
974
975 DEFER_CLEANUP(struct s2n_psk psk, s2n_psk_wipe);
976 EXPECT_OK(s2n_psk_init(&psk, S2N_PSK_TYPE_EXTERNAL));
977
978 EXPECT_EQUAL(psk.hmac_alg, S2N_HMAC_SHA256);
979 EXPECT_FAILURE_WITH_ERRNO(s2n_psk_set_hmac(&psk, -1), S2N_ERR_HMAC_INVALID_ALGORITHM);
980 EXPECT_EQUAL(psk.hmac_alg, S2N_HMAC_SHA256);
981
982 EXPECT_SUCCESS(s2n_psk_set_hmac(&psk, S2N_PSK_HMAC_SHA384));
983 EXPECT_EQUAL(psk.hmac_alg, S2N_HMAC_SHA384);
984
985 EXPECT_SUCCESS(s2n_psk_set_hmac(&psk, S2N_PSK_HMAC_SHA256));
986 EXPECT_EQUAL(psk.hmac_alg, S2N_HMAC_SHA256);
987 };
988
989 /* Test: s2n_config_set_psk_mode */
990 {
991 struct s2n_config *config = s2n_config_new();
992 EXPECT_NOT_NULL(config);
993 EXPECT_EQUAL(config->psk_mode, S2N_PSK_MODE_RESUMPTION);
994
995 EXPECT_FAILURE_WITH_ERRNO(s2n_config_set_psk_mode(NULL, S2N_PSK_MODE_EXTERNAL), S2N_ERR_NULL);
996 EXPECT_EQUAL(config->psk_mode, S2N_PSK_MODE_RESUMPTION);
997
998 EXPECT_SUCCESS(s2n_config_set_psk_mode(config, S2N_PSK_MODE_EXTERNAL));
999 EXPECT_EQUAL(config->psk_mode, S2N_PSK_MODE_EXTERNAL);
1000
1001 EXPECT_SUCCESS(s2n_config_set_psk_mode(config, S2N_PSK_MODE_RESUMPTION));
1002 EXPECT_EQUAL(config->psk_mode, S2N_PSK_MODE_RESUMPTION);
1003
1004 EXPECT_SUCCESS(s2n_config_free(config));
1005 };
1006
1007 /* Test: s2n_connection_set_psk_mode */
1008 {
1009 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
1010 EXPECT_NOT_NULL(conn);
1011 EXPECT_EQUAL(conn->psk_params.type, S2N_PSK_TYPE_RESUMPTION);
1012
1013 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_set_psk_mode(NULL, S2N_PSK_MODE_EXTERNAL), S2N_ERR_NULL);
1014 EXPECT_EQUAL(conn->psk_params.type, S2N_PSK_TYPE_RESUMPTION);
1015 EXPECT_FALSE(conn->psk_mode_overridden);
1016
1017 EXPECT_SUCCESS(s2n_connection_set_psk_mode(conn, S2N_PSK_MODE_RESUMPTION));
1018 EXPECT_EQUAL(conn->psk_params.type, S2N_PSK_TYPE_RESUMPTION);
1019 EXPECT_TRUE(conn->psk_mode_overridden);
1020
1021 EXPECT_SUCCESS(s2n_connection_set_psk_mode(conn, S2N_PSK_MODE_EXTERNAL));
1022 EXPECT_EQUAL(conn->psk_params.type, S2N_PSK_TYPE_EXTERNAL);
1023 EXPECT_TRUE(conn->psk_mode_overridden);
1024
1025 DEFER_CLEANUP(struct s2n_psk *test_external_psk = s2n_test_psk_new(conn), s2n_psk_free);
1026 EXPECT_SUCCESS(s2n_connection_append_psk(conn, test_external_psk));
1027 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_set_psk_mode(conn, S2N_PSK_MODE_RESUMPTION), S2N_ERR_PSK_MODE);
1028 EXPECT_EQUAL(conn->psk_params.type, S2N_PSK_TYPE_EXTERNAL);
1029
1030 EXPECT_SUCCESS(s2n_connection_free(conn));
1031 };
1032
1033 /* Test: s2n_connection_get_negotiated_psk_identity_length */
1034 {
1035 const uint8_t psk_identity[] = "identity";
1036 struct s2n_connection *conn = NULL;
1037 uint16_t identity_length = 0;
1038
1039 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
1040
1041 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_negotiated_psk_identity_length(NULL, &identity_length), S2N_ERR_NULL);
1042 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_negotiated_psk_identity_length(conn, NULL), S2N_ERR_NULL);
1043
1044 EXPECT_NULL(conn->psk_params.chosen_psk);
1045 EXPECT_SUCCESS(s2n_connection_get_negotiated_psk_identity_length(conn, &identity_length));
1046 EXPECT_EQUAL(identity_length, 0);
1047
1048 DEFER_CLEANUP(struct s2n_psk *psk = s2n_external_psk_new(), s2n_psk_free);
1049 EXPECT_SUCCESS(s2n_psk_set_identity(psk, psk_identity, sizeof(psk_identity)));
1050 conn->psk_params.chosen_psk = psk;
1051 EXPECT_SUCCESS(s2n_connection_get_negotiated_psk_identity_length(conn, &identity_length));
1052 EXPECT_EQUAL(identity_length, sizeof(psk_identity));
1053
1054 EXPECT_SUCCESS(s2n_connection_free(conn));
1055 };
1056
1057 /* Test: s2n_connection_get_negotiated_psk_identity */
1058 {
1059 const uint8_t psk_identity[] = "identity";
1060 const uint8_t empty_identity[sizeof(psk_identity)] = { 0 };
1061 struct s2n_connection *conn = NULL;
1062 uint8_t identity[sizeof(psk_identity)] = { 0 };
1063 uint16_t max_identity_length = 0;
1064
1065 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
1066
1067 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_negotiated_psk_identity(NULL, identity, max_identity_length), S2N_ERR_NULL);
1068 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_negotiated_psk_identity(conn, NULL, max_identity_length), S2N_ERR_NULL);
1069
1070 EXPECT_NULL(conn->psk_params.chosen_psk);
1071 EXPECT_SUCCESS(s2n_connection_get_negotiated_psk_identity(conn, identity, max_identity_length));
1072 EXPECT_EQUAL(max_identity_length, 0);
1073 EXPECT_BYTEARRAY_EQUAL(identity, empty_identity, sizeof(empty_identity));
1074
1075 DEFER_CLEANUP(struct s2n_psk *psk = s2n_external_psk_new(), s2n_psk_free);
1076 EXPECT_SUCCESS(s2n_psk_set_identity(psk, psk_identity, sizeof(psk_identity)));
1077 conn->psk_params.chosen_psk = psk;
1078 EXPECT_SUCCESS(s2n_connection_get_negotiated_psk_identity_length(conn, &max_identity_length));
1079 EXPECT_SUCCESS(s2n_connection_get_negotiated_psk_identity(conn, identity, max_identity_length));
1080 EXPECT_BYTEARRAY_EQUAL(identity, psk_identity, sizeof(psk_identity));
1081
1082 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_negotiated_psk_identity(conn, identity, 0), S2N_ERR_INSUFFICIENT_MEM_SIZE);
1083 EXPECT_SUCCESS(s2n_connection_free(conn));
1084 };
1085
1086 /* Test s2n_psk_validate_keying_material */
1087 {
1088 uint64_t current_time = 100;
1089
1090 /* Safety */
1091 EXPECT_ERROR_WITH_ERRNO(s2n_psk_validate_keying_material(NULL), S2N_ERR_NULL);
1092
1093 struct s2n_config *config = s2n_config_new();
1094 EXPECT_NOT_NULL(config);
1095 EXPECT_OK(s2n_config_mock_wall_clock(config, &current_time));
1096
1097 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
1098 EXPECT_NOT_NULL(conn);
1099 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
1100 DEFER_CLEANUP(struct s2n_psk *chosen_psk = s2n_test_psk_new(conn), s2n_psk_free);
1101
1102 /* No-op if no chosen PSK */
1103 EXPECT_OK(s2n_psk_validate_keying_material(conn));
1104
1105 conn->psk_params.chosen_psk = chosen_psk;
1106
1107 /* No-op if chosen PSK is external */
1108 chosen_psk->type = S2N_PSK_TYPE_EXTERNAL;
1109 EXPECT_OK(s2n_psk_validate_keying_material(conn));
1110
1111 chosen_psk->type = S2N_PSK_TYPE_RESUMPTION;
1112
1113 /* Okay if chosen PSK's material is not expired */
1114 chosen_psk->keying_material_expiration = UINT64_MAX;
1115 EXPECT_OK(s2n_psk_validate_keying_material(conn));
1116
1117 /* Fails if chosen PSK's material is expired */
1118 chosen_psk->keying_material_expiration = 0;
1119 EXPECT_ERROR_WITH_ERRNO(s2n_psk_validate_keying_material(conn), S2N_ERR_KEYING_MATERIAL_EXPIRED);
1120
1121 /* Fails if chosen PSK's material expires at current_time */
1122 chosen_psk->keying_material_expiration = current_time;
1123 EXPECT_ERROR_WITH_ERRNO(s2n_psk_validate_keying_material(conn), S2N_ERR_KEYING_MATERIAL_EXPIRED);
1124
1125 /* Fails if chosen PSK's material has less than 1s left to live */
1126 chosen_psk->keying_material_expiration = current_time + 1;
1127 EXPECT_ERROR_WITH_ERRNO(s2n_psk_validate_keying_material(conn), S2N_ERR_KEYING_MATERIAL_EXPIRED);
1128
1129 /* Okay if chosen PSK's material has more than 1s left to live */
1130 chosen_psk->keying_material_expiration = current_time + ONE_SEC_IN_NANOS + 1;
1131 EXPECT_OK(s2n_psk_validate_keying_material(conn));
1132
1133 EXPECT_SUCCESS(s2n_connection_free(conn));
1134 EXPECT_SUCCESS(s2n_config_free(config));
1135 };
1136
1137 END_TEST();
1138 }