blob: b580b06dfa8b9471e22e2a4a5f2fd5424a04ce47 [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
Bence Szépkútic662b362021-05-27 11:25:03 +020015#include "mbedtls/build_info.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020017#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000018#include "mbedtls/platform.h"
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000019#else
Simon Butcherd3138c32016-04-27 01:26:50 +010020#include <stdio.h>
21#include <stdlib.h>
David Horstmann5b9cb9e2021-11-29 17:28:13 +000022#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherd3138c32016-04-27 01:26:50 +010023#include <time.h>
Andres Amaya Garcia80081a62018-04-29 21:58:53 +010024#define mbedtls_time time
25#define mbedtls_time_t time_t
David Horstmann5b9cb9e2021-11-29 17:28:13 +000026#endif
Andres Amaya Garcia80081a62018-04-29 21:58:53 +010027#define mbedtls_printf printf
Hanno Becker01ea7782018-08-17 13:33:41 +010028#define mbedtls_calloc calloc
29#define mbedtls_free free
k-stachowiak776521a2019-05-23 09:46:47 +020030#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010031#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia80081a62018-04-29 21:58:53 +010032#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
33#endif /* MBEDTLS_PLATFORM_C */
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if !defined(MBEDTLS_NET_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010036int main(void)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020037{
Gilles Peskine449bd832023-01-11 14:50:10 +010038 mbedtls_printf("MBEDTLS_NET_C not defined.\n");
39 mbedtls_exit(0);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020040}
41#else
42
Andres AG788aa4a2016-09-14 14:32:09 +010043#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/error.h"
45#include "mbedtls/ssl.h"
Hanno Becker0cc77742017-10-31 14:10:07 +000046#include "mbedtls/timing.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020047
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +000048#include <string.h>
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020049
50/* For select() */
51#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
52 !defined(EFI32)
Alvaro Segura673e1eb2025-06-04 23:31:35 +020053
54#if defined(_MSC_VER)
55#pragma warning(disable : 5105) // warning inside winbase.h in C11 mode
56#endif
57
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020058#include <winsock2.h>
59#include <windows.h>
60#if defined(_MSC_VER)
61#if defined(_WIN32_WCE)
62#pragma comment( lib, "ws2.lib" )
63#else
64#pragma comment( lib, "ws2_32.lib" )
65#endif
66#endif /* _MSC_VER */
67#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Dave Rodgmanfaef6492022-05-11 16:05:16 +000068#if defined(MBEDTLS_HAVE_TIME) || (defined(MBEDTLS_TIMING_C) && !defined(MBEDTLS_TIMING_ALT))
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020069#include <sys/time.h>
Andrzej Kurek6056e7a2022-03-02 12:01:10 -050070#endif
Dave Rodgmanfaef6492022-05-11 16:05:16 +000071#include <sys/select.h>
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020072#include <sys/types.h>
73#include <unistd.h>
74#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
75
Manuel Pégourié-Gonnardb46780e2014-09-23 12:17:30 +020076#define MAX_MSG_SIZE 16384 + 2048 /* max record/datagram size */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020077
78#define DFL_SERVER_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020079#define DFL_SERVER_PORT "4433"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020080#define DFL_LISTEN_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020081#define DFL_LISTEN_PORT "5556"
Hanno Becker1dd62ea2017-05-22 14:30:59 +010082#define DFL_PACK 0
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020083
Hanno Becker0cc77742017-10-31 14:10:07 +000084#if defined(MBEDTLS_TIMING_C)
85#define USAGE_PACK \
86 " pack=%%d default: 0 (don't pack)\n" \
87 " options: t > 0 (pack for t milliseconds)\n"
88#else
89#define USAGE_PACK
90#endif
91
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020092#define USAGE \
93 "\n usage: udp_proxy param=<>...\n" \
94 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +020095 " server_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020096 " server_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +020097 " listen_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020098 " listen_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +020099 "\n" \
100 " duplicate=%%d default: 0 (no duplication)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200101 " duplicate about 1:N packets randomly\n" \
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200102 " delay=%%d default: 0 (no delayed packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200103 " delay about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200104 " delay_ccs=0/1 default: 0 (don't delay ChangeCipherSpec)\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 " delay_cli=%%s Handshake message from client that should be\n" \
Hanno Becker01ea7782018-08-17 13:33:41 +0100106 " delayed. Possible values are 'ClientHello',\n" \
107 " 'Certificate', 'CertificateVerify', and\n" \
108 " 'ClientKeyExchange'.\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 " May be used multiple times, even for the same\n" \
110 " message, in which case the respective message\n" \
Hanno Becker01ea7782018-08-17 13:33:41 +0100111 " gets delayed multiple times.\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 " delay_srv=%%s Handshake message from server that should be\n" \
113 " delayed. Possible values are 'HelloRequest',\n" \
114 " 'ServerHello', 'ServerHelloDone', 'Certificate'\n" \
115 " 'ServerKeyExchange', 'NewSessionTicket',\n" \
116 " 'HelloVerifyRequest' and ''CertificateRequest'.\n" \
117 " May be used multiple times, even for the same\n" \
118 " message, in which case the respective message\n" \
Hanno Becker01ea7782018-08-17 13:33:41 +0100119 " gets delayed multiple times.\n" \
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200120 " drop=%%d default: 0 (no dropped packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200121 " drop about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200122 " mtu=%%d default: 0 (unlimited)\n" \
123 " drop packets larger than N bytes\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200124 " bad_ad=0/1 default: 0 (don't add bad ApplicationData)\n" \
Hanno Becker98aaf252019-05-24 10:07:42 +0100125 " bad_cid=%%d default: 0 (don't corrupt Connection IDs)\n" \
126 " duplicate 1:N packets containing a CID,\n" \
127 " modifying CID in first instance of the packet.\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200128 " protect_hvr=0/1 default: 0 (don't protect HelloVerifyRequest)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000129 " protect_len=%%d default: (don't protect packets of this size)\n" \
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100130 " inject_clihlo=0/1 default: 0 (don't inject fake ClientHello)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200131 "\n" \
132 " seed=%%d default: (use current time)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000133 USAGE_PACK \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200134 "\n"
135
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200136/*
137 * global options
138 */
Hanno Becker01ea7782018-08-17 13:33:41 +0100139
140#define MAX_DELAYED_HS 10
141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142static struct options {
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200143 const char *server_addr; /* address to forward packets to */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200144 const char *server_port; /* port to forward packets to */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200145 const char *listen_addr; /* address for accepting client connections */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200146 const char *listen_port; /* port for accepting client connections */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200147
148 int duplicate; /* duplicate 1 in N packets (none if 0) */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200149 int delay; /* delay 1 packet in N (none if 0) */
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200150 int delay_ccs; /* delay ChangeCipherSpec */
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 char *delay_cli[MAX_DELAYED_HS]; /* handshake types of messages from
Hanno Becker41038102018-08-28 11:15:32 +0100152 * client that should be delayed. */
Hanno Becker01ea7782018-08-17 13:33:41 +0100153 uint8_t delay_cli_cnt; /* Number of entries in delay_cli. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 char *delay_srv[MAX_DELAYED_HS]; /* handshake types of messages from
Hanno Becker41038102018-08-28 11:15:32 +0100155 * server that should be delayed. */
Hanno Becker01ea7782018-08-17 13:33:41 +0100156 uint8_t delay_srv_cnt; /* Number of entries in delay_srv. */
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200157 int drop; /* drop 1 packet in N (none if 0) */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200158 int mtu; /* drop packets larger than this */
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200159 int bad_ad; /* inject corrupted ApplicationData record */
Hanno Becker98aaf252019-05-24 10:07:42 +0100160 unsigned bad_cid; /* inject corrupted CID record */
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200161 int protect_hvr; /* never drop or delay HelloVerifyRequest */
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200162 int protect_len; /* never drop/delay packet of the given size*/
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100163 int inject_clihlo; /* inject fake ClientHello after handshake */
Hanno Becker77abef52017-11-02 10:50:28 +0000164 unsigned pack; /* merge packets into single datagram for
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100165 * at most \c merge milliseconds if > 0 */
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200166 unsigned int seed; /* seed for "random" events */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200167} opt;
168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169static void exit_usage(const char *name, const char *value)
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200170{
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 if (value == NULL) {
172 mbedtls_printf(" unknown option or missing value: %s\n", name);
173 } else {
174 mbedtls_printf(" option %s: illegal value: %s\n", name, value);
175 }
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 mbedtls_printf(USAGE);
178 mbedtls_exit(1);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200179}
180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181static void get_options(int argc, char *argv[])
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200182{
183 int i;
184 char *p, *q;
185
186 opt.server_addr = DFL_SERVER_ADDR;
187 opt.server_port = DFL_SERVER_PORT;
188 opt.listen_addr = DFL_LISTEN_ADDR;
189 opt.listen_port = DFL_LISTEN_PORT;
Hanno Becker211f44c2017-10-31 14:08:10 +0000190 opt.pack = DFL_PACK;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200191 /* Other members default to 0 */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200192
Hanno Becker01ea7782018-08-17 13:33:41 +0100193 opt.delay_cli_cnt = 0;
194 opt.delay_srv_cnt = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 memset(opt.delay_cli, 0, sizeof(opt.delay_cli));
196 memset(opt.delay_srv, 0, sizeof(opt.delay_srv));
Hanno Becker01ea7782018-08-17 13:33:41 +0100197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 for (i = 1; i < argc; i++) {
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200199 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 if ((q = strchr(p, '=')) == NULL) {
201 exit_usage(p, NULL);
202 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200203 *q++ = '\0';
204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 if (strcmp(p, "server_addr") == 0) {
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200206 opt.server_addr = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 } else if (strcmp(p, "server_port") == 0) {
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200208 opt.server_port = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 } else if (strcmp(p, "listen_addr") == 0) {
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200210 opt.listen_addr = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 } else if (strcmp(p, "listen_port") == 0) {
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200212 opt.listen_port = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 } 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 }
218 } else if (strcmp(p, "delay") == 0) {
219 opt.delay = atoi(q);
220 if (opt.delay < 0 || opt.delay > 20 || opt.delay == 1) {
221 exit_usage(p, q);
222 }
223 } else if (strcmp(p, "delay_ccs") == 0) {
224 opt.delay_ccs = atoi(q);
225 if (opt.delay_ccs < 0 || opt.delay_ccs > 1) {
226 exit_usage(p, q);
227 }
228 } else if (strcmp(p, "delay_cli") == 0 ||
229 strcmp(p, "delay_srv") == 0) {
Hanno Becker01ea7782018-08-17 13:33:41 +0100230 uint8_t *delay_cnt;
231 char **delay_list;
232 size_t len;
233 char *buf;
234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 if (strcmp(p, "delay_cli") == 0) {
Hanno Becker01ea7782018-08-17 13:33:41 +0100236 delay_cnt = &opt.delay_cli_cnt;
237 delay_list = opt.delay_cli;
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 } else {
Hanno Becker01ea7782018-08-17 13:33:41 +0100239 delay_cnt = &opt.delay_srv_cnt;
240 delay_list = opt.delay_srv;
241 }
242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 if (*delay_cnt == MAX_DELAYED_HS) {
244 mbedtls_printf(" too many uses of %s: only %d allowed\n",
245 p, MAX_DELAYED_HS);
246 exit_usage(p, NULL);
Hanno Becker01ea7782018-08-17 13:33:41 +0100247 }
248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 len = strlen(q);
250 buf = mbedtls_calloc(1, len + 1);
251 if (buf == NULL) {
252 mbedtls_printf(" Allocation failure\n");
253 exit(1);
Hanno Becker01ea7782018-08-17 13:33:41 +0100254 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 memcpy(buf, q, len + 1);
Hanno Becker01ea7782018-08-17 13:33:41 +0100256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 delay_list[(*delay_cnt)++] = buf;
258 } else if (strcmp(p, "drop") == 0) {
259 opt.drop = atoi(q);
260 if (opt.drop < 0 || opt.drop > 20 || opt.drop == 1) {
261 exit_usage(p, q);
262 }
263 } else if (strcmp(p, "pack") == 0) {
Hanno Becker0cc77742017-10-31 14:10:07 +0000264#if defined(MBEDTLS_TIMING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 opt.pack = (unsigned) atoi(q);
Hanno Becker0cc77742017-10-31 14:10:07 +0000266#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 mbedtls_printf(" option pack only defined if MBEDTLS_TIMING_C is enabled\n");
268 exit(1);
Hanno Becker0cc77742017-10-31 14:10:07 +0000269#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 } else if (strcmp(p, "mtu") == 0) {
271 opt.mtu = atoi(q);
272 if (opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE) {
273 exit_usage(p, q);
274 }
275 } else if (strcmp(p, "bad_ad") == 0) {
276 opt.bad_ad = atoi(q);
277 if (opt.bad_ad < 0 || opt.bad_ad > 1) {
278 exit_usage(p, q);
279 }
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200280 }
Hanno Becker98aaf252019-05-24 10:07:42 +0100281#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 else if (strcmp(p, "bad_cid") == 0) {
283 opt.bad_cid = (unsigned) atoi(q);
Hanno Becker98aaf252019-05-24 10:07:42 +0100284 }
285#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 else if (strcmp(p, "protect_hvr") == 0) {
287 opt.protect_hvr = atoi(q);
288 if (opt.protect_hvr < 0 || opt.protect_hvr > 1) {
289 exit_usage(p, q);
290 }
291 } else if (strcmp(p, "protect_len") == 0) {
292 opt.protect_len = atoi(q);
293 if (opt.protect_len < 0) {
294 exit_usage(p, q);
295 }
296 } else if (strcmp(p, "inject_clihlo") == 0) {
297 opt.inject_clihlo = atoi(q);
298 if (opt.inject_clihlo < 0 || opt.inject_clihlo > 1) {
299 exit_usage(p, q);
300 }
301 } else if (strcmp(p, "seed") == 0) {
302 opt.seed = atoi(q);
303 if (opt.seed == 0) {
304 exit_usage(p, q);
305 }
306 } else {
307 exit_usage(p, NULL);
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200308 }
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200309 }
310}
311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312static const char *msg_type(unsigned char *msg, size_t len)
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200313{
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 if (len < 1) {
315 return "Invalid";
316 }
317 switch (msg[0]) {
318 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: return "ChangeCipherSpec";
319 case MBEDTLS_SSL_MSG_ALERT: return "Alert";
320 case MBEDTLS_SSL_MSG_APPLICATION_DATA: return "ApplicationData";
321 case MBEDTLS_SSL_MSG_CID: return "CID";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 case MBEDTLS_SSL_MSG_HANDSHAKE: break; /* See below */
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 default: return "Unknown";
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200324 }
325
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 if (len < 13 + 12) {
327 return "Invalid handshake";
328 }
Manuel Pégourié-Gonnard8cc7e032014-09-25 12:59:05 +0200329
330 /*
331 * Our handshake message are less than 2^16 bytes long, so they should
332 * have 0 as the first byte of length, frag_offset and frag_length.
333 * Otherwise, assume they are encrypted.
334 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 if (msg[14] || msg[19] || msg[22]) {
336 return "Encrypted handshake";
337 }
Manuel Pégourié-Gonnard8cc7e032014-09-25 12:59:05 +0200338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 switch (msg[13]) {
340 case MBEDTLS_SSL_HS_HELLO_REQUEST: return "HelloRequest";
341 case MBEDTLS_SSL_HS_CLIENT_HELLO: return "ClientHello";
342 case MBEDTLS_SSL_HS_SERVER_HELLO: return "ServerHello";
343 case MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST: return "HelloVerifyRequest";
344 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET: return "NewSessionTicket";
345 case MBEDTLS_SSL_HS_CERTIFICATE: return "Certificate";
346 case MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE: return "ServerKeyExchange";
347 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST: return "CertificateRequest";
348 case MBEDTLS_SSL_HS_SERVER_HELLO_DONE: return "ServerHelloDone";
349 case MBEDTLS_SSL_HS_CERTIFICATE_VERIFY: return "CertificateVerify";
350 case MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE: return "ClientKeyExchange";
351 case MBEDTLS_SSL_HS_FINISHED: return "Finished";
352 default: return "Unknown handshake";
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200353 }
354}
355
Hanno Becker0cc77742017-10-31 14:10:07 +0000356#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200357/* Return elapsed time in milliseconds since the first call */
Gilles Peskine449bd832023-01-11 14:50:10 +0100358static unsigned elapsed_time(void)
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200359{
Hanno Becker92474da2017-10-31 14:09:30 +0000360 static int initialized = 0;
361 static struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200362
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 if (initialized == 0) {
364 (void) mbedtls_timing_get_timer(&hires, 1);
Hanno Becker92474da2017-10-31 14:09:30 +0000365 initialized = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 return 0;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200367 }
368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 return mbedtls_timing_get_timer(&hires, 0);
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200370}
371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372typedef struct {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100373 mbedtls_net_context *ctx;
374
375 const char *description;
376
Hanno Becker77abef52017-11-02 10:50:28 +0000377 unsigned packet_lifetime;
378 unsigned num_datagrams;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100379
380 unsigned char data[MAX_MSG_SIZE];
Hanno Beckera5e68972017-12-06 08:35:02 +0000381 size_t len;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100382
383} ctx_buffer;
384
385static ctx_buffer outbuf[2];
386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387static int ctx_buffer_flush(ctx_buffer *buf)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100388{
389 int ret;
390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 mbedtls_printf(" %05u flush %s: %u bytes, %u datagrams, last %u ms\n",
392 elapsed_time(), buf->description,
393 (unsigned) buf->len, buf->num_datagrams,
394 elapsed_time() - buf->packet_lifetime);
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 ret = mbedtls_net_send(buf->ctx, buf->data, buf->len);
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100397
398 buf->len = 0;
399 buf->num_datagrams = 0;
400
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 return ret;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100402}
403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404static unsigned ctx_buffer_time_remaining(ctx_buffer *buf)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100405{
Tom Cosgrove1797b052022-12-04 17:19:59 +0000406 unsigned const cur_time = elapsed_time();
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 if (buf->num_datagrams == 0) {
409 return (unsigned) -1;
410 }
Hanno Becker77abef52017-11-02 10:50:28 +0000411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 if (cur_time - buf->packet_lifetime >= opt.pack) {
413 return 0;
414 }
Hanno Becker77abef52017-11-02 10:50:28 +0000415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 return opt.pack - (cur_time - buf->packet_lifetime);
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100417}
418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419static int ctx_buffer_append(ctx_buffer *buf,
420 const unsigned char *data,
421 size_t len)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100422{
423 int ret;
424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if (len > (size_t) INT_MAX) {
426 return -1;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100427 }
428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 if (len > sizeof(buf->data)) {
430 mbedtls_printf(" ! buffer size %u too large (max %u)\n",
431 (unsigned) len, (unsigned) sizeof(buf->data));
432 return -1;
433 }
434
435 if (sizeof(buf->data) - buf->len < len) {
436 if ((ret = ctx_buffer_flush(buf)) <= 0) {
437 mbedtls_printf("ctx_buffer_flush failed with -%#04x", (unsigned int) -ret);
438 return ret;
Hanno Becker31f6e372019-05-08 15:36:31 +0100439 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100440 }
441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 memcpy(buf->data + buf->len, data, len);
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100443
444 buf->len += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 if (++buf->num_datagrams == 1) {
Tom Cosgrove1797b052022-12-04 17:19:59 +0000446 buf->packet_lifetime = elapsed_time();
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 return (int) len;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100450}
Hanno Becker77abef52017-11-02 10:50:28 +0000451#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453static int dispatch_data(mbedtls_net_context *ctx,
454 const unsigned char *data,
455 size_t len)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100456{
Hanno Becker31f6e372019-05-08 15:36:31 +0100457 int ret;
Hanno Becker77abef52017-11-02 10:50:28 +0000458#if defined(MBEDTLS_TIMING_C)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100459 ctx_buffer *buf = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 if (opt.pack > 0) {
461 if (outbuf[0].ctx == ctx) {
Hanno Becker77abef52017-11-02 10:50:28 +0000462 buf = &outbuf[0];
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 } else if (outbuf[1].ctx == ctx) {
Hanno Becker77abef52017-11-02 10:50:28 +0000464 buf = &outbuf[1];
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 }
Hanno Becker0cc77742017-10-31 14:10:07 +0000466
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 if (buf == NULL) {
468 return -1;
469 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 return ctx_buffer_append(buf, data, len);
Hanno Becker77abef52017-11-02 10:50:28 +0000472 }
473#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 ret = mbedtls_net_send(ctx, data, len);
476 if (ret < 0) {
477 mbedtls_printf("net_send returned -%#04x\n", (unsigned int) -ret);
Hanno Becker31f6e372019-05-08 15:36:31 +0100478 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 return ret;
Hanno Becker0cc77742017-10-31 14:10:07 +0000480}
481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482typedef struct {
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200483 mbedtls_net_context *dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200484 const char *way;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200485 const char *type;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200486 unsigned len;
487 unsigned char buf[MAX_MSG_SIZE];
488} packet;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200489
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200490/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */
Michael Schuster6fa32fd2024-06-01 21:15:02 +0200491static void print_packet(const packet *p, const char *why)
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200492{
Hanno Becker0cc77742017-10-31 14:10:07 +0000493#if defined(MBEDTLS_TIMING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 if (why == NULL) {
495 mbedtls_printf(" %05u dispatch %s %s (%u bytes)\n",
496 elapsed_time(), p->way, p->type, p->len);
497 } else {
498 mbedtls_printf(" %05u dispatch %s %s (%u bytes): %s\n",
499 elapsed_time(), p->way, p->type, p->len, why);
500 }
Hanno Becker0cc77742017-10-31 14:10:07 +0000501#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 if (why == NULL) {
503 mbedtls_printf(" dispatch %s %s (%u bytes)\n",
504 p->way, p->type, p->len);
505 } else {
506 mbedtls_printf(" dispatch %s %s (%u bytes): %s\n",
507 p->way, p->type, p->len, why);
508 }
Hanno Becker0cc77742017-10-31 14:10:07 +0000509#endif
510
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 fflush(stdout);
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200512}
513
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100514/*
515 * In order to test the server's behaviour when receiving a ClientHello after
516 * the connection is established (this could be a hard reset from the client,
517 * but the server must not drop the existing connection before establishing
518 * client reachability, see RFC 6347 Section 4.2.8), we memorize the first
519 * ClientHello we see (which can't have a cookie), then replay it after the
520 * first ApplicationData record - then we're done.
521 *
522 * This is controlled by the inject_clihlo option.
523 *
524 * We want an explicit state and a place to store the packet.
525 */
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200526typedef enum {
527 ICH_INIT, /* haven't seen the first ClientHello yet */
528 ICH_CACHED, /* cached the initial ClientHello */
529 ICH_INJECTED, /* ClientHello already injected, done */
530} inject_clihlo_state_t;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100531
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200532static inject_clihlo_state_t inject_clihlo_state;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100533static packet initial_clihlo;
534
Michael Schuster6fa32fd2024-06-01 21:15:02 +0200535static int send_packet(const packet *p, const char *why)
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200536{
537 int ret;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200538 mbedtls_net_context *dst = p->dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200539
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100540 /* save initial ClientHello? */
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 if (opt.inject_clihlo != 0 &&
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200542 inject_clihlo_state == ICH_INIT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 strcmp(p->type, "ClientHello") == 0) {
544 memcpy(&initial_clihlo, p, sizeof(packet));
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200545 inject_clihlo_state = ICH_CACHED;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100546 }
547
Hanno Becker98aaf252019-05-24 10:07:42 +0100548 /* insert corrupted CID record? */
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 if (opt.bad_cid != 0 &&
550 strcmp(p->type, "CID") == 0 &&
551 (rand() % opt.bad_cid) == 0) {
Hanno Becker98aaf252019-05-24 10:07:42 +0100552 unsigned char buf[MAX_MSG_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 memcpy(buf, p->buf, p->len);
Hanno Becker98aaf252019-05-24 10:07:42 +0100554
555 /* The CID resides at offset 11 in the DTLS record header. */
556 buf[11] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 print_packet(p, "modified CID");
Hanno Becker98aaf252019-05-24 10:07:42 +0100558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 if ((ret = dispatch_data(dst, buf, p->len)) <= 0) {
560 mbedtls_printf(" ! dispatch returned %d\n", ret);
561 return ret;
Hanno Becker98aaf252019-05-24 10:07:42 +0100562 }
563 }
564
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200565 /* insert corrupted ApplicationData record? */
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 if (opt.bad_ad &&
567 strcmp(p->type, "ApplicationData") == 0) {
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200568 unsigned char buf[MAX_MSG_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 memcpy(buf, p->buf, p->len);
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 if (p->len <= 13) {
572 mbedtls_printf(" ! can't corrupt empty AD record");
573 } else {
Hanno Beckerfbb0b702017-05-26 16:55:07 +0100574 ++buf[13];
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 print_packet(p, "corrupted");
Hanno Beckerfbb0b702017-05-26 16:55:07 +0100576 }
577
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 if ((ret = dispatch_data(dst, buf, p->len)) <= 0) {
579 mbedtls_printf(" ! dispatch returned %d\n", ret);
580 return ret;
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200581 }
582 }
583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 print_packet(p, why);
585 if ((ret = dispatch_data(dst, p->buf, p->len)) <= 0) {
586 mbedtls_printf(" ! dispatch returned %d\n", ret);
587 return ret;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200588 }
589
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200590 /* Don't duplicate Application Data, only handshake covered */
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 if (opt.duplicate != 0 &&
592 strcmp(p->type, "ApplicationData") != 0 &&
593 rand() % opt.duplicate == 0) {
594 print_packet(p, "duplicated");
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200595
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 if ((ret = dispatch_data(dst, p->buf, p->len)) <= 0) {
597 mbedtls_printf(" ! dispatch returned %d\n", ret);
598 return ret;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200599 }
600 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200601
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100602 /* Inject ClientHello after first ApplicationData */
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 if (opt.inject_clihlo != 0 &&
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200604 inject_clihlo_state == ICH_CACHED &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 strcmp(p->type, "ApplicationData") == 0) {
606 print_packet(&initial_clihlo, "injected");
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 if ((ret = dispatch_data(dst, initial_clihlo.buf,
609 initial_clihlo.len)) <= 0) {
610 mbedtls_printf(" ! dispatch returned %d\n", ret);
611 return ret;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100612 }
613
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200614 inject_clihlo_state = ICH_INJECTED;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100615 }
616
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 return 0;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200618}
619
Hanno Becker101bcba2018-08-21 16:39:51 +0100620#define MAX_DELAYED_MSG 5
621static size_t prev_len;
622static packet prev[MAX_DELAYED_MSG];
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200623
Michael Schuster6fa32fd2024-06-01 21:15:02 +0200624static void clear_pending(void)
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200625{
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 memset(&prev, 0, sizeof(prev));
Hanno Becker101bcba2018-08-21 16:39:51 +0100627 prev_len = 0;
628}
629
Michael Schuster6fa32fd2024-06-01 21:15:02 +0200630static void delay_packet(packet *delay)
Hanno Becker101bcba2018-08-21 16:39:51 +0100631{
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 if (prev_len == MAX_DELAYED_MSG) {
Hanno Becker101bcba2018-08-21 16:39:51 +0100633 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 }
Hanno Becker101bcba2018-08-21 16:39:51 +0100635
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 memcpy(&prev[prev_len++], delay, sizeof(packet));
Hanno Becker101bcba2018-08-21 16:39:51 +0100637}
638
Michael Schuster6fa32fd2024-06-01 21:15:02 +0200639static int send_delayed(void)
Hanno Becker101bcba2018-08-21 16:39:51 +0100640{
641 uint8_t offset;
642 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 for (offset = 0; offset < prev_len; offset++) {
644 ret = send_packet(&prev[offset], "delayed");
645 if (ret != 0) {
646 return ret;
647 }
Hanno Becker101bcba2018-08-21 16:39:51 +0100648 }
649
650 clear_pending();
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 return 0;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200652}
653
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200654/*
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200655 * Avoid dropping or delaying a packet that was already dropped or delayed
656 * ("held") twice: this only results in uninteresting timeouts. We can't rely
657 * on type to identify packets, since during renegotiation they're all
658 * encrypted. So, rely on size mod 2048 (which is usually just size).
659 *
660 * We only hold packets at the level of entire datagrams, not at the level
Hanno Becker961e6772019-06-04 13:04:28 +0100661 * of records. In particular, if the peer changes the way it packs multiple
662 * records into a single datagram, we don't necessarily count the number of
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200663 * times a record has been held correctly. However, the only known reason
Hanno Becker961e6772019-06-04 13:04:28 +0100664 * why a peer would change datagram packing is disabling the latter on
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200665 * retransmission, in which case we'd hold involved records at most
666 * HOLD_MAX + 1 times.
667 */
668static unsigned char held[2048] = { 0 };
669#define HOLD_MAX 2
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200670
Michael Schuster6fa32fd2024-06-01 21:15:02 +0200671static int handle_message(const char *way,
Michael Schuster82984bc2024-06-12 00:05:25 +0200672 mbedtls_net_context *dst,
673 mbedtls_net_context *src)
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200674{
675 int ret;
676 packet cur;
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200677 size_t id;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200678
Hanno Becker01ea7782018-08-17 13:33:41 +0100679 uint8_t delay_idx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 char **delay_list;
Hanno Becker01ea7782018-08-17 13:33:41 +0100681 uint8_t delay_list_len;
682
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +0200683 /* receive packet */
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 if ((ret = mbedtls_net_recv(src, cur.buf, sizeof(cur.buf))) <= 0) {
685 mbedtls_printf(" ! mbedtls_net_recv returned %d\n", ret);
686 return ret;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200687 }
688
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200689 cur.len = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 cur.type = msg_type(cur.buf, cur.len);
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200691 cur.way = way;
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200692 cur.dst = dst;
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 print_packet(&cur, NULL);
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200694
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 id = cur.len % sizeof(held);
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200696
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if (strcmp(way, "S <- C") == 0) {
Hanno Becker01ea7782018-08-17 13:33:41 +0100698 delay_list = opt.delay_cli;
699 delay_list_len = opt.delay_cli_cnt;
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 } else {
Hanno Becker01ea7782018-08-17 13:33:41 +0100701 delay_list = opt.delay_srv;
702 delay_list_len = opt.delay_srv_cnt;
703 }
Hanno Beckercf469452018-08-28 10:09:47 +0100704
Hanno Becker01ea7782018-08-17 13:33:41 +0100705 /* Check if message type is in the list of messages
706 * that should be delayed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 for (delay_idx = 0; delay_idx < delay_list_len; delay_idx++) {
708 if (delay_list[delay_idx] == NULL) {
Hanno Becker01ea7782018-08-17 13:33:41 +0100709 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 }
Hanno Becker01ea7782018-08-17 13:33:41 +0100711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if (strcmp(delay_list[delay_idx], cur.type) == 0) {
Hanno Becker01ea7782018-08-17 13:33:41 +0100713 /* Delay message */
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 delay_packet(&cur);
Hanno Becker01ea7782018-08-17 13:33:41 +0100715
716 /* Remove entry from list */
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 mbedtls_free(delay_list[delay_idx]);
Hanno Becker01ea7782018-08-17 13:33:41 +0100718 delay_list[delay_idx] = NULL;
719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 return 0;
Hanno Becker01ea7782018-08-17 13:33:41 +0100721 }
722 }
723
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200724 /* do we want to drop, delay, or forward it? */
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 if ((opt.mtu != 0 &&
726 cur.len > (unsigned) opt.mtu) ||
727 (opt.drop != 0 &&
728 strcmp(cur.type, "CID") != 0 &&
729 strcmp(cur.type, "ApplicationData") != 0 &&
730 !(opt.protect_hvr &&
731 strcmp(cur.type, "HelloVerifyRequest") == 0) &&
732 cur.len != (size_t) opt.protect_len &&
733 held[id] < HOLD_MAX &&
734 rand() % opt.drop == 0)) {
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200735 ++held[id];
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 } else if ((opt.delay_ccs == 1 &&
737 strcmp(cur.type, "ChangeCipherSpec") == 0) ||
738 (opt.delay != 0 &&
739 strcmp(cur.type, "CID") != 0 &&
740 strcmp(cur.type, "ApplicationData") != 0 &&
741 !(opt.protect_hvr &&
742 strcmp(cur.type, "HelloVerifyRequest") == 0) &&
743 cur.len != (size_t) opt.protect_len &&
744 held[id] < HOLD_MAX &&
745 rand() % opt.delay == 0)) {
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200746 ++held[id];
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 delay_packet(&cur);
748 } else {
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200749 /* forward and possibly duplicate */
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 if ((ret = send_packet(&cur, "forwarded")) != 0) {
751 return ret;
752 }
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200753
Hanno Becker101bcba2018-08-21 16:39:51 +0100754 /* send previously delayed messages if any */
755 ret = send_delayed();
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 if (ret != 0) {
757 return ret;
758 }
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200759 }
760
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 return 0;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200762}
763
Gilles Peskine449bd832023-01-11 14:50:10 +0100764int main(int argc, char *argv[])
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200765{
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100766 int ret = 1;
767 int exit_code = MBEDTLS_EXIT_FAILURE;
Hanno Becker01ea7782018-08-17 13:33:41 +0100768 uint8_t delay_idx;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200769
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200770 mbedtls_net_context listen_fd, client_fd, server_fd;
Hanno Becker77abef52017-11-02 10:50:28 +0000771
Gilles Peskine449bd832023-01-11 14:50:10 +0100772#if defined(MBEDTLS_TIMING_C)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100773 struct timeval tm;
Hanno Becker77abef52017-11-02 10:50:28 +0000774#endif
775
776 struct timeval *tm_ptr = NULL;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200777
778 int nb_fds;
779 fd_set read_fds;
780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 mbedtls_net_init(&listen_fd);
782 mbedtls_net_init(&client_fd);
783 mbedtls_net_init(&server_fd);
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200784
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 get_options(argc, argv);
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200786
787 /*
788 * Decisions to drop/delay/duplicate packets are pseudo-random: dropping
789 * exactly 1 in N packets would lead to problems when a flight has exactly
790 * N packets: the same packet would be dropped on every resend.
791 *
792 * In order to be able to reproduce problems reliably, the seed may be
793 * specified explicitly.
794 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 if (opt.seed == 0) {
Gilles Peskinee756f642022-04-05 21:39:43 +0200796#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 opt.seed = (unsigned int) mbedtls_time(NULL);
Gilles Peskinee756f642022-04-05 21:39:43 +0200798#else
799 opt.seed = 1;
800#endif /* MBEDTLS_HAVE_TIME */
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 mbedtls_printf(" . Pseudo-random seed: %u\n", opt.seed);
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200802 }
803
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 srand(opt.seed);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200805
806 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200807 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200808 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 mbedtls_printf(" . Connect to server on UDP/%s/%s ...",
810 opt.server_addr, opt.server_port);
811 fflush(stdout);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200812
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 if ((ret = mbedtls_net_connect(&server_fd, opt.server_addr, opt.server_port,
814 MBEDTLS_NET_PROTO_UDP)) != 0) {
815 mbedtls_printf(" failed\n ! mbedtls_net_connect returned %d\n\n", ret);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200816 goto exit;
817 }
818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200820
821 /*
822 * 1. Setup the "listening" UDP socket
823 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 mbedtls_printf(" . Bind on UDP/%s/%s ...",
825 opt.listen_addr, opt.listen_port);
826 fflush(stdout);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200827
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 if ((ret = mbedtls_net_bind(&listen_fd, opt.listen_addr, opt.listen_port,
829 MBEDTLS_NET_PROTO_UDP)) != 0) {
830 mbedtls_printf(" failed\n ! mbedtls_net_bind returned %d\n\n", ret);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200831 goto exit;
832 }
833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200835
836 /*
837 * 2. Wait until a client connects
838 */
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200839accept:
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 mbedtls_net_free(&client_fd);
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200841
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 mbedtls_printf(" . Waiting for a remote connection ...");
843 fflush(stdout);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200844
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 if ((ret = mbedtls_net_accept(&listen_fd, &client_fd,
846 NULL, 0, NULL)) != 0) {
847 mbedtls_printf(" failed\n ! mbedtls_net_accept returned %d\n\n", ret);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200848 goto exit;
849 }
850
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200852
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200853 /*
854 * 3. Forward packets forever (kill the process to terminate it)
855 */
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200856 clear_pending();
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 memset(held, 0, sizeof(held));
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200858
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200859 nb_fds = client_fd.fd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 if (nb_fds < server_fd.fd) {
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200861 nb_fds = server_fd.fd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 }
863 if (nb_fds < listen_fd.fd) {
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200864 nb_fds = listen_fd.fd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 }
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200866 ++nb_fds;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200867
Hanno Becker0cc77742017-10-31 14:10:07 +0000868#if defined(MBEDTLS_TIMING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 if (opt.pack > 0) {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100870 outbuf[0].ctx = &server_fd;
871 outbuf[0].description = "S <- C";
872 outbuf[0].num_datagrams = 0;
873 outbuf[0].len = 0;
874
875 outbuf[1].ctx = &client_fd;
876 outbuf[1].description = "S -> C";
877 outbuf[1].num_datagrams = 0;
878 outbuf[1].len = 0;
879 }
Hanno Becker0cc77742017-10-31 14:10:07 +0000880#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100881
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 while (1) {
Hanno Becker77abef52017-11-02 10:50:28 +0000883#if defined(MBEDTLS_TIMING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 if (opt.pack > 0) {
Hanno Becker77abef52017-11-02 10:50:28 +0000885 unsigned max_wait_server, max_wait_client, max_wait;
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 max_wait_server = ctx_buffer_time_remaining(&outbuf[0]);
887 max_wait_client = ctx_buffer_time_remaining(&outbuf[1]);
Hanno Becker77abef52017-11-02 10:50:28 +0000888
889 max_wait = (unsigned) -1;
890
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 if (max_wait_server == 0) {
892 ctx_buffer_flush(&outbuf[0]);
893 } else {
Hanno Becker77abef52017-11-02 10:50:28 +0000894 max_wait = max_wait_server;
Hanno Becker77abef52017-11-02 10:50:28 +0000895 }
896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 if (max_wait_client == 0) {
898 ctx_buffer_flush(&outbuf[1]);
899 } else {
900 if (max_wait_client < max_wait) {
901 max_wait = max_wait_client;
902 }
903 }
904
905 if (max_wait != (unsigned) -1) {
Hanno Becker77abef52017-11-02 10:50:28 +0000906 tm.tv_sec = max_wait / 1000;
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 tm.tv_usec = (max_wait % 1000) * 1000;
Hanno Becker77abef52017-11-02 10:50:28 +0000908
909 tm_ptr = &tm;
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 } else {
Hanno Becker77abef52017-11-02 10:50:28 +0000911 tm_ptr = NULL;
912 }
913 }
914#endif /* MBEDTLS_TIMING_C */
915
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 FD_ZERO(&read_fds);
917 FD_SET(server_fd.fd, &read_fds);
918 FD_SET(client_fd.fd, &read_fds);
919 FD_SET(listen_fd.fd, &read_fds);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200920
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 if ((ret = select(nb_fds, &read_fds, NULL, NULL, tm_ptr)) < 0) {
922 perror("select");
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200923 goto exit;
924 }
925
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 if (FD_ISSET(listen_fd.fd, &read_fds)) {
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200927 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200928 }
929
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 if (FD_ISSET(client_fd.fd, &read_fds)) {
931 if ((ret = handle_message("S <- C",
932 &server_fd, &client_fd)) != 0) {
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200933 goto accept;
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 }
935 }
936
937 if (FD_ISSET(server_fd.fd, &read_fds)) {
938 if ((ret = handle_message("S -> C",
939 &client_fd, &server_fd)) != 0) {
940 goto accept;
941 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200942 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100943
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200944 }
945
946exit:
947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948#ifdef MBEDTLS_ERROR_C
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200950 char error_buf[100];
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 mbedtls_strerror(ret, error_buf, 100);
952 mbedtls_printf("Last error was: -0x%04X - %s\n\n", (unsigned int) -ret, error_buf);
953 fflush(stdout);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200954 }
955#endif
956
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 for (delay_idx = 0; delay_idx < MAX_DELAYED_HS; delay_idx++) {
958 mbedtls_free(opt.delay_cli[delay_idx]);
959 mbedtls_free(opt.delay_srv[delay_idx]);
Hanno Becker01ea7782018-08-17 13:33:41 +0100960 }
961
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 mbedtls_net_free(&client_fd);
963 mbedtls_net_free(&server_fd);
964 mbedtls_net_free(&listen_fd);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200965
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 mbedtls_exit(exit_code);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200967}
968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969#endif /* MBEDTLS_NET_C */