Debian Codegraph @

crt/s2n/tests/unit/s2n_ktls_io_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 "crypto/s2n_sequence.h"
17 #include "s2n_test.h"
18 #include "testlib/s2n_ktls_test_utils.h"
19 #include "testlib/s2n_mem_testlib.h"
20 #include "testlib/s2n_testlib.h"
21 #include "tls/s2n_ktls.h"
22 #include "tls/s2n_tls.h"
23 #include "utils/s2n_random.h"
24
25 #define S2N_TEST_TO_SEND 10
26 #define S2N_TEST_MSG_IOVLEN 5
27
28 S2N_RESULT s2n_ktls_set_control_data(struct msghdr *msg, char *buf, size_t buf_size,
29 int cmsg_type, uint8_t record_type);
30 S2N_RESULT s2n_ktls_get_control_data(struct msghdr *msg, int cmsg_type, uint8_t *record_type);
31
32 /* Mock implementation used for validating failure behavior */
33 struct s2n_test_ktls_io_fail_ctx {
34 size_t errno_code;
35 size_t invoked_count;
36 };
37
38 static ssize_t s2n_test_ktls_sendmsg_fail(void *io_context, const struct msghdr *msg)
39 {
40 struct s2n_test_ktls_io_fail_ctx *io_ctx = (struct s2n_test_ktls_io_fail_ctx *) io_context;
41 POSIX_ENSURE_REF(io_ctx);
42 io_ctx->invoked_count++;
43 errno = io_ctx->errno_code;
44 return -1;
45 }
46
47 static ssize_t s2n_test_ktls_recvmsg_fail(void *io_context, struct msghdr *msg)
48 {
49 POSIX_ENSURE_REF(msg);
50
51 struct s2n_test_ktls_io_fail_ctx *io_ctx = (struct s2n_test_ktls_io_fail_ctx *) io_context;
52 POSIX_ENSURE_REF(io_ctx);
53 io_ctx->invoked_count++;
54 errno = io_ctx->errno_code;
55 return -1;
56 }
57
58 static ssize_t s2n_test_ktls_recvmsg_eof(void *io_context, struct msghdr *msg)
59 {
60 struct s2n_test_ktls_io_fail_ctx *io_ctx = (struct s2n_test_ktls_io_fail_ctx *) io_context;
61 POSIX_ENSURE_REF(io_ctx);
62 io_ctx->invoked_count++;
63 return 0;
64 }
65
66 static ssize_t s2n_test_ktls_sendmsg_mark_all_sent(void *io_context, const struct msghdr *msg)
67 {
68 size_t sent = 0;
69 for (size_t i = 0; i < msg->msg_iovlen; i++) {
70 sent += msg->msg_iov[i].iov_len;
71 }
72 return sent;
73 }
74
75 ssize_t s2n_test_ktls_recvmsg_io_stuffer_and_ctrunc(void *io_context, struct msghdr *msg)
76 {
77 POSIX_ENSURE_REF(msg);
78
79 /* The stuffer mock IO is used to ensure `cmsghdr` is otherwise properly constructed
80 * and that the failure occurs due to the MSG_CTRUNC flag. */
81 ssize_t ret = s2n_test_ktls_recvmsg_io_stuffer(io_context, msg);
82 POSIX_GUARD(ret);
83 msg->msg_flags = MSG_CTRUNC;
84 return ret;
85 }
86
87 static S2N_RESULT s2n_assert_seq_num_equal(struct s2n_blob actual_blob, uint64_t expected)
88 {
89 uint64_t actual = 0;
90 RESULT_GUARD_POSIX(s2n_sequence_number_to_uint64(&actual_blob, &actual));
91 RESULT_ENSURE(actual == expected, S2N_ERR_TEST_ASSERTION);
92 return S2N_RESULT_OK;
93 }
94
95 int main(int argc, char **argv)
96 {
97 BEGIN_TEST();
98
99 const uint8_t test_record_type = 43;
100 /* test data */
101 uint8_t test_data[S2N_TLS_MAXIMUM_FRAGMENT_LENGTH] = { 0 };
102 struct s2n_blob test_data_blob = { 0 };
103 EXPECT_SUCCESS(s2n_blob_init(&test_data_blob, test_data, sizeof(test_data)));
104 EXPECT_OK(s2n_get_public_random_data(&test_data_blob));
105
106 /* Test s2n_ktls_set_control_data and s2n_ktls_get_control_data */
107 {
108 /* Test: Safety */
109 {
110 struct msghdr msg = { 0 };
111 char buf[100] = { 0 };
112 EXPECT_ERROR_WITH_ERRNO(s2n_ktls_set_control_data(NULL, buf, sizeof(buf), 0, 0),
113 S2N_ERR_NULL);
114 EXPECT_ERROR_WITH_ERRNO(s2n_ktls_set_control_data(&msg, NULL, sizeof(buf), 0, 0),
115 S2N_ERR_NULL);
116 EXPECT_ERROR_WITH_ERRNO(s2n_ktls_set_control_data(&msg, buf, 0, 0, 0),
117 S2N_ERR_NULL);
118
119 uint8_t record_type = 0;
120 EXPECT_ERROR_WITH_ERRNO(s2n_ktls_get_control_data(NULL, 0, &record_type),
121 S2N_ERR_NULL);
122 EXPECT_ERROR_WITH_ERRNO(s2n_ktls_get_control_data(&msg, 0, NULL),
123 S2N_ERR_NULL);
124 };
125
126 /* Test: s2n_ktls_set_control_data msg is parseable by s2n_ktls_get_control_data */
127 {
128 const uint8_t set_record_type = 5;
129 struct msghdr msg = { 0 };
130 const int cmsg_type = 11;
131 char buf[100] = { 0 };
132 EXPECT_OK(s2n_ktls_set_control_data(&msg, buf, sizeof(buf), cmsg_type, set_record_type));
133
134 uint8_t get_record_type = 0;
135 EXPECT_OK(s2n_ktls_get_control_data(&msg, cmsg_type, &get_record_type));
136
137 EXPECT_EQUAL(set_record_type, get_record_type);
138 };
139
140 /* Test: s2n_ktls_get_control_data fails with unexpected cmsg_type */
141 {
142 const uint8_t set_record_type = 5;
143 struct msghdr msg = { 0 };
144 const int cmsg_type = 11;
145 char buf[100] = { 0 };
146 EXPECT_OK(s2n_ktls_set_control_data(&msg, buf, sizeof(buf), cmsg_type, set_record_type));
147
148 const int bad_cmsg_type = 99;
149 uint8_t get_record_type = 0;
150 EXPECT_ERROR_WITH_ERRNO(s2n_ktls_get_control_data(&msg, bad_cmsg_type, &get_record_type),
151 S2N_ERR_KTLS_BAD_CMSG);
152 };
153 };
154
155 /* Test s2n_ktls_sendmsg */
156 {
157 /* Safety */
158 {
159 struct s2n_test_ktls_io_stuffer ctx = { 0 };
160 struct iovec msg_iov_valid = { .iov_base = test_data, .iov_len = S2N_TEST_TO_SEND };
161 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
162 size_t bytes_written = 0;
163
164 EXPECT_ERROR_WITH_ERRNO(
165 s2n_ktls_sendmsg(NULL, test_record_type, &msg_iov_valid, 1, &blocked, &bytes_written),
166 S2N_ERR_IO);
167 EXPECT_ERROR_WITH_ERRNO(
168 s2n_ktls_sendmsg(&ctx, test_record_type, NULL, 1, &blocked, &bytes_written),
169 S2N_ERR_NULL);
170 EXPECT_ERROR_WITH_ERRNO(
171 s2n_ktls_sendmsg(&ctx, test_record_type, &msg_iov_valid, 1, NULL, &bytes_written),
172 S2N_ERR_NULL);
173 EXPECT_ERROR_WITH_ERRNO(
174 s2n_ktls_sendmsg(&ctx, test_record_type, &msg_iov_valid, 1, &blocked, NULL),
175 S2N_ERR_NULL);
176 };
177
178 /* Happy case: msg_iovlen = 1 */
179 {
180 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
181 s2n_connection_ptr_free);
182 DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer client_in = { 0 },
183 s2n_ktls_io_stuffer_free);
184 EXPECT_OK(s2n_test_init_ktls_io_stuffer_send(server, &client_in));
185
186 struct iovec msg_iov = { .iov_base = test_data, .iov_len = S2N_TEST_TO_SEND };
187 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
188 size_t bytes_written = 0;
189 EXPECT_OK(s2n_ktls_sendmsg(server->send_io_context, test_record_type,
190 &msg_iov, 1, &blocked, &bytes_written));
191 EXPECT_EQUAL(bytes_written, S2N_TEST_TO_SEND);
192 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
193
194 /* confirm sent data */
195 EXPECT_OK(s2n_test_validate_ancillary(&client_in, test_record_type, S2N_TEST_TO_SEND));
196 EXPECT_OK(s2n_test_validate_data(&client_in, test_data, S2N_TEST_TO_SEND));
197
198 EXPECT_EQUAL(client_in.sendmsg_invoked_count, 1);
199 };
200
201 /* Happy case: msg_iovlen > 1 */
202 {
203 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
204 s2n_connection_ptr_free);
205 DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer client_in = { 0 },
206 s2n_ktls_io_stuffer_free);
207 EXPECT_OK(s2n_test_init_ktls_io_stuffer_send(server, &client_in));
208
209 struct iovec msg_iov[S2N_TEST_MSG_IOVLEN] = { 0 };
210 size_t total_sent = 0;
211 for (size_t i = 0; i < S2N_TEST_MSG_IOVLEN; i++) {
212 msg_iov[i].iov_base = test_data + total_sent;
213 msg_iov[i].iov_len = S2N_TEST_TO_SEND;
214 total_sent += S2N_TEST_TO_SEND;
215 }
216
217 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
218 size_t bytes_written = 0;
219 EXPECT_OK(s2n_ktls_sendmsg(server->send_io_context, test_record_type,
220 msg_iov, S2N_TEST_MSG_IOVLEN, &blocked, &bytes_written));
221 EXPECT_EQUAL(bytes_written, total_sent);
222 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
223
224 /* confirm sent data */
225 EXPECT_OK(s2n_test_validate_ancillary(&client_in, test_record_type, total_sent));
226 EXPECT_OK(s2n_test_validate_data(&client_in, test_data, total_sent));
227 /* validate only 1 record was sent */
228 EXPECT_EQUAL(s2n_stuffer_data_available(&client_in.ancillary_buffer),
229 S2N_TEST_KTLS_MOCK_HEADER_SIZE);
230
231 EXPECT_EQUAL(client_in.sendmsg_invoked_count, 1);
232 };
233
234 /* Simulate a blocked network and handle a S2N_ERR_IO_BLOCKED error */
235 {
236 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
237 s2n_connection_ptr_free);
238 DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer client_in = { 0 },
239 s2n_ktls_io_stuffer_free);
240 EXPECT_OK(s2n_test_init_ktls_io_stuffer_send(server, &client_in));
241 /* disable growable to simulate blocked/network buffer full */
242 client_in.data_buffer.growable = false;
243
244 struct iovec msg_iov = { .iov_base = test_data, .iov_len = S2N_TEST_TO_SEND };
245 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
246 size_t blocked_invoked_count = 5;
247 size_t bytes_written = 0;
248 for (size_t i = 0; i < blocked_invoked_count; i++) {
249 EXPECT_ERROR_WITH_ERRNO(
250 s2n_ktls_sendmsg(server->send_io_context, test_record_type,
251 &msg_iov, 1, &blocked, &bytes_written),
252 S2N_ERR_IO_BLOCKED);
253 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE);
254 }
255
256 /* enable growable to unblock write */
257 /* cppcheck-suppress redundantAssignment */
258 client_in.data_buffer.growable = true;
259 EXPECT_OK(s2n_ktls_sendmsg(server->send_io_context, test_record_type,
260 &msg_iov, 1, &blocked, &bytes_written));
261 EXPECT_EQUAL(bytes_written, S2N_TEST_TO_SEND);
262
263 /* confirm sent data */
264 EXPECT_OK(s2n_test_validate_ancillary(&client_in, test_record_type, S2N_TEST_TO_SEND));
265 EXPECT_OK(s2n_test_validate_data(&client_in, test_data, S2N_TEST_TO_SEND));
266
267 EXPECT_EQUAL(client_in.sendmsg_invoked_count, blocked_invoked_count + 1);
268 };
269
270 /* Both EWOULDBLOCK and EAGAIN should return a S2N_ERR_IO_BLOCKED error */
271 {
272 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
273 s2n_connection_ptr_free);
274 struct s2n_test_ktls_io_fail_ctx io_ctx = { 0 };
275 EXPECT_OK(s2n_ktls_set_sendmsg_cb(server, s2n_test_ktls_sendmsg_fail, &io_ctx));
276
277 struct iovec msg_iov = { .iov_base = test_data, .iov_len = S2N_TEST_TO_SEND };
278 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
279 size_t bytes_written = 0;
280
281 io_ctx.errno_code = EWOULDBLOCK;
282 EXPECT_ERROR_WITH_ERRNO(
283 s2n_ktls_sendmsg(server->send_io_context, test_record_type,
284 &msg_iov, 1, &blocked, &bytes_written),
285 S2N_ERR_IO_BLOCKED);
286
287 /* cppcheck-suppress redundantAssignment */
288 io_ctx.errno_code = EAGAIN;
289 EXPECT_ERROR_WITH_ERRNO(
290 s2n_ktls_sendmsg(server->send_io_context, test_record_type,
291 &msg_iov, 1, &blocked, &bytes_written),
292 S2N_ERR_IO_BLOCKED);
293
294 EXPECT_EQUAL(io_ctx.invoked_count, 2);
295 };
296
297 /* Handle a S2N_ERR_IO error */
298 {
299 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
300 s2n_connection_ptr_free);
301 struct s2n_test_ktls_io_fail_ctx io_ctx = {
302 .errno_code = EINVAL,
303 };
304 EXPECT_OK(s2n_ktls_set_sendmsg_cb(server, s2n_test_ktls_sendmsg_fail, &io_ctx));
305
306 struct iovec msg_iov = { .iov_base = test_data, .iov_len = S2N_TEST_TO_SEND };
307 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
308 size_t bytes_written = 0;
309 EXPECT_ERROR_WITH_ERRNO(
310 s2n_ktls_sendmsg(server->send_io_context, test_record_type,
311 &msg_iov, 1, &blocked, &bytes_written),
312 S2N_ERR_IO);
313 /* Blocked status intentionally not reset to preserve legacy s2n_send behavior */
314 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE);
315
316 EXPECT_EQUAL(io_ctx.invoked_count, 1);
317 };
318
319 /* Should be able to invoke s2n_ktls_sendmsg with '0' data */
320 {
321 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
322 s2n_connection_ptr_free);
323 DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer client_in = { 0 },
324 s2n_ktls_io_stuffer_free);
325 EXPECT_OK(s2n_test_init_ktls_io_stuffer_send(server, &client_in));
326
327 struct iovec msg_iov = { .iov_base = test_data, .iov_len = S2N_TEST_TO_SEND };
328 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
329 size_t bytes_written = 0;
330
331 size_t iovlen_zero = 0;
332 EXPECT_OK(s2n_ktls_sendmsg(server->send_io_context, test_record_type,
333 &msg_iov, iovlen_zero, &blocked, &bytes_written));
334 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
335 EXPECT_EQUAL(bytes_written, 0);
336
337 struct iovec msg_iov_len_zero = { .iov_base = test_data, .iov_len = 0 };
338 EXPECT_OK(s2n_ktls_sendmsg(server->send_io_context, test_record_type,
339 &msg_iov_len_zero, 1, &blocked, &bytes_written));
340 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
341 EXPECT_EQUAL(bytes_written, 0);
342
343 EXPECT_EQUAL(client_in.sendmsg_invoked_count, 2);
344 };
345 };
346
347 /* Test s2n_ktls_recvmsg */
348 {
349 /* Safety */
350 {
351 struct s2n_test_ktls_io_stuffer ctx = { 0 };
352 uint8_t recv_buf[S2N_TLS_MAXIMUM_FRAGMENT_LENGTH] = { 0 };
353 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
354 uint8_t recv_record_type = 0;
355 size_t bytes_read = 0;
356
357 EXPECT_ERROR_WITH_ERRNO(
358 s2n_ktls_recvmsg(NULL, &recv_record_type, recv_buf, S2N_TEST_TO_SEND, &blocked, &bytes_read),
359 S2N_ERR_IO);
360 EXPECT_ERROR_WITH_ERRNO(
361 s2n_ktls_recvmsg(&ctx, NULL, recv_buf, S2N_TEST_TO_SEND, &blocked, &bytes_read),
362 S2N_ERR_NULL);
363 EXPECT_ERROR_WITH_ERRNO(
364 s2n_ktls_recvmsg(&ctx, &recv_record_type, NULL, S2N_TEST_TO_SEND, &blocked, &bytes_read),
365 S2N_ERR_NULL);
366 EXPECT_ERROR_WITH_ERRNO(
367 s2n_ktls_recvmsg(&ctx, &recv_record_type, recv_buf, S2N_TEST_TO_SEND, NULL, &bytes_read),
368 S2N_ERR_NULL);
369 EXPECT_ERROR_WITH_ERRNO(
370 s2n_ktls_recvmsg(&ctx, &recv_record_type, recv_buf, S2N_TEST_TO_SEND, &blocked, NULL),
371 S2N_ERR_NULL);
372
373 size_t to_recv_zero = 0;
374 EXPECT_ERROR_WITH_ERRNO(
375 s2n_ktls_recvmsg(&ctx, &recv_record_type, recv_buf, to_recv_zero, &blocked, &bytes_read),
376 S2N_ERR_SAFETY);
377 };
378
379 /* Happy case: send/recv data using sendmsg/recvmsg */
380 {
381 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
382 s2n_connection_ptr_free);
383 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
384 s2n_connection_ptr_free);
385 DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer_pair io_pair = { 0 },
386 s2n_ktls_io_stuffer_pair_free);
387 EXPECT_OK(s2n_test_init_ktls_io_stuffer(server, client, &io_pair));
388
389 struct iovec msg_iov = { .iov_base = test_data, .iov_len = S2N_TEST_TO_SEND };
390 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
391 size_t bytes_written = 0;
392 EXPECT_OK(s2n_ktls_sendmsg(server->send_io_context, test_record_type,
393 &msg_iov, 1, &blocked, &bytes_written));
394 EXPECT_EQUAL(bytes_written, S2N_TEST_TO_SEND);
395
396 uint8_t recv_buf[S2N_TLS_MAXIMUM_FRAGMENT_LENGTH] = { 0 };
397 uint8_t recv_record_type = 0;
398 size_t bytes_read = 0;
399 EXPECT_OK(s2n_ktls_recvmsg(client->recv_io_context, &recv_record_type,
400 recv_buf, S2N_TEST_TO_SEND, &blocked, &bytes_read));
401 EXPECT_BYTEARRAY_EQUAL(test_data, recv_buf, bytes_read);
402 EXPECT_EQUAL(bytes_read, bytes_written);
403
404 EXPECT_EQUAL(io_pair.client_in.sendmsg_invoked_count, 1);
405 EXPECT_EQUAL(io_pair.client_in.recvmsg_invoked_count, 1);
406 };
407
408 /* Simulate blocked and handle a S2N_ERR_IO_BLOCKED error */
409 {
410 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
411 s2n_connection_ptr_free);
412 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
413 s2n_connection_ptr_free);
414 DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer_pair io_pair = { 0 },
415 s2n_ktls_io_stuffer_pair_free);
416 EXPECT_OK(s2n_test_init_ktls_io_stuffer(server, client, &io_pair));
417
418 uint8_t recv_buf[S2N_TLS_MAXIMUM_FRAGMENT_LENGTH] = { 0 };
419 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
420 size_t blocked_invoked_count = 5;
421 uint8_t recv_record_type = 0;
422 size_t bytes_read = 0;
423 /* recv should block since there is no data */
424 for (size_t i = 0; i < blocked_invoked_count; i++) {
425 EXPECT_ERROR_WITH_ERRNO(
426 s2n_ktls_recvmsg(client->recv_io_context, &recv_record_type,
427 recv_buf, S2N_TEST_TO_SEND, &blocked, &bytes_read),
428 S2N_ERR_IO_BLOCKED);
429 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ);
430 }
431
432 /* send data to unblock */
433 struct iovec msg_iov = { .iov_base = test_data, .iov_len = S2N_TEST_TO_SEND };
434 size_t bytes_written = 0;
435 EXPECT_OK(s2n_ktls_sendmsg(server->send_io_context, test_record_type,
436 &msg_iov, 1, &blocked, &bytes_written));
437 EXPECT_EQUAL(bytes_written, S2N_TEST_TO_SEND);
438
439 EXPECT_OK(s2n_ktls_recvmsg(client->recv_io_context, &recv_record_type,
440 recv_buf, S2N_TEST_TO_SEND, &blocked, &bytes_read));
441 EXPECT_BYTEARRAY_EQUAL(test_data, recv_buf, bytes_read);
442 EXPECT_EQUAL(bytes_read, bytes_written);
443
444 /* recv should block again since we have read all the data */
445 for (size_t i = 0; i < blocked_invoked_count; i++) {
446 EXPECT_ERROR_WITH_ERRNO(
447 s2n_ktls_recvmsg(client->recv_io_context, &recv_record_type,
448 recv_buf, S2N_TEST_TO_SEND, &blocked, &bytes_read),
449 S2N_ERR_IO_BLOCKED);
450 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ);
451 }
452
453 EXPECT_EQUAL(io_pair.client_in.recvmsg_invoked_count, (blocked_invoked_count * 2) + 1);
454 EXPECT_EQUAL(io_pair.client_in.sendmsg_invoked_count, 1);
455 };
456
457 /* Both EWOULDBLOCK and EAGAIN should return a S2N_ERR_IO_BLOCKED error */
458 {
459 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
460 s2n_connection_ptr_free);
461 struct s2n_test_ktls_io_fail_ctx io_ctx = { 0 };
462 EXPECT_OK(s2n_ktls_set_recvmsg_cb(client, s2n_test_ktls_recvmsg_fail, &io_ctx));
463
464 uint8_t recv_buf[S2N_TLS_MAXIMUM_FRAGMENT_LENGTH] = { 0 };
465 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
466 uint8_t recv_record_type = 0;
467 size_t bytes_read = 0;
468
469 io_ctx.errno_code = EWOULDBLOCK;
470 EXPECT_ERROR_WITH_ERRNO(
471 s2n_ktls_recvmsg(client->recv_io_context, &recv_record_type,
472 recv_buf, S2N_TEST_TO_SEND, &blocked, &bytes_read),
473 S2N_ERR_IO_BLOCKED);
474 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ);
475
476 /* cppcheck-suppress redundantAssignment */
477 io_ctx.errno_code = EAGAIN;
478 EXPECT_ERROR_WITH_ERRNO(
479 s2n_ktls_recvmsg(client->recv_io_context, &recv_record_type,
480 recv_buf, S2N_TEST_TO_SEND, &blocked, &bytes_read),
481 S2N_ERR_IO_BLOCKED);
482 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ);
483
484 EXPECT_EQUAL(io_ctx.invoked_count, 2);
485 };
486
487 /* Handle a S2N_ERR_IO error */
488 {
489 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
490 s2n_connection_ptr_free);
491 struct s2n_test_ktls_io_fail_ctx io_ctx = {
492 .errno_code = EINVAL,
493 };
494 EXPECT_OK(s2n_ktls_set_recvmsg_cb(client, s2n_test_ktls_recvmsg_fail, &io_ctx));
495
496 uint8_t recv_buf[S2N_TLS_MAXIMUM_FRAGMENT_LENGTH] = { 0 };
497 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
498 uint8_t recv_record_type = 0;
499 size_t bytes_read = 0;
500 EXPECT_ERROR_WITH_ERRNO(
501 s2n_ktls_recvmsg(client->recv_io_context, &recv_record_type,
502 recv_buf, S2N_TEST_TO_SEND, &blocked, &bytes_read),
503 S2N_ERR_IO);
504 /* Blocked status intentionally not reset to preserve legacy s2n_send behavior */
505 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ);
506
507 EXPECT_EQUAL(io_ctx.invoked_count, 1);
508 };
509
510 /* Simulate EOF and handle a S2N_ERR_CLOSED error */
511 {
512 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
513 s2n_connection_ptr_free);
514 struct s2n_test_ktls_io_fail_ctx io_ctx = { 0 };
515 EXPECT_OK(s2n_ktls_set_recvmsg_cb(client, s2n_test_ktls_recvmsg_eof, &io_ctx));
516
517 uint8_t recv_buf[S2N_TLS_MAXIMUM_FRAGMENT_LENGTH] = { 0 };
518 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
519 uint8_t recv_record_type = 0;
520 size_t bytes_read = 0;
521 EXPECT_ERROR_WITH_ERRNO(
522 s2n_ktls_recvmsg(client->recv_io_context, &recv_record_type,
523 recv_buf, S2N_TEST_TO_SEND, &blocked, &bytes_read),
524 S2N_ERR_CLOSED);
525 /* Blocked status intentionally not reset to preserve legacy s2n_send behavior */
526 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ);
527
528 EXPECT_EQUAL(io_ctx.invoked_count, 1);
529 };
530
531 /* Simulate control message truncated via MSG_CTRUNC flag and handle a S2N_ERR_KTLS_BAD_CMSG error */
532 {
533 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
534 s2n_connection_ptr_free);
535 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
536 s2n_connection_ptr_free);
537 DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer_pair io_pair = { 0 },
538 s2n_ktls_io_stuffer_pair_free);
539 EXPECT_OK(s2n_test_init_ktls_io_stuffer(server, client, &io_pair));
540 /* override the client recvmsg callback to add a MSG_CTRUNC flag to msghdr before returning */
541 EXPECT_OK(s2n_ktls_set_recvmsg_cb(client, s2n_test_ktls_recvmsg_io_stuffer_and_ctrunc, &io_pair.client_in));
542
543 struct iovec msg_iov = { .iov_base = test_data, .iov_len = S2N_TEST_TO_SEND };
544 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
545 size_t bytes_written = 0;
546 EXPECT_OK(s2n_ktls_sendmsg(server->send_io_context, test_record_type,
547 &msg_iov, 1, &blocked, &bytes_written));
548 EXPECT_EQUAL(bytes_written, S2N_TEST_TO_SEND);
549
550 uint8_t recv_buf[S2N_TLS_MAXIMUM_FRAGMENT_LENGTH] = { 0 };
551 uint8_t recv_record_type = 0;
552 size_t bytes_read = 0;
553 EXPECT_ERROR_WITH_ERRNO(
554 s2n_ktls_recvmsg(client->recv_io_context, &recv_record_type,
555 recv_buf, S2N_TEST_TO_SEND, &blocked, &bytes_read),
556 S2N_ERR_KTLS_BAD_CMSG);
557 /* Blocked status intentionally not reset to preserve legacy s2n_send behavior */
558 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ);
559
560 EXPECT_EQUAL(io_pair.client_in.sendmsg_invoked_count, 1);
561 EXPECT_EQUAL(io_pair.client_in.recvmsg_invoked_count, 1);
562 };
563 };
564
565 /* Test s2n_ktls_send */
566 {
567 const size_t test_iov_lens[] = { 10, 0, 1, 5, 100, 100, 10 };
568
569 /* Safety */
570 {
571 struct s2n_connection conn = { 0 };
572 s2n_blocked_status blocked = 0;
573 const struct iovec test_iovec = { .iov_base = &blocked, .iov_len = 1 };
574
575 EXPECT_FAILURE_WITH_ERRNO(
576 s2n_ktls_sendv_with_offset(NULL, &test_iovec, 1, 0, &blocked),
577 S2N_ERR_NULL);
578 EXPECT_FAILURE_WITH_ERRNO(
579 s2n_ktls_sendv_with_offset(&conn, NULL, 1, 0, &blocked),
580 S2N_ERR_NULL);
581 EXPECT_FAILURE_WITH_ERRNO(
582 s2n_ktls_sendv_with_offset(&conn, NULL, 1, 1, &blocked),
583 S2N_ERR_NULL);
584 EXPECT_FAILURE_WITH_ERRNO(
585 s2n_ktls_sendv_with_offset(&conn, &test_iovec, 1, 0, NULL),
586 S2N_ERR_NULL);
587 EXPECT_FAILURE_WITH_ERRNO(
588 s2n_ktls_sendv_with_offset(&conn, &test_iovec, -1, 0, &blocked),
589 S2N_ERR_INVALID_ARGUMENT);
590 EXPECT_FAILURE_WITH_ERRNO(
591 s2n_ktls_sendv_with_offset(&conn, &test_iovec, 1, -1, &blocked),
592 S2N_ERR_INVALID_ARGUMENT);
593 };
594
595 /* Test: Basic send with single iovec */
596 {
597 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
598 s2n_connection_ptr_free);
599 EXPECT_NOT_NULL(conn);
600
601 DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer out = { 0 },
602 s2n_ktls_io_stuffer_free);
603 EXPECT_OK(s2n_test_init_ktls_io_stuffer_send(conn, &out));
604
605 const struct iovec test_iovec = {
606 .iov_base = test_data,
607 .iov_len = sizeof(test_data),
608 };
609
610 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
611 EXPECT_EQUAL(
612 s2n_ktls_sendv_with_offset(conn, &test_iovec, 1, 0, &blocked),
613 sizeof(test_data));
614
615 EXPECT_EQUAL(out.sendmsg_invoked_count, 1);
616 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
617 EXPECT_OK(s2n_test_validate_ancillary(&out, TLS_APPLICATION_DATA, sizeof(test_data)));
618 EXPECT_OK(s2n_test_validate_data(&out, test_data, sizeof(test_data)));
619 };
620
621 /* Test: Handle IO error from sendmsg */
622 {
623 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
624 s2n_connection_ptr_free);
625 EXPECT_NOT_NULL(conn);
626
627 struct s2n_test_ktls_io_fail_ctx io_ctx = { .errno_code = EINVAL };
628 EXPECT_OK(s2n_ktls_set_sendmsg_cb(conn, s2n_test_ktls_sendmsg_fail, &io_ctx));
629
630 const struct iovec test_iovec = {
631 .iov_base = test_data,
632 .iov_len = sizeof(test_data),
633 };
634
635 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
636 EXPECT_FAILURE_WITH_ERRNO(
637 s2n_ktls_sendv_with_offset(conn, &test_iovec, 1, 0, &blocked),
638 S2N_ERR_IO);
639 EXPECT_EQUAL(io_ctx.invoked_count, 1);
640 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE);
641 };
642
643 /* Test: Send nothing */
644 {
645 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
646 s2n_connection_ptr_free);
647 EXPECT_NOT_NULL(conn);
648
649 DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer out = { 0 },
650 s2n_ktls_io_stuffer_free);
651 EXPECT_OK(s2n_test_init_ktls_io_stuffer_send(conn, &out));
652
653 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
654 const struct iovec test_iovec = {
655 .iov_base = test_data,
656 .iov_len = 0,
657 };
658
659 /* Send nothing with zero-length iovec array */
660 EXPECT_EQUAL(s2n_ktls_sendv_with_offset(conn, NULL, 0, 0, &blocked), 0);
661 EXPECT_EQUAL(out.sendmsg_invoked_count, 1);
662 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
663 EXPECT_OK(s2n_test_records_in_ancillary(&out, 0));
664
665 /* Send nothing with iovec array with zero-length buffer */
666 EXPECT_EQUAL(s2n_ktls_sendv_with_offset(conn, &test_iovec, 1, 0, &blocked), 0);
667 EXPECT_EQUAL(out.sendmsg_invoked_count, 2);
668 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
669 EXPECT_OK(s2n_test_records_in_ancillary(&out, 0));
670 };
671
672 /* Test: Send with multiple iovecs */
673 {
674 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
675 s2n_connection_ptr_free);
676 EXPECT_NOT_NULL(conn);
677
678 DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer out = { 0 },
679 s2n_ktls_io_stuffer_free);
680 EXPECT_OK(s2n_test_init_ktls_io_stuffer_send(conn, &out));
681
682 DEFER_CLEANUP(struct s2n_test_iovecs test_iovecs = { 0 }, s2n_test_iovecs_free);
683 EXPECT_OK(s2n_test_new_iovecs(&test_iovecs, &test_data_blob,
684 test_iov_lens, s2n_array_len(test_iov_lens)));
685
686 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
687 ssize_t result = s2n_ktls_sendv_with_offset(conn,
688 test_iovecs.iovecs, test_iovecs.iovecs_count, 0, &blocked);
689 EXPECT_EQUAL(result, sizeof(test_data));
690
691 EXPECT_EQUAL(out.sendmsg_invoked_count, 1);
692 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
693 EXPECT_OK(s2n_test_validate_ancillary(&out, TLS_APPLICATION_DATA, sizeof(test_data)));
694 EXPECT_OK(s2n_test_validate_data(&out, test_data, sizeof(test_data)));
695 };
696
697 /* Test: Send with offset */
698 {
699 DEFER_CLEANUP(struct s2n_test_iovecs test_iovecs = { 0 }, s2n_test_iovecs_free);
700 EXPECT_OK(s2n_test_new_iovecs(&test_iovecs, &test_data_blob,
701 test_iov_lens, s2n_array_len(test_iov_lens)));
702
703 size_t large_test_iov_lens[100] = { 0 };
704 EXPECT_MEMCPY_SUCCESS(large_test_iov_lens, test_iov_lens, sizeof(test_iov_lens));
705
706 DEFER_CLEANUP(struct s2n_test_iovecs large_test_iovecs = { 0 },
707 s2n_test_iovecs_free);
708 EXPECT_OK(s2n_test_new_iovecs(&large_test_iovecs, &test_data_blob,
709 large_test_iov_lens, s2n_array_len(large_test_iov_lens)));
710
711 /* Test: Send with invalid / too large offset */
712 {
713 const size_t bad_offset = sizeof(test_data) + 1;
714
715 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
716 s2n_connection_ptr_free);
717 EXPECT_NOT_NULL(conn);
718
719 DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer out = { 0 },
720 s2n_ktls_io_stuffer_free);
721 EXPECT_OK(s2n_test_init_ktls_io_stuffer_send(conn, &out));
722
723 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
724 ssize_t result = s2n_ktls_sendv_with_offset(conn, large_test_iovecs.iovecs,
725 large_test_iovecs.iovecs_count, bad_offset, &blocked);
726 EXPECT_FAILURE_WITH_ERRNO(result, S2N_ERR_INVALID_ARGUMENT);
727
728 EXPECT_EQUAL(out.sendmsg_invoked_count, 0);
729 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
730 EXPECT_OK(s2n_test_records_in_ancillary(&out, 0));
731 };
732
733 /* Test: Send with offset equal to total data size */
734 {
735 const size_t offset = sizeof(test_data);
736
737 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
738 s2n_connection_ptr_free);
739 EXPECT_NOT_NULL(conn);
740
741 DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer out = { 0 },
742 s2n_ktls_io_stuffer_free);
743 EXPECT_OK(s2n_test_init_ktls_io_stuffer_send(conn, &out));
744
745 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
746 int written = s2n_ktls_sendv_with_offset(conn, large_test_iovecs.iovecs,
747 large_test_iovecs.iovecs_count, offset, &blocked);
748 EXPECT_EQUAL(written, 0);
749
750 EXPECT_EQUAL(out.sendmsg_invoked_count, 1);
751 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
752 EXPECT_OK(s2n_test_records_in_ancillary(&out, 0));
753 };
754
755 /* Test: Send with small iovecs array and all possible valid offsets */
756 for (size_t offset = 0; offset < sizeof(test_data); offset++) {
757 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
758 s2n_connection_ptr_free);
759 EXPECT_NOT_NULL(conn);
760
761 DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer out = { 0 },
762 s2n_ktls_io_stuffer_free);
763 EXPECT_OK(s2n_test_init_ktls_io_stuffer_send(conn, &out));
764
765 const size_t expected_sent = sizeof(test_data) - offset;
766 EXPECT_TRUE(expected_sent > 0);
767
768 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
769 ssize_t result = s2n_ktls_sendv_with_offset(conn,
770 test_iovecs.iovecs, test_iovecs.iovecs_count, offset, &blocked);
771 EXPECT_EQUAL(result, expected_sent);
772
773 EXPECT_EQUAL(out.sendmsg_invoked_count, 1);
774 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
775 EXPECT_OK(s2n_test_validate_ancillary(&out, TLS_APPLICATION_DATA, expected_sent));
776 EXPECT_OK(s2n_test_validate_data(&out, test_data + offset, expected_sent));
777 }
778
779 /* Test: Send with large iovecs array and all possible valid offsets */
780 for (size_t offset = 0; offset < sizeof(test_data); offset++) {
781 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
782 s2n_connection_ptr_free);
783 EXPECT_NOT_NULL(conn);
784
785 DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer out = { 0 },
786 s2n_ktls_io_stuffer_free);
787 EXPECT_OK(s2n_test_init_ktls_io_stuffer_send(conn, &out));
788
789 const size_t expected_sent = sizeof(test_data) - offset;
790 EXPECT_TRUE(expected_sent > 0);
791
792 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
793 ssize_t result = s2n_ktls_sendv_with_offset(conn, large_test_iovecs.iovecs,
794 large_test_iovecs.iovecs_count, offset, &blocked);
795 EXPECT_EQUAL(result, expected_sent);
796
797 EXPECT_EQUAL(out.sendmsg_invoked_count, 1);
798 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
799 EXPECT_OK(s2n_test_validate_ancillary(&out, TLS_APPLICATION_DATA, expected_sent));
800 EXPECT_OK(s2n_test_validate_data(&out, test_data + offset, expected_sent));
801 }
802 };
803
804 /* Test: Partial write */
805 {
806 DEFER_CLEANUP(struct s2n_test_iovecs test_iovecs = { 0 }, s2n_test_iovecs_free);
807 EXPECT_OK(s2n_test_new_iovecs(&test_iovecs, &test_data_blob,
808 test_iov_lens, s2n_array_len(test_iov_lens)));
809
810 /* Test with all possible partial write lengths */
811 for (size_t size = 1; size < sizeof(test_data); size++) {
812 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
813 s2n_connection_ptr_free);
814 EXPECT_NOT_NULL(conn);
815
816 DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer out = { 0 },
817 s2n_ktls_io_stuffer_free);
818 EXPECT_OK(s2n_test_init_ktls_io_stuffer_send(conn, &out));
819 EXPECT_SUCCESS(s2n_stuffer_free(&out.data_buffer));
820 EXPECT_SUCCESS(s2n_stuffer_alloc(&out.data_buffer, size));
821
822 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
823 ssize_t result = s2n_ktls_sendv_with_offset(conn,
824 test_iovecs.iovecs, test_iovecs.iovecs_count, 0, &blocked);
825 EXPECT_EQUAL(result, size);
826 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
827
828 EXPECT_EQUAL(out.sendmsg_invoked_count, 1);
829 EXPECT_OK(s2n_test_validate_data(&out, test_data, size));
830 }
831 };
832
833 /* Test: IO would block */
834 {
835 DEFER_CLEANUP(struct s2n_test_iovecs test_iovecs = { 0 }, s2n_test_iovecs_free);
836 EXPECT_OK(s2n_test_new_iovecs(&test_iovecs, &test_data_blob,
837 test_iov_lens, s2n_array_len(test_iov_lens)));
838
839 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
840 s2n_connection_ptr_free);
841 EXPECT_NOT_NULL(conn);
842
843 struct s2n_test_ktls_io_fail_ctx io_ctx = { .errno_code = EAGAIN };
844 EXPECT_OK(s2n_ktls_set_sendmsg_cb(conn, s2n_test_ktls_sendmsg_fail, &io_ctx));
845
846 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
847 ssize_t result = s2n_ktls_sendv_with_offset(conn,
848 test_iovecs.iovecs, test_iovecs.iovecs_count, 0, &blocked);
849 EXPECT_FAILURE_WITH_ERRNO(result, S2N_ERR_IO_BLOCKED);
850 EXPECT_EQUAL(io_ctx.invoked_count, 1);
851 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE);
852 };
853
854 /* Test: Memory usage */
855 {
856 const size_t iov_lens[100] = { 10, 5, 0, 1, 100, 100, 10 };
857 const size_t small_iov_lens_count = 10;
858 const size_t large_iov_lens_count = s2n_array_len(iov_lens);
859
860 DEFER_CLEANUP(struct s2n_test_iovecs small_iovecs = { 0 }, s2n_test_iovecs_free);
861 EXPECT_OK(s2n_test_new_iovecs(&small_iovecs, &test_data_blob,
862 iov_lens, small_iov_lens_count));
863
864 DEFER_CLEANUP(struct s2n_test_iovecs large_iovecs = { 0 }, s2n_test_iovecs_free);
865 EXPECT_OK(s2n_test_new_iovecs(&large_iovecs, &test_data_blob,
866 iov_lens, large_iov_lens_count));
867
868 const size_t one_iovec_size = sizeof(struct iovec);
869 const size_t large_iovecs_size = large_iovecs.iovecs_count * one_iovec_size;
870
871 struct {
872 struct s2n_test_iovecs *iovecs;
873 size_t offset;
874 uint32_t expected_malloc;
875 uint32_t expected_malloc_count;
876 } test_cases[] = {
877 /* Small iovecs never require an allocation */
878 {
879 .iovecs = &small_iovecs,
880 .offset = 1,
881 .expected_malloc_count = 0,
882 },
883 {
884 .iovecs = &small_iovecs,
885 .offset = iov_lens[0],
886 .expected_malloc_count = 0,
887 },
888 {
889 .iovecs = &small_iovecs,
890 .offset = iov_lens[0] + 1,
891 .expected_malloc_count = 0,
892 },
893 /* Large iovecs with offset evenly divisible by the iov_lens do
894 * not require an alloc.
895 * Example: { x, y, z }, offset=x -> { y, z }
896 */
897 {
898 .iovecs = &large_iovecs,
899 .offset = iov_lens[0],
900 .expected_malloc_count = 0,
901 },
902 {
903 .iovecs = &large_iovecs,
904 .offset = iov_lens[0] + iov_lens[1],
905 .expected_malloc_count = 0,
906 },
907 /* Large iovecs with offset not evenly divisible by the iov_lens
908 * modify an entry so require an alloc.
909 * Example: { x, y, z }, offset=1 -> { x-1, y, z }
910 */
911 {
912 .iovecs = &large_iovecs,
913 .offset = 1,
914 .expected_malloc_count = 1,
915 .expected_malloc = large_iovecs_size,
916 },
917 {
918 .iovecs = &large_iovecs,
919 .offset = iov_lens[0] + 1,
920 .expected_malloc_count = 1,
921 .expected_malloc = large_iovecs_size - one_iovec_size,
922 },
923 /* Large iovecs that become small iovecs when the offset
924 * is applied do not require an alloc.
925 */
926 {
927 .iovecs = &large_iovecs,
928 .offset = sizeof(test_data) - 1,
929 .expected_malloc_count = 0,
930 },
931 /* No alloc if the entire large iovec is skipped */
932 {
933 .iovecs = &large_iovecs,
934 .offset = sizeof(test_data),
935 .expected_malloc_count = 0,
936 },
937 };
938
939 for (size_t i = 0; i < s2n_array_len(test_cases); i++) {
940 struct iovec *iovecs = test_cases[i].iovecs->iovecs;
941 const size_t iovecs_count = test_cases[i].iovecs->iovecs_count;
942 const size_t offset = test_cases[i].offset;
943
944 const size_t expected_send = sizeof(test_data) - offset;
945
946 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
947 s2n_connection_ptr_free);
948 EXPECT_NOT_NULL(conn);
949
950 DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer out = { 0 },
951 s2n_ktls_io_stuffer_free);
952 EXPECT_OK(s2n_test_init_ktls_io_stuffer_send(conn, &out));
953
954 /* Preemptively allocate sendmsg memory to avoid false positives */
955 EXPECT_SUCCESS(s2n_stuffer_resize(&out.data_buffer, expected_send));
956 EXPECT_SUCCESS(s2n_stuffer_resize(&out.ancillary_buffer, 100));
957
958 DEFER_CLEANUP(struct s2n_mem_test_cb_scope scope = { 0 },
959 s2n_mem_test_free_callbacks);
960 EXPECT_OK(s2n_mem_test_init_callbacks(&scope));
961
962 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
963 ssize_t result = s2n_ktls_sendv_with_offset(conn, iovecs, iovecs_count,
964 offset, &blocked);
965 EXPECT_EQUAL(result, sizeof(test_data) - offset);
966
967 size_t malloc_count = test_cases[i].expected_malloc_count;
968 EXPECT_OK(s2n_mem_test_assert_malloc_count(malloc_count));
969 if (malloc_count) {
970 EXPECT_OK(s2n_mem_test_assert_malloc(test_cases[i].expected_malloc));
971 }
972 EXPECT_OK(s2n_mem_test_assert_all_freed());
973 }
974 };
975 };
976
977 /* Test: s2n_ktls_send_cb */
978 {
979 /* It's safe to reuse a connection across tests because the connection
980 * isn't actually used by s2n_ktls_send_cb. It's just required for test
981 * setup methods.
982 */
983 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
984 s2n_connection_ptr_free);
985 EXPECT_NOT_NULL(conn);
986
987 /* Safety */
988 {
989 struct s2n_test_ktls_io_stuffer ctx = { 0 };
990 EXPECT_FAILURE_WITH_ERRNO(s2n_ktls_send_cb(NULL, test_data, 1), S2N_ERR_NULL);
991 EXPECT_FAILURE_WITH_ERRNO(s2n_ktls_send_cb(&ctx, NULL, 1), S2N_ERR_NULL);
992 };
993
994 /* Test: Basic write succeeds */
995 {
996 DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer ctx = { 0 },
997 s2n_ktls_io_stuffer_free);
998 EXPECT_OK(s2n_test_init_ktls_io_stuffer_send(conn, &ctx));
999
1000 EXPECT_SUCCESS(s2n_ktls_send_cb(&ctx, test_data, sizeof(test_data)));
1001 EXPECT_EQUAL(ctx.sendmsg_invoked_count, 1);
1002 EXPECT_OK(s2n_test_validate_ancillary(&ctx, TLS_ALERT, sizeof(test_data)));
1003 EXPECT_OK(s2n_test_validate_data(&ctx, test_data, sizeof(test_data)));
1004 };
1005
1006 /* Test: Errors passed on to caller */
1007 {
1008 struct s2n_test_ktls_io_fail_ctx ctx = { 0 };
1009 EXPECT_OK(s2n_ktls_set_sendmsg_cb(conn, s2n_test_ktls_sendmsg_fail, &ctx));
1010
1011 ctx.errno_code = 1;
1012 EXPECT_FAILURE_WITH_ERRNO(s2n_ktls_send_cb(&ctx, test_data, sizeof(test_data)),
1013 S2N_ERR_IO);
1014
1015 ctx.errno_code = EINVAL;
1016 EXPECT_FAILURE_WITH_ERRNO(s2n_ktls_send_cb(&ctx, test_data, sizeof(test_data)),
1017 S2N_ERR_IO);
1018
1019 ctx.errno_code = EAGAIN;
1020 EXPECT_FAILURE_WITH_ERRNO(s2n_ktls_send_cb(&ctx, test_data, sizeof(test_data)),
1021 S2N_ERR_IO_BLOCKED);
1022
1023 ctx.errno_code = EWOULDBLOCK;
1024 EXPECT_FAILURE_WITH_ERRNO(s2n_ktls_send_cb(&ctx, test_data, sizeof(test_data)),
1025 S2N_ERR_IO_BLOCKED);
1026 };
1027 };
1028
1029 /* Test: s2n_ktls_record_writev */
1030 {
1031 const size_t to_write = 10;
1032
1033 /* Safety */
1034 {
1035 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
1036 s2n_connection_ptr_free);
1037 EXPECT_NOT_NULL(conn);
1038 struct iovec iov = { 0 };
1039 EXPECT_FAILURE_WITH_ERRNO(s2n_ktls_record_writev(NULL, 0, &iov, 1, 1, 1),
1040 S2N_ERR_NULL);
1041 EXPECT_FAILURE_WITH_ERRNO(s2n_ktls_record_writev(conn, 0, NULL, 1, 1, 1),
1042 S2N_ERR_NULL);
1043 EXPECT_FAILURE_WITH_ERRNO(s2n_ktls_record_writev(conn, 0, &iov, -1, 1, 1),
1044 S2N_ERR_INVALID_ARGUMENT);
1045 };
1046
1047 /* Test: Basic write succeeds */
1048 {
1049 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
1050 s2n_connection_ptr_free);
1051 EXPECT_NOT_NULL(conn);
1052 struct iovec iov = {
1053 .iov_base = test_data,
1054 .iov_len = sizeof(test_data),
1055 };
1056 EXPECT_EQUAL(s2n_ktls_record_writev(conn, TLS_ALERT, &iov, 1, 0, to_write), to_write);
1057 EXPECT_EQUAL(conn->out.blob.allocated, to_write);
1058 EXPECT_EQUAL(s2n_stuffer_data_available(&conn->out), to_write);
1059 uint8_t *in_out = s2n_stuffer_raw_read(&conn->out, to_write);
1060 EXPECT_BYTEARRAY_EQUAL(in_out, test_data, to_write);
1061 };
1062
1063 /* Test: Only alerts currently supported */
1064 {
1065 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
1066 s2n_connection_ptr_free);
1067 EXPECT_NOT_NULL(conn);
1068 struct iovec iov = {
1069 .iov_base = test_data,
1070 .iov_len = sizeof(test_data),
1071 };
1072 EXPECT_FAILURE_WITH_ERRNO(
1073 s2n_ktls_record_writev(conn, TLS_HANDSHAKE, &iov, 1, 0, to_write),
1074 S2N_ERR_UNIMPLEMENTED);
1075 };
1076 };
1077
1078 /* Test: s2n_ktls_read_full_record */
1079 {
1080 const struct iovec test_iovec = {
1081 .iov_base = test_data,
1082 .iov_len = sizeof(test_data),
1083 };
1084 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
1085
1086 const size_t max_frag_len = S2N_DEFAULT_FRAGMENT_LENGTH;
1087 /* Our test assumptions are wrong if this isn't true */
1088 EXPECT_TRUE(max_frag_len < sizeof(test_data));
1089
1090 /* Safety */
1091 {
1092 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
1093 s2n_connection_ptr_free);
1094 EXPECT_NOT_NULL(conn);
1095 uint8_t record_type = 0;
1096 EXPECT_FAILURE_WITH_ERRNO(s2n_ktls_read_full_record(NULL, &record_type),
1097 S2N_ERR_NULL);
1098 EXPECT_FAILURE_WITH_ERRNO(s2n_ktls_read_full_record(conn, NULL),
1099 S2N_ERR_NULL);
1100 };
1101
1102 /* Test: Basic read succeeds */
1103 {
1104 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
1105 s2n_connection_ptr_free);
1106 EXPECT_NOT_NULL(conn);
1107
1108 DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer_pair pair = { 0 },
1109 s2n_ktls_io_stuffer_pair_free);
1110 EXPECT_OK(s2n_test_init_ktls_io_stuffer(conn, conn, &pair));
1111 struct s2n_test_ktls_io_stuffer *ctx = &pair.client_in;
1112
1113 size_t written = 0;
1114 EXPECT_OK(s2n_ktls_sendmsg(ctx, TLS_ALERT, &test_iovec, 1, &blocked, &written));
1115 EXPECT_EQUAL(written, sizeof(test_data));
1116
1117 uint8_t record_type = 0;
1118 EXPECT_SUCCESS(s2n_ktls_read_full_record(conn, &record_type));
1119 EXPECT_EQUAL(record_type, TLS_ALERT);
1120
1121 EXPECT_EQUAL(conn->buffer_in.blob.allocated, max_frag_len);
1122 EXPECT_EQUAL(conn->in.blob.size, max_frag_len);
1123 EXPECT_EQUAL(s2n_stuffer_data_available(&conn->in), max_frag_len);
1124 uint8_t *read = s2n_stuffer_raw_read(&conn->in, max_frag_len);
1125 EXPECT_BYTEARRAY_EQUAL(read, test_data, max_frag_len);
1126 };
1127
1128 /* Test: Receive does not completely fill the output buffer */
1129 {
1130 const size_t small_frag_len = 10;
1131 EXPECT_TRUE(small_frag_len < max_frag_len);
1132 EXPECT_TRUE(small_frag_len < sizeof(test_data));
1133 struct iovec small_test_iovec = test_iovec;
1134 small_test_iovec.iov_len = small_frag_len;
1135
1136 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
1137 s2n_connection_ptr_free);
1138 EXPECT_NOT_NULL(conn);
1139
1140 DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer_pair pair = { 0 },
1141 s2n_ktls_io_stuffer_pair_free);
1142 EXPECT_OK(s2n_test_init_ktls_io_stuffer(conn, conn, &pair));
1143 struct s2n_test_ktls_io_stuffer *ctx = &pair.client_in;
1144
1145 size_t written = 0;
1146 EXPECT_OK(s2n_ktls_sendmsg(ctx, TLS_ALERT, &small_test_iovec, 1, &blocked, &written));
1147 EXPECT_EQUAL(written, small_frag_len);
1148
1149 uint8_t record_type = 0;
1150 EXPECT_SUCCESS(s2n_ktls_read_full_record(conn, &record_type));
1151 EXPECT_EQUAL(record_type, TLS_ALERT);
1152
1153 /* Verify that conn->in reflects the correct size of the "record"
1154 * read and doesn't just assume the maximum read size.
1155 */
1156 EXPECT_EQUAL(conn->buffer_in.blob.allocated, max_frag_len);
1157 EXPECT_EQUAL(conn->in.blob.size, small_frag_len);
1158 EXPECT_EQUAL(s2n_stuffer_data_available(&conn->in), small_frag_len);
1159 uint8_t *read = s2n_stuffer_raw_read(&conn->in, small_frag_len);
1160 EXPECT_BYTEARRAY_EQUAL(read, test_data, small_frag_len);
1161 };
1162
1163 /* Test: Receive drains conn->in before calling recvmsg again */
1164 {
1165 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
1166 s2n_connection_ptr_free);
1167 EXPECT_NOT_NULL(conn);
1168
1169 DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer_pair pair = { 0 },
1170 s2n_ktls_io_stuffer_pair_free);
1171 EXPECT_OK(s2n_test_init_ktls_io_stuffer(conn, conn, &pair));
1172 struct s2n_test_ktls_io_stuffer *ctx = &pair.client_in;
1173
1174 /* Write half the test data into conn->in */
1175 const size_t offset = sizeof(test_data) / 2;
1176 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&conn->in, test_data, offset));
1177 /* Resize conn->buffer_in to match conn->in */
1178 EXPECT_SUCCESS(s2n_stuffer_resize(&conn->buffer_in, offset));
1179
1180 /* Write the other half into a new record */
1181 size_t written = 0;
1182 struct iovec offset_iovec = {
1183 .iov_base = test_data + offset,
1184 .iov_len = sizeof(test_data) - offset,
1185 };
1186 EXPECT_OK(s2n_ktls_sendmsg(ctx, TLS_ALERT, &offset_iovec, 1, &blocked, &written));
1187 EXPECT_EQUAL(written, offset_iovec.iov_len);
1188
1189 uint8_t record_type = 0;
1190 uint8_t *read = NULL;
1191
1192 /* Verify that our first read returns conn->in, not the new record */
1193 EXPECT_SUCCESS(s2n_ktls_read_full_record(conn, &record_type));
1194 EXPECT_EQUAL(record_type, TLS_APPLICATION_DATA);
1195 EXPECT_EQUAL(s2n_stuffer_data_available(&conn->in), offset);
1196 read = s2n_stuffer_raw_read(&conn->in, offset);
1197 EXPECT_BYTEARRAY_EQUAL(read, test_data, offset);
1198
1199 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->in));
1200
1201 /* Verify a second read returns the new record */
1202 EXPECT_SUCCESS(s2n_ktls_read_full_record(conn, &record_type));
1203 EXPECT_EQUAL(record_type, TLS_ALERT);
1204 EXPECT_EQUAL(s2n_stuffer_data_available(&conn->in), offset_iovec.iov_len);
1205 read = s2n_stuffer_raw_read(&conn->in, offset_iovec.iov_len);
1206 EXPECT_BYTEARRAY_EQUAL(read, offset_iovec.iov_base, offset_iovec.iov_len);
1207 };
1208
1209 /* Test: Receive multiple records */
1210 {
1211 const size_t small_frag_len = 10;
1212 EXPECT_TRUE(small_frag_len < max_frag_len);
1213 EXPECT_TRUE(small_frag_len < sizeof(test_data));
1214 struct iovec small_test_iovec = test_iovec;
1215 small_test_iovec.iov_len = small_frag_len;
1216
1217 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
1218 s2n_connection_ptr_free);
1219 EXPECT_NOT_NULL(conn);
1220
1221 DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer_pair pair = { 0 },
1222 s2n_ktls_io_stuffer_pair_free);
1223 EXPECT_OK(s2n_test_init_ktls_io_stuffer(conn, conn, &pair));
1224 struct s2n_test_ktls_io_stuffer *ctx = &pair.client_in;
1225
1226 for (size_t i = 0; i < 100; i++) {
1227 size_t written = 0;
1228 EXPECT_OK(s2n_ktls_sendmsg(ctx, TLS_ALERT, &small_test_iovec, 1, &blocked, &written));
1229 EXPECT_EQUAL(written, small_frag_len);
1230
1231 uint8_t record_type = 0;
1232 EXPECT_SUCCESS(s2n_ktls_read_full_record(conn, &record_type));
1233 EXPECT_EQUAL(record_type, TLS_ALERT);
1234 EXPECT_EQUAL(s2n_stuffer_data_available(&conn->in), written);
1235 uint8_t *read = s2n_stuffer_raw_read(&conn->in, small_frag_len);
1236 EXPECT_BYTEARRAY_EQUAL(read, test_data, small_frag_len);
1237
1238 EXPECT_OK(s2n_record_wipe(conn));
1239 size_t space_remaining = s2n_stuffer_space_remaining(&conn->buffer_in);
1240 EXPECT_EQUAL(space_remaining, max_frag_len);
1241 }
1242 };
1243 };
1244
1245 /* Test: key encryption limit tracked */
1246 {
1247 uint8_t large_test_data[S2N_TLS_MAXIMUM_FRAGMENT_LENGTH * 10] = { 0 };
1248 const size_t large_test_data_records = sizeof(large_test_data) / S2N_TLS_MAXIMUM_FRAGMENT_LENGTH;
1249 /* For simplicity, keep our test data a positive even multiple of the max frag size */
1250 EXPECT_TRUE(sizeof(large_test_data) % S2N_TLS_MAXIMUM_FRAGMENT_LENGTH == 0);
1251 EXPECT_TRUE(large_test_data_records > 0);
1252 const size_t test_encryption_limit = large_test_data_records;
1253
1254 struct s2n_record_algorithm test_record_alg = *s2n_tls13_aes_128_gcm_sha256.record_alg;
1255 test_record_alg.encryption_limit = test_encryption_limit;
1256 struct s2n_cipher_suite test_cipher_suite = s2n_tls13_aes_128_gcm_sha256;
1257 test_cipher_suite.record_alg = &test_record_alg;
1258
1259 for (s2n_mode mode = 0; mode <= 1; mode++) {
1260 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(mode),
1261 s2n_connection_ptr_free);
1262 EXPECT_NOT_NULL(conn);
1263 EXPECT_OK(s2n_ktls_set_sendmsg_cb(conn, s2n_test_ktls_sendmsg_mark_all_sent, conn));
1264 conn->ktls_send_enabled = true;
1265 EXPECT_NOT_NULL(conn->secure);
1266 conn->secure->cipher_suite = &test_cipher_suite;
1267 conn->actual_protocol_version = S2N_TLS13;
1268
1269 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
1270
1271 /* Test: Sequence number tracked correctly */
1272 {
1273 DEFER_CLEANUP(struct s2n_blob seq_num = { 0 }, s2n_blob_zero);
1274 EXPECT_OK(s2n_connection_get_sequence_number(conn, conn->mode, &seq_num));
1275
1276 /* Test: All connections start with zero records sent */
1277 uint64_t expected_seq_num = 0;
1278 EXPECT_OK(s2n_assert_seq_num_equal(seq_num, expected_seq_num));
1279
1280 /* Test: Send no data */
1281 EXPECT_EQUAL(s2n_send(conn, large_test_data, 0, &blocked), 0);
1282 EXPECT_OK(s2n_assert_seq_num_equal(seq_num, expected_seq_num));
1283
1284 /* Test: Send one minimum sized record */
1285 expected_seq_num++;
1286 EXPECT_EQUAL(s2n_send(conn, large_test_data, 1, &blocked), 1);
1287 EXPECT_OK(s2n_assert_seq_num_equal(seq_num, expected_seq_num));
1288
1289 /* Test: Send one maximum sized record */
1290 expected_seq_num++;
1291 EXPECT_EQUAL(
1292 s2n_send(conn, large_test_data, S2N_TLS_MAXIMUM_FRAGMENT_LENGTH, &blocked),
1293 S2N_TLS_MAXIMUM_FRAGMENT_LENGTH);
1294 EXPECT_OK(s2n_assert_seq_num_equal(seq_num, expected_seq_num));
1295
1296 /* Test: Send multiple records */
1297 expected_seq_num += 2;
1298 EXPECT_EQUAL(
1299 s2n_send(conn, large_test_data, S2N_TLS_MAXIMUM_FRAGMENT_LENGTH + 1, &blocked),
1300 S2N_TLS_MAXIMUM_FRAGMENT_LENGTH + 1);
1301 EXPECT_OK(s2n_assert_seq_num_equal(seq_num, expected_seq_num));
1302
1303 /* Test: Send enough data to hit the encryption limit */
1304 EXPECT_FAILURE_WITH_ERRNO(
1305 s2n_send(conn, large_test_data, sizeof(large_test_data), &blocked),
1306 S2N_ERR_KTLS_KEY_LIMIT);
1307 EXPECT_OK(s2n_assert_seq_num_equal(seq_num, expected_seq_num));
1308 };
1309
1310 /* Test: Exact encryption limit boundary */
1311 {
1312 DEFER_CLEANUP(struct s2n_blob seq_num = { 0 }, s2n_blob_zero);
1313 EXPECT_OK(s2n_connection_get_sequence_number(conn, conn->mode, &seq_num));
1314
1315 /* Send enough records to hit but not exceed the encryption limit */
1316 for (size_t i = 0; i < test_encryption_limit; i++) {
1317 EXPECT_EQUAL(s2n_send(conn, large_test_data, 1, &blocked), 1);
1318 }
1319 EXPECT_OK(s2n_assert_seq_num_equal(seq_num, test_encryption_limit));
1320
1321 /* One more record should exceed the encryption limit */
1322 EXPECT_FAILURE_WITH_ERRNO(
1323 s2n_send(conn, large_test_data, 1, &blocked),
1324 S2N_ERR_KTLS_KEY_LIMIT);
1325 EXPECT_OK(s2n_assert_seq_num_equal(seq_num, test_encryption_limit));
1326 };
1327
1328 /* Test: Limit not tracked with TLS1.2 */
1329 {
1330 DEFER_CLEANUP(struct s2n_blob seq_num = { 0 }, s2n_blob_zero);
1331 EXPECT_OK(s2n_connection_get_sequence_number(conn, conn->mode, &seq_num));
1332
1333 /* Sequence number not incremented with TLS1.2 */
1334 conn->actual_protocol_version = S2N_TLS12;
1335 EXPECT_EQUAL(
1336 s2n_send(conn, large_test_data, sizeof(large_test_data), &blocked),
1337 sizeof(large_test_data));
1338 EXPECT_OK(s2n_assert_seq_num_equal(seq_num, 0));
1339
1340 /* Sequence number incremented with TLS1.3 */
1341 conn->actual_protocol_version = S2N_TLS13;
1342 EXPECT_EQUAL(
1343 s2n_send(conn, large_test_data, sizeof(large_test_data), &blocked),
1344 sizeof(large_test_data));
1345 EXPECT_OK(s2n_assert_seq_num_equal(seq_num, test_encryption_limit));
1346
1347 /* Passing the limit with TLS1.3 is an error */
1348 conn->actual_protocol_version = S2N_TLS13;
1349 EXPECT_FAILURE_WITH_ERRNO(
1350 s2n_send(conn, large_test_data, 1, &blocked),
1351 S2N_ERR_KTLS_KEY_LIMIT);
1352
1353 /* Passing the limit with TLS1.2 is NOT an error */
1354 conn->actual_protocol_version = S2N_TLS12;
1355 EXPECT_EQUAL(s2n_send(conn, large_test_data, 1, &blocked), 1);
1356 };
1357 }
1358 };
1359
1360 END_TEST();
1361 }