]>
git.ipfire.org Git - thirdparty/openssl.git/blob - apps/s_time.c
2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
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
14 #include <openssl/opensslconf.h>
16 #ifndef OPENSSL_NO_SOCK
20 #include <openssl/x509.h>
21 #include <openssl/ssl.h>
22 #include <openssl/pem.h>
24 #include <openssl/err.h>
25 #include <internal/sockets.h>
26 #if !defined(OPENSSL_SYS_MSDOS)
27 # include OPENSSL_UNISTD
30 #define SSL_CONNECT_NAME "localhost:4433"
33 #define SECONDSSTR "30"
35 static SSL
*doConnection(SSL
*scon
, const char *host
, SSL_CTX
*ctx
);
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.
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;
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_NOCAPATH
, OPT_NOCAFILE
, OPT_NEW
, OPT_REUSE
,
49 OPT_BUGS
, OPT_VERIFY
, OPT_TIME
, OPT_SSL3
,
53 const OPTIONS s_time_options
[] = {
54 {"help", OPT_HELP
, '-', "Display this summary"},
55 {"connect", OPT_CONNECT
, 's',
56 "Where to connect as post:port (default is " SSL_CONNECT_NAME
")"},
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"},
60 {"cert", OPT_CERT
, '<', "Cert file to use, PEM format assumed"},
61 {"nameopt", OPT_NAMEOPT
, 's', "Various certificate name options"},
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"},
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"},
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"},
74 {"time", OPT_TIME
, 'p', "Seconds to collect data, default " SECONDSSTR
},
75 {"www", OPT_WWW
, 's', "Fetch specified page from the site"},
76 #ifndef OPENSSL_NO_SSL3
77 {"ssl3", OPT_SSL3
, '-', "Just use SSLv3"},
85 static double tm_Time_F(int s
)
87 return app_tminterval(s
, 1);
90 int s_time_main(int argc
, char **argv
)
95 const SSL_METHOD
*meth
= NULL
;
96 char *CApath
= NULL
, *CAfile
= NULL
, *cipher
= NULL
, *ciphersuites
= NULL
;
97 char *www_path
= NULL
;
98 char *host
= SSL_CONNECT_NAME
, *certfile
= NULL
, *keyfile
= NULL
, *prog
;
99 double totalTime
= 0.0;
100 int noCApath
= 0, noCAfile
= 0;
101 int maxtime
= SECONDS
, nConn
= 0, perform
= 3, ret
= 1, i
, st_bugs
= 0;
102 long bytes_read
= 0, finishtime
= 0;
104 int max_version
= 0, ver
, buf_len
;
107 meth
= TLS_client_method();
109 prog
= opt_init(argc
, argv
, s_time_options
);
110 while ((o
= opt_next()) != OPT_EOF
) {
115 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
118 opt_help(s_time_options
);
131 if (!opt_int(opt_arg(), &verify_args
.depth
))
133 BIO_printf(bio_err
, "%s: verify depth is %d\n",
134 prog
, verify_args
.depth
);
137 certfile
= opt_arg();
140 if (!set_nameopt(opt_arg()))
161 case OPT_CIPHERSUITES
:
162 ciphersuites
= opt_arg();
168 if (!opt_int(opt_arg(), &maxtime
))
172 www_path
= opt_arg();
173 buf_size
= strlen(www_path
) + fmt_http_get_cmd_size
;
174 if (buf_size
> sizeof(buf
)) {
175 BIO_printf(bio_err
, "%s: -www option is too long\n", prog
);
180 max_version
= SSL3_VERSION
;
184 argc
= opt_num_rest();
189 cipher
= getenv("SSL_CIPHER");
191 if ((ctx
= SSL_CTX_new(meth
)) == NULL
)
194 SSL_CTX_set_mode(ctx
, SSL_MODE_AUTO_RETRY
);
195 SSL_CTX_set_quiet_shutdown(ctx
, 1);
196 if (SSL_CTX_set_max_proto_version(ctx
, max_version
) == 0)
200 SSL_CTX_set_options(ctx
, SSL_OP_ALL
);
201 if (cipher
!= NULL
&& !SSL_CTX_set_cipher_list(ctx
, cipher
))
203 if (ciphersuites
!= NULL
&& !SSL_CTX_set_ciphersuites(ctx
, ciphersuites
))
205 if (!set_cert_stuff(ctx
, certfile
, keyfile
))
208 if (!ctx_set_verify_locations(ctx
, CAfile
, CApath
, noCAfile
, noCApath
)) {
209 ERR_print_errors(bio_err
);
214 printf("Collecting connection statistics for %d seconds\n", maxtime
);
216 /* Loop and time how long it takes to make connections */
219 finishtime
= (long)time(NULL
) + maxtime
;
222 if (finishtime
< (long)time(NULL
))
225 if ((scon
= doConnection(NULL
, host
, ctx
)) == NULL
)
228 if (www_path
!= NULL
) {
229 buf_len
= BIO_snprintf(buf
, sizeof(buf
), fmt_http_get_cmd
,
231 if (buf_len
<= 0 || SSL_write(scon
, buf
, buf_len
) <= 0)
233 while ((i
= SSL_read(scon
, buf
, sizeof(buf
))) > 0)
236 SSL_set_shutdown(scon
, SSL_SENT_SHUTDOWN
| SSL_RECEIVED_SHUTDOWN
);
237 BIO_closesocket(SSL_get_fd(scon
));
240 if (SSL_session_reused(scon
)) {
243 ver
= SSL_version(scon
);
244 if (ver
== TLS1_VERSION
)
246 else if (ver
== SSL3_VERSION
)
257 totalTime
+= tm_Time_F(STOP
); /* Add the time for this iteration */
259 i
= (int)((long)time(NULL
) - finishtime
+ maxtime
);
261 ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
262 nConn
, totalTime
, ((double)nConn
/ totalTime
), bytes_read
);
264 ("%d connections in %ld real seconds, %ld bytes read per connection\n",
265 nConn
, (long)time(NULL
) - finishtime
+ maxtime
, bytes_read
/ nConn
);
268 * Now loop and time connections using the same session id over and over
274 printf("\n\nNow timing with session id reuse.\n");
276 /* Get an SSL object so we can reuse the session id */
277 if ((scon
= doConnection(NULL
, host
, ctx
)) == NULL
) {
278 BIO_printf(bio_err
, "Unable to get connection\n");
282 if (www_path
!= NULL
) {
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)
286 while ((i
= SSL_read(scon
, buf
, sizeof(buf
))) > 0)
289 SSL_set_shutdown(scon
, SSL_SENT_SHUTDOWN
| SSL_RECEIVED_SHUTDOWN
);
290 BIO_closesocket(SSL_get_fd(scon
));
295 finishtime
= (long)time(NULL
) + maxtime
;
297 printf("starting\n");
302 if (finishtime
< (long)time(NULL
))
305 if ((doConnection(scon
, host
, ctx
)) == NULL
)
308 if (www_path
!= NULL
) {
309 buf_len
= BIO_snprintf(buf
, sizeof(buf
), fmt_http_get_cmd
,
311 if (buf_len
<= 0 || SSL_write(scon
, buf
, buf_len
) <= 0)
313 while ((i
= SSL_read(scon
, buf
, sizeof(buf
))) > 0)
316 SSL_set_shutdown(scon
, SSL_SENT_SHUTDOWN
| SSL_RECEIVED_SHUTDOWN
);
317 BIO_closesocket(SSL_get_fd(scon
));
320 if (SSL_session_reused(scon
)) {
323 ver
= SSL_version(scon
);
324 if (ver
== TLS1_VERSION
)
326 else if (ver
== SSL3_VERSION
)
334 totalTime
+= tm_Time_F(STOP
); /* Add the time for this iteration */
337 ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
338 nConn
, totalTime
, ((double)nConn
/ totalTime
), bytes_read
);
340 ("%d connections in %ld real seconds, %ld bytes read per connection\n",
341 nConn
, (long)time(NULL
) - finishtime
+ maxtime
, bytes_read
/ nConn
);
352 * doConnection - make a connection
354 static SSL
*doConnection(SSL
*scon
, const char *host
, SSL_CTX
*ctx
)
360 if ((conn
= BIO_new(BIO_s_connect())) == NULL
)
363 BIO_set_conn_hostname(conn
, host
);
364 BIO_set_conn_mode(conn
, BIO_SOCK_NODELAY
);
367 serverCon
= SSL_new(ctx
);
370 SSL_set_connect_state(serverCon
);
373 SSL_set_bio(serverCon
, conn
, conn
);
375 /* ok, lets connect */
376 i
= SSL_connect(serverCon
);
378 BIO_printf(bio_err
, "ERROR\n");
379 if (verify_args
.error
!= X509_V_OK
)
380 BIO_printf(bio_err
, "verify error:%s\n",
381 X509_verify_cert_error_string(verify_args
.error
));
383 ERR_print_errors(bio_err
);
389 #if defined(SOL_SOCKET) && defined(SO_LINGER)
391 struct linger no_linger
;
394 no_linger
.l_onoff
= 1;
395 no_linger
.l_linger
= 0;
396 fd
= SSL_get_fd(serverCon
);
398 (void)setsockopt(fd
, SOL_SOCKET
, SO_LINGER
, (char*)&no_linger
,
405 #endif /* OPENSSL_NO_SOCK */