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