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