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 | * |
Bence Szépkúti | 44bfbe3 | 2020-08-19 16:54:51 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 6 | * |
| 7 | * This file is provided under the Apache License 2.0, or the |
| 8 | * GNU General Public License v2.0 or later. |
| 9 | * |
| 10 | * ********** |
| 11 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 12 | * |
| 13 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 14 | * not use this file except in compliance with the License. |
| 15 | * You may obtain a copy of the License at |
| 16 | * |
| 17 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | * |
| 19 | * Unless required by applicable law or agreed to in writing, software |
| 20 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 21 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | * See the License for the specific language governing permissions and |
| 23 | * limitations under the License. |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 24 | * |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 25 | * ********** |
| 26 | * |
| 27 | * ********** |
| 28 | * GNU General Public License v2.0 or later: |
| 29 | * |
| 30 | * This program is free software; you can redistribute it and/or modify |
| 31 | * it under the terms of the GNU General Public License as published by |
| 32 | * the Free Software Foundation; either version 2 of the License, or |
| 33 | * (at your option) any later version. |
| 34 | * |
| 35 | * This program is distributed in the hope that it will be useful, |
| 36 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 37 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 38 | * GNU General Public License for more details. |
| 39 | * |
| 40 | * You should have received a copy of the GNU General Public License along |
| 41 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 42 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 43 | * |
| 44 | * ********** |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 45 | */ |
| 46 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 47 | /* |
| 48 | * Warning: this is an internal utility program we use for tests. |
| 49 | * It does break some abstractions from the NET layer, and is thus NOT an |
| 50 | * example of good general usage. |
| 51 | */ |
| 52 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 53 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 54 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 55 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 56 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 57 | #endif |
| 58 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 59 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 60 | #include "mbedtls/platform.h" |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame] | 61 | #else |
Simon Butcher | d3138c3 | 2016-04-27 01:26:50 +0100 | [diff] [blame] | 62 | #include <stdio.h> |
| 63 | #include <stdlib.h> |
| 64 | #include <time.h> |
Andres Amaya Garcia | 01e3d21 | 2018-04-29 21:58:53 +0100 | [diff] [blame] | 65 | #define mbedtls_time time |
| 66 | #define mbedtls_time_t time_t |
| 67 | #define mbedtls_printf printf |
Krzysztof Stachowiak | a086522 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 68 | #define mbedtls_exit exit |
Andres Amaya Garcia | 2b0599b | 2018-04-30 22:42:33 +0100 | [diff] [blame] | 69 | #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
Andres Amaya Garcia | 01e3d21 | 2018-04-29 21:58:53 +0100 | [diff] [blame] | 70 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
| 71 | #endif /* MBEDTLS_PLATFORM_C */ |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame] | 72 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 73 | #if !defined(MBEDTLS_NET_C) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 74 | int main( void ) |
| 75 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 76 | mbedtls_printf( "MBEDTLS_NET_C not defined.\n" ); |
Krzysztof Stachowiak | a086522 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 77 | mbedtls_exit( 0 ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 78 | } |
| 79 | #else |
| 80 | |
Andres AG | 788aa4a | 2016-09-14 14:32:09 +0100 | [diff] [blame] | 81 | #include "mbedtls/net_sockets.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 82 | #include "mbedtls/error.h" |
| 83 | #include "mbedtls/ssl.h" |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 84 | |
Manuel Pégourié-Gonnard | d901d17 | 2015-02-16 18:37:53 +0000 | [diff] [blame] | 85 | #include <string.h> |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 86 | |
| 87 | /* For select() */ |
| 88 | #if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \ |
| 89 | !defined(EFI32) |
| 90 | #include <winsock2.h> |
| 91 | #include <windows.h> |
| 92 | #if defined(_MSC_VER) |
| 93 | #if defined(_WIN32_WCE) |
| 94 | #pragma comment( lib, "ws2.lib" ) |
| 95 | #else |
| 96 | #pragma comment( lib, "ws2_32.lib" ) |
| 97 | #endif |
| 98 | #endif /* _MSC_VER */ |
| 99 | #else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */ |
| 100 | #include <sys/time.h> |
| 101 | #include <sys/types.h> |
| 102 | #include <unistd.h> |
| 103 | #endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */ |
| 104 | |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 105 | /* For gettimeofday() */ |
| 106 | #if !defined(_WIN32) |
| 107 | #include <sys/time.h> |
| 108 | #endif |
| 109 | |
Manuel Pégourié-Gonnard | b46780e | 2014-09-23 12:17:30 +0200 | [diff] [blame] | 110 | #define MAX_MSG_SIZE 16384 + 2048 /* max record/datagram size */ |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 111 | |
| 112 | #define DFL_SERVER_ADDR "localhost" |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 113 | #define DFL_SERVER_PORT "4433" |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 114 | #define DFL_LISTEN_ADDR "localhost" |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 115 | #define DFL_LISTEN_PORT "5556" |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 116 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 117 | #define USAGE \ |
| 118 | "\n usage: udp_proxy param=<>...\n" \ |
| 119 | "\n acceptable parameters:\n" \ |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 120 | " server_addr=%%s default: localhost\n" \ |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 121 | " server_port=%%d default: 4433\n" \ |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 122 | " listen_addr=%%s default: localhost\n" \ |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 123 | " listen_port=%%d default: 4433\n" \ |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 124 | "\n" \ |
| 125 | " duplicate=%%d default: 0 (no duplication)\n" \ |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 126 | " duplicate about 1:N packets randomly\n" \ |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 127 | " delay=%%d default: 0 (no delayed packets)\n" \ |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 128 | " delay about 1:N packets randomly\n" \ |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 129 | " delay_ccs=0/1 default: 0 (don't delay ChangeCipherSpec)\n" \ |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 130 | " drop=%%d default: 0 (no dropped packets)\n" \ |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 131 | " drop about 1:N packets randomly\n" \ |
Manuel Pégourié-Gonnard | eb00bfd | 2014-09-08 11:11:42 +0200 | [diff] [blame] | 132 | " mtu=%%d default: 0 (unlimited)\n" \ |
| 133 | " drop packets larger than N bytes\n" \ |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 134 | " bad_ad=0/1 default: 0 (don't add bad ApplicationData)\n" \ |
| 135 | " protect_hvr=0/1 default: 0 (don't protect HelloVerifyRequest)\n" \ |
Manuel Pégourié-Gonnard | a58b046 | 2020-03-13 11:11:02 +0100 | [diff] [blame] | 136 | " protect_len=%%d default: (don't protect packets of this size)\n" \ |
| 137 | " 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] | 138 | "\n" \ |
| 139 | " seed=%%d default: (use current time)\n" \ |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 140 | "\n" |
| 141 | |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 142 | /* |
| 143 | * global options |
| 144 | */ |
| 145 | static struct options |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 146 | { |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 147 | const char *server_addr; /* address to forward packets to */ |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 148 | const char *server_port; /* port to forward packets to */ |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 149 | const char *listen_addr; /* address for accepting client connections */ |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 150 | const char *listen_port; /* port for accepting client connections */ |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 151 | |
| 152 | int duplicate; /* duplicate 1 in N packets (none if 0) */ |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 153 | int delay; /* delay 1 packet in N (none if 0) */ |
Manuel Pégourié-Gonnard | 81f2fe9 | 2014-09-08 10:44:57 +0200 | [diff] [blame] | 154 | int delay_ccs; /* delay ChangeCipherSpec */ |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 155 | int drop; /* drop 1 packet in N (none if 0) */ |
Manuel Pégourié-Gonnard | eb00bfd | 2014-09-08 11:11:42 +0200 | [diff] [blame] | 156 | int mtu; /* drop packets larger than this */ |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 157 | int bad_ad; /* inject corrupted ApplicationData record */ |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 158 | int protect_hvr; /* never drop or delay HelloVerifyRequest */ |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 159 | int protect_len; /* never drop/delay packet of the given size*/ |
Manuel Pégourié-Gonnard | a58b046 | 2020-03-13 11:11:02 +0100 | [diff] [blame] | 160 | int inject_clihlo; /* inject fake ClientHello after handshake */ |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 161 | unsigned int seed; /* seed for "random" events */ |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 162 | } opt; |
| 163 | |
| 164 | static void exit_usage( const char *name, const char *value ) |
| 165 | { |
| 166 | if( value == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 167 | mbedtls_printf( " unknown option or missing value: %s\n", name ); |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 168 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 169 | mbedtls_printf( " option %s: illegal value: %s\n", name, value ); |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 170 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 171 | mbedtls_printf( USAGE ); |
Krzysztof Stachowiak | a086522 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 172 | mbedtls_exit( 1 ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | static void get_options( int argc, char *argv[] ) |
| 176 | { |
| 177 | int i; |
| 178 | char *p, *q; |
| 179 | |
| 180 | opt.server_addr = DFL_SERVER_ADDR; |
| 181 | opt.server_port = DFL_SERVER_PORT; |
| 182 | opt.listen_addr = DFL_LISTEN_ADDR; |
| 183 | opt.listen_port = DFL_LISTEN_PORT; |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 184 | /* Other members default to 0 */ |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 185 | |
| 186 | for( i = 1; i < argc; i++ ) |
| 187 | { |
| 188 | p = argv[i]; |
| 189 | if( ( q = strchr( p, '=' ) ) == NULL ) |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 190 | exit_usage( p, NULL ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 191 | *q++ = '\0'; |
| 192 | |
| 193 | if( strcmp( p, "server_addr" ) == 0 ) |
| 194 | opt.server_addr = q; |
| 195 | else if( strcmp( p, "server_port" ) == 0 ) |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 196 | opt.server_port = q; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 197 | else if( strcmp( p, "listen_addr" ) == 0 ) |
| 198 | opt.listen_addr = q; |
| 199 | else if( strcmp( p, "listen_port" ) == 0 ) |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 200 | opt.listen_port = q; |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 201 | else if( strcmp( p, "duplicate" ) == 0 ) |
| 202 | { |
| 203 | opt.duplicate = atoi( q ); |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 204 | if( opt.duplicate < 0 || opt.duplicate > 20 ) |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 205 | exit_usage( p, q ); |
| 206 | } |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 207 | else if( strcmp( p, "delay" ) == 0 ) |
| 208 | { |
| 209 | opt.delay = atoi( q ); |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 210 | if( opt.delay < 0 || opt.delay > 20 || opt.delay == 1 ) |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 211 | exit_usage( p, q ); |
| 212 | } |
Manuel Pégourié-Gonnard | 81f2fe9 | 2014-09-08 10:44:57 +0200 | [diff] [blame] | 213 | else if( strcmp( p, "delay_ccs" ) == 0 ) |
| 214 | { |
| 215 | opt.delay_ccs = atoi( q ); |
| 216 | if( opt.delay_ccs < 0 || opt.delay_ccs > 1 ) |
| 217 | exit_usage( p, q ); |
| 218 | } |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 219 | else if( strcmp( p, "drop" ) == 0 ) |
| 220 | { |
| 221 | opt.drop = atoi( q ); |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 222 | if( opt.drop < 0 || opt.drop > 20 || opt.drop == 1 ) |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 223 | exit_usage( p, q ); |
| 224 | } |
Manuel Pégourié-Gonnard | eb00bfd | 2014-09-08 11:11:42 +0200 | [diff] [blame] | 225 | else if( strcmp( p, "mtu" ) == 0 ) |
| 226 | { |
| 227 | opt.mtu = atoi( q ); |
| 228 | if( opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE ) |
| 229 | exit_usage( p, q ); |
| 230 | } |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 231 | else if( strcmp( p, "bad_ad" ) == 0 ) |
| 232 | { |
| 233 | opt.bad_ad = atoi( q ); |
| 234 | if( opt.bad_ad < 0 || opt.bad_ad > 1 ) |
| 235 | exit_usage( p, q ); |
| 236 | } |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 237 | else if( strcmp( p, "protect_hvr" ) == 0 ) |
| 238 | { |
| 239 | opt.protect_hvr = atoi( q ); |
| 240 | if( opt.protect_hvr < 0 || opt.protect_hvr > 1 ) |
| 241 | exit_usage( p, q ); |
| 242 | } |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 243 | else if( strcmp( p, "protect_len" ) == 0 ) |
| 244 | { |
| 245 | opt.protect_len = atoi( q ); |
| 246 | if( opt.protect_len < 0 ) |
| 247 | exit_usage( p, q ); |
| 248 | } |
Manuel Pégourié-Gonnard | a58b046 | 2020-03-13 11:11:02 +0100 | [diff] [blame] | 249 | else if( strcmp( p, "inject_clihlo" ) == 0 ) |
| 250 | { |
| 251 | opt.inject_clihlo = atoi( q ); |
| 252 | if( opt.inject_clihlo < 0 || opt.inject_clihlo > 1 ) |
| 253 | exit_usage( p, q ); |
| 254 | } |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 255 | else if( strcmp( p, "seed" ) == 0 ) |
| 256 | { |
| 257 | opt.seed = atoi( q ); |
| 258 | if( opt.seed == 0 ) |
| 259 | exit_usage( p, q ); |
| 260 | } |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 261 | else |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 262 | exit_usage( p, NULL ); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | static const char *msg_type( unsigned char *msg, size_t len ) |
| 267 | { |
| 268 | if( len < 1 ) return( "Invalid" ); |
| 269 | switch( msg[0] ) |
| 270 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 271 | case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" ); |
| 272 | case MBEDTLS_SSL_MSG_ALERT: return( "Alert" ); |
| 273 | case MBEDTLS_SSL_MSG_APPLICATION_DATA: return( "ApplicationData" ); |
| 274 | case MBEDTLS_SSL_MSG_HANDSHAKE: break; /* See below */ |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 275 | default: return( "Unknown" ); |
| 276 | } |
| 277 | |
Manuel Pégourié-Gonnard | 8cc7e03 | 2014-09-25 12:59:05 +0200 | [diff] [blame] | 278 | if( len < 13 + 12 ) return( "Invalid handshake" ); |
| 279 | |
| 280 | /* |
| 281 | * Our handshake message are less than 2^16 bytes long, so they should |
| 282 | * have 0 as the first byte of length, frag_offset and frag_length. |
| 283 | * Otherwise, assume they are encrypted. |
| 284 | */ |
| 285 | if( msg[14] || msg[19] || msg[22] ) return( "Encrypted handshake" ); |
| 286 | |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 287 | switch( msg[13] ) |
| 288 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 289 | case MBEDTLS_SSL_HS_HELLO_REQUEST: return( "HelloRequest" ); |
| 290 | case MBEDTLS_SSL_HS_CLIENT_HELLO: return( "ClientHello" ); |
| 291 | case MBEDTLS_SSL_HS_SERVER_HELLO: return( "ServerHello" ); |
| 292 | case MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" ); |
| 293 | case MBEDTLS_SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" ); |
| 294 | case MBEDTLS_SSL_HS_CERTIFICATE: return( "Certificate" ); |
| 295 | case MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" ); |
| 296 | case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" ); |
| 297 | case MBEDTLS_SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" ); |
| 298 | case MBEDTLS_SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" ); |
| 299 | case MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" ); |
| 300 | case MBEDTLS_SSL_HS_FINISHED: return( "Finished" ); |
Manuel Pégourié-Gonnard | bc010a0 | 2014-09-20 12:40:51 +0200 | [diff] [blame] | 301 | default: return( "Unknown handshake" ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 305 | /* Return elapsed time in milliseconds since the first call */ |
| 306 | static unsigned long ellapsed_time( void ) |
| 307 | { |
| 308 | #if defined(_WIN32) |
| 309 | return( 0 ); |
| 310 | #else |
| 311 | static struct timeval ref = { 0, 0 }; |
| 312 | struct timeval now; |
| 313 | |
| 314 | if( ref.tv_sec == 0 && ref.tv_usec == 0 ) |
| 315 | { |
| 316 | gettimeofday( &ref, NULL ); |
| 317 | return( 0 ); |
| 318 | } |
| 319 | |
| 320 | gettimeofday( &now, NULL ); |
| 321 | return( 1000 * ( now.tv_sec - ref.tv_sec ) |
| 322 | + ( now.tv_usec - ref.tv_usec ) / 1000 ); |
| 323 | #endif |
| 324 | } |
| 325 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 326 | typedef struct |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 327 | { |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 328 | mbedtls_net_context *dst; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 329 | const char *way; |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 330 | const char *type; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 331 | unsigned len; |
| 332 | unsigned char buf[MAX_MSG_SIZE]; |
| 333 | } packet; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 334 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 335 | /* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */ |
| 336 | void print_packet( const packet *p, const char *why ) |
| 337 | { |
| 338 | if( why == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 339 | mbedtls_printf( " %05lu %s %s (%u bytes)\n", |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 340 | ellapsed_time(), p->way, p->type, p->len ); |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 341 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 342 | mbedtls_printf( " %s %s (%u bytes): %s\n", |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 343 | p->way, p->type, p->len, why ); |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 344 | fflush( stdout ); |
| 345 | } |
| 346 | |
Manuel Pégourié-Gonnard | a58b046 | 2020-03-13 11:11:02 +0100 | [diff] [blame] | 347 | /* |
| 348 | * In order to test the server's behaviour when receiving a ClientHello after |
| 349 | * the connection is established (this could be a hard reset from the client, |
| 350 | * but the server must not drop the existing connection before establishing |
| 351 | * client reachability, see RFC 6347 Section 4.2.8), we memorize the first |
| 352 | * ClientHello we see (which can't have a cookie), then replay it after the |
| 353 | * first ApplicationData record - then we're done. |
| 354 | * |
| 355 | * This is controlled by the inject_clihlo option. |
| 356 | * |
| 357 | * We want an explicit state and a place to store the packet. |
| 358 | */ |
Manuel Pégourié-Gonnard | 7ef7bf3 | 2020-03-30 12:46:21 +0200 | [diff] [blame] | 359 | typedef enum { |
| 360 | ICH_INIT, /* haven't seen the first ClientHello yet */ |
| 361 | ICH_CACHED, /* cached the initial ClientHello */ |
| 362 | ICH_INJECTED, /* ClientHello already injected, done */ |
| 363 | } inject_clihlo_state_t; |
Manuel Pégourié-Gonnard | a58b046 | 2020-03-13 11:11:02 +0100 | [diff] [blame] | 364 | |
Manuel Pégourié-Gonnard | 7ef7bf3 | 2020-03-30 12:46:21 +0200 | [diff] [blame] | 365 | static inject_clihlo_state_t inject_clihlo_state; |
Manuel Pégourié-Gonnard | a58b046 | 2020-03-13 11:11:02 +0100 | [diff] [blame] | 366 | static packet initial_clihlo; |
| 367 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 368 | int send_packet( const packet *p, const char *why ) |
| 369 | { |
| 370 | int ret; |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 371 | mbedtls_net_context *dst = p->dst; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 372 | |
Manuel Pégourié-Gonnard | a58b046 | 2020-03-13 11:11:02 +0100 | [diff] [blame] | 373 | /* save initial ClientHello? */ |
| 374 | if( opt.inject_clihlo != 0 && |
Manuel Pégourié-Gonnard | 7ef7bf3 | 2020-03-30 12:46:21 +0200 | [diff] [blame] | 375 | inject_clihlo_state == ICH_INIT && |
Manuel Pégourié-Gonnard | a58b046 | 2020-03-13 11:11:02 +0100 | [diff] [blame] | 376 | strcmp( p->type, "ClientHello" ) == 0 ) |
| 377 | { |
| 378 | memcpy( &initial_clihlo, p, sizeof( packet ) ); |
Manuel Pégourié-Gonnard | 7ef7bf3 | 2020-03-30 12:46:21 +0200 | [diff] [blame] | 379 | inject_clihlo_state = ICH_CACHED; |
Manuel Pégourié-Gonnard | a58b046 | 2020-03-13 11:11:02 +0100 | [diff] [blame] | 380 | } |
| 381 | |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 382 | /* insert corrupted ApplicationData record? */ |
| 383 | if( opt.bad_ad && |
| 384 | strcmp( p->type, "ApplicationData" ) == 0 ) |
| 385 | { |
| 386 | unsigned char buf[MAX_MSG_SIZE]; |
| 387 | memcpy( buf, p->buf, p->len ); |
| 388 | ++buf[p->len - 1]; |
| 389 | |
| 390 | print_packet( p, "corrupted" ); |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 391 | if( ( ret = mbedtls_net_send( dst, buf, p->len ) ) <= 0 ) |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 392 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 393 | mbedtls_printf( " ! mbedtls_net_send returned %d\n", ret ); |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 394 | return( ret ); |
| 395 | } |
| 396 | } |
| 397 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 398 | print_packet( p, why ); |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 399 | if( ( ret = mbedtls_net_send( dst, p->buf, p->len ) ) <= 0 ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 400 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 401 | mbedtls_printf( " ! mbedtls_net_send returned %d\n", ret ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 402 | return( ret ); |
| 403 | } |
| 404 | |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 405 | /* Don't duplicate Application Data, only handshake covered */ |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 406 | if( opt.duplicate != 0 && |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 407 | strcmp( p->type, "ApplicationData" ) != 0 && |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 408 | rand() % opt.duplicate == 0 ) |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 409 | { |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 410 | print_packet( p, "duplicated" ); |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 411 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 412 | if( ( ret = mbedtls_net_send( dst, p->buf, p->len ) ) <= 0 ) |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 413 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 414 | mbedtls_printf( " ! mbedtls_net_send returned %d\n", ret ); |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 415 | return( ret ); |
| 416 | } |
| 417 | } |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 418 | |
Manuel Pégourié-Gonnard | a58b046 | 2020-03-13 11:11:02 +0100 | [diff] [blame] | 419 | /* Inject ClientHello after first ApplicationData */ |
| 420 | if( opt.inject_clihlo != 0 && |
Manuel Pégourié-Gonnard | 7ef7bf3 | 2020-03-30 12:46:21 +0200 | [diff] [blame] | 421 | inject_clihlo_state == ICH_CACHED && |
Manuel Pégourié-Gonnard | a58b046 | 2020-03-13 11:11:02 +0100 | [diff] [blame] | 422 | strcmp( p->type, "ApplicationData" ) == 0 ) |
| 423 | { |
| 424 | print_packet( &initial_clihlo, "injected" ); |
| 425 | |
| 426 | if( ( ret = mbedtls_net_send( dst, initial_clihlo.buf, |
| 427 | initial_clihlo.len ) ) <= 0 ) |
| 428 | { |
| 429 | mbedtls_printf( " ! mbedtls_net_send returned %d\n", ret ); |
| 430 | return( ret ); |
| 431 | } |
| 432 | |
Manuel Pégourié-Gonnard | 7ef7bf3 | 2020-03-30 12:46:21 +0200 | [diff] [blame] | 433 | inject_clihlo_state = ICH_INJECTED; |
Manuel Pégourié-Gonnard | a58b046 | 2020-03-13 11:11:02 +0100 | [diff] [blame] | 434 | } |
| 435 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 436 | return( 0 ); |
| 437 | } |
| 438 | |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 439 | static packet prev; |
| 440 | |
| 441 | void clear_pending( void ) |
| 442 | { |
| 443 | memset( &prev, 0, sizeof( packet ) ); |
| 444 | } |
| 445 | |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 446 | /* |
| 447 | * Avoid dropping or delaying a packet that was already dropped twice: this |
| 448 | * only results in uninteresting timeouts. We can't rely on type to identify |
| 449 | * packets, since during renegotiation they're all encrypted. So, rely on |
| 450 | * size mod 2048 (which is usually just size). |
| 451 | */ |
| 452 | static unsigned char dropped[2048] = { 0 }; |
| 453 | #define DROP_MAX 2 |
| 454 | |
Hanno Becker | 7bac1ab | 2019-06-04 13:04:28 +0100 | [diff] [blame] | 455 | /* We only drop packets at the level of entire datagrams, not at the level |
| 456 | * of records. In particular, if the peer changes the way it packs multiple |
| 457 | * records into a single datagram, we don't necessarily count the number of |
| 458 | * times a record has been dropped correctly. However, the only known reason |
| 459 | * why a peer would change datagram packing is disabling the latter on |
| 460 | * retransmission, in which case we'd drop involved records at most |
| 461 | * DROP_MAX + 1 times. */ |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 462 | void update_dropped( const packet *p ) |
| 463 | { |
| 464 | size_t id = p->len % sizeof( dropped ); |
Manuel Pégourié-Gonnard | fa60f12 | 2014-09-26 16:07:29 +0200 | [diff] [blame] | 465 | ++dropped[id]; |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 466 | } |
| 467 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 468 | int handle_message( const char *way, |
| 469 | mbedtls_net_context *dst, |
| 470 | mbedtls_net_context *src ) |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 471 | { |
| 472 | int ret; |
| 473 | packet cur; |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 474 | size_t id; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 475 | |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 476 | /* receive packet */ |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 477 | 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] | 478 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 479 | mbedtls_printf( " ! mbedtls_net_recv returned %d\n", ret ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 480 | return( ret ); |
| 481 | } |
| 482 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 483 | cur.len = ret; |
| 484 | cur.type = msg_type( cur.buf, cur.len ); |
| 485 | cur.way = way; |
Manuel Pégourié-Gonnard | 6265d30 | 2014-09-24 17:42:09 +0200 | [diff] [blame] | 486 | cur.dst = dst; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 487 | print_packet( &cur, NULL ); |
| 488 | |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 489 | id = cur.len % sizeof( dropped ); |
| 490 | |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 491 | /* do we want to drop, delay, or forward it? */ |
Manuel Pégourié-Gonnard | eb00bfd | 2014-09-08 11:11:42 +0200 | [diff] [blame] | 492 | if( ( opt.mtu != 0 && |
| 493 | cur.len > (unsigned) opt.mtu ) || |
| 494 | ( opt.drop != 0 && |
| 495 | strcmp( cur.type, "ApplicationData" ) != 0 && |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 496 | ! ( opt.protect_hvr && |
| 497 | strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) && |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 498 | cur.len != (size_t) opt.protect_len && |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 499 | dropped[id] < DROP_MAX && |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 500 | rand() % opt.drop == 0 ) ) |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 501 | { |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 502 | update_dropped( &cur ); |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 503 | } |
Manuel Pégourié-Gonnard | 81f2fe9 | 2014-09-08 10:44:57 +0200 | [diff] [blame] | 504 | else if( ( opt.delay_ccs == 1 && |
| 505 | strcmp( cur.type, "ChangeCipherSpec" ) == 0 ) || |
| 506 | ( opt.delay != 0 && |
| 507 | strcmp( cur.type, "ApplicationData" ) != 0 && |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 508 | ! ( opt.protect_hvr && |
| 509 | strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) && |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 510 | prev.dst == NULL && |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 511 | cur.len != (size_t) opt.protect_len && |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 512 | dropped[id] < DROP_MAX && |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 513 | rand() % opt.delay == 0 ) ) |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 514 | { |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 515 | memcpy( &prev, &cur, sizeof( packet ) ); |
| 516 | } |
| 517 | else |
| 518 | { |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 519 | /* forward and possibly duplicate */ |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 520 | if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 ) |
| 521 | return( ret ); |
| 522 | |
| 523 | /* send previously delayed message if any */ |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 524 | if( prev.dst != NULL ) |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 525 | { |
| 526 | ret = send_packet( &prev, "delayed" ); |
| 527 | memset( &prev, 0, sizeof( packet ) ); |
| 528 | if( ret != 0 ) |
| 529 | return( ret ); |
| 530 | } |
| 531 | } |
| 532 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 533 | return( 0 ); |
| 534 | } |
| 535 | |
| 536 | int main( int argc, char *argv[] ) |
| 537 | { |
Andres Amaya Garcia | 01e3d21 | 2018-04-29 21:58:53 +0100 | [diff] [blame] | 538 | int ret = 1; |
| 539 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 540 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 541 | mbedtls_net_context listen_fd, client_fd, server_fd; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 542 | |
| 543 | int nb_fds; |
| 544 | fd_set read_fds; |
| 545 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 546 | mbedtls_net_init( &listen_fd ); |
| 547 | mbedtls_net_init( &client_fd ); |
| 548 | mbedtls_net_init( &server_fd ); |
| 549 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 550 | get_options( argc, argv ); |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 551 | |
| 552 | /* |
| 553 | * Decisions to drop/delay/duplicate packets are pseudo-random: dropping |
| 554 | * exactly 1 in N packets would lead to problems when a flight has exactly |
| 555 | * N packets: the same packet would be dropped on every resend. |
| 556 | * |
| 557 | * In order to be able to reproduce problems reliably, the seed may be |
| 558 | * specified explicitly. |
| 559 | */ |
| 560 | if( opt.seed == 0 ) |
| 561 | { |
Manuel Pégourié-Gonnard | 9de64f5 | 2015-07-01 15:51:43 +0200 | [diff] [blame] | 562 | opt.seed = (unsigned int) time( NULL ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 563 | mbedtls_printf( " . Pseudo-random seed: %u\n", opt.seed ); |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | srand( opt.seed ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 567 | |
| 568 | /* |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 569 | * 0. "Connect" to the server |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 570 | */ |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 571 | mbedtls_printf( " . Connect to server on UDP/%s/%s ...", |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 572 | opt.server_addr, opt.server_port ); |
| 573 | fflush( stdout ); |
| 574 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 575 | if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port, |
| 576 | MBEDTLS_NET_PROTO_UDP ) ) != 0 ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 577 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 578 | 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] | 579 | goto exit; |
| 580 | } |
| 581 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 582 | mbedtls_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 583 | |
| 584 | /* |
| 585 | * 1. Setup the "listening" UDP socket |
| 586 | */ |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 587 | mbedtls_printf( " . Bind on UDP/%s/%s ...", |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 588 | opt.listen_addr, opt.listen_port ); |
| 589 | fflush( stdout ); |
| 590 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 591 | if( ( ret = mbedtls_net_bind( &listen_fd, opt.listen_addr, opt.listen_port, |
| 592 | MBEDTLS_NET_PROTO_UDP ) ) != 0 ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 593 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 594 | 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] | 595 | goto exit; |
| 596 | } |
| 597 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 598 | mbedtls_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 599 | |
| 600 | /* |
| 601 | * 2. Wait until a client connects |
| 602 | */ |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 603 | accept: |
Manuel Pégourié-Gonnard | abc729e | 2015-07-01 01:28:24 +0200 | [diff] [blame] | 604 | mbedtls_net_free( &client_fd ); |
| 605 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 606 | mbedtls_printf( " . Waiting for a remote connection ..." ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 607 | fflush( stdout ); |
| 608 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 609 | if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd, |
Manuel Pégourié-Gonnard | 0b104b0 | 2015-05-14 21:52:40 +0200 | [diff] [blame] | 610 | NULL, 0, NULL ) ) != 0 ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 611 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 612 | 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] | 613 | goto exit; |
| 614 | } |
| 615 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 616 | mbedtls_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 617 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 618 | /* |
| 619 | * 3. Forward packets forever (kill the process to terminate it) |
| 620 | */ |
Manuel Pégourié-Gonnard | ce8588c | 2014-10-01 00:56:03 +0200 | [diff] [blame] | 621 | clear_pending(); |
| 622 | memset( dropped, 0, sizeof( dropped ) ); |
| 623 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 624 | nb_fds = client_fd.fd; |
| 625 | if( nb_fds < server_fd.fd ) |
| 626 | nb_fds = server_fd.fd; |
| 627 | if( nb_fds < listen_fd.fd ) |
| 628 | nb_fds = listen_fd.fd; |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 629 | ++nb_fds; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 630 | |
| 631 | while( 1 ) |
| 632 | { |
| 633 | FD_ZERO( &read_fds ); |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 634 | FD_SET( server_fd.fd, &read_fds ); |
| 635 | FD_SET( client_fd.fd, &read_fds ); |
| 636 | FD_SET( listen_fd.fd, &read_fds ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 637 | |
| 638 | if( ( ret = select( nb_fds, &read_fds, NULL, NULL, NULL ) ) <= 0 ) |
| 639 | { |
| 640 | perror( "select" ); |
| 641 | goto exit; |
| 642 | } |
| 643 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 644 | if( FD_ISSET( listen_fd.fd, &read_fds ) ) |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 645 | goto accept; |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 646 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 647 | if( FD_ISSET( client_fd.fd, &read_fds ) ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 648 | { |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 649 | if( ( ret = handle_message( "S <- C", |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 650 | &server_fd, &client_fd ) ) != 0 ) |
Manuel Pégourié-Gonnard | ce8588c | 2014-10-01 00:56:03 +0200 | [diff] [blame] | 651 | goto accept; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 652 | } |
| 653 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 654 | if( FD_ISSET( server_fd.fd, &read_fds ) ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 655 | { |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 656 | if( ( ret = handle_message( "S -> C", |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 657 | &client_fd, &server_fd ) ) != 0 ) |
Manuel Pégourié-Gonnard | ce8588c | 2014-10-01 00:56:03 +0200 | [diff] [blame] | 658 | goto accept; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | |
Andres Amaya Garcia | 01e3d21 | 2018-04-29 21:58:53 +0100 | [diff] [blame] | 662 | exit_code = MBEDTLS_EXIT_SUCCESS; |
| 663 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 664 | exit: |
| 665 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 666 | #ifdef MBEDTLS_ERROR_C |
Andres Amaya Garcia | 01e3d21 | 2018-04-29 21:58:53 +0100 | [diff] [blame] | 667 | if( exit_code != MBEDTLS_EXIT_SUCCESS ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 668 | { |
| 669 | char error_buf[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 670 | mbedtls_strerror( ret, error_buf, 100 ); |
| 671 | 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] | 672 | fflush( stdout ); |
| 673 | } |
| 674 | #endif |
| 675 | |
Manuel Pégourié-Gonnard | 3d7d00a | 2015-06-30 15:55:03 +0200 | [diff] [blame] | 676 | mbedtls_net_free( &client_fd ); |
| 677 | mbedtls_net_free( &server_fd ); |
| 678 | mbedtls_net_free( &listen_fd ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 679 | |
| 680 | #if defined(_WIN32) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 681 | mbedtls_printf( " Press Enter to exit this program.\n" ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 682 | fflush( stdout ); getchar(); |
| 683 | #endif |
| 684 | |
Krzysztof Stachowiak | a086522 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 685 | mbedtls_exit( exit_code ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 686 | } |
| 687 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 688 | #endif /* MBEDTLS_NET_C */ |