-
Notifications
You must be signed in to change notification settings - Fork 722
/
Copy paths2n_client_early_data_indication_test.c
511 lines (420 loc) · 25.3 KB
/
s2n_client_early_data_indication_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#include "s2n_test.h"
#include "testlib/s2n_testlib.h"
#include "tls/extensions/s2n_client_psk.h"
#include "tls/extensions/s2n_early_data_indication.h"
#include "tls/s2n_tls.h"
#include "tls/s2n_tls13.h"
static S2N_RESULT s2n_set_early_data_app_protocol(struct s2n_connection *conn, struct s2n_blob *app_protocol)
{
RESULT_ENSURE_REF(conn);
RESULT_ENSURE_REF(app_protocol);
struct s2n_psk *psk = NULL;
RESULT_GUARD(s2n_array_get(&conn->psk_params.psk_list, 0, (void **) &psk));
RESULT_GUARD_POSIX(s2n_psk_set_application_protocol(psk, app_protocol->data, app_protocol->size));
return S2N_RESULT_OK;
}
int main(int argc, char **argv)
{
BEGIN_TEST();
if (!s2n_is_tls13_fully_supported()) {
END_TEST();
}
/* Test s2n_client_early_data_indication_should_send */
{
/* Safety check */
EXPECT_FALSE(s2n_client_early_data_indication_extension.should_send(NULL));
const uint32_t nonzero_max_early_data = 10;
/* All checks pass: send extension */
{
struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
EXPECT_NOT_NULL(conn);
EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "default_tls13"));
EXPECT_OK(s2n_append_test_psk_with_early_data(conn, nonzero_max_early_data, &s2n_tls13_aes_256_gcm_sha384));
EXPECT_SUCCESS(s2n_connection_set_early_data_expected(conn));
EXPECT_TRUE(s2n_client_early_data_indication_extension.should_send(conn));
EXPECT_SUCCESS(s2n_connection_free(conn));
};
/* Don't send if early data not supported */
{
struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
EXPECT_NOT_NULL(conn);
EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "default_tls13"));
EXPECT_OK(s2n_append_test_psk_with_early_data(conn, nonzero_max_early_data, &s2n_tls13_aes_256_gcm_sha384));
EXPECT_FALSE(s2n_client_early_data_indication_extension.should_send(conn));
EXPECT_SUCCESS(s2n_connection_set_early_data_expected(conn));
EXPECT_TRUE(s2n_client_early_data_indication_extension.should_send(conn));
EXPECT_SUCCESS(s2n_connection_free(conn));
};
/** Don't send if no PSK extension is sent.
*
*= https://www.rfc-editor.org/rfc/rfc8446#section-4.2.10
*= type=test
*# When a PSK is used and early data is allowed for that PSK, the client
*# can send Application Data in its first flight of messages. If the
*# client opts to do so, it MUST supply both the "pre_shared_key" and
*# "early_data" extensions.
*/
{
struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
EXPECT_NOT_NULL(conn);
EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "default_tls13"));
EXPECT_SUCCESS(s2n_connection_set_early_data_expected(conn));
EXPECT_FALSE(s2n_client_psk_extension.should_send(conn));
EXPECT_FALSE(s2n_client_early_data_indication_extension.should_send(conn));
EXPECT_SUCCESS(s2n_connection_free(conn));
}
/**
* Don't send when performing a retry.
*
*= https://www.rfc-editor.org/rfc/rfc8446#section-4.2.10
*= type=test
*# A client MUST NOT include the
*# "early_data" extension in its followup ClientHello.
*
*= https://www.rfc-editor.org/rfc/rfc8446#4.1.2
*= type=test
*# - Removing the "early_data" extension (Section 4.2.10) if one was
*# present. Early data is not permitted after a HelloRetryRequest.
**/
{
struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
EXPECT_NOT_NULL(conn);
EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "default_tls13"));
EXPECT_OK(s2n_append_test_psk_with_early_data(conn, nonzero_max_early_data, &s2n_tls13_aes_256_gcm_sha384));
EXPECT_SUCCESS(s2n_connection_set_early_data_expected(conn));
EXPECT_TRUE(s2n_client_early_data_indication_extension.should_send(conn));
EXPECT_SUCCESS(s2n_set_hello_retry_required(conn));
EXPECT_FALSE(s2n_client_early_data_indication_extension.should_send(conn));
EXPECT_SUCCESS(s2n_connection_free(conn));
};
/* Don't send if no early data allowed by first PSK */
{
struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
EXPECT_NOT_NULL(conn);
EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "default_tls13"));
EXPECT_SUCCESS(s2n_connection_set_early_data_expected(conn));
EXPECT_OK(s2n_append_test_psk_with_early_data(conn, 0, &s2n_tls13_aes_256_gcm_sha384));
EXPECT_OK(s2n_append_test_psk_with_early_data(conn, nonzero_max_early_data, &s2n_tls13_aes_256_gcm_sha384));
EXPECT_FALSE(s2n_client_early_data_indication_extension.should_send(conn));
EXPECT_OK(s2n_psk_parameters_wipe(&conn->psk_params));
EXPECT_OK(s2n_append_test_psk_with_early_data(conn, nonzero_max_early_data, &s2n_tls13_aes_256_gcm_sha384));
EXPECT_OK(s2n_append_test_psk_with_early_data(conn, 0, &s2n_tls13_aes_256_gcm_sha384));
EXPECT_TRUE(s2n_client_early_data_indication_extension.should_send(conn));
EXPECT_SUCCESS(s2n_connection_free(conn));
};
/* Don't send if protocol version too low */
{
struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
EXPECT_NOT_NULL(conn);
EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "default_tls13"));
EXPECT_OK(s2n_append_test_psk_with_early_data(conn, nonzero_max_early_data, &s2n_tls13_aes_256_gcm_sha384));
EXPECT_SUCCESS(s2n_connection_set_early_data_expected(conn));
conn->actual_protocol_version = S2N_TLS12;
EXPECT_FALSE(s2n_client_early_data_indication_extension.should_send(conn));
conn->actual_protocol_version = S2N_TLS13;
EXPECT_TRUE(s2n_client_early_data_indication_extension.should_send(conn));
conn->actual_protocol_version = S2N_TLS13 + 1;
EXPECT_TRUE(s2n_client_early_data_indication_extension.should_send(conn));
EXPECT_SUCCESS(s2n_connection_free(conn));
};
/* Don't send if cipher suite not allowed by cipher preferences */
{
struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
EXPECT_NOT_NULL(conn);
EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "default_tls13"));
EXPECT_SUCCESS(s2n_connection_set_early_data_expected(conn));
EXPECT_OK(s2n_append_test_psk_with_early_data(conn, nonzero_max_early_data, &s2n_rsa_with_3des_ede_cbc_sha));
EXPECT_FALSE(s2n_client_early_data_indication_extension.should_send(conn));
EXPECT_OK(s2n_psk_parameters_wipe(&conn->psk_params));
EXPECT_OK(s2n_append_test_psk_with_early_data(conn, nonzero_max_early_data, &s2n_tls13_aes_256_gcm_sha384));
EXPECT_TRUE(s2n_client_early_data_indication_extension.should_send(conn));
EXPECT_SUCCESS(s2n_connection_free(conn));
};
/* Don't send if application layer protocol not allowed by preferences */
{
struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
EXPECT_NOT_NULL(conn);
EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(conn, "default_tls13"));
EXPECT_OK(s2n_append_test_psk_with_early_data(conn, nonzero_max_early_data, &s2n_tls13_aes_256_gcm_sha384));
EXPECT_SUCCESS(s2n_connection_set_early_data_expected(conn));
uint8_t app_protocol_data[] = "protocol preference";
uint8_t other_app_protocol_data[] = "different protocol";
struct s2n_blob app_protocol = { 0 }, empty_app_protocol = { 0 };
EXPECT_SUCCESS(s2n_blob_init(&app_protocol, app_protocol_data, sizeof(app_protocol_data)));
/* No early data alp, empty alpn preferences: send */
EXPECT_OK(s2n_set_early_data_app_protocol(conn, &empty_app_protocol));
EXPECT_TRUE(s2n_client_early_data_indication_extension.should_send(conn));
/* Early data alp, empty alpn preferences: don't send */
EXPECT_OK(s2n_set_early_data_app_protocol(conn, &app_protocol));
EXPECT_FALSE(s2n_client_early_data_indication_extension.should_send(conn));
EXPECT_SUCCESS(s2n_connection_append_protocol_preference(conn, other_app_protocol_data,
sizeof(other_app_protocol_data)));
EXPECT_SUCCESS(s2n_connection_append_protocol_preference(conn, other_app_protocol_data,
sizeof(other_app_protocol_data)));
/* No early data alp, non-empty alpn preferences: send */
EXPECT_OK(s2n_set_early_data_app_protocol(conn, &empty_app_protocol));
EXPECT_TRUE(s2n_client_early_data_indication_extension.should_send(conn));
/* alpn preferences don't contain alp: don't send */
EXPECT_OK(s2n_set_early_data_app_protocol(conn, &app_protocol));
EXPECT_FALSE(s2n_client_early_data_indication_extension.should_send(conn));
EXPECT_SUCCESS(s2n_connection_append_protocol_preference(conn, app_protocol.data, app_protocol.size));
/* alpn preferences contain alp: send */
EXPECT_OK(s2n_set_early_data_app_protocol(conn, &app_protocol));
EXPECT_TRUE(s2n_client_early_data_indication_extension.should_send(conn));
EXPECT_SUCCESS(s2n_connection_free(conn));
};
};
/* Test s2n_client_early_data_indiction_send */
{
/* Set MIDDLEBOX_COMPAT | EARLY_CLIENT_CCS handshake type flags */
{
struct s2n_config *config = s2n_config_new();
EXPECT_NOT_NULL(config);
struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
EXPECT_NOT_NULL(conn);
EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
EXPECT_OK(s2n_append_test_psk_with_early_data(conn, 0, &s2n_tls13_aes_128_gcm_sha256));
EXPECT_EQUAL(conn->handshake.handshake_type, 0);
/* Don't set if < TLS1.3 */
conn->actual_protocol_version = S2N_TLS12;
config->quic_enabled = false;
EXPECT_SUCCESS(s2n_client_early_data_indication_extension.send(conn, NULL));
EXPECT_EQUAL(conn->handshake.handshake_type, 0);
/* Don't set if middlebox compat disabled */
conn->actual_protocol_version = S2N_TLS13;
config->quic_enabled = true;
EXPECT_SUCCESS(s2n_client_early_data_indication_extension.send(conn, NULL));
EXPECT_EQUAL(conn->handshake.handshake_type, 0);
/* Otherwise, set */
conn->actual_protocol_version = S2N_TLS13;
config->quic_enabled = false;
EXPECT_SUCCESS(s2n_client_early_data_indication_extension.send(conn, NULL));
EXPECT_EQUAL(conn->handshake.handshake_type, (MIDDLEBOX_COMPAT | EARLY_CLIENT_CCS));
EXPECT_SUCCESS(s2n_config_free(config));
EXPECT_SUCCESS(s2n_connection_free(conn));
};
};
/* Test s2n_client_early_data_indiction_recv */
{
struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
EXPECT_NOT_NULL(conn);
conn->actual_protocol_version = S2N_TLS13;
/* Successful if not retry */
conn->early_data_state = S2N_UNKNOWN_EARLY_DATA_STATE;
conn->handshake.message_number = 0;
EXPECT_SUCCESS(s2n_client_early_data_indication_extension.recv(conn, NULL));
EXPECT_EQUAL(conn->early_data_state, S2N_EARLY_DATA_REQUESTED);
/**
*= https://www.rfc-editor.org/rfc/rfc8446#section-4.2.10
*= type=test
*# A client MUST NOT include the
*# "early_data" extension in its followup ClientHello.
*/
conn->early_data_state = S2N_UNKNOWN_EARLY_DATA_STATE;
conn->handshake.message_number = 1;
EXPECT_FAILURE_WITH_ERRNO(s2n_client_early_data_indication_extension.recv(conn, NULL),
S2N_ERR_UNSUPPORTED_EXTENSION);
EXPECT_SUCCESS(s2n_connection_free(conn));
};
/* Test state transitions */
{
/* When early data not enabled on client */
{
struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
EXPECT_NOT_NULL(client_conn);
EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, "default_tls13"));
EXPECT_OK(s2n_append_test_psk_with_early_data(client_conn, 1, &s2n_tls13_aes_256_gcm_sha384));
EXPECT_EQUAL(client_conn->early_data_state, S2N_UNKNOWN_EARLY_DATA_STATE);
struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
EXPECT_NOT_NULL(server_conn);
EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "default_tls13"));
EXPECT_OK(s2n_append_test_psk_with_early_data(server_conn, 1, &s2n_tls13_aes_256_gcm_sha384));
EXPECT_EQUAL(server_conn->early_data_state, S2N_UNKNOWN_EARLY_DATA_STATE);
EXPECT_SUCCESS(s2n_connection_set_early_data_expected(server_conn));
EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
s2n_stuffer_data_available(&client_conn->handshake.io)));
EXPECT_SUCCESS(s2n_establish_session(server_conn));
EXPECT_EQUAL(client_conn->early_data_state, S2N_EARLY_DATA_NOT_REQUESTED);
EXPECT_EQUAL(server_conn->early_data_state, S2N_EARLY_DATA_NOT_REQUESTED);
EXPECT_SUCCESS(s2n_connection_free(client_conn));
EXPECT_SUCCESS(s2n_connection_free(server_conn));
};
/* When early data not enabled on server */
{
struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
EXPECT_NOT_NULL(client_conn);
EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, "default_tls13"));
EXPECT_OK(s2n_append_test_psk_with_early_data(client_conn, 1, &s2n_tls13_aes_256_gcm_sha384));
EXPECT_EQUAL(client_conn->early_data_state, S2N_UNKNOWN_EARLY_DATA_STATE);
EXPECT_SUCCESS(s2n_connection_set_early_data_expected(client_conn));
struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
EXPECT_NOT_NULL(server_conn);
EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "default_tls13"));
EXPECT_OK(s2n_append_test_psk_with_early_data(server_conn, 1, &s2n_tls13_aes_256_gcm_sha384));
EXPECT_EQUAL(server_conn->early_data_state, S2N_UNKNOWN_EARLY_DATA_STATE);
EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
s2n_stuffer_data_available(&client_conn->handshake.io)));
EXPECT_SUCCESS(s2n_establish_session(server_conn));
EXPECT_EQUAL(client_conn->early_data_state, S2N_EARLY_DATA_REQUESTED);
EXPECT_EQUAL(server_conn->early_data_state, S2N_EARLY_DATA_REJECTED);
EXPECT_SUCCESS(s2n_connection_free(client_conn));
EXPECT_SUCCESS(s2n_connection_free(server_conn));
};
/* When early data requested */
{
struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
EXPECT_NOT_NULL(client_conn);
EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, "default_tls13"));
EXPECT_OK(s2n_append_test_psk_with_early_data(client_conn, 1, &s2n_tls13_aes_256_gcm_sha384));
EXPECT_EQUAL(client_conn->early_data_state, S2N_UNKNOWN_EARLY_DATA_STATE);
EXPECT_SUCCESS(s2n_connection_set_early_data_expected(client_conn));
struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
EXPECT_NOT_NULL(server_conn);
EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "default_tls13"));
EXPECT_OK(s2n_append_test_psk_with_early_data(server_conn, 1, &s2n_tls13_aes_256_gcm_sha384));
EXPECT_EQUAL(server_conn->early_data_state, S2N_UNKNOWN_EARLY_DATA_STATE);
EXPECT_SUCCESS(s2n_connection_set_early_data_expected(server_conn));
EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
s2n_stuffer_data_available(&client_conn->handshake.io)));
EXPECT_SUCCESS(s2n_establish_session(server_conn));
EXPECT_EQUAL(client_conn->early_data_state, S2N_EARLY_DATA_REQUESTED);
EXPECT_EQUAL(server_conn->early_data_state, S2N_EARLY_DATA_ACCEPTED);
EXPECT_SUCCESS(s2n_connection_free(client_conn));
EXPECT_SUCCESS(s2n_connection_free(server_conn));
};
/* When early data not requested */
{
struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
EXPECT_NOT_NULL(client_conn);
EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, "default_tls13"));
EXPECT_OK(s2n_append_test_psk_with_early_data(client_conn, 0, &s2n_tls13_aes_256_gcm_sha384));
EXPECT_EQUAL(client_conn->early_data_state, S2N_UNKNOWN_EARLY_DATA_STATE);
EXPECT_SUCCESS(s2n_connection_set_early_data_expected(client_conn));
struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
EXPECT_NOT_NULL(server_conn);
EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "default_tls13"));
EXPECT_OK(s2n_append_test_psk_with_early_data(server_conn, 0, &s2n_tls13_aes_256_gcm_sha384));
EXPECT_EQUAL(server_conn->early_data_state, S2N_UNKNOWN_EARLY_DATA_STATE);
EXPECT_SUCCESS(s2n_connection_set_early_data_expected(server_conn));
EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
s2n_stuffer_data_available(&client_conn->handshake.io)));
EXPECT_SUCCESS(s2n_establish_session(server_conn));
EXPECT_EQUAL(client_conn->early_data_state, S2N_EARLY_DATA_NOT_REQUESTED);
EXPECT_EQUAL(server_conn->early_data_state, S2N_EARLY_DATA_NOT_REQUESTED);
EXPECT_SUCCESS(s2n_connection_free(client_conn));
EXPECT_SUCCESS(s2n_connection_free(server_conn));
};
};
/* Test state transitions with a HelloRetryRequest.
*
*= https://www.rfc-editor.org/rfc/rfc8446#section-4.2.10
*= type=test
*# A server which receives an "early_data" extension MUST behave in one
*# of three ways:
*
*= https://www.rfc-editor.org/rfc/rfc8446#section-4.2.10
*= type=test
*# - Request that the client send another ClientHello by responding
*# with a HelloRetryRequest.
*/
{
/* Hello Retry Request because of rejected early data.
*
* The S2N server does not reject early data via a HelloRetryRequest, but other implementations might.
* The S2N client should handle retries triggered by early data gracefully.
*/
{
struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
EXPECT_NOT_NULL(client_conn);
EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, "default_tls13"));
EXPECT_OK(s2n_append_test_psk_with_early_data(client_conn, 1, &s2n_tls13_aes_256_gcm_sha384));
EXPECT_EQUAL(client_conn->early_data_state, S2N_UNKNOWN_EARLY_DATA_STATE);
EXPECT_SUCCESS(s2n_connection_set_early_data_expected(client_conn));
struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
EXPECT_NOT_NULL(server_conn);
EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "default_tls13"));
EXPECT_OK(s2n_append_test_psk_with_early_data(server_conn, 0, &s2n_tls13_aes_256_gcm_sha384));
EXPECT_EQUAL(server_conn->early_data_state, S2N_UNKNOWN_EARLY_DATA_STATE);
EXPECT_SUCCESS(s2n_connection_set_early_data_expected(server_conn));
EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
s2n_stuffer_data_available(&client_conn->handshake.io)));
EXPECT_SUCCESS(s2n_establish_session(server_conn));
EXPECT_EQUAL(client_conn->early_data_state, S2N_EARLY_DATA_REQUESTED);
/* Force a retry */
EXPECT_SUCCESS(s2n_set_hello_retry_required(server_conn));
/* There is retry handling logic that checks that the current message
* is a hello retry message, which requires that we be at a specific message number. */
server_conn->handshake.message_number = 2;
client_conn->handshake.message_number = 2;
/* Update the selected_group to ensure the HRR is valid */
client_conn->kex_params.client_ecc_evp_params.negotiated_curve = &s2n_ecc_curve_secp521r1;
EXPECT_SUCCESS(s2n_server_hello_retry_send(server_conn));
EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io,
s2n_stuffer_data_available(&server_conn->handshake.io)));
EXPECT_SUCCESS(s2n_server_hello_recv(client_conn));
EXPECT_EQUAL(client_conn->early_data_state, S2N_EARLY_DATA_REJECTED);
EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
EXPECT_EQUAL(client_conn->early_data_state, S2N_EARLY_DATA_REJECTED);
EXPECT_SUCCESS(s2n_connection_free(client_conn));
EXPECT_SUCCESS(s2n_connection_free(server_conn));
};
/* Hello Retry Request because of missing key share: still rejects early data */
{
struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
EXPECT_NOT_NULL(client_conn);
client_conn->security_policy_override = &security_policy_test_tls13_retry;
EXPECT_OK(s2n_append_test_psk_with_early_data(client_conn, 1, &s2n_tls13_aes_256_gcm_sha384));
EXPECT_EQUAL(client_conn->early_data_state, S2N_UNKNOWN_EARLY_DATA_STATE);
EXPECT_SUCCESS(s2n_connection_set_early_data_expected(client_conn));
struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
EXPECT_NOT_NULL(server_conn);
server_conn->security_policy_override = &security_policy_test_all_tls13;
EXPECT_OK(s2n_append_test_psk_with_early_data(server_conn, 1, &s2n_tls13_aes_256_gcm_sha384));
EXPECT_EQUAL(server_conn->early_data_state, S2N_UNKNOWN_EARLY_DATA_STATE);
EXPECT_SUCCESS(s2n_connection_set_early_data_expected(server_conn));
EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
s2n_stuffer_data_available(&client_conn->handshake.io)));
EXPECT_SUCCESS(s2n_establish_session(server_conn));
EXPECT_EQUAL(client_conn->early_data_state, S2N_EARLY_DATA_REQUESTED);
EXPECT_EQUAL(server_conn->early_data_state, S2N_EARLY_DATA_REJECTED);
EXPECT_TRUE(s2n_is_hello_retry_handshake(server_conn));
/* There is retry handling logic that checks that the current message
* is a hello retry message, which requires that we be at a specific message number. */
server_conn->handshake.message_number = 2;
client_conn->handshake.message_number = 2;
EXPECT_SUCCESS(s2n_server_hello_retry_send(server_conn));
EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io,
s2n_stuffer_data_available(&server_conn->handshake.io)));
EXPECT_SUCCESS(s2n_server_hello_recv(client_conn));
EXPECT_TRUE(s2n_is_hello_retry_handshake(client_conn));
EXPECT_EQUAL(client_conn->early_data_state, S2N_EARLY_DATA_REJECTED);
EXPECT_EQUAL(server_conn->early_data_state, S2N_EARLY_DATA_REJECTED);
EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
EXPECT_EQUAL(client_conn->early_data_state, S2N_EARLY_DATA_REJECTED);
EXPECT_EQUAL(server_conn->early_data_state, S2N_EARLY_DATA_REJECTED);
EXPECT_SUCCESS(s2n_connection_free(client_conn));
EXPECT_SUCCESS(s2n_connection_free(server_conn));
};
};
END_TEST();
}