blob: 1aa7ee17e6e70fa8c7b1acb5e9663568611c5a33 [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
Manuel Pégourié-Gonnard013bffe2015-02-13 14:09:44 +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) && \
Rich Evans18b78c72015-02-11 14:06:19 +000043 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)
Rich Evans85b05ec2015-02-12 11:37:29 +000089int main( void )
Rich Evans18b78c72015-02-11 14:06:19 +000090{
Rich Evans18b78c72015-02-11 14:06:19 +000091 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
92 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
93 "POLARSSL_SSL_CLI_C and/or POLARSSL_NET_C and/or "
94 "POLARSSL_RSA_C and/or POLARSSL_CTR_DRBG_C and/or "
95 "POLARSSL_X509_CRT_PARSE_C not defined.\n");
96 return( 0 );
97}
98#else
Paul Bakker5121ce52009-01-03 21:22:43 +000099int server_fd = -1;
100
101/*
102 * global options
103 */
104struct options
105{
106 int opmode; /* operation mode (client or server) */
107 int iomode; /* I/O mode (blocking or non-blocking) */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200108 const char *server_name; /* hostname of the server (client only) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 int server_port; /* port on which the ssl service runs */
110 int command; /* what to do: read or write operation */
111 int buffer_size; /* size of the send/receive buffer */
112 int max_bytes; /* max. # of bytes before a reconnect */
113 int debug_level; /* level of debugging */
Paul Bakker44618dd2013-07-04 10:34:10 +0200114#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 int conn_timeout; /* max. delay before a reconnect */
Paul Bakker44618dd2013-07-04 10:34:10 +0200116#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 int max_connections; /* max. number of reconnections */
118 int session_reuse; /* flag to reuse the keying material */
119 int session_lifetime; /* if reached, session data is expired */
Paul Bakkere3166ce2011-01-27 17:40:50 +0000120 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
Paul Bakker5121ce52009-01-03 21:22:43 +0000121};
122
123/*
124 * Although this PRNG has good statistical properties (eg. passes
125 * DIEHARD), it is not cryptographically secure.
126 */
Paul Bakker3c5ef712013-06-25 16:37:45 +0200127static unsigned long int lcppm5( unsigned long int *state )
Paul Bakker5121ce52009-01-03 21:22:43 +0000128{
129 unsigned long int u, v;
130
131 u = v = state[4] ^ 1;
132 state[u & 3] ^= u;
133 u ^= (v << 12) ^ (v >> 12);
134 u ^= v * state[0]; v >>= 8;
135 u ^= v * state[1]; v >>= 8;
136 u ^= v * state[2]; v >>= 8;
137 u ^= v * state[3];
138 u &= 0xFFFFFFFF;
139 state[4] = u;
140
141 return( u );
142}
143
Paul Bakker3c5ef712013-06-25 16:37:45 +0200144static void my_debug( void *ctx, int level, const char *str )
Paul Bakker5121ce52009-01-03 21:22:43 +0000145{
146 if( level < ((struct options *) ctx)->debug_level )
Rich Evansf90016a2015-01-19 14:26:37 +0000147 polarssl_fprintf( stderr, "%s", str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000148}
149
150/*
151 * perform a single SSL connection
152 */
153static int ssl_test( struct options *opt )
154{
Alfred Klomp5b78f212014-07-14 22:10:14 +0200155 int ret = 1, i;
Manuel Pégourié-Gonnard68821da2013-09-16 12:34:33 +0200156 int client_fd = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 int bytes_to_read;
158 int bytes_to_write;
Paul Bakker026c03b2009-03-28 17:53:03 +0000159 int offset_to_read = 0;
160 int offset_to_write = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000161
162 long int nb_read;
163 long int nb_written;
164
165 unsigned long read_state[5];
166 unsigned long write_state[5];
167
Paul Bakker026c03b2009-03-28 17:53:03 +0000168 unsigned char *read_buf = NULL;
169 unsigned char *write_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000170
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200171 const char *pers = "ssl_test";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000172
Paul Bakker44618dd2013-07-04 10:34:10 +0200173#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000174 struct hr_time t;
Paul Bakker44618dd2013-07-04 10:34:10 +0200175#endif
Paul Bakker508ad5a2011-12-04 17:09:26 +0000176 entropy_context entropy;
177 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 ssl_context ssl;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200179 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200180 pk_context pkey;
Paul Bakker5121ce52009-01-03 21:22:43 +0000181
Paul Bakker0c226102014-04-17 16:02:36 +0200182 memset( &ssl, 0, sizeof(ssl_context) );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000183 entropy_init( &entropy );
Paul Bakker0c226102014-04-17 16:02:36 +0200184 x509_crt_init( &srvcert );
185 pk_init( &pkey );
186
Paul Bakker508ad5a2011-12-04 17:09:26 +0000187 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200188 (const unsigned char *) pers,
189 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000190 {
Rich Evansf90016a2015-01-19 14:26:37 +0000191 polarssl_printf( " ! ctr_drbg_init returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000192 goto exit;
193 }
194
Paul Bakker44618dd2013-07-04 10:34:10 +0200195#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000196 get_timer( &t, 1 );
Paul Bakker44618dd2013-07-04 10:34:10 +0200197#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000198
199 memset( read_state, 0, sizeof( read_state ) );
200 memset( write_state, 0, sizeof( write_state ) );
201
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
203 if( opt->opmode == OPMODE_CLIENT )
204 {
205 if( ( ret = net_connect( &client_fd, opt->server_name,
206 opt->server_port ) ) != 0 )
207 {
Rich Evansf90016a2015-01-19 14:26:37 +0000208 polarssl_printf( " ! net_connect returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 return( ret );
210 }
211
212 if( ( ret = ssl_init( &ssl ) ) != 0 )
213 {
Rich Evansf90016a2015-01-19 14:26:37 +0000214 polarssl_printf( " ! ssl_init returned %d\n\n", ret );
Paul Bakker0c226102014-04-17 16:02:36 +0200215 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +0000216 }
217
218 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
219 }
220
221 if( opt->opmode == OPMODE_SERVER )
222 {
Paul Bakker5690efc2011-05-26 13:16:06 +0000223#if !defined(POLARSSL_CERTS_C)
Rich Evansf90016a2015-01-19 14:26:37 +0000224 polarssl_printf("POLARSSL_CERTS_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +0000225 goto exit;
226#else
Paul Bakkerddf26b42013-09-18 13:46:23 +0200227 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_srv_crt,
228 strlen( test_srv_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000229 if( ret != 0 )
230 {
Rich Evansf90016a2015-01-19 14:26:37 +0000231 polarssl_printf( " ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 goto exit;
233 }
234
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200235 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_ca_list,
236 strlen( test_ca_list ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000237 if( ret != 0 )
238 {
Rich Evansf90016a2015-01-19 14:26:37 +0000239 polarssl_printf( " ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000240 goto exit;
241 }
242
Paul Bakker1a7550a2013-09-15 13:01:22 +0200243 ret = pk_parse_key( &pkey, (const unsigned char *) test_srv_key,
244 strlen( test_srv_key ), NULL, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000245 if( ret != 0 )
246 {
Rich Evansf90016a2015-01-19 14:26:37 +0000247 polarssl_printf( " ! pk_parse_key returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 goto exit;
249 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000250#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000251
252 if( server_fd < 0 )
253 {
254 if( ( ret = net_bind( &server_fd, NULL,
255 opt->server_port ) ) != 0 )
256 {
Rich Evansf90016a2015-01-19 14:26:37 +0000257 polarssl_printf( " ! net_bind returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000258 return( ret );
259 }
260 }
261
262 if( ( ret = net_accept( server_fd, &client_fd, NULL ) ) != 0 )
263 {
Rich Evansf90016a2015-01-19 14:26:37 +0000264 polarssl_printf( " ! net_accept returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000265 return( ret );
266 }
267
268 if( ( ret = ssl_init( &ssl ) ) != 0 )
269 {
Rich Evansf90016a2015-01-19 14:26:37 +0000270 polarssl_printf( " ! ssl_init returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000271 return( ret );
272 }
273
274 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker40ea7de2009-05-03 10:18:48 +0000275 ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200276 if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 )
277 {
Rich Evansf90016a2015-01-19 14:26:37 +0000278 polarssl_printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200279 goto exit;
280 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000281 }
282
283 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
284
Paul Bakker508ad5a2011-12-04 17:09:26 +0000285 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker5121ce52009-01-03 21:22:43 +0000286 ssl_set_dbg( &ssl, my_debug, opt );
287 ssl_set_bio( &ssl, net_recv, &client_fd,
288 net_send, &client_fd );
289
Paul Bakker68884e32013-01-07 18:20:04 +0100290 if( opt->force_ciphersuite[0] != DFL_FORCE_CIPHER )
291 ssl_set_ciphersuites( &ssl, opt->force_ciphersuite );
Paul Bakker5121ce52009-01-03 21:22:43 +0000292
293 if( opt->iomode == IOMODE_NONBLOCK )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200294 {
295 if( ( ret = net_set_nonblock( client_fd ) ) != 0 )
296 {
Rich Evansf90016a2015-01-19 14:26:37 +0000297 polarssl_printf( " ! net_set_nonblock returned %d\n\n", ret );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200298 return( ret );
299 }
300 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000301
Rich Evansf90016a2015-01-19 14:26:37 +0000302 read_buf = (unsigned char *) polarssl_malloc( opt->buffer_size );
303 write_buf = (unsigned char *) polarssl_malloc( opt->buffer_size );
Paul Bakker5121ce52009-01-03 21:22:43 +0000304
305 if( read_buf == NULL || write_buf == NULL )
306 {
Rich Evansf90016a2015-01-19 14:26:37 +0000307 polarssl_printf( " ! polarssl_malloc(%d bytes) failed\n\n", opt->buffer_size );
Paul Bakker5121ce52009-01-03 21:22:43 +0000308 goto exit;
309 }
310
311 nb_read = bytes_to_read = 0;
312 nb_written = bytes_to_write = 0;
313
314 while( 1 )
315 {
316 if( opt->command & COMMAND_WRITE )
317 {
318 if( bytes_to_write == 0 )
319 {
320 while( bytes_to_write == 0 )
321 bytes_to_write = rand() % opt->buffer_size;
322
323 for( i = 0; i < bytes_to_write; i++ )
324 write_buf[i] = (unsigned char) lcppm5( write_state );
325
326 offset_to_write = 0;
327 }
328
329 ret = ssl_write( &ssl, write_buf + offset_to_write,
330 bytes_to_write );
331
332 if( ret >= 0 )
333 {
334 nb_written += ret;
335 bytes_to_write -= ret;
336 offset_to_write += ret;
337 }
338
Paul Bakker40e46942009-01-03 21:51:57 +0000339 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY ||
340 ret == POLARSSL_ERR_NET_CONN_RESET )
Paul Bakker5121ce52009-01-03 21:22:43 +0000341 {
342 ret = 0;
343 goto exit;
344 }
345
Paul Bakker831a7552011-05-18 13:32:51 +0000346 if( ret < 0 && ret != POLARSSL_ERR_NET_WANT_READ &&
347 ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000348 {
Rich Evansf90016a2015-01-19 14:26:37 +0000349 polarssl_printf( " ! ssl_write returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000350 break;
351 }
352 }
353
354 if( opt->command & COMMAND_READ )
355 {
Paul Bakker396333e2013-09-26 13:32:19 +0200356 while( bytes_to_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000357 {
358 bytes_to_read = rand() % opt->buffer_size;
359 offset_to_read = 0;
360 }
361
362 ret = ssl_read( &ssl, read_buf + offset_to_read,
363 bytes_to_read );
364
Paul Bakker396333e2013-09-26 13:32:19 +0200365 if( ret > 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000366 {
367 for( i = 0; i < ret; i++ )
368 {
369 if( read_buf[offset_to_read + i] !=
370 (unsigned char) lcppm5( read_state ) )
371 {
372 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000373 polarssl_printf( " ! plaintext mismatch\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000374 goto exit;
375 }
376 }
377
378 nb_read += ret;
379 bytes_to_read -= ret;
380 offset_to_read += ret;
381 }
382
Paul Bakker396333e2013-09-26 13:32:19 +0200383 if( ret == 0 ||
384 ret == POLARSSL_ERR_SSL_CONN_EOF ||
385 ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY ||
Paul Bakker40e46942009-01-03 21:51:57 +0000386 ret == POLARSSL_ERR_NET_CONN_RESET )
Paul Bakker5121ce52009-01-03 21:22:43 +0000387 {
388 ret = 0;
389 goto exit;
390 }
391
Paul Bakker831a7552011-05-18 13:32:51 +0000392 if( ret < 0 && ret != POLARSSL_ERR_NET_WANT_READ &&
393 ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000394 {
Rich Evansf90016a2015-01-19 14:26:37 +0000395 polarssl_printf( " ! ssl_read returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000396 break;
397 }
398 }
399
400 ret = 0;
401
402 if( opt->max_bytes != 0 &&
403 ( opt->max_bytes <= nb_read ||
404 opt->max_bytes <= nb_written ) )
405 break;
406
Paul Bakker44618dd2013-07-04 10:34:10 +0200407#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 if( opt->conn_timeout != 0 &&
409 opt->conn_timeout <= (int) get_timer( &t, 0 ) )
410 break;
Paul Bakker44618dd2013-07-04 10:34:10 +0200411#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000412 }
413
414exit:
415
416 fflush( stdout );
417
418 if( read_buf != NULL )
419 free( read_buf );
420
421 if( write_buf != NULL )
422 free( write_buf );
423
424 ssl_close_notify( &ssl );
Paul Bakker36713e82013-09-17 13:25:29 +0200425 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200426 pk_free( &pkey );
Paul Bakker5121ce52009-01-03 21:22:43 +0000427 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +0200428 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200429 entropy_free( &entropy );
Paul Bakker0c226102014-04-17 16:02:36 +0200430
431 if( client_fd != -1 )
432 net_close( client_fd );
Paul Bakker5121ce52009-01-03 21:22:43 +0000433
434 return( ret );
435}
436
Paul Bakker44618dd2013-07-04 10:34:10 +0200437#if defined(POLARSSL_TIMING_C)
438#define USAGE_TIMING \
439 " conn_timeout=%%d (ms) default: 0 (no timeout)\n"
440#else
441#define USAGE_TIMING ""
442#endif
443
Paul Bakker5121ce52009-01-03 21:22:43 +0000444#define USAGE \
445 "\n usage: ssl_test opmode=<> command=<>...\n" \
446 "\n acceptable parameters:\n" \
447 " opmode=client/server default: <none>\n" \
448 " iomode=block/nonblock default: block\n" \
449 " server_name=%%s default: localhost\n" \
450 " server_port=%%d default: 4433\n" \
451 " command=read/write/both default: read\n" \
452 " buffer_size=%%d (bytes) default: 1024\n" \
453 " max_bytes=%%d (bytes) default: 0 (no limit)\n" \
454 " debug_level=%%d default: 0 (disabled)\n" \
Paul Bakker44618dd2013-07-04 10:34:10 +0200455 USAGE_TIMING \
Paul Bakker5121ce52009-01-03 21:22:43 +0000456 " max_connections=%%d default: 0 (no limit)\n" \
457 " session_reuse=on/off default: on (enabled)\n" \
458 " session_lifetime=%%d (s) default: 86400\n" \
Paul Bakkere3166ce2011-01-27 17:40:50 +0000459 " force_ciphersuite=<name> default: all enabled\n" \
460 " acceptable ciphersuite names:\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000461
462int main( int argc, char *argv[] )
463{
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100464 int i;
Paul Bakkere3166ce2011-01-27 17:40:50 +0000465 const int *list;
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 int ret = 1;
467 int nb_conn;
468 char *p, *q;
469 struct options opt;
470
471 if( argc == 1 )
472 {
473 usage:
Rich Evansf90016a2015-01-19 14:26:37 +0000474 polarssl_printf( USAGE );
Paul Bakker44618dd2013-07-04 10:34:10 +0200475
Paul Bakkere3166ce2011-01-27 17:40:50 +0000476 list = ssl_list_ciphersuites();
477 while( *list )
478 {
Rich Evansf90016a2015-01-19 14:26:37 +0000479 polarssl_printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakkere3166ce2011-01-27 17:40:50 +0000480 list++;
481 }
Rich Evansf90016a2015-01-19 14:26:37 +0000482 polarssl_printf("\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000483 goto exit;
484 }
485
486 opt.opmode = DFL_OPMODE;
487 opt.iomode = DFL_IOMODE;
488 opt.server_name = DFL_SERVER_NAME;
489 opt.server_port = DFL_SERVER_PORT;
490 opt.command = DFL_COMMAND;
491 opt.buffer_size = DFL_BUFFER_SIZE;
492 opt.max_bytes = DFL_MAX_BYTES;
493 opt.debug_level = DFL_DEBUG_LEVEL;
Paul Bakker44618dd2013-07-04 10:34:10 +0200494#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000495 opt.conn_timeout = DFL_CONN_TIMEOUT;
Paul Bakker44618dd2013-07-04 10:34:10 +0200496#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 opt.max_connections = DFL_MAX_CONNECTIONS;
498 opt.session_reuse = DFL_SESSION_REUSE;
499 opt.session_lifetime = DFL_SESSION_LIFETIME;
Paul Bakkere3166ce2011-01-27 17:40:50 +0000500 opt.force_ciphersuite[0] = DFL_FORCE_CIPHER;
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
502 for( i = 1; i < argc; i++ )
503 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000504 p = argv[i];
505 if( ( q = strchr( p, '=' ) ) == NULL )
506 continue;
507 *q++ = '\0';
508
509 if( strcmp( p, "opmode" ) == 0 )
510 {
511 if( strcmp( q, "client" ) == 0 )
512 opt.opmode = OPMODE_CLIENT;
513 else
514 if( strcmp( q, "server" ) == 0 )
515 opt.opmode = OPMODE_SERVER;
516 else goto usage;
517 }
518
519 if( strcmp( p, "iomode" ) == 0 )
520 {
521 if( strcmp( q, "block" ) == 0 )
522 opt.iomode = IOMODE_BLOCK;
523 else
524 if( strcmp( q, "nonblock" ) == 0 )
525 opt.iomode = IOMODE_NONBLOCK;
526 else goto usage;
527 }
528
529 if( strcmp( p, "server_name" ) == 0 )
530 opt.server_name = q;
531
532 if( strcmp( p, "server_port" ) == 0 )
533 {
534 opt.server_port = atoi( q );
535 if( opt.server_port < 1 || opt.server_port > 65535 )
536 goto usage;
537 }
538
539 if( strcmp( p, "command" ) == 0 )
540 {
541 if( strcmp( q, "read" ) == 0 )
542 opt.command = COMMAND_READ;
543 else
544 if( strcmp( q, "write" ) == 0 )
545 opt.command = COMMAND_WRITE;
546 else
547 if( strcmp( q, "both" ) == 0 )
548 {
549 opt.iomode = IOMODE_NONBLOCK;
550 opt.command = COMMAND_BOTH;
551 }
552 else goto usage;
553 }
554
555 if( strcmp( p, "buffer_size" ) == 0 )
556 {
557 opt.buffer_size = atoi( q );
558 if( opt.buffer_size < 1 || opt.buffer_size > 1048576 )
559 goto usage;
560 }
561
562 if( strcmp( p, "max_bytes" ) == 0 )
563 opt.max_bytes = atoi( q );
564
565 if( strcmp( p, "debug_level" ) == 0 )
566 opt.debug_level = atoi( q );
Paul Bakker44618dd2013-07-04 10:34:10 +0200567#if defined(POLARSSL_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 if( strcmp( p, "conn_timeout" ) == 0 )
569 opt.conn_timeout = atoi( q );
Paul Bakker44618dd2013-07-04 10:34:10 +0200570#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000571 if( strcmp( p, "max_connections" ) == 0 )
572 opt.max_connections = atoi( q );
573
574 if( strcmp( p, "session_reuse" ) == 0 )
575 {
576 if( strcmp( q, "on" ) == 0 )
577 opt.session_reuse = 1;
578 else
579 if( strcmp( q, "off" ) == 0 )
580 opt.session_reuse = 0;
581 else
582 goto usage;
583 }
584
585 if( strcmp( p, "session_lifetime" ) == 0 )
586 opt.session_lifetime = atoi( q );
587
Paul Bakkere3166ce2011-01-27 17:40:50 +0000588 if( strcmp( p, "force_ciphersuite" ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000589 {
Paul Bakkere3166ce2011-01-27 17:40:50 +0000590 opt.force_ciphersuite[0] = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000591
Paul Bakkere3166ce2011-01-27 17:40:50 +0000592 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
Paul Bakker5121ce52009-01-03 21:22:43 +0000593
Paul Bakkere3166ce2011-01-27 17:40:50 +0000594 if( opt.force_ciphersuite[0] <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000595 goto usage;
596
Paul Bakkere3166ce2011-01-27 17:40:50 +0000597 opt.force_ciphersuite[1] = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000598 }
599 }
600
601 switch( opt.opmode )
602 {
603 case OPMODE_CLIENT:
604 break;
605
606 case OPMODE_SERVER:
607 break;
608
609 default:
610 goto usage;
611 }
612
613 nb_conn = 0;
614
615 do {
616 nb_conn++;
617 ret = ssl_test( &opt );
618 if( opt.max_connections != 0 &&
619 opt.max_connections <= nb_conn )
620 break;
621 }
622 while( ret == 0 );
623
624exit:
625
Paul Bakkercce9d772011-11-18 14:26:47 +0000626#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000627 polarssl_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000628 fflush( stdout ); getchar();
629#endif
630
631 return( ret );
632}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000633#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000634 POLARSSL_SSL_SRV_C && POLARSSL_SSL_CLI_C && POLARSSL_NET_C &&
Paul Bakker508ad5a2011-12-04 17:09:26 +0000635 POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */