blob: cc0bf79fb18e1540f8c3ee4dfae28347074e06fd [file] [log] [blame]
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001/*
Shaun Case0e7791f2021-12-20 21:14:10 -08002 * UDP proxy: emulate an unreliable UDP connection for DTLS testing
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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é-Gonnardcb4137b2014-09-04 14:55:28 +020018 */
19
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020020/*
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020030#endif
31
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/platform.h"
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000034#else
Simon Butcherd3138c32016-04-27 01:26:50 +010035#include <stdio.h>
36#include <stdlib.h>
David Horstmann0e4a1aa2021-11-29 17:28:13 +000037#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherd3138c32016-04-27 01:26:50 +010038#include <time.h>
Andres Amaya Garcia80081a62018-04-29 21:58:53 +010039#define mbedtls_time time
40#define mbedtls_time_t time_t
David Horstmann0e4a1aa2021-11-29 17:28:13 +000041#endif
Andres Amaya Garcia80081a62018-04-29 21:58:53 +010042#define mbedtls_printf printf
Hanno Becker01ea7782018-08-17 13:33:41 +010043#define mbedtls_calloc calloc
44#define mbedtls_free free
k-stachowiak776521a2019-05-23 09:46:47 +020045#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010046#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia80081a62018-04-29 21:58:53 +010047#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
48#endif /* MBEDTLS_PLATFORM_C */
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#if !defined(MBEDTLS_NET_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010051int main(void)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020052{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010053 mbedtls_printf("MBEDTLS_NET_C not defined.\n");
54 mbedtls_exit(0);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020055}
56#else
57
Andres AG788aa4a2016-09-14 14:32:09 +010058#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/error.h"
60#include "mbedtls/ssl.h"
Hanno Becker0cc77742017-10-31 14:10:07 +000061#include "mbedtls/timing.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020062
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +000063#include <string.h>
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020064
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 Kurek65f93d52022-03-04 15:18:09 -050078#if defined(MBEDTLS_HAVE_TIME)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020079#include <sys/time.h>
Andrzej Kurek65f93d52022-03-04 15:18:09 -050080#endif
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020081#include <sys/types.h>
82#include <unistd.h>
83#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
84
Manuel Pégourié-Gonnardb46780e2014-09-23 12:17:30 +020085#define MAX_MSG_SIZE 16384 + 2048 /* max record/datagram size */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020086
87#define DFL_SERVER_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020088#define DFL_SERVER_PORT "4433"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020089#define DFL_LISTEN_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020090#define DFL_LISTEN_PORT "5556"
Hanno Becker1dd62ea2017-05-22 14:30:59 +010091#define DFL_PACK 0
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020092
Hanno Becker0cc77742017-10-31 14:10:07 +000093#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é-Gonnardcb4137b2014-09-04 14:55:28 +0200101#define USAGE \
102 "\n usage: udp_proxy param=<>...\n" \
103 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200104 " server_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200105 " server_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200106 " listen_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200107 " listen_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200108 "\n" \
109 " duplicate=%%d default: 0 (no duplication)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200110 " duplicate about 1:N packets randomly\n" \
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200111 " delay=%%d default: 0 (no delayed packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200112 " delay about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200113 " delay_ccs=0/1 default: 0 (don't delay ChangeCipherSpec)\n" \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100114 " delay_cli=%%s Handshake message from client that should be\n" \
Hanno Becker01ea7782018-08-17 13:33:41 +0100115 " delayed. Possible values are 'ClientHello',\n" \
116 " 'Certificate', 'CertificateVerify', and\n" \
117 " 'ClientKeyExchange'.\n" \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118 " May be used multiple times, even for the same\n" \
119 " message, in which case the respective message\n" \
Hanno Becker01ea7782018-08-17 13:33:41 +0100120 " gets delayed multiple times.\n" \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121 " 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" \
Hanno Becker01ea7782018-08-17 13:33:41 +0100128 " gets delayed multiple times.\n" \
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200129 " drop=%%d default: 0 (no dropped packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200130 " drop about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200131 " mtu=%%d default: 0 (unlimited)\n" \
132 " drop packets larger than N bytes\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200133 " bad_ad=0/1 default: 0 (don't add bad ApplicationData)\n" \
Hanno Becker98aaf252019-05-24 10:07:42 +0100134 " 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é-Gonnardd0fd1da2014-09-25 17:00:27 +0200137 " protect_hvr=0/1 default: 0 (don't protect HelloVerifyRequest)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000138 " protect_len=%%d default: (don't protect packets of this size)\n" \
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100139 " inject_clihlo=0/1 default: 0 (don't inject fake ClientHello)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200140 "\n" \
141 " seed=%%d default: (use current time)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000142 USAGE_PACK \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200143 "\n"
144
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200145/*
146 * global options
147 */
Hanno Becker01ea7782018-08-17 13:33:41 +0100148
149#define MAX_DELAYED_HS 10
150
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100151static struct options {
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200152 const char *server_addr; /* address to forward packets to */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200153 const char *server_port; /* port to forward packets to */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200154 const char *listen_addr; /* address for accepting client connections */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200155 const char *listen_port; /* port for accepting client connections */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200156
157 int duplicate; /* duplicate 1 in N packets (none if 0) */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200158 int delay; /* delay 1 packet in N (none if 0) */
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200159 int delay_ccs; /* delay ChangeCipherSpec */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 char *delay_cli[MAX_DELAYED_HS]; /* handshake types of messages from
Hanno Becker41038102018-08-28 11:15:32 +0100161 * client that should be delayed. */
Hanno Becker01ea7782018-08-17 13:33:41 +0100162 uint8_t delay_cli_cnt; /* Number of entries in delay_cli. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100163 char *delay_srv[MAX_DELAYED_HS]; /* handshake types of messages from
Hanno Becker41038102018-08-28 11:15:32 +0100164 * server that should be delayed. */
Hanno Becker01ea7782018-08-17 13:33:41 +0100165 uint8_t delay_srv_cnt; /* Number of entries in delay_srv. */
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200166 int drop; /* drop 1 packet in N (none if 0) */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200167 int mtu; /* drop packets larger than this */
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200168 int bad_ad; /* inject corrupted ApplicationData record */
Hanno Becker98aaf252019-05-24 10:07:42 +0100169 unsigned bad_cid; /* inject corrupted CID record */
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200170 int protect_hvr; /* never drop or delay HelloVerifyRequest */
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200171 int protect_len; /* never drop/delay packet of the given size*/
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100172 int inject_clihlo; /* inject fake ClientHello after handshake */
Hanno Becker77abef52017-11-02 10:50:28 +0000173 unsigned pack; /* merge packets into single datagram for
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100174 * at most \c merge milliseconds if > 0 */
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200175 unsigned int seed; /* seed for "random" events */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200176} opt;
177
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178static void exit_usage(const char *name, const char *value)
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200179{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100180 if (value == NULL) {
181 mbedtls_printf(" unknown option or missing value: %s\n", name);
182 } else {
183 mbedtls_printf(" option %s: illegal value: %s\n", name, value);
184 }
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200185
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186 mbedtls_printf(USAGE);
187 mbedtls_exit(1);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200188}
189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100190static void get_options(int argc, char *argv[])
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200191{
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 Becker211f44c2017-10-31 14:08:10 +0000199 opt.pack = DFL_PACK;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200200 /* Other members default to 0 */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200201
Hanno Becker01ea7782018-08-17 13:33:41 +0100202 opt.delay_cli_cnt = 0;
203 opt.delay_srv_cnt = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204 memset(opt.delay_cli, 0, sizeof(opt.delay_cli));
205 memset(opt.delay_srv, 0, sizeof(opt.delay_srv));
Hanno Becker01ea7782018-08-17 13:33:41 +0100206
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207 for (i = 1; i < argc; i++) {
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200208 p = argv[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100209 if ((q = strchr(p, '=')) == NULL) {
210 exit_usage(p, NULL);
211 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200212 *q++ = '\0';
213
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100214 if (strcmp(p, "server_addr") == 0) {
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200215 opt.server_addr = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216 } else if (strcmp(p, "server_port") == 0) {
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200217 opt.server_port = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100218 } else if (strcmp(p, "listen_addr") == 0) {
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200219 opt.listen_addr = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220 } else if (strcmp(p, "listen_port") == 0) {
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200221 opt.listen_port = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100222 } else if (strcmp(p, "duplicate") == 0) {
223 opt.duplicate = atoi(q);
224 if (opt.duplicate < 0 || opt.duplicate > 20) {
225 exit_usage(p, q);
226 }
227 } else if (strcmp(p, "delay") == 0) {
228 opt.delay = atoi(q);
229 if (opt.delay < 0 || opt.delay > 20 || opt.delay == 1) {
230 exit_usage(p, q);
231 }
232 } else if (strcmp(p, "delay_ccs") == 0) {
233 opt.delay_ccs = atoi(q);
234 if (opt.delay_ccs < 0 || opt.delay_ccs > 1) {
235 exit_usage(p, q);
236 }
237 } else if (strcmp(p, "delay_cli") == 0 ||
238 strcmp(p, "delay_srv") == 0) {
Hanno Becker01ea7782018-08-17 13:33:41 +0100239 uint8_t *delay_cnt;
240 char **delay_list;
241 size_t len;
242 char *buf;
243
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244 if (strcmp(p, "delay_cli") == 0) {
Hanno Becker01ea7782018-08-17 13:33:41 +0100245 delay_cnt = &opt.delay_cli_cnt;
246 delay_list = opt.delay_cli;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100247 } else {
Hanno Becker01ea7782018-08-17 13:33:41 +0100248 delay_cnt = &opt.delay_srv_cnt;
249 delay_list = opt.delay_srv;
250 }
251
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100252 if (*delay_cnt == MAX_DELAYED_HS) {
253 mbedtls_printf(" too many uses of %s: only %d allowed\n",
254 p, MAX_DELAYED_HS);
255 exit_usage(p, NULL);
Hanno Becker01ea7782018-08-17 13:33:41 +0100256 }
257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258 len = strlen(q);
259 buf = mbedtls_calloc(1, len + 1);
260 if (buf == NULL) {
261 mbedtls_printf(" Allocation failure\n");
262 exit(1);
Hanno Becker01ea7782018-08-17 13:33:41 +0100263 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 memcpy(buf, q, len + 1);
Hanno Becker01ea7782018-08-17 13:33:41 +0100265
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266 delay_list[(*delay_cnt)++] = buf;
267 } else if (strcmp(p, "drop") == 0) {
268 opt.drop = atoi(q);
269 if (opt.drop < 0 || opt.drop > 20 || opt.drop == 1) {
270 exit_usage(p, q);
271 }
272 } else if (strcmp(p, "pack") == 0) {
Hanno Becker0cc77742017-10-31 14:10:07 +0000273#if defined(MBEDTLS_TIMING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274 opt.pack = (unsigned) atoi(q);
Hanno Becker0cc77742017-10-31 14:10:07 +0000275#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276 mbedtls_printf(" option pack only defined if MBEDTLS_TIMING_C is enabled\n");
277 exit(1);
Hanno Becker0cc77742017-10-31 14:10:07 +0000278#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279 } else if (strcmp(p, "mtu") == 0) {
280 opt.mtu = atoi(q);
281 if (opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE) {
282 exit_usage(p, q);
283 }
284 } else if (strcmp(p, "bad_ad") == 0) {
285 opt.bad_ad = atoi(q);
286 if (opt.bad_ad < 0 || opt.bad_ad > 1) {
287 exit_usage(p, q);
288 }
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200289 }
Hanno Becker98aaf252019-05-24 10:07:42 +0100290#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291 else if (strcmp(p, "bad_cid") == 0) {
292 opt.bad_cid = (unsigned) atoi(q);
Hanno Becker98aaf252019-05-24 10:07:42 +0100293 }
294#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295 else if (strcmp(p, "protect_hvr") == 0) {
296 opt.protect_hvr = atoi(q);
297 if (opt.protect_hvr < 0 || opt.protect_hvr > 1) {
298 exit_usage(p, q);
299 }
300 } else if (strcmp(p, "protect_len") == 0) {
301 opt.protect_len = atoi(q);
302 if (opt.protect_len < 0) {
303 exit_usage(p, q);
304 }
305 } else if (strcmp(p, "inject_clihlo") == 0) {
306 opt.inject_clihlo = atoi(q);
307 if (opt.inject_clihlo < 0 || opt.inject_clihlo > 1) {
308 exit_usage(p, q);
309 }
310 } else if (strcmp(p, "seed") == 0) {
311 opt.seed = atoi(q);
312 if (opt.seed == 0) {
313 exit_usage(p, q);
314 }
315 } else {
316 exit_usage(p, NULL);
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200317 }
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200318 }
319}
320
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100321static const char *msg_type(unsigned char *msg, size_t len)
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200322{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100323 if (len < 1) {
324 return "Invalid";
325 }
326 switch (msg[0]) {
327 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: return "ChangeCipherSpec";
328 case MBEDTLS_SSL_MSG_ALERT: return "Alert";
329 case MBEDTLS_SSL_MSG_APPLICATION_DATA: return "ApplicationData";
330 case MBEDTLS_SSL_MSG_CID: return "CID";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 case MBEDTLS_SSL_MSG_HANDSHAKE: break; /* See below */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100332 default: return "Unknown";
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200333 }
334
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100335 if (len < 13 + 12) {
336 return "Invalid handshake";
337 }
Manuel Pégourié-Gonnard8cc7e032014-09-25 12:59:05 +0200338
339 /*
340 * Our handshake message are less than 2^16 bytes long, so they should
341 * have 0 as the first byte of length, frag_offset and frag_length.
342 * Otherwise, assume they are encrypted.
343 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100344 if (msg[14] || msg[19] || msg[22]) {
345 return "Encrypted handshake";
346 }
Manuel Pégourié-Gonnard8cc7e032014-09-25 12:59:05 +0200347
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100348 switch (msg[13]) {
349 case MBEDTLS_SSL_HS_HELLO_REQUEST: return "HelloRequest";
350 case MBEDTLS_SSL_HS_CLIENT_HELLO: return "ClientHello";
351 case MBEDTLS_SSL_HS_SERVER_HELLO: return "ServerHello";
352 case MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST: return "HelloVerifyRequest";
353 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET: return "NewSessionTicket";
354 case MBEDTLS_SSL_HS_CERTIFICATE: return "Certificate";
355 case MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE: return "ServerKeyExchange";
356 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST: return "CertificateRequest";
357 case MBEDTLS_SSL_HS_SERVER_HELLO_DONE: return "ServerHelloDone";
358 case MBEDTLS_SSL_HS_CERTIFICATE_VERIFY: return "CertificateVerify";
359 case MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE: return "ClientKeyExchange";
360 case MBEDTLS_SSL_HS_FINISHED: return "Finished";
361 default: return "Unknown handshake";
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200362 }
363}
364
Hanno Becker0cc77742017-10-31 14:10:07 +0000365#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200366/* Return elapsed time in milliseconds since the first call */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100367static unsigned elapsed_time(void)
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200368{
Hanno Becker92474da2017-10-31 14:09:30 +0000369 static int initialized = 0;
370 static struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200371
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372 if (initialized == 0) {
373 (void) mbedtls_timing_get_timer(&hires, 1);
Hanno Becker92474da2017-10-31 14:09:30 +0000374 initialized = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100375 return 0;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200376 }
377
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100378 return mbedtls_timing_get_timer(&hires, 0);
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200379}
380
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100381typedef struct {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100382 mbedtls_net_context *ctx;
383
384 const char *description;
385
Hanno Becker77abef52017-11-02 10:50:28 +0000386 unsigned packet_lifetime;
387 unsigned num_datagrams;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100388
389 unsigned char data[MAX_MSG_SIZE];
Hanno Beckera5e68972017-12-06 08:35:02 +0000390 size_t len;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100391
392} ctx_buffer;
393
394static ctx_buffer outbuf[2];
395
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100396static int ctx_buffer_flush(ctx_buffer *buf)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100397{
398 int ret;
399
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100400 mbedtls_printf(" %05u flush %s: %u bytes, %u datagrams, last %u ms\n",
401 elapsed_time(), buf->description,
402 (unsigned) buf->len, buf->num_datagrams,
403 elapsed_time() - buf->packet_lifetime);
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100404
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100405 ret = mbedtls_net_send(buf->ctx, buf->data, buf->len);
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100406
407 buf->len = 0;
408 buf->num_datagrams = 0;
409
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100410 return ret;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100411}
412
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100413static unsigned ctx_buffer_time_remaining(ctx_buffer *buf)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100414{
Tom Cosgrove49f99bc2022-12-04 16:44:21 +0000415 unsigned const cur_time = elapsed_time();
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100416
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100417 if (buf->num_datagrams == 0) {
418 return (unsigned) -1;
419 }
Hanno Becker77abef52017-11-02 10:50:28 +0000420
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100421 if (cur_time - buf->packet_lifetime >= opt.pack) {
422 return 0;
423 }
Hanno Becker77abef52017-11-02 10:50:28 +0000424
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100425 return opt.pack - (cur_time - buf->packet_lifetime);
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100426}
427
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100428static int ctx_buffer_append(ctx_buffer *buf,
429 const unsigned char *data,
430 size_t len)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100431{
432 int ret;
433
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100434 if (len > (size_t) INT_MAX) {
435 return -1;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100436 }
437
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100438 if (len > sizeof(buf->data)) {
439 mbedtls_printf(" ! buffer size %u too large (max %u)\n",
440 (unsigned) len, (unsigned) sizeof(buf->data));
441 return -1;
442 }
443
444 if (sizeof(buf->data) - buf->len < len) {
445 if ((ret = ctx_buffer_flush(buf)) <= 0) {
446 mbedtls_printf("ctx_buffer_flush failed with -%#04x", (unsigned int) -ret);
447 return ret;
Hanno Becker31f6e372019-05-08 15:36:31 +0100448 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100449 }
450
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100451 memcpy(buf->data + buf->len, data, len);
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100452
453 buf->len += len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100454 if (++buf->num_datagrams == 1) {
Tom Cosgrove49f99bc2022-12-04 16:44:21 +0000455 buf->packet_lifetime = elapsed_time();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100456 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100457
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100458 return (int) len;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100459}
Hanno Becker77abef52017-11-02 10:50:28 +0000460#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100461
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100462static int dispatch_data(mbedtls_net_context *ctx,
463 const unsigned char *data,
464 size_t len)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100465{
Hanno Becker31f6e372019-05-08 15:36:31 +0100466 int ret;
Hanno Becker77abef52017-11-02 10:50:28 +0000467#if defined(MBEDTLS_TIMING_C)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100468 ctx_buffer *buf = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100469 if (opt.pack > 0) {
470 if (outbuf[0].ctx == ctx) {
Hanno Becker77abef52017-11-02 10:50:28 +0000471 buf = &outbuf[0];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100472 } else if (outbuf[1].ctx == ctx) {
Hanno Becker77abef52017-11-02 10:50:28 +0000473 buf = &outbuf[1];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100474 }
Hanno Becker0cc77742017-10-31 14:10:07 +0000475
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100476 if (buf == NULL) {
477 return -1;
478 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100479
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100480 return ctx_buffer_append(buf, data, len);
Hanno Becker77abef52017-11-02 10:50:28 +0000481 }
482#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100483
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100484 ret = mbedtls_net_send(ctx, data, len);
485 if (ret < 0) {
486 mbedtls_printf("net_send returned -%#04x\n", (unsigned int) -ret);
Hanno Becker31f6e372019-05-08 15:36:31 +0100487 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100488 return ret;
Hanno Becker0cc77742017-10-31 14:10:07 +0000489}
490
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100491typedef struct {
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200492 mbedtls_net_context *dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200493 const char *way;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200494 const char *type;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200495 unsigned len;
496 unsigned char buf[MAX_MSG_SIZE];
497} packet;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200498
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200499/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100500void print_packet(const packet *p, const char *why)
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200501{
Hanno Becker0cc77742017-10-31 14:10:07 +0000502#if defined(MBEDTLS_TIMING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100503 if (why == NULL) {
504 mbedtls_printf(" %05u dispatch %s %s (%u bytes)\n",
505 elapsed_time(), p->way, p->type, p->len);
506 } else {
507 mbedtls_printf(" %05u dispatch %s %s (%u bytes): %s\n",
508 elapsed_time(), p->way, p->type, p->len, why);
509 }
Hanno Becker0cc77742017-10-31 14:10:07 +0000510#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100511 if (why == NULL) {
512 mbedtls_printf(" dispatch %s %s (%u bytes)\n",
513 p->way, p->type, p->len);
514 } else {
515 mbedtls_printf(" dispatch %s %s (%u bytes): %s\n",
516 p->way, p->type, p->len, why);
517 }
Hanno Becker0cc77742017-10-31 14:10:07 +0000518#endif
519
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100520 fflush(stdout);
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200521}
522
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100523/*
524 * In order to test the server's behaviour when receiving a ClientHello after
525 * the connection is established (this could be a hard reset from the client,
526 * but the server must not drop the existing connection before establishing
527 * client reachability, see RFC 6347 Section 4.2.8), we memorize the first
528 * ClientHello we see (which can't have a cookie), then replay it after the
529 * first ApplicationData record - then we're done.
530 *
531 * This is controlled by the inject_clihlo option.
532 *
533 * We want an explicit state and a place to store the packet.
534 */
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200535typedef enum {
536 ICH_INIT, /* haven't seen the first ClientHello yet */
537 ICH_CACHED, /* cached the initial ClientHello */
538 ICH_INJECTED, /* ClientHello already injected, done */
539} inject_clihlo_state_t;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100540
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200541static inject_clihlo_state_t inject_clihlo_state;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100542static packet initial_clihlo;
543
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100544int send_packet(const packet *p, const char *why)
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200545{
546 int ret;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200547 mbedtls_net_context *dst = p->dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200548
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100549 /* save initial ClientHello? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100550 if (opt.inject_clihlo != 0 &&
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200551 inject_clihlo_state == ICH_INIT &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100552 strcmp(p->type, "ClientHello") == 0) {
553 memcpy(&initial_clihlo, p, sizeof(packet));
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200554 inject_clihlo_state = ICH_CACHED;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100555 }
556
Hanno Becker98aaf252019-05-24 10:07:42 +0100557 /* insert corrupted CID record? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100558 if (opt.bad_cid != 0 &&
559 strcmp(p->type, "CID") == 0 &&
560 (rand() % opt.bad_cid) == 0) {
Hanno Becker98aaf252019-05-24 10:07:42 +0100561 unsigned char buf[MAX_MSG_SIZE];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100562 memcpy(buf, p->buf, p->len);
Hanno Becker98aaf252019-05-24 10:07:42 +0100563
564 /* The CID resides at offset 11 in the DTLS record header. */
565 buf[11] ^= 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100566 print_packet(p, "modified CID");
Hanno Becker98aaf252019-05-24 10:07:42 +0100567
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100568 if ((ret = dispatch_data(dst, buf, p->len)) <= 0) {
569 mbedtls_printf(" ! dispatch returned %d\n", ret);
570 return ret;
Hanno Becker98aaf252019-05-24 10:07:42 +0100571 }
572 }
573
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200574 /* insert corrupted ApplicationData record? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100575 if (opt.bad_ad &&
576 strcmp(p->type, "ApplicationData") == 0) {
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200577 unsigned char buf[MAX_MSG_SIZE];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100578 memcpy(buf, p->buf, p->len);
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200579
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100580 if (p->len <= 13) {
581 mbedtls_printf(" ! can't corrupt empty AD record");
582 } else {
Hanno Beckerfbb0b702017-05-26 16:55:07 +0100583 ++buf[13];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100584 print_packet(p, "corrupted");
Hanno Beckerfbb0b702017-05-26 16:55:07 +0100585 }
586
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100587 if ((ret = dispatch_data(dst, buf, p->len)) <= 0) {
588 mbedtls_printf(" ! dispatch returned %d\n", ret);
589 return ret;
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200590 }
591 }
592
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100593 print_packet(p, why);
594 if ((ret = dispatch_data(dst, p->buf, p->len)) <= 0) {
595 mbedtls_printf(" ! dispatch returned %d\n", ret);
596 return ret;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200597 }
598
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200599 /* Don't duplicate Application Data, only handshake covered */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100600 if (opt.duplicate != 0 &&
601 strcmp(p->type, "ApplicationData") != 0 &&
602 rand() % opt.duplicate == 0) {
603 print_packet(p, "duplicated");
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200604
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100605 if ((ret = dispatch_data(dst, p->buf, p->len)) <= 0) {
606 mbedtls_printf(" ! dispatch returned %d\n", ret);
607 return ret;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200608 }
609 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200610
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100611 /* Inject ClientHello after first ApplicationData */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100612 if (opt.inject_clihlo != 0 &&
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200613 inject_clihlo_state == ICH_CACHED &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100614 strcmp(p->type, "ApplicationData") == 0) {
615 print_packet(&initial_clihlo, "injected");
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100616
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100617 if ((ret = dispatch_data(dst, initial_clihlo.buf,
618 initial_clihlo.len)) <= 0) {
619 mbedtls_printf(" ! dispatch returned %d\n", ret);
620 return ret;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100621 }
622
Manuel Pégourié-Gonnardf4563b42020-03-30 12:46:21 +0200623 inject_clihlo_state = ICH_INJECTED;
Manuel Pégourié-Gonnardbaad2de2020-03-13 11:11:02 +0100624 }
625
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100626 return 0;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200627}
628
Hanno Becker101bcba2018-08-21 16:39:51 +0100629#define MAX_DELAYED_MSG 5
630static size_t prev_len;
631static packet prev[MAX_DELAYED_MSG];
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200632
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100633void clear_pending(void)
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200634{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100635 memset(&prev, 0, sizeof(prev));
Hanno Becker101bcba2018-08-21 16:39:51 +0100636 prev_len = 0;
637}
638
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100639void delay_packet(packet *delay)
Hanno Becker101bcba2018-08-21 16:39:51 +0100640{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100641 if (prev_len == MAX_DELAYED_MSG) {
Hanno Becker101bcba2018-08-21 16:39:51 +0100642 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100643 }
Hanno Becker101bcba2018-08-21 16:39:51 +0100644
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100645 memcpy(&prev[prev_len++], delay, sizeof(packet));
Hanno Becker101bcba2018-08-21 16:39:51 +0100646}
647
648int send_delayed()
649{
650 uint8_t offset;
651 int ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100652 for (offset = 0; offset < prev_len; offset++) {
653 ret = send_packet(&prev[offset], "delayed");
654 if (ret != 0) {
655 return ret;
656 }
Hanno Becker101bcba2018-08-21 16:39:51 +0100657 }
658
659 clear_pending();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100660 return 0;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200661}
662
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200663/*
Manuel Pégourié-Gonnard14134282021-07-06 12:39:43 +0200664 * Avoid dropping or delaying a packet that was already dropped or delayed
665 * ("held") twice: this only results in uninteresting timeouts. We can't rely
666 * on type to identify packets, since during renegotiation they're all
667 * encrypted. So, rely on size mod 2048 (which is usually just size).
668 *
669 * We only hold packets at the level of entire datagrams, not at the level
Hanno Becker961e6772019-06-04 13:04:28 +0100670 * of records. In particular, if the peer changes the way it packs multiple
671 * records into a single datagram, we don't necessarily count the number of
Manuel Pégourié-Gonnard14134282021-07-06 12:39:43 +0200672 * times a record has been held correctly. However, the only known reason
Hanno Becker961e6772019-06-04 13:04:28 +0100673 * why a peer would change datagram packing is disabling the latter on
Manuel Pégourié-Gonnard14134282021-07-06 12:39:43 +0200674 * retransmission, in which case we'd hold involved records at most
675 * HOLD_MAX + 1 times.
676 */
677static unsigned char held[2048] = { 0 };
678#define HOLD_MAX 2
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200679
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100680int handle_message(const char *way,
681 mbedtls_net_context *dst,
682 mbedtls_net_context *src)
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200683{
684 int ret;
685 packet cur;
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200686 size_t id;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200687
Hanno Becker01ea7782018-08-17 13:33:41 +0100688 uint8_t delay_idx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100689 char **delay_list;
Hanno Becker01ea7782018-08-17 13:33:41 +0100690 uint8_t delay_list_len;
691
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +0200692 /* receive packet */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100693 if ((ret = mbedtls_net_recv(src, cur.buf, sizeof(cur.buf))) <= 0) {
694 mbedtls_printf(" ! mbedtls_net_recv returned %d\n", ret);
695 return ret;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200696 }
697
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200698 cur.len = ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100699 cur.type = msg_type(cur.buf, cur.len);
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200700 cur.way = way;
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200701 cur.dst = dst;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100702 print_packet(&cur, NULL);
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200703
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100704 id = cur.len % sizeof(held);
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200705
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100706 if (strcmp(way, "S <- C") == 0) {
Hanno Becker01ea7782018-08-17 13:33:41 +0100707 delay_list = opt.delay_cli;
708 delay_list_len = opt.delay_cli_cnt;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100709 } else {
Hanno Becker01ea7782018-08-17 13:33:41 +0100710 delay_list = opt.delay_srv;
711 delay_list_len = opt.delay_srv_cnt;
712 }
Hanno Beckercf469452018-08-28 10:09:47 +0100713
Hanno Becker01ea7782018-08-17 13:33:41 +0100714 /* Check if message type is in the list of messages
715 * that should be delayed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100716 for (delay_idx = 0; delay_idx < delay_list_len; delay_idx++) {
717 if (delay_list[delay_idx] == NULL) {
Hanno Becker01ea7782018-08-17 13:33:41 +0100718 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100719 }
Hanno Becker01ea7782018-08-17 13:33:41 +0100720
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100721 if (strcmp(delay_list[delay_idx], cur.type) == 0) {
Hanno Becker01ea7782018-08-17 13:33:41 +0100722 /* Delay message */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100723 delay_packet(&cur);
Hanno Becker01ea7782018-08-17 13:33:41 +0100724
725 /* Remove entry from list */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100726 mbedtls_free(delay_list[delay_idx]);
Hanno Becker01ea7782018-08-17 13:33:41 +0100727 delay_list[delay_idx] = NULL;
728
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100729 return 0;
Hanno Becker01ea7782018-08-17 13:33:41 +0100730 }
731 }
732
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200733 /* do we want to drop, delay, or forward it? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100734 if ((opt.mtu != 0 &&
735 cur.len > (unsigned) opt.mtu) ||
736 (opt.drop != 0 &&
737 strcmp(cur.type, "CID") != 0 &&
738 strcmp(cur.type, "ApplicationData") != 0 &&
739 !(opt.protect_hvr &&
740 strcmp(cur.type, "HelloVerifyRequest") == 0) &&
741 cur.len != (size_t) opt.protect_len &&
742 held[id] < HOLD_MAX &&
743 rand() % opt.drop == 0)) {
Manuel Pégourié-Gonnard14134282021-07-06 12:39:43 +0200744 ++held[id];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100745 } else if ((opt.delay_ccs == 1 &&
746 strcmp(cur.type, "ChangeCipherSpec") == 0) ||
747 (opt.delay != 0 &&
748 strcmp(cur.type, "CID") != 0 &&
749 strcmp(cur.type, "ApplicationData") != 0 &&
750 !(opt.protect_hvr &&
751 strcmp(cur.type, "HelloVerifyRequest") == 0) &&
752 cur.len != (size_t) opt.protect_len &&
753 held[id] < HOLD_MAX &&
754 rand() % opt.delay == 0)) {
Manuel Pégourié-Gonnard14134282021-07-06 12:39:43 +0200755 ++held[id];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100756 delay_packet(&cur);
757 } else {
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200758 /* forward and possibly duplicate */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100759 if ((ret = send_packet(&cur, "forwarded")) != 0) {
760 return ret;
761 }
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200762
Hanno Becker101bcba2018-08-21 16:39:51 +0100763 /* send previously delayed messages if any */
764 ret = send_delayed();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100765 if (ret != 0) {
766 return ret;
767 }
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200768 }
769
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100770 return 0;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200771}
772
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100773int main(int argc, char *argv[])
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200774{
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100775 int ret = 1;
776 int exit_code = MBEDTLS_EXIT_FAILURE;
Hanno Becker01ea7782018-08-17 13:33:41 +0100777 uint8_t delay_idx;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200778
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200779 mbedtls_net_context listen_fd, client_fd, server_fd;
Hanno Becker77abef52017-11-02 10:50:28 +0000780
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100781#if defined(MBEDTLS_TIMING_C)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100782 struct timeval tm;
Hanno Becker77abef52017-11-02 10:50:28 +0000783#endif
784
785 struct timeval *tm_ptr = NULL;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200786
787 int nb_fds;
788 fd_set read_fds;
789
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100790 mbedtls_net_init(&listen_fd);
791 mbedtls_net_init(&client_fd);
792 mbedtls_net_init(&server_fd);
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200793
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100794 get_options(argc, argv);
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200795
796 /*
797 * Decisions to drop/delay/duplicate packets are pseudo-random: dropping
798 * exactly 1 in N packets would lead to problems when a flight has exactly
799 * N packets: the same packet would be dropped on every resend.
800 *
801 * In order to be able to reproduce problems reliably, the seed may be
802 * specified explicitly.
803 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100804 if (opt.seed == 0) {
Gilles Peskine7ece7682022-04-05 21:39:43 +0200805#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100806 opt.seed = (unsigned int) mbedtls_time(NULL);
Gilles Peskine7ece7682022-04-05 21:39:43 +0200807#else
808 opt.seed = 1;
809#endif /* MBEDTLS_HAVE_TIME */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100810 mbedtls_printf(" . Pseudo-random seed: %u\n", opt.seed);
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200811 }
812
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100813 srand(opt.seed);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200814
815 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200816 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200817 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100818 mbedtls_printf(" . Connect to server on UDP/%s/%s ...",
819 opt.server_addr, opt.server_port);
820 fflush(stdout);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200821
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100822 if ((ret = mbedtls_net_connect(&server_fd, opt.server_addr, opt.server_port,
823 MBEDTLS_NET_PROTO_UDP)) != 0) {
824 mbedtls_printf(" failed\n ! mbedtls_net_connect returned %d\n\n", ret);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200825 goto exit;
826 }
827
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100828 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200829
830 /*
831 * 1. Setup the "listening" UDP socket
832 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100833 mbedtls_printf(" . Bind on UDP/%s/%s ...",
834 opt.listen_addr, opt.listen_port);
835 fflush(stdout);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200836
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100837 if ((ret = mbedtls_net_bind(&listen_fd, opt.listen_addr, opt.listen_port,
838 MBEDTLS_NET_PROTO_UDP)) != 0) {
839 mbedtls_printf(" failed\n ! mbedtls_net_bind returned %d\n\n", ret);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200840 goto exit;
841 }
842
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100843 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200844
845 /*
846 * 2. Wait until a client connects
847 */
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200848accept:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100849 mbedtls_net_free(&client_fd);
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200850
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100851 mbedtls_printf(" . Waiting for a remote connection ...");
852 fflush(stdout);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200853
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100854 if ((ret = mbedtls_net_accept(&listen_fd, &client_fd,
855 NULL, 0, NULL)) != 0) {
856 mbedtls_printf(" failed\n ! mbedtls_net_accept returned %d\n\n", ret);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200857 goto exit;
858 }
859
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100860 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200861
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200862 /*
863 * 3. Forward packets forever (kill the process to terminate it)
864 */
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200865 clear_pending();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100866 memset(held, 0, sizeof(held));
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200867
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200868 nb_fds = client_fd.fd;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100869 if (nb_fds < server_fd.fd) {
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200870 nb_fds = server_fd.fd;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100871 }
872 if (nb_fds < listen_fd.fd) {
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200873 nb_fds = listen_fd.fd;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100874 }
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200875 ++nb_fds;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200876
Hanno Becker0cc77742017-10-31 14:10:07 +0000877#if defined(MBEDTLS_TIMING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100878 if (opt.pack > 0) {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100879 outbuf[0].ctx = &server_fd;
880 outbuf[0].description = "S <- C";
881 outbuf[0].num_datagrams = 0;
882 outbuf[0].len = 0;
883
884 outbuf[1].ctx = &client_fd;
885 outbuf[1].description = "S -> C";
886 outbuf[1].num_datagrams = 0;
887 outbuf[1].len = 0;
888 }
Hanno Becker0cc77742017-10-31 14:10:07 +0000889#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100890
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100891 while (1) {
Hanno Becker77abef52017-11-02 10:50:28 +0000892#if defined(MBEDTLS_TIMING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100893 if (opt.pack > 0) {
Hanno Becker77abef52017-11-02 10:50:28 +0000894 unsigned max_wait_server, max_wait_client, max_wait;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100895 max_wait_server = ctx_buffer_time_remaining(&outbuf[0]);
896 max_wait_client = ctx_buffer_time_remaining(&outbuf[1]);
Hanno Becker77abef52017-11-02 10:50:28 +0000897
898 max_wait = (unsigned) -1;
899
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100900 if (max_wait_server == 0) {
901 ctx_buffer_flush(&outbuf[0]);
902 } else {
Hanno Becker77abef52017-11-02 10:50:28 +0000903 max_wait = max_wait_server;
Hanno Becker77abef52017-11-02 10:50:28 +0000904 }
905
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100906 if (max_wait_client == 0) {
907 ctx_buffer_flush(&outbuf[1]);
908 } else {
909 if (max_wait_client < max_wait) {
910 max_wait = max_wait_client;
911 }
912 }
913
914 if (max_wait != (unsigned) -1) {
Hanno Becker77abef52017-11-02 10:50:28 +0000915 tm.tv_sec = max_wait / 1000;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100916 tm.tv_usec = (max_wait % 1000) * 1000;
Hanno Becker77abef52017-11-02 10:50:28 +0000917
918 tm_ptr = &tm;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100919 } else {
Hanno Becker77abef52017-11-02 10:50:28 +0000920 tm_ptr = NULL;
921 }
922 }
923#endif /* MBEDTLS_TIMING_C */
924
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100925 FD_ZERO(&read_fds);
926 FD_SET(server_fd.fd, &read_fds);
927 FD_SET(client_fd.fd, &read_fds);
928 FD_SET(listen_fd.fd, &read_fds);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200929
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100930 if ((ret = select(nb_fds, &read_fds, NULL, NULL, tm_ptr)) < 0) {
931 perror("select");
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200932 goto exit;
933 }
934
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100935 if (FD_ISSET(listen_fd.fd, &read_fds)) {
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200936 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200937 }
938
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100939 if (FD_ISSET(client_fd.fd, &read_fds)) {
940 if ((ret = handle_message("S <- C",
941 &server_fd, &client_fd)) != 0) {
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200942 goto accept;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100943 }
944 }
945
946 if (FD_ISSET(server_fd.fd, &read_fds)) {
947 if ((ret = handle_message("S -> C",
948 &client_fd, &server_fd)) != 0) {
949 goto accept;
950 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200951 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100952
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200953 }
954
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100955 exit_code = MBEDTLS_EXIT_SUCCESS;
956
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200957exit:
958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959#ifdef MBEDTLS_ERROR_C
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100960 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200961 char error_buf[100];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100962 mbedtls_strerror(ret, error_buf, 100);
963 mbedtls_printf("Last error was: -0x%04X - %s\n\n", (unsigned int) -ret, error_buf);
964 fflush(stdout);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200965 }
966#endif
967
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100968 for (delay_idx = 0; delay_idx < MAX_DELAYED_HS; delay_idx++) {
969 mbedtls_free(opt.delay_cli[delay_idx]);
970 mbedtls_free(opt.delay_srv[delay_idx]);
Hanno Becker01ea7782018-08-17 13:33:41 +0100971 }
972
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100973 mbedtls_net_free(&client_fd);
974 mbedtls_net_free(&server_fd);
975 mbedtls_net_free(&listen_fd);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200976
977#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100978 mbedtls_printf(" Press Enter to exit this program.\n");
979 fflush(stdout); getchar();
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200980#endif
981
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100982 mbedtls_exit(exit_code);
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200983}
984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985#endif /* MBEDTLS_NET_C */