blob: 6546e8fd0a7de1bc31844fd6602187cb7f20b98a [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úti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020018 */
19
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020020/*
21 * Warning: this is an internal utility program we use for tests.
22 * It does break some abstractions from the NET layer, and is thus NOT an
23 * example of good general usage.
24 */
25
Mateusz Starzyk6c2e9b62021-05-19 17:54:54 +020026#define MBEDTLS_ALLOW_PRIVATE_ACCESS
27
Bence Szépkútic662b362021-05-27 11:25:03 +020028#include "mbedtls/build_info.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/platform.h"
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000032#else
Simon Butcherd3138c32016-04-27 01:26:50 +010033#include <stdio.h>
34#include <stdlib.h>
35#include <time.h>
Andres Amaya Garcia80081a62018-04-29 21:58:53 +010036#define mbedtls_time time
37#define mbedtls_time_t time_t
38#define mbedtls_printf printf
Hanno Becker01ea7782018-08-17 13:33:41 +010039#define mbedtls_calloc calloc
40#define mbedtls_free free
k-stachowiak776521a2019-05-23 09:46:47 +020041#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010042#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia80081a62018-04-29 21:58:53 +010043#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
44#endif /* MBEDTLS_PLATFORM_C */
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if !defined(MBEDTLS_NET_C)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020047int main( void )
48{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049 mbedtls_printf( "MBEDTLS_NET_C not defined.\n" );
k-stachowiak776521a2019-05-23 09:46:47 +020050 mbedtls_exit( 0 );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020051}
52#else
53
Andres AG788aa4a2016-09-14 14:32:09 +010054#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/error.h"
56#include "mbedtls/ssl.h"
Hanno Becker0cc77742017-10-31 14:10:07 +000057#include "mbedtls/timing.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020058
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +000059#include <string.h>
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020060
61/* For select() */
62#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
63 !defined(EFI32)
64#include <winsock2.h>
65#include <windows.h>
66#if defined(_MSC_VER)
67#if defined(_WIN32_WCE)
68#pragma comment( lib, "ws2.lib" )
69#else
70#pragma comment( lib, "ws2_32.lib" )
71#endif
72#endif /* _MSC_VER */
73#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
74#include <sys/time.h>
75#include <sys/types.h>
76#include <unistd.h>
77#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
78
Manuel Pégourié-Gonnardb46780e2014-09-23 12:17:30 +020079#define MAX_MSG_SIZE 16384 + 2048 /* max record/datagram size */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020080
81#define DFL_SERVER_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020082#define DFL_SERVER_PORT "4433"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020083#define DFL_LISTEN_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020084#define DFL_LISTEN_PORT "5556"
Hanno Becker1dd62ea2017-05-22 14:30:59 +010085#define DFL_PACK 0
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020086
Hanno Becker0cc77742017-10-31 14:10:07 +000087#if defined(MBEDTLS_TIMING_C)
88#define USAGE_PACK \
89 " pack=%%d default: 0 (don't pack)\n" \
90 " options: t > 0 (pack for t milliseconds)\n"
91#else
92#define USAGE_PACK
93#endif
94
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020095#define USAGE \
96 "\n usage: udp_proxy param=<>...\n" \
97 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +020098 " server_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020099 " server_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200100 " listen_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200101 " listen_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200102 "\n" \
103 " duplicate=%%d default: 0 (no duplication)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200104 " duplicate about 1:N packets randomly\n" \
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200105 " delay=%%d default: 0 (no delayed packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200106 " delay about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200107 " delay_ccs=0/1 default: 0 (don't delay ChangeCipherSpec)\n" \
Hanno Becker01ea7782018-08-17 13:33:41 +0100108 " delay_cli=%%s Handshake message from client that should be\n"\
109 " delayed. Possible values are 'ClientHello',\n" \
110 " 'Certificate', 'CertificateVerify', and\n" \
111 " 'ClientKeyExchange'.\n" \
112 " May be used multiple times, even for the same\n"\
113 " message, in which case the respective message\n"\
114 " gets delayed multiple times.\n" \
115 " delay_srv=%%s Handshake message from server that should be\n"\
116 " delayed. Possible values are 'HelloRequest',\n"\
117 " 'ServerHello', 'ServerHelloDone', 'Certificate'\n"\
118 " 'ServerKeyExchange', 'NewSessionTicket',\n"\
119 " 'HelloVerifyRequest' and ''CertificateRequest'.\n"\
120 " May be used multiple times, even for the same\n"\
121 " message, in which case the respective message\n"\
122 " gets delayed multiple times.\n" \
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200123 " drop=%%d default: 0 (no dropped packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200124 " drop about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200125 " mtu=%%d default: 0 (unlimited)\n" \
126 " drop packets larger than N bytes\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200127 " bad_ad=0/1 default: 0 (don't add bad ApplicationData)\n" \
Hanno Becker98aaf252019-05-24 10:07:42 +0100128 " bad_cid=%%d default: 0 (don't corrupt Connection IDs)\n" \
129 " duplicate 1:N packets containing a CID,\n" \
130 " modifying CID in first instance of the packet.\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200131 " protect_hvr=0/1 default: 0 (don't protect HelloVerifyRequest)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000132 " protect_len=%%d default: (don't protect packets of this size)\n" \
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100133 " inject_clihlo=0/1 default: 0 (don't inject fake ClientHello)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200134 "\n" \
135 " seed=%%d default: (use current time)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000136 USAGE_PACK \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200137 "\n"
138
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200139/*
140 * global options
141 */
Hanno Becker01ea7782018-08-17 13:33:41 +0100142
143#define MAX_DELAYED_HS 10
144
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200145static struct options
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200146{
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200147 const char *server_addr; /* address to forward packets to */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200148 const char *server_port; /* port to forward packets to */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200149 const char *listen_addr; /* address for accepting client connections */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200150 const char *listen_port; /* port for accepting client connections */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200151
152 int duplicate; /* duplicate 1 in N packets (none if 0) */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200153 int delay; /* delay 1 packet in N (none if 0) */
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200154 int delay_ccs; /* delay ChangeCipherSpec */
Hanno Becker01ea7782018-08-17 13:33:41 +0100155 char* delay_cli[MAX_DELAYED_HS]; /* handshake types of messages from
Hanno Becker41038102018-08-28 11:15:32 +0100156 * client that should be delayed. */
Hanno Becker01ea7782018-08-17 13:33:41 +0100157 uint8_t delay_cli_cnt; /* Number of entries in delay_cli. */
158 char* delay_srv[MAX_DELAYED_HS]; /* handshake types of messages from
Hanno Becker41038102018-08-28 11:15:32 +0100159 * server that should be delayed. */
Hanno Becker01ea7782018-08-17 13:33:41 +0100160 uint8_t delay_srv_cnt; /* Number of entries in delay_srv. */
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200161 int drop; /* drop 1 packet in N (none if 0) */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200162 int mtu; /* drop packets larger than this */
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200163 int bad_ad; /* inject corrupted ApplicationData record */
Hanno Becker98aaf252019-05-24 10:07:42 +0100164 unsigned bad_cid; /* inject corrupted CID record */
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200165 int protect_hvr; /* never drop or delay HelloVerifyRequest */
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200166 int protect_len; /* never drop/delay packet of the given size*/
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100167 int inject_clihlo; /* inject fake ClientHello after handshake */
Hanno Becker77abef52017-11-02 10:50:28 +0000168 unsigned pack; /* merge packets into single datagram for
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100169 * at most \c merge milliseconds if > 0 */
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200170 unsigned int seed; /* seed for "random" events */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200171} opt;
172
173static void exit_usage( const char *name, const char *value )
174{
175 if( value == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_printf( " unknown option or missing value: %s\n", name );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200177 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_printf( " option %s: illegal value: %s\n", name, value );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 mbedtls_printf( USAGE );
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200181 mbedtls_exit( 1 );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200182}
183
184static void get_options( int argc, char *argv[] )
185{
186 int i;
187 char *p, *q;
188
189 opt.server_addr = DFL_SERVER_ADDR;
190 opt.server_port = DFL_SERVER_PORT;
191 opt.listen_addr = DFL_LISTEN_ADDR;
192 opt.listen_port = DFL_LISTEN_PORT;
Hanno Becker211f44c2017-10-31 14:08:10 +0000193 opt.pack = DFL_PACK;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200194 /* Other members default to 0 */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200195
Hanno Becker01ea7782018-08-17 13:33:41 +0100196 opt.delay_cli_cnt = 0;
197 opt.delay_srv_cnt = 0;
198 memset( opt.delay_cli, 0, sizeof( opt.delay_cli ) );
199 memset( opt.delay_srv, 0, sizeof( opt.delay_srv ) );
200
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200201 for( i = 1; i < argc; i++ )
202 {
203 p = argv[i];
204 if( ( q = strchr( p, '=' ) ) == NULL )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200205 exit_usage( p, NULL );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200206 *q++ = '\0';
207
208 if( strcmp( p, "server_addr" ) == 0 )
209 opt.server_addr = q;
210 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200211 opt.server_port = q;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200212 else if( strcmp( p, "listen_addr" ) == 0 )
213 opt.listen_addr = q;
214 else if( strcmp( p, "listen_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200215 opt.listen_port = q;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200216 else if( strcmp( p, "duplicate" ) == 0 )
217 {
218 opt.duplicate = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200219 if( opt.duplicate < 0 || opt.duplicate > 20 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200220 exit_usage( p, q );
221 }
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200222 else if( strcmp( p, "delay" ) == 0 )
223 {
224 opt.delay = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200225 if( opt.delay < 0 || opt.delay > 20 || opt.delay == 1 )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200226 exit_usage( p, q );
227 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200228 else if( strcmp( p, "delay_ccs" ) == 0 )
229 {
230 opt.delay_ccs = atoi( q );
231 if( opt.delay_ccs < 0 || opt.delay_ccs > 1 )
232 exit_usage( p, q );
233 }
Hanno Becker01ea7782018-08-17 13:33:41 +0100234 else if( strcmp( p, "delay_cli" ) == 0 ||
235 strcmp( p, "delay_srv" ) == 0 )
236 {
237 uint8_t *delay_cnt;
238 char **delay_list;
239 size_t len;
240 char *buf;
241
242 if( strcmp( p, "delay_cli" ) == 0 )
243 {
244 delay_cnt = &opt.delay_cli_cnt;
245 delay_list = opt.delay_cli;
246 }
247 else
248 {
249 delay_cnt = &opt.delay_srv_cnt;
250 delay_list = opt.delay_srv;
251 }
252
253 if( *delay_cnt == MAX_DELAYED_HS )
254 {
Hanno Beckerf34a4c12018-08-28 17:22:26 +0100255 mbedtls_printf( " too many uses of %s: only %d allowed\n",
256 p, MAX_DELAYED_HS );
Hanno Becker01ea7782018-08-17 13:33:41 +0100257 exit_usage( p, NULL );
258 }
259
260 len = strlen( q );
261 buf = mbedtls_calloc( 1, len + 1 );
262 if( buf == NULL )
263 {
264 mbedtls_printf( " Allocation failure\n" );
265 exit( 1 );
266 }
267 memcpy( buf, q, len + 1 );
268
269 delay_list[ (*delay_cnt)++ ] = buf;
270 }
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200271 else if( strcmp( p, "drop" ) == 0 )
272 {
273 opt.drop = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200274 if( opt.drop < 0 || opt.drop > 20 || opt.drop == 1 )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200275 exit_usage( p, q );
276 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100277 else if( strcmp( p, "pack" ) == 0 )
278 {
Hanno Becker0cc77742017-10-31 14:10:07 +0000279#if defined(MBEDTLS_TIMING_C)
Hanno Becker77abef52017-11-02 10:50:28 +0000280 opt.pack = (unsigned) atoi( q );
Hanno Becker0cc77742017-10-31 14:10:07 +0000281#else
282 mbedtls_printf( " option pack only defined if MBEDTLS_TIMING_C is enabled\n" );
283 exit( 1 );
284#endif
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100285 }
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200286 else if( strcmp( p, "mtu" ) == 0 )
287 {
288 opt.mtu = atoi( q );
289 if( opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE )
290 exit_usage( p, q );
291 }
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200292 else if( strcmp( p, "bad_ad" ) == 0 )
293 {
294 opt.bad_ad = atoi( q );
295 if( opt.bad_ad < 0 || opt.bad_ad > 1 )
296 exit_usage( p, q );
297 }
Hanno Becker98aaf252019-05-24 10:07:42 +0100298#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
299 else if( strcmp( p, "bad_cid" ) == 0 )
300 {
301 opt.bad_cid = (unsigned) atoi( q );
302 }
303#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200304 else if( strcmp( p, "protect_hvr" ) == 0 )
305 {
306 opt.protect_hvr = atoi( q );
307 if( opt.protect_hvr < 0 || opt.protect_hvr > 1 )
308 exit_usage( p, q );
309 }
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200310 else if( strcmp( p, "protect_len" ) == 0 )
311 {
312 opt.protect_len = atoi( q );
313 if( opt.protect_len < 0 )
314 exit_usage( p, q );
315 }
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100316 else if( strcmp( p, "inject_clihlo" ) == 0 )
317 {
318 opt.inject_clihlo = atoi( q );
319 if( opt.inject_clihlo < 0 || opt.inject_clihlo > 1 )
320 exit_usage( p, q );
321 }
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200322 else if( strcmp( p, "seed" ) == 0 )
323 {
324 opt.seed = atoi( q );
325 if( opt.seed == 0 )
326 exit_usage( p, q );
327 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200328 else
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200329 exit_usage( p, NULL );
330 }
331}
332
333static const char *msg_type( unsigned char *msg, size_t len )
334{
335 if( len < 1 ) return( "Invalid" );
336 switch( msg[0] )
337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" );
339 case MBEDTLS_SSL_MSG_ALERT: return( "Alert" );
340 case MBEDTLS_SSL_MSG_APPLICATION_DATA: return( "ApplicationData" );
Hanno Becker31f6e372019-05-08 15:36:31 +0100341 case MBEDTLS_SSL_MSG_CID: return( "CID" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 case MBEDTLS_SSL_MSG_HANDSHAKE: break; /* See below */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200343 default: return( "Unknown" );
344 }
345
Manuel Pégourié-Gonnard8cc7e032014-09-25 12:59:05 +0200346 if( len < 13 + 12 ) return( "Invalid handshake" );
347
348 /*
349 * Our handshake message are less than 2^16 bytes long, so they should
350 * have 0 as the first byte of length, frag_offset and frag_length.
351 * Otherwise, assume they are encrypted.
352 */
353 if( msg[14] || msg[19] || msg[22] ) return( "Encrypted handshake" );
354
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200355 switch( msg[13] )
356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 case MBEDTLS_SSL_HS_HELLO_REQUEST: return( "HelloRequest" );
358 case MBEDTLS_SSL_HS_CLIENT_HELLO: return( "ClientHello" );
359 case MBEDTLS_SSL_HS_SERVER_HELLO: return( "ServerHello" );
360 case MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" );
361 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" );
362 case MBEDTLS_SSL_HS_CERTIFICATE: return( "Certificate" );
363 case MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" );
364 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" );
365 case MBEDTLS_SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" );
366 case MBEDTLS_SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" );
367 case MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" );
368 case MBEDTLS_SSL_HS_FINISHED: return( "Finished" );
Manuel Pégourié-Gonnardbc010a02014-09-20 12:40:51 +0200369 default: return( "Unknown handshake" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200370 }
371}
372
Hanno Becker0cc77742017-10-31 14:10:07 +0000373#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200374/* Return elapsed time in milliseconds since the first call */
Hanno Becker77abef52017-11-02 10:50:28 +0000375static unsigned ellapsed_time( void )
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200376{
Hanno Becker92474da2017-10-31 14:09:30 +0000377 static int initialized = 0;
378 static struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200379
Hanno Becker92474da2017-10-31 14:09:30 +0000380 if( initialized == 0 )
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200381 {
Hanno Becker92474da2017-10-31 14:09:30 +0000382 (void) mbedtls_timing_get_timer( &hires, 1 );
383 initialized = 1;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200384 return( 0 );
385 }
386
Hanno Becker92474da2017-10-31 14:09:30 +0000387 return( mbedtls_timing_get_timer( &hires, 0 ) );
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200388}
389
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200390typedef struct
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200391{
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100392 mbedtls_net_context *ctx;
393
394 const char *description;
395
Hanno Becker77abef52017-11-02 10:50:28 +0000396 unsigned packet_lifetime;
397 unsigned num_datagrams;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100398
399 unsigned char data[MAX_MSG_SIZE];
Hanno Beckera5e68972017-12-06 08:35:02 +0000400 size_t len;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100401
402} ctx_buffer;
403
404static ctx_buffer outbuf[2];
405
406static int ctx_buffer_flush( ctx_buffer *buf )
407{
408 int ret;
409
Hanno Becker77abef52017-11-02 10:50:28 +0000410 mbedtls_printf( " %05u flush %s: %u bytes, %u datagrams, last %u ms\n",
411 ellapsed_time(), buf->description,
Hanno Beckera5e68972017-12-06 08:35:02 +0000412 (unsigned) buf->len, buf->num_datagrams,
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100413 ellapsed_time() - buf->packet_lifetime );
414
415 ret = mbedtls_net_send( buf->ctx, buf->data, buf->len );
416
417 buf->len = 0;
418 buf->num_datagrams = 0;
419
420 return( ret );
421}
422
Hanno Becker77abef52017-11-02 10:50:28 +0000423static unsigned ctx_buffer_time_remaining( ctx_buffer *buf )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100424{
Hanno Becker77abef52017-11-02 10:50:28 +0000425 unsigned const cur_time = ellapsed_time();
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100426
Hanno Becker77abef52017-11-02 10:50:28 +0000427 if( buf->num_datagrams == 0 )
428 return( (unsigned) -1 );
429
430 if( cur_time - buf->packet_lifetime >= opt.pack )
431 return( 0 );
432
433 return( opt.pack - ( cur_time - buf->packet_lifetime ) );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100434}
435
436static int ctx_buffer_append( ctx_buffer *buf,
437 const unsigned char * data,
438 size_t len )
439{
440 int ret;
441
Hanno Beckera5e68972017-12-06 08:35:02 +0000442 if( len > (size_t) INT_MAX )
443 return( -1 );
444
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100445 if( len > sizeof( buf->data ) )
446 {
Hanno Becker77abef52017-11-02 10:50:28 +0000447 mbedtls_printf( " ! buffer size %u too large (max %u)\n",
448 (unsigned) len, (unsigned) sizeof( buf->data ) );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100449 return( -1 );
450 }
451
452 if( sizeof( buf->data ) - buf->len < len )
453 {
454 if( ( ret = ctx_buffer_flush( buf ) ) <= 0 )
Hanno Becker31f6e372019-05-08 15:36:31 +0100455 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200456 mbedtls_printf( "ctx_buffer_flush failed with -%#04x", (unsigned int) -ret );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100457 return( ret );
Hanno Becker31f6e372019-05-08 15:36:31 +0100458 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100459 }
460
461 memcpy( buf->data + buf->len, data, len );
462
463 buf->len += len;
464 if( ++buf->num_datagrams == 1 )
465 buf->packet_lifetime = ellapsed_time();
466
Hanno Beckera5e68972017-12-06 08:35:02 +0000467 return( (int) len );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100468}
Hanno Becker77abef52017-11-02 10:50:28 +0000469#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100470
471static int dispatch_data( mbedtls_net_context *ctx,
472 const unsigned char * data,
473 size_t len )
474{
Hanno Becker31f6e372019-05-08 15:36:31 +0100475 int ret;
Hanno Becker77abef52017-11-02 10:50:28 +0000476#if defined(MBEDTLS_TIMING_C)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100477 ctx_buffer *buf = NULL;
Hanno Becker77abef52017-11-02 10:50:28 +0000478 if( opt.pack > 0 )
479 {
480 if( outbuf[0].ctx == ctx )
481 buf = &outbuf[0];
482 else if( outbuf[1].ctx == ctx )
483 buf = &outbuf[1];
Hanno Becker0cc77742017-10-31 14:10:07 +0000484
Hanno Becker77abef52017-11-02 10:50:28 +0000485 if( buf == NULL )
486 return( -1 );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100487
Hanno Becker77abef52017-11-02 10:50:28 +0000488 return( ctx_buffer_append( buf, data, len ) );
489 }
490#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100491
Hanno Becker31f6e372019-05-08 15:36:31 +0100492 ret = mbedtls_net_send( ctx, data, len );
493 if( ret < 0 )
494 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200495 mbedtls_printf( "net_send returned -%#04x\n", (unsigned int) -ret );
Hanno Becker31f6e372019-05-08 15:36:31 +0100496 }
497 return( ret );
Hanno Becker0cc77742017-10-31 14:10:07 +0000498}
499
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100500typedef struct
501{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200502 mbedtls_net_context *dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200503 const char *way;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200504 const char *type;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200505 unsigned len;
506 unsigned char buf[MAX_MSG_SIZE];
507} packet;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200508
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200509/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */
510void print_packet( const packet *p, const char *why )
511{
Hanno Becker0cc77742017-10-31 14:10:07 +0000512#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200513 if( why == NULL )
Hanno Becker77abef52017-11-02 10:50:28 +0000514 mbedtls_printf( " %05u dispatch %s %s (%u bytes)\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200515 ellapsed_time(), p->way, p->type, p->len );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200516 else
Hanno Becker77abef52017-11-02 10:50:28 +0000517 mbedtls_printf( " %05u dispatch %s %s (%u bytes): %s\n",
Hanno Becker0cc77742017-10-31 14:10:07 +0000518 ellapsed_time(), p->way, p->type, p->len, why );
519#else
520 if( why == NULL )
521 mbedtls_printf( " dispatch %s %s (%u bytes)\n",
522 p->way, p->type, p->len );
523 else
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100524 mbedtls_printf( " dispatch %s %s (%u bytes): %s\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200525 p->way, p->type, p->len, why );
Hanno Becker0cc77742017-10-31 14:10:07 +0000526#endif
527
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200528 fflush( stdout );
529}
530
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100531/*
532 * In order to test the server's behaviour when receiving a ClientHello after
533 * the connection is established (this could be a hard reset from the client,
534 * but the server must not drop the existing connection before establishing
535 * client reachability, see RFC 6347 Section 4.2.8), we memorize the first
536 * ClientHello we see (which can't have a cookie), then replay it after the
537 * first ApplicationData record - then we're done.
538 *
539 * This is controlled by the inject_clihlo option.
540 *
541 * We want an explicit state and a place to store the packet.
542 */
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200543typedef enum {
544 ICH_INIT, /* haven't seen the first ClientHello yet */
545 ICH_CACHED, /* cached the initial ClientHello */
546 ICH_INJECTED, /* ClientHello already injected, done */
547} inject_clihlo_state_t;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100548
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200549static inject_clihlo_state_t inject_clihlo_state;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100550static packet initial_clihlo;
551
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200552int send_packet( const packet *p, const char *why )
553{
554 int ret;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200555 mbedtls_net_context *dst = p->dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200556
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100557 /* save initial ClientHello? */
558 if( opt.inject_clihlo != 0 &&
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200559 inject_clihlo_state == ICH_INIT &&
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100560 strcmp( p->type, "ClientHello" ) == 0 )
561 {
562 memcpy( &initial_clihlo, p, sizeof( packet ) );
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200563 inject_clihlo_state = ICH_CACHED;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100564 }
565
Hanno Becker98aaf252019-05-24 10:07:42 +0100566 /* insert corrupted CID record? */
567 if( opt.bad_cid != 0 &&
568 strcmp( p->type, "CID" ) == 0 &&
569 ( rand() % opt.bad_cid ) == 0 )
570 {
571 unsigned char buf[MAX_MSG_SIZE];
572 memcpy( buf, p->buf, p->len );
573
574 /* The CID resides at offset 11 in the DTLS record header. */
575 buf[11] ^= 1;
576 print_packet( p, "modified CID" );
577
578 if( ( ret = dispatch_data( dst, buf, p->len ) ) <= 0 )
579 {
580 mbedtls_printf( " ! dispatch returned %d\n", ret );
581 return( ret );
582 }
583 }
584
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200585 /* insert corrupted ApplicationData record? */
586 if( opt.bad_ad &&
587 strcmp( p->type, "ApplicationData" ) == 0 )
588 {
589 unsigned char buf[MAX_MSG_SIZE];
590 memcpy( buf, p->buf, p->len );
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200591
Hanno Beckerfbb0b702017-05-26 16:55:07 +0100592 if( p->len <= 13 )
593 {
594 mbedtls_printf( " ! can't corrupt empty AD record" );
595 }
596 else
597 {
598 ++buf[13];
599 print_packet( p, "corrupted" );
600 }
601
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100602 if( ( ret = dispatch_data( dst, buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200603 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100604 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200605 return( ret );
606 }
607 }
608
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200609 print_packet( p, why );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100610 if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200611 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100612 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200613 return( ret );
614 }
615
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200616 /* Don't duplicate Application Data, only handshake covered */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200617 if( opt.duplicate != 0 &&
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200618 strcmp( p->type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200619 rand() % opt.duplicate == 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200620 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200621 print_packet( p, "duplicated" );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200622
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100623 if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200624 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100625 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200626 return( ret );
627 }
628 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200629
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100630 /* Inject ClientHello after first ApplicationData */
631 if( opt.inject_clihlo != 0 &&
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200632 inject_clihlo_state == ICH_CACHED &&
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100633 strcmp( p->type, "ApplicationData" ) == 0 )
634 {
635 print_packet( &initial_clihlo, "injected" );
636
637 if( ( ret = dispatch_data( dst, initial_clihlo.buf,
638 initial_clihlo.len ) ) <= 0 )
639 {
640 mbedtls_printf( " ! dispatch returned %d\n", ret );
641 return( ret );
642 }
643
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200644 inject_clihlo_state = ICH_INJECTED;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100645 }
646
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200647 return( 0 );
648}
649
Hanno Becker101bcba2018-08-21 16:39:51 +0100650#define MAX_DELAYED_MSG 5
651static size_t prev_len;
652static packet prev[MAX_DELAYED_MSG];
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200653
654void clear_pending( void )
655{
Hanno Becker12b72c12018-08-23 13:15:36 +0100656 memset( &prev, 0, sizeof( prev ) );
Hanno Becker101bcba2018-08-21 16:39:51 +0100657 prev_len = 0;
658}
659
660void delay_packet( packet *delay )
661{
662 if( prev_len == MAX_DELAYED_MSG )
663 return;
664
665 memcpy( &prev[prev_len++], delay, sizeof( packet ) );
666}
667
668int send_delayed()
669{
670 uint8_t offset;
671 int ret;
672 for( offset = 0; offset < prev_len; offset++ )
673 {
674 ret = send_packet( &prev[offset], "delayed" );
675 if( ret != 0 )
676 return( ret );
677 }
678
679 clear_pending();
680 return( 0 );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200681}
682
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200683/*
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200684 * Avoid dropping or delaying a packet that was already dropped or delayed
685 * ("held") twice: this only results in uninteresting timeouts. We can't rely
686 * on type to identify packets, since during renegotiation they're all
687 * encrypted. So, rely on size mod 2048 (which is usually just size).
688 *
689 * We only hold packets at the level of entire datagrams, not at the level
Hanno Becker961e6772019-06-04 13:04:28 +0100690 * of records. In particular, if the peer changes the way it packs multiple
691 * records into a single datagram, we don't necessarily count the number of
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200692 * times a record has been held correctly. However, the only known reason
Hanno Becker961e6772019-06-04 13:04:28 +0100693 * why a peer would change datagram packing is disabling the latter on
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200694 * retransmission, in which case we'd hold involved records at most
695 * HOLD_MAX + 1 times.
696 */
697static unsigned char held[2048] = { 0 };
698#define HOLD_MAX 2
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200699
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200700int handle_message( const char *way,
701 mbedtls_net_context *dst,
702 mbedtls_net_context *src )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200703{
704 int ret;
705 packet cur;
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200706 size_t id;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200707
Hanno Becker01ea7782018-08-17 13:33:41 +0100708 uint8_t delay_idx;
709 char ** delay_list;
710 uint8_t delay_list_len;
711
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +0200712 /* receive packet */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200713 if( ( ret = mbedtls_net_recv( src, cur.buf, sizeof( cur.buf ) ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 mbedtls_printf( " ! mbedtls_net_recv returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200716 return( ret );
717 }
718
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200719 cur.len = ret;
720 cur.type = msg_type( cur.buf, cur.len );
721 cur.way = way;
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200722 cur.dst = dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200723 print_packet( &cur, NULL );
724
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200725 id = cur.len % sizeof( held );
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200726
Hanno Becker01ea7782018-08-17 13:33:41 +0100727 if( strcmp( way, "S <- C" ) == 0 )
728 {
729 delay_list = opt.delay_cli;
730 delay_list_len = opt.delay_cli_cnt;
731 }
732 else
733 {
734 delay_list = opt.delay_srv;
735 delay_list_len = opt.delay_srv_cnt;
736 }
Hanno Beckercf469452018-08-28 10:09:47 +0100737
Hanno Becker01ea7782018-08-17 13:33:41 +0100738 /* Check if message type is in the list of messages
739 * that should be delayed */
740 for( delay_idx = 0; delay_idx < delay_list_len; delay_idx++ )
741 {
742 if( delay_list[ delay_idx ] == NULL )
743 continue;
744
745 if( strcmp( delay_list[ delay_idx ], cur.type ) == 0 )
746 {
747 /* Delay message */
Hanno Becker101bcba2018-08-21 16:39:51 +0100748 delay_packet( &cur );
Hanno Becker01ea7782018-08-17 13:33:41 +0100749
750 /* Remove entry from list */
751 mbedtls_free( delay_list[delay_idx] );
752 delay_list[delay_idx] = NULL;
753
754 return( 0 );
755 }
756 }
757
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200758 /* do we want to drop, delay, or forward it? */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200759 if( ( opt.mtu != 0 &&
760 cur.len > (unsigned) opt.mtu ) ||
761 ( opt.drop != 0 &&
Hanno Becker31f6e372019-05-08 15:36:31 +0100762 strcmp( cur.type, "CID" ) != 0 &&
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200763 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200764 ! ( opt.protect_hvr &&
765 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200766 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200767 held[id] < HOLD_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200768 rand() % opt.drop == 0 ) )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200769 {
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200770 ++held[id];
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200771 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200772 else if( ( opt.delay_ccs == 1 &&
773 strcmp( cur.type, "ChangeCipherSpec" ) == 0 ) ||
774 ( opt.delay != 0 &&
Hanno Becker31f6e372019-05-08 15:36:31 +0100775 strcmp( cur.type, "CID" ) != 0 &&
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200776 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200777 ! ( opt.protect_hvr &&
778 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200779 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200780 held[id] < HOLD_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200781 rand() % opt.delay == 0 ) )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200782 {
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200783 ++held[id];
Hanno Becker101bcba2018-08-21 16:39:51 +0100784 delay_packet( &cur );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200785 }
786 else
787 {
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200788 /* forward and possibly duplicate */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200789 if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 )
790 return( ret );
791
Hanno Becker101bcba2018-08-21 16:39:51 +0100792 /* send previously delayed messages if any */
793 ret = send_delayed();
794 if( ret != 0 )
795 return( ret );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200796 }
797
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200798 return( 0 );
799}
800
801int main( int argc, char *argv[] )
802{
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100803 int ret = 1;
804 int exit_code = MBEDTLS_EXIT_FAILURE;
Hanno Becker01ea7782018-08-17 13:33:41 +0100805 uint8_t delay_idx;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200806
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200807 mbedtls_net_context listen_fd, client_fd, server_fd;
Hanno Becker77abef52017-11-02 10:50:28 +0000808
809#if defined( MBEDTLS_TIMING_C )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100810 struct timeval tm;
Hanno Becker77abef52017-11-02 10:50:28 +0000811#endif
812
813 struct timeval *tm_ptr = NULL;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200814
815 int nb_fds;
816 fd_set read_fds;
817
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200818 mbedtls_net_init( &listen_fd );
819 mbedtls_net_init( &client_fd );
820 mbedtls_net_init( &server_fd );
821
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200822 get_options( argc, argv );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200823
824 /*
825 * Decisions to drop/delay/duplicate packets are pseudo-random: dropping
826 * exactly 1 in N packets would lead to problems when a flight has exactly
827 * N packets: the same packet would be dropped on every resend.
828 *
829 * In order to be able to reproduce problems reliably, the seed may be
830 * specified explicitly.
831 */
832 if( opt.seed == 0 )
833 {
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200834 opt.seed = (unsigned int) time( NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 mbedtls_printf( " . Pseudo-random seed: %u\n", opt.seed );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200836 }
837
838 srand( opt.seed );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200839
840 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200841 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200842 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200843 mbedtls_printf( " . Connect to server on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200844 opt.server_addr, opt.server_port );
845 fflush( stdout );
846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
848 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200851 goto exit;
852 }
853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200855
856 /*
857 * 1. Setup the "listening" UDP socket
858 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200859 mbedtls_printf( " . Bind on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200860 opt.listen_addr, opt.listen_port );
861 fflush( stdout );
862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 if( ( ret = mbedtls_net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
864 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200867 goto exit;
868 }
869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200871
872 /*
873 * 2. Wait until a client connects
874 */
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200875accept:
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200876 mbedtls_net_free( &client_fd );
877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 mbedtls_printf( " . Waiting for a remote connection ..." );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200879 fflush( stdout );
880
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200881 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200882 NULL, 0, NULL ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 mbedtls_printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200885 goto exit;
886 }
887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200889
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200890 /*
891 * 3. Forward packets forever (kill the process to terminate it)
892 */
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200893 clear_pending();
Manuel Pégourié-Gonnard71ce4ef2021-07-06 12:39:43 +0200894 memset( held, 0, sizeof( held ) );
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200895
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200896 nb_fds = client_fd.fd;
897 if( nb_fds < server_fd.fd )
898 nb_fds = server_fd.fd;
899 if( nb_fds < listen_fd.fd )
900 nb_fds = listen_fd.fd;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200901 ++nb_fds;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200902
Hanno Becker0cc77742017-10-31 14:10:07 +0000903#if defined(MBEDTLS_TIMING_C)
Hanno Becker211f44c2017-10-31 14:08:10 +0000904 if( opt.pack > 0 )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100905 {
906 outbuf[0].ctx = &server_fd;
907 outbuf[0].description = "S <- C";
908 outbuf[0].num_datagrams = 0;
909 outbuf[0].len = 0;
910
911 outbuf[1].ctx = &client_fd;
912 outbuf[1].description = "S -> C";
913 outbuf[1].num_datagrams = 0;
914 outbuf[1].len = 0;
915 }
Hanno Becker0cc77742017-10-31 14:10:07 +0000916#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100917
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200918 while( 1 )
919 {
Hanno Becker77abef52017-11-02 10:50:28 +0000920#if defined(MBEDTLS_TIMING_C)
921 if( opt.pack > 0 )
922 {
923 unsigned max_wait_server, max_wait_client, max_wait;
924 max_wait_server = ctx_buffer_time_remaining( &outbuf[0] );
925 max_wait_client = ctx_buffer_time_remaining( &outbuf[1] );
926
927 max_wait = (unsigned) -1;
928
929 if( max_wait_server == 0 )
930 ctx_buffer_flush( &outbuf[0] );
931 else
932 max_wait = max_wait_server;
933
934 if( max_wait_client == 0 )
935 ctx_buffer_flush( &outbuf[1] );
936 else
937 {
938 if( max_wait_client < max_wait )
939 max_wait = max_wait_client;
940 }
941
942 if( max_wait != (unsigned) -1 )
943 {
944 tm.tv_sec = max_wait / 1000;
945 tm.tv_usec = ( max_wait % 1000 ) * 1000;
946
947 tm_ptr = &tm;
948 }
949 else
950 {
951 tm_ptr = NULL;
952 }
953 }
954#endif /* MBEDTLS_TIMING_C */
955
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200956 FD_ZERO( &read_fds );
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200957 FD_SET( server_fd.fd, &read_fds );
958 FD_SET( client_fd.fd, &read_fds );
959 FD_SET( listen_fd.fd, &read_fds );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200960
Hanno Becker77abef52017-11-02 10:50:28 +0000961 if( ( ret = select( nb_fds, &read_fds, NULL, NULL, tm_ptr ) ) < 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200962 {
963 perror( "select" );
964 goto exit;
965 }
966
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200967 if( FD_ISSET( listen_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200968 goto accept;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200969
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200970 if( FD_ISSET( client_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200971 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200972 if( ( ret = handle_message( "S <- C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200973 &server_fd, &client_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200974 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200975 }
976
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200977 if( FD_ISSET( server_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200978 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200979 if( ( ret = handle_message( "S -> C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200980 &client_fd, &server_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200981 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200982 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100983
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200984 }
985
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100986 exit_code = MBEDTLS_EXIT_SUCCESS;
987
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200988exit:
989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990#ifdef MBEDTLS_ERROR_C
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100991 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200992 {
993 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 mbedtls_strerror( ret, error_buf, 100 );
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200995 mbedtls_printf( "Last error was: -0x%04X - %s\n\n", (unsigned int) -ret, error_buf );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200996 fflush( stdout );
997 }
998#endif
999
Hanno Becker01ea7782018-08-17 13:33:41 +01001000 for( delay_idx = 0; delay_idx < MAX_DELAYED_HS; delay_idx++ )
1001 {
Shawn Careyaa13e932021-05-06 15:11:30 -04001002 mbedtls_free( opt.delay_cli[delay_idx] );
1003 mbedtls_free( opt.delay_srv[delay_idx] );
Hanno Becker01ea7782018-08-17 13:33:41 +01001004 }
1005
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001006 mbedtls_net_free( &client_fd );
1007 mbedtls_net_free( &server_fd );
1008 mbedtls_net_free( &listen_fd );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001009
1010#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 mbedtls_printf( " Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001012 fflush( stdout ); getchar();
1013#endif
1014
k-stachowiak776521a2019-05-23 09:46:47 +02001015 mbedtls_exit( exit_code );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001016}
1017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018#endif /* MBEDTLS_NET_C */