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