mbedtls/programs/test/udp_proxy.c

631 lines
19 KiB
C
Raw Normal View History

/*
* UDP proxy: emulate an unreliable UDP connexion for DTLS testing
*
* Copyright (C) 2014-2015, ARM Limited, All Rights Reserved
*
2015-03-06 13:40:52 +00:00
* This file is part of mbed TLS (https://tls.mbed.org)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
2015-06-30 13:40:39 +00:00
/*
* Warning: this is an internal utility program we use for tests.
* It does break some abstractions from the NET layer, and is thus NOT an
* example of good general usage.
*/
#if !defined(MBEDTLS_CONFIG_FILE)
2015-03-09 17:05:11 +00:00
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#if defined(MBEDTLS_PLATFORM_C)
2015-03-09 17:05:11 +00:00
#include "mbedtls/platform.h"
2015-01-29 13:29:20 +00:00
#else
#define mbedtls_printf printf
2015-01-29 13:29:20 +00:00
#endif
#if !defined(MBEDTLS_NET_C)
#include <stdio.h>
int main( void )
{
mbedtls_printf( "MBEDTLS_NET_C not defined.\n" );
return( 0 );
}
#else
2015-03-09 17:05:11 +00:00
#include "mbedtls/net.h"
#include "mbedtls/error.h"
#include "mbedtls/ssl.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2014-09-06 06:14:47 +00:00
#include <time.h>
/* For select() */
#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
!defined(EFI32)
#include <winsock2.h>
#include <windows.h>
#if defined(_MSC_VER)
#if defined(_WIN32_WCE)
#pragma comment( lib, "ws2.lib" )
#else
#pragma comment( lib, "ws2_32.lib" )
#endif
#endif /* _MSC_VER */
#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
/* For gettimeofday() */
#if !defined(_WIN32)
#include <sys/time.h>
#endif
2014-09-23 10:17:30 +00:00
#define MAX_MSG_SIZE 16384 + 2048 /* max record/datagram size */
#define DFL_SERVER_ADDR "localhost"
#define DFL_SERVER_PORT "4433"
#define DFL_LISTEN_ADDR "localhost"
#define DFL_LISTEN_PORT "5556"
2014-09-06 06:07:45 +00:00
#define USAGE \
"\n usage: udp_proxy param=<>...\n" \
"\n acceptable parameters:\n" \
2014-09-25 15:00:27 +00:00
" server_addr=%%s default: localhost\n" \
2014-09-06 06:07:45 +00:00
" server_port=%%d default: 4433\n" \
2014-09-25 15:00:27 +00:00
" listen_addr=%%s default: localhost\n" \
2014-09-06 06:07:45 +00:00
" listen_port=%%d default: 4433\n" \
2014-09-06 06:14:47 +00:00
"\n" \
" duplicate=%%d default: 0 (no duplication)\n" \
" duplicate about 1:N packets randomly\n" \
2014-09-06 12:36:46 +00:00
" delay=%%d default: 0 (no delayed packets)\n" \
" delay about 1:N packets randomly\n" \
2014-09-25 15:00:27 +00:00
" delay_ccs=0/1 default: 0 (don't delay ChangeCipherSpec)\n" \
2014-09-06 12:49:52 +00:00
" drop=%%d default: 0 (no dropped packets)\n" \
" drop about 1:N packets randomly\n" \
2014-09-08 09:11:42 +00:00
" mtu=%%d default: 0 (unlimited)\n" \
" drop packets larger than N bytes\n" \
2014-09-25 15:00:27 +00:00
" bad_ad=0/1 default: 0 (don't add bad ApplicationData)\n" \
" protect_hvr=0/1 default: 0 (don't protect HelloVerifyRequest)\n" \
" protect_len=%%d default: (don't protect packets of this size)\n" \
"\n" \
" seed=%%d default: (use current time)\n" \
2014-09-06 06:07:45 +00:00
"\n"
/*
* global options
*/
2014-09-06 06:07:45 +00:00
static struct options
{
const char *server_addr; /* address to forward packets to */
const char *server_port; /* port to forward packets to */
const char *listen_addr; /* address for accepting client connections */
const char *listen_port; /* port for accepting client connections */
2014-09-06 06:14:47 +00:00
int duplicate; /* duplicate 1 in N packets (none if 0) */
2014-09-06 12:36:46 +00:00
int delay; /* delay 1 packet in N (none if 0) */
2014-09-08 08:44:57 +00:00
int delay_ccs; /* delay ChangeCipherSpec */
2014-09-06 12:49:52 +00:00
int drop; /* drop 1 packet in N (none if 0) */
2014-09-08 09:11:42 +00:00
int mtu; /* drop packets larger than this */
2014-09-08 09:24:58 +00:00
int bad_ad; /* inject corrupted ApplicationData record */
2014-09-25 15:00:27 +00:00
int protect_hvr; /* never drop or delay HelloVerifyRequest */
int protect_len; /* never drop/delay packet of the given size*/
2014-09-06 06:14:47 +00:00
unsigned int seed; /* seed for "random" events */
} opt;
2014-09-06 06:14:47 +00:00
2014-09-06 06:07:45 +00:00
static void exit_usage( const char *name, const char *value )
{
2014-09-06 06:07:45 +00:00
if( value == NULL )
mbedtls_printf( " unknown option or missing value: %s\n", name );
2014-09-06 06:07:45 +00:00
else
mbedtls_printf( " option %s: illegal value: %s\n", name, value );
2014-09-06 06:07:45 +00:00
mbedtls_printf( USAGE );
exit( 1 );
}
static void get_options( int argc, char *argv[] )
{
int i;
char *p, *q;
opt.server_addr = DFL_SERVER_ADDR;
opt.server_port = DFL_SERVER_PORT;
opt.listen_addr = DFL_LISTEN_ADDR;
opt.listen_port = DFL_LISTEN_PORT;
2014-09-06 12:49:52 +00:00
/* Other members default to 0 */
for( i = 1; i < argc; i++ )
{
p = argv[i];
if( ( q = strchr( p, '=' ) ) == NULL )
2014-09-06 06:07:45 +00:00
exit_usage( p, NULL );
*q++ = '\0';
if( strcmp( p, "server_addr" ) == 0 )
opt.server_addr = q;
else if( strcmp( p, "server_port" ) == 0 )
opt.server_port = q;
else if( strcmp( p, "listen_addr" ) == 0 )
opt.listen_addr = q;
else if( strcmp( p, "listen_port" ) == 0 )
opt.listen_port = q;
2014-09-06 06:14:47 +00:00
else if( strcmp( p, "duplicate" ) == 0 )
{
opt.duplicate = atoi( q );
if( opt.duplicate < 0 || opt.duplicate > 20 )
2014-09-06 06:14:47 +00:00
exit_usage( p, q );
}
2014-09-06 12:36:46 +00:00
else if( strcmp( p, "delay" ) == 0 )
{
opt.delay = atoi( q );
if( opt.delay < 0 || opt.delay > 20 || opt.delay == 1 )
2014-09-06 12:36:46 +00:00
exit_usage( p, q );
}
2014-09-08 08:44:57 +00:00
else if( strcmp( p, "delay_ccs" ) == 0 )
{
opt.delay_ccs = atoi( q );
if( opt.delay_ccs < 0 || opt.delay_ccs > 1 )
exit_usage( p, q );
}
2014-09-06 12:49:52 +00:00
else if( strcmp( p, "drop" ) == 0 )
{
opt.drop = atoi( q );
if( opt.drop < 0 || opt.drop > 20 || opt.drop == 1 )
2014-09-06 12:49:52 +00:00
exit_usage( p, q );
}
2014-09-08 09:11:42 +00:00
else if( strcmp( p, "mtu" ) == 0 )
{
opt.mtu = atoi( q );
if( opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE )
exit_usage( p, q );
}
2014-09-08 09:24:58 +00:00
else if( strcmp( p, "bad_ad" ) == 0 )
{
opt.bad_ad = atoi( q );
if( opt.bad_ad < 0 || opt.bad_ad > 1 )
exit_usage( p, q );
}
2014-09-25 15:00:27 +00:00
else if( strcmp( p, "protect_hvr" ) == 0 )
{
opt.protect_hvr = atoi( q );
if( opt.protect_hvr < 0 || opt.protect_hvr > 1 )
exit_usage( p, q );
}
else if( strcmp( p, "protect_len" ) == 0 )
{
opt.protect_len = atoi( q );
if( opt.protect_len < 0 )
exit_usage( p, q );
}
else if( strcmp( p, "seed" ) == 0 )
{
opt.seed = atoi( q );
if( opt.seed == 0 )
exit_usage( p, q );
}
else
2014-09-06 06:07:45 +00:00
exit_usage( p, NULL );
}
}
static const char *msg_type( unsigned char *msg, size_t len )
{
if( len < 1 ) return( "Invalid" );
switch( msg[0] )
{
case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" );
case MBEDTLS_SSL_MSG_ALERT: return( "Alert" );
case MBEDTLS_SSL_MSG_APPLICATION_DATA: return( "ApplicationData" );
case MBEDTLS_SSL_MSG_HANDSHAKE: break; /* See below */
2014-09-06 06:07:45 +00:00
default: return( "Unknown" );
}
if( len < 13 + 12 ) return( "Invalid handshake" );
/*
* Our handshake message are less than 2^16 bytes long, so they should
* have 0 as the first byte of length, frag_offset and frag_length.
* Otherwise, assume they are encrypted.
*/
if( msg[14] || msg[19] || msg[22] ) return( "Encrypted handshake" );
2014-09-06 06:07:45 +00:00
switch( msg[13] )
{
case MBEDTLS_SSL_HS_HELLO_REQUEST: return( "HelloRequest" );
case MBEDTLS_SSL_HS_CLIENT_HELLO: return( "ClientHello" );
case MBEDTLS_SSL_HS_SERVER_HELLO: return( "ServerHello" );
case MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" );
case MBEDTLS_SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" );
case MBEDTLS_SSL_HS_CERTIFICATE: return( "Certificate" );
case MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" );
case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" );
case MBEDTLS_SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" );
case MBEDTLS_SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" );
case MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" );
case MBEDTLS_SSL_HS_FINISHED: return( "Finished" );
default: return( "Unknown handshake" );
}
}
/* Return elapsed time in milliseconds since the first call */
static unsigned long ellapsed_time( void )
{
#if defined(_WIN32)
return( 0 );
#else
static struct timeval ref = { 0, 0 };
struct timeval now;
if( ref.tv_sec == 0 && ref.tv_usec == 0 )
{
gettimeofday( &ref, NULL );
return( 0 );
}
gettimeofday( &now, NULL );
return( 1000 * ( now.tv_sec - ref.tv_sec )
+ ( now.tv_usec - ref.tv_usec ) / 1000 );
#endif
}
2014-09-06 12:36:46 +00:00
typedef struct
{
2015-06-30 13:40:39 +00:00
mbedtls_net_context *dst;
2014-09-06 12:36:46 +00:00
const char *way;
2014-09-06 06:14:47 +00:00
const char *type;
2014-09-06 12:36:46 +00:00
unsigned len;
unsigned char buf[MAX_MSG_SIZE];
} packet;
/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */
void print_packet( const packet *p, const char *why )
{
if( why == NULL )
mbedtls_printf( " %05lu %s %s (%u bytes)\n",
ellapsed_time(), p->way, p->type, p->len );
2014-09-06 12:36:46 +00:00
else
mbedtls_printf( " %s %s (%u bytes): %s\n",
p->way, p->type, p->len, why );
2014-09-06 12:36:46 +00:00
fflush( stdout );
}
int send_packet( const packet *p, const char *why )
{
int ret;
2015-06-30 13:40:39 +00:00
mbedtls_net_context *dst = p->dst;
2014-09-08 09:24:58 +00:00
/* insert corrupted ApplicationData record? */
if( opt.bad_ad &&
strcmp( p->type, "ApplicationData" ) == 0 )
{
unsigned char buf[MAX_MSG_SIZE];
memcpy( buf, p->buf, p->len );
++buf[p->len - 1];
print_packet( p, "corrupted" );
2015-06-30 13:40:39 +00:00
if( ( ret = mbedtls_net_send( dst, buf, p->len ) ) <= 0 )
2014-09-08 09:24:58 +00:00
{
mbedtls_printf( " ! mbedtls_net_send returned %d\n", ret );
2014-09-08 09:24:58 +00:00
return( ret );
}
}
2014-09-06 12:36:46 +00:00
print_packet( p, why );
2015-06-30 13:40:39 +00:00
if( ( ret = mbedtls_net_send( dst, p->buf, p->len ) ) <= 0 )
{
mbedtls_printf( " ! mbedtls_net_send returned %d\n", ret );
return( ret );
}
2014-09-06 06:14:47 +00:00
/* Don't duplicate Application Data, only handshake covered */
if( opt.duplicate != 0 &&
2014-09-06 12:36:46 +00:00
strcmp( p->type, "ApplicationData" ) != 0 &&
rand() % opt.duplicate == 0 )
2014-09-06 06:14:47 +00:00
{
2014-09-06 12:36:46 +00:00
print_packet( p, "duplicated" );
2014-09-06 06:14:47 +00:00
2015-06-30 13:40:39 +00:00
if( ( ret = mbedtls_net_send( dst, p->buf, p->len ) ) <= 0 )
2014-09-06 06:14:47 +00:00
{
mbedtls_printf( " ! mbedtls_net_send returned %d\n", ret );
2014-09-06 06:14:47 +00:00
return( ret );
}
}
2014-09-06 12:36:46 +00:00
return( 0 );
}
2014-09-23 10:46:33 +00:00
static packet prev;
void clear_pending( void )
{
memset( &prev, 0, sizeof( packet ) );
}
/*
* Avoid dropping or delaying a packet that was already dropped twice: this
* only results in uninteresting timeouts. We can't rely on type to identify
* packets, since during renegotiation they're all encrypted. So, rely on
* size mod 2048 (which is usually just size).
*/
static unsigned char dropped[2048] = { 0 };
#define DROP_MAX 2
/*
* OpenSSL groups packets in a datagram the first time it sends them, but not
* when it resends them. Count every record as seen the first time.
*/
void update_dropped( const packet *p )
{
size_t id = p->len % sizeof( dropped );
const unsigned char *end = p->buf + p->len;
const unsigned char *cur = p->buf;
size_t len = ( ( cur[11] << 8 ) | cur[12] ) + 13;
++dropped[id];
/* Avoid counting single record twice */
if( len == p->len )
return;
while( cur < end )
{
size_t len = ( ( cur[11] << 8 ) | cur[12] ) + 13;
id = len % sizeof( dropped );
++dropped[id];
cur += len;
}
}
2015-06-30 13:40:39 +00:00
int handle_message( const char *way,
mbedtls_net_context *dst,
mbedtls_net_context *src )
2014-09-06 12:36:46 +00:00
{
int ret;
packet cur;
size_t id;
2014-09-06 12:36:46 +00:00
2014-09-08 14:39:08 +00:00
/* receive packet */
2015-06-30 13:40:39 +00:00
if( ( ret = mbedtls_net_recv( src, cur.buf, sizeof( cur.buf ) ) ) <= 0 )
{
mbedtls_printf( " ! mbedtls_net_recv returned %d\n", ret );
return( ret );
}
2014-09-06 12:36:46 +00:00
cur.len = ret;
cur.type = msg_type( cur.buf, cur.len );
cur.way = way;
cur.dst = dst;
2014-09-06 12:36:46 +00:00
print_packet( &cur, NULL );
id = cur.len % sizeof( dropped );
2014-09-06 12:49:52 +00:00
/* do we want to drop, delay, or forward it? */
2014-09-08 09:11:42 +00:00
if( ( opt.mtu != 0 &&
cur.len > (unsigned) opt.mtu ) ||
( opt.drop != 0 &&
strcmp( cur.type, "ApplicationData" ) != 0 &&
2014-09-25 15:00:27 +00:00
! ( opt.protect_hvr &&
strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
cur.len != (size_t) opt.protect_len &&
dropped[id] < DROP_MAX &&
rand() % opt.drop == 0 ) )
2014-09-06 12:49:52 +00:00
{
update_dropped( &cur );
2014-09-06 12:49:52 +00:00
}
2014-09-08 08:44:57 +00:00
else if( ( opt.delay_ccs == 1 &&
strcmp( cur.type, "ChangeCipherSpec" ) == 0 ) ||
( opt.delay != 0 &&
strcmp( cur.type, "ApplicationData" ) != 0 &&
2014-09-25 15:00:27 +00:00
! ( opt.protect_hvr &&
strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
2015-06-30 13:40:39 +00:00
prev.dst == NULL &&
cur.len != (size_t) opt.protect_len &&
dropped[id] < DROP_MAX &&
rand() % opt.delay == 0 ) )
2014-09-06 12:36:46 +00:00
{
memcpy( &prev, &cur, sizeof( packet ) );
}
else
{
2014-09-06 12:49:52 +00:00
/* forward and possibly duplicate */
2014-09-06 12:36:46 +00:00
if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 )
return( ret );
/* send previously delayed message if any */
2015-06-30 13:40:39 +00:00
if( prev.dst != NULL )
2014-09-06 12:36:46 +00:00
{
ret = send_packet( &prev, "delayed" );
memset( &prev, 0, sizeof( packet ) );
if( ret != 0 )
return( ret );
}
}
return( 0 );
}
int main( int argc, char *argv[] )
{
int ret;
2015-06-30 13:40:39 +00:00
mbedtls_net_context listen_fd, client_fd, server_fd;
int nb_fds;
fd_set read_fds;
2015-06-30 13:40:39 +00:00
mbedtls_net_init( &listen_fd );
mbedtls_net_init( &client_fd );
mbedtls_net_init( &server_fd );
get_options( argc, argv );
/*
* Decisions to drop/delay/duplicate packets are pseudo-random: dropping
* exactly 1 in N packets would lead to problems when a flight has exactly
* N packets: the same packet would be dropped on every resend.
*
* In order to be able to reproduce problems reliably, the seed may be
* specified explicitly.
*/
if( opt.seed == 0 )
{
opt.seed = time( NULL );
mbedtls_printf( " . Pseudo-random seed: %u\n", opt.seed );
}
srand( opt.seed );
/*
2014-09-06 06:07:45 +00:00
* 0. "Connect" to the server
*/
mbedtls_printf( " . Connect to server on UDP/%s/%s ...",
opt.server_addr, opt.server_port );
fflush( stdout );
if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
MBEDTLS_NET_PROTO_UDP ) ) != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
goto exit;
}
mbedtls_printf( " ok\n" );
/*
* 1. Setup the "listening" UDP socket
*/
mbedtls_printf( " . Bind on UDP/%s/%s ...",
opt.listen_addr, opt.listen_port );
fflush( stdout );
if( ( ret = mbedtls_net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
MBEDTLS_NET_PROTO_UDP ) ) != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
goto exit;
}
mbedtls_printf( " ok\n" );
/*
* 2. Wait until a client connects
*/
2014-09-23 10:46:33 +00:00
accept:
mbedtls_printf( " . Waiting for a remote connection ..." );
fflush( stdout );
2015-06-30 13:40:39 +00:00
if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
NULL, 0, NULL ) ) != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret );
goto exit;
}
mbedtls_printf( " ok\n" );
fflush( stdout );
mbedtls_printf( " . Re-bind on UDP/%s/%s ...",
2014-09-23 10:46:33 +00:00
opt.listen_addr, opt.listen_port );
fflush( stdout );
if( ( ret = mbedtls_net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
MBEDTLS_NET_PROTO_UDP ) ) != 0 )
2014-09-23 10:46:33 +00:00
{
mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
2014-09-23 10:46:33 +00:00
goto exit;
}
mbedtls_printf( " ok\n" );
2014-09-23 10:46:33 +00:00
/*
* 3. Forward packets forever (kill the process to terminate it)
*/
clear_pending();
memset( dropped, 0, sizeof( dropped ) );
2015-06-30 13:40:39 +00:00
nb_fds = client_fd.fd;
if( nb_fds < server_fd.fd )
nb_fds = server_fd.fd;
if( nb_fds < listen_fd.fd )
nb_fds = listen_fd.fd;
2014-09-23 10:46:33 +00:00
++nb_fds;
while( 1 )
{
FD_ZERO( &read_fds );
2015-06-30 13:40:39 +00:00
FD_SET( server_fd.fd, &read_fds );
FD_SET( client_fd.fd, &read_fds );
FD_SET( listen_fd.fd, &read_fds );
if( ( ret = select( nb_fds, &read_fds, NULL, NULL, NULL ) ) <= 0 )
{
perror( "select" );
goto exit;
}
2015-06-30 13:40:39 +00:00
if( FD_ISSET( listen_fd.fd, &read_fds ) )
2014-09-23 10:46:33 +00:00
goto accept;
2015-06-30 13:40:39 +00:00
if( FD_ISSET( client_fd.fd, &read_fds ) )
{
if( ( ret = handle_message( "S <- C",
2015-06-30 13:40:39 +00:00
&server_fd, &client_fd ) ) != 0 )
goto accept;
}
2015-06-30 13:40:39 +00:00
if( FD_ISSET( server_fd.fd, &read_fds ) )
{
if( ( ret = handle_message( "S -> C",
2015-06-30 13:40:39 +00:00
&client_fd, &server_fd ) ) != 0 )
goto accept;
}
}
exit:
#ifdef MBEDTLS_ERROR_C
if( ret != 0 )
{
char error_buf[100];
mbedtls_strerror( ret, error_buf, 100 );
mbedtls_printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf );
fflush( stdout );
}
#endif
mbedtls_net_free( &client_fd );
mbedtls_net_free( &server_fd );
mbedtls_net_free( &listen_fd );
#if defined(_WIN32)
mbedtls_printf( " Press Enter to exit this program.\n" );
fflush( stdout ); getchar();
#endif
return( ret != 0 );
}
#endif /* MBEDTLS_NET_C */