Debian Codegraph @

crt/s2n/tests/unit/s2n_self_talk_session_id_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 <stdint.h>
18 #include <sys/wait.h>
19 #include <unistd.h>
20
21 #include "api/s2n.h"
22 #include "s2n_test.h"
23 #include "testlib/s2n_testlib.h"
24 #include "tls/s2n_connection.h"
25 #include "tls/s2n_handshake.h"
26 #include "utils/s2n_bitmap.h"
27
28 #define MAX_KEY_LEN 32
29 #define MAX_VAL_LEN 255
30
31 static const char SESSION_ID[] = "0123456789abcdef0123456789abcdef";
32 static const char MSG[] = "Test";
33
34 struct session_cache_entry {
35 uint8_t key[MAX_KEY_LEN];
36 uint8_t key_len;
37 uint8_t value[MAX_VAL_LEN];
38 uint8_t value_len;
39 uint8_t lock;
40 };
41
42 struct session_cache_entry session_cache[256];
43
44 int cache_store_callback(struct s2n_connection *conn, void *ctx, uint64_t ttl, const void *key, uint64_t key_size, const void *value, uint64_t value_size)
45 {
46 struct session_cache_entry *cache = ctx;
47
48 if (key_size == 0 || key_size > MAX_KEY_LEN) {
49 return -1;
50 }
51 if (value_size == 0 || value_size > MAX_VAL_LEN) {
52 return -1;
53 }
54
55 uint8_t idx = ((const uint8_t *) key)[0];
56
57 EXPECT_MEMCPY_SUCCESS(cache[idx].key, key, key_size);
58 EXPECT_MEMCPY_SUCCESS(cache[idx].value, value, value_size);
59
60 cache[idx].key_len = key_size;
61 cache[idx].value_len = value_size;
62
63 return 0;
64 }
65
66 int cache_retrieve_callback(struct s2n_connection *conn, void *ctx, const void *key, uint64_t key_size, void *value, uint64_t *value_size)
67 {
68 struct session_cache_entry *cache = ctx;
69
70 if (key_size == 0 || key_size > MAX_KEY_LEN) {
71 return -1;
72 }
73
74 uint8_t idx = ((const uint8_t *) key)[0];
75
76 if (cache[idx].lock) {
77 /* here we mock a remote connection/event blocking the handshake
78 * state machine, until lock is free
79 */
80 cache[idx].lock = 0;
81 return S2N_CALLBACK_BLOCKED;
82 }
83
84 if (cache[idx].key_len != key_size) {
85 return -1;
86 }
87
88 if (memcmp(cache[idx].key, key, key_size)) {
89 return -1;
90 }
91
92 if (*value_size < cache[idx].value_len) {
93 return -1;
94 }
95
96 *value_size = cache[idx].value_len;
97 EXPECT_MEMCPY_SUCCESS(value, cache[idx].value, cache[idx].value_len);
98
99 return 0;
100 }
101
102 int cache_delete_callback(struct s2n_connection *conn, void *ctx, const void *key, uint64_t key_size)
103 {
104 struct session_cache_entry *cache = ctx;
105
106 if (key_size == 0 || key_size > MAX_KEY_LEN) {
107 return -1;
108 }
109
110 uint8_t idx = ((const uint8_t *) key)[0];
111
112 if (cache[idx].key_len == 0) {
113 return 0;
114 }
115
116 if (cache[idx].key_len != key_size) {
117 return -1;
118 }
119
120 if (memcmp(cache[idx].key, key, key_size)) {
121 return -1;
122 }
123
124 cache[idx].key_len = 0;
125 cache[idx].value_len = 0;
126
127 return 0;
128 }
129
130 /* init session cache lock field, which is used to mock a remote
131 * connection/event block
132 */
133 static void initialize_cache()
134 {
135 for (int i = 0; i < 256; i++) {
136 session_cache[i].lock = 1;
137 }
138 }
139
140 void mock_client(struct s2n_test_io_pair *io_pair)
141 {
142 size_t serialized_session_state_length = 0;
143 uint8_t serialized_session_state[256] = { 0 };
144
145 struct s2n_connection *conn = NULL;
146 struct s2n_config *config = NULL;
147 s2n_blocked_status blocked;
148 int result = 0;
149
150 /* Give the server a chance to listen */
151 sleep(1);
152
153 /* Initial handshake */
154 conn = s2n_connection_new(S2N_CLIENT);
155 config = s2n_config_new();
156 s2n_config_set_cipher_preferences(config, "20240501");
157 s2n_config_disable_x509_verification(config);
158 s2n_connection_set_config(conn, config);
159
160 s2n_connection_set_io_pair(conn, io_pair);
161
162 /* Set the session id to ensure we're able to fallback to full handshake if session is not in server cache */
163 EXPECT_MEMCPY_SUCCESS(conn->session_id, SESSION_ID, S2N_TLS_SESSION_ID_MAX_LEN);
164 conn->session_id_len = S2N_TLS_SESSION_ID_MAX_LEN;
165
166 if (s2n_negotiate(conn, &blocked) != 0) {
167 result = 1;
168 }
169
170 /* Make sure we did a full handshake */
171 if (!IS_FULL_HANDSHAKE(conn)) {
172 result = 2;
173 }
174
175 /* Save session state from the connection */
176 memset(serialized_session_state, 0, sizeof(serialized_session_state));
177 serialized_session_state_length = s2n_connection_get_session_length(conn);
178 if (serialized_session_state_length > sizeof(serialized_session_state)) {
179 result = 3;
180 }
181
182 /* Send very low session buffer size and see that you can get an error */
183 if (s2n_connection_get_session(conn, serialized_session_state, 1) == 0) {
184 result = 4;
185 }
186
187 if (s2n_errno != S2N_ERR_SERIALIZED_SESSION_STATE_TOO_LONG) {
188 result = 5;
189 }
190
191 if ((size_t) s2n_connection_get_session(conn, serialized_session_state, serialized_session_state_length) != serialized_session_state_length) {
192 result = 6;
193 }
194
195 /* server would choose a session ID for client */
196 if (memcmp(conn->session_id, SESSION_ID, S2N_TLS_SESSION_ID_MAX_LEN) == 0) {
197 result = 7;
198 }
199
200 if (s2n_send(conn, MSG, sizeof(MSG), &blocked) != sizeof(MSG)) {
201 result = 8;
202 }
203
204 int shutdown_rc = -1;
205 while (shutdown_rc != 0) {
206 shutdown_rc = s2n_shutdown(conn, &blocked);
207 }
208
209 s2n_connection_free(conn);
210
211 /* Give the server a chance to avoid sigpipe */
212 sleep(1);
213
214 /* Session resumption */
215 conn = s2n_connection_new(S2N_CLIENT);
216 s2n_connection_set_io_pair(conn, io_pair);
217 EXPECT_OK(s2n_connection_set_tls12_security_policy(conn));
218
219 /* Set session state on client connection */
220 if (s2n_connection_set_session(conn, serialized_session_state, serialized_session_state_length) < 0) {
221 result = 9;
222 }
223
224 if (s2n_negotiate(conn, &blocked) != 0) {
225 result = 10;
226 }
227
228 /* Make sure we did a abbreviated handshake */
229 if (!IS_RESUMPTION_HANDSHAKE(conn)) {
230 result = 11;
231 }
232
233 if (s2n_send(conn, MSG, sizeof(MSG), &blocked) != sizeof(MSG)) {
234 result = 12;
235 }
236
237 shutdown_rc = -1;
238 while (shutdown_rc != 0) {
239 shutdown_rc = s2n_shutdown(conn, &blocked);
240 }
241
242 s2n_connection_free(conn);
243
244 /* Give the server a chance to avoid sigpipe */
245 sleep(1);
246
247 /* Session resumption with bad session state */
248 conn = s2n_connection_new(S2N_CLIENT);
249 EXPECT_OK(s2n_connection_set_tls12_security_policy(conn));
250 EXPECT_SUCCESS(s2n_connection_set_blinding(conn, S2N_SELF_SERVICE_BLINDING));
251 s2n_connection_set_io_pair(conn, io_pair);
252
253 /* Change the format of the session state and check we cannot deserialize it */
254 serialized_session_state[0] = 3;
255 if (s2n_connection_set_session(conn, serialized_session_state, serialized_session_state_length) == 0) {
256 result = 13;
257 }
258
259 if (s2n_errno != S2N_ERR_INVALID_SERIALIZED_SESSION_STATE) {
260 result = 14;
261 }
262
263 serialized_session_state[0] = 0;
264 /* Change the protocol version (36th byte) in session state */
265 if (serialized_session_state_length < 36) {
266 result = 15;
267 }
268
269 serialized_session_state[35] = 30;
270 if (s2n_connection_set_session(conn, serialized_session_state, serialized_session_state_length) < 0) {
271 result = 16;
272 }
273
274 if (s2n_negotiate(conn, &blocked) == 0) {
275 result = 17;
276 }
277
278 if (s2n_errno != S2N_ERR_BAD_MESSAGE) {
279 result = 18;
280 }
281
282 s2n_connection_free(conn);
283 s2n_config_free(config);
284
285 /* Give the server a chance to avoid sigpipe */
286 sleep(1);
287
288 s2n_io_pair_close_one_end(io_pair, S2N_CLIENT);
289 exit(result);
290 }
291
292 int main(int argc, char **argv)
293 {
294 struct s2n_connection *conn = NULL;
295 struct s2n_config *config = NULL;
296 s2n_blocked_status blocked;
297 int status = 0;
298 pid_t pid = 0;
299 char *cert_chain_pem = NULL;
300 char *private_key_pem = NULL;
301 struct s2n_cert_chain_and_key *chain_and_key = NULL;
302 char buffer[256];
303 int bytes_read = 0;
304 int shutdown_rc = -1;
305 uint64_t now = 0;
306 uint8_t session_id_from_server[MAX_KEY_LEN];
307 uint8_t session_id_from_client[MAX_KEY_LEN];
308
309 /* aes keys. Used for session ticket/session data encryption. Taken from test vectors in https://tools.ietf.org/html/rfc5869 */
310 uint8_t ticket_key_name[16] = "2018.07.26.15\0";
311 uint8_t ticket_key[32] = { 0x19, 0xef, 0x24, 0xa3, 0x2c, 0x71, 0x7b, 0x16, 0x7f, 0x33,
312 0xa9, 0x1d, 0x6f, 0x64, 0x8b, 0xdf, 0x96, 0x59, 0x67, 0x76,
313 0xaf, 0xdb, 0x63, 0x77, 0xac, 0x43, 0x4c, 0x1c, 0x29, 0x3c,
314 0xcb, 0x04 };
315
316 BEGIN_TEST();
317
318 /* Create a pipe */
319 struct s2n_test_io_pair io_pair;
320 EXPECT_SUCCESS(s2n_io_pair_init(&io_pair));
321
322 /* Create a child process */
323 pid = fork();
324 if (pid == 0) {
325 /* This is the client process, close the server end of the pipe */
326 EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_SERVER));
327
328 /* Write the fragmented hello message */
329 mock_client(&io_pair);
330 }
331
332 EXPECT_NOT_NULL(cert_chain_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
333 EXPECT_NOT_NULL(private_key_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
334
335 /* This is the server process, close the client end of the pipe */
336 EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_CLIENT));
337
338 /* Initial handshake */
339 {
340 /* Initialize the cache so the client and server start off on the same page */
341 initialize_cache();
342 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
343 EXPECT_NOT_NULL(config = s2n_config_new());
344 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "20240501"));
345
346 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_CERT_CHAIN, cert_chain_pem, S2N_MAX_TEST_PEM_SIZE));
347 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_PRIVATE_KEY, private_key_pem, S2N_MAX_TEST_PEM_SIZE));
348 EXPECT_NOT_NULL(chain_and_key = s2n_cert_chain_and_key_new());
349 EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(chain_and_key, cert_chain_pem, private_key_pem));
350 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
351
352 EXPECT_SUCCESS(s2n_config_set_cache_store_callback(config, cache_store_callback, session_cache));
353 EXPECT_SUCCESS(s2n_config_set_cache_retrieve_callback(config, cache_retrieve_callback, session_cache));
354 EXPECT_SUCCESS(s2n_config_set_cache_delete_callback(config, cache_delete_callback, session_cache));
355
356 /* Although we disable session ticket, as long as session cache
357 * callbacks are binded, session ticket key storage would be initialized
358 */
359 POSIX_GUARD(s2n_config_set_session_cache_onoff(config, 1));
360 POSIX_GUARD(config->wall_clock(config->sys_clock_ctx, &now));
361 EXPECT_SUCCESS(s2n_config_add_ticket_crypto_key(config, ticket_key_name, strlen((char *) ticket_key_name), ticket_key, sizeof(ticket_key), now / ONE_SEC_IN_NANOS));
362
363 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
364
365 /* Set up the connection to read from the fd */
366 EXPECT_SUCCESS(s2n_connection_set_io_pair(conn, &io_pair));
367
368 /* Negotiate the handshake. */
369 int r = s2n_negotiate(conn, &blocked);
370 /* first time it always blocks the handshake, as we mock a remote
371 * connection/event from the lock
372 */
373 EXPECT_EQUAL(r, -1);
374 EXPECT_EQUAL(s2n_error_get_type(s2n_errno), S2N_ERR_T_BLOCKED);
375 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_APPLICATION_INPUT);
376 EXPECT_SUCCESS(s2n_negotiate(conn, &blocked));
377
378 /* Make sure the get_session_id and get_session_id_length APIs are
379 * working as expected */
380 EXPECT_EQUAL(s2n_connection_get_session_id_length(conn), MAX_KEY_LEN);
381 EXPECT_EQUAL(s2n_connection_get_session_id(conn, session_id_from_server, MAX_KEY_LEN), s2n_connection_get_session_id_length(conn));
382
383 /* Make sure we did a full TLS1.2 handshake */
384 EXPECT_TRUE(IS_FULL_HANDSHAKE(conn));
385 EXPECT_EQUAL(conn->actual_protocol_version, S2N_TLS12);
386
387 /* Ensure the message was delivered */
388 EXPECT_SUCCESS(bytes_read = s2n_recv(conn, buffer, sizeof(buffer), &blocked));
389 EXPECT_EQUAL(bytes_read, sizeof(MSG));
390 EXPECT_EQUAL(memcmp(buffer, MSG, sizeof(MSG)), 0);
391
392 /* Shutdown handshake */
393 do {
394 shutdown_rc = s2n_shutdown(conn, &blocked);
395 EXPECT_TRUE(shutdown_rc == 0 || (errno == EAGAIN && blocked));
396 } while (shutdown_rc != 0);
397
398 /* Clean up */
399 EXPECT_SUCCESS(s2n_connection_free(conn));
400 };
401
402 /* Session resumption */
403 {
404 initialize_cache();
405 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
406 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
407
408 /* Set up the connection to read from the fd */
409 EXPECT_SUCCESS(s2n_connection_set_io_pair(conn, &io_pair));
410
411 /* Negotiate the handshake. */
412 int r = s2n_negotiate(conn, &blocked);
413 /* first time it always blocks the handshake, as we mock a remote
414 * connection/event from the lock
415 */
416 EXPECT_EQUAL(r, -1);
417 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_APPLICATION_INPUT);
418 EXPECT_SUCCESS(s2n_negotiate(conn, &blocked));
419
420 /* Make sure the get_session_id and get_session_id_length APIs are
421 * working as expected */
422 EXPECT_EQUAL(s2n_connection_get_session_id_length(conn), MAX_KEY_LEN);
423 EXPECT_EQUAL(s2n_connection_get_session_id(conn, session_id_from_client, MAX_KEY_LEN), s2n_connection_get_session_id_length(conn));
424 EXPECT_EQUAL(0, memcmp(session_id_from_client, session_id_from_server, MAX_KEY_LEN));
425
426 /* Make sure we did a abbreviated handshake */
427 EXPECT_TRUE(IS_RESUMPTION_HANDSHAKE(conn));
428
429 /* Ensure the message was delivered */
430 memset(buffer, 0, sizeof(buffer));
431 EXPECT_SUCCESS(bytes_read = s2n_recv(conn, buffer, sizeof(buffer), &blocked));
432 EXPECT_EQUAL(bytes_read, sizeof(MSG));
433 EXPECT_EQUAL(memcmp(buffer, MSG, sizeof(MSG)), 0);
434
435 /* Shutdown handshake */
436 do {
437 shutdown_rc = s2n_shutdown(conn, &blocked);
438 EXPECT_TRUE(shutdown_rc == 0 || (errno == EAGAIN && blocked));
439 } while (shutdown_rc != 0);
440
441 /* Clean up */
442 EXPECT_SUCCESS(s2n_connection_free(conn));
443 };
444
445 /* Session resumption with bad session state on client side */
446 {
447 initialize_cache();
448 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
449 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
450
451 /* Set up the connection to read from the fd */
452 EXPECT_SUCCESS(s2n_connection_set_io_pair(conn, &io_pair));
453
454 int r = s2n_negotiate(conn, &blocked);
455 EXPECT_EQUAL(r, -1);
456 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_APPLICATION_INPUT);
457 /* Verify we failed to negotiate */
458 EXPECT_FAILURE(s2n_negotiate(conn, &blocked));
459
460 /* Clean up */
461 EXPECT_SUCCESS(s2n_connection_free(conn));
462 };
463
464 /* Close the pipes */
465 EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_SERVER));
466
467 /* Session caching with a server that does not support EMS */
468 {
469 initialize_cache();
470 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
471 EXPECT_NOT_NULL(client_conn);
472 s2n_config_disable_x509_verification(config);
473 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
474
475 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
476 EXPECT_NOT_NULL(server_conn);
477 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
478
479 /* Create nonblocking pipes */
480 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
481 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
482
483 /* Negotiate until server has read the Client Hello message but hasn't written the server hello message */
484 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
485 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, SERVER_HELLO));
486
487 /* s2n servers by default support EMS. We turn it off by manually setting ems_negotiated to false
488 * and removing the EMS extension from our received extensions. */
489 server_conn->ems_negotiated = false;
490 s2n_extension_type_id ems_ext_id = 0;
491 EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_EMS, &ems_ext_id));
492 S2N_CBIT_CLR(server_conn->extension_requests_received, ems_ext_id);
493
494 /* Connection is successful and EMS is not negotiated */
495 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
496 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS12);
497 EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS12);
498 EXPECT_FALSE(server_conn->ems_negotiated);
499 EXPECT_FALSE(client_conn->ems_negotiated);
500 EXPECT_TRUE(IS_FULL_HANDSHAKE(server_conn));
501 EXPECT_TRUE(IS_FULL_HANDSHAKE(client_conn));
502
503 size_t tls12_session_ticket_len = s2n_connection_get_session_length(client_conn);
504 uint8_t tls12_session_ticket[S2N_TLS12_SESSION_SIZE] = { 0 };
505 EXPECT_SUCCESS(s2n_connection_get_session(client_conn, tls12_session_ticket, tls12_session_ticket_len));
506
507 /* Wipe connections and set up new handshake */
508 EXPECT_SUCCESS(s2n_connection_wipe(server_conn));
509 EXPECT_SUCCESS(s2n_connection_wipe(client_conn));
510 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
511 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
512 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
513 EXPECT_SUCCESS(s2n_connection_set_session(client_conn, tls12_session_ticket, tls12_session_ticket_len));
514
515 /* Server will block the first time cache is accessed */
516 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, client_conn), S2N_ERR_ASYNC_BLOCKED);
517
518 /* Resumed connection is successful and EMS is not negotiated */
519 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
520 EXPECT_EQUAL(S2N_CBIT_TEST(server_conn->extension_requests_received, ems_ext_id), 0);
521 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS12);
522 EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS12);
523 EXPECT_FALSE(server_conn->ems_negotiated);
524 EXPECT_FALSE(client_conn->ems_negotiated);
525 EXPECT_FALSE(IS_FULL_HANDSHAKE(server_conn));
526 EXPECT_FALSE(IS_FULL_HANDSHAKE(client_conn));
527
528 EXPECT_SUCCESS(s2n_connection_free(client_conn));
529 EXPECT_SUCCESS(s2n_connection_free(server_conn));
530 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
531 };
532
533 /**
534 *= https://www.rfc-editor.org/rfc/rfc7627#section-5.3
535 *= type=test
536 *# If the original session used the "extended_master_secret"
537 *# extension but the new ClientHello does not contain it, the server
538 *# MUST abort the abbreviated handshake.
539 **/
540 {
541 initialize_cache();
542 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
543 EXPECT_NOT_NULL(client_conn);
544 s2n_config_disable_x509_verification(config);
545 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
546
547 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
548 EXPECT_NOT_NULL(server_conn);
549 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
550
551 /* Create nonblocking pipes */
552 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
553 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
554
555 /* Connection is successful and EMS is negotiated */
556 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
557 EXPECT_TRUE(server_conn->ems_negotiated);
558 EXPECT_TRUE(client_conn->ems_negotiated);
559
560 size_t tls12_session_ticket_len = s2n_connection_get_session_length(client_conn);
561 uint8_t tls12_session_ticket[S2N_TLS12_SESSION_SIZE] = { 0 };
562 EXPECT_SUCCESS(s2n_connection_get_session(client_conn, tls12_session_ticket, tls12_session_ticket_len));
563
564 /* Wipe connections and set up new handshake */
565 EXPECT_SUCCESS(s2n_connection_wipe(server_conn));
566 EXPECT_SUCCESS(s2n_connection_wipe(client_conn));
567 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
568 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
569 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
570 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
571
572 /* Force the client to not send the EMS extension */
573 EXPECT_SUCCESS(s2n_connection_set_session(client_conn, tls12_session_ticket, tls12_session_ticket_len));
574 client_conn->ems_negotiated = false;
575
576 /* Server will block the first time cache is accessed */
577 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, client_conn), S2N_ERR_ASYNC_BLOCKED);
578
579 /* Server did not receive the EMS extension from client */
580 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, client_conn), S2N_ERR_MISSING_EXTENSION);
581
582 EXPECT_SUCCESS(s2n_connection_free(client_conn));
583 EXPECT_SUCCESS(s2n_connection_free(server_conn));
584 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
585 };
586
587 /**
588 *= https://www.rfc-editor.org/rfc/rfc7627#section-5.3
589 *= type=test
590 *# If the original session did not use the "extended_master_secret"
591 *# extension but the new ClientHello contains the extension, then the
592 *# server MUST NOT perform the abbreviated handshake. Instead, it
593 *# SHOULD continue with a full handshake (as described in
594 *# Section 5.2) to negotiate a new session.
595 **/
596 {
597 initialize_cache();
598 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
599 EXPECT_NOT_NULL(client_conn);
600 s2n_config_disable_x509_verification(config);
601 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
602
603 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
604 EXPECT_NOT_NULL(server_conn);
605 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
606
607 /* Create nonblocking pipes */
608 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
609 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
610
611 /* Negotiate until server has read the Client Hello message but hasn't written the Server Hello message */
612 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
613 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, SERVER_HELLO));
614
615 /* s2n servers by default support EMS. We turn it off by manually setting ems_negotiated to false
616 * and removing the EMS extension from our received extensions. */
617 server_conn->ems_negotiated = false;
618 s2n_extension_type_id ems_ext_id = 0;
619 EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_EMS, &ems_ext_id));
620 S2N_CBIT_CLR(server_conn->extension_requests_received, ems_ext_id);
621
622 /* Connection is successful and EMS is not negotiated */
623 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
624 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS12);
625 EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS12);
626 EXPECT_FALSE(server_conn->ems_negotiated);
627 EXPECT_FALSE(client_conn->ems_negotiated);
628 EXPECT_TRUE(IS_FULL_HANDSHAKE(server_conn));
629 EXPECT_TRUE(IS_FULL_HANDSHAKE(client_conn));
630
631 size_t tls12_session_ticket_len = s2n_connection_get_session_length(client_conn);
632 uint8_t tls12_session_ticket[S2N_TLS12_SESSION_SIZE] = { 0 };
633 EXPECT_SUCCESS(s2n_connection_get_session(client_conn, tls12_session_ticket, tls12_session_ticket_len));
634
635 /* Wipe connections and set up new handshake */
636 EXPECT_SUCCESS(s2n_connection_wipe(server_conn));
637 EXPECT_SUCCESS(s2n_connection_wipe(client_conn));
638 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
639 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
640 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
641
642 /* Force the client to send the EMS extension even though the original session did not negotiate EMS */
643 EXPECT_SUCCESS(s2n_connection_set_session(client_conn, tls12_session_ticket, tls12_session_ticket_len));
644 client_conn->ems_negotiated = true;
645
646 /* Server will block the first time cache is accessed */
647 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, client_conn), S2N_ERR_ASYNC_BLOCKED);
648
649 /* Fallback to full handshake */
650 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
651 EXPECT_EQUAL(S2N_CBIT_TEST(server_conn->extension_requests_received, ems_ext_id), 1);
652 EXPECT_TRUE(server_conn->ems_negotiated);
653 EXPECT_TRUE(client_conn->ems_negotiated);
654 EXPECT_TRUE(IS_FULL_HANDSHAKE(server_conn));
655 EXPECT_TRUE(IS_FULL_HANDSHAKE(client_conn));
656
657 EXPECT_SUCCESS(s2n_connection_free(client_conn));
658 EXPECT_SUCCESS(s2n_connection_free(server_conn));
659 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
660 };
661
662 /* Clean up */
663 EXPECT_SUCCESS(s2n_config_free(config));
664 EXPECT_EQUAL(waitpid(-1, &status, 0), pid);
665 EXPECT_EQUAL(status, 0);
666
667 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
668 free(cert_chain_pem);
669 free(private_key_pem);
670
671 END_TEST();
672 }