Debian Codegraph @

crt/s2n/tests/unit/s2n_server_hello_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 "api/s2n.h"
17 #include "s2n_test.h"
18 #include "testlib/s2n_testlib.h"
19 #include "tls/s2n_cipher_suites.h"
20 #include "tls/s2n_quic_support.h"
21 #include "tls/s2n_security_policies.h"
22 #include "tls/s2n_tls.h"
23 #include "tls/s2n_tls13.h"
24 #include "utils/s2n_safety.h"
25
26 const uint8_t SESSION_ID_SIZE = 1;
27 const uint8_t COMPRESSION_METHOD_SIZE = 1;
28
29 /* from RFC: https://tools.ietf.org/html/rfc8446#section-4.1.3*/
30 const char hello_retry_random_hex[] =
31 "CF21AD74E59A6111BE1D8C021E65B891"
32 "C2A211167ABB8C5E079E09E2C8A8339C";
33
34 const uint8_t tls12_downgrade_protection_check_bytes[] = {
35 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44, 0x01
36 };
37
38 const uint8_t tls11_downgrade_protection_check_bytes[] = {
39 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44, 0x00
40 };
41
42 static S2N_RESULT s2n_test_client_hello(struct s2n_connection *client_conn, struct s2n_connection *server_conn)
43 {
44 /* We have to "write" the handshake header for the PSK binder calculation, which expects a complete
45 * ClientHello message. We'll skip these bytes later.
46 */
47 RESULT_GUARD_POSIX(s2n_stuffer_skip_write(&client_conn->handshake.io, TLS_HANDSHAKE_HEADER_LENGTH));
48
49 RESULT_GUARD_POSIX(s2n_client_hello_send(client_conn));
50 RESULT_GUARD_POSIX(s2n_stuffer_copy(&client_conn->handshake.io,
51 &server_conn->handshake.io, s2n_stuffer_data_available(&client_conn->handshake.io)));
52
53 /* Skip the handshake header bytes */
54 RESULT_GUARD_POSIX(s2n_stuffer_skip_read(&server_conn->handshake.io, TLS_HANDSHAKE_HEADER_LENGTH));
55
56 RESULT_GUARD_POSIX(s2n_client_hello_recv(server_conn));
57
58 RESULT_GUARD_POSIX(s2n_stuffer_wipe(&client_conn->handshake.io));
59 RESULT_GUARD_POSIX(s2n_stuffer_wipe(&server_conn->handshake.io));
60
61 return S2N_RESULT_OK;
62 }
63
64 int main(int argc, char **argv)
65 {
66 BEGIN_TEST();
67 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
68
69 struct s2n_cert_chain_and_key *chain_and_key = NULL;
70 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
71 S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY));
72
73 /* Test basic Server Hello Send */
74 {
75 struct s2n_config *config = NULL;
76 EXPECT_NOT_NULL(config = s2n_config_new());
77 EXPECT_OK(s2n_config_set_tls12_security_policy(config));
78
79 struct s2n_connection *conn = NULL;
80 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
81 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
82
83 struct s2n_stuffer *hello_stuffer = &conn->handshake.io;
84
85 /* Test s2n_server_hello_send */
86 const uint32_t total = S2N_TLS_PROTOCOL_VERSION_LEN
87 + S2N_TLS_RANDOM_DATA_LEN
88 + SESSION_ID_SIZE
89 + conn->session_id_len
90 + S2N_TLS_CIPHER_SUITE_LEN
91 + COMPRESSION_METHOD_SIZE;
92
93 conn->actual_protocol_version = S2N_TLS12;
94 EXPECT_SUCCESS(s2n_server_hello_send(conn));
95 EXPECT_EQUAL(hello_stuffer->blob.data[0], 0x03);
96 EXPECT_EQUAL(hello_stuffer->blob.data[1], 0x03);
97 S2N_STUFFER_LENGTH_WRITTEN_EXPECT_EQUAL(hello_stuffer, total);
98
99 EXPECT_SUCCESS(s2n_config_free(config));
100 EXPECT_SUCCESS(s2n_connection_free(conn));
101 };
102
103 /* Test that legacy_version_field is set correct for TLS 1.3 Server Hello Send */
104 {
105 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
106 struct s2n_config *config = NULL;
107 const struct s2n_ecc_preferences *ecc_preferences = NULL;
108 EXPECT_NOT_NULL(config = s2n_config_new());
109
110 struct s2n_connection *conn = NULL;
111 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
112 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
113 EXPECT_NOT_NULL(conn->config);
114 EXPECT_SUCCESS(s2n_connection_get_ecc_preferences(conn, &ecc_preferences));
115 EXPECT_NOT_NULL(ecc_preferences);
116 /* configure these parameters so server hello can be sent */
117 conn->actual_protocol_version = S2N_TLS13;
118 conn->kex_params.server_ecc_evp_params.negotiated_curve = ecc_preferences->ecc_curves[0];
119 conn->kex_params.client_ecc_evp_params.negotiated_curve = ecc_preferences->ecc_curves[0];
120 EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&conn->kex_params.client_ecc_evp_params));
121
122 struct s2n_stuffer *hello_stuffer = &conn->handshake.io;
123 EXPECT_SUCCESS(s2n_server_hello_send(conn));
124
125 /* verify that legacy protocol version is 0x0303 (TLS12) */
126 EXPECT_EQUAL(hello_stuffer->blob.data[0], 0x03);
127 EXPECT_EQUAL(hello_stuffer->blob.data[1], 0x03);
128
129 EXPECT_SUCCESS(s2n_config_free(config));
130 EXPECT_SUCCESS(s2n_connection_free(conn));
131
132 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
133 };
134
135 /* Test basic Server Hello Recv */
136 {
137 struct s2n_config *server_config = NULL;
138 struct s2n_config *client_config = NULL;
139
140 struct s2n_connection *server_conn = NULL;
141 struct s2n_connection *client_conn = NULL;
142
143 EXPECT_NOT_NULL(server_config = s2n_config_new());
144 EXPECT_OK(s2n_config_set_tls12_security_policy(server_config));
145 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
146 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
147
148 EXPECT_NOT_NULL(client_config = s2n_config_new());
149 EXPECT_OK(s2n_config_set_tls12_security_policy(client_config));
150 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
151 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
152
153 server_conn->actual_protocol_version = S2N_TLS12;
154 server_conn->secure->cipher_suite = &s2n_ecdhe_rsa_with_aes_256_gcm_sha384;
155
156 EXPECT_SUCCESS(s2n_server_hello_send(server_conn));
157
158 /* Copy server stuffer to client stuffer */
159 const uint32_t total = s2n_stuffer_data_available(&server_conn->handshake.io);
160 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io, total));
161
162 /* Test s2n_server_hello_recv() */
163 struct s2n_stuffer *client_stuffer = &client_conn->handshake.io;
164 EXPECT_SUCCESS(s2n_server_hello_recv(client_conn));
165
166 EXPECT_EQUAL(s2n_stuffer_data_available(client_stuffer), 0);
167
168 EXPECT_SUCCESS(s2n_config_free(client_config));
169 EXPECT_SUCCESS(s2n_config_free(server_config));
170 EXPECT_SUCCESS(s2n_connection_free(client_conn));
171 EXPECT_SUCCESS(s2n_connection_free(server_conn));
172 };
173
174 /* Test Server Hello Recv with invalid cipher */
175 {
176 struct s2n_connection *server_conn = NULL;
177 struct s2n_connection *client_conn = NULL;
178
179 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
180 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
181 EXPECT_OK(s2n_connection_set_tls12_security_policy(server_conn));
182 EXPECT_OK(s2n_connection_set_tls12_security_policy(client_conn));
183
184 server_conn->actual_protocol_version = S2N_TLS12;
185
186 /* This cipher is not in the client's default selection */
187 server_conn->secure->cipher_suite = &s2n_tls13_chacha20_poly1305_sha256;
188
189 EXPECT_SUCCESS(s2n_server_hello_send(server_conn));
190
191 /* Copy server stuffer to client stuffer */
192 const uint32_t total = s2n_stuffer_data_available(&server_conn->handshake.io);
193 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io, total));
194
195 /* The client should fail the handshake because an invalid cipher was offered */
196 EXPECT_FAILURE_WITH_ERRNO(s2n_server_hello_recv(client_conn), S2N_ERR_CIPHER_NOT_SUPPORTED);
197
198 EXPECT_SUCCESS(s2n_connection_free(client_conn));
199 EXPECT_SUCCESS(s2n_connection_free(server_conn));
200 };
201
202 /* Non-matching session IDs turn off EMS for the connection */
203 {
204 struct s2n_connection *server_conn = NULL;
205 struct s2n_connection *client_conn = NULL;
206
207 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
208 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
209 EXPECT_OK(s2n_connection_set_tls12_security_policy(server_conn));
210 EXPECT_OK(s2n_connection_set_tls12_security_policy(client_conn));
211
212 server_conn->actual_protocol_version = S2N_TLS12;
213 server_conn->secure->cipher_suite = &s2n_ecdhe_rsa_with_aes_256_gcm_sha384;
214
215 /* Create session ID for server */
216 for (int i = 0; i < 32; i++) {
217 server_conn->session_id[i] = i;
218 }
219
220 EXPECT_SUCCESS(s2n_server_hello_send(server_conn));
221
222 /* Copy server stuffer to client stuffer */
223 const uint32_t total = s2n_stuffer_data_available(&server_conn->handshake.io);
224 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io, total));
225
226 /* Create client session ID does not match server session ID */
227 for (int i = 0; i < 32; i++) {
228 client_conn->session_id[i] = 0;
229 }
230
231 /* Client is negotiating an EMS connection but is able to fallback to non-EMS connection
232 * if session IDs don't match */
233 client_conn->ems_negotiated = true;
234 EXPECT_SUCCESS(s2n_server_hello_recv(client_conn));
235 EXPECT_FALSE(client_conn->ems_negotiated);
236
237 EXPECT_SUCCESS(s2n_connection_free(client_conn));
238 EXPECT_SUCCESS(s2n_connection_free(server_conn));
239 };
240
241 /* Test TLS 1.3 session id matching */
242 {
243 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
244 struct s2n_config *client_config = NULL;
245 struct s2n_connection *client_conn = NULL;
246 EXPECT_NOT_NULL(client_config = s2n_config_new());
247 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
248 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
249
250 struct s2n_stuffer *io = &client_conn->handshake.io;
251 /* protocol version */
252 EXPECT_SUCCESS(s2n_stuffer_write_uint8(io, S2N_TLS12 / 10));
253 EXPECT_SUCCESS(s2n_stuffer_write_uint8(io, S2N_TLS12 % 10));
254
255 /* random payload */
256 uint8_t random[S2N_TLS_RANDOM_DATA_LEN] = { 0 };
257 EXPECT_SUCCESS(s2n_stuffer_write_bytes(io, random, S2N_TLS_RANDOM_DATA_LEN));
258
259 uint8_t session_id[S2N_TLS_SESSION_ID_MAX_LEN] = { 0 };
260
261 /* generate matching session id for payload and client connection */
262 for (int i = 0; i < 32; i++) {
263 session_id[i] = i;
264 client_conn->session_id[i] = i;
265 }
266
267 /* session id */
268 EXPECT_SUCCESS(s2n_stuffer_write_uint8(io, S2N_TLS_SESSION_ID_MAX_LEN));
269 EXPECT_SUCCESS(s2n_stuffer_write_bytes(io, session_id, S2N_TLS_SESSION_ID_MAX_LEN));
270 EXPECT_SUCCESS(s2n_stuffer_write_uint16(io, (0x13 << 8) + 0x01)); /* cipher suites */
271 EXPECT_SUCCESS(s2n_stuffer_write_uint8(io, 0)); /* no compression */
272
273 client_conn->server_protocol_version = S2N_TLS13;
274 client_conn->session_id_len = 32;
275
276 /* Test s2n_server_hello_recv() */
277 EXPECT_SUCCESS(s2n_server_hello_recv(client_conn));
278 EXPECT_EQUAL(s2n_stuffer_data_available(io), 0);
279
280 /* Check that corrupt session id fails server hello */
281 for (int i = 0; i < 32; i++) {
282 client_conn->session_id[i] ^= 1;
283 EXPECT_SUCCESS(s2n_stuffer_reread(io));
284 EXPECT_FAILURE(s2n_server_hello_recv(client_conn));
285 client_conn->session_id[i] ^= 1;
286 }
287
288 /* Check that server hello is successful again */
289 EXPECT_SUCCESS(s2n_stuffer_reread(io));
290 EXPECT_SUCCESS(s2n_server_hello_recv(client_conn));
291
292 /* Check that unmatched session length should also fail */
293 for (int i = 0; i < 32; i++) {
294 client_conn->session_id_len = i;
295 EXPECT_SUCCESS(s2n_stuffer_reread(io));
296 EXPECT_FAILURE(s2n_server_hello_recv(client_conn));
297 }
298
299 EXPECT_SUCCESS(s2n_config_free(client_config));
300 EXPECT_SUCCESS(s2n_connection_free(client_conn));
301 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
302 };
303
304 /* Test TLS 1.3 => 1.1 protocol downgrade detection with a TLS1.3 client */
305 {
306 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
307 struct s2n_config *client_config = NULL;
308 struct s2n_connection *client_conn = NULL;
309 struct s2n_config *server_config = NULL;
310 struct s2n_connection *server_conn = NULL;
311
312 EXPECT_NOT_NULL(server_config = s2n_config_new());
313 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
314 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
315
316 EXPECT_NOT_NULL(client_config = s2n_config_new());
317 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
318 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
319
320 /* The client will request TLS1.3 */
321 client_conn->client_protocol_version = S2N_TLS13;
322
323 /* Set the negotiated curve, otherwise the server might try to respond with a retry */
324 server_conn->kex_params.server_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
325
326 /* The server will respond with TLS1.1 even though it supports TLS1.3 */
327 server_conn->actual_protocol_version = S2N_TLS11;
328 EXPECT_SUCCESS(s2n_server_hello_send(server_conn));
329
330 /* Copy server stuffer to client stuffer */
331 const uint32_t total = s2n_stuffer_data_available(&server_conn->handshake.io);
332 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io, total));
333
334 /* Verify that the downgrade is detected */
335 struct s2n_stuffer *client_stuffer = &client_conn->handshake.io;
336 EXPECT_BYTEARRAY_EQUAL(&client_stuffer->blob.data[S2N_TLS_PROTOCOL_VERSION_LEN + 24], tls11_downgrade_protection_check_bytes, 8);
337 EXPECT_FAILURE_WITH_ERRNO(s2n_server_hello_recv(client_conn), S2N_ERR_PROTOCOL_DOWNGRADE_DETECTED);
338
339 EXPECT_EQUAL(s2n_stuffer_data_available(client_stuffer), 0);
340
341 EXPECT_SUCCESS(s2n_config_free(client_config));
342 EXPECT_SUCCESS(s2n_config_free(server_config));
343 EXPECT_SUCCESS(s2n_connection_free(client_conn));
344 EXPECT_SUCCESS(s2n_connection_free(server_conn));
345 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
346 };
347
348 /* Test TLS 1.3 => 1.2 protocol downgrade detection with a TLS1.3 client */
349 {
350 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
351 struct s2n_config *client_config = NULL;
352 struct s2n_connection *client_conn = NULL;
353 struct s2n_config *server_config = NULL;
354 struct s2n_connection *server_conn = NULL;
355
356 EXPECT_NOT_NULL(server_config = s2n_config_new());
357 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
358 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
359
360 EXPECT_NOT_NULL(client_config = s2n_config_new());
361 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
362 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
363
364 /* The client will request TLS1.3 */
365 client_conn->client_protocol_version = S2N_TLS13;
366
367 /* Set the negotiated curve, otherwise the server might try to respond with a retry */
368 server_conn->kex_params.server_ecc_evp_params.negotiated_curve = s2n_all_supported_curves_list[0];
369
370 /* The server will respond with TLS1.2 even though it supports TLS1.3 */
371 server_conn->actual_protocol_version = S2N_TLS12;
372 EXPECT_SUCCESS(s2n_server_hello_send(server_conn));
373
374 /* Copy server stuffer to client stuffer */
375 const uint32_t total = s2n_stuffer_data_available(&server_conn->handshake.io);
376 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io, total));
377
378 /* Verify that the downgrade is detected */
379 struct s2n_stuffer *client_stuffer = &client_conn->handshake.io;
380 EXPECT_BYTEARRAY_EQUAL(&client_stuffer->blob.data[S2N_TLS_PROTOCOL_VERSION_LEN + 24], tls12_downgrade_protection_check_bytes, 8);
381 EXPECT_FAILURE_WITH_ERRNO(s2n_server_hello_recv(client_conn), S2N_ERR_PROTOCOL_DOWNGRADE_DETECTED);
382
383 EXPECT_EQUAL(s2n_stuffer_data_available(client_stuffer), 0);
384
385 EXPECT_SUCCESS(s2n_config_free(client_config));
386 EXPECT_SUCCESS(s2n_config_free(server_config));
387 EXPECT_SUCCESS(s2n_connection_free(client_conn));
388 EXPECT_SUCCESS(s2n_connection_free(server_conn));
389 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
390 };
391
392 /* Verify a TLS1.2 client can negotiate with a TLS1.3 server */
393 {
394 struct s2n_config *client_config = NULL;
395 struct s2n_connection *client_conn = NULL;
396 struct s2n_config *server_config = NULL;
397 struct s2n_connection *server_conn = NULL;
398
399 EXPECT_NOT_NULL(server_config = s2n_config_new());
400 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
401 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
402
403 EXPECT_NOT_NULL(client_config = s2n_config_new());
404 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
405 EXPECT_OK(s2n_config_set_tls12_security_policy(client_config));
406 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
407
408 /* The client will request TLS1.2 */
409 client_conn->client_protocol_version = S2N_TLS12;
410
411 /* The server will respond with TLS1.2 even though it support TLS1.3. This is expected because */
412 /* the client only support TLS1.2 */
413 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
414 server_conn->actual_protocol_version = S2N_TLS12;
415 server_conn->secure->cipher_suite = &s2n_ecdhe_rsa_with_aes_256_gcm_sha384;
416 EXPECT_SUCCESS(s2n_server_hello_send(server_conn));
417 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
418
419 /* Copy server stuffer to client stuffer */
420 const uint32_t total = s2n_stuffer_data_available(&server_conn->handshake.io);
421 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io, total));
422
423 /* Verify that a TLS12 client does not error due to the downgrade */
424 struct s2n_stuffer *client_stuffer = &client_conn->handshake.io;
425 EXPECT_SUCCESS(s2n_server_hello_recv(client_conn));
426
427 EXPECT_EQUAL(s2n_stuffer_data_available(client_stuffer), 0);
428
429 EXPECT_SUCCESS(s2n_config_free(client_config));
430 EXPECT_SUCCESS(s2n_config_free(server_config));
431 EXPECT_SUCCESS(s2n_connection_free(client_conn));
432 EXPECT_SUCCESS(s2n_connection_free(server_conn));
433 };
434
435 /* Verify a TLS1.3 client can negotiate with a TLS1.2 server */
436 {
437 struct s2n_config *client_config = NULL;
438 struct s2n_connection *client_conn = NULL;
439 struct s2n_config *server_config = NULL;
440 struct s2n_connection *server_conn = NULL;
441
442 EXPECT_NOT_NULL(server_config = s2n_config_new());
443 EXPECT_OK(s2n_config_set_tls12_security_policy(server_config));
444 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
445 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
446
447 EXPECT_NOT_NULL(client_config = s2n_config_new());
448 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
449 EXPECT_OK(s2n_config_set_tls12_security_policy(client_config));
450 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
451
452 /* The client will request TLS1.3 */
453 client_conn->client_protocol_version = S2N_TLS13;
454
455 /* The server will respond with TLS1.2 */
456 server_conn->server_protocol_version = S2N_TLS12;
457 server_conn->actual_protocol_version = S2N_TLS12;
458
459 server_conn->secure->cipher_suite = &s2n_ecdhe_rsa_with_aes_256_gcm_sha384;
460 EXPECT_SUCCESS(s2n_server_hello_send(server_conn));
461
462 /* Copy server stuffer to client stuffer */
463 const uint32_t total = s2n_stuffer_data_available(&server_conn->handshake.io);
464 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io, total));
465
466 /* Verify that a TLS13 client does not error due to the downgrade */
467 struct s2n_stuffer *client_stuffer = &client_conn->handshake.io;
468 POSIX_GUARD(s2n_enable_tls13_in_test());
469 EXPECT_SUCCESS(s2n_server_hello_recv(client_conn));
470 POSIX_GUARD(s2n_disable_tls13_in_test());
471 EXPECT_EQUAL(s2n_stuffer_data_available(client_stuffer), 0);
472
473 EXPECT_SUCCESS(s2n_config_free(client_config));
474 EXPECT_SUCCESS(s2n_config_free(server_config));
475 EXPECT_SUCCESS(s2n_connection_free(client_conn));
476 EXPECT_SUCCESS(s2n_connection_free(server_conn));
477 };
478
479 /* Verify a TLS1.2 client can negotiate with a TLS1.3 server */
480 {
481 struct s2n_config *client_config = NULL;
482 struct s2n_connection *client_conn = NULL;
483 struct s2n_config *server_config = NULL;
484 struct s2n_connection *server_conn = NULL;
485
486 EXPECT_NOT_NULL(server_config = s2n_config_new());
487 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
488 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
489
490 EXPECT_NOT_NULL(client_config = s2n_config_new());
491 EXPECT_OK(s2n_config_set_tls12_security_policy(client_config));
492 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
493 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
494
495 /* The client will request TLS1.2 */
496 client_conn->client_protocol_version = S2N_TLS12;
497
498 /* The server will respond with TLS1.2 even though it support TLS1.3. This is expected because */
499 /* the client only support TLS1.2 */
500 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
501 server_conn->actual_protocol_version = S2N_TLS12;
502
503 server_conn->secure->cipher_suite = &s2n_ecdhe_rsa_with_aes_256_gcm_sha384;
504 EXPECT_SUCCESS(s2n_server_hello_send(server_conn));
505 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
506
507 /* Copy server stuffer to client stuffer */
508 const uint32_t total = s2n_stuffer_data_available(&server_conn->handshake.io);
509 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io, total));
510
511 /* Verify that a TLS12 client does not error due to the downgrade */
512 struct s2n_stuffer *client_stuffer = &client_conn->handshake.io;
513 EXPECT_SUCCESS(s2n_server_hello_recv(client_conn));
514
515 EXPECT_EQUAL(s2n_stuffer_data_available(client_stuffer), 0);
516
517 EXPECT_SUCCESS(s2n_config_free(client_config));
518 EXPECT_SUCCESS(s2n_config_free(server_config));
519 EXPECT_SUCCESS(s2n_connection_free(client_conn));
520 EXPECT_SUCCESS(s2n_connection_free(server_conn));
521 };
522
523 /* TLS13 hello retry message received results into S2N_ERR_UNIMPLEMENTED error*/
524 {
525 struct s2n_connection *client_conn = NULL;
526 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
527 struct s2n_stuffer *io = &client_conn->handshake.io;
528 client_conn->server_protocol_version = S2N_TLS13;
529
530 /* protocol version */
531 EXPECT_SUCCESS(s2n_stuffer_write_uint8(io, S2N_TLS12 / 10));
532 EXPECT_SUCCESS(s2n_stuffer_write_uint8(io, S2N_TLS12 % 10));
533
534 uint8_t session_id[S2N_TLS_SESSION_ID_MAX_LEN] = { 0 };
535 S2N_BLOB_FROM_HEX(random_blob, hello_retry_random_hex);
536
537 /* random payload */
538 EXPECT_SUCCESS(s2n_stuffer_write_bytes(io, random_blob.data, S2N_TLS_RANDOM_DATA_LEN));
539
540 /* session id */
541 EXPECT_SUCCESS(s2n_stuffer_write_uint8(io, S2N_TLS_SESSION_ID_MAX_LEN));
542 EXPECT_SUCCESS(s2n_stuffer_write_bytes(io, session_id, S2N_TLS_SESSION_ID_MAX_LEN));
543
544 /* cipher suites */
545 EXPECT_SUCCESS(s2n_stuffer_write_uint16(io, (0x13 << 8) + 0x01));
546
547 /* no compression */
548 EXPECT_SUCCESS(s2n_stuffer_write_uint8(io, 0));
549 EXPECT_EQUAL(S2N_TLS_RANDOM_DATA_LEN, random_blob.size);
550
551 EXPECT_SUCCESS(s2n_connection_free(client_conn));
552 };
553
554 /* Test that negotiating TLS1.2 with QUIC-enabled client fails */
555 if (s2n_is_tls13_fully_supported()) {
556 EXPECT_SUCCESS(s2n_reset_tls13_in_test());
557
558 struct s2n_config *quic_config = s2n_config_new();
559 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(quic_config, "test_all"));
560 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(quic_config, chain_and_key));
561 EXPECT_SUCCESS(s2n_config_enable_quic(quic_config));
562
563 struct s2n_config *non_quic_config = s2n_config_new();
564 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(non_quic_config, "test_all"));
565 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(non_quic_config, chain_and_key));
566
567 /* Succeeds when negotiating TLS1.3 */
568 if (s2n_is_tls13_fully_supported()) {
569 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
570 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, non_quic_config));
571
572 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
573 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, non_quic_config));
574
575 EXPECT_OK(s2n_test_client_hello(client_conn, server_conn));
576 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, quic_config));
577
578 EXPECT_SUCCESS(s2n_server_hello_send(server_conn));
579 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io,
580 &client_conn->handshake.io, s2n_stuffer_data_available(&server_conn->handshake.io)));
581 EXPECT_SUCCESS(s2n_server_hello_recv(client_conn));
582
583 EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS13);
584 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS13);
585
586 EXPECT_SUCCESS(s2n_connection_free(client_conn));
587 EXPECT_SUCCESS(s2n_connection_free(server_conn));
588 }
589
590 /* Fails when negotiating TLS1.2 */
591 {
592 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
593 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, non_quic_config));
594 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "test_all_tls12"));
595
596 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
597 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, non_quic_config));
598
599 EXPECT_OK(s2n_test_client_hello(client_conn, server_conn));
600 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, quic_config));
601
602 EXPECT_SUCCESS(s2n_server_hello_send(server_conn));
603 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io,
604 &client_conn->handshake.io, s2n_stuffer_data_available(&server_conn->handshake.io)));
605 EXPECT_FAILURE_WITH_ERRNO(s2n_server_hello_recv(client_conn), S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED);
606
607 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS12);
608
609 EXPECT_SUCCESS(s2n_connection_free(client_conn));
610 EXPECT_SUCCESS(s2n_connection_free(server_conn));
611 };
612
613 EXPECT_SUCCESS(s2n_config_free(quic_config));
614 EXPECT_SUCCESS(s2n_config_free(non_quic_config));
615 }
616
617 /* Test that negotiating TLS1.2 with an early data enabled client fails.
618 *
619 *= https://www.rfc-editor.org/rfc/rfc8446#appendix-D.3
620 *= type=test
621 *# A client that attempts to send 0-RTT data MUST fail a connection if
622 *# it receives a ServerHello with TLS 1.2 or older.
623 */
624 if (s2n_is_tls13_fully_supported()) {
625 EXPECT_SUCCESS(s2n_reset_tls13_in_test());
626
627 struct s2n_config *config = s2n_config_new();
628 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
629
630 /* Succeeds when negotiating TLS1.3 */
631 {
632 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
633 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
634 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, "test_all"));
635 EXPECT_OK(s2n_append_test_psk_with_early_data(client_conn, 1, &s2n_tls13_aes_128_gcm_sha256));
636 EXPECT_SUCCESS(s2n_connection_set_early_data_expected(client_conn));
637
638 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
639 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
640 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "test_all"));
641 EXPECT_OK(s2n_append_test_psk_with_early_data(server_conn, 1, &s2n_tls13_aes_128_gcm_sha256));
642 EXPECT_SUCCESS(s2n_connection_set_early_data_expected(server_conn));
643
644 EXPECT_OK(s2n_test_client_hello(client_conn, server_conn));
645
646 EXPECT_SUCCESS(s2n_server_hello_send(server_conn));
647 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io,
648 &client_conn->handshake.io, s2n_stuffer_data_available(&server_conn->handshake.io)));
649 EXPECT_SUCCESS(s2n_server_hello_recv(client_conn));
650
651 EXPECT_EQUAL(client_conn->early_data_state, S2N_EARLY_DATA_REQUESTED);
652 EXPECT_EQUAL(client_conn->server_protocol_version, S2N_TLS13);
653 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS13);
654
655 EXPECT_SUCCESS(s2n_connection_free(client_conn));
656 EXPECT_SUCCESS(s2n_connection_free(server_conn));
657 };
658
659 /* TLS 1.3 Client Early Data is rejected when server only supports TLS1.2 */
660 {
661 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
662 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
663 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, "test_all"));
664 EXPECT_OK(s2n_append_test_psk_with_early_data(client_conn, 1, &s2n_tls13_aes_128_gcm_sha256));
665 EXPECT_SUCCESS(s2n_connection_set_early_data_expected(client_conn));
666
667 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
668 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
669 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "test_all_tls12"));
670 EXPECT_OK(s2n_append_test_psk_with_early_data(server_conn, 1, &s2n_tls13_aes_128_gcm_sha256));
671 EXPECT_SUCCESS(s2n_connection_set_early_data_expected(server_conn));
672
673 EXPECT_OK(s2n_test_client_hello(client_conn, server_conn));
674
675 EXPECT_SUCCESS(s2n_server_hello_send(server_conn));
676 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io,
677 &client_conn->handshake.io, s2n_stuffer_data_available(&server_conn->handshake.io)));
678 EXPECT_FAILURE_WITH_ERRNO(s2n_server_hello_recv(client_conn), S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED);
679
680 EXPECT_EQUAL(client_conn->early_data_state, S2N_EARLY_DATA_REQUESTED);
681 EXPECT_EQUAL(client_conn->server_protocol_version, S2N_TLS12);
682 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS12);
683
684 EXPECT_SUCCESS(s2n_connection_free(client_conn));
685 EXPECT_SUCCESS(s2n_connection_free(server_conn));
686 };
687
688 EXPECT_SUCCESS(s2n_config_free(config));
689 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
690 }
691
692 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
693
694 END_TEST();
695 }