]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/s_time.c
Big apps cleanup (option-parsing, etc)
[thirdparty/openssl.git] / apps / s_time.c
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
7 *
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).
14 *
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.
21 *
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 :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
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.
51 *
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
58 #define NO_SHUTDOWN
59
60 /* ----------------------------------------
61 s_time - SSL client connection timer program
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
69 #define USE_SOCKETS
70 #include "apps.h"
71 #include <openssl/x509.h>
72 #include <openssl/ssl.h>
73 #include <openssl/pem.h>
74 #include "s_apps.h"
75 #include <openssl/err.h>
76 #ifdef WIN32_STUFF
77 # include "winmain.h"
78 # include "wintext.h"
79 #endif
80 #if !defined(OPENSSL_SYS_MSDOS)
81 # include OPENSSL_UNISTD
82 #endif
83
84 #undef ioctl
85 #define ioctl ioctlsocket
86
87 #define SSL_CONNECT_NAME "localhost:4433"
88
89 /* no default cert. */
90 /*
91 * #define TEST_CERT "client.pem"
92 */
93
94 #undef BUFSIZZ
95 #define BUFSIZZ 1024*10
96
97 #define MYBUFSIZ 1024*8
98
99 #undef min
100 #undef max
101 #define min(a,b) (((a) < (b)) ? (a) : (b))
102 #define max(a,b) (((a) > (b)) ? (a) : (b))
103
104 #undef SECONDS
105 #define SECONDS 30
106 #define SECONDSSTR "30"
107
108 extern int verify_depth;
109 extern int verify_error;
110
111 static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx);
112
113 typedef 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,
117 #ifndef OPENSSL_NO_SSL3
118 OPT_SSL3,
119 #endif
120 OPT_WWW
121 } OPTION_CHOICE;
122
123 OPTIONS s_time_options[] = {
124 {"help", OPT_HELP, '-', "Display this summary"},
125 {"connect", OPT_CONNECT, 's',
126 "Where to connect as post:port (default is " SSL_CONNECT_NAME ")"},
127 {"cipher", OPT_CIPHER, 's', "Cipher to use, see 'openssl ciphers'"},
128 {"cert", OPT_CERT, '<', "Cert file to use, PEM format assumed"},
129 {"key", OPT_KEY, '<', "File with key, PEM; default is -cert file"},
130 {"CApath", OPT_CAPATH, '/', "PEM format directory of CA's"},
131 {"cafile", OPT_CAFILE, '<', "PEM format file of CA's"},
132 {"new", OPT_NEW, '-', "Just time new connections"},
133 {"reuse", OPT_REUSE, '-', "Just time connection reuse"},
134 {"bugs", OPT_BUGS, '-', "Turn on SSL bug compatibility"},
135 {"verify", OPT_VERIFY, 'p',
136 "Turn on peer certificate verification, set depth"},
137 {"time", OPT_TIME, 'p', "Sf seconds to collect data, default" SECONDSSTR},
138 {"www", OPT_WWW, 's', "Fetch specified page from the site"},
139 #ifndef OPENSSL_NO_SSL3
140 {"ssl3", OPT_SSL3, '-', "Just use SSLv3"},
141 #endif
142 {NULL}
143 };
144
145 #define START 0
146 #define STOP 1
147
148 static double tm_Time_F(int s)
149 {
150 return app_tminterval(s, 1);
151 }
152
153 int s_time_main(int argc, char **argv)
154 {
155 char buf[1024 * 8];
156 SSL *scon = NULL;
157 SSL_CTX *ctx = NULL;
158 const SSL_METHOD *meth = NULL;
159 char *CApath = NULL, *CAfile = NULL, *cipher = NULL, *www_path = NULL;
160 char *host = SSL_CONNECT_NAME, *certfile = NULL, *keyfile = NULL, *prog;
161 double totalTime = 0.0;
162 int maxtime = SECONDS, nConn = 0, perform = 3, ret = 1, i, st_bugs =
163 0, ver;
164 long bytes_read = 0, finishtime = 0;
165 OPTION_CHOICE o;
166 #ifdef OPENSSL_SYS_WIN32
167 int exitNow = 0; /* Set when it's time to exit main */
168 #endif
169
170 meth = SSLv23_client_method();
171 verify_depth = 0;
172 verify_error = X509_V_OK;
173
174 prog = opt_init(argc, argv, s_time_options);
175 while ((o = opt_next()) != OPT_EOF) {
176 switch (o) {
177 case OPT_EOF:
178 case OPT_ERR:
179 opthelp:
180 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
181 goto end;
182 case OPT_HELP:
183 opt_help(s_time_options);
184 ret = 0;
185 goto end;
186 case OPT_CONNECT:
187 host = opt_arg();
188 break;
189 case OPT_REUSE:
190 perform = 2;
191 break;
192 case OPT_NEW:
193 perform = 1;
194 break;
195 case OPT_VERIFY:
196 if (!opt_int(opt_arg(), &verify_depth))
197 goto opthelp;
198 BIO_printf(bio_err, "%s: verify depth is %d\n",
199 prog, verify_depth);
200 break;
201 case OPT_CERT:
202 certfile = opt_arg();
203 break;
204 case OPT_KEY:
205 keyfile = opt_arg();
206 break;
207 case OPT_CAPATH:
208 CApath = opt_arg();
209 break;
210 case OPT_CAFILE:
211 CAfile = opt_arg();
212 break;
213 case OPT_CIPHER:
214 cipher = opt_arg();
215 break;
216 case OPT_BUGS:
217 st_bugs = 1;
218 break;
219 case OPT_TIME:
220 if (!opt_int(opt_arg(), &maxtime))
221 goto opthelp;
222 break;
223 case OPT_WWW:
224 www_path = opt_arg();
225 if (strlen(www_path) > MYBUFSIZ - 100) {
226 BIO_printf(bio_err, "%s: -www option too long\n", prog);
227 goto end;
228 }
229 break;
230 #ifndef OPENSSL_NO_SSL3
231 case OPT_SSL3:
232 meth = SSLv3_client_method();
233 break;
234 #endif
235 }
236 }
237 argc = opt_num_rest();
238 argv = opt_rest();
239
240 if (cipher == NULL)
241 cipher = getenv("SSL_CIPHER");
242 if (cipher == NULL) {
243 fprintf(stderr, "No CIPHER specified\n");
244 goto end;
245 }
246
247 if ((ctx = SSL_CTX_new(meth)) == NULL)
248 goto end;
249
250 SSL_CTX_set_quiet_shutdown(ctx, 1);
251
252 if (st_bugs)
253 SSL_CTX_set_options(ctx, SSL_OP_ALL);
254 if (!SSL_CTX_set_cipher_list(ctx, cipher))
255 goto end;
256 if (!set_cert_stuff(ctx, certfile, keyfile))
257 goto end;
258
259 if (!ctx_set_verify_locations(ctx, CAfile, CApath)) {
260 ERR_print_errors(bio_err);
261 goto end;
262 }
263 if (!(perform & 1))
264 goto next;
265 printf("Collecting connection statistics for %d seconds\n", maxtime);
266
267 /* Loop and time how long it takes to make connections */
268
269 bytes_read = 0;
270 finishtime = (long)time(NULL) + maxtime;
271 tm_Time_F(START);
272 for (;;) {
273 if (finishtime < (long)time(NULL))
274 break;
275 #ifdef WIN32_STUFF
276
277 if (flushWinMsgs(0) == -1)
278 goto end;
279
280 if (waitingToDie || exitNow) /* we're dead */
281 goto end;
282 #endif
283
284 if ((scon = doConnection(NULL, host, ctx)) == NULL)
285 goto end;
286
287 if (www_path != NULL) {
288 BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n",
289 www_path);
290 if (SSL_write(scon, buf, strlen(buf)) <= 0)
291 goto end;
292 while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
293 bytes_read += i;
294 }
295 #ifdef NO_SHUTDOWN
296 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
297 #else
298 SSL_shutdown(scon);
299 #endif
300 SHUTDOWN2(SSL_get_fd(scon));
301
302 nConn += 1;
303 if (SSL_session_reused(scon))
304 ver = 'r';
305 else {
306 ver = SSL_version(scon);
307 if (ver == TLS1_VERSION)
308 ver = 't';
309 else if (ver == SSL3_VERSION)
310 ver = '3';
311 else
312 ver = '*';
313 }
314 fputc(ver, stdout);
315 fflush(stdout);
316
317 SSL_free(scon);
318 scon = NULL;
319 }
320 totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
321
322 i = (int)((long)time(NULL) - finishtime + maxtime);
323 printf
324 ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
325 nConn, totalTime, ((double)nConn / totalTime), bytes_read);
326 printf
327 ("%d connections in %ld real seconds, %ld bytes read per connection\n",
328 nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
329
330 /*
331 * Now loop and time connections using the same session id over and over
332 */
333
334 next:
335 if (!(perform & 2))
336 goto end;
337 printf("\n\nNow timing with session id reuse.\n");
338
339 /* Get an SSL object so we can reuse the session id */
340 if ((scon = doConnection(NULL, host, ctx)) == NULL) {
341 fprintf(stderr, "Unable to get connection\n");
342 goto end;
343 }
344
345 if (www_path != NULL) {
346 BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n", www_path);
347 if (SSL_write(scon, buf, strlen(buf)) <= 0)
348 goto end;
349 while (SSL_read(scon, buf, sizeof(buf)) > 0)
350 continue;
351 }
352 #ifdef NO_SHUTDOWN
353 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
354 #else
355 SSL_shutdown(scon);
356 #endif
357 SHUTDOWN2(SSL_get_fd(scon));
358
359 nConn = 0;
360 totalTime = 0.0;
361
362 finishtime = (long)time(NULL) + maxtime;
363
364 printf("starting\n");
365 bytes_read = 0;
366 tm_Time_F(START);
367
368 for (;;) {
369 if (finishtime < (long)time(NULL))
370 break;
371
372 #ifdef WIN32_STUFF
373 if (flushWinMsgs(0) == -1)
374 goto end;
375
376 if (waitingToDie || exitNow) /* we're dead */
377 goto end;
378 #endif
379
380 if ((doConnection(scon, host, ctx)) == NULL)
381 goto end;
382
383 if (www_path) {
384 BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n",
385 www_path);
386 if (SSL_write(scon, buf, strlen(buf)) <= 0)
387 goto end;
388 while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
389 bytes_read += i;
390 }
391 #ifdef NO_SHUTDOWN
392 SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
393 #else
394 SSL_shutdown(scon);
395 #endif
396 SHUTDOWN2(SSL_get_fd(scon));
397
398 nConn += 1;
399 if (SSL_session_reused(scon))
400 ver = 'r';
401 else {
402 ver = SSL_version(scon);
403 if (ver == TLS1_VERSION)
404 ver = 't';
405 else if (ver == SSL3_VERSION)
406 ver = '3';
407 else
408 ver = '*';
409 }
410 fputc(ver, stdout);
411 fflush(stdout);
412 }
413 totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
414
415 printf
416 ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
417 nConn, totalTime, ((double)nConn / totalTime), bytes_read);
418 printf
419 ("%d connections in %ld real seconds, %ld bytes read per connection\n",
420 nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
421
422 ret = 0;
423
424 end:
425 SSL_free(scon);
426 SSL_CTX_free(ctx);
427 return (ret);
428 }
429
430 /*-
431 * doConnection - make a connection
432 */
433 static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx)
434 {
435 BIO *conn;
436 SSL *serverCon;
437 int width, i;
438 fd_set readfds;
439
440 if ((conn = BIO_new(BIO_s_connect())) == NULL)
441 return (NULL);
442
443 BIO_set_conn_hostname(conn, host);
444
445 if (scon == NULL)
446 serverCon = SSL_new(ctx);
447 else {
448 serverCon = scon;
449 SSL_set_connect_state(serverCon);
450 }
451
452 SSL_set_bio(serverCon, conn, conn);
453
454 /* ok, lets connect */
455 for (;;) {
456 i = SSL_connect(serverCon);
457 if (BIO_sock_should_retry(i)) {
458 BIO_printf(bio_err, "DELAY\n");
459
460 i = SSL_get_fd(serverCon);
461 width = i + 1;
462 FD_ZERO(&readfds);
463 openssl_fdset(i, &readfds);
464 /*
465 * Note: under VMS with SOCKETSHR the 2nd parameter is currently
466 * of type (int *) whereas under other systems it is (void *) if
467 * you don't have a cast it will choke the compiler: if you do
468 * have a cast then you can either go for (int *) or (void *).
469 */
470 select(width, (void *)&readfds, NULL, NULL, NULL);
471 continue;
472 }
473 break;
474 }
475 if (i <= 0) {
476 BIO_printf(bio_err, "ERROR\n");
477 if (verify_error != X509_V_OK)
478 BIO_printf(bio_err, "verify error:%s\n",
479 X509_verify_cert_error_string(verify_error));
480 else
481 ERR_print_errors(bio_err);
482 if (scon == NULL)
483 SSL_free(serverCon);
484 return NULL;
485 }
486
487 return serverCon;
488 }