blob: e59688ea0a800e471a5b802c0bfeb52327f0899b [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 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 *
Manuel Pégourié-Gonnarde4d48902015-03-06 13:40:52 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020020 */
21
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020022/*
23 * Warning: this is an internal utility program we use for tests.
24 * It does break some abstractions from the NET layer, and is thus NOT an
25 * example of good general usage.
26 */
27
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020030#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020032#endif
33
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/platform.h"
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000036#else
Simon Butcherd3138c32016-04-27 01:26:50 +010037#include <stdio.h>
38#include <stdlib.h>
39#include <time.h>
Andres Amaya Garcia80081a62018-04-29 21:58:53 +010040#define mbedtls_time time
41#define mbedtls_time_t time_t
42#define mbedtls_printf printf
Hanno Becker01ea7782018-08-17 13:33:41 +010043#define mbedtls_calloc calloc
44#define mbedtls_free free
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020045#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010046#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia80081a62018-04-29 21:58:53 +010047#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
48#endif /* MBEDTLS_PLATFORM_C */
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#if !defined(MBEDTLS_NET_C)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020051int main( void )
52{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053 mbedtls_printf( "MBEDTLS_NET_C not defined.\n" );
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020054 mbedtls_exit( 0 );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020055}
56#else
57
Andres AG788aa4a2016-09-14 14:32:09 +010058#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/error.h"
60#include "mbedtls/ssl.h"
Hanno Becker0cc77742017-10-31 14:10:07 +000061#include "mbedtls/timing.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020062
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +000063#include <string.h>
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020064
65/* For select() */
66#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
67 !defined(EFI32)
68#include <winsock2.h>
69#include <windows.h>
70#if defined(_MSC_VER)
71#if defined(_WIN32_WCE)
72#pragma comment( lib, "ws2.lib" )
73#else
74#pragma comment( lib, "ws2_32.lib" )
75#endif
76#endif /* _MSC_VER */
77#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
78#include <sys/time.h>
79#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" \
132 " protect_hvr=0/1 default: 0 (don't protect HelloVerifyRequest)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000133 " protect_len=%%d default: (don't protect packets of this size)\n" \
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100134 " inject_clihlo=0/1 default: 0 (don't inject fake ClientHello)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200135 "\n" \
136 " seed=%%d default: (use current time)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000137 USAGE_PACK \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200138 "\n"
139
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200140/*
141 * global options
142 */
Hanno Becker01ea7782018-08-17 13:33:41 +0100143
144#define MAX_DELAYED_HS 10
145
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200146static struct options
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200147{
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200148 const char *server_addr; /* address to forward packets to */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200149 const char *server_port; /* port to forward packets to */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200150 const char *listen_addr; /* address for accepting client connections */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200151 const char *listen_port; /* port for accepting client connections */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200152
153 int duplicate; /* duplicate 1 in N packets (none if 0) */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200154 int delay; /* delay 1 packet in N (none if 0) */
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200155 int delay_ccs; /* delay ChangeCipherSpec */
Hanno Becker01ea7782018-08-17 13:33:41 +0100156 char* delay_cli[MAX_DELAYED_HS]; /* handshake types of messages from
Hanno Becker41038102018-08-28 11:15:32 +0100157 * client that should be delayed. */
Hanno Becker01ea7782018-08-17 13:33:41 +0100158 uint8_t delay_cli_cnt; /* Number of entries in delay_cli. */
159 char* delay_srv[MAX_DELAYED_HS]; /* handshake types of messages from
Hanno Becker41038102018-08-28 11:15:32 +0100160 * server that should be delayed. */
Hanno Becker01ea7782018-08-17 13:33:41 +0100161 uint8_t delay_srv_cnt; /* Number of entries in delay_srv. */
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200162 int drop; /* drop 1 packet in N (none if 0) */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200163 int mtu; /* drop packets larger than this */
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200164 int bad_ad; /* inject corrupted ApplicationData record */
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200165 int protect_hvr; /* never drop or delay HelloVerifyRequest */
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200166 int protect_len; /* never drop/delay packet of the given size*/
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100167 int inject_clihlo; /* inject fake ClientHello after handshake */
Hanno Becker77abef52017-11-02 10:50:28 +0000168 unsigned pack; /* merge packets into single datagram for
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100169 * at most \c merge milliseconds if > 0 */
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200170 unsigned int seed; /* seed for "random" events */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200171} opt;
172
173static void exit_usage( const char *name, const char *value )
174{
175 if( value == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_printf( " unknown option or missing value: %s\n", name );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200177 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_printf( " option %s: illegal value: %s\n", name, value );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 mbedtls_printf( USAGE );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200181 exit( 1 );
182}
183
184static void get_options( int argc, char *argv[] )
185{
186 int i;
187 char *p, *q;
188
189 opt.server_addr = DFL_SERVER_ADDR;
190 opt.server_port = DFL_SERVER_PORT;
191 opt.listen_addr = DFL_LISTEN_ADDR;
192 opt.listen_port = DFL_LISTEN_PORT;
Hanno Becker211f44c2017-10-31 14:08:10 +0000193 opt.pack = DFL_PACK;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200194 /* Other members default to 0 */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200195
Hanno Becker01ea7782018-08-17 13:33:41 +0100196 opt.delay_cli_cnt = 0;
197 opt.delay_srv_cnt = 0;
198 memset( opt.delay_cli, 0, sizeof( opt.delay_cli ) );
199 memset( opt.delay_srv, 0, sizeof( opt.delay_srv ) );
200
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200201 for( i = 1; i < argc; i++ )
202 {
203 p = argv[i];
204 if( ( q = strchr( p, '=' ) ) == NULL )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200205 exit_usage( p, NULL );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200206 *q++ = '\0';
207
208 if( strcmp( p, "server_addr" ) == 0 )
209 opt.server_addr = q;
210 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200211 opt.server_port = q;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200212 else if( strcmp( p, "listen_addr" ) == 0 )
213 opt.listen_addr = q;
214 else if( strcmp( p, "listen_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200215 opt.listen_port = q;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200216 else if( strcmp( p, "duplicate" ) == 0 )
217 {
218 opt.duplicate = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200219 if( opt.duplicate < 0 || opt.duplicate > 20 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200220 exit_usage( p, q );
221 }
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200222 else if( strcmp( p, "delay" ) == 0 )
223 {
224 opt.delay = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200225 if( opt.delay < 0 || opt.delay > 20 || opt.delay == 1 )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200226 exit_usage( p, q );
227 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200228 else if( strcmp( p, "delay_ccs" ) == 0 )
229 {
230 opt.delay_ccs = atoi( q );
231 if( opt.delay_ccs < 0 || opt.delay_ccs > 1 )
232 exit_usage( p, q );
233 }
Hanno Becker01ea7782018-08-17 13:33:41 +0100234 else if( strcmp( p, "delay_cli" ) == 0 ||
235 strcmp( p, "delay_srv" ) == 0 )
236 {
237 uint8_t *delay_cnt;
238 char **delay_list;
239 size_t len;
240 char *buf;
241
242 if( strcmp( p, "delay_cli" ) == 0 )
243 {
244 delay_cnt = &opt.delay_cli_cnt;
245 delay_list = opt.delay_cli;
246 }
247 else
248 {
249 delay_cnt = &opt.delay_srv_cnt;
250 delay_list = opt.delay_srv;
251 }
252
253 if( *delay_cnt == MAX_DELAYED_HS )
254 {
Hanno Beckerf34a4c12018-08-28 17:22:26 +0100255 mbedtls_printf( " too many uses of %s: only %d allowed\n",
256 p, MAX_DELAYED_HS );
Hanno Becker01ea7782018-08-17 13:33:41 +0100257 exit_usage( p, NULL );
258 }
259
260 len = strlen( q );
261 buf = mbedtls_calloc( 1, len + 1 );
262 if( buf == NULL )
263 {
264 mbedtls_printf( " Allocation failure\n" );
265 exit( 1 );
266 }
267 memcpy( buf, q, len + 1 );
268
269 delay_list[ (*delay_cnt)++ ] = buf;
270 }
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200271 else if( strcmp( p, "drop" ) == 0 )
272 {
273 opt.drop = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200274 if( opt.drop < 0 || opt.drop > 20 || opt.drop == 1 )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200275 exit_usage( p, q );
276 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100277 else if( strcmp( p, "pack" ) == 0 )
278 {
Hanno Becker0cc77742017-10-31 14:10:07 +0000279#if defined(MBEDTLS_TIMING_C)
Hanno Becker77abef52017-11-02 10:50:28 +0000280 opt.pack = (unsigned) atoi( q );
Hanno Becker0cc77742017-10-31 14:10:07 +0000281#else
282 mbedtls_printf( " option pack only defined if MBEDTLS_TIMING_C is enabled\n" );
283 exit( 1 );
284#endif
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100285 }
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200286 else if( strcmp( p, "mtu" ) == 0 )
287 {
288 opt.mtu = atoi( q );
289 if( opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE )
290 exit_usage( p, q );
291 }
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200292 else if( strcmp( p, "bad_ad" ) == 0 )
293 {
294 opt.bad_ad = atoi( q );
295 if( opt.bad_ad < 0 || opt.bad_ad > 1 )
296 exit_usage( p, q );
297 }
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200298 else if( strcmp( p, "protect_hvr" ) == 0 )
299 {
300 opt.protect_hvr = atoi( q );
301 if( opt.protect_hvr < 0 || opt.protect_hvr > 1 )
302 exit_usage( p, q );
303 }
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200304 else if( strcmp( p, "protect_len" ) == 0 )
305 {
306 opt.protect_len = atoi( q );
307 if( opt.protect_len < 0 )
308 exit_usage( p, q );
309 }
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100310 else if( strcmp( p, "inject_clihlo" ) == 0 )
311 {
312 opt.inject_clihlo = atoi( q );
313 if( opt.inject_clihlo < 0 || opt.inject_clihlo > 1 )
314 exit_usage( p, q );
315 }
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200316 else if( strcmp( p, "seed" ) == 0 )
317 {
318 opt.seed = atoi( q );
319 if( opt.seed == 0 )
320 exit_usage( p, q );
321 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200322 else
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200323 exit_usage( p, NULL );
324 }
325}
326
327static const char *msg_type( unsigned char *msg, size_t len )
328{
329 if( len < 1 ) return( "Invalid" );
330 switch( msg[0] )
331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" );
333 case MBEDTLS_SSL_MSG_ALERT: return( "Alert" );
334 case MBEDTLS_SSL_MSG_APPLICATION_DATA: return( "ApplicationData" );
335 case MBEDTLS_SSL_MSG_HANDSHAKE: break; /* See below */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200336 default: return( "Unknown" );
337 }
338
Manuel Pégourié-Gonnard8cc7e032014-09-25 12:59:05 +0200339 if( len < 13 + 12 ) return( "Invalid handshake" );
340
341 /*
342 * Our handshake message are less than 2^16 bytes long, so they should
343 * have 0 as the first byte of length, frag_offset and frag_length.
344 * Otherwise, assume they are encrypted.
345 */
346 if( msg[14] || msg[19] || msg[22] ) return( "Encrypted handshake" );
347
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200348 switch( msg[13] )
349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 case MBEDTLS_SSL_HS_HELLO_REQUEST: return( "HelloRequest" );
351 case MBEDTLS_SSL_HS_CLIENT_HELLO: return( "ClientHello" );
352 case MBEDTLS_SSL_HS_SERVER_HELLO: return( "ServerHello" );
353 case MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" );
354 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" );
355 case MBEDTLS_SSL_HS_CERTIFICATE: return( "Certificate" );
356 case MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" );
357 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" );
358 case MBEDTLS_SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" );
359 case MBEDTLS_SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" );
360 case MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" );
361 case MBEDTLS_SSL_HS_FINISHED: return( "Finished" );
Manuel Pégourié-Gonnardbc010a02014-09-20 12:40:51 +0200362 default: return( "Unknown handshake" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200363 }
364}
365
Hanno Becker0cc77742017-10-31 14:10:07 +0000366#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200367/* Return elapsed time in milliseconds since the first call */
Hanno Becker77abef52017-11-02 10:50:28 +0000368static unsigned ellapsed_time( void )
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200369{
Hanno Becker92474da2017-10-31 14:09:30 +0000370 static int initialized = 0;
371 static struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200372
Hanno Becker92474da2017-10-31 14:09:30 +0000373 if( initialized == 0 )
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200374 {
Hanno Becker92474da2017-10-31 14:09:30 +0000375 (void) mbedtls_timing_get_timer( &hires, 1 );
376 initialized = 1;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200377 return( 0 );
378 }
379
Hanno Becker92474da2017-10-31 14:09:30 +0000380 return( mbedtls_timing_get_timer( &hires, 0 ) );
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200381}
382
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200383typedef struct
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200384{
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100385 mbedtls_net_context *ctx;
386
387 const char *description;
388
Hanno Becker77abef52017-11-02 10:50:28 +0000389 unsigned packet_lifetime;
390 unsigned num_datagrams;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100391
392 unsigned char data[MAX_MSG_SIZE];
Hanno Beckera5e68972017-12-06 08:35:02 +0000393 size_t len;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100394
395} ctx_buffer;
396
397static ctx_buffer outbuf[2];
398
399static int ctx_buffer_flush( ctx_buffer *buf )
400{
401 int ret;
402
Hanno Becker77abef52017-11-02 10:50:28 +0000403 mbedtls_printf( " %05u flush %s: %u bytes, %u datagrams, last %u ms\n",
404 ellapsed_time(), buf->description,
Hanno Beckera5e68972017-12-06 08:35:02 +0000405 (unsigned) buf->len, buf->num_datagrams,
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100406 ellapsed_time() - buf->packet_lifetime );
407
408 ret = mbedtls_net_send( buf->ctx, buf->data, buf->len );
409
410 buf->len = 0;
411 buf->num_datagrams = 0;
412
413 return( ret );
414}
415
Hanno Becker77abef52017-11-02 10:50:28 +0000416static unsigned ctx_buffer_time_remaining( ctx_buffer *buf )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100417{
Hanno Becker77abef52017-11-02 10:50:28 +0000418 unsigned const cur_time = ellapsed_time();
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100419
Hanno Becker77abef52017-11-02 10:50:28 +0000420 if( buf->num_datagrams == 0 )
421 return( (unsigned) -1 );
422
423 if( cur_time - buf->packet_lifetime >= opt.pack )
424 return( 0 );
425
426 return( opt.pack - ( cur_time - buf->packet_lifetime ) );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100427}
428
429static int ctx_buffer_append( ctx_buffer *buf,
430 const unsigned char * data,
431 size_t len )
432{
433 int ret;
434
Hanno Beckera5e68972017-12-06 08:35:02 +0000435 if( len > (size_t) INT_MAX )
436 return( -1 );
437
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100438 if( len > sizeof( buf->data ) )
439 {
Hanno Becker77abef52017-11-02 10:50:28 +0000440 mbedtls_printf( " ! buffer size %u too large (max %u)\n",
441 (unsigned) len, (unsigned) sizeof( buf->data ) );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100442 return( -1 );
443 }
444
445 if( sizeof( buf->data ) - buf->len < len )
446 {
447 if( ( ret = ctx_buffer_flush( buf ) ) <= 0 )
448 return( ret );
449 }
450
451 memcpy( buf->data + buf->len, data, len );
452
453 buf->len += len;
454 if( ++buf->num_datagrams == 1 )
455 buf->packet_lifetime = ellapsed_time();
456
Hanno Beckera5e68972017-12-06 08:35:02 +0000457 return( (int) len );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100458}
Hanno Becker77abef52017-11-02 10:50:28 +0000459#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100460
461static int dispatch_data( mbedtls_net_context *ctx,
462 const unsigned char * data,
463 size_t len )
464{
Hanno Becker77abef52017-11-02 10:50:28 +0000465#if defined(MBEDTLS_TIMING_C)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100466 ctx_buffer *buf = NULL;
Hanno Becker77abef52017-11-02 10:50:28 +0000467 if( opt.pack > 0 )
468 {
469 if( outbuf[0].ctx == ctx )
470 buf = &outbuf[0];
471 else if( outbuf[1].ctx == ctx )
472 buf = &outbuf[1];
Hanno Becker0cc77742017-10-31 14:10:07 +0000473
Hanno Becker77abef52017-11-02 10:50:28 +0000474 if( buf == NULL )
475 return( -1 );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100476
Hanno Becker77abef52017-11-02 10:50:28 +0000477 return( ctx_buffer_append( buf, data, len ) );
478 }
479#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100480
Hanno Becker0cc77742017-10-31 14:10:07 +0000481 return( mbedtls_net_send( ctx, data, len ) );
482}
483
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100484typedef struct
485{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200486 mbedtls_net_context *dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200487 const char *way;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200488 const char *type;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200489 unsigned len;
490 unsigned char buf[MAX_MSG_SIZE];
491} packet;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200492
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200493/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */
494void print_packet( const packet *p, const char *why )
495{
Hanno Becker0cc77742017-10-31 14:10:07 +0000496#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200497 if( why == NULL )
Hanno Becker77abef52017-11-02 10:50:28 +0000498 mbedtls_printf( " %05u dispatch %s %s (%u bytes)\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200499 ellapsed_time(), p->way, p->type, p->len );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200500 else
Hanno Becker77abef52017-11-02 10:50:28 +0000501 mbedtls_printf( " %05u dispatch %s %s (%u bytes): %s\n",
Hanno Becker0cc77742017-10-31 14:10:07 +0000502 ellapsed_time(), p->way, p->type, p->len, why );
503#else
504 if( why == NULL )
505 mbedtls_printf( " dispatch %s %s (%u bytes)\n",
506 p->way, p->type, p->len );
507 else
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100508 mbedtls_printf( " dispatch %s %s (%u bytes): %s\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200509 p->way, p->type, p->len, why );
Hanno Becker0cc77742017-10-31 14:10:07 +0000510#endif
511
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200512 fflush( stdout );
513}
514
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100515/*
516 * In order to test the server's behaviour when receiving a ClientHello after
517 * the connection is established (this could be a hard reset from the client,
518 * but the server must not drop the existing connection before establishing
519 * client reachability, see RFC 6347 Section 4.2.8), we memorize the first
520 * ClientHello we see (which can't have a cookie), then replay it after the
521 * first ApplicationData record - then we're done.
522 *
523 * This is controlled by the inject_clihlo option.
524 *
525 * We want an explicit state and a place to store the packet.
526 */
Manuel Pégourié-Gonnard7fe5ac12020-03-30 12:46:21 +0200527typedef enum {
528 ICH_INIT, /* haven't seen the first ClientHello yet */
529 ICH_CACHED, /* cached the initial ClientHello */
530 ICH_INJECTED, /* ClientHello already injected, done */
531} inject_clihlo_state_t;
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100532
Manuel Pégourié-Gonnard7fe5ac12020-03-30 12:46:21 +0200533static inject_clihlo_state_t inject_clihlo_state;
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100534static packet initial_clihlo;
535
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200536int send_packet( const packet *p, const char *why )
537{
538 int ret;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200539 mbedtls_net_context *dst = p->dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200540
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100541 /* save initial ClientHello? */
542 if( opt.inject_clihlo != 0 &&
Manuel Pégourié-Gonnard7fe5ac12020-03-30 12:46:21 +0200543 inject_clihlo_state == ICH_INIT &&
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100544 strcmp( p->type, "ClientHello" ) == 0 )
545 {
546 memcpy( &initial_clihlo, p, sizeof( packet ) );
Manuel Pégourié-Gonnard7fe5ac12020-03-30 12:46:21 +0200547 inject_clihlo_state = ICH_CACHED;
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100548 }
549
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200550 /* insert corrupted ApplicationData record? */
551 if( opt.bad_ad &&
552 strcmp( p->type, "ApplicationData" ) == 0 )
553 {
554 unsigned char buf[MAX_MSG_SIZE];
555 memcpy( buf, p->buf, p->len );
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200556
Hanno Beckerfbb0b702017-05-26 16:55:07 +0100557 if( p->len <= 13 )
558 {
559 mbedtls_printf( " ! can't corrupt empty AD record" );
560 }
561 else
562 {
563 ++buf[13];
564 print_packet( p, "corrupted" );
565 }
566
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100567 if( ( ret = dispatch_data( dst, buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200568 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100569 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200570 return( ret );
571 }
572 }
573
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200574 print_packet( p, why );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100575 if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200576 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100577 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200578 return( ret );
579 }
580
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200581 /* Don't duplicate Application Data, only handshake covered */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200582 if( opt.duplicate != 0 &&
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200583 strcmp( p->type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200584 rand() % opt.duplicate == 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200585 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200586 print_packet( p, "duplicated" );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200587
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100588 if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200589 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100590 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200591 return( ret );
592 }
593 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200594
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100595 /* Inject ClientHello after first ApplicationData */
596 if( opt.inject_clihlo != 0 &&
Manuel Pégourié-Gonnard7fe5ac12020-03-30 12:46:21 +0200597 inject_clihlo_state == ICH_CACHED &&
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100598 strcmp( p->type, "ApplicationData" ) == 0 )
599 {
600 print_packet( &initial_clihlo, "injected" );
601
602 if( ( ret = dispatch_data( dst, initial_clihlo.buf,
603 initial_clihlo.len ) ) <= 0 )
604 {
605 mbedtls_printf( " ! dispatch returned %d\n", ret );
606 return( ret );
607 }
608
Manuel Pégourié-Gonnard7fe5ac12020-03-30 12:46:21 +0200609 inject_clihlo_state = ICH_INJECTED;
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100610 }
611
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200612 return( 0 );
613}
614
Hanno Becker101bcba2018-08-21 16:39:51 +0100615#define MAX_DELAYED_MSG 5
616static size_t prev_len;
617static packet prev[MAX_DELAYED_MSG];
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200618
619void clear_pending( void )
620{
Hanno Becker12b72c12018-08-23 13:15:36 +0100621 memset( &prev, 0, sizeof( prev ) );
Hanno Becker101bcba2018-08-21 16:39:51 +0100622 prev_len = 0;
623}
624
625void delay_packet( packet *delay )
626{
627 if( prev_len == MAX_DELAYED_MSG )
628 return;
629
630 memcpy( &prev[prev_len++], delay, sizeof( packet ) );
631}
632
633int send_delayed()
634{
635 uint8_t offset;
636 int ret;
637 for( offset = 0; offset < prev_len; offset++ )
638 {
639 ret = send_packet( &prev[offset], "delayed" );
640 if( ret != 0 )
641 return( ret );
642 }
643
644 clear_pending();
645 return( 0 );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200646}
647
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200648/*
649 * Avoid dropping or delaying a packet that was already dropped twice: this
650 * only results in uninteresting timeouts. We can't rely on type to identify
651 * packets, since during renegotiation they're all encrypted. So, rely on
652 * size mod 2048 (which is usually just size).
653 */
654static unsigned char dropped[2048] = { 0 };
655#define DROP_MAX 2
656
Hanno Beckerbcf97ec2019-06-04 13:04:28 +0100657/* We only drop packets at the level of entire datagrams, not at the level
658 * 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
660 * times a record has been dropped correctly. However, the only known reason
661 * why a peer would change datagram packing is disabling the latter on
662 * retransmission, in which case we'd drop involved records at most
663 * DROP_MAX + 1 times. */
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200664void update_dropped( const packet *p )
665{
666 size_t id = p->len % sizeof( dropped );
Manuel Pégourié-Gonnardfa60f122014-09-26 16:07:29 +0200667 ++dropped[id];
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200668}
669
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200670int handle_message( const char *way,
671 mbedtls_net_context *dst,
672 mbedtls_net_context *src )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200673{
674 int ret;
675 packet cur;
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200676 size_t id;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200677
Hanno Becker01ea7782018-08-17 13:33:41 +0100678 uint8_t delay_idx;
679 char ** delay_list;
680 uint8_t delay_list_len;
681
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +0200682 /* receive packet */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200683 if( ( ret = mbedtls_net_recv( src, cur.buf, sizeof( cur.buf ) ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 mbedtls_printf( " ! mbedtls_net_recv returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200686 return( ret );
687 }
688
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200689 cur.len = ret;
690 cur.type = msg_type( cur.buf, cur.len );
691 cur.way = way;
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200692 cur.dst = dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200693 print_packet( &cur, NULL );
694
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200695 id = cur.len % sizeof( dropped );
696
Hanno Becker01ea7782018-08-17 13:33:41 +0100697 if( strcmp( way, "S <- C" ) == 0 )
698 {
699 delay_list = opt.delay_cli;
700 delay_list_len = opt.delay_cli_cnt;
701 }
702 else
703 {
704 delay_list = opt.delay_srv;
705 delay_list_len = opt.delay_srv_cnt;
706 }
Hanno Beckercf469452018-08-28 10:09:47 +0100707
Hanno Becker01ea7782018-08-17 13:33:41 +0100708 /* Check if message type is in the list of messages
709 * that should be delayed */
710 for( delay_idx = 0; delay_idx < delay_list_len; delay_idx++ )
711 {
712 if( delay_list[ delay_idx ] == NULL )
713 continue;
714
715 if( strcmp( delay_list[ delay_idx ], cur.type ) == 0 )
716 {
717 /* Delay message */
Hanno Becker101bcba2018-08-21 16:39:51 +0100718 delay_packet( &cur );
Hanno Becker01ea7782018-08-17 13:33:41 +0100719
720 /* Remove entry from list */
721 mbedtls_free( delay_list[delay_idx] );
722 delay_list[delay_idx] = NULL;
723
724 return( 0 );
725 }
726 }
727
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200728 /* do we want to drop, delay, or forward it? */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200729 if( ( opt.mtu != 0 &&
730 cur.len > (unsigned) opt.mtu ) ||
731 ( opt.drop != 0 &&
732 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200733 ! ( opt.protect_hvr &&
734 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200735 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200736 dropped[id] < DROP_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200737 rand() % opt.drop == 0 ) )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200738 {
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200739 update_dropped( &cur );
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200740 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200741 else if( ( opt.delay_ccs == 1 &&
742 strcmp( cur.type, "ChangeCipherSpec" ) == 0 ) ||
743 ( opt.delay != 0 &&
744 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200745 ! ( opt.protect_hvr &&
746 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200747 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200748 dropped[id] < DROP_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200749 rand() % opt.delay == 0 ) )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200750 {
Hanno Becker101bcba2018-08-21 16:39:51 +0100751 delay_packet( &cur );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200752 }
753 else
754 {
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200755 /* forward and possibly duplicate */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200756 if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 )
757 return( ret );
758
Hanno Becker101bcba2018-08-21 16:39:51 +0100759 /* send previously delayed messages if any */
760 ret = send_delayed();
761 if( ret != 0 )
762 return( ret );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200763 }
764
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200765 return( 0 );
766}
767
768int main( int argc, char *argv[] )
769{
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100770 int ret = 1;
771 int exit_code = MBEDTLS_EXIT_FAILURE;
Hanno Becker01ea7782018-08-17 13:33:41 +0100772 uint8_t delay_idx;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200773
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200774 mbedtls_net_context listen_fd, client_fd, server_fd;
Hanno Becker77abef52017-11-02 10:50:28 +0000775
776#if defined( MBEDTLS_TIMING_C )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100777 struct timeval tm;
Hanno Becker77abef52017-11-02 10:50:28 +0000778#endif
779
780 struct timeval *tm_ptr = NULL;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200781
782 int nb_fds;
783 fd_set read_fds;
784
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200785 mbedtls_net_init( &listen_fd );
786 mbedtls_net_init( &client_fd );
787 mbedtls_net_init( &server_fd );
788
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200789 get_options( argc, argv );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200790
791 /*
792 * Decisions to drop/delay/duplicate packets are pseudo-random: dropping
793 * exactly 1 in N packets would lead to problems when a flight has exactly
794 * N packets: the same packet would be dropped on every resend.
795 *
796 * In order to be able to reproduce problems reliably, the seed may be
797 * specified explicitly.
798 */
799 if( opt.seed == 0 )
800 {
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200801 opt.seed = (unsigned int) time( NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 mbedtls_printf( " . Pseudo-random seed: %u\n", opt.seed );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200803 }
804
805 srand( opt.seed );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200806
807 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200808 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200809 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200810 mbedtls_printf( " . Connect to server on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200811 opt.server_addr, opt.server_port );
812 fflush( stdout );
813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
815 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200818 goto exit;
819 }
820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200822
823 /*
824 * 1. Setup the "listening" UDP socket
825 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200826 mbedtls_printf( " . Bind on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200827 opt.listen_addr, opt.listen_port );
828 fflush( stdout );
829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 if( ( ret = mbedtls_net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
831 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200834 goto exit;
835 }
836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200838
839 /*
840 * 2. Wait until a client connects
841 */
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200842accept:
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200843 mbedtls_net_free( &client_fd );
844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 mbedtls_printf( " . Waiting for a remote connection ..." );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200846 fflush( stdout );
847
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200848 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200849 NULL, 0, NULL ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 mbedtls_printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200852 goto exit;
853 }
854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200856
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200857 /*
858 * 3. Forward packets forever (kill the process to terminate it)
859 */
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200860 clear_pending();
861 memset( dropped, 0, sizeof( dropped ) );
862
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200863 nb_fds = client_fd.fd;
864 if( nb_fds < server_fd.fd )
865 nb_fds = server_fd.fd;
866 if( nb_fds < listen_fd.fd )
867 nb_fds = listen_fd.fd;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200868 ++nb_fds;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200869
Hanno Becker0cc77742017-10-31 14:10:07 +0000870#if defined(MBEDTLS_TIMING_C)
Hanno Becker211f44c2017-10-31 14:08:10 +0000871 if( opt.pack > 0 )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100872 {
873 outbuf[0].ctx = &server_fd;
874 outbuf[0].description = "S <- C";
875 outbuf[0].num_datagrams = 0;
876 outbuf[0].len = 0;
877
878 outbuf[1].ctx = &client_fd;
879 outbuf[1].description = "S -> C";
880 outbuf[1].num_datagrams = 0;
881 outbuf[1].len = 0;
882 }
Hanno Becker0cc77742017-10-31 14:10:07 +0000883#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100884
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200885 while( 1 )
886 {
Hanno Becker77abef52017-11-02 10:50:28 +0000887#if defined(MBEDTLS_TIMING_C)
888 if( opt.pack > 0 )
889 {
890 unsigned max_wait_server, max_wait_client, max_wait;
891 max_wait_server = ctx_buffer_time_remaining( &outbuf[0] );
892 max_wait_client = ctx_buffer_time_remaining( &outbuf[1] );
893
894 max_wait = (unsigned) -1;
895
896 if( max_wait_server == 0 )
897 ctx_buffer_flush( &outbuf[0] );
898 else
899 max_wait = max_wait_server;
900
901 if( max_wait_client == 0 )
902 ctx_buffer_flush( &outbuf[1] );
903 else
904 {
905 if( max_wait_client < max_wait )
906 max_wait = max_wait_client;
907 }
908
909 if( max_wait != (unsigned) -1 )
910 {
911 tm.tv_sec = max_wait / 1000;
912 tm.tv_usec = ( max_wait % 1000 ) * 1000;
913
914 tm_ptr = &tm;
915 }
916 else
917 {
918 tm_ptr = NULL;
919 }
920 }
921#endif /* MBEDTLS_TIMING_C */
922
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200923 FD_ZERO( &read_fds );
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200924 FD_SET( server_fd.fd, &read_fds );
925 FD_SET( client_fd.fd, &read_fds );
926 FD_SET( listen_fd.fd, &read_fds );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200927
Hanno Becker77abef52017-11-02 10:50:28 +0000928 if( ( ret = select( nb_fds, &read_fds, NULL, NULL, tm_ptr ) ) < 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200929 {
930 perror( "select" );
931 goto exit;
932 }
933
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200934 if( FD_ISSET( listen_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200935 goto accept;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200936
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200937 if( FD_ISSET( client_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200938 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200939 if( ( ret = handle_message( "S <- C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200940 &server_fd, &client_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200941 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200942 }
943
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200944 if( FD_ISSET( server_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200945 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200946 if( ( ret = handle_message( "S -> C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200947 &client_fd, &server_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200948 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200949 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100950
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200951 }
952
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100953 exit_code = MBEDTLS_EXIT_SUCCESS;
954
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200955exit:
956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957#ifdef MBEDTLS_ERROR_C
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100958 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200959 {
960 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 mbedtls_strerror( ret, error_buf, 100 );
962 mbedtls_printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200963 fflush( stdout );
964 }
965#endif
966
Hanno Becker01ea7782018-08-17 13:33:41 +0100967 for( delay_idx = 0; delay_idx < MAX_DELAYED_HS; delay_idx++ )
968 {
969 mbedtls_free( opt.delay_cli + delay_idx );
970 mbedtls_free( opt.delay_srv + delay_idx );
971 }
972
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200973 mbedtls_net_free( &client_fd );
974 mbedtls_net_free( &server_fd );
975 mbedtls_net_free( &listen_fd );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200976
977#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 mbedtls_printf( " Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200979 fflush( stdout ); getchar();
980#endif
981
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200982 mbedtls_exit( exit_code );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200983}
984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985#endif /* MBEDTLS_NET_C */