blob: 1c8b500c4abbc7b9e6b7355086c6c6aee2e7692f [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
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010045#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia80081a62018-04-29 21:58:53 +010046#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
47#endif /* MBEDTLS_PLATFORM_C */
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_NET_C)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020050int main( void )
51{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052 mbedtls_printf( "MBEDTLS_NET_C not defined.\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020053 return( 0 );
54}
55#else
56
Andres AG788aa4a2016-09-14 14:32:09 +010057#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/error.h"
59#include "mbedtls/ssl.h"
Hanno Becker0cc77742017-10-31 14:10:07 +000060#include "mbedtls/timing.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020061
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +000062#include <string.h>
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020063
64/* For select() */
65#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
66 !defined(EFI32)
67#include <winsock2.h>
68#include <windows.h>
69#if defined(_MSC_VER)
70#if defined(_WIN32_WCE)
71#pragma comment( lib, "ws2.lib" )
72#else
73#pragma comment( lib, "ws2_32.lib" )
74#endif
75#endif /* _MSC_VER */
76#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
77#include <sys/time.h>
78#include <sys/types.h>
79#include <unistd.h>
80#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
81
Manuel Pégourié-Gonnard66491e12019-10-22 12:50:13 +020082#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
83int mbedtls_hardware_poll( void *data, unsigned char *output,
84 size_t len, size_t *olen )
85{
86 size_t i;
87 (void) data;
88 for( i = 0; i < len; ++i )
89 output[i] = rand();
90 *olen = len;
91 return( 0 );
92}
93#endif
94
Manuel Pégourié-Gonnardb46780e2014-09-23 12:17:30 +020095#define MAX_MSG_SIZE 16384 + 2048 /* max record/datagram size */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020096
97#define DFL_SERVER_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020098#define DFL_SERVER_PORT "4433"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020099#define DFL_LISTEN_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200100#define DFL_LISTEN_PORT "5556"
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100101#define DFL_PACK 0
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200102
Hanno Becker0cc77742017-10-31 14:10:07 +0000103#if defined(MBEDTLS_TIMING_C)
104#define USAGE_PACK \
105 " pack=%%d default: 0 (don't pack)\n" \
106 " options: t > 0 (pack for t milliseconds)\n"
107#else
108#define USAGE_PACK
109#endif
110
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200111#define USAGE \
112 "\n usage: udp_proxy param=<>...\n" \
113 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200114 " server_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200115 " server_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200116 " listen_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200117 " listen_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200118 "\n" \
119 " duplicate=%%d default: 0 (no duplication)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200120 " duplicate about 1:N packets randomly\n" \
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200121 " delay=%%d default: 0 (no delayed packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200122 " delay about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200123 " delay_ccs=0/1 default: 0 (don't delay ChangeCipherSpec)\n" \
Hanno Becker01ea7782018-08-17 13:33:41 +0100124 " delay_cli=%%s Handshake message from client that should be\n"\
125 " delayed. Possible values are 'ClientHello',\n" \
126 " 'Certificate', 'CertificateVerify', and\n" \
127 " 'ClientKeyExchange'.\n" \
128 " May be used multiple times, even for the same\n"\
129 " message, in which case the respective message\n"\
130 " gets delayed multiple times.\n" \
131 " delay_srv=%%s Handshake message from server that should be\n"\
132 " delayed. Possible values are 'HelloRequest',\n"\
133 " 'ServerHello', 'ServerHelloDone', 'Certificate'\n"\
134 " 'ServerKeyExchange', 'NewSessionTicket',\n"\
135 " 'HelloVerifyRequest' and ''CertificateRequest'.\n"\
136 " May be used multiple times, even for the same\n"\
137 " message, in which case the respective message\n"\
138 " gets delayed multiple times.\n" \
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200139 " drop=%%d default: 0 (no dropped packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200140 " drop about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200141 " mtu=%%d default: 0 (unlimited)\n" \
142 " drop packets larger than N bytes\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200143 " bad_ad=0/1 default: 0 (don't add bad ApplicationData)\n" \
Hanno Becker34dcf4e2019-05-24 10:07:42 +0100144 " bad_cid=%%d default: 0 (don't corrupt Connection IDs)\n" \
145 " duplicate 1:N packets containing a CID,\n" \
146 " modifying CID in first instance of the packet.\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200147 " protect_hvr=0/1 default: 0 (don't protect HelloVerifyRequest)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000148 " protect_len=%%d default: (don't protect packets of this size)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200149 "\n" \
150 " seed=%%d default: (use current time)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000151 USAGE_PACK \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200152 "\n"
153
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200154/*
155 * global options
156 */
Hanno Becker01ea7782018-08-17 13:33:41 +0100157
158#define MAX_DELAYED_HS 10
159
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200160static struct options
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200161{
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200162 const char *server_addr; /* address to forward packets to */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200163 const char *server_port; /* port to forward packets to */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200164 const char *listen_addr; /* address for accepting client connections */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200165 const char *listen_port; /* port for accepting client connections */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200166
167 int duplicate; /* duplicate 1 in N packets (none if 0) */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200168 int delay; /* delay 1 packet in N (none if 0) */
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200169 int delay_ccs; /* delay ChangeCipherSpec */
Hanno Becker01ea7782018-08-17 13:33:41 +0100170 char* delay_cli[MAX_DELAYED_HS]; /* handshake types of messages from
Hanno Becker41038102018-08-28 11:15:32 +0100171 * client that should be delayed. */
Hanno Becker01ea7782018-08-17 13:33:41 +0100172 uint8_t delay_cli_cnt; /* Number of entries in delay_cli. */
173 char* delay_srv[MAX_DELAYED_HS]; /* handshake types of messages from
Hanno Becker41038102018-08-28 11:15:32 +0100174 * server that should be delayed. */
Hanno Becker01ea7782018-08-17 13:33:41 +0100175 uint8_t delay_srv_cnt; /* Number of entries in delay_srv. */
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200176 int drop; /* drop 1 packet in N (none if 0) */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200177 int mtu; /* drop packets larger than this */
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200178 int bad_ad; /* inject corrupted ApplicationData record */
Hanno Becker34dcf4e2019-05-24 10:07:42 +0100179 unsigned bad_cid; /* inject corrupted CID record */
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200180 int protect_hvr; /* never drop or delay HelloVerifyRequest */
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200181 int protect_len; /* never drop/delay packet of the given size*/
Hanno Becker77abef52017-11-02 10:50:28 +0000182 unsigned pack; /* merge packets into single datagram for
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100183 * at most \c merge milliseconds if > 0 */
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200184 unsigned int seed; /* seed for "random" events */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200185} opt;
186
187static void exit_usage( const char *name, const char *value )
188{
189 if( value == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 mbedtls_printf( " unknown option or missing value: %s\n", name );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200191 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 mbedtls_printf( " option %s: illegal value: %s\n", name, value );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_printf( USAGE );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200195 exit( 1 );
196}
197
198static void get_options( int argc, char *argv[] )
199{
200 int i;
201 char *p, *q;
202
203 opt.server_addr = DFL_SERVER_ADDR;
204 opt.server_port = DFL_SERVER_PORT;
205 opt.listen_addr = DFL_LISTEN_ADDR;
206 opt.listen_port = DFL_LISTEN_PORT;
Hanno Becker211f44c2017-10-31 14:08:10 +0000207 opt.pack = DFL_PACK;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200208 /* Other members default to 0 */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200209
Hanno Becker01ea7782018-08-17 13:33:41 +0100210 opt.delay_cli_cnt = 0;
211 opt.delay_srv_cnt = 0;
212 memset( opt.delay_cli, 0, sizeof( opt.delay_cli ) );
213 memset( opt.delay_srv, 0, sizeof( opt.delay_srv ) );
214
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200215 for( i = 1; i < argc; i++ )
216 {
217 p = argv[i];
218 if( ( q = strchr( p, '=' ) ) == NULL )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200219 exit_usage( p, NULL );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200220 *q++ = '\0';
221
222 if( strcmp( p, "server_addr" ) == 0 )
223 opt.server_addr = q;
224 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200225 opt.server_port = q;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200226 else if( strcmp( p, "listen_addr" ) == 0 )
227 opt.listen_addr = q;
228 else if( strcmp( p, "listen_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200229 opt.listen_port = q;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200230 else if( strcmp( p, "duplicate" ) == 0 )
231 {
232 opt.duplicate = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200233 if( opt.duplicate < 0 || opt.duplicate > 20 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200234 exit_usage( p, q );
235 }
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200236 else if( strcmp( p, "delay" ) == 0 )
237 {
238 opt.delay = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200239 if( opt.delay < 0 || opt.delay > 20 || opt.delay == 1 )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200240 exit_usage( p, q );
241 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200242 else if( strcmp( p, "delay_ccs" ) == 0 )
243 {
244 opt.delay_ccs = atoi( q );
245 if( opt.delay_ccs < 0 || opt.delay_ccs > 1 )
246 exit_usage( p, q );
247 }
Hanno Becker01ea7782018-08-17 13:33:41 +0100248 else if( strcmp( p, "delay_cli" ) == 0 ||
249 strcmp( p, "delay_srv" ) == 0 )
250 {
251 uint8_t *delay_cnt;
252 char **delay_list;
253 size_t len;
254 char *buf;
255
256 if( strcmp( p, "delay_cli" ) == 0 )
257 {
258 delay_cnt = &opt.delay_cli_cnt;
259 delay_list = opt.delay_cli;
260 }
261 else
262 {
263 delay_cnt = &opt.delay_srv_cnt;
264 delay_list = opt.delay_srv;
265 }
266
267 if( *delay_cnt == MAX_DELAYED_HS )
268 {
Hanno Beckerf34a4c12018-08-28 17:22:26 +0100269 mbedtls_printf( " too many uses of %s: only %d allowed\n",
270 p, MAX_DELAYED_HS );
Hanno Becker01ea7782018-08-17 13:33:41 +0100271 exit_usage( p, NULL );
272 }
273
274 len = strlen( q );
275 buf = mbedtls_calloc( 1, len + 1 );
276 if( buf == NULL )
277 {
278 mbedtls_printf( " Allocation failure\n" );
279 exit( 1 );
280 }
281 memcpy( buf, q, len + 1 );
282
283 delay_list[ (*delay_cnt)++ ] = buf;
284 }
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200285 else if( strcmp( p, "drop" ) == 0 )
286 {
287 opt.drop = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200288 if( opt.drop < 0 || opt.drop > 20 || opt.drop == 1 )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200289 exit_usage( p, q );
290 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100291 else if( strcmp( p, "pack" ) == 0 )
292 {
Hanno Becker0cc77742017-10-31 14:10:07 +0000293#if defined(MBEDTLS_TIMING_C)
Hanno Becker77abef52017-11-02 10:50:28 +0000294 opt.pack = (unsigned) atoi( q );
Hanno Becker0cc77742017-10-31 14:10:07 +0000295#else
296 mbedtls_printf( " option pack only defined if MBEDTLS_TIMING_C is enabled\n" );
297 exit( 1 );
298#endif
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100299 }
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200300 else if( strcmp( p, "mtu" ) == 0 )
301 {
302 opt.mtu = atoi( q );
303 if( opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE )
304 exit_usage( p, q );
305 }
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200306 else if( strcmp( p, "bad_ad" ) == 0 )
307 {
308 opt.bad_ad = atoi( q );
309 if( opt.bad_ad < 0 || opt.bad_ad > 1 )
310 exit_usage( p, q );
311 }
Hanno Becker34dcf4e2019-05-24 10:07:42 +0100312#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
313 else if( strcmp( p, "bad_cid" ) == 0 )
314 {
315 opt.bad_cid = (unsigned) atoi( q );
316 }
317#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200318 else if( strcmp( p, "protect_hvr" ) == 0 )
319 {
320 opt.protect_hvr = atoi( q );
321 if( opt.protect_hvr < 0 || opt.protect_hvr > 1 )
322 exit_usage( p, q );
323 }
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200324 else if( strcmp( p, "protect_len" ) == 0 )
325 {
326 opt.protect_len = atoi( q );
327 if( opt.protect_len < 0 )
328 exit_usage( p, q );
329 }
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200330 else if( strcmp( p, "seed" ) == 0 )
331 {
332 opt.seed = atoi( q );
333 if( opt.seed == 0 )
334 exit_usage( p, q );
335 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200336 else
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200337 exit_usage( p, NULL );
338 }
339}
340
341static const char *msg_type( unsigned char *msg, size_t len )
342{
343 if( len < 1 ) return( "Invalid" );
344 switch( msg[0] )
345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" );
347 case MBEDTLS_SSL_MSG_ALERT: return( "Alert" );
348 case MBEDTLS_SSL_MSG_APPLICATION_DATA: return( "ApplicationData" );
Hanno Becker6c4bc142019-05-08 15:36:31 +0100349 case MBEDTLS_SSL_MSG_CID: return( "CID" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 case MBEDTLS_SSL_MSG_HANDSHAKE: break; /* See below */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200351 default: return( "Unknown" );
352 }
353
Manuel Pégourié-Gonnard8cc7e032014-09-25 12:59:05 +0200354 if( len < 13 + 12 ) return( "Invalid handshake" );
355
356 /*
357 * Our handshake message are less than 2^16 bytes long, so they should
358 * have 0 as the first byte of length, frag_offset and frag_length.
359 * Otherwise, assume they are encrypted.
360 */
361 if( msg[14] || msg[19] || msg[22] ) return( "Encrypted handshake" );
362
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200363 switch( msg[13] )
364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 case MBEDTLS_SSL_HS_HELLO_REQUEST: return( "HelloRequest" );
366 case MBEDTLS_SSL_HS_CLIENT_HELLO: return( "ClientHello" );
367 case MBEDTLS_SSL_HS_SERVER_HELLO: return( "ServerHello" );
368 case MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" );
369 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" );
370 case MBEDTLS_SSL_HS_CERTIFICATE: return( "Certificate" );
371 case MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" );
372 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" );
373 case MBEDTLS_SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" );
374 case MBEDTLS_SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" );
375 case MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" );
376 case MBEDTLS_SSL_HS_FINISHED: return( "Finished" );
Manuel Pégourié-Gonnardbc010a02014-09-20 12:40:51 +0200377 default: return( "Unknown handshake" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200378 }
379}
380
Hanno Becker0cc77742017-10-31 14:10:07 +0000381#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200382/* Return elapsed time in milliseconds since the first call */
Hanno Becker77abef52017-11-02 10:50:28 +0000383static unsigned ellapsed_time( void )
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200384{
Hanno Becker92474da2017-10-31 14:09:30 +0000385 static int initialized = 0;
386 static struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200387
Hanno Becker92474da2017-10-31 14:09:30 +0000388 if( initialized == 0 )
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200389 {
Hanno Becker92474da2017-10-31 14:09:30 +0000390 (void) mbedtls_timing_get_timer( &hires, 1 );
391 initialized = 1;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200392 return( 0 );
393 }
394
Hanno Becker92474da2017-10-31 14:09:30 +0000395 return( mbedtls_timing_get_timer( &hires, 0 ) );
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200396}
397
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200398typedef struct
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200399{
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100400 mbedtls_net_context *ctx;
401
402 const char *description;
403
Hanno Becker77abef52017-11-02 10:50:28 +0000404 unsigned packet_lifetime;
405 unsigned num_datagrams;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100406
407 unsigned char data[MAX_MSG_SIZE];
Hanno Beckera5e68972017-12-06 08:35:02 +0000408 size_t len;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100409
410} ctx_buffer;
411
412static ctx_buffer outbuf[2];
413
414static int ctx_buffer_flush( ctx_buffer *buf )
415{
416 int ret;
417
Hanno Becker77abef52017-11-02 10:50:28 +0000418 mbedtls_printf( " %05u flush %s: %u bytes, %u datagrams, last %u ms\n",
419 ellapsed_time(), buf->description,
Hanno Beckera5e68972017-12-06 08:35:02 +0000420 (unsigned) buf->len, buf->num_datagrams,
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100421 ellapsed_time() - buf->packet_lifetime );
422
423 ret = mbedtls_net_send( buf->ctx, buf->data, buf->len );
424
425 buf->len = 0;
426 buf->num_datagrams = 0;
427
428 return( ret );
429}
430
Hanno Becker77abef52017-11-02 10:50:28 +0000431static unsigned ctx_buffer_time_remaining( ctx_buffer *buf )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100432{
Hanno Becker77abef52017-11-02 10:50:28 +0000433 unsigned const cur_time = ellapsed_time();
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100434
Hanno Becker77abef52017-11-02 10:50:28 +0000435 if( buf->num_datagrams == 0 )
436 return( (unsigned) -1 );
437
438 if( cur_time - buf->packet_lifetime >= opt.pack )
439 return( 0 );
440
441 return( opt.pack - ( cur_time - buf->packet_lifetime ) );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100442}
443
444static int ctx_buffer_append( ctx_buffer *buf,
445 const unsigned char * data,
446 size_t len )
447{
448 int ret;
449
Hanno Beckera5e68972017-12-06 08:35:02 +0000450 if( len > (size_t) INT_MAX )
451 return( -1 );
452
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100453 if( len > sizeof( buf->data ) )
454 {
Hanno Becker77abef52017-11-02 10:50:28 +0000455 mbedtls_printf( " ! buffer size %u too large (max %u)\n",
456 (unsigned) len, (unsigned) sizeof( buf->data ) );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100457 return( -1 );
458 }
459
460 if( sizeof( buf->data ) - buf->len < len )
461 {
462 if( ( ret = ctx_buffer_flush( buf ) ) <= 0 )
Hanno Becker6c4bc142019-05-08 15:36:31 +0100463 {
464 mbedtls_printf( "ctx_buffer_flush failed with -%#04x", -ret );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100465 return( ret );
Hanno Becker6c4bc142019-05-08 15:36:31 +0100466 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100467 }
468
469 memcpy( buf->data + buf->len, data, len );
470
471 buf->len += len;
472 if( ++buf->num_datagrams == 1 )
473 buf->packet_lifetime = ellapsed_time();
474
Hanno Beckera5e68972017-12-06 08:35:02 +0000475 return( (int) len );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100476}
Hanno Becker77abef52017-11-02 10:50:28 +0000477#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100478
479static int dispatch_data( mbedtls_net_context *ctx,
480 const unsigned char * data,
481 size_t len )
482{
Hanno Becker6c4bc142019-05-08 15:36:31 +0100483 int ret;
Hanno Becker77abef52017-11-02 10:50:28 +0000484#if defined(MBEDTLS_TIMING_C)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100485 ctx_buffer *buf = NULL;
Hanno Becker77abef52017-11-02 10:50:28 +0000486 if( opt.pack > 0 )
487 {
488 if( outbuf[0].ctx == ctx )
489 buf = &outbuf[0];
490 else if( outbuf[1].ctx == ctx )
491 buf = &outbuf[1];
Hanno Becker0cc77742017-10-31 14:10:07 +0000492
Hanno Becker77abef52017-11-02 10:50:28 +0000493 if( buf == NULL )
494 return( -1 );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100495
Hanno Becker77abef52017-11-02 10:50:28 +0000496 return( ctx_buffer_append( buf, data, len ) );
497 }
498#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100499
Hanno Becker6c4bc142019-05-08 15:36:31 +0100500 ret = mbedtls_net_send( ctx, data, len );
501 if( ret < 0 )
502 {
503 mbedtls_printf( "net_send returned -%#04x\n", -ret );
504 }
505 return( ret );
Hanno Becker0cc77742017-10-31 14:10:07 +0000506}
507
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100508typedef struct
509{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200510 mbedtls_net_context *dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200511 const char *way;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200512 const char *type;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200513 unsigned len;
514 unsigned char buf[MAX_MSG_SIZE];
515} packet;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200516
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200517/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */
518void print_packet( const packet *p, const char *why )
519{
Hanno Becker0cc77742017-10-31 14:10:07 +0000520#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200521 if( why == NULL )
Hanno Becker77abef52017-11-02 10:50:28 +0000522 mbedtls_printf( " %05u dispatch %s %s (%u bytes)\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200523 ellapsed_time(), p->way, p->type, p->len );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200524 else
Hanno Becker77abef52017-11-02 10:50:28 +0000525 mbedtls_printf( " %05u dispatch %s %s (%u bytes): %s\n",
Hanno Becker0cc77742017-10-31 14:10:07 +0000526 ellapsed_time(), p->way, p->type, p->len, why );
527#else
528 if( why == NULL )
529 mbedtls_printf( " dispatch %s %s (%u bytes)\n",
530 p->way, p->type, p->len );
531 else
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100532 mbedtls_printf( " dispatch %s %s (%u bytes): %s\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200533 p->way, p->type, p->len, why );
Hanno Becker0cc77742017-10-31 14:10:07 +0000534#endif
535
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200536 fflush( stdout );
537}
538
539int send_packet( const packet *p, const char *why )
540{
541 int ret;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200542 mbedtls_net_context *dst = p->dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200543
Hanno Becker34dcf4e2019-05-24 10:07:42 +0100544 /* insert corrupted CID record? */
545 if( opt.bad_cid != 0 &&
546 strcmp( p->type, "CID" ) == 0 &&
547 ( rand() % opt.bad_cid ) == 0 )
548 {
549 unsigned char buf[MAX_MSG_SIZE];
550 memcpy( buf, p->buf, p->len );
551
552 /* The CID resides at offset 11 in the DTLS record header. */
553 buf[11] ^= 1;
554 print_packet( p, "modified CID" );
555
556 if( ( ret = dispatch_data( dst, buf, p->len ) ) <= 0 )
557 {
558 mbedtls_printf( " ! dispatch returned %d\n", ret );
559 return( ret );
560 }
561 }
562
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200563 /* insert corrupted ApplicationData record? */
564 if( opt.bad_ad &&
565 strcmp( p->type, "ApplicationData" ) == 0 )
566 {
567 unsigned char buf[MAX_MSG_SIZE];
568 memcpy( buf, p->buf, p->len );
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200569
Hanno Beckerfbb0b702017-05-26 16:55:07 +0100570 if( p->len <= 13 )
571 {
572 mbedtls_printf( " ! can't corrupt empty AD record" );
573 }
574 else
575 {
576 ++buf[13];
577 print_packet( p, "corrupted" );
578 }
579
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100580 if( ( ret = dispatch_data( dst, buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200581 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100582 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200583 return( ret );
584 }
585 }
586
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200587 print_packet( p, why );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100588 if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200589 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100590 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200591 return( ret );
592 }
593
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200594 /* Don't duplicate Application Data, only handshake covered */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200595 if( opt.duplicate != 0 &&
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200596 strcmp( p->type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200597 rand() % opt.duplicate == 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200598 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200599 print_packet( p, "duplicated" );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200600
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100601 if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200602 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100603 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200604 return( ret );
605 }
606 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200607
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200608 return( 0 );
609}
610
Hanno Becker101bcba2018-08-21 16:39:51 +0100611#define MAX_DELAYED_MSG 5
612static size_t prev_len;
613static packet prev[MAX_DELAYED_MSG];
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200614
615void clear_pending( void )
616{
Hanno Becker12b72c12018-08-23 13:15:36 +0100617 memset( &prev, 0, sizeof( prev ) );
Hanno Becker101bcba2018-08-21 16:39:51 +0100618 prev_len = 0;
619}
620
621void delay_packet( packet *delay )
622{
623 if( prev_len == MAX_DELAYED_MSG )
624 return;
625
626 memcpy( &prev[prev_len++], delay, sizeof( packet ) );
627}
628
629int send_delayed()
630{
631 uint8_t offset;
632 int ret;
633 for( offset = 0; offset < prev_len; offset++ )
634 {
635 ret = send_packet( &prev[offset], "delayed" );
636 if( ret != 0 )
637 return( ret );
638 }
639
640 clear_pending();
641 return( 0 );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200642}
643
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200644/*
645 * Avoid dropping or delaying a packet that was already dropped twice: this
646 * only results in uninteresting timeouts. We can't rely on type to identify
647 * packets, since during renegotiation they're all encrypted. So, rely on
648 * size mod 2048 (which is usually just size).
649 */
650static unsigned char dropped[2048] = { 0 };
651#define DROP_MAX 2
652
Hanno Becker08c78a22019-06-04 13:04:28 +0100653/* We only drop packets at the level of entire datagrams, not at the level
654 * of records. In particular, if the peer changes the way it packs multiple
655 * records into a single datagram, we don't necessarily count the number of
656 * times a record has been dropped correctly. However, the only known reason
657 * why a peer would change datagram packing is disabling the latter on
658 * retransmission, in which case we'd drop involved records at most
659 * DROP_MAX + 1 times. */
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200660void update_dropped( const packet *p )
661{
662 size_t id = p->len % sizeof( dropped );
Manuel Pégourié-Gonnardfa60f122014-09-26 16:07:29 +0200663 ++dropped[id];
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200664}
665
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200666int handle_message( const char *way,
667 mbedtls_net_context *dst,
668 mbedtls_net_context *src )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200669{
670 int ret;
671 packet cur;
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200672 size_t id;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200673
Hanno Becker01ea7782018-08-17 13:33:41 +0100674 uint8_t delay_idx;
675 char ** delay_list;
676 uint8_t delay_list_len;
677
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +0200678 /* receive packet */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200679 if( ( ret = mbedtls_net_recv( src, cur.buf, sizeof( cur.buf ) ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 mbedtls_printf( " ! mbedtls_net_recv returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200682 return( ret );
683 }
684
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200685 cur.len = ret;
686 cur.type = msg_type( cur.buf, cur.len );
687 cur.way = way;
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200688 cur.dst = dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200689 print_packet( &cur, NULL );
690
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200691 id = cur.len % sizeof( dropped );
692
Hanno Becker01ea7782018-08-17 13:33:41 +0100693 if( strcmp( way, "S <- C" ) == 0 )
694 {
695 delay_list = opt.delay_cli;
696 delay_list_len = opt.delay_cli_cnt;
697 }
698 else
699 {
700 delay_list = opt.delay_srv;
701 delay_list_len = opt.delay_srv_cnt;
702 }
Hanno Beckercf469452018-08-28 10:09:47 +0100703
Hanno Becker01ea7782018-08-17 13:33:41 +0100704 /* Check if message type is in the list of messages
705 * that should be delayed */
706 for( delay_idx = 0; delay_idx < delay_list_len; delay_idx++ )
707 {
708 if( delay_list[ delay_idx ] == NULL )
709 continue;
710
711 if( strcmp( delay_list[ delay_idx ], cur.type ) == 0 )
712 {
713 /* Delay message */
Hanno Becker101bcba2018-08-21 16:39:51 +0100714 delay_packet( &cur );
Hanno Becker01ea7782018-08-17 13:33:41 +0100715
716 /* Remove entry from list */
717 mbedtls_free( delay_list[delay_idx] );
718 delay_list[delay_idx] = NULL;
719
720 return( 0 );
721 }
722 }
723
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200724 /* do we want to drop, delay, or forward it? */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200725 if( ( opt.mtu != 0 &&
726 cur.len > (unsigned) opt.mtu ) ||
727 ( opt.drop != 0 &&
Hanno Becker6c4bc142019-05-08 15:36:31 +0100728 strcmp( cur.type, "CID" ) != 0 &&
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200729 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200730 ! ( opt.protect_hvr &&
731 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200732 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200733 dropped[id] < DROP_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200734 rand() % opt.drop == 0 ) )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200735 {
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200736 update_dropped( &cur );
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200737 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200738 else if( ( opt.delay_ccs == 1 &&
739 strcmp( cur.type, "ChangeCipherSpec" ) == 0 ) ||
740 ( opt.delay != 0 &&
Hanno Becker6c4bc142019-05-08 15:36:31 +0100741 strcmp( cur.type, "CID" ) != 0 &&
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200742 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200743 ! ( opt.protect_hvr &&
744 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200745 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200746 dropped[id] < DROP_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200747 rand() % opt.delay == 0 ) )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200748 {
Hanno Becker101bcba2018-08-21 16:39:51 +0100749 delay_packet( &cur );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200750 }
751 else
752 {
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200753 /* forward and possibly duplicate */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200754 if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 )
755 return( ret );
756
Hanno Becker101bcba2018-08-21 16:39:51 +0100757 /* send previously delayed messages if any */
758 ret = send_delayed();
759 if( ret != 0 )
760 return( ret );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200761 }
762
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200763 return( 0 );
764}
765
766int main( int argc, char *argv[] )
767{
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100768 int ret = 1;
769 int exit_code = MBEDTLS_EXIT_FAILURE;
Hanno Becker01ea7782018-08-17 13:33:41 +0100770 uint8_t delay_idx;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200771
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200772 mbedtls_net_context listen_fd, client_fd, server_fd;
Hanno Becker77abef52017-11-02 10:50:28 +0000773
774#if defined( MBEDTLS_TIMING_C )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100775 struct timeval tm;
Hanno Becker77abef52017-11-02 10:50:28 +0000776#endif
777
778 struct timeval *tm_ptr = NULL;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200779
780 int nb_fds;
781 fd_set read_fds;
782
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200783 mbedtls_net_init( &listen_fd );
784 mbedtls_net_init( &client_fd );
785 mbedtls_net_init( &server_fd );
786
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200787 get_options( argc, argv );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200788
789 /*
790 * Decisions to drop/delay/duplicate packets are pseudo-random: dropping
791 * exactly 1 in N packets would lead to problems when a flight has exactly
792 * N packets: the same packet would be dropped on every resend.
793 *
794 * In order to be able to reproduce problems reliably, the seed may be
795 * specified explicitly.
796 */
797 if( opt.seed == 0 )
798 {
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200799 opt.seed = (unsigned int) time( NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 mbedtls_printf( " . Pseudo-random seed: %u\n", opt.seed );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200801 }
802
803 srand( opt.seed );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200804
805 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200806 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200807 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200808 mbedtls_printf( " . Connect to server on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200809 opt.server_addr, opt.server_port );
810 fflush( stdout );
811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
813 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200816 goto exit;
817 }
818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200820
821 /*
822 * 1. Setup the "listening" UDP socket
823 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200824 mbedtls_printf( " . Bind on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200825 opt.listen_addr, opt.listen_port );
826 fflush( stdout );
827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 if( ( ret = mbedtls_net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
829 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200832 goto exit;
833 }
834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200836
837 /*
838 * 2. Wait until a client connects
839 */
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200840accept:
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200841 mbedtls_net_free( &client_fd );
842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 mbedtls_printf( " . Waiting for a remote connection ..." );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200844 fflush( stdout );
845
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200846 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200847 NULL, 0, NULL ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 mbedtls_printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200850 goto exit;
851 }
852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200854
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200855 /*
856 * 3. Forward packets forever (kill the process to terminate it)
857 */
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200858 clear_pending();
859 memset( dropped, 0, sizeof( dropped ) );
860
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200861 nb_fds = client_fd.fd;
862 if( nb_fds < server_fd.fd )
863 nb_fds = server_fd.fd;
864 if( nb_fds < listen_fd.fd )
865 nb_fds = listen_fd.fd;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200866 ++nb_fds;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200867
Hanno Becker0cc77742017-10-31 14:10:07 +0000868#if defined(MBEDTLS_TIMING_C)
Hanno Becker211f44c2017-10-31 14:08:10 +0000869 if( opt.pack > 0 )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100870 {
871 outbuf[0].ctx = &server_fd;
872 outbuf[0].description = "S <- C";
873 outbuf[0].num_datagrams = 0;
874 outbuf[0].len = 0;
875
876 outbuf[1].ctx = &client_fd;
877 outbuf[1].description = "S -> C";
878 outbuf[1].num_datagrams = 0;
879 outbuf[1].len = 0;
880 }
Hanno Becker0cc77742017-10-31 14:10:07 +0000881#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100882
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200883 while( 1 )
884 {
Hanno Becker77abef52017-11-02 10:50:28 +0000885#if defined(MBEDTLS_TIMING_C)
886 if( opt.pack > 0 )
887 {
888 unsigned max_wait_server, max_wait_client, max_wait;
889 max_wait_server = ctx_buffer_time_remaining( &outbuf[0] );
890 max_wait_client = ctx_buffer_time_remaining( &outbuf[1] );
891
892 max_wait = (unsigned) -1;
893
894 if( max_wait_server == 0 )
895 ctx_buffer_flush( &outbuf[0] );
896 else
897 max_wait = max_wait_server;
898
899 if( max_wait_client == 0 )
900 ctx_buffer_flush( &outbuf[1] );
901 else
902 {
903 if( max_wait_client < max_wait )
904 max_wait = max_wait_client;
905 }
906
907 if( max_wait != (unsigned) -1 )
908 {
909 tm.tv_sec = max_wait / 1000;
910 tm.tv_usec = ( max_wait % 1000 ) * 1000;
911
912 tm_ptr = &tm;
913 }
914 else
915 {
916 tm_ptr = NULL;
917 }
918 }
919#endif /* MBEDTLS_TIMING_C */
920
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200921 FD_ZERO( &read_fds );
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200922 FD_SET( server_fd.fd, &read_fds );
923 FD_SET( client_fd.fd, &read_fds );
924 FD_SET( listen_fd.fd, &read_fds );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200925
Hanno Becker77abef52017-11-02 10:50:28 +0000926 if( ( ret = select( nb_fds, &read_fds, NULL, NULL, tm_ptr ) ) < 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200927 {
928 perror( "select" );
929 goto exit;
930 }
931
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200932 if( FD_ISSET( listen_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200933 goto accept;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200934
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200935 if( FD_ISSET( client_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200936 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200937 if( ( ret = handle_message( "S <- C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200938 &server_fd, &client_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200939 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200940 }
941
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200942 if( FD_ISSET( server_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200943 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200944 if( ( ret = handle_message( "S -> C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200945 &client_fd, &server_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200946 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200947 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100948
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200949 }
950
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100951 exit_code = MBEDTLS_EXIT_SUCCESS;
952
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200953exit:
954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955#ifdef MBEDTLS_ERROR_C
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100956 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200957 {
958 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 mbedtls_strerror( ret, error_buf, 100 );
960 mbedtls_printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200961 fflush( stdout );
962 }
963#endif
964
Hanno Becker01ea7782018-08-17 13:33:41 +0100965 for( delay_idx = 0; delay_idx < MAX_DELAYED_HS; delay_idx++ )
966 {
967 mbedtls_free( opt.delay_cli + delay_idx );
968 mbedtls_free( opt.delay_srv + delay_idx );
969 }
970
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200971 mbedtls_net_free( &client_fd );
972 mbedtls_net_free( &server_fd );
973 mbedtls_net_free( &listen_fd );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200974
975#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 mbedtls_printf( " Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200977 fflush( stdout ); getchar();
978#endif
979
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100980 return( exit_code );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200981}
982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983#endif /* MBEDTLS_NET_C */