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