Debian Codegraph @

crt/s2n/tests/unit/s2n_self_talk_certificates_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_rsa_pss.h"
17 #include "s2n_test.h"
18 #include "testlib/s2n_testlib.h"
19 #include "tls/s2n_tls.h"
20
21 static uint8_t s2n_noop_verify_host_fn(const char *name, size_t len, void *data)
22 {
23 return 1;
24 }
25
26 static S2N_RESULT s2n_test_load_certificate(struct s2n_cert_chain_and_key **chain_out,
27 char *chain_pem_out, const char *chain_pem_path, const char *key_pem_path)
28 {
29 RESULT_ENSURE_REF(chain_out);
30
31 char key_pem[S2N_MAX_TEST_PEM_SIZE] = { 0 };
32 RESULT_GUARD_POSIX(s2n_read_test_pem(chain_pem_path, chain_pem_out, S2N_MAX_TEST_PEM_SIZE));
33 RESULT_GUARD_POSIX(s2n_read_test_pem(key_pem_path, key_pem, S2N_MAX_TEST_PEM_SIZE));
34
35 *chain_out = s2n_cert_chain_and_key_new();
36 RESULT_ENSURE_REF(*chain_out);
37 RESULT_GUARD_POSIX(s2n_cert_chain_and_key_load_pem(*chain_out, chain_pem_out, key_pem));
38
39 return S2N_RESULT_OK;
40 }
41
42 int main(int argc, char **argv)
43 {
44 BEGIN_TEST();
45
46 if (!s2n_is_rsa_pss_certs_supported() || !s2n_is_tls13_fully_supported()) {
47 END_TEST();
48 }
49
50 const uint8_t test_versions[] = { S2N_TLS13, S2N_TLS12, S2N_TLS11, S2N_TLS10 };
51 struct {
52 const char *cert;
53 const char *key;
54 uint8_t min_version;
55 } test_certs[] = {
56 {
57 .cert = S2N_RSA_2048_PKCS1_SHA256_CERT_CHAIN,
58 .key = S2N_RSA_2048_PKCS1_SHA256_CERT_KEY,
59 },
60 {
61 .cert = S2N_ECDSA_P384_PKCS1_CERT_CHAIN,
62 .key = S2N_ECDSA_P384_PKCS1_KEY,
63 },
64 {
65 .cert = S2N_ECDSA_P512_CERT_CHAIN,
66 .key = S2N_ECDSA_P512_KEY,
67 },
68 {
69 .cert = S2N_RSA_PSS_2048_SHA256_LEAF_CERT,
70 .key = S2N_RSA_PSS_2048_SHA256_LEAF_KEY,
71 .min_version = S2N_TLS12,
72 },
73 };
74
75 for (size_t cert_i = 0; cert_i < s2n_array_len(test_certs); cert_i++) {
76 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain = NULL,
77 s2n_cert_chain_and_key_ptr_free);
78 char pem[S2N_MAX_TEST_PEM_SIZE] = { 0 };
79 EXPECT_OK(s2n_test_load_certificate(&chain, pem,
80 test_certs[cert_i].cert, test_certs[cert_i].key));
81
82 for (size_t version_i = 0; version_i < s2n_array_len(test_versions); version_i++) {
83 uint8_t version = test_versions[version_i];
84 bool expect_success = (version >= test_certs[cert_i].min_version);
85
86 /* 20240501 only supports up to TLS1.2 */
87 const char *security_policy = "20240501";
88 if (version >= S2N_TLS13) {
89 security_policy = "default_tls13";
90 } else if (version < S2N_TLS12) {
91 /* The default policies don't support legacy versions */
92 security_policy = "test_all";
93 }
94
95 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
96 EXPECT_NOT_NULL(config);
97 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain));
98 EXPECT_SUCCESS(s2n_config_add_pem_to_trust_store(config, pem));
99 EXPECT_SUCCESS(s2n_config_set_verify_host_callback(config, s2n_noop_verify_host_fn, NULL));
100 EXPECT_SUCCESS(s2n_config_set_max_blinding_delay(config, 0));
101 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, security_policy));
102
103 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
104 s2n_connection_ptr_free);
105 EXPECT_NOT_NULL(server);
106 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
107 server->server_protocol_version = version;
108
109 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
110 s2n_connection_ptr_free);
111 EXPECT_NOT_NULL(client);
112 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
113 client->client_protocol_version = version;
114
115 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
116 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
117 EXPECT_SUCCESS(s2n_connections_set_io_pair(client, server, &io_pair));
118
119 bool handshake_success =
120 (s2n_negotiate_test_server_and_client(server, client) == S2N_SUCCESS);
121 if (handshake_success) {
122 EXPECT_EQUAL(server->actual_protocol_version, version);
123 EXPECT_EQUAL(client->actual_protocol_version, version);
124 }
125
126 const char *error_message = "Handshake failed";
127 if (!expect_success) {
128 error_message = "Handshake unexpectedly succeeded";
129 }
130 if (handshake_success != expect_success) {
131 fprintf(stderr, "%s version=%i cert=%s\n",
132 error_message, version, test_certs[cert_i].cert);
133 FAIL_MSG(error_message);
134 }
135 }
136 }
137
138 END_TEST();
139 }