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

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