Debian Codegraph @

crt/s2n/tests/unit/s2n_client_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 "tls/s2n_client_hello.h"
17
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <stdint.h>
21 #include <unistd.h>
22
23 #include "api/s2n.h"
24 #include "s2n_test.h"
25 #include "testlib/s2n_sslv2_client_hello.h"
26 #include "testlib/s2n_testlib.h"
27 #include "tls/s2n_connection.h"
28 #include "tls/s2n_handshake.h"
29 #include "tls/s2n_quic_support.h"
30 #include "tls/s2n_security_policies.h"
31 #include "tls/s2n_tls.h"
32 #include "tls/s2n_tls13.h"
33 #include "tls/s2n_tls_parameters.h"
34 #include "utils/s2n_safety.h"
35
36 #define ZERO_TO_THIRTY_ONE 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, \
37 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
38
39 #define LENGTH_TO_SESSION_ID (S2N_TLS_PROTOCOL_VERSION_LEN + S2N_TLS_RANDOM_DATA_LEN)
40 #define TLS12_LENGTH_TO_CIPHER_LIST (LENGTH_TO_SESSION_ID + 1)
41 #define TLS13_LENGTH_TO_CIPHER_LIST (TLS12_LENGTH_TO_CIPHER_LIST + S2N_TLS_SESSION_ID_MAX_LEN)
42
43 #define COMPRESSION_METHODS 0x00, 0x01, 0x02, 0x03, 0x04
44 #define COMPRESSION_METHODS_LEN 0x05
45
46 int s2n_parse_client_hello(struct s2n_connection *conn);
47 S2N_RESULT s2n_client_hello_get_raw_extension(uint16_t extension_iana,
48 struct s2n_blob *raw_extensions, struct s2n_blob *extension);
49
50 int main(int argc, char **argv)
51 {
52 struct s2n_cert_chain_and_key *chain_and_key = NULL, *ecdsa_chain_and_key = NULL;
53
54 BEGIN_TEST();
55 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
56
57 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
58 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
59 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&ecdsa_chain_and_key,
60 S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY));
61
62 EXPECT_SUCCESS(setenv("S2N_DONT_MLOCK", "1", 0));
63
64 /* Test s2n_client_hello_get_extension_by_id */
65 {
66 /* Test with invalid parsed extensions */
67 {
68 struct s2n_connection *conn = NULL;
69 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
70
71 s2n_tls_extension_type test_extension_type = S2N_EXTENSION_SERVER_NAME;
72
73 s2n_extension_type_id test_extension_type_id = 0;
74 EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(test_extension_type, &test_extension_type_id));
75
76 uint8_t data[] = "data";
77 s2n_parsed_extension *parsed_extension = &conn->client_hello.extensions.parsed_extensions[test_extension_type_id];
78 parsed_extension->extension_type = test_extension_type;
79 parsed_extension->extension.data = data;
80 parsed_extension->extension.size = sizeof(data);
81
82 /* Succeeds with correct extension type */
83 EXPECT_EQUAL(s2n_client_hello_get_extension_by_id(&conn->client_hello,
84 test_extension_type, data, sizeof(data)),
85 sizeof(data));
86
87 /* Fails with wrong extension type */
88 parsed_extension->extension_type = test_extension_type + 1;
89 EXPECT_EQUAL(s2n_client_hello_get_extension_by_id(&conn->client_hello,
90 test_extension_type, data, sizeof(data)),
91 0);
92
93 EXPECT_SUCCESS(s2n_connection_free(conn));
94 };
95 };
96
97 /* Test s2n_client_hello_has_extension */
98 {
99 struct s2n_connection *conn = NULL;
100 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
101
102 uint8_t data[] = {
103 /* arbitrary extension with 2 data */
104 0xFF, 0x00, /* extension type */
105 0x00, 0x02, /* extension payload length */
106 0xAB, 0xCD, /* extension payload */
107 /* Encrypt then mac extension without data */
108 0x00, 0x16,
109 0x00, 0x00
110 };
111
112 struct s2n_blob *raw_extension = &conn->client_hello.extensions.raw;
113 raw_extension->data = data;
114 raw_extension->size = sizeof(data);
115
116 /* Succeeds on an unsupported extension with no payload */
117 bool exists = false;
118 EXPECT_SUCCESS(s2n_client_hello_has_extension(&conn->client_hello, 0x0016, &exists));
119 EXPECT_TRUE(exists);
120
121 /* Succeeds on an unsupported extension with payload */
122 exists = false;
123 EXPECT_SUCCESS(s2n_client_hello_has_extension(&conn->client_hello, 0xFF00, &exists));
124 EXPECT_TRUE(exists);
125
126 /* Succeeds with an invalid extension */
127 exists = false;
128 EXPECT_SUCCESS(s2n_client_hello_has_extension(&conn->client_hello, 0xFFFF, &exists));
129 EXPECT_FALSE(exists);
130
131 EXPECT_SUCCESS(s2n_connection_free(conn));
132 };
133
134 /* Test s2n_client_hello_has_extension with a zero-length extension */
135 for (int send_sct = 0; send_sct <= 1; send_sct++) {
136 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
137 EXPECT_OK(s2n_config_set_tls12_security_policy(config));
138 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
139
140 /* The SCT extension is zero-length. */
141 if (send_sct) {
142 EXPECT_SUCCESS(s2n_config_set_ct_support_level(config, S2N_CT_SUPPORT_REQUEST));
143 }
144
145 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
146 s2n_connection_ptr_free);
147 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
148
149 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
150 s2n_connection_ptr_free);
151 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
152
153 EXPECT_SUCCESS(s2n_client_hello_send(client));
154 EXPECT_SUCCESS(s2n_stuffer_copy(&client->handshake.io, &server->handshake.io,
155 s2n_stuffer_data_available(&client->handshake.io)));
156 EXPECT_SUCCESS(s2n_client_hello_recv(server));
157
158 struct s2n_client_hello *client_hello = s2n_connection_get_client_hello(server);
159 EXPECT_NOT_NULL(client_hello);
160
161 s2n_parsed_extension *sct_extension = NULL;
162 int ret = s2n_client_hello_get_parsed_extension(S2N_EXTENSION_CERTIFICATE_TRANSPARENCY, &client_hello->extensions,
163 &sct_extension);
164
165 if (send_sct) {
166 /* Ensure that the extension was received. */
167 EXPECT_SUCCESS(ret);
168 POSIX_ENSURE_REF(sct_extension);
169
170 /* Ensure that the extension is zero-length. */
171 EXPECT_EQUAL(sct_extension->extension.size, 0);
172 } else {
173 /* The extension shouldn't have been received because it wasn't requested. */
174 EXPECT_FAILURE_WITH_ERRNO(ret, S2N_ERR_EXTENSION_NOT_RECEIVED);
175 }
176 }
177
178 /* Test s2n_client_hello_get_raw_extension */
179 {
180 uint8_t data[] = {
181 /* arbitrary extension with 2 data */
182 0xFF, 0x00, /* extension type */
183 0x00, 0x02, /* extension payload length */
184 0xAB, 0xCD, /* extension payload */
185 /* NPN extension without data */
186 0x33, 0x74,
187 0x00, 0x00
188 };
189 struct s2n_blob raw_extension = {
190 .data = data,
191 .size = sizeof(data),
192 };
193
194 struct s2n_blob extension = { 0 };
195 /* Succeeds with extension exists without payload */
196 EXPECT_OK(s2n_client_hello_get_raw_extension(0x3374, &raw_extension, &extension));
197 EXPECT_EQUAL(extension.size, 0);
198 EXPECT_NOT_NULL(extension.data);
199
200 /* Succeeds with extension exists with payload */
201 extension = (struct s2n_blob){ 0 };
202 EXPECT_OK(s2n_client_hello_get_raw_extension(0xFF00, &raw_extension, &extension));
203 EXPECT_EQUAL(extension.size, 2);
204 EXPECT_NOT_NULL(extension.data);
205 EXPECT_BYTEARRAY_EQUAL(extension.data, &data[4], 2);
206
207 /* Failed with extension not exist */
208 extension = (struct s2n_blob){ 0 };
209 EXPECT_OK(s2n_client_hello_get_raw_extension(0xFFFF, &raw_extension, &extension));
210 EXPECT_EQUAL(extension.size, 0);
211 EXPECT_NULL(extension.data);
212 };
213
214 /* Test setting cert chain on recv */
215 {
216 s2n_enable_tls13_in_test();
217 struct s2n_config *config = NULL;
218 EXPECT_NOT_NULL(config = s2n_config_new());
219
220 /* TLS13 fails to parse client hello when no certs set */
221 {
222 struct s2n_connection *conn = NULL;
223 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
224 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
225 conn->client_protocol_version = conn->server_protocol_version;
226 conn->actual_protocol_version = conn->client_protocol_version;
227
228 EXPECT_SUCCESS(s2n_client_hello_send(conn));
229 EXPECT_TRUE(s2n_stuffer_data_available(&conn->handshake.io) > 0);
230 EXPECT_FAILURE_WITH_ERRNO(s2n_client_hello_recv(conn), S2N_ERR_NO_VALID_SIGNATURE_SCHEME);
231
232 EXPECT_SUCCESS(s2n_connection_free(conn));
233 };
234
235 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, ecdsa_chain_and_key));
236
237 /* TLS13 successfully sets certs */
238 {
239 struct s2n_connection *conn = NULL;
240 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
241 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
242 conn->client_protocol_version = conn->server_protocol_version;
243 conn->actual_protocol_version = conn->client_protocol_version;
244
245 EXPECT_SUCCESS(s2n_client_hello_send(conn));
246 EXPECT_TRUE(s2n_stuffer_data_available(&conn->handshake.io) > 0);
247 EXPECT_SUCCESS(s2n_client_hello_recv(conn));
248
249 EXPECT_NOT_NULL(conn->handshake_params.our_chain_and_key);
250
251 EXPECT_SUCCESS(s2n_connection_free(conn));
252 };
253
254 EXPECT_SUCCESS(s2n_config_free(config));
255 s2n_disable_tls13_in_test();
256 };
257
258 /* Test getting supported versions from the client hello */
259 if (s2n_is_tls13_fully_supported()) {
260 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
261 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
262 EXPECT_NOT_NULL(config);
263 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "test_all"));
264 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, ecdsa_chain_and_key));
265 /* TLS13 has supported versions in the client hello */
266 {
267 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
268 s2n_connection_ptr_free);
269 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
270 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
271 s2n_connection_ptr_free);
272 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
273
274 uint8_t supported_versions[256] = { 0 };
275 uint8_t size_of_version_list = 0;
276 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
277 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io,
278 &server_conn->handshake.io, s2n_stuffer_data_available(&client_conn->handshake.io)));
279 EXPECT_SUCCESS(s2n_client_hello_get_extension_by_id(&server_conn->client_hello,
280 S2N_EXTENSION_SUPPORTED_VERSIONS, supported_versions, sizeof(supported_versions)));
281 size_of_version_list = supported_versions[0];
282 /* No supported versions before the handshake is received */
283 EXPECT_EQUAL(0, size_of_version_list);
284 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
285 EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS13);
286 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS13);
287 EXPECT_SUCCESS(s2n_client_hello_get_extension_by_id(&server_conn->client_hello,
288 S2N_EXTENSION_SUPPORTED_VERSIONS, supported_versions, sizeof(supported_versions)));
289 size_of_version_list = supported_versions[0];
290 EXPECT_TRUE(size_of_version_list > 0);
291 bool found_tls13 = false;
292 const uint8_t tls13_bytes[] = { 0x03, 0x04 };
293 const size_t supported_version_size = sizeof(tls13_bytes);
294 for (uint16_t offset = 1; offset < size_of_version_list; offset += supported_version_size) {
295 if (memcmp(tls13_bytes, &supported_versions[offset], supported_version_size) == 0) {
296 found_tls13 = true;
297 }
298 }
299 EXPECT_TRUE(found_tls13);
300 };
301 s2n_disable_tls13_in_test();
302 };
303
304 /* Test generating session id */
305 {
306 const uint8_t test_session_id[S2N_TLS_SESSION_ID_MAX_LEN] = { 7 };
307
308 /* Use session id if already generated */
309 for (uint8_t i = S2N_TLS10; i <= S2N_TLS13; i++) {
310 if (i >= S2N_TLS13) {
311 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
312 }
313
314 struct s2n_connection *conn = NULL;
315 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
316 EXPECT_OK(s2n_connection_set_tls12_security_policy(conn));
317 struct s2n_stuffer *hello_stuffer = &conn->handshake.io;
318 conn->actual_protocol_version = i;
319
320 conn->session_id_len = S2N_TLS_SESSION_ID_MAX_LEN;
321 EXPECT_MEMCPY_SUCCESS(conn->session_id, test_session_id, S2N_TLS_SESSION_ID_MAX_LEN);
322
323 EXPECT_SUCCESS(s2n_client_hello_send(conn));
324 EXPECT_SUCCESS(s2n_stuffer_skip_read(hello_stuffer, LENGTH_TO_SESSION_ID));
325
326 uint8_t session_id_length = 0;
327 EXPECT_SUCCESS(s2n_stuffer_read_uint8(hello_stuffer, &session_id_length));
328 EXPECT_EQUAL(session_id_length, S2N_TLS_SESSION_ID_MAX_LEN);
329
330 uint8_t *session_id = NULL;
331 EXPECT_NOT_NULL(session_id = s2n_stuffer_raw_read(hello_stuffer, S2N_TLS_SESSION_ID_MAX_LEN));
332 EXPECT_BYTEARRAY_EQUAL(session_id, test_session_id, S2N_TLS_SESSION_ID_MAX_LEN);
333
334 EXPECT_SUCCESS(s2n_connection_free(conn));
335 }
336 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
337
338 /* With TLS1.3 */
339 if (s2n_is_tls13_fully_supported()) {
340 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
341
342 /* Generate a session id by default */
343 {
344 struct s2n_connection *conn = NULL;
345 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
346 struct s2n_stuffer *hello_stuffer = &conn->handshake.io;
347
348 EXPECT_SUCCESS(s2n_client_hello_send(conn));
349 EXPECT_SUCCESS(s2n_stuffer_skip_read(hello_stuffer, LENGTH_TO_SESSION_ID));
350
351 uint8_t session_id_length = 0;
352 EXPECT_SUCCESS(s2n_stuffer_read_uint8(hello_stuffer, &session_id_length));
353 EXPECT_EQUAL(session_id_length, S2N_TLS_SESSION_ID_MAX_LEN);
354
355 EXPECT_SUCCESS(s2n_connection_free(conn));
356 };
357
358 /* Do NOT generate a session id if middlebox compatibility mode is disabled.
359 * For now, middlebox compatibility mode is only disabled by QUIC.
360 */
361 {
362 struct s2n_config *config = NULL;
363 EXPECT_NOT_NULL(config = s2n_config_new());
364 EXPECT_SUCCESS(s2n_config_enable_quic(config));
365
366 struct s2n_connection *conn = NULL;
367 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
368 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
369 struct s2n_stuffer *hello_stuffer = &conn->handshake.io;
370
371 EXPECT_SUCCESS(s2n_client_hello_send(conn));
372 EXPECT_SUCCESS(s2n_stuffer_skip_read(hello_stuffer, LENGTH_TO_SESSION_ID));
373
374 uint8_t session_id_length = 0;
375 EXPECT_SUCCESS(s2n_stuffer_read_uint8(hello_stuffer, &session_id_length));
376 EXPECT_EQUAL(session_id_length, 0);
377
378 EXPECT_SUCCESS(s2n_connection_free(conn));
379 EXPECT_SUCCESS(s2n_config_free(config));
380 };
381
382 /* Generate a session id if trying to resume a <TLS1.3 session */
383 {
384 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
385 s2n_connection_ptr_free);
386 EXPECT_NOT_NULL(conn);
387 struct s2n_stuffer *hello_stuffer = &conn->handshake.io;
388 conn->resume_protocol_version = S2N_TLS12;
389 EXPECT_TRUE(conn->actual_protocol_version >= S2N_TLS13);
390 EXPECT_TRUE(conn->client_protocol_version >= S2N_TLS13);
391
392 EXPECT_SUCCESS(s2n_client_hello_send(conn));
393 EXPECT_SUCCESS(s2n_stuffer_skip_read(hello_stuffer, LENGTH_TO_SESSION_ID));
394
395 uint8_t session_id_length = 0;
396 EXPECT_SUCCESS(s2n_stuffer_read_uint8(hello_stuffer, &session_id_length));
397 EXPECT_EQUAL(session_id_length, S2N_TLS_SESSION_ID_MAX_LEN);
398 };
399
400 /* Fail if we need to generate a session id to resume a <TLS1.3 session
401 * with QUIC support enabled.
402 *
403 * This should never happen because QUIC requires TLS1.3, so a QUIC
404 * client should never receive a valid TLS1.2 ticket from a QUIC server.
405 */
406 {
407 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
408 s2n_connection_ptr_free);
409 EXPECT_NOT_NULL(conn);
410 conn->resume_protocol_version = S2N_TLS12;
411 EXPECT_TRUE(conn->actual_protocol_version >= S2N_TLS13);
412 EXPECT_TRUE(conn->client_protocol_version >= S2N_TLS13);
413 conn->quic_enabled = true;
414
415 EXPECT_FAILURE_WITH_ERRNO(s2n_client_hello_send(conn),
416 S2N_ERR_UNSUPPORTED_WITH_QUIC);
417 };
418
419 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
420 }
421
422 /* With TLS1.2 */
423 {
424 /* Do NOT generate a session id by default */
425 {
426 struct s2n_connection *conn = NULL;
427 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
428 EXPECT_OK(s2n_connection_set_tls12_security_policy(conn));
429 struct s2n_stuffer *hello_stuffer = &conn->handshake.io;
430
431 EXPECT_SUCCESS(s2n_client_hello_send(conn));
432 EXPECT_SUCCESS(s2n_stuffer_skip_read(hello_stuffer, LENGTH_TO_SESSION_ID));
433
434 uint8_t session_id_length = 0;
435 EXPECT_SUCCESS(s2n_stuffer_read_uint8(hello_stuffer, &session_id_length));
436 EXPECT_EQUAL(session_id_length, 0);
437
438 EXPECT_SUCCESS(s2n_connection_free(conn));
439 };
440
441 /* Generate a session id if using tickets */
442 {
443 struct s2n_config *config = NULL;
444 EXPECT_NOT_NULL(config = s2n_config_new());
445 EXPECT_SUCCESS(s2n_config_set_session_tickets_onoff(config, true));
446
447 struct s2n_connection *conn = NULL;
448 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
449 EXPECT_OK(s2n_config_set_tls12_security_policy(config));
450 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
451 struct s2n_stuffer *hello_stuffer = &conn->handshake.io;
452
453 EXPECT_SUCCESS(s2n_client_hello_send(conn));
454 EXPECT_SUCCESS(s2n_stuffer_skip_read(hello_stuffer, LENGTH_TO_SESSION_ID));
455
456 uint8_t session_id_length = 0;
457 EXPECT_SUCCESS(s2n_stuffer_read_uint8(hello_stuffer, &session_id_length));
458 EXPECT_EQUAL(session_id_length, S2N_TLS_SESSION_ID_MAX_LEN);
459
460 EXPECT_SUCCESS(s2n_connection_free(conn));
461 EXPECT_SUCCESS(s2n_config_free(config));
462 };
463 };
464 };
465
466 /* Test cipher suites list */
467 {
468 /* When TLS 1.3 NOT supported */
469 {
470 /* TLS 1.3 cipher suites NOT written by client by default */
471 {
472 struct s2n_connection *conn = NULL;
473 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
474 EXPECT_OK(s2n_connection_set_tls12_security_policy(conn));
475 struct s2n_stuffer *hello_stuffer = &conn->handshake.io;
476
477 EXPECT_SUCCESS(s2n_client_hello_send(conn));
478 EXPECT_TRUE(conn->client_protocol_version < S2N_TLS13);
479 EXPECT_SUCCESS(s2n_stuffer_skip_read(hello_stuffer, TLS12_LENGTH_TO_CIPHER_LIST));
480
481 uint16_t list_length = 0;
482 EXPECT_SUCCESS(s2n_stuffer_read_uint16(hello_stuffer, &list_length));
483 EXPECT_NOT_EQUAL(list_length, 0);
484
485 uint8_t first_cipher_byte = 0;
486 for (int i = 0; i < list_length; i++) {
487 EXPECT_SUCCESS(s2n_stuffer_read_uint8(hello_stuffer, &first_cipher_byte));
488 EXPECT_NOT_EQUAL(first_cipher_byte, 0x13);
489 EXPECT_SUCCESS(s2n_stuffer_skip_read(hello_stuffer, 1));
490 }
491
492 EXPECT_SUCCESS(s2n_connection_free(conn));
493 };
494
495 /* TLS 1.3 cipher suites NOT written by client even if included in security policy */
496 {
497 struct s2n_connection *conn = NULL;
498 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
499 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "default_tls13"));
500
501 struct s2n_stuffer *hello_stuffer = &conn->handshake.io;
502
503 EXPECT_SUCCESS(s2n_client_hello_send(conn));
504 EXPECT_TRUE(conn->client_protocol_version < S2N_TLS13);
505 EXPECT_SUCCESS(s2n_stuffer_skip_read(hello_stuffer, TLS12_LENGTH_TO_CIPHER_LIST));
506
507 uint16_t list_length = 0;
508 EXPECT_SUCCESS(s2n_stuffer_read_uint16(hello_stuffer, &list_length));
509 EXPECT_NOT_EQUAL(list_length, 0);
510
511 uint8_t first_cipher_byte = 0;
512 for (int i = 0; i < list_length; i++) {
513 EXPECT_SUCCESS(s2n_stuffer_read_uint8(hello_stuffer, &first_cipher_byte));
514 EXPECT_NOT_EQUAL(first_cipher_byte, 0x13);
515 EXPECT_SUCCESS(s2n_stuffer_skip_read(hello_stuffer, 1));
516 }
517
518 EXPECT_SUCCESS(s2n_connection_free(conn));
519 };
520 };
521
522 /* When TLS 1.3 supported */
523 if (s2n_is_tls13_fully_supported()) {
524 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
525
526 struct s2n_config *config = NULL;
527 EXPECT_NOT_NULL(config = s2n_config_new());
528 s2n_config_set_session_tickets_onoff(config, 0);
529
530 /* TLS 1.3 cipher suites written by client */
531 {
532 struct s2n_connection *conn = NULL;
533 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
534 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
535
536 struct s2n_stuffer *hello_stuffer = &conn->handshake.io;
537
538 EXPECT_SUCCESS(s2n_client_hello_send(conn));
539
540 EXPECT_TRUE(conn->actual_protocol_version >= S2N_TLS13);
541 EXPECT_SUCCESS(s2n_stuffer_skip_read(hello_stuffer, TLS13_LENGTH_TO_CIPHER_LIST));
542
543 uint16_t list_length = 0;
544 EXPECT_SUCCESS(s2n_stuffer_read_uint16(hello_stuffer, &list_length));
545 EXPECT_NOT_EQUAL(list_length, 0);
546
547 uint8_t first_cipher_byte = 0;
548 int tls13_ciphers_found = 0;
549 for (int i = 0; i < list_length; i++) {
550 EXPECT_SUCCESS(s2n_stuffer_read_uint8(hello_stuffer, &first_cipher_byte));
551 if (first_cipher_byte == 0x13) {
552 tls13_ciphers_found++;
553 }
554 EXPECT_SUCCESS(s2n_stuffer_skip_read(hello_stuffer, 1));
555 }
556 EXPECT_NOT_EQUAL(tls13_ciphers_found, 0);
557
558 EXPECT_SUCCESS(s2n_connection_free(conn));
559 };
560
561 EXPECT_SUCCESS(s2n_config_free(config));
562 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
563 }
564
565 /* TLS_EMPTY_RENEGOTIATION_INFO_SCSV included if TLS1.2 ciphers included
566 *
567 *= https://www.rfc-editor.org/rfc/rfc5746#3.4
568 *= type=test
569 *# o The client MUST include either an empty "renegotiation_info"
570 *# extension, or the TLS_EMPTY_RENEGOTIATION_INFO_SCSV signaling
571 *# cipher suite value in the ClientHello.
572 */
573 if (s2n_is_tls13_fully_supported()) {
574 EXPECT_SUCCESS(s2n_reset_tls13_in_test());
575 const uint8_t empty_renegotiation_info_scsv[S2N_TLS_CIPHER_SUITE_LEN] = { TLS_EMPTY_RENEGOTIATION_INFO_SCSV };
576
577 struct {
578 const char *security_policy;
579 bool expect_renegotiation_info;
580 } test_cases[] = {
581 { .security_policy = "test_all_tls13", .expect_renegotiation_info = false },
582 { .security_policy = "default_tls13", .expect_renegotiation_info = true },
583 /* 20240501 can only negotiate up to tls12 */
584 { .security_policy = "20240501", .expect_renegotiation_info = true },
585 };
586
587 for (size_t i = 0; i < s2n_array_len(test_cases); i++) {
588 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
589 EXPECT_NOT_NULL(conn);
590 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, test_cases[i].security_policy));
591
592 EXPECT_SUCCESS(s2n_client_hello_send(conn));
593 EXPECT_SUCCESS(s2n_parse_client_hello(conn));
594
595 struct s2n_blob *cipher_suites = &conn->client_hello.cipher_suites;
596 EXPECT_TRUE(cipher_suites->size > 0);
597
598 uint8_t *iana = cipher_suites->data;
599 bool found_renegotiation_info = false;
600 for (size_t j = 0; j < cipher_suites->size; j += S2N_TLS_CIPHER_SUITE_LEN) {
601 if (memcmp(iana + j, empty_renegotiation_info_scsv, S2N_TLS_CIPHER_SUITE_LEN) == 0) {
602 found_renegotiation_info = true;
603 }
604 }
605
606 EXPECT_EQUAL(found_renegotiation_info, test_cases[i].expect_renegotiation_info);
607
608 EXPECT_SUCCESS(s2n_connection_free(conn));
609 }
610 }
611
612 /* TLS1.2 cipher suites not written if QUIC enabled */
613 {
614 EXPECT_SUCCESS(s2n_reset_tls13_in_test());
615
616 struct s2n_config *config = s2n_config_new();
617 EXPECT_NOT_NULL(config);
618 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13"));
619
620 bool quic_enabled[] = { false, s2n_is_tls13_fully_supported() };
621
622 /* TLS 1.2 cipher suites only written if QUIC not enabled */
623 for (size_t i = 0; i < s2n_array_len(quic_enabled); i++) {
624 config->quic_enabled = quic_enabled[i];
625
626 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
627 EXPECT_NOT_NULL(conn);
628 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
629
630 EXPECT_SUCCESS(s2n_client_hello_send(conn));
631 EXPECT_SUCCESS(s2n_parse_client_hello(conn));
632
633 struct s2n_blob *cipher_suites = &conn->client_hello.cipher_suites;
634 EXPECT_TRUE(cipher_suites->size > 0);
635
636 bool tls12_cipher_found = false;
637 uint8_t *iana = cipher_suites->data;
638 for (size_t j = 0; j < cipher_suites->size; j += S2N_TLS_CIPHER_SUITE_LEN) {
639 /* All TLS1.3 cipher suites have IANAs starting with 0x13 */
640 if (iana[j] != 0x13) {
641 tls12_cipher_found = true;
642 }
643 }
644
645 /* TLS1.2 and QUIC are mutually exclusive */
646 EXPECT_TRUE(tls12_cipher_found != quic_enabled[i]);
647
648 EXPECT_SUCCESS(s2n_connection_free(conn));
649 }
650
651 EXPECT_SUCCESS(s2n_config_free(config));
652 };
653
654 /* Error if no cipher suites written. */
655 {
656 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
657 s2n_connection_ptr_free);
658 EXPECT_NOT_NULL(conn);
659
660 struct s2n_cipher_suite cipher_suite = s2n_ecdhe_ecdsa_with_aes_128_gcm_sha256;
661 struct s2n_cipher_suite *cipher_suites = &cipher_suite;
662 struct s2n_cipher_preferences cipher_preferences = {
663 .suites = &cipher_suites,
664 .count = 1,
665 };
666 struct s2n_security_policy policy = *conn->config->security_policy;
667 policy.cipher_preferences = &cipher_preferences;
668 conn->security_policy_override = &policy;
669
670 /* Fails with no cipher suites available / written */
671 cipher_suite.available = false;
672 EXPECT_FAILURE_WITH_ERRNO(s2n_client_hello_send(conn),
673 S2N_ERR_INVALID_CIPHER_PREFERENCES);
674
675 /* Succeeds with one cipher suite available / written */
676 /* cppcheck-suppress redundantAssignment */
677 cipher_suite.available = true;
678 EXPECT_SUCCESS(s2n_client_hello_send(conn));
679 };
680 };
681
682 /* Test that negotiating TLS1.2 with QUIC-enabled server fails */
683 if (s2n_is_tls13_fully_supported()) {
684 EXPECT_SUCCESS(s2n_reset_tls13_in_test());
685
686 struct s2n_config *config = s2n_config_new();
687 EXPECT_SUCCESS(s2n_config_enable_quic(config));
688 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "test_all"));
689 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, ecdsa_chain_and_key));
690
691 /* Succeeds when negotiating TLS1.3 */
692 if (s2n_is_tls13_fully_supported()) {
693 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
694 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
695
696 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
697 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
698
699 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
700 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io,
701 &server_conn->handshake.io, s2n_stuffer_data_available(&client_conn->handshake.io)));
702 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
703
704 EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS13);
705 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS13);
706
707 EXPECT_SUCCESS(s2n_connection_free(client_conn));
708 EXPECT_SUCCESS(s2n_connection_free(server_conn));
709 }
710
711 /* Fails when negotiating TLS1.2 */
712 {
713 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
714 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, "test_all_tls12"));
715
716 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
717 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
718
719 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
720 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io,
721 &server_conn->handshake.io, s2n_stuffer_data_available(&client_conn->handshake.io)));
722 EXPECT_FAILURE_WITH_ERRNO(s2n_client_hello_recv(server_conn), S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED);
723
724 EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS12);
725
726 EXPECT_SUCCESS(s2n_connection_free(client_conn));
727 EXPECT_SUCCESS(s2n_connection_free(server_conn));
728 };
729
730 EXPECT_SUCCESS(s2n_config_free(config));
731 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
732 }
733
734 /* Test that cipher suites enforce proper highest supported versions.
735 * Eg. server configs TLS 1.2 only ciphers should never negotiate TLS 1.3
736 */
737 {
738 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
739
740 struct s2n_config *config = NULL;
741 EXPECT_NOT_NULL(config = s2n_config_new());
742
743 {
744 /* TLS 1.3 client cipher preference uses TLS13 version */
745 struct s2n_connection *conn = NULL;
746 const struct s2n_security_policy *security_policy = NULL;
747 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
748 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
749 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13"));
750 EXPECT_SUCCESS(s2n_connection_get_security_policy(conn, &security_policy));
751 EXPECT_TRUE(s2n_security_policy_supports_tls13(security_policy));
752
753 EXPECT_SUCCESS(s2n_client_hello_send(conn));
754 EXPECT_EQUAL(conn->actual_protocol_version, s2n_get_highest_fully_supported_tls_version());
755 EXPECT_EQUAL(conn->client_protocol_version, s2n_get_highest_fully_supported_tls_version());
756 EXPECT_EQUAL(conn->client_hello.legacy_version, S2N_TLS12);
757
758 EXPECT_SUCCESS(s2n_connection_free(conn));
759 };
760
761 {
762 /* TLS 1.2 client cipher preference uses TLS12 version */
763 struct s2n_connection *conn = NULL;
764 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
765 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
766 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "20240501"));
767
768 const struct s2n_security_policy *security_policy = NULL;
769 POSIX_GUARD(s2n_connection_get_security_policy(conn, &security_policy));
770 EXPECT_FALSE(s2n_security_policy_supports_tls13(security_policy));
771
772 EXPECT_SUCCESS(s2n_client_hello_send(conn));
773 EXPECT_EQUAL(conn->actual_protocol_version, S2N_TLS12);
774 EXPECT_EQUAL(conn->client_protocol_version, S2N_TLS12);
775 EXPECT_EQUAL(conn->client_hello.legacy_version, S2N_TLS12);
776
777 EXPECT_SUCCESS(s2n_connection_free(conn));
778 };
779
780 {
781 /* TLS 1.3 client cipher preference uses TLS13 version */
782 struct s2n_connection *client_conn = NULL, *server_conn = NULL;
783 const struct s2n_security_policy *security_policy = NULL;
784
785 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
786 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
787
788 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "test_all"));
789
790 POSIX_GUARD(s2n_connection_get_security_policy(client_conn, &security_policy));
791 EXPECT_TRUE(s2n_security_policy_supports_tls13(security_policy));
792
793 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
794 EXPECT_EQUAL(client_conn->actual_protocol_version, s2n_get_highest_fully_supported_tls_version());
795 EXPECT_EQUAL(client_conn->client_protocol_version, s2n_get_highest_fully_supported_tls_version());
796 EXPECT_EQUAL(client_conn->client_hello.legacy_version, S2N_TLS12);
797
798 /* Server configured with TLS 1.2 negotiates TLS12 version */
799 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
800 struct s2n_config *server_config = NULL;
801 EXPECT_NOT_NULL(server_config = s2n_config_new());
802 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
803 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
804
805 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "test_all_tls12"));
806
807 POSIX_GUARD(s2n_connection_get_security_policy(server_conn, &security_policy));
808 EXPECT_FALSE(s2n_security_policy_supports_tls13(security_policy));
809
810 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io, s2n_stuffer_data_available(&client_conn->handshake.io)));
811
812 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
813 EXPECT_EQUAL(server_conn->server_protocol_version, S2N_TLS12);
814 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS12);
815 EXPECT_EQUAL(server_conn->client_protocol_version, S2N_TLS12);
816 EXPECT_EQUAL(server_conn->client_hello.legacy_version, S2N_TLS12);
817
818 EXPECT_SUCCESS(s2n_connection_free(server_conn));
819 EXPECT_SUCCESS(s2n_connection_free(client_conn));
820 EXPECT_SUCCESS(s2n_config_free(server_config));
821 };
822
823 EXPECT_SUCCESS(s2n_config_free(config));
824 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
825 };
826
827 /* SSlv2 client hello */
828 {
829 struct s2n_connection *server_conn = NULL;
830 struct s2n_config *server_config = NULL;
831 s2n_blocked_status server_blocked;
832
833 uint8_t sslv2_client_hello[] = {
834 SSLv2_CLIENT_HELLO_PREFIX,
835 SSLv2_CLIENT_HELLO_CIPHER_SUITES,
836 SSLv2_CLIENT_HELLO_CHALLENGE,
837 };
838
839 int sslv2_client_hello_len = sizeof(sslv2_client_hello);
840
841 uint8_t sslv2_client_hello_header[] = {
842 SSLv2_CLIENT_HELLO_HEADER,
843 };
844
845 int sslv2_client_hello_header_len = sizeof(sslv2_client_hello_header);
846
847 /* Create nonblocking pipes */
848 struct s2n_test_io_pair io_pair;
849 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
850
851 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
852 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
853
854 EXPECT_NOT_NULL(server_config = s2n_config_new());
855 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
856 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
857
858 /* The security policy does not need to support SSLv2.
859 *
860 * s2n-tls does NOT support SSLv2. However, it does accept ClientHellos in the SSLv2
861 * format but advertising higher protocol versions. Clients use this strategy to
862 * communicate with servers in a backwards-compatible way.
863 *
864 * Our test SSLv2 ClientHello advertises TLS1.2.
865 * So the security policy only needs to support TLS1.2.
866 * (and at least one of the ciphers in the hard coded sslv2 client hello)
867 */
868 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config, "20170210"));
869
870 /* Send the client hello message */
871 EXPECT_EQUAL(s2n_test_send(io_pair.client, sslv2_client_hello_header, sslv2_client_hello_header_len), sslv2_client_hello_header_len);
872 EXPECT_EQUAL(s2n_test_send(io_pair.client, sslv2_client_hello, sslv2_client_hello_len), sslv2_client_hello_len);
873
874 /* Verify that the sent client hello message is accepted */
875 s2n_negotiate(server_conn, &server_blocked);
876 EXPECT_TRUE(s2n_conn_get_current_message_type(server_conn) > CLIENT_HELLO);
877 EXPECT_TRUE(IS_NEGOTIATED(server_conn));
878
879 struct s2n_client_hello *client_hello = s2n_connection_get_client_hello(server_conn);
880
881 /* Verify s2n_connection_get_client_hello returns the handle to the s2n_client_hello on the connection */
882 EXPECT_EQUAL(client_hello, &server_conn->client_hello);
883
884 uint8_t *collected_client_hello = client_hello->raw_message.data;
885 uint16_t collected_client_hello_len = client_hello->raw_message.size;
886
887 /* Verify correctly identified as SSLv2 */
888 EXPECT_TRUE(client_hello->sslv2);
889
890 /* Verify collected client hello message length */
891 EXPECT_EQUAL(collected_client_hello_len, sslv2_client_hello_len);
892
893 /* Verify the collected client hello matches what was sent */
894 EXPECT_BYTEARRAY_EQUAL(collected_client_hello, sslv2_client_hello, sslv2_client_hello_len);
895
896 /* Verify s2n_client_hello_get_raw_message_length correct */
897 EXPECT_EQUAL(s2n_client_hello_get_raw_message_length(client_hello), sslv2_client_hello_len);
898
899 uint8_t expected_cs[] = {
900 SSLv2_CLIENT_HELLO_CIPHER_SUITES,
901 };
902
903 /* Verify collected cipher_suites size correct */
904 EXPECT_EQUAL(client_hello->cipher_suites.size, sizeof(expected_cs));
905
906 /* Verify collected cipher_suites correct */
907 EXPECT_BYTEARRAY_EQUAL(client_hello->cipher_suites.data, expected_cs, sizeof(expected_cs));
908
909 /* Verify s2n_client_hello_get_cipher_suites_length correct */
910 EXPECT_EQUAL(s2n_client_hello_get_cipher_suites_length(client_hello), sizeof(expected_cs));
911
912 /* Verify collected extensions size correct */
913 EXPECT_EQUAL(client_hello->extensions.raw.size, 0);
914
915 /* Verify s2n_client_hello_get_extensions_length correct */
916 EXPECT_EQUAL(s2n_client_hello_get_extensions_length(client_hello), 0);
917
918 /* Verify s2n_client_hello_get_session_id_length correct */
919 uint32_t ch_session_id_length = 0;
920 EXPECT_SUCCESS(s2n_client_hello_get_session_id_length(client_hello, &ch_session_id_length));
921 EXPECT_EQUAL(ch_session_id_length, 0);
922
923 /* Free all handshake data */
924 EXPECT_SUCCESS(s2n_connection_free_handshake(server_conn));
925
926 /* Verify free_handshake resized the s2n_client_hello.raw_message stuffer back to 0 */
927 EXPECT_NULL(client_hello->raw_message.data);
928 EXPECT_EQUAL(client_hello->raw_message.size, 0);
929
930 EXPECT_SUCCESS(s2n_shutdown(server_conn, &server_blocked));
931
932 /* Wipe connection */
933 EXPECT_SUCCESS(s2n_connection_wipe(server_conn));
934
935 /* Verify connection_wipe resized the s2n_client_hello.raw_message stuffer back to 0 */
936 EXPECT_NULL(client_hello->raw_message.data);
937 EXPECT_EQUAL(client_hello->raw_message.size, 0);
938
939 /* Verify the s2n blobs referencing cipher_suites and extensions have cleared */
940 EXPECT_EQUAL(client_hello->cipher_suites.size, 0);
941 EXPECT_NULL(client_hello->cipher_suites.data);
942 EXPECT_EQUAL(client_hello->extensions.raw.size, 0);
943 EXPECT_NULL(client_hello->extensions.raw.data);
944
945 EXPECT_SUCCESS(s2n_connection_free(server_conn));
946
947 EXPECT_SUCCESS(s2n_config_free(server_config));
948 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
949 };
950
951 /* Minimal TLS 1.2 client hello. */
952 {
953 struct s2n_connection *server_conn = NULL;
954 struct s2n_config *server_config = NULL;
955 s2n_blocked_status server_blocked;
956 uint8_t *sent_client_hello = NULL;
957 uint8_t *expected_client_hello = NULL;
958
959 uint8_t client_extensions[] = {
960 /* Extension type TLS_EXTENSION_SERVER_NAME */
961 0x00,
962 0x00,
963 /* Extension size */
964 0x00,
965 0x08,
966 /* Server names len */
967 0x00,
968 0x06,
969 /* First server name type - host name */
970 0x00,
971 /* First server name len */
972 0x00,
973 0x03,
974 /* First server name, matches sent_server_name */
975 's',
976 'v',
977 'r',
978 };
979
980 uint8_t server_name_extension[] = {
981 /* Server names len */
982 0x00,
983 0x06,
984 /* First server name type - host name */
985 0x00,
986 /* First server name len */
987 0x00,
988 0x03,
989 /* First server name, matches sent_server_name */
990 's',
991 'v',
992 'r',
993 };
994 int server_name_extension_len = sizeof(server_name_extension);
995
996 size_t client_extensions_len = sizeof(client_extensions);
997 uint8_t client_hello_prefix[] = {
998 /* Protocol version TLS 1.2 */
999 0x03,
1000 0x03,
1001 /* Client random */
1002 ZERO_TO_THIRTY_ONE,
1003 /* SessionID len - 32 bytes */
1004 0x20,
1005 /* Session ID */
1006 ZERO_TO_THIRTY_ONE,
1007 /* Cipher suites len */
1008 0x00,
1009 0x02,
1010 /* Cipher suite - TLS_RSA_WITH_AES_128_CBC_SHA256 */
1011 0x00,
1012 0x3C,
1013 /* Compression methods len */
1014 0x01,
1015 /* Compression method - none */
1016 0x00,
1017 /* Extensions len */
1018 (client_extensions_len >> 8) & 0xff,
1019 (client_extensions_len & 0xff),
1020 };
1021 int client_hello_prefix_len = sizeof(client_hello_prefix);
1022 int sent_client_hello_len = client_hello_prefix_len + client_extensions_len;
1023 uint8_t message_header[] = {
1024 /* Handshake message type CLIENT HELLO */
1025 0x01,
1026 /* Body len */
1027 (sent_client_hello_len >> 16) & 0xff,
1028 (sent_client_hello_len >> 8) & 0xff,
1029 (sent_client_hello_len & 0xff),
1030 };
1031 int message_len = sizeof(message_header) + sent_client_hello_len;
1032 uint8_t record_header[] = {
1033 /* Record type HANDSHAKE */
1034 0x16,
1035 /* Protocol version TLS 1.2 */
1036 0x03,
1037 0x03,
1038 /* Message len */
1039 (message_len >> 8) & 0xff,
1040 (message_len & 0xff),
1041 };
1042
1043 EXPECT_NOT_NULL(sent_client_hello = malloc(sent_client_hello_len));
1044 EXPECT_MEMCPY_SUCCESS(sent_client_hello, client_hello_prefix, client_hello_prefix_len);
1045 EXPECT_MEMCPY_SUCCESS(sent_client_hello + client_hello_prefix_len, client_extensions, client_extensions_len);
1046
1047 /* Create nonblocking pipes */
1048 struct s2n_test_io_pair io_pair;
1049 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
1050
1051 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
1052 server_conn->actual_protocol_version = S2N_TLS12;
1053 server_conn->server_protocol_version = S2N_TLS12;
1054 server_conn->client_protocol_version = S2N_TLS12;
1055 /* Security policy must allow cipher suite hard coded into client hello */
1056 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "test_all"));
1057 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
1058
1059 EXPECT_NOT_NULL(server_config = s2n_config_new());
1060 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
1061 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
1062
1063 /* Verify s2n_connection_get_client_hello returns null if client hello not yet processed */
1064 EXPECT_NULL(s2n_connection_get_client_hello(server_conn));
1065
1066 uint8_t *ext_data = NULL;
1067 EXPECT_NOT_NULL(ext_data = malloc(server_name_extension_len));
1068 /* Verify we don't get extension and it's length when client hello is not yet processed */
1069 EXPECT_FAILURE(s2n_client_hello_get_extension_length(s2n_connection_get_client_hello(server_conn), S2N_EXTENSION_SERVER_NAME));
1070 EXPECT_FAILURE(s2n_client_hello_get_extension_by_id(s2n_connection_get_client_hello(server_conn), S2N_EXTENSION_SERVER_NAME, ext_data, server_name_extension_len));
1071 free(ext_data);
1072 ext_data = NULL;
1073
1074 /* Send the client hello message */
1075 EXPECT_EQUAL(s2n_test_send(io_pair.client, record_header, sizeof(record_header)), sizeof(record_header));
1076 EXPECT_EQUAL(s2n_test_send(io_pair.client, message_header, sizeof(message_header)), sizeof(message_header));
1077 EXPECT_EQUAL(s2n_test_send(io_pair.client, sent_client_hello, sent_client_hello_len), sent_client_hello_len);
1078
1079 /* Verify that the sent client hello message is accepted */
1080 s2n_negotiate(server_conn, &server_blocked);
1081 EXPECT_TRUE(s2n_conn_get_current_message_type(server_conn) > CLIENT_HELLO);
1082 EXPECT_EQUAL(server_conn->handshake.handshake_type, NEGOTIATED | FULL_HANDSHAKE);
1083
1084 struct s2n_client_hello *client_hello = s2n_connection_get_client_hello(server_conn);
1085
1086 /* Verify correctly identified as NOT sslv2 */
1087 EXPECT_FALSE(client_hello->sslv2);
1088
1089 /* Verify s2n_connection_get_client_hello returns the handle to the s2n_client_hello on the connection */
1090 EXPECT_EQUAL(client_hello, &server_conn->client_hello);
1091
1092 uint8_t *collected_client_hello = client_hello->raw_message.data;
1093 uint16_t collected_client_hello_len = client_hello->raw_message.size;
1094
1095 /* Verify collected client hello message length */
1096 EXPECT_EQUAL(collected_client_hello_len, sent_client_hello_len);
1097
1098 /* Verify the collected client hello has client random zero-ed out */
1099 uint8_t client_random_offset = S2N_TLS_PROTOCOL_VERSION_LEN;
1100 uint8_t expected_client_random[S2N_TLS_RANDOM_DATA_LEN] = { 0 };
1101 EXPECT_BYTEARRAY_EQUAL(collected_client_hello + client_random_offset, expected_client_random, S2N_TLS_RANDOM_DATA_LEN);
1102
1103 /* Verify the collected client hello matches what was sent except for the zero-ed client random */
1104 EXPECT_NOT_NULL(expected_client_hello = malloc(sent_client_hello_len));
1105 EXPECT_MEMCPY_SUCCESS(expected_client_hello, sent_client_hello, sent_client_hello_len);
1106 POSIX_CHECKED_MEMSET(expected_client_hello + client_random_offset, 0, S2N_TLS_RANDOM_DATA_LEN);
1107 EXPECT_BYTEARRAY_EQUAL(collected_client_hello, expected_client_hello, sent_client_hello_len);
1108
1109 /* Verify s2n_client_hello_get_raw_message_length correct */
1110 EXPECT_EQUAL(s2n_client_hello_get_raw_message_length(client_hello), sent_client_hello_len);
1111
1112 uint8_t *raw_ch_out = NULL;
1113
1114 /* Verify s2n_client_hello_get_raw_message retrieves the full message when its len <= max_len */
1115 EXPECT_TRUE(collected_client_hello_len < S2N_LARGE_RECORD_LENGTH);
1116 EXPECT_NOT_NULL(raw_ch_out = malloc(S2N_LARGE_RECORD_LENGTH));
1117 EXPECT_EQUAL(sent_client_hello_len, s2n_client_hello_get_raw_message(client_hello, raw_ch_out, S2N_LARGE_RECORD_LENGTH));
1118 EXPECT_BYTEARRAY_EQUAL(raw_ch_out, expected_client_hello, sent_client_hello_len);
1119 free(raw_ch_out);
1120 raw_ch_out = NULL;
1121
1122 /* Verify s2n_client_hello_get_raw_message retrieves truncated message when its len > max_len */
1123 EXPECT_TRUE(collected_client_hello_len > 0);
1124 uint32_t max_len = collected_client_hello_len - 1;
1125 EXPECT_NOT_NULL(raw_ch_out = malloc(max_len));
1126 EXPECT_EQUAL(max_len, s2n_client_hello_get_raw_message(client_hello, raw_ch_out, max_len));
1127 EXPECT_BYTEARRAY_EQUAL(raw_ch_out, expected_client_hello, max_len);
1128 free(raw_ch_out);
1129 raw_ch_out = NULL;
1130
1131 uint8_t expected_cs[] = { 0x00, 0x3C };
1132
1133 /* Verify collected cipher_suites size correct */
1134 EXPECT_EQUAL(client_hello->cipher_suites.size, sizeof(expected_cs));
1135
1136 /* Verify collected cipher_suites correct */
1137 EXPECT_BYTEARRAY_EQUAL(client_hello->cipher_suites.data, expected_cs, sizeof(expected_cs));
1138
1139 /* Verify s2n_client_hello_get_cipher_suites_length correct */
1140 EXPECT_EQUAL(s2n_client_hello_get_cipher_suites_length(client_hello), sizeof(expected_cs));
1141
1142 /* Verify s2n_client_hello_get_cipher_suites correct */
1143 uint8_t *cs_out = NULL;
1144
1145 /* Verify s2n_client_hello_get_cipher_suites retrieves the full cipher_suites when its len <= max_len */
1146 EXPECT_TRUE(client_hello->cipher_suites.size < S2N_LARGE_RECORD_LENGTH);
1147 EXPECT_NOT_NULL(cs_out = malloc(S2N_LARGE_RECORD_LENGTH));
1148 EXPECT_EQUAL(sizeof(expected_cs), s2n_client_hello_get_cipher_suites(client_hello, cs_out, S2N_LARGE_RECORD_LENGTH));
1149 EXPECT_BYTEARRAY_EQUAL(cs_out, client_hello->cipher_suites.data, sizeof(expected_cs));
1150 free(cs_out);
1151 cs_out = NULL;
1152
1153 /* Verify s2n_client_hello_get_cipher_suites retrieves truncated message when cipher_suites len > max_len */
1154 max_len = sizeof(expected_cs) - 1;
1155 EXPECT_TRUE(max_len > 0);
1156
1157 EXPECT_NOT_NULL(cs_out = malloc(max_len));
1158 EXPECT_EQUAL(max_len, s2n_client_hello_get_cipher_suites(client_hello, cs_out, max_len));
1159 EXPECT_BYTEARRAY_EQUAL(cs_out, client_hello->cipher_suites.data, max_len);
1160 free(cs_out);
1161 cs_out = NULL;
1162
1163 /* Verify collected extensions size correct */
1164 EXPECT_EQUAL(client_hello->extensions.raw.size, client_extensions_len);
1165
1166 /* Verify collected extensions correct */
1167 EXPECT_BYTEARRAY_EQUAL(client_hello->extensions.raw.data, client_extensions, client_extensions_len);
1168
1169 /* Verify s2n_client_hello_get_extensions_length correct */
1170 EXPECT_EQUAL(s2n_client_hello_get_extensions_length(client_hello), client_extensions_len);
1171
1172 /* Verify s2n_client_hello_get_extensions correct */
1173 uint8_t *extensions_out = NULL;
1174
1175 /* Verify s2n_client_hello_get_extensions retrieves the full cipher_suites when its len <= max_len */
1176 EXPECT_TRUE(client_hello->extensions.raw.size < S2N_LARGE_RECORD_LENGTH);
1177 EXPECT_NOT_NULL(extensions_out = malloc(S2N_LARGE_RECORD_LENGTH));
1178 EXPECT_EQUAL(client_extensions_len, s2n_client_hello_get_extensions(client_hello, extensions_out, S2N_LARGE_RECORD_LENGTH));
1179 EXPECT_BYTEARRAY_EQUAL(extensions_out, client_extensions, client_extensions_len);
1180 free(extensions_out);
1181 extensions_out = NULL;
1182
1183 /* Verify s2n_client_hello_get_extensions retrieves truncated message when cipher_suites len > max_len */
1184 max_len = client_extensions_len - 1;
1185 EXPECT_TRUE(max_len > 0);
1186
1187 EXPECT_NOT_NULL(extensions_out = malloc(max_len));
1188 EXPECT_EQUAL(max_len, s2n_client_hello_get_extensions(client_hello, extensions_out, max_len));
1189 EXPECT_BYTEARRAY_EQUAL(extensions_out, client_hello->extensions.raw.data, max_len);
1190 free(extensions_out);
1191 extensions_out = NULL;
1192
1193 /* Verify server name extension and it's length are returned correctly */
1194 EXPECT_EQUAL(s2n_client_hello_get_extension_length(client_hello, S2N_EXTENSION_SERVER_NAME), server_name_extension_len);
1195 EXPECT_NOT_NULL(ext_data = malloc(server_name_extension_len));
1196 EXPECT_EQUAL(s2n_client_hello_get_extension_by_id(client_hello, S2N_EXTENSION_SERVER_NAME, ext_data, server_name_extension_len), server_name_extension_len);
1197 EXPECT_BYTEARRAY_EQUAL(ext_data, server_name_extension, server_name_extension_len);
1198 free(ext_data);
1199 ext_data = NULL;
1200
1201 /* Verify server name extension is truncated if extension_size > max_len */
1202 EXPECT_NOT_NULL(ext_data = malloc(server_name_extension_len - 1));
1203 EXPECT_EQUAL(s2n_client_hello_get_extension_by_id(client_hello, S2N_EXTENSION_SERVER_NAME, ext_data, server_name_extension_len - 1), server_name_extension_len - 1);
1204 EXPECT_BYTEARRAY_EQUAL(ext_data, server_name_extension, server_name_extension_len - 1);
1205 free(ext_data);
1206 ext_data = NULL;
1207
1208 /* Verify get extension and it's length calls for a non-existing extension type */
1209 EXPECT_EQUAL(s2n_client_hello_get_extension_length(client_hello, S2N_EXTENSION_CERTIFICATE_TRANSPARENCY), 0);
1210 EXPECT_NOT_NULL(ext_data = malloc(server_name_extension_len));
1211 EXPECT_EQUAL(s2n_client_hello_get_extension_by_id(client_hello, S2N_EXTENSION_CERTIFICATE_TRANSPARENCY, ext_data, server_name_extension_len), 0);
1212 EXPECT_EQUAL(s2n_errno, S2N_ERR_EXTENSION_NOT_RECEIVED);
1213 free(ext_data);
1214 ext_data = NULL;
1215
1216 /* Verify server name extension exists */
1217 bool extension_exists = false;
1218 EXPECT_SUCCESS(s2n_client_hello_has_extension(client_hello, S2N_EXTENSION_SERVER_NAME, &extension_exists));
1219 EXPECT_TRUE(extension_exists);
1220
1221 /* Verify expected result for non-existing extension */
1222 extension_exists = false;
1223 EXPECT_SUCCESS(s2n_client_hello_has_extension(client_hello, S2N_EXTENSION_CERTIFICATE_TRANSPARENCY, &extension_exists));
1224 EXPECT_FALSE(extension_exists);
1225
1226 /* Verify s2n_client_hello_get_session_id is what we received in ClientHello */
1227 uint8_t expected_ch_session_id[] = { ZERO_TO_THIRTY_ONE };
1228 uint8_t ch_session_id[sizeof(expected_ch_session_id)];
1229 uint32_t ch_session_id_length = 0;
1230 EXPECT_SUCCESS(s2n_client_hello_get_session_id_length(client_hello, &ch_session_id_length));
1231 EXPECT_EQUAL(ch_session_id_length, sizeof(ch_session_id));
1232 EXPECT_SUCCESS(s2n_client_hello_get_session_id(client_hello, ch_session_id, &ch_session_id_length, sizeof(ch_session_id)));
1233 EXPECT_EQUAL(ch_session_id_length, sizeof(ch_session_id));
1234 EXPECT_BYTEARRAY_EQUAL(ch_session_id, expected_ch_session_id, sizeof(expected_ch_session_id));
1235
1236 /* Verify s2n_connection_get_session_id is different from the one we received in ClientHello, as we generated a new one in ServerHello */
1237 uint8_t conn_session_id[sizeof(expected_ch_session_id)];
1238 EXPECT_EQUAL(s2n_connection_get_session_id_length(server_conn), sizeof(conn_session_id));
1239 EXPECT_SUCCESS(s2n_connection_get_session_id(server_conn, conn_session_id, sizeof(conn_session_id)));
1240 EXPECT_BYTEARRAY_NOT_EQUAL(conn_session_id, ch_session_id, sizeof(expected_ch_session_id));
1241
1242 /* Free all handshake data */
1243 EXPECT_SUCCESS(s2n_connection_free_handshake(server_conn));
1244
1245 /* Verify free_handshake resized the s2n_client_hello.raw_message stuffer back to 0 */
1246 EXPECT_NULL(client_hello->raw_message.data);
1247 EXPECT_EQUAL(client_hello->raw_message.size, 0);
1248
1249 EXPECT_SUCCESS(s2n_shutdown(server_conn, &server_blocked));
1250
1251 /* Wipe connection */
1252 EXPECT_SUCCESS(s2n_connection_wipe(server_conn));
1253
1254 /* Verify connection_wipe resized the s2n_client_hello.raw_message stuffer back to 0 */
1255 EXPECT_NULL(client_hello->raw_message.data);
1256 EXPECT_EQUAL(client_hello->raw_message.size, 0);
1257
1258 /* Verify the s2n blobs referencing cipher_suites and extensions have cleared */
1259 EXPECT_EQUAL(client_hello->cipher_suites.size, 0);
1260 EXPECT_NULL(client_hello->cipher_suites.data);
1261 EXPECT_EQUAL(client_hello->extensions.raw.size, 0);
1262 EXPECT_NULL(client_hello->extensions.raw.data);
1263
1264 /* Verify the connection is successfully reused after connection_wipe */
1265
1266 /* Re-configure connection */
1267 server_conn->actual_protocol_version = S2N_TLS12;
1268 server_conn->server_protocol_version = S2N_TLS12;
1269 server_conn->client_protocol_version = S2N_TLS12;
1270 /* Security policy must allow cipher suite hard coded into client hello */
1271 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "test_all"));
1272 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
1273
1274 /* Recreate config */
1275 EXPECT_SUCCESS(s2n_config_free(server_config));
1276 EXPECT_NOT_NULL(server_config = s2n_config_new());
1277 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
1278 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
1279
1280 /* Re-send the client hello message */
1281 EXPECT_EQUAL(s2n_test_send(io_pair.client, record_header, sizeof(record_header)), sizeof(record_header));
1282 EXPECT_EQUAL(s2n_test_send(io_pair.client, message_header, sizeof(message_header)), sizeof(message_header));
1283 EXPECT_EQUAL(s2n_test_send(io_pair.client, sent_client_hello, sent_client_hello_len), sent_client_hello_len);
1284
1285 /* Verify that the sent client hello message is accepted */
1286 s2n_negotiate(server_conn, &server_blocked);
1287 EXPECT_TRUE(s2n_conn_get_current_message_type(server_conn) > CLIENT_HELLO);
1288 EXPECT_EQUAL(server_conn->handshake.handshake_type, NEGOTIATED | FULL_HANDSHAKE);
1289
1290 /* Verify the collected client hello on the reused connection matches the expected client hello */
1291 client_hello = s2n_connection_get_client_hello(server_conn);
1292 collected_client_hello = client_hello->raw_message.data;
1293 EXPECT_BYTEARRAY_EQUAL(collected_client_hello, expected_client_hello, sent_client_hello_len);
1294
1295 EXPECT_SUCCESS(s2n_shutdown(server_conn, &server_blocked));
1296
1297 EXPECT_SUCCESS(s2n_connection_free(server_conn));
1298
1299 EXPECT_SUCCESS(s2n_config_free(server_config));
1300 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
1301 free(expected_client_hello);
1302 free(sent_client_hello);
1303 };
1304
1305 /* Client hello api with NULL inputs */
1306 {
1307 uint32_t len = 128;
1308 uint8_t *out = NULL;
1309 EXPECT_NOT_NULL(out = malloc(len));
1310
1311 EXPECT_FAILURE(s2n_client_hello_get_raw_message_length(NULL));
1312 EXPECT_FAILURE(s2n_client_hello_get_raw_message(NULL, out, len));
1313 EXPECT_FAILURE(s2n_client_hello_get_cipher_suites_length(NULL));
1314 EXPECT_FAILURE(s2n_client_hello_get_cipher_suites(NULL, out, len));
1315 EXPECT_FAILURE(s2n_client_hello_get_extensions_length(NULL));
1316 EXPECT_FAILURE(s2n_client_hello_get_extensions(NULL, out, len));
1317 EXPECT_FAILURE(s2n_client_hello_get_extension_length(NULL, S2N_EXTENSION_SERVER_NAME));
1318 EXPECT_FAILURE(s2n_client_hello_get_extension_by_id(NULL, S2N_EXTENSION_SERVER_NAME, out, len));
1319 free(out);
1320 out = NULL;
1321
1322 bool exists = false;
1323 EXPECT_FAILURE(s2n_client_hello_has_extension(NULL, S2N_EXTENSION_SERVER_NAME, &exists));
1324 EXPECT_FALSE(exists);
1325 };
1326
1327 /* test_weird_client_hello_version() */
1328 {
1329 struct s2n_connection *server_conn = NULL;
1330 struct s2n_config *server_config = NULL;
1331 s2n_blocked_status server_blocked;
1332 uint8_t *sent_client_hello = NULL;
1333
1334 uint8_t client_extensions[] = {
1335 /* Extension type TLS_EXTENSION_SERVER_NAME */
1336 0x00,
1337 0x00,
1338 /* Extension size */
1339 0x00,
1340 0x08,
1341 /* Server names len */
1342 0x00,
1343 0x06,
1344 /* First server name type - host name */
1345 0x00,
1346 /* First server name len */
1347 0x00,
1348 0x03,
1349 /* First server name, matches sent_server_name */
1350 's',
1351 'v',
1352 'r',
1353 };
1354
1355 int client_extensions_len = sizeof(client_extensions);
1356 uint8_t client_hello_prefix[] = {
1357 /* Protocol version TLS ??? */
1358 0xFF,
1359 0xFF,
1360 /* Client random */
1361 ZERO_TO_THIRTY_ONE,
1362 /* SessionID len - 32 bytes */
1363 0x20,
1364 /* Session ID */
1365 ZERO_TO_THIRTY_ONE,
1366 /* Cipher suites len */
1367 0x00,
1368 0x02,
1369 /* Cipher suite - TLS_RSA_WITH_AES_128_CBC_SHA256 */
1370 0x00,
1371 0x3C,
1372 /* Compression methods len */
1373 0x01,
1374 /* Compression method - none */
1375 0x00,
1376 /* Extensions len */
1377 (client_extensions_len >> 8) & 0xff,
1378 (client_extensions_len & 0xff),
1379 };
1380 int client_hello_prefix_len = sizeof(client_hello_prefix);
1381 int sent_client_hello_len = client_hello_prefix_len + client_extensions_len;
1382 uint8_t message_header[] = {
1383 /* Handshake message type CLIENT HELLO */
1384 0x01,
1385 /* Body len */
1386 (sent_client_hello_len >> 16) & 0xff,
1387 (sent_client_hello_len >> 8) & 0xff,
1388 (sent_client_hello_len & 0xff),
1389 };
1390 int message_len = sizeof(message_header) + sent_client_hello_len;
1391 uint8_t record_header[] = {
1392 /* Record type HANDSHAKE */
1393 0x16,
1394 /* Protocol version TLS 1.2 */
1395 0x03,
1396 0x03,
1397 /* Message len */
1398 (message_len >> 8) & 0xff,
1399 (message_len & 0xff),
1400 };
1401
1402 EXPECT_NOT_NULL(sent_client_hello = malloc(sent_client_hello_len));
1403 EXPECT_MEMCPY_SUCCESS(sent_client_hello, client_hello_prefix, client_hello_prefix_len);
1404 EXPECT_MEMCPY_SUCCESS(sent_client_hello + client_hello_prefix_len, client_extensions, client_extensions_len);
1405
1406 /* Create nonblocking pipes */
1407 struct s2n_test_io_pair io_pair;
1408 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
1409
1410 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
1411 /* Security policy must allow cipher suite hard coded into client hello */
1412 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "test_all"));
1413 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
1414
1415 EXPECT_NOT_NULL(server_config = s2n_config_new());
1416 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
1417 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
1418
1419 /* Send the client hello message */
1420 EXPECT_EQUAL(s2n_test_send(io_pair.client, record_header, sizeof(record_header)), sizeof(record_header));
1421 EXPECT_EQUAL(s2n_test_send(io_pair.client, message_header, sizeof(message_header)), sizeof(message_header));
1422 EXPECT_EQUAL(s2n_test_send(io_pair.client, sent_client_hello, sent_client_hello_len), sent_client_hello_len);
1423
1424 /* Verify that the sent client hello message is accepted */
1425 s2n_negotiate(server_conn, &server_blocked);
1426 EXPECT_TRUE(s2n_conn_get_current_message_type(server_conn) > CLIENT_HELLO);
1427 EXPECT_EQUAL(server_conn->handshake.handshake_type, NEGOTIATED | FULL_HANDSHAKE);
1428 /* Client sent an invalid legacy protocol version. We should still have negotiate the maximum value(TLS1.2) */
1429 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS12);
1430
1431 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
1432 s2n_connection_free(server_conn);
1433 s2n_config_free(server_config);
1434 free(sent_client_hello);
1435 };
1436
1437 {
1438 struct s2n_cipher_suite *client_cipher_suites[] = {
1439 &s2n_ecdhe_ecdsa_with_aes_128_cbc_sha,
1440 };
1441
1442 struct s2n_cipher_preferences client_cipher_preferences = {
1443 .count = s2n_array_len(client_cipher_suites),
1444 .suites = client_cipher_suites,
1445 };
1446
1447 const struct s2n_signature_scheme *const client_sig_scheme_pref_list[] = {
1448 &s2n_rsa_pkcs1_sha1,
1449
1450 /* Intentionally do not send and ECDSA SignatureScheme in the Client Hello. This is malformed since the
1451 * Client's only Ciphersuite uses ECDSA, meaning that technically the Server could reject it, but there are
1452 * some clients that send this form of malformed Client Hello's in the wild. So ensure we are compatible
1453 * with them by assuming that the Client does support ECDSA, even though it's missing from the ClientHello.
1454 */
1455
1456 /* &s2n_ecdsa_sha1, */
1457 };
1458
1459 struct s2n_signature_preferences client_signature_preferences = {
1460 .count = s2n_array_len(client_sig_scheme_pref_list),
1461 .signature_schemes = client_sig_scheme_pref_list,
1462 };
1463
1464 struct s2n_security_policy client_security_policy = {
1465 .minimum_protocol_version = S2N_TLS10,
1466 .cipher_preferences = &client_cipher_preferences,
1467 .kem_preferences = &kem_preferences_null,
1468 .signature_preferences = &client_signature_preferences,
1469 .ecc_preferences = &s2n_ecc_preferences_20140601,
1470 };
1471
1472 EXPECT_TRUE(client_cipher_suites[0]->available);
1473
1474 struct s2n_cert_chain_and_key *ecdsa_cert_chain = NULL;
1475 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&ecdsa_cert_chain, S2N_ECDSA_P384_PKCS1_CERT_CHAIN, S2N_ECDSA_P384_PKCS1_KEY));
1476
1477 char dhparams_pem[S2N_MAX_TEST_PEM_SIZE];
1478 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_DHPARAMS, dhparams_pem, S2N_MAX_TEST_PEM_SIZE));
1479
1480 /* Create Configs */
1481 struct s2n_config *server_config = NULL, *client_config = NULL;
1482 EXPECT_NOT_NULL(server_config = s2n_config_new());
1483 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, ecdsa_cert_chain));
1484
1485 EXPECT_SUCCESS(s2n_config_add_dhparams(server_config, dhparams_pem));
1486 server_config->security_policy = &security_policy_20190214;
1487
1488 EXPECT_NOT_NULL(client_config = s2n_config_new());
1489 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(client_config));
1490 client_config->security_policy = &client_security_policy;
1491
1492 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(client_config, S2N_DEFAULT_TEST_CERT_CHAIN, NULL));
1493
1494 /* Create connection */
1495 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
1496 EXPECT_NOT_NULL(client_conn);
1497 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
1498
1499 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
1500 EXPECT_NOT_NULL(server_conn);
1501 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
1502
1503 /* Create nonblocking pipes */
1504 struct s2n_test_io_pair io_pair;
1505 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
1506 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
1507 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
1508
1509 /* We have to update the client's security policy after it sends the ClientHello.
1510 * The client sends all signature algorithms in its security policy, and
1511 * won't accept any signature algorithm it receives that's not in its security policy.
1512 * So we need to change the security policy between sending and receiving.
1513 */
1514 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server_conn, client_conn, SERVER_HELLO));
1515 client_config->security_policy = &security_policy_20190214;
1516 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
1517
1518 EXPECT_EQUAL(server_conn->secure->cipher_suite, &s2n_ecdhe_ecdsa_with_aes_128_cbc_sha);
1519 EXPECT_EQUAL(client_conn->secure->cipher_suite, &s2n_ecdhe_ecdsa_with_aes_128_cbc_sha);
1520 EXPECT_EQUAL(server_conn->handshake_params.server_cert_sig_scheme->sig_alg, S2N_SIGNATURE_ECDSA);
1521 EXPECT_EQUAL(server_conn->handshake_params.server_cert_sig_scheme->hash_alg, S2N_HASH_SHA1);
1522 EXPECT_EQUAL(client_conn->handshake_params.server_cert_sig_scheme->sig_alg, S2N_SIGNATURE_ECDSA);
1523 EXPECT_EQUAL(client_conn->handshake_params.server_cert_sig_scheme->hash_alg, S2N_HASH_SHA1);
1524
1525 /* Free the data */
1526 EXPECT_SUCCESS(s2n_connection_free(server_conn));
1527 EXPECT_SUCCESS(s2n_connection_free(client_conn));
1528 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
1529 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(ecdsa_cert_chain));
1530 EXPECT_SUCCESS(s2n_config_free(server_config));
1531 EXPECT_SUCCESS(s2n_config_free(client_config));
1532 };
1533
1534 /* s2n_client_hello_recv should fail when reading an SSLv2 client hello during a hello retry handshake */
1535 {
1536 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
1537 s2n_connection_ptr_free);
1538 EXPECT_NOT_NULL(server_conn);
1539
1540 /* Handshake is hello retry and TLS1.3 was negotiated */
1541 server_conn->actual_protocol_version = S2N_TLS13;
1542 EXPECT_OK(s2n_handshake_type_set_tls13_flag(server_conn, HELLO_RETRY_REQUEST));
1543
1544 /* Second client hello has version SSLv2 */
1545 server_conn->client_hello.sslv2 = true;
1546
1547 /* Mock having some data in the client hello */
1548 EXPECT_SUCCESS(s2n_stuffer_write_uint16(&server_conn->handshake.io, 100));
1549
1550 EXPECT_FAILURE_WITH_ERRNO(s2n_parse_client_hello(server_conn), S2N_ERR_BAD_MESSAGE);
1551 };
1552
1553 /* Test s2n_client_hello_parse_message
1554 *
1555 * Comparing ClientHellos produced by connection IO parsing vs
1556 * produced by s2n_client_hello_parse_message is difficult, but we can
1557 * use JA3 fingerprints as an approximation. See s2n_fingerprint_ja3_test.c
1558 */
1559 {
1560 /* 20240501 can only negotiate tls12 */
1561 const char *security_policies[] = { "20240501", "default_tls13", "test_all" };
1562
1563 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
1564 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
1565
1566 /* Test: Can parse ClientHellos sent by the s2n client */
1567 for (size_t i = 0; i < s2n_array_len(security_policies); i++) {
1568 const char *security_policy = security_policies[i];
1569
1570 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
1571 s2n_connection_ptr_free);
1572 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
1573 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client, security_policy));
1574
1575 EXPECT_SUCCESS(s2n_handshake_write_header(&client->handshake.io, TLS_CLIENT_HELLO));
1576 EXPECT_SUCCESS(s2n_client_hello_send(client));
1577 EXPECT_SUCCESS(s2n_handshake_finish_header(&client->handshake.io));
1578
1579 uint32_t raw_size = s2n_stuffer_data_available(&client->handshake.io);
1580 EXPECT_NOT_EQUAL(raw_size, 0);
1581 uint8_t *raw = s2n_stuffer_raw_read(&client->handshake.io, raw_size);
1582 EXPECT_NOT_NULL(raw);
1583
1584 DEFER_CLEANUP(struct s2n_client_hello *client_hello = NULL, s2n_client_hello_free);
1585 EXPECT_NOT_NULL(client_hello = s2n_client_hello_parse_message(raw, raw_size));
1586 EXPECT_TRUE(client_hello->alloced);
1587 };
1588
1589 /* Test: Rejects invalid ClientHellos
1590 *
1591 * This test is important to verify that no memory is leaked when parsing fails.
1592 */
1593 {
1594 struct s2n_client_hello *client_hello = NULL;
1595
1596 uint8_t wrong_message_type[50] = { 0x02, 0x00, 0x00, 1 };
1597 client_hello = s2n_client_hello_parse_message(wrong_message_type, sizeof(wrong_message_type));
1598 EXPECT_NULL(client_hello);
1599 EXPECT_EQUAL(s2n_errno, S2N_ERR_BAD_MESSAGE);
1600
1601 uint8_t wrong_message_size[50] = { 0x01, 0x00, 0x00, UINT8_MAX };
1602 client_hello = s2n_client_hello_parse_message(wrong_message_size, sizeof(wrong_message_size));
1603 EXPECT_NULL(client_hello);
1604 EXPECT_EQUAL(s2n_errno, S2N_ERR_BAD_MESSAGE);
1605
1606 uint8_t too_short[5] = { 0x01, 0x00, 0x00, 1 };
1607 client_hello = s2n_client_hello_parse_message(too_short, sizeof(too_short));
1608 EXPECT_NULL(client_hello);
1609 EXPECT_EQUAL(s2n_errno, S2N_ERR_STUFFER_OUT_OF_DATA);
1610
1611 uint8_t all_zeroes[50] = { 0x01, 0x00, 0x00, 46 };
1612 client_hello = s2n_client_hello_parse_message(all_zeroes, sizeof(all_zeroes));
1613 EXPECT_NULL(client_hello);
1614 EXPECT_EQUAL(s2n_errno, S2N_ERR_BAD_MESSAGE);
1615 };
1616
1617 /* Test: Rejects SSLv2 */
1618 {
1619 uint8_t sslv2_client_hello[] = {
1620 SSLv2_CLIENT_HELLO_HEADER,
1621 SSLv2_CLIENT_HELLO_PREFIX,
1622 SSLv2_CLIENT_HELLO_CIPHER_SUITES,
1623 SSLv2_CLIENT_HELLO_CHALLENGE,
1624 };
1625
1626 /* Try parsing variations on the complete record vs just the message.
1627 * The sslv2 record header is technically the first two bytes,
1628 * but s2n-tls usually starts parsing after the first five bytes.
1629 */
1630 for (size_t i = 0; i <= S2N_TLS_RECORD_HEADER_LENGTH; i++) {
1631 struct s2n_client_hello *client_hello = s2n_client_hello_parse_message(
1632 sslv2_client_hello + i, sizeof(sslv2_client_hello) - i);
1633 EXPECT_NULL(client_hello);
1634 EXPECT_EQUAL(s2n_errno, S2N_ERR_BAD_MESSAGE);
1635 }
1636
1637 /* Sanity check: s2n accepts the test sslv2 message via the connection */
1638 {
1639 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
1640 s2n_connection_ptr_free);
1641 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
1642 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server, "test_all"));
1643
1644 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&server->header_in,
1645 sslv2_client_hello, S2N_TLS_RECORD_HEADER_LENGTH));
1646 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&server->in,
1647 sslv2_client_hello + S2N_TLS_RECORD_HEADER_LENGTH,
1648 sizeof(sslv2_client_hello) - S2N_TLS_RECORD_HEADER_LENGTH));
1649
1650 EXPECT_FALSE(server->client_hello.sslv2);
1651 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
1652 EXPECT_OK(s2n_negotiate_until_message(server, &blocked, SERVER_HELLO));
1653 EXPECT_TRUE(server->client_hello.sslv2);
1654 EXPECT_FALSE(server->client_hello.alloced);
1655 }
1656 };
1657 };
1658
1659 /* Test s2n_client_hello_free */
1660 {
1661 /* Safety */
1662 EXPECT_FAILURE_WITH_ERRNO(s2n_client_hello_free(NULL), S2N_ERR_NULL);
1663
1664 /* Test: Accepts but ignores NULL / already freed */
1665 {
1666 struct s2n_client_hello *client_hello = NULL;
1667 for (size_t i = 0; i < 3; i++) {
1668 EXPECT_SUCCESS(s2n_client_hello_free(&client_hello));
1669 EXPECT_NULL(client_hello);
1670 }
1671 };
1672
1673 /* Test: Errors on client hello associated with a connection */
1674 {
1675 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
1676 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "test_all"));
1677 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
1678
1679 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
1680 s2n_connection_ptr_free);
1681 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
1682 s2n_connection_ptr_free);
1683 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
1684 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
1685
1686 EXPECT_SUCCESS(s2n_client_hello_send(client));
1687 EXPECT_SUCCESS(s2n_stuffer_copy(&client->handshake.io, &server->handshake.io,
1688 s2n_stuffer_data_available(&client->handshake.io)));
1689 EXPECT_SUCCESS(s2n_client_hello_recv(server));
1690
1691 struct s2n_client_hello *client_hello = s2n_connection_get_client_hello(server);
1692 EXPECT_NOT_NULL(client_hello);
1693 EXPECT_FAILURE_WITH_ERRNO(s2n_client_hello_free(&client_hello), S2N_ERR_INVALID_ARGUMENT);
1694 EXPECT_NOT_NULL(s2n_connection_get_client_hello(server));
1695 EXPECT_NOT_EQUAL(server->client_hello.raw_message.size, 0);
1696 };
1697
1698 /* Test: Frees client hello from raw message */
1699 {
1700 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
1701 s2n_connection_ptr_free);
1702 EXPECT_OK(s2n_connection_set_tls12_security_policy(client));
1703
1704 EXPECT_SUCCESS(s2n_handshake_write_header(&client->handshake.io, TLS_CLIENT_HELLO));
1705 EXPECT_SUCCESS(s2n_client_hello_send(client));
1706 EXPECT_SUCCESS(s2n_handshake_finish_header(&client->handshake.io));
1707
1708 uint32_t raw_size = s2n_stuffer_data_available(&client->handshake.io);
1709 EXPECT_NOT_EQUAL(raw_size, 0);
1710 uint8_t *raw = s2n_stuffer_raw_read(&client->handshake.io, raw_size);
1711 EXPECT_NOT_NULL(raw);
1712
1713 struct s2n_client_hello *client_hello = s2n_client_hello_parse_message(
1714 raw, raw_size);
1715 EXPECT_NOT_NULL(client_hello);
1716
1717 for (size_t i = 0; i < 3; i++) {
1718 EXPECT_SUCCESS(s2n_client_hello_free(&client_hello));
1719 EXPECT_NULL(client_hello);
1720 }
1721 };
1722 };
1723
1724 /* s2n_client_hello_get_compression_methods */
1725 {
1726 /* Safety */
1727 {
1728 uint32_t length = 0;
1729 struct s2n_client_hello client_hello = { 0 };
1730 EXPECT_FAILURE_WITH_ERRNO(s2n_client_hello_get_compression_methods_length(NULL, &length), S2N_ERR_NULL);
1731 EXPECT_FAILURE_WITH_ERRNO(s2n_client_hello_get_compression_methods_length(&client_hello, NULL), S2N_ERR_NULL);
1732
1733 uint8_t list = 0;
1734 uint32_t list_length = 0;
1735 uint32_t out_length = 0;
1736 EXPECT_FAILURE_WITH_ERRNO(s2n_client_hello_get_compression_methods(NULL, &list, list_length, &out_length), S2N_ERR_NULL);
1737 EXPECT_FAILURE_WITH_ERRNO(s2n_client_hello_get_compression_methods(&client_hello, NULL, list_length, &out_length), S2N_ERR_NULL);
1738 EXPECT_FAILURE_WITH_ERRNO(s2n_client_hello_get_compression_methods(&client_hello, &list, list_length, NULL), S2N_ERR_NULL);
1739
1740 /* User did not provide a large enough buffer to write the compression methods */
1741 uint8_t data[] = { 1, 2, 3, 4, 5 };
1742 EXPECT_SUCCESS(s2n_blob_init(&client_hello.compression_methods, data, sizeof(data)));
1743 EXPECT_FAILURE_WITH_ERRNO(s2n_client_hello_get_compression_methods(&client_hello, &list, list_length, &out_length),
1744 S2N_ERR_INSUFFICIENT_MEM_SIZE);
1745 };
1746
1747 /* Retrieves the compression methods list */
1748 {
1749 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
1750 s2n_connection_ptr_free);
1751 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
1752 s2n_connection_ptr_free);
1753 EXPECT_NOT_NULL(server_conn);
1754 EXPECT_NOT_NULL(client_conn);
1755
1756 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(),
1757 s2n_config_ptr_free);
1758 EXPECT_NOT_NULL(config);
1759
1760 EXPECT_OK(s2n_config_set_tls12_security_policy(config));
1761 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
1762 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
1763 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
1764
1765 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
1766 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
1767 s2n_stuffer_data_available(&client_conn->handshake.io)));
1768 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
1769
1770 struct s2n_client_hello *client_hello = s2n_connection_get_client_hello(server_conn);
1771 EXPECT_NOT_NULL(client_hello);
1772
1773 uint32_t length = 0;
1774 EXPECT_SUCCESS(s2n_client_hello_get_compression_methods_length(client_hello, &length));
1775 EXPECT_EQUAL(length, 1);
1776 uint8_t list = 0;
1777 uint32_t out_length = 0;
1778 EXPECT_SUCCESS(s2n_client_hello_get_compression_methods(client_hello, &list, sizeof(list), &out_length));
1779 EXPECT_EQUAL(out_length, 1);
1780 };
1781
1782 /* Retrieves compression methods list longer than one byte */
1783 {
1784 /* Compression methods were deprecated in TLS13 and s2n has never
1785 * supported them. However, it is conceivable that a client could send
1786 * us a list that contains more than the "null" byte. Therefore, we construct
1787 * a fake Client Hello that contains a longer list of compression methods
1788 * for testing.
1789 */
1790 uint8_t client_hello_message[] = {
1791 /* Protocol version TLS 1.2 */
1792 0x03, 0x03,
1793 /* Client random */
1794 ZERO_TO_THIRTY_ONE,
1795 /* SessionID len - 32 bytes */
1796 0x20,
1797 /* Session ID */
1798 ZERO_TO_THIRTY_ONE,
1799 /* Cipher suites len */
1800 0x00, 0x02,
1801 /* Cipher suite - TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */
1802 0xC0, 0x2F,
1803 COMPRESSION_METHODS_LEN,
1804 COMPRESSION_METHODS,
1805 /* Extensions len */
1806 0x00, 0x00
1807 };
1808
1809 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
1810 s2n_connection_ptr_free);
1811 EXPECT_NOT_NULL(server_conn);
1812
1813 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(),
1814 s2n_config_ptr_free);
1815 EXPECT_NOT_NULL(config);
1816
1817 EXPECT_OK(s2n_config_set_tls12_security_policy(config));
1818 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
1819 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
1820
1821 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&server_conn->handshake.io, client_hello_message, sizeof(client_hello_message)));
1822 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
1823
1824 struct s2n_client_hello *client_hello = s2n_connection_get_client_hello(server_conn);
1825 EXPECT_NOT_NULL(client_hello);
1826
1827 uint32_t length = 0;
1828 EXPECT_SUCCESS(s2n_client_hello_get_compression_methods_length(client_hello, &length));
1829 EXPECT_EQUAL(length, COMPRESSION_METHODS_LEN);
1830 uint8_t list[5] = { 0 };
1831 uint32_t out_length = 0;
1832 EXPECT_SUCCESS(s2n_client_hello_get_compression_methods(client_hello, list, sizeof(list), &out_length));
1833 EXPECT_EQUAL(out_length, COMPRESSION_METHODS_LEN);
1834
1835 uint8_t compression_data[] = { COMPRESSION_METHODS };
1836 EXPECT_BYTEARRAY_EQUAL(list, compression_data, out_length);
1837 }
1838 };
1839
1840 /* s2n_client_hello_get_legacy_protocol_version */
1841 {
1842 /* Safety */
1843 {
1844 uint8_t out = 0;
1845 struct s2n_client_hello client_hello = { 0 };
1846 EXPECT_FAILURE_WITH_ERRNO(s2n_client_hello_get_legacy_protocol_version(NULL, &out), S2N_ERR_NULL);
1847 EXPECT_FAILURE_WITH_ERRNO(s2n_client_hello_get_legacy_protocol_version(&client_hello, NULL), S2N_ERR_NULL);
1848 };
1849
1850 /* Retrieves the Client Hello protocol version */
1851 {
1852 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
1853 s2n_connection_ptr_free);
1854 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
1855 s2n_connection_ptr_free);
1856 EXPECT_NOT_NULL(server_conn);
1857 EXPECT_NOT_NULL(client_conn);
1858
1859 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(),
1860 s2n_config_ptr_free);
1861 EXPECT_NOT_NULL(config);
1862
1863 EXPECT_OK(s2n_config_set_tls12_security_policy(config));
1864 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
1865 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
1866 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
1867
1868 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
1869 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
1870 s2n_stuffer_data_available(&client_conn->handshake.io)));
1871 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
1872
1873 struct s2n_client_hello *client_hello = s2n_connection_get_client_hello(server_conn);
1874 EXPECT_NOT_NULL(client_hello);
1875
1876 uint8_t version = 0;
1877 EXPECT_SUCCESS(s2n_client_hello_get_legacy_protocol_version(client_hello, &version));
1878 EXPECT_EQUAL(version, S2N_TLS12);
1879 };
1880 };
1881
1882 /* s2n_client_hello_get_legacy_record_version */
1883 {
1884 /* Safety */
1885 {
1886 uint8_t out = 0;
1887 struct s2n_client_hello client_hello = { 0 };
1888
1889 EXPECT_FAILURE_WITH_ERRNO(s2n_client_hello_get_legacy_record_version(NULL, &out), S2N_ERR_NULL);
1890 EXPECT_FAILURE_WITH_ERRNO(s2n_client_hello_get_legacy_record_version(&client_hello, NULL), S2N_ERR_NULL);
1891 };
1892
1893 /* Retrieves record version */
1894 {
1895 uint8_t out = 0;
1896 struct s2n_client_hello client_hello = { 0 };
1897 client_hello.legacy_record_version = S2N_TLS12;
1898 client_hello.record_version_recorded = 1;
1899 EXPECT_SUCCESS(s2n_client_hello_get_legacy_record_version(&client_hello, &out));
1900 EXPECT_EQUAL(out, S2N_TLS12);
1901 };
1902 };
1903
1904 /* s2n_client_hello_get_server_name() */
1905 {
1906 /* Safety */
1907 {
1908 struct s2n_client_hello ch = { 0 };
1909 uint16_t length = 0;
1910 EXPECT_FAILURE_WITH_ERRNO(s2n_client_hello_get_server_name_length(NULL, &length), S2N_ERR_NULL);
1911 EXPECT_FAILURE_WITH_ERRNO(s2n_client_hello_get_server_name_length(&ch, NULL), S2N_ERR_NULL);
1912
1913 uint8_t buffer = 0;
1914 uint16_t out_length = 0;
1915 EXPECT_FAILURE_WITH_ERRNO(s2n_client_hello_get_server_name(NULL, &buffer, 0, &out_length), S2N_ERR_NULL);
1916 EXPECT_FAILURE_WITH_ERRNO(s2n_client_hello_get_server_name(&ch, NULL, 0, &out_length), S2N_ERR_NULL);
1917 EXPECT_FAILURE_WITH_ERRNO(s2n_client_hello_get_server_name(&ch, &buffer, 0, NULL), S2N_ERR_NULL);
1918 };
1919
1920 /* Retrieves the first entry in the server_name extension */
1921 {
1922 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
1923 s2n_connection_ptr_free);
1924 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
1925 s2n_connection_ptr_free);
1926 EXPECT_NOT_NULL(server_conn);
1927 EXPECT_NOT_NULL(client_conn);
1928
1929 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(),
1930 s2n_config_ptr_free);
1931 EXPECT_NOT_NULL(config);
1932
1933 EXPECT_OK(s2n_config_set_tls12_security_policy(config));
1934 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
1935 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
1936 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
1937
1938 const char *test_server_name = "test server name!";
1939 EXPECT_SUCCESS(s2n_set_server_name(client_conn, test_server_name));
1940
1941 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
1942 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
1943 s2n_stuffer_data_available(&client_conn->handshake.io)));
1944 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
1945
1946 struct s2n_client_hello *client_hello = s2n_connection_get_client_hello(server_conn);
1947 EXPECT_NOT_NULL(client_hello);
1948
1949 uint16_t length = 0;
1950 EXPECT_SUCCESS(s2n_client_hello_get_server_name_length(client_hello, &length));
1951 EXPECT_EQUAL(strlen(test_server_name), length);
1952 uint8_t buffer[20] = { 0 };
1953 uint16_t out_length = 0;
1954 EXPECT_SUCCESS(s2n_client_hello_get_server_name(client_hello, buffer, sizeof(buffer), &out_length));
1955 EXPECT_EQUAL(length, out_length);
1956
1957 EXPECT_BYTEARRAY_EQUAL(buffer, test_server_name, out_length);
1958
1959 /* Check error occurs if buffer is too small to hold server name */
1960 uint8_t small_buf[2] = { 0 };
1961 out_length = 0;
1962 EXPECT_FAILURE_WITH_ERRNO(s2n_client_hello_get_server_name(client_hello, small_buf, sizeof(small_buf), &out_length), S2N_ERR_SAFETY);
1963 };
1964 };
1965
1966 /* Test: large Client Hellos */
1967 {
1968 const uint16_t cipher_suites_max_length = (1 << 16) - 2;
1969 const uint16_t num_of_cipher_suites_to_drop = 150;
1970
1971 /**
1972 * S2N-TLS automatically includes TLS_EMPTY_RENEGOTIATION_INFO_SCSV in TLS 1.2 ClientHello,
1973 * so we subtract 1 from the maximum number of cipher suites to reserve space for it.
1974 */
1975 const uint16_t max_cipher_suite_count = (cipher_suites_max_length / S2N_TLS_CIPHER_SUITE_LEN) - 1;
1976
1977 /* Drop 150 cipher suites from max, so that the total handshake message length won't exceed 64KB */
1978 const uint16_t reduced_cipher_suite_count = max_cipher_suite_count - num_of_cipher_suites_to_drop;
1979
1980 uint16_t cipher_suites_counts[] = { reduced_cipher_suite_count, max_cipher_suite_count };
1981
1982 for (size_t i = 0; i < s2n_array_len(cipher_suites_counts); i++) {
1983 DEFER_CLEANUP(struct s2n_config *server_config = s2n_config_new(), s2n_config_ptr_free);
1984 EXPECT_NOT_NULL(server_config);
1985 EXPECT_OK(s2n_config_set_tls12_security_policy(server_config));
1986 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
1987
1988 /* We need to generate a large Client Hello.
1989 * We do this by manipulating the number of cipher suites,
1990 * which is the easiest way to make Client Hello large.
1991 */
1992 uint16_t cipher_suites_count = cipher_suites_counts[i];
1993 struct s2n_cipher_suite *test_cipher_suites[UINT16_MAX] = { 0 };
1994 for (size_t j = 0; j < cipher_suites_count; j++) {
1995 test_cipher_suites[j] = &s2n_rsa_with_aes_128_gcm_sha256;
1996 }
1997 const struct s2n_cipher_preferences test_cipher_suites_preferences = {
1998 .count = cipher_suites_count,
1999 .suites = test_cipher_suites,
2000 };
2001 const struct s2n_security_policy *default_policy = NULL;
2002 /* 20240501 is a policy that can only negotiate tls12 */
2003 EXPECT_SUCCESS(s2n_find_security_policy_from_version("20240501", &default_policy));
2004 struct s2n_security_policy test_security_policy = *default_policy;
2005 test_security_policy.cipher_preferences = &test_cipher_suites_preferences;
2006
2007 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
2008 s2n_connection_ptr_free);
2009 EXPECT_NOT_NULL(client);
2010 EXPECT_SUCCESS(s2n_connection_set_blinding(client, S2N_SELF_SERVICE_BLINDING));
2011 client->security_policy_override = &test_security_policy;
2012
2013 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
2014 s2n_connection_ptr_free);
2015 EXPECT_NOT_NULL(server);
2016 EXPECT_SUCCESS(s2n_connection_set_config(server, server_config));
2017 EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING));
2018 server->security_policy_override = &test_security_policy;
2019
2020 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
2021 EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
2022 EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair));
2023
2024 /**
2025 * Write Client Hello into io_pair.server_in.
2026 * The server_in buffer contains the Client Hello message plus a 5-byte record header.
2027 */
2028 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
2029 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate(client, &blocked), S2N_ERR_IO_BLOCKED);
2030
2031 /* Add one extra cipher suite length to account for TLS_EMPTY_RENEGOTIATION_INFO_SCSV */
2032 uint16_t cipher_suites_length = (cipher_suites_count + 1) * S2N_TLS_CIPHER_SUITE_LEN;
2033
2034 if (cipher_suites_length < cipher_suites_max_length) {
2035 /**
2036 * The Client Hello message size should be less than S2N_MAXIMUM_HANDSHAKE_MESSAGE_LENGTH, even with
2037 * the five byte record header.
2038 */
2039 EXPECT_TRUE(s2n_stuffer_data_available(&io_pair.server_in) < S2N_MAXIMUM_HANDSHAKE_MESSAGE_LENGTH);
2040 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server, client, SERVER_HELLO));
2041 } else {
2042 /**
2043 * When using maximum cipher suites, the Client Hello message size exceeds
2044 * S2N_MAXIMUM_HANDSHAKE_MESSAGE_LENGTH by more than five byte.
2045 *
2046 * Hence, if server_in's available data is greater than S2N_MAXIMUM_HANDSHAKE_MESSAGE_LENGTH,
2047 * then the Client Hello itself exceeds the maximum allowed size, even after accounting for the record header.
2048 */
2049 EXPECT_TRUE(s2n_stuffer_data_available(&io_pair.server_in) > S2N_MAXIMUM_HANDSHAKE_MESSAGE_LENGTH);
2050 EXPECT_ERROR_WITH_ERRNO(s2n_negotiate_test_server_and_client_until_message(server, client, SERVER_HELLO), S2N_ERR_BAD_MESSAGE);
2051 }
2052 }
2053 }
2054
2055 /* Test s2n_client_hello_get_random */
2056 {
2057 /* Safety */
2058 {
2059 uint8_t out[S2N_TLS_RANDOM_DATA_LEN] = { 0 };
2060 struct s2n_client_hello client_hello = { 0 };
2061
2062 EXPECT_FAILURE_WITH_ERRNO(
2063 s2n_client_hello_get_random(NULL, out, sizeof(out)),
2064 S2N_ERR_NULL);
2065 EXPECT_FAILURE_WITH_ERRNO(
2066 s2n_client_hello_get_random(&client_hello, NULL, sizeof(out)),
2067 S2N_ERR_NULL);
2068
2069 /* Buffer size must be large enough to hold the full client random */
2070 uint8_t small_buffer[S2N_TLS_RANDOM_DATA_LEN - 1] = { 0 };
2071 EXPECT_FAILURE_WITH_ERRNO(
2072 s2n_client_hello_get_random(&client_hello, small_buffer, sizeof(small_buffer)),
2073 S2N_ERR_INSUFFICIENT_MEM_SIZE);
2074 };
2075
2076 /* Retrieves the client random and zeroes it from the raw message */
2077 {
2078 /* Construct a minimal ClientHello with a custom random value */
2079 uint8_t custom_random[S2N_TLS_RANDOM_DATA_LEN] = { ZERO_TO_THIRTY_ONE };
2080
2081 /* clang-format off */
2082 uint8_t raw_client_hello[] = {
2083 /* message type */
2084 TLS_CLIENT_HELLO,
2085 /* message size */
2086 0x00, 0x00, 43,
2087 /* protocol version - TLS1.2 */
2088 0x03, 0x03,
2089 /* random - custom value */
2090 ZERO_TO_THIRTY_ONE,
2091 /* session id */
2092 0x00,
2093 /* cipher suites */
2094 0x00, 0x02,
2095 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
2096 /* legacy compression methods */
2097 0x01, 0x00,
2098 /* extensions - empty */
2099 0x00, 0x00,
2100 };
2101 /* clang-format on */
2102
2103 DEFER_CLEANUP(struct s2n_client_hello *client_hello = NULL, s2n_client_hello_free);
2104 client_hello = s2n_client_hello_parse_message(raw_client_hello, sizeof(raw_client_hello));
2105 EXPECT_NOT_NULL(client_hello);
2106
2107 /* Exact-size buffer: retrieve the client random */
2108 uint8_t retrieved_random[S2N_TLS_RANDOM_DATA_LEN] = { 0 };
2109 EXPECT_SUCCESS(s2n_client_hello_get_random(
2110 client_hello, retrieved_random, S2N_TLS_RANDOM_DATA_LEN));
2111 EXPECT_BYTEARRAY_EQUAL(retrieved_random, custom_random, S2N_TLS_RANDOM_DATA_LEN);
2112
2113 /* Buffer size > 32 bytes must also succeed and only write the first 32 bytes */
2114 {
2115 uint8_t large_buffer[S2N_TLS_RANDOM_DATA_LEN + 10] = { 0 };
2116 EXPECT_SUCCESS(s2n_client_hello_get_random(
2117 client_hello, large_buffer, sizeof(large_buffer)));
2118
2119 /* First 32 bytes match the client random */
2120 EXPECT_BYTEARRAY_EQUAL(large_buffer, custom_random, S2N_TLS_RANDOM_DATA_LEN);
2121
2122 /* Tail remains untouched */
2123 uint8_t zero_tail[10] = { 0 };
2124 EXPECT_BYTEARRAY_EQUAL(
2125 large_buffer + S2N_TLS_RANDOM_DATA_LEN,
2126 zero_tail,
2127 sizeof(zero_tail));
2128 }
2129
2130 /* The raw message should have the random zeroed out after retrieval */
2131 uint8_t *raw_message = client_hello->raw_message.data;
2132 uint8_t client_random_offset = S2N_TLS_PROTOCOL_VERSION_LEN;
2133 uint8_t zeroed_random[S2N_TLS_RANDOM_DATA_LEN] = { 0 };
2134 EXPECT_BYTEARRAY_EQUAL(
2135 raw_message + client_random_offset,
2136 zeroed_random,
2137 S2N_TLS_RANDOM_DATA_LEN);
2138 };
2139 };
2140
2141 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
2142 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(ecdsa_chain_and_key));
2143 END_TEST();
2144 }