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"
42 #define BUFSIZZ 1024*10
44 #define MYBUFSIZ 1024*8
48 #define min(a,b) (((a) < (b)) ? (a) : (b))
49 #define max(a,b) (((a) > (b)) ? (a) : (b))
53 #define SECONDSSTR "30"
55 extern int verify_depth;
56 extern int verify_error;
58 static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx);
60 typedef enum OPTION_choice {
61 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
62 OPT_CONNECT, OPT_CIPHER, OPT_CERT, OPT_KEY, OPT_CAPATH,
63 OPT_CAFILE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_NEW, OPT_REUSE, OPT_BUGS,
64 OPT_VERIFY, OPT_TIME, OPT_SSL3,
68 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', "Cipher to use, see 'openssl ciphers'"},
73 {"cert", OPT_CERT, '<', "Cert file to use, PEM format assumed"},
74 {"key", OPT_KEY, '<', "File with key, PEM; default is -cert file"},
75 {"CApath", OPT_CAPATH, '/', "PEM format directory of CA's"},
76 {"cafile", OPT_CAFILE, '<', "PEM format file of CA's"},
77 {"no-CAfile", OPT_NOCAFILE, '-',
78 "Do not load the default certificates file"},
79 {"no-CApath", OPT_NOCAPATH, '-',
80 "Do not load certificates from the default certificates directory"},
81 {"new", OPT_NEW, '-', "Just time new connections"},
82 {"reuse", OPT_REUSE, '-', "Just time connection reuse"},
83 {"bugs", OPT_BUGS, '-', "Turn on SSL bug compatibility"},
84 {"verify", OPT_VERIFY, 'p',
85 "Turn on peer certificate verification, set depth"},
86 {"time", OPT_TIME, 'p', "Seconds to collect data, default " SECONDSSTR},
87 {"www", OPT_WWW, 's', "Fetch specified page from the site"},
88 #ifndef OPENSSL_NO_SSL3
89 {"ssl3", OPT_SSL3, '-', "Just use SSLv3"},
97 static double tm_Time_F(int s)
99 return app_tminterval(s, 1);
102 int s_time_main(int argc, char **argv)
107 const SSL_METHOD *meth = NULL;
108 char *CApath = NULL, *CAfile = NULL, *cipher = NULL, *www_path = NULL;
109 char *host = SSL_CONNECT_NAME, *certfile = NULL, *keyfile = NULL, *prog;
110 double totalTime = 0.0;
111 int noCApath = 0, noCAfile = 0;
112 int maxtime = SECONDS, nConn = 0, perform = 3, ret = 1, i, st_bugs =
114 long bytes_read = 0, finishtime = 0;
118 meth = TLS_client_method();
120 verify_error = X509_V_OK;
122 prog = opt_init(argc, argv, s_time_options);
123 while ((o = opt_next()) != OPT_EOF) {
128 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
131 opt_help(s_time_options);
144 if (!opt_int(opt_arg(), &verify_depth))
146 BIO_printf(bio_err, "%s: verify depth is %d\n",
150 certfile = opt_arg();
174 if (!opt_int(opt_arg(), &maxtime))
178 www_path = opt_arg();
179 if (strlen(www_path) > MYBUFSIZ - 100) {
180 BIO_printf(bio_err, "%s: -www option too long\n", prog);
185 max_version = SSL3_VERSION;
189 argc = opt_num_rest();
194 cipher = getenv("SSL_CIPHER");
195 if (cipher == NULL) {
196 BIO_printf(bio_err, "No CIPHER specified\n");
200 if ((ctx = SSL_CTX_new(meth)) == NULL)
203 SSL_CTX_set_quiet_shutdown(ctx, 1);
204 if (SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
208 SSL_CTX_set_options(ctx, SSL_OP_ALL);
209 if (!SSL_CTX_set_cipher_list(ctx, cipher))
211 if (!set_cert_stuff(ctx, certfile, keyfile))
214 if (!ctx_set_verify_locations(ctx, CAfile, CApath, noCAfile, noCApath)) {
215 ERR_print_errors(bio_err);
220 printf("Collecting connection statistics for %d seconds\n", maxtime);
222 /* Loop and time how long it takes to make connections */
225 finishtime = (long)time(NULL) + maxtime;
228 if (finishtime < (long)time(NULL))
231 if ((scon = doConnection(NULL, host, ctx)) == NULL)
234 if (www_path != NULL) {
235 BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n",
237 if (SSL_write(scon, buf, strlen(buf)) <= 0)
239 while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
243 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
247 BIO_closesocket(SSL_get_fd(scon));
250 if (SSL_session_reused(scon))
253 ver = SSL_version(scon);
254 if (ver == TLS1_VERSION)
256 else if (ver == SSL3_VERSION)
267 totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
269 i = (int)((long)time(NULL) - finishtime + maxtime);
271 ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
272 nConn, totalTime, ((double)nConn / totalTime), bytes_read);
274 ("%d connections in %ld real seconds, %ld bytes read per connection\n",
275 nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
278 * Now loop and time connections using the same session id over and over
284 printf("\n\nNow timing with session id reuse.\n");
286 /* Get an SSL object so we can reuse the session id */
287 if ((scon = doConnection(NULL, host, ctx)) == NULL) {
288 BIO_printf(bio_err, "Unable to get connection\n");
292 if (www_path != NULL) {
293 BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n", www_path);
294 if (SSL_write(scon, buf, strlen(buf)) <= 0)
296 while (SSL_read(scon, buf, sizeof(buf)) > 0)
300 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
304 BIO_closesocket(SSL_get_fd(scon));
309 finishtime = (long)time(NULL) + maxtime;
311 printf("starting\n");
316 if (finishtime < (long)time(NULL))
319 if ((doConnection(scon, host, ctx)) == NULL)
323 BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n",
325 if (SSL_write(scon, buf, strlen(buf)) <= 0)
327 while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
331 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
335 BIO_closesocket(SSL_get_fd(scon));
338 if (SSL_session_reused(scon))
341 ver = SSL_version(scon);
342 if (ver == TLS1_VERSION)
344 else if (ver == SSL3_VERSION)
352 totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
355 ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
356 nConn, totalTime, ((double)nConn / totalTime), bytes_read);
358 ("%d connections in %ld real seconds, %ld bytes read per connection\n",
359 nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
370 * doConnection - make a connection
372 static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx)
379 if ((conn = BIO_new(BIO_s_connect())) == NULL)
382 BIO_set_conn_hostname(conn, host);
385 serverCon = SSL_new(ctx);
388 SSL_set_connect_state(serverCon);
391 SSL_set_bio(serverCon, conn, conn);
393 /* ok, lets connect */
395 i = SSL_connect(serverCon);
396 if (BIO_sock_should_retry(i)) {
397 BIO_printf(bio_err, "DELAY\n");
399 i = SSL_get_fd(serverCon);
402 openssl_fdset(i, &readfds);
404 * Note: under VMS with SOCKETSHR the 2nd parameter is currently
405 * of type (int *) whereas under other systems it is (void *) if
406 * you don't have a cast it will choke the compiler: if you do
407 * have a cast then you can either go for (int *) or (void *).
409 select(width, (void *)&readfds, NULL, NULL, NULL);
415 BIO_printf(bio_err, "ERROR\n");
416 if (verify_error != X509_V_OK)
417 BIO_printf(bio_err, "verify error:%s\n",
418 X509_verify_cert_error_string(verify_error));
420 ERR_print_errors(bio_err);
428 #endif /* OPENSSL_NO_SOCK */