xref: /onnv-gate/usr/src/common/openssl/apps/s_client.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* apps/s_client.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 <openssl/e_os2.h>
117*0Sstevel@tonic-gate #ifdef OPENSSL_NO_STDIO
118*0Sstevel@tonic-gate #define APPS_WIN16
119*0Sstevel@tonic-gate #endif
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate /* With IPv6, it looks like Digital has mixed up the proper order of
122*0Sstevel@tonic-gate    recursive header file inclusion, resulting in the compiler complaining
123*0Sstevel@tonic-gate    that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which
124*0Sstevel@tonic-gate    is needed to have fileno() declared correctly...  So let's define u_int */
125*0Sstevel@tonic-gate #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
126*0Sstevel@tonic-gate #define __U_INT
127*0Sstevel@tonic-gate typedef unsigned int u_int;
128*0Sstevel@tonic-gate #endif
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate #define USE_SOCKETS
131*0Sstevel@tonic-gate #include "apps.h"
132*0Sstevel@tonic-gate #include <openssl/x509.h>
133*0Sstevel@tonic-gate #include <openssl/ssl.h>
134*0Sstevel@tonic-gate #include <openssl/err.h>
135*0Sstevel@tonic-gate #include <openssl/pem.h>
136*0Sstevel@tonic-gate #include <openssl/rand.h>
137*0Sstevel@tonic-gate #include "s_apps.h"
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_WINCE
140*0Sstevel@tonic-gate /* Windows CE incorrectly defines fileno as returning void*, so to avoid problems below... */
141*0Sstevel@tonic-gate #ifdef fileno
142*0Sstevel@tonic-gate #undef fileno
143*0Sstevel@tonic-gate #endif
144*0Sstevel@tonic-gate #define fileno(a) (int)_fileno(a)
145*0Sstevel@tonic-gate #endif
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate #if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
149*0Sstevel@tonic-gate /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
150*0Sstevel@tonic-gate #undef FIONBIO
151*0Sstevel@tonic-gate #endif
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate #undef PROG
154*0Sstevel@tonic-gate #define PROG	s_client_main
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate /*#define SSL_HOST_NAME	"www.netscape.com" */
157*0Sstevel@tonic-gate /*#define SSL_HOST_NAME	"193.118.187.102" */
158*0Sstevel@tonic-gate #define SSL_HOST_NAME	"localhost"
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate /*#define TEST_CERT "client.pem" */ /* no default cert. */
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate #undef BUFSIZZ
163*0Sstevel@tonic-gate #define BUFSIZZ 1024*8
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate extern int verify_depth;
166*0Sstevel@tonic-gate extern int verify_error;
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate #ifdef FIONBIO
169*0Sstevel@tonic-gate static int c_nbio=0;
170*0Sstevel@tonic-gate #endif
171*0Sstevel@tonic-gate static int c_Pause=0;
172*0Sstevel@tonic-gate static int c_debug=0;
173*0Sstevel@tonic-gate static int c_msg=0;
174*0Sstevel@tonic-gate static int c_showcerts=0;
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate static void sc_usage(void);
177*0Sstevel@tonic-gate static void print_stuff(BIO *berr,SSL *con,int full);
178*0Sstevel@tonic-gate static BIO *bio_c_out=NULL;
179*0Sstevel@tonic-gate static int c_quiet=0;
180*0Sstevel@tonic-gate static int c_ign_eof=0;
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate static void sc_usage(void)
183*0Sstevel@tonic-gate 	{
184*0Sstevel@tonic-gate 	BIO_printf(bio_err,"usage: s_client args\n");
185*0Sstevel@tonic-gate 	BIO_printf(bio_err,"\n");
186*0Sstevel@tonic-gate 	BIO_printf(bio_err," -host host     - use -connect instead\n");
187*0Sstevel@tonic-gate 	BIO_printf(bio_err," -port port     - use -connect instead\n");
188*0Sstevel@tonic-gate 	BIO_printf(bio_err," -connect host:port - who to connect to (default is %s:%s)\n",SSL_HOST_NAME,PORT_STR);
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 	BIO_printf(bio_err," -verify arg   - turn on peer certificate verification\n");
191*0Sstevel@tonic-gate 	BIO_printf(bio_err," -cert arg     - certificate file to use, PEM format assumed\n");
192*0Sstevel@tonic-gate 	BIO_printf(bio_err," -key arg      - Private key file to use, PEM format assumed, in cert file if\n");
193*0Sstevel@tonic-gate 	BIO_printf(bio_err,"                 not specified but cert file is.\n");
194*0Sstevel@tonic-gate 	BIO_printf(bio_err," -CApath arg   - PEM format directory of CA's\n");
195*0Sstevel@tonic-gate 	BIO_printf(bio_err," -CAfile arg   - PEM format file of CA's\n");
196*0Sstevel@tonic-gate 	BIO_printf(bio_err," -reconnect    - Drop and re-make the connection with the same Session-ID\n");
197*0Sstevel@tonic-gate 	BIO_printf(bio_err," -pause        - sleep(1) after each read(2) and write(2) system call\n");
198*0Sstevel@tonic-gate 	BIO_printf(bio_err," -showcerts    - show all certificates in the chain\n");
199*0Sstevel@tonic-gate 	BIO_printf(bio_err," -debug        - extra output\n");
200*0Sstevel@tonic-gate 	BIO_printf(bio_err," -msg          - Show protocol messages\n");
201*0Sstevel@tonic-gate 	BIO_printf(bio_err," -nbio_test    - more ssl protocol testing\n");
202*0Sstevel@tonic-gate 	BIO_printf(bio_err," -state        - print the 'ssl' states\n");
203*0Sstevel@tonic-gate #ifdef FIONBIO
204*0Sstevel@tonic-gate 	BIO_printf(bio_err," -nbio         - Run with non-blocking IO\n");
205*0Sstevel@tonic-gate #endif
206*0Sstevel@tonic-gate 	BIO_printf(bio_err," -crlf         - convert LF from terminal into CRLF\n");
207*0Sstevel@tonic-gate 	BIO_printf(bio_err," -quiet        - no s_client output\n");
208*0Sstevel@tonic-gate 	BIO_printf(bio_err," -ign_eof      - ignore input eof (default when -quiet)\n");
209*0Sstevel@tonic-gate 	BIO_printf(bio_err," -ssl2         - just use SSLv2\n");
210*0Sstevel@tonic-gate 	BIO_printf(bio_err," -ssl3         - just use SSLv3\n");
211*0Sstevel@tonic-gate 	BIO_printf(bio_err," -tls1         - just use TLSv1\n");
212*0Sstevel@tonic-gate 	BIO_printf(bio_err," -no_tls1/-no_ssl3/-no_ssl2 - turn off that protocol\n");
213*0Sstevel@tonic-gate 	BIO_printf(bio_err," -bugs         - Switch on all SSL implementation bug workarounds\n");
214*0Sstevel@tonic-gate 	BIO_printf(bio_err," -serverpref   - Use server's cipher preferences (only SSLv2)\n");
215*0Sstevel@tonic-gate 	BIO_printf(bio_err," -cipher       - preferred cipher to use, use the 'openssl ciphers'\n");
216*0Sstevel@tonic-gate 	BIO_printf(bio_err,"                 command to see what is available\n");
217*0Sstevel@tonic-gate 	BIO_printf(bio_err," -starttls prot - use the STARTTLS command before starting TLS\n");
218*0Sstevel@tonic-gate 	BIO_printf(bio_err,"                 for those protocols that support it, where\n");
219*0Sstevel@tonic-gate 	BIO_printf(bio_err,"                 'prot' defines which one to assume.  Currently,\n");
220*0Sstevel@tonic-gate 	BIO_printf(bio_err,"                 only \"smtp\" and \"pop3\" are supported.\n");
221*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
222*0Sstevel@tonic-gate 	BIO_printf(bio_err," -engine id    - Initialise and use the specified engine\n");
223*0Sstevel@tonic-gate #endif
224*0Sstevel@tonic-gate 	BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 	}
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate int MAIN(int, char **);
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate int MAIN(int argc, char **argv)
231*0Sstevel@tonic-gate 	{
232*0Sstevel@tonic-gate 	int off=0;
233*0Sstevel@tonic-gate 	SSL *con=NULL,*con2=NULL;
234*0Sstevel@tonic-gate 	X509_STORE *store = NULL;
235*0Sstevel@tonic-gate 	int s,k,width,state=0;
236*0Sstevel@tonic-gate 	char *cbuf=NULL,*sbuf=NULL,*mbuf=NULL;
237*0Sstevel@tonic-gate 	int cbuf_len,cbuf_off;
238*0Sstevel@tonic-gate 	int sbuf_len,sbuf_off;
239*0Sstevel@tonic-gate 	fd_set readfds,writefds;
240*0Sstevel@tonic-gate 	short port=PORT;
241*0Sstevel@tonic-gate 	int full_log=1;
242*0Sstevel@tonic-gate 	char *host=SSL_HOST_NAME;
243*0Sstevel@tonic-gate 	char *cert_file=NULL,*key_file=NULL;
244*0Sstevel@tonic-gate 	char *CApath=NULL,*CAfile=NULL,*cipher=NULL;
245*0Sstevel@tonic-gate 	int reconnect=0,badop=0,verify=SSL_VERIFY_NONE,bugs=0;
246*0Sstevel@tonic-gate 	int crlf=0;
247*0Sstevel@tonic-gate 	int write_tty,read_tty,write_ssl,read_ssl,tty_on,ssl_pending;
248*0Sstevel@tonic-gate 	SSL_CTX *ctx=NULL;
249*0Sstevel@tonic-gate 	int ret=1,in_init=1,i,nbio_test=0;
250*0Sstevel@tonic-gate 	int starttls_proto = 0;
251*0Sstevel@tonic-gate 	int prexit = 0, vflags = 0;
252*0Sstevel@tonic-gate 	SSL_METHOD *meth=NULL;
253*0Sstevel@tonic-gate 	BIO *sbio;
254*0Sstevel@tonic-gate 	char *inrand=NULL;
255*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
256*0Sstevel@tonic-gate 	char *engine_id=NULL;
257*0Sstevel@tonic-gate 	ENGINE *e=NULL;
258*0Sstevel@tonic-gate #endif
259*0Sstevel@tonic-gate #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
260*0Sstevel@tonic-gate 	struct timeval tv;
261*0Sstevel@tonic-gate #endif
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate #if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
264*0Sstevel@tonic-gate 	meth=SSLv23_client_method();
265*0Sstevel@tonic-gate #elif !defined(OPENSSL_NO_SSL3)
266*0Sstevel@tonic-gate 	meth=SSLv3_client_method();
267*0Sstevel@tonic-gate #elif !defined(OPENSSL_NO_SSL2)
268*0Sstevel@tonic-gate 	meth=SSLv2_client_method();
269*0Sstevel@tonic-gate #endif
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate 	apps_startup();
272*0Sstevel@tonic-gate 	c_Pause=0;
273*0Sstevel@tonic-gate 	c_quiet=0;
274*0Sstevel@tonic-gate 	c_ign_eof=0;
275*0Sstevel@tonic-gate 	c_debug=0;
276*0Sstevel@tonic-gate 	c_msg=0;
277*0Sstevel@tonic-gate 	c_showcerts=0;
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate 	if (bio_err == NULL)
280*0Sstevel@tonic-gate 		bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 	if (!load_config(bio_err, NULL))
283*0Sstevel@tonic-gate 		goto end;
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate 	if (	((cbuf=OPENSSL_malloc(BUFSIZZ)) == NULL) ||
286*0Sstevel@tonic-gate 		((sbuf=OPENSSL_malloc(BUFSIZZ)) == NULL) ||
287*0Sstevel@tonic-gate 		((mbuf=OPENSSL_malloc(BUFSIZZ)) == NULL))
288*0Sstevel@tonic-gate 		{
289*0Sstevel@tonic-gate 		BIO_printf(bio_err,"out of memory\n");
290*0Sstevel@tonic-gate 		goto end;
291*0Sstevel@tonic-gate 		}
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 	verify_depth=0;
294*0Sstevel@tonic-gate 	verify_error=X509_V_OK;
295*0Sstevel@tonic-gate #ifdef FIONBIO
296*0Sstevel@tonic-gate 	c_nbio=0;
297*0Sstevel@tonic-gate #endif
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 	argc--;
300*0Sstevel@tonic-gate 	argv++;
301*0Sstevel@tonic-gate 	while (argc >= 1)
302*0Sstevel@tonic-gate 		{
303*0Sstevel@tonic-gate 		if	(strcmp(*argv,"-host") == 0)
304*0Sstevel@tonic-gate 			{
305*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
306*0Sstevel@tonic-gate 			host= *(++argv);
307*0Sstevel@tonic-gate 			}
308*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-port") == 0)
309*0Sstevel@tonic-gate 			{
310*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
311*0Sstevel@tonic-gate 			port=atoi(*(++argv));
312*0Sstevel@tonic-gate 			if (port == 0) goto bad;
313*0Sstevel@tonic-gate 			}
314*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-connect") == 0)
315*0Sstevel@tonic-gate 			{
316*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
317*0Sstevel@tonic-gate 			if (!extract_host_port(*(++argv),&host,NULL,&port))
318*0Sstevel@tonic-gate 				goto bad;
319*0Sstevel@tonic-gate 			}
320*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-verify") == 0)
321*0Sstevel@tonic-gate 			{
322*0Sstevel@tonic-gate 			verify=SSL_VERIFY_PEER;
323*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
324*0Sstevel@tonic-gate 			verify_depth=atoi(*(++argv));
325*0Sstevel@tonic-gate 			BIO_printf(bio_err,"verify depth is %d\n",verify_depth);
326*0Sstevel@tonic-gate 			}
327*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-cert") == 0)
328*0Sstevel@tonic-gate 			{
329*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
330*0Sstevel@tonic-gate 			cert_file= *(++argv);
331*0Sstevel@tonic-gate 			}
332*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-crl_check") == 0)
333*0Sstevel@tonic-gate 			vflags |= X509_V_FLAG_CRL_CHECK;
334*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-crl_check_all") == 0)
335*0Sstevel@tonic-gate 			vflags |= X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL;
336*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-prexit") == 0)
337*0Sstevel@tonic-gate 			prexit=1;
338*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-crlf") == 0)
339*0Sstevel@tonic-gate 			crlf=1;
340*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-quiet") == 0)
341*0Sstevel@tonic-gate 			{
342*0Sstevel@tonic-gate 			c_quiet=1;
343*0Sstevel@tonic-gate 			c_ign_eof=1;
344*0Sstevel@tonic-gate 			}
345*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-ign_eof") == 0)
346*0Sstevel@tonic-gate 			c_ign_eof=1;
347*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-pause") == 0)
348*0Sstevel@tonic-gate 			c_Pause=1;
349*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-debug") == 0)
350*0Sstevel@tonic-gate 			c_debug=1;
351*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-msg") == 0)
352*0Sstevel@tonic-gate 			c_msg=1;
353*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-showcerts") == 0)
354*0Sstevel@tonic-gate 			c_showcerts=1;
355*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-nbio_test") == 0)
356*0Sstevel@tonic-gate 			nbio_test=1;
357*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-state") == 0)
358*0Sstevel@tonic-gate 			state=1;
359*0Sstevel@tonic-gate #ifndef OPENSSL_NO_SSL2
360*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-ssl2") == 0)
361*0Sstevel@tonic-gate 			meth=SSLv2_client_method();
362*0Sstevel@tonic-gate #endif
363*0Sstevel@tonic-gate #ifndef OPENSSL_NO_SSL3
364*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-ssl3") == 0)
365*0Sstevel@tonic-gate 			meth=SSLv3_client_method();
366*0Sstevel@tonic-gate #endif
367*0Sstevel@tonic-gate #ifndef OPENSSL_NO_TLS1
368*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-tls1") == 0)
369*0Sstevel@tonic-gate 			meth=TLSv1_client_method();
370*0Sstevel@tonic-gate #endif
371*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-bugs") == 0)
372*0Sstevel@tonic-gate 			bugs=1;
373*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-key") == 0)
374*0Sstevel@tonic-gate 			{
375*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
376*0Sstevel@tonic-gate 			key_file= *(++argv);
377*0Sstevel@tonic-gate 			}
378*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-reconnect") == 0)
379*0Sstevel@tonic-gate 			{
380*0Sstevel@tonic-gate 			reconnect=5;
381*0Sstevel@tonic-gate 			}
382*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-CApath") == 0)
383*0Sstevel@tonic-gate 			{
384*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
385*0Sstevel@tonic-gate 			CApath= *(++argv);
386*0Sstevel@tonic-gate 			}
387*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-CAfile") == 0)
388*0Sstevel@tonic-gate 			{
389*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
390*0Sstevel@tonic-gate 			CAfile= *(++argv);
391*0Sstevel@tonic-gate 			}
392*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-no_tls1") == 0)
393*0Sstevel@tonic-gate 			off|=SSL_OP_NO_TLSv1;
394*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-no_ssl3") == 0)
395*0Sstevel@tonic-gate 			off|=SSL_OP_NO_SSLv3;
396*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-no_ssl2") == 0)
397*0Sstevel@tonic-gate 			off|=SSL_OP_NO_SSLv2;
398*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-serverpref") == 0)
399*0Sstevel@tonic-gate 			off|=SSL_OP_CIPHER_SERVER_PREFERENCE;
400*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-cipher") == 0)
401*0Sstevel@tonic-gate 			{
402*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
403*0Sstevel@tonic-gate 			cipher= *(++argv);
404*0Sstevel@tonic-gate 			}
405*0Sstevel@tonic-gate #ifdef FIONBIO
406*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-nbio") == 0)
407*0Sstevel@tonic-gate 			{ c_nbio=1; }
408*0Sstevel@tonic-gate #endif
409*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-starttls") == 0)
410*0Sstevel@tonic-gate 			{
411*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
412*0Sstevel@tonic-gate 			++argv;
413*0Sstevel@tonic-gate 			if (strcmp(*argv,"smtp") == 0)
414*0Sstevel@tonic-gate 				starttls_proto = 1;
415*0Sstevel@tonic-gate 			else if (strcmp(*argv,"pop3") == 0)
416*0Sstevel@tonic-gate 				starttls_proto = 2;
417*0Sstevel@tonic-gate 			else
418*0Sstevel@tonic-gate 				goto bad;
419*0Sstevel@tonic-gate 			}
420*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
421*0Sstevel@tonic-gate 		else if	(strcmp(*argv,"-engine") == 0)
422*0Sstevel@tonic-gate 			{
423*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
424*0Sstevel@tonic-gate 			engine_id = *(++argv);
425*0Sstevel@tonic-gate 			}
426*0Sstevel@tonic-gate #endif
427*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-rand") == 0)
428*0Sstevel@tonic-gate 			{
429*0Sstevel@tonic-gate 			if (--argc < 1) goto bad;
430*0Sstevel@tonic-gate 			inrand= *(++argv);
431*0Sstevel@tonic-gate 			}
432*0Sstevel@tonic-gate 		else
433*0Sstevel@tonic-gate 			{
434*0Sstevel@tonic-gate 			BIO_printf(bio_err,"unknown option %s\n",*argv);
435*0Sstevel@tonic-gate 			badop=1;
436*0Sstevel@tonic-gate 			break;
437*0Sstevel@tonic-gate 			}
438*0Sstevel@tonic-gate 		argc--;
439*0Sstevel@tonic-gate 		argv++;
440*0Sstevel@tonic-gate 		}
441*0Sstevel@tonic-gate 	if (badop)
442*0Sstevel@tonic-gate 		{
443*0Sstevel@tonic-gate bad:
444*0Sstevel@tonic-gate 		sc_usage();
445*0Sstevel@tonic-gate 		goto end;
446*0Sstevel@tonic-gate 		}
447*0Sstevel@tonic-gate 
448*0Sstevel@tonic-gate 	OpenSSL_add_ssl_algorithms();
449*0Sstevel@tonic-gate 	SSL_load_error_strings();
450*0Sstevel@tonic-gate 
451*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
452*0Sstevel@tonic-gate         e = setup_engine(bio_err, engine_id, 1);
453*0Sstevel@tonic-gate #endif
454*0Sstevel@tonic-gate 
455*0Sstevel@tonic-gate 	if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL
456*0Sstevel@tonic-gate 		&& !RAND_status())
457*0Sstevel@tonic-gate 		{
458*0Sstevel@tonic-gate 		BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
459*0Sstevel@tonic-gate 		}
460*0Sstevel@tonic-gate 	if (inrand != NULL)
461*0Sstevel@tonic-gate 		BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
462*0Sstevel@tonic-gate 			app_RAND_load_files(inrand));
463*0Sstevel@tonic-gate 
464*0Sstevel@tonic-gate 	if (bio_c_out == NULL)
465*0Sstevel@tonic-gate 		{
466*0Sstevel@tonic-gate 		if (c_quiet && !c_debug && !c_msg)
467*0Sstevel@tonic-gate 			{
468*0Sstevel@tonic-gate 			bio_c_out=BIO_new(BIO_s_null());
469*0Sstevel@tonic-gate 			}
470*0Sstevel@tonic-gate 		else
471*0Sstevel@tonic-gate 			{
472*0Sstevel@tonic-gate 			if (bio_c_out == NULL)
473*0Sstevel@tonic-gate 				bio_c_out=BIO_new_fp(stdout,BIO_NOCLOSE);
474*0Sstevel@tonic-gate 			}
475*0Sstevel@tonic-gate 		}
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate 	ctx=SSL_CTX_new(meth);
478*0Sstevel@tonic-gate 	if (ctx == NULL)
479*0Sstevel@tonic-gate 		{
480*0Sstevel@tonic-gate 		ERR_print_errors(bio_err);
481*0Sstevel@tonic-gate 		goto end;
482*0Sstevel@tonic-gate 		}
483*0Sstevel@tonic-gate 
484*0Sstevel@tonic-gate 	if (bugs)
485*0Sstevel@tonic-gate 		SSL_CTX_set_options(ctx,SSL_OP_ALL|off);
486*0Sstevel@tonic-gate 	else
487*0Sstevel@tonic-gate 		SSL_CTX_set_options(ctx,off);
488*0Sstevel@tonic-gate 
489*0Sstevel@tonic-gate 	if (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback);
490*0Sstevel@tonic-gate 	if (cipher != NULL)
491*0Sstevel@tonic-gate 		if(!SSL_CTX_set_cipher_list(ctx,cipher)) {
492*0Sstevel@tonic-gate 		BIO_printf(bio_err,"error setting cipher list\n");
493*0Sstevel@tonic-gate 		ERR_print_errors(bio_err);
494*0Sstevel@tonic-gate 		goto end;
495*0Sstevel@tonic-gate 	}
496*0Sstevel@tonic-gate #if 0
497*0Sstevel@tonic-gate 	else
498*0Sstevel@tonic-gate 		SSL_CTX_set_cipher_list(ctx,getenv("SSL_CIPHER"));
499*0Sstevel@tonic-gate #endif
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate 	SSL_CTX_set_verify(ctx,verify,verify_callback);
502*0Sstevel@tonic-gate 	if (!set_cert_stuff(ctx,cert_file,key_file))
503*0Sstevel@tonic-gate 		goto end;
504*0Sstevel@tonic-gate 
505*0Sstevel@tonic-gate 	if ((!SSL_CTX_load_verify_locations(ctx,CAfile,CApath)) ||
506*0Sstevel@tonic-gate 		(!SSL_CTX_set_default_verify_paths(ctx)))
507*0Sstevel@tonic-gate 		{
508*0Sstevel@tonic-gate 		/* BIO_printf(bio_err,"error setting default verify locations\n"); */
509*0Sstevel@tonic-gate 		ERR_print_errors(bio_err);
510*0Sstevel@tonic-gate 		/* goto end; */
511*0Sstevel@tonic-gate 		}
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate 	store = SSL_CTX_get_cert_store(ctx);
514*0Sstevel@tonic-gate 	X509_STORE_set_flags(store, vflags);
515*0Sstevel@tonic-gate 
516*0Sstevel@tonic-gate 	con=SSL_new(ctx);
517*0Sstevel@tonic-gate #ifndef OPENSSL_NO_KRB5
518*0Sstevel@tonic-gate 	if (con  &&  (con->kssl_ctx = kssl_ctx_new()) != NULL)
519*0Sstevel@tonic-gate                 {
520*0Sstevel@tonic-gate                 kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVER, host);
521*0Sstevel@tonic-gate 		}
522*0Sstevel@tonic-gate #endif	/* OPENSSL_NO_KRB5  */
523*0Sstevel@tonic-gate /*	SSL_set_cipher_list(con,"RC4-MD5"); */
524*0Sstevel@tonic-gate 
525*0Sstevel@tonic-gate re_start:
526*0Sstevel@tonic-gate 
527*0Sstevel@tonic-gate 	if (init_client(&s,host,port) == 0)
528*0Sstevel@tonic-gate 		{
529*0Sstevel@tonic-gate 		BIO_printf(bio_err,"connect:errno=%d\n",get_last_socket_error());
530*0Sstevel@tonic-gate 		SHUTDOWN(s);
531*0Sstevel@tonic-gate 		goto end;
532*0Sstevel@tonic-gate 		}
533*0Sstevel@tonic-gate 	BIO_printf(bio_c_out,"CONNECTED(%08X)\n",s);
534*0Sstevel@tonic-gate 
535*0Sstevel@tonic-gate #ifdef FIONBIO
536*0Sstevel@tonic-gate 	if (c_nbio)
537*0Sstevel@tonic-gate 		{
538*0Sstevel@tonic-gate 		unsigned long l=1;
539*0Sstevel@tonic-gate 		BIO_printf(bio_c_out,"turning on non blocking io\n");
540*0Sstevel@tonic-gate 		if (BIO_socket_ioctl(s,FIONBIO,&l) < 0)
541*0Sstevel@tonic-gate 			{
542*0Sstevel@tonic-gate 			ERR_print_errors(bio_err);
543*0Sstevel@tonic-gate 			goto end;
544*0Sstevel@tonic-gate 			}
545*0Sstevel@tonic-gate 		}
546*0Sstevel@tonic-gate #endif
547*0Sstevel@tonic-gate 	if (c_Pause & 0x01) con->debug=1;
548*0Sstevel@tonic-gate 	sbio=BIO_new_socket(s,BIO_NOCLOSE);
549*0Sstevel@tonic-gate 
550*0Sstevel@tonic-gate 	if (nbio_test)
551*0Sstevel@tonic-gate 		{
552*0Sstevel@tonic-gate 		BIO *test;
553*0Sstevel@tonic-gate 
554*0Sstevel@tonic-gate 		test=BIO_new(BIO_f_nbio_test());
555*0Sstevel@tonic-gate 		sbio=BIO_push(test,sbio);
556*0Sstevel@tonic-gate 		}
557*0Sstevel@tonic-gate 
558*0Sstevel@tonic-gate 	if (c_debug)
559*0Sstevel@tonic-gate 		{
560*0Sstevel@tonic-gate 		con->debug=1;
561*0Sstevel@tonic-gate 		BIO_set_callback(sbio,bio_dump_cb);
562*0Sstevel@tonic-gate 		BIO_set_callback_arg(sbio,bio_c_out);
563*0Sstevel@tonic-gate 		}
564*0Sstevel@tonic-gate 	if (c_msg)
565*0Sstevel@tonic-gate 		{
566*0Sstevel@tonic-gate 		SSL_set_msg_callback(con, msg_cb);
567*0Sstevel@tonic-gate 		SSL_set_msg_callback_arg(con, bio_c_out);
568*0Sstevel@tonic-gate 		}
569*0Sstevel@tonic-gate 
570*0Sstevel@tonic-gate 	SSL_set_bio(con,sbio,sbio);
571*0Sstevel@tonic-gate 	SSL_set_connect_state(con);
572*0Sstevel@tonic-gate 
573*0Sstevel@tonic-gate 	/* ok, lets connect */
574*0Sstevel@tonic-gate 	width=SSL_get_fd(con)+1;
575*0Sstevel@tonic-gate 
576*0Sstevel@tonic-gate 	read_tty=1;
577*0Sstevel@tonic-gate 	write_tty=0;
578*0Sstevel@tonic-gate 	tty_on=0;
579*0Sstevel@tonic-gate 	read_ssl=1;
580*0Sstevel@tonic-gate 	write_ssl=1;
581*0Sstevel@tonic-gate 
582*0Sstevel@tonic-gate 	cbuf_len=0;
583*0Sstevel@tonic-gate 	cbuf_off=0;
584*0Sstevel@tonic-gate 	sbuf_len=0;
585*0Sstevel@tonic-gate 	sbuf_off=0;
586*0Sstevel@tonic-gate 
587*0Sstevel@tonic-gate 	/* This is an ugly hack that does a lot of assumptions */
588*0Sstevel@tonic-gate 	if (starttls_proto == 1)
589*0Sstevel@tonic-gate 		{
590*0Sstevel@tonic-gate 		BIO_read(sbio,mbuf,BUFSIZZ);
591*0Sstevel@tonic-gate 		BIO_printf(sbio,"STARTTLS\r\n");
592*0Sstevel@tonic-gate 		BIO_read(sbio,sbuf,BUFSIZZ);
593*0Sstevel@tonic-gate 		}
594*0Sstevel@tonic-gate 	if (starttls_proto == 2)
595*0Sstevel@tonic-gate 		{
596*0Sstevel@tonic-gate 		BIO_read(sbio,mbuf,BUFSIZZ);
597*0Sstevel@tonic-gate 		BIO_printf(sbio,"STLS\r\n");
598*0Sstevel@tonic-gate 		BIO_read(sbio,sbuf,BUFSIZZ);
599*0Sstevel@tonic-gate 		}
600*0Sstevel@tonic-gate 
601*0Sstevel@tonic-gate 	for (;;)
602*0Sstevel@tonic-gate 		{
603*0Sstevel@tonic-gate 		FD_ZERO(&readfds);
604*0Sstevel@tonic-gate 		FD_ZERO(&writefds);
605*0Sstevel@tonic-gate 
606*0Sstevel@tonic-gate 		if (SSL_in_init(con) && !SSL_total_renegotiations(con))
607*0Sstevel@tonic-gate 			{
608*0Sstevel@tonic-gate 			in_init=1;
609*0Sstevel@tonic-gate 			tty_on=0;
610*0Sstevel@tonic-gate 			}
611*0Sstevel@tonic-gate 		else
612*0Sstevel@tonic-gate 			{
613*0Sstevel@tonic-gate 			tty_on=1;
614*0Sstevel@tonic-gate 			if (in_init)
615*0Sstevel@tonic-gate 				{
616*0Sstevel@tonic-gate 				in_init=0;
617*0Sstevel@tonic-gate 				print_stuff(bio_c_out,con,full_log);
618*0Sstevel@tonic-gate 				if (full_log > 0) full_log--;
619*0Sstevel@tonic-gate 
620*0Sstevel@tonic-gate 				if (starttls_proto)
621*0Sstevel@tonic-gate 					{
622*0Sstevel@tonic-gate 					BIO_printf(bio_err,"%s",mbuf);
623*0Sstevel@tonic-gate 					/* We don't need to know any more */
624*0Sstevel@tonic-gate 					starttls_proto = 0;
625*0Sstevel@tonic-gate 					}
626*0Sstevel@tonic-gate 
627*0Sstevel@tonic-gate 				if (reconnect)
628*0Sstevel@tonic-gate 					{
629*0Sstevel@tonic-gate 					reconnect--;
630*0Sstevel@tonic-gate 					BIO_printf(bio_c_out,"drop connection and then reconnect\n");
631*0Sstevel@tonic-gate 					SSL_shutdown(con);
632*0Sstevel@tonic-gate 					SSL_set_connect_state(con);
633*0Sstevel@tonic-gate 					SHUTDOWN(SSL_get_fd(con));
634*0Sstevel@tonic-gate 					goto re_start;
635*0Sstevel@tonic-gate 					}
636*0Sstevel@tonic-gate 				}
637*0Sstevel@tonic-gate 			}
638*0Sstevel@tonic-gate 
639*0Sstevel@tonic-gate 		ssl_pending = read_ssl && SSL_pending(con);
640*0Sstevel@tonic-gate 
641*0Sstevel@tonic-gate 		if (!ssl_pending)
642*0Sstevel@tonic-gate 			{
643*0Sstevel@tonic-gate #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
644*0Sstevel@tonic-gate 			if (tty_on)
645*0Sstevel@tonic-gate 				{
646*0Sstevel@tonic-gate 				if (read_tty)  FD_SET(fileno(stdin),&readfds);
647*0Sstevel@tonic-gate 				if (write_tty) FD_SET(fileno(stdout),&writefds);
648*0Sstevel@tonic-gate 				}
649*0Sstevel@tonic-gate 			if (read_ssl)
650*0Sstevel@tonic-gate 				FD_SET(SSL_get_fd(con),&readfds);
651*0Sstevel@tonic-gate 			if (write_ssl)
652*0Sstevel@tonic-gate 				FD_SET(SSL_get_fd(con),&writefds);
653*0Sstevel@tonic-gate #else
654*0Sstevel@tonic-gate 			if(!tty_on || !write_tty) {
655*0Sstevel@tonic-gate 				if (read_ssl)
656*0Sstevel@tonic-gate 					FD_SET(SSL_get_fd(con),&readfds);
657*0Sstevel@tonic-gate 				if (write_ssl)
658*0Sstevel@tonic-gate 					FD_SET(SSL_get_fd(con),&writefds);
659*0Sstevel@tonic-gate 			}
660*0Sstevel@tonic-gate #endif
661*0Sstevel@tonic-gate /*			printf("mode tty(%d %d%d) ssl(%d%d)\n",
662*0Sstevel@tonic-gate 				tty_on,read_tty,write_tty,read_ssl,write_ssl);*/
663*0Sstevel@tonic-gate 
664*0Sstevel@tonic-gate 			/* Note: under VMS with SOCKETSHR the second parameter
665*0Sstevel@tonic-gate 			 * is currently of type (int *) whereas under other
666*0Sstevel@tonic-gate 			 * systems it is (void *) if you don't have a cast it
667*0Sstevel@tonic-gate 			 * will choke the compiler: if you do have a cast then
668*0Sstevel@tonic-gate 			 * you can either go for (int *) or (void *).
669*0Sstevel@tonic-gate 			 */
670*0Sstevel@tonic-gate #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
671*0Sstevel@tonic-gate                         /* Under Windows/DOS we make the assumption that we can
672*0Sstevel@tonic-gate 			 * always write to the tty: therefore if we need to
673*0Sstevel@tonic-gate 			 * write to the tty we just fall through. Otherwise
674*0Sstevel@tonic-gate 			 * we timeout the select every second and see if there
675*0Sstevel@tonic-gate 			 * are any keypresses. Note: this is a hack, in a proper
676*0Sstevel@tonic-gate 			 * Windows application we wouldn't do this.
677*0Sstevel@tonic-gate 			 */
678*0Sstevel@tonic-gate 			i=0;
679*0Sstevel@tonic-gate 			if(!write_tty) {
680*0Sstevel@tonic-gate 				if(read_tty) {
681*0Sstevel@tonic-gate 					tv.tv_sec = 1;
682*0Sstevel@tonic-gate 					tv.tv_usec = 0;
683*0Sstevel@tonic-gate 					i=select(width,(void *)&readfds,(void *)&writefds,
684*0Sstevel@tonic-gate 						 NULL,&tv);
685*0Sstevel@tonic-gate #if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)
686*0Sstevel@tonic-gate 					if(!i && (!_kbhit() || !read_tty) ) continue;
687*0Sstevel@tonic-gate #else
688*0Sstevel@tonic-gate 					if(!i && (!((_kbhit()) || (WAIT_OBJECT_0 == WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0))) || !read_tty) ) continue;
689*0Sstevel@tonic-gate #endif
690*0Sstevel@tonic-gate 				} else 	i=select(width,(void *)&readfds,(void *)&writefds,
691*0Sstevel@tonic-gate 					 NULL,NULL);
692*0Sstevel@tonic-gate 			}
693*0Sstevel@tonic-gate #else
694*0Sstevel@tonic-gate 			i=select(width,(void *)&readfds,(void *)&writefds,
695*0Sstevel@tonic-gate 				 NULL,NULL);
696*0Sstevel@tonic-gate #endif
697*0Sstevel@tonic-gate 			if ( i < 0)
698*0Sstevel@tonic-gate 				{
699*0Sstevel@tonic-gate 				BIO_printf(bio_err,"bad select %d\n",
700*0Sstevel@tonic-gate 				get_last_socket_error());
701*0Sstevel@tonic-gate 				goto shut;
702*0Sstevel@tonic-gate 				/* goto end; */
703*0Sstevel@tonic-gate 				}
704*0Sstevel@tonic-gate 			}
705*0Sstevel@tonic-gate 
706*0Sstevel@tonic-gate 		if (!ssl_pending && FD_ISSET(SSL_get_fd(con),&writefds))
707*0Sstevel@tonic-gate 			{
708*0Sstevel@tonic-gate 			k=SSL_write(con,&(cbuf[cbuf_off]),
709*0Sstevel@tonic-gate 				(unsigned int)cbuf_len);
710*0Sstevel@tonic-gate 			switch (SSL_get_error(con,k))
711*0Sstevel@tonic-gate 				{
712*0Sstevel@tonic-gate 			case SSL_ERROR_NONE:
713*0Sstevel@tonic-gate 				cbuf_off+=k;
714*0Sstevel@tonic-gate 				cbuf_len-=k;
715*0Sstevel@tonic-gate 				if (k <= 0) goto end;
716*0Sstevel@tonic-gate 				/* we have done a  write(con,NULL,0); */
717*0Sstevel@tonic-gate 				if (cbuf_len <= 0)
718*0Sstevel@tonic-gate 					{
719*0Sstevel@tonic-gate 					read_tty=1;
720*0Sstevel@tonic-gate 					write_ssl=0;
721*0Sstevel@tonic-gate 					}
722*0Sstevel@tonic-gate 				else /* if (cbuf_len > 0) */
723*0Sstevel@tonic-gate 					{
724*0Sstevel@tonic-gate 					read_tty=0;
725*0Sstevel@tonic-gate 					write_ssl=1;
726*0Sstevel@tonic-gate 					}
727*0Sstevel@tonic-gate 				break;
728*0Sstevel@tonic-gate 			case SSL_ERROR_WANT_WRITE:
729*0Sstevel@tonic-gate 				BIO_printf(bio_c_out,"write W BLOCK\n");
730*0Sstevel@tonic-gate 				write_ssl=1;
731*0Sstevel@tonic-gate 				read_tty=0;
732*0Sstevel@tonic-gate 				break;
733*0Sstevel@tonic-gate 			case SSL_ERROR_WANT_READ:
734*0Sstevel@tonic-gate 				BIO_printf(bio_c_out,"write R BLOCK\n");
735*0Sstevel@tonic-gate 				write_tty=0;
736*0Sstevel@tonic-gate 				read_ssl=1;
737*0Sstevel@tonic-gate 				write_ssl=0;
738*0Sstevel@tonic-gate 				break;
739*0Sstevel@tonic-gate 			case SSL_ERROR_WANT_X509_LOOKUP:
740*0Sstevel@tonic-gate 				BIO_printf(bio_c_out,"write X BLOCK\n");
741*0Sstevel@tonic-gate 				break;
742*0Sstevel@tonic-gate 			case SSL_ERROR_ZERO_RETURN:
743*0Sstevel@tonic-gate 				if (cbuf_len != 0)
744*0Sstevel@tonic-gate 					{
745*0Sstevel@tonic-gate 					BIO_printf(bio_c_out,"shutdown\n");
746*0Sstevel@tonic-gate 					goto shut;
747*0Sstevel@tonic-gate 					}
748*0Sstevel@tonic-gate 				else
749*0Sstevel@tonic-gate 					{
750*0Sstevel@tonic-gate 					read_tty=1;
751*0Sstevel@tonic-gate 					write_ssl=0;
752*0Sstevel@tonic-gate 					break;
753*0Sstevel@tonic-gate 					}
754*0Sstevel@tonic-gate 
755*0Sstevel@tonic-gate 			case SSL_ERROR_SYSCALL:
756*0Sstevel@tonic-gate 				if ((k != 0) || (cbuf_len != 0))
757*0Sstevel@tonic-gate 					{
758*0Sstevel@tonic-gate 					BIO_printf(bio_err,"write:errno=%d\n",
759*0Sstevel@tonic-gate 						get_last_socket_error());
760*0Sstevel@tonic-gate 					goto shut;
761*0Sstevel@tonic-gate 					}
762*0Sstevel@tonic-gate 				else
763*0Sstevel@tonic-gate 					{
764*0Sstevel@tonic-gate 					read_tty=1;
765*0Sstevel@tonic-gate 					write_ssl=0;
766*0Sstevel@tonic-gate 					}
767*0Sstevel@tonic-gate 				break;
768*0Sstevel@tonic-gate 			case SSL_ERROR_SSL:
769*0Sstevel@tonic-gate 				ERR_print_errors(bio_err);
770*0Sstevel@tonic-gate 				goto shut;
771*0Sstevel@tonic-gate 				}
772*0Sstevel@tonic-gate 			}
773*0Sstevel@tonic-gate #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
774*0Sstevel@tonic-gate 		/* Assume Windows/DOS can always write */
775*0Sstevel@tonic-gate 		else if (!ssl_pending && write_tty)
776*0Sstevel@tonic-gate #else
777*0Sstevel@tonic-gate 		else if (!ssl_pending && FD_ISSET(fileno(stdout),&writefds))
778*0Sstevel@tonic-gate #endif
779*0Sstevel@tonic-gate 			{
780*0Sstevel@tonic-gate #ifdef CHARSET_EBCDIC
781*0Sstevel@tonic-gate 			ascii2ebcdic(&(sbuf[sbuf_off]),&(sbuf[sbuf_off]),sbuf_len);
782*0Sstevel@tonic-gate #endif
783*0Sstevel@tonic-gate 			i=write(fileno(stdout),&(sbuf[sbuf_off]),sbuf_len);
784*0Sstevel@tonic-gate 
785*0Sstevel@tonic-gate 			if (i <= 0)
786*0Sstevel@tonic-gate 				{
787*0Sstevel@tonic-gate 				BIO_printf(bio_c_out,"DONE\n");
788*0Sstevel@tonic-gate 				goto shut;
789*0Sstevel@tonic-gate 				/* goto end; */
790*0Sstevel@tonic-gate 				}
791*0Sstevel@tonic-gate 
792*0Sstevel@tonic-gate 			sbuf_len-=i;;
793*0Sstevel@tonic-gate 			sbuf_off+=i;
794*0Sstevel@tonic-gate 			if (sbuf_len <= 0)
795*0Sstevel@tonic-gate 				{
796*0Sstevel@tonic-gate 				read_ssl=1;
797*0Sstevel@tonic-gate 				write_tty=0;
798*0Sstevel@tonic-gate 				}
799*0Sstevel@tonic-gate 			}
800*0Sstevel@tonic-gate 		else if (ssl_pending || FD_ISSET(SSL_get_fd(con),&readfds))
801*0Sstevel@tonic-gate 			{
802*0Sstevel@tonic-gate #ifdef RENEG
803*0Sstevel@tonic-gate { static int iiii; if (++iiii == 52) { SSL_renegotiate(con); iiii=0; } }
804*0Sstevel@tonic-gate #endif
805*0Sstevel@tonic-gate #if 1
806*0Sstevel@tonic-gate 			k=SSL_read(con,sbuf,1024 /* BUFSIZZ */ );
807*0Sstevel@tonic-gate #else
808*0Sstevel@tonic-gate /* Demo for pending and peek :-) */
809*0Sstevel@tonic-gate 			k=SSL_read(con,sbuf,16);
810*0Sstevel@tonic-gate { char zbuf[10240];
811*0Sstevel@tonic-gate printf("read=%d pending=%d peek=%d\n",k,SSL_pending(con),SSL_peek(con,zbuf,10240));
812*0Sstevel@tonic-gate }
813*0Sstevel@tonic-gate #endif
814*0Sstevel@tonic-gate 
815*0Sstevel@tonic-gate 			switch (SSL_get_error(con,k))
816*0Sstevel@tonic-gate 				{
817*0Sstevel@tonic-gate 			case SSL_ERROR_NONE:
818*0Sstevel@tonic-gate 				if (k <= 0)
819*0Sstevel@tonic-gate 					goto end;
820*0Sstevel@tonic-gate 				sbuf_off=0;
821*0Sstevel@tonic-gate 				sbuf_len=k;
822*0Sstevel@tonic-gate 
823*0Sstevel@tonic-gate 				read_ssl=0;
824*0Sstevel@tonic-gate 				write_tty=1;
825*0Sstevel@tonic-gate 				break;
826*0Sstevel@tonic-gate 			case SSL_ERROR_WANT_WRITE:
827*0Sstevel@tonic-gate 				BIO_printf(bio_c_out,"read W BLOCK\n");
828*0Sstevel@tonic-gate 				write_ssl=1;
829*0Sstevel@tonic-gate 				read_tty=0;
830*0Sstevel@tonic-gate 				break;
831*0Sstevel@tonic-gate 			case SSL_ERROR_WANT_READ:
832*0Sstevel@tonic-gate 				BIO_printf(bio_c_out,"read R BLOCK\n");
833*0Sstevel@tonic-gate 				write_tty=0;
834*0Sstevel@tonic-gate 				read_ssl=1;
835*0Sstevel@tonic-gate 				if ((read_tty == 0) && (write_ssl == 0))
836*0Sstevel@tonic-gate 					write_ssl=1;
837*0Sstevel@tonic-gate 				break;
838*0Sstevel@tonic-gate 			case SSL_ERROR_WANT_X509_LOOKUP:
839*0Sstevel@tonic-gate 				BIO_printf(bio_c_out,"read X BLOCK\n");
840*0Sstevel@tonic-gate 				break;
841*0Sstevel@tonic-gate 			case SSL_ERROR_SYSCALL:
842*0Sstevel@tonic-gate 				BIO_printf(bio_err,"read:errno=%d\n",get_last_socket_error());
843*0Sstevel@tonic-gate 				goto shut;
844*0Sstevel@tonic-gate 			case SSL_ERROR_ZERO_RETURN:
845*0Sstevel@tonic-gate 				BIO_printf(bio_c_out,"closed\n");
846*0Sstevel@tonic-gate 				goto shut;
847*0Sstevel@tonic-gate 			case SSL_ERROR_SSL:
848*0Sstevel@tonic-gate 				ERR_print_errors(bio_err);
849*0Sstevel@tonic-gate 				goto shut;
850*0Sstevel@tonic-gate 				/* break; */
851*0Sstevel@tonic-gate 				}
852*0Sstevel@tonic-gate 			}
853*0Sstevel@tonic-gate 
854*0Sstevel@tonic-gate #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
855*0Sstevel@tonic-gate #if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)
856*0Sstevel@tonic-gate 		else if (_kbhit())
857*0Sstevel@tonic-gate #else
858*0Sstevel@tonic-gate 		else if ((_kbhit()) || (WAIT_OBJECT_0 == WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0)))
859*0Sstevel@tonic-gate #endif
860*0Sstevel@tonic-gate #else
861*0Sstevel@tonic-gate 		else if (FD_ISSET(fileno(stdin),&readfds))
862*0Sstevel@tonic-gate #endif
863*0Sstevel@tonic-gate 			{
864*0Sstevel@tonic-gate 			if (crlf)
865*0Sstevel@tonic-gate 				{
866*0Sstevel@tonic-gate 				int j, lf_num;
867*0Sstevel@tonic-gate 
868*0Sstevel@tonic-gate 				i=read(fileno(stdin),cbuf,BUFSIZZ/2);
869*0Sstevel@tonic-gate 				lf_num = 0;
870*0Sstevel@tonic-gate 				/* both loops are skipped when i <= 0 */
871*0Sstevel@tonic-gate 				for (j = 0; j < i; j++)
872*0Sstevel@tonic-gate 					if (cbuf[j] == '\n')
873*0Sstevel@tonic-gate 						lf_num++;
874*0Sstevel@tonic-gate 				for (j = i-1; j >= 0; j--)
875*0Sstevel@tonic-gate 					{
876*0Sstevel@tonic-gate 					cbuf[j+lf_num] = cbuf[j];
877*0Sstevel@tonic-gate 					if (cbuf[j] == '\n')
878*0Sstevel@tonic-gate 						{
879*0Sstevel@tonic-gate 						lf_num--;
880*0Sstevel@tonic-gate 						i++;
881*0Sstevel@tonic-gate 						cbuf[j+lf_num] = '\r';
882*0Sstevel@tonic-gate 						}
883*0Sstevel@tonic-gate 					}
884*0Sstevel@tonic-gate 				assert(lf_num == 0);
885*0Sstevel@tonic-gate 				}
886*0Sstevel@tonic-gate 			else
887*0Sstevel@tonic-gate 				i=read(fileno(stdin),cbuf,BUFSIZZ);
888*0Sstevel@tonic-gate 
889*0Sstevel@tonic-gate 			if ((!c_ign_eof) && ((i <= 0) || (cbuf[0] == 'Q')))
890*0Sstevel@tonic-gate 				{
891*0Sstevel@tonic-gate 				BIO_printf(bio_err,"DONE\n");
892*0Sstevel@tonic-gate 				goto shut;
893*0Sstevel@tonic-gate 				}
894*0Sstevel@tonic-gate 
895*0Sstevel@tonic-gate 			if ((!c_ign_eof) && (cbuf[0] == 'R'))
896*0Sstevel@tonic-gate 				{
897*0Sstevel@tonic-gate 				BIO_printf(bio_err,"RENEGOTIATING\n");
898*0Sstevel@tonic-gate 				SSL_renegotiate(con);
899*0Sstevel@tonic-gate 				cbuf_len=0;
900*0Sstevel@tonic-gate 				}
901*0Sstevel@tonic-gate 			else
902*0Sstevel@tonic-gate 				{
903*0Sstevel@tonic-gate 				cbuf_len=i;
904*0Sstevel@tonic-gate 				cbuf_off=0;
905*0Sstevel@tonic-gate #ifdef CHARSET_EBCDIC
906*0Sstevel@tonic-gate 				ebcdic2ascii(cbuf, cbuf, i);
907*0Sstevel@tonic-gate #endif
908*0Sstevel@tonic-gate 				}
909*0Sstevel@tonic-gate 
910*0Sstevel@tonic-gate 			write_ssl=1;
911*0Sstevel@tonic-gate 			read_tty=0;
912*0Sstevel@tonic-gate 			}
913*0Sstevel@tonic-gate 		}
914*0Sstevel@tonic-gate shut:
915*0Sstevel@tonic-gate 	SSL_shutdown(con);
916*0Sstevel@tonic-gate 	SHUTDOWN(SSL_get_fd(con));
917*0Sstevel@tonic-gate 	ret=0;
918*0Sstevel@tonic-gate end:
919*0Sstevel@tonic-gate 	if(prexit) print_stuff(bio_c_out,con,1);
920*0Sstevel@tonic-gate 	if (con != NULL) SSL_free(con);
921*0Sstevel@tonic-gate 	if (con2 != NULL) SSL_free(con2);
922*0Sstevel@tonic-gate 	if (ctx != NULL) SSL_CTX_free(ctx);
923*0Sstevel@tonic-gate 	if (cbuf != NULL) { OPENSSL_cleanse(cbuf,BUFSIZZ); OPENSSL_free(cbuf); }
924*0Sstevel@tonic-gate 	if (sbuf != NULL) { OPENSSL_cleanse(sbuf,BUFSIZZ); OPENSSL_free(sbuf); }
925*0Sstevel@tonic-gate 	if (mbuf != NULL) { OPENSSL_cleanse(mbuf,BUFSIZZ); OPENSSL_free(mbuf); }
926*0Sstevel@tonic-gate 	if (bio_c_out != NULL)
927*0Sstevel@tonic-gate 		{
928*0Sstevel@tonic-gate 		BIO_free(bio_c_out);
929*0Sstevel@tonic-gate 		bio_c_out=NULL;
930*0Sstevel@tonic-gate 		}
931*0Sstevel@tonic-gate 	apps_shutdown();
932*0Sstevel@tonic-gate 	OPENSSL_EXIT(ret);
933*0Sstevel@tonic-gate 	}
934*0Sstevel@tonic-gate 
935*0Sstevel@tonic-gate 
936*0Sstevel@tonic-gate static void print_stuff(BIO *bio, SSL *s, int full)
937*0Sstevel@tonic-gate 	{
938*0Sstevel@tonic-gate 	X509 *peer=NULL;
939*0Sstevel@tonic-gate 	char *p;
940*0Sstevel@tonic-gate 	static char *space="                ";
941*0Sstevel@tonic-gate 	char buf[BUFSIZ];
942*0Sstevel@tonic-gate 	STACK_OF(X509) *sk;
943*0Sstevel@tonic-gate 	STACK_OF(X509_NAME) *sk2;
944*0Sstevel@tonic-gate 	SSL_CIPHER *c;
945*0Sstevel@tonic-gate 	X509_NAME *xn;
946*0Sstevel@tonic-gate 	int j,i;
947*0Sstevel@tonic-gate 
948*0Sstevel@tonic-gate 	if (full)
949*0Sstevel@tonic-gate 		{
950*0Sstevel@tonic-gate 		int got_a_chain = 0;
951*0Sstevel@tonic-gate 
952*0Sstevel@tonic-gate 		sk=SSL_get_peer_cert_chain(s);
953*0Sstevel@tonic-gate 		if (sk != NULL)
954*0Sstevel@tonic-gate 			{
955*0Sstevel@tonic-gate 			got_a_chain = 1; /* we don't have it for SSL2 (yet) */
956*0Sstevel@tonic-gate 
957*0Sstevel@tonic-gate 			BIO_printf(bio,"---\nCertificate chain\n");
958*0Sstevel@tonic-gate 			for (i=0; i<sk_X509_num(sk); i++)
959*0Sstevel@tonic-gate 				{
960*0Sstevel@tonic-gate 				X509_NAME_oneline(X509_get_subject_name(
961*0Sstevel@tonic-gate 					sk_X509_value(sk,i)),buf,sizeof buf);
962*0Sstevel@tonic-gate 				BIO_printf(bio,"%2d s:%s\n",i,buf);
963*0Sstevel@tonic-gate 				X509_NAME_oneline(X509_get_issuer_name(
964*0Sstevel@tonic-gate 					sk_X509_value(sk,i)),buf,sizeof buf);
965*0Sstevel@tonic-gate 				BIO_printf(bio,"   i:%s\n",buf);
966*0Sstevel@tonic-gate 				if (c_showcerts)
967*0Sstevel@tonic-gate 					PEM_write_bio_X509(bio,sk_X509_value(sk,i));
968*0Sstevel@tonic-gate 				}
969*0Sstevel@tonic-gate 			}
970*0Sstevel@tonic-gate 
971*0Sstevel@tonic-gate 		BIO_printf(bio,"---\n");
972*0Sstevel@tonic-gate 		peer=SSL_get_peer_certificate(s);
973*0Sstevel@tonic-gate 		if (peer != NULL)
974*0Sstevel@tonic-gate 			{
975*0Sstevel@tonic-gate 			BIO_printf(bio,"Server certificate\n");
976*0Sstevel@tonic-gate 			if (!(c_showcerts && got_a_chain)) /* Redundant if we showed the whole chain */
977*0Sstevel@tonic-gate 				PEM_write_bio_X509(bio,peer);
978*0Sstevel@tonic-gate 			X509_NAME_oneline(X509_get_subject_name(peer),
979*0Sstevel@tonic-gate 				buf,sizeof buf);
980*0Sstevel@tonic-gate 			BIO_printf(bio,"subject=%s\n",buf);
981*0Sstevel@tonic-gate 			X509_NAME_oneline(X509_get_issuer_name(peer),
982*0Sstevel@tonic-gate 				buf,sizeof buf);
983*0Sstevel@tonic-gate 			BIO_printf(bio,"issuer=%s\n",buf);
984*0Sstevel@tonic-gate 			}
985*0Sstevel@tonic-gate 		else
986*0Sstevel@tonic-gate 			BIO_printf(bio,"no peer certificate available\n");
987*0Sstevel@tonic-gate 
988*0Sstevel@tonic-gate 		sk2=SSL_get_client_CA_list(s);
989*0Sstevel@tonic-gate 		if ((sk2 != NULL) && (sk_X509_NAME_num(sk2) > 0))
990*0Sstevel@tonic-gate 			{
991*0Sstevel@tonic-gate 			BIO_printf(bio,"---\nAcceptable client certificate CA names\n");
992*0Sstevel@tonic-gate 			for (i=0; i<sk_X509_NAME_num(sk2); i++)
993*0Sstevel@tonic-gate 				{
994*0Sstevel@tonic-gate 				xn=sk_X509_NAME_value(sk2,i);
995*0Sstevel@tonic-gate 				X509_NAME_oneline(xn,buf,sizeof(buf));
996*0Sstevel@tonic-gate 				BIO_write(bio,buf,strlen(buf));
997*0Sstevel@tonic-gate 				BIO_write(bio,"\n",1);
998*0Sstevel@tonic-gate 				}
999*0Sstevel@tonic-gate 			}
1000*0Sstevel@tonic-gate 		else
1001*0Sstevel@tonic-gate 			{
1002*0Sstevel@tonic-gate 			BIO_printf(bio,"---\nNo client certificate CA names sent\n");
1003*0Sstevel@tonic-gate 			}
1004*0Sstevel@tonic-gate 		p=SSL_get_shared_ciphers(s,buf,sizeof buf);
1005*0Sstevel@tonic-gate 		if (p != NULL)
1006*0Sstevel@tonic-gate 			{
1007*0Sstevel@tonic-gate 			/* This works only for SSL 2.  In later protocol
1008*0Sstevel@tonic-gate 			 * versions, the client does not know what other
1009*0Sstevel@tonic-gate 			 * ciphers (in addition to the one to be used
1010*0Sstevel@tonic-gate 			 * in the current connection) the server supports. */
1011*0Sstevel@tonic-gate 
1012*0Sstevel@tonic-gate 			BIO_printf(bio,"---\nCiphers common between both SSL endpoints:\n");
1013*0Sstevel@tonic-gate 			j=i=0;
1014*0Sstevel@tonic-gate 			while (*p)
1015*0Sstevel@tonic-gate 				{
1016*0Sstevel@tonic-gate 				if (*p == ':')
1017*0Sstevel@tonic-gate 					{
1018*0Sstevel@tonic-gate 					BIO_write(bio,space,15-j%25);
1019*0Sstevel@tonic-gate 					i++;
1020*0Sstevel@tonic-gate 					j=0;
1021*0Sstevel@tonic-gate 					BIO_write(bio,((i%3)?" ":"\n"),1);
1022*0Sstevel@tonic-gate 					}
1023*0Sstevel@tonic-gate 				else
1024*0Sstevel@tonic-gate 					{
1025*0Sstevel@tonic-gate 					BIO_write(bio,p,1);
1026*0Sstevel@tonic-gate 					j++;
1027*0Sstevel@tonic-gate 					}
1028*0Sstevel@tonic-gate 				p++;
1029*0Sstevel@tonic-gate 				}
1030*0Sstevel@tonic-gate 			BIO_write(bio,"\n",1);
1031*0Sstevel@tonic-gate 			}
1032*0Sstevel@tonic-gate 
1033*0Sstevel@tonic-gate 		BIO_printf(bio,"---\nSSL handshake has read %ld bytes and written %ld bytes\n",
1034*0Sstevel@tonic-gate 			BIO_number_read(SSL_get_rbio(s)),
1035*0Sstevel@tonic-gate 			BIO_number_written(SSL_get_wbio(s)));
1036*0Sstevel@tonic-gate 		}
1037*0Sstevel@tonic-gate 	BIO_printf(bio,((s->hit)?"---\nReused, ":"---\nNew, "));
1038*0Sstevel@tonic-gate 	c=SSL_get_current_cipher(s);
1039*0Sstevel@tonic-gate 	BIO_printf(bio,"%s, Cipher is %s\n",
1040*0Sstevel@tonic-gate 		SSL_CIPHER_get_version(c),
1041*0Sstevel@tonic-gate 		SSL_CIPHER_get_name(c));
1042*0Sstevel@tonic-gate 	if (peer != NULL) {
1043*0Sstevel@tonic-gate 		EVP_PKEY *pktmp;
1044*0Sstevel@tonic-gate 		pktmp = X509_get_pubkey(peer);
1045*0Sstevel@tonic-gate 		BIO_printf(bio,"Server public key is %d bit\n",
1046*0Sstevel@tonic-gate 							 EVP_PKEY_bits(pktmp));
1047*0Sstevel@tonic-gate 		EVP_PKEY_free(pktmp);
1048*0Sstevel@tonic-gate 	}
1049*0Sstevel@tonic-gate 	SSL_SESSION_print(bio,SSL_get_session(s));
1050*0Sstevel@tonic-gate 	BIO_printf(bio,"---\n");
1051*0Sstevel@tonic-gate 	if (peer != NULL)
1052*0Sstevel@tonic-gate 		X509_free(peer);
1053*0Sstevel@tonic-gate 	/* flush, or debugging output gets mixed with http response */
1054*0Sstevel@tonic-gate 	BIO_flush(bio);
1055*0Sstevel@tonic-gate 	}
1056*0Sstevel@tonic-gate 
1057