Debian Codegraph @

crt/s2n/tests/testlib/s2n_test_certs.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 <dirent.h>
17 #include <stdint.h>
18 #include <stdio.h>
19 #include <sys/types.h>
20
21 #include "crypto/s2n_libcrypto.h"
22 #include "crypto/s2n_mldsa.h"
23 #include "crypto/s2n_rsa_pss.h"
24 #include "error/s2n_errno.h"
25 #include "stuffer/s2n_stuffer.h"
26 #include "testlib/s2n_testlib.h"
27 #include "utils/s2n_safety.h"
28
29 #define S2N_TEST_CERT_CHAIN_LIST_MAX_SKIPPED 5
30
31 int s2n_test_cert_chain_and_key_new(struct s2n_cert_chain_and_key **chain_and_key,
32 const char *cert_chain_file, const char *private_key_file)
33 {
34 char cert_chain_pem[S2N_MAX_TEST_PEM_SIZE];
35 char private_key_pem[S2N_MAX_TEST_PEM_SIZE];
36
37 POSIX_GUARD(s2n_read_test_pem(cert_chain_file, cert_chain_pem, S2N_MAX_TEST_PEM_SIZE));
38 POSIX_GUARD(s2n_read_test_pem(private_key_file, private_key_pem, S2N_MAX_TEST_PEM_SIZE));
39
40 POSIX_GUARD_PTR(*chain_and_key = s2n_cert_chain_and_key_new());
41 POSIX_GUARD(s2n_cert_chain_and_key_load_pem(*chain_and_key, cert_chain_pem, private_key_pem));
42
43 return S2N_SUCCESS;
44 }
45
46 S2N_RESULT s2n_test_cert_permutation_load_server_chain_from_name(
47 struct s2n_cert_chain_and_key **chain_and_key, const char *name)
48 {
49 char path_buffer[S2N_MAX_TEST_PEM_PATH_LENGTH] = { 0 };
50
51 char cert_chain_pem[S2N_MAX_TEST_PEM_SIZE] = { 0 };
52 snprintf(path_buffer, S2N_MAX_TEST_PEM_PATH_LENGTH,
53 "../pems/permutations/%s/server-chain.pem", name);
54 RESULT_GUARD_POSIX(s2n_read_test_pem(path_buffer, cert_chain_pem, S2N_MAX_TEST_PEM_SIZE));
55
56 char private_key_pem[S2N_MAX_TEST_PEM_SIZE] = { 0 };
57 snprintf(path_buffer, S2N_MAX_TEST_PEM_PATH_LENGTH,
58 "../pems/permutations/%s/server-key.pem", name);
59 RESULT_GUARD_POSIX(s2n_read_test_pem(path_buffer, private_key_pem, S2N_MAX_TEST_PEM_SIZE));
60
61 RESULT_GUARD_PTR(*chain_and_key = s2n_cert_chain_and_key_new());
62 RESULT_GUARD_POSIX(s2n_cert_chain_and_key_load_pem(*chain_and_key, cert_chain_pem, private_key_pem));
63
64 return S2N_RESULT_OK;
65 }
66
67 int s2n_test_cert_permutation_load_server_chain(struct s2n_cert_chain_and_key **chain_and_key,
68 const char *type, const char *signature, const char *size, const char *digest)
69 {
70 char name_buffer[S2N_MAX_TEST_PEM_PATH_LENGTH] = { 0 };
71 snprintf(name_buffer, S2N_MAX_TEST_PEM_PATH_LENGTH,
72 "%s_%s_%s_%s", type, signature, size, digest);
73 POSIX_GUARD_RESULT(s2n_test_cert_permutation_load_server_chain_from_name(chain_and_key, name_buffer));
74 return S2N_SUCCESS;
75 }
76
77 int s2n_test_cert_permutation_get_ca_path(char *output, const char *type, const char *signature,
78 const char *size, const char *digest)
79 {
80 sprintf(output, "../pems/permutations/%s_%s_%s_%s/ca-cert.pem", type, signature, size, digest);
81 return S2N_SUCCESS;
82 }
83
84 S2N_RESULT s2n_test_cert_permutation_get_server_chain_path(char *output, const char *type,
85 const char *signature, const char *size, const char *digest)
86 {
87 sprintf(output, "../pems/permutations/%s_%s_%s_%s/server-chain.pem", type, signature, size,
88 digest);
89 return S2N_RESULT_OK;
90 }
91
92 int s2n_read_test_pem(const char *pem_path, char *pem_out, long int max_size)
93 {
94 uint32_t pem_len = 0;
95
96 POSIX_GUARD(s2n_read_test_pem_and_len(pem_path, (uint8_t *) pem_out, &pem_len, max_size - 1));
97 pem_out[pem_len] = '\0';
98
99 return 0;
100 }
101
102 int s2n_read_test_pem_and_len(const char *pem_path, uint8_t *pem_out, uint32_t *pem_len, long int max_size)
103 {
104 FILE *pem_file = fopen(pem_path, "rb");
105 if (!pem_file) {
106 POSIX_BAIL(S2N_ERR_NULL);
107 }
108
109 /* Make sure we can fit the pem into the output buffer */
110 fseek(pem_file, 0, SEEK_END);
111 const long int pem_file_size = ftell(pem_file);
112 /* one extra for the null byte */
113 rewind(pem_file);
114
115 if (max_size < (pem_file_size)) {
116 POSIX_BAIL(S2N_ERR_NOMEM);
117 }
118
119 if (fread(pem_out, sizeof(char), pem_file_size, pem_file) < pem_file_size) {
120 POSIX_BAIL(S2N_ERR_IO);
121 }
122
123 *pem_len = pem_file_size;
124 fclose(pem_file);
125
126 return 0;
127 }
128
129 S2N_RESULT s2n_test_cert_chain_data_from_pem(struct s2n_connection *conn, const char *pem_path,
130 struct s2n_stuffer *cert_chain_stuffer)
131 {
132 RESULT_ENSURE_REF(cert_chain_stuffer);
133
134 uint8_t cert_chain_pem[S2N_MAX_TEST_PEM_SIZE] = { 0 };
135 uint32_t cert_chain_pem_len = 0;
136 RESULT_GUARD_POSIX(s2n_read_test_pem_and_len(pem_path, cert_chain_pem, &cert_chain_pem_len, S2N_MAX_TEST_PEM_SIZE));
137
138 RESULT_GUARD(s2n_test_cert_chain_data_from_pem_data(conn, cert_chain_pem, cert_chain_pem_len, cert_chain_stuffer));
139
140 return S2N_RESULT_OK;
141 }
142
143 S2N_RESULT s2n_test_cert_chain_data_from_pem_data(struct s2n_connection *conn, uint8_t *pem_data, uint32_t pem_data_len,
144 struct s2n_stuffer *cert_chain_stuffer)
145 {
146 DEFER_CLEANUP(struct s2n_stuffer certificate_message_stuffer = { 0 }, s2n_stuffer_free);
147 RESULT_GUARD_POSIX(s2n_stuffer_growable_alloc(&certificate_message_stuffer, 4096));
148
149 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = s2n_cert_chain_and_key_new(),
150 s2n_cert_chain_and_key_ptr_free);
151 RESULT_GUARD_POSIX(s2n_cert_chain_and_key_load_public_pem_bytes(chain_and_key, pem_data, pem_data_len));
152
153 RESULT_GUARD_POSIX(s2n_send_cert_chain(conn, &certificate_message_stuffer, chain_and_key));
154
155 /* Skip the cert chain length */
156 RESULT_GUARD_POSIX(s2n_stuffer_skip_read(&certificate_message_stuffer, 3));
157
158 uint32_t cert_chain_len = s2n_stuffer_data_available(&certificate_message_stuffer);
159 RESULT_GUARD_POSIX(s2n_stuffer_alloc(cert_chain_stuffer, cert_chain_len));
160 RESULT_GUARD_POSIX(s2n_stuffer_copy(&certificate_message_stuffer, cert_chain_stuffer, cert_chain_len));
161
162 return S2N_RESULT_OK;
163 }
164
165 S2N_RESULT s2n_test_cert_chains_init(struct s2n_test_cert_chain_list *chains)
166 {
167 /* Load all permutations */
168 {
169 DIR *root = opendir("../pems/permutations");
170 RESULT_ENSURE_REF(root);
171 struct dirent *dir = NULL;
172 while ((dir = readdir(root)) != NULL) {
173 /* Skip ., .., the README, etc.
174 * This is pretty hacky: we can switch to stat later if necessary.
175 - */
176 if (strchr(dir->d_name, '_') == NULL) {
177 continue;
178 }
179 if (!s2n_is_rsa_pss_certs_supported() && strstr(dir->d_name, "pss")) {
180 chains->skipped++;
181 continue;
182 }
183 if (s2n_libcrypto_is_openssl_fips() && strstr(dir->d_name, "rsae_pkcs_1024_sha1")) {
184 chains->skipped++;
185 continue;
186 }
187
188 RESULT_ENSURE_LT(chains->count, S2N_TEST_CERT_CHAIN_LIST_MAX);
189 struct s2n_test_cert_chain_entry *entry = &chains->chains[chains->count];
190
191 RESULT_GUARD(s2n_test_cert_permutation_load_server_chain_from_name(
192 &entry->chain, dir->d_name));
193 chains->count++;
194 }
195 closedir(root);
196 }
197
198 /* Load all MLDSA test certs.
199 * MLDSA is not yet included in permutations due to difficulties generating MLDSA certs.
200 * We instead continue to test with the example certs from the RFC.
201 */
202 const char *mldsa_names[] = { "ML-DSA-44", "ML-DSA-65", "ML-DSA-87" };
203 if (s2n_mldsa_is_supported()) {
204 for (size_t i = 0; i < s2n_array_len(mldsa_names); i++) {
205 RESULT_ENSURE_LT(chains->count, S2N_TEST_CERT_CHAIN_LIST_MAX);
206 struct s2n_test_cert_chain_entry *entry = &chains->chains[chains->count];
207
208 char path_buffer[S2N_MAX_TEST_PEM_PATH_LENGTH] = { 0 };
209
210 char cert_chain_pem[S2N_MAX_TEST_PEM_SIZE] = { 0 };
211 snprintf(path_buffer, S2N_MAX_TEST_PEM_PATH_LENGTH,
212 "../pems/mldsa/%s.crt", mldsa_names[i]);
213 RESULT_GUARD_POSIX(s2n_read_test_pem(path_buffer, cert_chain_pem, S2N_MAX_TEST_PEM_SIZE));
214
215 char private_key_pem[S2N_MAX_TEST_PEM_SIZE] = { 0 };
216 snprintf(path_buffer, S2N_MAX_TEST_PEM_PATH_LENGTH,
217 "../pems/mldsa/%s-seed.priv", mldsa_names[i]);
218 RESULT_GUARD_POSIX(s2n_read_test_pem(path_buffer, private_key_pem, S2N_MAX_TEST_PEM_SIZE));
219
220 entry->chain = s2n_cert_chain_and_key_new();
221 RESULT_GUARD_POSIX(s2n_cert_chain_and_key_load_pem(
222 entry->chain, cert_chain_pem, private_key_pem));
223
224 chains->count++;
225 }
226 } else {
227 chains->skipped += s2n_array_len(mldsa_names);
228 }
229
230 /* Sanity check result */
231 RESULT_ENSURE_LTE(chains->skipped, S2N_TEST_CERT_CHAIN_LIST_MAX_SKIPPED);
232 RESULT_ENSURE_EQ(chains->skipped + chains->count, S2N_TEST_CERT_CHAIN_LIST_MAX);
233
234 return S2N_RESULT_OK;
235 }
236
237 /*
238 * Sets "supported" to a specific value for all chains of a given pkey_type.
239 * "supported" could indicate an index in an array of policies (as in s2n_security_policies_test),
240 * or could indicate a specific policy version, or could simply indicate true/false.
241 * The meaning of the value depends on the structure of the test.
242 */
243 S2N_RESULT s2n_test_cert_chains_set_supported(struct s2n_test_cert_chain_list *chains,
244 s2n_pkey_type target_type, uint64_t supported)
245 {
246 for (size_t i = 0; i < chains->count; i++) {
247 struct s2n_test_cert_chain_entry *entry = &chains->chains[i];
248 s2n_pkey_type entry_type = entry->chain->cert_chain->head->pkey_type;
249 if (entry_type == target_type) {
250 entry->supported = supported;
251 }
252 }
253 return S2N_RESULT_OK;
254 }
255
256 S2N_CLEANUP_RESULT s2n_test_cert_chains_free(struct s2n_test_cert_chain_list *chains)
257 {
258 for (size_t i = 0; i < chains->count; i++) {
259 struct s2n_test_cert_chain_entry *entry = &chains->chains[i];
260 RESULT_GUARD_POSIX(s2n_cert_chain_and_key_free(entry->chain));
261 entry->supported = 0;
262 }
263 chains->count = 0;
264 return S2N_RESULT_OK;
265 }