Debian Codegraph @

crt/s2n/tests/unit/s2n_config_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_config.h"
17
18 #include <stdlib.h>
19 #include <testlib/s2n_sslv2_client_hello.h>
20
21 #include "api/s2n.h"
22 #include "crypto/s2n_fips.h"
23 #include "crypto/s2n_libcrypto.h"
24 #include "crypto/s2n_openssl.h"
25 #include "crypto/s2n_pq.h"
26 #include "s2n_test.h"
27 #include "testlib/s2n_testlib.h"
28 #include "tls/extensions/s2n_client_supported_groups.h"
29 #include "tls/s2n_connection.h"
30 #include "tls/s2n_internal.h"
31 #include "tls/s2n_record.h"
32 #include "tls/s2n_security_policies.h"
33 #include "tls/s2n_tls.h"
34 #include "tls/s2n_tls13.h"
35 #include "unstable/npn.h"
36 #include "utils/s2n_map.h"
37 #include "utils/s2n_mem.h"
38
39 #define S2N_TEST_MAX_SUPPORTED_GROUPS_COUNT 30
40
41 /* forward declaration */
42 int s2n_config_build_domain_name_to_cert_map(struct s2n_config *config, struct s2n_cert_chain_and_key *cert_key_pair);
43
44 static int s2n_test_select_psk_identity_callback(struct s2n_connection *conn, void *context,
45 struct s2n_offered_psk_list *psk_identity_list)
46 {
47 return S2N_SUCCESS;
48 }
49
50 static int s2n_test_reneg_req_cb(struct s2n_connection *conn, void *context, s2n_renegotiate_response *response)
51 {
52 return S2N_SUCCESS;
53 }
54
55 static int s2n_test_crl_lookup_cb(struct s2n_crl_lookup *lookup, void *context)
56 {
57 return S2N_SUCCESS;
58 }
59
60 static int s2n_test_cert_validation_cb(struct s2n_connection *conn, struct s2n_cert_validation_info *info, void *context)
61 {
62 return S2N_SUCCESS;
63 }
64
65 static int s2n_test_async_pkey_fn(struct s2n_connection *conn, struct s2n_async_pkey_op *op)
66 {
67 return S2N_SUCCESS;
68 }
69
70 /* A malloc callback that always fails. Used by the regression test
71 * to force s2n_map_add to fail during cert map construction.
72 */
73 static int s2n_test_failing_malloc_cb(void **ptr, uint32_t requested, uint32_t *allocated)
74 {
75 *ptr = NULL;
76 *allocated = 0;
77 POSIX_BAIL(S2N_ERR_ALLOC);
78 }
79
80 int main(int argc, char **argv)
81 {
82 BEGIN_TEST();
83 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
84
85 const s2n_mode modes[] = { S2N_CLIENT, S2N_SERVER };
86
87 const struct s2n_security_policy *default_security_policy = NULL, *tls13_security_policy = NULL, *fips_security_policy = NULL;
88 EXPECT_SUCCESS(s2n_find_security_policy_from_version("default_tls13", &tls13_security_policy));
89 EXPECT_SUCCESS(s2n_find_security_policy_from_version("default_fips", &fips_security_policy));
90 EXPECT_SUCCESS(s2n_find_security_policy_from_version("default", &default_security_policy));
91
92 char cert[S2N_MAX_TEST_PEM_SIZE] = { 0 };
93 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_CERT_CHAIN, cert, S2N_MAX_TEST_PEM_SIZE));
94 char key[S2N_MAX_TEST_PEM_SIZE] = { 0 };
95 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_PRIVATE_KEY, key, S2N_MAX_TEST_PEM_SIZE));
96
97 /* Test: s2n_config_new and tls13_default_config match */
98 {
99 struct s2n_config *config = NULL, *default_config = NULL;
100
101 EXPECT_NOT_NULL(config = s2n_config_new());
102 EXPECT_NOT_NULL(default_config = s2n_fetch_default_config());
103
104 /* s2n_config_new() matches s2n_fetch_default_config() */
105 if (default_config->security_policy != config->security_policy) {
106 /* one possible cause for this is attempting to includes s2n_config.c
107 * for access to internal `static` functions. This causes two copies
108 * of the default config to be created. The default_config in *this*
109 * unit of translation doesn't get properly initialized. */
110 const char *default_policy = NULL;
111 const char *new_policy = NULL;
112 EXPECT_OK(s2n_security_policy_get_version(default_config->security_policy, &default_policy));
113 EXPECT_OK(s2n_security_policy_get_version(config->security_policy, &new_policy));
114 printf("default policy is %s but new policy is %s\n", default_policy, new_policy);
115 }
116 EXPECT_EQUAL(default_config->security_policy, config->security_policy);
117 EXPECT_EQUAL(default_config->security_policy->signature_preferences, config->security_policy->signature_preferences);
118 EXPECT_EQUAL(default_config->client_cert_auth_type, config->client_cert_auth_type);
119
120 /* Calling s2n_fetch_default_config() repeatedly returns the same object */
121 EXPECT_EQUAL(default_config, s2n_fetch_default_config());
122
123 /* TLS1.3 default does not match non-TLS1.3 default */
124 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
125 EXPECT_NOT_EQUAL(default_config, s2n_fetch_default_config());
126 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
127
128 EXPECT_SUCCESS(s2n_config_free(config));
129 };
130
131 /* Connections created with default configs */
132 {
133 /* For TLS1.2 */
134 if (!s2n_is_in_fips_mode()) {
135 struct s2n_connection *conn = NULL;
136 const struct s2n_security_policy *security_policy = NULL;
137 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
138
139 EXPECT_EQUAL(conn->config, s2n_fetch_default_config());
140
141 EXPECT_SUCCESS(s2n_connection_get_security_policy(conn, &security_policy));
142 EXPECT_EQUAL(security_policy, default_security_policy);
143
144 EXPECT_SUCCESS(s2n_connection_free(conn));
145 }
146
147 /* For TLS1.3 */
148 {
149 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
150 struct s2n_connection *conn = NULL;
151 const struct s2n_security_policy *security_policy = NULL;
152 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
153
154 EXPECT_EQUAL(conn->config, s2n_fetch_default_config());
155
156 EXPECT_SUCCESS(s2n_connection_get_security_policy(conn, &security_policy));
157 EXPECT_EQUAL(security_policy, tls13_security_policy);
158
159 EXPECT_SUCCESS(s2n_connection_free(conn));
160 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
161 };
162
163 /* For fips */
164 if (s2n_is_in_fips_mode()) {
165 struct s2n_connection *conn = NULL;
166 const struct s2n_security_policy *security_policy = NULL;
167 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
168
169 EXPECT_EQUAL(conn->config, s2n_fetch_default_config());
170
171 EXPECT_SUCCESS(s2n_connection_get_security_policy(conn, &security_policy));
172 EXPECT_EQUAL(security_policy, fips_security_policy);
173
174 EXPECT_SUCCESS(s2n_connection_free(conn));
175 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
176 }
177 };
178
179 /* Test for s2n_config_new() and tls 1.3 behavior */
180 {
181 if (!s2n_is_in_fips_mode()) {
182 struct s2n_config *config = NULL;
183 EXPECT_NOT_NULL(config = s2n_config_new());
184 EXPECT_EQUAL(config->security_policy, default_security_policy);
185 EXPECT_SUCCESS(s2n_config_free(config));
186
187 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
188 EXPECT_NOT_NULL(config = s2n_config_new());
189 EXPECT_EQUAL(config->security_policy, tls13_security_policy);
190 EXPECT_SUCCESS(s2n_config_free(config));
191 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
192 }
193 };
194
195 /* Test setting the callback to select PSK identity */
196 {
197 struct s2n_config *config = NULL;
198 EXPECT_NOT_NULL(config = s2n_config_new());
199 uint8_t context = 13;
200
201 /* Safety check */
202 EXPECT_FAILURE_WITH_ERRNO(s2n_config_set_psk_selection_callback(
203 NULL, s2n_test_select_psk_identity_callback, &context),
204 S2N_ERR_NULL);
205 EXPECT_NULL(config->psk_selection_cb);
206 EXPECT_NULL(config->psk_selection_ctx);
207
208 EXPECT_SUCCESS(s2n_config_set_psk_selection_callback(config, s2n_test_select_psk_identity_callback, &context));
209 EXPECT_EQUAL(config->psk_selection_cb, s2n_test_select_psk_identity_callback);
210 EXPECT_EQUAL(config->psk_selection_ctx, &context);
211
212 EXPECT_SUCCESS(s2n_config_set_psk_selection_callback(config, NULL, NULL));
213 EXPECT_NULL(config->psk_selection_cb);
214 EXPECT_NULL(config->psk_selection_ctx);
215
216 EXPECT_SUCCESS(s2n_config_free(config));
217 };
218
219 /*Test s2n_connection_set_config */
220 {
221 /* Test that tickets_to_send is set correctly */
222 {
223 struct s2n_connection *conn = NULL;
224 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
225
226 struct s2n_config *config = NULL;
227 uint8_t num_tickets = 1;
228
229 EXPECT_NOT_NULL(config = s2n_config_new());
230
231 config->initial_tickets_to_send = num_tickets;
232
233 EXPECT_EQUAL(conn->tickets_to_send, 0);
234 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
235 EXPECT_EQUAL(conn->tickets_to_send, num_tickets);
236
237 EXPECT_SUCCESS(s2n_config_free(config));
238 EXPECT_SUCCESS(s2n_connection_free(conn));
239 };
240
241 /* Test that PSK type is set correctly */
242 {
243 struct s2n_config *config = s2n_config_new();
244 EXPECT_NOT_NULL(config);
245
246 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
247 EXPECT_NOT_NULL(conn);
248 EXPECT_EQUAL(config->psk_mode, S2N_PSK_MODE_RESUMPTION);
249
250 /* Overrides connection value */
251 {
252 conn->config = NULL;
253 conn->psk_params.type = S2N_PSK_TYPE_EXTERNAL;
254 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
255 EXPECT_EQUAL(conn->psk_params.type, S2N_PSK_TYPE_RESUMPTION);
256 EXPECT_FALSE(conn->psk_mode_overridden);
257 };
258
259 /* Does not override connection value if conn->override_psk_mode set */
260 {
261 conn->config = NULL;
262 conn->psk_params.type = S2N_PSK_TYPE_EXTERNAL;
263 conn->psk_mode_overridden = true;
264 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
265 EXPECT_EQUAL(conn->psk_params.type, S2N_PSK_TYPE_EXTERNAL);
266 EXPECT_TRUE(conn->psk_mode_overridden);
267 conn->psk_mode_overridden = false;
268 };
269
270 /* Does not override connection value if PSKs already set */
271 {
272 conn->config = NULL;
273 DEFER_CLEANUP(struct s2n_psk *test_external_psk = s2n_test_psk_new(conn), s2n_psk_free);
274 EXPECT_SUCCESS(s2n_connection_append_psk(conn, test_external_psk));
275 EXPECT_EQUAL(conn->psk_params.type, S2N_PSK_TYPE_EXTERNAL);
276 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
277 EXPECT_EQUAL(conn->psk_params.type, S2N_PSK_TYPE_EXTERNAL);
278 EXPECT_FALSE(conn->psk_mode_overridden);
279 };
280
281 EXPECT_SUCCESS(s2n_connection_free(conn));
282 EXPECT_SUCCESS(s2n_config_free(config));
283 };
284 };
285
286 /* s2n_config_set_session_tickets_onoff */
287 {
288 /* Safety */
289 EXPECT_FAILURE_WITH_ERRNO(s2n_config_set_session_tickets_onoff(NULL, true), S2N_ERR_NULL);
290 EXPECT_FAILURE_WITH_ERRNO(s2n_config_set_session_tickets_onoff(NULL, false), S2N_ERR_NULL);
291
292 struct s2n_config *config = s2n_config_new();
293 EXPECT_NOT_NULL(config);
294
295 EXPECT_SUCCESS(s2n_config_set_session_tickets_onoff(config, true));
296 EXPECT_TRUE(config->use_tickets);
297 EXPECT_EQUAL(config->initial_tickets_to_send, 1);
298
299 EXPECT_SUCCESS(s2n_config_set_session_tickets_onoff(config, false));
300 EXPECT_FALSE(config->use_tickets);
301 EXPECT_EQUAL(config->initial_tickets_to_send, 1);
302
303 config->initial_tickets_to_send = 10;
304 EXPECT_SUCCESS(s2n_config_set_session_tickets_onoff(config, true));
305 EXPECT_TRUE(config->use_tickets);
306 EXPECT_EQUAL(config->initial_tickets_to_send, 10);
307
308 EXPECT_SUCCESS(s2n_config_free(config));
309 };
310
311 /* s2n_config_set_context */
312 /* s2n_config_get_context */
313 {
314 uint8_t context = 42;
315 uint8_t other = 123;
316 void *returned_context = NULL;
317
318 struct s2n_config *config = s2n_config_new();
319 EXPECT_NOT_NULL(config);
320
321 EXPECT_SUCCESS(s2n_config_get_ctx(config, &returned_context));
322 EXPECT_NULL(returned_context);
323
324 EXPECT_SUCCESS(s2n_config_set_ctx(config, &context));
325 EXPECT_SUCCESS(s2n_config_get_ctx(config, &returned_context));
326 EXPECT_NOT_NULL(returned_context);
327
328 EXPECT_EQUAL(*((uint8_t *) returned_context), context);
329 EXPECT_NOT_EQUAL(*((uint8_t *) returned_context), other);
330
331 EXPECT_SUCCESS(s2n_config_free(config));
332 };
333
334 /* Test s2n_config_set_extension_data */
335 {
336 uint8_t extension_data[] = "extension data";
337
338 /* Test s2n_config_set_extension_data can be called for owned cert chains */
339 {
340 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
341 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key(config, cert, key));
342 EXPECT_NOT_NULL(s2n_config_get_single_default_cert(config));
343
344 EXPECT_SUCCESS(s2n_config_set_extension_data(config, S2N_EXTENSION_OCSP_STAPLING,
345 extension_data, sizeof(extension_data)));
346 EXPECT_EQUAL(s2n_config_get_single_default_cert(config)->ocsp_status.size, sizeof(extension_data));
347 };
348
349 /* Test s2n_config_set_extension_data can't be called for unowned cert chains */
350 {
351 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain = NULL,
352 s2n_cert_chain_and_key_ptr_free);
353 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain,
354 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
355
356 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
357 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain));
358 EXPECT_NOT_NULL(s2n_config_get_single_default_cert(config));
359
360 EXPECT_FAILURE_WITH_ERRNO(s2n_config_set_extension_data(config, S2N_EXTENSION_OCSP_STAPLING,
361 extension_data, sizeof(extension_data)),
362 S2N_ERR_CERT_OWNERSHIP);
363 EXPECT_EQUAL(s2n_config_get_single_default_cert(config)->ocsp_status.size, 0);
364 EXPECT_EQUAL(chain->ocsp_status.size, 0);
365 };
366 };
367
368 /* Test s2n_config_free_cert_chain_and_key */
369 {
370 /* Chain owned by application */
371 {
372 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain = NULL, s2n_cert_chain_and_key_ptr_free);
373 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain,
374 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
375
376 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
377 EXPECT_NOT_NULL(config);
378 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain));
379 EXPECT_NOT_NULL(s2n_config_get_single_default_cert(config));
380 EXPECT_EQUAL(config->cert_ownership, S2N_APP_OWNED);
381
382 /* No-op for application-owned chains */
383 EXPECT_SUCCESS(s2n_config_free_cert_chain_and_key(config));
384 EXPECT_NOT_NULL(s2n_config_get_single_default_cert(config));
385 EXPECT_EQUAL(config->cert_ownership, S2N_APP_OWNED);
386
387 /* Still no-op if called again */
388 EXPECT_SUCCESS(s2n_config_free_cert_chain_and_key(config));
389 EXPECT_NOT_NULL(s2n_config_get_single_default_cert(config));
390 };
391
392 /* Chain owned by application and freed too early:
393 * This is arguably incorrect behavior, but did not cause errors in the past.
394 * We should continue to ensure it doesn't cause any errors.
395 */
396 {
397 struct s2n_cert_chain_and_key *chain = NULL;
398 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain,
399 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
400
401 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
402 EXPECT_NOT_NULL(config);
403 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain));
404 EXPECT_NOT_NULL(s2n_config_get_single_default_cert(config));
405 EXPECT_EQUAL(config->cert_ownership, S2N_APP_OWNED);
406
407 /* Free the chain early */
408 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain));
409
410 /* No-op for application-owned chains */
411 EXPECT_SUCCESS(s2n_config_free_cert_chain_and_key(config));
412 EXPECT_NOT_NULL(s2n_config_get_single_default_cert(config));
413 EXPECT_EQUAL(config->cert_ownership, S2N_APP_OWNED);
414
415 /* No-op if called again */
416 EXPECT_SUCCESS(s2n_config_free_cert_chain_and_key(config));
417 EXPECT_NOT_NULL(s2n_config_get_single_default_cert(config));
418 };
419
420 /* Chain owned by library */
421 {
422 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
423 EXPECT_NOT_NULL(config);
424 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key(config, cert, key));
425 EXPECT_NOT_NULL(s2n_config_get_single_default_cert(config));
426 EXPECT_EQUAL(config->cert_ownership, S2N_LIB_OWNED);
427
428 EXPECT_SUCCESS(s2n_config_free_cert_chain_and_key(config));
429 EXPECT_NULL(s2n_config_get_single_default_cert(config));
430 EXPECT_EQUAL(config->cert_ownership, S2N_NOT_OWNED);
431
432 /* No-op if called again */
433 EXPECT_SUCCESS(s2n_config_free_cert_chain_and_key(config));
434 EXPECT_NULL(s2n_config_get_single_default_cert(config));
435 };
436
437 /* Switch from library-owned certs to application-owned certs */
438 {
439 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain = NULL, s2n_cert_chain_and_key_ptr_free);
440 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain,
441 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
442
443 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
444 EXPECT_NOT_NULL(config);
445 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key(config, cert, key));
446 EXPECT_NOT_NULL(s2n_config_get_single_default_cert(config));
447 EXPECT_EQUAL(config->cert_ownership, S2N_LIB_OWNED);
448
449 EXPECT_SUCCESS(s2n_config_free_cert_chain_and_key(config));
450 EXPECT_NULL(s2n_config_get_single_default_cert(config));
451 EXPECT_EQUAL(config->cert_ownership, S2N_NOT_OWNED);
452
453 /* Now add an application-owned chain */
454 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain));
455 EXPECT_SUCCESS(s2n_config_free_cert_chain_and_key(config));
456 };
457 };
458
459 /* Test: a failed second call to s2n_config_add_cert_chain_and_key does not
460 * leave dangling pointers in the domain_name_to_cert_map.
461 */
462 {
463 /* The second call is rejected before any state is mutated */
464 {
465 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
466 EXPECT_NOT_NULL(config);
467
468 /* First call succeeds and populates the map */
469 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key(config, cert, key));
470 EXPECT_EQUAL(config->cert_ownership, S2N_LIB_OWNED);
471
472 uint32_t map_size_after_first = 0;
473 EXPECT_OK(s2n_map_size(config->domain_name_to_cert_map, &map_size_after_first));
474
475 /* Second call with the same cert type fails at the ownership guard */
476 EXPECT_FAILURE_WITH_ERRNO(s2n_config_add_cert_chain_and_key(config, cert, key),
477 S2N_ERR_CERT_OWNERSHIP);
478
479 /* The map must be unchanged — no new entries were inserted */
480 uint32_t map_size_after_second = 0;
481 EXPECT_OK(s2n_map_size(config->domain_name_to_cert_map, &map_size_after_second));
482 EXPECT_EQUAL(map_size_after_first, map_size_after_second);
483
484 /* The original default cert is still valid and reachable */
485 EXPECT_NOT_NULL(s2n_config_get_single_default_cert(config));
486 };
487
488 /* s2n_config_add_cert_chain_and_key still allows adding two certs of the
489 * same type. The map entry is updated in-place (no new entries).
490 */
491 {
492 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_a = NULL, s2n_cert_chain_and_key_ptr_free);
493 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_a,
494 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
495 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_b = NULL, s2n_cert_chain_and_key_ptr_free);
496 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_b,
497 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
498
499 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
500 EXPECT_NOT_NULL(config);
501
502 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_a));
503
504 uint32_t map_size_after_first = 0;
505 EXPECT_OK(s2n_map_size(config->domain_name_to_cert_map, &map_size_after_first));
506
507 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_b));
508
509 uint32_t map_size_after_second = 0;
510 EXPECT_OK(s2n_map_size(config->domain_name_to_cert_map, &map_size_after_second));
511 EXPECT_EQUAL(map_size_after_first, map_size_after_second);
512 };
513 };
514
515 /* Test s2n_config_set_cert_chain_and_key_defaults */
516 {
517 /* Succeeds if chains owned by app */
518 {
519 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_1 = NULL, s2n_cert_chain_and_key_ptr_free);
520 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_1,
521 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
522
523 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_2 = NULL, s2n_cert_chain_and_key_ptr_free);
524 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_2,
525 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
526
527 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
528 EXPECT_NOT_NULL(config);
529 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_1));
530 EXPECT_EQUAL(s2n_config_get_single_default_cert(config), chain_1);
531 EXPECT_EQUAL(config->cert_ownership, S2N_APP_OWNED);
532
533 EXPECT_SUCCESS(s2n_config_set_cert_chain_and_key_defaults(config, &chain_2, 1));
534 EXPECT_EQUAL(s2n_config_get_single_default_cert(config), chain_2);
535 EXPECT_EQUAL(config->cert_ownership, S2N_APP_OWNED);
536 };
537
538 /* Fails if chains owned by library */
539 {
540 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain = NULL, s2n_cert_chain_and_key_ptr_free);
541 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain,
542 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
543
544 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
545 EXPECT_NOT_NULL(config);
546 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key(config, cert, key));
547 EXPECT_NOT_NULL(s2n_config_get_single_default_cert(config));
548 EXPECT_EQUAL(config->cert_ownership, S2N_LIB_OWNED);
549
550 EXPECT_FAILURE_WITH_ERRNO(s2n_config_set_cert_chain_and_key_defaults(
551 config, &chain, 1),
552 S2N_ERR_CERT_OWNERSHIP);
553 };
554 };
555
556 /* Test s2n_config_set_send_buffer_size */
557 {
558 /* Safety */
559 {
560 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
561 EXPECT_NOT_NULL(config);
562
563 EXPECT_EQUAL(config->send_buffer_size_override, 0);
564 EXPECT_FAILURE_WITH_ERRNO(s2n_config_set_send_buffer_size(NULL, S2N_MIN_SEND_BUFFER_SIZE), S2N_ERR_NULL);
565 EXPECT_FAILURE_WITH_ERRNO(s2n_config_set_send_buffer_size(config, 0), S2N_ERR_INVALID_ARGUMENT);
566 EXPECT_EQUAL(config->send_buffer_size_override, 0);
567 };
568
569 /* Default applied to connection */
570 {
571 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
572 EXPECT_NOT_NULL(config);
573
574 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
575 EXPECT_NOT_NULL(conn);
576 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
577
578 EXPECT_EQUAL(config->send_buffer_size_override, 0);
579 EXPECT_FALSE(conn->multirecord_send);
580 };
581
582 /* Custom applied to connection */
583 {
584 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
585 EXPECT_NOT_NULL(config);
586 EXPECT_SUCCESS(s2n_config_set_send_buffer_size(config, S2N_MIN_SEND_BUFFER_SIZE));
587
588 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
589 EXPECT_NOT_NULL(conn);
590 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
591
592 EXPECT_EQUAL(config->send_buffer_size_override, S2N_MIN_SEND_BUFFER_SIZE);
593 EXPECT_TRUE(conn->multirecord_send);
594 };
595 };
596
597 /* Test s2n_config_set_verify_after_sign */
598 {
599 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
600 EXPECT_NOT_NULL(config);
601 EXPECT_FALSE(config->verify_after_sign);
602
603 /* Safety */
604 EXPECT_FAILURE_WITH_ERRNO(s2n_config_set_verify_after_sign(NULL, S2N_VERIFY_AFTER_SIGN_ENABLED), S2N_ERR_NULL);
605
606 /* Invalid mode */
607 config->verify_after_sign = true;
608 EXPECT_FAILURE_WITH_ERRNO(s2n_config_set_verify_after_sign(config, UINT8_MAX), S2N_ERR_INVALID_ARGUMENT);
609 EXPECT_TRUE(config->verify_after_sign);
610 config->verify_after_sign = false;
611 EXPECT_FAILURE_WITH_ERRNO(s2n_config_set_verify_after_sign(config, UINT8_MAX), S2N_ERR_INVALID_ARGUMENT);
612 EXPECT_FALSE(config->verify_after_sign);
613
614 /* Set and unset */
615 EXPECT_SUCCESS(s2n_config_set_verify_after_sign(config, S2N_VERIFY_AFTER_SIGN_ENABLED));
616 EXPECT_TRUE(config->verify_after_sign);
617 EXPECT_SUCCESS(s2n_config_set_verify_after_sign(config, S2N_VERIFY_AFTER_SIGN_DISABLED));
618 EXPECT_FALSE(config->verify_after_sign);
619 };
620
621 /* Test s2n_config_set_renegotiate_request_cb */
622 {
623 uint8_t context = 0;
624 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
625 EXPECT_NOT_NULL(config);
626
627 /* Unset by default */
628 EXPECT_EQUAL(config->renegotiate_request_cb, NULL);
629 EXPECT_EQUAL(config->renegotiate_request_ctx, NULL);
630
631 /* Safety */
632 EXPECT_FAILURE_WITH_ERRNO(s2n_config_set_renegotiate_request_cb(NULL, s2n_test_reneg_req_cb, &context), S2N_ERR_NULL);
633 EXPECT_SUCCESS(s2n_config_set_renegotiate_request_cb(config, NULL, &context));
634 EXPECT_SUCCESS(s2n_config_set_renegotiate_request_cb(config, s2n_test_reneg_req_cb, NULL));
635
636 /* Set */
637 EXPECT_SUCCESS(s2n_config_set_renegotiate_request_cb(config, s2n_test_reneg_req_cb, &context));
638 EXPECT_EQUAL(config->renegotiate_request_cb, s2n_test_reneg_req_cb);
639 EXPECT_EQUAL(config->renegotiate_request_ctx, &context);
640
641 /* Unset */
642 EXPECT_SUCCESS(s2n_config_set_renegotiate_request_cb(config, NULL, NULL));
643 EXPECT_EQUAL(config->renegotiate_request_cb, NULL);
644 EXPECT_EQUAL(config->renegotiate_request_ctx, NULL);
645 };
646
647 /* Test s2n_config_set_npn */
648 {
649 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
650 EXPECT_NOT_NULL(config);
651 EXPECT_FALSE(config->npn_supported);
652
653 /* Safety */
654 EXPECT_FAILURE_WITH_ERRNO(s2n_config_set_npn(NULL, true), S2N_ERR_NULL);
655
656 /* Set and unset */
657 EXPECT_SUCCESS(s2n_config_set_npn(config, true));
658 EXPECT_TRUE(config->npn_supported);
659 EXPECT_SUCCESS(s2n_config_set_npn(config, false));
660 EXPECT_FALSE(config->npn_supported);
661 };
662
663 /* Test s2n_config_set_crl_lookup_cb */
664 {
665 uint8_t context = 0;
666 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
667 EXPECT_NOT_NULL(config);
668
669 /* Unset by default */
670 EXPECT_EQUAL(config->crl_lookup_cb, NULL);
671 EXPECT_EQUAL(config->crl_lookup_ctx, NULL);
672
673 /* Safety */
674 EXPECT_FAILURE_WITH_ERRNO(s2n_config_set_crl_lookup_cb(NULL, s2n_test_crl_lookup_cb, &context), S2N_ERR_NULL);
675 EXPECT_SUCCESS(s2n_config_set_crl_lookup_cb(config, NULL, &context));
676 EXPECT_SUCCESS(s2n_config_set_crl_lookup_cb(config, s2n_test_crl_lookup_cb, NULL));
677
678 /* Set */
679 EXPECT_SUCCESS(s2n_config_set_crl_lookup_cb(config, s2n_test_crl_lookup_cb, &context));
680 EXPECT_EQUAL(config->crl_lookup_cb, s2n_test_crl_lookup_cb);
681 EXPECT_EQUAL(config->crl_lookup_ctx, &context);
682
683 /* Unset */
684 EXPECT_SUCCESS(s2n_config_set_crl_lookup_cb(config, NULL, NULL));
685 EXPECT_EQUAL(config->crl_lookup_cb, NULL);
686 EXPECT_EQUAL(config->crl_lookup_ctx, NULL);
687 };
688
689 /* Test s2n_config_set_cert_validation_cb */
690 {
691 uint8_t context = 0;
692 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
693 EXPECT_NOT_NULL(config);
694
695 /* Unset by default */
696 EXPECT_EQUAL(config->cert_validation_cb, NULL);
697 EXPECT_EQUAL(config->cert_validation_ctx, NULL);
698
699 /* Safety */
700 EXPECT_FAILURE_WITH_ERRNO(s2n_config_set_cert_validation_cb(NULL, s2n_test_cert_validation_cb, &context),
701 S2N_ERR_NULL);
702 EXPECT_SUCCESS(s2n_config_set_cert_validation_cb(config, NULL, &context));
703 EXPECT_SUCCESS(s2n_config_set_cert_validation_cb(config, s2n_test_cert_validation_cb, NULL));
704
705 /* Set */
706 EXPECT_SUCCESS(s2n_config_set_cert_validation_cb(config, s2n_test_cert_validation_cb, &context));
707 EXPECT_EQUAL(config->cert_validation_cb, s2n_test_cert_validation_cb);
708 EXPECT_EQUAL(config->cert_validation_ctx, &context);
709
710 /* Unset */
711 EXPECT_SUCCESS(s2n_config_set_cert_validation_cb(config, NULL, NULL));
712 EXPECT_EQUAL(config->cert_validation_cb, NULL);
713 EXPECT_EQUAL(config->cert_validation_ctx, NULL);
714 };
715
716 /* Test s2n_config_set_status_request_type */
717 for (size_t mode_i = 0; mode_i < s2n_array_len(modes); mode_i++) {
718 s2n_mode mode = modes[mode_i];
719
720 if (!s2n_x509_ocsp_stapling_supported()) {
721 break;
722 }
723
724 /* request_ocsp_status should be false by default */
725 {
726 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
727 EXPECT_NOT_NULL(config);
728 EXPECT_FALSE(config->ocsp_status_requested_by_user);
729 EXPECT_FALSE(config->ocsp_status_requested_by_s2n);
730
731 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(mode), s2n_connection_ptr_free);
732 EXPECT_NOT_NULL(conn);
733 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
734
735 EXPECT_FALSE(conn->request_ocsp_status);
736 };
737
738 /* request_ocsp_status should be true if set via s2n_config_set_status_request_type */
739 {
740 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
741 EXPECT_NOT_NULL(config);
742 EXPECT_SUCCESS(s2n_config_set_status_request_type(config, S2N_STATUS_REQUEST_OCSP));
743 EXPECT_TRUE(config->ocsp_status_requested_by_user);
744 EXPECT_FALSE(config->ocsp_status_requested_by_s2n);
745
746 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(mode), s2n_connection_ptr_free);
747 EXPECT_NOT_NULL(conn);
748 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
749
750 EXPECT_TRUE(conn->request_ocsp_status);
751 };
752
753 /* ocsp_status_requested_by_s2n can be set in s2n_config_set_verification_ca_location. For
754 * backwards compatibility, this should tell clients to request OCSP stapling. However, this
755 * API should not tell servers to request OCSP stapling.
756 */
757 for (int api_configuration_i = 0; api_configuration_i < 3; api_configuration_i++) {
758 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
759 EXPECT_NOT_NULL(config);
760
761 switch (api_configuration_i) {
762 case 0:
763 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(config, S2N_DEFAULT_TEST_CERT_CHAIN, NULL));
764 break;
765 case 1:
766 /* If a user intentionally disables OCSP stapling, s2n_config_set_verification_ca_location
767 * should not re-enable it for servers.
768 */
769 EXPECT_SUCCESS(s2n_config_set_status_request_type(config, S2N_STATUS_REQUEST_NONE));
770 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(config, S2N_DEFAULT_TEST_CERT_CHAIN, NULL));
771 break;
772 default:
773 EXPECT_SUCCESS(s2n_config_set_status_request_type(config, S2N_STATUS_REQUEST_OCSP));
774 EXPECT_SUCCESS(s2n_config_set_status_request_type(config, S2N_STATUS_REQUEST_NONE));
775 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(config, S2N_DEFAULT_TEST_CERT_CHAIN, NULL));
776 break;
777 }
778
779 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(mode), s2n_connection_ptr_free);
780 EXPECT_NOT_NULL(conn);
781 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
782
783 if (mode == S2N_CLIENT) {
784 EXPECT_TRUE(conn->request_ocsp_status);
785 } else {
786 EXPECT_FALSE(conn->request_ocsp_status);
787 }
788 };
789
790 /* Calling s2n_config_set_status_request_type with S2N_STATUS_REQUEST_OCSP should enable OCSP
791 * status requests, regardless of s2n_config_set_verification_ca_location.
792 */
793 {
794 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
795 EXPECT_NOT_NULL(config);
796
797 EXPECT_SUCCESS(s2n_config_set_status_request_type(config, S2N_STATUS_REQUEST_OCSP));
798 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(config, S2N_DEFAULT_TEST_CERT_CHAIN, NULL));
799
800 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(mode), s2n_connection_ptr_free);
801 EXPECT_NOT_NULL(conn);
802 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
803
804 EXPECT_TRUE(conn->request_ocsp_status);
805 };
806
807 /* Calling s2n_config_set_status_request_type with S2N_STATUS_REQUEST_NONE should disable OCSP
808 * status requests, regardless of s2n_config_set_verification_ca_location.
809 */
810 {
811 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
812 EXPECT_NOT_NULL(config);
813
814 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(config, S2N_DEFAULT_TEST_CERT_CHAIN, NULL));
815 EXPECT_SUCCESS(s2n_config_set_status_request_type(config, S2N_STATUS_REQUEST_NONE));
816
817 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(mode), s2n_connection_ptr_free);
818 EXPECT_NOT_NULL(conn);
819 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
820
821 EXPECT_FALSE(conn->request_ocsp_status);
822 };
823 };
824
825 /* Test s2n_config_add_cert_chain */
826 {
827 uint32_t pem_len = 0;
828 uint8_t pem_bytes[S2N_MAX_TEST_PEM_SIZE] = { 0 };
829 EXPECT_SUCCESS(s2n_read_test_pem_and_len(S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN,
830 pem_bytes, &pem_len, sizeof(pem_bytes)));
831
832 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
833 EXPECT_SUCCESS(s2n_config_add_cert_chain(config, pem_bytes, pem_len));
834 EXPECT_TRUE(config->no_signing_key);
835 EXPECT_EQUAL(config->cert_ownership, S2N_LIB_OWNED);
836
837 struct s2n_cert_chain_and_key *chain = s2n_config_get_single_default_cert(config);
838 POSIX_ENSURE_REF(chain);
839 EXPECT_FAILURE(s2n_pkey_check_key_exists(chain->private_key));
840
841 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
842 s2n_connection_ptr_free);
843 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_set_config(conn, config), S2N_ERR_NO_PRIVATE_KEY);
844 EXPECT_SUCCESS(s2n_config_set_async_pkey_callback(config, s2n_test_async_pkey_fn));
845 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
846 };
847
848 /* Test loading system certs */
849 {
850 /* s2n_config_load_system_certs safety */
851 {
852 EXPECT_FAILURE_WITH_ERRNO(s2n_config_load_system_certs(NULL), S2N_ERR_NULL);
853 }
854
855 /* s2n_config_new_minimal should not load system certs */
856 {
857 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new_minimal(), s2n_config_ptr_free);
858 EXPECT_NOT_NULL(config);
859 EXPECT_NULL(config->trust_store.trust_store);
860 EXPECT_FALSE(config->trust_store.loaded_system_certs);
861
862 /* System certs can be loaded onto the minimal config */
863 EXPECT_SUCCESS(s2n_config_load_system_certs(config));
864 EXPECT_NOT_NULL(config->trust_store.trust_store);
865 EXPECT_TRUE(config->trust_store.loaded_system_certs);
866
867 /* Attempting to load system certs multiple times on the same config should error */
868 for (int i = 0; i < 20; i++) {
869 EXPECT_FAILURE_WITH_ERRNO(s2n_config_load_system_certs(config), S2N_ERR_X509_TRUST_STORE);
870 EXPECT_TRUE(config->trust_store.loaded_system_certs);
871 }
872 }
873
874 /* s2n_config_new should load system certs */
875 {
876 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
877 EXPECT_NOT_NULL(config);
878 EXPECT_NOT_NULL(config->trust_store.trust_store);
879 EXPECT_TRUE(config->trust_store.loaded_system_certs);
880
881 /* Attempting to load system certs multiple times on the same config should error */
882 for (int i = 0; i < 20; i++) {
883 EXPECT_FAILURE_WITH_ERRNO(s2n_config_load_system_certs(config), S2N_ERR_X509_TRUST_STORE);
884 EXPECT_TRUE(config->trust_store.loaded_system_certs);
885 }
886 }
887
888 /* The default config should load system certs */
889 {
890 struct s2n_config *config = s2n_fetch_default_config();
891 EXPECT_NOT_NULL(config);
892 EXPECT_TRUE(config->trust_store.loaded_system_certs);
893 }
894
895 /* System certs can be loaded again after wiping the trust store */
896 {
897 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
898 EXPECT_NOT_NULL(config);
899
900 for (int i = 0; i < 20; i++) {
901 /* System certs were already loaded, so an attempt to load them should fail */
902 EXPECT_NOT_NULL(config->trust_store.trust_store);
903 EXPECT_TRUE(config->trust_store.loaded_system_certs);
904 EXPECT_FAILURE_WITH_ERRNO(s2n_config_load_system_certs(config), S2N_ERR_X509_TRUST_STORE);
905
906 EXPECT_SUCCESS(s2n_config_wipe_trust_store(config));
907
908 /* The trust store is cleared after a wipe, so it should be possible to load system certs again */
909 EXPECT_FALSE(config->trust_store.loaded_system_certs);
910 EXPECT_SUCCESS(s2n_config_load_system_certs(config));
911 EXPECT_TRUE(config->trust_store.loaded_system_certs);
912 }
913 }
914
915 /* Ensure that system certs are properly loaded into the X509_STORE.
916 *
917 * The API used to retrieve the contents of an X509_STORE, X509_STORE_get0_objects,
918 * wasn't added until OpenSSL 1.1.0.
919 */
920 #if S2N_OPENSSL_VERSION_AT_LEAST(1, 1, 0)
921 {
922 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new_minimal(), s2n_config_ptr_free);
923 EXPECT_NOT_NULL(config);
924 EXPECT_NULL(config->trust_store.trust_store);
925 EXPECT_FALSE(config->trust_store.loaded_system_certs);
926
927 /* Initialize the X509_STORE by adding a cert */
928 EXPECT_SUCCESS(s2n_x509_trust_store_from_ca_file(&config->trust_store, S2N_RSA_PSS_2048_SHA256_CA_CERT, NULL));
929 EXPECT_NOT_NULL(config->trust_store.trust_store);
930 EXPECT_FALSE(config->trust_store.loaded_system_certs);
931
932 /* The X509_STORE should only contain the single cert that was added. */
933 STACK_OF(X509_OBJECT) *x509_store_contents = X509_STORE_get0_objects(config->trust_store.trust_store);
934 EXPECT_NOT_NULL(x509_store_contents);
935 int initial_contents_count = sk_X509_OBJECT_num(x509_store_contents);
936 EXPECT_EQUAL(initial_contents_count, 1);
937
938 /* Override the system cert file to guarantee that a system cert will be loaded */
939 EXPECT_SUCCESS(setenv("SSL_CERT_FILE", S2N_SHA1_ROOT_SIGNATURE_CA_CERT, 1));
940
941 /* Load the system cert into the store */
942 EXPECT_SUCCESS(s2n_config_load_system_certs(config));
943 EXPECT_TRUE(config->trust_store.loaded_system_certs);
944 int system_certs_contents_count = sk_X509_OBJECT_num(x509_store_contents);
945
946 /* LibreSSL doesn't use the SSL_CERT_FILE environment variable to set the system cert location,
947 * so we don't know how many system certs will be loaded, if any.
948 */
949 if (!s2n_libcrypto_is_libressl()) {
950 EXPECT_EQUAL(system_certs_contents_count, initial_contents_count + 1);
951 }
952
953 /* Additional calls to s2n_config_load_default_certs should not add additional certs to the store */
954 for (int i = 0; i < 20; i++) {
955 EXPECT_FAILURE_WITH_ERRNO(s2n_config_load_system_certs(config), S2N_ERR_X509_TRUST_STORE);
956 EXPECT_TRUE(config->trust_store.loaded_system_certs);
957 int additional_call_contents_count = sk_X509_OBJECT_num(x509_store_contents);
958 EXPECT_TRUE(additional_call_contents_count == system_certs_contents_count);
959 }
960
961 EXPECT_SUCCESS(unsetenv("SSL_CERT_FILE"));
962 }
963 #endif
964
965 /* Self-talk tests */
966 {
967 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL,
968 s2n_cert_chain_and_key_ptr_free);
969 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
970 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
971
972 /* Ensure a handshake succeeds with a minimal server config and no mutual auth */
973 {
974 DEFER_CLEANUP(struct s2n_config *server_config = s2n_config_new_minimal(), s2n_config_ptr_free);
975 EXPECT_NOT_NULL(server_config);
976 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config, "default"));
977 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
978
979 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
980 s2n_connection_ptr_free);
981 EXPECT_NOT_NULL(server_conn);
982 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
983
984 DEFER_CLEANUP(struct s2n_config *client_config = s2n_config_new_minimal(), s2n_config_ptr_free);
985 EXPECT_NOT_NULL(client_config);
986 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "default"));
987 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(client_config, S2N_DEFAULT_TEST_CERT_CHAIN, NULL));
988
989 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
990 s2n_connection_ptr_free);
991 EXPECT_NOT_NULL(client_conn);
992 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
993 EXPECT_SUCCESS(s2n_set_server_name(client_conn, "localhost"));
994
995 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
996 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
997 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
998
999 EXPECT_FALSE(server_config->trust_store.loaded_system_certs);
1000 EXPECT_NULL(server_config->trust_store.trust_store);
1001
1002 EXPECT_FALSE(client_config->trust_store.loaded_system_certs);
1003 EXPECT_NOT_NULL(client_config->trust_store.trust_store);
1004
1005 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
1006 }
1007
1008 /* Ensure a handshake fails gracefully with an uninitialized trust store */
1009 {
1010 DEFER_CLEANUP(struct s2n_config *server_config = s2n_config_new_minimal(), s2n_config_ptr_free);
1011 EXPECT_NOT_NULL(server_config);
1012 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config, "default"));
1013 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
1014
1015 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
1016 s2n_connection_ptr_free);
1017 EXPECT_NOT_NULL(server_conn);
1018 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
1019
1020 DEFER_CLEANUP(struct s2n_config *client_config = s2n_config_new_minimal(), s2n_config_ptr_free);
1021 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "default"));
1022 EXPECT_NOT_NULL(client_config);
1023
1024 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
1025 s2n_connection_ptr_free);
1026 EXPECT_NOT_NULL(client_conn);
1027 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
1028 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
1029
1030 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
1031 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
1032 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
1033
1034 EXPECT_FALSE(server_config->trust_store.loaded_system_certs);
1035 EXPECT_NULL(server_config->trust_store.trust_store);
1036
1037 EXPECT_FALSE(client_config->trust_store.loaded_system_certs);
1038 EXPECT_NULL(client_config->trust_store.trust_store);
1039
1040 /* The client should fail to validate the server's certificate without an initialized trust store */
1041 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, client_conn),
1042 S2N_ERR_CERT_UNTRUSTED);
1043 }
1044 }
1045 }
1046
1047 /* s2n_config_disable_x509_time_verification tests */
1048 {
1049 /* Safety */
1050 EXPECT_FAILURE_WITH_ERRNO(s2n_config_disable_x509_time_verification(NULL), S2N_ERR_NULL);
1051
1052 /* Ensure s2n_config_disable_x509_time_verification sets the proper state */
1053 {
1054 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
1055 EXPECT_NOT_NULL(config);
1056 EXPECT_EQUAL(config->disable_x509_time_validation, false);
1057
1058 EXPECT_SUCCESS(s2n_config_disable_x509_time_verification(config));
1059 EXPECT_EQUAL(config->disable_x509_time_validation, true);
1060 }
1061 }
1062
1063 /* Test s2n_config_get_supported_groups */
1064 {
1065 /* Safety */
1066 {
1067 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
1068 EXPECT_NOT_NULL(config);
1069 uint16_t supported_groups[S2N_TEST_MAX_SUPPORTED_GROUPS_COUNT] = { 0 };
1070 uint16_t supported_groups_count = 0;
1071
1072 int ret = s2n_config_get_supported_groups(NULL, supported_groups, s2n_array_len(supported_groups),
1073 &supported_groups_count);
1074 EXPECT_FAILURE_WITH_ERRNO(ret, S2N_ERR_NULL);
1075 EXPECT_EQUAL(supported_groups_count, 0);
1076
1077 ret = s2n_config_get_supported_groups(config, NULL, s2n_array_len(supported_groups), &supported_groups_count);
1078 EXPECT_FAILURE_WITH_ERRNO(ret, S2N_ERR_NULL);
1079 EXPECT_EQUAL(supported_groups_count, 0);
1080
1081 ret = s2n_config_get_supported_groups(config, supported_groups, s2n_array_len(supported_groups), NULL);
1082 EXPECT_FAILURE_WITH_ERRNO(ret, S2N_ERR_NULL);
1083 EXPECT_EQUAL(supported_groups_count, 0);
1084 }
1085
1086 /* Error if the provided supported groups array is too small */
1087 {
1088 /* Test a policy with and without PQ kem groups */
1089 const char *policies[] = {
1090 "20170210",
1091 "PQ-TLS-1-2-2024-10-07",
1092 };
1093
1094 for (size_t i = 0; i < s2n_array_len(policies); i++) {
1095 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
1096 EXPECT_NOT_NULL(config);
1097 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, policies[i]));
1098
1099 uint32_t policy_groups_count = 0;
1100 EXPECT_OK(s2n_kem_preferences_groups_available(config->security_policy->kem_preferences,
1101 &policy_groups_count));
1102 policy_groups_count += config->security_policy->ecc_preferences->count;
1103 EXPECT_TRUE(policy_groups_count > 0);
1104
1105 uint16_t supported_groups[S2N_TEST_MAX_SUPPORTED_GROUPS_COUNT] = { 0 };
1106 uint16_t supported_groups_count = 11;
1107 for (size_t invalid_count = 0; invalid_count < policy_groups_count; invalid_count++) {
1108 int ret = s2n_config_get_supported_groups(config, supported_groups, invalid_count,
1109 &supported_groups_count);
1110 EXPECT_FAILURE_WITH_ERRNO(ret, S2N_ERR_INSUFFICIENT_MEM_SIZE);
1111 EXPECT_EQUAL(supported_groups_count, 0);
1112 }
1113
1114 EXPECT_SUCCESS(s2n_config_get_supported_groups(config, supported_groups, policy_groups_count,
1115 &supported_groups_count));
1116 EXPECT_EQUAL(supported_groups_count, policy_groups_count);
1117 }
1118 }
1119
1120 /* The groups produced by s2n_config_get_supported_groups should match the groups produced
1121 * by a connection that's configured to send its entire list of supported groups
1122 */
1123 for (size_t policy_idx = 0; security_policy_selection[policy_idx].version != NULL; policy_idx++) {
1124 const struct s2n_security_policy *security_policy = security_policy_selection[policy_idx].security_policy;
1125 EXPECT_NOT_NULL(security_policy);
1126
1127 uint32_t expected_groups_count = 0;
1128 EXPECT_OK(s2n_kem_preferences_groups_available(security_policy->kem_preferences, &expected_groups_count));
1129 expected_groups_count += security_policy->ecc_preferences->count;
1130
1131 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
1132 EXPECT_NOT_NULL(config);
1133 config->security_policy = security_policy;
1134
1135 uint16_t supported_groups[S2N_TEST_MAX_SUPPORTED_GROUPS_COUNT] = { 0 };
1136 uint16_t supported_groups_count = 0;
1137 EXPECT_SUCCESS(s2n_config_get_supported_groups(config, supported_groups, s2n_array_len(supported_groups),
1138 &supported_groups_count));
1139 EXPECT_EQUAL(supported_groups_count, expected_groups_count);
1140
1141 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
1142 EXPECT_NOT_NULL(conn);
1143 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
1144 /* PQ kem groups aren't sent in the supported groups extension before TLS 1.3. */
1145 conn->actual_protocol_version = S2N_TLS13;
1146
1147 DEFER_CLEANUP(struct s2n_stuffer extension_stuffer = { 0 }, s2n_stuffer_free);
1148 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&extension_stuffer, 0));
1149 EXPECT_SUCCESS(s2n_client_supported_groups_extension.send(conn, &extension_stuffer));
1150
1151 uint16_t extension_groups_count = 0;
1152 EXPECT_OK(s2n_supported_groups_parse_count(&extension_stuffer, &extension_groups_count));
1153 EXPECT_EQUAL(extension_groups_count, expected_groups_count);
1154
1155 for (size_t i = 0; i < supported_groups_count; i++) {
1156 /* s2n_stuffer_read_uint16 is used to read each of the supported groups in
1157 * network-order endianness.
1158 */
1159 uint16_t group_iana = 0;
1160 POSIX_GUARD(s2n_stuffer_read_uint16(&extension_stuffer, &group_iana));
1161
1162 EXPECT_EQUAL(group_iana, supported_groups[i]);
1163 }
1164 }
1165 }
1166
1167 /* Test s2n_config_validate_loaded_certificates */
1168 {
1169 DEFER_CLEANUP(struct s2n_cert_chain_and_key *invalid_cert = NULL, s2n_cert_chain_and_key_ptr_free);
1170 DEFER_CLEANUP(struct s2n_cert_chain_and_key *valid_cert = NULL, s2n_cert_chain_and_key_ptr_free);
1171 EXPECT_SUCCESS(s2n_test_cert_permutation_load_server_chain(&invalid_cert, "ec", "ecdsa", "p384", "sha256"));
1172 EXPECT_SUCCESS(s2n_test_cert_permutation_load_server_chain(&valid_cert, "ec", "ecdsa", "p384", "sha384"));
1173
1174 struct s2n_security_policy rfc9151_applied_locally = security_policy_20250429;
1175 rfc9151_applied_locally.certificate_preferences_apply_locally = true;
1176
1177 /* rfc9151 doesn't allow SHA256 signatures, but does allow SHA384 signatures,
1178 * so ecdsa_p384_sha256 is invalid and ecdsa_p384_sha384 is valid */
1179
1180 /* valid certs are accepted */
1181 {
1182 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
1183 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, valid_cert));
1184 EXPECT_OK(s2n_config_validate_loaded_certificates(config, &rfc9151_applied_locally));
1185 };
1186
1187 /* when cert preferences don't apply locally, invalid certs are accepted */
1188 {
1189 struct s2n_security_policy non_local_rfc9151 = security_policy_20250429;
1190 non_local_rfc9151.certificate_preferences_apply_locally = false;
1191
1192 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
1193 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, invalid_cert));
1194 EXPECT_OK(s2n_config_validate_loaded_certificates(config, &non_local_rfc9151));
1195 };
1196
1197 /* Certs in an s2n_config are stored in default_certs_by_type, domain_name_to_cert_map, or
1198 * both. We want to ensure that the s2n_config_validate_loaded_certificates method will
1199 * validate certs in both locations.
1200 */
1201
1202 /* certs in default_certs_by_type are validated */
1203 {
1204 /* s2n_config_set_cert_chain_and_key_defaults populates default_certs_by_type
1205 * but doesn't populate domain_name_to_cert_map
1206 */
1207 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new_minimal(), s2n_config_ptr_free);
1208 EXPECT_SUCCESS(s2n_config_set_cert_chain_and_key_defaults(config, &invalid_cert, 1));
1209
1210 /* domain certs is empty */
1211 uint32_t domain_certs_count = 0;
1212 EXPECT_OK(s2n_map_size(config->domain_name_to_cert_map, &domain_certs_count));
1213 EXPECT_EQUAL(domain_certs_count, 0);
1214
1215 /* certs in default_certs_by_type are validated */
1216 EXPECT_ERROR_WITH_ERRNO(s2n_config_validate_loaded_certificates(config, &rfc9151_applied_locally),
1217 S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT);
1218 };
1219
1220 /* certs in the domain map are validated */
1221 {
1222 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
1223 EXPECT_SUCCESS(s2n_config_build_domain_name_to_cert_map(config, invalid_cert));
1224
1225 /* default_certs_by_type is empty. */
1226 EXPECT_EQUAL(s2n_config_get_num_default_certs(config), 0);
1227
1228 /* certs in domain_map are validated */
1229 EXPECT_ERROR_WITH_ERRNO(s2n_config_validate_loaded_certificates(config, &rfc9151_applied_locally),
1230 S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT);
1231 };
1232
1233 /* when cert preferences don't apply locally, certs in domain map are not iterated over
1234 *
1235 * Some customers load large numbers of certificates, so even iterating
1236 * over every certificate without performing any validation is expensive.
1237 */
1238 {
1239 struct s2n_security_policy non_local_rfc9151 = security_policy_20250429;
1240
1241 /* Assert that the security policy WOULD apply,
1242 * if certificate_preferences_apply_locally was true.
1243 */
1244 EXPECT_NOT_NULL(non_local_rfc9151.certificate_key_preferences);
1245 EXPECT_NOT_NULL(non_local_rfc9151.certificate_signature_preferences);
1246 EXPECT_TRUE(non_local_rfc9151.certificate_key_preferences->count > 0);
1247 EXPECT_TRUE(non_local_rfc9151.certificate_signature_preferences->count > 0);
1248
1249 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
1250 EXPECT_NOT_NULL(config);
1251 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, valid_cert));
1252
1253 /* Invalidate the domain map so that attempting to use it will trigger
1254 * an error. We want to ensure that we DON'T use it.
1255 * Iterating over a map requires that map to be immutable / complete.
1256 */
1257 EXPECT_OK(s2n_map_unlock(config->domain_name_to_cert_map));
1258
1259 /* Control case: if local validation needed, attempt to use invalid domain map */
1260 non_local_rfc9151.certificate_preferences_apply_locally = true;
1261 EXPECT_ERROR_WITH_ERRNO(
1262 s2n_config_validate_loaded_certificates(config, &non_local_rfc9151),
1263 S2N_ERR_MAP_MUTABLE);
1264
1265 /* Test case: if no local validation needed, do not use invalid domain map */
1266 non_local_rfc9151.certificate_preferences_apply_locally = false;
1267 EXPECT_OK(s2n_config_validate_loaded_certificates(config, &non_local_rfc9151));
1268 };
1269 };
1270
1271 /* Checks that servers don't use a config before the client hello callback is executed.
1272 *
1273 * We want to assert that a config is never used by a server until the client hello callback
1274 * is called, given that users have the ability to swap out the config during this callback.
1275 */
1276 {
1277 DEFER_CLEANUP(struct s2n_config *tls12_client_config = s2n_config_new(), s2n_config_ptr_free);
1278 EXPECT_NOT_NULL(tls12_client_config);
1279 /* Security policy that only supports TLS12 */
1280 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(tls12_client_config, "20240501"));
1281
1282 DEFER_CLEANUP(struct s2n_config *tls13_client_config = s2n_config_new(), s2n_config_ptr_free);
1283 EXPECT_NOT_NULL(tls13_client_config);
1284 /* Security policy that supports TLS13 */
1285 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(tls13_client_config, "20240503"));
1286
1287 struct s2n_config *config_arr[] = { tls12_client_config, tls13_client_config };
1288
1289 /* Checks that the handshake gets as far as the client hello callback with a NULL config */
1290 for (size_t i = 0; i < s2n_array_len(config_arr); i++) {
1291 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
1292 s2n_connection_ptr_free);
1293 EXPECT_NOT_NULL(client_conn);
1294 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
1295 s2n_connection_ptr_free);
1296 EXPECT_NOT_NULL(server_conn);
1297
1298 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config_arr[i]));
1299
1300 /* Server config pointer is explicitly set to NULL */
1301 server_conn->config = NULL;
1302
1303 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair test_io = { 0 },
1304 s2n_io_stuffer_pair_free);
1305 EXPECT_OK(s2n_io_stuffer_pair_init(&test_io));
1306 EXPECT_OK(s2n_connections_set_io_stuffer_pair(client_conn, server_conn, &test_io));
1307
1308 /* S2N_ERR_CONFIG_NULL_BEFORE_CH_CALLBACK is only called in one location, just before the
1309 * client hello callback. Therefore, we can assert that if we hit this error, we
1310 * have gotten as far as the client hello callback without dereferencing the config.
1311 */
1312 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, client_conn),
1313 S2N_ERR_CONFIG_NULL_BEFORE_CH_CALLBACK);
1314 }
1315 }
1316
1317 /* Checks that servers don't use a config before the client hello callback is executed on a
1318 * SSLv2-formatted client hello.
1319 *
1320 * Parsing SSLv2 hellos uses a different code path and needs to be tested separately.
1321 */
1322 {
1323 uint8_t sslv2_client_hello[] = {
1324 SSLv2_CLIENT_HELLO_PREFIX,
1325 SSLv2_CLIENT_HELLO_CIPHER_SUITES,
1326 SSLv2_CLIENT_HELLO_CHALLENGE,
1327 };
1328
1329 struct s2n_blob client_hello = { 0 };
1330 EXPECT_SUCCESS(s2n_blob_init(&client_hello, sslv2_client_hello, sizeof(sslv2_client_hello)));
1331
1332 /* Checks that the handshake gets as far as the client hello callback with a NULL config */
1333 {
1334 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
1335 s2n_connection_ptr_free);
1336 EXPECT_NOT_NULL(server_conn);
1337 server_conn->config = NULL;
1338
1339 /* Record version and protocol version are in the header for SSLv2 */
1340 server_conn->client_hello.sslv2 = true;
1341 server_conn->client_hello.legacy_version = S2N_TLS12;
1342
1343 /* S2N_ERR_CONFIG_NULL_BEFORE_CH_CALLBACK is only called in one location, just before the
1344 * client hello callback. Therefore, we can assert that if we hit this error, we
1345 * have gotten as far as the client hello callback without dereferencing the config.
1346 */
1347 EXPECT_SUCCESS(s2n_stuffer_write(&server_conn->handshake.io, &client_hello));
1348 EXPECT_FAILURE_WITH_ERRNO(s2n_client_hello_recv(server_conn), S2N_ERR_CONFIG_NULL_BEFORE_CH_CALLBACK);
1349 }
1350 }
1351
1352 /* s2n_config_set_subscriber */
1353 {
1354 /* Safety */
1355 uint64_t fake_subscriber = 0;
1356 EXPECT_FAILURE_WITH_ERRNO(s2n_config_set_subscriber(NULL, (void *) &fake_subscriber), S2N_ERR_NULL);
1357 };
1358
1359 /* s2n_config_set_handshake_event */
1360 {
1361 /* Safety */
1362 uint64_t fake_callback = 0;
1363 EXPECT_FAILURE_WITH_ERRNO(s2n_config_set_subscriber(NULL, (s2n_event_on_handshake_cb) &fake_callback), S2N_ERR_NULL);
1364 };
1365
1366 /* Test: domain_name_to_cert_map remains usable after s2n_map_add fails
1367 *
1368 * Regression test: s2n_config_update_domain_name_to_cert_map
1369 * calls s2n_map_unlock -> s2n_map_add -> s2n_map_complete. If s2n_map_add
1370 * fails (e.g. OOM), the map was left in a mutable (!immutable) state.
1371 * All subsequent s2n_map_lookup calls, used on every ClientHello for
1372 * SNI-based cert selection, would then fail with S2N_ERR_MAP_MUTABLE,
1373 * silently disabling SNI matching for the config's lifetime.
1374 *
1375 * The fix ensures s2n_map_complete is always called after unlock,
1376 * even when add fails.
1377 */
1378 {
1379 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
1380 EXPECT_NOT_NULL(config);
1381
1382 /* The map starts empty and immutable. Verify a lookup succeeds. */
1383 {
1384 struct s2n_blob lookup_name = { 0 };
1385 EXPECT_SUCCESS(s2n_blob_init(&lookup_name,
1386 (uint8_t *) "test.example.com", strlen("test.example.com")));
1387 struct s2n_blob lookup_value = { 0 };
1388 bool found = false;
1389 EXPECT_OK(s2n_map_lookup(config->domain_name_to_cert_map,
1390 &lookup_name, &lookup_value, &found));
1391 EXPECT_FALSE(found);
1392 }
1393
1394 /* Create a cert chain to attempt adding. */
1395 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain = NULL,
1396 s2n_cert_chain_and_key_ptr_free);
1397 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain,
1398 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
1399
1400 /* Install a malloc callback that always fails so s2n_map_add will
1401 * fail during s2n_dup inside the unlock -> add -> complete path.
1402 */
1403 s2n_mem_init_callback saved_init_cb = NULL;
1404 s2n_mem_cleanup_callback saved_cleanup_cb = NULL;
1405 s2n_mem_malloc_callback saved_malloc_cb = NULL;
1406 s2n_mem_free_callback saved_free_cb = NULL;
1407 EXPECT_OK(s2n_mem_get_callbacks(&saved_init_cb, &saved_cleanup_cb,
1408 &saved_malloc_cb, &saved_free_cb));
1409 EXPECT_OK(s2n_mem_override_callbacks(saved_init_cb, saved_cleanup_cb,
1410 s2n_test_failing_malloc_cb, saved_free_cb));
1411
1412 /* The cert's SANs/CNs are not in the empty map, so the update
1413 * function will take the !key_found branch: unlock -> add (FAIL).
1414 * With the fix, complete is still called before returning the error.
1415 */
1416 EXPECT_FAILURE(s2n_config_build_domain_name_to_cert_map(config, chain));
1417
1418 /* Restore the real malloc callback before any further allocations. */
1419 EXPECT_OK(s2n_mem_override_callbacks(saved_init_cb, saved_cleanup_cb,
1420 saved_malloc_cb, saved_free_cb));
1421
1422 /* The regression check: the map must still be immutable so that
1423 * lookups succeed. Before the fix, this would fail with
1424 * S2N_ERR_MAP_MUTABLE because s2n_map_complete was never called
1425 * after the failed add.
1426 */
1427 {
1428 struct s2n_blob lookup_name = { 0 };
1429 EXPECT_SUCCESS(s2n_blob_init(&lookup_name,
1430 (uint8_t *) "test.example.com", strlen("test.example.com")));
1431 struct s2n_blob lookup_value = { 0 };
1432 bool found = false;
1433 EXPECT_OK(s2n_map_lookup(config->domain_name_to_cert_map,
1434 &lookup_name, &lookup_value, &found));
1435 /* Lookup should succeed (not error) even though the key isn't found. */
1436 }
1437 };
1438
1439 END_TEST();
1440 }