blob: d7146b14dd0687f861b0a662412d54546f37ed58 [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/*
Manuel Pégourié-Gonnard934cff32021-07-06 12:39:43 +0200674 * Avoid dropping or delaying a packet that was already dropped or delayed
675 * ("held") twice: this only results in uninteresting timeouts. We can't rely
676 * on type to identify packets, since during renegotiation they're all
677 * encrypted. So, rely on size mod 2048 (which is usually just size).
678 *
679 * We only hold packets at the level of entire datagrams, not at the level
Hanno Beckerbcf97ec2019-06-04 13:04:28 +0100680 * of records. In particular, if the peer changes the way it packs multiple
681 * records into a single datagram, we don't necessarily count the number of
Manuel Pégourié-Gonnard934cff32021-07-06 12:39:43 +0200682 * times a record has been held correctly. However, the only known reason
Hanno Beckerbcf97ec2019-06-04 13:04:28 +0100683 * why a peer would change datagram packing is disabling the latter on
Manuel Pégourié-Gonnard934cff32021-07-06 12:39:43 +0200684 * retransmission, in which case we'd hold involved records at most
685 * HOLD_MAX + 1 times.
686 */
687static unsigned char held[2048] = { 0 };
688#define HOLD_MAX 2
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200689
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200690int handle_message( const char *way,
691 mbedtls_net_context *dst,
692 mbedtls_net_context *src )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200693{
694 int ret;
695 packet cur;
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200696 size_t id;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200697
Hanno Becker01ea7782018-08-17 13:33:41 +0100698 uint8_t delay_idx;
699 char ** delay_list;
700 uint8_t delay_list_len;
701
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +0200702 /* receive packet */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200703 if( ( ret = mbedtls_net_recv( src, cur.buf, sizeof( cur.buf ) ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 mbedtls_printf( " ! mbedtls_net_recv returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200706 return( ret );
707 }
708
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200709 cur.len = ret;
710 cur.type = msg_type( cur.buf, cur.len );
711 cur.way = way;
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200712 cur.dst = dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200713 print_packet( &cur, NULL );
714
Manuel Pégourié-Gonnard934cff32021-07-06 12:39:43 +0200715 id = cur.len % sizeof( held );
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200716
Hanno Becker01ea7782018-08-17 13:33:41 +0100717 if( strcmp( way, "S <- C" ) == 0 )
718 {
719 delay_list = opt.delay_cli;
720 delay_list_len = opt.delay_cli_cnt;
721 }
722 else
723 {
724 delay_list = opt.delay_srv;
725 delay_list_len = opt.delay_srv_cnt;
726 }
Hanno Beckercf469452018-08-28 10:09:47 +0100727
Hanno Becker01ea7782018-08-17 13:33:41 +0100728 /* Check if message type is in the list of messages
729 * that should be delayed */
730 for( delay_idx = 0; delay_idx < delay_list_len; delay_idx++ )
731 {
732 if( delay_list[ delay_idx ] == NULL )
733 continue;
734
735 if( strcmp( delay_list[ delay_idx ], cur.type ) == 0 )
736 {
737 /* Delay message */
Hanno Becker101bcba2018-08-21 16:39:51 +0100738 delay_packet( &cur );
Hanno Becker01ea7782018-08-17 13:33:41 +0100739
740 /* Remove entry from list */
741 mbedtls_free( delay_list[delay_idx] );
742 delay_list[delay_idx] = NULL;
743
744 return( 0 );
745 }
746 }
747
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200748 /* do we want to drop, delay, or forward it? */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200749 if( ( opt.mtu != 0 &&
750 cur.len > (unsigned) opt.mtu ) ||
751 ( opt.drop != 0 &&
752 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200753 ! ( opt.protect_hvr &&
754 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200755 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnard934cff32021-07-06 12:39:43 +0200756 held[id] < HOLD_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200757 rand() % opt.drop == 0 ) )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200758 {
Manuel Pégourié-Gonnard934cff32021-07-06 12:39:43 +0200759 ++held[id];
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200760 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200761 else if( ( opt.delay_ccs == 1 &&
762 strcmp( cur.type, "ChangeCipherSpec" ) == 0 ) ||
763 ( opt.delay != 0 &&
764 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200765 ! ( opt.protect_hvr &&
766 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200767 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnard934cff32021-07-06 12:39:43 +0200768 held[id] < HOLD_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200769 rand() % opt.delay == 0 ) )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200770 {
Manuel Pégourié-Gonnard934cff32021-07-06 12:39:43 +0200771 ++held[id];
Hanno Becker101bcba2018-08-21 16:39:51 +0100772 delay_packet( &cur );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200773 }
774 else
775 {
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200776 /* forward and possibly duplicate */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200777 if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 )
778 return( ret );
779
Hanno Becker101bcba2018-08-21 16:39:51 +0100780 /* send previously delayed messages if any */
781 ret = send_delayed();
782 if( ret != 0 )
783 return( ret );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200784 }
785
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200786 return( 0 );
787}
788
789int main( int argc, char *argv[] )
790{
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100791 int ret = 1;
792 int exit_code = MBEDTLS_EXIT_FAILURE;
Hanno Becker01ea7782018-08-17 13:33:41 +0100793 uint8_t delay_idx;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200794
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200795 mbedtls_net_context listen_fd, client_fd, server_fd;
Hanno Becker77abef52017-11-02 10:50:28 +0000796
797#if defined( MBEDTLS_TIMING_C )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100798 struct timeval tm;
Hanno Becker77abef52017-11-02 10:50:28 +0000799#endif
800
801 struct timeval *tm_ptr = NULL;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200802
803 int nb_fds;
804 fd_set read_fds;
805
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200806 mbedtls_net_init( &listen_fd );
807 mbedtls_net_init( &client_fd );
808 mbedtls_net_init( &server_fd );
809
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200810 get_options( argc, argv );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200811
812 /*
813 * Decisions to drop/delay/duplicate packets are pseudo-random: dropping
814 * exactly 1 in N packets would lead to problems when a flight has exactly
815 * N packets: the same packet would be dropped on every resend.
816 *
817 * In order to be able to reproduce problems reliably, the seed may be
818 * specified explicitly.
819 */
820 if( opt.seed == 0 )
821 {
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200822 opt.seed = (unsigned int) time( NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 mbedtls_printf( " . Pseudo-random seed: %u\n", opt.seed );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200824 }
825
826 srand( opt.seed );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200827
828 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200829 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200830 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200831 mbedtls_printf( " . Connect to server on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200832 opt.server_addr, opt.server_port );
833 fflush( stdout );
834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
836 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200839 goto exit;
840 }
841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200843
844 /*
845 * 1. Setup the "listening" UDP socket
846 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200847 mbedtls_printf( " . Bind on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200848 opt.listen_addr, opt.listen_port );
849 fflush( stdout );
850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 if( ( ret = mbedtls_net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
852 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200855 goto exit;
856 }
857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200859
860 /*
861 * 2. Wait until a client connects
862 */
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200863accept:
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200864 mbedtls_net_free( &client_fd );
865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 mbedtls_printf( " . Waiting for a remote connection ..." );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200867 fflush( stdout );
868
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200869 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200870 NULL, 0, NULL ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 mbedtls_printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200873 goto exit;
874 }
875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200877
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200878 /*
879 * 3. Forward packets forever (kill the process to terminate it)
880 */
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200881 clear_pending();
Manuel Pégourié-Gonnard934cff32021-07-06 12:39:43 +0200882 memset( held, 0, sizeof( held ) );
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200883
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200884 nb_fds = client_fd.fd;
885 if( nb_fds < server_fd.fd )
886 nb_fds = server_fd.fd;
887 if( nb_fds < listen_fd.fd )
888 nb_fds = listen_fd.fd;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200889 ++nb_fds;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200890
Hanno Becker0cc77742017-10-31 14:10:07 +0000891#if defined(MBEDTLS_TIMING_C)
Hanno Becker211f44c2017-10-31 14:08:10 +0000892 if( opt.pack > 0 )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100893 {
894 outbuf[0].ctx = &server_fd;
895 outbuf[0].description = "S <- C";
896 outbuf[0].num_datagrams = 0;
897 outbuf[0].len = 0;
898
899 outbuf[1].ctx = &client_fd;
900 outbuf[1].description = "S -> C";
901 outbuf[1].num_datagrams = 0;
902 outbuf[1].len = 0;
903 }
Hanno Becker0cc77742017-10-31 14:10:07 +0000904#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100905
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200906 while( 1 )
907 {
Hanno Becker77abef52017-11-02 10:50:28 +0000908#if defined(MBEDTLS_TIMING_C)
909 if( opt.pack > 0 )
910 {
911 unsigned max_wait_server, max_wait_client, max_wait;
912 max_wait_server = ctx_buffer_time_remaining( &outbuf[0] );
913 max_wait_client = ctx_buffer_time_remaining( &outbuf[1] );
914
915 max_wait = (unsigned) -1;
916
917 if( max_wait_server == 0 )
918 ctx_buffer_flush( &outbuf[0] );
919 else
920 max_wait = max_wait_server;
921
922 if( max_wait_client == 0 )
923 ctx_buffer_flush( &outbuf[1] );
924 else
925 {
926 if( max_wait_client < max_wait )
927 max_wait = max_wait_client;
928 }
929
930 if( max_wait != (unsigned) -1 )
931 {
932 tm.tv_sec = max_wait / 1000;
933 tm.tv_usec = ( max_wait % 1000 ) * 1000;
934
935 tm_ptr = &tm;
936 }
937 else
938 {
939 tm_ptr = NULL;
940 }
941 }
942#endif /* MBEDTLS_TIMING_C */
943
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200944 FD_ZERO( &read_fds );
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200945 FD_SET( server_fd.fd, &read_fds );
946 FD_SET( client_fd.fd, &read_fds );
947 FD_SET( listen_fd.fd, &read_fds );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200948
Hanno Becker77abef52017-11-02 10:50:28 +0000949 if( ( ret = select( nb_fds, &read_fds, NULL, NULL, tm_ptr ) ) < 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200950 {
951 perror( "select" );
952 goto exit;
953 }
954
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200955 if( FD_ISSET( listen_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200956 goto accept;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200957
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200958 if( FD_ISSET( client_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200959 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200960 if( ( ret = handle_message( "S <- C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200961 &server_fd, &client_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200962 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200963 }
964
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200965 if( FD_ISSET( server_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200966 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200967 if( ( ret = handle_message( "S -> C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200968 &client_fd, &server_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200969 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200970 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100971
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200972 }
973
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100974 exit_code = MBEDTLS_EXIT_SUCCESS;
975
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200976exit:
977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978#ifdef MBEDTLS_ERROR_C
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100979 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200980 {
981 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 mbedtls_strerror( ret, error_buf, 100 );
983 mbedtls_printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200984 fflush( stdout );
985 }
986#endif
987
Hanno Becker01ea7782018-08-17 13:33:41 +0100988 for( delay_idx = 0; delay_idx < MAX_DELAYED_HS; delay_idx++ )
989 {
Shawn Carey7b81dcf2021-05-13 09:08:02 -0400990 mbedtls_free( opt.delay_cli[delay_idx] );
991 mbedtls_free( opt.delay_srv[delay_idx] );
Hanno Becker01ea7782018-08-17 13:33:41 +0100992 }
993
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200994 mbedtls_net_free( &client_fd );
995 mbedtls_net_free( &server_fd );
996 mbedtls_net_free( &listen_fd );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200997
998#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 mbedtls_printf( " Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001000 fflush( stdout ); getchar();
1001#endif
1002
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +02001003 mbedtls_exit( exit_code );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001004}
1005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006#endif /* MBEDTLS_NET_C */