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