blob: ea73d50a6b939a715ed9969f1c8f83cad573290e [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é-Gonnardabd6e022013-09-20 13:30:43 +020026#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
28#include <string.h>
29#include <stdlib.h>
30#include <stdio.h>
31
Paul Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/net.h"
33#include "polarssl/ssl.h"
Paul Bakker508ad5a2011-12-04 17:09:26 +000034#include "polarssl/entropy.h"
35#include "polarssl/ctr_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000036#include "polarssl/certs.h"
Paul Bakker44618dd2013-07-04 10:34:10 +020037#if defined(POLARSSL_TIMING_C)
38#include "polarssl/timing.h"
39#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Paul Bakker9a97c5d2013-09-15 17:07:33 +020041#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
42 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \
43 !defined(POLARSSL_SSL_CLI_C) || !defined(POLARSSL_NET_C) || \
44 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020045 !defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker9a97c5d2013-09-15 17:07:33 +020046int main( int argc, char *argv[] )
47{
48 ((void) argc);
49 ((void) argv);
50
51 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
52 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
53 "POLARSSL_SSL_CLI_C and/or POLARSSL_NET_C and/or "
54 "POLARSSL_RSA_C and/or POLARSSL_CTR_DRBG_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020055 "POLARSSL_X509_CRT_PARSE_C not defined.\n");
Paul Bakker9a97c5d2013-09-15 17:07:33 +020056 return( 0 );
57}
58#else
59
Paul Bakker5121ce52009-01-03 21:22:43 +000060#define OPMODE_NONE 0
61#define OPMODE_CLIENT 1
62#define OPMODE_SERVER 2
63
64#define IOMODE_BLOCK 0
65#define IOMODE_NONBLOCK 1
66
67#define COMMAND_READ 1
68#define COMMAND_WRITE 2
69#define COMMAND_BOTH 3
70
71#define DFL_OPMODE OPMODE_NONE
72#define DFL_IOMODE IOMODE_BLOCK
73#define DFL_SERVER_NAME "localhost"
74#define DFL_SERVER_PORT 4433
75#define DFL_COMMAND COMMAND_READ
76#define DFL_BUFFER_SIZE 1024
77#define DFL_MAX_BYTES 0
78#define DFL_DEBUG_LEVEL 0
79#define DFL_CONN_TIMEOUT 0
80#define DFL_MAX_CONNECTIONS 0
81#define DFL_SESSION_REUSE 1
82#define DFL_SESSION_LIFETIME 86400
83#define DFL_FORCE_CIPHER 0
84
Paul Bakker5121ce52009-01-03 21:22:43 +000085int server_fd = -1;
86
87/*
88 * global options
89 */
90struct options
91{
92 int opmode; /* operation mode (client or server) */
93 int iomode; /* I/O mode (blocking or non-blocking) */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020094 const char *server_name; /* hostname of the server (client only) */
Paul Bakker5121ce52009-01-03 21:22:43 +000095 int server_port; /* port on which the ssl service runs */
96 int command; /* what to do: read or write operation */
97 int buffer_size; /* size of the send/receive buffer */
98 int max_bytes; /* max. # of bytes before a reconnect */
99 int debug_level; /* level of debugging */
Paul Bakker44618dd2013-07-04 10:34:10 +0200100#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 int conn_timeout; /* max. delay before a reconnect */
Paul Bakker44618dd2013-07-04 10:34:10 +0200102#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 int max_connections; /* max. number of reconnections */
104 int session_reuse; /* flag to reuse the keying material */
105 int session_lifetime; /* if reached, session data is expired */
Paul Bakkere3166ce2011-01-27 17:40:50 +0000106 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
Paul Bakker5121ce52009-01-03 21:22:43 +0000107};
108
109/*
110 * Although this PRNG has good statistical properties (eg. passes
111 * DIEHARD), it is not cryptographically secure.
112 */
Paul Bakker3c5ef712013-06-25 16:37:45 +0200113static unsigned long int lcppm5( unsigned long int *state )
Paul Bakker5121ce52009-01-03 21:22:43 +0000114{
115 unsigned long int u, v;
116
117 u = v = state[4] ^ 1;
118 state[u & 3] ^= u;
119 u ^= (v << 12) ^ (v >> 12);
120 u ^= v * state[0]; v >>= 8;
121 u ^= v * state[1]; v >>= 8;
122 u ^= v * state[2]; v >>= 8;
123 u ^= v * state[3];
124 u &= 0xFFFFFFFF;
125 state[4] = u;
126
127 return( u );
128}
129
Paul Bakker3c5ef712013-06-25 16:37:45 +0200130static void my_debug( void *ctx, int level, const char *str )
Paul Bakker5121ce52009-01-03 21:22:43 +0000131{
132 if( level < ((struct options *) ctx)->debug_level )
133 fprintf( stderr, "%s", str );
134}
135
136/*
137 * perform a single SSL connection
138 */
139static int ssl_test( struct options *opt )
140{
141 int ret, i;
Manuel Pégourié-Gonnard68821da2013-09-16 12:34:33 +0200142 int client_fd = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 int bytes_to_read;
144 int bytes_to_write;
Paul Bakker026c03b2009-03-28 17:53:03 +0000145 int offset_to_read = 0;
146 int offset_to_write = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000147
148 long int nb_read;
149 long int nb_written;
150
151 unsigned long read_state[5];
152 unsigned long write_state[5];
153
Paul Bakker026c03b2009-03-28 17:53:03 +0000154 unsigned char *read_buf = NULL;
155 unsigned char *write_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200157 const char *pers = "ssl_test";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000158
Paul Bakker44618dd2013-07-04 10:34:10 +0200159#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 struct hr_time t;
Paul Bakker44618dd2013-07-04 10:34:10 +0200161#endif
Paul Bakker508ad5a2011-12-04 17:09:26 +0000162 entropy_context entropy;
163 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 ssl_context ssl;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200165 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200166 pk_context pkey;
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
168 ret = 1;
169
Paul Bakker508ad5a2011-12-04 17:09:26 +0000170 entropy_init( &entropy );
171 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200172 (const unsigned char *) pers,
173 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000174 {
175 printf( " ! ctr_drbg_init returned %d\n", ret );
176 goto exit;
177 }
178
Paul Bakker44618dd2013-07-04 10:34:10 +0200179#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000180 get_timer( &t, 1 );
Paul Bakker44618dd2013-07-04 10:34:10 +0200181#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000182
183 memset( read_state, 0, sizeof( read_state ) );
184 memset( write_state, 0, sizeof( write_state ) );
185
Paul Bakker369d2eb2013-09-18 11:58:25 +0200186 x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200187 pk_init( &pkey );
Paul Bakker5121ce52009-01-03 21:22:43 +0000188
189 if( opt->opmode == OPMODE_CLIENT )
190 {
191 if( ( ret = net_connect( &client_fd, opt->server_name,
192 opt->server_port ) ) != 0 )
193 {
194 printf( " ! net_connect returned %d\n\n", ret );
195 return( ret );
196 }
197
198 if( ( ret = ssl_init( &ssl ) ) != 0 )
199 {
200 printf( " ! ssl_init returned %d\n\n", ret );
201 return( ret );
202 }
203
204 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
205 }
206
207 if( opt->opmode == OPMODE_SERVER )
208 {
Paul Bakker5690efc2011-05-26 13:16:06 +0000209#if !defined(POLARSSL_CERTS_C)
210 printf("POLARSSL_CERTS_C not defined.\n");
211 goto exit;
212#else
Paul Bakkerddf26b42013-09-18 13:46:23 +0200213 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_srv_crt,
214 strlen( test_srv_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000215 if( ret != 0 )
216 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200217 printf( " ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 goto exit;
219 }
220
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200221 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_ca_list,
222 strlen( test_ca_list ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 if( ret != 0 )
224 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200225 printf( " ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 goto exit;
227 }
228
Paul Bakker1a7550a2013-09-15 13:01:22 +0200229 ret = pk_parse_key( &pkey, (const unsigned char *) test_srv_key,
230 strlen( test_srv_key ), NULL, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000231 if( ret != 0 )
232 {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200233 printf( " ! pk_parse_key returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000234 goto exit;
235 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000236#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000237
238 if( server_fd < 0 )
239 {
240 if( ( ret = net_bind( &server_fd, NULL,
241 opt->server_port ) ) != 0 )
242 {
243 printf( " ! net_bind returned %d\n\n", ret );
244 return( ret );
245 }
246 }
247
248 if( ( ret = net_accept( server_fd, &client_fd, NULL ) ) != 0 )
249 {
250 printf( " ! net_accept returned %d\n\n", ret );
251 return( ret );
252 }
253
254 if( ( ret = ssl_init( &ssl ) ) != 0 )
255 {
256 printf( " ! ssl_init returned %d\n\n", ret );
257 return( ret );
258 }
259
260 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000261 ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200262 ssl_set_own_cert( &ssl, &srvcert, &pkey );
Paul Bakker5121ce52009-01-03 21:22:43 +0000263 }
264
265 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
266
Paul Bakker508ad5a2011-12-04 17:09:26 +0000267 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker5121ce52009-01-03 21:22:43 +0000268 ssl_set_dbg( &ssl, my_debug, opt );
269 ssl_set_bio( &ssl, net_recv, &client_fd,
270 net_send, &client_fd );
271
Paul Bakker68884e32013-01-07 18:20:04 +0100272 if( opt->force_ciphersuite[0] != DFL_FORCE_CIPHER )
273 ssl_set_ciphersuites( &ssl, opt->force_ciphersuite );
Paul Bakker5121ce52009-01-03 21:22:43 +0000274
275 if( opt->iomode == IOMODE_NONBLOCK )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200276 {
277 if( ( ret = net_set_nonblock( client_fd ) ) != 0 )
278 {
279 printf( " ! net_set_nonblock returned %d\n\n", ret );
280 return( ret );
281 }
282 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000283
284 read_buf = (unsigned char *) malloc( opt->buffer_size );
285 write_buf = (unsigned char *) malloc( opt->buffer_size );
286
287 if( read_buf == NULL || write_buf == NULL )
288 {
289 printf( " ! malloc(%d bytes) failed\n\n", opt->buffer_size );
290 goto exit;
291 }
292
293 nb_read = bytes_to_read = 0;
294 nb_written = bytes_to_write = 0;
295
296 while( 1 )
297 {
298 if( opt->command & COMMAND_WRITE )
299 {
300 if( bytes_to_write == 0 )
301 {
302 while( bytes_to_write == 0 )
303 bytes_to_write = rand() % opt->buffer_size;
304
305 for( i = 0; i < bytes_to_write; i++ )
306 write_buf[i] = (unsigned char) lcppm5( write_state );
307
308 offset_to_write = 0;
309 }
310
311 ret = ssl_write( &ssl, write_buf + offset_to_write,
312 bytes_to_write );
313
314 if( ret >= 0 )
315 {
316 nb_written += ret;
317 bytes_to_write -= ret;
318 offset_to_write += ret;
319 }
320
Paul Bakker40e46942009-01-03 21:51:57 +0000321 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY ||
322 ret == POLARSSL_ERR_NET_CONN_RESET )
Paul Bakker5121ce52009-01-03 21:22:43 +0000323 {
324 ret = 0;
325 goto exit;
326 }
327
Paul Bakker831a7552011-05-18 13:32:51 +0000328 if( ret < 0 && ret != POLARSSL_ERR_NET_WANT_READ &&
329 ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000330 {
331 printf( " ! ssl_write returned %d\n\n", ret );
332 break;
333 }
334 }
335
336 if( opt->command & COMMAND_READ )
337 {
Paul Bakker396333e2013-09-26 13:32:19 +0200338 while( bytes_to_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000339 {
340 bytes_to_read = rand() % opt->buffer_size;
341 offset_to_read = 0;
342 }
343
344 ret = ssl_read( &ssl, read_buf + offset_to_read,
345 bytes_to_read );
346
Paul Bakker396333e2013-09-26 13:32:19 +0200347 if( ret > 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000348 {
349 for( i = 0; i < ret; i++ )
350 {
351 if( read_buf[offset_to_read + i] !=
352 (unsigned char) lcppm5( read_state ) )
353 {
354 ret = 1;
355 printf( " ! plaintext mismatch\n\n" );
356 goto exit;
357 }
358 }
359
360 nb_read += ret;
361 bytes_to_read -= ret;
362 offset_to_read += ret;
363 }
364
Paul Bakker396333e2013-09-26 13:32:19 +0200365 if( ret == 0 ||
366 ret == POLARSSL_ERR_SSL_CONN_EOF ||
367 ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY ||
Paul Bakker40e46942009-01-03 21:51:57 +0000368 ret == POLARSSL_ERR_NET_CONN_RESET )
Paul Bakker5121ce52009-01-03 21:22:43 +0000369 {
370 ret = 0;
371 goto exit;
372 }
373
Paul Bakker831a7552011-05-18 13:32:51 +0000374 if( ret < 0 && ret != POLARSSL_ERR_NET_WANT_READ &&
375 ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000376 {
377 printf( " ! ssl_read returned %d\n\n", ret );
378 break;
379 }
380 }
381
382 ret = 0;
383
384 if( opt->max_bytes != 0 &&
385 ( opt->max_bytes <= nb_read ||
386 opt->max_bytes <= nb_written ) )
387 break;
388
Paul Bakker44618dd2013-07-04 10:34:10 +0200389#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000390 if( opt->conn_timeout != 0 &&
391 opt->conn_timeout <= (int) get_timer( &t, 0 ) )
392 break;
Paul Bakker44618dd2013-07-04 10:34:10 +0200393#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000394 }
395
396exit:
397
398 fflush( stdout );
399
400 if( read_buf != NULL )
401 free( read_buf );
402
403 if( write_buf != NULL )
404 free( write_buf );
405
406 ssl_close_notify( &ssl );
Paul Bakker36713e82013-09-17 13:25:29 +0200407 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200408 pk_free( &pkey );
Paul Bakker5121ce52009-01-03 21:22:43 +0000409 ssl_free( &ssl );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200410 entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 net_close( client_fd );
412
413 return( ret );
414}
415
Paul Bakker44618dd2013-07-04 10:34:10 +0200416#if defined(POLARSSL_TIMING_C)
417#define USAGE_TIMING \
418 " conn_timeout=%%d (ms) default: 0 (no timeout)\n"
419#else
420#define USAGE_TIMING ""
421#endif
422
Paul Bakker5121ce52009-01-03 21:22:43 +0000423#define USAGE \
424 "\n usage: ssl_test opmode=<> command=<>...\n" \
425 "\n acceptable parameters:\n" \
426 " opmode=client/server default: <none>\n" \
427 " iomode=block/nonblock default: block\n" \
428 " server_name=%%s default: localhost\n" \
429 " server_port=%%d default: 4433\n" \
430 " command=read/write/both default: read\n" \
431 " buffer_size=%%d (bytes) default: 1024\n" \
432 " max_bytes=%%d (bytes) default: 0 (no limit)\n" \
433 " debug_level=%%d default: 0 (disabled)\n" \
Paul Bakker44618dd2013-07-04 10:34:10 +0200434 USAGE_TIMING \
Paul Bakker5121ce52009-01-03 21:22:43 +0000435 " max_connections=%%d default: 0 (no limit)\n" \
436 " session_reuse=on/off default: on (enabled)\n" \
437 " session_lifetime=%%d (s) default: 86400\n" \
Paul Bakkere3166ce2011-01-27 17:40:50 +0000438 " force_ciphersuite=<name> default: all enabled\n" \
439 " acceptable ciphersuite names:\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000440
441int main( int argc, char *argv[] )
442{
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100443 int i;
Paul Bakkere3166ce2011-01-27 17:40:50 +0000444 const int *list;
Paul Bakker5121ce52009-01-03 21:22:43 +0000445 int ret = 1;
446 int nb_conn;
447 char *p, *q;
448 struct options opt;
449
450 if( argc == 1 )
451 {
452 usage:
453 printf( USAGE );
Paul Bakker44618dd2013-07-04 10:34:10 +0200454
Paul Bakkere3166ce2011-01-27 17:40:50 +0000455 list = ssl_list_ciphersuites();
456 while( *list )
457 {
458 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
459 list++;
460 }
461 printf("\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000462 goto exit;
463 }
464
465 opt.opmode = DFL_OPMODE;
466 opt.iomode = DFL_IOMODE;
467 opt.server_name = DFL_SERVER_NAME;
468 opt.server_port = DFL_SERVER_PORT;
469 opt.command = DFL_COMMAND;
470 opt.buffer_size = DFL_BUFFER_SIZE;
471 opt.max_bytes = DFL_MAX_BYTES;
472 opt.debug_level = DFL_DEBUG_LEVEL;
Paul Bakker44618dd2013-07-04 10:34:10 +0200473#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000474 opt.conn_timeout = DFL_CONN_TIMEOUT;
Paul Bakker44618dd2013-07-04 10:34:10 +0200475#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000476 opt.max_connections = DFL_MAX_CONNECTIONS;
477 opt.session_reuse = DFL_SESSION_REUSE;
478 opt.session_lifetime = DFL_SESSION_LIFETIME;
Paul Bakkere3166ce2011-01-27 17:40:50 +0000479 opt.force_ciphersuite[0] = DFL_FORCE_CIPHER;
Paul Bakker5121ce52009-01-03 21:22:43 +0000480
481 for( i = 1; i < argc; i++ )
482 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000483 p = argv[i];
484 if( ( q = strchr( p, '=' ) ) == NULL )
485 continue;
486 *q++ = '\0';
487
488 if( strcmp( p, "opmode" ) == 0 )
489 {
490 if( strcmp( q, "client" ) == 0 )
491 opt.opmode = OPMODE_CLIENT;
492 else
493 if( strcmp( q, "server" ) == 0 )
494 opt.opmode = OPMODE_SERVER;
495 else goto usage;
496 }
497
498 if( strcmp( p, "iomode" ) == 0 )
499 {
500 if( strcmp( q, "block" ) == 0 )
501 opt.iomode = IOMODE_BLOCK;
502 else
503 if( strcmp( q, "nonblock" ) == 0 )
504 opt.iomode = IOMODE_NONBLOCK;
505 else goto usage;
506 }
507
508 if( strcmp( p, "server_name" ) == 0 )
509 opt.server_name = q;
510
511 if( strcmp( p, "server_port" ) == 0 )
512 {
513 opt.server_port = atoi( q );
514 if( opt.server_port < 1 || opt.server_port > 65535 )
515 goto usage;
516 }
517
518 if( strcmp( p, "command" ) == 0 )
519 {
520 if( strcmp( q, "read" ) == 0 )
521 opt.command = COMMAND_READ;
522 else
523 if( strcmp( q, "write" ) == 0 )
524 opt.command = COMMAND_WRITE;
525 else
526 if( strcmp( q, "both" ) == 0 )
527 {
528 opt.iomode = IOMODE_NONBLOCK;
529 opt.command = COMMAND_BOTH;
530 }
531 else goto usage;
532 }
533
534 if( strcmp( p, "buffer_size" ) == 0 )
535 {
536 opt.buffer_size = atoi( q );
537 if( opt.buffer_size < 1 || opt.buffer_size > 1048576 )
538 goto usage;
539 }
540
541 if( strcmp( p, "max_bytes" ) == 0 )
542 opt.max_bytes = atoi( q );
543
544 if( strcmp( p, "debug_level" ) == 0 )
545 opt.debug_level = atoi( q );
Paul Bakker44618dd2013-07-04 10:34:10 +0200546#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000547 if( strcmp( p, "conn_timeout" ) == 0 )
548 opt.conn_timeout = atoi( q );
Paul Bakker44618dd2013-07-04 10:34:10 +0200549#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000550 if( strcmp( p, "max_connections" ) == 0 )
551 opt.max_connections = atoi( q );
552
553 if( strcmp( p, "session_reuse" ) == 0 )
554 {
555 if( strcmp( q, "on" ) == 0 )
556 opt.session_reuse = 1;
557 else
558 if( strcmp( q, "off" ) == 0 )
559 opt.session_reuse = 0;
560 else
561 goto usage;
562 }
563
564 if( strcmp( p, "session_lifetime" ) == 0 )
565 opt.session_lifetime = atoi( q );
566
Paul Bakkere3166ce2011-01-27 17:40:50 +0000567 if( strcmp( p, "force_ciphersuite" ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 {
Paul Bakkere3166ce2011-01-27 17:40:50 +0000569 opt.force_ciphersuite[0] = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000570
Paul Bakkere3166ce2011-01-27 17:40:50 +0000571 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
Paul Bakker5121ce52009-01-03 21:22:43 +0000572
Paul Bakkere3166ce2011-01-27 17:40:50 +0000573 if( opt.force_ciphersuite[0] <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 goto usage;
575
Paul Bakkere3166ce2011-01-27 17:40:50 +0000576 opt.force_ciphersuite[1] = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000577 }
578 }
579
580 switch( opt.opmode )
581 {
582 case OPMODE_CLIENT:
583 break;
584
585 case OPMODE_SERVER:
586 break;
587
588 default:
589 goto usage;
590 }
591
592 nb_conn = 0;
593
594 do {
595 nb_conn++;
596 ret = ssl_test( &opt );
597 if( opt.max_connections != 0 &&
598 opt.max_connections <= nb_conn )
599 break;
600 }
601 while( ret == 0 );
602
603exit:
604
Paul Bakkercce9d772011-11-18 14:26:47 +0000605#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000606 printf( " Press Enter to exit this program.\n" );
607 fflush( stdout ); getchar();
608#endif
609
610 return( ret );
611}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000612#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000613 POLARSSL_SSL_SRV_C && POLARSSL_SSL_CLI_C && POLARSSL_NET_C &&
Paul Bakker508ad5a2011-12-04 17:09:26 +0000614 POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */