Debian Codegraph @

crt/s2n/tests/unit/s2n_self_talk_io_mem_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 <stdint.h>
17 #include <time.h>
18 #include <unistd.h>
19
20 #include "api/s2n.h"
21 #include "s2n_test.h"
22 #include "testlib/s2n_testlib.h"
23 #include "tls/s2n_connection.h"
24 #include "tls/s2n_handshake.h"
25 #include "tls/s2n_tls13.h"
26
27 int main(int argc, char **argv)
28 {
29 BEGIN_TEST();
30
31 if (!s2n_is_tls13_fully_supported()) {
32 END_TEST();
33 }
34
35 struct s2n_cert_chain_and_key *chain_and_key = NULL;
36 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
37 S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY));
38
39 struct s2n_config *config = s2n_config_new();
40 EXPECT_NOT_NULL(config);
41 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13"));
42 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config));
43 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
44
45 /* Allocate output buffer based on default fragment length */
46 {
47 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
48 EXPECT_NOT_NULL(client_conn);
49 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
50
51 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
52 EXPECT_NOT_NULL(server_conn);
53 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
54
55 /* Create nonblocking pipes */
56 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
57 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
58 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
59 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
60
61 /* Do handshake */
62 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server_conn, client_conn, SERVER_CERT));
63 EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS13);
64 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS13);
65
66 /* Output memory allocated according to max fragment length. */
67 EXPECT_EQUAL(client_conn->max_outgoing_fragment_length, S2N_DEFAULT_FRAGMENT_LENGTH);
68 EXPECT_EQUAL(server_conn->max_outgoing_fragment_length, S2N_DEFAULT_FRAGMENT_LENGTH);
69 /* The client allocates the protocol-agnostic max record size because when it sends its
70 * first message (ClientHello) the protocol hasn't been negotiated yet. */
71 EXPECT_EQUAL(client_conn->out.blob.size, S2N_TLS_MAX_RECORD_LEN_FOR(S2N_DEFAULT_FRAGMENT_LENGTH));
72 /* The server allocates only enough memory for TLS1.3 records because when it sends its
73 * first message (ServerHello) the protocol has already been negotiated. */
74 EXPECT_EQUAL(server_conn->out.blob.size, S2N_TLS13_MAX_RECORD_LEN_FOR(S2N_DEFAULT_FRAGMENT_LENGTH));
75
76 EXPECT_SUCCESS(s2n_connection_free(client_conn));
77 EXPECT_SUCCESS(s2n_connection_free(server_conn));
78 };
79
80 /* Allocate output buffer to max fragment size set manually */
81 {
82 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
83 EXPECT_NOT_NULL(client_conn);
84 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
85
86 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
87 EXPECT_NOT_NULL(server_conn);
88 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
89
90 /* Create nonblocking pipes */
91 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
92 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
93 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
94 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
95
96 /* Set connections to use different fragment sizes */
97 EXPECT_SUCCESS(s2n_connection_prefer_low_latency(client_conn));
98 EXPECT_SUCCESS(s2n_connection_prefer_throughput(server_conn));
99
100 /* Do handshake */
101 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server_conn, client_conn, SERVER_CERT));
102 EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS13);
103 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS13);
104
105 /* Output memory allocated according to max fragment length. */
106 EXPECT_EQUAL(client_conn->max_outgoing_fragment_length, S2N_SMALL_FRAGMENT_LENGTH);
107 EXPECT_EQUAL(server_conn->max_outgoing_fragment_length, S2N_LARGE_FRAGMENT_LENGTH);
108 EXPECT_EQUAL(client_conn->out.blob.size, S2N_TLS_MAX_RECORD_LEN_FOR(S2N_SMALL_FRAGMENT_LENGTH));
109 EXPECT_EQUAL(server_conn->out.blob.size, S2N_TLS13_MAX_RECORD_LEN_FOR(S2N_LARGE_FRAGMENT_LENGTH));
110
111 /* Switch max fragment lengths after handshake to verify whether buffers are resized */
112 EXPECT_SUCCESS(s2n_connection_prefer_throughput(client_conn));
113 EXPECT_SUCCESS(s2n_connection_prefer_low_latency(server_conn));
114 EXPECT_EQUAL(client_conn->max_outgoing_fragment_length, S2N_LARGE_FRAGMENT_LENGTH);
115 EXPECT_EQUAL(server_conn->max_outgoing_fragment_length, S2N_SMALL_FRAGMENT_LENGTH);
116 EXPECT_EQUAL(client_conn->out.blob.size, S2N_TLS13_MAX_RECORD_LEN_FOR(S2N_LARGE_FRAGMENT_LENGTH));
117 EXPECT_EQUAL(server_conn->out.blob.size, S2N_TLS13_MAX_RECORD_LEN_FOR(S2N_LARGE_FRAGMENT_LENGTH));
118
119 EXPECT_SUCCESS(s2n_connection_free(client_conn));
120 EXPECT_SUCCESS(s2n_connection_free(server_conn));
121 };
122
123 /* Allocate output buffer to value negotiated with max_fragment_length extension */
124 {
125 const s2n_max_frag_len mfl_code = S2N_TLS_MAX_FRAG_LEN_2048;
126 const uint16_t expected_mfl = 2048;
127
128 struct s2n_config *config_for_mfl = s2n_config_new();
129 EXPECT_NOT_NULL(config_for_mfl);
130 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config_for_mfl, "default_tls13"));
131 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config_for_mfl));
132 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config_for_mfl, chain_and_key));
133 EXPECT_SUCCESS(s2n_config_send_max_fragment_length(config_for_mfl, mfl_code));
134 EXPECT_SUCCESS(s2n_config_accept_max_fragment_length(config_for_mfl));
135
136 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
137 EXPECT_NOT_NULL(client_conn);
138 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config_for_mfl));
139
140 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
141 EXPECT_NOT_NULL(server_conn);
142 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config_for_mfl));
143
144 /* Create nonblocking pipes */
145 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
146 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
147 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
148 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
149
150 /* Set connections to use fragment size larger than what will be negotiated
151 * via the max_fragment_length extension */
152 EXPECT_SUCCESS(s2n_connection_prefer_throughput(client_conn));
153 EXPECT_SUCCESS(s2n_connection_prefer_throughput(server_conn));
154
155 /* Do handshake */
156 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server_conn, client_conn, SERVER_CERT));
157 EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS13);
158 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS13);
159
160 /* Output memory allocated according to max fragment length. */
161 EXPECT_EQUAL(client_conn->max_outgoing_fragment_length, expected_mfl);
162 EXPECT_EQUAL(server_conn->max_outgoing_fragment_length, expected_mfl);
163 /* The client allocates enough memory for the initial large max_fragment_length, because the
164 * final smaller max_fragment_length has not be negotiated yet when its first message (the ClientHello) is sent. */
165 EXPECT_EQUAL(client_conn->out.blob.size, S2N_TLS_MAX_RECORD_LEN_FOR(S2N_LARGE_FRAGMENT_LENGTH));
166 /* The server only allocates enough memory for the negotiated small max_fragment_length, because the
167 * max_fragment_length is negotiated before its first message (the ServerHello) is sent. */
168 EXPECT_EQUAL(server_conn->out.blob.size, S2N_TLS13_MAX_RECORD_LEN_FOR(expected_mfl));
169
170 EXPECT_SUCCESS(s2n_connection_free(client_conn));
171 EXPECT_SUCCESS(s2n_connection_free(server_conn));
172 EXPECT_SUCCESS(s2n_config_free(config_for_mfl));
173 };
174
175 /* Output and input buffers both freed on connection wipe */
176 {
177 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
178 EXPECT_NOT_NULL(client_conn);
179 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
180
181 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
182 EXPECT_NOT_NULL(server_conn);
183 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
184
185 /* All IO buffers empty */
186 EXPECT_EQUAL(client_conn->buffer_in.blob.size, 0);
187 EXPECT_EQUAL(server_conn->buffer_in.blob.size, 0);
188 EXPECT_EQUAL(client_conn->out.blob.size, 0);
189 EXPECT_EQUAL(server_conn->out.blob.size, 0);
190
191 /* Create nonblocking pipes */
192 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
193 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
194 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
195 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
196
197 /* Do handshake */
198 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server_conn, client_conn, SERVER_CERT));
199
200 /* All IO buffers not empty */
201 EXPECT_NOT_EQUAL(client_conn->buffer_in.blob.size, 0);
202 EXPECT_NOT_EQUAL(server_conn->buffer_in.blob.size, 0);
203 EXPECT_NOT_EQUAL(client_conn->out.blob.size, 0);
204 EXPECT_NOT_EQUAL(server_conn->out.blob.size, 0);
205
206 /* Wipe connections */
207 EXPECT_SUCCESS(s2n_connection_wipe(client_conn));
208 EXPECT_SUCCESS(s2n_connection_wipe(server_conn));
209 EXPECT_EQUAL(client_conn->buffer_in.blob.size, 0);
210 EXPECT_EQUAL(server_conn->buffer_in.blob.size, 0);
211 EXPECT_EQUAL(client_conn->out.blob.size, 0);
212 EXPECT_EQUAL(server_conn->out.blob.size, 0);
213
214 EXPECT_SUCCESS(s2n_connection_free(client_conn));
215 EXPECT_SUCCESS(s2n_connection_free(server_conn));
216 };
217
218 /* Test that dynamic buffers work correctly */
219 {
220 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
221 EXPECT_NOT_NULL(client_conn);
222 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
223
224 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
225 EXPECT_NOT_NULL(server_conn);
226 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
227
228 /* Enable the dynamic buffers setting */
229 EXPECT_SUCCESS(s2n_connection_set_dynamic_buffers(client_conn, true));
230 EXPECT_SUCCESS(s2n_connection_set_dynamic_buffers(server_conn, true));
231
232 /* Configure the connection to use stuffers instead of fds.
233 * This will let us block the send.
234 */
235 DEFER_CLEANUP(struct s2n_stuffer client_in = { 0 }, s2n_stuffer_free);
236 DEFER_CLEANUP(struct s2n_stuffer client_out = { 0 }, s2n_stuffer_free);
237 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&client_in, 0));
238 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&client_out, 0));
239 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&client_in, &client_out, client_conn));
240 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&client_out, &client_in, server_conn));
241
242 /* Do handshake */
243 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
244
245 /* all IO buffers should be empty after the handshake */
246 EXPECT_EQUAL(client_conn->buffer_in.blob.size, 0);
247 EXPECT_EQUAL(client_conn->out.blob.size, 0);
248 EXPECT_EQUAL(server_conn->buffer_in.blob.size, 0);
249 EXPECT_EQUAL(server_conn->out.blob.size, 0);
250
251 /* block the server from sending */
252 EXPECT_SUCCESS(s2n_stuffer_free(&client_in));
253
254 s2n_blocked_status blocked = 0;
255 int send_status = S2N_SUCCESS;
256
257 /* choose a large enough payload to send a full record, 4k is a common page size so we'll go with that */
258 uint8_t buf[4096] = { 42 };
259
260 send_status = s2n_send(server_conn, &buf, s2n_array_len(buf), &blocked);
261
262 /* the first send call should block */
263 EXPECT_FAILURE(send_status);
264 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE);
265
266 /* the `out` buffer should not be freed until it's completely flushed to the socket */
267 EXPECT_NOT_EQUAL(server_conn->out.blob.size, 0);
268
269 /* unblock the send call by letting the stuffer grow */
270 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&client_in, 0));
271
272 send_status = s2n_send(server_conn, &buf, s2n_array_len(buf), &blocked);
273 EXPECT_SUCCESS(send_status);
274
275 /* the entire payload should have been sent */
276 EXPECT_EQUAL(send_status, s2n_array_len(buf));
277
278 /* make sure the `out` buffer was freed after sending */
279 EXPECT_EQUAL(server_conn->out.blob.size, 0);
280
281 /* Receive half of the payload on the first call */
282 EXPECT_EQUAL(s2n_recv(client_conn, &buf, s2n_array_len(buf) / 2, &blocked), s2n_array_len(buf) / 2);
283
284 /* the `in` buffer should not be freed until it's completely flushed to the application */
285 EXPECT_NOT_EQUAL(client_conn->buffer_in.blob.size, 0);
286
287 /* Receive the second half of the payload on the second call */
288 EXPECT_EQUAL(s2n_recv(client_conn, &buf, s2n_array_len(buf) / 2, &blocked), s2n_array_len(buf) / 2);
289
290 /* at this point the application has received the full message and the `in` buffer should be freed */
291 EXPECT_EQUAL(client_conn->buffer_in.blob.size, 0);
292 };
293
294 EXPECT_SUCCESS(s2n_config_free(config));
295 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
296 END_TEST();
297 }