Annotation of src/usr.bin/openssl/s_client.c, Revision 1.60
1.60 ! tb 1: /* $OpenBSD: s_client.c,v 1.59 2022/11/11 17:07:39 joshua 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:
1.47 inoguchi 299: #ifndef OPENSSL_NO_DTLS1
300: static int
301: s_client_opt_protocol_version_dtls1(void)
302: {
1.60 ! tb 303: cfg.meth = DTLS_client_method();
! 304: cfg.min_version = DTLS1_VERSION;
! 305: cfg.max_version = DTLS1_VERSION;
! 306: cfg.socket_type = SOCK_DGRAM;
1.53 jsing 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: {
1.60 ! tb 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;
1.47 inoguchi 319: return (0);
320: }
321: #endif
322:
323: static int
324: s_client_opt_protocol_version_tls1(void)
325: {
1.60 ! tb 326: cfg.min_version = TLS1_VERSION;
! 327: cfg.max_version = TLS1_VERSION;
1.47 inoguchi 328: return (0);
329: }
330:
331: static int
332: s_client_opt_protocol_version_tls1_1(void)
333: {
1.60 ! tb 334: cfg.min_version = TLS1_1_VERSION;
! 335: cfg.max_version = TLS1_1_VERSION;
1.47 inoguchi 336: return (0);
337: }
338:
339: static int
340: s_client_opt_protocol_version_tls1_2(void)
341: {
1.60 ! tb 342: cfg.min_version = TLS1_2_VERSION;
! 343: cfg.max_version = TLS1_2_VERSION;
1.47 inoguchi 344: return (0);
345: }
346:
347: static int
348: s_client_opt_protocol_version_tls1_3(void)
349: {
1.60 ! tb 350: cfg.min_version = TLS1_3_VERSION;
! 351: cfg.max_version = TLS1_3_VERSION;
1.47 inoguchi 352: return (0);
353: }
354:
355: static int
356: s_client_opt_quiet(void)
357: {
1.60 ! tb 358: cfg.quiet = 1;
! 359: cfg.ign_eof = 1;
1.47 inoguchi 360: return (0);
361: }
362:
363: static int
364: s_client_opt_starttls(char *arg)
365: {
366: if (strcmp(arg, "smtp") == 0)
1.60 ! tb 367: cfg.starttls_proto = PROTO_SMTP;
1.47 inoguchi 368: else if (strcmp(arg, "lmtp") == 0)
1.60 ! tb 369: cfg.starttls_proto = PROTO_LMTP;
1.47 inoguchi 370: else if (strcmp(arg, "pop3") == 0)
1.60 ! tb 371: cfg.starttls_proto = PROTO_POP3;
1.47 inoguchi 372: else if (strcmp(arg, "imap") == 0)
1.60 ! tb 373: cfg.starttls_proto = PROTO_IMAP;
1.47 inoguchi 374: else if (strcmp(arg, "ftp") == 0)
1.60 ! tb 375: cfg.starttls_proto = PROTO_FTP;
1.47 inoguchi 376: else if (strcmp(arg, "xmpp") == 0)
1.60 ! tb 377: cfg.starttls_proto = PROTO_XMPP;
1.47 inoguchi 378: else
379: return (1);
380: return (0);
381: }
382:
383: static int
384: s_client_opt_verify(char *arg)
385: {
1.60 ! tb 386: cfg.verify = SSL_VERIFY_PEER;
1.47 inoguchi 387:
1.60 ! tb 388: verify_depth = strtonum(arg, 0, INT_MAX, &cfg.errstr);
! 389: if (cfg.errstr != NULL) {
1.47 inoguchi 390: BIO_printf(bio_err, "invalid argument %s: %s\n",
1.60 ! tb 391: arg, cfg.errstr);
1.47 inoguchi 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,
1.60 ! tb 406: &cfg.vpm)) {
1.47 inoguchi 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,
1.60 ! tb 422: .opt.value = &cfg.af,
1.47 inoguchi 423: .value = AF_INET,
424: },
425: {
426: .name = "6",
427: .desc = "Use IPv6 only",
428: .type = OPTION_VALUE,
1.60 ! tb 429: .opt.value = &cfg.af,
1.47 inoguchi 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,
1.60 ! tb 438: .opt.arg = &cfg.alpn_in,
1.47 inoguchi 439: },
440: {
441: .name = "bugs",
442: .desc = "Enable various workarounds for buggy implementations",
443: .type = OPTION_FLAG,
1.60 ! tb 444: .opt.flag = &cfg.bugs,
1.47 inoguchi 445: },
446: {
447: .name = "CAfile",
448: .argname = "file",
449: .desc = "PEM format file of CA certificates",
450: .type = OPTION_ARG,
1.60 ! tb 451: .opt.arg = &cfg.CAfile,
1.47 inoguchi 452: },
453: {
454: .name = "CApath",
455: .argname = "directory",
456: .desc = "PEM format directory of CA certificates",
457: .type = OPTION_ARG,
1.60 ! tb 458: .opt.arg = &cfg.CApath,
1.47 inoguchi 459: },
460: {
461: .name = "cert",
462: .argname = "file",
463: .desc = "Certificate file to use, PEM format assumed",
464: .type = OPTION_ARG,
1.60 ! tb 465: .opt.arg = &cfg.cert_file,
1.47 inoguchi 466: },
467: {
468: .name = "certform",
469: .argname = "fmt",
470: .desc = "Certificate format (PEM or DER) PEM default",
471: .type = OPTION_ARG_FORMAT,
1.60 ! tb 472: .opt.value = &cfg.cert_format,
1.47 inoguchi 473: },
474: {
475: .name = "cipher",
476: .argname = "cipherlist",
477: .desc = "Preferred cipher to use (see 'openssl ciphers')",
478: .type = OPTION_ARG,
1.60 ! tb 479: .opt.arg = &cfg.cipher,
1.47 inoguchi 480: },
481: {
482: .name = "connect",
483: .argname = "host:port",
484: .desc = "Who to connect to (default is localhost:4433)",
485: .type = OPTION_ARG,
1.60 ! tb 486: .opt.arg = &cfg.connect,
1.47 inoguchi 487: },
488: {
489: .name = "crlf",
490: .desc = "Convert LF from terminal into CRLF",
491: .type = OPTION_FLAG,
1.60 ! tb 492: .opt.flag = &cfg.crlf,
1.47 inoguchi 493: },
494: {
495: .name = "debug",
496: .desc = "Print extensive debugging information",
497: .type = OPTION_FLAG,
1.60 ! tb 498: .opt.flag = &cfg.debug,
1.47 inoguchi 499: },
1.53 jsing 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
1.47 inoguchi 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
1.53 jsing 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
1.47 inoguchi 524: {
525: .name = "groups",
526: .argname = "list",
527: .desc = "Specify EC groups (colon-separated list)",
528: .type = OPTION_ARG,
1.60 ! tb 529: .opt.arg = &cfg.groups_in,
1.47 inoguchi 530: },
531: {
532: .name = "host",
533: .argname = "host",
534: .desc = "Use -connect instead",
535: .type = OPTION_ARG,
1.60 ! tb 536: .opt.arg = &cfg.host,
1.47 inoguchi 537: },
538: {
539: .name = "ign_eof",
540: .desc = "Ignore input EOF (default when -quiet)",
541: .type = OPTION_VALUE,
1.60 ! tb 542: .opt.value = &cfg.ign_eof,
1.47 inoguchi 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,
1.60 ! tb 550: .opt.arg = &cfg.key_file,
1.47 inoguchi 551: },
552: {
553: .name = "keyform",
554: .argname = "fmt",
555: .desc = "Key format (PEM or DER) PEM default",
556: .type = OPTION_ARG_FORMAT,
1.60 ! tb 557: .opt.value = &cfg.key_format,
1.47 inoguchi 558: },
559: {
560: .name = "keymatexport",
561: .argname = "label",
562: .desc = "Export keying material using label",
563: .type = OPTION_ARG,
1.60 ! tb 564: .opt.arg = &cfg.keymatexportlabel,
1.47 inoguchi 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,
1.60 ! tb 581: .opt.value = &cfg.off,
1.47 inoguchi 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,
1.60 ! tb 588: .opt.flag = &cfg.msg,
1.47 inoguchi 589: },
1.53 jsing 590: #ifndef OPENSSL_NO_DTLS
1.47 inoguchi 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,
1.60 ! tb 603: .opt.flag = &cfg.nbio,
1.47 inoguchi 604: },
605: {
606: .name = "nbio_test",
607: .desc = "Test non-blocking I/O",
608: .type = OPTION_FLAG,
1.60 ! tb 609: .opt.flag = &cfg.nbio_test,
1.47 inoguchi 610: },
611: {
612: .name = "nextprotoneg",
613: .argname = "protocols",
614: .type = OPTION_ARG,
1.60 ! tb 615: .opt.arg = &cfg.npn_in, /* Ignored. */
1.47 inoguchi 616: },
617: {
618: .name = "no_comp",
619: .type = OPTION_VALUE_OR,
1.60 ! tb 620: .opt.value = &cfg.off,
1.47 inoguchi 621: .value = SSL_OP_NO_COMPRESSION,
622: },
623: {
624: .name = "no_ign_eof",
625: .desc = "Don't ignore input EOF",
626: .type = OPTION_VALUE,
1.60 ! tb 627: .opt.value = &cfg.ign_eof,
1.47 inoguchi 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,
1.60 ! tb 634: .opt.value = &cfg.clr,
1.47 inoguchi 635: .value = SSL_OP_LEGACY_SERVER_CONNECT,
636: },
637: {
1.57 jsing 638: .name = "no_servername",
639: .desc = "Do not send a Server Name Indication (SNI) extension",
640: .type = OPTION_FLAG,
1.60 ! tb 641: .opt.value = &cfg.no_servername,
1.57 jsing 642: },
643: {
1.47 inoguchi 644: .name = "no_ssl2",
645: .type = OPTION_VALUE_OR,
1.60 ! tb 646: .opt.value = &cfg.off,
1.47 inoguchi 647: .value = SSL_OP_NO_SSLv2,
648: },
649: {
650: .name = "no_ssl3",
651: .type = OPTION_VALUE_OR,
1.60 ! tb 652: .opt.value = &cfg.off,
1.47 inoguchi 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,
1.60 ! tb 659: .opt.value = &cfg.off,
1.47 inoguchi 660: .value = SSL_OP_NO_TICKET,
661: },
662: {
663: .name = "no_tls1",
664: .desc = "Disable the use of TLSv1",
665: .type = OPTION_VALUE_OR,
1.60 ! tb 666: .opt.value = &cfg.off,
1.47 inoguchi 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,
1.60 ! tb 673: .opt.value = &cfg.off,
1.47 inoguchi 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,
1.60 ! tb 680: .opt.value = &cfg.off,
1.47 inoguchi 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,
1.60 ! tb 687: .opt.value = &cfg.off,
1.47 inoguchi 688: .value = SSL_OP_NO_TLSv1_3,
689: },
690: {
1.57 jsing 691: .name = "noservername",
692: .type = OPTION_FLAG,
1.60 ! tb 693: .opt.value = &cfg.no_servername,
1.57 jsing 694: },
695: {
1.47 inoguchi 696: .name = "pass",
697: .argname = "arg",
698: .desc = "Private key file pass phrase source",
699: .type = OPTION_ARG,
1.60 ! tb 700: .opt.arg = &cfg.passarg,
1.47 inoguchi 701: },
702: {
703: .name = "pause",
704: .desc = "Pause 1 second between each read and write call",
705: .type = OPTION_FLAG,
1.60 ! tb 706: .opt.flag = &cfg.pause,
1.47 inoguchi 707: },
708: {
709: .name = "peekaboo",
710: .type = OPTION_FLAG,
1.60 ! tb 711: .opt.flag = &cfg.peekaboo,
1.47 inoguchi 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,
1.60 ! tb 724: .opt.flag = &cfg.prexit,
1.47 inoguchi 725: },
726: {
727: .name = "proxy",
728: .argname = "host:port",
729: .desc = "Connect to http proxy",
730: .type = OPTION_ARG,
1.60 ! tb 731: .opt.arg = &cfg.proxy,
1.47 inoguchi 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,
1.60 ! tb 743: .opt.value = &cfg.reconnect,
1.47 inoguchi 744: .value = 5,
745: },
746: {
747: .name = "servername",
748: .argname = "name",
749: .desc = "Set TLS extension servername in ClientHello (SNI)",
750: .type = OPTION_ARG,
1.60 ! tb 751: .opt.arg = &cfg.servername,
1.47 inoguchi 752: },
753: {
754: .name = "serverpref",
755: .desc = "Use server's cipher preferences",
756: .type = OPTION_VALUE_OR,
1.60 ! tb 757: .opt.value = &cfg.off,
1.47 inoguchi 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,
1.60 ! tb 765: .opt.arg = &cfg.sess_in,
1.47 inoguchi 766: },
767: {
768: .name = "sess_out",
769: .argname = "file",
770: .desc = "File to write TLS session to",
771: .type = OPTION_ARG,
1.60 ! tb 772: .opt.arg = &cfg.sess_out,
1.47 inoguchi 773: },
774: {
775: .name = "showcerts",
776: .desc = "Show all server certificates in the chain",
777: .type = OPTION_FLAG,
1.60 ! tb 778: .opt.flag = &cfg.showcerts,
1.47 inoguchi 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,
1.60 ! tb 792: .opt.flag = &cfg.state,
1.47 inoguchi 793: },
794: {
795: .name = "status",
796: .desc = "Send a certificate status request to the server (OCSP)",
797: .type = OPTION_FLAG,
1.60 ! tb 798: .opt.flag = &cfg.status_req,
1.47 inoguchi 799: },
1.53 jsing 800: #ifndef OPENSSL_NO_DTLS
1.47 inoguchi 801: {
802: .name = "timeout",
803: .desc = "Enable send/receive timeout on DTLS connections",
804: .type = OPTION_FLAG,
1.60 ! tb 805: .opt.flag = &cfg.enable_timeouts,
1.47 inoguchi 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,
1.60 ! tb 836: .opt.flag = &cfg.tlsextdebug,
1.47 inoguchi 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,
1.60 ! tb 844: .opt.arg = &cfg.srtp_profiles,
1.47 inoguchi 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,
1.60 ! tb 865: .opt.arg = &cfg.xmpphost,
1.47 inoguchi 866: },
867: {
868: .name = NULL,
869: .desc = "",
870: .type = OPTION_ARGV_FUNC,
871: .opt.argvfunc = s_client_opt_verify_param,
872: },
873: { NULL },
874: };
1.1 jsing 875:
876: static void
877: sc_usage(void)
878: {
1.47 inoguchi 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"
1.53 jsing 883: " [-crl_check_all] [-crlf] [-debug] [-dtls] [-dtls1] [-dtls1_2] [-extended_crl]\n"
1.47 inoguchi 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");
1.1 jsing 899: }
900:
901: int
902: s_client_main(int argc, char **argv)
903: {
904: SSL *con = NULL;
1.47 inoguchi 905: int s, k, p = 0, pending = 0;
1.39 beck 906: char *cbuf = NULL, *sbuf = NULL, *mbuf = NULL, *pbuf = NULL;
1.1 jsing 907: int cbuf_len, cbuf_off;
908: int sbuf_len, sbuf_off;
909: int full_log = 1;
1.57 jsing 910: const char *servername;
1.47 inoguchi 911: char *pass = NULL;
1.1 jsing 912: X509 *cert = NULL;
913: EVP_PKEY *key = NULL;
1.47 inoguchi 914: int badop = 0;
1.1 jsing 915: int write_tty, read_tty, write_ssl, read_ssl, tty_on, ssl_pending;
916: SSL_CTX *ctx = NULL;
1.47 inoguchi 917: int ret = 1, in_init = 1, i;
1.50 inoguchi 918: BIO *bio_c_out = NULL;
1.1 jsing 919: BIO *sbio;
920: int mbuf_len = 0;
1.9 deraadt 921: struct timeval timeout;
1.33 jsing 922: tlsextctx tlsextcbp = {NULL, 0};
1.45 deraadt 923: struct sockaddr_storage peer;
1.1 jsing 924: int peerlen = sizeof(peer);
1.21 doug 925:
1.59 joshua 926: if (pledge("stdio cpath wpath rpath inet dns tty", NULL) == -1) {
927: perror("pledge");
928: exit(1);
1.21 doug 929: }
1.1 jsing 930:
1.60 ! tb 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;
1.1 jsing 942:
943: if (((cbuf = malloc(BUFSIZZ)) == NULL) ||
944: ((sbuf = malloc(BUFSIZZ)) == NULL) ||
1.39 beck 945: ((pbuf = malloc(BUFSIZZ)) == NULL) ||
1.1 jsing 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:
1.47 inoguchi 952: if (options_parse(argc, argv, s_client_options, NULL, NULL) != 0) {
953: badop = 1;
954: goto bad;
1.1 jsing 955: }
1.60 ! tb 956: if (cfg.proxy != NULL) {
! 957: if (!extract_host_port(cfg.proxy,
! 958: &cfg.host, NULL, &cfg.port))
1.12 bluhm 959: goto bad;
1.60 ! tb 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))
1.12 bluhm 965: goto bad;
966: }
1.1 jsing 967: if (badop) {
1.35 jsing 968: bad:
1.60 ! tb 969: if (cfg.errstr == NULL)
1.1 jsing 970: sc_usage();
971: goto end;
972: }
973:
1.60 ! tb 974: if (!app_passwd(bio_err, cfg.passarg, NULL, &pass, NULL)) {
1.1 jsing 975: BIO_printf(bio_err, "Error getting password\n");
976: goto end;
977: }
1.60 ! tb 978: if (cfg.key_file == NULL)
! 979: cfg.key_file = cfg.cert_file;
1.1 jsing 980:
981:
1.60 ! tb 982: if (cfg.key_file) {
1.1 jsing 983:
1.60 ! tb 984: key = load_key(bio_err, cfg.key_file,
! 985: cfg.key_format, 0, pass,
1.1 jsing 986: "client certificate private key file");
987: if (!key) {
988: ERR_print_errors(bio_err);
989: goto end;
990: }
991: }
1.60 ! tb 992: if (cfg.cert_file) {
! 993: cert = load_cert(bio_err, cfg.cert_file,
! 994: cfg.cert_format,
1.19 bcook 995: NULL, "client certificate file");
1.1 jsing 996:
997: if (!cert) {
998: ERR_print_errors(bio_err);
999: goto end;
1000: }
1001: }
1.60 ! tb 1002: if (cfg.quiet && !cfg.debug &&
! 1003: !cfg.msg) {
1.50 inoguchi 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;
1.1 jsing 1009: }
1010:
1.60 ! tb 1011: ctx = SSL_CTX_new(cfg.meth);
1.1 jsing 1012: if (ctx == NULL) {
1013: ERR_print_errors(bio_err);
1014: goto end;
1015: }
1.46 tb 1016:
1017: SSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY);
1018:
1.60 ! tb 1019: if (cfg.vpm)
! 1020: SSL_CTX_set1_param(ctx, cfg.vpm);
1.42 jsing 1021:
1.60 ! tb 1022: if (!SSL_CTX_set_min_proto_version(ctx, cfg.min_version))
1.42 jsing 1023: goto end;
1.60 ! tb 1024: if (!SSL_CTX_set_max_proto_version(ctx, cfg.max_version))
1.42 jsing 1025: goto end;
1.1 jsing 1026:
1027: #ifndef OPENSSL_NO_SRTP
1.60 ! tb 1028: if (cfg.srtp_profiles != NULL)
! 1029: SSL_CTX_set_tlsext_use_srtp(ctx, cfg.srtp_profiles);
1.1 jsing 1030: #endif
1.60 ! tb 1031: if (cfg.bugs)
! 1032: SSL_CTX_set_options(ctx, SSL_OP_ALL | cfg.off);
1.1 jsing 1033: else
1.60 ! tb 1034: SSL_CTX_set_options(ctx, cfg.off);
1.1 jsing 1035:
1.60 ! tb 1036: if (cfg.clr)
! 1037: SSL_CTX_clear_options(ctx, cfg.clr);
1.1 jsing 1038:
1.60 ! tb 1039: if (cfg.alpn_in) {
1.10 jsing 1040: unsigned short alpn_len;
1.49 inoguchi 1041: unsigned char *alpn;
1.10 jsing 1042:
1.60 ! tb 1043: alpn = next_protos_parse(&alpn_len, cfg.alpn_in);
1.10 jsing 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);
1.31 jsing 1050: }
1.60 ! tb 1051: if (cfg.groups_in != NULL) {
! 1052: if (SSL_CTX_set1_groups_list(ctx, cfg.groups_in) != 1) {
1.31 jsing 1053: BIO_printf(bio_err, "Failed to set groups '%s'\n",
1.60 ! tb 1054: cfg.groups_in);
1.31 jsing 1055: goto end;
1056: }
1.10 jsing 1057: }
1.1 jsing 1058:
1.60 ! tb 1059: if (cfg.state)
1.1 jsing 1060: SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback);
1.60 ! tb 1061: if (cfg.cipher != NULL)
! 1062: if (!SSL_CTX_set_cipher_list(ctx, cfg.cipher)) {
1.1 jsing 1063: BIO_printf(bio_err, "error setting cipher list\n");
1064: ERR_print_errors(bio_err);
1065: goto end;
1066: }
1067:
1.60 ! tb 1068: SSL_CTX_set_verify(ctx, cfg.verify, verify_callback);
1.1 jsing 1069: if (!set_cert_key_stuff(ctx, cert, key))
1070: goto end;
1071:
1.60 ! tb 1072: if ((cfg.CAfile || cfg.CApath)
! 1073: && !SSL_CTX_load_verify_locations(ctx, cfg.CAfile,
! 1074: cfg.CApath))
1.1 jsing 1075: ERR_print_errors(bio_err);
1.28 bcook 1076:
1077: if (!SSL_CTX_set_default_verify_paths(ctx))
1078: ERR_print_errors(bio_err);
1079:
1.1 jsing 1080: con = SSL_new(ctx);
1.60 ! tb 1081: if (cfg.sess_in) {
1.1 jsing 1082: SSL_SESSION *sess;
1.60 ! tb 1083: BIO *stmp = BIO_new_file(cfg.sess_in, "r");
1.1 jsing 1084: if (!stmp) {
1085: BIO_printf(bio_err, "Can't open session file %s\n",
1.60 ! tb 1086: cfg.sess_in);
1.1 jsing 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",
1.60 ! tb 1094: cfg.sess_in);
1.1 jsing 1095: ERR_print_errors(bio_err);
1096: goto end;
1097: }
1098: SSL_set_session(con, sess);
1099: SSL_SESSION_free(sess);
1100: }
1.57 jsing 1101:
1102: /* Attempt to opportunistically use the host name for SNI. */
1.60 ! tb 1103: servername = cfg.servername;
1.57 jsing 1104: if (servername == NULL)
1.60 ! tb 1105: servername = cfg.host;
1.57 jsing 1106:
1.60 ! tb 1107: if (!cfg.no_servername && servername != NULL &&
1.57 jsing 1108: !SSL_set_tlsext_host_name(con, servername)) {
1109: long ssl_err = ERR_peek_error();
1110:
1.60 ! tb 1111: if (cfg.servername != NULL ||
1.57 jsing 1112: ERR_GET_LIB(ssl_err) != ERR_LIB_SSL ||
1113: ERR_GET_REASON(ssl_err) != SSL_R_SSL3_EXT_INVALID_SERVERNAME) {
1.49 inoguchi 1114: BIO_printf(bio_err,
1115: "Unable to set TLS servername extension.\n");
1.1 jsing 1116: ERR_print_errors(bio_err);
1117: goto end;
1118: }
1.57 jsing 1119: servername = NULL;
1120: ERR_clear_error();
1121: }
1.60 ! tb 1122: if (!cfg.no_servername && servername != NULL) {
1.57 jsing 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);
1.1 jsing 1126: }
1127:
1.49 inoguchi 1128: re_start:
1.1 jsing 1129:
1.60 ! tb 1130: if (init_client(&s, cfg.host, cfg.port,
! 1131: cfg.socket_type, cfg.af) == 0) {
1.1 jsing 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:
1.60 ! tb 1137: if (cfg.nbio) {
! 1138: if (!cfg.quiet)
1.3 bcook 1139: BIO_printf(bio_c_out, "turning on non blocking io\n");
1140: if (!BIO_socket_nbio(s, 1)) {
1.1 jsing 1141: ERR_print_errors(bio_err);
1142: goto end;
1143: }
1144: }
1.60 ! tb 1145: if (cfg.pause & 0x01)
1.1 jsing 1146: SSL_set_debug(con, 1);
1147:
1.53 jsing 1148: if (SSL_is_dtls(con)) {
1.1 jsing 1149: sbio = BIO_new_dgram(s, BIO_NOCLOSE);
1.45 deraadt 1150: if (getsockname(s, (struct sockaddr *)&peer,
1151: (void *)&peerlen) == -1) {
1.1 jsing 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:
1.60 ! tb 1160: if (cfg.enable_timeouts) {
1.1 jsing 1161: timeout.tv_sec = 0;
1162: timeout.tv_usec = DGRAM_RCV_TIMEOUT;
1.49 inoguchi 1163: BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0,
1164: &timeout);
1.1 jsing 1165:
1166: timeout.tv_sec = 0;
1167: timeout.tv_usec = DGRAM_SND_TIMEOUT;
1.49 inoguchi 1168: BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0,
1169: &timeout);
1.1 jsing 1170: }
1.60 ! tb 1171: if (cfg.socket_mtu > 28) {
1.1 jsing 1172: SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
1.60 ! tb 1173: SSL_set_mtu(con, cfg.socket_mtu - 28);
1.1 jsing 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:
1.60 ! tb 1180: if (cfg.nbio_test) {
1.1 jsing 1181: BIO *test;
1182:
1183: test = BIO_new(BIO_f_nbio_test());
1184: sbio = BIO_push(test, sbio);
1185: }
1.60 ! tb 1186: if (cfg.debug) {
1.1 jsing 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: }
1.60 ! tb 1191: if (cfg.msg) {
1.1 jsing 1192: SSL_set_msg_callback(con, msg_cb);
1193: SSL_set_msg_callback_arg(con, bio_c_out);
1194: }
1.60 ! tb 1195: if (cfg.tlsextdebug) {
1.1 jsing 1196: SSL_set_tlsext_debug_callback(con, tlsext_cb);
1197: SSL_set_tlsext_debug_arg(con, bio_c_out);
1198: }
1.60 ! tb 1199: if (cfg.status_req) {
1.1 jsing 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: */
1.60 ! tb 1228: if (cfg.starttls_proto == PROTO_SMTP ||
! 1229: cfg.starttls_proto == PROTO_LMTP) {
1.1 jsing 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",
1.60 ! tb 1240: cfg.starttls_proto == PROTO_SMTP ? 'E' : 'L');
1.1 jsing 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,
1.36 jmc 1254: "didn't find starttls in server response,"
1.1 jsing 1255: " try anyway...\n");
1256: BIO_printf(sbio, "STARTTLS\r\n");
1257: BIO_read(sbio, sbuf, BUFSIZZ);
1.60 ! tb 1258: } else if (cfg.starttls_proto == PROTO_POP3) {
1.1 jsing 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);
1.60 ! tb 1266: } else if (cfg.starttls_proto == PROTO_IMAP) {
1.1 jsing 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,
1.37 tb 1286: "didn't find STARTTLS in server response,"
1.1 jsing 1287: " try anyway...\n");
1288: BIO_printf(sbio, ". STARTTLS\r\n");
1289: BIO_read(sbio, sbuf, BUFSIZZ);
1.60 ! tb 1290: } else if (cfg.starttls_proto == PROTO_FTP) {
1.1 jsing 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);
1.60 ! tb 1303: } else if (cfg.starttls_proto == PROTO_XMPP) {
1.1 jsing 1304: int seen = 0;
1305: BIO_printf(sbio, "<stream:stream "
1306: "xmlns:stream='http://etherx.jabber.org/streams' "
1.49 inoguchi 1307: "xmlns='jabber:client' to='%s' version='1.0'>",
1.60 ! tb 1308: cfg.xmpphost ?
! 1309: cfg.xmpphost : cfg.host);
1.1 jsing 1310: seen = BIO_read(sbio, mbuf, BUFSIZZ);
1.15 landry 1311:
1312: if (seen <= 0)
1313: goto shut;
1314:
1.1 jsing 1315: mbuf[seen] = 0;
1.15 landry 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)
1.1 jsing 1321: goto shut;
1.15 landry 1322:
1.1 jsing 1323: mbuf[seen] = 0;
1324: }
1.49 inoguchi 1325: BIO_printf(sbio,
1326: "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
1.1 jsing 1327: seen = BIO_read(sbio, sbuf, BUFSIZZ);
1328: sbuf[seen] = 0;
1329: if (!strstr(sbuf, "<proceed"))
1330: goto shut;
1331: mbuf[0] = 0;
1.60 ! tb 1332: } else if (cfg.proxy != NULL) {
1.49 inoguchi 1333: BIO_printf(sbio, "CONNECT %s HTTP/1.0\r\n\r\n",
1.60 ! tb 1334: cfg.connect);
1.12 bluhm 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: }
1.1 jsing 1340: }
1341: for (;;) {
1.9 deraadt 1342: struct pollfd pfd[3]; /* stdin, stdout, socket */
1343: int ptimeout = -1;
1.1 jsing 1344:
1.53 jsing 1345: if (SSL_is_dtls(con) && DTLSv1_get_timeout(con, &timeout))
1.49 inoguchi 1346: ptimeout = timeout.tv_sec * 1000 +
1347: timeout.tv_usec / 1000;
1.1 jsing 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;
1.60 ! tb 1356: if (cfg.sess_out) {
1.49 inoguchi 1357: BIO *stmp = BIO_new_file(
1.60 ! tb 1358: cfg.sess_out, "w");
1.1 jsing 1359: if (stmp) {
1.49 inoguchi 1360: PEM_write_bio_SSL_SESSION(stmp,
1361: SSL_get_session(con));
1.1 jsing 1362: BIO_free(stmp);
1363: } else
1.49 inoguchi 1364: BIO_printf(bio_err,
1365: "Error writing session file %s\n",
1.60 ! tb 1366: cfg.sess_out);
1.1 jsing 1367: }
1368: print_stuff(bio_c_out, con, full_log);
1369: if (full_log > 0)
1370: full_log--;
1371:
1.60 ! tb 1372: if (cfg.starttls_proto) {
1.1 jsing 1373: BIO_write(bio_err, mbuf, mbuf_len);
1374: /* We don't need to know any more */
1.60 ! tb 1375: cfg.starttls_proto = PROTO_OFF;
1.1 jsing 1376: }
1.60 ! tb 1377: if (cfg.reconnect) {
! 1378: cfg.reconnect--;
1.49 inoguchi 1379: BIO_printf(bio_c_out,
1380: "drop connection and then reconnect\n");
1.1 jsing 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:
1.9 deraadt 1392: pfd[0].fd = -1;
1393: pfd[1].fd = -1;
1.1 jsing 1394: if (!ssl_pending) {
1395: if (tty_on) {
1.9 deraadt 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: }
1.1 jsing 1404: }
1.9 deraadt 1405:
1406: pfd[2].fd = SSL_get_fd(con);
1407: pfd[2].events = 0;
1.1 jsing 1408: if (read_ssl)
1.9 deraadt 1409: pfd[2].events |= POLLIN;
1.1 jsing 1410: if (write_ssl)
1.9 deraadt 1411: pfd[2].events |= POLLOUT;
1412:
1.1 jsing 1413: /* printf("mode tty(%d %d%d) ssl(%d%d)\n",
1414: tty_on,read_tty,write_tty,read_ssl,write_ssl);*/
1415:
1.9 deraadt 1416: i = poll(pfd, 3, ptimeout);
1.38 deraadt 1417: if (i == -1) {
1.1 jsing 1418: BIO_printf(bio_err, "bad select %d\n",
1419: errno);
1420: goto shut;
1421: /* goto end; */
1422: }
1423: }
1.53 jsing 1424: if (SSL_is_dtls(con) &&
1425: DTLSv1_handle_timeout(con) > 0)
1.1 jsing 1426: BIO_printf(bio_err, "TIMEOUT occured\n");
1.49 inoguchi 1427: if (!ssl_pending &&
1428: (pfd[2].revents & (POLLOUT|POLLERR|POLLNVAL))) {
1.9 deraadt 1429: if (pfd[2].revents & (POLLERR|POLLNVAL)) {
1430: BIO_printf(bio_err, "poll error");
1431: goto shut;
1432: }
1.1 jsing 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: }
1.9 deraadt 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: }
1.1 jsing 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: }
1.9 deraadt 1509: } else if (ssl_pending || (pfd[2].revents & (POLLIN|POLLHUP))) {
1.1 jsing 1510: #ifdef RENEG
1511: {
1512: static int iiii;
1513: if (++iiii == 52) {
1514: SSL_renegotiate(con);
1515: iiii = 0;
1516: }
1517: }
1518: #endif
1.60 ! tb 1519: if (cfg.peekaboo) {
1.40 jsing 1520: k = p = SSL_peek(con, pbuf, 1024 /* BUFSIZZ */ );
1.41 beck 1521: pending = SSL_pending(con);
1.40 jsing 1522: if (SSL_get_error(con, p) == SSL_ERROR_NONE) {
1.39 beck 1523: if (p <= 0)
1524: goto end;
1525:
1.40 jsing 1526: k = SSL_read(con, sbuf, p);
1.39 beck 1527: }
1.40 jsing 1528: } else {
1529: k = SSL_read(con, sbuf, 1024 /* BUFSIZZ */ );
1.39 beck 1530: }
1.1 jsing 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;
1.60 ! tb 1538: if (cfg.peekaboo) {
1.41 beck 1539: if (p != pending) {
1540: ret = -1;
1541: BIO_printf(bio_err,
1.49 inoguchi 1542: "peeked %d but pending %d!\n",
1543: p, pending);
1.41 beck 1544: goto shut;
1545: }
1.39 beck 1546: if (k < p) {
1547: ret = -1;
1548: BIO_printf(bio_err,
1549: "read less than peek!\n");
1550: goto shut;
1551: }
1.49 inoguchi 1552: if (p > 0 &&
1553: (memcmp(sbuf, pbuf, p) != 0)) {
1.39 beck 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: }
1.1 jsing 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: }
1.9 deraadt 1592: } else if (pfd[0].revents) {
1593: if (pfd[0].revents & (POLLERR|POLLNVAL)) {
1594: BIO_printf(bio_err, "poll error");
1595: goto shut;
1596: }
1.60 ! tb 1597: if (cfg.crlf) {
1.1 jsing 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:
1.60 ! tb 1618: if ((!cfg.ign_eof) &&
1.49 inoguchi 1619: ((i <= 0) || (cbuf[0] == 'Q'))) {
1.1 jsing 1620: BIO_printf(bio_err, "DONE\n");
1621: ret = 0;
1622: goto shut;
1623: }
1.60 ! tb 1624: if ((!cfg.ign_eof) && (cbuf[0] == 'R')) {
1.1 jsing 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;
1.35 jsing 1639: shut:
1.1 jsing 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));
1.35 jsing 1645: end:
1.1 jsing 1646: if (con != NULL) {
1.60 ! tb 1647: if (cfg.prexit != 0)
1.1 jsing 1648: print_stuff(bio_c_out, con, 1);
1649: SSL_free(con);
1650: }
1.34 jsing 1651: SSL_CTX_free(ctx);
1652: X509_free(cert);
1653: EVP_PKEY_free(key);
1.1 jsing 1654: free(pass);
1.60 ! tb 1655: X509_VERIFY_PARAM_free(cfg.vpm);
1.32 deraadt 1656: freezero(cbuf, BUFSIZZ);
1657: freezero(sbuf, BUFSIZZ);
1.52 tb 1658: freezero(pbuf, BUFSIZZ);
1.32 deraadt 1659: freezero(mbuf, BUFSIZZ);
1.50 inoguchi 1660: BIO_free(bio_c_out);
1.1 jsing 1661:
1662: return (ret);
1663: }
1664:
1665: static void
1.51 inoguchi 1666: print_stuff(BIO *bio, SSL *s, int full)
1.1 jsing 1667: {
1668: X509 *peer = NULL;
1669: char *p;
1670: static const char *space = " ";
1671: char buf[BUFSIZ];
1.51 inoguchi 1672: STACK_OF(X509) *sk;
1673: STACK_OF(X509_NAME) *sk2;
1.1 jsing 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);
1.60 ! tb 1695: if (cfg.showcerts)
1.49 inoguchi 1696: PEM_write_bio_X509(bio,
1697: sk_X509_value(sk, i));
1.1 jsing 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");
1.60 ! tb 1704: if (!(cfg.showcerts && got_a_chain)) {
1.49 inoguchi 1705: /* Redundant if we showed the whole chain */
1.1 jsing 1706: PEM_write_bio_X509(bio, peer);
1.49 inoguchi 1707: }
1.1 jsing 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)) {
1.49 inoguchi 1719: BIO_printf(bio,
1720: "---\nAcceptable client certificate CA names\n");
1.1 jsing 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 {
1.49 inoguchi 1728: BIO_printf(bio,
1729: "---\nNo client certificate CA names sent\n");
1.1 jsing 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:
1.49 inoguchi 1740: BIO_printf(bio,
1741: "---\nCiphers common between both SSL endpoints:\n");
1.1 jsing 1742: j = i = 0;
1743: while (*p) {
1744: if (*p == ':') {
1745: BIO_write(bio, space, 15 - j % 25);
1746: i++;
1747: j = 0;
1.49 inoguchi 1748: BIO_write(bio,
1749: ((i % 3) ? " " : "\n"), 1);
1.1 jsing 1750: } else {
1751: BIO_write(bio, p, 1);
1752: j++;
1753: }
1754: p++;
1755: }
1756: BIO_write(bio, "\n", 1);
1757: }
1.29 jsing 1758:
1759: ssl_print_tmp_key(bio, s);
1760:
1.49 inoguchi 1761: BIO_printf(bio,
1762: "---\nSSL handshake has read %ld bytes and written %ld bytes\n",
1.1 jsing 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;
1.58 tb 1773:
1774: pktmp = X509_get0_pubkey(peer);
1.1 jsing 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);
1.50 inoguchi 1793: BIO_printf(bio, "LOCAL PORT is %u\n",
1.49 inoguchi 1794: ntohs(ladd.sin_port));
1.1 jsing 1795: }
1796: #endif
1797:
1.10 jsing 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: }
1.1 jsing 1809:
1810: #ifndef OPENSSL_NO_SRTP
1811: {
1.49 inoguchi 1812: SRTP_PROTECTION_PROFILE *srtp_profile;
1.1 jsing 1813:
1.49 inoguchi 1814: srtp_profile = SSL_get_selected_srtp_profile(s);
1.1 jsing 1815: if (srtp_profile)
1.49 inoguchi 1816: BIO_printf(bio,
1817: "SRTP Extension negotiated, profile=%s\n",
1.1 jsing 1818: srtp_profile->name);
1819: }
1820: #endif
1821:
1822: SSL_SESSION_print(bio, SSL_get_session(s));
1.60 ! tb 1823: if (cfg.keymatexportlabel != NULL) {
1.1 jsing 1824: BIO_printf(bio, "Keying material exporter:\n");
1.49 inoguchi 1825: BIO_printf(bio, " Label: '%s'\n",
1.60 ! tb 1826: cfg.keymatexportlabel);
1.49 inoguchi 1827: BIO_printf(bio, " Length: %i bytes\n",
1.60 ! tb 1828: cfg.keymatexportlen);
! 1829: exportedkeymat = malloc(cfg.keymatexportlen);
1.1 jsing 1830: if (exportedkeymat != NULL) {
1831: if (!SSL_export_keying_material(s, exportedkeymat,
1.60 ! tb 1832: cfg.keymatexportlen,
! 1833: cfg.keymatexportlabel,
! 1834: strlen(cfg.keymatexportlabel),
1.1 jsing 1835: NULL, 0, 0)) {
1836: BIO_printf(bio, " Error\n");
1837: } else {
1838: BIO_printf(bio, " Keying material: ");
1.60 ! tb 1839: for (i = 0; i < cfg.keymatexportlen; i++)
1.1 jsing 1840: BIO_printf(bio, "%02X",
1841: exportedkeymat[i]);
1842: BIO_printf(bio, "\n");
1843: }
1844: free(exportedkeymat);
1845: }
1846: }
1847: BIO_printf(bio, "---\n");
1.34 jsing 1848: X509_free(peer);
1.1 jsing 1849: /* flush, or debugging output gets mixed with http response */
1850: (void) BIO_flush(bio);
1851: }
1852:
1853: static int
1.51 inoguchi 1854: ocsp_resp_cb(SSL *s, void *arg)
1.1 jsing 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;
1.51 inoguchi 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;
1.1 jsing 1889: }
1890: