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

Annotation of src/usr.bin/openssl/s_server.c, Revision 1.11

1.11    ! doug        1: /* $OpenBSD: s_server.c,v 1.10 2015/04/14 12:56:36 jsing Exp $ */
1.1       jsing       2: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
                      3:  * All rights reserved.
                      4:  *
                      5:  * This package is an SSL implementation written
                      6:  * by Eric Young (eay@cryptsoft.com).
                      7:  * The implementation was written so as to conform with Netscapes SSL.
                      8:  *
                      9:  * This library is free for commercial and non-commercial use as long as
                     10:  * the following conditions are aheared to.  The following conditions
                     11:  * apply to all code found in this distribution, be it the RC4, RSA,
                     12:  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
                     13:  * included with this distribution is covered by the same copyright terms
                     14:  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
                     15:  *
                     16:  * Copyright remains Eric Young's, and as such any Copyright notices in
                     17:  * the code are not to be removed.
                     18:  * If this package is used in a product, Eric Young should be given attribution
                     19:  * as the author of the parts of the library used.
                     20:  * This can be in the form of a textual message at program startup or
                     21:  * in documentation (online or textual) provided with the package.
                     22:  *
                     23:  * Redistribution and use in source and binary forms, with or without
                     24:  * modification, are permitted provided that the following conditions
                     25:  * are met:
                     26:  * 1. Redistributions of source code must retain the copyright
                     27:  *    notice, this list of conditions and the following disclaimer.
                     28:  * 2. Redistributions in binary form must reproduce the above copyright
                     29:  *    notice, this list of conditions and the following disclaimer in the
                     30:  *    documentation and/or other materials provided with the distribution.
                     31:  * 3. All advertising materials mentioning features or use of this software
                     32:  *    must display the following acknowledgement:
                     33:  *    "This product includes cryptographic software written by
                     34:  *     Eric Young (eay@cryptsoft.com)"
                     35:  *    The word 'cryptographic' can be left out if the rouines from the library
                     36:  *    being used are not cryptographic related :-).
                     37:  * 4. If you include any Windows specific code (or a derivative thereof) from
                     38:  *    the apps directory (application code) you must include an acknowledgement:
                     39:  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
                     40:  *
                     41:  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
                     42:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     43:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     44:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     45:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     46:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     47:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     48:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     49:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     50:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     51:  * SUCH DAMAGE.
                     52:  *
                     53:  * The licence and distribution terms for any publically available version or
                     54:  * derivative of this code cannot be changed.  i.e. this code cannot simply be
                     55:  * copied and put under another distribution licence
                     56:  * [including the GNU Public Licence.]
                     57:  */
                     58: /* ====================================================================
                     59:  * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
                     60:  *
                     61:  * Redistribution and use in source and binary forms, with or without
                     62:  * modification, are permitted provided that the following conditions
                     63:  * are met:
                     64:  *
                     65:  * 1. Redistributions of source code must retain the above copyright
                     66:  *    notice, this list of conditions and the following disclaimer.
                     67:  *
                     68:  * 2. Redistributions in binary form must reproduce the above copyright
                     69:  *    notice, this list of conditions and the following disclaimer in
                     70:  *    the documentation and/or other materials provided with the
                     71:  *    distribution.
                     72:  *
                     73:  * 3. All advertising materials mentioning features or use of this
                     74:  *    software must display the following acknowledgment:
                     75:  *    "This product includes software developed by the OpenSSL Project
                     76:  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
                     77:  *
                     78:  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
                     79:  *    endorse or promote products derived from this software without
                     80:  *    prior written permission. For written permission, please contact
                     81:  *    openssl-core@openssl.org.
                     82:  *
                     83:  * 5. Products derived from this software may not be called "OpenSSL"
                     84:  *    nor may "OpenSSL" appear in their names without prior written
                     85:  *    permission of the OpenSSL Project.
                     86:  *
                     87:  * 6. Redistributions of any form whatsoever must retain the following
                     88:  *    acknowledgment:
                     89:  *    "This product includes software developed by the OpenSSL Project
                     90:  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
                     91:  *
                     92:  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
                     93:  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     94:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     95:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
                     96:  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
                     97:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     98:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
                     99:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                    100:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
                    101:  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                    102:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
                    103:  * OF THE POSSIBILITY OF SUCH DAMAGE.
                    104:  * ====================================================================
                    105:  *
                    106:  * This product includes cryptographic software written by Eric Young
                    107:  * (eay@cryptsoft.com).  This product includes software written by Tim
                    108:  * Hudson (tjh@cryptsoft.com).
                    109:  *
                    110:  */
                    111: /* ====================================================================
                    112:  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
                    113:  * ECC cipher suite support in OpenSSL originally developed by
                    114:  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
                    115:  */
                    116: /* ====================================================================
                    117:  * Copyright 2005 Nokia. All rights reserved.
                    118:  *
                    119:  * The portions of the attached software ("Contribution") is developed by
                    120:  * Nokia Corporation and is licensed pursuant to the OpenSSL open source
                    121:  * license.
                    122:  *
                    123:  * The Contribution, originally written by Mika Kousa and Pasi Eronen of
                    124:  * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
                    125:  * support (see RFC 4279) to OpenSSL.
                    126:  *
                    127:  * No patent licenses or other rights except those expressly stated in
                    128:  * the OpenSSL open source license shall be deemed granted or received
                    129:  * expressly, by implication, estoppel, or otherwise.
                    130:  *
                    131:  * No assurances are provided by Nokia that the Contribution does not
                    132:  * infringe the patent or other intellectual property rights of any third
                    133:  * party or that the license provides you with all the necessary rights
                    134:  * to make use of the Contribution.
                    135:  *
                    136:  * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
                    137:  * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
                    138:  * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
                    139:  * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
                    140:  * OTHERWISE.
                    141:  */
                    142:
                    143: /* Until the key-gen callbacks are modified to use newer prototypes, we allow
                    144:  * deprecated functions for openssl-internal code */
                    145: #ifdef OPENSSL_NO_DEPRECATED
                    146: #undef OPENSSL_NO_DEPRECATED
                    147: #endif
                    148:
                    149: #include <sys/types.h>
                    150: #include <sys/ioctl.h>
                    151: #include <sys/socket.h>
                    152:
                    153: #include <assert.h>
                    154: #include <ctype.h>
                    155: #include <stdio.h>
                    156: #include <stdlib.h>
                    157: #include <limits.h>
                    158: #include <string.h>
                    159: #include <unistd.h>
1.7       deraadt   160: #include <poll.h>
1.1       jsing     161:
                    162: #include "apps.h"
                    163:
                    164: #include <openssl/bn.h>
                    165: #include <openssl/err.h>
                    166: #include <openssl/lhash.h>
                    167: #include <openssl/ocsp.h>
                    168: #include <openssl/pem.h>
                    169: #include <openssl/ssl.h>
                    170: #include <openssl/x509.h>
                    171:
                    172: #ifndef OPENSSL_NO_DH
                    173: #include <openssl/dh.h>
                    174: #endif
                    175:
                    176: #include <openssl/rsa.h>
                    177:
                    178: #include "s_apps.h"
                    179: #include "timeouts.h"
                    180:
                    181: static int sv_body(char *hostname, int s, unsigned char *context);
                    182: static int www_body(char *hostname, int s, unsigned char *context);
                    183: static void close_accept_socket(void);
                    184: static void sv_usage(void);
                    185: static int init_ssl_connection(SSL * s);
                    186: static void print_stats(BIO * bp, SSL_CTX * ctx);
                    187: static int
                    188: generate_session_id(const SSL * ssl, unsigned char *id,
                    189:     unsigned int *id_len);
                    190: #ifndef OPENSSL_NO_DH
                    191: static DH *load_dh_param(const char *dhfile);
                    192: #endif
                    193:
                    194: static void s_server_init(void);
                    195:
                    196: /* static int load_CA(SSL_CTX *ctx, char *file);*/
                    197:
                    198: #define BUFSIZZ        16*1024
                    199: static int bufsize = BUFSIZZ;
                    200: static int accept_socket = -1;
                    201:
                    202: #define TEST_CERT      "server.pem"
                    203: #define TEST_CERT2     "server2.pem"
                    204:
                    205: static char *cipher = NULL;
                    206: static int s_server_verify = SSL_VERIFY_NONE;
                    207: static int s_server_session_id_context = 1;    /* anything will do */
                    208: static const char *s_cert_file = TEST_CERT, *s_key_file = NULL;
                    209: static const char *s_cert_file2 = TEST_CERT2, *s_key_file2 = NULL;
                    210: static char *s_dcert_file = NULL, *s_dkey_file = NULL;
                    211: static int s_nbio = 0;
                    212: static int s_nbio_test = 0;
                    213: int s_crlf = 0;
                    214: static SSL_CTX *ctx = NULL;
                    215: static SSL_CTX *ctx2 = NULL;
                    216: static int www = 0;
                    217:
                    218: static BIO *bio_s_out = NULL;
                    219: static int s_debug = 0;
                    220: static int s_tlsextdebug = 0;
                    221: static int s_tlsextstatus = 0;
                    222: static int cert_status_cb(SSL * s, void *arg);
                    223: static int s_msg = 0;
                    224: static int s_quiet = 0;
                    225:
                    226: static char *keymatexportlabel = NULL;
                    227: static int keymatexportlen = 20;
                    228:
                    229: #ifndef OPENSSL_NO_ENGINE
                    230: static char *engine_id = NULL;
                    231: #endif
                    232: static const char *session_id_prefix = NULL;
                    233:
                    234: static int enable_timeouts = 0;
                    235: static long socket_mtu;
                    236: #ifndef OPENSSL_NO_DTLS1
                    237: static int cert_chain = 0;
                    238: #endif
                    239:
                    240:
                    241:
                    242:
                    243: static void
                    244: s_server_init(void)
                    245: {
                    246:        accept_socket = -1;
                    247:        cipher = NULL;
                    248:        s_server_verify = SSL_VERIFY_NONE;
                    249:        s_dcert_file = NULL;
                    250:        s_dkey_file = NULL;
                    251:        s_cert_file = TEST_CERT;
                    252:        s_key_file = NULL;
                    253:        s_cert_file2 = TEST_CERT2;
                    254:        s_key_file2 = NULL;
                    255:        ctx2 = NULL;
                    256:        s_nbio = 0;
                    257:        s_nbio_test = 0;
                    258:        ctx = NULL;
                    259:        www = 0;
                    260:
                    261:        bio_s_out = NULL;
                    262:        s_debug = 0;
                    263:        s_msg = 0;
                    264:        s_quiet = 0;
                    265: #ifndef OPENSSL_NO_ENGINE
                    266:        engine_id = NULL;
                    267: #endif
                    268: }
                    269:
                    270: static void
                    271: sv_usage(void)
                    272: {
                    273:        BIO_printf(bio_err, "usage: s_server [args ...]\n");
                    274:        BIO_printf(bio_err, "\n");
                    275:        BIO_printf(bio_err, " -accept arg   - port to accept on (default is %d)\n", PORT);
                    276:        BIO_printf(bio_err, " -context arg  - set session ID context\n");
                    277:        BIO_printf(bio_err, " -verify arg   - turn on peer certificate verification\n");
                    278:        BIO_printf(bio_err, " -Verify arg   - turn on peer certificate verification, must have a cert.\n");
                    279:        BIO_printf(bio_err, " -cert arg     - certificate file to use\n");
                    280:        BIO_printf(bio_err, "                 (default is %s)\n", TEST_CERT);
                    281:        BIO_printf(bio_err, " -crl_check    - check the peer certificate has not been revoked by its CA.\n" \
                    282:            "                 The CRL(s) are appended to the certificate file\n");
                    283:        BIO_printf(bio_err, " -crl_check_all - check the peer certificate has not been revoked by its CA\n" \
                    284:            "                 or any other CRL in the CA chain. CRL(s) are appended to the\n" \
                    285:            "                 the certificate file.\n");
                    286:        BIO_printf(bio_err, " -certform arg - certificate format (PEM or DER) PEM default\n");
                    287:        BIO_printf(bio_err, " -key arg      - Private Key file to use, in cert file if\n");
                    288:        BIO_printf(bio_err, "                 not specified (default is %s)\n", TEST_CERT);
                    289:        BIO_printf(bio_err, " -keyform arg  - key format (PEM, DER or ENGINE) PEM default\n");
                    290:        BIO_printf(bio_err, " -pass arg     - private key file pass phrase source\n");
                    291:        BIO_printf(bio_err, " -dcert arg    - second certificate file to use (usually for DSA)\n");
                    292:        BIO_printf(bio_err, " -dcertform x  - second certificate format (PEM or DER) PEM default\n");
                    293:        BIO_printf(bio_err, " -dkey arg     - second private key file to use (usually for DSA)\n");
                    294:        BIO_printf(bio_err, " -dkeyform arg - second key format (PEM, DER or ENGINE) PEM default\n");
                    295:        BIO_printf(bio_err, " -dpass arg    - second private key file pass phrase source\n");
                    296:        BIO_printf(bio_err, " -dhparam arg  - DH parameter file to use, in cert file if not specified\n");
                    297:        BIO_printf(bio_err, "                 or a default set of parameters is used\n");
                    298:        BIO_printf(bio_err, " -named_curve arg  - Elliptic curve name to use for ephemeral ECDH keys.\n" \
                    299:            "                 Use \"openssl ecparam -list_curves\" for all names\n" \
                    300:            "                 (default is nistp256).\n");
                    301:        BIO_printf(bio_err, " -nbio         - Run with non-blocking IO\n");
                    302:        BIO_printf(bio_err, " -nbio_test    - test with the non-blocking test bio\n");
                    303:        BIO_printf(bio_err, " -crlf         - convert LF from terminal into CRLF\n");
                    304:        BIO_printf(bio_err, " -debug        - Print more output\n");
                    305:        BIO_printf(bio_err, " -msg          - Show protocol messages\n");
                    306:        BIO_printf(bio_err, " -state        - Print the SSL states\n");
                    307:        BIO_printf(bio_err, " -CApath arg   - PEM format directory of CA's\n");
                    308:        BIO_printf(bio_err, " -CAfile arg   - PEM format file of CA's\n");
                    309:        BIO_printf(bio_err, " -nocert       - Don't use any certificates (Anon-DH)\n");
                    310:        BIO_printf(bio_err, " -cipher arg   - play with 'openssl ciphers' to see what goes here\n");
                    311:        BIO_printf(bio_err, " -serverpref   - Use server's cipher preferences\n");
                    312:        BIO_printf(bio_err, " -quiet        - Inhibit printing of session and certificate information\n");
                    313:        BIO_printf(bio_err, " -ssl3         - Just talk SSLv3\n");
                    314:        BIO_printf(bio_err, " -tls1_2       - Just talk TLSv1.2\n");
                    315:        BIO_printf(bio_err, " -tls1_1       - Just talk TLSv1.1\n");
                    316:        BIO_printf(bio_err, " -tls1         - Just talk TLSv1\n");
                    317:        BIO_printf(bio_err, " -dtls1        - Just talk DTLSv1\n");
                    318:        BIO_printf(bio_err, " -timeout      - Enable timeouts\n");
                    319:        BIO_printf(bio_err, " -mtu          - Set link layer MTU\n");
                    320:        BIO_printf(bio_err, " -chain        - Read a certificate chain\n");
                    321:        BIO_printf(bio_err, " -no_ssl2      - Just disable SSLv2\n");
                    322:        BIO_printf(bio_err, " -no_ssl3      - Just disable SSLv3\n");
                    323:        BIO_printf(bio_err, " -no_tls1      - Just disable TLSv1\n");
                    324:        BIO_printf(bio_err, " -no_tls1_1    - Just disable TLSv1.1\n");
                    325:        BIO_printf(bio_err, " -no_tls1_2    - Just disable TLSv1.2\n");
                    326: #ifndef OPENSSL_NO_DH
                    327:        BIO_printf(bio_err, " -no_dhe       - Disable ephemeral DH\n");
                    328: #endif
                    329:        BIO_printf(bio_err, " -no_ecdhe     - Disable ephemeral ECDH\n");
                    330:        BIO_printf(bio_err, " -bugs         - Turn on SSL bug compatibility\n");
                    331:        BIO_printf(bio_err, " -www          - Respond to a 'GET /' with a status page\n");
                    332:        BIO_printf(bio_err, " -WWW          - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
                    333:        BIO_printf(bio_err, " -HTTP         - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
                    334:        BIO_printf(bio_err, "                 with the assumption it contains a complete HTTP response.\n");
                    335: #ifndef OPENSSL_NO_ENGINE
                    336:        BIO_printf(bio_err, " -engine id    - Initialise and use the specified engine\n");
                    337: #endif
                    338:        BIO_printf(bio_err, " -id_prefix arg - Generate SSL/TLS session IDs prefixed by 'arg'\n");
                    339:        BIO_printf(bio_err, " -servername host - servername for HostName TLS extension\n");
                    340:        BIO_printf(bio_err, " -servername_fatal - on mismatch send fatal alert (default warning alert)\n");
                    341:        BIO_printf(bio_err, " -cert2 arg    - certificate file to use for servername\n");
                    342:        BIO_printf(bio_err, "                 (default is %s)\n", TEST_CERT2);
                    343:        BIO_printf(bio_err, " -key2 arg     - Private Key file to use for servername, in cert file if\n");
                    344:        BIO_printf(bio_err, "                 not specified (default is %s)\n", TEST_CERT2);
                    345:        BIO_printf(bio_err, " -tlsextdebug  - hex dump of all TLS extensions received\n");
                    346:        BIO_printf(bio_err, " -no_ticket    - disable use of RFC4507bis session tickets\n");
                    347:        BIO_printf(bio_err, " -nextprotoneg arg - set the advertised protocols for the NPN extension (comma-separated list)\n");
1.8       jsing     348:        BIO_printf(bio_err," -alpn arg  - set the advertised protocols for the ALPN extension (comma-separated list)\n");
1.1       jsing     349: #ifndef OPENSSL_NO_SRTP
                    350:        BIO_printf(bio_err, " -use_srtp profiles - Offer SRTP key management with a colon-separated profile list\n");
                    351: #endif
                    352:        BIO_printf(bio_err, " -keymatexport label   - Export keying material using label\n");
                    353:        BIO_printf(bio_err, " -keymatexportlen len  - Export len bytes of keying material (default 20)\n");
                    354: }
                    355:
                    356: static int local_argc = 0;
                    357: static char **local_argv;
                    358:
                    359:
                    360: /* This is a context that we pass to callbacks */
                    361: typedef struct tlsextctx_st {
                    362:        char *servername;
                    363:        BIO *biodebug;
                    364:        int extension_error;
                    365: } tlsextctx;
                    366:
                    367:
                    368: static int
                    369: ssl_servername_cb(SSL * s, int *ad, void *arg)
                    370: {
                    371:        tlsextctx *p = (tlsextctx *) arg;
                    372:        const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
                    373:        if (servername && p->biodebug)
                    374:                BIO_printf(p->biodebug, "Hostname in TLS extension: \"%s\"\n", servername);
                    375:
                    376:        if (!p->servername)
                    377:                return SSL_TLSEXT_ERR_NOACK;
                    378:
                    379:        if (servername) {
                    380:                if (strcmp(servername, p->servername))
                    381:                        return p->extension_error;
                    382:                if (ctx2) {
                    383:                        BIO_printf(p->biodebug, "Switching server context.\n");
                    384:                        SSL_set_SSL_CTX(s, ctx2);
                    385:                }
                    386:        }
                    387:        return SSL_TLSEXT_ERR_OK;
                    388: }
                    389:
                    390: /* Structure passed to cert status callback */
                    391:
                    392: typedef struct tlsextstatusctx_st {
                    393:        /* Default responder to use */
                    394:        char *host, *path, *port;
                    395:        int use_ssl;
                    396:        int timeout;
                    397:        BIO *err;
                    398:        int verbose;
                    399: } tlsextstatusctx;
                    400:
                    401: static tlsextstatusctx tlscstatp = {NULL, NULL, NULL, 0, -1, NULL, 0};
                    402:
                    403: /* Certificate Status callback. This is called when a client includes a
                    404:  * certificate status request extension.
                    405:  *
                    406:  * This is a simplified version. It examines certificates each time and
                    407:  * makes one OCSP responder query for each request.
                    408:  *
                    409:  * A full version would store details such as the OCSP certificate IDs and
                    410:  * minimise the number of OCSP responses by caching them until they were
                    411:  * considered "expired".
                    412:  */
                    413:
                    414: static int
                    415: cert_status_cb(SSL * s, void *arg)
                    416: {
                    417:        tlsextstatusctx *srctx = arg;
                    418:        BIO *err = srctx->err;
                    419:        char *host, *port, *path;
                    420:        int use_ssl;
                    421:        unsigned char *rspder = NULL;
                    422:        int rspderlen;
                    423:        STACK_OF(OPENSSL_STRING) * aia = NULL;
                    424:        X509 *x = NULL;
                    425:        X509_STORE_CTX inctx;
                    426:        X509_OBJECT obj;
                    427:        OCSP_REQUEST *req = NULL;
                    428:        OCSP_RESPONSE *resp = NULL;
                    429:        OCSP_CERTID *id = NULL;
                    430:        STACK_OF(X509_EXTENSION) * exts;
                    431:        int ret = SSL_TLSEXT_ERR_NOACK;
                    432:        int i;
                    433:
                    434:        if (srctx->verbose)
                    435:                BIO_puts(err, "cert_status: callback called\n");
                    436:        /* Build up OCSP query from server certificate */
                    437:        x = SSL_get_certificate(s);
                    438:        aia = X509_get1_ocsp(x);
                    439:        if (aia) {
                    440:                if (!OCSP_parse_url(sk_OPENSSL_STRING_value(aia, 0),
                    441:                        &host, &port, &path, &use_ssl)) {
                    442:                        BIO_puts(err, "cert_status: can't parse AIA URL\n");
                    443:                        goto err;
                    444:                }
                    445:                if (srctx->verbose)
                    446:                        BIO_printf(err, "cert_status: AIA URL: %s\n",
                    447:                            sk_OPENSSL_STRING_value(aia, 0));
                    448:        } else {
                    449:                if (!srctx->host) {
                    450:                        BIO_puts(srctx->err, "cert_status: no AIA and no default responder URL\n");
                    451:                        goto done;
                    452:                }
                    453:                host = srctx->host;
                    454:                path = srctx->path;
                    455:                port = srctx->port;
                    456:                use_ssl = srctx->use_ssl;
                    457:        }
                    458:
                    459:        if (!X509_STORE_CTX_init(&inctx,
                    460:                SSL_CTX_get_cert_store(SSL_get_SSL_CTX(s)),
                    461:                NULL, NULL))
                    462:                goto err;
                    463:        if (X509_STORE_get_by_subject(&inctx, X509_LU_X509,
                    464:                X509_get_issuer_name(x), &obj) <= 0) {
                    465:                BIO_puts(err, "cert_status: Can't retrieve issuer certificate.\n");
                    466:                X509_STORE_CTX_cleanup(&inctx);
                    467:                goto done;
                    468:        }
                    469:        req = OCSP_REQUEST_new();
                    470:        if (!req)
                    471:                goto err;
                    472:        id = OCSP_cert_to_id(NULL, x, obj.data.x509);
                    473:        X509_free(obj.data.x509);
                    474:        X509_STORE_CTX_cleanup(&inctx);
                    475:        if (!id)
                    476:                goto err;
                    477:        if (!OCSP_request_add0_id(req, id))
                    478:                goto err;
                    479:        id = NULL;
                    480:        /* Add any extensions to the request */
                    481:        SSL_get_tlsext_status_exts(s, &exts);
                    482:        for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
                    483:                X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
                    484:                if (!OCSP_REQUEST_add_ext(req, ext, -1))
                    485:                        goto err;
                    486:        }
                    487:        resp = process_responder(err, req, host, path, port, use_ssl, NULL,
                    488:            srctx->timeout);
                    489:        if (!resp) {
                    490:                BIO_puts(err, "cert_status: error querying responder\n");
                    491:                goto done;
                    492:        }
                    493:        rspderlen = i2d_OCSP_RESPONSE(resp, &rspder);
                    494:        if (rspderlen <= 0)
                    495:                goto err;
                    496:        SSL_set_tlsext_status_ocsp_resp(s, rspder, rspderlen);
                    497:        if (srctx->verbose) {
                    498:                BIO_puts(err, "cert_status: ocsp response sent:\n");
                    499:                OCSP_RESPONSE_print(err, resp, 2);
                    500:        }
                    501:        ret = SSL_TLSEXT_ERR_OK;
                    502: done:
                    503:        if (ret != SSL_TLSEXT_ERR_OK)
                    504:                ERR_print_errors(err);
                    505:        if (aia) {
                    506:                free(host);
                    507:                free(path);
                    508:                free(port);
                    509:                X509_email_free(aia);
                    510:        }
                    511:        if (id)
                    512:                OCSP_CERTID_free(id);
                    513:        if (req)
                    514:                OCSP_REQUEST_free(req);
                    515:        if (resp)
                    516:                OCSP_RESPONSE_free(resp);
                    517:        return ret;
                    518: err:
                    519:        ret = SSL_TLSEXT_ERR_ALERT_FATAL;
                    520:        goto done;
                    521: }
                    522:
                    523: /* This is the context that we pass to next_proto_cb */
                    524: typedef struct tlsextnextprotoctx_st {
                    525:        unsigned char *data;
                    526:        unsigned int len;
                    527: } tlsextnextprotoctx;
                    528:
                    529: static int
                    530: next_proto_cb(SSL * s, const unsigned char **data, unsigned int *len, void *arg)
                    531: {
                    532:        tlsextnextprotoctx *next_proto = arg;
                    533:
                    534:        *data = next_proto->data;
                    535:        *len = next_proto->len;
                    536:
                    537:        return SSL_TLSEXT_ERR_OK;
                    538: }
                    539:
                    540:
1.8       jsing     541: /* This the context that we pass to alpn_cb */
                    542: typedef struct tlsextalpnctx_st {
                    543:        unsigned char *data;
                    544:        unsigned short len;
                    545: } tlsextalpnctx;
                    546:
                    547: static int
                    548: alpn_cb(SSL *s, const unsigned char **out, unsigned char *outlen,
                    549:     const unsigned char *in, unsigned int inlen, void *arg)
                    550: {
                    551:        tlsextalpnctx *alpn_ctx = arg;
                    552:
                    553:        if (!s_quiet) {
                    554:                /* We can assume that in is syntactically valid. */
                    555:                unsigned i;
                    556:
                    557:                BIO_printf(bio_s_out,
                    558:                    "ALPN protocols advertised by the client: ");
                    559:                for (i = 0; i < inlen; ) {
                    560:                        if (i)
                    561:                                BIO_write(bio_s_out, ", ", 2);
                    562:                        BIO_write(bio_s_out, &in[i + 1], in[i]);
                    563:                        i += in[i] + 1;
                    564:                }
                    565:                BIO_write(bio_s_out, "\n", 1);
                    566:        }
                    567:
                    568:        if (SSL_select_next_proto((unsigned char**)out, outlen, alpn_ctx->data,
                    569:            alpn_ctx->len, in, inlen) != OPENSSL_NPN_NEGOTIATED)
                    570:                return (SSL_TLSEXT_ERR_NOACK);
                    571:
                    572:        if (!s_quiet) {
                    573:                BIO_printf(bio_s_out, "ALPN protocols selected: ");
                    574:                BIO_write(bio_s_out, *out, *outlen);
                    575:                BIO_write(bio_s_out, "\n", 1);
                    576:        }
                    577:
                    578:        return (SSL_TLSEXT_ERR_OK);
                    579: }
1.1       jsing     580:
                    581: int s_server_main(int, char **);
                    582:
                    583: #ifndef OPENSSL_NO_SRTP
                    584: static char *srtp_profiles = NULL;
                    585: #endif
                    586:
                    587: int
                    588: s_server_main(int argc, char *argv[])
                    589: {
                    590:        X509_VERIFY_PARAM *vpm = NULL;
                    591:        int badarg = 0;
                    592:        short port = PORT;
                    593:        char *CApath = NULL, *CAfile = NULL;
                    594:        unsigned char *context = NULL;
                    595:        char *dhfile = NULL;
                    596:        char *named_curve = NULL;
                    597:        int badop = 0, bugs = 0;
                    598:        int ret = 1;
                    599:        int off = 0;
1.5       jsing     600:        int no_dhe = 0, no_ecdhe = 0, nocert = 0;
1.1       jsing     601:        int state = 0;
                    602:        const SSL_METHOD *meth = NULL;
                    603:        int socket_type = SOCK_STREAM;
                    604:        ENGINE *e = NULL;
                    605:        int s_cert_format = FORMAT_PEM, s_key_format = FORMAT_PEM;
                    606:        char *passarg = NULL, *pass = NULL;
                    607:        char *dpassarg = NULL, *dpass = NULL;
                    608:        int s_dcert_format = FORMAT_PEM, s_dkey_format = FORMAT_PEM;
                    609:        X509 *s_cert = NULL, *s_dcert = NULL;
                    610:        EVP_PKEY *s_key = NULL, *s_dkey = NULL;
                    611:        int no_cache = 0;
                    612:        const char *errstr = NULL;
                    613:        EVP_PKEY *s_key2 = NULL;
                    614:        X509 *s_cert2 = NULL;
                    615:        tlsextctx tlsextcbp = {NULL, NULL, SSL_TLSEXT_ERR_ALERT_WARNING};
                    616:        const char *next_proto_neg_in = NULL;
1.8       jsing     617:        tlsextnextprotoctx next_proto = { NULL, 0 };
                    618:        const char *alpn_in = NULL;
                    619:        tlsextalpnctx alpn_ctx = { NULL, 0 };
1.1       jsing     620:        meth = SSLv23_server_method();
                    621:
                    622:        local_argc = argc;
                    623:        local_argv = argv;
                    624:
                    625:        s_server_init();
                    626:
                    627:        verify_depth = 0;
                    628:        s_nbio = 0;
                    629:        s_nbio_test = 0;
                    630:
                    631:        argc--;
                    632:        argv++;
                    633:
                    634:        while (argc >= 1) {
                    635:                if ((strcmp(*argv, "-port") == 0) ||
                    636:                    (strcmp(*argv, "-accept") == 0)) {
                    637:                        if (--argc < 1)
                    638:                                goto bad;
                    639:                        if (!extract_port(*(++argv), &port))
                    640:                                goto bad;
                    641:                } else if (strcmp(*argv, "-verify") == 0) {
                    642:                        s_server_verify = SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE;
                    643:                        if (--argc < 1)
                    644:                                goto bad;
                    645:                        verify_depth = strtonum(*(++argv), 0, INT_MAX, &errstr);
                    646:                        if (errstr)
                    647:                                goto bad;
                    648:                        BIO_printf(bio_err, "verify depth is %d\n", verify_depth);
                    649:                } else if (strcmp(*argv, "-Verify") == 0) {
                    650:                        s_server_verify = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
                    651:                            SSL_VERIFY_CLIENT_ONCE;
                    652:                        if (--argc < 1)
                    653:                                goto bad;
                    654:                        verify_depth = strtonum(*(++argv), 0, INT_MAX, &errstr);
                    655:                        if (errstr)
                    656:                                goto bad;
                    657:                        BIO_printf(bio_err, "verify depth is %d, must return a certificate\n", verify_depth);
                    658:                } else if (strcmp(*argv, "-context") == 0) {
                    659:                        if (--argc < 1)
                    660:                                goto bad;
                    661:                        context = (unsigned char *) *(++argv);
                    662:                } else if (strcmp(*argv, "-cert") == 0) {
                    663:                        if (--argc < 1)
                    664:                                goto bad;
                    665:                        s_cert_file = *(++argv);
                    666:                } else if (strcmp(*argv, "-certform") == 0) {
                    667:                        if (--argc < 1)
                    668:                                goto bad;
                    669:                        s_cert_format = str2fmt(*(++argv));
                    670:                } else if (strcmp(*argv, "-key") == 0) {
                    671:                        if (--argc < 1)
                    672:                                goto bad;
                    673:                        s_key_file = *(++argv);
                    674:                } else if (strcmp(*argv, "-keyform") == 0) {
                    675:                        if (--argc < 1)
                    676:                                goto bad;
                    677:                        s_key_format = str2fmt(*(++argv));
                    678:                } else if (strcmp(*argv, "-pass") == 0) {
                    679:                        if (--argc < 1)
                    680:                                goto bad;
                    681:                        passarg = *(++argv);
                    682:                } else if (strcmp(*argv, "-dhparam") == 0) {
                    683:                        if (--argc < 1)
                    684:                                goto bad;
                    685:                        dhfile = *(++argv);
                    686:                }
                    687:                else if (strcmp(*argv, "-named_curve") == 0) {
                    688:                        if (--argc < 1)
                    689:                                goto bad;
                    690:                        named_curve = *(++argv);
                    691:                }
                    692:                else if (strcmp(*argv, "-dcertform") == 0) {
                    693:                        if (--argc < 1)
                    694:                                goto bad;
                    695:                        s_dcert_format = str2fmt(*(++argv));
                    696:                } else if (strcmp(*argv, "-dcert") == 0) {
                    697:                        if (--argc < 1)
                    698:                                goto bad;
                    699:                        s_dcert_file = *(++argv);
                    700:                } else if (strcmp(*argv, "-dkeyform") == 0) {
                    701:                        if (--argc < 1)
                    702:                                goto bad;
                    703:                        s_dkey_format = str2fmt(*(++argv));
                    704:                } else if (strcmp(*argv, "-dpass") == 0) {
                    705:                        if (--argc < 1)
                    706:                                goto bad;
                    707:                        dpassarg = *(++argv);
                    708:                } else if (strcmp(*argv, "-dkey") == 0) {
                    709:                        if (--argc < 1)
                    710:                                goto bad;
                    711:                        s_dkey_file = *(++argv);
                    712:                } else if (strcmp(*argv, "-nocert") == 0) {
                    713:                        nocert = 1;
                    714:                } else if (strcmp(*argv, "-CApath") == 0) {
                    715:                        if (--argc < 1)
                    716:                                goto bad;
                    717:                        CApath = *(++argv);
                    718:                } else if (strcmp(*argv, "-no_cache") == 0)
                    719:                        no_cache = 1;
                    720:                else if (args_verify(&argv, &argc, &badarg, bio_err, &vpm)) {
                    721:                        if (badarg)
                    722:                                goto bad;
                    723:                        continue;
                    724:                } else if (strcmp(*argv, "-verify_return_error") == 0)
                    725:                        verify_return_error = 1;
                    726:                else if (strcmp(*argv, "-serverpref") == 0) {
                    727:                        off |= SSL_OP_CIPHER_SERVER_PREFERENCE;
                    728:                } else if (strcmp(*argv, "-legacy_renegotiation") == 0)
                    729:                        ; /* no-op */
                    730:                else if (strcmp(*argv, "-cipher") == 0) {
                    731:                        if (--argc < 1)
                    732:                                goto bad;
                    733:                        cipher = *(++argv);
                    734:                } else if (strcmp(*argv, "-CAfile") == 0) {
                    735:                        if (--argc < 1)
                    736:                                goto bad;
                    737:                        CAfile = *(++argv);
                    738:                }
                    739:                else if (strcmp(*argv, "-nbio") == 0) {
                    740:                        s_nbio = 1;
                    741:                }
                    742:                else if (strcmp(*argv, "-nbio_test") == 0) {
                    743:                        s_nbio = 1;
                    744:                        s_nbio_test = 1;
                    745:                } else if (strcmp(*argv, "-debug") == 0) {
                    746:                        s_debug = 1;
                    747:                }
                    748:                else if (strcmp(*argv, "-tlsextdebug") == 0)
                    749:                        s_tlsextdebug = 1;
                    750:                else if (strcmp(*argv, "-status") == 0)
                    751:                        s_tlsextstatus = 1;
                    752:                else if (strcmp(*argv, "-status_verbose") == 0) {
                    753:                        s_tlsextstatus = 1;
                    754:                        tlscstatp.verbose = 1;
                    755:                } else if (!strcmp(*argv, "-status_timeout")) {
                    756:                        s_tlsextstatus = 1;
                    757:                        if (--argc < 1)
                    758:                                goto bad;
                    759:                        tlscstatp.timeout = strtonum(*(++argv), 0, INT_MAX, &errstr);
                    760:                        if (errstr)
                    761:                                goto bad;
                    762:                } else if (!strcmp(*argv, "-status_url")) {
                    763:                        s_tlsextstatus = 1;
                    764:                        if (--argc < 1)
                    765:                                goto bad;
                    766:                        if (!OCSP_parse_url(*(++argv),
                    767:                                &tlscstatp.host,
                    768:                                &tlscstatp.port,
                    769:                                &tlscstatp.path,
                    770:                                &tlscstatp.use_ssl)) {
                    771:                                BIO_printf(bio_err, "Error parsing URL\n");
                    772:                                goto bad;
                    773:                        }
                    774:                }
                    775:                else if (strcmp(*argv, "-msg") == 0) {
                    776:                        s_msg = 1;
                    777:                } else if (strcmp(*argv, "-state") == 0) {
                    778:                        state = 1;
                    779:                } else if (strcmp(*argv, "-crlf") == 0) {
                    780:                        s_crlf = 1;
                    781:                } else if (strcmp(*argv, "-quiet") == 0) {
                    782:                        s_quiet = 1;
                    783:                } else if (strcmp(*argv, "-bugs") == 0) {
                    784:                        bugs = 1;
                    785:                } else if (strcmp(*argv, "-no_tmp_rsa") == 0) {
1.5       jsing     786:                        /* No-op. */
1.1       jsing     787:                } else if (strcmp(*argv, "-no_dhe") == 0) {
                    788:                        no_dhe = 1;
                    789:                } else if (strcmp(*argv, "-no_ecdhe") == 0) {
                    790:                        no_ecdhe = 1;
1.5       jsing     791:                } else if (strcmp(*argv, "-www") == 0) {
1.1       jsing     792:                        www = 1;
                    793:                } else if (strcmp(*argv, "-WWW") == 0) {
                    794:                        www = 2;
                    795:                } else if (strcmp(*argv, "-HTTP") == 0) {
                    796:                        www = 3;
                    797:                } else if (strcmp(*argv, "-no_ssl2") == 0) {
                    798:                        off |= SSL_OP_NO_SSLv2;
                    799:                } else if (strcmp(*argv, "-no_ssl3") == 0) {
                    800:                        off |= SSL_OP_NO_SSLv3;
                    801:                } else if (strcmp(*argv, "-no_tls1") == 0) {
                    802:                        off |= SSL_OP_NO_TLSv1;
                    803:                } else if (strcmp(*argv, "-no_tls1_1") == 0) {
                    804:                        off |= SSL_OP_NO_TLSv1_1;
                    805:                } else if (strcmp(*argv, "-no_tls1_2") == 0) {
                    806:                        off |= SSL_OP_NO_TLSv1_2;
                    807:                } else if (strcmp(*argv, "-no_comp") == 0) {
                    808:                        off |= SSL_OP_NO_COMPRESSION;
                    809:                }
                    810:                else if (strcmp(*argv, "-no_ticket") == 0) {
                    811:                        off |= SSL_OP_NO_TICKET;
                    812:                }
                    813:                else if (strcmp(*argv, "-ssl3") == 0) {
                    814:                        meth = SSLv3_server_method();
                    815:                } else if (strcmp(*argv, "-tls1") == 0) {
                    816:                        meth = TLSv1_server_method();
                    817:                } else if (strcmp(*argv, "-tls1_1") == 0) {
                    818:                        meth = TLSv1_1_server_method();
                    819:                } else if (strcmp(*argv, "-tls1_2") == 0) {
                    820:                        meth = TLSv1_2_server_method();
                    821:                }
                    822: #ifndef OPENSSL_NO_DTLS1
                    823:                else if (strcmp(*argv, "-dtls1") == 0) {
                    824:                        meth = DTLSv1_server_method();
                    825:                        socket_type = SOCK_DGRAM;
                    826:                } else if (strcmp(*argv, "-timeout") == 0)
                    827:                        enable_timeouts = 1;
                    828:                else if (strcmp(*argv, "-mtu") == 0) {
                    829:                        if (--argc < 1)
                    830:                                goto bad;
                    831:                        socket_mtu = strtonum(*(++argv), 0, LONG_MAX, &errstr);
                    832:                        if (errstr)
                    833:                                goto bad;
                    834:                } else if (strcmp(*argv, "-chain") == 0)
                    835:                        cert_chain = 1;
                    836: #endif
                    837:                else if (strcmp(*argv, "-id_prefix") == 0) {
                    838:                        if (--argc < 1)
                    839:                                goto bad;
                    840:                        session_id_prefix = *(++argv);
                    841:                }
                    842: #ifndef OPENSSL_NO_ENGINE
                    843:                else if (strcmp(*argv, "-engine") == 0) {
                    844:                        if (--argc < 1)
                    845:                                goto bad;
                    846:                        engine_id = *(++argv);
                    847:                }
                    848: #endif
                    849:                else if (strcmp(*argv, "-servername") == 0) {
                    850:                        if (--argc < 1)
                    851:                                goto bad;
                    852:                        tlsextcbp.servername = *(++argv);
                    853:                } else if (strcmp(*argv, "-servername_fatal") == 0) {
                    854:                        tlsextcbp.extension_error = SSL_TLSEXT_ERR_ALERT_FATAL;
                    855:                } else if (strcmp(*argv, "-cert2") == 0) {
                    856:                        if (--argc < 1)
                    857:                                goto bad;
                    858:                        s_cert_file2 = *(++argv);
                    859:                } else if (strcmp(*argv, "-key2") == 0) {
                    860:                        if (--argc < 1)
                    861:                                goto bad;
                    862:                        s_key_file2 = *(++argv);
                    863:                }
                    864:                else if (strcmp(*argv, "-nextprotoneg") == 0) {
                    865:                        if (--argc < 1)
                    866:                                goto bad;
                    867:                        next_proto_neg_in = *(++argv);
                    868:                }
1.8       jsing     869:                else if (strcmp(*argv,"-alpn") == 0) {
                    870:                        if (--argc < 1)
                    871:                                goto bad;
                    872:                        alpn_in = *(++argv);
                    873:                }
1.1       jsing     874: #ifndef OPENSSL_NO_SRTP
                    875:                else if (strcmp(*argv, "-use_srtp") == 0) {
                    876:                        if (--argc < 1)
                    877:                                goto bad;
                    878:                        srtp_profiles = *(++argv);
                    879:                }
                    880: #endif
                    881:                else if (strcmp(*argv, "-keymatexport") == 0) {
                    882:                        if (--argc < 1)
                    883:                                goto bad;
                    884:                        keymatexportlabel = *(++argv);
                    885:                } else if (strcmp(*argv, "-keymatexportlen") == 0) {
                    886:                        if (--argc < 1)
                    887:                                goto bad;
                    888:                        keymatexportlen = strtonum(*(++argv), 1, INT_MAX, &errstr);
                    889:                        if (errstr)
                    890:                                goto bad;
                    891:                } else {
                    892:                        BIO_printf(bio_err, "unknown option %s\n", *argv);
                    893:                        badop = 1;
                    894:                        break;
                    895:                }
                    896:                argc--;
                    897:                argv++;
                    898:        }
                    899:        if (badop) {
                    900: bad:
                    901:                if (errstr)
                    902:                        BIO_printf(bio_err, "invalid argument %s: %s\n",
                    903:                            *argv, errstr);
                    904:                else
                    905:                        sv_usage();
                    906:                goto end;
                    907:        }
                    908:
                    909: #ifndef OPENSSL_NO_ENGINE
                    910:        e = setup_engine(bio_err, engine_id, 1);
                    911: #endif
                    912:
                    913:        if (!app_passwd(bio_err, passarg, dpassarg, &pass, &dpass)) {
                    914:                BIO_printf(bio_err, "Error getting password\n");
                    915:                goto end;
                    916:        }
                    917:        if (s_key_file == NULL)
                    918:                s_key_file = s_cert_file;
                    919:        if (s_key_file2 == NULL)
                    920:                s_key_file2 = s_cert_file2;
                    921:
                    922:        if (nocert == 0) {
                    923:                s_key = load_key(bio_err, s_key_file, s_key_format, 0, pass, e,
                    924:                    "server certificate private key file");
                    925:                if (!s_key) {
                    926:                        ERR_print_errors(bio_err);
                    927:                        goto end;
                    928:                }
                    929:                s_cert = load_cert(bio_err, s_cert_file, s_cert_format,
                    930:                    NULL, e, "server certificate file");
                    931:
                    932:                if (!s_cert) {
                    933:                        ERR_print_errors(bio_err);
                    934:                        goto end;
                    935:                }
                    936:                if (tlsextcbp.servername) {
                    937:                        s_key2 = load_key(bio_err, s_key_file2, s_key_format, 0, pass, e,
                    938:                            "second server certificate private key file");
                    939:                        if (!s_key2) {
                    940:                                ERR_print_errors(bio_err);
                    941:                                goto end;
                    942:                        }
                    943:                        s_cert2 = load_cert(bio_err, s_cert_file2, s_cert_format,
                    944:                            NULL, e, "second server certificate file");
                    945:
                    946:                        if (!s_cert2) {
                    947:                                ERR_print_errors(bio_err);
                    948:                                goto end;
                    949:                        }
                    950:                }
                    951:        }
                    952:        if (next_proto_neg_in) {
                    953:                unsigned short len;
                    954:                next_proto.data = next_protos_parse(&len, next_proto_neg_in);
                    955:                if (next_proto.data == NULL)
                    956:                        goto end;
                    957:                next_proto.len = len;
                    958:        } else {
                    959:                next_proto.data = NULL;
                    960:        }
1.8       jsing     961:        alpn_ctx.data = NULL;
                    962:        if (alpn_in) {
                    963:                unsigned short len;
                    964:                alpn_ctx.data = next_protos_parse(&len, alpn_in);
                    965:                if (alpn_ctx.data == NULL)
                    966:                        goto end;
                    967:                alpn_ctx.len = len;
                    968:        }
1.1       jsing     969:
                    970:        if (s_dcert_file) {
                    971:
                    972:                if (s_dkey_file == NULL)
                    973:                        s_dkey_file = s_dcert_file;
                    974:
                    975:                s_dkey = load_key(bio_err, s_dkey_file, s_dkey_format,
                    976:                    0, dpass, e,
                    977:                    "second certificate private key file");
                    978:                if (!s_dkey) {
                    979:                        ERR_print_errors(bio_err);
                    980:                        goto end;
                    981:                }
                    982:                s_dcert = load_cert(bio_err, s_dcert_file, s_dcert_format,
                    983:                    NULL, e, "second server certificate file");
                    984:
                    985:                if (!s_dcert) {
                    986:                        ERR_print_errors(bio_err);
                    987:                        goto end;
                    988:                }
                    989:        }
                    990:        if (bio_s_out == NULL) {
                    991:                if (s_quiet && !s_debug && !s_msg) {
                    992:                        bio_s_out = BIO_new(BIO_s_null());
                    993:                } else {
                    994:                        if (bio_s_out == NULL)
                    995:                                bio_s_out = BIO_new_fp(stdout, BIO_NOCLOSE);
                    996:                }
                    997:        }
1.8       jsing     998:        if (nocert) {
1.1       jsing     999:                s_cert_file = NULL;
                   1000:                s_key_file = NULL;
                   1001:                s_dcert_file = NULL;
                   1002:                s_dkey_file = NULL;
                   1003:                s_cert_file2 = NULL;
                   1004:                s_key_file2 = NULL;
                   1005:        }
                   1006:        ctx = SSL_CTX_new(meth);
                   1007:        if (ctx == NULL) {
                   1008:                ERR_print_errors(bio_err);
                   1009:                goto end;
                   1010:        }
                   1011:        if (session_id_prefix) {
                   1012:                if (strlen(session_id_prefix) >= 32)
                   1013:                        BIO_printf(bio_err,
                   1014:                            "warning: id_prefix is too long, only one new session will be possible\n");
                   1015:                else if (strlen(session_id_prefix) >= 16)
                   1016:                        BIO_printf(bio_err,
                   1017:                            "warning: id_prefix is too long if you use SSLv2\n");
                   1018:                if (!SSL_CTX_set_generate_session_id(ctx, generate_session_id)) {
                   1019:                        BIO_printf(bio_err, "error setting 'id_prefix'\n");
                   1020:                        ERR_print_errors(bio_err);
                   1021:                        goto end;
                   1022:                }
                   1023:                BIO_printf(bio_err, "id_prefix '%s' set.\n", session_id_prefix);
                   1024:        }
                   1025:        SSL_CTX_set_quiet_shutdown(ctx, 1);
                   1026:        if (bugs)
                   1027:                SSL_CTX_set_options(ctx, SSL_OP_ALL);
                   1028:        SSL_CTX_set_options(ctx, off);
                   1029:        /*
                   1030:         * DTLS: partial reads end up discarding unread UDP bytes :-( Setting
                   1031:         * read ahead solves this problem.
                   1032:         */
                   1033:        if (socket_type == SOCK_DGRAM)
                   1034:                SSL_CTX_set_read_ahead(ctx, 1);
                   1035:
                   1036:        if (state)
                   1037:                SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback);
                   1038:        if (no_cache)
                   1039:                SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
                   1040:        else
                   1041:                SSL_CTX_sess_set_cache_size(ctx, 128);
                   1042:
                   1043: #ifndef OPENSSL_NO_SRTP
                   1044:        if (srtp_profiles != NULL)
                   1045:                SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles);
                   1046: #endif
                   1047:
                   1048:
                   1049:        if ((!SSL_CTX_load_verify_locations(ctx, CAfile, CApath)) ||
                   1050:            (!SSL_CTX_set_default_verify_paths(ctx))) {
                   1051:                /* BIO_printf(bio_err,"X509_load_verify_locations\n"); */
                   1052:                ERR_print_errors(bio_err);
                   1053:                /* goto end; */
                   1054:        }
                   1055:        if (vpm)
                   1056:                SSL_CTX_set1_param(ctx, vpm);
                   1057:
                   1058:        if (s_cert2) {
                   1059:                ctx2 = SSL_CTX_new(meth);
                   1060:                if (ctx2 == NULL) {
                   1061:                        ERR_print_errors(bio_err);
                   1062:                        goto end;
                   1063:                }
                   1064:        }
                   1065:        if (ctx2) {
                   1066:                BIO_printf(bio_s_out, "Setting secondary ctx parameters\n");
                   1067:
                   1068:                if (session_id_prefix) {
                   1069:                        if (strlen(session_id_prefix) >= 32)
                   1070:                                BIO_printf(bio_err,
                   1071:                                    "warning: id_prefix is too long, only one new session will be possible\n");
                   1072:                        else if (strlen(session_id_prefix) >= 16)
                   1073:                                BIO_printf(bio_err,
                   1074:                                    "warning: id_prefix is too long if you use SSLv2\n");
                   1075:                        if (!SSL_CTX_set_generate_session_id(ctx2, generate_session_id)) {
                   1076:                                BIO_printf(bio_err, "error setting 'id_prefix'\n");
                   1077:                                ERR_print_errors(bio_err);
                   1078:                                goto end;
                   1079:                        }
                   1080:                        BIO_printf(bio_err, "id_prefix '%s' set.\n", session_id_prefix);
                   1081:                }
                   1082:                SSL_CTX_set_quiet_shutdown(ctx2, 1);
                   1083:                if (bugs)
                   1084:                        SSL_CTX_set_options(ctx2, SSL_OP_ALL);
                   1085:                SSL_CTX_set_options(ctx2, off);
                   1086:                /*
                   1087:                 * DTLS: partial reads end up discarding unread UDP bytes :-(
                   1088:                 * Setting read ahead solves this problem.
                   1089:                 */
                   1090:                if (socket_type == SOCK_DGRAM)
                   1091:                        SSL_CTX_set_read_ahead(ctx2, 1);
                   1092:
                   1093:                if (state)
                   1094:                        SSL_CTX_set_info_callback(ctx2, apps_ssl_info_callback);
                   1095:
                   1096:                if (no_cache)
                   1097:                        SSL_CTX_set_session_cache_mode(ctx2, SSL_SESS_CACHE_OFF);
                   1098:                else
                   1099:                        SSL_CTX_sess_set_cache_size(ctx2, 128);
                   1100:
                   1101:                if ((!SSL_CTX_load_verify_locations(ctx2, CAfile, CApath)) ||
                   1102:                    (!SSL_CTX_set_default_verify_paths(ctx2))) {
                   1103:                        ERR_print_errors(bio_err);
                   1104:                }
                   1105:                if (vpm)
                   1106:                        SSL_CTX_set1_param(ctx2, vpm);
                   1107:        }
                   1108:        if (next_proto.data)
                   1109:                SSL_CTX_set_next_protos_advertised_cb(ctx, next_proto_cb, &next_proto);
1.8       jsing    1110:        if (alpn_ctx.data)
                   1111:                SSL_CTX_set_alpn_select_cb(ctx, alpn_cb, &alpn_ctx);
1.1       jsing    1112:
                   1113: #ifndef OPENSSL_NO_DH
                   1114:        if (!no_dhe) {
                   1115:                DH *dh = NULL;
                   1116:
                   1117:                if (dhfile)
                   1118:                        dh = load_dh_param(dhfile);
                   1119:                else if (s_cert_file)
                   1120:                        dh = load_dh_param(s_cert_file);
                   1121:
1.4       jsing    1122:                if (dh != NULL)
1.1       jsing    1123:                        BIO_printf(bio_s_out, "Setting temp DH parameters\n");
1.4       jsing    1124:                else
                   1125:                        BIO_printf(bio_s_out, "Using auto DH parameters\n");
                   1126:                (void) BIO_flush(bio_s_out);
                   1127:
                   1128:                if (dh == NULL)
                   1129:                        SSL_CTX_set_dh_auto(ctx, 1);
                   1130:                else if (!SSL_CTX_set_tmp_dh(ctx, dh)) {
                   1131:                        BIO_printf(bio_err,
                   1132:                            "Error setting temp DH parameters\n");
                   1133:                        ERR_print_errors(bio_err);
                   1134:                        DH_free(dh);
                   1135:                        goto end;
1.1       jsing    1136:                }
                   1137:
                   1138:                if (ctx2) {
                   1139:                        if (!dhfile) {
                   1140:                                DH *dh2 = load_dh_param(s_cert_file2);
                   1141:                                if (dh2 != NULL) {
                   1142:                                        BIO_printf(bio_s_out, "Setting temp DH parameters\n");
                   1143:                                        (void) BIO_flush(bio_s_out);
                   1144:
                   1145:                                        DH_free(dh);
                   1146:                                        dh = dh2;
                   1147:                                }
                   1148:                        }
1.4       jsing    1149:                        if (dh == NULL)
                   1150:                                SSL_CTX_set_dh_auto(ctx2, 1);
                   1151:                        else if (!SSL_CTX_set_tmp_dh(ctx2, dh)) {
                   1152:                                BIO_printf(bio_err,
                   1153:                                    "Error setting temp DH parameters\n");
                   1154:                                ERR_print_errors(bio_err);
                   1155:                                DH_free(dh);
                   1156:                                goto end;
                   1157:                        }
1.1       jsing    1158:                }
                   1159:                DH_free(dh);
                   1160:        }
                   1161: #endif
                   1162:
                   1163:        if (!no_ecdhe) {
                   1164:                EC_KEY *ecdh = NULL;
                   1165:
                   1166:                if (named_curve) {
                   1167:                        int nid = OBJ_sn2nid(named_curve);
                   1168:
                   1169:                        if (nid == 0) {
                   1170:                                BIO_printf(bio_err, "unknown curve name (%s)\n",
                   1171:                                    named_curve);
                   1172:                                goto end;
                   1173:                        }
                   1174:                        ecdh = EC_KEY_new_by_curve_name(nid);
                   1175:                        if (ecdh == NULL) {
                   1176:                                BIO_printf(bio_err, "unable to create curve (%s)\n",
                   1177:                                    named_curve);
                   1178:                                goto end;
                   1179:                        }
                   1180:                }
                   1181:                if (ecdh != NULL) {
                   1182:                        BIO_printf(bio_s_out, "Setting temp ECDH parameters\n");
                   1183:                } else {
                   1184:                        BIO_printf(bio_s_out, "Using default temp ECDH parameters\n");
                   1185:                        ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
                   1186:                        if (ecdh == NULL) {
                   1187:                                BIO_printf(bio_err, "unable to create curve (nistp256)\n");
                   1188:                                goto end;
                   1189:                        }
                   1190:                }
                   1191:                (void) BIO_flush(bio_s_out);
                   1192:
                   1193:                SSL_CTX_set_tmp_ecdh(ctx, ecdh);
                   1194:                if (ctx2)
                   1195:                        SSL_CTX_set_tmp_ecdh(ctx2, ecdh);
                   1196:                EC_KEY_free(ecdh);
                   1197:        }
                   1198:
                   1199:        if (!set_cert_key_stuff(ctx, s_cert, s_key))
                   1200:                goto end;
                   1201:        if (ctx2 && !set_cert_key_stuff(ctx2, s_cert2, s_key2))
                   1202:                goto end;
                   1203:        if (s_dcert != NULL) {
                   1204:                if (!set_cert_key_stuff(ctx, s_dcert, s_dkey))
                   1205:                        goto end;
                   1206:        }
                   1207:
                   1208:        if (cipher != NULL) {
                   1209:                if (!SSL_CTX_set_cipher_list(ctx, cipher)) {
                   1210:                        BIO_printf(bio_err, "error setting cipher list\n");
                   1211:                        ERR_print_errors(bio_err);
                   1212:                        goto end;
                   1213:                }
                   1214:                if (ctx2 && !SSL_CTX_set_cipher_list(ctx2, cipher)) {
                   1215:                        BIO_printf(bio_err, "error setting cipher list\n");
                   1216:                        ERR_print_errors(bio_err);
                   1217:                        goto end;
                   1218:                }
                   1219:        }
                   1220:        SSL_CTX_set_verify(ctx, s_server_verify, verify_callback);
                   1221:        SSL_CTX_set_session_id_context(ctx, (void *) &s_server_session_id_context,
                   1222:            sizeof s_server_session_id_context);
                   1223:
                   1224:        /* Set DTLS cookie generation and verification callbacks */
                   1225:        SSL_CTX_set_cookie_generate_cb(ctx, generate_cookie_callback);
                   1226:        SSL_CTX_set_cookie_verify_cb(ctx, verify_cookie_callback);
                   1227:
                   1228:        if (ctx2) {
                   1229:                SSL_CTX_set_verify(ctx2, s_server_verify, verify_callback);
                   1230:                SSL_CTX_set_session_id_context(ctx2, (void *) &s_server_session_id_context,
                   1231:                    sizeof s_server_session_id_context);
                   1232:
                   1233:                tlsextcbp.biodebug = bio_s_out;
                   1234:                SSL_CTX_set_tlsext_servername_callback(ctx2, ssl_servername_cb);
                   1235:                SSL_CTX_set_tlsext_servername_arg(ctx2, &tlsextcbp);
                   1236:                SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
                   1237:                SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
                   1238:        }
                   1239:
                   1240:        if (CAfile != NULL) {
                   1241:                SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(CAfile));
                   1242:                if (ctx2)
                   1243:                        SSL_CTX_set_client_CA_list(ctx2, SSL_load_client_CA_file(CAfile));
                   1244:        }
                   1245:        BIO_printf(bio_s_out, "ACCEPT\n");
                   1246:        (void) BIO_flush(bio_s_out);
                   1247:        if (www)
                   1248:                do_server(port, socket_type, &accept_socket, www_body, context);
                   1249:        else
                   1250:                do_server(port, socket_type, &accept_socket, sv_body, context);
                   1251:        print_stats(bio_s_out, ctx);
                   1252:        ret = 0;
                   1253: end:
                   1254:        if (ctx != NULL)
                   1255:                SSL_CTX_free(ctx);
                   1256:        if (s_cert)
                   1257:                X509_free(s_cert);
                   1258:        if (s_dcert)
                   1259:                X509_free(s_dcert);
                   1260:        if (s_key)
                   1261:                EVP_PKEY_free(s_key);
                   1262:        if (s_dkey)
                   1263:                EVP_PKEY_free(s_dkey);
                   1264:        free(pass);
                   1265:        free(dpass);
                   1266:        if (vpm)
                   1267:                X509_VERIFY_PARAM_free(vpm);
                   1268:        free(tlscstatp.host);
                   1269:        free(tlscstatp.port);
                   1270:        free(tlscstatp.path);
                   1271:        if (ctx2 != NULL)
                   1272:                SSL_CTX_free(ctx2);
                   1273:        if (s_cert2)
                   1274:                X509_free(s_cert2);
                   1275:        if (s_key2)
                   1276:                EVP_PKEY_free(s_key2);
1.8       jsing    1277:        free(next_proto.data);
                   1278:        free(alpn_ctx.data);
1.1       jsing    1279:        if (bio_s_out != NULL) {
                   1280:                BIO_free(bio_s_out);
                   1281:                bio_s_out = NULL;
                   1282:        }
                   1283:
                   1284:        return (ret);
                   1285: }
                   1286:
                   1287: static void
                   1288: print_stats(BIO * bio, SSL_CTX * ssl_ctx)
                   1289: {
                   1290:        BIO_printf(bio, "%4ld items in the session cache\n",
                   1291:            SSL_CTX_sess_number(ssl_ctx));
                   1292:        BIO_printf(bio, "%4ld client connects (SSL_connect())\n",
                   1293:            SSL_CTX_sess_connect(ssl_ctx));
                   1294:        BIO_printf(bio, "%4ld client renegotiates (SSL_connect())\n",
                   1295:            SSL_CTX_sess_connect_renegotiate(ssl_ctx));
                   1296:        BIO_printf(bio, "%4ld client connects that finished\n",
                   1297:            SSL_CTX_sess_connect_good(ssl_ctx));
                   1298:        BIO_printf(bio, "%4ld server accepts (SSL_accept())\n",
                   1299:            SSL_CTX_sess_accept(ssl_ctx));
                   1300:        BIO_printf(bio, "%4ld server renegotiates (SSL_accept())\n",
                   1301:            SSL_CTX_sess_accept_renegotiate(ssl_ctx));
                   1302:        BIO_printf(bio, "%4ld server accepts that finished\n",
                   1303:            SSL_CTX_sess_accept_good(ssl_ctx));
                   1304:        BIO_printf(bio, "%4ld session cache hits\n", SSL_CTX_sess_hits(ssl_ctx));
                   1305:        BIO_printf(bio, "%4ld session cache misses\n", SSL_CTX_sess_misses(ssl_ctx));
                   1306:        BIO_printf(bio, "%4ld session cache timeouts\n", SSL_CTX_sess_timeouts(ssl_ctx));
                   1307:        BIO_printf(bio, "%4ld callback cache hits\n", SSL_CTX_sess_cb_hits(ssl_ctx));
                   1308:        BIO_printf(bio, "%4ld cache full overflows (%ld allowed)\n",
                   1309:            SSL_CTX_sess_cache_full(ssl_ctx),
                   1310:            SSL_CTX_sess_get_cache_size(ssl_ctx));
                   1311: }
                   1312:
                   1313: static int
                   1314: sv_body(char *hostname, int s, unsigned char *context)
                   1315: {
                   1316:        char *buf = NULL;
1.7       deraadt  1317:        int ret = 1;
1.1       jsing    1318:        int k, i;
                   1319:        unsigned long l;
                   1320:        SSL *con = NULL;
                   1321:        BIO *sbio;
                   1322:        struct timeval timeout;
                   1323:
                   1324:        if ((buf = malloc(bufsize)) == NULL) {
                   1325:                BIO_printf(bio_err, "out of memory\n");
                   1326:                goto err;
                   1327:        }
                   1328:        if (s_nbio) {
                   1329:                if (!s_quiet)
                   1330:                        BIO_printf(bio_err, "turning on non blocking io\n");
1.2       bcook    1331:                if (!BIO_socket_nbio(s, 1))
1.1       jsing    1332:                        ERR_print_errors(bio_err);
                   1333:        }
                   1334:
                   1335:        if (con == NULL) {
                   1336:                con = SSL_new(ctx);
                   1337:                if (s_tlsextdebug) {
                   1338:                        SSL_set_tlsext_debug_callback(con, tlsext_cb);
                   1339:                        SSL_set_tlsext_debug_arg(con, bio_s_out);
                   1340:                }
                   1341:                if (s_tlsextstatus) {
                   1342:                        SSL_CTX_set_tlsext_status_cb(ctx, cert_status_cb);
                   1343:                        tlscstatp.err = bio_err;
                   1344:                        SSL_CTX_set_tlsext_status_arg(ctx, &tlscstatp);
                   1345:                }
                   1346:                if (context)
                   1347:                        SSL_set_session_id_context(con, context,
                   1348:                            strlen((char *) context));
                   1349:        }
                   1350:        SSL_clear(con);
                   1351:
                   1352:        if (SSL_version(con) == DTLS1_VERSION) {
                   1353:
                   1354:                sbio = BIO_new_dgram(s, BIO_NOCLOSE);
                   1355:
                   1356:                if (enable_timeouts) {
                   1357:                        timeout.tv_sec = 0;
                   1358:                        timeout.tv_usec = DGRAM_RCV_TIMEOUT;
                   1359:                        BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
                   1360:
                   1361:                        timeout.tv_sec = 0;
                   1362:                        timeout.tv_usec = DGRAM_SND_TIMEOUT;
                   1363:                        BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
                   1364:                }
                   1365:                if (socket_mtu > 28) {
                   1366:                        SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
                   1367:                        SSL_set_mtu(con, socket_mtu - 28);
                   1368:                } else
                   1369:                        /* want to do MTU discovery */
                   1370:                        BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
                   1371:
                   1372:                /* turn on cookie exchange */
                   1373:                SSL_set_options(con, SSL_OP_COOKIE_EXCHANGE);
                   1374:        } else
                   1375:                sbio = BIO_new_socket(s, BIO_NOCLOSE);
                   1376:
                   1377:        if (s_nbio_test) {
                   1378:                BIO *test;
                   1379:
                   1380:                test = BIO_new(BIO_f_nbio_test());
                   1381:                sbio = BIO_push(test, sbio);
                   1382:        }
                   1383:
                   1384:        SSL_set_bio(con, sbio, sbio);
                   1385:        SSL_set_accept_state(con);
                   1386:        /* SSL_set_fd(con,s); */
                   1387:
                   1388:        if (s_debug) {
                   1389:                SSL_set_debug(con, 1);
                   1390:                BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
                   1391:                BIO_set_callback_arg(SSL_get_rbio(con), (char *) bio_s_out);
                   1392:        }
                   1393:        if (s_msg) {
                   1394:                SSL_set_msg_callback(con, msg_cb);
                   1395:                SSL_set_msg_callback_arg(con, bio_s_out);
                   1396:        }
                   1397:        if (s_tlsextdebug) {
                   1398:                SSL_set_tlsext_debug_callback(con, tlsext_cb);
                   1399:                SSL_set_tlsext_debug_arg(con, bio_s_out);
                   1400:        }
                   1401:
                   1402:        for (;;) {
                   1403:                int read_from_terminal;
                   1404:                int read_from_sslcon;
1.7       deraadt  1405:                struct pollfd pfd[2];
                   1406:                int ptimeout;
1.1       jsing    1407:
                   1408:                read_from_terminal = 0;
                   1409:                read_from_sslcon = SSL_pending(con);
                   1410:
                   1411:                if (!read_from_sslcon) {
1.7       deraadt  1412:                        pfd[0].fd = fileno(stdin);
                   1413:                        pfd[0].events = POLLIN;
                   1414:                        pfd[1].fd = s;
                   1415:                        pfd[1].events = POLLIN;
                   1416:
1.1       jsing    1417:                        if ((SSL_version(con) == DTLS1_VERSION) &&
                   1418:                            DTLSv1_get_timeout(con, &timeout))
1.7       deraadt  1419:                                ptimeout = timeout.tv_sec * 1000 +
                   1420:                                    timeout.tv_usec / 1000;
1.1       jsing    1421:                        else
1.7       deraadt  1422:                                ptimeout = -1;
1.1       jsing    1423:
1.7       deraadt  1424:                        i = poll(pfd, 2, ptimeout);
1.1       jsing    1425:
                   1426:                        if ((SSL_version(con) == DTLS1_VERSION) && DTLSv1_handle_timeout(con) > 0) {
                   1427:                                BIO_printf(bio_err, "TIMEOUT occured\n");
                   1428:                        }
                   1429:                        if (i <= 0)
                   1430:                                continue;
1.7       deraadt  1431:                        if (pfd[0].revents) {
                   1432:                                if ((pfd[0].revents & (POLLERR|POLLNVAL)))
                   1433:                                        continue;
1.1       jsing    1434:                                read_from_terminal = 1;
1.7       deraadt  1435:                        }
                   1436:                        if (pfd[1].revents) {
                   1437:                                if ((pfd[1].revents & (POLLERR|POLLNVAL)))
                   1438:                                        continue;
1.1       jsing    1439:                                read_from_sslcon = 1;
1.7       deraadt  1440:                        }
1.1       jsing    1441:                }
                   1442:                if (read_from_terminal) {
                   1443:                        if (s_crlf) {
                   1444:                                int j, lf_num;
                   1445:
                   1446:                                i = read(fileno(stdin), buf, bufsize / 2);
                   1447:                                lf_num = 0;
                   1448:                                /* both loops are skipped when i <= 0 */
                   1449:                                for (j = 0; j < i; j++)
                   1450:                                        if (buf[j] == '\n')
                   1451:                                                lf_num++;
                   1452:                                for (j = i - 1; j >= 0; j--) {
                   1453:                                        buf[j + lf_num] = buf[j];
                   1454:                                        if (buf[j] == '\n') {
                   1455:                                                lf_num--;
                   1456:                                                i++;
                   1457:                                                buf[j + lf_num] = '\r';
                   1458:                                        }
                   1459:                                }
                   1460:                                assert(lf_num == 0);
                   1461:                        } else
                   1462:                                i = read(fileno(stdin), buf, bufsize);
                   1463:                        if (!s_quiet) {
                   1464:                                if ((i <= 0) || (buf[0] == 'Q')) {
                   1465:                                        BIO_printf(bio_s_out, "DONE\n");
                   1466:                                        shutdown(s, SHUT_RD);
                   1467:                                        close(s);
                   1468:                                        close_accept_socket();
                   1469:                                        ret = -11;
                   1470:                                        goto err;
                   1471:                                }
                   1472:                                if ((i <= 0) || (buf[0] == 'q')) {
                   1473:                                        BIO_printf(bio_s_out, "DONE\n");
                   1474:                                        if (SSL_version(con) != DTLS1_VERSION) {
                   1475:                                                shutdown(s, SHUT_RD);
                   1476:                                                close(s);
                   1477:                                        }
                   1478:                                        /*
                   1479:                                         * close_accept_socket(); ret= -11;
                   1480:                                         */
                   1481:                                        goto err;
                   1482:                                }
                   1483:                                if ((buf[0] == 'r') &&
                   1484:                                    ((buf[1] == '\n') || (buf[1] == '\r'))) {
                   1485:                                        SSL_renegotiate(con);
                   1486:                                        i = SSL_do_handshake(con);
                   1487:                                        printf("SSL_do_handshake -> %d\n", i);
                   1488:                                        i = 0;  /* 13; */
                   1489:                                        continue;
                   1490:                                        /*
                   1491:                                         * strcpy(buf,"server side
                   1492:                                         * RE-NEGOTIATE\n");
                   1493:                                         */
                   1494:                                }
                   1495:                                if ((buf[0] == 'R') &&
                   1496:                                    ((buf[1] == '\n') || (buf[1] == '\r'))) {
                   1497:                                        SSL_set_verify(con,
                   1498:                                            SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, NULL);
                   1499:                                        SSL_renegotiate(con);
                   1500:                                        i = SSL_do_handshake(con);
                   1501:                                        printf("SSL_do_handshake -> %d\n", i);
                   1502:                                        i = 0;  /* 13; */
                   1503:                                        continue;
                   1504:                                        /*
                   1505:                                         * strcpy(buf,"server side
                   1506:                                         * RE-NEGOTIATE asking for client
                   1507:                                         * cert\n");
                   1508:                                         */
                   1509:                                }
                   1510:                                if (buf[0] == 'P') {
                   1511:                                        static const char *str = "Lets print some clear text\n";
                   1512:                                        BIO_write(SSL_get_wbio(con), str, strlen(str));
                   1513:                                }
                   1514:                                if (buf[0] == 'S') {
                   1515:                                        print_stats(bio_s_out, SSL_get_SSL_CTX(con));
                   1516:                                }
                   1517:                        }
                   1518:                        l = k = 0;
                   1519:                        for (;;) {
                   1520:                                /* should do a select for the write */
                   1521: #ifdef RENEG
                   1522:                                {
                   1523:                                        static count = 0;
                   1524:                                        if (++count == 100) {
                   1525:                                                count = 0;
                   1526:                                                SSL_renegotiate(con);
                   1527:                                        }
                   1528:                                }
                   1529: #endif
                   1530:                                k = SSL_write(con, &(buf[l]), (unsigned int) i);
                   1531:                                switch (SSL_get_error(con, k)) {
                   1532:                                case SSL_ERROR_NONE:
                   1533:                                        break;
                   1534:                                case SSL_ERROR_WANT_WRITE:
                   1535:                                case SSL_ERROR_WANT_READ:
                   1536:                                case SSL_ERROR_WANT_X509_LOOKUP:
                   1537:                                        BIO_printf(bio_s_out, "Write BLOCK\n");
                   1538:                                        break;
                   1539:                                case SSL_ERROR_SYSCALL:
                   1540:                                case SSL_ERROR_SSL:
                   1541:                                        BIO_printf(bio_s_out, "ERROR\n");
                   1542:                                        ERR_print_errors(bio_err);
                   1543:                                        ret = 1;
                   1544:                                        goto err;
                   1545:                                        /* break; */
                   1546:                                case SSL_ERROR_ZERO_RETURN:
                   1547:                                        BIO_printf(bio_s_out, "DONE\n");
                   1548:                                        ret = 1;
                   1549:                                        goto err;
                   1550:                                }
                   1551:                                l += k;
                   1552:                                i -= k;
                   1553:                                if (i <= 0)
                   1554:                                        break;
                   1555:                        }
                   1556:                }
                   1557:                if (read_from_sslcon) {
                   1558:                        if (!SSL_is_init_finished(con)) {
                   1559:                                i = init_ssl_connection(con);
                   1560:
                   1561:                                if (i < 0) {
                   1562:                                        ret = 0;
                   1563:                                        goto err;
                   1564:                                } else if (i == 0) {
                   1565:                                        ret = 1;
                   1566:                                        goto err;
                   1567:                                }
                   1568:                        } else {
                   1569:                again:
                   1570:                                i = SSL_read(con, (char *) buf, bufsize);
                   1571:                                switch (SSL_get_error(con, i)) {
                   1572:                                case SSL_ERROR_NONE: {
                   1573:                                                int len, n;
                   1574:                                                for (len = 0; len < i;) {
                   1575:                                                        do {
                   1576:                                                                n = write(fileno(stdout), buf + len, i - len);
                   1577:                                                        } while (n == -1 && errno == EINTR);
                   1578:
                   1579:                                                        if (n < 0) {
                   1580:                                                                BIO_printf(bio_s_out, "ERROR\n");
                   1581:                                                                goto err;
                   1582:                                                        }
                   1583:                                                        len += n;
                   1584:                                                }
                   1585:                                        }
                   1586:                                        if (SSL_pending(con))
                   1587:                                                goto again;
                   1588:                                        break;
                   1589:                                case SSL_ERROR_WANT_WRITE:
                   1590:                                case SSL_ERROR_WANT_READ:
                   1591:                                        BIO_printf(bio_s_out, "Read BLOCK\n");
                   1592:                                        break;
                   1593:                                case SSL_ERROR_SYSCALL:
                   1594:                                case SSL_ERROR_SSL:
                   1595:                                        BIO_printf(bio_s_out, "ERROR\n");
                   1596:                                        ERR_print_errors(bio_err);
                   1597:                                        ret = 1;
                   1598:                                        goto err;
                   1599:                                case SSL_ERROR_ZERO_RETURN:
                   1600:                                        BIO_printf(bio_s_out, "DONE\n");
                   1601:                                        ret = 1;
                   1602:                                        goto err;
                   1603:                                }
                   1604:                        }
                   1605:                }
                   1606:        }
                   1607: err:
                   1608:        if (con != NULL) {
                   1609:                BIO_printf(bio_s_out, "shutting down SSL\n");
                   1610:                SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
                   1611:                SSL_free(con);
                   1612:        }
                   1613:        BIO_printf(bio_s_out, "CONNECTION CLOSED\n");
                   1614:        if (buf != NULL) {
                   1615:                OPENSSL_cleanse(buf, bufsize);
                   1616:                free(buf);
                   1617:        }
                   1618:        if (ret >= 0)
                   1619:                BIO_printf(bio_s_out, "ACCEPT\n");
                   1620:        return (ret);
                   1621: }
                   1622:
                   1623: static void
                   1624: close_accept_socket(void)
                   1625: {
                   1626:        BIO_printf(bio_err, "shutdown accept socket\n");
                   1627:        if (accept_socket >= 0) {
                   1628:                shutdown(accept_socket, SHUT_RDWR);
                   1629:                close(accept_socket);
                   1630:        }
                   1631: }
                   1632:
                   1633: static int
                   1634: init_ssl_connection(SSL * con)
                   1635: {
                   1636:        int i;
                   1637:        const char *str;
                   1638:        X509 *peer;
                   1639:        long verify_error;
                   1640:        char buf[BUFSIZ];
                   1641:        const unsigned char *next_proto_neg;
                   1642:        unsigned next_proto_neg_len;
                   1643:        unsigned char *exportedkeymat;
                   1644:
                   1645:        i = SSL_accept(con);
                   1646:        if (i <= 0) {
                   1647:                if (BIO_sock_should_retry(i)) {
                   1648:                        BIO_printf(bio_s_out, "DELAY\n");
                   1649:                        return (1);
                   1650:                }
                   1651:                BIO_printf(bio_err, "ERROR\n");
                   1652:                verify_error = SSL_get_verify_result(con);
                   1653:                if (verify_error != X509_V_OK) {
                   1654:                        BIO_printf(bio_err, "verify error:%s\n",
                   1655:                            X509_verify_cert_error_string(verify_error));
                   1656:                } else
                   1657:                        ERR_print_errors(bio_err);
                   1658:                return (0);
                   1659:        }
                   1660:        PEM_write_bio_SSL_SESSION(bio_s_out, SSL_get_session(con));
                   1661:
                   1662:        peer = SSL_get_peer_certificate(con);
                   1663:        if (peer != NULL) {
                   1664:                BIO_printf(bio_s_out, "Client certificate\n");
                   1665:                PEM_write_bio_X509(bio_s_out, peer);
                   1666:                X509_NAME_oneline(X509_get_subject_name(peer), buf, sizeof buf);
                   1667:                BIO_printf(bio_s_out, "subject=%s\n", buf);
                   1668:                X509_NAME_oneline(X509_get_issuer_name(peer), buf, sizeof buf);
                   1669:                BIO_printf(bio_s_out, "issuer=%s\n", buf);
                   1670:                X509_free(peer);
                   1671:        }
                   1672:        if (SSL_get_shared_ciphers(con, buf, sizeof buf) != NULL)
                   1673:                BIO_printf(bio_s_out, "Shared ciphers:%s\n", buf);
                   1674:        str = SSL_CIPHER_get_name(SSL_get_current_cipher(con));
                   1675:        BIO_printf(bio_s_out, "CIPHER is %s\n", (str != NULL) ? str : "(NONE)");
                   1676:
                   1677:        SSL_get0_next_proto_negotiated(con, &next_proto_neg, &next_proto_neg_len);
                   1678:        if (next_proto_neg) {
                   1679:                BIO_printf(bio_s_out, "NEXTPROTO is ");
                   1680:                BIO_write(bio_s_out, next_proto_neg, next_proto_neg_len);
                   1681:                BIO_printf(bio_s_out, "\n");
                   1682:        }
                   1683: #ifndef OPENSSL_NO_SRTP
                   1684:        {
                   1685:                SRTP_PROTECTION_PROFILE *srtp_profile
                   1686:                = SSL_get_selected_srtp_profile(con);
                   1687:
                   1688:                if (srtp_profile)
                   1689:                        BIO_printf(bio_s_out, "SRTP Extension negotiated, profile=%s\n",
                   1690:                            srtp_profile->name);
                   1691:        }
                   1692: #endif
                   1693:        if (SSL_cache_hit(con))
                   1694:                BIO_printf(bio_s_out, "Reused session-id\n");
                   1695:        if (SSL_ctrl(con, SSL_CTRL_GET_FLAGS, 0, NULL) &
                   1696:            TLS1_FLAGS_TLS_PADDING_BUG)
                   1697:                BIO_printf(bio_s_out,
                   1698:                    "Peer has incorrect TLSv1 block padding\n");
                   1699:        BIO_printf(bio_s_out, "Secure Renegotiation IS%s supported\n",
                   1700:            SSL_get_secure_renegotiation_support(con) ? "" : " NOT");
                   1701:        if (keymatexportlabel != NULL) {
                   1702:                BIO_printf(bio_s_out, "Keying material exporter:\n");
                   1703:                BIO_printf(bio_s_out, "    Label: '%s'\n", keymatexportlabel);
                   1704:                BIO_printf(bio_s_out, "    Length: %i bytes\n",
                   1705:                    keymatexportlen);
                   1706:                exportedkeymat = malloc(keymatexportlen);
                   1707:                if (exportedkeymat != NULL) {
                   1708:                        if (!SSL_export_keying_material(con, exportedkeymat,
                   1709:                                keymatexportlen,
                   1710:                                keymatexportlabel,
                   1711:                                strlen(keymatexportlabel),
                   1712:                                NULL, 0, 0)) {
                   1713:                                BIO_printf(bio_s_out, "    Error\n");
                   1714:                        } else {
                   1715:                                BIO_printf(bio_s_out, "    Keying material: ");
                   1716:                                for (i = 0; i < keymatexportlen; i++)
                   1717:                                        BIO_printf(bio_s_out, "%02X",
                   1718:                                            exportedkeymat[i]);
                   1719:                                BIO_printf(bio_s_out, "\n");
                   1720:                        }
                   1721:                        free(exportedkeymat);
                   1722:                }
                   1723:        }
                   1724:        return (1);
                   1725: }
                   1726:
                   1727: #ifndef OPENSSL_NO_DH
                   1728: static DH *
                   1729: load_dh_param(const char *dhfile)
                   1730: {
                   1731:        DH *ret = NULL;
                   1732:        BIO *bio;
                   1733:
                   1734:        if ((bio = BIO_new_file(dhfile, "r")) == NULL)
                   1735:                goto err;
                   1736:        ret = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
                   1737: err:
                   1738:        BIO_free(bio);
                   1739:        return (ret);
                   1740: }
                   1741: #endif
                   1742:
                   1743: static int
                   1744: www_body(char *hostname, int s, unsigned char *context)
                   1745: {
                   1746:        char *buf = NULL;
                   1747:        int ret = 1;
                   1748:        int i, j, k, dot;
                   1749:        SSL *con;
                   1750:        const SSL_CIPHER *c;
                   1751:        BIO *io, *ssl_bio, *sbio;
                   1752:
                   1753:        buf = malloc(bufsize);
                   1754:        if (buf == NULL)
                   1755:                return (0);
                   1756:        io = BIO_new(BIO_f_buffer());
                   1757:        ssl_bio = BIO_new(BIO_f_ssl());
                   1758:        if ((io == NULL) || (ssl_bio == NULL))
                   1759:                goto err;
                   1760:
                   1761:        if (s_nbio) {
                   1762:                if (!s_quiet)
                   1763:                        BIO_printf(bio_err, "turning on non blocking io\n");
1.2       bcook    1764:                if (!BIO_socket_nbio(s, 1))
1.1       jsing    1765:                        ERR_print_errors(bio_err);
                   1766:        }
                   1767:
                   1768:        /* lets make the output buffer a reasonable size */
                   1769:        if (!BIO_set_write_buffer_size(io, bufsize))
                   1770:                goto err;
                   1771:
                   1772:        if ((con = SSL_new(ctx)) == NULL)
                   1773:                goto err;
                   1774:        if (s_tlsextdebug) {
                   1775:                SSL_set_tlsext_debug_callback(con, tlsext_cb);
                   1776:                SSL_set_tlsext_debug_arg(con, bio_s_out);
                   1777:        }
                   1778:        if (context)
                   1779:                SSL_set_session_id_context(con, context,
                   1780:                    strlen((char *) context));
                   1781:
                   1782:        sbio = BIO_new_socket(s, BIO_NOCLOSE);
                   1783:        if (s_nbio_test) {
                   1784:                BIO *test;
                   1785:
                   1786:                test = BIO_new(BIO_f_nbio_test());
                   1787:                sbio = BIO_push(test, sbio);
                   1788:        }
                   1789:        SSL_set_bio(con, sbio, sbio);
                   1790:        SSL_set_accept_state(con);
                   1791:
                   1792:        /* SSL_set_fd(con,s); */
                   1793:        BIO_set_ssl(ssl_bio, con, BIO_CLOSE);
                   1794:        BIO_push(io, ssl_bio);
                   1795:
                   1796:        if (s_debug) {
                   1797:                SSL_set_debug(con, 1);
                   1798:                BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
                   1799:                BIO_set_callback_arg(SSL_get_rbio(con), (char *) bio_s_out);
                   1800:        }
                   1801:        if (s_msg) {
                   1802:                SSL_set_msg_callback(con, msg_cb);
                   1803:                SSL_set_msg_callback_arg(con, bio_s_out);
                   1804:        }
                   1805:        for (;;) {
                   1806:                i = BIO_gets(io, buf, bufsize - 1);
                   1807:                if (i < 0) {    /* error */
                   1808:                        if (!BIO_should_retry(io)) {
                   1809:                                if (!s_quiet)
                   1810:                                        ERR_print_errors(bio_err);
                   1811:                                goto err;
                   1812:                        } else {
                   1813:                                BIO_printf(bio_s_out, "read R BLOCK\n");
                   1814:                                sleep(1);
                   1815:                                continue;
                   1816:                        }
                   1817:                } else if (i == 0) {    /* end of input */
                   1818:                        ret = 1;
                   1819:                        goto end;
                   1820:                }
                   1821:                /* else we have data */
                   1822:                if (((www == 1) && (strncmp("GET ", buf, 4) == 0)) ||
                   1823:                    ((www == 2) && (strncmp("GET /stats ", buf, 11) == 0))) {
                   1824:                        char *p;
                   1825:                        X509 *peer;
                   1826:                        STACK_OF(SSL_CIPHER) * sk;
                   1827:                        static const char *space = "                          ";
                   1828:
                   1829:                        BIO_puts(io, "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
                   1830:                        BIO_puts(io, "<HTML><BODY BGCOLOR=\"#ffffff\">\n");
                   1831:                        BIO_puts(io, "<pre>\n");
                   1832: /*                     BIO_puts(io,SSLeay_version(SSLEAY_VERSION));*/
                   1833:                        BIO_puts(io, "\n");
                   1834:                        for (i = 0; i < local_argc; i++) {
                   1835:                                BIO_puts(io, local_argv[i]);
                   1836:                                BIO_write(io, " ", 1);
                   1837:                        }
                   1838:                        BIO_puts(io, "\n");
                   1839:
                   1840:                        BIO_printf(io,
                   1841:                            "Secure Renegotiation IS%s supported\n",
                   1842:                            SSL_get_secure_renegotiation_support(con) ?
                   1843:                            "" : " NOT");
                   1844:
                   1845:                        /*
                   1846:                         * The following is evil and should not really be
                   1847:                         * done
                   1848:                         */
                   1849:                        BIO_printf(io, "Ciphers supported in s_server binary\n");
                   1850:                        sk = SSL_get_ciphers(con);
                   1851:                        j = sk_SSL_CIPHER_num(sk);
                   1852:                        for (i = 0; i < j; i++) {
                   1853:                                c = sk_SSL_CIPHER_value(sk, i);
                   1854:                                BIO_printf(io, "%-11s:%-25s",
                   1855:                                    SSL_CIPHER_get_version(c),
                   1856:                                    SSL_CIPHER_get_name(c));
                   1857:                                if ((((i + 1) % 2) == 0) && (i + 1 != j))
                   1858:                                        BIO_puts(io, "\n");
                   1859:                        }
                   1860:                        BIO_puts(io, "\n");
                   1861:                        p = SSL_get_shared_ciphers(con, buf, bufsize);
                   1862:                        if (p != NULL) {
                   1863:                                BIO_printf(io, "---\nCiphers common between both SSL end points:\n");
                   1864:                                j = i = 0;
                   1865:                                while (*p) {
                   1866:                                        if (*p == ':') {
                   1867:                                                BIO_write(io, space, 26 - j);
                   1868:                                                i++;
                   1869:                                                j = 0;
                   1870:                                                BIO_write(io, ((i % 3) ? " " : "\n"), 1);
                   1871:                                        } else {
                   1872:                                                BIO_write(io, p, 1);
                   1873:                                                j++;
                   1874:                                        }
                   1875:                                        p++;
                   1876:                                }
                   1877:                                BIO_puts(io, "\n");
                   1878:                        }
                   1879:                        BIO_printf(io, (SSL_cache_hit(con)
                   1880:                                ? "---\nReused, "
                   1881:                                : "---\nNew, "));
                   1882:                        c = SSL_get_current_cipher(con);
                   1883:                        BIO_printf(io, "%s, Cipher is %s\n",
                   1884:                            SSL_CIPHER_get_version(c),
                   1885:                            SSL_CIPHER_get_name(c));
                   1886:                        SSL_SESSION_print(io, SSL_get_session(con));
                   1887:                        BIO_printf(io, "---\n");
                   1888:                        print_stats(io, SSL_get_SSL_CTX(con));
                   1889:                        BIO_printf(io, "---\n");
                   1890:                        peer = SSL_get_peer_certificate(con);
                   1891:                        if (peer != NULL) {
                   1892:                                BIO_printf(io, "Client certificate\n");
                   1893:                                X509_print(io, peer);
                   1894:                                PEM_write_bio_X509(io, peer);
                   1895:                        } else
                   1896:                                BIO_puts(io, "no client certificate available\n");
                   1897:                        BIO_puts(io, "</BODY></HTML>\r\n\r\n");
                   1898:                        break;
                   1899:                } else if ((www == 2 || www == 3)
                   1900:                    && (strncmp("GET /", buf, 5) == 0)) {
                   1901:                        BIO *file;
                   1902:                        char *p, *e;
                   1903:                        static const char *text = "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
                   1904:
                   1905:                        /* skip the '/' */
                   1906:                        p = &(buf[5]);
                   1907:
                   1908:                        dot = 1;
                   1909:                        for (e = p; *e != '\0'; e++) {
                   1910:                                if (e[0] == ' ')
                   1911:                                        break;
                   1912:
                   1913:                                switch (dot) {
                   1914:                                case 1:
                   1915:                                        dot = (e[0] == '.') ? 2 : 0;
                   1916:                                        break;
                   1917:                                case 2:
                   1918:                                        dot = (e[0] == '.') ? 3 : 0;
                   1919:                                        break;
                   1920:                                case 3:
                   1921:                                        dot = (e[0] == '/') ? -1 : 0;
                   1922:                                        break;
                   1923:                                }
                   1924:                                if (dot == 0)
                   1925:                                        dot = (e[0] == '/') ? 1 : 0;
                   1926:                        }
                   1927:                        dot = (dot == 3) || (dot == -1);        /* filename contains
                   1928:                                                                 * ".." component */
                   1929:
                   1930:                        if (*e == '\0') {
                   1931:                                BIO_puts(io, text);
                   1932:                                BIO_printf(io, "'%s' is an invalid file name\r\n", p);
                   1933:                                break;
                   1934:                        }
                   1935:                        *e = '\0';
                   1936:
                   1937:                        if (dot) {
                   1938:                                BIO_puts(io, text);
                   1939:                                BIO_printf(io, "'%s' contains '..' reference\r\n", p);
                   1940:                                break;
                   1941:                        }
                   1942:                        if (*p == '/') {
                   1943:                                BIO_puts(io, text);
                   1944:                                BIO_printf(io, "'%s' is an invalid path\r\n", p);
                   1945:                                break;
                   1946:                        }
                   1947:                        /* if a directory, do the index thang */
                   1948:                        if (app_isdir(p) > 0) {
                   1949:                                BIO_puts(io, text);
                   1950:                                BIO_printf(io, "'%s' is a directory\r\n", p);
                   1951:                                break;
                   1952:                        }
                   1953:                        if ((file = BIO_new_file(p, "r")) == NULL) {
                   1954:                                BIO_puts(io, text);
                   1955:                                BIO_printf(io, "Error opening '%s'\r\n", p);
                   1956:                                ERR_print_errors(io);
                   1957:                                break;
                   1958:                        }
                   1959:                        if (!s_quiet)
                   1960:                                BIO_printf(bio_err, "FILE:%s\n", p);
                   1961:
                   1962:                        if (www == 2) {
                   1963:                                i = strlen(p);
                   1964:                                if (((i > 5) && (strcmp(&(p[i - 5]), ".html") == 0)) ||
                   1965:                                    ((i > 4) && (strcmp(&(p[i - 4]), ".php") == 0)) ||
                   1966:                                    ((i > 4) && (strcmp(&(p[i - 4]), ".htm") == 0)))
                   1967:                                        BIO_puts(io, "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
                   1968:                                else
                   1969:                                        BIO_puts(io, "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
                   1970:                        }
                   1971:                        /* send the file */
                   1972:                        for (;;) {
                   1973:                                i = BIO_read(file, buf, bufsize);
                   1974:                                if (i <= 0)
                   1975:                                        break;
                   1976:
                   1977: #ifdef RENEG
                   1978:                                total_bytes += i;
                   1979:                                fprintf(stderr, "%d\n", i);
                   1980:                                if (total_bytes > 3 * 1024) {
                   1981:                                        total_bytes = 0;
                   1982:                                        fprintf(stderr, "RENEGOTIATE\n");
                   1983:                                        SSL_renegotiate(con);
                   1984:                                }
                   1985: #endif
                   1986:
                   1987:                                for (j = 0; j < i;) {
                   1988: #ifdef RENEG
                   1989:                                        {
                   1990:                                                static count = 0;
                   1991:                                                if (++count == 13) {
                   1992:                                                        SSL_renegotiate(con);
                   1993:                                                }
                   1994:                                        }
                   1995: #endif
                   1996:                                        k = BIO_write(io, &(buf[j]), i - j);
                   1997:                                        if (k <= 0) {
                   1998:                                                if (!BIO_should_retry(io))
                   1999:                                                        goto write_error;
                   2000:                                                else {
                   2001:                                                        BIO_printf(bio_s_out, "rwrite W BLOCK\n");
                   2002:                                                }
                   2003:                                        } else {
                   2004:                                                j += k;
                   2005:                                        }
                   2006:                                }
                   2007:                        }
                   2008:        write_error:
                   2009:                        BIO_free(file);
                   2010:                        break;
                   2011:                }
                   2012:        }
                   2013:
                   2014:        for (;;) {
                   2015:                i = (int) BIO_flush(io);
                   2016:                if (i <= 0) {
                   2017:                        if (!BIO_should_retry(io))
                   2018:                                break;
                   2019:                } else
                   2020:                        break;
                   2021:        }
                   2022: end:
                   2023:        /* make sure we re-use sessions */
                   2024:        SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
                   2025:
                   2026: err:
                   2027:
                   2028:        if (ret >= 0)
                   2029:                BIO_printf(bio_s_out, "ACCEPT\n");
                   2030:
                   2031:        if (buf != NULL)
                   2032:                free(buf);
                   2033:        if (io != NULL)
                   2034:                BIO_free_all(io);
                   2035: /*     if (ssl_bio != NULL) BIO_free(ssl_bio);*/
                   2036:        return (ret);
                   2037: }
                   2038:
                   2039: #define MAX_SESSION_ID_ATTEMPTS 10
                   2040: static int
                   2041: generate_session_id(const SSL * ssl, unsigned char *id,
                   2042:     unsigned int *id_len)
                   2043: {
                   2044:        unsigned int count = 0;
                   2045:        do {
1.3       jsing    2046:                arc4random_buf(id, *id_len);
1.1       jsing    2047:                /*
                   2048:                 * Prefix the session_id with the required prefix. NB: If our
                   2049:                 * prefix is too long, clip it - but there will be worse
                   2050:                 * effects anyway, eg. the server could only possibly create
                   2051:                 * 1 session ID (ie. the prefix!) so all future session
                   2052:                 * negotiations will fail due to conflicts.
                   2053:                 */
                   2054:                memcpy(id, session_id_prefix,
                   2055:                    (strlen(session_id_prefix) < *id_len) ?
                   2056:                    strlen(session_id_prefix) : *id_len);
                   2057:        }
                   2058:        while (SSL_has_matching_session_id(ssl, id, *id_len) &&
                   2059:            (++count < MAX_SESSION_ID_ATTEMPTS));
                   2060:        if (count >= MAX_SESSION_ID_ATTEMPTS)
                   2061:                return 0;
                   2062:        return 1;
                   2063: }