s_time.c 14.7 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>

M
Matt Caswell 已提交
69 70 71 72
#include <openssl/opensslconf.h>

#ifndef OPENSSL_NO_SOCK

73 74
#define USE_SOCKETS
#include "apps.h"
75 76 77
#include <openssl/x509.h>
#include <openssl/ssl.h>
#include <openssl/pem.h>
78
#include "s_apps.h"
79
#include <openssl/err.h>
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
typedef enum OPTION_choice {
    OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
    OPT_CONNECT, OPT_CIPHER, OPT_CERT, OPT_KEY, OPT_CAPATH,
116 117
    OPT_CAFILE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_NEW, OPT_REUSE, OPT_BUGS,
    OPT_VERIFY, OPT_TIME, OPT_SSL3,
118 119 120 121 122 123 124 125 126 127 128 129
    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"},
130 131 132 133
    {"no-CAfile", OPT_NOCAFILE, '-',
     "Do not load the default certificates file"},
    {"no-CApath", OPT_NOCAPATH, '-',
     "Do not load certificates from the default certificates directory"},
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"},
139
    {"time", OPT_TIME, 'p', "Seconds to collect data, default " SECONDSSTR},
140 141 142
    {"www", OPT_WWW, 's', "Fetch specified page from the site"},
#ifndef OPENSSL_NO_SSL3
    {"ssl3", OPT_SSL3, '-', "Just use SSLv3"},
143
#endif
144 145
    {NULL}
};
146

147 148
#define START   0
#define STOP    1
149

150
static double tm_Time_F(int s)
151
{
152
    return app_tminterval(s, 1);
153 154
}

155
int s_time_main(int argc, char **argv)
156
{
157 158 159 160 161 162 163
    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;
164
    int noCApath = 0, noCAfile = 0;
165 166 167 168
    int maxtime = SECONDS, nConn = 0, perform = 3, ret = 1, i, st_bugs =
        0, ver;
    long bytes_read = 0, finishtime = 0;
    OPTION_CHOICE o;
169
    int max_version = 0;
170

171
    meth = TLS_client_method();
172 173
    verify_depth = 0;
    verify_error = X509_V_OK;
174

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

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

253
    if ((ctx = SSL_CTX_new(meth)) == NULL)
254 255
        goto end;

256
    SSL_CTX_set_quiet_shutdown(ctx, 1);
257 258
    if (SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
        goto end;
259 260

    if (st_bugs)
261 262
        SSL_CTX_set_options(ctx, SSL_OP_ALL);
    if (!SSL_CTX_set_cipher_list(ctx, cipher))
M
Matt Caswell 已提交
263
        goto end;
264
    if (!set_cert_stuff(ctx, certfile, keyfile))
265 266
        goto end;

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

275
    /* Loop and time how long it takes to make connections */
276

277
    bytes_read = 0;
278
    finishtime = (long)time(NULL) + maxtime;
279 280 281 282
    tm_Time_F(START);
    for (;;) {
        if (finishtime < (long)time(NULL))
            break;
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);
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
R
Rich Salz 已提交
300
        BIO_closesocket(SSL_get_fd(scon));
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321

        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) {
R
Rich Salz 已提交
341
        BIO_printf(bio_err, "Unable to get connection\n");
342 343 344
        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
R
Rich Salz 已提交
357
    BIO_closesocket(SSL_get_fd(scon));
358 359 360

    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
        if ((doConnection(scon, host, ctx)) == NULL)
373
            goto end;
374

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

        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",
412
         nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
413 414

    ret = 0;
415

416
 end:
R
Rich Salz 已提交
417
    SSL_free(scon);
418 419
    SSL_CTX_free(ctx);
    return (ret);
420
}
421

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

444
    SSL_set_bio(serverCon, conn, conn);
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 474 475 476 477
    /* 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;
    }
478

479 480
    return serverCon;
}
M
Matt Caswell 已提交
481
#endif /* OPENSSL_NO_SOCK */