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