Update PR#3925
[oweals/openssl.git] / apps / s_time.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #define NO_SHUTDOWN
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include <openssl/opensslconf.h>
17
18 #ifndef OPENSSL_NO_SOCK
19
20 #define USE_SOCKETS
21 #include "apps.h"
22 #include <openssl/x509.h>
23 #include <openssl/ssl.h>
24 #include <openssl/pem.h>
25 #include "s_apps.h"
26 #include <openssl/err.h>
27 #if !defined(OPENSSL_SYS_MSDOS)
28 # include OPENSSL_UNISTD
29 #endif
30
31 #undef ioctl
32 #define ioctl ioctlsocket
33
34 #define SSL_CONNECT_NAME        "localhost:4433"
35
36 /* no default cert. */
37 /*
38  * #define TEST_CERT "client.pem"
39  */
40
41 #undef min
42 #undef max
43 #define min(a,b) (((a) < (b)) ? (a) : (b))
44 #define max(a,b) (((a) > (b)) ? (a) : (b))
45
46 #undef SECONDS
47 #define SECONDS 30
48 #define SECONDSSTR "30"
49
50 static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx);
51
52 static const char fmt_http_get_cmd[] = "GET %s HTTP/1.0\r\n\r\n";
53
54 typedef enum OPTION_choice {
55     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
56     OPT_CONNECT, OPT_CIPHER, OPT_CERT, OPT_KEY, OPT_CAPATH,
57     OPT_CAFILE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_NEW, OPT_REUSE, OPT_BUGS,
58     OPT_VERIFY, OPT_TIME, OPT_SSL3,
59     OPT_WWW
60 } OPTION_CHOICE;
61
62 OPTIONS s_time_options[] = {
63     {"help", OPT_HELP, '-', "Display this summary"},
64     {"connect", OPT_CONNECT, 's',
65      "Where to connect as post:port (default is " SSL_CONNECT_NAME ")"},
66     {"cipher", OPT_CIPHER, 's', "Cipher to use, see 'openssl ciphers'"},
67     {"cert", OPT_CERT, '<', "Cert file to use, PEM format assumed"},
68     {"key", OPT_KEY, '<', "File with key, PEM; default is -cert file"},
69     {"CApath", OPT_CAPATH, '/', "PEM format directory of CA's"},
70     {"cafile", OPT_CAFILE, '<', "PEM format file of CA's"},
71     {"no-CAfile", OPT_NOCAFILE, '-',
72      "Do not load the default certificates file"},
73     {"no-CApath", OPT_NOCAPATH, '-',
74      "Do not load certificates from the default certificates directory"},
75     {"new", OPT_NEW, '-', "Just time new connections"},
76     {"reuse", OPT_REUSE, '-', "Just time connection reuse"},
77     {"bugs", OPT_BUGS, '-', "Turn on SSL bug compatibility"},
78     {"verify", OPT_VERIFY, 'p',
79      "Turn on peer certificate verification, set depth"},
80     {"time", OPT_TIME, 'p', "Seconds to collect data, default " SECONDSSTR},
81     {"www", OPT_WWW, 's', "Fetch specified page from the site"},
82 #ifndef OPENSSL_NO_SSL3
83     {"ssl3", OPT_SSL3, '-', "Just use SSLv3"},
84 #endif
85     {NULL}
86 };
87
88 #define START   0
89 #define STOP    1
90
91 static double tm_Time_F(int s)
92 {
93     return app_tminterval(s, 1);
94 }
95
96 int s_time_main(int argc, char **argv)
97 {
98     char buf[1024 * 8];
99     SSL *scon = NULL;
100     SSL_CTX *ctx = NULL;
101     const SSL_METHOD *meth = NULL;
102     char *CApath = NULL, *CAfile = NULL, *cipher = NULL, *www_path = NULL;
103     char *host = SSL_CONNECT_NAME, *certfile = NULL, *keyfile = NULL, *prog;
104     double totalTime = 0.0;
105     int noCApath = 0, noCAfile = 0;
106     int maxtime = SECONDS, nConn = 0, perform = 3, ret = 1, i, st_bugs = 0;
107     long bytes_read = 0, finishtime = 0;
108     OPTION_CHOICE o;
109     int max_version = 0, ver, buf_len;
110     size_t buf_size;
111
112     meth = TLS_client_method();
113
114     prog = opt_init(argc, argv, s_time_options);
115     while ((o = opt_next()) != OPT_EOF) {
116         switch (o) {
117         case OPT_EOF:
118         case OPT_ERR:
119  opthelp:
120             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
121             goto end;
122         case OPT_HELP:
123             opt_help(s_time_options);
124             ret = 0;
125             goto end;
126         case OPT_CONNECT:
127             host = opt_arg();
128             break;
129         case OPT_REUSE:
130             perform = 2;
131             break;
132         case OPT_NEW:
133             perform = 1;
134             break;
135         case OPT_VERIFY:
136             if (!opt_int(opt_arg(), &verify_args.depth))
137                 goto opthelp;
138             BIO_printf(bio_err, "%s: verify depth is %d\n",
139                        prog, verify_args.depth);
140             break;
141         case OPT_CERT:
142             certfile = opt_arg();
143             break;
144         case OPT_KEY:
145             keyfile = opt_arg();
146             break;
147         case OPT_CAPATH:
148             CApath = opt_arg();
149             break;
150         case OPT_CAFILE:
151             CAfile = opt_arg();
152             break;
153         case OPT_NOCAPATH:
154             noCApath = 1;
155             break;
156         case OPT_NOCAFILE:
157             noCAfile = 1;
158             break;
159         case OPT_CIPHER:
160             cipher = opt_arg();
161             break;
162         case OPT_BUGS:
163             st_bugs = 1;
164             break;
165         case OPT_TIME:
166             if (!opt_int(opt_arg(), &maxtime))
167                 goto opthelp;
168             break;
169         case OPT_WWW:
170             www_path = opt_arg();
171             buf_size = strlen(www_path) + sizeof(fmt_http_get_cmd) - 2;  /* 2 is for %s */
172             if (buf_size > sizeof(buf)) {
173                 BIO_printf(bio_err, "%s: -www option is too long\n", prog);
174                 goto end;
175             }
176             break;
177         case OPT_SSL3:
178             max_version = SSL3_VERSION;
179             break;
180         }
181     }
182     argc = opt_num_rest();
183     if (argc != 0)
184         goto opthelp;
185
186     if (cipher == NULL)
187         cipher = getenv("SSL_CIPHER");
188     if (cipher == NULL) {
189         BIO_printf(bio_err, "No CIPHER specified\n");
190         goto end;
191     }
192
193     if ((ctx = SSL_CTX_new(meth)) == NULL)
194         goto end;
195
196     SSL_CTX_set_quiet_shutdown(ctx, 1);
197     if (SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
198         goto end;
199
200     if (st_bugs)
201         SSL_CTX_set_options(ctx, SSL_OP_ALL);
202     if (!SSL_CTX_set_cipher_list(ctx, cipher))
203         goto end;
204     if (!set_cert_stuff(ctx, certfile, keyfile))
205         goto end;
206
207     if (!ctx_set_verify_locations(ctx, CAfile, CApath, noCAfile, noCApath)) {
208         ERR_print_errors(bio_err);
209         goto end;
210     }
211     if (!(perform & 1))
212         goto next;
213     printf("Collecting connection statistics for %d seconds\n", maxtime);
214
215     /* Loop and time how long it takes to make connections */
216
217     bytes_read = 0;
218     finishtime = (long)time(NULL) + maxtime;
219     tm_Time_F(START);
220     for (;;) {
221         if (finishtime < (long)time(NULL))
222             break;
223
224         if ((scon = doConnection(NULL, host, ctx)) == NULL)
225             goto end;
226
227         if (www_path != NULL) {
228             buf_len = BIO_snprintf(buf, sizeof buf,
229                                    fmt_http_get_cmd, www_path);
230             if (SSL_write(scon, buf, buf_len) <= 0)
231                 goto end;
232             while ((i = SSL_read(scon, buf, sizeof(buf))) > 0 ||
233                         SSL_get_error(scon, i) == SSL_ERROR_WANT_READ ||
234                         SSL_get_error(scon, i) == SSL_ERROR_WANT_WRITE)
235                 if (i > 0) bytes_read += i;
236         }
237 #ifdef NO_SHUTDOWN
238         SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
239 #else
240         SSL_shutdown(scon);
241 #endif
242         BIO_closesocket(SSL_get_fd(scon));
243
244         nConn += 1;
245         if (SSL_session_reused(scon))
246             ver = 'r';
247         else {
248             ver = SSL_version(scon);
249             if (ver == TLS1_VERSION)
250                 ver = 't';
251             else if (ver == SSL3_VERSION)
252                 ver = '3';
253             else
254                 ver = '*';
255         }
256         fputc(ver, stdout);
257         fflush(stdout);
258
259         SSL_free(scon);
260         scon = NULL;
261     }
262     totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
263
264     i = (int)((long)time(NULL) - finishtime + maxtime);
265     printf
266         ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
267          nConn, totalTime, ((double)nConn / totalTime), bytes_read);
268     printf
269         ("%d connections in %ld real seconds, %ld bytes read per connection\n",
270          nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
271
272     /*
273      * Now loop and time connections using the same session id over and over
274      */
275
276  next:
277     if (!(perform & 2))
278         goto end;
279     printf("\n\nNow timing with session id reuse.\n");
280
281     /* Get an SSL object so we can reuse the session id */
282     if ((scon = doConnection(NULL, host, ctx)) == NULL) {
283         BIO_printf(bio_err, "Unable to get connection\n");
284         goto end;
285     }
286
287     if (www_path != NULL) {
288         buf_len = BIO_snprintf(buf, sizeof buf,
289                                fmt_http_get_cmd, www_path);
290         if (SSL_write(scon, buf, buf_len) <= 0)
291             goto end;
292         while ((i = SSL_read(scon, buf, sizeof(buf))) > 0 ||
293                     SSL_get_error(scon, i) == SSL_ERROR_WANT_READ ||
294                     SSL_get_error(scon, i) == SSL_ERROR_WANT_WRITE)
295             continue;
296     }
297 #ifdef NO_SHUTDOWN
298     SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
299 #else
300     SSL_shutdown(scon);
301 #endif
302     BIO_closesocket(SSL_get_fd(scon));
303
304     nConn = 0;
305     totalTime = 0.0;
306
307     finishtime = (long)time(NULL) + maxtime;
308
309     printf("starting\n");
310     bytes_read = 0;
311     tm_Time_F(START);
312
313     for (;;) {
314         if (finishtime < (long)time(NULL))
315             break;
316
317         if ((doConnection(scon, host, ctx)) == NULL)
318             goto end;
319
320         if (www_path) {
321             BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n",
322                          www_path);
323             if (SSL_write(scon, buf, strlen(buf)) <= 0)
324                 goto end;
325             while ((i = SSL_read(scon, buf, sizeof(buf))) > 0 ||
326                         SSL_get_error(scon, i) == SSL_ERROR_WANT_READ ||
327                         SSL_get_error(scon, i) == SSL_ERROR_WANT_WRITE)
328                 if (i > 0) bytes_read += i;
329         }
330 #ifdef NO_SHUTDOWN
331         SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
332 #else
333         SSL_shutdown(scon);
334 #endif
335         BIO_closesocket(SSL_get_fd(scon));
336
337         nConn += 1;
338         if (SSL_session_reused(scon))
339             ver = 'r';
340         else {
341             ver = SSL_version(scon);
342             if (ver == TLS1_VERSION)
343                 ver = 't';
344             else if (ver == SSL3_VERSION)
345                 ver = '3';
346             else
347                 ver = '*';
348         }
349         fputc(ver, stdout);
350         fflush(stdout);
351     }
352     totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
353
354     printf
355         ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
356          nConn, totalTime, ((double)nConn / totalTime), bytes_read);
357     printf
358         ("%d connections in %ld real seconds, %ld bytes read per connection\n",
359          nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
360
361     ret = 0;
362
363  end:
364     SSL_free(scon);
365     SSL_CTX_free(ctx);
366     return (ret);
367 }
368
369 /*-
370  * doConnection - make a connection
371  */
372 static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx)
373 {
374     BIO *conn;
375     SSL *serverCon;
376     int width, i;
377     fd_set readfds;
378
379     if ((conn = BIO_new(BIO_s_connect())) == NULL)
380         return (NULL);
381
382     BIO_set_conn_hostname(conn, host);
383
384     if (scon == NULL)
385         serverCon = SSL_new(ctx);
386     else {
387         serverCon = scon;
388         SSL_set_connect_state(serverCon);
389     }
390
391     SSL_set_bio(serverCon, conn, conn);
392
393     /* ok, lets connect */
394     for (;;) {
395         i = SSL_connect(serverCon);
396         if (BIO_sock_should_retry(i)) {
397             BIO_printf(bio_err, "DELAY\n");
398
399             i = SSL_get_fd(serverCon);
400             width = i + 1;
401             FD_ZERO(&readfds);
402             openssl_fdset(i, &readfds);
403             /*
404              * Note: under VMS with SOCKETSHR the 2nd parameter is currently
405              * of type (int *) whereas under other systems it is (void *) if
406              * you don't have a cast it will choke the compiler: if you do
407              * have a cast then you can either go for (int *) or (void *).
408              */
409             select(width, (void *)&readfds, NULL, NULL, NULL);
410             continue;
411         }
412         break;
413     }
414     if (i <= 0) {
415         BIO_printf(bio_err, "ERROR\n");
416         if (verify_args.error != X509_V_OK)
417             BIO_printf(bio_err, "verify error:%s\n",
418                        X509_verify_cert_error_string(verify_args.error));
419         else
420             ERR_print_errors(bio_err);
421         if (scon == NULL)
422             SSL_free(serverCon);
423         return NULL;
424     }
425
426     return serverCon;
427 }
428 #endif /* OPENSSL_NO_SOCK */