Debian Codegraph @

crt/s2n/tests/unit/s2n_handshake_test.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include "tls/s2n_handshake.h"
17
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <sys/wait.h>
23 #include <unistd.h>
24
25 #include "api/s2n.h"
26 #include "crypto/s2n_fips.h"
27 #include "crypto/s2n_mldsa.h"
28 #include "crypto/s2n_rsa_pss.h"
29 #include "s2n_test.h"
30 #include "testlib/s2n_testlib.h"
31 #include "tls/s2n_cipher_suites.h"
32 #include "tls/s2n_connection.h"
33 #include "tls/s2n_security_policies.h"
34 #include "tls/s2n_tls13.h"
35 #include "utils/s2n_safety.h"
36
37 enum test_type {
38 TEST_TYPE_START,
39 TEST_TYPE_SYNC = TEST_TYPE_START,
40 TEST_TYPE_ASYNC,
41 TEST_TYPE_END
42 } test_type;
43
44 struct s2n_async_pkey_op *pkey_op = NULL;
45 int async_pkey_op_called = 0;
46 int async_pkey_op_performed = 0;
47
48 static uint8_t s2n_trust_all_verify_host(const char *host_name, size_t len, void *data)
49 {
50 return 1;
51 }
52
53 static int handle_async(struct s2n_connection *server_conn)
54 {
55 s2n_blocked_status server_blocked;
56
57 /* Test that handshake can't proceed until async pkey op is complete */
58 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate(server_conn, &server_blocked), S2N_ERR_ASYNC_BLOCKED);
59
60 /* Check that we have pkey_op */
61 EXPECT_NOT_NULL(pkey_op);
62
63 /* Test that not performed pkey can't be applied */
64 EXPECT_FAILURE_WITH_ERRNO(s2n_async_pkey_op_apply(pkey_op, server_conn),
65 S2N_ERR_ASYNC_NOT_PERFORMED);
66
67 /* Extract pkey */
68 struct s2n_cert_chain_and_key *chain_and_key = s2n_connection_get_selected_cert(server_conn);
69 EXPECT_NOT_NULL(chain_and_key);
70
71 s2n_cert_private_key *pkey = s2n_cert_chain_and_key_get_private_key(chain_and_key);
72 EXPECT_NOT_NULL(pkey);
73
74 /* Test that we can perform pkey operation only once */
75 EXPECT_SUCCESS(s2n_async_pkey_op_perform(pkey_op, pkey));
76 EXPECT_FAILURE_WITH_ERRNO(s2n_async_pkey_op_perform(pkey_op, pkey), S2N_ERR_ASYNC_ALREADY_PERFORMED);
77
78 /* Test that pkey op can't be applied to connection other than original one */
79 struct s2n_connection *server_conn2 = s2n_connection_new(S2N_SERVER);
80 EXPECT_NOT_NULL(server_conn2);
81 EXPECT_FAILURE_WITH_ERRNO(s2n_async_pkey_op_apply(pkey_op, server_conn2),
82 S2N_ERR_ASYNC_WRONG_CONNECTION);
83 EXPECT_SUCCESS(s2n_connection_free(server_conn2));
84
85 /* Test that pkey op can be applied to original connection */
86 EXPECT_SUCCESS(s2n_async_pkey_op_apply(pkey_op, server_conn));
87
88 /* Test that pkey op can't be applied to original connection more than once */
89 EXPECT_FAILURE_WITH_ERRNO(s2n_async_pkey_op_apply(pkey_op, server_conn),
90 S2N_ERR_ASYNC_ALREADY_APPLIED);
91
92 /* Free the pkey op */
93 EXPECT_SUCCESS(s2n_async_pkey_op_free(pkey_op));
94 pkey_op = NULL;
95
96 async_pkey_op_performed++;
97
98 return 0;
99 }
100
101 static int try_handshake(struct s2n_connection *server_conn, struct s2n_connection *client_conn)
102 {
103 s2n_blocked_status server_blocked;
104 s2n_blocked_status client_blocked;
105
106 int tries = 0;
107 do {
108 int client_rc = s2n_negotiate(client_conn, &client_blocked);
109 if (!(client_rc == 0 || (client_blocked && s2n_error_get_type(s2n_errno) == S2N_ERR_T_BLOCKED))) {
110 return -1;
111 }
112
113 int server_rc = s2n_negotiate(server_conn, &server_blocked);
114 if (!(server_rc == 0 || (server_blocked && s2n_error_get_type(s2n_errno) == S2N_ERR_T_BLOCKED))) {
115 return -1;
116 }
117
118 if (server_blocked == S2N_BLOCKED_ON_APPLICATION_INPUT) {
119 /* Only can happen in async tests */
120 EXPECT_EQUAL(test_type, TEST_TYPE_ASYNC);
121 EXPECT_SUCCESS(handle_async(server_conn));
122 }
123
124 EXPECT_NOT_EQUAL(++tries, 5);
125 } while (client_blocked || server_blocked);
126
127 POSIX_GUARD(s2n_shutdown_test_server_and_client(server_conn, client_conn));
128 return S2N_SUCCESS;
129 }
130
131 int test_cipher_preferences(struct s2n_config *server_config, struct s2n_config *client_config,
132 struct s2n_cert_chain_and_key *expected_cert_chain, s2n_signature_algorithm sig_alg)
133 {
134 const struct s2n_security_policy *security_policy = server_config->security_policy;
135 EXPECT_NOT_NULL(security_policy);
136
137 if (s2n_is_in_fips_mode()) {
138 /* Override default client config ciphers when in FIPS mode to ensure all FIPS
139 * default ciphers are tested.
140 */
141 client_config->security_policy = security_policy;
142 }
143
144 const struct s2n_cipher_preferences *cipher_preferences = security_policy->cipher_preferences;
145 EXPECT_NOT_NULL(cipher_preferences);
146
147 /* Verify that a handshake succeeds for every available cipher in the default list. */
148 for (int cipher_idx = 0; cipher_idx < cipher_preferences->count; cipher_idx++) {
149 struct s2n_cipher_suite *expected_cipher = cipher_preferences->suites[cipher_idx];
150 uint8_t expect_failure = 0;
151
152 /* Expect failure if the libcrypto we're building with can't support the cipher */
153 if (!expected_cipher->available) {
154 expect_failure = 1;
155 }
156
157 s2n_signature_algorithm expected_sig_alg = sig_alg;
158 /* Expect no server signature algorithm if RSA kex */
159 if (expected_cipher->key_exchange_alg == &s2n_rsa) {
160 expected_sig_alg = S2N_SIGNATURE_ANONYMOUS;
161 }
162
163 TEST_DEBUG_PRINT("Testing %s in %s mode, expect_failure=%d\n", expected_cipher->name,
164 test_type == TEST_TYPE_SYNC ? "synchronous" : "asynchronous", expect_failure);
165
166 struct s2n_security_policy server_security_policy;
167 struct s2n_cipher_preferences server_cipher_preferences;
168 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
169 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
170 EXPECT_NOT_NULL(client_conn);
171 EXPECT_NOT_NULL(server_conn);
172
173 /* Craft a cipher preference with a cipher_idx cipher and assign it to server */
174 EXPECT_MEMCPY_SUCCESS(&server_cipher_preferences, cipher_preferences, sizeof(server_cipher_preferences));
175 server_cipher_preferences.count = 1;
176 server_cipher_preferences.suites = &expected_cipher;
177
178 EXPECT_MEMCPY_SUCCESS(&server_security_policy, security_policy, sizeof(server_security_policy));
179 server_security_policy.cipher_preferences = &server_cipher_preferences;
180
181 server_conn->security_policy_override = &server_security_policy;
182
183 /* Create nonblocking pipes */
184 struct s2n_test_io_pair io_pair;
185 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
186
187 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
188 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
189
190 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
191 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
192
193 /* Reset counters */
194 async_pkey_op_called = 0;
195 async_pkey_op_performed = 0;
196
197 if (!expect_failure) {
198 POSIX_GUARD(try_handshake(server_conn, client_conn));
199
200 EXPECT_STRING_EQUAL(s2n_connection_get_cipher(server_conn), expected_cipher->name);
201
202 EXPECT_EQUAL(server_conn->handshake_params.our_chain_and_key, expected_cert_chain);
203 EXPECT_NOT_NULL(server_conn->handshake_params.server_cert_sig_scheme);
204 EXPECT_EQUAL(server_conn->handshake_params.server_cert_sig_scheme->sig_alg, expected_sig_alg);
205
206 EXPECT_TRUE(IS_NEGOTIATED(server_conn));
207 EXPECT_TRUE(IS_NEGOTIATED(client_conn));
208
209 EXPECT_TRUE(IS_FULL_HANDSHAKE(server_conn));
210 EXPECT_TRUE(IS_FULL_HANDSHAKE(client_conn));
211
212 EXPECT_STRING_EQUAL(s2n_connection_get_last_message_name(server_conn), "APPLICATION_DATA");
213 EXPECT_STRING_EQUAL(s2n_connection_get_last_message_name(client_conn), "APPLICATION_DATA");
214
215 if (test_type == TEST_TYPE_ASYNC) {
216 EXPECT_EQUAL(async_pkey_op_called, 1);
217 EXPECT_EQUAL(async_pkey_op_performed, 1);
218 } else {
219 EXPECT_EQUAL(async_pkey_op_called, 0);
220 EXPECT_EQUAL(async_pkey_op_performed, 0);
221 }
222 } else {
223 POSIX_ENSURE_EQ(try_handshake(server_conn, client_conn), -1);
224 EXPECT_STRING_NOT_EQUAL(s2n_connection_get_last_message_name(server_conn), "APPLICATION_DATA");
225 EXPECT_STRING_NOT_EQUAL(s2n_connection_get_last_message_name(client_conn), "APPLICATION_DATA");
226 EXPECT_EQUAL(async_pkey_op_called, 0);
227 EXPECT_EQUAL(async_pkey_op_performed, 0);
228 }
229
230 EXPECT_SUCCESS(s2n_connection_free(server_conn));
231 EXPECT_SUCCESS(s2n_connection_free(client_conn));
232 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
233 }
234
235 return 0;
236 }
237
238 int async_pkey_fn(struct s2n_connection *conn, struct s2n_async_pkey_op *op)
239 {
240 /* Just store the op, we will process it later */
241 pkey_op = op;
242 async_pkey_op_called++;
243
244 return 0;
245 }
246
247 int main(int argc, char **argv)
248 {
249 BEGIN_TEST();
250 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
251
252 char dhparams_pem[S2N_MAX_TEST_PEM_SIZE];
253 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_DHPARAMS, dhparams_pem, S2N_MAX_TEST_PEM_SIZE));
254
255 for (test_type = TEST_TYPE_START; test_type < TEST_TYPE_END; test_type++) {
256 /* Test: RSA cert */
257 {
258 struct s2n_config *server_config = NULL, *client_config = NULL;
259
260 struct s2n_cert_chain_and_key *chain_and_key = NULL;
261 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
262 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
263
264 EXPECT_NOT_NULL(server_config = s2n_config_new());
265 /* We need a security policy that only supports RSA certificates for auth */
266 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config, "20170210"));
267 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
268 EXPECT_SUCCESS(s2n_config_add_dhparams(server_config, dhparams_pem));
269 /* Enable signature validation */
270 EXPECT_SUCCESS(s2n_config_set_verify_after_sign(server_config, S2N_VERIFY_AFTER_SIGN_ENABLED));
271 if (test_type == TEST_TYPE_ASYNC) {
272 EXPECT_SUCCESS(s2n_config_set_async_pkey_callback(server_config, async_pkey_fn));
273 }
274
275 EXPECT_NOT_NULL(client_config = s2n_config_new());
276 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "20170210"));
277 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(client_config));
278
279 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(client_config, S2N_DEFAULT_TEST_CERT_CHAIN, NULL));
280
281 EXPECT_SUCCESS(test_cipher_preferences(server_config, client_config,
282 chain_and_key, S2N_SIGNATURE_RSA));
283
284 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
285 EXPECT_SUCCESS(s2n_config_free(server_config));
286 EXPECT_SUCCESS(s2n_config_free(client_config));
287 };
288
289 /* Test: RSA (TLS 1.2) key exchanges with TLS 1.3 client */
290 {
291 if (!s2n_is_in_fips_mode()) {
292 /* Enable TLS 1.3 for the client */
293 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
294 struct s2n_config *server_config = NULL, *client_config = NULL;
295
296 struct s2n_cert_chain_and_key *chain_and_key = NULL;
297 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
298 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
299
300 EXPECT_NOT_NULL(server_config = s2n_config_new());
301 /* Configures server with maximum version 1.2 with only RSA key exchange ciphersuites */
302 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config, "test_all_rsa_kex"));
303 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
304 /* Enable signature validation */
305 EXPECT_SUCCESS(s2n_config_set_verify_after_sign(server_config, S2N_VERIFY_AFTER_SIGN_ENABLED));
306 if (test_type == TEST_TYPE_ASYNC) {
307 EXPECT_SUCCESS(s2n_config_set_async_pkey_callback(server_config, async_pkey_fn));
308 }
309
310 EXPECT_NOT_NULL(client_config = s2n_config_new());
311 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "test_all"));
312 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(client_config));
313
314 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(client_config, S2N_DEFAULT_TEST_CERT_CHAIN, NULL));
315
316 /* RSA encrypted premaster secret key exchange requires client versions
317 * to be set and read correctly, this test covers the behavior with a 1.3 client */
318 EXPECT_SUCCESS(test_cipher_preferences(server_config, client_config,
319 chain_and_key, S2N_SIGNATURE_RSA));
320
321 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
322 EXPECT_SUCCESS(s2n_config_free(server_config));
323 EXPECT_SUCCESS(s2n_config_free(client_config));
324 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
325 }
326 };
327
328 /* Test: ECDSA cert */
329 {
330 struct s2n_config *server_config = NULL, *client_config = NULL;
331
332 struct s2n_cert_chain_and_key *chain_and_key = NULL;
333 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
334 S2N_ECDSA_P384_PKCS1_CERT_CHAIN, S2N_ECDSA_P384_PKCS1_KEY));
335
336 EXPECT_NOT_NULL(server_config = s2n_config_new());
337 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config, "test_all_ecdsa"));
338 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
339 EXPECT_SUCCESS(s2n_config_add_dhparams(server_config, dhparams_pem));
340 /* Enable signature validation */
341 EXPECT_SUCCESS(s2n_config_set_verify_after_sign(server_config, S2N_VERIFY_AFTER_SIGN_ENABLED));
342 if (test_type == TEST_TYPE_ASYNC) {
343 EXPECT_SUCCESS(s2n_config_set_async_pkey_callback(server_config, async_pkey_fn));
344 }
345
346 EXPECT_NOT_NULL(client_config = s2n_config_new());
347 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "test_all_ecdsa"));
348 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(client_config));
349
350 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(client_config, S2N_ECDSA_P384_PKCS1_CERT_CHAIN, NULL));
351
352 EXPECT_SUCCESS(test_cipher_preferences(server_config, client_config,
353 chain_and_key, S2N_SIGNATURE_ECDSA));
354
355 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
356 EXPECT_SUCCESS(s2n_config_free(server_config));
357 EXPECT_SUCCESS(s2n_config_free(client_config));
358 };
359
360 /* Test: RSA cert with RSA PSS signatures */
361 if (s2n_is_rsa_pss_signing_supported()) {
362 const struct s2n_signature_scheme *const rsa_pss_rsae_sig_schemes[] = {
363 /* RSA PSS */
364 &s2n_rsa_pss_rsae_sha256,
365 &s2n_rsa_pss_rsae_sha384,
366 &s2n_rsa_pss_rsae_sha512,
367 };
368
369 struct s2n_signature_preferences sig_prefs = {
370 .count = 3,
371 .signature_schemes = rsa_pss_rsae_sig_schemes,
372 };
373
374 struct s2n_config *server_config = NULL, *client_config = NULL;
375
376 struct s2n_cert_chain_and_key *chain_and_key = NULL;
377 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
378 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
379
380 EXPECT_NOT_NULL(server_config = s2n_config_new());
381 /* We need a security policy that only supports RSA certificates for auth */
382 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config, "20170210"));
383
384 struct s2n_security_policy security_policy = {
385 .minimum_protocol_version = server_config->security_policy->minimum_protocol_version,
386 .cipher_preferences = server_config->security_policy->cipher_preferences,
387 .kem_preferences = server_config->security_policy->kem_preferences,
388 .signature_preferences = &sig_prefs,
389 .ecc_preferences = server_config->security_policy->ecc_preferences,
390 };
391
392 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
393 EXPECT_SUCCESS(s2n_config_add_dhparams(server_config, dhparams_pem));
394 if (test_type == TEST_TYPE_ASYNC) {
395 EXPECT_SUCCESS(s2n_config_set_async_pkey_callback(server_config, async_pkey_fn));
396 }
397 server_config->security_policy = &security_policy;
398
399 EXPECT_NOT_NULL(client_config = s2n_config_new());
400 client_config->check_ocsp = 0;
401 client_config->disable_x509_validation = 1;
402 client_config->security_policy = &security_policy;
403
404 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(client_config, S2N_DEFAULT_TEST_CERT_CHAIN, NULL));
405
406 EXPECT_SUCCESS(test_cipher_preferences(server_config, client_config,
407 chain_and_key, S2N_SIGNATURE_RSA_PSS_RSAE));
408
409 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
410 EXPECT_SUCCESS(s2n_config_free(server_config));
411 EXPECT_SUCCESS(s2n_config_free(client_config));
412 }
413
414 /* Test: RSA_PSS cert with RSA_PSS signatures */
415 if (s2n_is_rsa_pss_certs_supported()) {
416 s2n_enable_tls13_in_test();
417
418 struct s2n_config *server_config = NULL, *client_config = NULL;
419
420 struct s2n_cert_chain_and_key *chain_and_key = NULL;
421 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
422 S2N_RSA_PSS_2048_SHA256_LEAF_CERT, S2N_RSA_PSS_2048_SHA256_LEAF_KEY));
423
424 EXPECT_NOT_NULL(server_config = s2n_config_new());
425 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config, "20200207"));
426 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
427 /* Enable signature validation */
428 EXPECT_SUCCESS(s2n_config_set_verify_after_sign(server_config, S2N_VERIFY_AFTER_SIGN_ENABLED));
429 if (test_type == TEST_TYPE_ASYNC) {
430 EXPECT_SUCCESS(s2n_config_set_async_pkey_callback(server_config, async_pkey_fn));
431 }
432
433 EXPECT_NOT_NULL(client_config = s2n_config_new());
434 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "20200207"));
435 client_config->check_ocsp = 0;
436 client_config->disable_x509_validation = 1;
437
438 EXPECT_SUCCESS(test_cipher_preferences(server_config, client_config,
439 chain_and_key, S2N_SIGNATURE_RSA_PSS_PSS));
440
441 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
442 EXPECT_SUCCESS(s2n_config_free(server_config));
443 EXPECT_SUCCESS(s2n_config_free(client_config));
444
445 s2n_disable_tls13_in_test();
446 }
447
448 s2n_reset_tls13_in_test();
449
450 /* Test: ML-DSA cert */
451 if (s2n_mldsa_is_supported()) {
452 for (size_t verify = 0; verify <= 1; verify++) {
453 for (size_t client_auth = 0; client_auth <= 1; client_auth++) {
454 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL,
455 s2n_cert_chain_and_key_ptr_free);
456 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
457 S2N_MLDSA87_CERT, S2N_MLDSA87_KEY));
458
459 DEFER_CLEANUP(struct s2n_config *server_config = s2n_config_new(),
460 s2n_config_ptr_free);
461 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(
462 server_config, chain_and_key));
463 EXPECT_SUCCESS(s2n_config_set_verify_host_callback(server_config,
464 s2n_trust_all_verify_host, NULL));
465 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(server_config,
466 S2N_MLDSA87_CERT, NULL));
467
468 DEFER_CLEANUP(struct s2n_config *client_config = s2n_config_new(),
469 s2n_config_ptr_free);
470 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(
471 client_config, chain_and_key));
472 EXPECT_SUCCESS(s2n_config_set_verify_host_callback(client_config,
473 s2n_trust_all_verify_host, NULL));
474 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(client_config,
475 S2N_MLDSA87_CERT, NULL));
476
477 /* ML-DSA is only usable with TLS1.3 */
478 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config,
479 "test_all_tls13"));
480 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config,
481 "test_all_tls13"));
482
483 /* The hashing path for verify_after_sign is subtly different.
484 * Make sure we test both paths.
485 */
486 if (verify) {
487 EXPECT_SUCCESS(s2n_config_set_verify_after_sign(server_config,
488 S2N_VERIFY_AFTER_SIGN_ENABLED));
489 }
490
491 /* Test both the async and sync paths */
492 if (test_type == TEST_TYPE_ASYNC) {
493 EXPECT_SUCCESS(s2n_config_set_async_pkey_callback(
494 server_config, async_pkey_fn));
495 }
496
497 /* Also test the client side use of ML-DSA */
498 if (client_auth) {
499 EXPECT_SUCCESS(s2n_config_set_client_auth_type(server_config,
500 S2N_CERT_AUTH_REQUIRED));
501 EXPECT_SUCCESS(s2n_config_set_client_auth_type(client_config,
502 S2N_CERT_AUTH_REQUIRED));
503 }
504
505 EXPECT_SUCCESS(test_cipher_preferences(server_config, client_config,
506 chain_and_key, S2N_SIGNATURE_MLDSA));
507 }
508 }
509 }
510 }
511
512 /* Ensure that a handshake can be performed after all file descriptors are closed */
513 {
514 /* A fork is created to ensure that closing file descriptors (like stdout) won't impact
515 * other tests.
516 */
517 pid_t pid = fork();
518 if (pid == 0) {
519 long max_file_descriptors = sysconf(_SC_OPEN_MAX);
520 for (long fd = 0; fd < max_file_descriptors; fd++) {
521 EXPECT_TRUE(fd <= INT_MAX);
522 close((int) fd);
523 }
524
525 /* use a nested scope to force the DEFER_CLEANUP statements to
526 * execute before the exit() call.
527 */
528 {
529 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL, s2n_cert_chain_and_key_ptr_free);
530 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
531 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
532 EXPECT_NOT_NULL(chain_and_key);
533
534 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
535 EXPECT_NOT_NULL(config);
536 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "20170210"));
537 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
538 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(config, S2N_DEFAULT_TEST_CERT_CHAIN, NULL));
539 EXPECT_SUCCESS(s2n_config_disable_x509_verification(config));
540
541 EXPECT_SUCCESS(test_cipher_preferences(config, config, chain_and_key, S2N_SIGNATURE_RSA));
542 }
543
544 exit(EXIT_SUCCESS);
545 }
546
547 int status = 0;
548 EXPECT_EQUAL(waitpid(pid, &status, 0), pid);
549 EXPECT_EQUAL(status, EXIT_SUCCESS);
550 }
551
552 END_TEST();
553 }