1/* apps/s_client.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to.  The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 *    notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 *    notice, this list of conditions and the following disclaimer in the
30 *    documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 *    must display the following acknowledgement:
33 *    "This product includes cryptographic software written by
34 *     Eric Young (eay@cryptsoft.com)"
35 *    The word 'cryptographic' can be left out if the rouines from the library
36 *    being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 *    the apps directory (application code) you must include an acknowledgement:
39 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed.  i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58/* ====================================================================
59 * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 *
65 * 1. Redistributions of source code must retain the above copyright
66 *    notice, this list of conditions and the following disclaimer.
67 *
68 * 2. Redistributions in binary form must reproduce the above copyright
69 *    notice, this list of conditions and the following disclaimer in
70 *    the documentation and/or other materials provided with the
71 *    distribution.
72 *
73 * 3. All advertising materials mentioning features or use of this
74 *    software must display the following acknowledgment:
75 *    "This product includes software developed by the OpenSSL Project
76 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 *
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 *    endorse or promote products derived from this software without
80 *    prior written permission. For written permission, please contact
81 *    openssl-core@openssl.org.
82 *
83 * 5. Products derived from this software may not be called "OpenSSL"
84 *    nor may "OpenSSL" appear in their names without prior written
85 *    permission of the OpenSSL Project.
86 *
87 * 6. Redistributions of any form whatsoever must retain the following
88 *    acknowledgment:
89 *    "This product includes software developed by the OpenSSL Project
90 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 *
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
105 *
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com).  This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
109 *
110 */
111/* ====================================================================
112 * Copyright 2005 Nokia. All rights reserved.
113 *
114 * The portions of the attached software ("Contribution") is developed by
115 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
116 * license.
117 *
118 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
119 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
120 * support (see RFC 4279) to OpenSSL.
121 *
122 * No patent licenses or other rights except those expressly stated in
123 * the OpenSSL open source license shall be deemed granted or received
124 * expressly, by implication, estoppel, or otherwise.
125 *
126 * No assurances are provided by Nokia that the Contribution does not
127 * infringe the patent or other intellectual property rights of any third
128 * party or that the license provides you with all the necessary rights
129 * to make use of the Contribution.
130 *
131 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
132 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
133 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
134 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
135 * OTHERWISE.
136 */
137
138#include <assert.h>
139#include <ctype.h>
140#include <stdio.h>
141#include <stdlib.h>
142#include <string.h>
143#include <openssl/e_os2.h>
144#ifdef OPENSSL_NO_STDIO
145#define APPS_WIN16
146#endif
147
148/* With IPv6, it looks like Digital has mixed up the proper order of
149   recursive header file inclusion, resulting in the compiler complaining
150   that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which
151   is needed to have fileno() declared correctly...  So let's define u_int */
152#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
153#define __U_INT
154typedef unsigned int u_int;
155#endif
156
157#define USE_SOCKETS
158#include "apps.h"
159#include <openssl/x509.h>
160#include <openssl/ssl.h>
161#include <openssl/err.h>
162#include <openssl/pem.h>
163#include <openssl/rand.h>
164#include <openssl/ocsp.h>
165#include <openssl/bn.h>
166#include "s_apps.h"
167#include "timeouts.h"
168
169#if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
170/* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
171#undef FIONBIO
172#endif
173
174#if defined(OPENSSL_SYS_BEOS_R5)
175#include <fcntl.h>
176#endif
177
178#undef PROG
179#define PROG	s_client_main
180
181/*#define SSL_HOST_NAME	"www.netscape.com" */
182/*#define SSL_HOST_NAME	"193.118.187.102" */
183#define SSL_HOST_NAME	"localhost"
184
185/*#define TEST_CERT "client.pem" */ /* no default cert. */
186
187#undef BUFSIZZ
188#define BUFSIZZ 1024*8
189
190extern int verify_depth;
191extern int verify_error;
192extern int verify_return_error;
193
194#ifdef FIONBIO
195static int c_nbio=0;
196#endif
197static int c_Pause=0;
198static int c_debug=0;
199#ifndef OPENSSL_NO_TLSEXT
200static int c_tlsextdebug=0;
201static int c_status_req=0;
202#endif
203static int c_msg=0;
204static int c_showcerts=0;
205
206static void sc_usage(void);
207static void print_stuff(BIO *berr,SSL *con,int full);
208#ifndef OPENSSL_NO_TLSEXT
209static int ocsp_resp_cb(SSL *s, void *arg);
210#endif
211static BIO *bio_c_out=NULL;
212static int c_quiet=0;
213static int c_ign_eof=0;
214
215#ifndef OPENSSL_NO_PSK
216/* Default PSK identity and key */
217static char *psk_identity="Client_identity";
218/*char *psk_key=NULL;  by default PSK is not used */
219
220static unsigned int psk_client_cb(SSL *ssl, const char *hint, char *identity,
221	unsigned int max_identity_len, unsigned char *psk,
222	unsigned int max_psk_len)
223	{
224	unsigned int psk_len = 0;
225	int ret;
226        BIGNUM *bn=NULL;
227
228	if (c_debug)
229		BIO_printf(bio_c_out, "psk_client_cb\n");
230	if (!hint)
231                {
232                /* no ServerKeyExchange message*/
233		if (c_debug)
234			BIO_printf(bio_c_out,"NULL received PSK identity hint, continuing anyway\n");
235                }
236        else if (c_debug)
237		BIO_printf(bio_c_out, "Received PSK identity hint '%s'\n", hint);
238
239	/* lookup PSK identity and PSK key based on the given identity hint here */
240	ret = BIO_snprintf(identity, max_identity_len, "%s", psk_identity);
241	if (ret < 0 || (unsigned int)ret > max_identity_len)
242		goto out_err;
243	if (c_debug)
244		BIO_printf(bio_c_out, "created identity '%s' len=%d\n", identity, ret);
245        ret=BN_hex2bn(&bn, psk_key);
246        if (!ret)
247                {
248                BIO_printf(bio_err,"Could not convert PSK key '%s' to BIGNUM\n", psk_key);
249                if (bn)
250                        BN_free(bn);
251                return 0;
252                }
253
254        if ((unsigned int)BN_num_bytes(bn) > max_psk_len)
255                {
256                BIO_printf(bio_err,"psk buffer of callback is too small (%d) for key (%d)\n",
257                        max_psk_len, BN_num_bytes(bn));
258                BN_free(bn);
259                return 0;
260                }
261
262        psk_len=BN_bn2bin(bn, psk);
263        BN_free(bn);
264        if (psk_len == 0)
265                goto out_err;
266
267	if (c_debug)
268		BIO_printf(bio_c_out, "created PSK len=%d\n", psk_len);
269
270        return psk_len;
271 out_err:
272	if (c_debug)
273		BIO_printf(bio_err, "Error in PSK client callback\n");
274        return 0;
275	}
276#endif
277
278static void sc_usage(void)
279	{
280	BIO_printf(bio_err,"usage: s_client args\n");
281	BIO_printf(bio_err,"\n");
282	BIO_printf(bio_err," -host host     - use -connect instead\n");
283	BIO_printf(bio_err," -port port     - use -connect instead\n");
284	BIO_printf(bio_err," -connect host:port - who to connect to (default is %s:%s)\n",SSL_HOST_NAME,PORT_STR);
285
286	BIO_printf(bio_err," -verify arg   - turn on peer certificate verification\n");
287	BIO_printf(bio_err," -cert arg     - certificate file to use, PEM format assumed\n");
288	BIO_printf(bio_err," -certform arg - certificate format (PEM or DER) PEM default\n");
289	BIO_printf(bio_err," -key arg      - Private key file to use, in cert file if\n");
290	BIO_printf(bio_err,"                 not specified but cert file is.\n");
291	BIO_printf(bio_err," -keyform arg  - key format (PEM or DER) PEM default\n");
292	BIO_printf(bio_err," -pass arg     - private key file pass phrase source\n");
293	BIO_printf(bio_err," -CApath arg   - PEM format directory of CA's\n");
294	BIO_printf(bio_err," -CAfile arg   - PEM format file of CA's\n");
295	BIO_printf(bio_err," -reconnect    - Drop and re-make the connection with the same Session-ID\n");
296	BIO_printf(bio_err," -pause        - sleep(1) after each read(2) and write(2) system call\n");
297	BIO_printf(bio_err," -showcerts    - show all certificates in the chain\n");
298	BIO_printf(bio_err," -debug        - extra output\n");
299#ifdef WATT32
300	BIO_printf(bio_err," -wdebug       - WATT-32 tcp debugging\n");
301#endif
302	BIO_printf(bio_err," -msg          - Show protocol messages\n");
303	BIO_printf(bio_err," -nbio_test    - more ssl protocol testing\n");
304	BIO_printf(bio_err," -state        - print the 'ssl' states\n");
305#ifdef FIONBIO
306	BIO_printf(bio_err," -nbio         - Run with non-blocking IO\n");
307#endif
308	BIO_printf(bio_err," -crlf         - convert LF from terminal into CRLF\n");
309	BIO_printf(bio_err," -quiet        - no s_client output\n");
310	BIO_printf(bio_err," -ign_eof      - ignore input eof (default when -quiet)\n");
311	BIO_printf(bio_err," -no_ign_eof   - don't ignore input eof\n");
312#ifndef OPENSSL_NO_PSK
313	BIO_printf(bio_err," -psk_identity arg - PSK identity\n");
314	BIO_printf(bio_err," -psk arg      - PSK in hex (without 0x)\n");
315# ifndef OPENSSL_NO_JPAKE
316	BIO_printf(bio_err," -jpake arg    - JPAKE secret to use\n");
317# endif
318#endif
319	BIO_printf(bio_err," -ssl2         - just use SSLv2\n");
320	BIO_printf(bio_err," -ssl3         - just use SSLv3\n");
321	BIO_printf(bio_err," -tls1         - just use TLSv1\n");
322	BIO_printf(bio_err," -dtls1        - just use DTLSv1\n");
323	BIO_printf(bio_err," -mtu          - set the link layer MTU\n");
324	BIO_printf(bio_err," -no_tls1/-no_ssl3/-no_ssl2 - turn off that protocol\n");
325	BIO_printf(bio_err," -bugs         - Switch on all SSL implementation bug workarounds\n");
326	BIO_printf(bio_err," -serverpref   - Use server's cipher preferences (only SSLv2)\n");
327	BIO_printf(bio_err," -cipher       - preferred cipher to use, use the 'openssl ciphers'\n");
328	BIO_printf(bio_err,"                 command to see what is available\n");
329	BIO_printf(bio_err," -starttls prot - use the STARTTLS command before starting TLS\n");
330	BIO_printf(bio_err,"                 for those protocols that support it, where\n");
331	BIO_printf(bio_err,"                 'prot' defines which one to assume.  Currently,\n");
332	BIO_printf(bio_err,"                 only \"smtp\", \"pop3\", \"imap\", \"ftp\" and \"xmpp\"\n");
333	BIO_printf(bio_err,"                 are supported.\n");
334#ifndef OPENSSL_NO_ENGINE
335	BIO_printf(bio_err," -engine id    - Initialise and use the specified engine\n");
336#endif
337	BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
338	BIO_printf(bio_err," -sess_out arg - file to write SSL session to\n");
339	BIO_printf(bio_err," -sess_in arg  - file to read SSL session from\n");
340#ifndef OPENSSL_NO_TLSEXT
341	BIO_printf(bio_err," -servername host  - Set TLS extension servername in ClientHello\n");
342	BIO_printf(bio_err," -tlsextdebug      - hex dump of all TLS extensions received\n");
343	BIO_printf(bio_err," -status           - request certificate status from server\n");
344	BIO_printf(bio_err," -no_ticket        - disable use of RFC4507bis session tickets\n");
345# ifndef OPENSSL_NO_NEXTPROTONEG
346	BIO_printf(bio_err," -nextprotoneg arg - enable NPN extension, considering named protocols supported (comma-separated list)\n");
347# endif
348	BIO_printf(bio_err," -cutthrough       - enable 1-RTT full-handshake for strong ciphers\n");
349#endif
350	BIO_printf(bio_err," -legacy_renegotiation - enable use of legacy renegotiation (dangerous)\n");
351	}
352
353#ifndef OPENSSL_NO_TLSEXT
354
355/* This is a context that we pass to callbacks */
356typedef struct tlsextctx_st {
357   BIO * biodebug;
358   int ack;
359} tlsextctx;
360
361
362static int MS_CALLBACK ssl_servername_cb(SSL *s, int *ad, void *arg)
363	{
364	tlsextctx * p = (tlsextctx *) arg;
365	const char * hn= SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
366	if (SSL_get_servername_type(s) != -1)
367 	        p->ack = !SSL_session_reused(s) && hn != NULL;
368	else
369		BIO_printf(bio_err,"Can't use SSL_get_servername\n");
370
371	return SSL_TLSEXT_ERR_OK;
372	}
373
374# ifndef OPENSSL_NO_NEXTPROTONEG
375/* This the context that we pass to next_proto_cb */
376typedef struct tlsextnextprotoctx_st {
377	unsigned char *data;
378	unsigned short len;
379	int status;
380} tlsextnextprotoctx;
381
382static tlsextnextprotoctx next_proto;
383
384static int next_proto_cb(SSL *s, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)
385	{
386	tlsextnextprotoctx *ctx = arg;
387
388	if (!c_quiet)
389		{
390		/* We can assume that |in| is syntactically valid. */
391		unsigned i;
392		BIO_printf(bio_c_out, "Protocols advertised by server: ");
393		for (i = 0; i < inlen; )
394			{
395			if (i)
396				BIO_write(bio_c_out, ", ", 2);
397			BIO_write(bio_c_out, &in[i + 1], in[i]);
398			i += in[i] + 1;
399			}
400		BIO_write(bio_c_out, "\n", 1);
401		}
402
403	ctx->status = SSL_select_next_proto(out, outlen, in, inlen, ctx->data, ctx->len);
404	return SSL_TLSEXT_ERR_OK;
405	}
406# endif  /* ndef OPENSSL_NO_NEXTPROTONEG */
407#endif
408
409enum
410{
411	PROTO_OFF	= 0,
412	PROTO_SMTP,
413	PROTO_POP3,
414	PROTO_IMAP,
415	PROTO_FTP,
416	PROTO_XMPP
417};
418
419int MAIN(int, char **);
420
421int MAIN(int argc, char **argv)
422	{
423	unsigned int off=0, clr=0;
424	SSL *con=NULL;
425	int s,k,width,state=0;
426	char *cbuf=NULL,*sbuf=NULL,*mbuf=NULL;
427	int cbuf_len,cbuf_off;
428	int sbuf_len,sbuf_off;
429	fd_set readfds,writefds;
430	short port=PORT;
431	int full_log=1;
432	char *host=SSL_HOST_NAME;
433	char *cert_file=NULL,*key_file=NULL;
434	int cert_format = FORMAT_PEM, key_format = FORMAT_PEM;
435	char *passarg = NULL, *pass = NULL;
436	X509 *cert = NULL;
437	EVP_PKEY *key = NULL;
438	char *CApath=NULL,*CAfile=NULL,*cipher=NULL;
439	int reconnect=0,badop=0,verify=SSL_VERIFY_NONE,bugs=0;
440	int cutthrough=0;
441	int crlf=0;
442	int write_tty,read_tty,write_ssl,read_ssl,tty_on,ssl_pending;
443	SSL_CTX *ctx=NULL;
444	int ret=1,in_init=1,i,nbio_test=0;
445	int starttls_proto = PROTO_OFF;
446	int prexit = 0;
447	X509_VERIFY_PARAM *vpm = NULL;
448	int badarg = 0;
449	const SSL_METHOD *meth=NULL;
450	int socket_type=SOCK_STREAM;
451	BIO *sbio;
452	char *inrand=NULL;
453	int mbuf_len=0;
454	struct timeval timeout, *timeoutp;
455#ifndef OPENSSL_NO_ENGINE
456	char *engine_id=NULL;
457	char *ssl_client_engine_id=NULL;
458	ENGINE *ssl_client_engine=NULL;
459#endif
460	ENGINE *e=NULL;
461#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS_R5)
462	struct timeval tv;
463#if defined(OPENSSL_SYS_BEOS_R5)
464	int stdin_set = 0;
465#endif
466#endif
467#ifndef OPENSSL_NO_TLSEXT
468	char *servername = NULL;
469        tlsextctx tlsextcbp =
470        {NULL,0};
471# ifndef OPENSSL_NO_NEXTPROTONEG
472	const char *next_proto_neg_in = NULL;
473# endif
474#endif
475	char *sess_in = NULL;
476	char *sess_out = NULL;
477	struct sockaddr peer;
478	int peerlen = sizeof(peer);
479	int enable_timeouts = 0 ;
480	long socket_mtu = 0;
481#ifndef OPENSSL_NO_JPAKE
482	char *jpake_secret = NULL;
483#endif
484
485#if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
486	meth=SSLv23_client_method();
487#elif !defined(OPENSSL_NO_SSL3)
488	meth=SSLv3_client_method();
489#elif !defined(OPENSSL_NO_SSL2)
490	meth=SSLv2_client_method();
491#endif
492
493	apps_startup();
494	c_Pause=0;
495	c_quiet=0;
496	c_ign_eof=0;
497	c_debug=0;
498	c_msg=0;
499	c_showcerts=0;
500
501	if (bio_err == NULL)
502		bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
503
504	if (!load_config(bio_err, NULL))
505		goto end;
506
507	if (	((cbuf=OPENSSL_malloc(BUFSIZZ)) == NULL) ||
508		((sbuf=OPENSSL_malloc(BUFSIZZ)) == NULL) ||
509		((mbuf=OPENSSL_malloc(BUFSIZZ)) == NULL))
510		{
511		BIO_printf(bio_err,"out of memory\n");
512		goto end;
513		}
514
515	verify_depth=0;
516	verify_error=X509_V_OK;
517#ifdef FIONBIO
518	c_nbio=0;
519#endif
520
521	argc--;
522	argv++;
523	while (argc >= 1)
524		{
525		if	(strcmp(*argv,"-host") == 0)
526			{
527			if (--argc < 1) goto bad;
528			host= *(++argv);
529			}
530		else if	(strcmp(*argv,"-port") == 0)
531			{
532			if (--argc < 1) goto bad;
533			port=atoi(*(++argv));
534			if (port == 0) goto bad;
535			}
536		else if (strcmp(*argv,"-connect") == 0)
537			{
538			if (--argc < 1) goto bad;
539			if (!extract_host_port(*(++argv),&host,NULL,&port))
540				goto bad;
541			}
542		else if	(strcmp(*argv,"-verify") == 0)
543			{
544			verify=SSL_VERIFY_PEER;
545			if (--argc < 1) goto bad;
546			verify_depth=atoi(*(++argv));
547			BIO_printf(bio_err,"verify depth is %d\n",verify_depth);
548			}
549		else if	(strcmp(*argv,"-cert") == 0)
550			{
551			if (--argc < 1) goto bad;
552			cert_file= *(++argv);
553			}
554		else if	(strcmp(*argv,"-sess_out") == 0)
555			{
556			if (--argc < 1) goto bad;
557			sess_out = *(++argv);
558			}
559		else if	(strcmp(*argv,"-sess_in") == 0)
560			{
561			if (--argc < 1) goto bad;
562			sess_in = *(++argv);
563			}
564		else if	(strcmp(*argv,"-certform") == 0)
565			{
566			if (--argc < 1) goto bad;
567			cert_format = str2fmt(*(++argv));
568			}
569		else if (args_verify(&argv, &argc, &badarg, bio_err, &vpm))
570			{
571			if (badarg)
572				goto bad;
573			continue;
574			}
575		else if (strcmp(*argv,"-verify_return_error") == 0)
576			verify_return_error = 1;
577		else if	(strcmp(*argv,"-prexit") == 0)
578			prexit=1;
579		else if	(strcmp(*argv,"-crlf") == 0)
580			crlf=1;
581		else if	(strcmp(*argv,"-quiet") == 0)
582			{
583			c_quiet=1;
584			c_ign_eof=1;
585			}
586		else if	(strcmp(*argv,"-ign_eof") == 0)
587			c_ign_eof=1;
588		else if	(strcmp(*argv,"-no_ign_eof") == 0)
589			c_ign_eof=0;
590		else if	(strcmp(*argv,"-pause") == 0)
591			c_Pause=1;
592		else if	(strcmp(*argv,"-debug") == 0)
593			c_debug=1;
594#ifndef OPENSSL_NO_TLSEXT
595		else if	(strcmp(*argv,"-tlsextdebug") == 0)
596			c_tlsextdebug=1;
597		else if	(strcmp(*argv,"-status") == 0)
598			c_status_req=1;
599#endif
600#ifdef WATT32
601		else if (strcmp(*argv,"-wdebug") == 0)
602			dbug_init();
603#endif
604		else if	(strcmp(*argv,"-msg") == 0)
605			c_msg=1;
606		else if	(strcmp(*argv,"-showcerts") == 0)
607			c_showcerts=1;
608		else if	(strcmp(*argv,"-nbio_test") == 0)
609			nbio_test=1;
610		else if	(strcmp(*argv,"-state") == 0)
611			state=1;
612#ifndef OPENSSL_NO_PSK
613                else if (strcmp(*argv,"-psk_identity") == 0)
614			{
615			if (--argc < 1) goto bad;
616			psk_identity=*(++argv);
617			}
618                else if (strcmp(*argv,"-psk") == 0)
619			{
620                        size_t j;
621
622			if (--argc < 1) goto bad;
623			psk_key=*(++argv);
624			for (j = 0; j < strlen(psk_key); j++)
625                                {
626                                if (isxdigit((int)psk_key[j]))
627                                        continue;
628                                BIO_printf(bio_err,"Not a hex number '%s'\n",*argv);
629                                goto bad;
630                                }
631			}
632#endif
633#ifndef OPENSSL_NO_SSL2
634		else if	(strcmp(*argv,"-ssl2") == 0)
635			meth=SSLv2_client_method();
636#endif
637#ifndef OPENSSL_NO_SSL3
638		else if	(strcmp(*argv,"-ssl3") == 0)
639			meth=SSLv3_client_method();
640#endif
641#ifndef OPENSSL_NO_TLS1
642		else if	(strcmp(*argv,"-tls1") == 0)
643			meth=TLSv1_client_method();
644#endif
645#ifndef OPENSSL_NO_DTLS1
646		else if	(strcmp(*argv,"-dtls1") == 0)
647			{
648			meth=DTLSv1_client_method();
649			socket_type=SOCK_DGRAM;
650			}
651		else if (strcmp(*argv,"-timeout") == 0)
652			enable_timeouts=1;
653		else if (strcmp(*argv,"-mtu") == 0)
654			{
655			if (--argc < 1) goto bad;
656			socket_mtu = atol(*(++argv));
657			}
658#endif
659		else if (strcmp(*argv,"-bugs") == 0)
660			bugs=1;
661		else if	(strcmp(*argv,"-keyform") == 0)
662			{
663			if (--argc < 1) goto bad;
664			key_format = str2fmt(*(++argv));
665			}
666		else if	(strcmp(*argv,"-pass") == 0)
667			{
668			if (--argc < 1) goto bad;
669			passarg = *(++argv);
670			}
671		else if	(strcmp(*argv,"-key") == 0)
672			{
673			if (--argc < 1) goto bad;
674			key_file= *(++argv);
675			}
676		else if	(strcmp(*argv,"-reconnect") == 0)
677			{
678			reconnect=5;
679			}
680		else if	(strcmp(*argv,"-CApath") == 0)
681			{
682			if (--argc < 1) goto bad;
683			CApath= *(++argv);
684			}
685		else if	(strcmp(*argv,"-CAfile") == 0)
686			{
687			if (--argc < 1) goto bad;
688			CAfile= *(++argv);
689			}
690		else if (strcmp(*argv,"-no_tls1") == 0)
691			off|=SSL_OP_NO_TLSv1;
692		else if (strcmp(*argv,"-no_ssl3") == 0)
693			off|=SSL_OP_NO_SSLv3;
694		else if (strcmp(*argv,"-no_ssl2") == 0)
695			off|=SSL_OP_NO_SSLv2;
696		else if	(strcmp(*argv,"-no_comp") == 0)
697			{ off|=SSL_OP_NO_COMPRESSION; }
698#ifndef OPENSSL_NO_TLSEXT
699		else if	(strcmp(*argv,"-no_ticket") == 0)
700			{ off|=SSL_OP_NO_TICKET; }
701# ifndef OPENSSL_NO_NEXTPROTONEG
702		else if (strcmp(*argv,"-nextprotoneg") == 0)
703			{
704			if (--argc < 1) goto bad;
705			next_proto_neg_in = *(++argv);
706			}
707# endif
708#endif
709		else if (strcmp(*argv,"-cutthrough") == 0)
710			cutthrough=1;
711		else if (strcmp(*argv,"-serverpref") == 0)
712			off|=SSL_OP_CIPHER_SERVER_PREFERENCE;
713		else if (strcmp(*argv,"-legacy_renegotiation") == 0)
714			off|=SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION;
715		else if	(strcmp(*argv,"-legacy_server_connect") == 0)
716			{ off|=SSL_OP_LEGACY_SERVER_CONNECT; }
717		else if	(strcmp(*argv,"-no_legacy_server_connect") == 0)
718			{ clr|=SSL_OP_LEGACY_SERVER_CONNECT; }
719		else if	(strcmp(*argv,"-cipher") == 0)
720			{
721			if (--argc < 1) goto bad;
722			cipher= *(++argv);
723			}
724#ifdef FIONBIO
725		else if (strcmp(*argv,"-nbio") == 0)
726			{ c_nbio=1; }
727#endif
728		else if	(strcmp(*argv,"-starttls") == 0)
729			{
730			if (--argc < 1) goto bad;
731			++argv;
732			if (strcmp(*argv,"smtp") == 0)
733				starttls_proto = PROTO_SMTP;
734			else if (strcmp(*argv,"pop3") == 0)
735				starttls_proto = PROTO_POP3;
736			else if (strcmp(*argv,"imap") == 0)
737				starttls_proto = PROTO_IMAP;
738			else if (strcmp(*argv,"ftp") == 0)
739				starttls_proto = PROTO_FTP;
740			else if (strcmp(*argv, "xmpp") == 0)
741				starttls_proto = PROTO_XMPP;
742			else
743				goto bad;
744			}
745#ifndef OPENSSL_NO_ENGINE
746		else if	(strcmp(*argv,"-engine") == 0)
747			{
748			if (--argc < 1) goto bad;
749			engine_id = *(++argv);
750			}
751		else if	(strcmp(*argv,"-ssl_client_engine") == 0)
752			{
753			if (--argc < 1) goto bad;
754			ssl_client_engine_id = *(++argv);
755			}
756#endif
757		else if (strcmp(*argv,"-rand") == 0)
758			{
759			if (--argc < 1) goto bad;
760			inrand= *(++argv);
761			}
762#ifndef OPENSSL_NO_TLSEXT
763		else if (strcmp(*argv,"-servername") == 0)
764			{
765			if (--argc < 1) goto bad;
766			servername= *(++argv);
767			/* meth=TLSv1_client_method(); */
768			}
769#endif
770#ifndef OPENSSL_NO_JPAKE
771		else if (strcmp(*argv,"-jpake") == 0)
772			{
773			if (--argc < 1) goto bad;
774			jpake_secret = *++argv;
775			}
776#endif
777		else
778			{
779			BIO_printf(bio_err,"unknown option %s\n",*argv);
780			badop=1;
781			break;
782			}
783		argc--;
784		argv++;
785		}
786	if (badop)
787		{
788bad:
789		sc_usage();
790		goto end;
791		}
792
793#if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK)
794	if (jpake_secret)
795		{
796		if (psk_key)
797			{
798			BIO_printf(bio_err,
799				   "Can't use JPAKE and PSK together\n");
800			goto end;
801			}
802		psk_identity = "JPAKE";
803		}
804
805	if (cipher)
806		{
807		BIO_printf(bio_err, "JPAKE sets cipher to PSK\n");
808		goto end;
809		}
810	cipher = "PSK";
811#endif
812
813	OpenSSL_add_ssl_algorithms();
814	SSL_load_error_strings();
815
816#if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
817	next_proto.status = -1;
818	if (next_proto_neg_in)
819		{
820		next_proto.data = next_protos_parse(&next_proto.len, next_proto_neg_in);
821		if (next_proto.data == NULL)
822			{
823			BIO_printf(bio_err, "Error parsing -nextprotoneg argument\n");
824			goto end;
825			}
826		}
827	else
828		next_proto.data = NULL;
829#endif
830
831#ifndef OPENSSL_NO_ENGINE
832        e = setup_engine(bio_err, engine_id, 1);
833	if (ssl_client_engine_id)
834		{
835		ssl_client_engine = ENGINE_by_id(ssl_client_engine_id);
836		if (!ssl_client_engine)
837			{
838			BIO_printf(bio_err,
839					"Error getting client auth engine\n");
840			goto end;
841			}
842		}
843
844#endif
845	if (!app_passwd(bio_err, passarg, NULL, &pass, NULL))
846		{
847		BIO_printf(bio_err, "Error getting password\n");
848		goto end;
849		}
850
851	if (key_file == NULL)
852		key_file = cert_file;
853
854
855	if (key_file)
856
857		{
858
859		key = load_key(bio_err, key_file, key_format, 0, pass, e,
860			       "client certificate private key file");
861		if (!key)
862			{
863			ERR_print_errors(bio_err);
864			goto end;
865			}
866
867		}
868
869	if (cert_file)
870
871		{
872		cert = load_cert(bio_err,cert_file,cert_format,
873				NULL, e, "client certificate file");
874
875		if (!cert)
876			{
877			ERR_print_errors(bio_err);
878			goto end;
879			}
880		}
881
882	if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL
883		&& !RAND_status())
884		{
885		BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
886		}
887	if (inrand != NULL)
888		BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
889			app_RAND_load_files(inrand));
890
891	if (bio_c_out == NULL)
892		{
893		if (c_quiet && !c_debug && !c_msg)
894			{
895			bio_c_out=BIO_new(BIO_s_null());
896			}
897		else
898			{
899			if (bio_c_out == NULL)
900				bio_c_out=BIO_new_fp(stdout,BIO_NOCLOSE);
901			}
902		}
903
904	ctx=SSL_CTX_new(meth);
905	if (ctx == NULL)
906		{
907		ERR_print_errors(bio_err);
908		goto end;
909		}
910
911	if (vpm)
912		SSL_CTX_set1_param(ctx, vpm);
913
914#ifndef OPENSSL_NO_ENGINE
915	if (ssl_client_engine)
916		{
917		if (!SSL_CTX_set_client_cert_engine(ctx, ssl_client_engine))
918			{
919			BIO_puts(bio_err, "Error setting client auth engine\n");
920			ERR_print_errors(bio_err);
921			ENGINE_free(ssl_client_engine);
922			goto end;
923			}
924		ENGINE_free(ssl_client_engine);
925		}
926#endif
927
928#ifndef OPENSSL_NO_PSK
929#ifdef OPENSSL_NO_JPAKE
930	if (psk_key != NULL)
931#else
932	if (psk_key != NULL || jpake_secret)
933#endif
934		{
935		if (c_debug)
936			BIO_printf(bio_c_out, "PSK key given or JPAKE in use, setting client callback\n");
937		SSL_CTX_set_psk_client_callback(ctx, psk_client_cb);
938		}
939#endif
940	if (bugs)
941		SSL_CTX_set_options(ctx,SSL_OP_ALL|off);
942	else
943		SSL_CTX_set_options(ctx,off);
944
945	if (clr)
946		SSL_CTX_clear_options(ctx, clr);
947	/* DTLS: partial reads end up discarding unread UDP bytes :-(
948	 * Setting read ahead solves this problem.
949	 */
950	if (socket_type == SOCK_DGRAM) SSL_CTX_set_read_ahead(ctx, 1);
951
952	/* Enable handshake cutthrough for client connections using
953	 * strong ciphers. */
954	if (cutthrough)
955		{
956		int ssl_mode = SSL_CTX_get_mode(ctx);
957		ssl_mode |= SSL_MODE_HANDSHAKE_CUTTHROUGH;
958		SSL_CTX_set_mode(ctx, ssl_mode);
959		}
960
961#if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
962	if (next_proto.data)
963		SSL_CTX_set_next_proto_select_cb(ctx, next_proto_cb, &next_proto);
964#endif
965
966	if (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback);
967	if (cipher != NULL)
968		if(!SSL_CTX_set_cipher_list(ctx,cipher)) {
969		BIO_printf(bio_err,"error setting cipher list\n");
970		ERR_print_errors(bio_err);
971		goto end;
972	}
973#if 0
974	else
975		SSL_CTX_set_cipher_list(ctx,getenv("SSL_CIPHER"));
976#endif
977
978	SSL_CTX_set_verify(ctx,verify,verify_callback);
979	if (!set_cert_key_stuff(ctx,cert,key))
980		goto end;
981
982	if ((!SSL_CTX_load_verify_locations(ctx,CAfile,CApath)) ||
983		(!SSL_CTX_set_default_verify_paths(ctx)))
984		{
985		/* BIO_printf(bio_err,"error setting default verify locations\n"); */
986		ERR_print_errors(bio_err);
987		/* goto end; */
988		}
989
990#ifndef OPENSSL_NO_TLSEXT
991	if (servername != NULL)
992		{
993		tlsextcbp.biodebug = bio_err;
994		SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
995		SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
996		}
997#endif
998
999	con=SSL_new(ctx);
1000	if (sess_in)
1001		{
1002		SSL_SESSION *sess;
1003		BIO *stmp = BIO_new_file(sess_in, "r");
1004		if (!stmp)
1005			{
1006			BIO_printf(bio_err, "Can't open session file %s\n",
1007						sess_in);
1008			ERR_print_errors(bio_err);
1009			goto end;
1010			}
1011		sess = PEM_read_bio_SSL_SESSION(stmp, NULL, 0, NULL);
1012		BIO_free(stmp);
1013		if (!sess)
1014			{
1015			BIO_printf(bio_err, "Can't open session file %s\n",
1016						sess_in);
1017			ERR_print_errors(bio_err);
1018			goto end;
1019			}
1020		SSL_set_session(con, sess);
1021		SSL_SESSION_free(sess);
1022		}
1023#ifndef OPENSSL_NO_TLSEXT
1024	if (servername != NULL)
1025		{
1026		if (!SSL_set_tlsext_host_name(con,servername))
1027			{
1028			BIO_printf(bio_err,"Unable to set TLS servername extension.\n");
1029			ERR_print_errors(bio_err);
1030			goto end;
1031			}
1032		}
1033#endif
1034#ifndef OPENSSL_NO_KRB5
1035	if (con  &&  (con->kssl_ctx = kssl_ctx_new()) != NULL)
1036                {
1037                kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVER, host);
1038		}
1039#endif	/* OPENSSL_NO_KRB5  */
1040/*	SSL_set_cipher_list(con,"RC4-MD5"); */
1041#if 0
1042#ifdef TLSEXT_TYPE_opaque_prf_input
1043	SSL_set_tlsext_opaque_prf_input(con, "Test client", 11);
1044#endif
1045#endif
1046
1047re_start:
1048
1049	if (init_client(&s,host,port,socket_type) == 0)
1050		{
1051		BIO_printf(bio_err,"connect:errno=%d\n",get_last_socket_error());
1052		SHUTDOWN(s);
1053		goto end;
1054		}
1055	BIO_printf(bio_c_out,"CONNECTED(%08X)\n",s);
1056
1057#ifdef FIONBIO
1058	if (c_nbio)
1059		{
1060		unsigned long l=1;
1061		BIO_printf(bio_c_out,"turning on non blocking io\n");
1062		if (BIO_socket_ioctl(s,FIONBIO,&l) < 0)
1063			{
1064			ERR_print_errors(bio_err);
1065			goto end;
1066			}
1067		}
1068#endif
1069	if (c_Pause & 0x01) con->debug=1;
1070
1071	if ( SSL_version(con) == DTLS1_VERSION)
1072		{
1073
1074		sbio=BIO_new_dgram(s,BIO_NOCLOSE);
1075		if (getsockname(s, &peer, (void *)&peerlen) < 0)
1076			{
1077			BIO_printf(bio_err, "getsockname:errno=%d\n",
1078				get_last_socket_error());
1079			SHUTDOWN(s);
1080			goto end;
1081			}
1082
1083		(void)BIO_ctrl_set_connected(sbio, 1, &peer);
1084
1085		if (enable_timeouts)
1086			{
1087			timeout.tv_sec = 0;
1088			timeout.tv_usec = DGRAM_RCV_TIMEOUT;
1089			BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
1090
1091			timeout.tv_sec = 0;
1092			timeout.tv_usec = DGRAM_SND_TIMEOUT;
1093			BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
1094			}
1095
1096		if (socket_mtu > 28)
1097			{
1098			SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
1099			SSL_set_mtu(con, socket_mtu - 28);
1100			}
1101		else
1102			/* want to do MTU discovery */
1103			BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
1104		}
1105	else
1106		sbio=BIO_new_socket(s,BIO_NOCLOSE);
1107
1108	if (nbio_test)
1109		{
1110		BIO *test;
1111
1112		test=BIO_new(BIO_f_nbio_test());
1113		sbio=BIO_push(test,sbio);
1114		}
1115
1116	if (c_debug)
1117		{
1118		con->debug=1;
1119		BIO_set_callback(sbio,bio_dump_callback);
1120		BIO_set_callback_arg(sbio,(char *)bio_c_out);
1121		}
1122	if (c_msg)
1123		{
1124		SSL_set_msg_callback(con, msg_cb);
1125		SSL_set_msg_callback_arg(con, bio_c_out);
1126		}
1127#ifndef OPENSSL_NO_TLSEXT
1128	if (c_tlsextdebug)
1129		{
1130		SSL_set_tlsext_debug_callback(con, tlsext_cb);
1131		SSL_set_tlsext_debug_arg(con, bio_c_out);
1132		}
1133	if (c_status_req)
1134		{
1135		SSL_set_tlsext_status_type(con, TLSEXT_STATUSTYPE_ocsp);
1136		SSL_CTX_set_tlsext_status_cb(ctx, ocsp_resp_cb);
1137		SSL_CTX_set_tlsext_status_arg(ctx, bio_c_out);
1138#if 0
1139{
1140STACK_OF(OCSP_RESPID) *ids = sk_OCSP_RESPID_new_null();
1141OCSP_RESPID *id = OCSP_RESPID_new();
1142id->value.byKey = ASN1_OCTET_STRING_new();
1143id->type = V_OCSP_RESPID_KEY;
1144ASN1_STRING_set(id->value.byKey, "Hello World", -1);
1145sk_OCSP_RESPID_push(ids, id);
1146SSL_set_tlsext_status_ids(con, ids);
1147}
1148#endif
1149		}
1150#endif
1151#ifndef OPENSSL_NO_JPAKE
1152	if (jpake_secret)
1153		jpake_client_auth(bio_c_out, sbio, jpake_secret);
1154#endif
1155
1156	SSL_set_bio(con,sbio,sbio);
1157	SSL_set_connect_state(con);
1158
1159	/* ok, lets connect */
1160	width=SSL_get_fd(con)+1;
1161
1162	read_tty=1;
1163	write_tty=0;
1164	tty_on=0;
1165	read_ssl=1;
1166	write_ssl=1;
1167
1168	cbuf_len=0;
1169	cbuf_off=0;
1170	sbuf_len=0;
1171	sbuf_off=0;
1172
1173	/* This is an ugly hack that does a lot of assumptions */
1174	/* We do have to handle multi-line responses which may come
1175 	   in a single packet or not. We therefore have to use
1176	   BIO_gets() which does need a buffering BIO. So during
1177	   the initial chitchat we do push a buffering BIO into the
1178	   chain that is removed again later on to not disturb the
1179	   rest of the s_client operation. */
1180	if (starttls_proto == PROTO_SMTP)
1181		{
1182		int foundit=0;
1183		BIO *fbio = BIO_new(BIO_f_buffer());
1184		BIO_push(fbio, sbio);
1185		/* wait for multi-line response to end from SMTP */
1186		do
1187			{
1188			mbuf_len = BIO_gets(fbio,mbuf,BUFSIZZ);
1189			}
1190		while (mbuf_len>3 && mbuf[3]=='-');
1191		/* STARTTLS command requires EHLO... */
1192		BIO_printf(fbio,"EHLO openssl.client.net\r\n");
1193		(void)BIO_flush(fbio);
1194		/* wait for multi-line response to end EHLO SMTP response */
1195		do
1196			{
1197			mbuf_len = BIO_gets(fbio,mbuf,BUFSIZZ);
1198			if (strstr(mbuf,"STARTTLS"))
1199				foundit=1;
1200			}
1201		while (mbuf_len>3 && mbuf[3]=='-');
1202		(void)BIO_flush(fbio);
1203		BIO_pop(fbio);
1204		BIO_free(fbio);
1205		if (!foundit)
1206			BIO_printf(bio_err,
1207				   "didn't found starttls in server response,"
1208				   " try anyway...\n");
1209		BIO_printf(sbio,"STARTTLS\r\n");
1210		BIO_read(sbio,sbuf,BUFSIZZ);
1211		}
1212	else if (starttls_proto == PROTO_POP3)
1213		{
1214		BIO_read(sbio,mbuf,BUFSIZZ);
1215		BIO_printf(sbio,"STLS\r\n");
1216		BIO_read(sbio,sbuf,BUFSIZZ);
1217		}
1218	else if (starttls_proto == PROTO_IMAP)
1219		{
1220		int foundit=0;
1221		BIO *fbio = BIO_new(BIO_f_buffer());
1222		BIO_push(fbio, sbio);
1223		BIO_gets(fbio,mbuf,BUFSIZZ);
1224		/* STARTTLS command requires CAPABILITY... */
1225		BIO_printf(fbio,". CAPABILITY\r\n");
1226		(void)BIO_flush(fbio);
1227		/* wait for multi-line CAPABILITY response */
1228		do
1229			{
1230			mbuf_len = BIO_gets(fbio,mbuf,BUFSIZZ);
1231			if (strstr(mbuf,"STARTTLS"))
1232				foundit=1;
1233			}
1234		while (mbuf_len>3 && mbuf[0]!='.');
1235		(void)BIO_flush(fbio);
1236		BIO_pop(fbio);
1237		BIO_free(fbio);
1238		if (!foundit)
1239			BIO_printf(bio_err,
1240				   "didn't found STARTTLS in server response,"
1241				   " try anyway...\n");
1242		BIO_printf(sbio,". STARTTLS\r\n");
1243		BIO_read(sbio,sbuf,BUFSIZZ);
1244		}
1245	else if (starttls_proto == PROTO_FTP)
1246		{
1247		BIO *fbio = BIO_new(BIO_f_buffer());
1248		BIO_push(fbio, sbio);
1249		/* wait for multi-line response to end from FTP */
1250		do
1251			{
1252			mbuf_len = BIO_gets(fbio,mbuf,BUFSIZZ);
1253			}
1254		while (mbuf_len>3 && mbuf[3]=='-');
1255		(void)BIO_flush(fbio);
1256		BIO_pop(fbio);
1257		BIO_free(fbio);
1258		BIO_printf(sbio,"AUTH TLS\r\n");
1259		BIO_read(sbio,sbuf,BUFSIZZ);
1260		}
1261	if (starttls_proto == PROTO_XMPP)
1262		{
1263		int seen = 0;
1264		BIO_printf(sbio,"<stream:stream "
1265		    "xmlns:stream='http://etherx.jabber.org/streams' "
1266		    "xmlns='jabber:client' to='%s' version='1.0'>", host);
1267		seen = BIO_read(sbio,mbuf,BUFSIZZ);
1268		mbuf[seen] = 0;
1269		while (!strstr(mbuf, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'"))
1270			{
1271			if (strstr(mbuf, "/stream:features>"))
1272				goto shut;
1273			seen = BIO_read(sbio,mbuf,BUFSIZZ);
1274			mbuf[seen] = 0;
1275			}
1276		BIO_printf(sbio, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
1277		seen = BIO_read(sbio,sbuf,BUFSIZZ);
1278		sbuf[seen] = 0;
1279		if (!strstr(sbuf, "<proceed"))
1280			goto shut;
1281		mbuf[0] = 0;
1282		}
1283
1284	for (;;)
1285		{
1286		FD_ZERO(&readfds);
1287		FD_ZERO(&writefds);
1288
1289		if ((SSL_version(con) == DTLS1_VERSION) &&
1290			DTLSv1_get_timeout(con, &timeout))
1291			timeoutp = &timeout;
1292		else
1293			timeoutp = NULL;
1294
1295		if (SSL_in_init(con) && !SSL_total_renegotiations(con))
1296			{
1297			in_init=1;
1298			tty_on=0;
1299			}
1300		else
1301			{
1302			tty_on=1;
1303			if (in_init)
1304				{
1305				in_init=0;
1306#if 0 /* This test doesn't really work as intended (needs to be fixed) */
1307#ifndef OPENSSL_NO_TLSEXT
1308				if (servername != NULL && !SSL_session_reused(con))
1309					{
1310					BIO_printf(bio_c_out,"Server did %sacknowledge servername extension.\n",tlsextcbp.ack?"":"not ");
1311					}
1312#endif
1313#endif
1314				if (sess_out)
1315					{
1316					BIO *stmp = BIO_new_file(sess_out, "w");
1317					if (stmp)
1318						{
1319						PEM_write_bio_SSL_SESSION(stmp, SSL_get_session(con));
1320						BIO_free(stmp);
1321						}
1322					else
1323						BIO_printf(bio_err, "Error writing session file %s\n", sess_out);
1324					}
1325				print_stuff(bio_c_out,con,full_log);
1326				if (full_log > 0) full_log--;
1327
1328				if (starttls_proto)
1329					{
1330					BIO_printf(bio_err,"%s",mbuf);
1331					/* We don't need to know any more */
1332					starttls_proto = PROTO_OFF;
1333					}
1334
1335				if (reconnect)
1336					{
1337					reconnect--;
1338					BIO_printf(bio_c_out,"drop connection and then reconnect\n");
1339					SSL_shutdown(con);
1340					SSL_set_connect_state(con);
1341					SHUTDOWN(SSL_get_fd(con));
1342					goto re_start;
1343					}
1344				}
1345			}
1346
1347		ssl_pending = read_ssl && SSL_pending(con);
1348
1349		if (!ssl_pending)
1350			{
1351#if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE) && !defined (OPENSSL_SYS_BEOS_R5)
1352			if (tty_on)
1353				{
1354				if (read_tty)  openssl_fdset(fileno(stdin),&readfds);
1355				if (write_tty) openssl_fdset(fileno(stdout),&writefds);
1356				}
1357			if (read_ssl)
1358				openssl_fdset(SSL_get_fd(con),&readfds);
1359			if (write_ssl)
1360				openssl_fdset(SSL_get_fd(con),&writefds);
1361#else
1362			if(!tty_on || !write_tty) {
1363				if (read_ssl)
1364					openssl_fdset(SSL_get_fd(con),&readfds);
1365				if (write_ssl)
1366					openssl_fdset(SSL_get_fd(con),&writefds);
1367			}
1368#endif
1369/*			printf("mode tty(%d %d%d) ssl(%d%d)\n",
1370				tty_on,read_tty,write_tty,read_ssl,write_ssl);*/
1371
1372			/* Note: under VMS with SOCKETSHR the second parameter
1373			 * is currently of type (int *) whereas under other
1374			 * systems it is (void *) if you don't have a cast it
1375			 * will choke the compiler: if you do have a cast then
1376			 * you can either go for (int *) or (void *).
1377			 */
1378#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
1379                        /* Under Windows/DOS we make the assumption that we can
1380			 * always write to the tty: therefore if we need to
1381			 * write to the tty we just fall through. Otherwise
1382			 * we timeout the select every second and see if there
1383			 * are any keypresses. Note: this is a hack, in a proper
1384			 * Windows application we wouldn't do this.
1385			 */
1386			i=0;
1387			if(!write_tty) {
1388				if(read_tty) {
1389					tv.tv_sec = 1;
1390					tv.tv_usec = 0;
1391					i=select(width,(void *)&readfds,(void *)&writefds,
1392						 NULL,&tv);
1393#if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)
1394					if(!i && (!_kbhit() || !read_tty) ) continue;
1395#else
1396					if(!i && (!((_kbhit()) || (WAIT_OBJECT_0 == WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0))) || !read_tty) ) continue;
1397#endif
1398				} else 	i=select(width,(void *)&readfds,(void *)&writefds,
1399					 NULL,timeoutp);
1400			}
1401#elif defined(OPENSSL_SYS_NETWARE)
1402			if(!write_tty) {
1403				if(read_tty) {
1404					tv.tv_sec = 1;
1405					tv.tv_usec = 0;
1406					i=select(width,(void *)&readfds,(void *)&writefds,
1407						NULL,&tv);
1408				} else 	i=select(width,(void *)&readfds,(void *)&writefds,
1409					NULL,timeoutp);
1410			}
1411#elif defined(OPENSSL_SYS_BEOS_R5)
1412			/* Under BeOS-R5 the situation is similar to DOS */
1413			i=0;
1414			stdin_set = 0;
1415			(void)fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
1416			if(!write_tty) {
1417				if(read_tty) {
1418					tv.tv_sec = 1;
1419					tv.tv_usec = 0;
1420					i=select(width,(void *)&readfds,(void *)&writefds,
1421						 NULL,&tv);
1422					if (read(fileno(stdin), sbuf, 0) >= 0)
1423						stdin_set = 1;
1424					if (!i && (stdin_set != 1 || !read_tty))
1425						continue;
1426				} else 	i=select(width,(void *)&readfds,(void *)&writefds,
1427					 NULL,timeoutp);
1428			}
1429			(void)fcntl(fileno(stdin), F_SETFL, 0);
1430#else
1431			i=select(width,(void *)&readfds,(void *)&writefds,
1432				 NULL,timeoutp);
1433#endif
1434			if ( i < 0)
1435				{
1436				BIO_printf(bio_err,"bad select %d\n",
1437				get_last_socket_error());
1438				goto shut;
1439				/* goto end; */
1440				}
1441			}
1442
1443		if ((SSL_version(con) == DTLS1_VERSION) && DTLSv1_handle_timeout(con) > 0)
1444			{
1445			BIO_printf(bio_err,"TIMEOUT occured\n");
1446			}
1447
1448		if (!ssl_pending && FD_ISSET(SSL_get_fd(con),&writefds))
1449			{
1450			k=SSL_write(con,&(cbuf[cbuf_off]),
1451				(unsigned int)cbuf_len);
1452			switch (SSL_get_error(con,k))
1453				{
1454			case SSL_ERROR_NONE:
1455				cbuf_off+=k;
1456				cbuf_len-=k;
1457				if (k <= 0) goto end;
1458				/* we have done a  write(con,NULL,0); */
1459				if (cbuf_len <= 0)
1460					{
1461					read_tty=1;
1462					write_ssl=0;
1463					}
1464				else /* if (cbuf_len > 0) */
1465					{
1466					read_tty=0;
1467					write_ssl=1;
1468					}
1469				break;
1470			case SSL_ERROR_WANT_WRITE:
1471				BIO_printf(bio_c_out,"write W BLOCK\n");
1472				write_ssl=1;
1473				read_tty=0;
1474				break;
1475			case SSL_ERROR_WANT_READ:
1476				BIO_printf(bio_c_out,"write R BLOCK\n");
1477				write_tty=0;
1478				read_ssl=1;
1479				write_ssl=0;
1480				break;
1481			case SSL_ERROR_WANT_X509_LOOKUP:
1482				BIO_printf(bio_c_out,"write X BLOCK\n");
1483				break;
1484			case SSL_ERROR_ZERO_RETURN:
1485				if (cbuf_len != 0)
1486					{
1487					BIO_printf(bio_c_out,"shutdown\n");
1488					ret = 0;
1489					goto shut;
1490					}
1491				else
1492					{
1493					read_tty=1;
1494					write_ssl=0;
1495					break;
1496					}
1497
1498			case SSL_ERROR_SYSCALL:
1499				if ((k != 0) || (cbuf_len != 0))
1500					{
1501					BIO_printf(bio_err,"write:errno=%d\n",
1502						get_last_socket_error());
1503					goto shut;
1504					}
1505				else
1506					{
1507					read_tty=1;
1508					write_ssl=0;
1509					}
1510				break;
1511			case SSL_ERROR_SSL:
1512				ERR_print_errors(bio_err);
1513				goto shut;
1514				}
1515			}
1516#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS_R5)
1517		/* Assume Windows/DOS/BeOS can always write */
1518		else if (!ssl_pending && write_tty)
1519#else
1520		else if (!ssl_pending && FD_ISSET(fileno(stdout),&writefds))
1521#endif
1522			{
1523#ifdef CHARSET_EBCDIC
1524			ascii2ebcdic(&(sbuf[sbuf_off]),&(sbuf[sbuf_off]),sbuf_len);
1525#endif
1526			i=raw_write_stdout(&(sbuf[sbuf_off]),sbuf_len);
1527
1528			if (i <= 0)
1529				{
1530				BIO_printf(bio_c_out,"DONE\n");
1531				ret = 0;
1532				goto shut;
1533				/* goto end; */
1534				}
1535
1536			sbuf_len-=i;;
1537			sbuf_off+=i;
1538			if (sbuf_len <= 0)
1539				{
1540				read_ssl=1;
1541				write_tty=0;
1542				}
1543			}
1544		else if (ssl_pending || FD_ISSET(SSL_get_fd(con),&readfds))
1545			{
1546#ifdef RENEG
1547{ static int iiii; if (++iiii == 52) { SSL_renegotiate(con); iiii=0; } }
1548#endif
1549#if 1
1550			k=SSL_read(con,sbuf,1024 /* BUFSIZZ */ );
1551#else
1552/* Demo for pending and peek :-) */
1553			k=SSL_read(con,sbuf,16);
1554{ char zbuf[10240];
1555printf("read=%d pending=%d peek=%d\n",k,SSL_pending(con),SSL_peek(con,zbuf,10240));
1556}
1557#endif
1558
1559			switch (SSL_get_error(con,k))
1560				{
1561			case SSL_ERROR_NONE:
1562				if (k <= 0)
1563					goto end;
1564				sbuf_off=0;
1565				sbuf_len=k;
1566
1567				read_ssl=0;
1568				write_tty=1;
1569				break;
1570			case SSL_ERROR_WANT_WRITE:
1571				BIO_printf(bio_c_out,"read W BLOCK\n");
1572				write_ssl=1;
1573				read_tty=0;
1574				break;
1575			case SSL_ERROR_WANT_READ:
1576				BIO_printf(bio_c_out,"read R BLOCK\n");
1577				write_tty=0;
1578				read_ssl=1;
1579				if ((read_tty == 0) && (write_ssl == 0))
1580					write_ssl=1;
1581				break;
1582			case SSL_ERROR_WANT_X509_LOOKUP:
1583				BIO_printf(bio_c_out,"read X BLOCK\n");
1584				break;
1585			case SSL_ERROR_SYSCALL:
1586				ret=get_last_socket_error();
1587				BIO_printf(bio_err,"read:errno=%d\n",ret);
1588				goto shut;
1589			case SSL_ERROR_ZERO_RETURN:
1590				BIO_printf(bio_c_out,"closed\n");
1591				ret=0;
1592				goto shut;
1593			case SSL_ERROR_SSL:
1594				ERR_print_errors(bio_err);
1595				goto shut;
1596				/* break; */
1597				}
1598			}
1599
1600#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
1601#if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)
1602		else if (_kbhit())
1603#else
1604		else if ((_kbhit()) || (WAIT_OBJECT_0 == WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0)))
1605#endif
1606#elif defined (OPENSSL_SYS_NETWARE)
1607		else if (_kbhit())
1608#elif defined(OPENSSL_SYS_BEOS_R5)
1609		else if (stdin_set)
1610#else
1611		else if (FD_ISSET(fileno(stdin),&readfds))
1612#endif
1613			{
1614			if (crlf)
1615				{
1616				int j, lf_num;
1617
1618				i=raw_read_stdin(cbuf,BUFSIZZ/2);
1619				lf_num = 0;
1620				/* both loops are skipped when i <= 0 */
1621				for (j = 0; j < i; j++)
1622					if (cbuf[j] == '\n')
1623						lf_num++;
1624				for (j = i-1; j >= 0; j--)
1625					{
1626					cbuf[j+lf_num] = cbuf[j];
1627					if (cbuf[j] == '\n')
1628						{
1629						lf_num--;
1630						i++;
1631						cbuf[j+lf_num] = '\r';
1632						}
1633					}
1634				assert(lf_num == 0);
1635				}
1636			else
1637				i=raw_read_stdin(cbuf,BUFSIZZ);
1638
1639			if ((!c_ign_eof) && ((i <= 0) || (cbuf[0] == 'Q')))
1640				{
1641				BIO_printf(bio_err,"DONE\n");
1642				ret=0;
1643				goto shut;
1644				}
1645
1646			if ((!c_ign_eof) && (cbuf[0] == 'R'))
1647				{
1648				BIO_printf(bio_err,"RENEGOTIATING\n");
1649				SSL_renegotiate(con);
1650				cbuf_len=0;
1651				}
1652			else
1653				{
1654				cbuf_len=i;
1655				cbuf_off=0;
1656#ifdef CHARSET_EBCDIC
1657				ebcdic2ascii(cbuf, cbuf, i);
1658#endif
1659				}
1660
1661			write_ssl=1;
1662			read_tty=0;
1663			}
1664		}
1665
1666	ret=0;
1667shut:
1668	if (in_init)
1669		print_stuff(bio_c_out,con,full_log);
1670	SSL_shutdown(con);
1671	SHUTDOWN(SSL_get_fd(con));
1672end:
1673	if (con != NULL)
1674		{
1675		if (prexit != 0)
1676			print_stuff(bio_c_out,con,1);
1677		SSL_free(con);
1678		}
1679	if (ctx != NULL) SSL_CTX_free(ctx);
1680	if (cert)
1681		X509_free(cert);
1682	if (key)
1683		EVP_PKEY_free(key);
1684	if (pass)
1685		OPENSSL_free(pass);
1686	if (cbuf != NULL) { OPENSSL_cleanse(cbuf,BUFSIZZ); OPENSSL_free(cbuf); }
1687	if (sbuf != NULL) { OPENSSL_cleanse(sbuf,BUFSIZZ); OPENSSL_free(sbuf); }
1688	if (mbuf != NULL) { OPENSSL_cleanse(mbuf,BUFSIZZ); OPENSSL_free(mbuf); }
1689	if (bio_c_out != NULL)
1690		{
1691		BIO_free(bio_c_out);
1692		bio_c_out=NULL;
1693		}
1694	apps_shutdown();
1695	OPENSSL_EXIT(ret);
1696	}
1697
1698
1699static void print_stuff(BIO *bio, SSL *s, int full)
1700	{
1701	X509 *peer=NULL;
1702	char *p;
1703	static const char *space="                ";
1704	char buf[BUFSIZ];
1705	STACK_OF(X509) *sk;
1706	STACK_OF(X509_NAME) *sk2;
1707	const SSL_CIPHER *c;
1708	X509_NAME *xn;
1709	int j,i;
1710#ifndef OPENSSL_NO_COMP
1711	const COMP_METHOD *comp, *expansion;
1712#endif
1713
1714	if (full)
1715		{
1716		int got_a_chain = 0;
1717
1718		sk=SSL_get_peer_cert_chain(s);
1719		if (sk != NULL)
1720			{
1721			got_a_chain = 1; /* we don't have it for SSL2 (yet) */
1722
1723			BIO_printf(bio,"---\nCertificate chain\n");
1724			for (i=0; i<sk_X509_num(sk); i++)
1725				{
1726				X509_NAME_oneline(X509_get_subject_name(
1727					sk_X509_value(sk,i)),buf,sizeof buf);
1728				BIO_printf(bio,"%2d s:%s\n",i,buf);
1729				X509_NAME_oneline(X509_get_issuer_name(
1730					sk_X509_value(sk,i)),buf,sizeof buf);
1731				BIO_printf(bio,"   i:%s\n",buf);
1732				if (c_showcerts)
1733					PEM_write_bio_X509(bio,sk_X509_value(sk,i));
1734				}
1735			}
1736
1737		BIO_printf(bio,"---\n");
1738		peer=SSL_get_peer_certificate(s);
1739		if (peer != NULL)
1740			{
1741			BIO_printf(bio,"Server certificate\n");
1742			if (!(c_showcerts && got_a_chain)) /* Redundant if we showed the whole chain */
1743				PEM_write_bio_X509(bio,peer);
1744			X509_NAME_oneline(X509_get_subject_name(peer),
1745				buf,sizeof buf);
1746			BIO_printf(bio,"subject=%s\n",buf);
1747			X509_NAME_oneline(X509_get_issuer_name(peer),
1748				buf,sizeof buf);
1749			BIO_printf(bio,"issuer=%s\n",buf);
1750			}
1751		else
1752			BIO_printf(bio,"no peer certificate available\n");
1753
1754		sk2=SSL_get_client_CA_list(s);
1755		if ((sk2 != NULL) && (sk_X509_NAME_num(sk2) > 0))
1756			{
1757			BIO_printf(bio,"---\nAcceptable client certificate CA names\n");
1758			for (i=0; i<sk_X509_NAME_num(sk2); i++)
1759				{
1760				xn=sk_X509_NAME_value(sk2,i);
1761				X509_NAME_oneline(xn,buf,sizeof(buf));
1762				BIO_write(bio,buf,strlen(buf));
1763				BIO_write(bio,"\n",1);
1764				}
1765			}
1766		else
1767			{
1768			BIO_printf(bio,"---\nNo client certificate CA names sent\n");
1769			}
1770		p=SSL_get_shared_ciphers(s,buf,sizeof buf);
1771		if (p != NULL)
1772			{
1773			/* This works only for SSL 2.  In later protocol
1774			 * versions, the client does not know what other
1775			 * ciphers (in addition to the one to be used
1776			 * in the current connection) the server supports. */
1777
1778			BIO_printf(bio,"---\nCiphers common between both SSL endpoints:\n");
1779			j=i=0;
1780			while (*p)
1781				{
1782				if (*p == ':')
1783					{
1784					BIO_write(bio,space,15-j%25);
1785					i++;
1786					j=0;
1787					BIO_write(bio,((i%3)?" ":"\n"),1);
1788					}
1789				else
1790					{
1791					BIO_write(bio,p,1);
1792					j++;
1793					}
1794				p++;
1795				}
1796			BIO_write(bio,"\n",1);
1797			}
1798
1799		BIO_printf(bio,"---\nSSL handshake has read %ld bytes and written %ld bytes\n",
1800			BIO_number_read(SSL_get_rbio(s)),
1801			BIO_number_written(SSL_get_wbio(s)));
1802		}
1803	BIO_printf(bio,((s->hit)?"---\nReused, ":"---\nNew, "));
1804	c=SSL_get_current_cipher(s);
1805	BIO_printf(bio,"%s, Cipher is %s\n",
1806		SSL_CIPHER_get_version(c),
1807		SSL_CIPHER_get_name(c));
1808	if (peer != NULL) {
1809		EVP_PKEY *pktmp;
1810		pktmp = X509_get_pubkey(peer);
1811		BIO_printf(bio,"Server public key is %d bit\n",
1812							 EVP_PKEY_bits(pktmp));
1813		EVP_PKEY_free(pktmp);
1814	}
1815	BIO_printf(bio, "Secure Renegotiation IS%s supported\n",
1816			SSL_get_secure_renegotiation_support(s) ? "" : " NOT");
1817#ifndef OPENSSL_NO_COMP
1818	comp=SSL_get_current_compression(s);
1819	expansion=SSL_get_current_expansion(s);
1820	BIO_printf(bio,"Compression: %s\n",
1821		comp ? SSL_COMP_get_name(comp) : "NONE");
1822	BIO_printf(bio,"Expansion: %s\n",
1823		expansion ? SSL_COMP_get_name(expansion) : "NONE");
1824#endif
1825
1826#if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
1827	if (next_proto.status != -1) {
1828		const unsigned char *proto;
1829		unsigned int proto_len;
1830		SSL_get0_next_proto_negotiated(s, &proto, &proto_len);
1831		BIO_printf(bio, "Next protocol: (%d) ", next_proto.status);
1832		BIO_write(bio, proto, proto_len);
1833		BIO_write(bio, "\n", 1);
1834	}
1835#endif
1836
1837	SSL_SESSION_print(bio,SSL_get_session(s));
1838	BIO_printf(bio,"---\n");
1839	if (peer != NULL)
1840		X509_free(peer);
1841	/* flush, or debugging output gets mixed with http response */
1842	(void)BIO_flush(bio);
1843	}
1844
1845#ifndef OPENSSL_NO_TLSEXT
1846
1847static int ocsp_resp_cb(SSL *s, void *arg)
1848	{
1849	const unsigned char *p;
1850	int len;
1851	OCSP_RESPONSE *rsp;
1852	len = SSL_get_tlsext_status_ocsp_resp(s, &p);
1853	BIO_puts(arg, "OCSP response: ");
1854	if (!p)
1855		{
1856		BIO_puts(arg, "no response sent\n");
1857		return 1;
1858		}
1859	rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
1860	if (!rsp)
1861		{
1862		BIO_puts(arg, "response parse error\n");
1863		BIO_dump_indent(arg, (char *)p, len, 4);
1864		return 0;
1865		}
1866	BIO_puts(arg, "\n======================================\n");
1867	OCSP_RESPONSE_print(arg, rsp, 0);
1868	BIO_puts(arg, "======================================\n");
1869	OCSP_RESPONSE_free(rsp);
1870	return 1;
1871	}
1872
1873#endif
1874