Debian Codegraph @

crt/s2n/tests/unit/s2n_x509_validator_custom_oids_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 "api/unstable/custom_x509_extensions.h"
17 #include "crypto/s2n_libcrypto.h"
18 #include "s2n_test.h"
19 #include "testlib/s2n_testlib.h"
20 #include "tls/s2n_config.h"
21
22 #define S2N_SINGLE_OID_CERT_CHAIN "../pems/custom_oids/single_oid_cert_chain.pem"
23 #define S2N_SINGLE_OID_KEY "../pems/custom_oids/single_oid_key.pem"
24
25 #define S2N_MULTIPLE_OIDS_CERT_CHAIN "../pems/custom_oids/multiple_oids_cert_chain.pem"
26 #define S2N_MULTIPLE_OIDS_KEY "../pems/custom_oids/multiple_oids_key.pem"
27
28 const char *invalid_oid[] = { "1.3.4.2" };
29 const char *single_oid[] = { "1.3.187.25240.2" };
30 const char *multiple_oids[] = { "1.3.187.25240.2", "1.3.187.25240.3" };
31 const uint32_t multiple_oid_count = s2n_array_len(multiple_oids);
32
33 int main(int argc, char *argv[])
34 {
35 BEGIN_TEST();
36
37 /* Test adding custom critical cert extensions.
38 *
39 * By default, s2n-tls will reject certificates with custom critical extensions. Calling
40 * s2n_config_add_custom_x509_extension() allows s2n-tls to ingore these extensions during validation.
41 * Validation should succeed when added custom critical extensions match the ones provided in the certificate.
42 */
43 {
44 /* Safety Check */
45 {
46 EXPECT_FAILURE_WITH_ERRNO(s2n_config_add_custom_x509_extension(NULL, (uint8_t *) single_oid[0], strlen(single_oid[0])),
47 S2N_ERR_INVALID_ARGUMENT);
48
49 DEFER_CLEANUP(struct s2n_config *test_config = s2n_config_new_minimal(), s2n_config_ptr_free);
50 EXPECT_NOT_NULL(test_config);
51 EXPECT_FAILURE_WITH_ERRNO(s2n_config_add_custom_x509_extension(test_config, NULL, 0), S2N_ERR_INVALID_ARGUMENT);
52 }
53
54 /* Ensure custom extensions can't be added when linked to an unsupported libcrypto. */
55 if (!s2n_libcrypto_supports_custom_oid()) {
56 DEFER_CLEANUP(struct s2n_config *test_config = s2n_config_new_minimal(), s2n_config_ptr_free);
57 EXPECT_NOT_NULL(test_config);
58 EXPECT_FAILURE_WITH_ERRNO(s2n_config_add_custom_x509_extension(test_config, (uint8_t *) single_oid[0], strlen(single_oid[0])),
59 S2N_ERR_API_UNSUPPORTED_BY_LIBCRYPTO);
60
61 END_TEST();
62 }
63
64 /* clang-format off */
65 struct {
66 const char *cert_pem_path;
67 const char *key_pem_path;
68 const char **custom_critical_oids;
69 uint32_t custom_oid_count;
70 unsigned set_oids : 1;
71 s2n_error expected_error;
72 } test_cases[] = {
73 /* Receive a certificate with a single custom extension. */
74 {
75 .cert_pem_path = S2N_SINGLE_OID_CERT_CHAIN,
76 .key_pem_path = S2N_SINGLE_OID_KEY,
77 .custom_critical_oids = single_oid,
78 .custom_oid_count = 1,
79 .set_oids = true,
80 .expected_error = S2N_ERR_OK,
81 },
82
83 /* Receive a certificate with multiple custom extensions. */
84 {
85 .cert_pem_path = S2N_MULTIPLE_OIDS_CERT_CHAIN,
86 .key_pem_path = S2N_MULTIPLE_OIDS_KEY,
87 .custom_critical_oids = multiple_oids,
88 .custom_oid_count = multiple_oid_count,
89 .set_oids = true,
90 .expected_error = S2N_ERR_OK,
91 },
92
93 /* Validation should fail without calling s2n_config_add_custom_x509_extension(). */
94 {
95 .cert_pem_path = S2N_MULTIPLE_OIDS_CERT_CHAIN,
96 .key_pem_path = S2N_MULTIPLE_OIDS_KEY,
97 .custom_critical_oids = multiple_oids,
98 .custom_oid_count = multiple_oid_count,
99 .set_oids = false,
100 .expected_error = S2N_ERR_CERT_UNHANDLED_CRITICAL_EXTENSION,
101 },
102
103 /* Validation should fail with mismatched custom extensions. */
104 {
105 .cert_pem_path = S2N_SINGLE_OID_CERT_CHAIN,
106 .key_pem_path = S2N_SINGLE_OID_KEY,
107 .custom_critical_oids = invalid_oid,
108 .custom_oid_count = 1,
109 .set_oids = true,
110 .expected_error = S2N_ERR_CERT_UNHANDLED_CRITICAL_EXTENSION,
111 },
112
113 /* Validation should fail with missing custom extensions. */
114 {
115 .cert_pem_path = S2N_MULTIPLE_OIDS_CERT_CHAIN,
116 .key_pem_path = S2N_MULTIPLE_OIDS_KEY,
117 .custom_critical_oids = single_oid,
118 .custom_oid_count = 1,
119 .set_oids = true,
120 .expected_error = S2N_ERR_CERT_UNHANDLED_CRITICAL_EXTENSION,
121 },
122 };
123 /* clang-format on */
124
125 /* Self-talk: add custom critical extensions for server auth */
126 for (int test_idx = 0; test_idx < s2n_array_len(test_cases); test_idx++) {
127 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL, s2n_cert_chain_and_key_ptr_free);
128 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
129 test_cases[test_idx].cert_pem_path, test_cases[test_idx].key_pem_path));
130
131 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
132 EXPECT_NOT_NULL(config);
133 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
134 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(config, test_cases[test_idx].cert_pem_path, NULL));
135 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default"));
136
137 const char **custom_oids = test_cases[test_idx].custom_critical_oids;
138 uint32_t custom_oid_count = test_cases[test_idx].custom_oid_count;
139
140 EXPECT_NULL(config->custom_x509_extension_oids);
141 if (test_cases[test_idx].set_oids) {
142 for (int i = 0; i < custom_oid_count; i++) {
143 uint8_t *custom_oid = (uint8_t *) custom_oids[i];
144 EXPECT_SUCCESS(s2n_config_add_custom_x509_extension(config, custom_oid, strlen(custom_oids[i])));
145 }
146 EXPECT_EQUAL(sk_ASN1_OBJECT_num(config->custom_x509_extension_oids), custom_oid_count);
147 }
148
149 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
150 EXPECT_NOT_NULL(server_conn);
151 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
152
153 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
154 EXPECT_NOT_NULL(client_conn);
155 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
156 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
157 EXPECT_SUCCESS(s2n_set_server_name(client_conn, "localhost"));
158
159 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
160 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
161 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
162 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
163
164 s2n_error expected_error = test_cases[test_idx].expected_error;
165 if (expected_error == S2N_ERR_OK) {
166 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
167 } else {
168 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, client_conn),
169 expected_error);
170 }
171 }
172
173 /* Self-talk: add custom critical extensions for client auth */
174 for (int test_idx = 0; test_idx < s2n_array_len(test_cases); test_idx++) {
175 DEFER_CLEANUP(struct s2n_cert_chain_and_key *default_chain_and_key = NULL, s2n_cert_chain_and_key_ptr_free);
176 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&default_chain_and_key,
177 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
178
179 DEFER_CLEANUP(struct s2n_config *server_config = s2n_config_new(), s2n_config_ptr_free);
180 EXPECT_NOT_NULL(server_config);
181 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, default_chain_and_key));
182 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(server_config, test_cases[test_idx].cert_pem_path, NULL));
183 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config, "default"));
184 EXPECT_SUCCESS(s2n_config_set_client_auth_type(server_config, S2N_CERT_AUTH_REQUIRED));
185
186 const char **custom_oids = test_cases[test_idx].custom_critical_oids;
187 uint32_t custom_oid_count = test_cases[test_idx].custom_oid_count;
188
189 EXPECT_NULL(server_config->custom_x509_extension_oids);
190 if (test_cases[test_idx].set_oids) {
191 for (int i = 0; i < custom_oid_count; i++) {
192 uint8_t *custom_oid = (uint8_t *) custom_oids[i];
193 EXPECT_SUCCESS(s2n_config_add_custom_x509_extension(server_config, custom_oid, strlen(custom_oids[i])));
194 }
195 EXPECT_EQUAL(sk_ASN1_OBJECT_num(server_config->custom_x509_extension_oids), custom_oid_count);
196 }
197
198 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
199 EXPECT_NOT_NULL(server_conn);
200 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
201 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
202
203 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL, s2n_cert_chain_and_key_ptr_free);
204 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
205 test_cases[test_idx].cert_pem_path, test_cases[test_idx].key_pem_path));
206
207 DEFER_CLEANUP(struct s2n_config *client_config = s2n_config_new(), s2n_config_ptr_free);
208 EXPECT_NOT_NULL(client_config);
209 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(client_config, chain_and_key));
210 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(client_config, S2N_DEFAULT_TEST_CERT_CHAIN, NULL));
211 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "default"));
212 EXPECT_SUCCESS(s2n_config_set_client_auth_type(client_config, S2N_CERT_AUTH_OPTIONAL));
213
214 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
215 EXPECT_NOT_NULL(client_conn);
216 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
217 EXPECT_SUCCESS(s2n_set_server_name(client_conn, "localhost"));
218
219 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
220 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
221 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
222 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
223
224 s2n_error expected_error = test_cases[test_idx].expected_error;
225 if (expected_error == S2N_ERR_OK) {
226 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
227 } else {
228 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, client_conn),
229 expected_error);
230 }
231
232 /* Verify that both connections negotiated mutual auth. */
233 EXPECT_TRUE(IS_CLIENT_AUTH_HANDSHAKE(server_conn));
234 EXPECT_TRUE(IS_CLIENT_AUTH_HANDSHAKE(client_conn));
235 }
236 }
237
238 END_TEST();
239 }