blob: c414ad0324e2cb62e89e2793ba954abd959b51e2 [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é-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://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
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Rich Evansf90016a2015-01-19 14:26:37 +000033#define polarssl_free free
Rich Evans18b78c72015-02-11 14:06:19 +000034#define polarssl_malloc malloc
35#define polarssl_fprintf fprintf
36#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000037#endif
38
Rich Evans18b78c72015-02-11 14:06:19 +000039#if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_ENTROPY_C) &&\
40 defined(POLARSSL_SSL_TLS_C) && defined(POLARSSL_SSL_SRV_C) &&\
41 defined(POLARSSL_SSL_CLI_C) && defined(POLARSSL_NET_C) &&\
42 defined(POLARSSL_RSA_C) && defined(POLARSSL_CTR_DRBG_C) &&\
43 defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker40e46942009-01-03 21:51:57 +000044#include "polarssl/net.h"
45#include "polarssl/ssl.h"
Paul Bakker508ad5a2011-12-04 17:09:26 +000046#include "polarssl/entropy.h"
47#include "polarssl/ctr_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000048#include "polarssl/certs.h"
Rich Evans18b78c72015-02-11 14:06:19 +000049
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53#endif
54
Paul Bakker44618dd2013-07-04 10:34:10 +020055#if defined(POLARSSL_TIMING_C)
56#include "polarssl/timing.h"
57#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000058
59#define OPMODE_NONE 0
60#define OPMODE_CLIENT 1
61#define OPMODE_SERVER 2
62
63#define IOMODE_BLOCK 0
64#define IOMODE_NONBLOCK 1
65
66#define COMMAND_READ 1
67#define COMMAND_WRITE 2
68#define COMMAND_BOTH 3
69
70#define DFL_OPMODE OPMODE_NONE
71#define DFL_IOMODE IOMODE_BLOCK
72#define DFL_SERVER_NAME "localhost"
73#define DFL_SERVER_PORT 4433
74#define DFL_COMMAND COMMAND_READ
75#define DFL_BUFFER_SIZE 1024
76#define DFL_MAX_BYTES 0
77#define DFL_DEBUG_LEVEL 0
78#define DFL_CONN_TIMEOUT 0
79#define DFL_MAX_CONNECTIONS 0
80#define DFL_SESSION_REUSE 1
81#define DFL_SESSION_LIFETIME 86400
82#define DFL_FORCE_CIPHER 0
83
Rich Evans18b78c72015-02-11 14:06:19 +000084#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
85 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \
86 !defined(POLARSSL_SSL_CLI_C) || !defined(POLARSSL_NET_C) || \
87 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C) || \
88 !defined(POLARSSL_X509_CRT_PARSE_C)
89int main( int argc, char *argv[] )
90{
91 ((void) argc);
92 ((void) argv);
93
94 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
95 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
96 "POLARSSL_SSL_CLI_C and/or POLARSSL_NET_C and/or "
97 "POLARSSL_RSA_C and/or POLARSSL_CTR_DRBG_C and/or "
98 "POLARSSL_X509_CRT_PARSE_C not defined.\n");
99 return( 0 );
100}
101#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000102int server_fd = -1;
103
104/*
105 * global options
106 */
107struct options
108{
109 int opmode; /* operation mode (client or server) */
110 int iomode; /* I/O mode (blocking or non-blocking) */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200111 const char *server_name; /* hostname of the server (client only) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000112 int server_port; /* port on which the ssl service runs */
113 int command; /* what to do: read or write operation */
114 int buffer_size; /* size of the send/receive buffer */
115 int max_bytes; /* max. # of bytes before a reconnect */
116 int debug_level; /* level of debugging */
Paul Bakker44618dd2013-07-04 10:34:10 +0200117#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 int conn_timeout; /* max. delay before a reconnect */
Paul Bakker44618dd2013-07-04 10:34:10 +0200119#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 int max_connections; /* max. number of reconnections */
121 int session_reuse; /* flag to reuse the keying material */
122 int session_lifetime; /* if reached, session data is expired */
Paul Bakkere3166ce2011-01-27 17:40:50 +0000123 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
Paul Bakker5121ce52009-01-03 21:22:43 +0000124};
125
126/*
127 * Although this PRNG has good statistical properties (eg. passes
128 * DIEHARD), it is not cryptographically secure.
129 */
Paul Bakker3c5ef712013-06-25 16:37:45 +0200130static unsigned long int lcppm5( unsigned long int *state )
Paul Bakker5121ce52009-01-03 21:22:43 +0000131{
132 unsigned long int u, v;
133
134 u = v = state[4] ^ 1;
135 state[u & 3] ^= u;
136 u ^= (v << 12) ^ (v >> 12);
137 u ^= v * state[0]; v >>= 8;
138 u ^= v * state[1]; v >>= 8;
139 u ^= v * state[2]; v >>= 8;
140 u ^= v * state[3];
141 u &= 0xFFFFFFFF;
142 state[4] = u;
143
144 return( u );
145}
146
Paul Bakker3c5ef712013-06-25 16:37:45 +0200147static void my_debug( void *ctx, int level, const char *str )
Paul Bakker5121ce52009-01-03 21:22:43 +0000148{
149 if( level < ((struct options *) ctx)->debug_level )
Rich Evansf90016a2015-01-19 14:26:37 +0000150 polarssl_fprintf( stderr, "%s", str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151}
152
153/*
154 * perform a single SSL connection
155 */
156static int ssl_test( struct options *opt )
157{
Alfred Klomp5b78f212014-07-14 22:10:14 +0200158 int ret = 1, i;
Manuel Pégourié-Gonnard68821da2013-09-16 12:34:33 +0200159 int client_fd = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 int bytes_to_read;
161 int bytes_to_write;
Paul Bakker026c03b2009-03-28 17:53:03 +0000162 int offset_to_read = 0;
163 int offset_to_write = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000164
165 long int nb_read;
166 long int nb_written;
167
168 unsigned long read_state[5];
169 unsigned long write_state[5];
170
Paul Bakker026c03b2009-03-28 17:53:03 +0000171 unsigned char *read_buf = NULL;
172 unsigned char *write_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000173
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200174 const char *pers = "ssl_test";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000175
Paul Bakker44618dd2013-07-04 10:34:10 +0200176#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 struct hr_time t;
Paul Bakker44618dd2013-07-04 10:34:10 +0200178#endif
Paul Bakker508ad5a2011-12-04 17:09:26 +0000179 entropy_context entropy;
180 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 ssl_context ssl;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200182 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200183 pk_context pkey;
Paul Bakker5121ce52009-01-03 21:22:43 +0000184
Paul Bakker0c226102014-04-17 16:02:36 +0200185 memset( &ssl, 0, sizeof(ssl_context) );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000186 entropy_init( &entropy );
Paul Bakker0c226102014-04-17 16:02:36 +0200187 x509_crt_init( &srvcert );
188 pk_init( &pkey );
189
Paul Bakker508ad5a2011-12-04 17:09:26 +0000190 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200191 (const unsigned char *) pers,
192 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000193 {
Rich Evansf90016a2015-01-19 14:26:37 +0000194 polarssl_printf( " ! ctr_drbg_init returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000195 goto exit;
196 }
197
Paul Bakker44618dd2013-07-04 10:34:10 +0200198#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 get_timer( &t, 1 );
Paul Bakker44618dd2013-07-04 10:34:10 +0200200#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000201
202 memset( read_state, 0, sizeof( read_state ) );
203 memset( write_state, 0, sizeof( write_state ) );
204
Paul Bakker5121ce52009-01-03 21:22:43 +0000205
206 if( opt->opmode == OPMODE_CLIENT )
207 {
208 if( ( ret = net_connect( &client_fd, opt->server_name,
209 opt->server_port ) ) != 0 )
210 {
Rich Evansf90016a2015-01-19 14:26:37 +0000211 polarssl_printf( " ! net_connect returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000212 return( ret );
213 }
214
215 if( ( ret = ssl_init( &ssl ) ) != 0 )
216 {
Rich Evansf90016a2015-01-19 14:26:37 +0000217 polarssl_printf( " ! ssl_init returned %d\n\n", ret );
Paul Bakker0c226102014-04-17 16:02:36 +0200218 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 }
220
221 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
222 }
223
224 if( opt->opmode == OPMODE_SERVER )
225 {
Paul Bakker5690efc2011-05-26 13:16:06 +0000226#if !defined(POLARSSL_CERTS_C)
Rich Evansf90016a2015-01-19 14:26:37 +0000227 polarssl_printf("POLARSSL_CERTS_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +0000228 goto exit;
229#else
Paul Bakkerddf26b42013-09-18 13:46:23 +0200230 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_srv_crt,
231 strlen( test_srv_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 if( ret != 0 )
233 {
Rich Evansf90016a2015-01-19 14:26:37 +0000234 polarssl_printf( " ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 goto exit;
236 }
237
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200238 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_ca_list,
239 strlen( test_ca_list ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000240 if( ret != 0 )
241 {
Rich Evansf90016a2015-01-19 14:26:37 +0000242 polarssl_printf( " ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 goto exit;
244 }
245
Paul Bakker1a7550a2013-09-15 13:01:22 +0200246 ret = pk_parse_key( &pkey, (const unsigned char *) test_srv_key,
247 strlen( test_srv_key ), NULL, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 if( ret != 0 )
249 {
Rich Evansf90016a2015-01-19 14:26:37 +0000250 polarssl_printf( " ! pk_parse_key returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000251 goto exit;
252 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000253#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000254
255 if( server_fd < 0 )
256 {
257 if( ( ret = net_bind( &server_fd, NULL,
258 opt->server_port ) ) != 0 )
259 {
Rich Evansf90016a2015-01-19 14:26:37 +0000260 polarssl_printf( " ! net_bind returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000261 return( ret );
262 }
263 }
264
265 if( ( ret = net_accept( server_fd, &client_fd, NULL ) ) != 0 )
266 {
Rich Evansf90016a2015-01-19 14:26:37 +0000267 polarssl_printf( " ! net_accept returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000268 return( ret );
269 }
270
271 if( ( ret = ssl_init( &ssl ) ) != 0 )
272 {
Rich Evansf90016a2015-01-19 14:26:37 +0000273 polarssl_printf( " ! ssl_init returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000274 return( ret );
275 }
276
277 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000278 ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200279 if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 )
280 {
Rich Evansf90016a2015-01-19 14:26:37 +0000281 polarssl_printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200282 goto exit;
283 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000284 }
285
286 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
287
Paul Bakker508ad5a2011-12-04 17:09:26 +0000288 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker5121ce52009-01-03 21:22:43 +0000289 ssl_set_dbg( &ssl, my_debug, opt );
290 ssl_set_bio( &ssl, net_recv, &client_fd,
291 net_send, &client_fd );
292
Paul Bakker68884e32013-01-07 18:20:04 +0100293 if( opt->force_ciphersuite[0] != DFL_FORCE_CIPHER )
294 ssl_set_ciphersuites( &ssl, opt->force_ciphersuite );
Paul Bakker5121ce52009-01-03 21:22:43 +0000295
296 if( opt->iomode == IOMODE_NONBLOCK )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200297 {
298 if( ( ret = net_set_nonblock( client_fd ) ) != 0 )
299 {
Rich Evansf90016a2015-01-19 14:26:37 +0000300 polarssl_printf( " ! net_set_nonblock returned %d\n\n", ret );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200301 return( ret );
302 }
303 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000304
Rich Evansf90016a2015-01-19 14:26:37 +0000305 read_buf = (unsigned char *) polarssl_malloc( opt->buffer_size );
306 write_buf = (unsigned char *) polarssl_malloc( opt->buffer_size );
Paul Bakker5121ce52009-01-03 21:22:43 +0000307
308 if( read_buf == NULL || write_buf == NULL )
309 {
Rich Evansf90016a2015-01-19 14:26:37 +0000310 polarssl_printf( " ! polarssl_malloc(%d bytes) failed\n\n", opt->buffer_size );
Paul Bakker5121ce52009-01-03 21:22:43 +0000311 goto exit;
312 }
313
314 nb_read = bytes_to_read = 0;
315 nb_written = bytes_to_write = 0;
316
317 while( 1 )
318 {
319 if( opt->command & COMMAND_WRITE )
320 {
321 if( bytes_to_write == 0 )
322 {
323 while( bytes_to_write == 0 )
324 bytes_to_write = rand() % opt->buffer_size;
325
326 for( i = 0; i < bytes_to_write; i++ )
327 write_buf[i] = (unsigned char) lcppm5( write_state );
328
329 offset_to_write = 0;
330 }
331
332 ret = ssl_write( &ssl, write_buf + offset_to_write,
333 bytes_to_write );
334
335 if( ret >= 0 )
336 {
337 nb_written += ret;
338 bytes_to_write -= ret;
339 offset_to_write += ret;
340 }
341
Paul Bakker40e46942009-01-03 21:51:57 +0000342 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY ||
343 ret == POLARSSL_ERR_NET_CONN_RESET )
Paul Bakker5121ce52009-01-03 21:22:43 +0000344 {
345 ret = 0;
346 goto exit;
347 }
348
Paul Bakker831a7552011-05-18 13:32:51 +0000349 if( ret < 0 && ret != POLARSSL_ERR_NET_WANT_READ &&
350 ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000351 {
Rich Evansf90016a2015-01-19 14:26:37 +0000352 polarssl_printf( " ! ssl_write returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000353 break;
354 }
355 }
356
357 if( opt->command & COMMAND_READ )
358 {
Paul Bakker396333e2013-09-26 13:32:19 +0200359 while( bytes_to_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000360 {
361 bytes_to_read = rand() % opt->buffer_size;
362 offset_to_read = 0;
363 }
364
365 ret = ssl_read( &ssl, read_buf + offset_to_read,
366 bytes_to_read );
367
Paul Bakker396333e2013-09-26 13:32:19 +0200368 if( ret > 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000369 {
370 for( i = 0; i < ret; i++ )
371 {
372 if( read_buf[offset_to_read + i] !=
373 (unsigned char) lcppm5( read_state ) )
374 {
375 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000376 polarssl_printf( " ! plaintext mismatch\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000377 goto exit;
378 }
379 }
380
381 nb_read += ret;
382 bytes_to_read -= ret;
383 offset_to_read += ret;
384 }
385
Paul Bakker396333e2013-09-26 13:32:19 +0200386 if( ret == 0 ||
387 ret == POLARSSL_ERR_SSL_CONN_EOF ||
388 ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY ||
Paul Bakker40e46942009-01-03 21:51:57 +0000389 ret == POLARSSL_ERR_NET_CONN_RESET )
Paul Bakker5121ce52009-01-03 21:22:43 +0000390 {
391 ret = 0;
392 goto exit;
393 }
394
Paul Bakker831a7552011-05-18 13:32:51 +0000395 if( ret < 0 && ret != POLARSSL_ERR_NET_WANT_READ &&
396 ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000397 {
Rich Evansf90016a2015-01-19 14:26:37 +0000398 polarssl_printf( " ! ssl_read returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000399 break;
400 }
401 }
402
403 ret = 0;
404
405 if( opt->max_bytes != 0 &&
406 ( opt->max_bytes <= nb_read ||
407 opt->max_bytes <= nb_written ) )
408 break;
409
Paul Bakker44618dd2013-07-04 10:34:10 +0200410#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 if( opt->conn_timeout != 0 &&
412 opt->conn_timeout <= (int) get_timer( &t, 0 ) )
413 break;
Paul Bakker44618dd2013-07-04 10:34:10 +0200414#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000415 }
416
417exit:
418
419 fflush( stdout );
420
421 if( read_buf != NULL )
422 free( read_buf );
423
424 if( write_buf != NULL )
425 free( write_buf );
426
427 ssl_close_notify( &ssl );
Paul Bakker36713e82013-09-17 13:25:29 +0200428 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200429 pk_free( &pkey );
Paul Bakker5121ce52009-01-03 21:22:43 +0000430 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +0200431 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200432 entropy_free( &entropy );
Paul Bakker0c226102014-04-17 16:02:36 +0200433
434 if( client_fd != -1 )
435 net_close( client_fd );
Paul Bakker5121ce52009-01-03 21:22:43 +0000436
437 return( ret );
438}
439
Paul Bakker44618dd2013-07-04 10:34:10 +0200440#if defined(POLARSSL_TIMING_C)
441#define USAGE_TIMING \
442 " conn_timeout=%%d (ms) default: 0 (no timeout)\n"
443#else
444#define USAGE_TIMING ""
445#endif
446
Paul Bakker5121ce52009-01-03 21:22:43 +0000447#define USAGE \
448 "\n usage: ssl_test opmode=<> command=<>...\n" \
449 "\n acceptable parameters:\n" \
450 " opmode=client/server default: <none>\n" \
451 " iomode=block/nonblock default: block\n" \
452 " server_name=%%s default: localhost\n" \
453 " server_port=%%d default: 4433\n" \
454 " command=read/write/both default: read\n" \
455 " buffer_size=%%d (bytes) default: 1024\n" \
456 " max_bytes=%%d (bytes) default: 0 (no limit)\n" \
457 " debug_level=%%d default: 0 (disabled)\n" \
Paul Bakker44618dd2013-07-04 10:34:10 +0200458 USAGE_TIMING \
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 " max_connections=%%d default: 0 (no limit)\n" \
460 " session_reuse=on/off default: on (enabled)\n" \
461 " session_lifetime=%%d (s) default: 86400\n" \
Paul Bakkere3166ce2011-01-27 17:40:50 +0000462 " force_ciphersuite=<name> default: all enabled\n" \
463 " acceptable ciphersuite names:\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
465int main( int argc, char *argv[] )
466{
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100467 int i;
Paul Bakkere3166ce2011-01-27 17:40:50 +0000468 const int *list;
Paul Bakker5121ce52009-01-03 21:22:43 +0000469 int ret = 1;
470 int nb_conn;
471 char *p, *q;
472 struct options opt;
473
474 if( argc == 1 )
475 {
476 usage:
Rich Evansf90016a2015-01-19 14:26:37 +0000477 polarssl_printf( USAGE );
Paul Bakker44618dd2013-07-04 10:34:10 +0200478
Paul Bakkere3166ce2011-01-27 17:40:50 +0000479 list = ssl_list_ciphersuites();
480 while( *list )
481 {
Rich Evansf90016a2015-01-19 14:26:37 +0000482 polarssl_printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakkere3166ce2011-01-27 17:40:50 +0000483 list++;
484 }
Rich Evansf90016a2015-01-19 14:26:37 +0000485 polarssl_printf("\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000486 goto exit;
487 }
488
489 opt.opmode = DFL_OPMODE;
490 opt.iomode = DFL_IOMODE;
491 opt.server_name = DFL_SERVER_NAME;
492 opt.server_port = DFL_SERVER_PORT;
493 opt.command = DFL_COMMAND;
494 opt.buffer_size = DFL_BUFFER_SIZE;
495 opt.max_bytes = DFL_MAX_BYTES;
496 opt.debug_level = DFL_DEBUG_LEVEL;
Paul Bakker44618dd2013-07-04 10:34:10 +0200497#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000498 opt.conn_timeout = DFL_CONN_TIMEOUT;
Paul Bakker44618dd2013-07-04 10:34:10 +0200499#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000500 opt.max_connections = DFL_MAX_CONNECTIONS;
501 opt.session_reuse = DFL_SESSION_REUSE;
502 opt.session_lifetime = DFL_SESSION_LIFETIME;
Paul Bakkere3166ce2011-01-27 17:40:50 +0000503 opt.force_ciphersuite[0] = DFL_FORCE_CIPHER;
Paul Bakker5121ce52009-01-03 21:22:43 +0000504
505 for( i = 1; i < argc; i++ )
506 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000507 p = argv[i];
508 if( ( q = strchr( p, '=' ) ) == NULL )
509 continue;
510 *q++ = '\0';
511
512 if( strcmp( p, "opmode" ) == 0 )
513 {
514 if( strcmp( q, "client" ) == 0 )
515 opt.opmode = OPMODE_CLIENT;
516 else
517 if( strcmp( q, "server" ) == 0 )
518 opt.opmode = OPMODE_SERVER;
519 else goto usage;
520 }
521
522 if( strcmp( p, "iomode" ) == 0 )
523 {
524 if( strcmp( q, "block" ) == 0 )
525 opt.iomode = IOMODE_BLOCK;
526 else
527 if( strcmp( q, "nonblock" ) == 0 )
528 opt.iomode = IOMODE_NONBLOCK;
529 else goto usage;
530 }
531
532 if( strcmp( p, "server_name" ) == 0 )
533 opt.server_name = q;
534
535 if( strcmp( p, "server_port" ) == 0 )
536 {
537 opt.server_port = atoi( q );
538 if( opt.server_port < 1 || opt.server_port > 65535 )
539 goto usage;
540 }
541
542 if( strcmp( p, "command" ) == 0 )
543 {
544 if( strcmp( q, "read" ) == 0 )
545 opt.command = COMMAND_READ;
546 else
547 if( strcmp( q, "write" ) == 0 )
548 opt.command = COMMAND_WRITE;
549 else
550 if( strcmp( q, "both" ) == 0 )
551 {
552 opt.iomode = IOMODE_NONBLOCK;
553 opt.command = COMMAND_BOTH;
554 }
555 else goto usage;
556 }
557
558 if( strcmp( p, "buffer_size" ) == 0 )
559 {
560 opt.buffer_size = atoi( q );
561 if( opt.buffer_size < 1 || opt.buffer_size > 1048576 )
562 goto usage;
563 }
564
565 if( strcmp( p, "max_bytes" ) == 0 )
566 opt.max_bytes = atoi( q );
567
568 if( strcmp( p, "debug_level" ) == 0 )
569 opt.debug_level = atoi( q );
Paul Bakker44618dd2013-07-04 10:34:10 +0200570#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000571 if( strcmp( p, "conn_timeout" ) == 0 )
572 opt.conn_timeout = atoi( q );
Paul Bakker44618dd2013-07-04 10:34:10 +0200573#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 if( strcmp( p, "max_connections" ) == 0 )
575 opt.max_connections = atoi( q );
576
577 if( strcmp( p, "session_reuse" ) == 0 )
578 {
579 if( strcmp( q, "on" ) == 0 )
580 opt.session_reuse = 1;
581 else
582 if( strcmp( q, "off" ) == 0 )
583 opt.session_reuse = 0;
584 else
585 goto usage;
586 }
587
588 if( strcmp( p, "session_lifetime" ) == 0 )
589 opt.session_lifetime = atoi( q );
590
Paul Bakkere3166ce2011-01-27 17:40:50 +0000591 if( strcmp( p, "force_ciphersuite" ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000592 {
Paul Bakkere3166ce2011-01-27 17:40:50 +0000593 opt.force_ciphersuite[0] = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
Paul Bakkere3166ce2011-01-27 17:40:50 +0000595 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
Paul Bakker5121ce52009-01-03 21:22:43 +0000596
Paul Bakkere3166ce2011-01-27 17:40:50 +0000597 if( opt.force_ciphersuite[0] <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000598 goto usage;
599
Paul Bakkere3166ce2011-01-27 17:40:50 +0000600 opt.force_ciphersuite[1] = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000601 }
602 }
603
604 switch( opt.opmode )
605 {
606 case OPMODE_CLIENT:
607 break;
608
609 case OPMODE_SERVER:
610 break;
611
612 default:
613 goto usage;
614 }
615
616 nb_conn = 0;
617
618 do {
619 nb_conn++;
620 ret = ssl_test( &opt );
621 if( opt.max_connections != 0 &&
622 opt.max_connections <= nb_conn )
623 break;
624 }
625 while( ret == 0 );
626
627exit:
628
Paul Bakkercce9d772011-11-18 14:26:47 +0000629#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000630 polarssl_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000631 fflush( stdout ); getchar();
632#endif
633
634 return( ret );
635}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000636#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000637 POLARSSL_SSL_SRV_C && POLARSSL_SSL_CLI_C && POLARSSL_NET_C &&
Paul Bakker508ad5a2011-12-04 17:09:26 +0000638 POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */