xref: /openbsd-src/usr.bin/openssl/s_client.c (revision ff0e7be1ebbcc809ea8ad2b6dafe215824da9e46)
1 /* $OpenBSD: s_client.c,v 1.60 2023/03/06 14:32:06 tb Exp $ */
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 <sys/types.h>
139 #include <sys/socket.h>
140 
141 #include <netinet/in.h>
142 
143 #include <assert.h>
144 #include <ctype.h>
145 #include <limits.h>
146 #include <netdb.h>
147 #include <stdio.h>
148 #include <stdlib.h>
149 #include <string.h>
150 #include <unistd.h>
151 #include <poll.h>
152 
153 #include "apps.h"
154 
155 #include <openssl/bn.h>
156 #include <openssl/err.h>
157 #include <openssl/ocsp.h>
158 #include <openssl/pem.h>
159 #include <openssl/ssl.h>
160 #include <openssl/x509.h>
161 
162 #include "s_apps.h"
163 #include "timeouts.h"
164 
165 /*#define SSL_HOST_NAME	"www.netscape.com" */
166 /*#define SSL_HOST_NAME	"193.118.187.102" */
167 #define SSL_HOST_NAME	"localhost"
168 
169  /*#define TEST_CERT "client.pem" *//* no default cert. */
170 
171 #define BUFSIZZ 1024*8
172 
173 static void sc_usage(void);
174 static void print_stuff(BIO *berr, SSL *con, int full);
175 static int ocsp_resp_cb(SSL *s, void *arg);
176 static int ssl_servername_cb(SSL *s, int *ad, void *arg);
177 
178 enum {
179 	PROTO_OFF = 0,
180 	PROTO_SMTP,
181 	PROTO_LMTP,
182 	PROTO_POP3,
183 	PROTO_IMAP,
184 	PROTO_FTP,
185 	PROTO_XMPP,
186 };
187 
188 /* This is a context that we pass to callbacks */
189 typedef struct tlsextctx_st {
190 	BIO *biodebug;
191 	int ack;
192 } tlsextctx;
193 
194 static struct {
195 	int af;
196 	char *alpn_in;
197 	int bugs;
198 	char *CAfile;
199 	char *CApath;
200 	char *cert_file;
201 	int cert_format;
202 	char *cipher;
203 	unsigned int clr;
204 	char *connect;
205 	int crlf;
206 	int debug;
207 	int enable_timeouts;
208 	const char *errstr;
209 	char *groups_in;
210 	char *host;
211 	int ign_eof;
212 	char *key_file;
213 	int key_format;
214 	char *keymatexportlabel;
215 	int keymatexportlen;
216 	uint16_t max_version;
217 	uint16_t min_version;
218 	const SSL_METHOD *meth;
219 	int msg;
220 	int nbio;
221 	int nbio_test;
222 	int no_servername;
223 	char *npn_in;
224 	unsigned int off;
225 	char *passarg;
226 	int pause;
227 	int peekaboo;
228 	char *port;
229 	int prexit;
230 	char *proxy;
231 	int quiet;
232 	int reconnect;
233 	char *servername;
234 	char *sess_in;
235 	char *sess_out;
236 	int showcerts;
237 	int socket_type;
238 	long socket_mtu;
239 #ifndef OPENSSL_NO_SRTP
240 	char *srtp_profiles;
241 #endif
242 	int starttls_proto;
243 	int state;
244 	int status_req;
245 	int tlsextdebug;
246 	int verify;
247 	X509_VERIFY_PARAM *vpm;
248 	char *xmpphost;
249 } cfg;
250 
251 static int
252 s_client_opt_keymatexportlen(char *arg)
253 {
254 	cfg.keymatexportlen = strtonum(arg, 1, INT_MAX,
255 	    &cfg.errstr);
256 	if (cfg.errstr != NULL) {
257 		BIO_printf(bio_err, "invalid argument %s: %s\n",
258 		    arg, cfg.errstr);
259 		return (1);
260 	}
261 	return (0);
262 }
263 
264 #ifndef OPENSSL_NO_DTLS
265 static int
266 s_client_opt_mtu(char *arg)
267 {
268 	cfg.socket_mtu = strtonum(arg, 0, LONG_MAX,
269 	    &cfg.errstr);
270 	if (cfg.errstr != NULL) {
271 		BIO_printf(bio_err, "invalid argument %s: %s\n",
272 		    arg, cfg.errstr);
273 		return (1);
274 	}
275 	return (0);
276 }
277 #endif
278 
279 static int
280 s_client_opt_port(char *arg)
281 {
282 	if (*arg == '\0')
283 		return (1);
284 
285 	cfg.port = arg;
286 	return (0);
287 }
288 
289 #ifndef OPENSSL_NO_DTLS
290 static int
291 s_client_opt_protocol_version_dtls(void)
292 {
293 	cfg.meth = DTLS_client_method();
294 	cfg.socket_type = SOCK_DGRAM;
295 	return (0);
296 }
297 #endif
298 
299 #ifndef OPENSSL_NO_DTLS1
300 static int
301 s_client_opt_protocol_version_dtls1(void)
302 {
303 	cfg.meth = DTLS_client_method();
304 	cfg.min_version = DTLS1_VERSION;
305 	cfg.max_version = DTLS1_VERSION;
306 	cfg.socket_type = SOCK_DGRAM;
307 	return (0);
308 }
309 #endif
310 
311 #ifndef OPENSSL_NO_DTLS1_2
312 static int
313 s_client_opt_protocol_version_dtls1_2(void)
314 {
315 	cfg.meth = DTLS_client_method();
316 	cfg.min_version = DTLS1_2_VERSION;
317 	cfg.max_version = DTLS1_2_VERSION;
318 	cfg.socket_type = SOCK_DGRAM;
319 	return (0);
320 }
321 #endif
322 
323 static int
324 s_client_opt_protocol_version_tls1(void)
325 {
326 	cfg.min_version = TLS1_VERSION;
327 	cfg.max_version = TLS1_VERSION;
328 	return (0);
329 }
330 
331 static int
332 s_client_opt_protocol_version_tls1_1(void)
333 {
334 	cfg.min_version = TLS1_1_VERSION;
335 	cfg.max_version = TLS1_1_VERSION;
336 	return (0);
337 }
338 
339 static int
340 s_client_opt_protocol_version_tls1_2(void)
341 {
342 	cfg.min_version = TLS1_2_VERSION;
343 	cfg.max_version = TLS1_2_VERSION;
344 	return (0);
345 }
346 
347 static int
348 s_client_opt_protocol_version_tls1_3(void)
349 {
350 	cfg.min_version = TLS1_3_VERSION;
351 	cfg.max_version = TLS1_3_VERSION;
352 	return (0);
353 }
354 
355 static int
356 s_client_opt_quiet(void)
357 {
358 	cfg.quiet = 1;
359 	cfg.ign_eof = 1;
360 	return (0);
361 }
362 
363 static int
364 s_client_opt_starttls(char *arg)
365 {
366 	if (strcmp(arg, "smtp") == 0)
367 		cfg.starttls_proto = PROTO_SMTP;
368 	else if (strcmp(arg, "lmtp") == 0)
369 		cfg.starttls_proto = PROTO_LMTP;
370 	else if (strcmp(arg, "pop3") == 0)
371 		cfg.starttls_proto = PROTO_POP3;
372 	else if (strcmp(arg, "imap") == 0)
373 		cfg.starttls_proto = PROTO_IMAP;
374 	else if (strcmp(arg, "ftp") == 0)
375 		cfg.starttls_proto = PROTO_FTP;
376 	else if (strcmp(arg, "xmpp") == 0)
377 		cfg.starttls_proto = PROTO_XMPP;
378 	else
379 		return (1);
380 	return (0);
381 }
382 
383 static int
384 s_client_opt_verify(char *arg)
385 {
386 	cfg.verify = SSL_VERIFY_PEER;
387 
388 	verify_depth = strtonum(arg, 0, INT_MAX, &cfg.errstr);
389 	if (cfg.errstr != NULL) {
390 		BIO_printf(bio_err, "invalid argument %s: %s\n",
391 		    arg, cfg.errstr);
392 		return (1);
393 	}
394 	BIO_printf(bio_err, "verify depth is %d\n", verify_depth);
395 	return (0);
396 }
397 
398 static int
399 s_client_opt_verify_param(int argc, char **argv, int *argsused)
400 {
401 	char **pargs = argv;
402 	int pargc = argc;
403 	int badarg = 0;
404 
405 	if (!args_verify(&pargs, &pargc, &badarg, bio_err,
406 	    &cfg.vpm)) {
407 		BIO_printf(bio_err, "unknown option %s\n", *argv);
408 		return (1);
409 	}
410 	if (badarg)
411 		return (1);
412 
413 	*argsused = argc - pargc;
414 	return (0);
415 }
416 
417 static const struct option s_client_options[] = {
418 	{
419 		.name = "4",
420 		.desc = "Use IPv4 only",
421 		.type = OPTION_VALUE,
422 		.opt.value = &cfg.af,
423 		.value = AF_INET,
424 	},
425 	{
426 		.name = "6",
427 		.desc = "Use IPv6 only",
428 		.type = OPTION_VALUE,
429 		.opt.value = &cfg.af,
430 		.value = AF_INET6,
431 	},
432 	{
433 		.name = "alpn",
434 		.argname = "protocols",
435 		.desc = "Set the advertised protocols for ALPN"
436 			" (comma-separated list)",
437 		.type = OPTION_ARG,
438 		.opt.arg = &cfg.alpn_in,
439 	},
440 	{
441 		.name = "bugs",
442 		.desc = "Enable various workarounds for buggy implementations",
443 		.type = OPTION_FLAG,
444 		.opt.flag = &cfg.bugs,
445 	},
446 	{
447 		.name = "CAfile",
448 		.argname = "file",
449 		.desc = "PEM format file of CA certificates",
450 		.type = OPTION_ARG,
451 		.opt.arg = &cfg.CAfile,
452 	},
453 	{
454 		.name = "CApath",
455 		.argname = "directory",
456 		.desc = "PEM format directory of CA certificates",
457 		.type = OPTION_ARG,
458 		.opt.arg = &cfg.CApath,
459 	},
460 	{
461 		.name = "cert",
462 		.argname = "file",
463 		.desc = "Certificate file to use, PEM format assumed",
464 		.type = OPTION_ARG,
465 		.opt.arg = &cfg.cert_file,
466 	},
467 	{
468 		.name = "certform",
469 		.argname = "fmt",
470 		.desc = "Certificate format (PEM or DER) PEM default",
471 		.type = OPTION_ARG_FORMAT,
472 		.opt.value = &cfg.cert_format,
473 	},
474 	{
475 		.name = "cipher",
476 		.argname = "cipherlist",
477 		.desc = "Preferred cipher to use (see 'openssl ciphers')",
478 		.type = OPTION_ARG,
479 		.opt.arg = &cfg.cipher,
480 	},
481 	{
482 		.name = "connect",
483 		.argname = "host:port",
484 		.desc = "Who to connect to (default is localhost:4433)",
485 		.type = OPTION_ARG,
486 		.opt.arg = &cfg.connect,
487 	},
488 	{
489 		.name = "crlf",
490 		.desc = "Convert LF from terminal into CRLF",
491 		.type = OPTION_FLAG,
492 		.opt.flag = &cfg.crlf,
493 	},
494 	{
495 		.name = "debug",
496 		.desc = "Print extensive debugging information",
497 		.type = OPTION_FLAG,
498 		.opt.flag = &cfg.debug,
499 	},
500 #ifndef OPENSSL_NO_DTLS
501 	{
502 		.name = "dtls",
503 		.desc = "Use any version of DTLS",
504 		.type = OPTION_FUNC,
505 		.opt.func = s_client_opt_protocol_version_dtls,
506 	},
507 #endif
508 #ifndef OPENSSL_NO_DTLS1
509 	{
510 		.name = "dtls1",
511 		.desc = "Just use DTLSv1",
512 		.type = OPTION_FUNC,
513 		.opt.func = s_client_opt_protocol_version_dtls1,
514 	},
515 #endif
516 #ifndef OPENSSL_NO_DTLS1_2
517 	{
518 		.name = "dtls1_2",
519 		.desc = "Just use DTLSv1.2",
520 		.type = OPTION_FUNC,
521 		.opt.func = s_client_opt_protocol_version_dtls1_2,
522 	},
523 #endif
524 	{
525 		.name = "groups",
526 		.argname = "list",
527 		.desc = "Specify EC groups (colon-separated list)",
528 		.type = OPTION_ARG,
529 		.opt.arg = &cfg.groups_in,
530 	},
531 	{
532 		.name = "host",
533 		.argname = "host",
534 		.desc = "Use -connect instead",
535 		.type = OPTION_ARG,
536 		.opt.arg = &cfg.host,
537 	},
538 	{
539 		.name = "ign_eof",
540 		.desc = "Ignore input EOF (default when -quiet)",
541 		.type = OPTION_VALUE,
542 		.opt.value = &cfg.ign_eof,
543 		.value = 1,
544 	},
545 	{
546 		.name = "key",
547 		.argname = "file",
548 		.desc = "Private key file to use, if not, -cert file is used",
549 		.type = OPTION_ARG,
550 		.opt.arg = &cfg.key_file,
551 	},
552 	{
553 		.name = "keyform",
554 		.argname = "fmt",
555 		.desc = "Key format (PEM or DER) PEM default",
556 		.type = OPTION_ARG_FORMAT,
557 		.opt.value = &cfg.key_format,
558 	},
559 	{
560 		.name = "keymatexport",
561 		.argname = "label",
562 		.desc = "Export keying material using label",
563 		.type = OPTION_ARG,
564 		.opt.arg = &cfg.keymatexportlabel,
565 	},
566 	{
567 		.name = "keymatexportlen",
568 		.argname = "len",
569 		.desc = "Export len bytes of keying material (default 20)",
570 		.type = OPTION_ARG_FUNC,
571 		.opt.argfunc = s_client_opt_keymatexportlen,
572 	},
573 	{
574 		.name = "legacy_renegotiation",
575 		.type = OPTION_DISCARD,
576 	},
577 	{
578 		.name = "legacy_server_connect",
579 		.desc = "Allow initial connection to servers that don't support RI",
580 		.type = OPTION_VALUE_OR,
581 		.opt.value = &cfg.off,
582 		.value = SSL_OP_LEGACY_SERVER_CONNECT,
583 	},
584 	{
585 		.name = "msg",
586 		.desc = "Show all protocol messages with hex dump",
587 		.type = OPTION_FLAG,
588 		.opt.flag = &cfg.msg,
589 	},
590 #ifndef OPENSSL_NO_DTLS
591 	{
592 		.name = "mtu",
593 		.argname = "mtu",
594 		.desc = "Set the link layer MTU on DTLS connections",
595 		.type = OPTION_ARG_FUNC,
596 		.opt.argfunc = s_client_opt_mtu,
597 	},
598 #endif
599 	{
600 		.name = "nbio",
601 		.desc = "Turn on non-blocking I/O",
602 		.type = OPTION_FLAG,
603 		.opt.flag = &cfg.nbio,
604 	},
605 	{
606 		.name = "nbio_test",
607 		.desc = "Test non-blocking I/O",
608 		.type = OPTION_FLAG,
609 		.opt.flag = &cfg.nbio_test,
610 	},
611 	{
612 		.name = "nextprotoneg",
613 		.argname = "protocols",
614 		.type = OPTION_ARG,
615 		.opt.arg = &cfg.npn_in, /* Ignored. */
616 	},
617 	{
618 		.name = "no_comp",
619 		.type = OPTION_VALUE_OR,
620 		.opt.value = &cfg.off,
621 		.value = SSL_OP_NO_COMPRESSION,
622 	},
623 	{
624 		.name = "no_ign_eof",
625 		.desc = "Don't ignore input EOF",
626 		.type = OPTION_VALUE,
627 		.opt.value = &cfg.ign_eof,
628 		.value = 0,
629 	},
630 	{
631 		.name = "no_legacy_server_connect",
632 		.desc = "Disallow initial connection to servers that don't support RI",
633 		.type = OPTION_VALUE_OR,
634 		.opt.value = &cfg.clr,
635 		.value = SSL_OP_LEGACY_SERVER_CONNECT,
636 	},
637 	{
638 		.name = "no_servername",
639 		.desc = "Do not send a Server Name Indication (SNI) extension",
640 		.type = OPTION_FLAG,
641 		.opt.value = &cfg.no_servername,
642 	},
643 	{
644 		.name = "no_ssl2",
645 		.type = OPTION_VALUE_OR,
646 		.opt.value = &cfg.off,
647 		.value = SSL_OP_NO_SSLv2,
648 	},
649 	{
650 		.name = "no_ssl3",
651 		.type = OPTION_VALUE_OR,
652 		.opt.value = &cfg.off,
653 		.value = SSL_OP_NO_SSLv3,
654 	},
655 	{
656 		.name = "no_ticket",
657 		.desc = "Disable use of RFC4507 session ticket support",
658 		.type = OPTION_VALUE_OR,
659 		.opt.value = &cfg.off,
660 		.value = SSL_OP_NO_TICKET,
661 	},
662 	{
663 		.name = "no_tls1",
664 		.desc = "Disable the use of TLSv1",
665 		.type = OPTION_VALUE_OR,
666 		.opt.value = &cfg.off,
667 		.value = SSL_OP_NO_TLSv1,
668 	},
669 	{
670 		.name = "no_tls1_1",
671 		.desc = "Disable the use of TLSv1.1",
672 		.type = OPTION_VALUE_OR,
673 		.opt.value = &cfg.off,
674 		.value = SSL_OP_NO_TLSv1_1,
675 	},
676 	{
677 		.name = "no_tls1_2",
678 		.desc = "Disable the use of TLSv1.2",
679 		.type = OPTION_VALUE_OR,
680 		.opt.value = &cfg.off,
681 		.value = SSL_OP_NO_TLSv1_2,
682 	},
683 	{
684 		.name = "no_tls1_3",
685 		.desc = "Disable the use of TLSv1.3",
686 		.type = OPTION_VALUE_OR,
687 		.opt.value = &cfg.off,
688 		.value = SSL_OP_NO_TLSv1_3,
689 	},
690 	{
691 		.name = "noservername",
692 		.type = OPTION_FLAG,
693 		.opt.value = &cfg.no_servername,
694 	},
695 	{
696 		.name = "pass",
697 		.argname = "arg",
698 		.desc = "Private key file pass phrase source",
699 		.type = OPTION_ARG,
700 		.opt.arg = &cfg.passarg,
701 	},
702 	{
703 		.name = "pause",
704 		.desc = "Pause 1 second between each read and write call",
705 		.type = OPTION_FLAG,
706 		.opt.flag = &cfg.pause,
707 	},
708 	{
709 		.name = "peekaboo",
710 		.type = OPTION_FLAG,
711 		.opt.flag = &cfg.peekaboo,
712 	},
713 	{
714 		.name = "port",
715 		.argname = "port",
716 		.desc = "Use -connect instead",
717 		.type = OPTION_ARG_FUNC,
718 		.opt.argfunc = s_client_opt_port,
719 	},
720 	{
721 		.name = "prexit",
722 		.desc = "Print session information when the program exits",
723 		.type = OPTION_FLAG,
724 		.opt.flag = &cfg.prexit,
725 	},
726 	{
727 		.name = "proxy",
728 		.argname = "host:port",
729 		.desc = "Connect to http proxy",
730 		.type = OPTION_ARG,
731 		.opt.arg = &cfg.proxy,
732 	},
733 	{
734 		.name = "quiet",
735 		.desc = "Inhibit printing of session and certificate info",
736 		.type = OPTION_FUNC,
737 		.opt.func = s_client_opt_quiet,
738 	},
739 	{
740 		.name = "reconnect",
741 		.desc = "Drop and re-make the connection with the same Session-ID",
742 		.type = OPTION_VALUE,
743 		.opt.value = &cfg.reconnect,
744 		.value = 5,
745 	},
746 	{
747 		.name = "servername",
748 		.argname = "name",
749 		.desc = "Set TLS extension servername in ClientHello (SNI)",
750 		.type = OPTION_ARG,
751 		.opt.arg = &cfg.servername,
752 	},
753 	{
754 		.name = "serverpref",
755 		.desc = "Use server's cipher preferences",
756 		.type = OPTION_VALUE_OR,
757 		.opt.value = &cfg.off,
758 		.value = SSL_OP_CIPHER_SERVER_PREFERENCE,
759 	},
760 	{
761 		.name = "sess_in",
762 		.argname = "file",
763 		.desc = "File to read TLS session from",
764 		.type = OPTION_ARG,
765 		.opt.arg = &cfg.sess_in,
766 	},
767 	{
768 		.name = "sess_out",
769 		.argname = "file",
770 		.desc = "File to write TLS session to",
771 		.type = OPTION_ARG,
772 		.opt.arg = &cfg.sess_out,
773 	},
774 	{
775 		.name = "showcerts",
776 		.desc = "Show all server certificates in the chain",
777 		.type = OPTION_FLAG,
778 		.opt.flag = &cfg.showcerts,
779 	},
780 	{
781 		.name = "starttls",
782 		.argname = "protocol",
783 		.desc = "Use the STARTTLS command before starting TLS,\n"
784 		        "smtp, lmtp, pop3, imap, ftp and xmpp are supported.",
785 		.type = OPTION_ARG_FUNC,
786 		.opt.argfunc = s_client_opt_starttls,
787 	},
788 	{
789 		.name = "state",
790 		.desc = "Print the TLS session states",
791 		.type = OPTION_FLAG,
792 		.opt.flag = &cfg.state,
793 	},
794 	{
795 		.name = "status",
796 		.desc = "Send a certificate status request to the server (OCSP)",
797 		.type = OPTION_FLAG,
798 		.opt.flag = &cfg.status_req,
799 	},
800 #ifndef OPENSSL_NO_DTLS
801 	{
802 		.name = "timeout",
803 		.desc = "Enable send/receive timeout on DTLS connections",
804 		.type = OPTION_FLAG,
805 		.opt.flag = &cfg.enable_timeouts,
806 	},
807 #endif
808 	{
809 		.name = "tls1",
810 		.desc = "Just use TLSv1",
811 		.type = OPTION_FUNC,
812 		.opt.func = s_client_opt_protocol_version_tls1,
813 	},
814 	{
815 		.name = "tls1_1",
816 		.desc = "Just use TLSv1.1",
817 		.type = OPTION_FUNC,
818 		.opt.func = s_client_opt_protocol_version_tls1_1,
819 	},
820 	{
821 		.name = "tls1_2",
822 		.desc = "Just use TLSv1.2",
823 		.type = OPTION_FUNC,
824 		.opt.func = s_client_opt_protocol_version_tls1_2,
825 	},
826 	{
827 		.name = "tls1_3",
828 		.desc = "Just use TLSv1.3",
829 		.type = OPTION_FUNC,
830 		.opt.func = s_client_opt_protocol_version_tls1_3,
831 	},
832 	{
833 		.name = "tlsextdebug",
834 		.desc = "Hex dump of all TLS extensions received",
835 		.type = OPTION_FLAG,
836 		.opt.flag = &cfg.tlsextdebug,
837 	},
838 #ifndef OPENSSL_NO_SRTP
839 	{
840 		.name = "use_srtp",
841 		.argname = "profiles",
842 		.desc = "Offer SRTP key management with a colon-separated profiles",
843 		.type = OPTION_ARG,
844 		.opt.arg = &cfg.srtp_profiles,
845 	},
846 #endif
847 	{
848 		.name = "verify",
849 		.argname = "depth",
850 		.desc = "Turn on peer certificate verification, with a max of depth",
851 		.type = OPTION_ARG_FUNC,
852 		.opt.argfunc = s_client_opt_verify,
853 	},
854 	{
855 		.name = "verify_return_error",
856 		.desc = "Return verification error",
857 		.type = OPTION_FLAG,
858 		.opt.flag = &verify_return_error,
859 	},
860 	{
861 		.name = "xmpphost",
862 		.argname = "host",
863 		.desc = "Connect to this virtual host on the xmpp server",
864 		.type = OPTION_ARG,
865 		.opt.arg = &cfg.xmpphost,
866 	},
867 	{
868 		.name = NULL,
869 		.desc = "",
870 		.type = OPTION_ARGV_FUNC,
871 		.opt.argvfunc = s_client_opt_verify_param,
872 	},
873 	{ NULL },
874 };
875 
876 static void
877 sc_usage(void)
878 {
879 	fprintf(stderr, "usage: s_client "
880 	    "[-4 | -6] [-alpn protocols] [-bugs] [-CAfile file]\n"
881 	    "    [-CApath directory] [-cert file] [-certform der | pem] [-check_ss_sig]\n"
882 	    "    [-cipher cipherlist] [-connect host[:port]] [-crl_check]\n"
883 	    "    [-crl_check_all] [-crlf] [-debug] [-dtls] [-dtls1] [-dtls1_2] [-extended_crl]\n"
884 	    "    [-groups list] [-host host] [-ign_eof] [-ignore_critical]\n"
885 	    "    [-issuer_checks] [-key keyfile] [-keyform der | pem]\n"
886 	    "    [-keymatexport label] [-keymatexportlen len] [-legacy_server_connect]\n"
887 	    "    [-msg] [-mtu mtu] [-nbio] [-nbio_test] [-no_comp] [-no_ign_eof]\n"
888 	    "    [-no_legacy_server_connect] [-no_ticket] [-no_tls1] [-no_tls1_1]\n"
889 	    "    [-no_tls1_2] [-no_tls1_3] [-pass arg] [-pause] [-policy_check]\n"
890 	    "    [-port port] [-prexit] [-proxy host:port] [-quiet] [-reconnect]\n"
891 	    "    [-servername name] [-serverpref] [-sess_in file] [-sess_out file]\n"
892 	    "    [-showcerts] [-starttls protocol] [-state] [-status] [-timeout]\n"
893 	    "    [-tls1] [-tls1_1] [-tls1_2] [-tls1_3] [-tlsextdebug]\n"
894 	    "    [-use_srtp profiles] [-verify depth] [-verify_return_error]\n"
895 	    "    [-x509_strict] [-xmpphost host]\n");
896 	fprintf(stderr, "\n");
897 	options_usage(s_client_options);
898 	fprintf(stderr, "\n");
899 }
900 
901 int
902 s_client_main(int argc, char **argv)
903 {
904 	SSL *con = NULL;
905 	int s, k, p = 0, pending = 0;
906 	char *cbuf = NULL, *sbuf = NULL, *mbuf = NULL, *pbuf = NULL;
907 	int cbuf_len, cbuf_off;
908 	int sbuf_len, sbuf_off;
909 	int full_log = 1;
910 	const char *servername;
911 	char *pass = NULL;
912 	X509 *cert = NULL;
913 	EVP_PKEY *key = NULL;
914 	int badop = 0;
915 	int write_tty, read_tty, write_ssl, read_ssl, tty_on, ssl_pending;
916 	SSL_CTX *ctx = NULL;
917 	int ret = 1, in_init = 1, i;
918 	BIO *bio_c_out = NULL;
919 	BIO *sbio;
920 	int mbuf_len = 0;
921 	struct timeval timeout;
922 	tlsextctx tlsextcbp = {NULL, 0};
923 	struct sockaddr_storage peer;
924 	int peerlen = sizeof(peer);
925 
926 	if (pledge("stdio cpath wpath rpath inet dns tty", NULL) == -1) {
927 		perror("pledge");
928 		exit(1);
929 	}
930 
931 	memset(&cfg, 0, sizeof(cfg));
932 	cfg.af = AF_UNSPEC;
933 	cfg.cert_format = FORMAT_PEM;
934 	cfg.host = SSL_HOST_NAME;
935 	cfg.key_format = FORMAT_PEM;
936 	cfg.keymatexportlen = 20;
937 	cfg.meth = TLS_client_method();
938 	cfg.port = PORT_STR;
939 	cfg.socket_type = SOCK_STREAM;
940 	cfg.starttls_proto = PROTO_OFF;
941 	cfg.verify = SSL_VERIFY_NONE;
942 
943 	if (((cbuf = malloc(BUFSIZZ)) == NULL) ||
944 	    ((sbuf = malloc(BUFSIZZ)) == NULL) ||
945 	    ((pbuf = malloc(BUFSIZZ)) == NULL) ||
946 	    ((mbuf = malloc(BUFSIZZ + 1)) == NULL)) {	/* NUL byte */
947 		BIO_printf(bio_err, "out of memory\n");
948 		goto end;
949 	}
950 	verify_depth = 0;
951 
952 	if (options_parse(argc, argv, s_client_options, NULL, NULL) != 0) {
953 		badop = 1;
954 		goto bad;
955 	}
956 	if (cfg.proxy != NULL) {
957 		if (!extract_host_port(cfg.proxy,
958 		    &cfg.host, NULL, &cfg.port))
959 			goto bad;
960 		if (cfg.connect == NULL)
961 			cfg.connect = SSL_HOST_NAME;
962 	} else if (cfg.connect != NULL) {
963 		if (!extract_host_port(cfg.connect,
964 		    &cfg.host, NULL, &cfg.port))
965 			goto bad;
966 	}
967 	if (badop) {
968  bad:
969 		if (cfg.errstr == NULL)
970 			sc_usage();
971 		goto end;
972 	}
973 
974 	if (!app_passwd(bio_err, cfg.passarg, NULL, &pass, NULL)) {
975 		BIO_printf(bio_err, "Error getting password\n");
976 		goto end;
977 	}
978 	if (cfg.key_file == NULL)
979 		cfg.key_file = cfg.cert_file;
980 
981 
982 	if (cfg.key_file) {
983 
984 		key = load_key(bio_err, cfg.key_file,
985 		    cfg.key_format, 0, pass,
986 		    "client certificate private key file");
987 		if (!key) {
988 			ERR_print_errors(bio_err);
989 			goto end;
990 		}
991 	}
992 	if (cfg.cert_file) {
993 		cert = load_cert(bio_err, cfg.cert_file,
994 		    cfg.cert_format,
995 		    NULL, "client certificate file");
996 
997 		if (!cert) {
998 			ERR_print_errors(bio_err);
999 			goto end;
1000 		}
1001 	}
1002 	if (cfg.quiet && !cfg.debug &&
1003 	    !cfg.msg) {
1004 		if ((bio_c_out = BIO_new(BIO_s_null())) == NULL)
1005 			goto end;
1006 	} else {
1007 		if ((bio_c_out = BIO_new_fp(stdout, BIO_NOCLOSE)) == NULL)
1008 			goto end;
1009 	}
1010 
1011 	ctx = SSL_CTX_new(cfg.meth);
1012 	if (ctx == NULL) {
1013 		ERR_print_errors(bio_err);
1014 		goto end;
1015 	}
1016 
1017 	SSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY);
1018 
1019 	if (cfg.vpm)
1020 		SSL_CTX_set1_param(ctx, cfg.vpm);
1021 
1022 	if (!SSL_CTX_set_min_proto_version(ctx, cfg.min_version))
1023 		goto end;
1024 	if (!SSL_CTX_set_max_proto_version(ctx, cfg.max_version))
1025 		goto end;
1026 
1027 #ifndef OPENSSL_NO_SRTP
1028 	if (cfg.srtp_profiles != NULL)
1029 		SSL_CTX_set_tlsext_use_srtp(ctx, cfg.srtp_profiles);
1030 #endif
1031 	if (cfg.bugs)
1032 		SSL_CTX_set_options(ctx, SSL_OP_ALL | cfg.off);
1033 	else
1034 		SSL_CTX_set_options(ctx, cfg.off);
1035 
1036 	if (cfg.clr)
1037 		SSL_CTX_clear_options(ctx, cfg.clr);
1038 
1039 	if (cfg.alpn_in) {
1040 		unsigned short alpn_len;
1041 		unsigned char *alpn;
1042 
1043 		alpn = next_protos_parse(&alpn_len, cfg.alpn_in);
1044 		if (alpn == NULL) {
1045 			BIO_printf(bio_err, "Error parsing -alpn argument\n");
1046 			goto end;
1047 		}
1048 		SSL_CTX_set_alpn_protos(ctx, alpn, alpn_len);
1049 		free(alpn);
1050 	}
1051 	if (cfg.groups_in != NULL) {
1052 		if (SSL_CTX_set1_groups_list(ctx, cfg.groups_in) != 1) {
1053 			BIO_printf(bio_err, "Failed to set groups '%s'\n",
1054 			    cfg.groups_in);
1055 			goto end;
1056 		}
1057 	}
1058 
1059 	if (cfg.state)
1060 		SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback);
1061 	if (cfg.cipher != NULL)
1062 		if (!SSL_CTX_set_cipher_list(ctx, cfg.cipher)) {
1063 			BIO_printf(bio_err, "error setting cipher list\n");
1064 			ERR_print_errors(bio_err);
1065 			goto end;
1066 		}
1067 
1068 	SSL_CTX_set_verify(ctx, cfg.verify, verify_callback);
1069 	if (!set_cert_key_stuff(ctx, cert, key))
1070 		goto end;
1071 
1072 	if ((cfg.CAfile || cfg.CApath)
1073 	    && !SSL_CTX_load_verify_locations(ctx, cfg.CAfile,
1074 	    cfg.CApath))
1075 		ERR_print_errors(bio_err);
1076 
1077 	if (!SSL_CTX_set_default_verify_paths(ctx))
1078 		ERR_print_errors(bio_err);
1079 
1080 	con = SSL_new(ctx);
1081 	if (cfg.sess_in) {
1082 		SSL_SESSION *sess;
1083 		BIO *stmp = BIO_new_file(cfg.sess_in, "r");
1084 		if (!stmp) {
1085 			BIO_printf(bio_err, "Can't open session file %s\n",
1086 			    cfg.sess_in);
1087 			ERR_print_errors(bio_err);
1088 			goto end;
1089 		}
1090 		sess = PEM_read_bio_SSL_SESSION(stmp, NULL, 0, NULL);
1091 		BIO_free(stmp);
1092 		if (!sess) {
1093 			BIO_printf(bio_err, "Can't open session file %s\n",
1094 			    cfg.sess_in);
1095 			ERR_print_errors(bio_err);
1096 			goto end;
1097 		}
1098 		SSL_set_session(con, sess);
1099 		SSL_SESSION_free(sess);
1100 	}
1101 
1102 	/* Attempt to opportunistically use the host name for SNI. */
1103 	servername = cfg.servername;
1104 	if (servername == NULL)
1105 		servername = cfg.host;
1106 
1107 	if (!cfg.no_servername && servername != NULL &&
1108 	    !SSL_set_tlsext_host_name(con, servername)) {
1109 		long ssl_err = ERR_peek_error();
1110 
1111 		if (cfg.servername != NULL ||
1112 		    ERR_GET_LIB(ssl_err) != ERR_LIB_SSL ||
1113 		    ERR_GET_REASON(ssl_err) != SSL_R_SSL3_EXT_INVALID_SERVERNAME) {
1114 			BIO_printf(bio_err,
1115 			    "Unable to set TLS servername extension.\n");
1116 			ERR_print_errors(bio_err);
1117 			goto end;
1118 		}
1119 		servername = NULL;
1120 		ERR_clear_error();
1121 	}
1122 	if (!cfg.no_servername && servername != NULL) {
1123 		tlsextcbp.biodebug = bio_err;
1124 		SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
1125 		SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
1126 	}
1127 
1128  re_start:
1129 
1130 	if (init_client(&s, cfg.host, cfg.port,
1131 	    cfg.socket_type, cfg.af) == 0) {
1132 		BIO_printf(bio_err, "connect:errno=%d\n", errno);
1133 		goto end;
1134 	}
1135 	BIO_printf(bio_c_out, "CONNECTED(%08X)\n", s);
1136 
1137 	if (cfg.nbio) {
1138 		if (!cfg.quiet)
1139 			BIO_printf(bio_c_out, "turning on non blocking io\n");
1140 		if (!BIO_socket_nbio(s, 1)) {
1141 			ERR_print_errors(bio_err);
1142 			goto end;
1143 		}
1144 	}
1145 	if (cfg.pause & 0x01)
1146 		SSL_set_debug(con, 1);
1147 
1148 	if (SSL_is_dtls(con)) {
1149 		sbio = BIO_new_dgram(s, BIO_NOCLOSE);
1150 		if (getsockname(s, (struct sockaddr *)&peer,
1151 		    (void *)&peerlen) == -1) {
1152 			BIO_printf(bio_err, "getsockname:errno=%d\n",
1153 			    errno);
1154 			shutdown(s, SHUT_RD);
1155 			close(s);
1156 			goto end;
1157 		}
1158 		(void) BIO_ctrl_set_connected(sbio, 1, &peer);
1159 
1160 		if (cfg.enable_timeouts) {
1161 			timeout.tv_sec = 0;
1162 			timeout.tv_usec = DGRAM_RCV_TIMEOUT;
1163 			BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0,
1164 			    &timeout);
1165 
1166 			timeout.tv_sec = 0;
1167 			timeout.tv_usec = DGRAM_SND_TIMEOUT;
1168 			BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0,
1169 			    &timeout);
1170 		}
1171 		if (cfg.socket_mtu > 28) {
1172 			SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
1173 			SSL_set_mtu(con, cfg.socket_mtu - 28);
1174 		} else
1175 			/* want to do MTU discovery */
1176 			BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
1177 	} else
1178 		sbio = BIO_new_socket(s, BIO_NOCLOSE);
1179 
1180 	if (cfg.nbio_test) {
1181 		BIO *test;
1182 
1183 		test = BIO_new(BIO_f_nbio_test());
1184 		sbio = BIO_push(test, sbio);
1185 	}
1186 	if (cfg.debug) {
1187 		SSL_set_debug(con, 1);
1188 		BIO_set_callback(sbio, bio_dump_callback);
1189 		BIO_set_callback_arg(sbio, (char *) bio_c_out);
1190 	}
1191 	if (cfg.msg) {
1192 		SSL_set_msg_callback(con, msg_cb);
1193 		SSL_set_msg_callback_arg(con, bio_c_out);
1194 	}
1195 	if (cfg.tlsextdebug) {
1196 		SSL_set_tlsext_debug_callback(con, tlsext_cb);
1197 		SSL_set_tlsext_debug_arg(con, bio_c_out);
1198 	}
1199 	if (cfg.status_req) {
1200 		SSL_set_tlsext_status_type(con, TLSEXT_STATUSTYPE_ocsp);
1201 		SSL_CTX_set_tlsext_status_cb(ctx, ocsp_resp_cb);
1202 		SSL_CTX_set_tlsext_status_arg(ctx, bio_c_out);
1203 	}
1204 
1205 	SSL_set_bio(con, sbio, sbio);
1206 	SSL_set_connect_state(con);
1207 
1208 	/* ok, lets connect */
1209 	read_tty = 1;
1210 	write_tty = 0;
1211 	tty_on = 0;
1212 	read_ssl = 1;
1213 	write_ssl = 1;
1214 
1215 	cbuf_len = 0;
1216 	cbuf_off = 0;
1217 	sbuf_len = 0;
1218 	sbuf_off = 0;
1219 
1220 	/* This is an ugly hack that does a lot of assumptions */
1221 	/*
1222 	 * We do have to handle multi-line responses which may come in a
1223 	 * single packet or not. We therefore have to use BIO_gets() which
1224 	 * does need a buffering BIO. So during the initial chitchat we do
1225 	 * push a buffering BIO into the chain that is removed again later on
1226 	 * to not disturb the rest of the s_client operation.
1227 	 */
1228 	if (cfg.starttls_proto == PROTO_SMTP ||
1229 	    cfg.starttls_proto == PROTO_LMTP) {
1230 		int foundit = 0;
1231 		BIO *fbio = BIO_new(BIO_f_buffer());
1232 		BIO_push(fbio, sbio);
1233 		/* wait for multi-line response to end from SMTP */
1234 		do {
1235 			mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
1236 		}
1237 		while (mbuf_len > 3 && mbuf[3] == '-');
1238 		/* STARTTLS command requires EHLO... */
1239 		BIO_printf(fbio, "%cHLO openssl.client.net\r\n",
1240 		    cfg.starttls_proto == PROTO_SMTP ? 'E' : 'L');
1241 		(void) BIO_flush(fbio);
1242 		/* wait for multi-line response to end EHLO SMTP response */
1243 		do {
1244 			mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
1245 			if (strstr(mbuf, "STARTTLS"))
1246 				foundit = 1;
1247 		}
1248 		while (mbuf_len > 3 && mbuf[3] == '-');
1249 		(void) BIO_flush(fbio);
1250 		BIO_pop(fbio);
1251 		BIO_free(fbio);
1252 		if (!foundit)
1253 			BIO_printf(bio_err,
1254 			    "didn't find starttls in server response,"
1255 			    " try anyway...\n");
1256 		BIO_printf(sbio, "STARTTLS\r\n");
1257 		BIO_read(sbio, sbuf, BUFSIZZ);
1258 	} else if (cfg.starttls_proto == PROTO_POP3) {
1259 		mbuf_len = BIO_read(sbio, mbuf, BUFSIZZ);
1260 		if (mbuf_len == -1) {
1261 			BIO_printf(bio_err, "BIO_read failed\n");
1262 			goto end;
1263 		}
1264 		BIO_printf(sbio, "STLS\r\n");
1265 		BIO_read(sbio, sbuf, BUFSIZZ);
1266 	} else if (cfg.starttls_proto == PROTO_IMAP) {
1267 		int foundit = 0;
1268 		BIO *fbio = BIO_new(BIO_f_buffer());
1269 		BIO_push(fbio, sbio);
1270 		BIO_gets(fbio, mbuf, BUFSIZZ);
1271 		/* STARTTLS command requires CAPABILITY... */
1272 		BIO_printf(fbio, ". CAPABILITY\r\n");
1273 		(void) BIO_flush(fbio);
1274 		/* wait for multi-line CAPABILITY response */
1275 		do {
1276 			mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
1277 			if (strstr(mbuf, "STARTTLS"))
1278 				foundit = 1;
1279 		}
1280 		while (mbuf_len > 3 && mbuf[0] != '.');
1281 		(void) BIO_flush(fbio);
1282 		BIO_pop(fbio);
1283 		BIO_free(fbio);
1284 		if (!foundit)
1285 			BIO_printf(bio_err,
1286 			    "didn't find STARTTLS in server response,"
1287 			    " try anyway...\n");
1288 		BIO_printf(sbio, ". STARTTLS\r\n");
1289 		BIO_read(sbio, sbuf, BUFSIZZ);
1290 	} else if (cfg.starttls_proto == PROTO_FTP) {
1291 		BIO *fbio = BIO_new(BIO_f_buffer());
1292 		BIO_push(fbio, sbio);
1293 		/* wait for multi-line response to end from FTP */
1294 		do {
1295 			mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
1296 		}
1297 		while (mbuf_len > 3 && mbuf[3] == '-');
1298 		(void) BIO_flush(fbio);
1299 		BIO_pop(fbio);
1300 		BIO_free(fbio);
1301 		BIO_printf(sbio, "AUTH TLS\r\n");
1302 		BIO_read(sbio, sbuf, BUFSIZZ);
1303 	} else if (cfg.starttls_proto == PROTO_XMPP) {
1304 		int seen = 0;
1305 		BIO_printf(sbio, "<stream:stream "
1306 		    "xmlns:stream='http://etherx.jabber.org/streams' "
1307 		    "xmlns='jabber:client' to='%s' version='1.0'>",
1308 		    cfg.xmpphost ?
1309 		    cfg.xmpphost : cfg.host);
1310 		seen = BIO_read(sbio, mbuf, BUFSIZZ);
1311 
1312 		if (seen <= 0)
1313 			goto shut;
1314 
1315 		mbuf[seen] = 0;
1316 		while (!strstr(mbuf, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'") &&
1317 		       !strstr(mbuf, "<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"")) {
1318 			seen = BIO_read(sbio, mbuf, BUFSIZZ);
1319 
1320 			if (seen <= 0)
1321 				goto shut;
1322 
1323 			mbuf[seen] = 0;
1324 		}
1325 		BIO_printf(sbio,
1326 		    "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
1327 		seen = BIO_read(sbio, sbuf, BUFSIZZ);
1328 		sbuf[seen] = 0;
1329 		if (!strstr(sbuf, "<proceed"))
1330 			goto shut;
1331 		mbuf[0] = 0;
1332 	} else if (cfg.proxy != NULL) {
1333 		BIO_printf(sbio, "CONNECT %s HTTP/1.0\r\n\r\n",
1334 		    cfg.connect);
1335 		mbuf_len = BIO_read(sbio, mbuf, BUFSIZZ);
1336 		if (mbuf_len == -1) {
1337 			BIO_printf(bio_err, "BIO_read failed\n");
1338 			goto end;
1339 		}
1340 	}
1341 	for (;;) {
1342 		struct pollfd pfd[3];	/* stdin, stdout, socket */
1343 		int ptimeout = -1;
1344 
1345 		if (SSL_is_dtls(con) && DTLSv1_get_timeout(con, &timeout))
1346 			ptimeout = timeout.tv_sec * 1000 +
1347 			    timeout.tv_usec / 1000;
1348 
1349 		if (SSL_in_init(con) && !SSL_total_renegotiations(con)) {
1350 			in_init = 1;
1351 			tty_on = 0;
1352 		} else {
1353 			tty_on = 1;
1354 			if (in_init) {
1355 				in_init = 0;
1356 				if (cfg.sess_out) {
1357 					BIO *stmp = BIO_new_file(
1358 					    cfg.sess_out, "w");
1359 					if (stmp) {
1360 						PEM_write_bio_SSL_SESSION(stmp,
1361 						    SSL_get_session(con));
1362 						BIO_free(stmp);
1363 					} else
1364 						BIO_printf(bio_err,
1365 						    "Error writing session file %s\n",
1366 						    cfg.sess_out);
1367 				}
1368 				print_stuff(bio_c_out, con, full_log);
1369 				if (full_log > 0)
1370 					full_log--;
1371 
1372 				if (cfg.starttls_proto) {
1373 					BIO_write(bio_err, mbuf, mbuf_len);
1374 					/* We don't need to know any more */
1375 					cfg.starttls_proto = PROTO_OFF;
1376 				}
1377 				if (cfg.reconnect) {
1378 					cfg.reconnect--;
1379 					BIO_printf(bio_c_out,
1380 					    "drop connection and then reconnect\n");
1381 					SSL_shutdown(con);
1382 					SSL_set_connect_state(con);
1383 					shutdown(SSL_get_fd(con), SHUT_RD);
1384 					close(SSL_get_fd(con));
1385 					goto re_start;
1386 				}
1387 			}
1388 		}
1389 
1390 		ssl_pending = read_ssl && SSL_pending(con);
1391 
1392 		pfd[0].fd = -1;
1393 		pfd[1].fd = -1;
1394 		if (!ssl_pending) {
1395 			if (tty_on) {
1396 				if (read_tty) {
1397 					pfd[0].fd = fileno(stdin);
1398 					pfd[0].events = POLLIN;
1399 				}
1400 				if (write_tty) {
1401 					pfd[1].fd = fileno(stdout);
1402 					pfd[1].events = POLLOUT;
1403 				}
1404 			}
1405 
1406 			pfd[2].fd = SSL_get_fd(con);
1407 			pfd[2].events = 0;
1408 			if (read_ssl)
1409 				pfd[2].events |= POLLIN;
1410 			if (write_ssl)
1411 				pfd[2].events |= POLLOUT;
1412 
1413 /*			printf("mode tty(%d %d%d) ssl(%d%d)\n",
1414 				tty_on,read_tty,write_tty,read_ssl,write_ssl);*/
1415 
1416 			i = poll(pfd, 3, ptimeout);
1417 			if (i == -1) {
1418 				BIO_printf(bio_err, "bad select %d\n",
1419 				    errno);
1420 				goto shut;
1421 				/* goto end; */
1422 			}
1423 		}
1424 		if (SSL_is_dtls(con) &&
1425 		    DTLSv1_handle_timeout(con) > 0)
1426 			BIO_printf(bio_err, "TIMEOUT occured\n");
1427 		if (!ssl_pending &&
1428 		    (pfd[2].revents & (POLLOUT|POLLERR|POLLNVAL))) {
1429 			if (pfd[2].revents & (POLLERR|POLLNVAL)) {
1430 				BIO_printf(bio_err, "poll error");
1431 				goto shut;
1432 			}
1433 			k = SSL_write(con, &(cbuf[cbuf_off]),
1434 			    (unsigned int) cbuf_len);
1435 			switch (SSL_get_error(con, k)) {
1436 			case SSL_ERROR_NONE:
1437 				cbuf_off += k;
1438 				cbuf_len -= k;
1439 				if (k <= 0)
1440 					goto end;
1441 				/* we have done a  write(con,NULL,0); */
1442 				if (cbuf_len <= 0) {
1443 					read_tty = 1;
1444 					write_ssl = 0;
1445 				} else {	/* if (cbuf_len > 0) */
1446 					read_tty = 0;
1447 					write_ssl = 1;
1448 				}
1449 				break;
1450 			case SSL_ERROR_WANT_WRITE:
1451 				BIO_printf(bio_c_out, "write W BLOCK\n");
1452 				write_ssl = 1;
1453 				read_tty = 0;
1454 				break;
1455 			case SSL_ERROR_WANT_READ:
1456 				BIO_printf(bio_c_out, "write R BLOCK\n");
1457 				write_tty = 0;
1458 				read_ssl = 1;
1459 				write_ssl = 0;
1460 				break;
1461 			case SSL_ERROR_WANT_X509_LOOKUP:
1462 				BIO_printf(bio_c_out, "write X BLOCK\n");
1463 				break;
1464 			case SSL_ERROR_ZERO_RETURN:
1465 				if (cbuf_len != 0) {
1466 					BIO_printf(bio_c_out, "shutdown\n");
1467 					ret = 0;
1468 					goto shut;
1469 				} else {
1470 					read_tty = 1;
1471 					write_ssl = 0;
1472 					break;
1473 				}
1474 
1475 			case SSL_ERROR_SYSCALL:
1476 				if ((k != 0) || (cbuf_len != 0)) {
1477 					BIO_printf(bio_err, "write:errno=%d\n",
1478 					    errno);
1479 					goto shut;
1480 				} else {
1481 					read_tty = 1;
1482 					write_ssl = 0;
1483 				}
1484 				break;
1485 			case SSL_ERROR_SSL:
1486 				ERR_print_errors(bio_err);
1487 				goto shut;
1488 			}
1489 		} else if (!ssl_pending &&
1490 		    (pfd[1].revents & (POLLOUT|POLLERR|POLLNVAL))) {
1491 			if (pfd[1].revents & (POLLERR|POLLNVAL)) {
1492 				BIO_printf(bio_err, "poll error");
1493 				goto shut;
1494 			}
1495 			i = write(fileno(stdout), &(sbuf[sbuf_off]), sbuf_len);
1496 
1497 			if (i <= 0) {
1498 				BIO_printf(bio_c_out, "DONE\n");
1499 				ret = 0;
1500 				goto shut;
1501 				/* goto end; */
1502 			}
1503 			sbuf_len -= i;
1504 			sbuf_off += i;
1505 			if (sbuf_len <= 0) {
1506 				read_ssl = 1;
1507 				write_tty = 0;
1508 			}
1509 		} else if (ssl_pending || (pfd[2].revents & (POLLIN|POLLHUP))) {
1510 #ifdef RENEG
1511 			{
1512 				static int iiii;
1513 				if (++iiii == 52) {
1514 					SSL_renegotiate(con);
1515 					iiii = 0;
1516 				}
1517 			}
1518 #endif
1519 			if (cfg.peekaboo) {
1520 				k = p = SSL_peek(con, pbuf, 1024 /* BUFSIZZ */ );
1521 				pending = SSL_pending(con);
1522 				if (SSL_get_error(con, p) == SSL_ERROR_NONE) {
1523 					if (p <= 0)
1524 						goto end;
1525 
1526 					k = SSL_read(con, sbuf, p);
1527 				}
1528 			} else {
1529 				k = SSL_read(con, sbuf, 1024 /* BUFSIZZ */ );
1530 			}
1531 
1532 			switch (SSL_get_error(con, k)) {
1533 			case SSL_ERROR_NONE:
1534 				if (k <= 0)
1535 					goto end;
1536 				sbuf_off = 0;
1537 				sbuf_len = k;
1538 				if (cfg.peekaboo) {
1539 					if (p != pending) {
1540 						ret = -1;
1541 						BIO_printf(bio_err,
1542 						    "peeked %d but pending %d!\n",
1543 						    p, pending);
1544 						goto shut;
1545 					}
1546 					if (k < p) {
1547 						ret = -1;
1548 						BIO_printf(bio_err,
1549 						    "read less than peek!\n");
1550 						goto shut;
1551 					}
1552 					if (p > 0 &&
1553 					    (memcmp(sbuf, pbuf, p) != 0)) {
1554 						ret = -1;
1555 						BIO_printf(bio_err,
1556 						    "peek of %d different from read of %d!\n",
1557 						    p, k);
1558 						goto shut;
1559 					}
1560 				}
1561 				read_ssl = 0;
1562 				write_tty = 1;
1563 				break;
1564 			case SSL_ERROR_WANT_WRITE:
1565 				BIO_printf(bio_c_out, "read W BLOCK\n");
1566 				write_ssl = 1;
1567 				read_tty = 0;
1568 				break;
1569 			case SSL_ERROR_WANT_READ:
1570 				BIO_printf(bio_c_out, "read R BLOCK\n");
1571 				write_tty = 0;
1572 				read_ssl = 1;
1573 				if ((read_tty == 0) && (write_ssl == 0))
1574 					write_ssl = 1;
1575 				break;
1576 			case SSL_ERROR_WANT_X509_LOOKUP:
1577 				BIO_printf(bio_c_out, "read X BLOCK\n");
1578 				break;
1579 			case SSL_ERROR_SYSCALL:
1580 				ret = errno;
1581 				BIO_printf(bio_err, "read:errno=%d\n", ret);
1582 				goto shut;
1583 			case SSL_ERROR_ZERO_RETURN:
1584 				BIO_printf(bio_c_out, "closed\n");
1585 				ret = 0;
1586 				goto shut;
1587 			case SSL_ERROR_SSL:
1588 				ERR_print_errors(bio_err);
1589 				goto shut;
1590 				/* break; */
1591 			}
1592 		} else if (pfd[0].revents) {
1593 			if (pfd[0].revents & (POLLERR|POLLNVAL)) {
1594 				BIO_printf(bio_err, "poll error");
1595 				goto shut;
1596 			}
1597 			if (cfg.crlf) {
1598 				int j, lf_num;
1599 
1600 				i = read(fileno(stdin), cbuf, BUFSIZZ / 2);
1601 				lf_num = 0;
1602 				/* both loops are skipped when i <= 0 */
1603 				for (j = 0; j < i; j++)
1604 					if (cbuf[j] == '\n')
1605 						lf_num++;
1606 				for (j = i - 1; j >= 0; j--) {
1607 					cbuf[j + lf_num] = cbuf[j];
1608 					if (cbuf[j] == '\n') {
1609 						lf_num--;
1610 						i++;
1611 						cbuf[j + lf_num] = '\r';
1612 					}
1613 				}
1614 				assert(lf_num == 0);
1615 			} else
1616 				i = read(fileno(stdin), cbuf, BUFSIZZ);
1617 
1618 			if ((!cfg.ign_eof) &&
1619 			    ((i <= 0) || (cbuf[0] == 'Q'))) {
1620 				BIO_printf(bio_err, "DONE\n");
1621 				ret = 0;
1622 				goto shut;
1623 			}
1624 			if ((!cfg.ign_eof) && (cbuf[0] == 'R')) {
1625 				BIO_printf(bio_err, "RENEGOTIATING\n");
1626 				SSL_renegotiate(con);
1627 				cbuf_len = 0;
1628 			} else {
1629 				cbuf_len = i;
1630 				cbuf_off = 0;
1631 			}
1632 
1633 			write_ssl = 1;
1634 			read_tty = 0;
1635 		}
1636 	}
1637 
1638 	ret = 0;
1639  shut:
1640 	if (in_init)
1641 		print_stuff(bio_c_out, con, full_log);
1642 	SSL_shutdown(con);
1643 	shutdown(SSL_get_fd(con), SHUT_RD);
1644 	close(SSL_get_fd(con));
1645  end:
1646 	if (con != NULL) {
1647 		if (cfg.prexit != 0)
1648 			print_stuff(bio_c_out, con, 1);
1649 		SSL_free(con);
1650 	}
1651 	SSL_CTX_free(ctx);
1652 	X509_free(cert);
1653 	EVP_PKEY_free(key);
1654 	free(pass);
1655 	X509_VERIFY_PARAM_free(cfg.vpm);
1656 	freezero(cbuf, BUFSIZZ);
1657 	freezero(sbuf, BUFSIZZ);
1658 	freezero(pbuf, BUFSIZZ);
1659 	freezero(mbuf, BUFSIZZ);
1660 	BIO_free(bio_c_out);
1661 
1662 	return (ret);
1663 }
1664 
1665 static void
1666 print_stuff(BIO *bio, SSL *s, int full)
1667 {
1668 	X509 *peer = NULL;
1669 	char *p;
1670 	static const char *space = "                ";
1671 	char buf[BUFSIZ];
1672 	STACK_OF(X509) *sk;
1673 	STACK_OF(X509_NAME) *sk2;
1674 	const SSL_CIPHER *c;
1675 	X509_NAME *xn;
1676 	int j, i;
1677 	unsigned char *exportedkeymat;
1678 
1679 	if (full) {
1680 		int got_a_chain = 0;
1681 
1682 		sk = SSL_get_peer_cert_chain(s);
1683 		if (sk != NULL) {
1684 			got_a_chain = 1;	/* we don't have it for SSL2
1685 						 * (yet) */
1686 
1687 			BIO_printf(bio, "---\nCertificate chain\n");
1688 			for (i = 0; i < sk_X509_num(sk); i++) {
1689 				X509_NAME_oneline(X509_get_subject_name(
1690 					sk_X509_value(sk, i)), buf, sizeof buf);
1691 				BIO_printf(bio, "%2d s:%s\n", i, buf);
1692 				X509_NAME_oneline(X509_get_issuer_name(
1693 					sk_X509_value(sk, i)), buf, sizeof buf);
1694 				BIO_printf(bio, "   i:%s\n", buf);
1695 				if (cfg.showcerts)
1696 					PEM_write_bio_X509(bio,
1697 					    sk_X509_value(sk, i));
1698 			}
1699 		}
1700 		BIO_printf(bio, "---\n");
1701 		peer = SSL_get_peer_certificate(s);
1702 		if (peer != NULL) {
1703 			BIO_printf(bio, "Server certificate\n");
1704 			if (!(cfg.showcerts && got_a_chain)) {
1705 				/* Redundant if we showed the whole chain */
1706 				PEM_write_bio_X509(bio, peer);
1707 			}
1708 			X509_NAME_oneline(X509_get_subject_name(peer),
1709 			    buf, sizeof buf);
1710 			BIO_printf(bio, "subject=%s\n", buf);
1711 			X509_NAME_oneline(X509_get_issuer_name(peer),
1712 			    buf, sizeof buf);
1713 			BIO_printf(bio, "issuer=%s\n", buf);
1714 		} else
1715 			BIO_printf(bio, "no peer certificate available\n");
1716 
1717 		sk2 = SSL_get_client_CA_list(s);
1718 		if ((sk2 != NULL) && (sk_X509_NAME_num(sk2) > 0)) {
1719 			BIO_printf(bio,
1720 			    "---\nAcceptable client certificate CA names\n");
1721 			for (i = 0; i < sk_X509_NAME_num(sk2); i++) {
1722 				xn = sk_X509_NAME_value(sk2, i);
1723 				X509_NAME_oneline(xn, buf, sizeof(buf));
1724 				BIO_write(bio, buf, strlen(buf));
1725 				BIO_write(bio, "\n", 1);
1726 			}
1727 		} else {
1728 			BIO_printf(bio,
1729 			    "---\nNo client certificate CA names sent\n");
1730 		}
1731 		p = SSL_get_shared_ciphers(s, buf, sizeof buf);
1732 		if (p != NULL) {
1733 			/*
1734 			 * This works only for SSL 2.  In later protocol
1735 			 * versions, the client does not know what other
1736 			 * ciphers (in addition to the one to be used in the
1737 			 * current connection) the server supports.
1738 			 */
1739 
1740 			BIO_printf(bio,
1741 			    "---\nCiphers common between both SSL endpoints:\n");
1742 			j = i = 0;
1743 			while (*p) {
1744 				if (*p == ':') {
1745 					BIO_write(bio, space, 15 - j % 25);
1746 					i++;
1747 					j = 0;
1748 					BIO_write(bio,
1749 					    ((i % 3) ? " " : "\n"), 1);
1750 				} else {
1751 					BIO_write(bio, p, 1);
1752 					j++;
1753 				}
1754 				p++;
1755 			}
1756 			BIO_write(bio, "\n", 1);
1757 		}
1758 
1759 		ssl_print_tmp_key(bio, s);
1760 
1761 		BIO_printf(bio,
1762 		    "---\nSSL handshake has read %ld bytes and written %ld bytes\n",
1763 		    BIO_number_read(SSL_get_rbio(s)),
1764 		    BIO_number_written(SSL_get_wbio(s)));
1765 	}
1766 	BIO_printf(bio, (SSL_cache_hit(s) ? "---\nReused, " : "---\nNew, "));
1767 	c = SSL_get_current_cipher(s);
1768 	BIO_printf(bio, "%s, Cipher is %s\n",
1769 	    SSL_CIPHER_get_version(c),
1770 	    SSL_CIPHER_get_name(c));
1771 	if (peer != NULL) {
1772 		EVP_PKEY *pktmp;
1773 
1774 		pktmp = X509_get0_pubkey(peer);
1775 		BIO_printf(bio, "Server public key is %d bit\n",
1776 		    EVP_PKEY_bits(pktmp));
1777 	}
1778 	BIO_printf(bio, "Secure Renegotiation IS%s supported\n",
1779 	    SSL_get_secure_renegotiation_support(s) ? "" : " NOT");
1780 
1781 	/* Compression is not supported and will always be none. */
1782 	BIO_printf(bio, "Compression: NONE\n");
1783 	BIO_printf(bio, "Expansion: NONE\n");
1784 
1785 #ifdef SSL_DEBUG
1786 	{
1787 		/* Print out local port of connection: useful for debugging */
1788 		int sock;
1789 		struct sockaddr_in ladd;
1790 		socklen_t ladd_size = sizeof(ladd);
1791 		sock = SSL_get_fd(s);
1792 		getsockname(sock, (struct sockaddr *) & ladd, &ladd_size);
1793 		BIO_printf(bio, "LOCAL PORT is %u\n",
1794 		    ntohs(ladd.sin_port));
1795 	}
1796 #endif
1797 
1798 	{
1799 		const unsigned char *proto;
1800 		unsigned int proto_len;
1801 		SSL_get0_alpn_selected(s, &proto, &proto_len);
1802 		if (proto_len > 0) {
1803 			BIO_printf(bio, "ALPN protocol: ");
1804 			BIO_write(bio, proto, proto_len);
1805 			BIO_write(bio, "\n", 1);
1806 		} else
1807 			BIO_printf(bio, "No ALPN negotiated\n");
1808 	}
1809 
1810 #ifndef OPENSSL_NO_SRTP
1811 	{
1812 		SRTP_PROTECTION_PROFILE *srtp_profile;
1813 
1814 		srtp_profile = SSL_get_selected_srtp_profile(s);
1815 		if (srtp_profile)
1816 			BIO_printf(bio,
1817 			    "SRTP Extension negotiated, profile=%s\n",
1818 			    srtp_profile->name);
1819 	}
1820 #endif
1821 
1822 	SSL_SESSION_print(bio, SSL_get_session(s));
1823 	if (cfg.keymatexportlabel != NULL) {
1824 		BIO_printf(bio, "Keying material exporter:\n");
1825 		BIO_printf(bio, "    Label: '%s'\n",
1826 		    cfg.keymatexportlabel);
1827 		BIO_printf(bio, "    Length: %i bytes\n",
1828 		    cfg.keymatexportlen);
1829 		exportedkeymat = malloc(cfg.keymatexportlen);
1830 		if (exportedkeymat != NULL) {
1831 			if (!SSL_export_keying_material(s, exportedkeymat,
1832 				cfg.keymatexportlen,
1833 				cfg.keymatexportlabel,
1834 				strlen(cfg.keymatexportlabel),
1835 				NULL, 0, 0)) {
1836 				BIO_printf(bio, "    Error\n");
1837 			} else {
1838 				BIO_printf(bio, "    Keying material: ");
1839 				for (i = 0; i < cfg.keymatexportlen; i++)
1840 					BIO_printf(bio, "%02X",
1841 					    exportedkeymat[i]);
1842 				BIO_printf(bio, "\n");
1843 			}
1844 			free(exportedkeymat);
1845 		}
1846 	}
1847 	BIO_printf(bio, "---\n");
1848 	X509_free(peer);
1849 	/* flush, or debugging output gets mixed with http response */
1850 	(void) BIO_flush(bio);
1851 }
1852 
1853 static int
1854 ocsp_resp_cb(SSL *s, void *arg)
1855 {
1856 	const unsigned char *p;
1857 	int len;
1858 	OCSP_RESPONSE *rsp;
1859 	len = SSL_get_tlsext_status_ocsp_resp(s, &p);
1860 	BIO_puts(arg, "OCSP response: ");
1861 	if (!p) {
1862 		BIO_puts(arg, "no response sent\n");
1863 		return 1;
1864 	}
1865 	rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
1866 	if (!rsp) {
1867 		BIO_puts(arg, "response parse error\n");
1868 		BIO_dump_indent(arg, (char *) p, len, 4);
1869 		return 0;
1870 	}
1871 	BIO_puts(arg, "\n======================================\n");
1872 	OCSP_RESPONSE_print(arg, rsp, 0);
1873 	BIO_puts(arg, "======================================\n");
1874 	OCSP_RESPONSE_free(rsp);
1875 	return 1;
1876 }
1877 
1878 static int
1879 ssl_servername_cb(SSL *s, int *ad, void *arg)
1880 {
1881 	tlsextctx *p = (tlsextctx *) arg;
1882 	const char *hn = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
1883 	if (SSL_get_servername_type(s) != -1)
1884 		p->ack = !SSL_session_reused(s) && hn != NULL;
1885 	else
1886 		BIO_printf(bio_err, "Can't use SSL_get_servername\n");
1887 
1888 	return SSL_TLSEXT_ERR_OK;
1889 }
1890 
1891