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

s_server.c

Go to the documentation of this file.
00001 /* apps/s_server.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  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
00113  * ECC cipher suite support in OpenSSL originally developed by 
00114  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
00115  */
00116 
00117 /* Until the key-gen callbacks are modified to use newer prototypes, we allow
00118  * deprecated functions for openssl-internal code */
00119 #ifdef OPENSSL_NO_DEPRECATED
00120 #undef OPENSSL_NO_DEPRECATED
00121 #endif
00122 
00123 #include <assert.h>
00124 #include <stdio.h>
00125 #include <stdlib.h>
00126 #include <string.h>
00127 
00128 #include <sys/stat.h>
00129 #include <openssl/e_os2.h>
00130 #ifdef OPENSSL_NO_STDIO
00131 #define APPS_WIN16
00132 #endif
00133 
00134 #if !defined(OPENSSL_SYS_NETWARE)  /* conflicts with winsock2 stuff on netware */
00135 #include <sys/types.h>
00136 #endif
00137 
00138 /* With IPv6, it looks like Digital has mixed up the proper order of
00139    recursive header file inclusion, resulting in the compiler complaining
00140    that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which
00141    is needed to have fileno() declared correctly...  So let's define u_int */
00142 #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
00143 #define __U_INT
00144 typedef unsigned int u_int;
00145 #endif
00146 
00147 #include <openssl/lhash.h>
00148 #include <openssl/bn.h>
00149 #define USE_SOCKETS
00150 #include "apps.h"
00151 #include <openssl/err.h>
00152 #include <openssl/pem.h>
00153 #include <openssl/x509.h>
00154 #include <openssl/ssl.h>
00155 #include <openssl/rand.h>
00156 #ifndef OPENSSL_NO_DH
00157 #include <openssl/dh.h>
00158 #endif
00159 #ifndef OPENSSL_NO_RSA
00160 #include <openssl/rsa.h>
00161 #endif
00162 #include "s_apps.h"
00163 #include "timeouts.h"
00164 
00165 #ifdef OPENSSL_SYS_WINCE
00166 /* Windows CE incorrectly defines fileno as returning void*, so to avoid problems below... */
00167 #ifdef fileno
00168 #undef fileno
00169 #endif
00170 #define fileno(a) (int)_fileno(a)
00171 #endif
00172 
00173 #if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
00174 /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
00175 #undef FIONBIO
00176 #endif
00177 
00178 #ifndef OPENSSL_NO_RSA
00179 static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength);
00180 #endif
00181 static int sv_body(char *hostname, int s, unsigned char *context);
00182 static int www_body(char *hostname, int s, unsigned char *context);
00183 static void close_accept_socket(void );
00184 static void sv_usage(void);
00185 static int init_ssl_connection(SSL *s);
00186 static void print_stats(BIO *bp,SSL_CTX *ctx);
00187 static int generate_session_id(const SSL *ssl, unsigned char *id,
00188                                 unsigned int *id_len);
00189 #ifndef OPENSSL_NO_DH
00190 static DH *load_dh_param(const char *dhfile);
00191 static DH *get_dh512(void);
00192 #endif
00193 
00194 #ifdef MONOLITH
00195 static void s_server_init(void);
00196 #endif
00197 
00198 #ifndef S_ISDIR
00199 # if defined(_S_IFMT) && defined(_S_IFDIR)
00200 #  define S_ISDIR(a)    (((a) & _S_IFMT) == _S_IFDIR)
00201 # else
00202 #  define S_ISDIR(a)    (((a) & S_IFMT) == S_IFDIR)
00203 # endif
00204 #endif
00205 
00206 #ifndef OPENSSL_NO_DH
00207 static unsigned char dh512_p[]={
00208         0xDA,0x58,0x3C,0x16,0xD9,0x85,0x22,0x89,0xD0,0xE4,0xAF,0x75,
00209         0x6F,0x4C,0xCA,0x92,0xDD,0x4B,0xE5,0x33,0xB8,0x04,0xFB,0x0F,
00210         0xED,0x94,0xEF,0x9C,0x8A,0x44,0x03,0xED,0x57,0x46,0x50,0xD3,
00211         0x69,0x99,0xDB,0x29,0xD7,0x76,0x27,0x6B,0xA2,0xD3,0xD4,0x12,
00212         0xE2,0x18,0xF4,0xDD,0x1E,0x08,0x4C,0xF6,0xD8,0x00,0x3E,0x7C,
00213         0x47,0x74,0xE8,0x33,
00214         };
00215 static unsigned char dh512_g[]={
00216         0x02,
00217         };
00218 
00219 static DH *get_dh512(void)
00220         {
00221         DH *dh=NULL;
00222 
00223         if ((dh=DH_new()) == NULL) return(NULL);
00224         dh->p=BN_bin2bn(dh512_p,sizeof(dh512_p),NULL);
00225         dh->g=BN_bin2bn(dh512_g,sizeof(dh512_g),NULL);
00226         if ((dh->p == NULL) || (dh->g == NULL))
00227                 return(NULL);
00228         return(dh);
00229         }
00230 #endif
00231 
00232 
00233 /* static int load_CA(SSL_CTX *ctx, char *file);*/
00234 
00235 #undef BUFSIZZ
00236 #define BUFSIZZ 16*1024
00237 static int bufsize=BUFSIZZ;
00238 static int accept_socket= -1;
00239 
00240 #define TEST_CERT       "server.pem"
00241 #undef PROG
00242 #define PROG            s_server_main
00243 
00244 extern int verify_depth;
00245 
00246 static char *cipher=NULL;
00247 static int s_server_verify=SSL_VERIFY_NONE;
00248 static int s_server_session_id_context = 1; /* anything will do */
00249 static const char *s_cert_file=TEST_CERT,*s_key_file=NULL;
00250 static char *s_dcert_file=NULL,*s_dkey_file=NULL;
00251 #ifdef FIONBIO
00252 static int s_nbio=0;
00253 #endif
00254 static int s_nbio_test=0;
00255 int s_crlf=0;
00256 static SSL_CTX *ctx=NULL;
00257 static int www=0;
00258 
00259 static BIO *bio_s_out=NULL;
00260 static int s_debug=0;
00261 static int s_msg=0;
00262 static int s_quiet=0;
00263 
00264 static int hack=0;
00265 #ifndef OPENSSL_NO_ENGINE
00266 static char *engine_id=NULL;
00267 #endif
00268 static const char *session_id_prefix=NULL;
00269 
00270 static int enable_timeouts = 0;
00271 #ifdef mtu
00272 #undef mtu
00273 #endif
00274 static long mtu;
00275 static int cert_chain = 0;
00276 
00277 
00278 #ifdef MONOLITH
00279 static void s_server_init(void)
00280         {
00281         accept_socket=-1;
00282         cipher=NULL;
00283         s_server_verify=SSL_VERIFY_NONE;
00284         s_dcert_file=NULL;
00285         s_dkey_file=NULL;
00286         s_cert_file=TEST_CERT;
00287         s_key_file=NULL;
00288 #ifdef FIONBIO
00289         s_nbio=0;
00290 #endif
00291         s_nbio_test=0;
00292         ctx=NULL;
00293         www=0;
00294 
00295         bio_s_out=NULL;
00296         s_debug=0;
00297         s_msg=0;
00298         s_quiet=0;
00299         hack=0;
00300 #ifndef OPENSSL_NO_ENGINE
00301         engine_id=NULL;
00302 #endif
00303         }
00304 #endif
00305 
00306 static void sv_usage(void)
00307         {
00308         BIO_printf(bio_err,"usage: s_server [args ...]\n");
00309         BIO_printf(bio_err,"\n");
00310         BIO_printf(bio_err," -accept arg   - port to accept on (default is %d)\n",PORT);
00311         BIO_printf(bio_err," -context arg  - set session ID context\n");
00312         BIO_printf(bio_err," -verify arg   - turn on peer certificate verification\n");
00313         BIO_printf(bio_err," -Verify arg   - turn on peer certificate verification, must have a cert.\n");
00314         BIO_printf(bio_err," -cert arg     - certificate file to use\n");
00315         BIO_printf(bio_err,"                 (default is %s)\n",TEST_CERT);
00316         BIO_printf(bio_err," -certform arg - certificate format (PEM or DER) PEM default\n");
00317         BIO_printf(bio_err," -key arg      - Private Key file to use, in cert file if\n");
00318         BIO_printf(bio_err,"                 not specified (default is %s)\n",TEST_CERT);
00319         BIO_printf(bio_err," -keyform arg  - key format (PEM, DER or ENGINE) PEM default\n");
00320         BIO_printf(bio_err," -pass arg     - private key file pass phrase source\n");
00321         BIO_printf(bio_err," -dcert arg    - second certificate file to use (usually for DSA)\n");
00322         BIO_printf(bio_err," -dcertform x  - second certificate format (PEM or DER) PEM default\n");
00323         BIO_printf(bio_err," -dkey arg     - second private key file to use (usually for DSA)\n");
00324         BIO_printf(bio_err," -dkeyform arg - second key format (PEM, DER or ENGINE) PEM default\n");
00325         BIO_printf(bio_err," -dpass arg    - second private key file pass phrase source\n");
00326         BIO_printf(bio_err," -dhparam arg  - DH parameter file to use, in cert file if not specified\n");
00327         BIO_printf(bio_err,"                 or a default set of parameters is used\n");
00328 #ifndef OPENSSL_NO_ECDH
00329         BIO_printf(bio_err," -named_curve arg  - Elliptic curve name to use for ephemeral ECDH keys.\n" \
00330                            "                 Use \"openssl ecparam -list_curves\" for all names\n" \
00331                            "                 (default is sect163r2).\n");
00332 #endif
00333 #ifdef FIONBIO
00334         BIO_printf(bio_err," -nbio         - Run with non-blocking IO\n");
00335 #endif
00336         BIO_printf(bio_err," -nbio_test    - test with the non-blocking test bio\n");
00337         BIO_printf(bio_err," -crlf         - convert LF from terminal into CRLF\n");
00338         BIO_printf(bio_err," -debug        - Print more output\n");
00339         BIO_printf(bio_err," -msg          - Show protocol messages\n");
00340         BIO_printf(bio_err," -state        - Print the SSL states\n");
00341         BIO_printf(bio_err," -CApath arg   - PEM format directory of CA's\n");
00342         BIO_printf(bio_err," -CAfile arg   - PEM format file of CA's\n");
00343         BIO_printf(bio_err," -nocert       - Don't use any certificates (Anon-DH)\n");
00344         BIO_printf(bio_err," -cipher arg   - play with 'openssl ciphers' to see what goes here\n");
00345         BIO_printf(bio_err," -serverpref   - Use server's cipher preferences\n");
00346         BIO_printf(bio_err," -quiet        - No server output\n");
00347         BIO_printf(bio_err," -no_tmp_rsa   - Do not generate a tmp RSA key\n");
00348         BIO_printf(bio_err," -ssl2         - Just talk SSLv2\n");
00349         BIO_printf(bio_err," -ssl3         - Just talk SSLv3\n");
00350         BIO_printf(bio_err," -tls1         - Just talk TLSv1\n");
00351         BIO_printf(bio_err," -dtls1        - Just talk DTLSv1\n");
00352         BIO_printf(bio_err," -timeout      - Enable timeouts\n");
00353         BIO_printf(bio_err," -mtu          - Set MTU\n");
00354         BIO_printf(bio_err," -chain        - Read a certificate chain\n");
00355         BIO_printf(bio_err," -no_ssl2      - Just disable SSLv2\n");
00356         BIO_printf(bio_err," -no_ssl3      - Just disable SSLv3\n");
00357         BIO_printf(bio_err," -no_tls1      - Just disable TLSv1\n");
00358 #ifndef OPENSSL_NO_DH
00359         BIO_printf(bio_err," -no_dhe       - Disable ephemeral DH\n");
00360 #endif
00361 #ifndef OPENSSL_NO_ECDH
00362         BIO_printf(bio_err," -no_ecdhe     - Disable ephemeral ECDH\n");
00363 #endif
00364         BIO_printf(bio_err," -bugs         - Turn on SSL bug compatibility\n");
00365         BIO_printf(bio_err," -www          - Respond to a 'GET /' with a status page\n");
00366         BIO_printf(bio_err," -WWW          - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
00367         BIO_printf(bio_err," -HTTP         - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
00368         BIO_printf(bio_err,"                 with the assumption it contains a complete HTTP response.\n");
00369 #ifndef OPENSSL_NO_ENGINE
00370         BIO_printf(bio_err," -engine id    - Initialise and use the specified engine\n");
00371 #endif
00372         BIO_printf(bio_err," -id_prefix arg - Generate SSL/TLS session IDs prefixed by 'arg'\n");
00373         BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
00374         }
00375 
00376 static int local_argc=0;
00377 static char **local_argv;
00378 
00379 #ifdef CHARSET_EBCDIC
00380 static int ebcdic_new(BIO *bi);
00381 static int ebcdic_free(BIO *a);
00382 static int ebcdic_read(BIO *b, char *out, int outl);
00383 static int ebcdic_write(BIO *b, const char *in, int inl);
00384 static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr);
00385 static int ebcdic_gets(BIO *bp, char *buf, int size);
00386 static int ebcdic_puts(BIO *bp, const char *str);
00387 
00388 #define BIO_TYPE_EBCDIC_FILTER  (18|0x0200)
00389 static BIO_METHOD methods_ebcdic=
00390         {
00391         BIO_TYPE_EBCDIC_FILTER,
00392         "EBCDIC/ASCII filter",
00393         ebcdic_write,
00394         ebcdic_read,
00395         ebcdic_puts,
00396         ebcdic_gets,
00397         ebcdic_ctrl,
00398         ebcdic_new,
00399         ebcdic_free,
00400         };
00401 
00402 typedef struct
00403 {
00404         size_t  alloced;
00405         char    buff[1];
00406 } EBCDIC_OUTBUFF;
00407 
00408 BIO_METHOD *BIO_f_ebcdic_filter()
00409 {
00410         return(&methods_ebcdic);
00411 }
00412 
00413 static int ebcdic_new(BIO *bi)
00414 {
00415         EBCDIC_OUTBUFF *wbuf;
00416 
00417         wbuf = (EBCDIC_OUTBUFF *)OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + 1024);
00418         wbuf->alloced = 1024;
00419         wbuf->buff[0] = '\0';
00420 
00421         bi->ptr=(char *)wbuf;
00422         bi->init=1;
00423         bi->flags=0;
00424         return(1);
00425 }
00426 
00427 static int ebcdic_free(BIO *a)
00428 {
00429         if (a == NULL) return(0);
00430         if (a->ptr != NULL)
00431                 OPENSSL_free(a->ptr);
00432         a->ptr=NULL;
00433         a->init=0;
00434         a->flags=0;
00435         return(1);
00436 }
00437         
00438 static int ebcdic_read(BIO *b, char *out, int outl)
00439 {
00440         int ret=0;
00441 
00442         if (out == NULL || outl == 0) return(0);
00443         if (b->next_bio == NULL) return(0);
00444 
00445         ret=BIO_read(b->next_bio,out,outl);
00446         if (ret > 0)
00447                 ascii2ebcdic(out,out,ret);
00448         return(ret);
00449 }
00450 
00451 static int ebcdic_write(BIO *b, const char *in, int inl)
00452 {
00453         EBCDIC_OUTBUFF *wbuf;
00454         int ret=0;
00455         int num;
00456         unsigned char n;
00457 
00458         if ((in == NULL) || (inl <= 0)) return(0);
00459         if (b->next_bio == NULL) return(0);
00460 
00461         wbuf=(EBCDIC_OUTBUFF *)b->ptr;
00462 
00463         if (inl > (num = wbuf->alloced))
00464         {
00465                 num = num + num;  /* double the size */
00466                 if (num < inl)
00467                         num = inl;
00468                 OPENSSL_free(wbuf);
00469                 wbuf=(EBCDIC_OUTBUFF *)OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + num);
00470 
00471                 wbuf->alloced = num;
00472                 wbuf->buff[0] = '\0';
00473 
00474                 b->ptr=(char *)wbuf;
00475         }
00476 
00477         ebcdic2ascii(wbuf->buff, in, inl);
00478 
00479         ret=BIO_write(b->next_bio, wbuf->buff, inl);
00480 
00481         return(ret);
00482 }
00483 
00484 static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr)
00485 {
00486         long ret;
00487 
00488         if (b->next_bio == NULL) return(0);
00489         switch (cmd)
00490         {
00491         case BIO_CTRL_DUP:
00492                 ret=0L;
00493                 break;
00494         default:
00495                 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
00496                 break;
00497         }
00498         return(ret);
00499 }
00500 
00501 static int ebcdic_gets(BIO *bp, char *buf, int size)
00502 {
00503         int i, ret=0;
00504         if (bp->next_bio == NULL) return(0);
00505 /*      return(BIO_gets(bp->next_bio,buf,size));*/
00506         for (i=0; i<size-1; ++i)
00507         {
00508                 ret = ebcdic_read(bp,&buf[i],1);
00509                 if (ret <= 0)
00510                         break;
00511                 else if (buf[i] == '\n')
00512                 {
00513                         ++i;
00514                         break;
00515                 }
00516         }
00517         if (i < size)
00518                 buf[i] = '\0';
00519         return (ret < 0 && i == 0) ? ret : i;
00520 }
00521 
00522 static int ebcdic_puts(BIO *bp, const char *str)
00523 {
00524         if (bp->next_bio == NULL) return(0);
00525         return ebcdic_write(bp, str, strlen(str));
00526 }
00527 #endif
00528 
00529 int MAIN(int, char **);
00530 
00531 int MAIN(int argc, char *argv[])
00532         {
00533         X509_STORE *store = NULL;
00534         int vflags = 0;
00535         short port=PORT;
00536         char *CApath=NULL,*CAfile=NULL;
00537         unsigned char *context = NULL;
00538         char *dhfile = NULL;
00539 #ifndef OPENSSL_NO_ECDH
00540         char *named_curve = NULL;
00541 #endif
00542         int badop=0,bugs=0;
00543         int ret=1;
00544         int off=0;
00545         int no_tmp_rsa=0,no_dhe=0,no_ecdhe=0,nocert=0;
00546         int state=0;
00547         SSL_METHOD *meth=NULL;
00548 #ifdef sock_type
00549 #undef sock_type
00550 #endif
00551     int sock_type=SOCK_STREAM;
00552 #ifndef OPENSSL_NO_ENGINE
00553         ENGINE *e=NULL;
00554 #endif
00555         char *inrand=NULL;
00556         int s_cert_format = FORMAT_PEM, s_key_format = FORMAT_PEM;
00557         char *passarg = NULL, *pass = NULL;
00558         char *dpassarg = NULL, *dpass = NULL;
00559         int s_dcert_format = FORMAT_PEM, s_dkey_format = FORMAT_PEM;
00560         X509 *s_cert = NULL, *s_dcert = NULL;
00561         EVP_PKEY *s_key = NULL, *s_dkey = NULL;
00562 
00563 #if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
00564         meth=SSLv23_server_method();
00565 #elif !defined(OPENSSL_NO_SSL3)
00566         meth=SSLv3_server_method();
00567 #elif !defined(OPENSSL_NO_SSL2)
00568         meth=SSLv2_server_method();
00569 #endif
00570 
00571         local_argc=argc;
00572         local_argv=argv;
00573 
00574         apps_startup();
00575 #ifdef MONOLITH
00576         s_server_init();
00577 #endif
00578 
00579         if (bio_err == NULL)
00580                 bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
00581 
00582         if (!load_config(bio_err, NULL))
00583                 goto end;
00584 
00585         verify_depth=0;
00586 #ifdef FIONBIO
00587         s_nbio=0;
00588 #endif
00589         s_nbio_test=0;
00590 
00591         argc--;
00592         argv++;
00593 
00594         while (argc >= 1)
00595                 {
00596                 if      ((strcmp(*argv,"-port") == 0) ||
00597                          (strcmp(*argv,"-accept") == 0))
00598                         {
00599                         if (--argc < 1) goto bad;
00600                         if (!extract_port(*(++argv),&port))
00601                                 goto bad;
00602                         }
00603                 else if (strcmp(*argv,"-verify") == 0)
00604                         {
00605                         s_server_verify=SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE;
00606                         if (--argc < 1) goto bad;
00607                         verify_depth=atoi(*(++argv));
00608                         BIO_printf(bio_err,"verify depth is %d\n",verify_depth);
00609                         }
00610                 else if (strcmp(*argv,"-Verify") == 0)
00611                         {
00612                         s_server_verify=SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT|
00613                                 SSL_VERIFY_CLIENT_ONCE;
00614                         if (--argc < 1) goto bad;
00615                         verify_depth=atoi(*(++argv));
00616                         BIO_printf(bio_err,"verify depth is %d, must return a certificate\n",verify_depth);
00617                         }
00618                 else if (strcmp(*argv,"-context") == 0)
00619                         {
00620                         if (--argc < 1) goto bad;
00621                         context= (unsigned char *)*(++argv);
00622                         }
00623                 else if (strcmp(*argv,"-cert") == 0)
00624                         {
00625                         if (--argc < 1) goto bad;
00626                         s_cert_file= *(++argv);
00627                         }
00628                 else if (strcmp(*argv,"-certform") == 0)
00629                         {
00630                         if (--argc < 1) goto bad;
00631                         s_cert_format = str2fmt(*(++argv));
00632                         }
00633                 else if (strcmp(*argv,"-key") == 0)
00634                         {
00635                         if (--argc < 1) goto bad;
00636                         s_key_file= *(++argv);
00637                         }
00638                 else if (strcmp(*argv,"-keyform") == 0)
00639                         {
00640                         if (--argc < 1) goto bad;
00641                         s_key_format = str2fmt(*(++argv));
00642                         }
00643                 else if (strcmp(*argv,"-pass") == 0)
00644                         {
00645                         if (--argc < 1) goto bad;
00646                         passarg = *(++argv);
00647                         }
00648                 else if (strcmp(*argv,"-dhparam") == 0)
00649                         {
00650                         if (--argc < 1) goto bad;
00651                         dhfile = *(++argv);
00652                         }
00653 #ifndef OPENSSL_NO_ECDH         
00654                 else if (strcmp(*argv,"-named_curve") == 0)
00655                         {
00656                         if (--argc < 1) goto bad;
00657                         named_curve = *(++argv);
00658                         }
00659 #endif
00660                 else if (strcmp(*argv,"-dcertform") == 0)
00661                         {
00662                         if (--argc < 1) goto bad;
00663                         s_dcert_format = str2fmt(*(++argv));
00664                         }
00665                 else if (strcmp(*argv,"-dcert") == 0)
00666                         {
00667                         if (--argc < 1) goto bad;
00668                         s_dcert_file= *(++argv);
00669                         }
00670                 else if (strcmp(*argv,"-dkeyform") == 0)
00671                         {
00672                         if (--argc < 1) goto bad;
00673                         s_dkey_format = str2fmt(*(++argv));
00674                         }
00675                 else if (strcmp(*argv,"-dpass") == 0)
00676                         {
00677                         if (--argc < 1) goto bad;
00678                         dpassarg = *(++argv);
00679                         }
00680                 else if (strcmp(*argv,"-dkey") == 0)
00681                         {
00682                         if (--argc < 1) goto bad;
00683                         s_dkey_file= *(++argv);
00684                         }
00685                 else if (strcmp(*argv,"-nocert") == 0)
00686                         {
00687                         nocert=1;
00688                         }
00689                 else if (strcmp(*argv,"-CApath") == 0)
00690                         {
00691                         if (--argc < 1) goto bad;
00692                         CApath= *(++argv);
00693                         }
00694                 else if (strcmp(*argv,"-crl_check") == 0)
00695                         {
00696                         vflags |= X509_V_FLAG_CRL_CHECK;
00697                         }
00698                 else if (strcmp(*argv,"-crl_check") == 0)
00699                         {
00700                         vflags |= X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL;
00701                         }
00702                 else if (strcmp(*argv,"-serverpref") == 0)
00703                         { off|=SSL_OP_CIPHER_SERVER_PREFERENCE; }
00704                 else if (strcmp(*argv,"-cipher") == 0)
00705                         {
00706                         if (--argc < 1) goto bad;
00707                         cipher= *(++argv);
00708                         }
00709                 else if (strcmp(*argv,"-CAfile") == 0)
00710                         {
00711                         if (--argc < 1) goto bad;
00712                         CAfile= *(++argv);
00713                         }
00714 #ifdef FIONBIO  
00715                 else if (strcmp(*argv,"-nbio") == 0)
00716                         { s_nbio=1; }
00717 #endif
00718                 else if (strcmp(*argv,"-nbio_test") == 0)
00719                         {
00720 #ifdef FIONBIO  
00721                         s_nbio=1;
00722 #endif
00723                         s_nbio_test=1;
00724                         }
00725                 else if (strcmp(*argv,"-debug") == 0)
00726                         { s_debug=1; }
00727                 else if (strcmp(*argv,"-msg") == 0)
00728                         { s_msg=1; }
00729                 else if (strcmp(*argv,"-hack") == 0)
00730                         { hack=1; }
00731                 else if (strcmp(*argv,"-state") == 0)
00732                         { state=1; }
00733                 else if (strcmp(*argv,"-crlf") == 0)
00734                         { s_crlf=1; }
00735                 else if (strcmp(*argv,"-quiet") == 0)
00736                         { s_quiet=1; }
00737                 else if (strcmp(*argv,"-bugs") == 0)
00738                         { bugs=1; }
00739                 else if (strcmp(*argv,"-no_tmp_rsa") == 0)
00740                         { no_tmp_rsa=1; }
00741                 else if (strcmp(*argv,"-no_dhe") == 0)
00742                         { no_dhe=1; }
00743                 else if (strcmp(*argv,"-no_ecdhe") == 0)
00744                         { no_ecdhe=1; }
00745                 else if (strcmp(*argv,"-www") == 0)
00746                         { www=1; }
00747                 else if (strcmp(*argv,"-WWW") == 0)
00748                         { www=2; }
00749                 else if (strcmp(*argv,"-HTTP") == 0)
00750                         { www=3; }
00751                 else if (strcmp(*argv,"-no_ssl2") == 0)
00752                         { off|=SSL_OP_NO_SSLv2; }
00753                 else if (strcmp(*argv,"-no_ssl3") == 0)
00754                         { off|=SSL_OP_NO_SSLv3; }
00755                 else if (strcmp(*argv,"-no_tls1") == 0)
00756                         { off|=SSL_OP_NO_TLSv1; }
00757 #ifndef OPENSSL_NO_SSL2
00758                 else if (strcmp(*argv,"-ssl2") == 0)
00759                         { meth=SSLv2_server_method(); }
00760 #endif
00761 #ifndef OPENSSL_NO_SSL3
00762                 else if (strcmp(*argv,"-ssl3") == 0)
00763                         { meth=SSLv3_server_method(); }
00764 #endif
00765 #ifndef OPENSSL_NO_TLS1
00766                 else if (strcmp(*argv,"-tls1") == 0)
00767                         { meth=TLSv1_server_method(); }
00768 #endif
00769 #ifndef OPENSSL_NO_DTLS1
00770                 else if (strcmp(*argv,"-dtls1") == 0)
00771                         { 
00772                         meth=DTLSv1_server_method();
00773                         sock_type = SOCK_DGRAM;
00774                         }
00775                 else if (strcmp(*argv,"-timeout") == 0)
00776                         enable_timeouts = 1;
00777                 else if (strcmp(*argv,"-mtu") == 0)
00778                         {
00779                         if (--argc < 1) goto bad;
00780                         mtu = atol(*(++argv));
00781                         }
00782                 else if (strcmp(*argv, "-chain") == 0)
00783                         cert_chain = 1;
00784 #endif
00785                 else if (strcmp(*argv, "-id_prefix") == 0)
00786                         {
00787                         if (--argc < 1) goto bad;
00788                         session_id_prefix = *(++argv);
00789                         }
00790 #ifndef OPENSSL_NO_ENGINE
00791                 else if (strcmp(*argv,"-engine") == 0)
00792                         {
00793                         if (--argc < 1) goto bad;
00794                         engine_id= *(++argv);
00795                         }
00796 #endif
00797                 else if (strcmp(*argv,"-rand") == 0)
00798                         {
00799                         if (--argc < 1) goto bad;
00800                         inrand= *(++argv);
00801                         }
00802                 else
00803                         {
00804                         BIO_printf(bio_err,"unknown option %s\n",*argv);
00805                         badop=1;
00806                         break;
00807                         }
00808                 argc--;
00809                 argv++;
00810                 }
00811         if (badop)
00812                 {
00813 bad:
00814                 sv_usage();
00815                 goto end;
00816                 }
00817 
00818         SSL_load_error_strings();
00819         OpenSSL_add_ssl_algorithms();
00820 
00821 #ifndef OPENSSL_NO_ENGINE
00822         e = setup_engine(bio_err, engine_id, 1);
00823 #endif
00824 
00825         if (!app_passwd(bio_err, passarg, dpassarg, &pass, &dpass))
00826                 {
00827                 BIO_printf(bio_err, "Error getting password\n");
00828                 goto end;
00829                 }
00830 
00831 
00832         if (s_key_file == NULL)
00833                 s_key_file = s_cert_file;
00834 
00835         if (nocert == 0)
00836                 {
00837                 s_key = load_key(bio_err, s_key_file, s_key_format, 0, pass, e,
00838                        "server certificate private key file");
00839                 if (!s_key)
00840                         {
00841                         ERR_print_errors(bio_err);
00842                         goto end;
00843                         }
00844 
00845                 s_cert = load_cert(bio_err,s_cert_file,s_cert_format,
00846                         NULL, e, "server certificate file");
00847 
00848                 if (!s_cert)
00849                         {
00850                         ERR_print_errors(bio_err);
00851                         goto end;
00852                         }
00853                 }
00854 
00855         if (s_dcert_file)
00856                 {
00857 
00858                 if (s_dkey_file == NULL)
00859                         s_dkey_file = s_dcert_file;
00860 
00861                 s_dkey = load_key(bio_err, s_dkey_file, s_dkey_format,
00862                                 0, dpass, e,
00863                                "second certificate private key file");
00864                 if (!s_dkey)
00865                         {
00866                         ERR_print_errors(bio_err);
00867                         goto end;
00868                         }
00869 
00870                 s_dcert = load_cert(bio_err,s_dcert_file,s_dcert_format,
00871                                 NULL, e, "second server certificate file");
00872 
00873                 if (!s_dcert)
00874                         {
00875                         ERR_print_errors(bio_err);
00876                         goto end;
00877                         }
00878 
00879                 }
00880 
00881         if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL
00882                 && !RAND_status())
00883                 {
00884                 BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
00885                 }
00886         if (inrand != NULL)
00887                 BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
00888                         app_RAND_load_files(inrand));
00889 
00890         if (bio_s_out == NULL)
00891                 {
00892                 if (s_quiet && !s_debug && !s_msg)
00893                         {
00894                         bio_s_out=BIO_new(BIO_s_null());
00895                         }
00896                 else
00897                         {
00898                         if (bio_s_out == NULL)
00899                                 bio_s_out=BIO_new_fp(stdout,BIO_NOCLOSE);
00900                         }
00901                 }
00902 
00903 #if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
00904         if (nocert)
00905 #endif
00906                 {
00907                 s_cert_file=NULL;
00908                 s_key_file=NULL;
00909                 s_dcert_file=NULL;
00910                 s_dkey_file=NULL;
00911                 }
00912 
00913         ctx=SSL_CTX_new(meth);
00914         if (ctx == NULL)
00915                 {
00916                 ERR_print_errors(bio_err);
00917                 goto end;
00918                 }
00919         if (session_id_prefix)
00920                 {
00921                 if(strlen(session_id_prefix) >= 32)
00922                         BIO_printf(bio_err,
00923 "warning: id_prefix is too long, only one new session will be possible\n");
00924                 else if(strlen(session_id_prefix) >= 16)
00925                         BIO_printf(bio_err,
00926 "warning: id_prefix is too long if you use SSLv2\n");
00927                 if(!SSL_CTX_set_generate_session_id(ctx, generate_session_id))
00928                         {
00929                         BIO_printf(bio_err,"error setting 'id_prefix'\n");
00930                         ERR_print_errors(bio_err);
00931                         goto end;
00932                         }
00933                 BIO_printf(bio_err,"id_prefix '%s' set.\n", session_id_prefix);
00934                 }
00935         SSL_CTX_set_quiet_shutdown(ctx,1);
00936         if (bugs) SSL_CTX_set_options(ctx,SSL_OP_ALL);
00937         if (hack) SSL_CTX_set_options(ctx,SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG);
00938         SSL_CTX_set_options(ctx,off);
00939         /* DTLS: partial reads end up discarding unread UDP bytes :-( 
00940          * Setting read ahead solves this problem.
00941          */
00942         if (sock_type == SOCK_DGRAM) SSL_CTX_set_read_ahead(ctx, 1);
00943 
00944         if (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback);
00945 
00946         SSL_CTX_sess_set_cache_size(ctx,128);
00947 
00948 #if 0
00949         if (cipher == NULL) cipher=getenv("SSL_CIPHER");
00950 #endif
00951 
00952 #if 0
00953         if (s_cert_file == NULL)
00954                 {
00955                 BIO_printf(bio_err,"You must specify a certificate file for the server to use\n");
00956                 goto end;
00957                 }
00958 #endif
00959 
00960         if ((!SSL_CTX_load_verify_locations(ctx,CAfile,CApath)) ||
00961                 (!SSL_CTX_set_default_verify_paths(ctx)))
00962                 {
00963                 /* BIO_printf(bio_err,"X509_load_verify_locations\n"); */
00964                 ERR_print_errors(bio_err);
00965                 /* goto end; */
00966                 }
00967         store = SSL_CTX_get_cert_store(ctx);
00968         X509_STORE_set_flags(store, vflags);
00969 
00970 #ifndef OPENSSL_NO_DH
00971         if (!no_dhe)
00972                 {
00973                 DH *dh=NULL;
00974 
00975                 if (dhfile)
00976                         dh = load_dh_param(dhfile);
00977                 else if (s_cert_file)
00978                         dh = load_dh_param(s_cert_file);
00979 
00980                 if (dh != NULL)
00981                         {
00982                         BIO_printf(bio_s_out,"Setting temp DH parameters\n");
00983                         }
00984                 else
00985                         {
00986                         BIO_printf(bio_s_out,"Using default temp DH parameters\n");
00987                         dh=get_dh512();
00988                         }
00989                 (void)BIO_flush(bio_s_out);
00990 
00991                 SSL_CTX_set_tmp_dh(ctx,dh);
00992                 DH_free(dh);
00993                 }
00994 #endif
00995 
00996 #ifndef OPENSSL_NO_ECDH
00997         if (!no_ecdhe)
00998                 {
00999                 EC_KEY *ecdh=NULL;
01000 
01001                 if (named_curve)
01002                         {
01003                         int nid = OBJ_sn2nid(named_curve);
01004 
01005                         if (nid == 0)
01006                                 {
01007                                 BIO_printf(bio_err, "unknown curve name (%s)\n", 
01008                                         named_curve);
01009                                 goto end;
01010                                 }
01011                         ecdh = EC_KEY_new_by_curve_name(nid);
01012                         if (ecdh == NULL)
01013                                 {
01014                                 BIO_printf(bio_err, "unable to create curve (%s)\n", 
01015                                         named_curve);
01016                                 goto end;
01017                                 }
01018                         }
01019 
01020                 if (ecdh != NULL)
01021                         {
01022                         BIO_printf(bio_s_out,"Setting temp ECDH parameters\n");
01023                         }
01024                 else
01025                         {
01026                         BIO_printf(bio_s_out,"Using default temp ECDH parameters\n");
01027                         ecdh = EC_KEY_new_by_curve_name(NID_sect163r2);
01028                         if (ecdh == NULL) 
01029                                 {
01030                                 BIO_printf(bio_err, "unable to create curve (sect163r2)\n");
01031                                 goto end;
01032                                 }
01033                         }
01034                 (void)BIO_flush(bio_s_out);
01035 
01036                 SSL_CTX_set_tmp_ecdh(ctx,ecdh);
01037                 EC_KEY_free(ecdh);
01038                 }
01039 #endif
01040         
01041         if (!set_cert_key_stuff(ctx,s_cert,s_key))
01042                 goto end;
01043         if (s_dcert != NULL)
01044                 {
01045                 if (!set_cert_key_stuff(ctx,s_dcert,s_dkey))
01046                         goto end;
01047                 }
01048 
01049 #ifndef OPENSSL_NO_RSA
01050 #if 1
01051         if (!no_tmp_rsa)
01052                 SSL_CTX_set_tmp_rsa_callback(ctx,tmp_rsa_cb);
01053 #else
01054         if (!no_tmp_rsa && SSL_CTX_need_tmp_RSA(ctx))
01055                 {
01056                 RSA *rsa;
01057 
01058                 BIO_printf(bio_s_out,"Generating temp (512 bit) RSA key...");
01059                 BIO_flush(bio_s_out);
01060 
01061                 rsa=RSA_generate_key(512,RSA_F4,NULL);
01062 
01063                 if (!SSL_CTX_set_tmp_rsa(ctx,rsa))
01064                         {
01065                         ERR_print_errors(bio_err);
01066                         goto end;
01067                         }
01068                 RSA_free(rsa);
01069                 BIO_printf(bio_s_out,"\n");
01070                 }
01071 #endif
01072 #endif
01073 
01074         if (cipher != NULL)
01075                 if(!SSL_CTX_set_cipher_list(ctx,cipher)) {
01076                 BIO_printf(bio_err,"error setting cipher list\n");
01077                 ERR_print_errors(bio_err);
01078                 goto end;
01079         }
01080         SSL_CTX_set_verify(ctx,s_server_verify,verify_callback);
01081         SSL_CTX_set_session_id_context(ctx,(void*)&s_server_session_id_context,
01082                 sizeof s_server_session_id_context);
01083 
01084         if (CAfile != NULL)
01085             SSL_CTX_set_client_CA_list(ctx,SSL_load_client_CA_file(CAfile));
01086 
01087         BIO_printf(bio_s_out,"ACCEPT\n");
01088         if (www)
01089                 do_server(port,sock_type,&accept_socket,www_body,