Debian Codegraph @

crt/s2n/tests/unit/s2n_x509_intent_verification_test.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include "s2n_test.h"
17 #include "testlib/s2n_testlib.h"
18
19 static S2N_RESULT s2n_test_pem_paths_from_intent_dir(const char *intent_cert_dir, char *cert_chain_path,
20 char *leaf_key_path, char *root_cert_path)
21 {
22 sprintf(cert_chain_path, "%s/cert-chain.pem", intent_cert_dir);
23 sprintf(leaf_key_path, "%s/leaf-key.pem", intent_cert_dir);
24 sprintf(root_cert_path, "%s/root-cert.pem", intent_cert_dir);
25 return S2N_RESULT_OK;
26 }
27
28 int main(int argc, char *argv[])
29 {
30 BEGIN_TEST();
31
32 /* Test s2n_config_disable_x509_intent_verification(). */
33 {
34 /* Safety */
35 {
36 EXPECT_FAILURE_WITH_ERRNO(s2n_config_disable_x509_intent_verification(NULL), S2N_ERR_INVALID_ARGUMENT);
37
38 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new_minimal(), s2n_config_ptr_free);
39 EXPECT_SUCCESS(s2n_config_disable_x509_intent_verification(config));
40 }
41
42 /* The verification is enabled by default. */
43 {
44 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new_minimal(), s2n_config_ptr_free);
45 EXPECT_NOT_NULL(config);
46 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
47 EXPECT_NOT_NULL(conn);
48
49 EXPECT_FALSE(config->disable_x509_intent_verification);
50 }
51
52 /* Disabling the verification on the config updates the proper flags. */
53 {
54 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new_minimal(), s2n_config_ptr_free);
55 EXPECT_NOT_NULL(config);
56 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
57 EXPECT_NOT_NULL(conn);
58
59 EXPECT_FALSE(config->disable_x509_intent_verification);
60 EXPECT_SUCCESS(s2n_config_disable_x509_intent_verification(config));
61 EXPECT_TRUE(config->disable_x509_intent_verification);
62 }
63 }
64
65 /* Test certificate intent verification. */
66 {
67 struct {
68 const char *cert_chain_dir;
69 s2n_error expected_client_error;
70 s2n_error expected_server_error;
71 } test_cases[] = {
72 {
73 /* A certificate chain with no optional intent extensions specified. This
74 * certificate chain includes the mandatory BasicConstraints extension for CA
75 * certificates, but doesn't include the KeyUsage or ExtendedKeyUsage
76 * extensions.
77 */
78 .cert_chain_dir = "../pems/intent/cert_chains/no_intent",
79 /* The KeyUsage and ExtendedKeyUsage extensions are optional, so validation
80 * should succeed.
81 */
82 .expected_client_error = S2N_ERR_OK,
83 .expected_server_error = S2N_ERR_OK,
84 },
85 {
86 /* A certificate chain with digitalSignature set in the leaf KeyUsage
87 * extension.
88 */
89 .cert_chain_dir = "../pems/intent/cert_chains/ku_digital_signature_leaf",
90 /* Setting digitalSignature is valid for both client and server leaf
91 * certificates.
92 */
93 .expected_client_error = S2N_ERR_OK,
94 .expected_server_error = S2N_ERR_OK,
95 },
96 #if !defined(LIBRESSL_VERSION_NUMBER)
97 /* This test is skipped for LibreSSL since LibreSSL doesn't consider keyAgreement to be
98 * valid for client or server leaf certificates:
99 * https://github.com/libressl/openbsd/blob/8bb14039f52469491bf0058b1efdf0c75db6befc/src/lib/libcrypto/x509/x509_purp.c#L662-L691
100 */
101 {
102 /* A certificate chain with keyAgreement set in the leaf KeyUsage extension. */
103 .cert_chain_dir = "../pems/intent/cert_chains/ku_key_agreement_leaf",
104 /* Setting keyAgreement is valid for both client and server leaf certificates. */
105 .expected_client_error = S2N_ERR_OK,
106 .expected_server_error = S2N_ERR_OK,
107 },
108 #endif
109 {
110 /* A certificate chain with digitalSignature and keyAgreement set in the leaf
111 * KeyUsage extension.
112 */
113 .cert_chain_dir = "../pems/intent/cert_chains/ku_digital_signature_and_key_agreement_leaf",
114 /* Setting digitalSignature OR keyAgreement is valid for both client and server
115 * leaf certificates. Setting both fields is also valid.
116 */
117 .expected_client_error = S2N_ERR_OK,
118 .expected_server_error = S2N_ERR_OK,
119 },
120 {
121 /* A certificate chain with digitalSignature and contentCommitment set in the leaf
122 * KeyUsage extension.
123 */
124 .cert_chain_dir = "../pems/intent/cert_chains/ku_digital_signature_and_content_commitment_leaf",
125 /* Setting digitalSignature is valid for both client and server leaf
126 * certificates. contentCommitment is not relevant for client or server
127 * certificates, but since digitalSignature is set, validation should succeed.
128 */
129 .expected_client_error = S2N_ERR_OK,
130 .expected_server_error = S2N_ERR_OK,
131 },
132 {
133 /* A certificate chain with keyEncipherment set in the leaf KeyUsage extension. */
134 .cert_chain_dir = "../pems/intent/cert_chains/ku_key_encipherment_leaf",
135 /* Setting keyEncipherment is valid for server leaf certificates, but is NOT
136 * valid for client leaf certificates.
137 */
138 .expected_client_error = S2N_ERR_OK,
139 .expected_server_error = S2N_ERR_CERT_INTENT_INVALID,
140 },
141 {
142 /* A certificate chain with keyCertSign set in the leaf KeyUsage extension. */
143 .cert_chain_dir = "../pems/intent/cert_chains/ku_key_cert_sign_leaf",
144 /* Setting keyCertSign is only valid for CA certificates, NOT for leaf
145 * certificates.
146 */
147 .expected_client_error = S2N_ERR_CERT_INTENT_INVALID,
148 .expected_server_error = S2N_ERR_CERT_INTENT_INVALID,
149 },
150 {
151 /* A certificate chain with keyCertSign set in an intermediate KeyUsage
152 * extension.
153 */
154 .cert_chain_dir = "../pems/intent/cert_chains/ku_key_cert_sign_intermediate",
155 /* Setting keyCertSign is valid for CA certificates. */
156 .expected_client_error = S2N_ERR_OK,
157 .expected_server_error = S2N_ERR_OK,
158 },
159 {
160 /* A certificate chain with keyCertSign and contentCommitment set in an
161 * intermediate KeyUsage extension.
162 */
163 .cert_chain_dir = "../pems/intent/cert_chains/ku_key_cert_sign_and_content_commitment_intermediate",
164 /* Setting keyCertSign is valid for CA certificates. The irrelevant
165 * contentCommitment field should not impact validation.
166 */
167 .expected_client_error = S2N_ERR_OK,
168 .expected_server_error = S2N_ERR_OK,
169 },
170 {
171 /* A certificate chain with clientAuth and serverAuth set in the leaf
172 * ExtendedKeyUsage extension.
173 */
174 .cert_chain_dir = "../pems/intent/cert_chains/eku_clientAuth_and_serverAuth_leaf",
175 /* A certificate that sets both clientAuth and serverAuth is valid for both
176 * client and server certificates.
177 */
178 .expected_client_error = S2N_ERR_OK,
179 .expected_server_error = S2N_ERR_OK,
180 },
181 {
182 /* A certificate chain with clientAuth and serverAuth set in an intermediate
183 * ExtendedKeyUsage extension.
184 */
185 .cert_chain_dir = "../pems/intent/cert_chains/eku_clientAuth_and_serverAuth_intermediate",
186 /* A certificate that sets both clientAuth and serverAuth is valid for both
187 * client and server certificates.
188 */
189 .expected_client_error = S2N_ERR_OK,
190 .expected_server_error = S2N_ERR_OK,
191 },
192 {
193 /* A certificate chain with clientAuth, serverAuth, and emailProtection set in
194 * the leaf ExtendedKeyUsage extension.
195 */
196 .cert_chain_dir = "../pems/intent/cert_chains/eku_clientAuth_and_serverAuth_and_emailProtection_leaf",
197 /* A certificate that sets both clientAuth and serverAuth is valid for both
198 * client and server certificates. The irrelevant emailProtection field should
199 * not impact validation.
200 */
201 .expected_client_error = S2N_ERR_OK,
202 .expected_server_error = S2N_ERR_OK,
203 },
204 {
205 /* A certificate chain with clientAuth, serverAuth, and emailProtection set in
206 * an intermediate ExtendedKeyUsage extension.
207 */
208 .cert_chain_dir = "../pems/intent/cert_chains/eku_clientAuth_and_serverAuth_and_emailProtection_intermediate",
209 /* A certificate that sets both clientAuth and serverAuth is valid for both
210 * client and server certificates. The irrelevant emailProtection field should
211 * not impact validation.
212 */
213 .expected_client_error = S2N_ERR_OK,
214 .expected_server_error = S2N_ERR_OK,
215 },
216 {
217 /* A certificate chain with clientAuth set in the leaf ExtendedKeyUsage
218 * extension.
219 */
220 .cert_chain_dir = "../pems/intent/cert_chains/eku_clientAuth_leaf",
221 /* A certificate that sets clientAuth is valid for client certificates, but NOT
222 * for server certificates.
223 */
224 .expected_client_error = S2N_ERR_CERT_INTENT_INVALID,
225 .expected_server_error = S2N_ERR_OK,
226 },
227 {
228 /* A certificate chain with clientAuth set in an intermediate ExtendedKeyUsage
229 * extension.
230 */
231 .cert_chain_dir = "../pems/intent/cert_chains/eku_clientAuth_intermediate",
232 /* A certificate that sets clientAuth is valid for client certificates, but NOT
233 * for server certificates.
234 */
235 .expected_client_error = S2N_ERR_CERT_INTENT_INVALID,
236 .expected_server_error = S2N_ERR_OK,
237 },
238 {
239 /* A certificate chain with serverAuth set in the leaf ExtendedKeyUsage
240 * extension.
241 */
242 .cert_chain_dir = "../pems/intent/cert_chains/eku_serverAuth_leaf",
243 /* A certificate that sets serverAuth is valid for server certificates but NOT
244 * for client certificates.
245 */
246 .expected_client_error = S2N_ERR_OK,
247 .expected_server_error = S2N_ERR_CERT_INTENT_INVALID,
248 },
249 {
250 /* A certificate chain with serverAuth set in an intermediate ExtendedKeyUsage
251 * extension.
252 */
253 .cert_chain_dir = "../pems/intent/cert_chains/eku_serverAuth_intermediate",
254 /* A certificate that sets serverAuth is valid for server certificates but NOT
255 * for client certificates.
256 */
257 .expected_client_error = S2N_ERR_OK,
258 .expected_server_error = S2N_ERR_CERT_INTENT_INVALID,
259 },
260 {
261 /* A certificate chain with codeSigning in the leaf ExtendedKeyUsage extension. */
262 .cert_chain_dir = "../pems/intent/cert_chains/eku_codeSigning_leaf",
263 /* A certificate that sets codeSigning is NOT valid for client or server
264 * certificates.
265 */
266 .expected_client_error = S2N_ERR_CERT_INTENT_INVALID,
267 .expected_server_error = S2N_ERR_CERT_INTENT_INVALID,
268 },
269 {
270 /* A certificate chain with codeSigning in an intermediate ExtendedKeyUsage
271 * extension.
272 */
273 .cert_chain_dir = "../pems/intent/cert_chains/eku_codeSigning_intermediate",
274 /* A certificate that sets codeSigning is NOT valid for client or server
275 * certificates.
276 */
277 .expected_client_error = S2N_ERR_CERT_INTENT_INVALID,
278 .expected_server_error = S2N_ERR_CERT_INTENT_INVALID,
279 },
280 {
281 /* A long certificate chain with codeSigning set in the fourth intermediate
282 * ExtendedKeyUsage extension.
283 */
284 .cert_chain_dir = "../pems/intent/cert_chains/eku_codeSigning_intermediate_long",
285 /* A certificate that sets codeSigning is NOT valid for client or server
286 * certificates.
287 */
288 .expected_client_error = S2N_ERR_CERT_INTENT_INVALID,
289 .expected_server_error = S2N_ERR_CERT_INTENT_INVALID,
290 },
291 {
292 /* A certificate chain with crlSign set in the leaf KeyUsage extension, but the
293 * extension is marked as non-critical.
294 */
295 .cert_chain_dir = "../pems/intent/cert_chains/ku_crl_sign_leaf_non_critical",
296 /* A certificate that sets crlSign is NOT valid for client or server
297 * certificates. This should be validated regardless of the extension
298 * criticality.
299 */
300 .expected_client_error = S2N_ERR_CERT_INTENT_INVALID,
301 .expected_server_error = S2N_ERR_CERT_INTENT_INVALID,
302 },
303 {
304 /* A certificate chain with codeSigning set in the leaf ExtendedKeyUsage
305 * extension, but the extension is marked as non-critical.
306 */
307 .cert_chain_dir = "../pems/intent/cert_chains/eku_code_signing_intermediate_non_critical",
308 /* A certificate that sets codeSigning is NOT valid for client or server
309 * certificates. This should be validated regardless of the extension
310 * criticality.
311 */
312 .expected_client_error = S2N_ERR_CERT_INTENT_INVALID,
313 .expected_server_error = S2N_ERR_CERT_INTENT_INVALID,
314 },
315 };
316
317 for (size_t test_idx = 0; test_idx < s2n_array_len(test_cases); test_idx++) {
318 char cert_chain_path[S2N_MAX_TEST_PEM_PATH_LENGTH] = { 0 };
319 char leaf_key_path[S2N_MAX_TEST_PEM_PATH_LENGTH] = { 0 };
320 char root_cert_path[S2N_MAX_TEST_PEM_PATH_LENGTH] = { 0 };
321 EXPECT_OK(s2n_test_pem_paths_from_intent_dir(test_cases[test_idx].cert_chain_dir, cert_chain_path,
322 leaf_key_path, root_cert_path));
323
324 /* Intent is verified for server certificates received by the client. */
325 for (size_t disable_intent_verification = 0; disable_intent_verification <= 1; disable_intent_verification++) {
326 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL, s2n_cert_chain_and_key_ptr_free);
327 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
328 cert_chain_path, leaf_key_path));
329
330 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new_minimal(), s2n_config_ptr_free);
331 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(config, root_cert_path, NULL));
332 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
333
334 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
335 s2n_connection_ptr_free);
336 EXPECT_NOT_NULL(server_conn);
337 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
338
339 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
340 s2n_connection_ptr_free);
341 EXPECT_NOT_NULL(client_conn);
342 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
343 EXPECT_SUCCESS(s2n_set_server_name(client_conn, "localhost"));
344 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
345
346 if (disable_intent_verification) {
347 EXPECT_SUCCESS(s2n_config_disable_x509_intent_verification(config));
348 }
349
350 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
351 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
352 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
353 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
354
355 int ret = s2n_negotiate_test_server_and_client(server_conn, client_conn);
356
357 s2n_error expected_error = test_cases[test_idx].expected_client_error;
358 if (disable_intent_verification) {
359 EXPECT_SUCCESS(ret);
360 } else {
361 if (expected_error == S2N_ERR_OK) {
362 EXPECT_SUCCESS(ret);
363 } else {
364 EXPECT_FAILURE_WITH_ERRNO(ret, expected_error);
365 }
366 }
367 }
368
369 /* Intent is verified for client certificates received by the server. */
370 for (size_t disable_intent_verification = 0; disable_intent_verification <= 1; disable_intent_verification++) {
371 DEFER_CLEANUP(struct s2n_cert_chain_and_key *server_chain_and_key = NULL,
372 s2n_cert_chain_and_key_ptr_free);
373 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&server_chain_and_key,
374 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
375
376 DEFER_CLEANUP(struct s2n_cert_chain_and_key *client_chain_and_key = NULL,
377 s2n_cert_chain_and_key_ptr_free);
378 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&client_chain_and_key,
379 cert_chain_path, leaf_key_path));
380
381 DEFER_CLEANUP(struct s2n_config *server_config = s2n_config_new(), s2n_config_ptr_free);
382 EXPECT_NOT_NULL(server_config);
383 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(server_config, root_cert_path, NULL));
384 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, server_chain_and_key));
385 EXPECT_SUCCESS(s2n_config_set_client_auth_type(server_config, S2N_CERT_AUTH_REQUIRED));
386
387 if (disable_intent_verification) {
388 EXPECT_SUCCESS(s2n_config_disable_x509_intent_verification(server_config));
389 }
390
391 DEFER_CLEANUP(struct s2n_config *client_config = s2n_config_new(), s2n_config_ptr_free);
392 EXPECT_NOT_NULL(client_config);
393 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(client_config,
394 S2N_DEFAULT_TEST_CERT_CHAIN, NULL));
395 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(client_config, client_chain_and_key));
396 EXPECT_SUCCESS(s2n_config_set_client_auth_type(client_config, S2N_CERT_AUTH_OPTIONAL));
397
398 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
399 s2n_connection_ptr_free);
400 EXPECT_NOT_NULL(server_conn);
401 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
402 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
403
404 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
405 s2n_connection_ptr_free);
406 EXPECT_NOT_NULL(client_conn);
407 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
408 EXPECT_SUCCESS(s2n_set_server_name(client_conn, "localhost"));
409
410 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
411 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
412 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
413 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
414
415 int ret = s2n_negotiate_test_server_and_client(server_conn, client_conn);
416
417 s2n_error expected_error = test_cases[test_idx].expected_server_error;
418 if (disable_intent_verification) {
419 EXPECT_SUCCESS(ret);
420 } else {
421 if (expected_error == S2N_ERR_OK) {
422 EXPECT_SUCCESS(ret);
423 } else {
424 EXPECT_FAILURE_WITH_ERRNO(ret, expected_error);
425 }
426 }
427
428 /* Ensure that a client certificate was received. In the case of an expected error,
429 * this ensures that the error occurred on the server side, after the client
430 * successfully validated the server's certificate.
431 */
432 uint8_t *client_cert_chain = NULL;
433 uint32_t client_cert_chain_len = 0;
434 EXPECT_SUCCESS(s2n_connection_get_client_cert_chain(server_conn, &client_cert_chain,
435 &client_cert_chain_len));
436 EXPECT_TRUE(client_cert_chain_len > 0);
437 }
438 }
439 }
440
441 /* Test default s2n-tls intent verification.
442 *
443 * s2n-tls already verifies intent fields for intermediate certificates in the call to
444 * X509_verify_cert. So this verification should continue to be performed whether certificate
445 * intent verification is enabled or not.
446 */
447 {
448 struct {
449 const char *cert_chain_dir;
450 } test_cases[] = {
451 {
452 /* A certificate chain with the CA field set to false in an intermediate
453 * BasicConstraints extension.
454 *
455 * CA certificates MUST set the CA field to true.
456 */
457 .cert_chain_dir = "../pems/intent/cert_chains/bc_non_ca_intermediate",
458 },
459 {
460 /* A certificate chain with digitalSignature set in an intermediate KeyUsage
461 * extension.
462 *
463 * The digitalSignature field is valid only for leaf certificates, NOT CA
464 * certificates.
465 */
466 .cert_chain_dir = "../pems/intent/cert_chains/ku_digital_signature_intermediate",
467 },
468 {
469 /* A long certificate chain with digitalSignature set in the fourth
470 * intermediate KeyUsage extension.
471 *
472 * The digitalSignature field is valid only for leaf certificates, NOT CA
473 * certificates.
474 */
475 .cert_chain_dir = "../pems/intent/cert_chains/ku_digital_signature_intermediate_long",
476 },
477 };
478
479 for (size_t test_idx = 0; test_idx < s2n_array_len(test_cases); test_idx++) {
480 char cert_chain_path[S2N_MAX_TEST_PEM_PATH_LENGTH] = { 0 };
481 char leaf_key_path[S2N_MAX_TEST_PEM_PATH_LENGTH] = { 0 };
482 char root_cert_path[S2N_MAX_TEST_PEM_PATH_LENGTH] = { 0 };
483 EXPECT_OK(s2n_test_pem_paths_from_intent_dir(test_cases[test_idx].cert_chain_dir, cert_chain_path,
484 leaf_key_path, root_cert_path));
485
486 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL, s2n_cert_chain_and_key_ptr_free);
487 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key, cert_chain_path, leaf_key_path));
488
489 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new_minimal(), s2n_config_ptr_free);
490 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(config, root_cert_path, NULL));
491 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
492
493 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
494 s2n_connection_ptr_free);
495 EXPECT_NOT_NULL(server_conn);
496 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
497
498 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
499 s2n_connection_ptr_free);
500 EXPECT_NOT_NULL(client_conn);
501 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
502 EXPECT_SUCCESS(s2n_set_server_name(client_conn, "localhost"));
503 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
504
505 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
506 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
507 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
508 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
509
510 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, client_conn),
511 S2N_ERR_CERT_UNTRUSTED);
512 }
513 }
514
515 /* Ensure that intent verification doesn't apply to trust anchors.
516 *
517 * Despite certificate intent fields that may indicate otherwise, it is assumed that trust
518 * anchors were intended to be used for a TLS purpose, given that they were included in the
519 * s2n-tls trust store.
520 */
521 {
522 struct {
523 const char *cert_chain_path;
524 const char *leaf_key_path;
525 const char *trust_anchor_path;
526 s2n_error expected_error;
527 } test_cases[] = {
528 {
529 /* A certificate chain with codeSigning in the leaf ExtendedKeyUsage extension. */
530 .cert_chain_path = "../pems/intent/cert_chains/eku_codeSigning_leaf/cert-chain.pem",
531 .leaf_key_path = "../pems/intent/cert_chains/eku_codeSigning_leaf/leaf-key.pem",
532 /* A certificate that sets codeSigning is not valid for TLS. When the invalid
533 * leaf certificate is not a trust anchor, validation should fail.
534 */
535 .trust_anchor_path = "../pems/intent/cert_chains/eku_codeSigning_leaf/root-cert.pem",
536 .expected_error = S2N_ERR_CERT_INTENT_INVALID,
537 },
538 {
539 /* A certificate chain with codeSigning in the leaf ExtendedKeyUsage extension. */
540 .cert_chain_path = "../pems/intent/cert_chains/eku_codeSigning_leaf/cert-chain.pem",
541 .leaf_key_path = "../pems/intent/cert_chains/eku_codeSigning_leaf/leaf-key.pem",
542 /* A certificate that sets codeSigning is not valid for TLS. However, this is
543 * acceptable if the invalid leaf certificate is a trust anchor.
544 */
545 .trust_anchor_path = "../pems/intent/cert_chains/eku_codeSigning_leaf/leaf-cert.pem",
546 .expected_error = S2N_ERR_OK,
547 },
548 {
549 /* A certificate chain with codeSigning in an intermediate ExtendedKeyUsage
550 * extension.
551 */
552 .cert_chain_path = "../pems/intent/cert_chains/eku_codeSigning_intermediate/cert-chain.pem",
553 .leaf_key_path = "../pems/intent/cert_chains/eku_codeSigning_intermediate/leaf-key.pem",
554 /* A certificate that sets codeSigning is not valid for TLS. When the invalid
555 * intermediate certificate is not a trust anchor, validation should fail.
556 */
557 .trust_anchor_path = "../pems/intent/cert_chains/eku_codeSigning_intermediate/root-cert.pem",
558 .expected_error = S2N_ERR_CERT_INTENT_INVALID,
559 },
560 {
561 /* A certificate chain with codeSigning in an intermediate ExtendedKeyUsage
562 * extension.
563 */
564 .cert_chain_path = "../pems/intent/cert_chains/eku_codeSigning_intermediate/cert-chain.pem",
565 .leaf_key_path = "../pems/intent/cert_chains/eku_codeSigning_intermediate/leaf-key.pem",
566 /* A certificate that sets codeSigning is not valid for TLS. However, this is
567 * acceptable if the invalid intermediate certificate is a trust anchor.
568 */
569 .trust_anchor_path = "../pems/intent/cert_chains/eku_codeSigning_intermediate/intermediate_1-cert.pem",
570 .expected_error = S2N_ERR_OK,
571 },
572 {
573 /* A certificate chain with codeSigning in an intermediate ExtendedKeyUsage
574 * extension.
575 */
576 .cert_chain_path = "../pems/intent/cert_chains/eku_codeSigning_intermediate/cert-chain.pem",
577 .leaf_key_path = "../pems/intent/cert_chains/eku_codeSigning_intermediate/leaf-key.pem",
578 /* A certificate that sets codeSigning is not valid for TLS. However, this is
579 * acceptable if the leaf certificate is a trust anchor, since the invalid
580 * intermediate certificate won't be in the chain of trust.
581 */
582 .trust_anchor_path = "../pems/intent/cert_chains/eku_codeSigning_intermediate/leaf-cert.pem",
583 .expected_error = S2N_ERR_OK,
584 },
585 {
586 /* A certificate that sets emailProtection in the root ExtendedKeyUsage extension. */
587 .cert_chain_path = "../pems/intent/cert_chains/eku_email_protection_root/cert-chain.pem",
588 .leaf_key_path = "../pems/intent/cert_chains/eku_email_protection_root/leaf-key.pem",
589 /* A certificate that sets emailProtection is not valid for TLS. However, this is
590 * acceptable if the certificate is a trust anchor.
591 */
592 .trust_anchor_path = "../pems/intent/cert_chains/eku_email_protection_root/root-cert.pem",
593 .expected_error = S2N_ERR_OK,
594 },
595 };
596
597 for (size_t test_idx = 0; test_idx < s2n_array_len(test_cases); test_idx++) {
598 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL, s2n_cert_chain_and_key_ptr_free);
599 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
600 test_cases[test_idx].cert_chain_path, test_cases[test_idx].leaf_key_path));
601
602 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new_minimal(), s2n_config_ptr_free);
603 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(config,
604 test_cases[test_idx].trust_anchor_path, NULL));
605 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
606
607 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
608 s2n_connection_ptr_free);
609 EXPECT_NOT_NULL(server_conn);
610 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
611
612 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
613 s2n_connection_ptr_free);
614 EXPECT_NOT_NULL(client_conn);
615 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
616 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
617 EXPECT_SUCCESS(s2n_set_server_name(client_conn, "localhost"));
618
619 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
620 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
621 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
622 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
623
624 int ret = s2n_negotiate_test_server_and_client(server_conn, client_conn);
625
626 s2n_error expected_error = test_cases[test_idx].expected_error;
627 if (expected_error == S2N_ERR_OK) {
628 EXPECT_SUCCESS(ret);
629 } else {
630 EXPECT_FAILURE_WITH_ERRNO(ret, expected_error);
631 }
632 }
633 }
634
635 END_TEST();
636 }