Debian Codegraph @

crt/s2n/tests/unit/s2n_client_hello_retry_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_pq.h"
17 #include "s2n.h"
18 #include "s2n_test.h"
19 #include "stuffer/s2n_stuffer.h"
20 #include "testlib/s2n_testlib.h"
21 #include "tls/extensions/s2n_client_renegotiation_info.h"
22 #include "tls/extensions/s2n_cookie.h"
23 #include "tls/extensions/s2n_extension_type_lists.h"
24 #include "tls/extensions/s2n_server_supported_versions.h"
25 #include "tls/s2n_cipher_suites.h"
26 #include "tls/s2n_connection.h"
27 #include "tls/s2n_security_policies.h"
28 #include "tls/s2n_tls.h"
29 #include "tls/s2n_tls13.h"
30 #include "tls/s2n_tls13_handshake.h"
31
32 /* This include is required to access static function s2n_server_hello_parse */
33 #include "error/s2n_errno.h"
34 #include "tls/extensions/s2n_early_data_indication.h"
35 #include "tls/extensions/s2n_server_key_share.h"
36 #include "tls/s2n_server_hello.c"
37 #include "utils/s2n_bitmap.h"
38 #include "utils/s2n_safety.h"
39
40 #define HELLO_RETRY_MSG_NO 1
41 #define SERVER_HELLO_MSG_NO 5
42
43 int s2n_parse_client_hello(struct s2n_connection *conn);
44
45 static int s2n_client_hello_cb_with_get_server_name(struct s2n_connection *conn, void *ctx)
46 {
47 const char *expected_server_name = (const char *) ctx;
48 const char *actual_server_name = s2n_get_server_name(conn);
49 POSIX_ENSURE_EQ(strcmp(expected_server_name, actual_server_name), 0);
50 return S2N_SUCCESS;
51 }
52
53 int main(int argc, char **argv)
54 {
55 BEGIN_TEST();
56
57 if (!s2n_is_tls13_fully_supported()) {
58 END_TEST();
59 }
60
61 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
62
63 /* Test s2n_server_hello_retry_recv */
64 {
65 /* s2n_server_hello_retry_recv must fail when a keyshare for a matching curve was already present */
66 {
67 struct s2n_config *config = NULL;
68 struct s2n_connection *conn = NULL;
69
70 EXPECT_NOT_NULL(config = s2n_config_new());
71 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
72 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
73
74 const struct s2n_ecc_preferences *ecc_pref = NULL;
75 POSIX_GUARD(s2n_connection_get_ecc_preferences(conn, &ecc_pref));
76 EXPECT_NOT_NULL(ecc_pref);
77
78 conn->actual_protocol_version = S2N_TLS13;
79 conn->kex_params.server_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
80 conn->kex_params.client_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
81
82 EXPECT_NULL(conn->kex_params.client_ecc_evp_params.evp_pkey);
83 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&conn->kex_params.client_ecc_evp_params));
84 EXPECT_NOT_NULL(conn->kex_params.client_ecc_evp_params.evp_pkey);
85
86 EXPECT_FAILURE_WITH_ERRNO(s2n_server_hello_retry_recv(conn), S2N_ERR_INVALID_HELLO_RETRY);
87
88 EXPECT_SUCCESS(s2n_config_free(config));
89 EXPECT_SUCCESS(s2n_connection_free(conn));
90 };
91
92 /* s2n_server_hello_retry_recv must fail for a connection with actual protocol version less than TLS13 */
93 {
94 struct s2n_config *config = NULL;
95 struct s2n_connection *conn = NULL;
96
97 EXPECT_NOT_NULL(config = s2n_config_new());
98 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
99 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
100
101 conn->actual_protocol_version = S2N_TLS12;
102
103 EXPECT_FAILURE_WITH_ERRNO(s2n_server_hello_retry_recv(conn), S2N_ERR_INVALID_HELLO_RETRY);
104
105 EXPECT_SUCCESS(s2n_config_free(config));
106 EXPECT_SUCCESS(s2n_connection_free(conn));
107 };
108
109 /* Test ECC success case for s2n_server_hello_retry_recv */
110 {
111 struct s2n_config *server_config = NULL;
112 struct s2n_config *client_config = NULL;
113
114 struct s2n_connection *server_conn = NULL;
115 struct s2n_connection *client_conn = NULL;
116
117 struct s2n_cert_chain_and_key *tls13_chain_and_key = NULL;
118 char tls13_cert_chain[S2N_MAX_TEST_PEM_SIZE] = { 0 };
119 char tls13_private_key[S2N_MAX_TEST_PEM_SIZE] = { 0 };
120
121 EXPECT_NOT_NULL(server_config = s2n_config_new());
122 EXPECT_NOT_NULL(client_config = s2n_config_new());
123
124 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
125 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
126
127 EXPECT_NOT_NULL(tls13_chain_and_key = s2n_cert_chain_and_key_new());
128 EXPECT_SUCCESS(s2n_read_test_pem(S2N_ECDSA_P384_PKCS1_CERT_CHAIN, tls13_cert_chain, S2N_MAX_TEST_PEM_SIZE));
129 EXPECT_SUCCESS(s2n_read_test_pem(S2N_ECDSA_P384_PKCS1_KEY, tls13_private_key, S2N_MAX_TEST_PEM_SIZE));
130 EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(tls13_chain_and_key, tls13_cert_chain, tls13_private_key));
131 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(client_config, tls13_chain_and_key));
132 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, tls13_chain_and_key));
133
134 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
135
136 /* Force the HRR path */
137 client_conn->security_policy_override = &security_policy_test_tls13_retry;
138
139 /* ClientHello 1 */
140 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
141 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
142 s2n_stuffer_data_available(&client_conn->handshake.io)));
143
144 /* Server receives ClientHello 1 */
145 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
146
147 server_conn->kex_params.server_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
148 EXPECT_SUCCESS(s2n_set_connection_hello_retry_flags(server_conn));
149
150 /* Server sends HelloRetryMessage */
151 EXPECT_SUCCESS(s2n_server_hello_retry_send(server_conn));
152
153 EXPECT_SUCCESS(s2n_stuffer_wipe(&client_conn->handshake.io));
154 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io,
155 s2n_stuffer_data_available(&server_conn->handshake.io)));
156 client_conn->handshake.message_number = HELLO_RETRY_MSG_NO;
157 /* Read the message off the wire */
158 EXPECT_SUCCESS(s2n_server_hello_parse(client_conn));
159 client_conn->actual_protocol_version_established = 1;
160
161 EXPECT_SUCCESS(s2n_conn_set_handshake_type(client_conn));
162 /* Client receives the HelloRetryRequest mesage */
163 EXPECT_SUCCESS(s2n_server_hello_retry_recv(client_conn));
164
165 EXPECT_SUCCESS(s2n_config_free(client_config));
166 EXPECT_SUCCESS(s2n_config_free(server_config));
167 EXPECT_SUCCESS(s2n_connection_free(server_conn));
168 EXPECT_SUCCESS(s2n_connection_free(client_conn));
169 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(tls13_chain_and_key));
170 };
171
172 {
173 const struct s2n_security_policy test_security_policy = {
174 .minimum_protocol_version = S2N_SSLv3,
175 .cipher_preferences = &cipher_preferences_test_all_tls13,
176 .kem_preferences = &kem_preferences_all,
177 .signature_preferences = &s2n_signature_preferences_20200207,
178 .ecc_preferences = &s2n_ecc_preferences_20200310,
179 };
180
181 if (!s2n_pq_is_enabled()) {
182 struct s2n_connection *conn = NULL;
183 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
184 conn->actual_protocol_version = S2N_TLS13;
185 conn->security_policy_override = &test_security_policy;
186
187 const struct s2n_kem_preferences *kem_pref = NULL;
188 POSIX_GUARD(s2n_connection_get_kem_preferences(conn, &kem_pref));
189 EXPECT_NOT_NULL(kem_pref);
190
191 conn->kex_params.server_kem_group_params.kem_group = kem_pref->tls13_kem_groups[0];
192 EXPECT_NULL(conn->kex_params.server_ecc_evp_params.negotiated_curve);
193
194 EXPECT_FAILURE_WITH_ERRNO(s2n_server_hello_retry_recv(conn), S2N_ERR_INVALID_HELLO_RETRY);
195
196 EXPECT_SUCCESS(s2n_connection_free(conn));
197 } else {
198 /* s2n_server_hello_retry_recv must fail when a keyshare for a matching PQ KEM was already present */
199 {
200 struct s2n_connection *conn = NULL;
201 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
202 conn->actual_protocol_version = S2N_TLS13;
203 conn->security_policy_override = &test_security_policy;
204
205 const struct s2n_kem_preferences *kem_pref = NULL;
206 POSIX_GUARD(s2n_connection_get_kem_preferences(conn, &kem_pref));
207 EXPECT_NOT_NULL(kem_pref);
208
209 const struct s2n_kem_group *kem_group = s2n_kem_preferences_get_highest_priority_group(kem_pref);
210 EXPECT_NOT_NULL(kem_group);
211
212 conn->kex_params.server_kem_group_params.kem_group = kem_group;
213 EXPECT_NULL(conn->kex_params.server_ecc_evp_params.negotiated_curve);
214
215 struct s2n_kem_group_params *client_params = &conn->kex_params.client_kem_group_params;
216 client_params->kem_group = kem_group;
217 client_params->kem_params.kem = kem_group->kem;
218 client_params->ecc_params.negotiated_curve = kem_group->curve;
219
220 EXPECT_NULL(client_params->ecc_params.evp_pkey);
221 EXPECT_NULL(client_params->kem_params.private_key.data);
222
223 kem_public_key_size public_key_size = kem_group->kem->public_key_length;
224 EXPECT_SUCCESS(s2n_alloc(&client_params->kem_params.public_key, public_key_size));
225
226 EXPECT_OK(s2n_kem_generate_keypair(&client_params->kem_params));
227 EXPECT_NOT_NULL(client_params->kem_params.private_key.data);
228 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&client_params->ecc_params));
229 EXPECT_NOT_NULL(client_params->ecc_params.evp_pkey);
230
231 EXPECT_FAILURE_WITH_ERRNO(s2n_server_hello_retry_recv(conn), S2N_ERR_INVALID_HELLO_RETRY);
232
233 EXPECT_SUCCESS(s2n_free(&client_params->kem_params.public_key));
234 EXPECT_SUCCESS(s2n_connection_free(conn));
235 };
236 /* Test failure if exactly one of {named_curve, kem_group} isn't non-null */
237 {
238 struct s2n_connection *conn = NULL;
239 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
240 conn->actual_protocol_version = S2N_TLS13;
241 conn->security_policy_override = &test_security_policy;
242
243 conn->kex_params.server_kem_group_params.kem_group = &s2n_x25519_mlkem_768;
244 conn->kex_params.server_ecc_evp_params.negotiated_curve = &s2n_ecc_curve_secp256r1;
245
246 EXPECT_FAILURE_WITH_ERRNO(s2n_server_hello_retry_recv(conn), S2N_ERR_INVALID_HELLO_RETRY);
247
248 conn->kex_params.server_kem_group_params.kem_group = NULL;
249 conn->kex_params.server_ecc_evp_params.negotiated_curve = NULL;
250
251 EXPECT_FAILURE_WITH_ERRNO(s2n_server_hello_retry_recv(conn), S2N_ERR_INVALID_HELLO_RETRY);
252
253 EXPECT_SUCCESS(s2n_connection_free(conn));
254 };
255 /* Test PQ KEM success case for s2n_server_hello_retry_recv. */
256 /* Need at least two KEM's to test fallback */
257 uint32_t available_groups = 0;
258 EXPECT_OK(s2n_kem_preferences_groups_available(test_security_policy.kem_preferences, &available_groups));
259 if (available_groups >= 2) {
260 struct s2n_config *config = NULL;
261 struct s2n_connection *conn = NULL;
262
263 struct s2n_cert_chain_and_key *tls13_chain_and_key = NULL;
264 char tls13_cert_chain[S2N_MAX_TEST_PEM_SIZE] = { 0 };
265 char tls13_private_key[S2N_MAX_TEST_PEM_SIZE] = { 0 };
266
267 EXPECT_NOT_NULL(config = s2n_config_new());
268 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
269 conn->security_policy_override = &test_security_policy;
270
271 EXPECT_NOT_NULL(tls13_chain_and_key = s2n_cert_chain_and_key_new());
272 EXPECT_SUCCESS(s2n_read_test_pem(S2N_ECDSA_P384_PKCS1_CERT_CHAIN, tls13_cert_chain, S2N_MAX_TEST_PEM_SIZE));
273 EXPECT_SUCCESS(s2n_read_test_pem(S2N_ECDSA_P384_PKCS1_KEY, tls13_private_key, S2N_MAX_TEST_PEM_SIZE));
274 EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(tls13_chain_and_key, tls13_cert_chain, tls13_private_key));
275 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, tls13_chain_and_key));
276
277 /* Client sends ClientHello containing key share for X25519MLKEM768
278 * (but indicates support for SecP256r1MLKEM768 in supported_groups) */
279 EXPECT_SUCCESS(s2n_client_hello_send(conn));
280
281 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->handshake.io));
282 conn->session_id_len = 0; /* Wipe the session id to match the HRR hex */
283
284 /* Server responds with HRR indicating SecP256r1MLKEM768 as choice for negotiation;
285 * the last 6 bytes (0033 0002 11EB) are the key share extension with SecP256r1MLKEM768 */
286 DEFER_CLEANUP(struct s2n_stuffer hrr = { 0 }, s2n_stuffer_free);
287 EXPECT_OK(s2n_stuffer_alloc_from_hex(&hrr,
288 "0303CF21AD74E59A6111BE1D8C021E65B891C2A211167ABB8C5E079E09E2C8A8339C00130200000C002B000203040033000211EB"));
289
290 EXPECT_SUCCESS(s2n_stuffer_copy(&hrr, &conn->handshake.io, s2n_stuffer_data_available(&hrr)));
291 conn->handshake.message_number = HELLO_RETRY_MSG_NO;
292 /* Read the message off the wire */
293 EXPECT_SUCCESS(s2n_server_hello_parse(conn));
294 conn->actual_protocol_version_established = 1;
295
296 EXPECT_SUCCESS(s2n_conn_set_handshake_type(conn));
297 /* Client receives the HelloRetryRequest message */
298 EXPECT_SUCCESS(s2n_server_hello_retry_recv(conn));
299
300 EXPECT_SUCCESS(s2n_config_free(config));
301 EXPECT_SUCCESS(s2n_connection_free(conn));
302 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(tls13_chain_and_key));
303 }
304 }
305 };
306 };
307
308 /* Verify that the hash transcript recreation function is called correctly,
309 * within the s2n_server_hello_retry_send and s2n_server_hello_retry_recv functions.
310 * The hash transcript recreation function, if called correctly takes the existing ClientHello1
311 * hash, and generates a synthetic message. This test verifies that transcript hash recreated is the same
312 * on both the server and client side. */
313 {
314 struct s2n_config *server_config = NULL;
315 struct s2n_config *client_config = NULL;
316
317 struct s2n_connection *server_conn = NULL;
318 struct s2n_connection *client_conn = NULL;
319
320 struct s2n_cert_chain_and_key *tls13_chain_and_key = NULL;
321 char tls13_cert_chain[S2N_MAX_TEST_PEM_SIZE] = { 0 };
322 char tls13_private_key[S2N_MAX_TEST_PEM_SIZE] = { 0 };
323
324 EXPECT_NOT_NULL(server_config = s2n_config_new());
325 EXPECT_NOT_NULL(client_config = s2n_config_new());
326
327 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
328 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
329
330 EXPECT_NOT_NULL(tls13_chain_and_key = s2n_cert_chain_and_key_new());
331 EXPECT_SUCCESS(s2n_read_test_pem(S2N_ECDSA_P384_PKCS1_CERT_CHAIN, tls13_cert_chain, S2N_MAX_TEST_PEM_SIZE));
332 EXPECT_SUCCESS(s2n_read_test_pem(S2N_ECDSA_P384_PKCS1_KEY, tls13_private_key, S2N_MAX_TEST_PEM_SIZE));
333 EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(tls13_chain_and_key, tls13_cert_chain, tls13_private_key));
334 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(client_config, tls13_chain_and_key));
335 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, tls13_chain_and_key));
336
337 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
338
339 /* Force the HRR path */
340 client_conn->security_policy_override = &security_policy_test_tls13_retry;
341
342 /* ClientHello 1 */
343 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
344
345 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
346 s2n_stuffer_data_available(&client_conn->handshake.io)));
347
348 /* Server receives ClientHello 1 */
349 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
350 EXPECT_TRUE(s2n_is_hello_retry_handshake(server_conn));
351 EXPECT_SUCCESS(s2n_set_connection_hello_retry_flags(server_conn));
352
353 server_conn->kex_params.server_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
354
355 /* Server sends HelloRetryRequest message, note that s2n_server_hello_retry_recreate_transcript
356 * is called within the s2n_server_hello_retry_send function */
357 EXPECT_SUCCESS(s2n_server_hello_retry_send(server_conn));
358
359 s2n_tls13_connection_keys(server_keys, server_conn);
360 uint8_t hash_digest_length = server_keys.size;
361
362 /* Obtain the transcript hash recreated within the HelloRetryRequest message */
363 DEFER_CLEANUP(struct s2n_hash_state server_hash = { 0 }, s2n_hash_free);
364 uint8_t server_digest_out[S2N_MAX_DIGEST_LEN] = { 0 };
365 POSIX_GUARD(s2n_hash_new(&server_hash));
366 POSIX_GUARD_RESULT(s2n_handshake_copy_hash_state(server_conn, server_keys.hash_algorithm, &server_hash));
367 POSIX_GUARD(s2n_hash_digest(&server_hash, server_digest_out, hash_digest_length));
368
369 struct s2n_blob server_blob = { 0 };
370 EXPECT_SUCCESS(s2n_blob_init(&server_blob, server_digest_out, hash_digest_length));
371
372 EXPECT_SUCCESS(s2n_stuffer_wipe(&client_conn->handshake.io));
373 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io,
374 s2n_stuffer_data_available(&server_conn->handshake.io)));
375 client_conn->handshake.message_number = HELLO_RETRY_MSG_NO;
376 /* Client receives the HelloRetryRequest mesage, note that s2n_server_hello_retry_recreate_transcript
377 * is called within the s2n_server_hello_recv function */
378 EXPECT_SUCCESS(s2n_server_hello_recv(client_conn));
379
380 s2n_tls13_connection_keys(client_keys, client_conn);
381 hash_digest_length = client_keys.size;
382
383 /* Obtain the transcript hash recreated within ClientHello2 message */
384 DEFER_CLEANUP(struct s2n_hash_state client_hash = { 0 }, s2n_hash_free);
385 uint8_t client_digest_out[S2N_MAX_DIGEST_LEN];
386 POSIX_GUARD(s2n_hash_new(&client_hash));
387 POSIX_GUARD_RESULT(s2n_handshake_copy_hash_state(client_conn, client_keys.hash_algorithm, &client_hash));
388 POSIX_GUARD(s2n_hash_digest(&client_hash, client_digest_out, hash_digest_length));
389
390 struct s2n_blob client_blob = { 0 };
391 EXPECT_SUCCESS(s2n_blob_init(&client_blob, client_digest_out, hash_digest_length));
392
393 /* Test that the transcript hash recreated MUST be the same on the server and client side */
394 S2N_BLOB_EXPECT_EQUAL(client_blob, server_blob);
395
396 EXPECT_SUCCESS(s2n_config_free(client_config));
397 EXPECT_SUCCESS(s2n_config_free(server_config));
398 EXPECT_SUCCESS(s2n_connection_free(server_conn));
399 EXPECT_SUCCESS(s2n_connection_free(client_conn));
400 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(tls13_chain_and_key));
401 };
402
403 /**
404 * Self-Talk test: the client initiates a handshake with an unknown keyshare.
405 * The server sends a HelloRetryRequest that requires the client to generate a
406 * key share on the server negotiated curve.
407 *
408 *= https://www.rfc-editor.org/rfc/rfc8446#4.1.4
409 *= type=test
410 *# Otherwise, the client MUST process all extensions in the
411 *# HelloRetryRequest and send a second updated ClientHello.
412 **/
413 {
414 struct s2n_config *server_config = NULL;
415 struct s2n_config *client_config = NULL;
416
417 struct s2n_connection *server_conn = NULL;
418 struct s2n_connection *client_conn = NULL;
419
420 struct s2n_cert_chain_and_key *tls13_chain_and_key = NULL;
421 char tls13_cert_chain[S2N_MAX_TEST_PEM_SIZE] = { 0 };
422 char tls13_private_key[S2N_MAX_TEST_PEM_SIZE] = { 0 };
423
424 /* Create nonblocking pipes */
425 struct s2n_test_io_pair io_pair;
426 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
427
428 EXPECT_NOT_NULL(server_config = s2n_config_new());
429 EXPECT_NOT_NULL(client_config = s2n_config_new());
430
431 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
432 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
433
434 EXPECT_NOT_NULL(tls13_chain_and_key = s2n_cert_chain_and_key_new());
435 EXPECT_SUCCESS(s2n_read_test_pem(S2N_ECDSA_P384_PKCS1_CERT_CHAIN, tls13_cert_chain, S2N_MAX_TEST_PEM_SIZE));
436 EXPECT_SUCCESS(s2n_read_test_pem(S2N_ECDSA_P384_PKCS1_KEY, tls13_private_key, S2N_MAX_TEST_PEM_SIZE));
437 EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(tls13_chain_and_key, tls13_cert_chain, tls13_private_key));
438 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(client_config, tls13_chain_and_key));
439 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, tls13_chain_and_key));
440 EXPECT_SUCCESS(s2n_config_disable_x509_verification(client_config));
441
442 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
443 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
444
445 server_conn->x509_validator.skip_cert_validation = 1;
446 client_conn->x509_validator.skip_cert_validation = 1;
447
448 /* Force the HRR path */
449 client_conn->security_policy_override = &security_policy_test_tls13_retry;
450
451 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
452
453 /* Negotiate handshake */
454 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
455
456 EXPECT_SUCCESS(s2n_shutdown_test_server_and_client(server_conn, client_conn));
457
458 EXPECT_SUCCESS(s2n_config_free(client_config));
459 EXPECT_SUCCESS(s2n_config_free(server_config));
460 EXPECT_SUCCESS(s2n_connection_free(server_conn));
461 EXPECT_SUCCESS(s2n_connection_free(client_conn));
462 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(tls13_chain_and_key));
463 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
464 };
465
466 /**
467 * Self-Talk test: the client initiates a handshake with an X25519 share.
468 * The server, however does not support x25519 and prefers P-256.
469 * The server then sends a HelloRetryRequest that requires the
470 * client to generate a key share on the P-256 curve.
471 *
472 *= https://www.rfc-editor.org/rfc/rfc8446#4.1.1
473 *= type=test
474 *# If the server selects an (EC)DHE group and the client did not offer a
475 *# compatible "key_share" extension in the initial ClientHello, the
476 *# server MUST respond with a HelloRetryRequest (Section 4.1.4) message.
477 **/
478 if (s2n_is_evp_apis_supported()) {
479 struct s2n_config *server_config = NULL;
480 struct s2n_config *client_config = NULL;
481
482 struct s2n_connection *server_conn = NULL;
483 struct s2n_connection *client_conn = NULL;
484
485 struct s2n_cert_chain_and_key *tls13_chain_and_key = NULL;
486 char tls13_cert_chain[S2N_MAX_TEST_PEM_SIZE] = { 0 };
487 char tls13_private_key[S2N_MAX_TEST_PEM_SIZE] = { 0 };
488
489 /* Create nonblocking pipes */
490 struct s2n_test_io_pair io_pair;
491 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
492
493 EXPECT_NOT_NULL(server_config = s2n_config_new());
494 EXPECT_NOT_NULL(client_config = s2n_config_new());
495
496 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
497 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
498
499 EXPECT_NOT_NULL(tls13_chain_and_key = s2n_cert_chain_and_key_new());
500 EXPECT_SUCCESS(s2n_read_test_pem(S2N_ECDSA_P384_PKCS1_CERT_CHAIN, tls13_cert_chain, S2N_MAX_TEST_PEM_SIZE));
501 EXPECT_SUCCESS(s2n_read_test_pem(S2N_ECDSA_P384_PKCS1_KEY, tls13_private_key, S2N_MAX_TEST_PEM_SIZE));
502 EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(tls13_chain_and_key, tls13_cert_chain, tls13_private_key));
503 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(client_config, tls13_chain_and_key));
504 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, tls13_chain_and_key));
505 EXPECT_SUCCESS(s2n_config_disable_x509_verification(client_config));
506
507 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "20190801")); /* contains x25519 */
508 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config, "20190802")); /* does not contain x25519 */
509
510 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
511 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
512
513 server_conn->x509_validator.skip_cert_validation = 1;
514 client_conn->x509_validator.skip_cert_validation = 1;
515
516 /* Force the HRR path */
517 client_conn->security_policy_override = &security_policy_test_tls13_retry;
518
519 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
520
521 /* Negotiate handshake */
522 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
523
524 EXPECT_SUCCESS(s2n_shutdown_test_server_and_client(server_conn, client_conn));
525
526 /* Ensure the handshake included a hello retry request */
527 EXPECT_TRUE(IS_HELLO_RETRY_HANDSHAKE(client_conn));
528 EXPECT_TRUE(IS_HELLO_RETRY_HANDSHAKE(server_conn));
529
530 EXPECT_SUCCESS(s2n_config_free(client_config));
531 EXPECT_SUCCESS(s2n_config_free(server_config));
532 EXPECT_SUCCESS(s2n_connection_free(server_conn));
533 EXPECT_SUCCESS(s2n_connection_free(client_conn));
534 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(tls13_chain_and_key));
535 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
536 }
537
538 /**
539 * Ensure the client aborts the handshake if more than one
540 * HelloRetryRequest is received
541 *
542 *= https://www.rfc-editor.org/rfc/rfc8446#4.1.4
543 *= type=test
544 *# If a client receives a second
545 *# HelloRetryRequest in the same connection (i.e., where the ClientHello
546 *# was itself in response to a HelloRetryRequest), it MUST abort the
547 *# handshake with an "unexpected_message" alert.
548 **/
549 {
550 struct s2n_config *server_config = NULL;
551 struct s2n_config *client_config = NULL;
552
553 struct s2n_connection *server_conn = NULL;
554 struct s2n_connection *client_conn = NULL;
555
556 struct s2n_cert_chain_and_key *tls13_chain_and_key = NULL;
557 char tls13_cert_chain[S2N_MAX_TEST_PEM_SIZE] = { 0 };
558 char tls13_private_key[S2N_MAX_TEST_PEM_SIZE] = { 0 };
559
560 EXPECT_NOT_NULL(server_config = s2n_config_new());
561 EXPECT_NOT_NULL(client_config = s2n_config_new());
562
563 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
564 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
565
566 EXPECT_NOT_NULL(tls13_chain_and_key = s2n_cert_chain_and_key_new());
567 EXPECT_SUCCESS(s2n_read_test_pem(S2N_ECDSA_P384_PKCS1_CERT_CHAIN, tls13_cert_chain, S2N_MAX_TEST_PEM_SIZE));
568 EXPECT_SUCCESS(s2n_read_test_pem(S2N_ECDSA_P384_PKCS1_KEY, tls13_private_key, S2N_MAX_TEST_PEM_SIZE));
569 EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(tls13_chain_and_key, tls13_cert_chain, tls13_private_key));
570 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(client_config, tls13_chain_and_key));
571 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, tls13_chain_and_key));
572
573 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
574
575 /* Force the HRR path */
576 client_conn->security_policy_override = &security_policy_test_tls13_retry;
577
578 /* ClientHello 1 */
579 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
580 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
581 s2n_stuffer_data_available(&client_conn->handshake.io)));
582 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
583 EXPECT_TRUE(s2n_is_hello_retry_handshake(server_conn));
584 EXPECT_SUCCESS(s2n_set_connection_hello_retry_flags(server_conn));
585
586 /* Server HelloRetryRequest 1 */
587 EXPECT_SUCCESS(s2n_server_hello_retry_send(server_conn));
588 EXPECT_SUCCESS(s2n_set_connection_hello_retry_flags(server_conn));
589
590 EXPECT_SUCCESS(s2n_stuffer_wipe(&client_conn->handshake.io));
591 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io,
592 s2n_stuffer_data_available(&server_conn->handshake.io)));
593 client_conn->handshake.message_number = HELLO_RETRY_MSG_NO;
594 EXPECT_SUCCESS(s2n_server_hello_recv(client_conn));
595
596 /* ClientHello 2 */
597 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
598 EXPECT_SUCCESS(s2n_stuffer_wipe(&server_conn->handshake.io));
599 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
600 s2n_stuffer_data_available(&client_conn->handshake.io)));
601 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
602
603 /* Server HelloRetryRequest 2 */
604 EXPECT_SUCCESS(s2n_server_hello_retry_send(server_conn));
605
606 EXPECT_SUCCESS(s2n_stuffer_wipe(&client_conn->handshake.io));
607 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io,
608 s2n_stuffer_data_available(&server_conn->handshake.io)));
609
610 EXPECT_FAILURE_WITH_ERRNO(s2n_server_hello_recv(client_conn), S2N_ERR_INVALID_HELLO_RETRY);
611
612 EXPECT_SUCCESS(s2n_config_free(client_config));
613 EXPECT_SUCCESS(s2n_config_free(server_config));
614 EXPECT_SUCCESS(s2n_connection_free(server_conn));
615 EXPECT_SUCCESS(s2n_connection_free(client_conn));
616 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(tls13_chain_and_key));
617 };
618
619 /**
620 * Ensure that s2n_random_value_is_hello_retry returns true for hello
621 * retry random values, and false otherwise
622 *
623 *= https://www.rfc-editor.org/rfc/rfc8446#4.1.3
624 *= type=test
625 *# Upon receiving a message with type server_hello, implementations MUST
626 *# first examine the Random value and, if it matches this value, process
627 *# it as described in Section 4.1.4).
628 **/
629 {
630 struct s2n_connection *conn = NULL;
631 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
632 const uint8_t not_hello_retry_request_random[S2N_TLS_RANDOM_DATA_LEN] = { 0 };
633 EXPECT_MEMCPY_SUCCESS(conn->handshake_params.server_random, not_hello_retry_request_random,
634 S2N_TLS_RANDOM_DATA_LEN);
635
636 EXPECT_FAILURE_WITH_ERRNO(s2n_random_value_is_hello_retry(conn), S2N_ERR_INVALID_HELLO_RETRY);
637
638 EXPECT_MEMCPY_SUCCESS(conn->handshake_params.server_random, hello_retry_req_random, S2N_TLS_RANDOM_DATA_LEN);
639 EXPECT_SUCCESS(s2n_random_value_is_hello_retry(conn));
640
641 EXPECT_SUCCESS(s2n_connection_free(conn));
642 };
643
644 /**
645 *= https://www.rfc-editor.org/rfc/rfc8446#4.1.4
646 *= type=test
647 *# Upon receiving
648 *# the ServerHello, clients MUST check that the cipher suite supplied in
649 *# the ServerHello is the same as that in the HelloRetryRequest and
650 *# otherwise abort the handshake with an "illegal_parameter" alert.
651 **/
652 {
653 struct s2n_connection *server_conn = NULL;
654 struct s2n_connection *client_conn = NULL;
655
656 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
657 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
658
659 /* A Hello Retry Request has been processed */
660 EXPECT_SUCCESS(s2n_set_hello_retry_required(client_conn));
661 client_conn->secure->cipher_suite = &s2n_tls13_aes_256_gcm_sha384;
662 client_conn->server_protocol_version = S2N_TLS13;
663 client_conn->handshake.handshake_type |= NEGOTIATED;
664 client_conn->handshake.handshake_type |= FULL_HANDSHAKE;
665 client_conn->handshake.message_number = SERVER_HELLO_MSG_NO;
666
667 /* Server Hello with cipher suite that does not match Hello Retry Request cipher suite */
668 server_conn->secure->cipher_suite = &s2n_tls13_chacha20_poly1305_sha256;
669 EXPECT_SUCCESS(s2n_server_hello_send(server_conn));
670
671 EXPECT_SUCCESS(s2n_stuffer_wipe(&client_conn->handshake.io));
672 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io,
673 s2n_stuffer_data_available(&server_conn->handshake.io)));
674
675 EXPECT_FAILURE_WITH_ERRNO(s2n_server_hello_recv(client_conn), S2N_ERR_CIPHER_NOT_SUPPORTED);
676
677 EXPECT_SUCCESS(s2n_connection_free(server_conn));
678 EXPECT_SUCCESS(s2n_connection_free(client_conn));
679 };
680
681 /*
682 * Self-Talk
683 *
684 *= https://www.rfc-editor.org/rfc/rfc8446#section-4.1.2
685 *= type=test
686 *# The client will also send a
687 *# ClientHello when the server has responded to its ClientHello with a
688 *# HelloRetryRequest. In that case, the client MUST send the same
689 *# ClientHello without modification
690 */
691 if (s2n_is_tls13_fully_supported()) {
692 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL,
693 s2n_cert_chain_and_key_ptr_free);
694 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
695 S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY));
696
697 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(),
698 s2n_config_ptr_free);
699 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
700 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13"));
701 EXPECT_SUCCESS(s2n_config_disable_x509_verification(config));
702
703 /* Sanity Check: The server accepts an unchanged ClientHello */
704 {
705 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
706 s2n_connection_ptr_free);
707 EXPECT_NOT_NULL(server_conn);
708 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
709
710 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
711 s2n_connection_ptr_free);
712 EXPECT_NOT_NULL(client_conn);
713 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
714
715 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
716 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
717 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
718
719 /* Force the HRR path */
720 client_conn->security_policy_override = &security_policy_test_tls13_retry;
721
722 /* Send ClientHello */
723 s2n_blocked_status blocked = 0;
724 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
725
726 /* Finish handshake */
727 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
728 };
729
730 /* Test: The server rejects a second ClientHello with a changed legacy version */
731 {
732 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
733 s2n_connection_ptr_free);
734 EXPECT_NOT_NULL(server_conn);
735 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
736 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
737
738 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
739 s2n_connection_ptr_free);
740 EXPECT_NOT_NULL(client_conn);
741 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
742 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
743
744 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
745 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
746 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
747
748 /* Force the HRR path */
749 client_conn->security_policy_override = &security_policy_test_tls13_retry;
750
751 /* Skip to before the client sends the second ClientHello. */
752 s2n_blocked_status blocked = 0;
753 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
754 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, HELLO_RETRY_MSG));
755 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, CLIENT_HELLO));
756 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, CLIENT_HELLO));
757
758 /* Change the legacy version. */
759 client_conn->client_protocol_version = S2N_TLS11;
760
761 /* Expect failure because second client hello doesn't match */
762 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, client_conn),
763 S2N_ERR_BAD_MESSAGE);
764 };
765
766 /* Test: The server rejects a second ClientHello with changed client random */
767 {
768 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
769 s2n_connection_ptr_free);
770 EXPECT_NOT_NULL(server_conn);
771 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
772 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
773
774 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
775 s2n_connection_ptr_free);
776 EXPECT_NOT_NULL(client_conn);
777 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
778 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
779
780 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
781 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
782 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
783
784 /* Force the HRR path */
785 client_conn->security_policy_override = &security_policy_test_tls13_retry;
786
787 /* Send ClientHello */
788 s2n_blocked_status blocked = 0;
789 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
790
791 /* Change client random */
792 client_conn->client_hello.random[0]++;
793
794 /* Expect failure because second client hello doesn't match */
795 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, client_conn),
796 S2N_ERR_BAD_MESSAGE);
797 };
798
799 /* Test: Outside of testing, the server accepts a second ClientHello with a changed client random */
800 {
801 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
802 s2n_connection_ptr_free);
803 EXPECT_NOT_NULL(server_conn);
804 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
805 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
806
807 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
808 s2n_connection_ptr_free);
809 EXPECT_NOT_NULL(client_conn);
810 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
811 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
812
813 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
814 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
815 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
816
817 /* Force the HRR path */
818 client_conn->security_policy_override = &security_policy_test_tls13_retry;
819
820 /* Send ClientHello */
821 s2n_blocked_status blocked = 0;
822 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
823
824 /* Change client random */
825 client_conn->client_hello.random[0]++;
826
827 /* Expect success if we pretend that this isn't a unit test */
828 EXPECT_SUCCESS(s2n_in_unit_test_set(false));
829 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
830 EXPECT_SUCCESS(s2n_in_unit_test_set(true));
831 }
832
833 /* Test: The server accepts a second ClientHello with a changed session ID */
834 for (size_t test_in_test_mode = 0; test_in_test_mode <= 1; test_in_test_mode++) {
835 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
836 s2n_connection_ptr_free);
837 EXPECT_NOT_NULL(server_conn);
838 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
839 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
840
841 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
842 s2n_connection_ptr_free);
843 EXPECT_NOT_NULL(client_conn);
844 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
845 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
846
847 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
848 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
849 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
850
851 /* Force the HRR path. */
852 client_conn->security_policy_override = &security_policy_test_tls13_retry;
853
854 /* Skip to before the client sends the second ClientHello. */
855 s2n_blocked_status blocked = 0;
856 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
857 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, HELLO_RETRY_MSG));
858 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, CLIENT_HELLO));
859 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, CLIENT_HELLO));
860
861 /* Change session id */
862 client_conn->session_id[0]++;
863
864 if (test_in_test_mode) {
865 /* Ensure that validation fails in test mode to prevent regressions. */
866 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, client_conn),
867 S2N_ERR_BAD_MESSAGE);
868 } else {
869 EXPECT_SUCCESS(s2n_in_unit_test_set(false));
870 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
871 EXPECT_SUCCESS(s2n_in_unit_test_set(true));
872 }
873 };
874
875 /* Test: The server accepts a second ClientHello with a changed cipher suite list */
876 for (size_t test_in_test_mode = 0; test_in_test_mode <= 1; test_in_test_mode++) {
877 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
878 s2n_connection_ptr_free);
879 EXPECT_NOT_NULL(server_conn);
880 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
881 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
882
883 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
884 s2n_connection_ptr_free);
885 EXPECT_NOT_NULL(client_conn);
886 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
887 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
888
889 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
890 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
891 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
892
893 struct s2n_security_policy test_policy = security_policy_test_tls13_retry;
894 struct s2n_cipher_suite *test_cipher_suites[] = {
895 &s2n_tls13_aes_128_gcm_sha256,
896 &s2n_tls13_aes_256_gcm_sha384
897 };
898 struct s2n_cipher_preferences test_cipher_preferences = {
899 .count = s2n_array_len(test_cipher_suites),
900 .suites = test_cipher_suites,
901 };
902 test_policy.cipher_preferences = &test_cipher_preferences;
903
904 /* Force the HRR path. */
905 client_conn->security_policy_override = &test_policy;
906
907 /* Skip to before the client sends the second ClientHello. */
908 s2n_blocked_status blocked = 0;
909 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
910 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, HELLO_RETRY_MSG));
911 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, CLIENT_HELLO));
912 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, CLIENT_HELLO));
913
914 /* Modify a cipher suite. */
915 test_cipher_suites[1] = &s2n_tls13_chacha20_poly1305_sha256;
916
917 if (test_in_test_mode) {
918 /* Ensure that validation fails in test mode to prevent regressions. */
919 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, client_conn),
920 S2N_ERR_BAD_MESSAGE);
921 } else {
922 EXPECT_SUCCESS(s2n_in_unit_test_set(false));
923 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
924 EXPECT_SUCCESS(s2n_in_unit_test_set(true));
925 }
926 };
927
928 /* Test: Ensure that the connection fails if the cipher suite list changes such that the
929 * server cannot negotiate its original selection from the first ClientHello
930 *
931 *= https://www.rfc-editor.org/rfc/rfc8446#4.1.4
932 *= type=test
933 *# Servers MUST ensure that they negotiate the
934 *# same cipher suite when receiving a conformant updated ClientHello (if
935 *# the server selects the cipher suite as the first step in the
936 *# negotiation, then this will happen automatically).
937 **/
938 {
939 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
940 s2n_connection_ptr_free);
941 EXPECT_NOT_NULL(server_conn);
942 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
943 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
944
945 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
946 s2n_connection_ptr_free);
947 EXPECT_NOT_NULL(client_conn);
948 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
949 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
950
951 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
952 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
953 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
954
955 struct s2n_security_policy test_policy = security_policy_test_tls13_retry;
956 struct s2n_cipher_suite *test_cipher_suites[] = {
957 &s2n_tls13_aes_128_gcm_sha256,
958 &s2n_tls13_aes_256_gcm_sha384
959 };
960 struct s2n_cipher_preferences test_cipher_preferences = {
961 .count = s2n_array_len(test_cipher_suites),
962 .suites = test_cipher_suites,
963 };
964 test_policy.cipher_preferences = &test_cipher_preferences;
965
966 /* Force the HRR path. */
967 client_conn->security_policy_override = &test_policy;
968
969 /* Skip to before the client sends the second ClientHello. */
970 s2n_blocked_status blocked = 0;
971 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
972 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, HELLO_RETRY_MSG));
973 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, CLIENT_HELLO));
974 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, CLIENT_HELLO));
975
976 /* Replace the most preferred cipher suite, forcing the server to select a different
977 * cipher suite when processing the second ClientHello.
978 */
979 test_cipher_suites[0] = &s2n_tls13_chacha20_poly1305_sha256;
980
981 /* Test mode is disabled to skip the failing ClientHello comparison check due to a
982 * changed cipher suite list.
983 */
984 EXPECT_SUCCESS(s2n_in_unit_test_set(false));
985 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, client_conn),
986 S2N_ERR_BAD_MESSAGE);
987 EXPECT_SUCCESS(s2n_in_unit_test_set(true));
988 };
989
990 /* The server rejects a second ClientHello with a changed compression methods field */
991 for (uint8_t test_compression_method = 0; test_compression_method <= 1; test_compression_method++) {
992 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
993 s2n_connection_ptr_free);
994 EXPECT_NOT_NULL(server_conn);
995 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
996 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
997
998 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
999 s2n_connection_ptr_free);
1000 EXPECT_NOT_NULL(client_conn);
1001 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
1002 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
1003
1004 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
1005 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
1006 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
1007
1008 /* Force the HRR path. */
1009 client_conn->security_policy_override = &security_policy_test_tls13_retry;
1010
1011 /* Skip to before the client sends the second ClientHello. */
1012 s2n_blocked_status blocked = 0;
1013 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
1014 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, HELLO_RETRY_MSG));
1015 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, CLIENT_HELLO));
1016 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, CLIENT_HELLO));
1017
1018 /* Send the second ClientHello. */
1019 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
1020 EXPECT_SUCCESS(s2n_stuffer_wipe(&server_conn->handshake.io));
1021 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
1022 s2n_stuffer_data_available(&client_conn->handshake.io)));
1023
1024 struct s2n_stuffer client_hello_stuffer = { 0 };
1025 EXPECT_SUCCESS(s2n_stuffer_init_written(&client_hello_stuffer, &server_conn->handshake.io.blob));
1026
1027 /* Read up to the single null compression method byte */
1028 EXPECT_SUCCESS(s2n_stuffer_skip_read(&client_hello_stuffer, S2N_TLS_PROTOCOL_VERSION_LEN));
1029 EXPECT_SUCCESS(s2n_stuffer_skip_read(&client_hello_stuffer, S2N_TLS_RANDOM_DATA_LEN));
1030 uint8_t session_id_len = 0;
1031 EXPECT_SUCCESS(s2n_stuffer_read_uint8(&client_hello_stuffer, &session_id_len));
1032 EXPECT_SUCCESS(s2n_stuffer_skip_read(&client_hello_stuffer, session_id_len));
1033 uint16_t cipher_suites_len = 0;
1034 EXPECT_SUCCESS(s2n_stuffer_read_uint16(&client_hello_stuffer, &cipher_suites_len));
1035 EXPECT_SUCCESS(s2n_stuffer_skip_read(&client_hello_stuffer, cipher_suites_len));
1036 uint8_t compression_methods_len = 0;
1037 EXPECT_SUCCESS(s2n_stuffer_read_uint8(&client_hello_stuffer, &compression_methods_len));
1038 EXPECT_EQUAL(compression_methods_len, 1);
1039 uint32_t compression_method_pos = client_hello_stuffer.read_cursor;
1040
1041 /* Overwrite the compression method in the second ClientHello. */
1042 EXPECT_SUCCESS(s2n_stuffer_rewrite(&client_hello_stuffer));
1043 EXPECT_SUCCESS(s2n_stuffer_skip_write(&client_hello_stuffer, compression_method_pos));
1044 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&client_hello_stuffer, test_compression_method));
1045
1046 if (test_compression_method == 0) {
1047 /* A second ClientHello with a compression method of 0 shouldn't be different from
1048 * the first ClientHello, so validation should succeed.
1049 */
1050 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
1051 } else {
1052 EXPECT_FAILURE_WITH_ERRNO(s2n_client_hello_recv(server_conn), S2N_ERR_BAD_MESSAGE);
1053 }
1054 };
1055
1056 /* The server accepts a second ClientHello with a changed supported versions extension */
1057 {
1058 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
1059 s2n_connection_ptr_free);
1060 EXPECT_NOT_NULL(server_conn);
1061 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
1062 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
1063
1064 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
1065 s2n_connection_ptr_free);
1066 EXPECT_NOT_NULL(client_conn);
1067 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
1068 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
1069
1070 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
1071 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
1072 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
1073
1074 struct s2n_security_policy test_policy = security_policy_test_tls13_retry;
1075 test_policy.minimum_protocol_version = S2N_TLS10;
1076
1077 /* Force the HRR path. */
1078 client_conn->security_policy_override = &test_policy;
1079
1080 /* Skip to after the first ClientHello is received. */
1081 s2n_blocked_status blocked = 0;
1082 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
1083 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, HELLO_RETRY_MSG));
1084
1085 struct s2n_client_hello *client_hello = s2n_connection_get_client_hello(server_conn);
1086 EXPECT_NOT_NULL(client_hello);
1087 ssize_t first_supported_versions_length = s2n_client_hello_get_extension_length(client_hello,
1088 S2N_EXTENSION_SUPPORTED_VERSIONS);
1089
1090 /* Skip to before the client sends the second ClientHello. */
1091 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, CLIENT_HELLO));
1092 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, CLIENT_HELLO));
1093
1094 /* Increase the minimum protocol version for the security policy to send fewer
1095 * supported versions in the second ClientHello.
1096 */
1097 test_policy.minimum_protocol_version = S2N_TLS11;
1098
1099 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
1100
1101 /* Ensure that the supported versions extension changed. */
1102 ssize_t second_supported_versions_length = s2n_client_hello_get_extension_length(client_hello,
1103 S2N_EXTENSION_SUPPORTED_VERSIONS);
1104 EXPECT_TRUE(first_supported_versions_length != second_supported_versions_length);
1105 }
1106
1107 /* Test: The server rejects a supported versions extension with < TLS 1.3 in the second ClientHello */
1108 for (uint8_t test_version = S2N_TLS10; test_version <= S2N_TLS13; test_version++) {
1109 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
1110 s2n_connection_ptr_free);
1111 EXPECT_NOT_NULL(server_conn);
1112 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
1113 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
1114
1115 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
1116 s2n_connection_ptr_free);
1117 EXPECT_NOT_NULL(client_conn);
1118 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
1119 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
1120
1121 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
1122 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
1123 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
1124
1125 /* Force the HRR path. */
1126 client_conn->security_policy_override = &security_policy_test_tls13_retry;
1127
1128 /* Skip to before the client sends the second ClientHello. */
1129 s2n_blocked_status blocked = 0;
1130 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
1131 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, HELLO_RETRY_MSG));
1132 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, CLIENT_HELLO));
1133 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, CLIENT_HELLO));
1134
1135 /* Send the second ClientHello. */
1136 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
1137 EXPECT_SUCCESS(s2n_stuffer_wipe(&server_conn->handshake.io));
1138 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
1139 s2n_stuffer_data_available(&client_conn->handshake.io)));
1140
1141 /* Parse the ClientHello, but don't process the extensions yet. */
1142 EXPECT_SUCCESS(s2n_parse_client_hello(server_conn));
1143 server_conn->client_hello.parsed = true;
1144
1145 uint8_t extension_data[3] = { 0 };
1146 struct s2n_blob extension_blob = { 0 };
1147 EXPECT_SUCCESS(s2n_blob_init(&extension_blob, extension_data, sizeof(extension_data)));
1148 struct s2n_stuffer extension_stuffer = { 0 };
1149 EXPECT_SUCCESS(s2n_stuffer_init(&extension_stuffer, &extension_blob));
1150
1151 /* Overwrite the received supported versions extension with only the test version. */
1152 uint8_t extension_length = 2;
1153 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&extension_stuffer, extension_length));
1154 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&extension_stuffer, test_version / 10));
1155 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&extension_stuffer, test_version % 10));
1156
1157 struct s2n_client_hello *second_client_hello = s2n_connection_get_client_hello(server_conn);
1158 EXPECT_NOT_NULL(second_client_hello);
1159 s2n_extension_type_id supported_versions_id = 0;
1160 EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(S2N_EXTENSION_SUPPORTED_VERSIONS, &supported_versions_id));
1161 s2n_parsed_extension *extension = &second_client_hello->extensions.parsed_extensions[supported_versions_id];
1162 extension->extension_type = S2N_EXTENSION_SUPPORTED_VERSIONS;
1163 extension->extension = extension_blob;
1164
1165 int ret = s2n_client_hello_recv(server_conn);
1166 if (test_version == S2N_TLS13) {
1167 EXPECT_SUCCESS(ret);
1168 } else {
1169 EXPECT_FAILURE_WITH_ERRNO(ret, S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED);
1170 }
1171 }
1172
1173 /* Test: The server rejects a second ClientHello with a changed extension */
1174 {
1175 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
1176 s2n_connection_ptr_free);
1177 EXPECT_NOT_NULL(server_conn);
1178 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
1179 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
1180
1181 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
1182 s2n_connection_ptr_free);
1183 EXPECT_NOT_NULL(client_conn);
1184 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
1185 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
1186 EXPECT_SUCCESS(s2n_set_server_name(client_conn, "localhost"));
1187
1188 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
1189 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
1190 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
1191
1192 /* Force the HRR path */
1193 client_conn->security_policy_override = &security_policy_test_tls13_retry;
1194
1195 /* Send ClientHello */
1196 s2n_blocked_status blocked = 0;
1197 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
1198
1199 /* Change server name */
1200 client_conn->server_name[0]++;
1201
1202 /* Expect failure because second client hello doesn't match */
1203 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, client_conn),
1204 S2N_ERR_BAD_MESSAGE);
1205 };
1206
1207 /* Test: The server rejects a second ClientHello with a removed extension */
1208 {
1209 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
1210 s2n_connection_ptr_free);
1211 EXPECT_NOT_NULL(server_conn);
1212 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
1213 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
1214
1215 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
1216 s2n_connection_ptr_free);
1217 EXPECT_NOT_NULL(client_conn);
1218 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
1219 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
1220 EXPECT_SUCCESS(s2n_set_server_name(client_conn, "localhost"));
1221
1222 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
1223 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
1224 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
1225
1226 /* Force the HRR path */
1227 client_conn->security_policy_override = &security_policy_test_tls13_retry;
1228
1229 /* Send ClientHello */
1230 s2n_blocked_status blocked = 0;
1231 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
1232
1233 /* Clear server name.
1234 * Without a server name, we don't send the server name extension. */
1235 client_conn->server_name[0] = '\0';
1236
1237 /* Expect failure because second client hello doesn't match */
1238 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, client_conn),
1239 S2N_ERR_BAD_MESSAGE);
1240 };
1241
1242 /* Test: The server rejects a second ClientHello with an added extension */
1243 {
1244 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
1245 s2n_connection_ptr_free);
1246 EXPECT_NOT_NULL(server_conn);
1247 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
1248 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
1249
1250 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
1251 s2n_connection_ptr_free);
1252 EXPECT_NOT_NULL(client_conn);
1253 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
1254 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
1255
1256 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
1257 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
1258 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
1259
1260 /* Force the HRR path */
1261 client_conn->security_policy_override = &security_policy_test_tls13_retry;
1262
1263 /* Send ClientHello */
1264 s2n_blocked_status blocked = 0;
1265 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
1266
1267 /* Add a server name.
1268 * Without a server name, we don't send the server name extension. */
1269 EXPECT_SUCCESS(s2n_set_server_name(client_conn, "localhost"));
1270
1271 /* Expect failure because second client hello doesn't match */
1272 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, client_conn),
1273 S2N_ERR_BAD_MESSAGE);
1274 };
1275
1276 /*
1277 * Test: If the initial ClientHello includes all extensions, so does the second ClientHello.
1278 *
1279 * This includes TLS1.2 extensions, since the ClientHello is sent before
1280 * the client knows what version the server will negotiate.
1281 *
1282 * We have to test with all extensions to ensure that an s2n server will never reject
1283 * an s2n client's second ClientHello.
1284 *
1285 * TLS1.2 and TLS1.3 session tickets are mutually exclusive and use different
1286 * extensions, so we test once with each.
1287 */
1288 for (size_t tls13_tickets = 0; tls13_tickets < 2; tls13_tickets++) {
1289 DEFER_CLEANUP(struct s2n_config *client_config = s2n_config_new(),
1290 s2n_config_ptr_free);
1291 EXPECT_SUCCESS(s2n_config_disable_x509_verification(client_config));
1292
1293 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
1294 s2n_connection_ptr_free);
1295 EXPECT_NOT_NULL(server_conn);
1296 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
1297 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
1298
1299 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
1300 s2n_connection_ptr_free);
1301 EXPECT_NOT_NULL(client_conn);
1302 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
1303
1304 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
1305 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
1306 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
1307
1308 /* Force the HRR path */
1309 client_conn->security_policy_override = &security_policy_test_tls13_retry_with_pq;
1310
1311 /* Setup all extensions */
1312 uint8_t apn[] = "https";
1313 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "AWS-CRT-SDK-TLSv1.2-2025-PQ"));
1314 EXPECT_SUCCESS(s2n_config_set_status_request_type(client_config, S2N_STATUS_REQUEST_OCSP));
1315 EXPECT_SUCCESS(s2n_config_set_ct_support_level(client_config, S2N_CT_SUPPORT_REQUEST));
1316 EXPECT_SUCCESS(s2n_config_send_max_fragment_length(client_config, S2N_TLS_MAX_FRAG_LEN_4096));
1317 EXPECT_SUCCESS(s2n_config_set_session_tickets_onoff(client_config, 1));
1318 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
1319 EXPECT_SUCCESS(s2n_set_server_name(client_conn, "localhost"));
1320 EXPECT_SUCCESS(s2n_connection_append_protocol_preference(client_conn, apn, sizeof(apn)));
1321 EXPECT_SUCCESS(s2n_connection_set_early_data_expected(client_conn));
1322 client_conn->config->npn_supported = true;
1323 if (tls13_tickets) {
1324 EXPECT_OK(s2n_append_test_psk_with_early_data(client_conn, 1, &s2n_tls13_aes_256_gcm_sha384));
1325 }
1326 EXPECT_SUCCESS(s2n_connection_enable_quic(client_conn));
1327 /* Need to enable quic on both sides so they can communicate */
1328 EXPECT_SUCCESS(s2n_connection_enable_quic(server_conn));
1329
1330 /* Send and receive ClientHello */
1331 s2n_blocked_status blocked = 0;
1332 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
1333 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, HELLO_RETRY_MSG));
1334
1335 /* All ClientHello extensions must be present (except very specific exceptions) */
1336 s2n_extension_type_list *extensions = NULL;
1337 EXPECT_SUCCESS(s2n_extension_type_list_get(S2N_EXTENSION_LIST_CLIENT_HELLO, &extensions));
1338 for (size_t i = 0; i < extensions->count; i++) {
1339 uint16_t iana = extensions->extension_types[i]->iana_value;
1340
1341 /* The cookie is a special case and only appears AFTER the retry */
1342 if (iana == TLS_EXTENSION_COOKIE) {
1343 continue;
1344 }
1345
1346 /* PQ TLS 1.2 extension is not enabled */
1347 if (iana == TLS_EXTENSION_PQ_KEM_PARAMETERS) {
1348 continue;
1349 }
1350
1351 /* TLS1.2 session tickets and TLS1.3 session tickets are mutually exclusive */
1352 if (tls13_tickets && iana == TLS_EXTENSION_SESSION_TICKET) {
1353 continue;
1354 } else if (!tls13_tickets
1355 && (iana == TLS_EXTENSION_PRE_SHARED_KEY
1356 || iana == TLS_EXTENSION_PSK_KEY_EXCHANGE_MODES
1357 || iana == TLS_EXTENSION_EARLY_DATA)) {
1358 continue;
1359 }
1360
1361 /* No extension is sent for an initial handshake,
1362 * and TLS1.3 doesn't support renegotiation handshakes.
1363 */
1364 if (iana == TLS_EXTENSION_RENEGOTIATION_INFO) {
1365 continue;
1366 }
1367
1368 bool extension_exists = false;
1369 EXPECT_SUCCESS(s2n_client_hello_has_extension(&server_conn->client_hello,
1370 iana, &extension_exists));
1371 EXPECT_TRUE(extension_exists);
1372 }
1373
1374 /* Expect successful retry */
1375 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
1376 }
1377
1378 /* Processing an extension early does not affect the extension matching check.
1379 *
1380 * This test exists because of a previously released bug. Triggering the bug
1381 * required a specific series of events:
1382 * - The first ClientHello is received.
1383 * - The extensions are parsed.
1384 * - The ClientHello callback triggers.
1385 * - The customer's ClientHello callback implementation calls the s2n_get_server_name API.
1386 * - To retrieve the server name, s2n_get_server_name processes the server name extension early.
1387 * - The server name extension is wiped. Before the "processed" flag, we wiped extensions
1388 * to mark that they had been processed.
1389 * - The server sends a HelloRetryRequest, triggering a retry.
1390 * - The second ClientHello is received.
1391 * - The extensions are parsed.
1392 * - The customer's ClientHello callback does NOT trigger this time. The callback only
1393 * triggers after the first ClientHello.
1394 * - Therefore, s2n_get_server_name is not called and the server name extension is not
1395 * processed early or wiped.
1396 * - The first ClientHello is compared to the second ClientHello. The second ClientHello
1397 * appears to contain a server name extension not present in the first ClientHello.
1398 * - The handshake fails because the ClientHellos must match.
1399 */
1400 {
1401 char server_name[] = "test server name";
1402
1403 DEFER_CLEANUP(struct s2n_config *config_with_cb = s2n_config_new(),
1404 s2n_config_ptr_free);
1405 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config_with_cb, chain_and_key));
1406 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config_with_cb, "default_tls13"));
1407 EXPECT_SUCCESS(s2n_config_disable_x509_verification(config_with_cb));
1408
1409 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
1410 s2n_connection_ptr_free);
1411 EXPECT_NOT_NULL(server_conn);
1412 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
1413 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config_with_cb));
1414
1415 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
1416 s2n_connection_ptr_free);
1417 EXPECT_NOT_NULL(client_conn);
1418 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
1419 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config_with_cb));
1420
1421 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
1422 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
1423 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
1424
1425 /* Setup server name and client hello callback */
1426 EXPECT_SUCCESS(s2n_set_server_name(client_conn, server_name));
1427 EXPECT_SUCCESS(s2n_config_set_client_hello_cb(config_with_cb,
1428 s2n_client_hello_cb_with_get_server_name, server_name));
1429
1430 /* Force the HRR path */
1431 client_conn->security_policy_override = &security_policy_test_tls13_retry;
1432
1433 /* Handshake should complete as expected */
1434 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
1435 EXPECT_EQUAL(strcmp(s2n_get_server_name(server_conn), server_name), 0);
1436 EXPECT_TRUE(IS_HELLO_RETRY_HANDSHAKE(client_conn));
1437 EXPECT_TRUE(IS_HELLO_RETRY_HANDSHAKE(server_conn));
1438 };
1439 }
1440
1441 /**
1442 * Ensure all hello retry extensions sent by the server will have first
1443 * been sent by the client.
1444 *
1445 *= https://www.rfc-editor.org/rfc/rfc8446#4.1.4
1446 *= type=test
1447 *# As with the ServerHello, a HelloRetryRequest MUST NOT contain any
1448 *# extensions that were not first offered by the client in its
1449 *# ClientHello, with the exception of optionally the "cookie" (see
1450 *# Section 4.2.2) extension.
1451 **/
1452 {
1453 s2n_extension_type_list *hello_retry_extension_types = 0;
1454 POSIX_GUARD(s2n_extension_type_list_get(S2N_EXTENSION_LIST_HELLO_RETRY_REQUEST, &hello_retry_extension_types));
1455
1456 for (int i = 0; i < hello_retry_extension_types->count; ++i) {
1457 const s2n_extension_type *const extension_type = hello_retry_extension_types->extension_types[i];
1458
1459 /* with the exception of optionally the "cookie" extension. */
1460 if (extension_type->iana_value == TLS_EXTENSION_COOKIE) {
1461 continue;
1462 }
1463
1464 EXPECT_TRUE(extension_type->is_response);
1465 }
1466 };
1467
1468 /**
1469 * Ensure each of the following are checked: legacy_version,
1470 * legacy_session_id_echo, cipher_suite, and
1471 * legacy_compression_method
1472 *
1473 *= https://www.rfc-editor.org/rfc/rfc8446#4.1.4
1474 *= type=test
1475 *# Upon receipt of a HelloRetryRequest, the client MUST check the
1476 *# legacy_version, legacy_session_id_echo, cipher_suite, and
1477 *# legacy_compression_method as specified in Section 4.1.3 and then
1478 *# process the extensions, starting with determining the version using
1479 *# "supported_versions".
1480 **/
1481 {
1482 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL,
1483 s2n_cert_chain_and_key_ptr_free);
1484 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
1485 S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY));
1486
1487 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(),
1488 s2n_config_ptr_free);
1489 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
1490 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13"));
1491 EXPECT_SUCCESS(s2n_config_disable_x509_verification(config));
1492
1493 /* The client MUST check the legacy_version */
1494 {
1495 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
1496 s2n_connection_ptr_free);
1497 EXPECT_NOT_NULL(server_conn);
1498 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
1499
1500 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
1501 s2n_connection_ptr_free);
1502 EXPECT_NOT_NULL(client_conn);
1503 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
1504
1505 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
1506 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
1507 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
1508
1509 /* Force the HRR path */
1510 client_conn->security_policy_override = &security_policy_test_tls13_retry;
1511
1512 /* ClientHello 1 */
1513 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
1514 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
1515 s2n_stuffer_data_available(&client_conn->handshake.io)));
1516
1517 /* Server receives ClientHello 1 */
1518 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
1519 EXPECT_SUCCESS(s2n_set_connection_hello_retry_flags(server_conn));
1520
1521 /* Force the server to send an erroneous legacy protocol version in the HelloRetryRequest message */
1522 server_conn->actual_protocol_version = S2N_TLS11;
1523
1524 /* Server sends HelloRetryRequest */
1525 EXPECT_SUCCESS(s2n_server_hello_retry_send(server_conn));
1526
1527 EXPECT_SUCCESS(s2n_stuffer_wipe(&client_conn->handshake.io));
1528 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io,
1529 s2n_stuffer_data_available(&server_conn->handshake.io)));
1530 client_conn->handshake.message_number = HELLO_RETRY_MSG_NO;
1531
1532 /* Client receives HelloRetryRequest */
1533 EXPECT_FAILURE_WITH_ERRNO(s2n_server_hello_recv(client_conn),
1534 S2N_ERR_INVALID_HELLO_RETRY);
1535 };
1536
1537 /* The client MUST check the legacy_session_id_echo */
1538 {
1539 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
1540 s2n_connection_ptr_free);
1541 EXPECT_NOT_NULL(server_conn);
1542 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
1543
1544 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
1545 s2n_connection_ptr_free);
1546 EXPECT_NOT_NULL(client_conn);
1547 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
1548
1549 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
1550 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
1551 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
1552
1553 /* Force the HRR path */
1554 client_conn->security_policy_override = &security_policy_test_tls13_retry;
1555
1556 /* ClientHello 1 */
1557 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
1558 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
1559 s2n_stuffer_data_available(&client_conn->handshake.io)));
1560
1561 /* Server receives ClientHello 1 */
1562 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
1563 EXPECT_SUCCESS(s2n_set_connection_hello_retry_flags(server_conn));
1564
1565 /* Set a session id that's different from the client hello */
1566 POSIX_CHECKED_MEMSET(&server_conn->session_id, 0, S2N_TLS_SESSION_ID_MAX_LEN);
1567
1568 /* Server sends HelloRetryRequest */
1569 EXPECT_SUCCESS(s2n_server_hello_retry_send(server_conn));
1570
1571 EXPECT_SUCCESS(s2n_stuffer_wipe(&client_conn->handshake.io));
1572 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io,
1573 s2n_stuffer_data_available(&server_conn->handshake.io)));
1574 client_conn->handshake.message_number = HELLO_RETRY_MSG_NO;
1575
1576 /* Client receives HelloRetryRequest */
1577 EXPECT_FAILURE_WITH_ERRNO(s2n_server_hello_recv(client_conn),
1578 S2N_ERR_BAD_MESSAGE);
1579 };
1580
1581 /**
1582 * The client MUST check the cipher_suite
1583 *
1584 *= https://www.rfc-editor.org/rfc/rfc8446#4.1.4
1585 *= type=test
1586 *# A client which receives a cipher suite that was not offered MUST
1587 *# abort the handshake.
1588 **/
1589 {
1590 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "20200207"));
1591
1592 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
1593 s2n_connection_ptr_free);
1594 EXPECT_NOT_NULL(server_conn);
1595 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
1596
1597 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
1598 s2n_connection_ptr_free);
1599 EXPECT_NOT_NULL(client_conn);
1600 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
1601
1602 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
1603 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
1604 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
1605
1606 /* Force the HRR path */
1607 client_conn->security_policy_override = &security_policy_test_tls13_retry;
1608
1609 /* Send ClientHello */
1610 s2n_blocked_status blocked = 0;
1611 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
1612
1613 /* Receive ClientHello */
1614 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, HELLO_RETRY_MSG));
1615
1616 /*
1617 * Pick a cipher that wasn't offered in the CH, and should cause the
1618 * handshake to abort.
1619 */
1620 server_conn->secure->cipher_suite = &s2n_ecdhe_ecdsa_with_aes_256_gcm_sha384;
1621
1622 /* Finish handshake */
1623 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, client_conn),
1624 S2N_ERR_CIPHER_NOT_SUPPORTED);
1625 };
1626
1627 /* The client MUST check the legacy_compression_method */
1628 {
1629 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
1630 s2n_connection_ptr_free);
1631 EXPECT_NOT_NULL(server_conn);
1632 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
1633
1634 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
1635 s2n_connection_ptr_free);
1636 EXPECT_NOT_NULL(client_conn);
1637 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
1638
1639 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
1640 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
1641 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
1642
1643 /* Force the HRR path */
1644 client_conn->security_policy_override = &security_policy_test_tls13_retry;
1645
1646 /* ClientHello 1 */
1647 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
1648 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
1649 s2n_stuffer_data_available(&client_conn->handshake.io)));
1650
1651 /* Server receives ClientHello 1 */
1652 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
1653 EXPECT_SUCCESS(s2n_set_connection_hello_retry_flags(server_conn));
1654
1655 /* Server sends HelloRetryRequest */
1656 POSIX_CHECKED_MEMCPY(server_conn->handshake_params.server_random, hello_retry_req_random, S2N_TLS_RANDOM_DATA_LEN);
1657 EXPECT_SUCCESS(s2n_server_hello_write_message(server_conn));
1658
1659 /* Overwrite compression method to 1 */
1660 struct s2n_stuffer *io = &server_conn->handshake.io;
1661 io->write_cursor -= 1;
1662 EXPECT_SUCCESS(s2n_stuffer_write_uint8(io, 1));
1663
1664 /* Write the extensions */
1665 EXPECT_SUCCESS(s2n_server_extensions_send(server_conn, &server_conn->handshake.io));
1666
1667 EXPECT_SUCCESS(s2n_stuffer_wipe(&client_conn->handshake.io));
1668 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io,
1669 s2n_stuffer_data_available(&server_conn->handshake.io)));
1670 client_conn->handshake.message_number = HELLO_RETRY_MSG_NO;
1671
1672 /* Client receives HelloRetryRequest */
1673 EXPECT_FAILURE_WITH_ERRNO(s2n_server_hello_recv(client_conn),
1674 S2N_ERR_BAD_MESSAGE);
1675 };
1676 };
1677
1678 /**
1679 *= https://www.rfc-editor.org/rfc/rfc8446#4.1.4
1680 *= type=test
1681 *# The server's extensions MUST contain "supported_versions".
1682 **/
1683 {
1684 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL,
1685 s2n_cert_chain_and_key_ptr_free);
1686 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
1687 S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY));
1688
1689 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(),
1690 s2n_config_ptr_free);
1691 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
1692 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13"));
1693 EXPECT_SUCCESS(s2n_config_disable_x509_verification(config));
1694
1695 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
1696 s2n_connection_ptr_free);
1697 EXPECT_NOT_NULL(server_conn);
1698 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
1699
1700 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
1701 s2n_connection_ptr_free);
1702 EXPECT_NOT_NULL(client_conn);
1703 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
1704
1705 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
1706 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
1707 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
1708
1709 /* Force the HRR path */
1710 client_conn->security_policy_override = &security_policy_test_tls13_retry;
1711
1712 /* ClientHello 1 */
1713 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
1714
1715 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
1716 s2n_stuffer_data_available(&client_conn->handshake.io)));
1717
1718 /* Server receives ClientHello 1 */
1719 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
1720 EXPECT_SUCCESS(s2n_set_connection_hello_retry_flags(server_conn));
1721
1722 POSIX_CHECKED_MEMCPY(server_conn->handshake_params.server_random, hello_retry_req_random, S2N_TLS_RANDOM_DATA_LEN);
1723 POSIX_GUARD(s2n_server_hello_write_message(server_conn));
1724 struct s2n_stuffer_reservation total_extensions_size = { 0 };
1725 POSIX_GUARD(s2n_stuffer_reserve_uint16(&server_conn->handshake.io, &total_extensions_size));
1726
1727 /* Only send key share extension - exclude supported_versions */
1728 s2n_extension_send(&s2n_server_key_share_extension, server_conn, &server_conn->handshake.io);
1729
1730 POSIX_GUARD(s2n_stuffer_write_vector_size(&total_extensions_size));
1731
1732 EXPECT_SUCCESS(s2n_stuffer_wipe(&client_conn->handshake.io));
1733 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io,
1734 s2n_stuffer_data_available(&server_conn->handshake.io)));
1735 client_conn->handshake.message_number = HELLO_RETRY_MSG_NO;
1736
1737 /* Client receives HelloRetryRequest */
1738 EXPECT_FAILURE_WITH_ERRNO(s2n_server_hello_recv(client_conn),
1739 S2N_ERR_MISSING_EXTENSION);
1740 };
1741
1742 /**
1743 *= https://www.rfc-editor.org/rfc/rfc8446#4.1.4
1744 *= type=test
1745 *# Servers MUST ensure that they negotiate the
1746 *# same cipher suite when receiving a conformant updated ClientHello (if
1747 *# the server selects the cipher suite as the first step in the
1748 *# negotiation, then this will happen automatically).
1749 **/
1750 {
1751 /* Create a custom security policy so it can be changed mid-handshake */
1752 struct s2n_cipher_suite *test_cipher_suites[] = {
1753 &s2n_tls13_aes_128_gcm_sha256,
1754 &s2n_tls13_aes_256_gcm_sha384
1755 };
1756 struct s2n_cipher_preferences test_cipher_preferences = {
1757 .count = s2n_array_len(test_cipher_suites),
1758 .suites = test_cipher_suites,
1759 };
1760 struct s2n_security_policy security_policy_test_tls13_retry_temp = {
1761 .minimum_protocol_version = S2N_TLS10,
1762 .cipher_preferences = &test_cipher_preferences,
1763 .kem_preferences = &kem_preferences_null,
1764 .signature_preferences = &s2n_signature_preferences_20200207,
1765 .certificate_signature_preferences = &s2n_certificate_signature_preferences_20201110,
1766 .ecc_preferences = &ecc_preferences_for_retry,
1767 };
1768
1769 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL, s2n_cert_chain_and_key_ptr_free);
1770 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
1771 S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY));
1772
1773 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
1774 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
1775 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13"));
1776 EXPECT_SUCCESS(s2n_config_disable_x509_verification(config));
1777
1778 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
1779 EXPECT_NOT_NULL(server_conn);
1780 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
1781 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
1782
1783 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
1784 EXPECT_NOT_NULL(client_conn);
1785 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
1786 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
1787
1788 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
1789 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
1790 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
1791
1792 /* Force the HRR path */
1793 client_conn->security_policy_override = &security_policy_test_tls13_retry_temp;
1794
1795 /* Send ClientHello */
1796 s2n_blocked_status blocked = 0;
1797 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
1798
1799 /* Receive ClientHello */
1800 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, HELLO_RETRY_MSG));
1801
1802 /* Rearrange the cipher preference order so that a different cipher will be
1803 * picked by the server */
1804 server_conn->config->security_policy->cipher_preferences->suites[0] = &s2n_tls13_aes_256_gcm_sha384;
1805
1806 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, client_conn),
1807 S2N_ERR_BAD_MESSAGE);
1808 };
1809
1810 /**
1811 * Ensure that the client aborts the handshake if selected_version
1812 * differs in the received server hellos
1813 *
1814 *= https://www.rfc-editor.org/rfc/rfc8446#4.1.4
1815 *= type=test
1816 *# The value of selected_version in the HelloRetryRequest
1817 *# "supported_versions" extension MUST be retained in the ServerHello,
1818 *# and a client MUST abort the handshake with an "illegal_parameter"
1819 *# alert if the value changes.
1820 **/
1821 {
1822 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL, s2n_cert_chain_and_key_ptr_free);
1823 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
1824 S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY));
1825
1826 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
1827 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
1828 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13"));
1829 EXPECT_SUCCESS(s2n_config_disable_x509_verification(config));
1830
1831 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
1832 EXPECT_NOT_NULL(server_conn);
1833 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
1834 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
1835
1836 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
1837 EXPECT_NOT_NULL(client_conn);
1838 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
1839 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
1840
1841 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
1842 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
1843 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
1844
1845 /* Force the HRR path */
1846 client_conn->security_policy_override = &security_policy_test_tls13_retry;
1847
1848 /* Send ClientHello */
1849 s2n_blocked_status blocked = 0;
1850 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
1851
1852 /* Receive ClientHello */
1853 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, HELLO_RETRY_MSG));
1854
1855 /* Skip to before server sends ServerHello */
1856 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, CLIENT_HELLO));
1857 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, CLIENT_HELLO));
1858
1859 /* Change the server_protocol_version so the value found in the ServerHello
1860 * differs from the value found in the HelloRetryRequest */
1861 server_conn->server_protocol_version = S2N_TLS13 + 10;
1862
1863 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, client_conn),
1864 S2N_ERR_BAD_MESSAGE);
1865 };
1866
1867 /**
1868 *= https://www.rfc-editor.org/rfc/rfc8446#4.2.8
1869 *= type=test
1870 *# Upon receipt of this extension in a HelloRetryRequest, the client
1871 *# MUST verify that (1) the selected_group field corresponds to a group
1872 *# which was provided in the "supported_groups" extension in the
1873 *# original ClientHello
1874 **/
1875 {
1876 /* Create a custom security policy without secp521r1 */
1877 const struct s2n_ecc_named_curve *const test_ecc_pref_list_for_retry[] = {
1878 &s2n_ecc_curve_secp256r1,
1879 &s2n_ecc_curve_secp384r1,
1880 };
1881 const struct s2n_ecc_preferences test_ecc_preferences_for_retry = {
1882 .count = s2n_array_len(test_ecc_pref_list_for_retry),
1883 .ecc_curves = test_ecc_pref_list_for_retry,
1884 };
1885 struct s2n_security_policy security_policy_test_tls13_retry_temp = {
1886 .minimum_protocol_version = S2N_TLS10,
1887 .cipher_preferences = &cipher_preferences_20190801,
1888 .kem_preferences = &kem_preferences_null,
1889 .signature_preferences = &s2n_signature_preferences_20200207,
1890 .certificate_signature_preferences = &s2n_certificate_signature_preferences_20201110,
1891 .ecc_preferences = &test_ecc_preferences_for_retry,
1892 };
1893
1894 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL,
1895 s2n_cert_chain_and_key_ptr_free);
1896 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
1897 S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN,
1898 S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY));
1899
1900 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(),
1901 s2n_config_ptr_free);
1902 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
1903 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13"));
1904 EXPECT_SUCCESS(s2n_config_disable_x509_verification(config));
1905
1906 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
1907 s2n_connection_ptr_free);
1908 EXPECT_NOT_NULL(server_conn);
1909 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
1910
1911 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
1912 s2n_connection_ptr_free);
1913 EXPECT_NOT_NULL(client_conn);
1914 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
1915
1916 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
1917 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
1918 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
1919
1920 /* Force the HRR path */
1921 client_conn->security_policy_override = &security_policy_test_tls13_retry_temp;
1922
1923 /* ClientHello 1 */
1924 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
1925 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
1926 s2n_stuffer_data_available(&client_conn->handshake.io)));
1927
1928 /* Server receives ClientHello 1 */
1929 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
1930 EXPECT_SUCCESS(s2n_set_connection_hello_retry_flags(server_conn));
1931
1932 /* Server sends HelloRetryRequest */
1933 EXPECT_SUCCESS(s2n_server_hello_retry_send(server_conn));
1934
1935 EXPECT_SUCCESS(s2n_stuffer_wipe(&client_conn->handshake.io));
1936 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io,
1937 s2n_stuffer_data_available(&server_conn->handshake.io)));
1938 client_conn->handshake.message_number = HELLO_RETRY_MSG_NO;
1939
1940 /* Set the curve to secp521r1, which was not provided in supported_groups */
1941 client_conn->kex_params.server_ecc_evp_params.negotiated_curve = &s2n_ecc_curve_secp521r1;
1942
1943 /* Client receives HelloRetryRequest */
1944 EXPECT_FAILURE_WITH_ERRNO(s2n_server_hello_recv(client_conn),
1945 S2N_ERR_INVALID_HELLO_RETRY);
1946 };
1947
1948 /**
1949 *= https://www.rfc-editor.org/rfc/rfc8446#4.2.8
1950 *= type=test
1951 *# If using (EC)DHE key establishment and a HelloRetryRequest containing a
1952 *# "key_share" extension was received by the client, the client MUST
1953 *# verify that the selected NamedGroup in the ServerHello is the same as
1954 *# that in the HelloRetryRequest. If this check fails, the client MUST
1955 *# abort the handshake with an "illegal_parameter" alert.
1956 **/
1957 {
1958 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL,
1959 s2n_cert_chain_and_key_ptr_free);
1960 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
1961 S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN,
1962 S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY));
1963
1964 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(),
1965 s2n_config_ptr_free);
1966 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
1967 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13"));
1968 EXPECT_SUCCESS(s2n_config_disable_x509_verification(config));
1969
1970 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
1971 s2n_connection_ptr_free);
1972 EXPECT_NOT_NULL(server_conn);
1973 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
1974 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
1975
1976 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
1977 s2n_connection_ptr_free);
1978 EXPECT_NOT_NULL(client_conn);
1979 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
1980 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
1981
1982 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
1983 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
1984 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
1985
1986 /* Force the HRR path */
1987 client_conn->security_policy_override = &security_policy_test_tls13_retry;
1988
1989 /* Skip to before client receives ServerHello 2 */
1990 s2n_blocked_status blocked = 0;
1991 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
1992 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, HELLO_RETRY_MSG));
1993 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, CLIENT_HELLO));
1994 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, CLIENT_HELLO));
1995 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
1996 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, SERVER_HELLO));
1997 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, ENCRYPTED_EXTENSIONS));
1998
1999 /* Set the negotiated curve to something other than what was sent in the HRR */
2000 client_conn->kex_params.server_ecc_evp_params.negotiated_curve = &s2n_ecc_curve_secp521r1;
2001
2002 /* Client receives ServerHello 2 */
2003 EXPECT_ERROR_WITH_ERRNO(s2n_negotiate_until_message(client_conn, &blocked, ENCRYPTED_EXTENSIONS),
2004 S2N_ERR_BAD_MESSAGE);
2005 };
2006
2007 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
2008
2009 END_TEST();
2010 }