ssl_test.c 18 KB

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