xref: /onnv-gate/usr/src/common/openssl/ssl/ssltest.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* ssl/ssltest.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-2000 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 #define _BSD_SOURCE 1		/* Or gethostname won't be declared properly
113*0Sstevel@tonic-gate 				   on Linux and GNU platforms. */
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate #include <assert.h>
116*0Sstevel@tonic-gate #include <errno.h>
117*0Sstevel@tonic-gate #include <limits.h>
118*0Sstevel@tonic-gate #include <stdio.h>
119*0Sstevel@tonic-gate #include <stdlib.h>
120*0Sstevel@tonic-gate #include <string.h>
121*0Sstevel@tonic-gate #include <time.h>
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate #define USE_SOCKETS
124*0Sstevel@tonic-gate #include "e_os.h"
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate #include <openssl/bio.h>
127*0Sstevel@tonic-gate #include <openssl/crypto.h>
128*0Sstevel@tonic-gate #include <openssl/evp.h>
129*0Sstevel@tonic-gate #include <openssl/x509.h>
130*0Sstevel@tonic-gate #include <openssl/ssl.h>
131*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
132*0Sstevel@tonic-gate #include <openssl/engine.h>
133*0Sstevel@tonic-gate #endif
134*0Sstevel@tonic-gate #include <openssl/err.h>
135*0Sstevel@tonic-gate #include <openssl/rand.h>
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate #define _XOPEN_SOURCE_EXTENDED	1 /* Or gethostname won't be declared properly
138*0Sstevel@tonic-gate 				     on Compaq platforms (at least with DEC C).
139*0Sstevel@tonic-gate 				     Do not try to put it earlier, or IPv6 includes
140*0Sstevel@tonic-gate 				     get screwed...
141*0Sstevel@tonic-gate 				  */
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_WINDOWS
144*0Sstevel@tonic-gate #include <winsock.h>
145*0Sstevel@tonic-gate #else
146*0Sstevel@tonic-gate #include OPENSSL_UNISTD
147*0Sstevel@tonic-gate #endif
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
150*0Sstevel@tonic-gate #  define TEST_SERVER_CERT "SYS$DISK:[-.APPS]SERVER.PEM"
151*0Sstevel@tonic-gate #  define TEST_CLIENT_CERT "SYS$DISK:[-.APPS]CLIENT.PEM"
152*0Sstevel@tonic-gate #elif defined(OPENSSL_SYS_WINCE)
153*0Sstevel@tonic-gate #  define TEST_SERVER_CERT "\\OpenSSL\\server.pem"
154*0Sstevel@tonic-gate #  define TEST_CLIENT_CERT "\\OpenSSL\\client.pem"
155*0Sstevel@tonic-gate #else
156*0Sstevel@tonic-gate #  define TEST_SERVER_CERT "../apps/server.pem"
157*0Sstevel@tonic-gate #  define TEST_CLIENT_CERT "../apps/client.pem"
158*0Sstevel@tonic-gate #endif
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate /* There is really no standard for this, so let's assign some tentative
161*0Sstevel@tonic-gate    numbers.  In any case, these numbers are only for this test */
162*0Sstevel@tonic-gate #define COMP_RLE	1
163*0Sstevel@tonic-gate #define COMP_ZLIB	2
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate static int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx);
166*0Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
167*0Sstevel@tonic-gate static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export,int keylength);
168*0Sstevel@tonic-gate static void free_tmp_rsa(void);
169*0Sstevel@tonic-gate #endif
170*0Sstevel@tonic-gate static int MS_CALLBACK app_verify_callback(X509_STORE_CTX *ctx, void *arg);
171*0Sstevel@tonic-gate #define APP_CALLBACK "Test Callback Argument"
172*0Sstevel@tonic-gate static char *app_verify_arg = APP_CALLBACK;
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
175*0Sstevel@tonic-gate static DH *get_dh512(void);
176*0Sstevel@tonic-gate static DH *get_dh1024(void);
177*0Sstevel@tonic-gate static DH *get_dh1024dsa(void);
178*0Sstevel@tonic-gate #endif
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate static BIO *bio_err=NULL;
181*0Sstevel@tonic-gate static BIO *bio_stdout=NULL;
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate static char *cipher=NULL;
184*0Sstevel@tonic-gate static int verbose=0;
185*0Sstevel@tonic-gate static int debug=0;
186*0Sstevel@tonic-gate #if 0
187*0Sstevel@tonic-gate /* Not used yet. */
188*0Sstevel@tonic-gate #ifdef FIONBIO
189*0Sstevel@tonic-gate static int s_nbio=0;
190*0Sstevel@tonic-gate #endif
191*0Sstevel@tonic-gate #endif
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate static const char rnd_seed[] = "string to make the random number generator think it has entropy";
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate int doit_biopair(SSL *s_ssl,SSL *c_ssl,long bytes,clock_t *s_time,clock_t *c_time);
196*0Sstevel@tonic-gate int doit(SSL *s_ssl,SSL *c_ssl,long bytes);
197*0Sstevel@tonic-gate static void sv_usage(void)
198*0Sstevel@tonic-gate 	{
199*0Sstevel@tonic-gate 	fprintf(stderr,"usage: ssltest [args ...]\n");
200*0Sstevel@tonic-gate 	fprintf(stderr,"\n");
201*0Sstevel@tonic-gate 	fprintf(stderr," -server_auth  - check server certificate\n");
202*0Sstevel@tonic-gate 	fprintf(stderr," -client_auth  - do client authentication\n");
203*0Sstevel@tonic-gate 	fprintf(stderr," -v            - more output\n");
204*0Sstevel@tonic-gate 	fprintf(stderr," -d            - debug output\n");
205*0Sstevel@tonic-gate 	fprintf(stderr," -reuse        - use session-id reuse\n");
206*0Sstevel@tonic-gate 	fprintf(stderr," -num <val>    - number of connections to perform\n");
207*0Sstevel@tonic-gate 	fprintf(stderr," -bytes <val>  - number of bytes to swap between client/server\n");
208*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
209*0Sstevel@tonic-gate 	fprintf(stderr," -dhe1024      - use 1024 bit key (safe prime) for DHE\n");
210*0Sstevel@tonic-gate 	fprintf(stderr," -dhe1024dsa   - use 1024 bit key (with 160-bit subprime) for DHE\n");
211*0Sstevel@tonic-gate 	fprintf(stderr," -no_dhe       - disable DHE\n");
212*0Sstevel@tonic-gate #endif
213*0Sstevel@tonic-gate #ifndef OPENSSL_NO_SSL2
214*0Sstevel@tonic-gate 	fprintf(stderr," -ssl2         - use SSLv2\n");
215*0Sstevel@tonic-gate #endif
216*0Sstevel@tonic-gate #ifndef OPENSSL_NO_SSL3
217*0Sstevel@tonic-gate 	fprintf(stderr," -ssl3         - use SSLv3\n");
218*0Sstevel@tonic-gate #endif
219*0Sstevel@tonic-gate #ifndef OPENSSL_NO_TLS1
220*0Sstevel@tonic-gate 	fprintf(stderr," -tls1         - use TLSv1\n");
221*0Sstevel@tonic-gate #endif
222*0Sstevel@tonic-gate 	fprintf(stderr," -CApath arg   - PEM format directory of CA's\n");
223*0Sstevel@tonic-gate 	fprintf(stderr," -CAfile arg   - PEM format file of CA's\n");
224*0Sstevel@tonic-gate 	fprintf(stderr," -cert arg     - Server certificate file\n");
225*0Sstevel@tonic-gate 	fprintf(stderr," -key arg      - Server key file (default: same as -cert)\n");
226*0Sstevel@tonic-gate 	fprintf(stderr," -c_cert arg   - Client certificate file\n");
227*0Sstevel@tonic-gate 	fprintf(stderr," -c_key arg    - Client key file (default: same as -c_cert)\n");
228*0Sstevel@tonic-gate 	fprintf(stderr," -cipher arg   - The cipher list\n");
229*0Sstevel@tonic-gate 	fprintf(stderr," -bio_pair     - Use BIO pairs\n");
230*0Sstevel@tonic-gate 	fprintf(stderr," -f            - Test even cases that can't work\n");
231*0Sstevel@tonic-gate 	fprintf(stderr," -time         - measure processor time used by client and server\n");
232*0Sstevel@tonic-gate 	fprintf(stderr," -zlib         - use zlib compression\n");
233*0Sstevel@tonic-gate 	fprintf(stderr," -time         - use rle compression\n");
234*0Sstevel@tonic-gate 	}
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate static void print_details(SSL *c_ssl, const char *prefix)
237*0Sstevel@tonic-gate 	{
238*0Sstevel@tonic-gate 	SSL_CIPHER *ciph;
239*0Sstevel@tonic-gate 	X509 *cert;
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 	ciph=SSL_get_current_cipher(c_ssl);
242*0Sstevel@tonic-gate 	BIO_printf(bio_stdout,"%s%s, cipher %s %s",
243*0Sstevel@tonic-gate 		prefix,
244*0Sstevel@tonic-gate 		SSL_get_version(c_ssl),
245*0Sstevel@tonic-gate 		SSL_CIPHER_get_version(ciph),
246*0Sstevel@tonic-gate 		SSL_CIPHER_get_name(ciph));
247*0Sstevel@tonic-gate 	cert=SSL_get_peer_certificate(c_ssl);
248*0Sstevel@tonic-gate 	if (cert != NULL)
249*0Sstevel@tonic-gate 		{
250*0Sstevel@tonic-gate 		EVP_PKEY *pkey = X509_get_pubkey(cert);
251*0Sstevel@tonic-gate 		if (pkey != NULL)
252*0Sstevel@tonic-gate 			{
253*0Sstevel@tonic-gate 			if (0)
254*0Sstevel@tonic-gate 				;
255*0Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
256*0Sstevel@tonic-gate 			else if (pkey->type == EVP_PKEY_RSA && pkey->pkey.rsa != NULL
257*0Sstevel@tonic-gate 				&& pkey->pkey.rsa->n != NULL)
258*0Sstevel@tonic-gate 				{
259*0Sstevel@tonic-gate 				BIO_printf(bio_stdout, ", %d bit RSA",
260*0Sstevel@tonic-gate 					BN_num_bits(pkey->pkey.rsa->n));
261*0Sstevel@tonic-gate 				}
262*0Sstevel@tonic-gate #endif
263*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
264*0Sstevel@tonic-gate 			else if (pkey->type == EVP_PKEY_DSA && pkey->pkey.dsa != NULL
265*0Sstevel@tonic-gate 				&& pkey->pkey.dsa->p != NULL)
266*0Sstevel@tonic-gate 				{
267*0Sstevel@tonic-gate 				BIO_printf(bio_stdout, ", %d bit DSA",
268*0Sstevel@tonic-gate 					BN_num_bits(pkey->pkey.dsa->p));
269*0Sstevel@tonic-gate 				}
270*0Sstevel@tonic-gate #endif
271*0Sstevel@tonic-gate 			EVP_PKEY_free(pkey);
272*0Sstevel@tonic-gate 			}
273*0Sstevel@tonic-gate 		X509_free(cert);
274*0Sstevel@tonic-gate 		}
275*0Sstevel@tonic-gate 	/* The SSL API does not allow us to look at temporary RSA/DH keys,
276*0Sstevel@tonic-gate 	 * otherwise we should print their lengths too */
277*0Sstevel@tonic-gate 	BIO_printf(bio_stdout,"\n");
278*0Sstevel@tonic-gate 	}
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate static void lock_dbg_cb(int mode, int type, const char *file, int line)
281*0Sstevel@tonic-gate 	{
282*0Sstevel@tonic-gate 	static int modes[CRYPTO_NUM_LOCKS]; /* = {0, 0, ... } */
283*0Sstevel@tonic-gate 	const char *errstr = NULL;
284*0Sstevel@tonic-gate 	int rw;
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 	rw = mode & (CRYPTO_READ|CRYPTO_WRITE);
287*0Sstevel@tonic-gate 	if (!((rw == CRYPTO_READ) || (rw == CRYPTO_WRITE)))
288*0Sstevel@tonic-gate 		{
289*0Sstevel@tonic-gate 		errstr = "invalid mode";
290*0Sstevel@tonic-gate 		goto err;
291*0Sstevel@tonic-gate 		}
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 	if (type < 0 || type >= CRYPTO_NUM_LOCKS)
294*0Sstevel@tonic-gate 		{
295*0Sstevel@tonic-gate 		errstr = "type out of bounds";
296*0Sstevel@tonic-gate 		goto err;
297*0Sstevel@tonic-gate 		}
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 	if (mode & CRYPTO_LOCK)
300*0Sstevel@tonic-gate 		{
301*0Sstevel@tonic-gate 		if (modes[type])
302*0Sstevel@tonic-gate 			{
303*0Sstevel@tonic-gate 			errstr = "already locked";
304*0Sstevel@tonic-gate 			/* must not happen in a single-threaded program
305*0Sstevel@tonic-gate 			 * (would deadlock) */
306*0Sstevel@tonic-gate 			goto err;
307*0Sstevel@tonic-gate 			}
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate 		modes[type] = rw;
310*0Sstevel@tonic-gate 		}
311*0Sstevel@tonic-gate 	else if (mode & CRYPTO_UNLOCK)
312*0Sstevel@tonic-gate 		{
313*0Sstevel@tonic-gate 		if (!modes[type])
314*0Sstevel@tonic-gate 			{
315*0Sstevel@tonic-gate 			errstr = "not locked";
316*0Sstevel@tonic-gate 			goto err;
317*0Sstevel@tonic-gate 			}
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 		if (modes[type] != rw)
320*0Sstevel@tonic-gate 			{
321*0Sstevel@tonic-gate 			errstr = (rw == CRYPTO_READ) ?
322*0Sstevel@tonic-gate 				"CRYPTO_r_unlock on write lock" :
323*0Sstevel@tonic-gate 				"CRYPTO_w_unlock on read lock";
324*0Sstevel@tonic-gate 			}
325*0Sstevel@tonic-gate 
326*0Sstevel@tonic-gate 		modes[type] = 0;
327*0Sstevel@tonic-gate 		}
328*0Sstevel@tonic-gate 	else
329*0Sstevel@tonic-gate 		{
330*0Sstevel@tonic-gate 		errstr = "invalid mode";
331*0Sstevel@tonic-gate 		goto err;
332*0Sstevel@tonic-gate 		}
333*0Sstevel@tonic-gate 
334*0Sstevel@tonic-gate  err:
335*0Sstevel@tonic-gate 	if (errstr)
336*0Sstevel@tonic-gate 		{
337*0Sstevel@tonic-gate 		/* we cannot use bio_err here */
338*0Sstevel@tonic-gate 		fprintf(stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
339*0Sstevel@tonic-gate 			errstr, mode, type, file, line);
340*0Sstevel@tonic-gate 		}
341*0Sstevel@tonic-gate 	}
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate int main(int argc, char *argv[])
344*0Sstevel@tonic-gate 	{
345*0Sstevel@tonic-gate 	char *CApath=NULL,*CAfile=NULL;
346*0Sstevel@tonic-gate 	int badop=0;
347*0Sstevel@tonic-gate 	int bio_pair=0;
348*0Sstevel@tonic-gate 	int force=0;
349*0Sstevel@tonic-gate 	int tls1=0,ssl2=0,ssl3=0,ret=1;
350*0Sstevel@tonic-gate 	int client_auth=0;
351*0Sstevel@tonic-gate 	int server_auth=0,i;
352*0Sstevel@tonic-gate 	int app_verify=0;
353*0Sstevel@tonic-gate 	char *server_cert=TEST_SERVER_CERT;
354*0Sstevel@tonic-gate 	char *server_key=NULL;
355*0Sstevel@tonic-gate 	char *client_cert=TEST_CLIENT_CERT;
356*0Sstevel@tonic-gate 	char *client_key=NULL;
357*0Sstevel@tonic-gate 	SSL_CTX *s_ctx=NULL;
358*0Sstevel@tonic-gate 	SSL_CTX *c_ctx=NULL;
359*0Sstevel@tonic-gate 	SSL_METHOD *meth=NULL;
360*0Sstevel@tonic-gate 	SSL *c_ssl,*s_ssl;
361*0Sstevel@tonic-gate 	int number=1,reuse=0;
362*0Sstevel@tonic-gate 	long bytes=1L;
363*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
364*0Sstevel@tonic-gate 	DH *dh;
365*0Sstevel@tonic-gate 	int dhe1024 = 0, dhe1024dsa = 0;
366*0Sstevel@tonic-gate #endif
367*0Sstevel@tonic-gate 	int no_dhe = 0;
368*0Sstevel@tonic-gate 	int print_time = 0;
369*0Sstevel@tonic-gate 	clock_t s_time = 0, c_time = 0;
370*0Sstevel@tonic-gate 	int comp = 0;
371*0Sstevel@tonic-gate 	COMP_METHOD *cm = NULL;
372*0Sstevel@tonic-gate 
373*0Sstevel@tonic-gate 	verbose = 0;
374*0Sstevel@tonic-gate 	debug = 0;
375*0Sstevel@tonic-gate 	cipher = 0;
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 	bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate 	CRYPTO_set_locking_callback(lock_dbg_cb);
380*0Sstevel@tonic-gate 
381*0Sstevel@tonic-gate 	/* enable memory leak checking unless explicitly disabled */
382*0Sstevel@tonic-gate 	if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
383*0Sstevel@tonic-gate 		{
384*0Sstevel@tonic-gate 		CRYPTO_malloc_debug_init();
385*0Sstevel@tonic-gate 		CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
386*0Sstevel@tonic-gate 		}
387*0Sstevel@tonic-gate 	else
388*0Sstevel@tonic-gate 		{
389*0Sstevel@tonic-gate 		/* OPENSSL_DEBUG_MEMORY=off */
390*0Sstevel@tonic-gate 		CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
391*0Sstevel@tonic-gate 		}
392*0Sstevel@tonic-gate 	CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
393*0Sstevel@tonic-gate 
394*0Sstevel@tonic-gate 	RAND_seed(rnd_seed, sizeof rnd_seed);
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 	bio_stdout=BIO_new_fp(stdout,BIO_NOCLOSE);
397*0Sstevel@tonic-gate 
398*0Sstevel@tonic-gate 	argc--;
399*0Sstevel@tonic-gate 	argv++;
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 	while (argc >= 1)
402*0Sstevel@tonic-gate 		{
403*0Sstevel@tonic-gate 		if	(strcmp(*argv,"-server_auth") == 0)
404*0Sstevel@tonic-gate 			server_auth=1;
405*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-client_auth") == 0)
406*0Sstevel@tonic-gate 			client_auth=1;
407*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-v") == 0)
408*0Sstevel@tonic-gate 			verbose=1;
409*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-d") == 0)
410*0Sstevel@tonic-gate 			debug=1;
411*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-reuse") == 0)
412*0Sstevel@tonic-gate 			reuse=1;
413*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-dhe1024") == 0)
414*0Sstevel@tonic-gate 			{
415*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
416*0Sstevel@tonic-gate 			dhe1024=1;
417*0Sstevel@tonic-gate #else
418*0Sstevel@tonic-gate 			fprintf(stderr,"ignoring -dhe1024, since I'm compiled without DH\n");
419*0Sstevel@tonic-gate #endif
420*0Sstevel@tonic-gate 			}
421*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-dhe1024dsa") == 0)
422*0Sstevel@tonic-gate 			{
423*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
424*0Sstevel@tonic-gate 			dhe1024dsa=1;
425*0Sstevel@tonic-gate #else
426*0Sstevel@tonic-gate 			fprintf(stderr,"ignoring -dhe1024, since I'm compiled without DH\n");
427*0Sstevel@tonic-gate #endif
428*0Sstevel@tonic-gate 			}
429*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-no_dhe") == 0)
430*0Sstevel@tonic-gate 			no_dhe=1;
431*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-ssl2") == 0)
432*0Sstevel@tonic-gate 			ssl2=1;
433*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-tls1") == 0)
434*0Sstevel@tonic-gate 			tls1=1;
435*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-ssl3") == 0)
436*0Sstevel@tonic-gate 			ssl3=1;
437*0Sstevel@tonic-gate 		else if	(strncmp(*argv,"-num",4) == 0)
438*0Sstevel@tonic-gate 			{
439*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
440*0Sstevel@tonic-gate 			number= atoi(*(++argv));
441*0Sstevel@tonic-gate 			if (number == 0) number=1;
442*0Sstevel@tonic-gate 			}
443*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-bytes") == 0)
444*0Sstevel@tonic-gate 			{
445*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
446*0Sstevel@tonic-gate 			bytes= atol(*(++argv));
447*0Sstevel@tonic-gate 			if (bytes == 0L) bytes=1L;
448*0Sstevel@tonic-gate 			i=strlen(argv[0]);
449*0Sstevel@tonic-gate 			if (argv[0][i-1] == 'k') bytes*=1024L;
450*0Sstevel@tonic-gate 			if (argv[0][i-1] == 'm') bytes*=1024L*1024L;
451*0Sstevel@tonic-gate 			}
452*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-cert") == 0)
453*0Sstevel@tonic-gate 			{
454*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
455*0Sstevel@tonic-gate 			server_cert= *(++argv);
456*0Sstevel@tonic-gate 			}
457*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-s_cert") == 0)
458*0Sstevel@tonic-gate 			{
459*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
460*0Sstevel@tonic-gate 			server_cert= *(++argv);
461*0Sstevel@tonic-gate 			}
462*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-key") == 0)
463*0Sstevel@tonic-gate 			{
464*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
465*0Sstevel@tonic-gate 			server_key= *(++argv);
466*0Sstevel@tonic-gate 			}
467*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-s_key") == 0)
468*0Sstevel@tonic-gate 			{
469*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
470*0Sstevel@tonic-gate 			server_key= *(++argv);
471*0Sstevel@tonic-gate 			}
472*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-c_cert") == 0)
473*0Sstevel@tonic-gate 			{
474*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
475*0Sstevel@tonic-gate 			client_cert= *(++argv);
476*0Sstevel@tonic-gate 			}
477*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-c_key") == 0)
478*0Sstevel@tonic-gate 			{
479*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
480*0Sstevel@tonic-gate 			client_key= *(++argv);
481*0Sstevel@tonic-gate 			}
482*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-cipher") == 0)
483*0Sstevel@tonic-gate 			{
484*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
485*0Sstevel@tonic-gate 			cipher= *(++argv);
486*0Sstevel@tonic-gate 			}
487*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-CApath") == 0)
488*0Sstevel@tonic-gate 			{
489*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
490*0Sstevel@tonic-gate 			CApath= *(++argv);
491*0Sstevel@tonic-gate 			}
492*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-CAfile") == 0)
493*0Sstevel@tonic-gate 			{
494*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
495*0Sstevel@tonic-gate 			CAfile= *(++argv);
496*0Sstevel@tonic-gate 			}
497*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-bio_pair") == 0)
498*0Sstevel@tonic-gate 			{
499*0Sstevel@tonic-gate 			bio_pair = 1;
500*0Sstevel@tonic-gate 			}
501*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-f") == 0)
502*0Sstevel@tonic-gate 			{
503*0Sstevel@tonic-gate 			force = 1;
504*0Sstevel@tonic-gate 			}
505*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-time") == 0)
506*0Sstevel@tonic-gate 			{
507*0Sstevel@tonic-gate 			print_time = 1;
508*0Sstevel@tonic-gate 			}
509*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-zlib") == 0)
510*0Sstevel@tonic-gate 			{
511*0Sstevel@tonic-gate 			comp = COMP_ZLIB;
512*0Sstevel@tonic-gate 			}
513*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-rle") == 0)
514*0Sstevel@tonic-gate 			{
515*0Sstevel@tonic-gate 			comp = COMP_RLE;
516*0Sstevel@tonic-gate 			}
517*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-app_verify") == 0)
518*0Sstevel@tonic-gate 			{
519*0Sstevel@tonic-gate 			app_verify = 1;
520*0Sstevel@tonic-gate 			}
521*0Sstevel@tonic-gate 		else
522*0Sstevel@tonic-gate 			{
523*0Sstevel@tonic-gate 			fprintf(stderr,"unknown option %s\n",*argv);
524*0Sstevel@tonic-gate 			badop=1;
525*0Sstevel@tonic-gate 			break;
526*0Sstevel@tonic-gate 			}
527*0Sstevel@tonic-gate 		argc--;
528*0Sstevel@tonic-gate 		argv++;
529*0Sstevel@tonic-gate 		}
530*0Sstevel@tonic-gate 	if (badop)
531*0Sstevel@tonic-gate 		{
532*0Sstevel@tonic-gate bad:
533*0Sstevel@tonic-gate 		sv_usage();
534*0Sstevel@tonic-gate 		goto end;
535*0Sstevel@tonic-gate 		}
536*0Sstevel@tonic-gate 
537*0Sstevel@tonic-gate 	if (!ssl2 && !ssl3 && !tls1 && number > 1 && !reuse && !force)
538*0Sstevel@tonic-gate 		{
539*0Sstevel@tonic-gate 		fprintf(stderr, "This case cannot work.  Use -f to perform "
540*0Sstevel@tonic-gate 			"the test anyway (and\n-d to see what happens), "
541*0Sstevel@tonic-gate 			"or add one of -ssl2, -ssl3, -tls1, -reuse\n"
542*0Sstevel@tonic-gate 			"to avoid protocol mismatch.\n");
543*0Sstevel@tonic-gate 		EXIT(1);
544*0Sstevel@tonic-gate 		}
545*0Sstevel@tonic-gate 
546*0Sstevel@tonic-gate 	if (print_time)
547*0Sstevel@tonic-gate 		{
548*0Sstevel@tonic-gate 		if (!bio_pair)
549*0Sstevel@tonic-gate 			{
550*0Sstevel@tonic-gate 			fprintf(stderr, "Using BIO pair (-bio_pair)\n");
551*0Sstevel@tonic-gate 			bio_pair = 1;
552*0Sstevel@tonic-gate 			}
553*0Sstevel@tonic-gate 		if (number < 50 && !force)
554*0Sstevel@tonic-gate 			fprintf(stderr, "Warning: For accurate timings, use more connections (e.g. -num 1000)\n");
555*0Sstevel@tonic-gate 		}
556*0Sstevel@tonic-gate 
557*0Sstevel@tonic-gate /*	if (cipher == NULL) cipher=getenv("SSL_CIPHER"); */
558*0Sstevel@tonic-gate 
559*0Sstevel@tonic-gate 	SSL_library_init();
560*0Sstevel@tonic-gate 	SSL_load_error_strings();
561*0Sstevel@tonic-gate 
562*0Sstevel@tonic-gate 	if (comp == COMP_ZLIB) cm = COMP_zlib();
563*0Sstevel@tonic-gate 	if (comp == COMP_RLE) cm = COMP_rle();
564*0Sstevel@tonic-gate 	if (cm != NULL)
565*0Sstevel@tonic-gate 		{
566*0Sstevel@tonic-gate 		if (cm->type != NID_undef)
567*0Sstevel@tonic-gate 			{
568*0Sstevel@tonic-gate 			if (SSL_COMP_add_compression_method(comp, cm) != 0)
569*0Sstevel@tonic-gate 				{
570*0Sstevel@tonic-gate 				fprintf(stderr,
571*0Sstevel@tonic-gate 					"Failed to add compression method\n");
572*0Sstevel@tonic-gate 				ERR_print_errors_fp(stderr);
573*0Sstevel@tonic-gate 				}
574*0Sstevel@tonic-gate 			}
575*0Sstevel@tonic-gate 		else
576*0Sstevel@tonic-gate 			{
577*0Sstevel@tonic-gate 			fprintf(stderr,
578*0Sstevel@tonic-gate 				"Warning: %s compression not supported\n",
579*0Sstevel@tonic-gate 				(comp == COMP_RLE ? "rle" :
580*0Sstevel@tonic-gate 					(comp == COMP_ZLIB ? "zlib" :
581*0Sstevel@tonic-gate 						"unknown")));
582*0Sstevel@tonic-gate 			ERR_print_errors_fp(stderr);
583*0Sstevel@tonic-gate 			}
584*0Sstevel@tonic-gate 		}
585*0Sstevel@tonic-gate 
586*0Sstevel@tonic-gate #if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
587*0Sstevel@tonic-gate 	if (ssl2)
588*0Sstevel@tonic-gate 		meth=SSLv2_method();
589*0Sstevel@tonic-gate 	else
590*0Sstevel@tonic-gate 	if (tls1)
591*0Sstevel@tonic-gate 		meth=TLSv1_method();
592*0Sstevel@tonic-gate 	else
593*0Sstevel@tonic-gate 	if (ssl3)
594*0Sstevel@tonic-gate 		meth=SSLv3_method();
595*0Sstevel@tonic-gate 	else
596*0Sstevel@tonic-gate 		meth=SSLv23_method();
597*0Sstevel@tonic-gate #else
598*0Sstevel@tonic-gate #ifdef OPENSSL_NO_SSL2
599*0Sstevel@tonic-gate 	meth=SSLv3_method();
600*0Sstevel@tonic-gate #else
601*0Sstevel@tonic-gate 	meth=SSLv2_method();
602*0Sstevel@tonic-gate #endif
603*0Sstevel@tonic-gate #endif
604*0Sstevel@tonic-gate 
605*0Sstevel@tonic-gate 	c_ctx=SSL_CTX_new(meth);
606*0Sstevel@tonic-gate 	s_ctx=SSL_CTX_new(meth);
607*0Sstevel@tonic-gate 	if ((c_ctx == NULL) || (s_ctx == NULL))
608*0Sstevel@tonic-gate 		{
609*0Sstevel@tonic-gate 		ERR_print_errors(bio_err);
610*0Sstevel@tonic-gate 		goto end;
611*0Sstevel@tonic-gate 		}
612*0Sstevel@tonic-gate 
613*0Sstevel@tonic-gate 	if (cipher != NULL)
614*0Sstevel@tonic-gate 		{
615*0Sstevel@tonic-gate 		SSL_CTX_set_cipher_list(c_ctx,cipher);
616*0Sstevel@tonic-gate 		SSL_CTX_set_cipher_list(s_ctx,cipher);
617*0Sstevel@tonic-gate 		}
618*0Sstevel@tonic-gate 
619*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
620*0Sstevel@tonic-gate 	if (!no_dhe)
621*0Sstevel@tonic-gate 		{
622*0Sstevel@tonic-gate 		if (dhe1024dsa)
623*0Sstevel@tonic-gate 			{
624*0Sstevel@tonic-gate 			/* use SSL_OP_SINGLE_DH_USE to avoid small subgroup attacks */
625*0Sstevel@tonic-gate 			SSL_CTX_set_options(s_ctx, SSL_OP_SINGLE_DH_USE);
626*0Sstevel@tonic-gate 			dh=get_dh1024dsa();
627*0Sstevel@tonic-gate 			}
628*0Sstevel@tonic-gate 		else if (dhe1024)
629*0Sstevel@tonic-gate 			dh=get_dh1024();
630*0Sstevel@tonic-gate 		else
631*0Sstevel@tonic-gate 			dh=get_dh512();
632*0Sstevel@tonic-gate 		SSL_CTX_set_tmp_dh(s_ctx,dh);
633*0Sstevel@tonic-gate 		DH_free(dh);
634*0Sstevel@tonic-gate 		}
635*0Sstevel@tonic-gate #else
636*0Sstevel@tonic-gate 	(void)no_dhe;
637*0Sstevel@tonic-gate #endif
638*0Sstevel@tonic-gate 
639*0Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
640*0Sstevel@tonic-gate 	SSL_CTX_set_tmp_rsa_callback(s_ctx,tmp_rsa_cb);
641*0Sstevel@tonic-gate #endif
642*0Sstevel@tonic-gate 
643*0Sstevel@tonic-gate 	if (!SSL_CTX_use_certificate_file(s_ctx,server_cert,SSL_FILETYPE_PEM))
644*0Sstevel@tonic-gate 		{
645*0Sstevel@tonic-gate 		ERR_print_errors(bio_err);
646*0Sstevel@tonic-gate 		}
647*0Sstevel@tonic-gate 	else if (!SSL_CTX_use_PrivateKey_file(s_ctx,
648*0Sstevel@tonic-gate 		(server_key?server_key:server_cert), SSL_FILETYPE_PEM))
649*0Sstevel@tonic-gate 		{
650*0Sstevel@tonic-gate 		ERR_print_errors(bio_err);
651*0Sstevel@tonic-gate 		goto end;
652*0Sstevel@tonic-gate 		}
653*0Sstevel@tonic-gate 
654*0Sstevel@tonic-gate 	if (client_auth)
655*0Sstevel@tonic-gate 		{
656*0Sstevel@tonic-gate 		SSL_CTX_use_certificate_file(c_ctx,client_cert,
657*0Sstevel@tonic-gate 			SSL_FILETYPE_PEM);
658*0Sstevel@tonic-gate 		SSL_CTX_use_PrivateKey_file(c_ctx,
659*0Sstevel@tonic-gate 			(client_key?client_key:client_cert),
660*0Sstevel@tonic-gate 			SSL_FILETYPE_PEM);
661*0Sstevel@tonic-gate 		}
662*0Sstevel@tonic-gate 
663*0Sstevel@tonic-gate 	if (	(!SSL_CTX_load_verify_locations(s_ctx,CAfile,CApath)) ||
664*0Sstevel@tonic-gate 		(!SSL_CTX_set_default_verify_paths(s_ctx)) ||
665*0Sstevel@tonic-gate 		(!SSL_CTX_load_verify_locations(c_ctx,CAfile,CApath)) ||
666*0Sstevel@tonic-gate 		(!SSL_CTX_set_default_verify_paths(c_ctx)))
667*0Sstevel@tonic-gate 		{
668*0Sstevel@tonic-gate 		/* fprintf(stderr,"SSL_load_verify_locations\n"); */
669*0Sstevel@tonic-gate 		ERR_print_errors(bio_err);
670*0Sstevel@tonic-gate 		/* goto end; */
671*0Sstevel@tonic-gate 		}
672*0Sstevel@tonic-gate 
673*0Sstevel@tonic-gate 	if (client_auth)
674*0Sstevel@tonic-gate 		{
675*0Sstevel@tonic-gate 		BIO_printf(bio_err,"client authentication\n");
676*0Sstevel@tonic-gate 		SSL_CTX_set_verify(s_ctx,
677*0Sstevel@tonic-gate 			SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
678*0Sstevel@tonic-gate 			verify_callback);
679*0Sstevel@tonic-gate 		if (app_verify)
680*0Sstevel@tonic-gate 			{
681*0Sstevel@tonic-gate 			SSL_CTX_set_cert_verify_callback(s_ctx, app_verify_callback, app_verify_arg);
682*0Sstevel@tonic-gate 			}
683*0Sstevel@tonic-gate 		}
684*0Sstevel@tonic-gate 	if (server_auth)
685*0Sstevel@tonic-gate 		{
686*0Sstevel@tonic-gate 		BIO_printf(bio_err,"server authentication\n");
687*0Sstevel@tonic-gate 		SSL_CTX_set_verify(c_ctx,SSL_VERIFY_PEER,
688*0Sstevel@tonic-gate 			verify_callback);
689*0Sstevel@tonic-gate 		if (app_verify)
690*0Sstevel@tonic-gate 			{
691*0Sstevel@tonic-gate 			SSL_CTX_set_cert_verify_callback(s_ctx, app_verify_callback, app_verify_arg);
692*0Sstevel@tonic-gate 			}
693*0Sstevel@tonic-gate 		}
694*0Sstevel@tonic-gate 
695*0Sstevel@tonic-gate 	{
696*0Sstevel@tonic-gate 		int session_id_context = 0;
697*0Sstevel@tonic-gate 		SSL_CTX_set_session_id_context(s_ctx, (void *)&session_id_context, sizeof session_id_context);
698*0Sstevel@tonic-gate 	}
699*0Sstevel@tonic-gate 
700*0Sstevel@tonic-gate 	c_ssl=SSL_new(c_ctx);
701*0Sstevel@tonic-gate 	s_ssl=SSL_new(s_ctx);
702*0Sstevel@tonic-gate 
703*0Sstevel@tonic-gate #ifndef OPENSSL_NO_KRB5
704*0Sstevel@tonic-gate 	if (c_ssl  &&  c_ssl->kssl_ctx)
705*0Sstevel@tonic-gate                 {
706*0Sstevel@tonic-gate                 char	localhost[MAXHOSTNAMELEN+2];
707*0Sstevel@tonic-gate 
708*0Sstevel@tonic-gate 		if (gethostname(localhost, sizeof localhost-1) == 0)
709*0Sstevel@tonic-gate                         {
710*0Sstevel@tonic-gate 			localhost[sizeof localhost-1]='\0';
711*0Sstevel@tonic-gate 			if(strlen(localhost) == sizeof localhost-1)
712*0Sstevel@tonic-gate 				{
713*0Sstevel@tonic-gate 				BIO_printf(bio_err,"localhost name too long\n");
714*0Sstevel@tonic-gate 				goto end;
715*0Sstevel@tonic-gate 				}
716*0Sstevel@tonic-gate 			kssl_ctx_setstring(c_ssl->kssl_ctx, KSSL_SERVER,
717*0Sstevel@tonic-gate                                 localhost);
718*0Sstevel@tonic-gate 			}
719*0Sstevel@tonic-gate 		}
720*0Sstevel@tonic-gate #endif    /* OPENSSL_NO_KRB5  */
721*0Sstevel@tonic-gate 
722*0Sstevel@tonic-gate 	for (i=0; i<number; i++)
723*0Sstevel@tonic-gate 		{
724*0Sstevel@tonic-gate 		if (!reuse) SSL_set_session(c_ssl,NULL);
725*0Sstevel@tonic-gate 		if (bio_pair)
726*0Sstevel@tonic-gate 			ret=doit_biopair(s_ssl,c_ssl,bytes,&s_time,&c_time);
727*0Sstevel@tonic-gate 		else
728*0Sstevel@tonic-gate 			ret=doit(s_ssl,c_ssl,bytes);
729*0Sstevel@tonic-gate 		}
730*0Sstevel@tonic-gate 
731*0Sstevel@tonic-gate 	if (!verbose)
732*0Sstevel@tonic-gate 		{
733*0Sstevel@tonic-gate 		print_details(c_ssl, "");
734*0Sstevel@tonic-gate 		}
735*0Sstevel@tonic-gate 	if ((number > 1) || (bytes > 1L))
736*0Sstevel@tonic-gate 		BIO_printf(bio_stdout, "%d handshakes of %ld bytes done\n",number,bytes);
737*0Sstevel@tonic-gate 	if (print_time)
738*0Sstevel@tonic-gate 		{
739*0Sstevel@tonic-gate #ifdef CLOCKS_PER_SEC
740*0Sstevel@tonic-gate 		/* "To determine the time in seconds, the value returned
741*0Sstevel@tonic-gate 		 * by the clock function should be divided by the value
742*0Sstevel@tonic-gate 		 * of the macro CLOCKS_PER_SEC."
743*0Sstevel@tonic-gate 		 *                                       -- ISO/IEC 9899 */
744*0Sstevel@tonic-gate 		BIO_printf(bio_stdout, "Approximate total server time: %6.2f s\n"
745*0Sstevel@tonic-gate 			"Approximate total client time: %6.2f s\n",
746*0Sstevel@tonic-gate 			(double)s_time/CLOCKS_PER_SEC,
747*0Sstevel@tonic-gate 			(double)c_time/CLOCKS_PER_SEC);
748*0Sstevel@tonic-gate #else
749*0Sstevel@tonic-gate 		/* "`CLOCKS_PER_SEC' undeclared (first use this function)"
750*0Sstevel@tonic-gate 		 *                            -- cc on NeXTstep/OpenStep */
751*0Sstevel@tonic-gate 		BIO_printf(bio_stdout,
752*0Sstevel@tonic-gate 			"Approximate total server time: %6.2f units\n"
753*0Sstevel@tonic-gate 			"Approximate total client time: %6.2f units\n",
754*0Sstevel@tonic-gate 			(double)s_time,
755*0Sstevel@tonic-gate 			(double)c_time);
756*0Sstevel@tonic-gate #endif
757*0Sstevel@tonic-gate 		}
758*0Sstevel@tonic-gate 
759*0Sstevel@tonic-gate 	SSL_free(s_ssl);
760*0Sstevel@tonic-gate 	SSL_free(c_ssl);
761*0Sstevel@tonic-gate 
762*0Sstevel@tonic-gate end:
763*0Sstevel@tonic-gate 	if (s_ctx != NULL) SSL_CTX_free(s_ctx);
764*0Sstevel@tonic-gate 	if (c_ctx != NULL) SSL_CTX_free(c_ctx);
765*0Sstevel@tonic-gate 
766*0Sstevel@tonic-gate 	if (bio_stdout != NULL) BIO_free(bio_stdout);
767*0Sstevel@tonic-gate 
768*0Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
769*0Sstevel@tonic-gate 	free_tmp_rsa();
770*0Sstevel@tonic-gate #endif
771*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
772*0Sstevel@tonic-gate 	ENGINE_cleanup();
773*0Sstevel@tonic-gate #endif
774*0Sstevel@tonic-gate 	CRYPTO_cleanup_all_ex_data();
775*0Sstevel@tonic-gate 	ERR_free_strings();
776*0Sstevel@tonic-gate 	ERR_remove_state(0);
777*0Sstevel@tonic-gate 	EVP_cleanup();
778*0Sstevel@tonic-gate 	CRYPTO_mem_leaks(bio_err);
779*0Sstevel@tonic-gate 	if (bio_err != NULL) BIO_free(bio_err);
780*0Sstevel@tonic-gate 	EXIT(ret);
781*0Sstevel@tonic-gate 	}
782*0Sstevel@tonic-gate 
783*0Sstevel@tonic-gate int doit_biopair(SSL *s_ssl, SSL *c_ssl, long count,
784*0Sstevel@tonic-gate 	clock_t *s_time, clock_t *c_time)
785*0Sstevel@tonic-gate 	{
786*0Sstevel@tonic-gate 	long cw_num = count, cr_num = count, sw_num = count, sr_num = count;
787*0Sstevel@tonic-gate 	BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL;
788*0Sstevel@tonic-gate 	BIO *server = NULL, *server_io = NULL, *client = NULL, *client_io = NULL;
789*0Sstevel@tonic-gate 	int ret = 1;
790*0Sstevel@tonic-gate 
791*0Sstevel@tonic-gate 	size_t bufsiz = 256; /* small buffer for testing */
792*0Sstevel@tonic-gate 
793*0Sstevel@tonic-gate 	if (!BIO_new_bio_pair(&server, bufsiz, &server_io, bufsiz))
794*0Sstevel@tonic-gate 		goto err;
795*0Sstevel@tonic-gate 	if (!BIO_new_bio_pair(&client, bufsiz, &client_io, bufsiz))
796*0Sstevel@tonic-gate 		goto err;
797*0Sstevel@tonic-gate 
798*0Sstevel@tonic-gate 	s_ssl_bio = BIO_new(BIO_f_ssl());
799*0Sstevel@tonic-gate 	if (!s_ssl_bio)
800*0Sstevel@tonic-gate 		goto err;
801*0Sstevel@tonic-gate 
802*0Sstevel@tonic-gate 	c_ssl_bio = BIO_new(BIO_f_ssl());
803*0Sstevel@tonic-gate 	if (!c_ssl_bio)
804*0Sstevel@tonic-gate 		goto err;
805*0Sstevel@tonic-gate 
806*0Sstevel@tonic-gate 	SSL_set_connect_state(c_ssl);
807*0Sstevel@tonic-gate 	SSL_set_bio(c_ssl, client, client);
808*0Sstevel@tonic-gate 	(void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE);
809*0Sstevel@tonic-gate 
810*0Sstevel@tonic-gate 	SSL_set_accept_state(s_ssl);
811*0Sstevel@tonic-gate 	SSL_set_bio(s_ssl, server, server);
812*0Sstevel@tonic-gate 	(void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE);
813*0Sstevel@tonic-gate 
814*0Sstevel@tonic-gate 	do
815*0Sstevel@tonic-gate 		{
816*0Sstevel@tonic-gate 		/* c_ssl_bio:          SSL filter BIO
817*0Sstevel@tonic-gate 		 *
818*0Sstevel@tonic-gate 		 * client:             pseudo-I/O for SSL library
819*0Sstevel@tonic-gate 		 *
820*0Sstevel@tonic-gate 		 * client_io:          client's SSL communication; usually to be
821*0Sstevel@tonic-gate 		 *                     relayed over some I/O facility, but in this
822*0Sstevel@tonic-gate 		 *                     test program, we're the server, too:
823*0Sstevel@tonic-gate 		 *
824*0Sstevel@tonic-gate 		 * server_io:          server's SSL communication
825*0Sstevel@tonic-gate 		 *
826*0Sstevel@tonic-gate 		 * server:             pseudo-I/O for SSL library
827*0Sstevel@tonic-gate 		 *
828*0Sstevel@tonic-gate 		 * s_ssl_bio:          SSL filter BIO
829*0Sstevel@tonic-gate 		 *
830*0Sstevel@tonic-gate 		 * The client and the server each employ a "BIO pair":
831*0Sstevel@tonic-gate 		 * client + client_io, server + server_io.
832*0Sstevel@tonic-gate 		 * BIO pairs are symmetric.  A BIO pair behaves similar
833*0Sstevel@tonic-gate 		 * to a non-blocking socketpair (but both endpoints must
834*0Sstevel@tonic-gate 		 * be handled by the same thread).
835*0Sstevel@tonic-gate 		 * [Here we could connect client and server to the ends
836*0Sstevel@tonic-gate 		 * of a single BIO pair, but then this code would be less
837*0Sstevel@tonic-gate 		 * suitable as an example for BIO pairs in general.]
838*0Sstevel@tonic-gate 		 *
839*0Sstevel@tonic-gate 		 * Useful functions for querying the state of BIO pair endpoints:
840*0Sstevel@tonic-gate 		 *
841*0Sstevel@tonic-gate 		 * BIO_ctrl_pending(bio)              number of bytes we can read now
842*0Sstevel@tonic-gate 		 * BIO_ctrl_get_read_request(bio)     number of bytes needed to fulfil
843*0Sstevel@tonic-gate 		 *                                      other side's read attempt
844*0Sstevel@tonic-gate 		 * BIO_ctrl_get_write_guarantee(bio)   number of bytes we can write now
845*0Sstevel@tonic-gate 		 *
846*0Sstevel@tonic-gate 		 * ..._read_request is never more than ..._write_guarantee;
847*0Sstevel@tonic-gate 		 * it depends on the application which one you should use.
848*0Sstevel@tonic-gate 		 */
849*0Sstevel@tonic-gate 
850*0Sstevel@tonic-gate 		/* We have non-blocking behaviour throughout this test program, but
851*0Sstevel@tonic-gate 		 * can be sure that there is *some* progress in each iteration; so
852*0Sstevel@tonic-gate 		 * we don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE
853*0Sstevel@tonic-gate 		 * -- we just try everything in each iteration
854*0Sstevel@tonic-gate 		 */
855*0Sstevel@tonic-gate 
856*0Sstevel@tonic-gate 			{
857*0Sstevel@tonic-gate 			/* CLIENT */
858*0Sstevel@tonic-gate 
859*0Sstevel@tonic-gate 			MS_STATIC char cbuf[1024*8];
860*0Sstevel@tonic-gate 			int i, r;
861*0Sstevel@tonic-gate 			clock_t c_clock = clock();
862*0Sstevel@tonic-gate 
863*0Sstevel@tonic-gate 			memset(cbuf, 0, sizeof(cbuf));
864*0Sstevel@tonic-gate 
865*0Sstevel@tonic-gate 			if (debug)
866*0Sstevel@tonic-gate 				if (SSL_in_init(c_ssl))
867*0Sstevel@tonic-gate 					printf("client waiting in SSL_connect - %s\n",
868*0Sstevel@tonic-gate 						SSL_state_string_long(c_ssl));
869*0Sstevel@tonic-gate 
870*0Sstevel@tonic-gate 			if (cw_num > 0)
871*0Sstevel@tonic-gate 				{
872*0Sstevel@tonic-gate 				/* Write to server. */
873*0Sstevel@tonic-gate 
874*0Sstevel@tonic-gate 				if (cw_num > (long)sizeof cbuf)
875*0Sstevel@tonic-gate 					i = sizeof cbuf;
876*0Sstevel@tonic-gate 				else
877*0Sstevel@tonic-gate 					i = (int)cw_num;
878*0Sstevel@tonic-gate 				r = BIO_write(c_ssl_bio, cbuf, i);
879*0Sstevel@tonic-gate 				if (r < 0)
880*0Sstevel@tonic-gate 					{
881*0Sstevel@tonic-gate 					if (!BIO_should_retry(c_ssl_bio))
882*0Sstevel@tonic-gate 						{
883*0Sstevel@tonic-gate 						fprintf(stderr,"ERROR in CLIENT\n");
884*0Sstevel@tonic-gate 						goto err;
885*0Sstevel@tonic-gate 						}
886*0Sstevel@tonic-gate 					/* BIO_should_retry(...) can just be ignored here.
887*0Sstevel@tonic-gate 					 * The library expects us to call BIO_write with
888*0Sstevel@tonic-gate 					 * the same arguments again, and that's what we will
889*0Sstevel@tonic-gate 					 * do in the next iteration. */
890*0Sstevel@tonic-gate 					}
891*0Sstevel@tonic-gate 				else if (r == 0)
892*0Sstevel@tonic-gate 					{
893*0Sstevel@tonic-gate 					fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
894*0Sstevel@tonic-gate 					goto err;
895*0Sstevel@tonic-gate 					}
896*0Sstevel@tonic-gate 				else
897*0Sstevel@tonic-gate 					{
898*0Sstevel@tonic-gate 					if (debug)
899*0Sstevel@tonic-gate 						printf("client wrote %d\n", r);
900*0Sstevel@tonic-gate 					cw_num -= r;
901*0Sstevel@tonic-gate 					}
902*0Sstevel@tonic-gate 				}
903*0Sstevel@tonic-gate 
904*0Sstevel@tonic-gate 			if (cr_num > 0)
905*0Sstevel@tonic-gate 				{
906*0Sstevel@tonic-gate 				/* Read from server. */
907*0Sstevel@tonic-gate 
908*0Sstevel@tonic-gate 				r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf));
909*0Sstevel@tonic-gate 				if (r < 0)
910*0Sstevel@tonic-gate 					{
911*0Sstevel@tonic-gate 					if (!BIO_should_retry(c_ssl_bio))
912*0Sstevel@tonic-gate 						{
913*0Sstevel@tonic-gate 						fprintf(stderr,"ERROR in CLIENT\n");
914*0Sstevel@tonic-gate 						goto err;
915*0Sstevel@tonic-gate 						}
916*0Sstevel@tonic-gate 					/* Again, "BIO_should_retry" can be ignored. */
917*0Sstevel@tonic-gate 					}
918*0Sstevel@tonic-gate 				else if (r == 0)
919*0Sstevel@tonic-gate 					{
920*0Sstevel@tonic-gate 					fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
921*0Sstevel@tonic-gate 					goto err;
922*0Sstevel@tonic-gate 					}
923*0Sstevel@tonic-gate 				else
924*0Sstevel@tonic-gate 					{
925*0Sstevel@tonic-gate 					if (debug)
926*0Sstevel@tonic-gate 						printf("client read %d\n", r);
927*0Sstevel@tonic-gate 					cr_num -= r;
928*0Sstevel@tonic-gate 					}
929*0Sstevel@tonic-gate 				}
930*0Sstevel@tonic-gate 
931*0Sstevel@tonic-gate 			/* c_time and s_time increments will typically be very small
932*0Sstevel@tonic-gate 			 * (depending on machine speed and clock tick intervals),
933*0Sstevel@tonic-gate 			 * but sampling over a large number of connections should
934*0Sstevel@tonic-gate 			 * result in fairly accurate figures.  We cannot guarantee
935*0Sstevel@tonic-gate 			 * a lot, however -- if each connection lasts for exactly
936*0Sstevel@tonic-gate 			 * one clock tick, it will be counted only for the client
937*0Sstevel@tonic-gate 			 * or only for the server or even not at all.
938*0Sstevel@tonic-gate 			 */
939*0Sstevel@tonic-gate 			*c_time += (clock() - c_clock);
940*0Sstevel@tonic-gate 			}
941*0Sstevel@tonic-gate 
942*0Sstevel@tonic-gate 			{
943*0Sstevel@tonic-gate 			/* SERVER */
944*0Sstevel@tonic-gate 
945*0Sstevel@tonic-gate 			MS_STATIC char sbuf[1024*8];
946*0Sstevel@tonic-gate 			int i, r;
947*0Sstevel@tonic-gate 			clock_t s_clock = clock();
948*0Sstevel@tonic-gate 
949*0Sstevel@tonic-gate 			memset(sbuf, 0, sizeof(sbuf));
950*0Sstevel@tonic-gate 
951*0Sstevel@tonic-gate 			if (debug)
952*0Sstevel@tonic-gate 				if (SSL_in_init(s_ssl))
953*0Sstevel@tonic-gate 					printf("server waiting in SSL_accept - %s\n",
954*0Sstevel@tonic-gate 						SSL_state_string_long(s_ssl));
955*0Sstevel@tonic-gate 
956*0Sstevel@tonic-gate 			if (sw_num > 0)
957*0Sstevel@tonic-gate 				{
958*0Sstevel@tonic-gate 				/* Write to client. */
959*0Sstevel@tonic-gate 
960*0Sstevel@tonic-gate 				if (sw_num > (long)sizeof sbuf)
961*0Sstevel@tonic-gate 					i = sizeof sbuf;
962*0Sstevel@tonic-gate 				else
963*0Sstevel@tonic-gate 					i = (int)sw_num;
964*0Sstevel@tonic-gate 				r = BIO_write(s_ssl_bio, sbuf, i);
965*0Sstevel@tonic-gate 				if (r < 0)
966*0Sstevel@tonic-gate 					{
967*0Sstevel@tonic-gate 					if (!BIO_should_retry(s_ssl_bio))
968*0Sstevel@tonic-gate 						{
969*0Sstevel@tonic-gate 						fprintf(stderr,"ERROR in SERVER\n");
970*0Sstevel@tonic-gate 						goto err;
971*0Sstevel@tonic-gate 						}
972*0Sstevel@tonic-gate 					/* Ignore "BIO_should_retry". */
973*0Sstevel@tonic-gate 					}
974*0Sstevel@tonic-gate 				else if (r == 0)
975*0Sstevel@tonic-gate 					{
976*0Sstevel@tonic-gate 					fprintf(stderr,"SSL SERVER STARTUP FAILED\n");
977*0Sstevel@tonic-gate 					goto err;
978*0Sstevel@tonic-gate 					}
979*0Sstevel@tonic-gate 				else
980*0Sstevel@tonic-gate 					{
981*0Sstevel@tonic-gate 					if (debug)
982*0Sstevel@tonic-gate 						printf("server wrote %d\n", r);
983*0Sstevel@tonic-gate 					sw_num -= r;
984*0Sstevel@tonic-gate 					}
985*0Sstevel@tonic-gate 				}
986*0Sstevel@tonic-gate 
987*0Sstevel@tonic-gate 			if (sr_num > 0)
988*0Sstevel@tonic-gate 				{
989*0Sstevel@tonic-gate 				/* Read from client. */
990*0Sstevel@tonic-gate 
991*0Sstevel@tonic-gate 				r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf));
992*0Sstevel@tonic-gate 				if (r < 0)
993*0Sstevel@tonic-gate 					{
994*0Sstevel@tonic-gate 					if (!BIO_should_retry(s_ssl_bio))
995*0Sstevel@tonic-gate 						{
996*0Sstevel@tonic-gate 						fprintf(stderr,"ERROR in SERVER\n");
997*0Sstevel@tonic-gate 						goto err;
998*0Sstevel@tonic-gate 						}
999*0Sstevel@tonic-gate 					/* blah, blah */
1000*0Sstevel@tonic-gate 					}
1001*0Sstevel@tonic-gate 				else if (r == 0)
1002*0Sstevel@tonic-gate 					{
1003*0Sstevel@tonic-gate 					fprintf(stderr,"SSL SERVER STARTUP FAILED\n");
1004*0Sstevel@tonic-gate 					goto err;
1005*0Sstevel@tonic-gate 					}
1006*0Sstevel@tonic-gate 				else
1007*0Sstevel@tonic-gate 					{
1008*0Sstevel@tonic-gate 					if (debug)
1009*0Sstevel@tonic-gate 						printf("server read %d\n", r);
1010*0Sstevel@tonic-gate 					sr_num -= r;
1011*0Sstevel@tonic-gate 					}
1012*0Sstevel@tonic-gate 				}
1013*0Sstevel@tonic-gate 
1014*0Sstevel@tonic-gate 			*s_time += (clock() - s_clock);
1015*0Sstevel@tonic-gate 			}
1016*0Sstevel@tonic-gate 
1017*0Sstevel@tonic-gate 			{
1018*0Sstevel@tonic-gate 			/* "I/O" BETWEEN CLIENT AND SERVER. */
1019*0Sstevel@tonic-gate 
1020*0Sstevel@tonic-gate 			size_t r1, r2;
1021*0Sstevel@tonic-gate 			BIO *io1 = server_io, *io2 = client_io;
1022*0Sstevel@tonic-gate 			/* we use the non-copying interface for io1
1023*0Sstevel@tonic-gate 			 * and the standard BIO_write/BIO_read interface for io2
1024*0Sstevel@tonic-gate 			 */
1025*0Sstevel@tonic-gate 
1026*0Sstevel@tonic-gate 			static int prev_progress = 1;
1027*0Sstevel@tonic-gate 			int progress = 0;
1028*0Sstevel@tonic-gate 
1029*0Sstevel@tonic-gate 			/* io1 to io2 */
1030*0Sstevel@tonic-gate 			do
1031*0Sstevel@tonic-gate 				{
1032*0Sstevel@tonic-gate 				size_t num;
1033*0Sstevel@tonic-gate 				int r;
1034*0Sstevel@tonic-gate 
1035*0Sstevel@tonic-gate 				r1 = BIO_ctrl_pending(io1);
1036*0Sstevel@tonic-gate 				r2 = BIO_ctrl_get_write_guarantee(io2);
1037*0Sstevel@tonic-gate 
1038*0Sstevel@tonic-gate 				num = r1;
1039*0Sstevel@tonic-gate 				if (r2 < num)
1040*0Sstevel@tonic-gate 					num = r2;
1041*0Sstevel@tonic-gate 				if (num)
1042*0Sstevel@tonic-gate 					{
1043*0Sstevel@tonic-gate 					char *dataptr;
1044*0Sstevel@tonic-gate 
1045*0Sstevel@tonic-gate 					if (INT_MAX < num) /* yeah, right */
1046*0Sstevel@tonic-gate 						num = INT_MAX;
1047*0Sstevel@tonic-gate 
1048*0Sstevel@tonic-gate 					r = BIO_nread(io1, &dataptr, (int)num);
1049*0Sstevel@tonic-gate 					assert(r > 0);
1050*0Sstevel@tonic-gate 					assert(r <= (int)num);
1051*0Sstevel@tonic-gate 					/* possibly r < num (non-contiguous data) */
1052*0Sstevel@tonic-gate 					num = r;
1053*0Sstevel@tonic-gate 					r = BIO_write(io2, dataptr, (int)num);
1054*0Sstevel@tonic-gate 					if (r != (int)num) /* can't happen */
1055*0Sstevel@tonic-gate 						{
1056*0Sstevel@tonic-gate 						fprintf(stderr, "ERROR: BIO_write could not write "
1057*0Sstevel@tonic-gate 							"BIO_ctrl_get_write_guarantee() bytes");
1058*0Sstevel@tonic-gate 						goto err;
1059*0Sstevel@tonic-gate 						}
1060*0Sstevel@tonic-gate 					progress = 1;
1061*0Sstevel@tonic-gate 
1062*0Sstevel@tonic-gate 					if (debug)
1063*0Sstevel@tonic-gate 						printf((io1 == client_io) ?
1064*0Sstevel@tonic-gate 							"C->S relaying: %d bytes\n" :
1065*0Sstevel@tonic-gate 							"S->C relaying: %d bytes\n",
1066*0Sstevel@tonic-gate 							(int)num);
1067*0Sstevel@tonic-gate 					}
1068*0Sstevel@tonic-gate 				}
1069*0Sstevel@tonic-gate 			while (r1 && r2);
1070*0Sstevel@tonic-gate 
1071*0Sstevel@tonic-gate 			/* io2 to io1 */
1072*0Sstevel@tonic-gate 			{
1073*0Sstevel@tonic-gate 				size_t num;
1074*0Sstevel@tonic-gate 				int r;
1075*0Sstevel@tonic-gate 
1076*0Sstevel@tonic-gate 				r1 = BIO_ctrl_pending(io2);
1077*0Sstevel@tonic-gate 				r2 = BIO_ctrl_get_read_request(io1);
1078*0Sstevel@tonic-gate 				/* here we could use ..._get_write_guarantee instead of
1079*0Sstevel@tonic-gate 				 * ..._get_read_request, but by using the latter
1080*0Sstevel@tonic-gate 				 * we test restartability of the SSL implementation
1081*0Sstevel@tonic-gate 				 * more thoroughly */
1082*0Sstevel@tonic-gate 				num = r1;
1083*0Sstevel@tonic-gate 				if (r2 < num)
1084*0Sstevel@tonic-gate 					num = r2;
1085*0Sstevel@tonic-gate 				if (num)
1086*0Sstevel@tonic-gate 					{
1087*0Sstevel@tonic-gate 					char *dataptr;
1088*0Sstevel@tonic-gate 
1089*0Sstevel@tonic-gate 					if (INT_MAX < num)
1090*0Sstevel@tonic-gate 						num = INT_MAX;
1091*0Sstevel@tonic-gate 
1092*0Sstevel@tonic-gate 					if (num > 1)
1093*0Sstevel@tonic-gate 						--num; /* test restartability even more thoroughly */
1094*0Sstevel@tonic-gate 
1095*0Sstevel@tonic-gate 					r = BIO_nwrite0(io1, &dataptr);
1096*0Sstevel@tonic-gate 					assert(r > 0);
1097*0Sstevel@tonic-gate 					if (r < (int)num)
1098*0Sstevel@tonic-gate 						num = r;
1099*0Sstevel@tonic-gate 					r = BIO_read(io2, dataptr, (int)num);
1100*0Sstevel@tonic-gate 					if (r != (int)num) /* can't happen */
1101*0Sstevel@tonic-gate 						{
1102*0Sstevel@tonic-gate 						fprintf(stderr, "ERROR: BIO_read could not read "
1103*0Sstevel@tonic-gate 							"BIO_ctrl_pending() bytes");
1104*0Sstevel@tonic-gate 						goto err;
1105*0Sstevel@tonic-gate 						}
1106*0Sstevel@tonic-gate 					progress = 1;
1107*0Sstevel@tonic-gate 					r = BIO_nwrite(io1, &dataptr, (int)num);
1108*0Sstevel@tonic-gate 					if (r != (int)num) /* can't happen */
1109*0Sstevel@tonic-gate 						{
1110*0Sstevel@tonic-gate 						fprintf(stderr, "ERROR: BIO_nwrite() did not accept "
1111*0Sstevel@tonic-gate 							"BIO_nwrite0() bytes");
1112*0Sstevel@tonic-gate 						goto err;
1113*0Sstevel@tonic-gate 						}
1114*0Sstevel@tonic-gate 
1115*0Sstevel@tonic-gate 					if (debug)
1116*0Sstevel@tonic-gate 						printf((io2 == client_io) ?
1117*0Sstevel@tonic-gate 							"C->S relaying: %d bytes\n" :
1118*0Sstevel@tonic-gate 							"S->C relaying: %d bytes\n",
1119*0Sstevel@tonic-gate 							(int)num);
1120*0Sstevel@tonic-gate 					}
1121*0Sstevel@tonic-gate 			} /* no loop, BIO_ctrl_get_read_request now returns 0 anyway */
1122*0Sstevel@tonic-gate 
1123*0Sstevel@tonic-gate 			if (!progress && !prev_progress)
1124*0Sstevel@tonic-gate 				if (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0)
1125*0Sstevel@tonic-gate 					{
1126*0Sstevel@tonic-gate 					fprintf(stderr, "ERROR: got stuck\n");
1127*0Sstevel@tonic-gate 					if (strcmp("SSLv2", SSL_get_version(c_ssl)) == 0)
1128*0Sstevel@tonic-gate 						{
1129*0Sstevel@tonic-gate 						fprintf(stderr, "This can happen for SSL2 because "
1130*0Sstevel@tonic-gate 							"CLIENT-FINISHED and SERVER-VERIFY are written \n"
1131*0Sstevel@tonic-gate 							"concurrently ...");
1132*0Sstevel@tonic-gate 						if (strncmp("2SCF", SSL_state_string(c_ssl), 4) == 0
1133*0Sstevel@tonic-gate 							&& strncmp("2SSV", SSL_state_string(s_ssl), 4) == 0)
1134*0Sstevel@tonic-gate 							{
1135*0Sstevel@tonic-gate 							fprintf(stderr, " ok.\n");
1136*0Sstevel@tonic-gate 							goto end;
1137*0Sstevel@tonic-gate 							}
1138*0Sstevel@tonic-gate 						}
1139*0Sstevel@tonic-gate 					fprintf(stderr, " ERROR.\n");
1140*0Sstevel@tonic-gate 					goto err;
1141*0Sstevel@tonic-gate 					}
1142*0Sstevel@tonic-gate 			prev_progress = progress;
1143*0Sstevel@tonic-gate 			}
1144*0Sstevel@tonic-gate 		}
1145*0Sstevel@tonic-gate 	while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0);
1146*0Sstevel@tonic-gate 
1147*0Sstevel@tonic-gate 	if (verbose)
1148*0Sstevel@tonic-gate 		print_details(c_ssl, "DONE via BIO pair: ");
1149*0Sstevel@tonic-gate end:
1150*0Sstevel@tonic-gate 	ret = 0;
1151*0Sstevel@tonic-gate 
1152*0Sstevel@tonic-gate  err:
1153*0Sstevel@tonic-gate 	ERR_print_errors(bio_err);
1154*0Sstevel@tonic-gate 
1155*0Sstevel@tonic-gate 	if (server)
1156*0Sstevel@tonic-gate 		BIO_free(server);
1157*0Sstevel@tonic-gate 	if (server_io)
1158*0Sstevel@tonic-gate 		BIO_free(server_io);
1159*0Sstevel@tonic-gate 	if (client)
1160*0Sstevel@tonic-gate 		BIO_free(client);
1161*0Sstevel@tonic-gate 	if (client_io)
1162*0Sstevel@tonic-gate 		BIO_free(client_io);
1163*0Sstevel@tonic-gate 	if (s_ssl_bio)
1164*0Sstevel@tonic-gate 		BIO_free(s_ssl_bio);
1165*0Sstevel@tonic-gate 	if (c_ssl_bio)
1166*0Sstevel@tonic-gate 		BIO_free(c_ssl_bio);
1167*0Sstevel@tonic-gate 
1168*0Sstevel@tonic-gate 	return ret;
1169*0Sstevel@tonic-gate 	}
1170*0Sstevel@tonic-gate 
1171*0Sstevel@tonic-gate 
1172*0Sstevel@tonic-gate #define W_READ	1
1173*0Sstevel@tonic-gate #define W_WRITE	2
1174*0Sstevel@tonic-gate #define C_DONE	1
1175*0Sstevel@tonic-gate #define S_DONE	2
1176*0Sstevel@tonic-gate 
1177*0Sstevel@tonic-gate int doit(SSL *s_ssl, SSL *c_ssl, long count)
1178*0Sstevel@tonic-gate 	{
1179*0Sstevel@tonic-gate 	MS_STATIC char cbuf[1024*8],sbuf[1024*8];
1180*0Sstevel@tonic-gate 	long cw_num=count,cr_num=count;
1181*0Sstevel@tonic-gate 	long sw_num=count,sr_num=count;
1182*0Sstevel@tonic-gate 	int ret=1;
1183*0Sstevel@tonic-gate 	BIO *c_to_s=NULL;
1184*0Sstevel@tonic-gate 	BIO *s_to_c=NULL;
1185*0Sstevel@tonic-gate 	BIO *c_bio=NULL;
1186*0Sstevel@tonic-gate 	BIO *s_bio=NULL;
1187*0Sstevel@tonic-gate 	int c_r,c_w,s_r,s_w;
1188*0Sstevel@tonic-gate 	int c_want,s_want;
1189*0Sstevel@tonic-gate 	int i,j;
1190*0Sstevel@tonic-gate 	int done=0;
1191*0Sstevel@tonic-gate 	int c_write,s_write;
1192*0Sstevel@tonic-gate 	int do_server=0,do_client=0;
1193*0Sstevel@tonic-gate 
1194*0Sstevel@tonic-gate 	memset(cbuf,0,sizeof(cbuf));
1195*0Sstevel@tonic-gate 	memset(sbuf,0,sizeof(sbuf));
1196*0Sstevel@tonic-gate 
1197*0Sstevel@tonic-gate 	c_to_s=BIO_new(BIO_s_mem());
1198*0Sstevel@tonic-gate 	s_to_c=BIO_new(BIO_s_mem());
1199*0Sstevel@tonic-gate 	if ((s_to_c == NULL) || (c_to_s == NULL))
1200*0Sstevel@tonic-gate 		{
1201*0Sstevel@tonic-gate 		ERR_print_errors(bio_err);
1202*0Sstevel@tonic-gate 		goto err;
1203*0Sstevel@tonic-gate 		}
1204*0Sstevel@tonic-gate 
1205*0Sstevel@tonic-gate 	c_bio=BIO_new(BIO_f_ssl());
1206*0Sstevel@tonic-gate 	s_bio=BIO_new(BIO_f_ssl());
1207*0Sstevel@tonic-gate 	if ((c_bio == NULL) || (s_bio == NULL))
1208*0Sstevel@tonic-gate 		{
1209*0Sstevel@tonic-gate 		ERR_print_errors(bio_err);
1210*0Sstevel@tonic-gate 		goto err;
1211*0Sstevel@tonic-gate 		}
1212*0Sstevel@tonic-gate 
1213*0Sstevel@tonic-gate 	SSL_set_connect_state(c_ssl);
1214*0Sstevel@tonic-gate 	SSL_set_bio(c_ssl,s_to_c,c_to_s);
1215*0Sstevel@tonic-gate 	BIO_set_ssl(c_bio,c_ssl,BIO_NOCLOSE);
1216*0Sstevel@tonic-gate 
1217*0Sstevel@tonic-gate 	SSL_set_accept_state(s_ssl);
1218*0Sstevel@tonic-gate 	SSL_set_bio(s_ssl,c_to_s,s_to_c);
1219*0Sstevel@tonic-gate 	BIO_set_ssl(s_bio,s_ssl,BIO_NOCLOSE);
1220*0Sstevel@tonic-gate 
1221*0Sstevel@tonic-gate 	c_r=0; s_r=1;
1222*0Sstevel@tonic-gate 	c_w=1; s_w=0;
1223*0Sstevel@tonic-gate 	c_want=W_WRITE;
1224*0Sstevel@tonic-gate 	s_want=0;
1225*0Sstevel@tonic-gate 	c_write=1,s_write=0;
1226*0Sstevel@tonic-gate 
1227*0Sstevel@tonic-gate 	/* We can always do writes */
1228*0Sstevel@tonic-gate 	for (;;)
1229*0Sstevel@tonic-gate 		{
1230*0Sstevel@tonic-gate 		do_server=0;
1231*0Sstevel@tonic-gate 		do_client=0;
1232*0Sstevel@tonic-gate 
1233*0Sstevel@tonic-gate 		i=(int)BIO_pending(s_bio);
1234*0Sstevel@tonic-gate 		if ((i && s_r) || s_w) do_server=1;
1235*0Sstevel@tonic-gate 
1236*0Sstevel@tonic-gate 		i=(int)BIO_pending(c_bio);
1237*0Sstevel@tonic-gate 		if ((i && c_r) || c_w) do_client=1;
1238*0Sstevel@tonic-gate 
1239*0Sstevel@tonic-gate 		if (do_server && debug)
1240*0Sstevel@tonic-gate 			{
1241*0Sstevel@tonic-gate 			if (SSL_in_init(s_ssl))
1242*0Sstevel@tonic-gate 				printf("server waiting in SSL_accept - %s\n",
1243*0Sstevel@tonic-gate 					SSL_state_string_long(s_ssl));
1244*0Sstevel@tonic-gate /*			else if (s_write)
1245*0Sstevel@tonic-gate 				printf("server:SSL_write()\n");
1246*0Sstevel@tonic-gate 			else
1247*0Sstevel@tonic-gate 				printf("server:SSL_read()\n"); */
1248*0Sstevel@tonic-gate 			}
1249*0Sstevel@tonic-gate 
1250*0Sstevel@tonic-gate 		if (do_client && debug)
1251*0Sstevel@tonic-gate 			{
1252*0Sstevel@tonic-gate 			if (SSL_in_init(c_ssl))
1253*0Sstevel@tonic-gate 				printf("client waiting in SSL_connect - %s\n",
1254*0Sstevel@tonic-gate 					SSL_state_string_long(c_ssl));
1255*0Sstevel@tonic-gate /*			else if (c_write)
1256*0Sstevel@tonic-gate 				printf("client:SSL_write()\n");
1257*0Sstevel@tonic-gate 			else
1258*0Sstevel@tonic-gate 				printf("client:SSL_read()\n"); */
1259*0Sstevel@tonic-gate 			}
1260*0Sstevel@tonic-gate 
1261*0Sstevel@tonic-gate 		if (!do_client && !do_server)
1262*0Sstevel@tonic-gate 			{
1263*0Sstevel@tonic-gate 			fprintf(stdout,"ERROR IN STARTUP\n");
1264*0Sstevel@tonic-gate 			ERR_print_errors(bio_err);
1265*0Sstevel@tonic-gate 			break;
1266*0Sstevel@tonic-gate 			}
1267*0Sstevel@tonic-gate 		if (do_client && !(done & C_DONE))
1268*0Sstevel@tonic-gate 			{
1269*0Sstevel@tonic-gate 			if (c_write)
1270*0Sstevel@tonic-gate 				{
1271*0Sstevel@tonic-gate 				j=(cw_num > (long)sizeof(cbuf))
1272*0Sstevel@tonic-gate 					?sizeof(cbuf):(int)cw_num;
1273*0Sstevel@tonic-gate 				i=BIO_write(c_bio,cbuf,j);
1274*0Sstevel@tonic-gate 				if (i < 0)
1275*0Sstevel@tonic-gate 					{
1276*0Sstevel@tonic-gate 					c_r=0;
1277*0Sstevel@tonic-gate 					c_w=0;
1278*0Sstevel@tonic-gate 					if (BIO_should_retry(c_bio))
1279*0Sstevel@tonic-gate 						{
1280*0Sstevel@tonic-gate 						if (BIO_should_read(c_bio))
1281*0Sstevel@tonic-gate 							c_r=1;
1282*0Sstevel@tonic-gate 						if (BIO_should_write(c_bio))
1283*0Sstevel@tonic-gate 							c_w=1;
1284*0Sstevel@tonic-gate 						}
1285*0Sstevel@tonic-gate 					else
1286*0Sstevel@tonic-gate 						{
1287*0Sstevel@tonic-gate 						fprintf(stderr,"ERROR in CLIENT\n");
1288*0Sstevel@tonic-gate 						ERR_print_errors(bio_err);
1289*0Sstevel@tonic-gate 						goto err;
1290*0Sstevel@tonic-gate 						}
1291*0Sstevel@tonic-gate 					}
1292*0Sstevel@tonic-gate 				else if (i == 0)
1293*0Sstevel@tonic-gate 					{
1294*0Sstevel@tonic-gate 					fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
1295*0Sstevel@tonic-gate 					goto err;
1296*0Sstevel@tonic-gate 					}
1297*0Sstevel@tonic-gate 				else
1298*0Sstevel@tonic-gate 					{
1299*0Sstevel@tonic-gate 					if (debug)
1300*0Sstevel@tonic-gate 						printf("client wrote %d\n",i);
1301*0Sstevel@tonic-gate 					/* ok */
1302*0Sstevel@tonic-gate 					s_r=1;
1303*0Sstevel@tonic-gate 					c_write=0;
1304*0Sstevel@tonic-gate 					cw_num-=i;
1305*0Sstevel@tonic-gate 					}
1306*0Sstevel@tonic-gate 				}
1307*0Sstevel@tonic-gate 			else
1308*0Sstevel@tonic-gate 				{
1309*0Sstevel@tonic-gate 				i=BIO_read(c_bio,cbuf,sizeof(cbuf));
1310*0Sstevel@tonic-gate 				if (i < 0)
1311*0Sstevel@tonic-gate 					{
1312*0Sstevel@tonic-gate 					c_r=0;
1313*0Sstevel@tonic-gate 					c_w=0;
1314*0Sstevel@tonic-gate 					if (BIO_should_retry(c_bio))
1315*0Sstevel@tonic-gate 						{
1316*0Sstevel@tonic-gate 						if (BIO_should_read(c_bio))
1317*0Sstevel@tonic-gate 							c_r=1;
1318*0Sstevel@tonic-gate 						if (BIO_should_write(c_bio))
1319*0Sstevel@tonic-gate 							c_w=1;
1320*0Sstevel@tonic-gate 						}
1321*0Sstevel@tonic-gate 					else
1322*0Sstevel@tonic-gate 						{
1323*0Sstevel@tonic-gate 						fprintf(stderr,"ERROR in CLIENT\n");
1324*0Sstevel@tonic-gate 						ERR_print_errors(bio_err);
1325*0Sstevel@tonic-gate 						goto err;
1326*0Sstevel@tonic-gate 						}
1327*0Sstevel@tonic-gate 					}
1328*0Sstevel@tonic-gate 				else if (i == 0)
1329*0Sstevel@tonic-gate 					{
1330*0Sstevel@tonic-gate 					fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
1331*0Sstevel@tonic-gate 					goto err;
1332*0Sstevel@tonic-gate 					}
1333*0Sstevel@tonic-gate 				else
1334*0Sstevel@tonic-gate 					{
1335*0Sstevel@tonic-gate 					if (debug)
1336*0Sstevel@tonic-gate 						printf("client read %d\n",i);
1337*0Sstevel@tonic-gate 					cr_num-=i;
1338*0Sstevel@tonic-gate 					if (sw_num > 0)
1339*0Sstevel@tonic-gate 						{
1340*0Sstevel@tonic-gate 						s_write=1;
1341*0Sstevel@tonic-gate 						s_w=1;
1342*0Sstevel@tonic-gate 						}
1343*0Sstevel@tonic-gate 					if (cr_num <= 0)
1344*0Sstevel@tonic-gate 						{
1345*0Sstevel@tonic-gate 						s_write=1;
1346*0Sstevel@tonic-gate 						s_w=1;
1347*0Sstevel@tonic-gate 						done=S_DONE|C_DONE;
1348*0Sstevel@tonic-gate 						}
1349*0Sstevel@tonic-gate 					}
1350*0Sstevel@tonic-gate 				}
1351*0Sstevel@tonic-gate 			}
1352*0Sstevel@tonic-gate 
1353*0Sstevel@tonic-gate 		if (do_server && !(done & S_DONE))
1354*0Sstevel@tonic-gate 			{
1355*0Sstevel@tonic-gate 			if (!s_write)
1356*0Sstevel@tonic-gate 				{
1357*0Sstevel@tonic-gate 				i=BIO_read(s_bio,sbuf,sizeof(cbuf));
1358*0Sstevel@tonic-gate 				if (i < 0)
1359*0Sstevel@tonic-gate 					{
1360*0Sstevel@tonic-gate 					s_r=0;
1361*0Sstevel@tonic-gate 					s_w=0;
1362*0Sstevel@tonic-gate 					if (BIO_should_retry(s_bio))
1363*0Sstevel@tonic-gate 						{
1364*0Sstevel@tonic-gate 						if (BIO_should_read(s_bio))
1365*0Sstevel@tonic-gate 							s_r=1;
1366*0Sstevel@tonic-gate 						if (BIO_should_write(s_bio))
1367*0Sstevel@tonic-gate 							s_w=1;
1368*0Sstevel@tonic-gate 						}
1369*0Sstevel@tonic-gate 					else
1370*0Sstevel@tonic-gate 						{
1371*0Sstevel@tonic-gate 						fprintf(stderr,"ERROR in SERVER\n");
1372*0Sstevel@tonic-gate 						ERR_print_errors(bio_err);
1373*0Sstevel@tonic-gate 						goto err;
1374*0Sstevel@tonic-gate 						}
1375*0Sstevel@tonic-gate 					}
1376*0Sstevel@tonic-gate 				else if (i == 0)
1377*0Sstevel@tonic-gate 					{
1378*0Sstevel@tonic-gate 					ERR_print_errors(bio_err);
1379*0Sstevel@tonic-gate 					fprintf(stderr,"SSL SERVER STARTUP FAILED in SSL_read\n");
1380*0Sstevel@tonic-gate 					goto err;
1381*0Sstevel@tonic-gate 					}
1382*0Sstevel@tonic-gate 				else
1383*0Sstevel@tonic-gate 					{
1384*0Sstevel@tonic-gate 					if (debug)
1385*0Sstevel@tonic-gate 						printf("server read %d\n",i);
1386*0Sstevel@tonic-gate 					sr_num-=i;
1387*0Sstevel@tonic-gate 					if (cw_num > 0)
1388*0Sstevel@tonic-gate 						{
1389*0Sstevel@tonic-gate 						c_write=1;
1390*0Sstevel@tonic-gate 						c_w=1;
1391*0Sstevel@tonic-gate 						}
1392*0Sstevel@tonic-gate 					if (sr_num <= 0)
1393*0Sstevel@tonic-gate 						{
1394*0Sstevel@tonic-gate 						s_write=1;
1395*0Sstevel@tonic-gate 						s_w=1;
1396*0Sstevel@tonic-gate 						c_write=0;
1397*0Sstevel@tonic-gate 						}
1398*0Sstevel@tonic-gate 					}
1399*0Sstevel@tonic-gate 				}
1400*0Sstevel@tonic-gate 			else
1401*0Sstevel@tonic-gate 				{
1402*0Sstevel@tonic-gate 				j=(sw_num > (long)sizeof(sbuf))?
1403*0Sstevel@tonic-gate 					sizeof(sbuf):(int)sw_num;
1404*0Sstevel@tonic-gate 				i=BIO_write(s_bio,sbuf,j);
1405*0Sstevel@tonic-gate 				if (i < 0)
1406*0Sstevel@tonic-gate 					{
1407*0Sstevel@tonic-gate 					s_r=0;
1408*0Sstevel@tonic-gate 					s_w=0;
1409*0Sstevel@tonic-gate 					if (BIO_should_retry(s_bio))
1410*0Sstevel@tonic-gate 						{
1411*0Sstevel@tonic-gate 						if (BIO_should_read(s_bio))
1412*0Sstevel@tonic-gate 							s_r=1;
1413*0Sstevel@tonic-gate 						if (BIO_should_write(s_bio))
1414*0Sstevel@tonic-gate 							s_w=1;
1415*0Sstevel@tonic-gate 						}
1416*0Sstevel@tonic-gate 					else
1417*0Sstevel@tonic-gate 						{
1418*0Sstevel@tonic-gate 						fprintf(stderr,"ERROR in SERVER\n");
1419*0Sstevel@tonic-gate 						ERR_print_errors(bio_err);
1420*0Sstevel@tonic-gate 						goto err;
1421*0Sstevel@tonic-gate 						}
1422*0Sstevel@tonic-gate 					}
1423*0Sstevel@tonic-gate 				else if (i == 0)
1424*0Sstevel@tonic-gate 					{
1425*0Sstevel@tonic-gate 					ERR_print_errors(bio_err);
1426*0Sstevel@tonic-gate 					fprintf(stderr,"SSL SERVER STARTUP FAILED in SSL_write\n");
1427*0Sstevel@tonic-gate 					goto err;
1428*0Sstevel@tonic-gate 					}
1429*0Sstevel@tonic-gate 				else
1430*0Sstevel@tonic-gate 					{
1431*0Sstevel@tonic-gate 					if (debug)
1432*0Sstevel@tonic-gate 						printf("server wrote %d\n",i);
1433*0Sstevel@tonic-gate 					sw_num-=i;
1434*0Sstevel@tonic-gate 					s_write=0;
1435*0Sstevel@tonic-gate 					c_r=1;
1436*0Sstevel@tonic-gate 					if (sw_num <= 0)
1437*0Sstevel@tonic-gate 						done|=S_DONE;
1438*0Sstevel@tonic-gate 					}
1439*0Sstevel@tonic-gate 				}
1440*0Sstevel@tonic-gate 			}
1441*0Sstevel@tonic-gate 
1442*0Sstevel@tonic-gate 		if ((done & S_DONE) && (done & C_DONE)) break;
1443*0Sstevel@tonic-gate 		}
1444*0Sstevel@tonic-gate 
1445*0Sstevel@tonic-gate 	if (verbose)
1446*0Sstevel@tonic-gate 		print_details(c_ssl, "DONE: ");
1447*0Sstevel@tonic-gate 	ret=0;
1448*0Sstevel@tonic-gate err:
1449*0Sstevel@tonic-gate 	/* We have to set the BIO's to NULL otherwise they will be
1450*0Sstevel@tonic-gate 	 * OPENSSL_free()ed twice.  Once when th s_ssl is SSL_free()ed and
1451*0Sstevel@tonic-gate 	 * again when c_ssl is SSL_free()ed.
1452*0Sstevel@tonic-gate 	 * This is a hack required because s_ssl and c_ssl are sharing the same
1453*0Sstevel@tonic-gate 	 * BIO structure and SSL_set_bio() and SSL_free() automatically
1454*0Sstevel@tonic-gate 	 * BIO_free non NULL entries.
1455*0Sstevel@tonic-gate 	 * You should not normally do this or be required to do this */
1456*0Sstevel@tonic-gate 	if (s_ssl != NULL)
1457*0Sstevel@tonic-gate 		{
1458*0Sstevel@tonic-gate 		s_ssl->rbio=NULL;
1459*0Sstevel@tonic-gate 		s_ssl->wbio=NULL;
1460*0Sstevel@tonic-gate 		}
1461*0Sstevel@tonic-gate 	if (c_ssl != NULL)
1462*0Sstevel@tonic-gate 		{
1463*0Sstevel@tonic-gate 		c_ssl->rbio=NULL;
1464*0Sstevel@tonic-gate 		c_ssl->wbio=NULL;
1465*0Sstevel@tonic-gate 		}
1466*0Sstevel@tonic-gate 
1467*0Sstevel@tonic-gate 	if (c_to_s != NULL) BIO_free(c_to_s);
1468*0Sstevel@tonic-gate 	if (s_to_c != NULL) BIO_free(s_to_c);
1469*0Sstevel@tonic-gate 	if (c_bio != NULL) BIO_free_all(c_bio);
1470*0Sstevel@tonic-gate 	if (s_bio != NULL) BIO_free_all(s_bio);
1471*0Sstevel@tonic-gate 	return(ret);
1472*0Sstevel@tonic-gate 	}
1473*0Sstevel@tonic-gate 
1474*0Sstevel@tonic-gate static int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx)
1475*0Sstevel@tonic-gate 	{
1476*0Sstevel@tonic-gate 	char *s,buf[256];
1477*0Sstevel@tonic-gate 
1478*0Sstevel@tonic-gate 	s=X509_NAME_oneline(X509_get_subject_name(ctx->current_cert),buf,
1479*0Sstevel@tonic-gate 			    sizeof buf);
1480*0Sstevel@tonic-gate 	if (s != NULL)
1481*0Sstevel@tonic-gate 		{
1482*0Sstevel@tonic-gate 		if (ok)
1483*0Sstevel@tonic-gate 			fprintf(stderr,"depth=%d %s\n",ctx->error_depth,buf);
1484*0Sstevel@tonic-gate 		else
1485*0Sstevel@tonic-gate 			fprintf(stderr,"depth=%d error=%d %s\n",
1486*0Sstevel@tonic-gate 				ctx->error_depth,ctx->error,buf);
1487*0Sstevel@tonic-gate 		}
1488*0Sstevel@tonic-gate 
1489*0Sstevel@tonic-gate 	if (ok == 0)
1490*0Sstevel@tonic-gate 		{
1491*0Sstevel@tonic-gate 		switch (ctx->error)
1492*0Sstevel@tonic-gate 			{
1493*0Sstevel@tonic-gate 		case X509_V_ERR_CERT_NOT_YET_VALID:
1494*0Sstevel@tonic-gate 		case X509_V_ERR_CERT_HAS_EXPIRED:
1495*0Sstevel@tonic-gate 		case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
1496*0Sstevel@tonic-gate 			ok=1;
1497*0Sstevel@tonic-gate 			}
1498*0Sstevel@tonic-gate 		}
1499*0Sstevel@tonic-gate 
1500*0Sstevel@tonic-gate 	return(ok);
1501*0Sstevel@tonic-gate 	}
1502*0Sstevel@tonic-gate 
1503*0Sstevel@tonic-gate static int MS_CALLBACK app_verify_callback(X509_STORE_CTX *ctx, void *arg)
1504*0Sstevel@tonic-gate 	{
1505*0Sstevel@tonic-gate 	char *s = NULL,buf[256];
1506*0Sstevel@tonic-gate 	int ok=1;
1507*0Sstevel@tonic-gate 
1508*0Sstevel@tonic-gate 	fprintf(stderr, "In app_verify_callback, allowing cert. ");
1509*0Sstevel@tonic-gate 	fprintf(stderr, "Arg is: %s\n", (char *)arg);
1510*0Sstevel@tonic-gate 	fprintf(stderr, "Finished printing do we have a context? 0x%x a cert? 0x%x\n",
1511*0Sstevel@tonic-gate 			(unsigned int)ctx, (unsigned int)ctx->cert);
1512*0Sstevel@tonic-gate 	if (ctx->cert)
1513*0Sstevel@tonic-gate 		s=X509_NAME_oneline(X509_get_subject_name(ctx->cert),buf,256);
1514*0Sstevel@tonic-gate 	if (s != NULL)
1515*0Sstevel@tonic-gate 		{
1516*0Sstevel@tonic-gate 			fprintf(stderr,"cert depth=%d %s\n",ctx->error_depth,buf);
1517*0Sstevel@tonic-gate 		}
1518*0Sstevel@tonic-gate 
1519*0Sstevel@tonic-gate 	return(ok);
1520*0Sstevel@tonic-gate 	}
1521*0Sstevel@tonic-gate 
1522*0Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
1523*0Sstevel@tonic-gate static RSA *rsa_tmp=NULL;
1524*0Sstevel@tonic-gate 
1525*0Sstevel@tonic-gate static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength)
1526*0Sstevel@tonic-gate 	{
1527*0Sstevel@tonic-gate 	if (rsa_tmp == NULL)
1528*0Sstevel@tonic-gate 		{
1529*0Sstevel@tonic-gate 		BIO_printf(bio_err,"Generating temp (%d bit) RSA key...",keylength);
1530*0Sstevel@tonic-gate 		(void)BIO_flush(bio_err);
1531*0Sstevel@tonic-gate 		rsa_tmp=RSA_generate_key(keylength,RSA_F4,NULL,NULL);
1532*0Sstevel@tonic-gate 		BIO_printf(bio_err,"\n");
1533*0Sstevel@tonic-gate 		(void)BIO_flush(bio_err);
1534*0Sstevel@tonic-gate 		}
1535*0Sstevel@tonic-gate 	return(rsa_tmp);
1536*0Sstevel@tonic-gate 	}
1537*0Sstevel@tonic-gate 
1538*0Sstevel@tonic-gate static void free_tmp_rsa(void)
1539*0Sstevel@tonic-gate 	{
1540*0Sstevel@tonic-gate 	if (rsa_tmp != NULL)
1541*0Sstevel@tonic-gate 		{
1542*0Sstevel@tonic-gate 		RSA_free(rsa_tmp);
1543*0Sstevel@tonic-gate 		rsa_tmp = NULL;
1544*0Sstevel@tonic-gate 		}
1545*0Sstevel@tonic-gate 	}
1546*0Sstevel@tonic-gate #endif
1547*0Sstevel@tonic-gate 
1548*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
1549*0Sstevel@tonic-gate /* These DH parameters have been generated as follows:
1550*0Sstevel@tonic-gate  *    $ openssl dhparam -C -noout 512
1551*0Sstevel@tonic-gate  *    $ openssl dhparam -C -noout 1024
1552*0Sstevel@tonic-gate  *    $ openssl dhparam -C -noout -dsaparam 1024
1553*0Sstevel@tonic-gate  * (The third function has been renamed to avoid name conflicts.)
1554*0Sstevel@tonic-gate  */
1555*0Sstevel@tonic-gate static DH *get_dh512()
1556*0Sstevel@tonic-gate 	{
1557*0Sstevel@tonic-gate 	static unsigned char dh512_p[]={
1558*0Sstevel@tonic-gate 		0xCB,0xC8,0xE1,0x86,0xD0,0x1F,0x94,0x17,0xA6,0x99,0xF0,0xC6,
1559*0Sstevel@tonic-gate 		0x1F,0x0D,0xAC,0xB6,0x25,0x3E,0x06,0x39,0xCA,0x72,0x04,0xB0,
1560*0Sstevel@tonic-gate 		0x6E,0xDA,0xC0,0x61,0xE6,0x7A,0x77,0x25,0xE8,0x3B,0xB9,0x5F,
1561*0Sstevel@tonic-gate 		0x9A,0xB6,0xB5,0xFE,0x99,0x0B,0xA1,0x93,0x4E,0x35,0x33,0xB8,
1562*0Sstevel@tonic-gate 		0xE1,0xF1,0x13,0x4F,0x59,0x1A,0xD2,0x57,0xC0,0x26,0x21,0x33,
1563*0Sstevel@tonic-gate 		0x02,0xC5,0xAE,0x23,
1564*0Sstevel@tonic-gate 		};
1565*0Sstevel@tonic-gate 	static unsigned char dh512_g[]={
1566*0Sstevel@tonic-gate 		0x02,
1567*0Sstevel@tonic-gate 		};
1568*0Sstevel@tonic-gate 	DH *dh;
1569*0Sstevel@tonic-gate 
1570*0Sstevel@tonic-gate 	if ((dh=DH_new()) == NULL) return(NULL);
1571*0Sstevel@tonic-gate 	dh->p=BN_bin2bn(dh512_p,sizeof(dh512_p),NULL);
1572*0Sstevel@tonic-gate 	dh->g=BN_bin2bn(dh512_g,sizeof(dh512_g),NULL);
1573*0Sstevel@tonic-gate 	if ((dh->p == NULL) || (dh->g == NULL))
1574*0Sstevel@tonic-gate 		{ DH_free(dh); return(NULL); }
1575*0Sstevel@tonic-gate 	return(dh);
1576*0Sstevel@tonic-gate 	}
1577*0Sstevel@tonic-gate 
1578*0Sstevel@tonic-gate static DH *get_dh1024()
1579*0Sstevel@tonic-gate 	{
1580*0Sstevel@tonic-gate 	static unsigned char dh1024_p[]={
1581*0Sstevel@tonic-gate 		0xF8,0x81,0x89,0x7D,0x14,0x24,0xC5,0xD1,0xE6,0xF7,0xBF,0x3A,
1582*0Sstevel@tonic-gate 		0xE4,0x90,0xF4,0xFC,0x73,0xFB,0x34,0xB5,0xFA,0x4C,0x56,0xA2,
1583*0Sstevel@tonic-gate 		0xEA,0xA7,0xE9,0xC0,0xC0,0xCE,0x89,0xE1,0xFA,0x63,0x3F,0xB0,
1584*0Sstevel@tonic-gate 		0x6B,0x32,0x66,0xF1,0xD1,0x7B,0xB0,0x00,0x8F,0xCA,0x87,0xC2,
1585*0Sstevel@tonic-gate 		0xAE,0x98,0x89,0x26,0x17,0xC2,0x05,0xD2,0xEC,0x08,0xD0,0x8C,
1586*0Sstevel@tonic-gate 		0xFF,0x17,0x52,0x8C,0xC5,0x07,0x93,0x03,0xB1,0xF6,0x2F,0xB8,
1587*0Sstevel@tonic-gate 		0x1C,0x52,0x47,0x27,0x1B,0xDB,0xD1,0x8D,0x9D,0x69,0x1D,0x52,
1588*0Sstevel@tonic-gate 		0x4B,0x32,0x81,0xAA,0x7F,0x00,0xC8,0xDC,0xE6,0xD9,0xCC,0xC1,
1589*0Sstevel@tonic-gate 		0x11,0x2D,0x37,0x34,0x6C,0xEA,0x02,0x97,0x4B,0x0E,0xBB,0xB1,
1590*0Sstevel@tonic-gate 		0x71,0x33,0x09,0x15,0xFD,0xDD,0x23,0x87,0x07,0x5E,0x89,0xAB,
1591*0Sstevel@tonic-gate 		0x6B,0x7C,0x5F,0xEC,0xA6,0x24,0xDC,0x53,
1592*0Sstevel@tonic-gate 		};
1593*0Sstevel@tonic-gate 	static unsigned char dh1024_g[]={
1594*0Sstevel@tonic-gate 		0x02,
1595*0Sstevel@tonic-gate 		};
1596*0Sstevel@tonic-gate 	DH *dh;
1597*0Sstevel@tonic-gate 
1598*0Sstevel@tonic-gate 	if ((dh=DH_new()) == NULL) return(NULL);
1599*0Sstevel@tonic-gate 	dh->p=BN_bin2bn(dh1024_p,sizeof(dh1024_p),NULL);
1600*0Sstevel@tonic-gate 	dh->g=BN_bin2bn(dh1024_g,sizeof(dh1024_g),NULL);
1601*0Sstevel@tonic-gate 	if ((dh->p == NULL) || (dh->g == NULL))
1602*0Sstevel@tonic-gate 		{ DH_free(dh); return(NULL); }
1603*0Sstevel@tonic-gate 	return(dh);
1604*0Sstevel@tonic-gate 	}
1605*0Sstevel@tonic-gate 
1606*0Sstevel@tonic-gate static DH *get_dh1024dsa()
1607*0Sstevel@tonic-gate 	{
1608*0Sstevel@tonic-gate 	static unsigned char dh1024_p[]={
1609*0Sstevel@tonic-gate 		0xC8,0x00,0xF7,0x08,0x07,0x89,0x4D,0x90,0x53,0xF3,0xD5,0x00,
1610*0Sstevel@tonic-gate 		0x21,0x1B,0xF7,0x31,0xA6,0xA2,0xDA,0x23,0x9A,0xC7,0x87,0x19,
1611*0Sstevel@tonic-gate 		0x3B,0x47,0xB6,0x8C,0x04,0x6F,0xFF,0xC6,0x9B,0xB8,0x65,0xD2,
1612*0Sstevel@tonic-gate 		0xC2,0x5F,0x31,0x83,0x4A,0xA7,0x5F,0x2F,0x88,0x38,0xB6,0x55,
1613*0Sstevel@tonic-gate 		0xCF,0xD9,0x87,0x6D,0x6F,0x9F,0xDA,0xAC,0xA6,0x48,0xAF,0xFC,
1614*0Sstevel@tonic-gate 		0x33,0x84,0x37,0x5B,0x82,0x4A,0x31,0x5D,0xE7,0xBD,0x52,0x97,
1615*0Sstevel@tonic-gate 		0xA1,0x77,0xBF,0x10,0x9E,0x37,0xEA,0x64,0xFA,0xCA,0x28,0x8D,
1616*0Sstevel@tonic-gate 		0x9D,0x3B,0xD2,0x6E,0x09,0x5C,0x68,0xC7,0x45,0x90,0xFD,0xBB,
1617*0Sstevel@tonic-gate 		0x70,0xC9,0x3A,0xBB,0xDF,0xD4,0x21,0x0F,0xC4,0x6A,0x3C,0xF6,
1618*0Sstevel@tonic-gate 		0x61,0xCF,0x3F,0xD6,0x13,0xF1,0x5F,0xBC,0xCF,0xBC,0x26,0x9E,
1619*0Sstevel@tonic-gate 		0xBC,0x0B,0xBD,0xAB,0x5D,0xC9,0x54,0x39,
1620*0Sstevel@tonic-gate 		};
1621*0Sstevel@tonic-gate 	static unsigned char dh1024_g[]={
1622*0Sstevel@tonic-gate 		0x3B,0x40,0x86,0xE7,0xF3,0x6C,0xDE,0x67,0x1C,0xCC,0x80,0x05,
1623*0Sstevel@tonic-gate 		0x5A,0xDF,0xFE,0xBD,0x20,0x27,0x74,0x6C,0x24,0xC9,0x03,0xF3,
1624*0Sstevel@tonic-gate 		0xE1,0x8D,0xC3,0x7D,0x98,0x27,0x40,0x08,0xB8,0x8C,0x6A,0xE9,
1625*0Sstevel@tonic-gate 		0xBB,0x1A,0x3A,0xD6,0x86,0x83,0x5E,0x72,0x41,0xCE,0x85,0x3C,
1626*0Sstevel@tonic-gate 		0xD2,0xB3,0xFC,0x13,0xCE,0x37,0x81,0x9E,0x4C,0x1C,0x7B,0x65,
1627*0Sstevel@tonic-gate 		0xD3,0xE6,0xA6,0x00,0xF5,0x5A,0x95,0x43,0x5E,0x81,0xCF,0x60,
1628*0Sstevel@tonic-gate 		0xA2,0x23,0xFC,0x36,0xA7,0x5D,0x7A,0x4C,0x06,0x91,0x6E,0xF6,
1629*0Sstevel@tonic-gate 		0x57,0xEE,0x36,0xCB,0x06,0xEA,0xF5,0x3D,0x95,0x49,0xCB,0xA7,
1630*0Sstevel@tonic-gate 		0xDD,0x81,0xDF,0x80,0x09,0x4A,0x97,0x4D,0xA8,0x22,0x72,0xA1,
1631*0Sstevel@tonic-gate 		0x7F,0xC4,0x70,0x56,0x70,0xE8,0x20,0x10,0x18,0x8F,0x2E,0x60,
1632*0Sstevel@tonic-gate 		0x07,0xE7,0x68,0x1A,0x82,0x5D,0x32,0xA2,
1633*0Sstevel@tonic-gate 		};
1634*0Sstevel@tonic-gate 	DH *dh;
1635*0Sstevel@tonic-gate 
1636*0Sstevel@tonic-gate 	if ((dh=DH_new()) == NULL) return(NULL);
1637*0Sstevel@tonic-gate 	dh->p=BN_bin2bn(dh1024_p,sizeof(dh1024_p),NULL);
1638*0Sstevel@tonic-gate 	dh->g=BN_bin2bn(dh1024_g,sizeof(dh1024_g),NULL);
1639*0Sstevel@tonic-gate 	if ((dh->p == NULL) || (dh->g == NULL))
1640*0Sstevel@tonic-gate 		{ DH_free(dh); return(NULL); }
1641*0Sstevel@tonic-gate 	dh->length = 160;
1642*0Sstevel@tonic-gate 	return(dh);
1643*0Sstevel@tonic-gate 	}
1644*0Sstevel@tonic-gate #endif
1645