Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSL/TLS stress testing program |
| 3 | * |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 4 | * Copyright (C) 2006-2013, Brainspark B.V. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * |
| 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 8 | * |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 9 | * All rights reserved. |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 10 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | */ |
| 25 | |
| 26 | #ifndef _CRT_SECURE_NO_DEPRECATE |
| 27 | #define _CRT_SECURE_NO_DEPRECATE 1 |
| 28 | #endif |
| 29 | |
| 30 | #include <string.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <stdio.h> |
| 33 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 34 | #include "polarssl/config.h" |
| 35 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 36 | #include "polarssl/net.h" |
| 37 | #include "polarssl/ssl.h" |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 38 | #include "polarssl/entropy.h" |
| 39 | #include "polarssl/ctr_drbg.h" |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 40 | #include "polarssl/timing.h" |
| 41 | #include "polarssl/certs.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 42 | |
| 43 | #define OPMODE_NONE 0 |
| 44 | #define OPMODE_CLIENT 1 |
| 45 | #define OPMODE_SERVER 2 |
| 46 | |
| 47 | #define IOMODE_BLOCK 0 |
| 48 | #define IOMODE_NONBLOCK 1 |
| 49 | |
| 50 | #define COMMAND_READ 1 |
| 51 | #define COMMAND_WRITE 2 |
| 52 | #define COMMAND_BOTH 3 |
| 53 | |
| 54 | #define DFL_OPMODE OPMODE_NONE |
| 55 | #define DFL_IOMODE IOMODE_BLOCK |
| 56 | #define DFL_SERVER_NAME "localhost" |
| 57 | #define DFL_SERVER_PORT 4433 |
| 58 | #define DFL_COMMAND COMMAND_READ |
| 59 | #define DFL_BUFFER_SIZE 1024 |
| 60 | #define DFL_MAX_BYTES 0 |
| 61 | #define DFL_DEBUG_LEVEL 0 |
| 62 | #define DFL_CONN_TIMEOUT 0 |
| 63 | #define DFL_MAX_CONNECTIONS 0 |
| 64 | #define DFL_SESSION_REUSE 1 |
| 65 | #define DFL_SESSION_LIFETIME 86400 |
| 66 | #define DFL_FORCE_CIPHER 0 |
| 67 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 68 | int server_fd = -1; |
| 69 | |
| 70 | /* |
| 71 | * global options |
| 72 | */ |
| 73 | struct options |
| 74 | { |
| 75 | int opmode; /* operation mode (client or server) */ |
| 76 | int iomode; /* I/O mode (blocking or non-blocking) */ |
| 77 | char *server_name; /* hostname of the server (client only) */ |
| 78 | int server_port; /* port on which the ssl service runs */ |
| 79 | int command; /* what to do: read or write operation */ |
| 80 | int buffer_size; /* size of the send/receive buffer */ |
| 81 | int max_bytes; /* max. # of bytes before a reconnect */ |
| 82 | int debug_level; /* level of debugging */ |
| 83 | int conn_timeout; /* max. delay before a reconnect */ |
| 84 | int max_connections; /* max. number of reconnections */ |
| 85 | int session_reuse; /* flag to reuse the keying material */ |
| 86 | int session_lifetime; /* if reached, session data is expired */ |
Paul Bakker | e3166ce | 2011-01-27 17:40:50 +0000 | [diff] [blame] | 87 | int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | /* |
| 91 | * Although this PRNG has good statistical properties (eg. passes |
| 92 | * DIEHARD), it is not cryptographically secure. |
| 93 | */ |
| 94 | unsigned long int lcppm5( unsigned long int *state ) |
| 95 | { |
| 96 | unsigned long int u, v; |
| 97 | |
| 98 | u = v = state[4] ^ 1; |
| 99 | state[u & 3] ^= u; |
| 100 | u ^= (v << 12) ^ (v >> 12); |
| 101 | u ^= v * state[0]; v >>= 8; |
| 102 | u ^= v * state[1]; v >>= 8; |
| 103 | u ^= v * state[2]; v >>= 8; |
| 104 | u ^= v * state[3]; |
| 105 | u &= 0xFFFFFFFF; |
| 106 | state[4] = u; |
| 107 | |
| 108 | return( u ); |
| 109 | } |
| 110 | |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 111 | void my_debug( void *ctx, int level, const char *str ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 112 | { |
| 113 | if( level < ((struct options *) ctx)->debug_level ) |
| 114 | fprintf( stderr, "%s", str ); |
| 115 | } |
| 116 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 117 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \ |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 118 | !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \ |
| 119 | !defined(POLARSSL_SSL_CLI_C) || !defined(POLARSSL_NET_C) || \ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 120 | !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C) || \ |
| 121 | !defined(POLARSSL_X509_PARSE_C) |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 122 | int main( int argc, char *argv[] ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 123 | { |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 124 | ((void) argc); |
| 125 | ((void) argv); |
| 126 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 127 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or " |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 128 | "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or " |
| 129 | "POLARSSL_SSL_CLI_C and/or POLARSSL_NET_C and/or " |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 130 | "POLARSSL_RSA_C and/or POLARSSL_CTR_DRBG_C and/or " |
| 131 | "POLARSSL_X509_PARSE_C not defined.\n"); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 132 | return( 0 ); |
| 133 | } |
| 134 | #else |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 135 | /* |
| 136 | * perform a single SSL connection |
| 137 | */ |
| 138 | static int ssl_test( struct options *opt ) |
| 139 | { |
| 140 | int ret, i; |
| 141 | int client_fd; |
| 142 | int bytes_to_read; |
| 143 | int bytes_to_write; |
Paul Bakker | 026c03b | 2009-03-28 17:53:03 +0000 | [diff] [blame] | 144 | int offset_to_read = 0; |
| 145 | int offset_to_write = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 146 | |
| 147 | long int nb_read; |
| 148 | long int nb_written; |
| 149 | |
| 150 | unsigned long read_state[5]; |
| 151 | unsigned long write_state[5]; |
| 152 | |
Paul Bakker | 026c03b | 2009-03-28 17:53:03 +0000 | [diff] [blame] | 153 | unsigned char *read_buf = NULL; |
| 154 | unsigned char *write_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 155 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 156 | char *pers = "ssl_test"; |
| 157 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 158 | struct hr_time t; |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 159 | entropy_context entropy; |
| 160 | ctr_drbg_context ctr_drbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 161 | ssl_context ssl; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 162 | x509_cert srvcert; |
| 163 | rsa_context rsa; |
| 164 | |
| 165 | ret = 1; |
| 166 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 167 | entropy_init( &entropy ); |
| 168 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
| 169 | (unsigned char *) pers, strlen( pers ) ) ) != 0 ) |
| 170 | { |
| 171 | printf( " ! ctr_drbg_init returned %d\n", ret ); |
| 172 | goto exit; |
| 173 | } |
| 174 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 175 | get_timer( &t, 1 ); |
| 176 | |
| 177 | memset( read_state, 0, sizeof( read_state ) ); |
| 178 | memset( write_state, 0, sizeof( write_state ) ); |
| 179 | |
| 180 | memset( &srvcert, 0, sizeof( x509_cert ) ); |
| 181 | memset( &rsa, 0, sizeof( rsa_context ) ); |
| 182 | |
| 183 | if( opt->opmode == OPMODE_CLIENT ) |
| 184 | { |
| 185 | if( ( ret = net_connect( &client_fd, opt->server_name, |
| 186 | opt->server_port ) ) != 0 ) |
| 187 | { |
| 188 | printf( " ! net_connect returned %d\n\n", ret ); |
| 189 | return( ret ); |
| 190 | } |
| 191 | |
| 192 | if( ( ret = ssl_init( &ssl ) ) != 0 ) |
| 193 | { |
| 194 | printf( " ! ssl_init returned %d\n\n", ret ); |
| 195 | return( ret ); |
| 196 | } |
| 197 | |
| 198 | ssl_set_endpoint( &ssl, SSL_IS_CLIENT ); |
| 199 | } |
| 200 | |
| 201 | if( opt->opmode == OPMODE_SERVER ) |
| 202 | { |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 203 | #if !defined(POLARSSL_CERTS_C) |
| 204 | printf("POLARSSL_CERTS_C not defined.\n"); |
| 205 | goto exit; |
| 206 | #else |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 207 | ret = x509parse_crt( &srvcert, (unsigned char *) test_srv_crt, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 208 | strlen( test_srv_crt ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 209 | if( ret != 0 ) |
| 210 | { |
| 211 | printf( " ! x509parse_crt returned %d\n\n", ret ); |
| 212 | goto exit; |
| 213 | } |
| 214 | |
| 215 | ret = x509parse_crt( &srvcert, (unsigned char *) test_ca_crt, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 216 | strlen( test_ca_crt ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 217 | if( ret != 0 ) |
| 218 | { |
| 219 | printf( " ! x509parse_crt returned %d\n\n", ret ); |
| 220 | goto exit; |
| 221 | } |
| 222 | |
| 223 | ret = x509parse_key( &rsa, (unsigned char *) test_srv_key, |
| 224 | strlen( test_srv_key ), NULL, 0 ); |
| 225 | if( ret != 0 ) |
| 226 | { |
| 227 | printf( " ! x509parse_key returned %d\n\n", ret ); |
| 228 | goto exit; |
| 229 | } |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 230 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 231 | |
| 232 | if( server_fd < 0 ) |
| 233 | { |
| 234 | if( ( ret = net_bind( &server_fd, NULL, |
| 235 | opt->server_port ) ) != 0 ) |
| 236 | { |
| 237 | printf( " ! net_bind returned %d\n\n", ret ); |
| 238 | return( ret ); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | if( ( ret = net_accept( server_fd, &client_fd, NULL ) ) != 0 ) |
| 243 | { |
| 244 | printf( " ! net_accept returned %d\n\n", ret ); |
| 245 | return( ret ); |
| 246 | } |
| 247 | |
| 248 | if( ( ret = ssl_init( &ssl ) ) != 0 ) |
| 249 | { |
| 250 | printf( " ! ssl_init returned %d\n\n", ret ); |
| 251 | return( ret ); |
| 252 | } |
| 253 | |
| 254 | ssl_set_endpoint( &ssl, SSL_IS_SERVER ); |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 255 | ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 256 | ssl_set_own_cert( &ssl, &srvcert, &rsa ); |
| 257 | } |
| 258 | |
| 259 | ssl_set_authmode( &ssl, SSL_VERIFY_NONE ); |
| 260 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 261 | ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 262 | ssl_set_dbg( &ssl, my_debug, opt ); |
| 263 | ssl_set_bio( &ssl, net_recv, &client_fd, |
| 264 | net_send, &client_fd ); |
| 265 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 266 | if( opt->force_ciphersuite[0] != DFL_FORCE_CIPHER ) |
| 267 | ssl_set_ciphersuites( &ssl, opt->force_ciphersuite ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 268 | |
| 269 | if( opt->iomode == IOMODE_NONBLOCK ) |
| 270 | net_set_nonblock( client_fd ); |
| 271 | |
| 272 | read_buf = (unsigned char *) malloc( opt->buffer_size ); |
| 273 | write_buf = (unsigned char *) malloc( opt->buffer_size ); |
| 274 | |
| 275 | if( read_buf == NULL || write_buf == NULL ) |
| 276 | { |
| 277 | printf( " ! malloc(%d bytes) failed\n\n", opt->buffer_size ); |
| 278 | goto exit; |
| 279 | } |
| 280 | |
| 281 | nb_read = bytes_to_read = 0; |
| 282 | nb_written = bytes_to_write = 0; |
| 283 | |
| 284 | while( 1 ) |
| 285 | { |
| 286 | if( opt->command & COMMAND_WRITE ) |
| 287 | { |
| 288 | if( bytes_to_write == 0 ) |
| 289 | { |
| 290 | while( bytes_to_write == 0 ) |
| 291 | bytes_to_write = rand() % opt->buffer_size; |
| 292 | |
| 293 | for( i = 0; i < bytes_to_write; i++ ) |
| 294 | write_buf[i] = (unsigned char) lcppm5( write_state ); |
| 295 | |
| 296 | offset_to_write = 0; |
| 297 | } |
| 298 | |
| 299 | ret = ssl_write( &ssl, write_buf + offset_to_write, |
| 300 | bytes_to_write ); |
| 301 | |
| 302 | if( ret >= 0 ) |
| 303 | { |
| 304 | nb_written += ret; |
| 305 | bytes_to_write -= ret; |
| 306 | offset_to_write += ret; |
| 307 | } |
| 308 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 309 | if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY || |
| 310 | ret == POLARSSL_ERR_NET_CONN_RESET ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 311 | { |
| 312 | ret = 0; |
| 313 | goto exit; |
| 314 | } |
| 315 | |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 316 | if( ret < 0 && ret != POLARSSL_ERR_NET_WANT_READ && |
| 317 | ret != POLARSSL_ERR_NET_WANT_WRITE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 318 | { |
| 319 | printf( " ! ssl_write returned %d\n\n", ret ); |
| 320 | break; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | if( opt->command & COMMAND_READ ) |
| 325 | { |
| 326 | if( bytes_to_read == 0 ) |
| 327 | { |
| 328 | bytes_to_read = rand() % opt->buffer_size; |
| 329 | offset_to_read = 0; |
| 330 | } |
| 331 | |
| 332 | ret = ssl_read( &ssl, read_buf + offset_to_read, |
| 333 | bytes_to_read ); |
| 334 | |
| 335 | if( ret >= 0 ) |
| 336 | { |
| 337 | for( i = 0; i < ret; i++ ) |
| 338 | { |
| 339 | if( read_buf[offset_to_read + i] != |
| 340 | (unsigned char) lcppm5( read_state ) ) |
| 341 | { |
| 342 | ret = 1; |
| 343 | printf( " ! plaintext mismatch\n\n" ); |
| 344 | goto exit; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | nb_read += ret; |
| 349 | bytes_to_read -= ret; |
| 350 | offset_to_read += ret; |
| 351 | } |
| 352 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 353 | if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY || |
| 354 | ret == POLARSSL_ERR_NET_CONN_RESET ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 355 | { |
| 356 | ret = 0; |
| 357 | goto exit; |
| 358 | } |
| 359 | |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 360 | if( ret < 0 && ret != POLARSSL_ERR_NET_WANT_READ && |
| 361 | ret != POLARSSL_ERR_NET_WANT_WRITE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 362 | { |
| 363 | printf( " ! ssl_read returned %d\n\n", ret ); |
| 364 | break; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | ret = 0; |
| 369 | |
| 370 | if( opt->max_bytes != 0 && |
| 371 | ( opt->max_bytes <= nb_read || |
| 372 | opt->max_bytes <= nb_written ) ) |
| 373 | break; |
| 374 | |
| 375 | if( opt->conn_timeout != 0 && |
| 376 | opt->conn_timeout <= (int) get_timer( &t, 0 ) ) |
| 377 | break; |
| 378 | } |
| 379 | |
| 380 | exit: |
| 381 | |
| 382 | fflush( stdout ); |
| 383 | |
| 384 | if( read_buf != NULL ) |
| 385 | free( read_buf ); |
| 386 | |
| 387 | if( write_buf != NULL ) |
| 388 | free( write_buf ); |
| 389 | |
| 390 | ssl_close_notify( &ssl ); |
| 391 | x509_free( &srvcert ); |
| 392 | rsa_free( &rsa ); |
| 393 | ssl_free( &ssl ); |
| 394 | net_close( client_fd ); |
| 395 | |
| 396 | return( ret ); |
| 397 | } |
| 398 | |
| 399 | #define USAGE \ |
| 400 | "\n usage: ssl_test opmode=<> command=<>...\n" \ |
| 401 | "\n acceptable parameters:\n" \ |
| 402 | " opmode=client/server default: <none>\n" \ |
| 403 | " iomode=block/nonblock default: block\n" \ |
| 404 | " server_name=%%s default: localhost\n" \ |
| 405 | " server_port=%%d default: 4433\n" \ |
| 406 | " command=read/write/both default: read\n" \ |
| 407 | " buffer_size=%%d (bytes) default: 1024\n" \ |
| 408 | " max_bytes=%%d (bytes) default: 0 (no limit)\n" \ |
| 409 | " debug_level=%%d default: 0 (disabled)\n" \ |
| 410 | " conn_timeout=%%d (ms) default: 0 (no timeout)\n" \ |
| 411 | " max_connections=%%d default: 0 (no limit)\n" \ |
| 412 | " session_reuse=on/off default: on (enabled)\n" \ |
| 413 | " session_lifetime=%%d (s) default: 86400\n" \ |
Paul Bakker | e3166ce | 2011-01-27 17:40:50 +0000 | [diff] [blame] | 414 | " force_ciphersuite=<name> default: all enabled\n" \ |
| 415 | " acceptable ciphersuite names:\n" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 416 | |
| 417 | int main( int argc, char *argv[] ) |
| 418 | { |
| 419 | int i, j, n; |
Paul Bakker | e3166ce | 2011-01-27 17:40:50 +0000 | [diff] [blame] | 420 | const int *list; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 421 | int ret = 1; |
| 422 | int nb_conn; |
| 423 | char *p, *q; |
| 424 | struct options opt; |
| 425 | |
| 426 | if( argc == 1 ) |
| 427 | { |
| 428 | usage: |
| 429 | printf( USAGE ); |
Paul Bakker | e3166ce | 2011-01-27 17:40:50 +0000 | [diff] [blame] | 430 | |
| 431 | list = ssl_list_ciphersuites(); |
| 432 | while( *list ) |
| 433 | { |
| 434 | printf(" %s\n", ssl_get_ciphersuite_name( *list ) ); |
| 435 | list++; |
| 436 | } |
| 437 | printf("\n"); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 438 | goto exit; |
| 439 | } |
| 440 | |
| 441 | opt.opmode = DFL_OPMODE; |
| 442 | opt.iomode = DFL_IOMODE; |
| 443 | opt.server_name = DFL_SERVER_NAME; |
| 444 | opt.server_port = DFL_SERVER_PORT; |
| 445 | opt.command = DFL_COMMAND; |
| 446 | opt.buffer_size = DFL_BUFFER_SIZE; |
| 447 | opt.max_bytes = DFL_MAX_BYTES; |
| 448 | opt.debug_level = DFL_DEBUG_LEVEL; |
| 449 | opt.conn_timeout = DFL_CONN_TIMEOUT; |
| 450 | opt.max_connections = DFL_MAX_CONNECTIONS; |
| 451 | opt.session_reuse = DFL_SESSION_REUSE; |
| 452 | opt.session_lifetime = DFL_SESSION_LIFETIME; |
Paul Bakker | e3166ce | 2011-01-27 17:40:50 +0000 | [diff] [blame] | 453 | opt.force_ciphersuite[0] = DFL_FORCE_CIPHER; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 454 | |
| 455 | for( i = 1; i < argc; i++ ) |
| 456 | { |
| 457 | n = strlen( argv[i] ); |
| 458 | |
| 459 | for( j = 0; j < n; j++ ) |
| 460 | { |
| 461 | if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' ) |
| 462 | argv[i][j] |= 0x20; |
| 463 | } |
| 464 | |
| 465 | p = argv[i]; |
| 466 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 467 | continue; |
| 468 | *q++ = '\0'; |
| 469 | |
| 470 | if( strcmp( p, "opmode" ) == 0 ) |
| 471 | { |
| 472 | if( strcmp( q, "client" ) == 0 ) |
| 473 | opt.opmode = OPMODE_CLIENT; |
| 474 | else |
| 475 | if( strcmp( q, "server" ) == 0 ) |
| 476 | opt.opmode = OPMODE_SERVER; |
| 477 | else goto usage; |
| 478 | } |
| 479 | |
| 480 | if( strcmp( p, "iomode" ) == 0 ) |
| 481 | { |
| 482 | if( strcmp( q, "block" ) == 0 ) |
| 483 | opt.iomode = IOMODE_BLOCK; |
| 484 | else |
| 485 | if( strcmp( q, "nonblock" ) == 0 ) |
| 486 | opt.iomode = IOMODE_NONBLOCK; |
| 487 | else goto usage; |
| 488 | } |
| 489 | |
| 490 | if( strcmp( p, "server_name" ) == 0 ) |
| 491 | opt.server_name = q; |
| 492 | |
| 493 | if( strcmp( p, "server_port" ) == 0 ) |
| 494 | { |
| 495 | opt.server_port = atoi( q ); |
| 496 | if( opt.server_port < 1 || opt.server_port > 65535 ) |
| 497 | goto usage; |
| 498 | } |
| 499 | |
| 500 | if( strcmp( p, "command" ) == 0 ) |
| 501 | { |
| 502 | if( strcmp( q, "read" ) == 0 ) |
| 503 | opt.command = COMMAND_READ; |
| 504 | else |
| 505 | if( strcmp( q, "write" ) == 0 ) |
| 506 | opt.command = COMMAND_WRITE; |
| 507 | else |
| 508 | if( strcmp( q, "both" ) == 0 ) |
| 509 | { |
| 510 | opt.iomode = IOMODE_NONBLOCK; |
| 511 | opt.command = COMMAND_BOTH; |
| 512 | } |
| 513 | else goto usage; |
| 514 | } |
| 515 | |
| 516 | if( strcmp( p, "buffer_size" ) == 0 ) |
| 517 | { |
| 518 | opt.buffer_size = atoi( q ); |
| 519 | if( opt.buffer_size < 1 || opt.buffer_size > 1048576 ) |
| 520 | goto usage; |
| 521 | } |
| 522 | |
| 523 | if( strcmp( p, "max_bytes" ) == 0 ) |
| 524 | opt.max_bytes = atoi( q ); |
| 525 | |
| 526 | if( strcmp( p, "debug_level" ) == 0 ) |
| 527 | opt.debug_level = atoi( q ); |
| 528 | |
| 529 | if( strcmp( p, "conn_timeout" ) == 0 ) |
| 530 | opt.conn_timeout = atoi( q ); |
| 531 | |
| 532 | if( strcmp( p, "max_connections" ) == 0 ) |
| 533 | opt.max_connections = atoi( q ); |
| 534 | |
| 535 | if( strcmp( p, "session_reuse" ) == 0 ) |
| 536 | { |
| 537 | if( strcmp( q, "on" ) == 0 ) |
| 538 | opt.session_reuse = 1; |
| 539 | else |
| 540 | if( strcmp( q, "off" ) == 0 ) |
| 541 | opt.session_reuse = 0; |
| 542 | else |
| 543 | goto usage; |
| 544 | } |
| 545 | |
| 546 | if( strcmp( p, "session_lifetime" ) == 0 ) |
| 547 | opt.session_lifetime = atoi( q ); |
| 548 | |
Paul Bakker | e3166ce | 2011-01-27 17:40:50 +0000 | [diff] [blame] | 549 | if( strcmp( p, "force_ciphersuite" ) == 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 550 | { |
Paul Bakker | e3166ce | 2011-01-27 17:40:50 +0000 | [diff] [blame] | 551 | opt.force_ciphersuite[0] = -1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 552 | |
Paul Bakker | e3166ce | 2011-01-27 17:40:50 +0000 | [diff] [blame] | 553 | opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 554 | |
Paul Bakker | e3166ce | 2011-01-27 17:40:50 +0000 | [diff] [blame] | 555 | if( opt.force_ciphersuite[0] <= 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 556 | goto usage; |
| 557 | |
Paul Bakker | e3166ce | 2011-01-27 17:40:50 +0000 | [diff] [blame] | 558 | opt.force_ciphersuite[1] = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | |
| 562 | switch( opt.opmode ) |
| 563 | { |
| 564 | case OPMODE_CLIENT: |
| 565 | break; |
| 566 | |
| 567 | case OPMODE_SERVER: |
| 568 | break; |
| 569 | |
| 570 | default: |
| 571 | goto usage; |
| 572 | } |
| 573 | |
| 574 | nb_conn = 0; |
| 575 | |
| 576 | do { |
| 577 | nb_conn++; |
| 578 | ret = ssl_test( &opt ); |
| 579 | if( opt.max_connections != 0 && |
| 580 | opt.max_connections <= nb_conn ) |
| 581 | break; |
| 582 | } |
| 583 | while( ret == 0 ); |
| 584 | |
| 585 | exit: |
| 586 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 587 | #if defined(_WIN32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 588 | printf( " Press Enter to exit this program.\n" ); |
| 589 | fflush( stdout ); getchar(); |
| 590 | #endif |
| 591 | |
| 592 | return( ret ); |
| 593 | } |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 594 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C && |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 595 | POLARSSL_SSL_SRV_C && POLARSSL_SSL_CLI_C && POLARSSL_NET_C && |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 596 | POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */ |