Debian Codegraph @

crt/s2n/tests/unit/s2n_cert_chain_and_key_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 <errno.h>
17 #include <fcntl.h>
18
19 #include "api/s2n.h"
20 #include "crypto/s2n_fips.h"
21 #include "s2n_test.h"
22 #include "testlib/s2n_testlib.h"
23 #include "utils/s2n_safety.h"
24
25 #define NUM_TIED_CERTS 100
26
27 struct s2n_connection *create_conn(s2n_mode mode, struct s2n_config *config)
28 {
29 PTR_GUARD_RESULT(s2n_config_set_tls12_security_policy(config));
30 struct s2n_connection *conn = s2n_connection_new(mode);
31 PTR_GUARD_POSIX(s2n_connection_set_config(conn, config));
32 return conn;
33 }
34
35 static int num_times_cb_executed = 0;
36 static struct s2n_cert_chain_and_key *test_cert_tiebreak_cb(struct s2n_cert_chain_and_key *cert1,
37 struct s2n_cert_chain_and_key *cert2,
38 uint8_t *name,
39 uint32_t name_len)
40 {
41 const int priority1 = *((int *) s2n_cert_chain_and_key_get_ctx(cert1));
42 const int priority2 = *((int *) s2n_cert_chain_and_key_get_ctx(cert2));
43 num_times_cb_executed++;
44 return (priority1 > priority2 ? cert1 : cert2);
45 }
46
47 int main(int argc, char **argv)
48 {
49 struct s2n_config *server_config = NULL;
50 struct s2n_config *client_config = NULL;
51 struct s2n_connection *server_conn = NULL;
52 struct s2n_connection *client_conn = NULL;
53 char *alligator_cert = NULL;
54 char *alligator_key = NULL;
55 char *cert_chain = NULL;
56 char *private_key = NULL;
57
58 BEGIN_TEST();
59 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
60
61 struct s2n_test_io_pair io_pair;
62 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
63
64 EXPECT_NOT_NULL(alligator_cert = malloc(S2N_MAX_TEST_PEM_SIZE));
65 EXPECT_NOT_NULL(alligator_key = malloc(S2N_MAX_TEST_PEM_SIZE));
66 EXPECT_SUCCESS(s2n_read_test_pem(S2N_ALLIGATOR_SAN_CERT, alligator_cert, S2N_MAX_TEST_PEM_SIZE));
67 EXPECT_SUCCESS(s2n_read_test_pem(S2N_ALLIGATOR_SAN_KEY, alligator_key, S2N_MAX_TEST_PEM_SIZE));
68 EXPECT_NOT_NULL(cert_chain = malloc(S2N_MAX_TEST_PEM_SIZE));
69 EXPECT_NOT_NULL(private_key = malloc(S2N_MAX_TEST_PEM_SIZE));
70 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_CERT_CHAIN, cert_chain, S2N_MAX_TEST_PEM_SIZE));
71 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_PRIVATE_KEY, private_key, S2N_MAX_TEST_PEM_SIZE));
72
73 EXPECT_SUCCESS(setenv("S2N_DONT_MLOCK", "1", 0));
74
75 EXPECT_NOT_NULL(client_config = s2n_config_new());
76 EXPECT_SUCCESS(s2n_config_disable_x509_verification(client_config));
77 /* Create config with s2n_config_add_cert_chain_and_key_to_store API with multiple certs */
78 {
79 struct s2n_cert_chain_and_key *default_cert = NULL;
80 /* Associated data to attach to each certificate to use in the tiebreak callback. */
81 int tiebreak_priorites[NUM_TIED_CERTS] = { 0 };
82 /* Collection of certs with the same domain name that need to have ties resolved. */
83 struct s2n_cert_chain_and_key *tied_certs[NUM_TIED_CERTS] = { NULL };
84 EXPECT_NOT_NULL(server_config = s2n_config_new());
85 EXPECT_SUCCESS(s2n_config_set_cert_tiebreak_callback(server_config, test_cert_tiebreak_cb));
86
87 /* Need to add at least one cert with a different domain name to make cert lookup utilize hashmap */
88 EXPECT_NOT_NULL(default_cert = s2n_cert_chain_and_key_new());
89 EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(default_cert, cert_chain, private_key));
90 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, default_cert));
91
92 /* Add NUM_TIED_CERTS that are actually the same certificate(www.alligator.com) to trigger the tiebreak callback. */
93 for (unsigned int i = 0; i < NUM_TIED_CERTS; i++) {
94 EXPECT_NOT_NULL(tied_certs[i] = s2n_cert_chain_and_key_new());
95 EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(tied_certs[i], alligator_cert, alligator_key));
96 tiebreak_priorites[i] = i;
97 EXPECT_SUCCESS(s2n_cert_chain_and_key_set_ctx(tied_certs[i], (void *) &tiebreak_priorites[i]));
98 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, tied_certs[i]));
99 }
100
101 EXPECT_NOT_NULL(server_conn = create_conn(S2N_SERVER, server_config));
102 EXPECT_NOT_NULL(client_conn = create_conn(S2N_CLIENT, client_config));
103 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
104 EXPECT_SUCCESS(s2n_set_server_name(client_conn, "www.alligator.com"));
105 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
106 EXPECT_TRUE(IS_FULL_HANDSHAKE(server_conn));
107 EXPECT_EQUAL(num_times_cb_executed, NUM_TIED_CERTS - 1);
108 struct s2n_cert_chain_and_key *selected_cert = s2n_connection_get_selected_cert(server_conn);
109 /* The last alligator certificate should have the highest priority */
110 EXPECT_EQUAL(selected_cert, tied_certs[(NUM_TIED_CERTS - 1)]);
111 EXPECT_EQUAL(s2n_cert_chain_and_key_get_ctx(selected_cert), (void *) &tiebreak_priorites[(NUM_TIED_CERTS - 1)]);
112 EXPECT_EQUAL(*((int *) s2n_cert_chain_and_key_get_ctx(selected_cert)), NUM_TIED_CERTS - 1);
113 EXPECT_SUCCESS(s2n_shutdown_test_server_and_client(server_conn, client_conn));
114
115 EXPECT_SUCCESS(s2n_connection_free(server_conn));
116 EXPECT_SUCCESS(s2n_connection_free(client_conn));
117 for (size_t i = 0; i < NUM_TIED_CERTS; i++) {
118 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(tied_certs[i]));
119 }
120 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(default_cert));
121 EXPECT_SUCCESS(s2n_config_free(server_config));
122 };
123
124 /* Create config with deprecated s2n_config_add_cert_chain_and_key API */
125 {
126 EXPECT_NOT_NULL(server_config = s2n_config_new());
127 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key(server_config, cert_chain, private_key));
128
129 EXPECT_NOT_NULL(server_conn = create_conn(S2N_SERVER, server_config));
130 EXPECT_NOT_NULL(client_conn = create_conn(S2N_CLIENT, client_config));
131 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
132
133 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
134 EXPECT_TRUE(IS_FULL_HANDSHAKE(server_conn));
135 EXPECT_SUCCESS(s2n_shutdown_test_server_and_client(server_conn, client_conn));
136
137 EXPECT_SUCCESS(s2n_connection_free(server_conn));
138 EXPECT_SUCCESS(s2n_connection_free(client_conn));
139 EXPECT_SUCCESS(s2n_config_free(server_config));
140 };
141
142 /* Do not allow configs to call both
143 * s2n_config_add_cert_chain_and_key and s2n_config_add_cert_chain_and_key_to_store */
144 {
145 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain = NULL,
146 s2n_cert_chain_and_key_ptr_free);
147 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain,
148 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
149
150 /* Config first uses s2n_config_add_cert_chain_and_key: library owns chain */
151 {
152 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
153 EXPECT_NOT_NULL(config);
154 EXPECT_EQUAL(config->cert_ownership, S2N_NOT_OWNED);
155
156 /* Add first chain */
157 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key(config, cert_chain, private_key));
158 EXPECT_EQUAL(config->cert_ownership, S2N_LIB_OWNED);
159
160 /* Try to add second chain of same type.
161 * The deprecated API only allows a single call, so this is
162 * rejected at the ownership check.
163 */
164 EXPECT_FAILURE_WITH_ERRNO(s2n_config_add_cert_chain_and_key(config, cert_chain, private_key),
165 S2N_ERR_CERT_OWNERSHIP);
166 EXPECT_EQUAL(config->cert_ownership, S2N_LIB_OWNED);
167
168 /* Try to add chain using other method */
169 EXPECT_FAILURE_WITH_ERRNO(s2n_config_add_cert_chain_and_key_to_store(config, chain),
170 S2N_ERR_CERT_OWNERSHIP);
171 EXPECT_EQUAL(config->cert_ownership, S2N_LIB_OWNED);
172 };
173
174 /* Config first uses s2n_config_add_cert_chain_and_key_to_store: application owns chain */
175 {
176 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
177 EXPECT_NOT_NULL(config);
178 EXPECT_EQUAL(config->cert_ownership, S2N_NOT_OWNED);
179
180 /* Add first chain */
181 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain));
182 EXPECT_EQUAL(config->cert_ownership, S2N_APP_OWNED);
183
184 /* Add second chain */
185 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain));
186 EXPECT_EQUAL(config->cert_ownership, S2N_APP_OWNED);
187
188 /* Try to add chain using other method */
189 EXPECT_FAILURE_WITH_ERRNO(s2n_config_add_cert_chain_and_key(config, cert_chain, private_key),
190 S2N_ERR_CERT_OWNERSHIP);
191 EXPECT_EQUAL(config->cert_ownership, S2N_APP_OWNED);
192 };
193 };
194
195 /* s2n_cert_chain_and_key_load_pem */
196 {
197 /* when loading a chain, all certs have a info associated with them and root is self-signed */
198 {
199 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain = NULL,
200 s2n_cert_chain_and_key_ptr_free);
201 EXPECT_SUCCESS(s2n_test_cert_permutation_load_server_chain(&chain, "ec", "ecdsa",
202 "p384", "sha256"));
203 struct s2n_cert *leaf = chain->cert_chain->head;
204 EXPECT_EQUAL(leaf->info.self_signed, false);
205 EXPECT_EQUAL(leaf->info.signature_nid, NID_ecdsa_with_SHA256);
206 EXPECT_EQUAL(leaf->info.signature_digest_nid, NID_sha256);
207
208 struct s2n_cert *intermediate = leaf->next;
209 EXPECT_NOT_NULL(intermediate);
210 EXPECT_EQUAL(intermediate->info.self_signed, false);
211 EXPECT_EQUAL(intermediate->info.signature_nid, NID_ecdsa_with_SHA256);
212 EXPECT_EQUAL(intermediate->info.signature_digest_nid, NID_sha256);
213
214 struct s2n_cert *root = intermediate->next;
215 EXPECT_NOT_NULL(intermediate);
216 EXPECT_NULL(root->next);
217 EXPECT_EQUAL(root->info.self_signed, true);
218 EXPECT_EQUAL(root->info.signature_nid, NID_ecdsa_with_SHA256);
219 EXPECT_EQUAL(root->info.signature_digest_nid, NID_sha256);
220 };
221 };
222
223 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
224 EXPECT_SUCCESS(s2n_config_free(client_config));
225
226 free(cert_chain);
227 free(private_key);
228 free(alligator_cert);
229 free(alligator_key);
230 END_TEST();
231 }