Add X509_STORE lock and unlock functions
[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 extern int verify_depth;
54 extern int verify_error;
55
56 static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx);
57
58 static const char fmt_http_get_cmd[] = "GET %s HTTP/1.0\r\n\r\n";
59
60 typedef enum OPTION_choice {
61     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
62     OPT_CONNECT, OPT_CIPHER, OPT_CERT, 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 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     {"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     verify_depth = 0;
120     verify_error = X509_V_OK;
121
122     prog = opt_init(argc, argv, s_time_options);
123     while ((o = opt_next()) != OPT_EOF) {
124         switch (o) {
125         case OPT_EOF:
126         case OPT_ERR:
127  opthelp:
128             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
129             goto end;
130         case OPT_HELP:
131             opt_help(s_time_options);
132             ret = 0;
133             goto end;
134         case OPT_CONNECT:
135             host = opt_arg();
136             break;
137         case OPT_REUSE:
138             perform = 2;
139             break;
140         case OPT_NEW:
141             perform = 1;
142             break;
143         case OPT_VERIFY:
144             if (!opt_int(opt_arg(), &verify_depth))
145                 goto opthelp;
146             BIO_printf(bio_err, "%s: verify depth is %d\n",
147                        prog, verify_depth);
148             break;
149         case OPT_CERT:
150             certfile = opt_arg();
151             break;
152         case OPT_KEY:
153             keyfile = opt_arg();
154             break;
155         case OPT_CAPATH:
156             CApath = opt_arg();
157             break;
158         case OPT_CAFILE:
159             CAfile = opt_arg();
160             break;
161         case OPT_NOCAPATH:
162             noCApath = 1;
163             break;
164         case OPT_NOCAFILE:
165             noCAfile = 1;
166             break;
167         case OPT_CIPHER:
168             cipher = opt_arg();
169             break;
170         case OPT_BUGS:
171             st_bugs = 1;
172             break;
173         case OPT_TIME:
174             if (!opt_int(opt_arg(), &maxtime))
175                 goto opthelp;
176             break;
177         case OPT_WWW:
178             www_path = opt_arg();
179             buf_size = strlen(www_path) + sizeof(fmt_http_get_cmd) - 2;  /* 2 is for %s */
180             if (buf_size > sizeof(buf)) {
181                 BIO_printf(bio_err, "%s: -www option is too long\n", prog);
182                 goto end;
183             }
184             break;
185         case OPT_SSL3:
186             max_version = SSL3_VERSION;
187             break;
188         }
189     }
190     argc = opt_num_rest();
191     if (argc != 0)
192         goto opthelp;
193
194     if (cipher == NULL)
195         cipher = getenv("SSL_CIPHER");
196     if (cipher == NULL) {
197         BIO_printf(bio_err, "No CIPHER specified\n");
198         goto end;
199     }
200
201     if ((ctx = SSL_CTX_new(meth)) == NULL)
202         goto end;
203
204     SSL_CTX_set_quiet_shutdown(ctx, 1);
205     if (SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
206         goto end;
207
208     if (st_bugs)
209         SSL_CTX_set_options(ctx, SSL_OP_ALL);
210     if (!SSL_CTX_set_cipher_list(ctx, cipher))
211         goto end;
212     if (!set_cert_stuff(ctx, certfile, keyfile))
213         goto end;
214
215     if (!ctx_set_verify_locations(ctx, CAfile, CApath, noCAfile, noCApath)) {
216         ERR_print_errors(bio_err);
217         goto end;
218     }
219     if (!(perform & 1))
220         goto next;
221     printf("Collecting connection statistics for %d seconds\n", maxtime);
222
223     /* Loop and time how long it takes to make connections */
224
225     bytes_read = 0;
226     finishtime = (long)time(NULL) + maxtime;
227     tm_Time_F(START);
228     for (;;) {
229         if (finishtime < (long)time(NULL))
230             break;
231
232         if ((scon = doConnection(NULL, host, ctx)) == NULL)
233             goto end;
234
235         if (www_path != NULL) {
236             buf_len = BIO_snprintf(buf, sizeof buf,
237                                    fmt_http_get_cmd, www_path);
238             if (SSL_write(scon, buf, buf_len) <= 0)
239                 goto end;
240             while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
241                 bytes_read += i;
242         }
243 #ifdef NO_SHUTDOWN
244         SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
245 #else
246         SSL_shutdown(scon);
247 #endif
248         BIO_closesocket(SSL_get_fd(scon));
249
250         nConn += 1;
251         if (SSL_session_reused(scon))
252             ver = 'r';
253         else {
254             ver = SSL_version(scon);
255             if (ver == TLS1_VERSION)
256                 ver = 't';
257             else if (ver == SSL3_VERSION)
258                 ver = '3';
259             else
260                 ver = '*';
261         }
262         fputc(ver, stdout);
263         fflush(stdout);
264
265         SSL_free(scon);
266         scon = NULL;
267     }
268     totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
269
270     i = (int)((long)time(NULL) - finishtime + maxtime);
271     printf
272         ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
273          nConn, totalTime, ((double)nConn / totalTime), bytes_read);
274     printf
275         ("%d connections in %ld real seconds, %ld bytes read per connection\n",
276          nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
277
278     /*
279      * Now loop and time connections using the same session id over and over
280      */
281
282  next:
283     if (!(perform & 2))
284         goto end;
285     printf("\n\nNow timing with session id reuse.\n");
286
287     /* Get an SSL object so we can reuse the session id */
288     if ((scon = doConnection(NULL, host, ctx)) == NULL) {
289         BIO_printf(bio_err, "Unable to get connection\n");
290         goto end;
291     }
292
293     if (www_path != NULL) {
294         buf_len = BIO_snprintf(buf, sizeof buf,
295                                fmt_http_get_cmd, www_path);
296         if (SSL_write(scon, buf, buf_len) <= 0)
297             goto end;
298         while (SSL_read(scon, buf, sizeof(buf)) > 0)
299             continue;
300     }
301 #ifdef NO_SHUTDOWN
302     SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
303 #else
304     SSL_shutdown(scon);
305 #endif
306     BIO_closesocket(SSL_get_fd(scon));
307
308     nConn = 0;
309     totalTime = 0.0;
310
311     finishtime = (long)time(NULL) + maxtime;
312
313     printf("starting\n");
314     bytes_read = 0;
315     tm_Time_F(START);
316
317     for (;;) {
318         if (finishtime < (long)time(NULL))
319             break;
320
321         if ((doConnection(scon, host, ctx)) == NULL)
322             goto end;
323
324         if (www_path) {
325             BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n",
326                          www_path);
327             if (SSL_write(scon, buf, strlen(buf)) <= 0)
328                 goto end;
329             while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
330                 bytes_read += i;
331         }
332 #ifdef NO_SHUTDOWN
333         SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
334 #else
335         SSL_shutdown(scon);
336 #endif
337         BIO_closesocket(SSL_get_fd(scon));
338
339         nConn += 1;
340         if (SSL_session_reused(scon))
341             ver = 'r';
342         else {
343             ver = SSL_version(scon);
344             if (ver == TLS1_VERSION)
345                 ver = 't';
346             else if (ver == SSL3_VERSION)
347                 ver = '3';
348             else
349                 ver = '*';
350         }
351         fputc(ver, stdout);
352         fflush(stdout);
353     }
354     totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
355
356     printf
357         ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
358          nConn, totalTime, ((double)nConn / totalTime), bytes_read);
359     printf
360         ("%d connections in %ld real seconds, %ld bytes read per connection\n",
361          nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
362
363     ret = 0;
364
365  end:
366     SSL_free(scon);
367     SSL_CTX_free(ctx);
368     return (ret);
369 }
370
371 /*-
372  * doConnection - make a connection
373  */
374 static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx)
375 {
376     BIO *conn;
377     SSL *serverCon;
378     int width, i;
379     fd_set readfds;
380
381     if ((conn = BIO_new(BIO_s_connect())) == NULL)
382         return (NULL);
383
384     BIO_set_conn_hostname(conn, host);
385
386     if (scon == NULL)
387         serverCon = SSL_new(ctx);
388     else {
389         serverCon = scon;
390         SSL_set_connect_state(serverCon);
391     }
392
393     SSL_set_bio(serverCon, conn, conn);
394
395     /* ok, lets connect */
396     for (;;) {
397         i = SSL_connect(serverCon);
398         if (BIO_sock_should_retry(i)) {
399             BIO_printf(bio_err, "DELAY\n");
400
401             i = SSL_get_fd(serverCon);
402             width = i + 1;
403             FD_ZERO(&readfds);
404             openssl_fdset(i, &readfds);
405             /*
406              * Note: under VMS with SOCKETSHR the 2nd parameter is currently
407              * of type (int *) whereas under other systems it is (void *) if
408              * you don't have a cast it will choke the compiler: if you do
409              * have a cast then you can either go for (int *) or (void *).
410              */
411             select(width, (void *)&readfds, NULL, NULL, NULL);
412             continue;
413         }
414         break;
415     }
416     if (i <= 0) {
417         BIO_printf(bio_err, "ERROR\n");
418         if (verify_error != X509_V_OK)
419             BIO_printf(bio_err, "verify error:%s\n",
420                        X509_verify_cert_error_string(verify_error));
421         else
422             ERR_print_errors(bio_err);
423         if (scon == NULL)
424             SSL_free(serverCon);
425         return NULL;
426     }
427
428     return serverCon;
429 }
430 #endif /* OPENSSL_NO_SOCK */