2
0

s_time.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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. if (maxTime <= 0) {
  270. BIO_printf(bio_err, "time must be > 0\n");
  271. badop = 1;
  272. }
  273. } else {
  274. BIO_printf(bio_err, "unknown option %s\n", *argv);
  275. badop = 1;
  276. break;
  277. }
  278. argc--;
  279. argv++;
  280. }
  281. if (perform == 0)
  282. perform = 3;
  283. if (badop) {
  284. bad:
  285. s_time_usage();
  286. return -1;
  287. }
  288. return 0; /* Valid args */
  289. }
  290. /***********************************************************************
  291. * TIME - time functions
  292. */
  293. #define START 0
  294. #define STOP 1
  295. static double tm_Time_F(int s)
  296. {
  297. return app_tminterval(s, 1);
  298. }
  299. /***********************************************************************
  300. * MAIN - main processing area for client
  301. * real name depends on MONOLITH
  302. */
  303. int MAIN(int, char **);
  304. int MAIN(int argc, char **argv)
  305. {
  306. double totalTime = 0.0;
  307. int nConn = 0;
  308. SSL *scon = NULL;
  309. long finishtime = 0;
  310. int ret = 1, i;
  311. MS_STATIC char buf[1024 * 8];
  312. int ver;
  313. apps_startup();
  314. s_time_init();
  315. if (bio_err == NULL)
  316. bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
  317. s_time_meth = SSLv23_client_method();
  318. /* parse the command line arguments */
  319. if (parseArgs(argc, argv) < 0)
  320. goto end;
  321. OpenSSL_add_ssl_algorithms();
  322. if ((tm_ctx = SSL_CTX_new(s_time_meth)) == NULL)
  323. return (1);
  324. SSL_CTX_set_quiet_shutdown(tm_ctx, 1);
  325. if (st_bugs)
  326. SSL_CTX_set_options(tm_ctx, SSL_OP_ALL);
  327. SSL_CTX_set_cipher_list(tm_ctx, tm_cipher);
  328. if (!set_cert_stuff(tm_ctx, t_cert_file, t_key_file))
  329. goto end;
  330. SSL_load_error_strings();
  331. if ((!SSL_CTX_load_verify_locations(tm_ctx, CAfile, CApath)) ||
  332. (!SSL_CTX_set_default_verify_paths(tm_ctx))) {
  333. /*
  334. * BIO_printf(bio_err,"error setting default verify locations\n");
  335. */
  336. ERR_print_errors(bio_err);
  337. /* goto end; */
  338. }
  339. if (tm_cipher == NULL)
  340. tm_cipher = getenv("SSL_CIPHER");
  341. if (tm_cipher == NULL) {
  342. fprintf(stderr, "No CIPHER specified\n");
  343. }
  344. if (!(perform & 1))
  345. goto next;
  346. printf("Collecting connection statistics for %d seconds\n", maxTime);
  347. /* Loop and time how long it takes to make connections */
  348. bytes_read = 0;
  349. finishtime = (long)time(NULL) + maxTime;
  350. tm_Time_F(START);
  351. for (;;) {
  352. if (finishtime < (long)time(NULL))
  353. break;
  354. #ifdef WIN32_STUFF
  355. if (flushWinMsgs(0) == -1)
  356. goto end;
  357. if (waitingToDie || exitNow) /* we're dead */
  358. goto end;
  359. #endif
  360. if ((scon = doConnection(NULL)) == NULL)
  361. goto end;
  362. if (s_www_path != NULL) {
  363. BIO_snprintf(buf, sizeof(buf), "GET %s HTTP/1.0\r\n\r\n",
  364. s_www_path);
  365. SSL_write(scon, buf, strlen(buf));
  366. while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
  367. bytes_read += i;
  368. }
  369. #ifdef NO_SHUTDOWN
  370. SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
  371. #else
  372. SSL_shutdown(scon);
  373. #endif
  374. SHUTDOWN2(SSL_get_fd(scon));
  375. nConn += 1;
  376. if (SSL_session_reused(scon))
  377. ver = 'r';
  378. else {
  379. ver = SSL_version(scon);
  380. if (ver == TLS1_VERSION)
  381. ver = 't';
  382. else if (ver == SSL3_VERSION)
  383. ver = '3';
  384. else if (ver == SSL2_VERSION)
  385. ver = '2';
  386. else
  387. ver = '*';
  388. }
  389. fputc(ver, stdout);
  390. fflush(stdout);
  391. SSL_free(scon);
  392. scon = NULL;
  393. }
  394. totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
  395. i = (int)((long)time(NULL) - finishtime + maxTime);
  396. printf
  397. ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
  398. nConn, totalTime, ((double)nConn / totalTime), bytes_read);
  399. printf
  400. ("%d connections in %ld real seconds, %ld bytes read per connection\n",
  401. nConn, (long)time(NULL) - finishtime + maxTime, bytes_read / nConn);
  402. /*
  403. * Now loop and time connections using the same session id over and over
  404. */
  405. next:
  406. if (!(perform & 2))
  407. goto end;
  408. printf("\n\nNow timing with session id reuse.\n");
  409. /* Get an SSL object so we can reuse the session id */
  410. if ((scon = doConnection(NULL)) == NULL) {
  411. fprintf(stderr, "Unable to get connection\n");
  412. goto end;
  413. }
  414. if (s_www_path != NULL) {
  415. BIO_snprintf(buf, sizeof(buf), "GET %s HTTP/1.0\r\n\r\n", s_www_path);
  416. SSL_write(scon, buf, strlen(buf));
  417. while (SSL_read(scon, buf, sizeof(buf)) > 0) ;
  418. }
  419. #ifdef NO_SHUTDOWN
  420. SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
  421. #else
  422. SSL_shutdown(scon);
  423. #endif
  424. SHUTDOWN2(SSL_get_fd(scon));
  425. nConn = 0;
  426. totalTime = 0.0;
  427. finishtime = (long)time(NULL) + maxTime;
  428. printf("starting\n");
  429. bytes_read = 0;
  430. tm_Time_F(START);
  431. for (;;) {
  432. if (finishtime < (long)time(NULL))
  433. break;
  434. #ifdef WIN32_STUFF
  435. if (flushWinMsgs(0) == -1)
  436. goto end;
  437. if (waitingToDie || exitNow) /* we're dead */
  438. goto end;
  439. #endif
  440. if ((doConnection(scon)) == NULL)
  441. goto end;
  442. if (s_www_path) {
  443. BIO_snprintf(buf, sizeof(buf), "GET %s HTTP/1.0\r\n\r\n",
  444. s_www_path);
  445. SSL_write(scon, buf, strlen(buf));
  446. while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
  447. bytes_read += i;
  448. }
  449. #ifdef NO_SHUTDOWN
  450. SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
  451. #else
  452. SSL_shutdown(scon);
  453. #endif
  454. SHUTDOWN2(SSL_get_fd(scon));
  455. nConn += 1;
  456. if (SSL_session_reused(scon))
  457. ver = 'r';
  458. else {
  459. ver = SSL_version(scon);
  460. if (ver == TLS1_VERSION)
  461. ver = 't';
  462. else if (ver == SSL3_VERSION)
  463. ver = '3';
  464. else if (ver == SSL2_VERSION)
  465. ver = '2';
  466. else
  467. ver = '*';
  468. }
  469. fputc(ver, stdout);
  470. fflush(stdout);
  471. }
  472. totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
  473. printf
  474. ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
  475. nConn, totalTime, ((double)nConn / totalTime), bytes_read);
  476. printf
  477. ("%d connections in %ld real seconds, %ld bytes read per connection\n",
  478. nConn, (long)time(NULL) - finishtime + maxTime,
  479. bytes_read / (nConn?nConn:1));
  480. ret = 0;
  481. end:
  482. if (scon != NULL)
  483. SSL_free(scon);
  484. if (tm_ctx != NULL) {
  485. SSL_CTX_free(tm_ctx);
  486. tm_ctx = NULL;
  487. }
  488. apps_shutdown();
  489. OPENSSL_EXIT(ret);
  490. }
  491. /*-
  492. * doConnection - make a connection
  493. * Args:
  494. * scon = earlier ssl connection for session id, or NULL
  495. * Returns:
  496. * SSL * = the connection pointer.
  497. */
  498. static SSL *doConnection(SSL *scon)
  499. {
  500. BIO *conn;
  501. SSL *serverCon;
  502. int width, i;
  503. fd_set readfds;
  504. if ((conn = BIO_new(BIO_s_connect())) == NULL)
  505. return (NULL);
  506. /* BIO_set_conn_port(conn,port);*/
  507. BIO_set_conn_hostname(conn, host);
  508. if (scon == NULL)
  509. serverCon = SSL_new(tm_ctx);
  510. else {
  511. serverCon = scon;
  512. SSL_set_connect_state(serverCon);
  513. }
  514. SSL_set_bio(serverCon, conn, conn);
  515. #if 0
  516. if (scon != NULL)
  517. SSL_set_session(serverCon, SSL_get_session(scon));
  518. #endif
  519. /* ok, lets connect */
  520. for (;;) {
  521. i = SSL_connect(serverCon);
  522. if (BIO_sock_should_retry(i)) {
  523. BIO_printf(bio_err, "DELAY\n");
  524. i = SSL_get_fd(serverCon);
  525. width = i + 1;
  526. FD_ZERO(&readfds);
  527. openssl_fdset(i, &readfds);
  528. /*
  529. * Note: under VMS with SOCKETSHR the 2nd parameter is currently
  530. * of type (int *) whereas under other systems it is (void *) if
  531. * you don't have a cast it will choke the compiler: if you do
  532. * have a cast then you can either go for (int *) or (void *).
  533. */
  534. select(width, (void *)&readfds, NULL, NULL, NULL);
  535. continue;
  536. }
  537. break;
  538. }
  539. if (i <= 0) {
  540. BIO_printf(bio_err, "ERROR\n");
  541. if (verify_error != X509_V_OK)
  542. BIO_printf(bio_err, "verify error:%s\n",
  543. X509_verify_cert_error_string(verify_error));
  544. else
  545. ERR_print_errors(bio_err);
  546. if (scon == NULL)
  547. SSL_free(serverCon);
  548. return NULL;
  549. }
  550. return serverCon;
  551. }