Debian Codegraph @

crt/s2n/tests/unit/s2n_self_talk_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 #define TEST_SHARED_PSK_WIRE_INDEX_1 1
20 #define TEST_SHARED_PSK_WIRE_INDEX_2 2
21 #define TEST_PSK_HMAC S2N_PSK_HMAC_SHA256
22
23 #define ARE_FULL_HANDSHAKES(client, server) \
24 (IS_FULL_HANDSHAKE(client) && IS_FULL_HANDSHAKE(server))
25
26 #define IS_CLIENT_AUTH(client, server) \
27 (IS_CLIENT_AUTH_HANDSHAKE(client) && IS_CLIENT_AUTH_HANDSHAKE(server))
28
29 #define IS_HELLO_RETRY(client, server) \
30 (((client->handshake.handshake_type) & HELLO_RETRY_REQUEST) \
31 && ((server->handshake.handshake_type) & HELLO_RETRY_REQUEST))
32
33 #define s2n_set_io_pair_both_connections(client, server, io_pair) \
34 EXPECT_SUCCESS(s2n_connection_set_io_pair(client, &io_pair)); \
35 EXPECT_SUCCESS(s2n_connection_set_io_pair(server, &io_pair));
36
37 #define s2n_set_config_both_connections(client, server, config) \
38 EXPECT_SUCCESS(s2n_connection_set_config(client, config)); \
39 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
40
41 uint8_t test_shared_identity[] = "test shared identity";
42 uint8_t test_shared_secret[] = "test shared secret";
43 uint8_t test_shared_identity_2[] = "test shared identity 2";
44 uint8_t test_shared_secret_2[] = "test shared secret 2";
45 uint8_t test_other_client_data[] = "test other client data";
46 uint8_t test_other_server_data[] = "test other server data";
47
48 static s2n_result setup_psk(struct s2n_connection *conn, const uint8_t *test_identity_data, uint16_t test_identity_size,
49 const uint8_t *test_secret_data, uint16_t test_secret_size, s2n_psk_hmac test_hmac)
50 {
51 RESULT_ENSURE_REF(conn);
52 RESULT_ENSURE_REF(test_identity_data);
53 RESULT_ENSURE_REF(test_secret_data);
54
55 struct s2n_psk *psk = s2n_external_psk_new();
56 RESULT_GUARD_POSIX(s2n_psk_set_identity(psk, test_identity_data, test_identity_size));
57 RESULT_GUARD_POSIX(s2n_psk_set_secret(psk, test_secret_data, test_secret_size));
58 RESULT_GUARD_POSIX(s2n_psk_set_hmac(psk, test_hmac));
59 RESULT_GUARD_POSIX(s2n_connection_append_psk(conn, psk));
60 RESULT_GUARD_POSIX(s2n_psk_free(&psk));
61 EXPECT_NULL(psk);
62
63 return S2N_RESULT_OK;
64 }
65
66 static s2n_result setup_client_psks(struct s2n_connection *client_conn)
67 {
68 RESULT_ENSURE_REF(client_conn);
69
70 /* Setup other client PSK */
71 EXPECT_OK(setup_psk(client_conn, test_other_client_data, sizeof(test_other_client_data), test_other_client_data,
72 sizeof(test_other_client_data), TEST_PSK_HMAC));
73 /* Setup first shared PSK for client */
74 EXPECT_OK(setup_psk(client_conn, test_shared_identity, sizeof(test_shared_identity), test_shared_secret,
75 sizeof(test_shared_secret), TEST_PSK_HMAC));
76 /* Setup second shared PSK for client */
77 EXPECT_OK(setup_psk(client_conn, test_shared_identity_2, sizeof(test_shared_identity_2), test_shared_secret_2,
78 sizeof(test_shared_secret_2), TEST_PSK_HMAC));
79
80 return S2N_RESULT_OK;
81 }
82
83 static s2n_result setup_server_psks(struct s2n_connection *server_conn)
84 {
85 RESULT_ENSURE_REF(server_conn);
86
87 /* Setup first shared PSK for server */
88 EXPECT_OK(setup_psk(server_conn, test_shared_identity, sizeof(test_shared_identity), test_shared_secret,
89 sizeof(test_shared_secret), TEST_PSK_HMAC));
90 /* Setup other server PSK */
91 EXPECT_OK(setup_psk(server_conn, test_other_server_data, sizeof(test_other_server_data), test_other_server_data,
92 sizeof(test_other_server_data), S2N_PSK_HMAC_SHA384));
93 /* Setup second shared PSK for server */
94 EXPECT_OK(setup_psk(server_conn, test_shared_identity_2, sizeof(test_shared_identity_2), test_shared_secret_2,
95 sizeof(test_shared_secret_2), TEST_PSK_HMAC));
96
97 return S2N_RESULT_OK;
98 }
99
100 static s2n_result setup_psks_with_no_match(struct s2n_connection *client_conn, struct s2n_connection *server_conn)
101 {
102 RESULT_ENSURE_REF(client_conn);
103 RESULT_ENSURE_REF(server_conn);
104
105 /* Setup other client PSK */
106 EXPECT_OK(setup_psk(client_conn, test_other_client_data, sizeof(test_other_client_data), test_other_client_data,
107 sizeof(test_other_client_data), S2N_PSK_HMAC_SHA256));
108 /* Setup other server PSK */
109 EXPECT_OK(setup_psk(server_conn, test_other_server_data, sizeof(test_other_server_data), test_other_server_data,
110 sizeof(test_other_server_data), S2N_PSK_HMAC_SHA384));
111
112 return S2N_RESULT_OK;
113 }
114
115 static s2n_result validate_chosen_psk(struct s2n_connection *server_conn, uint8_t *psk_identity_data,
116 size_t psk_identity_size, size_t chosen_index)
117 {
118 RESULT_ENSURE_REF(server_conn);
119 RESULT_ENSURE_REF(psk_identity_data);
120 RESULT_ENSURE_REF(server_conn->psk_params.chosen_psk);
121
122 RESULT_ENSURE_EQ(server_conn->psk_params.chosen_psk->identity.size, psk_identity_size);
123 RESULT_ENSURE_EQ(memcmp(server_conn->psk_params.chosen_psk->identity.data, psk_identity_data, psk_identity_size), 0);
124 RESULT_ENSURE_EQ(server_conn->psk_params.chosen_psk_wire_index, chosen_index);
125
126 return S2N_RESULT_OK;
127 }
128
129 static int s2n_test_select_psk_identity_callback(struct s2n_connection *conn, void *context,
130 struct s2n_offered_psk_list *psk_identity_list)
131 {
132 struct s2n_offered_psk offered_psk = { 0 };
133 uint16_t idx = 0;
134 while (s2n_offered_psk_list_has_next(psk_identity_list)) {
135 POSIX_GUARD(s2n_offered_psk_list_next(psk_identity_list, &offered_psk));
136 if (idx == TEST_SHARED_PSK_WIRE_INDEX_2) {
137 POSIX_GUARD(s2n_offered_psk_list_choose_psk(psk_identity_list, &offered_psk));
138 break;
139 }
140 idx++;
141 };
142 return S2N_SUCCESS;
143 }
144
145 static int s2n_client_hello_no_op_cb(struct s2n_connection *conn, void *ctx)
146 {
147 return S2N_SUCCESS;
148 }
149
150 int main(int argc, char **argv)
151 {
152 BEGIN_TEST();
153
154 if (!s2n_is_tls13_fully_supported()) {
155 END_TEST();
156 }
157
158 /* Setup connections */
159 struct s2n_connection *client_conn = NULL;
160 struct s2n_connection *server_conn = NULL;
161 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
162 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
163
164 /* Setup config */
165 struct s2n_config *config = NULL;
166 EXPECT_NOT_NULL(config = s2n_config_new());
167 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13"));
168 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config));
169
170 /* Setup config with certificates set */
171 struct s2n_config *config_with_certs = NULL;
172 EXPECT_NOT_NULL(config_with_certs = s2n_config_new());
173 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config_with_certs, "default_tls13"));
174 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config_with_certs));
175 struct s2n_cert_chain_and_key *chain_and_key = NULL;
176 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key, S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN,
177 S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY));
178 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config_with_certs, chain_and_key));
179
180 /* Create nonblocking pipes */
181 struct s2n_test_io_pair io_pair = { 0 };
182 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
183
184 /* Basic PSK without callback function, without certificates and client auth not set */
185 {
186 s2n_set_config_both_connections(client_conn, server_conn, config);
187 s2n_set_io_pair_both_connections(client_conn, server_conn, io_pair);
188
189 /* Setup PSKs */
190 EXPECT_OK(setup_client_psks(client_conn));
191 EXPECT_OK(setup_server_psks(server_conn));
192
193 /* Negotiate handshake */
194 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
195
196 /* Validate handshake type */
197 EXPECT_FALSE(ARE_FULL_HANDSHAKES(client_conn, server_conn));
198
199 /* Validate chosen PSK */
200 EXPECT_OK(validate_chosen_psk(server_conn, test_shared_identity, sizeof(test_shared_identity),
201 TEST_SHARED_PSK_WIRE_INDEX_1));
202
203 EXPECT_SUCCESS(s2n_shutdown_test_server_and_client(server_conn, client_conn));
204
205 EXPECT_SUCCESS(s2n_connection_wipe(client_conn));
206 EXPECT_SUCCESS(s2n_connection_wipe(server_conn));
207 };
208
209 /* PSK with callback function */
210 {
211 s2n_set_config_both_connections(client_conn, server_conn, config);
212 s2n_set_io_pair_both_connections(client_conn, server_conn, io_pair);
213
214 /* Setup PSKs */
215 EXPECT_OK(setup_client_psks(client_conn));
216 EXPECT_OK(setup_server_psks(server_conn));
217
218 /* Set the customer callback to select PSK identity */
219 EXPECT_SUCCESS(s2n_config_set_psk_selection_callback(server_conn->config, s2n_test_select_psk_identity_callback, NULL));
220 EXPECT_EQUAL(server_conn->config->psk_selection_cb, s2n_test_select_psk_identity_callback);
221
222 /* Negotiate handshake */
223 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
224
225 /* Validate handshake type */
226 EXPECT_FALSE(ARE_FULL_HANDSHAKES(client_conn, server_conn));
227
228 /* Validate chosen PSK */
229 EXPECT_OK(validate_chosen_psk(server_conn, test_shared_identity_2, sizeof(test_shared_identity_2),
230 TEST_SHARED_PSK_WIRE_INDEX_2));
231
232 EXPECT_SUCCESS(s2n_shutdown_test_server_and_client(server_conn, client_conn));
233
234 server_conn->config->psk_selection_cb = NULL;
235
236 EXPECT_SUCCESS(s2n_connection_wipe(client_conn));
237 EXPECT_SUCCESS(s2n_connection_wipe(server_conn));
238 };
239
240 /* PSK with certificates set */
241 {
242 /* Setup certs */
243 s2n_set_config_both_connections(client_conn, server_conn, config_with_certs);
244 s2n_set_io_pair_both_connections(client_conn, server_conn, io_pair);
245
246 /* Setup PSKs */
247 EXPECT_OK(setup_client_psks(client_conn));
248 EXPECT_OK(setup_server_psks(server_conn));
249
250 /* Negotiate handshake */
251 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
252
253 /* Validate handshake type */
254 EXPECT_FALSE(ARE_FULL_HANDSHAKES(client_conn, server_conn));
255
256 /* Validate chosen PSK */
257 EXPECT_OK(validate_chosen_psk(server_conn, test_shared_identity, sizeof(test_shared_identity),
258 TEST_SHARED_PSK_WIRE_INDEX_1));
259
260 EXPECT_SUCCESS(s2n_shutdown_test_server_and_client(server_conn, client_conn));
261
262 EXPECT_SUCCESS(s2n_connection_wipe(client_conn));
263 EXPECT_SUCCESS(s2n_connection_wipe(server_conn));
264 };
265
266 /* PSK with certificates set and client auth set as required */
267 {
268 /* Setup certs */
269 s2n_set_config_both_connections(client_conn, server_conn, config_with_certs);
270 s2n_set_io_pair_both_connections(client_conn, server_conn, io_pair);
271
272 /* Explicitly set client_auth as required */
273 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(client_conn, S2N_CERT_AUTH_REQUIRED));
274 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(server_conn, S2N_CERT_AUTH_REQUIRED));
275
276 /* Setup PSKs */
277 EXPECT_OK(setup_client_psks(client_conn));
278 EXPECT_OK(setup_server_psks(server_conn));
279
280 /* Negotiate handshake */
281 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
282
283 /* Validate handshake type */
284 EXPECT_FALSE(IS_CLIENT_AUTH(client_conn, server_conn));
285 EXPECT_FALSE(ARE_FULL_HANDSHAKES(client_conn, server_conn));
286
287 /* Validate chosen PSK */
288 EXPECT_OK(validate_chosen_psk(server_conn, test_shared_identity, sizeof(test_shared_identity),
289 TEST_SHARED_PSK_WIRE_INDEX_1));
290
291 EXPECT_SUCCESS(s2n_shutdown_test_server_and_client(server_conn, client_conn));
292
293 EXPECT_SUCCESS(s2n_connection_wipe(client_conn));
294 EXPECT_SUCCESS(s2n_connection_wipe(server_conn));
295 };
296
297 /* Basic PSK with Client Hello async callback set */
298 {
299 EXPECT_SUCCESS(s2n_config_set_client_hello_cb(config, s2n_client_hello_no_op_cb, NULL));
300 EXPECT_SUCCESS(s2n_config_set_client_hello_cb_mode(config, S2N_CLIENT_HELLO_CB_NONBLOCKING));
301
302 s2n_set_config_both_connections(client_conn, server_conn, config);
303 s2n_set_io_pair_both_connections(client_conn, server_conn, io_pair);
304
305 /* Setup PSKs */
306 EXPECT_OK(setup_client_psks(client_conn));
307 EXPECT_OK(setup_server_psks(server_conn));
308
309 /* Handshake negotiation is successful when the Client Hello callback is marked as done */
310 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, client_conn), S2N_ERR_ASYNC_BLOCKED);
311 EXPECT_SUCCESS(s2n_client_hello_cb_done(server_conn));
312 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
313
314 /* Validate handshake type */
315 EXPECT_FALSE(ARE_FULL_HANDSHAKES(client_conn, server_conn));
316
317 /* Validate chosen PSK */
318 EXPECT_OK(validate_chosen_psk(server_conn, test_shared_identity, sizeof(test_shared_identity),
319 TEST_SHARED_PSK_WIRE_INDEX_1));
320
321 EXPECT_SUCCESS(s2n_shutdown_test_server_and_client(server_conn, client_conn));
322
323 EXPECT_SUCCESS(s2n_connection_wipe(client_conn));
324 EXPECT_SUCCESS(s2n_connection_wipe(server_conn));
325 EXPECT_SUCCESS(s2n_config_set_client_hello_cb(config, NULL, NULL));
326 };
327
328 /* HRR with PSK and Client Hello async callback set */
329 {
330 EXPECT_SUCCESS(s2n_config_set_client_hello_cb(config_with_certs, s2n_client_hello_no_op_cb, NULL));
331 EXPECT_SUCCESS(s2n_config_set_client_hello_cb_mode(config_with_certs, S2N_CLIENT_HELLO_CB_NONBLOCKING));
332
333 /* Setup certs */
334 s2n_set_config_both_connections(client_conn, server_conn, config_with_certs);
335 s2n_set_io_pair_both_connections(client_conn, server_conn, io_pair);
336
337 /* Force the HRR path */
338 client_conn->security_policy_override = &security_policy_test_tls13_retry;
339
340 /* Setup PSKs */
341 EXPECT_OK(setup_client_psks(client_conn));
342 EXPECT_OK(setup_server_psks(server_conn));
343
344 /* Handshake negotiation is successful when the Client Hello callback is marked as done */
345 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, client_conn), S2N_ERR_ASYNC_BLOCKED);
346 EXPECT_SUCCESS(s2n_client_hello_cb_done(server_conn));
347 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
348
349 /* Validate handshake type */
350 EXPECT_TRUE(IS_HELLO_RETRY(client_conn, server_conn));
351 EXPECT_FALSE(ARE_FULL_HANDSHAKES(client_conn, server_conn));
352
353 /* Validate chosen PSK */
354 EXPECT_OK(validate_chosen_psk(server_conn, test_shared_identity, sizeof(test_shared_identity),
355 TEST_SHARED_PSK_WIRE_INDEX_1));
356
357 EXPECT_SUCCESS(s2n_shutdown_test_server_and_client(server_conn, client_conn));
358
359 EXPECT_SUCCESS(s2n_connection_wipe(client_conn));
360 EXPECT_SUCCESS(s2n_connection_wipe(server_conn));
361 EXPECT_SUCCESS(s2n_config_set_client_hello_cb(config_with_certs, NULL, NULL));
362 };
363
364 /* Fallback to full handshake if no PSK is chosen and certificates are set */
365 {
366 /* Setup certs */
367 s2n_set_config_both_connections(client_conn, server_conn, config_with_certs);
368 s2n_set_io_pair_both_connections(client_conn, server_conn, io_pair);
369
370 /* Setup no matching PSKs */
371 EXPECT_OK(setup_psks_with_no_match(client_conn, server_conn));
372
373 /* Negotiate handshake */
374 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
375
376 /* Validate handshake type */
377 EXPECT_TRUE(ARE_FULL_HANDSHAKES(client_conn, server_conn));
378
379 /* Validate that a PSK is not chosen */
380 EXPECT_NULL(server_conn->psk_params.chosen_psk);
381
382 EXPECT_SUCCESS(s2n_shutdown_test_server_and_client(server_conn, client_conn));
383
384 EXPECT_SUCCESS(s2n_connection_wipe(client_conn));
385 EXPECT_SUCCESS(s2n_connection_wipe(server_conn));
386 };
387
388 /* Fallback to full handshake uses client auth if requested */
389 {
390 /* Setup certs */
391 s2n_set_config_both_connections(client_conn, server_conn, config_with_certs);
392 s2n_set_io_pair_both_connections(client_conn, server_conn, io_pair);
393
394 /* Explicitly set client_auth as required */
395 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(client_conn, S2N_CERT_AUTH_REQUIRED));
396 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(server_conn, S2N_CERT_AUTH_REQUIRED));
397
398 /* Setup no matching PSKs */
399 EXPECT_OK(setup_psks_with_no_match(client_conn, server_conn));
400
401 /* Negotiate handshake */
402 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
403
404 /* Validate handshake type */
405 EXPECT_TRUE(IS_CLIENT_AUTH(client_conn, server_conn));
406 EXPECT_TRUE(ARE_FULL_HANDSHAKES(client_conn, server_conn));
407
408 /* Validate that a PSK is not chosen */
409 EXPECT_NULL(server_conn->psk_params.chosen_psk);
410
411 EXPECT_SUCCESS(s2n_shutdown_test_server_and_client(server_conn, client_conn));
412
413 EXPECT_SUCCESS(s2n_connection_wipe(client_conn));
414 EXPECT_SUCCESS(s2n_connection_wipe(server_conn));
415 };
416
417 /* Error Case: Fallback to full handshake if no PSK is chosen and certificates are not set */
418 {
419 s2n_set_config_both_connections(client_conn, server_conn, config);
420 s2n_set_io_pair_both_connections(client_conn, server_conn, io_pair);
421 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
422 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
423
424 /* Setup no matching PSKs */
425 EXPECT_OK(setup_psks_with_no_match(client_conn, server_conn));
426
427 /* Negotiate handshake */
428 EXPECT_FAILURE(s2n_negotiate_test_server_and_client(server_conn, client_conn));
429
430 /* Validate that a PSK is not chosen */
431 EXPECT_NULL(server_conn->psk_params.chosen_psk);
432
433 /* Validate handshake type is not FULL_HANDSHAKE */
434 EXPECT_FALSE(ARE_FULL_HANDSHAKES(client_conn, server_conn));
435
436 EXPECT_SUCCESS(s2n_connection_wipe(client_conn));
437 EXPECT_SUCCESS(s2n_connection_wipe(server_conn));
438 };
439
440 /* Clean-up */
441 EXPECT_SUCCESS(s2n_connection_free(server_conn));
442 EXPECT_SUCCESS(s2n_connection_free(client_conn));
443 EXPECT_SUCCESS(s2n_config_free(config));
444 EXPECT_SUCCESS(s2n_config_free(config_with_certs));
445 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
446 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
447
448 END_TEST();
449 }