blob: ab7b8124cdf0fdd0e4f0869acf0f1d5c358a2225 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSL/TLS stress testing program
3 *
Paul Bakker68884e32013-01-07 18:20:04 +01004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000031
32#include <string.h>
33#include <stdlib.h>
34#include <stdio.h>
35
Paul Bakker40e46942009-01-03 21:51:57 +000036#include "polarssl/net.h"
37#include "polarssl/ssl.h"
Paul Bakker508ad5a2011-12-04 17:09:26 +000038#include "polarssl/entropy.h"
39#include "polarssl/ctr_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000040#include "polarssl/certs.h"
Paul Bakker44618dd2013-07-04 10:34:10 +020041#if defined(POLARSSL_TIMING_C)
42#include "polarssl/timing.h"
43#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000044
Paul Bakker9a97c5d2013-09-15 17:07:33 +020045#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
46 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \
47 !defined(POLARSSL_SSL_CLI_C) || !defined(POLARSSL_NET_C) || \
48 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020049 !defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker9a97c5d2013-09-15 17:07:33 +020050int main( int argc, char *argv[] )
51{
52 ((void) argc);
53 ((void) argv);
54
55 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
56 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
57 "POLARSSL_SSL_CLI_C and/or POLARSSL_NET_C and/or "
58 "POLARSSL_RSA_C and/or POLARSSL_CTR_DRBG_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020059 "POLARSSL_X509_CRT_PARSE_C not defined.\n");
Paul Bakker9a97c5d2013-09-15 17:07:33 +020060 return( 0 );
61}
62#else
63
Paul Bakker5121ce52009-01-03 21:22:43 +000064#define OPMODE_NONE 0
65#define OPMODE_CLIENT 1
66#define OPMODE_SERVER 2
67
68#define IOMODE_BLOCK 0
69#define IOMODE_NONBLOCK 1
70
71#define COMMAND_READ 1
72#define COMMAND_WRITE 2
73#define COMMAND_BOTH 3
74
75#define DFL_OPMODE OPMODE_NONE
76#define DFL_IOMODE IOMODE_BLOCK
77#define DFL_SERVER_NAME "localhost"
78#define DFL_SERVER_PORT 4433
79#define DFL_COMMAND COMMAND_READ
80#define DFL_BUFFER_SIZE 1024
81#define DFL_MAX_BYTES 0
82#define DFL_DEBUG_LEVEL 0
83#define DFL_CONN_TIMEOUT 0
84#define DFL_MAX_CONNECTIONS 0
85#define DFL_SESSION_REUSE 1
86#define DFL_SESSION_LIFETIME 86400
87#define DFL_FORCE_CIPHER 0
88
Paul Bakker5121ce52009-01-03 21:22:43 +000089int server_fd = -1;
90
91/*
92 * global options
93 */
94struct options
95{
96 int opmode; /* operation mode (client or server) */
97 int iomode; /* I/O mode (blocking or non-blocking) */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020098 const char *server_name; /* hostname of the server (client only) */
Paul Bakker5121ce52009-01-03 21:22:43 +000099 int server_port; /* port on which the ssl service runs */
100 int command; /* what to do: read or write operation */
101 int buffer_size; /* size of the send/receive buffer */
102 int max_bytes; /* max. # of bytes before a reconnect */
103 int debug_level; /* level of debugging */
Paul Bakker44618dd2013-07-04 10:34:10 +0200104#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 int conn_timeout; /* max. delay before a reconnect */
Paul Bakker44618dd2013-07-04 10:34:10 +0200106#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 int max_connections; /* max. number of reconnections */
108 int session_reuse; /* flag to reuse the keying material */
109 int session_lifetime; /* if reached, session data is expired */
Paul Bakkere3166ce2011-01-27 17:40:50 +0000110 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
Paul Bakker5121ce52009-01-03 21:22:43 +0000111};
112
113/*
114 * Although this PRNG has good statistical properties (eg. passes
115 * DIEHARD), it is not cryptographically secure.
116 */
Paul Bakker3c5ef712013-06-25 16:37:45 +0200117static unsigned long int lcppm5( unsigned long int *state )
Paul Bakker5121ce52009-01-03 21:22:43 +0000118{
119 unsigned long int u, v;
120
121 u = v = state[4] ^ 1;
122 state[u & 3] ^= u;
123 u ^= (v << 12) ^ (v >> 12);
124 u ^= v * state[0]; v >>= 8;
125 u ^= v * state[1]; v >>= 8;
126 u ^= v * state[2]; v >>= 8;
127 u ^= v * state[3];
128 u &= 0xFFFFFFFF;
129 state[4] = u;
130
131 return( u );
132}
133
Paul Bakker3c5ef712013-06-25 16:37:45 +0200134static void my_debug( void *ctx, int level, const char *str )
Paul Bakker5121ce52009-01-03 21:22:43 +0000135{
136 if( level < ((struct options *) ctx)->debug_level )
137 fprintf( stderr, "%s", str );
138}
139
140/*
141 * perform a single SSL connection
142 */
143static int ssl_test( struct options *opt )
144{
145 int ret, i;
Manuel Pégourié-Gonnard68821da2013-09-16 12:34:33 +0200146 int client_fd = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 int bytes_to_read;
148 int bytes_to_write;
Paul Bakker026c03b2009-03-28 17:53:03 +0000149 int offset_to_read = 0;
150 int offset_to_write = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152 long int nb_read;
153 long int nb_written;
154
155 unsigned long read_state[5];
156 unsigned long write_state[5];
157
Paul Bakker026c03b2009-03-28 17:53:03 +0000158 unsigned char *read_buf = NULL;
159 unsigned char *write_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000160
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200161 const char *pers = "ssl_test";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000162
Paul Bakker44618dd2013-07-04 10:34:10 +0200163#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 struct hr_time t;
Paul Bakker44618dd2013-07-04 10:34:10 +0200165#endif
Paul Bakker508ad5a2011-12-04 17:09:26 +0000166 entropy_context entropy;
167 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +0000168 ssl_context ssl;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200169 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200170 pk_context pkey;
Paul Bakker5121ce52009-01-03 21:22:43 +0000171
172 ret = 1;
173
Paul Bakker0c226102014-04-17 16:02:36 +0200174 memset( &ssl, 0, sizeof(ssl_context) );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000175 entropy_init( &entropy );
Paul Bakker0c226102014-04-17 16:02:36 +0200176 x509_crt_init( &srvcert );
177 pk_init( &pkey );
178
Paul Bakker508ad5a2011-12-04 17:09:26 +0000179 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200180 (const unsigned char *) pers,
181 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000182 {
183 printf( " ! ctr_drbg_init returned %d\n", ret );
184 goto exit;
185 }
186
Paul Bakker44618dd2013-07-04 10:34:10 +0200187#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 get_timer( &t, 1 );
Paul Bakker44618dd2013-07-04 10:34:10 +0200189#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000190
191 memset( read_state, 0, sizeof( read_state ) );
192 memset( write_state, 0, sizeof( write_state ) );
193
Paul Bakker5121ce52009-01-03 21:22:43 +0000194
195 if( opt->opmode == OPMODE_CLIENT )
196 {
197 if( ( ret = net_connect( &client_fd, opt->server_name,
198 opt->server_port ) ) != 0 )
199 {
200 printf( " ! net_connect returned %d\n\n", ret );
201 return( ret );
202 }
203
204 if( ( ret = ssl_init( &ssl ) ) != 0 )
205 {
206 printf( " ! ssl_init returned %d\n\n", ret );
Paul Bakker0c226102014-04-17 16:02:36 +0200207 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +0000208 }
209
210 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
211 }
212
213 if( opt->opmode == OPMODE_SERVER )
214 {
Paul Bakker5690efc2011-05-26 13:16:06 +0000215#if !defined(POLARSSL_CERTS_C)
216 printf("POLARSSL_CERTS_C not defined.\n");
217 goto exit;
218#else
Paul Bakkerddf26b42013-09-18 13:46:23 +0200219 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_srv_crt,
220 strlen( test_srv_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000221 if( ret != 0 )
222 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200223 printf( " ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 goto exit;
225 }
226
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200227 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_ca_list,
228 strlen( test_ca_list ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000229 if( ret != 0 )
230 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200231 printf( " ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 goto exit;
233 }
234
Paul Bakker1a7550a2013-09-15 13:01:22 +0200235 ret = pk_parse_key( &pkey, (const unsigned char *) test_srv_key,
236 strlen( test_srv_key ), NULL, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000237 if( ret != 0 )
238 {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200239 printf( " ! pk_parse_key returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000240 goto exit;
241 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000242#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000243
244 if( server_fd < 0 )
245 {
246 if( ( ret = net_bind( &server_fd, NULL,
247 opt->server_port ) ) != 0 )
248 {
249 printf( " ! net_bind returned %d\n\n", ret );
250 return( ret );
251 }
252 }
253
254 if( ( ret = net_accept( server_fd, &client_fd, NULL ) ) != 0 )
255 {
256 printf( " ! net_accept returned %d\n\n", ret );
257 return( ret );
258 }
259
260 if( ( ret = ssl_init( &ssl ) ) != 0 )
261 {
262 printf( " ! ssl_init returned %d\n\n", ret );
263 return( ret );
264 }
265
266 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000267 ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200268 if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 )
269 {
270 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
271 goto exit;
272 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000273 }
274
275 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
276
Paul Bakker508ad5a2011-12-04 17:09:26 +0000277 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker5121ce52009-01-03 21:22:43 +0000278 ssl_set_dbg( &ssl, my_debug, opt );
279 ssl_set_bio( &ssl, net_recv, &client_fd,
280 net_send, &client_fd );
281
Paul Bakker68884e32013-01-07 18:20:04 +0100282 if( opt->force_ciphersuite[0] != DFL_FORCE_CIPHER )
283 ssl_set_ciphersuites( &ssl, opt->force_ciphersuite );
Paul Bakker5121ce52009-01-03 21:22:43 +0000284
285 if( opt->iomode == IOMODE_NONBLOCK )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200286 {
287 if( ( ret = net_set_nonblock( client_fd ) ) != 0 )
288 {
289 printf( " ! net_set_nonblock returned %d\n\n", ret );
290 return( ret );
291 }
292 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000293
294 read_buf = (unsigned char *) malloc( opt->buffer_size );
295 write_buf = (unsigned char *) malloc( opt->buffer_size );
296
297 if( read_buf == NULL || write_buf == NULL )
298 {
299 printf( " ! malloc(%d bytes) failed\n\n", opt->buffer_size );
300 goto exit;
301 }
302
303 nb_read = bytes_to_read = 0;
304 nb_written = bytes_to_write = 0;
305
306 while( 1 )
307 {
308 if( opt->command & COMMAND_WRITE )
309 {
310 if( bytes_to_write == 0 )
311 {
312 while( bytes_to_write == 0 )
313 bytes_to_write = rand() % opt->buffer_size;
314
315 for( i = 0; i < bytes_to_write; i++ )
316 write_buf[i] = (unsigned char) lcppm5( write_state );
317
318 offset_to_write = 0;
319 }
320
321 ret = ssl_write( &ssl, write_buf + offset_to_write,
322 bytes_to_write );
323
324 if( ret >= 0 )
325 {
326 nb_written += ret;
327 bytes_to_write -= ret;
328 offset_to_write += ret;
329 }
330
Paul Bakker40e46942009-01-03 21:51:57 +0000331 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY ||
332 ret == POLARSSL_ERR_NET_CONN_RESET )
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 {
334 ret = 0;
335 goto exit;
336 }
337
Paul Bakker831a7552011-05-18 13:32:51 +0000338 if( ret < 0 && ret != POLARSSL_ERR_NET_WANT_READ &&
339 ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000340 {
341 printf( " ! ssl_write returned %d\n\n", ret );
342 break;
343 }
344 }
345
346 if( opt->command & COMMAND_READ )
347 {
Paul Bakker396333e2013-09-26 13:32:19 +0200348 while( bytes_to_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000349 {
350 bytes_to_read = rand() % opt->buffer_size;
351 offset_to_read = 0;
352 }
353
354 ret = ssl_read( &ssl, read_buf + offset_to_read,
355 bytes_to_read );
356
Paul Bakker396333e2013-09-26 13:32:19 +0200357 if( ret > 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000358 {
359 for( i = 0; i < ret; i++ )
360 {
361 if( read_buf[offset_to_read + i] !=
362 (unsigned char) lcppm5( read_state ) )
363 {
364 ret = 1;
365 printf( " ! plaintext mismatch\n\n" );
366 goto exit;
367 }
368 }
369
370 nb_read += ret;
371 bytes_to_read -= ret;
372 offset_to_read += ret;
373 }
374
Paul Bakker396333e2013-09-26 13:32:19 +0200375 if( ret == 0 ||
376 ret == POLARSSL_ERR_SSL_CONN_EOF ||
377 ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY ||
Paul Bakker40e46942009-01-03 21:51:57 +0000378 ret == POLARSSL_ERR_NET_CONN_RESET )
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 {
380 ret = 0;
381 goto exit;
382 }
383
Paul Bakker831a7552011-05-18 13:32:51 +0000384 if( ret < 0 && ret != POLARSSL_ERR_NET_WANT_READ &&
385 ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000386 {
387 printf( " ! ssl_read returned %d\n\n", ret );
388 break;
389 }
390 }
391
392 ret = 0;
393
394 if( opt->max_bytes != 0 &&
395 ( opt->max_bytes <= nb_read ||
396 opt->max_bytes <= nb_written ) )
397 break;
398
Paul Bakker44618dd2013-07-04 10:34:10 +0200399#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000400 if( opt->conn_timeout != 0 &&
401 opt->conn_timeout <= (int) get_timer( &t, 0 ) )
402 break;
Paul Bakker44618dd2013-07-04 10:34:10 +0200403#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000404 }
405
406exit:
407
408 fflush( stdout );
409
410 if( read_buf != NULL )
411 free( read_buf );
412
413 if( write_buf != NULL )
414 free( write_buf );
415
416 ssl_close_notify( &ssl );
Paul Bakker36713e82013-09-17 13:25:29 +0200417 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200418 pk_free( &pkey );
Paul Bakker5121ce52009-01-03 21:22:43 +0000419 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +0200420 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200421 entropy_free( &entropy );
Paul Bakker0c226102014-04-17 16:02:36 +0200422
423 if( client_fd != -1 )
424 net_close( client_fd );
Paul Bakker5121ce52009-01-03 21:22:43 +0000425
426 return( ret );
427}
428
Paul Bakker44618dd2013-07-04 10:34:10 +0200429#if defined(POLARSSL_TIMING_C)
430#define USAGE_TIMING \
431 " conn_timeout=%%d (ms) default: 0 (no timeout)\n"
432#else
433#define USAGE_TIMING ""
434#endif
435
Paul Bakker5121ce52009-01-03 21:22:43 +0000436#define USAGE \
437 "\n usage: ssl_test opmode=<> command=<>...\n" \
438 "\n acceptable parameters:\n" \
439 " opmode=client/server default: <none>\n" \
440 " iomode=block/nonblock default: block\n" \
441 " server_name=%%s default: localhost\n" \
442 " server_port=%%d default: 4433\n" \
443 " command=read/write/both default: read\n" \
444 " buffer_size=%%d (bytes) default: 1024\n" \
445 " max_bytes=%%d (bytes) default: 0 (no limit)\n" \
446 " debug_level=%%d default: 0 (disabled)\n" \
Paul Bakker44618dd2013-07-04 10:34:10 +0200447 USAGE_TIMING \
Paul Bakker5121ce52009-01-03 21:22:43 +0000448 " max_connections=%%d default: 0 (no limit)\n" \
449 " session_reuse=on/off default: on (enabled)\n" \
450 " session_lifetime=%%d (s) default: 86400\n" \
Paul Bakkere3166ce2011-01-27 17:40:50 +0000451 " force_ciphersuite=<name> default: all enabled\n" \
452 " acceptable ciphersuite names:\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000453
454int main( int argc, char *argv[] )
455{
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100456 int i;
Paul Bakkere3166ce2011-01-27 17:40:50 +0000457 const int *list;
Paul Bakker5121ce52009-01-03 21:22:43 +0000458 int ret = 1;
459 int nb_conn;
460 char *p, *q;
461 struct options opt;
462
463 if( argc == 1 )
464 {
465 usage:
466 printf( USAGE );
Paul Bakker44618dd2013-07-04 10:34:10 +0200467
Paul Bakkere3166ce2011-01-27 17:40:50 +0000468 list = ssl_list_ciphersuites();
469 while( *list )
470 {
471 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
472 list++;
473 }
474 printf("\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000475 goto exit;
476 }
477
478 opt.opmode = DFL_OPMODE;
479 opt.iomode = DFL_IOMODE;
480 opt.server_name = DFL_SERVER_NAME;
481 opt.server_port = DFL_SERVER_PORT;
482 opt.command = DFL_COMMAND;
483 opt.buffer_size = DFL_BUFFER_SIZE;
484 opt.max_bytes = DFL_MAX_BYTES;
485 opt.debug_level = DFL_DEBUG_LEVEL;
Paul Bakker44618dd2013-07-04 10:34:10 +0200486#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000487 opt.conn_timeout = DFL_CONN_TIMEOUT;
Paul Bakker44618dd2013-07-04 10:34:10 +0200488#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000489 opt.max_connections = DFL_MAX_CONNECTIONS;
490 opt.session_reuse = DFL_SESSION_REUSE;
491 opt.session_lifetime = DFL_SESSION_LIFETIME;
Paul Bakkere3166ce2011-01-27 17:40:50 +0000492 opt.force_ciphersuite[0] = DFL_FORCE_CIPHER;
Paul Bakker5121ce52009-01-03 21:22:43 +0000493
494 for( i = 1; i < argc; i++ )
495 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000496 p = argv[i];
497 if( ( q = strchr( p, '=' ) ) == NULL )
498 continue;
499 *q++ = '\0';
500
501 if( strcmp( p, "opmode" ) == 0 )
502 {
503 if( strcmp( q, "client" ) == 0 )
504 opt.opmode = OPMODE_CLIENT;
505 else
506 if( strcmp( q, "server" ) == 0 )
507 opt.opmode = OPMODE_SERVER;
508 else goto usage;
509 }
510
511 if( strcmp( p, "iomode" ) == 0 )
512 {
513 if( strcmp( q, "block" ) == 0 )
514 opt.iomode = IOMODE_BLOCK;
515 else
516 if( strcmp( q, "nonblock" ) == 0 )
517 opt.iomode = IOMODE_NONBLOCK;
518 else goto usage;
519 }
520
521 if( strcmp( p, "server_name" ) == 0 )
522 opt.server_name = q;
523
524 if( strcmp( p, "server_port" ) == 0 )
525 {
526 opt.server_port = atoi( q );
527 if( opt.server_port < 1 || opt.server_port > 65535 )
528 goto usage;
529 }
530
531 if( strcmp( p, "command" ) == 0 )
532 {
533 if( strcmp( q, "read" ) == 0 )
534 opt.command = COMMAND_READ;
535 else
536 if( strcmp( q, "write" ) == 0 )
537 opt.command = COMMAND_WRITE;
538 else
539 if( strcmp( q, "both" ) == 0 )
540 {
541 opt.iomode = IOMODE_NONBLOCK;
542 opt.command = COMMAND_BOTH;
543 }
544 else goto usage;
545 }
546
547 if( strcmp( p, "buffer_size" ) == 0 )
548 {
549 opt.buffer_size = atoi( q );
550 if( opt.buffer_size < 1 || opt.buffer_size > 1048576 )
551 goto usage;
552 }
553
554 if( strcmp( p, "max_bytes" ) == 0 )
555 opt.max_bytes = atoi( q );
556
557 if( strcmp( p, "debug_level" ) == 0 )
558 opt.debug_level = atoi( q );
Paul Bakker44618dd2013-07-04 10:34:10 +0200559#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000560 if( strcmp( p, "conn_timeout" ) == 0 )
561 opt.conn_timeout = atoi( q );
Paul Bakker44618dd2013-07-04 10:34:10 +0200562#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000563 if( strcmp( p, "max_connections" ) == 0 )
564 opt.max_connections = atoi( q );
565
566 if( strcmp( p, "session_reuse" ) == 0 )
567 {
568 if( strcmp( q, "on" ) == 0 )
569 opt.session_reuse = 1;
570 else
571 if( strcmp( q, "off" ) == 0 )
572 opt.session_reuse = 0;
573 else
574 goto usage;
575 }
576
577 if( strcmp( p, "session_lifetime" ) == 0 )
578 opt.session_lifetime = atoi( q );
579
Paul Bakkere3166ce2011-01-27 17:40:50 +0000580 if( strcmp( p, "force_ciphersuite" ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000581 {
Paul Bakkere3166ce2011-01-27 17:40:50 +0000582 opt.force_ciphersuite[0] = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000583
Paul Bakkere3166ce2011-01-27 17:40:50 +0000584 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
Paul Bakker5121ce52009-01-03 21:22:43 +0000585
Paul Bakkere3166ce2011-01-27 17:40:50 +0000586 if( opt.force_ciphersuite[0] <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000587 goto usage;
588
Paul Bakkere3166ce2011-01-27 17:40:50 +0000589 opt.force_ciphersuite[1] = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000590 }
591 }
592
593 switch( opt.opmode )
594 {
595 case OPMODE_CLIENT:
596 break;
597
598 case OPMODE_SERVER:
599 break;
600
601 default:
602 goto usage;
603 }
604
605 nb_conn = 0;
606
607 do {
608 nb_conn++;
609 ret = ssl_test( &opt );
610 if( opt.max_connections != 0 &&
611 opt.max_connections <= nb_conn )
612 break;
613 }
614 while( ret == 0 );
615
616exit:
617
Paul Bakkercce9d772011-11-18 14:26:47 +0000618#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000619 printf( " Press Enter to exit this program.\n" );
620 fflush( stdout ); getchar();
621#endif
622
623 return( ret );
624}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000625#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000626 POLARSSL_SSL_SRV_C && POLARSSL_SSL_CLI_C && POLARSSL_NET_C &&
Paul Bakker508ad5a2011-12-04 17:09:26 +0000627 POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */