Main Page | Class List | Directories | File List | Class Members | File Members

s_client.c

Go to the documentation of this file.
00001 /* apps/s_client.c */
00002 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
00003  * All rights reserved.
00004  *
00005  * This package is an SSL implementation written
00006  * by Eric Young (eay@cryptsoft.com).
00007  * The implementation was written so as to conform with Netscapes SSL.
00008  * 
00009  * This library is free for commercial and non-commercial use as long as
00010  * the following conditions are aheared to.  The following conditions
00011  * apply to all code found in this distribution, be it the RC4, RSA,
00012  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
00013  * included with this distribution is covered by the same copyright terms
00014  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
00015  * 
00016  * Copyright remains Eric Young's, and as such any Copyright notices in
00017  * the code are not to be removed.
00018  * If this package is used in a product, Eric Young should be given attribution
00019  * as the author of the parts of the library used.
00020  * This can be in the form of a textual message at program startup or
00021  * in documentation (online or textual) provided with the package.
00022  * 
00023  * Redistribution and use in source and binary forms, with or without
00024  * modification, are permitted provided that the following conditions
00025  * are met:
00026  * 1. Redistributions of source code must retain the copyright
00027  *    notice, this list of conditions and the following disclaimer.
00028  * 2. Redistributions in binary form must reproduce the above copyright
00029  *    notice, this list of conditions and the following disclaimer in the
00030  *    documentation and/or other materials provided with the distribution.
00031  * 3. All advertising materials mentioning features or use of this software
00032  *    must display the following acknowledgement:
00033  *    "This product includes cryptographic software written by
00034  *     Eric Young (eay@cryptsoft.com)"
00035  *    The word 'cryptographic' can be left out if the rouines from the library
00036  *    being used are not cryptographic related :-).
00037  * 4. If you include any Windows specific code (or a derivative thereof) from 
00038  *    the apps directory (application code) you must include an acknowledgement:
00039  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
00040  * 
00041  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
00042  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00043  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00044  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00045  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00046  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00047  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00048  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00049  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00050  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00051  * SUCH DAMAGE.
00052  * 
00053  * The licence and distribution terms for any publically available version or
00054  * derivative of this code cannot be changed.  i.e. this code cannot simply be
00055  * copied and put under another distribution licence
00056  * [including the GNU Public Licence.]
00057  */
00058 /* ====================================================================
00059  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
00060  *
00061  * Redistribution and use in source and binary forms, with or without
00062  * modification, are permitted provided that the following conditions
00063  * are met:
00064  *
00065  * 1. Redistributions of source code must retain the above copyright
00066  *    notice, this list of conditions and the following disclaimer. 
00067  *
00068  * 2. Redistributions in binary form must reproduce the above copyright
00069  *    notice, this list of conditions and the following disclaimer in
00070  *    the documentation and/or other materials provided with the
00071  *    distribution.
00072  *
00073  * 3. All advertising materials mentioning features or use of this
00074  *    software must display the following acknowledgment:
00075  *    "This product includes software developed by the OpenSSL Project
00076  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
00077  *
00078  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
00079  *    endorse or promote products derived from this software without
00080  *    prior written permission. For written permission, please contact
00081  *    openssl-core@openssl.org.
00082  *
00083  * 5. Products derived from this software may not be called "OpenSSL"
00084  *    nor may "OpenSSL" appear in their names without prior written
00085  *    permission of the OpenSSL Project.
00086  *
00087  * 6. Redistributions of any form whatsoever must retain the following
00088  *    acknowledgment:
00089  *    "This product includes software developed by the OpenSSL Project
00090  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
00091  *
00092  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
00093  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00094  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00095  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
00096  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00097  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00098  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00099  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00100  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00101  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00102  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
00103  * OF THE POSSIBILITY OF SUCH DAMAGE.
00104  * ====================================================================
00105  *
00106  * This product includes cryptographic software written by Eric Young
00107  * (eay@cryptsoft.com).  This product includes software written by Tim
00108  * Hudson (tjh@cryptsoft.com).
00109  *
00110  */
00111 
00112 #include <assert.h>
00113 #include <stdio.h>
00114 #include <stdlib.h>
00115 #include <string.h>
00116 #include <openssl/e_os2.h>
00117 #ifdef OPENSSL_NO_STDIO
00118 #define APPS_WIN16
00119 #endif
00120 
00121 /* With IPv6, it looks like Digital has mixed up the proper order of
00122    recursive header file inclusion, resulting in the compiler complaining
00123    that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which
00124    is needed to have fileno() declared correctly...  So let's define u_int */
00125 #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
00126 #define __U_INT
00127 typedef unsigned int u_int;
00128 #endif
00129 
00130 #define USE_SOCKETS
00131 #include "apps.h"
00132 #include <openssl/x509.h>
00133 #include <openssl/ssl.h>
00134 #include <openssl/err.h>
00135 #include <openssl/pem.h>
00136 #include <openssl/rand.h>
00137 #include "s_apps.h"
00138 #include "timeouts.h"
00139 
00140 #ifdef OPENSSL_SYS_WINCE
00141 /* Windows CE incorrectly defines fileno as returning void*, so to avoid problems below... */
00142 #ifdef fileno
00143 #undef fileno
00144 #endif
00145 #define fileno(a) (int)_fileno(a)
00146 #endif
00147 
00148 
00149 #if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
00150 /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
00151 #undef FIONBIO
00152 #endif
00153 
00154 #undef PROG
00155 #define PROG    s_client_main
00156 
00157 /*#define SSL_HOST_NAME "www.netscape.com" */
00158 /*#define SSL_HOST_NAME "193.118.187.102" */
00159 #define SSL_HOST_NAME   "localhost"
00160 
00161 /*#define TEST_CERT "client.pem" */ /* no default cert. */
00162 
00163 #undef BUFSIZZ
00164 #define BUFSIZZ 1024*8
00165 
00166 extern int verify_depth;
00167 extern int verify_error;
00168 
00169 #ifdef FIONBIO
00170 static int c_nbio=0;
00171 #endif
00172 static int c_Pause=0;
00173 static int c_debug=0;
00174 static int c_msg=0;
00175 static int c_showcerts=0;
00176 
00177 static void sc_usage(void);
00178 static void print_stuff(BIO *berr,SSL *con,int full);
00179 static BIO *bio_c_out=NULL;
00180 static int c_quiet=0;
00181 static int c_ign_eof=0;
00182 
00183 static void sc_usage(void)
00184         {
00185         BIO_printf(bio_err,"usage: s_client args\n");
00186         BIO_printf(bio_err,"\n");
00187         BIO_printf(bio_err," -host host     - use -connect instead\n");
00188         BIO_printf(bio_err," -port port     - use -connect instead\n");
00189         BIO_printf(bio_err," -connect host:port - who to connect to (default is %s:%s)\n",SSL_HOST_NAME,PORT_STR);
00190 
00191         BIO_printf(bio_err," -verify depth - turn on peer certificate verification\n");
00192         BIO_printf(bio_err," -cert arg     - certificate file to use, PEM format assumed\n");
00193         BIO_printf(bio_err," -certform arg - certificate format (PEM or DER) PEM default\n");
00194         BIO_printf(bio_err," -key arg      - Private key file to use, in cert file if\n");
00195         BIO_printf(bio_err,"                 not specified but cert file is.\n");
00196         BIO_printf(bio_err," -keyform arg  - key format (PEM or DER) PEM default\n");
00197         BIO_printf(bio_err," -pass arg     - private key file pass phrase source\n");
00198         BIO_printf(bio_err," -CApath arg   - PEM format directory of CA's\n");
00199         BIO_printf(bio_err," -CAfile arg   - PEM format file of CA's\n");
00200         BIO_printf(bio_err," -reconnect    - Drop and re-make the connection with the same Session-ID\n");
00201         BIO_printf(bio_err," -pause        - sleep(1) after each read(2) and write(2) system call\n");
00202         BIO_printf(bio_err," -showcerts    - show all certificates in the chain\n");
00203         BIO_printf(bio_err," -debug        - extra output\n");
00204 #ifdef WATT32
00205         BIO_printf(bio_err," -wdebug       - WATT-32 tcp debugging\n");
00206 #endif
00207         BIO_printf(bio_err," -msg          - Show protocol messages\n");
00208         BIO_printf(bio_err," -nbio_test    - more ssl protocol testing\n");
00209         BIO_printf(bio_err," -state        - print the 'ssl' states\n");
00210 #ifdef FIONBIO
00211         BIO_printf(bio_err," -nbio         - Run with non-blocking IO\n");
00212 #endif
00213         BIO_printf(bio_err," -crlf         - convert LF from terminal into CRLF\n");
00214         BIO_printf(bio_err," -quiet        - no s_client output\n");
00215         BIO_printf(bio_err," -ign_eof      - ignore input eof (default when -quiet)\n");
00216         BIO_printf(bio_err," -ssl2         - just use SSLv2\n");
00217         BIO_printf(bio_err," -ssl3         - just use SSLv3\n");
00218         BIO_printf(bio_err," -tls1         - just use TLSv1\n");
00219         BIO_printf(bio_err," -dtls1        - just use DTLSv1\n");    
00220         BIO_printf(bio_err," -mtu          - set the MTU\n");
00221         BIO_printf(bio_err," -no_tls1/-no_ssl3/-no_ssl2 - turn off that protocol\n");
00222         BIO_printf(bio_err," -bugs         - Switch on all SSL implementation bug workarounds\n");
00223         BIO_printf(bio_err," -serverpref   - Use server's cipher preferences (only SSLv2)\n");
00224         BIO_printf(bio_err," -cipher       - preferred cipher to use, use the 'openssl ciphers'\n");
00225         BIO_printf(bio_err,"                 command to see what is available\n");
00226         BIO_printf(bio_err," -starttls prot - use the STARTTLS command before starting TLS\n");
00227         BIO_printf(bio_err,"                 for those protocols that support it, where\n");
00228         BIO_printf(bio_err,"                 'prot' defines which one to assume.  Currently,\n");
00229         BIO_printf(bio_err,"                 only \"smtp\" and \"pop3\" are supported.\n");
00230 #ifndef OPENSSL_NO_ENGINE
00231         BIO_printf(bio_err," -engine id    - Initialise and use the specified engine\n");
00232 #endif
00233         BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
00234 
00235         }
00236 
00237 int MAIN(int, char **);
00238 
00239 int MAIN(int argc, char **argv)
00240         {
00241         int off=0;
00242         SSL *con=NULL,*con2=NULL;
00243         X509_STORE *store = NULL;
00244         int s,k,width,state=0;
00245         char *cbuf=NULL,*sbuf=NULL,*mbuf=NULL;
00246         int cbuf_len,cbuf_off;
00247         int sbuf_len,sbuf_off;
00248         fd_set readfds,writefds;
00249         short port=PORT;
00250         int full_log=1;
00251         char *host=SSL_HOST_NAME;
00252         char *cert_file=NULL,*key_file=NULL;
00253         int cert_format = FORMAT_PEM, key_format = FORMAT_PEM;
00254         char *passarg = NULL, *pass = NULL;
00255         X509 *cert = NULL;
00256         EVP_PKEY *key = NULL;
00257         char *CApath=NULL,*CAfile=NULL,*cipher=NULL;
00258         int reconnect=0,badop=0,verify=SSL_VERIFY_NONE,bugs=0;
00259         int crlf=0;
00260         int write_tty,read_tty,write_ssl,read_ssl,tty_on,ssl_pending;
00261         SSL_CTX *ctx=NULL;
00262         int ret=1,in_init=1,i,nbio_test=0;
00263         int starttls_proto = 0;
00264         int prexit = 0, vflags = 0;
00265         SSL_METHOD *meth=NULL;
00266 #ifdef sock_type
00267 #undef sock_type
00268 #endif
00269         int sock_type=SOCK_STREAM;
00270         BIO *sbio;
00271         char *inrand=NULL;
00272 #ifndef OPENSSL_NO_ENGINE
00273         char *engine_id=NULL;
00274         ENGINE *e=NULL;
00275 #endif
00276 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE)
00277         struct timeval tv;
00278 #endif
00279 
00280         struct sockaddr peer;
00281         int peerlen = sizeof(peer);
00282         int enable_timeouts = 0 ;
00283         long mtu = 0;
00284 
00285 #if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
00286         meth=SSLv23_client_method();
00287 #elif !defined(OPENSSL_NO_SSL3)
00288         meth=SSLv3_client_method();
00289 #elif !defined(OPENSSL_NO_SSL2)
00290         meth=SSLv2_client_method();
00291 #endif
00292 
00293         apps_startup();
00294         c_Pause=0;
00295         c_quiet=0;
00296         c_ign_eof=0;
00297         c_debug=0;
00298         c_msg=0;
00299         c_showcerts=0;
00300 
00301         if (bio_err == NULL)
00302                 bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
00303 
00304         if (!load_config(bio_err, NULL))
00305                 goto end;
00306 
00307         if (    ((cbuf=OPENSSL_malloc(BUFSIZZ)) == NULL) ||
00308                 ((sbuf=OPENSSL_malloc(BUFSIZZ)) == NULL) ||
00309                 ((mbuf=OPENSSL_malloc(BUFSIZZ)) == NULL))
00310                 {
00311                 BIO_printf(bio_err,"out of memory\n");
00312                 goto end;
00313                 }
00314 
00315         verify_depth=0;
00316         verify_error=X509_V_OK;
00317 #ifdef FIONBIO
00318         c_nbio=0;
00319 #endif
00320 
00321         argc--;
00322         argv++;
00323         while (argc >= 1)
00324                 {
00325                 if      (strcmp(*argv,"-host") == 0)
00326                         {
00327                         if (--argc < 1) goto bad;
00328                         host= *(++argv);
00329                         }
00330                 else if (strcmp(*argv,"-port") == 0)
00331                         {
00332                         if (--argc < 1) goto bad;
00333                         port=atoi(*(++argv));
00334                         if (port == 0) goto bad;
00335                         }
00336                 else if (strcmp(*argv,"-connect") == 0)
00337                         {
00338                         if (--argc < 1) goto bad;
00339                         if (!extract_host_port(*(++argv),&host,NULL,&port))
00340                                 goto bad;
00341                         }
00342                 else if (strcmp(*argv,"-verify") == 0)
00343                         {
00344                         verify=SSL_VERIFY_PEER;
00345                         if (--argc < 1) goto bad;
00346                         verify_depth=atoi(*(++argv));
00347                         BIO_printf(bio_err,"verify depth is %d\n",verify_depth);
00348                         }
00349                 else if (strcmp(*argv,"-cert") == 0)
00350                         {
00351                         if (--argc < 1) goto bad;
00352                         cert_file= *(++argv);
00353                         }
00354                 else if (strcmp(*argv,"-certform") == 0)
00355                         {
00356                         if (--argc < 1) goto bad;
00357                         cert_format = str2fmt(*(++argv));
00358                         }
00359                 else if (strcmp(*argv,"-crl_check") == 0)
00360                         vflags |= X509_V_FLAG_CRL_CHECK;
00361                 else if (strcmp(*argv,"-crl_check_all") == 0)
00362                         vflags |= X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL;
00363                 else if (strcmp(*argv,"-prexit") == 0)
00364                         prexit=1;
00365                 else if (strcmp(*argv,"-crlf") == 0)
00366                         crlf=1;
00367                 else if (strcmp(*argv,"-quiet") == 0)
00368                         {
00369                         c_quiet=1;
00370                         c_ign_eof=1;
00371                         }
00372                 else if (strcmp(*argv,"-ign_eof") == 0)
00373                         c_ign_eof=1;
00374                 else if (strcmp(*argv,"-pause") == 0)
00375                         c_Pause=1;
00376                 else if (strcmp(*argv,"-debug") == 0)
00377                         c_debug=1;
00378 #ifdef WATT32
00379                 else if (strcmp(*argv,"-wdebug") == 0)
00380                         dbug_init();
00381 #endif
00382                 else if (strcmp(*argv,"-msg") == 0)
00383                         c_msg=1;
00384                 else if (strcmp(*argv,"-showcerts") == 0)
00385                         c_showcerts=1;
00386                 else if (strcmp(*argv,"-nbio_test") == 0)
00387                         nbio_test=1;
00388                 else if (strcmp(*argv,"-state") == 0)
00389                         state=1;
00390 #ifndef OPENSSL_NO_SSL2
00391                 else if (strcmp(*argv,"-ssl2") == 0)
00392                         meth=SSLv2_client_method();
00393 #endif
00394 #ifndef OPENSSL_NO_SSL3
00395                 else if (strcmp(*argv,"-ssl3") == 0)
00396                         meth=SSLv3_client_method();
00397 #endif
00398 #ifndef OPENSSL_NO_TLS1
00399                 else if (strcmp(*argv,"-tls1") == 0)
00400                         meth=TLSv1_client_method();
00401 #endif
00402 #ifndef OPENSSL_NO_DTLS1
00403                 else if (strcmp(*argv,"-dtls1") == 0)
00404                         {
00405                         meth=DTLSv1_client_method();
00406                         sock_type=SOCK_DGRAM;
00407                         }
00408                 else if (strcmp(*argv,"-timeout") == 0)
00409                         enable_timeouts=1;
00410                 else if (strcmp(*argv,"-mtu") == 0)
00411                         {
00412                         if (--argc < 1) goto bad;
00413                         mtu = atol(*(++argv));
00414                         }
00415 #endif
00416                 else if (strcmp(*argv,"-bugs") == 0)
00417                         bugs=1;
00418                 else if (strcmp(*argv,"-keyform") == 0)
00419                         {
00420                         if (--argc < 1) goto bad;
00421                         key_format = str2fmt(*(++argv));
00422                         }
00423                 else if (strcmp(*argv,"-pass") == 0)
00424                         {
00425                         if (--argc < 1) goto bad;
00426                         passarg = *(++argv);
00427                         }
00428                 else if (strcmp(*argv,"-key") == 0)
00429                         {
00430                         if (--argc < 1) goto bad;
00431                         key_file= *(++argv);
00432                         }
00433                 else if (strcmp(*argv,"-reconnect") == 0)
00434                         {
00435                         reconnect=5;
00436                         }
00437                 else if (strcmp(*argv,"-CApath") == 0)
00438                         {
00439                         if (--argc < 1) goto bad;
00440                         CApath= *(++argv);
00441                         }
00442                 else if (strcmp(*argv,"-CAfile") == 0)
00443                         {
00444                         if (--argc < 1) goto bad;
00445                         CAfile= *(++argv);
00446                         }
00447                 else if (strcmp(*argv,"-no_tls1") == 0)
00448                         off|=SSL_OP_NO_TLSv1;
00449                 else if (strcmp(*argv,"-no_ssl3") == 0)
00450                         off|=SSL_OP_NO_SSLv3;
00451                 else if (strcmp(*argv,"-no_ssl2") == 0)
00452                         off|=SSL_OP_NO_SSLv2;
00453                 else if (strcmp(*argv,"-serverpref") == 0)
00454                         off|=SSL_OP_CIPHER_SERVER_PREFERENCE;
00455                 else if (strcmp(*argv,"-cipher") == 0)
00456                         {
00457                         if (--argc < 1) goto bad;
00458                         cipher= *(++argv);
00459                         }
00460 #ifdef FIONBIO
00461                 else if (strcmp(*argv,"-nbio") == 0)
00462                         { c_nbio=1; }
00463 #endif
00464                 else if (strcmp(*argv,"-starttls") == 0)
00465                         {
00466                         if (--argc < 1) goto bad;
00467                         ++argv;
00468                         if (strcmp(*argv,"smtp") == 0)
00469                                 starttls_proto = 1;
00470                         else if (strcmp(*argv,"pop3") == 0)
00471                                 starttls_proto = 2;
00472                         else
00473                                 goto bad;
00474                         }
00475 #ifndef OPENSSL_NO_ENGINE
00476                 else if (strcmp(*argv,"-engine") == 0)
00477                         {
00478                         if (--argc < 1) goto bad;
00479                         engine_id = *(++argv);
00480                         }
00481 #endif
00482                 else if (strcmp(*argv,"-rand") == 0)
00483                         {
00484                         if (--argc < 1) goto bad;
00485                         inrand= *(++argv);
00486                         }
00487                 else
00488                         {
00489                         BIO_printf(bio_err,"unknown option %s\n",*argv);
00490                         badop=1;
00491                         break;
00492                         }
00493                 argc--;
00494                 argv++;
00495                 }
00496         if (badop)
00497                 {
00498 bad:
00499                 sc_usage();
00500                 goto end;
00501                 }
00502 
00503         OpenSSL_add_ssl_algorithms();
00504         SSL_load_error_strings();
00505 
00506 #ifndef OPENSSL_NO_ENGINE
00507         e = setup_engine(bio_err, engine_id, 1);
00508 #endif
00509         if (!app_passwd(bio_err, passarg, NULL, &pass, NULL))
00510                 {
00511                 BIO_printf(bio_err, "Error getting password\n");
00512                 goto end;
00513                 }
00514 
00515         if (key_file == NULL)
00516                 key_file = cert_file;
00517 
00518 
00519         if (key_file)
00520 
00521                 {
00522 
00523                 key = load_key(bio_err, key_file, key_format, 0, pass, e,
00524                                "client certificate private key file");
00525                 if (!key)
00526                         {
00527                         ERR_print_errors(bio_err);
00528                         goto end;
00529                         }
00530 
00531                 }
00532 
00533         if (cert_file)
00534 
00535                 {
00536                 cert = load_cert(bio_err,cert_file,cert_format,
00537                                 NULL, e, "client certificate file");
00538 
00539                 if (!cert)
00540                         {
00541                         ERR_print_errors(bio_err);
00542                         goto end;
00543                         }
00544                 }
00545 
00546         if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL
00547                 && !RAND_status())
00548                 {
00549                 BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
00550                 }
00551         if (inrand != NULL)
00552                 BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
00553                         app_RAND_load_files(inrand));
00554 
00555         if (bio_c_out == NULL)
00556                 {
00557                 if (c_quiet && !c_debug && !c_msg)
00558                         {
00559                         bio_c_out=BIO_new(BIO_s_null());
00560                         }
00561                 else
00562                         {
00563                         if (bio_c_out == NULL)
00564                                 bio_c_out=BIO_new_fp(stdout,BIO_NOCLOSE);
00565                         }
00566                 }
00567 
00568         ctx=SSL_CTX_new(meth);
00569         if (ctx == NULL)
00570                 {
00571                 ERR_print_errors(bio_err);
00572                 goto end;
00573                 }
00574 
00575         if (bugs)
00576                 SSL_CTX_set_options(ctx,SSL_OP_ALL|off);
00577         else
00578                 SSL_CTX_set_options(ctx,off);
00579         /* DTLS: partial reads end up discarding unread UDP bytes :-( 
00580          * Setting read ahead solves this problem.
00581          */
00582         if (sock_type == SOCK_DGRAM) SSL_CTX_set_read_ahead(ctx, 1);
00583 
00584         if (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback);
00585         if (cipher != NULL)
00586                 if(!SSL_CTX_set_cipher_list(ctx,cipher)) {
00587                 BIO_printf(bio_err,"error setting cipher list\n");
00588                 ERR_print_errors(bio_err);
00589                 goto end;
00590         }
00591 #if 0
00592         else
00593                 SSL_CTX_set_cipher_list(ctx,getenv("SSL_CIPHER"));
00594 #endif
00595 
00596         SSL_CTX_set_verify(ctx,verify,verify_callback);
00597         if (!set_cert_key_stuff(ctx,cert,key))
00598                 goto end;
00599 
00600         if ((!SSL_CTX_load_verify_locations(ctx,CAfile,CApath)) ||
00601                 (!SSL_CTX_set_default_verify_paths(ctx)))
00602                 {
00603                 /* BIO_printf(bio_err,"error setting default verify locations\n"); */
00604                 ERR_print_errors(bio_err);
00605                 /* goto end; */
00606                 }
00607 
00608         store = SSL_CTX_get_cert_store(ctx);
00609         X509_STORE_set_flags(store, vflags);
00610 
00611         con=SSL_new(ctx);
00612 #ifndef OPENSSL_NO_KRB5
00613         if (con  &&  (con->kssl_ctx = kssl_ctx_new()) != NULL)
00614                 {
00615                 kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVER, host);
00616                 }
00617 #endif  /* OPENSSL_NO_KRB5  */
00618 /*      SSL_set_cipher_list(con,"RC4-MD5"); */
00619 
00620 re_start:
00621 
00622         if (init_client(&s,host,port,sock_type) == 0)
00623                 {
00624                 BIO_printf(bio_err,"connect:errno=%d\n",get_last_socket_error());
00625                 SHUTDOWN(s);
00626                 goto end;
00627                 }
00628         BIO_printf(bio_c_out,"CONNECTED(%08X)\n",s);
00629 
00630 #ifdef FIONBIO
00631         if (c_nbio)
00632                 {
00633                 unsigned long l=1;
00634                 BIO_printf(bio_c_out,"turning on non blocking io\n");
00635                 if (BIO_socket_ioctl(s,FIONBIO,&l) < 0)
00636                         {
00637                         ERR_print_errors(bio_err);
00638                         goto end;
00639                         }
00640                 }
00641 #endif                                              
00642         if (c_Pause & 0x01) con->debug=1;
00643 
00644         if ( SSL_version(con) == DTLS1_VERSION)
00645                 {
00646                 struct timeval timeout;
00647 
00648                 sbio=BIO_new_dgram(s,BIO_NOCLOSE);
00649                 if (getsockname(s, &peer, (void *)&peerlen) < 0)
00650                         {
00651                         BIO_printf(bio_err, "getsockname:errno=%d\n",
00652                                 get_last_socket_error());
00653                         SHUTDOWN(s);
00654                         goto end;
00655                         }
00656 
00657                 BIO_ctrl_set_connected(sbio, 1, &peer);
00658 
00659                 if ( enable_timeouts)
00660                         {
00661                         timeout.tv_sec = 0;
00662                         timeout.tv_usec = DGRAM_RCV_TIMEOUT;
00663                         BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
00664                         
00665                         timeout.tv_sec = 0;
00666                         timeout.tv_usec = DGRAM_SND_TIMEOUT;
00667                         BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
00668                         }
00669 
00670                 if ( mtu > 0)
00671                         {
00672                         SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
00673                         SSL_set_mtu(con, mtu);
00674                         }
00675                 else
00676                         /* want to do MTU discovery */
00677                         BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
00678                 }
00679         else
00680                 sbio=BIO_new_socket(s,BIO_NOCLOSE);
00681 
00682 
00683 
00684         if (nbio_test)
00685                 {
00686                 BIO *test;
00687 
00688                 test=BIO_new(BIO_f_nbio_test());
00689                 sbio=BIO_push(test,sbio);
00690                 }
00691 
00692         if (c_debug)
00693                 {
00694                 con->debug=1;
00695                 BIO_set_callback(sbio,bio_dump_callback);
00696                 BIO_set_callback_arg(sbio,bio_c_out);
00697                 }
00698         if (c_msg)
00699                 {
00700                 SSL_set_msg_callback(con, msg_cb);
00701                 SSL_set_msg_callback_arg(con, bio_c_out);
00702                 }
00703 
00704         SSL_set_bio(con,sbio,sbio);
00705         SSL_set_connect_state(con);
00706 
00707         /* ok, lets connect */
00708         width=SSL_get_fd(con)+1;
00709 
00710         read_tty=1;
00711         write_tty=0;
00712         tty_on=0;
00713         read_ssl=1;
00714         write_ssl=1;
00715         
00716         cbuf_len=0;
00717         cbuf_off=0;
00718         sbuf_len=0;
00719         sbuf_off=0;
00720 
00721         /* This is an ugly hack that does a lot of assumptions */
00722         if (starttls_proto == 1)
00723                 {
00724                 BIO_read(sbio,mbuf,BUFSIZZ);
00725                 BIO_printf(sbio,"STARTTLS\r\n");
00726                 BIO_read(sbio,sbuf,BUFSIZZ);
00727                 }
00728         if (starttls_proto == 2)
00729                 {
00730                 BIO_read(sbio,mbuf,BUFSIZZ);
00731                 BIO_printf(sbio,"STLS\r\n");
00732                 BIO_read(sbio,sbuf,BUFSIZZ);
00733                 }
00734 
00735         for (;;)
00736                 {
00737                 FD_ZERO(&readfds);
00738                 FD_ZERO(&writefds);
00739 
00740                 if (SSL_in_init(con) && !SSL_total_renegotiations(con))
00741                         {
00742                         in_init=1;
00743                         tty_on=0;
00744                         }
00745                 else
00746                         {
00747                         tty_on=1;
00748                         if (in_init)
00749                                 {
00750                                 in_init=0;
00751                                 print_stuff(bio_c_out,con,full_log);
00752                                 if (full_log > 0) full_log--;
00753 
00754                                 if (starttls_proto)
00755                                         {
00756                                         BIO_printf(bio_err,"%s",mbuf);
00757                                         /* We don't need to know any more */
00758                                         starttls_proto = 0;
00759                                         }
00760 
00761                                 if (reconnect)
00762                                         {
00763                                         reconnect--;
00764                                         BIO_printf(bio_c_out,"drop connection and then reconnect\n");
00765                                         SSL_shutdown(con);
00766                                         SSL_set_connect_state(con);
00767                                         SHUTDOWN(SSL_get_fd(con));
00768                                         goto re_start;
00769                                         }
00770                                 }
00771                         }
00772 
00773                 ssl_pending = read_ssl && SSL_pending(con);
00774 
00775                 if (!ssl_pending)
00776                         {
00777 #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE)
00778                         if (tty_on)
00779                                 {
00780                                 if (read_tty)  FD_SET(fileno(stdin),&readfds);
00781                                 if (write_tty) FD_SET(fileno(stdout),&writefds);
00782                                 }
00783                         if (read_ssl)
00784                                 FD_SET(SSL_get_fd(con),&readfds);
00785                         if (write_ssl)
00786                                 FD_SET(SSL_get_fd(con),&writefds);
00787 #else
00788                         if(!tty_on || !write_tty) {
00789                                 if (read_ssl)
00790                                         FD_SET(SSL_get_fd(con),&readfds);
00791                                 if (write_ssl)
00792                                         FD_SET(SSL_get_fd(con),&writefds);
00793                         }
00794 #endif
00795 /*                      printf("mode tty(%d %d%d) ssl(%d%d)\n",
00796                                 tty_on,read_tty,write_tty,read_ssl,write_ssl);*/
00797 
00798                         /* Note: under VMS with SOCKETSHR the second parameter
00799                          * is currently of type (int *) whereas under other
00800                          * systems it is (void *) if you don't have a cast it
00801                          * will choke the compiler: if you do have a cast then
00802                          * you can either go for (int *) or (void *).
00803                          */
00804 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
00805                         /* Under Windows/DOS we make the assumption that we can
00806                          * always write to the tty: therefore if we need to
00807                          * write to the tty we just fall through. Otherwise
00808                          * we timeout the select every second and see if there
00809                          * are any keypresses. Note: this is a hack, in a proper
00810                          * Windows application we wouldn't do this.
00811                          */
00812                         i=0;
00813                         if(!write_tty) {
00814                                 if(read_tty) {
00815                                         tv.tv_sec = 1;
00816                                         tv.tv_usec = 0;
00817                                         i=select(width,(void *)&readfds,(void *)&writefds,
00818                                                  NULL,&tv);
00819 #if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)
00820                                         if(!i && (!_kbhit() || !read_tty) ) continue;
00821 #else
00822                                         if(!i && (!((_kbhit()) || (WAIT_OBJECT_0 == WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0))) || !read_tty) ) continue;
00823 #endif
00824                                 } else  i=select(width,(void *)&readfds,(void *)&writefds,
00825                                          NULL,NULL);
00826                         }
00827 #elif defined(OPENSSL_SYS_NETWARE)
00828                         if(!write_tty) {
00829                                 if(read_tty) {
00830                                         tv.tv_sec = 1;
00831                                         tv.tv_usec = 0;
00832                                         i=select(width,(void *)&readfds,(void *)&writefds,
00833                                                 NULL,&tv);
00834                                 } else  i=select(width,(void *)&readfds,(void *)&writefds,
00835                                         NULL,NULL);
00836                         }
00837 #else
00838                         i=select(width,(void *)&readfds,(void *)&writefds,
00839                                  NULL,NULL);
00840 #endif
00841                         if ( i < 0)
00842                                 {
00843                                 BIO_printf(bio_err,"bad select %d\n",
00844                                 get_last_socket_error());
00845                                 goto shut;
00846                                 /* goto end; */
00847                                 }
00848                         }
00849 
00850                 if (!ssl_pending && FD_ISSET(SSL_get_fd(con),&writefds))
00851                         {
00852                         k=SSL_write(con,&(cbuf[cbuf_off]),
00853                                 (unsigned int)cbuf_len);
00854                         switch (SSL_get_error(con,k))
00855                                 {
00856                         case SSL_ERROR_NONE:
00857                                 cbuf_off+=k;
00858                                 cbuf_len-=k;
00859                                 if (k <= 0) goto end;
00860                                 /* we have done a  write(con,NULL,0); */
00861                                 if (cbuf_len <= 0)
00862                                         {
00863                                         read_tty=1;
00864                                         write_ssl=0;
00865                                         }
00866                                 else /* if (cbuf_len > 0) */
00867                                         {
00868                                         read_tty=0;
00869                                         write_ssl=1;
00870                                         }
00871                                 break;
00872                         case SSL_ERROR_WANT_WRITE:
00873                                 BIO_printf(bio_c_out,"write W BLOCK\n");
00874                                 write_ssl=1;
00875                                 read_tty=0;
00876                                 break;
00877                         case SSL_ERROR_WANT_READ:
00878                                 BIO_printf(bio_c_out,"write R BLOCK\n");
00879                                 write_tty=0;
00880                                 read_ssl=1;
00881                                 write_ssl=0;
00882                                 break;
00883                         case SSL_ERROR_WANT_X509_LOOKUP:
00884                                 BIO_printf(bio_c_out,"write X BLOCK\n");
00885                                 break;
00886                         case SSL_ERROR_ZERO_RETURN:
00887                                 if (cbuf_len != 0)
00888                                         {
00889                                         BIO_printf(bio_c_out,"shutdown\n");
00890                                         goto shut;
00891                                         }
00892                                 else
00893                                         {
00894                                         read_tty=1;
00895                                         write_ssl=0;
00896                                         break;
00897                                         }
00898                                 
00899                         case SSL_ERROR_SYSCALL:
00900                                 if ((k != 0) || (cbuf_len != 0))
00901                                         {
00902                                         BIO_printf(bio_err,"write:errno=%d\n",
00903                                                 get_last_socket_error());
00904                                         goto shut;
00905                                         }
00906                                 else
00907                                         {
00908                                         read_tty=1;
00909                                         write_ssl=0;
00910                                         }
00911                                 break;
00912                         case SSL_ERROR_SSL:
00913                                 ERR_print_errors(bio_err);
00914                                 goto shut;
00915                                 }
00916                         }
00917 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE)
00918                 /* Assume Windows/DOS can always write */
00919                 else if (!ssl_pending && write_tty)
00920 #else
00921                 else if (!ssl_pending && FD_ISSET(fileno(stdout),&writefds))
00922 #endif
00923                         {
00924 #ifdef CHARSET_EBCDIC
00925                         ascii2ebcdic(&(sbuf[sbuf_off]),&(sbuf[sbuf_off]),sbuf_len);
00926 #endif
00927                         i=write(fileno(stdout),&(sbuf[sbuf_off]),sbuf_len);
00928 
00929                         if (i <= 0)
00930                                 {
00931                                 BIO_printf(bio_c_out,"DONE\n");
00932                                 goto shut;
00933                                 /* goto end; */
00934                                 }
00935 
00936                         sbuf_len-=i;;
00937                         sbuf_off+=i;
00938                         if (sbuf_len <= 0)
00939                                 {
00940                                 read_ssl=1;
00941                                 write_tty=0;
00942                                 }
00943                         }
00944                 else if (ssl_pending || FD_ISSET(SSL_get_fd(con),&readfds))
00945                         {
00946 #ifdef RENEG
00947 { static int iiii; if (++iiii == 52) { SSL_renegotiate(con); iiii=0; } }
00948 #endif
00949 #if 1
00950                         k=SSL_read(con,sbuf,1024 /* BUFSIZZ */ );
00951 #else
00952 /* Demo for pending and peek :-) */
00953                         k=SSL_read(con,sbuf,16);
00954 { char zbuf[10240]; 
00955 printf("read=%d pending=%d peek=%d\n",k,SSL_pending(con),SSL_peek(con,zbuf,10240));
00956 }
00957 #endif
00958 
00959                         switch (SSL_get_error(con,k))
00960                                 {
00961                         case SSL_ERROR_NONE:
00962                                 if (k <= 0)
00963                                         goto end;
00964                                 sbuf_off=0;
00965                                 sbuf_len=k;
00966 
00967                                 read_ssl=0;
00968                                 write_tty=1;
00969                                 break;
00970                         case SSL_ERROR_WANT_WRITE:
00971                                 BIO_printf(bio_c_out,"read W BLOCK\n");
00972                                 write_ssl=1;
00973                                 read_tty=0;
00974                                 break;
00975                         case SSL_ERROR_WANT_READ:
00976                                 BIO_printf(bio_c_out,"read R BLOCK\n");
00977                                 write_tty=0;
00978                                 read_ssl=1;
00979                                 if ((read_tty == 0) && (write_ssl == 0))
00980                                         write_ssl=1;
00981                                 break;
00982                         case SSL_ERROR_WANT_X509_LOOKUP:
00983                                 BIO_printf(bio_c_out,"read X BLOCK\n");
00984                                 break;
00985                         case SSL_ERROR_SYSCALL:
00986                                 BIO_printf(bio_err,"read:errno=%d\n",get_last_socket_error());
00987                                 goto shut;
00988                         case SSL_ERROR_ZERO_RETURN:
00989                                 BIO_printf(bio_c_out,"closed\n");
00990                                 goto shut;
00991                         case SSL_ERROR_SSL:
00992                                 ERR_print_errors(bio_err);
00993                                 goto shut;
00994                                 /* break; */
00995                                 }
00996                         }
00997 
00998 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
00999 #if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)
01000                 else if (_kbhit())
01001 #else
01002                 else if ((_kbhit()) || (WAIT_OBJECT_0 == WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0)))
01003 #endif
01004 #elif defined (OPENSSL_SYS_NETWARE)
01005         else if (_kbhit())
01006 #else
01007                 else if (FD_ISSET(fileno(stdin),&readfds))
01008 #endif
01009                         {
01010                         if (crlf)
01011                                 {
01012                                 int j, lf_num;
01013 
01014                                 i=read(fileno(stdin),cbuf,BUFSIZZ/2);
01015                                 lf_num = 0;
01016                                 /* both loops are skipped when i <= 0 */
01017                                 for (j = 0; j < i; j++)
01018                                         if (cbuf[j] == '\n')
01019                                                 lf_num++;
01020                                 for (j = i-1; j >= 0; j--)
01021                                         {
01022                                         cbuf[j+lf_num] = cbuf[j];
01023                                         if (cbuf[j] == '\n')
01024                                                 {
01025                                                 lf_num--;
01026                                                 i++;
01027                                                 cbuf[j+lf_num] = '\r';
01028                                                 }
01029                                         }
01030                                 assert(lf_num == 0);
01031                                 }
01032                         else
01033                                 i=read(fileno(stdin),cbuf,BUFSIZZ);
01034 
01035                         if ((!c_ign_eof) && ((i <= 0) || (cbuf[0] == 'Q')))
01036                                 {
01037                                 BIO_printf(bio_err,"DONE\n");
01038                                 goto shut;
01039                                 }
01040 
01041                         if ((!c_ign_eof) && (cbuf[0] == 'R'))
01042                                 {
01043                                 BIO_printf(bio_err,"RENEGOTIATING\n");
01044                                 SSL_renegotiate(con);
01045                                 cbuf_len=0;
01046                                 }
01047                         else
01048                                 {
01049                                 cbuf_len=i;
01050                                 cbuf_off=0;
01051 #ifdef CHARSET_EBCDIC
01052                                 ebcdic2ascii(cbuf, cbuf, i);
01053 #endif
01054                                 }
01055 
01056                         write_ssl=1;
01057                         read_tty=0;
01058                         }
01059                 }
01060 shut:
01061         SSL_shutdown(con);
01062         SHUTDOWN(SSL_get_fd(con));
01063         ret=0;
01064 end:
01065         if(prexit) print_stuff(bio_c_out,con,1);
01066         if (con != NULL) SSL_free(con);
01067         if (con2 != NULL) SSL_free(con2);
01068         if (ctx != NULL) SSL_CTX_free(ctx);
01069         if (cert)
01070                 X509_free(cert);
01071         if (key)
01072                 EVP_PKEY_free(key);
01073         if (pass)
01074                 OPENSSL_free(pass);
01075         if (cbuf != NULL) { OPENSSL_cleanse(cbuf,BUFSIZZ); OPENSSL_free(cbuf); }
01076         if (sbuf != NULL) { OPENSSL_cleanse(sbuf,BUFSIZZ); OPENSSL_free(sbuf); }
01077         if (mbuf != NULL) { OPENSSL_cleanse(mbuf,BUFSIZZ); OPENSSL_free(mbuf); }
01078         if (bio_c_out != NULL)
01079                 {
01080                 BIO_free(bio_c_out);
01081                 bio_c_out=NULL;
01082                 }
01083         apps_shutdown();
01084         OPENSSL_EXIT(ret);
01085         }
01086 
01087 
01088 static void print_stuff(BIO *bio, SSL *s, int full)
01089         {
01090         X509 *peer=NULL;
01091         char *p;
01092         static const char *space="                ";
01093         char buf[BUFSIZ];
01094         STACK_OF(X509) *sk;
01095         STACK_OF(X509_NAME) *sk2;
01096         SSL_CIPHER *c;
01097         X509_NAME *xn;
01098         int j,i;
01099 #ifndef OPENSSL_NO_COMP
01100         const COMP_METHOD *comp, *expansion;
01101 #endif
01102 
01103         if (full)
01104                 {
01105                 int got_a_chain = 0;
01106 
01107                 sk=SSL_get_peer_cert_chain(s);
01108                 if (sk != NULL)
01109                         {
01110                         got_a_chain = 1; /* we don't have it for SSL2 (yet) */
01111 
01112                         BIO_printf(bio,"---\nCertificate chain\n");
01113                         for (i=0; i<sk_X509_num(sk); i++)
01114                                 {
01115                                 X509_NAME_oneline(X509_get_subject_name(
01116                                         sk_X509_value(sk,i)),buf,sizeof buf);
01117                                 BIO_printf(bio,"%2d s:%s\n",i,buf);
01118                                 X509_NAME_oneline(X509_get_issuer_name(
01119                                         sk_X509_value(sk,i)),buf,sizeof buf);
01120                                 BIO_printf(bio,"   i:%s\n",buf);
01121                                 if (c_showcerts)
01122                                         PEM_write_bio_X509(bio,sk_X509_value(sk,i));
01123                                 }
01124                         }
01125 
01126                 BIO_printf(bio,"---\n");
01127                 peer=SSL_get_peer_certificate(s);
01128                 if (peer != NULL)
01129                         {
01130                         BIO_printf(bio,"Server certificate\n");
01131                         if (!(c_showcerts && got_a_chain)) /* Redundant if we showed the whole chain */
01132                                 PEM_write_bio_X509(bio,peer);
01133                         X509_NAME_oneline(X509_get_subject_name<