Debian Codegraph @

crt/s2n/tests/testlib/s2n_testlib.h

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 #pragma once
17
18 #include <stdint.h>
19
20 #include "stuffer/s2n_stuffer.h"
21 #include "tls/s2n_connection.h"
22
23 #define S2N_CLOSED_FD -1
24
25 extern const struct s2n_ecc_preferences ecc_preferences_for_retry;
26 extern const struct s2n_security_policy security_policy_test_tls13_retry;
27 extern const struct s2n_security_policy security_policy_test_tls13_retry_with_pq;
28
29 /* Hex methods for testing */
30 S2N_RESULT s2n_stuffer_alloc_from_hex(struct s2n_stuffer *stuffer, const char *str);
31 /* Unlike other hex methods, the hex string read here may include spaces.
32 * This is useful for hex strings with odd whitespace for readability purposes. */
33 S2N_RESULT s2n_blob_alloc_from_hex_with_whitespace(struct s2n_blob *bytes_out, const char *str);
34
35 void s2n_print_connection(struct s2n_connection *conn, const char *marker);
36
37 int s2n_connection_set_io_stuffers(struct s2n_stuffer *input, struct s2n_stuffer *output, struct s2n_connection *conn);
38 int s2n_connection_set_recv_io_stuffer(struct s2n_stuffer *input, struct s2n_connection *conn);
39 int s2n_connection_set_send_io_stuffer(struct s2n_stuffer *output, struct s2n_connection *conn);
40
41 struct s2n_test_io_stuffer_pair {
42 struct s2n_stuffer client_in;
43 struct s2n_stuffer server_in;
44 };
45 S2N_RESULT s2n_io_stuffer_pair_init(struct s2n_test_io_stuffer_pair *io_pair);
46 S2N_CLEANUP_RESULT s2n_io_stuffer_pair_free(struct s2n_test_io_stuffer_pair *io_pair);
47 S2N_RESULT s2n_connections_set_io_stuffer_pair(struct s2n_connection *client, struct s2n_connection *server,
48 struct s2n_test_io_stuffer_pair *io_pair);
49
50 struct s2n_test_io_pair {
51 int client;
52 int server;
53 };
54 int s2n_io_pair_init(struct s2n_test_io_pair *io_pair);
55 int s2n_io_pair_init_non_blocking(struct s2n_test_io_pair *io_pair);
56 int s2n_io_pair_close(struct s2n_test_io_pair *io_pair);
57 int s2n_io_pair_close_one_end(struct s2n_test_io_pair *io_pair, int mode_to_close);
58 int s2n_io_pair_shutdown_one_end(struct s2n_test_io_pair *io_pair, int mode_to_close, int how);
59
60 int s2n_connection_set_io_pair(struct s2n_connection *conn, struct s2n_test_io_pair *io_pair);
61 int s2n_connections_set_io_pair(struct s2n_connection *client, struct s2n_connection *server,
62 struct s2n_test_io_pair *io_pair);
63
64 int s2n_fd_set_blocking(int fd);
65 int s2n_fd_set_non_blocking(int fd);
66
67 /* On Windows, POSIX write()/read() don't work on sockets.
68 * Use these macros when writing/reading raw bytes to/from socket fds in tests.
69 */
70 #ifdef _WIN32
71 #define WIN32_LEAN_AND_MEAN
72 #include <winsock2.h>
73 #define s2n_test_send(fd, buf, len) send(fd, (const char *) (buf), len, 0)
74 #define s2n_test_recv(fd, buf, len) recv(fd, (char *) (buf), len, 0)
75 #else
76 #define s2n_test_send(fd, buf, len) write(fd, buf, len)
77 #define s2n_test_recv(fd, buf, len) read(fd, buf, len)
78 #endif
79
80 int s2n_set_connection_hello_retry_flags(struct s2n_connection *conn);
81 int s2n_connection_mark_extension_received(struct s2n_connection *conn, uint16_t iana_value);
82 int s2n_connection_allow_response_extension(struct s2n_connection *conn, uint16_t iana_value);
83 int s2n_connection_allow_all_response_extensions(struct s2n_connection *conn);
84 int s2n_connection_set_all_protocol_versions(struct s2n_connection *conn, uint8_t version);
85 S2N_RESULT s2n_set_all_mutually_supported_groups(struct s2n_connection *conn);
86 S2N_RESULT s2n_skip_handshake(struct s2n_connection *conn);
87 S2N_RESULT s2n_connection_set_test_message_type(struct s2n_connection *conn, message_type_t expected_message_type);
88
89 S2N_RESULT s2n_connection_set_secrets(struct s2n_connection *conn);
90
91 S2N_RESULT s2n_config_mock_wall_clock(struct s2n_config *config, uint64_t *test_time_in_ns);
92
93 struct s2n_psk *s2n_test_psk_new(struct s2n_connection *conn);
94 S2N_RESULT s2n_append_test_psk_with_early_data(struct s2n_connection *conn, uint32_t max_early_data,
95 const struct s2n_cipher_suite *cipher_suite);
96 S2N_RESULT s2n_append_test_chosen_psk_with_early_data(struct s2n_connection *conn, uint32_t max_early_data,
97 const struct s2n_cipher_suite *cipher_suite);
98
99 S2N_RESULT s2n_connection_set_test_transcript_hash(struct s2n_connection *conn,
100 message_type_t message_type, const struct s2n_blob *digest);
101 S2N_RESULT s2n_connection_set_test_early_secret(struct s2n_connection *conn, const struct s2n_blob *early_secret);
102 S2N_RESULT s2n_connection_set_test_handshake_secret(struct s2n_connection *conn, const struct s2n_blob *handshake_secret);
103 S2N_RESULT s2n_connection_set_test_master_secret(struct s2n_connection *conn, const struct s2n_blob *master_secret);
104
105 #define S2N_MAX_TEST_PEM_SIZE 12000
106 #define S2N_MAX_TEST_PEM_PATH_LENGTH 512
107
108 /* These paths assume that the unit tests are run from inside the unit/ directory.
109 * Absolute paths will be needed if test directories go to deeper levels.
110 */
111 #define S2N_RSA_2048_PKCS8_CERT_CHAIN "../pems/rsa_2048_pkcs8_cert.pem"
112 #define S2N_RSA_2048_PKCS1_CERT_CHAIN "../pems/rsa_2048_pkcs1_cert.pem"
113 #define S2N_RSA_2048_PKCS1_SHA256_CERT_CHAIN "../pems/permutations/rsae_pkcs_2048_sha256/server-chain.pem"
114 #define S2N_RSA_2048_PKCS1_SHA256_CERT_KEY "../pems/permutations/rsae_pkcs_2048_sha256/server-key.pem"
115
116 #define S2N_RSA_2048_PKCS1_LEAF_CERT "../pems/rsa_2048_pkcs1_leaf.pem"
117 #define S2N_ECDSA_P256_PKCS1_CERT_CHAIN "../pems/ecdsa_p256_pkcs1_cert.pem"
118 #define S2N_ECDSA_P384_PKCS1_CERT_CHAIN "../pems/ecdsa_p384_pkcs1_cert.pem"
119 #define S2N_ECDSA_P512_CERT_CHAIN "../pems/ecdsa_p521_cert.pem"
120 #define S2N_RSA_CERT_CHAIN_CRLF "../pems/rsa_2048_pkcs1_cert_crlf.pem"
121 #define S2N_RSA_KEY_CRLF "../pems/rsa_2048_pkcs1_key_crlf.pem"
122 #define S2N_ECDSA_P256_PKCS1_KEY "../pems/ecdsa_p256_pkcs1_key.pem"
123 #define S2N_ECDSA_P384_PKCS1_KEY "../pems/ecdsa_p384_pkcs1_key.pem"
124 #define S2N_ECDSA_P512_KEY "../pems/ecdsa_p521_key.pem"
125 #define S2N_RSA_2048_PKCS1_KEY "../pems/rsa_2048_pkcs1_key.pem"
126 #define S2N_RSA_2048_PKCS8_KEY "../pems/rsa_2048_pkcs8_key.pem"
127
128 #define S2N_RSA_PSS_2048_SHA256_CA_KEY "../pems/rsa_pss_2048_sha256_CA_key.pem"
129 #define S2N_RSA_PSS_2048_SHA256_CA_CERT "../pems/rsa_pss_2048_sha256_CA_cert.pem"
130 #define S2N_RSA_PSS_2048_SHA256_LEAF_KEY "../pems/rsa_pss_2048_sha256_leaf_key.pem"
131 #define S2N_RSA_PSS_2048_SHA256_LEAF_CERT "../pems/rsa_pss_2048_sha256_leaf_cert.pem"
132
133 #define S2N_MLDSA44_KEY "../pems/mldsa/ML-DSA-44-seed.priv"
134 #define S2N_MLDSA44_CERT "../pems/mldsa/ML-DSA-44.crt"
135
136 #define S2N_MLDSA87_KEY "../pems/mldsa/ML-DSA-87-seed.priv"
137 #define S2N_MLDSA87_CERT "../pems/mldsa/ML-DSA-87.crt"
138
139 #define S2N_RSA_2048_SHA256_CLIENT_CERT "../pems/rsa_2048_sha256_client_cert.pem"
140
141 #define S2N_RSA_2048_SHA256_NO_DNS_SANS_CERT "../pems/rsa_2048_sha256_no_dns_sans_cert.pem"
142 #define S2N_RSA_2048_SHA256_WILDCARD_CERT "../pems/rsa_2048_sha256_wildcard_cert.pem"
143 #define S2N_RSA_2048_SHA256_WILDCARD_KEY "../pems/rsa_2048_sha256_wildcard_key.pem"
144
145 #define S2N_RSA_2048_SHA256_URI_SANS_CERT "../pems/rsa_2048_sha256_uri_sans_cert.pem"
146
147 /* "Strangely" formatted PEMs that should still parse successfully */
148 #define S2N_LEAF_WHITESPACE_CERT_CHAIN "../pems/rsa_2048_leaf_whitespace_cert.pem"
149 #define S2N_INTERMEDIATE_WHITESPACE_CERT_CHAIN "../pems/rsa_2048_intermediate_whitespace_cert.pem"
150 #define S2N_ROOT_WHITESPACE_CERT_CHAIN "../pems/rsa_2048_root_whitespace_cert.pem"
151 #define S2N_TRAILING_WHITESPACE_CERT_CHAIN "../pems/rsa_2048_trailing_whitespace_cert.pem"
152 #define S2N_LEADING_COMMENT_TEXT_CERT_CHAIN "../pems/rsa_2048_leading_comment_text_cert.pem"
153 #define S2N_LONG_BASE64_LINES_CERT_CHAIN "../pems/rsa_2048_varying_base64_len_cert.pem"
154 /* Missing line endings between PEM encapsulation boundaries */
155 #define S2N_MISSING_LINE_ENDINGS_CERT_CHAIN "../pems/rsa_2048_missing_line_endings_cert.pem"
156
157 /* PEMs with invalid timestamp fields */
158 #define S2N_EXPIRED_CERT_CHAIN "../pems/rsa_2048_expired_cert.pem"
159 #define S2N_EXPIRED_KEY "../pems/rsa_2048_expired_key.pem"
160 #define S2N_NOT_YET_VALID_CERT_CHAIN "../pems/rsa_2048_not_yet_valid_cert.pem"
161 #define S2N_NOT_YET_VALID_KEY "../pems/rsa_2048_not_yet_valid_key.pem"
162
163 /* Illegally formatted PEMs */
164 #define S2N_INVALID_HEADER_CERT_CHAIN "../pems/rsa_2048_invalid_header_cert.pem"
165 #define S2N_INVALID_TRAILER_CERT_CHAIN "../pems/rsa_2048_invalid_trailer_cert.pem"
166 #define S2N_UNKNOWN_KEYWORD_CERT_CHAIN "../pems/rsa_2048_unknown_keyword_cert.pem"
167 #define S2N_INVALID_HEADER_KEY "../pems/rsa_2048_invalid_header_key.pem"
168 #define S2N_INVALID_TRAILER_KEY "../pems/rsa_2048_invalid_trailer_key.pem"
169 #define S2N_UNKNOWN_KEYWORD_KEY "../pems/rsa_2048_unknown_keyword_key.pem"
170 #define S2N_WEIRD_DASHES_CERT_CHAIN "../pems/rsa_2048_weird_dashes_cert.pem"
171 #define S2N_NO_DASHES_CERT_CHAIN "../pems/rsa_2048_no_dashes_cert.pem"
172
173 /* Certificate with unusual curve not supported by awslc */
174 #define S2N_BRAINPOOL_CURVE_CERT "../pems/ecdsa_brainpoolP512t1_cert.pem"
175
176 /* OCSP Stapled Response Testing files */
177 #define S2N_OCSP_SERVER_CERT "../pems/ocsp/server_cert.pem"
178 #define S2N_OCSP_SERVER_CERT_EARLY_EXPIRE "../pems/ocsp/server_cert_early_expire.pem"
179 #define S2N_OCSP_SERVER_ECDSA_CERT "../pems/ocsp/server_ecdsa_cert.pem"
180
181 #define S2N_OCSP_SERVER_KEY "../pems/ocsp/server_key.pem"
182 #define S2N_OCSP_CA_CERT "../pems/ocsp/ca_cert.pem"
183 #define S2N_OCSP_CA_KEY "../pems/ocsp/ca_key.pem"
184 #define S2N_OCSP_RESPONSE_DER "../pems/ocsp/ocsp_response.der"
185 #define S2N_OCSP_RESPONSE_EARLY_EXPIRE_DER "../pems/ocsp/ocsp_response_early_expire.der"
186 #define S2N_OCSP_RESPONSE_NO_NEXT_UPDATE_DER "../pems/ocsp/ocsp_response_no_next_update.der"
187 #define S2N_OCSP_RESPONSE_REVOKED_DER "../pems/ocsp/ocsp_response_revoked.der"
188 #define S2N_OCSP_RESPONSE_WRONG_SIGNER_DER "../pems/ocsp/ocsp_response_wrong_signer.der"
189 #define S2N_OCSP_RESPONSE_CERT "../pems/ocsp/ocsp_cert.pem"
190
191 #define S2N_ALLIGATOR_SAN_CERT "../pems/sni/alligator_cert.pem"
192 #define S2N_ALLIGATOR_SAN_KEY "../pems/sni/alligator_key.pem"
193 #define S2N_IP_V6_LO_RSA_CERT "../pems/sni/ip_v6_lo_rsa_cert.pem"
194 #define S2N_IP_V6_LO_RSA_KEY "../pems/sni/ip_v6_lo_rsa_key.pem"
195 #define S2N_WITHOUT_CN_RSA_CERT "../pems/sni/without_cn_rsa_cert.pem"
196 #define S2N_WITHOUT_CN_RSA_KEY "../pems/sni/without_cn_rsa_key.pem"
197
198 #define S2N_DHPARAMS_2048 "../pems/dhparams_2048.pem"
199
200 #define S2N_ONE_TRAILING_BYTE_CERT_BIN "../pems/one_trailing_byte_cert.bin"
201 #define S2N_FOUR_TRAILING_BYTE_CERT_BIN "../pems/four_trailing_byte_cert.bin"
202
203 /* This is a certificate with a legacy SHA-1 signature on the root certificate. This is used to prove
204 * that our certificate validation code does not fail a root certificate signed with SHA-1. */
205 #define S2N_SHA1_ROOT_SIGNATURE_CA_CERT "../pems/rsa_1024_sha1_CA_cert.pem"
206
207 /* The leaf and intermediate have larger key sizes than the root. */
208 #define S2N_MIXED_CHAIN_CERTS "../pems/mixed_chains/ecdsa/server-chain.pem"
209 #define S2N_MIXED_CHAIN_KEY "../pems/mixed_chains/ecdsa/server-key.pem"
210 #define S2N_MIXED_CHAIN_CA "../pems/mixed_chains/ecdsa/ca-cert.pem"
211
212 #define S2N_TEST_TRUST_STORE "../pems/trust-store/ca-bundle.crt"
213
214 #define S2N_DEFAULT_TEST_CERT_CHAIN S2N_RSA_2048_PKCS1_SHA256_CERT_CHAIN
215 #define S2N_DEFAULT_TEST_PRIVATE_KEY S2N_RSA_2048_PKCS1_SHA256_CERT_KEY
216
217 #define S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN S2N_ECDSA_P384_PKCS1_CERT_CHAIN
218 #define S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY S2N_ECDSA_P384_PKCS1_KEY
219
220 #define S2N_DEFAULT_TEST_DHPARAMS S2N_DHPARAMS_2048
221
222 /* Read a cert given a path into pem_out */
223 int s2n_read_test_pem(const char *pem_path, char *pem_out, long int max_size);
224 int s2n_read_test_pem_and_len(const char *pem_path, uint8_t *pem_out, uint32_t *pem_len, long int max_size);
225 int s2n_test_cert_chain_and_key_new(struct s2n_cert_chain_and_key **chain_and_key,
226 const char *cert_chain_file, const char *private_key_file);
227
228 #define S2N_TEST_CERT_CHAIN_LIST_MAX 19
229 struct s2n_test_cert_chain_entry {
230 struct s2n_cert_chain_and_key *chain;
231 /* Use this field to indicate support: by array index, version number, etc. */
232 uint64_t supported;
233 };
234 struct s2n_test_cert_chain_list {
235 struct s2n_test_cert_chain_entry chains[S2N_TEST_CERT_CHAIN_LIST_MAX];
236 size_t count;
237 /* Certs skipped because they were not supported by the libcrypto. */
238 size_t skipped;
239 };
240 S2N_RESULT s2n_test_cert_chains_init(struct s2n_test_cert_chain_list *chains);
241 S2N_RESULT s2n_test_cert_chains_set_supported(struct s2n_test_cert_chain_list *chains,
242 s2n_pkey_type pkey_type, uint64_t supported);
243 S2N_CLEANUP_RESULT s2n_test_cert_chains_free(struct s2n_test_cert_chain_list *chains);
244
245 /**
246 * load the `server-cert.pem` for the appropriate permutation
247 * @param type indicates an `ec`, `rsae` or `rsapss` key type
248 * @param signature indicates an `ecdsa`, `pkcs`, or `pss` signature
249 * @param size indicates an rsa cert of `2048`, `3072`, or `4096` or an ecdsa
250 * cert with `p256` or `p384`
251 * @param digest indicates the certificate signature digest of `sha256` or
252 * `sha384`
253 *
254 * @note The OpenSSL FIPS module requires the key size for RSA certs to be greater than or equal to 1024 bits.
255 * See https://github.com/aws/s2n-tls/issues/4651 for more information.
256 */
257 int s2n_test_cert_permutation_load_server_chain(struct s2n_cert_chain_and_key **chain_and_key,
258 const char *type, const char *signature, const char *size, const char *digest);
259
260 int s2n_test_cert_permutation_get_ca_path(char *output, const char *type, const char *signature,
261 const char *size, const char *digest);
262 S2N_RESULT s2n_test_cert_permutation_get_server_chain_path(char *output, const char *type,
263 const char *signature, const char *size, const char *digest);
264
265 S2N_RESULT s2n_test_cert_chain_data_from_pem(struct s2n_connection *conn, const char *pem_path,
266 struct s2n_stuffer *cert_chain_stuffer);
267 S2N_RESULT s2n_test_cert_chain_data_from_pem_data(struct s2n_connection *conn, uint8_t *pem_data, uint32_t pem_data_len,
268 struct s2n_stuffer *cert_chain_stuffer);
269
270 int s2n_negotiate_test_server_and_client(struct s2n_connection *server_conn, struct s2n_connection *client_conn);
271 S2N_RESULT s2n_negotiate_test_server_and_client_until_message(struct s2n_connection *server_conn,
272 struct s2n_connection *client_conn, message_type_t message_type);
273 int s2n_shutdown_test_server_and_client(struct s2n_connection *server_conn, struct s2n_connection *client_conn);
274 S2N_RESULT s2n_negotiate_test_server_and_client_with_early_data(struct s2n_connection *server_conn,
275 struct s2n_connection *client_conn, struct s2n_blob *early_data_to_send, struct s2n_blob *early_data_received);
276 S2N_RESULT s2n_send_and_recv_test(struct s2n_connection *send_conn, struct s2n_connection *recv_conn);
277
278 /* Testing only with easily constructed contiguous data buffers could hide errors.
279 * We should use iovecs where every buffer is allocated separately.
280 * These test methods construct separate io buffers from one contiguous buffer.
281 */
282 struct s2n_test_iovecs {
283 struct iovec *iovecs;
284 size_t iovecs_count;
285 };
286 S2N_RESULT s2n_test_new_iovecs(struct s2n_test_iovecs *iovecs,
287 struct s2n_blob *data, const size_t *lens, size_t lens_count);
288 S2N_CLEANUP_RESULT s2n_test_iovecs_free(struct s2n_test_iovecs *in);
289
290 /* Expects 2 s2n_blobs to be equal (same size and contents) */
291 #define S2N_BLOB_EXPECT_EQUAL(blob1, blob2) \
292 do { \
293 EXPECT_EQUAL(blob1.size, blob2.size); \
294 EXPECT_BYTEARRAY_EQUAL(blob1.data, blob2.data, blob1.size); \
295 } while (0)
296
297 /* Expects data of type in stuffer, where type is uint32, uint64 etc.. */
298 #define S2N_STUFFER_READ_EXPECT_EQUAL(stuffer, expected, type) \
299 do { \
300 type##_t value; \
301 EXPECT_SUCCESS(s2n_stuffer_read_##type(stuffer, &value)); \
302 EXPECT_EQUAL(value, expected); \
303 } while (0)
304
305 /* Expects written length in stuffer */
306 #define S2N_STUFFER_LENGTH_WRITTEN_EXPECT_EQUAL(stuffer, bytes) \
307 do { \
308 EXPECT_SUCCESS(s2n_stuffer_skip_read(stuffer, bytes)); \
309 EXPECT_EQUAL(s2n_stuffer_data_available(stuffer), 0); \
310 } while (0)
311
312 int s2n_public_ecc_keys_are_equal(struct s2n_ecc_evp_params *params_1, struct s2n_ecc_evp_params *params_2);
313
314 extern const s2n_parsed_extension EMPTY_PARSED_EXTENSIONS[S2N_PARSED_EXTENSIONS_COUNT];
315 #define EXPECT_PARSED_EXTENSION_LIST_EMPTY(list) EXPECT_BYTEARRAY_EQUAL(list.parsed_extensions, EMPTY_PARSED_EXTENSIONS, sizeof(EMPTY_PARSED_EXTENSIONS))
316 #define EXPECT_PARSED_EXTENSION_LIST_NOT_EMPTY(list) EXPECT_BYTEARRAY_NOT_EQUAL(list.parsed_extensions, EMPTY_PARSED_EXTENSIONS, sizeof(EMPTY_PARSED_EXTENSIONS))
317
318 int s2n_kem_recv_public_key_fuzz_test(const uint8_t *buf, size_t len, struct s2n_kem_params *kem_params);
319 int s2n_kem_recv_ciphertext_fuzz_test(const uint8_t *buf, size_t len, struct s2n_kem_params *kem_params);
320 int s2n_kem_recv_ciphertext_fuzz_test_init(const char *kat_file_path, struct s2n_kem_params *kem_params);
321
322 S2N_RESULT s2n_resumption_test_ticket_key_setup(struct s2n_config *config);
323
324 #define S2N_BLOB_FROM_HEX(name, hex) S2N_CHECKED_BLOB_FROM_HEX(name, POSIX_GUARD_RESULT, hex)
325 #define S2N_CHECKED_BLOB_FROM_HEX(name, check, hex) \
326 DEFER_CLEANUP(struct s2n_blob name = { 0 }, s2n_free); \
327 check(s2n_blob_alloc_from_hex_with_whitespace(&name, (const char *) hex));
328
329 bool s2n_is_seccomp_supported();
330 S2N_RESULT s2n_seccomp_init();
331
332 S2N_RESULT s2n_config_set_tls12_security_policy(struct s2n_config *config);
333 S2N_RESULT s2n_connection_set_tls12_security_policy(struct s2n_connection *connection);