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