blob: 39b3bed4a90d8fdf556dbf027bda98a9f1aa8db8 [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>
40#define mbedtls_time time
41#define mbedtls_time_t time_t
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#define mbedtls_printf printf
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000043#endif
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if !defined(MBEDTLS_NET_C)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020046int main( void )
47{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 mbedtls_printf( "MBEDTLS_NET_C not defined.\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020049 return( 0 );
50}
51#else
52
Andres AG788aa4a2016-09-14 14:32:09 +010053#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/error.h"
55#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020056
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +000057#include <string.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"
Hanno Becker1dd62ea2017-05-22 14:30:59 +010088#define DFL_PACK 0
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020089
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020090#define USAGE \
91 "\n usage: udp_proxy param=<>...\n" \
92 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +020093 " server_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020094 " server_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +020095 " listen_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020096 " listen_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +020097 "\n" \
98 " duplicate=%%d default: 0 (no duplication)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +020099 " duplicate about 1:N packets randomly\n" \
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200100 " delay=%%d default: 0 (no delayed packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200101 " delay about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200102 " delay_ccs=0/1 default: 0 (don't delay ChangeCipherSpec)\n" \
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200103 " drop=%%d default: 0 (no dropped packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200104 " drop about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200105 " mtu=%%d default: 0 (unlimited)\n" \
106 " drop packets larger than N bytes\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200107 " bad_ad=0/1 default: 0 (don't add bad ApplicationData)\n" \
108 " protect_hvr=0/1 default: 0 (don't protect HelloVerifyRequest)\n" \
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200109 " protect_len=%%d default: (don't protect packets of this size)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200110 "\n" \
111 " seed=%%d default: (use current time)\n" \
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100112 " pack=%%d default: 0 (don't merge)\n" \
113 " options: t > 0 (merge for t milliseconds)\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200114 "\n"
115
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200116/*
117 * global options
118 */
119static struct options
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200120{
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200121 const char *server_addr; /* address to forward packets to */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200122 const char *server_port; /* port to forward packets to */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200123 const char *listen_addr; /* address for accepting client connections */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200124 const char *listen_port; /* port for accepting client connections */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200125
126 int duplicate; /* duplicate 1 in N packets (none if 0) */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200127 int delay; /* delay 1 packet in N (none if 0) */
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200128 int delay_ccs; /* delay ChangeCipherSpec */
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200129 int drop; /* drop 1 packet in N (none if 0) */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200130 int mtu; /* drop packets larger than this */
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200131 int bad_ad; /* inject corrupted ApplicationData record */
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200132 int protect_hvr; /* never drop or delay HelloVerifyRequest */
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200133 int protect_len; /* never drop/delay packet of the given size*/
Hanno Becker211f44c2017-10-31 14:08:10 +0000134 int pack; /* merge packets into single datagram for
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100135 * at most \c merge milliseconds if > 0 */
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200136
137 unsigned int seed; /* seed for "random" events */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200138} opt;
139
140static void exit_usage( const char *name, const char *value )
141{
142 if( value == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_printf( " unknown option or missing value: %s\n", name );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200144 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_printf( " option %s: illegal value: %s\n", name, value );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_printf( USAGE );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200148 exit( 1 );
149}
150
151static void get_options( int argc, char *argv[] )
152{
153 int i;
154 char *p, *q;
155
156 opt.server_addr = DFL_SERVER_ADDR;
157 opt.server_port = DFL_SERVER_PORT;
158 opt.listen_addr = DFL_LISTEN_ADDR;
159 opt.listen_port = DFL_LISTEN_PORT;
Hanno Becker211f44c2017-10-31 14:08:10 +0000160 opt.pack = DFL_PACK;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200161 /* Other members default to 0 */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200162
163 for( i = 1; i < argc; i++ )
164 {
165 p = argv[i];
166 if( ( q = strchr( p, '=' ) ) == NULL )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200167 exit_usage( p, NULL );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200168 *q++ = '\0';
169
170 if( strcmp( p, "server_addr" ) == 0 )
171 opt.server_addr = q;
172 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200173 opt.server_port = q;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200174 else if( strcmp( p, "listen_addr" ) == 0 )
175 opt.listen_addr = q;
176 else if( strcmp( p, "listen_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200177 opt.listen_port = q;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200178 else if( strcmp( p, "duplicate" ) == 0 )
179 {
180 opt.duplicate = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200181 if( opt.duplicate < 0 || opt.duplicate > 20 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200182 exit_usage( p, q );
183 }
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200184 else if( strcmp( p, "delay" ) == 0 )
185 {
186 opt.delay = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200187 if( opt.delay < 0 || opt.delay > 20 || opt.delay == 1 )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200188 exit_usage( p, q );
189 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200190 else if( strcmp( p, "delay_ccs" ) == 0 )
191 {
192 opt.delay_ccs = atoi( q );
193 if( opt.delay_ccs < 0 || opt.delay_ccs > 1 )
194 exit_usage( p, q );
195 }
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200196 else if( strcmp( p, "drop" ) == 0 )
197 {
198 opt.drop = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200199 if( opt.drop < 0 || opt.drop > 20 || opt.drop == 1 )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200200 exit_usage( p, q );
201 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100202 else if( strcmp( p, "pack" ) == 0 )
203 {
Hanno Becker211f44c2017-10-31 14:08:10 +0000204 opt.pack = atoi( q );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100205 }
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200206 else if( strcmp( p, "mtu" ) == 0 )
207 {
208 opt.mtu = atoi( q );
209 if( opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE )
210 exit_usage( p, q );
211 }
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200212 else if( strcmp( p, "bad_ad" ) == 0 )
213 {
214 opt.bad_ad = atoi( q );
215 if( opt.bad_ad < 0 || opt.bad_ad > 1 )
216 exit_usage( p, q );
217 }
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200218 else if( strcmp( p, "protect_hvr" ) == 0 )
219 {
220 opt.protect_hvr = atoi( q );
221 if( opt.protect_hvr < 0 || opt.protect_hvr > 1 )
222 exit_usage( p, q );
223 }
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200224 else if( strcmp( p, "protect_len" ) == 0 )
225 {
226 opt.protect_len = atoi( q );
227 if( opt.protect_len < 0 )
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{
Hanno Becker92474da2017-10-31 14:09:30 +0000283 static int initialized = 0;
284 static struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200285
Hanno Becker92474da2017-10-31 14:09:30 +0000286 if( initialized == 0 )
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200287 {
Hanno Becker92474da2017-10-31 14:09:30 +0000288 (void) mbedtls_timing_get_timer( &hires, 1 );
289 initialized = 1;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200290 return( 0 );
291 }
292
Hanno Becker92474da2017-10-31 14:09:30 +0000293 return( mbedtls_timing_get_timer( &hires, 0 ) );
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200294}
295
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200296typedef struct
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200297{
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100298 mbedtls_net_context *ctx;
299
300 const char *description;
301
302 unsigned long packet_lifetime;
303 size_t num_datagrams;
304
305 unsigned char data[MAX_MSG_SIZE];
306 unsigned len;
307
308} ctx_buffer;
309
310static ctx_buffer outbuf[2];
311
312static int ctx_buffer_flush( ctx_buffer *buf )
313{
314 int ret;
315
Hanno Beckerdf4180a2017-10-27 13:43:58 +0100316 mbedtls_printf( " %05lu flush %s: %u bytes, %lu datagrams, last %ld ms\n",
317 ellapsed_time(), buf->description, buf->len, buf->num_datagrams,
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100318 ellapsed_time() - buf->packet_lifetime );
319
320 ret = mbedtls_net_send( buf->ctx, buf->data, buf->len );
321
322 buf->len = 0;
323 buf->num_datagrams = 0;
324
325 return( ret );
326}
327
328static inline int ctx_buffer_check( ctx_buffer *buf )
329{
330 if( buf->len > 0 &&
Hanno Becker211f44c2017-10-31 14:08:10 +0000331 ellapsed_time() - buf->packet_lifetime >= (size_t) opt.pack )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100332 {
333 return( ctx_buffer_flush( buf ) );
334 }
335
336 return( 0 );
337}
338
339static int ctx_buffer_append( ctx_buffer *buf,
340 const unsigned char * data,
341 size_t len )
342{
343 int ret;
344
345 if( len > sizeof( buf->data ) )
346 {
347 mbedtls_printf( " ! buffer size %lu too large (max %lu)\n",
348 len, sizeof( buf->data ) );
349 return( -1 );
350 }
351
352 if( sizeof( buf->data ) - buf->len < len )
353 {
354 if( ( ret = ctx_buffer_flush( buf ) ) <= 0 )
355 return( ret );
356 }
357
358 memcpy( buf->data + buf->len, data, len );
359
360 buf->len += len;
361 if( ++buf->num_datagrams == 1 )
362 buf->packet_lifetime = ellapsed_time();
363
364 return( len );
365}
366
367static int dispatch_data( mbedtls_net_context *ctx,
368 const unsigned char * data,
369 size_t len )
370{
371 ctx_buffer *buf = NULL;
372 if( outbuf[0].ctx == ctx )
373 buf = &outbuf[0];
374 else if( outbuf[1].ctx == ctx )
375 buf = &outbuf[1];
376
377 if( buf == NULL )
378 return( mbedtls_net_send( ctx, data, len ) );
379
380 return( ctx_buffer_append( buf, data, len ) );
381}
382
383typedef struct
384{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200385 mbedtls_net_context *dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200386 const char *way;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200387 const char *type;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200388 unsigned len;
389 unsigned char buf[MAX_MSG_SIZE];
390} packet;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200391
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200392/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */
393void print_packet( const packet *p, const char *why )
394{
395 if( why == NULL )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100396 mbedtls_printf( " %05lu dispatch %s %s (%u bytes)\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200397 ellapsed_time(), p->way, p->type, p->len );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200398 else
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100399 mbedtls_printf( " dispatch %s %s (%u bytes): %s\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200400 p->way, p->type, p->len, why );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200401 fflush( stdout );
402}
403
404int send_packet( const packet *p, const char *why )
405{
406 int ret;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200407 mbedtls_net_context *dst = p->dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200408
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200409 /* insert corrupted ApplicationData record? */
410 if( opt.bad_ad &&
411 strcmp( p->type, "ApplicationData" ) == 0 )
412 {
413 unsigned char buf[MAX_MSG_SIZE];
414 memcpy( buf, p->buf, p->len );
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200415
Hanno Beckerfbb0b702017-05-26 16:55:07 +0100416 if( p->len <= 13 )
417 {
418 mbedtls_printf( " ! can't corrupt empty AD record" );
419 }
420 else
421 {
422 ++buf[13];
423 print_packet( p, "corrupted" );
424 }
425
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100426 if( ( ret = dispatch_data( dst, buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200427 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100428 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200429 return( ret );
430 }
431 }
432
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200433 print_packet( p, why );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100434 if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200435 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100436 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200437 return( ret );
438 }
439
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200440 /* Don't duplicate Application Data, only handshake covered */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200441 if( opt.duplicate != 0 &&
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200442 strcmp( p->type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200443 rand() % opt.duplicate == 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200444 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200445 print_packet( p, "duplicated" );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200446
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100447 if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200448 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100449 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200450 return( ret );
451 }
452 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200453
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200454 return( 0 );
455}
456
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200457static packet prev;
458
459void clear_pending( void )
460{
461 memset( &prev, 0, sizeof( packet ) );
462}
463
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200464/*
465 * Avoid dropping or delaying a packet that was already dropped twice: this
466 * only results in uninteresting timeouts. We can't rely on type to identify
467 * packets, since during renegotiation they're all encrypted. So, rely on
468 * size mod 2048 (which is usually just size).
469 */
470static unsigned char dropped[2048] = { 0 };
471#define DROP_MAX 2
472
473/*
474 * OpenSSL groups packets in a datagram the first time it sends them, but not
475 * when it resends them. Count every record as seen the first time.
476 */
477void update_dropped( const packet *p )
478{
479 size_t id = p->len % sizeof( dropped );
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200480 const unsigned char *end = p->buf + p->len;
481 const unsigned char *cur = p->buf;
482 size_t len = ( ( cur[11] << 8 ) | cur[12] ) + 13;
483
Manuel Pégourié-Gonnardfa60f122014-09-26 16:07:29 +0200484 ++dropped[id];
485
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200486 /* Avoid counting single record twice */
487 if( len == p->len )
488 return;
489
490 while( cur < end )
491 {
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200492 len = ( ( cur[11] << 8 ) | cur[12] ) + 13;
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200493
494 id = len % sizeof( dropped );
495 ++dropped[id];
496
497 cur += len;
498 }
499}
500
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200501int handle_message( const char *way,
502 mbedtls_net_context *dst,
503 mbedtls_net_context *src )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200504{
505 int ret;
506 packet cur;
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200507 size_t id;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200508
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +0200509 /* receive packet */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200510 if( ( ret = mbedtls_net_recv( src, cur.buf, sizeof( cur.buf ) ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 mbedtls_printf( " ! mbedtls_net_recv returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200513 return( ret );
514 }
515
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200516 cur.len = ret;
517 cur.type = msg_type( cur.buf, cur.len );
518 cur.way = way;
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200519 cur.dst = dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200520 print_packet( &cur, NULL );
521
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200522 id = cur.len % sizeof( dropped );
523
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200524 /* do we want to drop, delay, or forward it? */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200525 if( ( opt.mtu != 0 &&
526 cur.len > (unsigned) opt.mtu ) ||
527 ( opt.drop != 0 &&
528 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200529 ! ( opt.protect_hvr &&
530 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200531 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200532 dropped[id] < DROP_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200533 rand() % opt.drop == 0 ) )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200534 {
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200535 update_dropped( &cur );
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200536 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200537 else if( ( opt.delay_ccs == 1 &&
538 strcmp( cur.type, "ChangeCipherSpec" ) == 0 ) ||
539 ( opt.delay != 0 &&
540 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200541 ! ( opt.protect_hvr &&
542 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200543 prev.dst == NULL &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200544 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200545 dropped[id] < DROP_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200546 rand() % opt.delay == 0 ) )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200547 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200548 memcpy( &prev, &cur, sizeof( packet ) );
549 }
550 else
551 {
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200552 /* forward and possibly duplicate */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200553 if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 )
554 return( ret );
555
556 /* send previously delayed message if any */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200557 if( prev.dst != NULL )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200558 {
559 ret = send_packet( &prev, "delayed" );
560 memset( &prev, 0, sizeof( packet ) );
561 if( ret != 0 )
562 return( ret );
563 }
564 }
565
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200566 return( 0 );
567}
568
569int main( int argc, char *argv[] )
570{
571 int ret;
572
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200573 mbedtls_net_context listen_fd, client_fd, server_fd;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100574 struct timeval tm;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200575
576 int nb_fds;
577 fd_set read_fds;
578
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100579 tm.tv_sec = 0;
580 tm.tv_usec = 0;
581
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200582 mbedtls_net_init( &listen_fd );
583 mbedtls_net_init( &client_fd );
584 mbedtls_net_init( &server_fd );
585
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200586 get_options( argc, argv );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200587
588 /*
589 * Decisions to drop/delay/duplicate packets are pseudo-random: dropping
590 * exactly 1 in N packets would lead to problems when a flight has exactly
591 * N packets: the same packet would be dropped on every resend.
592 *
593 * In order to be able to reproduce problems reliably, the seed may be
594 * specified explicitly.
595 */
596 if( opt.seed == 0 )
597 {
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200598 opt.seed = (unsigned int) time( NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 mbedtls_printf( " . Pseudo-random seed: %u\n", opt.seed );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200600 }
601
602 srand( opt.seed );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200603
604 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200605 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200606 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200607 mbedtls_printf( " . Connect to server on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200608 opt.server_addr, opt.server_port );
609 fflush( stdout );
610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
612 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200615 goto exit;
616 }
617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200619
620 /*
621 * 1. Setup the "listening" UDP socket
622 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200623 mbedtls_printf( " . Bind on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200624 opt.listen_addr, opt.listen_port );
625 fflush( stdout );
626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 if( ( ret = mbedtls_net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
628 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200631 goto exit;
632 }
633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200635
636 /*
637 * 2. Wait until a client connects
638 */
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200639accept:
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200640 mbedtls_net_free( &client_fd );
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_printf( " . Waiting for a remote connection ..." );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200643 fflush( stdout );
644
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200645 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200646 NULL, 0, NULL ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200649 goto exit;
650 }
651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200653
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200654 /*
655 * 3. Forward packets forever (kill the process to terminate it)
656 */
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200657 clear_pending();
658 memset( dropped, 0, sizeof( dropped ) );
659
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200660 nb_fds = client_fd.fd;
661 if( nb_fds < server_fd.fd )
662 nb_fds = server_fd.fd;
663 if( nb_fds < listen_fd.fd )
664 nb_fds = listen_fd.fd;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200665 ++nb_fds;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200666
Hanno Becker211f44c2017-10-31 14:08:10 +0000667 if( opt.pack > 0 )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100668 {
669 outbuf[0].ctx = &server_fd;
670 outbuf[0].description = "S <- C";
671 outbuf[0].num_datagrams = 0;
672 outbuf[0].len = 0;
673
674 outbuf[1].ctx = &client_fd;
675 outbuf[1].description = "S -> C";
676 outbuf[1].num_datagrams = 0;
677 outbuf[1].len = 0;
678 }
679
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200680 while( 1 )
681 {
682 FD_ZERO( &read_fds );
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200683 FD_SET( server_fd.fd, &read_fds );
684 FD_SET( client_fd.fd, &read_fds );
685 FD_SET( listen_fd.fd, &read_fds );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200686
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100687 ctx_buffer_check( &outbuf[0] );
688 ctx_buffer_check( &outbuf[1] );
689
690 if( ( ret = select( nb_fds, &read_fds, NULL, NULL, &tm ) ) < 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200691 {
692 perror( "select" );
693 goto exit;
694 }
695
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200696 if( FD_ISSET( listen_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200697 goto accept;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200698
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200699 if( FD_ISSET( client_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200700 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200701 if( ( ret = handle_message( "S <- C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200702 &server_fd, &client_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200703 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200704 }
705
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200706 if( FD_ISSET( server_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200707 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200708 if( ( ret = handle_message( "S -> C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200709 &client_fd, &server_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200710 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200711 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100712
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200713 }
714
715exit:
716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717#ifdef MBEDTLS_ERROR_C
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200718 if( ret != 0 )
719 {
720 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_strerror( ret, error_buf, 100 );
722 mbedtls_printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200723 fflush( stdout );
724 }
725#endif
726
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200727 mbedtls_net_free( &client_fd );
728 mbedtls_net_free( &server_fd );
729 mbedtls_net_free( &listen_fd );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200730
731#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 mbedtls_printf( " Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200733 fflush( stdout ); getchar();
734#endif
735
736 return( ret != 0 );
737}
738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739#endif /* MBEDTLS_NET_C */