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