Set up the engine before doing anything random-related, since engine randomness
[openssl.git] / apps / s_server.c
index 561e3a2358fbbc951c7a6fa55e9e6b4db93da1d4..85d3b30ec11c1f9ce76ee3d101b947b56a55b7eb 100644 (file)
  * copied and put under another distribution licence
  * [including the GNU Public Licence.]
  */
+/* ====================================================================
+ * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer. 
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * 3. All advertising materials mentioning features or use of this
+ *    software must display the following acknowledgment:
+ *    "This product includes software developed by the OpenSSL Project
+ *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
+ *
+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
+ *    endorse or promote products derived from this software without
+ *    prior written permission. For written permission, please contact
+ *    openssl-core@openssl.org.
+ *
+ * 5. Products derived from this software may not be called "OpenSSL"
+ *    nor may "OpenSSL" appear in their names without prior written
+ *    permission of the OpenSSL Project.
+ *
+ * 6. Redistributions of any form whatsoever must retain the following
+ *    acknowledgment:
+ *    "This product includes software developed by the OpenSSL Project
+ *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This product includes cryptographic software written by Eric Young
+ * (eay@cryptsoft.com).  This product includes software written by Tim
+ * Hudson (tjh@cryptsoft.com).
+ *
+ */
 
 #include <assert.h>
 #include <stdio.h>
@@ -84,7 +137,7 @@ typedef unsigned int u_int;
 #include <openssl/pem.h>
 #include <openssl/x509.h>
 #include <openssl/ssl.h>
-#include <openssl/engine.h>
+#include <openssl/rand.h>
 #include "s_apps.h"
 
 #ifdef OPENSSL_SYS_WINDOWS
@@ -177,6 +230,7 @@ static int www=0;
 
 static BIO *bio_s_out=NULL;
 static int s_debug=0;
+static int s_msg=0;
 static int s_quiet=0;
 
 static int hack=0;
@@ -202,6 +256,7 @@ static void s_server_init(void)
 
        bio_s_out=NULL;
        s_debug=0;
+       s_msg=0;
        s_quiet=0;
        hack=0;
        engine_id=NULL;
@@ -230,6 +285,7 @@ static void sv_usage(void)
        BIO_printf(bio_err," -nbio_test    - test with the non-blocking test bio\n");
        BIO_printf(bio_err," -crlf         - convert LF from terminal into CRLF\n");
        BIO_printf(bio_err," -debug        - Print more output\n");
+       BIO_printf(bio_err," -msg          - Show protocol messages\n");
        BIO_printf(bio_err," -state        - Print the SSL states\n");
        BIO_printf(bio_err," -CApath arg   - PEM format directory of CA's\n");
        BIO_printf(bio_err," -CAfile arg   - PEM format file of CA's\n");
@@ -250,6 +306,8 @@ static void sv_usage(void)
        BIO_printf(bio_err," -bugs         - Turn on SSL bug compatibility\n");
        BIO_printf(bio_err," -www          - Respond to a 'GET /' with a status page\n");
        BIO_printf(bio_err," -WWW          - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
+       BIO_printf(bio_err," -HTTP         - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
+        BIO_printf(bio_err,"                 with the assumption it contains a complete HTTP response.\n");
        BIO_printf(bio_err," -engine id    - Initialise and use the specified engine\n");
        BIO_printf(bio_err," -id_prefix arg - Generate SSL/TLS session IDs prefixed by 'arg'\n");
        BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
@@ -412,6 +470,8 @@ int MAIN(int, char **);
 
 int MAIN(int argc, char *argv[])
        {
+       X509_STORE *store = NULL;
+       int vflags = 0;
        short port=PORT;
        char *CApath=NULL,*CAfile=NULL;
        char *context = NULL;
@@ -444,6 +504,9 @@ int MAIN(int argc, char *argv[])
        if (bio_err == NULL)
                bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
 
+       if (!load_config(bio_err, NULL))
+               goto end;
+
        verify_depth=0;
 #ifdef FIONBIO
        s_nbio=0;
@@ -516,6 +579,14 @@ int MAIN(int argc, char *argv[])
                        if (--argc < 1) goto bad;
                        CApath= *(++argv);
                        }
+               else if (strcmp(*argv,"-crl_check") == 0)
+                       {
+                       vflags |= X509_V_FLAG_CRL_CHECK;
+                       }
+               else if (strcmp(*argv,"-crl_check") == 0)
+                       {
+                       vflags |= X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL;
+                       }
                else if (strcmp(*argv,"-serverpref") == 0)
                        { off|=SSL_OP_CIPHER_SERVER_PREFERENCE; }
                else if (strcmp(*argv,"-cipher") == 0)
@@ -541,6 +612,8 @@ int MAIN(int argc, char *argv[])
                        }
                else if (strcmp(*argv,"-debug") == 0)
                        { s_debug=1; }
+               else if (strcmp(*argv,"-msg") == 0)
+                       { s_msg=1; }
                else if (strcmp(*argv,"-hack") == 0)
                        { hack=1; }
                else if (strcmp(*argv,"-state") == 0)
@@ -559,6 +632,8 @@ int MAIN(int argc, char *argv[])
                        { www=1; }
                else if (strcmp(*argv,"-WWW") == 0)
                        { www=2; }
+               else if (strcmp(*argv,"-HTTP") == 0)
+                       { www=3; }
                else if (strcmp(*argv,"-no_ssl2") == 0)
                        { off|=SSL_OP_NO_SSLv2; }
                else if (strcmp(*argv,"-no_ssl3") == 0)
@@ -608,6 +683,11 @@ bad:
                goto end;
                }
 
+       SSL_load_error_strings();
+       OpenSSL_add_ssl_algorithms();
+
+        e = setup_engine(bio_err, engine_id, 1);
+
        if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL
                && !RAND_status())
                {
@@ -619,7 +699,7 @@ bad:
 
        if (bio_s_out == NULL)
                {
-               if (s_quiet && !s_debug)
+               if (s_quiet && !s_debug && !s_msg)
                        {
                        bio_s_out=BIO_new(BIO_s_null());
                        }
@@ -630,7 +710,7 @@ bad:
                        }
                }
 
-#if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA)
+#if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
        if (nocert)
 #endif
                {
@@ -640,32 +720,6 @@ bad:
                s_dkey_file=NULL;
                }
 
-       SSL_load_error_strings();
-       OpenSSL_add_ssl_algorithms();
-
-       if (engine_id != NULL)
-               {
-               if((e = ENGINE_by_id(engine_id)) == NULL)
-                       {
-                       BIO_printf(bio_err,"invalid engine\n");
-                       ERR_print_errors(bio_err);
-                       goto end;
-                       }
-               if (s_debug)
-                       {
-                       ENGINE_ctrl(e, ENGINE_CTRL_SET_LOGSTREAM,
-                               0, bio_err, 0);
-                       }
-               if(!ENGINE_set_default(e, ENGINE_METHOD_ALL))
-                       {
-                       BIO_printf(bio_err,"can't use that engine\n");
-                       ERR_print_errors(bio_err);
-                       goto end;
-                       }
-               BIO_printf(bio_err,"engine \"%s\" set.\n", engine_id);
-               ENGINE_free(e);
-               }
-
        ctx=SSL_CTX_new(meth);
        if (ctx == NULL)
                {
@@ -692,7 +746,6 @@ bad:
        if (bugs) SSL_CTX_set_options(ctx,SSL_OP_ALL);
        if (hack) SSL_CTX_set_options(ctx,SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG);
        SSL_CTX_set_options(ctx,off);
-       if (hack) SSL_CTX_set_options(ctx,SSL_OP_NON_EXPORT_FIRST);
 
        if (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback);
 
@@ -717,6 +770,8 @@ bad:
                ERR_print_errors(bio_err);
                /* goto end; */
                }
+       store = SSL_CTX_get_cert_store(ctx);
+       X509_STORE_set_flags(store, vflags);
 
 #ifndef OPENSSL_NO_DH
        if (!no_dhe)
@@ -804,6 +859,7 @@ end:
                BIO_free(bio_s_out);
                bio_s_out=NULL;
                }
+       apps_shutdown();
        EXIT(ret);
        }
 
@@ -867,8 +923,10 @@ static int sv_body(char *hostname, int s, unsigned char *context)
 #ifndef OPENSSL_NO_KRB5
                if ((con->kssl_ctx = kssl_ctx_new()) != NULL)
                         {
-                        kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVICE, KRB5SVC);
-                        kssl_ctx_setstring(con->kssl_ctx, KSSL_KEYTAB, KRB5KEYTAB);
+                        kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVICE,
+                                                               KRB5SVC);
+                        kssl_ctx_setstring(con->kssl_ctx, KSSL_KEYTAB,
+                                                               KRB5KEYTAB);
                         }
 #endif /* OPENSSL_NO_KRB5 */
                if(context)
@@ -895,6 +953,11 @@ static int sv_body(char *hostname, int s, unsigned char *context)
                BIO_set_callback(SSL_get_rbio(con),bio_dump_cb);
                BIO_set_callback_arg(SSL_get_rbio(con),bio_s_out);
                }
+       if (s_msg)
+               {
+               SSL_set_msg_callback(con, msg_cb);
+               SSL_set_msg_callback_arg(con, bio_s_out);
+               }
 
        width=s+1;
        for (;;)
@@ -1253,6 +1316,13 @@ static int www_body(char *hostname, int s, unsigned char *context)
        if (!BIO_set_write_buffer_size(io,bufsize)) goto err;
 
        if ((con=SSL_new(ctx)) == NULL) goto err;
+#ifndef OPENSSL_NO_KRB5
+       if ((con->kssl_ctx = kssl_ctx_new()) != NULL)
+               {
+               kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVICE, KRB5SVC);
+               kssl_ctx_setstring(con->kssl_ctx, KSSL_KEYTAB, KRB5KEYTAB);
+               }
+#endif /* OPENSSL_NO_KRB5 */
        if(context) SSL_set_session_id_context(con, context,
                                               strlen((char *)context));
 
@@ -1280,6 +1350,11 @@ static int www_body(char *hostname, int s, unsigned char *context)
                BIO_set_callback(SSL_get_rbio(con),bio_dump_cb);
                BIO_set_callback_arg(SSL_get_rbio(con),bio_s_out);
                }
+       if (s_msg)
+               {
+               SSL_set_msg_callback(con, msg_cb);
+               SSL_set_msg_callback_arg(con, bio_s_out);
+               }
 
        blank=0;
        for (;;)
@@ -1414,7 +1489,8 @@ static int www_body(char *hostname, int s, unsigned char *context)
                        BIO_puts(io,"</BODY></HTML>\r\n\r\n");
                        break;
                        }
-               else if ((www == 2) && (strncmp("GET /",buf,5) == 0))
+               else if ((www == 2 || www == 3)
+                         && (strncmp("GET /",buf,5) == 0))
                        {
                        BIO *file;
                        char *p,*e;
@@ -1422,15 +1498,29 @@ static int www_body(char *hostname, int s, unsigned char *context)
 
                        /* skip the '/' */
                        p= &(buf[5]);
-                       dot=0;
+
+                       dot = 1;
                        for (e=p; *e != '\0'; e++)
                                {
-                               if (e[0] == ' ') break;
-                               if (    (e[0] == '.') &&
-                                       (strncmp(&(e[-1]),"/../",4) == 0))
-                                       dot=1;
+                               if (e[0] == ' ')
+                                       break;
+
+                               switch (dot)
+                                       {
+                               case 1:
+                                       dot = (e[0] == '.') ? 2 : 0;
+                                       break;
+                               case 2:
+                                       dot = (e[0] == '.') ? 3 : 0;
+                                       break;
+                               case 3:
+                                       dot = (e[0] == '/') ? -1 : 0;
+                                       break;
+                                       }
+                               if (dot == 0)
+                                       dot = (e[0] == '/') ? 1 : 0;
                                }
-                       
+                       dot = (dot == 3) || (dot == -1); /* filename contains ".." component */
 
                        if (*e == '\0')
                                {
@@ -1454,9 +1544,11 @@ static int www_body(char *hostname, int s, unsigned char *context)
                                break;
                                }
 
+#if 0
                        /* append if a directory lookup */
                        if (e[-1] == '/')
                                strcat(p,"index.html");
+#endif
 
                        /* if a directory, do the index thang */
                        if (stat(p,&st_buf) < 0)
@@ -1468,7 +1560,13 @@ static int www_body(char *hostname, int s, unsigned char *context)
                                }
                        if (S_ISDIR(st_buf.st_mode))
                                {
+#if 0 /* must check buffer size */
                                strcat(p,"/index.html");
+#else
+                               BIO_puts(io,text);
+                               BIO_printf(io,"'%s' is a directory\r\n",p);
+                               break;
+#endif
                                }
 
                        if ((file=BIO_new_file(p,"r")) == NULL)
@@ -1482,13 +1580,16 @@ static int www_body(char *hostname, int s, unsigned char *context)
                        if (!s_quiet)
                                BIO_printf(bio_err,"FILE:%s\n",p);
 
-                       i=strlen(p);
-                       if (    ((i > 5) && (strcmp(&(p[i-5]),".html") == 0)) ||
-                               ((i > 4) && (strcmp(&(p[i-4]),".php") == 0)) ||
-                               ((i > 4) && (strcmp(&(p[i-4]),".htm") == 0)))
-                               BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
-                       else
-                               BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
+                        if (www == 2)
+                                {
+                                i=strlen(p);
+                                if (   ((i > 5) && (strcmp(&(p[i-5]),".html") == 0)) ||
+                                        ((i > 4) && (strcmp(&(p[i-4]),".php") == 0)) ||
+                                        ((i > 4) && (strcmp(&(p[i-4]),".htm") == 0)))
+                                        BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
+                                else
+                                        BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
+                                }
                        /* send the file */
                        total_bytes=0;
                        for (;;)
@@ -1605,7 +1706,7 @@ static int generate_session_id(const SSL *ssl, unsigned char *id,
                        (strlen(session_id_prefix) < *id_len) ?
                        strlen(session_id_prefix) : *id_len);
                }
-       while(SSL_CTX_has_matching_session_id(ssl->ctx, id, *id_len) &&
+       while(SSL_has_matching_session_id(ssl, id, *id_len) &&
                (++count < MAX_SESSION_ID_ATTEMPTS));
        if(count >= MAX_SESSION_ID_ATTEMPTS)
                return 0;