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