s_time.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <openssl/opensslconf.h>
  13. #ifndef OPENSSL_NO_SOCK
  14. #include "apps.h"
  15. #include "progs.h"
  16. #include <openssl/x509.h>
  17. #include <openssl/ssl.h>
  18. #include <openssl/pem.h>
  19. #include "s_apps.h"
  20. #include <openssl/err.h>
  21. #include <internal/sockets.h>
  22. #if !defined(OPENSSL_SYS_MSDOS)
  23. # include OPENSSL_UNISTD
  24. #endif
  25. #define SSL_CONNECT_NAME "localhost:4433"
  26. #define SECONDS 30
  27. #define SECONDSSTR "30"
  28. static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx);
  29. /*
  30. * Define a HTTP get command globally.
  31. * Also define the size of the command, this is two bytes less than
  32. * the size of the string because the %s is replaced by the URL.
  33. */
  34. static const char fmt_http_get_cmd[] = "GET %s HTTP/1.0\r\n\r\n";
  35. static const size_t fmt_http_get_cmd_size = sizeof(fmt_http_get_cmd) - 2;
  36. typedef enum OPTION_choice {
  37. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  38. OPT_CONNECT, OPT_CIPHER, OPT_CIPHERSUITES, OPT_CERT, OPT_NAMEOPT, OPT_KEY,
  39. OPT_CAPATH, OPT_CAFILE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_NEW, OPT_REUSE,
  40. OPT_BUGS, OPT_VERIFY, OPT_TIME, OPT_SSL3,
  41. OPT_WWW
  42. } OPTION_CHOICE;
  43. const OPTIONS s_time_options[] = {
  44. {"help", OPT_HELP, '-', "Display this summary"},
  45. {"connect", OPT_CONNECT, 's',
  46. "Where to connect as post:port (default is " SSL_CONNECT_NAME ")"},
  47. {"cipher", OPT_CIPHER, 's', "TLSv1.2 and below cipher list to be used"},
  48. {"ciphersuites", OPT_CIPHERSUITES, 's',
  49. "Specify TLSv1.3 ciphersuites to be used"},
  50. {"cert", OPT_CERT, '<', "Cert file to use, PEM format assumed"},
  51. {"nameopt", OPT_NAMEOPT, 's', "Various certificate name options"},
  52. {"key", OPT_KEY, '<', "File with key, PEM; default is -cert file"},
  53. {"CApath", OPT_CAPATH, '/', "PEM format directory of CA's"},
  54. {"cafile", OPT_CAFILE, '<', "PEM format file of CA's"},
  55. {"no-CAfile", OPT_NOCAFILE, '-',
  56. "Do not load the default certificates file"},
  57. {"no-CApath", OPT_NOCAPATH, '-',
  58. "Do not load certificates from the default certificates directory"},
  59. {"new", OPT_NEW, '-', "Just time new connections"},
  60. {"reuse", OPT_REUSE, '-', "Just time connection reuse"},
  61. {"bugs", OPT_BUGS, '-', "Turn on SSL bug compatibility"},
  62. {"verify", OPT_VERIFY, 'p',
  63. "Turn on peer certificate verification, set depth"},
  64. {"time", OPT_TIME, 'p', "Seconds to collect data, default " SECONDSSTR},
  65. {"www", OPT_WWW, 's', "Fetch specified page from the site"},
  66. #ifndef OPENSSL_NO_SSL3
  67. {"ssl3", OPT_SSL3, '-', "Just use SSLv3"},
  68. #endif
  69. {NULL}
  70. };
  71. #define START 0
  72. #define STOP 1
  73. static double tm_Time_F(int s)
  74. {
  75. return app_tminterval(s, 1);
  76. }
  77. int s_time_main(int argc, char **argv)
  78. {
  79. char buf[1024 * 8];
  80. SSL *scon = NULL;
  81. SSL_CTX *ctx = NULL;
  82. const SSL_METHOD *meth = NULL;
  83. char *CApath = NULL, *CAfile = NULL, *cipher = NULL, *ciphersuites = NULL;
  84. char *www_path = NULL;
  85. char *host = SSL_CONNECT_NAME, *certfile = NULL, *keyfile = NULL, *prog;
  86. double totalTime = 0.0;
  87. int noCApath = 0, noCAfile = 0;
  88. int maxtime = SECONDS, nConn = 0, perform = 3, ret = 1, i, st_bugs = 0;
  89. long bytes_read = 0, finishtime = 0;
  90. OPTION_CHOICE o;
  91. int max_version = 0, ver, buf_len;
  92. size_t buf_size;
  93. meth = TLS_client_method();
  94. prog = opt_init(argc, argv, s_time_options);
  95. while ((o = opt_next()) != OPT_EOF) {
  96. switch (o) {
  97. case OPT_EOF:
  98. case OPT_ERR:
  99. opthelp:
  100. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  101. goto end;
  102. case OPT_HELP:
  103. opt_help(s_time_options);
  104. ret = 0;
  105. goto end;
  106. case OPT_CONNECT:
  107. host = opt_arg();
  108. break;
  109. case OPT_REUSE:
  110. perform = 2;
  111. break;
  112. case OPT_NEW:
  113. perform = 1;
  114. break;
  115. case OPT_VERIFY:
  116. if (!opt_int(opt_arg(), &verify_args.depth))
  117. goto opthelp;
  118. BIO_printf(bio_err, "%s: verify depth is %d\n",
  119. prog, verify_args.depth);
  120. break;
  121. case OPT_CERT:
  122. certfile = opt_arg();
  123. break;
  124. case OPT_NAMEOPT:
  125. if (!set_nameopt(opt_arg()))
  126. goto end;
  127. break;
  128. case OPT_KEY:
  129. keyfile = opt_arg();
  130. break;
  131. case OPT_CAPATH:
  132. CApath = opt_arg();
  133. break;
  134. case OPT_CAFILE:
  135. CAfile = opt_arg();
  136. break;
  137. case OPT_NOCAPATH:
  138. noCApath = 1;
  139. break;
  140. case OPT_NOCAFILE:
  141. noCAfile = 1;
  142. break;
  143. case OPT_CIPHER:
  144. cipher = opt_arg();
  145. break;
  146. case OPT_CIPHERSUITES:
  147. ciphersuites = opt_arg();
  148. break;
  149. case OPT_BUGS:
  150. st_bugs = 1;
  151. break;
  152. case OPT_TIME:
  153. if (!opt_int(opt_arg(), &maxtime))
  154. goto opthelp;
  155. break;
  156. case OPT_WWW:
  157. www_path = opt_arg();
  158. buf_size = strlen(www_path) + fmt_http_get_cmd_size;
  159. if (buf_size > sizeof(buf)) {
  160. BIO_printf(bio_err, "%s: -www option is too long\n", prog);
  161. goto end;
  162. }
  163. break;
  164. case OPT_SSL3:
  165. max_version = SSL3_VERSION;
  166. break;
  167. }
  168. }
  169. argc = opt_num_rest();
  170. if (argc != 0)
  171. goto opthelp;
  172. if (cipher == NULL)
  173. cipher = getenv("SSL_CIPHER");
  174. if ((ctx = SSL_CTX_new(meth)) == NULL)
  175. goto end;
  176. SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
  177. SSL_CTX_set_quiet_shutdown(ctx, 1);
  178. if (SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
  179. goto end;
  180. if (st_bugs)
  181. SSL_CTX_set_options(ctx, SSL_OP_ALL);
  182. if (cipher != NULL && !SSL_CTX_set_cipher_list(ctx, cipher))
  183. goto end;
  184. if (ciphersuites != NULL && !SSL_CTX_set_ciphersuites(ctx, ciphersuites))
  185. goto end;
  186. if (!set_cert_stuff(ctx, certfile, keyfile))
  187. goto end;
  188. if (!ctx_set_verify_locations(ctx, CAfile, CApath, noCAfile, noCApath)) {
  189. ERR_print_errors(bio_err);
  190. goto end;
  191. }
  192. if (!(perform & 1))
  193. goto next;
  194. printf("Collecting connection statistics for %d seconds\n", maxtime);
  195. /* Loop and time how long it takes to make connections */
  196. bytes_read = 0;
  197. finishtime = (long)time(NULL) + maxtime;
  198. tm_Time_F(START);
  199. for (;;) {
  200. if (finishtime < (long)time(NULL))
  201. break;
  202. if ((scon = doConnection(NULL, host, ctx)) == NULL)
  203. goto end;
  204. if (www_path != NULL) {
  205. buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd,
  206. www_path);
  207. if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
  208. goto end;
  209. while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
  210. bytes_read += i;
  211. }
  212. SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
  213. BIO_closesocket(SSL_get_fd(scon));
  214. nConn += 1;
  215. if (SSL_session_reused(scon)) {
  216. ver = 'r';
  217. } else {
  218. ver = SSL_version(scon);
  219. if (ver == TLS1_VERSION)
  220. ver = 't';
  221. else if (ver == SSL3_VERSION)
  222. ver = '3';
  223. else
  224. ver = '*';
  225. }
  226. fputc(ver, stdout);
  227. fflush(stdout);
  228. SSL_free(scon);
  229. scon = NULL;
  230. }
  231. totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
  232. i = (int)((long)time(NULL) - finishtime + maxtime);
  233. printf
  234. ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
  235. nConn, totalTime, ((double)nConn / totalTime), bytes_read);
  236. printf
  237. ("%d connections in %ld real seconds, %ld bytes read per connection\n",
  238. nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
  239. /*
  240. * Now loop and time connections using the same session id over and over
  241. */
  242. next:
  243. if (!(perform & 2))
  244. goto end;
  245. printf("\n\nNow timing with session id reuse.\n");
  246. /* Get an SSL object so we can reuse the session id */
  247. if ((scon = doConnection(NULL, host, ctx)) == NULL) {
  248. BIO_printf(bio_err, "Unable to get connection\n");
  249. goto end;
  250. }
  251. if (www_path != NULL) {
  252. buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd, www_path);
  253. if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
  254. goto end;
  255. while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
  256. continue;
  257. }
  258. SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
  259. BIO_closesocket(SSL_get_fd(scon));
  260. nConn = 0;
  261. totalTime = 0.0;
  262. finishtime = (long)time(NULL) + maxtime;
  263. printf("starting\n");
  264. bytes_read = 0;
  265. tm_Time_F(START);
  266. for (;;) {
  267. if (finishtime < (long)time(NULL))
  268. break;
  269. if ((doConnection(scon, host, ctx)) == NULL)
  270. goto end;
  271. if (www_path != NULL) {
  272. buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd,
  273. www_path);
  274. if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
  275. goto end;
  276. while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
  277. bytes_read += i;
  278. }
  279. SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
  280. BIO_closesocket(SSL_get_fd(scon));
  281. nConn += 1;
  282. if (SSL_session_reused(scon)) {
  283. ver = 'r';
  284. } else {
  285. ver = SSL_version(scon);
  286. if (ver == TLS1_VERSION)
  287. ver = 't';
  288. else if (ver == SSL3_VERSION)
  289. ver = '3';
  290. else
  291. ver = '*';
  292. }
  293. fputc(ver, stdout);
  294. fflush(stdout);
  295. }
  296. totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
  297. printf
  298. ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
  299. nConn, totalTime, ((double)nConn / totalTime), bytes_read);
  300. printf
  301. ("%d connections in %ld real seconds, %ld bytes read per connection\n",
  302. nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
  303. ret = 0;
  304. end:
  305. SSL_free(scon);
  306. SSL_CTX_free(ctx);
  307. return ret;
  308. }
  309. /*-
  310. * doConnection - make a connection
  311. */
  312. static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx)
  313. {
  314. BIO *conn;
  315. SSL *serverCon;
  316. int i;
  317. if ((conn = BIO_new(BIO_s_connect())) == NULL)
  318. return NULL;
  319. BIO_set_conn_hostname(conn, host);
  320. BIO_set_conn_mode(conn, BIO_SOCK_NODELAY);
  321. if (scon == NULL)
  322. serverCon = SSL_new(ctx);
  323. else {
  324. serverCon = scon;
  325. SSL_set_connect_state(serverCon);
  326. }
  327. SSL_set_bio(serverCon, conn, conn);
  328. /* ok, lets connect */
  329. i = SSL_connect(serverCon);
  330. if (i <= 0) {
  331. BIO_printf(bio_err, "ERROR\n");
  332. if (verify_args.error != X509_V_OK)
  333. BIO_printf(bio_err, "verify error:%s\n",
  334. X509_verify_cert_error_string(verify_args.error));
  335. else
  336. ERR_print_errors(bio_err);
  337. if (scon == NULL)
  338. SSL_free(serverCon);
  339. return NULL;
  340. }
  341. #if defined(SOL_SOCKET) && defined(SO_LINGER)
  342. {
  343. struct linger no_linger;
  344. int fd;
  345. no_linger.l_onoff = 1;
  346. no_linger.l_linger = 0;
  347. fd = SSL_get_fd(serverCon);
  348. if (fd >= 0)
  349. (void)setsockopt(fd, SOL_SOCKET, SO_LINGER, (char*)&no_linger,
  350. sizeof(no_linger));
  351. }
  352. #endif
  353. return serverCon;
  354. }
  355. #endif /* OPENSSL_NO_SOCK */