You need to sign in or sign up before continuing.
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
#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
    {"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"},
135
    {"time", OPT_TIME, 'p', "Seconds to collect data, default " SECONDSSTR},
136 137 138
    {"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
    int max_version = 0;
166

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

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

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

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

252
    SSL_CTX_set_quiet_shutdown(ctx, 1);
253 254
    if (SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
        goto end;
255 256

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

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

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

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

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

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

        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 */

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

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

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

    nConn = 0;
    totalTime = 0.0;
357

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

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

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

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

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

        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",
408
         nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
409 410

    ret = 0;
411

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

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

440
    SSL_set_bio(serverCon, conn, conn);
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 473
    /* 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;
    }
474

475 476
    return serverCon;
}