Debian Codegraph @

crt/s2n/tests/unit/s2n_tls12_handshake_test.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include "api/s2n.h"
17 #include "s2n_test.h"
18 #include "testlib/s2n_testlib.h"
19 #include "tls/s2n_connection.h"
20 #include "tls/s2n_handshake.h"
21 #include "utils/s2n_safety.h"
22
23 /* Just to get access to the static functions / variables we need to test */
24 #include "tls/s2n_handshake_io.c"
25
26 static message_type_t invalid_handshake[S2N_MAX_HANDSHAKE_LENGTH] = { 0 };
27
28 static int expected_handler_called;
29 static int unexpected_handler_called;
30
31 static int s2n_test_handler(struct s2n_connection *conn)
32 {
33 unexpected_handler_called = 1;
34 return 0;
35 }
36
37 static int s2n_test_expected_handler(struct s2n_connection *conn)
38 {
39 expected_handler_called = 1;
40 return 0;
41 }
42
43 static int s2n_setup_handler_to_expect(message_type_t expected, uint8_t direction)
44 {
45 for (int i = 0; i < s2n_array_len(state_machine); i++) {
46 state_machine[i].handler[0] = s2n_test_handler;
47 state_machine[i].handler[1] = s2n_test_handler;
48 }
49
50 state_machine[expected].handler[direction] = s2n_test_expected_handler;
51
52 expected_handler_called = 0;
53 unexpected_handler_called = 0;
54
55 return 0;
56 }
57
58 static int s2n_test_write_header(struct s2n_stuffer *output, uint8_t record_type, uint8_t message_type)
59 {
60 POSIX_GUARD(s2n_stuffer_write_uint8(output, record_type));
61
62 /* TLS1.2 protocol version */
63 POSIX_GUARD(s2n_stuffer_write_uint8(output, 3));
64 POSIX_GUARD(s2n_stuffer_write_uint8(output, 3));
65
66 if (record_type == TLS_HANDSHAKE) {
67 /* Total message size */
68 POSIX_GUARD(s2n_stuffer_write_uint16(output, 4));
69
70 POSIX_GUARD(s2n_stuffer_write_uint8(output, message_type));
71
72 /* Handshake message data size */
73 POSIX_GUARD(s2n_stuffer_write_uint24(output, 0));
74 return 0;
75 }
76
77 if (record_type == TLS_CHANGE_CIPHER_SPEC) {
78 /* Total message size */
79 POSIX_GUARD(s2n_stuffer_write_uint16(output, 1));
80
81 /* change spec is always just 0x01 */
82 POSIX_GUARD(s2n_stuffer_write_uint8(output, 1));
83 return 0;
84 }
85
86 return 0;
87 }
88
89 int main(int argc, char **argv)
90 {
91 BEGIN_TEST();
92 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
93
94 /* Construct an array of all valid tls1.2 handshake_types */
95 uint16_t valid_tls12_handshakes[S2N_HANDSHAKES_COUNT];
96 int valid_tls12_handshakes_size = 0;
97 for (int i = 0; i < S2N_HANDSHAKES_COUNT; i++) {
98 if (memcmp(handshakes[i], invalid_handshake, S2N_MAX_HANDSHAKE_LENGTH) != 0) {
99 valid_tls12_handshakes[valid_tls12_handshakes_size] = i;
100 valid_tls12_handshakes_size++;
101 }
102 }
103 EXPECT_TRUE(valid_tls12_handshakes_size > 0);
104 EXPECT_TRUE(valid_tls12_handshakes_size < S2N_HANDSHAKES_COUNT);
105
106 /* Test: When using TLS 1.2, use the existing state machine and handshakes */
107 {
108 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
109 conn->actual_protocol_version = S2N_TLS12;
110 EXPECT_EQUAL(&ACTIVE_STATE_MACHINE(conn)[0], &state_machine[0]);
111 EXPECT_EQUAL(&ACTIVE_HANDSHAKES(conn)[0], &handshakes[0]);
112 EXPECT_SUCCESS(s2n_connection_free(conn));
113 };
114
115 /* Test: TLS1.2 server waits for expected CCS messages */
116 {
117 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
118 conn->actual_protocol_version = S2N_TLS12;
119
120 for (int i = 0; i < valid_tls12_handshakes_size; i++) {
121 int handshake = valid_tls12_handshakes[i];
122
123 conn->handshake.handshake_type = handshake;
124
125 for (int j = 0; j < S2N_MAX_HANDSHAKE_LENGTH; j++) {
126 if (handshakes[handshake][j] == CLIENT_CHANGE_CIPHER_SPEC) {
127 conn->handshake.message_number = j - 1;
128
129 EXPECT_SUCCESS(s2n_advance_message(conn));
130
131 EXPECT_EQUAL(conn->handshake.message_number, j);
132 EXPECT_EQUAL(ACTIVE_MESSAGE(conn), CLIENT_CHANGE_CIPHER_SPEC);
133
134 break;
135 }
136 }
137 }
138
139 EXPECT_SUCCESS(s2n_connection_free(conn));
140 };
141
142 /* Test: Client CCS messages always come before Client Finished messages */
143 {
144 for (int i = 0; i < valid_tls12_handshakes_size; i++) {
145 int handshake = valid_tls12_handshakes[i];
146 /* Initial handshake doesn't contain a CCS message */
147 if (handshake == INITIAL) {
148 continue;
149 }
150
151 bool ccs_encountered = false;
152
153 for (int j = 0; j < S2N_MAX_HANDSHAKE_LENGTH; j++) {
154 if (handshakes[handshake][j] == CLIENT_CHANGE_CIPHER_SPEC) {
155 ccs_encountered = true;
156 }
157
158 if (handshakes[handshake][j] == CLIENT_FINISHED) {
159 EXPECT_TRUE(ccs_encountered);
160 }
161 }
162 /* Every valid handshake includes a CCS message */
163 EXPECT_TRUE(ccs_encountered);
164 }
165 };
166
167 /* Test: TLS1.2 client waits for expected CCS messages */
168 {
169 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
170 conn->actual_protocol_version = S2N_TLS12;
171
172 for (int i = 0; i < valid_tls12_handshakes_size; i++) {
173 int handshake = valid_tls12_handshakes[i];
174
175 conn->handshake.handshake_type = handshake;
176
177 for (int j = 0; j < S2N_MAX_HANDSHAKE_LENGTH; j++) {
178 if (handshakes[handshake][j] == SERVER_CHANGE_CIPHER_SPEC) {
179 conn->handshake.message_number = j - 1;
180
181 EXPECT_SUCCESS(s2n_advance_message(conn));
182
183 EXPECT_EQUAL(conn->handshake.message_number, j);
184 EXPECT_EQUAL(ACTIVE_MESSAGE(conn), SERVER_CHANGE_CIPHER_SPEC);
185
186 break;
187 }
188 }
189 }
190
191 EXPECT_SUCCESS(s2n_connection_free(conn));
192 };
193
194 /* Test: TLS1.2 client handles expected server CCS messages
195 * but errors on unexpected CCS messages */
196 {
197 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
198 conn->actual_protocol_version = S2N_TLS12;
199
200 struct s2n_stuffer input = { 0 };
201 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));
202 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, NULL, conn));
203
204 for (int i = 0; i < valid_tls12_handshakes_size; i++) {
205 int handshake = valid_tls12_handshakes[i];
206
207 conn->handshake.handshake_type = handshake;
208
209 for (int j = 1; j < S2N_MAX_HANDSHAKE_LENGTH; j++) {
210 EXPECT_SUCCESS(s2n_setup_handler_to_expect(SERVER_CHANGE_CIPHER_SPEC, S2N_CLIENT));
211 conn->handshake.message_number = j;
212 conn->in_status = ENCRYPTED;
213
214 EXPECT_SUCCESS(s2n_test_write_header(&input, TLS_CHANGE_CIPHER_SPEC, 0));
215
216 if (handshakes[handshake][j] == SERVER_CHANGE_CIPHER_SPEC) {
217 EXPECT_SUCCESS(s2n_handshake_read_io(conn));
218 EXPECT_TRUE(expected_handler_called);
219 EXPECT_FALSE(unexpected_handler_called);
220 } else {
221 EXPECT_FAILURE_WITH_ERRNO(s2n_handshake_read_io(conn), S2N_ERR_BAD_MESSAGE);
222 EXPECT_FALSE(expected_handler_called);
223 EXPECT_FALSE(unexpected_handler_called);
224 }
225
226 EXPECT_SUCCESS(s2n_stuffer_wipe(&input));
227 }
228 }
229
230 EXPECT_SUCCESS(s2n_stuffer_free(&input));
231 EXPECT_SUCCESS(s2n_connection_free(conn));
232 };
233
234 /* Test: TLS1.2 server handles expected client CCS messages
235 * but errors on unexpected CCS messages */
236 {
237 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
238 conn->actual_protocol_version = S2N_TLS12;
239
240 struct s2n_stuffer input = { 0 };
241 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));
242 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, NULL, conn));
243
244 for (int i = 0; i < valid_tls12_handshakes_size; i++) {
245 int handshake = valid_tls12_handshakes[i];
246
247 conn->handshake.handshake_type = handshake;
248
249 for (int j = 1; j < S2N_MAX_HANDSHAKE_LENGTH; j++) {
250 EXPECT_SUCCESS(s2n_setup_handler_to_expect(CLIENT_CHANGE_CIPHER_SPEC, S2N_SERVER));
251 conn->handshake.message_number = j;
252 conn->in_status = ENCRYPTED;
253
254 EXPECT_SUCCESS(s2n_test_write_header(&input, TLS_CHANGE_CIPHER_SPEC, 0));
255
256 if (handshakes[handshake][j] == CLIENT_CHANGE_CIPHER_SPEC) {
257 EXPECT_SUCCESS(s2n_handshake_read_io(conn));
258 EXPECT_TRUE(expected_handler_called);
259 EXPECT_FALSE(unexpected_handler_called);
260 } else {
261 EXPECT_FAILURE_WITH_ERRNO(s2n_handshake_read_io(conn), S2N_ERR_BAD_MESSAGE);
262 EXPECT_FALSE(expected_handler_called);
263 EXPECT_FALSE(unexpected_handler_called);
264 }
265
266 EXPECT_SUCCESS(s2n_stuffer_wipe(&input));
267 }
268 }
269
270 EXPECT_SUCCESS(s2n_stuffer_free(&input));
271 EXPECT_SUCCESS(s2n_connection_free(conn));
272 };
273
274 /* Test: TLS1.2 client can receive a hello request message at any time. */
275 {
276 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
277 conn->actual_protocol_version = S2N_TLS12;
278
279 struct s2n_stuffer input = { 0 };
280 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));
281 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, NULL, conn));
282
283 for (size_t i = 0; i < valid_tls12_handshakes_size; i++) {
284 uint16_t handshake = valid_tls12_handshakes[i];
285
286 for (size_t j = 0; j < S2N_MAX_HANDSHAKE_LENGTH; j++) {
287 if (handshakes[handshake][j] == APPLICATION_DATA) {
288 break;
289 }
290
291 conn->handshake.message_number = j;
292 conn->in_status = ENCRYPTED;
293 conn->handshake.handshake_type = handshake;
294
295 EXPECT_SUCCESS(s2n_test_write_header(&input, TLS_HANDSHAKE, TLS_HELLO_REQUEST));
296 EXPECT_SUCCESS(s2n_handshake_read_io(conn));
297 EXPECT_EQUAL(conn->handshake.message_number, j);
298
299 EXPECT_SUCCESS(s2n_stuffer_wipe(&input));
300 }
301 }
302
303 EXPECT_FALSE(unexpected_handler_called);
304 EXPECT_SUCCESS(s2n_stuffer_free(&input));
305 EXPECT_SUCCESS(s2n_connection_free(conn));
306 };
307
308 /* Test: TLS1.2 s2n_handshake_read_io should accept only the expected message */
309 {
310 /* TLS1.2 should accept the expected message */
311 {
312 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
313 conn->actual_protocol_version = S2N_TLS12;
314
315 struct s2n_stuffer input = { 0 };
316 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));
317 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, NULL, conn));
318
319 conn->handshake.handshake_type = 0;
320 conn->handshake.message_number = 0;
321 EXPECT_EQUAL(ACTIVE_MESSAGE(conn), CLIENT_HELLO);
322 EXPECT_SUCCESS(s2n_setup_handler_to_expect(CLIENT_HELLO, S2N_SERVER));
323
324 EXPECT_SUCCESS(s2n_test_write_header(&input, TLS_HANDSHAKE, TLS_CLIENT_HELLO));
325 EXPECT_SUCCESS(s2n_handshake_read_io(conn));
326
327 EXPECT_EQUAL(conn->handshake.message_number, 1);
328 EXPECT_FALSE(unexpected_handler_called);
329 EXPECT_TRUE(expected_handler_called);
330
331 EXPECT_SUCCESS(s2n_stuffer_free(&input));
332 EXPECT_SUCCESS(s2n_connection_free(conn));
333 };
334
335 /* TLS1.2 should error for an unexpected message */
336 {
337 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
338 conn->actual_protocol_version = S2N_TLS12;
339
340 struct s2n_stuffer input = { 0 };
341 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));
342 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, NULL, conn));
343
344 conn->handshake.handshake_type = 0;
345 conn->handshake.message_number = 0;
346 EXPECT_EQUAL(ACTIVE_MESSAGE(conn), CLIENT_HELLO);
347 EXPECT_SUCCESS(s2n_setup_handler_to_expect(CLIENT_HELLO, S2N_SERVER));
348
349 EXPECT_SUCCESS(s2n_test_write_header(&input, TLS_HANDSHAKE, TLS_CERTIFICATE));
350 EXPECT_FAILURE_WITH_ERRNO(s2n_handshake_read_io(conn), S2N_ERR_BAD_MESSAGE);
351
352 EXPECT_EQUAL(conn->handshake.message_number, 0);
353 EXPECT_FALSE(unexpected_handler_called);
354 EXPECT_FALSE(expected_handler_called);
355
356 EXPECT_SUCCESS(s2n_stuffer_free(&input));
357 EXPECT_SUCCESS(s2n_connection_free(conn));
358 };
359
360 /* TLS1.2 should error for an expected message from the wrong writer */
361 {
362 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
363 conn->actual_protocol_version = S2N_TLS12;
364
365 struct s2n_stuffer input = { 0 };
366 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));
367 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, NULL, conn));
368
369 conn->handshake.handshake_type = 0;
370 conn->handshake.message_number = 0;
371 EXPECT_EQUAL(ACTIVE_MESSAGE(conn), CLIENT_HELLO);
372 EXPECT_SUCCESS(s2n_setup_handler_to_expect(CLIENT_HELLO, S2N_SERVER));
373
374 EXPECT_SUCCESS(s2n_test_write_header(&input, TLS_HANDSHAKE, TLS_CLIENT_HELLO));
375 EXPECT_FAILURE_WITH_ERRNO(s2n_handshake_read_io(conn), S2N_ERR_BAD_MESSAGE);
376
377 EXPECT_EQUAL(conn->handshake.message_number, 0);
378 EXPECT_FALSE(unexpected_handler_called);
379 EXPECT_FALSE(expected_handler_called);
380
381 EXPECT_SUCCESS(s2n_stuffer_free(&input));
382 EXPECT_SUCCESS(s2n_connection_free(conn));
383 };
384
385 /* TLS1.2 should error for an expected message from the wrong record type */
386 {
387 /* Unfortunately, all our non-handshake record types have a message type of 0,
388 * and the combination of TLS_HANDSHAKE + "0" is actually a message (TLS_HELLO_REQUEST)
389 * which can appear at any point in a TLS1.2 handshake.
390 *
391 * To test, temporarily modify the actions table.
392 * We MUST restore this after this test.
393 */
394 uint8_t old_message_type = state_machine[SERVER_CHANGE_CIPHER_SPEC].message_type;
395 state_machine[SERVER_CHANGE_CIPHER_SPEC].message_type = 1;
396
397 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
398 conn->actual_protocol_version = S2N_TLS12;
399
400 struct s2n_stuffer input = { 0 };
401 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));
402 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, NULL, conn));
403
404 uint8_t server_css_message_number = 2;
405 conn->handshake.handshake_type = NEGOTIATED;
406 conn->handshake.message_number = server_css_message_number;
407 EXPECT_EQUAL(ACTIVE_MESSAGE(conn), SERVER_CHANGE_CIPHER_SPEC);
408 EXPECT_SUCCESS(s2n_setup_handler_to_expect(SERVER_CHANGE_CIPHER_SPEC, S2N_CLIENT));
409
410 EXPECT_SUCCESS(s2n_test_write_header(&input, TLS_HANDSHAKE, ACTIVE_STATE(conn).message_type));
411 EXPECT_FAILURE_WITH_ERRNO(s2n_handshake_read_io(conn), S2N_ERR_BAD_MESSAGE);
412
413 EXPECT_EQUAL(conn->handshake.message_number, server_css_message_number);
414 EXPECT_FALSE(unexpected_handler_called);
415 EXPECT_FALSE(expected_handler_called);
416
417 EXPECT_SUCCESS(s2n_stuffer_free(&input));
418 EXPECT_SUCCESS(s2n_connection_free(conn));
419 state_machine[SERVER_CHANGE_CIPHER_SPEC].message_type = old_message_type;
420 };
421 };
422
423 /* Test: TLS1.2 handshake type name maximum size is set correctly.
424 * The maximum size is the size of a name with all flags set. */
425 {
426 size_t correct_size = 0;
427 for (size_t i = 0; i < s2n_array_len(tls12_handshake_type_names); i++) {
428 correct_size += strlen(tls12_handshake_type_names[i]);
429 }
430 if (correct_size > MAX_HANDSHAKE_TYPE_LEN) {
431 fprintf(stderr, "\nMAX_HANDSHAKE_TYPE_LEN should be at least %lu\n", (unsigned long) correct_size);
432 FAIL_MSG("MAX_HANDSHAKE_TYPE_LEN wrong for TLS1.2 handshakes");
433 }
434 };
435
436 /* Test: TLS 1.2 handshake types are all properly printed */
437 {
438 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
439 conn->actual_protocol_version = S2N_TLS12;
440
441 conn->handshake.handshake_type = INITIAL;
442 EXPECT_STRING_EQUAL("INITIAL", s2n_connection_get_handshake_type_name(conn));
443
444 conn->handshake.handshake_type = NEGOTIATED | FULL_HANDSHAKE;
445 EXPECT_STRING_EQUAL("NEGOTIATED|FULL_HANDSHAKE", s2n_connection_get_handshake_type_name(conn));
446
447 const char *all_flags_handshake_type_name = "NEGOTIATED|FULL_HANDSHAKE|CLIENT_AUTH|NO_CLIENT_CERT|"
448 "TLS12_PERFECT_FORWARD_SECRECY|OCSP_STATUS|WITH_SESSION_TICKET|WITH_NPN";
449 conn->handshake.handshake_type = NEGOTIATED | FULL_HANDSHAKE | CLIENT_AUTH | NO_CLIENT_CERT | TLS12_PERFECT_FORWARD_SECRECY | OCSP_STATUS | WITH_SESSION_TICKET | WITH_NPN;
450 EXPECT_STRING_EQUAL(all_flags_handshake_type_name, s2n_connection_get_handshake_type_name(conn));
451
452 const char *handshake_type_name = NULL;
453 for (int i = 0; i < valid_tls12_handshakes_size; i++) {
454 conn->handshake.handshake_type = valid_tls12_handshakes[i];
455
456 handshake_type_name = s2n_connection_get_handshake_type_name(conn);
457
458 /* The handshake type names must be unique */
459 for (int j = 0; j < valid_tls12_handshakes_size; j++) {
460 conn->handshake.handshake_type = valid_tls12_handshakes[j];
461 if (i == j) {
462 EXPECT_STRING_EQUAL(handshake_type_name, s2n_connection_get_handshake_type_name(conn));
463 } else {
464 EXPECT_STRING_NOT_EQUAL(handshake_type_name, s2n_connection_get_handshake_type_name(conn));
465 }
466 }
467 }
468
469 EXPECT_SUCCESS(s2n_connection_free(conn));
470 };
471
472 /* Test: TLS 1.2 message types are all properly printed */
473 {
474 uint32_t test_handshake_type = NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY | OCSP_STATUS | CLIENT_AUTH | WITH_SESSION_TICKET | WITH_NPN;
475 const char *expected[] = { "CLIENT_HELLO",
476 "SERVER_HELLO", "SERVER_CERT", "SERVER_CERT_STATUS", "SERVER_KEY", "SERVER_CERT_REQ", "SERVER_HELLO_DONE",
477 "CLIENT_CERT", "CLIENT_KEY", "CLIENT_CERT_VERIFY", "CLIENT_CHANGE_CIPHER_SPEC", "CLIENT_NPN",
478 "CLIENT_FINISHED", "SERVER_NEW_SESSION_TICKET", "SERVER_CHANGE_CIPHER_SPEC", "SERVER_FINISHED",
479 "APPLICATION_DATA" };
480
481 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
482
483 conn->handshake.handshake_type = test_handshake_type;
484
485 for (size_t i = 0; i < s2n_array_len(expected); i++) {
486 conn->handshake.message_number = i;
487 EXPECT_STRING_EQUAL(expected[i], s2n_connection_get_last_message_name(conn));
488 }
489
490 EXPECT_SUCCESS(s2n_connection_free(conn));
491 };
492
493 /* Test: A WITH_NPN form of every valid, negotiated handshake exists */
494 {
495 for (size_t i = 0; i < valid_tls12_handshakes_size; i++) {
496 uint32_t handshake_type_original = valid_tls12_handshakes[i];
497 message_type_t *messages_original = handshakes[handshake_type_original];
498
499 /* Ignore INITIAL and WITH_NPN handshakes */
500 if (!(handshake_type_original & NEGOTIATED) || (handshake_type_original & WITH_NPN)) {
501 continue;
502 }
503
504 /* Get the WITH_NPN form of the handshake */
505 uint32_t handshake_type_npn = handshake_type_original | WITH_NPN;
506 message_type_t *messages_npn = handshakes[handshake_type_npn];
507
508 for (size_t j = 0, j_npn = 0; j < S2N_MAX_HANDSHAKE_LENGTH && j_npn < S2N_MAX_HANDSHAKE_LENGTH; j++, j_npn++) {
509 /* The original handshake cannot contain the Next Protocol message */
510 EXPECT_NOT_EQUAL(messages_original[j], CLIENT_NPN);
511
512 /* Skip the Next Protocol message in WITH_NPN handshake */
513 if (messages_npn[j_npn] == CLIENT_NPN) {
514 j_npn++;
515 }
516
517 /* Otherwise the handshakes must be equivalent */
518 EXPECT_EQUAL(messages_original[j], messages_npn[j_npn]);
519 }
520 }
521 };
522
523 /* Test: ensure that there isn't a collision between TLS 1.2 and TLS 1.3 flags
524 * that share the same bit.
525 */
526 {
527 /* sanity check: these flags are the same bit */
528 EXPECT_EQUAL((int) TLS12_PERFECT_FORWARD_SECRECY, (int) HELLO_RETRY_REQUEST);
529
530 uint32_t tls12_type = NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY;
531 uint32_t tls13_type = NEGOTIATED | FULL_HANDSHAKE | HELLO_RETRY_REQUEST;
532
533 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
534 EXPECT_NOT_NULL(conn);
535
536 /* Clear the cache entries so we start fresh */
537 memset(handshake_type_str_tls12ish[tls12_type], 0, MAX_HANDSHAKE_TYPE_LEN);
538 memset(handshake_type_str_tls13[tls13_type], 0, MAX_HANDSHAKE_TYPE_LEN);
539
540 /* First: populate cache via TLS 1.2 */
541 conn->actual_protocol_version = S2N_TLS12;
542 conn->handshake.handshake_type = tls12_type;
543 const char *tls12_name = s2n_connection_get_handshake_type_name(conn);
544 EXPECT_NOT_NULL(tls12_name);
545 EXPECT_STRING_EQUAL(tls12_name, "NEGOTIATED|FULL_HANDSHAKE|TLS12_PERFECT_FORWARD_SECRECY");
546
547 /* Second: query cache via TLS 1.3 with same bitmap — should not collide */
548 conn->actual_protocol_version = S2N_TLS13;
549 conn->handshake.handshake_type = tls13_type;
550 const char *tls13_name = s2n_connection_get_handshake_type_name(conn);
551 EXPECT_NOT_NULL(tls13_name);
552 EXPECT_STRING_EQUAL(tls13_name, "NEGOTIATED|FULL_HANDSHAKE|HELLO_RETRY_REQUEST");
553
554 EXPECT_SUCCESS(s2n_connection_free(conn));
555 };
556
557 END_TEST();
558 }