Make sure that apps/openssl prefixes its output with '# ' during tests
[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 #include "apps.h"
21 #include "progs.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 /*
53  * Define a HTTP get command globally.
54  * Also define the size of the command, this is two bytes less than
55  * the size of the string because the %s is replaced by the URL.
56  */
57 static const char fmt_http_get_cmd[] = "GET %s HTTP/1.0\r\n\r\n";
58 static const size_t fmt_http_get_cmd_size = sizeof(fmt_http_get_cmd) - 2;
59
60 typedef enum OPTION_choice {
61     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
62     OPT_CONNECT, OPT_CIPHER, OPT_CERT, OPT_NAMEOPT, OPT_KEY, OPT_CAPATH,
63     OPT_CAFILE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_NEW, OPT_REUSE, OPT_BUGS,
64     OPT_VERIFY, OPT_TIME, OPT_SSL3,
65     OPT_WWW
66 } OPTION_CHOICE;
67
68 const OPTIONS s_time_options[] = {
69     {"help", OPT_HELP, '-', "Display this summary"},
70     {"connect", OPT_CONNECT, 's',
71      "Where to connect as post:port (default is " SSL_CONNECT_NAME ")"},
72     {"cipher", OPT_CIPHER, 's', "Cipher to use, see 'openssl ciphers'"},
73     {"cert", OPT_CERT, '<', "Cert file to use, PEM format assumed"},
74     {"nameopt", OPT_NAMEOPT, 's', "Various certificate name options"},
75     {"key", OPT_KEY, '<', "File with key, PEM; default is -cert file"},
76     {"CApath", OPT_CAPATH, '/', "PEM format directory of CA's"},
77     {"cafile", OPT_CAFILE, '<', "PEM format file of CA's"},
78     {"no-CAfile", OPT_NOCAFILE, '-',
79      "Do not load the default certificates file"},
80     {"no-CApath", OPT_NOCAPATH, '-',
81      "Do not load certificates from the default certificates directory"},
82     {"new", OPT_NEW, '-', "Just time new connections"},
83     {"reuse", OPT_REUSE, '-', "Just time connection reuse"},
84     {"bugs", OPT_BUGS, '-', "Turn on SSL bug compatibility"},
85     {"verify", OPT_VERIFY, 'p',
86      "Turn on peer certificate verification, set depth"},
87     {"time", OPT_TIME, 'p', "Seconds to collect data, default " SECONDSSTR},
88     {"www", OPT_WWW, 's', "Fetch specified page from the site"},
89 #ifndef OPENSSL_NO_SSL3
90     {"ssl3", OPT_SSL3, '-', "Just use SSLv3"},
91 #endif
92     {NULL}
93 };
94
95 #define START   0
96 #define STOP    1
97
98 static double tm_Time_F(int s)
99 {
100     return app_tminterval(s, 1);
101 }
102
103 int s_time_main(int argc, char **argv)
104 {
105     char buf[1024 * 8];
106     SSL *scon = NULL;
107     SSL_CTX *ctx = NULL;
108     const SSL_METHOD *meth = NULL;
109     char *CApath = NULL, *CAfile = NULL, *cipher = NULL, *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 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_BUGS:
174             st_bugs = 1;
175             break;
176         case OPT_TIME:
177             if (!opt_int(opt_arg(), &maxtime))
178                 goto opthelp;
179             break;
180         case OPT_WWW:
181             www_path = opt_arg();
182             buf_size = strlen(www_path) + fmt_http_get_cmd_size;
183             if (buf_size > sizeof(buf)) {
184                 BIO_printf(bio_err, "%s: -www option is too long\n", prog);
185                 goto end;
186             }
187             break;
188         case OPT_SSL3:
189             max_version = SSL3_VERSION;
190             break;
191         }
192     }
193     argc = opt_num_rest();
194     if (argc != 0)
195         goto opthelp;
196
197     if (cipher == NULL)
198         cipher = getenv("SSL_CIPHER");
199     if (cipher == NULL) {
200         BIO_printf(bio_err, "No CIPHER specified\n");
201         goto end;
202     }
203
204     if ((ctx = SSL_CTX_new(meth)) == NULL)
205         goto end;
206
207     SSL_CTX_set_quiet_shutdown(ctx, 1);
208     if (SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
209         goto end;
210
211     if (st_bugs)
212         SSL_CTX_set_options(ctx, SSL_OP_ALL);
213     if (!SSL_CTX_set_cipher_list(ctx, cipher))
214         goto end;
215     if (!set_cert_stuff(ctx, certfile, keyfile))
216         goto end;
217
218     if (!ctx_set_verify_locations(ctx, CAfile, CApath, noCAfile, noCApath)) {
219         ERR_print_errors(bio_err);
220         goto end;
221     }
222     if (!(perform & 1))
223         goto next;
224     printf("Collecting connection statistics for %d seconds\n", maxtime);
225
226     /* Loop and time how long it takes to make connections */
227
228     bytes_read = 0;
229     finishtime = (long)time(NULL) + maxtime;
230     tm_Time_F(START);
231     for (;;) {
232         if (finishtime < (long)time(NULL))
233             break;
234
235         if ((scon = doConnection(NULL, host, ctx)) == NULL)
236             goto end;
237
238         if (www_path != NULL) {
239             buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd,
240                                    www_path);
241             if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
242                 goto end;
243             while ((i = SSL_read(scon, buf, sizeof(buf))) > 0 ||
244                         SSL_get_error(scon, i) == SSL_ERROR_WANT_READ ||
245                         SSL_get_error(scon, i) == SSL_ERROR_WANT_WRITE)
246                 if (i > 0) bytes_read += i;
247         }
248 #ifdef NO_SHUTDOWN
249         SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
250 #else
251         SSL_shutdown(scon);
252 #endif
253         BIO_closesocket(SSL_get_fd(scon));
254
255         nConn += 1;
256         if (SSL_session_reused(scon)) {
257             ver = 'r';
258         } else {
259             ver = SSL_version(scon);
260             if (ver == TLS1_VERSION)
261                 ver = 't';
262             else if (ver == SSL3_VERSION)
263                 ver = '3';
264             else
265                 ver = '*';
266         }
267         fputc(ver, stdout);
268         fflush(stdout);
269
270         SSL_free(scon);
271         scon = NULL;
272     }
273     totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
274
275     i = (int)((long)time(NULL) - finishtime + maxtime);
276     printf
277         ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
278          nConn, totalTime, ((double)nConn / totalTime), bytes_read);
279     printf
280         ("%d connections in %ld real seconds, %ld bytes read per connection\n",
281          nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
282
283     /*
284      * Now loop and time connections using the same session id over and over
285      */
286
287  next:
288     if (!(perform & 2))
289         goto end;
290     printf("\n\nNow timing with session id reuse.\n");
291
292     /* Get an SSL object so we can reuse the session id */
293     if ((scon = doConnection(NULL, host, ctx)) == NULL) {
294         BIO_printf(bio_err, "Unable to get connection\n");
295         goto end;
296     }
297
298     if (www_path != NULL) {
299         buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd, www_path);
300         if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
301             goto end;
302         while ((i = SSL_read(scon, buf, sizeof(buf))) > 0 ||
303                     SSL_get_error(scon, i) == SSL_ERROR_WANT_READ ||
304                     SSL_get_error(scon, i) == SSL_ERROR_WANT_WRITE)
305             continue;
306     }
307 #ifdef NO_SHUTDOWN
308     SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
309 #else
310     SSL_shutdown(scon);
311 #endif
312     BIO_closesocket(SSL_get_fd(scon));
313
314     nConn = 0;
315     totalTime = 0.0;
316
317     finishtime = (long)time(NULL) + maxtime;
318
319     printf("starting\n");
320     bytes_read = 0;
321     tm_Time_F(START);
322
323     for (;;) {
324         if (finishtime < (long)time(NULL))
325             break;
326
327         if ((doConnection(scon, host, ctx)) == NULL)
328             goto end;
329
330         if (www_path != NULL) {
331             buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd,
332                                    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                         SSL_get_error(scon, i) == SSL_ERROR_WANT_READ ||
337                         SSL_get_error(scon, i) == SSL_ERROR_WANT_WRITE)
338                 if (i > 0) bytes_read += i;
339         }
340 #ifdef NO_SHUTDOWN
341         SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
342 #else
343         SSL_shutdown(scon);
344 #endif
345         BIO_closesocket(SSL_get_fd(scon));
346
347         nConn += 1;
348         if (SSL_session_reused(scon)) {
349             ver = 'r';
350         } else {
351             ver = SSL_version(scon);
352             if (ver == TLS1_VERSION)
353                 ver = 't';
354             else if (ver == SSL3_VERSION)
355                 ver = '3';
356             else
357                 ver = '*';
358         }
359         fputc(ver, stdout);
360         fflush(stdout);
361     }
362     totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
363
364     printf
365         ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
366          nConn, totalTime, ((double)nConn / totalTime), bytes_read);
367     printf
368         ("%d connections in %ld real seconds, %ld bytes read per connection\n",
369          nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
370
371     ret = 0;
372
373  end:
374     SSL_free(scon);
375     SSL_CTX_free(ctx);
376     return ret;
377 }
378
379 /*-
380  * doConnection - make a connection
381  */
382 static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx)
383 {
384     BIO *conn;
385     SSL *serverCon;
386     int width, i;
387     fd_set readfds;
388
389     if ((conn = BIO_new(BIO_s_connect())) == NULL)
390         return NULL;
391
392     BIO_set_conn_hostname(conn, host);
393
394     if (scon == NULL)
395         serverCon = SSL_new(ctx);
396     else {
397         serverCon = scon;
398         SSL_set_connect_state(serverCon);
399     }
400
401     SSL_set_bio(serverCon, conn, conn);
402
403     /* ok, lets connect */
404     for (;;) {
405         i = SSL_connect(serverCon);
406         if (BIO_sock_should_retry(i)) {
407             BIO_printf(bio_err, "DELAY\n");
408
409             i = SSL_get_fd(serverCon);
410             width = i + 1;
411             FD_ZERO(&readfds);
412             openssl_fdset(i, &readfds);
413             /*
414              * Note: under VMS with SOCKETSHR the 2nd parameter is currently
415              * of type (int *) whereas under other systems it is (void *) if
416              * you don't have a cast it will choke the compiler: if you do
417              * have a cast then you can either go for (int *) or (void *).
418              */
419             select(width, (void *)&readfds, NULL, NULL, NULL);
420             continue;
421         }
422         break;
423     }
424     if (i <= 0) {
425         BIO_printf(bio_err, "ERROR\n");
426         if (verify_args.error != X509_V_OK)
427             BIO_printf(bio_err, "verify error:%s\n",
428                        X509_verify_cert_error_string(verify_args.error));
429         else
430             ERR_print_errors(bio_err);
431         if (scon == NULL)
432             SSL_free(serverCon);
433         return NULL;
434     }
435
436     return serverCon;
437 }
438 #endif /* OPENSSL_NO_SOCK */