2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
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
16 #include <openssl/opensslconf.h>
18 #ifndef OPENSSL_NO_SOCK
22 #include <openssl/x509.h>
23 #include <openssl/ssl.h>
24 #include <openssl/pem.h>
26 #include <openssl/err.h>
27 #if !defined(OPENSSL_SYS_MSDOS)
28 # include OPENSSL_UNISTD
32 #define ioctl ioctlsocket
34 #define SSL_CONNECT_NAME "localhost:4433"
36 /* no default cert. */
38 * #define TEST_CERT "client.pem"
43 #define min(a,b) (((a) < (b)) ? (a) : (b))
44 #define max(a,b) (((a) > (b)) ? (a) : (b))
48 #define SECONDSSTR "30"
50 static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx);
53 * Define a HTTP get command globally.
54 * Also define the size of the command, this is two bytes less than
55 * the size of the string because the %s is replaced by the URL.
57 static const char fmt_http_get_cmd[] = "GET %s HTTP/1.0\r\n\r\n";
58 static const size_t fmt_http_get_cmd_size = sizeof(fmt_http_get_cmd) - 2;
60 typedef enum OPTION_choice {
61 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
62 OPT_CONNECT, OPT_CIPHER, OPT_CIPHERSUITES, OPT_CERT, OPT_NAMEOPT, OPT_KEY,
63 OPT_CAPATH, OPT_CAFILE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_NEW, OPT_REUSE,
64 OPT_BUGS, OPT_VERIFY, OPT_TIME, OPT_SSL3,
68 const OPTIONS s_time_options[] = {
69 {"help", OPT_HELP, '-', "Display this summary"},
70 {"connect", OPT_CONNECT, 's',
71 "Where to connect as post:port (default is " SSL_CONNECT_NAME ")"},
72 {"cipher", OPT_CIPHER, 's', "TLSv1.2 and below cipher list to be used"},
73 {"ciphersuites", OPT_CIPHERSUITES, 's',
74 "Specify TLSv1.3 ciphersuites to be used"},
75 {"cert", OPT_CERT, '<', "Cert file to use, PEM format assumed"},
76 {"nameopt", OPT_NAMEOPT, 's', "Various certificate name options"},
77 {"key", OPT_KEY, '<', "File with key, PEM; default is -cert file"},
78 {"CApath", OPT_CAPATH, '/', "PEM format directory of CA's"},
79 {"cafile", OPT_CAFILE, '<', "PEM format file of CA's"},
80 {"no-CAfile", OPT_NOCAFILE, '-',
81 "Do not load the default certificates file"},
82 {"no-CApath", OPT_NOCAPATH, '-',
83 "Do not load certificates from the default certificates directory"},
84 {"new", OPT_NEW, '-', "Just time new connections"},
85 {"reuse", OPT_REUSE, '-', "Just time connection reuse"},
86 {"bugs", OPT_BUGS, '-', "Turn on SSL bug compatibility"},
87 {"verify", OPT_VERIFY, 'p',
88 "Turn on peer certificate verification, set depth"},
89 {"time", OPT_TIME, 'p', "Seconds to collect data, default " SECONDSSTR},
90 {"www", OPT_WWW, 's', "Fetch specified page from the site"},
91 #ifndef OPENSSL_NO_SSL3
92 {"ssl3", OPT_SSL3, '-', "Just use SSLv3"},
100 static double tm_Time_F(int s)
102 return app_tminterval(s, 1);
105 int s_time_main(int argc, char **argv)
110 const SSL_METHOD *meth = NULL;
111 char *CApath = NULL, *CAfile = NULL, *cipher = NULL, *ciphersuites = NULL;
112 char *www_path = NULL;
113 char *host = SSL_CONNECT_NAME, *certfile = NULL, *keyfile = NULL, *prog;
114 double totalTime = 0.0;
115 int noCApath = 0, noCAfile = 0;
116 int maxtime = SECONDS, nConn = 0, perform = 3, ret = 1, i, st_bugs = 0;
117 long bytes_read = 0, finishtime = 0;
119 int max_version = 0, ver, buf_len;
122 meth = TLS_client_method();
124 prog = opt_init(argc, argv, s_time_options);
125 while ((o = opt_next()) != OPT_EOF) {
130 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
133 opt_help(s_time_options);
146 if (!opt_int(opt_arg(), &verify_args.depth))
148 BIO_printf(bio_err, "%s: verify depth is %d\n",
149 prog, verify_args.depth);
152 certfile = opt_arg();
155 if (!set_nameopt(opt_arg()))
176 case OPT_CIPHERSUITES:
177 ciphersuites = opt_arg();
183 if (!opt_int(opt_arg(), &maxtime))
187 www_path = opt_arg();
188 buf_size = strlen(www_path) + fmt_http_get_cmd_size;
189 if (buf_size > sizeof(buf)) {
190 BIO_printf(bio_err, "%s: -www option is too long\n", prog);
195 max_version = SSL3_VERSION;
199 argc = opt_num_rest();
204 cipher = getenv("SSL_CIPHER");
206 if ((ctx = SSL_CTX_new(meth)) == NULL)
209 SSL_CTX_set_quiet_shutdown(ctx, 1);
210 if (SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
214 SSL_CTX_set_options(ctx, SSL_OP_ALL);
215 if (cipher != NULL && !SSL_CTX_set_cipher_list(ctx, cipher))
217 if (ciphersuites != NULL && !SSL_CTX_set_ciphersuites(ctx, ciphersuites))
219 if (!set_cert_stuff(ctx, certfile, keyfile))
222 if (!ctx_set_verify_locations(ctx, CAfile, CApath, noCAfile, noCApath)) {
223 ERR_print_errors(bio_err);
228 printf("Collecting connection statistics for %d seconds\n", maxtime);
230 /* Loop and time how long it takes to make connections */
233 finishtime = (long)time(NULL) + maxtime;
236 if (finishtime < (long)time(NULL))
239 if ((scon = doConnection(NULL, host, ctx)) == NULL)
242 if (www_path != NULL) {
243 buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd,
245 if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
247 while ((i = SSL_read(scon, buf, sizeof(buf))) > 0 ||
248 SSL_get_error(scon, i) == SSL_ERROR_WANT_READ ||
249 SSL_get_error(scon, i) == SSL_ERROR_WANT_WRITE)
250 if (i > 0) bytes_read += i;
253 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
257 BIO_closesocket(SSL_get_fd(scon));
260 if (SSL_session_reused(scon)) {
263 ver = SSL_version(scon);
264 if (ver == TLS1_VERSION)
266 else if (ver == SSL3_VERSION)
277 totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
279 i = (int)((long)time(NULL) - finishtime + maxtime);
281 ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
282 nConn, totalTime, ((double)nConn / totalTime), bytes_read);
284 ("%d connections in %ld real seconds, %ld bytes read per connection\n",
285 nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
288 * Now loop and time connections using the same session id over and over
294 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");
302 if (www_path != NULL) {
303 buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd, www_path);
304 if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
306 while ((i = SSL_read(scon, buf, sizeof(buf))) > 0 ||
307 SSL_get_error(scon, i) == SSL_ERROR_WANT_READ ||
308 SSL_get_error(scon, i) == SSL_ERROR_WANT_WRITE)
312 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
316 BIO_closesocket(SSL_get_fd(scon));
321 finishtime = (long)time(NULL) + maxtime;
323 printf("starting\n");
328 if (finishtime < (long)time(NULL))
331 if ((doConnection(scon, host, ctx)) == NULL)
334 if (www_path != NULL) {
335 buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd,
337 if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
339 while ((i = SSL_read(scon, buf, sizeof(buf))) > 0 ||
340 SSL_get_error(scon, i) == SSL_ERROR_WANT_READ ||
341 SSL_get_error(scon, i) == SSL_ERROR_WANT_WRITE)
342 if (i > 0) bytes_read += i;
345 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
349 BIO_closesocket(SSL_get_fd(scon));
352 if (SSL_session_reused(scon)) {
355 ver = SSL_version(scon);
356 if (ver == TLS1_VERSION)
358 else if (ver == SSL3_VERSION)
366 totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
369 ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
370 nConn, totalTime, ((double)nConn / totalTime), bytes_read);
372 ("%d connections in %ld real seconds, %ld bytes read per connection\n",
373 nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
384 * doConnection - make a connection
386 static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx)
393 if ((conn = BIO_new(BIO_s_connect())) == NULL)
396 BIO_set_conn_hostname(conn, host);
399 serverCon = SSL_new(ctx);
402 SSL_set_connect_state(serverCon);
405 SSL_set_bio(serverCon, conn, conn);
407 /* ok, lets connect */
409 i = SSL_connect(serverCon);
410 if (BIO_sock_should_retry(i)) {
411 BIO_printf(bio_err, "DELAY\n");
413 i = SSL_get_fd(serverCon);
416 openssl_fdset(i, &readfds);
418 * Note: under VMS with SOCKETSHR the 2nd parameter is currently
419 * of type (int *) whereas under other systems it is (void *) if
420 * you don't have a cast it will choke the compiler: if you do
421 * have a cast then you can either go for (int *) or (void *).
423 select(width, (void *)&readfds, NULL, NULL, NULL);
429 BIO_printf(bio_err, "ERROR\n");
430 if (verify_args.error != X509_V_OK)
431 BIO_printf(bio_err, "verify error:%s\n",
432 X509_verify_cert_error_string(verify_args.error));
434 ERR_print_errors(bio_err);
442 #endif /* OPENSSL_NO_SOCK */