sl@0: /* apps/s_client.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * openssl-core@openssl.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.openssl.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #ifdef OPENSSL_NO_STDIO sl@0: #define APPS_WIN16 sl@0: #endif sl@0: sl@0: /* With IPv6, it looks like Digital has mixed up the proper order of sl@0: recursive header file inclusion, resulting in the compiler complaining sl@0: that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which sl@0: is needed to have fileno() declared correctly... So let's define u_int */ sl@0: #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT) sl@0: #define __U_INT sl@0: typedef unsigned int u_int; sl@0: #endif sl@0: sl@0: #define USE_SOCKETS sl@0: #include "apps.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include "s_apps.h" sl@0: #include "timeouts.h" sl@0: sl@0: #ifdef OPENSSL_SYS_WINCE sl@0: /* Windows CE incorrectly defines fileno as returning void*, so to avoid problems below... */ sl@0: #ifdef fileno sl@0: #undef fileno sl@0: #endif sl@0: #define fileno(a) (int)_fileno(a) sl@0: #endif sl@0: sl@0: sl@0: #if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000) sl@0: /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */ sl@0: #undef FIONBIO sl@0: #endif sl@0: sl@0: #undef PROG sl@0: #define PROG s_client_main sl@0: sl@0: /*#define SSL_HOST_NAME "www.netscape.com" */ sl@0: /*#define SSL_HOST_NAME "193.118.187.102" */ sl@0: #define SSL_HOST_NAME "localhost" sl@0: sl@0: /*#define TEST_CERT "client.pem" */ /* no default cert. */ sl@0: sl@0: #undef BUFSIZZ sl@0: #define BUFSIZZ 1024*8 sl@0: sl@0: extern int verify_depth; sl@0: extern int verify_error; sl@0: sl@0: #ifdef FIONBIO sl@0: static int c_nbio=0; sl@0: #endif sl@0: static int c_Pause=0; sl@0: static int c_debug=0; sl@0: static int c_msg=0; sl@0: static int c_showcerts=0; sl@0: sl@0: static void sc_usage(void); sl@0: static void print_stuff(BIO *berr,SSL *con,int full); sl@0: static BIO *bio_c_out=NULL; sl@0: static int c_quiet=0; sl@0: static int c_ign_eof=0; sl@0: sl@0: sl@0: static void sc_usage(void) sl@0: { sl@0: BIO_printf(bio_err,"usage: s_client args\n"); sl@0: BIO_printf(bio_err,"\n"); sl@0: BIO_printf(bio_err," -host host - use -connect instead\n"); sl@0: BIO_printf(bio_err," -port port - use -connect instead\n"); sl@0: BIO_printf(bio_err," -connect host:port - who to connect to (default is %s:%s)\n",SSL_HOST_NAME,PORT_STR); sl@0: sl@0: BIO_printf(bio_err," -verify depth - turn on peer certificate verification\n"); sl@0: BIO_printf(bio_err," -cert arg - certificate file to use, PEM format assumed\n"); sl@0: BIO_printf(bio_err," -certform arg - certificate format (PEM or DER) PEM default\n"); sl@0: BIO_printf(bio_err," -key arg - Private key file to use, in cert file if\n"); sl@0: BIO_printf(bio_err," not specified but cert file is.\n"); sl@0: BIO_printf(bio_err," -keyform arg - key format (PEM or DER) PEM default\n"); sl@0: BIO_printf(bio_err," -pass arg - private key file pass phrase source\n"); sl@0: BIO_printf(bio_err," -CApath arg - PEM format directory of CA's\n"); sl@0: BIO_printf(bio_err," -CAfile arg - PEM format file of CA's\n"); sl@0: BIO_printf(bio_err," -reconnect - Drop and re-make the connection with the same Session-ID\n"); sl@0: BIO_printf(bio_err," -pause - sleep(1) after each read(2) and write(2) system call\n"); sl@0: BIO_printf(bio_err," -showcerts - show all certificates in the chain\n"); sl@0: BIO_printf(bio_err," -debug - extra output\n"); sl@0: #ifdef WATT32 sl@0: BIO_printf(bio_err," -wdebug - WATT-32 tcp debugging\n"); sl@0: #endif sl@0: BIO_printf(bio_err," -msg - Show protocol messages\n"); sl@0: BIO_printf(bio_err," -nbio_test - more ssl protocol testing\n"); sl@0: BIO_printf(bio_err," -state - print the 'ssl' states\n"); sl@0: #ifdef FIONBIO sl@0: BIO_printf(bio_err," -nbio - Run with non-blocking IO\n"); sl@0: #endif sl@0: BIO_printf(bio_err," -crlf - convert LF from terminal into CRLF\n"); sl@0: BIO_printf(bio_err," -quiet - no s_client output\n"); sl@0: BIO_printf(bio_err," -ign_eof - ignore input eof (default when -quiet)\n"); sl@0: BIO_printf(bio_err," -ssl2 - just use SSLv2\n"); sl@0: BIO_printf(bio_err," -ssl3 - just use SSLv3\n"); sl@0: BIO_printf(bio_err," -tls1 - just use TLSv1\n"); sl@0: BIO_printf(bio_err," -dtls1 - just use DTLSv1\n"); sl@0: BIO_printf(bio_err," -mtu - set the MTU\n"); sl@0: BIO_printf(bio_err," -no_tls1/-no_ssl3/-no_ssl2 - turn off that protocol\n"); sl@0: BIO_printf(bio_err," -bugs - Switch on all SSL implementation bug workarounds\n"); sl@0: BIO_printf(bio_err," -serverpref - Use server's cipher preferences (only SSLv2)\n"); sl@0: BIO_printf(bio_err," -cipher - preferred cipher to use, use the 'openssl ciphers'\n"); sl@0: BIO_printf(bio_err," command to see what is available\n"); sl@0: BIO_printf(bio_err," -starttls prot - use the STARTTLS command before starting TLS\n"); sl@0: BIO_printf(bio_err," for those protocols that support it, where\n"); sl@0: BIO_printf(bio_err," 'prot' defines which one to assume. Currently,\n"); sl@0: BIO_printf(bio_err," only \"smtp\", \"pop3\", \"imap\", and \"ftp\" are supported.\n"); sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: BIO_printf(bio_err," -engine id - Initialise and use the specified engine\n"); sl@0: #endif sl@0: BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR); sl@0: BIO_printf(bio_err," -sess_out arg - file to write SSL session to\n"); sl@0: BIO_printf(bio_err," -sess_in arg - file to read SSL session from\n"); sl@0: } sl@0: sl@0: enum sl@0: { sl@0: PROTO_OFF = 0, sl@0: PROTO_SMTP, sl@0: PROTO_POP3, sl@0: PROTO_IMAP, sl@0: PROTO_FTP sl@0: }; sl@0: sl@0: int MAIN(int, char **); sl@0: sl@0: int MAIN(int argc, char **argv) sl@0: { sl@0: int off=0; sl@0: SSL *con=NULL,*con2=NULL; sl@0: X509_STORE *store = NULL; sl@0: int s,k,width,state=0; sl@0: char *cbuf=NULL,*sbuf=NULL,*mbuf=NULL; sl@0: int cbuf_len,cbuf_off; sl@0: int sbuf_len,sbuf_off; sl@0: fd_set readfds,writefds; sl@0: short port=PORT; sl@0: int full_log=1; sl@0: char *host=SSL_HOST_NAME; sl@0: char *cert_file=NULL,*key_file=NULL; sl@0: int cert_format = FORMAT_PEM, key_format = FORMAT_PEM; sl@0: char *passarg = NULL, *pass = NULL; sl@0: X509 *cert = NULL; sl@0: EVP_PKEY *key = NULL; sl@0: char *CApath=NULL,*CAfile=NULL,*cipher=NULL; sl@0: int reconnect=0,badop=0,verify=SSL_VERIFY_NONE,bugs=0; sl@0: int crlf=0; sl@0: int write_tty,read_tty,write_ssl,read_ssl,tty_on,ssl_pending; sl@0: SSL_CTX *ctx=NULL; sl@0: int ret=1,in_init=1,i,nbio_test=0; sl@0: int starttls_proto = PROTO_OFF; sl@0: int prexit = 0, vflags = 0; sl@0: SSL_METHOD *meth=NULL; sl@0: #ifdef sock_type sl@0: #undef sock_type sl@0: #endif sl@0: int sock_type=SOCK_STREAM; sl@0: BIO *sbio; sl@0: char *inrand=NULL; sl@0: int mbuf_len=0; sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: char *engine_id=NULL; sl@0: ENGINE *e=NULL; sl@0: #endif sl@0: #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) sl@0: struct timeval tv; sl@0: #endif sl@0: sl@0: char *sess_in = NULL; sl@0: char *sess_out = NULL; sl@0: struct sockaddr peer; sl@0: int peerlen = sizeof(peer); sl@0: int enable_timeouts = 0 ; sl@0: long mtu = 0; sl@0: sl@0: #if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3) sl@0: meth=SSLv23_client_method(); sl@0: #elif !defined(OPENSSL_NO_SSL3) sl@0: meth=SSLv3_client_method(); sl@0: #elif !defined(OPENSSL_NO_SSL2) sl@0: meth=SSLv2_client_method(); sl@0: #endif sl@0: sl@0: apps_startup(); sl@0: c_Pause=0; sl@0: c_quiet=0; sl@0: c_ign_eof=0; sl@0: c_debug=0; sl@0: c_msg=0; sl@0: c_showcerts=0; sl@0: sl@0: if (bio_err == NULL) sl@0: bio_err=BIO_new_fp(stderr,BIO_NOCLOSE); sl@0: sl@0: if (!load_config(bio_err, NULL)) sl@0: goto end; sl@0: sl@0: if ( ((cbuf=OPENSSL_malloc(BUFSIZZ)) == NULL) || sl@0: ((sbuf=OPENSSL_malloc(BUFSIZZ)) == NULL) || sl@0: ((mbuf=OPENSSL_malloc(BUFSIZZ)) == NULL)) sl@0: { sl@0: BIO_printf(bio_err,"out of memory\n"); sl@0: goto end; sl@0: } sl@0: sl@0: verify_depth=0; sl@0: verify_error=X509_V_OK; sl@0: #ifdef FIONBIO sl@0: c_nbio=0; sl@0: #endif sl@0: sl@0: argc--; sl@0: argv++; sl@0: while (argc >= 1) sl@0: { sl@0: if (strcmp(*argv,"-host") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: host= *(++argv); sl@0: } sl@0: else if (strcmp(*argv,"-port") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: port=atoi(*(++argv)); sl@0: if (port == 0) goto bad; sl@0: } sl@0: else if (strcmp(*argv,"-connect") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: if (!extract_host_port(*(++argv),&host,NULL,&port)) sl@0: goto bad; sl@0: } sl@0: else if (strcmp(*argv,"-verify") == 0) sl@0: { sl@0: verify=SSL_VERIFY_PEER; sl@0: if (--argc < 1) goto bad; sl@0: verify_depth=atoi(*(++argv)); sl@0: BIO_printf(bio_err,"verify depth is %d\n",verify_depth); sl@0: } sl@0: else if (strcmp(*argv,"-cert") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: cert_file= *(++argv); sl@0: } sl@0: else if (strcmp(*argv,"-sess_out") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: sess_out = *(++argv); sl@0: } sl@0: else if (strcmp(*argv,"-sess_in") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: sess_in = *(++argv); sl@0: } sl@0: else if (strcmp(*argv,"-certform") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: cert_format = str2fmt(*(++argv)); sl@0: } sl@0: else if (strcmp(*argv,"-crl_check") == 0) sl@0: vflags |= X509_V_FLAG_CRL_CHECK; sl@0: else if (strcmp(*argv,"-crl_check_all") == 0) sl@0: vflags |= X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL; sl@0: else if (strcmp(*argv,"-prexit") == 0) sl@0: prexit=1; sl@0: else if (strcmp(*argv,"-crlf") == 0) sl@0: crlf=1; sl@0: else if (strcmp(*argv,"-quiet") == 0) sl@0: { sl@0: c_quiet=1; sl@0: c_ign_eof=1; sl@0: } sl@0: else if (strcmp(*argv,"-ign_eof") == 0) sl@0: c_ign_eof=1; sl@0: else if (strcmp(*argv,"-pause") == 0) sl@0: c_Pause=1; sl@0: else if (strcmp(*argv,"-debug") == 0) sl@0: c_debug=1; sl@0: #ifdef WATT32 sl@0: else if (strcmp(*argv,"-wdebug") == 0) sl@0: dbug_init(); sl@0: #endif sl@0: else if (strcmp(*argv,"-msg") == 0) sl@0: c_msg=1; sl@0: else if (strcmp(*argv,"-showcerts") == 0) sl@0: c_showcerts=1; sl@0: else if (strcmp(*argv,"-nbio_test") == 0) sl@0: nbio_test=1; sl@0: else if (strcmp(*argv,"-state") == 0) sl@0: state=1; sl@0: #ifndef OPENSSL_NO_SSL2 sl@0: else if (strcmp(*argv,"-ssl2") == 0) sl@0: meth=SSLv2_client_method(); sl@0: #endif sl@0: #ifndef OPENSSL_NO_SSL3 sl@0: else if (strcmp(*argv,"-ssl3") == 0) sl@0: meth=SSLv3_client_method(); sl@0: #endif sl@0: #ifndef OPENSSL_NO_TLS1 sl@0: else if (strcmp(*argv,"-tls1") == 0) sl@0: meth=TLSv1_client_method(); sl@0: #endif sl@0: #ifndef OPENSSL_NO_DTLS1 sl@0: else if (strcmp(*argv,"-dtls1") == 0) sl@0: { sl@0: meth=DTLSv1_client_method(); sl@0: sock_type=SOCK_DGRAM; sl@0: } sl@0: else if (strcmp(*argv,"-timeout") == 0) sl@0: enable_timeouts=1; sl@0: else if (strcmp(*argv,"-mtu") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: mtu = atol(*(++argv)); sl@0: } sl@0: #endif sl@0: else if (strcmp(*argv,"-bugs") == 0) sl@0: bugs=1; sl@0: else if (strcmp(*argv,"-keyform") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: key_format = str2fmt(*(++argv)); sl@0: } sl@0: else if (strcmp(*argv,"-pass") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: passarg = *(++argv); sl@0: } sl@0: else if (strcmp(*argv,"-key") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: key_file= *(++argv); sl@0: } sl@0: else if (strcmp(*argv,"-reconnect") == 0) sl@0: { sl@0: reconnect=5; sl@0: } sl@0: else if (strcmp(*argv,"-CApath") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: CApath= *(++argv); sl@0: } sl@0: else if (strcmp(*argv,"-CAfile") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: CAfile= *(++argv); sl@0: } sl@0: else if (strcmp(*argv,"-no_tls1") == 0) sl@0: off|=SSL_OP_NO_TLSv1; sl@0: else if (strcmp(*argv,"-no_ssl3") == 0) sl@0: off|=SSL_OP_NO_SSLv3; sl@0: else if (strcmp(*argv,"-no_ssl2") == 0) sl@0: off|=SSL_OP_NO_SSLv2; sl@0: else if (strcmp(*argv,"-serverpref") == 0) sl@0: off|=SSL_OP_CIPHER_SERVER_PREFERENCE; sl@0: else if (strcmp(*argv,"-cipher") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: cipher= *(++argv); sl@0: } sl@0: #ifdef FIONBIO sl@0: else if (strcmp(*argv,"-nbio") == 0) sl@0: { c_nbio=1; } sl@0: #endif sl@0: else if (strcmp(*argv,"-starttls") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: ++argv; sl@0: if (strcmp(*argv,"smtp") == 0) sl@0: starttls_proto = PROTO_SMTP; sl@0: else if (strcmp(*argv,"pop3") == 0) sl@0: starttls_proto = PROTO_POP3; sl@0: else if (strcmp(*argv,"imap") == 0) sl@0: starttls_proto = PROTO_IMAP; sl@0: else if (strcmp(*argv,"ftp") == 0) sl@0: starttls_proto = PROTO_FTP; sl@0: else sl@0: goto bad; sl@0: } sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: else if (strcmp(*argv,"-engine") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: engine_id = *(++argv); sl@0: } sl@0: #endif sl@0: else if (strcmp(*argv,"-rand") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: inrand= *(++argv); sl@0: } sl@0: sl@0: else sl@0: { sl@0: BIO_printf(bio_err,"unknown option %s\n",*argv); sl@0: badop=1; sl@0: break; sl@0: } sl@0: argc--; sl@0: argv++; sl@0: } sl@0: if (badop) sl@0: { sl@0: bad: sl@0: sc_usage(); sl@0: goto end; sl@0: } sl@0: sl@0: OpenSSL_add_ssl_algorithms(); sl@0: SSL_load_error_strings(); sl@0: sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: e = setup_engine(bio_err, engine_id, 1); sl@0: #endif sl@0: if (!app_passwd(bio_err, passarg, NULL, &pass, NULL)) sl@0: { sl@0: BIO_printf(bio_err, "Error getting password\n"); sl@0: goto end; sl@0: } sl@0: sl@0: if (key_file == NULL) sl@0: key_file = cert_file; sl@0: sl@0: sl@0: if (key_file) sl@0: sl@0: { sl@0: sl@0: key = load_key(bio_err, key_file, key_format, 0, pass, e, sl@0: "client certificate private key file"); sl@0: if (!key) sl@0: { sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: sl@0: } sl@0: sl@0: if (cert_file) sl@0: sl@0: { sl@0: cert = load_cert(bio_err,cert_file,cert_format, sl@0: NULL, e, "client certificate file"); sl@0: sl@0: if (!cert) sl@0: { sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: } sl@0: sl@0: if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL sl@0: && !RAND_status()) sl@0: { sl@0: BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n"); sl@0: } sl@0: if (inrand != NULL) sl@0: BIO_printf(bio_err,"%ld semi-random bytes loaded\n", sl@0: app_RAND_load_files(inrand)); sl@0: sl@0: if (bio_c_out == NULL) sl@0: { sl@0: if (c_quiet && !c_debug && !c_msg) sl@0: { sl@0: bio_c_out=BIO_new(BIO_s_null()); sl@0: } sl@0: else sl@0: { sl@0: if (bio_c_out == NULL) sl@0: bio_c_out=BIO_new_fp(stdout,BIO_NOCLOSE); sl@0: sl@0: } sl@0: } sl@0: sl@0: ctx=SSL_CTX_new(meth); sl@0: if (ctx == NULL) sl@0: { sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: sl@0: if (bugs) sl@0: SSL_CTX_set_options(ctx,SSL_OP_ALL|off); sl@0: else sl@0: SSL_CTX_set_options(ctx,off); sl@0: /* DTLS: partial reads end up discarding unread UDP bytes :-( sl@0: * Setting read ahead solves this problem. sl@0: */ sl@0: if (sock_type == SOCK_DGRAM) SSL_CTX_set_read_ahead(ctx, 1); sl@0: sl@0: if (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback); sl@0: if (cipher != NULL) sl@0: if(!SSL_CTX_set_cipher_list(ctx,cipher)) { sl@0: BIO_printf(bio_err,"error setting cipher list\n"); sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: #if 0 sl@0: else sl@0: SSL_CTX_set_cipher_list(ctx,getenv("SSL_CIPHER")); sl@0: #endif sl@0: sl@0: SSL_CTX_set_verify(ctx,verify,verify_callback); sl@0: if (!set_cert_key_stuff(ctx,cert,key)) sl@0: goto end; sl@0: sl@0: if ((!SSL_CTX_load_verify_locations(ctx,CAfile,CApath)) || sl@0: (!SSL_CTX_set_default_verify_paths(ctx))) sl@0: { sl@0: /* BIO_printf(bio_err,"error setting default verify locations\n"); */ sl@0: ERR_print_errors(bio_err); sl@0: /* goto end; */ sl@0: } sl@0: sl@0: store = SSL_CTX_get_cert_store(ctx); sl@0: X509_STORE_set_flags(store, vflags); sl@0: sl@0: con=SSL_new(ctx); sl@0: if (sess_in) sl@0: { sl@0: SSL_SESSION *sess; sl@0: BIO *stmp = BIO_new_file(sess_in, "r"); sl@0: if (!stmp) sl@0: { sl@0: BIO_printf(bio_err, "Can't open session file %s\n", sl@0: sess_in); sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: sess = PEM_read_bio_SSL_SESSION(stmp, NULL, 0, NULL); sl@0: BIO_free(stmp); sl@0: if (!sess) sl@0: { sl@0: BIO_printf(bio_err, "Can't open session file %s\n", sl@0: sess_in); sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: SSL_set_session(con, sess); sl@0: SSL_SESSION_free(sess); sl@0: } sl@0: sl@0: #ifndef OPENSSL_NO_KRB5 sl@0: if (con && (con->kssl_ctx = kssl_ctx_new()) != NULL) sl@0: { sl@0: kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVER, host); sl@0: } sl@0: #endif /* OPENSSL_NO_KRB5 */ sl@0: /* SSL_set_cipher_list(con,"RC4-MD5"); */ sl@0: sl@0: re_start: sl@0: sl@0: if (init_client(&s,host,port,sock_type) == 0) sl@0: { sl@0: BIO_printf(bio_err,"connect:errno=%d\n",get_last_socket_error()); sl@0: SHUTDOWN(s); sl@0: goto end; sl@0: } sl@0: BIO_printf(bio_c_out,"CONNECTED(%08X)\n",s); sl@0: sl@0: #ifdef FIONBIO sl@0: if (c_nbio) sl@0: { sl@0: unsigned long l=1; sl@0: BIO_printf(bio_c_out,"turning on non blocking io\n"); sl@0: if (BIO_socket_ioctl(s,FIONBIO,&l) < 0) sl@0: { sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: } sl@0: #endif sl@0: if (c_Pause & 0x01) con->debug=1; sl@0: sl@0: if ( SSL_version(con) == DTLS1_VERSION) sl@0: { sl@0: struct timeval timeout; sl@0: sl@0: sbio=BIO_new_dgram(s,BIO_NOCLOSE); sl@0: if (getsockname(s, &peer, (void *)&peerlen) < 0) sl@0: { sl@0: BIO_printf(bio_err, "getsockname:errno=%d\n", sl@0: get_last_socket_error()); sl@0: SHUTDOWN(s); sl@0: goto end; sl@0: } sl@0: sl@0: (void)BIO_ctrl_set_connected(sbio, 1, &peer); sl@0: sl@0: if ( enable_timeouts) sl@0: { sl@0: timeout.tv_sec = 0; sl@0: timeout.tv_usec = DGRAM_RCV_TIMEOUT; sl@0: BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout); sl@0: sl@0: timeout.tv_sec = 0; sl@0: timeout.tv_usec = DGRAM_SND_TIMEOUT; sl@0: BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout); sl@0: } sl@0: sl@0: if ( mtu > 0) sl@0: { sl@0: SSL_set_options(con, SSL_OP_NO_QUERY_MTU); sl@0: SSL_set_mtu(con, mtu); sl@0: } sl@0: else sl@0: /* want to do MTU discovery */ sl@0: BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL); sl@0: } sl@0: else sl@0: sbio=BIO_new_socket(s,BIO_NOCLOSE); sl@0: sl@0: sl@0: sl@0: if (nbio_test) sl@0: { sl@0: BIO *test; sl@0: sl@0: test=BIO_new(BIO_f_nbio_test()); sl@0: sbio=BIO_push(test,sbio); sl@0: } sl@0: sl@0: if (c_debug) sl@0: { sl@0: con->debug=1; sl@0: BIO_set_callback(sbio,bio_dump_callback); sl@0: BIO_set_callback_arg(sbio,(char *)bio_c_out); sl@0: } sl@0: if (c_msg) sl@0: { sl@0: SSL_set_msg_callback(con, msg_cb); sl@0: SSL_set_msg_callback_arg(con, bio_c_out); sl@0: } sl@0: sl@0: SSL_set_bio(con,sbio,sbio); sl@0: SSL_set_connect_state(con); sl@0: sl@0: /* ok, lets connect */ sl@0: width=SSL_get_fd(con)+1; sl@0: sl@0: read_tty=1; sl@0: write_tty=0; sl@0: tty_on=0; sl@0: read_ssl=1; sl@0: write_ssl=1; sl@0: sl@0: cbuf_len=0; sl@0: cbuf_off=0; sl@0: sbuf_len=0; sl@0: sbuf_off=0; sl@0: sl@0: /* This is an ugly hack that does a lot of assumptions */ sl@0: /* We do have to handle multi-line responses which may come sl@0: in a single packet or not. We therefore have to use sl@0: BIO_gets() which does need a buffering BIO. So during sl@0: the initial chitchat we do push a buffering BIO into the sl@0: chain that is removed again later on to not disturb the sl@0: rest of the s_client operation. */ sl@0: if (starttls_proto == PROTO_SMTP) sl@0: { sl@0: int foundit=0; sl@0: BIO *fbio = BIO_new(BIO_f_buffer()); sl@0: BIO_push(fbio, sbio); sl@0: /* wait for multi-line response to end from SMTP */ sl@0: do sl@0: { sl@0: mbuf_len = BIO_gets(fbio,mbuf,BUFSIZZ); sl@0: } sl@0: while (mbuf_len>3 && mbuf[3]=='-'); sl@0: /* STARTTLS command requires EHLO... */ sl@0: BIO_printf(fbio,"EHLO openssl.client.net\r\n"); sl@0: (void)BIO_flush(fbio); sl@0: /* wait for multi-line response to end EHLO SMTP response */ sl@0: do sl@0: { sl@0: mbuf_len = BIO_gets(fbio,mbuf,BUFSIZZ); sl@0: if (strstr(mbuf,"STARTTLS")) sl@0: foundit=1; sl@0: } sl@0: while (mbuf_len>3 && mbuf[3]=='-'); sl@0: (void)BIO_flush(fbio); sl@0: BIO_pop(fbio); sl@0: BIO_free(fbio); sl@0: if (!foundit) sl@0: BIO_printf(bio_err, sl@0: "didn't found starttls in server response," sl@0: " try anyway...\n"); sl@0: BIO_printf(sbio,"STARTTLS\r\n"); sl@0: BIO_read(sbio,sbuf,BUFSIZZ); sl@0: } sl@0: else if (starttls_proto == PROTO_POP3) sl@0: { sl@0: BIO_read(sbio,mbuf,BUFSIZZ); sl@0: BIO_printf(sbio,"STLS\r\n"); sl@0: BIO_read(sbio,sbuf,BUFSIZZ); sl@0: } sl@0: else if (starttls_proto == PROTO_IMAP) sl@0: { sl@0: int foundit=0; sl@0: BIO *fbio = BIO_new(BIO_f_buffer()); sl@0: BIO_push(fbio, sbio); sl@0: BIO_gets(fbio,mbuf,BUFSIZZ); sl@0: /* STARTTLS command requires CAPABILITY... */ sl@0: BIO_printf(fbio,". CAPABILITY\r\n"); sl@0: (void)BIO_flush(fbio); sl@0: /* wait for multi-line CAPABILITY response */ sl@0: do sl@0: { sl@0: mbuf_len = BIO_gets(fbio,mbuf,BUFSIZZ); sl@0: if (strstr(mbuf,"STARTTLS")) sl@0: foundit=1; sl@0: } sl@0: while (mbuf_len>3 && mbuf[0]!='.'); sl@0: (void)BIO_flush(fbio); sl@0: BIO_pop(fbio); sl@0: BIO_free(fbio); sl@0: if (!foundit) sl@0: BIO_printf(bio_err, sl@0: "didn't found STARTTLS in server response," sl@0: " try anyway...\n"); sl@0: BIO_printf(sbio,". STARTTLS\r\n"); sl@0: BIO_read(sbio,sbuf,BUFSIZZ); sl@0: } sl@0: else if (starttls_proto == PROTO_FTP) sl@0: { sl@0: BIO *fbio = BIO_new(BIO_f_buffer()); sl@0: BIO_push(fbio, sbio); sl@0: /* wait for multi-line response to end from FTP */ sl@0: do sl@0: { sl@0: mbuf_len = BIO_gets(fbio,mbuf,BUFSIZZ); sl@0: } sl@0: while (mbuf_len>3 && mbuf[3]=='-'); sl@0: (void)BIO_flush(fbio); sl@0: BIO_pop(fbio); sl@0: BIO_free(fbio); sl@0: BIO_printf(sbio,"AUTH TLS\r\n"); sl@0: BIO_read(sbio,sbuf,BUFSIZZ); sl@0: } sl@0: sl@0: for (;;) sl@0: { sl@0: FD_ZERO(&readfds); sl@0: FD_ZERO(&writefds); sl@0: sl@0: if (SSL_in_init(con) && !SSL_total_renegotiations(con)) sl@0: { sl@0: in_init=1; sl@0: tty_on=0; sl@0: } sl@0: else sl@0: { sl@0: tty_on=1; sl@0: if (in_init) sl@0: { sl@0: in_init=0; sl@0: if (sess_out) sl@0: { sl@0: BIO *stmp = BIO_new_file(sess_out, "w"); sl@0: if (stmp) sl@0: { sl@0: PEM_write_bio_SSL_SESSION(stmp, SSL_get_session(con)); sl@0: BIO_free(stmp); sl@0: } sl@0: else sl@0: BIO_printf(bio_err, "Error writing session file %s\n", sess_out); sl@0: } sl@0: print_stuff(bio_c_out,con,full_log); sl@0: if (full_log > 0) full_log--; sl@0: sl@0: if (starttls_proto) sl@0: { sl@0: BIO_printf(bio_err,"%s",mbuf); sl@0: /* We don't need to know any more */ sl@0: starttls_proto = PROTO_OFF; sl@0: } sl@0: sl@0: if (reconnect) sl@0: { sl@0: reconnect--; sl@0: BIO_printf(bio_c_out,"drop connection and then reconnect\n"); sl@0: SSL_shutdown(con); sl@0: SSL_set_connect_state(con); sl@0: SHUTDOWN(SSL_get_fd(con)); sl@0: goto re_start; sl@0: } sl@0: } sl@0: } sl@0: sl@0: ssl_pending = read_ssl && SSL_pending(con); sl@0: sl@0: if (!ssl_pending) sl@0: { sl@0: #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE) sl@0: if (tty_on) sl@0: { sl@0: if (read_tty) FD_SET(fileno(stdin),&readfds); sl@0: if (write_tty) FD_SET(fileno(stdout),&writefds); sl@0: sl@0: } sl@0: if (read_ssl) sl@0: FD_SET(SSL_get_fd(con),&readfds); sl@0: if (write_ssl) sl@0: FD_SET(SSL_get_fd(con),&writefds); sl@0: #else sl@0: if(!tty_on || !write_tty) { sl@0: if (read_ssl) sl@0: FD_SET(SSL_get_fd(con),&readfds); sl@0: if (write_ssl) sl@0: FD_SET(SSL_get_fd(con),&writefds); sl@0: } sl@0: #endif sl@0: /* printf("mode tty(%d %d%d) ssl(%d%d)\n", sl@0: tty_on,read_tty,write_tty,read_ssl,write_ssl);*/ sl@0: sl@0: /* Note: under VMS with SOCKETSHR the second parameter sl@0: * is currently of type (int *) whereas under other sl@0: * systems it is (void *) if you don't have a cast it sl@0: * will choke the compiler: if you do have a cast then sl@0: * you can either go for (int *) or (void *). sl@0: */ sl@0: #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) sl@0: /* Under Windows/DOS we make the assumption that we can sl@0: * always write to the tty: therefore if we need to sl@0: * write to the tty we just fall through. Otherwise sl@0: * we timeout the select every second and see if there sl@0: * are any keypresses. Note: this is a hack, in a proper sl@0: * Windows application we wouldn't do this. sl@0: */ sl@0: i=0; sl@0: if(!write_tty) { sl@0: if(read_tty) { sl@0: tv.tv_sec = 1; sl@0: tv.tv_usec = 0; sl@0: i=select(width,(void *)&readfds,(void *)&writefds, sl@0: NULL,&tv); sl@0: #if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS) sl@0: if(!i && (!_kbhit() || !read_tty) ) continue; sl@0: #else sl@0: if(!i && (!((_kbhit()) || (WAIT_OBJECT_0 == WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0))) || !read_tty) ) continue; sl@0: #endif sl@0: } else i=select(width,(void *)&readfds,(void *)&writefds, sl@0: NULL,NULL); sl@0: } sl@0: #elif defined(OPENSSL_SYS_NETWARE) sl@0: if(!write_tty) { sl@0: if(read_tty) { sl@0: tv.tv_sec = 1; sl@0: tv.tv_usec = 0; sl@0: i=select(width,(void *)&readfds,(void *)&writefds, sl@0: NULL,&tv); sl@0: } else i=select(width,(void *)&readfds,(void *)&writefds, sl@0: NULL,NULL); sl@0: } sl@0: #else sl@0: i=select(width,(void *)&readfds,(void *)&writefds, sl@0: NULL,NULL); sl@0: #endif sl@0: if ( i < 0) sl@0: { sl@0: BIO_printf(bio_err,"bad select %d\n", sl@0: get_last_socket_error()); sl@0: goto shut; sl@0: /* goto end; */ sl@0: } sl@0: } sl@0: sl@0: if (!ssl_pending && FD_ISSET(SSL_get_fd(con),&writefds)) sl@0: { sl@0: k=SSL_write(con,&(cbuf[cbuf_off]), sl@0: (unsigned int)cbuf_len); sl@0: switch (SSL_get_error(con,k)) sl@0: { sl@0: case SSL_ERROR_NONE: sl@0: cbuf_off+=k; sl@0: cbuf_len-=k; sl@0: if (k <= 0) goto end; sl@0: /* we have done a write(con,NULL,0); */ sl@0: if (cbuf_len <= 0) sl@0: { sl@0: read_tty=1; sl@0: write_ssl=0; sl@0: } sl@0: else /* if (cbuf_len > 0) */ sl@0: { sl@0: read_tty=0; sl@0: write_ssl=1; sl@0: } sl@0: break; sl@0: case SSL_ERROR_WANT_WRITE: sl@0: BIO_printf(bio_c_out,"write W BLOCK\n"); sl@0: write_ssl=1; sl@0: read_tty=0; sl@0: break; sl@0: case SSL_ERROR_WANT_READ: sl@0: BIO_printf(bio_c_out,"write R BLOCK\n"); sl@0: write_tty=0; sl@0: read_ssl=1; sl@0: write_ssl=0; sl@0: break; sl@0: case SSL_ERROR_WANT_X509_LOOKUP: sl@0: BIO_printf(bio_c_out,"write X BLOCK\n"); sl@0: break; sl@0: case SSL_ERROR_ZERO_RETURN: sl@0: if (cbuf_len != 0) sl@0: { sl@0: BIO_printf(bio_c_out,"shutdown\n"); sl@0: goto shut; sl@0: } sl@0: else sl@0: { sl@0: read_tty=1; sl@0: write_ssl=0; sl@0: break; sl@0: } sl@0: sl@0: case SSL_ERROR_SYSCALL: sl@0: if ((k != 0) || (cbuf_len != 0)) sl@0: { sl@0: BIO_printf(bio_err,"write:errno=%d\n", sl@0: get_last_socket_error()); sl@0: goto shut; sl@0: } sl@0: else sl@0: { sl@0: read_tty=1; sl@0: write_ssl=0; sl@0: } sl@0: break; sl@0: case SSL_ERROR_SSL: sl@0: ERR_print_errors(bio_err); sl@0: goto shut; sl@0: } sl@0: } sl@0: #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) sl@0: /* Assume Windows/DOS can always write */ sl@0: else if (!ssl_pending && write_tty) sl@0: #else sl@0: sl@0: else if (!ssl_pending && FD_ISSET(fileno(stdout),&writefds)) sl@0: sl@0: #endif sl@0: { sl@0: #ifdef CHARSET_EBCDIC sl@0: ascii2ebcdic(&(sbuf[sbuf_off]),&(sbuf[sbuf_off]),sbuf_len); sl@0: #endif sl@0: sl@0: i=write(fileno(stdout),&(sbuf[sbuf_off]),sbuf_len); sl@0: sl@0: if (i <= 0) sl@0: { sl@0: BIO_printf(bio_c_out,"DONE\n"); sl@0: goto shut; sl@0: /* goto end; */ sl@0: } sl@0: sl@0: sbuf_len-=i;; sl@0: sbuf_off+=i; sl@0: if (sbuf_len <= 0) sl@0: { sl@0: read_ssl=1; sl@0: write_tty=0; sl@0: } sl@0: } sl@0: else if (ssl_pending || FD_ISSET(SSL_get_fd(con),&readfds)) sl@0: { sl@0: #ifdef RENEG sl@0: { static int iiii; if (++iiii == 52) { SSL_renegotiate(con); iiii=0; } } sl@0: #endif sl@0: #if 1 sl@0: k=SSL_read(con,sbuf,1024 /* BUFSIZZ */ ); sl@0: #else sl@0: /* Demo for pending and peek :-) */ sl@0: k=SSL_read(con,sbuf,16); sl@0: { char zbuf[10240]; sl@0: printf("read=%d pending=%d peek=%d\n",k,SSL_pending(con),SSL_peek(con,zbuf,10240)); sl@0: sl@0: } sl@0: #endif sl@0: sl@0: switch (SSL_get_error(con,k)) sl@0: { sl@0: case SSL_ERROR_NONE: sl@0: if (k <= 0) sl@0: goto end; sl@0: sbuf_off=0; sl@0: sbuf_len=k; sl@0: sl@0: read_ssl=0; sl@0: write_tty=1; sl@0: break; sl@0: case SSL_ERROR_WANT_WRITE: sl@0: BIO_printf(bio_c_out,"read W BLOCK\n"); sl@0: write_ssl=1; sl@0: read_tty=0; sl@0: break; sl@0: case SSL_ERROR_WANT_READ: sl@0: BIO_printf(bio_c_out,"read R BLOCK\n"); sl@0: write_tty=0; sl@0: read_ssl=1; sl@0: if ((read_tty == 0) && (write_ssl == 0)) sl@0: write_ssl=1; sl@0: break; sl@0: case SSL_ERROR_WANT_X509_LOOKUP: sl@0: BIO_printf(bio_c_out,"read X BLOCK\n"); sl@0: break; sl@0: case SSL_ERROR_SYSCALL: sl@0: BIO_printf(bio_err,"read:errno=%d\n",get_last_socket_error()); sl@0: goto shut; sl@0: case SSL_ERROR_ZERO_RETURN: sl@0: BIO_printf(bio_c_out,"closed\n"); sl@0: goto shut; sl@0: case SSL_ERROR_SSL: sl@0: ERR_print_errors(bio_err); sl@0: goto shut; sl@0: /* break; */ sl@0: } sl@0: } sl@0: sl@0: #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) sl@0: #if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS) sl@0: else if (_kbhit()) sl@0: #else sl@0: else if ((_kbhit()) || (WAIT_OBJECT_0 == WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0))) sl@0: #endif sl@0: #elif defined (OPENSSL_SYS_NETWARE) sl@0: else if (_kbhit()) sl@0: #else sl@0: else if (FD_ISSET(fileno(stdin),&readfds)) sl@0: sl@0: #endif sl@0: { sl@0: if (crlf) sl@0: { sl@0: int j, lf_num; sl@0: i=read(fileno(stdin),cbuf,BUFSIZZ/2); sl@0: sl@0: lf_num = 0; sl@0: /* both loops are skipped when i <= 0 */ sl@0: for (j = 0; j < i; j++) sl@0: if (cbuf[j] == '\n') sl@0: lf_num++; sl@0: for (j = i-1; j >= 0; j--) sl@0: { sl@0: cbuf[j+lf_num] = cbuf[j]; sl@0: if (cbuf[j] == '\n') sl@0: { sl@0: lf_num--; sl@0: i++; sl@0: cbuf[j+lf_num] = '\r'; sl@0: } sl@0: } sl@0: assert(lf_num == 0); sl@0: } sl@0: else sl@0: i=read(fileno(stdin),cbuf,BUFSIZZ); sl@0: sl@0: if ((!c_ign_eof) && ((i <= 0) || (cbuf[0] == 'Q'))) sl@0: { sl@0: BIO_printf(bio_err,"DONE\n"); sl@0: goto shut; sl@0: } sl@0: sl@0: if ((!c_ign_eof) && (cbuf[0] == 'R')) sl@0: { sl@0: BIO_printf(bio_err,"RENEGOTIATING\n"); sl@0: SSL_renegotiate(con); sl@0: cbuf_len=0; sl@0: } sl@0: else sl@0: { sl@0: cbuf_len=i; sl@0: cbuf_off=0; sl@0: #ifdef CHARSET_EBCDIC sl@0: ebcdic2ascii(cbuf, cbuf, i); sl@0: #endif sl@0: } sl@0: sl@0: write_ssl=1; sl@0: read_tty=0; sl@0: } sl@0: } sl@0: shut: sl@0: SSL_shutdown(con); sl@0: SHUTDOWN(SSL_get_fd(con)); sl@0: ret=0; sl@0: end: sl@0: if(prexit) print_stuff(bio_c_out,con,1); sl@0: if (con != NULL) SSL_free(con); sl@0: if (con2 != NULL) SSL_free(con2); sl@0: if (ctx != NULL) SSL_CTX_free(ctx); sl@0: if (cert) sl@0: X509_free(cert); sl@0: if (key) sl@0: EVP_PKEY_free(key); sl@0: if (pass) sl@0: OPENSSL_free(pass); sl@0: if (cbuf != NULL) { OPENSSL_cleanse(cbuf,BUFSIZZ); OPENSSL_free(cbuf); } sl@0: if (sbuf != NULL) { OPENSSL_cleanse(sbuf,BUFSIZZ); OPENSSL_free(sbuf); } sl@0: if (mbuf != NULL) { OPENSSL_cleanse(mbuf,BUFSIZZ); OPENSSL_free(mbuf); } sl@0: if (bio_c_out != NULL) sl@0: { sl@0: BIO_free(bio_c_out); sl@0: bio_c_out=NULL; sl@0: } sl@0: apps_shutdown(); sl@0: OPENSSL_EXIT(ret); sl@0: } sl@0: sl@0: sl@0: static void print_stuff(BIO *bio, SSL *s, int full) sl@0: { sl@0: X509 *peer=NULL; sl@0: char *p; sl@0: static const char *space=" "; sl@0: char buf[BUFSIZ]; sl@0: STACK_OF(X509) *sk; sl@0: STACK_OF(X509_NAME) *sk2; sl@0: SSL_CIPHER *c; sl@0: X509_NAME *xn; sl@0: int j,i; sl@0: #ifndef OPENSSL_NO_COMP sl@0: const COMP_METHOD *comp, *expansion; sl@0: #endif sl@0: sl@0: if (full) sl@0: { sl@0: int got_a_chain = 0; sl@0: sl@0: sk=SSL_get_peer_cert_chain(s); sl@0: if (sk != NULL) sl@0: { sl@0: got_a_chain = 1; /* we don't have it for SSL2 (yet) */ sl@0: sl@0: BIO_printf(bio,"---\nCertificate chain\n"); sl@0: for (i=0; i 0)) sl@0: { sl@0: BIO_printf(bio,"---\nAcceptable client certificate CA names\n"); sl@0: for (i=0; ihit)?"---\nReused, ":"---\nNew, ")); sl@0: c=SSL_get_current_cipher(s); sl@0: BIO_printf(bio,"%s, Cipher is %s\n", sl@0: SSL_CIPHER_get_version(c), sl@0: SSL_CIPHER_get_name(c)); sl@0: if (peer != NULL) { sl@0: EVP_PKEY *pktmp; sl@0: pktmp = X509_get_pubkey(peer); sl@0: BIO_printf(bio,"Server public key is %d bit\n", sl@0: EVP_PKEY_bits(pktmp)); sl@0: EVP_PKEY_free(pktmp); sl@0: } sl@0: #ifndef OPENSSL_NO_COMP sl@0: comp=SSL_get_current_compression(s); sl@0: expansion=SSL_get_current_expansion(s); sl@0: BIO_printf(bio,"Compression: %s\n", sl@0: comp ? SSL_COMP_get_name(comp) : "NONE"); sl@0: BIO_printf(bio,"Expansion: %s\n", sl@0: expansion ? SSL_COMP_get_name(expansion) : "NONE"); sl@0: #endif sl@0: SSL_SESSION_print(bio,SSL_get_session(s)); sl@0: BIO_printf(bio,"---\n"); sl@0: if (peer != NULL) sl@0: X509_free(peer); sl@0: /* flush, or debugging output gets mixed with http response */ sl@0: (void)BIO_flush(bio); sl@0: } sl@0: