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
14 #include <openssl/opensslconf.h>
16 #ifndef OPENSSL_NO_SOCK
20 #include <openssl/x509.h>
21 #include <openssl/ssl.h>
22 #include <openssl/pem.h>
24 #include <openssl/err.h>
25 #if !defined(OPENSSL_SYS_MSDOS)
26 # include OPENSSL_UNISTD
29 #define SSL_CONNECT_NAME "localhost:4433"
32 #define SECONDSSTR "30"
34 static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx);
36 static const char fmt_http_get_cmd[] = "GET %s HTTP/1.0\r\n\r\n";
38 typedef enum OPTION_choice {
39 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
40 OPT_CONNECT, OPT_CIPHER, OPT_CERT, OPT_KEY, OPT_CAPATH,
41 OPT_CAFILE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_NEW, OPT_REUSE, OPT_BUGS,
42 OPT_VERIFY, OPT_TIME, OPT_SSL3,
46 OPTIONS s_time_options[] = {
47 {"help", OPT_HELP, '-', "Display this summary"},
48 {"connect", OPT_CONNECT, 's',
49 "Where to connect as post:port (default is " SSL_CONNECT_NAME ")"},
50 {"cipher", OPT_CIPHER, 's', "Cipher to use, see 'openssl ciphers'"},
51 {"cert", OPT_CERT, '<', "Cert file to use, PEM format assumed"},
52 {"key", OPT_KEY, '<', "File with key, PEM; default is -cert file"},
53 {"CApath", OPT_CAPATH, '/', "PEM format directory of CA's"},
54 {"cafile", OPT_CAFILE, '<', "PEM format file of CA's"},
55 {"no-CAfile", OPT_NOCAFILE, '-',
56 "Do not load the default certificates file"},
57 {"no-CApath", OPT_NOCAPATH, '-',
58 "Do not load certificates from the default certificates directory"},
59 {"new", OPT_NEW, '-', "Just time new connections"},
60 {"reuse", OPT_REUSE, '-', "Just time connection reuse"},
61 {"bugs", OPT_BUGS, '-', "Turn on SSL bug compatibility"},
62 {"verify", OPT_VERIFY, 'p',
63 "Turn on peer certificate verification, set depth"},
64 {"time", OPT_TIME, 'p', "Seconds to collect data, default " SECONDSSTR},
65 {"www", OPT_WWW, 's', "Fetch specified page from the site"},
66 #ifndef OPENSSL_NO_SSL3
67 {"ssl3", OPT_SSL3, '-', "Just use SSLv3"},
75 static double tm_Time_F(int s)
77 return app_tminterval(s, 1);
80 int s_time_main(int argc, char **argv)
85 const SSL_METHOD *meth = NULL;
86 char *CApath = NULL, *CAfile = NULL, *cipher = NULL, *www_path = NULL;
87 char *host = SSL_CONNECT_NAME, *certfile = NULL, *keyfile = NULL, *prog;
88 double totalTime = 0.0;
89 int noCApath = 0, noCAfile = 0;
90 int maxtime = SECONDS, nConn = 0, perform = 3, ret = 1, i, st_bugs = 0;
91 long bytes_read = 0, finishtime = 0;
93 int max_version = 0, ver, buf_len;
96 meth = TLS_client_method();
98 prog = opt_init(argc, argv, s_time_options);
99 while ((o = opt_next()) != OPT_EOF) {
104 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
107 opt_help(s_time_options);
120 if (!opt_int(opt_arg(), &verify_args.depth))
122 BIO_printf(bio_err, "%s: verify depth is %d\n",
123 prog, verify_args.depth);
126 certfile = opt_arg();
150 if (!opt_int(opt_arg(), &maxtime))
154 www_path = opt_arg();
155 buf_size = strlen(www_path) + sizeof(fmt_http_get_cmd) - 2; /* 2 is for %s */
156 if (buf_size > sizeof(buf)) {
157 BIO_printf(bio_err, "%s: -www option is too long\n", prog);
162 max_version = SSL3_VERSION;
166 argc = opt_num_rest();
171 cipher = getenv("SSL_CIPHER");
173 BIO_printf(bio_err, "No CIPHER specified\n");
175 if ((ctx = SSL_CTX_new(meth)) == NULL)
178 SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
179 SSL_CTX_set_quiet_shutdown(ctx, 1);
180 if (SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
184 SSL_CTX_set_options(ctx, SSL_OP_ALL);
185 if (cipher != NULL && !SSL_CTX_set_cipher_list(ctx, cipher))
187 if (!set_cert_stuff(ctx, certfile, keyfile))
190 if (!ctx_set_verify_locations(ctx, CAfile, CApath, noCAfile, noCApath)) {
191 ERR_print_errors(bio_err);
196 printf("Collecting connection statistics for %d seconds\n", maxtime);
198 /* Loop and time how long it takes to make connections */
201 finishtime = (long)time(NULL) + maxtime;
204 if (finishtime < (long)time(NULL))
207 if ((scon = doConnection(NULL, host, ctx)) == NULL)
210 if (www_path != NULL) {
211 buf_len = BIO_snprintf(buf, sizeof(buf),
212 fmt_http_get_cmd, www_path);
213 if (SSL_write(scon, buf, buf_len) <= 0)
215 while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
218 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
219 BIO_closesocket(SSL_get_fd(scon));
222 if (SSL_session_reused(scon))
225 ver = SSL_version(scon);
226 if (ver == TLS1_VERSION)
228 else if (ver == SSL3_VERSION)
239 totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
241 i = (int)((long)time(NULL) - finishtime + maxtime);
243 ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
244 nConn, totalTime, ((double)nConn / totalTime), bytes_read);
246 ("%d connections in %ld real seconds, %ld bytes read per connection\n",
247 nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
250 * Now loop and time connections using the same session id over and over
256 printf("\n\nNow timing with session id reuse.\n");
258 /* Get an SSL object so we can reuse the session id */
259 if ((scon = doConnection(NULL, host, ctx)) == NULL) {
260 BIO_printf(bio_err, "Unable to get connection\n");
264 if (www_path != NULL) {
265 buf_len = BIO_snprintf(buf, sizeof(buf),
266 fmt_http_get_cmd, www_path);
267 if (SSL_write(scon, buf, buf_len) <= 0)
269 while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
272 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
273 BIO_closesocket(SSL_get_fd(scon));
278 finishtime = (long)time(NULL) + maxtime;
280 printf("starting\n");
285 if (finishtime < (long)time(NULL))
288 if ((doConnection(scon, host, ctx)) == NULL)
292 BIO_snprintf(buf, sizeof(buf), "GET %s HTTP/1.0\r\n\r\n",
294 if (SSL_write(scon, buf, strlen(buf)) <= 0)
296 while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
299 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
300 BIO_closesocket(SSL_get_fd(scon));
303 if (SSL_session_reused(scon))
306 ver = SSL_version(scon);
307 if (ver == TLS1_VERSION)
309 else if (ver == SSL3_VERSION)
317 totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
320 ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
321 nConn, totalTime, ((double)nConn / totalTime), bytes_read);
323 ("%d connections in %ld real seconds, %ld bytes read per connection\n",
324 nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
335 * doConnection - make a connection
337 static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx)
343 if ((conn = BIO_new(BIO_s_connect())) == NULL)
346 BIO_set_conn_hostname(conn, host);
347 BIO_set_conn_mode(conn, BIO_SOCK_NODELAY);
350 serverCon = SSL_new(ctx);
353 SSL_set_connect_state(serverCon);
356 SSL_set_bio(serverCon, conn, conn);
358 /* ok, lets connect */
359 i = SSL_connect(serverCon);
361 BIO_printf(bio_err, "ERROR\n");
362 if (verify_args.error != X509_V_OK)
363 BIO_printf(bio_err, "verify error:%s\n",
364 X509_verify_cert_error_string(verify_args.error));
366 ERR_print_errors(bio_err);
372 #if defined(SOL_SOCKET) && defined(SO_LINGER)
374 struct linger no_linger;
376 no_linger.l_onoff = 1;
377 no_linger.l_linger = 0;
378 (void) setsockopt(SSL_get_fd(serverCon), SOL_SOCKET, SO_LINGER,
379 (char*)&no_linger, sizeof(no_linger));
385 #endif /* OPENSSL_NO_SOCK */