blob: 41a90a9ada28525ab391f0419832815b5113962f [file] [log] [blame]
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001/*
2 * UDP proxy: emulate an unreliable UDP connexion for DTLS testing
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020018 */
19
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020020/*
21 * Warning: this is an internal utility program we use for tests.
22 * It does break some abstractions from the NET layer, and is thus NOT an
23 * example of good general usage.
24 */
25
Mateusz Starzyk6c2e9b62021-05-19 17:54:54 +020026#define MBEDTLS_ALLOW_PRIVATE_ACCESS
27
Bence Szépkútic662b362021-05-27 11:25:03 +020028#include "mbedtls/build_info.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/platform.h"
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000032#else
Simon Butcherd3138c32016-04-27 01:26:50 +010033#include <stdio.h>
34#include <stdlib.h>
David Horstmann5b9cb9e2021-11-29 17:28:13 +000035#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherd3138c32016-04-27 01:26:50 +010036#include <time.h>
Andres Amaya Garcia80081a62018-04-29 21:58:53 +010037#define mbedtls_time time
38#define mbedtls_time_t time_t
David Horstmann5b9cb9e2021-11-29 17:28:13 +000039#endif
Andres Amaya Garcia80081a62018-04-29 21:58:53 +010040#define mbedtls_printf printf
Hanno Becker01ea7782018-08-17 13:33:41 +010041#define mbedtls_calloc calloc
42#define mbedtls_free free
k-stachowiak776521a2019-05-23 09:46:47 +020043#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010044#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia80081a62018-04-29 21:58:53 +010045#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
46#endif /* MBEDTLS_PLATFORM_C */
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if !defined(MBEDTLS_NET_C)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020049int main( void )
50{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051 mbedtls_printf( "MBEDTLS_NET_C not defined.\n" );
k-stachowiak776521a2019-05-23 09:46:47 +020052 mbedtls_exit( 0 );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020053}
54#else
55
Andres AG788aa4a2016-09-14 14:32:09 +010056#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/error.h"
58#include "mbedtls/ssl.h"
Hanno Becker0cc77742017-10-31 14:10:07 +000059#include "mbedtls/timing.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020060
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +000061#include <string.h>
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020062
63/* For select() */
64#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
65 !defined(EFI32)
66#include <winsock2.h>
67#include <windows.h>
68#if defined(_MSC_VER)
69#if defined(_WIN32_WCE)
70#pragma comment( lib, "ws2.lib" )
71#else
72#pragma comment( lib, "ws2_32.lib" )
73#endif
74#endif /* _MSC_VER */
75#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Andrzej Kurek6056e7a2022-03-02 12:01:10 -050076#if defined(MBEDTLS_HAVE_TIME)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020077#include <sys/time.h>
Andrzej Kurek6056e7a2022-03-02 12:01:10 -050078#endif
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020079#include <sys/types.h>
80#include <unistd.h>
81#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
82
Manuel Pégourié-Gonnardb46780e2014-09-23 12:17:30 +020083#define MAX_MSG_SIZE 16384 + 2048 /* max record/datagram size */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020084
85#define DFL_SERVER_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020086#define DFL_SERVER_PORT "4433"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020087#define DFL_LISTEN_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020088#define DFL_LISTEN_PORT "5556"
Hanno Becker1dd62ea2017-05-22 14:30:59 +010089#define DFL_PACK 0
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020090
Hanno Becker0cc77742017-10-31 14:10:07 +000091#if defined(MBEDTLS_TIMING_C)
92#define USAGE_PACK \
93 " pack=%%d default: 0 (don't pack)\n" \
94 " options: t > 0 (pack for t milliseconds)\n"
95#else
96#define USAGE_PACK
97#endif
98
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020099#define USAGE \
100 "\n usage: udp_proxy param=<>...\n" \
101 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200102 " server_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200103 " server_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200104 " listen_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200105 " listen_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200106 "\n" \
107 " duplicate=%%d default: 0 (no duplication)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200108 " duplicate about 1:N packets randomly\n" \
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200109 " delay=%%d default: 0 (no delayed packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200110 " delay about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200111 " delay_ccs=0/1 default: 0 (don't delay ChangeCipherSpec)\n" \
Hanno Becker01ea7782018-08-17 13:33:41 +0100112 " delay_cli=%%s Handshake message from client that should be\n"\
113 " delayed. Possible values are 'ClientHello',\n" \
114 " 'Certificate', 'CertificateVerify', and\n" \
115 " 'ClientKeyExchange'.\n" \
116 " May be used multiple times, even for the same\n"\
117 " message, in which case the respective message\n"\
118 " gets delayed multiple times.\n" \
119 " delay_srv=%%s Handshake message from server that should be\n"\
120 " delayed. Possible values are 'HelloRequest',\n"\
121 " 'ServerHello', 'ServerHelloDone', 'Certificate'\n"\
122 " 'ServerKeyExchange', 'NewSessionTicket',\n"\
123 " 'HelloVerifyRequest' and ''CertificateRequest'.\n"\
124 " May be used multiple times, even for the same\n"\
125 " message, in which case the respective message\n"\
126 " gets delayed multiple times.\n" \
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200127 " drop=%%d default: 0 (no dropped packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200128 " drop about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200129 " mtu=%%d default: 0 (unlimited)\n" \
130 " drop packets larger than N bytes\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200131 " bad_ad=0/1 default: 0 (don't add bad ApplicationData)\n" \
Hanno Becker98aaf252019-05-24 10:07:42 +0100132 " bad_cid=%%d default: 0 (don't corrupt Connection IDs)\n" \
133 " duplicate 1:N packets containing a CID,\n" \
134 " modifying CID in first instance of the packet.\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200135 " protect_hvr=0/1 default: 0 (don't protect HelloVerifyRequest)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000136 " protect_len=%%d default: (don't protect packets of this size)\n" \
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100137 " inject_clihlo=0/1 default: 0 (don't inject fake ClientHello)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200138 "\n" \
139 " seed=%%d default: (use current time)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000140 USAGE_PACK \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200141 "\n"
142
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200143/*
144 * global options
145 */
Hanno Becker01ea7782018-08-17 13:33:41 +0100146
147#define MAX_DELAYED_HS 10
148
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200149static struct options
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200150{
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200151 const char *server_addr; /* address to forward packets to */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200152 const char *server_port; /* port to forward packets to */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200153 const char *listen_addr; /* address for accepting client connections */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200154 const char *listen_port; /* port for accepting client connections */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200155
156 int duplicate; /* duplicate 1 in N packets (none if 0) */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200157 int delay; /* delay 1 packet in N (none if 0) */
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200158 int delay_ccs; /* delay ChangeCipherSpec */
Hanno Becker01ea7782018-08-17 13:33:41 +0100159 char* delay_cli[MAX_DELAYED_HS]; /* handshake types of messages from
Hanno Becker41038102018-08-28 11:15:32 +0100160 * client that should be delayed. */
Hanno Becker01ea7782018-08-17 13:33:41 +0100161 uint8_t delay_cli_cnt; /* Number of entries in delay_cli. */
162 char* delay_srv[MAX_DELAYED_HS]; /* handshake types of messages from
Hanno Becker41038102018-08-28 11:15:32 +0100163 * server that should be delayed. */
Hanno Becker01ea7782018-08-17 13:33:41 +0100164 uint8_t delay_srv_cnt; /* Number of entries in delay_srv. */
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200165 int drop; /* drop 1 packet in N (none if 0) */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200166 int mtu; /* drop packets larger than this */
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200167 int bad_ad; /* inject corrupted ApplicationData record */
Hanno Becker98aaf252019-05-24 10:07:42 +0100168 unsigned bad_cid; /* inject corrupted CID record */
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200169 int protect_hvr; /* never drop or delay HelloVerifyRequest */
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200170 int protect_len; /* never drop/delay packet of the given size*/
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100171 int inject_clihlo; /* inject fake ClientHello after handshake */
Hanno Becker77abef52017-11-02 10:50:28 +0000172 unsigned pack; /* merge packets into single datagram for
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100173 * at most \c merge milliseconds if > 0 */
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200174 unsigned int seed; /* seed for "random" events */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200175} opt;
176
177static void exit_usage( const char *name, const char *value )
178{
179 if( value == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 mbedtls_printf( " unknown option or missing value: %s\n", name );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200181 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 mbedtls_printf( " option %s: illegal value: %s\n", name, value );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 mbedtls_printf( USAGE );
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200185 mbedtls_exit( 1 );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200186}
187
188static void get_options( int argc, char *argv[] )
189{
190 int i;
191 char *p, *q;
192
193 opt.server_addr = DFL_SERVER_ADDR;
194 opt.server_port = DFL_SERVER_PORT;
195 opt.listen_addr = DFL_LISTEN_ADDR;
196 opt.listen_port = DFL_LISTEN_PORT;
Hanno Becker211f44c2017-10-31 14:08:10 +0000197 opt.pack = DFL_PACK;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200198 /* Other members default to 0 */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200199
Hanno Becker01ea7782018-08-17 13:33:41 +0100200 opt.delay_cli_cnt = 0;
201 opt.delay_srv_cnt = 0;
202 memset( opt.delay_cli, 0, sizeof( opt.delay_cli ) );
203 memset( opt.delay_srv, 0, sizeof( opt.delay_srv ) );
204
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200205 for( i = 1; i < argc; i++ )
206 {
207 p = argv[i];
208 if( ( q = strchr( p, '=' ) ) == NULL )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200209 exit_usage( p, NULL );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200210 *q++ = '\0';
211
212 if( strcmp( p, "server_addr" ) == 0 )
213 opt.server_addr = q;
214 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200215 opt.server_port = q;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200216 else if( strcmp( p, "listen_addr" ) == 0 )
217 opt.listen_addr = q;
218 else if( strcmp( p, "listen_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200219 opt.listen_port = q;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200220 else if( strcmp( p, "duplicate" ) == 0 )
221 {
222 opt.duplicate = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200223 if( opt.duplicate < 0 || opt.duplicate > 20 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200224 exit_usage( p, q );
225 }
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200226 else if( strcmp( p, "delay" ) == 0 )
227 {
228 opt.delay = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200229 if( opt.delay < 0 || opt.delay > 20 || opt.delay == 1 )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200230 exit_usage( p, q );
231 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200232 else if( strcmp( p, "delay_ccs" ) == 0 )
233 {
234 opt.delay_ccs = atoi( q );
235 if( opt.delay_ccs < 0 || opt.delay_ccs > 1 )
236 exit_usage( p, q );
237 }
Hanno Becker01ea7782018-08-17 13:33:41 +0100238 else if( strcmp( p, "delay_cli" ) == 0 ||
239 strcmp( p, "delay_srv" ) == 0 )
240 {
241 uint8_t *delay_cnt;
242 char **delay_list;
243 size_t len;
244 char *buf;
245
246 if( strcmp( p, "delay_cli" ) == 0 )
247 {
248 delay_cnt = &opt.delay_cli_cnt;
249 delay_list = opt.delay_cli;
250 }
251 else
252 {
253 delay_cnt = &opt.delay_srv_cnt;
254 delay_list = opt.delay_srv;
255 }
256
257 if( *delay_cnt == MAX_DELAYED_HS )
258 {
Hanno Beckerf34a4c12018-08-28 17:22:26 +0100259 mbedtls_printf( " too many uses of %s: only %d allowed\n",
260 p, MAX_DELAYED_HS );
Hanno Becker01ea7782018-08-17 13:33:41 +0100261 exit_usage( p, NULL );
262 }
263
264 len = strlen( q );
265 buf = mbedtls_calloc( 1, len + 1 );
266 if( buf == NULL )
267 {
268 mbedtls_printf( " Allocation failure\n" );
269 exit( 1 );
270 }
271 memcpy( buf, q, len + 1 );
272
273 delay_list[ (*delay_cnt)++ ] = buf;
274 }
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200275 else if( strcmp( p, "drop" ) == 0 )
276 {
277 opt.drop = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200278 if( opt.drop < 0 || opt.drop > 20 || opt.drop == 1 )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200279 exit_usage( p, q );
280 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100281 else if( strcmp( p, "pack" ) == 0 )
282 {
Hanno Becker0cc77742017-10-31 14:10:07 +0000283#if defined(MBEDTLS_TIMING_C)
Hanno Becker77abef52017-11-02 10:50:28 +0000284 opt.pack = (unsigned) atoi( q );
Hanno Becker0cc77742017-10-31 14:10:07 +0000285#else
286 mbedtls_printf( " option pack only defined if MBEDTLS_TIMING_C is enabled\n" );
287 exit( 1 );
288#endif
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100289 }
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200290 else if( strcmp( p, "mtu" ) == 0 )
291 {
292 opt.mtu = atoi( q );
293 if( opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE )
294 exit_usage( p, q );
295 }
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200296 else if( strcmp( p, "bad_ad" ) == 0 )
297 {
298 opt.bad_ad = atoi( q );
299 if( opt.bad_ad < 0 || opt.bad_ad > 1 )
300 exit_usage( p, q );
301 }
Hanno Becker98aaf252019-05-24 10:07:42 +0100302#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
303 else if( strcmp( p, "bad_cid" ) == 0 )
304 {
305 opt.bad_cid = (unsigned) atoi( q );
306 }
307#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200308 else if( strcmp( p, "protect_hvr" ) == 0 )
309 {
310 opt.protect_hvr = atoi( q );
311 if( opt.protect_hvr < 0 || opt.protect_hvr > 1 )
312 exit_usage( p, q );
313 }
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200314 else if( strcmp( p, "protect_len" ) == 0 )
315 {
316 opt.protect_len = atoi( q );
317 if( opt.protect_len < 0 )
318 exit_usage( p, q );
319 }
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100320 else if( strcmp( p, "inject_clihlo" ) == 0 )
321 {
322 opt.inject_clihlo = atoi( q );
323 if( opt.inject_clihlo < 0 || opt.inject_clihlo > 1 )
324 exit_usage( p, q );
325 }
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200326 else if( strcmp( p, "seed" ) == 0 )
327 {
328 opt.seed = atoi( q );
329 if( opt.seed == 0 )
330 exit_usage( p, q );
331 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200332 else
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200333 exit_usage( p, NULL );
334 }
335}
336
337static const char *msg_type( unsigned char *msg, size_t len )
338{
339 if( len < 1 ) return( "Invalid" );
340 switch( msg[0] )
341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" );
343 case MBEDTLS_SSL_MSG_ALERT: return( "Alert" );
344 case MBEDTLS_SSL_MSG_APPLICATION_DATA: return( "ApplicationData" );
Hanno Becker31f6e372019-05-08 15:36:31 +0100345 case MBEDTLS_SSL_MSG_CID: return( "CID" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 case MBEDTLS_SSL_MSG_HANDSHAKE: break; /* See below */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200347 default: return( "Unknown" );
348 }
349
Manuel Pégourié-Gonnard8cc7e032014-09-25 12:59:05 +0200350 if( len < 13 + 12 ) return( "Invalid handshake" );
351
352 /*
353 * Our handshake message are less than 2^16 bytes long, so they should
354 * have 0 as the first byte of length, frag_offset and frag_length.
355 * Otherwise, assume they are encrypted.
356 */
357 if( msg[14] || msg[19] || msg[22] ) return( "Encrypted handshake" );
358
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200359 switch( msg[13] )
360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 case MBEDTLS_SSL_HS_HELLO_REQUEST: return( "HelloRequest" );
362 case MBEDTLS_SSL_HS_CLIENT_HELLO: return( "ClientHello" );
363 case MBEDTLS_SSL_HS_SERVER_HELLO: return( "ServerHello" );
364 case MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" );
365 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" );
366 case MBEDTLS_SSL_HS_CERTIFICATE: return( "Certificate" );
367 case MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" );
368 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" );
369 case MBEDTLS_SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" );
370 case MBEDTLS_SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" );
371 case MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" );
372 case MBEDTLS_SSL_HS_FINISHED: return( "Finished" );
Manuel Pégourié-Gonnardbc010a02014-09-20 12:40:51 +0200373 default: return( "Unknown handshake" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200374 }
375}
376
Hanno Becker0cc77742017-10-31 14:10:07 +0000377#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200378/* Return elapsed time in milliseconds since the first call */
Hanno Becker77abef52017-11-02 10:50:28 +0000379static unsigned ellapsed_time( void )
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200380{
Hanno Becker92474da2017-10-31 14:09:30 +0000381 static int initialized = 0;
382 static struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200383
Hanno Becker92474da2017-10-31 14:09:30 +0000384 if( initialized == 0 )
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200385 {
Hanno Becker92474da2017-10-31 14:09:30 +0000386 (void) mbedtls_timing_get_timer( &hires, 1 );
387 initialized = 1;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200388 return( 0 );
389 }
390
Hanno Becker92474da2017-10-31 14:09:30 +0000391 return( mbedtls_timing_get_timer( &hires, 0 ) );
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200392}
393
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200394typedef struct
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200395{
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100396 mbedtls_net_context *ctx;
397
398 const char *description;
399
Hanno Becker77abef52017-11-02 10:50:28 +0000400 unsigned packet_lifetime;
401 unsigned num_datagrams;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100402
403 unsigned char data[MAX_MSG_SIZE];
Hanno Beckera5e68972017-12-06 08:35:02 +0000404 size_t len;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100405
406} ctx_buffer;
407
408static ctx_buffer outbuf[2];
409
410static int ctx_buffer_flush( ctx_buffer *buf )
411{
412 int ret;
413
Hanno Becker77abef52017-11-02 10:50:28 +0000414 mbedtls_printf( " %05u flush %s: %u bytes, %u datagrams, last %u ms\n",
415 ellapsed_time(), buf->description,
Hanno Beckera5e68972017-12-06 08:35:02 +0000416 (unsigned) buf->len, buf->num_datagrams,
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100417 ellapsed_time() - buf->packet_lifetime );
418
419 ret = mbedtls_net_send( buf->ctx, buf->data, buf->len );
420
421 buf->len = 0;
422 buf->num_datagrams = 0;
423
424 return( ret );
425}
426
Hanno Becker77abef52017-11-02 10:50:28 +0000427static unsigned ctx_buffer_time_remaining( ctx_buffer *buf )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100428{
Hanno Becker77abef52017-11-02 10:50:28 +0000429 unsigned const cur_time = ellapsed_time();
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100430
Hanno Becker77abef52017-11-02 10:50:28 +0000431 if( buf->num_datagrams == 0 )
432 return( (unsigned) -1 );
433
434 if( cur_time - buf->packet_lifetime >= opt.pack )
435 return( 0 );
436
437 return( opt.pack - ( cur_time - buf->packet_lifetime ) );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100438}
439
440static int ctx_buffer_append( ctx_buffer *buf,
441 const unsigned char * data,
442 size_t len )
443{
444 int ret;
445
Hanno Beckera5e68972017-12-06 08:35:02 +0000446 if( len > (size_t) INT_MAX )
447 return( -1 );
448
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100449 if( len > sizeof( buf->data ) )
450 {
Hanno Becker77abef52017-11-02 10:50:28 +0000451 mbedtls_printf( " ! buffer size %u too large (max %u)\n",
452 (unsigned) len, (unsigned) sizeof( buf->data ) );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100453 return( -1 );
454 }
455
456 if( sizeof( buf->data ) - buf->len < len )
457 {
458 if( ( ret = ctx_buffer_flush( buf ) ) <= 0 )
Hanno Becker31f6e372019-05-08 15:36:31 +0100459 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200460 mbedtls_printf( "ctx_buffer_flush failed with -%#04x", (unsigned int) -ret );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100461 return( ret );
Hanno Becker31f6e372019-05-08 15:36:31 +0100462 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100463 }
464
465 memcpy( buf->data + buf->len, data, len );
466
467 buf->len += len;
468 if( ++buf->num_datagrams == 1 )
469 buf->packet_lifetime = ellapsed_time();
470
Hanno Beckera5e68972017-12-06 08:35:02 +0000471 return( (int) len );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100472}
Hanno Becker77abef52017-11-02 10:50:28 +0000473#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100474
475static int dispatch_data( mbedtls_net_context *ctx,
476 const unsigned char * data,
477 size_t len )
478{
Hanno Becker31f6e372019-05-08 15:36:31 +0100479 int ret;
Hanno Becker77abef52017-11-02 10:50:28 +0000480#if defined(MBEDTLS_TIMING_C)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100481 ctx_buffer *buf = NULL;
Hanno Becker77abef52017-11-02 10:50:28 +0000482 if( opt.pack > 0 )
483 {
484 if( outbuf[0].ctx == ctx )
485 buf = &outbuf[0];
486 else if( outbuf[1].ctx == ctx )
487 buf = &outbuf[1];
Hanno Becker0cc77742017-10-31 14:10:07 +0000488
Hanno Becker77abef52017-11-02 10:50:28 +0000489 if( buf == NULL )
490 return( -1 );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100491
Hanno Becker77abef52017-11-02 10:50:28 +0000492 return( ctx_buffer_append( buf, data, len ) );
493 }
494#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100495
Hanno Becker31f6e372019-05-08 15:36:31 +0100496 ret = mbedtls_net_send( ctx, data, len );
497 if( ret < 0 )
498 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200499 mbedtls_printf( "net_send returned -%#04x\n", (unsigned int) -ret );
Hanno Becker31f6e372019-05-08 15:36:31 +0100500 }
501 return( ret );
Hanno Becker0cc77742017-10-31 14:10:07 +0000502}
503
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100504typedef struct
505{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200506 mbedtls_net_context *dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200507 const char *way;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200508 const char *type;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200509 unsigned len;
510 unsigned char buf[MAX_MSG_SIZE];
511} packet;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200512
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200513/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */
514void print_packet( const packet *p, const char *why )
515{
Hanno Becker0cc77742017-10-31 14:10:07 +0000516#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200517 if( why == NULL )
Hanno Becker77abef52017-11-02 10:50:28 +0000518 mbedtls_printf( " %05u dispatch %s %s (%u bytes)\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200519 ellapsed_time(), p->way, p->type, p->len );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200520 else
Hanno Becker77abef52017-11-02 10:50:28 +0000521 mbedtls_printf( " %05u dispatch %s %s (%u bytes): %s\n",
Hanno Becker0cc77742017-10-31 14:10:07 +0000522 ellapsed_time(), p->way, p->type, p->len, why );
523#else
524 if( why == NULL )
525 mbedtls_printf( " dispatch %s %s (%u bytes)\n",
526 p->way, p->type, p->len );
527 else
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100528 mbedtls_printf( " dispatch %s %s (%u bytes): %s\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200529 p->way, p->type, p->len, why );
Hanno Becker0cc77742017-10-31 14:10:07 +0000530#endif
531
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200532 fflush( stdout );
533}
534
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100535/*
536 * In order to test the server's behaviour when receiving a ClientHello after
537 * the connection is established (this could be a hard reset from the client,
538 * but the server must not drop the existing connection before establishing
539 * client reachability, see RFC 6347 Section 4.2.8), we memorize the first
540 * ClientHello we see (which can't have a cookie), then replay it after the
541 * first ApplicationData record - then we're done.
542 *
543 * This is controlled by the inject_clihlo option.
544 *
545 * We want an explicit state and a place to store the packet.
546 */
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200547typedef enum {
548 ICH_INIT, /* haven't seen the first ClientHello yet */
549 ICH_CACHED, /* cached the initial ClientHello */
550 ICH_INJECTED, /* ClientHello already injected, done */
551} inject_clihlo_state_t;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100552
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200553static inject_clihlo_state_t inject_clihlo_state;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100554static packet initial_clihlo;
555
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200556int send_packet( const packet *p, const char *why )
557{
558 int ret;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200559 mbedtls_net_context *dst = p->dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200560
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100561 /* save initial ClientHello? */
562 if( opt.inject_clihlo != 0 &&
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200563 inject_clihlo_state == ICH_INIT &&
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100564 strcmp( p->type, "ClientHello" ) == 0 )
565 {
566 memcpy( &initial_clihlo, p, sizeof( packet ) );
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200567 inject_clihlo_state = ICH_CACHED;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100568 }
569
Hanno Becker98aaf252019-05-24 10:07:42 +0100570 /* insert corrupted CID record? */
571 if( opt.bad_cid != 0 &&
572 strcmp( p->type, "CID" ) == 0 &&
573 ( rand() % opt.bad_cid ) == 0 )
574 {
575 unsigned char buf[MAX_MSG_SIZE];
576 memcpy( buf, p->buf, p->len );
577
578 /* The CID resides at offset 11 in the DTLS record header. */
579 buf[11] ^= 1;
580 print_packet( p, "modified CID" );
581
582 if( ( ret = dispatch_data( dst, buf, p->len ) ) <= 0 )
583 {
584 mbedtls_printf( " ! dispatch returned %d\n", ret );
585 return( ret );
586 }
587 }
588
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200589 /* insert corrupted ApplicationData record? */
590 if( opt.bad_ad &&
591 strcmp( p->type, "ApplicationData" ) == 0 )
592 {
593 unsigned char buf[MAX_MSG_SIZE];
594 memcpy( buf, p->buf, p->len );
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200595
Hanno Beckerfbb0b702017-05-26 16:55:07 +0100596 if( p->len <= 13 )
597 {
598 mbedtls_printf( " ! can't corrupt empty AD record" );
599 }
600 else
601 {
602 ++buf[13];
603 print_packet( p, "corrupted" );
604 }
605
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100606 if( ( ret = dispatch_data( dst, buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200607 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100608 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200609 return( ret );
610 }
611 }
612
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200613 print_packet( p, why );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100614 if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200615 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100616 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200617 return( ret );
618 }
619
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200620 /* Don't duplicate Application Data, only handshake covered */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200621 if( opt.duplicate != 0 &&
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200622 strcmp( p->type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200623 rand() % opt.duplicate == 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200624 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200625 print_packet( p, "duplicated" );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200626
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100627 if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200628 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100629 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200630 return( ret );
631 }
632 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200633
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100634 /* Inject ClientHello after first ApplicationData */
635 if( opt.inject_clihlo != 0 &&
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200636 inject_clihlo_state == ICH_CACHED &&
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100637 strcmp( p->type, "ApplicationData" ) == 0 )
638 {
639 print_packet( &initial_clihlo, "injected" );
640
641 if( ( ret = dispatch_data( dst, initial_clihlo.buf,
642 initial_clihlo.len ) ) <= 0 )
643 {
644 mbedtls_printf( " ! dispatch returned %d\n", ret );
645 return( ret );
646 }
647
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200648 inject_clihlo_state = ICH_INJECTED;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100649 }
650
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200651 return( 0 );
652}
653
Hanno Becker101bcba2018-08-21 16:39:51 +0100654#define MAX_DELAYED_MSG 5
655static size_t prev_len;
656static packet prev[MAX_DELAYED_MSG];
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200657
658void clear_pending( void )
659{
Hanno Becker12b72c12018-08-23 13:15:36 +0100660 memset( &prev, 0, sizeof( prev ) );
Hanno Becker101bcba2018-08-21 16:39:51 +0100661 prev_len = 0;
662}
663
664void delay_packet( packet *delay )
665{
666 if( prev_len == MAX_DELAYED_MSG )
667 return;
668
669 memcpy( &prev[prev_len++], delay, sizeof( packet ) );
670}
671
672int send_delayed()
673{
674 uint8_t offset;
675 int ret;
676 for( offset = 0; offset < prev_len; offset++ )
677 {
678 ret = send_packet( &prev[offset], "delayed" );
679 if( ret != 0 )
680 return( ret );
681 }
682
683 clear_pending();
684 return( 0 );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200685}
686
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200687/*
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200688 * Avoid dropping or delaying a packet that was already dropped or delayed
689 * ("held") twice: this only results in uninteresting timeouts. We can't rely
690 * on type to identify packets, since during renegotiation they're all
691 * encrypted. So, rely on size mod 2048 (which is usually just size).
692 *
693 * We only hold packets at the level of entire datagrams, not at the level
Hanno Becker961e6772019-06-04 13:04:28 +0100694 * of records. In particular, if the peer changes the way it packs multiple
695 * records into a single datagram, we don't necessarily count the number of
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200696 * times a record has been held correctly. However, the only known reason
Hanno Becker961e6772019-06-04 13:04:28 +0100697 * why a peer would change datagram packing is disabling the latter on
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200698 * retransmission, in which case we'd hold involved records at most
699 * HOLD_MAX + 1 times.
700 */
701static unsigned char held[2048] = { 0 };
702#define HOLD_MAX 2
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200703
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200704int handle_message( const char *way,
705 mbedtls_net_context *dst,
706 mbedtls_net_context *src )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200707{
708 int ret;
709 packet cur;
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200710 size_t id;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200711
Hanno Becker01ea7782018-08-17 13:33:41 +0100712 uint8_t delay_idx;
713 char ** delay_list;
714 uint8_t delay_list_len;
715
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +0200716 /* receive packet */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200717 if( ( ret = mbedtls_net_recv( src, cur.buf, sizeof( cur.buf ) ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 mbedtls_printf( " ! mbedtls_net_recv returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200720 return( ret );
721 }
722
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200723 cur.len = ret;
724 cur.type = msg_type( cur.buf, cur.len );
725 cur.way = way;
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200726 cur.dst = dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200727 print_packet( &cur, NULL );
728
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200729 id = cur.len % sizeof( held );
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200730
Hanno Becker01ea7782018-08-17 13:33:41 +0100731 if( strcmp( way, "S <- C" ) == 0 )
732 {
733 delay_list = opt.delay_cli;
734 delay_list_len = opt.delay_cli_cnt;
735 }
736 else
737 {
738 delay_list = opt.delay_srv;
739 delay_list_len = opt.delay_srv_cnt;
740 }
Hanno Beckercf469452018-08-28 10:09:47 +0100741
Hanno Becker01ea7782018-08-17 13:33:41 +0100742 /* Check if message type is in the list of messages
743 * that should be delayed */
744 for( delay_idx = 0; delay_idx < delay_list_len; delay_idx++ )
745 {
746 if( delay_list[ delay_idx ] == NULL )
747 continue;
748
749 if( strcmp( delay_list[ delay_idx ], cur.type ) == 0 )
750 {
751 /* Delay message */
Hanno Becker101bcba2018-08-21 16:39:51 +0100752 delay_packet( &cur );
Hanno Becker01ea7782018-08-17 13:33:41 +0100753
754 /* Remove entry from list */
755 mbedtls_free( delay_list[delay_idx] );
756 delay_list[delay_idx] = NULL;
757
758 return( 0 );
759 }
760 }
761
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200762 /* do we want to drop, delay, or forward it? */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200763 if( ( opt.mtu != 0 &&
764 cur.len > (unsigned) opt.mtu ) ||
765 ( opt.drop != 0 &&
Hanno Becker31f6e372019-05-08 15:36:31 +0100766 strcmp( cur.type, "CID" ) != 0 &&
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200767 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200768 ! ( opt.protect_hvr &&
769 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200770 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200771 held[id] < HOLD_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200772 rand() % opt.drop == 0 ) )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200773 {
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200774 ++held[id];
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200775 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200776 else if( ( opt.delay_ccs == 1 &&
777 strcmp( cur.type, "ChangeCipherSpec" ) == 0 ) ||
778 ( opt.delay != 0 &&
Hanno Becker31f6e372019-05-08 15:36:31 +0100779 strcmp( cur.type, "CID" ) != 0 &&
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200780 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200781 ! ( opt.protect_hvr &&
782 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200783 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200784 held[id] < HOLD_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200785 rand() % opt.delay == 0 ) )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200786 {
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200787 ++held[id];
Hanno Becker101bcba2018-08-21 16:39:51 +0100788 delay_packet( &cur );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200789 }
790 else
791 {
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200792 /* forward and possibly duplicate */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200793 if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 )
794 return( ret );
795
Hanno Becker101bcba2018-08-21 16:39:51 +0100796 /* send previously delayed messages if any */
797 ret = send_delayed();
798 if( ret != 0 )
799 return( ret );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200800 }
801
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200802 return( 0 );
803}
804
805int main( int argc, char *argv[] )
806{
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100807 int ret = 1;
808 int exit_code = MBEDTLS_EXIT_FAILURE;
Hanno Becker01ea7782018-08-17 13:33:41 +0100809 uint8_t delay_idx;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200810
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200811 mbedtls_net_context listen_fd, client_fd, server_fd;
Hanno Becker77abef52017-11-02 10:50:28 +0000812
813#if defined( MBEDTLS_TIMING_C )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100814 struct timeval tm;
Hanno Becker77abef52017-11-02 10:50:28 +0000815#endif
816
817 struct timeval *tm_ptr = NULL;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200818
819 int nb_fds;
820 fd_set read_fds;
821
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200822 mbedtls_net_init( &listen_fd );
823 mbedtls_net_init( &client_fd );
824 mbedtls_net_init( &server_fd );
825
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200826 get_options( argc, argv );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200827
828 /*
829 * Decisions to drop/delay/duplicate packets are pseudo-random: dropping
830 * exactly 1 in N packets would lead to problems when a flight has exactly
831 * N packets: the same packet would be dropped on every resend.
832 *
833 * In order to be able to reproduce problems reliably, the seed may be
834 * specified explicitly.
835 */
836 if( opt.seed == 0 )
837 {
Gilles Peskinee756f642022-04-05 21:39:43 +0200838#if defined(MBEDTLS_HAVE_TIME)
David Horstmann5b9cb9e2021-11-29 17:28:13 +0000839 opt.seed = (unsigned int) mbedtls_time( NULL );
Gilles Peskinee756f642022-04-05 21:39:43 +0200840#else
841 opt.seed = 1;
842#endif /* MBEDTLS_HAVE_TIME */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 mbedtls_printf( " . Pseudo-random seed: %u\n", opt.seed );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200844 }
845
846 srand( opt.seed );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200847
848 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200849 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200850 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200851 mbedtls_printf( " . Connect to server on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200852 opt.server_addr, opt.server_port );
853 fflush( stdout );
854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
856 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200859 goto exit;
860 }
861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200863
864 /*
865 * 1. Setup the "listening" UDP socket
866 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200867 mbedtls_printf( " . Bind on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200868 opt.listen_addr, opt.listen_port );
869 fflush( stdout );
870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 if( ( ret = mbedtls_net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
872 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200875 goto exit;
876 }
877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200879
880 /*
881 * 2. Wait until a client connects
882 */
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200883accept:
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200884 mbedtls_net_free( &client_fd );
885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 mbedtls_printf( " . Waiting for a remote connection ..." );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200887 fflush( stdout );
888
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200889 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200890 NULL, 0, NULL ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 mbedtls_printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200893 goto exit;
894 }
895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200897
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200898 /*
899 * 3. Forward packets forever (kill the process to terminate it)
900 */
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200901 clear_pending();
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200902 memset( held, 0, sizeof( held ) );
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200903
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200904 nb_fds = client_fd.fd;
905 if( nb_fds < server_fd.fd )
906 nb_fds = server_fd.fd;
907 if( nb_fds < listen_fd.fd )
908 nb_fds = listen_fd.fd;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200909 ++nb_fds;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200910
Hanno Becker0cc77742017-10-31 14:10:07 +0000911#if defined(MBEDTLS_TIMING_C)
Hanno Becker211f44c2017-10-31 14:08:10 +0000912 if( opt.pack > 0 )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100913 {
914 outbuf[0].ctx = &server_fd;
915 outbuf[0].description = "S <- C";
916 outbuf[0].num_datagrams = 0;
917 outbuf[0].len = 0;
918
919 outbuf[1].ctx = &client_fd;
920 outbuf[1].description = "S -> C";
921 outbuf[1].num_datagrams = 0;
922 outbuf[1].len = 0;
923 }
Hanno Becker0cc77742017-10-31 14:10:07 +0000924#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100925
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200926 while( 1 )
927 {
Hanno Becker77abef52017-11-02 10:50:28 +0000928#if defined(MBEDTLS_TIMING_C)
929 if( opt.pack > 0 )
930 {
931 unsigned max_wait_server, max_wait_client, max_wait;
932 max_wait_server = ctx_buffer_time_remaining( &outbuf[0] );
933 max_wait_client = ctx_buffer_time_remaining( &outbuf[1] );
934
935 max_wait = (unsigned) -1;
936
937 if( max_wait_server == 0 )
938 ctx_buffer_flush( &outbuf[0] );
939 else
940 max_wait = max_wait_server;
941
942 if( max_wait_client == 0 )
943 ctx_buffer_flush( &outbuf[1] );
944 else
945 {
946 if( max_wait_client < max_wait )
947 max_wait = max_wait_client;
948 }
949
950 if( max_wait != (unsigned) -1 )
951 {
952 tm.tv_sec = max_wait / 1000;
953 tm.tv_usec = ( max_wait % 1000 ) * 1000;
954
955 tm_ptr = &tm;
956 }
957 else
958 {
959 tm_ptr = NULL;
960 }
961 }
962#endif /* MBEDTLS_TIMING_C */
963
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200964 FD_ZERO( &read_fds );
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200965 FD_SET( server_fd.fd, &read_fds );
966 FD_SET( client_fd.fd, &read_fds );
967 FD_SET( listen_fd.fd, &read_fds );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200968
Hanno Becker77abef52017-11-02 10:50:28 +0000969 if( ( ret = select( nb_fds, &read_fds, NULL, NULL, tm_ptr ) ) < 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200970 {
971 perror( "select" );
972 goto exit;
973 }
974
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200975 if( FD_ISSET( listen_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200976 goto accept;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200977
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200978 if( FD_ISSET( client_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200979 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200980 if( ( ret = handle_message( "S <- C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200981 &server_fd, &client_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200982 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200983 }
984
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200985 if( FD_ISSET( server_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200986 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200987 if( ( ret = handle_message( "S -> C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200988 &client_fd, &server_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200989 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200990 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100991
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200992 }
993
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100994 exit_code = MBEDTLS_EXIT_SUCCESS;
995
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200996exit:
997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998#ifdef MBEDTLS_ERROR_C
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100999 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001000 {
1001 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 mbedtls_strerror( ret, error_buf, 100 );
Kenneth Soerensen518d4352020-04-01 17:22:45 +02001003 mbedtls_printf( "Last error was: -0x%04X - %s\n\n", (unsigned int) -ret, error_buf );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001004 fflush( stdout );
1005 }
1006#endif
1007
Hanno Becker01ea7782018-08-17 13:33:41 +01001008 for( delay_idx = 0; delay_idx < MAX_DELAYED_HS; delay_idx++ )
1009 {
Shawn Careyaa13e932021-05-06 15:11:30 -04001010 mbedtls_free( opt.delay_cli[delay_idx] );
1011 mbedtls_free( opt.delay_srv[delay_idx] );
Hanno Becker01ea7782018-08-17 13:33:41 +01001012 }
1013
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001014 mbedtls_net_free( &client_fd );
1015 mbedtls_net_free( &server_fd );
1016 mbedtls_net_free( &listen_fd );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001017
1018#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 mbedtls_printf( " Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001020 fflush( stdout ); getchar();
1021#endif
1022
k-stachowiak776521a2019-05-23 09:46:47 +02001023 mbedtls_exit( exit_code );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001024}
1025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026#endif /* MBEDTLS_NET_C */