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