source: rtems-libbsd/freebsd/crypto/openssl/apps/s_time.c @ d1dac78

5
Last change on this file since d1dac78 was d1dac78, checked in by Christian Mauderer <christian.mauderer@…>, on 03/26/19 at 10:08:47

bin/openssl: Port to RTEMS.

  • Property mode set to 100644
File size: 12.1 KB
Line 
1#include <machine/rtems-bsd-user-space.h>
2#ifdef __rtems__
3#include <machine/rtems-bsd-program.h>
4#include "rtems-bsd-openssl-namespace.h"
5#endif /* __rtems__ */
6
7/*
8 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
9 *
10 * Licensed under the OpenSSL license (the "License").  You may not use
11 * this file except in compliance with the License.  You can obtain a copy
12 * in the file LICENSE in the source distribution or at
13 * https://www.openssl.org/source/license.html
14 */
15
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19
20#include <openssl/opensslconf.h>
21
22#ifndef OPENSSL_NO_SOCK
23
24#include "apps.h"
25#include "progs.h"
26#include <openssl/x509.h>
27#include <openssl/ssl.h>
28#include <openssl/pem.h>
29#include "s_apps.h"
30#include <openssl/err.h>
31#include <internal/sockets.h>
32#if !defined(OPENSSL_SYS_MSDOS)
33# include OPENSSL_UNISTD
34#endif
35
36#define SSL_CONNECT_NAME        "localhost:4433"
37
38#define SECONDS 30
39#define SECONDSSTR "30"
40
41static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx);
42
43/*
44 * Define a HTTP get command globally.
45 * Also define the size of the command, this is two bytes less than
46 * the size of the string because the %s is replaced by the URL.
47 */
48static const char fmt_http_get_cmd[] = "GET %s HTTP/1.0\r\n\r\n";
49static const size_t fmt_http_get_cmd_size = sizeof(fmt_http_get_cmd) - 2;
50
51typedef enum OPTION_choice {
52    OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
53    OPT_CONNECT, OPT_CIPHER, OPT_CIPHERSUITES, OPT_CERT, OPT_NAMEOPT, OPT_KEY,
54    OPT_CAPATH, OPT_CAFILE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_NEW, OPT_REUSE,
55    OPT_BUGS, OPT_VERIFY, OPT_TIME, OPT_SSL3,
56    OPT_WWW
57} OPTION_CHOICE;
58
59const OPTIONS s_time_options[] = {
60    {"help", OPT_HELP, '-', "Display this summary"},
61    {"connect", OPT_CONNECT, 's',
62     "Where to connect as post:port (default is " SSL_CONNECT_NAME ")"},
63    {"cipher", OPT_CIPHER, 's', "TLSv1.2 and below cipher list to be used"},
64    {"ciphersuites", OPT_CIPHERSUITES, 's',
65     "Specify TLSv1.3 ciphersuites to be used"},
66    {"cert", OPT_CERT, '<', "Cert file to use, PEM format assumed"},
67    {"nameopt", OPT_NAMEOPT, 's', "Various certificate name options"},
68    {"key", OPT_KEY, '<', "File with key, PEM; default is -cert file"},
69    {"CApath", OPT_CAPATH, '/', "PEM format directory of CA's"},
70    {"cafile", OPT_CAFILE, '<', "PEM format file of CA's"},
71    {"no-CAfile", OPT_NOCAFILE, '-',
72     "Do not load the default certificates file"},
73    {"no-CApath", OPT_NOCAPATH, '-',
74     "Do not load certificates from the default certificates directory"},
75    {"new", OPT_NEW, '-', "Just time new connections"},
76    {"reuse", OPT_REUSE, '-', "Just time connection reuse"},
77    {"bugs", OPT_BUGS, '-', "Turn on SSL bug compatibility"},
78    {"verify", OPT_VERIFY, 'p',
79     "Turn on peer certificate verification, set depth"},
80    {"time", OPT_TIME, 'p', "Seconds to collect data, default " SECONDSSTR},
81    {"www", OPT_WWW, 's', "Fetch specified page from the site"},
82#ifndef OPENSSL_NO_SSL3
83    {"ssl3", OPT_SSL3, '-', "Just use SSLv3"},
84#endif
85    {NULL}
86};
87
88#define START   0
89#define STOP    1
90
91static double tm_Time_F(int s)
92{
93    return app_tminterval(s, 1);
94}
95
96int s_time_main(int argc, char **argv)
97{
98    char buf[1024 * 8];
99    SSL *scon = NULL;
100    SSL_CTX *ctx = NULL;
101    const SSL_METHOD *meth = NULL;
102    char *CApath = NULL, *CAfile = NULL, *cipher = NULL, *ciphersuites = NULL;
103    char *www_path = NULL;
104    char *host = SSL_CONNECT_NAME, *certfile = NULL, *keyfile = NULL, *prog;
105    double totalTime = 0.0;
106    int noCApath = 0, noCAfile = 0;
107    int maxtime = SECONDS, nConn = 0, perform = 3, ret = 1, i, st_bugs = 0;
108    long bytes_read = 0, finishtime = 0;
109    OPTION_CHOICE o;
110    int max_version = 0, ver, buf_len;
111    size_t buf_size;
112
113    meth = TLS_client_method();
114
115    prog = opt_init(argc, argv, s_time_options);
116    while ((o = opt_next()) != OPT_EOF) {
117        switch (o) {
118        case OPT_EOF:
119        case OPT_ERR:
120 opthelp:
121            BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
122            goto end;
123        case OPT_HELP:
124            opt_help(s_time_options);
125            ret = 0;
126            goto end;
127        case OPT_CONNECT:
128            host = opt_arg();
129            break;
130        case OPT_REUSE:
131            perform = 2;
132            break;
133        case OPT_NEW:
134            perform = 1;
135            break;
136        case OPT_VERIFY:
137            if (!opt_int(opt_arg(), &verify_args.depth))
138                goto opthelp;
139            BIO_printf(bio_err, "%s: verify depth is %d\n",
140                       prog, verify_args.depth);
141            break;
142        case OPT_CERT:
143            certfile = opt_arg();
144            break;
145        case OPT_NAMEOPT:
146            if (!set_nameopt(opt_arg()))
147                goto end;
148            break;
149        case OPT_KEY:
150            keyfile = opt_arg();
151            break;
152        case OPT_CAPATH:
153            CApath = opt_arg();
154            break;
155        case OPT_CAFILE:
156            CAfile = opt_arg();
157            break;
158        case OPT_NOCAPATH:
159            noCApath = 1;
160            break;
161        case OPT_NOCAFILE:
162            noCAfile = 1;
163            break;
164        case OPT_CIPHER:
165            cipher = opt_arg();
166            break;
167        case OPT_CIPHERSUITES:
168            ciphersuites = opt_arg();
169            break;
170        case OPT_BUGS:
171            st_bugs = 1;
172            break;
173        case OPT_TIME:
174            if (!opt_int(opt_arg(), &maxtime))
175                goto opthelp;
176            break;
177        case OPT_WWW:
178            www_path = opt_arg();
179            buf_size = strlen(www_path) + fmt_http_get_cmd_size;
180            if (buf_size > sizeof(buf)) {
181                BIO_printf(bio_err, "%s: -www option is too long\n", prog);
182                goto end;
183            }
184            break;
185        case OPT_SSL3:
186            max_version = SSL3_VERSION;
187            break;
188        }
189    }
190    argc = opt_num_rest();
191    if (argc != 0)
192        goto opthelp;
193
194    if (cipher == NULL)
195        cipher = getenv("SSL_CIPHER");
196
197    if ((ctx = SSL_CTX_new(meth)) == NULL)
198        goto end;
199
200    SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
201    SSL_CTX_set_quiet_shutdown(ctx, 1);
202    if (SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
203        goto end;
204
205    if (st_bugs)
206        SSL_CTX_set_options(ctx, SSL_OP_ALL);
207    if (cipher != NULL && !SSL_CTX_set_cipher_list(ctx, cipher))
208        goto end;
209    if (ciphersuites != NULL && !SSL_CTX_set_ciphersuites(ctx, ciphersuites))
210        goto end;
211    if (!set_cert_stuff(ctx, certfile, keyfile))
212        goto end;
213
214    if (!ctx_set_verify_locations(ctx, CAfile, CApath, noCAfile, noCApath)) {
215        ERR_print_errors(bio_err);
216        goto end;
217    }
218    if (!(perform & 1))
219        goto next;
220    printf("Collecting connection statistics for %d seconds\n", maxtime);
221
222    /* Loop and time how long it takes to make connections */
223
224    bytes_read = 0;
225    finishtime = (long)time(NULL) + maxtime;
226    tm_Time_F(START);
227    for (;;) {
228        if (finishtime < (long)time(NULL))
229            break;
230
231        if ((scon = doConnection(NULL, host, ctx)) == NULL)
232            goto end;
233
234        if (www_path != NULL) {
235            buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd,
236                                   www_path);
237            if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
238                goto end;
239            while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
240                bytes_read += i;
241        }
242        SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
243        BIO_closesocket(SSL_get_fd(scon));
244
245        nConn += 1;
246        if (SSL_session_reused(scon)) {
247            ver = 'r';
248        } else {
249            ver = SSL_version(scon);
250            if (ver == TLS1_VERSION)
251                ver = 't';
252            else if (ver == SSL3_VERSION)
253                ver = '3';
254            else
255                ver = '*';
256        }
257        fputc(ver, stdout);
258        fflush(stdout);
259
260        SSL_free(scon);
261        scon = NULL;
262    }
263    totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
264
265    i = (int)((long)time(NULL) - finishtime + maxtime);
266    printf
267        ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
268         nConn, totalTime, ((double)nConn / totalTime), bytes_read);
269    printf
270        ("%d connections in %ld real seconds, %ld bytes read per connection\n",
271         nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
272
273    /*
274     * Now loop and time connections using the same session id over and over
275     */
276
277 next:
278    if (!(perform & 2))
279        goto end;
280    printf("\n\nNow timing with session id reuse.\n");
281
282    /* Get an SSL object so we can reuse the session id */
283    if ((scon = doConnection(NULL, host, ctx)) == NULL) {
284        BIO_printf(bio_err, "Unable to get connection\n");
285        goto end;
286    }
287
288    if (www_path != NULL) {
289        buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd, www_path);
290        if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
291            goto end;
292        while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
293            continue;
294    }
295    SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
296    BIO_closesocket(SSL_get_fd(scon));
297
298    nConn = 0;
299    totalTime = 0.0;
300
301    finishtime = (long)time(NULL) + maxtime;
302
303    printf("starting\n");
304    bytes_read = 0;
305    tm_Time_F(START);
306
307    for (;;) {
308        if (finishtime < (long)time(NULL))
309            break;
310
311        if ((doConnection(scon, host, ctx)) == NULL)
312            goto end;
313
314        if (www_path != NULL) {
315            buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd,
316                                   www_path);
317            if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
318                goto end;
319            while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
320                bytes_read += i;
321        }
322        SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
323        BIO_closesocket(SSL_get_fd(scon));
324
325        nConn += 1;
326        if (SSL_session_reused(scon)) {
327            ver = 'r';
328        } else {
329            ver = SSL_version(scon);
330            if (ver == TLS1_VERSION)
331                ver = 't';
332            else if (ver == SSL3_VERSION)
333                ver = '3';
334            else
335                ver = '*';
336        }
337        fputc(ver, stdout);
338        fflush(stdout);
339    }
340    totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
341
342    printf
343        ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
344         nConn, totalTime, ((double)nConn / totalTime), bytes_read);
345    printf
346        ("%d connections in %ld real seconds, %ld bytes read per connection\n",
347         nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
348
349    ret = 0;
350
351 end:
352    SSL_free(scon);
353    SSL_CTX_free(ctx);
354    return ret;
355}
356
357/*-
358 * doConnection - make a connection
359 */
360static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx)
361{
362    BIO *conn;
363    SSL *serverCon;
364    int i;
365
366    if ((conn = BIO_new(BIO_s_connect())) == NULL)
367        return NULL;
368
369    BIO_set_conn_hostname(conn, host);
370    BIO_set_conn_mode(conn, BIO_SOCK_NODELAY);
371
372    if (scon == NULL)
373        serverCon = SSL_new(ctx);
374    else {
375        serverCon = scon;
376        SSL_set_connect_state(serverCon);
377    }
378
379    SSL_set_bio(serverCon, conn, conn);
380
381    /* ok, lets connect */
382    i = SSL_connect(serverCon);
383    if (i <= 0) {
384        BIO_printf(bio_err, "ERROR\n");
385        if (verify_args.error != X509_V_OK)
386            BIO_printf(bio_err, "verify error:%s\n",
387                       X509_verify_cert_error_string(verify_args.error));
388        else
389            ERR_print_errors(bio_err);
390        if (scon == NULL)
391            SSL_free(serverCon);
392        return NULL;
393    }
394
395#if defined(SOL_SOCKET) && defined(SO_LINGER)
396    {
397        struct linger no_linger;
398        int fd;
399
400        no_linger.l_onoff  = 1;
401        no_linger.l_linger = 0;
402        fd = SSL_get_fd(serverCon);
403        if (fd >= 0)
404            (void)setsockopt(fd, SOL_SOCKET, SO_LINGER, (char*)&no_linger,
405                             sizeof(no_linger));
406    }
407#endif
408
409    return serverCon;
410}
411#endif /* OPENSSL_NO_SOCK */
412#ifdef __rtems__
413#include "rtems-bsd-openssl-s_time-data.h"
414#endif /* __rtems__ */
Note: See TracBrowser for help on using the repository browser.