xref: /freebsd/crypto/openssl/apps/s_time.c (revision f579bf8e)
174664626SKris Kennaway /* apps/s_time.c */
274664626SKris Kennaway /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
374664626SKris Kennaway  * All rights reserved.
474664626SKris Kennaway  *
574664626SKris Kennaway  * This package is an SSL implementation written
674664626SKris Kennaway  * by Eric Young (eay@cryptsoft.com).
774664626SKris Kennaway  * The implementation was written so as to conform with Netscapes SSL.
874664626SKris Kennaway  *
974664626SKris Kennaway  * This library is free for commercial and non-commercial use as long as
1074664626SKris Kennaway  * the following conditions are aheared to.  The following conditions
1174664626SKris Kennaway  * apply to all code found in this distribution, be it the RC4, RSA,
1274664626SKris Kennaway  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1374664626SKris Kennaway  * included with this distribution is covered by the same copyright terms
1474664626SKris Kennaway  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1574664626SKris Kennaway  *
1674664626SKris Kennaway  * Copyright remains Eric Young's, and as such any Copyright notices in
1774664626SKris Kennaway  * the code are not to be removed.
1874664626SKris Kennaway  * If this package is used in a product, Eric Young should be given attribution
1974664626SKris Kennaway  * as the author of the parts of the library used.
2074664626SKris Kennaway  * This can be in the form of a textual message at program startup or
2174664626SKris Kennaway  * in documentation (online or textual) provided with the package.
2274664626SKris Kennaway  *
2374664626SKris Kennaway  * Redistribution and use in source and binary forms, with or without
2474664626SKris Kennaway  * modification, are permitted provided that the following conditions
2574664626SKris Kennaway  * are met:
2674664626SKris Kennaway  * 1. Redistributions of source code must retain the copyright
2774664626SKris Kennaway  *    notice, this list of conditions and the following disclaimer.
2874664626SKris Kennaway  * 2. Redistributions in binary form must reproduce the above copyright
2974664626SKris Kennaway  *    notice, this list of conditions and the following disclaimer in the
3074664626SKris Kennaway  *    documentation and/or other materials provided with the distribution.
3174664626SKris Kennaway  * 3. All advertising materials mentioning features or use of this software
3274664626SKris Kennaway  *    must display the following acknowledgement:
3374664626SKris Kennaway  *    "This product includes cryptographic software written by
3474664626SKris Kennaway  *     Eric Young (eay@cryptsoft.com)"
3574664626SKris Kennaway  *    The word 'cryptographic' can be left out if the rouines from the library
3674664626SKris Kennaway  *    being used are not cryptographic related :-).
3774664626SKris Kennaway  * 4. If you include any Windows specific code (or a derivative thereof) from
3874664626SKris Kennaway  *    the apps directory (application code) you must include an acknowledgement:
3974664626SKris Kennaway  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4074664626SKris Kennaway  *
4174664626SKris Kennaway  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4274664626SKris Kennaway  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4374664626SKris Kennaway  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4474664626SKris Kennaway  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4574664626SKris Kennaway  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4674664626SKris Kennaway  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4774664626SKris Kennaway  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4874664626SKris Kennaway  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4974664626SKris Kennaway  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5074664626SKris Kennaway  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5174664626SKris Kennaway  * SUCH DAMAGE.
5274664626SKris Kennaway  *
5374664626SKris Kennaway  * The licence and distribution terms for any publically available version or
5474664626SKris Kennaway  * derivative of this code cannot be changed.  i.e. this code cannot simply be
5574664626SKris Kennaway  * copied and put under another distribution licence
5674664626SKris Kennaway  * [including the GNU Public Licence.]
5774664626SKris Kennaway  */
5874664626SKris Kennaway 
5974664626SKris Kennaway #define NO_SHUTDOWN
6074664626SKris Kennaway 
6174664626SKris Kennaway /*-----------------------------------------
6274664626SKris Kennaway    s_time - SSL client connection timer program
6374664626SKris Kennaway    Written and donated by Larry Streepy <streepy@healthcare.com>
6474664626SKris Kennaway   -----------------------------------------*/
6574664626SKris Kennaway 
6674664626SKris Kennaway #include <stdio.h>
6774664626SKris Kennaway #include <stdlib.h>
6874664626SKris Kennaway #include <string.h>
6974664626SKris Kennaway 
7074664626SKris Kennaway #ifdef NO_STDIO
7174664626SKris Kennaway #define APPS_WIN16
7274664626SKris Kennaway #endif
7374664626SKris Kennaway #define USE_SOCKETS
7474664626SKris Kennaway #include <openssl/x509.h>
7574664626SKris Kennaway #include <openssl/ssl.h>
7674664626SKris Kennaway #include <openssl/pem.h>
7774664626SKris Kennaway #include "apps.h"
7874664626SKris Kennaway #include "s_apps.h"
7974664626SKris Kennaway #include <openssl/err.h>
8074664626SKris Kennaway #ifdef WIN32_STUFF
8174664626SKris Kennaway #include "winmain.h"
8274664626SKris Kennaway #include "wintext.h"
8374664626SKris Kennaway #endif
8474664626SKris Kennaway 
8574664626SKris Kennaway #if !defined(MSDOS) && (!defined(VMS) || defined(__DECC))
8674664626SKris Kennaway #define TIMES
8774664626SKris Kennaway #endif
8874664626SKris Kennaway 
8974664626SKris Kennaway #ifndef _IRIX
9074664626SKris Kennaway #include <time.h>
9174664626SKris Kennaway #endif
9274664626SKris Kennaway #ifdef TIMES
9374664626SKris Kennaway #include <sys/types.h>
9474664626SKris Kennaway #include <sys/times.h>
9574664626SKris Kennaway #endif
9674664626SKris Kennaway 
9774664626SKris Kennaway /* Depending on the VMS version, the tms structure is perhaps defined.
9874664626SKris Kennaway    The __TMS macro will show if it was.  If it wasn't defined, we should
9974664626SKris Kennaway    undefine TIMES, since that tells the rest of the program how things
10074664626SKris Kennaway    should be handled.				-- Richard Levitte */
10174664626SKris Kennaway #if defined(VMS) && defined(__DECC) && !defined(__TMS)
10274664626SKris Kennaway #undef TIMES
10374664626SKris Kennaway #endif
10474664626SKris Kennaway 
10574664626SKris Kennaway #ifndef TIMES
10674664626SKris Kennaway #include <sys/timeb.h>
10774664626SKris Kennaway #endif
10874664626SKris Kennaway 
10974664626SKris Kennaway #ifdef _AIX
11074664626SKris Kennaway #include <sys/select.h>
11174664626SKris Kennaway #endif
11274664626SKris Kennaway 
11374664626SKris Kennaway #if defined(sun) || defined(__ultrix)
11474664626SKris Kennaway #define _POSIX_SOURCE
11574664626SKris Kennaway #include <limits.h>
11674664626SKris Kennaway #include <sys/param.h>
11774664626SKris Kennaway #endif
11874664626SKris Kennaway 
11974664626SKris Kennaway /* The following if from times(3) man page.  It may need to be changed
12074664626SKris Kennaway */
12174664626SKris Kennaway #ifndef HZ
12274664626SKris Kennaway #ifndef CLK_TCK
12374664626SKris Kennaway #define HZ      100.0
12474664626SKris Kennaway #else /* CLK_TCK */
12574664626SKris Kennaway #define HZ ((double)CLK_TCK)
12674664626SKris Kennaway #endif
12774664626SKris Kennaway #endif
12874664626SKris Kennaway 
12974664626SKris Kennaway #undef PROG
13074664626SKris Kennaway #define PROG s_time_main
13174664626SKris Kennaway 
13274664626SKris Kennaway #undef ioctl
13374664626SKris Kennaway #define ioctl ioctlsocket
13474664626SKris Kennaway 
13574664626SKris Kennaway #define SSL_CONNECT_NAME	"localhost:4433"
13674664626SKris Kennaway 
13774664626SKris Kennaway /*#define TEST_CERT "client.pem" */ /* no default cert. */
13874664626SKris Kennaway 
13974664626SKris Kennaway #undef BUFSIZZ
14074664626SKris Kennaway #define BUFSIZZ 1024*10
14174664626SKris Kennaway 
14274664626SKris Kennaway #define min(a,b) (((a) < (b)) ? (a) : (b))
14374664626SKris Kennaway #define max(a,b) (((a) > (b)) ? (a) : (b))
14474664626SKris Kennaway 
14574664626SKris Kennaway #undef SECONDS
14674664626SKris Kennaway #define SECONDS	30
14774664626SKris Kennaway extern int verify_depth;
14874664626SKris Kennaway extern int verify_error;
14974664626SKris Kennaway 
15074664626SKris Kennaway static void s_time_usage(void);
15174664626SKris Kennaway static int parseArgs( int argc, char **argv );
15274664626SKris Kennaway static SSL *doConnection( SSL *scon );
15374664626SKris Kennaway static void s_time_init(void);
15474664626SKris Kennaway 
15574664626SKris Kennaway /***********************************************************************
15674664626SKris Kennaway  * Static data declarations
15774664626SKris Kennaway  */
15874664626SKris Kennaway 
15974664626SKris Kennaway /* static char *port=PORT_STR;*/
16074664626SKris Kennaway static char *host=SSL_CONNECT_NAME;
16174664626SKris Kennaway static char *t_cert_file=NULL;
16274664626SKris Kennaway static char *t_key_file=NULL;
16374664626SKris Kennaway static char *CApath=NULL;
16474664626SKris Kennaway static char *CAfile=NULL;
16574664626SKris Kennaway static char *tm_cipher=NULL;
16674664626SKris Kennaway static int tm_verify = SSL_VERIFY_NONE;
16774664626SKris Kennaway static int maxTime = SECONDS;
16874664626SKris Kennaway static SSL_CTX *tm_ctx=NULL;
16974664626SKris Kennaway static SSL_METHOD *s_time_meth=NULL;
17074664626SKris Kennaway static char *s_www_path=NULL;
17174664626SKris Kennaway static long bytes_read=0;
17274664626SKris Kennaway static int st_bugs=0;
17374664626SKris Kennaway static int perform=0;
17474664626SKris Kennaway #ifdef FIONBIO
17574664626SKris Kennaway static int t_nbio=0;
17674664626SKris Kennaway #endif
17774664626SKris Kennaway #ifdef WIN32
17874664626SKris Kennaway static int exitNow = 0;		/* Set when it's time to exit main */
17974664626SKris Kennaway #endif
18074664626SKris Kennaway 
18174664626SKris Kennaway static void s_time_init(void)
18274664626SKris Kennaway 	{
18374664626SKris Kennaway 	host=SSL_CONNECT_NAME;
18474664626SKris Kennaway 	t_cert_file=NULL;
18574664626SKris Kennaway 	t_key_file=NULL;
18674664626SKris Kennaway 	CApath=NULL;
18774664626SKris Kennaway 	CAfile=NULL;
18874664626SKris Kennaway 	tm_cipher=NULL;
18974664626SKris Kennaway 	tm_verify = SSL_VERIFY_NONE;
19074664626SKris Kennaway 	maxTime = SECONDS;
19174664626SKris Kennaway 	tm_ctx=NULL;
19274664626SKris Kennaway 	s_time_meth=NULL;
19374664626SKris Kennaway 	s_www_path=NULL;
19474664626SKris Kennaway 	bytes_read=0;
19574664626SKris Kennaway 	st_bugs=0;
19674664626SKris Kennaway 	perform=0;
19774664626SKris Kennaway 
19874664626SKris Kennaway #ifdef FIONBIO
19974664626SKris Kennaway 	t_nbio=0;
20074664626SKris Kennaway #endif
20174664626SKris Kennaway #ifdef WIN32
20274664626SKris Kennaway 	exitNow = 0;		/* Set when it's time to exit main */
20374664626SKris Kennaway #endif
20474664626SKris Kennaway 	}
20574664626SKris Kennaway 
20674664626SKris Kennaway /***********************************************************************
20774664626SKris Kennaway  * usage - display usage message
20874664626SKris Kennaway  */
20974664626SKris Kennaway static void s_time_usage(void)
21074664626SKris Kennaway {
21174664626SKris Kennaway 	static char umsg[] = "\
21274664626SKris Kennaway -time arg     - max number of seconds to collect data, default %d\n\
21374664626SKris Kennaway -verify arg   - turn on peer certificate verification, arg == depth\n\
21474664626SKris Kennaway -cert arg     - certificate file to use, PEM format assumed\n\
21574664626SKris Kennaway -key arg      - RSA file to use, PEM format assumed, key is in cert file\n\
21674664626SKris Kennaway                 file if not specified by this option\n\
21774664626SKris Kennaway -CApath arg   - PEM format directory of CA's\n\
21874664626SKris Kennaway -CAfile arg   - PEM format file of CA's\n\
219f579bf8eSKris Kennaway -cipher       - preferred cipher to use, play with 'openssl ciphers'\n\n";
22074664626SKris Kennaway 
22174664626SKris Kennaway 	printf( "usage: s_time <args>\n\n" );
22274664626SKris Kennaway 
22374664626SKris Kennaway 	printf("-connect host:port - host:port to connect to (default is %s)\n",SSL_CONNECT_NAME);
22474664626SKris Kennaway #ifdef FIONBIO
22574664626SKris Kennaway 	printf("-nbio         - Run with non-blocking IO\n");
22674664626SKris Kennaway 	printf("-ssl2         - Just use SSLv2\n");
22774664626SKris Kennaway 	printf("-ssl3         - Just use SSLv3\n");
228f579bf8eSKris Kennaway 	printf("-bugs         - Turn on SSL bug compatibility\n");
22974664626SKris Kennaway 	printf("-new          - Just time new connections\n");
23074664626SKris Kennaway 	printf("-reuse        - Just time connection reuse\n");
23174664626SKris Kennaway 	printf("-www page     - Retrieve 'page' from the site\n");
23274664626SKris Kennaway #endif
23374664626SKris Kennaway 	printf( umsg,SECONDS );
23474664626SKris Kennaway }
23574664626SKris Kennaway 
23674664626SKris Kennaway /***********************************************************************
23774664626SKris Kennaway  * parseArgs - Parse command line arguments and initialize data
23874664626SKris Kennaway  *
23974664626SKris Kennaway  * Returns 0 if ok, -1 on bad args
24074664626SKris Kennaway  */
24174664626SKris Kennaway static int parseArgs(int argc, char **argv)
24274664626SKris Kennaway {
24374664626SKris Kennaway     int badop = 0;
24474664626SKris Kennaway 
24574664626SKris Kennaway     verify_depth=0;
24674664626SKris Kennaway     verify_error=X509_V_OK;
24774664626SKris Kennaway 
24874664626SKris Kennaway     argc--;
24974664626SKris Kennaway     argv++;
25074664626SKris Kennaway 
25174664626SKris Kennaway     while (argc >= 1) {
25274664626SKris Kennaway 	if (strcmp(*argv,"-connect") == 0)
25374664626SKris Kennaway 		{
25474664626SKris Kennaway 		if (--argc < 1) goto bad;
25574664626SKris Kennaway 		host= *(++argv);
25674664626SKris Kennaway 		}
25774664626SKris Kennaway #if 0
25874664626SKris Kennaway 	else if( strcmp(*argv,"-host") == 0)
25974664626SKris Kennaway 		{
26074664626SKris Kennaway 		if (--argc < 1) goto bad;
26174664626SKris Kennaway 		host= *(++argv);
26274664626SKris Kennaway 		}
26374664626SKris Kennaway 	else if( strcmp(*argv,"-port") == 0)
26474664626SKris Kennaway 		{
26574664626SKris Kennaway 		if (--argc < 1) goto bad;
26674664626SKris Kennaway 		port= *(++argv);
26774664626SKris Kennaway 		}
26874664626SKris Kennaway #endif
26974664626SKris Kennaway 	else if (strcmp(*argv,"-reuse") == 0)
27074664626SKris Kennaway 		perform=2;
27174664626SKris Kennaway 	else if (strcmp(*argv,"-new") == 0)
27274664626SKris Kennaway 		perform=1;
27374664626SKris Kennaway 	else if( strcmp(*argv,"-verify") == 0) {
27474664626SKris Kennaway 
27574664626SKris Kennaway 	    tm_verify=SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE;
27674664626SKris Kennaway 	    if (--argc < 1) goto bad;
27774664626SKris Kennaway 	    verify_depth=atoi(*(++argv));
27874664626SKris Kennaway 	    BIO_printf(bio_err,"verify depth is %d\n",verify_depth);
27974664626SKris Kennaway 
28074664626SKris Kennaway 	} else if( strcmp(*argv,"-cert") == 0) {
28174664626SKris Kennaway 
28274664626SKris Kennaway 	    if (--argc < 1) goto bad;
28374664626SKris Kennaway 	    t_cert_file= *(++argv);
28474664626SKris Kennaway 
28574664626SKris Kennaway 	} else if( strcmp(*argv,"-key") == 0) {
28674664626SKris Kennaway 
28774664626SKris Kennaway 	    if (--argc < 1) goto bad;
28874664626SKris Kennaway 	    t_key_file= *(++argv);
28974664626SKris Kennaway 
29074664626SKris Kennaway 	} else if( strcmp(*argv,"-CApath") == 0) {
29174664626SKris Kennaway 
29274664626SKris Kennaway 	    if (--argc < 1) goto bad;
29374664626SKris Kennaway 	    CApath= *(++argv);
29474664626SKris Kennaway 
29574664626SKris Kennaway 	} else if( strcmp(*argv,"-CAfile") == 0) {
29674664626SKris Kennaway 
29774664626SKris Kennaway 	    if (--argc < 1) goto bad;
29874664626SKris Kennaway 	    CAfile= *(++argv);
29974664626SKris Kennaway 
30074664626SKris Kennaway 	} else if( strcmp(*argv,"-cipher") == 0) {
30174664626SKris Kennaway 
30274664626SKris Kennaway 	    if (--argc < 1) goto bad;
30374664626SKris Kennaway 	    tm_cipher= *(++argv);
30474664626SKris Kennaway 	}
30574664626SKris Kennaway #ifdef FIONBIO
30674664626SKris Kennaway 	else if(strcmp(*argv,"-nbio") == 0) {
30774664626SKris Kennaway 	    t_nbio=1;
30874664626SKris Kennaway 	}
30974664626SKris Kennaway #endif
31074664626SKris Kennaway 	else if(strcmp(*argv,"-www") == 0)
31174664626SKris Kennaway 		{
31274664626SKris Kennaway 		if (--argc < 1) goto bad;
31374664626SKris Kennaway 		s_www_path= *(++argv);
31474664626SKris Kennaway 		}
31574664626SKris Kennaway 	else if(strcmp(*argv,"-bugs") == 0)
31674664626SKris Kennaway 	    st_bugs=1;
31774664626SKris Kennaway #ifndef NO_SSL2
31874664626SKris Kennaway 	else if(strcmp(*argv,"-ssl2") == 0)
31974664626SKris Kennaway 	    s_time_meth=SSLv2_client_method();
32074664626SKris Kennaway #endif
32174664626SKris Kennaway #ifndef NO_SSL3
32274664626SKris Kennaway 	else if(strcmp(*argv,"-ssl3") == 0)
32374664626SKris Kennaway 	    s_time_meth=SSLv3_client_method();
32474664626SKris Kennaway #endif
32574664626SKris Kennaway 	else if( strcmp(*argv,"-time") == 0) {
32674664626SKris Kennaway 
32774664626SKris Kennaway 	    if (--argc < 1) goto bad;
32874664626SKris Kennaway 	    maxTime= atoi(*(++argv));
32974664626SKris Kennaway 	}
33074664626SKris Kennaway 	else {
33174664626SKris Kennaway 	    BIO_printf(bio_err,"unknown option %s\n",*argv);
33274664626SKris Kennaway 	    badop=1;
33374664626SKris Kennaway 	    break;
33474664626SKris Kennaway 	}
33574664626SKris Kennaway 
33674664626SKris Kennaway 	argc--;
33774664626SKris Kennaway 	argv++;
33874664626SKris Kennaway     }
33974664626SKris Kennaway 
34074664626SKris Kennaway     if (perform == 0) perform=3;
34174664626SKris Kennaway 
34274664626SKris Kennaway     if(badop) {
34374664626SKris Kennaway bad:
34474664626SKris Kennaway 		s_time_usage();
34574664626SKris Kennaway 		return -1;
34674664626SKris Kennaway     }
34774664626SKris Kennaway 
34874664626SKris Kennaway 	return 0;			/* Valid args */
34974664626SKris Kennaway }
35074664626SKris Kennaway 
35174664626SKris Kennaway /***********************************************************************
35274664626SKris Kennaway  * TIME - time functions
35374664626SKris Kennaway  */
35474664626SKris Kennaway #define START	0
35574664626SKris Kennaway #define STOP	1
35674664626SKris Kennaway 
35774664626SKris Kennaway static double tm_Time_F(int s)
35874664626SKris Kennaway 	{
35974664626SKris Kennaway 	static double ret;
36074664626SKris Kennaway #ifdef TIMES
36174664626SKris Kennaway 	static struct tms tstart,tend;
36274664626SKris Kennaway 
36374664626SKris Kennaway 	if(s == START) {
36474664626SKris Kennaway 		times(&tstart);
36574664626SKris Kennaway 		return(0);
36674664626SKris Kennaway 	} else {
36774664626SKris Kennaway 		times(&tend);
36874664626SKris Kennaway 		ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
36974664626SKris Kennaway 		return((ret == 0.0)?1e-6:ret);
37074664626SKris Kennaway 	}
37174664626SKris Kennaway #else /* !times() */
37274664626SKris Kennaway 	static struct timeb tstart,tend;
37374664626SKris Kennaway 	long i;
37474664626SKris Kennaway 
37574664626SKris Kennaway 	if(s == START) {
37674664626SKris Kennaway 		ftime(&tstart);
37774664626SKris Kennaway 		return(0);
37874664626SKris Kennaway 	} else {
37974664626SKris Kennaway 		ftime(&tend);
38074664626SKris Kennaway 		i=(long)tend.millitm-(long)tstart.millitm;
38174664626SKris Kennaway 		ret=((double)(tend.time-tstart.time))+((double)i)/1000.0;
38274664626SKris Kennaway 		return((ret == 0.0)?1e-6:ret);
38374664626SKris Kennaway 	}
38474664626SKris Kennaway #endif
38574664626SKris Kennaway }
38674664626SKris Kennaway 
38774664626SKris Kennaway /***********************************************************************
38874664626SKris Kennaway  * MAIN - main processing area for client
38974664626SKris Kennaway  *			real name depends on MONOLITH
39074664626SKris Kennaway  */
391f579bf8eSKris Kennaway int MAIN(int, char **);
392f579bf8eSKris Kennaway 
39374664626SKris Kennaway int MAIN(int argc, char **argv)
39474664626SKris Kennaway 	{
39574664626SKris Kennaway 	double totalTime = 0.0;
39674664626SKris Kennaway 	int nConn = 0;
39774664626SKris Kennaway 	SSL *scon=NULL;
39874664626SKris Kennaway 	long finishtime=0;
39974664626SKris Kennaway 	int ret=1,i;
40074664626SKris Kennaway 	MS_STATIC char buf[1024*8];
40174664626SKris Kennaway 	int ver;
40274664626SKris Kennaway 
403f579bf8eSKris Kennaway 	apps_startup();
404f579bf8eSKris Kennaway 	s_time_init();
405f579bf8eSKris Kennaway 
406f579bf8eSKris Kennaway 	if (bio_err == NULL)
407f579bf8eSKris Kennaway 		bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
408f579bf8eSKris Kennaway 
40974664626SKris Kennaway #if !defined(NO_SSL2) && !defined(NO_SSL3)
41074664626SKris Kennaway 	s_time_meth=SSLv23_client_method();
41174664626SKris Kennaway #elif !defined(NO_SSL3)
41274664626SKris Kennaway 	s_time_meth=SSLv3_client_method();
41374664626SKris Kennaway #elif !defined(NO_SSL2)
41474664626SKris Kennaway 	s_time_meth=SSLv2_client_method();
41574664626SKris Kennaway #endif
41674664626SKris Kennaway 
41774664626SKris Kennaway 	/* parse the command line arguments */
41874664626SKris Kennaway 	if( parseArgs( argc, argv ) < 0 )
41974664626SKris Kennaway 		goto end;
42074664626SKris Kennaway 
421f579bf8eSKris Kennaway 	OpenSSL_add_ssl_algorithms();
42274664626SKris Kennaway 	if ((tm_ctx=SSL_CTX_new(s_time_meth)) == NULL) return(1);
42374664626SKris Kennaway 
42474664626SKris Kennaway 	SSL_CTX_set_quiet_shutdown(tm_ctx,1);
42574664626SKris Kennaway 
42674664626SKris Kennaway 	if (st_bugs) SSL_CTX_set_options(tm_ctx,SSL_OP_ALL);
42774664626SKris Kennaway 	SSL_CTX_set_cipher_list(tm_ctx,tm_cipher);
42874664626SKris Kennaway 	if(!set_cert_stuff(tm_ctx,t_cert_file,t_key_file))
42974664626SKris Kennaway 		goto end;
43074664626SKris Kennaway 
43174664626SKris Kennaway 	SSL_load_error_strings();
43274664626SKris Kennaway 
43374664626SKris Kennaway 	if ((!SSL_CTX_load_verify_locations(tm_ctx,CAfile,CApath)) ||
43474664626SKris Kennaway 		(!SSL_CTX_set_default_verify_paths(tm_ctx)))
43574664626SKris Kennaway 		{
436f579bf8eSKris Kennaway 		/* BIO_printf(bio_err,"error setting default verify locations\n"); */
43774664626SKris Kennaway 		ERR_print_errors(bio_err);
43874664626SKris Kennaway 		/* goto end; */
43974664626SKris Kennaway 		}
44074664626SKris Kennaway 
44174664626SKris Kennaway 	if (tm_cipher == NULL)
44274664626SKris Kennaway 		tm_cipher = getenv("SSL_CIPHER");
44374664626SKris Kennaway 
44474664626SKris Kennaway 	if (tm_cipher == NULL ) {
44574664626SKris Kennaway 		fprintf( stderr, "No CIPHER specified\n" );
44674664626SKris Kennaway /*		EXIT(1); */
44774664626SKris Kennaway 	}
44874664626SKris Kennaway 
44974664626SKris Kennaway 	if (!(perform & 1)) goto next;
45074664626SKris Kennaway 	printf( "Collecting connection statistics for %d seconds\n", maxTime );
45174664626SKris Kennaway 
45274664626SKris Kennaway 	/* Loop and time how long it takes to make connections */
45374664626SKris Kennaway 
45474664626SKris Kennaway 	bytes_read=0;
45574664626SKris Kennaway 	finishtime=(long)time(NULL)+maxTime;
45674664626SKris Kennaway 	tm_Time_F(START);
45774664626SKris Kennaway 	for (;;)
45874664626SKris Kennaway 		{
45974664626SKris Kennaway 		if (finishtime < time(NULL)) break;
46074664626SKris Kennaway #ifdef WIN32_STUFF
46174664626SKris Kennaway 
46274664626SKris Kennaway 		if( flushWinMsgs(0) == -1 )
46374664626SKris Kennaway 			goto end;
46474664626SKris Kennaway 
46574664626SKris Kennaway 		if( waitingToDie || exitNow )		/* we're dead */
46674664626SKris Kennaway 			goto end;
46774664626SKris Kennaway #endif
46874664626SKris Kennaway 
46974664626SKris Kennaway 		if( (scon = doConnection( NULL )) == NULL )
47074664626SKris Kennaway 			goto end;
47174664626SKris Kennaway 
47274664626SKris Kennaway 		if (s_www_path != NULL)
47374664626SKris Kennaway 			{
47474664626SKris Kennaway 			sprintf(buf,"GET %s HTTP/1.0\r\n\r\n",s_www_path);
47574664626SKris Kennaway 			SSL_write(scon,buf,strlen(buf));
47674664626SKris Kennaway 			while ((i=SSL_read(scon,buf,sizeof(buf))) > 0)
47774664626SKris Kennaway 				bytes_read+=i;
47874664626SKris Kennaway 			}
47974664626SKris Kennaway 
48074664626SKris Kennaway #ifdef NO_SHUTDOWN
48174664626SKris Kennaway 		SSL_set_shutdown(scon,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
48274664626SKris Kennaway #else
48374664626SKris Kennaway 		SSL_shutdown(scon);
48474664626SKris Kennaway #endif
48574664626SKris Kennaway 		SHUTDOWN2(SSL_get_fd(scon));
48674664626SKris Kennaway 
48774664626SKris Kennaway 		nConn += 1;
48874664626SKris Kennaway 		if (SSL_session_reused(scon))
48974664626SKris Kennaway 			ver='r';
49074664626SKris Kennaway 		else
49174664626SKris Kennaway 			{
49274664626SKris Kennaway 			ver=SSL_version(scon);
49374664626SKris Kennaway 			if (ver == TLS1_VERSION)
49474664626SKris Kennaway 				ver='t';
49574664626SKris Kennaway 			else if (ver == SSL3_VERSION)
49674664626SKris Kennaway 				ver='3';
49774664626SKris Kennaway 			else if (ver == SSL2_VERSION)
49874664626SKris Kennaway 				ver='2';
49974664626SKris Kennaway 			else
50074664626SKris Kennaway 				ver='*';
50174664626SKris Kennaway 			}
50274664626SKris Kennaway 		fputc(ver,stdout);
50374664626SKris Kennaway 		fflush(stdout);
50474664626SKris Kennaway 
50574664626SKris Kennaway 		SSL_free( scon );
50674664626SKris Kennaway 		scon=NULL;
50774664626SKris Kennaway 		}
50874664626SKris Kennaway 	totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
50974664626SKris Kennaway 
51074664626SKris Kennaway 	i=(int)(time(NULL)-finishtime+maxTime);
51174664626SKris Kennaway 	printf( "\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n", nConn, totalTime, ((double)nConn/totalTime),bytes_read);
51274664626SKris Kennaway 	printf( "%d connections in %ld real seconds, %ld bytes read per connection\n",nConn,time(NULL)-finishtime+maxTime,bytes_read/nConn);
51374664626SKris Kennaway 
51474664626SKris Kennaway 	/* Now loop and time connections using the same session id over and over */
51574664626SKris Kennaway 
51674664626SKris Kennaway next:
51774664626SKris Kennaway 	if (!(perform & 2)) goto end;
51874664626SKris Kennaway 	printf( "\n\nNow timing with session id reuse.\n" );
51974664626SKris Kennaway 
52074664626SKris Kennaway 	/* Get an SSL object so we can reuse the session id */
52174664626SKris Kennaway 	if( (scon = doConnection( NULL )) == NULL )
52274664626SKris Kennaway 		{
52374664626SKris Kennaway 		fprintf( stderr, "Unable to get connection\n" );
52474664626SKris Kennaway 		goto end;
52574664626SKris Kennaway 		}
52674664626SKris Kennaway 
52774664626SKris Kennaway 	if (s_www_path != NULL)
52874664626SKris Kennaway 		{
52974664626SKris Kennaway 		sprintf(buf,"GET %s HTTP/1.0\r\n\r\n",s_www_path);
53074664626SKris Kennaway 		SSL_write(scon,buf,strlen(buf));
53174664626SKris Kennaway 		while (SSL_read(scon,buf,sizeof(buf)) > 0)
53274664626SKris Kennaway 			;
53374664626SKris Kennaway 		}
53474664626SKris Kennaway #ifdef NO_SHUTDOWN
53574664626SKris Kennaway 	SSL_set_shutdown(scon,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
53674664626SKris Kennaway #else
53774664626SKris Kennaway 	SSL_shutdown(scon);
53874664626SKris Kennaway #endif
53974664626SKris Kennaway 	SHUTDOWN2(SSL_get_fd(scon));
54074664626SKris Kennaway 
54174664626SKris Kennaway 	nConn = 0;
54274664626SKris Kennaway 	totalTime = 0.0;
54374664626SKris Kennaway 
54474664626SKris Kennaway 	finishtime=time(NULL)+maxTime;
54574664626SKris Kennaway 
54674664626SKris Kennaway 	printf( "starting\n" );
54774664626SKris Kennaway 	bytes_read=0;
54874664626SKris Kennaway 	tm_Time_F(START);
54974664626SKris Kennaway 
55074664626SKris Kennaway 	for (;;)
55174664626SKris Kennaway 		{
55274664626SKris Kennaway 		if (finishtime < time(NULL)) break;
55374664626SKris Kennaway 
55474664626SKris Kennaway #ifdef WIN32_STUFF
55574664626SKris Kennaway 		if( flushWinMsgs(0) == -1 )
55674664626SKris Kennaway 			goto end;
55774664626SKris Kennaway 
55874664626SKris Kennaway 		if( waitingToDie || exitNow )	/* we're dead */
55974664626SKris Kennaway 			goto end;
56074664626SKris Kennaway #endif
56174664626SKris Kennaway 
56274664626SKris Kennaway 	 	if( (doConnection( scon )) == NULL )
56374664626SKris Kennaway 			goto end;
56474664626SKris Kennaway 
56574664626SKris Kennaway 		if (s_www_path)
56674664626SKris Kennaway 			{
56774664626SKris Kennaway 			sprintf(buf,"GET %s HTTP/1.0\r\n\r\n",s_www_path);
56874664626SKris Kennaway 			SSL_write(scon,buf,strlen(buf));
56974664626SKris Kennaway 			while ((i=SSL_read(scon,buf,sizeof(buf))) > 0)
57074664626SKris Kennaway 				bytes_read+=i;
57174664626SKris Kennaway 			}
57274664626SKris Kennaway 
57374664626SKris Kennaway #ifdef NO_SHUTDOWN
57474664626SKris Kennaway 		SSL_set_shutdown(scon,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
57574664626SKris Kennaway #else
57674664626SKris Kennaway 		SSL_shutdown(scon);
57774664626SKris Kennaway #endif
57874664626SKris Kennaway 		SHUTDOWN2(SSL_get_fd(scon));
57974664626SKris Kennaway 
58074664626SKris Kennaway 		nConn += 1;
58174664626SKris Kennaway 		if (SSL_session_reused(scon))
58274664626SKris Kennaway 			ver='r';
58374664626SKris Kennaway 		else
58474664626SKris Kennaway 			{
58574664626SKris Kennaway 			ver=SSL_version(scon);
58674664626SKris Kennaway 			if (ver == TLS1_VERSION)
58774664626SKris Kennaway 				ver='t';
58874664626SKris Kennaway 			else if (ver == SSL3_VERSION)
58974664626SKris Kennaway 				ver='3';
59074664626SKris Kennaway 			else if (ver == SSL2_VERSION)
59174664626SKris Kennaway 				ver='2';
59274664626SKris Kennaway 			else
59374664626SKris Kennaway 				ver='*';
59474664626SKris Kennaway 			}
59574664626SKris Kennaway 		fputc(ver,stdout);
59674664626SKris Kennaway 		fflush(stdout);
59774664626SKris Kennaway 		}
59874664626SKris Kennaway 	totalTime += tm_Time_F(STOP); /* Add the time for this iteration*/
59974664626SKris Kennaway 
60074664626SKris Kennaway 
60174664626SKris Kennaway 	printf( "\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n", nConn, totalTime, ((double)nConn/totalTime),bytes_read);
60274664626SKris Kennaway 	printf( "%d connections in %ld real seconds, %ld bytes read per connection\n",nConn,time(NULL)-finishtime+maxTime,bytes_read/nConn);
60374664626SKris Kennaway 
60474664626SKris Kennaway 	ret=0;
60574664626SKris Kennaway end:
60674664626SKris Kennaway 	if (scon != NULL) SSL_free(scon);
60774664626SKris Kennaway 
60874664626SKris Kennaway 	if (tm_ctx != NULL)
60974664626SKris Kennaway 		{
61074664626SKris Kennaway 		SSL_CTX_free(tm_ctx);
61174664626SKris Kennaway 		tm_ctx=NULL;
61274664626SKris Kennaway 		}
61374664626SKris Kennaway 	EXIT(ret);
61474664626SKris Kennaway 	}
61574664626SKris Kennaway 
61674664626SKris Kennaway /***********************************************************************
61774664626SKris Kennaway  * doConnection - make a connection
61874664626SKris Kennaway  * Args:
61974664626SKris Kennaway  *		scon	= earlier ssl connection for session id, or NULL
62074664626SKris Kennaway  * Returns:
62174664626SKris Kennaway  *		SSL *	= the connection pointer.
62274664626SKris Kennaway  */
62374664626SKris Kennaway static SSL *doConnection(SSL *scon)
62474664626SKris Kennaway 	{
62574664626SKris Kennaway 	BIO *conn;
62674664626SKris Kennaway 	SSL *serverCon;
62774664626SKris Kennaway 	int width, i;
62874664626SKris Kennaway 	fd_set readfds;
62974664626SKris Kennaway 
63074664626SKris Kennaway 	if ((conn=BIO_new(BIO_s_connect())) == NULL)
63174664626SKris Kennaway 		return(NULL);
63274664626SKris Kennaway 
63374664626SKris Kennaway /*	BIO_set_conn_port(conn,port);*/
63474664626SKris Kennaway 	BIO_set_conn_hostname(conn,host);
63574664626SKris Kennaway 
63674664626SKris Kennaway 	if (scon == NULL)
637f579bf8eSKris Kennaway 		serverCon=SSL_new(tm_ctx);
63874664626SKris Kennaway 	else
63974664626SKris Kennaway 		{
64074664626SKris Kennaway 		serverCon=scon;
64174664626SKris Kennaway 		SSL_set_connect_state(serverCon);
64274664626SKris Kennaway 		}
64374664626SKris Kennaway 
64474664626SKris Kennaway 	SSL_set_bio(serverCon,conn,conn);
64574664626SKris Kennaway 
64674664626SKris Kennaway #if 0
64774664626SKris Kennaway 	if( scon != NULL )
64874664626SKris Kennaway 		SSL_set_session(serverCon,SSL_get_session(scon));
64974664626SKris Kennaway #endif
65074664626SKris Kennaway 
65174664626SKris Kennaway 	/* ok, lets connect */
65274664626SKris Kennaway 	for(;;) {
65374664626SKris Kennaway 		i=SSL_connect(serverCon);
65474664626SKris Kennaway 		if (BIO_sock_should_retry(i))
65574664626SKris Kennaway 			{
65674664626SKris Kennaway 			BIO_printf(bio_err,"DELAY\n");
65774664626SKris Kennaway 
65874664626SKris Kennaway 			i=SSL_get_fd(serverCon);
65974664626SKris Kennaway 			width=i+1;
66074664626SKris Kennaway 			FD_ZERO(&readfds);
66174664626SKris Kennaway 			FD_SET(i,&readfds);
66274664626SKris Kennaway 			/* Note: under VMS with SOCKETSHR the 2nd parameter
66374664626SKris Kennaway 			 * is currently of type (int *) whereas under other
66474664626SKris Kennaway 			 * systems it is (void *) if you don't have a cast it
66574664626SKris Kennaway 			 * will choke the compiler: if you do have a cast then
66674664626SKris Kennaway 			 * you can either go for (int *) or (void *).
66774664626SKris Kennaway 			 */
66874664626SKris Kennaway 			select(width,(void *)&readfds,NULL,NULL,NULL);
66974664626SKris Kennaway 			continue;
67074664626SKris Kennaway 			}
67174664626SKris Kennaway 		break;
67274664626SKris Kennaway 		}
67374664626SKris Kennaway 	if(i <= 0)
67474664626SKris Kennaway 		{
67574664626SKris Kennaway 		BIO_printf(bio_err,"ERROR\n");
67674664626SKris Kennaway 		if (verify_error != X509_V_OK)
67774664626SKris Kennaway 			BIO_printf(bio_err,"verify error:%s\n",
67874664626SKris Kennaway 				X509_verify_cert_error_string(verify_error));
67974664626SKris Kennaway 		else
68074664626SKris Kennaway 			ERR_print_errors(bio_err);
68174664626SKris Kennaway 		if (scon == NULL)
68274664626SKris Kennaway 			SSL_free(serverCon);
68374664626SKris Kennaway 		return NULL;
68474664626SKris Kennaway 		}
68574664626SKris Kennaway 
68674664626SKris Kennaway 	return serverCon;
68774664626SKris Kennaway 	}
68874664626SKris Kennaway 
68974664626SKris Kennaway 
690