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_COMMON,
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
tm_Time_F(int s)110 static double tm_Time_F(int s)
111 {
112 return app_tminterval(s, 1);
113 }
114
s_time_main(int argc,char ** argv)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 verify_args.depth = opt_int_arg();
158 BIO_printf(bio_err, "%s: verify depth is %d\n",
159 prog, verify_args.depth);
160 break;
161 case OPT_CERT:
162 certfile = opt_arg();
163 break;
164 case OPT_NAMEOPT:
165 if (!set_nameopt(opt_arg()))
166 goto end;
167 break;
168 case OPT_KEY:
169 keyfile = opt_arg();
170 break;
171 case OPT_CAPATH:
172 CApath = opt_arg();
173 break;
174 case OPT_CAFILE:
175 CAfile = opt_arg();
176 break;
177 case OPT_NOCAPATH:
178 noCApath = 1;
179 break;
180 case OPT_NOCAFILE:
181 noCAfile = 1;
182 break;
183 case OPT_CASTORE:
184 CAstore = opt_arg();
185 break;
186 case OPT_NOCASTORE:
187 noCAstore = 1;
188 break;
189 case OPT_CIPHER:
190 cipher = opt_arg();
191 break;
192 case OPT_CIPHERSUITES:
193 ciphersuites = opt_arg();
194 break;
195 case OPT_BUGS:
196 st_bugs = 1;
197 break;
198 case OPT_TIME:
199 maxtime = opt_int_arg();
200 break;
201 case OPT_WWW:
202 www_path = opt_arg();
203 buf_size = strlen(www_path) + fmt_http_get_cmd_size;
204 if (buf_size > sizeof(buf)) {
205 BIO_printf(bio_err, "%s: -www option is too long\n", prog);
206 goto end;
207 }
208 break;
209 case OPT_SSL3:
210 min_version = SSL3_VERSION;
211 max_version = SSL3_VERSION;
212 break;
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;
229 case OPT_PROV_CASES:
230 if (!opt_provider(o))
231 goto end;
232 break;
233 }
234 }
235
236 /* No extra arguments. */
237 argc = opt_num_rest();
238 if (argc != 0)
239 goto opthelp;
240
241 if (cipher == NULL)
242 cipher = getenv("SSL_CIPHER");
243
244 if ((ctx = SSL_CTX_new(meth)) == NULL)
245 goto end;
246
247 SSL_CTX_set_quiet_shutdown(ctx, 1);
248 if (SSL_CTX_set_min_proto_version(ctx, min_version) == 0)
249 goto end;
250 if (SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
251 goto end;
252
253 if (st_bugs)
254 SSL_CTX_set_options(ctx, SSL_OP_ALL);
255 if (cipher != NULL && !SSL_CTX_set_cipher_list(ctx, cipher))
256 goto end;
257 if (ciphersuites != NULL && !SSL_CTX_set_ciphersuites(ctx, ciphersuites))
258 goto end;
259 if (!set_cert_stuff(ctx, certfile, keyfile))
260 goto end;
261
262 if (!ctx_set_verify_locations(ctx, CAfile, noCAfile, CApath, noCApath,
263 CAstore, noCAstore)) {
264 ERR_print_errors(bio_err);
265 goto end;
266 }
267 if (!(perform & 1))
268 goto next;
269 printf("Collecting connection statistics for %d seconds\n", maxtime);
270
271 /* Loop and time how long it takes to make connections */
272
273 bytes_read = 0;
274 finishtime = (long)time(NULL) + maxtime;
275 tm_Time_F(START);
276 for (;;) {
277 if (finishtime < (long)time(NULL))
278 break;
279
280 if ((scon = doConnection(NULL, host, ctx)) == NULL)
281 goto end;
282
283 if (www_path != NULL) {
284 buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd,
285 www_path);
286 if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
287 goto end;
288 while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
289 bytes_read += i;
290 }
291 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
292 BIO_closesocket(SSL_get_fd(scon));
293
294 nConn += 1;
295 if (SSL_session_reused(scon)) {
296 ver = 'r';
297 } else {
298 ver = SSL_version(scon);
299 if (ver == TLS1_VERSION)
300 ver = 't';
301 else if (ver == SSL3_VERSION)
302 ver = '3';
303 else
304 ver = '*';
305 }
306 fputc(ver, stdout);
307 fflush(stdout);
308
309 SSL_free(scon);
310 scon = NULL;
311 }
312 totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
313
314 i = (int)((long)time(NULL) - finishtime + maxtime);
315 printf
316 ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
317 nConn, totalTime, ((double)nConn / totalTime), bytes_read);
318 printf
319 ("%d connections in %ld real seconds, %ld bytes read per connection\n",
320 nConn, (long)time(NULL) - finishtime + maxtime,
321 nConn > 0 ? bytes_read / nConn : 0l);
322
323 /*
324 * Now loop and time connections using the same session id over and over
325 */
326
327 next:
328 if (!(perform & 2))
329 goto end;
330 printf("\n\nNow timing with session id reuse.\n");
331
332 /* Get an SSL object so we can reuse the session id */
333 if ((scon = doConnection(NULL, host, ctx)) == NULL) {
334 BIO_printf(bio_err, "Unable to get connection\n");
335 goto end;
336 }
337
338 if (www_path != NULL) {
339 buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd, www_path);
340 if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
341 goto end;
342 while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
343 continue;
344 }
345 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
346 if ((fd = SSL_get_fd(scon)) >= 0)
347 BIO_closesocket(fd);
348
349 nConn = 0;
350 totalTime = 0.0;
351
352 finishtime = (long)time(NULL) + maxtime;
353
354 printf("starting\n");
355 bytes_read = 0;
356 tm_Time_F(START);
357
358 for (;;) {
359 if (finishtime < (long)time(NULL))
360 break;
361
362 if ((doConnection(scon, host, ctx)) == NULL)
363 goto end;
364
365 if (www_path != NULL) {
366 buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd,
367 www_path);
368 if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
369 goto end;
370 while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
371 bytes_read += i;
372 }
373 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
374 if ((fd = SSL_get_fd(scon)) >= 0)
375 BIO_closesocket(fd);
376
377 nConn += 1;
378 if (SSL_session_reused(scon)) {
379 ver = 'r';
380 } else {
381 ver = SSL_version(scon);
382 if (ver == TLS1_VERSION)
383 ver = 't';
384 else if (ver == SSL3_VERSION)
385 ver = '3';
386 else
387 ver = '*';
388 }
389 fputc(ver, stdout);
390 fflush(stdout);
391 }
392 totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
393
394 printf
395 ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
396 nConn, totalTime, ((double)nConn / totalTime), bytes_read);
397 if (nConn > 0)
398 printf
399 ("%d connections in %ld real seconds, %ld bytes read per connection\n",
400 nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
401 else
402 printf("0 connections in %ld real seconds\n",
403 (long)time(NULL) - finishtime + maxtime);
404 ret = 0;
405
406 end:
407 SSL_free(scon);
408 SSL_CTX_free(ctx);
409 return ret;
410 }
411
412 /*-
413 * doConnection - make a connection
414 */
doConnection(SSL * scon,const char * host,SSL_CTX * ctx)415 static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx)
416 {
417 BIO *conn;
418 SSL *serverCon;
419 int i;
420
421 if ((conn = BIO_new(BIO_s_connect())) == NULL)
422 return NULL;
423
424 if (BIO_set_conn_hostname(conn, host) <= 0
425 || BIO_set_conn_mode(conn, BIO_SOCK_NODELAY) <= 0) {
426 BIO_free(conn);
427 return NULL;
428 }
429
430 if (scon == NULL) {
431 serverCon = SSL_new(ctx);
432 if (serverCon == NULL) {
433 BIO_free(conn);
434 return NULL;
435 }
436 } else {
437 serverCon = scon;
438 SSL_set_connect_state(serverCon);
439 }
440
441 SSL_set_bio(serverCon, conn, conn);
442
443 /* ok, lets connect */
444 i = SSL_connect(serverCon);
445 if (i <= 0) {
446 BIO_printf(bio_err, "ERROR\n");
447 if (verify_args.error != X509_V_OK)
448 BIO_printf(bio_err, "verify error:%s\n",
449 X509_verify_cert_error_string(verify_args.error));
450 else
451 ERR_print_errors(bio_err);
452 if (scon == NULL)
453 SSL_free(serverCon);
454 return NULL;
455 }
456
457 #if defined(SOL_SOCKET) && defined(SO_LINGER)
458 {
459 struct linger no_linger;
460 int fd;
461
462 no_linger.l_onoff = 1;
463 no_linger.l_linger = 0;
464 fd = SSL_get_fd(serverCon);
465 if (fd >= 0)
466 (void)setsockopt(fd, SOL_SOCKET, SO_LINGER, (char*)&no_linger,
467 sizeof(no_linger));
468 }
469 #endif
470
471 return serverCon;
472 }
473 #endif /* OPENSSL_NO_SOCK */
474