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

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