Allow code which calls RSA temp key callback to cope
[openssl.git] / ssl / s23_clnt.c
index 57d3623f3f001bb5c54c71622d7b154a4507fc60..aaedf6a9bbcb4f42a7822be91123c530dab79679 100644 (file)
@@ -1,5 +1,5 @@
 /* ssl/s23_clnt.c */
-/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
+/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
  * This package is an SSL implementation written
  */
 
 #include <stdio.h>
-#include "buffer.h"
-#include "rand.h"
-#include "objects.h"
-#include "evp.h"
+#include <openssl/buffer.h>
+#include <openssl/rand.h>
+#include <openssl/objects.h>
+#include <openssl/evp.h>
 #include "ssl_locl.h"
 
-#define BREAK break
-
-#ifndef NOPROTO
+static SSL_METHOD *ssl23_get_client_method(int ver);
 static int ssl23_client_hello(SSL *s);
 static int ssl23_get_server_hello(SSL *s);
-#else
-static int ssl23_client_hello();
-static int ssl23_get_server_hello();
-#endif
-
-static SSL_METHOD *ssl23_get_client_method(ver)
-int ver;
+static SSL_METHOD *ssl23_get_client_method(int ver)
        {
-       if (ver == 2)
+#ifndef NO_SSL2
+       if (ver == SSL2_VERSION)
                return(SSLv2_client_method());
-       else if (ver == 3)
+#endif
+       if (ver == SSL3_VERSION)
                return(SSLv3_client_method());
+       else if (ver == TLS1_VERSION)
+               return(TLSv1_client_method());
        else
                return(NULL);
        }
 
-SSL_METHOD *SSLv23_client_method()
+SSL_METHOD *SSLv23_client_method(void)
        {
        static int init=1;
        static SSL_METHOD SSLv23_client_data;
 
        if (init)
                {
-               init=0;
                memcpy((char *)&SSLv23_client_data,
                        (char *)sslv23_base_method(),sizeof(SSL_METHOD));
                SSLv23_client_data.ssl_connect=ssl23_connect;
                SSLv23_client_data.get_ssl_method=ssl23_get_client_method;
+               init=0;
                }
        return(&SSLv23_client_data);
        }
 
-int ssl23_connect(s)
-SSL *s;
+int ssl23_connect(SSL *s)
        {
        BUF_MEM *buf;
        unsigned long Time=time(NULL);
@@ -109,9 +104,9 @@ SSL *s;
        int ret= -1;
        int new_state,state;
 
-       RAND_seed((unsigned char *)&Time,sizeof(Time));
+       RAND_add(&Time,sizeof(Time),0);
        ERR_clear_error();
-       errno=0;
+       clear_sys_error();
 
        if (s->info_callback != NULL)
                cb=s->info_callback;
@@ -132,9 +127,16 @@ SSL *s;
                case SSL_ST_BEFORE|SSL_ST_CONNECT:
                case SSL_ST_OK|SSL_ST_CONNECT:
 
+                       if (s->session != NULL)
+                               {
+                               SSLerr(SSL_F_SSL23_CONNECT,SSL_R_SSL23_DOING_SESSION_ID_REUSE);
+                               ret= -1;
+                               goto end;
+                               }
+                       s->server=0;
                        if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
 
-                       s->version=3;
+                       /* s->version=TLS1_VERSION; */
                        s->type=SSL_ST_CONNECT;
 
                        if (s->init_buf == NULL)
@@ -157,7 +159,7 @@ SSL *s;
                        ssl3_init_finished_mac(s);
 
                        s->state=SSL23_ST_CW_CLNT_HELLO_A;
-                       s->ctx->sess_connect++;
+                       s->ctx->stats.sess_connect++;
                        s->init_num=0;
                        break;
 
@@ -177,7 +179,7 @@ SSL *s;
                        ret=ssl23_get_server_hello(s);
                        if (ret >= 0) cb=NULL;
                        goto end;
-                       break;
+                       /* break; */
 
                default:
                        SSLerr(SSL_F_SSL23_CONNECT,SSL_R_UNKNOWN_STATE);
@@ -186,7 +188,7 @@ SSL *s;
                        /* break; */
                        }
 
-               if (s->debug) BIO_flush(s->wbio);
+               if (s->debug) { (void)BIO_flush(s->wbio); }
 
                if ((cb != NULL) && (s->state != state))
                        {
@@ -204,8 +206,7 @@ end:
        }
 
 
-static int ssl23_client_hello(s)
-SSL *s;
+static int ssl23_client_hello(SSL *s)
        {
        unsigned char *buf;
        unsigned char *p,*d;
@@ -223,15 +224,36 @@ SSL *s;
 #endif
 
                p=s->s3->client_random;
-               RAND_bytes(p,SSL3_RANDOM_SIZE);
+               RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE);
 
                /* Do the message type and length last */
                d= &(buf[2]);
                p=d+9;
 
                *(d++)=SSL2_MT_CLIENT_HELLO;
-               *(d++)=SSL3_VERSION_MAJOR;
-               *(d++)=SSL3_VERSION_MINOR;
+               if (!(s->options & SSL_OP_NO_TLSv1))
+                       {
+                       *(d++)=TLS1_VERSION_MAJOR;
+                       *(d++)=TLS1_VERSION_MINOR;
+                       s->client_version=TLS1_VERSION;
+                       }
+               else if (!(s->options & SSL_OP_NO_SSLv3))
+                       {
+                       *(d++)=SSL3_VERSION_MAJOR;
+                       *(d++)=SSL3_VERSION_MINOR;
+                       s->client_version=SSL3_VERSION;
+                       }
+               else if (!(s->options & SSL_OP_NO_SSLv2))
+                       {
+                       *(d++)=SSL2_VERSION_MAJOR;
+                       *(d++)=SSL2_VERSION_MINOR;
+                       s->client_version=SSL2_VERSION;
+                       }
+               else
+                       {
+                       SSLerr(SSL_F_SSL23_CLIENT_HELLO,SSL_R_NO_PROTOCOLS_AVAILABLE);
+                       return(-1);
+                       }
 
                /* Ciphers supported */
                i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),p);
@@ -251,7 +273,7 @@ SSL *s;
 #endif
                s2n(0,d);
 
-               if (s->ctx->options & SSL_OP_NETSCAPE_CHALLENGE_BUG)
+               if (s->options & SSL_OP_NETSCAPE_CHALLENGE_BUG)
                        ch_len=SSL2_CHALLENGE_LENGTH;
                else
                        ch_len=SSL2_MAX_CHALLENGE_LENGTH;
@@ -263,7 +285,7 @@ SSL *s;
                        i=ch_len;
                s2n(i,d);
                memset(&(s->s3->client_random[0]),0,SSL3_RANDOM_SIZE);
-               RAND_bytes(&(s->s3->client_random[SSL3_RANDOM_SIZE-i]),i);
+               RAND_pseudo_bytes(&(s->s3->client_random[SSL3_RANDOM_SIZE-i]),i);
                memcpy(p,&(s->s3->client_random[SSL3_RANDOM_SIZE-i]),i);
                p+=i;
 
@@ -283,14 +305,12 @@ SSL *s;
        return(ssl23_write_bytes(s));
        }
 
-static int ssl23_get_server_hello(s)
-SSL *s;
+static int ssl23_get_server_hello(SSL *s)
        {
        char buf[8];
        unsigned char *p;
-       int i,ch_len;
+       int i;
        int n;
-       BIO *bbio;
 
        n=ssl23_read_bytes(s,7);
 
@@ -302,10 +322,20 @@ SSL *s;
        if ((p[0] & 0x80) && (p[2] == SSL2_MT_SERVER_HELLO) &&
                (p[5] == 0x00) && (p[6] == 0x02))
                {
+#ifdef NO_SSL2
+               SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
+               goto err;
+#else
                /* we are talking sslv2 */
                /* we need to clean up the SSLv3 setup and put in the
                 * sslv2 stuff. */
+               int ch_len;
 
+               if (s->options & SSL_OP_NO_SSLv2)
+                       {
+                       SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
+                       goto err;
+                       }
                if (s->s2 == NULL)
                        {
                        if (!ssl2_new(s))
@@ -314,7 +344,7 @@ SSL *s;
                else
                        ssl2_clear(s);
 
-               if (s->ctx->options & SSL_OP_NETSCAPE_CHALLENGE_BUG)
+               if (s->options & SSL_OP_NETSCAPE_CHALLENGE_BUG)
                        ch_len=SSL2_CHALLENGE_LENGTH;
                else
                        ch_len=SSL2_MAX_CHALLENGE_LENGTH;
@@ -352,36 +382,17 @@ SSL *s;
 
                s->method=SSLv2_client_method();
                s->handshake_func=s->method->ssl_connect;
+#endif
                }
        else if ((p[0] == SSL3_RT_HANDSHAKE) &&
                 (p[1] == SSL3_VERSION_MAJOR) &&
-                (p[2] == SSL3_VERSION_MINOR) &&
+                ((p[2] == SSL3_VERSION_MINOR) ||
+                 (p[2] == TLS1_VERSION_MINOR)) &&
                 (p[5] == SSL3_MT_SERVER_HELLO))
                {
-               /* we have sslv3 */
-
-               if (s->bbio == NULL)
-                       {
-                       bbio=BIO_new(BIO_f_buffer());
-                       if (bbio == NULL)
-                               {
-                               SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,ERR_R_BUF_LIB);
-                               goto err;
-                               }
-                       s->bbio=bbio;
-                       }
-               else
-                       bbio=s->bbio;
-
-               BIO_reset(bbio);
-               if (!BIO_set_write_buffer_size(bbio,16*1024))
-                       {
-                       SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,ERR_R_BUF_LIB);
-                       goto err;
-                       }
+               /* we have sslv3 or tls1 */
 
-               /* start the buffering */
-               s->wbio=BIO_push(s->bbio,s->wbio);
+               if (!ssl_init_wbio_buffer(s,1)) goto err;
 
                /* we are in this state */
                s->state=SSL3_ST_CR_SRVR_HELLO_A;
@@ -395,12 +406,30 @@ SSL *s;
                s->s3->rbuf.left=n;
                s->s3->rbuf.offset=0;
 
-               s->method=SSLv3_client_method();
+               if ((p[2] == SSL3_VERSION_MINOR) &&
+                       !(s->options & SSL_OP_NO_SSLv3))
+                       {
+                       s->version=SSL3_VERSION;
+                       s->method=SSLv3_client_method();
+                       }
+               else if ((p[2] == TLS1_VERSION_MINOR) &&
+                       !(s->options & SSL_OP_NO_TLSv1))
+                       {
+                       s->version=TLS1_VERSION;
+                       s->method=TLSv1_client_method();
+                       }
+               else
+                       {
+                       SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
+                       goto err;
+                       }
+                       
                s->handshake_func=s->method->ssl_connect;
                }
        else if ((p[0] == SSL3_RT_ALERT) &&
                 (p[1] == SSL3_VERSION_MAJOR) &&
-                (p[2] == SSL3_VERSION_MINOR) &&
+                ((p[2] == SSL3_VERSION_MINOR) ||
+                 (p[2] == TLS1_VERSION_MINOR)) &&
                 (p[3] == 0) &&
                 (p[4] == 2))
                {
@@ -421,7 +450,7 @@ SSL *s;
                        }
 
                s->rwstate=SSL_NOTHING;
-               SSLerr(SSL_F_SSL3_READ_BYTES,1000+p[6]);
+               SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_AD_REASON_OFFSET+p[6]);
                goto err;
                }
        else