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