]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/s_time.c
Add the configuration target VxWorks.
[thirdparty/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 #ifdef OPENSSL_NO_STDIO
73 #define APPS_WIN16
74 #endif
75 #include <openssl/x509.h>
76 #include <openssl/ssl.h>
77 #include <openssl/pem.h>
78 #include "s_apps.h"
79 #include <openssl/err.h>
80 #ifdef WIN32_STUFF
81 #include "winmain.h"
82 #include "wintext.h"
83 #endif
84 #if !defined(OPENSSL_SYS_MSDOS)
85 #include OPENSSL_UNISTD
86 #endif
87
88 #if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VXWORKS) && (!defined(OPENSSL_SYS_VMS) || defined(__DECC)) && !defined(OPENSSL_SYS_MACOSX)
89 #define TIMES
90 #endif
91
92 #ifndef _IRIX
93 #include <time.h>
94 #endif
95 #ifdef TIMES
96 #include <sys/types.h>
97 #include <sys/times.h>
98 #endif
99
100 /* Depending on the VMS version, the tms structure is perhaps defined.
101 The __TMS macro will show if it was. If it wasn't defined, we should
102 undefine TIMES, since that tells the rest of the program how things
103 should be handled. -- Richard Levitte */
104 #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS)
105 #undef TIMES
106 #endif
107
108 #if !defined(TIMES) && !defined(OPENSSL_SYS_VXWORKS)
109 #include <sys/timeb.h>
110 #endif
111
112 #ifdef _AIX
113 #include <sys/select.h>
114 #endif
115
116 #if defined(sun) || defined(__ultrix)
117 #define _POSIX_SOURCE
118 #include <limits.h>
119 #include <sys/param.h>
120 #endif
121
122 /* The following if from times(3) man page. It may need to be changed
123 */
124 #ifndef HZ
125 # ifdef _SC_CLK_TCK
126 # define HZ ((double)sysconf(_SC_CLK_TCK))
127 # else
128 # ifndef CLK_TCK
129 # ifndef _BSD_CLK_TCK_ /* FreeBSD hack */
130 # define HZ 100.0
131 # else /* _BSD_CLK_TCK_ */
132 # define HZ ((double)_BSD_CLK_TCK_)
133 # endif
134 # else /* CLK_TCK */
135 # define HZ ((double)CLK_TCK)
136 # endif
137 # endif
138 #endif
139
140 #undef PROG
141 #define PROG s_time_main
142
143 #undef ioctl
144 #define ioctl ioctlsocket
145
146 #define SSL_CONNECT_NAME "localhost:4433"
147
148 /*#define TEST_CERT "client.pem" */ /* no default cert. */
149
150 #undef BUFSIZZ
151 #define BUFSIZZ 1024*10
152
153 #undef min
154 #undef max
155 #define min(a,b) (((a) < (b)) ? (a) : (b))
156 #define max(a,b) (((a) > (b)) ? (a) : (b))
157
158 #undef SECONDS
159 #define SECONDS 30
160 extern int verify_depth;
161 extern int verify_error;
162
163 static void s_time_usage(void);
164 static int parseArgs( int argc, char **argv );
165 static SSL *doConnection( SSL *scon );
166 static void s_time_init(void);
167
168 /***********************************************************************
169 * Static data declarations
170 */
171
172 /* static char *port=PORT_STR;*/
173 static char *host=SSL_CONNECT_NAME;
174 static char *t_cert_file=NULL;
175 static char *t_key_file=NULL;
176 static char *CApath=NULL;
177 static char *CAfile=NULL;
178 static char *tm_cipher=NULL;
179 static int tm_verify = SSL_VERIFY_NONE;
180 static int maxTime = SECONDS;
181 static SSL_CTX *tm_ctx=NULL;
182 static SSL_METHOD *s_time_meth=NULL;
183 static char *s_www_path=NULL;
184 static long bytes_read=0;
185 static int st_bugs=0;
186 static int perform=0;
187 #ifdef FIONBIO
188 static int t_nbio=0;
189 #endif
190 #ifdef OPENSSL_SYS_WIN32
191 static int exitNow = 0; /* Set when it's time to exit main */
192 #endif
193
194 static void s_time_init(void)
195 {
196 host=SSL_CONNECT_NAME;
197 t_cert_file=NULL;
198 t_key_file=NULL;
199 CApath=NULL;
200 CAfile=NULL;
201 tm_cipher=NULL;
202 tm_verify = SSL_VERIFY_NONE;
203 maxTime = SECONDS;
204 tm_ctx=NULL;
205 s_time_meth=NULL;
206 s_www_path=NULL;
207 bytes_read=0;
208 st_bugs=0;
209 perform=0;
210
211 #ifdef FIONBIO
212 t_nbio=0;
213 #endif
214 #ifdef OPENSSL_SYS_WIN32
215 exitNow = 0; /* Set when it's time to exit main */
216 #endif
217 }
218
219 /***********************************************************************
220 * usage - display usage message
221 */
222 static void s_time_usage(void)
223 {
224 static char umsg[] = "\
225 -time arg - max number of seconds to collect data, default %d\n\
226 -verify arg - turn on peer certificate verification, arg == depth\n\
227 -cert arg - certificate file to use, PEM format assumed\n\
228 -key arg - RSA file to use, PEM format assumed, key is in cert file\n\
229 file if not specified by this option\n\
230 -CApath arg - PEM format directory of CA's\n\
231 -CAfile arg - PEM format file of CA's\n\
232 -cipher - preferred cipher to use, play with 'openssl ciphers'\n\n";
233
234 printf( "usage: s_time <args>\n\n" );
235
236 printf("-connect host:port - host:port to connect to (default is %s)\n",SSL_CONNECT_NAME);
237 #ifdef FIONBIO
238 printf("-nbio - Run with non-blocking IO\n");
239 printf("-ssl2 - Just use SSLv2\n");
240 printf("-ssl3 - Just use SSLv3\n");
241 printf("-bugs - Turn on SSL bug compatibility\n");
242 printf("-new - Just time new connections\n");
243 printf("-reuse - Just time connection reuse\n");
244 printf("-www page - Retrieve 'page' from the site\n");
245 #endif
246 printf( umsg,SECONDS );
247 }
248
249 /***********************************************************************
250 * parseArgs - Parse command line arguments and initialize data
251 *
252 * Returns 0 if ok, -1 on bad args
253 */
254 static int parseArgs(int argc, char **argv)
255 {
256 int badop = 0;
257
258 verify_depth=0;
259 verify_error=X509_V_OK;
260
261 argc--;
262 argv++;
263
264 while (argc >= 1) {
265 if (strcmp(*argv,"-connect") == 0)
266 {
267 if (--argc < 1) goto bad;
268 host= *(++argv);
269 }
270 #if 0
271 else if( strcmp(*argv,"-host") == 0)
272 {
273 if (--argc < 1) goto bad;
274 host= *(++argv);
275 }
276 else if( strcmp(*argv,"-port") == 0)
277 {
278 if (--argc < 1) goto bad;
279 port= *(++argv);
280 }
281 #endif
282 else if (strcmp(*argv,"-reuse") == 0)
283 perform=2;
284 else if (strcmp(*argv,"-new") == 0)
285 perform=1;
286 else if( strcmp(*argv,"-verify") == 0) {
287
288 tm_verify=SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE;
289 if (--argc < 1) goto bad;
290 verify_depth=atoi(*(++argv));
291 BIO_printf(bio_err,"verify depth is %d\n",verify_depth);
292
293 } else if( strcmp(*argv,"-cert") == 0) {
294
295 if (--argc < 1) goto bad;
296 t_cert_file= *(++argv);
297
298 } else if( strcmp(*argv,"-key") == 0) {
299
300 if (--argc < 1) goto bad;
301 t_key_file= *(++argv);
302
303 } else if( strcmp(*argv,"-CApath") == 0) {
304
305 if (--argc < 1) goto bad;
306 CApath= *(++argv);
307
308 } else if( strcmp(*argv,"-CAfile") == 0) {
309
310 if (--argc < 1) goto bad;
311 CAfile= *(++argv);
312
313 } else if( strcmp(*argv,"-cipher") == 0) {
314
315 if (--argc < 1) goto bad;
316 tm_cipher= *(++argv);
317 }
318 #ifdef FIONBIO
319 else if(strcmp(*argv,"-nbio") == 0) {
320 t_nbio=1;
321 }
322 #endif
323 else if(strcmp(*argv,"-www") == 0)
324 {
325 if (--argc < 1) goto bad;
326 s_www_path= *(++argv);
327 }
328 else if(strcmp(*argv,"-bugs") == 0)
329 st_bugs=1;
330 #ifndef OPENSSL_NO_SSL2
331 else if(strcmp(*argv,"-ssl2") == 0)
332 s_time_meth=SSLv2_client_method();
333 #endif
334 #ifndef OPENSSL_NO_SSL3
335 else if(strcmp(*argv,"-ssl3") == 0)
336 s_time_meth=SSLv3_client_method();
337 #endif
338 else if( strcmp(*argv,"-time") == 0) {
339
340 if (--argc < 1) goto bad;
341 maxTime= atoi(*(++argv));
342 }
343 else {
344 BIO_printf(bio_err,"unknown option %s\n",*argv);
345 badop=1;
346 break;
347 }
348
349 argc--;
350 argv++;
351 }
352
353 if (perform == 0) perform=3;
354
355 if(badop) {
356 bad:
357 s_time_usage();
358 return -1;
359 }
360
361 return 0; /* Valid args */
362 }
363
364 /***********************************************************************
365 * TIME - time functions
366 */
367 #define START 0
368 #define STOP 1
369
370 static double tm_Time_F(int s)
371 {
372 static double ret;
373 #ifdef TIMES
374 static struct tms tstart,tend;
375
376 if(s == START) {
377 times(&tstart);
378 return(0);
379 } else {
380 times(&tend);
381 ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
382 return((ret == 0.0)?1e-6:ret);
383 }
384 #elif defined(OPENSSL_SYS_VXWORKS)
385 {
386 static unsigned long tick_start, tick_end;
387
388 if( s == START )
389 {
390 tick_start = tickGet();
391 return 0;
392 }
393 else
394 {
395 tick_end = tickGet();
396 ret = (double)(tick_end - tick_start) / (double)sysClkRateGet();
397 return((ret == 0.0)?1e-6:ret);
398 }
399 }
400 #else /* !times() */
401 static struct timeb tstart,tend;
402 long i;
403
404 if(s == START) {
405 ftime(&tstart);
406 return(0);
407 } else {
408 ftime(&tend);
409 i=(long)tend.millitm-(long)tstart.millitm;
410 ret=((double)(tend.time-tstart.time))+((double)i)/1000.0;
411 return((ret == 0.0)?1e-6:ret);
412 }
413 #endif
414 }
415
416 /***********************************************************************
417 * MAIN - main processing area for client
418 * real name depends on MONOLITH
419 */
420 int MAIN(int, char **);
421
422 int MAIN(int argc, char **argv)
423 {
424 double totalTime = 0.0;
425 int nConn = 0;
426 SSL *scon=NULL;
427 long finishtime=0;
428 int ret=1,i;
429 MS_STATIC char buf[1024*8];
430 int ver;
431
432 apps_startup();
433 s_time_init();
434
435 if (bio_err == NULL)
436 bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
437
438 #if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
439 s_time_meth=SSLv23_client_method();
440 #elif !defined(OPENSSL_NO_SSL3)
441 s_time_meth=SSLv3_client_method();
442 #elif !defined(OPENSSL_NO_SSL2)
443 s_time_meth=SSLv2_client_method();
444 #endif
445
446 /* parse the command line arguments */
447 if( parseArgs( argc, argv ) < 0 )
448 goto end;
449
450 OpenSSL_add_ssl_algorithms();
451 if ((tm_ctx=SSL_CTX_new(s_time_meth)) == NULL) return(1);
452
453 SSL_CTX_set_quiet_shutdown(tm_ctx,1);
454
455 if (st_bugs) SSL_CTX_set_options(tm_ctx,SSL_OP_ALL);
456 SSL_CTX_set_cipher_list(tm_ctx,tm_cipher);
457 if(!set_cert_stuff(tm_ctx,t_cert_file,t_key_file))
458 goto end;
459
460 SSL_load_error_strings();
461
462 if ((!SSL_CTX_load_verify_locations(tm_ctx,CAfile,CApath)) ||
463 (!SSL_CTX_set_default_verify_paths(tm_ctx)))
464 {
465 /* BIO_printf(bio_err,"error setting default verify locations\n"); */
466 ERR_print_errors(bio_err);
467 /* goto end; */
468 }
469
470 if (tm_cipher == NULL)
471 tm_cipher = getenv("SSL_CIPHER");
472
473 if (tm_cipher == NULL ) {
474 fprintf( stderr, "No CIPHER specified\n" );
475 }
476
477 if (!(perform & 1)) goto next;
478 printf( "Collecting connection statistics for %d seconds\n", maxTime );
479
480 /* Loop and time how long it takes to make connections */
481
482 bytes_read=0;
483 finishtime=(long)time(NULL)+maxTime;
484 tm_Time_F(START);
485 for (;;)
486 {
487 if (finishtime < time(NULL)) break;
488 #ifdef WIN32_STUFF
489
490 if( flushWinMsgs(0) == -1 )
491 goto end;
492
493 if( waitingToDie || exitNow ) /* we're dead */
494 goto end;
495 #endif
496
497 if( (scon = doConnection( NULL )) == NULL )
498 goto end;
499
500 if (s_www_path != NULL)
501 {
502 sprintf(buf,"GET %s HTTP/1.0\r\n\r\n",s_www_path);
503 SSL_write(scon,buf,strlen(buf));
504 while ((i=SSL_read(scon,buf,sizeof(buf))) > 0)
505 bytes_read+=i;
506 }
507
508 #ifdef NO_SHUTDOWN
509 SSL_set_shutdown(scon,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
510 #else
511 SSL_shutdown(scon);
512 #endif
513 SHUTDOWN2(SSL_get_fd(scon));
514
515 nConn += 1;
516 if (SSL_session_reused(scon))
517 ver='r';
518 else
519 {
520 ver=SSL_version(scon);
521 if (ver == TLS1_VERSION)
522 ver='t';
523 else if (ver == SSL3_VERSION)
524 ver='3';
525 else if (ver == SSL2_VERSION)
526 ver='2';
527 else
528 ver='*';
529 }
530 fputc(ver,stdout);
531 fflush(stdout);
532
533 SSL_free( scon );
534 scon=NULL;
535 }
536 totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
537
538 i=(int)(time(NULL)-finishtime+maxTime);
539 printf( "\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n", nConn, totalTime, ((double)nConn/totalTime),bytes_read);
540 printf( "%d connections in %ld real seconds, %ld bytes read per connection\n",nConn,time(NULL)-finishtime+maxTime,bytes_read/nConn);
541
542 /* Now loop and time connections using the same session id over and over */
543
544 next:
545 if (!(perform & 2)) goto end;
546 printf( "\n\nNow timing with session id reuse.\n" );
547
548 /* Get an SSL object so we can reuse the session id */
549 if( (scon = doConnection( NULL )) == NULL )
550 {
551 fprintf( stderr, "Unable to get connection\n" );
552 goto end;
553 }
554
555 if (s_www_path != NULL)
556 {
557 sprintf(buf,"GET %s HTTP/1.0\r\n\r\n",s_www_path);
558 SSL_write(scon,buf,strlen(buf));
559 while (SSL_read(scon,buf,sizeof(buf)) > 0)
560 ;
561 }
562 #ifdef NO_SHUTDOWN
563 SSL_set_shutdown(scon,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
564 #else
565 SSL_shutdown(scon);
566 #endif
567 SHUTDOWN2(SSL_get_fd(scon));
568
569 nConn = 0;
570 totalTime = 0.0;
571
572 finishtime=time(NULL)+maxTime;
573
574 printf( "starting\n" );
575 bytes_read=0;
576 tm_Time_F(START);
577
578 for (;;)
579 {
580 if (finishtime < time(NULL)) break;
581
582 #ifdef WIN32_STUFF
583 if( flushWinMsgs(0) == -1 )
584 goto end;
585
586 if( waitingToDie || exitNow ) /* we're dead */
587 goto end;
588 #endif
589
590 if( (doConnection( scon )) == NULL )
591 goto end;
592
593 if (s_www_path)
594 {
595 sprintf(buf,"GET %s HTTP/1.0\r\n\r\n",s_www_path);
596 SSL_write(scon,buf,strlen(buf));
597 while ((i=SSL_read(scon,buf,sizeof(buf))) > 0)
598 bytes_read+=i;
599 }
600
601 #ifdef NO_SHUTDOWN
602 SSL_set_shutdown(scon,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
603 #else
604 SSL_shutdown(scon);
605 #endif
606 SHUTDOWN2(SSL_get_fd(scon));
607
608 nConn += 1;
609 if (SSL_session_reused(scon))
610 ver='r';
611 else
612 {
613 ver=SSL_version(scon);
614 if (ver == TLS1_VERSION)
615 ver='t';
616 else if (ver == SSL3_VERSION)
617 ver='3';
618 else if (ver == SSL2_VERSION)
619 ver='2';
620 else
621 ver='*';
622 }
623 fputc(ver,stdout);
624 fflush(stdout);
625 }
626 totalTime += tm_Time_F(STOP); /* Add the time for this iteration*/
627
628
629 printf( "\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n", nConn, totalTime, ((double)nConn/totalTime),bytes_read);
630 printf( "%d connections in %ld real seconds, %ld bytes read per connection\n",nConn,time(NULL)-finishtime+maxTime,bytes_read/nConn);
631
632 ret=0;
633 end:
634 if (scon != NULL) SSL_free(scon);
635
636 if (tm_ctx != NULL)
637 {
638 SSL_CTX_free(tm_ctx);
639 tm_ctx=NULL;
640 }
641 apps_shutdown();
642 EXIT(ret);
643 }
644
645 /***********************************************************************
646 * doConnection - make a connection
647 * Args:
648 * scon = earlier ssl connection for session id, or NULL
649 * Returns:
650 * SSL * = the connection pointer.
651 */
652 static SSL *doConnection(SSL *scon)
653 {
654 BIO *conn;
655 SSL *serverCon;
656 int width, i;
657 fd_set readfds;
658
659 if ((conn=BIO_new(BIO_s_connect())) == NULL)
660 return(NULL);
661
662 /* BIO_set_conn_port(conn,port);*/
663 BIO_set_conn_hostname(conn,host);
664
665 if (scon == NULL)
666 serverCon=SSL_new(tm_ctx);
667 else
668 {
669 serverCon=scon;
670 SSL_set_connect_state(serverCon);
671 }
672
673 SSL_set_bio(serverCon,conn,conn);
674
675 #if 0
676 if( scon != NULL )
677 SSL_set_session(serverCon,SSL_get_session(scon));
678 #endif
679
680 /* ok, lets connect */
681 for(;;) {
682 i=SSL_connect(serverCon);
683 if (BIO_sock_should_retry(i))
684 {
685 BIO_printf(bio_err,"DELAY\n");
686
687 i=SSL_get_fd(serverCon);
688 width=i+1;
689 FD_ZERO(&readfds);
690 FD_SET(i,&readfds);
691 /* Note: under VMS with SOCKETSHR the 2nd parameter
692 * is currently of type (int *) whereas under other
693 * systems it is (void *) if you don't have a cast it
694 * will choke the compiler: if you do have a cast then
695 * you can either go for (int *) or (void *).
696 */
697 select(width,(void *)&readfds,NULL,NULL,NULL);
698 continue;
699 }
700 break;
701 }
702 if(i <= 0)
703 {
704 BIO_printf(bio_err,"ERROR\n");
705 if (verify_error != X509_V_OK)
706 BIO_printf(bio_err,"verify error:%s\n",
707 X509_verify_cert_error_string(verify_error));
708 else
709 ERR_print_errors(bio_err);
710 if (scon == NULL)
711 SSL_free(serverCon);
712 return NULL;
713 }
714
715 return serverCon;
716 }
717
718