Debian Codegraph @

crt/s2n/tests/unit/s2n_early_data_io_api_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 "s2n_test.h"
17 #include "testlib/s2n_testlib.h"
18 #include "tls/s2n_early_data.h"
19
20 #define BUFFER_SIZE 100
21
22 #define EXPECT_NOT_BLOCKED(conn, blocked, expected_msg) \
23 EXPECT_EQUAL((blocked), S2N_NOT_BLOCKED); \
24 EXPECT_EQUAL(s2n_conn_get_current_message_type(conn), (expected_msg))
25 #define EXPECT_BLOCKED_ON_EARLY_DATA(result) EXPECT_FAILURE_WITH_ERRNO((result), S2N_ERR_EARLY_DATA_BLOCKED)
26 #define EXPECT_BLOCKED_ON_IO(result) EXPECT_FAILURE_WITH_ERRNO((result), S2N_ERR_IO_BLOCKED)
27 #define EXPECT_BLOCKED_ON(conn, blocked, expected_blocked, expected_msg) \
28 EXPECT_EQUAL((blocked), (expected_blocked)); \
29 EXPECT_EQUAL(s2n_conn_get_current_message_type(conn), (expected_msg))
30
31 static S2N_RESULT s2n_test_client_and_server_new(struct s2n_connection **client_conn, struct s2n_connection **server_conn,
32 struct s2n_test_io_pair *io_pair)
33 {
34 *client_conn = s2n_connection_new(S2N_CLIENT);
35 EXPECT_NOT_NULL(*client_conn);
36 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(*client_conn, "default_tls13"));
37
38 *server_conn = s2n_connection_new(S2N_SERVER);
39 EXPECT_NOT_NULL(*server_conn);
40 EXPECT_SUCCESS(s2n_connection_set_blinding(*server_conn, S2N_SELF_SERVICE_BLINDING));
41 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(*server_conn, "default_tls13"));
42
43 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(io_pair));
44 EXPECT_SUCCESS(s2n_connections_set_io_pair(*client_conn, *server_conn, io_pair));
45
46 return S2N_RESULT_OK;
47 }
48
49 uint8_t s2n_allowed_reads = 0;
50 static int s2n_blocking_buffer_read(void *io_context, uint8_t *buf, uint32_t len)
51 {
52 struct s2n_stuffer *in = (struct s2n_stuffer *) io_context;
53
54 bool would_block = s2n_stuffer_data_available(in) < len;
55 if (would_block || !s2n_allowed_reads) {
56 errno = EAGAIN;
57 return -1;
58 }
59 s2n_allowed_reads--;
60 POSIX_GUARD(s2n_stuffer_read_bytes(in, buf, len));
61 return len;
62 }
63
64 uint8_t s2n_allowed_writes = 0;
65 static int s2n_blocking_buffer_write(void *io_context, const uint8_t *buf, uint32_t len)
66 {
67 struct s2n_stuffer *out = (struct s2n_stuffer *) io_context;
68
69 bool would_block = !out->growable && s2n_stuffer_space_remaining(out) < len;
70 if (would_block || !s2n_allowed_writes) {
71 errno = EAGAIN;
72 return -1;
73 }
74 s2n_allowed_writes--;
75 POSIX_GUARD(s2n_stuffer_write_bytes(out, buf, len));
76 return len;
77 }
78
79 static S2N_RESULT s2n_test_set_blocking_stuffer_io(
80 struct s2n_connection *client_conn, struct s2n_connection *server_conn,
81 struct s2n_stuffer *client_in, struct s2n_stuffer *server_in)
82 {
83 RESULT_GUARD_POSIX(s2n_stuffer_alloc(client_in, S2N_TLS_MAXIMUM_RECORD_LENGTH));
84 RESULT_GUARD_POSIX(s2n_stuffer_alloc(server_in, S2N_TLS_MAXIMUM_RECORD_LENGTH));
85
86 RESULT_GUARD_POSIX(s2n_connection_set_recv_cb(client_conn, &s2n_blocking_buffer_read));
87 RESULT_GUARD_POSIX(s2n_connection_set_recv_ctx(client_conn, client_in));
88 RESULT_GUARD_POSIX(s2n_connection_set_recv_cb(server_conn, &s2n_blocking_buffer_read));
89 RESULT_GUARD_POSIX(s2n_connection_set_recv_ctx(server_conn, server_in));
90
91 RESULT_GUARD_POSIX(s2n_connection_set_send_cb(client_conn, &s2n_blocking_buffer_write));
92 RESULT_GUARD_POSIX(s2n_connection_set_send_ctx(client_conn, server_in));
93 RESULT_GUARD_POSIX(s2n_connection_set_send_cb(server_conn, &s2n_blocking_buffer_write));
94 RESULT_GUARD_POSIX(s2n_connection_set_send_ctx(server_conn, client_in));
95
96 return S2N_RESULT_OK;
97 }
98
99 int main(int argc, char **argv)
100 {
101 BEGIN_TEST();
102
103 if (!s2n_is_tls13_fully_supported()) {
104 END_TEST();
105 }
106
107 const uint8_t test_data[] = "hello world";
108
109 /* Malformed record: empty handshake record */
110 uint8_t malformed_record[] = {
111 TLS_HANDSHAKE, 0x03, 0x03, 0x00, 0x04,
112 TLS_FINISHED, 0x00, 0x00, 0x00
113 };
114
115 DEFER_CLEANUP(struct s2n_psk *test_psk = s2n_external_psk_new(), s2n_psk_free);
116 EXPECT_SUCCESS(s2n_psk_set_identity(test_psk, test_data, sizeof(test_data)));
117 EXPECT_SUCCESS(s2n_psk_set_secret(test_psk, test_data, sizeof(test_data)));
118 EXPECT_SUCCESS(s2n_psk_configure_early_data(test_psk, UINT32_MAX, 0x13, 0x01));
119
120 DEFER_CLEANUP(struct s2n_psk *test_psk_without_early_data = s2n_external_psk_new(), s2n_psk_free);
121 EXPECT_SUCCESS(s2n_psk_set_identity(test_psk_without_early_data, test_data, sizeof(test_data)));
122 EXPECT_SUCCESS(s2n_psk_set_secret(test_psk_without_early_data, test_data, sizeof(test_data)));
123
124 DEFER_CLEANUP(struct s2n_psk *test_psk_with_wrong_early_data = s2n_external_psk_new(), s2n_psk_free);
125 EXPECT_SUCCESS(s2n_psk_set_identity(test_psk_with_wrong_early_data, test_data, sizeof(test_data)));
126 EXPECT_SUCCESS(s2n_psk_set_secret(test_psk_with_wrong_early_data, test_data, sizeof(test_data)));
127 EXPECT_SUCCESS(s2n_psk_configure_early_data(test_psk_with_wrong_early_data, UINT32_MAX, 0x13, 0x03));
128 EXPECT_SUCCESS(s2n_psk_set_application_protocol(test_psk_with_wrong_early_data, test_data, sizeof(test_data)));
129
130 /* Test s2n_send_early_data */
131 {
132 /* Safety checks */
133 {
134 struct s2n_connection conn = { 0 };
135 conn.mode = S2N_CLIENT;
136 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
137 ssize_t data_size = 0;
138 uint8_t data = 0;
139 EXPECT_FAILURE_WITH_ERRNO(s2n_send_early_data(NULL, &data, 1, &data_size, &blocked), S2N_ERR_NULL);
140 EXPECT_FAILURE_WITH_ERRNO(s2n_send_early_data(&conn, &data, 1, NULL, &blocked), S2N_ERR_NULL);
141 EXPECT_FAILURE_WITH_ERRNO(s2n_send_early_data(&conn, &data, 1, &data_size, NULL), S2N_ERR_NULL);
142
143 conn.mode = S2N_SERVER;
144 EXPECT_FAILURE_WITH_ERRNO(s2n_send_early_data(&conn, &data, 1, &data_size, &blocked), S2N_ERR_SERVER_MODE);
145 };
146
147 /* Propagate errors from s2n_negotiate */
148 {
149 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
150 EXPECT_NOT_NULL(client_conn);
151 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, "default_tls13"));
152 EXPECT_SUCCESS(s2n_connection_append_psk(client_conn, test_psk));
153
154 DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free);
155 DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free);
156 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, S2N_DEFAULT_RECORD_LENGTH));
157 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, S2N_DEFAULT_RECORD_LENGTH));
158 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, client_conn));
159
160 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&input, malformed_record, sizeof(malformed_record)));
161
162 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
163 ssize_t data_size = 0;
164
165 EXPECT_FAILURE_WITH_ERRNO(s2n_send_early_data(client_conn, test_data, sizeof(test_data), &data_size, &blocked),
166 S2N_ERR_BAD_MESSAGE);
167 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), SERVER_HELLO);
168 EXPECT_EQUAL(data_size, 0);
169
170 EXPECT_SUCCESS(s2n_stuffer_free(&input));
171 EXPECT_SUCCESS(s2n_stuffer_free(&output));
172 EXPECT_SUCCESS(s2n_connection_free(client_conn));
173 };
174
175 /* Propagate errors from s2n_send */
176 {
177 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
178 EXPECT_NOT_NULL(client_conn);
179 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, "default_tls13"));
180 EXPECT_SUCCESS(s2n_connection_append_psk(client_conn, test_psk));
181
182 DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free);
183 DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free);
184 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, S2N_DEFAULT_RECORD_LENGTH));
185 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, client_conn));
186
187 /* Indicate that we're already sending. That will cause an error. */
188 client_conn->send_in_use = true;
189
190 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
191 ssize_t data_size = 0;
192
193 EXPECT_FAILURE_WITH_ERRNO(s2n_send_early_data(client_conn, test_data, sizeof(test_data), &data_size, &blocked),
194 S2N_ERR_REENTRANCY);
195 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), SERVER_HELLO);
196 EXPECT_EQUAL(data_size, 0);
197
198 EXPECT_SUCCESS(s2n_stuffer_free(&output));
199 EXPECT_SUCCESS(s2n_connection_free(client_conn));
200 };
201 };
202
203 /* s2n_recv_early_data */
204 {
205 /* Safety checks */
206 {
207 struct s2n_connection conn = { 0 };
208 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
209 ssize_t data_size = 0;
210 uint8_t data = 0;
211 EXPECT_FAILURE_WITH_ERRNO(s2n_recv_early_data(NULL, &data, 1, &data_size, &blocked), S2N_ERR_NULL);
212 EXPECT_FAILURE_WITH_ERRNO(s2n_recv_early_data(&conn, &data, 1, NULL, &blocked), S2N_ERR_NULL);
213 EXPECT_FAILURE_WITH_ERRNO(s2n_recv_early_data(&conn, &data, 1, &data_size, NULL), S2N_ERR_NULL);
214
215 conn.mode = S2N_CLIENT;
216 EXPECT_FAILURE_WITH_ERRNO(s2n_recv_early_data(&conn, &data, 1, &data_size, &blocked), S2N_ERR_CLIENT_MODE);
217 };
218
219 /* Propagate errors from s2n_negotiate */
220 {
221 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
222 EXPECT_NOT_NULL(server_conn);
223 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
224 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "default_tls13"));
225 EXPECT_SUCCESS(s2n_connection_append_psk(server_conn, test_psk));
226
227 DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free);
228 DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free);
229 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, S2N_DEFAULT_RECORD_LENGTH));
230 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, server_conn));
231
232 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&input, malformed_record, sizeof(malformed_record)));
233
234 uint8_t payload[BUFFER_SIZE] = { 0 };
235 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
236 ssize_t data_size = 0;
237
238 EXPECT_FAILURE_WITH_ERRNO(s2n_recv_early_data(server_conn, payload, sizeof(payload), &data_size, &blocked),
239 S2N_ERR_BAD_MESSAGE);
240 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), CLIENT_HELLO);
241 EXPECT_EQUAL(data_size, 0);
242
243 EXPECT_SUCCESS(s2n_stuffer_free(&input));
244 EXPECT_SUCCESS(s2n_connection_free(server_conn));
245 };
246
247 /* Propagate errors from s2n_recv */
248 {
249 struct s2n_connection *client_conn = NULL, *server_conn = NULL;
250 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
251 EXPECT_OK(s2n_test_client_and_server_new(&client_conn, &server_conn, &io_pair));
252 EXPECT_SUCCESS(s2n_connection_append_psk(client_conn, test_psk));
253 EXPECT_SUCCESS(s2n_connection_append_psk(server_conn, test_psk));
254
255 /* Indicate that we're already receiving. That will cause an error. */
256 server_conn->recv_in_use = true;
257
258 uint8_t payload[BUFFER_SIZE] = { 0 };
259 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
260 ssize_t data_size = 0;
261
262 EXPECT_BLOCKED_ON_IO(s2n_send_early_data(client_conn, test_data, sizeof(test_data), &data_size, &blocked));
263 EXPECT_BLOCKED_ON(client_conn, blocked, S2N_BLOCKED_ON_READ, SERVER_HELLO);
264
265 EXPECT_FAILURE_WITH_ERRNO(s2n_recv_early_data(server_conn, payload, sizeof(payload), &data_size, &blocked),
266 S2N_ERR_REENTRANCY);
267 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), END_OF_EARLY_DATA);
268 EXPECT_EQUAL(data_size, 0);
269
270 EXPECT_SUCCESS(s2n_connection_free(client_conn));
271 EXPECT_SUCCESS(s2n_connection_free(server_conn));
272 };
273 };
274
275 /* Test sending and receiving early data */
276 {
277 /* Send zero-length early data */
278 {
279 struct s2n_connection *client_conn = NULL, *server_conn = NULL;
280 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
281 EXPECT_OK(s2n_test_client_and_server_new(&client_conn, &server_conn, &io_pair));
282
283 EXPECT_SUCCESS(s2n_connection_append_psk(client_conn, test_psk));
284 EXPECT_SUCCESS(s2n_connection_append_psk(server_conn, test_psk));
285
286 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
287 ssize_t data_size = 0;
288 s2n_early_data_status_t status = 0;
289
290 EXPECT_BLOCKED_ON_IO(s2n_send_early_data(client_conn, NULL, 0, &data_size, &blocked));
291 EXPECT_BLOCKED_ON(client_conn, blocked, S2N_BLOCKED_ON_READ, SERVER_HELLO);
292 EXPECT_EQUAL(data_size, 0);
293
294 EXPECT_SUCCESS(s2n_connection_get_early_data_status(client_conn, &status));
295 EXPECT_EQUAL(status, S2N_EARLY_DATA_STATUS_OK);
296
297 EXPECT_BLOCKED_ON_IO(s2n_recv_early_data(server_conn, NULL, 0, &data_size, &blocked));
298 EXPECT_BLOCKED_ON(server_conn, blocked, S2N_BLOCKED_ON_READ, END_OF_EARLY_DATA);
299 EXPECT_EQUAL(data_size, 0);
300
301 EXPECT_SUCCESS(s2n_connection_get_early_data_status(server_conn, &status));
302 EXPECT_EQUAL(status, S2N_EARLY_DATA_STATUS_OK);
303
304 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
305
306 EXPECT_SUCCESS(s2n_connection_free(client_conn));
307 EXPECT_SUCCESS(s2n_connection_free(server_conn));
308 };
309
310 /* Send early data once */
311 {
312 struct s2n_connection *client_conn = NULL, *server_conn = NULL;
313 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
314 EXPECT_OK(s2n_test_client_and_server_new(&client_conn, &server_conn, &io_pair));
315
316 EXPECT_SUCCESS(s2n_connection_append_psk(client_conn, test_psk));
317 EXPECT_SUCCESS(s2n_connection_append_psk(server_conn, test_psk));
318
319 uint8_t actual_payload[BUFFER_SIZE] = { 0 };
320 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
321 ssize_t data_size = 0;
322 s2n_early_data_status_t status = 0;
323
324 EXPECT_BLOCKED_ON_IO(s2n_send_early_data(client_conn, test_data, sizeof(test_data), &data_size, &blocked));
325 EXPECT_BLOCKED_ON(client_conn, blocked, S2N_BLOCKED_ON_READ, SERVER_HELLO);
326 EXPECT_EQUAL(data_size, sizeof(test_data));
327
328 EXPECT_SUCCESS(s2n_connection_get_early_data_status(client_conn, &status));
329 EXPECT_EQUAL(status, S2N_EARLY_DATA_STATUS_OK);
330
331 EXPECT_BLOCKED_ON_IO(s2n_recv_early_data(server_conn, actual_payload, sizeof(actual_payload), &data_size, &blocked));
332 EXPECT_BLOCKED_ON(server_conn, blocked, S2N_BLOCKED_ON_READ, END_OF_EARLY_DATA);
333 EXPECT_EQUAL(data_size, sizeof(test_data));
334 EXPECT_BYTEARRAY_EQUAL(actual_payload, test_data, sizeof(test_data));
335
336 EXPECT_SUCCESS(s2n_connection_get_early_data_status(server_conn, &status));
337 EXPECT_EQUAL(status, S2N_EARLY_DATA_STATUS_OK);
338
339 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
340
341 EXPECT_SUCCESS(s2n_connection_free(client_conn));
342 EXPECT_SUCCESS(s2n_connection_free(server_conn));
343 };
344
345 /* Receive early data too large for buffer */
346 {
347 struct s2n_connection *client_conn = NULL, *server_conn = NULL;
348 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
349 EXPECT_OK(s2n_test_client_and_server_new(&client_conn, &server_conn, &io_pair));
350
351 EXPECT_SUCCESS(s2n_connection_append_psk(client_conn, test_psk));
352 EXPECT_SUCCESS(s2n_connection_append_psk(server_conn, test_psk));
353
354 uint8_t actual_payload[BUFFER_SIZE] = { 0 };
355 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
356 ssize_t data_size = 0;
357
358 EXPECT_BLOCKED_ON_IO(s2n_send_early_data(client_conn, test_data, sizeof(test_data), &data_size, &blocked));
359 EXPECT_BLOCKED_ON(client_conn, blocked, S2N_BLOCKED_ON_READ, SERVER_HELLO);
360 EXPECT_EQUAL(data_size, sizeof(test_data));
361
362 EXPECT_BLOCKED_ON_EARLY_DATA(s2n_recv_early_data(server_conn, NULL, 0, &data_size, &blocked));
363 EXPECT_BLOCKED_ON(server_conn, blocked, S2N_BLOCKED_ON_EARLY_DATA, END_OF_EARLY_DATA);
364 EXPECT_EQUAL(data_size, 0);
365
366 EXPECT_BLOCKED_ON_EARLY_DATA(s2n_recv_early_data(server_conn, actual_payload, 1, &data_size, &blocked));
367 EXPECT_BLOCKED_ON(server_conn, blocked, S2N_BLOCKED_ON_EARLY_DATA, END_OF_EARLY_DATA);
368 EXPECT_EQUAL(data_size, 1);
369 EXPECT_BYTEARRAY_EQUAL(actual_payload, test_data, 1);
370
371 /* Remaining early data should block handshake.
372 * We can't successfully call s2n_negotiate again until we've drained all the early data
373 * via s2n_recv_early_data. For safety, we are not allowed to arbitrarily discard any early data.
374 */
375 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, client_conn),
376 S2N_ERR_BAD_MESSAGE);
377
378 /* Pretend we didn't test the above error condition.
379 * The S2N_ERR_BAD_MESSAGE error triggered S2N to close the connection. */
380 s2n_atomic_flag_clear(&server_conn->write_closed);
381 s2n_atomic_flag_clear(&client_conn->write_closed);
382
383 /* Read the remaining early data properly */
384 EXPECT_SUCCESS(s2n_recv_early_data(server_conn, actual_payload, sizeof(actual_payload),
385 &data_size, &blocked));
386
387 EXPECT_NOT_BLOCKED(server_conn, blocked, APPLICATION_DATA);
388 EXPECT_EQUAL(data_size, sizeof(test_data) - 1);
389 EXPECT_BYTEARRAY_EQUAL(actual_payload, test_data + 1, sizeof(test_data) - 1);
390
391 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
392
393 EXPECT_SUCCESS(s2n_connection_free(client_conn));
394 EXPECT_SUCCESS(s2n_connection_free(server_conn));
395 };
396
397 /* Send multiple early data messages */
398 {
399 struct s2n_connection *client_conn = NULL, *server_conn = NULL;
400 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
401 EXPECT_OK(s2n_test_client_and_server_new(&client_conn, &server_conn, &io_pair));
402
403 EXPECT_SUCCESS(s2n_connection_append_psk(client_conn, test_psk));
404 EXPECT_SUCCESS(s2n_connection_append_psk(server_conn, test_psk));
405
406 uint8_t actual_payload[BUFFER_SIZE] = { 0 };
407 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
408 ssize_t data_size = 0;
409
410 EXPECT_BLOCKED_ON_IO(s2n_send_early_data(client_conn, test_data, sizeof(test_data), &data_size, &blocked));
411 EXPECT_BLOCKED_ON(client_conn, blocked, S2N_BLOCKED_ON_READ, SERVER_HELLO);
412 EXPECT_EQUAL(data_size, sizeof(test_data));
413
414 EXPECT_BLOCKED_ON_IO(s2n_recv_early_data(server_conn, actual_payload, sizeof(actual_payload), &data_size, &blocked));
415 EXPECT_BLOCKED_ON(server_conn, blocked, S2N_BLOCKED_ON_READ, END_OF_EARLY_DATA);
416 EXPECT_EQUAL(data_size, sizeof(test_data));
417 EXPECT_BYTEARRAY_EQUAL(actual_payload, test_data, sizeof(test_data));
418
419 for (size_t i = 0; i < 10; i++) {
420 EXPECT_SUCCESS(s2n_send_early_data(client_conn, test_data, sizeof(test_data),
421 &data_size, &blocked));
422 EXPECT_NOT_BLOCKED(client_conn, blocked, END_OF_EARLY_DATA);
423 EXPECT_EQUAL(data_size, sizeof(test_data));
424
425 EXPECT_BLOCKED_ON_IO(s2n_recv_early_data(server_conn, actual_payload, sizeof(actual_payload), &data_size, &blocked));
426 EXPECT_BLOCKED_ON(client_conn, blocked, S2N_BLOCKED_ON_READ, END_OF_EARLY_DATA);
427 EXPECT_EQUAL(data_size, sizeof(test_data));
428 EXPECT_BYTEARRAY_EQUAL(actual_payload, test_data, sizeof(test_data));
429 }
430
431 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
432
433 EXPECT_SUCCESS(s2n_connection_free(client_conn));
434 EXPECT_SUCCESS(s2n_connection_free(server_conn));
435 };
436
437 /* Receive and combine multiple early data records */
438 {
439 struct s2n_connection *client_conn = NULL, *server_conn = NULL;
440 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
441 EXPECT_OK(s2n_test_client_and_server_new(&client_conn, &server_conn, &io_pair));
442
443 EXPECT_SUCCESS(s2n_connection_append_psk(client_conn, test_psk));
444 EXPECT_SUCCESS(s2n_connection_append_psk(server_conn, test_psk));
445
446 uint8_t actual_payload[BUFFER_SIZE] = { 0 };
447 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
448 ssize_t data_size = 0;
449
450 const size_t send_count = 5;
451 for (size_t i = 0; i < send_count; i++) {
452 EXPECT_BLOCKED_ON_IO(s2n_send_early_data(client_conn, test_data, sizeof(test_data), &data_size, &blocked));
453 EXPECT_BLOCKED_ON(client_conn, blocked, S2N_BLOCKED_ON_READ, SERVER_HELLO);
454 EXPECT_EQUAL(data_size, sizeof(test_data));
455 }
456
457 EXPECT_BLOCKED_ON_IO(s2n_recv_early_data(server_conn, actual_payload, sizeof(actual_payload), &data_size, &blocked));
458 EXPECT_BLOCKED_ON(server_conn, blocked, S2N_BLOCKED_ON_READ, END_OF_EARLY_DATA);
459 EXPECT_EQUAL(data_size, sizeof(test_data) * send_count);
460
461 struct s2n_blob payload_blob = { 0 };
462 struct s2n_stuffer payload_stuffer = { 0 };
463 EXPECT_SUCCESS(s2n_blob_init(&payload_blob, actual_payload, sizeof(actual_payload)));
464 EXPECT_SUCCESS(s2n_stuffer_init(&payload_stuffer, &payload_blob));
465 EXPECT_SUCCESS(s2n_stuffer_skip_write(&payload_stuffer, data_size));
466
467 uint8_t payload_chunk[sizeof(test_data)] = { 0 };
468 for (size_t i = 0; i < send_count; i++) {
469 EXPECT_SUCCESS(s2n_stuffer_read_bytes(&payload_stuffer, payload_chunk, sizeof(test_data)));
470 EXPECT_BYTEARRAY_EQUAL(payload_chunk, test_data, sizeof(test_data));
471 }
472 EXPECT_EQUAL(s2n_stuffer_data_available(&payload_stuffer), 0);
473
474 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
475
476 EXPECT_SUCCESS(s2n_connection_free(client_conn));
477 EXPECT_SUCCESS(s2n_connection_free(server_conn));
478 };
479
480 /* Early data not requested */
481 {
482 struct s2n_connection *client_conn = NULL, *server_conn = NULL;
483 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
484 EXPECT_OK(s2n_test_client_and_server_new(&client_conn, &server_conn, &io_pair));
485
486 EXPECT_SUCCESS(s2n_connection_append_psk(client_conn, test_psk_with_wrong_early_data));
487 EXPECT_SUCCESS(s2n_connection_append_psk(server_conn, test_psk));
488
489 uint8_t actual_payload[BUFFER_SIZE] = { 0 };
490 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
491 ssize_t data_size = 0;
492 s2n_early_data_status_t status = 0;
493
494 EXPECT_SUCCESS(s2n_send_early_data(client_conn, test_data, sizeof(test_data),
495 &data_size, &blocked));
496 EXPECT_NOT_BLOCKED(client_conn, blocked, SERVER_HELLO);
497 EXPECT_EQUAL(data_size, 0);
498
499 EXPECT_SUCCESS(s2n_connection_get_early_data_status(client_conn, &status));
500 EXPECT_EQUAL(status, S2N_EARLY_DATA_STATUS_NOT_REQUESTED);
501
502 EXPECT_SUCCESS(s2n_recv_early_data(server_conn, actual_payload, sizeof(actual_payload),
503 &data_size, &blocked));
504 EXPECT_NOT_BLOCKED(server_conn, blocked, CLIENT_FINISHED);
505 EXPECT_EQUAL(data_size, 0);
506
507 EXPECT_SUCCESS(s2n_connection_get_early_data_status(server_conn, &status));
508 EXPECT_EQUAL(status, S2N_EARLY_DATA_STATUS_NOT_REQUESTED);
509
510 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
511
512 EXPECT_SUCCESS(s2n_connection_free(client_conn));
513 EXPECT_SUCCESS(s2n_connection_free(server_conn));
514 };
515
516 /* Early data rejected */
517 {
518 struct s2n_connection *client_conn = NULL, *server_conn = NULL;
519 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
520 EXPECT_OK(s2n_test_client_and_server_new(&client_conn, &server_conn, &io_pair));
521
522 EXPECT_SUCCESS(s2n_connection_append_psk(client_conn, test_psk));
523 EXPECT_SUCCESS(s2n_connection_append_psk(server_conn, test_psk_with_wrong_early_data));
524
525 uint8_t actual_payload[BUFFER_SIZE] = { 0 };
526 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
527 ssize_t data_size = 0;
528 s2n_early_data_status_t status = 0;
529
530 EXPECT_BLOCKED_ON_IO(s2n_send_early_data(client_conn, test_data, sizeof(test_data), &data_size, &blocked));
531 EXPECT_BLOCKED_ON(client_conn, blocked, S2N_BLOCKED_ON_READ, SERVER_HELLO);
532 EXPECT_EQUAL(data_size, sizeof(test_data));
533
534 EXPECT_SUCCESS(s2n_connection_get_early_data_status(client_conn, &status));
535 EXPECT_EQUAL(status, S2N_EARLY_DATA_STATUS_OK);
536
537 EXPECT_SUCCESS(s2n_recv_early_data(server_conn, actual_payload, sizeof(actual_payload),
538 &data_size, &blocked));
539 EXPECT_NOT_BLOCKED(server_conn, blocked, CLIENT_FINISHED);
540 EXPECT_EQUAL(data_size, 0);
541
542 EXPECT_SUCCESS(s2n_connection_get_early_data_status(server_conn, &status));
543 EXPECT_EQUAL(status, S2N_EARLY_DATA_STATUS_REJECTED);
544
545 EXPECT_SUCCESS(s2n_send_early_data(client_conn, test_data, sizeof(test_data),
546 &data_size, &blocked));
547 EXPECT_NOT_BLOCKED(client_conn, blocked, APPLICATION_DATA);
548 EXPECT_EQUAL(data_size, 0);
549
550 EXPECT_SUCCESS(s2n_connection_get_early_data_status(client_conn, &status));
551 EXPECT_EQUAL(status, S2N_EARLY_DATA_STATUS_REJECTED);
552
553 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
554
555 EXPECT_SUCCESS(s2n_connection_free(client_conn));
556 EXPECT_SUCCESS(s2n_connection_free(server_conn));
557 };
558 };
559
560 /* Test blocking behavior when sending and receiving early data.
561 *
562 * We override the send and recv callbacks to allow us to block on every
563 * call to s2n_negotiate, s2n_send, and s2n_recv. This lets us exercise all
564 * possible blocking paths.
565 */
566 {
567 /* To read a record, we need to both read its header and read its data */
568 const uint8_t full_record_reads = 2;
569
570 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
571 EXPECT_NOT_NULL(client_conn);
572 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, "default_tls13"));
573 EXPECT_SUCCESS(s2n_connection_append_psk(client_conn, test_psk));
574
575 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
576 EXPECT_NOT_NULL(server_conn);
577 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
578 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "default_tls13"));
579 EXPECT_SUCCESS(s2n_connection_append_psk(server_conn, test_psk));
580
581 DEFER_CLEANUP(struct s2n_stuffer client_in = { 0 }, s2n_stuffer_free);
582 DEFER_CLEANUP(struct s2n_stuffer server_in = { 0 }, s2n_stuffer_free);
583 EXPECT_OK(s2n_test_set_blocking_stuffer_io(client_conn, server_conn, &client_in, &server_in));
584
585 uint8_t actual_payload[BUFFER_SIZE] = { 0 };
586 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
587 ssize_t data_size = 0;
588
589 /* Block writing the ClientHello */
590 s2n_allowed_writes = 0;
591 EXPECT_BLOCKED_ON_IO(s2n_send_early_data(client_conn, test_data, sizeof(test_data), &data_size, &blocked));
592 EXPECT_BLOCKED_ON(client_conn, blocked, S2N_BLOCKED_ON_WRITE, CLIENT_HELLO);
593 EXPECT_EQUAL(data_size, 0);
594
595 /* Write the ClientHello, but block on writing the Client CCS message */
596 s2n_allowed_writes = 1;
597 EXPECT_BLOCKED_ON_IO(s2n_send_early_data(client_conn, test_data, sizeof(test_data), &data_size, &blocked));
598 EXPECT_BLOCKED_ON(client_conn, blocked, S2N_BLOCKED_ON_WRITE, CLIENT_CHANGE_CIPHER_SPEC);
599 EXPECT_EQUAL(data_size, 0);
600
601 /* Write the Client CCS message, but block on writing the early data */
602 s2n_allowed_writes = 1;
603 EXPECT_BLOCKED_ON_IO(s2n_send_early_data(client_conn, test_data, sizeof(test_data), &data_size, &blocked));
604 EXPECT_BLOCKED_ON(client_conn, blocked, S2N_BLOCKED_ON_WRITE, SERVER_HELLO);
605 EXPECT_EQUAL(data_size, 0);
606
607 /* Write the early data, but block on reading the ServerHello */
608 s2n_allowed_writes = 1;
609 s2n_allowed_reads = 0;
610 EXPECT_BLOCKED_ON_IO(s2n_send_early_data(client_conn, test_data, sizeof(test_data), &data_size, &blocked));
611 EXPECT_BLOCKED_ON(client_conn, blocked, S2N_BLOCKED_ON_READ, SERVER_HELLO);
612 EXPECT_EQUAL(data_size, sizeof(test_data));
613
614 /* Block reading the ClientHello */
615 s2n_allowed_reads = 0;
616 EXPECT_BLOCKED_ON_IO(s2n_recv_early_data(server_conn, actual_payload, sizeof(actual_payload), &data_size, &blocked));
617 EXPECT_BLOCKED_ON(server_conn, blocked, S2N_BLOCKED_ON_READ, CLIENT_HELLO);
618 EXPECT_EQUAL(data_size, 0);
619
620 /* Read the ClientHello, but block on writing the ServerHello */
621 s2n_allowed_reads = full_record_reads;
622 s2n_allowed_writes = 0;
623 EXPECT_BLOCKED_ON_IO(s2n_recv_early_data(server_conn, actual_payload, sizeof(actual_payload), &data_size, &blocked));
624 EXPECT_BLOCKED_ON(server_conn, blocked, S2N_BLOCKED_ON_WRITE, SERVER_HELLO);
625 EXPECT_EQUAL(data_size, 0);
626
627 /* Write the server messages */
628 while (s2n_conn_get_current_message_type(server_conn) != SERVER_FINISHED) {
629 s2n_allowed_writes = 1;
630 EXPECT_FAILURE_WITH_ERRNO(s2n_recv_early_data(server_conn, actual_payload, sizeof(actual_payload), &data_size, &blocked),
631 S2N_ERR_IO_BLOCKED);
632 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE);
633 EXPECT_EQUAL(data_size, 0);
634 };
635
636 /* Write the last server message, but block on reading the early data */
637 s2n_allowed_writes = 1;
638 s2n_allowed_reads = 0;
639 EXPECT_BLOCKED_ON_IO(s2n_recv_early_data(server_conn, actual_payload, sizeof(actual_payload), &data_size, &blocked));
640 EXPECT_BLOCKED_ON(server_conn, blocked, S2N_BLOCKED_ON_READ, END_OF_EARLY_DATA);
641 EXPECT_EQUAL(data_size, 0);
642
643 /* Read the Client CCS message */
644 s2n_allowed_reads = full_record_reads;
645 EXPECT_BLOCKED_ON_IO(s2n_recv_early_data(server_conn, actual_payload, sizeof(actual_payload), &data_size, &blocked));
646 EXPECT_BLOCKED_ON(server_conn, blocked, S2N_BLOCKED_ON_READ, END_OF_EARLY_DATA);
647 EXPECT_EQUAL(data_size, 0);
648
649 /* Read the early data */
650 s2n_allowed_reads = full_record_reads;
651 EXPECT_BLOCKED_ON_IO(s2n_recv_early_data(server_conn, actual_payload, sizeof(actual_payload), &data_size, &blocked));
652 EXPECT_BLOCKED_ON(server_conn, blocked, S2N_BLOCKED_ON_READ, END_OF_EARLY_DATA);
653 EXPECT_EQUAL(data_size, sizeof(test_data));
654
655 /* Read the ServerHello, but block on writing more early data */
656 s2n_allowed_reads = full_record_reads;
657 s2n_allowed_writes = 0;
658 EXPECT_BLOCKED_ON_IO(s2n_send_early_data(client_conn, test_data, sizeof(test_data), &data_size, &blocked));
659 EXPECT_BLOCKED_ON(client_conn, blocked, S2N_BLOCKED_ON_WRITE, ENCRYPTED_EXTENSIONS);
660 EXPECT_EQUAL(data_size, 0);
661
662 EXPECT_SUCCESS(s2n_connection_free(client_conn));
663 EXPECT_SUCCESS(s2n_connection_free(server_conn));
664 };
665
666 /* Test blocking behavior with partial successes when sending early data.
667 *
668 * Parital successes should be reported to the application.
669 * Partial successes should count towards the early data limit.
670 */
671 {
672 const uint32_t max_early_data = UINT16_MAX;
673 DEFER_CLEANUP(struct s2n_psk *psk_with_early_data_limit = s2n_external_psk_new(), s2n_psk_free);
674 EXPECT_SUCCESS(s2n_psk_set_identity(psk_with_early_data_limit, test_data, sizeof(test_data)));
675 EXPECT_SUCCESS(s2n_psk_set_secret(psk_with_early_data_limit, test_data, sizeof(test_data)));
676 EXPECT_SUCCESS(s2n_psk_configure_early_data(psk_with_early_data_limit, max_early_data, 0x13, 0x01));
677
678 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
679 s2n_connection_ptr_free);
680 EXPECT_NOT_NULL(client_conn);
681 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, "default_tls13"));
682 EXPECT_SUCCESS(s2n_connection_append_psk(client_conn, psk_with_early_data_limit));
683
684 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
685 s2n_connection_ptr_free);
686 EXPECT_NOT_NULL(server_conn);
687 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "default_tls13"));
688 EXPECT_SUCCESS(s2n_connection_append_psk(server_conn, psk_with_early_data_limit));
689
690 DEFER_CLEANUP(struct s2n_stuffer client_in = { 0 }, s2n_stuffer_free);
691 DEFER_CLEANUP(struct s2n_stuffer server_in = { 0 }, s2n_stuffer_free);
692 EXPECT_OK(s2n_test_set_blocking_stuffer_io(client_conn, server_conn, &client_in, &server_in));
693
694 /* We will send more than one record worth of data,
695 * so that we can block after one record worth.
696 */
697 uint8_t large_data[S2N_DEFAULT_FRAGMENT_LENGTH * 2] = "hello world";
698
699 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
700 ssize_t data_size = 0;
701
702 /* Block after each record write.
703 * Eventually, we expect a partial success when we write one record of early data.
704 * That partial success should correctly report the number of bytes written,
705 * and record the correct number of early data bytes consumed.
706 */
707 size_t max_attempts = 100;
708 size_t attempts = 0;
709 while (true) {
710 s2n_allowed_writes = 1;
711 EXPECT_FAILURE_WITH_ERRNO(s2n_send_early_data(client_conn, large_data, sizeof(large_data), &data_size, &blocked),
712 S2N_ERR_IO_BLOCKED);
713
714 if (data_size != 0) {
715 /* We blocked on both reading the next handshake message (S2N_BLOCKED_ON_READ)
716 * and writing the early data (S2N_BLOCKED_ON_WRITE).
717 * We prefer the negotiate result.
718 */
719 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ);
720
721 /* Part of the early data sent */
722 EXPECT_EQUAL(data_size, S2N_DEFAULT_FRAGMENT_LENGTH);
723 EXPECT_EQUAL(client_conn->early_data_bytes, S2N_DEFAULT_FRAGMENT_LENGTH);
724 break;
725 } else {
726 /* We blocked sending a handshake message,
727 * since we haven't gotten to early data yet.
728 */
729 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE);
730
731 /* No early data sent yet */
732 EXPECT_EQUAL(client_conn->early_data_bytes, 0);
733 }
734
735 attempts++;
736 EXPECT_TRUE(attempts < max_attempts);
737 }
738 };
739
740 /* Known-value early data tests.
741 * The RFC8848s ClientHello uses x25519, which is only available if evp APIs are supported.
742 * Otherwise, skip these tests. */
743 if (s2n_is_evp_apis_supported()) {
744 DEFER_CLEANUP(struct s2n_psk resumption_psk = { 0 }, s2n_psk_wipe);
745 EXPECT_OK(s2n_psk_init(&resumption_psk, S2N_PSK_TYPE_RESUMPTION));
746 struct s2n_psk *known_psk = &resumption_psk;
747
748 /**
749 *= https://www.rfc-editor.org/rfc/rfc8448#section-3
750 *= type=test
751 *# {server} generate resumption secret "tls13 resumption":
752 *#
753 *# PRK (32 octets): 7d f2 35 f2 03 1d 2a 05 12 87 d0 2b 02 41 b0 bf
754 *# da f8 6c c8 56 23 1f 2d 5a ba 46 c4 34 ec 19 6c
755 *#
756 *# hash (2 octets): 00 00
757 *#
758 *# info (22 octets): 00 20 10 74 6c 73 31 33 20 72 65 73 75 6d 70 74
759 *# 69 6f 6e 02 00 00
760 *#
761 *# expanded (32 octets): 4e cd 0e b6 ec 3b 4d 87 f5 d6 02 8f 92 2c
762 *# a4 c5 85 1a 27 7f d4 13 11 c9 e6 2d 2c 94 92 e1 c4 f3
763 */
764 S2N_BLOB_FROM_HEX(psk_secret, "4e cd 0e b6 ec 3b 4d 87 f5 d6 02 8f 92 2c \
765 a4 c5 85 1a 27 7f d4 13 11 c9 e6 2d 2c 94 92 e1 c4 f3");
766
767 EXPECT_SUCCESS(s2n_psk_set_secret(known_psk, psk_secret.data, psk_secret.size));
768
769 /**
770 *= https://www.rfc-editor.org/rfc/rfc8448#section-3
771 *= type=test
772 *# {server} construct a NewSessionTicket handshake message:
773 *#
774 *# NewSessionTicket (205 octets): 04 00 00 c9 00 00 00 1e fa d6 aa
775 *# c5 02 00 00 00 b2 2c 03 5d 82 93 59 ee 5f f7 af 4e c9 00 00 00
776 *# 00 26 2a 64 94 dc 48 6d 2c 8a 34 cb 33 fa 90 bf 1b 00 70 ad 3c
777 *# 49 88 83 c9 36 7c 09 a2 be 78 5a bc 55 cd 22 60 97 a3 a9 82 11
778 *# 72 83 f8 2a 03 a1 43 ef d3 ff 5d d3 6d 64 e8 61 be 7f d6 1d 28
779 *# 27 db 27 9c ce 14 50 77 d4 54 a3 66 4d 4e 6d a4 d2 9e e0 37 25
780 *# a6 a4 da fc d0 fc 67 d2 ae a7 05 29 51 3e 3d a2 67 7f a5 90 6c
781 *# 5b 3f 7d 8f 92 f2 28 bd a4 0d da 72 14 70 f9 fb f2 97 b5 ae a6
782 *# 17 64 6f ac 5c 03 27 2e 97 07 27 c6 21 a7 91 41 ef 5f 7d e6 50
783 *# 5e 5b fb c3 88 e9 33 43 69 40 93 93 4a e4 d3 57 00 08 00 2a 00
784 *# 04 00 00 04 00
785 */
786 /* Skip past the message type, message size, ticket lifetime,
787 * ticket age add, nonce, and ticket size:
788 * 04 00 00 c9 00 00 00 1e fa d6 aa
789 * c5 02 00 00 00 b2
790 */
791 S2N_BLOB_FROM_HEX(psk_identity, "2c 03 5d 82 93 59 ee 5f f7 af 4e c9 00 00 00 \
792 00 26 2a 64 94 dc 48 6d 2c 8a 34 cb 33 fa 90 bf 1b 00 70 ad 3c \
793 49 88 83 c9 36 7c 09 a2 be 78 5a bc 55 cd 22 60 97 a3 a9 82 11 \
794 72 83 f8 2a 03 a1 43 ef d3 ff 5d d3 6d 64 e8 61 be 7f d6 1d 28 \
795 27 db 27 9c ce 14 50 77 d4 54 a3 66 4d 4e 6d a4 d2 9e e0 37 25 \
796 a6 a4 da fc d0 fc 67 d2 ae a7 05 29 51 3e 3d a2 67 7f a5 90 6c \
797 5b 3f 7d 8f 92 f2 28 bd a4 0d da 72 14 70 f9 fb f2 97 b5 ae a6 \
798 17 64 6f ac 5c 03 27 2e 97 07 27 c6 21 a7 91 41 ef 5f 7d e6 50 \
799 5e 5b fb c3 88 e9 33 43 69 40 93 93 4a e4 d3 57");
800 EXPECT_SUCCESS(s2n_psk_set_identity(known_psk, psk_identity.data, psk_identity.size));
801 /* Skip past the total extensions size, early data extension type,
802 * and early data extension size: 00 08 00 2a 00
803 * 04
804 */
805 const uint32_t max_early_data = 0x00000400;
806 EXPECT_SUCCESS(s2n_psk_configure_early_data(known_psk, max_early_data, 0x13, 0x01));
807
808 /** ClientHello record
809 *
810 *= https://www.rfc-editor.org/rfc/rfc8448#section-4
811 *= type=test
812 *#
813 *# complete record (517 octets): 16 03 01 02 00 01 00 01 fc 03 03 1b
814 *# c3 ce b6 bb e3 9c ff 93 83 55 b5 a5 0a db 6d b2 1b 7a 6a f6 49
815 *# d7 b4 bc 41 9d 78 76 48 7d 95 00 00 06 13 01 13 03 13 02 01 00
816 *# 01 cd 00 00 00 0b 00 09 00 00 06 73 65 72 76 65 72 ff 01 00 01
817 *# 00 00 0a 00 14 00 12 00 1d 00 17 00 18 00 19 01 00 01 01 01 02
818 *# 01 03 01 04 00 33 00 26 00 24 00 1d 00 20 e4 ff b6 8a c0 5f 8d
819 *# 96 c9 9d a2 66 98 34 6c 6b e1 64 82 ba dd da fe 05 1a 66 b4 f1
820 *# 8d 66 8f 0b 00 2a 00 00 00 2b 00 03 02 03 04 00 0d 00 20 00 1e
821 *# 04 03 05 03 06 03 02 03 08 04 08 05 08 06 04 01 05 01 06 01 02
822 *# 01 04 02 05 02 06 02 02 02 00 2d 00 02 01 01 00 1c 00 02 40 01
823 *# 00 15 00 57 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
824 *# 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
825 *# 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
826 *# 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
827 *# 00 00 00 00 00 00 00 00 29 00 dd 00 b8 00 b2 2c 03 5d 82 93 59
828 *# ee 5f f7 af 4e c9 00 00 00 00 26 2a 64 94 dc 48 6d 2c 8a 34 cb
829 *# 33 fa 90 bf 1b 00 70 ad 3c 49 88 83 c9 36 7c 09 a2 be 78 5a bc
830 *# 55 cd 22 60 97 a3 a9 82 11 72 83 f8 2a 03 a1 43 ef d3 ff 5d d3
831 *# 6d 64 e8 61 be 7f d6 1d 28 27 db 27 9c ce 14 50 77 d4 54 a3 66
832 *# 4d 4e 6d a4 d2 9e e0 37 25 a6 a4 da fc d0 fc 67 d2 ae a7 05 29
833 *# 51 3e 3d a2 67 7f a5 90 6c 5b 3f 7d 8f 92 f2 28 bd a4 0d da 72
834 *# 14 70 f9 fb f2 97 b5 ae a6 17 64 6f ac 5c 03 27 2e 97 07 27 c6
835 *# 21 a7 91 41 ef 5f 7d e6 50 5e 5b fb c3 88 e9 33 43 69 40 93 93
836 *# 4a e4 d3 57 fa d6 aa cb 00 21 20 3a dd 4f b2 d8 fd f8 22 a0 ca
837 *# 3c f7 67 8e f5 e8 8d ae 99 01 41 c5 92 4d 57 bb 6f a3 1b 9e 5f
838 *# 9d
839 */
840 S2N_BLOB_FROM_HEX(ch_record, "16 03 01 02 00 01 00 01 fc 03 03 1b \
841 c3 ce b6 bb e3 9c ff 93 83 55 b5 a5 0a db 6d b2 1b 7a 6a f6 49 \
842 d7 b4 bc 41 9d 78 76 48 7d 95 00 00 06 13 01 13 03 13 02 01 00 \
843 01 cd 00 00 00 0b 00 09 00 00 06 73 65 72 76 65 72 ff 01 00 01 \
844 00 00 0a 00 14 00 12 00 1d 00 17 00 18 00 19 01 00 01 01 01 02 \
845 01 03 01 04 00 33 00 26 00 24 00 1d 00 20 e4 ff b6 8a c0 5f 8d \
846 96 c9 9d a2 66 98 34 6c 6b e1 64 82 ba dd da fe 05 1a 66 b4 f1 \
847 8d 66 8f 0b 00 2a 00 00 00 2b 00 03 02 03 04 00 0d 00 20 00 1e \
848 04 03 05 03 06 03 02 03 08 04 08 05 08 06 04 01 05 01 06 01 02 \
849 01 04 02 05 02 06 02 02 02 00 2d 00 02 01 01 00 1c 00 02 40 01 \
850 00 15 00 57 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 \
851 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 \
852 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 \
853 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 \
854 00 00 00 00 00 00 00 00 29 00 dd 00 b8 00 b2 2c 03 5d 82 93 59 \
855 ee 5f f7 af 4e c9 00 00 00 00 26 2a 64 94 dc 48 6d 2c 8a 34 cb \
856 33 fa 90 bf 1b 00 70 ad 3c 49 88 83 c9 36 7c 09 a2 be 78 5a bc \
857 55 cd 22 60 97 a3 a9 82 11 72 83 f8 2a 03 a1 43 ef d3 ff 5d d3 \
858 6d 64 e8 61 be 7f d6 1d 28 27 db 27 9c ce 14 50 77 d4 54 a3 66 \
859 4d 4e 6d a4 d2 9e e0 37 25 a6 a4 da fc d0 fc 67 d2 ae a7 05 29 \
860 51 3e 3d a2 67 7f a5 90 6c 5b 3f 7d 8f 92 f2 28 bd a4 0d da 72 \
861 14 70 f9 fb f2 97 b5 ae a6 17 64 6f ac 5c 03 27 2e 97 07 27 c6 \
862 21 a7 91 41 ef 5f 7d e6 50 5e 5b fb c3 88 e9 33 43 69 40 93 93 \
863 4a e4 d3 57 fa d6 aa cb 00 21 20 3a dd 4f b2 d8 fd f8 22 a0 ca \
864 3c f7 67 8e f5 e8 8d ae 99 01 41 c5 92 4d 57 bb 6f a3 1b 9e 5f \
865 9d");
866
867 /* ApplicationData record containing early data
868 *
869 *= https://www.rfc-editor.org/rfc/rfc8448#section-4
870 *= type=test
871 *# {client} send application_data record:
872 *#
873 *# payload (6 octets): 41 42 43 44 45 46
874 *#
875 *# complete record (28 octets): 17 03 03 00 17 ab 1d f4 20 e7 5c 45
876 *# 7a 7c c5 d2 84 4f 76 d5 ae e4 b4 ed bf 04 9b e0
877 */
878 S2N_BLOB_FROM_HEX(payload, "41 42 43 44 45 46");
879 S2N_BLOB_FROM_HEX(early_record, "17 03 03 00 17 ab 1d f4 20 e7 5c 45 \
880 7a 7c c5 d2 84 4f 76 d5 ae e4 b4 ed bf 04 9b e0")
881
882 /* EndOfEarlyData record
883 *
884 *= https://www.rfc-editor.org/rfc/rfc8448#section-4
885 *= type=test
886 *#
887 *# complete record (26 octets): 17 03 03 00 15 ac a6 fc 94 48 41 29
888 *# 8d f9 95 93 72 5f 9b f9 75 44 29 b1 2f 09
889 */
890 S2N_BLOB_FROM_HEX(end_record, "17 03 03 00 15 ac a6 fc 94 48 41 29 \
891 8d f9 95 93 72 5f 9b f9 75 44 29 b1 2f 09");
892
893 /* Test s2n_recv_early_data without any blocking */
894 {
895 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
896 EXPECT_NOT_NULL(server_conn);
897 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
898 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "default_tls13"));
899 EXPECT_SUCCESS(s2n_connection_append_psk(server_conn, known_psk));
900 EXPECT_SUCCESS(s2n_connection_set_server_max_early_data_size(server_conn, max_early_data));
901 /* We need to explicitly set the psk_params type to skip our stateless session resumption recv
902 * code because the handshake traces we're using are meant for stateful session resumption.
903 * TODO: https://github.com/aws/s2n-tls/issues/2742 */
904 server_conn->psk_params.type = S2N_PSK_TYPE_EXTERNAL;
905
906 DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free);
907 DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free);
908 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, S2N_DEFAULT_RECORD_LENGTH));
909 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, S2N_DEFAULT_RECORD_LENGTH));
910 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, server_conn));
911
912 EXPECT_SUCCESS(s2n_stuffer_write(&input, &ch_record));
913 EXPECT_SUCCESS(s2n_stuffer_write(&input, &early_record));
914 EXPECT_SUCCESS(s2n_stuffer_write(&input, &end_record));
915
916 uint8_t actual_payload[BUFFER_SIZE] = { 0 };
917 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
918 ssize_t data_read = 0;
919
920 EXPECT_SUCCESS(s2n_recv_early_data(server_conn, actual_payload, sizeof(actual_payload),
921 &data_read, &blocked));
922 EXPECT_NOT_BLOCKED(server_conn, blocked, CLIENT_FINISHED);
923 EXPECT_EQUAL(data_read, payload.size);
924 EXPECT_BYTEARRAY_EQUAL(actual_payload, payload.data, payload.size);
925 EXPECT_EQUAL(server_conn->early_data_state, S2N_END_OF_EARLY_DATA);
926
927 EXPECT_SUCCESS(s2n_stuffer_free(&input));
928 EXPECT_SUCCESS(s2n_stuffer_free(&output));
929 EXPECT_SUCCESS(s2n_connection_free(server_conn));
930 };
931
932 /* Test s2n_recv_early_data with blocking */
933 {
934 /* When we block, we should continue to block regardless of how many times the API is called.
935 * Let's choose an arbitrary "retry" test value > 1.
936 */
937 const size_t repeat_count = 5;
938
939 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
940 EXPECT_NOT_NULL(server_conn);
941 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
942 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "default_tls13"));
943 EXPECT_SUCCESS(s2n_connection_append_psk(server_conn, known_psk));
944 EXPECT_SUCCESS(s2n_connection_set_server_max_early_data_size(server_conn, max_early_data));
945 /* We need to explicitly set the psk_params type to skip our stateless session resumption recv
946 * code because the handshake traces we're using are meant for stateful session resumption.
947 * TODO: https://github.com/aws/s2n-tls/issues/2742 */
948 server_conn->psk_params.type = S2N_PSK_TYPE_EXTERNAL;
949
950 DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free);
951 DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free);
952 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, S2N_DEFAULT_RECORD_LENGTH));
953 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, server_conn));
954
955 uint8_t actual_payload[BUFFER_SIZE] = { 0 };
956 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
957 ssize_t data_read = 0;
958
959 for (size_t i = 0; i < repeat_count; i++) {
960 EXPECT_BLOCKED_ON_IO(s2n_recv_early_data(server_conn, actual_payload, sizeof(actual_payload), &data_read, &blocked));
961 EXPECT_BLOCKED_ON(server_conn, blocked, S2N_BLOCKED_ON_READ, CLIENT_HELLO);
962 EXPECT_EQUAL(data_read, 0);
963 }
964
965 EXPECT_SUCCESS(s2n_stuffer_write(&input, &ch_record));
966
967 for (size_t i = 0; i < repeat_count; i++) {
968 EXPECT_BLOCKED_ON_IO(s2n_recv_early_data(server_conn, actual_payload, sizeof(actual_payload), &data_read, &blocked));
969 EXPECT_BLOCKED_ON(server_conn, blocked, S2N_BLOCKED_ON_WRITE, SERVER_HELLO);
970 EXPECT_EQUAL(data_read, 0);
971 }
972
973 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, S2N_DEFAULT_RECORD_LENGTH));
974
975 for (size_t i = 0; i < repeat_count; i++) {
976 EXPECT_BLOCKED_ON_IO(s2n_recv_early_data(server_conn, actual_payload, sizeof(actual_payload), &data_read, &blocked));
977 EXPECT_BLOCKED_ON(server_conn, blocked, S2N_BLOCKED_ON_READ, END_OF_EARLY_DATA);
978 EXPECT_EQUAL(data_read, 0);
979 }
980 EXPECT_EQUAL(server_conn->early_data_state, S2N_EARLY_DATA_ACCEPTED);
981
982 EXPECT_SUCCESS(s2n_stuffer_write(&input, &early_record));
983
984 EXPECT_BLOCKED_ON_IO(s2n_recv_early_data(server_conn, actual_payload, sizeof(actual_payload), &data_read, &blocked));
985 EXPECT_BLOCKED_ON(server_conn, blocked, S2N_BLOCKED_ON_READ, END_OF_EARLY_DATA);
986 EXPECT_EQUAL(data_read, payload.size);
987 EXPECT_BYTEARRAY_EQUAL(actual_payload, payload.data, payload.size);
988
989 for (size_t i = 0; i < repeat_count; i++) {
990 EXPECT_BLOCKED_ON_IO(s2n_recv_early_data(server_conn, actual_payload, sizeof(actual_payload), &data_read, &blocked));
991 EXPECT_BLOCKED_ON(server_conn, blocked, S2N_BLOCKED_ON_READ, END_OF_EARLY_DATA);
992 EXPECT_EQUAL(data_read, 0);
993 }
994 EXPECT_EQUAL(server_conn->early_data_state, S2N_EARLY_DATA_ACCEPTED);
995
996 EXPECT_SUCCESS(s2n_stuffer_write(&input, &end_record));
997
998 for (size_t i = 0; i < repeat_count; i++) {
999 EXPECT_SUCCESS(s2n_recv_early_data(server_conn, actual_payload, sizeof(actual_payload),
1000 &data_read, &blocked));
1001 EXPECT_NOT_BLOCKED(server_conn, blocked, CLIENT_FINISHED);
1002 EXPECT_EQUAL(data_read, 0);
1003 EXPECT_EQUAL(server_conn->early_data_state, S2N_END_OF_EARLY_DATA);
1004 }
1005
1006 EXPECT_BLOCKED_ON_IO(s2n_negotiate(server_conn, &blocked));
1007 EXPECT_BLOCKED_ON(server_conn, blocked, S2N_BLOCKED_ON_READ, CLIENT_FINISHED);
1008 EXPECT_EQUAL(server_conn->early_data_state, S2N_END_OF_EARLY_DATA);
1009
1010 EXPECT_SUCCESS(s2n_stuffer_free(&input));
1011 EXPECT_SUCCESS(s2n_stuffer_free(&output));
1012 EXPECT_SUCCESS(s2n_connection_free(server_conn));
1013 };
1014
1015 /* Test s2n_recv_early_data when early data not allowed for PSK */
1016 {
1017 struct s2n_psk psk_copy = *known_psk;
1018 EXPECT_SUCCESS(s2n_psk_configure_early_data(&psk_copy, 0, 0x13, 0x01));
1019 struct s2n_psk *known_psk_without_early_data = &psk_copy;
1020
1021 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
1022 EXPECT_NOT_NULL(server_conn);
1023 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
1024 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "default_tls13"));
1025 EXPECT_SUCCESS(s2n_connection_append_psk(server_conn, known_psk_without_early_data));
1026 EXPECT_SUCCESS(s2n_connection_set_server_max_early_data_size(server_conn, max_early_data));
1027 /* We need to explicitly set the psk_params type to skip our stateless session resumption recv
1028 * code because the handshake traces we're using are meant for stateful session resumption.
1029 * TODO: https://github.com/aws/s2n-tls/issues/2742 */
1030 server_conn->psk_params.type = S2N_PSK_TYPE_EXTERNAL;
1031
1032 DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free);
1033 DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free);
1034 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, S2N_DEFAULT_RECORD_LENGTH));
1035 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, S2N_DEFAULT_RECORD_LENGTH));
1036 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, server_conn));
1037
1038 EXPECT_SUCCESS(s2n_stuffer_write(&input, &ch_record));
1039 EXPECT_SUCCESS(s2n_stuffer_write(&input, &early_record));
1040 EXPECT_SUCCESS(s2n_stuffer_write(&input, &end_record));
1041
1042 uint8_t actual_payload[BUFFER_SIZE] = { 0 };
1043 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
1044 ssize_t data_read = 0;
1045
1046 EXPECT_SUCCESS(s2n_recv_early_data(server_conn, actual_payload, sizeof(actual_payload),
1047 &data_read, &blocked));
1048 EXPECT_NOT_BLOCKED(server_conn, blocked, CLIENT_HELLO);
1049 EXPECT_EQUAL(data_read, 0);
1050
1051 EXPECT_BLOCKED_ON_IO(s2n_negotiate(server_conn, &blocked));
1052 EXPECT_BLOCKED_ON(server_conn, blocked, S2N_BLOCKED_ON_READ, CLIENT_FINISHED);
1053 EXPECT_EQUAL(server_conn->early_data_state, S2N_EARLY_DATA_REJECTED);
1054
1055 EXPECT_SUCCESS(s2n_stuffer_free(&input));
1056 EXPECT_SUCCESS(s2n_stuffer_free(&output));
1057 EXPECT_SUCCESS(s2n_connection_free(server_conn));
1058 };
1059
1060 /* Test s2n_recv_early_data when early data rejected */
1061 {
1062 struct s2n_psk psk_copy = *known_psk;
1063 EXPECT_SUCCESS(s2n_psk_configure_early_data(&psk_copy, max_early_data, 0x13, 0x03));
1064 struct s2n_psk *known_psk_with_wrong_cipher_suite = &psk_copy;
1065
1066 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
1067 EXPECT_NOT_NULL(server_conn);
1068 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
1069 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "default_tls13"));
1070 EXPECT_SUCCESS(s2n_connection_append_psk(server_conn, known_psk_with_wrong_cipher_suite));
1071 EXPECT_SUCCESS(s2n_connection_set_server_max_early_data_size(server_conn, max_early_data));
1072 /* We need to explicitly set the psk_params type to skip our stateless session resumption recv
1073 * code because the handshake traces we're using are meant for stateful session resumption.
1074 * TODO: https://github.com/aws/s2n-tls/issues/2742 */
1075 server_conn->psk_params.type = S2N_PSK_TYPE_EXTERNAL;
1076
1077 DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free);
1078 DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free);
1079 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, S2N_DEFAULT_RECORD_LENGTH));
1080 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, S2N_DEFAULT_RECORD_LENGTH));
1081 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, server_conn));
1082
1083 EXPECT_SUCCESS(s2n_stuffer_write(&input, &ch_record));
1084 EXPECT_SUCCESS(s2n_stuffer_write(&input, &early_record));
1085 EXPECT_SUCCESS(s2n_stuffer_write(&input, &end_record));
1086
1087 uint8_t actual_payload[BUFFER_SIZE] = { 0 };
1088 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
1089 ssize_t data_read = 0;
1090
1091 EXPECT_SUCCESS(s2n_recv_early_data(server_conn, actual_payload, sizeof(actual_payload),
1092 &data_read, &blocked));
1093 EXPECT_NOT_BLOCKED(server_conn, blocked, CLIENT_FINISHED);
1094 EXPECT_EQUAL(data_read, 0);
1095 EXPECT_EQUAL(server_conn->early_data_state, S2N_EARLY_DATA_REJECTED);
1096
1097 EXPECT_SUCCESS(s2n_stuffer_free(&input));
1098 EXPECT_SUCCESS(s2n_stuffer_free(&output));
1099 EXPECT_SUCCESS(s2n_connection_free(server_conn));
1100 };
1101 }
1102
1103 END_TEST();
1104 }