2
0

s_time.c 12 KB

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