blob: debdb073e99129401e67e2347207d6aaa2df3994 [file] [log] [blame] [raw]
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>
Paul Bakker5690efc2011-05-26 13:16:06 +000031
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 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000271
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 )
276 net_set_nonblock( client_fd );
277
278 read_buf = (unsigned char *) malloc( opt->buffer_size );
279 write_buf = (unsigned char *) malloc( opt->buffer_size );
280
281 if( read_buf == NULL || write_buf == NULL )
282 {
283 printf( " ! malloc(%d bytes) failed\n\n", opt->buffer_size );
284 goto exit;
285 }
286
287 nb_read = bytes_to_read = 0;
288 nb_written = bytes_to_write = 0;
289
290 while( 1 )
291 {
292 if( opt->command & COMMAND_WRITE )
293 {
294 if( bytes_to_write == 0 )
295 {
296 while( bytes_to_write == 0 )
297 bytes_to_write = rand() % opt->buffer_size;
298
299 for( i = 0; i < bytes_to_write; i++ )
300 write_buf[i] = (unsigned char) lcppm5( write_state );
301
302 offset_to_write = 0;
303 }
304
305 ret = ssl_write( &ssl, write_buf + offset_to_write,
306 bytes_to_write );
307
308 if( ret >= 0 )
309 {
310 nb_written += ret;
311 bytes_to_write -= ret;
312 offset_to_write += ret;
313 }
314
Paul Bakker40e46942009-01-03 21:51:57 +0000315 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY ||
316 ret == POLARSSL_ERR_NET_CONN_RESET )
Paul Bakker5121ce52009-01-03 21:22:43 +0000317 {
318 ret = 0;
319 goto exit;
320 }
321
Paul Bakker831a7552011-05-18 13:32:51 +0000322 if( ret < 0 && ret != POLARSSL_ERR_NET_WANT_READ &&
323 ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000324 {
325 printf( " ! ssl_write returned %d\n\n", ret );
326 break;
327 }
328 }
329
330 if( opt->command & COMMAND_READ )
331 {
Paul Bakker396333e2013-09-26 13:32:19 +0200332 while( bytes_to_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 {
334 bytes_to_read = rand() % opt->buffer_size;
335 offset_to_read = 0;
336 }
337
338 ret = ssl_read( &ssl, read_buf + offset_to_read,
339 bytes_to_read );
340
Paul Bakker396333e2013-09-26 13:32:19 +0200341 if( ret > 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000342 {
343 for( i = 0; i < ret; i++ )
344 {
345 if( read_buf[offset_to_read + i] !=
346 (unsigned char) lcppm5( read_state ) )
347 {
348 ret = 1;
349 printf( " ! plaintext mismatch\n\n" );
350 goto exit;
351 }
352 }
353
354 nb_read += ret;
355 bytes_to_read -= ret;
356 offset_to_read += ret;
357 }
358
Paul Bakker396333e2013-09-26 13:32:19 +0200359 if( ret == 0 ||
360 ret == POLARSSL_ERR_SSL_CONN_EOF ||
361 ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY ||
Paul Bakker40e46942009-01-03 21:51:57 +0000362 ret == POLARSSL_ERR_NET_CONN_RESET )
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 {
364 ret = 0;
365 goto exit;
366 }
367
Paul Bakker831a7552011-05-18 13:32:51 +0000368 if( ret < 0 && ret != POLARSSL_ERR_NET_WANT_READ &&
369 ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000370 {
371 printf( " ! ssl_read returned %d\n\n", ret );
372 break;
373 }
374 }
375
376 ret = 0;
377
378 if( opt->max_bytes != 0 &&
379 ( opt->max_bytes <= nb_read ||
380 opt->max_bytes <= nb_written ) )
381 break;
382
Paul Bakker44618dd2013-07-04 10:34:10 +0200383#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000384 if( opt->conn_timeout != 0 &&
385 opt->conn_timeout <= (int) get_timer( &t, 0 ) )
386 break;
Paul Bakker44618dd2013-07-04 10:34:10 +0200387#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000388 }
389
390exit:
391
392 fflush( stdout );
393
394 if( read_buf != NULL )
395 free( read_buf );
396
397 if( write_buf != NULL )
398 free( write_buf );
399
400 ssl_close_notify( &ssl );
Paul Bakker36713e82013-09-17 13:25:29 +0200401 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200402 pk_free( &pkey );
Paul Bakker5121ce52009-01-03 21:22:43 +0000403 ssl_free( &ssl );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200404 entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000405 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 */