Debian Codegraph @

crt/s2n/tests/unit/s2n_x509_validator_time_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 "crypto/s2n_libcrypto.h"
17 #include "s2n_test.h"
18 #include "testlib/s2n_testlib.h"
19
20 bool s2n_libcrypto_supports_flag_no_check_time();
21 uint64_t s2n_libcrypto_awslc_api_version(void);
22
23 static uint8_t s2n_verify_host_accept_everything(const char *host_name, size_t host_name_len, void *data)
24 {
25 return 1;
26 }
27
28 int main(int argc, char *argv[])
29 {
30 BEGIN_TEST();
31
32 /* Test the NO_CHECK_TIME flag feature probe. The flag was added to AWS-LC in API version 19. */
33 if (s2n_libcrypto_is_awslc() && s2n_libcrypto_awslc_api_version() > 19) {
34 EXPECT_TRUE(s2n_libcrypto_supports_flag_no_check_time());
35 }
36
37 /* Test disabling x509 time validation.
38 *
39 * By default, validation should fail for certificates with invalid timestamps. However, if
40 * x509 time validation is disabled, validation should succeed.
41 *
42 * When time validation is disabled, s2n_config_set_wall_clock() will not set a custom time on
43 * the libcrypto, so this function cannot be used to set a fake time for testing. Instead, the
44 * test certificates themselves contain invalid timestamps.
45 */
46 {
47 /* clang-format off */
48 struct {
49 const char *cert_pem_path;
50 const char *key_pem_path;
51 bool disable_x509_time_validation;
52 s2n_error expected_error;
53 } test_cases[] = {
54 /* Validation should fail for a certificate that is not yet valid. */
55 {
56 .cert_pem_path = S2N_NOT_YET_VALID_CERT_CHAIN,
57 .key_pem_path = S2N_NOT_YET_VALID_KEY,
58 .disable_x509_time_validation = false,
59 .expected_error = S2N_ERR_CERT_NOT_YET_VALID,
60 },
61
62 /* Validation should succeed for a certificate that is not yet valid when time
63 * validation is disabled.
64 */
65 {
66 .cert_pem_path = S2N_NOT_YET_VALID_CERT_CHAIN,
67 .key_pem_path = S2N_NOT_YET_VALID_KEY,
68 .disable_x509_time_validation = true,
69 .expected_error = S2N_ERR_OK,
70 },
71
72 /* Validation should fail for an expired certificate. */
73 {
74 .cert_pem_path = S2N_EXPIRED_CERT_CHAIN,
75 .key_pem_path = S2N_EXPIRED_KEY,
76 .disable_x509_time_validation = false,
77 .expected_error = S2N_ERR_CERT_EXPIRED,
78 },
79
80 /* Validation should succeed for an expired certificate when time validation is
81 * disabled.
82 */
83 {
84 .cert_pem_path = S2N_EXPIRED_CERT_CHAIN,
85 .key_pem_path = S2N_EXPIRED_KEY,
86 .disable_x509_time_validation = true,
87 .expected_error = S2N_ERR_OK,
88 },
89 };
90 /* clang-format on */
91
92 /* s2n_x509_validator test */
93 for (int i = 0; i < s2n_array_len(test_cases); i++) {
94 DEFER_CLEANUP(struct s2n_x509_trust_store trust_store = { 0 }, s2n_x509_trust_store_wipe);
95 s2n_x509_trust_store_init_empty(&trust_store);
96
97 char cert_chain[S2N_MAX_TEST_PEM_SIZE] = { 0 };
98 EXPECT_SUCCESS(s2n_read_test_pem(test_cases[i].cert_pem_path, cert_chain, S2N_MAX_TEST_PEM_SIZE));
99 EXPECT_SUCCESS(s2n_x509_trust_store_add_pem(&trust_store, cert_chain));
100
101 DEFER_CLEANUP(struct s2n_x509_validator validator, s2n_x509_validator_wipe);
102 EXPECT_SUCCESS(s2n_x509_validator_init(&validator, &trust_store, 0));
103
104 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
105 EXPECT_NOT_NULL(config);
106 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "20240501"));
107
108 if (test_cases[i].disable_x509_time_validation) {
109 EXPECT_SUCCESS(s2n_config_disable_x509_time_verification(config));
110 }
111
112 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
113 EXPECT_NOT_NULL(conn);
114 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
115 EXPECT_SUCCESS(s2n_set_server_name(conn, "localhost"));
116
117 DEFER_CLEANUP(struct s2n_stuffer cert_chain_stuffer = { 0 }, s2n_stuffer_free);
118 EXPECT_OK(s2n_test_cert_chain_data_from_pem(conn, test_cases[i].cert_pem_path, &cert_chain_stuffer));
119 uint32_t chain_len = s2n_stuffer_data_available(&cert_chain_stuffer);
120 uint8_t *chain_data = s2n_stuffer_raw_read(&cert_chain_stuffer, chain_len);
121 EXPECT_NOT_NULL(chain_data);
122
123 DEFER_CLEANUP(struct s2n_pkey public_key_out = { 0 }, s2n_pkey_free);
124 EXPECT_SUCCESS(s2n_pkey_zero_init(&public_key_out));
125 s2n_pkey_type pkey_type = S2N_PKEY_TYPE_UNKNOWN;
126
127 s2n_result ret = s2n_x509_validator_validate_cert_chain(&validator, conn, chain_data, chain_len, &pkey_type,
128 &public_key_out);
129
130 s2n_error expected_error = test_cases[i].expected_error;
131 if (expected_error == S2N_ERR_OK) {
132 EXPECT_OK(ret);
133 } else {
134 EXPECT_ERROR_WITH_ERRNO(ret, expected_error);
135 }
136 }
137
138 /* Self-talk: Disable validity period validation on client for server auth */
139 for (int i = 0; i < s2n_array_len(test_cases); i++) {
140 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL, s2n_cert_chain_and_key_ptr_free);
141 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
142 test_cases[i].cert_pem_path, test_cases[i].key_pem_path));
143
144 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
145 EXPECT_NOT_NULL(config);
146 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
147 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(config, test_cases[i].cert_pem_path, NULL));
148 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "20240501"));
149
150 if (test_cases[i].disable_x509_time_validation) {
151 EXPECT_SUCCESS(s2n_config_disable_x509_time_verification(config));
152 }
153
154 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
155 EXPECT_NOT_NULL(server_conn);
156 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
157
158 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
159 EXPECT_NOT_NULL(client_conn);
160 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
161 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
162 EXPECT_SUCCESS(s2n_set_server_name(client_conn, "localhost"));
163
164 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
165 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
166 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
167 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
168
169 int ret = s2n_negotiate_test_server_and_client(server_conn, client_conn);
170
171 s2n_error expected_error = test_cases[i].expected_error;
172 if (expected_error == S2N_ERR_OK) {
173 EXPECT_SUCCESS(ret);
174 } else {
175 EXPECT_FAILURE_WITH_ERRNO(ret, expected_error);
176 }
177 }
178
179 /* Self-talk: Disable validity period validation on server for client auth */
180 for (int i = 0; i < s2n_array_len(test_cases); i++) {
181 DEFER_CLEANUP(struct s2n_cert_chain_and_key *default_chain_and_key = NULL, s2n_cert_chain_and_key_ptr_free);
182 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&default_chain_and_key,
183 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
184
185 DEFER_CLEANUP(struct s2n_config *server_config = s2n_config_new(), s2n_config_ptr_free);
186 EXPECT_NOT_NULL(server_config);
187 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, default_chain_and_key));
188 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(server_config, test_cases[i].cert_pem_path, NULL));
189 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config, "20240501"));
190 EXPECT_SUCCESS(s2n_config_set_client_auth_type(server_config, S2N_CERT_AUTH_REQUIRED));
191
192 if (test_cases[i].disable_x509_time_validation) {
193 EXPECT_SUCCESS(s2n_config_disable_x509_time_verification(server_config));
194 }
195
196 /* Disable verify host validation for client auth */
197 EXPECT_SUCCESS(s2n_config_set_verify_host_callback(server_config, s2n_verify_host_accept_everything, NULL));
198
199 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
200 EXPECT_NOT_NULL(server_conn);
201 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
202 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
203
204 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL, s2n_cert_chain_and_key_ptr_free);
205 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
206 test_cases[i].cert_pem_path, test_cases[i].key_pem_path));
207
208 DEFER_CLEANUP(struct s2n_config *client_config = s2n_config_new(), s2n_config_ptr_free);
209 EXPECT_NOT_NULL(client_config);
210 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(client_config, chain_and_key));
211 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(client_config, S2N_DEFAULT_TEST_CERT_CHAIN, NULL));
212 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "20240501"));
213 EXPECT_SUCCESS(s2n_config_set_client_auth_type(client_config, S2N_CERT_AUTH_OPTIONAL));
214
215 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
216 EXPECT_NOT_NULL(client_conn);
217 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
218 EXPECT_SUCCESS(s2n_set_server_name(client_conn, "localhost"));
219
220 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
221 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
222 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
223 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
224
225 int ret = s2n_negotiate_test_server_and_client(server_conn, client_conn);
226
227 s2n_error expected_error = test_cases[i].expected_error;
228 if (expected_error == S2N_ERR_OK) {
229 EXPECT_SUCCESS(ret);
230 } else {
231 EXPECT_FAILURE_WITH_ERRNO(ret, expected_error);
232 }
233 }
234 }
235
236 /* Ensure that certificate validation can fail for reasons other than time validation when time
237 * validation is disabled.
238 */
239 for (int trust_cert = 0; trust_cert <= 1; trust_cert += 1) {
240 DEFER_CLEANUP(struct s2n_x509_trust_store trust_store = { 0 }, s2n_x509_trust_store_wipe);
241 s2n_x509_trust_store_init_empty(&trust_store);
242
243 if (trust_cert) {
244 char cert_chain[S2N_MAX_TEST_PEM_SIZE] = { 0 };
245 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_CERT_CHAIN, cert_chain, S2N_MAX_TEST_PEM_SIZE));
246 EXPECT_SUCCESS(s2n_x509_trust_store_add_pem(&trust_store, cert_chain));
247 }
248
249 DEFER_CLEANUP(struct s2n_x509_validator validator, s2n_x509_validator_wipe);
250 EXPECT_SUCCESS(s2n_x509_validator_init(&validator, &trust_store, 0));
251
252 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
253 EXPECT_NOT_NULL(config);
254 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "20240501"));
255 EXPECT_SUCCESS(s2n_config_disable_x509_time_verification(config));
256
257 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
258 EXPECT_NOT_NULL(conn);
259 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
260 EXPECT_SUCCESS(s2n_set_server_name(conn, "localhost"));
261
262 DEFER_CLEANUP(struct s2n_stuffer cert_chain_stuffer = { 0 }, s2n_stuffer_free);
263 EXPECT_OK(s2n_test_cert_chain_data_from_pem(conn, S2N_DEFAULT_TEST_CERT_CHAIN, &cert_chain_stuffer));
264 uint32_t chain_len = s2n_stuffer_data_available(&cert_chain_stuffer);
265 uint8_t *chain_data = s2n_stuffer_raw_read(&cert_chain_stuffer, chain_len);
266 EXPECT_NOT_NULL(chain_data);
267
268 DEFER_CLEANUP(struct s2n_pkey public_key_out = { 0 }, s2n_pkey_free);
269 EXPECT_SUCCESS(s2n_pkey_zero_init(&public_key_out));
270 s2n_pkey_type pkey_type = S2N_PKEY_TYPE_UNKNOWN;
271
272 s2n_result ret = s2n_x509_validator_validate_cert_chain(&validator, conn, chain_data, chain_len, &pkey_type,
273 &public_key_out);
274
275 if (trust_cert) {
276 EXPECT_OK(ret);
277 } else {
278 /* If the certificate was not added to the trust store, validation should fail even
279 * though time validation was disabled.
280 */
281 EXPECT_ERROR_WITH_ERRNO(ret, S2N_ERR_CERT_UNTRUSTED);
282 }
283 }
284
285 END_TEST();
286 }