Fix various incorrect error function codes.
[openssl.git] / ssl / s3_clnt.c
index 96631db23042e1476549c6fe11ed438f4606876a..b40571dd5a6417e90cb7f939f97431c518e58625 100644 (file)
 #include <openssl/objects.h>
 #include <openssl/evp.h>
 #include <openssl/md5.h>
 #include <openssl/objects.h>
 #include <openssl/evp.h>
 #include <openssl/md5.h>
-#include "cryptlib.h"
+#include <openssl/dh.h>
+#include <openssl/bn.h>
 
 static SSL_METHOD *ssl3_get_client_method(int ver);
 
 static SSL_METHOD *ssl3_get_client_method(int ver);
-static int ssl3_client_hello(SSL *s);
-static int ssl3_get_server_hello(SSL *s);
-static int ssl3_get_certificate_request(SSL *s);
 static int ca_dn_cmp(const X509_NAME * const *a,const X509_NAME * const *b);
 static int ca_dn_cmp(const X509_NAME * const *a,const X509_NAME * const *b);
-static int ssl3_get_server_done(SSL *s);
-static int ssl3_send_client_verify(SSL *s);
-static int ssl3_send_client_certificate(SSL *s);
-static int ssl3_send_client_key_exchange(SSL *s);
-static int ssl3_get_key_exchange(SSL *s);
-static int ssl3_get_server_certificate(SSL *s);
-static int ssl3_check_cert_and_algorithm(SSL *s);
 
 #ifndef OPENSSL_NO_ECDH
 static int curve_id2nid(int curve_id);
 
 #ifndef OPENSSL_NO_ECDH
 static int curve_id2nid(int curve_id);
@@ -538,7 +529,7 @@ end:
        }
 
 
        }
 
 
-static int ssl3_client_hello(SSL *s)
+int ssl3_client_hello(SSL *s)
        {
        unsigned char *buf;
        unsigned char *p,*d;
        {
        unsigned char *buf;
        unsigned char *p,*d;
@@ -561,7 +552,7 @@ static int ssl3_client_hello(SSL *s)
                p=s->s3->client_random;
                Time=time(NULL);                        /* Time */
                l2n(Time,p);
                p=s->s3->client_random;
                Time=time(NULL);                        /* Time */
                l2n(Time,p);
-               RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
+               RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4);
 
                /* Do the message type and length last */
                d=p= &(buf[4]);
 
                /* Do the message type and length last */
                d=p= &(buf[4]);
@@ -631,7 +622,7 @@ err:
        return(-1);
        }
 
        return(-1);
        }
 
-static int ssl3_get_server_hello(SSL *s)
+int ssl3_get_server_hello(SSL *s)
        {
        STACK_OF(SSL_CIPHER) *sk;
        SSL_CIPHER *c;
        {
        STACK_OF(SSL_CIPHER) *sk;
        SSL_CIPHER *c;
@@ -641,14 +632,40 @@ static int ssl3_get_server_hello(SSL *s)
        long n;
        SSL_COMP *comp;
 
        long n;
        SSL_COMP *comp;
 
-       n=ssl3_get_message(s,
+       n=s->method->ssl_get_message(s,
                SSL3_ST_CR_SRVR_HELLO_A,
                SSL3_ST_CR_SRVR_HELLO_B,
                SSL3_ST_CR_SRVR_HELLO_A,
                SSL3_ST_CR_SRVR_HELLO_B,
-               SSL3_MT_SERVER_HELLO,
+               -1,
                300, /* ?? */
                &ok);
 
        if (!ok) return((int)n);
                300, /* ?? */
                &ok);
 
        if (!ok) return((int)n);
+
+       if ( SSL_version(s) == DTLS1_VERSION)
+               {
+               if ( s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST)
+                       {
+                       if ( s->d1->send_cookie == 0)
+                               {
+                               s->s3->tmp.reuse_message = 1;
+                               return 1;
+                               }
+                       else /* already sent a cookie */
+                               {
+                               al=SSL_AD_UNEXPECTED_MESSAGE;
+                               SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE);
+                               goto f_err;
+                               }
+                       }
+               }
+       
+       if ( s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO)
+               {
+               al=SSL_AD_UNEXPECTED_MESSAGE;
+               SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE);
+               goto f_err;
+               }
+
        d=p=(unsigned char *)s->init_msg;
 
        if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))
        d=p=(unsigned char *)s->init_msg;
 
        if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))
@@ -776,18 +793,19 @@ err:
        return(-1);
        }
 
        return(-1);
        }
 
-static int ssl3_get_server_certificate(SSL *s)
+int ssl3_get_server_certificate(SSL *s)
        {
        int al,i,ok,ret= -1;
        unsigned long n,nc,llen,l;
        X509 *x=NULL;
        {
        int al,i,ok,ret= -1;
        unsigned long n,nc,llen,l;
        X509 *x=NULL;
-       unsigned char *p,*d,*q;
+       const unsigned char *q,*p;
+       unsigned char *d;
        STACK_OF(X509) *sk=NULL;
        SESS_CERT *sc;
        EVP_PKEY *pkey=NULL;
        int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */
 
        STACK_OF(X509) *sk=NULL;
        SESS_CERT *sc;
        EVP_PKEY *pkey=NULL;
        int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */
 
-       n=ssl3_get_message(s,
+       n=s->method->ssl_get_message(s,
                SSL3_ST_CR_CERT_A,
                SSL3_ST_CR_CERT_B,
                -1,
                SSL3_ST_CR_CERT_A,
                SSL3_ST_CR_CERT_B,
                -1,
@@ -808,7 +826,7 @@ static int ssl3_get_server_certificate(SSL *s)
                SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE);
                goto f_err;
                }
                SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE);
                goto f_err;
                }
-       d=p=(unsigned char *)s->init_msg;
+       p=d=(unsigned char *)s->init_msg;
 
        if ((sk=sk_X509_new_null()) == NULL)
                {
 
        if ((sk=sk_X509_new_null()) == NULL)
                {
@@ -959,7 +977,7 @@ err:
        return(ret);
        }
 
        return(ret);
        }
 
-static int ssl3_get_key_exchange(SSL *s)
+int ssl3_get_key_exchange(SSL *s)
        {
 #ifndef OPENSSL_NO_RSA
        unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
        {
 #ifndef OPENSSL_NO_RSA
        unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
@@ -985,7 +1003,7 @@ static int ssl3_get_key_exchange(SSL *s)
 
        /* use same message size as in ssl3_get_certificate_request()
         * as ServerKeyExchange message may be skipped */
 
        /* use same message size as in ssl3_get_certificate_request()
         * as ServerKeyExchange message may be skipped */
-       n=ssl3_get_message(s,
+       n=s->method->ssl_get_message(s,
                SSL3_ST_CR_KEY_EXCH_A,
                SSL3_ST_CR_KEY_EXCH_B,
                -1,
                SSL3_ST_CR_KEY_EXCH_A,
                SSL3_ST_CR_KEY_EXCH_B,
                -1,
@@ -1403,16 +1421,17 @@ err:
        return(-1);
        }
 
        return(-1);
        }
 
-static int ssl3_get_certificate_request(SSL *s)
+int ssl3_get_certificate_request(SSL *s)
        {
        int ok,ret=0;
        unsigned long n,nc,l;
        unsigned int llen,ctype_num,i;
        X509_NAME *xn=NULL;
        {
        int ok,ret=0;
        unsigned long n,nc,l;
        unsigned int llen,ctype_num,i;
        X509_NAME *xn=NULL;
-       unsigned char *p,*d,*q;
+       const unsigned char *p,*q;
+       unsigned char *d;
        STACK_OF(X509_NAME) *ca_sk=NULL;
 
        STACK_OF(X509_NAME) *ca_sk=NULL;
 
-       n=ssl3_get_message(s,
+       n=s->method->ssl_get_message(s,
                SSL3_ST_CR_CERT_REQ_A,
                SSL3_ST_CR_CERT_REQ_B,
                -1,
                SSL3_ST_CR_CERT_REQ_A,
                SSL3_ST_CR_CERT_REQ_B,
                -1,
@@ -1448,7 +1467,7 @@ static int ssl3_get_certificate_request(SSL *s)
                        }
                }
 
                        }
                }
 
-       d=p=(unsigned char *)s->init_msg;
+       p=d=(unsigned char *)s->init_msg;
 
        if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL)
                {
 
        if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL)
                {
@@ -1550,12 +1569,12 @@ static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b)
        return(X509_NAME_cmp(*a,*b));
        }
 
        return(X509_NAME_cmp(*a,*b));
        }
 
-static int ssl3_get_server_done(SSL *s)
+int ssl3_get_server_done(SSL *s)
        {
        int ok,ret=0;
        long n;
 
        {
        int ok,ret=0;
        long n;
 
-       n=ssl3_get_message(s,
+       n=s->method->ssl_get_message(s,
                SSL3_ST_CR_SRVR_DONE_A,
                SSL3_ST_CR_SRVR_DONE_B,
                SSL3_MT_SERVER_DONE,
                SSL3_ST_CR_SRVR_DONE_A,
                SSL3_ST_CR_SRVR_DONE_B,
                SSL3_MT_SERVER_DONE,
@@ -1576,18 +1595,20 @@ static int ssl3_get_server_done(SSL *s)
 
 
 static const int KDF1_SHA1_len = 20;
 
 
 static const int KDF1_SHA1_len = 20;
-static void *KDF1_SHA1(void *in, size_t inlen, void *out, size_t outlen)
+static void *KDF1_SHA1(const void *in, size_t inlen, void *out, size_t *outlen)
        {
 #ifndef OPENSSL_NO_SHA
        {
 #ifndef OPENSSL_NO_SHA
-       if (outlen != SHA_DIGEST_LENGTH)
+       if (*outlen < SHA_DIGEST_LENGTH)
                return NULL;
                return NULL;
+       else
+               *outlen = SHA_DIGEST_LENGTH;
        return SHA1(in, inlen, out);
 #else
        return NULL;
 #endif
        }
 
        return SHA1(in, inlen, out);
 #else
        return NULL;
 #endif
        }
 
-static int ssl3_send_client_key_exchange(SSL *s)
+int ssl3_send_client_key_exchange(SSL *s)
        {
        unsigned char *p,*d;
        int n;
        {
        unsigned char *p,*d;
        int n;
@@ -2082,7 +2103,7 @@ err:
        return(-1);
        }
 
        return(-1);
        }
 
-static int ssl3_send_client_verify(SSL *s)
+int ssl3_send_client_verify(SSL *s)
        {
        unsigned char *p,*d;
        unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
        {
        unsigned char *p,*d;
        unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
@@ -2170,7 +2191,7 @@ err:
        return(-1);
        }
 
        return(-1);
        }
 
-static int ssl3_send_client_certificate(SSL *s)
+int ssl3_send_client_certificate(SSL *s)
        {
        X509 *x509=NULL;
        EVP_PKEY *pkey=NULL;
        {
        X509 *x509=NULL;
        EVP_PKEY *pkey=NULL;
@@ -2249,7 +2270,7 @@ static int ssl3_send_client_certificate(SSL *s)
 
 #define has_bits(i,m)  (((i)&(m)) == (m))
 
 
 #define has_bits(i,m)  (((i)&(m)) == (m))
 
-static int ssl3_check_cert_and_algorithm(SSL *s)
+int ssl3_check_cert_and_algorithm(SSL *s)
        {
        int i,idx;
        long algs;
        {
        int i,idx;
        long algs;