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