Debian Codegraph @

crt/s2n/tests/unit/s2n_examples_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 "testlib/s2n_examples.h"
17
18 #include <pthread.h>
19 #include <sys/types.h>
20 #include <sys/wait.h>
21
22 #include "s2n_test.h"
23 #include "testlib/s2n_testlib.h"
24 #include "utils/s2n_random.h"
25
26 #define S2N_TEST_RECORD_COUNT 200
27 #define S2N_TEST_BYTES_TO_SEND (S2N_DEFAULT_FRAGMENT_LENGTH * S2N_TEST_RECORD_COUNT)
28 #define S2N_TEST_LAST_SEQ_NUM_BYTE (S2N_TLS_SEQUENCE_NUM_LEN - 1)
29
30 typedef int (*s2n_io_fn)(struct s2n_connection *conn, uint8_t *buffer, size_t buffer_size);
31 struct s2n_test_thread_input {
32 s2n_io_fn fn;
33 struct s2n_connection *conn;
34 struct s2n_blob *mem;
35 };
36
37 static void *s2n_run_io_fn(void *arg)
38 {
39 struct s2n_test_thread_input *input = (struct s2n_test_thread_input *) arg;
40 if (input && input->fn && input->mem) {
41 if (input->fn(input->conn, input->mem->data, input->mem->size) == 0) {
42 return arg;
43 }
44 }
45 return NULL;
46 }
47
48 static S2N_RESULT s2n_test_shutdown(struct s2n_connection *conn)
49 {
50 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
51 int r = 0;
52 while ((r = s2n_shutdown(conn, &blocked)) != S2N_SUCCESS) {
53 if (s2n_error_get_type(s2n_errno) != S2N_ERR_T_BLOCKED) {
54 RESULT_GUARD_POSIX(r);
55 }
56 }
57 return S2N_RESULT_OK;
58 }
59
60 static S2N_RESULT s2n_test_shutdown_send(struct s2n_connection *conn)
61 {
62 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
63 int r = 0;
64 while ((r = s2n_shutdown_send(conn, &blocked)) != S2N_SUCCESS) {
65 if (s2n_error_get_type(s2n_errno) != S2N_ERR_T_BLOCKED) {
66 RESULT_GUARD_POSIX(r);
67 }
68 }
69 return S2N_RESULT_OK;
70 }
71
72 static S2N_RESULT s2n_test_example_negotiate(struct s2n_connection *conn,
73 struct s2n_blob *input)
74 {
75 RESULT_GUARD_POSIX(s2n_example_negotiate(conn));
76 RESULT_GUARD(s2n_test_shutdown(conn));
77 return S2N_RESULT_OK;
78 }
79
80 static S2N_RESULT s2n_test_example_send_and_recv(struct s2n_connection *conn,
81 struct s2n_blob *input)
82 {
83 RESULT_GUARD_POSIX(s2n_example_negotiate(conn));
84
85 DEFER_CLEANUP(struct s2n_blob output = { 0 }, s2n_free);
86 RESULT_GUARD_POSIX(s2n_alloc(&output, input->size));
87
88 pthread_t reader = 0;
89 struct s2n_test_thread_input reader_input = {
90 .fn = s2n_example_recv,
91 .conn = conn,
92 .mem = &output,
93 };
94 RESULT_ENSURE_EQ(pthread_create(&reader, NULL, s2n_run_io_fn, (void *) &reader_input), 0);
95
96 pthread_t writer = 0;
97 struct s2n_test_thread_input writer_input = {
98 .fn = s2n_example_send,
99 .conn = conn,
100 .mem = input,
101 };
102 RESULT_ENSURE_EQ(pthread_create(&writer, NULL, s2n_run_io_fn, (void *) &writer_input), 0);
103
104 void *reader_return = NULL;
105 RESULT_ENSURE_EQ(pthread_join(reader, &reader_return), 0);
106 RESULT_ENSURE_REF(reader_return);
107
108 void *writer_return = NULL;
109 RESULT_ENSURE_EQ(pthread_join(writer, &writer_return), 0);
110 RESULT_ENSURE_REF(writer_return);
111
112 RESULT_ENSURE_EQ(memcmp(output.data, input->data, input->size), 0);
113
114 RESULT_GUARD(s2n_test_shutdown(conn));
115 return S2N_RESULT_OK;
116 }
117
118 static S2N_RESULT s2n_test_example_sendv(struct s2n_connection *conn,
119 struct s2n_blob *input)
120 {
121 RESULT_GUARD_POSIX(s2n_example_negotiate(conn));
122
123 DEFER_CLEANUP(struct s2n_blob output = { 0 }, s2n_free);
124 RESULT_GUARD_POSIX(s2n_alloc(&output, input->size));
125
126 pthread_t reader = 0;
127 struct s2n_test_thread_input reader_input = {
128 .fn = s2n_example_recv,
129 .conn = conn,
130 .mem = &output,
131 };
132 RESULT_ENSURE_EQ(pthread_create(&reader, NULL, s2n_run_io_fn, (void *) &reader_input), 0);
133
134 pthread_t writer = 0;
135 struct s2n_test_thread_input writer_input = {
136 .fn = s2n_example_sendv,
137 .conn = conn,
138 .mem = input,
139 };
140 RESULT_ENSURE_EQ(pthread_create(&writer, NULL, s2n_run_io_fn, (void *) &writer_input), 0);
141
142 void *reader_return = NULL;
143 RESULT_ENSURE_EQ(pthread_join(reader, &reader_return), 0);
144 RESULT_ENSURE_REF(reader_return);
145
146 void *writer_return = NULL;
147 RESULT_ENSURE_EQ(pthread_join(writer, &writer_return), 0);
148 RESULT_ENSURE_REF(writer_return);
149
150 RESULT_ENSURE_EQ(memcmp(output.data, input->data, input->size), 0);
151
152 RESULT_GUARD(s2n_test_shutdown(conn));
153 return S2N_RESULT_OK;
154 }
155
156 static S2N_RESULT s2n_test_example_recv_echo(struct s2n_connection *conn,
157 struct s2n_blob *input)
158 {
159 RESULT_GUARD_POSIX(s2n_example_negotiate(conn));
160
161 /* We need to send a close_notify to stop the reader thread, which will cause
162 * a full connection shutdown without TLS1.3's half-close behavior.
163 * That can lead to unexpected "connection closed" errors depending on the timings.
164 */
165 if (conn->actual_protocol_version < S2N_TLS13) {
166 return S2N_RESULT_OK;
167 }
168
169 DEFER_CLEANUP(struct s2n_blob output = { 0 }, s2n_free);
170 RESULT_GUARD_POSIX(s2n_alloc(&output, 100));
171
172 pthread_t reader = 0;
173 struct s2n_test_thread_input reader_input = {
174 .fn = s2n_example_recv_echo,
175 .conn = conn,
176 .mem = &output,
177 };
178 RESULT_ENSURE_EQ(pthread_create(&reader, NULL, s2n_run_io_fn, (void *) &reader_input), 0);
179
180 pthread_t writer = 0;
181 struct s2n_test_thread_input writer_input = {
182 .fn = s2n_example_send,
183 .conn = conn,
184 .mem = input,
185 };
186 RESULT_ENSURE_EQ(pthread_create(&writer, NULL, s2n_run_io_fn, (void *) &writer_input), 0);
187
188 void *writer_return = NULL;
189 RESULT_ENSURE_EQ(pthread_join(writer, &writer_return), 0);
190 RESULT_ENSURE_REF(writer_return);
191
192 RESULT_GUARD(s2n_test_shutdown_send(conn));
193
194 void *reader_return = NULL;
195 RESULT_ENSURE_EQ(pthread_join(reader, &reader_return), 0);
196 RESULT_ENSURE_REF(reader_return);
197
198 /* We can't verify the exact data read, since we read in chunks and the buffer
199 * never contains the full data sent, but we can sanity check the number of
200 * records and total bytes read.
201 */
202 RESULT_ENSURE_GT(conn->wire_bytes_in, input->size);
203 RESULT_ENSURE_LTE(S2N_TEST_RECORD_COUNT, UINT8_MAX);
204 RESULT_ENSURE_GTE(conn->secure->client_sequence_number[S2N_TEST_LAST_SEQ_NUM_BYTE],
205 S2N_TEST_RECORD_COUNT);
206 RESULT_ENSURE_GTE(conn->secure->server_sequence_number[S2N_TEST_LAST_SEQ_NUM_BYTE],
207 S2N_TEST_RECORD_COUNT);
208
209 RESULT_GUARD(s2n_test_shutdown(conn));
210 return S2N_RESULT_OK;
211 }
212
213 typedef S2N_RESULT (*s2n_test_scenario)(struct s2n_connection *conn, struct s2n_blob *input);
214 static S2N_RESULT s2n_run_self_talk_test(s2n_test_scenario scenario_fn)
215 {
216 struct s2n_cert_chain_and_key *chain_and_key = NULL;
217 RESULT_GUARD_POSIX(s2n_test_cert_chain_and_key_new(&chain_and_key,
218 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
219
220 struct s2n_config *config = s2n_config_new();
221 RESULT_ENSURE_REF(config);
222 RESULT_GUARD_POSIX(s2n_config_set_unsafe_for_testing(config));
223 RESULT_GUARD_POSIX(s2n_config_set_cipher_preferences(config, "default_tls13"));
224 RESULT_GUARD_POSIX(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
225
226 struct s2n_blob input = { 0 };
227 RESULT_GUARD_POSIX(s2n_alloc(&input, S2N_TEST_BYTES_TO_SEND));
228 RESULT_GUARD(s2n_get_public_random_data(&input));
229
230 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
231 RESULT_GUARD_POSIX(s2n_io_pair_init_non_blocking(&io_pair));
232
233 pid_t client_pid = fork();
234 if (client_pid == 0) {
235 /* Suppress stdout when running the examples.
236 * This only affects the new client process.
237 */
238 fclose(stdout);
239
240 struct s2n_connection *client = s2n_connection_new(S2N_CLIENT);
241 EXPECT_NOT_NULL(client);
242 EXPECT_SUCCESS(s2n_connection_set_config(client, config));
243
244 EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_SERVER));
245 EXPECT_SUCCESS(s2n_connection_set_io_pair(client, &io_pair));
246
247 EXPECT_OK(scenario_fn(client, &input));
248
249 EXPECT_SUCCESS(s2n_connection_free(client));
250 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
251 EXPECT_SUCCESS(s2n_config_free(config));
252 EXPECT_SUCCESS(s2n_free(&input));
253 EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_CLIENT));
254
255 exit(EXIT_SUCCESS);
256 }
257
258 pid_t server_pid = fork();
259 if (server_pid == 0) {
260 /* Suppress stdout when running the examples.
261 * This only affects the new server process.
262 */
263 fclose(stdout);
264
265 struct s2n_connection *server = s2n_connection_new(S2N_SERVER);
266 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
267
268 EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_CLIENT));
269 EXPECT_SUCCESS(s2n_connection_set_io_pair(server, &io_pair));
270
271 EXPECT_OK(scenario_fn(server, &input));
272
273 EXPECT_SUCCESS(s2n_connection_free(server));
274 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
275 EXPECT_SUCCESS(s2n_config_free(config));
276 EXPECT_SUCCESS(s2n_free(&input));
277 EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_SERVER));
278
279 exit(EXIT_SUCCESS);
280 }
281
282 int status = 0;
283 RESULT_ENSURE_EQ(waitpid(client_pid, &status, 0), client_pid);
284 RESULT_ENSURE_EQ(status, EXIT_SUCCESS);
285 RESULT_ENSURE_EQ(waitpid(server_pid, &status, 0), server_pid);
286 RESULT_ENSURE_EQ(status, EXIT_SUCCESS);
287
288 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
289 EXPECT_SUCCESS(s2n_config_free(config));
290 EXPECT_SUCCESS(s2n_free(&input));
291
292 return S2N_RESULT_OK;
293 }
294
295 static S2N_RESULT s2n_run_failure_tests()
296 {
297 uint8_t buffer[100] = { 0 };
298 size_t buffer_size = sizeof(buffer);
299
300 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
301 EXPECT_NOT_NULL(conn);
302 EXPECT_SUCCESS(s2n_connection_set_blinding(conn, S2N_SELF_SERVICE_BLINDING));
303
304 pid_t pid = fork();
305 if (pid == 0) {
306 /* Suppress stdout AND stderr when running the examples.
307 * This only affects the new process.
308 */
309 fclose(stdout);
310 fclose(stderr);
311
312 EXPECT_EQUAL(s2n_example_negotiate(conn), S2N_FAILURE);
313 EXPECT_EQUAL(s2n_example_send(conn, buffer, buffer_size), S2N_FAILURE);
314 EXPECT_EQUAL(s2n_example_sendv(conn, buffer, buffer_size), S2N_FAILURE);
315 EXPECT_EQUAL(s2n_example_recv(conn, buffer, buffer_size), S2N_FAILURE);
316 EXPECT_EQUAL(s2n_example_recv_echo(conn, buffer, buffer_size), S2N_FAILURE);
317
318 EXPECT_SUCCESS(s2n_connection_free(conn));
319
320 exit(EXIT_SUCCESS);
321 }
322
323 int status = 0;
324 RESULT_ENSURE_EQ(waitpid(pid, &status, 0), pid);
325 RESULT_ENSURE_EQ(status, EXIT_SUCCESS);
326
327 EXPECT_SUCCESS(s2n_connection_free(conn));
328
329 return S2N_RESULT_OK;
330 }
331
332 int main(int argc, char **argv)
333 {
334 BEGIN_TEST();
335
336 /* We're going to fork, so flush the initial test output first */
337 EXPECT_EQUAL(fflush(stdout), 0);
338
339 EXPECT_OK(s2n_run_failure_tests());
340 EXPECT_OK(s2n_run_self_talk_test(s2n_test_example_negotiate));
341 EXPECT_OK(s2n_run_self_talk_test(s2n_test_example_send_and_recv));
342 EXPECT_OK(s2n_run_self_talk_test(s2n_test_example_sendv));
343 EXPECT_OK(s2n_run_self_talk_test(s2n_test_example_recv_echo));
344 END_TEST();
345
346 END_TEST();
347 }