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