Add support for -no-CApath and -no-CAfile options
[openssl.git] / apps / s_time.c
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young (eay@cryptsoft.com)"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.]
56  */
57
58 #define NO_SHUTDOWN
59
60 /* ----------------------------------------
61    s_time - SSL client connection timer program
62    Written and donated by Larry Streepy <streepy@healthcare.com>
63   -----------------------------------------*/
64
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68
69 #define USE_SOCKETS
70 #include "apps.h"
71 #include <openssl/x509.h>
72 #include <openssl/ssl.h>
73 #include <openssl/pem.h>
74 #include "s_apps.h"
75 #include <openssl/err.h>
76 #ifdef WIN32_STUFF
77 # include "winmain.h"
78 # include "wintext.h"
79 #endif
80 #if !defined(OPENSSL_SYS_MSDOS)
81 # include OPENSSL_UNISTD
82 #endif
83
84 #undef ioctl
85 #define ioctl ioctlsocket
86
87 #define SSL_CONNECT_NAME        "localhost:4433"
88
89 /* no default cert. */
90 /*
91  * #define TEST_CERT "client.pem"
92  */
93
94 #undef BUFSIZZ
95 #define BUFSIZZ 1024*10
96
97 #define MYBUFSIZ 1024*8
98
99 #undef min
100 #undef max
101 #define min(a,b) (((a) < (b)) ? (a) : (b))
102 #define max(a,b) (((a) > (b)) ? (a) : (b))
103
104 #undef SECONDS
105 #define SECONDS 30
106 #define SECONDSSTR "30"
107
108 extern int verify_depth;
109 extern int verify_error;
110
111 static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx);
112
113 typedef enum OPTION_choice {
114     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
115     OPT_CONNECT, OPT_CIPHER, OPT_CERT, OPT_KEY, OPT_CAPATH,
116     OPT_CAFILE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_NEW, OPT_REUSE, OPT_BUGS,
117     OPT_VERIFY, OPT_TIME, OPT_SSL3,
118     OPT_WWW
119 } OPTION_CHOICE;
120
121 OPTIONS s_time_options[] = {
122     {"help", OPT_HELP, '-', "Display this summary"},
123     {"connect", OPT_CONNECT, 's',
124      "Where to connect as post:port (default is " SSL_CONNECT_NAME ")"},
125     {"cipher", OPT_CIPHER, 's', "Cipher to use, see 'openssl ciphers'"},
126     {"cert", OPT_CERT, '<', "Cert file to use, PEM format assumed"},
127     {"key", OPT_KEY, '<', "File with key, PEM; default is -cert file"},
128     {"CApath", OPT_CAPATH, '/', "PEM format directory of CA's"},
129     {"cafile", OPT_CAFILE, '<', "PEM format file of CA's"},
130     {"no-CAfile", OPT_NOCAFILE, '-',
131      "Do not load the default certificates file"},
132     {"no-CApath", OPT_NOCAPATH, '-',
133      "Do not load certificates from the default certificates directory"},
134     {"new", OPT_NEW, '-', "Just time new connections"},
135     {"reuse", OPT_REUSE, '-', "Just time connection reuse"},
136     {"bugs", OPT_BUGS, '-', "Turn on SSL bug compatibility"},
137     {"verify", OPT_VERIFY, 'p',
138      "Turn on peer certificate verification, set depth"},
139     {"time", OPT_TIME, 'p', "Sf seconds to collect data, default" SECONDSSTR},
140     {"www", OPT_WWW, 's', "Fetch specified page from the site"},
141 #ifndef OPENSSL_NO_SSL3
142     {"ssl3", OPT_SSL3, '-', "Just use SSLv3"},
143 #endif
144     {NULL}
145 };
146
147 #define START   0
148 #define STOP    1
149
150 static double tm_Time_F(int s)
151 {
152     return app_tminterval(s, 1);
153 }
154
155 int s_time_main(int argc, char **argv)
156 {
157     char buf[1024 * 8];
158     SSL *scon = NULL;
159     SSL_CTX *ctx = NULL;
160     const SSL_METHOD *meth = NULL;
161     char *CApath = NULL, *CAfile = NULL, *cipher = NULL, *www_path = NULL;
162     char *host = SSL_CONNECT_NAME, *certfile = NULL, *keyfile = NULL, *prog;
163     double totalTime = 0.0;
164     int noCApath = 0, noCAfile = 0;
165     int maxtime = SECONDS, nConn = 0, perform = 3, ret = 1, i, st_bugs =
166         0, ver;
167     long bytes_read = 0, finishtime = 0;
168     OPTION_CHOICE o;
169 #ifdef OPENSSL_SYS_WIN32
170     int exitNow = 0;            /* Set when it's time to exit main */
171 #endif
172
173     meth = TLS_client_method();
174     verify_depth = 0;
175     verify_error = X509_V_OK;
176
177     prog = opt_init(argc, argv, s_time_options);
178     while ((o = opt_next()) != OPT_EOF) {
179         switch (o) {
180         case OPT_EOF:
181         case OPT_ERR:
182  opthelp:
183             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
184             goto end;
185         case OPT_HELP:
186             opt_help(s_time_options);
187             ret = 0;
188             goto end;
189         case OPT_CONNECT:
190             host = opt_arg();
191             break;
192         case OPT_REUSE:
193             perform = 2;
194             break;
195         case OPT_NEW:
196             perform = 1;
197             break;
198         case OPT_VERIFY:
199             if (!opt_int(opt_arg(), &verify_depth))
200                 goto opthelp;
201             BIO_printf(bio_err, "%s: verify depth is %d\n",
202                        prog, verify_depth);
203             break;
204         case OPT_CERT:
205             certfile = opt_arg();
206             break;
207         case OPT_KEY:
208             keyfile = opt_arg();
209             break;
210         case OPT_CAPATH:
211             CApath = opt_arg();
212             break;
213         case OPT_CAFILE:
214             CAfile = opt_arg();
215             break;
216         case OPT_NOCAPATH:
217             noCApath = 1;
218             break;
219         case OPT_NOCAFILE:
220             noCAfile = 1;
221             break;
222         case OPT_CIPHER:
223             cipher = opt_arg();
224             break;
225         case OPT_BUGS:
226             st_bugs = 1;
227             break;
228         case OPT_TIME:
229             if (!opt_int(opt_arg(), &maxtime))
230                 goto opthelp;
231             break;
232         case OPT_WWW:
233             www_path = opt_arg();
234             if (strlen(www_path) > MYBUFSIZ - 100) {
235                 BIO_printf(bio_err, "%s: -www option too long\n", prog);
236                 goto end;
237             }
238             break;
239         case OPT_SSL3:
240 #ifndef OPENSSL_NO_SSL3
241             meth = SSLv3_client_method();
242 #endif
243             break;
244         }
245     }
246     argc = opt_num_rest();
247     argv = opt_rest();
248
249     if (cipher == NULL)
250         cipher = getenv("SSL_CIPHER");
251     if (cipher == NULL) {
252         BIO_printf(bio_err, "No CIPHER specified\n");
253         goto end;
254     }
255
256     if ((ctx = SSL_CTX_new(meth)) == NULL)
257         goto end;
258
259     SSL_CTX_set_quiet_shutdown(ctx, 1);
260
261     if (st_bugs)
262         SSL_CTX_set_options(ctx, SSL_OP_ALL);
263     if (!SSL_CTX_set_cipher_list(ctx, cipher))
264         goto end;
265     if (!set_cert_stuff(ctx, certfile, keyfile))
266         goto end;
267
268     if (!ctx_set_verify_locations(ctx, CAfile, CApath, noCAfile, noCApath)) {
269         ERR_print_errors(bio_err);
270         goto end;
271     }
272     if (!(perform & 1))
273         goto next;
274     printf("Collecting connection statistics for %d seconds\n", maxtime);
275
276     /* Loop and time how long it takes to make connections */
277
278     bytes_read = 0;
279     finishtime = (long)time(NULL) + maxtime;
280     tm_Time_F(START);
281     for (;;) {
282         if (finishtime < (long)time(NULL))
283             break;
284 #ifdef WIN32_STUFF
285
286         if (flushWinMsgs(0) == -1)
287             goto end;
288
289         if (waitingToDie || exitNow) /* we're dead */
290             goto end;
291 #endif
292
293         if ((scon = doConnection(NULL, host, ctx)) == NULL)
294             goto end;
295
296         if (www_path != NULL) {
297             BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n",
298                          www_path);
299             if (SSL_write(scon, buf, strlen(buf)) <= 0)
300                 goto end;
301             while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
302                 bytes_read += i;
303         }
304 #ifdef NO_SHUTDOWN
305         SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
306 #else
307         SSL_shutdown(scon);
308 #endif
309         SHUTDOWN2(SSL_get_fd(scon));
310
311         nConn += 1;
312         if (SSL_session_reused(scon))
313             ver = 'r';
314         else {
315             ver = SSL_version(scon);
316             if (ver == TLS1_VERSION)
317                 ver = 't';
318             else if (ver == SSL3_VERSION)
319                 ver = '3';
320             else
321                 ver = '*';
322         }
323         fputc(ver, stdout);
324         fflush(stdout);
325
326         SSL_free(scon);
327         scon = NULL;
328     }
329     totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
330
331     i = (int)((long)time(NULL) - finishtime + maxtime);
332     printf
333         ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
334          nConn, totalTime, ((double)nConn / totalTime), bytes_read);
335     printf
336         ("%d connections in %ld real seconds, %ld bytes read per connection\n",
337          nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
338
339     /*
340      * Now loop and time connections using the same session id over and over
341      */
342
343  next:
344     if (!(perform & 2))
345         goto end;
346     printf("\n\nNow timing with session id reuse.\n");
347
348     /* Get an SSL object so we can reuse the session id */
349     if ((scon = doConnection(NULL, host, ctx)) == NULL) {
350         BIO_printf(bio_err, "Unable to get connection\n");
351         goto end;
352     }
353
354     if (www_path != NULL) {
355         BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n", www_path);
356         if (SSL_write(scon, buf, strlen(buf)) <= 0)
357             goto end;
358         while (SSL_read(scon, buf, sizeof(buf)) > 0)
359             continue;
360     }
361 #ifdef NO_SHUTDOWN
362     SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
363 #else
364     SSL_shutdown(scon);
365 #endif
366     SHUTDOWN2(SSL_get_fd(scon));
367
368     nConn = 0;
369     totalTime = 0.0;
370
371     finishtime = (long)time(NULL) + maxtime;
372
373     printf("starting\n");
374     bytes_read = 0;
375     tm_Time_F(START);
376
377     for (;;) {
378         if (finishtime < (long)time(NULL))
379             break;
380
381 #ifdef WIN32_STUFF
382         if (flushWinMsgs(0) == -1)
383             goto end;
384
385         if (waitingToDie || exitNow) /* we're dead */
386             goto end;
387 #endif
388
389         if ((doConnection(scon, host, ctx)) == NULL)
390             goto end;
391
392         if (www_path) {
393             BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n",
394                          www_path);
395             if (SSL_write(scon, buf, strlen(buf)) <= 0)
396                 goto end;
397             while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
398                 bytes_read += i;
399         }
400 #ifdef NO_SHUTDOWN
401         SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
402 #else
403         SSL_shutdown(scon);
404 #endif
405         SHUTDOWN2(SSL_get_fd(scon));
406
407         nConn += 1;
408         if (SSL_session_reused(scon))
409             ver = 'r';
410         else {
411             ver = SSL_version(scon);
412             if (ver == TLS1_VERSION)
413                 ver = 't';
414             else if (ver == SSL3_VERSION)
415                 ver = '3';
416             else
417                 ver = '*';
418         }
419         fputc(ver, stdout);
420         fflush(stdout);
421     }
422     totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
423
424     printf
425         ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
426          nConn, totalTime, ((double)nConn / totalTime), bytes_read);
427     printf
428         ("%d connections in %ld real seconds, %ld bytes read per connection\n",
429          nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
430
431     ret = 0;
432
433  end:
434     SSL_free(scon);
435     SSL_CTX_free(ctx);
436     return (ret);
437 }
438
439 /*-
440  * doConnection - make a connection
441  */
442 static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx)
443 {
444     BIO *conn;
445     SSL *serverCon;
446     int width, i;
447     fd_set readfds;
448
449     if ((conn = BIO_new(BIO_s_connect())) == NULL)
450         return (NULL);
451
452     BIO_set_conn_hostname(conn, host);
453
454     if (scon == NULL)
455         serverCon = SSL_new(ctx);
456     else {
457         serverCon = scon;
458         SSL_set_connect_state(serverCon);
459     }
460
461     SSL_set_bio(serverCon, conn, conn);
462
463     /* ok, lets connect */
464     for (;;) {
465         i = SSL_connect(serverCon);
466         if (BIO_sock_should_retry(i)) {
467             BIO_printf(bio_err, "DELAY\n");
468
469             i = SSL_get_fd(serverCon);
470             width = i + 1;
471             FD_ZERO(&readfds);
472             openssl_fdset(i, &readfds);
473             /*
474              * Note: under VMS with SOCKETSHR the 2nd parameter is currently
475              * of type (int *) whereas under other systems it is (void *) if
476              * you don't have a cast it will choke the compiler: if you do
477              * have a cast then you can either go for (int *) or (void *).
478              */
479             select(width, (void *)&readfds, NULL, NULL, NULL);
480             continue;
481         }
482         break;
483     }
484     if (i <= 0) {
485         BIO_printf(bio_err, "ERROR\n");
486         if (verify_error != X509_V_OK)
487             BIO_printf(bio_err, "verify error:%s\n",
488                        X509_verify_cert_error_string(verify_error));
489         else
490             ERR_print_errors(bio_err);
491         if (scon == NULL)
492             SSL_free(serverCon);
493         return NULL;
494     }
495
496     return serverCon;
497 }