Debian Codegraph @

crt/s2n/tests/testlib/s2n_ktls_test_utils.h

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 "tls/s2n_connection.h"
17 #include "tls/s2n_ktls.h"
18
19 #define S2N_TEST_KTLS_MOCK_HEADER_SIZE 3
20 #define S2N_TEST_KTLS_MOCK_HEADER_TAG_SIZE 1
21
22 /* The record_type is communicated via ancillary data when using kTLS. For this
23 * reason s2n must use `send/recvmsg` syscalls rather than `send/read`. To mimic
24 * the send/recvmsg calls more accurately, we mock the socket via two separate
25 * buffers: data_buffer and ancillary_buffer.
26 *
27 * The mock implementation uses 3 bytes with a tag + len format to represent
28 * each record. The first byte represents the record_type and the next two represent
29 * the length of the record. Length is represented as a u16 to capture the max
30 * possible TLS record length.
31 *
32 * Example: ancillary_buffer memory layout per record:
33 * ```
34 * [ u8 | u16 ]
35 * record_type length
36 * ```
37 *
38 * Example: memory layout of ancillary_buffer and data_buffer with 3 records:
39 * ```
40 * ancillary_buffer
41 *
42 * [ [record] [record] [record] ]
43 * [ [u8|u16] [u8|u16] [u8|u16] ]
44 * [ [23|5] [23|7] [21|2] ]
45 * | | |
46 * v-------v v-----------v v-v
47 * [1 2 3 4 5 1 2 3 4 5 6 7 1 2]
48 *
49 * data_buffer
50 * ```
51 */
52 struct s2n_test_ktls_io_stuffer {
53 struct s2n_stuffer ancillary_buffer;
54 struct s2n_stuffer data_buffer;
55 size_t sendmsg_invoked_count;
56 size_t recvmsg_invoked_count;
57 };
58 struct s2n_test_ktls_io_stuffer_pair {
59 struct s2n_test_ktls_io_stuffer client_in;
60 struct s2n_test_ktls_io_stuffer server_in;
61 };
62 ssize_t s2n_test_ktls_sendmsg_io_stuffer(void *io_context, const struct msghdr *msg);
63 ssize_t s2n_test_ktls_recvmsg_io_stuffer(void *io_context, struct msghdr *msg);
64
65 S2N_RESULT s2n_test_init_ktls_io_stuffer_send(struct s2n_connection *conn,
66 struct s2n_test_ktls_io_stuffer *io);
67 S2N_RESULT s2n_test_init_ktls_io_stuffer(struct s2n_connection *server,
68 struct s2n_connection *client, struct s2n_test_ktls_io_stuffer_pair *io_pair);
69 S2N_CLEANUP_RESULT s2n_ktls_io_stuffer_free(struct s2n_test_ktls_io_stuffer *io);
70 S2N_CLEANUP_RESULT s2n_ktls_io_stuffer_pair_free(struct s2n_test_ktls_io_stuffer_pair *pair);
71 S2N_RESULT s2n_test_validate_data(struct s2n_test_ktls_io_stuffer *ktls_io,
72 const uint8_t *expected_data, uint16_t expected_len);
73 S2N_RESULT s2n_test_validate_ancillary(struct s2n_test_ktls_io_stuffer *ktls_io,
74 uint8_t expected_record_type, uint16_t expected_len);
75 S2N_RESULT s2n_test_records_in_ancillary(struct s2n_test_ktls_io_stuffer *ktls_io,
76 uint16_t expected_records);