Annotation of src/usr.bin/openssl/s_client.c, Revision 1.8
1.8 ! krw 1: /* $OpenBSD: s_client.c,v 1.7 2014/11/07 14:16:48 jsing Exp $ */
1.1 jsing 2: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3: * All rights reserved.
4: *
5: * This package is an SSL implementation written
6: * by Eric Young (eay@cryptsoft.com).
7: * The implementation was written so as to conform with Netscapes SSL.
8: *
9: * This library is free for commercial and non-commercial use as long as
10: * the following conditions are aheared to. The following conditions
11: * apply to all code found in this distribution, be it the RC4, RSA,
12: * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13: * included with this distribution is covered by the same copyright terms
14: * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15: *
16: * Copyright remains Eric Young's, and as such any Copyright notices in
17: * the code are not to be removed.
18: * If this package is used in a product, Eric Young should be given attribution
19: * as the author of the parts of the library used.
20: * This can be in the form of a textual message at program startup or
21: * in documentation (online or textual) provided with the package.
22: *
23: * Redistribution and use in source and binary forms, with or without
24: * modification, are permitted provided that the following conditions
25: * are met:
26: * 1. Redistributions of source code must retain the copyright
27: * notice, this list of conditions and the following disclaimer.
28: * 2. Redistributions in binary form must reproduce the above copyright
29: * notice, this list of conditions and the following disclaimer in the
30: * documentation and/or other materials provided with the distribution.
31: * 3. All advertising materials mentioning features or use of this software
32: * must display the following acknowledgement:
33: * "This product includes cryptographic software written by
34: * Eric Young (eay@cryptsoft.com)"
35: * The word 'cryptographic' can be left out if the rouines from the library
36: * being used are not cryptographic related :-).
37: * 4. If you include any Windows specific code (or a derivative thereof) from
38: * the apps directory (application code) you must include an acknowledgement:
39: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40: *
41: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51: * SUCH DAMAGE.
52: *
53: * The licence and distribution terms for any publically available version or
54: * derivative of this code cannot be changed. i.e. this code cannot simply be
55: * copied and put under another distribution licence
56: * [including the GNU Public Licence.]
57: */
58: /* ====================================================================
59: * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
60: *
61: * Redistribution and use in source and binary forms, with or without
62: * modification, are permitted provided that the following conditions
63: * are met:
64: *
65: * 1. Redistributions of source code must retain the above copyright
66: * notice, this list of conditions and the following disclaimer.
67: *
68: * 2. Redistributions in binary form must reproduce the above copyright
69: * notice, this list of conditions and the following disclaimer in
70: * the documentation and/or other materials provided with the
71: * distribution.
72: *
73: * 3. All advertising materials mentioning features or use of this
74: * software must display the following acknowledgment:
75: * "This product includes software developed by the OpenSSL Project
76: * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77: *
78: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79: * endorse or promote products derived from this software without
80: * prior written permission. For written permission, please contact
81: * openssl-core@openssl.org.
82: *
83: * 5. Products derived from this software may not be called "OpenSSL"
84: * nor may "OpenSSL" appear in their names without prior written
85: * permission of the OpenSSL Project.
86: *
87: * 6. Redistributions of any form whatsoever must retain the following
88: * acknowledgment:
89: * "This product includes software developed by the OpenSSL Project
90: * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91: *
92: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103: * OF THE POSSIBILITY OF SUCH DAMAGE.
104: * ====================================================================
105: *
106: * This product includes cryptographic software written by Eric Young
107: * (eay@cryptsoft.com). This product includes software written by Tim
108: * Hudson (tjh@cryptsoft.com).
109: *
110: */
111: /* ====================================================================
112: * Copyright 2005 Nokia. All rights reserved.
113: *
114: * The portions of the attached software ("Contribution") is developed by
115: * Nokia Corporation and is licensed pursuant to the OpenSSL open source
116: * license.
117: *
118: * The Contribution, originally written by Mika Kousa and Pasi Eronen of
119: * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
120: * support (see RFC 4279) to OpenSSL.
121: *
122: * No patent licenses or other rights except those expressly stated in
123: * the OpenSSL open source license shall be deemed granted or received
124: * expressly, by implication, estoppel, or otherwise.
125: *
126: * No assurances are provided by Nokia that the Contribution does not
127: * infringe the patent or other intellectual property rights of any third
128: * party or that the license provides you with all the necessary rights
129: * to make use of the Contribution.
130: *
131: * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
132: * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
133: * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
134: * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
135: * OTHERWISE.
136: */
137:
138: #include <sys/types.h>
139: #include <sys/ioctl.h>
140: #include <sys/select.h>
141: #include <sys/socket.h>
142:
143: #include <netinet/in.h>
144:
145: #include <assert.h>
146: #include <ctype.h>
147: #include <limits.h>
148: #include <netdb.h>
149: #include <stdio.h>
150: #include <stdlib.h>
151: #include <string.h>
152: #include <unistd.h>
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
256: #ifndef OPENSSL_NO_SRTP
257: BIO_printf(bio_err, " -use_srtp profiles - Offer SRTP key management with a colon-separated profile list\n");
258: #endif
259: BIO_printf(bio_err, " -keymatexport label - Export keying material using label\n");
260: BIO_printf(bio_err, " -keymatexportlen len - Export len bytes of keying material (default 20)\n");
261: }
262:
263:
264: /* This is a context that we pass to callbacks */
265: typedef struct tlsextctx_st {
266: BIO *biodebug;
267: int ack;
268: } tlsextctx;
269:
270:
271: static int
272: ssl_servername_cb(SSL * s, int *ad, void *arg)
273: {
274: tlsextctx *p = (tlsextctx *) arg;
275: const char *hn = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
276: if (SSL_get_servername_type(s) != -1)
277: p->ack = !SSL_session_reused(s) && hn != NULL;
278: else
279: BIO_printf(bio_err, "Can't use SSL_get_servername\n");
280:
281: return SSL_TLSEXT_ERR_OK;
282: }
283:
284: #ifndef OPENSSL_NO_SRTP
285: char *srtp_profiles = NULL;
286: #endif
287:
288: #ifndef OPENSSL_NO_NEXTPROTONEG
289: /* This the context that we pass to next_proto_cb */
290: typedef struct tlsextnextprotoctx_st {
291: unsigned char *data;
292: unsigned short len;
293: int status;
294: } tlsextnextprotoctx;
295:
296: static tlsextnextprotoctx next_proto;
297:
298: static int
299: next_proto_cb(SSL * s, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)
300: {
301: tlsextnextprotoctx *ctx = arg;
302:
303: if (!c_quiet) {
304: /* We can assume that |in| is syntactically valid. */
305: unsigned i;
306: BIO_printf(bio_c_out, "Protocols advertised by server: ");
307: for (i = 0; i < inlen;) {
308: if (i)
309: BIO_write(bio_c_out, ", ", 2);
310: BIO_write(bio_c_out, &in[i + 1], in[i]);
311: i += in[i] + 1;
312: }
313: BIO_write(bio_c_out, "\n", 1);
314: }
315: ctx->status = SSL_select_next_proto(out, outlen, in, inlen, ctx->data, ctx->len);
316: return SSL_TLSEXT_ERR_OK;
317: }
318: #endif /* ndef OPENSSL_NO_NEXTPROTONEG */
319:
320: enum {
321: PROTO_OFF = 0,
322: PROTO_SMTP,
323: PROTO_LMTP,
324: PROTO_POP3,
325: PROTO_IMAP,
326: PROTO_FTP,
327: PROTO_XMPP
328: };
329:
330: int s_client_main(int, char **);
331:
332: int
333: s_client_main(int argc, char **argv)
334: {
335: unsigned int off = 0, clr = 0;
336: SSL *con = NULL;
337: int s, k, width, state = 0, af = AF_UNSPEC;
338: char *cbuf = NULL, *sbuf = NULL, *mbuf = NULL;
339: int cbuf_len, cbuf_off;
340: int sbuf_len, sbuf_off;
341: fd_set readfds, writefds;
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;
364: struct timeval timeout, *timeoutp;
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
378: char *sess_in = NULL;
379: char *sess_out = NULL;
380: struct sockaddr peer;
381: int peerlen = sizeof(peer);
382: int enable_timeouts = 0;
383: long socket_mtu = 0;
384:
385: meth = SSLv23_client_method();
386:
387: c_Pause = 0;
388: c_quiet = 0;
389: c_ign_eof = 0;
390: c_debug = 0;
391: c_msg = 0;
392: c_showcerts = 0;
393:
394: if (((cbuf = malloc(BUFSIZZ)) == NULL) ||
395: ((sbuf = malloc(BUFSIZZ)) == NULL) ||
396: ((mbuf = malloc(BUFSIZZ + 1)) == NULL)) { /* NUL byte */
397: BIO_printf(bio_err, "out of memory\n");
398: goto end;
399: }
400: verify_depth = 0;
401: verify_error = X509_V_OK;
402: c_nbio = 0;
403:
404: argc--;
405: argv++;
406: while (argc >= 1) {
407: if (strcmp(*argv, "-host") == 0) {
408: if (--argc < 1)
409: goto bad;
410: host = *(++argv);
411: } else if (strcmp(*argv, "-port") == 0) {
412: if (--argc < 1)
413: goto bad;
414: port = *(++argv);
415: if (port == NULL || *port == '\0')
416: goto bad;
417: } else if (strcmp(*argv, "-connect") == 0) {
418: if (--argc < 1)
419: goto bad;
420: if (!extract_host_port(*(++argv), &host, NULL, &port))
421: goto bad;
422: } else if (strcmp(*argv, "-verify") == 0) {
423: verify = SSL_VERIFY_PEER;
424: if (--argc < 1)
425: goto bad;
426: verify_depth = strtonum(*(++argv), 0, INT_MAX, &errstr);
427: if (errstr)
428: goto bad;
429: BIO_printf(bio_err, "verify depth is %d\n", verify_depth);
430: } else if (strcmp(*argv, "-cert") == 0) {
431: if (--argc < 1)
432: goto bad;
433: cert_file = *(++argv);
434: } else if (strcmp(*argv, "-sess_out") == 0) {
435: if (--argc < 1)
436: goto bad;
437: sess_out = *(++argv);
438: } else if (strcmp(*argv, "-sess_in") == 0) {
439: if (--argc < 1)
440: goto bad;
441: sess_in = *(++argv);
442: } else if (strcmp(*argv, "-certform") == 0) {
443: if (--argc < 1)
444: goto bad;
445: cert_format = str2fmt(*(++argv));
446: } else if (args_verify(&argv, &argc, &badarg, bio_err, &vpm)) {
447: if (badarg)
448: goto bad;
449: continue;
450: } else if (strcmp(*argv, "-verify_return_error") == 0)
451: verify_return_error = 1;
452: else if (strcmp(*argv, "-prexit") == 0)
453: prexit = 1;
454: else if (strcmp(*argv, "-crlf") == 0)
455: crlf = 1;
456: else if (strcmp(*argv, "-quiet") == 0) {
457: c_quiet = 1;
458: c_ign_eof = 1;
459: } else if (strcmp(*argv, "-ign_eof") == 0)
460: c_ign_eof = 1;
461: else if (strcmp(*argv, "-no_ign_eof") == 0)
462: c_ign_eof = 0;
463: else if (strcmp(*argv, "-pause") == 0)
464: c_Pause = 1;
465: else if (strcmp(*argv, "-debug") == 0)
466: c_debug = 1;
467: else if (strcmp(*argv, "-tlsextdebug") == 0)
468: c_tlsextdebug = 1;
469: else if (strcmp(*argv, "-status") == 0)
470: c_status_req = 1;
471: else if (strcmp(*argv, "-msg") == 0)
472: c_msg = 1;
473: else if (strcmp(*argv, "-showcerts") == 0)
474: c_showcerts = 1;
475: else if (strcmp(*argv, "-nbio_test") == 0)
476: nbio_test = 1;
477: else if (strcmp(*argv, "-state") == 0)
478: state = 1;
479: else if (strcmp(*argv, "-ssl3") == 0)
480: meth = SSLv3_client_method();
481: else if (strcmp(*argv, "-tls1_2") == 0)
482: meth = TLSv1_2_client_method();
483: else if (strcmp(*argv, "-tls1_1") == 0)
484: meth = TLSv1_1_client_method();
485: else if (strcmp(*argv, "-tls1") == 0)
486: meth = TLSv1_client_method();
487: #ifndef OPENSSL_NO_DTLS1
488: else if (strcmp(*argv, "-dtls1") == 0) {
489: meth = DTLSv1_client_method();
490: socket_type = SOCK_DGRAM;
491: } else if (strcmp(*argv, "-timeout") == 0)
492: enable_timeouts = 1;
493: else if (strcmp(*argv, "-mtu") == 0) {
494: if (--argc < 1)
495: goto bad;
496: socket_mtu = strtonum(*(++argv), 0, LONG_MAX, &errstr);
497: if (errstr)
498: goto bad;
499: }
500: #endif
501: else if (strcmp(*argv, "-bugs") == 0)
502: bugs = 1;
503: else if (strcmp(*argv, "-keyform") == 0) {
504: if (--argc < 1)
505: goto bad;
506: key_format = str2fmt(*(++argv));
507: } else if (strcmp(*argv, "-pass") == 0) {
508: if (--argc < 1)
509: goto bad;
510: passarg = *(++argv);
511: } else if (strcmp(*argv, "-key") == 0) {
512: if (--argc < 1)
513: goto bad;
514: key_file = *(++argv);
515: } else if (strcmp(*argv, "-reconnect") == 0) {
516: reconnect = 5;
517: } else if (strcmp(*argv, "-CApath") == 0) {
518: if (--argc < 1)
519: goto bad;
520: CApath = *(++argv);
521: } else if (strcmp(*argv, "-CAfile") == 0) {
522: if (--argc < 1)
523: goto bad;
524: CAfile = *(++argv);
525: } else if (strcmp(*argv, "-no_tls1_2") == 0)
526: off |= SSL_OP_NO_TLSv1_2;
527: else if (strcmp(*argv, "-no_tls1_1") == 0)
528: off |= SSL_OP_NO_TLSv1_1;
529: else if (strcmp(*argv, "-no_tls1") == 0)
530: off |= SSL_OP_NO_TLSv1;
531: else if (strcmp(*argv, "-no_ssl3") == 0)
532: off |= SSL_OP_NO_SSLv3;
533: else if (strcmp(*argv, "-no_ssl2") == 0)
534: off |= SSL_OP_NO_SSLv2;
535: else if (strcmp(*argv, "-no_comp") == 0) {
536: off |= SSL_OP_NO_COMPRESSION;
537: }
538: else if (strcmp(*argv, "-no_ticket") == 0) {
539: off |= SSL_OP_NO_TICKET;
540: }
541: #ifndef OPENSSL_NO_NEXTPROTONEG
542: else if (strcmp(*argv, "-nextprotoneg") == 0) {
543: if (--argc < 1)
544: goto bad;
545: next_proto_neg_in = *(++argv);
546: }
547: #endif
548: else if (strcmp(*argv, "-serverpref") == 0)
549: off |= SSL_OP_CIPHER_SERVER_PREFERENCE;
550: else if (strcmp(*argv, "-legacy_renegotiation") == 0)
551: ; /* no-op */
552: else if (strcmp(*argv, "-legacy_server_connect") == 0) {
553: off |= SSL_OP_LEGACY_SERVER_CONNECT;
554: } else if (strcmp(*argv, "-no_legacy_server_connect") == 0) {
555: clr |= SSL_OP_LEGACY_SERVER_CONNECT;
556: } else if (strcmp(*argv, "-cipher") == 0) {
557: if (--argc < 1)
558: goto bad;
559: cipher = *(++argv);
560: }
561: else if (strcmp(*argv, "-nbio") == 0) {
562: c_nbio = 1;
563: }
564: else if (strcmp(*argv, "-starttls") == 0) {
565: if (--argc < 1)
566: goto bad;
567: ++argv;
568: if (strcmp(*argv, "smtp") == 0)
569: starttls_proto = PROTO_SMTP;
570: else if (strcmp(*argv, "lmtp") == 0)
571: starttls_proto = PROTO_LMTP;
572: else if (strcmp(*argv, "pop3") == 0)
573: starttls_proto = PROTO_POP3;
574: else if (strcmp(*argv, "imap") == 0)
575: starttls_proto = PROTO_IMAP;
576: else if (strcmp(*argv, "ftp") == 0)
577: starttls_proto = PROTO_FTP;
578: else if (strcmp(*argv, "xmpp") == 0)
579: starttls_proto = PROTO_XMPP;
580: else
581: goto bad;
582: }
583: #ifndef OPENSSL_NO_ENGINE
584: else if (strcmp(*argv, "-engine") == 0) {
585: if (--argc < 1)
586: goto bad;
587: engine_id = *(++argv);
588: } else if (strcmp(*argv, "-ssl_client_engine") == 0) {
589: if (--argc < 1)
590: goto bad;
591: ssl_client_engine_id = *(++argv);
592: }
593: #endif
594: else if (strcmp(*argv, "-4") == 0) {
595: af = AF_INET;
596: } else if (strcmp(*argv, "-6") == 0) {
597: af = AF_INET6;
598: }
599: else if (strcmp(*argv, "-servername") == 0) {
600: if (--argc < 1)
601: goto bad;
602: servername = *(++argv);
603: /* meth=TLSv1_client_method(); */
604: }
605: #ifndef OPENSSL_NO_SRTP
606: else if (strcmp(*argv, "-use_srtp") == 0) {
607: if (--argc < 1)
608: goto bad;
609: srtp_profiles = *(++argv);
610: }
611: #endif
612: else if (strcmp(*argv, "-keymatexport") == 0) {
613: if (--argc < 1)
614: goto bad;
615: keymatexportlabel = *(++argv);
616: } else if (strcmp(*argv, "-keymatexportlen") == 0) {
617: if (--argc < 1)
618: goto bad;
619: keymatexportlen = strtonum(*(++argv), 1, INT_MAX, &errstr);
620: if (errstr)
621: goto bad;
622: } else {
623: BIO_printf(bio_err, "unknown option %s\n", *argv);
624: badop = 1;
625: break;
626: }
627: argc--;
628: argv++;
629: }
630: if (badop) {
631: bad:
632: if (errstr)
633: BIO_printf(bio_err, "invalid argument %s: %s\n",
634: *argv, errstr);
635: else
636: sc_usage();
637: goto end;
638: }
639:
1.7 jsing 640: #if !defined(OPENSSL_NO_NEXTPROTONEG)
1.1 jsing 641: next_proto.status = -1;
642: if (next_proto_neg_in) {
643: next_proto.data = next_protos_parse(&next_proto.len, next_proto_neg_in);
644: if (next_proto.data == NULL) {
645: BIO_printf(bio_err, "Error parsing -nextprotoneg argument\n");
646: goto end;
647: }
648: } else
649: next_proto.data = NULL;
650: #endif
651:
652: #ifndef OPENSSL_NO_ENGINE
653: e = setup_engine(bio_err, engine_id, 1);
654: if (ssl_client_engine_id) {
655: ssl_client_engine = ENGINE_by_id(ssl_client_engine_id);
656: if (!ssl_client_engine) {
657: BIO_printf(bio_err,
658: "Error getting client auth engine\n");
659: goto end;
660: }
661: }
662: #endif
663: if (!app_passwd(bio_err, passarg, NULL, &pass, NULL)) {
664: BIO_printf(bio_err, "Error getting password\n");
665: goto end;
666: }
667: if (key_file == NULL)
668: key_file = cert_file;
669:
670:
671: if (key_file) {
672:
673: key = load_key(bio_err, key_file, key_format, 0, pass, e,
674: "client certificate private key file");
675: if (!key) {
676: ERR_print_errors(bio_err);
677: goto end;
678: }
679: }
680: if (cert_file) {
681: cert = load_cert(bio_err, cert_file, cert_format,
682: NULL, e, "client certificate file");
683:
684: if (!cert) {
685: ERR_print_errors(bio_err);
686: goto end;
687: }
688: }
689: if (bio_c_out == NULL) {
690: if (c_quiet && !c_debug && !c_msg) {
691: bio_c_out = BIO_new(BIO_s_null());
692: } else {
693: if (bio_c_out == NULL)
694: bio_c_out = BIO_new_fp(stdout, BIO_NOCLOSE);
695: }
696: }
697:
698: ctx = SSL_CTX_new(meth);
699: if (ctx == NULL) {
700: ERR_print_errors(bio_err);
701: goto end;
702: }
703: if (vpm)
704: SSL_CTX_set1_param(ctx, vpm);
705:
706: #ifndef OPENSSL_NO_ENGINE
707: if (ssl_client_engine) {
708: if (!SSL_CTX_set_client_cert_engine(ctx, ssl_client_engine)) {
709: BIO_puts(bio_err, "Error setting client auth engine\n");
710: ERR_print_errors(bio_err);
711: ENGINE_free(ssl_client_engine);
712: goto end;
713: }
714: ENGINE_free(ssl_client_engine);
715: }
716: #endif
717:
718: #ifndef OPENSSL_NO_SRTP
719: if (srtp_profiles != NULL)
720: SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles);
721: #endif
722: if (bugs)
723: SSL_CTX_set_options(ctx, SSL_OP_ALL | off);
724: else
725: SSL_CTX_set_options(ctx, off);
726:
727: if (clr)
728: SSL_CTX_clear_options(ctx, clr);
729: /*
730: * DTLS: partial reads end up discarding unread UDP bytes :-( Setting
731: * read ahead solves this problem.
732: */
733: if (socket_type == SOCK_DGRAM)
734: SSL_CTX_set_read_ahead(ctx, 1);
735:
1.7 jsing 736: #if !defined(OPENSSL_NO_NEXTPROTONEG)
1.1 jsing 737: if (next_proto.data)
738: SSL_CTX_set_next_proto_select_cb(ctx, next_proto_cb, &next_proto);
739: #endif
740:
741: if (state)
742: SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback);
743: if (cipher != NULL)
744: if (!SSL_CTX_set_cipher_list(ctx, cipher)) {
745: BIO_printf(bio_err, "error setting cipher list\n");
746: ERR_print_errors(bio_err);
747: goto end;
748: }
749:
750: SSL_CTX_set_verify(ctx, verify, verify_callback);
751: if (!set_cert_key_stuff(ctx, cert, key))
752: goto end;
753:
754: if ((!SSL_CTX_load_verify_locations(ctx, CAfile, CApath)) ||
755: (!SSL_CTX_set_default_verify_paths(ctx))) {
756: /*
757: * BIO_printf(bio_err,"error setting default verify
758: * locations\n");
759: */
760: ERR_print_errors(bio_err);
761: /* goto end; */
762: }
763: if (servername != NULL) {
764: tlsextcbp.biodebug = bio_err;
765: SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
766: SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
767: }
768:
769: con = SSL_new(ctx);
770: if (sess_in) {
771: SSL_SESSION *sess;
772: BIO *stmp = BIO_new_file(sess_in, "r");
773: if (!stmp) {
774: BIO_printf(bio_err, "Can't open session file %s\n",
775: sess_in);
776: ERR_print_errors(bio_err);
777: goto end;
778: }
779: sess = PEM_read_bio_SSL_SESSION(stmp, NULL, 0, NULL);
780: BIO_free(stmp);
781: if (!sess) {
782: BIO_printf(bio_err, "Can't open session file %s\n",
783: sess_in);
784: ERR_print_errors(bio_err);
785: goto end;
786: }
787: SSL_set_session(con, sess);
788: SSL_SESSION_free(sess);
789: }
790: if (servername != NULL) {
791: if (!SSL_set_tlsext_host_name(con, servername)) {
792: BIO_printf(bio_err, "Unable to set TLS servername extension.\n");
793: ERR_print_errors(bio_err);
794: goto end;
795: }
796: }
797: /* SSL_set_cipher_list(con,"RC4-MD5"); */
798:
799: re_start:
800:
801: if (init_client(&s, host, port, socket_type, af) == 0) {
802: BIO_printf(bio_err, "connect:errno=%d\n", errno);
803: goto end;
804: }
805: BIO_printf(bio_c_out, "CONNECTED(%08X)\n", s);
806:
807: if (c_nbio) {
1.3 bcook 808: if (!c_quiet)
809: BIO_printf(bio_c_out, "turning on non blocking io\n");
810: if (!BIO_socket_nbio(s, 1)) {
1.1 jsing 811: ERR_print_errors(bio_err);
812: goto end;
813: }
814: }
815: if (c_Pause & 0x01)
816: SSL_set_debug(con, 1);
817:
818: if (SSL_version(con) == DTLS1_VERSION) {
819:
820: sbio = BIO_new_dgram(s, BIO_NOCLOSE);
821: if (getsockname(s, &peer, (void *) &peerlen) < 0) {
822: BIO_printf(bio_err, "getsockname:errno=%d\n",
823: errno);
824: shutdown(s, SHUT_RD);
825: close(s);
826: goto end;
827: }
828: (void) BIO_ctrl_set_connected(sbio, 1, &peer);
829:
830: if (enable_timeouts) {
831: timeout.tv_sec = 0;
832: timeout.tv_usec = DGRAM_RCV_TIMEOUT;
833: BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
834:
835: timeout.tv_sec = 0;
836: timeout.tv_usec = DGRAM_SND_TIMEOUT;
837: BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
838: }
839: if (socket_mtu > 28) {
840: SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
841: SSL_set_mtu(con, socket_mtu - 28);
842: } else
843: /* want to do MTU discovery */
844: BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
845: } else
846: sbio = BIO_new_socket(s, BIO_NOCLOSE);
847:
848: if (nbio_test) {
849: BIO *test;
850:
851: test = BIO_new(BIO_f_nbio_test());
852: sbio = BIO_push(test, sbio);
853: }
854: if (c_debug) {
855: SSL_set_debug(con, 1);
856: BIO_set_callback(sbio, bio_dump_callback);
857: BIO_set_callback_arg(sbio, (char *) bio_c_out);
858: }
859: if (c_msg) {
860: SSL_set_msg_callback(con, msg_cb);
861: SSL_set_msg_callback_arg(con, bio_c_out);
862: }
863: if (c_tlsextdebug) {
864: SSL_set_tlsext_debug_callback(con, tlsext_cb);
865: SSL_set_tlsext_debug_arg(con, bio_c_out);
866: }
867: if (c_status_req) {
868: SSL_set_tlsext_status_type(con, TLSEXT_STATUSTYPE_ocsp);
869: SSL_CTX_set_tlsext_status_cb(ctx, ocsp_resp_cb);
870: SSL_CTX_set_tlsext_status_arg(ctx, bio_c_out);
871: }
872:
873: SSL_set_bio(con, sbio, sbio);
874: SSL_set_connect_state(con);
875:
876: /* ok, lets connect */
877: width = SSL_get_fd(con) + 1;
878:
879: read_tty = 1;
880: write_tty = 0;
881: tty_on = 0;
882: read_ssl = 1;
883: write_ssl = 1;
884:
885: cbuf_len = 0;
886: cbuf_off = 0;
887: sbuf_len = 0;
888: sbuf_off = 0;
889:
890: /* This is an ugly hack that does a lot of assumptions */
891: /*
892: * We do have to handle multi-line responses which may come in a
893: * single packet or not. We therefore have to use BIO_gets() which
894: * does need a buffering BIO. So during the initial chitchat we do
895: * push a buffering BIO into the chain that is removed again later on
896: * to not disturb the rest of the s_client operation.
897: */
898: if (starttls_proto == PROTO_SMTP || starttls_proto == PROTO_LMTP) {
899: int foundit = 0;
900: BIO *fbio = BIO_new(BIO_f_buffer());
901: BIO_push(fbio, sbio);
902: /* wait for multi-line response to end from SMTP */
903: do {
904: mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
905: }
906: while (mbuf_len > 3 && mbuf[3] == '-');
907: /* STARTTLS command requires EHLO... */
908: BIO_printf(fbio, "%cHLO openssl.client.net\r\n",
909: starttls_proto == PROTO_SMTP ? 'E' : 'L');
910: (void) BIO_flush(fbio);
911: /* wait for multi-line response to end EHLO SMTP response */
912: do {
913: mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
914: if (strstr(mbuf, "STARTTLS"))
915: foundit = 1;
916: }
917: while (mbuf_len > 3 && mbuf[3] == '-');
918: (void) BIO_flush(fbio);
919: BIO_pop(fbio);
920: BIO_free(fbio);
921: if (!foundit)
922: BIO_printf(bio_err,
923: "didn't found starttls in server response,"
924: " try anyway...\n");
925: BIO_printf(sbio, "STARTTLS\r\n");
926: BIO_read(sbio, sbuf, BUFSIZZ);
927: } else if (starttls_proto == PROTO_POP3) {
928: mbuf_len = BIO_read(sbio, mbuf, BUFSIZZ);
929: if (mbuf_len == -1) {
930: BIO_printf(bio_err, "BIO_read failed\n");
931: goto end;
932: }
933: BIO_printf(sbio, "STLS\r\n");
934: BIO_read(sbio, sbuf, BUFSIZZ);
935: } else if (starttls_proto == PROTO_IMAP) {
936: int foundit = 0;
937: BIO *fbio = BIO_new(BIO_f_buffer());
938: BIO_push(fbio, sbio);
939: BIO_gets(fbio, mbuf, BUFSIZZ);
940: /* STARTTLS command requires CAPABILITY... */
941: BIO_printf(fbio, ". CAPABILITY\r\n");
942: (void) BIO_flush(fbio);
943: /* wait for multi-line CAPABILITY response */
944: do {
945: mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
946: if (strstr(mbuf, "STARTTLS"))
947: foundit = 1;
948: }
949: while (mbuf_len > 3 && mbuf[0] != '.');
950: (void) BIO_flush(fbio);
951: BIO_pop(fbio);
952: BIO_free(fbio);
953: if (!foundit)
954: BIO_printf(bio_err,
955: "didn't found STARTTLS in server response,"
956: " try anyway...\n");
957: BIO_printf(sbio, ". STARTTLS\r\n");
958: BIO_read(sbio, sbuf, BUFSIZZ);
959: } else if (starttls_proto == PROTO_FTP) {
960: BIO *fbio = BIO_new(BIO_f_buffer());
961: BIO_push(fbio, sbio);
962: /* wait for multi-line response to end from FTP */
963: do {
964: mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
965: }
966: while (mbuf_len > 3 && mbuf[3] == '-');
967: (void) BIO_flush(fbio);
968: BIO_pop(fbio);
969: BIO_free(fbio);
970: BIO_printf(sbio, "AUTH TLS\r\n");
971: BIO_read(sbio, sbuf, BUFSIZZ);
972: }
973: if (starttls_proto == PROTO_XMPP) {
974: int seen = 0;
975: BIO_printf(sbio, "<stream:stream "
976: "xmlns:stream='http://etherx.jabber.org/streams' "
977: "xmlns='jabber:client' to='%s' version='1.0'>", host);
978: seen = BIO_read(sbio, mbuf, BUFSIZZ);
979: mbuf[seen] = 0;
980: while (!strstr(mbuf, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'")) {
981: if (strstr(mbuf, "/stream:features>"))
982: goto shut;
983: seen = BIO_read(sbio, mbuf, BUFSIZZ);
984: mbuf[seen] = 0;
985: }
986: BIO_printf(sbio, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
987: seen = BIO_read(sbio, sbuf, BUFSIZZ);
988: sbuf[seen] = 0;
989: if (!strstr(sbuf, "<proceed"))
990: goto shut;
991: mbuf[0] = 0;
992: }
993: for (;;) {
994: FD_ZERO(&readfds);
995: FD_ZERO(&writefds);
996:
997: if ((SSL_version(con) == DTLS1_VERSION) &&
998: DTLSv1_get_timeout(con, &timeout))
999: timeoutp = &timeout;
1000: else
1001: timeoutp = NULL;
1002:
1003: if (SSL_in_init(con) && !SSL_total_renegotiations(con)) {
1004: in_init = 1;
1005: tty_on = 0;
1006: } else {
1007: tty_on = 1;
1008: if (in_init) {
1009: in_init = 0;
1010: if (sess_out) {
1011: BIO *stmp = BIO_new_file(sess_out, "w");
1012: if (stmp) {
1013: PEM_write_bio_SSL_SESSION(stmp, SSL_get_session(con));
1014: BIO_free(stmp);
1015: } else
1016: BIO_printf(bio_err, "Error writing session file %s\n", sess_out);
1017: }
1018: print_stuff(bio_c_out, con, full_log);
1019: if (full_log > 0)
1020: full_log--;
1021:
1022: if (starttls_proto) {
1023: BIO_write(bio_err, mbuf, mbuf_len);
1024: /* We don't need to know any more */
1025: starttls_proto = PROTO_OFF;
1026: }
1027: if (reconnect) {
1028: reconnect--;
1029: BIO_printf(bio_c_out, "drop connection and then reconnect\n");
1030: SSL_shutdown(con);
1031: SSL_set_connect_state(con);
1032: shutdown(SSL_get_fd(con), SHUT_RD);
1033: close(SSL_get_fd(con));
1034: goto re_start;
1035: }
1036: }
1037: }
1038:
1039: ssl_pending = read_ssl && SSL_pending(con);
1040:
1041: /* XXX should add tests for fd_set overflow */
1042:
1043: if (!ssl_pending) {
1044: if (tty_on) {
1045: if (read_tty)
1046: FD_SET(fileno(stdin), &readfds);
1047: if (write_tty)
1048: FD_SET(fileno(stdout), &writefds);
1049: }
1050: if (read_ssl)
1051: FD_SET(SSL_get_fd(con), &readfds);
1052: if (write_ssl)
1053: FD_SET(SSL_get_fd(con), &writefds);
1054: /* printf("mode tty(%d %d%d) ssl(%d%d)\n",
1055: tty_on,read_tty,write_tty,read_ssl,write_ssl);*/
1056:
1057: i = select(width, &readfds, &writefds,
1058: NULL, timeoutp);
1059: if (i < 0) {
1060: BIO_printf(bio_err, "bad select %d\n",
1061: errno);
1062: goto shut;
1063: /* goto end; */
1064: }
1065: }
1066: if ((SSL_version(con) == DTLS1_VERSION) && DTLSv1_handle_timeout(con) > 0) {
1067: BIO_printf(bio_err, "TIMEOUT occured\n");
1068: }
1069: if (!ssl_pending && FD_ISSET(SSL_get_fd(con), &writefds)) {
1070: k = SSL_write(con, &(cbuf[cbuf_off]),
1071: (unsigned int) cbuf_len);
1072: switch (SSL_get_error(con, k)) {
1073: case SSL_ERROR_NONE:
1074: cbuf_off += k;
1075: cbuf_len -= k;
1076: if (k <= 0)
1077: goto end;
1078: /* we have done a write(con,NULL,0); */
1079: if (cbuf_len <= 0) {
1080: read_tty = 1;
1081: write_ssl = 0;
1082: } else { /* if (cbuf_len > 0) */
1083: read_tty = 0;
1084: write_ssl = 1;
1085: }
1086: break;
1087: case SSL_ERROR_WANT_WRITE:
1088: BIO_printf(bio_c_out, "write W BLOCK\n");
1089: write_ssl = 1;
1090: read_tty = 0;
1091: break;
1092: case SSL_ERROR_WANT_READ:
1093: BIO_printf(bio_c_out, "write R BLOCK\n");
1094: write_tty = 0;
1095: read_ssl = 1;
1096: write_ssl = 0;
1097: break;
1098: case SSL_ERROR_WANT_X509_LOOKUP:
1099: BIO_printf(bio_c_out, "write X BLOCK\n");
1100: break;
1101: case SSL_ERROR_ZERO_RETURN:
1102: if (cbuf_len != 0) {
1103: BIO_printf(bio_c_out, "shutdown\n");
1104: ret = 0;
1105: goto shut;
1106: } else {
1107: read_tty = 1;
1108: write_ssl = 0;
1109: break;
1110: }
1111:
1112: case SSL_ERROR_SYSCALL:
1113: if ((k != 0) || (cbuf_len != 0)) {
1114: BIO_printf(bio_err, "write:errno=%d\n",
1115: errno);
1116: goto shut;
1117: } else {
1118: read_tty = 1;
1119: write_ssl = 0;
1120: }
1121: break;
1122: case SSL_ERROR_SSL:
1123: ERR_print_errors(bio_err);
1124: goto shut;
1125: }
1126: } else if (!ssl_pending && FD_ISSET(fileno(stdout), &writefds)) {
1127: i = write(fileno(stdout), &(sbuf[sbuf_off]), sbuf_len);
1128:
1129: if (i <= 0) {
1130: BIO_printf(bio_c_out, "DONE\n");
1131: ret = 0;
1132: goto shut;
1133: /* goto end; */
1134: }
1135: sbuf_len -= i;
1136: sbuf_off += i;
1137: if (sbuf_len <= 0) {
1138: read_ssl = 1;
1139: write_tty = 0;
1140: }
1141: } else if (ssl_pending || FD_ISSET(SSL_get_fd(con), &readfds)) {
1142: #ifdef RENEG
1143: {
1144: static int iiii;
1145: if (++iiii == 52) {
1146: SSL_renegotiate(con);
1147: iiii = 0;
1148: }
1149: }
1150: #endif
1151: k = SSL_read(con, sbuf, 1024 /* BUFSIZZ */ );
1152:
1153: switch (SSL_get_error(con, k)) {
1154: case SSL_ERROR_NONE:
1155: if (k <= 0)
1156: goto end;
1157: sbuf_off = 0;
1158: sbuf_len = k;
1159:
1160: read_ssl = 0;
1161: write_tty = 1;
1162: break;
1163: case SSL_ERROR_WANT_WRITE:
1164: BIO_printf(bio_c_out, "read W BLOCK\n");
1165: write_ssl = 1;
1166: read_tty = 0;
1167: break;
1168: case SSL_ERROR_WANT_READ:
1169: BIO_printf(bio_c_out, "read R BLOCK\n");
1170: write_tty = 0;
1171: read_ssl = 1;
1172: if ((read_tty == 0) && (write_ssl == 0))
1173: write_ssl = 1;
1174: break;
1175: case SSL_ERROR_WANT_X509_LOOKUP:
1176: BIO_printf(bio_c_out, "read X BLOCK\n");
1177: break;
1178: case SSL_ERROR_SYSCALL:
1179: ret = errno;
1180: BIO_printf(bio_err, "read:errno=%d\n", ret);
1181: goto shut;
1182: case SSL_ERROR_ZERO_RETURN:
1183: BIO_printf(bio_c_out, "closed\n");
1184: ret = 0;
1185: goto shut;
1186: case SSL_ERROR_SSL:
1187: ERR_print_errors(bio_err);
1188: goto shut;
1189: /* break; */
1190: }
1191: } else if (FD_ISSET(fileno(stdin), &readfds)) {
1192: if (crlf) {
1193: int j, lf_num;
1194:
1195: i = read(fileno(stdin), cbuf, BUFSIZZ / 2);
1196: lf_num = 0;
1197: /* both loops are skipped when i <= 0 */
1198: for (j = 0; j < i; j++)
1199: if (cbuf[j] == '\n')
1200: lf_num++;
1201: for (j = i - 1; j >= 0; j--) {
1202: cbuf[j + lf_num] = cbuf[j];
1203: if (cbuf[j] == '\n') {
1204: lf_num--;
1205: i++;
1206: cbuf[j + lf_num] = '\r';
1207: }
1208: }
1209: assert(lf_num == 0);
1210: } else
1211: i = read(fileno(stdin), cbuf, BUFSIZZ);
1212:
1213: if ((!c_ign_eof) && ((i <= 0) || (cbuf[0] == 'Q'))) {
1214: BIO_printf(bio_err, "DONE\n");
1215: ret = 0;
1216: goto shut;
1217: }
1218: if ((!c_ign_eof) && (cbuf[0] == 'R')) {
1219: BIO_printf(bio_err, "RENEGOTIATING\n");
1220: SSL_renegotiate(con);
1221: cbuf_len = 0;
1222: } else {
1223: cbuf_len = i;
1224: cbuf_off = 0;
1225: }
1226:
1227: write_ssl = 1;
1228: read_tty = 0;
1229: }
1230: }
1231:
1232: ret = 0;
1233: shut:
1234: if (in_init)
1235: print_stuff(bio_c_out, con, full_log);
1236: SSL_shutdown(con);
1237: shutdown(SSL_get_fd(con), SHUT_RD);
1238: close(SSL_get_fd(con));
1239: end:
1240: if (con != NULL) {
1241: if (prexit != 0)
1242: print_stuff(bio_c_out, con, 1);
1243: SSL_free(con);
1244: }
1.7 jsing 1245: #if !defined(OPENSSL_NO_NEXTPROTONEG)
1.1 jsing 1246: free(next_proto.data);
1247: #endif
1248: if (ctx != NULL)
1249: SSL_CTX_free(ctx);
1250: if (cert)
1251: X509_free(cert);
1252: if (key)
1253: EVP_PKEY_free(key);
1254: free(pass);
1255: if (vpm)
1256: X509_VERIFY_PARAM_free(vpm);
1257: if (cbuf != NULL) {
1258: OPENSSL_cleanse(cbuf, BUFSIZZ);
1259: free(cbuf);
1260: }
1261: if (sbuf != NULL) {
1262: OPENSSL_cleanse(sbuf, BUFSIZZ);
1263: free(sbuf);
1264: }
1265: if (mbuf != NULL) {
1266: OPENSSL_cleanse(mbuf, BUFSIZZ);
1267: free(mbuf);
1268: }
1269: if (bio_c_out != NULL) {
1270: BIO_free(bio_c_out);
1271: bio_c_out = NULL;
1272: }
1273:
1274: return (ret);
1275: }
1276:
1277:
1278: static void
1279: print_stuff(BIO * bio, SSL * s, int full)
1280: {
1281: X509 *peer = NULL;
1282: char *p;
1283: static const char *space = " ";
1284: char buf[BUFSIZ];
1285: STACK_OF(X509) * sk;
1286: STACK_OF(X509_NAME) * sk2;
1287: const SSL_CIPHER *c;
1288: X509_NAME *xn;
1289: int j, i;
1290: unsigned char *exportedkeymat;
1291:
1292: if (full) {
1293: int got_a_chain = 0;
1294:
1295: sk = SSL_get_peer_cert_chain(s);
1296: if (sk != NULL) {
1297: got_a_chain = 1; /* we don't have it for SSL2
1298: * (yet) */
1299:
1300: BIO_printf(bio, "---\nCertificate chain\n");
1301: for (i = 0; i < sk_X509_num(sk); i++) {
1302: X509_NAME_oneline(X509_get_subject_name(
1303: sk_X509_value(sk, i)), buf, sizeof buf);
1304: BIO_printf(bio, "%2d s:%s\n", i, buf);
1305: X509_NAME_oneline(X509_get_issuer_name(
1306: sk_X509_value(sk, i)), buf, sizeof buf);
1307: BIO_printf(bio, " i:%s\n", buf);
1308: if (c_showcerts)
1309: PEM_write_bio_X509(bio, sk_X509_value(sk, i));
1310: }
1311: }
1312: BIO_printf(bio, "---\n");
1313: peer = SSL_get_peer_certificate(s);
1314: if (peer != NULL) {
1315: BIO_printf(bio, "Server certificate\n");
1316: if (!(c_showcerts && got_a_chain)) /* Redundant if we
1317: * showed the whole
1318: * chain */
1319: PEM_write_bio_X509(bio, peer);
1320: X509_NAME_oneline(X509_get_subject_name(peer),
1321: buf, sizeof buf);
1322: BIO_printf(bio, "subject=%s\n", buf);
1323: X509_NAME_oneline(X509_get_issuer_name(peer),
1324: buf, sizeof buf);
1325: BIO_printf(bio, "issuer=%s\n", buf);
1326: } else
1327: BIO_printf(bio, "no peer certificate available\n");
1328:
1329: sk2 = SSL_get_client_CA_list(s);
1330: if ((sk2 != NULL) && (sk_X509_NAME_num(sk2) > 0)) {
1331: BIO_printf(bio, "---\nAcceptable client certificate CA names\n");
1332: for (i = 0; i < sk_X509_NAME_num(sk2); i++) {
1333: xn = sk_X509_NAME_value(sk2, i);
1334: X509_NAME_oneline(xn, buf, sizeof(buf));
1335: BIO_write(bio, buf, strlen(buf));
1336: BIO_write(bio, "\n", 1);
1337: }
1338: } else {
1339: BIO_printf(bio, "---\nNo client certificate CA names sent\n");
1340: }
1341: p = SSL_get_shared_ciphers(s, buf, sizeof buf);
1342: if (p != NULL) {
1343: /*
1344: * This works only for SSL 2. In later protocol
1345: * versions, the client does not know what other
1346: * ciphers (in addition to the one to be used in the
1347: * current connection) the server supports.
1348: */
1349:
1350: BIO_printf(bio, "---\nCiphers common between both SSL endpoints:\n");
1351: j = i = 0;
1352: while (*p) {
1353: if (*p == ':') {
1354: BIO_write(bio, space, 15 - j % 25);
1355: i++;
1356: j = 0;
1357: BIO_write(bio, ((i % 3) ? " " : "\n"), 1);
1358: } else {
1359: BIO_write(bio, p, 1);
1360: j++;
1361: }
1362: p++;
1363: }
1364: BIO_write(bio, "\n", 1);
1365: }
1366: BIO_printf(bio, "---\nSSL handshake has read %ld bytes and written %ld bytes\n",
1367: BIO_number_read(SSL_get_rbio(s)),
1368: BIO_number_written(SSL_get_wbio(s)));
1369: }
1370: BIO_printf(bio, (SSL_cache_hit(s) ? "---\nReused, " : "---\nNew, "));
1371: c = SSL_get_current_cipher(s);
1372: BIO_printf(bio, "%s, Cipher is %s\n",
1373: SSL_CIPHER_get_version(c),
1374: SSL_CIPHER_get_name(c));
1375: if (peer != NULL) {
1376: EVP_PKEY *pktmp;
1377: pktmp = X509_get_pubkey(peer);
1378: BIO_printf(bio, "Server public key is %d bit\n",
1379: EVP_PKEY_bits(pktmp));
1380: EVP_PKEY_free(pktmp);
1381: }
1382: BIO_printf(bio, "Secure Renegotiation IS%s supported\n",
1383: SSL_get_secure_renegotiation_support(s) ? "" : " NOT");
1384:
1385: /* Compression is not supported and will always be none. */
1386: BIO_printf(bio, "Compression: NONE\n");
1387: BIO_printf(bio, "Expansion: NONE\n");
1388:
1389: #ifdef SSL_DEBUG
1390: {
1391: /* Print out local port of connection: useful for debugging */
1392: int sock;
1393: struct sockaddr_in ladd;
1394: socklen_t ladd_size = sizeof(ladd);
1395: sock = SSL_get_fd(s);
1396: getsockname(sock, (struct sockaddr *) & ladd, &ladd_size);
1397: BIO_printf(bio_c_out, "LOCAL PORT is %u\n", ntohs(ladd.sin_port));
1398: }
1399: #endif
1400:
1.7 jsing 1401: #if !defined(OPENSSL_NO_NEXTPROTONEG)
1.1 jsing 1402: if (next_proto.status != -1) {
1403: const unsigned char *proto;
1404: unsigned int proto_len;
1405: SSL_get0_next_proto_negotiated(s, &proto, &proto_len);
1406: BIO_printf(bio, "Next protocol: (%d) ", next_proto.status);
1407: BIO_write(bio, proto, proto_len);
1408: BIO_write(bio, "\n", 1);
1409: }
1410: #endif
1411:
1412: #ifndef OPENSSL_NO_SRTP
1413: {
1414: SRTP_PROTECTION_PROFILE *srtp_profile = SSL_get_selected_srtp_profile(s);
1415:
1416: if (srtp_profile)
1417: BIO_printf(bio, "SRTP Extension negotiated, profile=%s\n",
1418: srtp_profile->name);
1419: }
1420: #endif
1421:
1422: SSL_SESSION_print(bio, SSL_get_session(s));
1423: if (keymatexportlabel != NULL) {
1424: BIO_printf(bio, "Keying material exporter:\n");
1425: BIO_printf(bio, " Label: '%s'\n", keymatexportlabel);
1426: BIO_printf(bio, " Length: %i bytes\n", keymatexportlen);
1427: exportedkeymat = malloc(keymatexportlen);
1428: if (exportedkeymat != NULL) {
1429: if (!SSL_export_keying_material(s, exportedkeymat,
1430: keymatexportlen,
1431: keymatexportlabel,
1432: strlen(keymatexportlabel),
1433: NULL, 0, 0)) {
1434: BIO_printf(bio, " Error\n");
1435: } else {
1436: BIO_printf(bio, " Keying material: ");
1437: for (i = 0; i < keymatexportlen; i++)
1438: BIO_printf(bio, "%02X",
1439: exportedkeymat[i]);
1440: BIO_printf(bio, "\n");
1441: }
1442: free(exportedkeymat);
1443: }
1444: }
1445: BIO_printf(bio, "---\n");
1446: if (peer != NULL)
1447: X509_free(peer);
1448: /* flush, or debugging output gets mixed with http response */
1449: (void) BIO_flush(bio);
1450: }
1451:
1452:
1453: static int
1454: ocsp_resp_cb(SSL * s, void *arg)
1455: {
1456: const unsigned char *p;
1457: int len;
1458: OCSP_RESPONSE *rsp;
1459: len = SSL_get_tlsext_status_ocsp_resp(s, &p);
1460: BIO_puts(arg, "OCSP response: ");
1461: if (!p) {
1462: BIO_puts(arg, "no response sent\n");
1463: return 1;
1464: }
1465: rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
1466: if (!rsp) {
1467: BIO_puts(arg, "response parse error\n");
1468: BIO_dump_indent(arg, (char *) p, len, 4);
1469: return 0;
1470: }
1471: BIO_puts(arg, "\n======================================\n");
1472: OCSP_RESPONSE_print(arg, rsp, 0);
1473: BIO_puts(arg, "======================================\n");
1474: OCSP_RESPONSE_free(rsp);
1475: return 1;
1476: }
1477: