Annotation of src/usr.bin/openssl/s_client.c, Revision 1.40
1.40 ! jsing 1: /* $OpenBSD: s_client.c,v 1.39 2020/01/22 04:51:48 beck 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 int c_nbio = 0;
174: static int c_Pause = 0;
175: static int c_debug = 0;
176: static int c_tlsextdebug = 0;
177: static int c_status_req = 0;
178: static int c_msg = 0;
179: static int c_showcerts = 0;
180:
181: static char *keymatexportlabel = NULL;
182: static int keymatexportlen = 20;
183:
184: static void sc_usage(void);
185: static void print_stuff(BIO * berr, SSL * con, int full);
186: static int ocsp_resp_cb(SSL * s, void *arg);
187: static BIO *bio_c_out = NULL;
188: static int c_quiet = 0;
189: static int c_ign_eof = 0;
190:
191:
192: static void
193: sc_usage(void)
194: {
195: BIO_printf(bio_err, "usage: s_client args\n");
196: BIO_printf(bio_err, "\n");
197: BIO_printf(bio_err, " -4 - Force IPv4\n");
198: BIO_printf(bio_err, " -6 - Force IPv6\n");
199: BIO_printf(bio_err, " -host host - use -connect instead\n");
200: BIO_printf(bio_err, " -port port - use -connect instead\n");
201: BIO_printf(bio_err, " -connect host:port - who to connect to (default is %s:%s)\n", SSL_HOST_NAME, PORT_STR);
1.12 bluhm 202: BIO_printf(bio_err, " -proxy host:port - connect to http proxy\n");
1.1 jsing 203:
204: BIO_printf(bio_err, " -verify arg - turn on peer certificate verification\n");
205: BIO_printf(bio_err, " -cert arg - certificate file to use, PEM format assumed\n");
206: BIO_printf(bio_err, " -certform arg - certificate format (PEM or DER) PEM default\n");
207: BIO_printf(bio_err, " -key arg - Private key file to use, in cert file if\n");
208: BIO_printf(bio_err, " not specified but cert file is.\n");
209: BIO_printf(bio_err, " -keyform arg - key format (PEM or DER) PEM default\n");
210: BIO_printf(bio_err, " -pass arg - private key file pass phrase source\n");
211: BIO_printf(bio_err, " -CApath arg - PEM format directory of CA's\n");
212: BIO_printf(bio_err, " -CAfile arg - PEM format file of CA's\n");
213: BIO_printf(bio_err, " -reconnect - Drop and re-make the connection with the same Session-ID\n");
214: BIO_printf(bio_err, " -pause - sleep(1) after each read(2) and write(2) system call\n");
215: BIO_printf(bio_err, " -showcerts - show all certificates in the chain\n");
216: BIO_printf(bio_err, " -debug - extra output\n");
217: BIO_printf(bio_err, " -msg - Show protocol messages\n");
218: BIO_printf(bio_err, " -nbio_test - more ssl protocol testing\n");
219: BIO_printf(bio_err, " -state - print the 'ssl' states\n");
220: BIO_printf(bio_err, " -nbio - Run with non-blocking IO\n");
221: BIO_printf(bio_err, " -crlf - convert LF from terminal into CRLF\n");
222: BIO_printf(bio_err, " -quiet - no s_client output\n");
223: BIO_printf(bio_err, " -ign_eof - ignore input eof (default when -quiet)\n");
224: BIO_printf(bio_err, " -no_ign_eof - don't ignore input eof\n");
225: BIO_printf(bio_err, " -tls1_2 - just use TLSv1.2\n");
226: BIO_printf(bio_err, " -tls1_1 - just use TLSv1.1\n");
227: BIO_printf(bio_err, " -tls1 - just use TLSv1\n");
228: BIO_printf(bio_err, " -dtls1 - just use DTLSv1\n");
229: BIO_printf(bio_err, " -mtu - set the link layer MTU\n");
230: BIO_printf(bio_err, " -no_tls1_2/-no_tls1_1/-no_tls1/-no_ssl3/-no_ssl2 - turn off that protocol\n");
231: BIO_printf(bio_err, " -bugs - Switch on all SSL implementation bug workarounds\n");
232: BIO_printf(bio_err, " -cipher - preferred cipher to use, use the 'openssl ciphers'\n");
233: BIO_printf(bio_err, " command to see what is available\n");
234: BIO_printf(bio_err, " -starttls prot - use the STARTTLS command before starting TLS\n");
235: BIO_printf(bio_err, " for those protocols that support it, where\n");
236: BIO_printf(bio_err, " 'prot' defines which one to assume. Currently,\n");
237: BIO_printf(bio_err, " only \"smtp\", \"lmtp\", \"pop3\", \"imap\", \"ftp\" and \"xmpp\"\n");
238: BIO_printf(bio_err, " are supported.\n");
1.15 landry 239: BIO_printf(bio_err, " -xmpphost host - connect to this virtual host on the xmpp server\n");
1.1 jsing 240: BIO_printf(bio_err, " -sess_out arg - file to write SSL session to\n");
241: BIO_printf(bio_err, " -sess_in arg - file to read SSL session from\n");
242: BIO_printf(bio_err, " -servername host - Set TLS extension servername in ClientHello\n");
243: BIO_printf(bio_err, " -tlsextdebug - hex dump of all TLS extensions received\n");
244: BIO_printf(bio_err, " -status - request certificate status from server\n");
245: BIO_printf(bio_err, " -no_ticket - disable use of RFC4507bis session tickets\n");
1.10 jsing 246: BIO_printf(bio_err, " -alpn arg - enable ALPN extension, considering named protocols supported (comma-separated list)\n");
1.31 jsing 247: BIO_printf(bio_err, " -groups arg - specify EC curve groups (colon-separated list)\n");
1.1 jsing 248: #ifndef OPENSSL_NO_SRTP
249: BIO_printf(bio_err, " -use_srtp profiles - Offer SRTP key management with a colon-separated profile list\n");
250: #endif
251: BIO_printf(bio_err, " -keymatexport label - Export keying material using label\n");
252: BIO_printf(bio_err, " -keymatexportlen len - Export len bytes of keying material (default 20)\n");
253: }
254:
255:
256: /* This is a context that we pass to callbacks */
257: typedef struct tlsextctx_st {
258: BIO *biodebug;
259: int ack;
260: } tlsextctx;
261:
262:
263: static int
264: ssl_servername_cb(SSL * s, int *ad, void *arg)
265: {
266: tlsextctx *p = (tlsextctx *) arg;
267: const char *hn = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
268: if (SSL_get_servername_type(s) != -1)
269: p->ack = !SSL_session_reused(s) && hn != NULL;
270: else
271: BIO_printf(bio_err, "Can't use SSL_get_servername\n");
272:
273: return SSL_TLSEXT_ERR_OK;
274: }
275:
276: #ifndef OPENSSL_NO_SRTP
277: char *srtp_profiles = NULL;
278: #endif
279:
280: enum {
281: PROTO_OFF = 0,
282: PROTO_SMTP,
283: PROTO_LMTP,
284: PROTO_POP3,
285: PROTO_IMAP,
286: PROTO_FTP,
287: PROTO_XMPP
288: };
289:
290: int
291: s_client_main(int argc, char **argv)
292: {
293: unsigned int off = 0, clr = 0;
294: SSL *con = NULL;
1.39 beck 295: int s, k, p, state = 0, af = AF_UNSPEC;
296: char *cbuf = NULL, *sbuf = NULL, *mbuf = NULL, *pbuf = NULL;
1.1 jsing 297: int cbuf_len, cbuf_off;
298: int sbuf_len, sbuf_off;
1.39 beck 299: int pbuf_len, pbuf_off;
1.1 jsing 300: char *port = PORT_STR;
301: int full_log = 1;
302: char *host = SSL_HOST_NAME;
1.15 landry 303: char *xmpphost = NULL;
1.12 bluhm 304: char *proxy = NULL, *connect = NULL;
1.1 jsing 305: char *cert_file = NULL, *key_file = NULL;
306: int cert_format = FORMAT_PEM, key_format = FORMAT_PEM;
307: char *passarg = NULL, *pass = NULL;
308: X509 *cert = NULL;
309: EVP_PKEY *key = NULL;
310: char *CApath = NULL, *CAfile = NULL, *cipher = NULL;
311: int reconnect = 0, badop = 0, verify = SSL_VERIFY_NONE, bugs = 0;
312: int crlf = 0;
313: int write_tty, read_tty, write_ssl, read_ssl, tty_on, ssl_pending;
314: SSL_CTX *ctx = NULL;
315: int ret = 1, in_init = 1, i, nbio_test = 0;
316: int starttls_proto = PROTO_OFF;
317: int prexit = 0;
1.39 beck 318: int peekaboo = 0;
1.1 jsing 319: X509_VERIFY_PARAM *vpm = NULL;
320: int badarg = 0;
321: const SSL_METHOD *meth = NULL;
322: int socket_type = SOCK_STREAM;
323: BIO *sbio;
324: int mbuf_len = 0;
1.9 deraadt 325: struct timeval timeout;
1.1 jsing 326: const char *errstr = NULL;
327: char *servername = NULL;
1.33 jsing 328: tlsextctx tlsextcbp = {NULL, 0};
1.10 jsing 329: const char *alpn_in = NULL;
1.31 jsing 330: const char *groups_in = NULL;
1.1 jsing 331: char *sess_in = NULL;
332: char *sess_out = NULL;
333: struct sockaddr peer;
334: int peerlen = sizeof(peer);
335: int enable_timeouts = 0;
336: long socket_mtu = 0;
1.21 doug 337:
338: if (single_execution) {
1.30 deraadt 339: if (pledge("stdio cpath wpath rpath inet dns tty", NULL) == -1) {
1.21 doug 340: perror("pledge");
1.23 doug 341: exit(1);
342: }
1.21 doug 343: }
1.1 jsing 344:
345: meth = SSLv23_client_method();
346:
347: c_Pause = 0;
348: c_quiet = 0;
349: c_ign_eof = 0;
350: c_debug = 0;
351: c_msg = 0;
352: c_showcerts = 0;
353:
354: if (((cbuf = malloc(BUFSIZZ)) == NULL) ||
355: ((sbuf = malloc(BUFSIZZ)) == NULL) ||
1.39 beck 356: ((pbuf = malloc(BUFSIZZ)) == NULL) ||
1.1 jsing 357: ((mbuf = malloc(BUFSIZZ + 1)) == NULL)) { /* NUL byte */
358: BIO_printf(bio_err, "out of memory\n");
359: goto end;
360: }
361: verify_depth = 0;
362: c_nbio = 0;
363:
364: argc--;
365: argv++;
366: while (argc >= 1) {
367: if (strcmp(*argv, "-host") == 0) {
368: if (--argc < 1)
369: goto bad;
370: host = *(++argv);
371: } else if (strcmp(*argv, "-port") == 0) {
372: if (--argc < 1)
373: goto bad;
374: port = *(++argv);
375: if (port == NULL || *port == '\0')
376: goto bad;
377: } else if (strcmp(*argv, "-connect") == 0) {
378: if (--argc < 1)
379: goto bad;
1.12 bluhm 380: connect = *(++argv);
381: } else if (strcmp(*argv, "-proxy") == 0) {
382: if (--argc < 1)
1.1 jsing 383: goto bad;
1.12 bluhm 384: proxy = *(++argv);
1.15 landry 385: } else if (strcmp(*argv,"-xmpphost") == 0) {
386: if (--argc < 1)
387: goto bad;
388: xmpphost= *(++argv);
1.1 jsing 389: } else if (strcmp(*argv, "-verify") == 0) {
390: verify = SSL_VERIFY_PEER;
391: if (--argc < 1)
392: goto bad;
393: verify_depth = strtonum(*(++argv), 0, INT_MAX, &errstr);
394: if (errstr)
395: goto bad;
396: BIO_printf(bio_err, "verify depth is %d\n", verify_depth);
397: } else if (strcmp(*argv, "-cert") == 0) {
398: if (--argc < 1)
399: goto bad;
400: cert_file = *(++argv);
401: } else if (strcmp(*argv, "-sess_out") == 0) {
402: if (--argc < 1)
403: goto bad;
404: sess_out = *(++argv);
405: } else if (strcmp(*argv, "-sess_in") == 0) {
406: if (--argc < 1)
407: goto bad;
408: sess_in = *(++argv);
409: } else if (strcmp(*argv, "-certform") == 0) {
410: if (--argc < 1)
411: goto bad;
412: cert_format = str2fmt(*(++argv));
413: } else if (args_verify(&argv, &argc, &badarg, bio_err, &vpm)) {
414: if (badarg)
415: goto bad;
416: continue;
417: } else if (strcmp(*argv, "-verify_return_error") == 0)
418: verify_return_error = 1;
419: else if (strcmp(*argv, "-prexit") == 0)
420: prexit = 1;
1.39 beck 421: else if (strcmp(*argv, "-peekaboo") == 0)
422: peekaboo = 1;
1.1 jsing 423: else if (strcmp(*argv, "-crlf") == 0)
424: crlf = 1;
425: else if (strcmp(*argv, "-quiet") == 0) {
426: c_quiet = 1;
427: c_ign_eof = 1;
428: } else if (strcmp(*argv, "-ign_eof") == 0)
429: c_ign_eof = 1;
430: else if (strcmp(*argv, "-no_ign_eof") == 0)
431: c_ign_eof = 0;
432: else if (strcmp(*argv, "-pause") == 0)
433: c_Pause = 1;
434: else if (strcmp(*argv, "-debug") == 0)
435: c_debug = 1;
436: else if (strcmp(*argv, "-tlsextdebug") == 0)
437: c_tlsextdebug = 1;
438: else if (strcmp(*argv, "-status") == 0)
439: c_status_req = 1;
440: else if (strcmp(*argv, "-msg") == 0)
441: c_msg = 1;
442: else if (strcmp(*argv, "-showcerts") == 0)
443: c_showcerts = 1;
444: else if (strcmp(*argv, "-nbio_test") == 0)
445: nbio_test = 1;
446: else if (strcmp(*argv, "-state") == 0)
447: state = 1;
448: else if (strcmp(*argv, "-tls1_2") == 0)
449: meth = TLSv1_2_client_method();
450: else if (strcmp(*argv, "-tls1_1") == 0)
451: meth = TLSv1_1_client_method();
452: else if (strcmp(*argv, "-tls1") == 0)
453: meth = TLSv1_client_method();
454: #ifndef OPENSSL_NO_DTLS1
455: else if (strcmp(*argv, "-dtls1") == 0) {
456: meth = DTLSv1_client_method();
457: socket_type = SOCK_DGRAM;
458: } else if (strcmp(*argv, "-timeout") == 0)
459: enable_timeouts = 1;
460: else if (strcmp(*argv, "-mtu") == 0) {
461: if (--argc < 1)
462: goto bad;
463: socket_mtu = strtonum(*(++argv), 0, LONG_MAX, &errstr);
464: if (errstr)
465: goto bad;
466: }
467: #endif
468: else if (strcmp(*argv, "-bugs") == 0)
469: bugs = 1;
470: else if (strcmp(*argv, "-keyform") == 0) {
471: if (--argc < 1)
472: goto bad;
473: key_format = str2fmt(*(++argv));
474: } else if (strcmp(*argv, "-pass") == 0) {
475: if (--argc < 1)
476: goto bad;
477: passarg = *(++argv);
478: } else if (strcmp(*argv, "-key") == 0) {
479: if (--argc < 1)
480: goto bad;
481: key_file = *(++argv);
482: } else if (strcmp(*argv, "-reconnect") == 0) {
483: reconnect = 5;
484: } else if (strcmp(*argv, "-CApath") == 0) {
485: if (--argc < 1)
486: goto bad;
487: CApath = *(++argv);
488: } else if (strcmp(*argv, "-CAfile") == 0) {
489: if (--argc < 1)
490: goto bad;
491: CAfile = *(++argv);
492: } else if (strcmp(*argv, "-no_tls1_2") == 0)
493: off |= SSL_OP_NO_TLSv1_2;
494: else if (strcmp(*argv, "-no_tls1_1") == 0)
495: off |= SSL_OP_NO_TLSv1_1;
496: else if (strcmp(*argv, "-no_tls1") == 0)
497: off |= SSL_OP_NO_TLSv1;
498: else if (strcmp(*argv, "-no_ssl3") == 0)
499: off |= SSL_OP_NO_SSLv3;
500: else if (strcmp(*argv, "-no_ssl2") == 0)
501: off |= SSL_OP_NO_SSLv2;
502: else if (strcmp(*argv, "-no_comp") == 0) {
503: off |= SSL_OP_NO_COMPRESSION;
1.31 jsing 504: } else if (strcmp(*argv, "-no_ticket") == 0) {
1.1 jsing 505: off |= SSL_OP_NO_TICKET;
1.31 jsing 506: } else if (strcmp(*argv, "-nextprotoneg") == 0) {
1.33 jsing 507: /* Ignored. */
1.1 jsing 508: if (--argc < 1)
509: goto bad;
1.33 jsing 510: ++argv;
1.31 jsing 511: } else if (strcmp(*argv, "-alpn") == 0) {
1.10 jsing 512: if (--argc < 1)
513: goto bad;
514: alpn_in = *(++argv);
1.31 jsing 515: } else if (strcmp(*argv, "-groups") == 0) {
516: if (--argc < 1)
517: goto bad;
518: groups_in = *(++argv);
1.10 jsing 519: } else if (strcmp(*argv, "-serverpref") == 0)
1.1 jsing 520: off |= SSL_OP_CIPHER_SERVER_PREFERENCE;
521: else if (strcmp(*argv, "-legacy_renegotiation") == 0)
522: ; /* no-op */
523: else if (strcmp(*argv, "-legacy_server_connect") == 0) {
524: off |= SSL_OP_LEGACY_SERVER_CONNECT;
525: } else if (strcmp(*argv, "-no_legacy_server_connect") == 0) {
526: clr |= SSL_OP_LEGACY_SERVER_CONNECT;
527: } else if (strcmp(*argv, "-cipher") == 0) {
528: if (--argc < 1)
529: goto bad;
530: cipher = *(++argv);
531: }
532: else if (strcmp(*argv, "-nbio") == 0) {
533: c_nbio = 1;
534: }
535: else if (strcmp(*argv, "-starttls") == 0) {
536: if (--argc < 1)
537: goto bad;
538: ++argv;
539: if (strcmp(*argv, "smtp") == 0)
540: starttls_proto = PROTO_SMTP;
541: else if (strcmp(*argv, "lmtp") == 0)
542: starttls_proto = PROTO_LMTP;
543: else if (strcmp(*argv, "pop3") == 0)
544: starttls_proto = PROTO_POP3;
545: else if (strcmp(*argv, "imap") == 0)
546: starttls_proto = PROTO_IMAP;
547: else if (strcmp(*argv, "ftp") == 0)
548: starttls_proto = PROTO_FTP;
549: else if (strcmp(*argv, "xmpp") == 0)
550: starttls_proto = PROTO_XMPP;
551: else
552: goto bad;
553: }
554: else if (strcmp(*argv, "-4") == 0) {
555: af = AF_INET;
556: } else if (strcmp(*argv, "-6") == 0) {
557: af = AF_INET6;
558: }
559: else if (strcmp(*argv, "-servername") == 0) {
560: if (--argc < 1)
561: goto bad;
562: servername = *(++argv);
563: /* meth=TLSv1_client_method(); */
564: }
565: #ifndef OPENSSL_NO_SRTP
566: else if (strcmp(*argv, "-use_srtp") == 0) {
567: if (--argc < 1)
568: goto bad;
569: srtp_profiles = *(++argv);
570: }
571: #endif
572: else if (strcmp(*argv, "-keymatexport") == 0) {
573: if (--argc < 1)
574: goto bad;
575: keymatexportlabel = *(++argv);
576: } else if (strcmp(*argv, "-keymatexportlen") == 0) {
577: if (--argc < 1)
578: goto bad;
579: keymatexportlen = strtonum(*(++argv), 1, INT_MAX, &errstr);
580: if (errstr)
581: goto bad;
582: } else {
583: BIO_printf(bio_err, "unknown option %s\n", *argv);
584: badop = 1;
585: break;
586: }
587: argc--;
588: argv++;
589: }
1.12 bluhm 590: if (proxy != NULL) {
591: if (!extract_host_port(proxy, &host, NULL, &port))
592: goto bad;
593: if (connect == NULL)
594: connect = SSL_HOST_NAME;
595: } else if (connect != NULL) {
596: if (!extract_host_port(connect, &host, NULL, &port))
597: goto bad;
598: }
1.1 jsing 599: if (badop) {
1.35 jsing 600: bad:
1.1 jsing 601: if (errstr)
602: BIO_printf(bio_err, "invalid argument %s: %s\n",
603: *argv, errstr);
604: else
605: sc_usage();
606: goto end;
607: }
608:
609: if (!app_passwd(bio_err, passarg, NULL, &pass, NULL)) {
610: BIO_printf(bio_err, "Error getting password\n");
611: goto end;
612: }
613: if (key_file == NULL)
614: key_file = cert_file;
615:
616:
617: if (key_file) {
618:
1.19 bcook 619: key = load_key(bio_err, key_file, key_format, 0, pass,
1.1 jsing 620: "client certificate private key file");
621: if (!key) {
622: ERR_print_errors(bio_err);
623: goto end;
624: }
625: }
626: if (cert_file) {
627: cert = load_cert(bio_err, cert_file, cert_format,
1.19 bcook 628: NULL, "client certificate file");
1.1 jsing 629:
630: if (!cert) {
631: ERR_print_errors(bio_err);
632: goto end;
633: }
634: }
635: if (bio_c_out == NULL) {
636: if (c_quiet && !c_debug && !c_msg) {
637: bio_c_out = BIO_new(BIO_s_null());
638: } else {
639: if (bio_c_out == NULL)
640: bio_c_out = BIO_new_fp(stdout, BIO_NOCLOSE);
641: }
642: }
643:
644: ctx = SSL_CTX_new(meth);
645: if (ctx == NULL) {
646: ERR_print_errors(bio_err);
647: goto end;
648: }
649: if (vpm)
650: SSL_CTX_set1_param(ctx, vpm);
651:
652: #ifndef OPENSSL_NO_SRTP
653: if (srtp_profiles != NULL)
654: SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles);
655: #endif
656: if (bugs)
657: SSL_CTX_set_options(ctx, SSL_OP_ALL | off);
658: else
659: SSL_CTX_set_options(ctx, off);
660:
661: if (clr)
662: SSL_CTX_clear_options(ctx, clr);
663: /*
664: * DTLS: partial reads end up discarding unread UDP bytes :-( Setting
665: * read ahead solves this problem.
666: */
667: if (socket_type == SOCK_DGRAM)
668: SSL_CTX_set_read_ahead(ctx, 1);
669:
1.10 jsing 670: if (alpn_in) {
671: unsigned short alpn_len;
672: unsigned char *alpn = next_protos_parse(&alpn_len, alpn_in);
673:
674: if (alpn == NULL) {
675: BIO_printf(bio_err, "Error parsing -alpn argument\n");
676: goto end;
677: }
678: SSL_CTX_set_alpn_protos(ctx, alpn, alpn_len);
679: free(alpn);
1.31 jsing 680: }
681: if (groups_in != NULL) {
682: if (SSL_CTX_set1_groups_list(ctx, groups_in) != 1) {
683: BIO_printf(bio_err, "Failed to set groups '%s'\n",
684: groups_in);
685: goto end;
686: }
1.10 jsing 687: }
1.1 jsing 688:
689: if (state)
690: SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback);
691: if (cipher != NULL)
692: if (!SSL_CTX_set_cipher_list(ctx, cipher)) {
693: BIO_printf(bio_err, "error setting cipher list\n");
694: ERR_print_errors(bio_err);
695: goto end;
696: }
697:
698: SSL_CTX_set_verify(ctx, verify, verify_callback);
699: if (!set_cert_key_stuff(ctx, cert, key))
700: goto end;
701:
1.28 bcook 702: if ((CAfile || CApath)
703: && !SSL_CTX_load_verify_locations(ctx, CAfile, CApath))
1.1 jsing 704: ERR_print_errors(bio_err);
1.28 bcook 705:
706: if (!SSL_CTX_set_default_verify_paths(ctx))
707: ERR_print_errors(bio_err);
708:
1.1 jsing 709: if (servername != NULL) {
710: tlsextcbp.biodebug = bio_err;
711: SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
712: SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
713: }
714:
715: con = SSL_new(ctx);
716: if (sess_in) {
717: SSL_SESSION *sess;
718: BIO *stmp = BIO_new_file(sess_in, "r");
719: if (!stmp) {
720: BIO_printf(bio_err, "Can't open session file %s\n",
721: sess_in);
722: ERR_print_errors(bio_err);
723: goto end;
724: }
725: sess = PEM_read_bio_SSL_SESSION(stmp, NULL, 0, NULL);
726: BIO_free(stmp);
727: if (!sess) {
728: BIO_printf(bio_err, "Can't open session file %s\n",
729: sess_in);
730: ERR_print_errors(bio_err);
731: goto end;
732: }
733: SSL_set_session(con, sess);
734: SSL_SESSION_free(sess);
735: }
736: if (servername != NULL) {
737: if (!SSL_set_tlsext_host_name(con, servername)) {
738: BIO_printf(bio_err, "Unable to set TLS servername extension.\n");
739: ERR_print_errors(bio_err);
740: goto end;
741: }
742: }
743: /* SSL_set_cipher_list(con,"RC4-MD5"); */
744:
745: re_start:
746:
747: if (init_client(&s, host, port, socket_type, af) == 0) {
748: BIO_printf(bio_err, "connect:errno=%d\n", errno);
749: goto end;
750: }
751: BIO_printf(bio_c_out, "CONNECTED(%08X)\n", s);
752:
753: if (c_nbio) {
1.3 bcook 754: if (!c_quiet)
755: BIO_printf(bio_c_out, "turning on non blocking io\n");
756: if (!BIO_socket_nbio(s, 1)) {
1.1 jsing 757: ERR_print_errors(bio_err);
758: goto end;
759: }
760: }
761: if (c_Pause & 0x01)
762: SSL_set_debug(con, 1);
763:
764: if (SSL_version(con) == DTLS1_VERSION) {
765:
766: sbio = BIO_new_dgram(s, BIO_NOCLOSE);
1.38 deraadt 767: if (getsockname(s, &peer, (void *) &peerlen) == -1) {
1.1 jsing 768: BIO_printf(bio_err, "getsockname:errno=%d\n",
769: errno);
770: shutdown(s, SHUT_RD);
771: close(s);
772: goto end;
773: }
774: (void) BIO_ctrl_set_connected(sbio, 1, &peer);
775:
776: if (enable_timeouts) {
777: timeout.tv_sec = 0;
778: timeout.tv_usec = DGRAM_RCV_TIMEOUT;
779: BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
780:
781: timeout.tv_sec = 0;
782: timeout.tv_usec = DGRAM_SND_TIMEOUT;
783: BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
784: }
785: if (socket_mtu > 28) {
786: SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
787: SSL_set_mtu(con, socket_mtu - 28);
788: } else
789: /* want to do MTU discovery */
790: BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
791: } else
792: sbio = BIO_new_socket(s, BIO_NOCLOSE);
793:
794: if (nbio_test) {
795: BIO *test;
796:
797: test = BIO_new(BIO_f_nbio_test());
798: sbio = BIO_push(test, sbio);
799: }
800: if (c_debug) {
801: SSL_set_debug(con, 1);
802: BIO_set_callback(sbio, bio_dump_callback);
803: BIO_set_callback_arg(sbio, (char *) bio_c_out);
804: }
805: if (c_msg) {
806: SSL_set_msg_callback(con, msg_cb);
807: SSL_set_msg_callback_arg(con, bio_c_out);
808: }
809: if (c_tlsextdebug) {
810: SSL_set_tlsext_debug_callback(con, tlsext_cb);
811: SSL_set_tlsext_debug_arg(con, bio_c_out);
812: }
813: if (c_status_req) {
814: SSL_set_tlsext_status_type(con, TLSEXT_STATUSTYPE_ocsp);
815: SSL_CTX_set_tlsext_status_cb(ctx, ocsp_resp_cb);
816: SSL_CTX_set_tlsext_status_arg(ctx, bio_c_out);
817: }
818:
819: SSL_set_bio(con, sbio, sbio);
820: SSL_set_connect_state(con);
821:
822: /* ok, lets connect */
823: read_tty = 1;
824: write_tty = 0;
825: tty_on = 0;
826: read_ssl = 1;
827: write_ssl = 1;
828:
829: cbuf_len = 0;
830: cbuf_off = 0;
831: sbuf_len = 0;
832: sbuf_off = 0;
1.39 beck 833: pbuf_len = 0;
834: pbuf_off = 0;
1.1 jsing 835:
836: /* This is an ugly hack that does a lot of assumptions */
837: /*
838: * We do have to handle multi-line responses which may come in a
839: * single packet or not. We therefore have to use BIO_gets() which
840: * does need a buffering BIO. So during the initial chitchat we do
841: * push a buffering BIO into the chain that is removed again later on
842: * to not disturb the rest of the s_client operation.
843: */
844: if (starttls_proto == PROTO_SMTP || starttls_proto == PROTO_LMTP) {
845: int foundit = 0;
846: BIO *fbio = BIO_new(BIO_f_buffer());
847: BIO_push(fbio, sbio);
848: /* wait for multi-line response to end from SMTP */
849: do {
850: mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
851: }
852: while (mbuf_len > 3 && mbuf[3] == '-');
853: /* STARTTLS command requires EHLO... */
854: BIO_printf(fbio, "%cHLO openssl.client.net\r\n",
855: starttls_proto == PROTO_SMTP ? 'E' : 'L');
856: (void) BIO_flush(fbio);
857: /* wait for multi-line response to end EHLO SMTP response */
858: do {
859: mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
860: if (strstr(mbuf, "STARTTLS"))
861: foundit = 1;
862: }
863: while (mbuf_len > 3 && mbuf[3] == '-');
864: (void) BIO_flush(fbio);
865: BIO_pop(fbio);
866: BIO_free(fbio);
867: if (!foundit)
868: BIO_printf(bio_err,
1.36 jmc 869: "didn't find starttls in server response,"
1.1 jsing 870: " try anyway...\n");
871: BIO_printf(sbio, "STARTTLS\r\n");
872: BIO_read(sbio, sbuf, BUFSIZZ);
873: } else if (starttls_proto == PROTO_POP3) {
874: mbuf_len = BIO_read(sbio, mbuf, BUFSIZZ);
875: if (mbuf_len == -1) {
876: BIO_printf(bio_err, "BIO_read failed\n");
877: goto end;
878: }
879: BIO_printf(sbio, "STLS\r\n");
880: BIO_read(sbio, sbuf, BUFSIZZ);
881: } else if (starttls_proto == PROTO_IMAP) {
882: int foundit = 0;
883: BIO *fbio = BIO_new(BIO_f_buffer());
884: BIO_push(fbio, sbio);
885: BIO_gets(fbio, mbuf, BUFSIZZ);
886: /* STARTTLS command requires CAPABILITY... */
887: BIO_printf(fbio, ". CAPABILITY\r\n");
888: (void) BIO_flush(fbio);
889: /* wait for multi-line CAPABILITY response */
890: do {
891: mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
892: if (strstr(mbuf, "STARTTLS"))
893: foundit = 1;
894: }
895: while (mbuf_len > 3 && mbuf[0] != '.');
896: (void) BIO_flush(fbio);
897: BIO_pop(fbio);
898: BIO_free(fbio);
899: if (!foundit)
900: BIO_printf(bio_err,
1.37 tb 901: "didn't find STARTTLS in server response,"
1.1 jsing 902: " try anyway...\n");
903: BIO_printf(sbio, ". STARTTLS\r\n");
904: BIO_read(sbio, sbuf, BUFSIZZ);
905: } else if (starttls_proto == PROTO_FTP) {
906: BIO *fbio = BIO_new(BIO_f_buffer());
907: BIO_push(fbio, sbio);
908: /* wait for multi-line response to end from FTP */
909: do {
910: mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
911: }
912: while (mbuf_len > 3 && mbuf[3] == '-');
913: (void) BIO_flush(fbio);
914: BIO_pop(fbio);
915: BIO_free(fbio);
916: BIO_printf(sbio, "AUTH TLS\r\n");
917: BIO_read(sbio, sbuf, BUFSIZZ);
1.12 bluhm 918: } else if (starttls_proto == PROTO_XMPP) {
1.1 jsing 919: int seen = 0;
920: BIO_printf(sbio, "<stream:stream "
921: "xmlns:stream='http://etherx.jabber.org/streams' "
1.15 landry 922: "xmlns='jabber:client' to='%s' version='1.0'>", xmpphost ? xmpphost : host);
1.1 jsing 923: seen = BIO_read(sbio, mbuf, BUFSIZZ);
1.15 landry 924:
925: if (seen <= 0)
926: goto shut;
927:
1.1 jsing 928: mbuf[seen] = 0;
1.15 landry 929: while (!strstr(mbuf, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'") &&
930: !strstr(mbuf, "<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"")) {
931: seen = BIO_read(sbio, mbuf, BUFSIZZ);
932:
933: if (seen <= 0)
1.1 jsing 934: goto shut;
1.15 landry 935:
1.1 jsing 936: mbuf[seen] = 0;
937: }
938: BIO_printf(sbio, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
939: seen = BIO_read(sbio, sbuf, BUFSIZZ);
940: sbuf[seen] = 0;
941: if (!strstr(sbuf, "<proceed"))
942: goto shut;
943: mbuf[0] = 0;
1.12 bluhm 944: } else if (proxy != NULL) {
945: BIO_printf(sbio, "CONNECT %s HTTP/1.0\r\n\r\n", connect);
946: mbuf_len = BIO_read(sbio, mbuf, BUFSIZZ);
947: if (mbuf_len == -1) {
948: BIO_printf(bio_err, "BIO_read failed\n");
949: goto end;
950: }
1.1 jsing 951: }
952: for (;;) {
1.9 deraadt 953: struct pollfd pfd[3]; /* stdin, stdout, socket */
954: int ptimeout = -1;
1.1 jsing 955:
956: if ((SSL_version(con) == DTLS1_VERSION) &&
957: DTLSv1_get_timeout(con, &timeout))
1.9 deraadt 958: ptimeout = timeout.tv_sec * 1000 + timeout.tv_usec / 1000;
1.1 jsing 959:
960: if (SSL_in_init(con) && !SSL_total_renegotiations(con)) {
961: in_init = 1;
962: tty_on = 0;
963: } else {
964: tty_on = 1;
965: if (in_init) {
966: in_init = 0;
967: if (sess_out) {
968: BIO *stmp = BIO_new_file(sess_out, "w");
969: if (stmp) {
970: PEM_write_bio_SSL_SESSION(stmp, SSL_get_session(con));
971: BIO_free(stmp);
972: } else
973: BIO_printf(bio_err, "Error writing session file %s\n", sess_out);
974: }
975: print_stuff(bio_c_out, con, full_log);
976: if (full_log > 0)
977: full_log--;
978:
979: if (starttls_proto) {
980: BIO_write(bio_err, mbuf, mbuf_len);
981: /* We don't need to know any more */
982: starttls_proto = PROTO_OFF;
983: }
984: if (reconnect) {
985: reconnect--;
986: BIO_printf(bio_c_out, "drop connection and then reconnect\n");
987: SSL_shutdown(con);
988: SSL_set_connect_state(con);
989: shutdown(SSL_get_fd(con), SHUT_RD);
990: close(SSL_get_fd(con));
991: goto re_start;
992: }
993: }
994: }
995:
996: ssl_pending = read_ssl && SSL_pending(con);
997:
1.9 deraadt 998: pfd[0].fd = -1;
999: pfd[1].fd = -1;
1.1 jsing 1000: if (!ssl_pending) {
1001: if (tty_on) {
1.9 deraadt 1002: if (read_tty) {
1003: pfd[0].fd = fileno(stdin);
1004: pfd[0].events = POLLIN;
1005: }
1006: if (write_tty) {
1007: pfd[1].fd = fileno(stdout);
1008: pfd[1].events = POLLOUT;
1009: }
1.1 jsing 1010: }
1.9 deraadt 1011:
1012: pfd[2].fd = SSL_get_fd(con);
1013: pfd[2].events = 0;
1.1 jsing 1014: if (read_ssl)
1.9 deraadt 1015: pfd[2].events |= POLLIN;
1.1 jsing 1016: if (write_ssl)
1.9 deraadt 1017: pfd[2].events |= POLLOUT;
1018:
1.1 jsing 1019: /* printf("mode tty(%d %d%d) ssl(%d%d)\n",
1020: tty_on,read_tty,write_tty,read_ssl,write_ssl);*/
1021:
1.9 deraadt 1022: i = poll(pfd, 3, ptimeout);
1.38 deraadt 1023: if (i == -1) {
1.1 jsing 1024: BIO_printf(bio_err, "bad select %d\n",
1025: errno);
1026: goto shut;
1027: /* goto end; */
1028: }
1029: }
1030: if ((SSL_version(con) == DTLS1_VERSION) && DTLSv1_handle_timeout(con) > 0) {
1031: BIO_printf(bio_err, "TIMEOUT occured\n");
1032: }
1.9 deraadt 1033: if (!ssl_pending && (pfd[2].revents & (POLLOUT|POLLERR|POLLNVAL))) {
1034: if (pfd[2].revents & (POLLERR|POLLNVAL)) {
1035: BIO_printf(bio_err, "poll error");
1036: goto shut;
1037: }
1.1 jsing 1038: k = SSL_write(con, &(cbuf[cbuf_off]),
1039: (unsigned int) cbuf_len);
1040: switch (SSL_get_error(con, k)) {
1041: case SSL_ERROR_NONE:
1042: cbuf_off += k;
1043: cbuf_len -= k;
1044: if (k <= 0)
1045: goto end;
1046: /* we have done a write(con,NULL,0); */
1047: if (cbuf_len <= 0) {
1048: read_tty = 1;
1049: write_ssl = 0;
1050: } else { /* if (cbuf_len > 0) */
1051: read_tty = 0;
1052: write_ssl = 1;
1053: }
1054: break;
1055: case SSL_ERROR_WANT_WRITE:
1056: BIO_printf(bio_c_out, "write W BLOCK\n");
1057: write_ssl = 1;
1058: read_tty = 0;
1059: break;
1060: case SSL_ERROR_WANT_READ:
1061: BIO_printf(bio_c_out, "write R BLOCK\n");
1062: write_tty = 0;
1063: read_ssl = 1;
1064: write_ssl = 0;
1065: break;
1066: case SSL_ERROR_WANT_X509_LOOKUP:
1067: BIO_printf(bio_c_out, "write X BLOCK\n");
1068: break;
1069: case SSL_ERROR_ZERO_RETURN:
1070: if (cbuf_len != 0) {
1071: BIO_printf(bio_c_out, "shutdown\n");
1072: ret = 0;
1073: goto shut;
1074: } else {
1075: read_tty = 1;
1076: write_ssl = 0;
1077: break;
1078: }
1079:
1080: case SSL_ERROR_SYSCALL:
1081: if ((k != 0) || (cbuf_len != 0)) {
1082: BIO_printf(bio_err, "write:errno=%d\n",
1083: errno);
1084: goto shut;
1085: } else {
1086: read_tty = 1;
1087: write_ssl = 0;
1088: }
1089: break;
1090: case SSL_ERROR_SSL:
1091: ERR_print_errors(bio_err);
1092: goto shut;
1093: }
1.9 deraadt 1094: } else if (!ssl_pending &&
1095: (pfd[1].revents & (POLLOUT|POLLERR|POLLNVAL))) {
1096: if (pfd[1].revents & (POLLERR|POLLNVAL)) {
1097: BIO_printf(bio_err, "poll error");
1098: goto shut;
1099: }
1.1 jsing 1100: i = write(fileno(stdout), &(sbuf[sbuf_off]), sbuf_len);
1101:
1102: if (i <= 0) {
1103: BIO_printf(bio_c_out, "DONE\n");
1104: ret = 0;
1105: goto shut;
1106: /* goto end; */
1107: }
1108: sbuf_len -= i;
1109: sbuf_off += i;
1110: if (sbuf_len <= 0) {
1111: read_ssl = 1;
1112: write_tty = 0;
1113: }
1.9 deraadt 1114: } else if (ssl_pending || (pfd[2].revents & (POLLIN|POLLHUP))) {
1.1 jsing 1115: #ifdef RENEG
1116: {
1117: static int iiii;
1118: if (++iiii == 52) {
1119: SSL_renegotiate(con);
1120: iiii = 0;
1121: }
1122: }
1123: #endif
1.39 beck 1124: if (peekaboo) {
1.40 ! jsing 1125: k = p = SSL_peek(con, pbuf, 1024 /* BUFSIZZ */ );
! 1126: if (SSL_get_error(con, p) == SSL_ERROR_NONE) {
1.39 beck 1127: if (p <= 0)
1128: goto end;
1129: pbuf_off = 0;
1130: pbuf_len = p;
1131:
1.40 ! jsing 1132: k = SSL_read(con, sbuf, p);
1.39 beck 1133: }
1.40 ! jsing 1134: } else {
! 1135: k = SSL_read(con, sbuf, 1024 /* BUFSIZZ */ );
1.39 beck 1136: }
1.1 jsing 1137:
1138: switch (SSL_get_error(con, k)) {
1139: case SSL_ERROR_NONE:
1140: if (k <= 0)
1141: goto end;
1142: sbuf_off = 0;
1143: sbuf_len = k;
1.39 beck 1144: if (peekaboo) {
1145: if (k < p) {
1146: ret = -1;
1147: BIO_printf(bio_err,
1148: "read less than peek!\n");
1149: goto shut;
1150: }
1151: if (p > 0 && (memcmp(sbuf, pbuf, p) != 0)) {
1152: ret = -1;
1153: BIO_printf(bio_err,
1154: "peek of %d different from read of %d!\n",
1155: p, k);
1156: goto shut;
1157: }
1158: }
1.1 jsing 1159: read_ssl = 0;
1160: write_tty = 1;
1161: break;
1162: case SSL_ERROR_WANT_WRITE:
1163: BIO_printf(bio_c_out, "read W BLOCK\n");
1164: write_ssl = 1;
1165: read_tty = 0;
1166: break;
1167: case SSL_ERROR_WANT_READ:
1168: BIO_printf(bio_c_out, "read R BLOCK\n");
1169: write_tty = 0;
1170: read_ssl = 1;
1171: if ((read_tty == 0) && (write_ssl == 0))
1172: write_ssl = 1;
1173: break;
1174: case SSL_ERROR_WANT_X509_LOOKUP:
1175: BIO_printf(bio_c_out, "read X BLOCK\n");
1176: break;
1177: case SSL_ERROR_SYSCALL:
1178: ret = errno;
1179: BIO_printf(bio_err, "read:errno=%d\n", ret);
1180: goto shut;
1181: case SSL_ERROR_ZERO_RETURN:
1182: BIO_printf(bio_c_out, "closed\n");
1183: ret = 0;
1184: goto shut;
1185: case SSL_ERROR_SSL:
1186: ERR_print_errors(bio_err);
1187: goto shut;
1188: /* break; */
1189: }
1.9 deraadt 1190: } else if (pfd[0].revents) {
1191: if (pfd[0].revents & (POLLERR|POLLNVAL)) {
1192: BIO_printf(bio_err, "poll error");
1193: goto shut;
1194: }
1.1 jsing 1195: if (crlf) {
1196: int j, lf_num;
1197:
1198: i = read(fileno(stdin), cbuf, BUFSIZZ / 2);
1199: lf_num = 0;
1200: /* both loops are skipped when i <= 0 */
1201: for (j = 0; j < i; j++)
1202: if (cbuf[j] == '\n')
1203: lf_num++;
1204: for (j = i - 1; j >= 0; j--) {
1205: cbuf[j + lf_num] = cbuf[j];
1206: if (cbuf[j] == '\n') {
1207: lf_num--;
1208: i++;
1209: cbuf[j + lf_num] = '\r';
1210: }
1211: }
1212: assert(lf_num == 0);
1213: } else
1214: i = read(fileno(stdin), cbuf, BUFSIZZ);
1215:
1216: if ((!c_ign_eof) && ((i <= 0) || (cbuf[0] == 'Q'))) {
1217: BIO_printf(bio_err, "DONE\n");
1218: ret = 0;
1219: goto shut;
1220: }
1221: if ((!c_ign_eof) && (cbuf[0] == 'R')) {
1222: BIO_printf(bio_err, "RENEGOTIATING\n");
1223: SSL_renegotiate(con);
1224: cbuf_len = 0;
1225: } else {
1226: cbuf_len = i;
1227: cbuf_off = 0;
1228: }
1229:
1230: write_ssl = 1;
1231: read_tty = 0;
1232: }
1233: }
1234:
1235: ret = 0;
1.35 jsing 1236: shut:
1.1 jsing 1237: if (in_init)
1238: print_stuff(bio_c_out, con, full_log);
1239: SSL_shutdown(con);
1240: shutdown(SSL_get_fd(con), SHUT_RD);
1241: close(SSL_get_fd(con));
1.35 jsing 1242: end:
1.1 jsing 1243: if (con != NULL) {
1244: if (prexit != 0)
1245: print_stuff(bio_c_out, con, 1);
1246: SSL_free(con);
1247: }
1.34 jsing 1248: SSL_CTX_free(ctx);
1249: X509_free(cert);
1250: EVP_PKEY_free(key);
1.1 jsing 1251: free(pass);
1.34 jsing 1252: X509_VERIFY_PARAM_free(vpm);
1.32 deraadt 1253: freezero(cbuf, BUFSIZZ);
1254: freezero(sbuf, BUFSIZZ);
1255: freezero(mbuf, BUFSIZZ);
1.1 jsing 1256: if (bio_c_out != NULL) {
1257: BIO_free(bio_c_out);
1258: bio_c_out = NULL;
1259: }
1260:
1261: return (ret);
1262: }
1263:
1264:
1265: static void
1266: print_stuff(BIO * bio, SSL * s, int full)
1267: {
1268: X509 *peer = NULL;
1269: char *p;
1270: static const char *space = " ";
1271: char buf[BUFSIZ];
1272: STACK_OF(X509) * sk;
1273: STACK_OF(X509_NAME) * sk2;
1274: const SSL_CIPHER *c;
1275: X509_NAME *xn;
1276: int j, i;
1277: unsigned char *exportedkeymat;
1278:
1279: if (full) {
1280: int got_a_chain = 0;
1281:
1282: sk = SSL_get_peer_cert_chain(s);
1283: if (sk != NULL) {
1284: got_a_chain = 1; /* we don't have it for SSL2
1285: * (yet) */
1286:
1287: BIO_printf(bio, "---\nCertificate chain\n");
1288: for (i = 0; i < sk_X509_num(sk); i++) {
1289: X509_NAME_oneline(X509_get_subject_name(
1290: sk_X509_value(sk, i)), buf, sizeof buf);
1291: BIO_printf(bio, "%2d s:%s\n", i, buf);
1292: X509_NAME_oneline(X509_get_issuer_name(
1293: sk_X509_value(sk, i)), buf, sizeof buf);
1294: BIO_printf(bio, " i:%s\n", buf);
1295: if (c_showcerts)
1296: PEM_write_bio_X509(bio, sk_X509_value(sk, i));
1297: }
1298: }
1299: BIO_printf(bio, "---\n");
1300: peer = SSL_get_peer_certificate(s);
1301: if (peer != NULL) {
1302: BIO_printf(bio, "Server certificate\n");
1303: if (!(c_showcerts && got_a_chain)) /* Redundant if we
1304: * showed the whole
1305: * chain */
1306: PEM_write_bio_X509(bio, peer);
1307: X509_NAME_oneline(X509_get_subject_name(peer),
1308: buf, sizeof buf);
1309: BIO_printf(bio, "subject=%s\n", buf);
1310: X509_NAME_oneline(X509_get_issuer_name(peer),
1311: buf, sizeof buf);
1312: BIO_printf(bio, "issuer=%s\n", buf);
1313: } else
1314: BIO_printf(bio, "no peer certificate available\n");
1315:
1316: sk2 = SSL_get_client_CA_list(s);
1317: if ((sk2 != NULL) && (sk_X509_NAME_num(sk2) > 0)) {
1318: BIO_printf(bio, "---\nAcceptable client certificate CA names\n");
1319: for (i = 0; i < sk_X509_NAME_num(sk2); i++) {
1320: xn = sk_X509_NAME_value(sk2, i);
1321: X509_NAME_oneline(xn, buf, sizeof(buf));
1322: BIO_write(bio, buf, strlen(buf));
1323: BIO_write(bio, "\n", 1);
1324: }
1325: } else {
1326: BIO_printf(bio, "---\nNo client certificate CA names sent\n");
1327: }
1328: p = SSL_get_shared_ciphers(s, buf, sizeof buf);
1329: if (p != NULL) {
1330: /*
1331: * This works only for SSL 2. In later protocol
1332: * versions, the client does not know what other
1333: * ciphers (in addition to the one to be used in the
1334: * current connection) the server supports.
1335: */
1336:
1337: BIO_printf(bio, "---\nCiphers common between both SSL endpoints:\n");
1338: j = i = 0;
1339: while (*p) {
1340: if (*p == ':') {
1341: BIO_write(bio, space, 15 - j % 25);
1342: i++;
1343: j = 0;
1344: BIO_write(bio, ((i % 3) ? " " : "\n"), 1);
1345: } else {
1346: BIO_write(bio, p, 1);
1347: j++;
1348: }
1349: p++;
1350: }
1351: BIO_write(bio, "\n", 1);
1352: }
1.29 jsing 1353:
1354: ssl_print_tmp_key(bio, s);
1355:
1.1 jsing 1356: BIO_printf(bio, "---\nSSL handshake has read %ld bytes and written %ld bytes\n",
1357: BIO_number_read(SSL_get_rbio(s)),
1358: BIO_number_written(SSL_get_wbio(s)));
1359: }
1360: BIO_printf(bio, (SSL_cache_hit(s) ? "---\nReused, " : "---\nNew, "));
1361: c = SSL_get_current_cipher(s);
1362: BIO_printf(bio, "%s, Cipher is %s\n",
1363: SSL_CIPHER_get_version(c),
1364: SSL_CIPHER_get_name(c));
1365: if (peer != NULL) {
1366: EVP_PKEY *pktmp;
1367: pktmp = X509_get_pubkey(peer);
1368: BIO_printf(bio, "Server public key is %d bit\n",
1369: EVP_PKEY_bits(pktmp));
1370: EVP_PKEY_free(pktmp);
1371: }
1372: BIO_printf(bio, "Secure Renegotiation IS%s supported\n",
1373: SSL_get_secure_renegotiation_support(s) ? "" : " NOT");
1374:
1375: /* Compression is not supported and will always be none. */
1376: BIO_printf(bio, "Compression: NONE\n");
1377: BIO_printf(bio, "Expansion: NONE\n");
1378:
1379: #ifdef SSL_DEBUG
1380: {
1381: /* Print out local port of connection: useful for debugging */
1382: int sock;
1383: struct sockaddr_in ladd;
1384: socklen_t ladd_size = sizeof(ladd);
1385: sock = SSL_get_fd(s);
1386: getsockname(sock, (struct sockaddr *) & ladd, &ladd_size);
1387: BIO_printf(bio_c_out, "LOCAL PORT is %u\n", ntohs(ladd.sin_port));
1388: }
1389: #endif
1390:
1.10 jsing 1391: {
1392: const unsigned char *proto;
1393: unsigned int proto_len;
1394: SSL_get0_alpn_selected(s, &proto, &proto_len);
1395: if (proto_len > 0) {
1396: BIO_printf(bio, "ALPN protocol: ");
1397: BIO_write(bio, proto, proto_len);
1398: BIO_write(bio, "\n", 1);
1399: } else
1400: BIO_printf(bio, "No ALPN negotiated\n");
1401: }
1.1 jsing 1402:
1403: #ifndef OPENSSL_NO_SRTP
1404: {
1405: SRTP_PROTECTION_PROFILE *srtp_profile = SSL_get_selected_srtp_profile(s);
1406:
1407: if (srtp_profile)
1408: BIO_printf(bio, "SRTP Extension negotiated, profile=%s\n",
1409: srtp_profile->name);
1410: }
1411: #endif
1412:
1413: SSL_SESSION_print(bio, SSL_get_session(s));
1414: if (keymatexportlabel != NULL) {
1415: BIO_printf(bio, "Keying material exporter:\n");
1416: BIO_printf(bio, " Label: '%s'\n", keymatexportlabel);
1417: BIO_printf(bio, " Length: %i bytes\n", keymatexportlen);
1418: exportedkeymat = malloc(keymatexportlen);
1419: if (exportedkeymat != NULL) {
1420: if (!SSL_export_keying_material(s, exportedkeymat,
1421: keymatexportlen,
1422: keymatexportlabel,
1423: strlen(keymatexportlabel),
1424: NULL, 0, 0)) {
1425: BIO_printf(bio, " Error\n");
1426: } else {
1427: BIO_printf(bio, " Keying material: ");
1428: for (i = 0; i < keymatexportlen; i++)
1429: BIO_printf(bio, "%02X",
1430: exportedkeymat[i]);
1431: BIO_printf(bio, "\n");
1432: }
1433: free(exportedkeymat);
1434: }
1435: }
1436: BIO_printf(bio, "---\n");
1.34 jsing 1437: X509_free(peer);
1.1 jsing 1438: /* flush, or debugging output gets mixed with http response */
1439: (void) BIO_flush(bio);
1440: }
1441:
1442:
1443: static int
1444: ocsp_resp_cb(SSL * s, void *arg)
1445: {
1446: const unsigned char *p;
1447: int len;
1448: OCSP_RESPONSE *rsp;
1449: len = SSL_get_tlsext_status_ocsp_resp(s, &p);
1450: BIO_puts(arg, "OCSP response: ");
1451: if (!p) {
1452: BIO_puts(arg, "no response sent\n");
1453: return 1;
1454: }
1455: rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
1456: if (!rsp) {
1457: BIO_puts(arg, "response parse error\n");
1458: BIO_dump_indent(arg, (char *) p, len, 4);
1459: return 0;
1460: }
1461: BIO_puts(arg, "\n======================================\n");
1462: OCSP_RESPONSE_print(arg, rsp, 0);
1463: BIO_puts(arg, "======================================\n");
1464: OCSP_RESPONSE_free(rsp);
1465: return 1;
1466: }
1467: