Debian Codegraph @

crt/s2n/tests/unit/s2n_cert_authorities_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 "tls/extensions/s2n_cert_authorities.h"
17
18 #include "crypto/s2n_libcrypto.h"
19 #include "crypto/s2n_rsa_pss.h"
20 #include "s2n_test.h"
21 #include "testlib/s2n_testlib.h"
22 #include "tls/s2n_tls.h"
23 #include "utils/s2n_bitmap.h"
24
25 int s2n_parse_client_hello(struct s2n_connection *conn);
26
27 int main(int argc, char **argv)
28 {
29 BEGIN_TEST();
30
31 DEFER_CLEANUP(struct s2n_cert_chain_and_key *cert_chain = NULL,
32 s2n_cert_chain_and_key_ptr_free);
33 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&cert_chain,
34 S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY));
35
36 s2n_extension_type_id temp_id = s2n_unsupported_extension;
37 EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(
38 s2n_cert_authorities_extension.iana_value, &temp_id));
39 const s2n_extension_type_id ca_ext_id = temp_id;
40
41 /* Test: awslc should always support loading from the trust store */
42 if (s2n_libcrypto_is_awslc()) {
43 EXPECT_TRUE(s2n_cert_authorities_supported_from_trust_store());
44 }
45
46 /* Test: s2n_config_set_cert_authorities_from_trust_store */
47 {
48 /* Test: Safety */
49 {
50 EXPECT_FAILURE_WITH_ERRNO(
51 s2n_config_set_cert_authorities_from_trust_store(NULL),
52 S2N_ERR_NULL);
53 };
54
55 /* Test: fails if not supported */
56 {
57 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new_minimal(),
58 s2n_config_ptr_free);
59 EXPECT_NOT_NULL(config);
60 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(config,
61 S2N_ECDSA_P512_CERT_CHAIN, NULL));
62
63 if (s2n_cert_authorities_supported_from_trust_store()) {
64 EXPECT_SUCCESS(s2n_config_set_cert_authorities_from_trust_store(config));
65 EXPECT_NOT_EQUAL(config->cert_authorities.size, 0);
66 } else {
67 EXPECT_FAILURE_WITH_ERRNO(
68 s2n_config_set_cert_authorities_from_trust_store(config),
69 S2N_ERR_API_UNSUPPORTED_BY_LIBCRYPTO);
70 EXPECT_EQUAL(config->cert_authorities.size, 0);
71 }
72 };
73
74 /* Test: not allowed with system trust store */
75 {
76 /* s2n_config_new configures the default trust store */
77 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
78 EXPECT_NOT_NULL(config);
79
80 /* Fails with default system trust store */
81 EXPECT_FAILURE_WITH_ERRNO(
82 s2n_config_set_cert_authorities_from_trust_store(config),
83 S2N_ERR_INVALID_STATE);
84 EXPECT_EQUAL(config->cert_authorities.size, 0);
85
86 /* Succeeds again after wiping trust store */
87 EXPECT_SUCCESS(s2n_config_wipe_trust_store(config));
88 EXPECT_SUCCESS(s2n_config_set_cert_authorities_from_trust_store(config));
89 EXPECT_EQUAL(config->cert_authorities.size, 0);
90 };
91
92 /* Test: empty trust store */
93 {
94 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new_minimal(), s2n_config_ptr_free);
95 EXPECT_NOT_NULL(config);
96 EXPECT_SUCCESS(s2n_config_set_cert_authorities_from_trust_store(config));
97 EXPECT_EQUAL(config->cert_authorities.size, 0);
98 };
99
100 /* Test: too many CAs in trust store */
101 if (s2n_cert_authorities_supported_from_trust_store()) {
102 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new_minimal(), s2n_config_ptr_free);
103 EXPECT_NOT_NULL(config);
104 /* This is just a copy of the default trust store from an Amazon Linux instance */
105 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(config, S2N_TEST_TRUST_STORE, NULL));
106
107 EXPECT_FAILURE_WITH_ERRNO(
108 s2n_config_set_cert_authorities_from_trust_store(config),
109 S2N_ERR_TOO_MANY_CAS);
110 EXPECT_EQUAL(config->cert_authorities.size, 0);
111 };
112 };
113
114 /* Test: s2n_certificate_authorities_extension.send */
115 {
116 /* Test: writes whatever CA data is available */
117 {
118 const uint8_t ca_data[] = "these are my CAs";
119
120 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new_minimal(), s2n_config_ptr_free);
121 EXPECT_SUCCESS(s2n_alloc(&config->cert_authorities, sizeof(ca_data)));
122 EXPECT_MEMCPY_SUCCESS(config->cert_authorities.data, ca_data, sizeof(ca_data));
123
124 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
125 s2n_connection_ptr_free);
126 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
127 conn->actual_protocol_version = S2N_TLS13;
128
129 DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free);
130 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0));
131
132 EXPECT_SUCCESS(s2n_cert_authorities_extension.send(conn, &output));
133
134 uint16_t size = 0;
135 EXPECT_SUCCESS(s2n_stuffer_read_uint16(&output, &size));
136 EXPECT_EQUAL(size, sizeof(ca_data));
137 EXPECT_EQUAL(size, s2n_stuffer_data_available(&output));
138
139 uint8_t *data = s2n_stuffer_raw_read(&output, size);
140 EXPECT_NOT_NULL(data);
141 EXPECT_BYTEARRAY_EQUAL(data, ca_data, sizeof(ca_data));
142 };
143 };
144
145 /* Test: s2n_certificate_authorities_extension.should_send */
146 {
147 /* Test: do not send for TLS1.2 */
148 {
149 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new_minimal(), s2n_config_ptr_free);
150 EXPECT_SUCCESS(s2n_alloc(&config->cert_authorities, 10));
151
152 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
153 s2n_connection_ptr_free);
154 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
155
156 DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free);
157 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0));
158
159 conn->actual_protocol_version = S2N_TLS12;
160 EXPECT_SUCCESS(s2n_extension_send(&s2n_cert_authorities_extension,
161 conn, &output));
162 EXPECT_EQUAL(s2n_stuffer_data_available(&output), 0);
163
164 conn->actual_protocol_version = S2N_TLS13;
165 EXPECT_SUCCESS(s2n_extension_send(&s2n_cert_authorities_extension,
166 conn, &output));
167 EXPECT_NOT_EQUAL(s2n_stuffer_data_available(&output), 0);
168 };
169
170 /* Test: do not send if no CA data set */
171 {
172 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new_minimal(), s2n_config_ptr_free);
173
174 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
175 s2n_connection_ptr_free);
176 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
177
178 EXPECT_FALSE(s2n_cert_authorities_extension.should_send(conn));
179
180 EXPECT_SUCCESS(s2n_alloc(&config->cert_authorities, 10));
181 EXPECT_TRUE(s2n_cert_authorities_extension.should_send(conn));
182
183 EXPECT_SUCCESS(s2n_free(&config->cert_authorities));
184 EXPECT_FALSE(s2n_cert_authorities_extension.should_send(conn));
185 };
186 };
187
188 /* Test: ClientHello extension ignored
189 *
190 *= https://www.rfc-editor.org/rfc/rfc8446#section-4.2.4
191 *= type=test
192 *# The client MAY send the "certificate_authorities" extension in the
193 *# ClientHello message.
194 */
195 if (s2n_is_tls13_fully_supported()) {
196 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new_minimal(), s2n_config_ptr_free);
197 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config));
198 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, cert_chain));
199 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13"));
200 EXPECT_SUCCESS(s2n_alloc(&config->cert_authorities, 10));
201
202 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
203 s2n_connection_ptr_free);
204 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
205 EXPECT_SUCCESS(s2n_client_hello_send(client));
206 EXPECT_SUCCESS(s2n_parse_client_hello(client));
207 struct s2n_client_hello *client_hello = &client->client_hello;
208
209 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
210 s2n_connection_ptr_free);
211 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
212 struct s2n_stuffer *input = &server->handshake.io;
213
214 /* Copy ClientHello, except extensions */
215 size_t size_without_extensions = client_hello->raw_message.size
216 - client_hello->extensions.raw.size
217 - sizeof(uint16_t) /* Extensions size */;
218 EXPECT_SUCCESS(s2n_stuffer_write_bytes(input,
219 client_hello->raw_message.data, size_without_extensions));
220
221 /* Save space for new extensions size */
222 struct s2n_stuffer_reservation extensions_size = { 0 };
223 EXPECT_SUCCESS(s2n_stuffer_reserve_uint16(input, &extensions_size));
224
225 /* Write the certificate_authorities extension.
226 * The client isn't allowed to write it, so use the server.
227 */
228 server->actual_protocol_version = S2N_TLS13;
229 EXPECT_SUCCESS(s2n_extension_send(&s2n_cert_authorities_extension,
230 server, input));
231
232 /* Write the rest of the extensions */
233 EXPECT_SUCCESS(s2n_stuffer_write(input, &client_hello->extensions.raw));
234
235 /* Update the extensions size */
236 EXPECT_SUCCESS(s2n_stuffer_write_vector_size(&extensions_size));
237
238 /* Server should be able to successfully receive the modified ClientHello */
239 EXPECT_SUCCESS(s2n_client_hello_recv(server));
240 EXPECT_TRUE(server->client_hello.parsed);
241 EXPECT_EQUAL(server->actual_protocol_version, S2N_TLS13);
242
243 s2n_parsed_extension *extension = &server->client_hello.extensions.parsed_extensions[ca_ext_id];
244 EXPECT_TRUE(extension->extension.size > 0);
245 EXPECT_FALSE(extension->processed);
246 EXPECT_EQUAL(extension->extension_type, s2n_cert_authorities_extension.iana_value);
247 };
248
249 /* Self-talk test: CertificateRequest extension parsed
250 *
251 *= https://www.rfc-editor.org/rfc/rfc8446#section-4.2.4
252 *= type=test
253 *# The server MAY send it in the CertificateRequest message.
254 **/
255 if (s2n_is_tls13_fully_supported()) {
256 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new_minimal(), s2n_config_ptr_free);
257 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config));
258 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, cert_chain));
259 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13"));
260 EXPECT_SUCCESS(s2n_config_set_client_auth_type(config, S2N_CERT_AUTH_OPTIONAL));
261 EXPECT_SUCCESS(s2n_alloc(&config->cert_authorities, 10));
262
263 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
264 s2n_connection_ptr_free);
265 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
266
267 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
268 s2n_connection_ptr_free);
269 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
270
271 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
272 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
273 EXPECT_SUCCESS(s2n_connections_set_io_pair(client, server, &io_pair));
274
275 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));
276 EXPECT_EQUAL(server->actual_protocol_version, S2N_TLS13);
277 EXPECT_TRUE(IS_CLIENT_AUTH_HANDSHAKE(server));
278
279 /* Server sent extension */
280 EXPECT_TRUE(S2N_CBIT_TEST(server->extension_requests_sent, ca_ext_id));
281 EXPECT_FALSE(S2N_CBIT_TEST(server->extension_requests_received, ca_ext_id));
282
283 /* Client received extension */
284 EXPECT_FALSE(S2N_CBIT_TEST(client->extension_requests_sent, ca_ext_id));
285 EXPECT_TRUE(S2N_CBIT_TEST(client->extension_requests_received, ca_ext_id));
286 };
287
288 /* Known value test: compare our extension to openssl s_server */
289 if (s2n_is_rsa_pss_certs_supported() && s2n_cert_authorities_supported_from_trust_store()) {
290 /* clang-format off */
291 const struct {
292 const char *cert_name;
293 uint8_t expected_bytes_size;
294 uint8_t expected_bytes[1000];
295 } test_cases[] = {
296 {
297 .cert_name = S2N_RSA_PSS_2048_SHA256_LEAF_CERT,
298 .expected_bytes_size = 32,
299 .expected_bytes = {
300 0x00, 0x2f, 0x00, 0x1c, 0x00, 0x1a, 0x00, 0x18,
301 0x30, 0x16, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03,
302 0x55, 0x04, 0x03, 0x0c, 0x0b, 0x65, 0x78, 0x61,
303 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d
304 },
305 },
306 {
307 .cert_name = S2N_ECDSA_P512_CERT_CHAIN,
308 .expected_bytes_size = 107,
309 .expected_bytes = {
310 0x00, 0x2f, 0x00, 0x67, 0x00, 0x65, 0x00, 0x63,
311 0x30, 0x61, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03,
312 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
313 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x08,
314 0x0c, 0x02, 0x57, 0x41, 0x31, 0x10, 0x30, 0x0e,
315 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53,
316 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x0f,
317 0x30, 0x0d, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
318 0x06, 0x41, 0x6d, 0x61, 0x7a, 0x6f, 0x6e, 0x31,
319 0x0c, 0x30, 0x0a, 0x06, 0x03, 0x55, 0x04, 0x0b,
320 0x0c, 0x03, 0x73, 0x32, 0x6e, 0x31, 0x14, 0x30,
321 0x12, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0b,
322 0x73, 0x32, 0x6e, 0x54, 0x65, 0x73, 0x74, 0x43,
323 0x65, 0x72, 0x74
324 },
325 },
326 {
327 .cert_name = S2N_RSA_2048_SHA256_URI_SANS_CERT,
328 .expected_bytes_size = 192,
329 .expected_bytes = {
330 0x00, 0x2f, 0x00, 0xbc, 0x00, 0xba, 0x00, 0x53,
331 0x30, 0x51, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03,
332 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
333 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x08,
334 0x0c, 0x02, 0x57, 0x41, 0x31, 0x0f, 0x30, 0x0d,
335 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x06, 0x41,
336 0x6d, 0x61, 0x7a, 0x6f, 0x6e, 0x31, 0x0c, 0x30,
337 0x0a, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x03,
338 0x73, 0x32, 0x6e, 0x31, 0x16, 0x30, 0x14, 0x06,
339 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0d, 0x73, 0x32,
340 0x6e, 0x54, 0x65, 0x73, 0x74, 0x53, 0x65, 0x72,
341 0x76, 0x65, 0x72, 0x00, 0x63, 0x30, 0x61, 0x31,
342 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06,
343 0x13, 0x02, 0x55, 0x53, 0x31, 0x0b, 0x30, 0x09,
344 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x02, 0x57,
345 0x41, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
346 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74,
347 0x74, 0x6c, 0x65, 0x31, 0x0f, 0x30, 0x0d, 0x06,
348 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x06, 0x41, 0x6d,
349 0x61, 0x7a, 0x6f, 0x6e, 0x31, 0x0c, 0x30, 0x0a,
350 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x03, 0x73,
351 0x32, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03,
352 0x55, 0x04, 0x03, 0x0c, 0x0b, 0x73, 0x32, 0x6e,
353 0x54, 0x65, 0x73, 0x74, 0x52, 0x6f, 0x6f, 0x74
354 },
355 },
356 {
357 .cert_name = S2N_RSA_2048_PKCS1_CERT_CHAIN,
358 .expected_bytes_size = 94,
359 .expected_bytes = {
360 0x00, 0x2f, 0x00, 0x5a, 0x00, 0x58, 0x00, 0x1a,
361 0x30, 0x18, 0x31, 0x16, 0x30, 0x14, 0x06, 0x03,
362 0x55, 0x04, 0x03, 0x0c, 0x0d, 0x73, 0x32, 0x6e,
363 0x54, 0x65, 0x73, 0x74, 0x53, 0x65, 0x72, 0x76,
364 0x65, 0x72, 0x00, 0x20, 0x30, 0x1e, 0x31, 0x1c,
365 0x30, 0x1a, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c,
366 0x13, 0x73, 0x32, 0x6e, 0x54, 0x65, 0x73, 0x74,
367 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6d, 0x65, 0x64,
368 0x69, 0x61, 0x74, 0x65, 0x00, 0x18, 0x30, 0x16,
369 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04,
370 0x03, 0x0c, 0x0b, 0x73, 0x32, 0x6e, 0x54, 0x65,
371 0x73, 0x74, 0x52, 0x6f, 0x6f, 0x74
372 },
373 },
374 };
375 /* clang-format on */
376
377 for (size_t i = 0; i < s2n_array_len(test_cases); i++) {
378 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new_minimal(),
379 s2n_config_ptr_free);
380 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(config,
381 test_cases[i].cert_name, NULL));
382
383 EXPECT_SUCCESS(s2n_config_set_cert_authorities_from_trust_store(config));
384
385 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
386 s2n_connection_ptr_free);
387 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
388 conn->actual_protocol_version = S2N_TLS13;
389
390 DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free);
391 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0));
392
393 EXPECT_SUCCESS(s2n_extension_send(&s2n_cert_authorities_extension,
394 conn, &output));
395
396 size_t output_size = s2n_stuffer_data_available(&output);
397 EXPECT_EQUAL(test_cases[i].expected_bytes_size, output_size);
398
399 uint8_t *output_bytes = s2n_stuffer_raw_read(&output, output_size);
400 EXPECT_NOT_NULL(output_bytes);
401 EXPECT_BYTEARRAY_EQUAL(test_cases[i].expected_bytes, output_bytes, output_size);
402 }
403 };
404
405 /* Safety Test: s2n_certificate_request_get_ca_list */
406 {
407 EXPECT_NULL(s2n_certificate_request_get_ca_list(NULL));
408 };
409
410 /* Safety Test: s2n_certificate_request_set_certificate */
411 {
412 /* Note: NULL for the 2nd argument is tested in s2n_mutual_auth. */
413 EXPECT_FAILURE_WITH_ERRNO(s2n_certificate_request_set_certificate(NULL, NULL), S2N_ERR_INVALID_ARGUMENT);
414 };
415
416 /* Safety Test: s2n_certificate_authority_list_has_next */
417 {
418 EXPECT_FALSE(s2n_certificate_authority_list_has_next(NULL));
419 };
420
421 END_TEST();
422 }