xref: /openbsd/usr.bin/openssl/s_time.c (revision e7718ada)
1*e7718adaStb /* $OpenBSD: s_time.c,v 1.38 2023/03/06 14:32:06 tb Exp $ */
2dab3f910Sjsing /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3dab3f910Sjsing  * All rights reserved.
4dab3f910Sjsing  *
5dab3f910Sjsing  * This package is an SSL implementation written
6dab3f910Sjsing  * by Eric Young (eay@cryptsoft.com).
7dab3f910Sjsing  * The implementation was written so as to conform with Netscapes SSL.
8dab3f910Sjsing  *
9dab3f910Sjsing  * This library is free for commercial and non-commercial use as long as
10dab3f910Sjsing  * the following conditions are aheared to.  The following conditions
11dab3f910Sjsing  * apply to all code found in this distribution, be it the RC4, RSA,
12dab3f910Sjsing  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13dab3f910Sjsing  * included with this distribution is covered by the same copyright terms
14dab3f910Sjsing  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15dab3f910Sjsing  *
16dab3f910Sjsing  * Copyright remains Eric Young's, and as such any Copyright notices in
17dab3f910Sjsing  * the code are not to be removed.
18dab3f910Sjsing  * If this package is used in a product, Eric Young should be given attribution
19dab3f910Sjsing  * as the author of the parts of the library used.
20dab3f910Sjsing  * This can be in the form of a textual message at program startup or
21dab3f910Sjsing  * in documentation (online or textual) provided with the package.
22dab3f910Sjsing  *
23dab3f910Sjsing  * Redistribution and use in source and binary forms, with or without
24dab3f910Sjsing  * modification, are permitted provided that the following conditions
25dab3f910Sjsing  * are met:
26dab3f910Sjsing  * 1. Redistributions of source code must retain the copyright
27dab3f910Sjsing  *    notice, this list of conditions and the following disclaimer.
28dab3f910Sjsing  * 2. Redistributions in binary form must reproduce the above copyright
29dab3f910Sjsing  *    notice, this list of conditions and the following disclaimer in the
30dab3f910Sjsing  *    documentation and/or other materials provided with the distribution.
31dab3f910Sjsing  * 3. All advertising materials mentioning features or use of this software
32dab3f910Sjsing  *    must display the following acknowledgement:
33dab3f910Sjsing  *    "This product includes cryptographic software written by
34dab3f910Sjsing  *     Eric Young (eay@cryptsoft.com)"
35dab3f910Sjsing  *    The word 'cryptographic' can be left out if the rouines from the library
36dab3f910Sjsing  *    being used are not cryptographic related :-).
37dab3f910Sjsing  * 4. If you include any Windows specific code (or a derivative thereof) from
38dab3f910Sjsing  *    the apps directory (application code) you must include an acknowledgement:
39dab3f910Sjsing  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40dab3f910Sjsing  *
41dab3f910Sjsing  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42dab3f910Sjsing  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43dab3f910Sjsing  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44dab3f910Sjsing  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45dab3f910Sjsing  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46dab3f910Sjsing  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47dab3f910Sjsing  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48dab3f910Sjsing  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49dab3f910Sjsing  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50dab3f910Sjsing  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51dab3f910Sjsing  * SUCH DAMAGE.
52dab3f910Sjsing  *
53dab3f910Sjsing  * The licence and distribution terms for any publically available version or
54dab3f910Sjsing  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55dab3f910Sjsing  * copied and put under another distribution licence
56dab3f910Sjsing  * [including the GNU Public Licence.]
57dab3f910Sjsing  */
58dab3f910Sjsing 
59dab3f910Sjsing /*-----------------------------------------
60dab3f910Sjsing    s_time - SSL client connection timer program
61dab3f910Sjsing    Written and donated by Larry Streepy <streepy@healthcare.com>
62dab3f910Sjsing   -----------------------------------------*/
63dab3f910Sjsing 
647b69b2e4Sderaadt #include <sys/types.h>
65dab3f910Sjsing #include <sys/socket.h>
66dab3f910Sjsing 
67dab3f910Sjsing #include <stdio.h>
68dab3f910Sjsing #include <stdlib.h>
69dab3f910Sjsing #include <limits.h>
70dab3f910Sjsing #include <string.h>
71dab3f910Sjsing #include <unistd.h>
727b69b2e4Sderaadt #include <poll.h>
73dab3f910Sjsing 
74dab3f910Sjsing #include "apps.h"
75dab3f910Sjsing 
76dab3f910Sjsing #include <openssl/err.h>
77dab3f910Sjsing #include <openssl/pem.h>
78dab3f910Sjsing #include <openssl/ssl.h>
79dab3f910Sjsing #include <openssl/x509.h>
80dab3f910Sjsing 
81dab3f910Sjsing #include "s_apps.h"
82dab3f910Sjsing 
83dab3f910Sjsing #define SSL_CONNECT_NAME	"localhost:4433"
84dab3f910Sjsing 
85dab3f910Sjsing #define BUFSIZZ 1024*10
86dab3f910Sjsing 
87dab3f910Sjsing #define MYBUFSIZ 1024*8
88dab3f910Sjsing 
89dab3f910Sjsing #define SECONDS	30
90abbe1350Sjsing extern int verify_depth;
91dab3f910Sjsing 
92dab3f910Sjsing static void s_time_usage(void);
932d831f52Scheloha static int run_test(SSL *);
941ceb7bbbScheloha static int benchmark(int);
9511c2e033Scheloha static void print_tally_mark(SSL *);
96dab3f910Sjsing 
97dab3f910Sjsing static SSL_CTX *tm_ctx = NULL;
98dab3f910Sjsing static const SSL_METHOD *s_time_meth = NULL;
99dab3f910Sjsing static long bytes_read = 0;
100dab3f910Sjsing 
101efc7e65fStb static struct {
1028c2803e9Sjsing 	int bugs;
1038c2803e9Sjsing 	char *CAfile;
1048c2803e9Sjsing 	char *CApath;
1058c2803e9Sjsing 	char *certfile;
1068c2803e9Sjsing 	char *cipher;
1078c2803e9Sjsing 	char *host;
1088c2803e9Sjsing 	char *keyfile;
109e7036f67Sderaadt 	time_t maxtime;
1108c2803e9Sjsing 	int nbio;
111f33eb045Slteo 	int no_shutdown;
1128c2803e9Sjsing 	int perform;
1138c2803e9Sjsing 	int verify;
1148c2803e9Sjsing 	int verify_depth;
1158c2803e9Sjsing 	char *www_path;
116*e7718adaStb } cfg;
1178c2803e9Sjsing 
118ea149709Sguenther static const struct option s_time_options[] = {
119dab3f910Sjsing 	{
1208c2803e9Sjsing 		.name = "bugs",
1218c2803e9Sjsing 		.desc = "Enable workarounds for known SSL/TLS bugs",
1228c2803e9Sjsing 		.type = OPTION_FLAG,
123*e7718adaStb 		.opt.flag = &cfg.bugs,
1248c2803e9Sjsing 	},
1258c2803e9Sjsing 	{
1268c2803e9Sjsing 		.name = "CAfile",
1278c2803e9Sjsing 		.argname = "file",
1288c2803e9Sjsing 		.desc = "File containing trusted certificates in PEM format",
1298c2803e9Sjsing 		.type = OPTION_ARG,
130*e7718adaStb 		.opt.arg = &cfg.CAfile,
1318c2803e9Sjsing 	},
1328c2803e9Sjsing 	{
1338c2803e9Sjsing 		.name = "CApath",
1348c2803e9Sjsing 		.argname = "path",
1358c2803e9Sjsing 		.desc = "Directory containing trusted certificates",
1368c2803e9Sjsing 		.type = OPTION_ARG,
137*e7718adaStb 		.opt.arg = &cfg.CApath,
1388c2803e9Sjsing 	},
1398c2803e9Sjsing 	{
1408c2803e9Sjsing 		.name = "cert",
1418c2803e9Sjsing 		.argname = "file",
1428c2803e9Sjsing 		.desc = "Client certificate to use, if one is requested",
1438c2803e9Sjsing 		.type = OPTION_ARG,
144*e7718adaStb 		.opt.arg = &cfg.certfile,
1458c2803e9Sjsing 	},
1468c2803e9Sjsing 	{
1478c2803e9Sjsing 		.name = "cipher",
1488c2803e9Sjsing 		.argname = "list",
1498c2803e9Sjsing 		.desc = "List of cipher suites to send to the server",
1508c2803e9Sjsing 		.type = OPTION_ARG,
151*e7718adaStb 		.opt.arg = &cfg.cipher,
1528c2803e9Sjsing 	},
1538c2803e9Sjsing 	{
1548c2803e9Sjsing 		.name = "connect",
1558c2803e9Sjsing 		.argname = "host:port",
1568c2803e9Sjsing 		.desc = "Host and port to connect to (default "
1578c2803e9Sjsing 		    SSL_CONNECT_NAME ")",
1588c2803e9Sjsing 		.type = OPTION_ARG,
159*e7718adaStb 		.opt.arg = &cfg.host,
1608c2803e9Sjsing 	},
1618c2803e9Sjsing 	{
1628c2803e9Sjsing 		.name = "key",
1638c2803e9Sjsing 		.argname = "file",
1648c2803e9Sjsing 		.desc = "Client private key to use, if one is required",
1658c2803e9Sjsing 		.type = OPTION_ARG,
166*e7718adaStb 		.opt.arg = &cfg.keyfile,
1678c2803e9Sjsing 	},
1688c2803e9Sjsing 	{
1698c2803e9Sjsing 		.name = "nbio",
1708c2803e9Sjsing 		.desc = "Use non-blocking I/O",
1718c2803e9Sjsing 		.type = OPTION_FLAG,
172*e7718adaStb 		.opt.flag = &cfg.nbio,
1738c2803e9Sjsing 	},
1748c2803e9Sjsing 	{
1758c2803e9Sjsing 		.name = "new",
1768c2803e9Sjsing 		.desc = "Use a new session ID for each connection",
1778c2803e9Sjsing 		.type = OPTION_VALUE,
178*e7718adaStb 		.opt.value = &cfg.perform,
1798c2803e9Sjsing 		.value = 1,
1808c2803e9Sjsing 	},
1818c2803e9Sjsing 	{
182f33eb045Slteo 		.name = "no_shutdown",
183f33eb045Slteo 		.desc = "Shut down the connection without notifying the server",
184f33eb045Slteo 		.type = OPTION_FLAG,
185*e7718adaStb 		.opt.flag = &cfg.no_shutdown,
186f33eb045Slteo 	},
187f33eb045Slteo 	{
1888c2803e9Sjsing 		.name = "reuse",
1898c2803e9Sjsing 		.desc = "Reuse the same session ID for each connection",
1908c2803e9Sjsing 		.type = OPTION_VALUE,
191*e7718adaStb 		.opt.value = &cfg.perform,
1928c2803e9Sjsing 		.value = 2,
1938c2803e9Sjsing 	},
1948c2803e9Sjsing 	{
1958c2803e9Sjsing 		.name = "time",
1968c2803e9Sjsing 		.argname = "seconds",
1978c2803e9Sjsing 		.desc = "Duration to perform timing tests for (default 30)",
198ea1128d8Sderaadt 		.type = OPTION_ARG_TIME,
199*e7718adaStb 		.opt.tvalue = &cfg.maxtime,
2008c2803e9Sjsing 	},
2018c2803e9Sjsing 	{
2028c2803e9Sjsing 		.name = "verify",
2038c2803e9Sjsing 		.argname = "depth",
2048c2803e9Sjsing 		.desc = "Enable peer certificate verification with given depth",
2058c2803e9Sjsing 		.type = OPTION_ARG_INT,
206*e7718adaStb 		.opt.value = &cfg.verify_depth,
2078c2803e9Sjsing 	},
2088c2803e9Sjsing 	{
2098c2803e9Sjsing 		.name = "www",
2108c2803e9Sjsing 		.argname = "page",
2118c2803e9Sjsing 		.desc = "Page to GET from the server (default none)",
2128c2803e9Sjsing 		.type = OPTION_ARG,
213*e7718adaStb 		.opt.arg = &cfg.www_path,
2148c2803e9Sjsing 	},
2158c2803e9Sjsing 	{ NULL },
2168c2803e9Sjsing };
217dab3f910Sjsing 
218dab3f910Sjsing static void
s_time_usage(void)219dab3f910Sjsing s_time_usage(void)
220dab3f910Sjsing {
2218c2803e9Sjsing 	fprintf(stderr,
2228c2803e9Sjsing 	    "usage: s_time "
2238c2803e9Sjsing 	    "[-bugs] [-CAfile file] [-CApath directory] [-cert file]\n"
2248c2803e9Sjsing 	    "    [-cipher cipherlist] [-connect host:port] [-key keyfile]\n"
225f33eb045Slteo 	    "    [-nbio] [-new] [-no_shutdown] [-reuse] [-time seconds]\n"
2268c2803e9Sjsing 	    "    [-verify depth] [-www page]\n\n");
2278c2803e9Sjsing 	options_usage(s_time_options);
228dab3f910Sjsing }
229dab3f910Sjsing 
230dab3f910Sjsing /***********************************************************************
231dab3f910Sjsing  * MAIN - main processing area for client
232dab3f910Sjsing  *			real name depends on MONOLITH
233dab3f910Sjsing  */
234dab3f910Sjsing int
s_time_main(int argc,char ** argv)235dab3f910Sjsing s_time_main(int argc, char **argv)
236dab3f910Sjsing {
237e7036f67Sderaadt 	int ret = 1;
238dab3f910Sjsing 
239ebcad598Smestre 	if (pledge("stdio rpath inet dns", NULL) == -1) {
2409bc487adSdoug 		perror("pledge");
241e370f0eeSdoug 		exit(1);
242e370f0eeSdoug 	}
2439bc487adSdoug 
2441a76ec68Scheloha 	s_time_meth = TLS_client_method();
245dab3f910Sjsing 
2468c2803e9Sjsing 	verify_depth = 0;
2478c2803e9Sjsing 
248*e7718adaStb 	memset(&cfg, 0, sizeof(cfg));
2498c2803e9Sjsing 
250*e7718adaStb 	cfg.host = SSL_CONNECT_NAME;
251*e7718adaStb 	cfg.maxtime = SECONDS;
252*e7718adaStb 	cfg.perform = 3;
253*e7718adaStb 	cfg.verify = SSL_VERIFY_NONE;
254*e7718adaStb 	cfg.verify_depth = -1;
2558c2803e9Sjsing 
2568c2803e9Sjsing 	if (options_parse(argc, argv, s_time_options, NULL, NULL) != 0) {
2578c2803e9Sjsing 		s_time_usage();
258dab3f910Sjsing 		goto end;
2598c2803e9Sjsing 	}
2608c2803e9Sjsing 
261*e7718adaStb 	if (cfg.verify_depth >= 0) {
262*e7718adaStb 		cfg.verify = SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE;
263*e7718adaStb 		verify_depth = cfg.verify_depth;
2648c2803e9Sjsing 		BIO_printf(bio_err, "verify depth is %d\n", verify_depth);
2658c2803e9Sjsing 	}
2668c2803e9Sjsing 
267*e7718adaStb 	if (cfg.www_path != NULL &&
268*e7718adaStb 	    strlen(cfg.www_path) > MYBUFSIZ - 100) {
2698c2803e9Sjsing 		BIO_printf(bio_err, "-www option too long\n");
2708c2803e9Sjsing 		goto end;
2718c2803e9Sjsing 	}
272dab3f910Sjsing 
273dab3f910Sjsing 	if ((tm_ctx = SSL_CTX_new(s_time_meth)) == NULL)
274dab3f910Sjsing 		return (1);
275dab3f910Sjsing 
276dab3f910Sjsing 	SSL_CTX_set_quiet_shutdown(tm_ctx, 1);
277dab3f910Sjsing 
278*e7718adaStb 	if (cfg.bugs)
279dab3f910Sjsing 		SSL_CTX_set_options(tm_ctx, SSL_OP_ALL);
280abbe1350Sjsing 
281*e7718adaStb 	if (cfg.cipher != NULL) {
282*e7718adaStb 		if (!SSL_CTX_set_cipher_list(tm_ctx, cfg.cipher)) {
283abbe1350Sjsing 			BIO_printf(bio_err, "error setting cipher list\n");
284abbe1350Sjsing 			ERR_print_errors(bio_err);
285abbe1350Sjsing 			goto end;
286abbe1350Sjsing 		}
287abbe1350Sjsing 	}
288abbe1350Sjsing 
289*e7718adaStb 	SSL_CTX_set_verify(tm_ctx, cfg.verify, NULL);
2901292f75aSbcook 
291*e7718adaStb 	if (!set_cert_stuff(tm_ctx, cfg.certfile,
292*e7718adaStb 	    cfg.keyfile))
293dab3f910Sjsing 		goto end;
294dab3f910Sjsing 
295*e7718adaStb 	if ((!SSL_CTX_load_verify_locations(tm_ctx, cfg.CAfile,
296*e7718adaStb 	    cfg.CApath)) ||
297dab3f910Sjsing 	    (!SSL_CTX_set_default_verify_paths(tm_ctx))) {
298dab3f910Sjsing 		/*
299dab3f910Sjsing 		 * BIO_printf(bio_err,"error setting default verify
300dab3f910Sjsing 		 * locations\n");
301dab3f910Sjsing 		 */
302dab3f910Sjsing 		ERR_print_errors(bio_err);
303dab3f910Sjsing 		/* goto end; */
304dab3f910Sjsing 	}
305dab3f910Sjsing 
306a81eedd5Scheloha 	/* Loop and time how long it takes to make connections */
307*e7718adaStb 	if (cfg.perform & 1) {
308e7036f67Sderaadt 		printf("Collecting connection statistics for %lld seconds\n",
309*e7718adaStb 		    (long long)cfg.maxtime);
3101ceb7bbbScheloha 		if (benchmark(0))
311dab3f910Sjsing 			goto end;
312a81eedd5Scheloha 	}
313dab3f910Sjsing 	/*
314dab3f910Sjsing 	 * Now loop and time connections using the same session id over and
315dab3f910Sjsing 	 * over
316dab3f910Sjsing 	 */
317*e7718adaStb 	if (cfg.perform & 2) {
318dab3f910Sjsing 		printf("\n\nNow timing with session id reuse.\n");
3191ceb7bbbScheloha 		if (benchmark(1))
320dab3f910Sjsing 			goto end;
321a81eedd5Scheloha 	}
322dab3f910Sjsing 	ret = 0;
323dab3f910Sjsing  end:
324dab3f910Sjsing 	if (tm_ctx != NULL) {
325dab3f910Sjsing 		SSL_CTX_free(tm_ctx);
326dab3f910Sjsing 		tm_ctx = NULL;
327dab3f910Sjsing 	}
328dab3f910Sjsing 
329dab3f910Sjsing 	return (ret);
330dab3f910Sjsing }
331dab3f910Sjsing 
332dab3f910Sjsing /***********************************************************************
3332d831f52Scheloha  * run_test - make a connection, get a file, and shut down the connection
3342d831f52Scheloha  *
335dab3f910Sjsing  * Args:
3365e66bdb3Scheloha  *		scon	= SSL connection
337dab3f910Sjsing  * Returns:
3385e66bdb3Scheloha  *		1 on success, 0 on error
339dab3f910Sjsing  */
3405e66bdb3Scheloha static int
run_test(SSL * scon)3412d831f52Scheloha run_test(SSL *scon)
342dab3f910Sjsing {
3432d831f52Scheloha 	char buf[1024 * 8];
3447b69b2e4Sderaadt 	struct pollfd pfd[1];
3457b69b2e4Sderaadt 	BIO *conn;
3461292f75aSbcook 	long verify_error;
3472d831f52Scheloha 	int i, retval;
348dab3f910Sjsing 
349dab3f910Sjsing 	if ((conn = BIO_new(BIO_s_connect())) == NULL)
3505e66bdb3Scheloha 		return 0;
351*e7718adaStb 	BIO_set_conn_hostname(conn, cfg.host);
3525e66bdb3Scheloha 	SSL_set_connect_state(scon);
3535e66bdb3Scheloha 	SSL_set_bio(scon, conn, conn);
354dab3f910Sjsing 	for (;;) {
3555e66bdb3Scheloha 		i = SSL_connect(scon);
356dab3f910Sjsing 		if (BIO_sock_should_retry(i)) {
357dab3f910Sjsing 			BIO_printf(bio_err, "DELAY\n");
3585e66bdb3Scheloha 			pfd[0].fd = SSL_get_fd(scon);
3597b69b2e4Sderaadt 			pfd[0].events = POLLIN;
3607b69b2e4Sderaadt 			poll(pfd, 1, -1);
361dab3f910Sjsing 			continue;
362dab3f910Sjsing 		}
363dab3f910Sjsing 		break;
364dab3f910Sjsing 	}
365dab3f910Sjsing 	if (i <= 0) {
366dab3f910Sjsing 		BIO_printf(bio_err, "ERROR\n");
3675e66bdb3Scheloha 		verify_error = SSL_get_verify_result(scon);
368dab3f910Sjsing 		if (verify_error != X509_V_OK)
369dab3f910Sjsing 			BIO_printf(bio_err, "verify error:%s\n",
370dab3f910Sjsing 			    X509_verify_cert_error_string(verify_error));
371dab3f910Sjsing 		else
372dab3f910Sjsing 			ERR_print_errors(bio_err);
3735e66bdb3Scheloha 		return 0;
374dab3f910Sjsing 	}
375*e7718adaStb 	if (cfg.www_path != NULL) {
3762d831f52Scheloha 		retval = snprintf(buf, sizeof buf,
377*e7718adaStb 		    "GET %s HTTP/1.0\r\n\r\n", cfg.www_path);
378515e489cSderaadt 		if (retval < 0 || retval >= sizeof buf) {
3792d831f52Scheloha 			fprintf(stderr, "URL too long\n");
3802d831f52Scheloha 			return 0;
3812d831f52Scheloha 		}
3825f8f2a30Scheloha 		if (SSL_write(scon, buf, retval) != retval)
3835f8f2a30Scheloha 			return 0;
3842d831f52Scheloha 		while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
3852d831f52Scheloha 			bytes_read += i;
3862d831f52Scheloha 	}
387*e7718adaStb 	if (cfg.no_shutdown)
3882d831f52Scheloha 		SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN |
3892d831f52Scheloha 		    SSL_RECEIVED_SHUTDOWN);
3902d831f52Scheloha 	else
3912d831f52Scheloha 		SSL_shutdown(scon);
3925e66bdb3Scheloha 	return 1;
393dab3f910Sjsing }
3941ceb7bbbScheloha 
39511c2e033Scheloha static void
print_tally_mark(SSL * scon)39611c2e033Scheloha print_tally_mark(SSL *scon)
39711c2e033Scheloha {
39811c2e033Scheloha 	int ver;
39911c2e033Scheloha 
40011c2e033Scheloha 	if (SSL_session_reused(scon))
40111c2e033Scheloha 		ver = 'r';
40211c2e033Scheloha 	else {
40311c2e033Scheloha 		ver = SSL_version(scon);
40411c2e033Scheloha 		if (ver == TLS1_VERSION)
40511c2e033Scheloha 			ver = 't';
40611c2e033Scheloha 		else
40711c2e033Scheloha 			ver = '*';
40811c2e033Scheloha 	}
40911c2e033Scheloha 	fputc(ver, stdout);
41011c2e033Scheloha 	fflush(stdout);
41111c2e033Scheloha }
41211c2e033Scheloha 
4131ceb7bbbScheloha static int
benchmark(int reuse_session)4141ceb7bbbScheloha benchmark(int reuse_session)
4151ceb7bbbScheloha {
416ce92332eScheloha 	double elapsed, totalTime;
4171ceb7bbbScheloha 	int nConn = 0;
4181ceb7bbbScheloha 	SSL *scon = NULL;
4191ceb7bbbScheloha 	int ret = 1;
4201ceb7bbbScheloha 
4211ceb7bbbScheloha 	if (reuse_session) {
4221ceb7bbbScheloha 		/* Get an SSL object so we can reuse the session id */
4235e66bdb3Scheloha 		if ((scon = SSL_new(tm_ctx)) == NULL)
4245e66bdb3Scheloha 			goto end;
4252d831f52Scheloha 		if (!run_test(scon)) {
4261ceb7bbbScheloha 			fprintf(stderr, "Unable to get connection\n");
4271ceb7bbbScheloha 			goto end;
4281ceb7bbbScheloha 		}
4291ceb7bbbScheloha 		printf("starting\n");
4301ceb7bbbScheloha 	}
4311ceb7bbbScheloha 
4321ceb7bbbScheloha 	nConn = 0;
4331ceb7bbbScheloha 	bytes_read = 0;
4341ceb7bbbScheloha 
435ce92332eScheloha 	app_timer_real(TM_RESET);
436ce92332eScheloha 	app_timer_user(TM_RESET);
4371ceb7bbbScheloha 	for (;;) {
438ce92332eScheloha 		elapsed = app_timer_real(TM_GET);
439*e7718adaStb 		if (elapsed > cfg.maxtime)
4401ceb7bbbScheloha 			break;
4415e66bdb3Scheloha 		if (scon == NULL) {
4425e66bdb3Scheloha 			if ((scon = SSL_new(tm_ctx)) == NULL)
4435e66bdb3Scheloha 				goto end;
4445e66bdb3Scheloha 		}
4452d831f52Scheloha 		if (!run_test(scon))
4461ceb7bbbScheloha 			goto end;
4471ceb7bbbScheloha 		nConn += 1;
44811c2e033Scheloha 		print_tally_mark(scon);
4491ceb7bbbScheloha 		if (!reuse_session) {
4501ceb7bbbScheloha 			SSL_free(scon);
4511ceb7bbbScheloha 			scon = NULL;
4521ceb7bbbScheloha 		}
4531ceb7bbbScheloha 	}
454ce92332eScheloha 	totalTime = app_timer_user(TM_GET);
4551ceb7bbbScheloha 
4561ceb7bbbScheloha 	printf("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
4571ceb7bbbScheloha 	    nConn, totalTime, ((double) nConn / totalTime), bytes_read);
458ce92332eScheloha 	printf("%d connections in %.0f real seconds, %ld bytes read per connection\n",
4599c9e7ec3Stb 	    nConn, elapsed, nConn > 0 ? bytes_read / nConn : 0);
4601ceb7bbbScheloha 
4611ceb7bbbScheloha 	ret = 0;
4621ceb7bbbScheloha  end:
4631ceb7bbbScheloha 	SSL_free(scon);
4641ceb7bbbScheloha 	return ret;
4651ceb7bbbScheloha }
466