blob: b77994e033b6b46b33b309be6d92678b6c74cbdf [file] [log] [blame]
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001/*
2 * UDP proxy: emulate an unreliable UDP connexion for DTLS testing
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020045 */
46
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020047/*
48 * Warning: this is an internal utility program we use for tests.
49 * It does break some abstractions from the NET layer, and is thus NOT an
50 * example of good general usage.
51 */
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020055#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020057#endif
58
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/platform.h"
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000061#else
Simon Butcherd3138c32016-04-27 01:26:50 +010062#include <stdio.h>
63#include <stdlib.h>
64#include <time.h>
Andres Amaya Garcia80081a62018-04-29 21:58:53 +010065#define mbedtls_time time
66#define mbedtls_time_t time_t
67#define mbedtls_printf printf
Hanno Becker01ea7782018-08-17 13:33:41 +010068#define mbedtls_calloc calloc
69#define mbedtls_free free
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020070#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010071#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia80081a62018-04-29 21:58:53 +010072#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
73#endif /* MBEDTLS_PLATFORM_C */
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#if !defined(MBEDTLS_NET_C)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020076int main( void )
77{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 mbedtls_printf( "MBEDTLS_NET_C not defined.\n" );
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020079 mbedtls_exit( 0 );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020080}
81#else
82
Andres AG788aa4a2016-09-14 14:32:09 +010083#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000084#include "mbedtls/error.h"
85#include "mbedtls/ssl.h"
Hanno Becker0cc77742017-10-31 14:10:07 +000086#include "mbedtls/timing.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020087
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +000088#include <string.h>
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020089
90/* For select() */
91#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
92 !defined(EFI32)
93#include <winsock2.h>
94#include <windows.h>
95#if defined(_MSC_VER)
96#if defined(_WIN32_WCE)
97#pragma comment( lib, "ws2.lib" )
98#else
99#pragma comment( lib, "ws2_32.lib" )
100#endif
101#endif /* _MSC_VER */
102#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
103#include <sys/time.h>
104#include <sys/types.h>
105#include <unistd.h>
106#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
107
Manuel Pégourié-Gonnardb46780e2014-09-23 12:17:30 +0200108#define MAX_MSG_SIZE 16384 + 2048 /* max record/datagram size */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200109
110#define DFL_SERVER_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200111#define DFL_SERVER_PORT "4433"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200112#define DFL_LISTEN_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200113#define DFL_LISTEN_PORT "5556"
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100114#define DFL_PACK 0
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200115
Hanno Becker0cc77742017-10-31 14:10:07 +0000116#if defined(MBEDTLS_TIMING_C)
117#define USAGE_PACK \
118 " pack=%%d default: 0 (don't pack)\n" \
119 " options: t > 0 (pack for t milliseconds)\n"
120#else
121#define USAGE_PACK
122#endif
123
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200124#define USAGE \
125 "\n usage: udp_proxy param=<>...\n" \
126 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200127 " server_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200128 " server_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200129 " listen_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200130 " listen_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200131 "\n" \
132 " duplicate=%%d default: 0 (no duplication)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200133 " duplicate about 1:N packets randomly\n" \
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200134 " delay=%%d default: 0 (no delayed packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200135 " delay about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200136 " delay_ccs=0/1 default: 0 (don't delay ChangeCipherSpec)\n" \
Hanno Becker01ea7782018-08-17 13:33:41 +0100137 " delay_cli=%%s Handshake message from client that should be\n"\
138 " delayed. Possible values are 'ClientHello',\n" \
139 " 'Certificate', 'CertificateVerify', and\n" \
140 " 'ClientKeyExchange'.\n" \
141 " May be used multiple times, even for the same\n"\
142 " message, in which case the respective message\n"\
143 " gets delayed multiple times.\n" \
144 " delay_srv=%%s Handshake message from server that should be\n"\
145 " delayed. Possible values are 'HelloRequest',\n"\
146 " 'ServerHello', 'ServerHelloDone', 'Certificate'\n"\
147 " 'ServerKeyExchange', 'NewSessionTicket',\n"\
148 " 'HelloVerifyRequest' and ''CertificateRequest'.\n"\
149 " May be used multiple times, even for the same\n"\
150 " message, in which case the respective message\n"\
151 " gets delayed multiple times.\n" \
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200152 " drop=%%d default: 0 (no dropped packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200153 " drop about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200154 " mtu=%%d default: 0 (unlimited)\n" \
155 " drop packets larger than N bytes\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200156 " bad_ad=0/1 default: 0 (don't add bad ApplicationData)\n" \
157 " protect_hvr=0/1 default: 0 (don't protect HelloVerifyRequest)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000158 " protect_len=%%d default: (don't protect packets of this size)\n" \
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100159 " inject_clihlo=0/1 default: 0 (don't inject fake ClientHello)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200160 "\n" \
161 " seed=%%d default: (use current time)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000162 USAGE_PACK \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200163 "\n"
164
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200165/*
166 * global options
167 */
Hanno Becker01ea7782018-08-17 13:33:41 +0100168
169#define MAX_DELAYED_HS 10
170
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200171static struct options
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200172{
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200173 const char *server_addr; /* address to forward packets to */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200174 const char *server_port; /* port to forward packets to */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200175 const char *listen_addr; /* address for accepting client connections */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200176 const char *listen_port; /* port for accepting client connections */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200177
178 int duplicate; /* duplicate 1 in N packets (none if 0) */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200179 int delay; /* delay 1 packet in N (none if 0) */
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200180 int delay_ccs; /* delay ChangeCipherSpec */
Hanno Becker01ea7782018-08-17 13:33:41 +0100181 char* delay_cli[MAX_DELAYED_HS]; /* handshake types of messages from
Hanno Becker41038102018-08-28 11:15:32 +0100182 * client that should be delayed. */
Hanno Becker01ea7782018-08-17 13:33:41 +0100183 uint8_t delay_cli_cnt; /* Number of entries in delay_cli. */
184 char* delay_srv[MAX_DELAYED_HS]; /* handshake types of messages from
Hanno Becker41038102018-08-28 11:15:32 +0100185 * server that should be delayed. */
Hanno Becker01ea7782018-08-17 13:33:41 +0100186 uint8_t delay_srv_cnt; /* Number of entries in delay_srv. */
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200187 int drop; /* drop 1 packet in N (none if 0) */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200188 int mtu; /* drop packets larger than this */
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200189 int bad_ad; /* inject corrupted ApplicationData record */
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200190 int protect_hvr; /* never drop or delay HelloVerifyRequest */
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200191 int protect_len; /* never drop/delay packet of the given size*/
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100192 int inject_clihlo; /* inject fake ClientHello after handshake */
Hanno Becker77abef52017-11-02 10:50:28 +0000193 unsigned pack; /* merge packets into single datagram for
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100194 * at most \c merge milliseconds if > 0 */
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200195 unsigned int seed; /* seed for "random" events */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200196} opt;
197
198static void exit_usage( const char *name, const char *value )
199{
200 if( value == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 mbedtls_printf( " unknown option or missing value: %s\n", name );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200202 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 mbedtls_printf( " option %s: illegal value: %s\n", name, value );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 mbedtls_printf( USAGE );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200206 exit( 1 );
207}
208
209static void get_options( int argc, char *argv[] )
210{
211 int i;
212 char *p, *q;
213
214 opt.server_addr = DFL_SERVER_ADDR;
215 opt.server_port = DFL_SERVER_PORT;
216 opt.listen_addr = DFL_LISTEN_ADDR;
217 opt.listen_port = DFL_LISTEN_PORT;
Hanno Becker211f44c2017-10-31 14:08:10 +0000218 opt.pack = DFL_PACK;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200219 /* Other members default to 0 */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200220
Hanno Becker01ea7782018-08-17 13:33:41 +0100221 opt.delay_cli_cnt = 0;
222 opt.delay_srv_cnt = 0;
223 memset( opt.delay_cli, 0, sizeof( opt.delay_cli ) );
224 memset( opt.delay_srv, 0, sizeof( opt.delay_srv ) );
225
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200226 for( i = 1; i < argc; i++ )
227 {
228 p = argv[i];
229 if( ( q = strchr( p, '=' ) ) == NULL )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200230 exit_usage( p, NULL );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200231 *q++ = '\0';
232
233 if( strcmp( p, "server_addr" ) == 0 )
234 opt.server_addr = q;
235 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200236 opt.server_port = q;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200237 else if( strcmp( p, "listen_addr" ) == 0 )
238 opt.listen_addr = q;
239 else if( strcmp( p, "listen_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200240 opt.listen_port = q;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200241 else if( strcmp( p, "duplicate" ) == 0 )
242 {
243 opt.duplicate = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200244 if( opt.duplicate < 0 || opt.duplicate > 20 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200245 exit_usage( p, q );
246 }
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200247 else if( strcmp( p, "delay" ) == 0 )
248 {
249 opt.delay = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200250 if( opt.delay < 0 || opt.delay > 20 || opt.delay == 1 )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200251 exit_usage( p, q );
252 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200253 else if( strcmp( p, "delay_ccs" ) == 0 )
254 {
255 opt.delay_ccs = atoi( q );
256 if( opt.delay_ccs < 0 || opt.delay_ccs > 1 )
257 exit_usage( p, q );
258 }
Hanno Becker01ea7782018-08-17 13:33:41 +0100259 else if( strcmp( p, "delay_cli" ) == 0 ||
260 strcmp( p, "delay_srv" ) == 0 )
261 {
262 uint8_t *delay_cnt;
263 char **delay_list;
264 size_t len;
265 char *buf;
266
267 if( strcmp( p, "delay_cli" ) == 0 )
268 {
269 delay_cnt = &opt.delay_cli_cnt;
270 delay_list = opt.delay_cli;
271 }
272 else
273 {
274 delay_cnt = &opt.delay_srv_cnt;
275 delay_list = opt.delay_srv;
276 }
277
278 if( *delay_cnt == MAX_DELAYED_HS )
279 {
Hanno Beckerf34a4c12018-08-28 17:22:26 +0100280 mbedtls_printf( " too many uses of %s: only %d allowed\n",
281 p, MAX_DELAYED_HS );
Hanno Becker01ea7782018-08-17 13:33:41 +0100282 exit_usage( p, NULL );
283 }
284
285 len = strlen( q );
286 buf = mbedtls_calloc( 1, len + 1 );
287 if( buf == NULL )
288 {
289 mbedtls_printf( " Allocation failure\n" );
290 exit( 1 );
291 }
292 memcpy( buf, q, len + 1 );
293
294 delay_list[ (*delay_cnt)++ ] = buf;
295 }
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200296 else if( strcmp( p, "drop" ) == 0 )
297 {
298 opt.drop = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200299 if( opt.drop < 0 || opt.drop > 20 || opt.drop == 1 )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200300 exit_usage( p, q );
301 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100302 else if( strcmp( p, "pack" ) == 0 )
303 {
Hanno Becker0cc77742017-10-31 14:10:07 +0000304#if defined(MBEDTLS_TIMING_C)
Hanno Becker77abef52017-11-02 10:50:28 +0000305 opt.pack = (unsigned) atoi( q );
Hanno Becker0cc77742017-10-31 14:10:07 +0000306#else
307 mbedtls_printf( " option pack only defined if MBEDTLS_TIMING_C is enabled\n" );
308 exit( 1 );
309#endif
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100310 }
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200311 else if( strcmp( p, "mtu" ) == 0 )
312 {
313 opt.mtu = atoi( q );
314 if( opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE )
315 exit_usage( p, q );
316 }
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200317 else if( strcmp( p, "bad_ad" ) == 0 )
318 {
319 opt.bad_ad = atoi( q );
320 if( opt.bad_ad < 0 || opt.bad_ad > 1 )
321 exit_usage( p, q );
322 }
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200323 else if( strcmp( p, "protect_hvr" ) == 0 )
324 {
325 opt.protect_hvr = atoi( q );
326 if( opt.protect_hvr < 0 || opt.protect_hvr > 1 )
327 exit_usage( p, q );
328 }
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200329 else if( strcmp( p, "protect_len" ) == 0 )
330 {
331 opt.protect_len = atoi( q );
332 if( opt.protect_len < 0 )
333 exit_usage( p, q );
334 }
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100335 else if( strcmp( p, "inject_clihlo" ) == 0 )
336 {
337 opt.inject_clihlo = atoi( q );
338 if( opt.inject_clihlo < 0 || opt.inject_clihlo > 1 )
339 exit_usage( p, q );
340 }
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200341 else if( strcmp( p, "seed" ) == 0 )
342 {
343 opt.seed = atoi( q );
344 if( opt.seed == 0 )
345 exit_usage( p, q );
346 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200347 else
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200348 exit_usage( p, NULL );
349 }
350}
351
352static const char *msg_type( unsigned char *msg, size_t len )
353{
354 if( len < 1 ) return( "Invalid" );
355 switch( msg[0] )
356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" );
358 case MBEDTLS_SSL_MSG_ALERT: return( "Alert" );
359 case MBEDTLS_SSL_MSG_APPLICATION_DATA: return( "ApplicationData" );
360 case MBEDTLS_SSL_MSG_HANDSHAKE: break; /* See below */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200361 default: return( "Unknown" );
362 }
363
Manuel Pégourié-Gonnard8cc7e032014-09-25 12:59:05 +0200364 if( len < 13 + 12 ) return( "Invalid handshake" );
365
366 /*
367 * Our handshake message are less than 2^16 bytes long, so they should
368 * have 0 as the first byte of length, frag_offset and frag_length.
369 * Otherwise, assume they are encrypted.
370 */
371 if( msg[14] || msg[19] || msg[22] ) return( "Encrypted handshake" );
372
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200373 switch( msg[13] )
374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 case MBEDTLS_SSL_HS_HELLO_REQUEST: return( "HelloRequest" );
376 case MBEDTLS_SSL_HS_CLIENT_HELLO: return( "ClientHello" );
377 case MBEDTLS_SSL_HS_SERVER_HELLO: return( "ServerHello" );
378 case MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" );
379 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" );
380 case MBEDTLS_SSL_HS_CERTIFICATE: return( "Certificate" );
381 case MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" );
382 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" );
383 case MBEDTLS_SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" );
384 case MBEDTLS_SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" );
385 case MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" );
386 case MBEDTLS_SSL_HS_FINISHED: return( "Finished" );
Manuel Pégourié-Gonnardbc010a02014-09-20 12:40:51 +0200387 default: return( "Unknown handshake" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200388 }
389}
390
Hanno Becker0cc77742017-10-31 14:10:07 +0000391#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200392/* Return elapsed time in milliseconds since the first call */
Hanno Becker77abef52017-11-02 10:50:28 +0000393static unsigned ellapsed_time( void )
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200394{
Hanno Becker92474da2017-10-31 14:09:30 +0000395 static int initialized = 0;
396 static struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200397
Hanno Becker92474da2017-10-31 14:09:30 +0000398 if( initialized == 0 )
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200399 {
Hanno Becker92474da2017-10-31 14:09:30 +0000400 (void) mbedtls_timing_get_timer( &hires, 1 );
401 initialized = 1;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200402 return( 0 );
403 }
404
Hanno Becker92474da2017-10-31 14:09:30 +0000405 return( mbedtls_timing_get_timer( &hires, 0 ) );
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200406}
407
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200408typedef struct
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200409{
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100410 mbedtls_net_context *ctx;
411
412 const char *description;
413
Hanno Becker77abef52017-11-02 10:50:28 +0000414 unsigned packet_lifetime;
415 unsigned num_datagrams;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100416
417 unsigned char data[MAX_MSG_SIZE];
Hanno Beckera5e68972017-12-06 08:35:02 +0000418 size_t len;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100419
420} ctx_buffer;
421
422static ctx_buffer outbuf[2];
423
424static int ctx_buffer_flush( ctx_buffer *buf )
425{
426 int ret;
427
Hanno Becker77abef52017-11-02 10:50:28 +0000428 mbedtls_printf( " %05u flush %s: %u bytes, %u datagrams, last %u ms\n",
429 ellapsed_time(), buf->description,
Hanno Beckera5e68972017-12-06 08:35:02 +0000430 (unsigned) buf->len, buf->num_datagrams,
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100431 ellapsed_time() - buf->packet_lifetime );
432
433 ret = mbedtls_net_send( buf->ctx, buf->data, buf->len );
434
435 buf->len = 0;
436 buf->num_datagrams = 0;
437
438 return( ret );
439}
440
Hanno Becker77abef52017-11-02 10:50:28 +0000441static unsigned ctx_buffer_time_remaining( ctx_buffer *buf )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100442{
Hanno Becker77abef52017-11-02 10:50:28 +0000443 unsigned const cur_time = ellapsed_time();
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100444
Hanno Becker77abef52017-11-02 10:50:28 +0000445 if( buf->num_datagrams == 0 )
446 return( (unsigned) -1 );
447
448 if( cur_time - buf->packet_lifetime >= opt.pack )
449 return( 0 );
450
451 return( opt.pack - ( cur_time - buf->packet_lifetime ) );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100452}
453
454static int ctx_buffer_append( ctx_buffer *buf,
455 const unsigned char * data,
456 size_t len )
457{
458 int ret;
459
Hanno Beckera5e68972017-12-06 08:35:02 +0000460 if( len > (size_t) INT_MAX )
461 return( -1 );
462
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100463 if( len > sizeof( buf->data ) )
464 {
Hanno Becker77abef52017-11-02 10:50:28 +0000465 mbedtls_printf( " ! buffer size %u too large (max %u)\n",
466 (unsigned) len, (unsigned) sizeof( buf->data ) );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100467 return( -1 );
468 }
469
470 if( sizeof( buf->data ) - buf->len < len )
471 {
472 if( ( ret = ctx_buffer_flush( buf ) ) <= 0 )
473 return( ret );
474 }
475
476 memcpy( buf->data + buf->len, data, len );
477
478 buf->len += len;
479 if( ++buf->num_datagrams == 1 )
480 buf->packet_lifetime = ellapsed_time();
481
Hanno Beckera5e68972017-12-06 08:35:02 +0000482 return( (int) len );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100483}
Hanno Becker77abef52017-11-02 10:50:28 +0000484#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100485
486static int dispatch_data( mbedtls_net_context *ctx,
487 const unsigned char * data,
488 size_t len )
489{
Hanno Becker77abef52017-11-02 10:50:28 +0000490#if defined(MBEDTLS_TIMING_C)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100491 ctx_buffer *buf = NULL;
Hanno Becker77abef52017-11-02 10:50:28 +0000492 if( opt.pack > 0 )
493 {
494 if( outbuf[0].ctx == ctx )
495 buf = &outbuf[0];
496 else if( outbuf[1].ctx == ctx )
497 buf = &outbuf[1];
Hanno Becker0cc77742017-10-31 14:10:07 +0000498
Hanno Becker77abef52017-11-02 10:50:28 +0000499 if( buf == NULL )
500 return( -1 );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100501
Hanno Becker77abef52017-11-02 10:50:28 +0000502 return( ctx_buffer_append( buf, data, len ) );
503 }
504#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100505
Hanno Becker0cc77742017-10-31 14:10:07 +0000506 return( mbedtls_net_send( ctx, data, len ) );
507}
508
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100509typedef struct
510{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200511 mbedtls_net_context *dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200512 const char *way;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200513 const char *type;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200514 unsigned len;
515 unsigned char buf[MAX_MSG_SIZE];
516} packet;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200517
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200518/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */
519void print_packet( const packet *p, const char *why )
520{
Hanno Becker0cc77742017-10-31 14:10:07 +0000521#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200522 if( why == NULL )
Hanno Becker77abef52017-11-02 10:50:28 +0000523 mbedtls_printf( " %05u dispatch %s %s (%u bytes)\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200524 ellapsed_time(), p->way, p->type, p->len );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200525 else
Hanno Becker77abef52017-11-02 10:50:28 +0000526 mbedtls_printf( " %05u dispatch %s %s (%u bytes): %s\n",
Hanno Becker0cc77742017-10-31 14:10:07 +0000527 ellapsed_time(), p->way, p->type, p->len, why );
528#else
529 if( why == NULL )
530 mbedtls_printf( " dispatch %s %s (%u bytes)\n",
531 p->way, p->type, p->len );
532 else
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100533 mbedtls_printf( " dispatch %s %s (%u bytes): %s\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200534 p->way, p->type, p->len, why );
Hanno Becker0cc77742017-10-31 14:10:07 +0000535#endif
536
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200537 fflush( stdout );
538}
539
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100540/*
541 * In order to test the server's behaviour when receiving a ClientHello after
542 * the connection is established (this could be a hard reset from the client,
543 * but the server must not drop the existing connection before establishing
544 * client reachability, see RFC 6347 Section 4.2.8), we memorize the first
545 * ClientHello we see (which can't have a cookie), then replay it after the
546 * first ApplicationData record - then we're done.
547 *
548 * This is controlled by the inject_clihlo option.
549 *
550 * We want an explicit state and a place to store the packet.
551 */
Manuel Pégourié-Gonnard7fe5ac12020-03-30 12:46:21 +0200552typedef enum {
553 ICH_INIT, /* haven't seen the first ClientHello yet */
554 ICH_CACHED, /* cached the initial ClientHello */
555 ICH_INJECTED, /* ClientHello already injected, done */
556} inject_clihlo_state_t;
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100557
Manuel Pégourié-Gonnard7fe5ac12020-03-30 12:46:21 +0200558static inject_clihlo_state_t inject_clihlo_state;
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100559static packet initial_clihlo;
560
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200561int send_packet( const packet *p, const char *why )
562{
563 int ret;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200564 mbedtls_net_context *dst = p->dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200565
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100566 /* save initial ClientHello? */
567 if( opt.inject_clihlo != 0 &&
Manuel Pégourié-Gonnard7fe5ac12020-03-30 12:46:21 +0200568 inject_clihlo_state == ICH_INIT &&
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100569 strcmp( p->type, "ClientHello" ) == 0 )
570 {
571 memcpy( &initial_clihlo, p, sizeof( packet ) );
Manuel Pégourié-Gonnard7fe5ac12020-03-30 12:46:21 +0200572 inject_clihlo_state = ICH_CACHED;
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100573 }
574
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200575 /* insert corrupted ApplicationData record? */
576 if( opt.bad_ad &&
577 strcmp( p->type, "ApplicationData" ) == 0 )
578 {
579 unsigned char buf[MAX_MSG_SIZE];
580 memcpy( buf, p->buf, p->len );
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200581
Hanno Beckerfbb0b702017-05-26 16:55:07 +0100582 if( p->len <= 13 )
583 {
584 mbedtls_printf( " ! can't corrupt empty AD record" );
585 }
586 else
587 {
588 ++buf[13];
589 print_packet( p, "corrupted" );
590 }
591
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100592 if( ( ret = dispatch_data( dst, buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200593 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100594 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200595 return( ret );
596 }
597 }
598
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200599 print_packet( p, why );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100600 if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200601 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100602 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200603 return( ret );
604 }
605
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200606 /* Don't duplicate Application Data, only handshake covered */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200607 if( opt.duplicate != 0 &&
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200608 strcmp( p->type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200609 rand() % opt.duplicate == 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200610 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200611 print_packet( p, "duplicated" );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200612
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100613 if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200614 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100615 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200616 return( ret );
617 }
618 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200619
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100620 /* Inject ClientHello after first ApplicationData */
621 if( opt.inject_clihlo != 0 &&
Manuel Pégourié-Gonnard7fe5ac12020-03-30 12:46:21 +0200622 inject_clihlo_state == ICH_CACHED &&
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100623 strcmp( p->type, "ApplicationData" ) == 0 )
624 {
625 print_packet( &initial_clihlo, "injected" );
626
627 if( ( ret = dispatch_data( dst, initial_clihlo.buf,
628 initial_clihlo.len ) ) <= 0 )
629 {
630 mbedtls_printf( " ! dispatch returned %d\n", ret );
631 return( ret );
632 }
633
Manuel Pégourié-Gonnard7fe5ac12020-03-30 12:46:21 +0200634 inject_clihlo_state = ICH_INJECTED;
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100635 }
636
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200637 return( 0 );
638}
639
Hanno Becker101bcba2018-08-21 16:39:51 +0100640#define MAX_DELAYED_MSG 5
641static size_t prev_len;
642static packet prev[MAX_DELAYED_MSG];
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200643
644void clear_pending( void )
645{
Hanno Becker12b72c12018-08-23 13:15:36 +0100646 memset( &prev, 0, sizeof( prev ) );
Hanno Becker101bcba2018-08-21 16:39:51 +0100647 prev_len = 0;
648}
649
650void delay_packet( packet *delay )
651{
652 if( prev_len == MAX_DELAYED_MSG )
653 return;
654
655 memcpy( &prev[prev_len++], delay, sizeof( packet ) );
656}
657
658int send_delayed()
659{
660 uint8_t offset;
661 int ret;
662 for( offset = 0; offset < prev_len; offset++ )
663 {
664 ret = send_packet( &prev[offset], "delayed" );
665 if( ret != 0 )
666 return( ret );
667 }
668
669 clear_pending();
670 return( 0 );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200671}
672
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200673/*
674 * Avoid dropping or delaying a packet that was already dropped twice: this
675 * only results in uninteresting timeouts. We can't rely on type to identify
676 * packets, since during renegotiation they're all encrypted. So, rely on
677 * size mod 2048 (which is usually just size).
678 */
679static unsigned char dropped[2048] = { 0 };
680#define DROP_MAX 2
681
Hanno Beckerbcf97ec2019-06-04 13:04:28 +0100682/* We only drop packets at the level of entire datagrams, not at the level
683 * of records. In particular, if the peer changes the way it packs multiple
684 * records into a single datagram, we don't necessarily count the number of
685 * times a record has been dropped correctly. However, the only known reason
686 * why a peer would change datagram packing is disabling the latter on
687 * retransmission, in which case we'd drop involved records at most
688 * DROP_MAX + 1 times. */
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200689void update_dropped( const packet *p )
690{
691 size_t id = p->len % sizeof( dropped );
Manuel Pégourié-Gonnardfa60f122014-09-26 16:07:29 +0200692 ++dropped[id];
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200693}
694
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200695int handle_message( const char *way,
696 mbedtls_net_context *dst,
697 mbedtls_net_context *src )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200698{
699 int ret;
700 packet cur;
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200701 size_t id;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200702
Hanno Becker01ea7782018-08-17 13:33:41 +0100703 uint8_t delay_idx;
704 char ** delay_list;
705 uint8_t delay_list_len;
706
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +0200707 /* receive packet */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200708 if( ( ret = mbedtls_net_recv( src, cur.buf, sizeof( cur.buf ) ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_printf( " ! mbedtls_net_recv returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200711 return( ret );
712 }
713
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200714 cur.len = ret;
715 cur.type = msg_type( cur.buf, cur.len );
716 cur.way = way;
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200717 cur.dst = dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200718 print_packet( &cur, NULL );
719
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200720 id = cur.len % sizeof( dropped );
721
Hanno Becker01ea7782018-08-17 13:33:41 +0100722 if( strcmp( way, "S <- C" ) == 0 )
723 {
724 delay_list = opt.delay_cli;
725 delay_list_len = opt.delay_cli_cnt;
726 }
727 else
728 {
729 delay_list = opt.delay_srv;
730 delay_list_len = opt.delay_srv_cnt;
731 }
Hanno Beckercf469452018-08-28 10:09:47 +0100732
Hanno Becker01ea7782018-08-17 13:33:41 +0100733 /* Check if message type is in the list of messages
734 * that should be delayed */
735 for( delay_idx = 0; delay_idx < delay_list_len; delay_idx++ )
736 {
737 if( delay_list[ delay_idx ] == NULL )
738 continue;
739
740 if( strcmp( delay_list[ delay_idx ], cur.type ) == 0 )
741 {
742 /* Delay message */
Hanno Becker101bcba2018-08-21 16:39:51 +0100743 delay_packet( &cur );
Hanno Becker01ea7782018-08-17 13:33:41 +0100744
745 /* Remove entry from list */
746 mbedtls_free( delay_list[delay_idx] );
747 delay_list[delay_idx] = NULL;
748
749 return( 0 );
750 }
751 }
752
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200753 /* do we want to drop, delay, or forward it? */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200754 if( ( opt.mtu != 0 &&
755 cur.len > (unsigned) opt.mtu ) ||
756 ( opt.drop != 0 &&
757 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200758 ! ( opt.protect_hvr &&
759 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200760 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200761 dropped[id] < DROP_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200762 rand() % opt.drop == 0 ) )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200763 {
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200764 update_dropped( &cur );
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200765 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200766 else if( ( opt.delay_ccs == 1 &&
767 strcmp( cur.type, "ChangeCipherSpec" ) == 0 ) ||
768 ( opt.delay != 0 &&
769 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200770 ! ( opt.protect_hvr &&
771 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200772 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200773 dropped[id] < DROP_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200774 rand() % opt.delay == 0 ) )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200775 {
Hanno Becker101bcba2018-08-21 16:39:51 +0100776 delay_packet( &cur );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200777 }
778 else
779 {
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200780 /* forward and possibly duplicate */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200781 if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 )
782 return( ret );
783
Hanno Becker101bcba2018-08-21 16:39:51 +0100784 /* send previously delayed messages if any */
785 ret = send_delayed();
786 if( ret != 0 )
787 return( ret );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200788 }
789
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200790 return( 0 );
791}
792
793int main( int argc, char *argv[] )
794{
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100795 int ret = 1;
796 int exit_code = MBEDTLS_EXIT_FAILURE;
Hanno Becker01ea7782018-08-17 13:33:41 +0100797 uint8_t delay_idx;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200798
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200799 mbedtls_net_context listen_fd, client_fd, server_fd;
Hanno Becker77abef52017-11-02 10:50:28 +0000800
801#if defined( MBEDTLS_TIMING_C )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100802 struct timeval tm;
Hanno Becker77abef52017-11-02 10:50:28 +0000803#endif
804
805 struct timeval *tm_ptr = NULL;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200806
807 int nb_fds;
808 fd_set read_fds;
809
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200810 mbedtls_net_init( &listen_fd );
811 mbedtls_net_init( &client_fd );
812 mbedtls_net_init( &server_fd );
813
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200814 get_options( argc, argv );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200815
816 /*
817 * Decisions to drop/delay/duplicate packets are pseudo-random: dropping
818 * exactly 1 in N packets would lead to problems when a flight has exactly
819 * N packets: the same packet would be dropped on every resend.
820 *
821 * In order to be able to reproduce problems reliably, the seed may be
822 * specified explicitly.
823 */
824 if( opt.seed == 0 )
825 {
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200826 opt.seed = (unsigned int) time( NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 mbedtls_printf( " . Pseudo-random seed: %u\n", opt.seed );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200828 }
829
830 srand( opt.seed );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200831
832 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200833 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200834 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200835 mbedtls_printf( " . Connect to server on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200836 opt.server_addr, opt.server_port );
837 fflush( stdout );
838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
840 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200843 goto exit;
844 }
845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200847
848 /*
849 * 1. Setup the "listening" UDP socket
850 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200851 mbedtls_printf( " . Bind on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200852 opt.listen_addr, opt.listen_port );
853 fflush( stdout );
854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 if( ( ret = mbedtls_net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
856 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200859 goto exit;
860 }
861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200863
864 /*
865 * 2. Wait until a client connects
866 */
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200867accept:
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200868 mbedtls_net_free( &client_fd );
869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 mbedtls_printf( " . Waiting for a remote connection ..." );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200871 fflush( stdout );
872
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200873 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200874 NULL, 0, NULL ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 mbedtls_printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200877 goto exit;
878 }
879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200881
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200882 /*
883 * 3. Forward packets forever (kill the process to terminate it)
884 */
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200885 clear_pending();
886 memset( dropped, 0, sizeof( dropped ) );
887
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200888 nb_fds = client_fd.fd;
889 if( nb_fds < server_fd.fd )
890 nb_fds = server_fd.fd;
891 if( nb_fds < listen_fd.fd )
892 nb_fds = listen_fd.fd;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200893 ++nb_fds;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200894
Hanno Becker0cc77742017-10-31 14:10:07 +0000895#if defined(MBEDTLS_TIMING_C)
Hanno Becker211f44c2017-10-31 14:08:10 +0000896 if( opt.pack > 0 )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100897 {
898 outbuf[0].ctx = &server_fd;
899 outbuf[0].description = "S <- C";
900 outbuf[0].num_datagrams = 0;
901 outbuf[0].len = 0;
902
903 outbuf[1].ctx = &client_fd;
904 outbuf[1].description = "S -> C";
905 outbuf[1].num_datagrams = 0;
906 outbuf[1].len = 0;
907 }
Hanno Becker0cc77742017-10-31 14:10:07 +0000908#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100909
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200910 while( 1 )
911 {
Hanno Becker77abef52017-11-02 10:50:28 +0000912#if defined(MBEDTLS_TIMING_C)
913 if( opt.pack > 0 )
914 {
915 unsigned max_wait_server, max_wait_client, max_wait;
916 max_wait_server = ctx_buffer_time_remaining( &outbuf[0] );
917 max_wait_client = ctx_buffer_time_remaining( &outbuf[1] );
918
919 max_wait = (unsigned) -1;
920
921 if( max_wait_server == 0 )
922 ctx_buffer_flush( &outbuf[0] );
923 else
924 max_wait = max_wait_server;
925
926 if( max_wait_client == 0 )
927 ctx_buffer_flush( &outbuf[1] );
928 else
929 {
930 if( max_wait_client < max_wait )
931 max_wait = max_wait_client;
932 }
933
934 if( max_wait != (unsigned) -1 )
935 {
936 tm.tv_sec = max_wait / 1000;
937 tm.tv_usec = ( max_wait % 1000 ) * 1000;
938
939 tm_ptr = &tm;
940 }
941 else
942 {
943 tm_ptr = NULL;
944 }
945 }
946#endif /* MBEDTLS_TIMING_C */
947
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200948 FD_ZERO( &read_fds );
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200949 FD_SET( server_fd.fd, &read_fds );
950 FD_SET( client_fd.fd, &read_fds );
951 FD_SET( listen_fd.fd, &read_fds );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200952
Hanno Becker77abef52017-11-02 10:50:28 +0000953 if( ( ret = select( nb_fds, &read_fds, NULL, NULL, tm_ptr ) ) < 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200954 {
955 perror( "select" );
956 goto exit;
957 }
958
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200959 if( FD_ISSET( listen_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200960 goto accept;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200961
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200962 if( FD_ISSET( client_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200963 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200964 if( ( ret = handle_message( "S <- C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200965 &server_fd, &client_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200966 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200967 }
968
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200969 if( FD_ISSET( server_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200970 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200971 if( ( ret = handle_message( "S -> C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200972 &client_fd, &server_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200973 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200974 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100975
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200976 }
977
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100978 exit_code = MBEDTLS_EXIT_SUCCESS;
979
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200980exit:
981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982#ifdef MBEDTLS_ERROR_C
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100983 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200984 {
985 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 mbedtls_strerror( ret, error_buf, 100 );
987 mbedtls_printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200988 fflush( stdout );
989 }
990#endif
991
Hanno Becker01ea7782018-08-17 13:33:41 +0100992 for( delay_idx = 0; delay_idx < MAX_DELAYED_HS; delay_idx++ )
993 {
Shawn Carey7b81dcf2021-05-13 09:08:02 -0400994 mbedtls_free( opt.delay_cli[delay_idx] );
995 mbedtls_free( opt.delay_srv[delay_idx] );
Hanno Becker01ea7782018-08-17 13:33:41 +0100996 }
997
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200998 mbedtls_net_free( &client_fd );
999 mbedtls_net_free( &server_fd );
1000 mbedtls_net_free( &listen_fd );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001001
1002#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003 mbedtls_printf( " Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001004 fflush( stdout ); getchar();
1005#endif
1006
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +02001007 mbedtls_exit( exit_code );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001008}
1009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010#endif /* MBEDTLS_NET_C */