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

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