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