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