[BACK]Return to s_client.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / openssl

Annotation of src/usr.bin/openssl/s_client.c, Revision 1.2

1.2     ! doug        1: /* $OpenBSD: s_client.c,v 1.1 2014/08/26 17:47:25 jsing Exp $ */
1.1       jsing       2: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
                      3:  * All rights reserved.
                      4:  *
                      5:  * This package is an SSL implementation written
                      6:  * by Eric Young (eay@cryptsoft.com).
                      7:  * The implementation was written so as to conform with Netscapes SSL.
                      8:  *
                      9:  * This library is free for commercial and non-commercial use as long as
                     10:  * the following conditions are aheared to.  The following conditions
                     11:  * apply to all code found in this distribution, be it the RC4, RSA,
                     12:  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
                     13:  * included with this distribution is covered by the same copyright terms
                     14:  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
                     15:  *
                     16:  * Copyright remains Eric Young's, and as such any Copyright notices in
                     17:  * the code are not to be removed.
                     18:  * If this package is used in a product, Eric Young should be given attribution
                     19:  * as the author of the parts of the library used.
                     20:  * This can be in the form of a textual message at program startup or
                     21:  * in documentation (online or textual) provided with the package.
                     22:  *
                     23:  * Redistribution and use in source and binary forms, with or without
                     24:  * modification, are permitted provided that the following conditions
                     25:  * are met:
                     26:  * 1. Redistributions of source code must retain the copyright
                     27:  *    notice, this list of conditions and the following disclaimer.
                     28:  * 2. Redistributions in binary form must reproduce the above copyright
                     29:  *    notice, this list of conditions and the following disclaimer in the
                     30:  *    documentation and/or other materials provided with the distribution.
                     31:  * 3. All advertising materials mentioning features or use of this software
                     32:  *    must display the following acknowledgement:
                     33:  *    "This product includes cryptographic software written by
                     34:  *     Eric Young (eay@cryptsoft.com)"
                     35:  *    The word 'cryptographic' can be left out if the rouines from the library
                     36:  *    being used are not cryptographic related :-).
                     37:  * 4. If you include any Windows specific code (or a derivative thereof) from
                     38:  *    the apps directory (application code) you must include an acknowledgement:
                     39:  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
                     40:  *
                     41:  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
                     42:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     43:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     44:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     45:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     46:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     47:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     48:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     49:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     50:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     51:  * SUCH DAMAGE.
                     52:  *
                     53:  * The licence and distribution terms for any publically available version or
                     54:  * derivative of this code cannot be changed.  i.e. this code cannot simply be
                     55:  * copied and put under another distribution licence
                     56:  * [including the GNU Public Licence.]
                     57:  */
                     58: /* ====================================================================
                     59:  * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
                     60:  *
                     61:  * Redistribution and use in source and binary forms, with or without
                     62:  * modification, are permitted provided that the following conditions
                     63:  * are met:
                     64:  *
                     65:  * 1. Redistributions of source code must retain the above copyright
                     66:  *    notice, this list of conditions and the following disclaimer.
                     67:  *
                     68:  * 2. Redistributions in binary form must reproduce the above copyright
                     69:  *    notice, this list of conditions and the following disclaimer in
                     70:  *    the documentation and/or other materials provided with the
                     71:  *    distribution.
                     72:  *
                     73:  * 3. All advertising materials mentioning features or use of this
                     74:  *    software must display the following acknowledgment:
                     75:  *    "This product includes software developed by the OpenSSL Project
                     76:  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
                     77:  *
                     78:  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
                     79:  *    endorse or promote products derived from this software without
                     80:  *    prior written permission. For written permission, please contact
                     81:  *    openssl-core@openssl.org.
                     82:  *
                     83:  * 5. Products derived from this software may not be called "OpenSSL"
                     84:  *    nor may "OpenSSL" appear in their names without prior written
                     85:  *    permission of the OpenSSL Project.
                     86:  *
                     87:  * 6. Redistributions of any form whatsoever must retain the following
                     88:  *    acknowledgment:
                     89:  *    "This product includes software developed by the OpenSSL Project
                     90:  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
                     91:  *
                     92:  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
                     93:  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     94:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     95:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
                     96:  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
                     97:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     98:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
                     99:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                    100:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
                    101:  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                    102:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
                    103:  * OF THE POSSIBILITY OF SUCH DAMAGE.
                    104:  * ====================================================================
                    105:  *
                    106:  * This product includes cryptographic software written by Eric Young
                    107:  * (eay@cryptsoft.com).  This product includes software written by Tim
                    108:  * Hudson (tjh@cryptsoft.com).
                    109:  *
                    110:  */
                    111: /* ====================================================================
                    112:  * Copyright 2005 Nokia. All rights reserved.
                    113:  *
                    114:  * The portions of the attached software ("Contribution") is developed by
                    115:  * Nokia Corporation and is licensed pursuant to the OpenSSL open source
                    116:  * license.
                    117:  *
                    118:  * The Contribution, originally written by Mika Kousa and Pasi Eronen of
                    119:  * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
                    120:  * support (see RFC 4279) to OpenSSL.
                    121:  *
                    122:  * No patent licenses or other rights except those expressly stated in
                    123:  * the OpenSSL open source license shall be deemed granted or received
                    124:  * expressly, by implication, estoppel, or otherwise.
                    125:  *
                    126:  * No assurances are provided by Nokia that the Contribution does not
                    127:  * infringe the patent or other intellectual property rights of any third
                    128:  * party or that the license provides you with all the necessary rights
                    129:  * to make use of the Contribution.
                    130:  *
                    131:  * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
                    132:  * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
                    133:  * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
                    134:  * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
                    135:  * OTHERWISE.
                    136:  */
                    137:
                    138: #include <sys/types.h>
                    139: #include <sys/ioctl.h>
                    140: #include <sys/select.h>
                    141: #include <sys/socket.h>
                    142:
                    143: #include <netinet/in.h>
                    144:
                    145: #include <assert.h>
                    146: #include <ctype.h>
                    147: #include <limits.h>
                    148: #include <netdb.h>
                    149: #include <stdio.h>
                    150: #include <stdlib.h>
                    151: #include <limits.h>
                    152: #include <string.h>
                    153: #include <unistd.h>
                    154:
                    155: #include "apps.h"
                    156:
                    157: #include <openssl/bn.h>
                    158: #include <openssl/err.h>
                    159: #include <openssl/ocsp.h>
                    160: #include <openssl/pem.h>
                    161: #include <openssl/rand.h>
                    162: #include <openssl/ssl.h>
                    163: #include <openssl/x509.h>
                    164:
                    165: #include "s_apps.h"
                    166: #include "timeouts.h"
                    167:
                    168: /*#define SSL_HOST_NAME        "www.netscape.com" */
                    169: /*#define SSL_HOST_NAME        "193.118.187.102" */
                    170: #define SSL_HOST_NAME  "localhost"
                    171:
                    172:  /*#define TEST_CERT "client.pem" *//* no default cert. */
                    173:
                    174: #define BUFSIZZ 1024*8
                    175:
                    176: extern int verify_depth;
                    177: extern int verify_error;
                    178: extern int verify_return_error;
                    179:
                    180: static int c_nbio = 0;
                    181: static int c_Pause = 0;
                    182: static int c_debug = 0;
                    183: #ifndef OPENSSL_NO_TLSEXT
                    184: static int c_tlsextdebug = 0;
                    185: static int c_status_req = 0;
                    186: #endif
                    187: static int c_msg = 0;
                    188: static int c_showcerts = 0;
                    189:
                    190: static char *keymatexportlabel = NULL;
                    191: static int keymatexportlen = 20;
                    192:
                    193: static void sc_usage(void);
                    194: static void print_stuff(BIO * berr, SSL * con, int full);
                    195: #ifndef OPENSSL_NO_TLSEXT
                    196: static int ocsp_resp_cb(SSL * s, void *arg);
                    197: #endif
                    198: static BIO *bio_c_out = NULL;
                    199: static int c_quiet = 0;
                    200: static int c_ign_eof = 0;
                    201:
                    202:
                    203: static void
                    204: sc_usage(void)
                    205: {
                    206:        BIO_printf(bio_err, "usage: s_client args\n");
                    207:        BIO_printf(bio_err, "\n");
                    208:        BIO_printf(bio_err, " -4            - Force IPv4\n");
                    209:        BIO_printf(bio_err, " -6            - Force IPv6\n");
                    210:        BIO_printf(bio_err, " -host host     - use -connect instead\n");
                    211:        BIO_printf(bio_err, " -port port     - use -connect instead\n");
                    212:        BIO_printf(bio_err, " -connect host:port - who to connect to (default is %s:%s)\n", SSL_HOST_NAME, PORT_STR);
                    213:
                    214:        BIO_printf(bio_err, " -verify arg   - turn on peer certificate verification\n");
                    215:        BIO_printf(bio_err, " -cert arg     - certificate file to use, PEM format assumed\n");
                    216:        BIO_printf(bio_err, " -certform arg - certificate format (PEM or DER) PEM default\n");
                    217:        BIO_printf(bio_err, " -key arg      - Private key file to use, in cert file if\n");
                    218:        BIO_printf(bio_err, "                 not specified but cert file is.\n");
                    219:        BIO_printf(bio_err, " -keyform arg  - key format (PEM or DER) PEM default\n");
                    220:        BIO_printf(bio_err, " -pass arg     - private key file pass phrase source\n");
                    221:        BIO_printf(bio_err, " -CApath arg   - PEM format directory of CA's\n");
                    222:        BIO_printf(bio_err, " -CAfile arg   - PEM format file of CA's\n");
                    223:        BIO_printf(bio_err, " -reconnect    - Drop and re-make the connection with the same Session-ID\n");
                    224:        BIO_printf(bio_err, " -pause        - sleep(1) after each read(2) and write(2) system call\n");
                    225:        BIO_printf(bio_err, " -showcerts    - show all certificates in the chain\n");
                    226:        BIO_printf(bio_err, " -debug        - extra output\n");
                    227:        BIO_printf(bio_err, " -msg          - Show protocol messages\n");
                    228:        BIO_printf(bio_err, " -nbio_test    - more ssl protocol testing\n");
                    229:        BIO_printf(bio_err, " -state        - print the 'ssl' states\n");
                    230:        BIO_printf(bio_err, " -nbio         - Run with non-blocking IO\n");
                    231:        BIO_printf(bio_err, " -crlf         - convert LF from terminal into CRLF\n");
                    232:        BIO_printf(bio_err, " -quiet        - no s_client output\n");
                    233:        BIO_printf(bio_err, " -ign_eof      - ignore input eof (default when -quiet)\n");
                    234:        BIO_printf(bio_err, " -no_ign_eof   - don't ignore input eof\n");
                    235:        BIO_printf(bio_err, " -ssl3         - just use SSLv3\n");
                    236:        BIO_printf(bio_err, " -tls1_2       - just use TLSv1.2\n");
                    237:        BIO_printf(bio_err, " -tls1_1       - just use TLSv1.1\n");
                    238:        BIO_printf(bio_err, " -tls1         - just use TLSv1\n");
                    239:        BIO_printf(bio_err, " -dtls1        - just use DTLSv1\n");
                    240:        BIO_printf(bio_err, " -mtu          - set the link layer MTU\n");
                    241:        BIO_printf(bio_err, " -no_tls1_2/-no_tls1_1/-no_tls1/-no_ssl3/-no_ssl2 - turn off that protocol\n");
                    242:        BIO_printf(bio_err, " -bugs         - Switch on all SSL implementation bug workarounds\n");
                    243:        BIO_printf(bio_err, " -cipher       - preferred cipher to use, use the 'openssl ciphers'\n");
                    244:        BIO_printf(bio_err, "                 command to see what is available\n");
                    245:        BIO_printf(bio_err, " -starttls prot - use the STARTTLS command before starting TLS\n");
                    246:        BIO_printf(bio_err, "                 for those protocols that support it, where\n");
                    247:        BIO_printf(bio_err, "                 'prot' defines which one to assume.  Currently,\n");
                    248:        BIO_printf(bio_err, "                 only \"smtp\", \"lmtp\", \"pop3\", \"imap\", \"ftp\" and \"xmpp\"\n");
                    249:        BIO_printf(bio_err, "                 are supported.\n");
                    250: #ifndef OPENSSL_NO_ENGINE
                    251:        BIO_printf(bio_err, " -engine id    - Initialise and use the specified engine\n");
                    252: #endif
                    253:        BIO_printf(bio_err, " -sess_out arg - file to write SSL session to\n");
                    254:        BIO_printf(bio_err, " -sess_in arg  - file to read SSL session from\n");
                    255: #ifndef OPENSSL_NO_TLSEXT
                    256:        BIO_printf(bio_err, " -servername host  - Set TLS extension servername in ClientHello\n");
                    257:        BIO_printf(bio_err, " -tlsextdebug      - hex dump of all TLS extensions received\n");
                    258:        BIO_printf(bio_err, " -status           - request certificate status from server\n");
                    259:        BIO_printf(bio_err, " -no_ticket        - disable use of RFC4507bis session tickets\n");
                    260: #ifndef OPENSSL_NO_NEXTPROTONEG
                    261:        BIO_printf(bio_err, " -nextprotoneg arg - enable NPN extension, considering named protocols supported (comma-separated list)\n");
                    262: #endif
                    263: #endif
                    264: #ifndef OPENSSL_NO_SRTP
                    265:        BIO_printf(bio_err, " -use_srtp profiles - Offer SRTP key management with a colon-separated profile list\n");
                    266: #endif
                    267:        BIO_printf(bio_err, " -keymatexport label   - Export keying material using label\n");
                    268:        BIO_printf(bio_err, " -keymatexportlen len  - Export len bytes of keying material (default 20)\n");
                    269: }
                    270:
                    271: #ifndef OPENSSL_NO_TLSEXT
                    272:
                    273: /* This is a context that we pass to callbacks */
                    274: typedef struct tlsextctx_st {
                    275:        BIO *biodebug;
                    276:        int ack;
                    277: } tlsextctx;
                    278:
                    279:
                    280: static int
                    281: ssl_servername_cb(SSL * s, int *ad, void *arg)
                    282: {
                    283:        tlsextctx *p = (tlsextctx *) arg;
                    284:        const char *hn = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
                    285:        if (SSL_get_servername_type(s) != -1)
                    286:                p->ack = !SSL_session_reused(s) && hn != NULL;
                    287:        else
                    288:                BIO_printf(bio_err, "Can't use SSL_get_servername\n");
                    289:
                    290:        return SSL_TLSEXT_ERR_OK;
                    291: }
                    292:
                    293: #ifndef OPENSSL_NO_SRTP
                    294: char *srtp_profiles = NULL;
                    295: #endif
                    296:
                    297: #ifndef OPENSSL_NO_NEXTPROTONEG
                    298: /* This the context that we pass to next_proto_cb */
                    299: typedef struct tlsextnextprotoctx_st {
                    300:        unsigned char *data;
                    301:        unsigned short len;
                    302:        int status;
                    303: } tlsextnextprotoctx;
                    304:
                    305: static tlsextnextprotoctx next_proto;
                    306:
                    307: static int
                    308: next_proto_cb(SSL * s, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)
                    309: {
                    310:        tlsextnextprotoctx *ctx = arg;
                    311:
                    312:        if (!c_quiet) {
                    313:                /* We can assume that |in| is syntactically valid. */
                    314:                unsigned i;
                    315:                BIO_printf(bio_c_out, "Protocols advertised by server: ");
                    316:                for (i = 0; i < inlen;) {
                    317:                        if (i)
                    318:                                BIO_write(bio_c_out, ", ", 2);
                    319:                        BIO_write(bio_c_out, &in[i + 1], in[i]);
                    320:                        i += in[i] + 1;
                    321:                }
                    322:                BIO_write(bio_c_out, "\n", 1);
                    323:        }
                    324:        ctx->status = SSL_select_next_proto(out, outlen, in, inlen, ctx->data, ctx->len);
                    325:        return SSL_TLSEXT_ERR_OK;
                    326: }
                    327: #endif                         /* ndef OPENSSL_NO_NEXTPROTONEG */
                    328: #endif
                    329:
                    330: enum {
                    331:        PROTO_OFF = 0,
                    332:        PROTO_SMTP,
                    333:        PROTO_LMTP,
                    334:        PROTO_POP3,
                    335:        PROTO_IMAP,
                    336:        PROTO_FTP,
                    337:        PROTO_XMPP
                    338: };
                    339:
                    340: int s_client_main(int, char **);
                    341:
                    342: int
                    343: s_client_main(int argc, char **argv)
                    344: {
                    345:        unsigned int off = 0, clr = 0;
                    346:        SSL *con = NULL;
                    347:        int s, k, width, state = 0, af = AF_UNSPEC;
                    348:        char *cbuf = NULL, *sbuf = NULL, *mbuf = NULL;
                    349:        int cbuf_len, cbuf_off;
                    350:        int sbuf_len, sbuf_off;
                    351:        fd_set readfds, writefds;
                    352:        char *port = PORT_STR;
                    353:        int full_log = 1;
                    354:        char *host = SSL_HOST_NAME;
                    355:        char *cert_file = NULL, *key_file = NULL;
                    356:        int cert_format = FORMAT_PEM, key_format = FORMAT_PEM;
                    357:        char *passarg = NULL, *pass = NULL;
                    358:        X509 *cert = NULL;
                    359:        EVP_PKEY *key = NULL;
                    360:        char *CApath = NULL, *CAfile = NULL, *cipher = NULL;
                    361:        int reconnect = 0, badop = 0, verify = SSL_VERIFY_NONE, bugs = 0;
                    362:        int crlf = 0;
                    363:        int write_tty, read_tty, write_ssl, read_ssl, tty_on, ssl_pending;
                    364:        SSL_CTX *ctx = NULL;
                    365:        int ret = 1, in_init = 1, i, nbio_test = 0;
                    366:        int starttls_proto = PROTO_OFF;
                    367:        int prexit = 0;
                    368:        X509_VERIFY_PARAM *vpm = NULL;
                    369:        int badarg = 0;
                    370:        const SSL_METHOD *meth = NULL;
                    371:        int socket_type = SOCK_STREAM;
                    372:        BIO *sbio;
                    373:        int mbuf_len = 0;
                    374:        struct timeval timeout, *timeoutp;
                    375:        const char *errstr = NULL;
                    376: #ifndef OPENSSL_NO_ENGINE
                    377:        char *engine_id = NULL;
                    378:        char *ssl_client_engine_id = NULL;
                    379:        ENGINE *ssl_client_engine = NULL;
                    380: #endif
                    381:        ENGINE *e = NULL;
                    382: #ifndef OPENSSL_NO_TLSEXT
                    383:        char *servername = NULL;
                    384:        tlsextctx tlsextcbp =
                    385:        {NULL, 0};
                    386: #ifndef OPENSSL_NO_NEXTPROTONEG
                    387:        const char *next_proto_neg_in = NULL;
                    388: #endif
                    389: #endif
                    390:        char *sess_in = NULL;
                    391:        char *sess_out = NULL;
                    392:        struct sockaddr peer;
                    393:        int peerlen = sizeof(peer);
                    394:        int enable_timeouts = 0;
                    395:        long socket_mtu = 0;
                    396:
                    397:        meth = SSLv23_client_method();
                    398:
                    399:        c_Pause = 0;
                    400:        c_quiet = 0;
                    401:        c_ign_eof = 0;
                    402:        c_debug = 0;
                    403:        c_msg = 0;
                    404:        c_showcerts = 0;
                    405:
                    406:        if (((cbuf = malloc(BUFSIZZ)) == NULL) ||
                    407:            ((sbuf = malloc(BUFSIZZ)) == NULL) ||
                    408:            ((mbuf = malloc(BUFSIZZ + 1)) == NULL)) {   /* NUL byte */
                    409:                BIO_printf(bio_err, "out of memory\n");
                    410:                goto end;
                    411:        }
                    412:        verify_depth = 0;
                    413:        verify_error = X509_V_OK;
                    414:        c_nbio = 0;
                    415:
                    416:        argc--;
                    417:        argv++;
                    418:        while (argc >= 1) {
                    419:                if (strcmp(*argv, "-host") == 0) {
                    420:                        if (--argc < 1)
                    421:                                goto bad;
                    422:                        host = *(++argv);
                    423:                } else if (strcmp(*argv, "-port") == 0) {
                    424:                        if (--argc < 1)
                    425:                                goto bad;
                    426:                        port = *(++argv);
                    427:                        if (port == NULL || *port == '\0')
                    428:                                goto bad;
                    429:                } else if (strcmp(*argv, "-connect") == 0) {
                    430:                        if (--argc < 1)
                    431:                                goto bad;
                    432:                        if (!extract_host_port(*(++argv), &host, NULL, &port))
                    433:                                goto bad;
                    434:                } else if (strcmp(*argv, "-verify") == 0) {
                    435:                        verify = SSL_VERIFY_PEER;
                    436:                        if (--argc < 1)
                    437:                                goto bad;
                    438:                        verify_depth = strtonum(*(++argv), 0, INT_MAX, &errstr);
                    439:                        if (errstr)
                    440:                                goto bad;
                    441:                        BIO_printf(bio_err, "verify depth is %d\n", verify_depth);
                    442:                } else if (strcmp(*argv, "-cert") == 0) {
                    443:                        if (--argc < 1)
                    444:                                goto bad;
                    445:                        cert_file = *(++argv);
                    446:                } else if (strcmp(*argv, "-sess_out") == 0) {
                    447:                        if (--argc < 1)
                    448:                                goto bad;
                    449:                        sess_out = *(++argv);
                    450:                } else if (strcmp(*argv, "-sess_in") == 0) {
                    451:                        if (--argc < 1)
                    452:                                goto bad;
                    453:                        sess_in = *(++argv);
                    454:                } else if (strcmp(*argv, "-certform") == 0) {
                    455:                        if (--argc < 1)
                    456:                                goto bad;
                    457:                        cert_format = str2fmt(*(++argv));
                    458:                } else if (args_verify(&argv, &argc, &badarg, bio_err, &vpm)) {
                    459:                        if (badarg)
                    460:                                goto bad;
                    461:                        continue;
                    462:                } else if (strcmp(*argv, "-verify_return_error") == 0)
                    463:                        verify_return_error = 1;
                    464:                else if (strcmp(*argv, "-prexit") == 0)
                    465:                        prexit = 1;
                    466:                else if (strcmp(*argv, "-crlf") == 0)
                    467:                        crlf = 1;
                    468:                else if (strcmp(*argv, "-quiet") == 0) {
                    469:                        c_quiet = 1;
                    470:                        c_ign_eof = 1;
                    471:                } else if (strcmp(*argv, "-ign_eof") == 0)
                    472:                        c_ign_eof = 1;
                    473:                else if (strcmp(*argv, "-no_ign_eof") == 0)
                    474:                        c_ign_eof = 0;
                    475:                else if (strcmp(*argv, "-pause") == 0)
                    476:                        c_Pause = 1;
                    477:                else if (strcmp(*argv, "-debug") == 0)
                    478:                        c_debug = 1;
                    479: #ifndef OPENSSL_NO_TLSEXT
                    480:                else if (strcmp(*argv, "-tlsextdebug") == 0)
                    481:                        c_tlsextdebug = 1;
                    482:                else if (strcmp(*argv, "-status") == 0)
                    483:                        c_status_req = 1;
                    484: #endif
                    485:                else if (strcmp(*argv, "-msg") == 0)
                    486:                        c_msg = 1;
                    487:                else if (strcmp(*argv, "-showcerts") == 0)
                    488:                        c_showcerts = 1;
                    489:                else if (strcmp(*argv, "-nbio_test") == 0)
                    490:                        nbio_test = 1;
                    491:                else if (strcmp(*argv, "-state") == 0)
                    492:                        state = 1;
                    493:                else if (strcmp(*argv, "-ssl3") == 0)
                    494:                        meth = SSLv3_client_method();
                    495:                else if (strcmp(*argv, "-tls1_2") == 0)
                    496:                        meth = TLSv1_2_client_method();
                    497:                else if (strcmp(*argv, "-tls1_1") == 0)
                    498:                        meth = TLSv1_1_client_method();
                    499:                else if (strcmp(*argv, "-tls1") == 0)
                    500:                        meth = TLSv1_client_method();
                    501: #ifndef OPENSSL_NO_DTLS1
                    502:                else if (strcmp(*argv, "-dtls1") == 0) {
                    503:                        meth = DTLSv1_client_method();
                    504:                        socket_type = SOCK_DGRAM;
                    505:                } else if (strcmp(*argv, "-timeout") == 0)
                    506:                        enable_timeouts = 1;
                    507:                else if (strcmp(*argv, "-mtu") == 0) {
                    508:                        if (--argc < 1)
                    509:                                goto bad;
                    510:                        socket_mtu = strtonum(*(++argv), 0, LONG_MAX, &errstr);
                    511:                        if (errstr)
                    512:                                goto bad;
                    513:                }
                    514: #endif
                    515:                else if (strcmp(*argv, "-bugs") == 0)
                    516:                        bugs = 1;
                    517:                else if (strcmp(*argv, "-keyform") == 0) {
                    518:                        if (--argc < 1)
                    519:                                goto bad;
                    520:                        key_format = str2fmt(*(++argv));
                    521:                } else if (strcmp(*argv, "-pass") == 0) {
                    522:                        if (--argc < 1)
                    523:                                goto bad;
                    524:                        passarg = *(++argv);
                    525:                } else if (strcmp(*argv, "-key") == 0) {
                    526:                        if (--argc < 1)
                    527:                                goto bad;
                    528:                        key_file = *(++argv);
                    529:                } else if (strcmp(*argv, "-reconnect") == 0) {
                    530:                        reconnect = 5;
                    531:                } else if (strcmp(*argv, "-CApath") == 0) {
                    532:                        if (--argc < 1)
                    533:                                goto bad;
                    534:                        CApath = *(++argv);
                    535:                } else if (strcmp(*argv, "-CAfile") == 0) {
                    536:                        if (--argc < 1)
                    537:                                goto bad;
                    538:                        CAfile = *(++argv);
                    539:                } else if (strcmp(*argv, "-no_tls1_2") == 0)
                    540:                        off |= SSL_OP_NO_TLSv1_2;
                    541:                else if (strcmp(*argv, "-no_tls1_1") == 0)
                    542:                        off |= SSL_OP_NO_TLSv1_1;
                    543:                else if (strcmp(*argv, "-no_tls1") == 0)
                    544:                        off |= SSL_OP_NO_TLSv1;
                    545:                else if (strcmp(*argv, "-no_ssl3") == 0)
                    546:                        off |= SSL_OP_NO_SSLv3;
                    547:                else if (strcmp(*argv, "-no_ssl2") == 0)
                    548:                        off |= SSL_OP_NO_SSLv2;
                    549:                else if (strcmp(*argv, "-no_comp") == 0) {
                    550:                        off |= SSL_OP_NO_COMPRESSION;
                    551:                }
                    552: #ifndef OPENSSL_NO_TLSEXT
                    553:                else if (strcmp(*argv, "-no_ticket") == 0) {
                    554:                        off |= SSL_OP_NO_TICKET;
                    555:                }
                    556: #ifndef OPENSSL_NO_NEXTPROTONEG
                    557:                else if (strcmp(*argv, "-nextprotoneg") == 0) {
                    558:                        if (--argc < 1)
                    559:                                goto bad;
                    560:                        next_proto_neg_in = *(++argv);
                    561:                }
                    562: #endif
                    563: #endif
                    564:                else if (strcmp(*argv, "-serverpref") == 0)
                    565:                        off |= SSL_OP_CIPHER_SERVER_PREFERENCE;
                    566:                else if (strcmp(*argv, "-legacy_renegotiation") == 0)
                    567:                        ; /* no-op */
                    568:                else if (strcmp(*argv, "-legacy_server_connect") == 0) {
                    569:                        off |= SSL_OP_LEGACY_SERVER_CONNECT;
                    570:                } else if (strcmp(*argv, "-no_legacy_server_connect") == 0) {
                    571:                        clr |= SSL_OP_LEGACY_SERVER_CONNECT;
                    572:                } else if (strcmp(*argv, "-cipher") == 0) {
                    573:                        if (--argc < 1)
                    574:                                goto bad;
                    575:                        cipher = *(++argv);
                    576:                }
                    577:                else if (strcmp(*argv, "-nbio") == 0) {
                    578:                        c_nbio = 1;
                    579:                }
                    580:                else if (strcmp(*argv, "-starttls") == 0) {
                    581:                        if (--argc < 1)
                    582:                                goto bad;
                    583:                        ++argv;
                    584:                        if (strcmp(*argv, "smtp") == 0)
                    585:                                starttls_proto = PROTO_SMTP;
                    586:                        else if (strcmp(*argv, "lmtp") == 0)
                    587:                                starttls_proto = PROTO_LMTP;
                    588:                        else if (strcmp(*argv, "pop3") == 0)
                    589:                                starttls_proto = PROTO_POP3;
                    590:                        else if (strcmp(*argv, "imap") == 0)
                    591:                                starttls_proto = PROTO_IMAP;
                    592:                        else if (strcmp(*argv, "ftp") == 0)
                    593:                                starttls_proto = PROTO_FTP;
                    594:                        else if (strcmp(*argv, "xmpp") == 0)
                    595:                                starttls_proto = PROTO_XMPP;
                    596:                        else
                    597:                                goto bad;
                    598:                }
                    599: #ifndef OPENSSL_NO_ENGINE
                    600:                else if (strcmp(*argv, "-engine") == 0) {
                    601:                        if (--argc < 1)
                    602:                                goto bad;
                    603:                        engine_id = *(++argv);
                    604:                } else if (strcmp(*argv, "-ssl_client_engine") == 0) {
                    605:                        if (--argc < 1)
                    606:                                goto bad;
                    607:                        ssl_client_engine_id = *(++argv);
                    608:                }
                    609: #endif
                    610:                else if (strcmp(*argv, "-4") == 0) {
                    611:                        af = AF_INET;
                    612:                } else if (strcmp(*argv, "-6") == 0) {
                    613:                        af = AF_INET6;
                    614:                }
                    615: #ifndef OPENSSL_NO_TLSEXT
                    616:                else if (strcmp(*argv, "-servername") == 0) {
                    617:                        if (--argc < 1)
                    618:                                goto bad;
                    619:                        servername = *(++argv);
                    620:                        /* meth=TLSv1_client_method(); */
                    621:                }
                    622: #endif
                    623: #ifndef OPENSSL_NO_SRTP
                    624:                else if (strcmp(*argv, "-use_srtp") == 0) {
                    625:                        if (--argc < 1)
                    626:                                goto bad;
                    627:                        srtp_profiles = *(++argv);
                    628:                }
                    629: #endif
                    630:                else if (strcmp(*argv, "-keymatexport") == 0) {
                    631:                        if (--argc < 1)
                    632:                                goto bad;
                    633:                        keymatexportlabel = *(++argv);
                    634:                } else if (strcmp(*argv, "-keymatexportlen") == 0) {
                    635:                        if (--argc < 1)
                    636:                                goto bad;
                    637:                        keymatexportlen = strtonum(*(++argv), 1, INT_MAX, &errstr);
                    638:                        if (errstr)
                    639:                                goto bad;
                    640:                } else {
                    641:                        BIO_printf(bio_err, "unknown option %s\n", *argv);
                    642:                        badop = 1;
                    643:                        break;
                    644:                }
                    645:                argc--;
                    646:                argv++;
                    647:        }
                    648:        if (badop) {
                    649: bad:
                    650:                if (errstr)
                    651:                        BIO_printf(bio_err, "invalid argument %s: %s\n",
                    652:                            *argv, errstr);
                    653:                else
                    654:                        sc_usage();
                    655:                goto end;
                    656:        }
                    657:
                    658: #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
                    659:        next_proto.status = -1;
                    660:        if (next_proto_neg_in) {
                    661:                next_proto.data = next_protos_parse(&next_proto.len, next_proto_neg_in);
                    662:                if (next_proto.data == NULL) {
                    663:                        BIO_printf(bio_err, "Error parsing -nextprotoneg argument\n");
                    664:                        goto end;
                    665:                }
                    666:        } else
                    667:                next_proto.data = NULL;
                    668: #endif
                    669:
                    670: #ifndef OPENSSL_NO_ENGINE
                    671:        e = setup_engine(bio_err, engine_id, 1);
                    672:        if (ssl_client_engine_id) {
                    673:                ssl_client_engine = ENGINE_by_id(ssl_client_engine_id);
                    674:                if (!ssl_client_engine) {
                    675:                        BIO_printf(bio_err,
                    676:                            "Error getting client auth engine\n");
                    677:                        goto end;
                    678:                }
                    679:        }
                    680: #endif
                    681:        if (!app_passwd(bio_err, passarg, NULL, &pass, NULL)) {
                    682:                BIO_printf(bio_err, "Error getting password\n");
                    683:                goto end;
                    684:        }
                    685:        if (key_file == NULL)
                    686:                key_file = cert_file;
                    687:
                    688:
                    689:        if (key_file) {
                    690:
                    691:                key = load_key(bio_err, key_file, key_format, 0, pass, e,
                    692:                    "client certificate private key file");
                    693:                if (!key) {
                    694:                        ERR_print_errors(bio_err);
                    695:                        goto end;
                    696:                }
                    697:        }
                    698:        if (cert_file) {
                    699:                cert = load_cert(bio_err, cert_file, cert_format,
                    700:                    NULL, e, "client certificate file");
                    701:
                    702:                if (!cert) {
                    703:                        ERR_print_errors(bio_err);
                    704:                        goto end;
                    705:                }
                    706:        }
                    707:        if (bio_c_out == NULL) {
                    708:                if (c_quiet && !c_debug && !c_msg) {
                    709:                        bio_c_out = BIO_new(BIO_s_null());
                    710:                } else {
                    711:                        if (bio_c_out == NULL)
                    712:                                bio_c_out = BIO_new_fp(stdout, BIO_NOCLOSE);
                    713:                }
                    714:        }
                    715:
                    716:        ctx = SSL_CTX_new(meth);
                    717:        if (ctx == NULL) {
                    718:                ERR_print_errors(bio_err);
                    719:                goto end;
                    720:        }
                    721:        if (vpm)
                    722:                SSL_CTX_set1_param(ctx, vpm);
                    723:
                    724: #ifndef OPENSSL_NO_ENGINE
                    725:        if (ssl_client_engine) {
                    726:                if (!SSL_CTX_set_client_cert_engine(ctx, ssl_client_engine)) {
                    727:                        BIO_puts(bio_err, "Error setting client auth engine\n");
                    728:                        ERR_print_errors(bio_err);
                    729:                        ENGINE_free(ssl_client_engine);
                    730:                        goto end;
                    731:                }
                    732:                ENGINE_free(ssl_client_engine);
                    733:        }
                    734: #endif
                    735:
                    736: #ifndef OPENSSL_NO_SRTP
                    737:        if (srtp_profiles != NULL)
                    738:                SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles);
                    739: #endif
                    740:        if (bugs)
                    741:                SSL_CTX_set_options(ctx, SSL_OP_ALL | off);
                    742:        else
                    743:                SSL_CTX_set_options(ctx, off);
                    744:
                    745:        if (clr)
                    746:                SSL_CTX_clear_options(ctx, clr);
                    747:        /*
                    748:         * DTLS: partial reads end up discarding unread UDP bytes :-( Setting
                    749:         * read ahead solves this problem.
                    750:         */
                    751:        if (socket_type == SOCK_DGRAM)
                    752:                SSL_CTX_set_read_ahead(ctx, 1);
                    753:
                    754: #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
                    755:        if (next_proto.data)
                    756:                SSL_CTX_set_next_proto_select_cb(ctx, next_proto_cb, &next_proto);
                    757: #endif
                    758:
                    759:        if (state)
                    760:                SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback);
                    761:        if (cipher != NULL)
                    762:                if (!SSL_CTX_set_cipher_list(ctx, cipher)) {
                    763:                        BIO_printf(bio_err, "error setting cipher list\n");
                    764:                        ERR_print_errors(bio_err);
                    765:                        goto end;
                    766:                }
                    767:
                    768:        SSL_CTX_set_verify(ctx, verify, verify_callback);
                    769:        if (!set_cert_key_stuff(ctx, cert, key))
                    770:                goto end;
                    771:
                    772:        if ((!SSL_CTX_load_verify_locations(ctx, CAfile, CApath)) ||
                    773:            (!SSL_CTX_set_default_verify_paths(ctx))) {
                    774:                /*
                    775:                 * BIO_printf(bio_err,"error setting default verify
                    776:                 * locations\n");
                    777:                 */
                    778:                ERR_print_errors(bio_err);
                    779:                /* goto end; */
                    780:        }
                    781: #ifndef OPENSSL_NO_TLSEXT
                    782:        if (servername != NULL) {
                    783:                tlsextcbp.biodebug = bio_err;
                    784:                SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
                    785:                SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
                    786:        }
                    787: #endif
                    788:
                    789:        con = SSL_new(ctx);
                    790:        if (sess_in) {
                    791:                SSL_SESSION *sess;
                    792:                BIO *stmp = BIO_new_file(sess_in, "r");
                    793:                if (!stmp) {
                    794:                        BIO_printf(bio_err, "Can't open session file %s\n",
                    795:                            sess_in);
                    796:                        ERR_print_errors(bio_err);
                    797:                        goto end;
                    798:                }
                    799:                sess = PEM_read_bio_SSL_SESSION(stmp, NULL, 0, NULL);
                    800:                BIO_free(stmp);
                    801:                if (!sess) {
                    802:                        BIO_printf(bio_err, "Can't open session file %s\n",
                    803:                            sess_in);
                    804:                        ERR_print_errors(bio_err);
                    805:                        goto end;
                    806:                }
                    807:                SSL_set_session(con, sess);
                    808:                SSL_SESSION_free(sess);
                    809:        }
                    810: #ifndef OPENSSL_NO_TLSEXT
                    811:        if (servername != NULL) {
                    812:                if (!SSL_set_tlsext_host_name(con, servername)) {
                    813:                        BIO_printf(bio_err, "Unable to set TLS servername extension.\n");
                    814:                        ERR_print_errors(bio_err);
                    815:                        goto end;
                    816:                }
                    817:        }
                    818: #endif
                    819: /*     SSL_set_cipher_list(con,"RC4-MD5"); */
                    820:
                    821: re_start:
                    822:
                    823:        if (init_client(&s, host, port, socket_type, af) == 0) {
                    824:                BIO_printf(bio_err, "connect:errno=%d\n", errno);
                    825:                shutdown(s, SHUT_RD);
                    826:                close(s);
                    827:                goto end;
                    828:        }
                    829:        BIO_printf(bio_c_out, "CONNECTED(%08X)\n", s);
                    830:
                    831:        if (c_nbio) {
                    832:                unsigned long l = 1;
                    833:                BIO_printf(bio_c_out, "turning on non blocking io\n");
                    834:                if (BIO_socket_ioctl(s, FIONBIO, &l) < 0) {
                    835:                        ERR_print_errors(bio_err);
                    836:                        goto end;
                    837:                }
                    838:        }
                    839:        if (c_Pause & 0x01)
                    840:                SSL_set_debug(con, 1);
                    841:
                    842:        if (SSL_version(con) == DTLS1_VERSION) {
                    843:
                    844:                sbio = BIO_new_dgram(s, BIO_NOCLOSE);
                    845:                if (getsockname(s, &peer, (void *) &peerlen) < 0) {
                    846:                        BIO_printf(bio_err, "getsockname:errno=%d\n",
                    847:                            errno);
                    848:                        shutdown(s, SHUT_RD);
                    849:                        close(s);
                    850:                        goto end;
                    851:                }
                    852:                (void) BIO_ctrl_set_connected(sbio, 1, &peer);
                    853:
                    854:                if (enable_timeouts) {
                    855:                        timeout.tv_sec = 0;
                    856:                        timeout.tv_usec = DGRAM_RCV_TIMEOUT;
                    857:                        BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
                    858:
                    859:                        timeout.tv_sec = 0;
                    860:                        timeout.tv_usec = DGRAM_SND_TIMEOUT;
                    861:                        BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
                    862:                }
                    863:                if (socket_mtu > 28) {
                    864:                        SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
                    865:                        SSL_set_mtu(con, socket_mtu - 28);
                    866:                } else
                    867:                        /* want to do MTU discovery */
                    868:                        BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
                    869:        } else
                    870:                sbio = BIO_new_socket(s, BIO_NOCLOSE);
                    871:
                    872:        if (nbio_test) {
                    873:                BIO *test;
                    874:
                    875:                test = BIO_new(BIO_f_nbio_test());
                    876:                sbio = BIO_push(test, sbio);
                    877:        }
                    878:        if (c_debug) {
                    879:                SSL_set_debug(con, 1);
                    880:                BIO_set_callback(sbio, bio_dump_callback);
                    881:                BIO_set_callback_arg(sbio, (char *) bio_c_out);
                    882:        }
                    883:        if (c_msg) {
                    884:                SSL_set_msg_callback(con, msg_cb);
                    885:                SSL_set_msg_callback_arg(con, bio_c_out);
                    886:        }
                    887: #ifndef OPENSSL_NO_TLSEXT
                    888:        if (c_tlsextdebug) {
                    889:                SSL_set_tlsext_debug_callback(con, tlsext_cb);
                    890:                SSL_set_tlsext_debug_arg(con, bio_c_out);
                    891:        }
                    892:        if (c_status_req) {
                    893:                SSL_set_tlsext_status_type(con, TLSEXT_STATUSTYPE_ocsp);
                    894:                SSL_CTX_set_tlsext_status_cb(ctx, ocsp_resp_cb);
                    895:                SSL_CTX_set_tlsext_status_arg(ctx, bio_c_out);
                    896:        }
                    897: #endif
                    898:
                    899:        SSL_set_bio(con, sbio, sbio);
                    900:        SSL_set_connect_state(con);
                    901:
                    902:        /* ok, lets connect */
                    903:        width = SSL_get_fd(con) + 1;
                    904:
                    905:        read_tty = 1;
                    906:        write_tty = 0;
                    907:        tty_on = 0;
                    908:        read_ssl = 1;
                    909:        write_ssl = 1;
                    910:
                    911:        cbuf_len = 0;
                    912:        cbuf_off = 0;
                    913:        sbuf_len = 0;
                    914:        sbuf_off = 0;
                    915:
                    916:        /* This is an ugly hack that does a lot of assumptions */
                    917:        /*
                    918:         * We do have to handle multi-line responses which may come in a
                    919:         * single packet or not. We therefore have to use BIO_gets() which
                    920:         * does need a buffering BIO. So during the initial chitchat we do
                    921:         * push a buffering BIO into the chain that is removed again later on
                    922:         * to not disturb the rest of the s_client operation.
                    923:         */
                    924:        if (starttls_proto == PROTO_SMTP || starttls_proto == PROTO_LMTP) {
                    925:                int foundit = 0;
                    926:                BIO *fbio = BIO_new(BIO_f_buffer());
                    927:                BIO_push(fbio, sbio);
                    928:                /* wait for multi-line response to end from SMTP */
                    929:                do {
                    930:                        mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
                    931:                }
                    932:                while (mbuf_len > 3 && mbuf[3] == '-');
                    933:                /* STARTTLS command requires EHLO... */
                    934:                BIO_printf(fbio, "%cHLO openssl.client.net\r\n",
                    935:                           starttls_proto == PROTO_SMTP ? 'E' : 'L');
                    936:                (void) BIO_flush(fbio);
                    937:                /* wait for multi-line response to end EHLO SMTP response */
                    938:                do {
                    939:                        mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
                    940:                        if (strstr(mbuf, "STARTTLS"))
                    941:                                foundit = 1;
                    942:                }
                    943:                while (mbuf_len > 3 && mbuf[3] == '-');
                    944:                (void) BIO_flush(fbio);
                    945:                BIO_pop(fbio);
                    946:                BIO_free(fbio);
                    947:                if (!foundit)
                    948:                        BIO_printf(bio_err,
                    949:                            "didn't found starttls in server response,"
                    950:                            " try anyway...\n");
                    951:                BIO_printf(sbio, "STARTTLS\r\n");
                    952:                BIO_read(sbio, sbuf, BUFSIZZ);
                    953:        } else if (starttls_proto == PROTO_POP3) {
                    954:                mbuf_len = BIO_read(sbio, mbuf, BUFSIZZ);
                    955:                if (mbuf_len == -1) {
                    956:                        BIO_printf(bio_err, "BIO_read failed\n");
                    957:                        goto end;
                    958:                }
                    959:                BIO_printf(sbio, "STLS\r\n");
                    960:                BIO_read(sbio, sbuf, BUFSIZZ);
                    961:        } else if (starttls_proto == PROTO_IMAP) {
                    962:                int foundit = 0;
                    963:                BIO *fbio = BIO_new(BIO_f_buffer());
                    964:                BIO_push(fbio, sbio);
                    965:                BIO_gets(fbio, mbuf, BUFSIZZ);
                    966:                /* STARTTLS command requires CAPABILITY... */
                    967:                BIO_printf(fbio, ". CAPABILITY\r\n");
                    968:                (void) BIO_flush(fbio);
                    969:                /* wait for multi-line CAPABILITY response */
                    970:                do {
                    971:                        mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
                    972:                        if (strstr(mbuf, "STARTTLS"))
                    973:                                foundit = 1;
                    974:                }
                    975:                while (mbuf_len > 3 && mbuf[0] != '.');
                    976:                (void) BIO_flush(fbio);
                    977:                BIO_pop(fbio);
                    978:                BIO_free(fbio);
                    979:                if (!foundit)
                    980:                        BIO_printf(bio_err,
                    981:                            "didn't found STARTTLS in server response,"
                    982:                            " try anyway...\n");
                    983:                BIO_printf(sbio, ". STARTTLS\r\n");
                    984:                BIO_read(sbio, sbuf, BUFSIZZ);
                    985:        } else if (starttls_proto == PROTO_FTP) {
                    986:                BIO *fbio = BIO_new(BIO_f_buffer());
                    987:                BIO_push(fbio, sbio);
                    988:                /* wait for multi-line response to end from FTP */
                    989:                do {
                    990:                        mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
                    991:                }
                    992:                while (mbuf_len > 3 && mbuf[3] == '-');
                    993:                (void) BIO_flush(fbio);
                    994:                BIO_pop(fbio);
                    995:                BIO_free(fbio);
                    996:                BIO_printf(sbio, "AUTH TLS\r\n");
                    997:                BIO_read(sbio, sbuf, BUFSIZZ);
                    998:        }
                    999:        if (starttls_proto == PROTO_XMPP) {
                   1000:                int seen = 0;
                   1001:                BIO_printf(sbio, "<stream:stream "
                   1002:                    "xmlns:stream='http://etherx.jabber.org/streams' "
                   1003:                    "xmlns='jabber:client' to='%s' version='1.0'>", host);
                   1004:                seen = BIO_read(sbio, mbuf, BUFSIZZ);
                   1005:                mbuf[seen] = 0;
                   1006:                while (!strstr(mbuf, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'")) {
                   1007:                        if (strstr(mbuf, "/stream:features>"))
                   1008:                                goto shut;
                   1009:                        seen = BIO_read(sbio, mbuf, BUFSIZZ);
                   1010:                        mbuf[seen] = 0;
                   1011:                }
                   1012:                BIO_printf(sbio, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
                   1013:                seen = BIO_read(sbio, sbuf, BUFSIZZ);
                   1014:                sbuf[seen] = 0;
                   1015:                if (!strstr(sbuf, "<proceed"))
                   1016:                        goto shut;
                   1017:                mbuf[0] = 0;
                   1018:        }
                   1019:        for (;;) {
                   1020:                FD_ZERO(&readfds);
                   1021:                FD_ZERO(&writefds);
                   1022:
                   1023:                if ((SSL_version(con) == DTLS1_VERSION) &&
                   1024:                    DTLSv1_get_timeout(con, &timeout))
                   1025:                        timeoutp = &timeout;
                   1026:                else
                   1027:                        timeoutp = NULL;
                   1028:
                   1029:                if (SSL_in_init(con) && !SSL_total_renegotiations(con)) {
                   1030:                        in_init = 1;
                   1031:                        tty_on = 0;
                   1032:                } else {
                   1033:                        tty_on = 1;
                   1034:                        if (in_init) {
                   1035:                                in_init = 0;
                   1036:                                if (sess_out) {
                   1037:                                        BIO *stmp = BIO_new_file(sess_out, "w");
                   1038:                                        if (stmp) {
                   1039:                                                PEM_write_bio_SSL_SESSION(stmp, SSL_get_session(con));
                   1040:                                                BIO_free(stmp);
                   1041:                                        } else
                   1042:                                                BIO_printf(bio_err, "Error writing session file %s\n", sess_out);
                   1043:                                }
                   1044:                                print_stuff(bio_c_out, con, full_log);
                   1045:                                if (full_log > 0)
                   1046:                                        full_log--;
                   1047:
                   1048:                                if (starttls_proto) {
                   1049:                                        BIO_write(bio_err, mbuf, mbuf_len);
                   1050:                                        /* We don't need to know any more */
                   1051:                                        starttls_proto = PROTO_OFF;
                   1052:                                }
                   1053:                                if (reconnect) {
                   1054:                                        reconnect--;
                   1055:                                        BIO_printf(bio_c_out, "drop connection and then reconnect\n");
                   1056:                                        SSL_shutdown(con);
                   1057:                                        SSL_set_connect_state(con);
                   1058:                                        shutdown(SSL_get_fd(con), SHUT_RD);
                   1059:                                        close(SSL_get_fd(con));
                   1060:                                        goto re_start;
                   1061:                                }
                   1062:                        }
                   1063:                }
                   1064:
                   1065:                ssl_pending = read_ssl && SSL_pending(con);
                   1066:
                   1067:                /* XXX should add tests for fd_set overflow */
                   1068:
                   1069:                if (!ssl_pending) {
                   1070:                        if (tty_on) {
                   1071:                                if (read_tty)
                   1072:                                        FD_SET(fileno(stdin), &readfds);
                   1073:                                if (write_tty)
                   1074:                                        FD_SET(fileno(stdout), &writefds);
                   1075:                        }
                   1076:                        if (read_ssl)
                   1077:                                FD_SET(SSL_get_fd(con), &readfds);
                   1078:                        if (write_ssl)
                   1079:                                FD_SET(SSL_get_fd(con), &writefds);
                   1080: /*                     printf("mode tty(%d %d%d) ssl(%d%d)\n",
                   1081:                                tty_on,read_tty,write_tty,read_ssl,write_ssl);*/
                   1082:
                   1083:                        i = select(width, &readfds, &writefds,
                   1084:                            NULL, timeoutp);
                   1085:                        if (i < 0) {
                   1086:                                BIO_printf(bio_err, "bad select %d\n",
                   1087:                                    errno);
                   1088:                                goto shut;
                   1089:                                /* goto end; */
                   1090:                        }
                   1091:                }
                   1092:                if ((SSL_version(con) == DTLS1_VERSION) && DTLSv1_handle_timeout(con) > 0) {
                   1093:                        BIO_printf(bio_err, "TIMEOUT occured\n");
                   1094:                }
                   1095:                if (!ssl_pending && FD_ISSET(SSL_get_fd(con), &writefds)) {
                   1096:                        k = SSL_write(con, &(cbuf[cbuf_off]),
                   1097:                            (unsigned int) cbuf_len);
                   1098:                        switch (SSL_get_error(con, k)) {
                   1099:                        case SSL_ERROR_NONE:
                   1100:                                cbuf_off += k;
                   1101:                                cbuf_len -= k;
                   1102:                                if (k <= 0)
                   1103:                                        goto end;
                   1104:                                /* we have done a  write(con,NULL,0); */
                   1105:                                if (cbuf_len <= 0) {
                   1106:                                        read_tty = 1;
                   1107:                                        write_ssl = 0;
                   1108:                                } else {        /* if (cbuf_len > 0) */
                   1109:                                        read_tty = 0;
                   1110:                                        write_ssl = 1;
                   1111:                                }
                   1112:                                break;
                   1113:                        case SSL_ERROR_WANT_WRITE:
                   1114:                                BIO_printf(bio_c_out, "write W BLOCK\n");
                   1115:                                write_ssl = 1;
                   1116:                                read_tty = 0;
                   1117:                                break;
                   1118:                        case SSL_ERROR_WANT_READ:
                   1119:                                BIO_printf(bio_c_out, "write R BLOCK\n");
                   1120:                                write_tty = 0;
                   1121:                                read_ssl = 1;
                   1122:                                write_ssl = 0;
                   1123:                                break;
                   1124:                        case SSL_ERROR_WANT_X509_LOOKUP:
                   1125:                                BIO_printf(bio_c_out, "write X BLOCK\n");
                   1126:                                break;
                   1127:                        case SSL_ERROR_ZERO_RETURN:
                   1128:                                if (cbuf_len != 0) {
                   1129:                                        BIO_printf(bio_c_out, "shutdown\n");
                   1130:                                        ret = 0;
                   1131:                                        goto shut;
                   1132:                                } else {
                   1133:                                        read_tty = 1;
                   1134:                                        write_ssl = 0;
                   1135:                                        break;
                   1136:                                }
                   1137:
                   1138:                        case SSL_ERROR_SYSCALL:
                   1139:                                if ((k != 0) || (cbuf_len != 0)) {
                   1140:                                        BIO_printf(bio_err, "write:errno=%d\n",
                   1141:                                            errno);
                   1142:                                        goto shut;
                   1143:                                } else {
                   1144:                                        read_tty = 1;
                   1145:                                        write_ssl = 0;
                   1146:                                }
                   1147:                                break;
                   1148:                        case SSL_ERROR_SSL:
                   1149:                                ERR_print_errors(bio_err);
                   1150:                                goto shut;
                   1151:                        }
                   1152:                } else if (!ssl_pending && FD_ISSET(fileno(stdout), &writefds)) {
                   1153:                        i = write(fileno(stdout), &(sbuf[sbuf_off]), sbuf_len);
                   1154:
                   1155:                        if (i <= 0) {
                   1156:                                BIO_printf(bio_c_out, "DONE\n");
                   1157:                                ret = 0;
                   1158:                                goto shut;
                   1159:                                /* goto end; */
                   1160:                        }
                   1161:                        sbuf_len -= i;
                   1162:                        sbuf_off += i;
                   1163:                        if (sbuf_len <= 0) {
                   1164:                                read_ssl = 1;
                   1165:                                write_tty = 0;
                   1166:                        }
                   1167:                } else if (ssl_pending || FD_ISSET(SSL_get_fd(con), &readfds)) {
                   1168: #ifdef RENEG
                   1169:                        {
                   1170:                                static int iiii;
                   1171:                                if (++iiii == 52) {
                   1172:                                        SSL_renegotiate(con);
                   1173:                                        iiii = 0;
                   1174:                                }
                   1175:                        }
                   1176: #endif
                   1177:                        k = SSL_read(con, sbuf, 1024 /* BUFSIZZ */ );
                   1178:
                   1179:                        switch (SSL_get_error(con, k)) {
                   1180:                        case SSL_ERROR_NONE:
                   1181:                                if (k <= 0)
                   1182:                                        goto end;
                   1183:                                sbuf_off = 0;
                   1184:                                sbuf_len = k;
                   1185:
                   1186:                                read_ssl = 0;
                   1187:                                write_tty = 1;
                   1188:                                break;
                   1189:                        case SSL_ERROR_WANT_WRITE:
                   1190:                                BIO_printf(bio_c_out, "read W BLOCK\n");
                   1191:                                write_ssl = 1;
                   1192:                                read_tty = 0;
                   1193:                                break;
                   1194:                        case SSL_ERROR_WANT_READ:
                   1195:                                BIO_printf(bio_c_out, "read R BLOCK\n");
                   1196:                                write_tty = 0;
                   1197:                                read_ssl = 1;
                   1198:                                if ((read_tty == 0) && (write_ssl == 0))
                   1199:                                        write_ssl = 1;
                   1200:                                break;
                   1201:                        case SSL_ERROR_WANT_X509_LOOKUP:
                   1202:                                BIO_printf(bio_c_out, "read X BLOCK\n");
                   1203:                                break;
                   1204:                        case SSL_ERROR_SYSCALL:
                   1205:                                ret = errno;
                   1206:                                BIO_printf(bio_err, "read:errno=%d\n", ret);
                   1207:                                goto shut;
                   1208:                        case SSL_ERROR_ZERO_RETURN:
                   1209:                                BIO_printf(bio_c_out, "closed\n");
                   1210:                                ret = 0;
                   1211:                                goto shut;
                   1212:                        case SSL_ERROR_SSL:
                   1213:                                ERR_print_errors(bio_err);
                   1214:                                goto shut;
                   1215:                                /* break; */
                   1216:                        }
                   1217:                } else if (FD_ISSET(fileno(stdin), &readfds)) {
                   1218:                        if (crlf) {
                   1219:                                int j, lf_num;
                   1220:
                   1221:                                i = read(fileno(stdin), cbuf, BUFSIZZ / 2);
                   1222:                                lf_num = 0;
                   1223:                                /* both loops are skipped when i <= 0 */
                   1224:                                for (j = 0; j < i; j++)
                   1225:                                        if (cbuf[j] == '\n')
                   1226:                                                lf_num++;
                   1227:                                for (j = i - 1; j >= 0; j--) {
                   1228:                                        cbuf[j + lf_num] = cbuf[j];
                   1229:                                        if (cbuf[j] == '\n') {
                   1230:                                                lf_num--;
                   1231:                                                i++;
                   1232:                                                cbuf[j + lf_num] = '\r';
                   1233:                                        }
                   1234:                                }
                   1235:                                assert(lf_num == 0);
                   1236:                        } else
                   1237:                                i = read(fileno(stdin), cbuf, BUFSIZZ);
                   1238:
                   1239:                        if ((!c_ign_eof) && ((i <= 0) || (cbuf[0] == 'Q'))) {
                   1240:                                BIO_printf(bio_err, "DONE\n");
                   1241:                                ret = 0;
                   1242:                                goto shut;
                   1243:                        }
                   1244:                        if ((!c_ign_eof) && (cbuf[0] == 'R')) {
                   1245:                                BIO_printf(bio_err, "RENEGOTIATING\n");
                   1246:                                SSL_renegotiate(con);
                   1247:                                cbuf_len = 0;
                   1248:                        } else {
                   1249:                                cbuf_len = i;
                   1250:                                cbuf_off = 0;
                   1251:                        }
                   1252:
                   1253:                        write_ssl = 1;
                   1254:                        read_tty = 0;
                   1255:                }
                   1256:        }
                   1257:
                   1258:        ret = 0;
                   1259: shut:
                   1260:        if (in_init)
                   1261:                print_stuff(bio_c_out, con, full_log);
                   1262:        SSL_shutdown(con);
                   1263:        shutdown(SSL_get_fd(con), SHUT_RD);
                   1264:        close(SSL_get_fd(con));
                   1265: end:
                   1266:        if (con != NULL) {
                   1267:                if (prexit != 0)
                   1268:                        print_stuff(bio_c_out, con, 1);
                   1269:                SSL_free(con);
                   1270:        }
                   1271: #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
                   1272:        free(next_proto.data);
                   1273: #endif
                   1274:        if (ctx != NULL)
                   1275:                SSL_CTX_free(ctx);
                   1276:        if (cert)
                   1277:                X509_free(cert);
                   1278:        if (key)
                   1279:                EVP_PKEY_free(key);
                   1280:        free(pass);
                   1281:        if (vpm)
                   1282:                X509_VERIFY_PARAM_free(vpm);
                   1283:        if (cbuf != NULL) {
                   1284:                OPENSSL_cleanse(cbuf, BUFSIZZ);
                   1285:                free(cbuf);
                   1286:        }
                   1287:        if (sbuf != NULL) {
                   1288:                OPENSSL_cleanse(sbuf, BUFSIZZ);
                   1289:                free(sbuf);
                   1290:        }
                   1291:        if (mbuf != NULL) {
                   1292:                OPENSSL_cleanse(mbuf, BUFSIZZ);
                   1293:                free(mbuf);
                   1294:        }
                   1295:        if (bio_c_out != NULL) {
                   1296:                BIO_free(bio_c_out);
                   1297:                bio_c_out = NULL;
                   1298:        }
                   1299:
                   1300:        return (ret);
                   1301: }
                   1302:
                   1303:
                   1304: static void
                   1305: print_stuff(BIO * bio, SSL * s, int full)
                   1306: {
                   1307:        X509 *peer = NULL;
                   1308:        char *p;
                   1309:        static const char *space = "                ";
                   1310:        char buf[BUFSIZ];
                   1311:        STACK_OF(X509) * sk;
                   1312:        STACK_OF(X509_NAME) * sk2;
                   1313:        const SSL_CIPHER *c;
                   1314:        X509_NAME *xn;
                   1315:        int j, i;
                   1316:        unsigned char *exportedkeymat;
                   1317:
                   1318:        if (full) {
                   1319:                int got_a_chain = 0;
                   1320:
                   1321:                sk = SSL_get_peer_cert_chain(s);
                   1322:                if (sk != NULL) {
                   1323:                        got_a_chain = 1;        /* we don't have it for SSL2
                   1324:                                                 * (yet) */
                   1325:
                   1326:                        BIO_printf(bio, "---\nCertificate chain\n");
                   1327:                        for (i = 0; i < sk_X509_num(sk); i++) {
                   1328:                                X509_NAME_oneline(X509_get_subject_name(
                   1329:                                        sk_X509_value(sk, i)), buf, sizeof buf);
                   1330:                                BIO_printf(bio, "%2d s:%s\n", i, buf);
                   1331:                                X509_NAME_oneline(X509_get_issuer_name(
                   1332:                                        sk_X509_value(sk, i)), buf, sizeof buf);
                   1333:                                BIO_printf(bio, "   i:%s\n", buf);
                   1334:                                if (c_showcerts)
                   1335:                                        PEM_write_bio_X509(bio, sk_X509_value(sk, i));
                   1336:                        }
                   1337:                }
                   1338:                BIO_printf(bio, "---\n");
                   1339:                peer = SSL_get_peer_certificate(s);
                   1340:                if (peer != NULL) {
                   1341:                        BIO_printf(bio, "Server certificate\n");
                   1342:                        if (!(c_showcerts && got_a_chain))      /* Redundant if we
                   1343:                                                                 * showed the whole
                   1344:                                                                 * chain */
                   1345:                                PEM_write_bio_X509(bio, peer);
                   1346:                        X509_NAME_oneline(X509_get_subject_name(peer),
                   1347:                            buf, sizeof buf);
                   1348:                        BIO_printf(bio, "subject=%s\n", buf);
                   1349:                        X509_NAME_oneline(X509_get_issuer_name(peer),
                   1350:                            buf, sizeof buf);
                   1351:                        BIO_printf(bio, "issuer=%s\n", buf);
                   1352:                } else
                   1353:                        BIO_printf(bio, "no peer certificate available\n");
                   1354:
                   1355:                sk2 = SSL_get_client_CA_list(s);
                   1356:                if ((sk2 != NULL) && (sk_X509_NAME_num(sk2) > 0)) {
                   1357:                        BIO_printf(bio, "---\nAcceptable client certificate CA names\n");
                   1358:                        for (i = 0; i < sk_X509_NAME_num(sk2); i++) {
                   1359:                                xn = sk_X509_NAME_value(sk2, i);
                   1360:                                X509_NAME_oneline(xn, buf, sizeof(buf));
                   1361:                                BIO_write(bio, buf, strlen(buf));
                   1362:                                BIO_write(bio, "\n", 1);
                   1363:                        }
                   1364:                } else {
                   1365:                        BIO_printf(bio, "---\nNo client certificate CA names sent\n");
                   1366:                }
                   1367:                p = SSL_get_shared_ciphers(s, buf, sizeof buf);
                   1368:                if (p != NULL) {
                   1369:                        /*
                   1370:                         * This works only for SSL 2.  In later protocol
                   1371:                         * versions, the client does not know what other
                   1372:                         * ciphers (in addition to the one to be used in the
                   1373:                         * current connection) the server supports.
                   1374:                         */
                   1375:
                   1376:                        BIO_printf(bio, "---\nCiphers common between both SSL endpoints:\n");
                   1377:                        j = i = 0;
                   1378:                        while (*p) {
                   1379:                                if (*p == ':') {
                   1380:                                        BIO_write(bio, space, 15 - j % 25);
                   1381:                                        i++;
                   1382:                                        j = 0;
                   1383:                                        BIO_write(bio, ((i % 3) ? " " : "\n"), 1);
                   1384:                                } else {
                   1385:                                        BIO_write(bio, p, 1);
                   1386:                                        j++;
                   1387:                                }
                   1388:                                p++;
                   1389:                        }
                   1390:                        BIO_write(bio, "\n", 1);
                   1391:                }
                   1392:                BIO_printf(bio, "---\nSSL handshake has read %ld bytes and written %ld bytes\n",
                   1393:                    BIO_number_read(SSL_get_rbio(s)),
                   1394:                    BIO_number_written(SSL_get_wbio(s)));
                   1395:        }
                   1396:        BIO_printf(bio, (SSL_cache_hit(s) ? "---\nReused, " : "---\nNew, "));
                   1397:        c = SSL_get_current_cipher(s);
                   1398:        BIO_printf(bio, "%s, Cipher is %s\n",
                   1399:            SSL_CIPHER_get_version(c),
                   1400:            SSL_CIPHER_get_name(c));
                   1401:        if (peer != NULL) {
                   1402:                EVP_PKEY *pktmp;
                   1403:                pktmp = X509_get_pubkey(peer);
                   1404:                BIO_printf(bio, "Server public key is %d bit\n",
                   1405:                    EVP_PKEY_bits(pktmp));
                   1406:                EVP_PKEY_free(pktmp);
                   1407:        }
                   1408:        BIO_printf(bio, "Secure Renegotiation IS%s supported\n",
                   1409:            SSL_get_secure_renegotiation_support(s) ? "" : " NOT");
                   1410:
                   1411:        /* Compression is not supported and will always be none. */
                   1412:        BIO_printf(bio, "Compression: NONE\n");
                   1413:        BIO_printf(bio, "Expansion: NONE\n");
                   1414:
                   1415: #ifdef SSL_DEBUG
                   1416:        {
                   1417:                /* Print out local port of connection: useful for debugging */
                   1418:                int sock;
                   1419:                struct sockaddr_in ladd;
                   1420:                socklen_t ladd_size = sizeof(ladd);
                   1421:                sock = SSL_get_fd(s);
                   1422:                getsockname(sock, (struct sockaddr *) & ladd, &ladd_size);
                   1423:                BIO_printf(bio_c_out, "LOCAL PORT is %u\n", ntohs(ladd.sin_port));
                   1424:        }
                   1425: #endif
                   1426:
                   1427: #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
                   1428:        if (next_proto.status != -1) {
                   1429:                const unsigned char *proto;
                   1430:                unsigned int proto_len;
                   1431:                SSL_get0_next_proto_negotiated(s, &proto, &proto_len);
                   1432:                BIO_printf(bio, "Next protocol: (%d) ", next_proto.status);
                   1433:                BIO_write(bio, proto, proto_len);
                   1434:                BIO_write(bio, "\n", 1);
                   1435:        }
                   1436: #endif
                   1437:
                   1438: #ifndef OPENSSL_NO_SRTP
                   1439:        {
                   1440:                SRTP_PROTECTION_PROFILE *srtp_profile = SSL_get_selected_srtp_profile(s);
                   1441:
                   1442:                if (srtp_profile)
                   1443:                        BIO_printf(bio, "SRTP Extension negotiated, profile=%s\n",
                   1444:                            srtp_profile->name);
                   1445:        }
                   1446: #endif
                   1447:
                   1448:        SSL_SESSION_print(bio, SSL_get_session(s));
                   1449:        if (keymatexportlabel != NULL) {
                   1450:                BIO_printf(bio, "Keying material exporter:\n");
                   1451:                BIO_printf(bio, "    Label: '%s'\n", keymatexportlabel);
                   1452:                BIO_printf(bio, "    Length: %i bytes\n", keymatexportlen);
                   1453:                exportedkeymat = malloc(keymatexportlen);
                   1454:                if (exportedkeymat != NULL) {
                   1455:                        if (!SSL_export_keying_material(s, exportedkeymat,
                   1456:                                keymatexportlen,
                   1457:                                keymatexportlabel,
                   1458:                                strlen(keymatexportlabel),
                   1459:                                NULL, 0, 0)) {
                   1460:                                BIO_printf(bio, "    Error\n");
                   1461:                        } else {
                   1462:                                BIO_printf(bio, "    Keying material: ");
                   1463:                                for (i = 0; i < keymatexportlen; i++)
                   1464:                                        BIO_printf(bio, "%02X",
                   1465:                                            exportedkeymat[i]);
                   1466:                                BIO_printf(bio, "\n");
                   1467:                        }
                   1468:                        free(exportedkeymat);
                   1469:                }
                   1470:        }
                   1471:        BIO_printf(bio, "---\n");
                   1472:        if (peer != NULL)
                   1473:                X509_free(peer);
                   1474:        /* flush, or debugging output gets mixed with http response */
                   1475:        (void) BIO_flush(bio);
                   1476: }
                   1477:
                   1478: #ifndef OPENSSL_NO_TLSEXT
                   1479:
                   1480: static int
                   1481: ocsp_resp_cb(SSL * s, void *arg)
                   1482: {
                   1483:        const unsigned char *p;
                   1484:        int len;
                   1485:        OCSP_RESPONSE *rsp;
                   1486:        len = SSL_get_tlsext_status_ocsp_resp(s, &p);
                   1487:        BIO_puts(arg, "OCSP response: ");
                   1488:        if (!p) {
                   1489:                BIO_puts(arg, "no response sent\n");
                   1490:                return 1;
                   1491:        }
                   1492:        rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
                   1493:        if (!rsp) {
                   1494:                BIO_puts(arg, "response parse error\n");
                   1495:                BIO_dump_indent(arg, (char *) p, len, 4);
                   1496:                return 0;
                   1497:        }
                   1498:        BIO_puts(arg, "\n======================================\n");
                   1499:        OCSP_RESPONSE_print(arg, rsp, 0);
                   1500:        BIO_puts(arg, "======================================\n");
                   1501:        OCSP_RESPONSE_free(rsp);
                   1502:        return 1;
                   1503: }
                   1504:
                   1505: #endif