xref: /onnv-gate/usr/src/common/openssl/apps/s_server.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* apps/s_server.c */
2*0Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3*0Sstevel@tonic-gate  * All rights reserved.
4*0Sstevel@tonic-gate  *
5*0Sstevel@tonic-gate  * This package is an SSL implementation written
6*0Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
7*0Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
10*0Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
11*0Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
12*0Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13*0Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
14*0Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15*0Sstevel@tonic-gate  *
16*0Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
17*0Sstevel@tonic-gate  * the code are not to be removed.
18*0Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
19*0Sstevel@tonic-gate  * as the author of the parts of the library used.
20*0Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
21*0Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
22*0Sstevel@tonic-gate  *
23*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
24*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
25*0Sstevel@tonic-gate  * are met:
26*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
27*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
28*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
29*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
30*0Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
31*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
32*0Sstevel@tonic-gate  *    must display the following acknowledgement:
33*0Sstevel@tonic-gate  *    "This product includes cryptographic software written by
34*0Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
35*0Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
36*0Sstevel@tonic-gate  *    being used are not cryptographic related :-).
37*0Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
38*0Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
39*0Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42*0Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44*0Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45*0Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46*0Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47*0Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49*0Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50*0Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51*0Sstevel@tonic-gate  * SUCH DAMAGE.
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
54*0Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55*0Sstevel@tonic-gate  * copied and put under another distribution licence
56*0Sstevel@tonic-gate  * [including the GNU Public Licence.]
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate /* ====================================================================
59*0Sstevel@tonic-gate  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
60*0Sstevel@tonic-gate  *
61*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
62*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
63*0Sstevel@tonic-gate  * are met:
64*0Sstevel@tonic-gate  *
65*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
66*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
67*0Sstevel@tonic-gate  *
68*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
69*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
70*0Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
71*0Sstevel@tonic-gate  *    distribution.
72*0Sstevel@tonic-gate  *
73*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
74*0Sstevel@tonic-gate  *    software must display the following acknowledgment:
75*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
76*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77*0Sstevel@tonic-gate  *
78*0Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79*0Sstevel@tonic-gate  *    endorse or promote products derived from this software without
80*0Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
81*0Sstevel@tonic-gate  *    openssl-core@openssl.org.
82*0Sstevel@tonic-gate  *
83*0Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
84*0Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
85*0Sstevel@tonic-gate  *    permission of the OpenSSL Project.
86*0Sstevel@tonic-gate  *
87*0Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
88*0Sstevel@tonic-gate  *    acknowledgment:
89*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
90*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91*0Sstevel@tonic-gate  *
92*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93*0Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95*0Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96*0Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97*0Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98*0Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99*0Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101*0Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102*0Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103*0Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
104*0Sstevel@tonic-gate  * ====================================================================
105*0Sstevel@tonic-gate  *
106*0Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
107*0Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
108*0Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
109*0Sstevel@tonic-gate  *
110*0Sstevel@tonic-gate  */
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate #include <assert.h>
113*0Sstevel@tonic-gate #include <stdio.h>
114*0Sstevel@tonic-gate #include <stdlib.h>
115*0Sstevel@tonic-gate #include <string.h>
116*0Sstevel@tonic-gate #include <sys/types.h>
117*0Sstevel@tonic-gate #include <sys/stat.h>
118*0Sstevel@tonic-gate #include <openssl/e_os2.h>
119*0Sstevel@tonic-gate #ifdef OPENSSL_NO_STDIO
120*0Sstevel@tonic-gate #define APPS_WIN16
121*0Sstevel@tonic-gate #endif
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate /* With IPv6, it looks like Digital has mixed up the proper order of
124*0Sstevel@tonic-gate    recursive header file inclusion, resulting in the compiler complaining
125*0Sstevel@tonic-gate    that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which
126*0Sstevel@tonic-gate    is needed to have fileno() declared correctly...  So let's define u_int */
127*0Sstevel@tonic-gate #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
128*0Sstevel@tonic-gate #define __U_INT
129*0Sstevel@tonic-gate typedef unsigned int u_int;
130*0Sstevel@tonic-gate #endif
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate #include <openssl/lhash.h>
133*0Sstevel@tonic-gate #include <openssl/bn.h>
134*0Sstevel@tonic-gate #define USE_SOCKETS
135*0Sstevel@tonic-gate #include "apps.h"
136*0Sstevel@tonic-gate #include <openssl/err.h>
137*0Sstevel@tonic-gate #include <openssl/pem.h>
138*0Sstevel@tonic-gate #include <openssl/x509.h>
139*0Sstevel@tonic-gate #include <openssl/ssl.h>
140*0Sstevel@tonic-gate #include <openssl/rand.h>
141*0Sstevel@tonic-gate #include "s_apps.h"
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_WINCE
144*0Sstevel@tonic-gate /* Windows CE incorrectly defines fileno as returning void*, so to avoid problems below... */
145*0Sstevel@tonic-gate #ifdef fileno
146*0Sstevel@tonic-gate #undef fileno
147*0Sstevel@tonic-gate #endif
148*0Sstevel@tonic-gate #define fileno(a) (int)_fileno(a)
149*0Sstevel@tonic-gate #endif
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate #if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
152*0Sstevel@tonic-gate /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
153*0Sstevel@tonic-gate #undef FIONBIO
154*0Sstevel@tonic-gate #endif
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
157*0Sstevel@tonic-gate static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength);
158*0Sstevel@tonic-gate #endif
159*0Sstevel@tonic-gate static int sv_body(char *hostname, int s, unsigned char *context);
160*0Sstevel@tonic-gate static int www_body(char *hostname, int s, unsigned char *context);
161*0Sstevel@tonic-gate static void close_accept_socket(void );
162*0Sstevel@tonic-gate static void sv_usage(void);
163*0Sstevel@tonic-gate static int init_ssl_connection(SSL *s);
164*0Sstevel@tonic-gate static void print_stats(BIO *bp,SSL_CTX *ctx);
165*0Sstevel@tonic-gate static int generate_session_id(const SSL *ssl, unsigned char *id,
166*0Sstevel@tonic-gate 				unsigned int *id_len);
167*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
168*0Sstevel@tonic-gate static DH *load_dh_param(char *dhfile);
169*0Sstevel@tonic-gate static DH *get_dh512(void);
170*0Sstevel@tonic-gate #endif
171*0Sstevel@tonic-gate #ifdef MONOLITH
172*0Sstevel@tonic-gate static void s_server_init(void);
173*0Sstevel@tonic-gate #endif
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate #ifndef S_ISDIR
176*0Sstevel@tonic-gate # if defined(_S_IFMT) && defined(_S_IFDIR)
177*0Sstevel@tonic-gate #  define S_ISDIR(a)	(((a) & _S_IFMT) == _S_IFDIR)
178*0Sstevel@tonic-gate # else
179*0Sstevel@tonic-gate #  define S_ISDIR(a)	(((a) & S_IFMT) == S_IFDIR)
180*0Sstevel@tonic-gate # endif
181*0Sstevel@tonic-gate #endif
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
184*0Sstevel@tonic-gate static unsigned char dh512_p[]={
185*0Sstevel@tonic-gate 	0xDA,0x58,0x3C,0x16,0xD9,0x85,0x22,0x89,0xD0,0xE4,0xAF,0x75,
186*0Sstevel@tonic-gate 	0x6F,0x4C,0xCA,0x92,0xDD,0x4B,0xE5,0x33,0xB8,0x04,0xFB,0x0F,
187*0Sstevel@tonic-gate 	0xED,0x94,0xEF,0x9C,0x8A,0x44,0x03,0xED,0x57,0x46,0x50,0xD3,
188*0Sstevel@tonic-gate 	0x69,0x99,0xDB,0x29,0xD7,0x76,0x27,0x6B,0xA2,0xD3,0xD4,0x12,
189*0Sstevel@tonic-gate 	0xE2,0x18,0xF4,0xDD,0x1E,0x08,0x4C,0xF6,0xD8,0x00,0x3E,0x7C,
190*0Sstevel@tonic-gate 	0x47,0x74,0xE8,0x33,
191*0Sstevel@tonic-gate 	};
192*0Sstevel@tonic-gate static unsigned char dh512_g[]={
193*0Sstevel@tonic-gate 	0x02,
194*0Sstevel@tonic-gate 	};
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate static DH *get_dh512(void)
197*0Sstevel@tonic-gate 	{
198*0Sstevel@tonic-gate 	DH *dh=NULL;
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 	if ((dh=DH_new()) == NULL) return(NULL);
201*0Sstevel@tonic-gate 	dh->p=BN_bin2bn(dh512_p,sizeof(dh512_p),NULL);
202*0Sstevel@tonic-gate 	dh->g=BN_bin2bn(dh512_g,sizeof(dh512_g),NULL);
203*0Sstevel@tonic-gate 	if ((dh->p == NULL) || (dh->g == NULL))
204*0Sstevel@tonic-gate 		return(NULL);
205*0Sstevel@tonic-gate 	return(dh);
206*0Sstevel@tonic-gate 	}
207*0Sstevel@tonic-gate #endif
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate /* static int load_CA(SSL_CTX *ctx, char *file);*/
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate #undef BUFSIZZ
212*0Sstevel@tonic-gate #define BUFSIZZ	16*1024
213*0Sstevel@tonic-gate static int bufsize=BUFSIZZ;
214*0Sstevel@tonic-gate static int accept_socket= -1;
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate #define TEST_CERT	"server.pem"
217*0Sstevel@tonic-gate #undef PROG
218*0Sstevel@tonic-gate #define PROG		s_server_main
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate extern int verify_depth;
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate static char *cipher=NULL;
223*0Sstevel@tonic-gate static int s_server_verify=SSL_VERIFY_NONE;
224*0Sstevel@tonic-gate static int s_server_session_id_context = 1; /* anything will do */
225*0Sstevel@tonic-gate static char *s_cert_file=TEST_CERT,*s_key_file=NULL;
226*0Sstevel@tonic-gate static char *s_dcert_file=NULL,*s_dkey_file=NULL;
227*0Sstevel@tonic-gate #ifdef FIONBIO
228*0Sstevel@tonic-gate static int s_nbio=0;
229*0Sstevel@tonic-gate #endif
230*0Sstevel@tonic-gate static int s_nbio_test=0;
231*0Sstevel@tonic-gate int s_crlf=0;
232*0Sstevel@tonic-gate static SSL_CTX *ctx=NULL;
233*0Sstevel@tonic-gate static int www=0;
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate static BIO *bio_s_out=NULL;
236*0Sstevel@tonic-gate static int s_debug=0;
237*0Sstevel@tonic-gate static int s_msg=0;
238*0Sstevel@tonic-gate static int s_quiet=0;
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate static int hack=0;
241*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
242*0Sstevel@tonic-gate static char *engine_id=NULL;
243*0Sstevel@tonic-gate #endif
244*0Sstevel@tonic-gate static const char *session_id_prefix=NULL;
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate #ifdef MONOLITH
247*0Sstevel@tonic-gate static void s_server_init(void)
248*0Sstevel@tonic-gate 	{
249*0Sstevel@tonic-gate 	accept_socket=-1;
250*0Sstevel@tonic-gate 	cipher=NULL;
251*0Sstevel@tonic-gate 	s_server_verify=SSL_VERIFY_NONE;
252*0Sstevel@tonic-gate 	s_dcert_file=NULL;
253*0Sstevel@tonic-gate 	s_dkey_file=NULL;
254*0Sstevel@tonic-gate 	s_cert_file=TEST_CERT;
255*0Sstevel@tonic-gate 	s_key_file=NULL;
256*0Sstevel@tonic-gate #ifdef FIONBIO
257*0Sstevel@tonic-gate 	s_nbio=0;
258*0Sstevel@tonic-gate #endif
259*0Sstevel@tonic-gate 	s_nbio_test=0;
260*0Sstevel@tonic-gate 	ctx=NULL;
261*0Sstevel@tonic-gate 	www=0;
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate 	bio_s_out=NULL;
264*0Sstevel@tonic-gate 	s_debug=0;
265*0Sstevel@tonic-gate 	s_msg=0;
266*0Sstevel@tonic-gate 	s_quiet=0;
267*0Sstevel@tonic-gate 	hack=0;
268*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
269*0Sstevel@tonic-gate 	engine_id=NULL;
270*0Sstevel@tonic-gate #endif
271*0Sstevel@tonic-gate 	}
272*0Sstevel@tonic-gate #endif
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate static void sv_usage(void)
275*0Sstevel@tonic-gate 	{
276*0Sstevel@tonic-gate 	BIO_printf(bio_err,"usage: s_server [args ...]\n");
277*0Sstevel@tonic-gate 	BIO_printf(bio_err,"\n");
278*0Sstevel@tonic-gate 	BIO_printf(bio_err," -accept arg   - port to accept on (default is %d)\n",PORT);
279*0Sstevel@tonic-gate 	BIO_printf(bio_err," -context arg  - set session ID context\n");
280*0Sstevel@tonic-gate 	BIO_printf(bio_err," -verify arg   - turn on peer certificate verification\n");
281*0Sstevel@tonic-gate 	BIO_printf(bio_err," -Verify arg   - turn on peer certificate verification, must have a cert.\n");
282*0Sstevel@tonic-gate 	BIO_printf(bio_err," -cert arg     - certificate file to use, PEM format assumed\n");
283*0Sstevel@tonic-gate 	BIO_printf(bio_err,"                 (default is %s)\n",TEST_CERT);
284*0Sstevel@tonic-gate 	BIO_printf(bio_err," -key arg      - Private Key file to use, PEM format assumed, in cert file if\n");
285*0Sstevel@tonic-gate 	BIO_printf(bio_err,"                 not specified (default is %s)\n",TEST_CERT);
286*0Sstevel@tonic-gate 	BIO_printf(bio_err," -dcert arg    - second certificate file to use (usually for DSA)\n");
287*0Sstevel@tonic-gate 	BIO_printf(bio_err," -dkey arg     - second private key file to use (usually for DSA)\n");
288*0Sstevel@tonic-gate 	BIO_printf(bio_err," -dhparam arg  - DH parameter file to use, in cert file if not specified\n");
289*0Sstevel@tonic-gate 	BIO_printf(bio_err,"                 or a default set of parameters is used\n");
290*0Sstevel@tonic-gate #ifdef FIONBIO
291*0Sstevel@tonic-gate 	BIO_printf(bio_err," -nbio         - Run with non-blocking IO\n");
292*0Sstevel@tonic-gate #endif
293*0Sstevel@tonic-gate 	BIO_printf(bio_err," -nbio_test    - test with the non-blocking test bio\n");
294*0Sstevel@tonic-gate 	BIO_printf(bio_err," -crlf         - convert LF from terminal into CRLF\n");
295*0Sstevel@tonic-gate 	BIO_printf(bio_err," -debug        - Print more output\n");
296*0Sstevel@tonic-gate 	BIO_printf(bio_err," -msg          - Show protocol messages\n");
297*0Sstevel@tonic-gate 	BIO_printf(bio_err," -state        - Print the SSL states\n");
298*0Sstevel@tonic-gate 	BIO_printf(bio_err," -CApath arg   - PEM format directory of CA's\n");
299*0Sstevel@tonic-gate 	BIO_printf(bio_err," -CAfile arg   - PEM format file of CA's\n");
300*0Sstevel@tonic-gate 	BIO_printf(bio_err," -nocert       - Don't use any certificates (Anon-DH)\n");
301*0Sstevel@tonic-gate 	BIO_printf(bio_err," -cipher arg   - play with 'openssl ciphers' to see what goes here\n");
302*0Sstevel@tonic-gate 	BIO_printf(bio_err," -serverpref   - Use server's cipher preferences\n");
303*0Sstevel@tonic-gate 	BIO_printf(bio_err," -quiet        - No server output\n");
304*0Sstevel@tonic-gate 	BIO_printf(bio_err," -no_tmp_rsa   - Do not generate a tmp RSA key\n");
305*0Sstevel@tonic-gate 	BIO_printf(bio_err," -ssl2         - Just talk SSLv2\n");
306*0Sstevel@tonic-gate 	BIO_printf(bio_err," -ssl3         - Just talk SSLv3\n");
307*0Sstevel@tonic-gate 	BIO_printf(bio_err," -tls1         - Just talk TLSv1\n");
308*0Sstevel@tonic-gate 	BIO_printf(bio_err," -no_ssl2      - Just disable SSLv2\n");
309*0Sstevel@tonic-gate 	BIO_printf(bio_err," -no_ssl3      - Just disable SSLv3\n");
310*0Sstevel@tonic-gate 	BIO_printf(bio_err," -no_tls1      - Just disable TLSv1\n");
311*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
312*0Sstevel@tonic-gate 	BIO_printf(bio_err," -no_dhe       - Disable ephemeral DH\n");
313*0Sstevel@tonic-gate #endif
314*0Sstevel@tonic-gate 	BIO_printf(bio_err," -bugs         - Turn on SSL bug compatibility\n");
315*0Sstevel@tonic-gate 	BIO_printf(bio_err," -www          - Respond to a 'GET /' with a status page\n");
316*0Sstevel@tonic-gate 	BIO_printf(bio_err," -WWW          - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
317*0Sstevel@tonic-gate 	BIO_printf(bio_err," -HTTP         - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
318*0Sstevel@tonic-gate         BIO_printf(bio_err,"                 with the assumption it contains a complete HTTP response.\n");
319*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
320*0Sstevel@tonic-gate 	BIO_printf(bio_err," -engine id    - Initialise and use the specified engine\n");
321*0Sstevel@tonic-gate #endif
322*0Sstevel@tonic-gate 	BIO_printf(bio_err," -id_prefix arg - Generate SSL/TLS session IDs prefixed by 'arg'\n");
323*0Sstevel@tonic-gate 	BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
324*0Sstevel@tonic-gate 	}
325*0Sstevel@tonic-gate 
326*0Sstevel@tonic-gate static int local_argc=0;
327*0Sstevel@tonic-gate static char **local_argv;
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate #ifdef CHARSET_EBCDIC
330*0Sstevel@tonic-gate static int ebcdic_new(BIO *bi);
331*0Sstevel@tonic-gate static int ebcdic_free(BIO *a);
332*0Sstevel@tonic-gate static int ebcdic_read(BIO *b, char *out, int outl);
333*0Sstevel@tonic-gate static int ebcdic_write(BIO *b, const char *in, int inl);
334*0Sstevel@tonic-gate static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr);
335*0Sstevel@tonic-gate static int ebcdic_gets(BIO *bp, char *buf, int size);
336*0Sstevel@tonic-gate static int ebcdic_puts(BIO *bp, const char *str);
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate #define BIO_TYPE_EBCDIC_FILTER	(18|0x0200)
339*0Sstevel@tonic-gate static BIO_METHOD methods_ebcdic=
340*0Sstevel@tonic-gate 	{
341*0Sstevel@tonic-gate 	BIO_TYPE_EBCDIC_FILTER,
342*0Sstevel@tonic-gate 	"EBCDIC/ASCII filter",
343*0Sstevel@tonic-gate 	ebcdic_write,
344*0Sstevel@tonic-gate 	ebcdic_read,
345*0Sstevel@tonic-gate 	ebcdic_puts,
346*0Sstevel@tonic-gate 	ebcdic_gets,
347*0Sstevel@tonic-gate 	ebcdic_ctrl,
348*0Sstevel@tonic-gate 	ebcdic_new,
349*0Sstevel@tonic-gate 	ebcdic_free,
350*0Sstevel@tonic-gate 	};
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate typedef struct
353*0Sstevel@tonic-gate {
354*0Sstevel@tonic-gate 	size_t	alloced;
355*0Sstevel@tonic-gate 	char	buff[1];
356*0Sstevel@tonic-gate } EBCDIC_OUTBUFF;
357*0Sstevel@tonic-gate 
358*0Sstevel@tonic-gate BIO_METHOD *BIO_f_ebcdic_filter()
359*0Sstevel@tonic-gate {
360*0Sstevel@tonic-gate 	return(&methods_ebcdic);
361*0Sstevel@tonic-gate }
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate static int ebcdic_new(BIO *bi)
364*0Sstevel@tonic-gate {
365*0Sstevel@tonic-gate 	EBCDIC_OUTBUFF *wbuf;
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate 	wbuf = (EBCDIC_OUTBUFF *)OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + 1024);
368*0Sstevel@tonic-gate 	wbuf->alloced = 1024;
369*0Sstevel@tonic-gate 	wbuf->buff[0] = '\0';
370*0Sstevel@tonic-gate 
371*0Sstevel@tonic-gate 	bi->ptr=(char *)wbuf;
372*0Sstevel@tonic-gate 	bi->init=1;
373*0Sstevel@tonic-gate 	bi->flags=0;
374*0Sstevel@tonic-gate 	return(1);
375*0Sstevel@tonic-gate }
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate static int ebcdic_free(BIO *a)
378*0Sstevel@tonic-gate {
379*0Sstevel@tonic-gate 	if (a == NULL) return(0);
380*0Sstevel@tonic-gate 	if (a->ptr != NULL)
381*0Sstevel@tonic-gate 		OPENSSL_free(a->ptr);
382*0Sstevel@tonic-gate 	a->ptr=NULL;
383*0Sstevel@tonic-gate 	a->init=0;
384*0Sstevel@tonic-gate 	a->flags=0;
385*0Sstevel@tonic-gate 	return(1);
386*0Sstevel@tonic-gate }
387*0Sstevel@tonic-gate 
388*0Sstevel@tonic-gate static int ebcdic_read(BIO *b, char *out, int outl)
389*0Sstevel@tonic-gate {
390*0Sstevel@tonic-gate 	int ret=0;
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate 	if (out == NULL || outl == 0) return(0);
393*0Sstevel@tonic-gate 	if (b->next_bio == NULL) return(0);
394*0Sstevel@tonic-gate 
395*0Sstevel@tonic-gate 	ret=BIO_read(b->next_bio,out,outl);
396*0Sstevel@tonic-gate 	if (ret > 0)
397*0Sstevel@tonic-gate 		ascii2ebcdic(out,out,ret);
398*0Sstevel@tonic-gate 	return(ret);
399*0Sstevel@tonic-gate }
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate static int ebcdic_write(BIO *b, const char *in, int inl)
402*0Sstevel@tonic-gate {
403*0Sstevel@tonic-gate 	EBCDIC_OUTBUFF *wbuf;
404*0Sstevel@tonic-gate 	int ret=0;
405*0Sstevel@tonic-gate 	int num;
406*0Sstevel@tonic-gate 	unsigned char n;
407*0Sstevel@tonic-gate 
408*0Sstevel@tonic-gate 	if ((in == NULL) || (inl <= 0)) return(0);
409*0Sstevel@tonic-gate 	if (b->next_bio == NULL) return(0);
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate 	wbuf=(EBCDIC_OUTBUFF *)b->ptr;
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate 	if (inl > (num = wbuf->alloced))
414*0Sstevel@tonic-gate 	{
415*0Sstevel@tonic-gate 		num = num + num;  /* double the size */
416*0Sstevel@tonic-gate 		if (num < inl)
417*0Sstevel@tonic-gate 			num = inl;
418*0Sstevel@tonic-gate 		OPENSSL_free(wbuf);
419*0Sstevel@tonic-gate 		wbuf=(EBCDIC_OUTBUFF *)OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + num);
420*0Sstevel@tonic-gate 
421*0Sstevel@tonic-gate 		wbuf->alloced = num;
422*0Sstevel@tonic-gate 		wbuf->buff[0] = '\0';
423*0Sstevel@tonic-gate 
424*0Sstevel@tonic-gate 		b->ptr=(char *)wbuf;
425*0Sstevel@tonic-gate 	}
426*0Sstevel@tonic-gate 
427*0Sstevel@tonic-gate 	ebcdic2ascii(wbuf->buff, in, inl);
428*0Sstevel@tonic-gate 
429*0Sstevel@tonic-gate 	ret=BIO_write(b->next_bio, wbuf->buff, inl);
430*0Sstevel@tonic-gate 
431*0Sstevel@tonic-gate 	return(ret);
432*0Sstevel@tonic-gate }
433*0Sstevel@tonic-gate 
434*0Sstevel@tonic-gate static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr)
435*0Sstevel@tonic-gate {
436*0Sstevel@tonic-gate 	long ret;
437*0Sstevel@tonic-gate 
438*0Sstevel@tonic-gate 	if (b->next_bio == NULL) return(0);
439*0Sstevel@tonic-gate 	switch (cmd)
440*0Sstevel@tonic-gate 	{
441*0Sstevel@tonic-gate 	case BIO_CTRL_DUP:
442*0Sstevel@tonic-gate 		ret=0L;
443*0Sstevel@tonic-gate 		break;
444*0Sstevel@tonic-gate 	default:
445*0Sstevel@tonic-gate 		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
446*0Sstevel@tonic-gate 		break;
447*0Sstevel@tonic-gate 	}
448*0Sstevel@tonic-gate 	return(ret);
449*0Sstevel@tonic-gate }
450*0Sstevel@tonic-gate 
451*0Sstevel@tonic-gate static int ebcdic_gets(BIO *bp, char *buf, int size)
452*0Sstevel@tonic-gate {
453*0Sstevel@tonic-gate 	int i, ret=0;
454*0Sstevel@tonic-gate 	if (bp->next_bio == NULL) return(0);
455*0Sstevel@tonic-gate /*	return(BIO_gets(bp->next_bio,buf,size));*/
456*0Sstevel@tonic-gate 	for (i=0; i<size-1; ++i)
457*0Sstevel@tonic-gate 	{
458*0Sstevel@tonic-gate 		ret = ebcdic_read(bp,&buf[i],1);
459*0Sstevel@tonic-gate 		if (ret <= 0)
460*0Sstevel@tonic-gate 			break;
461*0Sstevel@tonic-gate 		else if (buf[i] == '\n')
462*0Sstevel@tonic-gate 		{
463*0Sstevel@tonic-gate 			++i;
464*0Sstevel@tonic-gate 			break;
465*0Sstevel@tonic-gate 		}
466*0Sstevel@tonic-gate 	}
467*0Sstevel@tonic-gate 	if (i < size)
468*0Sstevel@tonic-gate 		buf[i] = '\0';
469*0Sstevel@tonic-gate 	return (ret < 0 && i == 0) ? ret : i;
470*0Sstevel@tonic-gate }
471*0Sstevel@tonic-gate 
472*0Sstevel@tonic-gate static int ebcdic_puts(BIO *bp, const char *str)
473*0Sstevel@tonic-gate {
474*0Sstevel@tonic-gate 	if (bp->next_bio == NULL) return(0);
475*0Sstevel@tonic-gate 	return ebcdic_write(bp, str, strlen(str));
476*0Sstevel@tonic-gate }
477*0Sstevel@tonic-gate #endif
478*0Sstevel@tonic-gate 
479*0Sstevel@tonic-gate int MAIN(int, char **);
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate int MAIN(int argc, char *argv[])
482*0Sstevel@tonic-gate 	{
483*0Sstevel@tonic-gate 	X509_STORE *store = NULL;
484*0Sstevel@tonic-gate 	int vflags = 0;
485*0Sstevel@tonic-gate 	short port=PORT;
486*0Sstevel@tonic-gate 	char *CApath=NULL,*CAfile=NULL;
487*0Sstevel@tonic-gate 	char *context = NULL;
488*0Sstevel@tonic-gate 	char *dhfile = NULL;
489*0Sstevel@tonic-gate 	int badop=0,bugs=0;
490*0Sstevel@tonic-gate 	int ret=1;
491*0Sstevel@tonic-gate 	int off=0;
492*0Sstevel@tonic-gate 	int no_tmp_rsa=0,no_dhe=0,nocert=0;
493*0Sstevel@tonic-gate 	int state=0;
494*0Sstevel@tonic-gate 	SSL_METHOD *meth=NULL;
495*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
496*0Sstevel@tonic-gate 	ENGINE *e=NULL;
497*0Sstevel@tonic-gate #endif
498*0Sstevel@tonic-gate 	char *inrand=NULL;
499*0Sstevel@tonic-gate 
500*0Sstevel@tonic-gate #if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
501*0Sstevel@tonic-gate 	meth=SSLv23_server_method();
502*0Sstevel@tonic-gate #elif !defined(OPENSSL_NO_SSL3)
503*0Sstevel@tonic-gate 	meth=SSLv3_server_method();
504*0Sstevel@tonic-gate #elif !defined(OPENSSL_NO_SSL2)
505*0Sstevel@tonic-gate 	meth=SSLv2_server_method();
506*0Sstevel@tonic-gate #endif
507*0Sstevel@tonic-gate 
508*0Sstevel@tonic-gate 	local_argc=argc;
509*0Sstevel@tonic-gate 	local_argv=argv;
510*0Sstevel@tonic-gate 
511*0Sstevel@tonic-gate 	apps_startup();
512*0Sstevel@tonic-gate #ifdef MONOLITH
513*0Sstevel@tonic-gate 	s_server_init();
514*0Sstevel@tonic-gate #endif
515*0Sstevel@tonic-gate 
516*0Sstevel@tonic-gate 	if (bio_err == NULL)
517*0Sstevel@tonic-gate 		bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
518*0Sstevel@tonic-gate 
519*0Sstevel@tonic-gate 	if (!load_config(bio_err, NULL))
520*0Sstevel@tonic-gate 		goto end;
521*0Sstevel@tonic-gate 
522*0Sstevel@tonic-gate 	verify_depth=0;
523*0Sstevel@tonic-gate #ifdef FIONBIO
524*0Sstevel@tonic-gate 	s_nbio=0;
525*0Sstevel@tonic-gate #endif
526*0Sstevel@tonic-gate 	s_nbio_test=0;
527*0Sstevel@tonic-gate 
528*0Sstevel@tonic-gate 	argc--;
529*0Sstevel@tonic-gate 	argv++;
530*0Sstevel@tonic-gate 
531*0Sstevel@tonic-gate 	while (argc >= 1)
532*0Sstevel@tonic-gate 		{
533*0Sstevel@tonic-gate 		if	((strcmp(*argv,"-port") == 0) ||
534*0Sstevel@tonic-gate 			 (strcmp(*argv,"-accept") == 0))
535*0Sstevel@tonic-gate 			{
536*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
537*0Sstevel@tonic-gate 			if (!extract_port(*(++argv),&port))
538*0Sstevel@tonic-gate 				goto bad;
539*0Sstevel@tonic-gate 			}
540*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-verify") == 0)
541*0Sstevel@tonic-gate 			{
542*0Sstevel@tonic-gate 			s_server_verify=SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE;
543*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
544*0Sstevel@tonic-gate 			verify_depth=atoi(*(++argv));
545*0Sstevel@tonic-gate 			BIO_printf(bio_err,"verify depth is %d\n",verify_depth);
546*0Sstevel@tonic-gate 			}
547*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-Verify") == 0)
548*0Sstevel@tonic-gate 			{
549*0Sstevel@tonic-gate 			s_server_verify=SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT|
550*0Sstevel@tonic-gate 				SSL_VERIFY_CLIENT_ONCE;
551*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
552*0Sstevel@tonic-gate 			verify_depth=atoi(*(++argv));
553*0Sstevel@tonic-gate 			BIO_printf(bio_err,"verify depth is %d, must return a certificate\n",verify_depth);
554*0Sstevel@tonic-gate 			}
555*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-context") == 0)
556*0Sstevel@tonic-gate 			{
557*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
558*0Sstevel@tonic-gate 			context= *(++argv);
559*0Sstevel@tonic-gate 			}
560*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-cert") == 0)
561*0Sstevel@tonic-gate 			{
562*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
563*0Sstevel@tonic-gate 			s_cert_file= *(++argv);
564*0Sstevel@tonic-gate 			}
565*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-key") == 0)
566*0Sstevel@tonic-gate 			{
567*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
568*0Sstevel@tonic-gate 			s_key_file= *(++argv);
569*0Sstevel@tonic-gate 			}
570*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-dhparam") == 0)
571*0Sstevel@tonic-gate 			{
572*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
573*0Sstevel@tonic-gate 			dhfile = *(++argv);
574*0Sstevel@tonic-gate 			}
575*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-dcert") == 0)
576*0Sstevel@tonic-gate 			{
577*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
578*0Sstevel@tonic-gate 			s_dcert_file= *(++argv);
579*0Sstevel@tonic-gate 			}
580*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-dkey") == 0)
581*0Sstevel@tonic-gate 			{
582*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
583*0Sstevel@tonic-gate 			s_dkey_file= *(++argv);
584*0Sstevel@tonic-gate 			}
585*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-nocert") == 0)
586*0Sstevel@tonic-gate 			{
587*0Sstevel@tonic-gate 			nocert=1;
588*0Sstevel@tonic-gate 			}
589*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-CApath") == 0)
590*0Sstevel@tonic-gate 			{
591*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
592*0Sstevel@tonic-gate 			CApath= *(++argv);
593*0Sstevel@tonic-gate 			}
594*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-crl_check") == 0)
595*0Sstevel@tonic-gate 			{
596*0Sstevel@tonic-gate 			vflags |= X509_V_FLAG_CRL_CHECK;
597*0Sstevel@tonic-gate 			}
598*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-crl_check") == 0)
599*0Sstevel@tonic-gate 			{
600*0Sstevel@tonic-gate 			vflags |= X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL;
601*0Sstevel@tonic-gate 			}
602*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-serverpref") == 0)
603*0Sstevel@tonic-gate 			{ off|=SSL_OP_CIPHER_SERVER_PREFERENCE; }
604*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-cipher") == 0)
605*0Sstevel@tonic-gate 			{
606*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
607*0Sstevel@tonic-gate 			cipher= *(++argv);
608*0Sstevel@tonic-gate 			}
609*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-CAfile") == 0)
610*0Sstevel@tonic-gate 			{
611*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
612*0Sstevel@tonic-gate 			CAfile= *(++argv);
613*0Sstevel@tonic-gate 			}
614*0Sstevel@tonic-gate #ifdef FIONBIO
615*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-nbio") == 0)
616*0Sstevel@tonic-gate 			{ s_nbio=1; }
617*0Sstevel@tonic-gate #endif
618*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-nbio_test") == 0)
619*0Sstevel@tonic-gate 			{
620*0Sstevel@tonic-gate #ifdef FIONBIO
621*0Sstevel@tonic-gate 			s_nbio=1;
622*0Sstevel@tonic-gate #endif
623*0Sstevel@tonic-gate 			s_nbio_test=1;
624*0Sstevel@tonic-gate 			}
625*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-debug") == 0)
626*0Sstevel@tonic-gate 			{ s_debug=1; }
627*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-msg") == 0)
628*0Sstevel@tonic-gate 			{ s_msg=1; }
629*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-hack") == 0)
630*0Sstevel@tonic-gate 			{ hack=1; }
631*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-state") == 0)
632*0Sstevel@tonic-gate 			{ state=1; }
633*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-crlf") == 0)
634*0Sstevel@tonic-gate 			{ s_crlf=1; }
635*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-quiet") == 0)
636*0Sstevel@tonic-gate 			{ s_quiet=1; }
637*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-bugs") == 0)
638*0Sstevel@tonic-gate 			{ bugs=1; }
639*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-no_tmp_rsa") == 0)
640*0Sstevel@tonic-gate 			{ no_tmp_rsa=1; }
641*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-no_dhe") == 0)
642*0Sstevel@tonic-gate 			{ no_dhe=1; }
643*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-www") == 0)
644*0Sstevel@tonic-gate 			{ www=1; }
645*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-WWW") == 0)
646*0Sstevel@tonic-gate 			{ www=2; }
647*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-HTTP") == 0)
648*0Sstevel@tonic-gate 			{ www=3; }
649*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-no_ssl2") == 0)
650*0Sstevel@tonic-gate 			{ off|=SSL_OP_NO_SSLv2; }
651*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-no_ssl3") == 0)
652*0Sstevel@tonic-gate 			{ off|=SSL_OP_NO_SSLv3; }
653*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-no_tls1") == 0)
654*0Sstevel@tonic-gate 			{ off|=SSL_OP_NO_TLSv1; }
655*0Sstevel@tonic-gate #ifndef OPENSSL_NO_SSL2
656*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-ssl2") == 0)
657*0Sstevel@tonic-gate 			{ meth=SSLv2_server_method(); }
658*0Sstevel@tonic-gate #endif
659*0Sstevel@tonic-gate #ifndef OPENSSL_NO_SSL3
660*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-ssl3") == 0)
661*0Sstevel@tonic-gate 			{ meth=SSLv3_server_method(); }
662*0Sstevel@tonic-gate #endif
663*0Sstevel@tonic-gate #ifndef OPENSSL_NO_TLS1
664*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-tls1") == 0)
665*0Sstevel@tonic-gate 			{ meth=TLSv1_server_method(); }
666*0Sstevel@tonic-gate #endif
667*0Sstevel@tonic-gate 		else if (strcmp(*argv, "-id_prefix") == 0)
668*0Sstevel@tonic-gate 			{
669*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
670*0Sstevel@tonic-gate 			session_id_prefix = *(++argv);
671*0Sstevel@tonic-gate 			}
672*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
673*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-engine") == 0)
674*0Sstevel@tonic-gate 			{
675*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
676*0Sstevel@tonic-gate 			engine_id= *(++argv);
677*0Sstevel@tonic-gate 			}
678*0Sstevel@tonic-gate #endif
679*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-rand") == 0)
680*0Sstevel@tonic-gate 			{
681*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
682*0Sstevel@tonic-gate 			inrand= *(++argv);
683*0Sstevel@tonic-gate 			}
684*0Sstevel@tonic-gate 		else
685*0Sstevel@tonic-gate 			{
686*0Sstevel@tonic-gate 			BIO_printf(bio_err,"unknown option %s\n",*argv);
687*0Sstevel@tonic-gate 			badop=1;
688*0Sstevel@tonic-gate 			break;
689*0Sstevel@tonic-gate 			}
690*0Sstevel@tonic-gate 		argc--;
691*0Sstevel@tonic-gate 		argv++;
692*0Sstevel@tonic-gate 		}
693*0Sstevel@tonic-gate 	if (badop)
694*0Sstevel@tonic-gate 		{
695*0Sstevel@tonic-gate bad:
696*0Sstevel@tonic-gate 		sv_usage();
697*0Sstevel@tonic-gate 		goto end;
698*0Sstevel@tonic-gate 		}
699*0Sstevel@tonic-gate 
700*0Sstevel@tonic-gate 	SSL_load_error_strings();
701*0Sstevel@tonic-gate 	OpenSSL_add_ssl_algorithms();
702*0Sstevel@tonic-gate 
703*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
704*0Sstevel@tonic-gate         e = setup_engine(bio_err, engine_id, 1);
705*0Sstevel@tonic-gate #endif
706*0Sstevel@tonic-gate 
707*0Sstevel@tonic-gate 	if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL
708*0Sstevel@tonic-gate 		&& !RAND_status())
709*0Sstevel@tonic-gate 		{
710*0Sstevel@tonic-gate 		BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
711*0Sstevel@tonic-gate 		}
712*0Sstevel@tonic-gate 	if (inrand != NULL)
713*0Sstevel@tonic-gate 		BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
714*0Sstevel@tonic-gate 			app_RAND_load_files(inrand));
715*0Sstevel@tonic-gate 
716*0Sstevel@tonic-gate 	if (bio_s_out == NULL)
717*0Sstevel@tonic-gate 		{
718*0Sstevel@tonic-gate 		if (s_quiet && !s_debug && !s_msg)
719*0Sstevel@tonic-gate 			{
720*0Sstevel@tonic-gate 			bio_s_out=BIO_new(BIO_s_null());
721*0Sstevel@tonic-gate 			}
722*0Sstevel@tonic-gate 		else
723*0Sstevel@tonic-gate 			{
724*0Sstevel@tonic-gate 			if (bio_s_out == NULL)
725*0Sstevel@tonic-gate 				bio_s_out=BIO_new_fp(stdout,BIO_NOCLOSE);
726*0Sstevel@tonic-gate 			}
727*0Sstevel@tonic-gate 		}
728*0Sstevel@tonic-gate 
729*0Sstevel@tonic-gate #if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA)
730*0Sstevel@tonic-gate 	if (nocert)
731*0Sstevel@tonic-gate #endif
732*0Sstevel@tonic-gate 		{
733*0Sstevel@tonic-gate 		s_cert_file=NULL;
734*0Sstevel@tonic-gate 		s_key_file=NULL;
735*0Sstevel@tonic-gate 		s_dcert_file=NULL;
736*0Sstevel@tonic-gate 		s_dkey_file=NULL;
737*0Sstevel@tonic-gate 		}
738*0Sstevel@tonic-gate 
739*0Sstevel@tonic-gate 	ctx=SSL_CTX_new(meth);
740*0Sstevel@tonic-gate 	if (ctx == NULL)
741*0Sstevel@tonic-gate 		{
742*0Sstevel@tonic-gate 		ERR_print_errors(bio_err);
743*0Sstevel@tonic-gate 		goto end;
744*0Sstevel@tonic-gate 		}
745*0Sstevel@tonic-gate 	if (session_id_prefix)
746*0Sstevel@tonic-gate 		{
747*0Sstevel@tonic-gate 		if(strlen(session_id_prefix) >= 32)
748*0Sstevel@tonic-gate 			BIO_printf(bio_err,
749*0Sstevel@tonic-gate "warning: id_prefix is too long, only one new session will be possible\n");
750*0Sstevel@tonic-gate 		else if(strlen(session_id_prefix) >= 16)
751*0Sstevel@tonic-gate 			BIO_printf(bio_err,
752*0Sstevel@tonic-gate "warning: id_prefix is too long if you use SSLv2\n");
753*0Sstevel@tonic-gate 		if(!SSL_CTX_set_generate_session_id(ctx, generate_session_id))
754*0Sstevel@tonic-gate 			{
755*0Sstevel@tonic-gate 			BIO_printf(bio_err,"error setting 'id_prefix'\n");
756*0Sstevel@tonic-gate 			ERR_print_errors(bio_err);
757*0Sstevel@tonic-gate 			goto end;
758*0Sstevel@tonic-gate 			}
759*0Sstevel@tonic-gate 		BIO_printf(bio_err,"id_prefix '%s' set.\n", session_id_prefix);
760*0Sstevel@tonic-gate 		}
761*0Sstevel@tonic-gate 	SSL_CTX_set_quiet_shutdown(ctx,1);
762*0Sstevel@tonic-gate 	if (bugs) SSL_CTX_set_options(ctx,SSL_OP_ALL);
763*0Sstevel@tonic-gate 	if (hack) SSL_CTX_set_options(ctx,SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG);
764*0Sstevel@tonic-gate 	SSL_CTX_set_options(ctx,off);
765*0Sstevel@tonic-gate 
766*0Sstevel@tonic-gate 	if (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback);
767*0Sstevel@tonic-gate 
768*0Sstevel@tonic-gate 	SSL_CTX_sess_set_cache_size(ctx,128);
769*0Sstevel@tonic-gate 
770*0Sstevel@tonic-gate #if 0
771*0Sstevel@tonic-gate 	if (cipher == NULL) cipher=getenv("SSL_CIPHER");
772*0Sstevel@tonic-gate #endif
773*0Sstevel@tonic-gate 
774*0Sstevel@tonic-gate #if 0
775*0Sstevel@tonic-gate 	if (s_cert_file == NULL)
776*0Sstevel@tonic-gate 		{
777*0Sstevel@tonic-gate 		BIO_printf(bio_err,"You must specify a certificate file for the server to use\n");
778*0Sstevel@tonic-gate 		goto end;
779*0Sstevel@tonic-gate 		}
780*0Sstevel@tonic-gate #endif
781*0Sstevel@tonic-gate 
782*0Sstevel@tonic-gate 	if ((!SSL_CTX_load_verify_locations(ctx,CAfile,CApath)) ||
783*0Sstevel@tonic-gate 		(!SSL_CTX_set_default_verify_paths(ctx)))
784*0Sstevel@tonic-gate 		{
785*0Sstevel@tonic-gate 		/* BIO_printf(bio_err,"X509_load_verify_locations\n"); */
786*0Sstevel@tonic-gate 		ERR_print_errors(bio_err);
787*0Sstevel@tonic-gate 		/* goto end; */
788*0Sstevel@tonic-gate 		}
789*0Sstevel@tonic-gate 	store = SSL_CTX_get_cert_store(ctx);
790*0Sstevel@tonic-gate 	X509_STORE_set_flags(store, vflags);
791*0Sstevel@tonic-gate 
792*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
793*0Sstevel@tonic-gate 	if (!no_dhe)
794*0Sstevel@tonic-gate 		{
795*0Sstevel@tonic-gate 		DH *dh=NULL;
796*0Sstevel@tonic-gate 
797*0Sstevel@tonic-gate 		if (dhfile)
798*0Sstevel@tonic-gate 			dh = load_dh_param(dhfile);
799*0Sstevel@tonic-gate 		else if (s_cert_file)
800*0Sstevel@tonic-gate 			dh = load_dh_param(s_cert_file);
801*0Sstevel@tonic-gate 
802*0Sstevel@tonic-gate 		if (dh != NULL)
803*0Sstevel@tonic-gate 			{
804*0Sstevel@tonic-gate 			BIO_printf(bio_s_out,"Setting temp DH parameters\n");
805*0Sstevel@tonic-gate 			}
806*0Sstevel@tonic-gate 		else
807*0Sstevel@tonic-gate 			{
808*0Sstevel@tonic-gate 			BIO_printf(bio_s_out,"Using default temp DH parameters\n");
809*0Sstevel@tonic-gate 			dh=get_dh512();
810*0Sstevel@tonic-gate 			}
811*0Sstevel@tonic-gate 		(void)BIO_flush(bio_s_out);
812*0Sstevel@tonic-gate 
813*0Sstevel@tonic-gate 		SSL_CTX_set_tmp_dh(ctx,dh);
814*0Sstevel@tonic-gate 		DH_free(dh);
815*0Sstevel@tonic-gate 		}
816*0Sstevel@tonic-gate #endif
817*0Sstevel@tonic-gate 
818*0Sstevel@tonic-gate 	if (!set_cert_stuff(ctx,s_cert_file,s_key_file))
819*0Sstevel@tonic-gate 		goto end;
820*0Sstevel@tonic-gate 	if (s_dcert_file != NULL)
821*0Sstevel@tonic-gate 		{
822*0Sstevel@tonic-gate 		if (!set_cert_stuff(ctx,s_dcert_file,s_dkey_file))
823*0Sstevel@tonic-gate 			goto end;
824*0Sstevel@tonic-gate 		}
825*0Sstevel@tonic-gate 
826*0Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
827*0Sstevel@tonic-gate #if 1
828*0Sstevel@tonic-gate 	if (!no_tmp_rsa)
829*0Sstevel@tonic-gate 		SSL_CTX_set_tmp_rsa_callback(ctx,tmp_rsa_cb);
830*0Sstevel@tonic-gate #else
831*0Sstevel@tonic-gate 	if (!no_tmp_rsa && SSL_CTX_need_tmp_RSA(ctx))
832*0Sstevel@tonic-gate 		{
833*0Sstevel@tonic-gate 		RSA *rsa;
834*0Sstevel@tonic-gate 
835*0Sstevel@tonic-gate 		BIO_printf(bio_s_out,"Generating temp (512 bit) RSA key...");
836*0Sstevel@tonic-gate 		BIO_flush(bio_s_out);
837*0Sstevel@tonic-gate 
838*0Sstevel@tonic-gate 		rsa=RSA_generate_key(512,RSA_F4,NULL);
839*0Sstevel@tonic-gate 
840*0Sstevel@tonic-gate 		if (!SSL_CTX_set_tmp_rsa(ctx,rsa))
841*0Sstevel@tonic-gate 			{
842*0Sstevel@tonic-gate 			ERR_print_errors(bio_err);
843*0Sstevel@tonic-gate 			goto end;
844*0Sstevel@tonic-gate 			}
845*0Sstevel@tonic-gate 		RSA_free(rsa);
846*0Sstevel@tonic-gate 		BIO_printf(bio_s_out,"\n");
847*0Sstevel@tonic-gate 		}
848*0Sstevel@tonic-gate #endif
849*0Sstevel@tonic-gate #endif
850*0Sstevel@tonic-gate 
851*0Sstevel@tonic-gate 	if (cipher != NULL)
852*0Sstevel@tonic-gate 		if(!SSL_CTX_set_cipher_list(ctx,cipher)) {
853*0Sstevel@tonic-gate 		BIO_printf(bio_err,"error setting cipher list\n");
854*0Sstevel@tonic-gate 		ERR_print_errors(bio_err);
855*0Sstevel@tonic-gate 		goto end;
856*0Sstevel@tonic-gate 	}
857*0Sstevel@tonic-gate 	SSL_CTX_set_verify(ctx,s_server_verify,verify_callback);
858*0Sstevel@tonic-gate 	SSL_CTX_set_session_id_context(ctx,(void*)&s_server_session_id_context,
859*0Sstevel@tonic-gate 		sizeof s_server_session_id_context);
860*0Sstevel@tonic-gate 
861*0Sstevel@tonic-gate 	if (CAfile != NULL)
862*0Sstevel@tonic-gate 	    SSL_CTX_set_client_CA_list(ctx,SSL_load_client_CA_file(CAfile));
863*0Sstevel@tonic-gate 
864*0Sstevel@tonic-gate 	BIO_printf(bio_s_out,"ACCEPT\n");
865*0Sstevel@tonic-gate 	if (www)
866*0Sstevel@tonic-gate 		do_server(port,&accept_socket,www_body, context);
867*0Sstevel@tonic-gate 	else
868*0Sstevel@tonic-gate 		do_server(port,&accept_socket,sv_body, context);
869*0Sstevel@tonic-gate 	print_stats(bio_s_out,ctx);
870*0Sstevel@tonic-gate 	ret=0;
871*0Sstevel@tonic-gate end:
872*0Sstevel@tonic-gate 	if (ctx != NULL) SSL_CTX_free(ctx);
873*0Sstevel@tonic-gate 	if (bio_s_out != NULL)
874*0Sstevel@tonic-gate 		{
875*0Sstevel@tonic-gate 		BIO_free(bio_s_out);
876*0Sstevel@tonic-gate 		bio_s_out=NULL;
877*0Sstevel@tonic-gate 		}
878*0Sstevel@tonic-gate 	apps_shutdown();
879*0Sstevel@tonic-gate 	OPENSSL_EXIT(ret);
880*0Sstevel@tonic-gate 	}
881*0Sstevel@tonic-gate 
882*0Sstevel@tonic-gate static void print_stats(BIO *bio, SSL_CTX *ssl_ctx)
883*0Sstevel@tonic-gate 	{
884*0Sstevel@tonic-gate 	BIO_printf(bio,"%4ld items in the session cache\n",
885*0Sstevel@tonic-gate 		SSL_CTX_sess_number(ssl_ctx));
886*0Sstevel@tonic-gate 	BIO_printf(bio,"%4d client connects (SSL_connect())\n",
887*0Sstevel@tonic-gate 		SSL_CTX_sess_connect(ssl_ctx));
888*0Sstevel@tonic-gate 	BIO_printf(bio,"%4d client renegotiates (SSL_connect())\n",
889*0Sstevel@tonic-gate 		SSL_CTX_sess_connect_renegotiate(ssl_ctx));
890*0Sstevel@tonic-gate 	BIO_printf(bio,"%4d client connects that finished\n",
891*0Sstevel@tonic-gate 		SSL_CTX_sess_connect_good(ssl_ctx));
892*0Sstevel@tonic-gate 	BIO_printf(bio,"%4d server accepts (SSL_accept())\n",
893*0Sstevel@tonic-gate 		SSL_CTX_sess_accept(ssl_ctx));
894*0Sstevel@tonic-gate 	BIO_printf(bio,"%4d server renegotiates (SSL_accept())\n",
895*0Sstevel@tonic-gate 		SSL_CTX_sess_accept_renegotiate(ssl_ctx));
896*0Sstevel@tonic-gate 	BIO_printf(bio,"%4d server accepts that finished\n",
897*0Sstevel@tonic-gate 		SSL_CTX_sess_accept_good(ssl_ctx));
898*0Sstevel@tonic-gate 	BIO_printf(bio,"%4d session cache hits\n",SSL_CTX_sess_hits(ssl_ctx));
899*0Sstevel@tonic-gate 	BIO_printf(bio,"%4d session cache misses\n",SSL_CTX_sess_misses(ssl_ctx));
900*0Sstevel@tonic-gate 	BIO_printf(bio,"%4d session cache timeouts\n",SSL_CTX_sess_timeouts(ssl_ctx));
901*0Sstevel@tonic-gate 	BIO_printf(bio,"%4d callback cache hits\n",SSL_CTX_sess_cb_hits(ssl_ctx));
902*0Sstevel@tonic-gate 	BIO_printf(bio,"%4d cache full overflows (%d allowed)\n",
903*0Sstevel@tonic-gate 		SSL_CTX_sess_cache_full(ssl_ctx),
904*0Sstevel@tonic-gate 		SSL_CTX_sess_get_cache_size(ssl_ctx));
905*0Sstevel@tonic-gate 	}
906*0Sstevel@tonic-gate 
907*0Sstevel@tonic-gate static int sv_body(char *hostname, int s, unsigned char *context)
908*0Sstevel@tonic-gate 	{
909*0Sstevel@tonic-gate 	char *buf=NULL;
910*0Sstevel@tonic-gate 	fd_set readfds;
911*0Sstevel@tonic-gate 	int ret=1,width;
912*0Sstevel@tonic-gate 	int k,i;
913*0Sstevel@tonic-gate 	unsigned long l;
914*0Sstevel@tonic-gate 	SSL *con=NULL;
915*0Sstevel@tonic-gate 	BIO *sbio;
916*0Sstevel@tonic-gate #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
917*0Sstevel@tonic-gate 	struct timeval tv;
918*0Sstevel@tonic-gate #endif
919*0Sstevel@tonic-gate 
920*0Sstevel@tonic-gate 	if ((buf=OPENSSL_malloc(bufsize)) == NULL)
921*0Sstevel@tonic-gate 		{
922*0Sstevel@tonic-gate 		BIO_printf(bio_err,"out of memory\n");
923*0Sstevel@tonic-gate 		goto err;
924*0Sstevel@tonic-gate 		}
925*0Sstevel@tonic-gate #ifdef FIONBIO
926*0Sstevel@tonic-gate 	if (s_nbio)
927*0Sstevel@tonic-gate 		{
928*0Sstevel@tonic-gate 		unsigned long sl=1;
929*0Sstevel@tonic-gate 
930*0Sstevel@tonic-gate 		if (!s_quiet)
931*0Sstevel@tonic-gate 			BIO_printf(bio_err,"turning on non blocking io\n");
932*0Sstevel@tonic-gate 		if (BIO_socket_ioctl(s,FIONBIO,&sl) < 0)
933*0Sstevel@tonic-gate 			ERR_print_errors(bio_err);
934*0Sstevel@tonic-gate 		}
935*0Sstevel@tonic-gate #endif
936*0Sstevel@tonic-gate 
937*0Sstevel@tonic-gate 	if (con == NULL) {
938*0Sstevel@tonic-gate 		con=SSL_new(ctx);
939*0Sstevel@tonic-gate #ifndef OPENSSL_NO_KRB5
940*0Sstevel@tonic-gate 		if ((con->kssl_ctx = kssl_ctx_new()) != NULL)
941*0Sstevel@tonic-gate                         {
942*0Sstevel@tonic-gate                         kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVICE,
943*0Sstevel@tonic-gate 								KRB5SVC);
944*0Sstevel@tonic-gate                         kssl_ctx_setstring(con->kssl_ctx, KSSL_KEYTAB,
945*0Sstevel@tonic-gate 								KRB5KEYTAB);
946*0Sstevel@tonic-gate                         }
947*0Sstevel@tonic-gate #endif	/* OPENSSL_NO_KRB5 */
948*0Sstevel@tonic-gate 		if(context)
949*0Sstevel@tonic-gate 		      SSL_set_session_id_context(con, context,
950*0Sstevel@tonic-gate 						 strlen((char *)context));
951*0Sstevel@tonic-gate 	}
952*0Sstevel@tonic-gate 	SSL_clear(con);
953*0Sstevel@tonic-gate 
954*0Sstevel@tonic-gate 	sbio=BIO_new_socket(s,BIO_NOCLOSE);
955*0Sstevel@tonic-gate 	if (s_nbio_test)
956*0Sstevel@tonic-gate 		{
957*0Sstevel@tonic-gate 		BIO *test;
958*0Sstevel@tonic-gate 
959*0Sstevel@tonic-gate 		test=BIO_new(BIO_f_nbio_test());
960*0Sstevel@tonic-gate 		sbio=BIO_push(test,sbio);
961*0Sstevel@tonic-gate 		}
962*0Sstevel@tonic-gate 	SSL_set_bio(con,sbio,sbio);
963*0Sstevel@tonic-gate 	SSL_set_accept_state(con);
964*0Sstevel@tonic-gate 	/* SSL_set_fd(con,s); */
965*0Sstevel@tonic-gate 
966*0Sstevel@tonic-gate 	if (s_debug)
967*0Sstevel@tonic-gate 		{
968*0Sstevel@tonic-gate 		con->debug=1;
969*0Sstevel@tonic-gate 		BIO_set_callback(SSL_get_rbio(con),bio_dump_cb);
970*0Sstevel@tonic-gate 		BIO_set_callback_arg(SSL_get_rbio(con),bio_s_out);
971*0Sstevel@tonic-gate 		}
972*0Sstevel@tonic-gate 	if (s_msg)
973*0Sstevel@tonic-gate 		{
974*0Sstevel@tonic-gate 		SSL_set_msg_callback(con, msg_cb);
975*0Sstevel@tonic-gate 		SSL_set_msg_callback_arg(con, bio_s_out);
976*0Sstevel@tonic-gate 		}
977*0Sstevel@tonic-gate 
978*0Sstevel@tonic-gate 	width=s+1;
979*0Sstevel@tonic-gate 	for (;;)
980*0Sstevel@tonic-gate 		{
981*0Sstevel@tonic-gate 		int read_from_terminal;
982*0Sstevel@tonic-gate 		int read_from_sslcon;
983*0Sstevel@tonic-gate 
984*0Sstevel@tonic-gate 		read_from_terminal = 0;
985*0Sstevel@tonic-gate 		read_from_sslcon = SSL_pending(con);
986*0Sstevel@tonic-gate 
987*0Sstevel@tonic-gate 		if (!read_from_sslcon)
988*0Sstevel@tonic-gate 			{
989*0Sstevel@tonic-gate 			FD_ZERO(&readfds);
990*0Sstevel@tonic-gate #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
991*0Sstevel@tonic-gate 			FD_SET(fileno(stdin),&readfds);
992*0Sstevel@tonic-gate #endif
993*0Sstevel@tonic-gate 			FD_SET(s,&readfds);
994*0Sstevel@tonic-gate 			/* Note: under VMS with SOCKETSHR the second parameter is
995*0Sstevel@tonic-gate 			 * currently of type (int *) whereas under other systems
996*0Sstevel@tonic-gate 			 * it is (void *) if you don't have a cast it will choke
997*0Sstevel@tonic-gate 			 * the compiler: if you do have a cast then you can either
998*0Sstevel@tonic-gate 			 * go for (int *) or (void *).
999*0Sstevel@tonic-gate 			 */
1000*0Sstevel@tonic-gate #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
1001*0Sstevel@tonic-gate                         /* Under DOS (non-djgpp) and Windows we can't select on stdin: only
1002*0Sstevel@tonic-gate 			 * on sockets. As a workaround we timeout the select every
1003*0Sstevel@tonic-gate 			 * second and check for any keypress. In a proper Windows
1004*0Sstevel@tonic-gate 			 * application we wouldn't do this because it is inefficient.
1005*0Sstevel@tonic-gate 			 */
1006*0Sstevel@tonic-gate 			tv.tv_sec = 1;
1007*0Sstevel@tonic-gate 			tv.tv_usec = 0;
1008*0Sstevel@tonic-gate 			i=select(width,(void *)&readfds,NULL,NULL,&tv);
1009*0Sstevel@tonic-gate 			if((i < 0) || (!i && !_kbhit() ) )continue;
1010*0Sstevel@tonic-gate 			if(_kbhit())
1011*0Sstevel@tonic-gate 				read_from_terminal = 1;
1012*0Sstevel@tonic-gate #else
1013*0Sstevel@tonic-gate 			i=select(width,(void *)&readfds,NULL,NULL,NULL);
1014*0Sstevel@tonic-gate 			if (i <= 0) continue;
1015*0Sstevel@tonic-gate 			if (FD_ISSET(fileno(stdin),&readfds))
1016*0Sstevel@tonic-gate 				read_from_terminal = 1;
1017*0Sstevel@tonic-gate #endif
1018*0Sstevel@tonic-gate 			if (FD_ISSET(s,&readfds))
1019*0Sstevel@tonic-gate 				read_from_sslcon = 1;
1020*0Sstevel@tonic-gate 			}
1021*0Sstevel@tonic-gate 		if (read_from_terminal)
1022*0Sstevel@tonic-gate 			{
1023*0Sstevel@tonic-gate 			if (s_crlf)
1024*0Sstevel@tonic-gate 				{
1025*0Sstevel@tonic-gate 				int j, lf_num;
1026*0Sstevel@tonic-gate 
1027*0Sstevel@tonic-gate 				i=read(fileno(stdin), buf, bufsize/2);
1028*0Sstevel@tonic-gate 				lf_num = 0;
1029*0Sstevel@tonic-gate 				/* both loops are skipped when i <= 0 */
1030*0Sstevel@tonic-gate 				for (j = 0; j < i; j++)
1031*0Sstevel@tonic-gate 					if (buf[j] == '\n')
1032*0Sstevel@tonic-gate 						lf_num++;
1033*0Sstevel@tonic-gate 				for (j = i-1; j >= 0; j--)
1034*0Sstevel@tonic-gate 					{
1035*0Sstevel@tonic-gate 					buf[j+lf_num] = buf[j];
1036*0Sstevel@tonic-gate 					if (buf[j] == '\n')
1037*0Sstevel@tonic-gate 						{
1038*0Sstevel@tonic-gate 						lf_num--;
1039*0Sstevel@tonic-gate 						i++;
1040*0Sstevel@tonic-gate 						buf[j+lf_num] = '\r';
1041*0Sstevel@tonic-gate 						}
1042*0Sstevel@tonic-gate 					}
1043*0Sstevel@tonic-gate 				assert(lf_num == 0);
1044*0Sstevel@tonic-gate 				}
1045*0Sstevel@tonic-gate 			else
1046*0Sstevel@tonic-gate 				i=read(fileno(stdin),buf,bufsize);
1047*0Sstevel@tonic-gate 			if (!s_quiet)
1048*0Sstevel@tonic-gate 				{
1049*0Sstevel@tonic-gate 				if ((i <= 0) || (buf[0] == 'Q'))
1050*0Sstevel@tonic-gate 					{
1051*0Sstevel@tonic-gate 					BIO_printf(bio_s_out,"DONE\n");
1052*0Sstevel@tonic-gate 					SHUTDOWN(s);
1053*0Sstevel@tonic-gate 					close_accept_socket();
1054*0Sstevel@tonic-gate 					ret= -11;
1055*0Sstevel@tonic-gate 					goto err;
1056*0Sstevel@tonic-gate 					}
1057*0Sstevel@tonic-gate 				if ((i <= 0) || (buf[0] == 'q'))
1058*0Sstevel@tonic-gate 					{
1059*0Sstevel@tonic-gate 					BIO_printf(bio_s_out,"DONE\n");
1060*0Sstevel@tonic-gate 					SHUTDOWN(s);
1061*0Sstevel@tonic-gate 	/*				close_accept_socket();
1062*0Sstevel@tonic-gate 					ret= -11;*/
1063*0Sstevel@tonic-gate 					goto err;
1064*0Sstevel@tonic-gate 					}
1065*0Sstevel@tonic-gate 				if ((buf[0] == 'r') &&
1066*0Sstevel@tonic-gate 					((buf[1] == '\n') || (buf[1] == '\r')))
1067*0Sstevel@tonic-gate 					{
1068*0Sstevel@tonic-gate 					SSL_renegotiate(con);
1069*0Sstevel@tonic-gate 					i=SSL_do_handshake(con);
1070*0Sstevel@tonic-gate 					printf("SSL_do_handshake -> %d\n",i);
1071*0Sstevel@tonic-gate 					i=0; /*13; */
1072*0Sstevel@tonic-gate 					continue;
1073*0Sstevel@tonic-gate 					/* strcpy(buf,"server side RE-NEGOTIATE\n"); */
1074*0Sstevel@tonic-gate 					}
1075*0Sstevel@tonic-gate 				if ((buf[0] == 'R') &&
1076*0Sstevel@tonic-gate 					((buf[1] == '\n') || (buf[1] == '\r')))
1077*0Sstevel@tonic-gate 					{
1078*0Sstevel@tonic-gate 					SSL_set_verify(con,
1079*0Sstevel@tonic-gate 						SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,NULL);
1080*0Sstevel@tonic-gate 					SSL_renegotiate(con);
1081*0Sstevel@tonic-gate 					i=SSL_do_handshake(con);
1082*0Sstevel@tonic-gate 					printf("SSL_do_handshake -> %d\n",i);
1083*0Sstevel@tonic-gate 					i=0; /* 13; */
1084*0Sstevel@tonic-gate 					continue;
1085*0Sstevel@tonic-gate 					/* strcpy(buf,"server side RE-NEGOTIATE asking for client cert\n"); */
1086*0Sstevel@tonic-gate 					}
1087*0Sstevel@tonic-gate 				if (buf[0] == 'P')
1088*0Sstevel@tonic-gate 					{
1089*0Sstevel@tonic-gate 					static char *str="Lets print some clear text\n";
1090*0Sstevel@tonic-gate 					BIO_write(SSL_get_wbio(con),str,strlen(str));
1091*0Sstevel@tonic-gate 					}
1092*0Sstevel@tonic-gate 				if (buf[0] == 'S')
1093*0Sstevel@tonic-gate 					{
1094*0Sstevel@tonic-gate 					print_stats(bio_s_out,SSL_get_SSL_CTX(con));
1095*0Sstevel@tonic-gate 					}
1096*0Sstevel@tonic-gate 				}
1097*0Sstevel@tonic-gate #ifdef CHARSET_EBCDIC
1098*0Sstevel@tonic-gate 			ebcdic2ascii(buf,buf,i);
1099*0Sstevel@tonic-gate #endif
1100*0Sstevel@tonic-gate 			l=k=0;
1101*0Sstevel@tonic-gate 			for (;;)
1102*0Sstevel@tonic-gate 				{
1103*0Sstevel@tonic-gate 				/* should do a select for the write */
1104*0Sstevel@tonic-gate #ifdef RENEG
1105*0Sstevel@tonic-gate { static count=0; if (++count == 100) { count=0; SSL_renegotiate(con); } }
1106*0Sstevel@tonic-gate #endif
1107*0Sstevel@tonic-gate 				k=SSL_write(con,&(buf[l]),(unsigned int)i);
1108*0Sstevel@tonic-gate 				switch (SSL_get_error(con,k))
1109*0Sstevel@tonic-gate 					{
1110*0Sstevel@tonic-gate 				case SSL_ERROR_NONE:
1111*0Sstevel@tonic-gate 					break;
1112*0Sstevel@tonic-gate 				case SSL_ERROR_WANT_WRITE:
1113*0Sstevel@tonic-gate 				case SSL_ERROR_WANT_READ:
1114*0Sstevel@tonic-gate 				case SSL_ERROR_WANT_X509_LOOKUP:
1115*0Sstevel@tonic-gate 					BIO_printf(bio_s_out,"Write BLOCK\n");
1116*0Sstevel@tonic-gate 					break;
1117*0Sstevel@tonic-gate 				case SSL_ERROR_SYSCALL:
1118*0Sstevel@tonic-gate 				case SSL_ERROR_SSL:
1119*0Sstevel@tonic-gate 					BIO_printf(bio_s_out,"ERROR\n");
1120*0Sstevel@tonic-gate 					ERR_print_errors(bio_err);
1121*0Sstevel@tonic-gate 					ret=1;
1122*0Sstevel@tonic-gate 					goto err;
1123*0Sstevel@tonic-gate 					/* break; */
1124*0Sstevel@tonic-gate 				case SSL_ERROR_ZERO_RETURN:
1125*0Sstevel@tonic-gate 					BIO_printf(bio_s_out,"DONE\n");
1126*0Sstevel@tonic-gate 					ret=1;
1127*0Sstevel@tonic-gate 					goto err;
1128*0Sstevel@tonic-gate 					}
1129*0Sstevel@tonic-gate 				l+=k;
1130*0Sstevel@tonic-gate 				i-=k;
1131*0Sstevel@tonic-gate 				if (i <= 0) break;
1132*0Sstevel@tonic-gate 				}
1133*0Sstevel@tonic-gate 			}
1134*0Sstevel@tonic-gate 		if (read_from_sslcon)
1135*0Sstevel@tonic-gate 			{
1136*0Sstevel@tonic-gate 			if (!SSL_is_init_finished(con))
1137*0Sstevel@tonic-gate 				{
1138*0Sstevel@tonic-gate 				i=init_ssl_connection(con);
1139*0Sstevel@tonic-gate 
1140*0Sstevel@tonic-gate 				if (i < 0)
1141*0Sstevel@tonic-gate 					{
1142*0Sstevel@tonic-gate 					ret=0;
1143*0Sstevel@tonic-gate 					goto err;
1144*0Sstevel@tonic-gate 					}
1145*0Sstevel@tonic-gate 				else if (i == 0)
1146*0Sstevel@tonic-gate 					{
1147*0Sstevel@tonic-gate 					ret=1;
1148*0Sstevel@tonic-gate 					goto err;
1149*0Sstevel@tonic-gate 					}
1150*0Sstevel@tonic-gate 				}
1151*0Sstevel@tonic-gate 			else
1152*0Sstevel@tonic-gate 				{
1153*0Sstevel@tonic-gate again:
1154*0Sstevel@tonic-gate 				i=SSL_read(con,(char *)buf,bufsize);
1155*0Sstevel@tonic-gate 				switch (SSL_get_error(con,i))
1156*0Sstevel@tonic-gate 					{
1157*0Sstevel@tonic-gate 				case SSL_ERROR_NONE:
1158*0Sstevel@tonic-gate #ifdef CHARSET_EBCDIC
1159*0Sstevel@tonic-gate 					ascii2ebcdic(buf,buf,i);
1160*0Sstevel@tonic-gate #endif
1161*0Sstevel@tonic-gate 					write(fileno(stdout),buf,
1162*0Sstevel@tonic-gate 						(unsigned int)i);
1163*0Sstevel@tonic-gate 					if (SSL_pending(con)) goto again;
1164*0Sstevel@tonic-gate 					break;
1165*0Sstevel@tonic-gate 				case SSL_ERROR_WANT_WRITE:
1166*0Sstevel@tonic-gate 				case SSL_ERROR_WANT_READ:
1167*0Sstevel@tonic-gate 				case SSL_ERROR_WANT_X509_LOOKUP:
1168*0Sstevel@tonic-gate 					BIO_printf(bio_s_out,"Read BLOCK\n");
1169*0Sstevel@tonic-gate 					break;
1170*0Sstevel@tonic-gate 				case SSL_ERROR_SYSCALL:
1171*0Sstevel@tonic-gate 				case SSL_ERROR_SSL:
1172*0Sstevel@tonic-gate 					BIO_printf(bio_s_out,"ERROR\n");
1173*0Sstevel@tonic-gate 					ERR_print_errors(bio_err);
1174*0Sstevel@tonic-gate 					ret=1;
1175*0Sstevel@tonic-gate 					goto err;
1176*0Sstevel@tonic-gate 				case SSL_ERROR_ZERO_RETURN:
1177*0Sstevel@tonic-gate 					BIO_printf(bio_s_out,"DONE\n");
1178*0Sstevel@tonic-gate 					ret=1;
1179*0Sstevel@tonic-gate 					goto err;
1180*0Sstevel@tonic-gate 					}
1181*0Sstevel@tonic-gate 				}
1182*0Sstevel@tonic-gate 			}
1183*0Sstevel@tonic-gate 		}
1184*0Sstevel@tonic-gate err:
1185*0Sstevel@tonic-gate 	BIO_printf(bio_s_out,"shutting down SSL\n");
1186*0Sstevel@tonic-gate #if 1
1187*0Sstevel@tonic-gate 	SSL_set_shutdown(con,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
1188*0Sstevel@tonic-gate #else
1189*0Sstevel@tonic-gate 	SSL_shutdown(con);
1190*0Sstevel@tonic-gate #endif
1191*0Sstevel@tonic-gate 	if (con != NULL) SSL_free(con);
1192*0Sstevel@tonic-gate 	BIO_printf(bio_s_out,"CONNECTION CLOSED\n");
1193*0Sstevel@tonic-gate 	if (buf != NULL)
1194*0Sstevel@tonic-gate 		{
1195*0Sstevel@tonic-gate 		OPENSSL_cleanse(buf,bufsize);
1196*0Sstevel@tonic-gate 		OPENSSL_free(buf);
1197*0Sstevel@tonic-gate 		}
1198*0Sstevel@tonic-gate 	if (ret >= 0)
1199*0Sstevel@tonic-gate 		BIO_printf(bio_s_out,"ACCEPT\n");
1200*0Sstevel@tonic-gate 	return(ret);
1201*0Sstevel@tonic-gate 	}
1202*0Sstevel@tonic-gate 
1203*0Sstevel@tonic-gate static void close_accept_socket(void)
1204*0Sstevel@tonic-gate 	{
1205*0Sstevel@tonic-gate 	BIO_printf(bio_err,"shutdown accept socket\n");
1206*0Sstevel@tonic-gate 	if (accept_socket >= 0)
1207*0Sstevel@tonic-gate 		{
1208*0Sstevel@tonic-gate 		SHUTDOWN2(accept_socket);
1209*0Sstevel@tonic-gate 		}
1210*0Sstevel@tonic-gate 	}
1211*0Sstevel@tonic-gate 
1212*0Sstevel@tonic-gate static int init_ssl_connection(SSL *con)
1213*0Sstevel@tonic-gate 	{
1214*0Sstevel@tonic-gate 	int i;
1215*0Sstevel@tonic-gate 	const char *str;
1216*0Sstevel@tonic-gate 	X509 *peer;
1217*0Sstevel@tonic-gate 	long verify_error;
1218*0Sstevel@tonic-gate 	MS_STATIC char buf[BUFSIZ];
1219*0Sstevel@tonic-gate 
1220*0Sstevel@tonic-gate 	if ((i=SSL_accept(con)) <= 0)
1221*0Sstevel@tonic-gate 		{
1222*0Sstevel@tonic-gate 		if (BIO_sock_should_retry(i))
1223*0Sstevel@tonic-gate 			{
1224*0Sstevel@tonic-gate 			BIO_printf(bio_s_out,"DELAY\n");
1225*0Sstevel@tonic-gate 			return(1);
1226*0Sstevel@tonic-gate 			}
1227*0Sstevel@tonic-gate 
1228*0Sstevel@tonic-gate 		BIO_printf(bio_err,"ERROR\n");
1229*0Sstevel@tonic-gate 		verify_error=SSL_get_verify_result(con);
1230*0Sstevel@tonic-gate 		if (verify_error != X509_V_OK)
1231*0Sstevel@tonic-gate 			{
1232*0Sstevel@tonic-gate 			BIO_printf(bio_err,"verify error:%s\n",
1233*0Sstevel@tonic-gate 				X509_verify_cert_error_string(verify_error));
1234*0Sstevel@tonic-gate 			}
1235*0Sstevel@tonic-gate 		else
1236*0Sstevel@tonic-gate 			ERR_print_errors(bio_err);
1237*0Sstevel@tonic-gate 		return(0);
1238*0Sstevel@tonic-gate 		}
1239*0Sstevel@tonic-gate 
1240*0Sstevel@tonic-gate 	PEM_write_bio_SSL_SESSION(bio_s_out,SSL_get_session(con));
1241*0Sstevel@tonic-gate 
1242*0Sstevel@tonic-gate 	peer=SSL_get_peer_certificate(con);
1243*0Sstevel@tonic-gate 	if (peer != NULL)
1244*0Sstevel@tonic-gate 		{
1245*0Sstevel@tonic-gate 		BIO_printf(bio_s_out,"Client certificate\n");
1246*0Sstevel@tonic-gate 		PEM_write_bio_X509(bio_s_out,peer);
1247*0Sstevel@tonic-gate 		X509_NAME_oneline(X509_get_subject_name(peer),buf,sizeof buf);
1248*0Sstevel@tonic-gate 		BIO_printf(bio_s_out,"subject=%s\n",buf);
1249*0Sstevel@tonic-gate 		X509_NAME_oneline(X509_get_issuer_name(peer),buf,sizeof buf);
1250*0Sstevel@tonic-gate 		BIO_printf(bio_s_out,"issuer=%s\n",buf);
1251*0Sstevel@tonic-gate 		X509_free(peer);
1252*0Sstevel@tonic-gate 		}
1253*0Sstevel@tonic-gate 
1254*0Sstevel@tonic-gate 	if (SSL_get_shared_ciphers(con,buf,sizeof buf) != NULL)
1255*0Sstevel@tonic-gate 		BIO_printf(bio_s_out,"Shared ciphers:%s\n",buf);
1256*0Sstevel@tonic-gate 	str=SSL_CIPHER_get_name(SSL_get_current_cipher(con));
1257*0Sstevel@tonic-gate 	BIO_printf(bio_s_out,"CIPHER is %s\n",(str != NULL)?str:"(NONE)");
1258*0Sstevel@tonic-gate 	if (con->hit) BIO_printf(bio_s_out,"Reused session-id\n");
1259*0Sstevel@tonic-gate 	if (SSL_ctrl(con,SSL_CTRL_GET_FLAGS,0,NULL) &
1260*0Sstevel@tonic-gate 		TLS1_FLAGS_TLS_PADDING_BUG)
1261*0Sstevel@tonic-gate 		BIO_printf(bio_s_out,"Peer has incorrect TLSv1 block padding\n");
1262*0Sstevel@tonic-gate #ifndef OPENSSL_NO_KRB5
1263*0Sstevel@tonic-gate 	if (con->kssl_ctx->client_princ != NULL)
1264*0Sstevel@tonic-gate 		{
1265*0Sstevel@tonic-gate 		BIO_printf(bio_s_out,"Kerberos peer principal is %s\n",
1266*0Sstevel@tonic-gate 			con->kssl_ctx->client_princ);
1267*0Sstevel@tonic-gate 		}
1268*0Sstevel@tonic-gate #endif /* OPENSSL_NO_KRB5 */
1269*0Sstevel@tonic-gate 	return(1);
1270*0Sstevel@tonic-gate 	}
1271*0Sstevel@tonic-gate 
1272*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
1273*0Sstevel@tonic-gate static DH *load_dh_param(char *dhfile)
1274*0Sstevel@tonic-gate 	{
1275*0Sstevel@tonic-gate 	DH *ret=NULL;
1276*0Sstevel@tonic-gate 	BIO *bio;
1277*0Sstevel@tonic-gate 
1278*0Sstevel@tonic-gate 	if ((bio=BIO_new_file(dhfile,"r")) == NULL)
1279*0Sstevel@tonic-gate 		goto err;
1280*0Sstevel@tonic-gate 	ret=PEM_read_bio_DHparams(bio,NULL,NULL,NULL);
1281*0Sstevel@tonic-gate err:
1282*0Sstevel@tonic-gate 	if (bio != NULL) BIO_free(bio);
1283*0Sstevel@tonic-gate 	return(ret);
1284*0Sstevel@tonic-gate 	}
1285*0Sstevel@tonic-gate #endif
1286*0Sstevel@tonic-gate 
1287*0Sstevel@tonic-gate #if 0
1288*0Sstevel@tonic-gate static int load_CA(SSL_CTX *ctx, char *file)
1289*0Sstevel@tonic-gate 	{
1290*0Sstevel@tonic-gate 	FILE *in;
1291*0Sstevel@tonic-gate 	X509 *x=NULL;
1292*0Sstevel@tonic-gate 
1293*0Sstevel@tonic-gate 	if ((in=fopen(file,"r")) == NULL)
1294*0Sstevel@tonic-gate 		return(0);
1295*0Sstevel@tonic-gate 
1296*0Sstevel@tonic-gate 	for (;;)
1297*0Sstevel@tonic-gate 		{
1298*0Sstevel@tonic-gate 		if (PEM_read_X509(in,&x,NULL) == NULL)
1299*0Sstevel@tonic-gate 			break;
1300*0Sstevel@tonic-gate 		SSL_CTX_add_client_CA(ctx,x);
1301*0Sstevel@tonic-gate 		}
1302*0Sstevel@tonic-gate 	if (x != NULL) X509_free(x);
1303*0Sstevel@tonic-gate 	fclose(in);
1304*0Sstevel@tonic-gate 	return(1);
1305*0Sstevel@tonic-gate 	}
1306*0Sstevel@tonic-gate #endif
1307*0Sstevel@tonic-gate 
1308*0Sstevel@tonic-gate static int www_body(char *hostname, int s, unsigned char *context)
1309*0Sstevel@tonic-gate 	{
1310*0Sstevel@tonic-gate 	char *buf=NULL;
1311*0Sstevel@tonic-gate 	int ret=1;
1312*0Sstevel@tonic-gate 	int i,j,k,blank,dot;
1313*0Sstevel@tonic-gate 	struct stat st_buf;
1314*0Sstevel@tonic-gate 	SSL *con;
1315*0Sstevel@tonic-gate 	SSL_CIPHER *c;
1316*0Sstevel@tonic-gate 	BIO *io,*ssl_bio,*sbio;
1317*0Sstevel@tonic-gate 	long total_bytes;
1318*0Sstevel@tonic-gate 
1319*0Sstevel@tonic-gate 	buf=OPENSSL_malloc(bufsize);
1320*0Sstevel@tonic-gate 	if (buf == NULL) return(0);
1321*0Sstevel@tonic-gate 	io=BIO_new(BIO_f_buffer());
1322*0Sstevel@tonic-gate 	ssl_bio=BIO_new(BIO_f_ssl());
1323*0Sstevel@tonic-gate 	if ((io == NULL) || (ssl_bio == NULL)) goto err;
1324*0Sstevel@tonic-gate 
1325*0Sstevel@tonic-gate #ifdef FIONBIO
1326*0Sstevel@tonic-gate 	if (s_nbio)
1327*0Sstevel@tonic-gate 		{
1328*0Sstevel@tonic-gate 		unsigned long sl=1;
1329*0Sstevel@tonic-gate 
1330*0Sstevel@tonic-gate 		if (!s_quiet)
1331*0Sstevel@tonic-gate 			BIO_printf(bio_err,"turning on non blocking io\n");
1332*0Sstevel@tonic-gate 		if (BIO_socket_ioctl(s,FIONBIO,&sl) < 0)
1333*0Sstevel@tonic-gate 			ERR_print_errors(bio_err);
1334*0Sstevel@tonic-gate 		}
1335*0Sstevel@tonic-gate #endif
1336*0Sstevel@tonic-gate 
1337*0Sstevel@tonic-gate 	/* lets make the output buffer a reasonable size */
1338*0Sstevel@tonic-gate 	if (!BIO_set_write_buffer_size(io,bufsize)) goto err;
1339*0Sstevel@tonic-gate 
1340*0Sstevel@tonic-gate 	if ((con=SSL_new(ctx)) == NULL) goto err;
1341*0Sstevel@tonic-gate #ifndef OPENSSL_NO_KRB5
1342*0Sstevel@tonic-gate 	if ((con->kssl_ctx = kssl_ctx_new()) != NULL)
1343*0Sstevel@tonic-gate 		{
1344*0Sstevel@tonic-gate 		kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVICE, KRB5SVC);
1345*0Sstevel@tonic-gate 		kssl_ctx_setstring(con->kssl_ctx, KSSL_KEYTAB, KRB5KEYTAB);
1346*0Sstevel@tonic-gate 		}
1347*0Sstevel@tonic-gate #endif	/* OPENSSL_NO_KRB5 */
1348*0Sstevel@tonic-gate 	if(context) SSL_set_session_id_context(con, context,
1349*0Sstevel@tonic-gate 					       strlen((char *)context));
1350*0Sstevel@tonic-gate 
1351*0Sstevel@tonic-gate 	sbio=BIO_new_socket(s,BIO_NOCLOSE);
1352*0Sstevel@tonic-gate 	if (s_nbio_test)
1353*0Sstevel@tonic-gate 		{
1354*0Sstevel@tonic-gate 		BIO *test;
1355*0Sstevel@tonic-gate 
1356*0Sstevel@tonic-gate 		test=BIO_new(BIO_f_nbio_test());
1357*0Sstevel@tonic-gate 		sbio=BIO_push(test,sbio);
1358*0Sstevel@tonic-gate 		}
1359*0Sstevel@tonic-gate 	SSL_set_bio(con,sbio,sbio);
1360*0Sstevel@tonic-gate 	SSL_set_accept_state(con);
1361*0Sstevel@tonic-gate 
1362*0Sstevel@tonic-gate 	/* SSL_set_fd(con,s); */
1363*0Sstevel@tonic-gate 	BIO_set_ssl(ssl_bio,con,BIO_CLOSE);
1364*0Sstevel@tonic-gate 	BIO_push(io,ssl_bio);
1365*0Sstevel@tonic-gate #ifdef CHARSET_EBCDIC
1366*0Sstevel@tonic-gate 	io = BIO_push(BIO_new(BIO_f_ebcdic_filter()),io);
1367*0Sstevel@tonic-gate #endif
1368*0Sstevel@tonic-gate 
1369*0Sstevel@tonic-gate 	if (s_debug)
1370*0Sstevel@tonic-gate 		{
1371*0Sstevel@tonic-gate 		con->debug=1;
1372*0Sstevel@tonic-gate 		BIO_set_callback(SSL_get_rbio(con),bio_dump_cb);
1373*0Sstevel@tonic-gate 		BIO_set_callback_arg(SSL_get_rbio(con),bio_s_out);
1374*0Sstevel@tonic-gate 		}
1375*0Sstevel@tonic-gate 	if (s_msg)
1376*0Sstevel@tonic-gate 		{
1377*0Sstevel@tonic-gate 		SSL_set_msg_callback(con, msg_cb);
1378*0Sstevel@tonic-gate 		SSL_set_msg_callback_arg(con, bio_s_out);
1379*0Sstevel@tonic-gate 		}
1380*0Sstevel@tonic-gate 
1381*0Sstevel@tonic-gate 	blank=0;
1382*0Sstevel@tonic-gate 	for (;;)
1383*0Sstevel@tonic-gate 		{
1384*0Sstevel@tonic-gate 		if (hack)
1385*0Sstevel@tonic-gate 			{
1386*0Sstevel@tonic-gate 			i=SSL_accept(con);
1387*0Sstevel@tonic-gate 
1388*0Sstevel@tonic-gate 			switch (SSL_get_error(con,i))
1389*0Sstevel@tonic-gate 				{
1390*0Sstevel@tonic-gate 			case SSL_ERROR_NONE:
1391*0Sstevel@tonic-gate 				break;
1392*0Sstevel@tonic-gate 			case SSL_ERROR_WANT_WRITE:
1393*0Sstevel@tonic-gate 			case SSL_ERROR_WANT_READ:
1394*0Sstevel@tonic-gate 			case SSL_ERROR_WANT_X509_LOOKUP:
1395*0Sstevel@tonic-gate 				continue;
1396*0Sstevel@tonic-gate 			case SSL_ERROR_SYSCALL:
1397*0Sstevel@tonic-gate 			case SSL_ERROR_SSL:
1398*0Sstevel@tonic-gate 			case SSL_ERROR_ZERO_RETURN:
1399*0Sstevel@tonic-gate 				ret=1;
1400*0Sstevel@tonic-gate 				goto err;
1401*0Sstevel@tonic-gate 				/* break; */
1402*0Sstevel@tonic-gate 				}
1403*0Sstevel@tonic-gate 
1404*0Sstevel@tonic-gate 			SSL_renegotiate(con);
1405*0Sstevel@tonic-gate 			SSL_write(con,NULL,0);
1406*0Sstevel@tonic-gate 			}
1407*0Sstevel@tonic-gate 
1408*0Sstevel@tonic-gate 		i=BIO_gets(io,buf,bufsize-1);
1409*0Sstevel@tonic-gate 		if (i < 0) /* error */
1410*0Sstevel@tonic-gate 			{
1411*0Sstevel@tonic-gate 			if (!BIO_should_retry(io))
1412*0Sstevel@tonic-gate 				{
1413*0Sstevel@tonic-gate 				if (!s_quiet)
1414*0Sstevel@tonic-gate 					ERR_print_errors(bio_err);
1415*0Sstevel@tonic-gate 				goto err;
1416*0Sstevel@tonic-gate 				}
1417*0Sstevel@tonic-gate 			else
1418*0Sstevel@tonic-gate 				{
1419*0Sstevel@tonic-gate 				BIO_printf(bio_s_out,"read R BLOCK\n");
1420*0Sstevel@tonic-gate #if !defined(OPENSSL_SYS_MSDOS) && !defined(__DJGPP__)
1421*0Sstevel@tonic-gate 				sleep(1);
1422*0Sstevel@tonic-gate #endif
1423*0Sstevel@tonic-gate 				continue;
1424*0Sstevel@tonic-gate 				}
1425*0Sstevel@tonic-gate 			}
1426*0Sstevel@tonic-gate 		else if (i == 0) /* end of input */
1427*0Sstevel@tonic-gate 			{
1428*0Sstevel@tonic-gate 			ret=1;
1429*0Sstevel@tonic-gate 			goto end;
1430*0Sstevel@tonic-gate 			}
1431*0Sstevel@tonic-gate 
1432*0Sstevel@tonic-gate 		/* else we have data */
1433*0Sstevel@tonic-gate 		if (	((www == 1) && (strncmp("GET ",buf,4) == 0)) ||
1434*0Sstevel@tonic-gate 			((www == 2) && (strncmp("GET /stats ",buf,10) == 0)))
1435*0Sstevel@tonic-gate 			{
1436*0Sstevel@tonic-gate 			char *p;
1437*0Sstevel@tonic-gate 			X509 *peer;
1438*0Sstevel@tonic-gate 			STACK_OF(SSL_CIPHER) *sk;
1439*0Sstevel@tonic-gate 			static char *space="                          ";
1440*0Sstevel@tonic-gate 
1441*0Sstevel@tonic-gate 			BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
1442*0Sstevel@tonic-gate 			BIO_puts(io,"<HTML><BODY BGCOLOR=\"#ffffff\">\n");
1443*0Sstevel@tonic-gate 			BIO_puts(io,"<pre>\n");
1444*0Sstevel@tonic-gate /*			BIO_puts(io,SSLeay_version(SSLEAY_VERSION));*/
1445*0Sstevel@tonic-gate 			BIO_puts(io,"\n");
1446*0Sstevel@tonic-gate 			for (i=0; i<local_argc; i++)
1447*0Sstevel@tonic-gate 				{
1448*0Sstevel@tonic-gate 				BIO_puts(io,local_argv[i]);
1449*0Sstevel@tonic-gate 				BIO_write(io," ",1);
1450*0Sstevel@tonic-gate 				}
1451*0Sstevel@tonic-gate 			BIO_puts(io,"\n");
1452*0Sstevel@tonic-gate 
1453*0Sstevel@tonic-gate 			/* The following is evil and should not really
1454*0Sstevel@tonic-gate 			 * be done */
1455*0Sstevel@tonic-gate 			BIO_printf(io,"Ciphers supported in s_server binary\n");
1456*0Sstevel@tonic-gate 			sk=SSL_get_ciphers(con);
1457*0Sstevel@tonic-gate 			j=sk_SSL_CIPHER_num(sk);
1458*0Sstevel@tonic-gate 			for (i=0; i<j; i++)
1459*0Sstevel@tonic-gate 				{
1460*0Sstevel@tonic-gate 				c=sk_SSL_CIPHER_value(sk,i);
1461*0Sstevel@tonic-gate 				BIO_printf(io,"%-11s:%-25s",
1462*0Sstevel@tonic-gate 					SSL_CIPHER_get_version(c),
1463*0Sstevel@tonic-gate 					SSL_CIPHER_get_name(c));
1464*0Sstevel@tonic-gate 				if ((((i+1)%2) == 0) && (i+1 != j))
1465*0Sstevel@tonic-gate 					BIO_puts(io,"\n");
1466*0Sstevel@tonic-gate 				}
1467*0Sstevel@tonic-gate 			BIO_puts(io,"\n");
1468*0Sstevel@tonic-gate 			p=SSL_get_shared_ciphers(con,buf,bufsize);
1469*0Sstevel@tonic-gate 			if (p != NULL)
1470*0Sstevel@tonic-gate 				{
1471*0Sstevel@tonic-gate 				BIO_printf(io,"---\nCiphers common between both SSL end points:\n");
1472*0Sstevel@tonic-gate 				j=i=0;
1473*0Sstevel@tonic-gate 				while (*p)
1474*0Sstevel@tonic-gate 					{
1475*0Sstevel@tonic-gate 					if (*p == ':')
1476*0Sstevel@tonic-gate 						{
1477*0Sstevel@tonic-gate 						BIO_write(io,space,26-j);
1478*0Sstevel@tonic-gate 						i++;
1479*0Sstevel@tonic-gate 						j=0;
1480*0Sstevel@tonic-gate 						BIO_write(io,((i%3)?" ":"\n"),1);
1481*0Sstevel@tonic-gate 						}
1482*0Sstevel@tonic-gate 					else
1483*0Sstevel@tonic-gate 						{
1484*0Sstevel@tonic-gate 						BIO_write(io,p,1);
1485*0Sstevel@tonic-gate 						j++;
1486*0Sstevel@tonic-gate 						}
1487*0Sstevel@tonic-gate 					p++;
1488*0Sstevel@tonic-gate 					}
1489*0Sstevel@tonic-gate 				BIO_puts(io,"\n");
1490*0Sstevel@tonic-gate 				}
1491*0Sstevel@tonic-gate 			BIO_printf(io,((con->hit)
1492*0Sstevel@tonic-gate 				?"---\nReused, "
1493*0Sstevel@tonic-gate 				:"---\nNew, "));
1494*0Sstevel@tonic-gate 			c=SSL_get_current_cipher(con);
1495*0Sstevel@tonic-gate 			BIO_printf(io,"%s, Cipher is %s\n",
1496*0Sstevel@tonic-gate 				SSL_CIPHER_get_version(c),
1497*0Sstevel@tonic-gate 				SSL_CIPHER_get_name(c));
1498*0Sstevel@tonic-gate 			SSL_SESSION_print(io,SSL_get_session(con));
1499*0Sstevel@tonic-gate 			BIO_printf(io,"---\n");
1500*0Sstevel@tonic-gate 			print_stats(io,SSL_get_SSL_CTX(con));
1501*0Sstevel@tonic-gate 			BIO_printf(io,"---\n");
1502*0Sstevel@tonic-gate 			peer=SSL_get_peer_certificate(con);
1503*0Sstevel@tonic-gate 			if (peer != NULL)
1504*0Sstevel@tonic-gate 				{
1505*0Sstevel@tonic-gate 				BIO_printf(io,"Client certificate\n");
1506*0Sstevel@tonic-gate 				X509_print(io,peer);
1507*0Sstevel@tonic-gate 				PEM_write_bio_X509(io,peer);
1508*0Sstevel@tonic-gate 				}
1509*0Sstevel@tonic-gate 			else
1510*0Sstevel@tonic-gate 				BIO_puts(io,"no client certificate available\n");
1511*0Sstevel@tonic-gate 			BIO_puts(io,"</BODY></HTML>\r\n\r\n");
1512*0Sstevel@tonic-gate 			break;
1513*0Sstevel@tonic-gate 			}
1514*0Sstevel@tonic-gate 		else if ((www == 2 || www == 3)
1515*0Sstevel@tonic-gate                          && (strncmp("GET /",buf,5) == 0))
1516*0Sstevel@tonic-gate 			{
1517*0Sstevel@tonic-gate 			BIO *file;
1518*0Sstevel@tonic-gate 			char *p,*e;
1519*0Sstevel@tonic-gate 			static char *text="HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
1520*0Sstevel@tonic-gate 
1521*0Sstevel@tonic-gate 			/* skip the '/' */
1522*0Sstevel@tonic-gate 			p= &(buf[5]);
1523*0Sstevel@tonic-gate 
1524*0Sstevel@tonic-gate 			dot = 1;
1525*0Sstevel@tonic-gate 			for (e=p; *e != '\0'; e++)
1526*0Sstevel@tonic-gate 				{
1527*0Sstevel@tonic-gate 				if (e[0] == ' ')
1528*0Sstevel@tonic-gate 					break;
1529*0Sstevel@tonic-gate 
1530*0Sstevel@tonic-gate 				switch (dot)
1531*0Sstevel@tonic-gate 					{
1532*0Sstevel@tonic-gate 				case 1:
1533*0Sstevel@tonic-gate 					dot = (e[0] == '.') ? 2 : 0;
1534*0Sstevel@tonic-gate 					break;
1535*0Sstevel@tonic-gate 				case 2:
1536*0Sstevel@tonic-gate 					dot = (e[0] == '.') ? 3 : 0;
1537*0Sstevel@tonic-gate 					break;
1538*0Sstevel@tonic-gate 				case 3:
1539*0Sstevel@tonic-gate 					dot = (e[0] == '/') ? -1 : 0;
1540*0Sstevel@tonic-gate 					break;
1541*0Sstevel@tonic-gate 					}
1542*0Sstevel@tonic-gate 				if (dot == 0)
1543*0Sstevel@tonic-gate 					dot = (e[0] == '/') ? 1 : 0;
1544*0Sstevel@tonic-gate 				}
1545*0Sstevel@tonic-gate 			dot = (dot == 3) || (dot == -1); /* filename contains ".." component */
1546*0Sstevel@tonic-gate 
1547*0Sstevel@tonic-gate 			if (*e == '\0')
1548*0Sstevel@tonic-gate 				{
1549*0Sstevel@tonic-gate 				BIO_puts(io,text);
1550*0Sstevel@tonic-gate 				BIO_printf(io,"'%s' is an invalid file name\r\n",p);
1551*0Sstevel@tonic-gate 				break;
1552*0Sstevel@tonic-gate 				}
1553*0Sstevel@tonic-gate 			*e='\0';
1554*0Sstevel@tonic-gate 
1555*0Sstevel@tonic-gate 			if (dot)
1556*0Sstevel@tonic-gate 				{
1557*0Sstevel@tonic-gate 				BIO_puts(io,text);
1558*0Sstevel@tonic-gate 				BIO_printf(io,"'%s' contains '..' reference\r\n",p);
1559*0Sstevel@tonic-gate 				break;
1560*0Sstevel@tonic-gate 				}
1561*0Sstevel@tonic-gate 
1562*0Sstevel@tonic-gate 			if (*p == '/')
1563*0Sstevel@tonic-gate 				{
1564*0Sstevel@tonic-gate 				BIO_puts(io,text);
1565*0Sstevel@tonic-gate 				BIO_printf(io,"'%s' is an invalid path\r\n",p);
1566*0Sstevel@tonic-gate 				break;
1567*0Sstevel@tonic-gate 				}
1568*0Sstevel@tonic-gate 
1569*0Sstevel@tonic-gate #if 0
1570*0Sstevel@tonic-gate 			/* append if a directory lookup */
1571*0Sstevel@tonic-gate 			if (e[-1] == '/')
1572*0Sstevel@tonic-gate 				strcat(p,"index.html");
1573*0Sstevel@tonic-gate #endif
1574*0Sstevel@tonic-gate 
1575*0Sstevel@tonic-gate 			/* if a directory, do the index thang */
1576*0Sstevel@tonic-gate 			if (stat(p,&st_buf) < 0)
1577*0Sstevel@tonic-gate 				{
1578*0Sstevel@tonic-gate 				BIO_puts(io,text);
1579*0Sstevel@tonic-gate 				BIO_printf(io,"Error accessing '%s'\r\n",p);
1580*0Sstevel@tonic-gate 				ERR_print_errors(io);
1581*0Sstevel@tonic-gate 				break;
1582*0Sstevel@tonic-gate 				}
1583*0Sstevel@tonic-gate 			if (S_ISDIR(st_buf.st_mode))
1584*0Sstevel@tonic-gate 				{
1585*0Sstevel@tonic-gate #if 0 /* must check buffer size */
1586*0Sstevel@tonic-gate 				strcat(p,"/index.html");
1587*0Sstevel@tonic-gate #else
1588*0Sstevel@tonic-gate 				BIO_puts(io,text);
1589*0Sstevel@tonic-gate 				BIO_printf(io,"'%s' is a directory\r\n",p);
1590*0Sstevel@tonic-gate 				break;
1591*0Sstevel@tonic-gate #endif
1592*0Sstevel@tonic-gate 				}
1593*0Sstevel@tonic-gate 
1594*0Sstevel@tonic-gate 			if ((file=BIO_new_file(p,"r")) == NULL)
1595*0Sstevel@tonic-gate 				{
1596*0Sstevel@tonic-gate 				BIO_puts(io,text);
1597*0Sstevel@tonic-gate 				BIO_printf(io,"Error opening '%s'\r\n",p);
1598*0Sstevel@tonic-gate 				ERR_print_errors(io);
1599*0Sstevel@tonic-gate 				break;
1600*0Sstevel@tonic-gate 				}
1601*0Sstevel@tonic-gate 
1602*0Sstevel@tonic-gate 			if (!s_quiet)
1603*0Sstevel@tonic-gate 				BIO_printf(bio_err,"FILE:%s\n",p);
1604*0Sstevel@tonic-gate 
1605*0Sstevel@tonic-gate                         if (www == 2)
1606*0Sstevel@tonic-gate                                 {
1607*0Sstevel@tonic-gate                                 i=strlen(p);
1608*0Sstevel@tonic-gate                                 if (	((i > 5) && (strcmp(&(p[i-5]),".html") == 0)) ||
1609*0Sstevel@tonic-gate                                         ((i > 4) && (strcmp(&(p[i-4]),".php") == 0)) ||
1610*0Sstevel@tonic-gate                                         ((i > 4) && (strcmp(&(p[i-4]),".htm") == 0)))
1611*0Sstevel@tonic-gate                                         BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
1612*0Sstevel@tonic-gate                                 else
1613*0Sstevel@tonic-gate                                         BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
1614*0Sstevel@tonic-gate                                 }
1615*0Sstevel@tonic-gate 			/* send the file */
1616*0Sstevel@tonic-gate 			total_bytes=0;
1617*0Sstevel@tonic-gate 			for (;;)
1618*0Sstevel@tonic-gate 				{
1619*0Sstevel@tonic-gate 				i=BIO_read(file,buf,bufsize);
1620*0Sstevel@tonic-gate 				if (i <= 0) break;
1621*0Sstevel@tonic-gate 
1622*0Sstevel@tonic-gate #ifdef RENEG
1623*0Sstevel@tonic-gate 				total_bytes+=i;
1624*0Sstevel@tonic-gate 				fprintf(stderr,"%d\n",i);
1625*0Sstevel@tonic-gate 				if (total_bytes > 3*1024)
1626*0Sstevel@tonic-gate 					{
1627*0Sstevel@tonic-gate 					total_bytes=0;
1628*0Sstevel@tonic-gate 					fprintf(stderr,"RENEGOTIATE\n");
1629*0Sstevel@tonic-gate 					SSL_renegotiate(con);
1630*0Sstevel@tonic-gate 					}
1631*0Sstevel@tonic-gate #endif
1632*0Sstevel@tonic-gate 
1633*0Sstevel@tonic-gate 				for (j=0; j<i; )
1634*0Sstevel@tonic-gate 					{
1635*0Sstevel@tonic-gate #ifdef RENEG
1636*0Sstevel@tonic-gate { static count=0; if (++count == 13) { SSL_renegotiate(con); } }
1637*0Sstevel@tonic-gate #endif
1638*0Sstevel@tonic-gate 					k=BIO_write(io,&(buf[j]),i-j);
1639*0Sstevel@tonic-gate 					if (k <= 0)
1640*0Sstevel@tonic-gate 						{
1641*0Sstevel@tonic-gate 						if (!BIO_should_retry(io))
1642*0Sstevel@tonic-gate 							goto write_error;
1643*0Sstevel@tonic-gate 						else
1644*0Sstevel@tonic-gate 							{
1645*0Sstevel@tonic-gate 							BIO_printf(bio_s_out,"rwrite W BLOCK\n");
1646*0Sstevel@tonic-gate 							}
1647*0Sstevel@tonic-gate 						}
1648*0Sstevel@tonic-gate 					else
1649*0Sstevel@tonic-gate 						{
1650*0Sstevel@tonic-gate 						j+=k;
1651*0Sstevel@tonic-gate 						}
1652*0Sstevel@tonic-gate 					}
1653*0Sstevel@tonic-gate 				}
1654*0Sstevel@tonic-gate write_error:
1655*0Sstevel@tonic-gate 			BIO_free(file);
1656*0Sstevel@tonic-gate 			break;
1657*0Sstevel@tonic-gate 			}
1658*0Sstevel@tonic-gate 		}
1659*0Sstevel@tonic-gate 
1660*0Sstevel@tonic-gate 	for (;;)
1661*0Sstevel@tonic-gate 		{
1662*0Sstevel@tonic-gate 		i=(int)BIO_flush(io);
1663*0Sstevel@tonic-gate 		if (i <= 0)
1664*0Sstevel@tonic-gate 			{
1665*0Sstevel@tonic-gate 			if (!BIO_should_retry(io))
1666*0Sstevel@tonic-gate 				break;
1667*0Sstevel@tonic-gate 			}
1668*0Sstevel@tonic-gate 		else
1669*0Sstevel@tonic-gate 			break;
1670*0Sstevel@tonic-gate 		}
1671*0Sstevel@tonic-gate end:
1672*0Sstevel@tonic-gate #if 1
1673*0Sstevel@tonic-gate 	/* make sure we re-use sessions */
1674*0Sstevel@tonic-gate 	SSL_set_shutdown(con,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
1675*0Sstevel@tonic-gate #else
1676*0Sstevel@tonic-gate 	/* This kills performance */
1677*0Sstevel@tonic-gate /*	SSL_shutdown(con); A shutdown gets sent in the
1678*0Sstevel@tonic-gate  *	BIO_free_all(io) procession */
1679*0Sstevel@tonic-gate #endif
1680*0Sstevel@tonic-gate 
1681*0Sstevel@tonic-gate err:
1682*0Sstevel@tonic-gate 
1683*0Sstevel@tonic-gate 	if (ret >= 0)
1684*0Sstevel@tonic-gate 		BIO_printf(bio_s_out,"ACCEPT\n");
1685*0Sstevel@tonic-gate 
1686*0Sstevel@tonic-gate 	if (buf != NULL) OPENSSL_free(buf);
1687*0Sstevel@tonic-gate 	if (io != NULL) BIO_free_all(io);
1688*0Sstevel@tonic-gate /*	if (ssl_bio != NULL) BIO_free(ssl_bio);*/
1689*0Sstevel@tonic-gate 	return(ret);
1690*0Sstevel@tonic-gate 	}
1691*0Sstevel@tonic-gate 
1692*0Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
1693*0Sstevel@tonic-gate static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength)
1694*0Sstevel@tonic-gate 	{
1695*0Sstevel@tonic-gate 	static RSA *rsa_tmp=NULL;
1696*0Sstevel@tonic-gate 
1697*0Sstevel@tonic-gate 	if (rsa_tmp == NULL)
1698*0Sstevel@tonic-gate 		{
1699*0Sstevel@tonic-gate 		if (!s_quiet)
1700*0Sstevel@tonic-gate 			{
1701*0Sstevel@tonic-gate 			BIO_printf(bio_err,"Generating temp (%d bit) RSA key...",keylength);
1702*0Sstevel@tonic-gate 			(void)BIO_flush(bio_err);
1703*0Sstevel@tonic-gate 			}
1704*0Sstevel@tonic-gate 		rsa_tmp=RSA_generate_key(keylength,RSA_F4,NULL,NULL);
1705*0Sstevel@tonic-gate 		if (!s_quiet)
1706*0Sstevel@tonic-gate 			{
1707*0Sstevel@tonic-gate 			BIO_printf(bio_err,"\n");
1708*0Sstevel@tonic-gate 			(void)BIO_flush(bio_err);
1709*0Sstevel@tonic-gate 			}
1710*0Sstevel@tonic-gate 		}
1711*0Sstevel@tonic-gate 	return(rsa_tmp);
1712*0Sstevel@tonic-gate 	}
1713*0Sstevel@tonic-gate #endif
1714*0Sstevel@tonic-gate 
1715*0Sstevel@tonic-gate #define MAX_SESSION_ID_ATTEMPTS 10
1716*0Sstevel@tonic-gate static int generate_session_id(const SSL *ssl, unsigned char *id,
1717*0Sstevel@tonic-gate 				unsigned int *id_len)
1718*0Sstevel@tonic-gate 	{
1719*0Sstevel@tonic-gate 	unsigned int count = 0;
1720*0Sstevel@tonic-gate 	do	{
1721*0Sstevel@tonic-gate 		RAND_pseudo_bytes(id, *id_len);
1722*0Sstevel@tonic-gate 		/* Prefix the session_id with the required prefix. NB: If our
1723*0Sstevel@tonic-gate 		 * prefix is too long, clip it - but there will be worse effects
1724*0Sstevel@tonic-gate 		 * anyway, eg. the server could only possibly create 1 session
1725*0Sstevel@tonic-gate 		 * ID (ie. the prefix!) so all future session negotiations will
1726*0Sstevel@tonic-gate 		 * fail due to conflicts. */
1727*0Sstevel@tonic-gate 		memcpy(id, session_id_prefix,
1728*0Sstevel@tonic-gate 			(strlen(session_id_prefix) < *id_len) ?
1729*0Sstevel@tonic-gate 			strlen(session_id_prefix) : *id_len);
1730*0Sstevel@tonic-gate 		}
1731*0Sstevel@tonic-gate 	while(SSL_has_matching_session_id(ssl, id, *id_len) &&
1732*0Sstevel@tonic-gate 		(++count < MAX_SESSION_ID_ATTEMPTS));
1733*0Sstevel@tonic-gate 	if(count >= MAX_SESSION_ID_ATTEMPTS)
1734*0Sstevel@tonic-gate 		return 0;
1735*0Sstevel@tonic-gate 	return 1;
1736*0Sstevel@tonic-gate 	}
1737