blob: eee0e2e77922053f4073749590fcc0d4b0c9a306 [file] [log] [blame]
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001/* apps/s_client.c */
Ralf S. Engelschall58964a41998-12-21 10:56:39 +00002/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003 * 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 */
Bodo Möllera661b652001-10-20 17:56:36 +000058/* ====================================================================
Bodo Möllerb1277b92006-01-02 23:29:12 +000059 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
Bodo Möllera661b652001-10-20 17:56:36 +000060 *
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 */
Nils Larschddac1972006-03-10 23:06:27 +0000111/* ====================================================================
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 */
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000137
Bodo Möller1b1a6e71999-08-09 13:01:48 +0000138#include <assert.h>
Nils Larschddac1972006-03-10 23:06:27 +0000139#include <ctype.h>
Ulf Möller8c197cc1999-07-28 23:25:59 +0000140#include <stdio.h>
141#include <stdlib.h>
142#include <string.h>
Richard Levittebe1bd922001-02-20 14:07:03 +0000143#include <openssl/e_os2.h>
Richard Levittecf1b7d92001-02-19 16:06:34 +0000144#ifdef OPENSSL_NO_STDIO
Ulf Möller8c197cc1999-07-28 23:25:59 +0000145#define APPS_WIN16
146#endif
147
Ulf Möller7d7d2cb1999-05-13 11:37:32 +0000148/* With IPv6, it looks like Digital has mixed up the proper order of
149 recursive header file inclusion, resulting in the compiler complaining
150 that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which
151 is needed to have fileno() declared correctly... So let's define u_int */
Richard Levittebc36ee62001-02-20 08:13:47 +0000152#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
Ulf Möller7d7d2cb1999-05-13 11:37:32 +0000153#define __U_INT
154typedef unsigned int u_int;
155#endif
156
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000157#define USE_SOCKETS
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000158#include "apps.h"
Bodo Möllerec577821999-04-23 22:13:45 +0000159#include <openssl/x509.h>
160#include <openssl/ssl.h>
161#include <openssl/err.h>
162#include <openssl/pem.h>
Geoff Thorpe13729652001-09-12 02:39:06 +0000163#include <openssl/rand.h>
Dr. Stephen Henson67c8e7f2007-09-26 21:56:59 +0000164#include <openssl/ocsp.h>
Geoff Thorpe1e26a8b2008-03-16 21:05:46 +0000165#include <openssl/bn.h>
Ben Laurieedc032b2011-03-12 17:01:19 +0000166#ifndef OPENSSL_NO_SRP
167#include <openssl/srp.h>
168#endif
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000169#include "s_apps.h"
Ben Laurie36d16f82005-04-26 16:02:40 +0000170#include "timeouts.h"
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000171
Richard Levittebc36ee62001-02-20 08:13:47 +0000172#if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
Ulf Möller75e07701999-05-13 13:21:17 +0000173/* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
Ulf Möller7d7d2cb1999-05-13 11:37:32 +0000174#undef FIONBIO
175#endif
176
Ulf Möller4700aea2006-04-11 21:34:21 +0000177#if defined(OPENSSL_SYS_BEOS_R5)
178#include <fcntl.h>
179#endif
180
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000181#undef PROG
182#define PROG s_client_main
183
184/*#define SSL_HOST_NAME "www.netscape.com" */
185/*#define SSL_HOST_NAME "193.118.187.102" */
186#define SSL_HOST_NAME "localhost"
187
188/*#define TEST_CERT "client.pem" */ /* no default cert. */
189
190#undef BUFSIZZ
191#define BUFSIZZ 1024*8
192
193extern int verify_depth;
194extern int verify_error;
Dr. Stephen Henson5d20c4f2006-09-17 17:16:28 +0000195extern int verify_return_error;
Dr. Stephen Henson2a7cbe72012-09-12 23:14:28 +0000196extern int verify_quiet;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000197
198#ifdef FIONBIO
199static int c_nbio=0;
200#endif
201static int c_Pause=0;
202static int c_debug=0;
Dr. Stephen Henson6434abb2007-08-11 23:18:29 +0000203#ifndef OPENSSL_NO_TLSEXT
204static int c_tlsextdebug=0;
Dr. Stephen Henson67c8e7f2007-09-26 21:56:59 +0000205static int c_status_req=0;
Dr. Stephen Henson6434abb2007-08-11 23:18:29 +0000206#endif
Bodo Möllera661b652001-10-20 17:56:36 +0000207static int c_msg=0;
Bodo Möller6d02d8e1999-03-31 12:06:30 +0000208static int c_showcerts=0;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000209
Ben Lauriee0af0402011-11-15 23:50:52 +0000210static char *keymatexportlabel=NULL;
211static int keymatexportlen=20;
212
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000213static void sc_usage(void);
214static void print_stuff(BIO *berr,SSL *con,int full);
Dr. Stephen Henson07021502008-09-03 12:29:57 +0000215#ifndef OPENSSL_NO_TLSEXT
Dr. Stephen Henson67c8e7f2007-09-26 21:56:59 +0000216static int ocsp_resp_cb(SSL *s, void *arg);
Scott Deboy36086182013-06-18 14:34:38 -0700217static int c_auth = 0;
218static int c_auth_require_reneg = 0;
Dr. Stephen Henson07021502008-09-03 12:29:57 +0000219#endif
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000220static BIO *bio_c_out=NULL;
Dr. Stephen Henson93ab9e42012-06-15 12:46:09 +0000221static BIO *bio_c_msg=NULL;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000222static int c_quiet=0;
Richard Levittece301b62000-03-10 12:18:28 +0000223static int c_ign_eof=0;
Dr. Stephen Henson2a7cbe72012-09-12 23:14:28 +0000224static int c_brief=0;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000225
Scott Deboy36086182013-06-18 14:34:38 -0700226#ifndef OPENSSL_NO_TLSEXT
227
Scott Deboy67c408c2013-08-01 11:54:09 -0700228static unsigned char *generated_supp_data = NULL;
229
Ben Laurie1769dfa2013-09-06 14:03:28 +0100230static const unsigned char *most_recent_supplemental_data = NULL;
Scott Deboy67c408c2013-08-01 11:54:09 -0700231static size_t most_recent_supplemental_data_length = 0;
Scott Deboy36086182013-06-18 14:34:38 -0700232
233static int server_provided_server_authz = 0;
234static int server_provided_client_authz = 0;
235
236static const unsigned char auth_ext_data[]={TLSEXT_AUTHZDATAFORMAT_dtcp};
237
238static int suppdata_cb(SSL *s, unsigned short supp_data_type,
Ben Laurie0a602872014-02-04 23:16:46 +0000239 const unsigned char *in,
240 unsigned short inlen, int *al,
241 void *arg);
Scott Deboy36086182013-06-18 14:34:38 -0700242
243static int auth_suppdata_generate_cb(SSL *s, unsigned short supp_data_type,
Ben Laurie0a602872014-02-04 23:16:46 +0000244 const unsigned char **out,
245 unsigned short *outlen, int *al, void *arg);
Scott Deboy36086182013-06-18 14:34:38 -0700246
247static int authz_tlsext_generate_cb(SSL *s, unsigned short ext_type,
Ben Laurie0a602872014-02-04 23:16:46 +0000248 const unsigned char **out, unsigned short *outlen,
249 int *al, void *arg);
Scott Deboy36086182013-06-18 14:34:38 -0700250
251static int authz_tlsext_cb(SSL *s, unsigned short ext_type,
Ben Laurie0a602872014-02-04 23:16:46 +0000252 const unsigned char *in,
253 unsigned short inlen, int *al,
254 void *arg);
Scott Deboy36086182013-06-18 14:34:38 -0700255#endif
256
Nils Larschddac1972006-03-10 23:06:27 +0000257#ifndef OPENSSL_NO_PSK
258/* Default PSK identity and key */
259static char *psk_identity="Client_identity";
Ben Laurief3b7bda2008-11-16 12:47:12 +0000260/*char *psk_key=NULL; by default PSK is not used */
Nils Larschddac1972006-03-10 23:06:27 +0000261
262static unsigned int psk_client_cb(SSL *ssl, const char *hint, char *identity,
263 unsigned int max_identity_len, unsigned char *psk,
264 unsigned int max_psk_len)
265 {
266 unsigned int psk_len = 0;
267 int ret;
268 BIGNUM *bn=NULL;
269
270 if (c_debug)
271 BIO_printf(bio_c_out, "psk_client_cb\n");
272 if (!hint)
273 {
274 /* no ServerKeyExchange message*/
275 if (c_debug)
276 BIO_printf(bio_c_out,"NULL received PSK identity hint, continuing anyway\n");
277 }
278 else if (c_debug)
279 BIO_printf(bio_c_out, "Received PSK identity hint '%s'\n", hint);
280
281 /* lookup PSK identity and PSK key based on the given identity hint here */
Dr. Stephen Henson0ed6b522009-02-15 15:29:59 +0000282 ret = BIO_snprintf(identity, max_identity_len, "%s", psk_identity);
Nils Larscha0aa8b42006-03-11 12:18:11 +0000283 if (ret < 0 || (unsigned int)ret > max_identity_len)
Nils Larschddac1972006-03-10 23:06:27 +0000284 goto out_err;
285 if (c_debug)
286 BIO_printf(bio_c_out, "created identity '%s' len=%d\n", identity, ret);
287 ret=BN_hex2bn(&bn, psk_key);
288 if (!ret)
289 {
290 BIO_printf(bio_err,"Could not convert PSK key '%s' to BIGNUM\n", psk_key);
291 if (bn)
292 BN_free(bn);
293 return 0;
294 }
295
Nils Larscha0aa8b42006-03-11 12:18:11 +0000296 if ((unsigned int)BN_num_bytes(bn) > max_psk_len)
Nils Larschddac1972006-03-10 23:06:27 +0000297 {
298 BIO_printf(bio_err,"psk buffer of callback is too small (%d) for key (%d)\n",
299 max_psk_len, BN_num_bytes(bn));
300 BN_free(bn);
301 return 0;
302 }
303
304 psk_len=BN_bn2bin(bn, psk);
305 BN_free(bn);
306 if (psk_len == 0)
307 goto out_err;
308
309 if (c_debug)
310 BIO_printf(bio_c_out, "created PSK len=%d\n", psk_len);
311
312 return psk_len;
313 out_err:
314 if (c_debug)
315 BIO_printf(bio_err, "Error in PSK client callback\n");
316 return 0;
317 }
318#endif
319
Ulf Möller6b691a51999-04-19 21:31:43 +0000320static void sc_usage(void)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000321 {
Ralf S. Engelschallb6cff931999-01-28 14:44:08 +0000322 BIO_printf(bio_err,"usage: s_client args\n");
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000323 BIO_printf(bio_err,"\n");
324 BIO_printf(bio_err," -host host - use -connect instead\n");
325 BIO_printf(bio_err," -port port - use -connect instead\n");
Geoff Thorpea9351322014-04-26 01:22:54 -0400326 BIO_printf(bio_err," -connect host:port - connect over TCP/IP (default is %s:%s)\n",SSL_HOST_NAME,PORT_STR);
327 BIO_printf(bio_err," -unix path - connect over unix domain sockets\n");
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000328 BIO_printf(bio_err," -verify arg - turn on peer certificate verification\n");
329 BIO_printf(bio_err," -cert arg - certificate file to use, PEM format assumed\n");
Dr. Stephen Henson826a42a2004-11-16 17:30:59 +0000330 BIO_printf(bio_err," -certform arg - certificate format (PEM or DER) PEM default\n");
331 BIO_printf(bio_err," -key arg - Private key file to use, in cert file if\n");
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000332 BIO_printf(bio_err," not specified but cert file is.\n");
Dr. Stephen Henson826a42a2004-11-16 17:30:59 +0000333 BIO_printf(bio_err," -keyform arg - key format (PEM or DER) PEM default\n");
334 BIO_printf(bio_err," -pass arg - private key file pass phrase source\n");
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000335 BIO_printf(bio_err," -CApath arg - PEM format directory of CA's\n");
336 BIO_printf(bio_err," -CAfile arg - PEM format file of CA's\n");
337 BIO_printf(bio_err," -reconnect - Drop and re-make the connection with the same Session-ID\n");
338 BIO_printf(bio_err," -pause - sleep(1) after each read(2) and write(2) system call\n");
Bodo Möller6d02d8e1999-03-31 12:06:30 +0000339 BIO_printf(bio_err," -showcerts - show all certificates in the chain\n");
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000340 BIO_printf(bio_err," -debug - extra output\n");
Andy Polyakov02a00bb2005-01-04 10:28:38 +0000341#ifdef WATT32
342 BIO_printf(bio_err," -wdebug - WATT-32 tcp debugging\n");
343#endif
Bodo Möllera661b652001-10-20 17:56:36 +0000344 BIO_printf(bio_err," -msg - Show protocol messages\n");
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000345 BIO_printf(bio_err," -nbio_test - more ssl protocol testing\n");
346 BIO_printf(bio_err," -state - print the 'ssl' states\n");
347#ifdef FIONBIO
348 BIO_printf(bio_err," -nbio - Run with non-blocking IO\n");
349#endif
Bodo Möller1bdb8631999-08-07 02:51:10 +0000350 BIO_printf(bio_err," -crlf - convert LF from terminal into CRLF\n");
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000351 BIO_printf(bio_err," -quiet - no s_client output\n");
Richard Levittece301b62000-03-10 12:18:28 +0000352 BIO_printf(bio_err," -ign_eof - ignore input eof (default when -quiet)\n");
Lutz Jänicke020d67f2008-10-22 06:46:14 +0000353 BIO_printf(bio_err," -no_ign_eof - don't ignore input eof\n");
Nils Larschddac1972006-03-10 23:06:27 +0000354#ifndef OPENSSL_NO_PSK
355 BIO_printf(bio_err," -psk_identity arg - PSK identity\n");
356 BIO_printf(bio_err," -psk arg - PSK in hex (without 0x)\n");
Dr. Stephen Henson79bd20f2008-11-24 17:27:08 +0000357# ifndef OPENSSL_NO_JPAKE
Ben Laurief3b7bda2008-11-16 12:47:12 +0000358 BIO_printf(bio_err," -jpake arg - JPAKE secret to use\n");
359# endif
Nils Larschddac1972006-03-10 23:06:27 +0000360#endif
Ben Laurieedc032b2011-03-12 17:01:19 +0000361#ifndef OPENSSL_NO_SRP
362 BIO_printf(bio_err," -srpuser user - SRP authentification for 'user'\n");
363 BIO_printf(bio_err," -srppass arg - password for 'user'\n");
364 BIO_printf(bio_err," -srp_lateuser - SRP username into second ClientHello message\n");
365 BIO_printf(bio_err," -srp_moregroups - Tolerate other than the known g N values.\n");
366 BIO_printf(bio_err," -srp_strength int - minimal mength in bits for N (default %d).\n",SRP_MINIMAL_N);
367#endif
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000368 BIO_printf(bio_err," -ssl2 - just use SSLv2\n");
369 BIO_printf(bio_err," -ssl3 - just use SSLv3\n");
Dr. Stephen Henson7409d7a2011-04-29 22:56:51 +0000370 BIO_printf(bio_err," -tls1_2 - just use TLSv1.2\n");
Dr. Stephen Henson637f3742009-12-07 13:31:02 +0000371 BIO_printf(bio_err," -tls1_1 - just use TLSv1.1\n");
Ralf S. Engelschall58964a41998-12-21 10:56:39 +0000372 BIO_printf(bio_err," -tls1 - just use TLSv1\n");
Ben Laurie36d16f82005-04-26 16:02:40 +0000373 BIO_printf(bio_err," -dtls1 - just use DTLSv1\n");
Dr. Stephen Henson046f2102009-05-17 16:04:58 +0000374 BIO_printf(bio_err," -mtu - set the link layer MTU\n");
Dr. Stephen Henson7409d7a2011-04-29 22:56:51 +0000375 BIO_printf(bio_err," -no_tls1_2/-no_tls1_1/-no_tls1/-no_ssl3/-no_ssl2 - turn off that protocol\n");
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000376 BIO_printf(bio_err," -bugs - Switch on all SSL implementation bug workarounds\n");
Lutz Jänicke836f9962001-02-09 19:56:31 +0000377 BIO_printf(bio_err," -serverpref - Use server's cipher preferences (only SSLv2)\n");
Ulf Möller657e60f2000-02-03 23:23:24 +0000378 BIO_printf(bio_err," -cipher - preferred cipher to use, use the 'openssl ciphers'\n");
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000379 BIO_printf(bio_err," command to see what is available\n");
Richard Levitte135c0af2001-11-14 13:57:52 +0000380 BIO_printf(bio_err," -starttls prot - use the STARTTLS command before starting TLS\n");
381 BIO_printf(bio_err," for those protocols that support it, where\n");
382 BIO_printf(bio_err," 'prot' defines which one to assume. Currently,\n");
Ben Lauried5bbead2008-10-14 19:11:26 +0000383 BIO_printf(bio_err," only \"smtp\", \"pop3\", \"imap\", \"ftp\" and \"xmpp\"\n");
384 BIO_printf(bio_err," are supported.\n");
Carlos Alberto Lopez Perezb98af492012-08-06 02:24:51 +0200385 BIO_printf(bio_err," -xmpphost host - When used with \"-starttls xmpp\" specifies the virtual host.\n");
Richard Levitte0b13e9f2003-01-30 17:39:26 +0000386#ifndef OPENSSL_NO_ENGINE
Richard Levitte5270e702000-10-26 21:07:28 +0000387 BIO_printf(bio_err," -engine id - Initialise and use the specified engine\n");
Richard Levitte0b13e9f2003-01-30 17:39:26 +0000388#endif
Lutz Jänicke52b621d2001-02-15 10:22:07 +0000389 BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
Dr. Stephen Henson014f62b2007-08-23 12:20:36 +0000390 BIO_printf(bio_err," -sess_out arg - file to write SSL session to\n");
391 BIO_printf(bio_err," -sess_in arg - file to read SSL session from\n");
Bodo Möllered3883d2006-01-02 23:14:37 +0000392#ifndef OPENSSL_NO_TLSEXT
393 BIO_printf(bio_err," -servername host - Set TLS extension servername in ClientHello\n");
Dr. Stephen Hensond24a9c82007-08-23 11:34:48 +0000394 BIO_printf(bio_err," -tlsextdebug - hex dump of all TLS extensions received\n");
Dr. Stephen Henson67c8e7f2007-09-26 21:56:59 +0000395 BIO_printf(bio_err," -status - request certificate status from server\n");
Dr. Stephen Hensond24a9c82007-08-23 11:34:48 +0000396 BIO_printf(bio_err," -no_ticket - disable use of RFC4507bis session tickets\n");
Scott Deboy36086182013-06-18 14:34:38 -0700397 BIO_printf(bio_err," -serverinfo types - send empty ClientHello extensions (comma-separated numbers)\n");
398 BIO_printf(bio_err," -auth - send and receive RFC 5878 TLS auth extensions and supplemental data\n");
399 BIO_printf(bio_err," -auth_require_reneg - Do not send TLS auth extensions until renegotiation\n");
Ben Lauriebf488362010-09-05 17:14:01 +0000400# ifndef OPENSSL_NO_NEXTPROTONEG
Ben Laurieee2ffc22010-07-28 10:06:55 +0000401 BIO_printf(bio_err," -nextprotoneg arg - enable NPN extension, considering named protocols supported (comma-separated list)\n");
Ben Laurieee2ffc22010-07-28 10:06:55 +0000402# endif
Piotr Sikora29115752013-11-13 15:20:22 -0800403 BIO_printf(bio_err," -alpn arg - enable ALPN extension, considering named protocols supported (comma-separated list)\n");
Bodo Möllered3883d2006-01-02 23:14:37 +0000404#endif
Dr. Stephen Henson2942dde2009-11-11 14:10:24 +0000405 BIO_printf(bio_err," -legacy_renegotiation - enable use of legacy renegotiation (dangerous)\n");
Dr. Stephen Hensonbe81f4d2012-02-11 23:20:53 +0000406 BIO_printf(bio_err," -use_srtp profiles - Offer SRTP key management with a colon-separated profile list\n");
Ben Lauriee0af0402011-11-15 23:50:52 +0000407 BIO_printf(bio_err," -keymatexport label - Export keying material using label\n");
408 BIO_printf(bio_err," -keymatexportlen len - Export len bytes of keying material (default 20)\n");
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000409 }
410
Bodo Möllered3883d2006-01-02 23:14:37 +0000411#ifndef OPENSSL_NO_TLSEXT
412
413/* This is a context that we pass to callbacks */
414typedef struct tlsextctx_st {
415 BIO * biodebug;
416 int ack;
417} tlsextctx;
418
419
Bodo Möllerb1277b92006-01-02 23:29:12 +0000420static int MS_CALLBACK ssl_servername_cb(SSL *s, int *ad, void *arg)
421 {
Bodo Möllered3883d2006-01-02 23:14:37 +0000422 tlsextctx * p = (tlsextctx *) arg;
Richard Levitte8de5b7f2006-01-04 12:02:43 +0000423 const char * hn= SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
Bodo Möllered3883d2006-01-02 23:14:37 +0000424 if (SSL_get_servername_type(s) != -1)
425 p->ack = !SSL_session_reused(s) && hn != NULL;
426 else
Bodo Möllerf1fd4542006-01-03 03:27:19 +0000427 BIO_printf(bio_err,"Can't use SSL_get_servername\n");
Bodo Möllered3883d2006-01-02 23:14:37 +0000428
Bodo Möller241520e2006-01-11 06:10:40 +0000429 return SSL_TLSEXT_ERR_OK;
Bodo Möllerb1277b92006-01-02 23:29:12 +0000430 }
Ben Laurieee2ffc22010-07-28 10:06:55 +0000431
Ben Laurieedc032b2011-03-12 17:01:19 +0000432#ifndef OPENSSL_NO_SRP
433
434/* This is a context that we pass to all callbacks */
435typedef struct srp_arg_st
436 {
437 char *srppassin;
438 char *srplogin;
439 int msg; /* copy from c_msg */
440 int debug; /* copy from c_debug */
441 int amp; /* allow more groups */
442 int strength /* minimal size for N */ ;
443 } SRP_ARG;
444
445#define SRP_NUMBER_ITERATIONS_FOR_PRIME 64
446
Dr. Stephen Hensonf2fc3072011-12-14 22:17:06 +0000447static int srp_Verify_N_and_g(const BIGNUM *N, const BIGNUM *g)
Ben Laurieedc032b2011-03-12 17:01:19 +0000448 {
449 BN_CTX *bn_ctx = BN_CTX_new();
450 BIGNUM *p = BN_new();
451 BIGNUM *r = BN_new();
452 int ret =
453 g != NULL && N != NULL && bn_ctx != NULL && BN_is_odd(N) &&
Dr. Stephen Hensonf2fc3072011-12-14 22:17:06 +0000454 BN_is_prime_ex(N, SRP_NUMBER_ITERATIONS_FOR_PRIME, bn_ctx, NULL) &&
Ben Laurieedc032b2011-03-12 17:01:19 +0000455 p != NULL && BN_rshift1(p, N) &&
456
457 /* p = (N-1)/2 */
Dr. Stephen Hensonf2fc3072011-12-14 22:17:06 +0000458 BN_is_prime_ex(p, SRP_NUMBER_ITERATIONS_FOR_PRIME, bn_ctx, NULL) &&
Ben Laurieedc032b2011-03-12 17:01:19 +0000459 r != NULL &&
460
461 /* verify g^((N-1)/2) == -1 (mod N) */
462 BN_mod_exp(r, g, p, N, bn_ctx) &&
463 BN_add_word(r, 1) &&
464 BN_cmp(r, N) == 0;
465
466 if(r)
467 BN_free(r);
468 if(p)
469 BN_free(p);
470 if(bn_ctx)
471 BN_CTX_free(bn_ctx);
472 return ret;
473 }
474
Dr. Stephen Hensonf2fc3072011-12-14 22:17:06 +0000475/* This callback is used here for two purposes:
476 - extended debugging
477 - making some primality tests for unknown groups
478 The callback is only called for a non default group.
479
480 An application does not need the call back at all if
481 only the stanard groups are used. In real life situations,
482 client and server already share well known groups,
483 thus there is no need to verify them.
484 Furthermore, in case that a server actually proposes a group that
485 is not one of those defined in RFC 5054, it is more appropriate
486 to add the group to a static list and then compare since
487 primality tests are rather cpu consuming.
488*/
489
Ben Laurieedc032b2011-03-12 17:01:19 +0000490static int MS_CALLBACK ssl_srp_verify_param_cb(SSL *s, void *arg)
491 {
492 SRP_ARG *srp_arg = (SRP_ARG *)arg;
493 BIGNUM *N = NULL, *g = NULL;
494 if (!(N = SSL_get_srp_N(s)) || !(g = SSL_get_srp_g(s)))
495 return 0;
496 if (srp_arg->debug || srp_arg->msg || srp_arg->amp == 1)
497 {
498 BIO_printf(bio_err, "SRP parameters:\n");
499 BIO_printf(bio_err,"\tN="); BN_print(bio_err,N);
500 BIO_printf(bio_err,"\n\tg="); BN_print(bio_err,g);
501 BIO_printf(bio_err,"\n");
502 }
503
504 if (SRP_check_known_gN_param(g,N))
505 return 1;
506
507 if (srp_arg->amp == 1)
508 {
509 if (srp_arg->debug)
510 BIO_printf(bio_err, "SRP param N and g are not known params, going to check deeper.\n");
511
Dr. Stephen Hensonf2fc3072011-12-14 22:17:06 +0000512/* The srp_moregroups is a real debugging feature.
Ben Laurieedc032b2011-03-12 17:01:19 +0000513 Implementors should rather add the value to the known ones.
514 The minimal size has already been tested.
515*/
Dr. Stephen Hensonf2fc3072011-12-14 22:17:06 +0000516 if (BN_num_bits(g) <= BN_BITS && srp_Verify_N_and_g(N,g))
Ben Laurieedc032b2011-03-12 17:01:19 +0000517 return 1;
518 }
519 BIO_printf(bio_err, "SRP param N and g rejected.\n");
520 return 0;
521 }
522
523#define PWD_STRLEN 1024
524
525static char * MS_CALLBACK ssl_give_srp_client_pwd_cb(SSL *s, void *arg)
526 {
527 SRP_ARG *srp_arg = (SRP_ARG *)arg;
528 char *pass = (char *)OPENSSL_malloc(PWD_STRLEN+1);
529 PW_CB_DATA cb_tmp;
530 int l;
531
532 cb_tmp.password = (char *)srp_arg->srppassin;
533 cb_tmp.prompt_info = "SRP user";
534 if ((l = password_callback(pass, PWD_STRLEN, 0, &cb_tmp))<0)
535 {
536 BIO_printf (bio_err, "Can't read Password\n");
537 OPENSSL_free(pass);
538 return NULL;
539 }
540 *(pass+l)= '\0';
541
542 return pass;
543 }
544
Ben Laurieedc032b2011-03-12 17:01:19 +0000545#endif
Ben Laurie333f9262011-11-15 22:59:20 +0000546 char *srtp_profiles = NULL;
Ben Laurieedc032b2011-03-12 17:01:19 +0000547
Ben Lauriebf488362010-09-05 17:14:01 +0000548# ifndef OPENSSL_NO_NEXTPROTONEG
Ben Laurieee2ffc22010-07-28 10:06:55 +0000549/* This the context that we pass to next_proto_cb */
550typedef struct tlsextnextprotoctx_st {
551 unsigned char *data;
552 unsigned short len;
553 int status;
554} tlsextnextprotoctx;
555
556static tlsextnextprotoctx next_proto;
557
558static int next_proto_cb(SSL *s, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)
559 {
560 tlsextnextprotoctx *ctx = arg;
561
562 if (!c_quiet)
563 {
564 /* We can assume that |in| is syntactically valid. */
565 unsigned i;
566 BIO_printf(bio_c_out, "Protocols advertised by server: ");
567 for (i = 0; i < inlen; )
568 {
569 if (i)
570 BIO_write(bio_c_out, ", ", 2);
571 BIO_write(bio_c_out, &in[i + 1], in[i]);
572 i += in[i] + 1;
573 }
574 BIO_write(bio_c_out, "\n", 1);
575 }
576
577 ctx->status = SSL_select_next_proto(out, outlen, in, inlen, ctx->data, ctx->len);
578 return SSL_TLSEXT_ERR_OK;
579 }
Ben Lauriebf488362010-09-05 17:14:01 +0000580# endif /* ndef OPENSSL_NO_NEXTPROTONEG */
Trevora398f822013-05-12 18:55:27 -0700581
582static int serverinfo_cli_cb(SSL* s, unsigned short ext_type,
583 const unsigned char* in, unsigned short inlen,
584 int* al, void* arg)
585 {
586 char pem_name[100];
587 unsigned char ext_buf[4 + 65536];
588
589 /* Reconstruct the type/len fields prior to extension data */
590 ext_buf[0] = ext_type >> 8;
591 ext_buf[1] = ext_type & 0xFF;
592 ext_buf[2] = inlen >> 8;
593 ext_buf[3] = inlen & 0xFF;
594 memcpy(ext_buf+4, in, inlen);
595
Ben Laurie70d416e2013-09-25 13:55:06 +0100596 BIO_snprintf(pem_name, sizeof(pem_name), "SERVERINFO FOR EXTENSION %d",
597 ext_type);
Trevora398f822013-05-12 18:55:27 -0700598 PEM_write_bio(bio_c_out, pem_name, "", ext_buf, 4 + inlen);
599 return 1;
600 }
601
Bodo Möllered3883d2006-01-02 23:14:37 +0000602#endif
603
Richard Levitte85c67492007-02-16 18:12:16 +0000604enum
605{
606 PROTO_OFF = 0,
607 PROTO_SMTP,
608 PROTO_POP3,
609 PROTO_IMAP,
Ben Lauried5bbead2008-10-14 19:11:26 +0000610 PROTO_FTP,
Dr. Stephen Henson640b86c2008-10-19 17:22:34 +0000611 PROTO_XMPP
Richard Levitte85c67492007-02-16 18:12:16 +0000612};
613
Ralf S. Engelschall667ac4e2000-02-11 09:47:18 +0000614int MAIN(int, char **);
615
Ulf Möller6b691a51999-04-19 21:31:43 +0000616int MAIN(int argc, char **argv)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000617 {
Dr. Stephen Henson74ecfab2012-07-23 23:34:28 +0000618 int build_chain = 0;
Nils Larsch67b6f1c2006-03-15 17:45:43 +0000619 SSL *con=NULL;
Dr. Stephen Henson4f7a2ab2011-05-11 22:50:18 +0000620#ifndef OPENSSL_NO_KRB5
621 KSSL_CTX *kctx;
622#endif
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000623 int s,k,width,state=0;
Richard Levitte135c0af2001-11-14 13:57:52 +0000624 char *cbuf=NULL,*sbuf=NULL,*mbuf=NULL;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000625 int cbuf_len,cbuf_off;
626 int sbuf_len,sbuf_off;
627 fd_set readfds,writefds;
628 short port=PORT;
629 int full_log=1;
630 char *host=SSL_HOST_NAME;
Geoff Thorpea9351322014-04-26 01:22:54 -0400631 const char *unix_path = NULL;
Carlos Alberto Lopez Perezb98af492012-08-06 02:24:51 +0200632 char *xmpphost = NULL;
Dr. Stephen Henson4e71d952012-12-12 00:50:26 +0000633 char *cert_file=NULL,*key_file=NULL,*chain_file=NULL;
Dr. Stephen Henson826a42a2004-11-16 17:30:59 +0000634 int cert_format = FORMAT_PEM, key_format = FORMAT_PEM;
635 char *passarg = NULL, *pass = NULL;
636 X509 *cert = NULL;
637 EVP_PKEY *key = NULL;
Dr. Stephen Henson4e71d952012-12-12 00:50:26 +0000638 STACK_OF(X509) *chain = NULL;
Dr. Stephen Henson5d2e07f2012-11-17 14:42:22 +0000639 char *CApath=NULL,*CAfile=NULL;
Dr. Stephen Hensona5afc0a2012-11-23 18:56:25 +0000640 char *chCApath=NULL,*chCAfile=NULL;
641 char *vfyCApath=NULL,*vfyCAfile=NULL;
Dr. Stephen Henson5d2e07f2012-11-17 14:42:22 +0000642 int reconnect=0,badop=0,verify=SSL_VERIFY_NONE;
Bodo Möller1bdb8631999-08-07 02:51:10 +0000643 int crlf=0;
Bodo Möllerc7ac31e1999-04-09 20:54:25 +0000644 int write_tty,read_tty,write_ssl,read_ssl,tty_on,ssl_pending;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000645 SSL_CTX *ctx=NULL;
646 int ret=1,in_init=1,i,nbio_test=0;
Richard Levitte85c67492007-02-16 18:12:16 +0000647 int starttls_proto = PROTO_OFF;
Dr. Stephen Hensondb997792009-06-30 15:56:35 +0000648 int prexit = 0;
649 X509_VERIFY_PARAM *vpm = NULL;
650 int badarg = 0;
Nils Larsch4ebb3422005-08-14 21:48:33 +0000651 const SSL_METHOD *meth=NULL;
Bodo Möllerb1277b92006-01-02 23:29:12 +0000652 int socket_type=SOCK_STREAM;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000653 BIO *sbio;
Lutz Jänicke52b621d2001-02-15 10:22:07 +0000654 char *inrand=NULL;
Richard Levitte85c67492007-02-16 18:12:16 +0000655 int mbuf_len=0;
Dr. Stephen Hensonb972fba2009-08-12 13:19:54 +0000656 struct timeval timeout, *timeoutp;
Richard Levitte0b13e9f2003-01-30 17:39:26 +0000657#ifndef OPENSSL_NO_ENGINE
Richard Levitte5270e702000-10-26 21:07:28 +0000658 char *engine_id=NULL;
Dr. Stephen Henson59d2d482008-06-03 11:26:27 +0000659 char *ssl_client_engine_id=NULL;
Dr. Stephen Henson70531c12008-12-20 17:04:40 +0000660 ENGINE *ssl_client_engine=NULL;
Richard Levitte0b13e9f2003-01-30 17:39:26 +0000661#endif
Dr. Stephen Henson70531c12008-12-20 17:04:40 +0000662 ENGINE *e=NULL;
Ulf Möller4700aea2006-04-11 21:34:21 +0000663#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS_R5)
Dr. Stephen Henson06f45361999-09-20 22:09:17 +0000664 struct timeval tv;
Ulf Möller4700aea2006-04-11 21:34:21 +0000665#if defined(OPENSSL_SYS_BEOS_R5)
666 int stdin_set = 0;
667#endif
Dr. Stephen Henson06f45361999-09-20 22:09:17 +0000668#endif
Bodo Möllered3883d2006-01-02 23:14:37 +0000669#ifndef OPENSSL_NO_TLSEXT
670 char *servername = NULL;
671 tlsextctx tlsextcbp =
672 {NULL,0};
Ben Lauriebf488362010-09-05 17:14:01 +0000673# ifndef OPENSSL_NO_NEXTPROTONEG
Ben Laurieee2ffc22010-07-28 10:06:55 +0000674 const char *next_proto_neg_in = NULL;
Ben Laurieee2ffc22010-07-28 10:06:55 +0000675# endif
Piotr Sikora29115752013-11-13 15:20:22 -0800676 const char *alpn_in = NULL;
Trevora398f822013-05-12 18:55:27 -0700677# define MAX_SI_TYPES 100
678 unsigned short serverinfo_types[MAX_SI_TYPES];
679 int serverinfo_types_count = 0;
Bodo Möllered3883d2006-01-02 23:14:37 +0000680#endif
Dr. Stephen Henson6434abb2007-08-11 23:18:29 +0000681 char *sess_in = NULL;
682 char *sess_out = NULL;
Ben Laurie36d16f82005-04-26 16:02:40 +0000683 struct sockaddr peer;
Dr. Stephen Henson6c617262005-04-27 16:27:14 +0000684 int peerlen = sizeof(peer);
Ben Laurie36d16f82005-04-26 16:02:40 +0000685 int enable_timeouts = 0 ;
Bodo Möllerb1277b92006-01-02 23:29:12 +0000686 long socket_mtu = 0;
Dr. Stephen Henson79bd20f2008-11-24 17:27:08 +0000687#ifndef OPENSSL_NO_JPAKE
Dr. Stephen Hensonb252cf02012-12-29 23:38:20 +0000688static char *jpake_secret = NULL;
689#define no_jpake !jpake_secret
690#else
691#define no_jpake 1
Dr. Stephen Hensoned551cd2008-11-12 17:28:18 +0000692#endif
Ben Laurieedc032b2011-03-12 17:01:19 +0000693#ifndef OPENSSL_NO_SRP
694 char * srppass = NULL;
695 int srp_lateuser = 0;
696 SRP_ARG srp_arg = {NULL,NULL,0,0,0,1024};
697#endif
Dr. Stephen Henson3208fc52012-07-03 14:53:27 +0000698 SSL_EXCERT *exc = NULL;
Ben Laurie36d16f82005-04-26 16:02:40 +0000699
Dr. Stephen Henson5d2e07f2012-11-17 14:42:22 +0000700 SSL_CONF_CTX *cctx = NULL;
701 STACK_OF(OPENSSL_STRING) *ssl_args = NULL;
Dr. Stephen Hensona70da5b2012-10-08 15:10:07 +0000702
Dr. Stephen Hensonfdb78f32012-12-02 16:16:28 +0000703 char *crl_file = NULL;
704 int crl_format = FORMAT_PEM;
Dr. Stephen Henson0090a682012-12-06 18:43:40 +0000705 int crl_download = 0;
Dr. Stephen Hensonfdb78f32012-12-02 16:16:28 +0000706 STACK_OF(X509_CRL) *crls = NULL;
Dr. Stephen Hensone03c5b52014-02-17 00:10:00 +0000707 int sdebug = 0;
Dr. Stephen Hensonfdb78f32012-12-02 16:16:28 +0000708
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000709 meth=SSLv23_client_method();
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000710
711 apps_startup();
Ralf S. Engelschall58964a41998-12-21 10:56:39 +0000712 c_Pause=0;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000713 c_quiet=0;
Richard Levittece301b62000-03-10 12:18:28 +0000714 c_ign_eof=0;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000715 c_debug=0;
Bodo Möllera661b652001-10-20 17:56:36 +0000716 c_msg=0;
Bodo Möller6d02d8e1999-03-31 12:06:30 +0000717 c_showcerts=0;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000718
719 if (bio_err == NULL)
720 bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
721
Dr. Stephen Henson3647bee2002-02-22 14:01:21 +0000722 if (!load_config(bio_err, NULL))
723 goto end;
Dr. Stephen Henson5d2e07f2012-11-17 14:42:22 +0000724 cctx = SSL_CONF_CTX_new();
725 if (!cctx)
726 goto end;
727 SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT);
728 SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CMDLINE);
Dr. Stephen Henson3647bee2002-02-22 14:01:21 +0000729
Richard Levitte26a3a482000-06-01 22:19:21 +0000730 if ( ((cbuf=OPENSSL_malloc(BUFSIZZ)) == NULL) ||
Richard Levitte135c0af2001-11-14 13:57:52 +0000731 ((sbuf=OPENSSL_malloc(BUFSIZZ)) == NULL) ||
732 ((mbuf=OPENSSL_malloc(BUFSIZZ)) == NULL))
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000733 {
734 BIO_printf(bio_err,"out of memory\n");
735 goto end;
736 }
737
738 verify_depth=0;
739 verify_error=X509_V_OK;
740#ifdef FIONBIO
741 c_nbio=0;
742#endif
743
744 argc--;
745 argv++;
746 while (argc >= 1)
747 {
748 if (strcmp(*argv,"-host") == 0)
749 {
750 if (--argc < 1) goto bad;
751 host= *(++argv);
752 }
753 else if (strcmp(*argv,"-port") == 0)
754 {
755 if (--argc < 1) goto bad;
756 port=atoi(*(++argv));
757 if (port == 0) goto bad;
758 }
759 else if (strcmp(*argv,"-connect") == 0)
760 {
761 if (--argc < 1) goto bad;
762 if (!extract_host_port(*(++argv),&host,NULL,&port))
763 goto bad;
764 }
Geoff Thorpea9351322014-04-26 01:22:54 -0400765 else if (strcmp(*argv,"-unix") == 0)
766 {
767 if (--argc < 1) goto bad;
768 unix_path = *(++argv);
769 }
Carlos Alberto Lopez Perezb98af492012-08-06 02:24:51 +0200770 else if (strcmp(*argv,"-xmpphost") == 0)
771 {
772 if (--argc < 1) goto bad;
773 xmpphost= *(++argv);
774 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000775 else if (strcmp(*argv,"-verify") == 0)
776 {
777 verify=SSL_VERIFY_PEER;
778 if (--argc < 1) goto bad;
779 verify_depth=atoi(*(++argv));
Dr. Stephen Henson2a7cbe72012-09-12 23:14:28 +0000780 if (!c_quiet)
781 BIO_printf(bio_err,"verify depth is %d\n",verify_depth);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000782 }
783 else if (strcmp(*argv,"-cert") == 0)
784 {
785 if (--argc < 1) goto bad;
786 cert_file= *(++argv);
787 }
Dr. Stephen Hensonfdb78f32012-12-02 16:16:28 +0000788 else if (strcmp(*argv,"-CRL") == 0)
789 {
790 if (--argc < 1) goto bad;
791 crl_file= *(++argv);
792 }
Dr. Stephen Henson0090a682012-12-06 18:43:40 +0000793 else if (strcmp(*argv,"-crl_download") == 0)
794 crl_download = 1;
Dr. Stephen Henson6434abb2007-08-11 23:18:29 +0000795 else if (strcmp(*argv,"-sess_out") == 0)
796 {
797 if (--argc < 1) goto bad;
798 sess_out = *(++argv);
799 }
800 else if (strcmp(*argv,"-sess_in") == 0)
801 {
802 if (--argc < 1) goto bad;
803 sess_in = *(++argv);
804 }
Dr. Stephen Henson826a42a2004-11-16 17:30:59 +0000805 else if (strcmp(*argv,"-certform") == 0)
806 {
807 if (--argc < 1) goto bad;
808 cert_format = str2fmt(*(++argv));
809 }
Dr. Stephen Hensonfdb78f32012-12-02 16:16:28 +0000810 else if (strcmp(*argv,"-CRLform") == 0)
811 {
812 if (--argc < 1) goto bad;
813 crl_format = str2fmt(*(++argv));
814 }
Dr. Stephen Hensondb997792009-06-30 15:56:35 +0000815 else if (args_verify(&argv, &argc, &badarg, bio_err, &vpm))
816 {
817 if (badarg)
818 goto bad;
819 continue;
820 }
Dr. Stephen Henson5d20c4f2006-09-17 17:16:28 +0000821 else if (strcmp(*argv,"-verify_return_error") == 0)
822 verify_return_error = 1;
Dr. Stephen Henson2a7cbe72012-09-12 23:14:28 +0000823 else if (strcmp(*argv,"-verify_quiet") == 0)
824 verify_quiet = 1;
825 else if (strcmp(*argv,"-brief") == 0)
826 {
827 c_brief = 1;
828 verify_quiet = 1;
829 c_quiet = 1;
830 }
Dr. Stephen Henson3208fc52012-07-03 14:53:27 +0000831 else if (args_excert(&argv, &argc, &badarg, bio_err, &exc))
832 {
833 if (badarg)
834 goto bad;
835 continue;
836 }
Dr. Stephen Henson5d2e07f2012-11-17 14:42:22 +0000837 else if (args_ssl(&argv, &argc, cctx, &badarg, bio_err, &ssl_args))
838 {
839 if (badarg)
840 goto bad;
841 continue;
842 }
Dr. Stephen Hensonc3ed3b62000-01-08 19:05:47 +0000843 else if (strcmp(*argv,"-prexit") == 0)
844 prexit=1;
Bodo Möller1bdb8631999-08-07 02:51:10 +0000845 else if (strcmp(*argv,"-crlf") == 0)
846 crlf=1;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000847 else if (strcmp(*argv,"-quiet") == 0)
Richard Levittece301b62000-03-10 12:18:28 +0000848 {
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000849 c_quiet=1;
Richard Levittece301b62000-03-10 12:18:28 +0000850 c_ign_eof=1;
851 }
852 else if (strcmp(*argv,"-ign_eof") == 0)
853 c_ign_eof=1;
Lutz Jänicke020d67f2008-10-22 06:46:14 +0000854 else if (strcmp(*argv,"-no_ign_eof") == 0)
855 c_ign_eof=0;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000856 else if (strcmp(*argv,"-pause") == 0)
857 c_Pause=1;
858 else if (strcmp(*argv,"-debug") == 0)
859 c_debug=1;
Dr. Stephen Henson6434abb2007-08-11 23:18:29 +0000860#ifndef OPENSSL_NO_TLSEXT
861 else if (strcmp(*argv,"-tlsextdebug") == 0)
862 c_tlsextdebug=1;
Dr. Stephen Henson67c8e7f2007-09-26 21:56:59 +0000863 else if (strcmp(*argv,"-status") == 0)
864 c_status_req=1;
Scott Deboy36086182013-06-18 14:34:38 -0700865 else if (strcmp(*argv,"-auth") == 0)
866 c_auth = 1;
867 else if (strcmp(*argv,"-auth_require_reneg") == 0)
868 c_auth_require_reneg = 1;
Dr. Stephen Henson6434abb2007-08-11 23:18:29 +0000869#endif
Andy Polyakov02a00bb2005-01-04 10:28:38 +0000870#ifdef WATT32
871 else if (strcmp(*argv,"-wdebug") == 0)
872 dbug_init();
873#endif
Bodo Möllera661b652001-10-20 17:56:36 +0000874 else if (strcmp(*argv,"-msg") == 0)
875 c_msg=1;
Dr. Stephen Henson93ab9e42012-06-15 12:46:09 +0000876 else if (strcmp(*argv,"-msgfile") == 0)
877 {
878 if (--argc < 1) goto bad;
879 bio_c_msg = BIO_new_file(*(++argv), "w");
880 }
881#ifndef OPENSSL_NO_SSL_TRACE
882 else if (strcmp(*argv,"-trace") == 0)
883 c_msg=2;
884#endif
Dr. Stephen Hensone03c5b52014-02-17 00:10:00 +0000885 else if (strcmp(*argv,"-security_debug") == 0)
886 { sdebug=1; }
887 else if (strcmp(*argv,"-security_debug_verbose") == 0)
888 { sdebug=2; }
Bodo Möller6d02d8e1999-03-31 12:06:30 +0000889 else if (strcmp(*argv,"-showcerts") == 0)
890 c_showcerts=1;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000891 else if (strcmp(*argv,"-nbio_test") == 0)
892 nbio_test=1;
893 else if (strcmp(*argv,"-state") == 0)
894 state=1;
Nils Larschddac1972006-03-10 23:06:27 +0000895#ifndef OPENSSL_NO_PSK
896 else if (strcmp(*argv,"-psk_identity") == 0)
897 {
898 if (--argc < 1) goto bad;
899 psk_identity=*(++argv);
900 }
901 else if (strcmp(*argv,"-psk") == 0)
902 {
903 size_t j;
904
905 if (--argc < 1) goto bad;
906 psk_key=*(++argv);
907 for (j = 0; j < strlen(psk_key); j++)
908 {
Andy Polyakova50bce82012-01-12 16:21:35 +0000909 if (isxdigit((unsigned char)psk_key[j]))
Nils Larschddac1972006-03-10 23:06:27 +0000910 continue;
911 BIO_printf(bio_err,"Not a hex number '%s'\n",*argv);
912 goto bad;
913 }
914 }
915#endif
Ben Laurieedc032b2011-03-12 17:01:19 +0000916#ifndef OPENSSL_NO_SRP
917 else if (strcmp(*argv,"-srpuser") == 0)
918 {
919 if (--argc < 1) goto bad;
920 srp_arg.srplogin= *(++argv);
921 meth=TLSv1_client_method();
922 }
923 else if (strcmp(*argv,"-srppass") == 0)
924 {
925 if (--argc < 1) goto bad;
926 srppass= *(++argv);
927 meth=TLSv1_client_method();
928 }
929 else if (strcmp(*argv,"-srp_strength") == 0)
930 {
931 if (--argc < 1) goto bad;
932 srp_arg.strength=atoi(*(++argv));
933 BIO_printf(bio_err,"SRP minimal length for N is %d\n",srp_arg.strength);
934 meth=TLSv1_client_method();
935 }
936 else if (strcmp(*argv,"-srp_lateuser") == 0)
937 {
938 srp_lateuser= 1;
939 meth=TLSv1_client_method();
940 }
941 else if (strcmp(*argv,"-srp_moregroups") == 0)
942 {
943 srp_arg.amp=1;
944 meth=TLSv1_client_method();
945 }
946#endif
Richard Levittecf1b7d92001-02-19 16:06:34 +0000947#ifndef OPENSSL_NO_SSL2
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000948 else if (strcmp(*argv,"-ssl2") == 0)
949 meth=SSLv2_client_method();
950#endif
Richard Levittecf1b7d92001-02-19 16:06:34 +0000951#ifndef OPENSSL_NO_SSL3
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000952 else if (strcmp(*argv,"-ssl3") == 0)
953 meth=SSLv3_client_method();
954#endif
Richard Levittecf1b7d92001-02-19 16:06:34 +0000955#ifndef OPENSSL_NO_TLS1
Dr. Stephen Henson7409d7a2011-04-29 22:56:51 +0000956 else if (strcmp(*argv,"-tls1_2") == 0)
957 meth=TLSv1_2_client_method();
Dr. Stephen Henson637f3742009-12-07 13:31:02 +0000958 else if (strcmp(*argv,"-tls1_1") == 0)
959 meth=TLSv1_1_client_method();
Ralf S. Engelschall58964a41998-12-21 10:56:39 +0000960 else if (strcmp(*argv,"-tls1") == 0)
961 meth=TLSv1_client_method();
962#endif
Ben Laurie36d16f82005-04-26 16:02:40 +0000963#ifndef OPENSSL_NO_DTLS1
Dr. Stephen Hensonc6913ee2013-04-06 15:50:12 +0100964 else if (strcmp(*argv,"-dtls") == 0)
965 {
966 meth=DTLS_client_method();
967 socket_type=SOCK_DGRAM;
968 }
Ben Laurie36d16f82005-04-26 16:02:40 +0000969 else if (strcmp(*argv,"-dtls1") == 0)
970 {
971 meth=DTLSv1_client_method();
Bodo Möllerb1277b92006-01-02 23:29:12 +0000972 socket_type=SOCK_DGRAM;
Ben Laurie36d16f82005-04-26 16:02:40 +0000973 }
Dr. Stephen Hensonc3b344e2013-03-20 15:49:14 +0000974 else if (strcmp(*argv,"-dtls1_2") == 0)
975 {
976 meth=DTLSv1_2_client_method();
977 socket_type=SOCK_DGRAM;
978 }
Ben Laurie36d16f82005-04-26 16:02:40 +0000979 else if (strcmp(*argv,"-timeout") == 0)
980 enable_timeouts=1;
981 else if (strcmp(*argv,"-mtu") == 0)
982 {
983 if (--argc < 1) goto bad;
Bodo Möllerb1277b92006-01-02 23:29:12 +0000984 socket_mtu = atol(*(++argv));
Ben Laurie36d16f82005-04-26 16:02:40 +0000985 }
986#endif
Dr. Stephen Henson826a42a2004-11-16 17:30:59 +0000987 else if (strcmp(*argv,"-keyform") == 0)
988 {
989 if (--argc < 1) goto bad;
990 key_format = str2fmt(*(++argv));
991 }
992 else if (strcmp(*argv,"-pass") == 0)
993 {
994 if (--argc < 1) goto bad;
995 passarg = *(++argv);
996 }
Dr. Stephen Henson4e71d952012-12-12 00:50:26 +0000997 else if (strcmp(*argv,"-cert_chain") == 0)
998 {
999 if (--argc < 1) goto bad;
1000 chain_file= *(++argv);
1001 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001002 else if (strcmp(*argv,"-key") == 0)
1003 {
1004 if (--argc < 1) goto bad;
1005 key_file= *(++argv);
1006 }
1007 else if (strcmp(*argv,"-reconnect") == 0)
1008 {
1009 reconnect=5;
1010 }
1011 else if (strcmp(*argv,"-CApath") == 0)
1012 {
1013 if (--argc < 1) goto bad;
1014 CApath= *(++argv);
1015 }
Dr. Stephen Hensona5afc0a2012-11-23 18:56:25 +00001016 else if (strcmp(*argv,"-chainCApath") == 0)
1017 {
1018 if (--argc < 1) goto bad;
1019 chCApath= *(++argv);
1020 }
1021 else if (strcmp(*argv,"-verifyCApath") == 0)
1022 {
1023 if (--argc < 1) goto bad;
1024 vfyCApath= *(++argv);
1025 }
Dr. Stephen Henson74ecfab2012-07-23 23:34:28 +00001026 else if (strcmp(*argv,"-build_chain") == 0)
1027 build_chain = 1;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001028 else if (strcmp(*argv,"-CAfile") == 0)
1029 {
1030 if (--argc < 1) goto bad;
1031 CAfile= *(++argv);
1032 }
Dr. Stephen Hensona5afc0a2012-11-23 18:56:25 +00001033 else if (strcmp(*argv,"-chainCAfile") == 0)
1034 {
1035 if (--argc < 1) goto bad;
1036 chCAfile= *(++argv);
1037 }
1038 else if (strcmp(*argv,"-verifyCAfile") == 0)
1039 {
1040 if (--argc < 1) goto bad;
1041 vfyCAfile= *(++argv);
1042 }
Dr. Stephen Henson6434abb2007-08-11 23:18:29 +00001043#ifndef OPENSSL_NO_TLSEXT
Ben Lauriebf488362010-09-05 17:14:01 +00001044# ifndef OPENSSL_NO_NEXTPROTONEG
Ben Laurieee2ffc22010-07-28 10:06:55 +00001045 else if (strcmp(*argv,"-nextprotoneg") == 0)
1046 {
1047 if (--argc < 1) goto bad;
1048 next_proto_neg_in = *(++argv);
1049 }
Piotr Sikora29115752013-11-13 15:20:22 -08001050# endif
Adam Langley6f017a82013-04-15 18:07:47 -04001051 else if (strcmp(*argv,"-alpn") == 0)
1052 {
1053 if (--argc < 1) goto bad;
1054 alpn_in = *(++argv);
1055 }
Trevora398f822013-05-12 18:55:27 -07001056 else if (strcmp(*argv,"-serverinfo") == 0)
1057 {
1058 char *c;
1059 int start = 0;
1060 int len;
1061
1062 if (--argc < 1) goto bad;
1063 c = *(++argv);
1064 serverinfo_types_count = 0;
1065 len = strlen(c);
1066 for (i = 0; i <= len; ++i)
1067 {
1068 if (i == len || c[i] == ',')
1069 {
1070 serverinfo_types[serverinfo_types_count]
1071 = atoi(c+start);
1072 serverinfo_types_count++;
1073 start = i+1;
1074 }
1075 if (serverinfo_types_count == MAX_SI_TYPES)
1076 break;
1077 }
1078 }
Dr. Stephen Henson6434abb2007-08-11 23:18:29 +00001079#endif
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001080#ifdef FIONBIO
1081 else if (strcmp(*argv,"-nbio") == 0)
1082 { c_nbio=1; }
1083#endif
Richard Levitte135c0af2001-11-14 13:57:52 +00001084 else if (strcmp(*argv,"-starttls") == 0)
1085 {
1086 if (--argc < 1) goto bad;
1087 ++argv;
1088 if (strcmp(*argv,"smtp") == 0)
Richard Levitte85c67492007-02-16 18:12:16 +00001089 starttls_proto = PROTO_SMTP;
Lutz Jänicke4f17dfc2003-05-28 20:24:57 +00001090 else if (strcmp(*argv,"pop3") == 0)
Richard Levitte85c67492007-02-16 18:12:16 +00001091 starttls_proto = PROTO_POP3;
1092 else if (strcmp(*argv,"imap") == 0)
1093 starttls_proto = PROTO_IMAP;
1094 else if (strcmp(*argv,"ftp") == 0)
1095 starttls_proto = PROTO_FTP;
Ben Lauried5bbead2008-10-14 19:11:26 +00001096 else if (strcmp(*argv, "xmpp") == 0)
1097 starttls_proto = PROTO_XMPP;
Richard Levitte135c0af2001-11-14 13:57:52 +00001098 else
1099 goto bad;
1100 }
Richard Levitte0b13e9f2003-01-30 17:39:26 +00001101#ifndef OPENSSL_NO_ENGINE
Richard Levitte5270e702000-10-26 21:07:28 +00001102 else if (strcmp(*argv,"-engine") == 0)
1103 {
1104 if (--argc < 1) goto bad;
1105 engine_id = *(++argv);
1106 }
Dr. Stephen Henson59d2d482008-06-03 11:26:27 +00001107 else if (strcmp(*argv,"-ssl_client_engine") == 0)
1108 {
1109 if (--argc < 1) goto bad;
1110 ssl_client_engine_id = *(++argv);
1111 }
Richard Levitte0b13e9f2003-01-30 17:39:26 +00001112#endif
Lutz Jänicke52b621d2001-02-15 10:22:07 +00001113 else if (strcmp(*argv,"-rand") == 0)
1114 {
1115 if (--argc < 1) goto bad;
1116 inrand= *(++argv);
1117 }
Bodo Möllered3883d2006-01-02 23:14:37 +00001118#ifndef OPENSSL_NO_TLSEXT
1119 else if (strcmp(*argv,"-servername") == 0)
1120 {
1121 if (--argc < 1) goto bad;
1122 servername= *(++argv);
1123 /* meth=TLSv1_client_method(); */
1124 }
1125#endif
Dr. Stephen Henson79bd20f2008-11-24 17:27:08 +00001126#ifndef OPENSSL_NO_JPAKE
Ben Laurie6caa4ed2008-10-26 18:40:52 +00001127 else if (strcmp(*argv,"-jpake") == 0)
1128 {
1129 if (--argc < 1) goto bad;
1130 jpake_secret = *++argv;
1131 }
Dr. Stephen Hensoned551cd2008-11-12 17:28:18 +00001132#endif
Ben Laurie333f9262011-11-15 22:59:20 +00001133 else if (strcmp(*argv,"-use_srtp") == 0)
1134 {
1135 if (--argc < 1) goto bad;
1136 srtp_profiles = *(++argv);
1137 }
Ben Lauriee0af0402011-11-15 23:50:52 +00001138 else if (strcmp(*argv,"-keymatexport") == 0)
1139 {
1140 if (--argc < 1) goto bad;
1141 keymatexportlabel= *(++argv);
1142 }
1143 else if (strcmp(*argv,"-keymatexportlen") == 0)
1144 {
1145 if (--argc < 1) goto bad;
1146 keymatexportlen=atoi(*(++argv));
1147 if (keymatexportlen == 0) goto bad;
1148 }
Ben Laurie333f9262011-11-15 22:59:20 +00001149 else
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001150 {
1151 BIO_printf(bio_err,"unknown option %s\n",*argv);
1152 badop=1;
1153 break;
1154 }
1155 argc--;
1156 argv++;
1157 }
1158 if (badop)
1159 {
1160bad:
1161 sc_usage();
1162 goto end;
1163 }
1164
Geoff Thorpea9351322014-04-26 01:22:54 -04001165 if (unix_path && (socket_type != SOCK_STREAM))
1166 {
1167 BIO_printf(bio_err, "Can't use unix sockets and datagrams together\n");
1168 goto end;
1169 }
Dr. Stephen Henson79bd20f2008-11-24 17:27:08 +00001170#if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK)
Ben Laurief3b7bda2008-11-16 12:47:12 +00001171 if (jpake_secret)
1172 {
1173 if (psk_key)
1174 {
1175 BIO_printf(bio_err,
1176 "Can't use JPAKE and PSK together\n");
1177 goto end;
1178 }
1179 psk_identity = "JPAKE";
1180 }
Ben Laurief3b7bda2008-11-16 12:47:12 +00001181#endif
1182
Richard Levittecead7f32002-07-16 06:52:03 +00001183 OpenSSL_add_ssl_algorithms();
1184 SSL_load_error_strings();
1185
Ben Lauriebf488362010-09-05 17:14:01 +00001186#if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
Ben Laurieee2ffc22010-07-28 10:06:55 +00001187 next_proto.status = -1;
1188 if (next_proto_neg_in)
1189 {
1190 next_proto.data = next_protos_parse(&next_proto.len, next_proto_neg_in);
1191 if (next_proto.data == NULL)
1192 {
1193 BIO_printf(bio_err, "Error parsing -nextprotoneg argument\n");
1194 goto end;
1195 }
1196 }
1197 else
1198 next_proto.data = NULL;
1199#endif
1200
Richard Levitte0b13e9f2003-01-30 17:39:26 +00001201#ifndef OPENSSL_NO_ENGINE
Richard Levittecead7f32002-07-16 06:52:03 +00001202 e = setup_engine(bio_err, engine_id, 1);
Dr. Stephen Henson59d2d482008-06-03 11:26:27 +00001203 if (ssl_client_engine_id)
1204 {
1205 ssl_client_engine = ENGINE_by_id(ssl_client_engine_id);
1206 if (!ssl_client_engine)
1207 {
1208 BIO_printf(bio_err,
1209 "Error getting client auth engine\n");
1210 goto end;
1211 }
1212 }
1213
Richard Levitte0b13e9f2003-01-30 17:39:26 +00001214#endif
Dr. Stephen Henson826a42a2004-11-16 17:30:59 +00001215 if (!app_passwd(bio_err, passarg, NULL, &pass, NULL))
1216 {
1217 BIO_printf(bio_err, "Error getting password\n");
1218 goto end;
1219 }
1220
1221 if (key_file == NULL)
1222 key_file = cert_file;
1223
Dr. Stephen Hensonabbc1862004-12-13 18:02:23 +00001224
1225 if (key_file)
1226
Dr. Stephen Henson826a42a2004-11-16 17:30:59 +00001227 {
Dr. Stephen Hensonabbc1862004-12-13 18:02:23 +00001228
1229 key = load_key(bio_err, key_file, key_format, 0, pass, e,
1230 "client certificate private key file");
1231 if (!key)
1232 {
1233 ERR_print_errors(bio_err);
1234 goto end;
1235 }
1236
Dr. Stephen Henson826a42a2004-11-16 17:30:59 +00001237 }
1238
Dr. Stephen Hensonabbc1862004-12-13 18:02:23 +00001239 if (cert_file)
Dr. Stephen Henson826a42a2004-11-16 17:30:59 +00001240
Dr. Stephen Henson826a42a2004-11-16 17:30:59 +00001241 {
Dr. Stephen Hensonabbc1862004-12-13 18:02:23 +00001242 cert = load_cert(bio_err,cert_file,cert_format,
1243 NULL, e, "client certificate file");
1244
1245 if (!cert)
1246 {
1247 ERR_print_errors(bio_err);
1248 goto end;
1249 }
Dr. Stephen Henson826a42a2004-11-16 17:30:59 +00001250 }
Richard Levittecead7f32002-07-16 06:52:03 +00001251
Dr. Stephen Henson4e71d952012-12-12 00:50:26 +00001252 if (chain_file)
1253 {
1254 chain = load_certs(bio_err, chain_file,FORMAT_PEM,
1255 NULL, e, "client certificate chain");
1256 if (!chain)
1257 goto end;
1258 }
1259
Dr. Stephen Hensonfdb78f32012-12-02 16:16:28 +00001260 if (crl_file)
1261 {
1262 X509_CRL *crl;
1263 crl = load_crl(crl_file, crl_format);
1264 if (!crl)
1265 {
1266 BIO_puts(bio_err, "Error loading CRL\n");
1267 ERR_print_errors(bio_err);
1268 goto end;
1269 }
1270 crls = sk_X509_CRL_new_null();
1271 if (!crls || !sk_X509_CRL_push(crls, crl))
1272 {
1273 BIO_puts(bio_err, "Error adding CRL\n");
1274 ERR_print_errors(bio_err);
1275 X509_CRL_free(crl);
1276 goto end;
1277 }
1278 }
1279
Dr. Stephen Henson3208fc52012-07-03 14:53:27 +00001280 if (!load_excert(&exc, bio_err))
1281 goto end;
1282
Lutz Jänicke52b621d2001-02-15 10:22:07 +00001283 if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL
1284 && !RAND_status())
1285 {
1286 BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
1287 }
1288 if (inrand != NULL)
1289 BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
1290 app_RAND_load_files(inrand));
Bodo Möllera31011e1999-10-26 01:56:29 +00001291
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001292 if (bio_c_out == NULL)
1293 {
Dr. Stephen Henson1740c9f2012-11-21 17:11:42 +00001294 if (c_quiet && !c_debug)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001295 {
1296 bio_c_out=BIO_new(BIO_s_null());
Dr. Stephen Henson1740c9f2012-11-21 17:11:42 +00001297 if (c_msg && !bio_c_msg)
1298 bio_c_msg=BIO_new_fp(stdout,BIO_NOCLOSE);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001299 }
1300 else
1301 {
1302 if (bio_c_out == NULL)
1303 bio_c_out=BIO_new_fp(stdout,BIO_NOCLOSE);
1304 }
1305 }
1306
Ben Laurieedc032b2011-03-12 17:01:19 +00001307#ifndef OPENSSL_NO_SRP
1308 if(!app_passwd(bio_err, srppass, NULL, &srp_arg.srppassin, NULL))
1309 {
1310 BIO_printf(bio_err, "Error getting password\n");
1311 goto end;
1312 }
1313#endif
1314
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001315 ctx=SSL_CTX_new(meth);
1316 if (ctx == NULL)
1317 {
1318 ERR_print_errors(bio_err);
1319 goto end;
1320 }
1321
Dr. Stephen Hensone03c5b52014-02-17 00:10:00 +00001322 if (sdebug)
1323 ssl_ctx_security_debug(ctx, bio_err, sdebug);
1324
Dr. Stephen Hensondb997792009-06-30 15:56:35 +00001325 if (vpm)
1326 SSL_CTX_set1_param(ctx, vpm);
1327
Dr. Stephen Hensonb252cf02012-12-29 23:38:20 +00001328 if (!args_ssl_call(ctx, bio_err, cctx, ssl_args, 1, no_jpake))
Dr. Stephen Henson5d2e07f2012-11-17 14:42:22 +00001329 {
1330 ERR_print_errors(bio_err);
1331 goto end;
1332 }
1333
Dr. Stephen Henson0090a682012-12-06 18:43:40 +00001334 if (!ssl_load_stores(ctx, vfyCApath, vfyCAfile, chCApath, chCAfile,
1335 crls, crl_download))
Dr. Stephen Hensona5afc0a2012-11-23 18:56:25 +00001336 {
1337 BIO_printf(bio_err, "Error loading store locations\n");
1338 ERR_print_errors(bio_err);
1339 goto end;
1340 }
1341
Dr. Stephen Henson59d2d482008-06-03 11:26:27 +00001342#ifndef OPENSSL_NO_ENGINE
1343 if (ssl_client_engine)
1344 {
1345 if (!SSL_CTX_set_client_cert_engine(ctx, ssl_client_engine))
1346 {
1347 BIO_puts(bio_err, "Error setting client auth engine\n");
1348 ERR_print_errors(bio_err);
1349 ENGINE_free(ssl_client_engine);
1350 goto end;
1351 }
1352 ENGINE_free(ssl_client_engine);
1353 }
1354#endif
1355
Nils Larschddac1972006-03-10 23:06:27 +00001356#ifndef OPENSSL_NO_PSK
Dr. Stephen Henson79bd20f2008-11-24 17:27:08 +00001357#ifdef OPENSSL_NO_JPAKE
1358 if (psk_key != NULL)
1359#else
Ben Laurief3b7bda2008-11-16 12:47:12 +00001360 if (psk_key != NULL || jpake_secret)
Dr. Stephen Henson79bd20f2008-11-24 17:27:08 +00001361#endif
Nils Larschddac1972006-03-10 23:06:27 +00001362 {
1363 if (c_debug)
Ben Laurief3b7bda2008-11-16 12:47:12 +00001364 BIO_printf(bio_c_out, "PSK key given or JPAKE in use, setting client callback\n");
Nils Larschddac1972006-03-10 23:06:27 +00001365 SSL_CTX_set_psk_client_callback(ctx, psk_client_cb);
1366 }
Ben Laurie333f9262011-11-15 22:59:20 +00001367 if (srtp_profiles != NULL)
1368 SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles);
Nils Larschddac1972006-03-10 23:06:27 +00001369#endif
Dr. Stephen Henson3208fc52012-07-03 14:53:27 +00001370 if (exc) ssl_ctx_set_excert(ctx, exc);
Ben Laurie36d16f82005-04-26 16:02:40 +00001371 /* DTLS: partial reads end up discarding unread UDP bytes :-(
1372 * Setting read ahead solves this problem.
1373 */
Bodo Möllerb1277b92006-01-02 23:29:12 +00001374 if (socket_type == SOCK_DGRAM) SSL_CTX_set_read_ahead(ctx, 1);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001375
Adam Langley6f017a82013-04-15 18:07:47 -04001376#if !defined(OPENSSL_NO_TLSEXT)
1377# if !defined(OPENSSL_NO_NEXTPROTONEG)
Ben Laurieee2ffc22010-07-28 10:06:55 +00001378 if (next_proto.data)
1379 SSL_CTX_set_next_proto_select_cb(ctx, next_proto_cb, &next_proto);
Adam Langley6f017a82013-04-15 18:07:47 -04001380# endif
1381 if (alpn_in)
1382 {
1383 unsigned short alpn_len;
1384 unsigned char *alpn = next_protos_parse(&alpn_len, alpn_in);
1385
1386 if (alpn == NULL)
1387 {
1388 BIO_printf(bio_err, "Error parsing -alpn argument\n");
1389 goto end;
1390 }
1391 SSL_CTX_set_alpn_protos(ctx, alpn, alpn_len);
Adam Langleya8989362013-07-15 15:57:16 -04001392 OPENSSL_free(alpn);
Adam Langley6f017a82013-04-15 18:07:47 -04001393 }
Ben Laurieee2ffc22010-07-28 10:06:55 +00001394#endif
Trevora398f822013-05-12 18:55:27 -07001395#ifndef OPENSSL_NO_TLSEXT
1396 if (serverinfo_types_count)
1397 {
1398 for (i = 0; i < serverinfo_types_count; i++)
1399 {
1400 SSL_CTX_set_custom_cli_ext(ctx,
1401 serverinfo_types[i],
1402 NULL,
1403 serverinfo_cli_cb,
1404 NULL);
1405 }
1406 }
1407#endif
Ben Laurieee2ffc22010-07-28 10:06:55 +00001408
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001409 if (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001410#if 0
1411 else
1412 SSL_CTX_set_cipher_list(ctx,getenv("SSL_CIPHER"));
1413#endif
1414
1415 SSL_CTX_set_verify(ctx,verify,verify_callback);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001416
1417 if ((!SSL_CTX_load_verify_locations(ctx,CAfile,CApath)) ||
1418 (!SSL_CTX_set_default_verify_paths(ctx)))
1419 {
Ulf Möller657e60f2000-02-03 23:23:24 +00001420 /* BIO_printf(bio_err,"error setting default verify locations\n"); */
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001421 ERR_print_errors(bio_err);
Ralf S. Engelschall58964a41998-12-21 10:56:39 +00001422 /* goto end; */
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001423 }
1424
Dr. Stephen Henson0090a682012-12-06 18:43:40 +00001425 ssl_ctx_add_crls(ctx, crls, crl_download);
Dr. Stephen Hensonfdb78f32012-12-02 16:16:28 +00001426
Dr. Stephen Henson4e71d952012-12-12 00:50:26 +00001427 if (!set_cert_key_stuff(ctx,cert,key,chain,build_chain))
Dr. Stephen Henson74ecfab2012-07-23 23:34:28 +00001428 goto end;
1429
Bodo Möllered3883d2006-01-02 23:14:37 +00001430#ifndef OPENSSL_NO_TLSEXT
Bodo Möllerb1277b92006-01-02 23:29:12 +00001431 if (servername != NULL)
1432 {
Bodo Möllered3883d2006-01-02 23:14:37 +00001433 tlsextcbp.biodebug = bio_err;
1434 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
1435 SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
Bodo Möllerb1277b92006-01-02 23:29:12 +00001436 }
Ben Laurieedc032b2011-03-12 17:01:19 +00001437#ifndef OPENSSL_NO_SRP
1438 if (srp_arg.srplogin)
1439 {
Dr. Stephen Hensonf2fc3072011-12-14 22:17:06 +00001440 if (!srp_lateuser && !SSL_CTX_set_srp_username(ctx, srp_arg.srplogin))
Ben Laurieedc032b2011-03-12 17:01:19 +00001441 {
1442 BIO_printf(bio_err,"Unable to set SRP username\n");
1443 goto end;
1444 }
1445 srp_arg.msg = c_msg;
1446 srp_arg.debug = c_debug ;
1447 SSL_CTX_set_srp_cb_arg(ctx,&srp_arg);
1448 SSL_CTX_set_srp_client_pwd_callback(ctx, ssl_give_srp_client_pwd_cb);
1449 SSL_CTX_set_srp_strength(ctx, srp_arg.strength);
1450 if (c_msg || c_debug || srp_arg.amp == 0)
1451 SSL_CTX_set_srp_verify_param_callback(ctx, ssl_srp_verify_param_cb);
1452 }
1453
1454#endif
Scott Deboy36086182013-06-18 14:34:38 -07001455 if (c_auth)
1456 {
1457 SSL_CTX_set_custom_cli_ext(ctx, TLSEXT_TYPE_client_authz, authz_tlsext_generate_cb, authz_tlsext_cb, bio_err);
1458 SSL_CTX_set_custom_cli_ext(ctx, TLSEXT_TYPE_server_authz, authz_tlsext_generate_cb, authz_tlsext_cb, bio_err);
1459 SSL_CTX_set_cli_supp_data(ctx, TLSEXT_SUPPLEMENTALDATATYPE_authz_data, suppdata_cb, auth_suppdata_generate_cb, bio_err);
1460 }
Bodo Möllered3883d2006-01-02 23:14:37 +00001461#endif
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001462
Dr. Stephen Henson82fc1d92000-02-03 02:56:48 +00001463 con=SSL_new(ctx);
Dr. Stephen Henson6434abb2007-08-11 23:18:29 +00001464 if (sess_in)
1465 {
1466 SSL_SESSION *sess;
1467 BIO *stmp = BIO_new_file(sess_in, "r");
1468 if (!stmp)
1469 {
1470 BIO_printf(bio_err, "Can't open session file %s\n",
1471 sess_in);
1472 ERR_print_errors(bio_err);
1473 goto end;
1474 }
1475 sess = PEM_read_bio_SSL_SESSION(stmp, NULL, 0, NULL);
1476 BIO_free(stmp);
1477 if (!sess)
1478 {
1479 BIO_printf(bio_err, "Can't open session file %s\n",
1480 sess_in);
1481 ERR_print_errors(bio_err);
1482 goto end;
1483 }
1484 SSL_set_session(con, sess);
1485 SSL_SESSION_free(sess);
1486 }
Bodo Möllered3883d2006-01-02 23:14:37 +00001487#ifndef OPENSSL_NO_TLSEXT
Bodo Möllerb1277b92006-01-02 23:29:12 +00001488 if (servername != NULL)
1489 {
Bodo Möllera13c20f2006-01-09 19:49:05 +00001490 if (!SSL_set_tlsext_host_name(con,servername))
Bodo Möllerb1277b92006-01-02 23:29:12 +00001491 {
Bodo Möllered3883d2006-01-02 23:14:37 +00001492 BIO_printf(bio_err,"Unable to set TLS servername extension.\n");
1493 ERR_print_errors(bio_err);
1494 goto end;
Bodo Möllerb1277b92006-01-02 23:29:12 +00001495 }
Bodo Möllered3883d2006-01-02 23:14:37 +00001496 }
Bodo Möllered3883d2006-01-02 23:14:37 +00001497#endif
Richard Levittecf1b7d92001-02-19 16:06:34 +00001498#ifndef OPENSSL_NO_KRB5
Dr. Stephen Henson4f7a2ab2011-05-11 22:50:18 +00001499 if (con && (kctx = kssl_ctx_new()) != NULL)
Richard Levittef9b3bff2000-11-30 22:53:34 +00001500 {
Dr. Stephen Henson4f7a2ab2011-05-11 22:50:18 +00001501 SSL_set0_kssl_ctx(con, kctx);
1502 kssl_ctx_setstring(kctx, KSSL_SERVER, host);
Richard Levittef9b3bff2000-11-30 22:53:34 +00001503 }
Richard Levittecf1b7d92001-02-19 16:06:34 +00001504#endif /* OPENSSL_NO_KRB5 */
Ralf S. Engelschall58964a41998-12-21 10:56:39 +00001505/* SSL_set_cipher_list(con,"RC4-MD5"); */
Bodo Möller761772d2007-09-21 06:54:24 +00001506#if 0
1507#ifdef TLSEXT_TYPE_opaque_prf_input
Bodo Möller86d4bc32007-09-23 11:08:59 +00001508 SSL_set_tlsext_opaque_prf_input(con, "Test client", 11);
Bodo Möller761772d2007-09-21 06:54:24 +00001509#endif
1510#endif
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001511
1512re_start:
1513
Geoff Thorpea9351322014-04-26 01:22:54 -04001514 if ((!unix_path && (init_client(&s,host,port,socket_type) == 0)) ||
1515 (unix_path && (init_client_unix(&s,unix_path) == 0)))
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001516 {
Ralf S. Engelschall58964a41998-12-21 10:56:39 +00001517 BIO_printf(bio_err,"connect:errno=%d\n",get_last_socket_error());
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001518 SHUTDOWN(s);
1519 goto end;
1520 }
1521 BIO_printf(bio_c_out,"CONNECTED(%08X)\n",s);
1522
1523#ifdef FIONBIO
1524 if (c_nbio)
1525 {
1526 unsigned long l=1;
1527 BIO_printf(bio_c_out,"turning on non blocking io\n");
Ralf S. Engelschall58964a41998-12-21 10:56:39 +00001528 if (BIO_socket_ioctl(s,FIONBIO,&l) < 0)
1529 {
1530 ERR_print_errors(bio_err);
1531 goto end;
1532 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001533 }
1534#endif
Dr. Stephen Henson08557cf2011-04-29 22:37:12 +00001535 if (c_Pause & 0x01) SSL_set_debug(con, 1);
Ben Laurie36d16f82005-04-26 16:02:40 +00001536
Dr. Stephen Hensonc3b344e2013-03-20 15:49:14 +00001537 if (socket_type == SOCK_DGRAM)
Ben Laurie36d16f82005-04-26 16:02:40 +00001538 {
Ben Laurie36d16f82005-04-26 16:02:40 +00001539
1540 sbio=BIO_new_dgram(s,BIO_NOCLOSE);
Dr. Stephen Henson6c617262005-04-27 16:27:14 +00001541 if (getsockname(s, &peer, (void *)&peerlen) < 0)
Ben Laurie36d16f82005-04-26 16:02:40 +00001542 {
1543 BIO_printf(bio_err, "getsockname:errno=%d\n",
1544 get_last_socket_error());
1545 SHUTDOWN(s);
1546 goto end;
1547 }
1548
Dr. Stephen Henson710069c2007-08-12 17:44:32 +00001549 (void)BIO_ctrl_set_connected(sbio, 1, &peer);
Ben Laurie36d16f82005-04-26 16:02:40 +00001550
Bodo Möllerb1277b92006-01-02 23:29:12 +00001551 if (enable_timeouts)
Ben Laurie36d16f82005-04-26 16:02:40 +00001552 {
1553 timeout.tv_sec = 0;
1554 timeout.tv_usec = DGRAM_RCV_TIMEOUT;
1555 BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
1556
1557 timeout.tv_sec = 0;
1558 timeout.tv_usec = DGRAM_SND_TIMEOUT;
1559 BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
1560 }
1561
Dr. Stephen Henson046f2102009-05-17 16:04:58 +00001562 if (socket_mtu > 28)
Ben Laurie36d16f82005-04-26 16:02:40 +00001563 {
1564 SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
Dr. Stephen Henson046f2102009-05-17 16:04:58 +00001565 SSL_set_mtu(con, socket_mtu - 28);
Ben Laurie36d16f82005-04-26 16:02:40 +00001566 }
1567 else
1568 /* want to do MTU discovery */
1569 BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
1570 }
1571 else
1572 sbio=BIO_new_socket(s,BIO_NOCLOSE);
1573
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001574 if (nbio_test)
1575 {
1576 BIO *test;
1577
1578 test=BIO_new(BIO_f_nbio_test());
1579 sbio=BIO_push(test,sbio);
1580 }
1581
1582 if (c_debug)
1583 {
Dr. Stephen Henson08557cf2011-04-29 22:37:12 +00001584 SSL_set_debug(con, 1);
Richard Levitte25495642004-08-12 08:58:55 +00001585 BIO_set_callback(sbio,bio_dump_callback);
Nils Larsch7806f3d2006-11-29 20:54:57 +00001586 BIO_set_callback_arg(sbio,(char *)bio_c_out);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001587 }
Bodo Möllera661b652001-10-20 17:56:36 +00001588 if (c_msg)
1589 {
Dr. Stephen Henson93ab9e42012-06-15 12:46:09 +00001590#ifndef OPENSSL_NO_SSL_TRACE
1591 if (c_msg == 2)
1592 SSL_set_msg_callback(con, SSL_trace);
1593 else
1594#endif
1595 SSL_set_msg_callback(con, msg_cb);
1596 SSL_set_msg_callback_arg(con, bio_c_msg ? bio_c_msg : bio_c_out);
Bodo Möllera661b652001-10-20 17:56:36 +00001597 }
Dr. Stephen Henson6434abb2007-08-11 23:18:29 +00001598#ifndef OPENSSL_NO_TLSEXT
1599 if (c_tlsextdebug)
1600 {
1601 SSL_set_tlsext_debug_callback(con, tlsext_cb);
1602 SSL_set_tlsext_debug_arg(con, bio_c_out);
1603 }
Dr. Stephen Henson67c8e7f2007-09-26 21:56:59 +00001604 if (c_status_req)
1605 {
1606 SSL_set_tlsext_status_type(con, TLSEXT_STATUSTYPE_ocsp);
1607 SSL_CTX_set_tlsext_status_cb(ctx, ocsp_resp_cb);
1608 SSL_CTX_set_tlsext_status_arg(ctx, bio_c_out);
1609#if 0
1610{
1611STACK_OF(OCSP_RESPID) *ids = sk_OCSP_RESPID_new_null();
1612OCSP_RESPID *id = OCSP_RESPID_new();
1613id->value.byKey = ASN1_OCTET_STRING_new();
1614id->type = V_OCSP_RESPID_KEY;
1615ASN1_STRING_set(id->value.byKey, "Hello World", -1);
1616sk_OCSP_RESPID_push(ids, id);
1617SSL_set_tlsext_status_ids(con, ids);
1618}
1619#endif
1620 }
Dr. Stephen Henson6434abb2007-08-11 23:18:29 +00001621#endif
Dr. Stephen Henson79bd20f2008-11-24 17:27:08 +00001622#ifndef OPENSSL_NO_JPAKE
Ben Laurie6caa4ed2008-10-26 18:40:52 +00001623 if (jpake_secret)
1624 jpake_client_auth(bio_c_out, sbio, jpake_secret);
Dr. Stephen Hensoned551cd2008-11-12 17:28:18 +00001625#endif
Ben Laurie6caa4ed2008-10-26 18:40:52 +00001626
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001627 SSL_set_bio(con,sbio,sbio);
1628 SSL_set_connect_state(con);
1629
1630 /* ok, lets connect */
1631 width=SSL_get_fd(con)+1;
1632
1633 read_tty=1;
1634 write_tty=0;
1635 tty_on=0;
1636 read_ssl=1;
1637 write_ssl=1;
1638
1639 cbuf_len=0;
1640 cbuf_off=0;
1641 sbuf_len=0;
1642 sbuf_off=0;
1643
Richard Levitte135c0af2001-11-14 13:57:52 +00001644 /* This is an ugly hack that does a lot of assumptions */
Lutz Jänickeee373e72007-02-22 17:39:47 +00001645 /* We do have to handle multi-line responses which may come
1646 in a single packet or not. We therefore have to use
1647 BIO_gets() which does need a buffering BIO. So during
1648 the initial chitchat we do push a buffering BIO into the
1649 chain that is removed again later on to not disturb the
1650 rest of the s_client operation. */
Richard Levitte85c67492007-02-16 18:12:16 +00001651 if (starttls_proto == PROTO_SMTP)
Richard Levitte135c0af2001-11-14 13:57:52 +00001652 {
Lutz Jänicke8d724762007-02-21 18:20:41 +00001653 int foundit=0;
Lutz Jänickeee373e72007-02-22 17:39:47 +00001654 BIO *fbio = BIO_new(BIO_f_buffer());
1655 BIO_push(fbio, sbio);
Richard Levitte85c67492007-02-16 18:12:16 +00001656 /* wait for multi-line response to end from SMTP */
1657 do
1658 {
Lutz Jänickeee373e72007-02-22 17:39:47 +00001659 mbuf_len = BIO_gets(fbio,mbuf,BUFSIZZ);
Richard Levitte85c67492007-02-16 18:12:16 +00001660 }
1661 while (mbuf_len>3 && mbuf[3]=='-');
Lutz Jänicke8d724762007-02-21 18:20:41 +00001662 /* STARTTLS command requires EHLO... */
Lutz Jänickeee373e72007-02-22 17:39:47 +00001663 BIO_printf(fbio,"EHLO openssl.client.net\r\n");
Dr. Stephen Henson710069c2007-08-12 17:44:32 +00001664 (void)BIO_flush(fbio);
Lutz Jänicke8d724762007-02-21 18:20:41 +00001665 /* wait for multi-line response to end EHLO SMTP response */
1666 do
1667 {
Lutz Jänickeee373e72007-02-22 17:39:47 +00001668 mbuf_len = BIO_gets(fbio,mbuf,BUFSIZZ);
Lutz Jänicke8d724762007-02-21 18:20:41 +00001669 if (strstr(mbuf,"STARTTLS"))
1670 foundit=1;
1671 }
1672 while (mbuf_len>3 && mbuf[3]=='-');
Dr. Stephen Henson710069c2007-08-12 17:44:32 +00001673 (void)BIO_flush(fbio);
Lutz Jänickeee373e72007-02-22 17:39:47 +00001674 BIO_pop(fbio);
1675 BIO_free(fbio);
Lutz Jänicke8d724762007-02-21 18:20:41 +00001676 if (!foundit)
1677 BIO_printf(bio_err,
1678 "didn't found starttls in server response,"
1679 " try anyway...\n");
Richard Levitte135c0af2001-11-14 13:57:52 +00001680 BIO_printf(sbio,"STARTTLS\r\n");
1681 BIO_read(sbio,sbuf,BUFSIZZ);
1682 }
Richard Levitte85c67492007-02-16 18:12:16 +00001683 else if (starttls_proto == PROTO_POP3)
Lutz Jänicke4f17dfc2003-05-28 20:24:57 +00001684 {
1685 BIO_read(sbio,mbuf,BUFSIZZ);
1686 BIO_printf(sbio,"STLS\r\n");
1687 BIO_read(sbio,sbuf,BUFSIZZ);
1688 }
Richard Levitte85c67492007-02-16 18:12:16 +00001689 else if (starttls_proto == PROTO_IMAP)
1690 {
Lutz Jänicke8d724762007-02-21 18:20:41 +00001691 int foundit=0;
Lutz Jänickeee373e72007-02-22 17:39:47 +00001692 BIO *fbio = BIO_new(BIO_f_buffer());
1693 BIO_push(fbio, sbio);
1694 BIO_gets(fbio,mbuf,BUFSIZZ);
Lutz Jänicke8d724762007-02-21 18:20:41 +00001695 /* STARTTLS command requires CAPABILITY... */
Lutz Jänickeee373e72007-02-22 17:39:47 +00001696 BIO_printf(fbio,". CAPABILITY\r\n");
Dr. Stephen Henson710069c2007-08-12 17:44:32 +00001697 (void)BIO_flush(fbio);
Lutz Jänicke8d724762007-02-21 18:20:41 +00001698 /* wait for multi-line CAPABILITY response */
1699 do
1700 {
Lutz Jänickeee373e72007-02-22 17:39:47 +00001701 mbuf_len = BIO_gets(fbio,mbuf,BUFSIZZ);
Lutz Jänicke8d724762007-02-21 18:20:41 +00001702 if (strstr(mbuf,"STARTTLS"))
1703 foundit=1;
1704 }
Lutz Jänickeee373e72007-02-22 17:39:47 +00001705 while (mbuf_len>3 && mbuf[0]!='.');
Dr. Stephen Henson710069c2007-08-12 17:44:32 +00001706 (void)BIO_flush(fbio);
Lutz Jänickeee373e72007-02-22 17:39:47 +00001707 BIO_pop(fbio);
1708 BIO_free(fbio);
Lutz Jänicke8d724762007-02-21 18:20:41 +00001709 if (!foundit)
1710 BIO_printf(bio_err,
1711 "didn't found STARTTLS in server response,"
1712 " try anyway...\n");
1713 BIO_printf(sbio,". STARTTLS\r\n");
Richard Levitte85c67492007-02-16 18:12:16 +00001714 BIO_read(sbio,sbuf,BUFSIZZ);
1715 }
1716 else if (starttls_proto == PROTO_FTP)
1717 {
Lutz Jänickeee373e72007-02-22 17:39:47 +00001718 BIO *fbio = BIO_new(BIO_f_buffer());
1719 BIO_push(fbio, sbio);
Richard Levitte85c67492007-02-16 18:12:16 +00001720 /* wait for multi-line response to end from FTP */
1721 do
1722 {
Lutz Jänickeee373e72007-02-22 17:39:47 +00001723 mbuf_len = BIO_gets(fbio,mbuf,BUFSIZZ);
Richard Levitte85c67492007-02-16 18:12:16 +00001724 }
1725 while (mbuf_len>3 && mbuf[3]=='-');
Dr. Stephen Henson710069c2007-08-12 17:44:32 +00001726 (void)BIO_flush(fbio);
Lutz Jänickeee373e72007-02-22 17:39:47 +00001727 BIO_pop(fbio);
1728 BIO_free(fbio);
Richard Levitte85c67492007-02-16 18:12:16 +00001729 BIO_printf(sbio,"AUTH TLS\r\n");
1730 BIO_read(sbio,sbuf,BUFSIZZ);
1731 }
Ben Lauried5bbead2008-10-14 19:11:26 +00001732 if (starttls_proto == PROTO_XMPP)
1733 {
1734 int seen = 0;
1735 BIO_printf(sbio,"<stream:stream "
1736 "xmlns:stream='http://etherx.jabber.org/streams' "
Ben Lauried2625fd2013-09-05 17:28:05 +01001737 "xmlns='jabber:client' to='%s' version='1.0'>", xmpphost ?
1738 xmpphost : host);
Ben Lauried5bbead2008-10-14 19:11:26 +00001739 seen = BIO_read(sbio,mbuf,BUFSIZZ);
1740 mbuf[seen] = 0;
Carlos Alberto Lopez Perez4e48c772012-08-06 01:45:51 +02001741 while (!strstr(mbuf, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'") &&
1742 !strstr(mbuf, "<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\""))
Ben Lauried5bbead2008-10-14 19:11:26 +00001743 {
Ben Lauried5bbead2008-10-14 19:11:26 +00001744 seen = BIO_read(sbio,mbuf,BUFSIZZ);
Carlos Alberto Lopez Perez4249d4b2012-08-06 02:00:07 +02001745
1746 if (seen <= 0)
1747 goto shut;
1748
Ben Lauried5bbead2008-10-14 19:11:26 +00001749 mbuf[seen] = 0;
1750 }
1751 BIO_printf(sbio, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
1752 seen = BIO_read(sbio,sbuf,BUFSIZZ);
1753 sbuf[seen] = 0;
1754 if (!strstr(sbuf, "<proceed"))
1755 goto shut;
1756 mbuf[0] = 0;
1757 }
Richard Levitte135c0af2001-11-14 13:57:52 +00001758
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001759 for (;;)
1760 {
1761 FD_ZERO(&readfds);
1762 FD_ZERO(&writefds);
1763
Dr. Stephen Hensonb972fba2009-08-12 13:19:54 +00001764 if ((SSL_version(con) == DTLS1_VERSION) &&
1765 DTLSv1_get_timeout(con, &timeout))
1766 timeoutp = &timeout;
1767 else
1768 timeoutp = NULL;
1769
Ralf S. Engelschall58964a41998-12-21 10:56:39 +00001770 if (SSL_in_init(con) && !SSL_total_renegotiations(con))
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001771 {
1772 in_init=1;
1773 tty_on=0;
1774 }
1775 else
1776 {
1777 tty_on=1;
1778 if (in_init)
1779 {
1780 in_init=0;
Bodo Möller761772d2007-09-21 06:54:24 +00001781#if 0 /* This test doesn't really work as intended (needs to be fixed) */
Bodo Möllered3883d2006-01-02 23:14:37 +00001782#ifndef OPENSSL_NO_TLSEXT
Bodo Möllerb166f132006-06-15 19:00:34 +00001783 if (servername != NULL && !SSL_session_reused(con))
1784 {
1785 BIO_printf(bio_c_out,"Server did %sacknowledge servername extension.\n",tlsextcbp.ack?"":"not ");
1786 }
Bodo Möllered3883d2006-01-02 23:14:37 +00001787#endif
Bodo Möller761772d2007-09-21 06:54:24 +00001788#endif
Dr. Stephen Henson6434abb2007-08-11 23:18:29 +00001789 if (sess_out)
1790 {
1791 BIO *stmp = BIO_new_file(sess_out, "w");
1792 if (stmp)
1793 {
1794 PEM_write_bio_SSL_SESSION(stmp, SSL_get_session(con));
1795 BIO_free(stmp);
1796 }
1797 else
1798 BIO_printf(bio_err, "Error writing session file %s\n", sess_out);
1799 }
Dr. Stephen Henson2a7cbe72012-09-12 23:14:28 +00001800 if (c_brief)
1801 {
1802 BIO_puts(bio_err,
1803 "CONNECTION ESTABLISHED\n");
1804 print_ssl_summary(bio_err, con);
1805 }
Scott Deboy67c408c2013-08-01 11:54:09 -07001806 /*handshake is complete - free the generated supp data allocated in the callback */
1807 if (generated_supp_data)
1808 {
1809 OPENSSL_free(generated_supp_data);
1810 generated_supp_data = NULL;
1811 }
1812
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001813 print_stuff(bio_c_out,con,full_log);
1814 if (full_log > 0) full_log--;
1815
Lutz Jänicke4f17dfc2003-05-28 20:24:57 +00001816 if (starttls_proto)
Richard Levitte135c0af2001-11-14 13:57:52 +00001817 {
1818 BIO_printf(bio_err,"%s",mbuf);
1819 /* We don't need to know any more */
Richard Levitte85c67492007-02-16 18:12:16 +00001820 starttls_proto = PROTO_OFF;
Richard Levitte135c0af2001-11-14 13:57:52 +00001821 }
1822
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001823 if (reconnect)
1824 {
1825 reconnect--;
1826 BIO_printf(bio_c_out,"drop connection and then reconnect\n");
1827 SSL_shutdown(con);
1828 SSL_set_connect_state(con);
1829 SHUTDOWN(SSL_get_fd(con));
1830 goto re_start;
1831 }
1832 }
1833 }
1834
Bodo Möllerc7ac31e1999-04-09 20:54:25 +00001835 ssl_pending = read_ssl && SSL_pending(con);
1836
1837 if (!ssl_pending)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001838 {
Ulf Möller4700aea2006-04-11 21:34:21 +00001839#if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE) && !defined (OPENSSL_SYS_BEOS_R5)
Bodo Möllerc7ac31e1999-04-09 20:54:25 +00001840 if (tty_on)
1841 {
Dr. Stephen Henson7bf73332006-04-17 12:22:13 +00001842 if (read_tty) openssl_fdset(fileno(stdin),&readfds);
1843 if (write_tty) openssl_fdset(fileno(stdout),&writefds);
Bodo Möllerc7ac31e1999-04-09 20:54:25 +00001844 }
Bodo Möllerc7ac31e1999-04-09 20:54:25 +00001845 if (read_ssl)
Dr. Stephen Henson7bf73332006-04-17 12:22:13 +00001846 openssl_fdset(SSL_get_fd(con),&readfds);
Bodo Möllerc7ac31e1999-04-09 20:54:25 +00001847 if (write_ssl)
Dr. Stephen Henson7bf73332006-04-17 12:22:13 +00001848 openssl_fdset(SSL_get_fd(con),&writefds);
Dr. Stephen Henson06f45361999-09-20 22:09:17 +00001849#else
1850 if(!tty_on || !write_tty) {
1851 if (read_ssl)
Dr. Stephen Henson7bf73332006-04-17 12:22:13 +00001852 openssl_fdset(SSL_get_fd(con),&readfds);
Dr. Stephen Henson06f45361999-09-20 22:09:17 +00001853 if (write_ssl)
Dr. Stephen Henson7bf73332006-04-17 12:22:13 +00001854 openssl_fdset(SSL_get_fd(con),&writefds);
Dr. Stephen Henson06f45361999-09-20 22:09:17 +00001855 }
1856#endif
Bodo Möllerc7ac31e1999-04-09 20:54:25 +00001857/* printf("mode tty(%d %d%d) ssl(%d%d)\n",
1858 tty_on,read_tty,write_tty,read_ssl,write_ssl);*/
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001859
Ulf Möller75e07701999-05-13 13:21:17 +00001860 /* Note: under VMS with SOCKETSHR the second parameter
Ulf Möller7d7d2cb1999-05-13 11:37:32 +00001861 * is currently of type (int *) whereas under other
1862 * systems it is (void *) if you don't have a cast it
1863 * will choke the compiler: if you do have a cast then
1864 * you can either go for (int *) or (void *).
1865 */
Richard Levitte3d7c4a52003-09-27 21:56:08 +00001866#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
1867 /* Under Windows/DOS we make the assumption that we can
Dr. Stephen Henson06f45361999-09-20 22:09:17 +00001868 * always write to the tty: therefore if we need to
1869 * write to the tty we just fall through. Otherwise
1870 * we timeout the select every second and see if there
1871 * are any keypresses. Note: this is a hack, in a proper
1872 * Windows application we wouldn't do this.
1873 */
Ulf Möller4ec19e22000-02-20 20:59:21 +00001874 i=0;
Dr. Stephen Henson06f45361999-09-20 22:09:17 +00001875 if(!write_tty) {
1876 if(read_tty) {
1877 tv.tv_sec = 1;
1878 tv.tv_usec = 0;
1879 i=select(width,(void *)&readfds,(void *)&writefds,
1880 NULL,&tv);
Richard Levitte3d7c4a52003-09-27 21:56:08 +00001881#if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)
Richard Levitte0bf23d92002-11-15 22:37:18 +00001882 if(!i && (!_kbhit() || !read_tty) ) continue;
1883#else
Richard Levittea9ef75c2000-06-01 11:23:20 +00001884 if(!i && (!((_kbhit()) || (WAIT_OBJECT_0 == WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0))) || !read_tty) ) continue;
Richard Levitte0bf23d92002-11-15 22:37:18 +00001885#endif
Dr. Stephen Henson06f45361999-09-20 22:09:17 +00001886 } else i=select(width,(void *)&readfds,(void *)&writefds,
Dr. Stephen Hensonb972fba2009-08-12 13:19:54 +00001887 NULL,timeoutp);
Dr. Stephen Henson06f45361999-09-20 22:09:17 +00001888 }
Richard Levitte47c17352004-06-28 11:55:28 +00001889#elif defined(OPENSSL_SYS_NETWARE)
1890 if(!write_tty) {
1891 if(read_tty) {
1892 tv.tv_sec = 1;
1893 tv.tv_usec = 0;
1894 i=select(width,(void *)&readfds,(void *)&writefds,
1895 NULL,&tv);
1896 } else i=select(width,(void *)&readfds,(void *)&writefds,
Dr. Stephen Hensonb972fba2009-08-12 13:19:54 +00001897 NULL,timeoutp);
Richard Levitte47c17352004-06-28 11:55:28 +00001898 }
Ulf Möller4700aea2006-04-11 21:34:21 +00001899#elif defined(OPENSSL_SYS_BEOS_R5)
1900 /* Under BeOS-R5 the situation is similar to DOS */
1901 i=0;
1902 stdin_set = 0;
1903 (void)fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
1904 if(!write_tty) {
1905 if(read_tty) {
1906 tv.tv_sec = 1;
1907 tv.tv_usec = 0;
1908 i=select(width,(void *)&readfds,(void *)&writefds,
1909 NULL,&tv);
1910 if (read(fileno(stdin), sbuf, 0) >= 0)
1911 stdin_set = 1;
1912 if (!i && (stdin_set != 1 || !read_tty))
1913 continue;
1914 } else i=select(width,(void *)&readfds,(void *)&writefds,
Dr. Stephen Hensonb972fba2009-08-12 13:19:54 +00001915 NULL,timeoutp);
Ulf Möller4700aea2006-04-11 21:34:21 +00001916 }
1917 (void)fcntl(fileno(stdin), F_SETFL, 0);
Dr. Stephen Henson06f45361999-09-20 22:09:17 +00001918#else
Ulf Möller7d7d2cb1999-05-13 11:37:32 +00001919 i=select(width,(void *)&readfds,(void *)&writefds,
Dr. Stephen Hensonb972fba2009-08-12 13:19:54 +00001920 NULL,timeoutp);
Dr. Stephen Henson06f45361999-09-20 22:09:17 +00001921#endif
Bodo Möllerc7ac31e1999-04-09 20:54:25 +00001922 if ( i < 0)
1923 {
1924 BIO_printf(bio_err,"bad select %d\n",
Ralf S. Engelschall58964a41998-12-21 10:56:39 +00001925 get_last_socket_error());
Bodo Möllerc7ac31e1999-04-09 20:54:25 +00001926 goto shut;
1927 /* goto end; */
1928 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001929 }
1930
Dr. Stephen Hensonb972fba2009-08-12 13:19:54 +00001931 if ((SSL_version(con) == DTLS1_VERSION) && DTLSv1_handle_timeout(con) > 0)
1932 {
Veres Lajos478b50c2013-06-13 00:22:32 +01001933 BIO_printf(bio_err,"TIMEOUT occurred\n");
Dr. Stephen Hensonb972fba2009-08-12 13:19:54 +00001934 }
1935
Bodo Möllerc7ac31e1999-04-09 20:54:25 +00001936 if (!ssl_pending && FD_ISSET(SSL_get_fd(con),&writefds))
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001937 {
1938 k=SSL_write(con,&(cbuf[cbuf_off]),
1939 (unsigned int)cbuf_len);
1940 switch (SSL_get_error(con,k))
1941 {
1942 case SSL_ERROR_NONE:
1943 cbuf_off+=k;
1944 cbuf_len-=k;
1945 if (k <= 0) goto end;
1946 /* we have done a write(con,NULL,0); */
1947 if (cbuf_len <= 0)
1948 {
1949 read_tty=1;
1950 write_ssl=0;
1951 }
1952 else /* if (cbuf_len > 0) */
1953 {
1954 read_tty=0;
1955 write_ssl=1;
1956 }
1957 break;
1958 case SSL_ERROR_WANT_WRITE:
1959 BIO_printf(bio_c_out,"write W BLOCK\n");
1960 write_ssl=1;
1961 read_tty=0;
1962 break;
1963 case SSL_ERROR_WANT_READ:
1964 BIO_printf(bio_c_out,"write R BLOCK\n");
1965 write_tty=0;
1966 read_ssl=1;
1967 write_ssl=0;
1968 break;
1969 case SSL_ERROR_WANT_X509_LOOKUP:
1970 BIO_printf(bio_c_out,"write X BLOCK\n");
1971 break;
1972 case SSL_ERROR_ZERO_RETURN:
1973 if (cbuf_len != 0)
1974 {
1975 BIO_printf(bio_c_out,"shutdown\n");
Dr. Stephen Henson0e1dba92007-10-26 12:06:36 +00001976 ret = 0;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001977 goto shut;
1978 }
1979 else
1980 {
1981 read_tty=1;
1982 write_ssl=0;
1983 break;
1984 }
1985
1986 case SSL_ERROR_SYSCALL:
1987 if ((k != 0) || (cbuf_len != 0))
1988 {
1989 BIO_printf(bio_err,"write:errno=%d\n",
Ralf S. Engelschall58964a41998-12-21 10:56:39 +00001990 get_last_socket_error());
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001991 goto shut;
1992 }
1993 else
1994 {
1995 read_tty=1;
1996 write_ssl=0;
1997 }
1998 break;
1999 case SSL_ERROR_SSL:
2000 ERR_print_errors(bio_err);
2001 goto shut;
2002 }
2003 }
Ulf Möller4700aea2006-04-11 21:34:21 +00002004#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS_R5)
2005 /* Assume Windows/DOS/BeOS can always write */
Dr. Stephen Henson06f45361999-09-20 22:09:17 +00002006 else if (!ssl_pending && write_tty)
2007#else
Bodo Möllerc7ac31e1999-04-09 20:54:25 +00002008 else if (!ssl_pending && FD_ISSET(fileno(stdout),&writefds))
Dr. Stephen Henson06f45361999-09-20 22:09:17 +00002009#endif
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002010 {
Ulf Möllera53955d1999-06-04 21:35:58 +00002011#ifdef CHARSET_EBCDIC
2012 ascii2ebcdic(&(sbuf[sbuf_off]),&(sbuf[sbuf_off]),sbuf_len);
2013#endif
Andy Polyakovffa10182005-11-04 09:30:55 +00002014 i=raw_write_stdout(&(sbuf[sbuf_off]),sbuf_len);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002015
2016 if (i <= 0)
2017 {
2018 BIO_printf(bio_c_out,"DONE\n");
Dr. Stephen Henson0e1dba92007-10-26 12:06:36 +00002019 ret = 0;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002020 goto shut;
2021 /* goto end; */
2022 }
2023
2024 sbuf_len-=i;;
2025 sbuf_off+=i;
2026 if (sbuf_len <= 0)
2027 {
2028 read_ssl=1;
2029 write_tty=0;
2030 }
2031 }
Bodo Möllerc7ac31e1999-04-09 20:54:25 +00002032 else if (ssl_pending || FD_ISSET(SSL_get_fd(con),&readfds))
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002033 {
Ralf S. Engelschall58964a41998-12-21 10:56:39 +00002034#ifdef RENEG
2035{ static int iiii; if (++iiii == 52) { SSL_renegotiate(con); iiii=0; } }
2036#endif
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +00002037#if 1
Ralf S. Engelschall58964a41998-12-21 10:56:39 +00002038 k=SSL_read(con,sbuf,1024 /* BUFSIZZ */ );
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +00002039#else
2040/* Demo for pending and peek :-) */
2041 k=SSL_read(con,sbuf,16);
2042{ char zbuf[10240];
2043printf("read=%d pending=%d peek=%d\n",k,SSL_pending(con),SSL_peek(con,zbuf,10240));
2044}
2045#endif
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002046
2047 switch (SSL_get_error(con,k))
2048 {
2049 case SSL_ERROR_NONE:
2050 if (k <= 0)
2051 goto end;
2052 sbuf_off=0;
2053 sbuf_len=k;
2054
2055 read_ssl=0;
2056 write_tty=1;
2057 break;
2058 case SSL_ERROR_WANT_WRITE:
2059 BIO_printf(bio_c_out,"read W BLOCK\n");
2060 write_ssl=1;
2061 read_tty=0;
2062 break;
2063 case SSL_ERROR_WANT_READ:
2064 BIO_printf(bio_c_out,"read R BLOCK\n");
2065 write_tty=0;
2066 read_ssl=1;
2067 if ((read_tty == 0) && (write_ssl == 0))
2068 write_ssl=1;
2069 break;
2070 case SSL_ERROR_WANT_X509_LOOKUP:
2071 BIO_printf(bio_c_out,"read X BLOCK\n");
2072 break;
2073 case SSL_ERROR_SYSCALL:
Dr. Stephen Henson0e1dba92007-10-26 12:06:36 +00002074 ret=get_last_socket_error();
Dr. Stephen Henson2537d462012-12-03 03:40:57 +00002075 if (c_brief)
Dr. Stephen Henson66d9f2e2012-12-03 03:33:44 +00002076 BIO_puts(bio_err, "CONNECTION CLOSED BY SERVER\n");
2077 else
2078 BIO_printf(bio_err,"read:errno=%d\n",ret);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002079 goto shut;
2080 case SSL_ERROR_ZERO_RETURN:
2081 BIO_printf(bio_c_out,"closed\n");
Dr. Stephen Henson0e1dba92007-10-26 12:06:36 +00002082 ret=0;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002083 goto shut;
2084 case SSL_ERROR_SSL:
2085 ERR_print_errors(bio_err);
2086 goto shut;
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +00002087 /* break; */
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002088 }
2089 }
2090
Richard Levitte3d7c4a52003-09-27 21:56:08 +00002091#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
2092#if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)
Richard Levitte0bf23d92002-11-15 22:37:18 +00002093 else if (_kbhit())
2094#else
Richard Levittea9ef75c2000-06-01 11:23:20 +00002095 else if ((_kbhit()) || (WAIT_OBJECT_0 == WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0)))
Richard Levitte0bf23d92002-11-15 22:37:18 +00002096#endif
Richard Levitte4d8743f2003-11-28 13:10:58 +00002097#elif defined (OPENSSL_SYS_NETWARE)
Andy Polyakovffa10182005-11-04 09:30:55 +00002098 else if (_kbhit())
Ulf Möller4700aea2006-04-11 21:34:21 +00002099#elif defined(OPENSSL_SYS_BEOS_R5)
2100 else if (stdin_set)
Dr. Stephen Henson06f45361999-09-20 22:09:17 +00002101#else
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002102 else if (FD_ISSET(fileno(stdin),&readfds))
Dr. Stephen Henson06f45361999-09-20 22:09:17 +00002103#endif
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002104 {
Bodo Möller1bdb8631999-08-07 02:51:10 +00002105 if (crlf)
2106 {
2107 int j, lf_num;
2108
Andy Polyakovffa10182005-11-04 09:30:55 +00002109 i=raw_read_stdin(cbuf,BUFSIZZ/2);
Bodo Möller1bdb8631999-08-07 02:51:10 +00002110 lf_num = 0;
2111 /* both loops are skipped when i <= 0 */
2112 for (j = 0; j < i; j++)
2113 if (cbuf[j] == '\n')
2114 lf_num++;
2115 for (j = i-1; j >= 0; j--)
2116 {
2117 cbuf[j+lf_num] = cbuf[j];
2118 if (cbuf[j] == '\n')
2119 {
2120 lf_num--;
2121 i++;
2122 cbuf[j+lf_num] = '\r';
2123 }
2124 }
2125 assert(lf_num == 0);
2126 }
2127 else
Andy Polyakovffa10182005-11-04 09:30:55 +00002128 i=raw_read_stdin(cbuf,BUFSIZZ);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002129
Richard Levittece301b62000-03-10 12:18:28 +00002130 if ((!c_ign_eof) && ((i <= 0) || (cbuf[0] == 'Q')))
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002131 {
2132 BIO_printf(bio_err,"DONE\n");
Dr. Stephen Henson0e1dba92007-10-26 12:06:36 +00002133 ret=0;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002134 goto shut;
2135 }
2136
Richard Levittece301b62000-03-10 12:18:28 +00002137 if ((!c_ign_eof) && (cbuf[0] == 'R'))
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002138 {
Ben Laurie3bb307c1999-04-10 12:08:46 +00002139 BIO_printf(bio_err,"RENEGOTIATING\n");
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002140 SSL_renegotiate(con);
Ben Laurie3bb307c1999-04-10 12:08:46 +00002141 cbuf_len=0;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002142 }
Dr. Stephen Henson48175042011-12-31 22:59:57 +00002143#ifndef OPENSSL_NO_HEARTBEATS
2144 else if ((!c_ign_eof) && (cbuf[0] == 'B'))
2145 {
2146 BIO_printf(bio_err,"HEARTBEATING\n");
2147 SSL_heartbeat(con);
2148 cbuf_len=0;
2149 }
2150#endif
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002151 else
2152 {
2153 cbuf_len=i;
2154 cbuf_off=0;
Ulf Möllera53955d1999-06-04 21:35:58 +00002155#ifdef CHARSET_EBCDIC
2156 ebcdic2ascii(cbuf, cbuf, i);
2157#endif
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002158 }
2159
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002160 write_ssl=1;
Ben Laurie3bb307c1999-04-10 12:08:46 +00002161 read_tty=0;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002162 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002163 }
Dr. Stephen Henson0e1dba92007-10-26 12:06:36 +00002164
2165 ret=0;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002166shut:
Bodo Möllerb166f132006-06-15 19:00:34 +00002167 if (in_init)
2168 print_stuff(bio_c_out,con,full_log);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002169 SSL_shutdown(con);
2170 SHUTDOWN(SSL_get_fd(con));
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002171end:
Nils Larschd916ba12006-03-18 14:24:02 +00002172 if (con != NULL)
2173 {
2174 if (prexit != 0)
2175 print_stuff(bio_c_out,con,1);
2176 SSL_free(con);
2177 }
Dr. Stephen Hensondd251652012-07-03 16:37:50 +00002178#if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
2179 if (next_proto.data)
2180 OPENSSL_free(next_proto.data);
2181#endif
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002182 if (ctx != NULL) SSL_CTX_free(ctx);
Dr. Stephen Henson826a42a2004-11-16 17:30:59 +00002183 if (cert)
2184 X509_free(cert);
Dr. Stephen Hensonfdb78f32012-12-02 16:16:28 +00002185 if (crls)
2186 sk_X509_CRL_pop_free(crls, X509_CRL_free);
Dr. Stephen Henson826a42a2004-11-16 17:30:59 +00002187 if (key)
2188 EVP_PKEY_free(key);
Dr. Stephen Henson4e71d952012-12-12 00:50:26 +00002189 if (chain)
2190 sk_X509_pop_free(chain, X509_free);
Dr. Stephen Henson826a42a2004-11-16 17:30:59 +00002191 if (pass)
2192 OPENSSL_free(pass);
Dr. Stephen Henson22b5d7c2012-11-20 00:24:52 +00002193 if (vpm)
2194 X509_VERIFY_PARAM_free(vpm);
Dr. Stephen Henson3208fc52012-07-03 14:53:27 +00002195 ssl_excert_free(exc);
Dr. Stephen Henson5d2e07f2012-11-17 14:42:22 +00002196 if (ssl_args)
2197 sk_OPENSSL_STRING_free(ssl_args);
2198 if (cctx)
2199 SSL_CONF_CTX_free(cctx);
Dr. Stephen Hensonb252cf02012-12-29 23:38:20 +00002200#ifndef OPENSSL_NO_JPAKE
2201 if (jpake_secret && psk_key)
2202 OPENSSL_free(psk_key);
2203#endif
Richard Levitte45799242002-11-28 08:04:36 +00002204 if (cbuf != NULL) { OPENSSL_cleanse(cbuf,BUFSIZZ); OPENSSL_free(cbuf); }
2205 if (sbuf != NULL) { OPENSSL_cleanse(sbuf,BUFSIZZ); OPENSSL_free(sbuf); }
2206 if (mbuf != NULL) { OPENSSL_cleanse(mbuf,BUFSIZZ); OPENSSL_free(mbuf); }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002207 if (bio_c_out != NULL)
2208 {
2209 BIO_free(bio_c_out);
2210 bio_c_out=NULL;
2211 }
Dr. Stephen Henson93ab9e42012-06-15 12:46:09 +00002212 if (bio_c_msg != NULL)
2213 {
2214 BIO_free(bio_c_msg);
2215 bio_c_msg=NULL;
2216 }
Richard Levittec04f8cf2001-06-23 16:37:32 +00002217 apps_shutdown();
Richard Levitte1c3e4a32002-12-03 16:33:03 +00002218 OPENSSL_EXIT(ret);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002219 }
2220
2221
Ulf Möller6b691a51999-04-19 21:31:43 +00002222static void print_stuff(BIO *bio, SSL *s, int full)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002223 {
Ralf S. Engelschall58964a41998-12-21 10:56:39 +00002224 X509 *peer=NULL;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002225 char *p;
Nils Larsch7d727232005-04-05 19:11:19 +00002226 static const char *space=" ";
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002227 char buf[BUFSIZ];
Ben Laurief73e07c1999-04-12 17:23:57 +00002228 STACK_OF(X509) *sk;
2229 STACK_OF(X509_NAME) *sk2;
Ben Lauriebabb3792008-10-12 14:32:47 +00002230 const SSL_CIPHER *c;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002231 X509_NAME *xn;
2232 int j,i;
Dr. Stephen Henson09b6c2e2005-09-30 23:35:33 +00002233#ifndef OPENSSL_NO_COMP
Geoff Thorped8ec0dc2003-11-04 00:51:32 +00002234 const COMP_METHOD *comp, *expansion;
Dr. Stephen Henson09b6c2e2005-09-30 23:35:33 +00002235#endif
Ben Lauriee0af0402011-11-15 23:50:52 +00002236 unsigned char *exportedkeymat;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002237
2238 if (full)
2239 {
Bodo Möllerbc2e5191999-05-17 20:46:43 +00002240 int got_a_chain = 0;
2241
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002242 sk=SSL_get_peer_cert_chain(s);
2243 if (sk != NULL)
2244 {
Bodo Möllerbc2e5191999-05-17 20:46:43 +00002245 got_a_chain = 1; /* we don't have it for SSL2 (yet) */
2246
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +00002247 BIO_printf(bio,"---\nCertificate chain\n");
Ben Laurief73e07c1999-04-12 17:23:57 +00002248 for (i=0; i<sk_X509_num(sk); i++)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002249 {
Ben Laurief73e07c1999-04-12 17:23:57 +00002250 X509_NAME_oneline(X509_get_subject_name(
Ben Laurie54a656e2002-11-13 15:43:43 +00002251 sk_X509_value(sk,i)),buf,sizeof buf);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002252 BIO_printf(bio,"%2d s:%s\n",i,buf);
Ben Laurief73e07c1999-04-12 17:23:57 +00002253 X509_NAME_oneline(X509_get_issuer_name(
Ben Laurie54a656e2002-11-13 15:43:43 +00002254 sk_X509_value(sk,i)),buf,sizeof buf);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002255 BIO_printf(bio," i:%s\n",buf);
Bodo Möller6d02d8e1999-03-31 12:06:30 +00002256 if (c_showcerts)
Ben Laurief73e07c1999-04-12 17:23:57 +00002257 PEM_write_bio_X509(bio,sk_X509_value(sk,i));
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002258 }
2259 }
2260
2261 BIO_printf(bio,"---\n");
2262 peer=SSL_get_peer_certificate(s);
2263 if (peer != NULL)
2264 {
2265 BIO_printf(bio,"Server certificate\n");
Bodo Möllerbc2e5191999-05-17 20:46:43 +00002266 if (!(c_showcerts && got_a_chain)) /* Redundant if we showed the whole chain */
Bodo Möller6d02d8e1999-03-31 12:06:30 +00002267 PEM_write_bio_X509(bio,peer);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002268 X509_NAME_oneline(X509_get_subject_name(peer),
Ben Laurie54a656e2002-11-13 15:43:43 +00002269 buf,sizeof buf);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002270 BIO_printf(bio,"subject=%s\n",buf);
2271 X509_NAME_oneline(X509_get_issuer_name(peer),
Ben Laurie54a656e2002-11-13 15:43:43 +00002272 buf,sizeof buf);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002273 BIO_printf(bio,"issuer=%s\n",buf);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002274 }
2275 else
2276 BIO_printf(bio,"no peer certificate available\n");
2277
Ben Laurief73e07c1999-04-12 17:23:57 +00002278 sk2=SSL_get_client_CA_list(s);
Bodo Möllerd91f8c31999-04-14 23:10:11 +00002279 if ((sk2 != NULL) && (sk_X509_NAME_num(sk2) > 0))
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002280 {
2281 BIO_printf(bio,"---\nAcceptable client certificate CA names\n");
Ben Laurief73e07c1999-04-12 17:23:57 +00002282 for (i=0; i<sk_X509_NAME_num(sk2); i++)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002283 {
Ben Laurief73e07c1999-04-12 17:23:57 +00002284 xn=sk_X509_NAME_value(sk2,i);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002285 X509_NAME_oneline(xn,buf,sizeof(buf));
2286 BIO_write(bio,buf,strlen(buf));
2287 BIO_write(bio,"\n",1);
2288 }
2289 }
2290 else
2291 {
2292 BIO_printf(bio,"---\nNo client certificate CA names sent\n");
2293 }
Ben Laurie54a656e2002-11-13 15:43:43 +00002294 p=SSL_get_shared_ciphers(s,buf,sizeof buf);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002295 if (p != NULL)
2296 {
Bodo Möller67a47281999-04-27 16:48:06 +00002297 /* This works only for SSL 2. In later protocol
2298 * versions, the client does not know what other
2299 * ciphers (in addition to the one to be used
2300 * in the current connection) the server supports. */
2301
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002302 BIO_printf(bio,"---\nCiphers common between both SSL endpoints:\n");
2303 j=i=0;
2304 while (*p)
2305 {
2306 if (*p == ':')
2307 {
Ralf S. Engelschall58964a41998-12-21 10:56:39 +00002308 BIO_write(bio,space,15-j%25);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002309 i++;
2310 j=0;
2311 BIO_write(bio,((i%3)?" ":"\n"),1);
2312 }
2313 else
2314 {
2315 BIO_write(bio,p,1);
2316 j++;
2317 }
2318 p++;
2319 }
2320 BIO_write(bio,"\n",1);
2321 }
2322
Dr. Stephen Henson9f27b1e2012-07-08 14:22:45 +00002323 ssl_print_sigalgs(bio, s);
Dr. Stephen Henson33a8de62012-09-08 13:59:51 +00002324 ssl_print_tmp_key(bio, s);
Dr. Stephen Hensone7f8ff42012-03-06 14:28:21 +00002325
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002326 BIO_printf(bio,"---\nSSL handshake has read %ld bytes and written %ld bytes\n",
2327 BIO_number_read(SSL_get_rbio(s)),
2328 BIO_number_written(SSL_get_wbio(s)));
2329 }
Dr. Stephen Henson08557cf2011-04-29 22:37:12 +00002330 BIO_printf(bio,(SSL_cache_hit(s)?"---\nReused, ":"---\nNew, "));
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002331 c=SSL_get_current_cipher(s);
2332 BIO_printf(bio,"%s, Cipher is %s\n",
2333 SSL_CIPHER_get_version(c),
2334 SSL_CIPHER_get_name(c));
Dr. Stephen Hensona8236c81999-02-15 21:05:21 +00002335 if (peer != NULL) {
2336 EVP_PKEY *pktmp;
2337 pktmp = X509_get_pubkey(peer);
Ralf S. Engelschall58964a41998-12-21 10:56:39 +00002338 BIO_printf(bio,"Server public key is %d bit\n",
Dr. Stephen Hensona8236c81999-02-15 21:05:21 +00002339 EVP_PKEY_bits(pktmp));
2340 EVP_PKEY_free(pktmp);
2341 }
Dr. Stephen Henson54302002009-12-08 13:42:08 +00002342 BIO_printf(bio, "Secure Renegotiation IS%s supported\n",
2343 SSL_get_secure_renegotiation_support(s) ? "" : " NOT");
Dr. Stephen Henson09b6c2e2005-09-30 23:35:33 +00002344#ifndef OPENSSL_NO_COMP
Richard Levittef44e1842003-10-06 12:19:38 +00002345 comp=SSL_get_current_compression(s);
Geoff Thorped8ec0dc2003-11-04 00:51:32 +00002346 expansion=SSL_get_current_expansion(s);
Richard Levittef44e1842003-10-06 12:19:38 +00002347 BIO_printf(bio,"Compression: %s\n",
2348 comp ? SSL_COMP_get_name(comp) : "NONE");
2349 BIO_printf(bio,"Expansion: %s\n",
Geoff Thorped8ec0dc2003-11-04 00:51:32 +00002350 expansion ? SSL_COMP_get_name(expansion) : "NONE");
Dr. Stephen Henson09b6c2e2005-09-30 23:35:33 +00002351#endif
Ben Laurie71fa4512012-06-03 22:00:21 +00002352
Dr. Stephen Henson57559472012-02-09 15:43:58 +00002353#ifdef SSL_DEBUG
Dr. Stephen Hensona2f92002011-05-09 15:44:01 +00002354 {
2355 /* Print out local port of connection: useful for debugging */
2356 int sock;
2357 struct sockaddr_in ladd;
2358 socklen_t ladd_size = sizeof(ladd);
2359 sock = SSL_get_fd(s);
2360 getsockname(sock, (struct sockaddr *)&ladd, &ladd_size);
2361 BIO_printf(bio_c_out, "LOCAL PORT is %u\n", ntohs(ladd.sin_port));
2362 }
2363#endif
2364
Adam Langley6f017a82013-04-15 18:07:47 -04002365#if !defined(OPENSSL_NO_TLSEXT)
2366# if !defined(OPENSSL_NO_NEXTPROTONEG)
Ben Laurie71fa4512012-06-03 22:00:21 +00002367 if (next_proto.status != -1) {
2368 const unsigned char *proto;
2369 unsigned int proto_len;
2370 SSL_get0_next_proto_negotiated(s, &proto, &proto_len);
2371 BIO_printf(bio, "Next protocol: (%d) ", next_proto.status);
2372 BIO_write(bio, proto, proto_len);
2373 BIO_write(bio, "\n", 1);
2374 }
Piotr Sikora29115752013-11-13 15:20:22 -08002375# endif
Adam Langley6f017a82013-04-15 18:07:47 -04002376 {
2377 const unsigned char *proto;
2378 unsigned int proto_len;
2379 SSL_get0_alpn_selected(s, &proto, &proto_len);
2380 if (proto_len > 0)
2381 {
2382 BIO_printf(bio, "ALPN protocol: ");
2383 BIO_write(bio, proto, proto_len);
2384 BIO_write(bio, "\n", 1);
2385 }
2386 else
2387 BIO_printf(bio, "No ALPN negotiated\n");
2388 }
Ben Laurie71fa4512012-06-03 22:00:21 +00002389#endif
2390
Ben Laurie333f9262011-11-15 22:59:20 +00002391 {
2392 SRTP_PROTECTION_PROFILE *srtp_profile=SSL_get_selected_srtp_profile(s);
2393
2394 if(srtp_profile)
2395 BIO_printf(bio,"SRTP Extension negotiated, profile=%s\n",
2396 srtp_profile->name);
2397 }
2398
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002399 SSL_SESSION_print(bio,SSL_get_session(s));
Dr. Stephen Hensonbe81f4d2012-02-11 23:20:53 +00002400 if (keymatexportlabel != NULL)
2401 {
Ben Lauriee0af0402011-11-15 23:50:52 +00002402 BIO_printf(bio, "Keying material exporter:\n");
2403 BIO_printf(bio, " Label: '%s'\n", keymatexportlabel);
2404 BIO_printf(bio, " Length: %i bytes\n", keymatexportlen);
2405 exportedkeymat = OPENSSL_malloc(keymatexportlen);
Dr. Stephen Hensonbe81f4d2012-02-11 23:20:53 +00002406 if (exportedkeymat != NULL)
2407 {
2408 if (!SSL_export_keying_material(s, exportedkeymat,
2409 keymatexportlen,
2410 keymatexportlabel,
2411 strlen(keymatexportlabel),
2412 NULL, 0, 0))
2413 {
2414 BIO_printf(bio, " Error\n");
2415 }
2416 else
2417 {
Ben Lauriee0af0402011-11-15 23:50:52 +00002418 BIO_printf(bio, " Keying material: ");
2419 for (i=0; i<keymatexportlen; i++)
2420 BIO_printf(bio, "%02X",
2421 exportedkeymat[i]);
2422 BIO_printf(bio, "\n");
Dr. Stephen Hensonbe81f4d2012-02-11 23:20:53 +00002423 }
Ben Lauriee0af0402011-11-15 23:50:52 +00002424 OPENSSL_free(exportedkeymat);
Dr. Stephen Hensonbe81f4d2012-02-11 23:20:53 +00002425 }
Ben Lauriee0af0402011-11-15 23:50:52 +00002426 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002427 BIO_printf(bio,"---\n");
Ralf S. Engelschall58964a41998-12-21 10:56:39 +00002428 if (peer != NULL)
2429 X509_free(peer);
Lutz Jänicke41ebed22001-10-16 14:24:46 +00002430 /* flush, or debugging output gets mixed with http response */
Dr. Stephen Henson710069c2007-08-12 17:44:32 +00002431 (void)BIO_flush(bio);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002432 }
2433
Dr. Stephen Henson07021502008-09-03 12:29:57 +00002434#ifndef OPENSSL_NO_TLSEXT
2435
Dr. Stephen Henson67c8e7f2007-09-26 21:56:59 +00002436static int ocsp_resp_cb(SSL *s, void *arg)
2437 {
2438 const unsigned char *p;
2439 int len;
2440 OCSP_RESPONSE *rsp;
2441 len = SSL_get_tlsext_status_ocsp_resp(s, &p);
2442 BIO_puts(arg, "OCSP response: ");
2443 if (!p)
2444 {
2445 BIO_puts(arg, "no response sent\n");
2446 return 1;
2447 }
2448 rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
2449 if (!rsp)
2450 {
2451 BIO_puts(arg, "response parse error\n");
2452 BIO_dump_indent(arg, (char *)p, len, 4);
2453 return 0;
2454 }
2455 BIO_puts(arg, "\n======================================\n");
2456 OCSP_RESPONSE_print(arg, rsp, 0);
2457 BIO_puts(arg, "======================================\n");
2458 OCSP_RESPONSE_free(rsp);
2459 return 1;
2460 }
Dr. Stephen Henson07021502008-09-03 12:29:57 +00002461
Scott Deboy36086182013-06-18 14:34:38 -07002462static int authz_tlsext_cb(SSL *s, unsigned short ext_type,
Ben Laurie0a602872014-02-04 23:16:46 +00002463 const unsigned char *in,
2464 unsigned short inlen, int *al,
2465 void *arg)
Ben Lauriea9e1c502012-05-30 10:10:58 +00002466 {
Scott Deboy36086182013-06-18 14:34:38 -07002467 if (TLSEXT_TYPE_server_authz == ext_type)
Ben Laurie5eda2132013-08-01 12:33:15 +01002468 server_provided_server_authz
2469 = (memchr(in, TLSEXT_AUTHZDATAFORMAT_dtcp, inlen) != NULL);
Scott Deboy36086182013-06-18 14:34:38 -07002470
2471 if (TLSEXT_TYPE_client_authz == ext_type)
Ben Laurie5eda2132013-08-01 12:33:15 +01002472 server_provided_client_authz
2473 = (memchr(in, TLSEXT_AUTHZDATAFORMAT_dtcp, inlen) != NULL);
Scott Deboy36086182013-06-18 14:34:38 -07002474
2475 return 1;
2476 }
2477
2478static int authz_tlsext_generate_cb(SSL *s, unsigned short ext_type,
Ben Laurie0a602872014-02-04 23:16:46 +00002479 const unsigned char **out, unsigned short *outlen,
2480 int *al, void *arg)
Scott Deboy36086182013-06-18 14:34:38 -07002481 {
2482 if (c_auth)
2483 {
Scott Deboy67c408c2013-08-01 11:54:09 -07002484 /*if auth_require_reneg flag is set, only send extensions if
2485 renegotiation has occurred */
Scott Deboy36086182013-06-18 14:34:38 -07002486 if (!c_auth_require_reneg || (c_auth_require_reneg && SSL_num_renegotiations(s)))
2487 {
2488 *out = auth_ext_data;
2489 *outlen = 1;
2490 return 1;
2491 }
2492 }
Ben Lauriea6a48e82013-08-01 11:14:23 +01002493 /* no auth extension to send */
Scott Deboy36086182013-06-18 14:34:38 -07002494 return -1;
2495 }
2496
2497static int suppdata_cb(SSL *s, unsigned short supp_data_type,
Ben Laurie0a602872014-02-04 23:16:46 +00002498 const unsigned char *in,
2499 unsigned short inlen, int *al,
2500 void *arg)
Scott Deboy36086182013-06-18 14:34:38 -07002501 {
2502 if (supp_data_type == TLSEXT_SUPPLEMENTALDATATYPE_authz_data)
2503 {
2504 most_recent_supplemental_data = in;
2505 most_recent_supplemental_data_length = inlen;
Ben Lauriea9e1c502012-05-30 10:10:58 +00002506 }
2507 return 1;
2508 }
Scott Deboy36086182013-06-18 14:34:38 -07002509
2510static int auth_suppdata_generate_cb(SSL *s, unsigned short supp_data_type,
Ben Laurie0a602872014-02-04 23:16:46 +00002511 const unsigned char **out,
2512 unsigned short *outlen, int *al, void *arg)
Scott Deboy36086182013-06-18 14:34:38 -07002513 {
Scott Deboy36086182013-06-18 14:34:38 -07002514 if (c_auth && server_provided_client_authz && server_provided_server_authz)
2515 {
Scott Deboy67c408c2013-08-01 11:54:09 -07002516 /*if auth_require_reneg flag is set, only send supplemental data if
2517 renegotiation has occurred */
Ben Laurie5eda2132013-08-01 12:33:15 +01002518 if (!c_auth_require_reneg
2519 || (c_auth_require_reneg && SSL_num_renegotiations(s)))
Scott Deboy36086182013-06-18 14:34:38 -07002520 {
Scott Deboy67c408c2013-08-01 11:54:09 -07002521 generated_supp_data = OPENSSL_malloc(10);
2522 memcpy(generated_supp_data, "5432154321", 10);
2523 *out = generated_supp_data;
Scott Deboy36086182013-06-18 14:34:38 -07002524 *outlen = 10;
2525 return 1;
2526 }
2527 }
Ben Lauriea6a48e82013-08-01 11:14:23 +01002528 /* no supplemental data to send */
Scott Deboy36086182013-06-18 14:34:38 -07002529 return -1;
2530 }
2531
Dr. Stephen Henson07021502008-09-03 12:29:57 +00002532#endif