config: fix HP-UX PA-RISC detection.
[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 #if !defined(OPENSSL_SYS_MSDOS)
77 # include OPENSSL_UNISTD
78 #endif
79
80 #undef ioctl
81 #define ioctl ioctlsocket
82
83 #define SSL_CONNECT_NAME        "localhost:4433"
84
85 /* no default cert. */
86 /*
87  * #define TEST_CERT "client.pem"
88  */
89
90 #undef BUFSIZZ
91 #define BUFSIZZ 1024*10
92
93 #define MYBUFSIZ 1024*8
94
95 #undef min
96 #undef max
97 #define min(a,b) (((a) < (b)) ? (a) : (b))
98 #define max(a,b) (((a) > (b)) ? (a) : (b))
99
100 #undef SECONDS
101 #define SECONDS 30
102 #define SECONDSSTR "30"
103
104 extern int verify_depth;
105 extern int verify_error;
106
107 static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx);
108
109 typedef enum OPTION_choice {
110     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
111     OPT_CONNECT, OPT_CIPHER, OPT_CERT, OPT_KEY, OPT_CAPATH,
112     OPT_CAFILE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_NEW, OPT_REUSE, OPT_BUGS,
113     OPT_VERIFY, OPT_TIME, OPT_SSL3,
114     OPT_WWW
115 } OPTION_CHOICE;
116
117 OPTIONS s_time_options[] = {
118     {"help", OPT_HELP, '-', "Display this summary"},
119     {"connect", OPT_CONNECT, 's',
120      "Where to connect as post:port (default is " SSL_CONNECT_NAME ")"},
121     {"cipher", OPT_CIPHER, 's', "Cipher to use, see 'openssl ciphers'"},
122     {"cert", OPT_CERT, '<', "Cert file to use, PEM format assumed"},
123     {"key", OPT_KEY, '<', "File with key, PEM; default is -cert file"},
124     {"CApath", OPT_CAPATH, '/', "PEM format directory of CA's"},
125     {"cafile", OPT_CAFILE, '<', "PEM format file of CA's"},
126     {"no-CAfile", OPT_NOCAFILE, '-',
127      "Do not load the default certificates file"},
128     {"no-CApath", OPT_NOCAPATH, '-',
129      "Do not load certificates from the default certificates directory"},
130     {"new", OPT_NEW, '-', "Just time new connections"},
131     {"reuse", OPT_REUSE, '-', "Just time connection reuse"},
132     {"bugs", OPT_BUGS, '-', "Turn on SSL bug compatibility"},
133     {"verify", OPT_VERIFY, 'p',
134      "Turn on peer certificate verification, set depth"},
135     {"time", OPT_TIME, 'p', "Seconds to collect data, default " SECONDSSTR},
136     {"www", OPT_WWW, 's', "Fetch specified page from the site"},
137 #ifndef OPENSSL_NO_SSL3
138     {"ssl3", OPT_SSL3, '-', "Just use SSLv3"},
139 #endif
140     {NULL}
141 };
142
143 #define START   0
144 #define STOP    1
145
146 static double tm_Time_F(int s)
147 {
148     return app_tminterval(s, 1);
149 }
150
151 int s_time_main(int argc, char **argv)
152 {
153     char buf[1024 * 8];
154     SSL *scon = NULL;
155     SSL_CTX *ctx = NULL;
156     const SSL_METHOD *meth = NULL;
157     char *CApath = NULL, *CAfile = NULL, *cipher = NULL, *www_path = NULL;
158     char *host = SSL_CONNECT_NAME, *certfile = NULL, *keyfile = NULL, *prog;
159     double totalTime = 0.0;
160     int noCApath = 0, noCAfile = 0;
161     int maxtime = SECONDS, nConn = 0, perform = 3, ret = 1, i, st_bugs =
162         0, ver;
163     long bytes_read = 0, finishtime = 0;
164     OPTION_CHOICE o;
165     int max_version = 0;
166
167     meth = TLS_client_method();
168     verify_depth = 0;
169     verify_error = X509_V_OK;
170
171     prog = opt_init(argc, argv, s_time_options);
172     while ((o = opt_next()) != OPT_EOF) {
173         switch (o) {
174         case OPT_EOF:
175         case OPT_ERR:
176  opthelp:
177             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
178             goto end;
179         case OPT_HELP:
180             opt_help(s_time_options);
181             ret = 0;
182             goto end;
183         case OPT_CONNECT:
184             host = opt_arg();
185             break;
186         case OPT_REUSE:
187             perform = 2;
188             break;
189         case OPT_NEW:
190             perform = 1;
191             break;
192         case OPT_VERIFY:
193             if (!opt_int(opt_arg(), &verify_depth))
194                 goto opthelp;
195             BIO_printf(bio_err, "%s: verify depth is %d\n",
196                        prog, verify_depth);
197             break;
198         case OPT_CERT:
199             certfile = opt_arg();
200             break;
201         case OPT_KEY:
202             keyfile = opt_arg();
203             break;
204         case OPT_CAPATH:
205             CApath = opt_arg();
206             break;
207         case OPT_CAFILE:
208             CAfile = opt_arg();
209             break;
210         case OPT_NOCAPATH:
211             noCApath = 1;
212             break;
213         case OPT_NOCAFILE:
214             noCAfile = 1;
215             break;
216         case OPT_CIPHER:
217             cipher = opt_arg();
218             break;
219         case OPT_BUGS:
220             st_bugs = 1;
221             break;
222         case OPT_TIME:
223             if (!opt_int(opt_arg(), &maxtime))
224                 goto opthelp;
225             break;
226         case OPT_WWW:
227             www_path = opt_arg();
228             if (strlen(www_path) > MYBUFSIZ - 100) {
229                 BIO_printf(bio_err, "%s: -www option too long\n", prog);
230                 goto end;
231             }
232             break;
233         case OPT_SSL3:
234             max_version = SSL3_VERSION;
235             break;
236         }
237     }
238     argc = opt_num_rest();
239     if (argc != 0)
240         goto opthelp;
241
242     if (cipher == NULL)
243         cipher = getenv("SSL_CIPHER");
244     if (cipher == NULL) {
245         BIO_printf(bio_err, "No CIPHER specified\n");
246         goto end;
247     }
248
249     if ((ctx = SSL_CTX_new(meth)) == NULL)
250         goto end;
251
252     SSL_CTX_set_quiet_shutdown(ctx, 1);
253     if (SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
254         goto end;
255
256     if (st_bugs)
257         SSL_CTX_set_options(ctx, SSL_OP_ALL);
258     if (!SSL_CTX_set_cipher_list(ctx, cipher))
259         goto end;
260     if (!set_cert_stuff(ctx, certfile, keyfile))
261         goto end;
262
263     if (!ctx_set_verify_locations(ctx, CAfile, CApath, noCAfile, noCApath)) {
264         ERR_print_errors(bio_err);
265         goto end;
266     }
267     if (!(perform & 1))
268         goto next;
269     printf("Collecting connection statistics for %d seconds\n", maxtime);
270
271     /* Loop and time how long it takes to make connections */
272
273     bytes_read = 0;
274     finishtime = (long)time(NULL) + maxtime;
275     tm_Time_F(START);
276     for (;;) {
277         if (finishtime < (long)time(NULL))
278             break;
279
280         if ((scon = doConnection(NULL, host, ctx)) == NULL)
281             goto end;
282
283         if (www_path != NULL) {
284             BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n",
285                          www_path);
286             if (SSL_write(scon, buf, strlen(buf)) <= 0)
287                 goto end;
288             while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
289                 bytes_read += i;
290         }
291 #ifdef NO_SHUTDOWN
292         SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
293 #else
294         SSL_shutdown(scon);
295 #endif
296         BIO_closesocket(SSL_get_fd(scon));
297
298         nConn += 1;
299         if (SSL_session_reused(scon))
300             ver = 'r';
301         else {
302             ver = SSL_version(scon);
303             if (ver == TLS1_VERSION)
304                 ver = 't';
305             else if (ver == SSL3_VERSION)
306                 ver = '3';
307             else
308                 ver = '*';
309         }
310         fputc(ver, stdout);
311         fflush(stdout);
312
313         SSL_free(scon);
314         scon = NULL;
315     }
316     totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
317
318     i = (int)((long)time(NULL) - finishtime + maxtime);
319     printf
320         ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
321          nConn, totalTime, ((double)nConn / totalTime), bytes_read);
322     printf
323         ("%d connections in %ld real seconds, %ld bytes read per connection\n",
324          nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
325
326     /*
327      * Now loop and time connections using the same session id over and over
328      */
329
330  next:
331     if (!(perform & 2))
332         goto end;
333     printf("\n\nNow timing with session id reuse.\n");
334
335     /* Get an SSL object so we can reuse the session id */
336     if ((scon = doConnection(NULL, host, ctx)) == NULL) {
337         BIO_printf(bio_err, "Unable to get connection\n");
338         goto end;
339     }
340
341     if (www_path != NULL) {
342         BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n", www_path);
343         if (SSL_write(scon, buf, strlen(buf)) <= 0)
344             goto end;
345         while (SSL_read(scon, buf, sizeof(buf)) > 0)
346             continue;
347     }
348 #ifdef NO_SHUTDOWN
349     SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
350 #else
351     SSL_shutdown(scon);
352 #endif
353     BIO_closesocket(SSL_get_fd(scon));
354
355     nConn = 0;
356     totalTime = 0.0;
357
358     finishtime = (long)time(NULL) + maxtime;
359
360     printf("starting\n");
361     bytes_read = 0;
362     tm_Time_F(START);
363
364     for (;;) {
365         if (finishtime < (long)time(NULL))
366             break;
367
368         if ((doConnection(scon, host, ctx)) == NULL)
369             goto end;
370
371         if (www_path) {
372             BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n",
373                          www_path);
374             if (SSL_write(scon, buf, strlen(buf)) <= 0)
375                 goto end;
376             while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
377                 bytes_read += i;
378         }
379 #ifdef NO_SHUTDOWN
380         SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
381 #else
382         SSL_shutdown(scon);
383 #endif
384         BIO_closesocket(SSL_get_fd(scon));
385
386         nConn += 1;
387         if (SSL_session_reused(scon))
388             ver = 'r';
389         else {
390             ver = SSL_version(scon);
391             if (ver == TLS1_VERSION)
392                 ver = 't';
393             else if (ver == SSL3_VERSION)
394                 ver = '3';
395             else
396                 ver = '*';
397         }
398         fputc(ver, stdout);
399         fflush(stdout);
400     }
401     totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
402
403     printf
404         ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
405          nConn, totalTime, ((double)nConn / totalTime), bytes_read);
406     printf
407         ("%d connections in %ld real seconds, %ld bytes read per connection\n",
408          nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
409
410     ret = 0;
411
412  end:
413     SSL_free(scon);
414     SSL_CTX_free(ctx);
415     return (ret);
416 }
417
418 /*-
419  * doConnection - make a connection
420  */
421 static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx)
422 {
423     BIO *conn;
424     SSL *serverCon;
425     int width, i;
426     fd_set readfds;
427
428     if ((conn = BIO_new(BIO_s_connect())) == NULL)
429         return (NULL);
430
431     BIO_set_conn_hostname(conn, host);
432
433     if (scon == NULL)
434         serverCon = SSL_new(ctx);
435     else {
436         serverCon = scon;
437         SSL_set_connect_state(serverCon);
438     }
439
440     SSL_set_bio(serverCon, conn, conn);
441
442     /* ok, lets connect */
443     for (;;) {
444         i = SSL_connect(serverCon);
445         if (BIO_sock_should_retry(i)) {
446             BIO_printf(bio_err, "DELAY\n");
447
448             i = SSL_get_fd(serverCon);
449             width = i + 1;
450             FD_ZERO(&readfds);
451             openssl_fdset(i, &readfds);
452             /*
453              * Note: under VMS with SOCKETSHR the 2nd parameter is currently
454              * of type (int *) whereas under other systems it is (void *) if
455              * you don't have a cast it will choke the compiler: if you do
456              * have a cast then you can either go for (int *) or (void *).
457              */
458             select(width, (void *)&readfds, NULL, NULL, NULL);
459             continue;
460         }
461         break;
462     }
463     if (i <= 0) {
464         BIO_printf(bio_err, "ERROR\n");
465         if (verify_error != X509_V_OK)
466             BIO_printf(bio_err, "verify error:%s\n",
467                        X509_verify_cert_error_string(verify_error));
468         else
469             ERR_print_errors(bio_err);
470         if (scon == NULL)
471             SSL_free(serverCon);
472         return NULL;
473     }
474
475     return serverCon;
476 }