blob: e0b43517d3747f6c4bfa815369b80c145728311a [file] [log] [blame]
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001/*
2 * UDP proxy: emulate an unreliable UDP connexion for DTLS testing
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020018 *
Manuel Pégourié-Gonnarde4d48902015-03-06 13:40:52 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020020 */
21
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020022/*
23 * Warning: this is an internal utility program we use for tests.
24 * It does break some abstractions from the NET layer, and is thus NOT an
25 * example of good general usage.
26 */
27
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020030#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020032#endif
33
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/platform.h"
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000036#else
Simon Butcherd3138c32016-04-27 01:26:50 +010037#include <stdio.h>
38#include <stdlib.h>
39#include <time.h>
Andres Amaya Garcia01e3d212018-04-29 21:58:53 +010040#define mbedtls_time time
41#define mbedtls_time_t time_t
42#define mbedtls_printf printf
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020043#define mbedtls_exit exit
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010044#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia01e3d212018-04-29 21:58:53 +010045#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
46#endif /* MBEDTLS_PLATFORM_C */
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if !defined(MBEDTLS_NET_C)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020049int main( void )
50{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051 mbedtls_printf( "MBEDTLS_NET_C not defined.\n" );
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020052 mbedtls_exit( 0 );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020053}
54#else
55
Andres AG788aa4a2016-09-14 14:32:09 +010056#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/error.h"
58#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020059
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +000060#include <string.h>
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020061
62/* For select() */
63#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
64 !defined(EFI32)
65#include <winsock2.h>
66#include <windows.h>
67#if defined(_MSC_VER)
68#if defined(_WIN32_WCE)
69#pragma comment( lib, "ws2.lib" )
70#else
71#pragma comment( lib, "ws2_32.lib" )
72#endif
73#endif /* _MSC_VER */
74#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
75#include <sys/time.h>
76#include <sys/types.h>
77#include <unistd.h>
78#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
79
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +020080/* For gettimeofday() */
81#if !defined(_WIN32)
82#include <sys/time.h>
83#endif
84
Manuel Pégourié-Gonnardb46780e2014-09-23 12:17:30 +020085#define MAX_MSG_SIZE 16384 + 2048 /* max record/datagram size */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020086
87#define DFL_SERVER_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020088#define DFL_SERVER_PORT "4433"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020089#define DFL_LISTEN_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020090#define DFL_LISTEN_PORT "5556"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020091
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020092#define USAGE \
93 "\n usage: udp_proxy param=<>...\n" \
94 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +020095 " server_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020096 " server_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +020097 " listen_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020098 " listen_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +020099 "\n" \
100 " duplicate=%%d default: 0 (no duplication)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200101 " duplicate about 1:N packets randomly\n" \
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200102 " delay=%%d default: 0 (no delayed packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200103 " delay about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200104 " delay_ccs=0/1 default: 0 (don't delay ChangeCipherSpec)\n" \
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200105 " drop=%%d default: 0 (no dropped packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200106 " drop about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200107 " mtu=%%d default: 0 (unlimited)\n" \
108 " drop packets larger than N bytes\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200109 " bad_ad=0/1 default: 0 (don't add bad ApplicationData)\n" \
110 " protect_hvr=0/1 default: 0 (don't protect HelloVerifyRequest)\n" \
Manuel Pégourié-Gonnarda58b0462020-03-13 11:11:02 +0100111 " protect_len=%%d default: (don't protect packets of this size)\n" \
112 " inject_clihlo=0/1 default: 0 (don't inject fake ClientHello)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200113 "\n" \
114 " seed=%%d default: (use current time)\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200115 "\n"
116
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200117/*
118 * global options
119 */
120static struct options
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200121{
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200122 const char *server_addr; /* address to forward packets to */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200123 const char *server_port; /* port to forward packets to */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200124 const char *listen_addr; /* address for accepting client connections */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200125 const char *listen_port; /* port for accepting client connections */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200126
127 int duplicate; /* duplicate 1 in N packets (none if 0) */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200128 int delay; /* delay 1 packet in N (none if 0) */
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200129 int delay_ccs; /* delay ChangeCipherSpec */
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200130 int drop; /* drop 1 packet in N (none if 0) */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200131 int mtu; /* drop packets larger than this */
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200132 int bad_ad; /* inject corrupted ApplicationData record */
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200133 int protect_hvr; /* never drop or delay HelloVerifyRequest */
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200134 int protect_len; /* never drop/delay packet of the given size*/
Manuel Pégourié-Gonnarda58b0462020-03-13 11:11:02 +0100135 int inject_clihlo; /* inject fake ClientHello after handshake */
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200136 unsigned int seed; /* seed for "random" events */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200137} opt;
138
139static void exit_usage( const char *name, const char *value )
140{
141 if( value == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_printf( " unknown option or missing value: %s\n", name );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200143 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_printf( " option %s: illegal value: %s\n", name, value );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 mbedtls_printf( USAGE );
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200147 mbedtls_exit( 1 );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200148}
149
150static void get_options( int argc, char *argv[] )
151{
152 int i;
153 char *p, *q;
154
155 opt.server_addr = DFL_SERVER_ADDR;
156 opt.server_port = DFL_SERVER_PORT;
157 opt.listen_addr = DFL_LISTEN_ADDR;
158 opt.listen_port = DFL_LISTEN_PORT;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200159 /* Other members default to 0 */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200160
161 for( i = 1; i < argc; i++ )
162 {
163 p = argv[i];
164 if( ( q = strchr( p, '=' ) ) == NULL )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200165 exit_usage( p, NULL );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200166 *q++ = '\0';
167
168 if( strcmp( p, "server_addr" ) == 0 )
169 opt.server_addr = q;
170 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200171 opt.server_port = q;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200172 else if( strcmp( p, "listen_addr" ) == 0 )
173 opt.listen_addr = q;
174 else if( strcmp( p, "listen_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200175 opt.listen_port = q;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200176 else if( strcmp( p, "duplicate" ) == 0 )
177 {
178 opt.duplicate = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200179 if( opt.duplicate < 0 || opt.duplicate > 20 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200180 exit_usage( p, q );
181 }
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200182 else if( strcmp( p, "delay" ) == 0 )
183 {
184 opt.delay = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200185 if( opt.delay < 0 || opt.delay > 20 || opt.delay == 1 )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200186 exit_usage( p, q );
187 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200188 else if( strcmp( p, "delay_ccs" ) == 0 )
189 {
190 opt.delay_ccs = atoi( q );
191 if( opt.delay_ccs < 0 || opt.delay_ccs > 1 )
192 exit_usage( p, q );
193 }
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200194 else if( strcmp( p, "drop" ) == 0 )
195 {
196 opt.drop = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200197 if( opt.drop < 0 || opt.drop > 20 || opt.drop == 1 )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200198 exit_usage( p, q );
199 }
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200200 else if( strcmp( p, "mtu" ) == 0 )
201 {
202 opt.mtu = atoi( q );
203 if( opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE )
204 exit_usage( p, q );
205 }
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200206 else if( strcmp( p, "bad_ad" ) == 0 )
207 {
208 opt.bad_ad = atoi( q );
209 if( opt.bad_ad < 0 || opt.bad_ad > 1 )
210 exit_usage( p, q );
211 }
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200212 else if( strcmp( p, "protect_hvr" ) == 0 )
213 {
214 opt.protect_hvr = atoi( q );
215 if( opt.protect_hvr < 0 || opt.protect_hvr > 1 )
216 exit_usage( p, q );
217 }
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200218 else if( strcmp( p, "protect_len" ) == 0 )
219 {
220 opt.protect_len = atoi( q );
221 if( opt.protect_len < 0 )
222 exit_usage( p, q );
223 }
Manuel Pégourié-Gonnarda58b0462020-03-13 11:11:02 +0100224 else if( strcmp( p, "inject_clihlo" ) == 0 )
225 {
226 opt.inject_clihlo = atoi( q );
227 if( opt.inject_clihlo < 0 || opt.inject_clihlo > 1 )
228 exit_usage( p, q );
229 }
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200230 else if( strcmp( p, "seed" ) == 0 )
231 {
232 opt.seed = atoi( q );
233 if( opt.seed == 0 )
234 exit_usage( p, q );
235 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200236 else
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200237 exit_usage( p, NULL );
238 }
239}
240
241static const char *msg_type( unsigned char *msg, size_t len )
242{
243 if( len < 1 ) return( "Invalid" );
244 switch( msg[0] )
245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" );
247 case MBEDTLS_SSL_MSG_ALERT: return( "Alert" );
248 case MBEDTLS_SSL_MSG_APPLICATION_DATA: return( "ApplicationData" );
249 case MBEDTLS_SSL_MSG_HANDSHAKE: break; /* See below */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200250 default: return( "Unknown" );
251 }
252
Manuel Pégourié-Gonnard8cc7e032014-09-25 12:59:05 +0200253 if( len < 13 + 12 ) return( "Invalid handshake" );
254
255 /*
256 * Our handshake message are less than 2^16 bytes long, so they should
257 * have 0 as the first byte of length, frag_offset and frag_length.
258 * Otherwise, assume they are encrypted.
259 */
260 if( msg[14] || msg[19] || msg[22] ) return( "Encrypted handshake" );
261
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200262 switch( msg[13] )
263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 case MBEDTLS_SSL_HS_HELLO_REQUEST: return( "HelloRequest" );
265 case MBEDTLS_SSL_HS_CLIENT_HELLO: return( "ClientHello" );
266 case MBEDTLS_SSL_HS_SERVER_HELLO: return( "ServerHello" );
267 case MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" );
268 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" );
269 case MBEDTLS_SSL_HS_CERTIFICATE: return( "Certificate" );
270 case MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" );
271 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" );
272 case MBEDTLS_SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" );
273 case MBEDTLS_SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" );
274 case MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" );
275 case MBEDTLS_SSL_HS_FINISHED: return( "Finished" );
Manuel Pégourié-Gonnardbc010a02014-09-20 12:40:51 +0200276 default: return( "Unknown handshake" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200277 }
278}
279
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200280/* Return elapsed time in milliseconds since the first call */
281static unsigned long ellapsed_time( void )
282{
283#if defined(_WIN32)
284 return( 0 );
285#else
286 static struct timeval ref = { 0, 0 };
287 struct timeval now;
288
289 if( ref.tv_sec == 0 && ref.tv_usec == 0 )
290 {
291 gettimeofday( &ref, NULL );
292 return( 0 );
293 }
294
295 gettimeofday( &now, NULL );
296 return( 1000 * ( now.tv_sec - ref.tv_sec )
297 + ( now.tv_usec - ref.tv_usec ) / 1000 );
298#endif
299}
300
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200301typedef struct
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200302{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200303 mbedtls_net_context *dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200304 const char *way;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200305 const char *type;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200306 unsigned len;
307 unsigned char buf[MAX_MSG_SIZE];
308} packet;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200309
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200310/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */
311void print_packet( const packet *p, const char *why )
312{
313 if( why == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 mbedtls_printf( " %05lu %s %s (%u bytes)\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200315 ellapsed_time(), p->way, p->type, p->len );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200316 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 mbedtls_printf( " %s %s (%u bytes): %s\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200318 p->way, p->type, p->len, why );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200319 fflush( stdout );
320}
321
Manuel Pégourié-Gonnarda58b0462020-03-13 11:11:02 +0100322/*
323 * In order to test the server's behaviour when receiving a ClientHello after
324 * the connection is established (this could be a hard reset from the client,
325 * but the server must not drop the existing connection before establishing
326 * client reachability, see RFC 6347 Section 4.2.8), we memorize the first
327 * ClientHello we see (which can't have a cookie), then replay it after the
328 * first ApplicationData record - then we're done.
329 *
330 * This is controlled by the inject_clihlo option.
331 *
332 * We want an explicit state and a place to store the packet.
333 */
Manuel Pégourié-Gonnard7ef7bf32020-03-30 12:46:21 +0200334typedef enum {
335 ICH_INIT, /* haven't seen the first ClientHello yet */
336 ICH_CACHED, /* cached the initial ClientHello */
337 ICH_INJECTED, /* ClientHello already injected, done */
338} inject_clihlo_state_t;
Manuel Pégourié-Gonnarda58b0462020-03-13 11:11:02 +0100339
Manuel Pégourié-Gonnard7ef7bf32020-03-30 12:46:21 +0200340static inject_clihlo_state_t inject_clihlo_state;
Manuel Pégourié-Gonnarda58b0462020-03-13 11:11:02 +0100341static packet initial_clihlo;
342
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200343int send_packet( const packet *p, const char *why )
344{
345 int ret;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200346 mbedtls_net_context *dst = p->dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200347
Manuel Pégourié-Gonnarda58b0462020-03-13 11:11:02 +0100348 /* save initial ClientHello? */
349 if( opt.inject_clihlo != 0 &&
Manuel Pégourié-Gonnard7ef7bf32020-03-30 12:46:21 +0200350 inject_clihlo_state == ICH_INIT &&
Manuel Pégourié-Gonnarda58b0462020-03-13 11:11:02 +0100351 strcmp( p->type, "ClientHello" ) == 0 )
352 {
353 memcpy( &initial_clihlo, p, sizeof( packet ) );
Manuel Pégourié-Gonnard7ef7bf32020-03-30 12:46:21 +0200354 inject_clihlo_state = ICH_CACHED;
Manuel Pégourié-Gonnarda58b0462020-03-13 11:11:02 +0100355 }
356
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200357 /* insert corrupted ApplicationData record? */
358 if( opt.bad_ad &&
359 strcmp( p->type, "ApplicationData" ) == 0 )
360 {
361 unsigned char buf[MAX_MSG_SIZE];
362 memcpy( buf, p->buf, p->len );
363 ++buf[p->len - 1];
364
365 print_packet( p, "corrupted" );
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200366 if( ( ret = mbedtls_net_send( dst, buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 mbedtls_printf( " ! mbedtls_net_send returned %d\n", ret );
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200369 return( ret );
370 }
371 }
372
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200373 print_packet( p, why );
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200374 if( ( ret = mbedtls_net_send( dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 mbedtls_printf( " ! mbedtls_net_send returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200377 return( ret );
378 }
379
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200380 /* Don't duplicate Application Data, only handshake covered */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200381 if( opt.duplicate != 0 &&
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200382 strcmp( p->type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200383 rand() % opt.duplicate == 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200384 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200385 print_packet( p, "duplicated" );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200386
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200387 if( ( ret = mbedtls_net_send( dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 mbedtls_printf( " ! mbedtls_net_send returned %d\n", ret );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200390 return( ret );
391 }
392 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200393
Manuel Pégourié-Gonnarda58b0462020-03-13 11:11:02 +0100394 /* Inject ClientHello after first ApplicationData */
395 if( opt.inject_clihlo != 0 &&
Manuel Pégourié-Gonnard7ef7bf32020-03-30 12:46:21 +0200396 inject_clihlo_state == ICH_CACHED &&
Manuel Pégourié-Gonnarda58b0462020-03-13 11:11:02 +0100397 strcmp( p->type, "ApplicationData" ) == 0 )
398 {
399 print_packet( &initial_clihlo, "injected" );
400
401 if( ( ret = mbedtls_net_send( dst, initial_clihlo.buf,
402 initial_clihlo.len ) ) <= 0 )
403 {
404 mbedtls_printf( " ! mbedtls_net_send returned %d\n", ret );
405 return( ret );
406 }
407
Manuel Pégourié-Gonnard7ef7bf32020-03-30 12:46:21 +0200408 inject_clihlo_state = ICH_INJECTED;
Manuel Pégourié-Gonnarda58b0462020-03-13 11:11:02 +0100409 }
410
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200411 return( 0 );
412}
413
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200414static packet prev;
415
416void clear_pending( void )
417{
418 memset( &prev, 0, sizeof( packet ) );
419}
420
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200421/*
422 * Avoid dropping or delaying a packet that was already dropped twice: this
423 * only results in uninteresting timeouts. We can't rely on type to identify
424 * packets, since during renegotiation they're all encrypted. So, rely on
425 * size mod 2048 (which is usually just size).
426 */
427static unsigned char dropped[2048] = { 0 };
428#define DROP_MAX 2
429
Hanno Becker7bac1ab2019-06-04 13:04:28 +0100430/* We only drop packets at the level of entire datagrams, not at the level
431 * of records. In particular, if the peer changes the way it packs multiple
432 * records into a single datagram, we don't necessarily count the number of
433 * times a record has been dropped correctly. However, the only known reason
434 * why a peer would change datagram packing is disabling the latter on
435 * retransmission, in which case we'd drop involved records at most
436 * DROP_MAX + 1 times. */
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200437void update_dropped( const packet *p )
438{
439 size_t id = p->len % sizeof( dropped );
Manuel Pégourié-Gonnardfa60f122014-09-26 16:07:29 +0200440 ++dropped[id];
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200441}
442
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200443int handle_message( const char *way,
444 mbedtls_net_context *dst,
445 mbedtls_net_context *src )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200446{
447 int ret;
448 packet cur;
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200449 size_t id;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200450
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +0200451 /* receive packet */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200452 if( ( ret = mbedtls_net_recv( src, cur.buf, sizeof( cur.buf ) ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_printf( " ! mbedtls_net_recv returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200455 return( ret );
456 }
457
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200458 cur.len = ret;
459 cur.type = msg_type( cur.buf, cur.len );
460 cur.way = way;
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200461 cur.dst = dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200462 print_packet( &cur, NULL );
463
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200464 id = cur.len % sizeof( dropped );
465
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200466 /* do we want to drop, delay, or forward it? */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200467 if( ( opt.mtu != 0 &&
468 cur.len > (unsigned) opt.mtu ) ||
469 ( opt.drop != 0 &&
470 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200471 ! ( opt.protect_hvr &&
472 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200473 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200474 dropped[id] < DROP_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200475 rand() % opt.drop == 0 ) )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200476 {
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200477 update_dropped( &cur );
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200478 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200479 else if( ( opt.delay_ccs == 1 &&
480 strcmp( cur.type, "ChangeCipherSpec" ) == 0 ) ||
481 ( opt.delay != 0 &&
482 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200483 ! ( opt.protect_hvr &&
484 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200485 prev.dst == NULL &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200486 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200487 dropped[id] < DROP_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200488 rand() % opt.delay == 0 ) )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200489 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200490 memcpy( &prev, &cur, sizeof( packet ) );
491 }
492 else
493 {
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200494 /* forward and possibly duplicate */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200495 if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 )
496 return( ret );
497
498 /* send previously delayed message if any */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200499 if( prev.dst != NULL )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200500 {
501 ret = send_packet( &prev, "delayed" );
502 memset( &prev, 0, sizeof( packet ) );
503 if( ret != 0 )
504 return( ret );
505 }
506 }
507
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200508 return( 0 );
509}
510
511int main( int argc, char *argv[] )
512{
Andres Amaya Garcia01e3d212018-04-29 21:58:53 +0100513 int ret = 1;
514 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200515
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200516 mbedtls_net_context listen_fd, client_fd, server_fd;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200517
518 int nb_fds;
519 fd_set read_fds;
520
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200521 mbedtls_net_init( &listen_fd );
522 mbedtls_net_init( &client_fd );
523 mbedtls_net_init( &server_fd );
524
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200525 get_options( argc, argv );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200526
527 /*
528 * Decisions to drop/delay/duplicate packets are pseudo-random: dropping
529 * exactly 1 in N packets would lead to problems when a flight has exactly
530 * N packets: the same packet would be dropped on every resend.
531 *
532 * In order to be able to reproduce problems reliably, the seed may be
533 * specified explicitly.
534 */
535 if( opt.seed == 0 )
536 {
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200537 opt.seed = (unsigned int) time( NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 mbedtls_printf( " . Pseudo-random seed: %u\n", opt.seed );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200539 }
540
541 srand( opt.seed );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200542
543 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200544 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200545 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200546 mbedtls_printf( " . Connect to server on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200547 opt.server_addr, opt.server_port );
548 fflush( stdout );
549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
551 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200554 goto exit;
555 }
556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200558
559 /*
560 * 1. Setup the "listening" UDP socket
561 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200562 mbedtls_printf( " . Bind on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200563 opt.listen_addr, opt.listen_port );
564 fflush( stdout );
565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 if( ( ret = mbedtls_net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
567 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200570 goto exit;
571 }
572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200574
575 /*
576 * 2. Wait until a client connects
577 */
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200578accept:
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200579 mbedtls_net_free( &client_fd );
580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 mbedtls_printf( " . Waiting for a remote connection ..." );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200582 fflush( stdout );
583
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200584 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200585 NULL, 0, NULL ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 mbedtls_printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200588 goto exit;
589 }
590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200592
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200593 /*
594 * 3. Forward packets forever (kill the process to terminate it)
595 */
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200596 clear_pending();
597 memset( dropped, 0, sizeof( dropped ) );
598
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200599 nb_fds = client_fd.fd;
600 if( nb_fds < server_fd.fd )
601 nb_fds = server_fd.fd;
602 if( nb_fds < listen_fd.fd )
603 nb_fds = listen_fd.fd;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200604 ++nb_fds;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200605
606 while( 1 )
607 {
608 FD_ZERO( &read_fds );
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200609 FD_SET( server_fd.fd, &read_fds );
610 FD_SET( client_fd.fd, &read_fds );
611 FD_SET( listen_fd.fd, &read_fds );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200612
613 if( ( ret = select( nb_fds, &read_fds, NULL, NULL, NULL ) ) <= 0 )
614 {
615 perror( "select" );
616 goto exit;
617 }
618
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200619 if( FD_ISSET( listen_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200620 goto accept;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200621
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200622 if( FD_ISSET( client_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200623 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200624 if( ( ret = handle_message( "S <- C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200625 &server_fd, &client_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200626 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200627 }
628
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200629 if( FD_ISSET( server_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200630 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200631 if( ( ret = handle_message( "S -> C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200632 &client_fd, &server_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200633 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200634 }
635 }
636
Andres Amaya Garcia01e3d212018-04-29 21:58:53 +0100637 exit_code = MBEDTLS_EXIT_SUCCESS;
638
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200639exit:
640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641#ifdef MBEDTLS_ERROR_C
Andres Amaya Garcia01e3d212018-04-29 21:58:53 +0100642 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200643 {
644 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 mbedtls_strerror( ret, error_buf, 100 );
646 mbedtls_printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200647 fflush( stdout );
648 }
649#endif
650
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200651 mbedtls_net_free( &client_fd );
652 mbedtls_net_free( &server_fd );
653 mbedtls_net_free( &listen_fd );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200654
655#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 mbedtls_printf( " Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200657 fflush( stdout ); getchar();
658#endif
659
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200660 mbedtls_exit( exit_code );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200661}
662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663#endif /* MBEDTLS_NET_C */