blob: f6e0cd0749cc8a73cca8ca5476e8543a33f148e1 [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é-Gonnard53ebe132015-05-15 11:56:13 +02004 * Copyright (C) 2014-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02005 *
Manuel Pégourié-Gonnarde4d48902015-03-06 13:40:52 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020023/*
24 * Warning: this is an internal utility program we use for tests.
25 * It does break some abstractions from the NET layer, and is thus NOT an
26 * example of good general usage.
27 */
28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020031#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020033#endif
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/platform.h"
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000037#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#define mbedtls_printf printf
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000039#endif
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if !defined(MBEDTLS_NET_C)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020042#include <stdio.h>
43int main( void )
44{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045 mbedtls_printf( "MBEDTLS_NET_C not defined.\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020046 return( 0 );
47}
48#else
49
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/net.h"
51#include "mbedtls/error.h"
52#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020053
54#include <stdio.h>
55#include <stdlib.h>
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +000056#include <string.h>
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +020057#include <time.h>
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020058
59/* For select() */
60#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
61 !defined(EFI32)
62#include <winsock2.h>
63#include <windows.h>
64#if defined(_MSC_VER)
65#if defined(_WIN32_WCE)
66#pragma comment( lib, "ws2.lib" )
67#else
68#pragma comment( lib, "ws2_32.lib" )
69#endif
70#endif /* _MSC_VER */
71#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
72#include <sys/time.h>
73#include <sys/types.h>
74#include <unistd.h>
75#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
76
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +020077/* For gettimeofday() */
78#if !defined(_WIN32)
79#include <sys/time.h>
80#endif
81
Manuel Pégourié-Gonnardb46780e2014-09-23 12:17:30 +020082#define MAX_MSG_SIZE 16384 + 2048 /* max record/datagram size */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020083
84#define DFL_SERVER_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020085#define DFL_SERVER_PORT "4433"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020086#define DFL_LISTEN_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020087#define DFL_LISTEN_PORT "5556"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020088
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020089#define USAGE \
90 "\n usage: udp_proxy param=<>...\n" \
91 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +020092 " server_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020093 " server_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +020094 " listen_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020095 " listen_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +020096 "\n" \
97 " duplicate=%%d default: 0 (no duplication)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +020098 " duplicate about 1:N packets randomly\n" \
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +020099 " delay=%%d default: 0 (no delayed packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200100 " delay about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200101 " delay_ccs=0/1 default: 0 (don't delay ChangeCipherSpec)\n" \
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200102 " drop=%%d default: 0 (no dropped packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200103 " drop about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200104 " mtu=%%d default: 0 (unlimited)\n" \
105 " drop packets larger than N bytes\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200106 " bad_ad=0/1 default: 0 (don't add bad ApplicationData)\n" \
107 " protect_hvr=0/1 default: 0 (don't protect HelloVerifyRequest)\n" \
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200108 " protect_len=%%d default: (don't protect packets of this size)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200109 "\n" \
110 " seed=%%d default: (use current time)\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200111 "\n"
112
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200113/*
114 * global options
115 */
116static struct options
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200117{
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200118 const char *server_addr; /* address to forward packets to */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200119 const char *server_port; /* port to forward packets to */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200120 const char *listen_addr; /* address for accepting client connections */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200121 const char *listen_port; /* port for accepting client connections */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200122
123 int duplicate; /* duplicate 1 in N packets (none if 0) */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200124 int delay; /* delay 1 packet in N (none if 0) */
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200125 int delay_ccs; /* delay ChangeCipherSpec */
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200126 int drop; /* drop 1 packet in N (none if 0) */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200127 int mtu; /* drop packets larger than this */
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200128 int bad_ad; /* inject corrupted ApplicationData record */
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200129 int protect_hvr; /* never drop or delay HelloVerifyRequest */
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200130 int protect_len; /* never drop/delay packet of the given size*/
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200131
132 unsigned int seed; /* seed for "random" events */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200133} opt;
134
135static void exit_usage( const char *name, const char *value )
136{
137 if( value == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_printf( " unknown option or missing value: %s\n", name );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200139 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_printf( " option %s: illegal value: %s\n", name, value );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_printf( USAGE );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200143 exit( 1 );
144}
145
146static void get_options( int argc, char *argv[] )
147{
148 int i;
149 char *p, *q;
150
151 opt.server_addr = DFL_SERVER_ADDR;
152 opt.server_port = DFL_SERVER_PORT;
153 opt.listen_addr = DFL_LISTEN_ADDR;
154 opt.listen_port = DFL_LISTEN_PORT;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200155 /* Other members default to 0 */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200156
157 for( i = 1; i < argc; i++ )
158 {
159 p = argv[i];
160 if( ( q = strchr( p, '=' ) ) == NULL )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200161 exit_usage( p, NULL );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200162 *q++ = '\0';
163
164 if( strcmp( p, "server_addr" ) == 0 )
165 opt.server_addr = q;
166 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200167 opt.server_port = q;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200168 else if( strcmp( p, "listen_addr" ) == 0 )
169 opt.listen_addr = q;
170 else if( strcmp( p, "listen_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200171 opt.listen_port = q;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200172 else if( strcmp( p, "duplicate" ) == 0 )
173 {
174 opt.duplicate = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200175 if( opt.duplicate < 0 || opt.duplicate > 20 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200176 exit_usage( p, q );
177 }
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200178 else if( strcmp( p, "delay" ) == 0 )
179 {
180 opt.delay = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200181 if( opt.delay < 0 || opt.delay > 20 || opt.delay == 1 )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200182 exit_usage( p, q );
183 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200184 else if( strcmp( p, "delay_ccs" ) == 0 )
185 {
186 opt.delay_ccs = atoi( q );
187 if( opt.delay_ccs < 0 || opt.delay_ccs > 1 )
188 exit_usage( p, q );
189 }
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200190 else if( strcmp( p, "drop" ) == 0 )
191 {
192 opt.drop = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200193 if( opt.drop < 0 || opt.drop > 20 || opt.drop == 1 )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200194 exit_usage( p, q );
195 }
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200196 else if( strcmp( p, "mtu" ) == 0 )
197 {
198 opt.mtu = atoi( q );
199 if( opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE )
200 exit_usage( p, q );
201 }
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200202 else if( strcmp( p, "bad_ad" ) == 0 )
203 {
204 opt.bad_ad = atoi( q );
205 if( opt.bad_ad < 0 || opt.bad_ad > 1 )
206 exit_usage( p, q );
207 }
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200208 else if( strcmp( p, "protect_hvr" ) == 0 )
209 {
210 opt.protect_hvr = atoi( q );
211 if( opt.protect_hvr < 0 || opt.protect_hvr > 1 )
212 exit_usage( p, q );
213 }
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200214 else if( strcmp( p, "protect_len" ) == 0 )
215 {
216 opt.protect_len = atoi( q );
217 if( opt.protect_len < 0 )
218 exit_usage( p, q );
219 }
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200220 else if( strcmp( p, "seed" ) == 0 )
221 {
222 opt.seed = atoi( q );
223 if( opt.seed == 0 )
224 exit_usage( p, q );
225 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200226 else
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200227 exit_usage( p, NULL );
228 }
229}
230
231static const char *msg_type( unsigned char *msg, size_t len )
232{
233 if( len < 1 ) return( "Invalid" );
234 switch( msg[0] )
235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" );
237 case MBEDTLS_SSL_MSG_ALERT: return( "Alert" );
238 case MBEDTLS_SSL_MSG_APPLICATION_DATA: return( "ApplicationData" );
239 case MBEDTLS_SSL_MSG_HANDSHAKE: break; /* See below */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200240 default: return( "Unknown" );
241 }
242
Manuel Pégourié-Gonnard8cc7e032014-09-25 12:59:05 +0200243 if( len < 13 + 12 ) return( "Invalid handshake" );
244
245 /*
246 * Our handshake message are less than 2^16 bytes long, so they should
247 * have 0 as the first byte of length, frag_offset and frag_length.
248 * Otherwise, assume they are encrypted.
249 */
250 if( msg[14] || msg[19] || msg[22] ) return( "Encrypted handshake" );
251
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200252 switch( msg[13] )
253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 case MBEDTLS_SSL_HS_HELLO_REQUEST: return( "HelloRequest" );
255 case MBEDTLS_SSL_HS_CLIENT_HELLO: return( "ClientHello" );
256 case MBEDTLS_SSL_HS_SERVER_HELLO: return( "ServerHello" );
257 case MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" );
258 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" );
259 case MBEDTLS_SSL_HS_CERTIFICATE: return( "Certificate" );
260 case MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" );
261 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" );
262 case MBEDTLS_SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" );
263 case MBEDTLS_SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" );
264 case MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" );
265 case MBEDTLS_SSL_HS_FINISHED: return( "Finished" );
Manuel Pégourié-Gonnardbc010a02014-09-20 12:40:51 +0200266 default: return( "Unknown handshake" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200267 }
268}
269
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200270/* Return elapsed time in milliseconds since the first call */
271static unsigned long ellapsed_time( void )
272{
273#if defined(_WIN32)
274 return( 0 );
275#else
276 static struct timeval ref = { 0, 0 };
277 struct timeval now;
278
279 if( ref.tv_sec == 0 && ref.tv_usec == 0 )
280 {
281 gettimeofday( &ref, NULL );
282 return( 0 );
283 }
284
285 gettimeofday( &now, NULL );
286 return( 1000 * ( now.tv_sec - ref.tv_sec )
287 + ( now.tv_usec - ref.tv_usec ) / 1000 );
288#endif
289}
290
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200291typedef struct
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200292{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200293 mbedtls_net_context *dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200294 const char *way;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200295 const char *type;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200296 unsigned len;
297 unsigned char buf[MAX_MSG_SIZE];
298} packet;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200299
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200300/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */
301void print_packet( const packet *p, const char *why )
302{
303 if( why == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 mbedtls_printf( " %05lu %s %s (%u bytes)\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200305 ellapsed_time(), p->way, p->type, p->len );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200306 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 mbedtls_printf( " %s %s (%u bytes): %s\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200308 p->way, p->type, p->len, why );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200309 fflush( stdout );
310}
311
312int send_packet( const packet *p, const char *why )
313{
314 int ret;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200315 mbedtls_net_context *dst = p->dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200316
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200317 /* insert corrupted ApplicationData record? */
318 if( opt.bad_ad &&
319 strcmp( p->type, "ApplicationData" ) == 0 )
320 {
321 unsigned char buf[MAX_MSG_SIZE];
322 memcpy( buf, p->buf, p->len );
323 ++buf[p->len - 1];
324
325 print_packet( p, "corrupted" );
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200326 if( ( ret = mbedtls_net_send( dst, buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 mbedtls_printf( " ! mbedtls_net_send returned %d\n", ret );
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200329 return( ret );
330 }
331 }
332
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200333 print_packet( p, why );
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200334 if( ( ret = mbedtls_net_send( dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 mbedtls_printf( " ! mbedtls_net_send returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200337 return( ret );
338 }
339
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200340 /* Don't duplicate Application Data, only handshake covered */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200341 if( opt.duplicate != 0 &&
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200342 strcmp( p->type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200343 rand() % opt.duplicate == 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200344 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200345 print_packet( p, "duplicated" );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200346
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200347 if( ( ret = mbedtls_net_send( dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 mbedtls_printf( " ! mbedtls_net_send returned %d\n", ret );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200350 return( ret );
351 }
352 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200353
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200354 return( 0 );
355}
356
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200357static packet prev;
358
359void clear_pending( void )
360{
361 memset( &prev, 0, sizeof( packet ) );
362}
363
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200364/*
365 * Avoid dropping or delaying a packet that was already dropped twice: this
366 * only results in uninteresting timeouts. We can't rely on type to identify
367 * packets, since during renegotiation they're all encrypted. So, rely on
368 * size mod 2048 (which is usually just size).
369 */
370static unsigned char dropped[2048] = { 0 };
371#define DROP_MAX 2
372
373/*
374 * OpenSSL groups packets in a datagram the first time it sends them, but not
375 * when it resends them. Count every record as seen the first time.
376 */
377void update_dropped( const packet *p )
378{
379 size_t id = p->len % sizeof( dropped );
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200380 const unsigned char *end = p->buf + p->len;
381 const unsigned char *cur = p->buf;
382 size_t len = ( ( cur[11] << 8 ) | cur[12] ) + 13;
383
Manuel Pégourié-Gonnardfa60f122014-09-26 16:07:29 +0200384 ++dropped[id];
385
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200386 /* Avoid counting single record twice */
387 if( len == p->len )
388 return;
389
390 while( cur < end )
391 {
392 size_t len = ( ( cur[11] << 8 ) | cur[12] ) + 13;
393
394 id = len % sizeof( dropped );
395 ++dropped[id];
396
397 cur += len;
398 }
399}
400
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200401int handle_message( const char *way,
402 mbedtls_net_context *dst,
403 mbedtls_net_context *src )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200404{
405 int ret;
406 packet cur;
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200407 size_t id;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200408
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +0200409 /* receive packet */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200410 if( ( ret = mbedtls_net_recv( src, cur.buf, sizeof( cur.buf ) ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 mbedtls_printf( " ! mbedtls_net_recv returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200413 return( ret );
414 }
415
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200416 cur.len = ret;
417 cur.type = msg_type( cur.buf, cur.len );
418 cur.way = way;
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200419 cur.dst = dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200420 print_packet( &cur, NULL );
421
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200422 id = cur.len % sizeof( dropped );
423
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200424 /* do we want to drop, delay, or forward it? */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200425 if( ( opt.mtu != 0 &&
426 cur.len > (unsigned) opt.mtu ) ||
427 ( opt.drop != 0 &&
428 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200429 ! ( opt.protect_hvr &&
430 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200431 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200432 dropped[id] < DROP_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200433 rand() % opt.drop == 0 ) )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200434 {
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200435 update_dropped( &cur );
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200436 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200437 else if( ( opt.delay_ccs == 1 &&
438 strcmp( cur.type, "ChangeCipherSpec" ) == 0 ) ||
439 ( opt.delay != 0 &&
440 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200441 ! ( opt.protect_hvr &&
442 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200443 prev.dst == NULL &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200444 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200445 dropped[id] < DROP_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200446 rand() % opt.delay == 0 ) )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200447 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200448 memcpy( &prev, &cur, sizeof( packet ) );
449 }
450 else
451 {
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200452 /* forward and possibly duplicate */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200453 if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 )
454 return( ret );
455
456 /* send previously delayed message if any */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200457 if( prev.dst != NULL )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200458 {
459 ret = send_packet( &prev, "delayed" );
460 memset( &prev, 0, sizeof( packet ) );
461 if( ret != 0 )
462 return( ret );
463 }
464 }
465
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200466 return( 0 );
467}
468
469int main( int argc, char *argv[] )
470{
471 int ret;
472
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200473 mbedtls_net_context listen_fd, client_fd, server_fd;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200474
475 int nb_fds;
476 fd_set read_fds;
477
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200478 mbedtls_net_init( &listen_fd );
479 mbedtls_net_init( &client_fd );
480 mbedtls_net_init( &server_fd );
481
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200482 get_options( argc, argv );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200483
484 /*
485 * Decisions to drop/delay/duplicate packets are pseudo-random: dropping
486 * exactly 1 in N packets would lead to problems when a flight has exactly
487 * N packets: the same packet would be dropped on every resend.
488 *
489 * In order to be able to reproduce problems reliably, the seed may be
490 * specified explicitly.
491 */
492 if( opt.seed == 0 )
493 {
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200494 opt.seed = (unsigned int) time( NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 mbedtls_printf( " . Pseudo-random seed: %u\n", opt.seed );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200496 }
497
498 srand( opt.seed );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200499
500 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200501 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200502 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200503 mbedtls_printf( " . Connect to server on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200504 opt.server_addr, opt.server_port );
505 fflush( stdout );
506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
508 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200511 goto exit;
512 }
513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200515
516 /*
517 * 1. Setup the "listening" UDP socket
518 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200519 mbedtls_printf( " . Bind on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200520 opt.listen_addr, opt.listen_port );
521 fflush( stdout );
522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 if( ( ret = mbedtls_net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
524 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200527 goto exit;
528 }
529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200531
532 /*
533 * 2. Wait until a client connects
534 */
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200535accept:
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200536 mbedtls_net_free( &client_fd );
537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 mbedtls_printf( " . Waiting for a remote connection ..." );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200539 fflush( stdout );
540
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200541 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200542 NULL, 0, NULL ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 mbedtls_printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200545 goto exit;
546 }
547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200549
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200550 /*
551 * 3. Forward packets forever (kill the process to terminate it)
552 */
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200553 clear_pending();
554 memset( dropped, 0, sizeof( dropped ) );
555
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200556 nb_fds = client_fd.fd;
557 if( nb_fds < server_fd.fd )
558 nb_fds = server_fd.fd;
559 if( nb_fds < listen_fd.fd )
560 nb_fds = listen_fd.fd;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200561 ++nb_fds;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200562
563 while( 1 )
564 {
565 FD_ZERO( &read_fds );
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200566 FD_SET( server_fd.fd, &read_fds );
567 FD_SET( client_fd.fd, &read_fds );
568 FD_SET( listen_fd.fd, &read_fds );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200569
570 if( ( ret = select( nb_fds, &read_fds, NULL, NULL, NULL ) ) <= 0 )
571 {
572 perror( "select" );
573 goto exit;
574 }
575
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200576 if( FD_ISSET( listen_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200577 goto accept;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200578
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200579 if( FD_ISSET( client_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200580 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200581 if( ( ret = handle_message( "S <- C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200582 &server_fd, &client_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200583 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200584 }
585
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200586 if( FD_ISSET( server_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200587 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200588 if( ( ret = handle_message( "S -> C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200589 &client_fd, &server_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200590 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200591 }
592 }
593
594exit:
595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596#ifdef MBEDTLS_ERROR_C
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200597 if( ret != 0 )
598 {
599 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 mbedtls_strerror( ret, error_buf, 100 );
601 mbedtls_printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200602 fflush( stdout );
603 }
604#endif
605
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200606 mbedtls_net_free( &client_fd );
607 mbedtls_net_free( &server_fd );
608 mbedtls_net_free( &listen_fd );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200609
610#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 mbedtls_printf( " Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200612 fflush( stdout ); getchar();
613#endif
614
615 return( ret != 0 );
616}
617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618#endif /* MBEDTLS_NET_C */