Debian Codegraph @

crt/s2n/tests/unit/s2n_server_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 "error/s2n_errno.h"
17 #include "s2n_test.h"
18 #include "testlib/s2n_testlib.h"
19 #include "tls/extensions/s2n_key_share.h"
20 #include "tls/extensions/s2n_server_key_share.h"
21 #include "tls/extensions/s2n_server_supported_versions.h"
22 #include "tls/s2n_cipher_suites.h"
23 #include "tls/s2n_internal.h"
24 #include "tls/s2n_security_policies.h"
25 #include "tls/s2n_tls.h"
26 #include "tls/s2n_tls13.h"
27 #include "tls/s2n_tls13_handshake.h"
28 #include "utils/s2n_bitmap.h"
29 #include "utils/s2n_result.h"
30
31 #define HELLO_RETRY_MSG_NO 1
32
33 const uint8_t SESSION_ID_SIZE = 1;
34 const uint8_t COMPRESSION_METHOD_SIZE = 1;
35
36 struct client_hello_context {
37 int invocations;
38 s2n_client_hello_cb_mode mode;
39 bool mark_done;
40 };
41
42 int s2n_negotiate_poll_hello_retry(struct s2n_connection *server_conn,
43 struct s2n_connection *client_conn,
44 struct client_hello_context *client_hello_ctx)
45 {
46 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
47 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate(client_conn, &blocked), S2N_ERR_IO_BLOCKED);
48
49 /* complete the callback on the next call */
50 client_hello_ctx->mark_done = true;
51 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
52
53 /*
54 * hello retry will invoke the s2n_negotiate twice but the callback should
55 * be called once regardless of polling
56 */
57 EXPECT_EQUAL(client_hello_ctx->invocations, 1);
58
59 return S2N_SUCCESS;
60 }
61
62 static int client_hello_detect_duplicate_calls(struct s2n_connection *conn, void *ctx)
63 {
64 if (ctx == NULL) {
65 return -1;
66 }
67
68 struct client_hello_context *client_hello_ctx = ctx;
69
70 /* Incremet counter */
71 client_hello_ctx->invocations++;
72 EXPECT_EQUAL(client_hello_ctx->invocations, 1);
73 if (client_hello_ctx->mode == S2N_CLIENT_HELLO_CB_NONBLOCKING) {
74 EXPECT_SUCCESS(s2n_client_hello_cb_done(conn));
75 }
76 return 0;
77 }
78
79 int s2n_client_hello_poll_cb(struct s2n_connection *conn, void *ctx)
80 {
81 struct client_hello_context *client_hello_ctx = NULL;
82 if (ctx == NULL) {
83 return -1;
84 }
85 client_hello_ctx = ctx;
86 /* Increment counter to ensure that callback was invoked */
87 client_hello_ctx->invocations++;
88
89 if (client_hello_ctx->mark_done) {
90 EXPECT_SUCCESS(s2n_client_hello_cb_done(conn));
91 return S2N_SUCCESS;
92 }
93
94 return S2N_SUCCESS;
95 }
96
97 S2N_RESULT hello_retry_client_hello_cb_test()
98 {
99 struct s2n_cert_chain_and_key *tls13_chain_and_key = NULL;
100 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&tls13_chain_and_key,
101 S2N_ECDSA_P384_PKCS1_CERT_CHAIN, S2N_ECDSA_P384_PKCS1_KEY));
102 EXPECT_NOT_NULL(tls13_chain_and_key);
103
104 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
105 EXPECT_NOT_NULL(config);
106
107 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config));
108 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, tls13_chain_and_key));
109 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13"));
110
111 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
112 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
113 EXPECT_NOT_NULL(server_conn);
114 EXPECT_NOT_NULL(client_conn);
115
116 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
117 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
118
119 struct s2n_test_io_pair io_pair = { 0 };
120 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
121 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
122
123 /* Force HRR path */
124 client_conn->security_policy_override = &security_policy_test_tls13_retry;
125
126 /* setup the client hello callback */
127 struct client_hello_context client_hello_ctx = { .invocations = 0,
128 .mode = S2N_CLIENT_HELLO_CB_NONBLOCKING,
129 .mark_done = false };
130 EXPECT_SUCCESS(s2n_config_set_client_hello_cb(config,
131 s2n_client_hello_poll_cb, &client_hello_ctx));
132 EXPECT_SUCCESS(s2n_config_set_client_hello_cb_mode(config,
133 S2N_CLIENT_HELLO_CB_NONBLOCKING));
134
135 /* negotiate and make assertions */
136 EXPECT_SUCCESS(s2n_negotiate_poll_hello_retry(server_conn, client_conn, &client_hello_ctx));
137
138 /* check hello retry state */
139 EXPECT_TRUE(IS_HELLO_RETRY_HANDSHAKE(client_conn));
140 EXPECT_TRUE(IS_HELLO_RETRY_HANDSHAKE(server_conn));
141
142 /* cleanup */
143 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(tls13_chain_and_key));
144 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
145 return S2N_RESULT_OK;
146 }
147
148 int main(int argc, char **argv)
149 {
150 BEGIN_TEST();
151
152 if (!s2n_is_tls13_fully_supported()) {
153 END_TEST();
154 }
155
156 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
157
158 /* Send Hello Retry Request messages */
159 {
160 struct s2n_config *server_config = NULL;
161 struct s2n_connection *server_conn = NULL;
162
163 EXPECT_NOT_NULL(server_config = s2n_config_new());
164 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
165 EXPECT_SUCCESS(s2n_connection_allow_all_response_extensions(server_conn));
166 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
167
168 struct s2n_stuffer *server_stuffer = &server_conn->handshake.io;
169
170 uint32_t total = S2N_TLS_PROTOCOL_VERSION_LEN
171 + S2N_TLS_RANDOM_DATA_LEN
172 + SESSION_ID_SIZE
173 + server_conn->session_id_len
174 + S2N_TLS_CIPHER_SUITE_LEN
175 + COMPRESSION_METHOD_SIZE;
176
177 EXPECT_SUCCESS(s2n_connection_set_all_protocol_versions(server_conn, S2N_TLS13));
178 server_conn->secure->cipher_suite = &s2n_ecdhe_ecdsa_with_aes_128_gcm_sha256;
179 server_conn->kex_params.server_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
180 server_conn->kex_params.client_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
181 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&server_conn->kex_params.client_ecc_evp_params));
182
183 EXPECT_SUCCESS(s2n_set_connection_hello_retry_flags(server_conn));
184 EXPECT_OK(s2n_conn_choose_state_machine(server_conn, S2N_TLS13));
185
186 /* The client will need a key share extension to properly parse the hello */
187 /* Total extension size + size of each extension */
188 total += 2 + s2n_extensions_server_supported_versions_size(server_conn)
189 + s2n_extensions_server_key_share_send_size(server_conn);
190
191 EXPECT_TRUE(s2n_is_hello_retry_message(server_conn));
192 EXPECT_SUCCESS(s2n_server_hello_retry_send(server_conn));
193
194 EXPECT_EQUAL(s2n_stuffer_data_available(server_stuffer), total);
195
196 EXPECT_NOT_NULL(server_conn->kex_params.server_ecc_evp_params.negotiated_curve);
197 EXPECT_NULL(server_conn->kex_params.server_ecc_evp_params.evp_pkey);
198 EXPECT_TRUE(memcmp(server_conn->handshake_params.server_random, hello_retry_req_random, S2N_TLS_RANDOM_DATA_LEN) == 0);
199
200 EXPECT_SUCCESS(s2n_config_free(server_config));
201 EXPECT_SUCCESS(s2n_connection_free(server_conn));
202 };
203
204 /* Verify the requires_retry flag causes a retry to be sent */
205 {
206 struct s2n_config *conf = NULL;
207 struct s2n_connection *conn = NULL;
208
209 EXPECT_NOT_NULL(conf = s2n_config_new());
210 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
211 EXPECT_SUCCESS(s2n_connection_set_config(conn, conf));
212
213 EXPECT_SUCCESS(s2n_connection_set_all_protocol_versions(conn, S2N_TLS13));
214 conn->secure->cipher_suite = &s2n_ecdhe_ecdsa_with_aes_128_gcm_sha256;
215 conn->kex_params.server_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
216 conn->kex_params.client_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
217 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&conn->kex_params.client_ecc_evp_params));
218
219 EXPECT_OK(s2n_conn_choose_state_machine(conn, S2N_TLS13));
220 EXPECT_SUCCESS(s2n_set_connection_hello_retry_flags(conn));
221
222 EXPECT_TRUE(s2n_is_hello_retry_message(conn));
223 EXPECT_SUCCESS(s2n_server_hello_retry_send(conn));
224
225 EXPECT_SUCCESS(s2n_config_free(conf));
226 EXPECT_SUCCESS(s2n_connection_free(conn));
227 };
228
229 /* Retry requests with incorrect random data are not accepted */
230 {
231 struct s2n_config *conf = NULL;
232 struct s2n_connection *conn = NULL;
233
234 EXPECT_NOT_NULL(conf = s2n_config_new());
235 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
236 EXPECT_SUCCESS(s2n_connection_set_config(conn, conf));
237
238 struct s2n_stuffer *io = &conn->handshake.io;
239 EXPECT_SUCCESS(s2n_connection_set_all_protocol_versions(conn, S2N_TLS13));
240
241 /* protocol version */
242 EXPECT_SUCCESS(s2n_stuffer_write_uint8(io, S2N_TLS12 / 10));
243 EXPECT_SUCCESS(s2n_stuffer_write_uint8(io, S2N_TLS12 % 10));
244
245 /* random data */
246 uint8_t bad_retry_random[S2N_TLS_RANDOM_DATA_LEN] = { 0 };
247 EXPECT_SUCCESS(s2n_stuffer_write_bytes(io, bad_retry_random, S2N_TLS_RANDOM_DATA_LEN));
248
249 /* session id */
250 uint8_t session_id[S2N_TLS_SESSION_ID_MAX_LEN] = { 0 };
251 EXPECT_SUCCESS(s2n_stuffer_write_uint8(io, S2N_TLS_SESSION_ID_MAX_LEN));
252 EXPECT_SUCCESS(s2n_stuffer_write_bytes(io, session_id, S2N_TLS_SESSION_ID_MAX_LEN));
253
254 /* cipher suites */
255 EXPECT_SUCCESS(s2n_stuffer_write_uint16(io, 0x1301));
256
257 /* no compression */
258 EXPECT_SUCCESS(s2n_stuffer_write_uint8(io, 0));
259
260 EXPECT_FAILURE_WITH_ERRNO(s2n_server_hello_recv(conn), S2N_ERR_BAD_MESSAGE);
261
262 EXPECT_FALSE(s2n_is_hello_retry_message(conn));
263
264 EXPECT_SUCCESS(s2n_config_free(conf));
265 EXPECT_SUCCESS(s2n_connection_free(conn));
266 };
267
268 /* Verify the client key share extension properly handles HelloRetryRequests */
269 {
270 struct s2n_connection *server_conn = NULL;
271 struct s2n_connection *client_conn = NULL;
272
273 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
274 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
275
276 struct s2n_stuffer *extension_stuffer = &server_conn->handshake.io;
277
278 EXPECT_SUCCESS(s2n_connection_allow_response_extension(client_conn, s2n_server_key_share_extension.iana_value));
279 EXPECT_SUCCESS(s2n_connection_allow_response_extension(server_conn, s2n_server_key_share_extension.iana_value));
280
281 POSIX_CHECKED_MEMCPY(server_conn->handshake_params.server_random, hello_retry_req_random, S2N_TLS_RANDOM_DATA_LEN);
282 EXPECT_SUCCESS(s2n_connection_set_all_protocol_versions(server_conn, S2N_TLS13));
283 server_conn->kex_params.server_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
284 server_conn->kex_params.client_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
285 EXPECT_SUCCESS(s2n_set_connection_hello_retry_flags(server_conn));
286 EXPECT_OK(s2n_conn_choose_state_machine(server_conn, S2N_TLS13));
287 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&server_conn->kex_params.client_ecc_evp_params));
288 EXPECT_SUCCESS(s2n_extensions_server_key_share_send(server_conn, extension_stuffer));
289
290 S2N_STUFFER_READ_EXPECT_EQUAL(extension_stuffer, TLS_EXTENSION_KEY_SHARE, uint16);
291 /* 4 = S2N_SIZE_OF_EXTENSION_TYPE + S2N_SIZE_OF_EXTENSION_DATA_SIZE */
292 S2N_STUFFER_READ_EXPECT_EQUAL(extension_stuffer, s2n_extensions_server_key_share_send_size(server_conn) - 4, uint16);
293
294 client_conn->kex_params.client_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
295 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&client_conn->kex_params.client_ecc_evp_params));
296
297 /* Setup the client to receive a HelloRetryRequest */
298 POSIX_CHECKED_MEMCPY(client_conn->handshake_params.server_random, hello_retry_req_random, S2N_TLS_RANDOM_DATA_LEN);
299 EXPECT_SUCCESS(s2n_connection_set_all_protocol_versions(client_conn, S2N_TLS13));
300
301 /* Setup the handshake type and message number to simulate a condition where a HelloRetry should be sent */
302 EXPECT_SUCCESS(s2n_set_connection_hello_retry_flags(client_conn));
303 EXPECT_OK(s2n_conn_choose_state_machine(client_conn, S2N_TLS13));
304 EXPECT_SUCCESS(s2n_set_hello_retry_required(client_conn));
305
306 /* Parse the key share */
307 EXPECT_SUCCESS(s2n_extensions_server_key_share_recv(client_conn, extension_stuffer));
308 EXPECT_EQUAL(s2n_stuffer_data_available(extension_stuffer), 0);
309
310 /* Server negotiated curve value will be non-null, if the extension succeeded */
311 EXPECT_NOT_NULL(client_conn->kex_params.server_ecc_evp_params.negotiated_curve);
312
313 EXPECT_SUCCESS(s2n_connection_free(server_conn));
314 EXPECT_SUCCESS(s2n_connection_free(client_conn));
315 };
316
317 /* Verify that the hash transcript recreation function correctly takes the existing ClientHello1
318 * hash, and generates a synthetic message. */
319 {
320 struct s2n_config *conf = NULL;
321 struct s2n_connection *conn = NULL;
322
323 EXPECT_NOT_NULL(conf = s2n_config_new());
324 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
325 EXPECT_SUCCESS(s2n_connection_set_config(conn, conf));
326
327 conn->server_protocol_version = S2N_TLS13;
328 conn->secure->cipher_suite = &s2n_ecdhe_ecdsa_with_aes_128_gcm_sha256;
329 conn->kex_params.server_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
330 conn->kex_params.client_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
331 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&conn->kex_params.client_ecc_evp_params));
332
333 /* This blob is taken from the functional test RFC. That RFC does not actually provide hash transcript
334 * values, so the expected hashes are taken from what our hash functions generated and the hash
335 * generated from the transcript recreation.
336 * https://tools.ietf.org/html/rfc8448#section-5 */
337 S2N_BLOB_FROM_HEX(client_hello1,
338 "010000c00303cb34ecb1e78163"
339 "ba1c38c6dacb196a6dffa21a8d9912ec18a2ef6283"
340 "024dece7000006130113031302010000910000000b"
341 "0009000006736572766572ff01000100000a001400"
342 "12001d001700180019010001010102010301040023"
343 "0000003300260024001d002099381de560e4bd43d2"
344 "3d8e435a7dbafeb3c06e51c13cae4d5413691e529a"
345 "af2c002b0003020304000d0020001e040305030603"
346 "020308040805080604010501060102010402050206"
347 "020202002d00020101001c00024001");
348
349 S2N_BLOB_FROM_HEX(client_hello1_expected_hash,
350 "4db255f30da09a407c841720be831a06a5aa9b3662a5f44267d37706b73c2b8c");
351
352 S2N_BLOB_FROM_HEX(synthetic_message_with_ch1_expected_hash,
353 "ff1135ed878322e29699da3e451d2f08bf11fc693038769978e75bb63304a225");
354
355 EXPECT_SUCCESS(s2n_conn_update_handshake_hashes(conn, &client_hello1));
356
357 s2n_tls13_connection_keys(keys, conn);
358 uint8_t hash_digest_length = keys.size;
359 struct s2n_blob compare_blob = { 0 };
360
361 DEFER_CLEANUP(struct s2n_hash_state client_hello1_hash = { 0 }, s2n_hash_free);
362 EXPECT_SUCCESS(s2n_hash_new(&client_hello1_hash));
363 POSIX_GUARD_RESULT(s2n_handshake_copy_hash_state(conn, keys.hash_algorithm, &client_hello1_hash));
364
365 uint8_t client_hello1_digest_out[S2N_MAX_DIGEST_LEN] = { 0 };
366 EXPECT_SUCCESS(s2n_hash_digest(&client_hello1_hash, client_hello1_digest_out, hash_digest_length));
367
368 EXPECT_SUCCESS(s2n_blob_init(&compare_blob, client_hello1_digest_out, hash_digest_length));
369 S2N_BLOB_EXPECT_EQUAL(client_hello1_expected_hash, compare_blob);
370
371 EXPECT_SUCCESS(s2n_server_hello_retry_recreate_transcript(conn));
372
373 DEFER_CLEANUP(struct s2n_hash_state recreated_hash = { 0 }, s2n_hash_free);
374 uint8_t recreated_transcript_digest_out[S2N_MAX_DIGEST_LEN] = { 0 };
375 EXPECT_SUCCESS(s2n_hash_new(&recreated_hash));
376 POSIX_GUARD_RESULT(s2n_handshake_copy_hash_state(conn, keys.hash_algorithm, &recreated_hash));
377 EXPECT_SUCCESS(s2n_hash_digest(&recreated_hash, recreated_transcript_digest_out, hash_digest_length));
378
379 EXPECT_SUCCESS(s2n_blob_init(&compare_blob, recreated_transcript_digest_out, hash_digest_length));
380 S2N_BLOB_EXPECT_EQUAL(synthetic_message_with_ch1_expected_hash, compare_blob);
381
382 EXPECT_SUCCESS(s2n_config_free(conf));
383 EXPECT_SUCCESS(s2n_connection_free(conn));
384 };
385
386 /* Send and receive Hello Retry Request messages */
387 {
388 struct s2n_config *server_config = NULL;
389 struct s2n_config *client_config = NULL;
390
391 struct s2n_connection *server_conn = NULL;
392 struct s2n_connection *client_conn = NULL;
393
394 struct s2n_cert_chain_and_key *tls13_chain_and_key = NULL;
395
396 EXPECT_NOT_NULL(server_config = s2n_config_new());
397 EXPECT_NOT_NULL(client_config = s2n_config_new());
398
399 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
400 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
401
402 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "default_tls13"));
403 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config, "default_tls13"));
404
405 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&tls13_chain_and_key,
406 S2N_ECDSA_P384_PKCS1_CERT_CHAIN, S2N_ECDSA_P384_PKCS1_KEY));
407 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(client_config, tls13_chain_and_key));
408 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, tls13_chain_and_key));
409
410 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
411 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
412 struct client_hello_context client_hello_ctx = { .invocations = 0, .mode = S2N_CLIENT_HELLO_CB_BLOCKING };
413 EXPECT_SUCCESS(s2n_config_set_client_hello_cb(server_config, client_hello_detect_duplicate_calls, &client_hello_ctx));
414
415 EXPECT_SUCCESS(s2n_connection_set_all_protocol_versions(server_conn, S2N_TLS13));
416 EXPECT_SUCCESS(s2n_connection_set_all_protocol_versions(client_conn, S2N_TLS13));
417
418 /* Force HRR path */
419 client_conn->security_policy_override = &security_policy_test_tls13_retry;
420
421 /* Send the first CH message */
422 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
423 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
424 s2n_stuffer_data_available(&client_conn->handshake.io)));
425
426 /* Receive the CH and send an HRR, which will execute the HRR code paths */
427 EXPECT_EQUAL(client_hello_ctx.invocations, 0);
428 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
429 EXPECT_EQUAL(client_hello_ctx.invocations, 1);
430
431 EXPECT_TRUE(s2n_is_hello_retry_handshake(server_conn));
432 EXPECT_SUCCESS(s2n_set_connection_hello_retry_flags(server_conn));
433 EXPECT_TRUE(s2n_is_hello_retry_message(server_conn));
434
435 EXPECT_SUCCESS(s2n_server_hello_retry_send(server_conn));
436
437 EXPECT_SUCCESS(s2n_stuffer_wipe(&client_conn->handshake.io));
438 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io,
439 s2n_stuffer_data_available(&server_conn->handshake.io)));
440 client_conn->handshake.message_number = HELLO_RETRY_MSG_NO;
441 EXPECT_SUCCESS(s2n_server_hello_recv(client_conn));
442
443 /* Send the second CH message */
444 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
445 EXPECT_SUCCESS(s2n_stuffer_wipe(&server_conn->handshake.io));
446 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
447 s2n_stuffer_data_available(&client_conn->handshake.io)));
448
449 /* Verify that receiving the second CH message does not execute the callback */
450 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
451 EXPECT_EQUAL(client_hello_ctx.invocations, 1);
452
453 EXPECT_SUCCESS(s2n_connection_free(server_conn));
454 EXPECT_SUCCESS(s2n_connection_free(client_conn));
455 EXPECT_SUCCESS(s2n_config_free(client_config));
456 EXPECT_SUCCESS(s2n_config_free(server_config));
457 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(tls13_chain_and_key));
458 };
459
460 /* Send and receive Hello Retry Request messages, test for non blocking client hello callback */
461 {
462 struct s2n_config *server_config = NULL;
463 struct s2n_config *client_config = NULL;
464
465 struct s2n_connection *server_conn = NULL;
466 struct s2n_connection *client_conn = NULL;
467
468 struct s2n_cert_chain_and_key *tls13_chain_and_key = NULL;
469
470 EXPECT_NOT_NULL(server_config = s2n_config_new());
471 EXPECT_NOT_NULL(client_config = s2n_config_new());
472
473 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
474 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
475
476 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "default_tls13"));
477 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config, "default_tls13"));
478
479 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(server_config));
480 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(client_config));
481
482 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&tls13_chain_and_key,
483 S2N_ECDSA_P384_PKCS1_CERT_CHAIN, S2N_ECDSA_P384_PKCS1_KEY));
484 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(client_config, tls13_chain_and_key));
485 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, tls13_chain_and_key));
486
487 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
488 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
489
490 struct s2n_test_io_pair io_pair;
491 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
492 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
493
494 /* Force HRR path */
495 client_conn->security_policy_override = &security_policy_test_tls13_retry;
496
497 /* setup the client hello callback */
498 struct client_hello_context client_hello_ctx = { .invocations = 0,
499 .mode = S2N_CLIENT_HELLO_CB_NONBLOCKING };
500 EXPECT_SUCCESS(s2n_config_set_client_hello_cb(server_config,
501 client_hello_detect_duplicate_calls, &client_hello_ctx));
502 EXPECT_SUCCESS(s2n_config_set_client_hello_cb_mode(server_config,
503 S2N_CLIENT_HELLO_CB_NONBLOCKING));
504
505 /* ensure that handshake succeeds via HRR path using non_blocking CH */
506 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
507 EXPECT_TRUE(server_conn->handshake.handshake_type & HELLO_RETRY_REQUEST);
508 EXPECT_EQUAL(client_hello_ctx.invocations, 1);
509
510 EXPECT_NOT_NULL(s2n_connection_get_client_hello(server_conn));
511 EXPECT_TRUE(IS_HELLO_RETRY_HANDSHAKE(client_conn));
512 EXPECT_TRUE(IS_HELLO_RETRY_HANDSHAKE(server_conn));
513
514 EXPECT_SUCCESS(s2n_connection_free(server_conn));
515 EXPECT_SUCCESS(s2n_connection_free(client_conn));
516 EXPECT_SUCCESS(s2n_config_free(client_config));
517 EXPECT_SUCCESS(s2n_config_free(server_config));
518 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(tls13_chain_and_key));
519 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
520 };
521
522 /* Hello Retry Request + (poll and no-poll) client hello callback */
523 {
524 EXPECT_OK(hello_retry_client_hello_cb_test());
525 };
526
527 /* Test s2n_set_hello_retry_required correctly sets the handshake type to HELLO_RETRY_REQUEST,
528 * when conn->actual_protocol_version is set to TLS1.3 version */
529 {
530 struct s2n_connection *conn = NULL;
531 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
532 EXPECT_SUCCESS(s2n_connection_set_all_protocol_versions(conn, S2N_TLS13));
533
534 EXPECT_SUCCESS(s2n_set_hello_retry_required(conn));
535 EXPECT_TRUE(conn->handshake.handshake_type & HELLO_RETRY_REQUEST);
536
537 EXPECT_SUCCESS(s2n_connection_free(conn));
538 };
539
540 /* Test s2n_set_hello_retry_required raises a S2N_ERR_INVALID_HELLO_RETRY error
541 * when conn->actual_protocol_version is less than TLS1.3 */
542 {
543 struct s2n_connection *conn = NULL;
544 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
545 conn->actual_protocol_version = S2N_TLS12;
546
547 EXPECT_FAILURE_WITH_ERRNO(s2n_set_hello_retry_required(conn), S2N_ERR_INVALID_HELLO_RETRY);
548 EXPECT_FALSE(conn->handshake.handshake_type & HELLO_RETRY_REQUEST);
549
550 EXPECT_SUCCESS(s2n_connection_free(conn));
551 };
552
553 /*
554 *= https://www.rfc-editor.org/rfc/rfc8446#section-4.1.4
555 *= type=test
556 *# Clients MUST abort the handshake with an
557 *# "illegal_parameter" alert if the HelloRetryRequest would not result
558 *# in any change in the ClientHello.
559 */
560 {
561 const struct s2n_security_policy *security_policy = NULL;
562 EXPECT_SUCCESS(s2n_find_security_policy_from_version("20201021", &security_policy));
563 EXPECT_NOT_NULL(security_policy);
564 const struct s2n_ecc_named_curve *test_curve = security_policy->ecc_preferences->ecc_curves[0];
565
566 /**
567 * Retry for key share is valid
568 *
569 *= https://www.rfc-editor.org/rfc/rfc8446#4.2.8
570 *= type=test
571 *# and (2) the selected_group field does not
572 *# correspond to a group which was provided in the "key_share" extension
573 *# in the original ClientHello.
574 **/
575 {
576 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
577 EXPECT_NOT_NULL(conn);
578 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "20201021"));
579 conn->actual_protocol_version = S2N_TLS13;
580 conn->secure->cipher_suite = &s2n_tls13_aes_256_gcm_sha384;
581
582 conn->kex_params.server_ecc_evp_params.negotiated_curve = test_curve;
583
584 /* Server requested key share is NOT present: allow retry */
585 EXPECT_SUCCESS(s2n_server_hello_retry_recv(conn));
586
587 /* Server requested key share is present: do NOT allow retry */
588 conn->kex_params.client_ecc_evp_params.negotiated_curve = test_curve;
589 conn->kex_params.client_ecc_evp_params.evp_pkey = EVP_PKEY_new();
590 EXPECT_NOT_NULL(conn->kex_params.client_ecc_evp_params.evp_pkey);
591 EXPECT_FAILURE_WITH_ERRNO(s2n_server_hello_retry_recv(conn),
592 S2N_ERR_INVALID_HELLO_RETRY);
593
594 EXPECT_SUCCESS(s2n_connection_free(conn));
595 };
596
597 /* Retry for multiple reasons is valid */
598 {
599 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
600 EXPECT_NOT_NULL(conn);
601 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "20201021"));
602 conn->actual_protocol_version = S2N_TLS13;
603 conn->secure->cipher_suite = &s2n_tls13_aes_256_gcm_sha384;
604
605 conn->kex_params.server_ecc_evp_params.negotiated_curve = test_curve;
606
607 /* All retry conditions met: allow retry */
608 conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
609 EXPECT_SUCCESS(s2n_server_hello_retry_recv(conn));
610
611 /* No retry conditions met: do NOT allow retry */
612 conn->early_data_state = S2N_EARLY_DATA_NOT_REQUESTED;
613 conn->kex_params.client_ecc_evp_params.negotiated_curve = test_curve;
614 conn->kex_params.client_ecc_evp_params.evp_pkey = EVP_PKEY_new();
615 EXPECT_NOT_NULL(conn->kex_params.client_ecc_evp_params.evp_pkey);
616 EXPECT_FAILURE_WITH_ERRNO(s2n_server_hello_retry_recv(conn),
617 S2N_ERR_INVALID_HELLO_RETRY);
618
619 EXPECT_SUCCESS(s2n_connection_free(conn));
620 };
621 };
622
623 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
624
625 END_TEST();
626 }