Debian Codegraph @

crt/s2n/tests/unit/s2n_mutual_auth_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 <openssl/pem.h>
17 #include <openssl/x509.h>
18 #include <stdlib.h>
19
20 #include "api/s2n.h"
21 #include "api/unstable/cert_authorities.h"
22 #include "crypto/s2n_fips.h"
23 #include "s2n_test.h"
24 #include "testlib/s2n_testlib.h"
25 #include "tls/extensions/s2n_cert_authorities.h"
26 #include "tls/s2n_cipher_suites.h"
27 #include "tls/s2n_connection.h"
28 #include "tls/s2n_handshake.h"
29 #include "tls/s2n_security_policies.h"
30 #include "utils/s2n_safety.h"
31
32 DEFINE_POINTER_CLEANUP_FUNC(BIO *, BIO_free);
33 DEFINE_POINTER_CLEANUP_FUNC(X509_NAME *, X509_NAME_free);
34
35 struct host_verify_data {
36 uint8_t callback_invoked;
37 uint8_t allow;
38 };
39
40 static uint8_t verify_host_fn(const char *host_name, size_t host_name_len, void *data)
41 {
42 struct host_verify_data *verify_data = (struct host_verify_data *) data;
43 verify_data->callback_invoked = 1;
44 return verify_data->allow;
45 }
46
47 /**
48 * Pretty prints a DER-encoded Distinguished Name to an s2n_blob
49 *
50 * @param der_dn The DER-encoded Distinguished Name input
51 * @param der_dn_len Length of the DER-encoded Distinguished Name
52 * @param output_blob Pointer to an s2n_blob that will contain the pretty-printed output
53 */
54 S2N_RESULT pretty_print_dn(const uint8_t *der_dn, size_t der_dn_len, struct s2n_blob *output_blob)
55 {
56 RESULT_ENSURE_REF(der_dn);
57 RESULT_ENSURE_REF(output_blob);
58
59 DEFER_CLEANUP(BIO *bio = BIO_new(BIO_s_mem()), BIO_free_pointer);
60 RESULT_ENSURE_REF(bio);
61
62 /* Parse the DER-encoded DN into an X509_NAME structure */
63 DEFER_CLEANUP(X509_NAME *name = d2i_X509_NAME(NULL, &der_dn, der_dn_len), X509_NAME_free_pointer);
64 RESULT_ENSURE_REF(name);
65
66 /* Pretty print the X509_NAME to the BIO */
67 X509_NAME_print_ex(bio, name, 0, XN_FLAG_ONELINE);
68
69 BUF_MEM *bptr;
70 BIO_get_mem_ptr(bio, &bptr);
71
72 RESULT_GUARD_POSIX(s2n_realloc(output_blob, bptr->length));
73 RESULT_CHECKED_MEMCPY(output_blob->data, bptr->data, bptr->length);
74
75 return S2N_RESULT_OK;
76 }
77
78 static int cert_req_cb(struct s2n_connection *conn, void *ctx_in, struct s2n_certificate_request *req)
79 {
80 EXPECT_NOT_NULL(conn);
81 EXPECT_NOT_NULL(ctx_in);
82 EXPECT_NOT_NULL(req);
83
84 struct s2n_cert_chain_and_key **ctx = ctx_in;
85
86 struct s2n_certificate_authority_list *list = s2n_certificate_request_get_ca_list(req);
87 EXPECT_NOT_NULL(list);
88
89 uint8_t *name = NULL;
90 uint16_t length = 0;
91
92 if (s2n_cert_authorities_supported_from_trust_store()) {
93 /* Confirm the list is re-readable (by reading it 3 times) */
94 for (int i = 0; i < 3; i++) {
95 const char *expected_subjects[] = {
96 "C = US, CN = leaf",
97 "C = US, CN = branch",
98 "C = US, CN = root",
99 };
100
101 EXPECT_FAILURE_WITH_ERRNO(s2n_certificate_authority_list_next(NULL, &name, &length), S2N_ERR_INVALID_ARGUMENT);
102 EXPECT_FAILURE_WITH_ERRNO(s2n_certificate_authority_list_next(list, NULL, &length), S2N_ERR_INVALID_ARGUMENT);
103 EXPECT_FAILURE_WITH_ERRNO(s2n_certificate_authority_list_next(list, &name, NULL), S2N_ERR_INVALID_ARGUMENT);
104 EXPECT_FAILURE_WITH_ERRNO(s2n_certificate_authority_list_reread(NULL), S2N_ERR_INVALID_ARGUMENT);
105
106 for (int j = 0; j < s2n_array_len(expected_subjects); j++) {
107 EXPECT_TRUE(s2n_certificate_authority_list_has_next(list));
108 EXPECT_SUCCESS(s2n_certificate_authority_list_next(list, &name, &length));
109
110 DEFER_CLEANUP(struct s2n_blob printed = { 0 }, s2n_free);
111 POSIX_GUARD_RESULT(pretty_print_dn(name, length, &printed));
112
113 const char *expected = expected_subjects[j];
114 EXPECT_EQUAL(strlen(expected), printed.size);
115 EXPECT_EQUAL(memcmp(expected, printed.data, printed.size), 0);
116 }
117
118 EXPECT_FALSE(s2n_certificate_authority_list_has_next(list));
119 EXPECT_FAILURE_WITH_ERRNO(s2n_certificate_authority_list_next(list, &name, &length), S2N_ERR_INVALID_ARGUMENT);
120 EXPECT_SUCCESS(s2n_certificate_authority_list_reread(list));
121 }
122 } else {
123 /* If certificate authorities weren't set, then there shouldn't be anything in the list. */
124 EXPECT_FALSE(s2n_certificate_authority_list_has_next(list));
125 EXPECT_SUCCESS(s2n_certificate_authority_list_reread(list));
126 EXPECT_FALSE(s2n_certificate_authority_list_has_next(list));
127 }
128
129 /* We set to null to test failure in which case setting will fail. */
130 if (*ctx != NULL) {
131 EXPECT_SUCCESS(s2n_certificate_request_set_certificate(req, *ctx));
132 } else {
133 EXPECT_FAILURE_WITH_ERRNO(s2n_certificate_request_set_certificate(req, *ctx),
134 S2N_ERR_INVALID_ARGUMENT);
135 }
136
137 return 0;
138 }
139
140 static bool s2n_should_skip_cipher(struct s2n_cipher_suite *suite)
141 {
142 if (!suite->available) {
143 /* Skip Ciphers that aren't supported with the linked libcrypto */
144 return true;
145 }
146
147 /* Skip Ciphers that aren't supported with the certificate chain we use in this test */
148 if (suite->auth_method == S2N_AUTHENTICATION_ECDSA) {
149 return true;
150 }
151
152 if (suite->minimum_required_tls_version == S2N_TLS13 && !s2n_is_tls13_fully_supported()) {
153 return true;
154 }
155
156 return false;
157 }
158
159 int main(int argc, char **argv)
160 {
161 struct s2n_config *config = NULL;
162 const struct s2n_security_policy *default_security_policy = NULL;
163 const struct s2n_cipher_preferences *default_cipher_preferences = NULL;
164 char *cert_chain_pem = NULL;
165 char *private_key_pem = NULL;
166 char *dhparams_pem = NULL;
167 struct s2n_cert_chain_and_key *chain_and_key = NULL;
168
169 BEGIN_TEST();
170
171 EXPECT_NOT_NULL(cert_chain_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
172 EXPECT_NOT_NULL(private_key_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
173 EXPECT_NOT_NULL(dhparams_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
174
175 /*
176 * Test Mutual Auth using **s2n_connection_set_client_auth_type**
177 */
178
179 EXPECT_NOT_NULL(config = s2n_config_new_minimal());
180 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_CERT_CHAIN, cert_chain_pem, S2N_MAX_TEST_PEM_SIZE));
181 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_PRIVATE_KEY, private_key_pem, S2N_MAX_TEST_PEM_SIZE));
182 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_DHPARAMS, dhparams_pem, S2N_MAX_TEST_PEM_SIZE));
183 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13"));
184
185 EXPECT_NOT_NULL(chain_and_key = s2n_cert_chain_and_key_new());
186 EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(chain_and_key, cert_chain_pem, private_key_pem));
187 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
188
189 EXPECT_SUCCESS(s2n_config_add_dhparams(config, dhparams_pem));
190 EXPECT_NOT_NULL(default_security_policy = config->security_policy);
191 EXPECT_NOT_NULL(default_cipher_preferences = default_security_policy->cipher_preferences);
192
193 struct host_verify_data verify_data = { .allow = 1, .callback_invoked = 0 };
194 EXPECT_SUCCESS(s2n_config_set_verify_host_callback(config, verify_host_fn, &verify_data));
195 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(config, S2N_DEFAULT_TEST_CERT_CHAIN, NULL));
196
197 struct s2n_cert_chain_and_key *cb_chain = NULL;
198
199 /* If this isn't supported we can't really test the end-to-end flow. */
200 if (s2n_cert_authorities_supported_from_trust_store()) {
201 EXPECT_SUCCESS(s2n_config_set_cert_authorities_from_trust_store(config));
202 }
203
204 EXPECT_FAILURE_WITH_ERRNO(s2n_config_set_cert_request_callback(NULL, cert_req_cb, (void *) &cb_chain),
205 S2N_ERR_INVALID_ARGUMENT);
206
207 /**
208 * Test with 3 variants:
209 *
210 * * Callback not set
211 * * Callback set, returns NULL
212 * * Callback set, returns a valid cert chain
213 */
214 for (int cert_req_callback_mode = 0; cert_req_callback_mode < 3; cert_req_callback_mode++) {
215 if (cert_req_callback_mode != 0) {
216 EXPECT_SUCCESS(s2n_config_set_cert_request_callback(config, cert_req_cb, (void *) &cb_chain));
217 }
218
219 bool saw_tls12 = false;
220 bool saw_tls13 = false;
221 const bool expected_success = cert_req_callback_mode != 1;
222
223 /* Verify that a handshake succeeds for every cipher in the default list. */
224 for (size_t cipher_idx = 0; cipher_idx < default_cipher_preferences->count; cipher_idx++) {
225 verify_data.callback_invoked = 0;
226 struct s2n_cipher_preferences server_cipher_preferences;
227 struct s2n_security_policy server_security_policy;
228 struct s2n_connection *client_conn = NULL;
229 struct s2n_connection *server_conn = NULL;
230 struct s2n_stuffer client_to_server = { 0 };
231 struct s2n_stuffer server_to_client = { 0 };
232
233 /* Craft a cipher preference with a cipher_idx cipher */
234 EXPECT_MEMCPY_SUCCESS(&server_cipher_preferences, default_cipher_preferences, sizeof(server_cipher_preferences));
235 server_cipher_preferences.count = 1;
236 struct s2n_cipher_suite *cur_cipher = default_cipher_preferences->suites[cipher_idx];
237
238 if (s2n_should_skip_cipher(cur_cipher)) {
239 continue;
240 }
241
242 server_cipher_preferences.suites = &cur_cipher;
243
244 EXPECT_MEMCPY_SUCCESS(&server_security_policy, default_security_policy, sizeof(server_security_policy));
245 server_security_policy.cipher_preferences = &server_cipher_preferences;
246
247 config->security_policy = &server_security_policy;
248
249 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
250 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(server_conn, S2N_CERT_AUTH_REQUIRED));
251 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
252 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
253
254 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
255 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(client_conn, S2N_CERT_AUTH_REQUIRED));
256 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
257 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
258
259 /* Set up our I/O callbacks. Use stuffers for the "I/O context" */
260 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&client_to_server, 0));
261 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&server_to_client, 0));
262 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&server_to_client, &client_to_server, client_conn));
263 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&client_to_server, &server_to_client, server_conn));
264
265 if (cert_req_callback_mode == 0) {
266 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
267 } else if (cert_req_callback_mode == 1) {
268 cb_chain = NULL;
269 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, client_conn), S2N_ERR_NO_CERT_FOUND);
270 } else if (cert_req_callback_mode == 2) {
271 cb_chain = chain_and_key;
272 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
273 }
274
275 if (expected_success) {
276 switch (s2n_connection_get_actual_protocol_version(server_conn)) {
277 case S2N_TLS13:
278 saw_tls13 = true;
279 break;
280 case S2N_TLS12:
281 saw_tls12 = true;
282 break;
283 default:
284 /* no-op */
285 break;
286 }
287
288 /* Verify that both connections negotiated Mutual Auth */
289 EXPECT_TRUE(s2n_connection_client_cert_used(server_conn));
290 EXPECT_TRUE(s2n_connection_client_cert_used(client_conn));
291 EXPECT_TRUE(verify_data.callback_invoked);
292 }
293
294 EXPECT_SUCCESS(s2n_connection_free(client_conn));
295 EXPECT_SUCCESS(s2n_connection_free(server_conn));
296 EXPECT_SUCCESS(s2n_stuffer_free(&server_to_client));
297 EXPECT_SUCCESS(s2n_stuffer_free(&client_to_server));
298 }
299
300 if (expected_success) {
301 /* If not supported, don't expect to see TlS 1.3 */
302 EXPECT_TRUE(saw_tls13 || !s2n_is_tls13_fully_supported());
303 EXPECT_TRUE(saw_tls12);
304 }
305
306 /* Reset the callback */
307 config->cert_request_cb = NULL;
308 config->cert_request_cb_ctx = NULL;
309 }
310
311 /*
312 * Test Mutual Auth using **s2n_config_set_client_auth_type**
313 */
314
315 EXPECT_SUCCESS(s2n_config_set_client_auth_type(config, S2N_CERT_AUTH_REQUIRED));
316
317 /* Verify that a handshake succeeds for every cipher in the default list. */
318 for (size_t cipher_idx = 0; cipher_idx < default_cipher_preferences->count; cipher_idx++) {
319 struct s2n_cipher_preferences server_cipher_preferences;
320 struct s2n_security_policy server_security_policy;
321 struct s2n_connection *client_conn = NULL;
322 struct s2n_connection *server_conn = NULL;
323 struct s2n_stuffer client_to_server = { 0 };
324 struct s2n_stuffer server_to_client = { 0 };
325
326 /* Craft a cipher preference with a cipher_idx cipher */
327 EXPECT_MEMCPY_SUCCESS(&server_cipher_preferences, default_cipher_preferences, sizeof(server_cipher_preferences));
328 server_cipher_preferences.count = 1;
329 struct s2n_cipher_suite *cur_cipher = default_cipher_preferences->suites[cipher_idx];
330
331 if (s2n_should_skip_cipher(cur_cipher)) {
332 continue;
333 }
334
335 server_cipher_preferences.suites = &cur_cipher;
336 EXPECT_MEMCPY_SUCCESS(&server_security_policy, default_security_policy, sizeof(server_security_policy));
337 server_security_policy.cipher_preferences = &server_cipher_preferences;
338 config->security_policy = &server_security_policy;
339
340 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
341 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
342
343 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
344 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
345
346 /* Set up our I/O callbacks. Use stuffers for the "I/O context" */
347 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&client_to_server, 0));
348 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&server_to_client, 0));
349 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&server_to_client, &client_to_server, client_conn));
350 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&client_to_server, &server_to_client, server_conn));
351
352 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
353
354 /* Verify that both connections negotiated Mutual Auth */
355 EXPECT_TRUE(s2n_connection_client_cert_used(server_conn));
356 EXPECT_TRUE(s2n_connection_client_cert_used(client_conn));
357
358 EXPECT_SUCCESS(s2n_connection_free(client_conn));
359 EXPECT_SUCCESS(s2n_connection_free(server_conn));
360 EXPECT_SUCCESS(s2n_stuffer_free(&server_to_client));
361 EXPECT_SUCCESS(s2n_stuffer_free(&client_to_server));
362 }
363
364 /*
365 * Test Mutual Auth using connection override of **s2n_config_set_client_auth_type**
366 */
367
368 EXPECT_SUCCESS(s2n_config_set_client_auth_type(config, S2N_CERT_AUTH_NONE));
369
370 /* Verify that a handshake succeeds for every cipher in the default list. */
371 for (size_t cipher_idx = 0; cipher_idx < default_cipher_preferences->count; cipher_idx++) {
372 struct s2n_cipher_preferences server_cipher_preferences;
373 struct s2n_security_policy server_security_policy;
374 struct s2n_connection *client_conn = NULL;
375 struct s2n_connection *server_conn = NULL;
376 struct s2n_stuffer client_to_server = { 0 };
377 struct s2n_stuffer server_to_client = { 0 };
378
379 /* Craft a cipher preference with a cipher_idx cipher */
380 EXPECT_MEMCPY_SUCCESS(&server_cipher_preferences, default_cipher_preferences, sizeof(server_cipher_preferences));
381 server_cipher_preferences.count = 1;
382 struct s2n_cipher_suite *cur_cipher = default_cipher_preferences->suites[cipher_idx];
383
384 if (s2n_should_skip_cipher(cur_cipher)) {
385 continue;
386 }
387
388 server_cipher_preferences.suites = &cur_cipher;
389
390 EXPECT_MEMCPY_SUCCESS(&server_security_policy, default_security_policy, sizeof(server_security_policy));
391 server_security_policy.cipher_preferences = &server_cipher_preferences;
392
393 config->security_policy = &server_security_policy;
394
395 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
396 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
397
398 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
399 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
400
401 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(server_conn, S2N_CERT_AUTH_REQUIRED));
402 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(client_conn, S2N_CERT_AUTH_REQUIRED));
403
404 /* Set up our I/O callbacks. Use stuffers for the "I/O context" */
405 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&client_to_server, 0));
406 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&server_to_client, 0));
407 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&server_to_client, &client_to_server, client_conn));
408 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&client_to_server, &server_to_client, server_conn));
409
410 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
411
412 /* Verify that both connections negotiated Mutual Auth */
413 EXPECT_TRUE(s2n_connection_client_cert_used(server_conn));
414 EXPECT_TRUE(s2n_connection_client_cert_used(client_conn));
415
416 EXPECT_SUCCESS(s2n_connection_free(client_conn));
417 EXPECT_SUCCESS(s2n_connection_free(server_conn));
418 EXPECT_SUCCESS(s2n_stuffer_free(&server_to_client));
419 EXPECT_SUCCESS(s2n_stuffer_free(&client_to_server));
420 }
421
422 /*
423 * Test Mutual Auth using connection override of **s2n_config_set_client_auth_type** only on one side of the
424 * connection and verify that a connection is not established
425 */
426
427 EXPECT_SUCCESS(s2n_config_set_client_auth_type(config, S2N_CERT_AUTH_NONE));
428
429 /* Verify that a handshake succeeds for every cipher in the default list. */
430 for (size_t cipher_idx = 0; cipher_idx < default_cipher_preferences->count; cipher_idx++) {
431 struct s2n_cipher_preferences server_cipher_preferences;
432 struct s2n_security_policy server_security_policy;
433 struct s2n_connection *client_conn = NULL;
434 struct s2n_connection *server_conn = NULL;
435 struct s2n_stuffer client_to_server = { 0 };
436 struct s2n_stuffer server_to_client = { 0 };
437
438 /* Craft a cipher preference with a cipher_idx cipher */
439 EXPECT_MEMCPY_SUCCESS(&server_cipher_preferences, default_cipher_preferences, sizeof(server_cipher_preferences));
440 server_cipher_preferences.count = 1;
441 struct s2n_cipher_suite *cur_cipher = default_cipher_preferences->suites[cipher_idx];
442
443 if (s2n_should_skip_cipher(cur_cipher)) {
444 continue;
445 }
446
447 server_cipher_preferences.suites = &cur_cipher;
448
449 EXPECT_MEMCPY_SUCCESS(&server_security_policy, default_security_policy, sizeof(server_security_policy));
450 server_security_policy.cipher_preferences = &server_cipher_preferences;
451
452 config->security_policy = &server_security_policy;
453
454 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
455 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
456
457 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
458 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
459
460 /* Only set S2N_CERT_AUTH_REQUIRED on the server and not the client so that the connection fails */
461 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(server_conn, S2N_CERT_AUTH_REQUIRED));
462
463 /* Set up our I/O callbacks. Use stuffers for the "I/O context" */
464 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&client_to_server, 0));
465 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&server_to_client, 0));
466 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&server_to_client, &client_to_server, client_conn));
467 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&client_to_server, &server_to_client, server_conn));
468
469 EXPECT_FAILURE(s2n_negotiate_test_server_and_client(server_conn, client_conn));
470
471 /* Verify that NEITHER connections negotiated Mutual Auth */
472 EXPECT_FALSE(s2n_connection_client_cert_used(server_conn));
473 EXPECT_FALSE(s2n_connection_client_cert_used(client_conn));
474
475 EXPECT_SUCCESS(s2n_connection_free(client_conn));
476 EXPECT_SUCCESS(s2n_connection_free(server_conn));
477 EXPECT_SUCCESS(s2n_stuffer_free(&server_to_client));
478 EXPECT_SUCCESS(s2n_stuffer_free(&client_to_server));
479 }
480
481 /* Ensure that the client's certificate is validated, regardless of how client auth was enabled */
482 {
483 typedef enum {
484 TEST_ENABLE_WITH_CONFIG,
485 TEST_ENABLE_WITH_CONN_BEFORE_CONFIG,
486 TEST_ENABLE_WITH_CONN_AFTER_CONFIG,
487 TEST_COUNT,
488 } test_case;
489
490 for (test_case test = TEST_ENABLE_WITH_CONFIG; test < TEST_COUNT; test++) {
491 DEFER_CLEANUP(struct s2n_config *client_config = s2n_config_new(), s2n_config_ptr_free);
492 EXPECT_NOT_NULL(client_config);
493 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "default_tls13"));
494 EXPECT_SUCCESS(s2n_config_set_client_auth_type(client_config, S2N_CERT_AUTH_OPTIONAL));
495
496 /* The client trusts the server's cert, and sends the same cert to the server. */
497 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(client_config, S2N_DEFAULT_TEST_CERT_CHAIN, NULL));
498 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(client_config, chain_and_key));
499
500 DEFER_CLEANUP(struct s2n_config *server_config = s2n_config_new(), s2n_config_ptr_free);
501 EXPECT_NOT_NULL(server_config);
502 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config, "default_tls13"));
503 struct host_verify_data verify_data_allow = { .allow = 1 };
504 EXPECT_SUCCESS(s2n_config_set_verify_host_callback(server_config, verify_host_fn, &verify_data_allow));
505
506 /* The server sends its cert, but does NOT trust the client's cert. This should always
507 * cause certificate validation to fail on the server.
508 */
509 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
510
511 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
512 s2n_connection_ptr_free);
513 EXPECT_NOT_NULL(server_conn);
514 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
515
516 switch (test) {
517 case TEST_ENABLE_WITH_CONFIG:
518 EXPECT_SUCCESS(s2n_config_set_client_auth_type(server_config, S2N_CERT_AUTH_REQUIRED));
519 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
520 break;
521 case TEST_ENABLE_WITH_CONN_BEFORE_CONFIG:
522 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(server_conn, S2N_CERT_AUTH_REQUIRED));
523 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
524 break;
525 case TEST_ENABLE_WITH_CONN_AFTER_CONFIG:
526 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
527 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(server_conn, S2N_CERT_AUTH_REQUIRED));
528 break;
529 default:
530 FAIL_MSG("Invalid test case");
531 }
532
533 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
534 s2n_connection_ptr_free);
535 EXPECT_NOT_NULL(client_conn);
536 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
537 EXPECT_SUCCESS(s2n_set_server_name(client_conn, "localhost"));
538
539 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
540 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
541 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
542 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
543
544 EXPECT_FAILURE_WITH_ALERT(s2n_negotiate_test_server_and_client(server_conn, client_conn),
545 S2N_ERR_CERT_UNTRUSTED, S2N_TLS_ALERT_CERTIFICATE_UNKNOWN);
546
547 /* Ensure that a client certificate was received on the server, indicating that the
548 * validation error occurred when processing the client's certificate, rather than the
549 * server's.
550 */
551 uint8_t *client_cert_chain = NULL;
552 uint32_t client_cert_chain_len = 0;
553 EXPECT_SUCCESS(s2n_connection_get_client_cert_chain(server_conn,
554 &client_cert_chain, &client_cert_chain_len));
555 EXPECT_TRUE(client_cert_chain_len > 0);
556 }
557 }
558
559 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
560 EXPECT_SUCCESS(s2n_config_free(config));
561 free(cert_chain_pem);
562 free(private_key_pem);
563 free(dhparams_pem);
564 END_TEST();
565 }