2
0

s_time.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * Copyright 1995-2021 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_COMMON,
  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, fd;
  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. verify_args.depth = opt_int_arg();
  140. BIO_printf(bio_err, "%s: verify depth is %d\n",
  141. prog, verify_args.depth);
  142. break;
  143. case OPT_CERT:
  144. certfile = opt_arg();
  145. break;
  146. case OPT_NAMEOPT:
  147. if (!set_nameopt(opt_arg()))
  148. goto end;
  149. break;
  150. case OPT_KEY:
  151. keyfile = opt_arg();
  152. break;
  153. case OPT_CAPATH:
  154. CApath = opt_arg();
  155. break;
  156. case OPT_CAFILE:
  157. CAfile = opt_arg();
  158. break;
  159. case OPT_NOCAPATH:
  160. noCApath = 1;
  161. break;
  162. case OPT_NOCAFILE:
  163. noCAfile = 1;
  164. break;
  165. case OPT_CASTORE:
  166. CAstore = opt_arg();
  167. break;
  168. case OPT_NOCASTORE:
  169. noCAstore = 1;
  170. break;
  171. case OPT_CIPHER:
  172. cipher = opt_arg();
  173. break;
  174. case OPT_CIPHERSUITES:
  175. ciphersuites = opt_arg();
  176. break;
  177. case OPT_BUGS:
  178. st_bugs = 1;
  179. break;
  180. case OPT_TIME:
  181. maxtime = opt_int_arg();
  182. break;
  183. case OPT_WWW:
  184. www_path = opt_arg();
  185. buf_size = strlen(www_path) + fmt_http_get_cmd_size;
  186. if (buf_size > sizeof(buf)) {
  187. BIO_printf(bio_err, "%s: -www option is too long\n", prog);
  188. goto end;
  189. }
  190. break;
  191. case OPT_SSL3:
  192. min_version = SSL3_VERSION;
  193. max_version = SSL3_VERSION;
  194. break;
  195. case OPT_TLS1:
  196. min_version = TLS1_VERSION;
  197. max_version = TLS1_VERSION;
  198. break;
  199. case OPT_TLS1_1:
  200. min_version = TLS1_1_VERSION;
  201. max_version = TLS1_1_VERSION;
  202. break;
  203. case OPT_TLS1_2:
  204. min_version = TLS1_2_VERSION;
  205. max_version = TLS1_2_VERSION;
  206. break;
  207. case OPT_TLS1_3:
  208. min_version = TLS1_3_VERSION;
  209. max_version = TLS1_3_VERSION;
  210. break;
  211. case OPT_PROV_CASES:
  212. if (!opt_provider(o))
  213. goto end;
  214. break;
  215. }
  216. }
  217. /* No extra arguments. */
  218. argc = opt_num_rest();
  219. if (argc != 0)
  220. goto opthelp;
  221. if (cipher == NULL)
  222. cipher = getenv("SSL_CIPHER");
  223. if ((ctx = SSL_CTX_new(meth)) == NULL)
  224. goto end;
  225. SSL_CTX_set_quiet_shutdown(ctx, 1);
  226. if (SSL_CTX_set_min_proto_version(ctx, min_version) == 0)
  227. goto end;
  228. if (SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
  229. goto end;
  230. if (st_bugs)
  231. SSL_CTX_set_options(ctx, SSL_OP_ALL);
  232. if (cipher != NULL && !SSL_CTX_set_cipher_list(ctx, cipher))
  233. goto end;
  234. if (ciphersuites != NULL && !SSL_CTX_set_ciphersuites(ctx, ciphersuites))
  235. goto end;
  236. if (!set_cert_stuff(ctx, certfile, keyfile))
  237. goto end;
  238. if (!ctx_set_verify_locations(ctx, CAfile, noCAfile, CApath, noCApath,
  239. CAstore, noCAstore)) {
  240. ERR_print_errors(bio_err);
  241. goto end;
  242. }
  243. if (!(perform & 1))
  244. goto next;
  245. printf("Collecting connection statistics for %d seconds\n", maxtime);
  246. /* Loop and time how long it takes to make connections */
  247. bytes_read = 0;
  248. finishtime = (long)time(NULL) + maxtime;
  249. tm_Time_F(START);
  250. for (;;) {
  251. if (finishtime < (long)time(NULL))
  252. break;
  253. if ((scon = doConnection(NULL, host, ctx)) == NULL)
  254. goto end;
  255. if (www_path != NULL) {
  256. buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd,
  257. www_path);
  258. if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
  259. goto end;
  260. while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
  261. bytes_read += i;
  262. }
  263. SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
  264. BIO_closesocket(SSL_get_fd(scon));
  265. nConn += 1;
  266. if (SSL_session_reused(scon)) {
  267. ver = 'r';
  268. } else {
  269. ver = SSL_version(scon);
  270. if (ver == TLS1_VERSION)
  271. ver = 't';
  272. else if (ver == SSL3_VERSION)
  273. ver = '3';
  274. else
  275. ver = '*';
  276. }
  277. fputc(ver, stdout);
  278. fflush(stdout);
  279. SSL_free(scon);
  280. scon = NULL;
  281. }
  282. totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
  283. i = (int)((long)time(NULL) - finishtime + maxtime);
  284. printf
  285. ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
  286. nConn, totalTime, ((double)nConn / totalTime), bytes_read);
  287. printf
  288. ("%d connections in %ld real seconds, %ld bytes read per connection\n",
  289. nConn, (long)time(NULL) - finishtime + maxtime,
  290. nConn > 0 ? bytes_read / nConn : 0l);
  291. /*
  292. * Now loop and time connections using the same session id over and over
  293. */
  294. next:
  295. if (!(perform & 2))
  296. goto end;
  297. printf("\n\nNow timing with session id reuse.\n");
  298. /* Get an SSL object so we can reuse the session id */
  299. if ((scon = doConnection(NULL, host, ctx)) == NULL) {
  300. BIO_printf(bio_err, "Unable to get connection\n");
  301. goto end;
  302. }
  303. if (www_path != NULL) {
  304. buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd, www_path);
  305. if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
  306. goto end;
  307. while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
  308. continue;
  309. }
  310. SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
  311. if ((fd = SSL_get_fd(scon)) >= 0)
  312. BIO_closesocket(fd);
  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. if ((fd = SSL_get_fd(scon)) >= 0)
  334. BIO_closesocket(fd);
  335. nConn += 1;
  336. if (SSL_session_reused(scon)) {
  337. ver = 'r';
  338. } else {
  339. ver = SSL_version(scon);
  340. if (ver == TLS1_VERSION)
  341. ver = 't';
  342. else if (ver == SSL3_VERSION)
  343. ver = '3';
  344. else
  345. ver = '*';
  346. }
  347. fputc(ver, stdout);
  348. fflush(stdout);
  349. }
  350. totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
  351. printf
  352. ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
  353. nConn, totalTime, ((double)nConn / totalTime), bytes_read);
  354. if (nConn > 0)
  355. printf
  356. ("%d connections in %ld real seconds, %ld bytes read per connection\n",
  357. nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
  358. else
  359. printf("0 connections in %ld real seconds\n",
  360. (long)time(NULL) - finishtime + maxtime);
  361. ret = 0;
  362. end:
  363. SSL_free(scon);
  364. SSL_CTX_free(ctx);
  365. return ret;
  366. }
  367. /*-
  368. * doConnection - make a connection
  369. */
  370. static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx)
  371. {
  372. BIO *conn;
  373. SSL *serverCon;
  374. int i;
  375. if ((conn = BIO_new(BIO_s_connect())) == NULL)
  376. return NULL;
  377. if (BIO_set_conn_hostname(conn, host) <= 0
  378. || BIO_set_conn_mode(conn, BIO_SOCK_NODELAY) <= 0) {
  379. BIO_free(conn);
  380. return NULL;
  381. }
  382. if (scon == NULL) {
  383. serverCon = SSL_new(ctx);
  384. if (serverCon == NULL) {
  385. BIO_free(conn);
  386. return NULL;
  387. }
  388. } else {
  389. serverCon = scon;
  390. SSL_set_connect_state(serverCon);
  391. }
  392. SSL_set_bio(serverCon, conn, conn);
  393. /* ok, lets connect */
  394. i = SSL_connect(serverCon);
  395. if (i <= 0) {
  396. BIO_printf(bio_err, "ERROR\n");
  397. if (verify_args.error != X509_V_OK)
  398. BIO_printf(bio_err, "verify error:%s\n",
  399. X509_verify_cert_error_string(verify_args.error));
  400. else
  401. ERR_print_errors(bio_err);
  402. if (scon == NULL)
  403. SSL_free(serverCon);
  404. return NULL;
  405. }
  406. #if defined(SOL_SOCKET) && defined(SO_LINGER)
  407. {
  408. struct linger no_linger;
  409. int fd;
  410. no_linger.l_onoff = 1;
  411. no_linger.l_linger = 0;
  412. fd = SSL_get_fd(serverCon);
  413. if (fd >= 0)
  414. (void)setsockopt(fd, SOL_SOCKET, SO_LINGER, (char*)&no_linger,
  415. sizeof(no_linger));
  416. }
  417. #endif
  418. return serverCon;
  419. }
  420. #endif /* OPENSSL_NO_SOCK */