blob: 62508a097f68c6ee542dca041f9996b408ace6ec [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
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020045#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010046#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia80081a62018-04-29 21:58:53 +010047#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
48#endif /* MBEDTLS_PLATFORM_C */
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#if !defined(MBEDTLS_NET_C)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020051int main( void )
52{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053 mbedtls_printf( "MBEDTLS_NET_C not defined.\n" );
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020054 mbedtls_exit( 0 );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020055}
56#else
57
Andres AG788aa4a2016-09-14 14:32:09 +010058#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/error.h"
60#include "mbedtls/ssl.h"
Hanno Becker0cc77742017-10-31 14:10:07 +000061#include "mbedtls/timing.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020062
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +000063#include <string.h>
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020064
65/* For select() */
66#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
67 !defined(EFI32)
68#include <winsock2.h>
69#include <windows.h>
70#if defined(_MSC_VER)
71#if defined(_WIN32_WCE)
72#pragma comment( lib, "ws2.lib" )
73#else
74#pragma comment( lib, "ws2_32.lib" )
75#endif
76#endif /* _MSC_VER */
77#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
78#include <sys/time.h>
79#include <sys/types.h>
80#include <unistd.h>
81#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
82
Manuel Pégourié-Gonnardb46780e2014-09-23 12:17:30 +020083#define MAX_MSG_SIZE 16384 + 2048 /* max record/datagram size */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020084
85#define DFL_SERVER_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020086#define DFL_SERVER_PORT "4433"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020087#define DFL_LISTEN_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020088#define DFL_LISTEN_PORT "5556"
Hanno Becker1dd62ea2017-05-22 14:30:59 +010089#define DFL_PACK 0
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020090
Hanno Becker0cc77742017-10-31 14:10:07 +000091#if defined(MBEDTLS_TIMING_C)
92#define USAGE_PACK \
93 " pack=%%d default: 0 (don't pack)\n" \
94 " options: t > 0 (pack for t milliseconds)\n"
95#else
96#define USAGE_PACK
97#endif
98
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020099#define USAGE \
100 "\n usage: udp_proxy param=<>...\n" \
101 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200102 " server_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200103 " server_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200104 " listen_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200105 " listen_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200106 "\n" \
107 " duplicate=%%d default: 0 (no duplication)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200108 " duplicate about 1:N packets randomly\n" \
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200109 " delay=%%d default: 0 (no delayed packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200110 " delay about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200111 " delay_ccs=0/1 default: 0 (don't delay ChangeCipherSpec)\n" \
Hanno Becker01ea7782018-08-17 13:33:41 +0100112 " delay_cli=%%s Handshake message from client that should be\n"\
113 " delayed. Possible values are 'ClientHello',\n" \
114 " 'Certificate', 'CertificateVerify', and\n" \
115 " 'ClientKeyExchange'.\n" \
116 " May be used multiple times, even for the same\n"\
117 " message, in which case the respective message\n"\
118 " gets delayed multiple times.\n" \
119 " delay_srv=%%s Handshake message from server that should be\n"\
120 " delayed. Possible values are 'HelloRequest',\n"\
121 " 'ServerHello', 'ServerHelloDone', 'Certificate'\n"\
122 " 'ServerKeyExchange', 'NewSessionTicket',\n"\
123 " 'HelloVerifyRequest' and ''CertificateRequest'.\n"\
124 " May be used multiple times, even for the same\n"\
125 " message, in which case the respective message\n"\
126 " gets delayed multiple times.\n" \
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200127 " drop=%%d default: 0 (no dropped packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200128 " drop about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200129 " mtu=%%d default: 0 (unlimited)\n" \
130 " drop packets larger than N bytes\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200131 " bad_ad=0/1 default: 0 (don't add bad ApplicationData)\n" \
132 " protect_hvr=0/1 default: 0 (don't protect HelloVerifyRequest)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000133 " protect_len=%%d default: (don't protect packets of this size)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200134 "\n" \
135 " seed=%%d default: (use current time)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000136 USAGE_PACK \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200137 "\n"
138
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200139/*
140 * global options
141 */
Hanno Becker01ea7782018-08-17 13:33:41 +0100142
143#define MAX_DELAYED_HS 10
144
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200145static struct options
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200146{
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200147 const char *server_addr; /* address to forward packets to */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200148 const char *server_port; /* port to forward packets to */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200149 const char *listen_addr; /* address for accepting client connections */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200150 const char *listen_port; /* port for accepting client connections */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200151
152 int duplicate; /* duplicate 1 in N packets (none if 0) */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200153 int delay; /* delay 1 packet in N (none if 0) */
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200154 int delay_ccs; /* delay ChangeCipherSpec */
Hanno Becker01ea7782018-08-17 13:33:41 +0100155 char* delay_cli[MAX_DELAYED_HS]; /* handshake types of messages from
Hanno Becker41038102018-08-28 11:15:32 +0100156 * client that should be delayed. */
Hanno Becker01ea7782018-08-17 13:33:41 +0100157 uint8_t delay_cli_cnt; /* Number of entries in delay_cli. */
158 char* delay_srv[MAX_DELAYED_HS]; /* handshake types of messages from
Hanno Becker41038102018-08-28 11:15:32 +0100159 * server that should be delayed. */
Hanno Becker01ea7782018-08-17 13:33:41 +0100160 uint8_t delay_srv_cnt; /* Number of entries in delay_srv. */
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200161 int drop; /* drop 1 packet in N (none if 0) */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200162 int mtu; /* drop packets larger than this */
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200163 int bad_ad; /* inject corrupted ApplicationData record */
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200164 int protect_hvr; /* never drop or delay HelloVerifyRequest */
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200165 int protect_len; /* never drop/delay packet of the given size*/
Hanno Becker77abef52017-11-02 10:50:28 +0000166 unsigned pack; /* merge packets into single datagram for
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100167 * at most \c merge milliseconds if > 0 */
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200168 unsigned int seed; /* seed for "random" events */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200169} opt;
170
171static void exit_usage( const char *name, const char *value )
172{
173 if( value == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 mbedtls_printf( " unknown option or missing value: %s\n", name );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200175 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_printf( " option %s: illegal value: %s\n", name, value );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_printf( USAGE );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200179 exit( 1 );
180}
181
182static void get_options( int argc, char *argv[] )
183{
184 int i;
185 char *p, *q;
186
187 opt.server_addr = DFL_SERVER_ADDR;
188 opt.server_port = DFL_SERVER_PORT;
189 opt.listen_addr = DFL_LISTEN_ADDR;
190 opt.listen_port = DFL_LISTEN_PORT;
Hanno Becker211f44c2017-10-31 14:08:10 +0000191 opt.pack = DFL_PACK;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200192 /* Other members default to 0 */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200193
Hanno Becker01ea7782018-08-17 13:33:41 +0100194 opt.delay_cli_cnt = 0;
195 opt.delay_srv_cnt = 0;
196 memset( opt.delay_cli, 0, sizeof( opt.delay_cli ) );
197 memset( opt.delay_srv, 0, sizeof( opt.delay_srv ) );
198
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200199 for( i = 1; i < argc; i++ )
200 {
201 p = argv[i];
202 if( ( q = strchr( p, '=' ) ) == NULL )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200203 exit_usage( p, NULL );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200204 *q++ = '\0';
205
206 if( strcmp( p, "server_addr" ) == 0 )
207 opt.server_addr = q;
208 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200209 opt.server_port = q;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200210 else if( strcmp( p, "listen_addr" ) == 0 )
211 opt.listen_addr = q;
212 else if( strcmp( p, "listen_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200213 opt.listen_port = q;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200214 else if( strcmp( p, "duplicate" ) == 0 )
215 {
216 opt.duplicate = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200217 if( opt.duplicate < 0 || opt.duplicate > 20 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200218 exit_usage( p, q );
219 }
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200220 else if( strcmp( p, "delay" ) == 0 )
221 {
222 opt.delay = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200223 if( opt.delay < 0 || opt.delay > 20 || opt.delay == 1 )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200224 exit_usage( p, q );
225 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200226 else if( strcmp( p, "delay_ccs" ) == 0 )
227 {
228 opt.delay_ccs = atoi( q );
229 if( opt.delay_ccs < 0 || opt.delay_ccs > 1 )
230 exit_usage( p, q );
231 }
Hanno Becker01ea7782018-08-17 13:33:41 +0100232 else if( strcmp( p, "delay_cli" ) == 0 ||
233 strcmp( p, "delay_srv" ) == 0 )
234 {
235 uint8_t *delay_cnt;
236 char **delay_list;
237 size_t len;
238 char *buf;
239
240 if( strcmp( p, "delay_cli" ) == 0 )
241 {
242 delay_cnt = &opt.delay_cli_cnt;
243 delay_list = opt.delay_cli;
244 }
245 else
246 {
247 delay_cnt = &opt.delay_srv_cnt;
248 delay_list = opt.delay_srv;
249 }
250
251 if( *delay_cnt == MAX_DELAYED_HS )
252 {
Hanno Beckerf34a4c12018-08-28 17:22:26 +0100253 mbedtls_printf( " too many uses of %s: only %d allowed\n",
254 p, MAX_DELAYED_HS );
Hanno Becker01ea7782018-08-17 13:33:41 +0100255 exit_usage( p, NULL );
256 }
257
258 len = strlen( q );
259 buf = mbedtls_calloc( 1, len + 1 );
260 if( buf == NULL )
261 {
262 mbedtls_printf( " Allocation failure\n" );
263 exit( 1 );
264 }
265 memcpy( buf, q, len + 1 );
266
267 delay_list[ (*delay_cnt)++ ] = buf;
268 }
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200269 else if( strcmp( p, "drop" ) == 0 )
270 {
271 opt.drop = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200272 if( opt.drop < 0 || opt.drop > 20 || opt.drop == 1 )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200273 exit_usage( p, q );
274 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100275 else if( strcmp( p, "pack" ) == 0 )
276 {
Hanno Becker0cc77742017-10-31 14:10:07 +0000277#if defined(MBEDTLS_TIMING_C)
Hanno Becker77abef52017-11-02 10:50:28 +0000278 opt.pack = (unsigned) atoi( q );
Hanno Becker0cc77742017-10-31 14:10:07 +0000279#else
280 mbedtls_printf( " option pack only defined if MBEDTLS_TIMING_C is enabled\n" );
281 exit( 1 );
282#endif
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100283 }
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200284 else if( strcmp( p, "mtu" ) == 0 )
285 {
286 opt.mtu = atoi( q );
287 if( opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE )
288 exit_usage( p, q );
289 }
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200290 else if( strcmp( p, "bad_ad" ) == 0 )
291 {
292 opt.bad_ad = atoi( q );
293 if( opt.bad_ad < 0 || opt.bad_ad > 1 )
294 exit_usage( p, q );
295 }
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200296 else if( strcmp( p, "protect_hvr" ) == 0 )
297 {
298 opt.protect_hvr = atoi( q );
299 if( opt.protect_hvr < 0 || opt.protect_hvr > 1 )
300 exit_usage( p, q );
301 }
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200302 else if( strcmp( p, "protect_len" ) == 0 )
303 {
304 opt.protect_len = atoi( q );
305 if( opt.protect_len < 0 )
306 exit_usage( p, q );
307 }
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200308 else if( strcmp( p, "seed" ) == 0 )
309 {
310 opt.seed = atoi( q );
311 if( opt.seed == 0 )
312 exit_usage( p, q );
313 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200314 else
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200315 exit_usage( p, NULL );
316 }
317}
318
319static const char *msg_type( unsigned char *msg, size_t len )
320{
321 if( len < 1 ) return( "Invalid" );
322 switch( msg[0] )
323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" );
325 case MBEDTLS_SSL_MSG_ALERT: return( "Alert" );
326 case MBEDTLS_SSL_MSG_APPLICATION_DATA: return( "ApplicationData" );
327 case MBEDTLS_SSL_MSG_HANDSHAKE: break; /* See below */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200328 default: return( "Unknown" );
329 }
330
Manuel Pégourié-Gonnard8cc7e032014-09-25 12:59:05 +0200331 if( len < 13 + 12 ) return( "Invalid handshake" );
332
333 /*
334 * Our handshake message are less than 2^16 bytes long, so they should
335 * have 0 as the first byte of length, frag_offset and frag_length.
336 * Otherwise, assume they are encrypted.
337 */
338 if( msg[14] || msg[19] || msg[22] ) return( "Encrypted handshake" );
339
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200340 switch( msg[13] )
341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 case MBEDTLS_SSL_HS_HELLO_REQUEST: return( "HelloRequest" );
343 case MBEDTLS_SSL_HS_CLIENT_HELLO: return( "ClientHello" );
344 case MBEDTLS_SSL_HS_SERVER_HELLO: return( "ServerHello" );
345 case MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" );
346 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" );
347 case MBEDTLS_SSL_HS_CERTIFICATE: return( "Certificate" );
348 case MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" );
349 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" );
350 case MBEDTLS_SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" );
351 case MBEDTLS_SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" );
352 case MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" );
353 case MBEDTLS_SSL_HS_FINISHED: return( "Finished" );
Manuel Pégourié-Gonnardbc010a02014-09-20 12:40:51 +0200354 default: return( "Unknown handshake" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200355 }
356}
357
Hanno Becker0cc77742017-10-31 14:10:07 +0000358#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200359/* Return elapsed time in milliseconds since the first call */
Hanno Becker77abef52017-11-02 10:50:28 +0000360static unsigned ellapsed_time( void )
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200361{
Hanno Becker92474da2017-10-31 14:09:30 +0000362 static int initialized = 0;
363 static struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200364
Hanno Becker92474da2017-10-31 14:09:30 +0000365 if( initialized == 0 )
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200366 {
Hanno Becker92474da2017-10-31 14:09:30 +0000367 (void) mbedtls_timing_get_timer( &hires, 1 );
368 initialized = 1;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200369 return( 0 );
370 }
371
Hanno Becker92474da2017-10-31 14:09:30 +0000372 return( mbedtls_timing_get_timer( &hires, 0 ) );
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200373}
374
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200375typedef struct
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200376{
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100377 mbedtls_net_context *ctx;
378
379 const char *description;
380
Hanno Becker77abef52017-11-02 10:50:28 +0000381 unsigned packet_lifetime;
382 unsigned num_datagrams;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100383
384 unsigned char data[MAX_MSG_SIZE];
Hanno Beckera5e68972017-12-06 08:35:02 +0000385 size_t len;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100386
387} ctx_buffer;
388
389static ctx_buffer outbuf[2];
390
391static int ctx_buffer_flush( ctx_buffer *buf )
392{
393 int ret;
394
Hanno Becker77abef52017-11-02 10:50:28 +0000395 mbedtls_printf( " %05u flush %s: %u bytes, %u datagrams, last %u ms\n",
396 ellapsed_time(), buf->description,
Hanno Beckera5e68972017-12-06 08:35:02 +0000397 (unsigned) buf->len, buf->num_datagrams,
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100398 ellapsed_time() - buf->packet_lifetime );
399
400 ret = mbedtls_net_send( buf->ctx, buf->data, buf->len );
401
402 buf->len = 0;
403 buf->num_datagrams = 0;
404
405 return( ret );
406}
407
Hanno Becker77abef52017-11-02 10:50:28 +0000408static unsigned ctx_buffer_time_remaining( ctx_buffer *buf )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100409{
Hanno Becker77abef52017-11-02 10:50:28 +0000410 unsigned const cur_time = ellapsed_time();
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100411
Hanno Becker77abef52017-11-02 10:50:28 +0000412 if( buf->num_datagrams == 0 )
413 return( (unsigned) -1 );
414
415 if( cur_time - buf->packet_lifetime >= opt.pack )
416 return( 0 );
417
418 return( opt.pack - ( cur_time - buf->packet_lifetime ) );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100419}
420
421static int ctx_buffer_append( ctx_buffer *buf,
422 const unsigned char * data,
423 size_t len )
424{
425 int ret;
426
Hanno Beckera5e68972017-12-06 08:35:02 +0000427 if( len > (size_t) INT_MAX )
428 return( -1 );
429
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100430 if( len > sizeof( buf->data ) )
431 {
Hanno Becker77abef52017-11-02 10:50:28 +0000432 mbedtls_printf( " ! buffer size %u too large (max %u)\n",
433 (unsigned) len, (unsigned) sizeof( buf->data ) );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100434 return( -1 );
435 }
436
437 if( sizeof( buf->data ) - buf->len < len )
438 {
439 if( ( ret = ctx_buffer_flush( buf ) ) <= 0 )
440 return( ret );
441 }
442
443 memcpy( buf->data + buf->len, data, len );
444
445 buf->len += len;
446 if( ++buf->num_datagrams == 1 )
447 buf->packet_lifetime = ellapsed_time();
448
Hanno Beckera5e68972017-12-06 08:35:02 +0000449 return( (int) len );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100450}
Hanno Becker77abef52017-11-02 10:50:28 +0000451#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100452
453static int dispatch_data( mbedtls_net_context *ctx,
454 const unsigned char * data,
455 size_t len )
456{
Hanno Becker77abef52017-11-02 10:50:28 +0000457#if defined(MBEDTLS_TIMING_C)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100458 ctx_buffer *buf = NULL;
Hanno Becker77abef52017-11-02 10:50:28 +0000459 if( opt.pack > 0 )
460 {
461 if( outbuf[0].ctx == ctx )
462 buf = &outbuf[0];
463 else if( outbuf[1].ctx == ctx )
464 buf = &outbuf[1];
Hanno Becker0cc77742017-10-31 14:10:07 +0000465
Hanno Becker77abef52017-11-02 10:50:28 +0000466 if( buf == NULL )
467 return( -1 );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100468
Hanno Becker77abef52017-11-02 10:50:28 +0000469 return( ctx_buffer_append( buf, data, len ) );
470 }
471#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100472
Hanno Becker0cc77742017-10-31 14:10:07 +0000473 return( mbedtls_net_send( ctx, data, len ) );
474}
475
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100476typedef struct
477{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200478 mbedtls_net_context *dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200479 const char *way;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200480 const char *type;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200481 unsigned len;
482 unsigned char buf[MAX_MSG_SIZE];
483} packet;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200484
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200485/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */
486void print_packet( const packet *p, const char *why )
487{
Hanno Becker0cc77742017-10-31 14:10:07 +0000488#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200489 if( why == NULL )
Hanno Becker77abef52017-11-02 10:50:28 +0000490 mbedtls_printf( " %05u dispatch %s %s (%u bytes)\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200491 ellapsed_time(), p->way, p->type, p->len );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200492 else
Hanno Becker77abef52017-11-02 10:50:28 +0000493 mbedtls_printf( " %05u dispatch %s %s (%u bytes): %s\n",
Hanno Becker0cc77742017-10-31 14:10:07 +0000494 ellapsed_time(), p->way, p->type, p->len, why );
495#else
496 if( why == NULL )
497 mbedtls_printf( " dispatch %s %s (%u bytes)\n",
498 p->way, p->type, p->len );
499 else
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100500 mbedtls_printf( " dispatch %s %s (%u bytes): %s\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200501 p->way, p->type, p->len, why );
Hanno Becker0cc77742017-10-31 14:10:07 +0000502#endif
503
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200504 fflush( stdout );
505}
506
507int send_packet( const packet *p, const char *why )
508{
509 int ret;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200510 mbedtls_net_context *dst = p->dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200511
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200512 /* insert corrupted ApplicationData record? */
513 if( opt.bad_ad &&
514 strcmp( p->type, "ApplicationData" ) == 0 )
515 {
516 unsigned char buf[MAX_MSG_SIZE];
517 memcpy( buf, p->buf, p->len );
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200518
Hanno Beckerfbb0b702017-05-26 16:55:07 +0100519 if( p->len <= 13 )
520 {
521 mbedtls_printf( " ! can't corrupt empty AD record" );
522 }
523 else
524 {
525 ++buf[13];
526 print_packet( p, "corrupted" );
527 }
528
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100529 if( ( ret = dispatch_data( dst, buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200530 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100531 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200532 return( ret );
533 }
534 }
535
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200536 print_packet( p, why );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100537 if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200538 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100539 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200540 return( ret );
541 }
542
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200543 /* Don't duplicate Application Data, only handshake covered */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200544 if( opt.duplicate != 0 &&
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200545 strcmp( p->type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200546 rand() % opt.duplicate == 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200547 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200548 print_packet( p, "duplicated" );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200549
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100550 if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200551 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100552 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200553 return( ret );
554 }
555 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200556
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200557 return( 0 );
558}
559
Hanno Becker101bcba2018-08-21 16:39:51 +0100560#define MAX_DELAYED_MSG 5
561static size_t prev_len;
562static packet prev[MAX_DELAYED_MSG];
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200563
564void clear_pending( void )
565{
Hanno Becker12b72c12018-08-23 13:15:36 +0100566 memset( &prev, 0, sizeof( prev ) );
Hanno Becker101bcba2018-08-21 16:39:51 +0100567 prev_len = 0;
568}
569
570void delay_packet( packet *delay )
571{
572 if( prev_len == MAX_DELAYED_MSG )
573 return;
574
575 memcpy( &prev[prev_len++], delay, sizeof( packet ) );
576}
577
578int send_delayed()
579{
580 uint8_t offset;
581 int ret;
582 for( offset = 0; offset < prev_len; offset++ )
583 {
584 ret = send_packet( &prev[offset], "delayed" );
585 if( ret != 0 )
586 return( ret );
587 }
588
589 clear_pending();
590 return( 0 );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200591}
592
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200593/*
594 * Avoid dropping or delaying a packet that was already dropped twice: this
595 * only results in uninteresting timeouts. We can't rely on type to identify
596 * packets, since during renegotiation they're all encrypted. So, rely on
597 * size mod 2048 (which is usually just size).
598 */
599static unsigned char dropped[2048] = { 0 };
600#define DROP_MAX 2
601
Hanno Beckerbcf97ec2019-06-04 13:04:28 +0100602/* We only drop packets at the level of entire datagrams, not at the level
603 * of records. In particular, if the peer changes the way it packs multiple
604 * records into a single datagram, we don't necessarily count the number of
605 * times a record has been dropped correctly. However, the only known reason
606 * why a peer would change datagram packing is disabling the latter on
607 * retransmission, in which case we'd drop involved records at most
608 * DROP_MAX + 1 times. */
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200609void update_dropped( const packet *p )
610{
611 size_t id = p->len % sizeof( dropped );
Manuel Pégourié-Gonnardfa60f122014-09-26 16:07:29 +0200612 ++dropped[id];
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200613}
614
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200615int handle_message( const char *way,
616 mbedtls_net_context *dst,
617 mbedtls_net_context *src )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200618{
619 int ret;
620 packet cur;
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200621 size_t id;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200622
Hanno Becker01ea7782018-08-17 13:33:41 +0100623 uint8_t delay_idx;
624 char ** delay_list;
625 uint8_t delay_list_len;
626
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +0200627 /* receive packet */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200628 if( ( ret = mbedtls_net_recv( src, cur.buf, sizeof( cur.buf ) ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 mbedtls_printf( " ! mbedtls_net_recv returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200631 return( ret );
632 }
633
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200634 cur.len = ret;
635 cur.type = msg_type( cur.buf, cur.len );
636 cur.way = way;
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200637 cur.dst = dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200638 print_packet( &cur, NULL );
639
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200640 id = cur.len % sizeof( dropped );
641
Hanno Becker01ea7782018-08-17 13:33:41 +0100642 if( strcmp( way, "S <- C" ) == 0 )
643 {
644 delay_list = opt.delay_cli;
645 delay_list_len = opt.delay_cli_cnt;
646 }
647 else
648 {
649 delay_list = opt.delay_srv;
650 delay_list_len = opt.delay_srv_cnt;
651 }
Hanno Beckercf469452018-08-28 10:09:47 +0100652
Hanno Becker01ea7782018-08-17 13:33:41 +0100653 /* Check if message type is in the list of messages
654 * that should be delayed */
655 for( delay_idx = 0; delay_idx < delay_list_len; delay_idx++ )
656 {
657 if( delay_list[ delay_idx ] == NULL )
658 continue;
659
660 if( strcmp( delay_list[ delay_idx ], cur.type ) == 0 )
661 {
662 /* Delay message */
Hanno Becker101bcba2018-08-21 16:39:51 +0100663 delay_packet( &cur );
Hanno Becker01ea7782018-08-17 13:33:41 +0100664
665 /* Remove entry from list */
666 mbedtls_free( delay_list[delay_idx] );
667 delay_list[delay_idx] = NULL;
668
669 return( 0 );
670 }
671 }
672
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200673 /* do we want to drop, delay, or forward it? */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200674 if( ( opt.mtu != 0 &&
675 cur.len > (unsigned) opt.mtu ) ||
676 ( opt.drop != 0 &&
677 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200678 ! ( opt.protect_hvr &&
679 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200680 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200681 dropped[id] < DROP_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200682 rand() % opt.drop == 0 ) )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200683 {
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200684 update_dropped( &cur );
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200685 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200686 else if( ( opt.delay_ccs == 1 &&
687 strcmp( cur.type, "ChangeCipherSpec" ) == 0 ) ||
688 ( opt.delay != 0 &&
689 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200690 ! ( opt.protect_hvr &&
691 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200692 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200693 dropped[id] < DROP_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200694 rand() % opt.delay == 0 ) )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200695 {
Hanno Becker101bcba2018-08-21 16:39:51 +0100696 delay_packet( &cur );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200697 }
698 else
699 {
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200700 /* forward and possibly duplicate */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200701 if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 )
702 return( ret );
703
Hanno Becker101bcba2018-08-21 16:39:51 +0100704 /* send previously delayed messages if any */
705 ret = send_delayed();
706 if( ret != 0 )
707 return( ret );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200708 }
709
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200710 return( 0 );
711}
712
713int main( int argc, char *argv[] )
714{
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100715 int ret = 1;
716 int exit_code = MBEDTLS_EXIT_FAILURE;
Hanno Becker01ea7782018-08-17 13:33:41 +0100717 uint8_t delay_idx;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200718
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200719 mbedtls_net_context listen_fd, client_fd, server_fd;
Hanno Becker77abef52017-11-02 10:50:28 +0000720
721#if defined( MBEDTLS_TIMING_C )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100722 struct timeval tm;
Hanno Becker77abef52017-11-02 10:50:28 +0000723#endif
724
725 struct timeval *tm_ptr = NULL;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200726
727 int nb_fds;
728 fd_set read_fds;
729
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200730 mbedtls_net_init( &listen_fd );
731 mbedtls_net_init( &client_fd );
732 mbedtls_net_init( &server_fd );
733
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200734 get_options( argc, argv );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200735
736 /*
737 * Decisions to drop/delay/duplicate packets are pseudo-random: dropping
738 * exactly 1 in N packets would lead to problems when a flight has exactly
739 * N packets: the same packet would be dropped on every resend.
740 *
741 * In order to be able to reproduce problems reliably, the seed may be
742 * specified explicitly.
743 */
744 if( opt.seed == 0 )
745 {
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200746 opt.seed = (unsigned int) time( NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 mbedtls_printf( " . Pseudo-random seed: %u\n", opt.seed );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200748 }
749
750 srand( opt.seed );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200751
752 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200753 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200754 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200755 mbedtls_printf( " . Connect to server on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200756 opt.server_addr, opt.server_port );
757 fflush( stdout );
758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
760 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200763 goto exit;
764 }
765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200767
768 /*
769 * 1. Setup the "listening" UDP socket
770 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200771 mbedtls_printf( " . Bind on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200772 opt.listen_addr, opt.listen_port );
773 fflush( stdout );
774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 if( ( ret = mbedtls_net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
776 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200779 goto exit;
780 }
781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200783
784 /*
785 * 2. Wait until a client connects
786 */
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200787accept:
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200788 mbedtls_net_free( &client_fd );
789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 mbedtls_printf( " . Waiting for a remote connection ..." );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200791 fflush( stdout );
792
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200793 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200794 NULL, 0, NULL ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 mbedtls_printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200797 goto exit;
798 }
799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200801
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200802 /*
803 * 3. Forward packets forever (kill the process to terminate it)
804 */
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200805 clear_pending();
806 memset( dropped, 0, sizeof( dropped ) );
807
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200808 nb_fds = client_fd.fd;
809 if( nb_fds < server_fd.fd )
810 nb_fds = server_fd.fd;
811 if( nb_fds < listen_fd.fd )
812 nb_fds = listen_fd.fd;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200813 ++nb_fds;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200814
Hanno Becker0cc77742017-10-31 14:10:07 +0000815#if defined(MBEDTLS_TIMING_C)
Hanno Becker211f44c2017-10-31 14:08:10 +0000816 if( opt.pack > 0 )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100817 {
818 outbuf[0].ctx = &server_fd;
819 outbuf[0].description = "S <- C";
820 outbuf[0].num_datagrams = 0;
821 outbuf[0].len = 0;
822
823 outbuf[1].ctx = &client_fd;
824 outbuf[1].description = "S -> C";
825 outbuf[1].num_datagrams = 0;
826 outbuf[1].len = 0;
827 }
Hanno Becker0cc77742017-10-31 14:10:07 +0000828#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100829
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200830 while( 1 )
831 {
Hanno Becker77abef52017-11-02 10:50:28 +0000832#if defined(MBEDTLS_TIMING_C)
833 if( opt.pack > 0 )
834 {
835 unsigned max_wait_server, max_wait_client, max_wait;
836 max_wait_server = ctx_buffer_time_remaining( &outbuf[0] );
837 max_wait_client = ctx_buffer_time_remaining( &outbuf[1] );
838
839 max_wait = (unsigned) -1;
840
841 if( max_wait_server == 0 )
842 ctx_buffer_flush( &outbuf[0] );
843 else
844 max_wait = max_wait_server;
845
846 if( max_wait_client == 0 )
847 ctx_buffer_flush( &outbuf[1] );
848 else
849 {
850 if( max_wait_client < max_wait )
851 max_wait = max_wait_client;
852 }
853
854 if( max_wait != (unsigned) -1 )
855 {
856 tm.tv_sec = max_wait / 1000;
857 tm.tv_usec = ( max_wait % 1000 ) * 1000;
858
859 tm_ptr = &tm;
860 }
861 else
862 {
863 tm_ptr = NULL;
864 }
865 }
866#endif /* MBEDTLS_TIMING_C */
867
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200868 FD_ZERO( &read_fds );
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200869 FD_SET( server_fd.fd, &read_fds );
870 FD_SET( client_fd.fd, &read_fds );
871 FD_SET( listen_fd.fd, &read_fds );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200872
Hanno Becker77abef52017-11-02 10:50:28 +0000873 if( ( ret = select( nb_fds, &read_fds, NULL, NULL, tm_ptr ) ) < 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200874 {
875 perror( "select" );
876 goto exit;
877 }
878
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200879 if( FD_ISSET( listen_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200880 goto accept;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200881
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200882 if( FD_ISSET( client_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200883 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200884 if( ( ret = handle_message( "S <- C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200885 &server_fd, &client_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200886 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200887 }
888
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200889 if( FD_ISSET( server_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200890 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200891 if( ( ret = handle_message( "S -> C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200892 &client_fd, &server_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200893 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200894 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100895
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200896 }
897
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100898 exit_code = MBEDTLS_EXIT_SUCCESS;
899
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200900exit:
901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902#ifdef MBEDTLS_ERROR_C
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100903 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200904 {
905 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 mbedtls_strerror( ret, error_buf, 100 );
907 mbedtls_printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200908 fflush( stdout );
909 }
910#endif
911
Hanno Becker01ea7782018-08-17 13:33:41 +0100912 for( delay_idx = 0; delay_idx < MAX_DELAYED_HS; delay_idx++ )
913 {
914 mbedtls_free( opt.delay_cli + delay_idx );
915 mbedtls_free( opt.delay_srv + delay_idx );
916 }
917
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200918 mbedtls_net_free( &client_fd );
919 mbedtls_net_free( &server_fd );
920 mbedtls_net_free( &listen_fd );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200921
922#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 mbedtls_printf( " Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200924 fflush( stdout ); getchar();
925#endif
926
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200927 mbedtls_exit( exit_code );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200928}
929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930#endif /* MBEDTLS_NET_C */