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