1*0Sstevel@tonic-gate /* apps/s_time.c */ 2*0Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3*0Sstevel@tonic-gate * All rights reserved. 4*0Sstevel@tonic-gate * 5*0Sstevel@tonic-gate * This package is an SSL implementation written 6*0Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com). 7*0Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as 10*0Sstevel@tonic-gate * the following conditions are aheared to. The following conditions 11*0Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA, 12*0Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13*0Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms 14*0Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15*0Sstevel@tonic-gate * 16*0Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in 17*0Sstevel@tonic-gate * the code are not to be removed. 18*0Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution 19*0Sstevel@tonic-gate * as the author of the parts of the library used. 20*0Sstevel@tonic-gate * This can be in the form of a textual message at program startup or 21*0Sstevel@tonic-gate * in documentation (online or textual) provided with the package. 22*0Sstevel@tonic-gate * 23*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 24*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions 25*0Sstevel@tonic-gate * are met: 26*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright 27*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 28*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 29*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 30*0Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 31*0Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 32*0Sstevel@tonic-gate * must display the following acknowledgement: 33*0Sstevel@tonic-gate * "This product includes cryptographic software written by 34*0Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)" 35*0Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library 36*0Sstevel@tonic-gate * being used are not cryptographic related :-). 37*0Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from 38*0Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement: 39*0Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40*0Sstevel@tonic-gate * 41*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42*0Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43*0Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44*0Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45*0Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46*0Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47*0Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48*0Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49*0Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50*0Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51*0Sstevel@tonic-gate * SUCH DAMAGE. 52*0Sstevel@tonic-gate * 53*0Sstevel@tonic-gate * The licence and distribution terms for any publically available version or 54*0Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be 55*0Sstevel@tonic-gate * copied and put under another distribution licence 56*0Sstevel@tonic-gate * [including the GNU Public Licence.] 57*0Sstevel@tonic-gate */ 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate #define NO_SHUTDOWN 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate /*----------------------------------------- 62*0Sstevel@tonic-gate s_time - SSL client connection timer program 63*0Sstevel@tonic-gate Written and donated by Larry Streepy <streepy@healthcare.com> 64*0Sstevel@tonic-gate -----------------------------------------*/ 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate #include <stdio.h> 67*0Sstevel@tonic-gate #include <stdlib.h> 68*0Sstevel@tonic-gate #include <string.h> 69*0Sstevel@tonic-gate 70*0Sstevel@tonic-gate #define USE_SOCKETS 71*0Sstevel@tonic-gate #include "apps.h" 72*0Sstevel@tonic-gate #ifdef OPENSSL_NO_STDIO 73*0Sstevel@tonic-gate #define APPS_WIN16 74*0Sstevel@tonic-gate #endif 75*0Sstevel@tonic-gate #include <openssl/x509.h> 76*0Sstevel@tonic-gate #include <openssl/ssl.h> 77*0Sstevel@tonic-gate #include <openssl/pem.h> 78*0Sstevel@tonic-gate #include "s_apps.h" 79*0Sstevel@tonic-gate #include <openssl/err.h> 80*0Sstevel@tonic-gate #ifdef WIN32_STUFF 81*0Sstevel@tonic-gate #include "winmain.h" 82*0Sstevel@tonic-gate #include "wintext.h" 83*0Sstevel@tonic-gate #endif 84*0Sstevel@tonic-gate #if !defined(OPENSSL_SYS_MSDOS) 85*0Sstevel@tonic-gate #include OPENSSL_UNISTD 86*0Sstevel@tonic-gate #endif 87*0Sstevel@tonic-gate 88*0Sstevel@tonic-gate #if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VXWORKS) && (!defined(OPENSSL_SYS_VMS) || defined(__DECC)) 89*0Sstevel@tonic-gate #define TIMES 90*0Sstevel@tonic-gate #endif 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate #ifndef _IRIX 93*0Sstevel@tonic-gate #include <time.h> 94*0Sstevel@tonic-gate #endif 95*0Sstevel@tonic-gate #ifdef TIMES 96*0Sstevel@tonic-gate #include <sys/types.h> 97*0Sstevel@tonic-gate #include <sys/times.h> 98*0Sstevel@tonic-gate #endif 99*0Sstevel@tonic-gate 100*0Sstevel@tonic-gate /* Depending on the VMS version, the tms structure is perhaps defined. 101*0Sstevel@tonic-gate The __TMS macro will show if it was. If it wasn't defined, we should 102*0Sstevel@tonic-gate undefine TIMES, since that tells the rest of the program how things 103*0Sstevel@tonic-gate should be handled. -- Richard Levitte */ 104*0Sstevel@tonic-gate #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS) 105*0Sstevel@tonic-gate #undef TIMES 106*0Sstevel@tonic-gate #endif 107*0Sstevel@tonic-gate 108*0Sstevel@tonic-gate #if !defined(TIMES) && !defined(OPENSSL_SYS_VXWORKS) 109*0Sstevel@tonic-gate #include <sys/timeb.h> 110*0Sstevel@tonic-gate #endif 111*0Sstevel@tonic-gate 112*0Sstevel@tonic-gate #if defined(sun) || defined(__ultrix) 113*0Sstevel@tonic-gate #define _POSIX_SOURCE 114*0Sstevel@tonic-gate #include <limits.h> 115*0Sstevel@tonic-gate #include <sys/param.h> 116*0Sstevel@tonic-gate #endif 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate /* The following if from times(3) man page. It may need to be changed 119*0Sstevel@tonic-gate */ 120*0Sstevel@tonic-gate #ifndef HZ 121*0Sstevel@tonic-gate # ifdef _SC_CLK_TCK 122*0Sstevel@tonic-gate # define HZ ((double)sysconf(_SC_CLK_TCK)) 123*0Sstevel@tonic-gate # else 124*0Sstevel@tonic-gate # ifndef CLK_TCK 125*0Sstevel@tonic-gate # ifndef _BSD_CLK_TCK_ /* FreeBSD hack */ 126*0Sstevel@tonic-gate # define HZ 100.0 127*0Sstevel@tonic-gate # else /* _BSD_CLK_TCK_ */ 128*0Sstevel@tonic-gate # define HZ ((double)_BSD_CLK_TCK_) 129*0Sstevel@tonic-gate # endif 130*0Sstevel@tonic-gate # else /* CLK_TCK */ 131*0Sstevel@tonic-gate # define HZ ((double)CLK_TCK) 132*0Sstevel@tonic-gate # endif 133*0Sstevel@tonic-gate # endif 134*0Sstevel@tonic-gate #endif 135*0Sstevel@tonic-gate 136*0Sstevel@tonic-gate #undef PROG 137*0Sstevel@tonic-gate #define PROG s_time_main 138*0Sstevel@tonic-gate 139*0Sstevel@tonic-gate #undef ioctl 140*0Sstevel@tonic-gate #define ioctl ioctlsocket 141*0Sstevel@tonic-gate 142*0Sstevel@tonic-gate #define SSL_CONNECT_NAME "localhost:4433" 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gate /*#define TEST_CERT "client.pem" */ /* no default cert. */ 145*0Sstevel@tonic-gate 146*0Sstevel@tonic-gate #undef BUFSIZZ 147*0Sstevel@tonic-gate #define BUFSIZZ 1024*10 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gate #define MYBUFSIZ 1024*8 150*0Sstevel@tonic-gate 151*0Sstevel@tonic-gate #undef min 152*0Sstevel@tonic-gate #undef max 153*0Sstevel@tonic-gate #define min(a,b) (((a) < (b)) ? (a) : (b)) 154*0Sstevel@tonic-gate #define max(a,b) (((a) > (b)) ? (a) : (b)) 155*0Sstevel@tonic-gate 156*0Sstevel@tonic-gate #undef SECONDS 157*0Sstevel@tonic-gate #define SECONDS 30 158*0Sstevel@tonic-gate extern int verify_depth; 159*0Sstevel@tonic-gate extern int verify_error; 160*0Sstevel@tonic-gate 161*0Sstevel@tonic-gate static void s_time_usage(void); 162*0Sstevel@tonic-gate static int parseArgs( int argc, char **argv ); 163*0Sstevel@tonic-gate static SSL *doConnection( SSL *scon ); 164*0Sstevel@tonic-gate static void s_time_init(void); 165*0Sstevel@tonic-gate 166*0Sstevel@tonic-gate /*********************************************************************** 167*0Sstevel@tonic-gate * Static data declarations 168*0Sstevel@tonic-gate */ 169*0Sstevel@tonic-gate 170*0Sstevel@tonic-gate /* static char *port=PORT_STR;*/ 171*0Sstevel@tonic-gate static char *host=SSL_CONNECT_NAME; 172*0Sstevel@tonic-gate static char *t_cert_file=NULL; 173*0Sstevel@tonic-gate static char *t_key_file=NULL; 174*0Sstevel@tonic-gate static char *CApath=NULL; 175*0Sstevel@tonic-gate static char *CAfile=NULL; 176*0Sstevel@tonic-gate static char *tm_cipher=NULL; 177*0Sstevel@tonic-gate static int tm_verify = SSL_VERIFY_NONE; 178*0Sstevel@tonic-gate static int maxTime = SECONDS; 179*0Sstevel@tonic-gate static SSL_CTX *tm_ctx=NULL; 180*0Sstevel@tonic-gate static SSL_METHOD *s_time_meth=NULL; 181*0Sstevel@tonic-gate static char *s_www_path=NULL; 182*0Sstevel@tonic-gate static long bytes_read=0; 183*0Sstevel@tonic-gate static int st_bugs=0; 184*0Sstevel@tonic-gate static int perform=0; 185*0Sstevel@tonic-gate #ifdef FIONBIO 186*0Sstevel@tonic-gate static int t_nbio=0; 187*0Sstevel@tonic-gate #endif 188*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_WIN32 189*0Sstevel@tonic-gate static int exitNow = 0; /* Set when it's time to exit main */ 190*0Sstevel@tonic-gate #endif 191*0Sstevel@tonic-gate 192*0Sstevel@tonic-gate static void s_time_init(void) 193*0Sstevel@tonic-gate { 194*0Sstevel@tonic-gate host=SSL_CONNECT_NAME; 195*0Sstevel@tonic-gate t_cert_file=NULL; 196*0Sstevel@tonic-gate t_key_file=NULL; 197*0Sstevel@tonic-gate CApath=NULL; 198*0Sstevel@tonic-gate CAfile=NULL; 199*0Sstevel@tonic-gate tm_cipher=NULL; 200*0Sstevel@tonic-gate tm_verify = SSL_VERIFY_NONE; 201*0Sstevel@tonic-gate maxTime = SECONDS; 202*0Sstevel@tonic-gate tm_ctx=NULL; 203*0Sstevel@tonic-gate s_time_meth=NULL; 204*0Sstevel@tonic-gate s_www_path=NULL; 205*0Sstevel@tonic-gate bytes_read=0; 206*0Sstevel@tonic-gate st_bugs=0; 207*0Sstevel@tonic-gate perform=0; 208*0Sstevel@tonic-gate 209*0Sstevel@tonic-gate #ifdef FIONBIO 210*0Sstevel@tonic-gate t_nbio=0; 211*0Sstevel@tonic-gate #endif 212*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_WIN32 213*0Sstevel@tonic-gate exitNow = 0; /* Set when it's time to exit main */ 214*0Sstevel@tonic-gate #endif 215*0Sstevel@tonic-gate } 216*0Sstevel@tonic-gate 217*0Sstevel@tonic-gate /*********************************************************************** 218*0Sstevel@tonic-gate * usage - display usage message 219*0Sstevel@tonic-gate */ 220*0Sstevel@tonic-gate static void s_time_usage(void) 221*0Sstevel@tonic-gate { 222*0Sstevel@tonic-gate static char umsg[] = "\ 223*0Sstevel@tonic-gate -time arg - max number of seconds to collect data, default %d\n\ 224*0Sstevel@tonic-gate -verify arg - turn on peer certificate verification, arg == depth\n\ 225*0Sstevel@tonic-gate -cert arg - certificate file to use, PEM format assumed\n\ 226*0Sstevel@tonic-gate -key arg - RSA file to use, PEM format assumed, key is in cert file\n\ 227*0Sstevel@tonic-gate file if not specified by this option\n\ 228*0Sstevel@tonic-gate -CApath arg - PEM format directory of CA's\n\ 229*0Sstevel@tonic-gate -CAfile arg - PEM format file of CA's\n\ 230*0Sstevel@tonic-gate -cipher - preferred cipher to use, play with 'openssl ciphers'\n\n"; 231*0Sstevel@tonic-gate 232*0Sstevel@tonic-gate printf( "usage: s_time <args>\n\n" ); 233*0Sstevel@tonic-gate 234*0Sstevel@tonic-gate printf("-connect host:port - host:port to connect to (default is %s)\n",SSL_CONNECT_NAME); 235*0Sstevel@tonic-gate #ifdef FIONBIO 236*0Sstevel@tonic-gate printf("-nbio - Run with non-blocking IO\n"); 237*0Sstevel@tonic-gate printf("-ssl2 - Just use SSLv2\n"); 238*0Sstevel@tonic-gate printf("-ssl3 - Just use SSLv3\n"); 239*0Sstevel@tonic-gate printf("-bugs - Turn on SSL bug compatibility\n"); 240*0Sstevel@tonic-gate printf("-new - Just time new connections\n"); 241*0Sstevel@tonic-gate printf("-reuse - Just time connection reuse\n"); 242*0Sstevel@tonic-gate printf("-www page - Retrieve 'page' from the site\n"); 243*0Sstevel@tonic-gate #endif 244*0Sstevel@tonic-gate printf( umsg,SECONDS ); 245*0Sstevel@tonic-gate } 246*0Sstevel@tonic-gate 247*0Sstevel@tonic-gate /*********************************************************************** 248*0Sstevel@tonic-gate * parseArgs - Parse command line arguments and initialize data 249*0Sstevel@tonic-gate * 250*0Sstevel@tonic-gate * Returns 0 if ok, -1 on bad args 251*0Sstevel@tonic-gate */ 252*0Sstevel@tonic-gate static int parseArgs(int argc, char **argv) 253*0Sstevel@tonic-gate { 254*0Sstevel@tonic-gate int badop = 0; 255*0Sstevel@tonic-gate 256*0Sstevel@tonic-gate verify_depth=0; 257*0Sstevel@tonic-gate verify_error=X509_V_OK; 258*0Sstevel@tonic-gate 259*0Sstevel@tonic-gate argc--; 260*0Sstevel@tonic-gate argv++; 261*0Sstevel@tonic-gate 262*0Sstevel@tonic-gate while (argc >= 1) { 263*0Sstevel@tonic-gate if (strcmp(*argv,"-connect") == 0) 264*0Sstevel@tonic-gate { 265*0Sstevel@tonic-gate if (--argc < 1) goto bad; 266*0Sstevel@tonic-gate host= *(++argv); 267*0Sstevel@tonic-gate } 268*0Sstevel@tonic-gate #if 0 269*0Sstevel@tonic-gate else if( strcmp(*argv,"-host") == 0) 270*0Sstevel@tonic-gate { 271*0Sstevel@tonic-gate if (--argc < 1) goto bad; 272*0Sstevel@tonic-gate host= *(++argv); 273*0Sstevel@tonic-gate } 274*0Sstevel@tonic-gate else if( strcmp(*argv,"-port") == 0) 275*0Sstevel@tonic-gate { 276*0Sstevel@tonic-gate if (--argc < 1) goto bad; 277*0Sstevel@tonic-gate port= *(++argv); 278*0Sstevel@tonic-gate } 279*0Sstevel@tonic-gate #endif 280*0Sstevel@tonic-gate else if (strcmp(*argv,"-reuse") == 0) 281*0Sstevel@tonic-gate perform=2; 282*0Sstevel@tonic-gate else if (strcmp(*argv,"-new") == 0) 283*0Sstevel@tonic-gate perform=1; 284*0Sstevel@tonic-gate else if( strcmp(*argv,"-verify") == 0) { 285*0Sstevel@tonic-gate 286*0Sstevel@tonic-gate tm_verify=SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE; 287*0Sstevel@tonic-gate if (--argc < 1) goto bad; 288*0Sstevel@tonic-gate verify_depth=atoi(*(++argv)); 289*0Sstevel@tonic-gate BIO_printf(bio_err,"verify depth is %d\n",verify_depth); 290*0Sstevel@tonic-gate 291*0Sstevel@tonic-gate } else if( strcmp(*argv,"-cert") == 0) { 292*0Sstevel@tonic-gate 293*0Sstevel@tonic-gate if (--argc < 1) goto bad; 294*0Sstevel@tonic-gate t_cert_file= *(++argv); 295*0Sstevel@tonic-gate 296*0Sstevel@tonic-gate } else if( strcmp(*argv,"-key") == 0) { 297*0Sstevel@tonic-gate 298*0Sstevel@tonic-gate if (--argc < 1) goto bad; 299*0Sstevel@tonic-gate t_key_file= *(++argv); 300*0Sstevel@tonic-gate 301*0Sstevel@tonic-gate } else if( strcmp(*argv,"-CApath") == 0) { 302*0Sstevel@tonic-gate 303*0Sstevel@tonic-gate if (--argc < 1) goto bad; 304*0Sstevel@tonic-gate CApath= *(++argv); 305*0Sstevel@tonic-gate 306*0Sstevel@tonic-gate } else if( strcmp(*argv,"-CAfile") == 0) { 307*0Sstevel@tonic-gate 308*0Sstevel@tonic-gate if (--argc < 1) goto bad; 309*0Sstevel@tonic-gate CAfile= *(++argv); 310*0Sstevel@tonic-gate 311*0Sstevel@tonic-gate } else if( strcmp(*argv,"-cipher") == 0) { 312*0Sstevel@tonic-gate 313*0Sstevel@tonic-gate if (--argc < 1) goto bad; 314*0Sstevel@tonic-gate tm_cipher= *(++argv); 315*0Sstevel@tonic-gate } 316*0Sstevel@tonic-gate #ifdef FIONBIO 317*0Sstevel@tonic-gate else if(strcmp(*argv,"-nbio") == 0) { 318*0Sstevel@tonic-gate t_nbio=1; 319*0Sstevel@tonic-gate } 320*0Sstevel@tonic-gate #endif 321*0Sstevel@tonic-gate else if(strcmp(*argv,"-www") == 0) 322*0Sstevel@tonic-gate { 323*0Sstevel@tonic-gate if (--argc < 1) goto bad; 324*0Sstevel@tonic-gate s_www_path= *(++argv); 325*0Sstevel@tonic-gate if(strlen(s_www_path) > MYBUFSIZ-100) 326*0Sstevel@tonic-gate { 327*0Sstevel@tonic-gate BIO_printf(bio_err,"-www option too long\n"); 328*0Sstevel@tonic-gate badop=1; 329*0Sstevel@tonic-gate } 330*0Sstevel@tonic-gate } 331*0Sstevel@tonic-gate else if(strcmp(*argv,"-bugs") == 0) 332*0Sstevel@tonic-gate st_bugs=1; 333*0Sstevel@tonic-gate #ifndef OPENSSL_NO_SSL2 334*0Sstevel@tonic-gate else if(strcmp(*argv,"-ssl2") == 0) 335*0Sstevel@tonic-gate s_time_meth=SSLv2_client_method(); 336*0Sstevel@tonic-gate #endif 337*0Sstevel@tonic-gate #ifndef OPENSSL_NO_SSL3 338*0Sstevel@tonic-gate else if(strcmp(*argv,"-ssl3") == 0) 339*0Sstevel@tonic-gate s_time_meth=SSLv3_client_method(); 340*0Sstevel@tonic-gate #endif 341*0Sstevel@tonic-gate else if( strcmp(*argv,"-time") == 0) { 342*0Sstevel@tonic-gate 343*0Sstevel@tonic-gate if (--argc < 1) goto bad; 344*0Sstevel@tonic-gate maxTime= atoi(*(++argv)); 345*0Sstevel@tonic-gate } 346*0Sstevel@tonic-gate else { 347*0Sstevel@tonic-gate BIO_printf(bio_err,"unknown option %s\n",*argv); 348*0Sstevel@tonic-gate badop=1; 349*0Sstevel@tonic-gate break; 350*0Sstevel@tonic-gate } 351*0Sstevel@tonic-gate 352*0Sstevel@tonic-gate argc--; 353*0Sstevel@tonic-gate argv++; 354*0Sstevel@tonic-gate } 355*0Sstevel@tonic-gate 356*0Sstevel@tonic-gate if (perform == 0) perform=3; 357*0Sstevel@tonic-gate 358*0Sstevel@tonic-gate if(badop) { 359*0Sstevel@tonic-gate bad: 360*0Sstevel@tonic-gate s_time_usage(); 361*0Sstevel@tonic-gate return -1; 362*0Sstevel@tonic-gate } 363*0Sstevel@tonic-gate 364*0Sstevel@tonic-gate return 0; /* Valid args */ 365*0Sstevel@tonic-gate } 366*0Sstevel@tonic-gate 367*0Sstevel@tonic-gate /*********************************************************************** 368*0Sstevel@tonic-gate * TIME - time functions 369*0Sstevel@tonic-gate */ 370*0Sstevel@tonic-gate #define START 0 371*0Sstevel@tonic-gate #define STOP 1 372*0Sstevel@tonic-gate 373*0Sstevel@tonic-gate static double tm_Time_F(int s) 374*0Sstevel@tonic-gate { 375*0Sstevel@tonic-gate static double ret; 376*0Sstevel@tonic-gate #ifdef TIMES 377*0Sstevel@tonic-gate static struct tms tstart,tend; 378*0Sstevel@tonic-gate 379*0Sstevel@tonic-gate if(s == START) { 380*0Sstevel@tonic-gate times(&tstart); 381*0Sstevel@tonic-gate return(0); 382*0Sstevel@tonic-gate } else { 383*0Sstevel@tonic-gate times(&tend); 384*0Sstevel@tonic-gate ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ; 385*0Sstevel@tonic-gate return((ret == 0.0)?1e-6:ret); 386*0Sstevel@tonic-gate } 387*0Sstevel@tonic-gate #elif defined(OPENSSL_SYS_VXWORKS) 388*0Sstevel@tonic-gate { 389*0Sstevel@tonic-gate static unsigned long tick_start, tick_end; 390*0Sstevel@tonic-gate 391*0Sstevel@tonic-gate if( s == START ) 392*0Sstevel@tonic-gate { 393*0Sstevel@tonic-gate tick_start = tickGet(); 394*0Sstevel@tonic-gate return 0; 395*0Sstevel@tonic-gate } 396*0Sstevel@tonic-gate else 397*0Sstevel@tonic-gate { 398*0Sstevel@tonic-gate tick_end = tickGet(); 399*0Sstevel@tonic-gate ret = (double)(tick_end - tick_start) / (double)sysClkRateGet(); 400*0Sstevel@tonic-gate return((ret == 0.0)?1e-6:ret); 401*0Sstevel@tonic-gate } 402*0Sstevel@tonic-gate } 403*0Sstevel@tonic-gate #else /* !times() */ 404*0Sstevel@tonic-gate static struct timeb tstart,tend; 405*0Sstevel@tonic-gate long i; 406*0Sstevel@tonic-gate 407*0Sstevel@tonic-gate if(s == START) { 408*0Sstevel@tonic-gate ftime(&tstart); 409*0Sstevel@tonic-gate return(0); 410*0Sstevel@tonic-gate } else { 411*0Sstevel@tonic-gate ftime(&tend); 412*0Sstevel@tonic-gate i=(long)tend.millitm-(long)tstart.millitm; 413*0Sstevel@tonic-gate ret=((double)(tend.time-tstart.time))+((double)i)/1000.0; 414*0Sstevel@tonic-gate return((ret == 0.0)?1e-6:ret); 415*0Sstevel@tonic-gate } 416*0Sstevel@tonic-gate #endif 417*0Sstevel@tonic-gate } 418*0Sstevel@tonic-gate 419*0Sstevel@tonic-gate /*********************************************************************** 420*0Sstevel@tonic-gate * MAIN - main processing area for client 421*0Sstevel@tonic-gate * real name depends on MONOLITH 422*0Sstevel@tonic-gate */ 423*0Sstevel@tonic-gate int MAIN(int, char **); 424*0Sstevel@tonic-gate 425*0Sstevel@tonic-gate int MAIN(int argc, char **argv) 426*0Sstevel@tonic-gate { 427*0Sstevel@tonic-gate double totalTime = 0.0; 428*0Sstevel@tonic-gate int nConn = 0; 429*0Sstevel@tonic-gate SSL *scon=NULL; 430*0Sstevel@tonic-gate long finishtime=0; 431*0Sstevel@tonic-gate int ret=1,i; 432*0Sstevel@tonic-gate MS_STATIC char buf[1024*8]; 433*0Sstevel@tonic-gate int ver; 434*0Sstevel@tonic-gate 435*0Sstevel@tonic-gate apps_startup(); 436*0Sstevel@tonic-gate s_time_init(); 437*0Sstevel@tonic-gate 438*0Sstevel@tonic-gate if (bio_err == NULL) 439*0Sstevel@tonic-gate bio_err=BIO_new_fp(stderr,BIO_NOCLOSE); 440*0Sstevel@tonic-gate 441*0Sstevel@tonic-gate #if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3) 442*0Sstevel@tonic-gate s_time_meth=SSLv23_client_method(); 443*0Sstevel@tonic-gate #elif !defined(OPENSSL_NO_SSL3) 444*0Sstevel@tonic-gate s_time_meth=SSLv3_client_method(); 445*0Sstevel@tonic-gate #elif !defined(OPENSSL_NO_SSL2) 446*0Sstevel@tonic-gate s_time_meth=SSLv2_client_method(); 447*0Sstevel@tonic-gate #endif 448*0Sstevel@tonic-gate 449*0Sstevel@tonic-gate /* parse the command line arguments */ 450*0Sstevel@tonic-gate if( parseArgs( argc, argv ) < 0 ) 451*0Sstevel@tonic-gate goto end; 452*0Sstevel@tonic-gate 453*0Sstevel@tonic-gate OpenSSL_add_ssl_algorithms(); 454*0Sstevel@tonic-gate if ((tm_ctx=SSL_CTX_new(s_time_meth)) == NULL) return(1); 455*0Sstevel@tonic-gate 456*0Sstevel@tonic-gate SSL_CTX_set_quiet_shutdown(tm_ctx,1); 457*0Sstevel@tonic-gate 458*0Sstevel@tonic-gate if (st_bugs) SSL_CTX_set_options(tm_ctx,SSL_OP_ALL); 459*0Sstevel@tonic-gate SSL_CTX_set_cipher_list(tm_ctx,tm_cipher); 460*0Sstevel@tonic-gate if(!set_cert_stuff(tm_ctx,t_cert_file,t_key_file)) 461*0Sstevel@tonic-gate goto end; 462*0Sstevel@tonic-gate 463*0Sstevel@tonic-gate SSL_load_error_strings(); 464*0Sstevel@tonic-gate 465*0Sstevel@tonic-gate if ((!SSL_CTX_load_verify_locations(tm_ctx,CAfile,CApath)) || 466*0Sstevel@tonic-gate (!SSL_CTX_set_default_verify_paths(tm_ctx))) 467*0Sstevel@tonic-gate { 468*0Sstevel@tonic-gate /* BIO_printf(bio_err,"error setting default verify locations\n"); */ 469*0Sstevel@tonic-gate ERR_print_errors(bio_err); 470*0Sstevel@tonic-gate /* goto end; */ 471*0Sstevel@tonic-gate } 472*0Sstevel@tonic-gate 473*0Sstevel@tonic-gate if (tm_cipher == NULL) 474*0Sstevel@tonic-gate tm_cipher = getenv("SSL_CIPHER"); 475*0Sstevel@tonic-gate 476*0Sstevel@tonic-gate if (tm_cipher == NULL ) { 477*0Sstevel@tonic-gate fprintf( stderr, "No CIPHER specified\n" ); 478*0Sstevel@tonic-gate } 479*0Sstevel@tonic-gate 480*0Sstevel@tonic-gate if (!(perform & 1)) goto next; 481*0Sstevel@tonic-gate printf( "Collecting connection statistics for %d seconds\n", maxTime ); 482*0Sstevel@tonic-gate 483*0Sstevel@tonic-gate /* Loop and time how long it takes to make connections */ 484*0Sstevel@tonic-gate 485*0Sstevel@tonic-gate bytes_read=0; 486*0Sstevel@tonic-gate finishtime=(long)time(NULL)+maxTime; 487*0Sstevel@tonic-gate tm_Time_F(START); 488*0Sstevel@tonic-gate for (;;) 489*0Sstevel@tonic-gate { 490*0Sstevel@tonic-gate if (finishtime < (long)time(NULL)) break; 491*0Sstevel@tonic-gate #ifdef WIN32_STUFF 492*0Sstevel@tonic-gate 493*0Sstevel@tonic-gate if( flushWinMsgs(0) == -1 ) 494*0Sstevel@tonic-gate goto end; 495*0Sstevel@tonic-gate 496*0Sstevel@tonic-gate if( waitingToDie || exitNow ) /* we're dead */ 497*0Sstevel@tonic-gate goto end; 498*0Sstevel@tonic-gate #endif 499*0Sstevel@tonic-gate 500*0Sstevel@tonic-gate if( (scon = doConnection( NULL )) == NULL ) 501*0Sstevel@tonic-gate goto end; 502*0Sstevel@tonic-gate 503*0Sstevel@tonic-gate if (s_www_path != NULL) 504*0Sstevel@tonic-gate { 505*0Sstevel@tonic-gate BIO_snprintf(buf,sizeof buf,"GET %s HTTP/1.0\r\n\r\n",s_www_path); 506*0Sstevel@tonic-gate SSL_write(scon,buf,strlen(buf)); 507*0Sstevel@tonic-gate while ((i=SSL_read(scon,buf,sizeof(buf))) > 0) 508*0Sstevel@tonic-gate bytes_read+=i; 509*0Sstevel@tonic-gate } 510*0Sstevel@tonic-gate 511*0Sstevel@tonic-gate #ifdef NO_SHUTDOWN 512*0Sstevel@tonic-gate SSL_set_shutdown(scon,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN); 513*0Sstevel@tonic-gate #else 514*0Sstevel@tonic-gate SSL_shutdown(scon); 515*0Sstevel@tonic-gate #endif 516*0Sstevel@tonic-gate SHUTDOWN2(SSL_get_fd(scon)); 517*0Sstevel@tonic-gate 518*0Sstevel@tonic-gate nConn += 1; 519*0Sstevel@tonic-gate if (SSL_session_reused(scon)) 520*0Sstevel@tonic-gate ver='r'; 521*0Sstevel@tonic-gate else 522*0Sstevel@tonic-gate { 523*0Sstevel@tonic-gate ver=SSL_version(scon); 524*0Sstevel@tonic-gate if (ver == TLS1_VERSION) 525*0Sstevel@tonic-gate ver='t'; 526*0Sstevel@tonic-gate else if (ver == SSL3_VERSION) 527*0Sstevel@tonic-gate ver='3'; 528*0Sstevel@tonic-gate else if (ver == SSL2_VERSION) 529*0Sstevel@tonic-gate ver='2'; 530*0Sstevel@tonic-gate else 531*0Sstevel@tonic-gate ver='*'; 532*0Sstevel@tonic-gate } 533*0Sstevel@tonic-gate fputc(ver,stdout); 534*0Sstevel@tonic-gate fflush(stdout); 535*0Sstevel@tonic-gate 536*0Sstevel@tonic-gate SSL_free( scon ); 537*0Sstevel@tonic-gate scon=NULL; 538*0Sstevel@tonic-gate } 539*0Sstevel@tonic-gate totalTime += tm_Time_F(STOP); /* Add the time for this iteration */ 540*0Sstevel@tonic-gate 541*0Sstevel@tonic-gate i=(int)((long)time(NULL)-finishtime+maxTime); 542*0Sstevel@tonic-gate printf( "\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n", nConn, totalTime, ((double)nConn/totalTime),bytes_read); 543*0Sstevel@tonic-gate printf( "%d connections in %ld real seconds, %ld bytes read per connection\n",nConn,(long)time(NULL)-finishtime+maxTime,bytes_read/nConn); 544*0Sstevel@tonic-gate 545*0Sstevel@tonic-gate /* Now loop and time connections using the same session id over and over */ 546*0Sstevel@tonic-gate 547*0Sstevel@tonic-gate next: 548*0Sstevel@tonic-gate if (!(perform & 2)) goto end; 549*0Sstevel@tonic-gate printf( "\n\nNow timing with session id reuse.\n" ); 550*0Sstevel@tonic-gate 551*0Sstevel@tonic-gate /* Get an SSL object so we can reuse the session id */ 552*0Sstevel@tonic-gate if( (scon = doConnection( NULL )) == NULL ) 553*0Sstevel@tonic-gate { 554*0Sstevel@tonic-gate fprintf( stderr, "Unable to get connection\n" ); 555*0Sstevel@tonic-gate goto end; 556*0Sstevel@tonic-gate } 557*0Sstevel@tonic-gate 558*0Sstevel@tonic-gate if (s_www_path != NULL) 559*0Sstevel@tonic-gate { 560*0Sstevel@tonic-gate BIO_snprintf(buf,sizeof buf,"GET %s HTTP/1.0\r\n\r\n",s_www_path); 561*0Sstevel@tonic-gate SSL_write(scon,buf,strlen(buf)); 562*0Sstevel@tonic-gate while (SSL_read(scon,buf,sizeof(buf)) > 0) 563*0Sstevel@tonic-gate ; 564*0Sstevel@tonic-gate } 565*0Sstevel@tonic-gate #ifdef NO_SHUTDOWN 566*0Sstevel@tonic-gate SSL_set_shutdown(scon,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN); 567*0Sstevel@tonic-gate #else 568*0Sstevel@tonic-gate SSL_shutdown(scon); 569*0Sstevel@tonic-gate #endif 570*0Sstevel@tonic-gate SHUTDOWN2(SSL_get_fd(scon)); 571*0Sstevel@tonic-gate 572*0Sstevel@tonic-gate nConn = 0; 573*0Sstevel@tonic-gate totalTime = 0.0; 574*0Sstevel@tonic-gate 575*0Sstevel@tonic-gate finishtime=(long)time(NULL)+maxTime; 576*0Sstevel@tonic-gate 577*0Sstevel@tonic-gate printf( "starting\n" ); 578*0Sstevel@tonic-gate bytes_read=0; 579*0Sstevel@tonic-gate tm_Time_F(START); 580*0Sstevel@tonic-gate 581*0Sstevel@tonic-gate for (;;) 582*0Sstevel@tonic-gate { 583*0Sstevel@tonic-gate if (finishtime < (long)time(NULL)) break; 584*0Sstevel@tonic-gate 585*0Sstevel@tonic-gate #ifdef WIN32_STUFF 586*0Sstevel@tonic-gate if( flushWinMsgs(0) == -1 ) 587*0Sstevel@tonic-gate goto end; 588*0Sstevel@tonic-gate 589*0Sstevel@tonic-gate if( waitingToDie || exitNow ) /* we're dead */ 590*0Sstevel@tonic-gate goto end; 591*0Sstevel@tonic-gate #endif 592*0Sstevel@tonic-gate 593*0Sstevel@tonic-gate if( (doConnection( scon )) == NULL ) 594*0Sstevel@tonic-gate goto end; 595*0Sstevel@tonic-gate 596*0Sstevel@tonic-gate if (s_www_path) 597*0Sstevel@tonic-gate { 598*0Sstevel@tonic-gate BIO_snprintf(buf,sizeof buf,"GET %s HTTP/1.0\r\n\r\n",s_www_path); 599*0Sstevel@tonic-gate SSL_write(scon,buf,strlen(buf)); 600*0Sstevel@tonic-gate while ((i=SSL_read(scon,buf,sizeof(buf))) > 0) 601*0Sstevel@tonic-gate bytes_read+=i; 602*0Sstevel@tonic-gate } 603*0Sstevel@tonic-gate 604*0Sstevel@tonic-gate #ifdef NO_SHUTDOWN 605*0Sstevel@tonic-gate SSL_set_shutdown(scon,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN); 606*0Sstevel@tonic-gate #else 607*0Sstevel@tonic-gate SSL_shutdown(scon); 608*0Sstevel@tonic-gate #endif 609*0Sstevel@tonic-gate SHUTDOWN2(SSL_get_fd(scon)); 610*0Sstevel@tonic-gate 611*0Sstevel@tonic-gate nConn += 1; 612*0Sstevel@tonic-gate if (SSL_session_reused(scon)) 613*0Sstevel@tonic-gate ver='r'; 614*0Sstevel@tonic-gate else 615*0Sstevel@tonic-gate { 616*0Sstevel@tonic-gate ver=SSL_version(scon); 617*0Sstevel@tonic-gate if (ver == TLS1_VERSION) 618*0Sstevel@tonic-gate ver='t'; 619*0Sstevel@tonic-gate else if (ver == SSL3_VERSION) 620*0Sstevel@tonic-gate ver='3'; 621*0Sstevel@tonic-gate else if (ver == SSL2_VERSION) 622*0Sstevel@tonic-gate ver='2'; 623*0Sstevel@tonic-gate else 624*0Sstevel@tonic-gate ver='*'; 625*0Sstevel@tonic-gate } 626*0Sstevel@tonic-gate fputc(ver,stdout); 627*0Sstevel@tonic-gate fflush(stdout); 628*0Sstevel@tonic-gate } 629*0Sstevel@tonic-gate totalTime += tm_Time_F(STOP); /* Add the time for this iteration*/ 630*0Sstevel@tonic-gate 631*0Sstevel@tonic-gate 632*0Sstevel@tonic-gate printf( "\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n", nConn, totalTime, ((double)nConn/totalTime),bytes_read); 633*0Sstevel@tonic-gate printf( "%d connections in %ld real seconds, %ld bytes read per connection\n",nConn,(long)time(NULL)-finishtime+maxTime,bytes_read/nConn); 634*0Sstevel@tonic-gate 635*0Sstevel@tonic-gate ret=0; 636*0Sstevel@tonic-gate end: 637*0Sstevel@tonic-gate if (scon != NULL) SSL_free(scon); 638*0Sstevel@tonic-gate 639*0Sstevel@tonic-gate if (tm_ctx != NULL) 640*0Sstevel@tonic-gate { 641*0Sstevel@tonic-gate SSL_CTX_free(tm_ctx); 642*0Sstevel@tonic-gate tm_ctx=NULL; 643*0Sstevel@tonic-gate } 644*0Sstevel@tonic-gate apps_shutdown(); 645*0Sstevel@tonic-gate OPENSSL_EXIT(ret); 646*0Sstevel@tonic-gate } 647*0Sstevel@tonic-gate 648*0Sstevel@tonic-gate /*********************************************************************** 649*0Sstevel@tonic-gate * doConnection - make a connection 650*0Sstevel@tonic-gate * Args: 651*0Sstevel@tonic-gate * scon = earlier ssl connection for session id, or NULL 652*0Sstevel@tonic-gate * Returns: 653*0Sstevel@tonic-gate * SSL * = the connection pointer. 654*0Sstevel@tonic-gate */ 655*0Sstevel@tonic-gate static SSL *doConnection(SSL *scon) 656*0Sstevel@tonic-gate { 657*0Sstevel@tonic-gate BIO *conn; 658*0Sstevel@tonic-gate SSL *serverCon; 659*0Sstevel@tonic-gate int width, i; 660*0Sstevel@tonic-gate fd_set readfds; 661*0Sstevel@tonic-gate 662*0Sstevel@tonic-gate if ((conn=BIO_new(BIO_s_connect())) == NULL) 663*0Sstevel@tonic-gate return(NULL); 664*0Sstevel@tonic-gate 665*0Sstevel@tonic-gate /* BIO_set_conn_port(conn,port);*/ 666*0Sstevel@tonic-gate BIO_set_conn_hostname(conn,host); 667*0Sstevel@tonic-gate 668*0Sstevel@tonic-gate if (scon == NULL) 669*0Sstevel@tonic-gate serverCon=SSL_new(tm_ctx); 670*0Sstevel@tonic-gate else 671*0Sstevel@tonic-gate { 672*0Sstevel@tonic-gate serverCon=scon; 673*0Sstevel@tonic-gate SSL_set_connect_state(serverCon); 674*0Sstevel@tonic-gate } 675*0Sstevel@tonic-gate 676*0Sstevel@tonic-gate SSL_set_bio(serverCon,conn,conn); 677*0Sstevel@tonic-gate 678*0Sstevel@tonic-gate #if 0 679*0Sstevel@tonic-gate if( scon != NULL ) 680*0Sstevel@tonic-gate SSL_set_session(serverCon,SSL_get_session(scon)); 681*0Sstevel@tonic-gate #endif 682*0Sstevel@tonic-gate 683*0Sstevel@tonic-gate /* ok, lets connect */ 684*0Sstevel@tonic-gate for(;;) { 685*0Sstevel@tonic-gate i=SSL_connect(serverCon); 686*0Sstevel@tonic-gate if (BIO_sock_should_retry(i)) 687*0Sstevel@tonic-gate { 688*0Sstevel@tonic-gate BIO_printf(bio_err,"DELAY\n"); 689*0Sstevel@tonic-gate 690*0Sstevel@tonic-gate i=SSL_get_fd(serverCon); 691*0Sstevel@tonic-gate width=i+1; 692*0Sstevel@tonic-gate FD_ZERO(&readfds); 693*0Sstevel@tonic-gate FD_SET(i,&readfds); 694*0Sstevel@tonic-gate /* Note: under VMS with SOCKETSHR the 2nd parameter 695*0Sstevel@tonic-gate * is currently of type (int *) whereas under other 696*0Sstevel@tonic-gate * systems it is (void *) if you don't have a cast it 697*0Sstevel@tonic-gate * will choke the compiler: if you do have a cast then 698*0Sstevel@tonic-gate * you can either go for (int *) or (void *). 699*0Sstevel@tonic-gate */ 700*0Sstevel@tonic-gate select(width,(void *)&readfds,NULL,NULL,NULL); 701*0Sstevel@tonic-gate continue; 702*0Sstevel@tonic-gate } 703*0Sstevel@tonic-gate break; 704*0Sstevel@tonic-gate } 705*0Sstevel@tonic-gate if(i <= 0) 706*0Sstevel@tonic-gate { 707*0Sstevel@tonic-gate BIO_printf(bio_err,"ERROR\n"); 708*0Sstevel@tonic-gate if (verify_error != X509_V_OK) 709*0Sstevel@tonic-gate BIO_printf(bio_err,"verify error:%s\n", 710*0Sstevel@tonic-gate X509_verify_cert_error_string(verify_error)); 711*0Sstevel@tonic-gate else 712*0Sstevel@tonic-gate ERR_print_errors(bio_err); 713*0Sstevel@tonic-gate if (scon == NULL) 714*0Sstevel@tonic-gate SSL_free(serverCon); 715*0Sstevel@tonic-gate return NULL; 716*0Sstevel@tonic-gate } 717*0Sstevel@tonic-gate 718*0Sstevel@tonic-gate return serverCon; 719*0Sstevel@tonic-gate } 720*0Sstevel@tonic-gate 721*0Sstevel@tonic-gate 722