Annotation of src/usr.bin/openssl/s_time.c, Revision 1.8
1.8 ! doug 1: /* $OpenBSD: s_time.c,v 1.7 2015/04/15 16:33:49 jsing Exp $ */
1.1 jsing 2: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3: * All rights reserved.
4: *
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.
8: *
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).
15: *
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.
22: *
23: * Redistribution and use in source and binary forms, with or without
24: * modification, are permitted provided that the following conditions
25: * are met:
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)"
40: *
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
51: * SUCH DAMAGE.
52: *
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.]
57: */
58:
59: #define NO_SHUTDOWN
60:
61: /*-----------------------------------------
62: s_time - SSL client connection timer program
63: Written and donated by Larry Streepy <streepy@healthcare.com>
64: -----------------------------------------*/
65:
1.3 deraadt 66: #include <sys/types.h>
1.1 jsing 67: #include <sys/socket.h>
68:
69: #include <stdio.h>
70: #include <stdlib.h>
71: #include <limits.h>
72: #include <string.h>
73: #include <unistd.h>
1.3 deraadt 74: #include <poll.h>
1.1 jsing 75:
76: #include "apps.h"
77:
78: #include <openssl/err.h>
79: #include <openssl/pem.h>
80: #include <openssl/ssl.h>
81: #include <openssl/x509.h>
82:
83: #include "s_apps.h"
84:
85: #define SSL_CONNECT_NAME "localhost:4433"
86:
87: #define BUFSIZZ 1024*10
88:
89: #define MYBUFSIZ 1024*8
90:
91: #undef min
92: #undef max
93: #define min(a,b) (((a) < (b)) ? (a) : (b))
94: #define max(a,b) (((a) > (b)) ? (a) : (b))
95:
96: #define SECONDS 30
1.7 jsing 97: extern int verify_depth;
98: extern int verify_error;
1.1 jsing 99:
100: static void s_time_usage(void);
101: static SSL *doConnection(SSL * scon);
102:
103: static SSL_CTX *tm_ctx = NULL;
104: static const SSL_METHOD *s_time_meth = NULL;
105: static long bytes_read = 0;
106:
1.5 jsing 107: struct {
108: int bugs;
109: char *CAfile;
110: char *CApath;
111: char *certfile;
112: char *cipher;
113: char *host;
114: char *keyfile;
115: int maxtime;
116: int nbio;
117: int perform;
118: int verify;
119: int verify_depth;
120: char *www_path;
121: } s_time_config;
122:
123: struct option s_time_options[] = {
124: {
125: .name = "bugs",
126: .desc = "Enable workarounds for known SSL/TLS bugs",
127: .type = OPTION_FLAG,
128: .opt.flag = &s_time_config.bugs,
129: },
130: {
131: .name = "CAfile",
132: .argname = "file",
133: .desc = "File containing trusted certificates in PEM format",
134: .type = OPTION_ARG,
135: .opt.arg = &s_time_config.CAfile,
136: },
137: {
138: .name = "CApath",
139: .argname = "path",
140: .desc = "Directory containing trusted certificates",
141: .type = OPTION_ARG,
142: .opt.arg = &s_time_config.CApath,
143: },
144: {
145: .name = "cert",
146: .argname = "file",
147: .desc = "Client certificate to use, if one is requested",
148: .type = OPTION_ARG,
149: .opt.arg = &s_time_config.certfile,
150: },
151: {
152: .name = "cipher",
153: .argname = "list",
154: .desc = "List of cipher suites to send to the server",
155: .type = OPTION_ARG,
156: .opt.arg = &s_time_config.cipher,
157: },
158: {
159: .name = "connect",
160: .argname = "host:port",
161: .desc = "Host and port to connect to (default "
162: SSL_CONNECT_NAME ")",
163: .type = OPTION_ARG,
164: .opt.arg = &s_time_config.host,
165: },
166: {
167: .name = "key",
168: .argname = "file",
169: .desc = "Client private key to use, if one is required",
170: .type = OPTION_ARG,
171: .opt.arg = &s_time_config.keyfile,
172: },
173: {
174: .name = "nbio",
175: .desc = "Use non-blocking I/O",
176: .type = OPTION_FLAG,
177: .opt.flag = &s_time_config.nbio,
178: },
179: {
180: .name = "new",
181: .desc = "Use a new session ID for each connection",
182: .type = OPTION_VALUE,
183: .opt.value = &s_time_config.perform,
184: .value = 1,
185: },
186: {
187: .name = "reuse",
188: .desc = "Reuse the same session ID for each connection",
189: .type = OPTION_VALUE,
190: .opt.value = &s_time_config.perform,
191: .value = 2,
192: },
193: {
194: .name = "time",
195: .argname = "seconds",
196: .desc = "Duration to perform timing tests for (default 30)",
197: .type = OPTION_ARG_INT,
198: .opt.value = &s_time_config.maxtime,
199: },
200: {
201: .name = "verify",
202: .argname = "depth",
203: .desc = "Enable peer certificate verification with given depth",
204: .type = OPTION_ARG_INT,
205: .opt.value = &s_time_config.verify_depth,
206: },
207: {
208: .name = "www",
209: .argname = "page",
210: .desc = "Page to GET from the server (default none)",
211: .type = OPTION_ARG,
212: .opt.arg = &s_time_config.www_path,
213: },
214: { NULL },
215: };
1.1 jsing 216:
217: static void
218: s_time_usage(void)
219: {
1.5 jsing 220: fprintf(stderr,
221: "usage: s_time "
222: "[-bugs] [-CAfile file] [-CApath directory] [-cert file]\n"
223: " [-cipher cipherlist] [-connect host:port] [-key keyfile]\n"
1.8 ! doug 224: " [-nbio] [-new] [-reuse] [-time seconds]\n"
1.5 jsing 225: " [-verify depth] [-www page]\n\n");
226: options_usage(s_time_options);
1.1 jsing 227: }
228:
229: /***********************************************************************
230: * TIME - time functions
231: */
232: #define START 0
233: #define STOP 1
234:
235: static double
236: tm_Time_F(int s)
237: {
238: return app_tminterval(s, 1);
239: }
240:
241: /***********************************************************************
242: * MAIN - main processing area for client
243: * real name depends on MONOLITH
244: */
245: int s_time_main(int, char **);
246:
247: int
248: s_time_main(int argc, char **argv)
249: {
250: double totalTime = 0.0;
251: int nConn = 0;
252: SSL *scon = NULL;
253: long finishtime = 0;
254: int ret = 1, i;
255: char buf[1024 * 8];
256: int ver;
1.8 ! doug 257:
1.5 jsing 258: s_time_meth = SSLv23_client_method();
1.1 jsing 259:
1.5 jsing 260: verify_depth = 0;
261: verify_error = X509_V_OK;
262:
263: memset(&s_time_config, 0, sizeof(s_time_config));
264:
265: s_time_config.host = SSL_CONNECT_NAME;
266: s_time_config.maxtime = SECONDS;
267: s_time_config.perform = 3;
268: s_time_config.verify = SSL_VERIFY_NONE;
269: s_time_config.verify_depth = -1;
1.1 jsing 270:
1.5 jsing 271: if (options_parse(argc, argv, s_time_options, NULL, NULL) != 0) {
272: s_time_usage();
273: goto end;
274: }
275:
276: if (s_time_config.verify_depth >= 0) {
277: s_time_config.verify = SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE;
278: verify_depth = s_time_config.verify_depth;
279: BIO_printf(bio_err, "verify depth is %d\n", verify_depth);
280: }
1.1 jsing 281:
1.5 jsing 282: if (s_time_config.www_path != NULL &&
283: strlen(s_time_config.www_path) > MYBUFSIZ - 100) {
284: BIO_printf(bio_err, "-www option too long\n");
1.1 jsing 285: goto end;
1.5 jsing 286: }
1.1 jsing 287:
288: if ((tm_ctx = SSL_CTX_new(s_time_meth)) == NULL)
289: return (1);
290:
291: SSL_CTX_set_quiet_shutdown(tm_ctx, 1);
292:
1.5 jsing 293: if (s_time_config.bugs)
1.1 jsing 294: SSL_CTX_set_options(tm_ctx, SSL_OP_ALL);
1.7 jsing 295:
296: if (s_time_config.cipher != NULL) {
297: if (!SSL_CTX_set_cipher_list(tm_ctx, s_time_config.cipher)) {
298: BIO_printf(bio_err, "error setting cipher list\n");
299: ERR_print_errors(bio_err);
300: goto end;
301: }
302: }
303:
1.5 jsing 304: if (!set_cert_stuff(tm_ctx, s_time_config.certfile,
305: s_time_config.keyfile))
1.1 jsing 306: goto end;
307:
1.5 jsing 308: if ((!SSL_CTX_load_verify_locations(tm_ctx, s_time_config.CAfile,
309: s_time_config.CApath)) ||
1.1 jsing 310: (!SSL_CTX_set_default_verify_paths(tm_ctx))) {
311: /*
312: * BIO_printf(bio_err,"error setting default verify
313: * locations\n");
314: */
315: ERR_print_errors(bio_err);
316: /* goto end; */
317: }
318:
1.5 jsing 319: if (!(s_time_config.perform & 1))
1.1 jsing 320: goto next;
1.5 jsing 321: printf("Collecting connection statistics for %d seconds\n",
322: s_time_config.maxtime);
1.1 jsing 323:
324: /* Loop and time how long it takes to make connections */
325:
326: bytes_read = 0;
1.5 jsing 327: finishtime = (long) time(NULL) + s_time_config.maxtime;
1.1 jsing 328: tm_Time_F(START);
329: for (;;) {
330: if (finishtime < (long) time(NULL))
331: break;
332: if ((scon = doConnection(NULL)) == NULL)
333: goto end;
334:
1.5 jsing 335: if (s_time_config.www_path != NULL) {
1.2 doug 336: int retval = snprintf(buf, sizeof buf,
1.5 jsing 337: "GET %s HTTP/1.0\r\n\r\n", s_time_config.www_path);
1.2 doug 338: if ((size_t)retval >= sizeof buf) {
1.1 jsing 339: fprintf(stderr, "URL too long\n");
340: goto end;
341: }
342: SSL_write(scon, buf, strlen(buf));
343: while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
344: bytes_read += i;
345: }
346: #ifdef NO_SHUTDOWN
347: SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
348: #else
349: SSL_shutdown(scon);
350: #endif
351: shutdown(SSL_get_fd(scon), SHUT_RDWR);
352: close(SSL_get_fd(scon));
353:
354: nConn += 1;
355: if (SSL_session_reused(scon))
356: ver = 'r';
357: else {
358: ver = SSL_version(scon);
359: if (ver == TLS1_VERSION)
360: ver = 't';
361: else if (ver == SSL3_VERSION)
362: ver = '3';
363: else if (ver == SSL2_VERSION)
364: ver = '2';
365: else
366: ver = '*';
367: }
368: fputc(ver, stdout);
369: fflush(stdout);
370:
371: SSL_free(scon);
372: scon = NULL;
373: }
374: totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
375:
1.5 jsing 376: i = (int) ((long) time(NULL) - finishtime + s_time_config.maxtime);
1.1 jsing 377: printf("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n", nConn, totalTime, ((double) nConn / totalTime), bytes_read);
1.5 jsing 378: printf("%d connections in %ld real seconds, %ld bytes read per connection\n", nConn, (long) time(NULL) - finishtime + s_time_config.maxtime, bytes_read / nConn);
1.1 jsing 379:
380: /*
381: * Now loop and time connections using the same session id over and
382: * over
383: */
384:
385: next:
1.5 jsing 386: if (!(s_time_config.perform & 2))
1.1 jsing 387: goto end;
388: printf("\n\nNow timing with session id reuse.\n");
389:
390: /* Get an SSL object so we can reuse the session id */
391: if ((scon = doConnection(NULL)) == NULL) {
392: fprintf(stderr, "Unable to get connection\n");
393: goto end;
394: }
1.5 jsing 395: if (s_time_config.www_path != NULL) {
1.2 doug 396: int retval = snprintf(buf, sizeof buf,
1.5 jsing 397: "GET %s HTTP/1.0\r\n\r\n", s_time_config.www_path);
1.2 doug 398: if ((size_t)retval >= sizeof buf) {
1.1 jsing 399: fprintf(stderr, "URL too long\n");
400: goto end;
401: }
402: SSL_write(scon, buf, strlen(buf));
403: while (SSL_read(scon, buf, sizeof(buf)) > 0);
404: }
405: #ifdef NO_SHUTDOWN
406: SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
407: #else
408: SSL_shutdown(scon);
409: #endif
410: shutdown(SSL_get_fd(scon), SHUT_RDWR);
411: close(SSL_get_fd(scon));
412:
413: nConn = 0;
414: totalTime = 0.0;
415:
1.5 jsing 416: finishtime = (long) time(NULL) + s_time_config.maxtime;
1.1 jsing 417:
418: printf("starting\n");
419: bytes_read = 0;
420: tm_Time_F(START);
421:
422: for (;;) {
423: if (finishtime < (long) time(NULL))
424: break;
425: if ((doConnection(scon)) == NULL)
426: goto end;
427:
1.5 jsing 428: if (s_time_config.www_path) {
1.2 doug 429: int retval = snprintf(buf, sizeof buf,
1.5 jsing 430: "GET %s HTTP/1.0\r\n\r\n", s_time_config.www_path);
1.2 doug 431: if ((size_t)retval >= sizeof buf) {
1.1 jsing 432: fprintf(stderr, "URL too long\n");
433: goto end;
434: }
435: SSL_write(scon, buf, strlen(buf));
436: while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
437: bytes_read += i;
438: }
439: #ifdef NO_SHUTDOWN
440: SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
441: #else
442: SSL_shutdown(scon);
443: #endif
444: shutdown(SSL_get_fd(scon), SHUT_RDWR);
445: close(SSL_get_fd(scon));
446:
447: nConn += 1;
448: if (SSL_session_reused(scon))
449: ver = 'r';
450: else {
451: ver = SSL_version(scon);
452: if (ver == TLS1_VERSION)
453: ver = 't';
454: else if (ver == SSL3_VERSION)
455: ver = '3';
456: else if (ver == SSL2_VERSION)
457: ver = '2';
458: else
459: ver = '*';
460: }
461: fputc(ver, stdout);
462: fflush(stdout);
463: }
464: totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
465:
466:
467: printf("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n", nConn, totalTime, ((double) nConn / totalTime), bytes_read);
1.5 jsing 468: printf("%d connections in %ld real seconds, %ld bytes read per connection\n", nConn, (long) time(NULL) - finishtime + s_time_config.maxtime, bytes_read / nConn);
1.1 jsing 469:
470: ret = 0;
471: end:
472: if (scon != NULL)
473: SSL_free(scon);
474:
475: if (tm_ctx != NULL) {
476: SSL_CTX_free(tm_ctx);
477: tm_ctx = NULL;
478: }
479:
480: return (ret);
481: }
482:
483: /***********************************************************************
484: * doConnection - make a connection
485: * Args:
486: * scon = earlier ssl connection for session id, or NULL
487: * Returns:
488: * SSL * = the connection pointer.
489: */
490: static SSL *
491: doConnection(SSL * scon)
492: {
1.3 deraadt 493: struct pollfd pfd[1];
494: SSL *serverCon;
1.1 jsing 495: BIO *conn;
1.3 deraadt 496: int i;
1.1 jsing 497:
498: if ((conn = BIO_new(BIO_s_connect())) == NULL)
499: return (NULL);
500:
501: /* BIO_set_conn_port(conn,port);*/
1.5 jsing 502: BIO_set_conn_hostname(conn, s_time_config.host);
1.1 jsing 503:
504: if (scon == NULL)
505: serverCon = SSL_new(tm_ctx);
506: else {
507: serverCon = scon;
508: SSL_set_connect_state(serverCon);
509: }
510:
511: SSL_set_bio(serverCon, conn, conn);
512:
513: /* ok, lets connect */
514: for (;;) {
515: i = SSL_connect(serverCon);
516: if (BIO_sock_should_retry(i)) {
517: BIO_printf(bio_err, "DELAY\n");
518:
519: i = SSL_get_fd(serverCon);
1.3 deraadt 520: pfd[0].fd = i;
521: pfd[0].events = POLLIN;
522: poll(pfd, 1, -1);
1.1 jsing 523: continue;
524: }
525: break;
526: }
527: if (i <= 0) {
528: BIO_printf(bio_err, "ERROR\n");
529: if (verify_error != X509_V_OK)
530: BIO_printf(bio_err, "verify error:%s\n",
531: X509_verify_cert_error_string(verify_error));
532: else
533: ERR_print_errors(bio_err);
534: if (scon == NULL)
535: SSL_free(serverCon);
536: return NULL;
537: }
538: return serverCon;
539: }