ssl_test.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. * SSL/TLS stress testing program
  3. *
  4. * Copyright (C) 2006-2010, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  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. #ifndef _CRT_SECURE_NO_DEPRECATE
  26. #define _CRT_SECURE_NO_DEPRECATE 1
  27. #endif
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include "polarssl/config.h"
  32. #include "polarssl/net.h"
  33. #include "polarssl/ssl.h"
  34. #include "polarssl/havege.h"
  35. #include "polarssl/timing.h"
  36. #include "polarssl/certs.h"
  37. #define OPMODE_NONE 0
  38. #define OPMODE_CLIENT 1
  39. #define OPMODE_SERVER 2
  40. #define IOMODE_BLOCK 0
  41. #define IOMODE_NONBLOCK 1
  42. #define COMMAND_READ 1
  43. #define COMMAND_WRITE 2
  44. #define COMMAND_BOTH 3
  45. #define DFL_OPMODE OPMODE_NONE
  46. #define DFL_IOMODE IOMODE_BLOCK
  47. #define DFL_SERVER_NAME "localhost"
  48. #define DFL_SERVER_PORT 4433
  49. #define DFL_COMMAND COMMAND_READ
  50. #define DFL_BUFFER_SIZE 1024
  51. #define DFL_MAX_BYTES 0
  52. #define DFL_DEBUG_LEVEL 0
  53. #define DFL_CONN_TIMEOUT 0
  54. #define DFL_MAX_CONNECTIONS 0
  55. #define DFL_SESSION_REUSE 1
  56. #define DFL_SESSION_LIFETIME 86400
  57. #define DFL_FORCE_CIPHER 0
  58. /*
  59. * server-specific data
  60. */
  61. char *dhm_G = "4";
  62. char *dhm_P =
  63. "E4004C1F94182000103D883A448B3F802CE4B44A83301270002C20D0321CFD00" \
  64. "11CCEF784C26A400F43DFB901BCA7538F2C6B176001CF5A0FD16D2C48B1D0C1C" \
  65. "F6AC8E1DA6BCC3B4E1F96B0564965300FFA1D0B601EB2800F489AA512C4B248C" \
  66. "01F76949A60BB7F00A40B1EAB64BDD48E8A700D60B7F1200FA8E77B0A979DABF";
  67. int server_fd = -1;
  68. /*
  69. * global options
  70. */
  71. struct options
  72. {
  73. int opmode; /* operation mode (client or server) */
  74. int iomode; /* I/O mode (blocking or non-blocking) */
  75. char *server_name; /* hostname of the server (client only) */
  76. int server_port; /* port on which the ssl service runs */
  77. int command; /* what to do: read or write operation */
  78. int buffer_size; /* size of the send/receive buffer */
  79. int max_bytes; /* max. # of bytes before a reconnect */
  80. int debug_level; /* level of debugging */
  81. int conn_timeout; /* max. delay before a reconnect */
  82. int max_connections; /* max. number of reconnections */
  83. int session_reuse; /* flag to reuse the keying material */
  84. int session_lifetime; /* if reached, session data is expired */
  85. int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
  86. };
  87. /*
  88. * Although this PRNG has good statistical properties (eg. passes
  89. * DIEHARD), it is not cryptographically secure.
  90. */
  91. unsigned long int lcppm5( unsigned long int *state )
  92. {
  93. unsigned long int u, v;
  94. u = v = state[4] ^ 1;
  95. state[u & 3] ^= u;
  96. u ^= (v << 12) ^ (v >> 12);
  97. u ^= v * state[0]; v >>= 8;
  98. u ^= v * state[1]; v >>= 8;
  99. u ^= v * state[2]; v >>= 8;
  100. u ^= v * state[3];
  101. u &= 0xFFFFFFFF;
  102. state[4] = u;
  103. return( u );
  104. }
  105. void my_debug( void *ctx, int level, const char *str )
  106. {
  107. if( level < ((struct options *) ctx)->debug_level )
  108. fprintf( stderr, "%s", str );
  109. }
  110. #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_HAVEGE_C) || \
  111. !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \
  112. !defined(POLARSSL_SSL_CLI_C) || !defined(POLARSSL_NET_C) || \
  113. !defined(POLARSSL_RSA_C)
  114. int main( void )
  115. {
  116. printf("POLARSSL_BIGNUM_C and/or POLARSSL_HAVEGE_C and/or "
  117. "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
  118. "POLARSSL_SSL_CLI_C and/or POLARSSL_NET_C and/or "
  119. "POLARSSL_RSA_C not defined.\n");
  120. return( 0 );
  121. }
  122. #else
  123. /*
  124. * perform a single SSL connection
  125. */
  126. static int ssl_test( struct options *opt )
  127. {
  128. int ret, i;
  129. int client_fd;
  130. int bytes_to_read;
  131. int bytes_to_write;
  132. int offset_to_read = 0;
  133. int offset_to_write = 0;
  134. long int nb_read;
  135. long int nb_written;
  136. unsigned long read_state[5];
  137. unsigned long write_state[5];
  138. unsigned char *read_buf = NULL;
  139. unsigned char *write_buf = NULL;
  140. struct hr_time t;
  141. havege_state hs;
  142. ssl_context ssl;
  143. ssl_session ssn;
  144. x509_cert srvcert;
  145. rsa_context rsa;
  146. ret = 1;
  147. havege_init( &hs );
  148. get_timer( &t, 1 );
  149. memset( read_state, 0, sizeof( read_state ) );
  150. memset( write_state, 0, sizeof( write_state ) );
  151. memset( &srvcert, 0, sizeof( x509_cert ) );
  152. memset( &rsa, 0, sizeof( rsa_context ) );
  153. if( opt->opmode == OPMODE_CLIENT )
  154. {
  155. if( ( ret = net_connect( &client_fd, opt->server_name,
  156. opt->server_port ) ) != 0 )
  157. {
  158. printf( " ! net_connect returned %d\n\n", ret );
  159. return( ret );
  160. }
  161. if( ( ret = ssl_init( &ssl ) ) != 0 )
  162. {
  163. printf( " ! ssl_init returned %d\n\n", ret );
  164. return( ret );
  165. }
  166. ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
  167. }
  168. if( opt->opmode == OPMODE_SERVER )
  169. {
  170. #if !defined(POLARSSL_CERTS_C)
  171. printf("POLARSSL_CERTS_C not defined.\n");
  172. goto exit;
  173. #else
  174. ret = x509parse_crt( &srvcert, (unsigned char *) test_srv_crt,
  175. strlen( test_srv_crt ) );
  176. if( ret != 0 )
  177. {
  178. printf( " ! x509parse_crt returned %d\n\n", ret );
  179. goto exit;
  180. }
  181. ret = x509parse_crt( &srvcert, (unsigned char *) test_ca_crt,
  182. strlen( test_ca_crt ) );
  183. if( ret != 0 )
  184. {
  185. printf( " ! x509parse_crt returned %d\n\n", ret );
  186. goto exit;
  187. }
  188. ret = x509parse_key( &rsa, (unsigned char *) test_srv_key,
  189. strlen( test_srv_key ), NULL, 0 );
  190. if( ret != 0 )
  191. {
  192. printf( " ! x509parse_key returned %d\n\n", ret );
  193. goto exit;
  194. }
  195. #endif
  196. if( server_fd < 0 )
  197. {
  198. if( ( ret = net_bind( &server_fd, NULL,
  199. opt->server_port ) ) != 0 )
  200. {
  201. printf( " ! net_bind returned %d\n\n", ret );
  202. return( ret );
  203. }
  204. }
  205. if( ( ret = net_accept( server_fd, &client_fd, NULL ) ) != 0 )
  206. {
  207. printf( " ! net_accept returned %d\n\n", ret );
  208. return( ret );
  209. }
  210. if( ( ret = ssl_init( &ssl ) ) != 0 )
  211. {
  212. printf( " ! ssl_init returned %d\n\n", ret );
  213. return( ret );
  214. }
  215. ssl_set_endpoint( &ssl, SSL_IS_SERVER );
  216. ssl_set_dh_param( &ssl, dhm_P, dhm_G );
  217. ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
  218. ssl_set_own_cert( &ssl, &srvcert, &rsa );
  219. }
  220. ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
  221. ssl_set_rng( &ssl, havege_rand, &hs );
  222. ssl_set_dbg( &ssl, my_debug, opt );
  223. ssl_set_bio( &ssl, net_recv, &client_fd,
  224. net_send, &client_fd );
  225. ssl_set_session( &ssl, opt->session_reuse,
  226. opt->session_lifetime, &ssn );
  227. if( opt->force_ciphersuite[0] == DFL_FORCE_CIPHER )
  228. ssl_set_ciphersuites( &ssl, ssl_default_ciphersuites );
  229. else ssl_set_ciphersuites( &ssl, opt->force_ciphersuite );
  230. if( opt->iomode == IOMODE_NONBLOCK )
  231. net_set_nonblock( client_fd );
  232. read_buf = (unsigned char *) malloc( opt->buffer_size );
  233. write_buf = (unsigned char *) malloc( opt->buffer_size );
  234. if( read_buf == NULL || write_buf == NULL )
  235. {
  236. printf( " ! malloc(%d bytes) failed\n\n", opt->buffer_size );
  237. goto exit;
  238. }
  239. nb_read = bytes_to_read = 0;
  240. nb_written = bytes_to_write = 0;
  241. while( 1 )
  242. {
  243. if( opt->command & COMMAND_WRITE )
  244. {
  245. if( bytes_to_write == 0 )
  246. {
  247. while( bytes_to_write == 0 )
  248. bytes_to_write = rand() % opt->buffer_size;
  249. for( i = 0; i < bytes_to_write; i++ )
  250. write_buf[i] = (unsigned char) lcppm5( write_state );
  251. offset_to_write = 0;
  252. }
  253. ret = ssl_write( &ssl, write_buf + offset_to_write,
  254. bytes_to_write );
  255. if( ret >= 0 )
  256. {
  257. nb_written += ret;
  258. bytes_to_write -= ret;
  259. offset_to_write += ret;
  260. }
  261. if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY ||
  262. ret == POLARSSL_ERR_NET_CONN_RESET )
  263. {
  264. ret = 0;
  265. goto exit;
  266. }
  267. if( ret < 0 && ret != POLARSSL_ERR_NET_WANT_READ &&
  268. ret != POLARSSL_ERR_NET_WANT_WRITE )
  269. {
  270. printf( " ! ssl_write returned %d\n\n", ret );
  271. break;
  272. }
  273. }
  274. if( opt->command & COMMAND_READ )
  275. {
  276. if( bytes_to_read == 0 )
  277. {
  278. bytes_to_read = rand() % opt->buffer_size;
  279. offset_to_read = 0;
  280. }
  281. ret = ssl_read( &ssl, read_buf + offset_to_read,
  282. bytes_to_read );
  283. if( ret >= 0 )
  284. {
  285. for( i = 0; i < ret; i++ )
  286. {
  287. if( read_buf[offset_to_read + i] !=
  288. (unsigned char) lcppm5( read_state ) )
  289. {
  290. ret = 1;
  291. printf( " ! plaintext mismatch\n\n" );
  292. goto exit;
  293. }
  294. }
  295. nb_read += ret;
  296. bytes_to_read -= ret;
  297. offset_to_read += ret;
  298. }
  299. if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY ||
  300. ret == POLARSSL_ERR_NET_CONN_RESET )
  301. {
  302. ret = 0;
  303. goto exit;
  304. }
  305. if( ret < 0 && ret != POLARSSL_ERR_NET_WANT_READ &&
  306. ret != POLARSSL_ERR_NET_WANT_WRITE )
  307. {
  308. printf( " ! ssl_read returned %d\n\n", ret );
  309. break;
  310. }
  311. }
  312. ret = 0;
  313. if( opt->max_bytes != 0 &&
  314. ( opt->max_bytes <= nb_read ||
  315. opt->max_bytes <= nb_written ) )
  316. break;
  317. if( opt->conn_timeout != 0 &&
  318. opt->conn_timeout <= (int) get_timer( &t, 0 ) )
  319. break;
  320. }
  321. exit:
  322. fflush( stdout );
  323. if( read_buf != NULL )
  324. free( read_buf );
  325. if( write_buf != NULL )
  326. free( write_buf );
  327. ssl_close_notify( &ssl );
  328. x509_free( &srvcert );
  329. rsa_free( &rsa );
  330. ssl_free( &ssl );
  331. net_close( client_fd );
  332. return( ret );
  333. }
  334. #define USAGE \
  335. "\n usage: ssl_test opmode=<> command=<>...\n" \
  336. "\n acceptable parameters:\n" \
  337. " opmode=client/server default: <none>\n" \
  338. " iomode=block/nonblock default: block\n" \
  339. " server_name=%%s default: localhost\n" \
  340. " server_port=%%d default: 4433\n" \
  341. " command=read/write/both default: read\n" \
  342. " buffer_size=%%d (bytes) default: 1024\n" \
  343. " max_bytes=%%d (bytes) default: 0 (no limit)\n" \
  344. " debug_level=%%d default: 0 (disabled)\n" \
  345. " conn_timeout=%%d (ms) default: 0 (no timeout)\n" \
  346. " max_connections=%%d default: 0 (no limit)\n" \
  347. " session_reuse=on/off default: on (enabled)\n" \
  348. " session_lifetime=%%d (s) default: 86400\n" \
  349. " force_ciphersuite=<name> default: all enabled\n" \
  350. " acceptable ciphersuite names:\n"
  351. int main( int argc, char *argv[] )
  352. {
  353. int i, j, n;
  354. const int *list;
  355. int ret = 1;
  356. int nb_conn;
  357. char *p, *q;
  358. struct options opt;
  359. if( argc == 1 )
  360. {
  361. usage:
  362. printf( USAGE );
  363. list = ssl_list_ciphersuites();
  364. while( *list )
  365. {
  366. printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
  367. list++;
  368. }
  369. printf("\n");
  370. goto exit;
  371. }
  372. opt.opmode = DFL_OPMODE;
  373. opt.iomode = DFL_IOMODE;
  374. opt.server_name = DFL_SERVER_NAME;
  375. opt.server_port = DFL_SERVER_PORT;
  376. opt.command = DFL_COMMAND;
  377. opt.buffer_size = DFL_BUFFER_SIZE;
  378. opt.max_bytes = DFL_MAX_BYTES;
  379. opt.debug_level = DFL_DEBUG_LEVEL;
  380. opt.conn_timeout = DFL_CONN_TIMEOUT;
  381. opt.max_connections = DFL_MAX_CONNECTIONS;
  382. opt.session_reuse = DFL_SESSION_REUSE;
  383. opt.session_lifetime = DFL_SESSION_LIFETIME;
  384. opt.force_ciphersuite[0] = DFL_FORCE_CIPHER;
  385. for( i = 1; i < argc; i++ )
  386. {
  387. n = strlen( argv[i] );
  388. for( j = 0; j < n; j++ )
  389. {
  390. if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
  391. argv[i][j] |= 0x20;
  392. }
  393. p = argv[i];
  394. if( ( q = strchr( p, '=' ) ) == NULL )
  395. continue;
  396. *q++ = '\0';
  397. if( strcmp( p, "opmode" ) == 0 )
  398. {
  399. if( strcmp( q, "client" ) == 0 )
  400. opt.opmode = OPMODE_CLIENT;
  401. else
  402. if( strcmp( q, "server" ) == 0 )
  403. opt.opmode = OPMODE_SERVER;
  404. else goto usage;
  405. }
  406. if( strcmp( p, "iomode" ) == 0 )
  407. {
  408. if( strcmp( q, "block" ) == 0 )
  409. opt.iomode = IOMODE_BLOCK;
  410. else
  411. if( strcmp( q, "nonblock" ) == 0 )
  412. opt.iomode = IOMODE_NONBLOCK;
  413. else goto usage;
  414. }
  415. if( strcmp( p, "server_name" ) == 0 )
  416. opt.server_name = q;
  417. if( strcmp( p, "server_port" ) == 0 )
  418. {
  419. opt.server_port = atoi( q );
  420. if( opt.server_port < 1 || opt.server_port > 65535 )
  421. goto usage;
  422. }
  423. if( strcmp( p, "command" ) == 0 )
  424. {
  425. if( strcmp( q, "read" ) == 0 )
  426. opt.command = COMMAND_READ;
  427. else
  428. if( strcmp( q, "write" ) == 0 )
  429. opt.command = COMMAND_WRITE;
  430. else
  431. if( strcmp( q, "both" ) == 0 )
  432. {
  433. opt.iomode = IOMODE_NONBLOCK;
  434. opt.command = COMMAND_BOTH;
  435. }
  436. else goto usage;
  437. }
  438. if( strcmp( p, "buffer_size" ) == 0 )
  439. {
  440. opt.buffer_size = atoi( q );
  441. if( opt.buffer_size < 1 || opt.buffer_size > 1048576 )
  442. goto usage;
  443. }
  444. if( strcmp( p, "max_bytes" ) == 0 )
  445. opt.max_bytes = atoi( q );
  446. if( strcmp( p, "debug_level" ) == 0 )
  447. opt.debug_level = atoi( q );
  448. if( strcmp( p, "conn_timeout" ) == 0 )
  449. opt.conn_timeout = atoi( q );
  450. if( strcmp( p, "max_connections" ) == 0 )
  451. opt.max_connections = atoi( q );
  452. if( strcmp( p, "session_reuse" ) == 0 )
  453. {
  454. if( strcmp( q, "on" ) == 0 )
  455. opt.session_reuse = 1;
  456. else
  457. if( strcmp( q, "off" ) == 0 )
  458. opt.session_reuse = 0;
  459. else
  460. goto usage;
  461. }
  462. if( strcmp( p, "session_lifetime" ) == 0 )
  463. opt.session_lifetime = atoi( q );
  464. if( strcmp( p, "force_ciphersuite" ) == 0 )
  465. {
  466. opt.force_ciphersuite[0] = -1;
  467. opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
  468. if( opt.force_ciphersuite[0] <= 0 )
  469. goto usage;
  470. opt.force_ciphersuite[1] = 0;
  471. }
  472. }
  473. switch( opt.opmode )
  474. {
  475. case OPMODE_CLIENT:
  476. break;
  477. case OPMODE_SERVER:
  478. break;
  479. default:
  480. goto usage;
  481. }
  482. nb_conn = 0;
  483. do {
  484. nb_conn++;
  485. ret = ssl_test( &opt );
  486. if( opt.max_connections != 0 &&
  487. opt.max_connections <= nb_conn )
  488. break;
  489. }
  490. while( ret == 0 );
  491. exit:
  492. #ifdef WIN32
  493. printf( " Press Enter to exit this program.\n" );
  494. fflush( stdout ); getchar();
  495. #endif
  496. return( ret );
  497. }
  498. #endif /* POLARSSL_BIGNUM_C && POLARSSL_HAVEGE_C && POLARSSL_SSL_TLS_C &&
  499. POLARSSL_SSL_SRV_C && POLARSSL_SSL_CLI_C && POLARSSL_NET_C &&
  500. POLARSSL_RSA_C */