Debian Codegraph @

crt/s2n/tests/unit/s2n_client_hello_recv_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 <errno.h>
17 #include <fcntl.h>
18 #include <stdint.h>
19 #include <unistd.h>
20
21 #include "api/s2n.h"
22 #include "s2n_test.h"
23 #include "testlib/s2n_sslv2_client_hello.h"
24 #include "testlib/s2n_testlib.h"
25 #include "tls/s2n_client_hello.h"
26 #include "tls/s2n_connection.h"
27 #include "tls/s2n_quic_support.h"
28 #include "tls/s2n_tls.h"
29 #include "tls/s2n_tls13.h"
30 #include "utils/s2n_blob.h"
31 #include "utils/s2n_safety.h"
32
33 int main(int argc, char **argv)
34 {
35 struct s2n_connection *server_conn = NULL;
36 struct s2n_connection *client_conn = NULL;
37 struct s2n_stuffer *hello_stuffer = NULL;
38 struct s2n_config *tls12_config = NULL;
39 struct s2n_config *tls13_config = NULL;
40 struct s2n_cert_chain_and_key *chain_and_key = NULL;
41 struct s2n_cert_chain_and_key *tls13_chain_and_key = NULL;
42 char *cert_chain = NULL;
43 char *tls13_cert_chain = NULL;
44 char *private_key = NULL;
45 char *tls13_private_key = NULL;
46
47 BEGIN_TEST();
48 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
49
50 EXPECT_NOT_NULL(cert_chain = malloc(S2N_MAX_TEST_PEM_SIZE));
51 EXPECT_NOT_NULL(private_key = malloc(S2N_MAX_TEST_PEM_SIZE));
52 EXPECT_NOT_NULL(tls12_config = s2n_config_new());
53 EXPECT_NOT_NULL(chain_and_key = s2n_cert_chain_and_key_new());
54 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(tls12_config, "test_all_tls12"));
55
56 EXPECT_NOT_NULL(tls13_cert_chain = malloc(S2N_MAX_TEST_PEM_SIZE));
57 EXPECT_NOT_NULL(tls13_private_key = malloc(S2N_MAX_TEST_PEM_SIZE));
58 EXPECT_NOT_NULL(tls13_config = s2n_config_new());
59 EXPECT_NOT_NULL(tls13_chain_and_key = s2n_cert_chain_and_key_new());
60 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(tls13_config, "test_all"));
61
62 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_CERT_CHAIN, cert_chain, S2N_MAX_TEST_PEM_SIZE));
63 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_PRIVATE_KEY, private_key, S2N_MAX_TEST_PEM_SIZE));
64 EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(chain_and_key, cert_chain, private_key));
65 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(tls12_config, chain_and_key));
66
67 EXPECT_SUCCESS(s2n_read_test_pem(S2N_ECDSA_P384_PKCS1_CERT_CHAIN, tls13_cert_chain, S2N_MAX_TEST_PEM_SIZE));
68 EXPECT_SUCCESS(s2n_read_test_pem(S2N_ECDSA_P384_PKCS1_KEY, tls13_private_key, S2N_MAX_TEST_PEM_SIZE));
69 EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(tls13_chain_and_key, tls13_cert_chain, tls13_private_key));
70 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(tls13_config, tls13_chain_and_key));
71
72 /* These tests verify the logic behind the setting of these three connection fields:
73 server_protocol_version, client_protocol_version, and actual_protocol version. */
74
75 /* Test we can successfully receive an sslv2 client hello and set a
76 * tls12 connection */
77 {
78 /* "enable" tls13, to test under default s2n-tls behavior */
79 EXPECT_SUCCESS(s2n_reset_tls13_in_test());
80
81 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
82 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, tls13_config));
83
84 /* Record version and protocol version are in the header for SSLv2 */
85 server_conn->client_hello.sslv2 = true;
86 server_conn->client_hello.legacy_version = S2N_TLS12;
87
88 uint8_t sslv2_client_hello[] = {
89 SSLv2_CLIENT_HELLO_PREFIX,
90 SSLv2_CLIENT_HELLO_CIPHER_SUITES,
91 SSLv2_CLIENT_HELLO_CHALLENGE,
92 };
93
94 struct s2n_blob client_hello = {
95 .data = sslv2_client_hello,
96 .size = sizeof(sslv2_client_hello),
97 .allocated = 0,
98 .growable = 0
99 };
100 EXPECT_SUCCESS(s2n_stuffer_write(&server_conn->handshake.io, &client_hello));
101 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
102
103 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS12);
104 EXPECT_EQUAL(server_conn->client_protocol_version, S2N_TLS12);
105 EXPECT_EQUAL(server_conn->client_hello.sslv2, true);
106 EXPECT_EQUAL(server_conn->client_hello.legacy_version, S2N_TLS12);
107 EXPECT_EQUAL(server_conn->client_hello.callback_invoked, 1);
108 EXPECT_EQUAL(s2n_connection_get_client_hello_version(server_conn), S2N_SSLv2);
109
110 s2n_connection_free(server_conn);
111
112 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
113 }
114
115 /* Test that a tls12 client legacy version and tls12 server version
116 will successfully set a tls12 connection, since tls13 is not enabled. */
117 {
118 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
119 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
120 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, tls12_config));
121 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, tls12_config));
122
123 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
124 EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS12);
125 EXPECT_EQUAL(client_conn->client_protocol_version, S2N_TLS12);
126 EXPECT_EQUAL(client_conn->client_hello.legacy_version, S2N_TLS12);
127
128 EXPECT_SUCCESS(s2n_stuffer_write(&server_conn->handshake.io, &client_conn->handshake.io.blob));
129 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
130
131 EXPECT_EQUAL(server_conn->server_protocol_version, S2N_TLS12);
132 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS12);
133 EXPECT_EQUAL(server_conn->client_protocol_version, S2N_TLS12);
134
135 s2n_connection_free(server_conn);
136 s2n_connection_free(client_conn);
137 };
138
139 /* Test that a tls12 client legacy version and tls12 server version
140 will successfully set a tls12 connection, even when tls13 is enabled. */
141 {
142 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
143 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
144 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
145 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, tls12_config));
146 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, tls12_config));
147
148 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
149 EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS12);
150 EXPECT_EQUAL(client_conn->client_protocol_version, S2N_TLS12);
151 EXPECT_EQUAL(client_conn->client_hello.legacy_version, S2N_TLS12);
152
153 EXPECT_SUCCESS(s2n_stuffer_write(&server_conn->handshake.io, &client_conn->handshake.io.blob));
154 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
155
156 EXPECT_EQUAL(server_conn->server_protocol_version, S2N_TLS12);
157 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS12);
158 EXPECT_EQUAL(server_conn->client_protocol_version, S2N_TLS12);
159 EXPECT_EQUAL(server_conn->client_hello.legacy_version, S2N_TLS12);
160
161 s2n_connection_free(server_conn);
162 s2n_connection_free(client_conn);
163 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
164 };
165
166 /* Test that a tls11 client legacy version and tls12 server version
167 will successfully set a tls11 connection. */
168 for (uint8_t i = 0; i < 2; i++) {
169 if (i == 1) {
170 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
171 }
172
173 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
174 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
175 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, tls12_config));
176 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, tls12_config));
177
178 client_conn->client_protocol_version = S2N_TLS11;
179
180 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
181 EXPECT_EQUAL(client_conn->client_hello.legacy_version, S2N_TLS11);
182
183 EXPECT_SUCCESS(s2n_stuffer_write(&server_conn->handshake.io, &client_conn->handshake.io.blob));
184 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
185
186 EXPECT_EQUAL(server_conn->server_protocol_version, S2N_TLS12);
187 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS11);
188 EXPECT_EQUAL(server_conn->client_protocol_version, S2N_TLS11);
189 EXPECT_EQUAL(server_conn->client_hello.legacy_version, S2N_TLS11);
190
191 s2n_connection_free(server_conn);
192 s2n_connection_free(client_conn);
193
194 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
195 }
196 /* Test that a tls12 client and tls13 server will successfully
197 set a tls12 connection. */
198 {
199 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
200 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, tls12_config));
201
202 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
203
204 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
205 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, tls13_config));
206
207 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
208 EXPECT_EQUAL(client_conn->client_protocol_version, S2N_TLS12);
209 EXPECT_EQUAL(client_conn->client_hello.legacy_version, S2N_TLS12);
210
211 EXPECT_SUCCESS(s2n_stuffer_write(&server_conn->handshake.io, &client_conn->handshake.io.blob));
212 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
213
214 EXPECT_EQUAL(server_conn->server_protocol_version, S2N_TLS13);
215 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS12);
216 EXPECT_EQUAL(server_conn->client_protocol_version, S2N_TLS12);
217 EXPECT_EQUAL(server_conn->client_hello.legacy_version, S2N_TLS12);
218
219 s2n_connection_free(server_conn);
220 s2n_connection_free(client_conn);
221 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
222 };
223 /* Test that an erroneous client legacy version and tls13 server version
224 will still successfully set a tls13 connection, when real client version is tls13. */
225 {
226 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
227 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
228 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
229 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, tls13_config));
230 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, tls12_config));
231
232 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, "default_tls13"));
233 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "default_tls13"));
234
235 hello_stuffer = &client_conn->handshake.io;
236
237 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
238
239 /* Overwrite the client legacy version so that it reads tls13 (incorrectly) */
240 uint8_t incorrect_protocol_version[S2N_TLS_PROTOCOL_VERSION_LEN];
241 incorrect_protocol_version[0] = S2N_TLS13 / 10;
242 incorrect_protocol_version[1] = S2N_TLS13 % 10;
243 EXPECT_SUCCESS(s2n_stuffer_rewrite(hello_stuffer));
244 EXPECT_SUCCESS(s2n_stuffer_write_bytes(hello_stuffer, incorrect_protocol_version, S2N_TLS_PROTOCOL_VERSION_LEN));
245
246 EXPECT_SUCCESS(s2n_stuffer_write(&server_conn->handshake.io, &hello_stuffer->blob));
247 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
248
249 EXPECT_EQUAL(client_conn->actual_protocol_version, s2n_get_highest_fully_supported_tls_version());
250 EXPECT_EQUAL(client_conn->client_protocol_version, s2n_get_highest_fully_supported_tls_version());
251 EXPECT_EQUAL(client_conn->client_hello.legacy_version, S2N_TLS12);
252
253 EXPECT_EQUAL(server_conn->server_protocol_version, S2N_TLS13);
254 EXPECT_EQUAL(server_conn->actual_protocol_version, s2n_get_highest_fully_supported_tls_version());
255 EXPECT_EQUAL(server_conn->client_protocol_version, s2n_get_highest_fully_supported_tls_version());
256 EXPECT_EQUAL(server_conn->client_hello.legacy_version, S2N_TLS13);
257
258 s2n_connection_free(server_conn);
259 s2n_connection_free(client_conn);
260 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
261 };
262
263 /* Test that a tls12 client legacy version and tls13 server version
264 will still successfully set a tls13 connection, if possible. */
265 {
266 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
267 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
268 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
269 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, tls13_config));
270 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, tls12_config));
271
272 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, "default_tls13"));
273 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "default_tls13"));
274
275 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
276
277 EXPECT_SUCCESS(s2n_stuffer_write(&server_conn->handshake.io, &client_conn->handshake.io.blob));
278 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
279
280 EXPECT_EQUAL(server_conn->server_protocol_version, S2N_TLS13);
281 EXPECT_EQUAL(server_conn->actual_protocol_version, s2n_get_highest_fully_supported_tls_version());
282 EXPECT_EQUAL(server_conn->client_protocol_version, s2n_get_highest_fully_supported_tls_version());
283 EXPECT_EQUAL(server_conn->client_hello.legacy_version, S2N_TLS12);
284
285 s2n_connection_free(server_conn);
286 s2n_connection_free(client_conn);
287 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
288 };
289 /* Test that an erroneous(tls13) client legacy version and tls13 server version
290 will still successfully set a tls12 connection, if tls12 is the true client version. */
291 {
292 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
293 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, tls12_config));
294 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
295 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
296 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, tls12_config));
297
298 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "default_tls13"));
299
300 hello_stuffer = &client_conn->handshake.io;
301
302 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
303
304 /* Overwrite the client legacy version so that it reads tls13 (incorrectly) */
305 uint8_t incorrect_protocol_version[S2N_TLS_PROTOCOL_VERSION_LEN];
306 incorrect_protocol_version[0] = S2N_TLS13 / 10;
307 incorrect_protocol_version[1] = S2N_TLS13 % 10;
308 EXPECT_SUCCESS(s2n_stuffer_rewrite(hello_stuffer));
309 EXPECT_SUCCESS(s2n_stuffer_write_bytes(hello_stuffer, incorrect_protocol_version, S2N_TLS_PROTOCOL_VERSION_LEN));
310
311 EXPECT_SUCCESS(s2n_stuffer_write(&server_conn->handshake.io, &hello_stuffer->blob));
312 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
313
314 EXPECT_EQUAL(server_conn->server_protocol_version, s2n_get_highest_fully_supported_tls_version());
315 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS12);
316 EXPECT_EQUAL(server_conn->client_protocol_version, S2N_TLS12);
317 EXPECT_EQUAL(server_conn->client_hello.legacy_version, S2N_TLS13);
318
319 s2n_connection_free(server_conn);
320 s2n_connection_free(client_conn);
321 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
322 };
323
324 /* s2n receiving a client hello will error when parsing an empty cipher suite */
325 {
326 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
327 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
328
329 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, tls13_config));
330 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, "default_tls13"));
331
332 hello_stuffer = &client_conn->handshake.io;
333
334 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
335
336 uint8_t empty_cipher_suite[S2N_TLS_CIPHER_SUITE_LEN] = { 0 };
337
338 /* Move write_cursor to cipher_suite position */
339 EXPECT_SUCCESS(s2n_stuffer_rewrite(hello_stuffer));
340 EXPECT_SUCCESS(s2n_stuffer_skip_write(hello_stuffer, S2N_TLS_PROTOCOL_VERSION_LEN + S2N_TLS_RANDOM_DATA_LEN + 1));
341 EXPECT_SUCCESS(s2n_stuffer_write_bytes(hello_stuffer, empty_cipher_suite, S2N_TLS_CIPHER_SUITE_LEN));
342
343 EXPECT_SUCCESS(s2n_stuffer_write(&server_conn->handshake.io, &hello_stuffer->blob));
344 EXPECT_FAILURE_WITH_ERRNO(s2n_client_hello_recv(server_conn), S2N_ERR_BAD_MESSAGE);
345
346 s2n_connection_free(server_conn);
347 s2n_connection_free(client_conn);
348 };
349
350 /* s2n receiving a sslv2 client hello will error when parsing an empty cipher suite */
351 {
352 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
353 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, tls12_config));
354
355 /* Record version and protocol version are in the header for SSLv2 */
356 server_conn->client_hello.sslv2 = true;
357 server_conn->client_hello.legacy_version = S2N_TLS12;
358
359 /* Writing a sslv2 client hello with a length 0 cipher suite list */
360 uint8_t sslv2_client_hello[] = {
361 0x00,
362 0x00,
363 0x00,
364 0x00,
365 0x00,
366 0x20,
367 SSLv2_CLIENT_HELLO_CIPHER_SUITES,
368 SSLv2_CLIENT_HELLO_CHALLENGE,
369 };
370
371 struct s2n_blob client_hello = {
372 .data = sslv2_client_hello,
373 .size = sizeof(sslv2_client_hello),
374 .allocated = 0,
375 .growable = 0
376 };
377 EXPECT_SUCCESS(s2n_stuffer_write(&server_conn->handshake.io, &client_hello));
378 EXPECT_FAILURE_WITH_ERRNO(s2n_client_hello_recv(server_conn), S2N_ERR_BAD_MESSAGE);
379
380 s2n_connection_free(server_conn);
381 };
382
383 /* Test that S2N will accept a ClientHello with legacy_session_id set when running with QUIC.
384 * Since this requirement is a SHOULD, we're accepting it for non-compliant endpoints.
385 * https://tools.ietf.org/html/draft-ietf-quic-tls-32#section-8.4*/
386 if (s2n_is_tls13_fully_supported()) {
387 EXPECT_SUCCESS(s2n_reset_tls13_in_test());
388
389 const size_t test_session_id_len = 10;
390
391 struct s2n_config *quic_config = NULL;
392 EXPECT_NOT_NULL(quic_config = s2n_config_new());
393 EXPECT_SUCCESS(s2n_config_enable_quic(quic_config));
394 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(quic_config, tls13_chain_and_key));
395 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(quic_config, "default_tls13"));
396
397 /* Succeeds without a session id */
398 {
399 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
400 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, quic_config));
401
402 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
403 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, quic_config));
404
405 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
406 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
407 s2n_stuffer_data_available(&client_conn->handshake.io)));
408 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
409
410 s2n_connection_free(client_conn);
411 s2n_connection_free(server_conn);
412 };
413
414 /* Also, succeeds with a session id */
415 {
416 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
417 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, quic_config));
418
419 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
420 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, quic_config));
421
422 /* Directly set session id, which is not set by default when using QUIC */
423 client_conn->session_id_len = test_session_id_len;
424
425 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
426 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
427 s2n_stuffer_data_available(&client_conn->handshake.io)));
428 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
429
430 s2n_connection_free(client_conn);
431 s2n_connection_free(server_conn);
432 };
433
434 s2n_config_free(quic_config);
435 }
436
437 /* Test that the server will not choose a signature algorithm or certificate if using PSKs */
438 {
439 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
440 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
441 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, "default_tls13"));
442 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "default_tls13"));
443 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, tls13_config));
444 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, tls13_config));
445
446 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
447 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
448 s2n_stuffer_data_available(&client_conn->handshake.io)));
449
450 struct s2n_psk chosen_psk = { 0 };
451 chosen_psk.hmac_alg = S2N_HMAC_SHA256;
452 server_conn->psk_params.chosen_psk = &chosen_psk;
453 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
454
455 EXPECT_EQUAL(server_conn->handshake_params.server_cert_sig_scheme->iana_value, 0);
456 EXPECT_NULL(server_conn->handshake_params.our_chain_and_key);
457
458 EXPECT_SUCCESS(s2n_connection_free(client_conn));
459 EXPECT_SUCCESS(s2n_connection_free(server_conn));
460 };
461
462 /* Test that curve selection will be NIST P-256 when tls12 client does not send curve extension.
463 *
464 *= https://www.rfc-editor.org/rfc/rfc4492#section-4
465 *= type=test
466 *# A client that proposes ECC cipher suites may choose not to include these extensions.
467 *# In this case, the server is free to choose any one of the elliptic curves or point formats listed in Section 5.
468 */
469 {
470 S2N_BLOB_FROM_HEX(tls12_client_hello_no_curves,
471 /* clang-format off */
472 "030307de81928fe1" "7cba77904c2798da" "2521a76b013a16e4" "21ade32208f658d4" "327d000048000400"
473 "05000a0016002f00" "3300350039003c00" "3d0067006b009c00" "9d009e009fc009c0" "0ac011c012c013c0"
474 "14c023c024c027c0" "28c02bc02cc02fc0" "30cca8cca9ccaaff" "04ff0800ff010000" "30000d0016001404"
475 "0105010601030104" "0305030603030302" "010203000b000201" "00fe01000c000a00" "17000d0013000100"
476 "0a" /* clang-format on */);
477
478 /* The above code is generated the following code,
479 disabling s2n_client_supported_groups_extension
480 from client_hello_extensions (s2n_extension_type_lists.c)
481 and exporting the resulting client_conn->handshake.io.blob
482
483 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
484 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, tls12_config));
485 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
486 EXPECT_EQUAL(client_conn->client_protocol_version, S2N_TLS12);
487 EXPECT_EQUAL(client_conn->client_hello_version, S2N_TLS12);
488 s2n_connection_free(client_conn);
489 */
490
491 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
492
493 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
494 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, tls13_config));
495
496 EXPECT_SUCCESS(s2n_stuffer_write(&server_conn->handshake.io, &tls12_client_hello_no_curves));
497 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
498
499 /* ensure negotiated_curve == secp256r1 for maximum client compatibility */
500 EXPECT_EQUAL(server_conn->kex_params.server_ecc_evp_params.negotiated_curve, &s2n_ecc_curve_secp256r1);
501
502 EXPECT_EQUAL(server_conn->server_protocol_version, S2N_TLS13);
503 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS12);
504 EXPECT_EQUAL(server_conn->client_protocol_version, S2N_TLS12);
505 EXPECT_EQUAL(server_conn->client_hello.legacy_version, S2N_TLS12);
506
507 s2n_connection_free(server_conn);
508 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
509 };
510
511 /* Test: Parse fragmented sslv2 client hello.
512 *
513 * Even if the sslv2 client hello is sent in one packet, there is no requirement
514 * that our first call to conn->recv returns the whole message. sslv2 uses separate
515 * record parsing code, so we need to ensure that those paths can handle partial reads.
516 */
517 {
518 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
519 s2n_connection_ptr_free);
520 EXPECT_SUCCESS(s2n_connection_set_config(server, tls12_config));
521
522 struct s2n_stuffer server_in = { 0 };
523 uint8_t sslv2_client_hello_bytes[] = {
524 SSLv2_CLIENT_HELLO_HEADER,
525 SSLv2_CLIENT_HELLO_PREFIX,
526 SSLv2_CLIENT_HELLO_CIPHER_SUITES,
527 SSLv2_CLIENT_HELLO_CHALLENGE,
528 };
529 EXPECT_SUCCESS(s2n_blob_init(&server_in.blob,
530 sslv2_client_hello_bytes, sizeof(sslv2_client_hello_bytes)));
531 EXPECT_SUCCESS(s2n_connection_set_recv_io_stuffer(&server_in, server));
532
533 /* Read message one byte at a time */
534 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
535 for (size_t i = 0; i < sizeof(sslv2_client_hello_bytes); i++) {
536 EXPECT_ERROR_WITH_ERRNO(
537 s2n_negotiate_until_message(server, &blocked, SERVER_HELLO),
538 S2N_ERR_IO_BLOCKED);
539 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ);
540 EXPECT_SUCCESS(s2n_stuffer_skip_write(&server_in, 1));
541 }
542
543 /* Successfully read the full message */
544 EXPECT_OK(s2n_negotiate_until_message(server, &blocked, SERVER_HELLO));
545 EXPECT_EQUAL(server->client_protocol_version, S2N_TLS12);
546 EXPECT_EQUAL(server->client_hello.legacy_version, S2N_TLS12);
547 EXPECT_TRUE(server->client_hello.sslv2);
548 EXPECT_EQUAL(s2n_connection_get_client_hello_version(server), S2N_SSLv2);
549 };
550
551 s2n_config_free(tls12_config);
552 s2n_config_free(tls13_config);
553 s2n_cert_chain_and_key_free(chain_and_key);
554 free(cert_chain);
555 free(private_key);
556 s2n_cert_chain_and_key_free(tls13_chain_and_key);
557 free(tls13_cert_chain);
558 free(tls13_private_key);
559 END_TEST();
560 }