2 * Copyright 1995-2017 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_CERT, OPT_NAMEOPT, 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 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', "Cipher to use, see 'openssl ciphers'"},
73 {"cert", OPT_CERT, '<', "Cert file to use, PEM format assumed"},
74 {"nameopt", OPT_NAMEOPT, 's', "Various certificate name options"},
75 {"key", OPT_KEY, '<', "File with key, PEM; default is -cert file"},
76 {"CApath", OPT_CAPATH, '/', "PEM format directory of CA's"},
77 {"cafile", OPT_CAFILE, '<', "PEM format file of CA's"},
78 {"no-CAfile", OPT_NOCAFILE, '-',
79 "Do not load the default certificates file"},
80 {"no-CApath", OPT_NOCAPATH, '-',
81 "Do not load certificates from the default certificates directory"},
82 {"new", OPT_NEW, '-', "Just time new connections"},
83 {"reuse", OPT_REUSE, '-', "Just time connection reuse"},
84 {"bugs", OPT_BUGS, '-', "Turn on SSL bug compatibility"},
85 {"verify", OPT_VERIFY, 'p',
86 "Turn on peer certificate verification, set depth"},
87 {"time", OPT_TIME, 'p', "Seconds to collect data, default " SECONDSSTR},
88 {"www", OPT_WWW, 's', "Fetch specified page from the site"},
89 #ifndef OPENSSL_NO_SSL3
90 {"ssl3", OPT_SSL3, '-', "Just use SSLv3"},
98 static double tm_Time_F(int s)
100 return app_tminterval(s, 1);
103 int s_time_main(int argc, char **argv)
108 const SSL_METHOD *meth = NULL;
109 char *CApath = NULL, *CAfile = NULL, *cipher = NULL, *www_path = NULL;
110 char *host = SSL_CONNECT_NAME, *certfile = NULL, *keyfile = NULL, *prog;
111 double totalTime = 0.0;
112 int noCApath = 0, noCAfile = 0;
113 int maxtime = SECONDS, nConn = 0, perform = 3, ret = 1, i, st_bugs = 0;
114 long bytes_read = 0, finishtime = 0;
116 int max_version = 0, ver, buf_len;
119 meth = TLS_client_method();
121 prog = opt_init(argc, argv, s_time_options);
122 while ((o = opt_next()) != OPT_EOF) {
127 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
130 opt_help(s_time_options);
143 if (!opt_int(opt_arg(), &verify_args.depth))
145 BIO_printf(bio_err, "%s: verify depth is %d\n",
146 prog, verify_args.depth);
149 certfile = opt_arg();
152 if (!set_nameopt(opt_arg()))
177 if (!opt_int(opt_arg(), &maxtime))
181 www_path = opt_arg();
182 buf_size = strlen(www_path) + fmt_http_get_cmd_size;
183 if (buf_size > sizeof(buf)) {
184 BIO_printf(bio_err, "%s: -www option is too long\n", prog);
189 max_version = SSL3_VERSION;
193 argc = opt_num_rest();
198 cipher = getenv("SSL_CIPHER");
199 if (cipher == NULL) {
200 BIO_printf(bio_err, "No CIPHER specified\n");
204 if ((ctx = SSL_CTX_new(meth)) == NULL)
207 SSL_CTX_set_quiet_shutdown(ctx, 1);
208 if (SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
212 SSL_CTX_set_options(ctx, SSL_OP_ALL);
213 if (!SSL_CTX_set_cipher_list(ctx, cipher))
215 if (!set_cert_stuff(ctx, certfile, keyfile))
218 if (!ctx_set_verify_locations(ctx, CAfile, CApath, noCAfile, noCApath)) {
219 ERR_print_errors(bio_err);
224 printf("Collecting connection statistics for %d seconds\n", maxtime);
226 /* Loop and time how long it takes to make connections */
229 finishtime = (long)time(NULL) + maxtime;
232 if (finishtime < (long)time(NULL))
235 if ((scon = doConnection(NULL, host, ctx)) == NULL)
238 if (www_path != NULL) {
239 buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd,
241 if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
243 while ((i = SSL_read(scon, buf, sizeof(buf))) > 0 || SSL_get_error(scon, i) == SSL_ERROR_WANT_READ)
244 if (i > 0) bytes_read += i;
247 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
251 BIO_closesocket(SSL_get_fd(scon));
254 if (SSL_session_reused(scon)) {
257 ver = SSL_version(scon);
258 if (ver == TLS1_VERSION)
260 else if (ver == SSL3_VERSION)
271 totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
273 i = (int)((long)time(NULL) - finishtime + maxtime);
275 ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
276 nConn, totalTime, ((double)nConn / totalTime), bytes_read);
278 ("%d connections in %ld real seconds, %ld bytes read per connection\n",
279 nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
282 * Now loop and time connections using the same session id over and over
288 printf("\n\nNow timing with session id reuse.\n");
290 /* Get an SSL object so we can reuse the session id */
291 if ((scon = doConnection(NULL, host, ctx)) == NULL) {
292 BIO_printf(bio_err, "Unable to get connection\n");
296 if (www_path != NULL) {
297 buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd, www_path);
298 if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
300 while ((i = SSL_read(scon, buf, sizeof(buf))) > 0 || SSL_get_error(scon, i) == SSL_ERROR_WANT_READ)
304 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
308 BIO_closesocket(SSL_get_fd(scon));
313 finishtime = (long)time(NULL) + maxtime;
315 printf("starting\n");
320 if (finishtime < (long)time(NULL))
323 if ((doConnection(scon, host, ctx)) == NULL)
326 if (www_path != NULL) {
327 buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd,
329 if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
331 while ((i = SSL_read(scon, buf, sizeof(buf))) > 0 || SSL_get_error(scon, i) == SSL_ERROR_WANT_READ)
332 if (i > 0) bytes_read += i;
335 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
339 BIO_closesocket(SSL_get_fd(scon));
342 if (SSL_session_reused(scon)) {
345 ver = SSL_version(scon);
346 if (ver == TLS1_VERSION)
348 else if (ver == SSL3_VERSION)
356 totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
359 ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
360 nConn, totalTime, ((double)nConn / totalTime), bytes_read);
362 ("%d connections in %ld real seconds, %ld bytes read per connection\n",
363 nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
374 * doConnection - make a connection
376 static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx)
383 if ((conn = BIO_new(BIO_s_connect())) == NULL)
386 BIO_set_conn_hostname(conn, host);
389 serverCon = SSL_new(ctx);
392 SSL_set_connect_state(serverCon);
395 SSL_set_bio(serverCon, conn, conn);
397 /* ok, lets connect */
399 i = SSL_connect(serverCon);
400 if (BIO_sock_should_retry(i)) {
401 BIO_printf(bio_err, "DELAY\n");
403 i = SSL_get_fd(serverCon);
406 openssl_fdset(i, &readfds);
408 * Note: under VMS with SOCKETSHR the 2nd parameter is currently
409 * of type (int *) whereas under other systems it is (void *) if
410 * you don't have a cast it will choke the compiler: if you do
411 * have a cast then you can either go for (int *) or (void *).
413 select(width, (void *)&readfds, NULL, NULL, NULL);
419 BIO_printf(bio_err, "ERROR\n");
420 if (verify_args.error != X509_V_OK)
421 BIO_printf(bio_err, "verify error:%s\n",
422 X509_verify_cert_error_string(verify_args.error));
424 ERR_print_errors(bio_err);
432 #endif /* OPENSSL_NO_SOCK */