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