s_time.c 14.5 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
#if !defined(OPENSSL_SYS_MSDOS)
77
# include OPENSSL_UNISTD
78
#endif
79

U
Ulf Möller 已提交
80
#undef ioctl
81 82
#define ioctl ioctlsocket

83
#define SSL_CONNECT_NAME        "localhost:4433"
84

85
/* no default cert. */
86 87 88
/*
 * #define TEST_CERT "client.pem"
 */
89 90 91 92

#undef BUFSIZZ
#define BUFSIZZ 1024*10

93 94
#define MYBUFSIZ 1024*8

95 96
#undef min
#undef max
97 98 99 100
#define min(a,b) (((a) < (b)) ? (a) : (b))
#define max(a,b) (((a) > (b)) ? (a) : (b))

#undef SECONDS
101
#define SECONDS 30
102 103
#define SECONDSSTR "30"

104 105 106
extern int verify_depth;
extern int verify_error;

107
static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx);
108

109 110 111
typedef enum OPTION_choice {
    OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
    OPT_CONNECT, OPT_CIPHER, OPT_CERT, OPT_KEY, OPT_CAPATH,
112 113
    OPT_CAFILE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_NEW, OPT_REUSE, OPT_BUGS,
    OPT_VERIFY, OPT_TIME, OPT_SSL3,
114 115 116 117 118 119 120 121 122 123 124 125
    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"},
126 127 128 129
    {"no-CAfile", OPT_NOCAFILE, '-',
     "Do not load the default certificates file"},
    {"no-CApath", OPT_NOCAPATH, '-',
     "Do not load certificates from the default certificates directory"},
130 131 132 133 134 135 136 137 138
    {"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
    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;
160
    int noCApath = 0, noCAfile = 0;
161 162 163 164
    int maxtime = SECONDS, nConn = 0, perform = 3, ret = 1, i, st_bugs =
        0, ver;
    long bytes_read = 0, finishtime = 0;
    OPTION_CHOICE o;
165

166
    meth = TLS_client_method();
167 168
    verify_depth = 0;
    verify_error = X509_V_OK;
169

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    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:
186
            perform = 2;
187 188
            break;
        case OPT_NEW:
189
            perform = 1;
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
            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;
209 210 211 212 213 214
        case OPT_NOCAPATH:
            noCApath = 1;
            break;
        case OPT_NOCAFILE:
            noCAfile = 1;
            break;
215 216 217 218
        case OPT_CIPHER:
            cipher = opt_arg();
            break;
        case OPT_BUGS:
219
            st_bugs = 1;
220 221 222 223 224 225 226 227 228 229
            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 已提交
230
            }
231
            break;
232
        case OPT_SSL3:
233
#ifndef OPENSSL_NO_SSL3
234 235
            meth = SSLv3_client_method();
#endif
236
            break;
237
        }
238
    }
239
    argc = opt_num_rest();
K
Kurt Roeckx 已提交
240 241
    if (argc != 0)
        goto opthelp;
242

243 244 245
    if (cipher == NULL)
        cipher = getenv("SSL_CIPHER");
    if (cipher == NULL) {
R
Rich Salz 已提交
246
        BIO_printf(bio_err, "No CIPHER specified\n");
247
        goto end;
248 249
    }

250
    if ((ctx = SSL_CTX_new(meth)) == NULL)
251 252
        goto end;

253
    SSL_CTX_set_quiet_shutdown(ctx, 1);
254 255

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

262
    if (!ctx_set_verify_locations(ctx, CAfile, CApath, noCAfile, noCApath)) {
263
        ERR_print_errors(bio_err);
264
        goto end;
265 266 267
    }
    if (!(perform & 1))
        goto next;
268
    printf("Collecting connection statistics for %d seconds\n", maxtime);
269

270
    /* Loop and time how long it takes to make connections */
271

272
    bytes_read = 0;
273
    finishtime = (long)time(NULL) + maxtime;
274 275 276 277
    tm_Time_F(START);
    for (;;) {
        if (finishtime < (long)time(NULL))
            break;
278

279
        if ((scon = doConnection(NULL, host, ctx)) == NULL)
280
            goto end;
281

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

317
    i = (int)((long)time(NULL) - finishtime + maxtime);
318 319 320 321 322
    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",
323
         nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
324 325 326 327 328 329 330 331 332 333 334

    /*
     * 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 */
335
    if ((scon = doConnection(NULL, host, ctx)) == NULL) {
R
Rich Salz 已提交
336
        BIO_printf(bio_err, "Unable to get connection\n");
337 338 339
        goto end;
    }

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

    nConn = 0;
    totalTime = 0.0;
356

357
    finishtime = (long)time(NULL) + maxtime;
358

359 360 361
    printf("starting\n");
    bytes_read = 0;
    tm_Time_F(START);
362

363 364 365
    for (;;) {
        if (finishtime < (long)time(NULL))
            break;
366

367
        if ((doConnection(scon, host, ctx)) == NULL)
368
            goto end;
369

370
        if (www_path) {
371
            BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n",
372
                         www_path);
V
Viktor Dukhovni 已提交
373
            if (SSL_write(scon, buf, strlen(buf)) <= 0)
M
Matt Caswell 已提交
374
                goto end;
375 376 377
            while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
                bytes_read += i;
        }
378
#ifdef NO_SHUTDOWN
379
        SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
380
#else
381
        SSL_shutdown(scon);
382
#endif
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
        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",
407
         nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
408 409

    ret = 0;
410

411
 end:
R
Rich Salz 已提交
412
    SSL_free(scon);
413 414
    SSL_CTX_free(ctx);
    return (ret);
415
}
416

417
/*-
418 419
 * doConnection - make a connection
 */
420
static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx)
421 422 423 424 425 426 427 428 429 430 431 432
{
    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)
433
        serverCon = SSL_new(ctx);
434 435 436 437
    else {
        serverCon = scon;
        SSL_set_connect_state(serverCon);
    }
438

439
    SSL_set_bio(serverCon, conn, conn);
440

441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
    /* 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;
    }
473

474 475
    return serverCon;
}