]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/s_time.c
Document the default CA path functions
[thirdparty/openssl.git] / apps / s_time.c
CommitLineData
58964a49 1/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
d02b48c6
RE
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
0f113f3e 7 *
d02b48c6
RE
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
0f113f3e 14 *
d02b48c6
RE
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
0f113f3e 21 *
d02b48c6
RE
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
0f113f3e 36 * 4. If you include any Windows specific code (or a derivative thereof) from
d02b48c6
RE
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
0f113f3e 39 *
d02b48c6
RE
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
0f113f3e 51 *
d02b48c6
RE
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57
58964a49 58#define NO_SHUTDOWN
d02b48c6 59
0f113f3e 60/* ----------------------------------------
b6cff93d 61 s_time - SSL client connection timer program
d02b48c6
RE
62 Written and donated by Larry Streepy <streepy@healthcare.com>
63 -----------------------------------------*/
64
65#include <stdio.h>
66#include <stdlib.h>
67#include <string.h>
68
a4a8f7b3
RL
69#define USE_SOCKETS
70#include "apps.h"
ec577822
BM
71#include <openssl/x509.h>
72#include <openssl/ssl.h>
73#include <openssl/pem.h>
d02b48c6 74#include "s_apps.h"
ec577822 75#include <openssl/err.h>
d02b48c6 76#ifdef WIN32_STUFF
0f113f3e
MC
77# include "winmain.h"
78# include "wintext.h"
d02b48c6 79#endif
f559f31b 80#if !defined(OPENSSL_SYS_MSDOS)
0f113f3e 81# include OPENSSL_UNISTD
f559f31b 82#endif
d02b48c6 83
7d7d2cbc 84#undef ioctl
d02b48c6
RE
85#define ioctl ioctlsocket
86
0f113f3e 87#define SSL_CONNECT_NAME "localhost:4433"
d02b48c6 88
e636e2ac 89/* no default cert. */
0f113f3e
MC
90/*
91 * #define TEST_CERT "client.pem"
92 */
d02b48c6
RE
93
94#undef BUFSIZZ
95#define BUFSIZZ 1024*10
96
54a656ef
BL
97#define MYBUFSIZ 1024*8
98
3e83e686
RL
99#undef min
100#undef max
d02b48c6
RE
101#define min(a,b) (((a) < (b)) ? (a) : (b))
102#define max(a,b) (((a) > (b)) ? (a) : (b))
103
104#undef SECONDS
0f113f3e 105#define SECONDS 30
7e1b7485
RS
106#define SECONDSSTR "30"
107
d02b48c6
RE
108extern int verify_depth;
109extern int verify_error;
110
7e1b7485 111static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx);
d02b48c6 112
7e1b7485
RS
113typedef enum OPTION_choice {
114 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
115 OPT_CONNECT, OPT_CIPHER, OPT_CERT, OPT_KEY, OPT_CAPATH,
116 OPT_CAFILE, OPT_NEW, OPT_REUSE, OPT_BUGS, OPT_VERIFY, OPT_TIME,
7e1b7485 117 OPT_SSL3,
7e1b7485
RS
118 OPT_WWW
119} OPTION_CHOICE;
120
121OPTIONS s_time_options[] = {
122 {"help", OPT_HELP, '-', "Display this summary"},
123 {"connect", OPT_CONNECT, 's',
124 "Where to connect as post:port (default is " SSL_CONNECT_NAME ")"},
125 {"cipher", OPT_CIPHER, 's', "Cipher to use, see 'openssl ciphers'"},
126 {"cert", OPT_CERT, '<', "Cert file to use, PEM format assumed"},
127 {"key", OPT_KEY, '<', "File with key, PEM; default is -cert file"},
128 {"CApath", OPT_CAPATH, '/', "PEM format directory of CA's"},
129 {"cafile", OPT_CAFILE, '<', "PEM format file of CA's"},
130 {"new", OPT_NEW, '-', "Just time new connections"},
131 {"reuse", OPT_REUSE, '-', "Just time connection reuse"},
132 {"bugs", OPT_BUGS, '-', "Turn on SSL bug compatibility"},
133 {"verify", OPT_VERIFY, 'p',
134 "Turn on peer certificate verification, set depth"},
135 {"time", OPT_TIME, 'p', "Sf seconds to collect data, default" SECONDSSTR},
136 {"www", OPT_WWW, 's', "Fetch specified page from the site"},
137#ifndef OPENSSL_NO_SSL3
138 {"ssl3", OPT_SSL3, '-', "Just use SSLv3"},
d02b48c6 139#endif
7e1b7485
RS
140 {NULL}
141};
d02b48c6 142
7e1b7485
RS
143#define START 0
144#define STOP 1
58964a49 145
7e1b7485 146static double tm_Time_F(int s)
d02b48c6 147{
7e1b7485 148 return app_tminterval(s, 1);
d02b48c6
RE
149}
150
7e1b7485 151int s_time_main(int argc, char **argv)
d02b48c6 152{
7e1b7485
RS
153 char buf[1024 * 8];
154 SSL *scon = NULL;
155 SSL_CTX *ctx = NULL;
156 const SSL_METHOD *meth = NULL;
157 char *CApath = NULL, *CAfile = NULL, *cipher = NULL, *www_path = NULL;
158 char *host = SSL_CONNECT_NAME, *certfile = NULL, *keyfile = NULL, *prog;
159 double totalTime = 0.0;
160 int maxtime = SECONDS, nConn = 0, perform = 3, ret = 1, i, st_bugs =
161 0, ver;
162 long bytes_read = 0, finishtime = 0;
163 OPTION_CHOICE o;
164#ifdef OPENSSL_SYS_WIN32
165 int exitNow = 0; /* Set when it's time to exit main */
166#endif
d02b48c6 167
13c9bb3e 168 meth = TLS_client_method();
0f113f3e
MC
169 verify_depth = 0;
170 verify_error = X509_V_OK;
d02b48c6 171
7e1b7485
RS
172 prog = opt_init(argc, argv, s_time_options);
173 while ((o = opt_next()) != OPT_EOF) {
174 switch (o) {
175 case OPT_EOF:
176 case OPT_ERR:
177 opthelp:
178 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
179 goto end;
180 case OPT_HELP:
181 opt_help(s_time_options);
182 ret = 0;
183 goto end;
184 case OPT_CONNECT:
185 host = opt_arg();
186 break;
187 case OPT_REUSE:
0f113f3e 188 perform = 2;
7e1b7485
RS
189 break;
190 case OPT_NEW:
0f113f3e 191 perform = 1;
7e1b7485
RS
192 break;
193 case OPT_VERIFY:
194 if (!opt_int(opt_arg(), &verify_depth))
195 goto opthelp;
196 BIO_printf(bio_err, "%s: verify depth is %d\n",
197 prog, verify_depth);
198 break;
199 case OPT_CERT:
200 certfile = opt_arg();
201 break;
202 case OPT_KEY:
203 keyfile = opt_arg();
204 break;
205 case OPT_CAPATH:
206 CApath = opt_arg();
207 break;
208 case OPT_CAFILE:
209 CAfile = opt_arg();
210 break;
211 case OPT_CIPHER:
212 cipher = opt_arg();
213 break;
214 case OPT_BUGS:
0f113f3e 215 st_bugs = 1;
7e1b7485
RS
216 break;
217 case OPT_TIME:
218 if (!opt_int(opt_arg(), &maxtime))
219 goto opthelp;
220 break;
221 case OPT_WWW:
222 www_path = opt_arg();
223 if (strlen(www_path) > MYBUFSIZ - 100) {
224 BIO_printf(bio_err, "%s: -www option too long\n", prog);
225 goto end;
dfef52f6 226 }
0f113f3e 227 break;
7e1b7485 228 case OPT_SSL3:
9c3bcfa0 229#ifndef OPENSSL_NO_SSL3
7e1b7485 230 meth = SSLv3_client_method();
7e1b7485 231#endif
9c3bcfa0 232 break;
0f113f3e 233 }
d02b48c6 234 }
7e1b7485
RS
235 argc = opt_num_rest();
236 argv = opt_rest();
d02b48c6 237
7e1b7485
RS
238 if (cipher == NULL)
239 cipher = getenv("SSL_CIPHER");
240 if (cipher == NULL) {
7768e116 241 BIO_printf(bio_err, "No CIPHER specified\n");
7e1b7485 242 goto end;
d02b48c6
RE
243 }
244
7e1b7485 245 if ((ctx = SSL_CTX_new(meth)) == NULL)
0f113f3e
MC
246 goto end;
247
7e1b7485 248 SSL_CTX_set_quiet_shutdown(ctx, 1);
0f113f3e
MC
249
250 if (st_bugs)
7e1b7485
RS
251 SSL_CTX_set_options(ctx, SSL_OP_ALL);
252 if (!SSL_CTX_set_cipher_list(ctx, cipher))
ac59d705 253 goto end;
7e1b7485 254 if (!set_cert_stuff(ctx, certfile, keyfile))
0f113f3e
MC
255 goto end;
256
7e1b7485 257 if (!ctx_set_verify_locations(ctx, CAfile, CApath)) {
0f113f3e 258 ERR_print_errors(bio_err);
7e1b7485 259 goto end;
0f113f3e 260 }
0f113f3e
MC
261 if (!(perform & 1))
262 goto next;
7e1b7485 263 printf("Collecting connection statistics for %d seconds\n", maxtime);
d02b48c6 264
0f113f3e 265 /* Loop and time how long it takes to make connections */
d02b48c6 266
0f113f3e 267 bytes_read = 0;
7e1b7485 268 finishtime = (long)time(NULL) + maxtime;
0f113f3e
MC
269 tm_Time_F(START);
270 for (;;) {
271 if (finishtime < (long)time(NULL))
272 break;
d02b48c6
RE
273#ifdef WIN32_STUFF
274
0f113f3e
MC
275 if (flushWinMsgs(0) == -1)
276 goto end;
d02b48c6 277
0f113f3e
MC
278 if (waitingToDie || exitNow) /* we're dead */
279 goto end;
d02b48c6
RE
280#endif
281
7e1b7485 282 if ((scon = doConnection(NULL, host, ctx)) == NULL)
0f113f3e 283 goto end;
d02b48c6 284
7e1b7485 285 if (www_path != NULL) {
0f113f3e 286 BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n",
7e1b7485 287 www_path);
61986d32 288 if (SSL_write(scon, buf, strlen(buf)) <= 0)
ac59d705 289 goto end;
0f113f3e
MC
290 while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
291 bytes_read += i;
292 }
d02b48c6 293#ifdef NO_SHUTDOWN
0f113f3e 294 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
d02b48c6 295#else
0f113f3e 296 SSL_shutdown(scon);
d02b48c6 297#endif
0f113f3e
MC
298 SHUTDOWN2(SSL_get_fd(scon));
299
300 nConn += 1;
301 if (SSL_session_reused(scon))
302 ver = 'r';
303 else {
304 ver = SSL_version(scon);
305 if (ver == TLS1_VERSION)
306 ver = 't';
307 else if (ver == SSL3_VERSION)
308 ver = '3';
309 else
310 ver = '*';
311 }
312 fputc(ver, stdout);
313 fflush(stdout);
314
315 SSL_free(scon);
316 scon = NULL;
317 }
318 totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
319
7e1b7485 320 i = (int)((long)time(NULL) - finishtime + maxtime);
0f113f3e
MC
321 printf
322 ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
323 nConn, totalTime, ((double)nConn / totalTime), bytes_read);
324 printf
325 ("%d connections in %ld real seconds, %ld bytes read per connection\n",
7e1b7485 326 nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
0f113f3e
MC
327
328 /*
329 * Now loop and time connections using the same session id over and over
330 */
331
332 next:
333 if (!(perform & 2))
334 goto end;
335 printf("\n\nNow timing with session id reuse.\n");
336
337 /* Get an SSL object so we can reuse the session id */
7e1b7485 338 if ((scon = doConnection(NULL, host, ctx)) == NULL) {
7768e116 339 BIO_printf(bio_err, "Unable to get connection\n");
0f113f3e
MC
340 goto end;
341 }
342
7e1b7485
RS
343 if (www_path != NULL) {
344 BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n", www_path);
61986d32 345 if (SSL_write(scon, buf, strlen(buf)) <= 0)
ac59d705 346 goto end;
7e1b7485
RS
347 while (SSL_read(scon, buf, sizeof(buf)) > 0)
348 continue;
0f113f3e 349 }
d02b48c6 350#ifdef NO_SHUTDOWN
0f113f3e 351 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
d02b48c6 352#else
0f113f3e 353 SSL_shutdown(scon);
d02b48c6 354#endif
0f113f3e
MC
355 SHUTDOWN2(SSL_get_fd(scon));
356
357 nConn = 0;
358 totalTime = 0.0;
d02b48c6 359
7e1b7485 360 finishtime = (long)time(NULL) + maxtime;
d02b48c6 361
0f113f3e
MC
362 printf("starting\n");
363 bytes_read = 0;
364 tm_Time_F(START);
d02b48c6 365
0f113f3e
MC
366 for (;;) {
367 if (finishtime < (long)time(NULL))
368 break;
d02b48c6
RE
369
370#ifdef WIN32_STUFF
0f113f3e
MC
371 if (flushWinMsgs(0) == -1)
372 goto end;
d02b48c6 373
0f113f3e
MC
374 if (waitingToDie || exitNow) /* we're dead */
375 goto end;
d02b48c6
RE
376#endif
377
7e1b7485 378 if ((doConnection(scon, host, ctx)) == NULL)
0f113f3e 379 goto end;
d02b48c6 380
7e1b7485 381 if (www_path) {
0f113f3e 382 BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n",
7e1b7485 383 www_path);
61986d32 384 if (SSL_write(scon, buf, strlen(buf)) <= 0)
ac59d705 385 goto end;
0f113f3e
MC
386 while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
387 bytes_read += i;
388 }
d02b48c6 389#ifdef NO_SHUTDOWN
0f113f3e 390 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
d02b48c6 391#else
0f113f3e 392 SSL_shutdown(scon);
d02b48c6 393#endif
0f113f3e
MC
394 SHUTDOWN2(SSL_get_fd(scon));
395
396 nConn += 1;
397 if (SSL_session_reused(scon))
398 ver = 'r';
399 else {
400 ver = SSL_version(scon);
401 if (ver == TLS1_VERSION)
402 ver = 't';
403 else if (ver == SSL3_VERSION)
404 ver = '3';
405 else
406 ver = '*';
407 }
408 fputc(ver, stdout);
409 fflush(stdout);
410 }
411 totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
412
413 printf
414 ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
415 nConn, totalTime, ((double)nConn / totalTime), bytes_read);
416 printf
417 ("%d connections in %ld real seconds, %ld bytes read per connection\n",
7e1b7485 418 nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
0f113f3e
MC
419
420 ret = 0;
7e1b7485 421
0f113f3e 422 end:
62adbcee 423 SSL_free(scon);
7e1b7485
RS
424 SSL_CTX_free(ctx);
425 return (ret);
0f113f3e 426}
d02b48c6 427
c80fd6b2 428/*-
d02b48c6 429 * doConnection - make a connection
d02b48c6 430 */
7e1b7485 431static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx)
0f113f3e
MC
432{
433 BIO *conn;
434 SSL *serverCon;
435 int width, i;
436 fd_set readfds;
437
438 if ((conn = BIO_new(BIO_s_connect())) == NULL)
439 return (NULL);
440
0f113f3e
MC
441 BIO_set_conn_hostname(conn, host);
442
443 if (scon == NULL)
7e1b7485 444 serverCon = SSL_new(ctx);
0f113f3e
MC
445 else {
446 serverCon = scon;
447 SSL_set_connect_state(serverCon);
448 }
d02b48c6 449
0f113f3e 450 SSL_set_bio(serverCon, conn, conn);
d02b48c6 451
0f113f3e
MC
452 /* ok, lets connect */
453 for (;;) {
454 i = SSL_connect(serverCon);
455 if (BIO_sock_should_retry(i)) {
456 BIO_printf(bio_err, "DELAY\n");
457
458 i = SSL_get_fd(serverCon);
459 width = i + 1;
460 FD_ZERO(&readfds);
461 openssl_fdset(i, &readfds);
462 /*
463 * Note: under VMS with SOCKETSHR the 2nd parameter is currently
464 * of type (int *) whereas under other systems it is (void *) if
465 * you don't have a cast it will choke the compiler: if you do
466 * have a cast then you can either go for (int *) or (void *).
467 */
468 select(width, (void *)&readfds, NULL, NULL, NULL);
469 continue;
470 }
471 break;
472 }
473 if (i <= 0) {
474 BIO_printf(bio_err, "ERROR\n");
475 if (verify_error != X509_V_OK)
476 BIO_printf(bio_err, "verify error:%s\n",
477 X509_verify_cert_error_string(verify_error));
478 else
479 ERR_print_errors(bio_err);
480 if (scon == NULL)
481 SSL_free(serverCon);
482 return NULL;
483 }
d02b48c6 484
0f113f3e
MC
485 return serverCon;
486}