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