1 /* $OpenBSD: s_time.c,v 1.17 2017/01/20 08:57:12 deraadt Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
59 /*-----------------------------------------
60 s_time - SSL client connection timer program
61 Written and donated by Larry Streepy <streepy@healthcare.com>
62 -----------------------------------------*/
64 #include <sys/types.h>
65 #include <sys/socket.h>
76 #include <openssl/err.h>
77 #include <openssl/pem.h>
78 #include <openssl/ssl.h>
79 #include <openssl/x509.h>
83 #define SSL_CONNECT_NAME "localhost:4433"
85 #define BUFSIZZ 1024*10
87 #define MYBUFSIZ 1024*8
90 extern int verify_depth
;
91 extern int verify_error
;
93 static void s_time_usage(void);
94 static SSL
*doConnection(SSL
* scon
);
96 static SSL_CTX
*tm_ctx
= NULL
;
97 static const SSL_METHOD
*s_time_meth
= NULL
;
98 static long bytes_read
= 0;
117 struct option s_time_options
[] = {
120 .desc
= "Enable workarounds for known SSL/TLS bugs",
122 .opt
.flag
= &s_time_config
.bugs
,
127 .desc
= "File containing trusted certificates in PEM format",
129 .opt
.arg
= &s_time_config
.CAfile
,
134 .desc
= "Directory containing trusted certificates",
136 .opt
.arg
= &s_time_config
.CApath
,
141 .desc
= "Client certificate to use, if one is requested",
143 .opt
.arg
= &s_time_config
.certfile
,
148 .desc
= "List of cipher suites to send to the server",
150 .opt
.arg
= &s_time_config
.cipher
,
154 .argname
= "host:port",
155 .desc
= "Host and port to connect to (default "
156 SSL_CONNECT_NAME
")",
158 .opt
.arg
= &s_time_config
.host
,
163 .desc
= "Client private key to use, if one is required",
165 .opt
.arg
= &s_time_config
.keyfile
,
169 .desc
= "Use non-blocking I/O",
171 .opt
.flag
= &s_time_config
.nbio
,
175 .desc
= "Use a new session ID for each connection",
176 .type
= OPTION_VALUE
,
177 .opt
.value
= &s_time_config
.perform
,
181 .name
= "no_shutdown",
182 .desc
= "Shut down the connection without notifying the server",
184 .opt
.flag
= &s_time_config
.no_shutdown
,
188 .desc
= "Reuse the same session ID for each connection",
189 .type
= OPTION_VALUE
,
190 .opt
.value
= &s_time_config
.perform
,
195 .argname
= "seconds",
196 .desc
= "Duration to perform timing tests for (default 30)",
197 .type
= OPTION_ARG_TIME
,
198 .opt
.tvalue
= &s_time_config
.maxtime
,
203 .desc
= "Enable peer certificate verification with given depth",
204 .type
= OPTION_ARG_INT
,
205 .opt
.value
= &s_time_config
.verify_depth
,
210 .desc
= "Page to GET from the server (default none)",
212 .opt
.arg
= &s_time_config
.www_path
,
222 "[-bugs] [-CAfile file] [-CApath directory] [-cert file]\n"
223 " [-cipher cipherlist] [-connect host:port] [-key keyfile]\n"
224 " [-nbio] [-new] [-no_shutdown] [-reuse] [-time seconds]\n"
225 " [-verify depth] [-www page]\n\n");
226 options_usage(s_time_options
);
229 /***********************************************************************
230 * TIME - time functions
238 return app_tminterval(s
, 1);
241 /***********************************************************************
242 * MAIN - main processing area for client
243 * real name depends on MONOLITH
246 s_time_main(int argc
, char **argv
)
248 double totalTime
= 0.0;
256 if (single_execution
) {
257 if (pledge("stdio rpath inet", NULL
) == -1) {
263 s_time_meth
= SSLv23_client_method();
267 memset(&s_time_config
, 0, sizeof(s_time_config
));
269 s_time_config
.host
= SSL_CONNECT_NAME
;
270 s_time_config
.maxtime
= SECONDS
;
271 s_time_config
.perform
= 3;
272 s_time_config
.verify
= SSL_VERIFY_NONE
;
273 s_time_config
.verify_depth
= -1;
275 if (options_parse(argc
, argv
, s_time_options
, NULL
, NULL
) != 0) {
280 if (s_time_config
.verify_depth
>= 0) {
281 s_time_config
.verify
= SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
;
282 verify_depth
= s_time_config
.verify_depth
;
283 BIO_printf(bio_err
, "verify depth is %d\n", verify_depth
);
286 if (s_time_config
.www_path
!= NULL
&&
287 strlen(s_time_config
.www_path
) > MYBUFSIZ
- 100) {
288 BIO_printf(bio_err
, "-www option too long\n");
292 if ((tm_ctx
= SSL_CTX_new(s_time_meth
)) == NULL
)
295 SSL_CTX_set_quiet_shutdown(tm_ctx
, 1);
297 if (s_time_config
.bugs
)
298 SSL_CTX_set_options(tm_ctx
, SSL_OP_ALL
);
300 if (s_time_config
.cipher
!= NULL
) {
301 if (!SSL_CTX_set_cipher_list(tm_ctx
, s_time_config
.cipher
)) {
302 BIO_printf(bio_err
, "error setting cipher list\n");
303 ERR_print_errors(bio_err
);
308 SSL_CTX_set_verify(tm_ctx
, s_time_config
.verify
, NULL
);
310 if (!set_cert_stuff(tm_ctx
, s_time_config
.certfile
,
311 s_time_config
.keyfile
))
314 if ((!SSL_CTX_load_verify_locations(tm_ctx
, s_time_config
.CAfile
,
315 s_time_config
.CApath
)) ||
316 (!SSL_CTX_set_default_verify_paths(tm_ctx
))) {
318 * BIO_printf(bio_err,"error setting default verify
321 ERR_print_errors(bio_err
);
325 if (!(s_time_config
.perform
& 1))
327 printf("Collecting connection statistics for %lld seconds\n",
328 (long long)s_time_config
.maxtime
);
330 /* Loop and time how long it takes to make connections */
333 finishtime
= time(NULL
) + s_time_config
.maxtime
;
336 if (finishtime
< time(NULL
))
338 if ((scon
= doConnection(NULL
)) == NULL
)
341 if (s_time_config
.www_path
!= NULL
) {
342 int i
, retval
= snprintf(buf
, sizeof buf
,
343 "GET %s HTTP/1.0\r\n\r\n", s_time_config
.www_path
);
344 if ((size_t)retval
>= sizeof buf
) {
345 fprintf(stderr
, "URL too long\n");
348 SSL_write(scon
, buf
, strlen(buf
));
349 while ((i
= SSL_read(scon
, buf
, sizeof(buf
))) > 0)
352 if (s_time_config
.no_shutdown
)
353 SSL_set_shutdown(scon
, SSL_SENT_SHUTDOWN
|
354 SSL_RECEIVED_SHUTDOWN
);
357 shutdown(SSL_get_fd(scon
), SHUT_RDWR
);
358 close(SSL_get_fd(scon
));
361 if (SSL_session_reused(scon
))
364 ver
= SSL_version(scon
);
365 if (ver
== TLS1_VERSION
)
367 else if (ver
== SSL3_VERSION
)
369 else if (ver
== SSL2_VERSION
)
380 totalTime
+= tm_Time_F(STOP
); /* Add the time for this iteration */
382 printf("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
383 nConn
, totalTime
, ((double) nConn
/ totalTime
), bytes_read
);
384 printf("%d connections in %lld real seconds, %ld bytes read per connection\n",
386 (long long)(time(NULL
) - finishtime
+ s_time_config
.maxtime
),
390 * Now loop and time connections using the same session id over and
395 if (!(s_time_config
.perform
& 2))
397 printf("\n\nNow timing with session id reuse.\n");
399 /* Get an SSL object so we can reuse the session id */
400 if ((scon
= doConnection(NULL
)) == NULL
) {
401 fprintf(stderr
, "Unable to get connection\n");
404 if (s_time_config
.www_path
!= NULL
) {
405 int retval
= snprintf(buf
, sizeof buf
,
406 "GET %s HTTP/1.0\r\n\r\n", s_time_config
.www_path
);
407 if ((size_t)retval
>= sizeof buf
) {
408 fprintf(stderr
, "URL too long\n");
411 SSL_write(scon
, buf
, strlen(buf
));
412 while (SSL_read(scon
, buf
, sizeof(buf
)) > 0);
414 if (s_time_config
.no_shutdown
)
415 SSL_set_shutdown(scon
, SSL_SENT_SHUTDOWN
|
416 SSL_RECEIVED_SHUTDOWN
);
419 shutdown(SSL_get_fd(scon
), SHUT_RDWR
);
420 close(SSL_get_fd(scon
));
425 finishtime
= time(NULL
) + s_time_config
.maxtime
;
427 printf("starting\n");
432 if (finishtime
< time(NULL
))
434 if ((doConnection(scon
)) == NULL
)
437 if (s_time_config
.www_path
) {
438 int i
, retval
= snprintf(buf
, sizeof buf
,
439 "GET %s HTTP/1.0\r\n\r\n", s_time_config
.www_path
);
440 if ((size_t)retval
>= sizeof buf
) {
441 fprintf(stderr
, "URL too long\n");
444 SSL_write(scon
, buf
, strlen(buf
));
445 while ((i
= SSL_read(scon
, buf
, sizeof(buf
))) > 0)
448 if (s_time_config
.no_shutdown
)
449 SSL_set_shutdown(scon
, SSL_SENT_SHUTDOWN
|
450 SSL_RECEIVED_SHUTDOWN
);
453 shutdown(SSL_get_fd(scon
), SHUT_RDWR
);
454 close(SSL_get_fd(scon
));
457 if (SSL_session_reused(scon
))
460 ver
= SSL_version(scon
);
461 if (ver
== TLS1_VERSION
)
463 else if (ver
== SSL3_VERSION
)
465 else if (ver
== SSL2_VERSION
)
473 totalTime
+= tm_Time_F(STOP
); /* Add the time for this iteration */
475 printf("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n", nConn
, totalTime
, ((double) nConn
/ totalTime
), bytes_read
);
476 printf("%d connections in %lld real seconds, %ld bytes read per connection\n",
478 (long long)(time(NULL
) - finishtime
+ s_time_config
.maxtime
),
486 if (tm_ctx
!= NULL
) {
487 SSL_CTX_free(tm_ctx
);
494 /***********************************************************************
495 * doConnection - make a connection
497 * scon = earlier ssl connection for session id, or NULL
499 * SSL * = the connection pointer.
502 doConnection(SSL
* scon
)
504 struct pollfd pfd
[1];
510 if ((conn
= BIO_new(BIO_s_connect())) == NULL
)
513 /* BIO_set_conn_port(conn,port);*/
514 BIO_set_conn_hostname(conn
, s_time_config
.host
);
517 serverCon
= SSL_new(tm_ctx
);
520 SSL_set_connect_state(serverCon
);
523 SSL_set_bio(serverCon
, conn
, conn
);
525 /* ok, lets connect */
527 i
= SSL_connect(serverCon
);
528 if (BIO_sock_should_retry(i
)) {
529 BIO_printf(bio_err
, "DELAY\n");
531 i
= SSL_get_fd(serverCon
);
533 pfd
[0].events
= POLLIN
;
540 BIO_printf(bio_err
, "ERROR\n");
541 verify_error
= SSL_get_verify_result(serverCon
);
542 if (verify_error
!= X509_V_OK
)
543 BIO_printf(bio_err
, "verify error:%s\n",
544 X509_verify_cert_error_string(verify_error
));
546 ERR_print_errors(bio_err
);