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