blob: 2585220037131e12a6ab8a5df7e1b9fbca37eafd [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 Garcia80081a62018-04-29 21:58:53 +010040#define mbedtls_time time
41#define mbedtls_time_t time_t
42#define mbedtls_printf printf
Hanno Becker01ea7782018-08-17 13:33:41 +010043#define mbedtls_calloc calloc
44#define mbedtls_free free
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010045#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia80081a62018-04-29 21:58:53 +010046#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
47#endif /* MBEDTLS_PLATFORM_C */
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_NET_C)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020050int main( void )
51{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052 mbedtls_printf( "MBEDTLS_NET_C not defined.\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020053 return( 0 );
54}
55#else
56
Andres AG788aa4a2016-09-14 14:32:09 +010057#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/error.h"
59#include "mbedtls/ssl.h"
Hanno Becker0cc77742017-10-31 14:10:07 +000060#include "mbedtls/timing.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020061
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +000062#include <string.h>
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020063
64/* For select() */
65#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
66 !defined(EFI32)
67#include <winsock2.h>
68#include <windows.h>
69#if defined(_MSC_VER)
70#if defined(_WIN32_WCE)
71#pragma comment( lib, "ws2.lib" )
72#else
73#pragma comment( lib, "ws2_32.lib" )
74#endif
75#endif /* _MSC_VER */
76#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
77#include <sys/time.h>
78#include <sys/types.h>
79#include <unistd.h>
80#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
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
Hanno Becker0cc77742017-10-31 14:10:07 +000090#if defined(MBEDTLS_TIMING_C)
91#define USAGE_PACK \
92 " pack=%%d default: 0 (don't pack)\n" \
93 " options: t > 0 (pack for t milliseconds)\n"
94#else
95#define USAGE_PACK
96#endif
97
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020098#define USAGE \
99 "\n usage: udp_proxy param=<>...\n" \
100 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200101 " server_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200102 " server_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200103 " listen_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200104 " listen_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200105 "\n" \
106 " duplicate=%%d default: 0 (no duplication)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200107 " duplicate about 1:N packets randomly\n" \
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200108 " delay=%%d default: 0 (no delayed packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200109 " delay about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200110 " delay_ccs=0/1 default: 0 (don't delay ChangeCipherSpec)\n" \
Hanno Becker01ea7782018-08-17 13:33:41 +0100111 " delay_cli=%%s Handshake message from client that should be\n"\
112 " delayed. Possible values are 'ClientHello',\n" \
113 " 'Certificate', 'CertificateVerify', and\n" \
114 " 'ClientKeyExchange'.\n" \
115 " May be used multiple times, even for the same\n"\
116 " message, in which case the respective message\n"\
117 " gets delayed multiple times.\n" \
118 " delay_srv=%%s Handshake message from server that should be\n"\
119 " delayed. Possible values are 'HelloRequest',\n"\
120 " 'ServerHello', 'ServerHelloDone', 'Certificate'\n"\
121 " 'ServerKeyExchange', 'NewSessionTicket',\n"\
122 " 'HelloVerifyRequest' and ''CertificateRequest'.\n"\
123 " May be used multiple times, even for the same\n"\
124 " message, in which case the respective message\n"\
125 " gets delayed multiple times.\n" \
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200126 " drop=%%d default: 0 (no dropped packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200127 " drop about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200128 " mtu=%%d default: 0 (unlimited)\n" \
129 " drop packets larger than N bytes\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200130 " bad_ad=0/1 default: 0 (don't add bad ApplicationData)\n" \
131 " protect_hvr=0/1 default: 0 (don't protect HelloVerifyRequest)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000132 " protect_len=%%d default: (don't protect packets of this size)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200133 "\n" \
134 " seed=%%d default: (use current time)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000135 USAGE_PACK \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200136 "\n"
137
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200138/*
139 * global options
140 */
Hanno Becker01ea7782018-08-17 13:33:41 +0100141
142#define MAX_DELAYED_HS 10
143
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200144static struct options
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200145{
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200146 const char *server_addr; /* address to forward packets to */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200147 const char *server_port; /* port to forward packets to */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200148 const char *listen_addr; /* address for accepting client connections */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200149 const char *listen_port; /* port for accepting client connections */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200150
151 int duplicate; /* duplicate 1 in N packets (none if 0) */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200152 int delay; /* delay 1 packet in N (none if 0) */
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200153 int delay_ccs; /* delay ChangeCipherSpec */
Hanno Becker01ea7782018-08-17 13:33:41 +0100154 char* delay_cli[MAX_DELAYED_HS]; /* handshake types of messages from
155 * client that should be delayed. */
156 uint8_t delay_cli_cnt; /* Number of entries in delay_cli. */
157 char* delay_srv[MAX_DELAYED_HS]; /* handshake types of messages from
158 * server that should be delayed. */
159 uint8_t delay_srv_cnt; /* Number of entries in delay_srv. */
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200160 int drop; /* drop 1 packet in N (none if 0) */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200161 int mtu; /* drop packets larger than this */
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200162 int bad_ad; /* inject corrupted ApplicationData record */
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200163 int protect_hvr; /* never drop or delay HelloVerifyRequest */
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200164 int protect_len; /* never drop/delay packet of the given size*/
Hanno Becker77abef52017-11-02 10:50:28 +0000165 unsigned pack; /* merge packets into single datagram for
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100166 * at most \c merge milliseconds if > 0 */
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200167 unsigned int seed; /* seed for "random" events */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200168} opt;
169
170static void exit_usage( const char *name, const char *value )
171{
172 if( value == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 mbedtls_printf( " unknown option or missing value: %s\n", name );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200174 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_printf( " option %s: illegal value: %s\n", name, value );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 mbedtls_printf( USAGE );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200178 exit( 1 );
179}
180
181static void get_options( int argc, char *argv[] )
182{
183 int i;
184 char *p, *q;
185
186 opt.server_addr = DFL_SERVER_ADDR;
187 opt.server_port = DFL_SERVER_PORT;
188 opt.listen_addr = DFL_LISTEN_ADDR;
189 opt.listen_port = DFL_LISTEN_PORT;
Hanno Becker211f44c2017-10-31 14:08:10 +0000190 opt.pack = DFL_PACK;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200191 /* Other members default to 0 */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200192
Hanno Becker01ea7782018-08-17 13:33:41 +0100193 opt.delay_cli_cnt = 0;
194 opt.delay_srv_cnt = 0;
195 memset( opt.delay_cli, 0, sizeof( opt.delay_cli ) );
196 memset( opt.delay_srv, 0, sizeof( opt.delay_srv ) );
197
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200198 for( i = 1; i < argc; i++ )
199 {
200 p = argv[i];
201 if( ( q = strchr( p, '=' ) ) == NULL )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200202 exit_usage( p, NULL );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200203 *q++ = '\0';
204
205 if( strcmp( p, "server_addr" ) == 0 )
206 opt.server_addr = q;
207 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200208 opt.server_port = q;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200209 else if( strcmp( p, "listen_addr" ) == 0 )
210 opt.listen_addr = q;
211 else if( strcmp( p, "listen_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200212 opt.listen_port = q;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200213 else if( strcmp( p, "duplicate" ) == 0 )
214 {
215 opt.duplicate = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200216 if( opt.duplicate < 0 || opt.duplicate > 20 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200217 exit_usage( p, q );
218 }
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200219 else if( strcmp( p, "delay" ) == 0 )
220 {
221 opt.delay = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200222 if( opt.delay < 0 || opt.delay > 20 || opt.delay == 1 )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200223 exit_usage( p, q );
224 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200225 else if( strcmp( p, "delay_ccs" ) == 0 )
226 {
227 opt.delay_ccs = atoi( q );
228 if( opt.delay_ccs < 0 || opt.delay_ccs > 1 )
229 exit_usage( p, q );
230 }
Hanno Becker01ea7782018-08-17 13:33:41 +0100231 else if( strcmp( p, "delay_cli" ) == 0 ||
232 strcmp( p, "delay_srv" ) == 0 )
233 {
234 uint8_t *delay_cnt;
235 char **delay_list;
236 size_t len;
237 char *buf;
238
239 if( strcmp( p, "delay_cli" ) == 0 )
240 {
241 delay_cnt = &opt.delay_cli_cnt;
242 delay_list = opt.delay_cli;
243 }
244 else
245 {
246 delay_cnt = &opt.delay_srv_cnt;
247 delay_list = opt.delay_srv;
248 }
249
250 if( *delay_cnt == MAX_DELAYED_HS )
251 {
Hanno Beckercaf87412018-08-20 09:45:51 +0100252 mbedtls_printf( " maximally %d uses of delay_cli argument allowed\n",
Hanno Becker01ea7782018-08-17 13:33:41 +0100253 MAX_DELAYED_HS );
254 exit_usage( p, NULL );
255 }
256
257 len = strlen( q );
258 buf = mbedtls_calloc( 1, len + 1 );
259 if( buf == NULL )
260 {
261 mbedtls_printf( " Allocation failure\n" );
262 exit( 1 );
263 }
264 memcpy( buf, q, len + 1 );
265
266 delay_list[ (*delay_cnt)++ ] = buf;
267 }
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200268 else if( strcmp( p, "drop" ) == 0 )
269 {
270 opt.drop = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200271 if( opt.drop < 0 || opt.drop > 20 || opt.drop == 1 )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200272 exit_usage( p, q );
273 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100274 else if( strcmp( p, "pack" ) == 0 )
275 {
Hanno Becker0cc77742017-10-31 14:10:07 +0000276#if defined(MBEDTLS_TIMING_C)
Hanno Becker77abef52017-11-02 10:50:28 +0000277 opt.pack = (unsigned) atoi( q );
Hanno Becker0cc77742017-10-31 14:10:07 +0000278#else
279 mbedtls_printf( " option pack only defined if MBEDTLS_TIMING_C is enabled\n" );
280 exit( 1 );
281#endif
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100282 }
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200283 else if( strcmp( p, "mtu" ) == 0 )
284 {
285 opt.mtu = atoi( q );
286 if( opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE )
287 exit_usage( p, q );
288 }
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200289 else if( strcmp( p, "bad_ad" ) == 0 )
290 {
291 opt.bad_ad = atoi( q );
292 if( opt.bad_ad < 0 || opt.bad_ad > 1 )
293 exit_usage( p, q );
294 }
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200295 else if( strcmp( p, "protect_hvr" ) == 0 )
296 {
297 opt.protect_hvr = atoi( q );
298 if( opt.protect_hvr < 0 || opt.protect_hvr > 1 )
299 exit_usage( p, q );
300 }
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200301 else if( strcmp( p, "protect_len" ) == 0 )
302 {
303 opt.protect_len = atoi( q );
304 if( opt.protect_len < 0 )
305 exit_usage( p, q );
306 }
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200307 else if( strcmp( p, "seed" ) == 0 )
308 {
309 opt.seed = atoi( q );
310 if( opt.seed == 0 )
311 exit_usage( p, q );
312 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200313 else
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200314 exit_usage( p, NULL );
315 }
316}
317
318static const char *msg_type( unsigned char *msg, size_t len )
319{
320 if( len < 1 ) return( "Invalid" );
321 switch( msg[0] )
322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" );
324 case MBEDTLS_SSL_MSG_ALERT: return( "Alert" );
325 case MBEDTLS_SSL_MSG_APPLICATION_DATA: return( "ApplicationData" );
326 case MBEDTLS_SSL_MSG_HANDSHAKE: break; /* See below */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200327 default: return( "Unknown" );
328 }
329
Manuel Pégourié-Gonnard8cc7e032014-09-25 12:59:05 +0200330 if( len < 13 + 12 ) return( "Invalid handshake" );
331
332 /*
333 * Our handshake message are less than 2^16 bytes long, so they should
334 * have 0 as the first byte of length, frag_offset and frag_length.
335 * Otherwise, assume they are encrypted.
336 */
337 if( msg[14] || msg[19] || msg[22] ) return( "Encrypted handshake" );
338
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200339 switch( msg[13] )
340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 case MBEDTLS_SSL_HS_HELLO_REQUEST: return( "HelloRequest" );
342 case MBEDTLS_SSL_HS_CLIENT_HELLO: return( "ClientHello" );
343 case MBEDTLS_SSL_HS_SERVER_HELLO: return( "ServerHello" );
344 case MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" );
345 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" );
346 case MBEDTLS_SSL_HS_CERTIFICATE: return( "Certificate" );
347 case MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" );
348 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" );
349 case MBEDTLS_SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" );
350 case MBEDTLS_SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" );
351 case MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" );
352 case MBEDTLS_SSL_HS_FINISHED: return( "Finished" );
Manuel Pégourié-Gonnardbc010a02014-09-20 12:40:51 +0200353 default: return( "Unknown handshake" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200354 }
355}
356
Hanno Becker0cc77742017-10-31 14:10:07 +0000357#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200358/* Return elapsed time in milliseconds since the first call */
Hanno Becker77abef52017-11-02 10:50:28 +0000359static unsigned ellapsed_time( void )
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200360{
Hanno Becker92474da2017-10-31 14:09:30 +0000361 static int initialized = 0;
362 static struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200363
Hanno Becker92474da2017-10-31 14:09:30 +0000364 if( initialized == 0 )
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200365 {
Hanno Becker92474da2017-10-31 14:09:30 +0000366 (void) mbedtls_timing_get_timer( &hires, 1 );
367 initialized = 1;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200368 return( 0 );
369 }
370
Hanno Becker92474da2017-10-31 14:09:30 +0000371 return( mbedtls_timing_get_timer( &hires, 0 ) );
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200372}
373
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200374typedef struct
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200375{
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100376 mbedtls_net_context *ctx;
377
378 const char *description;
379
Hanno Becker77abef52017-11-02 10:50:28 +0000380 unsigned packet_lifetime;
381 unsigned num_datagrams;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100382
383 unsigned char data[MAX_MSG_SIZE];
Hanno Beckera5e68972017-12-06 08:35:02 +0000384 size_t len;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100385
386} ctx_buffer;
387
388static ctx_buffer outbuf[2];
389
390static int ctx_buffer_flush( ctx_buffer *buf )
391{
392 int ret;
393
Hanno Becker77abef52017-11-02 10:50:28 +0000394 mbedtls_printf( " %05u flush %s: %u bytes, %u datagrams, last %u ms\n",
395 ellapsed_time(), buf->description,
Hanno Beckera5e68972017-12-06 08:35:02 +0000396 (unsigned) buf->len, buf->num_datagrams,
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100397 ellapsed_time() - buf->packet_lifetime );
398
399 ret = mbedtls_net_send( buf->ctx, buf->data, buf->len );
400
401 buf->len = 0;
402 buf->num_datagrams = 0;
403
404 return( ret );
405}
406
Hanno Becker77abef52017-11-02 10:50:28 +0000407static unsigned ctx_buffer_time_remaining( ctx_buffer *buf )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100408{
Hanno Becker77abef52017-11-02 10:50:28 +0000409 unsigned const cur_time = ellapsed_time();
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100410
Hanno Becker77abef52017-11-02 10:50:28 +0000411 if( buf->num_datagrams == 0 )
412 return( (unsigned) -1 );
413
414 if( cur_time - buf->packet_lifetime >= opt.pack )
415 return( 0 );
416
417 return( opt.pack - ( cur_time - buf->packet_lifetime ) );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100418}
419
420static int ctx_buffer_append( ctx_buffer *buf,
421 const unsigned char * data,
422 size_t len )
423{
424 int ret;
425
Hanno Beckera5e68972017-12-06 08:35:02 +0000426 if( len > (size_t) INT_MAX )
427 return( -1 );
428
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100429 if( len > sizeof( buf->data ) )
430 {
Hanno Becker77abef52017-11-02 10:50:28 +0000431 mbedtls_printf( " ! buffer size %u too large (max %u)\n",
432 (unsigned) len, (unsigned) sizeof( buf->data ) );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100433 return( -1 );
434 }
435
436 if( sizeof( buf->data ) - buf->len < len )
437 {
438 if( ( ret = ctx_buffer_flush( buf ) ) <= 0 )
439 return( ret );
440 }
441
442 memcpy( buf->data + buf->len, data, len );
443
444 buf->len += len;
445 if( ++buf->num_datagrams == 1 )
446 buf->packet_lifetime = ellapsed_time();
447
Hanno Beckera5e68972017-12-06 08:35:02 +0000448 return( (int) len );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100449}
Hanno Becker77abef52017-11-02 10:50:28 +0000450#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100451
452static int dispatch_data( mbedtls_net_context *ctx,
453 const unsigned char * data,
454 size_t len )
455{
Hanno Becker77abef52017-11-02 10:50:28 +0000456#if defined(MBEDTLS_TIMING_C)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100457 ctx_buffer *buf = NULL;
Hanno Becker77abef52017-11-02 10:50:28 +0000458 if( opt.pack > 0 )
459 {
460 if( outbuf[0].ctx == ctx )
461 buf = &outbuf[0];
462 else if( outbuf[1].ctx == ctx )
463 buf = &outbuf[1];
Hanno Becker0cc77742017-10-31 14:10:07 +0000464
Hanno Becker77abef52017-11-02 10:50:28 +0000465 if( buf == NULL )
466 return( -1 );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100467
Hanno Becker77abef52017-11-02 10:50:28 +0000468 return( ctx_buffer_append( buf, data, len ) );
469 }
470#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100471
Hanno Becker0cc77742017-10-31 14:10:07 +0000472 return( mbedtls_net_send( ctx, data, len ) );
473}
474
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100475typedef struct
476{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200477 mbedtls_net_context *dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200478 const char *way;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200479 const char *type;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200480 unsigned len;
481 unsigned char buf[MAX_MSG_SIZE];
482} packet;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200483
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200484/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */
485void print_packet( const packet *p, const char *why )
486{
Hanno Becker0cc77742017-10-31 14:10:07 +0000487#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200488 if( why == NULL )
Hanno Becker77abef52017-11-02 10:50:28 +0000489 mbedtls_printf( " %05u dispatch %s %s (%u bytes)\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200490 ellapsed_time(), p->way, p->type, p->len );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200491 else
Hanno Becker77abef52017-11-02 10:50:28 +0000492 mbedtls_printf( " %05u dispatch %s %s (%u bytes): %s\n",
Hanno Becker0cc77742017-10-31 14:10:07 +0000493 ellapsed_time(), p->way, p->type, p->len, why );
494#else
495 if( why == NULL )
496 mbedtls_printf( " dispatch %s %s (%u bytes)\n",
497 p->way, p->type, p->len );
498 else
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100499 mbedtls_printf( " dispatch %s %s (%u bytes): %s\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200500 p->way, p->type, p->len, why );
Hanno Becker0cc77742017-10-31 14:10:07 +0000501#endif
502
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200503 fflush( stdout );
504}
505
506int send_packet( const packet *p, const char *why )
507{
508 int ret;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200509 mbedtls_net_context *dst = p->dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200510
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200511 /* insert corrupted ApplicationData record? */
512 if( opt.bad_ad &&
513 strcmp( p->type, "ApplicationData" ) == 0 )
514 {
515 unsigned char buf[MAX_MSG_SIZE];
516 memcpy( buf, p->buf, p->len );
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200517
Hanno Beckerfbb0b702017-05-26 16:55:07 +0100518 if( p->len <= 13 )
519 {
520 mbedtls_printf( " ! can't corrupt empty AD record" );
521 }
522 else
523 {
524 ++buf[13];
525 print_packet( p, "corrupted" );
526 }
527
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100528 if( ( ret = dispatch_data( dst, buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200529 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100530 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200531 return( ret );
532 }
533 }
534
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200535 print_packet( p, why );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100536 if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200537 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100538 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200539 return( ret );
540 }
541
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200542 /* Don't duplicate Application Data, only handshake covered */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200543 if( opt.duplicate != 0 &&
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200544 strcmp( p->type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200545 rand() % opt.duplicate == 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200546 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200547 print_packet( p, "duplicated" );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200548
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100549 if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200550 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100551 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200552 return( ret );
553 }
554 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200555
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200556 return( 0 );
557}
558
Hanno Becker101bcba2018-08-21 16:39:51 +0100559#define MAX_DELAYED_MSG 5
560static size_t prev_len;
561static packet prev[MAX_DELAYED_MSG];
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200562
563void clear_pending( void )
564{
Hanno Becker12b72c12018-08-23 13:15:36 +0100565 memset( &prev, 0, sizeof( prev ) );
Hanno Becker101bcba2018-08-21 16:39:51 +0100566 prev_len = 0;
567}
568
569void delay_packet( packet *delay )
570{
571 if( prev_len == MAX_DELAYED_MSG )
572 return;
573
574 memcpy( &prev[prev_len++], delay, sizeof( packet ) );
575}
576
577int send_delayed()
578{
579 uint8_t offset;
580 int ret;
581 for( offset = 0; offset < prev_len; offset++ )
582 {
583 ret = send_packet( &prev[offset], "delayed" );
584 if( ret != 0 )
585 return( ret );
586 }
587
588 clear_pending();
589 return( 0 );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200590}
591
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200592/*
593 * Avoid dropping or delaying a packet that was already dropped twice: this
594 * only results in uninteresting timeouts. We can't rely on type to identify
595 * packets, since during renegotiation they're all encrypted. So, rely on
596 * size mod 2048 (which is usually just size).
597 */
598static unsigned char dropped[2048] = { 0 };
599#define DROP_MAX 2
600
601/*
602 * OpenSSL groups packets in a datagram the first time it sends them, but not
603 * when it resends them. Count every record as seen the first time.
604 */
605void update_dropped( const packet *p )
606{
607 size_t id = p->len % sizeof( dropped );
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200608 const unsigned char *end = p->buf + p->len;
609 const unsigned char *cur = p->buf;
610 size_t len = ( ( cur[11] << 8 ) | cur[12] ) + 13;
611
Manuel Pégourié-Gonnardfa60f122014-09-26 16:07:29 +0200612 ++dropped[id];
613
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200614 /* Avoid counting single record twice */
615 if( len == p->len )
616 return;
617
618 while( cur < end )
619 {
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200620 len = ( ( cur[11] << 8 ) | cur[12] ) + 13;
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200621
622 id = len % sizeof( dropped );
623 ++dropped[id];
624
625 cur += len;
626 }
627}
628
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200629int handle_message( const char *way,
630 mbedtls_net_context *dst,
631 mbedtls_net_context *src )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200632{
633 int ret;
634 packet cur;
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200635 size_t id;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200636
Hanno Becker01ea7782018-08-17 13:33:41 +0100637 uint8_t delay_idx;
638 char ** delay_list;
639 uint8_t delay_list_len;
640
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +0200641 /* receive packet */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200642 if( ( ret = mbedtls_net_recv( src, cur.buf, sizeof( cur.buf ) ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 mbedtls_printf( " ! mbedtls_net_recv returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200645 return( ret );
646 }
647
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200648 cur.len = ret;
649 cur.type = msg_type( cur.buf, cur.len );
650 cur.way = way;
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200651 cur.dst = dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200652 print_packet( &cur, NULL );
653
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200654 id = cur.len % sizeof( dropped );
655
Hanno Becker01ea7782018-08-17 13:33:41 +0100656 if( strcmp( way, "S <- C" ) == 0 )
657 {
658 delay_list = opt.delay_cli;
659 delay_list_len = opt.delay_cli_cnt;
660 }
661 else
662 {
663 delay_list = opt.delay_srv;
664 delay_list_len = opt.delay_srv_cnt;
665 }
666 /* Check if message type is in the list of messages
667 * that should be delayed */
668 for( delay_idx = 0; delay_idx < delay_list_len; delay_idx++ )
669 {
670 if( delay_list[ delay_idx ] == NULL )
671 continue;
672
673 if( strcmp( delay_list[ delay_idx ], cur.type ) == 0 )
674 {
675 /* Delay message */
Hanno Becker101bcba2018-08-21 16:39:51 +0100676 delay_packet( &cur );
Hanno Becker01ea7782018-08-17 13:33:41 +0100677
678 /* Remove entry from list */
679 mbedtls_free( delay_list[delay_idx] );
680 delay_list[delay_idx] = NULL;
681
682 return( 0 );
683 }
684 }
685
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200686 /* do we want to drop, delay, or forward it? */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200687 if( ( opt.mtu != 0 &&
688 cur.len > (unsigned) opt.mtu ) ||
689 ( opt.drop != 0 &&
690 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200691 ! ( opt.protect_hvr &&
692 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200693 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200694 dropped[id] < DROP_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200695 rand() % opt.drop == 0 ) )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200696 {
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200697 update_dropped( &cur );
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200698 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200699 else if( ( opt.delay_ccs == 1 &&
700 strcmp( cur.type, "ChangeCipherSpec" ) == 0 ) ||
701 ( opt.delay != 0 &&
702 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200703 ! ( opt.protect_hvr &&
704 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200705 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200706 dropped[id] < DROP_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200707 rand() % opt.delay == 0 ) )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200708 {
Hanno Becker101bcba2018-08-21 16:39:51 +0100709 delay_packet( &cur );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200710 }
711 else
712 {
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200713 /* forward and possibly duplicate */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200714 if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 )
715 return( ret );
716
Hanno Becker101bcba2018-08-21 16:39:51 +0100717 /* send previously delayed messages if any */
718 ret = send_delayed();
719 if( ret != 0 )
720 return( ret );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200721 }
722
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200723 return( 0 );
724}
725
726int main( int argc, char *argv[] )
727{
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100728 int ret = 1;
729 int exit_code = MBEDTLS_EXIT_FAILURE;
Hanno Becker01ea7782018-08-17 13:33:41 +0100730 uint8_t delay_idx;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200731
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200732 mbedtls_net_context listen_fd, client_fd, server_fd;
Hanno Becker77abef52017-11-02 10:50:28 +0000733
734#if defined( MBEDTLS_TIMING_C )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100735 struct timeval tm;
Hanno Becker77abef52017-11-02 10:50:28 +0000736#endif
737
738 struct timeval *tm_ptr = NULL;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200739
740 int nb_fds;
741 fd_set read_fds;
742
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200743 mbedtls_net_init( &listen_fd );
744 mbedtls_net_init( &client_fd );
745 mbedtls_net_init( &server_fd );
746
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200747 get_options( argc, argv );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200748
749 /*
750 * Decisions to drop/delay/duplicate packets are pseudo-random: dropping
751 * exactly 1 in N packets would lead to problems when a flight has exactly
752 * N packets: the same packet would be dropped on every resend.
753 *
754 * In order to be able to reproduce problems reliably, the seed may be
755 * specified explicitly.
756 */
757 if( opt.seed == 0 )
758 {
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200759 opt.seed = (unsigned int) time( NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 mbedtls_printf( " . Pseudo-random seed: %u\n", opt.seed );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200761 }
762
763 srand( opt.seed );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200764
765 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200766 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200767 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200768 mbedtls_printf( " . Connect to server on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200769 opt.server_addr, opt.server_port );
770 fflush( stdout );
771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
773 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200776 goto exit;
777 }
778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200780
781 /*
782 * 1. Setup the "listening" UDP socket
783 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200784 mbedtls_printf( " . Bind on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200785 opt.listen_addr, opt.listen_port );
786 fflush( stdout );
787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 if( ( ret = mbedtls_net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
789 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200792 goto exit;
793 }
794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200796
797 /*
798 * 2. Wait until a client connects
799 */
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200800accept:
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200801 mbedtls_net_free( &client_fd );
802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 mbedtls_printf( " . Waiting for a remote connection ..." );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200804 fflush( stdout );
805
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200806 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200807 NULL, 0, NULL ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 mbedtls_printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200810 goto exit;
811 }
812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200814
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200815 /*
816 * 3. Forward packets forever (kill the process to terminate it)
817 */
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200818 clear_pending();
819 memset( dropped, 0, sizeof( dropped ) );
820
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200821 nb_fds = client_fd.fd;
822 if( nb_fds < server_fd.fd )
823 nb_fds = server_fd.fd;
824 if( nb_fds < listen_fd.fd )
825 nb_fds = listen_fd.fd;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200826 ++nb_fds;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200827
Hanno Becker0cc77742017-10-31 14:10:07 +0000828#if defined(MBEDTLS_TIMING_C)
Hanno Becker211f44c2017-10-31 14:08:10 +0000829 if( opt.pack > 0 )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100830 {
831 outbuf[0].ctx = &server_fd;
832 outbuf[0].description = "S <- C";
833 outbuf[0].num_datagrams = 0;
834 outbuf[0].len = 0;
835
836 outbuf[1].ctx = &client_fd;
837 outbuf[1].description = "S -> C";
838 outbuf[1].num_datagrams = 0;
839 outbuf[1].len = 0;
840 }
Hanno Becker0cc77742017-10-31 14:10:07 +0000841#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100842
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200843 while( 1 )
844 {
Hanno Becker77abef52017-11-02 10:50:28 +0000845#if defined(MBEDTLS_TIMING_C)
846 if( opt.pack > 0 )
847 {
848 unsigned max_wait_server, max_wait_client, max_wait;
849 max_wait_server = ctx_buffer_time_remaining( &outbuf[0] );
850 max_wait_client = ctx_buffer_time_remaining( &outbuf[1] );
851
852 max_wait = (unsigned) -1;
853
854 if( max_wait_server == 0 )
855 ctx_buffer_flush( &outbuf[0] );
856 else
857 max_wait = max_wait_server;
858
859 if( max_wait_client == 0 )
860 ctx_buffer_flush( &outbuf[1] );
861 else
862 {
863 if( max_wait_client < max_wait )
864 max_wait = max_wait_client;
865 }
866
867 if( max_wait != (unsigned) -1 )
868 {
869 tm.tv_sec = max_wait / 1000;
870 tm.tv_usec = ( max_wait % 1000 ) * 1000;
871
872 tm_ptr = &tm;
873 }
874 else
875 {
876 tm_ptr = NULL;
877 }
878 }
879#endif /* MBEDTLS_TIMING_C */
880
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200881 FD_ZERO( &read_fds );
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200882 FD_SET( server_fd.fd, &read_fds );
883 FD_SET( client_fd.fd, &read_fds );
884 FD_SET( listen_fd.fd, &read_fds );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200885
Hanno Becker77abef52017-11-02 10:50:28 +0000886 if( ( ret = select( nb_fds, &read_fds, NULL, NULL, tm_ptr ) ) < 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200887 {
888 perror( "select" );
889 goto exit;
890 }
891
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200892 if( FD_ISSET( listen_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200893 goto accept;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200894
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200895 if( FD_ISSET( client_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200896 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200897 if( ( ret = handle_message( "S <- C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200898 &server_fd, &client_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200899 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200900 }
901
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200902 if( FD_ISSET( server_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200903 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200904 if( ( ret = handle_message( "S -> C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200905 &client_fd, &server_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200906 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200907 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100908
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200909 }
910
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100911 exit_code = MBEDTLS_EXIT_SUCCESS;
912
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200913exit:
914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915#ifdef MBEDTLS_ERROR_C
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100916 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200917 {
918 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 mbedtls_strerror( ret, error_buf, 100 );
920 mbedtls_printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200921 fflush( stdout );
922 }
923#endif
924
Hanno Becker01ea7782018-08-17 13:33:41 +0100925 for( delay_idx = 0; delay_idx < MAX_DELAYED_HS; delay_idx++ )
926 {
927 mbedtls_free( opt.delay_cli + delay_idx );
928 mbedtls_free( opt.delay_srv + delay_idx );
929 }
930
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200931 mbedtls_net_free( &client_fd );
932 mbedtls_net_free( &server_fd );
933 mbedtls_net_free( &listen_fd );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200934
935#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 mbedtls_printf( " Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200937 fflush( stdout ); getchar();
938#endif
939
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100940 return( exit_code );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200941}
942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943#endif /* MBEDTLS_NET_C */