]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/s_time.c
Check non-option arguments
[thirdparty/openssl.git] / apps / s_time.c
CommitLineData
846e33c7 1/*
33388b44 2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
dffa7520 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
846e33c7
RS
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
d02b48c6
RE
8 */
9
d02b48c6
RE
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13
f9e55034
MC
14#include <openssl/opensslconf.h>
15
16#ifndef OPENSSL_NO_SOCK
17
a4a8f7b3 18#include "apps.h"
dab2cd68 19#include "progs.h"
ec577822
BM
20#include <openssl/x509.h>
21#include <openssl/ssl.h>
22#include <openssl/pem.h>
d02b48c6 23#include "s_apps.h"
ec577822 24#include <openssl/err.h>
0870c8ea 25#include <internal/sockets.h>
f559f31b 26#if !defined(OPENSSL_SYS_MSDOS)
6b10d29c 27# include <unistd.h>
f559f31b 28#endif
d02b48c6 29
0f113f3e 30#define SSL_CONNECT_NAME "localhost:4433"
d02b48c6 31
0f113f3e 32#define SECONDS 30
7e1b7485
RS
33#define SECONDSSTR "30"
34
7e1b7485 35static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx);
d02b48c6 36
eee95522
P
37/*
38 * Define a HTTP get command globally.
39 * Also define the size of the command, this is two bytes less than
40 * the size of the string because the %s is replaced by the URL.
41 */
7606c231 42static const char fmt_http_get_cmd[] = "GET %s HTTP/1.0\r\n\r\n";
eee95522 43static const size_t fmt_http_get_cmd_size = sizeof(fmt_http_get_cmd) - 2;
7606c231 44
7e1b7485
RS
45typedef enum OPTION_choice {
46 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
e54b3ccd 47 OPT_CONNECT, OPT_CIPHER, OPT_CIPHERSUITES, OPT_CERT, OPT_NAMEOPT, OPT_KEY,
fd3397fc
RL
48 OPT_CAPATH, OPT_CAFILE, OPT_CASTORE,
49 OPT_NOCAPATH, OPT_NOCAFILE, OPT_NOCASTORE,
50 OPT_NEW, OPT_REUSE, OPT_BUGS, OPT_VERIFY, OPT_TIME, OPT_SSL3,
6bd4e3f2
P
51 OPT_WWW, OPT_TLS1, OPT_TLS1_1, OPT_TLS1_2, OPT_TLS1_3,
52 OPT_PROV_ENUM
7e1b7485
RS
53} OPTION_CHOICE;
54
44c83ebd 55const OPTIONS s_time_options[] = {
5388f986 56 OPT_SECTION("General"),
7e1b7485 57 {"help", OPT_HELP, '-', "Display this summary"},
5388f986
RS
58
59 OPT_SECTION("Connection"),
7e1b7485
RS
60 {"connect", OPT_CONNECT, 's',
61 "Where to connect as post:port (default is " SSL_CONNECT_NAME ")"},
7e1b7485
RS
62 {"new", OPT_NEW, '-', "Just time new connections"},
63 {"reuse", OPT_REUSE, '-', "Just time connection reuse"},
64 {"bugs", OPT_BUGS, '-', "Turn on SSL bug compatibility"},
5388f986
RS
65 {"cipher", OPT_CIPHER, 's', "TLSv1.2 and below cipher list to be used"},
66 {"ciphersuites", OPT_CIPHERSUITES, 's',
67 "Specify TLSv1.3 ciphersuites to be used"},
7e1b7485
RS
68#ifndef OPENSSL_NO_SSL3
69 {"ssl3", OPT_SSL3, '-', "Just use SSLv3"},
7757a90e 70#endif
71#ifndef OPENSSL_NO_TLS1
72 {"tls1", OPT_TLS1, '-', "Just use TLSv1.0"},
73#endif
74#ifndef OPENSSL_NO_TLS1_1
75 {"tls1_1", OPT_TLS1_1, '-', "Just use TLSv1.1"},
76#endif
77#ifndef OPENSSL_NO_TLS1_2
78 {"tls1_2", OPT_TLS1_2, '-', "Just use TLSv1.2"},
79#endif
80#ifndef OPENSSL_NO_TLS1_3
81 {"tls1_3", OPT_TLS1_3, '-', "Just use TLSv1.3"},
d02b48c6 82#endif
5388f986
RS
83 {"verify", OPT_VERIFY, 'p',
84 "Turn on peer certificate verification, set depth"},
85 {"time", OPT_TIME, 'p', "Seconds to collect data, default " SECONDSSTR},
86 {"www", OPT_WWW, 's', "Fetch specified page from the site"},
87
88 OPT_SECTION("Certificate"),
2b264aee 89 {"nameopt", OPT_NAMEOPT, 's', "Certificate subject/issuer name printing options"},
5388f986
RS
90 {"cert", OPT_CERT, '<', "Cert file to use, PEM format assumed"},
91 {"key", OPT_KEY, '<', "File with key, PEM; default is -cert file"},
92 {"cafile", OPT_CAFILE, '<', "PEM format file of CA's"},
65718c51 93 {"CAfile", OPT_CAFILE, '<', "PEM format file of CA's"},
5388f986
RS
94 {"CApath", OPT_CAPATH, '/', "PEM format directory of CA's"},
95 {"CAstore", OPT_CASTORE, ':', "URI to store of CA's"},
96 {"no-CAfile", OPT_NOCAFILE, '-',
97 "Do not load the default certificates file"},
98 {"no-CApath", OPT_NOCAPATH, '-',
99 "Do not load certificates from the default certificates directory"},
100 {"no-CAstore", OPT_NOCASTORE, '-',
101 "Do not load certificates from the default certificates store URI"},
102
6bd4e3f2 103 OPT_PROV_OPTIONS,
7e1b7485
RS
104 {NULL}
105};
d02b48c6 106
7e1b7485
RS
107#define START 0
108#define STOP 1
58964a49 109
7e1b7485 110static double tm_Time_F(int s)
d02b48c6 111{
7e1b7485 112 return app_tminterval(s, 1);
d02b48c6
RE
113}
114
7e1b7485 115int s_time_main(int argc, char **argv)
d02b48c6 116{
7e1b7485
RS
117 char buf[1024 * 8];
118 SSL *scon = NULL;
119 SSL_CTX *ctx = NULL;
120 const SSL_METHOD *meth = NULL;
fd3397fc
RL
121 char *CApath = NULL, *CAfile = NULL, *CAstore = NULL;
122 char *cipher = NULL, *ciphersuites = NULL;
e54b3ccd 123 char *www_path = NULL;
7e1b7485
RS
124 char *host = SSL_CONNECT_NAME, *certfile = NULL, *keyfile = NULL, *prog;
125 double totalTime = 0.0;
fd3397fc 126 int noCApath = 0, noCAfile = 0, noCAstore = 0;
7606c231 127 int maxtime = SECONDS, nConn = 0, perform = 3, ret = 1, i, st_bugs = 0;
7e1b7485
RS
128 long bytes_read = 0, finishtime = 0;
129 OPTION_CHOICE o;
7757a90e 130 int min_version = 0, max_version = 0, ver, buf_len;
7606c231 131 size_t buf_size;
d02b48c6 132
13c9bb3e 133 meth = TLS_client_method();
d02b48c6 134
7e1b7485
RS
135 prog = opt_init(argc, argv, s_time_options);
136 while ((o = opt_next()) != OPT_EOF) {
137 switch (o) {
138 case OPT_EOF:
139 case OPT_ERR:
140 opthelp:
141 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
142 goto end;
143 case OPT_HELP:
144 opt_help(s_time_options);
145 ret = 0;
146 goto end;
147 case OPT_CONNECT:
148 host = opt_arg();
149 break;
150 case OPT_REUSE:
0f113f3e 151 perform = 2;
7e1b7485
RS
152 break;
153 case OPT_NEW:
0f113f3e 154 perform = 1;
7e1b7485
RS
155 break;
156 case OPT_VERIFY:
acc00492 157 if (!opt_int(opt_arg(), &verify_args.depth))
7e1b7485
RS
158 goto opthelp;
159 BIO_printf(bio_err, "%s: verify depth is %d\n",
acc00492 160 prog, verify_args.depth);
7e1b7485
RS
161 break;
162 case OPT_CERT:
163 certfile = opt_arg();
164 break;
a7c04f2b
DB
165 case OPT_NAMEOPT:
166 if (!set_nameopt(opt_arg()))
167 goto end;
168 break;
7e1b7485
RS
169 case OPT_KEY:
170 keyfile = opt_arg();
171 break;
172 case OPT_CAPATH:
173 CApath = opt_arg();
174 break;
175 case OPT_CAFILE:
176 CAfile = opt_arg();
177 break;
2b6bcb70
MC
178 case OPT_NOCAPATH:
179 noCApath = 1;
180 break;
181 case OPT_NOCAFILE:
182 noCAfile = 1;
183 break;
fd3397fc
RL
184 case OPT_CASTORE:
185 CAstore = opt_arg();
186 break;
187 case OPT_NOCASTORE:
188 noCAstore = 1;
189 break;
7e1b7485
RS
190 case OPT_CIPHER:
191 cipher = opt_arg();
192 break;
e54b3ccd
MC
193 case OPT_CIPHERSUITES:
194 ciphersuites = opt_arg();
195 break;
7e1b7485 196 case OPT_BUGS:
0f113f3e 197 st_bugs = 1;
7e1b7485
RS
198 break;
199 case OPT_TIME:
200 if (!opt_int(opt_arg(), &maxtime))
201 goto opthelp;
202 break;
203 case OPT_WWW:
204 www_path = opt_arg();
eee95522 205 buf_size = strlen(www_path) + fmt_http_get_cmd_size;
7606c231
F
206 if (buf_size > sizeof(buf)) {
207 BIO_printf(bio_err, "%s: -www option is too long\n", prog);
7e1b7485 208 goto end;
dfef52f6 209 }
0f113f3e 210 break;
7e1b7485 211 case OPT_SSL3:
7757a90e 212 min_version = SSL3_VERSION;
0d5301af 213 max_version = SSL3_VERSION;
9c3bcfa0 214 break;
7757a90e 215 case OPT_TLS1:
216 min_version = TLS1_VERSION;
217 max_version = TLS1_VERSION;
218 break;
219 case OPT_TLS1_1:
220 min_version = TLS1_1_VERSION;
221 max_version = TLS1_1_VERSION;
222 break;
223 case OPT_TLS1_2:
224 min_version = TLS1_2_VERSION;
225 max_version = TLS1_2_VERSION;
226 break;
227 case OPT_TLS1_3:
228 min_version = TLS1_3_VERSION;
229 max_version = TLS1_3_VERSION;
230 break;
6bd4e3f2
P
231 case OPT_PROV_CASES:
232 if (!opt_provider(o))
233 goto end;
234 break;
0f113f3e 235 }
d02b48c6 236 }
021410ea
RS
237
238 /* No extra arguments. */
7e1b7485 239 argc = opt_num_rest();
03358517
KR
240 if (argc != 0)
241 goto opthelp;
d02b48c6 242
7e1b7485
RS
243 if (cipher == NULL)
244 cipher = getenv("SSL_CIPHER");
d02b48c6 245
7e1b7485 246 if ((ctx = SSL_CTX_new(meth)) == NULL)
0f113f3e
MC
247 goto end;
248
0870c8ea 249 SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
7e1b7485 250 SSL_CTX_set_quiet_shutdown(ctx, 1);
7757a90e 251 if (SSL_CTX_set_min_proto_version(ctx, min_version) == 0)
252 goto end;
0d5301af
KR
253 if (SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
254 goto end;
0f113f3e
MC
255
256 if (st_bugs)
7e1b7485 257 SSL_CTX_set_options(ctx, SSL_OP_ALL);
e54b3ccd
MC
258 if (cipher != NULL && !SSL_CTX_set_cipher_list(ctx, cipher))
259 goto end;
260 if (ciphersuites != NULL && !SSL_CTX_set_ciphersuites(ctx, ciphersuites))
ac59d705 261 goto end;
7e1b7485 262 if (!set_cert_stuff(ctx, certfile, keyfile))
0f113f3e
MC
263 goto end;
264
fd3397fc
RL
265 if (!ctx_set_verify_locations(ctx, CAfile, noCAfile, CApath, noCApath,
266 CAstore, noCAstore)) {
0f113f3e 267 ERR_print_errors(bio_err);
7e1b7485 268 goto end;
0f113f3e 269 }
0f113f3e
MC
270 if (!(perform & 1))
271 goto next;
7e1b7485 272 printf("Collecting connection statistics for %d seconds\n", maxtime);
d02b48c6 273
0f113f3e 274 /* Loop and time how long it takes to make connections */
d02b48c6 275
0f113f3e 276 bytes_read = 0;
7e1b7485 277 finishtime = (long)time(NULL) + maxtime;
0f113f3e
MC
278 tm_Time_F(START);
279 for (;;) {
280 if (finishtime < (long)time(NULL))
281 break;
d02b48c6 282
7e1b7485 283 if ((scon = doConnection(NULL, host, ctx)) == NULL)
0f113f3e 284 goto end;
d02b48c6 285
7e1b7485 286 if (www_path != NULL) {
eee95522
P
287 buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd,
288 www_path);
289 if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
ac59d705 290 goto end;
0870c8ea
BE
291 while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
292 bytes_read += i;
0f113f3e 293 }
0f113f3e 294 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
8731a4fc 295 BIO_closesocket(SSL_get_fd(scon));
0f113f3e
MC
296
297 nConn += 1;
2234212c 298 if (SSL_session_reused(scon)) {
0f113f3e 299 ver = 'r';
2234212c 300 } else {
0f113f3e
MC
301 ver = SSL_version(scon);
302 if (ver == TLS1_VERSION)
303 ver = 't';
304 else if (ver == SSL3_VERSION)
305 ver = '3';
306 else
307 ver = '*';
308 }
309 fputc(ver, stdout);
310 fflush(stdout);
311
312 SSL_free(scon);
313 scon = NULL;
314 }
315 totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
316
7e1b7485 317 i = (int)((long)time(NULL) - finishtime + maxtime);
0f113f3e
MC
318 printf
319 ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
320 nConn, totalTime, ((double)nConn / totalTime), bytes_read);
321 printf
322 ("%d connections in %ld real seconds, %ld bytes read per connection\n",
7e1b7485 323 nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
0f113f3e
MC
324
325 /*
326 * Now loop and time connections using the same session id over and over
327 */
328
329 next:
330 if (!(perform & 2))
331 goto end;
332 printf("\n\nNow timing with session id reuse.\n");
333
334 /* Get an SSL object so we can reuse the session id */
7e1b7485 335 if ((scon = doConnection(NULL, host, ctx)) == NULL) {
7768e116 336 BIO_printf(bio_err, "Unable to get connection\n");
0f113f3e
MC
337 goto end;
338 }
339
7e1b7485 340 if (www_path != NULL) {
eee95522
P
341 buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd, www_path);
342 if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
ac59d705 343 goto end;
0870c8ea 344 while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
7e1b7485 345 continue;
0f113f3e 346 }
0f113f3e 347 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
8731a4fc 348 BIO_closesocket(SSL_get_fd(scon));
0f113f3e
MC
349
350 nConn = 0;
351 totalTime = 0.0;
d02b48c6 352
7e1b7485 353 finishtime = (long)time(NULL) + maxtime;
d02b48c6 354
0f113f3e
MC
355 printf("starting\n");
356 bytes_read = 0;
357 tm_Time_F(START);
d02b48c6 358
0f113f3e
MC
359 for (;;) {
360 if (finishtime < (long)time(NULL))
361 break;
d02b48c6 362
7e1b7485 363 if ((doConnection(scon, host, ctx)) == NULL)
0f113f3e 364 goto end;
d02b48c6 365
2234212c 366 if (www_path != NULL) {
eee95522
P
367 buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd,
368 www_path);
369 if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
ac59d705 370 goto end;
0870c8ea
BE
371 while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
372 bytes_read += i;
0f113f3e 373 }
0f113f3e 374 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
8731a4fc 375 BIO_closesocket(SSL_get_fd(scon));
0f113f3e
MC
376
377 nConn += 1;
2234212c 378 if (SSL_session_reused(scon)) {
0f113f3e 379 ver = 'r';
2234212c 380 } else {
0f113f3e
MC
381 ver = SSL_version(scon);
382 if (ver == TLS1_VERSION)
383 ver = 't';
384 else if (ver == SSL3_VERSION)
385 ver = '3';
386 else
387 ver = '*';
388 }
389 fputc(ver, stdout);
390 fflush(stdout);
391 }
392 totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
393
394 printf
395 ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
396 nConn, totalTime, ((double)nConn / totalTime), bytes_read);
397 printf
398 ("%d connections in %ld real seconds, %ld bytes read per connection\n",
7e1b7485 399 nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
0f113f3e
MC
400
401 ret = 0;
7e1b7485 402
0f113f3e 403 end:
62adbcee 404 SSL_free(scon);
7e1b7485 405 SSL_CTX_free(ctx);
eee95522 406 return ret;
0f113f3e 407}
d02b48c6 408
c80fd6b2 409/*-
d02b48c6 410 * doConnection - make a connection
d02b48c6 411 */
7e1b7485 412static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx)
0f113f3e
MC
413{
414 BIO *conn;
415 SSL *serverCon;
0870c8ea 416 int i;
0f113f3e
MC
417
418 if ((conn = BIO_new(BIO_s_connect())) == NULL)
eee95522 419 return NULL;
0f113f3e 420
b7a8fb52
P
421 if (BIO_set_conn_hostname(conn, host) <= 0
422 || BIO_set_conn_mode(conn, BIO_SOCK_NODELAY) <= 0) {
423 BIO_free(conn);
424 return NULL;
425 }
0f113f3e 426
b7a8fb52 427 if (scon == NULL) {
7e1b7485 428 serverCon = SSL_new(ctx);
b7a8fb52
P
429 if (serverCon == NULL) {
430 BIO_free(conn);
431 return NULL;
432 }
433 } else {
0f113f3e
MC
434 serverCon = scon;
435 SSL_set_connect_state(serverCon);
436 }
d02b48c6 437
0f113f3e 438 SSL_set_bio(serverCon, conn, conn);
d02b48c6 439
0f113f3e 440 /* ok, lets connect */
0870c8ea 441 i = SSL_connect(serverCon);
0f113f3e
MC
442 if (i <= 0) {
443 BIO_printf(bio_err, "ERROR\n");
acc00492 444 if (verify_args.error != X509_V_OK)
0f113f3e 445 BIO_printf(bio_err, "verify error:%s\n",
acc00492 446 X509_verify_cert_error_string(verify_args.error));
0f113f3e
MC
447 else
448 ERR_print_errors(bio_err);
449 if (scon == NULL)
450 SSL_free(serverCon);
451 return NULL;
452 }
d02b48c6 453
0870c8ea
BE
454#if defined(SOL_SOCKET) && defined(SO_LINGER)
455 {
456 struct linger no_linger;
5f49783c 457 int fd;
0870c8ea
BE
458
459 no_linger.l_onoff = 1;
460 no_linger.l_linger = 0;
5f49783c
MC
461 fd = SSL_get_fd(serverCon);
462 if (fd >= 0)
463 (void)setsockopt(fd, SOL_SOCKET, SO_LINGER, (char*)&no_linger,
464 sizeof(no_linger));
0870c8ea
BE
465 }
466#endif
467
0f113f3e
MC
468 return serverCon;
469}
f9e55034 470#endif /* OPENSSL_NO_SOCK */