Debian Codegraph @

crt/s2n/tests/unit/s2n_tls13_handshake_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 #include "tls/s2n_tls13_handshake.h"
16
17 #include <stdint.h>
18 #include <stdlib.h>
19
20 #include "api/s2n.h"
21 #include "crypto/s2n_fips.h"
22 #include "s2n_test.h"
23 #include "testlib/s2n_testlib.h"
24 #include "tls/extensions/s2n_client_key_share.h"
25 #include "tls/extensions/s2n_server_key_share.h"
26 #include "tls/s2n_cipher_suites.h"
27 #include "tls/s2n_connection.h"
28 #include "tls/s2n_handshake.h"
29 #include "tls/s2n_quic_support.h"
30 #include "tls/s2n_security_policies.h"
31 #include "tls/s2n_tls13.h"
32 #include "utils/s2n_safety.h"
33
34 /* Just to get access to the static functions / variables we need to test */
35 #include "tls/s2n_handshake_io.c"
36 #include "tls/s2n_handshake_transcript.c"
37 #include "tls/s2n_tls13_handshake.c"
38
39 #define S2N_SECRET_TYPE_COUNT 5
40 #define S2N_TEST_PSK_COUNT 10
41
42 int main(int argc, char **argv)
43 {
44 BEGIN_TEST();
45
46 if (!s2n_is_tls13_fully_supported()) {
47 END_TEST();
48 }
49
50 /* Test wiping PSKs after use */
51 {
52 /* PSKs are wiped when chosen PSK is NULL */
53 {
54 struct s2n_connection *conn = NULL;
55 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
56
57 const struct s2n_ecc_preferences *ecc_preferences = NULL;
58 EXPECT_SUCCESS(s2n_connection_get_ecc_preferences(conn, &ecc_preferences));
59 EXPECT_NOT_NULL(ecc_preferences);
60
61 conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
62 conn->kex_params.server_ecc_evp_params.negotiated_curve = ecc_preferences->ecc_curves[0];
63 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&conn->kex_params.server_ecc_evp_params));
64 conn->kex_params.client_ecc_evp_params.negotiated_curve = ecc_preferences->ecc_curves[0];
65 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&conn->kex_params.client_ecc_evp_params));
66
67 const uint8_t psk_data[] = "test identity data";
68 const uint8_t secret_data[] = "test secret data";
69 for (size_t i = 0; i < S2N_TEST_PSK_COUNT; i++) {
70 struct s2n_psk *psk = NULL;
71 EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &psk));
72 EXPECT_OK(s2n_psk_init(psk, S2N_PSK_TYPE_EXTERNAL));
73 EXPECT_SUCCESS(s2n_psk_set_identity(psk, psk_data, sizeof(psk_data)));
74 EXPECT_NOT_EQUAL(psk->identity.size, 0);
75 EXPECT_NOT_EQUAL(psk->identity.data, NULL);
76 EXPECT_SUCCESS(s2n_psk_set_secret(psk, secret_data, sizeof(secret_data)));
77 EXPECT_NOT_EQUAL(psk->secret.size, 0);
78 EXPECT_NOT_EQUAL(psk->secret.data, NULL);
79 }
80
81 EXPECT_NOT_EQUAL(conn->psk_params.psk_list.mem.allocated, 0);
82 EXPECT_EQUAL(conn->psk_params.psk_list.len, S2N_TEST_PSK_COUNT);
83 EXPECT_NULL(conn->psk_params.chosen_psk);
84
85 DEFER_CLEANUP(struct s2n_blob shared_secret = { 0 }, s2n_free);
86 EXPECT_SUCCESS(s2n_tls13_compute_shared_secret(conn, &shared_secret));
87
88 /* Verify secrets are wiped */
89 for (size_t i = 0; i < conn->psk_params.psk_list.len; i++) {
90 struct s2n_psk *psk = NULL;
91 EXPECT_OK(s2n_array_get(&conn->psk_params.psk_list, i, (void **) &psk));
92 EXPECT_NOT_EQUAL(psk->identity.size, 0);
93 EXPECT_NULL(psk->secret.data);
94 EXPECT_EQUAL(psk->secret.size, 0);
95 }
96
97 EXPECT_SUCCESS(s2n_connection_free(conn));
98 };
99
100 /* PSKs are wiped when chosen PSK is NOT NULL */
101 {
102 struct s2n_connection *conn = NULL;
103 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
104
105 const struct s2n_ecc_preferences *ecc_preferences = NULL;
106 EXPECT_SUCCESS(s2n_connection_get_ecc_preferences(conn, &ecc_preferences));
107 EXPECT_NOT_NULL(ecc_preferences);
108
109 conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
110 conn->kex_params.server_ecc_evp_params.negotiated_curve = ecc_preferences->ecc_curves[0];
111 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&conn->kex_params.server_ecc_evp_params));
112 conn->kex_params.client_ecc_evp_params.negotiated_curve = ecc_preferences->ecc_curves[0];
113 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&conn->kex_params.client_ecc_evp_params));
114
115 const uint8_t psk_data[] = "test identity data";
116 const uint8_t secret_data[] = "test secret data";
117 const uint8_t early_secret_data[SHA256_DIGEST_LENGTH] = "test early secret data";
118 for (size_t i = 0; i < S2N_TEST_PSK_COUNT; i++) {
119 struct s2n_psk *psk = NULL;
120 EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &psk));
121 EXPECT_OK(s2n_psk_init(psk, S2N_PSK_TYPE_EXTERNAL));
122 EXPECT_SUCCESS(s2n_psk_set_identity(psk, psk_data, sizeof(psk_data)));
123 EXPECT_NOT_EQUAL(psk->identity.size, 0);
124 EXPECT_NOT_EQUAL(psk->identity.data, NULL);
125 EXPECT_SUCCESS(s2n_psk_set_secret(psk, secret_data, sizeof(secret_data)));
126 EXPECT_NOT_EQUAL(psk->secret.size, 0);
127 EXPECT_NOT_EQUAL(psk->secret.data, NULL);
128 EXPECT_SUCCESS(s2n_realloc(&psk->early_secret, sizeof(early_secret_data)));
129 POSIX_CHECKED_MEMCPY(psk->early_secret.data, early_secret_data, sizeof(early_secret_data));
130 EXPECT_NOT_EQUAL(psk->early_secret.size, 0);
131 EXPECT_NOT_EQUAL(psk->early_secret.data, NULL);
132 }
133
134 /* Set chosen PSK */
135 struct s2n_psk *chosen_psk = NULL;
136 EXPECT_OK(s2n_array_get(&conn->psk_params.psk_list, 0, (void **) &chosen_psk));
137 EXPECT_NOT_NULL(chosen_psk);
138 conn->psk_params.chosen_psk = chosen_psk;
139 conn->psk_params.chosen_psk_wire_index = 0;
140
141 EXPECT_NOT_EQUAL(conn->psk_params.psk_list.mem.allocated, 0);
142 EXPECT_EQUAL(conn->psk_params.psk_list.len, S2N_TEST_PSK_COUNT);
143
144 DEFER_CLEANUP(struct s2n_blob shared_secret = { 0 }, s2n_free);
145 EXPECT_SUCCESS(s2n_tls13_compute_shared_secret(conn, &shared_secret));
146
147 /* Verify secrets are wiped */
148 for (size_t i = 0; i < conn->psk_params.psk_list.len; i++) {
149 struct s2n_psk *psk = NULL;
150 EXPECT_OK(s2n_array_get(&conn->psk_params.psk_list, i, (void **) &psk));
151 EXPECT_NOT_EQUAL(psk->identity.size, 0);
152 EXPECT_NULL(psk->secret.data);
153 EXPECT_EQUAL(psk->secret.size, 0);
154 EXPECT_NULL(psk->early_secret.data);
155 EXPECT_EQUAL(psk->early_secret.size, 0);
156 }
157
158 EXPECT_SUCCESS(s2n_connection_free(conn));
159 };
160 };
161
162 /* Test: Handshake self-talks using s2n_handshake_write_io and s2n_handshake_read_io */
163 {
164 struct s2n_connection *client_conn = NULL;
165 struct s2n_connection *server_conn = NULL;
166
167 struct s2n_config *server_config = NULL, *client_config = NULL;
168 EXPECT_NOT_NULL(server_config = s2n_config_new());
169 EXPECT_NOT_NULL(client_config = s2n_config_new());
170 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(client_config));
171
172 uint8_t *cert_chain = NULL;
173 uint8_t *private_key = NULL;
174 uint32_t cert_chain_len = 0;
175 uint32_t private_key_len = 0;
176
177 EXPECT_NOT_NULL(cert_chain = malloc(S2N_MAX_TEST_PEM_SIZE));
178 EXPECT_NOT_NULL(private_key = malloc(S2N_MAX_TEST_PEM_SIZE));
179
180 EXPECT_SUCCESS(s2n_read_test_pem_and_len(S2N_ECDSA_P384_PKCS1_CERT_CHAIN, cert_chain, &cert_chain_len, S2N_MAX_TEST_PEM_SIZE));
181 EXPECT_SUCCESS(s2n_read_test_pem_and_len(S2N_ECDSA_P384_PKCS1_KEY, private_key, &private_key_len, S2N_MAX_TEST_PEM_SIZE));
182
183 struct s2n_cert_chain_and_key *default_cert = NULL;
184 EXPECT_NOT_NULL(default_cert = s2n_cert_chain_and_key_new());
185 EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem_bytes(default_cert, cert_chain, cert_chain_len, private_key, private_key_len));
186 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, default_cert));
187
188 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
189 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
190 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
191 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
192
193 struct s2n_stuffer client_to_server = { 0 };
194 struct s2n_stuffer server_to_client = { 0 };
195
196 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&client_to_server, 0));
197 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&server_to_client, 0));
198
199 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&server_to_client, &client_to_server, client_conn));
200 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&client_to_server, &server_to_client, server_conn));
201
202 struct s2n_blob server_seq = { .data = server_conn->secure->server_sequence_number, .size = sizeof(server_conn->secure->server_sequence_number) };
203 S2N_BLOB_FROM_HEX(seq_0, "0000000000000000");
204 S2N_BLOB_FROM_HEX(seq_1, "0000000000000001");
205
206 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, "default_tls13"));
207 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "default_tls13"));
208
209 /* Client sends ClientHello */
210 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), CLIENT_HELLO);
211 EXPECT_SUCCESS(s2n_handshake_write_io(client_conn));
212
213 EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS13);
214 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_UNKNOWN_PROTOCOL_VERSION);
215
216 s2n_tls13_connection_keys(server_secrets_0, server_conn);
217 EXPECT_EQUAL(server_secrets_0.size, 0);
218
219 EXPECT_EQUAL(server_conn->handshake.handshake_type, INITIAL);
220
221 /* Server reads ClientHello */
222 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), CLIENT_HELLO);
223 EXPECT_SUCCESS(s2n_handshake_read_io(server_conn));
224
225 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS13); /* Server is now on TLS13 */
226 EXPECT_EQUAL(server_conn->handshake.handshake_type, NEGOTIATED | FULL_HANDSHAKE | MIDDLEBOX_COMPAT);
227
228 s2n_tls13_connection_keys(server_secrets, server_conn);
229 EXPECT_EQUAL(server_secrets.size, SHA256_DIGEST_LENGTH);
230
231 EXPECT_SUCCESS(s2n_conn_set_handshake_type(server_conn));
232
233 /* Server sends ServerHello */
234 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), SERVER_HELLO);
235 EXPECT_SUCCESS(s2n_handshake_write_io(server_conn));
236
237 /* Server sends CCS */
238 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), SERVER_CHANGE_CIPHER_SPEC);
239 EXPECT_SUCCESS(s2n_handshake_write_io(server_conn));
240 S2N_BLOB_EXPECT_EQUAL(server_seq, seq_0);
241
242 /* Server sends EncryptedExtensions */
243 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), ENCRYPTED_EXTENSIONS);
244 EXPECT_SUCCESS(s2n_handshake_write_io(server_conn));
245 S2N_BLOB_EXPECT_EQUAL(server_seq, seq_1);
246
247 /* Server sends ServerCert */
248 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), SERVER_CERT);
249 EXPECT_SUCCESS(s2n_handshake_write_io(server_conn));
250
251 /* Server sends CertVerify */
252 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), SERVER_CERT_VERIFY);
253 EXPECT_SUCCESS(s2n_handshake_write_io(server_conn));
254
255 /* Client reads ServerHello */
256 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), SERVER_HELLO);
257 EXPECT_SUCCESS(s2n_handshake_read_io(client_conn));
258
259 /* Client reads CCS
260 * The CCS message does not affect its place in the state machine. */
261 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), ENCRYPTED_EXTENSIONS);
262 EXPECT_SUCCESS(s2n_handshake_read_io(client_conn));
263
264 s2n_tls13_connection_keys(client_secrets, client_conn);
265 EXPECT_EQUAL(client_secrets.size, SHA256_DIGEST_LENGTH);
266
267 /* Verify that derive and extract secrets match */
268 S2N_BLOB_EXPECT_EQUAL(server_secrets.derive_secret, client_secrets.derive_secret);
269 S2N_BLOB_EXPECT_EQUAL(server_secrets.extract_secret, client_secrets.extract_secret);
270
271 /* Client reads Encrypted extensions */
272 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), ENCRYPTED_EXTENSIONS);
273 EXPECT_SUCCESS(s2n_handshake_read_io(client_conn));
274
275 /* Client reads ServerCert */
276 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), SERVER_CERT);
277 EXPECT_SUCCESS(s2n_handshake_read_io(client_conn));
278
279 /* Client reads CertVerify */
280 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), SERVER_CERT_VERIFY);
281 EXPECT_SUCCESS(s2n_handshake_read_io(client_conn));
282
283 /* Server sends ServerFinished */
284 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), SERVER_FINISHED);
285 EXPECT_SUCCESS(s2n_handshake_write_io(server_conn));
286
287 /* Client reads ServerFinished */
288 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), SERVER_FINISHED);
289 EXPECT_SUCCESS(s2n_handshake_read_io(client_conn));
290
291 /* Client sends CCS */
292 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), CLIENT_CHANGE_CIPHER_SPEC);
293 EXPECT_SUCCESS(s2n_handshake_write_io(client_conn));
294
295 /* Client sends ClientFinished */
296 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), CLIENT_FINISHED);
297 EXPECT_SUCCESS(s2n_handshake_write_io(client_conn));
298
299 /* Server reads CCS
300 * The CCS message does not affect its place in the state machine. */
301 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), CLIENT_FINISHED);
302 EXPECT_SUCCESS(s2n_handshake_read_io(server_conn));
303
304 /* Server reads ClientFinished */
305 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), CLIENT_FINISHED);
306 EXPECT_SUCCESS(s2n_handshake_read_io(server_conn));
307
308 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), APPLICATION_DATA);
309 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), APPLICATION_DATA);
310
311 /* Verify that derive and extract secrets match */
312 S2N_BLOB_EXPECT_EQUAL(server_secrets.derive_secret, client_secrets.derive_secret);
313 S2N_BLOB_EXPECT_EQUAL(server_secrets.extract_secret, client_secrets.extract_secret);
314
315 /* Clean up */
316 EXPECT_SUCCESS(s2n_stuffer_free(&client_to_server));
317 EXPECT_SUCCESS(s2n_stuffer_free(&server_to_client));
318
319 EXPECT_SUCCESS(s2n_connection_free(client_conn));
320 EXPECT_SUCCESS(s2n_connection_free(server_conn));
321
322 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(default_cert));
323 EXPECT_SUCCESS(s2n_config_free(server_config));
324 EXPECT_SUCCESS(s2n_config_free(client_config));
325
326 free(private_key);
327 free(cert_chain);
328 };
329
330 END_TEST();
331 }