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