blob: 6ed90f79bf41d683db0caa3cce81334635c2fd5e [file] [log] [blame]
Kinson Chika8fa74c2011-07-29 11:33:41 -07001/* apps/s_client.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58/* ====================================================================
59 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 *
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 *
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
71 * distribution.
72 *
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 *
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
82 *
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
86 *
87 * 6. Redistributions of any form whatsoever must retain the following
88 * acknowledgment:
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 *
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
105 *
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
109 *
110 */
111/* ====================================================================
112 * Copyright 2005 Nokia. All rights reserved.
113 *
114 * The portions of the attached software ("Contribution") is developed by
115 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
116 * license.
117 *
118 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
119 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
120 * support (see RFC 4279) to OpenSSL.
121 *
122 * No patent licenses or other rights except those expressly stated in
123 * the OpenSSL open source license shall be deemed granted or received
124 * expressly, by implication, estoppel, or otherwise.
125 *
126 * No assurances are provided by Nokia that the Contribution does not
127 * infringe the patent or other intellectual property rights of any third
128 * party or that the license provides you with all the necessary rights
129 * to make use of the Contribution.
130 *
131 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
132 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
133 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
134 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
135 * OTHERWISE.
136 */
137
138#include <assert.h>
139#include <ctype.h>
140#include <stdio.h>
141#include <stdlib.h>
142#include <string.h>
143#include <openssl/e_os2.h>
144#ifdef OPENSSL_NO_STDIO
145#define APPS_WIN16
146#endif
147
148/* 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 */
152#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
153#define __U_INT
154typedef unsigned int u_int;
155#endif
156
157#define USE_SOCKETS
158#include "apps.h"
159#include <openssl/x509.h>
160#include <openssl/ssl.h>
161#include <openssl/err.h>
162#include <openssl/pem.h>
163#include <openssl/rand.h>
164#include <openssl/ocsp.h>
165#include <openssl/bn.h>
166#include "s_apps.h"
167#include "timeouts.h"
168
169#if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
170/* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
171#undef FIONBIO
172#endif
173
174#if defined(OPENSSL_SYS_BEOS_R5)
175#include <fcntl.h>
176#endif
177
178#undef PROG
179#define PROG s_client_main
180
181/*#define SSL_HOST_NAME "www.netscape.com" */
182/*#define SSL_HOST_NAME "193.118.187.102" */
183#define SSL_HOST_NAME "localhost"
184
185/*#define TEST_CERT "client.pem" */ /* no default cert. */
186
187#undef BUFSIZZ
188#define BUFSIZZ 1024*8
189
190extern int verify_depth;
191extern int verify_error;
192extern int verify_return_error;
193
194#ifdef FIONBIO
195static int c_nbio=0;
196#endif
197static int c_Pause=0;
198static int c_debug=0;
199#ifndef OPENSSL_NO_TLSEXT
200static int c_tlsextdebug=0;
201static int c_status_req=0;
202#endif
203static int c_msg=0;
204static int c_showcerts=0;
205
206static void sc_usage(void);
207static void print_stuff(BIO *berr,SSL *con,int full);
208#ifndef OPENSSL_NO_TLSEXT
209static int ocsp_resp_cb(SSL *s, void *arg);
210#endif
211static BIO *bio_c_out=NULL;
212static int c_quiet=0;
213static int c_ign_eof=0;
214
215#ifndef OPENSSL_NO_PSK
216/* Default PSK identity and key */
217static char *psk_identity="Client_identity";
218/*char *psk_key=NULL; by default PSK is not used */
219
220static unsigned int psk_client_cb(SSL *ssl, const char *hint, char *identity,
221 unsigned int max_identity_len, unsigned char *psk,
222 unsigned int max_psk_len)
223 {
224 unsigned int psk_len = 0;
225 int ret;
226 BIGNUM *bn=NULL;
227
228 if (c_debug)
229 BIO_printf(bio_c_out, "psk_client_cb\n");
230 if (!hint)
231 {
232 /* no ServerKeyExchange message*/
233 if (c_debug)
234 BIO_printf(bio_c_out,"NULL received PSK identity hint, continuing anyway\n");
235 }
236 else if (c_debug)
237 BIO_printf(bio_c_out, "Received PSK identity hint '%s'\n", hint);
238
239 /* lookup PSK identity and PSK key based on the given identity hint here */
240 ret = BIO_snprintf(identity, max_identity_len, "%s", psk_identity);
241 if (ret < 0 || (unsigned int)ret > max_identity_len)
242 goto out_err;
243 if (c_debug)
244 BIO_printf(bio_c_out, "created identity '%s' len=%d\n", identity, ret);
245 ret=BN_hex2bn(&bn, psk_key);
246 if (!ret)
247 {
248 BIO_printf(bio_err,"Could not convert PSK key '%s' to BIGNUM\n", psk_key);
249 if (bn)
250 BN_free(bn);
251 return 0;
252 }
253
254 if ((unsigned int)BN_num_bytes(bn) > max_psk_len)
255 {
256 BIO_printf(bio_err,"psk buffer of callback is too small (%d) for key (%d)\n",
257 max_psk_len, BN_num_bytes(bn));
258 BN_free(bn);
259 return 0;
260 }
261
262 psk_len=BN_bn2bin(bn, psk);
263 BN_free(bn);
264 if (psk_len == 0)
265 goto out_err;
266
267 if (c_debug)
268 BIO_printf(bio_c_out, "created PSK len=%d\n", psk_len);
269
270 return psk_len;
271 out_err:
272 if (c_debug)
273 BIO_printf(bio_err, "Error in PSK client callback\n");
274 return 0;
275 }
276#endif
277
278static void sc_usage(void)
279 {
280 BIO_printf(bio_err,"usage: s_client args\n");
281 BIO_printf(bio_err,"\n");
282 BIO_printf(bio_err," -host host - use -connect instead\n");
283 BIO_printf(bio_err," -port port - use -connect instead\n");
284 BIO_printf(bio_err," -connect host:port - who to connect to (default is %s:%s)\n",SSL_HOST_NAME,PORT_STR);
285
286 BIO_printf(bio_err," -verify arg - turn on peer certificate verification\n");
287 BIO_printf(bio_err," -cert arg - certificate file to use, PEM format assumed\n");
288 BIO_printf(bio_err," -certform arg - certificate format (PEM or DER) PEM default\n");
289 BIO_printf(bio_err," -key arg - Private key file to use, in cert file if\n");
290 BIO_printf(bio_err," not specified but cert file is.\n");
291 BIO_printf(bio_err," -keyform arg - key format (PEM or DER) PEM default\n");
292 BIO_printf(bio_err," -pass arg - private key file pass phrase source\n");
293 BIO_printf(bio_err," -CApath arg - PEM format directory of CA's\n");
294 BIO_printf(bio_err," -CAfile arg - PEM format file of CA's\n");
295 BIO_printf(bio_err," -reconnect - Drop and re-make the connection with the same Session-ID\n");
296 BIO_printf(bio_err," -pause - sleep(1) after each read(2) and write(2) system call\n");
297 BIO_printf(bio_err," -showcerts - show all certificates in the chain\n");
298 BIO_printf(bio_err," -debug - extra output\n");
299#ifdef WATT32
300 BIO_printf(bio_err," -wdebug - WATT-32 tcp debugging\n");
301#endif
302 BIO_printf(bio_err," -msg - Show protocol messages\n");
303 BIO_printf(bio_err," -nbio_test - more ssl protocol testing\n");
304 BIO_printf(bio_err," -state - print the 'ssl' states\n");
305#ifdef FIONBIO
306 BIO_printf(bio_err," -nbio - Run with non-blocking IO\n");
307#endif
308 BIO_printf(bio_err," -crlf - convert LF from terminal into CRLF\n");
309 BIO_printf(bio_err," -quiet - no s_client output\n");
310 BIO_printf(bio_err," -ign_eof - ignore input eof (default when -quiet)\n");
311 BIO_printf(bio_err," -no_ign_eof - don't ignore input eof\n");
312#ifndef OPENSSL_NO_PSK
313 BIO_printf(bio_err," -psk_identity arg - PSK identity\n");
314 BIO_printf(bio_err," -psk arg - PSK in hex (without 0x)\n");
315# ifndef OPENSSL_NO_JPAKE
316 BIO_printf(bio_err," -jpake arg - JPAKE secret to use\n");
317# endif
318#endif
319 BIO_printf(bio_err," -ssl2 - just use SSLv2\n");
320 BIO_printf(bio_err," -ssl3 - just use SSLv3\n");
321 BIO_printf(bio_err," -tls1 - just use TLSv1\n");
322 BIO_printf(bio_err," -dtls1 - just use DTLSv1\n");
323 BIO_printf(bio_err," -mtu - set the link layer MTU\n");
324 BIO_printf(bio_err," -no_tls1/-no_ssl3/-no_ssl2 - turn off that protocol\n");
325 BIO_printf(bio_err," -bugs - Switch on all SSL implementation bug workarounds\n");
326 BIO_printf(bio_err," -serverpref - Use server's cipher preferences (only SSLv2)\n");
327 BIO_printf(bio_err," -cipher - preferred cipher to use, use the 'openssl ciphers'\n");
328 BIO_printf(bio_err," command to see what is available\n");
329 BIO_printf(bio_err," -starttls prot - use the STARTTLS command before starting TLS\n");
330 BIO_printf(bio_err," for those protocols that support it, where\n");
331 BIO_printf(bio_err," 'prot' defines which one to assume. Currently,\n");
332 BIO_printf(bio_err," only \"smtp\", \"pop3\", \"imap\", \"ftp\" and \"xmpp\"\n");
333 BIO_printf(bio_err," are supported.\n");
334#ifndef OPENSSL_NO_ENGINE
335 BIO_printf(bio_err," -engine id - Initialise and use the specified engine\n");
336#endif
337 BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
338 BIO_printf(bio_err," -sess_out arg - file to write SSL session to\n");
339 BIO_printf(bio_err," -sess_in arg - file to read SSL session from\n");
340#ifndef OPENSSL_NO_TLSEXT
341 BIO_printf(bio_err," -servername host - Set TLS extension servername in ClientHello\n");
342 BIO_printf(bio_err," -tlsextdebug - hex dump of all TLS extensions received\n");
343 BIO_printf(bio_err," -status - request certificate status from server\n");
344 BIO_printf(bio_err," -no_ticket - disable use of RFC4507bis session tickets\n");
345 BIO_printf(bio_err," -cutthrough - enable 1-RTT full-handshake for strong ciphers\n");
346#endif
347 BIO_printf(bio_err," -legacy_renegotiation - enable use of legacy renegotiation (dangerous)\n");
348 }
349
350#ifndef OPENSSL_NO_TLSEXT
351
352/* This is a context that we pass to callbacks */
353typedef struct tlsextctx_st {
354 BIO * biodebug;
355 int ack;
356} tlsextctx;
357
358
359static int MS_CALLBACK ssl_servername_cb(SSL *s, int *ad, void *arg)
360 {
361 tlsextctx * p = (tlsextctx *) arg;
362 const char * hn= SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
363 if (SSL_get_servername_type(s) != -1)
364 p->ack = !SSL_session_reused(s) && hn != NULL;
365 else
366 BIO_printf(bio_err,"Can't use SSL_get_servername\n");
367
368 return SSL_TLSEXT_ERR_OK;
369 }
370#endif
371
372enum
373{
374 PROTO_OFF = 0,
375 PROTO_SMTP,
376 PROTO_POP3,
377 PROTO_IMAP,
378 PROTO_FTP,
379 PROTO_XMPP
380};
381
382int MAIN(int, char **);
383
384int MAIN(int argc, char **argv)
385 {
386 unsigned int off=0, clr=0;
387 SSL *con=NULL;
388 int s,k,width,state=0;
389 char *cbuf=NULL,*sbuf=NULL,*mbuf=NULL;
390 int cbuf_len,cbuf_off;
391 int sbuf_len,sbuf_off;
392 fd_set readfds,writefds;
393 short port=PORT;
394 int full_log=1;
395 char *host=SSL_HOST_NAME;
396 char *cert_file=NULL,*key_file=NULL;
397 int cert_format = FORMAT_PEM, key_format = FORMAT_PEM;
398 char *passarg = NULL, *pass = NULL;
399 X509 *cert = NULL;
400 EVP_PKEY *key = NULL;
401 char *CApath=NULL,*CAfile=NULL,*cipher=NULL;
402 int reconnect=0,badop=0,verify=SSL_VERIFY_NONE,bugs=0;
403 int cutthrough=0;
404 int crlf=0;
405 int write_tty,read_tty,write_ssl,read_ssl,tty_on,ssl_pending;
406 SSL_CTX *ctx=NULL;
407 int ret=1,in_init=1,i,nbio_test=0;
408 int starttls_proto = PROTO_OFF;
409 int prexit = 0;
410 X509_VERIFY_PARAM *vpm = NULL;
411 int badarg = 0;
412 const SSL_METHOD *meth=NULL;
413 int socket_type=SOCK_STREAM;
414 BIO *sbio;
415 char *inrand=NULL;
416 int mbuf_len=0;
417 struct timeval timeout, *timeoutp;
418#ifndef OPENSSL_NO_ENGINE
419 char *engine_id=NULL;
420 char *ssl_client_engine_id=NULL;
421 ENGINE *ssl_client_engine=NULL;
422#endif
423 ENGINE *e=NULL;
424#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS_R5)
425 struct timeval tv;
426#if defined(OPENSSL_SYS_BEOS_R5)
427 int stdin_set = 0;
428#endif
429#endif
430#ifndef OPENSSL_NO_TLSEXT
431 char *servername = NULL;
432 tlsextctx tlsextcbp =
433 {NULL,0};
434#endif
435 char *sess_in = NULL;
436 char *sess_out = NULL;
437 struct sockaddr peer;
438 int peerlen = sizeof(peer);
439 int enable_timeouts = 0 ;
440 long socket_mtu = 0;
441#ifndef OPENSSL_NO_JPAKE
442 char *jpake_secret = NULL;
443#endif
444
445#if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
446 meth=SSLv23_client_method();
447#elif !defined(OPENSSL_NO_SSL3)
448 meth=SSLv3_client_method();
449#elif !defined(OPENSSL_NO_SSL2)
450 meth=SSLv2_client_method();
451#endif
452
453 apps_startup();
454 c_Pause=0;
455 c_quiet=0;
456 c_ign_eof=0;
457 c_debug=0;
458 c_msg=0;
459 c_showcerts=0;
460
461 if (bio_err == NULL)
462 bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
463
464 if (!load_config(bio_err, NULL))
465 goto end;
466
467 if ( ((cbuf=OPENSSL_malloc(BUFSIZZ)) == NULL) ||
468 ((sbuf=OPENSSL_malloc(BUFSIZZ)) == NULL) ||
469 ((mbuf=OPENSSL_malloc(BUFSIZZ)) == NULL))
470 {
471 BIO_printf(bio_err,"out of memory\n");
472 goto end;
473 }
474
475 verify_depth=0;
476 verify_error=X509_V_OK;
477#ifdef FIONBIO
478 c_nbio=0;
479#endif
480
481 argc--;
482 argv++;
483 while (argc >= 1)
484 {
485 if (strcmp(*argv,"-host") == 0)
486 {
487 if (--argc < 1) goto bad;
488 host= *(++argv);
489 }
490 else if (strcmp(*argv,"-port") == 0)
491 {
492 if (--argc < 1) goto bad;
493 port=atoi(*(++argv));
494 if (port == 0) goto bad;
495 }
496 else if (strcmp(*argv,"-connect") == 0)
497 {
498 if (--argc < 1) goto bad;
499 if (!extract_host_port(*(++argv),&host,NULL,&port))
500 goto bad;
501 }
502 else if (strcmp(*argv,"-verify") == 0)
503 {
504 verify=SSL_VERIFY_PEER;
505 if (--argc < 1) goto bad;
506 verify_depth=atoi(*(++argv));
507 BIO_printf(bio_err,"verify depth is %d\n",verify_depth);
508 }
509 else if (strcmp(*argv,"-cert") == 0)
510 {
511 if (--argc < 1) goto bad;
512 cert_file= *(++argv);
513 }
514 else if (strcmp(*argv,"-sess_out") == 0)
515 {
516 if (--argc < 1) goto bad;
517 sess_out = *(++argv);
518 }
519 else if (strcmp(*argv,"-sess_in") == 0)
520 {
521 if (--argc < 1) goto bad;
522 sess_in = *(++argv);
523 }
524 else if (strcmp(*argv,"-certform") == 0)
525 {
526 if (--argc < 1) goto bad;
527 cert_format = str2fmt(*(++argv));
528 }
529 else if (args_verify(&argv, &argc, &badarg, bio_err, &vpm))
530 {
531 if (badarg)
532 goto bad;
533 continue;
534 }
535 else if (strcmp(*argv,"-verify_return_error") == 0)
536 verify_return_error = 1;
537 else if (strcmp(*argv,"-prexit") == 0)
538 prexit=1;
539 else if (strcmp(*argv,"-crlf") == 0)
540 crlf=1;
541 else if (strcmp(*argv,"-quiet") == 0)
542 {
543 c_quiet=1;
544 c_ign_eof=1;
545 }
546 else if (strcmp(*argv,"-ign_eof") == 0)
547 c_ign_eof=1;
548 else if (strcmp(*argv,"-no_ign_eof") == 0)
549 c_ign_eof=0;
550 else if (strcmp(*argv,"-pause") == 0)
551 c_Pause=1;
552 else if (strcmp(*argv,"-debug") == 0)
553 c_debug=1;
554#ifndef OPENSSL_NO_TLSEXT
555 else if (strcmp(*argv,"-tlsextdebug") == 0)
556 c_tlsextdebug=1;
557 else if (strcmp(*argv,"-status") == 0)
558 c_status_req=1;
559#endif
560#ifdef WATT32
561 else if (strcmp(*argv,"-wdebug") == 0)
562 dbug_init();
563#endif
564 else if (strcmp(*argv,"-msg") == 0)
565 c_msg=1;
566 else if (strcmp(*argv,"-showcerts") == 0)
567 c_showcerts=1;
568 else if (strcmp(*argv,"-nbio_test") == 0)
569 nbio_test=1;
570 else if (strcmp(*argv,"-state") == 0)
571 state=1;
572#ifndef OPENSSL_NO_PSK
573 else if (strcmp(*argv,"-psk_identity") == 0)
574 {
575 if (--argc < 1) goto bad;
576 psk_identity=*(++argv);
577 }
578 else if (strcmp(*argv,"-psk") == 0)
579 {
580 size_t j;
581
582 if (--argc < 1) goto bad;
583 psk_key=*(++argv);
584 for (j = 0; j < strlen(psk_key); j++)
585 {
586 if (isxdigit((int)psk_key[j]))
587 continue;
588 BIO_printf(bio_err,"Not a hex number '%s'\n",*argv);
589 goto bad;
590 }
591 }
592#endif
593#ifndef OPENSSL_NO_SSL2
594 else if (strcmp(*argv,"-ssl2") == 0)
595 meth=SSLv2_client_method();
596#endif
597#ifndef OPENSSL_NO_SSL3
598 else if (strcmp(*argv,"-ssl3") == 0)
599 meth=SSLv3_client_method();
600#endif
601#ifndef OPENSSL_NO_TLS1
602 else if (strcmp(*argv,"-tls1") == 0)
603 meth=TLSv1_client_method();
604#endif
605#ifndef OPENSSL_NO_DTLS1
606 else if (strcmp(*argv,"-dtls1") == 0)
607 {
608 meth=DTLSv1_client_method();
609 socket_type=SOCK_DGRAM;
610 }
611 else if (strcmp(*argv,"-timeout") == 0)
612 enable_timeouts=1;
613 else if (strcmp(*argv,"-mtu") == 0)
614 {
615 if (--argc < 1) goto bad;
616 socket_mtu = atol(*(++argv));
617 }
618#endif
619 else if (strcmp(*argv,"-bugs") == 0)
620 bugs=1;
621 else if (strcmp(*argv,"-keyform") == 0)
622 {
623 if (--argc < 1) goto bad;
624 key_format = str2fmt(*(++argv));
625 }
626 else if (strcmp(*argv,"-pass") == 0)
627 {
628 if (--argc < 1) goto bad;
629 passarg = *(++argv);
630 }
631 else if (strcmp(*argv,"-key") == 0)
632 {
633 if (--argc < 1) goto bad;
634 key_file= *(++argv);
635 }
636 else if (strcmp(*argv,"-reconnect") == 0)
637 {
638 reconnect=5;
639 }
640 else if (strcmp(*argv,"-CApath") == 0)
641 {
642 if (--argc < 1) goto bad;
643 CApath= *(++argv);
644 }
645 else if (strcmp(*argv,"-CAfile") == 0)
646 {
647 if (--argc < 1) goto bad;
648 CAfile= *(++argv);
649 }
650 else if (strcmp(*argv,"-no_tls1") == 0)
651 off|=SSL_OP_NO_TLSv1;
652 else if (strcmp(*argv,"-no_ssl3") == 0)
653 off|=SSL_OP_NO_SSLv3;
654 else if (strcmp(*argv,"-no_ssl2") == 0)
655 off|=SSL_OP_NO_SSLv2;
656 else if (strcmp(*argv,"-no_comp") == 0)
657 { off|=SSL_OP_NO_COMPRESSION; }
658#ifndef OPENSSL_NO_TLSEXT
659 else if (strcmp(*argv,"-no_ticket") == 0)
660 { off|=SSL_OP_NO_TICKET; }
661#endif
662 else if (strcmp(*argv,"-cutthrough") == 0)
663 cutthrough=1;
664 else if (strcmp(*argv,"-serverpref") == 0)
665 off|=SSL_OP_CIPHER_SERVER_PREFERENCE;
666 else if (strcmp(*argv,"-legacy_renegotiation") == 0)
667 off|=SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION;
668 else if (strcmp(*argv,"-legacy_server_connect") == 0)
669 { off|=SSL_OP_LEGACY_SERVER_CONNECT; }
670 else if (strcmp(*argv,"-no_legacy_server_connect") == 0)
671 { clr|=SSL_OP_LEGACY_SERVER_CONNECT; }
672 else if (strcmp(*argv,"-cipher") == 0)
673 {
674 if (--argc < 1) goto bad;
675 cipher= *(++argv);
676 }
677#ifdef FIONBIO
678 else if (strcmp(*argv,"-nbio") == 0)
679 { c_nbio=1; }
680#endif
681 else if (strcmp(*argv,"-starttls") == 0)
682 {
683 if (--argc < 1) goto bad;
684 ++argv;
685 if (strcmp(*argv,"smtp") == 0)
686 starttls_proto = PROTO_SMTP;
687 else if (strcmp(*argv,"pop3") == 0)
688 starttls_proto = PROTO_POP3;
689 else if (strcmp(*argv,"imap") == 0)
690 starttls_proto = PROTO_IMAP;
691 else if (strcmp(*argv,"ftp") == 0)
692 starttls_proto = PROTO_FTP;
693 else if (strcmp(*argv, "xmpp") == 0)
694 starttls_proto = PROTO_XMPP;
695 else
696 goto bad;
697 }
698#ifndef OPENSSL_NO_ENGINE
699 else if (strcmp(*argv,"-engine") == 0)
700 {
701 if (--argc < 1) goto bad;
702 engine_id = *(++argv);
703 }
704 else if (strcmp(*argv,"-ssl_client_engine") == 0)
705 {
706 if (--argc < 1) goto bad;
707 ssl_client_engine_id = *(++argv);
708 }
709#endif
710 else if (strcmp(*argv,"-rand") == 0)
711 {
712 if (--argc < 1) goto bad;
713 inrand= *(++argv);
714 }
715#ifndef OPENSSL_NO_TLSEXT
716 else if (strcmp(*argv,"-servername") == 0)
717 {
718 if (--argc < 1) goto bad;
719 servername= *(++argv);
720 /* meth=TLSv1_client_method(); */
721 }
722#endif
723#ifndef OPENSSL_NO_JPAKE
724 else if (strcmp(*argv,"-jpake") == 0)
725 {
726 if (--argc < 1) goto bad;
727 jpake_secret = *++argv;
728 }
729#endif
730 else
731 {
732 BIO_printf(bio_err,"unknown option %s\n",*argv);
733 badop=1;
734 break;
735 }
736 argc--;
737 argv++;
738 }
739 if (badop)
740 {
741bad:
742 sc_usage();
743 goto end;
744 }
745
746#if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK)
747 if (jpake_secret)
748 {
749 if (psk_key)
750 {
751 BIO_printf(bio_err,
752 "Can't use JPAKE and PSK together\n");
753 goto end;
754 }
755 psk_identity = "JPAKE";
756 }
757
758 if (cipher)
759 {
760 BIO_printf(bio_err, "JPAKE sets cipher to PSK\n");
761 goto end;
762 }
763 cipher = "PSK";
764#endif
765
766 OpenSSL_add_ssl_algorithms();
767 SSL_load_error_strings();
768
769#ifndef OPENSSL_NO_ENGINE
770 e = setup_engine(bio_err, engine_id, 1);
771 if (ssl_client_engine_id)
772 {
773 ssl_client_engine = ENGINE_by_id(ssl_client_engine_id);
774 if (!ssl_client_engine)
775 {
776 BIO_printf(bio_err,
777 "Error getting client auth engine\n");
778 goto end;
779 }
780 }
781
782#endif
783 if (!app_passwd(bio_err, passarg, NULL, &pass, NULL))
784 {
785 BIO_printf(bio_err, "Error getting password\n");
786 goto end;
787 }
788
789 if (key_file == NULL)
790 key_file = cert_file;
791
792
793 if (key_file)
794
795 {
796
797 key = load_key(bio_err, key_file, key_format, 0, pass, e,
798 "client certificate private key file");
799 if (!key)
800 {
801 ERR_print_errors(bio_err);
802 goto end;
803 }
804
805 }
806
807 if (cert_file)
808
809 {
810 cert = load_cert(bio_err,cert_file,cert_format,
811 NULL, e, "client certificate file");
812
813 if (!cert)
814 {
815 ERR_print_errors(bio_err);
816 goto end;
817 }
818 }
819
820 if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL
821 && !RAND_status())
822 {
823 BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
824 }
825 if (inrand != NULL)
826 BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
827 app_RAND_load_files(inrand));
828
829 if (bio_c_out == NULL)
830 {
831 if (c_quiet && !c_debug && !c_msg)
832 {
833 bio_c_out=BIO_new(BIO_s_null());
834 }
835 else
836 {
837 if (bio_c_out == NULL)
838 bio_c_out=BIO_new_fp(stdout,BIO_NOCLOSE);
839 }
840 }
841
842 ctx=SSL_CTX_new(meth);
843 if (ctx == NULL)
844 {
845 ERR_print_errors(bio_err);
846 goto end;
847 }
848
849 if (vpm)
850 SSL_CTX_set1_param(ctx, vpm);
851
852#ifndef OPENSSL_NO_ENGINE
853 if (ssl_client_engine)
854 {
855 if (!SSL_CTX_set_client_cert_engine(ctx, ssl_client_engine))
856 {
857 BIO_puts(bio_err, "Error setting client auth engine\n");
858 ERR_print_errors(bio_err);
859 ENGINE_free(ssl_client_engine);
860 goto end;
861 }
862 ENGINE_free(ssl_client_engine);
863 }
864#endif
865
866#ifndef OPENSSL_NO_PSK
867#ifdef OPENSSL_NO_JPAKE
868 if (psk_key != NULL)
869#else
870 if (psk_key != NULL || jpake_secret)
871#endif
872 {
873 if (c_debug)
874 BIO_printf(bio_c_out, "PSK key given or JPAKE in use, setting client callback\n");
875 SSL_CTX_set_psk_client_callback(ctx, psk_client_cb);
876 }
877#endif
878 if (bugs)
879 SSL_CTX_set_options(ctx,SSL_OP_ALL|off);
880 else
881 SSL_CTX_set_options(ctx,off);
882
883 if (clr)
884 SSL_CTX_clear_options(ctx, clr);
885 /* DTLS: partial reads end up discarding unread UDP bytes :-(
886 * Setting read ahead solves this problem.
887 */
888 if (socket_type == SOCK_DGRAM) SSL_CTX_set_read_ahead(ctx, 1);
889
890 /* Enable handshake cutthrough for client connections using
891 * strong ciphers. */
892 if (cutthrough)
893 {
894 int ssl_mode = SSL_CTX_get_mode(ctx);
895 ssl_mode |= SSL_MODE_HANDSHAKE_CUTTHROUGH;
896 SSL_CTX_set_mode(ctx, ssl_mode);
897 }
898
899 if (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback);
900 if (cipher != NULL)
901 if(!SSL_CTX_set_cipher_list(ctx,cipher)) {
902 BIO_printf(bio_err,"error setting cipher list\n");
903 ERR_print_errors(bio_err);
904 goto end;
905 }
906#if 0
907 else
908 SSL_CTX_set_cipher_list(ctx,getenv("SSL_CIPHER"));
909#endif
910
911 SSL_CTX_set_verify(ctx,verify,verify_callback);
912 if (!set_cert_key_stuff(ctx,cert,key))
913 goto end;
914
915 if ((!SSL_CTX_load_verify_locations(ctx,CAfile,CApath)) ||
916 (!SSL_CTX_set_default_verify_paths(ctx)))
917 {
918 /* BIO_printf(bio_err,"error setting default verify locations\n"); */
919 ERR_print_errors(bio_err);
920 /* goto end; */
921 }
922
923#ifndef OPENSSL_NO_TLSEXT
924 if (servername != NULL)
925 {
926 tlsextcbp.biodebug = bio_err;
927 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
928 SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
929 }
930#endif
931
932 con=SSL_new(ctx);
933 if (sess_in)
934 {
935 SSL_SESSION *sess;
936 BIO *stmp = BIO_new_file(sess_in, "r");
937 if (!stmp)
938 {
939 BIO_printf(bio_err, "Can't open session file %s\n",
940 sess_in);
941 ERR_print_errors(bio_err);
942 goto end;
943 }
944 sess = PEM_read_bio_SSL_SESSION(stmp, NULL, 0, NULL);
945 BIO_free(stmp);
946 if (!sess)
947 {
948 BIO_printf(bio_err, "Can't open session file %s\n",
949 sess_in);
950 ERR_print_errors(bio_err);
951 goto end;
952 }
953 SSL_set_session(con, sess);
954 SSL_SESSION_free(sess);
955 }
956#ifndef OPENSSL_NO_TLSEXT
957 if (servername != NULL)
958 {
959 if (!SSL_set_tlsext_host_name(con,servername))
960 {
961 BIO_printf(bio_err,"Unable to set TLS servername extension.\n");
962 ERR_print_errors(bio_err);
963 goto end;
964 }
965 }
966#endif
967#ifndef OPENSSL_NO_KRB5
968 if (con && (con->kssl_ctx = kssl_ctx_new()) != NULL)
969 {
970 kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVER, host);
971 }
972#endif /* OPENSSL_NO_KRB5 */
973/* SSL_set_cipher_list(con,"RC4-MD5"); */
974#if 0
975#ifdef TLSEXT_TYPE_opaque_prf_input
976 SSL_set_tlsext_opaque_prf_input(con, "Test client", 11);
977#endif
978#endif
979
980re_start:
981
982 if (init_client(&s,host,port,socket_type) == 0)
983 {
984 BIO_printf(bio_err,"connect:errno=%d\n",get_last_socket_error());
985 SHUTDOWN(s);
986 goto end;
987 }
988 BIO_printf(bio_c_out,"CONNECTED(%08X)\n",s);
989
990#ifdef FIONBIO
991 if (c_nbio)
992 {
993 unsigned long l=1;
994 BIO_printf(bio_c_out,"turning on non blocking io\n");
995 if (BIO_socket_ioctl(s,FIONBIO,&l) < 0)
996 {
997 ERR_print_errors(bio_err);
998 goto end;
999 }
1000 }
1001#endif
1002 if (c_Pause & 0x01) con->debug=1;
1003
1004 if ( SSL_version(con) == DTLS1_VERSION)
1005 {
1006
1007 sbio=BIO_new_dgram(s,BIO_NOCLOSE);
1008 if (getsockname(s, &peer, (void *)&peerlen) < 0)
1009 {
1010 BIO_printf(bio_err, "getsockname:errno=%d\n",
1011 get_last_socket_error());
1012 SHUTDOWN(s);
1013 goto end;
1014 }
1015
1016 (void)BIO_ctrl_set_connected(sbio, 1, &peer);
1017
1018 if (enable_timeouts)
1019 {
1020 timeout.tv_sec = 0;
1021 timeout.tv_usec = DGRAM_RCV_TIMEOUT;
1022 BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
1023
1024 timeout.tv_sec = 0;
1025 timeout.tv_usec = DGRAM_SND_TIMEOUT;
1026 BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
1027 }
1028
1029 if (socket_mtu > 28)
1030 {
1031 SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
1032 SSL_set_mtu(con, socket_mtu - 28);
1033 }
1034 else
1035 /* want to do MTU discovery */
1036 BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
1037 }
1038 else
1039 sbio=BIO_new_socket(s,BIO_NOCLOSE);
1040
1041 if (nbio_test)
1042 {
1043 BIO *test;
1044
1045 test=BIO_new(BIO_f_nbio_test());
1046 sbio=BIO_push(test,sbio);
1047 }
1048
1049 if (c_debug)
1050 {
1051 con->debug=1;
1052 BIO_set_callback(sbio,bio_dump_callback);
1053 BIO_set_callback_arg(sbio,(char *)bio_c_out);
1054 }
1055 if (c_msg)
1056 {
1057 SSL_set_msg_callback(con, msg_cb);
1058 SSL_set_msg_callback_arg(con, bio_c_out);
1059 }
1060#ifndef OPENSSL_NO_TLSEXT
1061 if (c_tlsextdebug)
1062 {
1063 SSL_set_tlsext_debug_callback(con, tlsext_cb);
1064 SSL_set_tlsext_debug_arg(con, bio_c_out);
1065 }
1066 if (c_status_req)
1067 {
1068 SSL_set_tlsext_status_type(con, TLSEXT_STATUSTYPE_ocsp);
1069 SSL_CTX_set_tlsext_status_cb(ctx, ocsp_resp_cb);
1070 SSL_CTX_set_tlsext_status_arg(ctx, bio_c_out);
1071#if 0
1072{
1073STACK_OF(OCSP_RESPID) *ids = sk_OCSP_RESPID_new_null();
1074OCSP_RESPID *id = OCSP_RESPID_new();
1075id->value.byKey = ASN1_OCTET_STRING_new();
1076id->type = V_OCSP_RESPID_KEY;
1077ASN1_STRING_set(id->value.byKey, "Hello World", -1);
1078sk_OCSP_RESPID_push(ids, id);
1079SSL_set_tlsext_status_ids(con, ids);
1080}
1081#endif
1082 }
1083#endif
1084#ifndef OPENSSL_NO_JPAKE
1085 if (jpake_secret)
1086 jpake_client_auth(bio_c_out, sbio, jpake_secret);
1087#endif
1088
1089 SSL_set_bio(con,sbio,sbio);
1090 SSL_set_connect_state(con);
1091
1092 /* ok, lets connect */
1093 width=SSL_get_fd(con)+1;
1094
1095 read_tty=1;
1096 write_tty=0;
1097 tty_on=0;
1098 read_ssl=1;
1099 write_ssl=1;
1100
1101 cbuf_len=0;
1102 cbuf_off=0;
1103 sbuf_len=0;
1104 sbuf_off=0;
1105
1106 /* This is an ugly hack that does a lot of assumptions */
1107 /* We do have to handle multi-line responses which may come
1108 in a single packet or not. We therefore have to use
1109 BIO_gets() which does need a buffering BIO. So during
1110 the initial chitchat we do push a buffering BIO into the
1111 chain that is removed again later on to not disturb the
1112 rest of the s_client operation. */
1113 if (starttls_proto == PROTO_SMTP)
1114 {
1115 int foundit=0;
1116 BIO *fbio = BIO_new(BIO_f_buffer());
1117 BIO_push(fbio, sbio);
1118 /* wait for multi-line response to end from SMTP */
1119 do
1120 {
1121 mbuf_len = BIO_gets(fbio,mbuf,BUFSIZZ);
1122 }
1123 while (mbuf_len>3 && mbuf[3]=='-');
1124 /* STARTTLS command requires EHLO... */
1125 BIO_printf(fbio,"EHLO openssl.client.net\r\n");
1126 (void)BIO_flush(fbio);
1127 /* wait for multi-line response to end EHLO SMTP response */
1128 do
1129 {
1130 mbuf_len = BIO_gets(fbio,mbuf,BUFSIZZ);
1131 if (strstr(mbuf,"STARTTLS"))
1132 foundit=1;
1133 }
1134 while (mbuf_len>3 && mbuf[3]=='-');
1135 (void)BIO_flush(fbio);
1136 BIO_pop(fbio);
1137 BIO_free(fbio);
1138 if (!foundit)
1139 BIO_printf(bio_err,
1140 "didn't found starttls in server response,"
1141 " try anyway...\n");
1142 BIO_printf(sbio,"STARTTLS\r\n");
1143 BIO_read(sbio,sbuf,BUFSIZZ);
1144 }
1145 else if (starttls_proto == PROTO_POP3)
1146 {
1147 BIO_read(sbio,mbuf,BUFSIZZ);
1148 BIO_printf(sbio,"STLS\r\n");
1149 BIO_read(sbio,sbuf,BUFSIZZ);
1150 }
1151 else if (starttls_proto == PROTO_IMAP)
1152 {
1153 int foundit=0;
1154 BIO *fbio = BIO_new(BIO_f_buffer());
1155 BIO_push(fbio, sbio);
1156 BIO_gets(fbio,mbuf,BUFSIZZ);
1157 /* STARTTLS command requires CAPABILITY... */
1158 BIO_printf(fbio,". CAPABILITY\r\n");
1159 (void)BIO_flush(fbio);
1160 /* wait for multi-line CAPABILITY response */
1161 do
1162 {
1163 mbuf_len = BIO_gets(fbio,mbuf,BUFSIZZ);
1164 if (strstr(mbuf,"STARTTLS"))
1165 foundit=1;
1166 }
1167 while (mbuf_len>3 && mbuf[0]!='.');
1168 (void)BIO_flush(fbio);
1169 BIO_pop(fbio);
1170 BIO_free(fbio);
1171 if (!foundit)
1172 BIO_printf(bio_err,
1173 "didn't found STARTTLS in server response,"
1174 " try anyway...\n");
1175 BIO_printf(sbio,". STARTTLS\r\n");
1176 BIO_read(sbio,sbuf,BUFSIZZ);
1177 }
1178 else if (starttls_proto == PROTO_FTP)
1179 {
1180 BIO *fbio = BIO_new(BIO_f_buffer());
1181 BIO_push(fbio, sbio);
1182 /* wait for multi-line response to end from FTP */
1183 do
1184 {
1185 mbuf_len = BIO_gets(fbio,mbuf,BUFSIZZ);
1186 }
1187 while (mbuf_len>3 && mbuf[3]=='-');
1188 (void)BIO_flush(fbio);
1189 BIO_pop(fbio);
1190 BIO_free(fbio);
1191 BIO_printf(sbio,"AUTH TLS\r\n");
1192 BIO_read(sbio,sbuf,BUFSIZZ);
1193 }
1194 if (starttls_proto == PROTO_XMPP)
1195 {
1196 int seen = 0;
1197 BIO_printf(sbio,"<stream:stream "
1198 "xmlns:stream='http://etherx.jabber.org/streams' "
1199 "xmlns='jabber:client' to='%s' version='1.0'>", host);
1200 seen = BIO_read(sbio,mbuf,BUFSIZZ);
1201 mbuf[seen] = 0;
1202 while (!strstr(mbuf, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'"))
1203 {
1204 if (strstr(mbuf, "/stream:features>"))
1205 goto shut;
1206 seen = BIO_read(sbio,mbuf,BUFSIZZ);
1207 mbuf[seen] = 0;
1208 }
1209 BIO_printf(sbio, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
1210 seen = BIO_read(sbio,sbuf,BUFSIZZ);
1211 sbuf[seen] = 0;
1212 if (!strstr(sbuf, "<proceed"))
1213 goto shut;
1214 mbuf[0] = 0;
1215 }
1216
1217 for (;;)
1218 {
1219 FD_ZERO(&readfds);
1220 FD_ZERO(&writefds);
1221
1222 if ((SSL_version(con) == DTLS1_VERSION) &&
1223 DTLSv1_get_timeout(con, &timeout))
1224 timeoutp = &timeout;
1225 else
1226 timeoutp = NULL;
1227
1228 if (SSL_in_init(con) && !SSL_total_renegotiations(con))
1229 {
1230 in_init=1;
1231 tty_on=0;
1232 }
1233 else
1234 {
1235 tty_on=1;
1236 if (in_init)
1237 {
1238 in_init=0;
1239#if 0 /* This test doesn't really work as intended (needs to be fixed) */
1240#ifndef OPENSSL_NO_TLSEXT
1241 if (servername != NULL && !SSL_session_reused(con))
1242 {
1243 BIO_printf(bio_c_out,"Server did %sacknowledge servername extension.\n",tlsextcbp.ack?"":"not ");
1244 }
1245#endif
1246#endif
1247 if (sess_out)
1248 {
1249 BIO *stmp = BIO_new_file(sess_out, "w");
1250 if (stmp)
1251 {
1252 PEM_write_bio_SSL_SESSION(stmp, SSL_get_session(con));
1253 BIO_free(stmp);
1254 }
1255 else
1256 BIO_printf(bio_err, "Error writing session file %s\n", sess_out);
1257 }
1258 print_stuff(bio_c_out,con,full_log);
1259 if (full_log > 0) full_log--;
1260
1261 if (starttls_proto)
1262 {
1263 BIO_printf(bio_err,"%s",mbuf);
1264 /* We don't need to know any more */
1265 starttls_proto = PROTO_OFF;
1266 }
1267
1268 if (reconnect)
1269 {
1270 reconnect--;
1271 BIO_printf(bio_c_out,"drop connection and then reconnect\n");
1272 SSL_shutdown(con);
1273 SSL_set_connect_state(con);
1274 SHUTDOWN(SSL_get_fd(con));
1275 goto re_start;
1276 }
1277 }
1278 }
1279
1280 ssl_pending = read_ssl && SSL_pending(con);
1281
1282 if (!ssl_pending)
1283 {
1284#if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE) && !defined (OPENSSL_SYS_BEOS_R5)
1285 if (tty_on)
1286 {
1287 if (read_tty) openssl_fdset(fileno(stdin),&readfds);
1288 if (write_tty) openssl_fdset(fileno(stdout),&writefds);
1289 }
1290 if (read_ssl)
1291 openssl_fdset(SSL_get_fd(con),&readfds);
1292 if (write_ssl)
1293 openssl_fdset(SSL_get_fd(con),&writefds);
1294#else
1295 if(!tty_on || !write_tty) {
1296 if (read_ssl)
1297 openssl_fdset(SSL_get_fd(con),&readfds);
1298 if (write_ssl)
1299 openssl_fdset(SSL_get_fd(con),&writefds);
1300 }
1301#endif
1302/* printf("mode tty(%d %d%d) ssl(%d%d)\n",
1303 tty_on,read_tty,write_tty,read_ssl,write_ssl);*/
1304
1305 /* Note: under VMS with SOCKETSHR the second parameter
1306 * is currently of type (int *) whereas under other
1307 * systems it is (void *) if you don't have a cast it
1308 * will choke the compiler: if you do have a cast then
1309 * you can either go for (int *) or (void *).
1310 */
1311#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
1312 /* Under Windows/DOS we make the assumption that we can
1313 * always write to the tty: therefore if we need to
1314 * write to the tty we just fall through. Otherwise
1315 * we timeout the select every second and see if there
1316 * are any keypresses. Note: this is a hack, in a proper
1317 * Windows application we wouldn't do this.
1318 */
1319 i=0;
1320 if(!write_tty) {
1321 if(read_tty) {
1322 tv.tv_sec = 1;
1323 tv.tv_usec = 0;
1324 i=select(width,(void *)&readfds,(void *)&writefds,
1325 NULL,&tv);
1326#if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)
1327 if(!i && (!_kbhit() || !read_tty) ) continue;
1328#else
1329 if(!i && (!((_kbhit()) || (WAIT_OBJECT_0 == WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0))) || !read_tty) ) continue;
1330#endif
1331 } else i=select(width,(void *)&readfds,(void *)&writefds,
1332 NULL,timeoutp);
1333 }
1334#elif defined(OPENSSL_SYS_NETWARE)
1335 if(!write_tty) {
1336 if(read_tty) {
1337 tv.tv_sec = 1;
1338 tv.tv_usec = 0;
1339 i=select(width,(void *)&readfds,(void *)&writefds,
1340 NULL,&tv);
1341 } else i=select(width,(void *)&readfds,(void *)&writefds,
1342 NULL,timeoutp);
1343 }
1344#elif defined(OPENSSL_SYS_BEOS_R5)
1345 /* Under BeOS-R5 the situation is similar to DOS */
1346 i=0;
1347 stdin_set = 0;
1348 (void)fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
1349 if(!write_tty) {
1350 if(read_tty) {
1351 tv.tv_sec = 1;
1352 tv.tv_usec = 0;
1353 i=select(width,(void *)&readfds,(void *)&writefds,
1354 NULL,&tv);
1355 if (read(fileno(stdin), sbuf, 0) >= 0)
1356 stdin_set = 1;
1357 if (!i && (stdin_set != 1 || !read_tty))
1358 continue;
1359 } else i=select(width,(void *)&readfds,(void *)&writefds,
1360 NULL,timeoutp);
1361 }
1362 (void)fcntl(fileno(stdin), F_SETFL, 0);
1363#else
1364 i=select(width,(void *)&readfds,(void *)&writefds,
1365 NULL,timeoutp);
1366#endif
1367 if ( i < 0)
1368 {
1369 BIO_printf(bio_err,"bad select %d\n",
1370 get_last_socket_error());
1371 goto shut;
1372 /* goto end; */
1373 }
1374 }
1375
1376 if ((SSL_version(con) == DTLS1_VERSION) && DTLSv1_handle_timeout(con) > 0)
1377 {
1378 BIO_printf(bio_err,"TIMEOUT occured\n");
1379 }
1380
1381 if (!ssl_pending && FD_ISSET(SSL_get_fd(con),&writefds))
1382 {
1383 k=SSL_write(con,&(cbuf[cbuf_off]),
1384 (unsigned int)cbuf_len);
1385 switch (SSL_get_error(con,k))
1386 {
1387 case SSL_ERROR_NONE:
1388 cbuf_off+=k;
1389 cbuf_len-=k;
1390 if (k <= 0) goto end;
1391 /* we have done a write(con,NULL,0); */
1392 if (cbuf_len <= 0)
1393 {
1394 read_tty=1;
1395 write_ssl=0;
1396 }
1397 else /* if (cbuf_len > 0) */
1398 {
1399 read_tty=0;
1400 write_ssl=1;
1401 }
1402 break;
1403 case SSL_ERROR_WANT_WRITE:
1404 BIO_printf(bio_c_out,"write W BLOCK\n");
1405 write_ssl=1;
1406 read_tty=0;
1407 break;
1408 case SSL_ERROR_WANT_READ:
1409 BIO_printf(bio_c_out,"write R BLOCK\n");
1410 write_tty=0;
1411 read_ssl=1;
1412 write_ssl=0;
1413 break;
1414 case SSL_ERROR_WANT_X509_LOOKUP:
1415 BIO_printf(bio_c_out,"write X BLOCK\n");
1416 break;
1417 case SSL_ERROR_ZERO_RETURN:
1418 if (cbuf_len != 0)
1419 {
1420 BIO_printf(bio_c_out,"shutdown\n");
1421 ret = 0;
1422 goto shut;
1423 }
1424 else
1425 {
1426 read_tty=1;
1427 write_ssl=0;
1428 break;
1429 }
1430
1431 case SSL_ERROR_SYSCALL:
1432 if ((k != 0) || (cbuf_len != 0))
1433 {
1434 BIO_printf(bio_err,"write:errno=%d\n",
1435 get_last_socket_error());
1436 goto shut;
1437 }
1438 else
1439 {
1440 read_tty=1;
1441 write_ssl=0;
1442 }
1443 break;
1444 case SSL_ERROR_SSL:
1445 ERR_print_errors(bio_err);
1446 goto shut;
1447 }
1448 }
1449#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS_R5)
1450 /* Assume Windows/DOS/BeOS can always write */
1451 else if (!ssl_pending && write_tty)
1452#else
1453 else if (!ssl_pending && FD_ISSET(fileno(stdout),&writefds))
1454#endif
1455 {
1456#ifdef CHARSET_EBCDIC
1457 ascii2ebcdic(&(sbuf[sbuf_off]),&(sbuf[sbuf_off]),sbuf_len);
1458#endif
1459 i=raw_write_stdout(&(sbuf[sbuf_off]),sbuf_len);
1460
1461 if (i <= 0)
1462 {
1463 BIO_printf(bio_c_out,"DONE\n");
1464 ret = 0;
1465 goto shut;
1466 /* goto end; */
1467 }
1468
1469 sbuf_len-=i;;
1470 sbuf_off+=i;
1471 if (sbuf_len <= 0)
1472 {
1473 read_ssl=1;
1474 write_tty=0;
1475 }
1476 }
1477 else if (ssl_pending || FD_ISSET(SSL_get_fd(con),&readfds))
1478 {
1479#ifdef RENEG
1480{ static int iiii; if (++iiii == 52) { SSL_renegotiate(con); iiii=0; } }
1481#endif
1482#if 1
1483 k=SSL_read(con,sbuf,1024 /* BUFSIZZ */ );
1484#else
1485/* Demo for pending and peek :-) */
1486 k=SSL_read(con,sbuf,16);
1487{ char zbuf[10240];
1488printf("read=%d pending=%d peek=%d\n",k,SSL_pending(con),SSL_peek(con,zbuf,10240));
1489}
1490#endif
1491
1492 switch (SSL_get_error(con,k))
1493 {
1494 case SSL_ERROR_NONE:
1495 if (k <= 0)
1496 goto end;
1497 sbuf_off=0;
1498 sbuf_len=k;
1499
1500 read_ssl=0;
1501 write_tty=1;
1502 break;
1503 case SSL_ERROR_WANT_WRITE:
1504 BIO_printf(bio_c_out,"read W BLOCK\n");
1505 write_ssl=1;
1506 read_tty=0;
1507 break;
1508 case SSL_ERROR_WANT_READ:
1509 BIO_printf(bio_c_out,"read R BLOCK\n");
1510 write_tty=0;
1511 read_ssl=1;
1512 if ((read_tty == 0) && (write_ssl == 0))
1513 write_ssl=1;
1514 break;
1515 case SSL_ERROR_WANT_X509_LOOKUP:
1516 BIO_printf(bio_c_out,"read X BLOCK\n");
1517 break;
1518 case SSL_ERROR_SYSCALL:
1519 ret=get_last_socket_error();
1520 BIO_printf(bio_err,"read:errno=%d\n",ret);
1521 goto shut;
1522 case SSL_ERROR_ZERO_RETURN:
1523 BIO_printf(bio_c_out,"closed\n");
1524 ret=0;
1525 goto shut;
1526 case SSL_ERROR_SSL:
1527 ERR_print_errors(bio_err);
1528 goto shut;
1529 /* break; */
1530 }
1531 }
1532
1533#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
1534#if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)
1535 else if (_kbhit())
1536#else
1537 else if ((_kbhit()) || (WAIT_OBJECT_0 == WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0)))
1538#endif
1539#elif defined (OPENSSL_SYS_NETWARE)
1540 else if (_kbhit())
1541#elif defined(OPENSSL_SYS_BEOS_R5)
1542 else if (stdin_set)
1543#else
1544 else if (FD_ISSET(fileno(stdin),&readfds))
1545#endif
1546 {
1547 if (crlf)
1548 {
1549 int j, lf_num;
1550
1551 i=raw_read_stdin(cbuf,BUFSIZZ/2);
1552 lf_num = 0;
1553 /* both loops are skipped when i <= 0 */
1554 for (j = 0; j < i; j++)
1555 if (cbuf[j] == '\n')
1556 lf_num++;
1557 for (j = i-1; j >= 0; j--)
1558 {
1559 cbuf[j+lf_num] = cbuf[j];
1560 if (cbuf[j] == '\n')
1561 {
1562 lf_num--;
1563 i++;
1564 cbuf[j+lf_num] = '\r';
1565 }
1566 }
1567 assert(lf_num == 0);
1568 }
1569 else
1570 i=raw_read_stdin(cbuf,BUFSIZZ);
1571
1572 if ((!c_ign_eof) && ((i <= 0) || (cbuf[0] == 'Q')))
1573 {
1574 BIO_printf(bio_err,"DONE\n");
1575 ret=0;
1576 goto shut;
1577 }
1578
1579 if ((!c_ign_eof) && (cbuf[0] == 'R'))
1580 {
1581 BIO_printf(bio_err,"RENEGOTIATING\n");
1582 SSL_renegotiate(con);
1583 cbuf_len=0;
1584 }
1585 else
1586 {
1587 cbuf_len=i;
1588 cbuf_off=0;
1589#ifdef CHARSET_EBCDIC
1590 ebcdic2ascii(cbuf, cbuf, i);
1591#endif
1592 }
1593
1594 write_ssl=1;
1595 read_tty=0;
1596 }
1597 }
1598
1599 ret=0;
1600shut:
1601 if (in_init)
1602 print_stuff(bio_c_out,con,full_log);
1603 SSL_shutdown(con);
1604 SHUTDOWN(SSL_get_fd(con));
1605end:
1606 if (con != NULL)
1607 {
1608 if (prexit != 0)
1609 print_stuff(bio_c_out,con,1);
1610 SSL_free(con);
1611 }
1612 if (ctx != NULL) SSL_CTX_free(ctx);
1613 if (cert)
1614 X509_free(cert);
1615 if (key)
1616 EVP_PKEY_free(key);
1617 if (pass)
1618 OPENSSL_free(pass);
1619 if (cbuf != NULL) { OPENSSL_cleanse(cbuf,BUFSIZZ); OPENSSL_free(cbuf); }
1620 if (sbuf != NULL) { OPENSSL_cleanse(sbuf,BUFSIZZ); OPENSSL_free(sbuf); }
1621 if (mbuf != NULL) { OPENSSL_cleanse(mbuf,BUFSIZZ); OPENSSL_free(mbuf); }
1622 if (bio_c_out != NULL)
1623 {
1624 BIO_free(bio_c_out);
1625 bio_c_out=NULL;
1626 }
1627 apps_shutdown();
1628 OPENSSL_EXIT(ret);
1629 }
1630
1631
1632static void print_stuff(BIO *bio, SSL *s, int full)
1633 {
1634 X509 *peer=NULL;
1635 char *p;
1636 static const char *space=" ";
1637 char buf[BUFSIZ];
1638 STACK_OF(X509) *sk;
1639 STACK_OF(X509_NAME) *sk2;
1640 const SSL_CIPHER *c;
1641 X509_NAME *xn;
1642 int j,i;
1643#ifndef OPENSSL_NO_COMP
1644 const COMP_METHOD *comp, *expansion;
1645#endif
1646
1647 if (full)
1648 {
1649 int got_a_chain = 0;
1650
1651 sk=SSL_get_peer_cert_chain(s);
1652 if (sk != NULL)
1653 {
1654 got_a_chain = 1; /* we don't have it for SSL2 (yet) */
1655
1656 BIO_printf(bio,"---\nCertificate chain\n");
1657 for (i=0; i<sk_X509_num(sk); i++)
1658 {
1659 X509_NAME_oneline(X509_get_subject_name(
1660 sk_X509_value(sk,i)),buf,sizeof buf);
1661 BIO_printf(bio,"%2d s:%s\n",i,buf);
1662 X509_NAME_oneline(X509_get_issuer_name(
1663 sk_X509_value(sk,i)),buf,sizeof buf);
1664 BIO_printf(bio," i:%s\n",buf);
1665 if (c_showcerts)
1666 PEM_write_bio_X509(bio,sk_X509_value(sk,i));
1667 }
1668 }
1669
1670 BIO_printf(bio,"---\n");
1671 peer=SSL_get_peer_certificate(s);
1672 if (peer != NULL)
1673 {
1674 BIO_printf(bio,"Server certificate\n");
1675 if (!(c_showcerts && got_a_chain)) /* Redundant if we showed the whole chain */
1676 PEM_write_bio_X509(bio,peer);
1677 X509_NAME_oneline(X509_get_subject_name(peer),
1678 buf,sizeof buf);
1679 BIO_printf(bio,"subject=%s\n",buf);
1680 X509_NAME_oneline(X509_get_issuer_name(peer),
1681 buf,sizeof buf);
1682 BIO_printf(bio,"issuer=%s\n",buf);
1683 }
1684 else
1685 BIO_printf(bio,"no peer certificate available\n");
1686
1687 sk2=SSL_get_client_CA_list(s);
1688 if ((sk2 != NULL) && (sk_X509_NAME_num(sk2) > 0))
1689 {
1690 BIO_printf(bio,"---\nAcceptable client certificate CA names\n");
1691 for (i=0; i<sk_X509_NAME_num(sk2); i++)
1692 {
1693 xn=sk_X509_NAME_value(sk2,i);
1694 X509_NAME_oneline(xn,buf,sizeof(buf));
1695 BIO_write(bio,buf,strlen(buf));
1696 BIO_write(bio,"\n",1);
1697 }
1698 }
1699 else
1700 {
1701 BIO_printf(bio,"---\nNo client certificate CA names sent\n");
1702 }
1703 p=SSL_get_shared_ciphers(s,buf,sizeof buf);
1704 if (p != NULL)
1705 {
1706 /* This works only for SSL 2. In later protocol
1707 * versions, the client does not know what other
1708 * ciphers (in addition to the one to be used
1709 * in the current connection) the server supports. */
1710
1711 BIO_printf(bio,"---\nCiphers common between both SSL endpoints:\n");
1712 j=i=0;
1713 while (*p)
1714 {
1715 if (*p == ':')
1716 {
1717 BIO_write(bio,space,15-j%25);
1718 i++;
1719 j=0;
1720 BIO_write(bio,((i%3)?" ":"\n"),1);
1721 }
1722 else
1723 {
1724 BIO_write(bio,p,1);
1725 j++;
1726 }
1727 p++;
1728 }
1729 BIO_write(bio,"\n",1);
1730 }
1731
1732 BIO_printf(bio,"---\nSSL handshake has read %ld bytes and written %ld bytes\n",
1733 BIO_number_read(SSL_get_rbio(s)),
1734 BIO_number_written(SSL_get_wbio(s)));
1735 }
1736 BIO_printf(bio,((s->hit)?"---\nReused, ":"---\nNew, "));
1737 c=SSL_get_current_cipher(s);
1738 BIO_printf(bio,"%s, Cipher is %s\n",
1739 SSL_CIPHER_get_version(c),
1740 SSL_CIPHER_get_name(c));
1741 if (peer != NULL) {
1742 EVP_PKEY *pktmp;
1743 pktmp = X509_get_pubkey(peer);
1744 BIO_printf(bio,"Server public key is %d bit\n",
1745 EVP_PKEY_bits(pktmp));
1746 EVP_PKEY_free(pktmp);
1747 }
1748 BIO_printf(bio, "Secure Renegotiation IS%s supported\n",
1749 SSL_get_secure_renegotiation_support(s) ? "" : " NOT");
1750#ifndef OPENSSL_NO_COMP
1751 comp=SSL_get_current_compression(s);
1752 expansion=SSL_get_current_expansion(s);
1753 BIO_printf(bio,"Compression: %s\n",
1754 comp ? SSL_COMP_get_name(comp) : "NONE");
1755 BIO_printf(bio,"Expansion: %s\n",
1756 expansion ? SSL_COMP_get_name(expansion) : "NONE");
1757#endif
1758 SSL_SESSION_print(bio,SSL_get_session(s));
1759 BIO_printf(bio,"---\n");
1760 if (peer != NULL)
1761 X509_free(peer);
1762 /* flush, or debugging output gets mixed with http response */
1763 (void)BIO_flush(bio);
1764 }
1765
1766#ifndef OPENSSL_NO_TLSEXT
1767
1768static int ocsp_resp_cb(SSL *s, void *arg)
1769 {
1770 const unsigned char *p;
1771 int len;
1772 OCSP_RESPONSE *rsp;
1773 len = SSL_get_tlsext_status_ocsp_resp(s, &p);
1774 BIO_puts(arg, "OCSP response: ");
1775 if (!p)
1776 {
1777 BIO_puts(arg, "no response sent\n");
1778 return 1;
1779 }
1780 rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
1781 if (!rsp)
1782 {
1783 BIO_puts(arg, "response parse error\n");
1784 BIO_dump_indent(arg, (char *)p, len, 4);
1785 return 0;
1786 }
1787 BIO_puts(arg, "\n======================================\n");
1788 OCSP_RESPONSE_print(arg, rsp, 0);
1789 BIO_puts(arg, "======================================\n");
1790 OCSP_RESPONSE_free(rsp);
1791 return 1;
1792 }
1793
1794#endif