blob: 5a4968a894982a380f375716c5e414dd6e0fb59a [file] [log] [blame]
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001/*
2 * UDP proxy: emulate an unreliable UDP connexion for DTLS testing
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 * **********
45 *
Manuel Pégourié-Gonnarde4d48902015-03-06 13:40:52 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020047 */
48
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020049/*
50 * Warning: this is an internal utility program we use for tests.
51 * It does break some abstractions from the NET layer, and is thus NOT an
52 * example of good general usage.
53 */
54
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020057#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020059#endif
60
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/platform.h"
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000063#else
Simon Butcherd3138c32016-04-27 01:26:50 +010064#include <stdio.h>
65#include <stdlib.h>
66#include <time.h>
Andres Amaya Garcia80081a62018-04-29 21:58:53 +010067#define mbedtls_time time
68#define mbedtls_time_t time_t
69#define mbedtls_printf printf
Hanno Becker01ea7782018-08-17 13:33:41 +010070#define mbedtls_calloc calloc
71#define mbedtls_free free
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020072#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010073#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia80081a62018-04-29 21:58:53 +010074#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
75#endif /* MBEDTLS_PLATFORM_C */
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#if !defined(MBEDTLS_NET_C)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020078int main( void )
79{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 mbedtls_printf( "MBEDTLS_NET_C not defined.\n" );
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020081 mbedtls_exit( 0 );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020082}
83#else
84
Andres AG788aa4a2016-09-14 14:32:09 +010085#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000086#include "mbedtls/error.h"
87#include "mbedtls/ssl.h"
Hanno Becker0cc77742017-10-31 14:10:07 +000088#include "mbedtls/timing.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020089
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +000090#include <string.h>
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020091
92/* For select() */
93#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
94 !defined(EFI32)
95#include <winsock2.h>
96#include <windows.h>
97#if defined(_MSC_VER)
98#if defined(_WIN32_WCE)
99#pragma comment( lib, "ws2.lib" )
100#else
101#pragma comment( lib, "ws2_32.lib" )
102#endif
103#endif /* _MSC_VER */
104#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
105#include <sys/time.h>
106#include <sys/types.h>
107#include <unistd.h>
108#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
109
Manuel Pégourié-Gonnardb46780e2014-09-23 12:17:30 +0200110#define MAX_MSG_SIZE 16384 + 2048 /* max record/datagram size */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200111
112#define DFL_SERVER_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200113#define DFL_SERVER_PORT "4433"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200114#define DFL_LISTEN_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200115#define DFL_LISTEN_PORT "5556"
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100116#define DFL_PACK 0
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200117
Hanno Becker0cc77742017-10-31 14:10:07 +0000118#if defined(MBEDTLS_TIMING_C)
119#define USAGE_PACK \
120 " pack=%%d default: 0 (don't pack)\n" \
121 " options: t > 0 (pack for t milliseconds)\n"
122#else
123#define USAGE_PACK
124#endif
125
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200126#define USAGE \
127 "\n usage: udp_proxy param=<>...\n" \
128 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200129 " server_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200130 " server_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200131 " listen_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200132 " listen_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200133 "\n" \
134 " duplicate=%%d default: 0 (no duplication)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200135 " duplicate about 1:N packets randomly\n" \
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200136 " delay=%%d default: 0 (no delayed packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200137 " delay about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200138 " delay_ccs=0/1 default: 0 (don't delay ChangeCipherSpec)\n" \
Hanno Becker01ea7782018-08-17 13:33:41 +0100139 " delay_cli=%%s Handshake message from client that should be\n"\
140 " delayed. Possible values are 'ClientHello',\n" \
141 " 'Certificate', 'CertificateVerify', and\n" \
142 " 'ClientKeyExchange'.\n" \
143 " May be used multiple times, even for the same\n"\
144 " message, in which case the respective message\n"\
145 " gets delayed multiple times.\n" \
146 " delay_srv=%%s Handshake message from server that should be\n"\
147 " delayed. Possible values are 'HelloRequest',\n"\
148 " 'ServerHello', 'ServerHelloDone', 'Certificate'\n"\
149 " 'ServerKeyExchange', 'NewSessionTicket',\n"\
150 " 'HelloVerifyRequest' and ''CertificateRequest'.\n"\
151 " May be used multiple times, even for the same\n"\
152 " message, in which case the respective message\n"\
153 " gets delayed multiple times.\n" \
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200154 " drop=%%d default: 0 (no dropped packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200155 " drop about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200156 " mtu=%%d default: 0 (unlimited)\n" \
157 " drop packets larger than N bytes\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200158 " bad_ad=0/1 default: 0 (don't add bad ApplicationData)\n" \
159 " protect_hvr=0/1 default: 0 (don't protect HelloVerifyRequest)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000160 " protect_len=%%d default: (don't protect packets of this size)\n" \
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100161 " inject_clihlo=0/1 default: 0 (don't inject fake ClientHello)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200162 "\n" \
163 " seed=%%d default: (use current time)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000164 USAGE_PACK \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200165 "\n"
166
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200167/*
168 * global options
169 */
Hanno Becker01ea7782018-08-17 13:33:41 +0100170
171#define MAX_DELAYED_HS 10
172
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200173static struct options
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200174{
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200175 const char *server_addr; /* address to forward packets to */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200176 const char *server_port; /* port to forward packets to */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200177 const char *listen_addr; /* address for accepting client connections */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200178 const char *listen_port; /* port for accepting client connections */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200179
180 int duplicate; /* duplicate 1 in N packets (none if 0) */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200181 int delay; /* delay 1 packet in N (none if 0) */
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200182 int delay_ccs; /* delay ChangeCipherSpec */
Hanno Becker01ea7782018-08-17 13:33:41 +0100183 char* delay_cli[MAX_DELAYED_HS]; /* handshake types of messages from
Hanno Becker41038102018-08-28 11:15:32 +0100184 * client that should be delayed. */
Hanno Becker01ea7782018-08-17 13:33:41 +0100185 uint8_t delay_cli_cnt; /* Number of entries in delay_cli. */
186 char* delay_srv[MAX_DELAYED_HS]; /* handshake types of messages from
Hanno Becker41038102018-08-28 11:15:32 +0100187 * server that should be delayed. */
Hanno Becker01ea7782018-08-17 13:33:41 +0100188 uint8_t delay_srv_cnt; /* Number of entries in delay_srv. */
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200189 int drop; /* drop 1 packet in N (none if 0) */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200190 int mtu; /* drop packets larger than this */
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200191 int bad_ad; /* inject corrupted ApplicationData record */
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200192 int protect_hvr; /* never drop or delay HelloVerifyRequest */
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200193 int protect_len; /* never drop/delay packet of the given size*/
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100194 int inject_clihlo; /* inject fake ClientHello after handshake */
Hanno Becker77abef52017-11-02 10:50:28 +0000195 unsigned pack; /* merge packets into single datagram for
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100196 * at most \c merge milliseconds if > 0 */
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200197 unsigned int seed; /* seed for "random" events */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200198} opt;
199
200static void exit_usage( const char *name, const char *value )
201{
202 if( value == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 mbedtls_printf( " unknown option or missing value: %s\n", name );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200204 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 mbedtls_printf( " option %s: illegal value: %s\n", name, value );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 mbedtls_printf( USAGE );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200208 exit( 1 );
209}
210
211static void get_options( int argc, char *argv[] )
212{
213 int i;
214 char *p, *q;
215
216 opt.server_addr = DFL_SERVER_ADDR;
217 opt.server_port = DFL_SERVER_PORT;
218 opt.listen_addr = DFL_LISTEN_ADDR;
219 opt.listen_port = DFL_LISTEN_PORT;
Hanno Becker211f44c2017-10-31 14:08:10 +0000220 opt.pack = DFL_PACK;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200221 /* Other members default to 0 */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200222
Hanno Becker01ea7782018-08-17 13:33:41 +0100223 opt.delay_cli_cnt = 0;
224 opt.delay_srv_cnt = 0;
225 memset( opt.delay_cli, 0, sizeof( opt.delay_cli ) );
226 memset( opt.delay_srv, 0, sizeof( opt.delay_srv ) );
227
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200228 for( i = 1; i < argc; i++ )
229 {
230 p = argv[i];
231 if( ( q = strchr( p, '=' ) ) == NULL )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200232 exit_usage( p, NULL );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200233 *q++ = '\0';
234
235 if( strcmp( p, "server_addr" ) == 0 )
236 opt.server_addr = q;
237 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200238 opt.server_port = q;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200239 else if( strcmp( p, "listen_addr" ) == 0 )
240 opt.listen_addr = q;
241 else if( strcmp( p, "listen_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200242 opt.listen_port = q;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200243 else if( strcmp( p, "duplicate" ) == 0 )
244 {
245 opt.duplicate = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200246 if( opt.duplicate < 0 || opt.duplicate > 20 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200247 exit_usage( p, q );
248 }
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200249 else if( strcmp( p, "delay" ) == 0 )
250 {
251 opt.delay = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200252 if( opt.delay < 0 || opt.delay > 20 || opt.delay == 1 )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200253 exit_usage( p, q );
254 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200255 else if( strcmp( p, "delay_ccs" ) == 0 )
256 {
257 opt.delay_ccs = atoi( q );
258 if( opt.delay_ccs < 0 || opt.delay_ccs > 1 )
259 exit_usage( p, q );
260 }
Hanno Becker01ea7782018-08-17 13:33:41 +0100261 else if( strcmp( p, "delay_cli" ) == 0 ||
262 strcmp( p, "delay_srv" ) == 0 )
263 {
264 uint8_t *delay_cnt;
265 char **delay_list;
266 size_t len;
267 char *buf;
268
269 if( strcmp( p, "delay_cli" ) == 0 )
270 {
271 delay_cnt = &opt.delay_cli_cnt;
272 delay_list = opt.delay_cli;
273 }
274 else
275 {
276 delay_cnt = &opt.delay_srv_cnt;
277 delay_list = opt.delay_srv;
278 }
279
280 if( *delay_cnt == MAX_DELAYED_HS )
281 {
Hanno Beckerf34a4c12018-08-28 17:22:26 +0100282 mbedtls_printf( " too many uses of %s: only %d allowed\n",
283 p, MAX_DELAYED_HS );
Hanno Becker01ea7782018-08-17 13:33:41 +0100284 exit_usage( p, NULL );
285 }
286
287 len = strlen( q );
288 buf = mbedtls_calloc( 1, len + 1 );
289 if( buf == NULL )
290 {
291 mbedtls_printf( " Allocation failure\n" );
292 exit( 1 );
293 }
294 memcpy( buf, q, len + 1 );
295
296 delay_list[ (*delay_cnt)++ ] = buf;
297 }
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200298 else if( strcmp( p, "drop" ) == 0 )
299 {
300 opt.drop = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200301 if( opt.drop < 0 || opt.drop > 20 || opt.drop == 1 )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200302 exit_usage( p, q );
303 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100304 else if( strcmp( p, "pack" ) == 0 )
305 {
Hanno Becker0cc77742017-10-31 14:10:07 +0000306#if defined(MBEDTLS_TIMING_C)
Hanno Becker77abef52017-11-02 10:50:28 +0000307 opt.pack = (unsigned) atoi( q );
Hanno Becker0cc77742017-10-31 14:10:07 +0000308#else
309 mbedtls_printf( " option pack only defined if MBEDTLS_TIMING_C is enabled\n" );
310 exit( 1 );
311#endif
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100312 }
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200313 else if( strcmp( p, "mtu" ) == 0 )
314 {
315 opt.mtu = atoi( q );
316 if( opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE )
317 exit_usage( p, q );
318 }
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200319 else if( strcmp( p, "bad_ad" ) == 0 )
320 {
321 opt.bad_ad = atoi( q );
322 if( opt.bad_ad < 0 || opt.bad_ad > 1 )
323 exit_usage( p, q );
324 }
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200325 else if( strcmp( p, "protect_hvr" ) == 0 )
326 {
327 opt.protect_hvr = atoi( q );
328 if( opt.protect_hvr < 0 || opt.protect_hvr > 1 )
329 exit_usage( p, q );
330 }
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200331 else if( strcmp( p, "protect_len" ) == 0 )
332 {
333 opt.protect_len = atoi( q );
334 if( opt.protect_len < 0 )
335 exit_usage( p, q );
336 }
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100337 else if( strcmp( p, "inject_clihlo" ) == 0 )
338 {
339 opt.inject_clihlo = atoi( q );
340 if( opt.inject_clihlo < 0 || opt.inject_clihlo > 1 )
341 exit_usage( p, q );
342 }
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200343 else if( strcmp( p, "seed" ) == 0 )
344 {
345 opt.seed = atoi( q );
346 if( opt.seed == 0 )
347 exit_usage( p, q );
348 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200349 else
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200350 exit_usage( p, NULL );
351 }
352}
353
354static const char *msg_type( unsigned char *msg, size_t len )
355{
356 if( len < 1 ) return( "Invalid" );
357 switch( msg[0] )
358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" );
360 case MBEDTLS_SSL_MSG_ALERT: return( "Alert" );
361 case MBEDTLS_SSL_MSG_APPLICATION_DATA: return( "ApplicationData" );
362 case MBEDTLS_SSL_MSG_HANDSHAKE: break; /* See below */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200363 default: return( "Unknown" );
364 }
365
Manuel Pégourié-Gonnard8cc7e032014-09-25 12:59:05 +0200366 if( len < 13 + 12 ) return( "Invalid handshake" );
367
368 /*
369 * Our handshake message are less than 2^16 bytes long, so they should
370 * have 0 as the first byte of length, frag_offset and frag_length.
371 * Otherwise, assume they are encrypted.
372 */
373 if( msg[14] || msg[19] || msg[22] ) return( "Encrypted handshake" );
374
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200375 switch( msg[13] )
376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 case MBEDTLS_SSL_HS_HELLO_REQUEST: return( "HelloRequest" );
378 case MBEDTLS_SSL_HS_CLIENT_HELLO: return( "ClientHello" );
379 case MBEDTLS_SSL_HS_SERVER_HELLO: return( "ServerHello" );
380 case MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" );
381 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" );
382 case MBEDTLS_SSL_HS_CERTIFICATE: return( "Certificate" );
383 case MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" );
384 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" );
385 case MBEDTLS_SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" );
386 case MBEDTLS_SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" );
387 case MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" );
388 case MBEDTLS_SSL_HS_FINISHED: return( "Finished" );
Manuel Pégourié-Gonnardbc010a02014-09-20 12:40:51 +0200389 default: return( "Unknown handshake" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200390 }
391}
392
Hanno Becker0cc77742017-10-31 14:10:07 +0000393#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200394/* Return elapsed time in milliseconds since the first call */
Hanno Becker77abef52017-11-02 10:50:28 +0000395static unsigned ellapsed_time( void )
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200396{
Hanno Becker92474da2017-10-31 14:09:30 +0000397 static int initialized = 0;
398 static struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200399
Hanno Becker92474da2017-10-31 14:09:30 +0000400 if( initialized == 0 )
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200401 {
Hanno Becker92474da2017-10-31 14:09:30 +0000402 (void) mbedtls_timing_get_timer( &hires, 1 );
403 initialized = 1;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200404 return( 0 );
405 }
406
Hanno Becker92474da2017-10-31 14:09:30 +0000407 return( mbedtls_timing_get_timer( &hires, 0 ) );
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200408}
409
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200410typedef struct
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200411{
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100412 mbedtls_net_context *ctx;
413
414 const char *description;
415
Hanno Becker77abef52017-11-02 10:50:28 +0000416 unsigned packet_lifetime;
417 unsigned num_datagrams;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100418
419 unsigned char data[MAX_MSG_SIZE];
Hanno Beckera5e68972017-12-06 08:35:02 +0000420 size_t len;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100421
422} ctx_buffer;
423
424static ctx_buffer outbuf[2];
425
426static int ctx_buffer_flush( ctx_buffer *buf )
427{
428 int ret;
429
Hanno Becker77abef52017-11-02 10:50:28 +0000430 mbedtls_printf( " %05u flush %s: %u bytes, %u datagrams, last %u ms\n",
431 ellapsed_time(), buf->description,
Hanno Beckera5e68972017-12-06 08:35:02 +0000432 (unsigned) buf->len, buf->num_datagrams,
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100433 ellapsed_time() - buf->packet_lifetime );
434
435 ret = mbedtls_net_send( buf->ctx, buf->data, buf->len );
436
437 buf->len = 0;
438 buf->num_datagrams = 0;
439
440 return( ret );
441}
442
Hanno Becker77abef52017-11-02 10:50:28 +0000443static unsigned ctx_buffer_time_remaining( ctx_buffer *buf )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100444{
Hanno Becker77abef52017-11-02 10:50:28 +0000445 unsigned const cur_time = ellapsed_time();
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100446
Hanno Becker77abef52017-11-02 10:50:28 +0000447 if( buf->num_datagrams == 0 )
448 return( (unsigned) -1 );
449
450 if( cur_time - buf->packet_lifetime >= opt.pack )
451 return( 0 );
452
453 return( opt.pack - ( cur_time - buf->packet_lifetime ) );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100454}
455
456static int ctx_buffer_append( ctx_buffer *buf,
457 const unsigned char * data,
458 size_t len )
459{
460 int ret;
461
Hanno Beckera5e68972017-12-06 08:35:02 +0000462 if( len > (size_t) INT_MAX )
463 return( -1 );
464
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100465 if( len > sizeof( buf->data ) )
466 {
Hanno Becker77abef52017-11-02 10:50:28 +0000467 mbedtls_printf( " ! buffer size %u too large (max %u)\n",
468 (unsigned) len, (unsigned) sizeof( buf->data ) );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100469 return( -1 );
470 }
471
472 if( sizeof( buf->data ) - buf->len < len )
473 {
474 if( ( ret = ctx_buffer_flush( buf ) ) <= 0 )
475 return( ret );
476 }
477
478 memcpy( buf->data + buf->len, data, len );
479
480 buf->len += len;
481 if( ++buf->num_datagrams == 1 )
482 buf->packet_lifetime = ellapsed_time();
483
Hanno Beckera5e68972017-12-06 08:35:02 +0000484 return( (int) len );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100485}
Hanno Becker77abef52017-11-02 10:50:28 +0000486#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100487
488static int dispatch_data( mbedtls_net_context *ctx,
489 const unsigned char * data,
490 size_t len )
491{
Hanno Becker77abef52017-11-02 10:50:28 +0000492#if defined(MBEDTLS_TIMING_C)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100493 ctx_buffer *buf = NULL;
Hanno Becker77abef52017-11-02 10:50:28 +0000494 if( opt.pack > 0 )
495 {
496 if( outbuf[0].ctx == ctx )
497 buf = &outbuf[0];
498 else if( outbuf[1].ctx == ctx )
499 buf = &outbuf[1];
Hanno Becker0cc77742017-10-31 14:10:07 +0000500
Hanno Becker77abef52017-11-02 10:50:28 +0000501 if( buf == NULL )
502 return( -1 );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100503
Hanno Becker77abef52017-11-02 10:50:28 +0000504 return( ctx_buffer_append( buf, data, len ) );
505 }
506#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100507
Hanno Becker0cc77742017-10-31 14:10:07 +0000508 return( mbedtls_net_send( ctx, data, len ) );
509}
510
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100511typedef struct
512{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200513 mbedtls_net_context *dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200514 const char *way;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200515 const char *type;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200516 unsigned len;
517 unsigned char buf[MAX_MSG_SIZE];
518} packet;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200519
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200520/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */
521void print_packet( const packet *p, const char *why )
522{
Hanno Becker0cc77742017-10-31 14:10:07 +0000523#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200524 if( why == NULL )
Hanno Becker77abef52017-11-02 10:50:28 +0000525 mbedtls_printf( " %05u dispatch %s %s (%u bytes)\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200526 ellapsed_time(), p->way, p->type, p->len );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200527 else
Hanno Becker77abef52017-11-02 10:50:28 +0000528 mbedtls_printf( " %05u dispatch %s %s (%u bytes): %s\n",
Hanno Becker0cc77742017-10-31 14:10:07 +0000529 ellapsed_time(), p->way, p->type, p->len, why );
530#else
531 if( why == NULL )
532 mbedtls_printf( " dispatch %s %s (%u bytes)\n",
533 p->way, p->type, p->len );
534 else
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100535 mbedtls_printf( " dispatch %s %s (%u bytes): %s\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200536 p->way, p->type, p->len, why );
Hanno Becker0cc77742017-10-31 14:10:07 +0000537#endif
538
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200539 fflush( stdout );
540}
541
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100542/*
543 * In order to test the server's behaviour when receiving a ClientHello after
544 * the connection is established (this could be a hard reset from the client,
545 * but the server must not drop the existing connection before establishing
546 * client reachability, see RFC 6347 Section 4.2.8), we memorize the first
547 * ClientHello we see (which can't have a cookie), then replay it after the
548 * first ApplicationData record - then we're done.
549 *
550 * This is controlled by the inject_clihlo option.
551 *
552 * We want an explicit state and a place to store the packet.
553 */
Manuel Pégourié-Gonnard7fe5ac12020-03-30 12:46:21 +0200554typedef enum {
555 ICH_INIT, /* haven't seen the first ClientHello yet */
556 ICH_CACHED, /* cached the initial ClientHello */
557 ICH_INJECTED, /* ClientHello already injected, done */
558} inject_clihlo_state_t;
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100559
Manuel Pégourié-Gonnard7fe5ac12020-03-30 12:46:21 +0200560static inject_clihlo_state_t inject_clihlo_state;
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100561static packet initial_clihlo;
562
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200563int send_packet( const packet *p, const char *why )
564{
565 int ret;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200566 mbedtls_net_context *dst = p->dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200567
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100568 /* save initial ClientHello? */
569 if( opt.inject_clihlo != 0 &&
Manuel Pégourié-Gonnard7fe5ac12020-03-30 12:46:21 +0200570 inject_clihlo_state == ICH_INIT &&
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100571 strcmp( p->type, "ClientHello" ) == 0 )
572 {
573 memcpy( &initial_clihlo, p, sizeof( packet ) );
Manuel Pégourié-Gonnard7fe5ac12020-03-30 12:46:21 +0200574 inject_clihlo_state = ICH_CACHED;
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100575 }
576
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200577 /* insert corrupted ApplicationData record? */
578 if( opt.bad_ad &&
579 strcmp( p->type, "ApplicationData" ) == 0 )
580 {
581 unsigned char buf[MAX_MSG_SIZE];
582 memcpy( buf, p->buf, p->len );
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200583
Hanno Beckerfbb0b702017-05-26 16:55:07 +0100584 if( p->len <= 13 )
585 {
586 mbedtls_printf( " ! can't corrupt empty AD record" );
587 }
588 else
589 {
590 ++buf[13];
591 print_packet( p, "corrupted" );
592 }
593
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100594 if( ( ret = dispatch_data( dst, buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200595 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100596 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200597 return( ret );
598 }
599 }
600
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200601 print_packet( p, why );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100602 if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200603 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100604 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200605 return( ret );
606 }
607
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200608 /* Don't duplicate Application Data, only handshake covered */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200609 if( opt.duplicate != 0 &&
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200610 strcmp( p->type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200611 rand() % opt.duplicate == 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200612 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200613 print_packet( p, "duplicated" );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200614
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100615 if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200616 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100617 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200618 return( ret );
619 }
620 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200621
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100622 /* Inject ClientHello after first ApplicationData */
623 if( opt.inject_clihlo != 0 &&
Manuel Pégourié-Gonnard7fe5ac12020-03-30 12:46:21 +0200624 inject_clihlo_state == ICH_CACHED &&
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100625 strcmp( p->type, "ApplicationData" ) == 0 )
626 {
627 print_packet( &initial_clihlo, "injected" );
628
629 if( ( ret = dispatch_data( dst, initial_clihlo.buf,
630 initial_clihlo.len ) ) <= 0 )
631 {
632 mbedtls_printf( " ! dispatch returned %d\n", ret );
633 return( ret );
634 }
635
Manuel Pégourié-Gonnard7fe5ac12020-03-30 12:46:21 +0200636 inject_clihlo_state = ICH_INJECTED;
Manuel Pégourié-Gonnardb85ce9e2020-03-13 11:11:02 +0100637 }
638
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200639 return( 0 );
640}
641
Hanno Becker101bcba2018-08-21 16:39:51 +0100642#define MAX_DELAYED_MSG 5
643static size_t prev_len;
644static packet prev[MAX_DELAYED_MSG];
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200645
646void clear_pending( void )
647{
Hanno Becker12b72c12018-08-23 13:15:36 +0100648 memset( &prev, 0, sizeof( prev ) );
Hanno Becker101bcba2018-08-21 16:39:51 +0100649 prev_len = 0;
650}
651
652void delay_packet( packet *delay )
653{
654 if( prev_len == MAX_DELAYED_MSG )
655 return;
656
657 memcpy( &prev[prev_len++], delay, sizeof( packet ) );
658}
659
660int send_delayed()
661{
662 uint8_t offset;
663 int ret;
664 for( offset = 0; offset < prev_len; offset++ )
665 {
666 ret = send_packet( &prev[offset], "delayed" );
667 if( ret != 0 )
668 return( ret );
669 }
670
671 clear_pending();
672 return( 0 );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200673}
674
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200675/*
676 * Avoid dropping or delaying a packet that was already dropped twice: this
677 * only results in uninteresting timeouts. We can't rely on type to identify
678 * packets, since during renegotiation they're all encrypted. So, rely on
679 * size mod 2048 (which is usually just size).
680 */
681static unsigned char dropped[2048] = { 0 };
682#define DROP_MAX 2
683
Hanno Beckerbcf97ec2019-06-04 13:04:28 +0100684/* We only drop packets at the level of entire datagrams, not at the level
685 * of records. In particular, if the peer changes the way it packs multiple
686 * records into a single datagram, we don't necessarily count the number of
687 * times a record has been dropped correctly. However, the only known reason
688 * why a peer would change datagram packing is disabling the latter on
689 * retransmission, in which case we'd drop involved records at most
690 * DROP_MAX + 1 times. */
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200691void update_dropped( const packet *p )
692{
693 size_t id = p->len % sizeof( dropped );
Manuel Pégourié-Gonnardfa60f122014-09-26 16:07:29 +0200694 ++dropped[id];
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200695}
696
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200697int handle_message( const char *way,
698 mbedtls_net_context *dst,
699 mbedtls_net_context *src )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200700{
701 int ret;
702 packet cur;
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200703 size_t id;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200704
Hanno Becker01ea7782018-08-17 13:33:41 +0100705 uint8_t delay_idx;
706 char ** delay_list;
707 uint8_t delay_list_len;
708
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +0200709 /* receive packet */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200710 if( ( ret = mbedtls_net_recv( src, cur.buf, sizeof( cur.buf ) ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 mbedtls_printf( " ! mbedtls_net_recv returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200713 return( ret );
714 }
715
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200716 cur.len = ret;
717 cur.type = msg_type( cur.buf, cur.len );
718 cur.way = way;
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200719 cur.dst = dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200720 print_packet( &cur, NULL );
721
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200722 id = cur.len % sizeof( dropped );
723
Hanno Becker01ea7782018-08-17 13:33:41 +0100724 if( strcmp( way, "S <- C" ) == 0 )
725 {
726 delay_list = opt.delay_cli;
727 delay_list_len = opt.delay_cli_cnt;
728 }
729 else
730 {
731 delay_list = opt.delay_srv;
732 delay_list_len = opt.delay_srv_cnt;
733 }
Hanno Beckercf469452018-08-28 10:09:47 +0100734
Hanno Becker01ea7782018-08-17 13:33:41 +0100735 /* Check if message type is in the list of messages
736 * that should be delayed */
737 for( delay_idx = 0; delay_idx < delay_list_len; delay_idx++ )
738 {
739 if( delay_list[ delay_idx ] == NULL )
740 continue;
741
742 if( strcmp( delay_list[ delay_idx ], cur.type ) == 0 )
743 {
744 /* Delay message */
Hanno Becker101bcba2018-08-21 16:39:51 +0100745 delay_packet( &cur );
Hanno Becker01ea7782018-08-17 13:33:41 +0100746
747 /* Remove entry from list */
748 mbedtls_free( delay_list[delay_idx] );
749 delay_list[delay_idx] = NULL;
750
751 return( 0 );
752 }
753 }
754
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200755 /* do we want to drop, delay, or forward it? */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200756 if( ( opt.mtu != 0 &&
757 cur.len > (unsigned) opt.mtu ) ||
758 ( opt.drop != 0 &&
759 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200760 ! ( opt.protect_hvr &&
761 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200762 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200763 dropped[id] < DROP_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200764 rand() % opt.drop == 0 ) )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200765 {
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200766 update_dropped( &cur );
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200767 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200768 else if( ( opt.delay_ccs == 1 &&
769 strcmp( cur.type, "ChangeCipherSpec" ) == 0 ) ||
770 ( opt.delay != 0 &&
771 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200772 ! ( opt.protect_hvr &&
773 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200774 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200775 dropped[id] < DROP_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200776 rand() % opt.delay == 0 ) )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200777 {
Hanno Becker101bcba2018-08-21 16:39:51 +0100778 delay_packet( &cur );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200779 }
780 else
781 {
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200782 /* forward and possibly duplicate */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200783 if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 )
784 return( ret );
785
Hanno Becker101bcba2018-08-21 16:39:51 +0100786 /* send previously delayed messages if any */
787 ret = send_delayed();
788 if( ret != 0 )
789 return( ret );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200790 }
791
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200792 return( 0 );
793}
794
795int main( int argc, char *argv[] )
796{
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100797 int ret = 1;
798 int exit_code = MBEDTLS_EXIT_FAILURE;
Hanno Becker01ea7782018-08-17 13:33:41 +0100799 uint8_t delay_idx;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200800
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200801 mbedtls_net_context listen_fd, client_fd, server_fd;
Hanno Becker77abef52017-11-02 10:50:28 +0000802
803#if defined( MBEDTLS_TIMING_C )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100804 struct timeval tm;
Hanno Becker77abef52017-11-02 10:50:28 +0000805#endif
806
807 struct timeval *tm_ptr = NULL;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200808
809 int nb_fds;
810 fd_set read_fds;
811
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200812 mbedtls_net_init( &listen_fd );
813 mbedtls_net_init( &client_fd );
814 mbedtls_net_init( &server_fd );
815
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200816 get_options( argc, argv );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200817
818 /*
819 * Decisions to drop/delay/duplicate packets are pseudo-random: dropping
820 * exactly 1 in N packets would lead to problems when a flight has exactly
821 * N packets: the same packet would be dropped on every resend.
822 *
823 * In order to be able to reproduce problems reliably, the seed may be
824 * specified explicitly.
825 */
826 if( opt.seed == 0 )
827 {
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200828 opt.seed = (unsigned int) time( NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 mbedtls_printf( " . Pseudo-random seed: %u\n", opt.seed );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200830 }
831
832 srand( opt.seed );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200833
834 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200835 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200836 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200837 mbedtls_printf( " . Connect to server on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200838 opt.server_addr, opt.server_port );
839 fflush( stdout );
840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
842 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200845 goto exit;
846 }
847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200849
850 /*
851 * 1. Setup the "listening" UDP socket
852 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200853 mbedtls_printf( " . Bind on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200854 opt.listen_addr, opt.listen_port );
855 fflush( stdout );
856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 if( ( ret = mbedtls_net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
858 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200861 goto exit;
862 }
863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200865
866 /*
867 * 2. Wait until a client connects
868 */
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200869accept:
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200870 mbedtls_net_free( &client_fd );
871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 mbedtls_printf( " . Waiting for a remote connection ..." );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200873 fflush( stdout );
874
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200875 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200876 NULL, 0, NULL ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 mbedtls_printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200879 goto exit;
880 }
881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200883
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200884 /*
885 * 3. Forward packets forever (kill the process to terminate it)
886 */
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200887 clear_pending();
888 memset( dropped, 0, sizeof( dropped ) );
889
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200890 nb_fds = client_fd.fd;
891 if( nb_fds < server_fd.fd )
892 nb_fds = server_fd.fd;
893 if( nb_fds < listen_fd.fd )
894 nb_fds = listen_fd.fd;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200895 ++nb_fds;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200896
Hanno Becker0cc77742017-10-31 14:10:07 +0000897#if defined(MBEDTLS_TIMING_C)
Hanno Becker211f44c2017-10-31 14:08:10 +0000898 if( opt.pack > 0 )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100899 {
900 outbuf[0].ctx = &server_fd;
901 outbuf[0].description = "S <- C";
902 outbuf[0].num_datagrams = 0;
903 outbuf[0].len = 0;
904
905 outbuf[1].ctx = &client_fd;
906 outbuf[1].description = "S -> C";
907 outbuf[1].num_datagrams = 0;
908 outbuf[1].len = 0;
909 }
Hanno Becker0cc77742017-10-31 14:10:07 +0000910#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100911
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200912 while( 1 )
913 {
Hanno Becker77abef52017-11-02 10:50:28 +0000914#if defined(MBEDTLS_TIMING_C)
915 if( opt.pack > 0 )
916 {
917 unsigned max_wait_server, max_wait_client, max_wait;
918 max_wait_server = ctx_buffer_time_remaining( &outbuf[0] );
919 max_wait_client = ctx_buffer_time_remaining( &outbuf[1] );
920
921 max_wait = (unsigned) -1;
922
923 if( max_wait_server == 0 )
924 ctx_buffer_flush( &outbuf[0] );
925 else
926 max_wait = max_wait_server;
927
928 if( max_wait_client == 0 )
929 ctx_buffer_flush( &outbuf[1] );
930 else
931 {
932 if( max_wait_client < max_wait )
933 max_wait = max_wait_client;
934 }
935
936 if( max_wait != (unsigned) -1 )
937 {
938 tm.tv_sec = max_wait / 1000;
939 tm.tv_usec = ( max_wait % 1000 ) * 1000;
940
941 tm_ptr = &tm;
942 }
943 else
944 {
945 tm_ptr = NULL;
946 }
947 }
948#endif /* MBEDTLS_TIMING_C */
949
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200950 FD_ZERO( &read_fds );
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200951 FD_SET( server_fd.fd, &read_fds );
952 FD_SET( client_fd.fd, &read_fds );
953 FD_SET( listen_fd.fd, &read_fds );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200954
Hanno Becker77abef52017-11-02 10:50:28 +0000955 if( ( ret = select( nb_fds, &read_fds, NULL, NULL, tm_ptr ) ) < 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200956 {
957 perror( "select" );
958 goto exit;
959 }
960
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200961 if( FD_ISSET( listen_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200962 goto accept;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200963
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200964 if( FD_ISSET( client_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200965 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200966 if( ( ret = handle_message( "S <- C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200967 &server_fd, &client_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200968 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200969 }
970
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200971 if( FD_ISSET( server_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200972 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200973 if( ( ret = handle_message( "S -> C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200974 &client_fd, &server_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200975 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200976 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100977
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200978 }
979
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100980 exit_code = MBEDTLS_EXIT_SUCCESS;
981
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200982exit:
983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984#ifdef MBEDTLS_ERROR_C
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100985 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200986 {
987 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 mbedtls_strerror( ret, error_buf, 100 );
989 mbedtls_printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200990 fflush( stdout );
991 }
992#endif
993
Hanno Becker01ea7782018-08-17 13:33:41 +0100994 for( delay_idx = 0; delay_idx < MAX_DELAYED_HS; delay_idx++ )
995 {
996 mbedtls_free( opt.delay_cli + delay_idx );
997 mbedtls_free( opt.delay_srv + delay_idx );
998 }
999
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001000 mbedtls_net_free( &client_fd );
1001 mbedtls_net_free( &server_fd );
1002 mbedtls_net_free( &listen_fd );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001003
1004#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 mbedtls_printf( " Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001006 fflush( stdout ); getchar();
1007#endif
1008
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +02001009 mbedtls_exit( exit_code );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001010}
1011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012#endif /* MBEDTLS_NET_C */