s_client.c revision 9dc0df1bac950d6e491f9a7c7e4888f2b301cb15
/* apps/s_client.c */
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
* This package is an SSL implementation written
* by Eric Young (eay@cryptsoft.com).
* The implementation was written so as to conform with Netscapes SSL.
*
* This library is free for commercial and non-commercial use as long as
* the following conditions are aheared to. The following conditions
* apply to all code found in this distribution, be it the RC4, RSA,
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
* included with this distribution is covered by the same copyright terms
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
*
* Copyright remains Eric Young's, and as such any Copyright notices in
* the code are not to be removed.
* If this package is used in a product, Eric Young should be given attribution
* as the author of the parts of the library used.
* This can be in the form of a textual message at program startup or
* in documentation (online or textual) provided with the package.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* "This product includes cryptographic software written by
* Eric Young (eay@cryptsoft.com)"
* The word 'cryptographic' can be left out if the rouines from the library
* being used are not cryptographic related :-).
* 4. If you include any Windows specific code (or a derivative thereof) from
* the apps directory (application code) you must include an acknowledgement:
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
*
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* The licence and distribution terms for any publically available version or
* derivative of this code cannot be changed. i.e. this code cannot simply be
* copied and put under another distribution licence
* [including the GNU Public Licence.]
*/
/* ====================================================================
* Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* openssl-core@openssl.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.openssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com).
*
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef OPENSSL_NO_STDIO
#define APPS_WIN16
#endif
/* With IPv6, it looks like Digital has mixed up the proper order of
recursive header file inclusion, resulting in the compiler complaining
that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which
is needed to have fileno() declared correctly... So let's define u_int */
#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
#define __U_INT
typedef unsigned int u_int;
#endif
#define USE_SOCKETS
#include "apps.h"
#include "s_apps.h"
#include "timeouts.h"
#ifdef OPENSSL_SYS_WINCE
/* Windows CE incorrectly defines fileno as returning void*, so to avoid problems below... */
#ifdef fileno
#endif
#endif
/* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
#endif
#define PROG s_client_main
/*#define SSL_HOST_NAME "www.netscape.com" */
/*#define SSL_HOST_NAME "193.118.187.102" */
#define SSL_HOST_NAME "localhost"
/*#define TEST_CERT "client.pem" */ /* no default cert. */
extern int verify_depth;
extern int verify_error;
#ifdef FIONBIO
static int c_nbio=0;
#endif
static int c_Pause=0;
static int c_debug=0;
static int c_msg=0;
static int c_showcerts=0;
static void sc_usage(void);
static int c_quiet=0;
static int c_ign_eof=0;
static void sc_usage(void)
{
BIO_printf(bio_err," -connect host:port - who to connect to (default is %s:%s)\n",SSL_HOST_NAME,PORT_STR);
#ifdef WATT32
#endif
#ifdef FIONBIO
#endif
#ifndef OPENSSL_NO_ENGINE
#endif
}
int MAIN(int, char **);
{
int off=0;
int full_log=1;
char *host=SSL_HOST_NAME;
int crlf=0;
int starttls_proto = 0;
#ifdef sock_type
#endif
int sock_type=SOCK_STREAM;
#ifndef OPENSSL_NO_ENGINE
#endif
#endif
int enable_timeouts = 0 ;
long mtu = 0;
#if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
#elif !defined(OPENSSL_NO_SSL3)
#elif !defined(OPENSSL_NO_SSL2)
#endif
apps_startup();
c_Pause=0;
c_quiet=0;
c_ign_eof=0;
c_debug=0;
c_msg=0;
c_showcerts=0;
goto end;
{
goto end;
}
verify_depth=0;
#ifdef FIONBIO
c_nbio=0;
#endif
argc--;
argv++;
while (argc >= 1)
{
{
}
{
}
{
goto bad;
}
{
}
{
}
{
}
prexit=1;
crlf=1;
{
c_quiet=1;
c_ign_eof=1;
}
c_ign_eof=1;
c_Pause=1;
c_debug=1;
#ifdef WATT32
dbug_init();
#endif
c_msg=1;
c_showcerts=1;
nbio_test=1;
state=1;
#ifndef OPENSSL_NO_SSL2
#endif
#ifndef OPENSSL_NO_SSL3
#endif
#ifndef OPENSSL_NO_TLS1
#endif
#ifndef OPENSSL_NO_DTLS1
{
}
{
}
#endif
bugs=1;
{
}
{
}
{
}
{
reconnect=5;
}
{
}
{
}
{
}
#ifdef FIONBIO
{ c_nbio=1; }
#endif
{
++argv;
starttls_proto = 1;
starttls_proto = 2;
else
goto bad;
}
#ifndef OPENSSL_NO_ENGINE
{
}
#endif
{
}
else
{
badop=1;
break;
}
argc--;
argv++;
}
if (badop)
{
bad:
sc_usage();
goto end;
}
#ifndef OPENSSL_NO_ENGINE
#endif
{
goto end;
}
if (key_file)
{
"client certificate private key file");
if (!key)
{
goto end;
}
}
if (cert_file)
{
NULL, e, "client certificate file");
if (!cert)
{
goto end;
}
}
&& !RAND_status())
{
}
{
{
}
else
{
}
}
{
goto end;
}
if (bugs)
else
/* DTLS: partial reads end up discarding unread UDP bytes :-(
* Setting read ahead solves this problem.
*/
goto end;
}
#if 0
else
#endif
goto end;
{
/* BIO_printf(bio_err,"error setting default verify locations\n"); */
/* goto end; */
}
#ifndef OPENSSL_NO_KRB5
{
}
#endif /* OPENSSL_NO_KRB5 */
/* SSL_set_cipher_list(con,"RC4-MD5"); */
{
SHUTDOWN(s);
goto end;
}
#ifdef FIONBIO
if (c_nbio)
{
unsigned long l=1;
if (BIO_socket_ioctl(s,FIONBIO,&l) < 0)
{
goto end;
}
}
#endif
{
{
SHUTDOWN(s);
goto end;
}
if ( enable_timeouts)
{
}
if ( mtu > 0)
{
}
else
/* want to do MTU discovery */
}
else
if (nbio_test)
{
}
if (c_debug)
{
}
if (c_msg)
{
}
/* ok, lets connect */
read_tty=1;
write_tty=0;
tty_on=0;
read_ssl=1;
write_ssl=1;
cbuf_len=0;
cbuf_off=0;
sbuf_len=0;
sbuf_off=0;
/* This is an ugly hack that does a lot of assumptions */
if (starttls_proto == 1)
{
}
if (starttls_proto == 2)
{
}
for (;;)
{
{
in_init=1;
tty_on=0;
}
else
{
tty_on=1;
if (in_init)
{
in_init=0;
if (starttls_proto)
{
/* We don't need to know any more */
starttls_proto = 0;
}
if (reconnect)
{
reconnect--;
goto re_start;
}
}
}
if (!ssl_pending)
{
if (tty_on)
{
}
if (read_ssl)
if (write_ssl)
#else
if (read_ssl)
if (write_ssl)
}
#endif
/* printf("mode tty(%d %d%d) ssl(%d%d)\n",
tty_on,read_tty,write_tty,read_ssl,write_ssl);*/
/* Note: under VMS with SOCKETSHR the second parameter
* is currently of type (int *) whereas under other
* systems it is (void *) if you don't have a cast it
* will choke the compiler: if you do have a cast then
* you can either go for (int *) or (void *).
*/
#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
* always write to the tty: therefore if we need to
* write to the tty we just fall through. Otherwise
* we timeout the select every second and see if there
* are any keypresses. Note: this is a hack, in a proper
* Windows application we wouldn't do this.
*/
i=0;
if(!write_tty) {
if(read_tty) {
#if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)
#else
if(!i && (!((_kbhit()) || (WAIT_OBJECT_0 == WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0))) || !read_tty) ) continue;
#endif
}
#elif defined(OPENSSL_SYS_NETWARE)
if(!write_tty) {
if(read_tty) {
}
#else
#endif
if ( i < 0)
{
goto shut;
/* goto end; */
}
}
{
(unsigned int)cbuf_len);
switch (SSL_get_error(con,k))
{
case SSL_ERROR_NONE:
cbuf_off+=k;
cbuf_len-=k;
if (k <= 0) goto end;
/* we have done a write(con,NULL,0); */
if (cbuf_len <= 0)
{
read_tty=1;
write_ssl=0;
}
else /* if (cbuf_len > 0) */
{
read_tty=0;
write_ssl=1;
}
break;
case SSL_ERROR_WANT_WRITE:
write_ssl=1;
read_tty=0;
break;
case SSL_ERROR_WANT_READ:
write_tty=0;
read_ssl=1;
write_ssl=0;
break;
break;
case SSL_ERROR_ZERO_RETURN:
if (cbuf_len != 0)
{
goto shut;
}
else
{
read_tty=1;
write_ssl=0;
break;
}
case SSL_ERROR_SYSCALL:
if ((k != 0) || (cbuf_len != 0))
{
goto shut;
}
else
{
read_tty=1;
write_ssl=0;
}
break;
case SSL_ERROR_SSL:
goto shut;
}
}
else if (!ssl_pending && write_tty)
#else
#endif
{
#ifdef CHARSET_EBCDIC
#endif
if (i <= 0)
{
goto shut;
/* goto end; */
}
sbuf_len-=i;;
sbuf_off+=i;
if (sbuf_len <= 0)
{
read_ssl=1;
write_tty=0;
}
}
{
#ifdef RENEG
#endif
#if 1
#else
/* Demo for pending and peek :-) */
{ char zbuf[10240];
}
#endif
switch (SSL_get_error(con,k))
{
case SSL_ERROR_NONE:
if (k <= 0)
goto end;
sbuf_off=0;
sbuf_len=k;
read_ssl=0;
write_tty=1;
break;
case SSL_ERROR_WANT_WRITE:
write_ssl=1;
read_tty=0;
break;
case SSL_ERROR_WANT_READ:
write_tty=0;
read_ssl=1;
write_ssl=1;
break;
break;
case SSL_ERROR_SYSCALL:
goto shut;
case SSL_ERROR_ZERO_RETURN:
goto shut;
case SSL_ERROR_SSL:
goto shut;
/* break; */
}
}
#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
#if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)
else if (_kbhit())
#else
#endif
#elif defined (OPENSSL_SYS_NETWARE)
else if (_kbhit())
#else
#endif
{
if (crlf)
{
int j, lf_num;
lf_num = 0;
/* both loops are skipped when i <= 0 */
for (j = 0; j < i; j++)
if (cbuf[j] == '\n')
lf_num++;
for (j = i-1; j >= 0; j--)
{
if (cbuf[j] == '\n')
{
lf_num--;
i++;
}
}
}
else
{
goto shut;
}
{
cbuf_len=0;
}
else
{
cbuf_len=i;
cbuf_off=0;
#ifdef CHARSET_EBCDIC
#endif
}
write_ssl=1;
read_tty=0;
}
}
shut:
ret=0;
end:
if (cert)
if (key)
if (pass)
{
}
}
{
char *p;
static const char *space=" ";
SSL_CIPHER *c;
int j,i;
#ifndef OPENSSL_NO_COMP
#endif
if (full)
{
int got_a_chain = 0;
{
for (i=0; i<sk_X509_num(sk); i++)
{
if (c_showcerts)
}
}
{
}
else
{
for (i=0; i<sk_X509_NAME_num(sk2); i++)
{
}
}
else
{
}
if (p != NULL)
{
/* This works only for SSL 2. In later protocol
* versions, the client does not know what other
* ciphers (in addition to the one to be used
* in the current connection) the server supports. */
j=i=0;
while (*p)
{
if (*p == ':')
{
i++;
j=0;
}
else
{
j++;
}
p++;
}
}
}
c=SSL_get_current_cipher(s);
SSL_CIPHER_get_name(c));
}
#ifndef OPENSSL_NO_COMP
#endif
/* flush, or debugging output gets mixed with http response */
}