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