blob: 34c23eaf76ac2141bab2aee3cd59e1acac0a4c68 [file] [log] [blame]
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001/*
2 * UDP proxy: emulate an unreliable UDP connexion for DTLS testing
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020018 */
19
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020020/*
21 * Warning: this is an internal utility program we use for tests.
22 * It does break some abstractions from the NET layer, and is thus NOT an
23 * example of good general usage.
24 */
25
Mateusz Starzyk6c2e9b62021-05-19 17:54:54 +020026#define MBEDTLS_ALLOW_PRIVATE_ACCESS
27
Bence Szépkútic662b362021-05-27 11:25:03 +020028#include "mbedtls/build_info.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if defined(MBEDTLS_PLATFORM_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020031# include "mbedtls/platform.h"
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000032#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020033# include <stdio.h>
34# include <stdlib.h>
35# include <time.h>
36# define mbedtls_time time
37# define mbedtls_time_t time_t
38# define mbedtls_printf printf
39# define mbedtls_calloc calloc
40# define mbedtls_free free
41# define mbedtls_exit exit
42# define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
43# define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Andres Amaya Garcia80081a62018-04-29 21:58:53 +010044#endif /* MBEDTLS_PLATFORM_C */
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if !defined(MBEDTLS_NET_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020047int main(void)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020048{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020049 mbedtls_printf("MBEDTLS_NET_C not defined.\n");
50 mbedtls_exit(0);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020051}
52#else
53
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020054# include "mbedtls/net_sockets.h"
55# include "mbedtls/error.h"
56# include "mbedtls/ssl.h"
57# include "mbedtls/timing.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020058
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020059# include <string.h>
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020060
61/* For select() */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020062# if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
63 !defined(EFI32)
64# include <winsock2.h>
65# include <windows.h>
66# if defined(_MSC_VER)
67# if defined(_WIN32_WCE)
68# pragma comment(lib, "ws2.lib")
69# else
70# pragma comment(lib, "ws2_32.lib")
71# endif
72# endif /* _MSC_VER */
73# else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
74# include <sys/time.h>
75# include <sys/types.h>
76# include <unistd.h>
77# endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020078
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020079# define MAX_MSG_SIZE 16384 + 2048 /* max record/datagram size */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020080
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020081# define DFL_SERVER_ADDR "localhost"
82# define DFL_SERVER_PORT "4433"
83# define DFL_LISTEN_ADDR "localhost"
84# define DFL_LISTEN_PORT "5556"
85# define DFL_PACK 0
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020086
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020087# if defined(MBEDTLS_TIMING_C)
88# define USAGE_PACK \
89 " pack=%%d default: 0 (don't pack)\n" \
90 " options: t > 0 (pack for t milliseconds)\n"
91# else
92# define USAGE_PACK
93# endif
Hanno Becker0cc77742017-10-31 14:10:07 +000094
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020095# define USAGE \
96 "\n usage: udp_proxy param=<>...\n" \
97 "\n acceptable parameters:\n" \
98 " server_addr=%%s default: localhost\n" \
99 " server_port=%%d default: 4433\n" \
100 " listen_addr=%%s default: localhost\n" \
101 " listen_port=%%d default: 4433\n" \
102 "\n" \
103 " duplicate=%%d default: 0 (no duplication)\n" \
104 " duplicate about 1:N packets randomly\n" \
105 " delay=%%d default: 0 (no delayed packets)\n" \
106 " delay about 1:N packets randomly\n" \
107 " delay_ccs=0/1 default: 0 (don't delay ChangeCipherSpec)\n" \
108 " delay_cli=%%s Handshake message from client that should be\n" \
109 " delayed. Possible values are 'ClientHello',\n" \
110 " 'Certificate', 'CertificateVerify', and\n" \
111 " 'ClientKeyExchange'.\n" \
112 " May be used multiple times, even for the same\n" \
113 " message, in which case the respective message\n" \
114 " gets delayed multiple times.\n" \
115 " delay_srv=%%s Handshake message from server that should be\n" \
116 " delayed. Possible values are 'HelloRequest',\n" \
117 " 'ServerHello', 'ServerHelloDone', 'Certificate'\n" \
118 " 'ServerKeyExchange', 'NewSessionTicket',\n" \
119 " 'HelloVerifyRequest' and ''CertificateRequest'.\n" \
120 " May be used multiple times, even for the same\n" \
121 " message, in which case the respective message\n" \
122 " gets delayed multiple times.\n" \
123 " drop=%%d default: 0 (no dropped packets)\n" \
124 " drop about 1:N packets randomly\n" \
125 " mtu=%%d default: 0 (unlimited)\n" \
126 " drop packets larger than N bytes\n" \
127 " bad_ad=0/1 default: 0 (don't add bad ApplicationData)\n" \
128 " bad_cid=%%d default: 0 (don't corrupt Connection IDs)\n" \
129 " duplicate 1:N packets containing a CID,\n" \
130 " modifying CID in first instance of the packet.\n" \
131 " protect_hvr=0/1 default: 0 (don't protect HelloVerifyRequest)\n" \
132 " protect_len=%%d default: (don't protect packets of this size)\n" \
133 " inject_clihlo=0/1 default: 0 (don't inject fake ClientHello)\n" \
134 "\n" \
135 " seed=%%d default: (use current time)\n" USAGE_PACK \
136 "\n"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200137
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200138/*
139 * global options
140 */
Hanno Becker01ea7782018-08-17 13:33:41 +0100141
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200142# define MAX_DELAYED_HS 10
Hanno Becker01ea7782018-08-17 13:33:41 +0100143
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200144static struct options {
145 const char *server_addr; /* address to forward packets to */
146 const char *server_port; /* port to forward packets to */
147 const char *listen_addr; /* address for accepting client connections */
148 const char *listen_port; /* port for accepting client connections */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200149
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200150 int duplicate; /* duplicate 1 in N packets (none if 0) */
151 int delay; /* delay 1 packet in N (none if 0) */
152 int delay_ccs; /* delay ChangeCipherSpec */
153 char *delay_cli[MAX_DELAYED_HS]; /* handshake types of messages from
154 * client that should be delayed. */
155 uint8_t delay_cli_cnt; /* Number of entries in delay_cli. */
156 char *delay_srv[MAX_DELAYED_HS]; /* handshake types of messages from
157 * server that should be delayed. */
158 uint8_t delay_srv_cnt; /* Number of entries in delay_srv. */
159 int drop; /* drop 1 packet in N (none if 0) */
160 int mtu; /* drop packets larger than this */
161 int bad_ad; /* inject corrupted ApplicationData record */
162 unsigned bad_cid; /* inject corrupted CID record */
163 int protect_hvr; /* never drop or delay HelloVerifyRequest */
164 int protect_len; /* never drop/delay packet of the given size*/
165 int inject_clihlo; /* inject fake ClientHello after handshake */
166 unsigned pack; /* merge packets into single datagram for
167 * at most \c merge milliseconds if > 0 */
168 unsigned int seed; /* seed for "random" events */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200169} opt;
170
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200171static void exit_usage(const char *name, const char *value)
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200172{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200173 if (value == NULL)
174 mbedtls_printf(" unknown option or missing value: %s\n", name);
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200175 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200176 mbedtls_printf(" option %s: illegal value: %s\n", name, value);
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200177
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200178 mbedtls_printf(USAGE);
179 mbedtls_exit(1);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200180}
181
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200182static void get_options(int argc, char *argv[])
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200183{
184 int i;
185 char *p, *q;
186
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200187 opt.server_addr = DFL_SERVER_ADDR;
188 opt.server_port = DFL_SERVER_PORT;
189 opt.listen_addr = DFL_LISTEN_ADDR;
190 opt.listen_port = DFL_LISTEN_PORT;
191 opt.pack = DFL_PACK;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200192 /* Other members default to 0 */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200193
Hanno Becker01ea7782018-08-17 13:33:41 +0100194 opt.delay_cli_cnt = 0;
195 opt.delay_srv_cnt = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200196 memset(opt.delay_cli, 0, sizeof(opt.delay_cli));
197 memset(opt.delay_srv, 0, sizeof(opt.delay_srv));
Hanno Becker01ea7782018-08-17 13:33:41 +0100198
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200199 for (i = 1; i < argc; i++) {
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200200 p = argv[i];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200201 if ((q = strchr(p, '=')) == NULL)
202 exit_usage(p, NULL);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200203 *q++ = '\0';
204
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200205 if (strcmp(p, "server_addr") == 0)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200206 opt.server_addr = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200207 else if (strcmp(p, "server_port") == 0)
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200208 opt.server_port = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200209 else if (strcmp(p, "listen_addr") == 0)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200210 opt.listen_addr = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200211 else if (strcmp(p, "listen_port") == 0)
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200212 opt.listen_port = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200213 else if (strcmp(p, "duplicate") == 0) {
214 opt.duplicate = atoi(q);
215 if (opt.duplicate < 0 || opt.duplicate > 20)
216 exit_usage(p, q);
217 } else if (strcmp(p, "delay") == 0) {
218 opt.delay = atoi(q);
219 if (opt.delay < 0 || opt.delay > 20 || opt.delay == 1)
220 exit_usage(p, q);
221 } else if (strcmp(p, "delay_ccs") == 0) {
222 opt.delay_ccs = atoi(q);
223 if (opt.delay_ccs < 0 || opt.delay_ccs > 1)
224 exit_usage(p, q);
225 } else if (strcmp(p, "delay_cli") == 0 || strcmp(p, "delay_srv") == 0) {
Hanno Becker01ea7782018-08-17 13:33:41 +0100226 uint8_t *delay_cnt;
227 char **delay_list;
228 size_t len;
229 char *buf;
230
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200231 if (strcmp(p, "delay_cli") == 0) {
232 delay_cnt = &opt.delay_cli_cnt;
Hanno Becker01ea7782018-08-17 13:33:41 +0100233 delay_list = opt.delay_cli;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200234 } else {
235 delay_cnt = &opt.delay_srv_cnt;
Hanno Becker01ea7782018-08-17 13:33:41 +0100236 delay_list = opt.delay_srv;
237 }
238
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200239 if (*delay_cnt == MAX_DELAYED_HS) {
240 mbedtls_printf(" too many uses of %s: only %d allowed\n", p,
241 MAX_DELAYED_HS);
242 exit_usage(p, NULL);
Hanno Becker01ea7782018-08-17 13:33:41 +0100243 }
244
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200245 len = strlen(q);
246 buf = mbedtls_calloc(1, len + 1);
247 if (buf == NULL) {
248 mbedtls_printf(" Allocation failure\n");
249 exit(1);
Hanno Becker01ea7782018-08-17 13:33:41 +0100250 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200251 memcpy(buf, q, len + 1);
Hanno Becker01ea7782018-08-17 13:33:41 +0100252
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200253 delay_list[(*delay_cnt)++] = buf;
254 } else if (strcmp(p, "drop") == 0) {
255 opt.drop = atoi(q);
256 if (opt.drop < 0 || opt.drop > 20 || opt.drop == 1)
257 exit_usage(p, q);
258 } else if (strcmp(p, "pack") == 0) {
259# if defined(MBEDTLS_TIMING_C)
260 opt.pack = (unsigned)atoi(q);
261# else
262 mbedtls_printf(
263 " option pack only defined if MBEDTLS_TIMING_C is enabled\n");
264 exit(1);
265# endif
266 } else if (strcmp(p, "mtu") == 0) {
267 opt.mtu = atoi(q);
268 if (opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE)
269 exit_usage(p, q);
270 } else if (strcmp(p, "bad_ad") == 0) {
271 opt.bad_ad = atoi(q);
272 if (opt.bad_ad < 0 || opt.bad_ad > 1)
273 exit_usage(p, q);
Hanno Becker01ea7782018-08-17 13:33:41 +0100274 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200275# if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
276 else if (strcmp(p, "bad_cid") == 0) {
277 opt.bad_cid = (unsigned)atoi(q);
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200278 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200279# endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
280 else if (strcmp(p, "protect_hvr") == 0) {
281 opt.protect_hvr = atoi(q);
282 if (opt.protect_hvr < 0 || opt.protect_hvr > 1)
283 exit_usage(p, q);
284 } else if (strcmp(p, "protect_len") == 0) {
285 opt.protect_len = atoi(q);
286 if (opt.protect_len < 0)
287 exit_usage(p, q);
288 } else if (strcmp(p, "inject_clihlo") == 0) {
289 opt.inject_clihlo = atoi(q);
290 if (opt.inject_clihlo < 0 || opt.inject_clihlo > 1)
291 exit_usage(p, q);
292 } else if (strcmp(p, "seed") == 0) {
293 opt.seed = atoi(q);
294 if (opt.seed == 0)
295 exit_usage(p, q);
296 } else
297 exit_usage(p, NULL);
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200298 }
299}
300
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200301static const char *msg_type(unsigned char *msg, size_t len)
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200302{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200303 if (len < 1)
304 return "Invalid";
305 switch (msg[0]) {
306 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
307 return "ChangeCipherSpec";
308 case MBEDTLS_SSL_MSG_ALERT:
309 return "Alert";
310 case MBEDTLS_SSL_MSG_APPLICATION_DATA:
311 return "ApplicationData";
312 case MBEDTLS_SSL_MSG_CID:
313 return "CID";
314 case MBEDTLS_SSL_MSG_HANDSHAKE:
315 break; /* See below */
316 default:
317 return "Unknown";
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200318 }
319
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200320 if (len < 13 + 12)
321 return "Invalid handshake";
Manuel Pégourié-Gonnard8cc7e032014-09-25 12:59:05 +0200322
323 /*
324 * Our handshake message are less than 2^16 bytes long, so they should
325 * have 0 as the first byte of length, frag_offset and frag_length.
326 * Otherwise, assume they are encrypted.
327 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200328 if (msg[14] || msg[19] || msg[22])
329 return "Encrypted handshake";
Manuel Pégourié-Gonnard8cc7e032014-09-25 12:59:05 +0200330
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200331 switch (msg[13]) {
332 case MBEDTLS_SSL_HS_HELLO_REQUEST:
333 return "HelloRequest";
334 case MBEDTLS_SSL_HS_CLIENT_HELLO:
335 return "ClientHello";
336 case MBEDTLS_SSL_HS_SERVER_HELLO:
337 return "ServerHello";
338 case MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST:
339 return "HelloVerifyRequest";
340 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
341 return "NewSessionTicket";
342 case MBEDTLS_SSL_HS_CERTIFICATE:
343 return "Certificate";
344 case MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE:
345 return "ServerKeyExchange";
346 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
347 return "CertificateRequest";
348 case MBEDTLS_SSL_HS_SERVER_HELLO_DONE:
349 return "ServerHelloDone";
350 case MBEDTLS_SSL_HS_CERTIFICATE_VERIFY:
351 return "CertificateVerify";
352 case MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE:
353 return "ClientKeyExchange";
354 case MBEDTLS_SSL_HS_FINISHED:
355 return "Finished";
356 default:
357 return "Unknown handshake";
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200358 }
359}
360
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200361# if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200362/* Return elapsed time in milliseconds since the first call */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200363static unsigned ellapsed_time(void)
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200364{
Hanno Becker92474da2017-10-31 14:09:30 +0000365 static int initialized = 0;
366 static struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200367
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200368 if (initialized == 0) {
369 (void)mbedtls_timing_get_timer(&hires, 1);
Hanno Becker92474da2017-10-31 14:09:30 +0000370 initialized = 1;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200371 return 0;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200372 }
373
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200374 return mbedtls_timing_get_timer(&hires, 0);
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200375}
376
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200377typedef struct {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100378 mbedtls_net_context *ctx;
379
380 const char *description;
381
Hanno Becker77abef52017-11-02 10:50:28 +0000382 unsigned packet_lifetime;
383 unsigned num_datagrams;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100384
385 unsigned char data[MAX_MSG_SIZE];
Hanno Beckera5e68972017-12-06 08:35:02 +0000386 size_t len;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100387
388} ctx_buffer;
389
390static ctx_buffer outbuf[2];
391
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200392static int ctx_buffer_flush(ctx_buffer *buf)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100393{
394 int ret;
395
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200396 mbedtls_printf(" %05u flush %s: %u bytes, %u datagrams, last %u ms\n",
397 ellapsed_time(), buf->description, (unsigned)buf->len,
398 buf->num_datagrams, ellapsed_time() - buf->packet_lifetime);
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100399
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200400 ret = mbedtls_net_send(buf->ctx, buf->data, buf->len);
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100401
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200402 buf->len = 0;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100403 buf->num_datagrams = 0;
404
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200405 return ret;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100406}
407
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200408static unsigned ctx_buffer_time_remaining(ctx_buffer *buf)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100409{
Hanno Becker77abef52017-11-02 10:50:28 +0000410 unsigned const cur_time = ellapsed_time();
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100411
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200412 if (buf->num_datagrams == 0)
413 return (unsigned)-1;
Hanno Becker77abef52017-11-02 10:50:28 +0000414
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200415 if (cur_time - buf->packet_lifetime >= opt.pack)
416 return 0;
Hanno Becker77abef52017-11-02 10:50:28 +0000417
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200418 return opt.pack - (cur_time - buf->packet_lifetime);
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100419}
420
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200421static int
422ctx_buffer_append(ctx_buffer *buf, const unsigned char *data, size_t len)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100423{
424 int ret;
425
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200426 if (len > (size_t)INT_MAX)
427 return -1;
Hanno Beckera5e68972017-12-06 08:35:02 +0000428
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200429 if (len > sizeof(buf->data)) {
430 mbedtls_printf(" ! buffer size %u too large (max %u)\n", (unsigned)len,
431 (unsigned)sizeof(buf->data));
432 return -1;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100433 }
434
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200435 if (sizeof(buf->data) - buf->len < len) {
436 if ((ret = ctx_buffer_flush(buf)) <= 0) {
437 mbedtls_printf("ctx_buffer_flush failed with -%#04x",
438 (unsigned int)-ret);
439 return ret;
Hanno Becker31f6e372019-05-08 15:36:31 +0100440 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100441 }
442
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200443 memcpy(buf->data + buf->len, data, len);
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100444
445 buf->len += len;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200446 if (++buf->num_datagrams == 1)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100447 buf->packet_lifetime = ellapsed_time();
448
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200449 return (int)len;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100450}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200451# endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100452
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200453static int
454dispatch_data(mbedtls_net_context *ctx, const unsigned char *data, size_t len)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100455{
Hanno Becker31f6e372019-05-08 15:36:31 +0100456 int ret;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200457# if defined(MBEDTLS_TIMING_C)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100458 ctx_buffer *buf = NULL;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200459 if (opt.pack > 0) {
460 if (outbuf[0].ctx == ctx)
Hanno Becker77abef52017-11-02 10:50:28 +0000461 buf = &outbuf[0];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200462 else if (outbuf[1].ctx == ctx)
Hanno Becker77abef52017-11-02 10:50:28 +0000463 buf = &outbuf[1];
Hanno Becker0cc77742017-10-31 14:10:07 +0000464
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200465 if (buf == NULL)
466 return -1;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100467
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200468 return ctx_buffer_append(buf, data, len);
Hanno Becker77abef52017-11-02 10:50:28 +0000469 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200470# endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100471
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200472 ret = mbedtls_net_send(ctx, data, len);
473 if (ret < 0) {
474 mbedtls_printf("net_send returned -%#04x\n", (unsigned int)-ret);
Hanno Becker31f6e372019-05-08 15:36:31 +0100475 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200476 return ret;
Hanno Becker0cc77742017-10-31 14:10:07 +0000477}
478
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200479typedef struct {
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200480 mbedtls_net_context *dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200481 const char *way;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200482 const char *type;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200483 unsigned len;
484 unsigned char buf[MAX_MSG_SIZE];
485} packet;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200486
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200487/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200488void print_packet(const packet *p, const char *why)
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200489{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200490# if defined(MBEDTLS_TIMING_C)
491 if (why == NULL)
492 mbedtls_printf(" %05u dispatch %s %s (%u bytes)\n", ellapsed_time(),
493 p->way, p->type, p->len);
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200494 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200495 mbedtls_printf(" %05u dispatch %s %s (%u bytes): %s\n",
496 ellapsed_time(), p->way, p->type, p->len, why);
497# else
498 if (why == NULL)
499 mbedtls_printf(" dispatch %s %s (%u bytes)\n", p->way, p->type,
500 p->len);
Hanno Becker0cc77742017-10-31 14:10:07 +0000501 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200502 mbedtls_printf(" dispatch %s %s (%u bytes): %s\n", p->way,
503 p->type, p->len, why);
504# endif
Hanno Becker0cc77742017-10-31 14:10:07 +0000505
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200506 fflush(stdout);
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200507}
508
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100509/*
510 * In order to test the server's behaviour when receiving a ClientHello after
511 * the connection is established (this could be a hard reset from the client,
512 * but the server must not drop the existing connection before establishing
513 * client reachability, see RFC 6347 Section 4.2.8), we memorize the first
514 * ClientHello we see (which can't have a cookie), then replay it after the
515 * first ApplicationData record - then we're done.
516 *
517 * This is controlled by the inject_clihlo option.
518 *
519 * We want an explicit state and a place to store the packet.
520 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200521typedef enum
522{
523 ICH_INIT, /* haven't seen the first ClientHello yet */
524 ICH_CACHED, /* cached the initial ClientHello */
525 ICH_INJECTED, /* ClientHello already injected, done */
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200526} inject_clihlo_state_t;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100527
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200528static inject_clihlo_state_t inject_clihlo_state;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100529static packet initial_clihlo;
530
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200531int send_packet(const packet *p, const char *why)
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200532{
533 int ret;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200534 mbedtls_net_context *dst = p->dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200535
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100536 /* save initial ClientHello? */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200537 if (opt.inject_clihlo != 0 && inject_clihlo_state == ICH_INIT &&
538 strcmp(p->type, "ClientHello") == 0) {
539 memcpy(&initial_clihlo, p, sizeof(packet));
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200540 inject_clihlo_state = ICH_CACHED;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100541 }
542
Hanno Becker98aaf252019-05-24 10:07:42 +0100543 /* insert corrupted CID record? */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200544 if (opt.bad_cid != 0 && strcmp(p->type, "CID") == 0 &&
545 (rand() % opt.bad_cid) == 0) {
Hanno Becker98aaf252019-05-24 10:07:42 +0100546 unsigned char buf[MAX_MSG_SIZE];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200547 memcpy(buf, p->buf, p->len);
Hanno Becker98aaf252019-05-24 10:07:42 +0100548
549 /* The CID resides at offset 11 in the DTLS record header. */
550 buf[11] ^= 1;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200551 print_packet(p, "modified CID");
Hanno Becker98aaf252019-05-24 10:07:42 +0100552
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200553 if ((ret = dispatch_data(dst, buf, p->len)) <= 0) {
554 mbedtls_printf(" ! dispatch returned %d\n", ret);
555 return ret;
Hanno Becker98aaf252019-05-24 10:07:42 +0100556 }
557 }
558
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200559 /* insert corrupted ApplicationData record? */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200560 if (opt.bad_ad && strcmp(p->type, "ApplicationData") == 0) {
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200561 unsigned char buf[MAX_MSG_SIZE];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200562 memcpy(buf, p->buf, p->len);
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200563
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200564 if (p->len <= 13) {
565 mbedtls_printf(" ! can't corrupt empty AD record");
566 } else {
Hanno Beckerfbb0b702017-05-26 16:55:07 +0100567 ++buf[13];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200568 print_packet(p, "corrupted");
Hanno Beckerfbb0b702017-05-26 16:55:07 +0100569 }
570
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200571 if ((ret = dispatch_data(dst, buf, p->len)) <= 0) {
572 mbedtls_printf(" ! dispatch returned %d\n", ret);
573 return ret;
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200574 }
575 }
576
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200577 print_packet(p, why);
578 if ((ret = dispatch_data(dst, p->buf, p->len)) <= 0) {
579 mbedtls_printf(" ! dispatch returned %d\n", ret);
580 return ret;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200581 }
582
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200583 /* Don't duplicate Application Data, only handshake covered */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200584 if (opt.duplicate != 0 && strcmp(p->type, "ApplicationData") != 0 &&
585 rand() % opt.duplicate == 0) {
586 print_packet(p, "duplicated");
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200587
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200588 if ((ret = dispatch_data(dst, p->buf, p->len)) <= 0) {
589 mbedtls_printf(" ! dispatch returned %d\n", ret);
590 return ret;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200591 }
592 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200593
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100594 /* Inject ClientHello after first ApplicationData */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200595 if (opt.inject_clihlo != 0 && inject_clihlo_state == ICH_CACHED &&
596 strcmp(p->type, "ApplicationData") == 0) {
597 print_packet(&initial_clihlo, "injected");
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100598
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200599 if ((ret = dispatch_data(dst, initial_clihlo.buf,
600 initial_clihlo.len)) <= 0) {
601 mbedtls_printf(" ! dispatch returned %d\n", ret);
602 return ret;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100603 }
604
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200605 inject_clihlo_state = ICH_INJECTED;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100606 }
607
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200608 return 0;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200609}
610
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200611# define MAX_DELAYED_MSG 5
Hanno Becker101bcba2018-08-21 16:39:51 +0100612static size_t prev_len;
613static packet prev[MAX_DELAYED_MSG];
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200614
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200615void clear_pending(void)
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200616{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200617 memset(&prev, 0, sizeof(prev));
Hanno Becker101bcba2018-08-21 16:39:51 +0100618 prev_len = 0;
619}
620
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200621void delay_packet(packet *delay)
Hanno Becker101bcba2018-08-21 16:39:51 +0100622{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200623 if (prev_len == MAX_DELAYED_MSG)
Hanno Becker101bcba2018-08-21 16:39:51 +0100624 return;
625
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200626 memcpy(&prev[prev_len++], delay, sizeof(packet));
Hanno Becker101bcba2018-08-21 16:39:51 +0100627}
628
629int send_delayed()
630{
631 uint8_t offset;
632 int ret;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200633 for (offset = 0; offset < prev_len; offset++) {
634 ret = send_packet(&prev[offset], "delayed");
635 if (ret != 0)
636 return ret;
Hanno Becker101bcba2018-08-21 16:39:51 +0100637 }
638
639 clear_pending();
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200640 return 0;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200641}
642
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200643/*
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200644 * Avoid dropping or delaying a packet that was already dropped or delayed
645 * ("held") twice: this only results in uninteresting timeouts. We can't rely
646 * on type to identify packets, since during renegotiation they're all
647 * encrypted. So, rely on size mod 2048 (which is usually just size).
648 *
649 * We only hold packets at the level of entire datagrams, not at the level
Hanno Becker961e6772019-06-04 13:04:28 +0100650 * of records. In particular, if the peer changes the way it packs multiple
651 * records into a single datagram, we don't necessarily count the number of
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200652 * times a record has been held correctly. However, the only known reason
Hanno Becker961e6772019-06-04 13:04:28 +0100653 * why a peer would change datagram packing is disabling the latter on
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200654 * retransmission, in which case we'd hold involved records at most
655 * HOLD_MAX + 1 times.
656 */
657static unsigned char held[2048] = { 0 };
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200658# define HOLD_MAX 2
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200659
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200660int handle_message(const char *way,
661 mbedtls_net_context *dst,
662 mbedtls_net_context *src)
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200663{
664 int ret;
665 packet cur;
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200666 size_t id;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200667
Hanno Becker01ea7782018-08-17 13:33:41 +0100668 uint8_t delay_idx;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200669 char **delay_list;
Hanno Becker01ea7782018-08-17 13:33:41 +0100670 uint8_t delay_list_len;
671
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +0200672 /* receive packet */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200673 if ((ret = mbedtls_net_recv(src, cur.buf, sizeof(cur.buf))) <= 0) {
674 mbedtls_printf(" ! mbedtls_net_recv returned %d\n", ret);
675 return ret;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200676 }
677
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200678 cur.len = ret;
679 cur.type = msg_type(cur.buf, cur.len);
680 cur.way = way;
681 cur.dst = dst;
682 print_packet(&cur, NULL);
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200683
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200684 id = cur.len % sizeof(held);
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200685
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200686 if (strcmp(way, "S <- C") == 0) {
687 delay_list = opt.delay_cli;
Hanno Becker01ea7782018-08-17 13:33:41 +0100688 delay_list_len = opt.delay_cli_cnt;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200689 } else {
690 delay_list = opt.delay_srv;
Hanno Becker01ea7782018-08-17 13:33:41 +0100691 delay_list_len = opt.delay_srv_cnt;
692 }
Hanno Beckercf469452018-08-28 10:09:47 +0100693
Hanno Becker01ea7782018-08-17 13:33:41 +0100694 /* Check if message type is in the list of messages
695 * that should be delayed */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200696 for (delay_idx = 0; delay_idx < delay_list_len; delay_idx++) {
697 if (delay_list[delay_idx] == NULL)
Hanno Becker01ea7782018-08-17 13:33:41 +0100698 continue;
699
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200700 if (strcmp(delay_list[delay_idx], cur.type) == 0) {
Hanno Becker01ea7782018-08-17 13:33:41 +0100701 /* Delay message */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200702 delay_packet(&cur);
Hanno Becker01ea7782018-08-17 13:33:41 +0100703
704 /* Remove entry from list */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200705 mbedtls_free(delay_list[delay_idx]);
Hanno Becker01ea7782018-08-17 13:33:41 +0100706 delay_list[delay_idx] = NULL;
707
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200708 return 0;
Hanno Becker01ea7782018-08-17 13:33:41 +0100709 }
710 }
711
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200712 /* do we want to drop, delay, or forward it? */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200713 if ((opt.mtu != 0 && cur.len > (unsigned)opt.mtu) ||
714 (opt.drop != 0 && strcmp(cur.type, "CID") != 0 &&
715 strcmp(cur.type, "ApplicationData") != 0 &&
716 !(opt.protect_hvr && strcmp(cur.type, "HelloVerifyRequest") == 0) &&
717 cur.len != (size_t)opt.protect_len && held[id] < HOLD_MAX &&
718 rand() % opt.drop == 0)) {
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200719 ++held[id];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200720 } else if ((opt.delay_ccs == 1 &&
721 strcmp(cur.type, "ChangeCipherSpec") == 0) ||
722 (opt.delay != 0 && strcmp(cur.type, "CID") != 0 &&
723 strcmp(cur.type, "ApplicationData") != 0 &&
724 !(opt.protect_hvr &&
725 strcmp(cur.type, "HelloVerifyRequest") == 0) &&
726 cur.len != (size_t)opt.protect_len && held[id] < HOLD_MAX &&
727 rand() % opt.delay == 0)) {
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200728 ++held[id];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200729 delay_packet(&cur);
730 } else {
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200731 /* forward and possibly duplicate */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200732 if ((ret = send_packet(&cur, "forwarded")) != 0)
733 return ret;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200734
Hanno Becker101bcba2018-08-21 16:39:51 +0100735 /* send previously delayed messages if any */
736 ret = send_delayed();
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200737 if (ret != 0)
738 return ret;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200739 }
740
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200741 return 0;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200742}
743
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200744int main(int argc, char *argv[])
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200745{
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100746 int ret = 1;
747 int exit_code = MBEDTLS_EXIT_FAILURE;
Hanno Becker01ea7782018-08-17 13:33:41 +0100748 uint8_t delay_idx;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200749
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200750 mbedtls_net_context listen_fd, client_fd, server_fd;
Hanno Becker77abef52017-11-02 10:50:28 +0000751
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200752# if defined(MBEDTLS_TIMING_C)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100753 struct timeval tm;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200754# endif
Hanno Becker77abef52017-11-02 10:50:28 +0000755
756 struct timeval *tm_ptr = NULL;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200757
758 int nb_fds;
759 fd_set read_fds;
760
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200761 mbedtls_net_init(&listen_fd);
762 mbedtls_net_init(&client_fd);
763 mbedtls_net_init(&server_fd);
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200764
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200765 get_options(argc, argv);
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200766
767 /*
768 * Decisions to drop/delay/duplicate packets are pseudo-random: dropping
769 * exactly 1 in N packets would lead to problems when a flight has exactly
770 * N packets: the same packet would be dropped on every resend.
771 *
772 * In order to be able to reproduce problems reliably, the seed may be
773 * specified explicitly.
774 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200775 if (opt.seed == 0) {
776 opt.seed = (unsigned int)time(NULL);
777 mbedtls_printf(" . Pseudo-random seed: %u\n", opt.seed);
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200778 }
779
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200780 srand(opt.seed);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200781
782 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200783 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200784 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200785 mbedtls_printf(" . Connect to server on UDP/%s/%s ...", opt.server_addr,
786 opt.server_port);
787 fflush(stdout);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200788
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200789 if ((ret = mbedtls_net_connect(&server_fd, opt.server_addr, opt.server_port,
790 MBEDTLS_NET_PROTO_UDP)) != 0) {
791 mbedtls_printf(" failed\n ! mbedtls_net_connect returned %d\n\n", ret);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200792 goto exit;
793 }
794
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200795 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200796
797 /*
798 * 1. Setup the "listening" UDP socket
799 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200800 mbedtls_printf(" . Bind on UDP/%s/%s ...", opt.listen_addr,
801 opt.listen_port);
802 fflush(stdout);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200803
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200804 if ((ret = mbedtls_net_bind(&listen_fd, opt.listen_addr, opt.listen_port,
805 MBEDTLS_NET_PROTO_UDP)) != 0) {
806 mbedtls_printf(" failed\n ! mbedtls_net_bind returned %d\n\n", ret);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200807 goto exit;
808 }
809
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200810 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200811
812 /*
813 * 2. Wait until a client connects
814 */
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200815accept:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200816 mbedtls_net_free(&client_fd);
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200817
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200818 mbedtls_printf(" . Waiting for a remote connection ...");
819 fflush(stdout);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200820
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200821 if ((ret = mbedtls_net_accept(&listen_fd, &client_fd, NULL, 0, NULL)) !=
822 0) {
823 mbedtls_printf(" failed\n ! mbedtls_net_accept returned %d\n\n", ret);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200824 goto exit;
825 }
826
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200827 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200828
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200829 /*
830 * 3. Forward packets forever (kill the process to terminate it)
831 */
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200832 clear_pending();
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200833 memset(held, 0, sizeof(held));
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200834
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200835 nb_fds = client_fd.fd;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200836 if (nb_fds < server_fd.fd)
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200837 nb_fds = server_fd.fd;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200838 if (nb_fds < listen_fd.fd)
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200839 nb_fds = listen_fd.fd;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200840 ++nb_fds;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200841
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200842# if defined(MBEDTLS_TIMING_C)
843 if (opt.pack > 0) {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100844 outbuf[0].ctx = &server_fd;
845 outbuf[0].description = "S <- C";
846 outbuf[0].num_datagrams = 0;
847 outbuf[0].len = 0;
848
849 outbuf[1].ctx = &client_fd;
850 outbuf[1].description = "S -> C";
851 outbuf[1].num_datagrams = 0;
852 outbuf[1].len = 0;
853 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200854# endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100855
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200856 while (1) {
857# if defined(MBEDTLS_TIMING_C)
858 if (opt.pack > 0) {
Hanno Becker77abef52017-11-02 10:50:28 +0000859 unsigned max_wait_server, max_wait_client, max_wait;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200860 max_wait_server = ctx_buffer_time_remaining(&outbuf[0]);
861 max_wait_client = ctx_buffer_time_remaining(&outbuf[1]);
Hanno Becker77abef52017-11-02 10:50:28 +0000862
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200863 max_wait = (unsigned)-1;
Hanno Becker77abef52017-11-02 10:50:28 +0000864
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200865 if (max_wait_server == 0)
866 ctx_buffer_flush(&outbuf[0]);
Hanno Becker77abef52017-11-02 10:50:28 +0000867 else
868 max_wait = max_wait_server;
869
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200870 if (max_wait_client == 0)
871 ctx_buffer_flush(&outbuf[1]);
872 else {
873 if (max_wait_client < max_wait)
Hanno Becker77abef52017-11-02 10:50:28 +0000874 max_wait = max_wait_client;
875 }
876
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200877 if (max_wait != (unsigned)-1) {
878 tm.tv_sec = max_wait / 1000;
879 tm.tv_usec = (max_wait % 1000) * 1000;
Hanno Becker77abef52017-11-02 10:50:28 +0000880
881 tm_ptr = &tm;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200882 } else {
Hanno Becker77abef52017-11-02 10:50:28 +0000883 tm_ptr = NULL;
884 }
885 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200886# endif /* MBEDTLS_TIMING_C */
Hanno Becker77abef52017-11-02 10:50:28 +0000887
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200888 FD_ZERO(&read_fds);
889 FD_SET(server_fd.fd, &read_fds);
890 FD_SET(client_fd.fd, &read_fds);
891 FD_SET(listen_fd.fd, &read_fds);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200892
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200893 if ((ret = select(nb_fds, &read_fds, NULL, NULL, tm_ptr)) < 0) {
894 perror("select");
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200895 goto exit;
896 }
897
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200898 if (FD_ISSET(listen_fd.fd, &read_fds))
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200899 goto accept;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200900
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200901 if (FD_ISSET(client_fd.fd, &read_fds)) {
902 if ((ret = handle_message("S <- C", &server_fd, &client_fd)) != 0)
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200903 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200904 }
905
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200906 if (FD_ISSET(server_fd.fd, &read_fds)) {
907 if ((ret = handle_message("S -> C", &client_fd, &server_fd)) != 0)
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200908 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200909 }
910 }
911
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100912 exit_code = MBEDTLS_EXIT_SUCCESS;
913
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200914exit:
915
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200916# ifdef MBEDTLS_ERROR_C
917 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200918 char error_buf[100];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200919 mbedtls_strerror(ret, error_buf, 100);
920 mbedtls_printf("Last error was: -0x%04X - %s\n\n", (unsigned int)-ret,
921 error_buf);
922 fflush(stdout);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200923 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200924# endif
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200925
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200926 for (delay_idx = 0; delay_idx < MAX_DELAYED_HS; delay_idx++) {
927 mbedtls_free(opt.delay_cli[delay_idx]);
928 mbedtls_free(opt.delay_srv[delay_idx]);
Hanno Becker01ea7782018-08-17 13:33:41 +0100929 }
930
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200931 mbedtls_net_free(&client_fd);
932 mbedtls_net_free(&server_fd);
933 mbedtls_net_free(&listen_fd);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200934
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200935# if defined(_WIN32)
936 mbedtls_printf(" Press Enter to exit this program.\n");
937 fflush(stdout);
938 getchar();
939# endif
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200940
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200941 mbedtls_exit(exit_code);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200942}
943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944#endif /* MBEDTLS_NET_C */