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