2 * Copyright 1995-2016 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);
52 static const char fmt_http_get_cmd[] = "GET %s HTTP/1.0\r\n\r\n";
54 typedef enum OPTION_choice {
55 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
56 OPT_CONNECT, OPT_CIPHER, OPT_CERT, OPT_KEY, OPT_CAPATH,
57 OPT_CAFILE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_NEW, OPT_REUSE, OPT_BUGS,
58 OPT_VERIFY, OPT_TIME, OPT_SSL3,
62 const OPTIONS s_time_options[] = {
63 {"help", OPT_HELP, '-', "Display this summary"},
64 {"connect", OPT_CONNECT, 's',
65 "Where to connect as post:port (default is " SSL_CONNECT_NAME ")"},
66 {"cipher", OPT_CIPHER, 's', "Cipher to use, see 'openssl ciphers'"},
67 {"cert", OPT_CERT, '<', "Cert file to use, PEM format assumed"},
68 {"key", OPT_KEY, '<', "File with key, PEM; default is -cert file"},
69 {"CApath", OPT_CAPATH, '/', "PEM format directory of CA's"},
70 {"cafile", OPT_CAFILE, '<', "PEM format file of CA's"},
71 {"no-CAfile", OPT_NOCAFILE, '-',
72 "Do not load the default certificates file"},
73 {"no-CApath", OPT_NOCAPATH, '-',
74 "Do not load certificates from the default certificates directory"},
75 {"new", OPT_NEW, '-', "Just time new connections"},
76 {"reuse", OPT_REUSE, '-', "Just time connection reuse"},
77 {"bugs", OPT_BUGS, '-', "Turn on SSL bug compatibility"},
78 {"verify", OPT_VERIFY, 'p',
79 "Turn on peer certificate verification, set depth"},
80 {"time", OPT_TIME, 'p', "Seconds to collect data, default " SECONDSSTR},
81 {"www", OPT_WWW, 's', "Fetch specified page from the site"},
82 #ifndef OPENSSL_NO_SSL3
83 {"ssl3", OPT_SSL3, '-', "Just use SSLv3"},
91 static double tm_Time_F(int s)
93 return app_tminterval(s, 1);
96 int s_time_main(int argc, char **argv)
101 const SSL_METHOD *meth = NULL;
102 char *CApath = NULL, *CAfile = NULL, *cipher = NULL, *www_path = NULL;
103 char *host = SSL_CONNECT_NAME, *certfile = NULL, *keyfile = NULL, *prog;
104 double totalTime = 0.0;
105 int noCApath = 0, noCAfile = 0;
106 int maxtime = SECONDS, nConn = 0, perform = 3, ret = 1, i, st_bugs = 0;
107 long bytes_read = 0, finishtime = 0;
109 int max_version = 0, ver, buf_len;
112 meth = TLS_client_method();
114 prog = opt_init(argc, argv, s_time_options);
115 while ((o = opt_next()) != OPT_EOF) {
120 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
123 opt_help(s_time_options);
136 if (!opt_int(opt_arg(), &verify_args.depth))
138 BIO_printf(bio_err, "%s: verify depth is %d\n",
139 prog, verify_args.depth);
142 certfile = opt_arg();
166 if (!opt_int(opt_arg(), &maxtime))
170 www_path = opt_arg();
171 buf_size = strlen(www_path) + sizeof(fmt_http_get_cmd) - 2; /* 2 is for %s */
172 if (buf_size > sizeof(buf)) {
173 BIO_printf(bio_err, "%s: -www option is too long\n", prog);
178 max_version = SSL3_VERSION;
182 argc = opt_num_rest();
187 cipher = getenv("SSL_CIPHER");
188 if (cipher == NULL) {
189 BIO_printf(bio_err, "No CIPHER specified\n");
193 if ((ctx = SSL_CTX_new(meth)) == NULL)
196 SSL_CTX_set_quiet_shutdown(ctx, 1);
197 if (SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
201 SSL_CTX_set_options(ctx, SSL_OP_ALL);
202 if (!SSL_CTX_set_cipher_list(ctx, cipher))
204 if (!set_cert_stuff(ctx, certfile, keyfile))
207 if (!ctx_set_verify_locations(ctx, CAfile, CApath, noCAfile, noCApath)) {
208 ERR_print_errors(bio_err);
213 printf("Collecting connection statistics for %d seconds\n", maxtime);
215 /* Loop and time how long it takes to make connections */
218 finishtime = (long)time(NULL) + maxtime;
221 if (finishtime < (long)time(NULL))
224 if ((scon = doConnection(NULL, host, ctx)) == NULL)
227 if (www_path != NULL) {
228 buf_len = BIO_snprintf(buf, sizeof buf,
229 fmt_http_get_cmd, www_path);
230 if (SSL_write(scon, buf, buf_len) <= 0)
232 while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
236 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
240 BIO_closesocket(SSL_get_fd(scon));
243 if (SSL_session_reused(scon))
246 ver = SSL_version(scon);
247 if (ver == TLS1_VERSION)
249 else if (ver == SSL3_VERSION)
260 totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
262 i = (int)((long)time(NULL) - finishtime + maxtime);
264 ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
265 nConn, totalTime, ((double)nConn / totalTime), bytes_read);
267 ("%d connections in %ld real seconds, %ld bytes read per connection\n",
268 nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
271 * Now loop and time connections using the same session id over and over
277 printf("\n\nNow timing with session id reuse.\n");
279 /* Get an SSL object so we can reuse the session id */
280 if ((scon = doConnection(NULL, host, ctx)) == NULL) {
281 BIO_printf(bio_err, "Unable to get connection\n");
285 if (www_path != NULL) {
286 buf_len = BIO_snprintf(buf, sizeof buf,
287 fmt_http_get_cmd, www_path);
288 if (SSL_write(scon, buf, buf_len) <= 0)
290 while (SSL_read(scon, buf, sizeof(buf)) > 0)
294 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
298 BIO_closesocket(SSL_get_fd(scon));
303 finishtime = (long)time(NULL) + maxtime;
305 printf("starting\n");
310 if (finishtime < (long)time(NULL))
313 if ((doConnection(scon, host, ctx)) == NULL)
317 BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n",
319 if (SSL_write(scon, buf, strlen(buf)) <= 0)
321 while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
325 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
329 BIO_closesocket(SSL_get_fd(scon));
332 if (SSL_session_reused(scon))
335 ver = SSL_version(scon);
336 if (ver == TLS1_VERSION)
338 else if (ver == SSL3_VERSION)
346 totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
349 ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
350 nConn, totalTime, ((double)nConn / totalTime), bytes_read);
352 ("%d connections in %ld real seconds, %ld bytes read per connection\n",
353 nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
364 * doConnection - make a connection
366 static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx)
373 if ((conn = BIO_new(BIO_s_connect())) == NULL)
376 BIO_set_conn_hostname(conn, host);
379 serverCon = SSL_new(ctx);
382 SSL_set_connect_state(serverCon);
385 SSL_set_bio(serverCon, conn, conn);
387 /* ok, lets connect */
389 i = SSL_connect(serverCon);
390 if (BIO_sock_should_retry(i)) {
391 BIO_printf(bio_err, "DELAY\n");
393 i = SSL_get_fd(serverCon);
396 openssl_fdset(i, &readfds);
398 * Note: under VMS with SOCKETSHR the 2nd parameter is currently
399 * of type (int *) whereas under other systems it is (void *) if
400 * you don't have a cast it will choke the compiler: if you do
401 * have a cast then you can either go for (int *) or (void *).
403 select(width, (void *)&readfds, NULL, NULL, NULL);
409 BIO_printf(bio_err, "ERROR\n");
410 if (verify_args.error != X509_V_OK)
411 BIO_printf(bio_err, "verify error:%s\n",
412 X509_verify_cert_error_string(verify_args.error));
414 ERR_print_errors(bio_err);
422 #endif /* OPENSSL_NO_SOCK */