s_time.c 14.6 KB
Newer Older
1
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 3 4 5 6
 * All rights reserved.
 *
 * This package is an SSL implementation written
 * by Eric Young (eay@cryptsoft.com).
 * The implementation was written so as to conform with Netscapes SSL.
7
 *
8 9 10 11 12 13
 * This library is free for commercial and non-commercial use as long as
 * the following conditions are aheared to.  The following conditions
 * apply to all code found in this distribution, be it the RC4, RSA,
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
 * included with this distribution is covered by the same copyright terms
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14
 *
15 16 17 18 19 20
 * Copyright remains Eric Young's, and as such any Copyright notices in
 * the code are not to be removed.
 * If this package is used in a product, Eric Young should be given attribution
 * as the author of the parts of the library used.
 * This can be in the form of a textual message at program startup or
 * in documentation (online or textual) provided with the package.
21
 *
22 23 24 25 26 27 28 29 30 31 32 33 34 35
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    "This product includes cryptographic software written by
 *     Eric Young (eay@cryptsoft.com)"
 *    The word 'cryptographic' can be left out if the rouines from the library
 *    being used are not cryptographic related :-).
36
 * 4. If you include any Windows specific code (or a derivative thereof) from
37 38
 *    the apps directory (application code) you must include an acknowledgement:
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39
 *
40 41 42 43 44 45 46 47 48 49 50
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
51
 *
52 53 54 55 56 57
 * The licence and distribution terms for any publically available version or
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
 * copied and put under another distribution licence
 * [including the GNU Public Licence.]
 */

58
#define NO_SHUTDOWN
59

60
/* ----------------------------------------
61
   s_time - SSL client connection timer program
62 63 64 65 66 67 68
   Written and donated by Larry Streepy <streepy@healthcare.com>
  -----------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

69 70
#define USE_SOCKETS
#include "apps.h"
71 72 73
#include <openssl/x509.h>
#include <openssl/ssl.h>
#include <openssl/pem.h>
74
#include "s_apps.h"
75
#include <openssl/err.h>
76
#ifdef WIN32_STUFF
77 78
# include "winmain.h"
# include "wintext.h"
79
#endif
80
#if !defined(OPENSSL_SYS_MSDOS)
81
# include OPENSSL_UNISTD
82
#endif
83

U
Ulf Möller 已提交
84
#undef ioctl
85 86
#define ioctl ioctlsocket

87
#define SSL_CONNECT_NAME        "localhost:4433"
88

89
/* no default cert. */
90 91 92
/*
 * #define TEST_CERT "client.pem"
 */
93 94 95 96

#undef BUFSIZZ
#define BUFSIZZ 1024*10

97 98
#define MYBUFSIZ 1024*8

99 100
#undef min
#undef max
101 102 103 104
#define min(a,b) (((a) < (b)) ? (a) : (b))
#define max(a,b) (((a) > (b)) ? (a) : (b))

#undef SECONDS
105
#define SECONDS 30
106 107
#define SECONDSSTR "30"

108 109 110
extern int verify_depth;
extern int verify_error;

111
static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx);
112

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
typedef enum OPTION_choice {
    OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
    OPT_CONNECT, OPT_CIPHER, OPT_CERT, OPT_KEY, OPT_CAPATH,
    OPT_CAFILE, OPT_NEW, OPT_REUSE, OPT_BUGS, OPT_VERIFY, OPT_TIME,
    OPT_SSL3,
    OPT_WWW
} OPTION_CHOICE;

OPTIONS s_time_options[] = {
    {"help", OPT_HELP, '-', "Display this summary"},
    {"connect", OPT_CONNECT, 's',
     "Where to connect as post:port (default is " SSL_CONNECT_NAME ")"},
    {"cipher", OPT_CIPHER, 's', "Cipher to use, see 'openssl ciphers'"},
    {"cert", OPT_CERT, '<', "Cert file to use, PEM format assumed"},
    {"key", OPT_KEY, '<', "File with key, PEM; default is -cert file"},
    {"CApath", OPT_CAPATH, '/', "PEM format directory of CA's"},
    {"cafile", OPT_CAFILE, '<', "PEM format file of CA's"},
    {"new", OPT_NEW, '-', "Just time new connections"},
    {"reuse", OPT_REUSE, '-', "Just time connection reuse"},
    {"bugs", OPT_BUGS, '-', "Turn on SSL bug compatibility"},
    {"verify", OPT_VERIFY, 'p',
     "Turn on peer certificate verification, set depth"},
    {"time", OPT_TIME, 'p', "Sf seconds to collect data, default" SECONDSSTR},
    {"www", OPT_WWW, 's', "Fetch specified page from the site"},
#ifndef OPENSSL_NO_SSL3
    {"ssl3", OPT_SSL3, '-', "Just use SSLv3"},
139
#endif
140 141
    {NULL}
};
142

143 144
#define START   0
#define STOP    1
145

146
static double tm_Time_F(int s)
147
{
148
    return app_tminterval(s, 1);
149 150
}

151
int s_time_main(int argc, char **argv)
152
{
153 154 155 156 157 158 159 160 161 162 163 164 165 166
    char buf[1024 * 8];
    SSL *scon = NULL;
    SSL_CTX *ctx = NULL;
    const SSL_METHOD *meth = NULL;
    char *CApath = NULL, *CAfile = NULL, *cipher = NULL, *www_path = NULL;
    char *host = SSL_CONNECT_NAME, *certfile = NULL, *keyfile = NULL, *prog;
    double totalTime = 0.0;
    int maxtime = SECONDS, nConn = 0, perform = 3, ret = 1, i, st_bugs =
        0, ver;
    long bytes_read = 0, finishtime = 0;
    OPTION_CHOICE o;
#ifdef OPENSSL_SYS_WIN32
    int exitNow = 0;            /* Set when it's time to exit main */
#endif
167

168
    meth = TLS_client_method();
169 170
    verify_depth = 0;
    verify_error = X509_V_OK;
171

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    prog = opt_init(argc, argv, s_time_options);
    while ((o = opt_next()) != OPT_EOF) {
        switch (o) {
        case OPT_EOF:
        case OPT_ERR:
 opthelp:
            BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
            goto end;
        case OPT_HELP:
            opt_help(s_time_options);
            ret = 0;
            goto end;
        case OPT_CONNECT:
            host = opt_arg();
            break;
        case OPT_REUSE:
188
            perform = 2;
189 190
            break;
        case OPT_NEW:
191
            perform = 1;
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
            break;
        case OPT_VERIFY:
            if (!opt_int(opt_arg(), &verify_depth))
                goto opthelp;
            BIO_printf(bio_err, "%s: verify depth is %d\n",
                       prog, verify_depth);
            break;
        case OPT_CERT:
            certfile = opt_arg();
            break;
        case OPT_KEY:
            keyfile = opt_arg();
            break;
        case OPT_CAPATH:
            CApath = opt_arg();
            break;
        case OPT_CAFILE:
            CAfile = opt_arg();
            break;
        case OPT_CIPHER:
            cipher = opt_arg();
            break;
        case OPT_BUGS:
215
            st_bugs = 1;
216 217 218 219 220 221 222 223 224 225
            break;
        case OPT_TIME:
            if (!opt_int(opt_arg(), &maxtime))
                goto opthelp;
            break;
        case OPT_WWW:
            www_path = opt_arg();
            if (strlen(www_path) > MYBUFSIZ - 100) {
                BIO_printf(bio_err, "%s: -www option too long\n", prog);
                goto end;
M
Matt Caswell 已提交
226
            }
227
            break;
228
        case OPT_SSL3:
229
#ifndef OPENSSL_NO_SSL3
230 231
            meth = SSLv3_client_method();
#endif
232
            break;
233
        }
234
    }
235 236
    argc = opt_num_rest();
    argv = opt_rest();
237

238 239 240
    if (cipher == NULL)
        cipher = getenv("SSL_CIPHER");
    if (cipher == NULL) {
R
Rich Salz 已提交
241
        BIO_printf(bio_err, "No CIPHER specified\n");
242
        goto end;
243 244
    }

245
    if ((ctx = SSL_CTX_new(meth)) == NULL)
246 247
        goto end;

248
    SSL_CTX_set_quiet_shutdown(ctx, 1);
249 250

    if (st_bugs)
251 252
        SSL_CTX_set_options(ctx, SSL_OP_ALL);
    if (!SSL_CTX_set_cipher_list(ctx, cipher))
M
Matt Caswell 已提交
253
        goto end;
254
    if (!set_cert_stuff(ctx, certfile, keyfile))
255 256
        goto end;

257
    if (!ctx_set_verify_locations(ctx, CAfile, CApath)) {
258
        ERR_print_errors(bio_err);
259
        goto end;
260 261 262
    }
    if (!(perform & 1))
        goto next;
263
    printf("Collecting connection statistics for %d seconds\n", maxtime);
264

265
    /* Loop and time how long it takes to make connections */
266

267
    bytes_read = 0;
268
    finishtime = (long)time(NULL) + maxtime;
269 270 271 272
    tm_Time_F(START);
    for (;;) {
        if (finishtime < (long)time(NULL))
            break;
273 274
#ifdef WIN32_STUFF

275 276
        if (flushWinMsgs(0) == -1)
            goto end;
277

278 279
        if (waitingToDie || exitNow) /* we're dead */
            goto end;
280 281
#endif

282
        if ((scon = doConnection(NULL, host, ctx)) == NULL)
283
            goto end;
284

285
        if (www_path != NULL) {
286
            BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n",
287
                         www_path);
V
Viktor Dukhovni 已提交
288
            if (SSL_write(scon, buf, strlen(buf)) <= 0)
M
Matt Caswell 已提交
289
                goto end;
290 291 292
            while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
                bytes_read += i;
        }
293
#ifdef NO_SHUTDOWN
294
        SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
295
#else
296
        SSL_shutdown(scon);
297
#endif
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
        SHUTDOWN2(SSL_get_fd(scon));

        nConn += 1;
        if (SSL_session_reused(scon))
            ver = 'r';
        else {
            ver = SSL_version(scon);
            if (ver == TLS1_VERSION)
                ver = 't';
            else if (ver == SSL3_VERSION)
                ver = '3';
            else
                ver = '*';
        }
        fputc(ver, stdout);
        fflush(stdout);

        SSL_free(scon);
        scon = NULL;
    }
    totalTime += tm_Time_F(STOP); /* Add the time for this iteration */

320
    i = (int)((long)time(NULL) - finishtime + maxtime);
321 322 323 324 325
    printf
        ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
         nConn, totalTime, ((double)nConn / totalTime), bytes_read);
    printf
        ("%d connections in %ld real seconds, %ld bytes read per connection\n",
326
         nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
327 328 329 330 331 332 333 334 335 336 337

    /*
     * Now loop and time connections using the same session id over and over
     */

 next:
    if (!(perform & 2))
        goto end;
    printf("\n\nNow timing with session id reuse.\n");

    /* Get an SSL object so we can reuse the session id */
338
    if ((scon = doConnection(NULL, host, ctx)) == NULL) {
R
Rich Salz 已提交
339
        BIO_printf(bio_err, "Unable to get connection\n");
340 341 342
        goto end;
    }

343 344
    if (www_path != NULL) {
        BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n", www_path);
V
Viktor Dukhovni 已提交
345
        if (SSL_write(scon, buf, strlen(buf)) <= 0)
M
Matt Caswell 已提交
346
            goto end;
347 348
        while (SSL_read(scon, buf, sizeof(buf)) > 0)
            continue;
349
    }
350
#ifdef NO_SHUTDOWN
351
    SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
352
#else
353
    SSL_shutdown(scon);
354
#endif
355 356 357 358
    SHUTDOWN2(SSL_get_fd(scon));

    nConn = 0;
    totalTime = 0.0;
359

360
    finishtime = (long)time(NULL) + maxtime;
361

362 363 364
    printf("starting\n");
    bytes_read = 0;
    tm_Time_F(START);
365

366 367 368
    for (;;) {
        if (finishtime < (long)time(NULL))
            break;
369 370

#ifdef WIN32_STUFF
371 372
        if (flushWinMsgs(0) == -1)
            goto end;
373

374 375
        if (waitingToDie || exitNow) /* we're dead */
            goto end;
376 377
#endif

378
        if ((doConnection(scon, host, ctx)) == NULL)
379
            goto end;
380

381
        if (www_path) {
382
            BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n",
383
                         www_path);
V
Viktor Dukhovni 已提交
384
            if (SSL_write(scon, buf, strlen(buf)) <= 0)
M
Matt Caswell 已提交
385
                goto end;
386 387 388
            while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
                bytes_read += i;
        }
389
#ifdef NO_SHUTDOWN
390
        SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
391
#else
392
        SSL_shutdown(scon);
393
#endif
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
        SHUTDOWN2(SSL_get_fd(scon));

        nConn += 1;
        if (SSL_session_reused(scon))
            ver = 'r';
        else {
            ver = SSL_version(scon);
            if (ver == TLS1_VERSION)
                ver = 't';
            else if (ver == SSL3_VERSION)
                ver = '3';
            else
                ver = '*';
        }
        fputc(ver, stdout);
        fflush(stdout);
    }
    totalTime += tm_Time_F(STOP); /* Add the time for this iteration */

    printf
        ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
         nConn, totalTime, ((double)nConn / totalTime), bytes_read);
    printf
        ("%d connections in %ld real seconds, %ld bytes read per connection\n",
418
         nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
419 420

    ret = 0;
421

422
 end:
R
Rich Salz 已提交
423
    SSL_free(scon);
424 425
    SSL_CTX_free(ctx);
    return (ret);
426
}
427

428
/*-
429 430
 * doConnection - make a connection
 */
431
static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx)
432 433 434 435 436 437 438 439 440 441 442 443
{
    BIO *conn;
    SSL *serverCon;
    int width, i;
    fd_set readfds;

    if ((conn = BIO_new(BIO_s_connect())) == NULL)
        return (NULL);

    BIO_set_conn_hostname(conn, host);

    if (scon == NULL)
444
        serverCon = SSL_new(ctx);
445 446 447 448
    else {
        serverCon = scon;
        SSL_set_connect_state(serverCon);
    }
449

450
    SSL_set_bio(serverCon, conn, conn);
451

452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
    /* ok, lets connect */
    for (;;) {
        i = SSL_connect(serverCon);
        if (BIO_sock_should_retry(i)) {
            BIO_printf(bio_err, "DELAY\n");

            i = SSL_get_fd(serverCon);
            width = i + 1;
            FD_ZERO(&readfds);
            openssl_fdset(i, &readfds);
            /*
             * Note: under VMS with SOCKETSHR the 2nd parameter is currently
             * of type (int *) whereas under other systems it is (void *) if
             * you don't have a cast it will choke the compiler: if you do
             * have a cast then you can either go for (int *) or (void *).
             */
            select(width, (void *)&readfds, NULL, NULL, NULL);
            continue;
        }
        break;
    }
    if (i <= 0) {
        BIO_printf(bio_err, "ERROR\n");
        if (verify_error != X509_V_OK)
            BIO_printf(bio_err, "verify error:%s\n",
                       X509_verify_cert_error_string(verify_error));
        else
            ERR_print_errors(bio_err);
        if (scon == NULL)
            SSL_free(serverCon);
        return NULL;
    }
484

485 486
    return serverCon;
}