Debian Codegraph @

crt/s2n/tests/unit/s2n_x509_get_validated_cert_chain_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 "crypto/s2n_libcrypto.h"
17 #include "s2n_test.h"
18 #include "testlib/s2n_testlib.h"
19 #include "tls/s2n_x509_validator.h"
20
21 bool s2n_libcrypto_supports_get0_chain()
22 {
23 #if S2N_LIBCRYPTO_SUPPORTS_GET0_CHAIN
24 return true;
25 #else
26 return false;
27 #endif
28 }
29
30 int main(int argc, char *argv[])
31 {
32 BEGIN_TEST();
33
34 /* Test the GET0_CHAIN feature probe. Modern libcryptos such as AWS-LC should support
35 * X509_STORE_CTX_get0_chain().
36 */
37 if (s2n_libcrypto_is_awslc()) {
38 EXPECT_TRUE(s2n_libcrypto_supports_get0_chain());
39 }
40
41 /* Test s2n_x509_validator_get_validated_cert_chain(). */
42 {
43 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL, s2n_cert_chain_and_key_ptr_free);
44 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
45 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
46
47 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new_minimal(), s2n_config_ptr_free);
48 EXPECT_NOT_NULL(config);
49 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
50 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(config, S2N_DEFAULT_TEST_CERT_CHAIN, NULL));
51
52 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
53 EXPECT_NOT_NULL(server_conn);
54 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
55
56 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
57 s2n_connection_ptr_free);
58 EXPECT_NOT_NULL(client_conn);
59 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
60 EXPECT_SUCCESS(s2n_set_server_name(client_conn, "localhost"));
61
62 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
63 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
64 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
65 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
66
67 /* An error should be raised if there's no validated cert chain to get. */
68 struct s2n_validated_cert_chain validated_cert_chain = { 0 };
69 EXPECT_ERROR_WITH_ERRNO(s2n_x509_validator_get_validated_cert_chain(
70 &client_conn->x509_validator, &validated_cert_chain),
71 S2N_ERR_INVALID_CERT_STATE);
72 EXPECT_NULL(validated_cert_chain.stack);
73
74 /* Perform a handshake to validate a cert chain. */
75 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
76
77 /* Get and free the validated cert chain repeatedly. This ensures that the internal state
78 * of the X509_STORE_CTX isn't accidentally freed.
79 */
80 for (size_t i = 0; i < 10; i++) {
81 EXPECT_OK(s2n_x509_validator_get_validated_cert_chain(&client_conn->x509_validator, &validated_cert_chain));
82 EXPECT_NOT_NULL(validated_cert_chain.stack);
83
84 EXPECT_OK(s2n_x509_validator_validated_cert_chain_free(&validated_cert_chain));
85 EXPECT_NULL(validated_cert_chain.stack);
86 }
87 }
88
89 END_TEST();
90 }