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