s_time.c 14 KB

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