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