s_time.c 14 KB

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