Make the RSA structure opaque
[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 #include <openssl/opensslconf.h>
70
71 #ifndef OPENSSL_NO_SOCK
72
73 #define USE_SOCKETS
74 #include "apps.h"
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 #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', "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     int max_version = 0;
170
171     meth = TLS_client_method();
172     verify_depth = 0;
173     verify_error = X509_V_OK;
174
175     prog = opt_init(argc, argv, s_time_options);
176     while ((o = opt_next()) != OPT_EOF) {
177         switch (o) {
178         case OPT_EOF:
179         case OPT_ERR:
180  opthelp:
181             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
182             goto end;
183         case OPT_HELP:
184             opt_help(s_time_options);
185             ret = 0;
186             goto end;
187         case OPT_CONNECT:
188             host = opt_arg();
189             break;
190         case OPT_REUSE:
191             perform = 2;
192             break;
193         case OPT_NEW:
194             perform = 1;
195             break;
196         case OPT_VERIFY:
197             if (!opt_int(opt_arg(), &verify_depth))
198                 goto opthelp;
199             BIO_printf(bio_err, "%s: verify depth is %d\n",
200                        prog, verify_depth);
201             break;
202         case OPT_CERT:
203             certfile = opt_arg();
204             break;
205         case OPT_KEY:
206             keyfile = opt_arg();
207             break;
208         case OPT_CAPATH:
209             CApath = opt_arg();
210             break;
211         case OPT_CAFILE:
212             CAfile = opt_arg();
213             break;
214         case OPT_NOCAPATH:
215             noCApath = 1;
216             break;
217         case OPT_NOCAFILE:
218             noCAfile = 1;
219             break;
220         case OPT_CIPHER:
221             cipher = opt_arg();
222             break;
223         case OPT_BUGS:
224             st_bugs = 1;
225             break;
226         case OPT_TIME:
227             if (!opt_int(opt_arg(), &maxtime))
228                 goto opthelp;
229             break;
230         case OPT_WWW:
231             www_path = opt_arg();
232             if (strlen(www_path) > MYBUFSIZ - 100) {
233                 BIO_printf(bio_err, "%s: -www option too long\n", prog);
234                 goto end;
235             }
236             break;
237         case OPT_SSL3:
238             max_version = SSL3_VERSION;
239             break;
240         }
241     }
242     argc = opt_num_rest();
243     if (argc != 0)
244         goto opthelp;
245
246     if (cipher == NULL)
247         cipher = getenv("SSL_CIPHER");
248     if (cipher == NULL) {
249         BIO_printf(bio_err, "No CIPHER specified\n");
250         goto end;
251     }
252
253     if ((ctx = SSL_CTX_new(meth)) == NULL)
254         goto end;
255
256     SSL_CTX_set_quiet_shutdown(ctx, 1);
257     if (SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
258         goto end;
259
260     if (st_bugs)
261         SSL_CTX_set_options(ctx, SSL_OP_ALL);
262     if (!SSL_CTX_set_cipher_list(ctx, cipher))
263         goto end;
264     if (!set_cert_stuff(ctx, certfile, keyfile))
265         goto end;
266
267     if (!ctx_set_verify_locations(ctx, CAfile, CApath, noCAfile, noCApath)) {
268         ERR_print_errors(bio_err);
269         goto end;
270     }
271     if (!(perform & 1))
272         goto next;
273     printf("Collecting connection statistics for %d seconds\n", maxtime);
274
275     /* Loop and time how long it takes to make connections */
276
277     bytes_read = 0;
278     finishtime = (long)time(NULL) + maxtime;
279     tm_Time_F(START);
280     for (;;) {
281         if (finishtime < (long)time(NULL))
282             break;
283
284         if ((scon = doConnection(NULL, host, ctx)) == NULL)
285             goto end;
286
287         if (www_path != NULL) {
288             BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n",
289                          www_path);
290             if (SSL_write(scon, buf, strlen(buf)) <= 0)
291                 goto end;
292             while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
293                 bytes_read += i;
294         }
295 #ifdef NO_SHUTDOWN
296         SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
297 #else
298         SSL_shutdown(scon);
299 #endif
300         BIO_closesocket(SSL_get_fd(scon));
301
302         nConn += 1;
303         if (SSL_session_reused(scon))
304             ver = 'r';
305         else {
306             ver = SSL_version(scon);
307             if (ver == TLS1_VERSION)
308                 ver = 't';
309             else if (ver == SSL3_VERSION)
310                 ver = '3';
311             else
312                 ver = '*';
313         }
314         fputc(ver, stdout);
315         fflush(stdout);
316
317         SSL_free(scon);
318         scon = NULL;
319     }
320     totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
321
322     i = (int)((long)time(NULL) - finishtime + maxtime);
323     printf
324         ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
325          nConn, totalTime, ((double)nConn / totalTime), bytes_read);
326     printf
327         ("%d connections in %ld real seconds, %ld bytes read per connection\n",
328          nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
329
330     /*
331      * Now loop and time connections using the same session id over and over
332      */
333
334  next:
335     if (!(perform & 2))
336         goto end;
337     printf("\n\nNow timing with session id reuse.\n");
338
339     /* Get an SSL object so we can reuse the session id */
340     if ((scon = doConnection(NULL, host, ctx)) == NULL) {
341         BIO_printf(bio_err, "Unable to get connection\n");
342         goto end;
343     }
344
345     if (www_path != NULL) {
346         BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n", www_path);
347         if (SSL_write(scon, buf, strlen(buf)) <= 0)
348             goto end;
349         while (SSL_read(scon, buf, sizeof(buf)) > 0)
350             continue;
351     }
352 #ifdef NO_SHUTDOWN
353     SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
354 #else
355     SSL_shutdown(scon);
356 #endif
357     BIO_closesocket(SSL_get_fd(scon));
358
359     nConn = 0;
360     totalTime = 0.0;
361
362     finishtime = (long)time(NULL) + maxtime;
363
364     printf("starting\n");
365     bytes_read = 0;
366     tm_Time_F(START);
367
368     for (;;) {
369         if (finishtime < (long)time(NULL))
370             break;
371
372         if ((doConnection(scon, host, ctx)) == NULL)
373             goto end;
374
375         if (www_path) {
376             BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n",
377                          www_path);
378             if (SSL_write(scon, buf, strlen(buf)) <= 0)
379                 goto end;
380             while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
381                 bytes_read += i;
382         }
383 #ifdef NO_SHUTDOWN
384         SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
385 #else
386         SSL_shutdown(scon);
387 #endif
388         BIO_closesocket(SSL_get_fd(scon));
389
390         nConn += 1;
391         if (SSL_session_reused(scon))
392             ver = 'r';
393         else {
394             ver = SSL_version(scon);
395             if (ver == TLS1_VERSION)
396                 ver = 't';
397             else if (ver == SSL3_VERSION)
398                 ver = '3';
399             else
400                 ver = '*';
401         }
402         fputc(ver, stdout);
403         fflush(stdout);
404     }
405     totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
406
407     printf
408         ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
409          nConn, totalTime, ((double)nConn / totalTime), bytes_read);
410     printf
411         ("%d connections in %ld real seconds, %ld bytes read per connection\n",
412          nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
413
414     ret = 0;
415
416  end:
417     SSL_free(scon);
418     SSL_CTX_free(ctx);
419     return (ret);
420 }
421
422 /*-
423  * doConnection - make a connection
424  */
425 static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx)
426 {
427     BIO *conn;
428     SSL *serverCon;
429     int width, i;
430     fd_set readfds;
431
432     if ((conn = BIO_new(BIO_s_connect())) == NULL)
433         return (NULL);
434
435     BIO_set_conn_hostname(conn, host);
436
437     if (scon == NULL)
438         serverCon = SSL_new(ctx);
439     else {
440         serverCon = scon;
441         SSL_set_connect_state(serverCon);
442     }
443
444     SSL_set_bio(serverCon, conn, conn);
445
446     /* ok, lets connect */
447     for (;;) {
448         i = SSL_connect(serverCon);
449         if (BIO_sock_should_retry(i)) {
450             BIO_printf(bio_err, "DELAY\n");
451
452             i = SSL_get_fd(serverCon);
453             width = i + 1;
454             FD_ZERO(&readfds);
455             openssl_fdset(i, &readfds);
456             /*
457              * Note: under VMS with SOCKETSHR the 2nd parameter is currently
458              * of type (int *) whereas under other systems it is (void *) if
459              * you don't have a cast it will choke the compiler: if you do
460              * have a cast then you can either go for (int *) or (void *).
461              */
462             select(width, (void *)&readfds, NULL, NULL, NULL);
463             continue;
464         }
465         break;
466     }
467     if (i <= 0) {
468         BIO_printf(bio_err, "ERROR\n");
469         if (verify_error != X509_V_OK)
470             BIO_printf(bio_err, "verify error:%s\n",
471                        X509_verify_cert_error_string(verify_error));
472         else
473             ERR_print_errors(bio_err);
474         if (scon == NULL)
475             SSL_free(serverCon);
476         return NULL;
477     }
478
479     return serverCon;
480 }
481 #endif /* OPENSSL_NO_SOCK */