sl@0: /* apps/s_server.c */
sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
sl@0:  * All rights reserved.
sl@0:  *
sl@0:  * This package is an SSL implementation written
sl@0:  * by Eric Young (eay@cryptsoft.com).
sl@0:  * The implementation was written so as to conform with Netscapes SSL.
sl@0:  * 
sl@0:  * This library is free for commercial and non-commercial use as long as
sl@0:  * the following conditions are aheared to.  The following conditions
sl@0:  * apply to all code found in this distribution, be it the RC4, RSA,
sl@0:  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
sl@0:  * included with this distribution is covered by the same copyright terms
sl@0:  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
sl@0:  * 
sl@0:  * Copyright remains Eric Young's, and as such any Copyright notices in
sl@0:  * the code are not to be removed.
sl@0:  * If this package is used in a product, Eric Young should be given attribution
sl@0:  * as the author of the parts of the library used.
sl@0:  * This can be in the form of a textual message at program startup or
sl@0:  * in documentation (online or textual) provided with the package.
sl@0:  * 
sl@0:  * Redistribution and use in source and binary forms, with or without
sl@0:  * modification, are permitted provided that the following conditions
sl@0:  * are met:
sl@0:  * 1. Redistributions of source code must retain the copyright
sl@0:  *    notice, this list of conditions and the following disclaimer.
sl@0:  * 2. Redistributions in binary form must reproduce the above copyright
sl@0:  *    notice, this list of conditions and the following disclaimer in the
sl@0:  *    documentation and/or other materials provided with the distribution.
sl@0:  * 3. All advertising materials mentioning features or use of this software
sl@0:  *    must display the following acknowledgement:
sl@0:  *    "This product includes cryptographic software written by
sl@0:  *     Eric Young (eay@cryptsoft.com)"
sl@0:  *    The word 'cryptographic' can be left out if the rouines from the library
sl@0:  *    being used are not cryptographic related :-).
sl@0:  * 4. If you include any Windows specific code (or a derivative thereof) from 
sl@0:  *    the apps directory (application code) you must include an acknowledgement:
sl@0:  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
sl@0:  * 
sl@0:  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
sl@0:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
sl@0:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0:  * SUCH DAMAGE.
sl@0:  * 
sl@0:  * The licence and distribution terms for any publically available version or
sl@0:  * derivative of this code cannot be changed.  i.e. this code cannot simply be
sl@0:  * copied and put under another distribution licence
sl@0:  * [including the GNU Public Licence.]
sl@0:  */
sl@0: /* ====================================================================
sl@0:  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
sl@0:  *
sl@0:  * Redistribution and use in source and binary forms, with or without
sl@0:  * modification, are permitted provided that the following conditions
sl@0:  * are met:
sl@0:  *
sl@0:  * 1. Redistributions of source code must retain the above copyright
sl@0:  *    notice, this list of conditions and the following disclaimer. 
sl@0:  *
sl@0:  * 2. Redistributions in binary form must reproduce the above copyright
sl@0:  *    notice, this list of conditions and the following disclaimer in
sl@0:  *    the documentation and/or other materials provided with the
sl@0:  *    distribution.
sl@0:  *
sl@0:  * 3. All advertising materials mentioning features or use of this
sl@0:  *    software must display the following acknowledgment:
sl@0:  *    "This product includes software developed by the OpenSSL Project
sl@0:  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
sl@0:  *
sl@0:  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
sl@0:  *    endorse or promote products derived from this software without
sl@0:  *    prior written permission. For written permission, please contact
sl@0:  *    openssl-core@openssl.org.
sl@0:  *
sl@0:  * 5. Products derived from this software may not be called "OpenSSL"
sl@0:  *    nor may "OpenSSL" appear in their names without prior written
sl@0:  *    permission of the OpenSSL Project.
sl@0:  *
sl@0:  * 6. Redistributions of any form whatsoever must retain the following
sl@0:  *    acknowledgment:
sl@0:  *    "This product includes software developed by the OpenSSL Project
sl@0:  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
sl@0:  *
sl@0:  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
sl@0:  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
sl@0:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
sl@0:  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
sl@0:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
sl@0:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
sl@0:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
sl@0:  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
sl@0:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
sl@0:  * OF THE POSSIBILITY OF SUCH DAMAGE.
sl@0:  * ====================================================================
sl@0:  *
sl@0:  * This product includes cryptographic software written by Eric Young
sl@0:  * (eay@cryptsoft.com).  This product includes software written by Tim
sl@0:  * Hudson (tjh@cryptsoft.com).
sl@0:  *
sl@0:  */
sl@0: /* ====================================================================
sl@0:  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
sl@0:  * ECC cipher suite support in OpenSSL originally developed by 
sl@0:  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
sl@0:  */
sl@0: 
sl@0: /* Until the key-gen callbacks are modified to use newer prototypes, we allow
sl@0:  * deprecated functions for openssl-internal code */
sl@0: #ifdef OPENSSL_NO_DEPRECATED
sl@0: #undef OPENSSL_NO_DEPRECATED
sl@0: #endif
sl@0: 
sl@0: #include <assert.h>
sl@0: #include <stdio.h>
sl@0: #include <stdlib.h>
sl@0: #include <string.h>
sl@0: #include <sys/select.h>
sl@0: #include <sys/stat.h>
sl@0: #include <openssl/e_os2.h>
sl@0: #ifdef OPENSSL_NO_STDIO
sl@0: #define APPS_WIN16
sl@0: #endif
sl@0: 
sl@0: #if !defined(OPENSSL_SYS_NETWARE)  /* conflicts with winsock2 stuff on netware */
sl@0: #include <sys/types.h>
sl@0: #endif
sl@0: 
sl@0: /* With IPv6, it looks like Digital has mixed up the proper order of
sl@0:    recursive header file inclusion, resulting in the compiler complaining
sl@0:    that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which
sl@0:    is needed to have fileno() declared correctly...  So let's define u_int */
sl@0: #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
sl@0: #define __U_INT
sl@0: typedef unsigned int u_int;
sl@0: #endif
sl@0: 
sl@0: #include <openssl/lhash.h>
sl@0: #include <openssl/bn.h>
sl@0: #define USE_SOCKETS
sl@0: #include "apps.h"
sl@0: #include <openssl/err.h>
sl@0: #include <openssl/pem.h>
sl@0: #include <openssl/x509.h>
sl@0: #include <openssl/ssl.h>
sl@0: #include <openssl/rand.h>
sl@0: #ifndef OPENSSL_NO_DH
sl@0: #include <openssl/dh.h>
sl@0: #endif
sl@0: #ifndef OPENSSL_NO_RSA
sl@0: #include <openssl/rsa.h>
sl@0: #endif
sl@0: #include "s_apps.h"
sl@0: #include "timeouts.h"
sl@0: 
sl@0: #ifdef OPENSSL_SYS_WINCE
sl@0: /* Windows CE incorrectly defines fileno as returning void*, so to avoid problems below... */
sl@0: #ifdef fileno
sl@0: #undef fileno
sl@0: #endif
sl@0: #define fileno(a) (int)_fileno(a)
sl@0: #endif
sl@0: 
sl@0: #if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
sl@0: /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
sl@0: #undef FIONBIO
sl@0: #endif
sl@0: 
sl@0: #ifndef OPENSSL_NO_RSA
sl@0: static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength);
sl@0: #endif
sl@0: static int sv_body(char *hostname, int s, unsigned char *context);
sl@0: static int www_body(char *hostname, int s, unsigned char *context);
sl@0: static void close_accept_socket(void );
sl@0: static void sv_usage(void);
sl@0: static int init_ssl_connection(SSL *s);
sl@0: static void print_stats(BIO *bp,SSL_CTX *ctx);
sl@0: static int generate_session_id(const SSL *ssl, unsigned char *id,
sl@0: 				unsigned int *id_len);
sl@0: #ifndef OPENSSL_NO_DH
sl@0: static DH *load_dh_param(const char *dhfile);
sl@0: static DH *get_dh512(void);
sl@0: #endif
sl@0: 
sl@0: 
sl@0: #ifdef MONOLITH
sl@0: static void s_server_init(void);
sl@0: #endif
sl@0: 
sl@0: #ifndef S_ISDIR
sl@0: # if defined(_S_IFMT) && defined(_S_IFDIR)
sl@0: #  define S_ISDIR(a)	(((a) & _S_IFMT) == _S_IFDIR)
sl@0: # else
sl@0: #  define S_ISDIR(a)	(((a) & S_IFMT) == S_IFDIR)
sl@0: # endif
sl@0: #endif
sl@0: 
sl@0: #ifndef OPENSSL_NO_DH
sl@0: static unsigned char dh512_p[]={
sl@0: 	0xDA,0x58,0x3C,0x16,0xD9,0x85,0x22,0x89,0xD0,0xE4,0xAF,0x75,
sl@0: 	0x6F,0x4C,0xCA,0x92,0xDD,0x4B,0xE5,0x33,0xB8,0x04,0xFB,0x0F,
sl@0: 	0xED,0x94,0xEF,0x9C,0x8A,0x44,0x03,0xED,0x57,0x46,0x50,0xD3,
sl@0: 	0x69,0x99,0xDB,0x29,0xD7,0x76,0x27,0x6B,0xA2,0xD3,0xD4,0x12,
sl@0: 	0xE2,0x18,0xF4,0xDD,0x1E,0x08,0x4C,0xF6,0xD8,0x00,0x3E,0x7C,
sl@0: 	0x47,0x74,0xE8,0x33,
sl@0: 	};
sl@0: static unsigned char dh512_g[]={
sl@0: 	0x02,
sl@0: 	};
sl@0: 
sl@0: static DH *get_dh512(void)
sl@0: 	{
sl@0: 	DH *dh=NULL;
sl@0: 
sl@0: 	if ((dh=DH_new()) == NULL) return(NULL);
sl@0: 	dh->p=BN_bin2bn(dh512_p,sizeof(dh512_p),NULL);
sl@0: 	dh->g=BN_bin2bn(dh512_g,sizeof(dh512_g),NULL);
sl@0: 	if ((dh->p == NULL) || (dh->g == NULL))
sl@0: 		return(NULL);
sl@0: 	return(dh);
sl@0: 	}
sl@0: #endif
sl@0: 
sl@0: 
sl@0: /* static int load_CA(SSL_CTX *ctx, char *file);*/
sl@0: 
sl@0: #undef BUFSIZZ
sl@0: #define BUFSIZZ	16*1024
sl@0: static int bufsize=BUFSIZZ;
sl@0: static int accept_socket= -1;
sl@0: 
sl@0: #define TEST_CERT	"server.pem"
sl@0: #undef PROG
sl@0: #define PROG		s_server_main
sl@0: 
sl@0: extern int verify_depth;
sl@0: 
sl@0: static char *cipher=NULL;
sl@0: static int s_server_verify=SSL_VERIFY_NONE;
sl@0: static int s_server_session_id_context = 1; /* anything will do */
sl@0: static const char *s_cert_file=TEST_CERT,*s_key_file=NULL;
sl@0: static char *s_dcert_file=NULL,*s_dkey_file=NULL;
sl@0: #ifdef FIONBIO
sl@0: static int s_nbio=0;
sl@0: #endif
sl@0: static int s_nbio_test=0;
sl@0: int s_crlf=0;
sl@0: static SSL_CTX *ctx=NULL;
sl@0: static int www=0;
sl@0: 
sl@0: static BIO *bio_s_out=NULL;
sl@0: static int s_debug=0;
sl@0: static int s_msg=0;
sl@0: static int s_quiet=0;
sl@0: 
sl@0: static int hack=0;
sl@0: #ifndef OPENSSL_NO_ENGINE
sl@0: static char *engine_id=NULL;
sl@0: #endif
sl@0: static const char *session_id_prefix=NULL;
sl@0: 
sl@0: static int enable_timeouts = 0;
sl@0: #ifdef mtu
sl@0: #undef mtu
sl@0: #endif
sl@0: static long mtu;
sl@0: static int cert_chain = 0;
sl@0: 
sl@0: 
sl@0: #ifdef MONOLITH
sl@0: static void s_server_init(void)
sl@0: 	{
sl@0: 	accept_socket=-1;
sl@0: 	cipher=NULL;
sl@0: 	s_server_verify=SSL_VERIFY_NONE;
sl@0: 	s_dcert_file=NULL;
sl@0: 	s_dkey_file=NULL;
sl@0: 	s_cert_file=TEST_CERT;
sl@0: 	s_key_file=NULL;
sl@0: #ifdef FIONBIO
sl@0: 	s_nbio=0;
sl@0: #endif
sl@0: 	s_nbio_test=0;
sl@0: 	ctx=NULL;
sl@0: 	www=0;
sl@0: 
sl@0: 	bio_s_out=NULL;
sl@0: 	s_debug=0;
sl@0: 	s_msg=0;
sl@0: 	s_quiet=0;
sl@0: 	hack=0;
sl@0: #ifndef OPENSSL_NO_ENGINE
sl@0: 	engine_id=NULL;
sl@0: #endif
sl@0: 	}
sl@0: #endif
sl@0: 
sl@0: static void sv_usage(void)
sl@0: 	{
sl@0: 	BIO_printf(bio_err,"usage: s_server [args ...]\n");
sl@0: 	BIO_printf(bio_err,"\n");
sl@0: 	BIO_printf(bio_err," -accept arg   - port to accept on (default is %d)\n",PORT);
sl@0: 	BIO_printf(bio_err," -context arg  - set session ID context\n");
sl@0: 	BIO_printf(bio_err," -verify arg   - turn on peer certificate verification\n");
sl@0: 	BIO_printf(bio_err," -Verify arg   - turn on peer certificate verification, must have a cert.\n");
sl@0: 	BIO_printf(bio_err," -cert arg     - certificate file to use\n");
sl@0: 	BIO_printf(bio_err,"                 (default is %s)\n",TEST_CERT);
sl@0: 	BIO_printf(bio_err," -certform arg - certificate format (PEM or DER) PEM default\n");
sl@0: 	BIO_printf(bio_err," -key arg      - Private Key file to use, in cert file if\n");
sl@0: 	BIO_printf(bio_err,"                 not specified (default is %s)\n",TEST_CERT);
sl@0: 	BIO_printf(bio_err," -keyform arg  - key format (PEM, DER or ENGINE) PEM default\n");
sl@0: 	BIO_printf(bio_err," -pass arg     - private key file pass phrase source\n");
sl@0: 	BIO_printf(bio_err," -dcert arg    - second certificate file to use (usually for DSA)\n");
sl@0: 	BIO_printf(bio_err," -dcertform x  - second certificate format (PEM or DER) PEM default\n");
sl@0: 	BIO_printf(bio_err," -dkey arg     - second private key file to use (usually for DSA)\n");
sl@0: 	BIO_printf(bio_err," -dkeyform arg - second key format (PEM, DER or ENGINE) PEM default\n");
sl@0: 	BIO_printf(bio_err," -dpass arg    - second private key file pass phrase source\n");
sl@0: 	BIO_printf(bio_err," -dhparam arg  - DH parameter file to use, in cert file if not specified\n");
sl@0: 	BIO_printf(bio_err,"                 or a default set of parameters is used\n");
sl@0: #ifndef OPENSSL_NO_ECDH
sl@0: 	BIO_printf(bio_err," -named_curve arg  - Elliptic curve name to use for ephemeral ECDH keys.\n" \
sl@0: 	                   "                 Use \"openssl ecparam -list_curves\" for all names\n" \
sl@0: 	                   "                 (default is sect163r2).\n");
sl@0: #endif
sl@0: #ifdef FIONBIO
sl@0: 	BIO_printf(bio_err," -nbio         - Run with non-blocking IO\n");
sl@0: #endif
sl@0: 	BIO_printf(bio_err," -nbio_test    - test with the non-blocking test bio\n");
sl@0: 	BIO_printf(bio_err," -crlf         - convert LF from terminal into CRLF\n");
sl@0: 	BIO_printf(bio_err," -debug        - Print more output\n");
sl@0: 	BIO_printf(bio_err," -msg          - Show protocol messages\n");
sl@0: 	BIO_printf(bio_err," -state        - Print the SSL states\n");
sl@0: 	BIO_printf(bio_err," -CApath arg   - PEM format directory of CA's\n");
sl@0: 	BIO_printf(bio_err," -CAfile arg   - PEM format file of CA's\n");
sl@0: 	BIO_printf(bio_err," -nocert       - Don't use any certificates (Anon-DH)\n");
sl@0: 	BIO_printf(bio_err," -cipher arg   - play with 'openssl ciphers' to see what goes here\n");
sl@0: 	BIO_printf(bio_err," -serverpref   - Use server's cipher preferences\n");
sl@0: 	BIO_printf(bio_err," -quiet        - No server output\n");
sl@0: 	BIO_printf(bio_err," -no_tmp_rsa   - Do not generate a tmp RSA key\n");
sl@0: 	BIO_printf(bio_err," -ssl2         - Just talk SSLv2\n");
sl@0: 	BIO_printf(bio_err," -ssl3         - Just talk SSLv3\n");
sl@0: 	BIO_printf(bio_err," -tls1         - Just talk TLSv1\n");
sl@0: 	BIO_printf(bio_err," -dtls1        - Just talk DTLSv1\n");
sl@0: 	BIO_printf(bio_err," -timeout      - Enable timeouts\n");
sl@0: 	BIO_printf(bio_err," -mtu          - Set MTU\n");
sl@0: 	BIO_printf(bio_err," -chain        - Read a certificate chain\n");
sl@0: 	BIO_printf(bio_err," -no_ssl2      - Just disable SSLv2\n");
sl@0: 	BIO_printf(bio_err," -no_ssl3      - Just disable SSLv3\n");
sl@0: 	BIO_printf(bio_err," -no_tls1      - Just disable TLSv1\n");
sl@0: #ifndef OPENSSL_NO_DH
sl@0: 	BIO_printf(bio_err," -no_dhe       - Disable ephemeral DH\n");
sl@0: #endif
sl@0: #ifndef OPENSSL_NO_ECDH
sl@0: 	BIO_printf(bio_err," -no_ecdhe     - Disable ephemeral ECDH\n");
sl@0: #endif
sl@0: 	BIO_printf(bio_err," -bugs         - Turn on SSL bug compatibility\n");
sl@0: 	BIO_printf(bio_err," -www          - Respond to a 'GET /' with a status page\n");
sl@0: 	BIO_printf(bio_err," -WWW          - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
sl@0: 	BIO_printf(bio_err," -HTTP         - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
sl@0:         BIO_printf(bio_err,"                 with the assumption it contains a complete HTTP response.\n");
sl@0: #ifndef OPENSSL_NO_ENGINE
sl@0: 	BIO_printf(bio_err," -engine id    - Initialise and use the specified engine\n");
sl@0: #endif
sl@0: 	BIO_printf(bio_err," -id_prefix arg - Generate SSL/TLS session IDs prefixed by 'arg'\n");
sl@0: 	BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
sl@0: 	}
sl@0: 
sl@0: static int local_argc=0;
sl@0: static char **local_argv;
sl@0: 
sl@0: #ifdef CHARSET_EBCDIC
sl@0: static int ebcdic_new(BIO *bi);
sl@0: static int ebcdic_free(BIO *a);
sl@0: static int ebcdic_read(BIO *b, char *out, int outl);
sl@0: static int ebcdic_write(BIO *b, const char *in, int inl);
sl@0: static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr);
sl@0: static int ebcdic_gets(BIO *bp, char *buf, int size);
sl@0: static int ebcdic_puts(BIO *bp, const char *str);
sl@0: 
sl@0: #define BIO_TYPE_EBCDIC_FILTER	(18|0x0200)
sl@0: static BIO_METHOD methods_ebcdic=
sl@0: 	{
sl@0: 	BIO_TYPE_EBCDIC_FILTER,
sl@0: 	"EBCDIC/ASCII filter",
sl@0: 	ebcdic_write,
sl@0: 	ebcdic_read,
sl@0: 	ebcdic_puts,
sl@0: 	ebcdic_gets,
sl@0: 	ebcdic_ctrl,
sl@0: 	ebcdic_new,
sl@0: 	ebcdic_free,
sl@0: 	};
sl@0: 
sl@0: typedef struct
sl@0: {
sl@0: 	size_t	alloced;
sl@0: 	char	buff[1];
sl@0: } EBCDIC_OUTBUFF;
sl@0: 
sl@0: BIO_METHOD *BIO_f_ebcdic_filter()
sl@0: {
sl@0: 	return(&methods_ebcdic);
sl@0: }
sl@0: 
sl@0: static int ebcdic_new(BIO *bi)
sl@0: {
sl@0: 	EBCDIC_OUTBUFF *wbuf;
sl@0: 
sl@0: 	wbuf = (EBCDIC_OUTBUFF *)OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + 1024);
sl@0: 	wbuf->alloced = 1024;
sl@0: 	wbuf->buff[0] = '\0';
sl@0: 
sl@0: 	bi->ptr=(char *)wbuf;
sl@0: 	bi->init=1;
sl@0: 	bi->flags=0;
sl@0: 	return(1);
sl@0: }
sl@0: 
sl@0: static int ebcdic_free(BIO *a)
sl@0: {
sl@0: 	if (a == NULL) return(0);
sl@0: 	if (a->ptr != NULL)
sl@0: 		OPENSSL_free(a->ptr);
sl@0: 	a->ptr=NULL;
sl@0: 	a->init=0;
sl@0: 	a->flags=0;
sl@0: 	return(1);
sl@0: }
sl@0: 	
sl@0: static int ebcdic_read(BIO *b, char *out, int outl)
sl@0: {
sl@0: 	int ret=0;
sl@0: 
sl@0: 	if (out == NULL || outl == 0) return(0);
sl@0: 	if (b->next_bio == NULL) return(0);
sl@0: 
sl@0: 	ret=BIO_read(b->next_bio,out,outl);
sl@0: 	if (ret > 0)
sl@0: 		ascii2ebcdic(out,out,ret);
sl@0: 	return(ret);
sl@0: }
sl@0: 
sl@0: static int ebcdic_write(BIO *b, const char *in, int inl)
sl@0: {
sl@0: 	EBCDIC_OUTBUFF *wbuf;
sl@0: 	int ret=0;
sl@0: 	int num;
sl@0: 	unsigned char n;
sl@0: 
sl@0: 	if ((in == NULL) || (inl <= 0)) return(0);
sl@0: 	if (b->next_bio == NULL) return(0);
sl@0: 
sl@0: 	wbuf=(EBCDIC_OUTBUFF *)b->ptr;
sl@0: 
sl@0: 	if (inl > (num = wbuf->alloced))
sl@0: 	{
sl@0: 		num = num + num;  /* double the size */
sl@0: 		if (num < inl)
sl@0: 			num = inl;
sl@0: 		OPENSSL_free(wbuf);
sl@0: 		wbuf=(EBCDIC_OUTBUFF *)OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + num);
sl@0: 
sl@0: 		wbuf->alloced = num;
sl@0: 		wbuf->buff[0] = '\0';
sl@0: 
sl@0: 		b->ptr=(char *)wbuf;
sl@0: 	}
sl@0: 
sl@0: 	ebcdic2ascii(wbuf->buff, in, inl);
sl@0: 
sl@0: 	ret=BIO_write(b->next_bio, wbuf->buff, inl);
sl@0: 
sl@0: 	return(ret);
sl@0: }
sl@0: 
sl@0: static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr)
sl@0: {
sl@0: 	long ret;
sl@0: 
sl@0: 	if (b->next_bio == NULL) return(0);
sl@0: 	switch (cmd)
sl@0: 	{
sl@0: 	case BIO_CTRL_DUP:
sl@0: 		ret=0L;
sl@0: 		break;
sl@0: 	default:
sl@0: 		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
sl@0: 		break;
sl@0: 	}
sl@0: 	return(ret);
sl@0: }
sl@0: 
sl@0: static int ebcdic_gets(BIO *bp, char *buf, int size)
sl@0: {
sl@0: 	int i, ret=0;
sl@0: 	if (bp->next_bio == NULL) return(0);
sl@0: /*	return(BIO_gets(bp->next_bio,buf,size));*/
sl@0: 	for (i=0; i<size-1; ++i)
sl@0: 	{
sl@0: 		ret = ebcdic_read(bp,&buf[i],1);
sl@0: 		if (ret <= 0)
sl@0: 			break;
sl@0: 		else if (buf[i] == '\n')
sl@0: 		{
sl@0: 			++i;
sl@0: 			break;
sl@0: 		}
sl@0: 	}
sl@0: 	if (i < size)
sl@0: 		buf[i] = '\0';
sl@0: 	return (ret < 0 && i == 0) ? ret : i;
sl@0: }
sl@0: 
sl@0: static int ebcdic_puts(BIO *bp, const char *str)
sl@0: {
sl@0: 	if (bp->next_bio == NULL) return(0);
sl@0: 	return ebcdic_write(bp, str, strlen(str));
sl@0: }
sl@0: #endif
sl@0: 
sl@0: int MAIN(int, char **);
sl@0: 
sl@0: int MAIN(int argc, char *argv[])
sl@0: 	{
sl@0: 	X509_STORE *store = NULL;
sl@0: 	int vflags = 0;
sl@0: 	short port=PORT;
sl@0: 	char *CApath=NULL,*CAfile=NULL;
sl@0: 	unsigned char *context = NULL;
sl@0: 	char *dhfile = NULL;
sl@0: #ifndef OPENSSL_NO_ECDH
sl@0: 	char *named_curve = NULL;
sl@0: #endif
sl@0: 	int badop=0,bugs=0;
sl@0: 	int ret=1;
sl@0: 	int off=0;
sl@0: 	int no_tmp_rsa=0,no_dhe=0,no_ecdhe=0,nocert=0;
sl@0: 	int state=0;
sl@0: 	SSL_METHOD *meth=NULL;
sl@0:         int socket_type=SOCK_STREAM;
sl@0: #ifndef OPENSSL_NO_ENGINE
sl@0: 	ENGINE *e=NULL;
sl@0: #endif
sl@0: 	char *inrand=NULL;
sl@0: 	int s_cert_format = FORMAT_PEM, s_key_format = FORMAT_PEM;
sl@0: 	char *passarg = NULL, *pass = NULL;
sl@0: 	char *dpassarg = NULL, *dpass = NULL;
sl@0: 	int s_dcert_format = FORMAT_PEM, s_dkey_format = FORMAT_PEM;
sl@0: 	X509 *s_cert = NULL, *s_dcert = NULL;
sl@0: 	EVP_PKEY *s_key = NULL, *s_dkey = NULL;
sl@0: 
sl@0: 
sl@0: #if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
sl@0: 	meth=SSLv23_server_method();
sl@0: #elif !defined(OPENSSL_NO_SSL3)
sl@0: 	meth=SSLv3_server_method();
sl@0: #elif !defined(OPENSSL_NO_SSL2)
sl@0: 	meth=SSLv2_server_method();
sl@0: #endif
sl@0: 
sl@0: 	local_argc=argc;
sl@0: 	local_argv=argv;
sl@0: 
sl@0: 	apps_startup();
sl@0: #ifdef MONOLITH
sl@0: 	s_server_init();
sl@0: #endif
sl@0: 
sl@0: 	if (bio_err == NULL)
sl@0: 		bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
sl@0: 
sl@0: 	if (!load_config(bio_err, NULL))
sl@0: 		goto end;
sl@0: 
sl@0: 	verify_depth=0;
sl@0: #ifdef FIONBIO
sl@0: 	s_nbio=0;
sl@0: #endif
sl@0: 	s_nbio_test=0;
sl@0: 
sl@0: 	argc--;
sl@0: 	argv++;
sl@0: 
sl@0: 	while (argc >= 1)
sl@0: 		{
sl@0: 		if	((strcmp(*argv,"-port") == 0) ||
sl@0: 			 (strcmp(*argv,"-accept") == 0))
sl@0: 			{
sl@0: 			if (--argc < 1) goto bad;
sl@0: 			if (!extract_port(*(++argv),&port))
sl@0: 				goto bad;
sl@0: 			}
sl@0: 		else if	(strcmp(*argv,"-verify") == 0)
sl@0: 			{
sl@0: 			s_server_verify=SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE;
sl@0: 			if (--argc < 1) goto bad;
sl@0: 			verify_depth=atoi(*(++argv));
sl@0: 			BIO_printf(bio_err,"verify depth is %d\n",verify_depth);
sl@0: 			}
sl@0: 		else if	(strcmp(*argv,"-Verify") == 0)
sl@0: 			{
sl@0: 			s_server_verify=SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT|
sl@0: 				SSL_VERIFY_CLIENT_ONCE;
sl@0: 			if (--argc < 1) goto bad;
sl@0: 			verify_depth=atoi(*(++argv));
sl@0: 			BIO_printf(bio_err,"verify depth is %d, must return a certificate\n",verify_depth);
sl@0: 			}
sl@0: 		else if	(strcmp(*argv,"-context") == 0)
sl@0: 			{
sl@0: 			if (--argc < 1) goto bad;
sl@0: 			context= (unsigned char *)*(++argv);
sl@0: 			}
sl@0: 		else if	(strcmp(*argv,"-cert") == 0)
sl@0: 			{
sl@0: 			if (--argc < 1) goto bad;
sl@0: 			s_cert_file= *(++argv);
sl@0: 			}
sl@0: 		else if	(strcmp(*argv,"-certform") == 0)
sl@0: 			{
sl@0: 			if (--argc < 1) goto bad;
sl@0: 			s_cert_format = str2fmt(*(++argv));
sl@0: 			}
sl@0: 		else if	(strcmp(*argv,"-key") == 0)
sl@0: 			{
sl@0: 			if (--argc < 1) goto bad;
sl@0: 			s_key_file= *(++argv);
sl@0: 			}
sl@0: 		else if	(strcmp(*argv,"-keyform") == 0)
sl@0: 			{
sl@0: 			if (--argc < 1) goto bad;
sl@0: 			s_key_format = str2fmt(*(++argv));
sl@0: 			}
sl@0: 		else if	(strcmp(*argv,"-pass") == 0)
sl@0: 			{
sl@0: 			if (--argc < 1) goto bad;
sl@0: 			passarg = *(++argv);
sl@0: 			}
sl@0: 		else if	(strcmp(*argv,"-dhparam") == 0)
sl@0: 			{
sl@0: 			if (--argc < 1) goto bad;
sl@0: 			dhfile = *(++argv);
sl@0: 			}
sl@0: #ifndef OPENSSL_NO_ECDH		
sl@0: 		else if	(strcmp(*argv,"-named_curve") == 0)
sl@0: 			{
sl@0: 			if (--argc < 1) goto bad;
sl@0: 			named_curve = *(++argv);
sl@0: 			}
sl@0: #endif
sl@0: 		else if	(strcmp(*argv,"-dcertform") == 0)
sl@0: 			{
sl@0: 			if (--argc < 1) goto bad;
sl@0: 			s_dcert_format = str2fmt(*(++argv));
sl@0: 			}
sl@0: 		else if	(strcmp(*argv,"-dcert") == 0)
sl@0: 			{
sl@0: 			if (--argc < 1) goto bad;
sl@0: 			s_dcert_file= *(++argv);
sl@0: 			}
sl@0: 		else if	(strcmp(*argv,"-dkeyform") == 0)
sl@0: 			{
sl@0: 			if (--argc < 1) goto bad;
sl@0: 			s_dkey_format = str2fmt(*(++argv));
sl@0: 			}
sl@0: 		else if	(strcmp(*argv,"-dpass") == 0)
sl@0: 			{
sl@0: 			if (--argc < 1) goto bad;
sl@0: 			dpassarg = *(++argv);
sl@0: 			}
sl@0: 		else if	(strcmp(*argv,"-dkey") == 0)
sl@0: 			{
sl@0: 			if (--argc < 1) goto bad;
sl@0: 			s_dkey_file= *(++argv);
sl@0: 			}
sl@0: 		else if (strcmp(*argv,"-nocert") == 0)
sl@0: 			{
sl@0: 			nocert=1;
sl@0: 			}
sl@0: 		else if	(strcmp(*argv,"-CApath") == 0)
sl@0: 			{
sl@0: 			if (--argc < 1) goto bad;
sl@0: 			CApath= *(++argv);
sl@0: 			}
sl@0: 		else if (strcmp(*argv,"-crl_check") == 0)
sl@0: 			{
sl@0: 			vflags |= X509_V_FLAG_CRL_CHECK;
sl@0: 			}
sl@0: 		else if (strcmp(*argv,"-crl_check") == 0)
sl@0: 			{
sl@0: 			vflags |= X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL;
sl@0: 			}
sl@0: 		else if	(strcmp(*argv,"-serverpref") == 0)
sl@0: 			{ off|=SSL_OP_CIPHER_SERVER_PREFERENCE; }
sl@0: 		else if	(strcmp(*argv,"-cipher") == 0)
sl@0: 			{
sl@0: 			if (--argc < 1) goto bad;
sl@0: 			cipher= *(++argv);
sl@0: 			}
sl@0: 		else if	(strcmp(*argv,"-CAfile") == 0)
sl@0: 			{
sl@0: 			if (--argc < 1) goto bad;
sl@0: 			CAfile= *(++argv);
sl@0: 			}
sl@0: #ifdef FIONBIO	
sl@0: 		else if	(strcmp(*argv,"-nbio") == 0)
sl@0: 			{ s_nbio=1; }
sl@0: #endif
sl@0: 		else if	(strcmp(*argv,"-nbio_test") == 0)
sl@0: 			{
sl@0: #ifdef FIONBIO	
sl@0: 			s_nbio=1;
sl@0: #endif
sl@0: 			s_nbio_test=1;
sl@0: 			}
sl@0: 		else if	(strcmp(*argv,"-debug") == 0)
sl@0: 			{ s_debug=1; }
sl@0: 		else if	(strcmp(*argv,"-msg") == 0)
sl@0: 			{ s_msg=1; }
sl@0: 		else if	(strcmp(*argv,"-hack") == 0)
sl@0: 			{ hack=1; }
sl@0: 		else if	(strcmp(*argv,"-state") == 0)
sl@0: 			{ state=1; }
sl@0: 		else if	(strcmp(*argv,"-crlf") == 0)
sl@0: 			{ s_crlf=1; }
sl@0: 		else if	(strcmp(*argv,"-quiet") == 0)
sl@0: 			{ s_quiet=1; }
sl@0: 		else if	(strcmp(*argv,"-bugs") == 0)
sl@0: 			{ bugs=1; }
sl@0: 		else if	(strcmp(*argv,"-no_tmp_rsa") == 0)
sl@0: 			{ no_tmp_rsa=1; }
sl@0: 		else if	(strcmp(*argv,"-no_dhe") == 0)
sl@0: 			{ no_dhe=1; }
sl@0: 		else if	(strcmp(*argv,"-no_ecdhe") == 0)
sl@0: 			{ no_ecdhe=1; }
sl@0: 		else if	(strcmp(*argv,"-www") == 0)
sl@0: 			{ www=1; }
sl@0: 		else if	(strcmp(*argv,"-WWW") == 0)
sl@0: 			{ www=2; }
sl@0: 		else if	(strcmp(*argv,"-HTTP") == 0)
sl@0: 			{ www=3; }
sl@0: 		else if	(strcmp(*argv,"-no_ssl2") == 0)
sl@0: 			{ off|=SSL_OP_NO_SSLv2; }
sl@0: 		else if	(strcmp(*argv,"-no_ssl3") == 0)
sl@0: 			{ off|=SSL_OP_NO_SSLv3; }
sl@0: 		else if	(strcmp(*argv,"-no_tls1") == 0)
sl@0: 			{ off|=SSL_OP_NO_TLSv1; }
sl@0: #ifndef OPENSSL_NO_SSL2
sl@0: 		else if	(strcmp(*argv,"-ssl2") == 0)
sl@0: 			{ meth=SSLv2_server_method(); }
sl@0: #endif
sl@0: #ifndef OPENSSL_NO_SSL3
sl@0: 		else if	(strcmp(*argv,"-ssl3") == 0)
sl@0: 			{ meth=SSLv3_server_method(); }
sl@0: #endif
sl@0: #ifndef OPENSSL_NO_TLS1
sl@0: 		else if	(strcmp(*argv,"-tls1") == 0)
sl@0: 			{ meth=TLSv1_server_method(); }
sl@0: #endif
sl@0: #ifndef OPENSSL_NO_DTLS1
sl@0: 		else if	(strcmp(*argv,"-dtls1") == 0)
sl@0: 			{ 
sl@0: 			meth=DTLSv1_server_method();
sl@0: 			socket_type = SOCK_DGRAM;
sl@0: 			}
sl@0: 		else if (strcmp(*argv,"-timeout") == 0)
sl@0: 			enable_timeouts = 1;
sl@0: 		else if (strcmp(*argv,"-mtu") == 0)
sl@0: 			{
sl@0: 			if (--argc < 1) goto bad;
sl@0: 			mtu = atol(*(++argv));
sl@0: 			}
sl@0: 		else if (strcmp(*argv, "-chain") == 0)
sl@0: 			cert_chain = 1;
sl@0: #endif
sl@0: 		else if (strcmp(*argv, "-id_prefix") == 0)
sl@0: 			{
sl@0: 			if (--argc < 1) goto bad;
sl@0: 			session_id_prefix = *(++argv);
sl@0: 			}
sl@0: #ifndef OPENSSL_NO_ENGINE
sl@0: 		else if (strcmp(*argv,"-engine") == 0)
sl@0: 			{
sl@0: 			if (--argc < 1) goto bad;
sl@0: 			engine_id= *(++argv);
sl@0: 			}
sl@0: #endif
sl@0: 		else if (strcmp(*argv,"-rand") == 0)
sl@0: 			{
sl@0: 			if (--argc < 1) goto bad;
sl@0: 			inrand= *(++argv);
sl@0: 			}
sl@0: 		else
sl@0: 			{
sl@0: 			BIO_printf(bio_err,"unknown option %s\n",*argv);
sl@0: 			badop=1;
sl@0: 			break;
sl@0: 			}
sl@0: 		argc--;
sl@0: 		argv++;
sl@0: 		}
sl@0: 	if (badop)
sl@0: 		{
sl@0: bad:
sl@0: 		sv_usage();
sl@0: 		goto end;
sl@0: 		}
sl@0: 
sl@0: 	SSL_load_error_strings();
sl@0: 	OpenSSL_add_ssl_algorithms();
sl@0: 
sl@0: #ifndef OPENSSL_NO_ENGINE
sl@0:         e = setup_engine(bio_err, engine_id, 1);
sl@0: #endif
sl@0: 
sl@0: 	if (!app_passwd(bio_err, passarg, dpassarg, &pass, &dpass))
sl@0: 		{
sl@0: 		BIO_printf(bio_err, "Error getting password\n");
sl@0: 		goto end;
sl@0: 		}
sl@0: 
sl@0: 
sl@0: 	if (s_key_file == NULL)
sl@0: 		s_key_file = s_cert_file;
sl@0: 
sl@0: 	if (nocert == 0)
sl@0: 		{
sl@0: 		s_key = load_key(bio_err, s_key_file, s_key_format, 0, pass, e,
sl@0: 		       "server certificate private key file");
sl@0: 		if (!s_key)
sl@0: 			{
sl@0: 			ERR_print_errors(bio_err);
sl@0: 			goto end;
sl@0: 			}
sl@0: 
sl@0: 		s_cert = load_cert(bio_err,s_cert_file,s_cert_format,
sl@0: 			NULL, e, "server certificate file");
sl@0: 
sl@0: 		if (!s_cert)
sl@0: 			{
sl@0: 			ERR_print_errors(bio_err);
sl@0: 			goto end;
sl@0: 			}
sl@0: 
sl@0: 		}
sl@0: 	if (s_dcert_file)
sl@0: 		{
sl@0: 
sl@0: 		if (s_dkey_file == NULL)
sl@0: 			s_dkey_file = s_dcert_file;
sl@0: 
sl@0: 		s_dkey = load_key(bio_err, s_dkey_file, s_dkey_format,
sl@0: 				0, dpass, e,
sl@0: 			       "second certificate private key file");
sl@0: 		if (!s_dkey)
sl@0: 			{
sl@0: 			ERR_print_errors(bio_err);
sl@0: 			goto end;
sl@0: 			}
sl@0: 
sl@0: 		s_dcert = load_cert(bio_err,s_dcert_file,s_dcert_format,
sl@0: 				NULL, e, "second server certificate file");
sl@0: 
sl@0: 		if (!s_dcert)
sl@0: 			{
sl@0: 			ERR_print_errors(bio_err);
sl@0: 			goto end;
sl@0: 			}
sl@0: 
sl@0: 		}
sl@0: 
sl@0: 	if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL
sl@0: 		&& !RAND_status())
sl@0: 		{
sl@0: 		BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
sl@0: 		}
sl@0: 	if (inrand != NULL)
sl@0: 		BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
sl@0: 			app_RAND_load_files(inrand));
sl@0: 
sl@0: 	if (bio_s_out == NULL)
sl@0: 		{
sl@0: 		if (s_quiet && !s_debug && !s_msg)
sl@0: 			{
sl@0: 			bio_s_out=BIO_new(BIO_s_null());
sl@0: 			}
sl@0: 		else
sl@0: 			{
sl@0: 			if (bio_s_out == NULL)
sl@0: 				bio_s_out=BIO_new_fp(stdout,BIO_NOCLOSE);
sl@0: 			
sl@0: 			}
sl@0: 		}
sl@0: 
sl@0: #if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
sl@0: 	if (nocert)
sl@0: #endif
sl@0: 		{
sl@0: 		s_cert_file=NULL;
sl@0: 		s_key_file=NULL;
sl@0: 		s_dcert_file=NULL;
sl@0: 		s_dkey_file=NULL;
sl@0: 		}
sl@0: 
sl@0: 	ctx=SSL_CTX_new(meth);
sl@0: 	if (ctx == NULL)
sl@0: 		{
sl@0: 		ERR_print_errors(bio_err);
sl@0: 		goto end;
sl@0: 		}
sl@0: 	if (session_id_prefix)
sl@0: 		{
sl@0: 		if(strlen(session_id_prefix) >= 32)
sl@0: 			BIO_printf(bio_err,
sl@0: "warning: id_prefix is too long, only one new session will be possible\n");
sl@0: 		else if(strlen(session_id_prefix) >= 16)
sl@0: 			BIO_printf(bio_err,
sl@0: "warning: id_prefix is too long if you use SSLv2\n");
sl@0: 		if(!SSL_CTX_set_generate_session_id(ctx, generate_session_id))
sl@0: 			{
sl@0: 			BIO_printf(bio_err,"error setting 'id_prefix'\n");
sl@0: 			ERR_print_errors(bio_err);
sl@0: 			goto end;
sl@0: 			}
sl@0: 		BIO_printf(bio_err,"id_prefix '%s' set.\n", session_id_prefix);
sl@0: 		}
sl@0: 	SSL_CTX_set_quiet_shutdown(ctx,1);
sl@0: 	if (bugs) SSL_CTX_set_options(ctx,SSL_OP_ALL);
sl@0: 	if (hack) SSL_CTX_set_options(ctx,SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG);
sl@0: 	SSL_CTX_set_options(ctx,off);
sl@0: 	/* DTLS: partial reads end up discarding unread UDP bytes :-( 
sl@0: 	 * Setting read ahead solves this problem.
sl@0: 	 */
sl@0: 	if (socket_type == SOCK_DGRAM) SSL_CTX_set_read_ahead(ctx, 1);
sl@0: 
sl@0: 	if (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback);
sl@0: 
sl@0: 	SSL_CTX_sess_set_cache_size(ctx,128);
sl@0: 
sl@0: #if 0
sl@0: 	if (cipher == NULL) cipher=getenv("SSL_CIPHER");
sl@0: #endif
sl@0: 
sl@0: #if 0
sl@0: 	if (s_cert_file == NULL)
sl@0: 		{
sl@0: 		BIO_printf(bio_err,"You must specify a certificate file for the server to use\n");
sl@0: 		goto end;
sl@0: 		}
sl@0: #endif
sl@0: 
sl@0: 	if ((!SSL_CTX_load_verify_locations(ctx,CAfile,CApath)) ||
sl@0: 		(!SSL_CTX_set_default_verify_paths(ctx)))
sl@0: 		{
sl@0: 		/* BIO_printf(bio_err,"X509_load_verify_locations\n"); */
sl@0: 		ERR_print_errors(bio_err);
sl@0: 		/* goto end; */
sl@0: 		}
sl@0: 	store = SSL_CTX_get_cert_store(ctx);
sl@0: 	X509_STORE_set_flags(store, vflags);
sl@0: 
sl@0: #ifndef OPENSSL_NO_DH
sl@0: 	if (!no_dhe)
sl@0: 		{
sl@0: 		DH *dh=NULL;
sl@0: 
sl@0: 		if (dhfile)
sl@0: 			dh = load_dh_param(dhfile);
sl@0: 		else if (s_cert_file)
sl@0: 			dh = load_dh_param(s_cert_file);
sl@0: 
sl@0: 		if (dh != NULL)
sl@0: 			{
sl@0: 			BIO_printf(bio_s_out,"Setting temp DH parameters\n");
sl@0: 			}
sl@0: 		else
sl@0: 			{
sl@0: 			BIO_printf(bio_s_out,"Using default temp DH parameters\n");
sl@0: 			dh=get_dh512();
sl@0: 			}
sl@0: 		(void)BIO_flush(bio_s_out);
sl@0: 
sl@0: 		SSL_CTX_set_tmp_dh(ctx,dh);
sl@0: 		DH_free(dh);
sl@0: 		}
sl@0: #endif
sl@0: 
sl@0: #ifndef OPENSSL_NO_ECDH
sl@0: 	if (!no_ecdhe)
sl@0: 		{
sl@0: 		EC_KEY *ecdh=NULL;
sl@0: 
sl@0: 		if (named_curve)
sl@0: 			{
sl@0: 			int nid = OBJ_sn2nid(named_curve);
sl@0: 
sl@0: 			if (nid == 0)
sl@0: 				{
sl@0: 				BIO_printf(bio_err, "unknown curve name (%s)\n", 
sl@0: 					named_curve);
sl@0: 				goto end;
sl@0: 				}
sl@0: 			ecdh = EC_KEY_new_by_curve_name(nid);
sl@0: 			if (ecdh == NULL)
sl@0: 				{
sl@0: 				BIO_printf(bio_err, "unable to create curve (%s)\n", 
sl@0: 					named_curve);
sl@0: 				goto end;
sl@0: 				}
sl@0: 			}
sl@0: 
sl@0: 		if (ecdh != NULL)
sl@0: 			{
sl@0: 			BIO_printf(bio_s_out,"Setting temp ECDH parameters\n");
sl@0: 			}
sl@0: 		else
sl@0: 			{
sl@0: 			BIO_printf(bio_s_out,"Using default temp ECDH parameters\n");
sl@0: 			ecdh = EC_KEY_new_by_curve_name(NID_sect163r2);
sl@0: 			if (ecdh == NULL) 
sl@0: 				{
sl@0: 				BIO_printf(bio_err, "unable to create curve (sect163r2)\n");
sl@0: 				goto end;
sl@0: 				}
sl@0: 			}
sl@0: 		(void)BIO_flush(bio_s_out);
sl@0: 
sl@0: 		SSL_CTX_set_tmp_ecdh(ctx,ecdh);
sl@0: 
sl@0: 		EC_KEY_free(ecdh);
sl@0: 		}
sl@0: #endif
sl@0: 	
sl@0: 	if (!set_cert_key_stuff(ctx,s_cert,s_key))
sl@0: 		goto end;
sl@0: 
sl@0: 	if (s_dcert != NULL)
sl@0: 		{
sl@0: 		if (!set_cert_key_stuff(ctx,s_dcert,s_dkey))
sl@0: 			goto end;
sl@0: 		}
sl@0: 
sl@0: #ifndef OPENSSL_NO_RSA
sl@0: #if 1
sl@0: 	if (!no_tmp_rsa)
sl@0: 		{
sl@0: 		SSL_CTX_set_tmp_rsa_callback(ctx,tmp_rsa_cb);
sl@0: 
sl@0: 		}
sl@0: #else
sl@0: 	if (!no_tmp_rsa && SSL_CTX_need_tmp_RSA(ctx))
sl@0: 		{
sl@0: 		RSA *rsa;
sl@0: 
sl@0: 		BIO_printf(bio_s_out,"Generating temp (512 bit) RSA key...");
sl@0: 		BIO_flush(bio_s_out);
sl@0: 
sl@0: 		rsa=RSA_generate_key(512,RSA_F4,NULL);
sl@0: 
sl@0: 		if (!SSL_CTX_set_tmp_rsa(ctx,rsa))
sl@0: 			{
sl@0: 			ERR_print_errors(bio_err);
sl@0: 			goto end;
sl@0: 			}
sl@0: 		RSA_free(rsa);
sl@0: 		BIO_printf(bio_s_out,"\n");
sl@0: 		}
sl@0: #endif
sl@0: #endif
sl@0: 
sl@0: 	if (cipher != NULL)
sl@0: 		if(!SSL_CTX_set_cipher_list(ctx,cipher)) {
sl@0: 		BIO_printf(bio_err,"error setting cipher list\n");
sl@0: 		ERR_print_errors(bio_err);
sl@0: 		goto end;
sl@0: 	}
sl@0: 	SSL_CTX_set_verify(ctx,s_server_verify,verify_callback);
sl@0: 	SSL_CTX_set_session_id_context(ctx,(void*)&s_server_session_id_context,
sl@0: 		sizeof s_server_session_id_context);
sl@0: 
sl@0: 	if (CAfile != NULL)
sl@0: 		{
sl@0: 		SSL_CTX_set_client_CA_list(ctx,SSL_load_client_CA_file(CAfile));
sl@0: 		}
sl@0: 	BIO_printf(bio_s_out,"ACCEPT\n");
sl@0: 	if (www)
sl@0: 		do_server(port,socket_type,&accept_socket,www_body, context);
sl@0: 	else
sl@0: 		do_server(port,socket_type,&accept_socket,sv_body, context);
sl@0: 	print_stats(bio_s_out,ctx);
sl@0: 	ret=0;
sl@0: end:
sl@0: 	if (ctx != NULL) SSL_CTX_free(ctx);
sl@0: 	if (s_cert)
sl@0: 		X509_free(s_cert);
sl@0: 	if (s_dcert)
sl@0: 		X509_free(s_dcert);
sl@0: 	if (s_key)
sl@0: 		EVP_PKEY_free(s_key);
sl@0: 	if (s_dkey)
sl@0: 		EVP_PKEY_free(s_dkey);
sl@0: 	if (pass)
sl@0: 		OPENSSL_free(pass);
sl@0: 	if (dpass)
sl@0: 		OPENSSL_free(dpass);
sl@0: 	if (bio_s_out != NULL)
sl@0: 		{
sl@0:         BIO_free(bio_s_out);
sl@0: 		bio_s_out=NULL;
sl@0: 		}
sl@0: 	apps_shutdown();
sl@0: 	OPENSSL_EXIT(ret);
sl@0: 	}
sl@0: 
sl@0: static void print_stats(BIO *bio, SSL_CTX *ssl_ctx)
sl@0: 	{
sl@0: 	BIO_printf(bio,"%4ld items in the session cache\n",
sl@0: 		SSL_CTX_sess_number(ssl_ctx));
sl@0: 	BIO_printf(bio,"%4ld client connects (SSL_connect())\n",
sl@0: 		SSL_CTX_sess_connect(ssl_ctx));
sl@0: 	BIO_printf(bio,"%4ld client renegotiates (SSL_connect())\n",
sl@0: 		SSL_CTX_sess_connect_renegotiate(ssl_ctx));
sl@0: 	BIO_printf(bio,"%4ld client connects that finished\n",
sl@0: 		SSL_CTX_sess_connect_good(ssl_ctx));
sl@0: 	BIO_printf(bio,"%4ld server accepts (SSL_accept())\n",
sl@0: 		SSL_CTX_sess_accept(ssl_ctx));
sl@0: 	BIO_printf(bio,"%4ld server renegotiates (SSL_accept())\n",
sl@0: 		SSL_CTX_sess_accept_renegotiate(ssl_ctx));
sl@0: 	BIO_printf(bio,"%4ld server accepts that finished\n",
sl@0: 		SSL_CTX_sess_accept_good(ssl_ctx));
sl@0: 	BIO_printf(bio,"%4ld session cache hits\n",SSL_CTX_sess_hits(ssl_ctx));
sl@0: 	BIO_printf(bio,"%4ld session cache misses\n",SSL_CTX_sess_misses(ssl_ctx));
sl@0: 	BIO_printf(bio,"%4ld session cache timeouts\n",SSL_CTX_sess_timeouts(ssl_ctx));
sl@0: 	BIO_printf(bio,"%4ld callback cache hits\n",SSL_CTX_sess_cb_hits(ssl_ctx));
sl@0: 	BIO_printf(bio,"%4ld cache full overflows (%ld allowed)\n",
sl@0: 		SSL_CTX_sess_cache_full(ssl_ctx),
sl@0: 		SSL_CTX_sess_get_cache_size(ssl_ctx));
sl@0: 	}
sl@0: 
sl@0: static int sv_body(char *hostname, int s, unsigned char *context)
sl@0: 	{
sl@0: 	char *buf=NULL;
sl@0: 	fd_set readfds;
sl@0: 	int ret=1,width;
sl@0: 	int k,i;
sl@0: 	unsigned long l;
sl@0: 	SSL *con=NULL;
sl@0: 	BIO *sbio;
sl@0: #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE)
sl@0: 	struct timeval tv;
sl@0: #endif
sl@0: 
sl@0: 	if ((buf=OPENSSL_malloc(bufsize)) == NULL)
sl@0: 		{
sl@0: 		BIO_printf(bio_err,"out of memory\n");
sl@0: 		goto err;
sl@0: 		}
sl@0: #ifdef FIONBIO	
sl@0: 	if (s_nbio)
sl@0: 		{
sl@0: 		unsigned long sl=1;
sl@0: 
sl@0: 		if (!s_quiet)
sl@0: 			BIO_printf(bio_err,"turning on non blocking io\n");
sl@0: 		if (BIO_socket_ioctl(s,FIONBIO,&sl) < 0)
sl@0: 			ERR_print_errors(bio_err);
sl@0: 		}
sl@0: #endif
sl@0: 
sl@0: 	if (con == NULL) {
sl@0: 		con=SSL_new(ctx);
sl@0: #ifndef OPENSSL_NO_KRB5
sl@0: 		if ((con->kssl_ctx = kssl_ctx_new()) != NULL)
sl@0:                         {
sl@0:                         kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVICE,
sl@0: 								KRB5SVC);
sl@0:                         kssl_ctx_setstring(con->kssl_ctx, KSSL_KEYTAB,
sl@0: 								KRB5KEYTAB);
sl@0:                         }
sl@0: #endif	/* OPENSSL_NO_KRB5 */
sl@0: 		if(context)
sl@0: 		      SSL_set_session_id_context(con, context,
sl@0: 						 strlen((char *)context));
sl@0: 	}
sl@0: 	SSL_clear(con);
sl@0: 
sl@0: 	if (SSL_version(con) == DTLS1_VERSION)
sl@0: 		{
sl@0: 		struct timeval timeout;
sl@0: 
sl@0: 		sbio=BIO_new_dgram(s,BIO_NOCLOSE);
sl@0: 
sl@0: 		if ( enable_timeouts)
sl@0: 			{
sl@0: 			timeout.tv_sec = 0;
sl@0: 			timeout.tv_usec = DGRAM_RCV_TIMEOUT;
sl@0: 			BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
sl@0: 			
sl@0: 			timeout.tv_sec = 0;
sl@0: 			timeout.tv_usec = DGRAM_SND_TIMEOUT;
sl@0: 			BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
sl@0: 			}
sl@0: 
sl@0: 		
sl@0: 		if ( mtu > 0)
sl@0: 			{
sl@0: 			SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
sl@0: 			SSL_set_mtu(con, mtu);
sl@0: 			}
sl@0: 		else
sl@0: 			/* want to do MTU discovery */
sl@0: 			BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
sl@0: 
sl@0:         /* turn on cookie exchange */
sl@0:         SSL_set_options(con, SSL_OP_COOKIE_EXCHANGE);
sl@0: 		}
sl@0: 	else
sl@0: 		sbio=BIO_new_socket(s,BIO_NOCLOSE);
sl@0: 
sl@0: 	if (s_nbio_test)
sl@0: 		{
sl@0: 		BIO *test;
sl@0: 
sl@0: 		test=BIO_new(BIO_f_nbio_test());
sl@0: 		sbio=BIO_push(test,sbio);
sl@0: 		}
sl@0: 	SSL_set_bio(con,sbio,sbio);
sl@0: 	SSL_set_accept_state(con);
sl@0: 	/* SSL_set_fd(con,s); */
sl@0: 
sl@0: 	if (s_debug)
sl@0: 		{
sl@0: 		con->debug=1;
sl@0: 		BIO_set_callback(SSL_get_rbio(con),bio_dump_callback);
sl@0: 		BIO_set_callback_arg(SSL_get_rbio(con),(char *)bio_s_out);
sl@0: 		}
sl@0: 	if (s_msg)
sl@0: 		{
sl@0: 		SSL_set_msg_callback(con, msg_cb);
sl@0: 		SSL_set_msg_callback_arg(con, bio_s_out);
sl@0: 		}
sl@0: 
sl@0: 	width=s+1;
sl@0: 	for (;;)
sl@0: 		{
sl@0: 		int read_from_terminal;
sl@0: 		int read_from_sslcon;
sl@0: 
sl@0: 		read_from_terminal = 0;
sl@0: 		read_from_sslcon = SSL_pending(con);
sl@0: 
sl@0: 		if (!read_from_sslcon)
sl@0: 			{
sl@0: 			FD_ZERO(&readfds);
sl@0: #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE)
sl@0: 			FD_SET(fileno(stdin),&readfds);
sl@0: 	
sl@0: #endif
sl@0: 			FD_SET(s,&readfds);
sl@0: 			/* Note: under VMS with SOCKETSHR the second parameter is
sl@0: 			 * currently of type (int *) whereas under other systems
sl@0: 			 * it is (void *) if you don't have a cast it will choke
sl@0: 			 * the compiler: if you do have a cast then you can either
sl@0: 			 * go for (int *) or (void *).
sl@0: 			 */
sl@0: #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE)
sl@0:                         /* Under DOS (non-djgpp) and Windows we can't select on stdin: only
sl@0: 			 * on sockets. As a workaround we timeout the select every
sl@0: 			 * second and check for any keypress. In a proper Windows
sl@0: 			 * application we wouldn't do this because it is inefficient.
sl@0: 			 */
sl@0: 			tv.tv_sec = 1;
sl@0: 			tv.tv_usec = 0;
sl@0: 			i=select(width,(void *)&readfds,NULL,NULL,&tv);
sl@0: 			if((i < 0) || (!i && !_kbhit() ) )continue;
sl@0: 			if(_kbhit())
sl@0: 				read_from_terminal = 1;
sl@0: #else
sl@0: 			i=select(width,(void *)&readfds,NULL,NULL,NULL);
sl@0: 			if (i <= 0) continue;
sl@0: 			if (FD_ISSET(fileno(stdin),&readfds))
sl@0: 			
sl@0: 				read_from_terminal = 1;
sl@0: #endif
sl@0: 			if (FD_ISSET(s,&readfds))
sl@0: 				read_from_sslcon = 1;
sl@0: 			}
sl@0: 		if (read_from_terminal)
sl@0: 			{
sl@0: 			if (s_crlf)
sl@0: 				{
sl@0: 				int j, lf_num;
sl@0: 				i=read(fileno(stdin), buf, bufsize/2);
sl@0: 				lf_num = 0;
sl@0: 				/* both loops are skipped when i <= 0 */
sl@0: 				for (j = 0; j < i; j++)
sl@0: 					if (buf[j] == '\n')
sl@0: 						lf_num++;
sl@0: 				for (j = i-1; j >= 0; j--)
sl@0: 					{
sl@0: 					buf[j+lf_num] = buf[j];
sl@0: 					if (buf[j] == '\n')
sl@0: 						{
sl@0: 						lf_num--;
sl@0: 						i++;
sl@0: 						buf[j+lf_num] = '\r';
sl@0: 						}
sl@0: 					}
sl@0: 				assert(lf_num == 0);
sl@0: 				}
sl@0: 			else
sl@0: 				i=read(fileno(stdin),buf,bufsize);
sl@0: 				
sl@0: 			if (!s_quiet)
sl@0: 				{
sl@0: 				if ((i <= 0) || (buf[0] == 'Q'))
sl@0: 					{
sl@0: 					BIO_printf(bio_s_out,"DONE\n");
sl@0: 					SHUTDOWN(s);
sl@0: 					close_accept_socket();
sl@0: 					ret= -11;
sl@0: 					goto err;
sl@0: 					}
sl@0: 				if ((i <= 0) || (buf[0] == 'q'))
sl@0: 					{
sl@0: 					BIO_printf(bio_s_out,"DONE\n");
sl@0: 					if (SSL_version(con) != DTLS1_VERSION)
sl@0:                         SHUTDOWN(s);
sl@0: 	/*				close_accept_socket();
sl@0: 					ret= -11;*/
sl@0: 					goto err;
sl@0: 					}
sl@0: 				if ((buf[0] == 'r') && 
sl@0: 					((buf[1] == '\n') || (buf[1] == '\r')))
sl@0: 					{
sl@0: 					SSL_renegotiate(con);
sl@0: 					i=SSL_do_handshake(con);
sl@0: 					printf("SSL_do_handshake -> %d\n",i);
sl@0: 					
sl@0: 					i=0; /*13; */
sl@0: 					continue;
sl@0: 					/* strcpy(buf,"server side RE-NEGOTIATE\n"); */
sl@0: 					}
sl@0: 				if ((buf[0] == 'R') &&
sl@0: 					((buf[1] == '\n') || (buf[1] == '\r')))
sl@0: 					{
sl@0: 					SSL_set_verify(con,
sl@0: 						SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,NULL);
sl@0: 					SSL_renegotiate(con);
sl@0: 					i=SSL_do_handshake(con);
sl@0: 					printf("SSL_do_handshake -> %d\n",i);
sl@0: 				
sl@0: 					i=0; /* 13; */
sl@0: 					continue;
sl@0: 					/* strcpy(buf,"server side RE-NEGOTIATE asking for client cert\n"); */
sl@0: 					}
sl@0: 				if (buf[0] == 'P')
sl@0: 					{
sl@0: 					static const char *str="Lets print some clear text\n";
sl@0: 					BIO_write(SSL_get_wbio(con),str,strlen(str));
sl@0: 					}
sl@0: 				if (buf[0] == 'S')
sl@0: 					{
sl@0: 					print_stats(bio_s_out,SSL_get_SSL_CTX(con));
sl@0: 					}
sl@0: 				}
sl@0: #ifdef CHARSET_EBCDIC
sl@0: 			ebcdic2ascii(buf,buf,i);
sl@0: #endif
sl@0: 			l=k=0;
sl@0: 			for (;;)
sl@0: 				{
sl@0: 				/* should do a select for the write */
sl@0: #ifdef RENEG
sl@0: { static count=0; if (++count == 100) { count=0; SSL_renegotiate(con); } }
sl@0: #endif
sl@0: 				k=SSL_write(con,&(buf[l]),(unsigned int)i);
sl@0: 				switch (SSL_get_error(con,k))
sl@0: 					{
sl@0: 				case SSL_ERROR_NONE:
sl@0: 					break;
sl@0: 				case SSL_ERROR_WANT_WRITE:
sl@0: 				case SSL_ERROR_WANT_READ:
sl@0: 				case SSL_ERROR_WANT_X509_LOOKUP:
sl@0: 					BIO_printf(bio_s_out,"Write BLOCK\n");
sl@0: 					break;
sl@0: 				case SSL_ERROR_SYSCALL:
sl@0: 				case SSL_ERROR_SSL:
sl@0: 					BIO_printf(bio_s_out,"ERROR\n");
sl@0: 					ERR_print_errors(bio_err);
sl@0: 					ret=1;
sl@0: 					goto err;
sl@0: 					/* break; */
sl@0: 				case SSL_ERROR_ZERO_RETURN:
sl@0: 					BIO_printf(bio_s_out,"DONE\n");
sl@0: 					ret=1;
sl@0: 					goto err;
sl@0: 					}
sl@0: 				l+=k;
sl@0: 				i-=k;
sl@0: 				if (i <= 0) break;
sl@0: 				}
sl@0: 			}
sl@0: 		if (read_from_sslcon)
sl@0: 			{
sl@0: 			if (!SSL_is_init_finished(con))
sl@0: 				{
sl@0: 				i=init_ssl_connection(con);
sl@0: 				
sl@0: 				if (i < 0)
sl@0: 					{
sl@0: 					ret=0;
sl@0: 					goto err;
sl@0: 					}
sl@0: 				else if (i == 0)
sl@0: 					{
sl@0: 					ret=1;
sl@0: 					goto err;
sl@0: 					}
sl@0: 				}
sl@0: 			else
sl@0: 				{
sl@0: again:	
sl@0: 				i=SSL_read(con,(char *)buf,bufsize);
sl@0: 				switch (SSL_get_error(con,i))
sl@0: 					{
sl@0: 				case SSL_ERROR_NONE:
sl@0: #ifdef CHARSET_EBCDIC
sl@0: 					ascii2ebcdic(buf,buf,i);
sl@0: #endif
sl@0: 					write(fileno(stdout),buf,
sl@0: 						(unsigned int)i);
sl@0: 		
sl@0: 					if (SSL_pending(con)) goto again;
sl@0: 					break;
sl@0: 				case SSL_ERROR_WANT_WRITE:
sl@0: 				case SSL_ERROR_WANT_READ:
sl@0: 				case SSL_ERROR_WANT_X509_LOOKUP:
sl@0: 					BIO_printf(bio_s_out,"Read BLOCK\n");
sl@0: 					break;
sl@0: 				case SSL_ERROR_SYSCALL:
sl@0: 				case SSL_ERROR_SSL:
sl@0: 					BIO_printf(bio_s_out,"ERROR\n");
sl@0: 					ERR_print_errors(bio_err);
sl@0: 					ret=1;
sl@0: 					goto err;
sl@0: 				case SSL_ERROR_ZERO_RETURN:
sl@0: 					BIO_printf(bio_s_out,"DONE\n");
sl@0: 					ret=1;
sl@0: 					goto err;
sl@0: 					}
sl@0: 				}
sl@0: 			}
sl@0: 		}
sl@0: err:
sl@0: 	BIO_printf(bio_s_out,"shutting down SSL\n");
sl@0: #if 1
sl@0: 	SSL_set_shutdown(con,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
sl@0: #else
sl@0: 	SSL_shutdown(con);
sl@0: #endif
sl@0: 	if (con != NULL) SSL_free(con);
sl@0: 	BIO_printf(bio_s_out,"CONNECTION CLOSED\n");
sl@0: 	if (buf != NULL)
sl@0: 		{
sl@0: 		OPENSSL_cleanse(buf,bufsize);
sl@0: 		OPENSSL_free(buf);
sl@0: 		}
sl@0: 	if (ret >= 0)
sl@0: 		BIO_printf(bio_s_out,"ACCEPT\n");
sl@0: 	return(ret);
sl@0: 	}
sl@0: 
sl@0: static void close_accept_socket(void)
sl@0: 	{
sl@0: 	BIO_printf(bio_err,"shutdown accept socket\n");
sl@0: 	if (accept_socket >= 0)
sl@0: 		{
sl@0: 		SHUTDOWN2(accept_socket);
sl@0: 		}
sl@0: 	}
sl@0: 
sl@0: static int init_ssl_connection(SSL *con)
sl@0: 	{
sl@0: 	int i;
sl@0: 	const char *str;
sl@0: 	X509 *peer;
sl@0: 	long verify_error;
sl@0: 	MS_STATIC char buf[BUFSIZ];
sl@0: 
sl@0: 	if ((i=SSL_accept(con)) <= 0)
sl@0: 		{
sl@0: 		if (BIO_sock_should_retry(i))
sl@0: 			{
sl@0: 			BIO_printf(bio_s_out,"DELAY\n");
sl@0: 			return(1);
sl@0: 			}
sl@0: 
sl@0: 		BIO_printf(bio_err,"ERROR\n");
sl@0: 		verify_error=SSL_get_verify_result(con);
sl@0: 		if (verify_error != X509_V_OK)
sl@0: 			{
sl@0: 			BIO_printf(bio_err,"verify error:%s\n",
sl@0: 				X509_verify_cert_error_string(verify_error));
sl@0: 			}
sl@0: 		else
sl@0: 			ERR_print_errors(bio_err);
sl@0: 		return(0);
sl@0: 		}
sl@0: 
sl@0: 	PEM_write_bio_SSL_SESSION(bio_s_out,SSL_get_session(con));
sl@0: 
sl@0: 	peer=SSL_get_peer_certificate(con);
sl@0: 	if (peer != NULL)
sl@0: 		{
sl@0: 		BIO_printf(bio_s_out,"Client certificate\n");
sl@0: 		PEM_write_bio_X509(bio_s_out,peer);
sl@0: 		X509_NAME_oneline(X509_get_subject_name(peer),buf,sizeof buf);
sl@0: 		BIO_printf(bio_s_out,"subject=%s\n",buf);
sl@0: 		X509_NAME_oneline(X509_get_issuer_name(peer),buf,sizeof buf);
sl@0: 		BIO_printf(bio_s_out,"issuer=%s\n",buf);
sl@0: 		X509_free(peer);
sl@0: 		}
sl@0: 
sl@0: 	if (SSL_get_shared_ciphers(con,buf,sizeof buf) != NULL)
sl@0: 		BIO_printf(bio_s_out,"Shared ciphers:%s\n",buf);
sl@0: 	str=SSL_CIPHER_get_name(SSL_get_current_cipher(con));
sl@0: 	BIO_printf(bio_s_out,"CIPHER is %s\n",(str != NULL)?str:"(NONE)");
sl@0: 	if (con->hit) BIO_printf(bio_s_out,"Reused session-id\n");
sl@0: 	if (SSL_ctrl(con,SSL_CTRL_GET_FLAGS,0,NULL) &
sl@0: 		TLS1_FLAGS_TLS_PADDING_BUG)
sl@0: 		BIO_printf(bio_s_out,"Peer has incorrect TLSv1 block padding\n");
sl@0: #ifndef OPENSSL_NO_KRB5
sl@0: 	if (con->kssl_ctx->client_princ != NULL)
sl@0: 		{
sl@0: 		BIO_printf(bio_s_out,"Kerberos peer principal is %s\n",
sl@0: 			con->kssl_ctx->client_princ);
sl@0: 		}
sl@0: #endif /* OPENSSL_NO_KRB5 */
sl@0: 	return(1);
sl@0: 	}
sl@0: 
sl@0: #ifndef OPENSSL_NO_DH
sl@0: static DH *load_dh_param(const char *dhfile)
sl@0: 	{
sl@0: 	DH *ret=NULL;
sl@0: 	BIO *bio;
sl@0: 
sl@0: 	if ((bio=BIO_new_file(dhfile,"r")) == NULL)
sl@0: 		goto err;
sl@0: 	ret=PEM_read_bio_DHparams(bio,NULL,NULL,NULL);
sl@0: err:
sl@0: 	if (bio != NULL) BIO_free(bio);
sl@0: 	return(ret);
sl@0: 	}
sl@0: #endif
sl@0: 
sl@0: #if 0
sl@0: static int load_CA(SSL_CTX *ctx, char *file)
sl@0: 	{
sl@0: 	FILE *in;
sl@0: 	X509 *x=NULL;
sl@0: 
sl@0: 	if ((in=fopen(file,"r")) == NULL)
sl@0: 		return(0);
sl@0: 
sl@0: 	for (;;)
sl@0: 		{
sl@0: 		if (PEM_read_X509(in,&x,NULL) == NULL)
sl@0: 			break;
sl@0: 		SSL_CTX_add_client_CA(ctx,x);
sl@0: 		}
sl@0: 	if (x != NULL) X509_free(x);
sl@0: 	fclose(in);
sl@0: 	return(1);
sl@0: 	}
sl@0: #endif
sl@0: 
sl@0: static int www_body(char *hostname, int s, unsigned char *context)
sl@0: 	{
sl@0: 	char *buf=NULL;
sl@0: 	int ret=1;
sl@0: 	int i,j,k,blank,dot;
sl@0: 	struct stat st_buf;
sl@0: 	SSL *con;
sl@0: 	SSL_CIPHER *c;
sl@0: 	BIO *io,*ssl_bio,*sbio;
sl@0: 	long total_bytes;
sl@0: 
sl@0: 	buf=OPENSSL_malloc(bufsize);
sl@0: 	if (buf == NULL) return(0);
sl@0: 	io=BIO_new(BIO_f_buffer());
sl@0: 	ssl_bio=BIO_new(BIO_f_ssl());
sl@0: 	if ((io == NULL) || (ssl_bio == NULL)) goto err;
sl@0: 
sl@0: #ifdef FIONBIO	
sl@0: 	if (s_nbio)
sl@0: 		{
sl@0: 		unsigned long sl=1;
sl@0: 
sl@0: 		if (!s_quiet)
sl@0: 			BIO_printf(bio_err,"turning on non blocking io\n");
sl@0: 		if (BIO_socket_ioctl(s,FIONBIO,&sl) < 0)
sl@0: 			ERR_print_errors(bio_err);
sl@0: 		}
sl@0: #endif
sl@0: 
sl@0: 	/* lets make the output buffer a reasonable size */
sl@0: 	if (!BIO_set_write_buffer_size(io,bufsize)) goto err;
sl@0: 
sl@0: 	if ((con=SSL_new(ctx)) == NULL) goto err;
sl@0: #ifndef OPENSSL_NO_KRB5
sl@0: 	if ((con->kssl_ctx = kssl_ctx_new()) != NULL)
sl@0: 		{
sl@0: 		kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVICE, KRB5SVC);
sl@0: 		kssl_ctx_setstring(con->kssl_ctx, KSSL_KEYTAB, KRB5KEYTAB);
sl@0: 		}
sl@0: #endif	/* OPENSSL_NO_KRB5 */
sl@0: 	if(context) SSL_set_session_id_context(con, context,
sl@0: 					       strlen((char *)context));
sl@0: 
sl@0: 	sbio=BIO_new_socket(s,BIO_NOCLOSE);
sl@0: 	if (s_nbio_test)
sl@0: 		{
sl@0: 		BIO *test;
sl@0: 
sl@0: 		test=BIO_new(BIO_f_nbio_test());
sl@0: 		sbio=BIO_push(test,sbio);
sl@0: 		}
sl@0: 	SSL_set_bio(con,sbio,sbio);
sl@0: 	SSL_set_accept_state(con);
sl@0: 
sl@0: 	/* SSL_set_fd(con,s); */
sl@0: 	BIO_set_ssl(ssl_bio,con,BIO_CLOSE);
sl@0: 	BIO_push(io,ssl_bio);
sl@0: #ifdef CHARSET_EBCDIC
sl@0: 	io = BIO_push(BIO_new(BIO_f_ebcdic_filter()),io);
sl@0: #endif
sl@0: 
sl@0: 	if (s_debug)
sl@0: 		{
sl@0: 		con->debug=1;
sl@0: 		BIO_set_callback(SSL_get_rbio(con),bio_dump_callback);
sl@0: 		BIO_set_callback_arg(SSL_get_rbio(con),(char *)bio_s_out);
sl@0: 		}
sl@0: 	if (s_msg)
sl@0: 		{
sl@0: 		SSL_set_msg_callback(con, msg_cb);
sl@0: 		SSL_set_msg_callback_arg(con, bio_s_out);
sl@0: 		}
sl@0: 
sl@0: 	blank=0;
sl@0: 	for (;;)
sl@0: 		{
sl@0: 		if (hack)
sl@0: 			{
sl@0: 			i=SSL_accept(con);
sl@0: 
sl@0: 			switch (SSL_get_error(con,i))
sl@0: 				{
sl@0: 			case SSL_ERROR_NONE:
sl@0: 				break;
sl@0: 			case SSL_ERROR_WANT_WRITE:
sl@0: 			case SSL_ERROR_WANT_READ:
sl@0: 			case SSL_ERROR_WANT_X509_LOOKUP:
sl@0: 				continue;
sl@0: 			case SSL_ERROR_SYSCALL:
sl@0: 			case SSL_ERROR_SSL:
sl@0: 			case SSL_ERROR_ZERO_RETURN:
sl@0: 				ret=1;
sl@0: 				goto err;
sl@0: 				/* break; */
sl@0: 				}
sl@0: 
sl@0: 			SSL_renegotiate(con);
sl@0: 			SSL_write(con,NULL,0);
sl@0: 			}
sl@0: 
sl@0: 		i=BIO_gets(io,buf,bufsize-1);
sl@0: 		if (i < 0) /* error */
sl@0: 			{
sl@0: 			if (!BIO_should_retry(io))
sl@0: 				{
sl@0: 				if (!s_quiet)
sl@0: 					ERR_print_errors(bio_err);
sl@0: 				goto err;
sl@0: 				}
sl@0: 			else
sl@0: 				{
sl@0: 				BIO_printf(bio_s_out,"read R BLOCK\n");
sl@0: #if defined(OPENSSL_SYS_NETWARE)
sl@0:             delay(1000);
sl@0: #elif !defined(OPENSSL_SYS_MSDOS) && !defined(__DJGPP__)
sl@0: 				sleep(1);
sl@0: #endif
sl@0: 				continue;
sl@0: 				}
sl@0: 			}
sl@0: 		else if (i == 0) /* end of input */
sl@0: 			{
sl@0: 			ret=1;
sl@0: 			goto end;
sl@0: 			}
sl@0: 
sl@0: 		/* else we have data */
sl@0: 		if (	((www == 1) && (strncmp("GET ",buf,4) == 0)) ||
sl@0: 			((www == 2) && (strncmp("GET /stats ",buf,10) == 0)))
sl@0: 			{
sl@0: 			char *p;
sl@0: 			X509 *peer;
sl@0: 			STACK_OF(SSL_CIPHER) *sk;
sl@0: 			static const char *space="                          ";
sl@0: 
sl@0: 			BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
sl@0: 			BIO_puts(io,"<HTML><BODY BGCOLOR=\"#ffffff\">\n");
sl@0: 			BIO_puts(io,"<pre>\n");
sl@0: /*			BIO_puts(io,SSLeay_version(SSLEAY_VERSION));*/
sl@0: 			BIO_puts(io,"\n");
sl@0: 			for (i=0; i<local_argc; i++)
sl@0: 				{
sl@0: 				BIO_puts(io,local_argv[i]);
sl@0: 				BIO_write(io," ",1);
sl@0: 				}
sl@0: 			BIO_puts(io,"\n");
sl@0: 
sl@0: 			/* The following is evil and should not really
sl@0: 			 * be done */
sl@0: 			BIO_printf(io,"Ciphers supported in s_server binary\n");
sl@0: 			sk=SSL_get_ciphers(con);
sl@0: 			j=sk_SSL_CIPHER_num(sk);
sl@0: 			for (i=0; i<j; i++)
sl@0: 				{
sl@0: 				c=sk_SSL_CIPHER_value(sk,i);
sl@0: 				BIO_printf(io,"%-11s:%-25s",
sl@0: 					SSL_CIPHER_get_version(c),
sl@0: 					SSL_CIPHER_get_name(c));
sl@0: 				if ((((i+1)%2) == 0) && (i+1 != j))
sl@0: 					BIO_puts(io,"\n");
sl@0: 				}
sl@0: 			BIO_puts(io,"\n");
sl@0: 			p=SSL_get_shared_ciphers(con,buf,bufsize);
sl@0: 			if (p != NULL)
sl@0: 				{
sl@0: 				BIO_printf(io,"---\nCiphers common between both SSL end points:\n");
sl@0: 				j=i=0;
sl@0: 				while (*p)
sl@0: 					{
sl@0: 					if (*p == ':')
sl@0: 						{
sl@0: 						BIO_write(io,space,26-j);
sl@0: 						i++;
sl@0: 						j=0;
sl@0: 						BIO_write(io,((i%3)?" ":"\n"),1);
sl@0: 						}
sl@0: 					else
sl@0: 						{
sl@0: 						BIO_write(io,p,1);
sl@0: 						j++;
sl@0: 						}
sl@0: 					p++;
sl@0: 					}
sl@0: 				BIO_puts(io,"\n");
sl@0: 				}
sl@0: 			BIO_printf(io,((con->hit)
sl@0: 				?"---\nReused, "
sl@0: 				:"---\nNew, "));
sl@0: 			c=SSL_get_current_cipher(con);
sl@0: 			BIO_printf(io,"%s, Cipher is %s\n",
sl@0: 				SSL_CIPHER_get_version(c),
sl@0: 				SSL_CIPHER_get_name(c));
sl@0: 			SSL_SESSION_print(io,SSL_get_session(con));
sl@0: 			BIO_printf(io,"---\n");
sl@0: 			print_stats(io,SSL_get_SSL_CTX(con));
sl@0: 			BIO_printf(io,"---\n");
sl@0: 			peer=SSL_get_peer_certificate(con);
sl@0: 			if (peer != NULL)
sl@0: 				{
sl@0: 				BIO_printf(io,"Client certificate\n");
sl@0: 				X509_print(io,peer);
sl@0: 				PEM_write_bio_X509(io,peer);
sl@0: 				}
sl@0: 			else
sl@0: 				BIO_puts(io,"no client certificate available\n");
sl@0: 			BIO_puts(io,"</BODY></HTML>\r\n\r\n");
sl@0: 			break;
sl@0: 			}
sl@0: 		else if ((www == 2 || www == 3)
sl@0:                          && (strncmp("GET /",buf,5) == 0))
sl@0: 			{
sl@0: 			BIO *file;
sl@0: 			char *p,*e;
sl@0: 			static const char *text="HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
sl@0: 
sl@0: 			/* skip the '/' */
sl@0: 			p= &(buf[5]);
sl@0: 
sl@0: 			dot = 1;
sl@0: 			for (e=p; *e != '\0'; e++)
sl@0: 				{
sl@0: 				if (e[0] == ' ')
sl@0: 					break;
sl@0: 
sl@0: 				switch (dot)
sl@0: 					{
sl@0: 				case 1:
sl@0: 					dot = (e[0] == '.') ? 2 : 0;
sl@0: 					break;
sl@0: 				case 2:
sl@0: 					dot = (e[0] == '.') ? 3 : 0;
sl@0: 					break;
sl@0: 				case 3:
sl@0: 					dot = (e[0] == '/') ? -1 : 0;
sl@0: 					break;
sl@0: 					}
sl@0: 				if (dot == 0)
sl@0: 					dot = (e[0] == '/') ? 1 : 0;
sl@0: 				}
sl@0: 			dot = (dot == 3) || (dot == -1); /* filename contains ".." component */
sl@0: 
sl@0: 			if (*e == '\0')
sl@0: 				{
sl@0: 				BIO_puts(io,text);
sl@0: 				BIO_printf(io,"'%s' is an invalid file name\r\n",p);
sl@0: 				break;
sl@0: 				}
sl@0: 			*e='\0';
sl@0: 
sl@0: 			if (dot)
sl@0: 				{
sl@0: 				BIO_puts(io,text);
sl@0: 				BIO_printf(io,"'%s' contains '..' reference\r\n",p);
sl@0: 				break;
sl@0: 				}
sl@0: 
sl@0: 			if (*p == '/')
sl@0: 				{
sl@0: 				BIO_puts(io,text);
sl@0: 				BIO_printf(io,"'%s' is an invalid path\r\n",p);
sl@0: 				break;
sl@0: 				}
sl@0: 
sl@0: #if 0
sl@0: 			/* append if a directory lookup */
sl@0: 			if (e[-1] == '/')
sl@0: 				strcat(p,"index.html");
sl@0: #endif
sl@0: 
sl@0: 			/* if a directory, do the index thang */
sl@0: 			if (stat(p,&st_buf) < 0)
sl@0: 				{
sl@0: 				BIO_puts(io,text);
sl@0: 				BIO_printf(io,"Error accessing '%s'\r\n",p);
sl@0: 				ERR_print_errors(io);
sl@0: 				break;
sl@0: 				}
sl@0: 			if (S_ISDIR(st_buf.st_mode))
sl@0: 				{
sl@0: #if 0 /* must check buffer size */
sl@0: 				strcat(p,"/index.html");
sl@0: #else
sl@0: 				BIO_puts(io,text);
sl@0: 				BIO_printf(io,"'%s' is a directory\r\n",p);
sl@0: 				break;
sl@0: #endif
sl@0: 				}
sl@0: 
sl@0: 			if ((file=BIO_new_file(p,"r")) == NULL)
sl@0: 				{
sl@0: 				BIO_puts(io,text);
sl@0: 				BIO_printf(io,"Error opening '%s'\r\n",p);
sl@0: 				ERR_print_errors(io);
sl@0: 				break;
sl@0: 				}
sl@0: 
sl@0: 			if (!s_quiet)
sl@0: 				BIO_printf(bio_err,"FILE:%s\n",p);
sl@0: 
sl@0:                         if (www == 2)
sl@0:                                 {
sl@0:                                 i=strlen(p);
sl@0:                                 if (	((i > 5) && (strcmp(&(p[i-5]),".html") == 0)) ||
sl@0:                                         ((i > 4) && (strcmp(&(p[i-4]),".php") == 0)) ||
sl@0:                                         ((i > 4) && (strcmp(&(p[i-4]),".htm") == 0)))
sl@0:                                         BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
sl@0:                                 else
sl@0:                                         BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
sl@0:                                 }
sl@0: 			/* send the file */
sl@0: 			total_bytes=0;
sl@0: 			for (;;)
sl@0: 				{
sl@0: 				i=BIO_read(file,buf,bufsize);
sl@0: 				if (i <= 0) break;
sl@0: 
sl@0: #ifdef RENEG
sl@0: 				total_bytes+=i;
sl@0: 				fprintf(stderr,"%d\n",i);
sl@0: 				if (total_bytes > 3*1024)
sl@0: 					{
sl@0: 					total_bytes=0;
sl@0: 					fprintf(stderr,"RENEGOTIATE\n");
sl@0: 					SSL_renegotiate(con);
sl@0: 					}
sl@0: 		
sl@0: #endif
sl@0: 
sl@0: 				for (j=0; j<i; )
sl@0: 					{
sl@0: #ifdef RENEG
sl@0: { static count=0; if (++count == 13) { SSL_renegotiate(con); } }
sl@0: #endif
sl@0: 					k=BIO_write(io,&(buf[j]),i-j);
sl@0: 					if (k <= 0)
sl@0: 						{
sl@0: 						if (!BIO_should_retry(io))
sl@0: 							goto write_error;
sl@0: 						else
sl@0: 							{
sl@0: 							BIO_printf(bio_s_out,"rwrite W BLOCK\n");
sl@0: 							}
sl@0: 						}
sl@0: 					else
sl@0: 						{
sl@0: 						j+=k;
sl@0: 						}
sl@0: 					}
sl@0: 				}
sl@0: write_error:
sl@0: 			BIO_free(file);
sl@0: 			break;
sl@0: 			}
sl@0: 		}
sl@0: 
sl@0: 	for (;;)
sl@0: 		{
sl@0: 		i=(int)BIO_flush(io);
sl@0: 		if (i <= 0)
sl@0: 			{
sl@0: 			if (!BIO_should_retry(io))
sl@0: 				break;
sl@0: 			}
sl@0: 		else
sl@0: 			break;
sl@0: 		}
sl@0: end:
sl@0: #if 1
sl@0: 	/* make sure we re-use sessions */
sl@0: 	SSL_set_shutdown(con,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
sl@0: #else
sl@0: 	/* This kills performance */
sl@0: /*	SSL_shutdown(con); A shutdown gets sent in the
sl@0:  *	BIO_free_all(io) procession */
sl@0: #endif
sl@0: 
sl@0: err:
sl@0: 
sl@0: 	if (ret >= 0)
sl@0: 		BIO_printf(bio_s_out,"ACCEPT\n");
sl@0: 
sl@0: 	if (buf != NULL) OPENSSL_free(buf);
sl@0: 	if (io != NULL) BIO_free_all(io);
sl@0: /*	if (ssl_bio != NULL) BIO_free(ssl_bio);*/
sl@0: 	return(ret);
sl@0: 	}
sl@0: 
sl@0: #ifndef OPENSSL_NO_RSA
sl@0: static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength)
sl@0: 	{
sl@0: 	BIGNUM *bn = NULL;
sl@0: 	static RSA *rsa_tmp=NULL;
sl@0: 
sl@0: 	if (!rsa_tmp && ((bn = BN_new()) == NULL))
sl@0: 		BIO_printf(bio_err,"Allocation error in generating RSA key\n");
sl@0: 	if (!rsa_tmp && bn)
sl@0: 		{
sl@0: 		if (!s_quiet)
sl@0: 			{
sl@0: 			BIO_printf(bio_err,"Generating temp (%d bit) RSA key...",keylength);
sl@0: 			(void)BIO_flush(bio_err);
sl@0: 			}
sl@0: 		if(!BN_set_word(bn, RSA_F4) || ((rsa_tmp = RSA_new()) == NULL) ||
sl@0: 				!RSA_generate_key_ex(rsa_tmp, keylength, bn, NULL))
sl@0: 			{
sl@0: 			if(rsa_tmp) RSA_free(rsa_tmp);
sl@0: 			rsa_tmp = NULL;
sl@0: 			}
sl@0: 		if (!s_quiet)
sl@0: 			{
sl@0: 			BIO_printf(bio_err,"\n");
sl@0: 			(void)BIO_flush(bio_err);
sl@0: 			}
sl@0: 		BN_free(bn);
sl@0: 		}
sl@0: 	return(rsa_tmp);
sl@0: 	}
sl@0: #endif
sl@0: 
sl@0: #define MAX_SESSION_ID_ATTEMPTS 10
sl@0: static int generate_session_id(const SSL *ssl, unsigned char *id,
sl@0: 				unsigned int *id_len)
sl@0: 	{
sl@0: 	unsigned int count = 0;
sl@0: 	do	{
sl@0: 		RAND_pseudo_bytes(id, *id_len);
sl@0: 		/* Prefix the session_id with the required prefix. NB: If our
sl@0: 		 * prefix is too long, clip it - but there will be worse effects
sl@0: 		 * anyway, eg. the server could only possibly create 1 session
sl@0: 		 * ID (ie. the prefix!) so all future session negotiations will
sl@0: 		 * fail due to conflicts. */
sl@0: 		memcpy(id, session_id_prefix,
sl@0: 			(strlen(session_id_prefix) < *id_len) ?
sl@0: 			strlen(session_id_prefix) : *id_len);
sl@0: 		}
sl@0: 	while(SSL_has_matching_session_id(ssl, id, *id_len) &&
sl@0: 		(++count < MAX_SESSION_ID_ATTEMPTS));
sl@0: 	if(count >= MAX_SESSION_ID_ATTEMPTS)
sl@0: 		return 0;
sl@0: 	return 1;
sl@0: 	}