blob: c6b56ec098f701f7a08404579db5061541515813 [file] [log] [blame]
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002 * UDP proxy: emulate an unreliable UDP connection for DTLS testing
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02006 */
7
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02008/*
9 * Warning: this is an internal utility program we use for tests.
10 * It does break some abstractions from the NET layer, and is thus NOT an
11 * example of good general usage.
12 */
13
Mateusz Starzyk6c2e9b62021-05-19 17:54:54 +020014#define MBEDTLS_ALLOW_PRIVATE_ACCESS
15
Bence Szépkútic662b362021-05-27 11:25:03 +020016#include "mbedtls/build_info.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020018#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000019#include "mbedtls/platform.h"
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000020#else
Simon Butcherd3138c32016-04-27 01:26:50 +010021#include <stdio.h>
22#include <stdlib.h>
David Horstmann5b9cb9e2021-11-29 17:28:13 +000023#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherd3138c32016-04-27 01:26:50 +010024#include <time.h>
Andres Amaya Garcia80081a62018-04-29 21:58:53 +010025#define mbedtls_time time
26#define mbedtls_time_t time_t
David Horstmann5b9cb9e2021-11-29 17:28:13 +000027#endif
Andres Amaya Garcia80081a62018-04-29 21:58:53 +010028#define mbedtls_printf printf
Hanno Becker01ea7782018-08-17 13:33:41 +010029#define mbedtls_calloc calloc
30#define mbedtls_free free
k-stachowiak776521a2019-05-23 09:46:47 +020031#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010032#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia80081a62018-04-29 21:58:53 +010033#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
34#endif /* MBEDTLS_PLATFORM_C */
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if !defined(MBEDTLS_NET_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010037int main(void)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020038{
Gilles Peskine449bd832023-01-11 14:50:10 +010039 mbedtls_printf("MBEDTLS_NET_C not defined.\n");
40 mbedtls_exit(0);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020041}
42#else
43
Andres AG788aa4a2016-09-14 14:32:09 +010044#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/error.h"
46#include "mbedtls/ssl.h"
Hanno Becker0cc77742017-10-31 14:10:07 +000047#include "mbedtls/timing.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020048
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +000049#include <string.h>
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020050
51/* For select() */
52#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
53 !defined(EFI32)
54#include <winsock2.h>
55#include <windows.h>
56#if defined(_MSC_VER)
57#if defined(_WIN32_WCE)
58#pragma comment( lib, "ws2.lib" )
59#else
60#pragma comment( lib, "ws2_32.lib" )
61#endif
62#endif /* _MSC_VER */
63#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Dave Rodgmanfaef6492022-05-11 16:05:16 +000064#if defined(MBEDTLS_HAVE_TIME) || (defined(MBEDTLS_TIMING_C) && !defined(MBEDTLS_TIMING_ALT))
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020065#include <sys/time.h>
Andrzej Kurek6056e7a2022-03-02 12:01:10 -050066#endif
Dave Rodgmanfaef6492022-05-11 16:05:16 +000067#include <sys/select.h>
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020068#include <sys/types.h>
69#include <unistd.h>
70#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
71
Manuel Pégourié-Gonnardb46780e2014-09-23 12:17:30 +020072#define MAX_MSG_SIZE 16384 + 2048 /* max record/datagram size */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020073
74#define DFL_SERVER_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020075#define DFL_SERVER_PORT "4433"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020076#define DFL_LISTEN_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020077#define DFL_LISTEN_PORT "5556"
Hanno Becker1dd62ea2017-05-22 14:30:59 +010078#define DFL_PACK 0
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020079
Hanno Becker0cc77742017-10-31 14:10:07 +000080#if defined(MBEDTLS_TIMING_C)
81#define USAGE_PACK \
82 " pack=%%d default: 0 (don't pack)\n" \
83 " options: t > 0 (pack for t milliseconds)\n"
84#else
85#define USAGE_PACK
86#endif
87
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020088#define USAGE \
89 "\n usage: udp_proxy param=<>...\n" \
90 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +020091 " server_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020092 " server_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +020093 " listen_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020094 " listen_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +020095 "\n" \
96 " duplicate=%%d default: 0 (no duplication)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +020097 " duplicate about 1:N packets randomly\n" \
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +020098 " delay=%%d default: 0 (no delayed packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +020099 " delay about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200100 " delay_ccs=0/1 default: 0 (don't delay ChangeCipherSpec)\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 " delay_cli=%%s Handshake message from client that should be\n" \
Hanno Becker01ea7782018-08-17 13:33:41 +0100102 " delayed. Possible values are 'ClientHello',\n" \
103 " 'Certificate', 'CertificateVerify', and\n" \
104 " 'ClientKeyExchange'.\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 " May be used multiple times, even for the same\n" \
106 " message, in which case the respective message\n" \
Hanno Becker01ea7782018-08-17 13:33:41 +0100107 " gets delayed multiple times.\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 " delay_srv=%%s Handshake message from server that should be\n" \
109 " delayed. Possible values are 'HelloRequest',\n" \
110 " 'ServerHello', 'ServerHelloDone', 'Certificate'\n" \
111 " 'ServerKeyExchange', 'NewSessionTicket',\n" \
112 " 'HelloVerifyRequest' and ''CertificateRequest'.\n" \
113 " May be used multiple times, even for the same\n" \
114 " message, in which case the respective message\n" \
Hanno Becker01ea7782018-08-17 13:33:41 +0100115 " gets delayed multiple times.\n" \
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200116 " drop=%%d default: 0 (no dropped packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200117 " drop about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200118 " mtu=%%d default: 0 (unlimited)\n" \
119 " drop packets larger than N bytes\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200120 " bad_ad=0/1 default: 0 (don't add bad ApplicationData)\n" \
Hanno Becker98aaf252019-05-24 10:07:42 +0100121 " bad_cid=%%d default: 0 (don't corrupt Connection IDs)\n" \
122 " duplicate 1:N packets containing a CID,\n" \
123 " modifying CID in first instance of the packet.\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200124 " protect_hvr=0/1 default: 0 (don't protect HelloVerifyRequest)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000125 " protect_len=%%d default: (don't protect packets of this size)\n" \
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100126 " inject_clihlo=0/1 default: 0 (don't inject fake ClientHello)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200127 "\n" \
128 " seed=%%d default: (use current time)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000129 USAGE_PACK \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200130 "\n"
131
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200132/*
133 * global options
134 */
Hanno Becker01ea7782018-08-17 13:33:41 +0100135
136#define MAX_DELAYED_HS 10
137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138static struct options {
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200139 const char *server_addr; /* address to forward packets to */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200140 const char *server_port; /* port to forward packets to */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200141 const char *listen_addr; /* address for accepting client connections */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200142 const char *listen_port; /* port for accepting client connections */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200143
144 int duplicate; /* duplicate 1 in N packets (none if 0) */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200145 int delay; /* delay 1 packet in N (none if 0) */
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200146 int delay_ccs; /* delay ChangeCipherSpec */
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 char *delay_cli[MAX_DELAYED_HS]; /* handshake types of messages from
Hanno Becker41038102018-08-28 11:15:32 +0100148 * client that should be delayed. */
Hanno Becker01ea7782018-08-17 13:33:41 +0100149 uint8_t delay_cli_cnt; /* Number of entries in delay_cli. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 char *delay_srv[MAX_DELAYED_HS]; /* handshake types of messages from
Hanno Becker41038102018-08-28 11:15:32 +0100151 * server that should be delayed. */
Hanno Becker01ea7782018-08-17 13:33:41 +0100152 uint8_t delay_srv_cnt; /* Number of entries in delay_srv. */
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200153 int drop; /* drop 1 packet in N (none if 0) */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200154 int mtu; /* drop packets larger than this */
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200155 int bad_ad; /* inject corrupted ApplicationData record */
Hanno Becker98aaf252019-05-24 10:07:42 +0100156 unsigned bad_cid; /* inject corrupted CID record */
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200157 int protect_hvr; /* never drop or delay HelloVerifyRequest */
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200158 int protect_len; /* never drop/delay packet of the given size*/
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100159 int inject_clihlo; /* inject fake ClientHello after handshake */
Hanno Becker77abef52017-11-02 10:50:28 +0000160 unsigned pack; /* merge packets into single datagram for
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100161 * at most \c merge milliseconds if > 0 */
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200162 unsigned int seed; /* seed for "random" events */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200163} opt;
164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165static void exit_usage(const char *name, const char *value)
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200166{
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 if (value == NULL) {
168 mbedtls_printf(" unknown option or missing value: %s\n", name);
169 } else {
170 mbedtls_printf(" option %s: illegal value: %s\n", name, value);
171 }
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 mbedtls_printf(USAGE);
174 mbedtls_exit(1);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200175}
176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177static void get_options(int argc, char *argv[])
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200178{
179 int i;
180 char *p, *q;
181
182 opt.server_addr = DFL_SERVER_ADDR;
183 opt.server_port = DFL_SERVER_PORT;
184 opt.listen_addr = DFL_LISTEN_ADDR;
185 opt.listen_port = DFL_LISTEN_PORT;
Hanno Becker211f44c2017-10-31 14:08:10 +0000186 opt.pack = DFL_PACK;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200187 /* Other members default to 0 */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200188
Hanno Becker01ea7782018-08-17 13:33:41 +0100189 opt.delay_cli_cnt = 0;
190 opt.delay_srv_cnt = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 memset(opt.delay_cli, 0, sizeof(opt.delay_cli));
192 memset(opt.delay_srv, 0, sizeof(opt.delay_srv));
Hanno Becker01ea7782018-08-17 13:33:41 +0100193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 for (i = 1; i < argc; i++) {
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200195 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 if ((q = strchr(p, '=')) == NULL) {
197 exit_usage(p, NULL);
198 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200199 *q++ = '\0';
200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 if (strcmp(p, "server_addr") == 0) {
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200202 opt.server_addr = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 } else if (strcmp(p, "server_port") == 0) {
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200204 opt.server_port = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 } else if (strcmp(p, "listen_addr") == 0) {
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200206 opt.listen_addr = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 } else if (strcmp(p, "listen_port") == 0) {
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200208 opt.listen_port = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 } else if (strcmp(p, "duplicate") == 0) {
210 opt.duplicate = atoi(q);
211 if (opt.duplicate < 0 || opt.duplicate > 20) {
212 exit_usage(p, q);
213 }
214 } else if (strcmp(p, "delay") == 0) {
215 opt.delay = atoi(q);
216 if (opt.delay < 0 || opt.delay > 20 || opt.delay == 1) {
217 exit_usage(p, q);
218 }
219 } else if (strcmp(p, "delay_ccs") == 0) {
220 opt.delay_ccs = atoi(q);
221 if (opt.delay_ccs < 0 || opt.delay_ccs > 1) {
222 exit_usage(p, q);
223 }
224 } else if (strcmp(p, "delay_cli") == 0 ||
225 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 if (strcmp(p, "delay_cli") == 0) {
Hanno Becker01ea7782018-08-17 13:33:41 +0100232 delay_cnt = &opt.delay_cli_cnt;
233 delay_list = opt.delay_cli;
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 } else {
Hanno Becker01ea7782018-08-17 13:33:41 +0100235 delay_cnt = &opt.delay_srv_cnt;
236 delay_list = opt.delay_srv;
237 }
238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 if (*delay_cnt == MAX_DELAYED_HS) {
240 mbedtls_printf(" too many uses of %s: only %d allowed\n",
241 p, MAX_DELAYED_HS);
242 exit_usage(p, NULL);
Hanno Becker01ea7782018-08-17 13:33:41 +0100243 }
244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 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 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 memcpy(buf, q, len + 1);
Hanno Becker01ea7782018-08-17 13:33:41 +0100252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 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 }
259 } else if (strcmp(p, "pack") == 0) {
Hanno Becker0cc77742017-10-31 14:10:07 +0000260#if defined(MBEDTLS_TIMING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 opt.pack = (unsigned) atoi(q);
Hanno Becker0cc77742017-10-31 14:10:07 +0000262#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 mbedtls_printf(" option pack only defined if MBEDTLS_TIMING_C is enabled\n");
264 exit(1);
Hanno Becker0cc77742017-10-31 14:10:07 +0000265#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 } 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 }
271 } else if (strcmp(p, "bad_ad") == 0) {
272 opt.bad_ad = atoi(q);
273 if (opt.bad_ad < 0 || opt.bad_ad > 1) {
274 exit_usage(p, q);
275 }
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200276 }
Hanno Becker98aaf252019-05-24 10:07:42 +0100277#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 else if (strcmp(p, "bad_cid") == 0) {
279 opt.bad_cid = (unsigned) atoi(q);
Hanno Becker98aaf252019-05-24 10:07:42 +0100280 }
281#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 else if (strcmp(p, "protect_hvr") == 0) {
283 opt.protect_hvr = atoi(q);
284 if (opt.protect_hvr < 0 || opt.protect_hvr > 1) {
285 exit_usage(p, q);
286 }
287 } else if (strcmp(p, "protect_len") == 0) {
288 opt.protect_len = atoi(q);
289 if (opt.protect_len < 0) {
290 exit_usage(p, q);
291 }
292 } else if (strcmp(p, "inject_clihlo") == 0) {
293 opt.inject_clihlo = atoi(q);
294 if (opt.inject_clihlo < 0 || opt.inject_clihlo > 1) {
295 exit_usage(p, q);
296 }
297 } else if (strcmp(p, "seed") == 0) {
298 opt.seed = atoi(q);
299 if (opt.seed == 0) {
300 exit_usage(p, q);
301 }
302 } else {
303 exit_usage(p, NULL);
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200304 }
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200305 }
306}
307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308static const char *msg_type(unsigned char *msg, size_t len)
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200309{
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 if (len < 1) {
311 return "Invalid";
312 }
313 switch (msg[0]) {
314 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: return "ChangeCipherSpec";
315 case MBEDTLS_SSL_MSG_ALERT: return "Alert";
316 case MBEDTLS_SSL_MSG_APPLICATION_DATA: return "ApplicationData";
317 case MBEDTLS_SSL_MSG_CID: return "CID";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 case MBEDTLS_SSL_MSG_HANDSHAKE: break; /* See below */
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 default: return "Unknown";
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200320 }
321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 if (len < 13 + 12) {
323 return "Invalid handshake";
324 }
Manuel Pégourié-Gonnard8cc7e032014-09-25 12:59:05 +0200325
326 /*
327 * Our handshake message are less than 2^16 bytes long, so they should
328 * have 0 as the first byte of length, frag_offset and frag_length.
329 * Otherwise, assume they are encrypted.
330 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 if (msg[14] || msg[19] || msg[22]) {
332 return "Encrypted handshake";
333 }
Manuel Pégourié-Gonnard8cc7e032014-09-25 12:59:05 +0200334
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 switch (msg[13]) {
336 case MBEDTLS_SSL_HS_HELLO_REQUEST: return "HelloRequest";
337 case MBEDTLS_SSL_HS_CLIENT_HELLO: return "ClientHello";
338 case MBEDTLS_SSL_HS_SERVER_HELLO: return "ServerHello";
339 case MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST: return "HelloVerifyRequest";
340 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET: return "NewSessionTicket";
341 case MBEDTLS_SSL_HS_CERTIFICATE: return "Certificate";
342 case MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE: return "ServerKeyExchange";
343 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST: return "CertificateRequest";
344 case MBEDTLS_SSL_HS_SERVER_HELLO_DONE: return "ServerHelloDone";
345 case MBEDTLS_SSL_HS_CERTIFICATE_VERIFY: return "CertificateVerify";
346 case MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE: return "ClientKeyExchange";
347 case MBEDTLS_SSL_HS_FINISHED: return "Finished";
348 default: return "Unknown handshake";
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200349 }
350}
351
Hanno Becker0cc77742017-10-31 14:10:07 +0000352#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200353/* Return elapsed time in milliseconds since the first call */
Gilles Peskine449bd832023-01-11 14:50:10 +0100354static unsigned elapsed_time(void)
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200355{
Hanno Becker92474da2017-10-31 14:09:30 +0000356 static int initialized = 0;
357 static struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200358
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 if (initialized == 0) {
360 (void) mbedtls_timing_get_timer(&hires, 1);
Hanno Becker92474da2017-10-31 14:09:30 +0000361 initialized = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 return 0;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200363 }
364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 return mbedtls_timing_get_timer(&hires, 0);
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200366}
367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368typedef struct {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100369 mbedtls_net_context *ctx;
370
371 const char *description;
372
Hanno Becker77abef52017-11-02 10:50:28 +0000373 unsigned packet_lifetime;
374 unsigned num_datagrams;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100375
376 unsigned char data[MAX_MSG_SIZE];
Hanno Beckera5e68972017-12-06 08:35:02 +0000377 size_t len;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100378
379} ctx_buffer;
380
381static ctx_buffer outbuf[2];
382
Gilles Peskine449bd832023-01-11 14:50:10 +0100383static int ctx_buffer_flush(ctx_buffer *buf)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100384{
385 int ret;
386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 mbedtls_printf(" %05u flush %s: %u bytes, %u datagrams, last %u ms\n",
388 elapsed_time(), buf->description,
389 (unsigned) buf->len, buf->num_datagrams,
390 elapsed_time() - buf->packet_lifetime);
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 ret = mbedtls_net_send(buf->ctx, buf->data, buf->len);
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100393
394 buf->len = 0;
395 buf->num_datagrams = 0;
396
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 return ret;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100398}
399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400static unsigned ctx_buffer_time_remaining(ctx_buffer *buf)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100401{
Tom Cosgrove1797b052022-12-04 17:19:59 +0000402 unsigned const cur_time = elapsed_time();
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 if (buf->num_datagrams == 0) {
405 return (unsigned) -1;
406 }
Hanno Becker77abef52017-11-02 10:50:28 +0000407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 if (cur_time - buf->packet_lifetime >= opt.pack) {
409 return 0;
410 }
Hanno Becker77abef52017-11-02 10:50:28 +0000411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 return opt.pack - (cur_time - buf->packet_lifetime);
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100413}
414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415static int ctx_buffer_append(ctx_buffer *buf,
416 const unsigned char *data,
417 size_t len)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100418{
419 int ret;
420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 if (len > (size_t) INT_MAX) {
422 return -1;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100423 }
424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if (len > sizeof(buf->data)) {
426 mbedtls_printf(" ! buffer size %u too large (max %u)\n",
427 (unsigned) len, (unsigned) sizeof(buf->data));
428 return -1;
429 }
430
431 if (sizeof(buf->data) - buf->len < len) {
432 if ((ret = ctx_buffer_flush(buf)) <= 0) {
433 mbedtls_printf("ctx_buffer_flush failed with -%#04x", (unsigned int) -ret);
434 return ret;
Hanno Becker31f6e372019-05-08 15:36:31 +0100435 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100436 }
437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 memcpy(buf->data + buf->len, data, len);
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100439
440 buf->len += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 if (++buf->num_datagrams == 1) {
Tom Cosgrove1797b052022-12-04 17:19:59 +0000442 buf->packet_lifetime = elapsed_time();
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 return (int) len;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100446}
Hanno Becker77abef52017-11-02 10:50:28 +0000447#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449static int dispatch_data(mbedtls_net_context *ctx,
450 const unsigned char *data,
451 size_t len)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100452{
Hanno Becker31f6e372019-05-08 15:36:31 +0100453 int ret;
Hanno Becker77abef52017-11-02 10:50:28 +0000454#if defined(MBEDTLS_TIMING_C)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100455 ctx_buffer *buf = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 if (opt.pack > 0) {
457 if (outbuf[0].ctx == ctx) {
Hanno Becker77abef52017-11-02 10:50:28 +0000458 buf = &outbuf[0];
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 } else if (outbuf[1].ctx == ctx) {
Hanno Becker77abef52017-11-02 10:50:28 +0000460 buf = &outbuf[1];
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 }
Hanno Becker0cc77742017-10-31 14:10:07 +0000462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 if (buf == NULL) {
464 return -1;
465 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100466
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 return ctx_buffer_append(buf, data, len);
Hanno Becker77abef52017-11-02 10:50:28 +0000468 }
469#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 ret = mbedtls_net_send(ctx, data, len);
472 if (ret < 0) {
473 mbedtls_printf("net_send returned -%#04x\n", (unsigned int) -ret);
Hanno Becker31f6e372019-05-08 15:36:31 +0100474 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 return ret;
Hanno Becker0cc77742017-10-31 14:10:07 +0000476}
477
Gilles Peskine449bd832023-01-11 14:50:10 +0100478typedef struct {
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200479 mbedtls_net_context *dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200480 const char *way;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200481 const char *type;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200482 unsigned len;
483 unsigned char buf[MAX_MSG_SIZE];
484} packet;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200485
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200486/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100487void print_packet(const packet *p, const char *why)
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200488{
Hanno Becker0cc77742017-10-31 14:10:07 +0000489#if defined(MBEDTLS_TIMING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 if (why == NULL) {
491 mbedtls_printf(" %05u dispatch %s %s (%u bytes)\n",
492 elapsed_time(), p->way, p->type, p->len);
493 } else {
494 mbedtls_printf(" %05u dispatch %s %s (%u bytes): %s\n",
495 elapsed_time(), p->way, p->type, p->len, why);
496 }
Hanno Becker0cc77742017-10-31 14:10:07 +0000497#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 if (why == NULL) {
499 mbedtls_printf(" dispatch %s %s (%u bytes)\n",
500 p->way, p->type, p->len);
501 } else {
502 mbedtls_printf(" dispatch %s %s (%u bytes): %s\n",
503 p->way, p->type, p->len, why);
504 }
Hanno Becker0cc77742017-10-31 14:10:07 +0000505#endif
506
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 fflush(stdout);
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200508}
509
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100510/*
511 * In order to test the server's behaviour when receiving a ClientHello after
512 * the connection is established (this could be a hard reset from the client,
513 * but the server must not drop the existing connection before establishing
514 * client reachability, see RFC 6347 Section 4.2.8), we memorize the first
515 * ClientHello we see (which can't have a cookie), then replay it after the
516 * first ApplicationData record - then we're done.
517 *
518 * This is controlled by the inject_clihlo option.
519 *
520 * We want an explicit state and a place to store the packet.
521 */
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200522typedef enum {
523 ICH_INIT, /* haven't seen the first ClientHello yet */
524 ICH_CACHED, /* cached the initial ClientHello */
525 ICH_INJECTED, /* ClientHello already injected, done */
526} 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100531int 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? */
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 if (opt.inject_clihlo != 0 &&
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200538 inject_clihlo_state == ICH_INIT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 strcmp(p->type, "ClientHello") == 0) {
540 memcpy(&initial_clihlo, p, sizeof(packet));
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200541 inject_clihlo_state = ICH_CACHED;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100542 }
543
Hanno Becker98aaf252019-05-24 10:07:42 +0100544 /* insert corrupted CID record? */
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 if (opt.bad_cid != 0 &&
546 strcmp(p->type, "CID") == 0 &&
547 (rand() % opt.bad_cid) == 0) {
Hanno Becker98aaf252019-05-24 10:07:42 +0100548 unsigned char buf[MAX_MSG_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 memcpy(buf, p->buf, p->len);
Hanno Becker98aaf252019-05-24 10:07:42 +0100550
551 /* The CID resides at offset 11 in the DTLS record header. */
552 buf[11] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 print_packet(p, "modified CID");
Hanno Becker98aaf252019-05-24 10:07:42 +0100554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 if ((ret = dispatch_data(dst, buf, p->len)) <= 0) {
556 mbedtls_printf(" ! dispatch returned %d\n", ret);
557 return ret;
Hanno Becker98aaf252019-05-24 10:07:42 +0100558 }
559 }
560
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200561 /* insert corrupted ApplicationData record? */
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 if (opt.bad_ad &&
563 strcmp(p->type, "ApplicationData") == 0) {
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200564 unsigned char buf[MAX_MSG_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 memcpy(buf, p->buf, p->len);
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 if (p->len <= 13) {
568 mbedtls_printf(" ! can't corrupt empty AD record");
569 } else {
Hanno Beckerfbb0b702017-05-26 16:55:07 +0100570 ++buf[13];
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 print_packet(p, "corrupted");
Hanno Beckerfbb0b702017-05-26 16:55:07 +0100572 }
573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 if ((ret = dispatch_data(dst, buf, p->len)) <= 0) {
575 mbedtls_printf(" ! dispatch returned %d\n", ret);
576 return ret;
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200577 }
578 }
579
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 print_packet(p, why);
581 if ((ret = dispatch_data(dst, p->buf, p->len)) <= 0) {
582 mbedtls_printf(" ! dispatch returned %d\n", ret);
583 return ret;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200584 }
585
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200586 /* Don't duplicate Application Data, only handshake covered */
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 if (opt.duplicate != 0 &&
588 strcmp(p->type, "ApplicationData") != 0 &&
589 rand() % opt.duplicate == 0) {
590 print_packet(p, "duplicated");
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 if ((ret = dispatch_data(dst, p->buf, p->len)) <= 0) {
593 mbedtls_printf(" ! dispatch returned %d\n", ret);
594 return ret;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200595 }
596 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200597
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100598 /* Inject ClientHello after first ApplicationData */
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 if (opt.inject_clihlo != 0 &&
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200600 inject_clihlo_state == ICH_CACHED &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 strcmp(p->type, "ApplicationData") == 0) {
602 print_packet(&initial_clihlo, "injected");
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100603
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 if ((ret = dispatch_data(dst, initial_clihlo.buf,
605 initial_clihlo.len)) <= 0) {
606 mbedtls_printf(" ! dispatch returned %d\n", ret);
607 return ret;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100608 }
609
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200610 inject_clihlo_state = ICH_INJECTED;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100611 }
612
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 return 0;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200614}
615
Hanno Becker101bcba2018-08-21 16:39:51 +0100616#define MAX_DELAYED_MSG 5
617static size_t prev_len;
618static packet prev[MAX_DELAYED_MSG];
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200619
Gilles Peskine449bd832023-01-11 14:50:10 +0100620void clear_pending(void)
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200621{
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 memset(&prev, 0, sizeof(prev));
Hanno Becker101bcba2018-08-21 16:39:51 +0100623 prev_len = 0;
624}
625
Gilles Peskine449bd832023-01-11 14:50:10 +0100626void delay_packet(packet *delay)
Hanno Becker101bcba2018-08-21 16:39:51 +0100627{
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 if (prev_len == MAX_DELAYED_MSG) {
Hanno Becker101bcba2018-08-21 16:39:51 +0100629 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 }
Hanno Becker101bcba2018-08-21 16:39:51 +0100631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 memcpy(&prev[prev_len++], delay, sizeof(packet));
Hanno Becker101bcba2018-08-21 16:39:51 +0100633}
634
Gowtham Suresh Kumar186731b2023-07-26 15:47:45 +0100635int send_delayed(void)
Hanno Becker101bcba2018-08-21 16:39:51 +0100636{
637 uint8_t offset;
638 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 for (offset = 0; offset < prev_len; offset++) {
640 ret = send_packet(&prev[offset], "delayed");
641 if (ret != 0) {
642 return ret;
643 }
Hanno Becker101bcba2018-08-21 16:39:51 +0100644 }
645
646 clear_pending();
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 return 0;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200648}
649
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200650/*
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200651 * Avoid dropping or delaying a packet that was already dropped or delayed
652 * ("held") twice: this only results in uninteresting timeouts. We can't rely
653 * on type to identify packets, since during renegotiation they're all
654 * encrypted. So, rely on size mod 2048 (which is usually just size).
655 *
656 * We only hold packets at the level of entire datagrams, not at the level
Hanno Becker961e6772019-06-04 13:04:28 +0100657 * of records. In particular, if the peer changes the way it packs multiple
658 * records into a single datagram, we don't necessarily count the number of
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200659 * times a record has been held correctly. However, the only known reason
Hanno Becker961e6772019-06-04 13:04:28 +0100660 * why a peer would change datagram packing is disabling the latter on
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200661 * retransmission, in which case we'd hold involved records at most
662 * HOLD_MAX + 1 times.
663 */
664static unsigned char held[2048] = { 0 };
665#define HOLD_MAX 2
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200666
Gilles Peskine449bd832023-01-11 14:50:10 +0100667int handle_message(const char *way,
668 mbedtls_net_context *dst,
669 mbedtls_net_context *src)
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200670{
671 int ret;
672 packet cur;
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200673 size_t id;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200674
Hanno Becker01ea7782018-08-17 13:33:41 +0100675 uint8_t delay_idx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 char **delay_list;
Hanno Becker01ea7782018-08-17 13:33:41 +0100677 uint8_t delay_list_len;
678
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +0200679 /* receive packet */
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 if ((ret = mbedtls_net_recv(src, cur.buf, sizeof(cur.buf))) <= 0) {
681 mbedtls_printf(" ! mbedtls_net_recv returned %d\n", ret);
682 return ret;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200683 }
684
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200685 cur.len = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 cur.type = msg_type(cur.buf, cur.len);
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200687 cur.way = way;
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200688 cur.dst = dst;
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 print_packet(&cur, NULL);
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200690
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 id = cur.len % sizeof(held);
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200692
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 if (strcmp(way, "S <- C") == 0) {
Hanno Becker01ea7782018-08-17 13:33:41 +0100694 delay_list = opt.delay_cli;
695 delay_list_len = opt.delay_cli_cnt;
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 } else {
Hanno Becker01ea7782018-08-17 13:33:41 +0100697 delay_list = opt.delay_srv;
698 delay_list_len = opt.delay_srv_cnt;
699 }
Hanno Beckercf469452018-08-28 10:09:47 +0100700
Hanno Becker01ea7782018-08-17 13:33:41 +0100701 /* Check if message type is in the list of messages
702 * that should be delayed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 for (delay_idx = 0; delay_idx < delay_list_len; delay_idx++) {
704 if (delay_list[delay_idx] == NULL) {
Hanno Becker01ea7782018-08-17 13:33:41 +0100705 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 }
Hanno Becker01ea7782018-08-17 13:33:41 +0100707
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if (strcmp(delay_list[delay_idx], cur.type) == 0) {
Hanno Becker01ea7782018-08-17 13:33:41 +0100709 /* Delay message */
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 delay_packet(&cur);
Hanno Becker01ea7782018-08-17 13:33:41 +0100711
712 /* Remove entry from list */
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 mbedtls_free(delay_list[delay_idx]);
Hanno Becker01ea7782018-08-17 13:33:41 +0100714 delay_list[delay_idx] = NULL;
715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 return 0;
Hanno Becker01ea7782018-08-17 13:33:41 +0100717 }
718 }
719
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200720 /* do we want to drop, delay, or forward it? */
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 if ((opt.mtu != 0 &&
722 cur.len > (unsigned) opt.mtu) ||
723 (opt.drop != 0 &&
724 strcmp(cur.type, "CID") != 0 &&
725 strcmp(cur.type, "ApplicationData") != 0 &&
726 !(opt.protect_hvr &&
727 strcmp(cur.type, "HelloVerifyRequest") == 0) &&
728 cur.len != (size_t) opt.protect_len &&
729 held[id] < HOLD_MAX &&
730 rand() % opt.drop == 0)) {
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200731 ++held[id];
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 } else if ((opt.delay_ccs == 1 &&
733 strcmp(cur.type, "ChangeCipherSpec") == 0) ||
734 (opt.delay != 0 &&
735 strcmp(cur.type, "CID") != 0 &&
736 strcmp(cur.type, "ApplicationData") != 0 &&
737 !(opt.protect_hvr &&
738 strcmp(cur.type, "HelloVerifyRequest") == 0) &&
739 cur.len != (size_t) opt.protect_len &&
740 held[id] < HOLD_MAX &&
741 rand() % opt.delay == 0)) {
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200742 ++held[id];
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 delay_packet(&cur);
744 } else {
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200745 /* forward and possibly duplicate */
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 if ((ret = send_packet(&cur, "forwarded")) != 0) {
747 return ret;
748 }
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200749
Hanno Becker101bcba2018-08-21 16:39:51 +0100750 /* send previously delayed messages if any */
751 ret = send_delayed();
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 if (ret != 0) {
753 return ret;
754 }
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200755 }
756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 return 0;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200758}
759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760int main(int argc, char *argv[])
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200761{
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100762 int ret = 1;
763 int exit_code = MBEDTLS_EXIT_FAILURE;
Hanno Becker01ea7782018-08-17 13:33:41 +0100764 uint8_t delay_idx;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200765
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200766 mbedtls_net_context listen_fd, client_fd, server_fd;
Hanno Becker77abef52017-11-02 10:50:28 +0000767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768#if defined(MBEDTLS_TIMING_C)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100769 struct timeval tm;
Hanno Becker77abef52017-11-02 10:50:28 +0000770#endif
771
772 struct timeval *tm_ptr = NULL;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200773
774 int nb_fds;
775 fd_set read_fds;
776
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 mbedtls_net_init(&listen_fd);
778 mbedtls_net_init(&client_fd);
779 mbedtls_net_init(&server_fd);
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 get_options(argc, argv);
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200782
783 /*
784 * Decisions to drop/delay/duplicate packets are pseudo-random: dropping
785 * exactly 1 in N packets would lead to problems when a flight has exactly
786 * N packets: the same packet would be dropped on every resend.
787 *
788 * In order to be able to reproduce problems reliably, the seed may be
789 * specified explicitly.
790 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 if (opt.seed == 0) {
Gilles Peskinee756f642022-04-05 21:39:43 +0200792#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 opt.seed = (unsigned int) mbedtls_time(NULL);
Gilles Peskinee756f642022-04-05 21:39:43 +0200794#else
795 opt.seed = 1;
796#endif /* MBEDTLS_HAVE_TIME */
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 mbedtls_printf(" . Pseudo-random seed: %u\n", opt.seed);
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200798 }
799
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 srand(opt.seed);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200801
802 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200803 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200804 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 mbedtls_printf(" . Connect to server on UDP/%s/%s ...",
806 opt.server_addr, opt.server_port);
807 fflush(stdout);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200808
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 if ((ret = mbedtls_net_connect(&server_fd, opt.server_addr, opt.server_port,
810 MBEDTLS_NET_PROTO_UDP)) != 0) {
811 mbedtls_printf(" failed\n ! mbedtls_net_connect returned %d\n\n", ret);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200812 goto exit;
813 }
814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200816
817 /*
818 * 1. Setup the "listening" UDP socket
819 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 mbedtls_printf(" . Bind on UDP/%s/%s ...",
821 opt.listen_addr, opt.listen_port);
822 fflush(stdout);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200823
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 if ((ret = mbedtls_net_bind(&listen_fd, opt.listen_addr, opt.listen_port,
825 MBEDTLS_NET_PROTO_UDP)) != 0) {
826 mbedtls_printf(" failed\n ! mbedtls_net_bind returned %d\n\n", ret);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200827 goto exit;
828 }
829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200831
832 /*
833 * 2. Wait until a client connects
834 */
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200835accept:
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 mbedtls_net_free(&client_fd);
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200837
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 mbedtls_printf(" . Waiting for a remote connection ...");
839 fflush(stdout);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200840
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 if ((ret = mbedtls_net_accept(&listen_fd, &client_fd,
842 NULL, 0, NULL)) != 0) {
843 mbedtls_printf(" failed\n ! mbedtls_net_accept returned %d\n\n", ret);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200844 goto exit;
845 }
846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200848
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200849 /*
850 * 3. Forward packets forever (kill the process to terminate it)
851 */
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200852 clear_pending();
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 memset(held, 0, sizeof(held));
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200854
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200855 nb_fds = client_fd.fd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 if (nb_fds < server_fd.fd) {
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200857 nb_fds = server_fd.fd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 }
859 if (nb_fds < listen_fd.fd) {
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200860 nb_fds = listen_fd.fd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 }
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200862 ++nb_fds;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200863
Hanno Becker0cc77742017-10-31 14:10:07 +0000864#if defined(MBEDTLS_TIMING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 if (opt.pack > 0) {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100866 outbuf[0].ctx = &server_fd;
867 outbuf[0].description = "S <- C";
868 outbuf[0].num_datagrams = 0;
869 outbuf[0].len = 0;
870
871 outbuf[1].ctx = &client_fd;
872 outbuf[1].description = "S -> C";
873 outbuf[1].num_datagrams = 0;
874 outbuf[1].len = 0;
875 }
Hanno Becker0cc77742017-10-31 14:10:07 +0000876#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100877
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 while (1) {
Hanno Becker77abef52017-11-02 10:50:28 +0000879#if defined(MBEDTLS_TIMING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 if (opt.pack > 0) {
Hanno Becker77abef52017-11-02 10:50:28 +0000881 unsigned max_wait_server, max_wait_client, max_wait;
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 max_wait_server = ctx_buffer_time_remaining(&outbuf[0]);
883 max_wait_client = ctx_buffer_time_remaining(&outbuf[1]);
Hanno Becker77abef52017-11-02 10:50:28 +0000884
885 max_wait = (unsigned) -1;
886
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 if (max_wait_server == 0) {
888 ctx_buffer_flush(&outbuf[0]);
889 } else {
Hanno Becker77abef52017-11-02 10:50:28 +0000890 max_wait = max_wait_server;
Hanno Becker77abef52017-11-02 10:50:28 +0000891 }
892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if (max_wait_client == 0) {
894 ctx_buffer_flush(&outbuf[1]);
895 } else {
896 if (max_wait_client < max_wait) {
897 max_wait = max_wait_client;
898 }
899 }
900
901 if (max_wait != (unsigned) -1) {
Hanno Becker77abef52017-11-02 10:50:28 +0000902 tm.tv_sec = max_wait / 1000;
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 tm.tv_usec = (max_wait % 1000) * 1000;
Hanno Becker77abef52017-11-02 10:50:28 +0000904
905 tm_ptr = &tm;
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 } else {
Hanno Becker77abef52017-11-02 10:50:28 +0000907 tm_ptr = NULL;
908 }
909 }
910#endif /* MBEDTLS_TIMING_C */
911
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 FD_ZERO(&read_fds);
913 FD_SET(server_fd.fd, &read_fds);
914 FD_SET(client_fd.fd, &read_fds);
915 FD_SET(listen_fd.fd, &read_fds);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200916
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 if ((ret = select(nb_fds, &read_fds, NULL, NULL, tm_ptr)) < 0) {
918 perror("select");
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200919 goto exit;
920 }
921
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 if (FD_ISSET(listen_fd.fd, &read_fds)) {
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200923 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200924 }
925
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 if (FD_ISSET(client_fd.fd, &read_fds)) {
927 if ((ret = handle_message("S <- C",
928 &server_fd, &client_fd)) != 0) {
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200929 goto accept;
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 }
931 }
932
933 if (FD_ISSET(server_fd.fd, &read_fds)) {
934 if ((ret = handle_message("S -> C",
935 &client_fd, &server_fd)) != 0) {
936 goto accept;
937 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200938 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100939
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200940 }
941
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100942 exit_code = MBEDTLS_EXIT_SUCCESS;
943
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200944exit:
945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946#ifdef MBEDTLS_ERROR_C
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200948 char error_buf[100];
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 mbedtls_strerror(ret, error_buf, 100);
950 mbedtls_printf("Last error was: -0x%04X - %s\n\n", (unsigned int) -ret, error_buf);
951 fflush(stdout);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200952 }
953#endif
954
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 for (delay_idx = 0; delay_idx < MAX_DELAYED_HS; delay_idx++) {
956 mbedtls_free(opt.delay_cli[delay_idx]);
957 mbedtls_free(opt.delay_srv[delay_idx]);
Hanno Becker01ea7782018-08-17 13:33:41 +0100958 }
959
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 mbedtls_net_free(&client_fd);
961 mbedtls_net_free(&server_fd);
962 mbedtls_net_free(&listen_fd);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200963
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 mbedtls_exit(exit_code);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200965}
966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967#endif /* MBEDTLS_NET_C */