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