Debian Codegraph @

crt/s2n/tests/testlib/s2n_connection_test_utils.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 <fcntl.h>
17 #include <stdio.h>
18 #ifdef _WIN32
19 #define WIN32_LEAN_AND_MEAN
20 #include <winsock2.h>
21 #include <ws2tcpip.h>
22 #else
23 #include <sys/socket.h>
24 #include <unistd.h>
25 #endif
26 #include <sys/types.h>
27
28 #include "testlib/s2n_testlib.h"
29 #include "tls/s2n_connection.h"
30 #include "utils/s2n_safety.h"
31 #ifndef _WIN32
32 #include "utils/s2n_socket.h"
33 #endif
34
35 int s2n_fd_set_blocking(int fd)
36 {
37 #ifdef _WIN32
38 u_long mode = 0;
39 return ioctlsocket(fd, FIONBIO, &mode);
40 #else
41 return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK);
42 #endif
43 }
44
45 int s2n_fd_set_non_blocking(int fd)
46 {
47 #ifdef _WIN32
48 u_long mode = 1;
49 return ioctlsocket(fd, FIONBIO, &mode);
50 #else
51 return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
52 #endif
53 }
54
55 static int buffer_read(void *io_context, uint8_t *buf, uint32_t len)
56 {
57 struct s2n_stuffer *in_buf = NULL;
58 int n_read = 0, n_avail = 0;
59 errno = EIO;
60
61 if (buf == NULL) {
62 return 0;
63 }
64
65 in_buf = (struct s2n_stuffer *) io_context;
66 if (in_buf == NULL) {
67 errno = EINVAL;
68 return -1;
69 }
70
71 /* read the number of bytes requested or less if it isn't available */
72 n_avail = s2n_stuffer_data_available(in_buf);
73 n_read = (len < n_avail) ? len : n_avail;
74
75 if (n_read == 0) {
76 errno = EAGAIN;
77 return -1;
78 }
79
80 POSIX_GUARD(s2n_stuffer_read_bytes(in_buf, buf, n_read));
81 return n_read;
82 }
83
84 static int buffer_write(void *io_context, const uint8_t *buf, uint32_t len)
85 {
86 struct s2n_stuffer *out = NULL;
87
88 if (buf == NULL) {
89 return 0;
90 }
91
92 out = (struct s2n_stuffer *) io_context;
93 if (out == NULL) {
94 errno = EINVAL;
95 return -1;
96 }
97
98 if (s2n_stuffer_write_bytes(out, buf, len) < 0) {
99 errno = EAGAIN;
100 return -1;
101 }
102
103 return len;
104 }
105
106 /* The connection will read/write to/from a stuffer, instead of sockets */
107 int s2n_connection_set_io_stuffers(struct s2n_stuffer *input, struct s2n_stuffer *output, struct s2n_connection *conn)
108 {
109 POSIX_GUARD(s2n_connection_set_recv_io_stuffer(input, conn));
110 POSIX_GUARD(s2n_connection_set_send_io_stuffer(output, conn));
111
112 return S2N_SUCCESS;
113 }
114
115 int s2n_connection_set_recv_io_stuffer(struct s2n_stuffer *input, struct s2n_connection *conn)
116 {
117 POSIX_GUARD(s2n_connection_set_recv_cb(conn, &buffer_read));
118 POSIX_GUARD(s2n_connection_set_recv_ctx(conn, input));
119
120 return S2N_SUCCESS;
121 }
122
123 int s2n_connection_set_send_io_stuffer(struct s2n_stuffer *output, struct s2n_connection *conn)
124 {
125 POSIX_GUARD(s2n_connection_set_send_cb(conn, &buffer_write));
126 POSIX_GUARD(s2n_connection_set_send_ctx(conn, output));
127
128 return S2N_SUCCESS;
129 }
130
131 S2N_RESULT s2n_io_stuffer_pair_init(struct s2n_test_io_stuffer_pair *io_pair)
132 {
133 RESULT_ENSURE_REF(io_pair);
134 RESULT_GUARD_POSIX(s2n_stuffer_growable_alloc(&io_pair->client_in, 0));
135 RESULT_GUARD_POSIX(s2n_stuffer_growable_alloc(&io_pair->server_in, 0));
136 return S2N_RESULT_OK;
137 }
138
139 S2N_CLEANUP_RESULT s2n_io_stuffer_pair_free(struct s2n_test_io_stuffer_pair *io_pair)
140 {
141 RESULT_ENSURE_REF(io_pair);
142 RESULT_GUARD_POSIX(s2n_stuffer_free(&io_pair->client_in));
143 RESULT_GUARD_POSIX(s2n_stuffer_free(&io_pair->server_in));
144 return S2N_RESULT_OK;
145 }
146
147 S2N_RESULT s2n_connections_set_io_stuffer_pair(struct s2n_connection *client, struct s2n_connection *server,
148 struct s2n_test_io_stuffer_pair *io_pair)
149 {
150 RESULT_ENSURE_REF(io_pair);
151 RESULT_GUARD_POSIX(s2n_connection_set_io_stuffers(&io_pair->client_in, &io_pair->server_in, client));
152 RESULT_GUARD_POSIX(s2n_connection_set_io_stuffers(&io_pair->server_in, &io_pair->client_in, server));
153 return S2N_RESULT_OK;
154 }
155
156 int s2n_io_pair_init(struct s2n_test_io_pair *io_pair)
157 {
158 #ifdef _WIN32
159 /* Winsock requires initialization before any socket call. */
160 WSADATA wsa_data = { 0 };
161 int wsa_rc = WSAStartup(MAKEWORD(2, 2), &wsa_data);
162 POSIX_ENSURE(wsa_rc == 0, S2N_ERR_IO);
163
164 /* Windows doesn't have socketpair. Emulate with TCP loopback. */
165 int listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
166 POSIX_ENSURE(listener >= 0, S2N_ERR_IO);
167
168 struct sockaddr_in addr = { 0 };
169 addr.sin_family = AF_INET;
170 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
171 addr.sin_port = 0;
172
173 POSIX_ENSURE(bind(listener, (struct sockaddr *) &addr, sizeof(addr)) == 0, S2N_ERR_IO);
174 POSIX_ENSURE(listen(listener, 1) == 0, S2N_ERR_IO);
175
176 int addrlen = sizeof(addr);
177 POSIX_ENSURE(getsockname(listener, (struct sockaddr *) &addr, &addrlen) == 0, S2N_ERR_IO);
178
179 io_pair->client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
180 POSIX_ENSURE(io_pair->client >= 0, S2N_ERR_IO);
181 POSIX_ENSURE(connect(io_pair->client, (struct sockaddr *) &addr, sizeof(addr)) == 0, S2N_ERR_IO);
182
183 io_pair->server = accept(listener, NULL, NULL);
184 POSIX_ENSURE(io_pair->server >= 0, S2N_ERR_IO);
185
186 closesocket(listener);
187 #else
188 signal(SIGPIPE, SIG_IGN);
189
190 int socket_pair[2];
191
192 POSIX_GUARD(socketpair(AF_UNIX, SOCK_STREAM, 0, socket_pair));
193
194 io_pair->client = socket_pair[0];
195 io_pair->server = socket_pair[1];
196 #endif
197
198 return 0;
199 }
200
201 int s2n_io_pair_init_non_blocking(struct s2n_test_io_pair *io_pair)
202 {
203 POSIX_GUARD(s2n_io_pair_init(io_pair));
204
205 POSIX_GUARD(s2n_fd_set_non_blocking(io_pair->client));
206 POSIX_GUARD(s2n_fd_set_non_blocking(io_pair->server));
207
208 return 0;
209 }
210
211 #ifdef _WIN32
212 /* On Windows, the s2n library does not provide built-in socket I/O.
213 * Tests use these custom I/O callbacks that wrap Winsock recv/send.
214 * The socket fd is passed directly as the io_context pointer via uintptr_t cast.
215 */
216 static int s2n_test_recv_cb(void *io_context, uint8_t *buf, uint32_t len)
217 {
218 int fd = (int) (uintptr_t) io_context;
219
220 int result = recv(fd, (char *) buf, len, 0);
221 if (result < 0) {
222 int wsa_err = WSAGetLastError();
223 if (wsa_err == WSAEWOULDBLOCK) {
224 errno = EAGAIN;
225 } else {
226 errno = EIO;
227 }
228 }
229 return result;
230 }
231
232 static int s2n_test_send_cb(void *io_context, const uint8_t *buf, uint32_t len)
233 {
234 int fd = (int) (uintptr_t) io_context;
235
236 int result = send(fd, (const char *) buf, len, 0);
237 if (result < 0) {
238 int wsa_err = WSAGetLastError();
239 if (wsa_err == WSAEWOULDBLOCK) {
240 errno = EAGAIN;
241 } else {
242 errno = EIO;
243 }
244 }
245 return result;
246 }
247 #endif
248
249 int s2n_connection_set_io_pair(struct s2n_connection *conn, struct s2n_test_io_pair *io_pair)
250 {
251 int fd = 0;
252 if (conn->mode == S2N_CLIENT) {
253 fd = io_pair->client;
254 } else if (conn->mode == S2N_SERVER) {
255 fd = io_pair->server;
256 } else {
257 POSIX_BAIL(S2N_ERR_INVALID_STATE);
258 }
259
260 #ifdef _WIN32
261 POSIX_GUARD(s2n_connection_set_recv_cb(conn, s2n_test_recv_cb));
262 POSIX_GUARD(s2n_connection_set_send_cb(conn, s2n_test_send_cb));
263 POSIX_GUARD(s2n_connection_set_recv_ctx(conn, (void *) (uintptr_t) fd));
264 POSIX_GUARD(s2n_connection_set_send_ctx(conn, (void *) (uintptr_t) fd));
265 #else
266 POSIX_GUARD(s2n_connection_set_fd(conn, fd));
267 #endif
268
269 return 0;
270 }
271
272 int s2n_connections_set_io_pair(struct s2n_connection *client, struct s2n_connection *server,
273 struct s2n_test_io_pair *io_pair)
274 {
275 POSIX_GUARD(s2n_connection_set_io_pair(client, io_pair));
276 POSIX_GUARD(s2n_connection_set_io_pair(server, io_pair));
277 return 0;
278 }
279
280 int s2n_io_pair_close(struct s2n_test_io_pair *io_pair)
281 {
282 POSIX_GUARD(s2n_io_pair_close_one_end(io_pair, S2N_CLIENT));
283 POSIX_GUARD(s2n_io_pair_close_one_end(io_pair, S2N_SERVER));
284 #ifdef _WIN32
285 WSACleanup();
286 #endif
287 return 0;
288 }
289
290 int s2n_io_pair_close_one_end(struct s2n_test_io_pair *io_pair, int mode_to_close)
291 {
292 if (mode_to_close == S2N_CLIENT && io_pair->client != S2N_CLOSED_FD) {
293 #ifdef _WIN32
294 POSIX_GUARD(closesocket(io_pair->client));
295 #else
296 POSIX_GUARD(close(io_pair->client));
297 #endif
298 io_pair->client = S2N_CLOSED_FD;
299 } else if (mode_to_close == S2N_SERVER && io_pair->server != S2N_CLOSED_FD) {
300 #ifdef _WIN32
301 POSIX_GUARD(closesocket(io_pair->server));
302 #else
303 POSIX_GUARD(close(io_pair->server));
304 #endif
305 io_pair->server = S2N_CLOSED_FD;
306 }
307 return 0;
308 }
309
310 int s2n_io_pair_shutdown_one_end(struct s2n_test_io_pair *io_pair, int mode_to_close, int how)
311 {
312 if (mode_to_close == S2N_CLIENT) {
313 POSIX_GUARD(shutdown(io_pair->client, how));
314 } else if (mode_to_close == S2N_SERVER) {
315 POSIX_GUARD(shutdown(io_pair->server, how));
316 }
317 return 0;
318 }
319
320 void s2n_print_connection(struct s2n_connection *conn, const char *marker)
321 {
322 size_t i = 0;
323
324 printf("marker: %s\n", marker);
325 printf("HEADER IN Stuffer (write: %d, read: %d, size: %d)\n", conn->header_in.write_cursor, conn->header_in.read_cursor, conn->header_in.blob.size);
326 for (i = 0; i < conn->header_in.blob.size; i++) {
327 printf("%02x", conn->header_in.blob.data[i]);
328 if ((i + 1) % 8 == 0) {
329 printf(" ");
330 }
331 if ((i + 1) % 40 == 0) {
332 printf("\n");
333 }
334 }
335 printf("\n");
336
337 printf("IN Stuffer (write: %d, read: %d, size: %d)\n", conn->in.write_cursor, conn->in.read_cursor, conn->in.blob.size);
338 for (i = 0; i < conn->in.write_cursor; i++) {
339 printf("%02x", conn->in.blob.data[i]);
340 if ((i + 1) % 8 == 0) {
341 printf(" ");
342 }
343 if ((i + 1) % 40 == 0) {
344 printf("\n");
345 }
346 }
347 printf("\n");
348
349 printf("OUT Stuffer (write: %d, read: %d, size: %d)\n", conn->out.write_cursor, conn->out.read_cursor, conn->out.blob.size);
350 for (i = 0; i < conn->out.write_cursor; i++) {
351 printf("%02x", conn->out.blob.data[i]);
352 if ((i + 1) % 8 == 0) {
353 printf(" ");
354 }
355 if ((i + 1) % 40 == 0) {
356 printf("\n");
357 }
358 }
359 printf("\n");
360 }
361
362 int s2n_set_connection_hello_retry_flags(struct s2n_connection *conn)
363 {
364 POSIX_ENSURE_REF(conn);
365
366 conn->handshake.message_number = 1;
367 conn->handshake.handshake_type = NEGOTIATED | HELLO_RETRY_REQUEST | FULL_HANDSHAKE;
368
369 return S2N_SUCCESS;
370 }
371
372 int s2n_connection_set_all_protocol_versions(struct s2n_connection *conn, uint8_t version)
373 {
374 POSIX_ENSURE_REF(conn);
375
376 conn->server_protocol_version = version;
377 conn->client_protocol_version = version;
378 conn->actual_protocol_version = version;
379
380 return S2N_SUCCESS;
381 }
382
383 static int mock_time(void *data, uint64_t *nanoseconds)
384 {
385 POSIX_ENSURE_REF(data);
386 POSIX_ENSURE_REF(nanoseconds);
387 *nanoseconds = *((uint64_t *) data);
388 return S2N_SUCCESS;
389 }
390
391 S2N_RESULT s2n_config_mock_wall_clock(struct s2n_config *config, uint64_t *test_time_in_ns)
392 {
393 RESULT_ENSURE_REF(config);
394 RESULT_GUARD_POSIX(s2n_config_set_wall_clock(config, mock_time, test_time_in_ns));
395 return S2N_RESULT_OK;
396 }
397
398 /* Sets the encryption and decryption keys to enable sending and receiving encrypted data.
399 * Basically, it bypasses the usual key exchange -> shared secret -> derive keys process
400 * and just uses static mock keys.
401 */
402 S2N_RESULT s2n_connection_set_secrets(struct s2n_connection *conn)
403 {
404 RESULT_ENSURE_REF(conn);
405 conn->secure->cipher_suite = &s2n_tls13_aes_256_gcm_sha384;
406 const struct s2n_cipher *cipher = conn->secure->cipher_suite->record_alg->cipher;
407
408 uint8_t client_key_bytes[S2N_TLS13_SECRET_MAX_LEN] = "client key";
409 struct s2n_blob client_key = { 0 };
410 RESULT_GUARD_POSIX(s2n_blob_init(&client_key, client_key_bytes, cipher->key_material_size));
411 RESULT_GUARD(cipher->init(&conn->secure->client_key));
412 RESULT_GUARD(cipher->set_encryption_key(&conn->secure->client_key, &client_key));
413
414 uint8_t server_key_bytes[S2N_TLS13_SECRET_MAX_LEN] = "server key";
415 struct s2n_blob server_key = { 0 };
416 RESULT_GUARD_POSIX(s2n_blob_init(&server_key, server_key_bytes, cipher->key_material_size));
417 RESULT_GUARD(cipher->init(&conn->secure->server_key));
418 RESULT_GUARD(cipher->set_encryption_key(&conn->secure->server_key, &server_key));
419
420 conn->client = conn->secure;
421 conn->server = conn->secure;
422
423 return S2N_RESULT_OK;
424 }
425
426 S2N_RESULT s2n_set_all_mutually_supported_groups(struct s2n_connection *conn)
427 {
428 RESULT_ENSURE_REF(conn);
429
430 const struct s2n_ecc_preferences *ecc_pref = NULL;
431 RESULT_GUARD_POSIX(s2n_connection_get_ecc_preferences(conn, &ecc_pref));
432 RESULT_ENSURE_REF(ecc_pref);
433
434 for (size_t i = 0; i < ecc_pref->count; i++) {
435 conn->kex_params.mutually_supported_curves[i] = ecc_pref->ecc_curves[i];
436 }
437
438 const struct s2n_kem_preferences *kem_pref = NULL;
439 RESULT_GUARD_POSIX(s2n_connection_get_kem_preferences(conn, &kem_pref));
440 RESULT_ENSURE_REF(kem_pref);
441
442 for (size_t i = 0; i < kem_pref->tls13_kem_group_count; i++) {
443 conn->kex_params.mutually_supported_kem_groups[i] = kem_pref->tls13_kem_groups[i];
444 }
445
446 return S2N_RESULT_OK;
447 }
448
449 S2N_RESULT s2n_skip_handshake(struct s2n_connection *conn)
450 {
451 conn->handshake.handshake_type = NEGOTIATED | FULL_HANDSHAKE;
452 while (!s2n_handshake_is_complete(conn)) {
453 conn->handshake.message_number++;
454 }
455 return S2N_RESULT_OK;
456 }