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