]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/s_time.c
Fix a memleak in tls13_generate_secret.
[thirdparty/openssl.git] / apps / s_time.c
CommitLineData
846e33c7
RS
1/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
846e33c7
RS
4 * Licensed under the OpenSSL license (the "License"). You may not use
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
58964a49 10#define NO_SHUTDOWN
d02b48c6 11
d02b48c6
RE
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15
f9e55034
MC
16#include <openssl/opensslconf.h>
17
18#ifndef OPENSSL_NO_SOCK
19
a4a8f7b3
RL
20#define USE_SOCKETS
21#include "apps.h"
ec577822
BM
22#include <openssl/x509.h>
23#include <openssl/ssl.h>
24#include <openssl/pem.h>
d02b48c6 25#include "s_apps.h"
ec577822 26#include <openssl/err.h>
f559f31b 27#if !defined(OPENSSL_SYS_MSDOS)
0f113f3e 28# include OPENSSL_UNISTD
f559f31b 29#endif
d02b48c6 30
7d7d2cbc 31#undef ioctl
d02b48c6
RE
32#define ioctl ioctlsocket
33
0f113f3e 34#define SSL_CONNECT_NAME "localhost:4433"
d02b48c6 35
e636e2ac 36/* no default cert. */
0f113f3e
MC
37/*
38 * #define TEST_CERT "client.pem"
39 */
d02b48c6 40
3e83e686
RL
41#undef min
42#undef max
d02b48c6
RE
43#define min(a,b) (((a) < (b)) ? (a) : (b))
44#define max(a,b) (((a) > (b)) ? (a) : (b))
45
46#undef SECONDS
0f113f3e 47#define SECONDS 30
7e1b7485
RS
48#define SECONDSSTR "30"
49
7e1b7485 50static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx);
d02b48c6 51
7606c231
F
52static const char fmt_http_get_cmd[] = "GET %s HTTP/1.0\r\n\r\n";
53
7e1b7485
RS
54typedef enum OPTION_choice {
55 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
a7c04f2b 56 OPT_CONNECT, OPT_CIPHER, OPT_CERT, OPT_NAMEOPT, OPT_KEY, OPT_CAPATH,
2b6bcb70
MC
57 OPT_CAFILE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_NEW, OPT_REUSE, OPT_BUGS,
58 OPT_VERIFY, OPT_TIME, OPT_SSL3,
7e1b7485
RS
59 OPT_WWW
60} OPTION_CHOICE;
61
44c83ebd 62const OPTIONS s_time_options[] = {
7e1b7485
RS
63 {"help", OPT_HELP, '-', "Display this summary"},
64 {"connect", OPT_CONNECT, 's',
65 "Where to connect as post:port (default is " SSL_CONNECT_NAME ")"},
66 {"cipher", OPT_CIPHER, 's', "Cipher to use, see 'openssl ciphers'"},
67 {"cert", OPT_CERT, '<', "Cert file to use, PEM format assumed"},
a7c04f2b 68 {"nameopt", OPT_NAMEOPT, 's', "Various certificate name options"},
7e1b7485
RS
69 {"key", OPT_KEY, '<', "File with key, PEM; default is -cert file"},
70 {"CApath", OPT_CAPATH, '/', "PEM format directory of CA's"},
71 {"cafile", OPT_CAFILE, '<', "PEM format file of CA's"},
2b6bcb70
MC
72 {"no-CAfile", OPT_NOCAFILE, '-',
73 "Do not load the default certificates file"},
74 {"no-CApath", OPT_NOCAPATH, '-',
75 "Do not load certificates from the default certificates directory"},
7e1b7485
RS
76 {"new", OPT_NEW, '-', "Just time new connections"},
77 {"reuse", OPT_REUSE, '-', "Just time connection reuse"},
78 {"bugs", OPT_BUGS, '-', "Turn on SSL bug compatibility"},
79 {"verify", OPT_VERIFY, 'p',
80 "Turn on peer certificate verification, set depth"},
0d5301af 81 {"time", OPT_TIME, 'p', "Seconds to collect data, default " SECONDSSTR},
7e1b7485
RS
82 {"www", OPT_WWW, 's', "Fetch specified page from the site"},
83#ifndef OPENSSL_NO_SSL3
84 {"ssl3", OPT_SSL3, '-', "Just use SSLv3"},
d02b48c6 85#endif
7e1b7485
RS
86 {NULL}
87};
d02b48c6 88
7e1b7485
RS
89#define START 0
90#define STOP 1
58964a49 91
7e1b7485 92static double tm_Time_F(int s)
d02b48c6 93{
7e1b7485 94 return app_tminterval(s, 1);
d02b48c6
RE
95}
96
7e1b7485 97int s_time_main(int argc, char **argv)
d02b48c6 98{
7e1b7485
RS
99 char buf[1024 * 8];
100 SSL *scon = NULL;
101 SSL_CTX *ctx = NULL;
102 const SSL_METHOD *meth = NULL;
103 char *CApath = NULL, *CAfile = NULL, *cipher = NULL, *www_path = NULL;
104 char *host = SSL_CONNECT_NAME, *certfile = NULL, *keyfile = NULL, *prog;
105 double totalTime = 0.0;
2b6bcb70 106 int noCApath = 0, noCAfile = 0;
7606c231 107 int maxtime = SECONDS, nConn = 0, perform = 3, ret = 1, i, st_bugs = 0;
7e1b7485
RS
108 long bytes_read = 0, finishtime = 0;
109 OPTION_CHOICE o;
7606c231
F
110 int max_version = 0, ver, buf_len;
111 size_t buf_size;
d02b48c6 112
13c9bb3e 113 meth = TLS_client_method();
d02b48c6 114
7e1b7485
RS
115 prog = opt_init(argc, argv, s_time_options);
116 while ((o = opt_next()) != OPT_EOF) {
117 switch (o) {
118 case OPT_EOF:
119 case OPT_ERR:
120 opthelp:
121 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
122 goto end;
123 case OPT_HELP:
124 opt_help(s_time_options);
125 ret = 0;
126 goto end;
127 case OPT_CONNECT:
128 host = opt_arg();
129 break;
130 case OPT_REUSE:
0f113f3e 131 perform = 2;
7e1b7485
RS
132 break;
133 case OPT_NEW:
0f113f3e 134 perform = 1;
7e1b7485
RS
135 break;
136 case OPT_VERIFY:
acc00492 137 if (!opt_int(opt_arg(), &verify_args.depth))
7e1b7485
RS
138 goto opthelp;
139 BIO_printf(bio_err, "%s: verify depth is %d\n",
acc00492 140 prog, verify_args.depth);
7e1b7485
RS
141 break;
142 case OPT_CERT:
143 certfile = opt_arg();
144 break;
a7c04f2b
DB
145 case OPT_NAMEOPT:
146 if (!set_nameopt(opt_arg()))
147 goto end;
148 break;
7e1b7485
RS
149 case OPT_KEY:
150 keyfile = opt_arg();
151 break;
152 case OPT_CAPATH:
153 CApath = opt_arg();
154 break;
155 case OPT_CAFILE:
156 CAfile = opt_arg();
157 break;
2b6bcb70
MC
158 case OPT_NOCAPATH:
159 noCApath = 1;
160 break;
161 case OPT_NOCAFILE:
162 noCAfile = 1;
163 break;
7e1b7485
RS
164 case OPT_CIPHER:
165 cipher = opt_arg();
166 break;
167 case OPT_BUGS:
0f113f3e 168 st_bugs = 1;
7e1b7485
RS
169 break;
170 case OPT_TIME:
171 if (!opt_int(opt_arg(), &maxtime))
172 goto opthelp;
173 break;
174 case OPT_WWW:
175 www_path = opt_arg();
7606c231
F
176 buf_size = strlen(www_path) + sizeof(fmt_http_get_cmd) - 2; /* 2 is for %s */
177 if (buf_size > sizeof(buf)) {
178 BIO_printf(bio_err, "%s: -www option is too long\n", prog);
7e1b7485 179 goto end;
dfef52f6 180 }
0f113f3e 181 break;
7e1b7485 182 case OPT_SSL3:
0d5301af 183 max_version = SSL3_VERSION;
9c3bcfa0 184 break;
0f113f3e 185 }
d02b48c6 186 }
7e1b7485 187 argc = opt_num_rest();
03358517
KR
188 if (argc != 0)
189 goto opthelp;
d02b48c6 190
7e1b7485
RS
191 if (cipher == NULL)
192 cipher = getenv("SSL_CIPHER");
193 if (cipher == NULL) {
7768e116 194 BIO_printf(bio_err, "No CIPHER specified\n");
7e1b7485 195 goto end;
d02b48c6
RE
196 }
197
7e1b7485 198 if ((ctx = SSL_CTX_new(meth)) == NULL)
0f113f3e
MC
199 goto end;
200
7e1b7485 201 SSL_CTX_set_quiet_shutdown(ctx, 1);
0d5301af
KR
202 if (SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
203 goto end;
0f113f3e
MC
204
205 if (st_bugs)
7e1b7485
RS
206 SSL_CTX_set_options(ctx, SSL_OP_ALL);
207 if (!SSL_CTX_set_cipher_list(ctx, cipher))
ac59d705 208 goto end;
7e1b7485 209 if (!set_cert_stuff(ctx, certfile, keyfile))
0f113f3e
MC
210 goto end;
211
2b6bcb70 212 if (!ctx_set_verify_locations(ctx, CAfile, CApath, noCAfile, noCApath)) {
0f113f3e 213 ERR_print_errors(bio_err);
7e1b7485 214 goto end;
0f113f3e 215 }
0f113f3e
MC
216 if (!(perform & 1))
217 goto next;
7e1b7485 218 printf("Collecting connection statistics for %d seconds\n", maxtime);
d02b48c6 219
0f113f3e 220 /* Loop and time how long it takes to make connections */
d02b48c6 221
0f113f3e 222 bytes_read = 0;
7e1b7485 223 finishtime = (long)time(NULL) + maxtime;
0f113f3e
MC
224 tm_Time_F(START);
225 for (;;) {
226 if (finishtime < (long)time(NULL))
227 break;
d02b48c6 228
7e1b7485 229 if ((scon = doConnection(NULL, host, ctx)) == NULL)
0f113f3e 230 goto end;
d02b48c6 231
7e1b7485 232 if (www_path != NULL) {
7606c231
F
233 buf_len = BIO_snprintf(buf, sizeof buf,
234 fmt_http_get_cmd, www_path);
235 if (SSL_write(scon, buf, buf_len) <= 0)
ac59d705 236 goto end;
0f113f3e
MC
237 while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
238 bytes_read += i;
239 }
d02b48c6 240#ifdef NO_SHUTDOWN
0f113f3e 241 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
d02b48c6 242#else
0f113f3e 243 SSL_shutdown(scon);
d02b48c6 244#endif
8731a4fc 245 BIO_closesocket(SSL_get_fd(scon));
0f113f3e
MC
246
247 nConn += 1;
248 if (SSL_session_reused(scon))
249 ver = 'r';
250 else {
251 ver = SSL_version(scon);
252 if (ver == TLS1_VERSION)
253 ver = 't';
254 else if (ver == SSL3_VERSION)
255 ver = '3';
256 else
257 ver = '*';
258 }
259 fputc(ver, stdout);
260 fflush(stdout);
261
262 SSL_free(scon);
263 scon = NULL;
264 }
265 totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
266
7e1b7485 267 i = (int)((long)time(NULL) - finishtime + maxtime);
0f113f3e
MC
268 printf
269 ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
270 nConn, totalTime, ((double)nConn / totalTime), bytes_read);
271 printf
272 ("%d connections in %ld real seconds, %ld bytes read per connection\n",
7e1b7485 273 nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
0f113f3e
MC
274
275 /*
276 * Now loop and time connections using the same session id over and over
277 */
278
279 next:
280 if (!(perform & 2))
281 goto end;
282 printf("\n\nNow timing with session id reuse.\n");
283
284 /* Get an SSL object so we can reuse the session id */
7e1b7485 285 if ((scon = doConnection(NULL, host, ctx)) == NULL) {
7768e116 286 BIO_printf(bio_err, "Unable to get connection\n");
0f113f3e
MC
287 goto end;
288 }
289
7e1b7485 290 if (www_path != NULL) {
7606c231
F
291 buf_len = BIO_snprintf(buf, sizeof buf,
292 fmt_http_get_cmd, www_path);
293 if (SSL_write(scon, buf, buf_len) <= 0)
ac59d705 294 goto end;
7e1b7485
RS
295 while (SSL_read(scon, buf, sizeof(buf)) > 0)
296 continue;
0f113f3e 297 }
d02b48c6 298#ifdef NO_SHUTDOWN
0f113f3e 299 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
d02b48c6 300#else
0f113f3e 301 SSL_shutdown(scon);
d02b48c6 302#endif
8731a4fc 303 BIO_closesocket(SSL_get_fd(scon));
0f113f3e
MC
304
305 nConn = 0;
306 totalTime = 0.0;
d02b48c6 307
7e1b7485 308 finishtime = (long)time(NULL) + maxtime;
d02b48c6 309
0f113f3e
MC
310 printf("starting\n");
311 bytes_read = 0;
312 tm_Time_F(START);
d02b48c6 313
0f113f3e
MC
314 for (;;) {
315 if (finishtime < (long)time(NULL))
316 break;
d02b48c6 317
7e1b7485 318 if ((doConnection(scon, host, ctx)) == NULL)
0f113f3e 319 goto end;
d02b48c6 320
7e1b7485 321 if (www_path) {
0f113f3e 322 BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n",
7e1b7485 323 www_path);
61986d32 324 if (SSL_write(scon, buf, strlen(buf)) <= 0)
ac59d705 325 goto end;
0f113f3e
MC
326 while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
327 bytes_read += i;
328 }
d02b48c6 329#ifdef NO_SHUTDOWN
0f113f3e 330 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
d02b48c6 331#else
0f113f3e 332 SSL_shutdown(scon);
d02b48c6 333#endif
8731a4fc 334 BIO_closesocket(SSL_get_fd(scon));
0f113f3e
MC
335
336 nConn += 1;
337 if (SSL_session_reused(scon))
338 ver = 'r';
339 else {
340 ver = SSL_version(scon);
341 if (ver == TLS1_VERSION)
342 ver = 't';
343 else if (ver == SSL3_VERSION)
344 ver = '3';
345 else
346 ver = '*';
347 }
348 fputc(ver, stdout);
349 fflush(stdout);
350 }
351 totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
352
353 printf
354 ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
355 nConn, totalTime, ((double)nConn / totalTime), bytes_read);
356 printf
357 ("%d connections in %ld real seconds, %ld bytes read per connection\n",
7e1b7485 358 nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
0f113f3e
MC
359
360 ret = 0;
7e1b7485 361
0f113f3e 362 end:
62adbcee 363 SSL_free(scon);
7e1b7485
RS
364 SSL_CTX_free(ctx);
365 return (ret);
0f113f3e 366}
d02b48c6 367
c80fd6b2 368/*-
d02b48c6 369 * doConnection - make a connection
d02b48c6 370 */
7e1b7485 371static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx)
0f113f3e
MC
372{
373 BIO *conn;
374 SSL *serverCon;
375 int width, i;
376 fd_set readfds;
377
378 if ((conn = BIO_new(BIO_s_connect())) == NULL)
379 return (NULL);
380
0f113f3e
MC
381 BIO_set_conn_hostname(conn, host);
382
383 if (scon == NULL)
7e1b7485 384 serverCon = SSL_new(ctx);
0f113f3e
MC
385 else {
386 serverCon = scon;
387 SSL_set_connect_state(serverCon);
388 }
d02b48c6 389
0f113f3e 390 SSL_set_bio(serverCon, conn, conn);
d02b48c6 391
0f113f3e
MC
392 /* ok, lets connect */
393 for (;;) {
394 i = SSL_connect(serverCon);
395 if (BIO_sock_should_retry(i)) {
396 BIO_printf(bio_err, "DELAY\n");
397
398 i = SSL_get_fd(serverCon);
399 width = i + 1;
400 FD_ZERO(&readfds);
401 openssl_fdset(i, &readfds);
402 /*
403 * Note: under VMS with SOCKETSHR the 2nd parameter is currently
404 * of type (int *) whereas under other systems it is (void *) if
405 * you don't have a cast it will choke the compiler: if you do
406 * have a cast then you can either go for (int *) or (void *).
407 */
408 select(width, (void *)&readfds, NULL, NULL, NULL);
409 continue;
410 }
411 break;
412 }
413 if (i <= 0) {
414 BIO_printf(bio_err, "ERROR\n");
acc00492 415 if (verify_args.error != X509_V_OK)
0f113f3e 416 BIO_printf(bio_err, "verify error:%s\n",
acc00492 417 X509_verify_cert_error_string(verify_args.error));
0f113f3e
MC
418 else
419 ERR_print_errors(bio_err);
420 if (scon == NULL)
421 SSL_free(serverCon);
422 return NULL;
423 }
d02b48c6 424
0f113f3e
MC
425 return serverCon;
426}
f9e55034 427#endif /* OPENSSL_NO_SOCK */