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