blob: e5b821719896a6028b69459bf8a479ea312117e7 [file] [log] [blame]
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001/*
Shaun Case0e7791f2021-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 Rodgman7ff79652023-11-03 12:04:52 +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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000015#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020016#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020017#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020018#endif
19
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000021#include "mbedtls/platform.h"
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000022#else
Simon Butcherd3138c32016-04-27 01:26:50 +010023#include <stdio.h>
24#include <stdlib.h>
David Horstmann0e4a1aa2021-11-29 17:28:13 +000025#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherd3138c32016-04-27 01:26:50 +010026#include <time.h>
Andres Amaya Garcia80081a62018-04-29 21:58:53 +010027#define mbedtls_time time
28#define mbedtls_time_t time_t
David Horstmann0e4a1aa2021-11-29 17:28:13 +000029#endif
Andres Amaya Garcia80081a62018-04-29 21:58:53 +010030#define mbedtls_printf printf
Hanno Becker01ea7782018-08-17 13:33:41 +010031#define mbedtls_calloc calloc
32#define mbedtls_free free
k-stachowiak776521a2019-05-23 09:46:47 +020033#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010034#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia80081a62018-04-29 21:58:53 +010035#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
36#endif /* MBEDTLS_PLATFORM_C */
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if !defined(MBEDTLS_NET_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010039int main(void)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020040{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010041 mbedtls_printf("MBEDTLS_NET_C not defined.\n");
42 mbedtls_exit(0);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020043}
44#else
45
Andres AG788aa4a2016-09-14 14:32:09 +010046#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/error.h"
48#include "mbedtls/ssl.h"
Hanno Becker0cc77742017-10-31 14:10:07 +000049#include "mbedtls/timing.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020050
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +000051#include <string.h>
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020052
53/* For select() */
54#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
55 !defined(EFI32)
56#include <winsock2.h>
57#include <windows.h>
58#if defined(_MSC_VER)
59#if defined(_WIN32_WCE)
60#pragma comment( lib, "ws2.lib" )
61#else
62#pragma comment( lib, "ws2_32.lib" )
63#endif
64#endif /* _MSC_VER */
65#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Andrzej Kurek65f93d52022-03-04 15:18:09 -050066#if defined(MBEDTLS_HAVE_TIME)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020067#include <sys/time.h>
Andrzej Kurek65f93d52022-03-04 15:18:09 -050068#endif
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020069#include <sys/types.h>
70#include <unistd.h>
71#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
72
Manuel Pégourié-Gonnardb46780e2014-09-23 12:17:30 +020073#define MAX_MSG_SIZE 16384 + 2048 /* max record/datagram size */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020074
75#define DFL_SERVER_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020076#define DFL_SERVER_PORT "4433"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020077#define DFL_LISTEN_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020078#define DFL_LISTEN_PORT "5556"
Hanno Becker1dd62ea2017-05-22 14:30:59 +010079#define DFL_PACK 0
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020080
Hanno Becker0cc77742017-10-31 14:10:07 +000081#if defined(MBEDTLS_TIMING_C)
82#define USAGE_PACK \
83 " pack=%%d default: 0 (don't pack)\n" \
84 " options: t > 0 (pack for t milliseconds)\n"
85#else
86#define USAGE_PACK
87#endif
88
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020089#define USAGE \
90 "\n usage: udp_proxy param=<>...\n" \
91 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +020092 " server_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020093 " server_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +020094 " listen_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020095 " listen_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +020096 "\n" \
97 " duplicate=%%d default: 0 (no duplication)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +020098 " duplicate about 1:N packets randomly\n" \
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +020099 " delay=%%d default: 0 (no delayed packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200100 " delay about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200101 " delay_ccs=0/1 default: 0 (don't delay ChangeCipherSpec)\n" \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100102 " delay_cli=%%s Handshake message from client that should be\n" \
Hanno Becker01ea7782018-08-17 13:33:41 +0100103 " delayed. Possible values are 'ClientHello',\n" \
104 " 'Certificate', 'CertificateVerify', and\n" \
105 " 'ClientKeyExchange'.\n" \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 " May be used multiple times, even for the same\n" \
107 " message, in which case the respective message\n" \
Hanno Becker01ea7782018-08-17 13:33:41 +0100108 " gets delayed multiple times.\n" \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100109 " delay_srv=%%s Handshake message from server that should be\n" \
110 " delayed. Possible values are 'HelloRequest',\n" \
111 " 'ServerHello', 'ServerHelloDone', 'Certificate'\n" \
112 " 'ServerKeyExchange', 'NewSessionTicket',\n" \
113 " 'HelloVerifyRequest' and ''CertificateRequest'.\n" \
114 " May be used multiple times, even for the same\n" \
115 " message, in which case the respective message\n" \
Hanno Becker01ea7782018-08-17 13:33:41 +0100116 " gets delayed multiple times.\n" \
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200117 " drop=%%d default: 0 (no dropped packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200118 " drop about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200119 " mtu=%%d default: 0 (unlimited)\n" \
120 " drop packets larger than N bytes\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200121 " bad_ad=0/1 default: 0 (don't add bad ApplicationData)\n" \
Hanno Becker98aaf252019-05-24 10:07:42 +0100122 " bad_cid=%%d default: 0 (don't corrupt Connection IDs)\n" \
123 " duplicate 1:N packets containing a CID,\n" \
124 " modifying CID in first instance of the packet.\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200125 " protect_hvr=0/1 default: 0 (don't protect HelloVerifyRequest)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000126 " protect_len=%%d default: (don't protect packets of this size)\n" \
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100127 " inject_clihlo=0/1 default: 0 (don't inject fake ClientHello)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200128 "\n" \
129 " seed=%%d default: (use current time)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000130 USAGE_PACK \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200131 "\n"
132
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200133/*
134 * global options
135 */
Hanno Becker01ea7782018-08-17 13:33:41 +0100136
137#define MAX_DELAYED_HS 10
138
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100139static struct options {
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200140 const char *server_addr; /* address to forward packets to */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200141 const char *server_port; /* port to forward packets to */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200142 const char *listen_addr; /* address for accepting client connections */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200143 const char *listen_port; /* port for accepting client connections */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200144
145 int duplicate; /* duplicate 1 in N packets (none if 0) */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200146 int delay; /* delay 1 packet in N (none if 0) */
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200147 int delay_ccs; /* delay ChangeCipherSpec */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100148 char *delay_cli[MAX_DELAYED_HS]; /* handshake types of messages from
Hanno Becker41038102018-08-28 11:15:32 +0100149 * client that should be delayed. */
Hanno Becker01ea7782018-08-17 13:33:41 +0100150 uint8_t delay_cli_cnt; /* Number of entries in delay_cli. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100151 char *delay_srv[MAX_DELAYED_HS]; /* handshake types of messages from
Hanno Becker41038102018-08-28 11:15:32 +0100152 * server that should be delayed. */
Hanno Becker01ea7782018-08-17 13:33:41 +0100153 uint8_t delay_srv_cnt; /* Number of entries in delay_srv. */
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200154 int drop; /* drop 1 packet in N (none if 0) */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200155 int mtu; /* drop packets larger than this */
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200156 int bad_ad; /* inject corrupted ApplicationData record */
Hanno Becker98aaf252019-05-24 10:07:42 +0100157 unsigned bad_cid; /* inject corrupted CID record */
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200158 int protect_hvr; /* never drop or delay HelloVerifyRequest */
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200159 int protect_len; /* never drop/delay packet of the given size*/
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100160 int inject_clihlo; /* inject fake ClientHello after handshake */
Hanno Becker77abef52017-11-02 10:50:28 +0000161 unsigned pack; /* merge packets into single datagram for
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100162 * at most \c merge milliseconds if > 0 */
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200163 unsigned int seed; /* seed for "random" events */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200164} opt;
165
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100166static void exit_usage(const char *name, const char *value)
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200167{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100168 if (value == NULL) {
169 mbedtls_printf(" unknown option or missing value: %s\n", name);
170 } else {
171 mbedtls_printf(" option %s: illegal value: %s\n", name, value);
172 }
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200173
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100174 mbedtls_printf(USAGE);
175 mbedtls_exit(1);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200176}
177
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178static void get_options(int argc, char *argv[])
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200179{
180 int i;
181 char *p, *q;
182
183 opt.server_addr = DFL_SERVER_ADDR;
184 opt.server_port = DFL_SERVER_PORT;
185 opt.listen_addr = DFL_LISTEN_ADDR;
186 opt.listen_port = DFL_LISTEN_PORT;
Hanno Becker211f44c2017-10-31 14:08:10 +0000187 opt.pack = DFL_PACK;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200188 /* Other members default to 0 */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200189
Hanno Becker01ea7782018-08-17 13:33:41 +0100190 opt.delay_cli_cnt = 0;
191 opt.delay_srv_cnt = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192 memset(opt.delay_cli, 0, sizeof(opt.delay_cli));
193 memset(opt.delay_srv, 0, sizeof(opt.delay_srv));
Hanno Becker01ea7782018-08-17 13:33:41 +0100194
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100195 for (i = 1; i < argc; i++) {
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200196 p = argv[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100197 if ((q = strchr(p, '=')) == NULL) {
198 exit_usage(p, NULL);
199 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200200 *q++ = '\0';
201
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100202 if (strcmp(p, "server_addr") == 0) {
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200203 opt.server_addr = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204 } else if (strcmp(p, "server_port") == 0) {
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200205 opt.server_port = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206 } else if (strcmp(p, "listen_addr") == 0) {
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200207 opt.listen_addr = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100208 } else if (strcmp(p, "listen_port") == 0) {
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200209 opt.listen_port = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210 } else if (strcmp(p, "duplicate") == 0) {
211 opt.duplicate = atoi(q);
212 if (opt.duplicate < 0 || opt.duplicate > 20) {
213 exit_usage(p, q);
214 }
215 } else if (strcmp(p, "delay") == 0) {
216 opt.delay = atoi(q);
217 if (opt.delay < 0 || opt.delay > 20 || opt.delay == 1) {
218 exit_usage(p, q);
219 }
220 } else if (strcmp(p, "delay_ccs") == 0) {
221 opt.delay_ccs = atoi(q);
222 if (opt.delay_ccs < 0 || opt.delay_ccs > 1) {
223 exit_usage(p, q);
224 }
225 } else if (strcmp(p, "delay_cli") == 0 ||
226 strcmp(p, "delay_srv") == 0) {
Hanno Becker01ea7782018-08-17 13:33:41 +0100227 uint8_t *delay_cnt;
228 char **delay_list;
229 size_t len;
230 char *buf;
231
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100232 if (strcmp(p, "delay_cli") == 0) {
Hanno Becker01ea7782018-08-17 13:33:41 +0100233 delay_cnt = &opt.delay_cli_cnt;
234 delay_list = opt.delay_cli;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100235 } else {
Hanno Becker01ea7782018-08-17 13:33:41 +0100236 delay_cnt = &opt.delay_srv_cnt;
237 delay_list = opt.delay_srv;
238 }
239
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100240 if (*delay_cnt == MAX_DELAYED_HS) {
241 mbedtls_printf(" too many uses of %s: only %d allowed\n",
242 p, MAX_DELAYED_HS);
243 exit_usage(p, NULL);
Hanno Becker01ea7782018-08-17 13:33:41 +0100244 }
245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246 len = strlen(q);
247 buf = mbedtls_calloc(1, len + 1);
248 if (buf == NULL) {
249 mbedtls_printf(" Allocation failure\n");
250 exit(1);
Hanno Becker01ea7782018-08-17 13:33:41 +0100251 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100252 memcpy(buf, q, len + 1);
Hanno Becker01ea7782018-08-17 13:33:41 +0100253
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 delay_list[(*delay_cnt)++] = buf;
255 } else if (strcmp(p, "drop") == 0) {
256 opt.drop = atoi(q);
257 if (opt.drop < 0 || opt.drop > 20 || opt.drop == 1) {
258 exit_usage(p, q);
259 }
260 } else if (strcmp(p, "pack") == 0) {
Hanno Becker0cc77742017-10-31 14:10:07 +0000261#if defined(MBEDTLS_TIMING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262 opt.pack = (unsigned) atoi(q);
Hanno Becker0cc77742017-10-31 14:10:07 +0000263#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 mbedtls_printf(" option pack only defined if MBEDTLS_TIMING_C is enabled\n");
265 exit(1);
Hanno Becker0cc77742017-10-31 14:10:07 +0000266#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100267 } else if (strcmp(p, "mtu") == 0) {
268 opt.mtu = atoi(q);
269 if (opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE) {
270 exit_usage(p, q);
271 }
272 } else if (strcmp(p, "bad_ad") == 0) {
273 opt.bad_ad = atoi(q);
274 if (opt.bad_ad < 0 || opt.bad_ad > 1) {
275 exit_usage(p, q);
276 }
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200277 }
Hanno Becker98aaf252019-05-24 10:07:42 +0100278#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279 else if (strcmp(p, "bad_cid") == 0) {
280 opt.bad_cid = (unsigned) atoi(q);
Hanno Becker98aaf252019-05-24 10:07:42 +0100281 }
282#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100283 else if (strcmp(p, "protect_hvr") == 0) {
284 opt.protect_hvr = atoi(q);
285 if (opt.protect_hvr < 0 || opt.protect_hvr > 1) {
286 exit_usage(p, q);
287 }
288 } else if (strcmp(p, "protect_len") == 0) {
289 opt.protect_len = atoi(q);
290 if (opt.protect_len < 0) {
291 exit_usage(p, q);
292 }
293 } else if (strcmp(p, "inject_clihlo") == 0) {
294 opt.inject_clihlo = atoi(q);
295 if (opt.inject_clihlo < 0 || opt.inject_clihlo > 1) {
296 exit_usage(p, q);
297 }
298 } else if (strcmp(p, "seed") == 0) {
299 opt.seed = atoi(q);
300 if (opt.seed == 0) {
301 exit_usage(p, q);
302 }
303 } else {
304 exit_usage(p, NULL);
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200305 }
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200306 }
307}
308
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100309static const char *msg_type(unsigned char *msg, size_t len)
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200310{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311 if (len < 1) {
312 return "Invalid";
313 }
314 switch (msg[0]) {
315 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: return "ChangeCipherSpec";
316 case MBEDTLS_SSL_MSG_ALERT: return "Alert";
317 case MBEDTLS_SSL_MSG_APPLICATION_DATA: return "ApplicationData";
318 case MBEDTLS_SSL_MSG_CID: return "CID";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 case MBEDTLS_SSL_MSG_HANDSHAKE: break; /* See below */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100320 default: return "Unknown";
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200321 }
322
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100323 if (len < 13 + 12) {
324 return "Invalid handshake";
325 }
Manuel Pégourié-Gonnard8cc7e032014-09-25 12:59:05 +0200326
327 /*
328 * Our handshake message are less than 2^16 bytes long, so they should
329 * have 0 as the first byte of length, frag_offset and frag_length.
330 * Otherwise, assume they are encrypted.
331 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100332 if (msg[14] || msg[19] || msg[22]) {
333 return "Encrypted handshake";
334 }
Manuel Pégourié-Gonnard8cc7e032014-09-25 12:59:05 +0200335
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100336 switch (msg[13]) {
337 case MBEDTLS_SSL_HS_HELLO_REQUEST: return "HelloRequest";
338 case MBEDTLS_SSL_HS_CLIENT_HELLO: return "ClientHello";
339 case MBEDTLS_SSL_HS_SERVER_HELLO: return "ServerHello";
340 case MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST: return "HelloVerifyRequest";
341 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET: return "NewSessionTicket";
342 case MBEDTLS_SSL_HS_CERTIFICATE: return "Certificate";
343 case MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE: return "ServerKeyExchange";
344 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST: return "CertificateRequest";
345 case MBEDTLS_SSL_HS_SERVER_HELLO_DONE: return "ServerHelloDone";
346 case MBEDTLS_SSL_HS_CERTIFICATE_VERIFY: return "CertificateVerify";
347 case MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE: return "ClientKeyExchange";
348 case MBEDTLS_SSL_HS_FINISHED: return "Finished";
349 default: return "Unknown handshake";
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200350 }
351}
352
Hanno Becker0cc77742017-10-31 14:10:07 +0000353#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200354/* Return elapsed time in milliseconds since the first call */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100355static unsigned elapsed_time(void)
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200356{
Hanno Becker92474da2017-10-31 14:09:30 +0000357 static int initialized = 0;
358 static struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200359
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100360 if (initialized == 0) {
361 (void) mbedtls_timing_get_timer(&hires, 1);
Hanno Becker92474da2017-10-31 14:09:30 +0000362 initialized = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100363 return 0;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200364 }
365
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100366 return mbedtls_timing_get_timer(&hires, 0);
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200367}
368
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100369typedef struct {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100370 mbedtls_net_context *ctx;
371
372 const char *description;
373
Hanno Becker77abef52017-11-02 10:50:28 +0000374 unsigned packet_lifetime;
375 unsigned num_datagrams;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100376
377 unsigned char data[MAX_MSG_SIZE];
Hanno Beckera5e68972017-12-06 08:35:02 +0000378 size_t len;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100379
380} ctx_buffer;
381
382static ctx_buffer outbuf[2];
383
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100384static int ctx_buffer_flush(ctx_buffer *buf)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100385{
386 int ret;
387
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100388 mbedtls_printf(" %05u flush %s: %u bytes, %u datagrams, last %u ms\n",
389 elapsed_time(), buf->description,
390 (unsigned) buf->len, buf->num_datagrams,
391 elapsed_time() - buf->packet_lifetime);
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100392
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393 ret = mbedtls_net_send(buf->ctx, buf->data, buf->len);
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100394
395 buf->len = 0;
396 buf->num_datagrams = 0;
397
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100398 return ret;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100399}
400
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100401static unsigned ctx_buffer_time_remaining(ctx_buffer *buf)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100402{
Tom Cosgrove49f99bc2022-12-04 16:44:21 +0000403 unsigned const cur_time = elapsed_time();
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100404
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100405 if (buf->num_datagrams == 0) {
406 return (unsigned) -1;
407 }
Hanno Becker77abef52017-11-02 10:50:28 +0000408
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100409 if (cur_time - buf->packet_lifetime >= opt.pack) {
410 return 0;
411 }
Hanno Becker77abef52017-11-02 10:50:28 +0000412
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100413 return opt.pack - (cur_time - buf->packet_lifetime);
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100414}
415
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100416static int ctx_buffer_append(ctx_buffer *buf,
417 const unsigned char *data,
418 size_t len)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100419{
420 int ret;
421
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100422 if (len > (size_t) INT_MAX) {
423 return -1;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100424 }
425
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100426 if (len > sizeof(buf->data)) {
427 mbedtls_printf(" ! buffer size %u too large (max %u)\n",
428 (unsigned) len, (unsigned) sizeof(buf->data));
429 return -1;
430 }
431
432 if (sizeof(buf->data) - buf->len < len) {
433 if ((ret = ctx_buffer_flush(buf)) <= 0) {
434 mbedtls_printf("ctx_buffer_flush failed with -%#04x", (unsigned int) -ret);
435 return ret;
Hanno Becker31f6e372019-05-08 15:36:31 +0100436 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100437 }
438
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100439 memcpy(buf->data + buf->len, data, len);
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100440
441 buf->len += len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100442 if (++buf->num_datagrams == 1) {
Tom Cosgrove49f99bc2022-12-04 16:44:21 +0000443 buf->packet_lifetime = elapsed_time();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100444 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100445
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100446 return (int) len;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100447}
Hanno Becker77abef52017-11-02 10:50:28 +0000448#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100449
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100450static int dispatch_data(mbedtls_net_context *ctx,
451 const unsigned char *data,
452 size_t len)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100453{
Hanno Becker31f6e372019-05-08 15:36:31 +0100454 int ret;
Hanno Becker77abef52017-11-02 10:50:28 +0000455#if defined(MBEDTLS_TIMING_C)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100456 ctx_buffer *buf = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100457 if (opt.pack > 0) {
458 if (outbuf[0].ctx == ctx) {
Hanno Becker77abef52017-11-02 10:50:28 +0000459 buf = &outbuf[0];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100460 } else if (outbuf[1].ctx == ctx) {
Hanno Becker77abef52017-11-02 10:50:28 +0000461 buf = &outbuf[1];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100462 }
Hanno Becker0cc77742017-10-31 14:10:07 +0000463
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100464 if (buf == NULL) {
465 return -1;
466 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100467
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100468 return ctx_buffer_append(buf, data, len);
Hanno Becker77abef52017-11-02 10:50:28 +0000469 }
470#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100471
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100472 ret = mbedtls_net_send(ctx, data, len);
473 if (ret < 0) {
474 mbedtls_printf("net_send returned -%#04x\n", (unsigned int) -ret);
Hanno Becker31f6e372019-05-08 15:36:31 +0100475 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100476 return ret;
Hanno Becker0cc77742017-10-31 14:10:07 +0000477}
478
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100479typedef struct {
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200480 mbedtls_net_context *dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200481 const char *way;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200482 const char *type;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200483 unsigned len;
484 unsigned char buf[MAX_MSG_SIZE];
485} packet;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200486
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200487/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100488void print_packet(const packet *p, const char *why)
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200489{
Hanno Becker0cc77742017-10-31 14:10:07 +0000490#if defined(MBEDTLS_TIMING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100491 if (why == NULL) {
492 mbedtls_printf(" %05u dispatch %s %s (%u bytes)\n",
493 elapsed_time(), p->way, p->type, p->len);
494 } else {
495 mbedtls_printf(" %05u dispatch %s %s (%u bytes): %s\n",
496 elapsed_time(), p->way, p->type, p->len, why);
497 }
Hanno Becker0cc77742017-10-31 14:10:07 +0000498#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100499 if (why == NULL) {
500 mbedtls_printf(" dispatch %s %s (%u bytes)\n",
501 p->way, p->type, p->len);
502 } else {
503 mbedtls_printf(" dispatch %s %s (%u bytes): %s\n",
504 p->way, p->type, p->len, why);
505 }
Hanno Becker0cc77742017-10-31 14:10:07 +0000506#endif
507
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100508 fflush(stdout);
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200509}
510
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100511/*
512 * In order to test the server's behaviour when receiving a ClientHello after
513 * the connection is established (this could be a hard reset from the client,
514 * but the server must not drop the existing connection before establishing
515 * client reachability, see RFC 6347 Section 4.2.8), we memorize the first
516 * ClientHello we see (which can't have a cookie), then replay it after the
517 * first ApplicationData record - then we're done.
518 *
519 * This is controlled by the inject_clihlo option.
520 *
521 * We want an explicit state and a place to store the packet.
522 */
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200523typedef enum {
524 ICH_INIT, /* haven't seen the first ClientHello yet */
525 ICH_CACHED, /* cached the initial ClientHello */
526 ICH_INJECTED, /* ClientHello already injected, done */
527} inject_clihlo_state_t;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100528
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200529static inject_clihlo_state_t inject_clihlo_state;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100530static packet initial_clihlo;
531
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100532int send_packet(const packet *p, const char *why)
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200533{
534 int ret;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200535 mbedtls_net_context *dst = p->dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200536
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100537 /* save initial ClientHello? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100538 if (opt.inject_clihlo != 0 &&
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200539 inject_clihlo_state == ICH_INIT &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100540 strcmp(p->type, "ClientHello") == 0) {
541 memcpy(&initial_clihlo, p, sizeof(packet));
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200542 inject_clihlo_state = ICH_CACHED;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100543 }
544
Hanno Becker98aaf252019-05-24 10:07:42 +0100545 /* insert corrupted CID record? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100546 if (opt.bad_cid != 0 &&
547 strcmp(p->type, "CID") == 0 &&
548 (rand() % opt.bad_cid) == 0) {
Hanno Becker98aaf252019-05-24 10:07:42 +0100549 unsigned char buf[MAX_MSG_SIZE];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100550 memcpy(buf, p->buf, p->len);
Hanno Becker98aaf252019-05-24 10:07:42 +0100551
552 /* The CID resides at offset 11 in the DTLS record header. */
553 buf[11] ^= 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100554 print_packet(p, "modified CID");
Hanno Becker98aaf252019-05-24 10:07:42 +0100555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100556 if ((ret = dispatch_data(dst, buf, p->len)) <= 0) {
557 mbedtls_printf(" ! dispatch returned %d\n", ret);
558 return ret;
Hanno Becker98aaf252019-05-24 10:07:42 +0100559 }
560 }
561
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200562 /* insert corrupted ApplicationData record? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100563 if (opt.bad_ad &&
564 strcmp(p->type, "ApplicationData") == 0) {
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200565 unsigned char buf[MAX_MSG_SIZE];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100566 memcpy(buf, p->buf, p->len);
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200567
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100568 if (p->len <= 13) {
569 mbedtls_printf(" ! can't corrupt empty AD record");
570 } else {
Hanno Beckerfbb0b702017-05-26 16:55:07 +0100571 ++buf[13];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100572 print_packet(p, "corrupted");
Hanno Beckerfbb0b702017-05-26 16:55:07 +0100573 }
574
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100575 if ((ret = dispatch_data(dst, buf, p->len)) <= 0) {
576 mbedtls_printf(" ! dispatch returned %d\n", ret);
577 return ret;
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200578 }
579 }
580
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100581 print_packet(p, why);
582 if ((ret = dispatch_data(dst, p->buf, p->len)) <= 0) {
583 mbedtls_printf(" ! dispatch returned %d\n", ret);
584 return ret;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200585 }
586
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200587 /* Don't duplicate Application Data, only handshake covered */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100588 if (opt.duplicate != 0 &&
589 strcmp(p->type, "ApplicationData") != 0 &&
590 rand() % opt.duplicate == 0) {
591 print_packet(p, "duplicated");
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200592
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100593 if ((ret = dispatch_data(dst, p->buf, p->len)) <= 0) {
594 mbedtls_printf(" ! dispatch returned %d\n", ret);
595 return ret;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200596 }
597 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200598
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100599 /* Inject ClientHello after first ApplicationData */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100600 if (opt.inject_clihlo != 0 &&
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200601 inject_clihlo_state == ICH_CACHED &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100602 strcmp(p->type, "ApplicationData") == 0) {
603 print_packet(&initial_clihlo, "injected");
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100604
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100605 if ((ret = dispatch_data(dst, initial_clihlo.buf,
606 initial_clihlo.len)) <= 0) {
607 mbedtls_printf(" ! dispatch returned %d\n", ret);
608 return ret;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100609 }
610
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200611 inject_clihlo_state = ICH_INJECTED;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100612 }
613
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100614 return 0;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200615}
616
Hanno Becker101bcba2018-08-21 16:39:51 +0100617#define MAX_DELAYED_MSG 5
618static size_t prev_len;
619static packet prev[MAX_DELAYED_MSG];
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200620
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100621void clear_pending(void)
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200622{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100623 memset(&prev, 0, sizeof(prev));
Hanno Becker101bcba2018-08-21 16:39:51 +0100624 prev_len = 0;
625}
626
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100627void delay_packet(packet *delay)
Hanno Becker101bcba2018-08-21 16:39:51 +0100628{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100629 if (prev_len == MAX_DELAYED_MSG) {
Hanno Becker101bcba2018-08-21 16:39:51 +0100630 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100631 }
Hanno Becker101bcba2018-08-21 16:39:51 +0100632
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100633 memcpy(&prev[prev_len++], delay, sizeof(packet));
Hanno Becker101bcba2018-08-21 16:39:51 +0100634}
635
Gowtham Suresh Kumar34d8bd32023-07-26 17:18:55 +0100636int send_delayed(void)
Hanno Becker101bcba2018-08-21 16:39:51 +0100637{
638 uint8_t offset;
639 int ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100640 for (offset = 0; offset < prev_len; offset++) {
641 ret = send_packet(&prev[offset], "delayed");
642 if (ret != 0) {
643 return ret;
644 }
Hanno Becker101bcba2018-08-21 16:39:51 +0100645 }
646
647 clear_pending();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100648 return 0;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200649}
650
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200651/*
Manuel Pégourié-Gonnard14134282021-07-06 12:39:43 +0200652 * Avoid dropping or delaying a packet that was already dropped or delayed
653 * ("held") twice: this only results in uninteresting timeouts. We can't rely
654 * on type to identify packets, since during renegotiation they're all
655 * encrypted. So, rely on size mod 2048 (which is usually just size).
656 *
657 * We only hold packets at the level of entire datagrams, not at the level
Hanno Becker961e6772019-06-04 13:04:28 +0100658 * of records. In particular, if the peer changes the way it packs multiple
659 * records into a single datagram, we don't necessarily count the number of
Manuel Pégourié-Gonnard14134282021-07-06 12:39:43 +0200660 * times a record has been held correctly. However, the only known reason
Hanno Becker961e6772019-06-04 13:04:28 +0100661 * why a peer would change datagram packing is disabling the latter on
Manuel Pégourié-Gonnard14134282021-07-06 12:39:43 +0200662 * retransmission, in which case we'd hold involved records at most
663 * HOLD_MAX + 1 times.
664 */
665static unsigned char held[2048] = { 0 };
666#define HOLD_MAX 2
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200667
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100668int handle_message(const char *way,
669 mbedtls_net_context *dst,
670 mbedtls_net_context *src)
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200671{
672 int ret;
673 packet cur;
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200674 size_t id;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200675
Hanno Becker01ea7782018-08-17 13:33:41 +0100676 uint8_t delay_idx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100677 char **delay_list;
Hanno Becker01ea7782018-08-17 13:33:41 +0100678 uint8_t delay_list_len;
679
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +0200680 /* receive packet */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100681 if ((ret = mbedtls_net_recv(src, cur.buf, sizeof(cur.buf))) <= 0) {
682 mbedtls_printf(" ! mbedtls_net_recv returned %d\n", ret);
683 return ret;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200684 }
685
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200686 cur.len = ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100687 cur.type = msg_type(cur.buf, cur.len);
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200688 cur.way = way;
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200689 cur.dst = dst;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100690 print_packet(&cur, NULL);
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200691
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100692 id = cur.len % sizeof(held);
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200693
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100694 if (strcmp(way, "S <- C") == 0) {
Hanno Becker01ea7782018-08-17 13:33:41 +0100695 delay_list = opt.delay_cli;
696 delay_list_len = opt.delay_cli_cnt;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100697 } else {
Hanno Becker01ea7782018-08-17 13:33:41 +0100698 delay_list = opt.delay_srv;
699 delay_list_len = opt.delay_srv_cnt;
700 }
Hanno Beckercf469452018-08-28 10:09:47 +0100701
Hanno Becker01ea7782018-08-17 13:33:41 +0100702 /* Check if message type is in the list of messages
703 * that should be delayed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100704 for (delay_idx = 0; delay_idx < delay_list_len; delay_idx++) {
705 if (delay_list[delay_idx] == NULL) {
Hanno Becker01ea7782018-08-17 13:33:41 +0100706 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100707 }
Hanno Becker01ea7782018-08-17 13:33:41 +0100708
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100709 if (strcmp(delay_list[delay_idx], cur.type) == 0) {
Hanno Becker01ea7782018-08-17 13:33:41 +0100710 /* Delay message */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100711 delay_packet(&cur);
Hanno Becker01ea7782018-08-17 13:33:41 +0100712
713 /* Remove entry from list */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100714 mbedtls_free(delay_list[delay_idx]);
Hanno Becker01ea7782018-08-17 13:33:41 +0100715 delay_list[delay_idx] = NULL;
716
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100717 return 0;
Hanno Becker01ea7782018-08-17 13:33:41 +0100718 }
719 }
720
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200721 /* do we want to drop, delay, or forward it? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100722 if ((opt.mtu != 0 &&
723 cur.len > (unsigned) opt.mtu) ||
724 (opt.drop != 0 &&
725 strcmp(cur.type, "CID") != 0 &&
726 strcmp(cur.type, "ApplicationData") != 0 &&
727 !(opt.protect_hvr &&
728 strcmp(cur.type, "HelloVerifyRequest") == 0) &&
729 cur.len != (size_t) opt.protect_len &&
730 held[id] < HOLD_MAX &&
731 rand() % opt.drop == 0)) {
Manuel Pégourié-Gonnard14134282021-07-06 12:39:43 +0200732 ++held[id];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100733 } else if ((opt.delay_ccs == 1 &&
734 strcmp(cur.type, "ChangeCipherSpec") == 0) ||
735 (opt.delay != 0 &&
736 strcmp(cur.type, "CID") != 0 &&
737 strcmp(cur.type, "ApplicationData") != 0 &&
738 !(opt.protect_hvr &&
739 strcmp(cur.type, "HelloVerifyRequest") == 0) &&
740 cur.len != (size_t) opt.protect_len &&
741 held[id] < HOLD_MAX &&
742 rand() % opt.delay == 0)) {
Manuel Pégourié-Gonnard14134282021-07-06 12:39:43 +0200743 ++held[id];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100744 delay_packet(&cur);
745 } else {
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200746 /* forward and possibly duplicate */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100747 if ((ret = send_packet(&cur, "forwarded")) != 0) {
748 return ret;
749 }
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200750
Hanno Becker101bcba2018-08-21 16:39:51 +0100751 /* send previously delayed messages if any */
752 ret = send_delayed();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100753 if (ret != 0) {
754 return ret;
755 }
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200756 }
757
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100758 return 0;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200759}
760
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100761int main(int argc, char *argv[])
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200762{
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100763 int ret = 1;
764 int exit_code = MBEDTLS_EXIT_FAILURE;
Hanno Becker01ea7782018-08-17 13:33:41 +0100765 uint8_t delay_idx;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200766
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200767 mbedtls_net_context listen_fd, client_fd, server_fd;
Hanno Becker77abef52017-11-02 10:50:28 +0000768
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100769#if defined(MBEDTLS_TIMING_C)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100770 struct timeval tm;
Hanno Becker77abef52017-11-02 10:50:28 +0000771#endif
772
773 struct timeval *tm_ptr = NULL;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200774
775 int nb_fds;
776 fd_set read_fds;
777
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100778 mbedtls_net_init(&listen_fd);
779 mbedtls_net_init(&client_fd);
780 mbedtls_net_init(&server_fd);
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200781
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100782 get_options(argc, argv);
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200783
784 /*
785 * Decisions to drop/delay/duplicate packets are pseudo-random: dropping
786 * exactly 1 in N packets would lead to problems when a flight has exactly
787 * N packets: the same packet would be dropped on every resend.
788 *
789 * In order to be able to reproduce problems reliably, the seed may be
790 * specified explicitly.
791 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100792 if (opt.seed == 0) {
Gilles Peskine7ece7682022-04-05 21:39:43 +0200793#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100794 opt.seed = (unsigned int) mbedtls_time(NULL);
Gilles Peskine7ece7682022-04-05 21:39:43 +0200795#else
796 opt.seed = 1;
797#endif /* MBEDTLS_HAVE_TIME */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100798 mbedtls_printf(" . Pseudo-random seed: %u\n", opt.seed);
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200799 }
800
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100801 srand(opt.seed);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200802
803 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200804 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200805 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100806 mbedtls_printf(" . Connect to server on UDP/%s/%s ...",
807 opt.server_addr, opt.server_port);
808 fflush(stdout);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200809
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100810 if ((ret = mbedtls_net_connect(&server_fd, opt.server_addr, opt.server_port,
811 MBEDTLS_NET_PROTO_UDP)) != 0) {
812 mbedtls_printf(" failed\n ! mbedtls_net_connect returned %d\n\n", ret);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200813 goto exit;
814 }
815
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100816 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200817
818 /*
819 * 1. Setup the "listening" UDP socket
820 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100821 mbedtls_printf(" . Bind on UDP/%s/%s ...",
822 opt.listen_addr, opt.listen_port);
823 fflush(stdout);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200824
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100825 if ((ret = mbedtls_net_bind(&listen_fd, opt.listen_addr, opt.listen_port,
826 MBEDTLS_NET_PROTO_UDP)) != 0) {
827 mbedtls_printf(" failed\n ! mbedtls_net_bind returned %d\n\n", ret);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200828 goto exit;
829 }
830
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100831 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200832
833 /*
834 * 2. Wait until a client connects
835 */
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200836accept:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100837 mbedtls_net_free(&client_fd);
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200838
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100839 mbedtls_printf(" . Waiting for a remote connection ...");
840 fflush(stdout);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200841
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100842 if ((ret = mbedtls_net_accept(&listen_fd, &client_fd,
843 NULL, 0, NULL)) != 0) {
844 mbedtls_printf(" failed\n ! mbedtls_net_accept returned %d\n\n", ret);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200845 goto exit;
846 }
847
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100848 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200849
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200850 /*
851 * 3. Forward packets forever (kill the process to terminate it)
852 */
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200853 clear_pending();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100854 memset(held, 0, sizeof(held));
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200855
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200856 nb_fds = client_fd.fd;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100857 if (nb_fds < server_fd.fd) {
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200858 nb_fds = server_fd.fd;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100859 }
860 if (nb_fds < listen_fd.fd) {
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200861 nb_fds = listen_fd.fd;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100862 }
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200863 ++nb_fds;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200864
Hanno Becker0cc77742017-10-31 14:10:07 +0000865#if defined(MBEDTLS_TIMING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100866 if (opt.pack > 0) {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100867 outbuf[0].ctx = &server_fd;
868 outbuf[0].description = "S <- C";
869 outbuf[0].num_datagrams = 0;
870 outbuf[0].len = 0;
871
872 outbuf[1].ctx = &client_fd;
873 outbuf[1].description = "S -> C";
874 outbuf[1].num_datagrams = 0;
875 outbuf[1].len = 0;
876 }
Hanno Becker0cc77742017-10-31 14:10:07 +0000877#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100878
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100879 while (1) {
Hanno Becker77abef52017-11-02 10:50:28 +0000880#if defined(MBEDTLS_TIMING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100881 if (opt.pack > 0) {
Hanno Becker77abef52017-11-02 10:50:28 +0000882 unsigned max_wait_server, max_wait_client, max_wait;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100883 max_wait_server = ctx_buffer_time_remaining(&outbuf[0]);
884 max_wait_client = ctx_buffer_time_remaining(&outbuf[1]);
Hanno Becker77abef52017-11-02 10:50:28 +0000885
886 max_wait = (unsigned) -1;
887
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100888 if (max_wait_server == 0) {
889 ctx_buffer_flush(&outbuf[0]);
890 } else {
Hanno Becker77abef52017-11-02 10:50:28 +0000891 max_wait = max_wait_server;
Hanno Becker77abef52017-11-02 10:50:28 +0000892 }
893
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100894 if (max_wait_client == 0) {
895 ctx_buffer_flush(&outbuf[1]);
896 } else {
897 if (max_wait_client < max_wait) {
898 max_wait = max_wait_client;
899 }
900 }
901
902 if (max_wait != (unsigned) -1) {
Hanno Becker77abef52017-11-02 10:50:28 +0000903 tm.tv_sec = max_wait / 1000;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100904 tm.tv_usec = (max_wait % 1000) * 1000;
Hanno Becker77abef52017-11-02 10:50:28 +0000905
906 tm_ptr = &tm;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100907 } else {
Hanno Becker77abef52017-11-02 10:50:28 +0000908 tm_ptr = NULL;
909 }
910 }
911#endif /* MBEDTLS_TIMING_C */
912
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100913 FD_ZERO(&read_fds);
914 FD_SET(server_fd.fd, &read_fds);
915 FD_SET(client_fd.fd, &read_fds);
916 FD_SET(listen_fd.fd, &read_fds);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200917
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100918 if ((ret = select(nb_fds, &read_fds, NULL, NULL, tm_ptr)) < 0) {
919 perror("select");
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200920 goto exit;
921 }
922
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100923 if (FD_ISSET(listen_fd.fd, &read_fds)) {
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200924 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200925 }
926
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100927 if (FD_ISSET(client_fd.fd, &read_fds)) {
928 if ((ret = handle_message("S <- C",
929 &server_fd, &client_fd)) != 0) {
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200930 goto accept;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100931 }
932 }
933
934 if (FD_ISSET(server_fd.fd, &read_fds)) {
935 if ((ret = handle_message("S -> C",
936 &client_fd, &server_fd)) != 0) {
937 goto accept;
938 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200939 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100940
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200941 }
942
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100943 exit_code = MBEDTLS_EXIT_SUCCESS;
944
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200945exit:
946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947#ifdef MBEDTLS_ERROR_C
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100948 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200949 char error_buf[100];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100950 mbedtls_strerror(ret, error_buf, 100);
951 mbedtls_printf("Last error was: -0x%04X - %s\n\n", (unsigned int) -ret, error_buf);
952 fflush(stdout);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200953 }
954#endif
955
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100956 for (delay_idx = 0; delay_idx < MAX_DELAYED_HS; delay_idx++) {
957 mbedtls_free(opt.delay_cli[delay_idx]);
958 mbedtls_free(opt.delay_srv[delay_idx]);
Hanno Becker01ea7782018-08-17 13:33:41 +0100959 }
960
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100961 mbedtls_net_free(&client_fd);
962 mbedtls_net_free(&server_fd);
963 mbedtls_net_free(&listen_fd);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200964
965#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100966 mbedtls_printf(" Press Enter to exit this program.\n");
967 fflush(stdout); getchar();
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200968#endif
969
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100970 mbedtls_exit(exit_code);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200971}
972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973#endif /* MBEDTLS_NET_C */