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