blob: 797226bb12e2052dda5391655d0e8e085d4fa5d9 [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
26#ifndef _CRT_SECURE_NO_DEPRECATE
27#define _CRT_SECURE_NO_DEPRECATE 1
28#endif
29
30#include <string.h>
31#include <stdlib.h>
32#include <stdio.h>
33
Paul Bakker5690efc2011-05-26 13:16:06 +000034#include "polarssl/config.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
45#define OPMODE_NONE 0
46#define OPMODE_CLIENT 1
47#define OPMODE_SERVER 2
48
49#define IOMODE_BLOCK 0
50#define IOMODE_NONBLOCK 1
51
52#define COMMAND_READ 1
53#define COMMAND_WRITE 2
54#define COMMAND_BOTH 3
55
56#define DFL_OPMODE OPMODE_NONE
57#define DFL_IOMODE IOMODE_BLOCK
58#define DFL_SERVER_NAME "localhost"
59#define DFL_SERVER_PORT 4433
60#define DFL_COMMAND COMMAND_READ
61#define DFL_BUFFER_SIZE 1024
62#define DFL_MAX_BYTES 0
63#define DFL_DEBUG_LEVEL 0
64#define DFL_CONN_TIMEOUT 0
65#define DFL_MAX_CONNECTIONS 0
66#define DFL_SESSION_REUSE 1
67#define DFL_SESSION_LIFETIME 86400
68#define DFL_FORCE_CIPHER 0
69
Paul Bakker5121ce52009-01-03 21:22:43 +000070int server_fd = -1;
71
72/*
73 * global options
74 */
75struct options
76{
77 int opmode; /* operation mode (client or server) */
78 int iomode; /* I/O mode (blocking or non-blocking) */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020079 const char *server_name; /* hostname of the server (client only) */
Paul Bakker5121ce52009-01-03 21:22:43 +000080 int server_port; /* port on which the ssl service runs */
81 int command; /* what to do: read or write operation */
82 int buffer_size; /* size of the send/receive buffer */
83 int max_bytes; /* max. # of bytes before a reconnect */
84 int debug_level; /* level of debugging */
Paul Bakker44618dd2013-07-04 10:34:10 +020085#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000086 int conn_timeout; /* max. delay before a reconnect */
Paul Bakker44618dd2013-07-04 10:34:10 +020087#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000088 int max_connections; /* max. number of reconnections */
89 int session_reuse; /* flag to reuse the keying material */
90 int session_lifetime; /* if reached, session data is expired */
Paul Bakkere3166ce2011-01-27 17:40:50 +000091 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
Paul Bakker5121ce52009-01-03 21:22:43 +000092};
93
94/*
95 * Although this PRNG has good statistical properties (eg. passes
96 * DIEHARD), it is not cryptographically secure.
97 */
Paul Bakker3c5ef712013-06-25 16:37:45 +020098static unsigned long int lcppm5( unsigned long int *state )
Paul Bakker5121ce52009-01-03 21:22:43 +000099{
100 unsigned long int u, v;
101
102 u = v = state[4] ^ 1;
103 state[u & 3] ^= u;
104 u ^= (v << 12) ^ (v >> 12);
105 u ^= v * state[0]; v >>= 8;
106 u ^= v * state[1]; v >>= 8;
107 u ^= v * state[2]; v >>= 8;
108 u ^= v * state[3];
109 u &= 0xFFFFFFFF;
110 state[4] = u;
111
112 return( u );
113}
114
Paul Bakker3c5ef712013-06-25 16:37:45 +0200115static void my_debug( void *ctx, int level, const char *str )
Paul Bakker5121ce52009-01-03 21:22:43 +0000116{
117 if( level < ((struct options *) ctx)->debug_level )
118 fprintf( stderr, "%s", str );
119}
120
Paul Bakker508ad5a2011-12-04 17:09:26 +0000121#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
Paul Bakker5690efc2011-05-26 13:16:06 +0000122 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \
123 !defined(POLARSSL_SSL_CLI_C) || !defined(POLARSSL_NET_C) || \
Paul Bakkered27a042013-04-18 22:46:23 +0200124 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C) || \
125 !defined(POLARSSL_X509_PARSE_C)
Paul Bakkercce9d772011-11-18 14:26:47 +0000126int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000127{
Paul Bakkercce9d772011-11-18 14:26:47 +0000128 ((void) argc);
129 ((void) argv);
130
Paul Bakker508ad5a2011-12-04 17:09:26 +0000131 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
Paul Bakker5690efc2011-05-26 13:16:06 +0000132 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
133 "POLARSSL_SSL_CLI_C and/or POLARSSL_NET_C and/or "
Paul Bakkered27a042013-04-18 22:46:23 +0200134 "POLARSSL_RSA_C and/or POLARSSL_CTR_DRBG_C and/or "
135 "POLARSSL_X509_PARSE_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +0000136 return( 0 );
137}
138#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000139/*
140 * perform a single SSL connection
141 */
142static int ssl_test( struct options *opt )
143{
144 int ret, i;
145 int client_fd;
146 int bytes_to_read;
147 int bytes_to_write;
Paul Bakker026c03b2009-03-28 17:53:03 +0000148 int offset_to_read = 0;
149 int offset_to_write = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
151 long int nb_read;
152 long int nb_written;
153
154 unsigned long read_state[5];
155 unsigned long write_state[5];
156
Paul Bakker026c03b2009-03-28 17:53:03 +0000157 unsigned char *read_buf = NULL;
158 unsigned char *write_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000159
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200160 const char *pers = "ssl_test";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000161
Paul Bakker44618dd2013-07-04 10:34:10 +0200162#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 struct hr_time t;
Paul Bakker44618dd2013-07-04 10:34:10 +0200164#endif
Paul Bakker508ad5a2011-12-04 17:09:26 +0000165 entropy_context entropy;
166 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 ssl_context ssl;
Paul Bakker5121ce52009-01-03 21:22:43 +0000168 x509_cert srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200169 pk_context pkey;
Paul Bakker5121ce52009-01-03 21:22:43 +0000170
171 ret = 1;
172
Paul Bakker508ad5a2011-12-04 17:09:26 +0000173 entropy_init( &entropy );
174 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200175 (const unsigned char *) pers,
176 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000177 {
178 printf( " ! ctr_drbg_init returned %d\n", ret );
179 goto exit;
180 }
181
Paul Bakker44618dd2013-07-04 10:34:10 +0200182#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 get_timer( &t, 1 );
Paul Bakker44618dd2013-07-04 10:34:10 +0200184#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
186 memset( read_state, 0, sizeof( read_state ) );
187 memset( write_state, 0, sizeof( write_state ) );
188
189 memset( &srvcert, 0, sizeof( x509_cert ) );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200190 pk_init( &pkey );
Paul Bakker5121ce52009-01-03 21:22:43 +0000191
192 if( opt->opmode == OPMODE_CLIENT )
193 {
194 if( ( ret = net_connect( &client_fd, opt->server_name,
195 opt->server_port ) ) != 0 )
196 {
197 printf( " ! net_connect returned %d\n\n", ret );
198 return( ret );
199 }
200
201 if( ( ret = ssl_init( &ssl ) ) != 0 )
202 {
203 printf( " ! ssl_init returned %d\n\n", ret );
204 return( ret );
205 }
206
207 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
208 }
209
210 if( opt->opmode == OPMODE_SERVER )
211 {
Paul Bakker5690efc2011-05-26 13:16:06 +0000212#if !defined(POLARSSL_CERTS_C)
213 printf("POLARSSL_CERTS_C not defined.\n");
214 goto exit;
215#else
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200216 ret = x509parse_crt( &srvcert, (const unsigned char *) test_srv_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000217 strlen( test_srv_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 if( ret != 0 )
219 {
220 printf( " ! x509parse_crt returned %d\n\n", ret );
221 goto exit;
222 }
223
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200224 ret = x509parse_crt( &srvcert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000225 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 if( ret != 0 )
227 {
228 printf( " ! x509parse_crt returned %d\n\n", ret );
229 goto exit;
230 }
231
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200232 ret = x509parse_key( &pkey, (const unsigned char *) test_srv_key,
Paul Bakker5121ce52009-01-03 21:22:43 +0000233 strlen( test_srv_key ), NULL, 0 );
234 if( ret != 0 )
235 {
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200236 printf( " ! x509parse_key returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000237 goto exit;
238 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000239#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000240
241 if( server_fd < 0 )
242 {
243 if( ( ret = net_bind( &server_fd, NULL,
244 opt->server_port ) ) != 0 )
245 {
246 printf( " ! net_bind returned %d\n\n", ret );
247 return( ret );
248 }
249 }
250
251 if( ( ret = net_accept( server_fd, &client_fd, NULL ) ) != 0 )
252 {
253 printf( " ! net_accept returned %d\n\n", ret );
254 return( ret );
255 }
256
257 if( ( ret = ssl_init( &ssl ) ) != 0 )
258 {
259 printf( " ! ssl_init returned %d\n\n", ret );
260 return( ret );
261 }
262
263 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000264 ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200265 ssl_set_own_cert( &ssl, &srvcert, &pkey );
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 }
267
268 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
269
Paul Bakker508ad5a2011-12-04 17:09:26 +0000270 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker5121ce52009-01-03 21:22:43 +0000271 ssl_set_dbg( &ssl, my_debug, opt );
272 ssl_set_bio( &ssl, net_recv, &client_fd,
273 net_send, &client_fd );
274
Paul Bakker68884e32013-01-07 18:20:04 +0100275 if( opt->force_ciphersuite[0] != DFL_FORCE_CIPHER )
276 ssl_set_ciphersuites( &ssl, opt->force_ciphersuite );
Paul Bakker5121ce52009-01-03 21:22:43 +0000277
278 if( opt->iomode == IOMODE_NONBLOCK )
279 net_set_nonblock( client_fd );
280
281 read_buf = (unsigned char *) malloc( opt->buffer_size );
282 write_buf = (unsigned char *) malloc( opt->buffer_size );
283
284 if( read_buf == NULL || write_buf == NULL )
285 {
286 printf( " ! malloc(%d bytes) failed\n\n", opt->buffer_size );
287 goto exit;
288 }
289
290 nb_read = bytes_to_read = 0;
291 nb_written = bytes_to_write = 0;
292
293 while( 1 )
294 {
295 if( opt->command & COMMAND_WRITE )
296 {
297 if( bytes_to_write == 0 )
298 {
299 while( bytes_to_write == 0 )
300 bytes_to_write = rand() % opt->buffer_size;
301
302 for( i = 0; i < bytes_to_write; i++ )
303 write_buf[i] = (unsigned char) lcppm5( write_state );
304
305 offset_to_write = 0;
306 }
307
308 ret = ssl_write( &ssl, write_buf + offset_to_write,
309 bytes_to_write );
310
311 if( ret >= 0 )
312 {
313 nb_written += ret;
314 bytes_to_write -= ret;
315 offset_to_write += ret;
316 }
317
Paul Bakker40e46942009-01-03 21:51:57 +0000318 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY ||
319 ret == POLARSSL_ERR_NET_CONN_RESET )
Paul Bakker5121ce52009-01-03 21:22:43 +0000320 {
321 ret = 0;
322 goto exit;
323 }
324
Paul Bakker831a7552011-05-18 13:32:51 +0000325 if( ret < 0 && ret != POLARSSL_ERR_NET_WANT_READ &&
326 ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000327 {
328 printf( " ! ssl_write returned %d\n\n", ret );
329 break;
330 }
331 }
332
333 if( opt->command & COMMAND_READ )
334 {
335 if( bytes_to_read == 0 )
336 {
337 bytes_to_read = rand() % opt->buffer_size;
338 offset_to_read = 0;
339 }
340
341 ret = ssl_read( &ssl, read_buf + offset_to_read,
342 bytes_to_read );
343
344 if( ret >= 0 )
345 {
346 for( i = 0; i < ret; i++ )
347 {
348 if( read_buf[offset_to_read + i] !=
349 (unsigned char) lcppm5( read_state ) )
350 {
351 ret = 1;
352 printf( " ! plaintext mismatch\n\n" );
353 goto exit;
354 }
355 }
356
357 nb_read += ret;
358 bytes_to_read -= ret;
359 offset_to_read += ret;
360 }
361
Paul Bakker40e46942009-01-03 21:51:57 +0000362 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY ||
363 ret == POLARSSL_ERR_NET_CONN_RESET )
Paul Bakker5121ce52009-01-03 21:22:43 +0000364 {
365 ret = 0;
366 goto exit;
367 }
368
Paul Bakker831a7552011-05-18 13:32:51 +0000369 if( ret < 0 && ret != POLARSSL_ERR_NET_WANT_READ &&
370 ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000371 {
372 printf( " ! ssl_read returned %d\n\n", ret );
373 break;
374 }
375 }
376
377 ret = 0;
378
379 if( opt->max_bytes != 0 &&
380 ( opt->max_bytes <= nb_read ||
381 opt->max_bytes <= nb_written ) )
382 break;
383
Paul Bakker44618dd2013-07-04 10:34:10 +0200384#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000385 if( opt->conn_timeout != 0 &&
386 opt->conn_timeout <= (int) get_timer( &t, 0 ) )
387 break;
Paul Bakker44618dd2013-07-04 10:34:10 +0200388#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000389 }
390
391exit:
392
393 fflush( stdout );
394
395 if( read_buf != NULL )
396 free( read_buf );
397
398 if( write_buf != NULL )
399 free( write_buf );
400
401 ssl_close_notify( &ssl );
402 x509_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200403 pk_free( &pkey );
Paul Bakker5121ce52009-01-03 21:22:43 +0000404 ssl_free( &ssl );
405 net_close( client_fd );
406
407 return( ret );
408}
409
Paul Bakker44618dd2013-07-04 10:34:10 +0200410#if defined(POLARSSL_TIMING_C)
411#define USAGE_TIMING \
412 " conn_timeout=%%d (ms) default: 0 (no timeout)\n"
413#else
414#define USAGE_TIMING ""
415#endif
416
Paul Bakker5121ce52009-01-03 21:22:43 +0000417#define USAGE \
418 "\n usage: ssl_test opmode=<> command=<>...\n" \
419 "\n acceptable parameters:\n" \
420 " opmode=client/server default: <none>\n" \
421 " iomode=block/nonblock default: block\n" \
422 " server_name=%%s default: localhost\n" \
423 " server_port=%%d default: 4433\n" \
424 " command=read/write/both default: read\n" \
425 " buffer_size=%%d (bytes) default: 1024\n" \
426 " max_bytes=%%d (bytes) default: 0 (no limit)\n" \
427 " debug_level=%%d default: 0 (disabled)\n" \
Paul Bakker44618dd2013-07-04 10:34:10 +0200428 USAGE_TIMING \
Paul Bakker5121ce52009-01-03 21:22:43 +0000429 " max_connections=%%d default: 0 (no limit)\n" \
430 " session_reuse=on/off default: on (enabled)\n" \
431 " session_lifetime=%%d (s) default: 86400\n" \
Paul Bakkere3166ce2011-01-27 17:40:50 +0000432 " force_ciphersuite=<name> default: all enabled\n" \
433 " acceptable ciphersuite names:\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000434
435int main( int argc, char *argv[] )
436{
437 int i, j, n;
Paul Bakkere3166ce2011-01-27 17:40:50 +0000438 const int *list;
Paul Bakker5121ce52009-01-03 21:22:43 +0000439 int ret = 1;
440 int nb_conn;
441 char *p, *q;
442 struct options opt;
443
444 if( argc == 1 )
445 {
446 usage:
447 printf( USAGE );
Paul Bakker44618dd2013-07-04 10:34:10 +0200448
Paul Bakkere3166ce2011-01-27 17:40:50 +0000449 list = ssl_list_ciphersuites();
450 while( *list )
451 {
452 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
453 list++;
454 }
455 printf("\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000456 goto exit;
457 }
458
459 opt.opmode = DFL_OPMODE;
460 opt.iomode = DFL_IOMODE;
461 opt.server_name = DFL_SERVER_NAME;
462 opt.server_port = DFL_SERVER_PORT;
463 opt.command = DFL_COMMAND;
464 opt.buffer_size = DFL_BUFFER_SIZE;
465 opt.max_bytes = DFL_MAX_BYTES;
466 opt.debug_level = DFL_DEBUG_LEVEL;
Paul Bakker44618dd2013-07-04 10:34:10 +0200467#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000468 opt.conn_timeout = DFL_CONN_TIMEOUT;
Paul Bakker44618dd2013-07-04 10:34:10 +0200469#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000470 opt.max_connections = DFL_MAX_CONNECTIONS;
471 opt.session_reuse = DFL_SESSION_REUSE;
472 opt.session_lifetime = DFL_SESSION_LIFETIME;
Paul Bakkere3166ce2011-01-27 17:40:50 +0000473 opt.force_ciphersuite[0] = DFL_FORCE_CIPHER;
Paul Bakker5121ce52009-01-03 21:22:43 +0000474
475 for( i = 1; i < argc; i++ )
476 {
477 n = strlen( argv[i] );
478
479 for( j = 0; j < n; j++ )
480 {
481 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
482 argv[i][j] |= 0x20;
483 }
484
485 p = argv[i];
486 if( ( q = strchr( p, '=' ) ) == NULL )
487 continue;
488 *q++ = '\0';
489
490 if( strcmp( p, "opmode" ) == 0 )
491 {
492 if( strcmp( q, "client" ) == 0 )
493 opt.opmode = OPMODE_CLIENT;
494 else
495 if( strcmp( q, "server" ) == 0 )
496 opt.opmode = OPMODE_SERVER;
497 else goto usage;
498 }
499
500 if( strcmp( p, "iomode" ) == 0 )
501 {
502 if( strcmp( q, "block" ) == 0 )
503 opt.iomode = IOMODE_BLOCK;
504 else
505 if( strcmp( q, "nonblock" ) == 0 )
506 opt.iomode = IOMODE_NONBLOCK;
507 else goto usage;
508 }
509
510 if( strcmp( p, "server_name" ) == 0 )
511 opt.server_name = q;
512
513 if( strcmp( p, "server_port" ) == 0 )
514 {
515 opt.server_port = atoi( q );
516 if( opt.server_port < 1 || opt.server_port > 65535 )
517 goto usage;
518 }
519
520 if( strcmp( p, "command" ) == 0 )
521 {
522 if( strcmp( q, "read" ) == 0 )
523 opt.command = COMMAND_READ;
524 else
525 if( strcmp( q, "write" ) == 0 )
526 opt.command = COMMAND_WRITE;
527 else
528 if( strcmp( q, "both" ) == 0 )
529 {
530 opt.iomode = IOMODE_NONBLOCK;
531 opt.command = COMMAND_BOTH;
532 }
533 else goto usage;
534 }
535
536 if( strcmp( p, "buffer_size" ) == 0 )
537 {
538 opt.buffer_size = atoi( q );
539 if( opt.buffer_size < 1 || opt.buffer_size > 1048576 )
540 goto usage;
541 }
542
543 if( strcmp( p, "max_bytes" ) == 0 )
544 opt.max_bytes = atoi( q );
545
546 if( strcmp( p, "debug_level" ) == 0 )
547 opt.debug_level = atoi( q );
Paul Bakker44618dd2013-07-04 10:34:10 +0200548#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000549 if( strcmp( p, "conn_timeout" ) == 0 )
550 opt.conn_timeout = atoi( q );
Paul Bakker44618dd2013-07-04 10:34:10 +0200551#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 if( strcmp( p, "max_connections" ) == 0 )
553 opt.max_connections = atoi( q );
554
555 if( strcmp( p, "session_reuse" ) == 0 )
556 {
557 if( strcmp( q, "on" ) == 0 )
558 opt.session_reuse = 1;
559 else
560 if( strcmp( q, "off" ) == 0 )
561 opt.session_reuse = 0;
562 else
563 goto usage;
564 }
565
566 if( strcmp( p, "session_lifetime" ) == 0 )
567 opt.session_lifetime = atoi( q );
568
Paul Bakkere3166ce2011-01-27 17:40:50 +0000569 if( strcmp( p, "force_ciphersuite" ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000570 {
Paul Bakkere3166ce2011-01-27 17:40:50 +0000571 opt.force_ciphersuite[0] = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000572
Paul Bakkere3166ce2011-01-27 17:40:50 +0000573 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
Paul Bakker5121ce52009-01-03 21:22:43 +0000574
Paul Bakkere3166ce2011-01-27 17:40:50 +0000575 if( opt.force_ciphersuite[0] <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000576 goto usage;
577
Paul Bakkere3166ce2011-01-27 17:40:50 +0000578 opt.force_ciphersuite[1] = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 }
580 }
581
582 switch( opt.opmode )
583 {
584 case OPMODE_CLIENT:
585 break;
586
587 case OPMODE_SERVER:
588 break;
589
590 default:
591 goto usage;
592 }
593
594 nb_conn = 0;
595
596 do {
597 nb_conn++;
598 ret = ssl_test( &opt );
599 if( opt.max_connections != 0 &&
600 opt.max_connections <= nb_conn )
601 break;
602 }
603 while( ret == 0 );
604
605exit:
606
Paul Bakkercce9d772011-11-18 14:26:47 +0000607#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000608 printf( " Press Enter to exit this program.\n" );
609 fflush( stdout ); getchar();
610#endif
611
612 return( ret );
613}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000614#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000615 POLARSSL_SSL_SRV_C && POLARSSL_SSL_CLI_C && POLARSSL_NET_C &&
Paul Bakker508ad5a2011-12-04 17:09:26 +0000616 POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */