s_time.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. /* No extra arguments. */
  220. argc = opt_num_rest();
  221. if (argc != 0)
  222. goto opthelp;
  223. if (cipher == NULL)
  224. cipher = getenv("SSL_CIPHER");
  225. if ((ctx = SSL_CTX_new(meth)) == NULL)
  226. goto end;
  227. SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
  228. SSL_CTX_set_quiet_shutdown(ctx, 1);
  229. if (SSL_CTX_set_min_proto_version(ctx, min_version) == 0)
  230. goto end;
  231. if (SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
  232. goto end;
  233. if (st_bugs)
  234. SSL_CTX_set_options(ctx, SSL_OP_ALL);
  235. if (cipher != NULL && !SSL_CTX_set_cipher_list(ctx, cipher))
  236. goto end;
  237. if (ciphersuites != NULL && !SSL_CTX_set_ciphersuites(ctx, ciphersuites))
  238. goto end;
  239. if (!set_cert_stuff(ctx, certfile, keyfile))
  240. goto end;
  241. if (!ctx_set_verify_locations(ctx, CAfile, noCAfile, CApath, noCApath,
  242. CAstore, noCAstore)) {
  243. ERR_print_errors(bio_err);
  244. goto end;
  245. }
  246. if (!(perform & 1))
  247. goto next;
  248. printf("Collecting connection statistics for %d seconds\n", maxtime);
  249. /* Loop and time how long it takes to make connections */
  250. bytes_read = 0;
  251. finishtime = (long)time(NULL) + maxtime;
  252. tm_Time_F(START);
  253. for (;;) {
  254. if (finishtime < (long)time(NULL))
  255. break;
  256. if ((scon = doConnection(NULL, host, ctx)) == NULL)
  257. goto end;
  258. if (www_path != NULL) {
  259. buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd,
  260. www_path);
  261. if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
  262. goto end;
  263. while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
  264. bytes_read += i;
  265. }
  266. SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
  267. BIO_closesocket(SSL_get_fd(scon));
  268. nConn += 1;
  269. if (SSL_session_reused(scon)) {
  270. ver = 'r';
  271. } else {
  272. ver = SSL_version(scon);
  273. if (ver == TLS1_VERSION)
  274. ver = 't';
  275. else if (ver == SSL3_VERSION)
  276. ver = '3';
  277. else
  278. ver = '*';
  279. }
  280. fputc(ver, stdout);
  281. fflush(stdout);
  282. SSL_free(scon);
  283. scon = NULL;
  284. }
  285. totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
  286. i = (int)((long)time(NULL) - finishtime + maxtime);
  287. printf
  288. ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
  289. nConn, totalTime, ((double)nConn / totalTime), bytes_read);
  290. printf
  291. ("%d connections in %ld real seconds, %ld bytes read per connection\n",
  292. nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
  293. /*
  294. * Now loop and time connections using the same session id over and over
  295. */
  296. next:
  297. if (!(perform & 2))
  298. goto end;
  299. printf("\n\nNow timing with session id reuse.\n");
  300. /* Get an SSL object so we can reuse the session id */
  301. if ((scon = doConnection(NULL, host, ctx)) == NULL) {
  302. BIO_printf(bio_err, "Unable to get connection\n");
  303. goto end;
  304. }
  305. if (www_path != NULL) {
  306. buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd, www_path);
  307. if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
  308. goto end;
  309. while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
  310. continue;
  311. }
  312. SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
  313. BIO_closesocket(SSL_get_fd(scon));
  314. nConn = 0;
  315. totalTime = 0.0;
  316. finishtime = (long)time(NULL) + maxtime;
  317. printf("starting\n");
  318. bytes_read = 0;
  319. tm_Time_F(START);
  320. for (;;) {
  321. if (finishtime < (long)time(NULL))
  322. break;
  323. if ((doConnection(scon, host, ctx)) == NULL)
  324. goto end;
  325. if (www_path != NULL) {
  326. buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd,
  327. www_path);
  328. if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
  329. goto end;
  330. while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
  331. bytes_read += i;
  332. }
  333. SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
  334. BIO_closesocket(SSL_get_fd(scon));
  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. printf
  355. ("%d connections in %ld real seconds, %ld bytes read per connection\n",
  356. nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
  357. ret = 0;
  358. end:
  359. SSL_free(scon);
  360. SSL_CTX_free(ctx);
  361. return ret;
  362. }
  363. /*-
  364. * doConnection - make a connection
  365. */
  366. static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx)
  367. {
  368. BIO *conn;
  369. SSL *serverCon;
  370. int i;
  371. if ((conn = BIO_new(BIO_s_connect())) == NULL)
  372. return NULL;
  373. if (BIO_set_conn_hostname(conn, host) <= 0
  374. || BIO_set_conn_mode(conn, BIO_SOCK_NODELAY) <= 0) {
  375. BIO_free(conn);
  376. return NULL;
  377. }
  378. if (scon == NULL) {
  379. serverCon = SSL_new(ctx);
  380. if (serverCon == NULL) {
  381. BIO_free(conn);
  382. return NULL;
  383. }
  384. } else {
  385. serverCon = scon;
  386. SSL_set_connect_state(serverCon);
  387. }
  388. SSL_set_bio(serverCon, conn, conn);
  389. /* ok, lets connect */
  390. i = SSL_connect(serverCon);
  391. if (i <= 0) {
  392. BIO_printf(bio_err, "ERROR\n");
  393. if (verify_args.error != X509_V_OK)
  394. BIO_printf(bio_err, "verify error:%s\n",
  395. X509_verify_cert_error_string(verify_args.error));
  396. else
  397. ERR_print_errors(bio_err);
  398. if (scon == NULL)
  399. SSL_free(serverCon);
  400. return NULL;
  401. }
  402. #if defined(SOL_SOCKET) && defined(SO_LINGER)
  403. {
  404. struct linger no_linger;
  405. int fd;
  406. no_linger.l_onoff = 1;
  407. no_linger.l_linger = 0;
  408. fd = SSL_get_fd(serverCon);
  409. if (fd >= 0)
  410. (void)setsockopt(fd, SOL_SOCKET, SO_LINGER, (char*)&no_linger,
  411. sizeof(no_linger));
  412. }
  413. #endif
  414. return serverCon;
  415. }
  416. #endif /* OPENSSL_NO_SOCK */