blob: 7f58e3dd577cf27f572202ef49252cc3ab4c6697 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSL/TLS stress testing program
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
32#define polarssl_printf printf
33#define polarssl_fprintf fprintf
34#define polarssl_malloc malloc
35#define polarssl_free free
36#endif
37
Paul Bakker5121ce52009-01-03 21:22:43 +000038#include <string.h>
39#include <stdlib.h>
40#include <stdio.h>
41
Paul Bakker40e46942009-01-03 21:51:57 +000042#include "polarssl/net.h"
43#include "polarssl/ssl.h"
Paul Bakker508ad5a2011-12-04 17:09:26 +000044#include "polarssl/entropy.h"
45#include "polarssl/ctr_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000046#include "polarssl/certs.h"
Paul Bakker44618dd2013-07-04 10:34:10 +020047#if defined(POLARSSL_TIMING_C)
48#include "polarssl/timing.h"
49#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000050
Paul Bakker9a97c5d2013-09-15 17:07:33 +020051#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
52 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \
53 !defined(POLARSSL_SSL_CLI_C) || !defined(POLARSSL_NET_C) || \
54 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020055 !defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker9a97c5d2013-09-15 17:07:33 +020056int main( int argc, char *argv[] )
57{
58 ((void) argc);
59 ((void) argv);
60
Rich Evansf90016a2015-01-19 14:26:37 +000061 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
Paul Bakker9a97c5d2013-09-15 17:07:33 +020062 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
63 "POLARSSL_SSL_CLI_C and/or POLARSSL_NET_C and/or "
64 "POLARSSL_RSA_C and/or POLARSSL_CTR_DRBG_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020065 "POLARSSL_X509_CRT_PARSE_C not defined.\n");
Paul Bakker9a97c5d2013-09-15 17:07:33 +020066 return( 0 );
67}
68#else
69
Paul Bakker5121ce52009-01-03 21:22:43 +000070#define OPMODE_NONE 0
71#define OPMODE_CLIENT 1
72#define OPMODE_SERVER 2
73
74#define IOMODE_BLOCK 0
75#define IOMODE_NONBLOCK 1
76
77#define COMMAND_READ 1
78#define COMMAND_WRITE 2
79#define COMMAND_BOTH 3
80
81#define DFL_OPMODE OPMODE_NONE
82#define DFL_IOMODE IOMODE_BLOCK
83#define DFL_SERVER_NAME "localhost"
84#define DFL_SERVER_PORT 4433
85#define DFL_COMMAND COMMAND_READ
86#define DFL_BUFFER_SIZE 1024
87#define DFL_MAX_BYTES 0
88#define DFL_DEBUG_LEVEL 0
89#define DFL_CONN_TIMEOUT 0
90#define DFL_MAX_CONNECTIONS 0
91#define DFL_SESSION_REUSE 1
92#define DFL_SESSION_LIFETIME 86400
93#define DFL_FORCE_CIPHER 0
94
Paul Bakker5121ce52009-01-03 21:22:43 +000095int server_fd = -1;
96
97/*
98 * global options
99 */
100struct options
101{
102 int opmode; /* operation mode (client or server) */
103 int iomode; /* I/O mode (blocking or non-blocking) */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200104 const char *server_name; /* hostname of the server (client only) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 int server_port; /* port on which the ssl service runs */
106 int command; /* what to do: read or write operation */
107 int buffer_size; /* size of the send/receive buffer */
108 int max_bytes; /* max. # of bytes before a reconnect */
109 int debug_level; /* level of debugging */
Paul Bakker44618dd2013-07-04 10:34:10 +0200110#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 int conn_timeout; /* max. delay before a reconnect */
Paul Bakker44618dd2013-07-04 10:34:10 +0200112#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 int max_connections; /* max. number of reconnections */
114 int session_reuse; /* flag to reuse the keying material */
115 int session_lifetime; /* if reached, session data is expired */
Paul Bakkere3166ce2011-01-27 17:40:50 +0000116 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
Paul Bakker5121ce52009-01-03 21:22:43 +0000117};
118
119/*
120 * Although this PRNG has good statistical properties (eg. passes
121 * DIEHARD), it is not cryptographically secure.
122 */
Paul Bakker3c5ef712013-06-25 16:37:45 +0200123static unsigned long int lcppm5( unsigned long int *state )
Paul Bakker5121ce52009-01-03 21:22:43 +0000124{
125 unsigned long int u, v;
126
127 u = v = state[4] ^ 1;
128 state[u & 3] ^= u;
129 u ^= (v << 12) ^ (v >> 12);
130 u ^= v * state[0]; v >>= 8;
131 u ^= v * state[1]; v >>= 8;
132 u ^= v * state[2]; v >>= 8;
133 u ^= v * state[3];
134 u &= 0xFFFFFFFF;
135 state[4] = u;
136
137 return( u );
138}
139
Paul Bakker3c5ef712013-06-25 16:37:45 +0200140static void my_debug( void *ctx, int level, const char *str )
Paul Bakker5121ce52009-01-03 21:22:43 +0000141{
142 if( level < ((struct options *) ctx)->debug_level )
Rich Evansf90016a2015-01-19 14:26:37 +0000143 polarssl_fprintf( stderr, "%s", str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000144}
145
146/*
147 * perform a single SSL connection
148 */
149static int ssl_test( struct options *opt )
150{
Alfred Klomp5b78f212014-07-14 22:10:14 +0200151 int ret = 1, i;
Manuel Pégourié-Gonnard68821da2013-09-16 12:34:33 +0200152 int client_fd = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 int bytes_to_read;
154 int bytes_to_write;
Paul Bakker026c03b2009-03-28 17:53:03 +0000155 int offset_to_read = 0;
156 int offset_to_write = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000157
158 long int nb_read;
159 long int nb_written;
160
161 unsigned long read_state[5];
162 unsigned long write_state[5];
163
Paul Bakker026c03b2009-03-28 17:53:03 +0000164 unsigned char *read_buf = NULL;
165 unsigned char *write_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000166
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200167 const char *pers = "ssl_test";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000168
Paul Bakker44618dd2013-07-04 10:34:10 +0200169#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000170 struct hr_time t;
Paul Bakker44618dd2013-07-04 10:34:10 +0200171#endif
Paul Bakker508ad5a2011-12-04 17:09:26 +0000172 entropy_context entropy;
173 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +0000174 ssl_context ssl;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200175 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200176 pk_context pkey;
Paul Bakker5121ce52009-01-03 21:22:43 +0000177
Paul Bakker0c226102014-04-17 16:02:36 +0200178 memset( &ssl, 0, sizeof(ssl_context) );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000179 entropy_init( &entropy );
Paul Bakker0c226102014-04-17 16:02:36 +0200180 x509_crt_init( &srvcert );
181 pk_init( &pkey );
182
Paul Bakker508ad5a2011-12-04 17:09:26 +0000183 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200184 (const unsigned char *) pers,
185 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000186 {
Rich Evansf90016a2015-01-19 14:26:37 +0000187 polarssl_printf( " ! ctr_drbg_init returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000188 goto exit;
189 }
190
Paul Bakker44618dd2013-07-04 10:34:10 +0200191#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000192 get_timer( &t, 1 );
Paul Bakker44618dd2013-07-04 10:34:10 +0200193#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000194
195 memset( read_state, 0, sizeof( read_state ) );
196 memset( write_state, 0, sizeof( write_state ) );
197
Paul Bakker5121ce52009-01-03 21:22:43 +0000198
199 if( opt->opmode == OPMODE_CLIENT )
200 {
201 if( ( ret = net_connect( &client_fd, opt->server_name,
202 opt->server_port ) ) != 0 )
203 {
Rich Evansf90016a2015-01-19 14:26:37 +0000204 polarssl_printf( " ! net_connect returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 return( ret );
206 }
207
208 if( ( ret = ssl_init( &ssl ) ) != 0 )
209 {
Rich Evansf90016a2015-01-19 14:26:37 +0000210 polarssl_printf( " ! ssl_init returned %d\n\n", ret );
Paul Bakker0c226102014-04-17 16:02:36 +0200211 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +0000212 }
213
214 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
215 }
216
217 if( opt->opmode == OPMODE_SERVER )
218 {
Paul Bakker5690efc2011-05-26 13:16:06 +0000219#if !defined(POLARSSL_CERTS_C)
Rich Evansf90016a2015-01-19 14:26:37 +0000220 polarssl_printf("POLARSSL_CERTS_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +0000221 goto exit;
222#else
Paul Bakkerddf26b42013-09-18 13:46:23 +0200223 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_srv_crt,
224 strlen( test_srv_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 if( ret != 0 )
226 {
Rich Evansf90016a2015-01-19 14:26:37 +0000227 polarssl_printf( " ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 goto exit;
229 }
230
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200231 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_ca_list,
232 strlen( test_ca_list ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000233 if( ret != 0 )
234 {
Rich Evansf90016a2015-01-19 14:26:37 +0000235 polarssl_printf( " ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000236 goto exit;
237 }
238
Paul Bakker1a7550a2013-09-15 13:01:22 +0200239 ret = pk_parse_key( &pkey, (const unsigned char *) test_srv_key,
240 strlen( test_srv_key ), NULL, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 if( ret != 0 )
242 {
Rich Evansf90016a2015-01-19 14:26:37 +0000243 polarssl_printf( " ! pk_parse_key returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000244 goto exit;
245 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000246#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000247
248 if( server_fd < 0 )
249 {
250 if( ( ret = net_bind( &server_fd, NULL,
251 opt->server_port ) ) != 0 )
252 {
Rich Evansf90016a2015-01-19 14:26:37 +0000253 polarssl_printf( " ! net_bind returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 return( ret );
255 }
256 }
257
258 if( ( ret = net_accept( server_fd, &client_fd, NULL ) ) != 0 )
259 {
Rich Evansf90016a2015-01-19 14:26:37 +0000260 polarssl_printf( " ! net_accept returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000261 return( ret );
262 }
263
264 if( ( ret = ssl_init( &ssl ) ) != 0 )
265 {
Rich Evansf90016a2015-01-19 14:26:37 +0000266 polarssl_printf( " ! ssl_init returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 return( ret );
268 }
269
270 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000271 ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200272 if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 )
273 {
Rich Evansf90016a2015-01-19 14:26:37 +0000274 polarssl_printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200275 goto exit;
276 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000277 }
278
279 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
280
Paul Bakker508ad5a2011-12-04 17:09:26 +0000281 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker5121ce52009-01-03 21:22:43 +0000282 ssl_set_dbg( &ssl, my_debug, opt );
283 ssl_set_bio( &ssl, net_recv, &client_fd,
284 net_send, &client_fd );
285
Paul Bakker68884e32013-01-07 18:20:04 +0100286 if( opt->force_ciphersuite[0] != DFL_FORCE_CIPHER )
287 ssl_set_ciphersuites( &ssl, opt->force_ciphersuite );
Paul Bakker5121ce52009-01-03 21:22:43 +0000288
289 if( opt->iomode == IOMODE_NONBLOCK )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200290 {
291 if( ( ret = net_set_nonblock( client_fd ) ) != 0 )
292 {
Rich Evansf90016a2015-01-19 14:26:37 +0000293 polarssl_printf( " ! net_set_nonblock returned %d\n\n", ret );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200294 return( ret );
295 }
296 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000297
Rich Evansf90016a2015-01-19 14:26:37 +0000298 read_buf = (unsigned char *) polarssl_malloc( opt->buffer_size );
299 write_buf = (unsigned char *) polarssl_malloc( opt->buffer_size );
Paul Bakker5121ce52009-01-03 21:22:43 +0000300
301 if( read_buf == NULL || write_buf == NULL )
302 {
Rich Evansf90016a2015-01-19 14:26:37 +0000303 polarssl_printf( " ! polarssl_malloc(%d bytes) failed\n\n", opt->buffer_size );
Paul Bakker5121ce52009-01-03 21:22:43 +0000304 goto exit;
305 }
306
307 nb_read = bytes_to_read = 0;
308 nb_written = bytes_to_write = 0;
309
310 while( 1 )
311 {
312 if( opt->command & COMMAND_WRITE )
313 {
314 if( bytes_to_write == 0 )
315 {
316 while( bytes_to_write == 0 )
317 bytes_to_write = rand() % opt->buffer_size;
318
319 for( i = 0; i < bytes_to_write; i++ )
320 write_buf[i] = (unsigned char) lcppm5( write_state );
321
322 offset_to_write = 0;
323 }
324
325 ret = ssl_write( &ssl, write_buf + offset_to_write,
326 bytes_to_write );
327
328 if( ret >= 0 )
329 {
330 nb_written += ret;
331 bytes_to_write -= ret;
332 offset_to_write += ret;
333 }
334
Paul Bakker40e46942009-01-03 21:51:57 +0000335 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY ||
336 ret == POLARSSL_ERR_NET_CONN_RESET )
Paul Bakker5121ce52009-01-03 21:22:43 +0000337 {
338 ret = 0;
339 goto exit;
340 }
341
Paul Bakker831a7552011-05-18 13:32:51 +0000342 if( ret < 0 && ret != POLARSSL_ERR_NET_WANT_READ &&
343 ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000344 {
Rich Evansf90016a2015-01-19 14:26:37 +0000345 polarssl_printf( " ! ssl_write returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000346 break;
347 }
348 }
349
350 if( opt->command & COMMAND_READ )
351 {
Paul Bakker396333e2013-09-26 13:32:19 +0200352 while( bytes_to_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000353 {
354 bytes_to_read = rand() % opt->buffer_size;
355 offset_to_read = 0;
356 }
357
358 ret = ssl_read( &ssl, read_buf + offset_to_read,
359 bytes_to_read );
360
Paul Bakker396333e2013-09-26 13:32:19 +0200361 if( ret > 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 {
363 for( i = 0; i < ret; i++ )
364 {
365 if( read_buf[offset_to_read + i] !=
366 (unsigned char) lcppm5( read_state ) )
367 {
368 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000369 polarssl_printf( " ! plaintext mismatch\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000370 goto exit;
371 }
372 }
373
374 nb_read += ret;
375 bytes_to_read -= ret;
376 offset_to_read += ret;
377 }
378
Paul Bakker396333e2013-09-26 13:32:19 +0200379 if( ret == 0 ||
380 ret == POLARSSL_ERR_SSL_CONN_EOF ||
381 ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY ||
Paul Bakker40e46942009-01-03 21:51:57 +0000382 ret == POLARSSL_ERR_NET_CONN_RESET )
Paul Bakker5121ce52009-01-03 21:22:43 +0000383 {
384 ret = 0;
385 goto exit;
386 }
387
Paul Bakker831a7552011-05-18 13:32:51 +0000388 if( ret < 0 && ret != POLARSSL_ERR_NET_WANT_READ &&
389 ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000390 {
Rich Evansf90016a2015-01-19 14:26:37 +0000391 polarssl_printf( " ! ssl_read returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000392 break;
393 }
394 }
395
396 ret = 0;
397
398 if( opt->max_bytes != 0 &&
399 ( opt->max_bytes <= nb_read ||
400 opt->max_bytes <= nb_written ) )
401 break;
402
Paul Bakker44618dd2013-07-04 10:34:10 +0200403#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000404 if( opt->conn_timeout != 0 &&
405 opt->conn_timeout <= (int) get_timer( &t, 0 ) )
406 break;
Paul Bakker44618dd2013-07-04 10:34:10 +0200407#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 }
409
410exit:
411
412 fflush( stdout );
413
414 if( read_buf != NULL )
415 free( read_buf );
416
417 if( write_buf != NULL )
418 free( write_buf );
419
420 ssl_close_notify( &ssl );
Paul Bakker36713e82013-09-17 13:25:29 +0200421 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200422 pk_free( &pkey );
Paul Bakker5121ce52009-01-03 21:22:43 +0000423 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +0200424 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200425 entropy_free( &entropy );
Paul Bakker0c226102014-04-17 16:02:36 +0200426
427 if( client_fd != -1 )
428 net_close( client_fd );
Paul Bakker5121ce52009-01-03 21:22:43 +0000429
430 return( ret );
431}
432
Paul Bakker44618dd2013-07-04 10:34:10 +0200433#if defined(POLARSSL_TIMING_C)
434#define USAGE_TIMING \
435 " conn_timeout=%%d (ms) default: 0 (no timeout)\n"
436#else
437#define USAGE_TIMING ""
438#endif
439
Paul Bakker5121ce52009-01-03 21:22:43 +0000440#define USAGE \
441 "\n usage: ssl_test opmode=<> command=<>...\n" \
442 "\n acceptable parameters:\n" \
443 " opmode=client/server default: <none>\n" \
444 " iomode=block/nonblock default: block\n" \
445 " server_name=%%s default: localhost\n" \
446 " server_port=%%d default: 4433\n" \
447 " command=read/write/both default: read\n" \
448 " buffer_size=%%d (bytes) default: 1024\n" \
449 " max_bytes=%%d (bytes) default: 0 (no limit)\n" \
450 " debug_level=%%d default: 0 (disabled)\n" \
Paul Bakker44618dd2013-07-04 10:34:10 +0200451 USAGE_TIMING \
Paul Bakker5121ce52009-01-03 21:22:43 +0000452 " max_connections=%%d default: 0 (no limit)\n" \
453 " session_reuse=on/off default: on (enabled)\n" \
454 " session_lifetime=%%d (s) default: 86400\n" \
Paul Bakkere3166ce2011-01-27 17:40:50 +0000455 " force_ciphersuite=<name> default: all enabled\n" \
456 " acceptable ciphersuite names:\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000457
458int main( int argc, char *argv[] )
459{
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100460 int i;
Paul Bakkere3166ce2011-01-27 17:40:50 +0000461 const int *list;
Paul Bakker5121ce52009-01-03 21:22:43 +0000462 int ret = 1;
463 int nb_conn;
464 char *p, *q;
465 struct options opt;
466
467 if( argc == 1 )
468 {
469 usage:
Rich Evansf90016a2015-01-19 14:26:37 +0000470 polarssl_printf( USAGE );
Paul Bakker44618dd2013-07-04 10:34:10 +0200471
Paul Bakkere3166ce2011-01-27 17:40:50 +0000472 list = ssl_list_ciphersuites();
473 while( *list )
474 {
Rich Evansf90016a2015-01-19 14:26:37 +0000475 polarssl_printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakkere3166ce2011-01-27 17:40:50 +0000476 list++;
477 }
Rich Evansf90016a2015-01-19 14:26:37 +0000478 polarssl_printf("\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000479 goto exit;
480 }
481
482 opt.opmode = DFL_OPMODE;
483 opt.iomode = DFL_IOMODE;
484 opt.server_name = DFL_SERVER_NAME;
485 opt.server_port = DFL_SERVER_PORT;
486 opt.command = DFL_COMMAND;
487 opt.buffer_size = DFL_BUFFER_SIZE;
488 opt.max_bytes = DFL_MAX_BYTES;
489 opt.debug_level = DFL_DEBUG_LEVEL;
Paul Bakker44618dd2013-07-04 10:34:10 +0200490#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000491 opt.conn_timeout = DFL_CONN_TIMEOUT;
Paul Bakker44618dd2013-07-04 10:34:10 +0200492#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000493 opt.max_connections = DFL_MAX_CONNECTIONS;
494 opt.session_reuse = DFL_SESSION_REUSE;
495 opt.session_lifetime = DFL_SESSION_LIFETIME;
Paul Bakkere3166ce2011-01-27 17:40:50 +0000496 opt.force_ciphersuite[0] = DFL_FORCE_CIPHER;
Paul Bakker5121ce52009-01-03 21:22:43 +0000497
498 for( i = 1; i < argc; i++ )
499 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000500 p = argv[i];
501 if( ( q = strchr( p, '=' ) ) == NULL )
502 continue;
503 *q++ = '\0';
504
505 if( strcmp( p, "opmode" ) == 0 )
506 {
507 if( strcmp( q, "client" ) == 0 )
508 opt.opmode = OPMODE_CLIENT;
509 else
510 if( strcmp( q, "server" ) == 0 )
511 opt.opmode = OPMODE_SERVER;
512 else goto usage;
513 }
514
515 if( strcmp( p, "iomode" ) == 0 )
516 {
517 if( strcmp( q, "block" ) == 0 )
518 opt.iomode = IOMODE_BLOCK;
519 else
520 if( strcmp( q, "nonblock" ) == 0 )
521 opt.iomode = IOMODE_NONBLOCK;
522 else goto usage;
523 }
524
525 if( strcmp( p, "server_name" ) == 0 )
526 opt.server_name = q;
527
528 if( strcmp( p, "server_port" ) == 0 )
529 {
530 opt.server_port = atoi( q );
531 if( opt.server_port < 1 || opt.server_port > 65535 )
532 goto usage;
533 }
534
535 if( strcmp( p, "command" ) == 0 )
536 {
537 if( strcmp( q, "read" ) == 0 )
538 opt.command = COMMAND_READ;
539 else
540 if( strcmp( q, "write" ) == 0 )
541 opt.command = COMMAND_WRITE;
542 else
543 if( strcmp( q, "both" ) == 0 )
544 {
545 opt.iomode = IOMODE_NONBLOCK;
546 opt.command = COMMAND_BOTH;
547 }
548 else goto usage;
549 }
550
551 if( strcmp( p, "buffer_size" ) == 0 )
552 {
553 opt.buffer_size = atoi( q );
554 if( opt.buffer_size < 1 || opt.buffer_size > 1048576 )
555 goto usage;
556 }
557
558 if( strcmp( p, "max_bytes" ) == 0 )
559 opt.max_bytes = atoi( q );
560
561 if( strcmp( p, "debug_level" ) == 0 )
562 opt.debug_level = atoi( q );
Paul Bakker44618dd2013-07-04 10:34:10 +0200563#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 if( strcmp( p, "conn_timeout" ) == 0 )
565 opt.conn_timeout = atoi( q );
Paul Bakker44618dd2013-07-04 10:34:10 +0200566#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 if( strcmp( p, "max_connections" ) == 0 )
568 opt.max_connections = atoi( q );
569
570 if( strcmp( p, "session_reuse" ) == 0 )
571 {
572 if( strcmp( q, "on" ) == 0 )
573 opt.session_reuse = 1;
574 else
575 if( strcmp( q, "off" ) == 0 )
576 opt.session_reuse = 0;
577 else
578 goto usage;
579 }
580
581 if( strcmp( p, "session_lifetime" ) == 0 )
582 opt.session_lifetime = atoi( q );
583
Paul Bakkere3166ce2011-01-27 17:40:50 +0000584 if( strcmp( p, "force_ciphersuite" ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000585 {
Paul Bakkere3166ce2011-01-27 17:40:50 +0000586 opt.force_ciphersuite[0] = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000587
Paul Bakkere3166ce2011-01-27 17:40:50 +0000588 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
Paul Bakker5121ce52009-01-03 21:22:43 +0000589
Paul Bakkere3166ce2011-01-27 17:40:50 +0000590 if( opt.force_ciphersuite[0] <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000591 goto usage;
592
Paul Bakkere3166ce2011-01-27 17:40:50 +0000593 opt.force_ciphersuite[1] = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000594 }
595 }
596
597 switch( opt.opmode )
598 {
599 case OPMODE_CLIENT:
600 break;
601
602 case OPMODE_SERVER:
603 break;
604
605 default:
606 goto usage;
607 }
608
609 nb_conn = 0;
610
611 do {
612 nb_conn++;
613 ret = ssl_test( &opt );
614 if( opt.max_connections != 0 &&
615 opt.max_connections <= nb_conn )
616 break;
617 }
618 while( ret == 0 );
619
620exit:
621
Paul Bakkercce9d772011-11-18 14:26:47 +0000622#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000623 polarssl_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000624 fflush( stdout ); getchar();
625#endif
626
627 return( ret );
628}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000629#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000630 POLARSSL_SSL_SRV_C && POLARSSL_SSL_CLI_C && POLARSSL_NET_C &&
Paul Bakker508ad5a2011-12-04 17:09:26 +0000631 POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */