Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 1 | /* |
| 2 | * UDP proxy: emulate an unreliable UDP connexion for DTLS testing |
| 3 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * 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é-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 18 | * |
Manuel Pégourié-Gonnard | e4d4890 | 2015-03-06 13:40:52 +0000 | [diff] [blame] | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 20 | */ |
| 21 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 22 | /* |
| 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é-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 29 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 30 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 31 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 32 | #endif |
| 33 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 34 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 35 | #include "mbedtls/platform.h" |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame] | 36 | #else |
Simon Butcher | d3138c3 | 2016-04-27 01:26:50 +0100 | [diff] [blame] | 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <time.h> |
Andres Amaya Garcia | 80081a6 | 2018-04-29 21:58:53 +0100 | [diff] [blame] | 40 | #define mbedtls_time time |
| 41 | #define mbedtls_time_t time_t |
| 42 | #define mbedtls_printf printf |
Hanno Becker | 01ea778 | 2018-08-17 13:33:41 +0100 | [diff] [blame] | 43 | #define mbedtls_calloc calloc |
| 44 | #define mbedtls_free free |
Andrzej Kurek | 825ebd4 | 2020-05-18 11:47:25 -0400 | [diff] [blame] | 45 | #define mbedtls_exit exit |
Andres Amaya Garcia | 7d42965 | 2018-04-30 22:42:33 +0100 | [diff] [blame] | 46 | #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
Andres Amaya Garcia | 80081a6 | 2018-04-29 21:58:53 +0100 | [diff] [blame] | 47 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
| 48 | #endif /* MBEDTLS_PLATFORM_C */ |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame] | 49 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 50 | #if !defined(MBEDTLS_NET_C) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 51 | int main( void ) |
| 52 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 53 | mbedtls_printf( "MBEDTLS_NET_C not defined.\n" ); |
Andrzej Kurek | 825ebd4 | 2020-05-18 11:47:25 -0400 | [diff] [blame] | 54 | mbedtls_exit( 0 ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 55 | } |
| 56 | #else |
| 57 | |
Andres AG | 788aa4a | 2016-09-14 14:32:09 +0100 | [diff] [blame] | 58 | #include "mbedtls/net_sockets.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 59 | #include "mbedtls/error.h" |
| 60 | #include "mbedtls/ssl.h" |
Hanno Becker | 0cc7774 | 2017-10-31 14:10:07 +0000 | [diff] [blame] | 61 | #include "mbedtls/timing.h" |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 62 | |
Manuel Pégourié-Gonnard | d901d17 | 2015-02-16 18:37:53 +0000 | [diff] [blame] | 63 | #include <string.h> |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 64 | |
| 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é-Gonnard | 66491e1 | 2019-10-22 12:50:13 +0200 | [diff] [blame] | 83 | #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT) |
| 84 | int mbedtls_hardware_poll( void *data, unsigned char *output, |
| 85 | size_t len, size_t *olen ) |
| 86 | { |
| 87 | size_t i; |
| 88 | (void) data; |
| 89 | for( i = 0; i < len; ++i ) |
| 90 | output[i] = rand(); |
| 91 | *olen = len; |
| 92 | return( 0 ); |
| 93 | } |
| 94 | #endif |
| 95 | |
Manuel Pégourié-Gonnard | b46780e | 2014-09-23 12:17:30 +0200 | [diff] [blame] | 96 | #define MAX_MSG_SIZE 16384 + 2048 /* max record/datagram size */ |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 97 | |
| 98 | #define DFL_SERVER_ADDR "localhost" |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 99 | #define DFL_SERVER_PORT "4433" |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 100 | #define DFL_LISTEN_ADDR "localhost" |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 101 | #define DFL_LISTEN_PORT "5556" |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 102 | #define DFL_PACK 0 |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 103 | |
Hanno Becker | 0cc7774 | 2017-10-31 14:10:07 +0000 | [diff] [blame] | 104 | #if defined(MBEDTLS_TIMING_C) |
| 105 | #define USAGE_PACK \ |
| 106 | " pack=%%d default: 0 (don't pack)\n" \ |
| 107 | " options: t > 0 (pack for t milliseconds)\n" |
| 108 | #else |
| 109 | #define USAGE_PACK |
| 110 | #endif |
| 111 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 112 | #define USAGE \ |
| 113 | "\n usage: udp_proxy param=<>...\n" \ |
| 114 | "\n acceptable parameters:\n" \ |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 115 | " server_addr=%%s default: localhost\n" \ |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 116 | " server_port=%%d default: 4433\n" \ |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 117 | " listen_addr=%%s default: localhost\n" \ |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 118 | " listen_port=%%d default: 4433\n" \ |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 119 | "\n" \ |
| 120 | " duplicate=%%d default: 0 (no duplication)\n" \ |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 121 | " duplicate about 1:N packets randomly\n" \ |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 122 | " delay=%%d default: 0 (no delayed packets)\n" \ |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 123 | " delay about 1:N packets randomly\n" \ |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 124 | " delay_ccs=0/1 default: 0 (don't delay ChangeCipherSpec)\n" \ |
Hanno Becker | 01ea778 | 2018-08-17 13:33:41 +0100 | [diff] [blame] | 125 | " delay_cli=%%s Handshake message from client that should be\n"\ |
| 126 | " delayed. Possible values are 'ClientHello',\n" \ |
| 127 | " 'Certificate', 'CertificateVerify', and\n" \ |
| 128 | " 'ClientKeyExchange'.\n" \ |
| 129 | " May be used multiple times, even for the same\n"\ |
| 130 | " message, in which case the respective message\n"\ |
| 131 | " gets delayed multiple times.\n" \ |
| 132 | " delay_srv=%%s Handshake message from server that should be\n"\ |
| 133 | " delayed. Possible values are 'HelloRequest',\n"\ |
| 134 | " 'ServerHello', 'ServerHelloDone', 'Certificate'\n"\ |
| 135 | " 'ServerKeyExchange', 'NewSessionTicket',\n"\ |
| 136 | " 'HelloVerifyRequest' and ''CertificateRequest'.\n"\ |
| 137 | " May be used multiple times, even for the same\n"\ |
| 138 | " message, in which case the respective message\n"\ |
| 139 | " gets delayed multiple times.\n" \ |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 140 | " drop=%%d default: 0 (no dropped packets)\n" \ |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 141 | " drop about 1:N packets randomly\n" \ |
Manuel Pégourié-Gonnard | eb00bfd | 2014-09-08 11:11:42 +0200 | [diff] [blame] | 142 | " mtu=%%d default: 0 (unlimited)\n" \ |
| 143 | " drop packets larger than N bytes\n" \ |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 144 | " bad_ad=0/1 default: 0 (don't add bad ApplicationData)\n" \ |
Hanno Becker | 34dcf4e | 2019-05-24 10:07:42 +0100 | [diff] [blame] | 145 | " bad_cid=%%d default: 0 (don't corrupt Connection IDs)\n" \ |
| 146 | " duplicate 1:N packets containing a CID,\n" \ |
| 147 | " modifying CID in first instance of the packet.\n" \ |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 148 | " protect_hvr=0/1 default: 0 (don't protect HelloVerifyRequest)\n" \ |
Hanno Becker | 0cc7774 | 2017-10-31 14:10:07 +0000 | [diff] [blame] | 149 | " protect_len=%%d default: (don't protect packets of this size)\n" \ |
Andrzej Kurek | 825ebd4 | 2020-05-18 11:47:25 -0400 | [diff] [blame] | 150 | " inject_clihlo=0/1 default: 0 (don't inject fake ClientHello)\n" \ |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 151 | "\n" \ |
| 152 | " seed=%%d default: (use current time)\n" \ |
Hanno Becker | 0cc7774 | 2017-10-31 14:10:07 +0000 | [diff] [blame] | 153 | USAGE_PACK \ |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 154 | "\n" |
| 155 | |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 156 | /* |
| 157 | * global options |
| 158 | */ |
Hanno Becker | 01ea778 | 2018-08-17 13:33:41 +0100 | [diff] [blame] | 159 | |
| 160 | #define MAX_DELAYED_HS 10 |
| 161 | |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 162 | static struct options |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 163 | { |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 164 | const char *server_addr; /* address to forward packets to */ |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 165 | const char *server_port; /* port to forward packets to */ |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 166 | const char *listen_addr; /* address for accepting client connections */ |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 167 | const char *listen_port; /* port for accepting client connections */ |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 168 | |
| 169 | int duplicate; /* duplicate 1 in N packets (none if 0) */ |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 170 | int delay; /* delay 1 packet in N (none if 0) */ |
Manuel Pégourié-Gonnard | 81f2fe9 | 2014-09-08 10:44:57 +0200 | [diff] [blame] | 171 | int delay_ccs; /* delay ChangeCipherSpec */ |
Hanno Becker | 01ea778 | 2018-08-17 13:33:41 +0100 | [diff] [blame] | 172 | char* delay_cli[MAX_DELAYED_HS]; /* handshake types of messages from |
Hanno Becker | 4103810 | 2018-08-28 11:15:32 +0100 | [diff] [blame] | 173 | * client that should be delayed. */ |
Hanno Becker | 01ea778 | 2018-08-17 13:33:41 +0100 | [diff] [blame] | 174 | uint8_t delay_cli_cnt; /* Number of entries in delay_cli. */ |
| 175 | char* delay_srv[MAX_DELAYED_HS]; /* handshake types of messages from |
Hanno Becker | 4103810 | 2018-08-28 11:15:32 +0100 | [diff] [blame] | 176 | * server that should be delayed. */ |
Hanno Becker | 01ea778 | 2018-08-17 13:33:41 +0100 | [diff] [blame] | 177 | uint8_t delay_srv_cnt; /* Number of entries in delay_srv. */ |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 178 | int drop; /* drop 1 packet in N (none if 0) */ |
Manuel Pégourié-Gonnard | eb00bfd | 2014-09-08 11:11:42 +0200 | [diff] [blame] | 179 | int mtu; /* drop packets larger than this */ |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 180 | int bad_ad; /* inject corrupted ApplicationData record */ |
Hanno Becker | 34dcf4e | 2019-05-24 10:07:42 +0100 | [diff] [blame] | 181 | unsigned bad_cid; /* inject corrupted CID record */ |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 182 | int protect_hvr; /* never drop or delay HelloVerifyRequest */ |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 183 | int protect_len; /* never drop/delay packet of the given size*/ |
Andrzej Kurek | 825ebd4 | 2020-05-18 11:47:25 -0400 | [diff] [blame] | 184 | int inject_clihlo; /* inject fake ClientHello after handshake */ |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 185 | unsigned pack; /* merge packets into single datagram for |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 186 | * at most \c merge milliseconds if > 0 */ |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 187 | unsigned int seed; /* seed for "random" events */ |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 188 | } opt; |
| 189 | |
| 190 | static void exit_usage( const char *name, const char *value ) |
| 191 | { |
| 192 | if( value == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 193 | mbedtls_printf( " unknown option or missing value: %s\n", name ); |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 194 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 195 | mbedtls_printf( " option %s: illegal value: %s\n", name, value ); |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 196 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 197 | mbedtls_printf( USAGE ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 198 | exit( 1 ); |
| 199 | } |
| 200 | |
| 201 | static void get_options( int argc, char *argv[] ) |
| 202 | { |
| 203 | int i; |
| 204 | char *p, *q; |
| 205 | |
| 206 | opt.server_addr = DFL_SERVER_ADDR; |
| 207 | opt.server_port = DFL_SERVER_PORT; |
| 208 | opt.listen_addr = DFL_LISTEN_ADDR; |
| 209 | opt.listen_port = DFL_LISTEN_PORT; |
Hanno Becker | 211f44c | 2017-10-31 14:08:10 +0000 | [diff] [blame] | 210 | opt.pack = DFL_PACK; |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 211 | /* Other members default to 0 */ |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 212 | |
Hanno Becker | 01ea778 | 2018-08-17 13:33:41 +0100 | [diff] [blame] | 213 | opt.delay_cli_cnt = 0; |
| 214 | opt.delay_srv_cnt = 0; |
| 215 | memset( opt.delay_cli, 0, sizeof( opt.delay_cli ) ); |
| 216 | memset( opt.delay_srv, 0, sizeof( opt.delay_srv ) ); |
| 217 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 218 | for( i = 1; i < argc; i++ ) |
| 219 | { |
| 220 | p = argv[i]; |
| 221 | if( ( q = strchr( p, '=' ) ) == NULL ) |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 222 | exit_usage( p, NULL ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 223 | *q++ = '\0'; |
| 224 | |
| 225 | if( strcmp( p, "server_addr" ) == 0 ) |
| 226 | opt.server_addr = q; |
| 227 | else if( strcmp( p, "server_port" ) == 0 ) |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 228 | opt.server_port = q; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 229 | else if( strcmp( p, "listen_addr" ) == 0 ) |
| 230 | opt.listen_addr = q; |
| 231 | else if( strcmp( p, "listen_port" ) == 0 ) |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 232 | opt.listen_port = q; |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 233 | else if( strcmp( p, "duplicate" ) == 0 ) |
| 234 | { |
| 235 | opt.duplicate = atoi( q ); |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 236 | if( opt.duplicate < 0 || opt.duplicate > 20 ) |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 237 | exit_usage( p, q ); |
| 238 | } |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 239 | else if( strcmp( p, "delay" ) == 0 ) |
| 240 | { |
| 241 | opt.delay = atoi( q ); |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 242 | if( opt.delay < 0 || opt.delay > 20 || opt.delay == 1 ) |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 243 | exit_usage( p, q ); |
| 244 | } |
Manuel Pégourié-Gonnard | 81f2fe9 | 2014-09-08 10:44:57 +0200 | [diff] [blame] | 245 | else if( strcmp( p, "delay_ccs" ) == 0 ) |
| 246 | { |
| 247 | opt.delay_ccs = atoi( q ); |
| 248 | if( opt.delay_ccs < 0 || opt.delay_ccs > 1 ) |
| 249 | exit_usage( p, q ); |
| 250 | } |
Hanno Becker | 01ea778 | 2018-08-17 13:33:41 +0100 | [diff] [blame] | 251 | else if( strcmp( p, "delay_cli" ) == 0 || |
| 252 | strcmp( p, "delay_srv" ) == 0 ) |
| 253 | { |
| 254 | uint8_t *delay_cnt; |
| 255 | char **delay_list; |
| 256 | size_t len; |
| 257 | char *buf; |
| 258 | |
| 259 | if( strcmp( p, "delay_cli" ) == 0 ) |
| 260 | { |
| 261 | delay_cnt = &opt.delay_cli_cnt; |
| 262 | delay_list = opt.delay_cli; |
| 263 | } |
| 264 | else |
| 265 | { |
| 266 | delay_cnt = &opt.delay_srv_cnt; |
| 267 | delay_list = opt.delay_srv; |
| 268 | } |
| 269 | |
| 270 | if( *delay_cnt == MAX_DELAYED_HS ) |
| 271 | { |
Hanno Becker | f34a4c1 | 2018-08-28 17:22:26 +0100 | [diff] [blame] | 272 | mbedtls_printf( " too many uses of %s: only %d allowed\n", |
| 273 | p, MAX_DELAYED_HS ); |
Hanno Becker | 01ea778 | 2018-08-17 13:33:41 +0100 | [diff] [blame] | 274 | exit_usage( p, NULL ); |
| 275 | } |
| 276 | |
| 277 | len = strlen( q ); |
| 278 | buf = mbedtls_calloc( 1, len + 1 ); |
| 279 | if( buf == NULL ) |
| 280 | { |
| 281 | mbedtls_printf( " Allocation failure\n" ); |
| 282 | exit( 1 ); |
| 283 | } |
| 284 | memcpy( buf, q, len + 1 ); |
| 285 | |
| 286 | delay_list[ (*delay_cnt)++ ] = buf; |
| 287 | } |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 288 | else if( strcmp( p, "drop" ) == 0 ) |
| 289 | { |
| 290 | opt.drop = atoi( q ); |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 291 | if( opt.drop < 0 || opt.drop > 20 || opt.drop == 1 ) |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 292 | exit_usage( p, q ); |
| 293 | } |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 294 | else if( strcmp( p, "pack" ) == 0 ) |
| 295 | { |
Hanno Becker | 0cc7774 | 2017-10-31 14:10:07 +0000 | [diff] [blame] | 296 | #if defined(MBEDTLS_TIMING_C) |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 297 | opt.pack = (unsigned) atoi( q ); |
Hanno Becker | 0cc7774 | 2017-10-31 14:10:07 +0000 | [diff] [blame] | 298 | #else |
| 299 | mbedtls_printf( " option pack only defined if MBEDTLS_TIMING_C is enabled\n" ); |
| 300 | exit( 1 ); |
| 301 | #endif |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 302 | } |
Manuel Pégourié-Gonnard | eb00bfd | 2014-09-08 11:11:42 +0200 | [diff] [blame] | 303 | else if( strcmp( p, "mtu" ) == 0 ) |
| 304 | { |
| 305 | opt.mtu = atoi( q ); |
| 306 | if( opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE ) |
| 307 | exit_usage( p, q ); |
| 308 | } |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 309 | else if( strcmp( p, "bad_ad" ) == 0 ) |
| 310 | { |
| 311 | opt.bad_ad = atoi( q ); |
| 312 | if( opt.bad_ad < 0 || opt.bad_ad > 1 ) |
| 313 | exit_usage( p, q ); |
| 314 | } |
Hanno Becker | 34dcf4e | 2019-05-24 10:07:42 +0100 | [diff] [blame] | 315 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 316 | else if( strcmp( p, "bad_cid" ) == 0 ) |
| 317 | { |
| 318 | opt.bad_cid = (unsigned) atoi( q ); |
| 319 | } |
| 320 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 321 | else if( strcmp( p, "protect_hvr" ) == 0 ) |
| 322 | { |
| 323 | opt.protect_hvr = atoi( q ); |
| 324 | if( opt.protect_hvr < 0 || opt.protect_hvr > 1 ) |
| 325 | exit_usage( p, q ); |
| 326 | } |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 327 | else if( strcmp( p, "protect_len" ) == 0 ) |
| 328 | { |
| 329 | opt.protect_len = atoi( q ); |
| 330 | if( opt.protect_len < 0 ) |
| 331 | exit_usage( p, q ); |
| 332 | } |
Andrzej Kurek | 825ebd4 | 2020-05-18 11:47:25 -0400 | [diff] [blame] | 333 | else if( strcmp( p, "inject_clihlo" ) == 0 ) |
| 334 | { |
| 335 | opt.inject_clihlo = atoi( q ); |
| 336 | if( opt.inject_clihlo < 0 || opt.inject_clihlo > 1 ) |
| 337 | exit_usage( p, q ); |
| 338 | } |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 339 | else if( strcmp( p, "seed" ) == 0 ) |
| 340 | { |
| 341 | opt.seed = atoi( q ); |
| 342 | if( opt.seed == 0 ) |
| 343 | exit_usage( p, q ); |
| 344 | } |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 345 | else |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 346 | exit_usage( p, NULL ); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | static const char *msg_type( unsigned char *msg, size_t len ) |
| 351 | { |
| 352 | if( len < 1 ) return( "Invalid" ); |
| 353 | switch( msg[0] ) |
| 354 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 355 | case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" ); |
| 356 | case MBEDTLS_SSL_MSG_ALERT: return( "Alert" ); |
| 357 | case MBEDTLS_SSL_MSG_APPLICATION_DATA: return( "ApplicationData" ); |
Hanno Becker | 6c4bc14 | 2019-05-08 15:36:31 +0100 | [diff] [blame] | 358 | case MBEDTLS_SSL_MSG_CID: return( "CID" ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 359 | case MBEDTLS_SSL_MSG_HANDSHAKE: break; /* See below */ |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 360 | default: return( "Unknown" ); |
| 361 | } |
| 362 | |
Manuel Pégourié-Gonnard | 8cc7e03 | 2014-09-25 12:59:05 +0200 | [diff] [blame] | 363 | if( len < 13 + 12 ) return( "Invalid handshake" ); |
| 364 | |
| 365 | /* |
| 366 | * Our handshake message are less than 2^16 bytes long, so they should |
| 367 | * have 0 as the first byte of length, frag_offset and frag_length. |
| 368 | * Otherwise, assume they are encrypted. |
| 369 | */ |
| 370 | if( msg[14] || msg[19] || msg[22] ) return( "Encrypted handshake" ); |
| 371 | |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 372 | switch( msg[13] ) |
| 373 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 374 | case MBEDTLS_SSL_HS_HELLO_REQUEST: return( "HelloRequest" ); |
| 375 | case MBEDTLS_SSL_HS_CLIENT_HELLO: return( "ClientHello" ); |
| 376 | case MBEDTLS_SSL_HS_SERVER_HELLO: return( "ServerHello" ); |
| 377 | case MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" ); |
| 378 | case MBEDTLS_SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" ); |
| 379 | case MBEDTLS_SSL_HS_CERTIFICATE: return( "Certificate" ); |
| 380 | case MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" ); |
| 381 | case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" ); |
| 382 | case MBEDTLS_SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" ); |
| 383 | case MBEDTLS_SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" ); |
| 384 | case MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" ); |
| 385 | case MBEDTLS_SSL_HS_FINISHED: return( "Finished" ); |
Manuel Pégourié-Gonnard | bc010a0 | 2014-09-20 12:40:51 +0200 | [diff] [blame] | 386 | default: return( "Unknown handshake" ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
Hanno Becker | 0cc7774 | 2017-10-31 14:10:07 +0000 | [diff] [blame] | 390 | #if defined(MBEDTLS_TIMING_C) |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 391 | /* Return elapsed time in milliseconds since the first call */ |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 392 | static unsigned ellapsed_time( void ) |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 393 | { |
Hanno Becker | 92474da | 2017-10-31 14:09:30 +0000 | [diff] [blame] | 394 | static int initialized = 0; |
| 395 | static struct mbedtls_timing_hr_time hires; |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 396 | |
Hanno Becker | 92474da | 2017-10-31 14:09:30 +0000 | [diff] [blame] | 397 | if( initialized == 0 ) |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 398 | { |
Hanno Becker | 92474da | 2017-10-31 14:09:30 +0000 | [diff] [blame] | 399 | (void) mbedtls_timing_get_timer( &hires, 1 ); |
| 400 | initialized = 1; |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 401 | return( 0 ); |
| 402 | } |
| 403 | |
Hanno Becker | 92474da | 2017-10-31 14:09:30 +0000 | [diff] [blame] | 404 | return( mbedtls_timing_get_timer( &hires, 0 ) ); |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 405 | } |
| 406 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 407 | typedef struct |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 408 | { |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 409 | mbedtls_net_context *ctx; |
| 410 | |
| 411 | const char *description; |
| 412 | |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 413 | unsigned packet_lifetime; |
| 414 | unsigned num_datagrams; |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 415 | |
| 416 | unsigned char data[MAX_MSG_SIZE]; |
Hanno Becker | a5e6897 | 2017-12-06 08:35:02 +0000 | [diff] [blame] | 417 | size_t len; |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 418 | |
| 419 | } ctx_buffer; |
| 420 | |
| 421 | static ctx_buffer outbuf[2]; |
| 422 | |
| 423 | static int ctx_buffer_flush( ctx_buffer *buf ) |
| 424 | { |
| 425 | int ret; |
| 426 | |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 427 | mbedtls_printf( " %05u flush %s: %u bytes, %u datagrams, last %u ms\n", |
| 428 | ellapsed_time(), buf->description, |
Hanno Becker | a5e6897 | 2017-12-06 08:35:02 +0000 | [diff] [blame] | 429 | (unsigned) buf->len, buf->num_datagrams, |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 430 | ellapsed_time() - buf->packet_lifetime ); |
| 431 | |
| 432 | ret = mbedtls_net_send( buf->ctx, buf->data, buf->len ); |
| 433 | |
| 434 | buf->len = 0; |
| 435 | buf->num_datagrams = 0; |
| 436 | |
| 437 | return( ret ); |
| 438 | } |
| 439 | |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 440 | static unsigned ctx_buffer_time_remaining( ctx_buffer *buf ) |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 441 | { |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 442 | unsigned const cur_time = ellapsed_time(); |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 443 | |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 444 | if( buf->num_datagrams == 0 ) |
| 445 | return( (unsigned) -1 ); |
| 446 | |
| 447 | if( cur_time - buf->packet_lifetime >= opt.pack ) |
| 448 | return( 0 ); |
| 449 | |
| 450 | return( opt.pack - ( cur_time - buf->packet_lifetime ) ); |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | static int ctx_buffer_append( ctx_buffer *buf, |
| 454 | const unsigned char * data, |
| 455 | size_t len ) |
| 456 | { |
| 457 | int ret; |
| 458 | |
Hanno Becker | a5e6897 | 2017-12-06 08:35:02 +0000 | [diff] [blame] | 459 | if( len > (size_t) INT_MAX ) |
| 460 | return( -1 ); |
| 461 | |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 462 | if( len > sizeof( buf->data ) ) |
| 463 | { |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 464 | mbedtls_printf( " ! buffer size %u too large (max %u)\n", |
| 465 | (unsigned) len, (unsigned) sizeof( buf->data ) ); |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 466 | return( -1 ); |
| 467 | } |
| 468 | |
| 469 | if( sizeof( buf->data ) - buf->len < len ) |
| 470 | { |
| 471 | if( ( ret = ctx_buffer_flush( buf ) ) <= 0 ) |
Hanno Becker | 6c4bc14 | 2019-05-08 15:36:31 +0100 | [diff] [blame] | 472 | { |
| 473 | mbedtls_printf( "ctx_buffer_flush failed with -%#04x", -ret ); |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 474 | return( ret ); |
Hanno Becker | 6c4bc14 | 2019-05-08 15:36:31 +0100 | [diff] [blame] | 475 | } |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | memcpy( buf->data + buf->len, data, len ); |
| 479 | |
| 480 | buf->len += len; |
| 481 | if( ++buf->num_datagrams == 1 ) |
| 482 | buf->packet_lifetime = ellapsed_time(); |
| 483 | |
Hanno Becker | a5e6897 | 2017-12-06 08:35:02 +0000 | [diff] [blame] | 484 | return( (int) len ); |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 485 | } |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 486 | #endif /* MBEDTLS_TIMING_C */ |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 487 | |
| 488 | static int dispatch_data( mbedtls_net_context *ctx, |
| 489 | const unsigned char * data, |
| 490 | size_t len ) |
| 491 | { |
Hanno Becker | 6c4bc14 | 2019-05-08 15:36:31 +0100 | [diff] [blame] | 492 | int ret; |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 493 | #if defined(MBEDTLS_TIMING_C) |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 494 | ctx_buffer *buf = NULL; |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 495 | if( opt.pack > 0 ) |
| 496 | { |
| 497 | if( outbuf[0].ctx == ctx ) |
| 498 | buf = &outbuf[0]; |
| 499 | else if( outbuf[1].ctx == ctx ) |
| 500 | buf = &outbuf[1]; |
Hanno Becker | 0cc7774 | 2017-10-31 14:10:07 +0000 | [diff] [blame] | 501 | |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 502 | if( buf == NULL ) |
| 503 | return( -1 ); |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 504 | |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 505 | return( ctx_buffer_append( buf, data, len ) ); |
| 506 | } |
| 507 | #endif /* MBEDTLS_TIMING_C */ |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 508 | |
Hanno Becker | 6c4bc14 | 2019-05-08 15:36:31 +0100 | [diff] [blame] | 509 | ret = mbedtls_net_send( ctx, data, len ); |
| 510 | if( ret < 0 ) |
| 511 | { |
| 512 | mbedtls_printf( "net_send returned -%#04x\n", -ret ); |
| 513 | } |
| 514 | return( ret ); |
Hanno Becker | 0cc7774 | 2017-10-31 14:10:07 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 517 | typedef struct |
| 518 | { |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 519 | mbedtls_net_context *dst; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 520 | const char *way; |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 521 | const char *type; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 522 | unsigned len; |
| 523 | unsigned char buf[MAX_MSG_SIZE]; |
| 524 | } packet; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 525 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 526 | /* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */ |
| 527 | void print_packet( const packet *p, const char *why ) |
| 528 | { |
Hanno Becker | 0cc7774 | 2017-10-31 14:10:07 +0000 | [diff] [blame] | 529 | #if defined(MBEDTLS_TIMING_C) |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 530 | if( why == NULL ) |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 531 | mbedtls_printf( " %05u dispatch %s %s (%u bytes)\n", |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 532 | ellapsed_time(), p->way, p->type, p->len ); |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 533 | else |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 534 | mbedtls_printf( " %05u dispatch %s %s (%u bytes): %s\n", |
Hanno Becker | 0cc7774 | 2017-10-31 14:10:07 +0000 | [diff] [blame] | 535 | ellapsed_time(), p->way, p->type, p->len, why ); |
| 536 | #else |
| 537 | if( why == NULL ) |
| 538 | mbedtls_printf( " dispatch %s %s (%u bytes)\n", |
| 539 | p->way, p->type, p->len ); |
| 540 | else |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 541 | mbedtls_printf( " dispatch %s %s (%u bytes): %s\n", |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 542 | p->way, p->type, p->len, why ); |
Hanno Becker | 0cc7774 | 2017-10-31 14:10:07 +0000 | [diff] [blame] | 543 | #endif |
| 544 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 545 | fflush( stdout ); |
| 546 | } |
| 547 | |
Andrzej Kurek | 825ebd4 | 2020-05-18 11:47:25 -0400 | [diff] [blame] | 548 | /* |
| 549 | * In order to test the server's behaviour when receiving a ClientHello after |
| 550 | * the connection is established (this could be a hard reset from the client, |
| 551 | * but the server must not drop the existing connection before establishing |
| 552 | * client reachability, see RFC 6347 Section 4.2.8), we memorize the first |
| 553 | * ClientHello we see (which can't have a cookie), then replay it after the |
| 554 | * first ApplicationData record - then we're done. |
| 555 | * |
| 556 | * This is controlled by the inject_clihlo option. |
| 557 | * |
| 558 | * We want an explicit state and a place to store the packet. |
| 559 | */ |
| 560 | typedef enum { |
| 561 | ICH_INIT, /* haven't seen the first ClientHello yet */ |
| 562 | ICH_CACHED, /* cached the initial ClientHello */ |
| 563 | ICH_INJECTED, /* ClientHello already injected, done */ |
| 564 | } inject_clihlo_state_t; |
| 565 | |
| 566 | static inject_clihlo_state_t inject_clihlo_state; |
| 567 | static packet initial_clihlo; |
| 568 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 569 | int send_packet( const packet *p, const char *why ) |
| 570 | { |
| 571 | int ret; |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 572 | mbedtls_net_context *dst = p->dst; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 573 | |
Andrzej Kurek | 825ebd4 | 2020-05-18 11:47:25 -0400 | [diff] [blame] | 574 | /* save initial ClientHello? */ |
| 575 | if( opt.inject_clihlo != 0 && |
| 576 | inject_clihlo_state == ICH_INIT && |
| 577 | strcmp( p->type, "ClientHello" ) == 0 ) |
| 578 | { |
| 579 | memcpy( &initial_clihlo, p, sizeof( packet ) ); |
| 580 | inject_clihlo_state = ICH_CACHED; |
| 581 | } |
| 582 | |
Hanno Becker | 34dcf4e | 2019-05-24 10:07:42 +0100 | [diff] [blame] | 583 | /* insert corrupted CID record? */ |
| 584 | if( opt.bad_cid != 0 && |
| 585 | strcmp( p->type, "CID" ) == 0 && |
| 586 | ( rand() % opt.bad_cid ) == 0 ) |
| 587 | { |
| 588 | unsigned char buf[MAX_MSG_SIZE]; |
| 589 | memcpy( buf, p->buf, p->len ); |
| 590 | |
| 591 | /* The CID resides at offset 11 in the DTLS record header. */ |
| 592 | buf[11] ^= 1; |
| 593 | print_packet( p, "modified CID" ); |
| 594 | |
| 595 | if( ( ret = dispatch_data( dst, buf, p->len ) ) <= 0 ) |
| 596 | { |
| 597 | mbedtls_printf( " ! dispatch returned %d\n", ret ); |
| 598 | return( ret ); |
| 599 | } |
| 600 | } |
| 601 | |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 602 | /* insert corrupted ApplicationData record? */ |
| 603 | if( opt.bad_ad && |
| 604 | strcmp( p->type, "ApplicationData" ) == 0 ) |
| 605 | { |
| 606 | unsigned char buf[MAX_MSG_SIZE]; |
| 607 | memcpy( buf, p->buf, p->len ); |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 608 | |
Hanno Becker | fbb0b70 | 2017-05-26 16:55:07 +0100 | [diff] [blame] | 609 | if( p->len <= 13 ) |
| 610 | { |
| 611 | mbedtls_printf( " ! can't corrupt empty AD record" ); |
| 612 | } |
| 613 | else |
| 614 | { |
| 615 | ++buf[13]; |
| 616 | print_packet( p, "corrupted" ); |
| 617 | } |
| 618 | |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 619 | if( ( ret = dispatch_data( dst, buf, p->len ) ) <= 0 ) |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 620 | { |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 621 | mbedtls_printf( " ! dispatch returned %d\n", ret ); |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 622 | return( ret ); |
| 623 | } |
| 624 | } |
| 625 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 626 | print_packet( p, why ); |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 627 | if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 628 | { |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 629 | mbedtls_printf( " ! dispatch returned %d\n", ret ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 630 | return( ret ); |
| 631 | } |
| 632 | |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 633 | /* Don't duplicate Application Data, only handshake covered */ |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 634 | if( opt.duplicate != 0 && |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 635 | strcmp( p->type, "ApplicationData" ) != 0 && |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 636 | rand() % opt.duplicate == 0 ) |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 637 | { |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 638 | print_packet( p, "duplicated" ); |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 639 | |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 640 | if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 ) |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 641 | { |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 642 | mbedtls_printf( " ! dispatch returned %d\n", ret ); |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 643 | return( ret ); |
| 644 | } |
| 645 | } |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 646 | |
Andrzej Kurek | 825ebd4 | 2020-05-18 11:47:25 -0400 | [diff] [blame] | 647 | /* Inject ClientHello after first ApplicationData */ |
| 648 | if( opt.inject_clihlo != 0 && |
| 649 | inject_clihlo_state == ICH_CACHED && |
| 650 | strcmp( p->type, "ApplicationData" ) == 0 ) |
| 651 | { |
| 652 | print_packet( &initial_clihlo, "injected" ); |
| 653 | |
| 654 | if( ( ret = dispatch_data( dst, initial_clihlo.buf, |
| 655 | initial_clihlo.len ) ) <= 0 ) |
| 656 | { |
| 657 | mbedtls_printf( " ! dispatch returned %d\n", ret ); |
| 658 | return( ret ); |
| 659 | } |
| 660 | |
| 661 | inject_clihlo_state = ICH_INJECTED; |
| 662 | } |
| 663 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 664 | return( 0 ); |
| 665 | } |
| 666 | |
Hanno Becker | 101bcba | 2018-08-21 16:39:51 +0100 | [diff] [blame] | 667 | #define MAX_DELAYED_MSG 5 |
| 668 | static size_t prev_len; |
| 669 | static packet prev[MAX_DELAYED_MSG]; |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 670 | |
| 671 | void clear_pending( void ) |
| 672 | { |
Hanno Becker | 12b72c1 | 2018-08-23 13:15:36 +0100 | [diff] [blame] | 673 | memset( &prev, 0, sizeof( prev ) ); |
Hanno Becker | 101bcba | 2018-08-21 16:39:51 +0100 | [diff] [blame] | 674 | prev_len = 0; |
| 675 | } |
| 676 | |
| 677 | void delay_packet( packet *delay ) |
| 678 | { |
| 679 | if( prev_len == MAX_DELAYED_MSG ) |
| 680 | return; |
| 681 | |
| 682 | memcpy( &prev[prev_len++], delay, sizeof( packet ) ); |
| 683 | } |
| 684 | |
| 685 | int send_delayed() |
| 686 | { |
| 687 | uint8_t offset; |
| 688 | int ret; |
| 689 | for( offset = 0; offset < prev_len; offset++ ) |
| 690 | { |
| 691 | ret = send_packet( &prev[offset], "delayed" ); |
| 692 | if( ret != 0 ) |
| 693 | return( ret ); |
| 694 | } |
| 695 | |
| 696 | clear_pending(); |
| 697 | return( 0 ); |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 698 | } |
| 699 | |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 700 | /* |
| 701 | * Avoid dropping or delaying a packet that was already dropped twice: this |
| 702 | * only results in uninteresting timeouts. We can't rely on type to identify |
| 703 | * packets, since during renegotiation they're all encrypted. So, rely on |
| 704 | * size mod 2048 (which is usually just size). |
| 705 | */ |
| 706 | static unsigned char dropped[2048] = { 0 }; |
| 707 | #define DROP_MAX 2 |
| 708 | |
Hanno Becker | 08c78a2 | 2019-06-04 13:04:28 +0100 | [diff] [blame] | 709 | /* We only drop packets at the level of entire datagrams, not at the level |
| 710 | * of records. In particular, if the peer changes the way it packs multiple |
| 711 | * records into a single datagram, we don't necessarily count the number of |
| 712 | * times a record has been dropped correctly. However, the only known reason |
| 713 | * why a peer would change datagram packing is disabling the latter on |
| 714 | * retransmission, in which case we'd drop involved records at most |
| 715 | * DROP_MAX + 1 times. */ |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 716 | void update_dropped( const packet *p ) |
| 717 | { |
| 718 | size_t id = p->len % sizeof( dropped ); |
Manuel Pégourié-Gonnard | fa60f12 | 2014-09-26 16:07:29 +0200 | [diff] [blame] | 719 | ++dropped[id]; |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 720 | } |
| 721 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 722 | int handle_message( const char *way, |
| 723 | mbedtls_net_context *dst, |
| 724 | mbedtls_net_context *src ) |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 725 | { |
| 726 | int ret; |
| 727 | packet cur; |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 728 | size_t id; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 729 | |
Hanno Becker | 01ea778 | 2018-08-17 13:33:41 +0100 | [diff] [blame] | 730 | uint8_t delay_idx; |
| 731 | char ** delay_list; |
| 732 | uint8_t delay_list_len; |
| 733 | |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 734 | /* receive packet */ |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 735 | if( ( ret = mbedtls_net_recv( src, cur.buf, sizeof( cur.buf ) ) ) <= 0 ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 736 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 737 | mbedtls_printf( " ! mbedtls_net_recv returned %d\n", ret ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 738 | return( ret ); |
| 739 | } |
| 740 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 741 | cur.len = ret; |
| 742 | cur.type = msg_type( cur.buf, cur.len ); |
| 743 | cur.way = way; |
Manuel Pégourié-Gonnard | 6265d30 | 2014-09-24 17:42:09 +0200 | [diff] [blame] | 744 | cur.dst = dst; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 745 | print_packet( &cur, NULL ); |
| 746 | |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 747 | id = cur.len % sizeof( dropped ); |
| 748 | |
Hanno Becker | 01ea778 | 2018-08-17 13:33:41 +0100 | [diff] [blame] | 749 | if( strcmp( way, "S <- C" ) == 0 ) |
| 750 | { |
| 751 | delay_list = opt.delay_cli; |
| 752 | delay_list_len = opt.delay_cli_cnt; |
| 753 | } |
| 754 | else |
| 755 | { |
| 756 | delay_list = opt.delay_srv; |
| 757 | delay_list_len = opt.delay_srv_cnt; |
| 758 | } |
Hanno Becker | cf46945 | 2018-08-28 10:09:47 +0100 | [diff] [blame] | 759 | |
Hanno Becker | 01ea778 | 2018-08-17 13:33:41 +0100 | [diff] [blame] | 760 | /* Check if message type is in the list of messages |
| 761 | * that should be delayed */ |
| 762 | for( delay_idx = 0; delay_idx < delay_list_len; delay_idx++ ) |
| 763 | { |
| 764 | if( delay_list[ delay_idx ] == NULL ) |
| 765 | continue; |
| 766 | |
| 767 | if( strcmp( delay_list[ delay_idx ], cur.type ) == 0 ) |
| 768 | { |
| 769 | /* Delay message */ |
Hanno Becker | 101bcba | 2018-08-21 16:39:51 +0100 | [diff] [blame] | 770 | delay_packet( &cur ); |
Hanno Becker | 01ea778 | 2018-08-17 13:33:41 +0100 | [diff] [blame] | 771 | |
| 772 | /* Remove entry from list */ |
| 773 | mbedtls_free( delay_list[delay_idx] ); |
| 774 | delay_list[delay_idx] = NULL; |
| 775 | |
| 776 | return( 0 ); |
| 777 | } |
| 778 | } |
| 779 | |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 780 | /* do we want to drop, delay, or forward it? */ |
Manuel Pégourié-Gonnard | eb00bfd | 2014-09-08 11:11:42 +0200 | [diff] [blame] | 781 | if( ( opt.mtu != 0 && |
| 782 | cur.len > (unsigned) opt.mtu ) || |
| 783 | ( opt.drop != 0 && |
Hanno Becker | 6c4bc14 | 2019-05-08 15:36:31 +0100 | [diff] [blame] | 784 | strcmp( cur.type, "CID" ) != 0 && |
Manuel Pégourié-Gonnard | eb00bfd | 2014-09-08 11:11:42 +0200 | [diff] [blame] | 785 | strcmp( cur.type, "ApplicationData" ) != 0 && |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 786 | ! ( opt.protect_hvr && |
| 787 | strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) && |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 788 | cur.len != (size_t) opt.protect_len && |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 789 | dropped[id] < DROP_MAX && |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 790 | rand() % opt.drop == 0 ) ) |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 791 | { |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 792 | update_dropped( &cur ); |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 793 | } |
Manuel Pégourié-Gonnard | 81f2fe9 | 2014-09-08 10:44:57 +0200 | [diff] [blame] | 794 | else if( ( opt.delay_ccs == 1 && |
| 795 | strcmp( cur.type, "ChangeCipherSpec" ) == 0 ) || |
| 796 | ( opt.delay != 0 && |
Hanno Becker | 6c4bc14 | 2019-05-08 15:36:31 +0100 | [diff] [blame] | 797 | strcmp( cur.type, "CID" ) != 0 && |
Manuel Pégourié-Gonnard | 81f2fe9 | 2014-09-08 10:44:57 +0200 | [diff] [blame] | 798 | strcmp( cur.type, "ApplicationData" ) != 0 && |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 799 | ! ( opt.protect_hvr && |
| 800 | strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) && |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 801 | cur.len != (size_t) opt.protect_len && |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 802 | dropped[id] < DROP_MAX && |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 803 | rand() % opt.delay == 0 ) ) |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 804 | { |
Hanno Becker | 101bcba | 2018-08-21 16:39:51 +0100 | [diff] [blame] | 805 | delay_packet( &cur ); |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 806 | } |
| 807 | else |
| 808 | { |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 809 | /* forward and possibly duplicate */ |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 810 | if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 ) |
| 811 | return( ret ); |
| 812 | |
Hanno Becker | 101bcba | 2018-08-21 16:39:51 +0100 | [diff] [blame] | 813 | /* send previously delayed messages if any */ |
| 814 | ret = send_delayed(); |
| 815 | if( ret != 0 ) |
| 816 | return( ret ); |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 817 | } |
| 818 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 819 | return( 0 ); |
| 820 | } |
| 821 | |
| 822 | int main( int argc, char *argv[] ) |
| 823 | { |
Andres Amaya Garcia | 80081a6 | 2018-04-29 21:58:53 +0100 | [diff] [blame] | 824 | int ret = 1; |
| 825 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Hanno Becker | 01ea778 | 2018-08-17 13:33:41 +0100 | [diff] [blame] | 826 | uint8_t delay_idx; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 827 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 828 | mbedtls_net_context listen_fd, client_fd, server_fd; |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 829 | |
| 830 | #if defined( MBEDTLS_TIMING_C ) |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 831 | struct timeval tm; |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 832 | #endif |
| 833 | |
| 834 | struct timeval *tm_ptr = NULL; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 835 | |
| 836 | int nb_fds; |
| 837 | fd_set read_fds; |
| 838 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 839 | mbedtls_net_init( &listen_fd ); |
| 840 | mbedtls_net_init( &client_fd ); |
| 841 | mbedtls_net_init( &server_fd ); |
| 842 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 843 | get_options( argc, argv ); |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 844 | |
| 845 | /* |
| 846 | * Decisions to drop/delay/duplicate packets are pseudo-random: dropping |
| 847 | * exactly 1 in N packets would lead to problems when a flight has exactly |
| 848 | * N packets: the same packet would be dropped on every resend. |
| 849 | * |
| 850 | * In order to be able to reproduce problems reliably, the seed may be |
| 851 | * specified explicitly. |
| 852 | */ |
| 853 | if( opt.seed == 0 ) |
| 854 | { |
Manuel Pégourié-Gonnard | 9de64f5 | 2015-07-01 15:51:43 +0200 | [diff] [blame] | 855 | opt.seed = (unsigned int) time( NULL ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 856 | mbedtls_printf( " . Pseudo-random seed: %u\n", opt.seed ); |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | srand( opt.seed ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 860 | |
| 861 | /* |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 862 | * 0. "Connect" to the server |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 863 | */ |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 864 | mbedtls_printf( " . Connect to server on UDP/%s/%s ...", |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 865 | opt.server_addr, opt.server_port ); |
| 866 | fflush( stdout ); |
| 867 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 868 | if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port, |
| 869 | MBEDTLS_NET_PROTO_UDP ) ) != 0 ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 870 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 871 | mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 872 | goto exit; |
| 873 | } |
| 874 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 875 | mbedtls_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 876 | |
| 877 | /* |
| 878 | * 1. Setup the "listening" UDP socket |
| 879 | */ |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 880 | mbedtls_printf( " . Bind on UDP/%s/%s ...", |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 881 | opt.listen_addr, opt.listen_port ); |
| 882 | fflush( stdout ); |
| 883 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 884 | if( ( ret = mbedtls_net_bind( &listen_fd, opt.listen_addr, opt.listen_port, |
| 885 | MBEDTLS_NET_PROTO_UDP ) ) != 0 ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 886 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 887 | mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 888 | goto exit; |
| 889 | } |
| 890 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 891 | mbedtls_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 892 | |
| 893 | /* |
| 894 | * 2. Wait until a client connects |
| 895 | */ |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 896 | accept: |
Manuel Pégourié-Gonnard | abc729e | 2015-07-01 01:28:24 +0200 | [diff] [blame] | 897 | mbedtls_net_free( &client_fd ); |
| 898 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 899 | mbedtls_printf( " . Waiting for a remote connection ..." ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 900 | fflush( stdout ); |
| 901 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 902 | if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd, |
Manuel Pégourié-Gonnard | 0b104b0 | 2015-05-14 21:52:40 +0200 | [diff] [blame] | 903 | NULL, 0, NULL ) ) != 0 ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 904 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 905 | mbedtls_printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 906 | goto exit; |
| 907 | } |
| 908 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 909 | mbedtls_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 910 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 911 | /* |
| 912 | * 3. Forward packets forever (kill the process to terminate it) |
| 913 | */ |
Manuel Pégourié-Gonnard | ce8588c | 2014-10-01 00:56:03 +0200 | [diff] [blame] | 914 | clear_pending(); |
| 915 | memset( dropped, 0, sizeof( dropped ) ); |
| 916 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 917 | nb_fds = client_fd.fd; |
| 918 | if( nb_fds < server_fd.fd ) |
| 919 | nb_fds = server_fd.fd; |
| 920 | if( nb_fds < listen_fd.fd ) |
| 921 | nb_fds = listen_fd.fd; |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 922 | ++nb_fds; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 923 | |
Hanno Becker | 0cc7774 | 2017-10-31 14:10:07 +0000 | [diff] [blame] | 924 | #if defined(MBEDTLS_TIMING_C) |
Hanno Becker | 211f44c | 2017-10-31 14:08:10 +0000 | [diff] [blame] | 925 | if( opt.pack > 0 ) |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 926 | { |
| 927 | outbuf[0].ctx = &server_fd; |
| 928 | outbuf[0].description = "S <- C"; |
| 929 | outbuf[0].num_datagrams = 0; |
| 930 | outbuf[0].len = 0; |
| 931 | |
| 932 | outbuf[1].ctx = &client_fd; |
| 933 | outbuf[1].description = "S -> C"; |
| 934 | outbuf[1].num_datagrams = 0; |
| 935 | outbuf[1].len = 0; |
| 936 | } |
Hanno Becker | 0cc7774 | 2017-10-31 14:10:07 +0000 | [diff] [blame] | 937 | #endif /* MBEDTLS_TIMING_C */ |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 938 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 939 | while( 1 ) |
| 940 | { |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 941 | #if defined(MBEDTLS_TIMING_C) |
| 942 | if( opt.pack > 0 ) |
| 943 | { |
| 944 | unsigned max_wait_server, max_wait_client, max_wait; |
| 945 | max_wait_server = ctx_buffer_time_remaining( &outbuf[0] ); |
| 946 | max_wait_client = ctx_buffer_time_remaining( &outbuf[1] ); |
| 947 | |
| 948 | max_wait = (unsigned) -1; |
| 949 | |
| 950 | if( max_wait_server == 0 ) |
| 951 | ctx_buffer_flush( &outbuf[0] ); |
| 952 | else |
| 953 | max_wait = max_wait_server; |
| 954 | |
| 955 | if( max_wait_client == 0 ) |
| 956 | ctx_buffer_flush( &outbuf[1] ); |
| 957 | else |
| 958 | { |
| 959 | if( max_wait_client < max_wait ) |
| 960 | max_wait = max_wait_client; |
| 961 | } |
| 962 | |
| 963 | if( max_wait != (unsigned) -1 ) |
| 964 | { |
| 965 | tm.tv_sec = max_wait / 1000; |
| 966 | tm.tv_usec = ( max_wait % 1000 ) * 1000; |
| 967 | |
| 968 | tm_ptr = &tm; |
| 969 | } |
| 970 | else |
| 971 | { |
| 972 | tm_ptr = NULL; |
| 973 | } |
| 974 | } |
| 975 | #endif /* MBEDTLS_TIMING_C */ |
| 976 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 977 | FD_ZERO( &read_fds ); |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 978 | FD_SET( server_fd.fd, &read_fds ); |
| 979 | FD_SET( client_fd.fd, &read_fds ); |
| 980 | FD_SET( listen_fd.fd, &read_fds ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 981 | |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 982 | if( ( ret = select( nb_fds, &read_fds, NULL, NULL, tm_ptr ) ) < 0 ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 983 | { |
| 984 | perror( "select" ); |
| 985 | goto exit; |
| 986 | } |
| 987 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 988 | if( FD_ISSET( listen_fd.fd, &read_fds ) ) |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 989 | goto accept; |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 990 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 991 | if( FD_ISSET( client_fd.fd, &read_fds ) ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 992 | { |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 993 | if( ( ret = handle_message( "S <- C", |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 994 | &server_fd, &client_fd ) ) != 0 ) |
Manuel Pégourié-Gonnard | ce8588c | 2014-10-01 00:56:03 +0200 | [diff] [blame] | 995 | goto accept; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 996 | } |
| 997 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 998 | if( FD_ISSET( server_fd.fd, &read_fds ) ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 999 | { |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 1000 | if( ( ret = handle_message( "S -> C", |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 1001 | &client_fd, &server_fd ) ) != 0 ) |
Manuel Pégourié-Gonnard | ce8588c | 2014-10-01 00:56:03 +0200 | [diff] [blame] | 1002 | goto accept; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 1003 | } |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 1004 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 1005 | } |
| 1006 | |
Andres Amaya Garcia | 80081a6 | 2018-04-29 21:58:53 +0100 | [diff] [blame] | 1007 | exit_code = MBEDTLS_EXIT_SUCCESS; |
| 1008 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 1009 | exit: |
| 1010 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1011 | #ifdef MBEDTLS_ERROR_C |
Andres Amaya Garcia | 80081a6 | 2018-04-29 21:58:53 +0100 | [diff] [blame] | 1012 | if( exit_code != MBEDTLS_EXIT_SUCCESS ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 1013 | { |
| 1014 | char error_buf[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1015 | mbedtls_strerror( ret, error_buf, 100 ); |
| 1016 | mbedtls_printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 1017 | fflush( stdout ); |
| 1018 | } |
| 1019 | #endif |
| 1020 | |
Hanno Becker | 01ea778 | 2018-08-17 13:33:41 +0100 | [diff] [blame] | 1021 | for( delay_idx = 0; delay_idx < MAX_DELAYED_HS; delay_idx++ ) |
| 1022 | { |
| 1023 | mbedtls_free( opt.delay_cli + delay_idx ); |
| 1024 | mbedtls_free( opt.delay_srv + delay_idx ); |
| 1025 | } |
| 1026 | |
Manuel Pégourié-Gonnard | 3d7d00a | 2015-06-30 15:55:03 +0200 | [diff] [blame] | 1027 | mbedtls_net_free( &client_fd ); |
| 1028 | mbedtls_net_free( &server_fd ); |
| 1029 | mbedtls_net_free( &listen_fd ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 1030 | |
| 1031 | #if defined(_WIN32) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1032 | mbedtls_printf( " Press Enter to exit this program.\n" ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 1033 | fflush( stdout ); getchar(); |
| 1034 | #endif |
| 1035 | |
Andrzej Kurek | 825ebd4 | 2020-05-18 11:47:25 -0400 | [diff] [blame] | 1036 | mbedtls_exit( exit_code ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 1037 | } |
| 1038 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1039 | #endif /* MBEDTLS_NET_C */ |