[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.63

1.63    ! tb          1: /* $OpenBSD: s_client.c,v 1.62 2023/07/03 08:03:56 beck 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/socket.h>
                    140:
                    141: #include <netinet/in.h>
                    142:
                    143: #include <assert.h>
                    144: #include <ctype.h>
                    145: #include <limits.h>
                    146: #include <netdb.h>
                    147: #include <stdio.h>
                    148: #include <stdlib.h>
                    149: #include <string.h>
                    150: #include <unistd.h>
1.9       deraadt   151: #include <poll.h>
1.1       jsing     152:
                    153: #include "apps.h"
                    154:
                    155: #include <openssl/bn.h>
                    156: #include <openssl/err.h>
                    157: #include <openssl/ocsp.h>
                    158: #include <openssl/pem.h>
                    159: #include <openssl/ssl.h>
                    160: #include <openssl/x509.h>
                    161:
                    162: #include "s_apps.h"
                    163: #include "timeouts.h"
                    164:
                    165: /*#define SSL_HOST_NAME        "www.netscape.com" */
                    166: /*#define SSL_HOST_NAME        "193.118.187.102" */
                    167: #define SSL_HOST_NAME  "localhost"
                    168:
                    169:  /*#define TEST_CERT "client.pem" *//* no default cert. */
                    170:
                    171: #define BUFSIZZ 1024*8
                    172:
                    173: static void sc_usage(void);
1.51      inoguchi  174: static void print_stuff(BIO *berr, SSL *con, int full);
                    175: static int ocsp_resp_cb(SSL *s, void *arg);
                    176: static int ssl_servername_cb(SSL *s, int *ad, void *arg);
1.1       jsing     177:
1.47      inoguchi  178: enum {
                    179:        PROTO_OFF = 0,
                    180:        PROTO_SMTP,
                    181:        PROTO_LMTP,
                    182:        PROTO_POP3,
                    183:        PROTO_IMAP,
                    184:        PROTO_FTP,
                    185:        PROTO_XMPP,
                    186: };
                    187:
1.51      inoguchi  188: /* This is a context that we pass to callbacks */
                    189: typedef struct tlsextctx_st {
                    190:        BIO *biodebug;
                    191:        int ack;
                    192: } tlsextctx;
                    193:
1.47      inoguchi  194: static struct {
                    195:        int af;
                    196:        char *alpn_in;
                    197:        int bugs;
                    198:        char *CAfile;
                    199:        char *CApath;
                    200:        char *cert_file;
                    201:        int cert_format;
                    202:        char *cipher;
                    203:        unsigned int clr;
                    204:        char *connect;
                    205:        int crlf;
1.48      inoguchi  206:        int debug;
1.47      inoguchi  207:        int enable_timeouts;
                    208:        const char *errstr;
                    209:        char *groups_in;
                    210:        char *host;
1.48      inoguchi  211:        int ign_eof;
1.47      inoguchi  212:        char *key_file;
                    213:        int key_format;
                    214:        char *keymatexportlabel;
                    215:        int keymatexportlen;
                    216:        uint16_t max_version;
                    217:        uint16_t min_version;
                    218:        const SSL_METHOD *meth;
1.48      inoguchi  219:        int msg;
                    220:        int nbio;
1.47      inoguchi  221:        int nbio_test;
1.57      jsing     222:        int no_servername;
1.47      inoguchi  223:        char *npn_in;
                    224:        unsigned int off;
                    225:        char *passarg;
                    226:        int peekaboo;
                    227:        char *port;
                    228:        int prexit;
                    229:        char *proxy;
1.48      inoguchi  230:        int quiet;
1.47      inoguchi  231:        int reconnect;
                    232:        char *servername;
                    233:        char *sess_in;
                    234:        char *sess_out;
1.48      inoguchi  235:        int showcerts;
1.47      inoguchi  236:        int socket_type;
                    237:        long socket_mtu;
                    238: #ifndef OPENSSL_NO_SRTP
                    239:        char *srtp_profiles;
                    240: #endif
                    241:        int starttls_proto;
                    242:        int state;
1.48      inoguchi  243:        int status_req;
                    244:        int tlsextdebug;
1.47      inoguchi  245:        int verify;
                    246:        X509_VERIFY_PARAM *vpm;
                    247:        char *xmpphost;
1.60      tb        248: } cfg;
1.47      inoguchi  249:
                    250: static int
                    251: s_client_opt_keymatexportlen(char *arg)
                    252: {
1.60      tb        253:        cfg.keymatexportlen = strtonum(arg, 1, INT_MAX,
                    254:            &cfg.errstr);
                    255:        if (cfg.errstr != NULL) {
1.47      inoguchi  256:                BIO_printf(bio_err, "invalid argument %s: %s\n",
1.60      tb        257:                    arg, cfg.errstr);
1.47      inoguchi  258:                return (1);
                    259:        }
                    260:        return (0);
                    261: }
                    262:
1.53      jsing     263: #ifndef OPENSSL_NO_DTLS
1.47      inoguchi  264: static int
                    265: s_client_opt_mtu(char *arg)
                    266: {
1.60      tb        267:        cfg.socket_mtu = strtonum(arg, 0, LONG_MAX,
                    268:            &cfg.errstr);
                    269:        if (cfg.errstr != NULL) {
1.47      inoguchi  270:                BIO_printf(bio_err, "invalid argument %s: %s\n",
1.60      tb        271:                    arg, cfg.errstr);
1.47      inoguchi  272:                return (1);
                    273:        }
                    274:        return (0);
                    275: }
                    276: #endif
                    277:
                    278: static int
                    279: s_client_opt_port(char *arg)
                    280: {
                    281:        if (*arg == '\0')
                    282:                return (1);
                    283:
1.60      tb        284:        cfg.port = arg;
1.47      inoguchi  285:        return (0);
                    286: }
                    287:
1.53      jsing     288: #ifndef OPENSSL_NO_DTLS
                    289: static int
                    290: s_client_opt_protocol_version_dtls(void)
                    291: {
1.60      tb        292:        cfg.meth = DTLS_client_method();
                    293:        cfg.socket_type = SOCK_DGRAM;
1.53      jsing     294:        return (0);
                    295: }
                    296: #endif
                    297:
                    298: #ifndef OPENSSL_NO_DTLS1_2
                    299: static int
                    300: s_client_opt_protocol_version_dtls1_2(void)
                    301: {
1.60      tb        302:        cfg.meth = DTLS_client_method();
                    303:        cfg.min_version = DTLS1_2_VERSION;
                    304:        cfg.max_version = DTLS1_2_VERSION;
                    305:        cfg.socket_type = SOCK_DGRAM;
1.47      inoguchi  306:        return (0);
                    307: }
                    308: #endif
                    309:
                    310: static int
                    311: s_client_opt_protocol_version_tls1_2(void)
                    312: {
1.60      tb        313:        cfg.min_version = TLS1_2_VERSION;
                    314:        cfg.max_version = TLS1_2_VERSION;
1.47      inoguchi  315:        return (0);
                    316: }
                    317:
                    318: static int
                    319: s_client_opt_protocol_version_tls1_3(void)
                    320: {
1.60      tb        321:        cfg.min_version = TLS1_3_VERSION;
                    322:        cfg.max_version = TLS1_3_VERSION;
1.47      inoguchi  323:        return (0);
                    324: }
                    325:
                    326: static int
                    327: s_client_opt_quiet(void)
                    328: {
1.60      tb        329:        cfg.quiet = 1;
                    330:        cfg.ign_eof = 1;
1.47      inoguchi  331:        return (0);
                    332: }
                    333:
                    334: static int
                    335: s_client_opt_starttls(char *arg)
                    336: {
                    337:        if (strcmp(arg, "smtp") == 0)
1.60      tb        338:                cfg.starttls_proto = PROTO_SMTP;
1.47      inoguchi  339:        else if (strcmp(arg, "lmtp") == 0)
1.60      tb        340:                cfg.starttls_proto = PROTO_LMTP;
1.47      inoguchi  341:        else if (strcmp(arg, "pop3") == 0)
1.60      tb        342:                cfg.starttls_proto = PROTO_POP3;
1.47      inoguchi  343:        else if (strcmp(arg, "imap") == 0)
1.60      tb        344:                cfg.starttls_proto = PROTO_IMAP;
1.47      inoguchi  345:        else if (strcmp(arg, "ftp") == 0)
1.60      tb        346:                cfg.starttls_proto = PROTO_FTP;
1.47      inoguchi  347:        else if (strcmp(arg, "xmpp") == 0)
1.60      tb        348:                cfg.starttls_proto = PROTO_XMPP;
1.47      inoguchi  349:        else
                    350:                return (1);
                    351:        return (0);
                    352: }
                    353:
                    354: static int
                    355: s_client_opt_verify(char *arg)
                    356: {
1.60      tb        357:        cfg.verify = SSL_VERIFY_PEER;
1.47      inoguchi  358:
1.60      tb        359:        verify_depth = strtonum(arg, 0, INT_MAX, &cfg.errstr);
                    360:        if (cfg.errstr != NULL) {
1.47      inoguchi  361:                BIO_printf(bio_err, "invalid argument %s: %s\n",
1.60      tb        362:                    arg, cfg.errstr);
1.47      inoguchi  363:                return (1);
                    364:        }
                    365:        BIO_printf(bio_err, "verify depth is %d\n", verify_depth);
                    366:        return (0);
                    367: }
                    368:
                    369: static int
                    370: s_client_opt_verify_param(int argc, char **argv, int *argsused)
                    371: {
                    372:        char **pargs = argv;
                    373:        int pargc = argc;
                    374:        int badarg = 0;
                    375:
                    376:        if (!args_verify(&pargs, &pargc, &badarg, bio_err,
1.60      tb        377:            &cfg.vpm)) {
1.47      inoguchi  378:                BIO_printf(bio_err, "unknown option %s\n", *argv);
                    379:                return (1);
                    380:        }
                    381:        if (badarg)
                    382:                return (1);
                    383:
                    384:        *argsused = argc - pargc;
                    385:        return (0);
                    386: }
                    387:
                    388: static const struct option s_client_options[] = {
                    389:        {
                    390:                .name = "4",
                    391:                .desc = "Use IPv4 only",
                    392:                .type = OPTION_VALUE,
1.60      tb        393:                .opt.value = &cfg.af,
1.47      inoguchi  394:                .value = AF_INET,
                    395:        },
                    396:        {
                    397:                .name = "6",
                    398:                .desc = "Use IPv6 only",
                    399:                .type = OPTION_VALUE,
1.60      tb        400:                .opt.value = &cfg.af,
1.47      inoguchi  401:                .value = AF_INET6,
                    402:        },
                    403:        {
                    404:                .name = "alpn",
                    405:                .argname = "protocols",
                    406:                .desc = "Set the advertised protocols for ALPN"
                    407:                        " (comma-separated list)",
                    408:                .type = OPTION_ARG,
1.60      tb        409:                .opt.arg = &cfg.alpn_in,
1.47      inoguchi  410:        },
                    411:        {
                    412:                .name = "bugs",
                    413:                .desc = "Enable various workarounds for buggy implementations",
                    414:                .type = OPTION_FLAG,
1.60      tb        415:                .opt.flag = &cfg.bugs,
1.47      inoguchi  416:        },
                    417:        {
                    418:                .name = "CAfile",
                    419:                .argname = "file",
                    420:                .desc = "PEM format file of CA certificates",
                    421:                .type = OPTION_ARG,
1.60      tb        422:                .opt.arg = &cfg.CAfile,
1.47      inoguchi  423:        },
                    424:        {
                    425:                .name = "CApath",
                    426:                .argname = "directory",
                    427:                .desc = "PEM format directory of CA certificates",
                    428:                .type = OPTION_ARG,
1.60      tb        429:                .opt.arg = &cfg.CApath,
1.47      inoguchi  430:        },
                    431:        {
                    432:                .name = "cert",
                    433:                .argname = "file",
                    434:                .desc = "Certificate file to use, PEM format assumed",
                    435:                .type = OPTION_ARG,
1.60      tb        436:                .opt.arg = &cfg.cert_file,
1.47      inoguchi  437:        },
                    438:        {
                    439:                .name = "certform",
                    440:                .argname = "fmt",
                    441:                .desc = "Certificate format (PEM or DER) PEM default",
                    442:                .type = OPTION_ARG_FORMAT,
1.60      tb        443:                .opt.value = &cfg.cert_format,
1.47      inoguchi  444:        },
                    445:        {
                    446:                .name = "cipher",
                    447:                .argname = "cipherlist",
                    448:                .desc = "Preferred cipher to use (see 'openssl ciphers')",
                    449:                .type = OPTION_ARG,
1.60      tb        450:                .opt.arg = &cfg.cipher,
1.47      inoguchi  451:        },
                    452:        {
                    453:                .name = "connect",
                    454:                .argname = "host:port",
                    455:                .desc = "Who to connect to (default is localhost:4433)",
                    456:                .type = OPTION_ARG,
1.60      tb        457:                .opt.arg = &cfg.connect,
1.47      inoguchi  458:        },
                    459:        {
                    460:                .name = "crlf",
                    461:                .desc = "Convert LF from terminal into CRLF",
                    462:                .type = OPTION_FLAG,
1.60      tb        463:                .opt.flag = &cfg.crlf,
1.47      inoguchi  464:        },
                    465:        {
                    466:                .name = "debug",
                    467:                .desc = "Print extensive debugging information",
                    468:                .type = OPTION_FLAG,
1.60      tb        469:                .opt.flag = &cfg.debug,
1.47      inoguchi  470:        },
1.53      jsing     471: #ifndef OPENSSL_NO_DTLS
                    472:        {
                    473:                .name = "dtls",
                    474:                .desc = "Use any version of DTLS",
                    475:                .type = OPTION_FUNC,
                    476:                .opt.func = s_client_opt_protocol_version_dtls,
                    477:        },
                    478: #endif
                    479: #ifndef OPENSSL_NO_DTLS1_2
                    480:        {
                    481:                .name = "dtls1_2",
                    482:                .desc = "Just use DTLSv1.2",
                    483:                .type = OPTION_FUNC,
                    484:                .opt.func = s_client_opt_protocol_version_dtls1_2,
                    485:        },
                    486: #endif
1.47      inoguchi  487:        {
                    488:                .name = "groups",
                    489:                .argname = "list",
                    490:                .desc = "Specify EC groups (colon-separated list)",
                    491:                .type = OPTION_ARG,
1.60      tb        492:                .opt.arg = &cfg.groups_in,
1.47      inoguchi  493:        },
                    494:        {
                    495:                .name = "host",
                    496:                .argname = "host",
                    497:                .desc = "Use -connect instead",
                    498:                .type = OPTION_ARG,
1.60      tb        499:                .opt.arg = &cfg.host,
1.47      inoguchi  500:        },
                    501:        {
                    502:                .name = "ign_eof",
                    503:                .desc = "Ignore input EOF (default when -quiet)",
                    504:                .type = OPTION_VALUE,
1.60      tb        505:                .opt.value = &cfg.ign_eof,
1.47      inoguchi  506:                .value = 1,
                    507:        },
                    508:        {
                    509:                .name = "key",
                    510:                .argname = "file",
                    511:                .desc = "Private key file to use, if not, -cert file is used",
                    512:                .type = OPTION_ARG,
1.60      tb        513:                .opt.arg = &cfg.key_file,
1.47      inoguchi  514:        },
                    515:        {
                    516:                .name = "keyform",
                    517:                .argname = "fmt",
                    518:                .desc = "Key format (PEM or DER) PEM default",
                    519:                .type = OPTION_ARG_FORMAT,
1.60      tb        520:                .opt.value = &cfg.key_format,
1.47      inoguchi  521:        },
                    522:        {
                    523:                .name = "keymatexport",
                    524:                .argname = "label",
                    525:                .desc = "Export keying material using label",
                    526:                .type = OPTION_ARG,
1.60      tb        527:                .opt.arg = &cfg.keymatexportlabel,
1.47      inoguchi  528:        },
                    529:        {
                    530:                .name = "keymatexportlen",
                    531:                .argname = "len",
                    532:                .desc = "Export len bytes of keying material (default 20)",
                    533:                .type = OPTION_ARG_FUNC,
                    534:                .opt.argfunc = s_client_opt_keymatexportlen,
                    535:        },
                    536:        {
                    537:                .name = "legacy_renegotiation",
                    538:                .type = OPTION_DISCARD,
                    539:        },
                    540:        {
                    541:                .name = "legacy_server_connect",
                    542:                .desc = "Allow initial connection to servers that don't support RI",
                    543:                .type = OPTION_VALUE_OR,
1.60      tb        544:                .opt.value = &cfg.off,
1.47      inoguchi  545:                .value = SSL_OP_LEGACY_SERVER_CONNECT,
                    546:        },
                    547:        {
                    548:                .name = "msg",
                    549:                .desc = "Show all protocol messages with hex dump",
                    550:                .type = OPTION_FLAG,
1.60      tb        551:                .opt.flag = &cfg.msg,
1.47      inoguchi  552:        },
1.53      jsing     553: #ifndef OPENSSL_NO_DTLS
1.47      inoguchi  554:        {
                    555:                .name = "mtu",
                    556:                .argname = "mtu",
                    557:                .desc = "Set the link layer MTU on DTLS connections",
                    558:                .type = OPTION_ARG_FUNC,
                    559:                .opt.argfunc = s_client_opt_mtu,
                    560:        },
                    561: #endif
                    562:        {
                    563:                .name = "nbio",
                    564:                .desc = "Turn on non-blocking I/O",
                    565:                .type = OPTION_FLAG,
1.60      tb        566:                .opt.flag = &cfg.nbio,
1.47      inoguchi  567:        },
                    568:        {
                    569:                .name = "nbio_test",
                    570:                .desc = "Test non-blocking I/O",
                    571:                .type = OPTION_FLAG,
1.60      tb        572:                .opt.flag = &cfg.nbio_test,
1.47      inoguchi  573:        },
                    574:        {
                    575:                .name = "nextprotoneg",
                    576:                .argname = "protocols",
                    577:                .type = OPTION_ARG,
1.60      tb        578:                .opt.arg = &cfg.npn_in, /* Ignored. */
1.47      inoguchi  579:        },
                    580:        {
                    581:                .name = "no_comp",
                    582:                .type = OPTION_VALUE_OR,
1.60      tb        583:                .opt.value = &cfg.off,
1.47      inoguchi  584:                .value = SSL_OP_NO_COMPRESSION,
                    585:        },
                    586:        {
                    587:                .name = "no_ign_eof",
                    588:                .desc = "Don't ignore input EOF",
                    589:                .type = OPTION_VALUE,
1.60      tb        590:                .opt.value = &cfg.ign_eof,
1.47      inoguchi  591:                .value = 0,
                    592:        },
                    593:        {
                    594:                .name = "no_legacy_server_connect",
                    595:                .desc = "Disallow initial connection to servers that don't support RI",
                    596:                .type = OPTION_VALUE_OR,
1.60      tb        597:                .opt.value = &cfg.clr,
1.47      inoguchi  598:                .value = SSL_OP_LEGACY_SERVER_CONNECT,
                    599:        },
                    600:        {
1.57      jsing     601:                .name = "no_servername",
                    602:                .desc = "Do not send a Server Name Indication (SNI) extension",
                    603:                .type = OPTION_FLAG,
1.60      tb        604:                .opt.value = &cfg.no_servername,
1.57      jsing     605:        },
                    606:        {
1.47      inoguchi  607:                .name = "no_ssl2",
                    608:                .type = OPTION_VALUE_OR,
1.60      tb        609:                .opt.value = &cfg.off,
1.47      inoguchi  610:                .value = SSL_OP_NO_SSLv2,
                    611:        },
                    612:        {
                    613:                .name = "no_ssl3",
                    614:                .type = OPTION_VALUE_OR,
1.60      tb        615:                .opt.value = &cfg.off,
1.47      inoguchi  616:                .value = SSL_OP_NO_SSLv3,
                    617:        },
                    618:        {
                    619:                .name = "no_ticket",
                    620:                .desc = "Disable use of RFC4507 session ticket support",
                    621:                .type = OPTION_VALUE_OR,
1.60      tb        622:                .opt.value = &cfg.off,
1.47      inoguchi  623:                .value = SSL_OP_NO_TICKET,
1.62      beck      624:        },
                    625:        {
                    626:                .name = "no_tls1",
                    627:                .type = OPTION_DISCARD,
                    628:        },
                    629:        {
                    630:                .name = "no_tls1_1",
                    631:                .type = OPTION_DISCARD,
1.47      inoguchi  632:        },
                    633:        {
                    634:                .name = "no_tls1_2",
                    635:                .desc = "Disable the use of TLSv1.2",
                    636:                .type = OPTION_VALUE_OR,
1.60      tb        637:                .opt.value = &cfg.off,
1.47      inoguchi  638:                .value = SSL_OP_NO_TLSv1_2,
                    639:        },
                    640:        {
                    641:                .name = "no_tls1_3",
                    642:                .desc = "Disable the use of TLSv1.3",
                    643:                .type = OPTION_VALUE_OR,
1.60      tb        644:                .opt.value = &cfg.off,
1.47      inoguchi  645:                .value = SSL_OP_NO_TLSv1_3,
                    646:        },
                    647:        {
1.57      jsing     648:                .name = "noservername",
                    649:                .type = OPTION_FLAG,
1.60      tb        650:                .opt.value = &cfg.no_servername,
1.57      jsing     651:        },
                    652:        {
1.47      inoguchi  653:                .name = "pass",
                    654:                .argname = "arg",
                    655:                .desc = "Private key file pass phrase source",
                    656:                .type = OPTION_ARG,
1.60      tb        657:                .opt.arg = &cfg.passarg,
1.47      inoguchi  658:        },
                    659:        {
                    660:                .name = "pause",
1.63    ! tb        661:                .type = OPTION_DISCARD,
1.47      inoguchi  662:        },
                    663:        {
                    664:                .name = "peekaboo",
                    665:                .type = OPTION_FLAG,
1.60      tb        666:                .opt.flag = &cfg.peekaboo,
1.47      inoguchi  667:        },
                    668:        {
                    669:                .name = "port",
                    670:                .argname = "port",
                    671:                .desc = "Use -connect instead",
                    672:                .type = OPTION_ARG_FUNC,
                    673:                .opt.argfunc = s_client_opt_port,
                    674:        },
                    675:        {
                    676:                .name = "prexit",
                    677:                .desc = "Print session information when the program exits",
                    678:                .type = OPTION_FLAG,
1.60      tb        679:                .opt.flag = &cfg.prexit,
1.47      inoguchi  680:        },
                    681:        {
                    682:                .name = "proxy",
                    683:                .argname = "host:port",
                    684:                .desc = "Connect to http proxy",
                    685:                .type = OPTION_ARG,
1.60      tb        686:                .opt.arg = &cfg.proxy,
1.47      inoguchi  687:        },
                    688:        {
                    689:                .name = "quiet",
                    690:                .desc = "Inhibit printing of session and certificate info",
                    691:                .type = OPTION_FUNC,
                    692:                .opt.func = s_client_opt_quiet,
                    693:        },
                    694:        {
                    695:                .name = "reconnect",
                    696:                .desc = "Drop and re-make the connection with the same Session-ID",
                    697:                .type = OPTION_VALUE,
1.60      tb        698:                .opt.value = &cfg.reconnect,
1.47      inoguchi  699:                .value = 5,
                    700:        },
                    701:        {
                    702:                .name = "servername",
                    703:                .argname = "name",
                    704:                .desc = "Set TLS extension servername in ClientHello (SNI)",
                    705:                .type = OPTION_ARG,
1.60      tb        706:                .opt.arg = &cfg.servername,
1.47      inoguchi  707:        },
                    708:        {
                    709:                .name = "serverpref",
                    710:                .desc = "Use server's cipher preferences",
                    711:                .type = OPTION_VALUE_OR,
1.60      tb        712:                .opt.value = &cfg.off,
1.47      inoguchi  713:                .value = SSL_OP_CIPHER_SERVER_PREFERENCE,
                    714:        },
                    715:        {
                    716:                .name = "sess_in",
                    717:                .argname = "file",
                    718:                .desc = "File to read TLS session from",
                    719:                .type = OPTION_ARG,
1.60      tb        720:                .opt.arg = &cfg.sess_in,
1.47      inoguchi  721:        },
                    722:        {
                    723:                .name = "sess_out",
                    724:                .argname = "file",
                    725:                .desc = "File to write TLS session to",
                    726:                .type = OPTION_ARG,
1.60      tb        727:                .opt.arg = &cfg.sess_out,
1.47      inoguchi  728:        },
                    729:        {
                    730:                .name = "showcerts",
                    731:                .desc = "Show all server certificates in the chain",
                    732:                .type = OPTION_FLAG,
1.60      tb        733:                .opt.flag = &cfg.showcerts,
1.47      inoguchi  734:        },
                    735:        {
                    736:                .name = "starttls",
                    737:                .argname = "protocol",
                    738:                .desc = "Use the STARTTLS command before starting TLS,\n"
                    739:                        "smtp, lmtp, pop3, imap, ftp and xmpp are supported.",
                    740:                .type = OPTION_ARG_FUNC,
                    741:                .opt.argfunc = s_client_opt_starttls,
                    742:        },
                    743:        {
                    744:                .name = "state",
                    745:                .desc = "Print the TLS session states",
                    746:                .type = OPTION_FLAG,
1.60      tb        747:                .opt.flag = &cfg.state,
1.47      inoguchi  748:        },
                    749:        {
                    750:                .name = "status",
                    751:                .desc = "Send a certificate status request to the server (OCSP)",
                    752:                .type = OPTION_FLAG,
1.60      tb        753:                .opt.flag = &cfg.status_req,
1.47      inoguchi  754:        },
1.53      jsing     755: #ifndef OPENSSL_NO_DTLS
1.47      inoguchi  756:        {
                    757:                .name = "timeout",
                    758:                .desc = "Enable send/receive timeout on DTLS connections",
                    759:                .type = OPTION_FLAG,
1.60      tb        760:                .opt.flag = &cfg.enable_timeouts,
1.47      inoguchi  761:        },
                    762: #endif
                    763:        {
                    764:                .name = "tls1_2",
                    765:                .desc = "Just use TLSv1.2",
                    766:                .type = OPTION_FUNC,
                    767:                .opt.func = s_client_opt_protocol_version_tls1_2,
                    768:        },
                    769:        {
                    770:                .name = "tls1_3",
                    771:                .desc = "Just use TLSv1.3",
                    772:                .type = OPTION_FUNC,
                    773:                .opt.func = s_client_opt_protocol_version_tls1_3,
                    774:        },
                    775:        {
                    776:                .name = "tlsextdebug",
                    777:                .desc = "Hex dump of all TLS extensions received",
                    778:                .type = OPTION_FLAG,
1.60      tb        779:                .opt.flag = &cfg.tlsextdebug,
1.47      inoguchi  780:        },
                    781: #ifndef OPENSSL_NO_SRTP
                    782:        {
                    783:                .name = "use_srtp",
                    784:                .argname = "profiles",
                    785:                .desc = "Offer SRTP key management with a colon-separated profiles",
                    786:                .type = OPTION_ARG,
1.60      tb        787:                .opt.arg = &cfg.srtp_profiles,
1.47      inoguchi  788:        },
                    789: #endif
                    790:        {
                    791:                .name = "verify",
                    792:                .argname = "depth",
                    793:                .desc = "Turn on peer certificate verification, with a max of depth",
                    794:                .type = OPTION_ARG_FUNC,
                    795:                .opt.argfunc = s_client_opt_verify,
                    796:        },
                    797:        {
                    798:                .name = "verify_return_error",
                    799:                .desc = "Return verification error",
                    800:                .type = OPTION_FLAG,
                    801:                .opt.flag = &verify_return_error,
                    802:        },
                    803:        {
                    804:                .name = "xmpphost",
                    805:                .argname = "host",
                    806:                .desc = "Connect to this virtual host on the xmpp server",
                    807:                .type = OPTION_ARG,
1.60      tb        808:                .opt.arg = &cfg.xmpphost,
1.47      inoguchi  809:        },
                    810:        {
                    811:                .name = NULL,
                    812:                .desc = "",
                    813:                .type = OPTION_ARGV_FUNC,
                    814:                .opt.argvfunc = s_client_opt_verify_param,
                    815:        },
                    816:        { NULL },
                    817: };
1.1       jsing     818:
                    819: static void
                    820: sc_usage(void)
                    821: {
1.47      inoguchi  822:        fprintf(stderr, "usage: s_client "
                    823:            "[-4 | -6] [-alpn protocols] [-bugs] [-CAfile file]\n"
                    824:            "    [-CApath directory] [-cert file] [-certform der | pem] [-check_ss_sig]\n"
                    825:            "    [-cipher cipherlist] [-connect host[:port]] [-crl_check]\n"
1.61      beck      826:            "    [-crl_check_all] [-crlf] [-debug] [-dtls] [-dtls1_2] [-extended_crl]\n"
1.47      inoguchi  827:            "    [-groups list] [-host host] [-ign_eof] [-ignore_critical]\n"
                    828:            "    [-issuer_checks] [-key keyfile] [-keyform der | pem]\n"
                    829:            "    [-keymatexport label] [-keymatexportlen len] [-legacy_server_connect]\n"
                    830:            "    [-msg] [-mtu mtu] [-nbio] [-nbio_test] [-no_comp] [-no_ign_eof]\n"
1.61      beck      831:            "    [-no_legacy_server_connect] [-no_ticket] \n"
1.63    ! tb        832:            "    [-no_tls1_2] [-no_tls1_3] [-pass arg] [-policy_check]\n"
1.47      inoguchi  833:            "    [-port port] [-prexit] [-proxy host:port] [-quiet] [-reconnect]\n"
                    834:            "    [-servername name] [-serverpref] [-sess_in file] [-sess_out file]\n"
                    835:            "    [-showcerts] [-starttls protocol] [-state] [-status] [-timeout]\n"
1.61      beck      836:            "    [-tls1_2] [-tls1_3] [-tlsextdebug]\n"
1.47      inoguchi  837:            "    [-use_srtp profiles] [-verify depth] [-verify_return_error]\n"
                    838:            "    [-x509_strict] [-xmpphost host]\n");
                    839:        fprintf(stderr, "\n");
                    840:        options_usage(s_client_options);
                    841:        fprintf(stderr, "\n");
1.1       jsing     842: }
                    843:
                    844: int
                    845: s_client_main(int argc, char **argv)
                    846: {
                    847:        SSL *con = NULL;
1.47      inoguchi  848:        int s, k, p = 0, pending = 0;
1.39      beck      849:        char *cbuf = NULL, *sbuf = NULL, *mbuf = NULL, *pbuf = NULL;
1.1       jsing     850:        int cbuf_len, cbuf_off;
                    851:        int sbuf_len, sbuf_off;
                    852:        int full_log = 1;
1.57      jsing     853:        const char *servername;
1.47      inoguchi  854:        char *pass = NULL;
1.1       jsing     855:        X509 *cert = NULL;
                    856:        EVP_PKEY *key = NULL;
1.47      inoguchi  857:        int badop = 0;
1.1       jsing     858:        int write_tty, read_tty, write_ssl, read_ssl, tty_on, ssl_pending;
                    859:        SSL_CTX *ctx = NULL;
1.47      inoguchi  860:        int ret = 1, in_init = 1, i;
1.50      inoguchi  861:        BIO *bio_c_out = NULL;
1.1       jsing     862:        BIO *sbio;
                    863:        int mbuf_len = 0;
1.9       deraadt   864:        struct timeval timeout;
1.33      jsing     865:        tlsextctx tlsextcbp = {NULL, 0};
1.45      deraadt   866:        struct sockaddr_storage peer;
1.1       jsing     867:        int peerlen = sizeof(peer);
1.21      doug      868:
1.59      joshua    869:        if (pledge("stdio cpath wpath rpath inet dns tty", NULL) == -1) {
                    870:                perror("pledge");
                    871:                exit(1);
1.21      doug      872:        }
1.1       jsing     873:
1.60      tb        874:        memset(&cfg, 0, sizeof(cfg));
                    875:        cfg.af = AF_UNSPEC;
                    876:        cfg.cert_format = FORMAT_PEM;
                    877:        cfg.host = SSL_HOST_NAME;
                    878:        cfg.key_format = FORMAT_PEM;
                    879:        cfg.keymatexportlen = 20;
                    880:        cfg.meth = TLS_client_method();
                    881:        cfg.port = PORT_STR;
                    882:        cfg.socket_type = SOCK_STREAM;
                    883:        cfg.starttls_proto = PROTO_OFF;
                    884:        cfg.verify = SSL_VERIFY_NONE;
1.1       jsing     885:
                    886:        if (((cbuf = malloc(BUFSIZZ)) == NULL) ||
                    887:            ((sbuf = malloc(BUFSIZZ)) == NULL) ||
1.39      beck      888:            ((pbuf = malloc(BUFSIZZ)) == NULL) ||
1.1       jsing     889:            ((mbuf = malloc(BUFSIZZ + 1)) == NULL)) {   /* NUL byte */
                    890:                BIO_printf(bio_err, "out of memory\n");
                    891:                goto end;
                    892:        }
                    893:        verify_depth = 0;
                    894:
1.47      inoguchi  895:        if (options_parse(argc, argv, s_client_options, NULL, NULL) != 0) {
                    896:                badop = 1;
                    897:                goto bad;
1.1       jsing     898:        }
1.60      tb        899:        if (cfg.proxy != NULL) {
                    900:                if (!extract_host_port(cfg.proxy,
                    901:                    &cfg.host, NULL, &cfg.port))
1.12      bluhm     902:                        goto bad;
1.60      tb        903:                if (cfg.connect == NULL)
                    904:                        cfg.connect = SSL_HOST_NAME;
                    905:        } else if (cfg.connect != NULL) {
                    906:                if (!extract_host_port(cfg.connect,
                    907:                    &cfg.host, NULL, &cfg.port))
1.12      bluhm     908:                        goto bad;
                    909:        }
1.1       jsing     910:        if (badop) {
1.35      jsing     911:  bad:
1.60      tb        912:                if (cfg.errstr == NULL)
1.1       jsing     913:                        sc_usage();
                    914:                goto end;
                    915:        }
                    916:
1.60      tb        917:        if (!app_passwd(bio_err, cfg.passarg, NULL, &pass, NULL)) {
1.1       jsing     918:                BIO_printf(bio_err, "Error getting password\n");
                    919:                goto end;
                    920:        }
1.60      tb        921:        if (cfg.key_file == NULL)
                    922:                cfg.key_file = cfg.cert_file;
1.1       jsing     923:
                    924:
1.60      tb        925:        if (cfg.key_file) {
1.1       jsing     926:
1.60      tb        927:                key = load_key(bio_err, cfg.key_file,
                    928:                    cfg.key_format, 0, pass,
1.1       jsing     929:                    "client certificate private key file");
                    930:                if (!key) {
                    931:                        ERR_print_errors(bio_err);
                    932:                        goto end;
                    933:                }
                    934:        }
1.60      tb        935:        if (cfg.cert_file) {
                    936:                cert = load_cert(bio_err, cfg.cert_file,
                    937:                    cfg.cert_format,
1.19      bcook     938:                    NULL, "client certificate file");
1.1       jsing     939:
                    940:                if (!cert) {
                    941:                        ERR_print_errors(bio_err);
                    942:                        goto end;
                    943:                }
                    944:        }
1.60      tb        945:        if (cfg.quiet && !cfg.debug &&
                    946:            !cfg.msg) {
1.50      inoguchi  947:                if ((bio_c_out = BIO_new(BIO_s_null())) == NULL)
                    948:                        goto end;
                    949:        } else {
                    950:                if ((bio_c_out = BIO_new_fp(stdout, BIO_NOCLOSE)) == NULL)
                    951:                        goto end;
1.1       jsing     952:        }
                    953:
1.60      tb        954:        ctx = SSL_CTX_new(cfg.meth);
1.1       jsing     955:        if (ctx == NULL) {
                    956:                ERR_print_errors(bio_err);
                    957:                goto end;
                    958:        }
1.46      tb        959:
                    960:        SSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY);
                    961:
1.60      tb        962:        if (cfg.vpm)
                    963:                SSL_CTX_set1_param(ctx, cfg.vpm);
1.42      jsing     964:
1.60      tb        965:        if (!SSL_CTX_set_min_proto_version(ctx, cfg.min_version))
1.42      jsing     966:                goto end;
1.60      tb        967:        if (!SSL_CTX_set_max_proto_version(ctx, cfg.max_version))
1.42      jsing     968:                goto end;
1.1       jsing     969:
                    970: #ifndef OPENSSL_NO_SRTP
1.60      tb        971:        if (cfg.srtp_profiles != NULL)
                    972:                SSL_CTX_set_tlsext_use_srtp(ctx, cfg.srtp_profiles);
1.1       jsing     973: #endif
1.60      tb        974:        if (cfg.bugs)
                    975:                SSL_CTX_set_options(ctx, SSL_OP_ALL | cfg.off);
1.1       jsing     976:        else
1.60      tb        977:                SSL_CTX_set_options(ctx, cfg.off);
1.1       jsing     978:
1.60      tb        979:        if (cfg.clr)
                    980:                SSL_CTX_clear_options(ctx, cfg.clr);
1.1       jsing     981:
1.60      tb        982:        if (cfg.alpn_in) {
1.10      jsing     983:                unsigned short alpn_len;
1.49      inoguchi  984:                unsigned char *alpn;
1.10      jsing     985:
1.60      tb        986:                alpn = next_protos_parse(&alpn_len, cfg.alpn_in);
1.10      jsing     987:                if (alpn == NULL) {
                    988:                        BIO_printf(bio_err, "Error parsing -alpn argument\n");
                    989:                        goto end;
                    990:                }
                    991:                SSL_CTX_set_alpn_protos(ctx, alpn, alpn_len);
                    992:                free(alpn);
1.31      jsing     993:        }
1.60      tb        994:        if (cfg.groups_in != NULL) {
                    995:                if (SSL_CTX_set1_groups_list(ctx, cfg.groups_in) != 1) {
1.31      jsing     996:                        BIO_printf(bio_err, "Failed to set groups '%s'\n",
1.60      tb        997:                            cfg.groups_in);
1.31      jsing     998:                        goto end;
                    999:                }
1.10      jsing    1000:        }
1.1       jsing    1001:
1.60      tb       1002:        if (cfg.state)
1.1       jsing    1003:                SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback);
1.60      tb       1004:        if (cfg.cipher != NULL)
                   1005:                if (!SSL_CTX_set_cipher_list(ctx, cfg.cipher)) {
1.1       jsing    1006:                        BIO_printf(bio_err, "error setting cipher list\n");
                   1007:                        ERR_print_errors(bio_err);
                   1008:                        goto end;
                   1009:                }
                   1010:
1.60      tb       1011:        SSL_CTX_set_verify(ctx, cfg.verify, verify_callback);
1.1       jsing    1012:        if (!set_cert_key_stuff(ctx, cert, key))
                   1013:                goto end;
                   1014:
1.60      tb       1015:        if ((cfg.CAfile || cfg.CApath)
                   1016:            && !SSL_CTX_load_verify_locations(ctx, cfg.CAfile,
                   1017:            cfg.CApath))
1.1       jsing    1018:                ERR_print_errors(bio_err);
1.28      bcook    1019:
                   1020:        if (!SSL_CTX_set_default_verify_paths(ctx))
                   1021:                ERR_print_errors(bio_err);
                   1022:
1.1       jsing    1023:        con = SSL_new(ctx);
1.60      tb       1024:        if (cfg.sess_in) {
1.1       jsing    1025:                SSL_SESSION *sess;
1.60      tb       1026:                BIO *stmp = BIO_new_file(cfg.sess_in, "r");
1.1       jsing    1027:                if (!stmp) {
                   1028:                        BIO_printf(bio_err, "Can't open session file %s\n",
1.60      tb       1029:                            cfg.sess_in);
1.1       jsing    1030:                        ERR_print_errors(bio_err);
                   1031:                        goto end;
                   1032:                }
                   1033:                sess = PEM_read_bio_SSL_SESSION(stmp, NULL, 0, NULL);
                   1034:                BIO_free(stmp);
                   1035:                if (!sess) {
                   1036:                        BIO_printf(bio_err, "Can't open session file %s\n",
1.60      tb       1037:                            cfg.sess_in);
1.1       jsing    1038:                        ERR_print_errors(bio_err);
                   1039:                        goto end;
                   1040:                }
                   1041:                SSL_set_session(con, sess);
                   1042:                SSL_SESSION_free(sess);
                   1043:        }
1.57      jsing    1044:
                   1045:        /* Attempt to opportunistically use the host name for SNI. */
1.60      tb       1046:        servername = cfg.servername;
1.57      jsing    1047:        if (servername == NULL)
1.60      tb       1048:                servername = cfg.host;
1.57      jsing    1049:
1.60      tb       1050:        if (!cfg.no_servername && servername != NULL &&
1.57      jsing    1051:            !SSL_set_tlsext_host_name(con, servername)) {
                   1052:                long ssl_err = ERR_peek_error();
                   1053:
1.60      tb       1054:                if (cfg.servername != NULL ||
1.57      jsing    1055:                    ERR_GET_LIB(ssl_err) != ERR_LIB_SSL ||
                   1056:                    ERR_GET_REASON(ssl_err) != SSL_R_SSL3_EXT_INVALID_SERVERNAME) {
1.49      inoguchi 1057:                        BIO_printf(bio_err,
                   1058:                            "Unable to set TLS servername extension.\n");
1.1       jsing    1059:                        ERR_print_errors(bio_err);
                   1060:                        goto end;
                   1061:                }
1.57      jsing    1062:                servername = NULL;
                   1063:                ERR_clear_error();
                   1064:        }
1.60      tb       1065:        if (!cfg.no_servername && servername != NULL) {
1.57      jsing    1066:                tlsextcbp.biodebug = bio_err;
                   1067:                SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
                   1068:                SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
1.1       jsing    1069:        }
                   1070:
1.49      inoguchi 1071:  re_start:
1.1       jsing    1072:
1.60      tb       1073:        if (init_client(&s, cfg.host, cfg.port,
                   1074:            cfg.socket_type, cfg.af) == 0) {
1.1       jsing    1075:                BIO_printf(bio_err, "connect:errno=%d\n", errno);
                   1076:                goto end;
                   1077:        }
                   1078:        BIO_printf(bio_c_out, "CONNECTED(%08X)\n", s);
                   1079:
1.60      tb       1080:        if (cfg.nbio) {
                   1081:                if (!cfg.quiet)
1.3       bcook    1082:                        BIO_printf(bio_c_out, "turning on non blocking io\n");
                   1083:                if (!BIO_socket_nbio(s, 1)) {
1.1       jsing    1084:                        ERR_print_errors(bio_err);
                   1085:                        goto end;
                   1086:                }
                   1087:        }
                   1088:
1.53      jsing    1089:        if (SSL_is_dtls(con)) {
1.1       jsing    1090:                sbio = BIO_new_dgram(s, BIO_NOCLOSE);
1.45      deraadt  1091:                if (getsockname(s, (struct sockaddr *)&peer,
                   1092:                    (void *)&peerlen) == -1) {
1.1       jsing    1093:                        BIO_printf(bio_err, "getsockname:errno=%d\n",
                   1094:                            errno);
                   1095:                        shutdown(s, SHUT_RD);
                   1096:                        close(s);
                   1097:                        goto end;
                   1098:                }
                   1099:                (void) BIO_ctrl_set_connected(sbio, 1, &peer);
                   1100:
1.60      tb       1101:                if (cfg.enable_timeouts) {
1.1       jsing    1102:                        timeout.tv_sec = 0;
                   1103:                        timeout.tv_usec = DGRAM_RCV_TIMEOUT;
1.49      inoguchi 1104:                        BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0,
                   1105:                            &timeout);
1.1       jsing    1106:
                   1107:                        timeout.tv_sec = 0;
                   1108:                        timeout.tv_usec = DGRAM_SND_TIMEOUT;
1.49      inoguchi 1109:                        BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0,
                   1110:                            &timeout);
1.1       jsing    1111:                }
1.60      tb       1112:                if (cfg.socket_mtu > 28) {
1.1       jsing    1113:                        SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
1.60      tb       1114:                        SSL_set_mtu(con, cfg.socket_mtu - 28);
1.1       jsing    1115:                } else
                   1116:                        /* want to do MTU discovery */
                   1117:                        BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
                   1118:        } else
                   1119:                sbio = BIO_new_socket(s, BIO_NOCLOSE);
                   1120:
1.60      tb       1121:        if (cfg.nbio_test) {
1.1       jsing    1122:                BIO *test;
                   1123:
                   1124:                test = BIO_new(BIO_f_nbio_test());
                   1125:                sbio = BIO_push(test, sbio);
                   1126:        }
1.60      tb       1127:        if (cfg.debug) {
1.1       jsing    1128:                SSL_set_debug(con, 1);
                   1129:                BIO_set_callback(sbio, bio_dump_callback);
                   1130:                BIO_set_callback_arg(sbio, (char *) bio_c_out);
                   1131:        }
1.60      tb       1132:        if (cfg.msg) {
1.1       jsing    1133:                SSL_set_msg_callback(con, msg_cb);
                   1134:                SSL_set_msg_callback_arg(con, bio_c_out);
                   1135:        }
1.60      tb       1136:        if (cfg.tlsextdebug) {
1.1       jsing    1137:                SSL_set_tlsext_debug_callback(con, tlsext_cb);
                   1138:                SSL_set_tlsext_debug_arg(con, bio_c_out);
                   1139:        }
1.60      tb       1140:        if (cfg.status_req) {
1.1       jsing    1141:                SSL_set_tlsext_status_type(con, TLSEXT_STATUSTYPE_ocsp);
                   1142:                SSL_CTX_set_tlsext_status_cb(ctx, ocsp_resp_cb);
                   1143:                SSL_CTX_set_tlsext_status_arg(ctx, bio_c_out);
                   1144:        }
                   1145:
                   1146:        SSL_set_bio(con, sbio, sbio);
                   1147:        SSL_set_connect_state(con);
                   1148:
                   1149:        /* ok, lets connect */
                   1150:        read_tty = 1;
                   1151:        write_tty = 0;
                   1152:        tty_on = 0;
                   1153:        read_ssl = 1;
                   1154:        write_ssl = 1;
                   1155:
                   1156:        cbuf_len = 0;
                   1157:        cbuf_off = 0;
                   1158:        sbuf_len = 0;
                   1159:        sbuf_off = 0;
                   1160:
                   1161:        /* This is an ugly hack that does a lot of assumptions */
                   1162:        /*
                   1163:         * We do have to handle multi-line responses which may come in a
                   1164:         * single packet or not. We therefore have to use BIO_gets() which
                   1165:         * does need a buffering BIO. So during the initial chitchat we do
                   1166:         * push a buffering BIO into the chain that is removed again later on
                   1167:         * to not disturb the rest of the s_client operation.
                   1168:         */
1.60      tb       1169:        if (cfg.starttls_proto == PROTO_SMTP ||
                   1170:            cfg.starttls_proto == PROTO_LMTP) {
1.1       jsing    1171:                int foundit = 0;
                   1172:                BIO *fbio = BIO_new(BIO_f_buffer());
                   1173:                BIO_push(fbio, sbio);
                   1174:                /* wait for multi-line response to end from SMTP */
                   1175:                do {
                   1176:                        mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
                   1177:                }
                   1178:                while (mbuf_len > 3 && mbuf[3] == '-');
                   1179:                /* STARTTLS command requires EHLO... */
                   1180:                BIO_printf(fbio, "%cHLO openssl.client.net\r\n",
1.60      tb       1181:                    cfg.starttls_proto == PROTO_SMTP ? 'E' : 'L');
1.1       jsing    1182:                (void) BIO_flush(fbio);
                   1183:                /* wait for multi-line response to end EHLO SMTP response */
                   1184:                do {
                   1185:                        mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
                   1186:                        if (strstr(mbuf, "STARTTLS"))
                   1187:                                foundit = 1;
                   1188:                }
                   1189:                while (mbuf_len > 3 && mbuf[3] == '-');
                   1190:                (void) BIO_flush(fbio);
                   1191:                BIO_pop(fbio);
                   1192:                BIO_free(fbio);
                   1193:                if (!foundit)
                   1194:                        BIO_printf(bio_err,
1.36      jmc      1195:                            "didn't find starttls in server response,"
1.1       jsing    1196:                            " try anyway...\n");
                   1197:                BIO_printf(sbio, "STARTTLS\r\n");
                   1198:                BIO_read(sbio, sbuf, BUFSIZZ);
1.60      tb       1199:        } else if (cfg.starttls_proto == PROTO_POP3) {
1.1       jsing    1200:                mbuf_len = BIO_read(sbio, mbuf, BUFSIZZ);
                   1201:                if (mbuf_len == -1) {
                   1202:                        BIO_printf(bio_err, "BIO_read failed\n");
                   1203:                        goto end;
                   1204:                }
                   1205:                BIO_printf(sbio, "STLS\r\n");
                   1206:                BIO_read(sbio, sbuf, BUFSIZZ);
1.60      tb       1207:        } else if (cfg.starttls_proto == PROTO_IMAP) {
1.1       jsing    1208:                int foundit = 0;
                   1209:                BIO *fbio = BIO_new(BIO_f_buffer());
                   1210:                BIO_push(fbio, sbio);
                   1211:                BIO_gets(fbio, mbuf, BUFSIZZ);
                   1212:                /* STARTTLS command requires CAPABILITY... */
                   1213:                BIO_printf(fbio, ". CAPABILITY\r\n");
                   1214:                (void) BIO_flush(fbio);
                   1215:                /* wait for multi-line CAPABILITY response */
                   1216:                do {
                   1217:                        mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
                   1218:                        if (strstr(mbuf, "STARTTLS"))
                   1219:                                foundit = 1;
                   1220:                }
                   1221:                while (mbuf_len > 3 && mbuf[0] != '.');
                   1222:                (void) BIO_flush(fbio);
                   1223:                BIO_pop(fbio);
                   1224:                BIO_free(fbio);
                   1225:                if (!foundit)
                   1226:                        BIO_printf(bio_err,
1.37      tb       1227:                            "didn't find STARTTLS in server response,"
1.1       jsing    1228:                            " try anyway...\n");
                   1229:                BIO_printf(sbio, ". STARTTLS\r\n");
                   1230:                BIO_read(sbio, sbuf, BUFSIZZ);
1.60      tb       1231:        } else if (cfg.starttls_proto == PROTO_FTP) {
1.1       jsing    1232:                BIO *fbio = BIO_new(BIO_f_buffer());
                   1233:                BIO_push(fbio, sbio);
                   1234:                /* wait for multi-line response to end from FTP */
                   1235:                do {
                   1236:                        mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
                   1237:                }
                   1238:                while (mbuf_len > 3 && mbuf[3] == '-');
                   1239:                (void) BIO_flush(fbio);
                   1240:                BIO_pop(fbio);
                   1241:                BIO_free(fbio);
                   1242:                BIO_printf(sbio, "AUTH TLS\r\n");
                   1243:                BIO_read(sbio, sbuf, BUFSIZZ);
1.60      tb       1244:        } else if (cfg.starttls_proto == PROTO_XMPP) {
1.1       jsing    1245:                int seen = 0;
                   1246:                BIO_printf(sbio, "<stream:stream "
                   1247:                    "xmlns:stream='http://etherx.jabber.org/streams' "
1.49      inoguchi 1248:                    "xmlns='jabber:client' to='%s' version='1.0'>",
1.60      tb       1249:                    cfg.xmpphost ?
                   1250:                    cfg.xmpphost : cfg.host);
1.1       jsing    1251:                seen = BIO_read(sbio, mbuf, BUFSIZZ);
1.15      landry   1252:
                   1253:                if (seen <= 0)
                   1254:                        goto shut;
                   1255:
1.1       jsing    1256:                mbuf[seen] = 0;
1.15      landry   1257:                while (!strstr(mbuf, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'") &&
                   1258:                       !strstr(mbuf, "<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"")) {
                   1259:                        seen = BIO_read(sbio, mbuf, BUFSIZZ);
                   1260:
                   1261:                        if (seen <= 0)
1.1       jsing    1262:                                goto shut;
1.15      landry   1263:
1.1       jsing    1264:                        mbuf[seen] = 0;
                   1265:                }
1.49      inoguchi 1266:                BIO_printf(sbio,
                   1267:                    "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
1.1       jsing    1268:                seen = BIO_read(sbio, sbuf, BUFSIZZ);
                   1269:                sbuf[seen] = 0;
                   1270:                if (!strstr(sbuf, "<proceed"))
                   1271:                        goto shut;
                   1272:                mbuf[0] = 0;
1.60      tb       1273:        } else if (cfg.proxy != NULL) {
1.49      inoguchi 1274:                BIO_printf(sbio, "CONNECT %s HTTP/1.0\r\n\r\n",
1.60      tb       1275:                    cfg.connect);
1.12      bluhm    1276:                mbuf_len = BIO_read(sbio, mbuf, BUFSIZZ);
                   1277:                if (mbuf_len == -1) {
                   1278:                        BIO_printf(bio_err, "BIO_read failed\n");
                   1279:                        goto end;
                   1280:                }
1.1       jsing    1281:        }
                   1282:        for (;;) {
1.9       deraadt  1283:                struct pollfd pfd[3];   /* stdin, stdout, socket */
                   1284:                int ptimeout = -1;
1.1       jsing    1285:
1.53      jsing    1286:                if (SSL_is_dtls(con) && DTLSv1_get_timeout(con, &timeout))
1.49      inoguchi 1287:                        ptimeout = timeout.tv_sec * 1000 +
                   1288:                            timeout.tv_usec / 1000;
1.1       jsing    1289:
                   1290:                if (SSL_in_init(con) && !SSL_total_renegotiations(con)) {
                   1291:                        in_init = 1;
                   1292:                        tty_on = 0;
                   1293:                } else {
                   1294:                        tty_on = 1;
                   1295:                        if (in_init) {
                   1296:                                in_init = 0;
1.60      tb       1297:                                if (cfg.sess_out) {
1.49      inoguchi 1298:                                        BIO *stmp = BIO_new_file(
1.60      tb       1299:                                            cfg.sess_out, "w");
1.1       jsing    1300:                                        if (stmp) {
1.49      inoguchi 1301:                                                PEM_write_bio_SSL_SESSION(stmp,
                   1302:                                                    SSL_get_session(con));
1.1       jsing    1303:                                                BIO_free(stmp);
                   1304:                                        } else
1.49      inoguchi 1305:                                                BIO_printf(bio_err,
                   1306:                                                    "Error writing session file %s\n",
1.60      tb       1307:                                                    cfg.sess_out);
1.1       jsing    1308:                                }
                   1309:                                print_stuff(bio_c_out, con, full_log);
                   1310:                                if (full_log > 0)
                   1311:                                        full_log--;
                   1312:
1.60      tb       1313:                                if (cfg.starttls_proto) {
1.1       jsing    1314:                                        BIO_write(bio_err, mbuf, mbuf_len);
                   1315:                                        /* We don't need to know any more */
1.60      tb       1316:                                        cfg.starttls_proto = PROTO_OFF;
1.1       jsing    1317:                                }
1.60      tb       1318:                                if (cfg.reconnect) {
                   1319:                                        cfg.reconnect--;
1.49      inoguchi 1320:                                        BIO_printf(bio_c_out,
                   1321:                                            "drop connection and then reconnect\n");
1.1       jsing    1322:                                        SSL_shutdown(con);
                   1323:                                        SSL_set_connect_state(con);
                   1324:                                        shutdown(SSL_get_fd(con), SHUT_RD);
                   1325:                                        close(SSL_get_fd(con));
                   1326:                                        goto re_start;
                   1327:                                }
                   1328:                        }
                   1329:                }
                   1330:
                   1331:                ssl_pending = read_ssl && SSL_pending(con);
                   1332:
1.9       deraadt  1333:                pfd[0].fd = -1;
                   1334:                pfd[1].fd = -1;
1.1       jsing    1335:                if (!ssl_pending) {
                   1336:                        if (tty_on) {
1.9       deraadt  1337:                                if (read_tty) {
                   1338:                                        pfd[0].fd = fileno(stdin);
                   1339:                                        pfd[0].events = POLLIN;
                   1340:                                }
                   1341:                                if (write_tty) {
                   1342:                                        pfd[1].fd = fileno(stdout);
                   1343:                                        pfd[1].events = POLLOUT;
                   1344:                                }
1.1       jsing    1345:                        }
1.9       deraadt  1346:
                   1347:                        pfd[2].fd = SSL_get_fd(con);
                   1348:                        pfd[2].events = 0;
1.1       jsing    1349:                        if (read_ssl)
1.9       deraadt  1350:                                pfd[2].events |= POLLIN;
1.1       jsing    1351:                        if (write_ssl)
1.9       deraadt  1352:                                pfd[2].events |= POLLOUT;
                   1353:
1.1       jsing    1354: /*                     printf("mode tty(%d %d%d) ssl(%d%d)\n",
                   1355:                                tty_on,read_tty,write_tty,read_ssl,write_ssl);*/
                   1356:
1.9       deraadt  1357:                        i = poll(pfd, 3, ptimeout);
1.38      deraadt  1358:                        if (i == -1) {
1.1       jsing    1359:                                BIO_printf(bio_err, "bad select %d\n",
                   1360:                                    errno);
                   1361:                                goto shut;
                   1362:                                /* goto end; */
                   1363:                        }
                   1364:                }
1.53      jsing    1365:                if (SSL_is_dtls(con) &&
                   1366:                    DTLSv1_handle_timeout(con) > 0)
1.1       jsing    1367:                        BIO_printf(bio_err, "TIMEOUT occured\n");
1.49      inoguchi 1368:                if (!ssl_pending &&
                   1369:                    (pfd[2].revents & (POLLOUT|POLLERR|POLLNVAL))) {
1.9       deraadt  1370:                        if (pfd[2].revents & (POLLERR|POLLNVAL)) {
                   1371:                                BIO_printf(bio_err, "poll error");
                   1372:                                goto shut;
                   1373:                        }
1.1       jsing    1374:                        k = SSL_write(con, &(cbuf[cbuf_off]),
                   1375:                            (unsigned int) cbuf_len);
                   1376:                        switch (SSL_get_error(con, k)) {
                   1377:                        case SSL_ERROR_NONE:
                   1378:                                cbuf_off += k;
                   1379:                                cbuf_len -= k;
                   1380:                                if (k <= 0)
                   1381:                                        goto end;
                   1382:                                /* we have done a  write(con,NULL,0); */
                   1383:                                if (cbuf_len <= 0) {
                   1384:                                        read_tty = 1;
                   1385:                                        write_ssl = 0;
                   1386:                                } else {        /* if (cbuf_len > 0) */
                   1387:                                        read_tty = 0;
                   1388:                                        write_ssl = 1;
                   1389:                                }
                   1390:                                break;
                   1391:                        case SSL_ERROR_WANT_WRITE:
                   1392:                                BIO_printf(bio_c_out, "write W BLOCK\n");
                   1393:                                write_ssl = 1;
                   1394:                                read_tty = 0;
                   1395:                                break;
                   1396:                        case SSL_ERROR_WANT_READ:
                   1397:                                BIO_printf(bio_c_out, "write R BLOCK\n");
                   1398:                                write_tty = 0;
                   1399:                                read_ssl = 1;
                   1400:                                write_ssl = 0;
                   1401:                                break;
                   1402:                        case SSL_ERROR_WANT_X509_LOOKUP:
                   1403:                                BIO_printf(bio_c_out, "write X BLOCK\n");
                   1404:                                break;
                   1405:                        case SSL_ERROR_ZERO_RETURN:
                   1406:                                if (cbuf_len != 0) {
                   1407:                                        BIO_printf(bio_c_out, "shutdown\n");
                   1408:                                        ret = 0;
                   1409:                                        goto shut;
                   1410:                                } else {
                   1411:                                        read_tty = 1;
                   1412:                                        write_ssl = 0;
                   1413:                                        break;
                   1414:                                }
                   1415:
                   1416:                        case SSL_ERROR_SYSCALL:
                   1417:                                if ((k != 0) || (cbuf_len != 0)) {
                   1418:                                        BIO_printf(bio_err, "write:errno=%d\n",
                   1419:                                            errno);
                   1420:                                        goto shut;
                   1421:                                } else {
                   1422:                                        read_tty = 1;
                   1423:                                        write_ssl = 0;
                   1424:                                }
                   1425:                                break;
                   1426:                        case SSL_ERROR_SSL:
                   1427:                                ERR_print_errors(bio_err);
                   1428:                                goto shut;
                   1429:                        }
1.9       deraadt  1430:                } else if (!ssl_pending &&
                   1431:                    (pfd[1].revents & (POLLOUT|POLLERR|POLLNVAL))) {
                   1432:                        if (pfd[1].revents & (POLLERR|POLLNVAL)) {
                   1433:                                BIO_printf(bio_err, "poll error");
                   1434:                                goto shut;
                   1435:                        }
1.1       jsing    1436:                        i = write(fileno(stdout), &(sbuf[sbuf_off]), sbuf_len);
                   1437:
                   1438:                        if (i <= 0) {
                   1439:                                BIO_printf(bio_c_out, "DONE\n");
                   1440:                                ret = 0;
                   1441:                                goto shut;
                   1442:                                /* goto end; */
                   1443:                        }
                   1444:                        sbuf_len -= i;
                   1445:                        sbuf_off += i;
                   1446:                        if (sbuf_len <= 0) {
                   1447:                                read_ssl = 1;
                   1448:                                write_tty = 0;
                   1449:                        }
1.9       deraadt  1450:                } else if (ssl_pending || (pfd[2].revents & (POLLIN|POLLHUP))) {
1.1       jsing    1451: #ifdef RENEG
                   1452:                        {
                   1453:                                static int iiii;
                   1454:                                if (++iiii == 52) {
                   1455:                                        SSL_renegotiate(con);
                   1456:                                        iiii = 0;
                   1457:                                }
                   1458:                        }
                   1459: #endif
1.60      tb       1460:                        if (cfg.peekaboo) {
1.40      jsing    1461:                                k = p = SSL_peek(con, pbuf, 1024 /* BUFSIZZ */ );
1.41      beck     1462:                                pending = SSL_pending(con);
1.40      jsing    1463:                                if (SSL_get_error(con, p) == SSL_ERROR_NONE) {
1.39      beck     1464:                                        if (p <= 0)
                   1465:                                                goto end;
                   1466:
1.40      jsing    1467:                                        k = SSL_read(con, sbuf, p);
1.39      beck     1468:                                }
1.40      jsing    1469:                        } else {
                   1470:                                k = SSL_read(con, sbuf, 1024 /* BUFSIZZ */ );
1.39      beck     1471:                        }
1.1       jsing    1472:
                   1473:                        switch (SSL_get_error(con, k)) {
                   1474:                        case SSL_ERROR_NONE:
                   1475:                                if (k <= 0)
                   1476:                                        goto end;
                   1477:                                sbuf_off = 0;
                   1478:                                sbuf_len = k;
1.60      tb       1479:                                if (cfg.peekaboo) {
1.41      beck     1480:                                        if (p != pending) {
                   1481:                                                ret = -1;
                   1482:                                                BIO_printf(bio_err,
1.49      inoguchi 1483:                                                    "peeked %d but pending %d!\n",
                   1484:                                                    p, pending);
1.41      beck     1485:                                                goto shut;
                   1486:                                        }
1.39      beck     1487:                                        if (k < p) {
                   1488:                                                ret = -1;
                   1489:                                                BIO_printf(bio_err,
                   1490:                                                    "read less than peek!\n");
                   1491:                                                goto shut;
                   1492:                                        }
1.49      inoguchi 1493:                                        if (p > 0 &&
                   1494:                                            (memcmp(sbuf, pbuf, p) != 0)) {
1.39      beck     1495:                                                ret = -1;
                   1496:                                                BIO_printf(bio_err,
                   1497:                                                    "peek of %d different from read of %d!\n",
                   1498:                                                    p, k);
                   1499:                                                goto shut;
                   1500:                                        }
                   1501:                                }
1.1       jsing    1502:                                read_ssl = 0;
                   1503:                                write_tty = 1;
                   1504:                                break;
                   1505:                        case SSL_ERROR_WANT_WRITE:
                   1506:                                BIO_printf(bio_c_out, "read W BLOCK\n");
                   1507:                                write_ssl = 1;
                   1508:                                read_tty = 0;
                   1509:                                break;
                   1510:                        case SSL_ERROR_WANT_READ:
                   1511:                                BIO_printf(bio_c_out, "read R BLOCK\n");
                   1512:                                write_tty = 0;
                   1513:                                read_ssl = 1;
                   1514:                                if ((read_tty == 0) && (write_ssl == 0))
                   1515:                                        write_ssl = 1;
                   1516:                                break;
                   1517:                        case SSL_ERROR_WANT_X509_LOOKUP:
                   1518:                                BIO_printf(bio_c_out, "read X BLOCK\n");
                   1519:                                break;
                   1520:                        case SSL_ERROR_SYSCALL:
                   1521:                                ret = errno;
                   1522:                                BIO_printf(bio_err, "read:errno=%d\n", ret);
                   1523:                                goto shut;
                   1524:                        case SSL_ERROR_ZERO_RETURN:
                   1525:                                BIO_printf(bio_c_out, "closed\n");
                   1526:                                ret = 0;
                   1527:                                goto shut;
                   1528:                        case SSL_ERROR_SSL:
                   1529:                                ERR_print_errors(bio_err);
                   1530:                                goto shut;
                   1531:                                /* break; */
                   1532:                        }
1.9       deraadt  1533:                } else if (pfd[0].revents) {
                   1534:                        if (pfd[0].revents & (POLLERR|POLLNVAL)) {
                   1535:                                BIO_printf(bio_err, "poll error");
                   1536:                                goto shut;
                   1537:                        }
1.60      tb       1538:                        if (cfg.crlf) {
1.1       jsing    1539:                                int j, lf_num;
                   1540:
                   1541:                                i = read(fileno(stdin), cbuf, BUFSIZZ / 2);
                   1542:                                lf_num = 0;
                   1543:                                /* both loops are skipped when i <= 0 */
                   1544:                                for (j = 0; j < i; j++)
                   1545:                                        if (cbuf[j] == '\n')
                   1546:                                                lf_num++;
                   1547:                                for (j = i - 1; j >= 0; j--) {
                   1548:                                        cbuf[j + lf_num] = cbuf[j];
                   1549:                                        if (cbuf[j] == '\n') {
                   1550:                                                lf_num--;
                   1551:                                                i++;
                   1552:                                                cbuf[j + lf_num] = '\r';
                   1553:                                        }
                   1554:                                }
                   1555:                                assert(lf_num == 0);
                   1556:                        } else
                   1557:                                i = read(fileno(stdin), cbuf, BUFSIZZ);
                   1558:
1.60      tb       1559:                        if ((!cfg.ign_eof) &&
1.49      inoguchi 1560:                            ((i <= 0) || (cbuf[0] == 'Q'))) {
1.1       jsing    1561:                                BIO_printf(bio_err, "DONE\n");
                   1562:                                ret = 0;
                   1563:                                goto shut;
                   1564:                        }
1.60      tb       1565:                        if ((!cfg.ign_eof) && (cbuf[0] == 'R')) {
1.1       jsing    1566:                                BIO_printf(bio_err, "RENEGOTIATING\n");
                   1567:                                SSL_renegotiate(con);
                   1568:                                cbuf_len = 0;
                   1569:                        } else {
                   1570:                                cbuf_len = i;
                   1571:                                cbuf_off = 0;
                   1572:                        }
                   1573:
                   1574:                        write_ssl = 1;
                   1575:                        read_tty = 0;
                   1576:                }
                   1577:        }
                   1578:
                   1579:        ret = 0;
1.35      jsing    1580:  shut:
1.1       jsing    1581:        if (in_init)
                   1582:                print_stuff(bio_c_out, con, full_log);
                   1583:        SSL_shutdown(con);
                   1584:        shutdown(SSL_get_fd(con), SHUT_RD);
                   1585:        close(SSL_get_fd(con));
1.35      jsing    1586:  end:
1.1       jsing    1587:        if (con != NULL) {
1.60      tb       1588:                if (cfg.prexit != 0)
1.1       jsing    1589:                        print_stuff(bio_c_out, con, 1);
                   1590:                SSL_free(con);
                   1591:        }
1.34      jsing    1592:        SSL_CTX_free(ctx);
                   1593:        X509_free(cert);
                   1594:        EVP_PKEY_free(key);
1.1       jsing    1595:        free(pass);
1.60      tb       1596:        X509_VERIFY_PARAM_free(cfg.vpm);
1.32      deraadt  1597:        freezero(cbuf, BUFSIZZ);
                   1598:        freezero(sbuf, BUFSIZZ);
1.52      tb       1599:        freezero(pbuf, BUFSIZZ);
1.32      deraadt  1600:        freezero(mbuf, BUFSIZZ);
1.50      inoguchi 1601:        BIO_free(bio_c_out);
1.1       jsing    1602:
                   1603:        return (ret);
                   1604: }
                   1605:
                   1606: static void
1.51      inoguchi 1607: print_stuff(BIO *bio, SSL *s, int full)
1.1       jsing    1608: {
                   1609:        X509 *peer = NULL;
                   1610:        char *p;
                   1611:        static const char *space = "                ";
                   1612:        char buf[BUFSIZ];
1.51      inoguchi 1613:        STACK_OF(X509) *sk;
                   1614:        STACK_OF(X509_NAME) *sk2;
1.1       jsing    1615:        const SSL_CIPHER *c;
                   1616:        X509_NAME *xn;
                   1617:        int j, i;
                   1618:        unsigned char *exportedkeymat;
                   1619:
                   1620:        if (full) {
                   1621:                int got_a_chain = 0;
                   1622:
                   1623:                sk = SSL_get_peer_cert_chain(s);
                   1624:                if (sk != NULL) {
                   1625:                        got_a_chain = 1;        /* we don't have it for SSL2
                   1626:                                                 * (yet) */
                   1627:
                   1628:                        BIO_printf(bio, "---\nCertificate chain\n");
                   1629:                        for (i = 0; i < sk_X509_num(sk); i++) {
                   1630:                                X509_NAME_oneline(X509_get_subject_name(
                   1631:                                        sk_X509_value(sk, i)), buf, sizeof buf);
                   1632:                                BIO_printf(bio, "%2d s:%s\n", i, buf);
                   1633:                                X509_NAME_oneline(X509_get_issuer_name(
                   1634:                                        sk_X509_value(sk, i)), buf, sizeof buf);
                   1635:                                BIO_printf(bio, "   i:%s\n", buf);
1.60      tb       1636:                                if (cfg.showcerts)
1.49      inoguchi 1637:                                        PEM_write_bio_X509(bio,
                   1638:                                            sk_X509_value(sk, i));
1.1       jsing    1639:                        }
                   1640:                }
                   1641:                BIO_printf(bio, "---\n");
                   1642:                peer = SSL_get_peer_certificate(s);
                   1643:                if (peer != NULL) {
                   1644:                        BIO_printf(bio, "Server certificate\n");
1.60      tb       1645:                        if (!(cfg.showcerts && got_a_chain)) {
1.49      inoguchi 1646:                                /* Redundant if we showed the whole chain */
1.1       jsing    1647:                                PEM_write_bio_X509(bio, peer);
1.49      inoguchi 1648:                        }
1.1       jsing    1649:                        X509_NAME_oneline(X509_get_subject_name(peer),
                   1650:                            buf, sizeof buf);
                   1651:                        BIO_printf(bio, "subject=%s\n", buf);
                   1652:                        X509_NAME_oneline(X509_get_issuer_name(peer),
                   1653:                            buf, sizeof buf);
                   1654:                        BIO_printf(bio, "issuer=%s\n", buf);
                   1655:                } else
                   1656:                        BIO_printf(bio, "no peer certificate available\n");
                   1657:
                   1658:                sk2 = SSL_get_client_CA_list(s);
                   1659:                if ((sk2 != NULL) && (sk_X509_NAME_num(sk2) > 0)) {
1.49      inoguchi 1660:                        BIO_printf(bio,
                   1661:                            "---\nAcceptable client certificate CA names\n");
1.1       jsing    1662:                        for (i = 0; i < sk_X509_NAME_num(sk2); i++) {
                   1663:                                xn = sk_X509_NAME_value(sk2, i);
                   1664:                                X509_NAME_oneline(xn, buf, sizeof(buf));
                   1665:                                BIO_write(bio, buf, strlen(buf));
                   1666:                                BIO_write(bio, "\n", 1);
                   1667:                        }
                   1668:                } else {
1.49      inoguchi 1669:                        BIO_printf(bio,
                   1670:                            "---\nNo client certificate CA names sent\n");
1.1       jsing    1671:                }
                   1672:                p = SSL_get_shared_ciphers(s, buf, sizeof buf);
                   1673:                if (p != NULL) {
                   1674:                        /*
                   1675:                         * This works only for SSL 2.  In later protocol
                   1676:                         * versions, the client does not know what other
                   1677:                         * ciphers (in addition to the one to be used in the
                   1678:                         * current connection) the server supports.
                   1679:                         */
                   1680:
1.49      inoguchi 1681:                        BIO_printf(bio,
                   1682:                            "---\nCiphers common between both SSL endpoints:\n");
1.1       jsing    1683:                        j = i = 0;
                   1684:                        while (*p) {
                   1685:                                if (*p == ':') {
                   1686:                                        BIO_write(bio, space, 15 - j % 25);
                   1687:                                        i++;
                   1688:                                        j = 0;
1.49      inoguchi 1689:                                        BIO_write(bio,
                   1690:                                            ((i % 3) ? " " : "\n"), 1);
1.1       jsing    1691:                                } else {
                   1692:                                        BIO_write(bio, p, 1);
                   1693:                                        j++;
                   1694:                                }
                   1695:                                p++;
                   1696:                        }
                   1697:                        BIO_write(bio, "\n", 1);
                   1698:                }
1.29      jsing    1699:
                   1700:                ssl_print_tmp_key(bio, s);
                   1701:
1.49      inoguchi 1702:                BIO_printf(bio,
                   1703:                    "---\nSSL handshake has read %ld bytes and written %ld bytes\n",
1.1       jsing    1704:                    BIO_number_read(SSL_get_rbio(s)),
                   1705:                    BIO_number_written(SSL_get_wbio(s)));
                   1706:        }
                   1707:        BIO_printf(bio, (SSL_cache_hit(s) ? "---\nReused, " : "---\nNew, "));
                   1708:        c = SSL_get_current_cipher(s);
                   1709:        BIO_printf(bio, "%s, Cipher is %s\n",
                   1710:            SSL_CIPHER_get_version(c),
                   1711:            SSL_CIPHER_get_name(c));
                   1712:        if (peer != NULL) {
                   1713:                EVP_PKEY *pktmp;
1.58      tb       1714:
                   1715:                pktmp = X509_get0_pubkey(peer);
1.1       jsing    1716:                BIO_printf(bio, "Server public key is %d bit\n",
                   1717:                    EVP_PKEY_bits(pktmp));
                   1718:        }
                   1719:        BIO_printf(bio, "Secure Renegotiation IS%s supported\n",
                   1720:            SSL_get_secure_renegotiation_support(s) ? "" : " NOT");
                   1721:
                   1722:        /* Compression is not supported and will always be none. */
                   1723:        BIO_printf(bio, "Compression: NONE\n");
                   1724:        BIO_printf(bio, "Expansion: NONE\n");
                   1725:
                   1726: #ifdef SSL_DEBUG
                   1727:        {
                   1728:                /* Print out local port of connection: useful for debugging */
                   1729:                int sock;
                   1730:                struct sockaddr_in ladd;
                   1731:                socklen_t ladd_size = sizeof(ladd);
                   1732:                sock = SSL_get_fd(s);
                   1733:                getsockname(sock, (struct sockaddr *) & ladd, &ladd_size);
1.50      inoguchi 1734:                BIO_printf(bio, "LOCAL PORT is %u\n",
1.49      inoguchi 1735:                    ntohs(ladd.sin_port));
1.1       jsing    1736:        }
                   1737: #endif
                   1738:
1.10      jsing    1739:        {
                   1740:                const unsigned char *proto;
                   1741:                unsigned int proto_len;
                   1742:                SSL_get0_alpn_selected(s, &proto, &proto_len);
                   1743:                if (proto_len > 0) {
                   1744:                        BIO_printf(bio, "ALPN protocol: ");
                   1745:                        BIO_write(bio, proto, proto_len);
                   1746:                        BIO_write(bio, "\n", 1);
                   1747:                } else
                   1748:                        BIO_printf(bio, "No ALPN negotiated\n");
                   1749:        }
1.1       jsing    1750:
                   1751: #ifndef OPENSSL_NO_SRTP
                   1752:        {
1.49      inoguchi 1753:                SRTP_PROTECTION_PROFILE *srtp_profile;
1.1       jsing    1754:
1.49      inoguchi 1755:                srtp_profile = SSL_get_selected_srtp_profile(s);
1.1       jsing    1756:                if (srtp_profile)
1.49      inoguchi 1757:                        BIO_printf(bio,
                   1758:                            "SRTP Extension negotiated, profile=%s\n",
1.1       jsing    1759:                            srtp_profile->name);
                   1760:        }
                   1761: #endif
                   1762:
                   1763:        SSL_SESSION_print(bio, SSL_get_session(s));
1.60      tb       1764:        if (cfg.keymatexportlabel != NULL) {
1.1       jsing    1765:                BIO_printf(bio, "Keying material exporter:\n");
1.49      inoguchi 1766:                BIO_printf(bio, "    Label: '%s'\n",
1.60      tb       1767:                    cfg.keymatexportlabel);
1.49      inoguchi 1768:                BIO_printf(bio, "    Length: %i bytes\n",
1.60      tb       1769:                    cfg.keymatexportlen);
                   1770:                exportedkeymat = malloc(cfg.keymatexportlen);
1.1       jsing    1771:                if (exportedkeymat != NULL) {
                   1772:                        if (!SSL_export_keying_material(s, exportedkeymat,
1.60      tb       1773:                                cfg.keymatexportlen,
                   1774:                                cfg.keymatexportlabel,
                   1775:                                strlen(cfg.keymatexportlabel),
1.1       jsing    1776:                                NULL, 0, 0)) {
                   1777:                                BIO_printf(bio, "    Error\n");
                   1778:                        } else {
                   1779:                                BIO_printf(bio, "    Keying material: ");
1.60      tb       1780:                                for (i = 0; i < cfg.keymatexportlen; i++)
1.1       jsing    1781:                                        BIO_printf(bio, "%02X",
                   1782:                                            exportedkeymat[i]);
                   1783:                                BIO_printf(bio, "\n");
                   1784:                        }
                   1785:                        free(exportedkeymat);
                   1786:                }
                   1787:        }
                   1788:        BIO_printf(bio, "---\n");
1.34      jsing    1789:        X509_free(peer);
1.1       jsing    1790:        /* flush, or debugging output gets mixed with http response */
                   1791:        (void) BIO_flush(bio);
                   1792: }
                   1793:
                   1794: static int
1.51      inoguchi 1795: ocsp_resp_cb(SSL *s, void *arg)
1.1       jsing    1796: {
                   1797:        const unsigned char *p;
                   1798:        int len;
                   1799:        OCSP_RESPONSE *rsp;
                   1800:        len = SSL_get_tlsext_status_ocsp_resp(s, &p);
                   1801:        BIO_puts(arg, "OCSP response: ");
                   1802:        if (!p) {
                   1803:                BIO_puts(arg, "no response sent\n");
                   1804:                return 1;
                   1805:        }
                   1806:        rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
                   1807:        if (!rsp) {
                   1808:                BIO_puts(arg, "response parse error\n");
                   1809:                BIO_dump_indent(arg, (char *) p, len, 4);
                   1810:                return 0;
                   1811:        }
                   1812:        BIO_puts(arg, "\n======================================\n");
                   1813:        OCSP_RESPONSE_print(arg, rsp, 0);
                   1814:        BIO_puts(arg, "======================================\n");
                   1815:        OCSP_RESPONSE_free(rsp);
                   1816:        return 1;
1.51      inoguchi 1817: }
                   1818:
                   1819: static int
                   1820: ssl_servername_cb(SSL *s, int *ad, void *arg)
                   1821: {
                   1822:        tlsextctx *p = (tlsextctx *) arg;
                   1823:        const char *hn = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
                   1824:        if (SSL_get_servername_type(s) != -1)
                   1825:                p->ack = !SSL_session_reused(s) && hn != NULL;
                   1826:        else
                   1827:                BIO_printf(bio_err, "Can't use SSL_get_servername\n");
                   1828:
                   1829:        return SSL_TLSEXT_ERR_OK;
1.1       jsing    1830: }
                   1831: