blob: 7447571f4fb38c8a8202e4794b8768698cdec530 [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é-Gonnardb46780e2014-09-23 12:17:30 +020082#define MAX_MSG_SIZE 16384 + 2048 /* max record/datagram size */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020083
84#define DFL_SERVER_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020085#define DFL_SERVER_PORT "4433"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020086#define DFL_LISTEN_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020087#define DFL_LISTEN_PORT "5556"
Hanno Becker1dd62ea2017-05-22 14:30:59 +010088#define DFL_PACK 0
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020089
Hanno Becker0cc77742017-10-31 14:10:07 +000090#if defined(MBEDTLS_TIMING_C)
91#define USAGE_PACK \
92 " pack=%%d default: 0 (don't pack)\n" \
93 " options: t > 0 (pack for t milliseconds)\n"
94#else
95#define USAGE_PACK
96#endif
97
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020098#define USAGE \
99 "\n usage: udp_proxy param=<>...\n" \
100 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200101 " server_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200102 " server_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200103 " listen_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200104 " listen_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200105 "\n" \
106 " duplicate=%%d default: 0 (no duplication)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200107 " duplicate about 1:N packets randomly\n" \
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200108 " delay=%%d default: 0 (no delayed packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200109 " delay about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200110 " delay_ccs=0/1 default: 0 (don't delay ChangeCipherSpec)\n" \
Hanno Becker01ea7782018-08-17 13:33:41 +0100111 " delay_cli=%%s Handshake message from client that should be\n"\
112 " delayed. Possible values are 'ClientHello',\n" \
113 " 'Certificate', 'CertificateVerify', and\n" \
114 " 'ClientKeyExchange'.\n" \
115 " May be used multiple times, even for the same\n"\
116 " message, in which case the respective message\n"\
117 " gets delayed multiple times.\n" \
118 " delay_srv=%%s Handshake message from server that should be\n"\
119 " delayed. Possible values are 'HelloRequest',\n"\
120 " 'ServerHello', 'ServerHelloDone', 'Certificate'\n"\
121 " 'ServerKeyExchange', 'NewSessionTicket',\n"\
122 " 'HelloVerifyRequest' and ''CertificateRequest'.\n"\
123 " May be used multiple times, even for the same\n"\
124 " message, in which case the respective message\n"\
125 " gets delayed multiple times.\n" \
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200126 " drop=%%d default: 0 (no dropped packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200127 " drop about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200128 " mtu=%%d default: 0 (unlimited)\n" \
129 " drop packets larger than N bytes\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200130 " bad_ad=0/1 default: 0 (don't add bad ApplicationData)\n" \
Hanno Becker98aaf252019-05-24 10:07:42 +0100131 " bad_cid=%%d default: 0 (don't corrupt Connection IDs)\n" \
132 " duplicate 1:N packets containing a CID,\n" \
133 " modifying CID in first instance of the packet.\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200134 " protect_hvr=0/1 default: 0 (don't protect HelloVerifyRequest)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000135 " protect_len=%%d default: (don't protect packets of this size)\n" \
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100136 " inject_clihlo=0/1 default: 0 (don't inject fake ClientHello)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200137 "\n" \
138 " seed=%%d default: (use current time)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000139 USAGE_PACK \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200140 "\n"
141
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200142/*
143 * global options
144 */
Hanno Becker01ea7782018-08-17 13:33:41 +0100145
146#define MAX_DELAYED_HS 10
147
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200148static struct options
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200149{
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200150 const char *server_addr; /* address to forward packets to */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200151 const char *server_port; /* port to forward packets to */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200152 const char *listen_addr; /* address for accepting client connections */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200153 const char *listen_port; /* port for accepting client connections */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200154
155 int duplicate; /* duplicate 1 in N packets (none if 0) */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200156 int delay; /* delay 1 packet in N (none if 0) */
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200157 int delay_ccs; /* delay ChangeCipherSpec */
Hanno Becker01ea7782018-08-17 13:33:41 +0100158 char* delay_cli[MAX_DELAYED_HS]; /* handshake types of messages from
Hanno Becker41038102018-08-28 11:15:32 +0100159 * client that should be delayed. */
Hanno Becker01ea7782018-08-17 13:33:41 +0100160 uint8_t delay_cli_cnt; /* Number of entries in delay_cli. */
161 char* delay_srv[MAX_DELAYED_HS]; /* handshake types of messages from
Hanno Becker41038102018-08-28 11:15:32 +0100162 * server that should be delayed. */
Hanno Becker01ea7782018-08-17 13:33:41 +0100163 uint8_t delay_srv_cnt; /* Number of entries in delay_srv. */
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200164 int drop; /* drop 1 packet in N (none if 0) */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200165 int mtu; /* drop packets larger than this */
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200166 int bad_ad; /* inject corrupted ApplicationData record */
Hanno Becker98aaf252019-05-24 10:07:42 +0100167 unsigned bad_cid; /* inject corrupted CID record */
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200168 int protect_hvr; /* never drop or delay HelloVerifyRequest */
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200169 int protect_len; /* never drop/delay packet of the given size*/
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100170 int inject_clihlo; /* inject fake ClientHello after handshake */
Hanno Becker77abef52017-11-02 10:50:28 +0000171 unsigned pack; /* merge packets into single datagram for
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100172 * at most \c merge milliseconds if > 0 */
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200173 unsigned int seed; /* seed for "random" events */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200174} opt;
175
176static void exit_usage( const char *name, const char *value )
177{
178 if( value == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 mbedtls_printf( " unknown option or missing value: %s\n", name );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200180 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 mbedtls_printf( " option %s: illegal value: %s\n", name, value );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 mbedtls_printf( USAGE );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200184 exit( 1 );
185}
186
187static void get_options( int argc, char *argv[] )
188{
189 int i;
190 char *p, *q;
191
192 opt.server_addr = DFL_SERVER_ADDR;
193 opt.server_port = DFL_SERVER_PORT;
194 opt.listen_addr = DFL_LISTEN_ADDR;
195 opt.listen_port = DFL_LISTEN_PORT;
Hanno Becker211f44c2017-10-31 14:08:10 +0000196 opt.pack = DFL_PACK;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200197 /* Other members default to 0 */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200198
Hanno Becker01ea7782018-08-17 13:33:41 +0100199 opt.delay_cli_cnt = 0;
200 opt.delay_srv_cnt = 0;
201 memset( opt.delay_cli, 0, sizeof( opt.delay_cli ) );
202 memset( opt.delay_srv, 0, sizeof( opt.delay_srv ) );
203
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200204 for( i = 1; i < argc; i++ )
205 {
206 p = argv[i];
207 if( ( q = strchr( p, '=' ) ) == NULL )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200208 exit_usage( p, NULL );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200209 *q++ = '\0';
210
211 if( strcmp( p, "server_addr" ) == 0 )
212 opt.server_addr = q;
213 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200214 opt.server_port = q;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200215 else if( strcmp( p, "listen_addr" ) == 0 )
216 opt.listen_addr = q;
217 else if( strcmp( p, "listen_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200218 opt.listen_port = q;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200219 else if( strcmp( p, "duplicate" ) == 0 )
220 {
221 opt.duplicate = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200222 if( opt.duplicate < 0 || opt.duplicate > 20 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200223 exit_usage( p, q );
224 }
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200225 else if( strcmp( p, "delay" ) == 0 )
226 {
227 opt.delay = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200228 if( opt.delay < 0 || opt.delay > 20 || opt.delay == 1 )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200229 exit_usage( p, q );
230 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200231 else if( strcmp( p, "delay_ccs" ) == 0 )
232 {
233 opt.delay_ccs = atoi( q );
234 if( opt.delay_ccs < 0 || opt.delay_ccs > 1 )
235 exit_usage( p, q );
236 }
Hanno Becker01ea7782018-08-17 13:33:41 +0100237 else if( strcmp( p, "delay_cli" ) == 0 ||
238 strcmp( p, "delay_srv" ) == 0 )
239 {
240 uint8_t *delay_cnt;
241 char **delay_list;
242 size_t len;
243 char *buf;
244
245 if( strcmp( p, "delay_cli" ) == 0 )
246 {
247 delay_cnt = &opt.delay_cli_cnt;
248 delay_list = opt.delay_cli;
249 }
250 else
251 {
252 delay_cnt = &opt.delay_srv_cnt;
253 delay_list = opt.delay_srv;
254 }
255
256 if( *delay_cnt == MAX_DELAYED_HS )
257 {
Hanno Beckerf34a4c12018-08-28 17:22:26 +0100258 mbedtls_printf( " too many uses of %s: only %d allowed\n",
259 p, MAX_DELAYED_HS );
Hanno Becker01ea7782018-08-17 13:33:41 +0100260 exit_usage( p, NULL );
261 }
262
263 len = strlen( q );
264 buf = mbedtls_calloc( 1, len + 1 );
265 if( buf == NULL )
266 {
267 mbedtls_printf( " Allocation failure\n" );
268 exit( 1 );
269 }
270 memcpy( buf, q, len + 1 );
271
272 delay_list[ (*delay_cnt)++ ] = buf;
273 }
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200274 else if( strcmp( p, "drop" ) == 0 )
275 {
276 opt.drop = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200277 if( opt.drop < 0 || opt.drop > 20 || opt.drop == 1 )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200278 exit_usage( p, q );
279 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100280 else if( strcmp( p, "pack" ) == 0 )
281 {
Hanno Becker0cc77742017-10-31 14:10:07 +0000282#if defined(MBEDTLS_TIMING_C)
Hanno Becker77abef52017-11-02 10:50:28 +0000283 opt.pack = (unsigned) atoi( q );
Hanno Becker0cc77742017-10-31 14:10:07 +0000284#else
285 mbedtls_printf( " option pack only defined if MBEDTLS_TIMING_C is enabled\n" );
286 exit( 1 );
287#endif
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100288 }
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200289 else if( strcmp( p, "mtu" ) == 0 )
290 {
291 opt.mtu = atoi( q );
292 if( opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE )
293 exit_usage( p, q );
294 }
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200295 else if( strcmp( p, "bad_ad" ) == 0 )
296 {
297 opt.bad_ad = atoi( q );
298 if( opt.bad_ad < 0 || opt.bad_ad > 1 )
299 exit_usage( p, q );
300 }
Hanno Becker98aaf252019-05-24 10:07:42 +0100301#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
302 else if( strcmp( p, "bad_cid" ) == 0 )
303 {
304 opt.bad_cid = (unsigned) atoi( q );
305 }
306#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200307 else if( strcmp( p, "protect_hvr" ) == 0 )
308 {
309 opt.protect_hvr = atoi( q );
310 if( opt.protect_hvr < 0 || opt.protect_hvr > 1 )
311 exit_usage( p, q );
312 }
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200313 else if( strcmp( p, "protect_len" ) == 0 )
314 {
315 opt.protect_len = atoi( q );
316 if( opt.protect_len < 0 )
317 exit_usage( p, q );
318 }
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100319 else if( strcmp( p, "inject_clihlo" ) == 0 )
320 {
321 opt.inject_clihlo = atoi( q );
322 if( opt.inject_clihlo < 0 || opt.inject_clihlo > 1 )
323 exit_usage( p, q );
324 }
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200325 else if( strcmp( p, "seed" ) == 0 )
326 {
327 opt.seed = atoi( q );
328 if( opt.seed == 0 )
329 exit_usage( p, q );
330 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200331 else
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200332 exit_usage( p, NULL );
333 }
334}
335
336static const char *msg_type( unsigned char *msg, size_t len )
337{
338 if( len < 1 ) return( "Invalid" );
339 switch( msg[0] )
340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" );
342 case MBEDTLS_SSL_MSG_ALERT: return( "Alert" );
343 case MBEDTLS_SSL_MSG_APPLICATION_DATA: return( "ApplicationData" );
Hanno Becker31f6e372019-05-08 15:36:31 +0100344 case MBEDTLS_SSL_MSG_CID: return( "CID" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 case MBEDTLS_SSL_MSG_HANDSHAKE: break; /* See below */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200346 default: return( "Unknown" );
347 }
348
Manuel Pégourié-Gonnard8cc7e032014-09-25 12:59:05 +0200349 if( len < 13 + 12 ) return( "Invalid handshake" );
350
351 /*
352 * Our handshake message are less than 2^16 bytes long, so they should
353 * have 0 as the first byte of length, frag_offset and frag_length.
354 * Otherwise, assume they are encrypted.
355 */
356 if( msg[14] || msg[19] || msg[22] ) return( "Encrypted handshake" );
357
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200358 switch( msg[13] )
359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 case MBEDTLS_SSL_HS_HELLO_REQUEST: return( "HelloRequest" );
361 case MBEDTLS_SSL_HS_CLIENT_HELLO: return( "ClientHello" );
362 case MBEDTLS_SSL_HS_SERVER_HELLO: return( "ServerHello" );
363 case MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" );
364 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" );
365 case MBEDTLS_SSL_HS_CERTIFICATE: return( "Certificate" );
366 case MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" );
367 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" );
368 case MBEDTLS_SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" );
369 case MBEDTLS_SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" );
370 case MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" );
371 case MBEDTLS_SSL_HS_FINISHED: return( "Finished" );
Manuel Pégourié-Gonnardbc010a02014-09-20 12:40:51 +0200372 default: return( "Unknown handshake" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200373 }
374}
375
Hanno Becker0cc77742017-10-31 14:10:07 +0000376#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200377/* Return elapsed time in milliseconds since the first call */
Hanno Becker77abef52017-11-02 10:50:28 +0000378static unsigned ellapsed_time( void )
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200379{
Hanno Becker92474da2017-10-31 14:09:30 +0000380 static int initialized = 0;
381 static struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200382
Hanno Becker92474da2017-10-31 14:09:30 +0000383 if( initialized == 0 )
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200384 {
Hanno Becker92474da2017-10-31 14:09:30 +0000385 (void) mbedtls_timing_get_timer( &hires, 1 );
386 initialized = 1;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200387 return( 0 );
388 }
389
Hanno Becker92474da2017-10-31 14:09:30 +0000390 return( mbedtls_timing_get_timer( &hires, 0 ) );
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200391}
392
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200393typedef struct
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200394{
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100395 mbedtls_net_context *ctx;
396
397 const char *description;
398
Hanno Becker77abef52017-11-02 10:50:28 +0000399 unsigned packet_lifetime;
400 unsigned num_datagrams;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100401
402 unsigned char data[MAX_MSG_SIZE];
Hanno Beckera5e68972017-12-06 08:35:02 +0000403 size_t len;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100404
405} ctx_buffer;
406
407static ctx_buffer outbuf[2];
408
409static int ctx_buffer_flush( ctx_buffer *buf )
410{
411 int ret;
412
Hanno Becker77abef52017-11-02 10:50:28 +0000413 mbedtls_printf( " %05u flush %s: %u bytes, %u datagrams, last %u ms\n",
414 ellapsed_time(), buf->description,
Hanno Beckera5e68972017-12-06 08:35:02 +0000415 (unsigned) buf->len, buf->num_datagrams,
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100416 ellapsed_time() - buf->packet_lifetime );
417
418 ret = mbedtls_net_send( buf->ctx, buf->data, buf->len );
419
420 buf->len = 0;
421 buf->num_datagrams = 0;
422
423 return( ret );
424}
425
Hanno Becker77abef52017-11-02 10:50:28 +0000426static unsigned ctx_buffer_time_remaining( ctx_buffer *buf )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100427{
Hanno Becker77abef52017-11-02 10:50:28 +0000428 unsigned const cur_time = ellapsed_time();
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100429
Hanno Becker77abef52017-11-02 10:50:28 +0000430 if( buf->num_datagrams == 0 )
431 return( (unsigned) -1 );
432
433 if( cur_time - buf->packet_lifetime >= opt.pack )
434 return( 0 );
435
436 return( opt.pack - ( cur_time - buf->packet_lifetime ) );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100437}
438
439static int ctx_buffer_append( ctx_buffer *buf,
440 const unsigned char * data,
441 size_t len )
442{
443 int ret;
444
Hanno Beckera5e68972017-12-06 08:35:02 +0000445 if( len > (size_t) INT_MAX )
446 return( -1 );
447
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100448 if( len > sizeof( buf->data ) )
449 {
Hanno Becker77abef52017-11-02 10:50:28 +0000450 mbedtls_printf( " ! buffer size %u too large (max %u)\n",
451 (unsigned) len, (unsigned) sizeof( buf->data ) );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100452 return( -1 );
453 }
454
455 if( sizeof( buf->data ) - buf->len < len )
456 {
457 if( ( ret = ctx_buffer_flush( buf ) ) <= 0 )
Hanno Becker31f6e372019-05-08 15:36:31 +0100458 {
459 mbedtls_printf( "ctx_buffer_flush failed with -%#04x", -ret );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100460 return( ret );
Hanno Becker31f6e372019-05-08 15:36:31 +0100461 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100462 }
463
464 memcpy( buf->data + buf->len, data, len );
465
466 buf->len += len;
467 if( ++buf->num_datagrams == 1 )
468 buf->packet_lifetime = ellapsed_time();
469
Hanno Beckera5e68972017-12-06 08:35:02 +0000470 return( (int) len );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100471}
Hanno Becker77abef52017-11-02 10:50:28 +0000472#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100473
474static int dispatch_data( mbedtls_net_context *ctx,
475 const unsigned char * data,
476 size_t len )
477{
Hanno Becker31f6e372019-05-08 15:36:31 +0100478 int ret;
Hanno Becker77abef52017-11-02 10:50:28 +0000479#if defined(MBEDTLS_TIMING_C)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100480 ctx_buffer *buf = NULL;
Hanno Becker77abef52017-11-02 10:50:28 +0000481 if( opt.pack > 0 )
482 {
483 if( outbuf[0].ctx == ctx )
484 buf = &outbuf[0];
485 else if( outbuf[1].ctx == ctx )
486 buf = &outbuf[1];
Hanno Becker0cc77742017-10-31 14:10:07 +0000487
Hanno Becker77abef52017-11-02 10:50:28 +0000488 if( buf == NULL )
489 return( -1 );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100490
Hanno Becker77abef52017-11-02 10:50:28 +0000491 return( ctx_buffer_append( buf, data, len ) );
492 }
493#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100494
Hanno Becker31f6e372019-05-08 15:36:31 +0100495 ret = mbedtls_net_send( ctx, data, len );
496 if( ret < 0 )
497 {
498 mbedtls_printf( "net_send returned -%#04x\n", -ret );
499 }
500 return( ret );
Hanno Becker0cc77742017-10-31 14:10:07 +0000501}
502
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100503typedef struct
504{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200505 mbedtls_net_context *dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200506 const char *way;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200507 const char *type;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200508 unsigned len;
509 unsigned char buf[MAX_MSG_SIZE];
510} packet;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200511
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200512/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */
513void print_packet( const packet *p, const char *why )
514{
Hanno Becker0cc77742017-10-31 14:10:07 +0000515#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200516 if( why == NULL )
Hanno Becker77abef52017-11-02 10:50:28 +0000517 mbedtls_printf( " %05u dispatch %s %s (%u bytes)\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200518 ellapsed_time(), p->way, p->type, p->len );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200519 else
Hanno Becker77abef52017-11-02 10:50:28 +0000520 mbedtls_printf( " %05u dispatch %s %s (%u bytes): %s\n",
Hanno Becker0cc77742017-10-31 14:10:07 +0000521 ellapsed_time(), p->way, p->type, p->len, why );
522#else
523 if( why == NULL )
524 mbedtls_printf( " dispatch %s %s (%u bytes)\n",
525 p->way, p->type, p->len );
526 else
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100527 mbedtls_printf( " dispatch %s %s (%u bytes): %s\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200528 p->way, p->type, p->len, why );
Hanno Becker0cc77742017-10-31 14:10:07 +0000529#endif
530
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200531 fflush( stdout );
532}
533
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100534/*
535 * In order to test the server's behaviour when receiving a ClientHello after
536 * the connection is established (this could be a hard reset from the client,
537 * but the server must not drop the existing connection before establishing
538 * client reachability, see RFC 6347 Section 4.2.8), we memorize the first
539 * ClientHello we see (which can't have a cookie), then replay it after the
540 * first ApplicationData record - then we're done.
541 *
542 * This is controlled by the inject_clihlo option.
543 *
544 * We want an explicit state and a place to store the packet.
545 */
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200546typedef enum {
547 ICH_INIT, /* haven't seen the first ClientHello yet */
548 ICH_CACHED, /* cached the initial ClientHello */
549 ICH_INJECTED, /* ClientHello already injected, done */
550} inject_clihlo_state_t;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100551
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200552static inject_clihlo_state_t inject_clihlo_state;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100553static packet initial_clihlo;
554
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200555int send_packet( const packet *p, const char *why )
556{
557 int ret;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200558 mbedtls_net_context *dst = p->dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200559
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100560 /* save initial ClientHello? */
561 if( opt.inject_clihlo != 0 &&
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200562 inject_clihlo_state == ICH_INIT &&
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100563 strcmp( p->type, "ClientHello" ) == 0 )
564 {
565 memcpy( &initial_clihlo, p, sizeof( packet ) );
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200566 inject_clihlo_state = ICH_CACHED;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100567 }
568
Hanno Becker98aaf252019-05-24 10:07:42 +0100569 /* insert corrupted CID record? */
570 if( opt.bad_cid != 0 &&
571 strcmp( p->type, "CID" ) == 0 &&
572 ( rand() % opt.bad_cid ) == 0 )
573 {
574 unsigned char buf[MAX_MSG_SIZE];
575 memcpy( buf, p->buf, p->len );
576
577 /* The CID resides at offset 11 in the DTLS record header. */
578 buf[11] ^= 1;
579 print_packet( p, "modified CID" );
580
581 if( ( ret = dispatch_data( dst, buf, p->len ) ) <= 0 )
582 {
583 mbedtls_printf( " ! dispatch returned %d\n", ret );
584 return( ret );
585 }
586 }
587
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200588 /* insert corrupted ApplicationData record? */
589 if( opt.bad_ad &&
590 strcmp( p->type, "ApplicationData" ) == 0 )
591 {
592 unsigned char buf[MAX_MSG_SIZE];
593 memcpy( buf, p->buf, p->len );
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200594
Hanno Beckerfbb0b702017-05-26 16:55:07 +0100595 if( p->len <= 13 )
596 {
597 mbedtls_printf( " ! can't corrupt empty AD record" );
598 }
599 else
600 {
601 ++buf[13];
602 print_packet( p, "corrupted" );
603 }
604
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100605 if( ( ret = dispatch_data( dst, buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200606 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100607 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200608 return( ret );
609 }
610 }
611
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200612 print_packet( p, why );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100613 if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200614 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100615 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200616 return( ret );
617 }
618
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200619 /* Don't duplicate Application Data, only handshake covered */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200620 if( opt.duplicate != 0 &&
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200621 strcmp( p->type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200622 rand() % opt.duplicate == 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200623 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200624 print_packet( p, "duplicated" );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200625
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100626 if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200627 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100628 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200629 return( ret );
630 }
631 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200632
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100633 /* Inject ClientHello after first ApplicationData */
634 if( opt.inject_clihlo != 0 &&
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200635 inject_clihlo_state == ICH_CACHED &&
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100636 strcmp( p->type, "ApplicationData" ) == 0 )
637 {
638 print_packet( &initial_clihlo, "injected" );
639
640 if( ( ret = dispatch_data( dst, initial_clihlo.buf,
641 initial_clihlo.len ) ) <= 0 )
642 {
643 mbedtls_printf( " ! dispatch returned %d\n", ret );
644 return( ret );
645 }
646
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200647 inject_clihlo_state = ICH_INJECTED;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100648 }
649
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200650 return( 0 );
651}
652
Hanno Becker101bcba2018-08-21 16:39:51 +0100653#define MAX_DELAYED_MSG 5
654static size_t prev_len;
655static packet prev[MAX_DELAYED_MSG];
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200656
657void clear_pending( void )
658{
Hanno Becker12b72c12018-08-23 13:15:36 +0100659 memset( &prev, 0, sizeof( prev ) );
Hanno Becker101bcba2018-08-21 16:39:51 +0100660 prev_len = 0;
661}
662
663void delay_packet( packet *delay )
664{
665 if( prev_len == MAX_DELAYED_MSG )
666 return;
667
668 memcpy( &prev[prev_len++], delay, sizeof( packet ) );
669}
670
671int send_delayed()
672{
673 uint8_t offset;
674 int ret;
675 for( offset = 0; offset < prev_len; offset++ )
676 {
677 ret = send_packet( &prev[offset], "delayed" );
678 if( ret != 0 )
679 return( ret );
680 }
681
682 clear_pending();
683 return( 0 );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200684}
685
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200686/*
687 * Avoid dropping or delaying a packet that was already dropped twice: this
688 * only results in uninteresting timeouts. We can't rely on type to identify
689 * packets, since during renegotiation they're all encrypted. So, rely on
690 * size mod 2048 (which is usually just size).
691 */
692static unsigned char dropped[2048] = { 0 };
693#define DROP_MAX 2
694
Hanno Becker961e6772019-06-04 13:04:28 +0100695/* We only drop packets at the level of entire datagrams, not at the level
696 * of records. In particular, if the peer changes the way it packs multiple
697 * records into a single datagram, we don't necessarily count the number of
698 * times a record has been dropped correctly. However, the only known reason
699 * why a peer would change datagram packing is disabling the latter on
700 * retransmission, in which case we'd drop involved records at most
701 * DROP_MAX + 1 times. */
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200702void update_dropped( const packet *p )
703{
704 size_t id = p->len % sizeof( dropped );
Manuel Pégourié-Gonnardfa60f122014-09-26 16:07:29 +0200705 ++dropped[id];
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200706}
707
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200708int handle_message( const char *way,
709 mbedtls_net_context *dst,
710 mbedtls_net_context *src )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200711{
712 int ret;
713 packet cur;
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200714 size_t id;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200715
Hanno Becker01ea7782018-08-17 13:33:41 +0100716 uint8_t delay_idx;
717 char ** delay_list;
718 uint8_t delay_list_len;
719
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +0200720 /* receive packet */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200721 if( ( ret = mbedtls_net_recv( src, cur.buf, sizeof( cur.buf ) ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 mbedtls_printf( " ! mbedtls_net_recv returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200724 return( ret );
725 }
726
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200727 cur.len = ret;
728 cur.type = msg_type( cur.buf, cur.len );
729 cur.way = way;
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200730 cur.dst = dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200731 print_packet( &cur, NULL );
732
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200733 id = cur.len % sizeof( dropped );
734
Hanno Becker01ea7782018-08-17 13:33:41 +0100735 if( strcmp( way, "S <- C" ) == 0 )
736 {
737 delay_list = opt.delay_cli;
738 delay_list_len = opt.delay_cli_cnt;
739 }
740 else
741 {
742 delay_list = opt.delay_srv;
743 delay_list_len = opt.delay_srv_cnt;
744 }
Hanno Beckercf469452018-08-28 10:09:47 +0100745
Hanno Becker01ea7782018-08-17 13:33:41 +0100746 /* Check if message type is in the list of messages
747 * that should be delayed */
748 for( delay_idx = 0; delay_idx < delay_list_len; delay_idx++ )
749 {
750 if( delay_list[ delay_idx ] == NULL )
751 continue;
752
753 if( strcmp( delay_list[ delay_idx ], cur.type ) == 0 )
754 {
755 /* Delay message */
Hanno Becker101bcba2018-08-21 16:39:51 +0100756 delay_packet( &cur );
Hanno Becker01ea7782018-08-17 13:33:41 +0100757
758 /* Remove entry from list */
759 mbedtls_free( delay_list[delay_idx] );
760 delay_list[delay_idx] = NULL;
761
762 return( 0 );
763 }
764 }
765
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200766 /* do we want to drop, delay, or forward it? */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200767 if( ( opt.mtu != 0 &&
768 cur.len > (unsigned) opt.mtu ) ||
769 ( opt.drop != 0 &&
Hanno Becker31f6e372019-05-08 15:36:31 +0100770 strcmp( cur.type, "CID" ) != 0 &&
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200771 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200772 ! ( opt.protect_hvr &&
773 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200774 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200775 dropped[id] < DROP_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200776 rand() % opt.drop == 0 ) )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200777 {
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200778 update_dropped( &cur );
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200779 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200780 else if( ( opt.delay_ccs == 1 &&
781 strcmp( cur.type, "ChangeCipherSpec" ) == 0 ) ||
782 ( opt.delay != 0 &&
Hanno Becker31f6e372019-05-08 15:36:31 +0100783 strcmp( cur.type, "CID" ) != 0 &&
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200784 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200785 ! ( opt.protect_hvr &&
786 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200787 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200788 dropped[id] < DROP_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200789 rand() % opt.delay == 0 ) )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200790 {
Hanno Becker101bcba2018-08-21 16:39:51 +0100791 delay_packet( &cur );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200792 }
793 else
794 {
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200795 /* forward and possibly duplicate */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200796 if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 )
797 return( ret );
798
Hanno Becker101bcba2018-08-21 16:39:51 +0100799 /* send previously delayed messages if any */
800 ret = send_delayed();
801 if( ret != 0 )
802 return( ret );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200803 }
804
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200805 return( 0 );
806}
807
808int main( int argc, char *argv[] )
809{
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100810 int ret = 1;
811 int exit_code = MBEDTLS_EXIT_FAILURE;
Hanno Becker01ea7782018-08-17 13:33:41 +0100812 uint8_t delay_idx;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200813
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200814 mbedtls_net_context listen_fd, client_fd, server_fd;
Hanno Becker77abef52017-11-02 10:50:28 +0000815
816#if defined( MBEDTLS_TIMING_C )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100817 struct timeval tm;
Hanno Becker77abef52017-11-02 10:50:28 +0000818#endif
819
820 struct timeval *tm_ptr = NULL;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200821
822 int nb_fds;
823 fd_set read_fds;
824
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200825 mbedtls_net_init( &listen_fd );
826 mbedtls_net_init( &client_fd );
827 mbedtls_net_init( &server_fd );
828
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200829 get_options( argc, argv );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200830
831 /*
832 * Decisions to drop/delay/duplicate packets are pseudo-random: dropping
833 * exactly 1 in N packets would lead to problems when a flight has exactly
834 * N packets: the same packet would be dropped on every resend.
835 *
836 * In order to be able to reproduce problems reliably, the seed may be
837 * specified explicitly.
838 */
839 if( opt.seed == 0 )
840 {
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200841 opt.seed = (unsigned int) time( NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 mbedtls_printf( " . Pseudo-random seed: %u\n", opt.seed );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200843 }
844
845 srand( opt.seed );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200846
847 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200848 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200849 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200850 mbedtls_printf( " . Connect to server on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200851 opt.server_addr, opt.server_port );
852 fflush( stdout );
853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
855 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200858 goto exit;
859 }
860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200862
863 /*
864 * 1. Setup the "listening" UDP socket
865 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200866 mbedtls_printf( " . Bind on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200867 opt.listen_addr, opt.listen_port );
868 fflush( stdout );
869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 if( ( ret = mbedtls_net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
871 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200874 goto exit;
875 }
876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200878
879 /*
880 * 2. Wait until a client connects
881 */
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200882accept:
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200883 mbedtls_net_free( &client_fd );
884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 mbedtls_printf( " . Waiting for a remote connection ..." );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200886 fflush( stdout );
887
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200888 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200889 NULL, 0, NULL ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 mbedtls_printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200892 goto exit;
893 }
894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200896
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200897 /*
898 * 3. Forward packets forever (kill the process to terminate it)
899 */
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200900 clear_pending();
901 memset( dropped, 0, sizeof( dropped ) );
902
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200903 nb_fds = client_fd.fd;
904 if( nb_fds < server_fd.fd )
905 nb_fds = server_fd.fd;
906 if( nb_fds < listen_fd.fd )
907 nb_fds = listen_fd.fd;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200908 ++nb_fds;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200909
Hanno Becker0cc77742017-10-31 14:10:07 +0000910#if defined(MBEDTLS_TIMING_C)
Hanno Becker211f44c2017-10-31 14:08:10 +0000911 if( opt.pack > 0 )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100912 {
913 outbuf[0].ctx = &server_fd;
914 outbuf[0].description = "S <- C";
915 outbuf[0].num_datagrams = 0;
916 outbuf[0].len = 0;
917
918 outbuf[1].ctx = &client_fd;
919 outbuf[1].description = "S -> C";
920 outbuf[1].num_datagrams = 0;
921 outbuf[1].len = 0;
922 }
Hanno Becker0cc77742017-10-31 14:10:07 +0000923#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100924
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200925 while( 1 )
926 {
Hanno Becker77abef52017-11-02 10:50:28 +0000927#if defined(MBEDTLS_TIMING_C)
928 if( opt.pack > 0 )
929 {
930 unsigned max_wait_server, max_wait_client, max_wait;
931 max_wait_server = ctx_buffer_time_remaining( &outbuf[0] );
932 max_wait_client = ctx_buffer_time_remaining( &outbuf[1] );
933
934 max_wait = (unsigned) -1;
935
936 if( max_wait_server == 0 )
937 ctx_buffer_flush( &outbuf[0] );
938 else
939 max_wait = max_wait_server;
940
941 if( max_wait_client == 0 )
942 ctx_buffer_flush( &outbuf[1] );
943 else
944 {
945 if( max_wait_client < max_wait )
946 max_wait = max_wait_client;
947 }
948
949 if( max_wait != (unsigned) -1 )
950 {
951 tm.tv_sec = max_wait / 1000;
952 tm.tv_usec = ( max_wait % 1000 ) * 1000;
953
954 tm_ptr = &tm;
955 }
956 else
957 {
958 tm_ptr = NULL;
959 }
960 }
961#endif /* MBEDTLS_TIMING_C */
962
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200963 FD_ZERO( &read_fds );
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200964 FD_SET( server_fd.fd, &read_fds );
965 FD_SET( client_fd.fd, &read_fds );
966 FD_SET( listen_fd.fd, &read_fds );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200967
Hanno Becker77abef52017-11-02 10:50:28 +0000968 if( ( ret = select( nb_fds, &read_fds, NULL, NULL, tm_ptr ) ) < 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200969 {
970 perror( "select" );
971 goto exit;
972 }
973
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200974 if( FD_ISSET( listen_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200975 goto accept;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200976
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200977 if( FD_ISSET( client_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200978 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200979 if( ( ret = handle_message( "S <- C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200980 &server_fd, &client_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200981 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200982 }
983
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200984 if( FD_ISSET( server_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200985 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200986 if( ( ret = handle_message( "S -> C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200987 &client_fd, &server_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200988 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200989 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100990
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200991 }
992
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100993 exit_code = MBEDTLS_EXIT_SUCCESS;
994
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200995exit:
996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997#ifdef MBEDTLS_ERROR_C
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100998 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200999 {
1000 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 mbedtls_strerror( ret, error_buf, 100 );
1002 mbedtls_printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001003 fflush( stdout );
1004 }
1005#endif
1006
Hanno Becker01ea7782018-08-17 13:33:41 +01001007 for( delay_idx = 0; delay_idx < MAX_DELAYED_HS; delay_idx++ )
1008 {
1009 mbedtls_free( opt.delay_cli + delay_idx );
1010 mbedtls_free( opt.delay_srv + delay_idx );
1011 }
1012
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001013 mbedtls_net_free( &client_fd );
1014 mbedtls_net_free( &server_fd );
1015 mbedtls_net_free( &listen_fd );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001016
1017#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 mbedtls_printf( " Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001019 fflush( stdout ); getchar();
1020#endif
1021
Andres Amaya Garcia80081a62018-04-29 21:58:53 +01001022 return( exit_code );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001023}
1024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025#endif /* MBEDTLS_NET_C */