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
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,
#ifndef OPENSSL_NO_SSL3
    OPT_SSL3,
119
#endif
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    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"},
141
#endif
142 143
    {NULL}
};
144

145 146
#define START   0
#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 156 157 158 159 160 161 162 163 164 165 166 167 168
    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
169

170
    meth = SSLv23_client_method();
171 172
    verify_depth = 0;
    verify_error = X509_V_OK;
173

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    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:
190
            perform = 2;
191 192
            break;
        case OPT_NEW:
193
            perform = 1;
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
            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:
217
            st_bugs = 1;
218 219 220 221 222 223 224 225 226 227
            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 已提交
228
            }
229
            break;
230 231 232 233 234
#ifndef OPENSSL_NO_SSL3
        case OPT_SSL3:
            meth = SSLv3_client_method();
            break;
#endif
235
        }
236
    }
237 238
    argc = opt_num_rest();
    argv = opt_rest();
239

240 241 242 243 244
    if (cipher == NULL)
        cipher = getenv("SSL_CIPHER");
    if (cipher == NULL) {
        fprintf(stderr, "No CIPHER specified\n");
        goto end;
245 246
    }

247
    if ((ctx = SSL_CTX_new(meth)) == NULL)
248 249
        goto end;

250
    SSL_CTX_set_quiet_shutdown(ctx, 1);
251 252

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

259
    if (!ctx_set_verify_locations(ctx, CAfile, CApath)) {
260
        ERR_print_errors(bio_err);
261
        goto end;
262 263 264
    }
    if (!(perform & 1))
        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 272 273 274
    tm_Time_F(START);
    for (;;) {
        if (finishtime < (long)time(NULL))
            break;
275 276
#ifdef WIN32_STUFF

277 278
        if (flushWinMsgs(0) == -1)
            goto end;
279

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

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);
V
Viktor Dukhovni 已提交
290
            if (SSL_write(scon, buf, strlen(buf)) <= 0)
M
Matt Caswell 已提交
291
                goto end;
292 293 294
            while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
                bytes_read += i;
        }
295
#ifdef NO_SHUTDOWN
296
        SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
297
#else
298
        SSL_shutdown(scon);
299
#endif
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
        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 */

322
    i = (int)((long)time(NULL) - finishtime + maxtime);
323 324 325 326 327
    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",
328
         nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
329 330 331 332 333 334 335 336 337 338 339

    /*
     * 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 */
340
    if ((scon = doConnection(NULL, host, ctx)) == NULL) {
341 342 343 344
        fprintf(stderr, "Unable to get connection\n");
        goto end;
    }

345 346
    if (www_path != NULL) {
        BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n", www_path);
V
Viktor Dukhovni 已提交
347
        if (SSL_write(scon, buf, strlen(buf)) <= 0)
M
Matt Caswell 已提交
348
            goto end;
349 350
        while (SSL_read(scon, buf, sizeof(buf)) > 0)
            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 358 359 360
    SHUTDOWN2(SSL_get_fd(scon));

    nConn = 0;
    totalTime = 0.0;
361

362
    finishtime = (long)time(NULL) + maxtime;
363

364 365 366
    printf("starting\n");
    bytes_read = 0;
    tm_Time_F(START);
367

368 369 370
    for (;;) {
        if (finishtime < (long)time(NULL))
            break;
371 372

#ifdef WIN32_STUFF
373 374
        if (flushWinMsgs(0) == -1)
            goto end;
375

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

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);
V
Viktor Dukhovni 已提交
386
            if (SSL_write(scon, buf, strlen(buf)) <= 0)
M
Matt Caswell 已提交
387
                goto end;
388 389 390
            while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
                bytes_read += i;
        }
391
#ifdef NO_SHUTDOWN
392
        SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
393
#else
394
        SSL_shutdown(scon);
395
#endif
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
        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",
420
         nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
421 422

    ret = 0;
423

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

430
/*-
431 432
 * doConnection - make a connection
 */
433
static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx)
434 435 436 437 438 439 440 441 442 443 444 445
{
    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)
446
        serverCon = SSL_new(ctx);
447 448 449 450
    else {
        serverCon = scon;
        SSL_set_connect_state(serverCon);
    }
451

452
    SSL_set_bio(serverCon, conn, conn);
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 484 485
    /* 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;
    }
486

487 488
    return serverCon;
}