s_time.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. /* apps/s_time.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #define NO_SHUTDOWN
  59. /*-----------------------------------------
  60. s_time - SSL client connection timer program
  61. Written and donated by Larry Streepy <streepy@healthcare.com>
  62. -----------------------------------------*/
  63. #include <stdio.h>
  64. #include <stdlib.h>
  65. #include <string.h>
  66. #define USE_SOCKETS
  67. #include "apps.h"
  68. #ifdef OPENSSL_NO_STDIO
  69. #define APPS_WIN16
  70. #endif
  71. #include <openssl/x509.h>
  72. #include <openssl/ssl.h>
  73. #include <openssl/pem.h>
  74. #include "s_apps.h"
  75. #include <openssl/err.h>
  76. #ifdef WIN32_STUFF
  77. #include "winmain.h"
  78. #include "wintext.h"
  79. #endif
  80. #if !defined(OPENSSL_SYS_MSDOS)
  81. #include OPENSSL_UNISTD
  82. #endif
  83. #undef PROG
  84. #define PROG s_time_main
  85. #undef ioctl
  86. #define ioctl ioctlsocket
  87. #define SSL_CONNECT_NAME "localhost:4433"
  88. /*#define TEST_CERT "client.pem" */ /* no default cert. */
  89. #undef BUFSIZZ
  90. #define BUFSIZZ 1024*10
  91. #define MYBUFSIZ 1024*8
  92. #undef min
  93. #undef max
  94. #define min(a,b) (((a) < (b)) ? (a) : (b))
  95. #define max(a,b) (((a) > (b)) ? (a) : (b))
  96. #undef SECONDS
  97. #define SECONDS 30
  98. extern int verify_depth;
  99. extern int verify_error;
  100. static void s_time_usage(void);
  101. static int parseArgs( int argc, char **argv );
  102. static SSL *doConnection( SSL *scon );
  103. static void s_time_init(void);
  104. /***********************************************************************
  105. * Static data declarations
  106. */
  107. /* static char *port=PORT_STR;*/
  108. static char *host=SSL_CONNECT_NAME;
  109. static char *t_cert_file=NULL;
  110. static char *t_key_file=NULL;
  111. static char *CApath=NULL;
  112. static char *CAfile=NULL;
  113. static char *tm_cipher=NULL;
  114. static int tm_verify = SSL_VERIFY_NONE;
  115. static int maxTime = SECONDS;
  116. static SSL_CTX *tm_ctx=NULL;
  117. static const SSL_METHOD *s_time_meth=NULL;
  118. static char *s_www_path=NULL;
  119. static long bytes_read=0;
  120. static int st_bugs=0;
  121. static int perform=0;
  122. #ifdef FIONBIO
  123. static int t_nbio=0;
  124. #endif
  125. #ifdef OPENSSL_SYS_WIN32
  126. static int exitNow = 0; /* Set when it's time to exit main */
  127. #endif
  128. static void s_time_init(void)
  129. {
  130. host=SSL_CONNECT_NAME;
  131. t_cert_file=NULL;
  132. t_key_file=NULL;
  133. CApath=NULL;
  134. CAfile=NULL;
  135. tm_cipher=NULL;
  136. tm_verify = SSL_VERIFY_NONE;
  137. maxTime = SECONDS;
  138. tm_ctx=NULL;
  139. s_time_meth=NULL;
  140. s_www_path=NULL;
  141. bytes_read=0;
  142. st_bugs=0;
  143. perform=0;
  144. #ifdef FIONBIO
  145. t_nbio=0;
  146. #endif
  147. #ifdef OPENSSL_SYS_WIN32
  148. exitNow = 0; /* Set when it's time to exit main */
  149. #endif
  150. }
  151. /***********************************************************************
  152. * usage - display usage message
  153. */
  154. static void s_time_usage(void)
  155. {
  156. static char umsg[] = "\
  157. -time arg - max number of seconds to collect data, default %d\n\
  158. -verify arg - turn on peer certificate verification, arg == depth\n\
  159. -cert arg - certificate file to use, PEM format assumed\n\
  160. -key arg - RSA file to use, PEM format assumed, key is in cert file\n\
  161. file if not specified by this option\n\
  162. -CApath arg - PEM format directory of CA's\n\
  163. -CAfile arg - PEM format file of CA's\n\
  164. -cipher - preferred cipher to use, play with 'openssl ciphers'\n\n";
  165. printf( "usage: s_time <args>\n\n" );
  166. printf("-connect host:port - host:port to connect to (default is %s)\n",SSL_CONNECT_NAME);
  167. #ifdef FIONBIO
  168. printf("-nbio - Run with non-blocking IO\n");
  169. printf("-ssl2 - Just use SSLv2\n");
  170. printf("-ssl3 - Just use SSLv3\n");
  171. printf("-bugs - Turn on SSL bug compatibility\n");
  172. printf("-new - Just time new connections\n");
  173. printf("-reuse - Just time connection reuse\n");
  174. printf("-www page - Retrieve 'page' from the site\n");
  175. #endif
  176. printf( umsg,SECONDS );
  177. }
  178. /***********************************************************************
  179. * parseArgs - Parse command line arguments and initialize data
  180. *
  181. * Returns 0 if ok, -1 on bad args
  182. */
  183. static int parseArgs(int argc, char **argv)
  184. {
  185. int badop = 0;
  186. verify_depth=0;
  187. verify_error=X509_V_OK;
  188. argc--;
  189. argv++;
  190. while (argc >= 1) {
  191. if (strcmp(*argv,"-connect") == 0)
  192. {
  193. if (--argc < 1) goto bad;
  194. host= *(++argv);
  195. }
  196. #if 0
  197. else if( strcmp(*argv,"-host") == 0)
  198. {
  199. if (--argc < 1) goto bad;
  200. host= *(++argv);
  201. }
  202. else if( strcmp(*argv,"-port") == 0)
  203. {
  204. if (--argc < 1) goto bad;
  205. port= *(++argv);
  206. }
  207. #endif
  208. else if (strcmp(*argv,"-reuse") == 0)
  209. perform=2;
  210. else if (strcmp(*argv,"-new") == 0)
  211. perform=1;
  212. else if( strcmp(*argv,"-verify") == 0) {
  213. tm_verify=SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE;
  214. if (--argc < 1) goto bad;
  215. verify_depth=atoi(*(++argv));
  216. BIO_printf(bio_err,"verify depth is %d\n",verify_depth);
  217. } else if( strcmp(*argv,"-cert") == 0) {
  218. if (--argc < 1) goto bad;
  219. t_cert_file= *(++argv);
  220. } else if( strcmp(*argv,"-key") == 0) {
  221. if (--argc < 1) goto bad;
  222. t_key_file= *(++argv);
  223. } else if( strcmp(*argv,"-CApath") == 0) {
  224. if (--argc < 1) goto bad;
  225. CApath= *(++argv);
  226. } else if( strcmp(*argv,"-CAfile") == 0) {
  227. if (--argc < 1) goto bad;
  228. CAfile= *(++argv);
  229. } else if( strcmp(*argv,"-cipher") == 0) {
  230. if (--argc < 1) goto bad;
  231. tm_cipher= *(++argv);
  232. }
  233. #ifdef FIONBIO
  234. else if(strcmp(*argv,"-nbio") == 0) {
  235. t_nbio=1;
  236. }
  237. #endif
  238. else if(strcmp(*argv,"-www") == 0)
  239. {
  240. if (--argc < 1) goto bad;
  241. s_www_path= *(++argv);
  242. if(strlen(s_www_path) > MYBUFSIZ-100)
  243. {
  244. BIO_printf(bio_err,"-www option too long\n");
  245. badop=1;
  246. }
  247. }
  248. else if(strcmp(*argv,"-bugs") == 0)
  249. st_bugs=1;
  250. #ifndef OPENSSL_NO_SSL2
  251. else if(strcmp(*argv,"-ssl2") == 0)
  252. s_time_meth=SSLv2_client_method();
  253. #endif
  254. #ifndef OPENSSL_NO_SSL3
  255. else if(strcmp(*argv,"-ssl3") == 0)
  256. s_time_meth=SSLv3_client_method();
  257. #endif
  258. else if( strcmp(*argv,"-time") == 0) {
  259. if (--argc < 1) goto bad;
  260. maxTime= atoi(*(++argv));
  261. }
  262. else {
  263. BIO_printf(bio_err,"unknown option %s\n",*argv);
  264. badop=1;
  265. break;
  266. }
  267. argc--;
  268. argv++;
  269. }
  270. if (perform == 0) perform=3;
  271. if(badop) {
  272. bad:
  273. s_time_usage();
  274. return -1;
  275. }
  276. return 0; /* Valid args */
  277. }
  278. /***********************************************************************
  279. * TIME - time functions
  280. */
  281. #define START 0
  282. #define STOP 1
  283. static double tm_Time_F(int s)
  284. {
  285. return app_tminterval(s,1);
  286. }
  287. /***********************************************************************
  288. * MAIN - main processing area for client
  289. * real name depends on MONOLITH
  290. */
  291. int MAIN(int, char **);
  292. int MAIN(int argc, char **argv)
  293. {
  294. double totalTime = 0.0;
  295. int nConn = 0;
  296. SSL *scon=NULL;
  297. long finishtime=0;
  298. int ret=1,i;
  299. MS_STATIC char buf[1024*8];
  300. int ver;
  301. apps_startup();
  302. s_time_init();
  303. if (bio_err == NULL)
  304. bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
  305. #if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
  306. s_time_meth=SSLv23_client_method();
  307. #elif !defined(OPENSSL_NO_SSL3)
  308. s_time_meth=SSLv3_client_method();
  309. #elif !defined(OPENSSL_NO_SSL2)
  310. s_time_meth=SSLv2_client_method();
  311. #endif
  312. /* parse the command line arguments */
  313. if( parseArgs( argc, argv ) < 0 )
  314. goto end;
  315. OpenSSL_add_ssl_algorithms();
  316. if ((tm_ctx=SSL_CTX_new(s_time_meth)) == NULL) return(1);
  317. SSL_CTX_set_quiet_shutdown(tm_ctx,1);
  318. if (st_bugs) SSL_CTX_set_options(tm_ctx,SSL_OP_ALL);
  319. SSL_CTX_set_cipher_list(tm_ctx,tm_cipher);
  320. if(!set_cert_stuff(tm_ctx,t_cert_file,t_key_file))
  321. goto end;
  322. SSL_load_error_strings();
  323. if ((!SSL_CTX_load_verify_locations(tm_ctx,CAfile,CApath)) ||
  324. (!SSL_CTX_set_default_verify_paths(tm_ctx)))
  325. {
  326. /* BIO_printf(bio_err,"error setting default verify locations\n"); */
  327. ERR_print_errors(bio_err);
  328. /* goto end; */
  329. }
  330. if (tm_cipher == NULL)
  331. tm_cipher = getenv("SSL_CIPHER");
  332. if (tm_cipher == NULL ) {
  333. fprintf( stderr, "No CIPHER specified\n" );
  334. }
  335. if (!(perform & 1)) goto next;
  336. printf( "Collecting connection statistics for %d seconds\n", maxTime );
  337. /* Loop and time how long it takes to make connections */
  338. bytes_read=0;
  339. finishtime=(long)time(NULL)+maxTime;
  340. tm_Time_F(START);
  341. for (;;)
  342. {
  343. if (finishtime < (long)time(NULL)) break;
  344. #ifdef WIN32_STUFF
  345. if( flushWinMsgs(0) == -1 )
  346. goto end;
  347. if( waitingToDie || exitNow ) /* we're dead */
  348. goto end;
  349. #endif
  350. if( (scon = doConnection( NULL )) == NULL )
  351. goto end;
  352. if (s_www_path != NULL)
  353. {
  354. BIO_snprintf(buf,sizeof buf,"GET %s HTTP/1.0\r\n\r\n",s_www_path);
  355. SSL_write(scon,buf,strlen(buf));
  356. while ((i=SSL_read(scon,buf,sizeof(buf))) > 0)
  357. bytes_read+=i;
  358. }
  359. #ifdef NO_SHUTDOWN
  360. SSL_set_shutdown(scon,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
  361. #else
  362. SSL_shutdown(scon);
  363. #endif
  364. SHUTDOWN2(SSL_get_fd(scon));
  365. nConn += 1;
  366. if (SSL_session_reused(scon))
  367. ver='r';
  368. else
  369. {
  370. ver=SSL_version(scon);
  371. if (ver == TLS1_VERSION)
  372. ver='t';
  373. else if (ver == SSL3_VERSION)
  374. ver='3';
  375. else if (ver == SSL2_VERSION)
  376. ver='2';
  377. else
  378. ver='*';
  379. }
  380. fputc(ver,stdout);
  381. fflush(stdout);
  382. SSL_free( scon );
  383. scon=NULL;
  384. }
  385. totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
  386. i=(int)((long)time(NULL)-finishtime+maxTime);
  387. printf( "\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n", nConn, totalTime, ((double)nConn/totalTime),bytes_read);
  388. printf( "%d connections in %ld real seconds, %ld bytes read per connection\n",nConn,(long)time(NULL)-finishtime+maxTime,bytes_read/nConn);
  389. /* Now loop and time connections using the same session id over and over */
  390. next:
  391. if (!(perform & 2)) goto end;
  392. printf( "\n\nNow timing with session id reuse.\n" );
  393. /* Get an SSL object so we can reuse the session id */
  394. if( (scon = doConnection( NULL )) == NULL )
  395. {
  396. fprintf( stderr, "Unable to get connection\n" );
  397. goto end;
  398. }
  399. if (s_www_path != NULL)
  400. {
  401. BIO_snprintf(buf,sizeof buf,"GET %s HTTP/1.0\r\n\r\n",s_www_path);
  402. SSL_write(scon,buf,strlen(buf));
  403. while (SSL_read(scon,buf,sizeof(buf)) > 0)
  404. ;
  405. }
  406. #ifdef NO_SHUTDOWN
  407. SSL_set_shutdown(scon,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
  408. #else
  409. SSL_shutdown(scon);
  410. #endif
  411. SHUTDOWN2(SSL_get_fd(scon));
  412. nConn = 0;
  413. totalTime = 0.0;
  414. finishtime=(long)time(NULL)+maxTime;
  415. printf( "starting\n" );
  416. bytes_read=0;
  417. tm_Time_F(START);
  418. for (;;)
  419. {
  420. if (finishtime < (long)time(NULL)) break;
  421. #ifdef WIN32_STUFF
  422. if( flushWinMsgs(0) == -1 )
  423. goto end;
  424. if( waitingToDie || exitNow ) /* we're dead */
  425. goto end;
  426. #endif
  427. if( (doConnection( scon )) == NULL )
  428. goto end;
  429. if (s_www_path)
  430. {
  431. BIO_snprintf(buf,sizeof buf,"GET %s HTTP/1.0\r\n\r\n",s_www_path);
  432. SSL_write(scon,buf,strlen(buf));
  433. while ((i=SSL_read(scon,buf,sizeof(buf))) > 0)
  434. bytes_read+=i;
  435. }
  436. #ifdef NO_SHUTDOWN
  437. SSL_set_shutdown(scon,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
  438. #else
  439. SSL_shutdown(scon);
  440. #endif
  441. SHUTDOWN2(SSL_get_fd(scon));
  442. nConn += 1;
  443. if (SSL_session_reused(scon))
  444. ver='r';
  445. else
  446. {
  447. ver=SSL_version(scon);
  448. if (ver == TLS1_VERSION)
  449. ver='t';
  450. else if (ver == SSL3_VERSION)
  451. ver='3';
  452. else if (ver == SSL2_VERSION)
  453. ver='2';
  454. else
  455. ver='*';
  456. }
  457. fputc(ver,stdout);
  458. fflush(stdout);
  459. }
  460. totalTime += tm_Time_F(STOP); /* Add the time for this iteration*/
  461. printf( "\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n", nConn, totalTime, ((double)nConn/totalTime),bytes_read);
  462. printf( "%d connections in %ld real seconds, %ld bytes read per connection\n",nConn,(long)time(NULL)-finishtime+maxTime,bytes_read/nConn);
  463. ret=0;
  464. end:
  465. if (scon != NULL) SSL_free(scon);
  466. if (tm_ctx != NULL)
  467. {
  468. SSL_CTX_free(tm_ctx);
  469. tm_ctx=NULL;
  470. }
  471. apps_shutdown();
  472. OPENSSL_EXIT(ret);
  473. }
  474. /***********************************************************************
  475. * doConnection - make a connection
  476. * Args:
  477. * scon = earlier ssl connection for session id, or NULL
  478. * Returns:
  479. * SSL * = the connection pointer.
  480. */
  481. static SSL *doConnection(SSL *scon)
  482. {
  483. BIO *conn;
  484. SSL *serverCon;
  485. int width, i;
  486. fd_set readfds;
  487. if ((conn=BIO_new(BIO_s_connect())) == NULL)
  488. return(NULL);
  489. /* BIO_set_conn_port(conn,port);*/
  490. BIO_set_conn_hostname(conn,host);
  491. if (scon == NULL)
  492. serverCon=SSL_new(tm_ctx);
  493. else
  494. {
  495. serverCon=scon;
  496. SSL_set_connect_state(serverCon);
  497. }
  498. SSL_set_bio(serverCon,conn,conn);
  499. #if 0
  500. if( scon != NULL )
  501. SSL_set_session(serverCon,SSL_get_session(scon));
  502. #endif
  503. /* ok, lets connect */
  504. for(;;) {
  505. i=SSL_connect(serverCon);
  506. if (BIO_sock_should_retry(i))
  507. {
  508. BIO_printf(bio_err,"DELAY\n");
  509. i=SSL_get_fd(serverCon);
  510. width=i+1;
  511. FD_ZERO(&readfds);
  512. openssl_fdset(i,&readfds);
  513. /* Note: under VMS with SOCKETSHR the 2nd parameter
  514. * is currently of type (int *) whereas under other
  515. * systems it is (void *) if you don't have a cast it
  516. * will choke the compiler: if you do have a cast then
  517. * you can either go for (int *) or (void *).
  518. */
  519. select(width,(void *)&readfds,NULL,NULL,NULL);
  520. continue;
  521. }
  522. break;
  523. }
  524. if(i <= 0)
  525. {
  526. BIO_printf(bio_err,"ERROR\n");
  527. if (verify_error != X509_V_OK)
  528. BIO_printf(bio_err,"verify error:%s\n",
  529. X509_verify_cert_error_string(verify_error));
  530. else
  531. ERR_print_errors(bio_err);
  532. if (scon == NULL)
  533. SSL_free(serverCon);
  534. return NULL;
  535. }
  536. return serverCon;
  537. }