Home | History | Annotate | Line # | Download | only in test
      1 /*
      2  * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
      3  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
      4  * Copyright 2005 Nokia. All rights reserved.
      5  *
      6  * Licensed under the Apache License 2.0 (the "License").  You may not use
      7  * this file except in compliance with the License.  You can obtain a copy
      8  * in the file LICENSE in the source distribution or at
      9  * https://www.openssl.org/source/license.html
     10  */
     11 
     12 #include "internal/e_os.h"
     13 
     14 /* Or gethostname won't be declared properly on Linux and GNU platforms. */
     15 #ifndef _BSD_SOURCE
     16 #define _BSD_SOURCE 1
     17 #endif
     18 #ifndef _DEFAULT_SOURCE
     19 #define _DEFAULT_SOURCE 1
     20 #endif
     21 
     22 #include <assert.h>
     23 #include <errno.h>
     24 #include <limits.h>
     25 #include <stdio.h>
     26 #include <stdlib.h>
     27 #include <string.h>
     28 #include <time.h>
     29 
     30 #include "internal/nelem.h"
     31 
     32 #ifdef OPENSSL_SYS_VMS
     33 /*
     34  * Or isascii won't be declared properly on VMS (at least with DECompHP C).
     35  */
     36 #define _XOPEN_SOURCE 500
     37 #endif
     38 
     39 #include <ctype.h>
     40 
     41 #include <openssl/bio.h>
     42 #include <openssl/crypto.h>
     43 #include <openssl/evp.h>
     44 #include <openssl/x509.h>
     45 #include <openssl/x509v3.h>
     46 #include <openssl/ssl.h>
     47 #include <openssl/err.h>
     48 #include <openssl/rand.h>
     49 #include <openssl/rsa.h>
     50 #ifndef OPENSSL_NO_DSA
     51 #include <openssl/dsa.h>
     52 #endif
     53 #include <openssl/bn.h>
     54 #ifndef OPENSSL_NO_CT
     55 #include <openssl/ct.h>
     56 #endif
     57 #include <openssl/provider.h>
     58 #include "testutil.h"
     59 #include "testutil/output.h"
     60 
     61 /*
     62  * Or gethostname won't be declared properly
     63  * on Compaq platforms (at least with DEC C).
     64  * Do not try to put it earlier, or IPv6 includes
     65  * get screwed...
     66  */
     67 #define _XOPEN_SOURCE_EXTENDED 1
     68 
     69 #ifdef OPENSSL_SYS_WINDOWS
     70 #include <winsock.h>
     71 #else
     72 #include <unistd.h>
     73 #endif
     74 
     75 #include "helpers/predefined_dhparams.h"
     76 
     77 static SSL_CTX *s_ctx = NULL;
     78 static SSL_CTX *s_ctx2 = NULL;
     79 
     80 /*
     81  * There is really no standard for this, so let's assign something
     82  * only for this test
     83  */
     84 #define COMP_ZLIB 1
     85 
     86 static int verify_callback(int ok, X509_STORE_CTX *ctx);
     87 static int app_verify_callback(X509_STORE_CTX *ctx, void *arg);
     88 #define APP_CALLBACK_STRING "Test Callback Argument"
     89 struct app_verify_arg {
     90     char *string;
     91     int app_verify;
     92 };
     93 
     94 static char *psk_key = NULL; /* by default PSK is not used */
     95 #ifndef OPENSSL_NO_PSK
     96 static unsigned int psk_client_callback(SSL *ssl, const char *hint,
     97     char *identity,
     98     unsigned int max_identity_len,
     99     unsigned char *psk,
    100     unsigned int max_psk_len);
    101 static unsigned int psk_server_callback(SSL *ssl, const char *identity,
    102     unsigned char *psk,
    103     unsigned int max_psk_len);
    104 #endif
    105 
    106 static BIO *bio_stdout = NULL;
    107 
    108 #ifndef OPENSSL_NO_NEXTPROTONEG
    109 /* Note that this code assumes that this is only a one element list: */
    110 static const char NEXT_PROTO_STRING[] = "\x09testproto";
    111 static int npn_client = 0;
    112 static int npn_server = 0;
    113 static int npn_server_reject = 0;
    114 
    115 static int cb_client_npn(SSL *s, unsigned char **out, unsigned char *outlen,
    116     const unsigned char *in, unsigned int inlen,
    117     void *arg)
    118 {
    119     /*
    120      * This callback only returns the protocol string, rather than a length
    121      * prefixed set. We assume that NEXT_PROTO_STRING is a one element list
    122      * and remove the first byte to chop off the length prefix.
    123      */
    124     *out = (unsigned char *)NEXT_PROTO_STRING + 1;
    125     *outlen = sizeof(NEXT_PROTO_STRING) - 2;
    126     return SSL_TLSEXT_ERR_OK;
    127 }
    128 
    129 static int cb_server_npn(SSL *s, const unsigned char **data,
    130     unsigned int *len, void *arg)
    131 {
    132     *data = (const unsigned char *)NEXT_PROTO_STRING;
    133     *len = sizeof(NEXT_PROTO_STRING) - 1;
    134     return SSL_TLSEXT_ERR_OK;
    135 }
    136 
    137 static int cb_server_rejects_npn(SSL *s, const unsigned char **data,
    138     unsigned int *len, void *arg)
    139 {
    140     return SSL_TLSEXT_ERR_NOACK;
    141 }
    142 
    143 static int verify_npn(SSL *client, SSL *server)
    144 {
    145     const unsigned char *client_s;
    146     unsigned client_len;
    147     const unsigned char *server_s;
    148     unsigned server_len;
    149 
    150     SSL_get0_next_proto_negotiated(client, &client_s, &client_len);
    151     SSL_get0_next_proto_negotiated(server, &server_s, &server_len);
    152 
    153     if (client_len) {
    154         BIO_printf(bio_stdout, "Client NPN: ");
    155         BIO_write(bio_stdout, client_s, client_len);
    156         BIO_printf(bio_stdout, "\n");
    157     }
    158 
    159     if (server_len) {
    160         BIO_printf(bio_stdout, "Server NPN: ");
    161         BIO_write(bio_stdout, server_s, server_len);
    162         BIO_printf(bio_stdout, "\n");
    163     }
    164 
    165     /*
    166      * If an NPN string was returned, it must be the protocol that we
    167      * expected to negotiate.
    168      */
    169     if (client_len && (client_len != sizeof(NEXT_PROTO_STRING) - 2 || memcmp(client_s, NEXT_PROTO_STRING + 1, client_len)))
    170         return -1;
    171     if (server_len && (server_len != sizeof(NEXT_PROTO_STRING) - 2 || memcmp(server_s, NEXT_PROTO_STRING + 1, server_len)))
    172         return -1;
    173 
    174     if (!npn_client && client_len)
    175         return -1;
    176     if (!npn_server && server_len)
    177         return -1;
    178     if (npn_server_reject && server_len)
    179         return -1;
    180     if (npn_client && npn_server && (!client_len || !server_len))
    181         return -1;
    182 
    183     return 0;
    184 }
    185 #endif
    186 
    187 static const char *alpn_client;
    188 static char *alpn_server;
    189 static char *alpn_server2;
    190 static const char *alpn_expected;
    191 static unsigned char *alpn_selected;
    192 static const char *server_min_proto;
    193 static const char *server_max_proto;
    194 static const char *client_min_proto;
    195 static const char *client_max_proto;
    196 static const char *should_negotiate;
    197 static const char *sn_client;
    198 static const char *sn_server1;
    199 static const char *sn_server2;
    200 static int sn_expect = 0;
    201 static const char *server_sess_out;
    202 static const char *server_sess_in;
    203 static const char *client_sess_out;
    204 static const char *client_sess_in;
    205 static SSL_SESSION *server_sess;
    206 static SSL_SESSION *client_sess;
    207 
    208 static int servername_cb(SSL *s, int *ad, void *arg)
    209 {
    210     const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
    211     if (sn_server2 == NULL) {
    212         BIO_printf(bio_stdout, "Servername 2 is NULL\n");
    213         return SSL_TLSEXT_ERR_NOACK;
    214     }
    215 
    216     if (servername) {
    217         if (s_ctx2 != NULL && sn_server2 != NULL && !OPENSSL_strcasecmp(servername, sn_server2)) {
    218             BIO_printf(bio_stdout, "Switching server context.\n");
    219             SSL_set_SSL_CTX(s, s_ctx2);
    220         }
    221     }
    222     return SSL_TLSEXT_ERR_OK;
    223 }
    224 static int verify_servername(SSL *client, SSL *server)
    225 {
    226     /* just need to see if sn_context is what we expect */
    227     SSL_CTX *ctx = SSL_get_SSL_CTX(server);
    228     if (sn_expect == 0)
    229         return 0;
    230     if (sn_expect == 1 && ctx == s_ctx)
    231         return 0;
    232     if (sn_expect == 2 && ctx == s_ctx2)
    233         return 0;
    234     BIO_printf(bio_stdout, "Servername: expected context %d\n", sn_expect);
    235     if (ctx == s_ctx2)
    236         BIO_printf(bio_stdout, "Servername: context is 2\n");
    237     else if (ctx == s_ctx)
    238         BIO_printf(bio_stdout, "Servername: context is 1\n");
    239     else
    240         BIO_printf(bio_stdout, "Servername: context is unknown\n");
    241     return -1;
    242 }
    243 
    244 /*-
    245  * next_protos_parse parses a comma separated list of strings into a string
    246  * in a format suitable for passing to SSL_CTX_set_next_protos_advertised.
    247  *   outlen: (output) set to the length of the resulting buffer on success.
    248  *   in: a NUL terminated string like "abc,def,ghi"
    249  *
    250  *   returns: a malloced buffer or NULL on failure.
    251  */
    252 static unsigned char *next_protos_parse(size_t *outlen,
    253     const char *in)
    254 {
    255     size_t len;
    256     unsigned char *out;
    257     size_t i, start = 0;
    258 
    259     len = strlen(in);
    260     if (len >= 65535)
    261         return NULL;
    262 
    263     out = OPENSSL_malloc(strlen(in) + 1);
    264     if (!out)
    265         return NULL;
    266 
    267     for (i = 0; i <= len; ++i) {
    268         if (i == len || in[i] == ',') {
    269             if (i - start > 255) {
    270                 OPENSSL_free(out);
    271                 return NULL;
    272             }
    273             out[start] = (unsigned char)(i - start);
    274             start = i + 1;
    275         } else
    276             out[i + 1] = in[i];
    277     }
    278 
    279     *outlen = len + 1;
    280     return out;
    281 }
    282 
    283 static int cb_server_alpn(SSL *s, const unsigned char **out,
    284     unsigned char *outlen, const unsigned char *in,
    285     unsigned int inlen, void *arg)
    286 {
    287     unsigned char *protos;
    288     size_t protos_len;
    289     char *alpn_str = arg;
    290 
    291     protos = next_protos_parse(&protos_len, alpn_str);
    292     if (protos == NULL) {
    293         fprintf(stderr, "failed to parser ALPN server protocol string: %s\n",
    294             alpn_str);
    295         abort();
    296     }
    297 
    298     if (SSL_select_next_proto((unsigned char **)out, outlen, protos, protos_len, in,
    299             inlen)
    300         != OPENSSL_NPN_NEGOTIATED) {
    301         OPENSSL_free(protos);
    302         return SSL_TLSEXT_ERR_NOACK;
    303     }
    304 
    305     /*
    306      * Make a copy of the selected protocol which will be freed in
    307      * verify_alpn.
    308      */
    309     alpn_selected = OPENSSL_malloc(*outlen);
    310     if (alpn_selected == NULL) {
    311         fprintf(stderr, "failed to allocate memory\n");
    312         OPENSSL_free(protos);
    313         abort();
    314     }
    315     memcpy(alpn_selected, *out, *outlen);
    316     *out = alpn_selected;
    317 
    318     OPENSSL_free(protos);
    319     return SSL_TLSEXT_ERR_OK;
    320 }
    321 
    322 static int verify_alpn(SSL *client, SSL *server)
    323 {
    324     const unsigned char *client_proto, *server_proto;
    325     unsigned int client_proto_len = 0, server_proto_len = 0;
    326     SSL_get0_alpn_selected(client, &client_proto, &client_proto_len);
    327     SSL_get0_alpn_selected(server, &server_proto, &server_proto_len);
    328 
    329     OPENSSL_free(alpn_selected);
    330     alpn_selected = NULL;
    331 
    332     if (client_proto == NULL && client_proto_len != 0) {
    333         BIO_printf(bio_stdout,
    334             "Inconsistent SSL_get0_alpn_selected() for client!\n");
    335         goto err;
    336     }
    337 
    338     if (server_proto == NULL && server_proto_len != 0) {
    339         BIO_printf(bio_stdout,
    340             "Inconsistent SSL_get0_alpn_selected() for server!\n");
    341         goto err;
    342     }
    343 
    344     if (client_proto_len != server_proto_len) {
    345         BIO_printf(bio_stdout, "ALPN selected protocols differ!\n");
    346         goto err;
    347     }
    348 
    349     if (client_proto != NULL && memcmp(client_proto, server_proto, client_proto_len) != 0) {
    350         BIO_printf(bio_stdout, "ALPN selected protocols differ!\n");
    351         goto err;
    352     }
    353 
    354     if (client_proto_len > 0 && alpn_expected == NULL) {
    355         BIO_printf(bio_stdout, "ALPN unexpectedly negotiated\n");
    356         goto err;
    357     }
    358 
    359     if (alpn_expected != NULL && (client_proto_len != strlen(alpn_expected) || memcmp(client_proto, alpn_expected, client_proto_len) != 0)) {
    360         BIO_printf(bio_stdout,
    361             "ALPN selected protocols not equal to expected protocol: %s\n",
    362             alpn_expected);
    363         goto err;
    364     }
    365 
    366     return 0;
    367 
    368 err:
    369     BIO_printf(bio_stdout, "ALPN results: client: '");
    370     BIO_write(bio_stdout, client_proto, client_proto_len);
    371     BIO_printf(bio_stdout, "', server: '");
    372     BIO_write(bio_stdout, server_proto, server_proto_len);
    373     BIO_printf(bio_stdout, "'\n");
    374     BIO_printf(bio_stdout, "ALPN configured: client: '%s', server: '",
    375         alpn_client);
    376     if (SSL_get_SSL_CTX(server) == s_ctx2) {
    377         BIO_printf(bio_stdout, "%s'\n",
    378             alpn_server2);
    379     } else {
    380         BIO_printf(bio_stdout, "%s'\n",
    381             alpn_server);
    382     }
    383     return -1;
    384 }
    385 
    386 /*
    387  * WARNING : below extension types are *NOT* IETF assigned, and could
    388  * conflict if these types are reassigned and handled specially by OpenSSL
    389  * in the future
    390  */
    391 #define TACK_EXT_TYPE 62208
    392 #define CUSTOM_EXT_TYPE_0 1000
    393 #define CUSTOM_EXT_TYPE_1 1001
    394 #define CUSTOM_EXT_TYPE_2 1002
    395 #define CUSTOM_EXT_TYPE_3 1003
    396 
    397 static const char custom_ext_cli_string[] = "abc";
    398 static const char custom_ext_srv_string[] = "defg";
    399 
    400 /* These set from cmdline */
    401 static char *serverinfo_file = NULL;
    402 static int serverinfo_sct = 0;
    403 static int serverinfo_tack = 0;
    404 
    405 /* These set based on extension callbacks */
    406 static int serverinfo_sct_seen = 0;
    407 static int serverinfo_tack_seen = 0;
    408 static int serverinfo_other_seen = 0;
    409 
    410 /* This set from cmdline */
    411 static int custom_ext = 0;
    412 
    413 /* This set based on extension callbacks */
    414 static int custom_ext_error = 0;
    415 
    416 static int serverinfo_cli_parse_cb(SSL *s, unsigned int ext_type,
    417     const unsigned char *in, size_t inlen,
    418     int *al, void *arg)
    419 {
    420     if (ext_type == TLSEXT_TYPE_signed_certificate_timestamp)
    421         serverinfo_sct_seen++;
    422     else if (ext_type == TACK_EXT_TYPE)
    423         serverinfo_tack_seen++;
    424     else
    425         serverinfo_other_seen++;
    426     return 1;
    427 }
    428 
    429 static int verify_serverinfo(void)
    430 {
    431     if (serverinfo_sct != serverinfo_sct_seen)
    432         return -1;
    433     if (serverinfo_tack != serverinfo_tack_seen)
    434         return -1;
    435     if (serverinfo_other_seen)
    436         return -1;
    437     return 0;
    438 }
    439 
    440 /*-
    441  * Four test cases for custom extensions:
    442  * 0 - no ClientHello extension or ServerHello response
    443  * 1 - ClientHello with "abc", no response
    444  * 2 - ClientHello with "abc", empty response
    445  * 3 - ClientHello with "abc", "defg" response
    446  */
    447 
    448 static int custom_ext_0_cli_add_cb(SSL *s, unsigned int ext_type,
    449     const unsigned char **out,
    450     size_t *outlen, int *al, void *arg)
    451 {
    452     if (ext_type != CUSTOM_EXT_TYPE_0)
    453         custom_ext_error = 1;
    454     return 0; /* Don't send an extension */
    455 }
    456 
    457 static int custom_ext_0_cli_parse_cb(SSL *s, unsigned int ext_type,
    458     const unsigned char *in,
    459     size_t inlen, int *al, void *arg)
    460 {
    461     return 1;
    462 }
    463 
    464 static int custom_ext_1_cli_add_cb(SSL *s, unsigned int ext_type,
    465     const unsigned char **out,
    466     size_t *outlen, int *al, void *arg)
    467 {
    468     if (ext_type != CUSTOM_EXT_TYPE_1)
    469         custom_ext_error = 1;
    470     *out = (const unsigned char *)custom_ext_cli_string;
    471     *outlen = strlen(custom_ext_cli_string);
    472     return 1; /* Send "abc" */
    473 }
    474 
    475 static int custom_ext_1_cli_parse_cb(SSL *s, unsigned int ext_type,
    476     const unsigned char *in,
    477     size_t inlen, int *al, void *arg)
    478 {
    479     return 1;
    480 }
    481 
    482 static int custom_ext_2_cli_add_cb(SSL *s, unsigned int ext_type,
    483     const unsigned char **out,
    484     size_t *outlen, int *al, void *arg)
    485 {
    486     if (ext_type != CUSTOM_EXT_TYPE_2)
    487         custom_ext_error = 1;
    488     *out = (const unsigned char *)custom_ext_cli_string;
    489     *outlen = strlen(custom_ext_cli_string);
    490     return 1; /* Send "abc" */
    491 }
    492 
    493 static int custom_ext_2_cli_parse_cb(SSL *s, unsigned int ext_type,
    494     const unsigned char *in,
    495     size_t inlen, int *al, void *arg)
    496 {
    497     if (ext_type != CUSTOM_EXT_TYPE_2)
    498         custom_ext_error = 1;
    499     if (inlen != 0)
    500         custom_ext_error = 1; /* Should be empty response */
    501     return 1;
    502 }
    503 
    504 static int custom_ext_3_cli_add_cb(SSL *s, unsigned int ext_type,
    505     const unsigned char **out,
    506     size_t *outlen, int *al, void *arg)
    507 {
    508     if (ext_type != CUSTOM_EXT_TYPE_3)
    509         custom_ext_error = 1;
    510     *out = (const unsigned char *)custom_ext_cli_string;
    511     *outlen = strlen(custom_ext_cli_string);
    512     return 1; /* Send "abc" */
    513 }
    514 
    515 static int custom_ext_3_cli_parse_cb(SSL *s, unsigned int ext_type,
    516     const unsigned char *in,
    517     size_t inlen, int *al, void *arg)
    518 {
    519     if (ext_type != CUSTOM_EXT_TYPE_3)
    520         custom_ext_error = 1;
    521     if (inlen != strlen(custom_ext_srv_string))
    522         custom_ext_error = 1;
    523     if (memcmp(custom_ext_srv_string, in, inlen) != 0)
    524         custom_ext_error = 1; /* Check for "defg" */
    525     return 1;
    526 }
    527 
    528 /*
    529  * custom_ext_0_cli_add_cb returns 0 - the server won't receive a callback
    530  * for this extension
    531  */
    532 static int custom_ext_0_srv_parse_cb(SSL *s, unsigned int ext_type,
    533     const unsigned char *in,
    534     size_t inlen, int *al, void *arg)
    535 {
    536     custom_ext_error = 1;
    537     return 1;
    538 }
    539 
    540 /* 'add' callbacks are only called if the 'parse' callback is called */
    541 static int custom_ext_0_srv_add_cb(SSL *s, unsigned int ext_type,
    542     const unsigned char **out,
    543     size_t *outlen, int *al, void *arg)
    544 {
    545     /* Error: should not have been called */
    546     custom_ext_error = 1;
    547     return 0; /* Don't send an extension */
    548 }
    549 
    550 static int custom_ext_1_srv_parse_cb(SSL *s, unsigned int ext_type,
    551     const unsigned char *in,
    552     size_t inlen, int *al, void *arg)
    553 {
    554     if (ext_type != CUSTOM_EXT_TYPE_1)
    555         custom_ext_error = 1;
    556     /* Check for "abc" */
    557     if (inlen != strlen(custom_ext_cli_string))
    558         custom_ext_error = 1;
    559     if (memcmp(in, custom_ext_cli_string, inlen) != 0)
    560         custom_ext_error = 1;
    561     return 1;
    562 }
    563 
    564 static int custom_ext_1_srv_add_cb(SSL *s, unsigned int ext_type,
    565     const unsigned char **out,
    566     size_t *outlen, int *al, void *arg)
    567 {
    568     return 0; /* Don't send an extension */
    569 }
    570 
    571 static int custom_ext_2_srv_parse_cb(SSL *s, unsigned int ext_type,
    572     const unsigned char *in,
    573     size_t inlen, int *al, void *arg)
    574 {
    575     if (ext_type != CUSTOM_EXT_TYPE_2)
    576         custom_ext_error = 1;
    577     /* Check for "abc" */
    578     if (inlen != strlen(custom_ext_cli_string))
    579         custom_ext_error = 1;
    580     if (memcmp(in, custom_ext_cli_string, inlen) != 0)
    581         custom_ext_error = 1;
    582     return 1;
    583 }
    584 
    585 static int custom_ext_2_srv_add_cb(SSL *s, unsigned int ext_type,
    586     const unsigned char **out,
    587     size_t *outlen, int *al, void *arg)
    588 {
    589     *out = NULL;
    590     *outlen = 0;
    591     return 1; /* Send empty extension */
    592 }
    593 
    594 static int custom_ext_3_srv_parse_cb(SSL *s, unsigned int ext_type,
    595     const unsigned char *in,
    596     size_t inlen, int *al, void *arg)
    597 {
    598     if (ext_type != CUSTOM_EXT_TYPE_3)
    599         custom_ext_error = 1;
    600     /* Check for "abc" */
    601     if (inlen != strlen(custom_ext_cli_string))
    602         custom_ext_error = 1;
    603     if (memcmp(in, custom_ext_cli_string, inlen) != 0)
    604         custom_ext_error = 1;
    605     return 1;
    606 }
    607 
    608 static int custom_ext_3_srv_add_cb(SSL *s, unsigned int ext_type,
    609     const unsigned char **out,
    610     size_t *outlen, int *al, void *arg)
    611 {
    612     *out = (const unsigned char *)custom_ext_srv_string;
    613     *outlen = strlen(custom_ext_srv_string);
    614     return 1; /* Send "defg" */
    615 }
    616 
    617 static char *cipher = NULL;
    618 static char *ciphersuites = NULL;
    619 static int verbose = 0;
    620 static int debug = 0;
    621 
    622 int doit_localhost(SSL *s_ssl, SSL *c_ssl, int family,
    623     long bytes, clock_t *s_time, clock_t *c_time);
    624 int doit_biopair(SSL *s_ssl, SSL *c_ssl, long bytes, clock_t *s_time,
    625     clock_t *c_time);
    626 int doit(SSL *s_ssl, SSL *c_ssl, long bytes);
    627 
    628 static void sv_usage(void)
    629 {
    630     fprintf(stderr, "usage: ssltest [args ...]\n");
    631     fprintf(stderr, "\n");
    632     fprintf(stderr, " -server_auth  - check server certificate\n");
    633     fprintf(stderr, " -client_auth  - do client authentication\n");
    634     fprintf(stderr, " -v            - more output\n");
    635     fprintf(stderr, " -d            - debug output\n");
    636     fprintf(stderr, " -reuse        - use session-id reuse\n");
    637     fprintf(stderr, " -num <val>    - number of connections to perform\n");
    638     fprintf(stderr,
    639         " -bytes <val>  - number of bytes to swap between client/server\n");
    640 #ifndef OPENSSL_NO_DH
    641     fprintf(stderr,
    642         " -dhe512       - use 512 bit key for DHE (to test failure)\n");
    643     fprintf(stderr,
    644         " -dhe1024dsa   - use 1024 bit key (with 160-bit subprime) for DHE\n");
    645     fprintf(stderr,
    646         " -dhe2048      - use 2048 bit key (safe prime) for DHE (default, no-op)\n");
    647     fprintf(stderr,
    648         " -dhe4096      - use 4096 bit key (safe prime) for DHE\n");
    649 #endif
    650     fprintf(stderr, " -no_dhe       - disable DHE\n");
    651 #ifndef OPENSSL_NO_EC
    652     fprintf(stderr, " -no_ecdhe     - disable ECDHE\n");
    653 #endif
    654 #ifndef OPENSSL_NO_PSK
    655     fprintf(stderr, " -psk arg      - PSK in hex (without 0x)\n");
    656 #endif
    657 #ifndef OPENSSL_NO_SSL3
    658     fprintf(stderr, " -ssl3         - use SSLv3\n");
    659 #endif
    660 #ifndef OPENSSL_NO_TLS1
    661     fprintf(stderr, " -tls1         - use TLSv1\n");
    662 #endif
    663 #ifndef OPENSSL_NO_TLS1_1
    664     fprintf(stderr, " -tls1_1       - use TLSv1.1\n");
    665 #endif
    666 #ifndef OPENSSL_NO_TLS1_2
    667     fprintf(stderr, " -tls1_2       - use TLSv1.2\n");
    668 #endif
    669 #ifndef OPENSSL_NO_DTLS
    670     fprintf(stderr, " -dtls         - use DTLS\n");
    671 #ifndef OPENSSL_NO_DTLS1
    672     fprintf(stderr, " -dtls1        - use DTLSv1\n");
    673 #endif
    674 #ifndef OPENSSL_NO_DTLS1_2
    675     fprintf(stderr, " -dtls12       - use DTLSv1.2\n");
    676 #endif
    677 #endif
    678     fprintf(stderr, " -CApath arg   - PEM format directory of CA's\n");
    679     fprintf(stderr, " -CAfile arg   - PEM format file of CA's\n");
    680     fprintf(stderr, " -s_cert arg   - Server certificate file\n");
    681     fprintf(stderr,
    682         " -s_key arg    - Server key file (default: same as -cert)\n");
    683     fprintf(stderr, " -c_cert arg   - Client certificate file\n");
    684     fprintf(stderr,
    685         " -c_key arg    - Client key file (default: same as -c_cert)\n");
    686     fprintf(stderr, " -cipher arg   - The TLSv1.2 and below cipher list\n");
    687     fprintf(stderr, " -ciphersuites arg   - The TLSv1.3 ciphersuites\n");
    688     fprintf(stderr, " -bio_pair     - Use BIO pairs\n");
    689     fprintf(stderr, " -ipv4         - Use IPv4 connection on localhost\n");
    690     fprintf(stderr, " -ipv6         - Use IPv6 connection on localhost\n");
    691     fprintf(stderr, " -f            - Test even cases that can't work\n");
    692     fprintf(stderr,
    693         " -time         - measure processor time used by client and server\n");
    694     fprintf(stderr, " -zlib         - use zlib compression\n");
    695 #ifndef OPENSSL_NO_NEXTPROTONEG
    696     fprintf(stderr, " -npn_client - have client side offer NPN\n");
    697     fprintf(stderr, " -npn_server - have server side offer NPN\n");
    698     fprintf(stderr, " -npn_server_reject - have server reject NPN\n");
    699 #endif
    700     fprintf(stderr, " -serverinfo_file file - have server use this file\n");
    701     fprintf(stderr, " -serverinfo_sct  - have client offer and expect SCT\n");
    702     fprintf(stderr,
    703         " -serverinfo_tack - have client offer and expect TACK\n");
    704     fprintf(stderr,
    705         " -custom_ext - try various custom extension callbacks\n");
    706     fprintf(stderr, " -alpn_client <string> - have client side offer ALPN\n");
    707     fprintf(stderr, " -alpn_server <string> - have server side offer ALPN\n");
    708     fprintf(stderr, " -alpn_server1 <string> - alias for -alpn_server\n");
    709     fprintf(stderr, " -alpn_server2 <string> - have server side context 2 offer ALPN\n");
    710     fprintf(stderr,
    711         " -alpn_expected <string> - the ALPN protocol that should be negotiated\n");
    712     fprintf(stderr, " -server_min_proto <string> - Minimum version the server should support\n");
    713     fprintf(stderr, " -server_max_proto <string> - Maximum version the server should support\n");
    714     fprintf(stderr, " -client_min_proto <string> - Minimum version the client should support\n");
    715     fprintf(stderr, " -client_max_proto <string> - Maximum version the client should support\n");
    716     fprintf(stderr, " -should_negotiate <string> - The version that should be negotiated, fail-client or fail-server\n");
    717 #ifndef OPENSSL_NO_CT
    718     fprintf(stderr, " -noct         - no certificate transparency\n");
    719     fprintf(stderr, " -requestct    - request certificate transparency\n");
    720     fprintf(stderr, " -requirect    - require certificate transparency\n");
    721 #endif
    722     fprintf(stderr, " -sn_client <string>  - have client request this servername\n");
    723     fprintf(stderr, " -sn_server1 <string> - have server context 1 respond to this servername\n");
    724     fprintf(stderr, " -sn_server2 <string> - have server context 2 respond to this servername\n");
    725     fprintf(stderr, " -sn_expect1          - expected server 1\n");
    726     fprintf(stderr, " -sn_expect2          - expected server 2\n");
    727     fprintf(stderr, " -server_sess_out <file>    - Save the server session to a file\n");
    728     fprintf(stderr, " -server_sess_in <file>     - Read the server session from a file\n");
    729     fprintf(stderr, " -client_sess_out <file>    - Save the client session to a file\n");
    730     fprintf(stderr, " -client_sess_in <file>     - Read the client session from a file\n");
    731     fprintf(stderr, " -should_reuse <number>     - The expected state of reusing the session\n");
    732     fprintf(stderr, " -no_ticket    - do not issue TLS session ticket\n");
    733     fprintf(stderr, " -client_ktls  - try to enable client KTLS\n");
    734     fprintf(stderr, " -server_ktls  - try to enable server KTLS\n");
    735     fprintf(stderr, " -provider <name>    - Load the given provider into the library context\n");
    736     fprintf(stderr, " -config <cnf>    - Load the given config file into the library context\n");
    737 }
    738 
    739 static void print_key_details(BIO *out, EVP_PKEY *key)
    740 {
    741     int keyid = EVP_PKEY_get_id(key);
    742 
    743 #ifndef OPENSSL_NO_EC
    744     if (keyid == EVP_PKEY_EC) {
    745         char group[80];
    746         size_t size;
    747 
    748         if (!EVP_PKEY_get_group_name(key, group, sizeof(group), &size))
    749             strcpy(group, "unknown group");
    750         BIO_printf(out, "%d bits EC (%s)", EVP_PKEY_get_bits(key), group);
    751     } else
    752 #endif
    753     {
    754         const char *algname;
    755         switch (keyid) {
    756         case EVP_PKEY_RSA:
    757             algname = "RSA";
    758             break;
    759         case EVP_PKEY_DSA:
    760             algname = "DSA";
    761             break;
    762         case EVP_PKEY_DH:
    763             algname = "DH";
    764             break;
    765         default:
    766             algname = OBJ_nid2sn(keyid);
    767             break;
    768         }
    769         BIO_printf(out, "%d bits %s", EVP_PKEY_get_bits(key), algname);
    770     }
    771 }
    772 
    773 static void print_details(SSL *c_ssl, const char *prefix)
    774 {
    775     const SSL_CIPHER *ciph;
    776     int mdnid;
    777     X509 *cert;
    778     EVP_PKEY *pkey;
    779 
    780     ciph = SSL_get_current_cipher(c_ssl);
    781     BIO_printf(bio_stdout, "%s%s, cipher %s %s",
    782         prefix,
    783         SSL_get_version(c_ssl),
    784         SSL_CIPHER_get_version(ciph), SSL_CIPHER_get_name(ciph));
    785     cert = SSL_get0_peer_certificate(c_ssl);
    786     if (cert != NULL) {
    787         EVP_PKEY *pubkey = X509_get0_pubkey(cert);
    788 
    789         if (pubkey != NULL) {
    790             BIO_puts(bio_stdout, ", ");
    791             print_key_details(bio_stdout, pubkey);
    792         }
    793     }
    794     if (SSL_get_peer_tmp_key(c_ssl, &pkey)) {
    795         BIO_puts(bio_stdout, ", temp key: ");
    796         print_key_details(bio_stdout, pkey);
    797         EVP_PKEY_free(pkey);
    798     }
    799     if (SSL_get_peer_signature_nid(c_ssl, &mdnid))
    800         BIO_printf(bio_stdout, ", digest=%s", OBJ_nid2sn(mdnid));
    801     BIO_printf(bio_stdout, "\n");
    802 }
    803 
    804 /*
    805  * protocol_from_string - converts a protocol version string to a number
    806  *
    807  * Returns -1 on failure or the version on success
    808  */
    809 static int protocol_from_string(const char *value)
    810 {
    811     struct protocol_versions {
    812         const char *name;
    813         int version;
    814     };
    815     static const struct protocol_versions versions[] = {
    816         { "ssl3", SSL3_VERSION },
    817         { "tls1", TLS1_VERSION },
    818         { "tls1.1", TLS1_1_VERSION },
    819         { "tls1.2", TLS1_2_VERSION },
    820         { "tls1.3", TLS1_3_VERSION },
    821         { "dtls1", DTLS1_VERSION },
    822         { "dtls1.2", DTLS1_2_VERSION }
    823     };
    824     size_t i;
    825     size_t n = OSSL_NELEM(versions);
    826 
    827     for (i = 0; i < n; i++)
    828         if (strcmp(versions[i].name, value) == 0)
    829             return versions[i].version;
    830     return -1;
    831 }
    832 
    833 static SSL_SESSION *read_session(const char *filename)
    834 {
    835     SSL_SESSION *sess;
    836     BIO *f = BIO_new_file(filename, "r");
    837 
    838     if (f == NULL) {
    839         BIO_printf(bio_err, "Can't open session file %s\n", filename);
    840         ERR_print_errors(bio_err);
    841         return NULL;
    842     }
    843     sess = PEM_read_bio_SSL_SESSION(f, NULL, 0, NULL);
    844     if (sess == NULL) {
    845         BIO_printf(bio_err, "Can't parse session file %s\n", filename);
    846         ERR_print_errors(bio_err);
    847     }
    848     BIO_free(f);
    849     return sess;
    850 }
    851 
    852 static int write_session(const char *filename, SSL_SESSION *sess)
    853 {
    854     BIO *f;
    855 
    856     if (sess == NULL) {
    857         BIO_printf(bio_err, "No session information\n");
    858         return 0;
    859     }
    860 
    861     f = BIO_new_file(filename, "w");
    862     if (f == NULL) {
    863         BIO_printf(bio_err, "Can't open session file %s\n", filename);
    864         ERR_print_errors(bio_err);
    865         return 0;
    866     }
    867     PEM_write_bio_SSL_SESSION(f, sess);
    868     BIO_free(f);
    869     return 1;
    870 }
    871 
    872 /*
    873  * set_protocol_version - Sets protocol version minimum or maximum
    874  *
    875  * Returns 0 on failure and 1 on success
    876  */
    877 static int set_protocol_version(const char *version, SSL *ssl, int setting)
    878 {
    879     if (version != NULL) {
    880         int ver = protocol_from_string(version);
    881         if (ver < 0) {
    882             BIO_printf(bio_err, "Error parsing: %s\n", version);
    883             return 0;
    884         }
    885         return SSL_ctrl(ssl, setting, ver, NULL);
    886     }
    887     return 1;
    888 }
    889 
    890 int main(int argc, char *argv[])
    891 {
    892     const char *CApath = NULL, *CAfile = NULL;
    893     int badop = 0;
    894     enum { BIO_MEM,
    895         BIO_PAIR,
    896         BIO_IPV4,
    897         BIO_IPV6 } bio_type
    898         = BIO_MEM;
    899     int force = 0;
    900     int dtls1 = 0, dtls12 = 0, dtls = 0, tls1 = 0, tls1_1 = 0, tls1_2 = 0, ssl3 = 0;
    901     int ret = EXIT_FAILURE;
    902     int client_auth = 0;
    903     int server_auth = 0, i;
    904     struct app_verify_arg app_verify_arg = { APP_CALLBACK_STRING, 0 };
    905     SSL_CTX *c_ctx = NULL;
    906     const SSL_METHOD *meth = NULL;
    907     SSL *c_ssl = NULL;
    908     SSL *s_ssl = NULL;
    909     int number = 1, reuse = 0;
    910     int should_reuse = -1;
    911     int no_ticket = 0;
    912     int client_ktls = 0, server_ktls = 0;
    913     long bytes = 256L;
    914 #ifndef OPENSSL_NO_DH
    915     EVP_PKEY *dhpkey;
    916     int dhe512 = 0, dhe1024dsa = 0, dhe4096 = 0;
    917     int no_dhe = 0;
    918 #endif
    919     int no_psk = 0;
    920     int print_time = 0;
    921     clock_t s_time = 0, c_time = 0;
    922 #ifndef OPENSSL_NO_COMP
    923     int n, comp = 0;
    924     COMP_METHOD *cm = NULL;
    925     STACK_OF(SSL_COMP) *ssl_comp_methods = NULL;
    926 #endif
    927     int no_protocol;
    928     int min_version = 0, max_version = 0;
    929 #ifndef OPENSSL_NO_CT
    930     /*
    931      * Disable CT validation by default, because it will interfere with
    932      * anything using custom extension handlers to deal with SCT extensions.
    933      */
    934     int ct_validation = 0;
    935 #endif
    936     SSL_CONF_CTX *s_cctx = NULL, *c_cctx = NULL, *s_cctx2 = NULL;
    937     STACK_OF(OPENSSL_STRING) *conf_args = NULL;
    938     char *arg = NULL, *argn = NULL;
    939     const char *provider = NULL, *config = NULL;
    940     OSSL_PROVIDER *thisprov = NULL, *defctxnull = NULL;
    941     OSSL_LIB_CTX *libctx = NULL;
    942 
    943     verbose = 0;
    944     debug = 0;
    945 
    946     test_open_streams();
    947 
    948     bio_stdout = BIO_new_fp(stdout, BIO_NOCLOSE | BIO_FP_TEXT);
    949 
    950     s_cctx = SSL_CONF_CTX_new();
    951     s_cctx2 = SSL_CONF_CTX_new();
    952     c_cctx = SSL_CONF_CTX_new();
    953 
    954     if (!s_cctx || !c_cctx || !s_cctx2) {
    955         ERR_print_errors(bio_err);
    956         goto end;
    957     }
    958 
    959     SSL_CONF_CTX_set_flags(s_cctx,
    960         SSL_CONF_FLAG_CMDLINE | SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE | SSL_CONF_FLAG_REQUIRE_PRIVATE);
    961     SSL_CONF_CTX_set_flags(s_cctx2,
    962         SSL_CONF_FLAG_CMDLINE | SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE | SSL_CONF_FLAG_REQUIRE_PRIVATE);
    963     if (!SSL_CONF_CTX_set1_prefix(s_cctx, "-s_")) {
    964         ERR_print_errors(bio_err);
    965         goto end;
    966     }
    967     if (!SSL_CONF_CTX_set1_prefix(s_cctx2, "-s_")) {
    968         ERR_print_errors(bio_err);
    969         goto end;
    970     }
    971 
    972     SSL_CONF_CTX_set_flags(c_cctx,
    973         SSL_CONF_FLAG_CMDLINE | SSL_CONF_FLAG_CLIENT | SSL_CONF_FLAG_CERTIFICATE | SSL_CONF_FLAG_REQUIRE_PRIVATE);
    974     if (!SSL_CONF_CTX_set1_prefix(c_cctx, "-c_")) {
    975         ERR_print_errors(bio_err);
    976         goto end;
    977     }
    978 
    979     argc--;
    980     argv++;
    981 
    982     while (argc >= 1) {
    983         if (strcmp(*argv, "-F") == 0) {
    984             fprintf(stderr,
    985                 "not compiled with FIPS support, so exiting without running.\n");
    986             ret = EXIT_SUCCESS;
    987             goto end;
    988         } else if (strcmp(*argv, "-server_auth") == 0)
    989             server_auth = 1;
    990         else if (strcmp(*argv, "-client_auth") == 0)
    991             client_auth = 1;
    992         else if (strcmp(*argv, "-v") == 0)
    993             verbose = 1;
    994         else if (strcmp(*argv, "-d") == 0)
    995             debug = 1;
    996         else if (strcmp(*argv, "-reuse") == 0)
    997             reuse = 1;
    998         else if (strcmp(*argv, "-no_dhe") == 0)
    999 #ifdef OPENSSL_NO_DH
   1000             /* unused in this case */;
   1001 #else
   1002             no_dhe = 1;
   1003         else if (strcmp(*argv, "-dhe512") == 0)
   1004             dhe512 = 1;
   1005         else if (strcmp(*argv, "-dhe1024dsa") == 0)
   1006             dhe1024dsa = 1;
   1007         else if (strcmp(*argv, "-dhe4096") == 0)
   1008             dhe4096 = 1;
   1009 #endif
   1010         else if (strcmp(*argv, "-no_ecdhe") == 0)
   1011             /* obsolete */;
   1012         else if (strcmp(*argv, "-psk") == 0) {
   1013             if (--argc < 1)
   1014                 goto bad;
   1015             psk_key = *(++argv);
   1016 #ifndef OPENSSL_NO_PSK
   1017             if (strspn(psk_key, "abcdefABCDEF1234567890") != strlen(psk_key)) {
   1018                 BIO_printf(bio_err, "Not a hex number '%s'\n", *argv);
   1019                 goto bad;
   1020             }
   1021 #else
   1022             no_psk = 1;
   1023 #endif
   1024         } else if (strcmp(*argv, "-tls1_2") == 0) {
   1025             tls1_2 = 1;
   1026         } else if (strcmp(*argv, "-tls1_1") == 0) {
   1027             tls1_1 = 1;
   1028         } else if (strcmp(*argv, "-tls1") == 0) {
   1029             tls1 = 1;
   1030         } else if (strcmp(*argv, "-ssl3") == 0) {
   1031             ssl3 = 1;
   1032         } else if (strcmp(*argv, "-dtls1") == 0) {
   1033             dtls1 = 1;
   1034         } else if (strcmp(*argv, "-dtls12") == 0) {
   1035             dtls12 = 1;
   1036         } else if (strcmp(*argv, "-dtls") == 0) {
   1037             dtls = 1;
   1038         } else if (HAS_PREFIX(*argv, "-num")) {
   1039             if (--argc < 1)
   1040                 goto bad;
   1041             number = atoi(*(++argv));
   1042             if (number == 0)
   1043                 number = 1;
   1044         } else if (strcmp(*argv, "-bytes") == 0) {
   1045             if (--argc < 1)
   1046                 goto bad;
   1047             bytes = atol(*(++argv));
   1048             if (bytes == 0L)
   1049                 bytes = 1L;
   1050             i = strlen(argv[0]);
   1051             if (argv[0][i - 1] == 'k')
   1052                 bytes *= 1024L;
   1053             if (argv[0][i - 1] == 'm')
   1054                 bytes *= 1024L * 1024L;
   1055         } else if (strcmp(*argv, "-cipher") == 0) {
   1056             if (--argc < 1)
   1057                 goto bad;
   1058             cipher = *(++argv);
   1059         } else if (strcmp(*argv, "-ciphersuites") == 0) {
   1060             if (--argc < 1)
   1061                 goto bad;
   1062             ciphersuites = *(++argv);
   1063         } else if (strcmp(*argv, "-CApath") == 0) {
   1064             if (--argc < 1)
   1065                 goto bad;
   1066             CApath = *(++argv);
   1067         } else if (strcmp(*argv, "-CAfile") == 0) {
   1068             if (--argc < 1)
   1069                 goto bad;
   1070             CAfile = *(++argv);
   1071         } else if (strcmp(*argv, "-bio_pair") == 0) {
   1072             bio_type = BIO_PAIR;
   1073         }
   1074 #ifndef OPENSSL_NO_SOCK
   1075         else if (strcmp(*argv, "-ipv4") == 0) {
   1076             bio_type = BIO_IPV4;
   1077         } else if (strcmp(*argv, "-ipv6") == 0) {
   1078             bio_type = BIO_IPV6;
   1079         }
   1080 #endif
   1081         else if (strcmp(*argv, "-f") == 0) {
   1082             force = 1;
   1083         } else if (strcmp(*argv, "-time") == 0) {
   1084             print_time = 1;
   1085         }
   1086 #ifndef OPENSSL_NO_CT
   1087         else if (strcmp(*argv, "-noct") == 0) {
   1088             ct_validation = 0;
   1089         } else if (strcmp(*argv, "-ct") == 0) {
   1090             ct_validation = 1;
   1091         }
   1092 #endif
   1093 #ifndef OPENSSL_NO_COMP
   1094         else if (strcmp(*argv, "-zlib") == 0) {
   1095             comp = COMP_ZLIB;
   1096         }
   1097 #endif
   1098         else if (strcmp(*argv, "-app_verify") == 0) {
   1099             app_verify_arg.app_verify = 1;
   1100         }
   1101 #ifndef OPENSSL_NO_NEXTPROTONEG
   1102         else if (strcmp(*argv, "-npn_client") == 0) {
   1103             npn_client = 1;
   1104         } else if (strcmp(*argv, "-npn_server") == 0) {
   1105             npn_server = 1;
   1106         } else if (strcmp(*argv, "-npn_server_reject") == 0) {
   1107             npn_server_reject = 1;
   1108         }
   1109 #endif
   1110         else if (strcmp(*argv, "-serverinfo_sct") == 0) {
   1111             serverinfo_sct = 1;
   1112         } else if (strcmp(*argv, "-serverinfo_tack") == 0) {
   1113             serverinfo_tack = 1;
   1114         } else if (strcmp(*argv, "-serverinfo_file") == 0) {
   1115             if (--argc < 1)
   1116                 goto bad;
   1117             serverinfo_file = *(++argv);
   1118         } else if (strcmp(*argv, "-custom_ext") == 0) {
   1119             custom_ext = 1;
   1120         } else if (strcmp(*argv, "-alpn_client") == 0) {
   1121             if (--argc < 1)
   1122                 goto bad;
   1123             alpn_client = *(++argv);
   1124         } else if (strcmp(*argv, "-alpn_server") == 0 || strcmp(*argv, "-alpn_server1") == 0) {
   1125             if (--argc < 1)
   1126                 goto bad;
   1127             alpn_server = *(++argv);
   1128         } else if (strcmp(*argv, "-alpn_server2") == 0) {
   1129             if (--argc < 1)
   1130                 goto bad;
   1131             alpn_server2 = *(++argv);
   1132         } else if (strcmp(*argv, "-alpn_expected") == 0) {
   1133             if (--argc < 1)
   1134                 goto bad;
   1135             alpn_expected = *(++argv);
   1136         } else if (strcmp(*argv, "-server_min_proto") == 0) {
   1137             if (--argc < 1)
   1138                 goto bad;
   1139             server_min_proto = *(++argv);
   1140         } else if (strcmp(*argv, "-server_max_proto") == 0) {
   1141             if (--argc < 1)
   1142                 goto bad;
   1143             server_max_proto = *(++argv);
   1144         } else if (strcmp(*argv, "-client_min_proto") == 0) {
   1145             if (--argc < 1)
   1146                 goto bad;
   1147             client_min_proto = *(++argv);
   1148         } else if (strcmp(*argv, "-client_max_proto") == 0) {
   1149             if (--argc < 1)
   1150                 goto bad;
   1151             client_max_proto = *(++argv);
   1152         } else if (strcmp(*argv, "-should_negotiate") == 0) {
   1153             if (--argc < 1)
   1154                 goto bad;
   1155             should_negotiate = *(++argv);
   1156         } else if (strcmp(*argv, "-sn_client") == 0) {
   1157             if (--argc < 1)
   1158                 goto bad;
   1159             sn_client = *(++argv);
   1160         } else if (strcmp(*argv, "-sn_server1") == 0) {
   1161             if (--argc < 1)
   1162                 goto bad;
   1163             sn_server1 = *(++argv);
   1164         } else if (strcmp(*argv, "-sn_server2") == 0) {
   1165             if (--argc < 1)
   1166                 goto bad;
   1167             sn_server2 = *(++argv);
   1168         } else if (strcmp(*argv, "-sn_expect1") == 0) {
   1169             sn_expect = 1;
   1170         } else if (strcmp(*argv, "-sn_expect2") == 0) {
   1171             sn_expect = 2;
   1172         } else if (strcmp(*argv, "-server_sess_out") == 0) {
   1173             if (--argc < 1)
   1174                 goto bad;
   1175             server_sess_out = *(++argv);
   1176         } else if (strcmp(*argv, "-server_sess_in") == 0) {
   1177             if (--argc < 1)
   1178                 goto bad;
   1179             server_sess_in = *(++argv);
   1180         } else if (strcmp(*argv, "-client_sess_out") == 0) {
   1181             if (--argc < 1)
   1182                 goto bad;
   1183             client_sess_out = *(++argv);
   1184         } else if (strcmp(*argv, "-client_sess_in") == 0) {
   1185             if (--argc < 1)
   1186                 goto bad;
   1187             client_sess_in = *(++argv);
   1188         } else if (strcmp(*argv, "-should_reuse") == 0) {
   1189             if (--argc < 1)
   1190                 goto bad;
   1191             should_reuse = !!atoi(*(++argv));
   1192         } else if (strcmp(*argv, "-no_ticket") == 0) {
   1193             no_ticket = 1;
   1194         } else if (strcmp(*argv, "-client_ktls") == 0) {
   1195             client_ktls = 1;
   1196         } else if (strcmp(*argv, "-server_ktls") == 0) {
   1197             server_ktls = 1;
   1198         } else if (strcmp(*argv, "-provider") == 0) {
   1199             if (--argc < 1)
   1200                 goto bad;
   1201             provider = *(++argv);
   1202         } else if (strcmp(*argv, "-config") == 0) {
   1203             if (--argc < 1)
   1204                 goto bad;
   1205             config = *(++argv);
   1206         } else {
   1207             int rv;
   1208             arg = argv[0];
   1209             argn = argv[1];
   1210             /* Try to process command using SSL_CONF */
   1211             rv = SSL_CONF_cmd_argv(c_cctx, &argc, &argv);
   1212             /* If not processed try server */
   1213             if (rv == 0)
   1214                 rv = SSL_CONF_cmd_argv(s_cctx, &argc, &argv);
   1215             /* Recognised: store it for later use */
   1216             if (rv > 0) {
   1217                 if (rv == 1)
   1218                     argn = NULL;
   1219                 if (!conf_args) {
   1220                     conf_args = sk_OPENSSL_STRING_new_null();
   1221                     if (!conf_args)
   1222                         goto end;
   1223                 }
   1224                 if (!sk_OPENSSL_STRING_push(conf_args, arg))
   1225                     goto end;
   1226                 if (!sk_OPENSSL_STRING_push(conf_args, argn))
   1227                     goto end;
   1228                 continue;
   1229             }
   1230             if (rv == -3)
   1231                 BIO_printf(bio_err, "Missing argument for %s\n", arg);
   1232             else if (rv < 0)
   1233                 BIO_printf(bio_err, "Error with command %s\n", arg);
   1234             else if (rv == 0)
   1235                 BIO_printf(bio_err, "unknown option %s\n", arg);
   1236             badop = 1;
   1237             break;
   1238         }
   1239         argc--;
   1240         argv++;
   1241     }
   1242     if (badop) {
   1243     bad:
   1244         sv_usage();
   1245         goto end;
   1246     }
   1247 
   1248     if (ssl3 + tls1 + tls1_1 + tls1_2 + dtls + dtls1 + dtls12 > 1) {
   1249         fprintf(stderr, "At most one of -ssl3, -tls1, -tls1_1, -tls1_2, -dtls, -dtls1 or -dtls12 should "
   1250                         "be requested.\n");
   1251         goto end;
   1252     }
   1253 
   1254 #ifdef OPENSSL_NO_SSL3
   1255     if (ssl3)
   1256         no_protocol = 1;
   1257     else
   1258 #endif
   1259 #ifdef OPENSSL_NO_TLS1
   1260         if (tls1)
   1261         no_protocol = 1;
   1262     else
   1263 #endif
   1264 #ifdef OPENSSL_NO_TLS1_1
   1265         if (tls1_1)
   1266         no_protocol = 1;
   1267     else
   1268 #endif
   1269 #ifdef OPENSSL_NO_TLS1_2
   1270         if (tls1_2)
   1271         no_protocol = 1;
   1272     else
   1273 #endif
   1274 #if defined(OPENSSL_NO_DTLS) || defined(OPENSSL_NO_DTLS1)
   1275         if (dtls1)
   1276         no_protocol = 1;
   1277     else
   1278 #endif
   1279 #if defined(OPENSSL_NO_DTLS) || defined(OPENSSL_NO_DTLS1_2)
   1280         if (dtls12)
   1281         no_protocol = 1;
   1282     else
   1283 #endif
   1284         no_protocol = 0;
   1285 
   1286     /*
   1287      * Testing was requested for a compiled-out protocol (e.g. SSLv3).
   1288      * Ideally, we would error out, but the generic test wrapper can't know
   1289      * when to expect failure. So we do nothing and return success.
   1290      */
   1291     if (no_protocol) {
   1292         fprintf(stderr, "Testing was requested for a disabled protocol. "
   1293                         "Skipping tests.\n");
   1294         ret = EXIT_SUCCESS;
   1295         goto end;
   1296     }
   1297 
   1298     if (!ssl3 && !tls1 && !tls1_1 && !tls1_2 && !dtls && !dtls1 && !dtls12 && number > 1
   1299         && !reuse && !force) {
   1300         fprintf(stderr, "This case cannot work.  Use -f to perform "
   1301                         "the test anyway (and\n-d to see what happens), "
   1302                         "or add one of -ssl3, -tls1, -tls1_1, -tls1_2, -dtls, -dtls1, -dtls12, -reuse\n"
   1303                         "to avoid protocol mismatch.\n");
   1304         goto end;
   1305     }
   1306 
   1307     if (print_time) {
   1308         if (bio_type == BIO_MEM) {
   1309             fprintf(stderr, "Using BIO pair (-bio_pair)\n");
   1310             bio_type = BIO_PAIR;
   1311         }
   1312         if (number < 50 && !force)
   1313             fprintf(stderr,
   1314                 "Warning: For accurate timings, use more connections (e.g. -num 1000)\n");
   1315     }
   1316 
   1317 #ifndef OPENSSL_NO_COMP
   1318     if (comp == COMP_ZLIB)
   1319         cm = COMP_zlib();
   1320     if (cm != NULL) {
   1321         if (SSL_COMP_add_compression_method(comp, cm) != 0) {
   1322             fprintf(stderr, "Failed to add compression method\n");
   1323             ERR_print_errors_fp(stderr);
   1324         }
   1325     } else {
   1326         fprintf(stderr,
   1327             "Warning: %s compression not supported\n",
   1328             comp == COMP_ZLIB ? "zlib" : "unknown");
   1329         ERR_print_errors_fp(stderr);
   1330     }
   1331     ssl_comp_methods = SSL_COMP_get_compression_methods();
   1332     n = sk_SSL_COMP_num(ssl_comp_methods);
   1333     if (n) {
   1334         int j;
   1335         printf("Available compression methods:");
   1336         for (j = 0; j < n; j++) {
   1337             const SSL_COMP *c = sk_SSL_COMP_value(ssl_comp_methods, j);
   1338             printf("  %s:%d", SSL_COMP_get0_name(c), SSL_COMP_get_id(c));
   1339         }
   1340         printf("\n");
   1341     }
   1342 #endif
   1343 
   1344 #ifndef OPENSSL_NO_TLS
   1345     meth = TLS_method();
   1346     if (ssl3) {
   1347         min_version = SSL3_VERSION;
   1348         max_version = SSL3_VERSION;
   1349     } else if (tls1) {
   1350         min_version = TLS1_VERSION;
   1351         max_version = TLS1_VERSION;
   1352     } else if (tls1_1) {
   1353         min_version = TLS1_1_VERSION;
   1354         max_version = TLS1_1_VERSION;
   1355     } else if (tls1_2) {
   1356         min_version = TLS1_2_VERSION;
   1357         max_version = TLS1_2_VERSION;
   1358     } else {
   1359         min_version = 0;
   1360 #if defined(OPENSSL_NO_EC) && defined(OPENSSL_NO_DH)
   1361         /* We only have ec and dh based built-in groups for TLSv1.3 */
   1362         max_version = TLS1_2_VERSION;
   1363 #else
   1364         max_version = 0;
   1365 #endif
   1366     }
   1367 #endif
   1368 #ifndef OPENSSL_NO_DTLS
   1369     if (dtls || dtls1 || dtls12) {
   1370         meth = DTLS_method();
   1371         if (dtls1) {
   1372             min_version = DTLS1_VERSION;
   1373             max_version = DTLS1_VERSION;
   1374         } else if (dtls12) {
   1375             min_version = DTLS1_2_VERSION;
   1376             max_version = DTLS1_2_VERSION;
   1377         } else {
   1378             min_version = 0;
   1379             max_version = 0;
   1380         }
   1381     }
   1382 #endif
   1383 
   1384     if (provider != NULL
   1385         && !test_get_libctx(&libctx, &defctxnull, config, &thisprov, provider))
   1386         goto end;
   1387 
   1388     c_ctx = SSL_CTX_new_ex(libctx, NULL, meth);
   1389     s_ctx = SSL_CTX_new_ex(libctx, NULL, meth);
   1390     s_ctx2 = SSL_CTX_new_ex(libctx, NULL, meth); /* no SSL_CTX_dup! */
   1391     if ((c_ctx == NULL) || (s_ctx == NULL) || (s_ctx2 == NULL)) {
   1392         ERR_print_errors(bio_err);
   1393         goto end;
   1394     }
   1395     /*
   1396      * Since we will use low security ciphersuites and keys for testing set
   1397      * security level to zero by default. Tests can override this by adding
   1398      * "@SECLEVEL=n" to the cipher string.
   1399      */
   1400     SSL_CTX_set_security_level(c_ctx, 0);
   1401     SSL_CTX_set_security_level(s_ctx, 0);
   1402     SSL_CTX_set_security_level(s_ctx2, 0);
   1403 
   1404     if (no_ticket) {
   1405         SSL_CTX_set_options(c_ctx, SSL_OP_NO_TICKET);
   1406         SSL_CTX_set_options(s_ctx, SSL_OP_NO_TICKET);
   1407     }
   1408 
   1409     if (SSL_CTX_set_min_proto_version(c_ctx, min_version) == 0)
   1410         goto end;
   1411     if (SSL_CTX_set_max_proto_version(c_ctx, max_version) == 0)
   1412         goto end;
   1413     if (SSL_CTX_set_min_proto_version(s_ctx, min_version) == 0)
   1414         goto end;
   1415     if (SSL_CTX_set_max_proto_version(s_ctx, max_version) == 0)
   1416         goto end;
   1417 
   1418     if (cipher != NULL) {
   1419         if (strcmp(cipher, "") == 0) {
   1420             if (!SSL_CTX_set_cipher_list(c_ctx, cipher)) {
   1421                 if (ERR_GET_REASON(ERR_peek_error()) == SSL_R_NO_CIPHER_MATCH) {
   1422                     ERR_clear_error();
   1423                 } else {
   1424                     ERR_print_errors(bio_err);
   1425                     goto end;
   1426                 }
   1427             } else {
   1428                 /* Should have failed when clearing all TLSv1.2 ciphers. */
   1429                 fprintf(stderr, "CLEARING ALL TLSv1.2 CIPHERS SHOULD FAIL\n");
   1430                 goto end;
   1431             }
   1432 
   1433             if (!SSL_CTX_set_cipher_list(s_ctx, cipher)) {
   1434                 if (ERR_GET_REASON(ERR_peek_error()) == SSL_R_NO_CIPHER_MATCH) {
   1435                     ERR_clear_error();
   1436                 } else {
   1437                     ERR_print_errors(bio_err);
   1438                     goto end;
   1439                 }
   1440             } else {
   1441                 /* Should have failed when clearing all TLSv1.2 ciphers. */
   1442                 fprintf(stderr, "CLEARING ALL TLSv1.2 CIPHERS SHOULD FAIL\n");
   1443                 goto end;
   1444             }
   1445 
   1446             if (!SSL_CTX_set_cipher_list(s_ctx2, cipher)) {
   1447                 if (ERR_GET_REASON(ERR_peek_error()) == SSL_R_NO_CIPHER_MATCH) {
   1448                     ERR_clear_error();
   1449                 } else {
   1450                     ERR_print_errors(bio_err);
   1451                     goto end;
   1452                 }
   1453             } else {
   1454                 /* Should have failed when clearing all TLSv1.2 ciphers. */
   1455                 fprintf(stderr, "CLEARING ALL TLSv1.2 CIPHERS SHOULD FAIL\n");
   1456                 goto end;
   1457             }
   1458         } else {
   1459             if (!SSL_CTX_set_cipher_list(c_ctx, cipher)
   1460                 || !SSL_CTX_set_cipher_list(s_ctx, cipher)
   1461                 || !SSL_CTX_set_cipher_list(s_ctx2, cipher)) {
   1462                 ERR_print_errors(bio_err);
   1463                 goto end;
   1464             }
   1465         }
   1466     }
   1467     if (ciphersuites != NULL) {
   1468         if (!SSL_CTX_set_ciphersuites(c_ctx, ciphersuites)
   1469             || !SSL_CTX_set_ciphersuites(s_ctx, ciphersuites)
   1470             || !SSL_CTX_set_ciphersuites(s_ctx2, ciphersuites)) {
   1471             ERR_print_errors(bio_err);
   1472             goto end;
   1473         }
   1474     }
   1475 
   1476 #ifndef OPENSSL_NO_CT
   1477     if (ct_validation && !SSL_CTX_enable_ct(c_ctx, SSL_CT_VALIDATION_STRICT)) {
   1478         ERR_print_errors(bio_err);
   1479         goto end;
   1480     }
   1481 #endif
   1482 
   1483     /* Process SSL_CONF arguments */
   1484     SSL_CONF_CTX_set_ssl_ctx(c_cctx, c_ctx);
   1485     SSL_CONF_CTX_set_ssl_ctx(s_cctx, s_ctx);
   1486     SSL_CONF_CTX_set_ssl_ctx(s_cctx2, s_ctx2);
   1487 
   1488     for (i = 0; i < sk_OPENSSL_STRING_num(conf_args); i += 2) {
   1489         int rv;
   1490         arg = sk_OPENSSL_STRING_value(conf_args, i);
   1491         argn = sk_OPENSSL_STRING_value(conf_args, i + 1);
   1492         rv = SSL_CONF_cmd(c_cctx, arg, argn);
   1493         /* If not recognised use server context */
   1494         if (rv == -2) {
   1495             rv = SSL_CONF_cmd(s_cctx2, arg, argn);
   1496             if (rv > 0)
   1497                 rv = SSL_CONF_cmd(s_cctx, arg, argn);
   1498         }
   1499         if (rv <= 0) {
   1500             BIO_printf(bio_err, "Error processing %s %s\n",
   1501                 arg, argn ? argn : "");
   1502             ERR_print_errors(bio_err);
   1503             goto end;
   1504         }
   1505     }
   1506 
   1507     if (!SSL_CONF_CTX_finish(s_cctx) || !SSL_CONF_CTX_finish(c_cctx) || !SSL_CONF_CTX_finish(s_cctx2)) {
   1508         BIO_puts(bio_err, "Error finishing context\n");
   1509         ERR_print_errors(bio_err);
   1510         goto end;
   1511     }
   1512 #ifndef OPENSSL_NO_DH
   1513     if (!no_dhe) {
   1514         if (dhe1024dsa)
   1515             dhpkey = get_dh1024dsa(libctx);
   1516         else if (dhe512)
   1517             dhpkey = get_dh512(libctx);
   1518         else if (dhe4096)
   1519             dhpkey = get_dh4096(libctx);
   1520         else
   1521             dhpkey = get_dh2048(libctx);
   1522 
   1523         if (dhpkey == NULL || !EVP_PKEY_up_ref(dhpkey)) {
   1524             EVP_PKEY_free(dhpkey);
   1525             BIO_puts(bio_err, "Error getting DH parameters\n");
   1526             ERR_print_errors(bio_err);
   1527             goto end;
   1528         }
   1529         if (!SSL_CTX_set0_tmp_dh_pkey(s_ctx, dhpkey))
   1530             EVP_PKEY_free(dhpkey);
   1531         if (!SSL_CTX_set0_tmp_dh_pkey(s_ctx2, dhpkey))
   1532             EVP_PKEY_free(dhpkey);
   1533     }
   1534 #endif
   1535 
   1536     if (!(SSL_CTX_load_verify_file(s_ctx, CAfile)
   1537             || SSL_CTX_load_verify_dir(s_ctx, CApath))
   1538         || !SSL_CTX_set_default_verify_paths(s_ctx)
   1539         || !(SSL_CTX_load_verify_file(s_ctx2, CAfile)
   1540             || SSL_CTX_load_verify_dir(s_ctx2, CApath))
   1541         || !SSL_CTX_set_default_verify_paths(s_ctx2)
   1542         || !(SSL_CTX_load_verify_file(c_ctx, CAfile)
   1543             || SSL_CTX_load_verify_dir(c_ctx, CApath))
   1544         || !SSL_CTX_set_default_verify_paths(c_ctx)) {
   1545         ERR_print_errors(bio_err);
   1546     }
   1547 
   1548 #ifndef OPENSSL_NO_CT
   1549     if (!SSL_CTX_set_default_ctlog_list_file(s_ctx) || !SSL_CTX_set_default_ctlog_list_file(s_ctx2) || !SSL_CTX_set_default_ctlog_list_file(c_ctx)) {
   1550         ERR_print_errors(bio_err);
   1551     }
   1552 #endif
   1553 
   1554     if (client_auth) {
   1555         printf("client authentication\n");
   1556         SSL_CTX_set_verify(s_ctx,
   1557             SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
   1558             verify_callback);
   1559         SSL_CTX_set_verify(s_ctx2,
   1560             SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
   1561             verify_callback);
   1562         SSL_CTX_set_cert_verify_callback(s_ctx, app_verify_callback,
   1563             &app_verify_arg);
   1564         SSL_CTX_set_cert_verify_callback(s_ctx2, app_verify_callback,
   1565             &app_verify_arg);
   1566     }
   1567     if (server_auth) {
   1568         printf("server authentication\n");
   1569         SSL_CTX_set_verify(c_ctx, SSL_VERIFY_PEER, verify_callback);
   1570         SSL_CTX_set_cert_verify_callback(c_ctx, app_verify_callback,
   1571             &app_verify_arg);
   1572     }
   1573 
   1574     {
   1575         int session_id_context = 0;
   1576         if (!SSL_CTX_set_session_id_context(s_ctx, (void *)&session_id_context,
   1577                 sizeof(session_id_context))
   1578             || !SSL_CTX_set_session_id_context(s_ctx2, (void *)&session_id_context,
   1579                 sizeof(session_id_context))) {
   1580             ERR_print_errors(bio_err);
   1581             goto end;
   1582         }
   1583     }
   1584 
   1585     /* Use PSK only if PSK key is given */
   1586     if (psk_key != NULL) {
   1587         /*
   1588          * no_psk is used to avoid putting psk command to openssl tool
   1589          */
   1590         if (no_psk) {
   1591             /*
   1592              * if PSK is not compiled in and psk key is given, do nothing and
   1593              * exit successfully
   1594              */
   1595             ret = EXIT_SUCCESS;
   1596             goto end;
   1597         }
   1598 #ifndef OPENSSL_NO_PSK
   1599         SSL_CTX_set_psk_client_callback(c_ctx, psk_client_callback);
   1600         SSL_CTX_set_psk_server_callback(s_ctx, psk_server_callback);
   1601         SSL_CTX_set_psk_server_callback(s_ctx2, psk_server_callback);
   1602         if (debug)
   1603             BIO_printf(bio_err, "setting PSK identity hint to s_ctx\n");
   1604         if (!SSL_CTX_use_psk_identity_hint(s_ctx, "ctx server identity_hint") || !SSL_CTX_use_psk_identity_hint(s_ctx2, "ctx server identity_hint")) {
   1605             BIO_printf(bio_err, "error setting PSK identity hint to s_ctx\n");
   1606             ERR_print_errors(bio_err);
   1607             goto end;
   1608         }
   1609 #endif
   1610     }
   1611 
   1612 #ifndef OPENSSL_NO_NEXTPROTONEG
   1613     if (npn_client) {
   1614         SSL_CTX_set_next_proto_select_cb(c_ctx, cb_client_npn, NULL);
   1615     }
   1616     if (npn_server) {
   1617         if (npn_server_reject) {
   1618             BIO_printf(bio_err,
   1619                 "Can't have both -npn_server and -npn_server_reject\n");
   1620             goto end;
   1621         }
   1622         SSL_CTX_set_npn_advertised_cb(s_ctx, cb_server_npn, NULL);
   1623         SSL_CTX_set_npn_advertised_cb(s_ctx2, cb_server_npn, NULL);
   1624     }
   1625     if (npn_server_reject) {
   1626         SSL_CTX_set_npn_advertised_cb(s_ctx, cb_server_rejects_npn, NULL);
   1627         SSL_CTX_set_npn_advertised_cb(s_ctx2, cb_server_rejects_npn, NULL);
   1628     }
   1629 #endif
   1630 
   1631     if (serverinfo_sct) {
   1632         if (!SSL_CTX_add_client_custom_ext(c_ctx,
   1633                 TLSEXT_TYPE_signed_certificate_timestamp,
   1634                 NULL, NULL, NULL,
   1635                 serverinfo_cli_parse_cb, NULL)) {
   1636             BIO_printf(bio_err, "Error adding SCT extension\n");
   1637             goto end;
   1638         }
   1639     }
   1640     if (serverinfo_tack) {
   1641         if (!SSL_CTX_add_client_custom_ext(c_ctx, TACK_EXT_TYPE,
   1642                 NULL, NULL, NULL,
   1643                 serverinfo_cli_parse_cb, NULL)) {
   1644             BIO_printf(bio_err, "Error adding TACK extension\n");
   1645             goto end;
   1646         }
   1647     }
   1648     if (serverinfo_file)
   1649         if (!SSL_CTX_use_serverinfo_file(s_ctx, serverinfo_file) || !SSL_CTX_use_serverinfo_file(s_ctx2, serverinfo_file)) {
   1650             BIO_printf(bio_err, "missing serverinfo file\n");
   1651             goto end;
   1652         }
   1653 
   1654     if (custom_ext) {
   1655         if (!SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_0,
   1656                 custom_ext_0_cli_add_cb,
   1657                 NULL, NULL,
   1658                 custom_ext_0_cli_parse_cb, NULL)
   1659             || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_1,
   1660                 custom_ext_1_cli_add_cb,
   1661                 NULL, NULL,
   1662                 custom_ext_1_cli_parse_cb, NULL)
   1663             || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_2,
   1664                 custom_ext_2_cli_add_cb,
   1665                 NULL, NULL,
   1666                 custom_ext_2_cli_parse_cb, NULL)
   1667             || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_3,
   1668                 custom_ext_3_cli_add_cb,
   1669                 NULL, NULL,
   1670                 custom_ext_3_cli_parse_cb, NULL)
   1671             || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_0,
   1672                 custom_ext_0_srv_add_cb,
   1673                 NULL, NULL,
   1674                 custom_ext_0_srv_parse_cb, NULL)
   1675             || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_0,
   1676                 custom_ext_0_srv_add_cb,
   1677                 NULL, NULL,
   1678                 custom_ext_0_srv_parse_cb, NULL)
   1679             || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_1,
   1680                 custom_ext_1_srv_add_cb,
   1681                 NULL, NULL,
   1682                 custom_ext_1_srv_parse_cb, NULL)
   1683             || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_1,
   1684                 custom_ext_1_srv_add_cb,
   1685                 NULL, NULL,
   1686                 custom_ext_1_srv_parse_cb, NULL)
   1687             || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_2,
   1688                 custom_ext_2_srv_add_cb,
   1689                 NULL, NULL,
   1690                 custom_ext_2_srv_parse_cb, NULL)
   1691             || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_2,
   1692                 custom_ext_2_srv_add_cb,
   1693                 NULL, NULL,
   1694                 custom_ext_2_srv_parse_cb, NULL)
   1695             || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_3,
   1696                 custom_ext_3_srv_add_cb,
   1697                 NULL, NULL,
   1698                 custom_ext_3_srv_parse_cb, NULL)
   1699             || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_3,
   1700                 custom_ext_3_srv_add_cb,
   1701                 NULL, NULL,
   1702                 custom_ext_3_srv_parse_cb, NULL)) {
   1703             BIO_printf(bio_err, "Error setting custom extensions\n");
   1704             goto end;
   1705         }
   1706     }
   1707 
   1708     if (alpn_server)
   1709         SSL_CTX_set_alpn_select_cb(s_ctx, cb_server_alpn, alpn_server);
   1710     if (alpn_server2)
   1711         SSL_CTX_set_alpn_select_cb(s_ctx2, cb_server_alpn, alpn_server2);
   1712 
   1713     if (alpn_client) {
   1714         size_t alpn_len;
   1715         unsigned char *alpn = next_protos_parse(&alpn_len, alpn_client);
   1716 
   1717         if (alpn == NULL) {
   1718             BIO_printf(bio_err, "Error parsing -alpn_client argument\n");
   1719             goto end;
   1720         }
   1721         /* Returns 0 on success!! */
   1722         if (SSL_CTX_set_alpn_protos(c_ctx, alpn, alpn_len)) {
   1723             BIO_printf(bio_err, "Error setting ALPN\n");
   1724             OPENSSL_free(alpn);
   1725             goto end;
   1726         }
   1727         OPENSSL_free(alpn);
   1728     }
   1729 
   1730     if (server_sess_in != NULL) {
   1731         server_sess = read_session(server_sess_in);
   1732         if (server_sess == NULL)
   1733             goto end;
   1734     }
   1735     if (client_sess_in != NULL) {
   1736         client_sess = read_session(client_sess_in);
   1737         if (client_sess == NULL)
   1738             goto end;
   1739     }
   1740 
   1741     if (server_sess_out != NULL || server_sess_in != NULL) {
   1742         char *keys;
   1743         long size;
   1744 
   1745         /* Use a fixed key so that we can decrypt the ticket. */
   1746         size = SSL_CTX_set_tlsext_ticket_keys(s_ctx, NULL, 0);
   1747         keys = OPENSSL_zalloc(size);
   1748         if (keys == NULL)
   1749             goto end;
   1750         SSL_CTX_set_tlsext_ticket_keys(s_ctx, keys, size);
   1751         OPENSSL_free(keys);
   1752     }
   1753 
   1754     if (sn_server1 != NULL || sn_server2 != NULL)
   1755         SSL_CTX_set_tlsext_servername_callback(s_ctx, servername_cb);
   1756 
   1757     c_ssl = SSL_new(c_ctx);
   1758     s_ssl = SSL_new(s_ctx);
   1759     if (c_ssl == NULL || s_ssl == NULL)
   1760         goto end;
   1761 
   1762     if (sn_client)
   1763         SSL_set_tlsext_host_name(c_ssl, sn_client);
   1764     if (client_ktls)
   1765         SSL_set_options(c_ssl, SSL_OP_ENABLE_KTLS);
   1766     if (server_ktls)
   1767         SSL_set_options(s_ssl, SSL_OP_ENABLE_KTLS);
   1768 
   1769     if (!set_protocol_version(server_min_proto, s_ssl, SSL_CTRL_SET_MIN_PROTO_VERSION))
   1770         goto end;
   1771     if (!set_protocol_version(server_max_proto, s_ssl, SSL_CTRL_SET_MAX_PROTO_VERSION))
   1772         goto end;
   1773     if (!set_protocol_version(client_min_proto, c_ssl, SSL_CTRL_SET_MIN_PROTO_VERSION))
   1774         goto end;
   1775     if (!set_protocol_version(client_max_proto, c_ssl, SSL_CTRL_SET_MAX_PROTO_VERSION))
   1776         goto end;
   1777 
   1778     if (server_sess) {
   1779         if (SSL_CTX_add_session(s_ctx, server_sess) == 0) {
   1780             BIO_printf(bio_err, "Can't add server session\n");
   1781             ERR_print_errors(bio_err);
   1782             goto end;
   1783         }
   1784     }
   1785 
   1786     BIO_printf(bio_stdout, "Doing handshakes=%d bytes=%ld\n", number, bytes);
   1787     for (i = 0; i < number; i++) {
   1788         if (!reuse) {
   1789             if (!SSL_set_session(c_ssl, NULL)) {
   1790                 BIO_printf(bio_err, "Failed to set session\n");
   1791                 goto end;
   1792             }
   1793         }
   1794         if (client_sess_in != NULL) {
   1795             if (SSL_set_session(c_ssl, client_sess) == 0) {
   1796                 BIO_printf(bio_err, "Can't set client session\n");
   1797                 ERR_print_errors(bio_err);
   1798                 goto end;
   1799             }
   1800         }
   1801         switch (bio_type) {
   1802         case BIO_MEM:
   1803             ret = doit(s_ssl, c_ssl, bytes);
   1804             break;
   1805         case BIO_PAIR:
   1806             ret = doit_biopair(s_ssl, c_ssl, bytes, &s_time, &c_time);
   1807             break;
   1808 #ifndef OPENSSL_NO_SOCK
   1809         case BIO_IPV4:
   1810             ret = doit_localhost(s_ssl, c_ssl, BIO_FAMILY_IPV4,
   1811                 bytes, &s_time, &c_time);
   1812             break;
   1813         case BIO_IPV6:
   1814             ret = doit_localhost(s_ssl, c_ssl, BIO_FAMILY_IPV6,
   1815                 bytes, &s_time, &c_time);
   1816             break;
   1817 #else
   1818         case BIO_IPV4:
   1819         case BIO_IPV6:
   1820             ret = EXIT_FAILURE;
   1821             goto end;
   1822 #endif
   1823         }
   1824         if (ret != EXIT_SUCCESS)
   1825             break;
   1826     }
   1827 
   1828     if (should_negotiate && ret == EXIT_SUCCESS && strcmp(should_negotiate, "fail-server") != 0 && strcmp(should_negotiate, "fail-client") != 0) {
   1829         int version = protocol_from_string(should_negotiate);
   1830         if (version < 0) {
   1831             BIO_printf(bio_err, "Error parsing: %s\n", should_negotiate);
   1832             ret = EXIT_FAILURE;
   1833             goto end;
   1834         }
   1835         if (SSL_version(c_ssl) != version) {
   1836             BIO_printf(bio_err, "Unexpected version negotiated. "
   1837                                 "Expected: %s, got %s\n",
   1838                 should_negotiate, SSL_get_version(c_ssl));
   1839             ret = EXIT_FAILURE;
   1840             goto end;
   1841         }
   1842     }
   1843 
   1844     if (should_reuse != -1) {
   1845         if (SSL_session_reused(s_ssl) != should_reuse || SSL_session_reused(c_ssl) != should_reuse) {
   1846             BIO_printf(bio_err, "Unexpected session reuse state. "
   1847                                 "Expected: %d, server: %d, client: %d\n",
   1848                 should_reuse,
   1849                 SSL_session_reused(s_ssl), SSL_session_reused(c_ssl));
   1850             ret = EXIT_FAILURE;
   1851             goto end;
   1852         }
   1853     }
   1854 
   1855     if (server_sess_out != NULL) {
   1856         if (write_session(server_sess_out, SSL_get_session(s_ssl)) == 0) {
   1857             ret = EXIT_FAILURE;
   1858             goto end;
   1859         }
   1860     }
   1861     if (client_sess_out != NULL) {
   1862         if (write_session(client_sess_out, SSL_get_session(c_ssl)) == 0) {
   1863             ret = EXIT_FAILURE;
   1864             goto end;
   1865         }
   1866     }
   1867 
   1868     if (!verbose) {
   1869         print_details(c_ssl, "");
   1870     }
   1871     if (print_time) {
   1872 #ifdef CLOCKS_PER_SEC
   1873         /*
   1874          * "To determine the time in seconds, the value returned by the clock
   1875          * function should be divided by the value of the macro
   1876          * CLOCKS_PER_SEC." -- ISO/IEC 9899
   1877          */
   1878         BIO_printf(bio_stdout, "Approximate total server time: %6.2f s\n"
   1879                                "Approximate total client time: %6.2f s\n",
   1880             (double)s_time / CLOCKS_PER_SEC,
   1881             (double)c_time / CLOCKS_PER_SEC);
   1882 #else
   1883         BIO_printf(bio_stdout,
   1884             "Approximate total server time: %6.2f units\n"
   1885             "Approximate total client time: %6.2f units\n",
   1886             (double)s_time, (double)c_time);
   1887 #endif
   1888     }
   1889 
   1890 end:
   1891     SSL_free(s_ssl);
   1892     SSL_free(c_ssl);
   1893     SSL_CTX_free(s_ctx);
   1894     SSL_CTX_free(s_ctx2);
   1895     SSL_CTX_free(c_ctx);
   1896     SSL_CONF_CTX_free(s_cctx);
   1897     SSL_CONF_CTX_free(s_cctx2);
   1898     SSL_CONF_CTX_free(c_cctx);
   1899     sk_OPENSSL_STRING_free(conf_args);
   1900 
   1901     BIO_free(bio_stdout);
   1902 
   1903     SSL_SESSION_free(server_sess);
   1904     SSL_SESSION_free(client_sess);
   1905 
   1906     OSSL_PROVIDER_unload(defctxnull);
   1907     OSSL_PROVIDER_unload(thisprov);
   1908     OSSL_LIB_CTX_free(libctx);
   1909 
   1910     test_close_streams();
   1911 
   1912     EXIT(ret);
   1913 }
   1914 
   1915 #ifndef OPENSSL_NO_SOCK
   1916 int doit_localhost(SSL *s_ssl, SSL *c_ssl, int family, long count,
   1917     clock_t *s_time, clock_t *c_time)
   1918 {
   1919     long cw_num = count, cr_num = count, sw_num = count, sr_num = count;
   1920     BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL;
   1921     BIO *acpt = NULL, *server = NULL, *client = NULL;
   1922     char addr_str[40];
   1923     int ret = EXIT_FAILURE;
   1924     int err_in_client = 0;
   1925     int err_in_server = 0;
   1926 
   1927     acpt = BIO_new_accept(family == BIO_FAMILY_IPV4 ? "127.0.0.1:0"
   1928                                                     : "[::1]:0");
   1929     if (acpt == NULL)
   1930         goto err;
   1931     BIO_set_accept_ip_family(acpt, family);
   1932     BIO_set_bind_mode(acpt, BIO_SOCK_NONBLOCK | BIO_SOCK_REUSEADDR);
   1933     if (BIO_do_accept(acpt) <= 0)
   1934         goto err;
   1935 
   1936     BIO_snprintf(addr_str, sizeof(addr_str), ":%s", BIO_get_accept_port(acpt));
   1937 
   1938     client = BIO_new_connect(addr_str);
   1939     if (!client)
   1940         goto err;
   1941     BIO_set_conn_ip_family(client, family);
   1942 
   1943     if (BIO_set_nbio(client, 1) <= 0)
   1944         goto err;
   1945     if (BIO_set_nbio(acpt, 1) <= 0)
   1946         goto err;
   1947 
   1948     {
   1949         int st_connect = 0, st_accept = 0;
   1950 
   1951         while (!st_connect || !st_accept) {
   1952             if (!st_connect) {
   1953                 if (BIO_do_connect(client) <= 0) {
   1954                     if (!BIO_should_retry(client))
   1955                         goto err;
   1956                 } else {
   1957                     st_connect = 1;
   1958                 }
   1959             }
   1960             if (!st_accept) {
   1961                 if (BIO_do_accept(acpt) <= 0) {
   1962                     if (!BIO_should_retry(acpt))
   1963                         goto err;
   1964                 } else {
   1965                     st_accept = 1;
   1966                 }
   1967             }
   1968         }
   1969     }
   1970     /* We're not interested in accepting further connects */
   1971     server = BIO_pop(acpt);
   1972     BIO_free_all(acpt);
   1973     acpt = NULL;
   1974 
   1975     s_ssl_bio = BIO_new(BIO_f_ssl());
   1976     if (!s_ssl_bio)
   1977         goto err;
   1978 
   1979     c_ssl_bio = BIO_new(BIO_f_ssl());
   1980     if (!c_ssl_bio)
   1981         goto err;
   1982 
   1983     SSL_set_connect_state(c_ssl);
   1984     SSL_set_bio(c_ssl, client, client);
   1985     (void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE);
   1986 
   1987     SSL_set_accept_state(s_ssl);
   1988     SSL_set_bio(s_ssl, server, server);
   1989     (void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE);
   1990 
   1991     do {
   1992         /*-
   1993          * c_ssl_bio:          SSL filter BIO
   1994          *
   1995          * client:             I/O for SSL library
   1996          *
   1997          *
   1998          * server:             I/O for SSL library
   1999          *
   2000          * s_ssl_bio:          SSL filter BIO
   2001          */
   2002 
   2003         /*
   2004          * We have non-blocking behaviour throughout this test program, but
   2005          * can be sure that there is *some* progress in each iteration; so we
   2006          * don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE --
   2007          * we just try everything in each iteration
   2008          */
   2009 
   2010         {
   2011             /* CLIENT */
   2012 
   2013             char cbuf[1024 * 8];
   2014             int i, r;
   2015             clock_t c_clock = clock();
   2016 
   2017             memset(cbuf, 0, sizeof(cbuf));
   2018 
   2019             if (debug)
   2020                 if (SSL_in_init(c_ssl))
   2021                     printf("client waiting in SSL_connect - %s\n",
   2022                         SSL_state_string_long(c_ssl));
   2023 
   2024             if (cw_num > 0) {
   2025                 /* Write to server. */
   2026 
   2027                 if (cw_num > (long)sizeof(cbuf))
   2028                     i = sizeof(cbuf);
   2029                 else
   2030                     i = (int)cw_num;
   2031                 r = BIO_write(c_ssl_bio, cbuf, i);
   2032                 if (r < 0) {
   2033                     if (!BIO_should_retry(c_ssl_bio)) {
   2034                         fprintf(stderr, "ERROR in CLIENT (write)\n");
   2035                         err_in_client = 1;
   2036                         goto err;
   2037                     }
   2038                     /*
   2039                      * BIO_should_retry(...) can just be ignored here. The
   2040                      * library expects us to call BIO_write with the same
   2041                      * arguments again, and that's what we will do in the
   2042                      * next iteration.
   2043                      */
   2044                 } else if (r == 0) {
   2045                     fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
   2046                     goto err;
   2047                 } else {
   2048                     if (debug)
   2049                         printf("client wrote %d\n", r);
   2050                     cw_num -= r;
   2051                 }
   2052             }
   2053 
   2054             if (cr_num > 0) {
   2055                 /* Read from server. */
   2056 
   2057                 r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf));
   2058                 if (r < 0) {
   2059                     if (!BIO_should_retry(c_ssl_bio)) {
   2060                         fprintf(stderr, "ERROR in CLIENT (read)\n");
   2061                         err_in_client = 1;
   2062                         goto err;
   2063                     }
   2064                     /*
   2065                      * Again, "BIO_should_retry" can be ignored.
   2066                      */
   2067                 } else if (r == 0) {
   2068                     fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
   2069                     goto err;
   2070                 } else {
   2071                     if (debug)
   2072                         printf("client read %d\n", r);
   2073                     cr_num -= r;
   2074                 }
   2075             }
   2076 
   2077             /*
   2078              * c_time and s_time increments will typically be very small
   2079              * (depending on machine speed and clock tick intervals), but
   2080              * sampling over a large number of connections should result in
   2081              * fairly accurate figures.  We cannot guarantee a lot, however
   2082              * -- if each connection lasts for exactly one clock tick, it
   2083              * will be counted only for the client or only for the server or
   2084              * even not at all.
   2085              */
   2086             *c_time += (clock() - c_clock);
   2087         }
   2088 
   2089         {
   2090             /* SERVER */
   2091 
   2092             char sbuf[1024 * 8];
   2093             int i, r;
   2094             clock_t s_clock = clock();
   2095 
   2096             memset(sbuf, 0, sizeof(sbuf));
   2097 
   2098             if (debug)
   2099                 if (SSL_in_init(s_ssl))
   2100                     printf("server waiting in SSL_accept - %s\n",
   2101                         SSL_state_string_long(s_ssl));
   2102 
   2103             if (sw_num > 0) {
   2104                 /* Write to client. */
   2105 
   2106                 if (sw_num > (long)sizeof(sbuf))
   2107                     i = sizeof(sbuf);
   2108                 else
   2109                     i = (int)sw_num;
   2110                 r = BIO_write(s_ssl_bio, sbuf, i);
   2111                 if (r < 0) {
   2112                     if (!BIO_should_retry(s_ssl_bio)) {
   2113                         fprintf(stderr, "ERROR in SERVER (write)\n");
   2114                         err_in_server = 1;
   2115                         goto err;
   2116                     }
   2117                     /* Ignore "BIO_should_retry". */
   2118                 } else if (r == 0) {
   2119                     fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
   2120                     goto err;
   2121                 } else {
   2122                     if (debug)
   2123                         printf("server wrote %d\n", r);
   2124                     sw_num -= r;
   2125                 }
   2126             }
   2127 
   2128             if (sr_num > 0) {
   2129                 /* Read from client. */
   2130 
   2131                 r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf));
   2132                 if (r < 0) {
   2133                     if (!BIO_should_retry(s_ssl_bio)) {
   2134                         fprintf(stderr, "ERROR in SERVER (read)\n");
   2135                         err_in_server = 1;
   2136                         goto err;
   2137                     }
   2138                     /* blah, blah */
   2139                 } else if (r == 0) {
   2140                     fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
   2141                     goto err;
   2142                 } else {
   2143                     if (debug)
   2144                         printf("server read %d\n", r);
   2145                     sr_num -= r;
   2146                 }
   2147             }
   2148 
   2149             *s_time += (clock() - s_clock);
   2150         }
   2151     } while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0);
   2152 
   2153     if (verbose) {
   2154         print_details(c_ssl, "DONE via TCP connect: ");
   2155 
   2156         if (BIO_get_ktls_send(SSL_get_wbio(s_ssl))
   2157             && BIO_get_ktls_recv(SSL_get_rbio(s_ssl)))
   2158             BIO_printf(bio_stdout, "Server using Kernel TLS in both directions\n");
   2159         else if (BIO_get_ktls_send(SSL_get_wbio(s_ssl)))
   2160             BIO_printf(bio_stdout, "Server using Kernel TLS for sending\n");
   2161         else if (BIO_get_ktls_recv(SSL_get_rbio(s_ssl)))
   2162             BIO_printf(bio_stdout, "Server using Kernel TLS for receiving\n");
   2163 
   2164         if (BIO_get_ktls_send(SSL_get_wbio(c_ssl))
   2165             && BIO_get_ktls_recv(SSL_get_rbio(c_ssl)))
   2166             BIO_printf(bio_stdout, "Client using Kernel TLS in both directions\n");
   2167         else if (BIO_get_ktls_send(SSL_get_wbio(c_ssl)))
   2168             BIO_printf(bio_stdout, "Client using Kernel TLS for sending\n");
   2169         else if (BIO_get_ktls_recv(SSL_get_rbio(c_ssl)))
   2170             BIO_printf(bio_stdout, "Client using Kernel TLS for receiving\n");
   2171     }
   2172 #ifndef OPENSSL_NO_NEXTPROTONEG
   2173     if (verify_npn(c_ssl, s_ssl) < 0)
   2174         goto end;
   2175 #endif
   2176     if (verify_serverinfo() < 0) {
   2177         fprintf(stderr, "Server info verify error\n");
   2178         goto err;
   2179     }
   2180     if (verify_alpn(c_ssl, s_ssl) < 0
   2181         || verify_servername(c_ssl, s_ssl) < 0)
   2182         goto err;
   2183 
   2184     if (custom_ext_error) {
   2185         fprintf(stderr, "Custom extension error\n");
   2186         goto err;
   2187     }
   2188 
   2189 #ifndef OPENSSL_NO_NEXTPROTONEG
   2190 end:
   2191 #endif
   2192     ret = EXIT_SUCCESS;
   2193 
   2194 err:
   2195     ERR_print_errors(bio_err);
   2196 
   2197     BIO_free_all(acpt);
   2198     BIO_free(server);
   2199     BIO_free(client);
   2200     BIO_free(s_ssl_bio);
   2201     BIO_free(c_ssl_bio);
   2202 
   2203     if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0)
   2204         ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
   2205     else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0)
   2206         ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
   2207 
   2208     return ret;
   2209 }
   2210 #endif
   2211 
   2212 int doit_biopair(SSL *s_ssl, SSL *c_ssl, long count,
   2213     clock_t *s_time, clock_t *c_time)
   2214 {
   2215     long cw_num = count, cr_num = count, sw_num = count, sr_num = count;
   2216     BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL;
   2217     BIO *server = NULL, *server_io = NULL, *client = NULL, *client_io = NULL;
   2218     int ret = EXIT_FAILURE;
   2219     int err_in_client = 0;
   2220     int err_in_server = 0;
   2221 
   2222     size_t bufsiz = 256; /* small buffer for testing */
   2223 
   2224     if (!BIO_new_bio_pair(&server, bufsiz, &server_io, bufsiz))
   2225         goto err;
   2226     if (!BIO_new_bio_pair(&client, bufsiz, &client_io, bufsiz))
   2227         goto err;
   2228 
   2229     s_ssl_bio = BIO_new(BIO_f_ssl());
   2230     if (!s_ssl_bio)
   2231         goto err;
   2232 
   2233     c_ssl_bio = BIO_new(BIO_f_ssl());
   2234     if (!c_ssl_bio)
   2235         goto err;
   2236 
   2237     SSL_set_connect_state(c_ssl);
   2238     SSL_set_bio(c_ssl, client, client);
   2239     (void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE);
   2240 
   2241     SSL_set_accept_state(s_ssl);
   2242     SSL_set_bio(s_ssl, server, server);
   2243     (void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE);
   2244 
   2245     do {
   2246         /*-
   2247          * c_ssl_bio:          SSL filter BIO
   2248          *
   2249          * client:             pseudo-I/O for SSL library
   2250          *
   2251          * client_io:          client's SSL communication; usually to be
   2252          *                     relayed over some I/O facility, but in this
   2253          *                     test program, we're the server, too:
   2254          *
   2255          * server_io:          server's SSL communication
   2256          *
   2257          * server:             pseudo-I/O for SSL library
   2258          *
   2259          * s_ssl_bio:          SSL filter BIO
   2260          *
   2261          * The client and the server each employ a "BIO pair":
   2262          * client + client_io, server + server_io.
   2263          * BIO pairs are symmetric.  A BIO pair behaves similar
   2264          * to a non-blocking socketpair (but both endpoints must
   2265          * be handled by the same thread).
   2266          * [Here we could connect client and server to the ends
   2267          * of a single BIO pair, but then this code would be less
   2268          * suitable as an example for BIO pairs in general.]
   2269          *
   2270          * Useful functions for querying the state of BIO pair endpoints:
   2271          *
   2272          * BIO_ctrl_pending(bio)              number of bytes we can read now
   2273          * BIO_ctrl_get_read_request(bio)     number of bytes needed to fulfill
   2274          *                                      other side's read attempt
   2275          * BIO_ctrl_get_write_guarantee(bio)   number of bytes we can write now
   2276          *
   2277          * ..._read_request is never more than ..._write_guarantee;
   2278          * it depends on the application which one you should use.
   2279          */
   2280 
   2281         /*
   2282          * We have non-blocking behaviour throughout this test program, but
   2283          * can be sure that there is *some* progress in each iteration; so we
   2284          * don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE --
   2285          * we just try everything in each iteration
   2286          */
   2287 
   2288         {
   2289             /* CLIENT */
   2290 
   2291             char cbuf[1024 * 8];
   2292             int i, r;
   2293             clock_t c_clock = clock();
   2294 
   2295             memset(cbuf, 0, sizeof(cbuf));
   2296 
   2297             if (debug)
   2298                 if (SSL_in_init(c_ssl))
   2299                     printf("client waiting in SSL_connect - %s\n",
   2300                         SSL_state_string_long(c_ssl));
   2301 
   2302             if (cw_num > 0) {
   2303                 /* Write to server. */
   2304 
   2305                 if (cw_num > (long)sizeof(cbuf))
   2306                     i = sizeof(cbuf);
   2307                 else
   2308                     i = (int)cw_num;
   2309                 r = BIO_write(c_ssl_bio, cbuf, i);
   2310                 if (r < 0) {
   2311                     if (!BIO_should_retry(c_ssl_bio)) {
   2312                         fprintf(stderr, "ERROR in CLIENT\n");
   2313                         err_in_client = 1;
   2314                         goto err;
   2315                     }
   2316                     /*
   2317                      * BIO_should_retry(...) can just be ignored here. The
   2318                      * library expects us to call BIO_write with the same
   2319                      * arguments again, and that's what we will do in the
   2320                      * next iteration.
   2321                      */
   2322                 } else if (r == 0) {
   2323                     fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
   2324                     goto err;
   2325                 } else {
   2326                     if (debug)
   2327                         printf("client wrote %d\n", r);
   2328                     cw_num -= r;
   2329                 }
   2330             }
   2331 
   2332             if (cr_num > 0) {
   2333                 /* Read from server. */
   2334 
   2335                 r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf));
   2336                 if (r < 0) {
   2337                     if (!BIO_should_retry(c_ssl_bio)) {
   2338                         fprintf(stderr, "ERROR in CLIENT\n");
   2339                         err_in_client = 1;
   2340                         goto err;
   2341                     }
   2342                     /*
   2343                      * Again, "BIO_should_retry" can be ignored.
   2344                      */
   2345                 } else if (r == 0) {
   2346                     fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
   2347                     goto err;
   2348                 } else {
   2349                     if (debug)
   2350                         printf("client read %d\n", r);
   2351                     cr_num -= r;
   2352                 }
   2353             }
   2354 
   2355             /*
   2356              * c_time and s_time increments will typically be very small
   2357              * (depending on machine speed and clock tick intervals), but
   2358              * sampling over a large number of connections should result in
   2359              * fairly accurate figures.  We cannot guarantee a lot, however
   2360              * -- if each connection lasts for exactly one clock tick, it
   2361              * will be counted only for the client or only for the server or
   2362              * even not at all.
   2363              */
   2364             *c_time += (clock() - c_clock);
   2365         }
   2366 
   2367         {
   2368             /* SERVER */
   2369 
   2370             char sbuf[1024 * 8];
   2371             int i, r;
   2372             clock_t s_clock = clock();
   2373 
   2374             memset(sbuf, 0, sizeof(sbuf));
   2375 
   2376             if (debug)
   2377                 if (SSL_in_init(s_ssl))
   2378                     printf("server waiting in SSL_accept - %s\n",
   2379                         SSL_state_string_long(s_ssl));
   2380 
   2381             if (sw_num > 0) {
   2382                 /* Write to client. */
   2383 
   2384                 if (sw_num > (long)sizeof(sbuf))
   2385                     i = sizeof(sbuf);
   2386                 else
   2387                     i = (int)sw_num;
   2388                 r = BIO_write(s_ssl_bio, sbuf, i);
   2389                 if (r < 0) {
   2390                     if (!BIO_should_retry(s_ssl_bio)) {
   2391                         fprintf(stderr, "ERROR in SERVER\n");
   2392                         err_in_server = 1;
   2393                         goto err;
   2394                     }
   2395                     /* Ignore "BIO_should_retry". */
   2396                 } else if (r == 0) {
   2397                     fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
   2398                     goto err;
   2399                 } else {
   2400                     if (debug)
   2401                         printf("server wrote %d\n", r);
   2402                     sw_num -= r;
   2403                 }
   2404             }
   2405 
   2406             if (sr_num > 0) {
   2407                 /* Read from client. */
   2408 
   2409                 r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf));
   2410                 if (r < 0) {
   2411                     if (!BIO_should_retry(s_ssl_bio)) {
   2412                         fprintf(stderr, "ERROR in SERVER\n");
   2413                         err_in_server = 1;
   2414                         goto err;
   2415                     }
   2416                     /* blah, blah */
   2417                 } else if (r == 0) {
   2418                     fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
   2419                     goto err;
   2420                 } else {
   2421                     if (debug)
   2422                         printf("server read %d\n", r);
   2423                     sr_num -= r;
   2424                 }
   2425             }
   2426 
   2427             *s_time += (clock() - s_clock);
   2428         }
   2429 
   2430         {
   2431             /* "I/O" BETWEEN CLIENT AND SERVER. */
   2432 
   2433             size_t r1, r2;
   2434             BIO *io1 = server_io, *io2 = client_io;
   2435             /*
   2436              * we use the non-copying interface for io1 and the standard
   2437              * BIO_write/BIO_read interface for io2
   2438              */
   2439 
   2440             static int prev_progress = 1;
   2441             int progress = 0;
   2442 
   2443             /* io1 to io2 */
   2444             do {
   2445                 size_t num;
   2446                 int r;
   2447 
   2448                 r1 = BIO_ctrl_pending(io1);
   2449                 r2 = BIO_ctrl_get_write_guarantee(io2);
   2450 
   2451                 num = r1;
   2452                 if (r2 < num)
   2453                     num = r2;
   2454                 if (num) {
   2455                     char *dataptr;
   2456 
   2457                     if (INT_MAX < num) /* yeah, right */
   2458                         num = INT_MAX;
   2459 
   2460                     r = BIO_nread(io1, &dataptr, (int)num);
   2461                     assert(r > 0);
   2462                     assert(r <= (int)num);
   2463                     /*
   2464                      * possibly r < num (non-contiguous data)
   2465                      */
   2466                     num = r;
   2467                     r = BIO_write(io2, dataptr, (int)num);
   2468                     if (r != (int)num) { /* can't happen */
   2469                         fprintf(stderr, "ERROR: BIO_write could not write "
   2470                                         "BIO_ctrl_get_write_guarantee() bytes");
   2471                         goto err;
   2472                     }
   2473                     progress = 1;
   2474 
   2475                     if (debug)
   2476                         printf((io1 == client_io) ? "C->S relaying: %d bytes\n" : "S->C relaying: %d bytes\n", (int)num);
   2477                 }
   2478             } while (r1 && r2);
   2479 
   2480             /* io2 to io1 */
   2481             {
   2482                 size_t num;
   2483                 int r;
   2484 
   2485                 r1 = BIO_ctrl_pending(io2);
   2486                 r2 = BIO_ctrl_get_read_request(io1);
   2487                 /*
   2488                  * here we could use ..._get_write_guarantee instead of
   2489                  * ..._get_read_request, but by using the latter we test
   2490                  * restartability of the SSL implementation more thoroughly
   2491                  */
   2492                 num = r1;
   2493                 if (r2 < num)
   2494                     num = r2;
   2495                 if (num) {
   2496                     char *dataptr;
   2497 
   2498                     if (INT_MAX < num)
   2499                         num = INT_MAX;
   2500 
   2501                     if (num > 1)
   2502                         --num; /* test restartability even more thoroughly */
   2503 
   2504                     r = BIO_nwrite0(io1, &dataptr);
   2505                     assert(r > 0);
   2506                     if (r < (int)num)
   2507                         num = r;
   2508                     r = BIO_read(io2, dataptr, (int)num);
   2509                     if (r != (int)num) { /* can't happen */
   2510                         fprintf(stderr, "ERROR: BIO_read could not read "
   2511                                         "BIO_ctrl_pending() bytes");
   2512                         goto err;
   2513                     }
   2514                     progress = 1;
   2515                     r = BIO_nwrite(io1, &dataptr, (int)num);
   2516                     if (r != (int)num) { /* can't happen */
   2517                         fprintf(stderr, "ERROR: BIO_nwrite() did not accept "
   2518                                         "BIO_nwrite0() bytes");
   2519                         goto err;
   2520                     }
   2521 
   2522                     if (debug)
   2523                         printf((io2 == client_io) ? "C->S relaying: %d bytes\n" : "S->C relaying: %d bytes\n", (int)num);
   2524                 }
   2525             } /* no loop, BIO_ctrl_get_read_request now
   2526                * returns 0 anyway */
   2527 
   2528             if (!progress && !prev_progress)
   2529                 if (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0) {
   2530                     fprintf(stderr, "ERROR: got stuck\n");
   2531                     fprintf(stderr, " ERROR.\n");
   2532                     goto err;
   2533                 }
   2534             prev_progress = progress;
   2535         }
   2536     } while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0);
   2537 
   2538     if (verbose)
   2539         print_details(c_ssl, "DONE via BIO pair: ");
   2540 #ifndef OPENSSL_NO_NEXTPROTONEG
   2541     if (verify_npn(c_ssl, s_ssl) < 0)
   2542         goto end;
   2543 #endif
   2544     if (verify_serverinfo() < 0) {
   2545         fprintf(stderr, "Server info verify error\n");
   2546         goto err;
   2547     }
   2548     if (verify_alpn(c_ssl, s_ssl) < 0
   2549         || verify_servername(c_ssl, s_ssl) < 0)
   2550         goto err;
   2551 
   2552     if (custom_ext_error) {
   2553         fprintf(stderr, "Custom extension error\n");
   2554         goto err;
   2555     }
   2556 
   2557 #ifndef OPENSSL_NO_NEXTPROTONEG
   2558 end:
   2559 #endif
   2560     ret = EXIT_SUCCESS;
   2561 
   2562 err:
   2563     ERR_print_errors(bio_err);
   2564 
   2565     BIO_free(server);
   2566     BIO_free(server_io);
   2567     BIO_free(client);
   2568     BIO_free(client_io);
   2569     BIO_free(s_ssl_bio);
   2570     BIO_free(c_ssl_bio);
   2571 
   2572     if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0)
   2573         ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
   2574     else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0)
   2575         ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
   2576 
   2577     return ret;
   2578 }
   2579 
   2580 #define W_READ 1
   2581 #define W_WRITE 2
   2582 #define C_DONE 1
   2583 #define S_DONE 2
   2584 
   2585 int doit(SSL *s_ssl, SSL *c_ssl, long count)
   2586 {
   2587     char *cbuf = NULL, *sbuf = NULL;
   2588     long bufsiz;
   2589     long cw_num = count, cr_num = count;
   2590     long sw_num = count, sr_num = count;
   2591     int ret = EXIT_FAILURE;
   2592     BIO *c_to_s = NULL;
   2593     BIO *s_to_c = NULL;
   2594     BIO *c_bio = NULL;
   2595     BIO *s_bio = NULL;
   2596     int c_r, c_w, s_r, s_w;
   2597     int i, j;
   2598     int done = 0;
   2599     int c_write, s_write;
   2600     int do_server = 0, do_client = 0;
   2601     int max_frag = 5 * 1024;
   2602     int err_in_client = 0;
   2603     int err_in_server = 0;
   2604 
   2605     bufsiz = count > 40 * 1024 ? 40 * 1024 : count;
   2606 
   2607     if ((cbuf = OPENSSL_zalloc(bufsiz)) == NULL)
   2608         goto err;
   2609     if ((sbuf = OPENSSL_zalloc(bufsiz)) == NULL)
   2610         goto err;
   2611 
   2612     c_to_s = BIO_new(BIO_s_mem());
   2613     s_to_c = BIO_new(BIO_s_mem());
   2614     if ((s_to_c == NULL) || (c_to_s == NULL)) {
   2615         ERR_print_errors(bio_err);
   2616         goto err;
   2617     }
   2618 
   2619     c_bio = BIO_new(BIO_f_ssl());
   2620     s_bio = BIO_new(BIO_f_ssl());
   2621     if ((c_bio == NULL) || (s_bio == NULL)) {
   2622         ERR_print_errors(bio_err);
   2623         goto err;
   2624     }
   2625 
   2626     SSL_set_connect_state(c_ssl);
   2627     SSL_set_bio(c_ssl, s_to_c, c_to_s);
   2628     SSL_set_max_send_fragment(c_ssl, max_frag);
   2629     BIO_set_ssl(c_bio, c_ssl, BIO_NOCLOSE);
   2630 
   2631     /*
   2632      * We've just given our ref to these BIOs to c_ssl. We need another one to
   2633      * give to s_ssl
   2634      */
   2635     if (!BIO_up_ref(c_to_s)) {
   2636         /* c_to_s and s_to_c will get freed when we free c_ssl */
   2637         c_to_s = NULL;
   2638         s_to_c = NULL;
   2639         goto err;
   2640     }
   2641     if (!BIO_up_ref(s_to_c)) {
   2642         /* s_to_c will get freed when we free c_ssl */
   2643         s_to_c = NULL;
   2644         goto err;
   2645     }
   2646 
   2647     SSL_set_accept_state(s_ssl);
   2648     SSL_set_bio(s_ssl, c_to_s, s_to_c);
   2649 
   2650     /* We've used up all our refs to these now */
   2651     c_to_s = NULL;
   2652     s_to_c = NULL;
   2653 
   2654     SSL_set_max_send_fragment(s_ssl, max_frag);
   2655     BIO_set_ssl(s_bio, s_ssl, BIO_NOCLOSE);
   2656 
   2657     c_r = 0;
   2658     s_r = 1;
   2659     c_w = 1;
   2660     s_w = 0;
   2661     c_write = 1, s_write = 0;
   2662 
   2663     /* We can always do writes */
   2664     for (;;) {
   2665         do_server = 0;
   2666         do_client = 0;
   2667 
   2668         i = (int)BIO_pending(s_bio);
   2669         if ((i && s_r) || s_w)
   2670             do_server = 1;
   2671 
   2672         i = (int)BIO_pending(c_bio);
   2673         if ((i && c_r) || c_w)
   2674             do_client = 1;
   2675 
   2676         if (do_server && debug) {
   2677             if (SSL_in_init(s_ssl))
   2678                 printf("server waiting in SSL_accept - %s\n",
   2679                     SSL_state_string_long(s_ssl));
   2680         }
   2681 
   2682         if (do_client && debug) {
   2683             if (SSL_in_init(c_ssl))
   2684                 printf("client waiting in SSL_connect - %s\n",
   2685                     SSL_state_string_long(c_ssl));
   2686         }
   2687 
   2688         if (!do_client && !do_server) {
   2689             fprintf(stdout, "ERROR IN STARTUP\n");
   2690             ERR_print_errors(bio_err);
   2691             goto err;
   2692         }
   2693         if (do_client && !(done & C_DONE)) {
   2694             if (c_write) {
   2695                 j = (cw_num > bufsiz) ? (int)bufsiz : (int)cw_num;
   2696                 i = BIO_write(c_bio, cbuf, j);
   2697                 if (i < 0) {
   2698                     c_r = 0;
   2699                     c_w = 0;
   2700                     if (BIO_should_retry(c_bio)) {
   2701                         if (BIO_should_read(c_bio))
   2702                             c_r = 1;
   2703                         if (BIO_should_write(c_bio))
   2704                             c_w = 1;
   2705                     } else {
   2706                         fprintf(stderr, "ERROR in CLIENT\n");
   2707                         err_in_client = 1;
   2708                         ERR_print_errors(bio_err);
   2709                         goto err;
   2710                     }
   2711                 } else if (i == 0) {
   2712                     fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
   2713                     goto err;
   2714                 } else {
   2715                     if (debug)
   2716                         printf("client wrote %d\n", i);
   2717                     /* ok */
   2718                     s_r = 1;
   2719                     c_write = 0;
   2720                     cw_num -= i;
   2721                     if (max_frag > 1029)
   2722                         SSL_set_max_send_fragment(c_ssl, max_frag -= 5);
   2723                 }
   2724             } else {
   2725                 i = BIO_read(c_bio, cbuf, bufsiz);
   2726                 if (i < 0) {
   2727                     c_r = 0;
   2728                     c_w = 0;
   2729                     if (BIO_should_retry(c_bio)) {
   2730                         if (BIO_should_read(c_bio))
   2731                             c_r = 1;
   2732                         if (BIO_should_write(c_bio))
   2733                             c_w = 1;
   2734                     } else {
   2735                         fprintf(stderr, "ERROR in CLIENT\n");
   2736                         err_in_client = 1;
   2737                         ERR_print_errors(bio_err);
   2738                         goto err;
   2739                     }
   2740                 } else if (i == 0) {
   2741                     fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
   2742                     goto err;
   2743                 } else {
   2744                     if (debug)
   2745                         printf("client read %d\n", i);
   2746                     cr_num -= i;
   2747                     if (sw_num > 0) {
   2748                         s_write = 1;
   2749                         s_w = 1;
   2750                     }
   2751                     if (cr_num <= 0) {
   2752                         s_write = 1;
   2753                         s_w = 1;
   2754                         done = S_DONE | C_DONE;
   2755                     }
   2756                 }
   2757             }
   2758         }
   2759 
   2760         if (do_server && !(done & S_DONE)) {
   2761             if (!s_write) {
   2762                 i = BIO_read(s_bio, sbuf, bufsiz);
   2763                 if (i < 0) {
   2764                     s_r = 0;
   2765                     s_w = 0;
   2766                     if (BIO_should_retry(s_bio)) {
   2767                         if (BIO_should_read(s_bio))
   2768                             s_r = 1;
   2769                         if (BIO_should_write(s_bio))
   2770                             s_w = 1;
   2771                     } else {
   2772                         fprintf(stderr, "ERROR in SERVER\n");
   2773                         err_in_server = 1;
   2774                         ERR_print_errors(bio_err);
   2775                         goto err;
   2776                     }
   2777                 } else if (i == 0) {
   2778                     ERR_print_errors(bio_err);
   2779                     fprintf(stderr,
   2780                         "SSL SERVER STARTUP FAILED in SSL_read\n");
   2781                     goto err;
   2782                 } else {
   2783                     if (debug)
   2784                         printf("server read %d\n", i);
   2785                     sr_num -= i;
   2786                     if (cw_num > 0) {
   2787                         c_write = 1;
   2788                         c_w = 1;
   2789                     }
   2790                     if (sr_num <= 0) {
   2791                         s_write = 1;
   2792                         s_w = 1;
   2793                         c_write = 0;
   2794                     }
   2795                 }
   2796             } else {
   2797                 j = (sw_num > bufsiz) ? (int)bufsiz : (int)sw_num;
   2798                 i = BIO_write(s_bio, sbuf, j);
   2799                 if (i < 0) {
   2800                     s_r = 0;
   2801                     s_w = 0;
   2802                     if (BIO_should_retry(s_bio)) {
   2803                         if (BIO_should_read(s_bio))
   2804                             s_r = 1;
   2805                         if (BIO_should_write(s_bio))
   2806                             s_w = 1;
   2807                     } else {
   2808                         fprintf(stderr, "ERROR in SERVER\n");
   2809                         err_in_server = 1;
   2810                         ERR_print_errors(bio_err);
   2811                         goto err;
   2812                     }
   2813                 } else if (i == 0) {
   2814                     ERR_print_errors(bio_err);
   2815                     fprintf(stderr,
   2816                         "SSL SERVER STARTUP FAILED in SSL_write\n");
   2817                     goto err;
   2818                 } else {
   2819                     if (debug)
   2820                         printf("server wrote %d\n", i);
   2821                     sw_num -= i;
   2822                     s_write = 0;
   2823                     c_r = 1;
   2824                     if (sw_num <= 0)
   2825                         done |= S_DONE;
   2826                     if (max_frag > 1029)
   2827                         SSL_set_max_send_fragment(s_ssl, max_frag -= 5);
   2828                 }
   2829             }
   2830         }
   2831 
   2832         if ((done & S_DONE) && (done & C_DONE))
   2833             break;
   2834     }
   2835 
   2836     if (verbose)
   2837         print_details(c_ssl, "DONE: ");
   2838 #ifndef OPENSSL_NO_NEXTPROTONEG
   2839     if (verify_npn(c_ssl, s_ssl) < 0)
   2840         goto err;
   2841 #endif
   2842     if (verify_serverinfo() < 0) {
   2843         fprintf(stderr, "Server info verify error\n");
   2844         goto err;
   2845     }
   2846     if (custom_ext_error) {
   2847         fprintf(stderr, "Custom extension error\n");
   2848         goto err;
   2849     }
   2850     ret = EXIT_SUCCESS;
   2851 err:
   2852     BIO_free(c_to_s);
   2853     BIO_free(s_to_c);
   2854     BIO_free_all(c_bio);
   2855     BIO_free_all(s_bio);
   2856     OPENSSL_free(cbuf);
   2857     OPENSSL_free(sbuf);
   2858 
   2859     if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0)
   2860         ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
   2861     else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0)
   2862         ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
   2863 
   2864     return ret;
   2865 }
   2866 
   2867 static int verify_callback(int ok, X509_STORE_CTX *ctx)
   2868 {
   2869     char *s, buf[256];
   2870 
   2871     s = X509_NAME_oneline(X509_get_subject_name(X509_STORE_CTX_get_current_cert(ctx)),
   2872         buf, sizeof(buf));
   2873     if (s != NULL) {
   2874         if (ok)
   2875             printf("depth=%d %s\n", X509_STORE_CTX_get_error_depth(ctx), buf);
   2876         else {
   2877             fprintf(stderr, "depth=%d error=%d %s\n",
   2878                 X509_STORE_CTX_get_error_depth(ctx),
   2879                 X509_STORE_CTX_get_error(ctx), buf);
   2880         }
   2881     }
   2882 
   2883     if (ok == 0) {
   2884         int i = X509_STORE_CTX_get_error(ctx);
   2885 
   2886         switch (i) {
   2887         default:
   2888             fprintf(stderr, "Error string: %s\n",
   2889                 X509_verify_cert_error_string(i));
   2890             break;
   2891         case X509_V_ERR_CERT_NOT_YET_VALID:
   2892         case X509_V_ERR_CERT_HAS_EXPIRED:
   2893         case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
   2894             ok = 1;
   2895             break;
   2896         }
   2897     }
   2898 
   2899     return ok;
   2900 }
   2901 
   2902 static int app_verify_callback(X509_STORE_CTX *ctx, void *arg)
   2903 {
   2904     int ok = 1;
   2905     struct app_verify_arg *cb_arg = arg;
   2906 
   2907     if (cb_arg->app_verify) {
   2908         char *s = NULL, buf[256];
   2909         X509 *c = X509_STORE_CTX_get0_cert(ctx);
   2910 
   2911         printf("In app_verify_callback, allowing cert. ");
   2912         printf("Arg is: %s\n", cb_arg->string);
   2913         printf("Finished printing do we have a context? 0x%p a cert? 0x%p\n",
   2914             (void *)ctx, (void *)c);
   2915         if (c)
   2916             s = X509_NAME_oneline(X509_get_subject_name(c), buf, 256);
   2917         if (s != NULL) {
   2918             printf("cert depth=%d %s\n",
   2919                 X509_STORE_CTX_get_error_depth(ctx), buf);
   2920         }
   2921         return 1;
   2922     }
   2923 
   2924     ok = X509_verify_cert(ctx);
   2925 
   2926     return ok;
   2927 }
   2928 
   2929 #ifndef OPENSSL_NO_PSK
   2930 /* convert the PSK key (psk_key) in ascii to binary (psk) */
   2931 static int psk_key2bn(const char *pskkey, unsigned char *psk,
   2932     unsigned int max_psk_len)
   2933 {
   2934     int ret;
   2935     BIGNUM *bn = NULL;
   2936 
   2937     ret = BN_hex2bn(&bn, pskkey);
   2938     if (!ret) {
   2939         BIO_printf(bio_err, "Could not convert PSK key '%s' to BIGNUM\n",
   2940             pskkey);
   2941         BN_free(bn);
   2942         return 0;
   2943     }
   2944     if (BN_num_bytes(bn) > (int)max_psk_len) {
   2945         BIO_printf(bio_err,
   2946             "psk buffer of callback is too small (%d) for key (%d)\n",
   2947             max_psk_len, BN_num_bytes(bn));
   2948         BN_free(bn);
   2949         return 0;
   2950     }
   2951     ret = BN_bn2bin(bn, psk);
   2952     BN_free(bn);
   2953     return ret;
   2954 }
   2955 
   2956 static unsigned int psk_client_callback(SSL *ssl, const char *hint,
   2957     char *identity,
   2958     unsigned int max_identity_len,
   2959     unsigned char *psk,
   2960     unsigned int max_psk_len)
   2961 {
   2962     int ret;
   2963     unsigned int psk_len = 0;
   2964 
   2965     ret = BIO_snprintf(identity, max_identity_len, "Client_identity");
   2966     if (ret < 0)
   2967         goto out_err;
   2968     if (debug)
   2969         fprintf(stderr, "client: created identity '%s' len=%d\n", identity,
   2970             ret);
   2971     ret = psk_key2bn(psk_key, psk, max_psk_len);
   2972     if (ret < 0)
   2973         goto out_err;
   2974     psk_len = ret;
   2975 out_err:
   2976     return psk_len;
   2977 }
   2978 
   2979 static unsigned int psk_server_callback(SSL *ssl, const char *identity,
   2980     unsigned char *psk,
   2981     unsigned int max_psk_len)
   2982 {
   2983     unsigned int psk_len = 0;
   2984 
   2985     if (strcmp(identity, "Client_identity") != 0) {
   2986         BIO_printf(bio_err, "server: PSK error: client identity not found\n");
   2987         return 0;
   2988     }
   2989     psk_len = psk_key2bn(psk_key, psk, max_psk_len);
   2990     return psk_len;
   2991 }
   2992 #endif
   2993