s_time.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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. /* no default cert. */
  89. /*
  90. * #define TEST_CERT "client.pem"
  91. */
  92. #undef BUFSIZZ
  93. #define BUFSIZZ 1024*10
  94. #define MYBUFSIZ 1024*8
  95. #undef min
  96. #undef max
  97. #define min(a,b) (((a) < (b)) ? (a) : (b))
  98. #define max(a,b) (((a) > (b)) ? (a) : (b))
  99. #undef SECONDS
  100. #define SECONDS 30
  101. extern int verify_depth;
  102. extern int verify_error;
  103. static void s_time_usage(void);
  104. static int parseArgs(int argc, char **argv);
  105. static SSL *doConnection(SSL *scon);
  106. static void s_time_init(void);
  107. /***********************************************************************
  108. * Static data declarations
  109. */
  110. /* static char *port=PORT_STR;*/
  111. static char *host = SSL_CONNECT_NAME;
  112. static char *t_cert_file = NULL;
  113. static char *t_key_file = NULL;
  114. static char *CApath = NULL;
  115. static char *CAfile = NULL;
  116. static char *tm_cipher = NULL;
  117. static int tm_verify = SSL_VERIFY_NONE;
  118. static int maxTime = SECONDS;
  119. static SSL_CTX *tm_ctx = NULL;
  120. static const SSL_METHOD *s_time_meth = NULL;
  121. static char *s_www_path = NULL;
  122. static long bytes_read = 0;
  123. static int st_bugs = 0;
  124. static int perform = 0;
  125. #ifdef FIONBIO
  126. static int t_nbio = 0;
  127. #endif
  128. #ifdef OPENSSL_SYS_WIN32
  129. static int exitNow = 0; /* Set when it's time to exit main */
  130. #endif
  131. static void s_time_init(void)
  132. {
  133. host = SSL_CONNECT_NAME;
  134. t_cert_file = NULL;
  135. t_key_file = NULL;
  136. CApath = NULL;
  137. CAfile = NULL;
  138. tm_cipher = NULL;
  139. tm_verify = SSL_VERIFY_NONE;
  140. maxTime = SECONDS;
  141. tm_ctx = NULL;
  142. s_time_meth = NULL;
  143. s_www_path = NULL;
  144. bytes_read = 0;
  145. st_bugs = 0;
  146. perform = 0;
  147. #ifdef FIONBIO
  148. t_nbio = 0;
  149. #endif
  150. #ifdef OPENSSL_SYS_WIN32
  151. exitNow = 0; /* Set when it's time to exit main */
  152. #endif
  153. }
  154. /***********************************************************************
  155. * usage - display usage message
  156. */
  157. static void s_time_usage(void)
  158. {
  159. static char umsg[] = "\
  160. -time arg - max number of seconds to collect data, default %d\n\
  161. -verify arg - turn on peer certificate verification, arg == depth\n\
  162. -cert arg - certificate file to use, PEM format assumed\n\
  163. -key arg - RSA file to use, PEM format assumed, key is in cert file\n\
  164. file if not specified by this option\n\
  165. -CApath arg - PEM format directory of CA's\n\
  166. -CAfile arg - PEM format file of CA's\n\
  167. -cipher - preferred cipher to use, play with 'openssl ciphers'\n\n";
  168. printf("usage: s_time <args>\n\n");
  169. printf("-connect host:port - host:port to connect to (default is %s)\n",
  170. SSL_CONNECT_NAME);
  171. #ifdef FIONBIO
  172. printf("-nbio - Run with non-blocking IO\n");
  173. printf("-ssl2 - Just use SSLv2\n");
  174. printf("-ssl3 - Just use SSLv3\n");
  175. printf("-bugs - Turn on SSL bug compatibility\n");
  176. printf("-new - Just time new connections\n");
  177. printf("-reuse - Just time connection reuse\n");
  178. printf("-www page - Retrieve 'page' from the site\n");
  179. #endif
  180. printf(umsg, SECONDS);
  181. }
  182. /***********************************************************************
  183. * parseArgs - Parse command line arguments and initialize data
  184. *
  185. * Returns 0 if ok, -1 on bad args
  186. */
  187. static int parseArgs(int argc, char **argv)
  188. {
  189. int badop = 0;
  190. verify_depth = 0;
  191. verify_error = X509_V_OK;
  192. argc--;
  193. argv++;
  194. while (argc >= 1) {
  195. if (strcmp(*argv, "-connect") == 0) {
  196. if (--argc < 1)
  197. goto bad;
  198. host = *(++argv);
  199. }
  200. #if 0
  201. else if (strcmp(*argv, "-host") == 0) {
  202. if (--argc < 1)
  203. goto bad;
  204. host = *(++argv);
  205. } else if (strcmp(*argv, "-port") == 0) {
  206. if (--argc < 1)
  207. goto bad;
  208. port = *(++argv);
  209. }
  210. #endif
  211. else if (strcmp(*argv, "-reuse") == 0)
  212. perform = 2;
  213. else if (strcmp(*argv, "-new") == 0)
  214. perform = 1;
  215. else if (strcmp(*argv, "-verify") == 0) {
  216. tm_verify = SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE;
  217. if (--argc < 1)
  218. goto bad;
  219. verify_depth = atoi(*(++argv));
  220. BIO_printf(bio_err, "verify depth is %d\n", verify_depth);
  221. } else if (strcmp(*argv, "-cert") == 0) {
  222. if (--argc < 1)
  223. goto bad;
  224. t_cert_file = *(++argv);
  225. } else if (strcmp(*argv, "-key") == 0) {
  226. if (--argc < 1)
  227. goto bad;
  228. t_key_file = *(++argv);
  229. } else if (strcmp(*argv, "-CApath") == 0) {
  230. if (--argc < 1)
  231. goto bad;
  232. CApath = *(++argv);
  233. } else if (strcmp(*argv, "-CAfile") == 0) {
  234. if (--argc < 1)
  235. goto bad;
  236. CAfile = *(++argv);
  237. } else if (strcmp(*argv, "-cipher") == 0) {
  238. if (--argc < 1)
  239. goto bad;
  240. tm_cipher = *(++argv);
  241. }
  242. #ifdef FIONBIO
  243. else if (strcmp(*argv, "-nbio") == 0) {
  244. t_nbio = 1;
  245. }
  246. #endif
  247. else if (strcmp(*argv, "-www") == 0) {
  248. if (--argc < 1)
  249. goto bad;
  250. s_www_path = *(++argv);
  251. if (strlen(s_www_path) > MYBUFSIZ - 100) {
  252. BIO_printf(bio_err, "-www option too long\n");
  253. badop = 1;
  254. }
  255. } else if (strcmp(*argv, "-bugs") == 0)
  256. st_bugs = 1;
  257. #ifndef OPENSSL_NO_SSL2
  258. else if (strcmp(*argv, "-ssl2") == 0)
  259. s_time_meth = SSLv2_client_method();
  260. #endif
  261. #ifndef OPENSSL_NO_SSL3
  262. else if (strcmp(*argv, "-ssl3") == 0)
  263. s_time_meth = SSLv3_client_method();
  264. #endif
  265. else if (strcmp(*argv, "-time") == 0) {
  266. if (--argc < 1)
  267. goto bad;
  268. maxTime = atoi(*(++argv));
  269. } else {
  270. BIO_printf(bio_err, "unknown option %s\n", *argv);
  271. badop = 1;
  272. break;
  273. }
  274. argc--;
  275. argv++;
  276. }
  277. if (perform == 0)
  278. perform = 3;
  279. if (badop) {
  280. bad:
  281. s_time_usage();
  282. return -1;
  283. }
  284. return 0; /* Valid args */
  285. }
  286. /***********************************************************************
  287. * TIME - time functions
  288. */
  289. #define START 0
  290. #define STOP 1
  291. static double tm_Time_F(int s)
  292. {
  293. return app_tminterval(s, 1);
  294. }
  295. /***********************************************************************
  296. * MAIN - main processing area for client
  297. * real name depends on MONOLITH
  298. */
  299. int MAIN(int, char **);
  300. int MAIN(int argc, char **argv)
  301. {
  302. double totalTime = 0.0;
  303. int nConn = 0;
  304. SSL *scon = NULL;
  305. long finishtime = 0;
  306. int ret = 1, i;
  307. MS_STATIC char buf[1024 * 8];
  308. int ver;
  309. apps_startup();
  310. s_time_init();
  311. if (bio_err == NULL)
  312. bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
  313. #if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
  314. s_time_meth = SSLv23_client_method();
  315. #elif !defined(OPENSSL_NO_SSL3)
  316. s_time_meth = SSLv3_client_method();
  317. #elif !defined(OPENSSL_NO_SSL2)
  318. s_time_meth = SSLv2_client_method();
  319. #endif
  320. /* parse the command line arguments */
  321. if (parseArgs(argc, argv) < 0)
  322. goto end;
  323. OpenSSL_add_ssl_algorithms();
  324. if ((tm_ctx = SSL_CTX_new(s_time_meth)) == NULL)
  325. return (1);
  326. SSL_CTX_set_quiet_shutdown(tm_ctx, 1);
  327. if (st_bugs)
  328. SSL_CTX_set_options(tm_ctx, SSL_OP_ALL);
  329. SSL_CTX_set_cipher_list(tm_ctx, tm_cipher);
  330. if (!set_cert_stuff(tm_ctx, t_cert_file, t_key_file))
  331. goto end;
  332. SSL_load_error_strings();
  333. if ((!SSL_CTX_load_verify_locations(tm_ctx, CAfile, CApath)) ||
  334. (!SSL_CTX_set_default_verify_paths(tm_ctx))) {
  335. /*
  336. * BIO_printf(bio_err,"error setting default verify locations\n");
  337. */
  338. ERR_print_errors(bio_err);
  339. /* goto end; */
  340. }
  341. if (tm_cipher == NULL)
  342. tm_cipher = getenv("SSL_CIPHER");
  343. if (tm_cipher == NULL) {
  344. fprintf(stderr, "No CIPHER specified\n");
  345. }
  346. if (!(perform & 1))
  347. goto next;
  348. printf("Collecting connection statistics for %d seconds\n", maxTime);
  349. /* Loop and time how long it takes to make connections */
  350. bytes_read = 0;
  351. finishtime = (long)time(NULL) + maxTime;
  352. tm_Time_F(START);
  353. for (;;) {
  354. if (finishtime < (long)time(NULL))
  355. break;
  356. #ifdef WIN32_STUFF
  357. if (flushWinMsgs(0) == -1)
  358. goto end;
  359. if (waitingToDie || exitNow) /* we're dead */
  360. goto end;
  361. #endif
  362. if ((scon = doConnection(NULL)) == NULL)
  363. goto end;
  364. if (s_www_path != NULL) {
  365. BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n",
  366. s_www_path);
  367. SSL_write(scon, buf, strlen(buf));
  368. while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
  369. bytes_read += i;
  370. }
  371. #ifdef NO_SHUTDOWN
  372. SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
  373. #else
  374. SSL_shutdown(scon);
  375. #endif
  376. SHUTDOWN2(SSL_get_fd(scon));
  377. nConn += 1;
  378. if (SSL_session_reused(scon))
  379. ver = 'r';
  380. else {
  381. ver = SSL_version(scon);
  382. if (ver == TLS1_VERSION)
  383. ver = 't';
  384. else if (ver == SSL3_VERSION)
  385. ver = '3';
  386. else if (ver == SSL2_VERSION)
  387. ver = '2';
  388. else
  389. ver = '*';
  390. }
  391. fputc(ver, stdout);
  392. fflush(stdout);
  393. SSL_free(scon);
  394. scon = NULL;
  395. }
  396. totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
  397. i = (int)((long)time(NULL) - finishtime + maxTime);
  398. printf
  399. ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
  400. nConn, totalTime, ((double)nConn / totalTime), bytes_read);
  401. printf
  402. ("%d connections in %ld real seconds, %ld bytes read per connection\n",
  403. nConn, (long)time(NULL) - finishtime + maxTime, bytes_read / nConn);
  404. /*
  405. * Now loop and time connections using the same session id over and over
  406. */
  407. next:
  408. if (!(perform & 2))
  409. goto end;
  410. printf("\n\nNow timing with session id reuse.\n");
  411. /* Get an SSL object so we can reuse the session id */
  412. if ((scon = doConnection(NULL)) == NULL) {
  413. fprintf(stderr, "Unable to get connection\n");
  414. goto end;
  415. }
  416. if (s_www_path != NULL) {
  417. BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n", s_www_path);
  418. SSL_write(scon, buf, strlen(buf));
  419. while (SSL_read(scon, buf, sizeof(buf)) > 0) ;
  420. }
  421. #ifdef NO_SHUTDOWN
  422. SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
  423. #else
  424. SSL_shutdown(scon);
  425. #endif
  426. SHUTDOWN2(SSL_get_fd(scon));
  427. nConn = 0;
  428. totalTime = 0.0;
  429. finishtime = (long)time(NULL) + maxTime;
  430. printf("starting\n");
  431. bytes_read = 0;
  432. tm_Time_F(START);
  433. for (;;) {
  434. if (finishtime < (long)time(NULL))
  435. break;
  436. #ifdef WIN32_STUFF
  437. if (flushWinMsgs(0) == -1)
  438. goto end;
  439. if (waitingToDie || exitNow) /* we're dead */
  440. goto end;
  441. #endif
  442. if ((doConnection(scon)) == NULL)
  443. goto end;
  444. if (s_www_path) {
  445. BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n",
  446. s_www_path);
  447. SSL_write(scon, buf, strlen(buf));
  448. while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
  449. bytes_read += i;
  450. }
  451. #ifdef NO_SHUTDOWN
  452. SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
  453. #else
  454. SSL_shutdown(scon);
  455. #endif
  456. SHUTDOWN2(SSL_get_fd(scon));
  457. nConn += 1;
  458. if (SSL_session_reused(scon))
  459. ver = 'r';
  460. else {
  461. ver = SSL_version(scon);
  462. if (ver == TLS1_VERSION)
  463. ver = 't';
  464. else if (ver == SSL3_VERSION)
  465. ver = '3';
  466. else if (ver == SSL2_VERSION)
  467. ver = '2';
  468. else
  469. ver = '*';
  470. }
  471. fputc(ver, stdout);
  472. fflush(stdout);
  473. }
  474. totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
  475. printf
  476. ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
  477. nConn, totalTime, ((double)nConn / totalTime), bytes_read);
  478. printf
  479. ("%d connections in %ld real seconds, %ld bytes read per connection\n",
  480. nConn, (long)time(NULL) - finishtime + maxTime, bytes_read / nConn);
  481. ret = 0;
  482. end:
  483. if (scon != NULL)
  484. SSL_free(scon);
  485. if (tm_ctx != NULL) {
  486. SSL_CTX_free(tm_ctx);
  487. tm_ctx = NULL;
  488. }
  489. apps_shutdown();
  490. OPENSSL_EXIT(ret);
  491. }
  492. /*-
  493. * doConnection - make a connection
  494. * Args:
  495. * scon = earlier ssl connection for session id, or NULL
  496. * Returns:
  497. * SSL * = the connection pointer.
  498. */
  499. static SSL *doConnection(SSL *scon)
  500. {
  501. BIO *conn;
  502. SSL *serverCon;
  503. int width, i;
  504. fd_set readfds;
  505. if ((conn = BIO_new(BIO_s_connect())) == NULL)
  506. return (NULL);
  507. /* BIO_set_conn_port(conn,port);*/
  508. BIO_set_conn_hostname(conn, host);
  509. if (scon == NULL)
  510. serverCon = SSL_new(tm_ctx);
  511. else {
  512. serverCon = scon;
  513. SSL_set_connect_state(serverCon);
  514. }
  515. SSL_set_bio(serverCon, conn, conn);
  516. #if 0
  517. if (scon != NULL)
  518. SSL_set_session(serverCon, SSL_get_session(scon));
  519. #endif
  520. /* ok, lets connect */
  521. for (;;) {
  522. i = SSL_connect(serverCon);
  523. if (BIO_sock_should_retry(i)) {
  524. BIO_printf(bio_err, "DELAY\n");
  525. i = SSL_get_fd(serverCon);
  526. width = i + 1;
  527. FD_ZERO(&readfds);
  528. openssl_fdset(i, &readfds);
  529. /*
  530. * Note: under VMS with SOCKETSHR the 2nd parameter is currently
  531. * of type (int *) whereas under other systems it is (void *) if
  532. * you don't have a cast it will choke the compiler: if you do
  533. * have a cast then you can either go for (int *) or (void *).
  534. */
  535. select(width, (void *)&readfds, NULL, NULL, NULL);
  536. continue;
  537. }
  538. break;
  539. }
  540. if (i <= 0) {
  541. BIO_printf(bio_err, "ERROR\n");
  542. if (verify_error != X509_V_OK)
  543. BIO_printf(bio_err, "verify error:%s\n",
  544. X509_verify_cert_error_string(verify_error));
  545. else
  546. ERR_print_errors(bio_err);
  547. if (scon == NULL)
  548. SSL_free(serverCon);
  549. return NULL;
  550. }
  551. return serverCon;
  552. }