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