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

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