"make update"
[openssl.git] / ssl / s3_srvr.c
index 2a9e115e9c5306cb43d71140ed3f2ca80005dcf5..dce89bf2bdad8e4deb87227b159fadca0a38400f 100644 (file)
 #include <openssl/x509.h>
 #include "ssl_locl.h"
 
+#ifndef OPENSSL_NO_KRB5
+#include "kssl.h"
+#endif /* OPENSSL_NO_KRB5 */
+
 static SSL_METHOD *ssl3_get_server_method(int ver);
 static int ssl3_get_client_hello(SSL *s);
 static int ssl3_check_client_hello(SSL *s);
@@ -153,7 +157,10 @@ int ssl3_accept(SSL *s)
                        if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
 
                        if ((s->version>>8) != 3)
-                               abort();
+                               {
+                               SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
+                               return -1;
+                               }
                        s->type=SSL_ST_ACCEPT;
 
                        if (s->init_buf == NULL)
@@ -259,7 +266,11 @@ int ssl3_accept(SSL *s)
 
                        /* clear this, it may get reset by
                         * send_server_key_exchange */
-                       if (s->options & SSL_OP_EPHEMERAL_RSA)
+                       if ((s->options & SSL_OP_EPHEMERAL_RSA)
+#ifndef OPENSSL_NO_KRB5
+                               && !(l & SSL_KRB5)
+#endif /* OPENSSL_NO_KRB5 */
+                               )
                                s->s3->tmp.use_rsa_tmp=1;
                        else
                                s->s3->tmp.use_rsa_tmp=0;
@@ -544,7 +555,7 @@ static int ssl3_check_client_hello(SSL *s)
                 * which will now be aborted. (A full SSL_clear would be too much.)
                 * I hope that tmp.dh is the only thing that may need to be cleared
                 * when a handshake is not completed ... */
-#ifndef NO_DH
+#ifndef OPENSSL_NO_DH
                if (s->s3->tmp.dh != NULL)
                        {
                        DH_free(s->s3->tmp.dh);
@@ -744,7 +755,7 @@ static int ssl3_get_client_hello(SSL *s)
                        }
                }
 
-       /* Given s->session->ciphers and ssl_get_ciphers_by_id(s), we must
+       /* Given s->session->ciphers and SSL_get_ciphers, we must
         * pick a cipher */
 
        if (!s->hit)
@@ -761,7 +772,7 @@ static int ssl3_get_client_hello(SSL *s)
                        }
                ciphers=NULL;
                c=ssl3_choose_cipher(s,s->session->ciphers,
-                                    ssl_get_ciphers_by_id(s));
+                                    SSL_get_ciphers(s));
 
                if (c == NULL)
                        {
@@ -916,14 +927,14 @@ static int ssl3_send_server_done(SSL *s)
 
 static int ssl3_send_server_key_exchange(SSL *s)
        {
-#ifndef NO_RSA
+#ifndef OPENSSL_NO_RSA
        unsigned char *q;
        int j,num;
        RSA *rsa;
        unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
        unsigned int u;
 #endif
-#ifndef NO_DH
+#ifndef OPENSSL_NO_DH
        DH *dh=NULL,*dhp;
 #endif
        EVP_PKEY *pkey;
@@ -946,7 +957,7 @@ static int ssl3_send_server_key_exchange(SSL *s)
 
                r[0]=r[1]=r[2]=r[3]=NULL;
                n=0;
-#ifndef NO_RSA
+#ifndef OPENSSL_NO_RSA
                if (type & SSL_kRSA)
                        {
                        rsa=cert->rsa_tmp;
@@ -955,6 +966,12 @@ static int ssl3_send_server_key_exchange(SSL *s)
                                rsa=s->cert->rsa_tmp_cb(s,
                                      SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
                                      SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
+                               if(rsa == NULL)
+                               {
+                                       al=SSL_AD_HANDSHAKE_FAILURE;
+                                       SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
+                                       goto f_err;
+                               }
                                CRYPTO_add(&rsa->references,1,CRYPTO_LOCK_RSA);
                                cert->rsa_tmp=rsa;
                                }
@@ -970,13 +987,13 @@ static int ssl3_send_server_key_exchange(SSL *s)
                        }
                else
 #endif
-#ifndef NO_DH
+#ifndef OPENSSL_NO_DH
                        if (type & SSL_kEDH)
                        {
                        dhp=cert->dh_tmp;
                        if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
                                dhp=s->cert->dh_tmp_cb(s,
-                                     !SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
+                                     SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
                                      SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
                        if (dhp == NULL)
                                {
@@ -988,7 +1005,7 @@ static int ssl3_send_server_key_exchange(SSL *s)
                        if (s->s3->tmp.dh != NULL)
                                {
                                DH_free(dh);
-                               SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, SSL_R_INTERNAL_ERROR);
+                               SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
                                goto err;
                                }
 
@@ -1074,7 +1091,7 @@ static int ssl3_send_server_key_exchange(SSL *s)
                        {
                        /* n is the length of the params, they start at &(d[4])
                         * and p points to the space at the end. */
-#ifndef NO_RSA
+#ifndef OPENSSL_NO_RSA
                        if (pkey->type == EVP_PKEY_RSA)
                                {
                                q=md_buf;
@@ -1102,7 +1119,7 @@ static int ssl3_send_server_key_exchange(SSL *s)
                                }
                        else
 #endif
-#if !defined(NO_DSA)
+#if !defined(OPENSSL_NO_DSA)
                                if (pkey->type == EVP_PKEY_DSA)
                                {
                                /* lets do DSS */
@@ -1240,20 +1257,23 @@ static int ssl3_get_client_key_exchange(SSL *s)
        long n;
        unsigned long l;
        unsigned char *p;
-#ifndef NO_RSA
+#ifndef OPENSSL_NO_RSA
        RSA *rsa=NULL;
        EVP_PKEY *pkey=NULL;
 #endif
-#ifndef NO_DH
+#ifndef OPENSSL_NO_DH
        BIGNUM *pub=NULL;
        DH *dh_srvr;
 #endif
+#ifndef OPENSSL_NO_KRB5
+        KSSL_ERR kssl_err;
+#endif /* OPENSSL_NO_KRB5 */
 
        n=ssl3_get_message(s,
                SSL3_ST_SR_KEY_EXCH_A,
                SSL3_ST_SR_KEY_EXCH_B,
                SSL3_MT_CLIENT_KEY_EXCHANGE,
-               400, /* ???? */
+               2048, /* ??? */
                &ok);
 
        if (!ok) return((int)n);
@@ -1261,7 +1281,7 @@ static int ssl3_get_client_key_exchange(SSL *s)
 
        l=s->s3->tmp.new_cipher->algorithms;
 
-#ifndef NO_RSA
+#ifndef OPENSSL_NO_RSA
        if (l & SSL_kRSA)
                {
                /* FIX THIS UP EAY EAY EAY EAY */
@@ -1320,13 +1340,23 @@ static int ssl3_get_client_key_exchange(SSL *s)
                        goto f_err;
                        }
 
-               if ((p[0] != (s->client_version>>8)) || (p[1] != (s->client_version & 0xff)))
+               if (!((p[0] == (s->client_version>>8)) && (p[1] == (s->client_version & 0xff))))
                        {
-                       al=SSL_AD_DECODE_ERROR;
-                       SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
-                       goto f_err;
+                       /* The premaster secret must contain the same version number as the
+                        * ClientHello to detect version rollback attacks (strangely, the
+                        * protocol does not offer such protection for DH ciphersuites).
+                        * However, buggy clients exist that send the negotiated protocol
+                        * version instead if the server does not support the requested
+                        * protocol version.
+                        * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */
+                       if (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) &&
+                               (p[0] == (s->version>>8)) && (p[1] == (s->version & 0xff))))
+                               {
+                               al=SSL_AD_DECODE_ERROR;
+                               SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
+                               goto f_err;
+                               }
                        }
-#endif
 
                s->session->master_key_length=
                        s->method->ssl3_enc->generate_master_secret(s,
@@ -1335,7 +1365,8 @@ static int ssl3_get_client_key_exchange(SSL *s)
                memset(p,0,i);
                }
        else
-#ifndef NO_DH
+#endif
+#ifndef OPENSSL_NO_DH
                if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
                {
                n2s(p,i);
@@ -1394,9 +1425,57 @@ static int ssl3_get_client_key_exchange(SSL *s)
                s->session->master_key_length=
                        s->method->ssl3_enc->generate_master_secret(s,
                                s->session->master_key,p,i);
+               memset(p,0,i);
                }
        else
 #endif
+#ifndef OPENSSL_NO_KRB5
+        if (l & SSL_kKRB5)
+                {
+                krb5_error_code        krb5rc;
+                KSSL_CTX       *kssl_ctx = s->kssl_ctx;
+
+                if (!kssl_ctx)  kssl_ctx = kssl_ctx_new();
+                if ((krb5rc = kssl_sget_tkt(kssl_ctx,
+                        s->init_buf->data, s->init_buf->length,
+                        &kssl_err)) != 0)
+                        {
+#ifdef KSSL_DEBUG
+                        printf("kssl_sget_tkt rtn %d [%d]\n",
+                                krb5rc, kssl_err.reason);
+                        if (kssl_err.text)
+                                printf("kssl_err text= %s\n", kssl_err.text);
+#endif /* KSSL_DEBUG */
+                        SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
+                                kssl_err.reason);
+                        goto err;
+                        }
+
+#ifdef KSSL_DEBUG
+                kssl_ctx_show(kssl_ctx);
+#endif /* KSSL_DEBUG */
+
+                /*     19991013 VRS -  3DES is kind of bogus here,
+                **     at least until Kerberos supports 3DES.  The only
+                **     real secret is the 8-byte Kerberos session key;
+                **     the other key material (client_random, server_random)
+                **     could be sniffed.  Nonces may help against replays though.
+                **
+                **     Alternate code for Kerberos Purists:
+                **
+                **     memcpy(s->session->master_key,  kssl_ctx->key, kssl_ctx->length);
+                **     s->session->master_key_length = kssl_ctx->length;
+                */
+                s->session->master_key_length=
+                        s->method->ssl3_enc->generate_master_secret(s,
+                                s->session->master_key, kssl_ctx->key, kssl_ctx->length);
+                /*     Was doing kssl_ctx_free() here, but it caused problems for apache.
+                **     kssl_ctx = kssl_ctx_free(kssl_ctx);
+                **     if (s->kssl_ctx)  s->kssl_ctx = NULL;
+                */
+                }
+       else
+#endif /* OPENSSL_NO_KRB5 */
                {
                al=SSL_AD_HANDSHAKE_FAILURE;
                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNKNOWN_CIPHER_TYPE);
@@ -1406,7 +1485,7 @@ static int ssl3_get_client_key_exchange(SSL *s)
        return(1);
 f_err:
        ssl3_send_alert(s,SSL3_AL_FATAL,al);
-#if !defined(NO_DH) || !defined(NO_RSA)
+#if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA)
 err:
 #endif
        return(-1);
@@ -1495,7 +1574,7 @@ static int ssl3_get_cert_verify(SSL *s)
                goto f_err;
                }
 
-#ifndef NO_RSA 
+#ifndef OPENSSL_NO_RSA 
        if (pkey->type == EVP_PKEY_RSA)
                {
                i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
@@ -1516,7 +1595,7 @@ static int ssl3_get_cert_verify(SSL *s)
                }
        else
 #endif
-#ifndef NO_DSA
+#ifndef OPENSSL_NO_DSA
                if (pkey->type == EVP_PKEY_DSA)
                {
                j=DSA_verify(pkey->save_type,
@@ -1533,7 +1612,7 @@ static int ssl3_get_cert_verify(SSL *s)
        else
 #endif
                {
-               SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_INTERNAL_ERROR);
+               SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,ERR_R_INTERNAL_ERROR);
                al=SSL_AD_UNSUPPORTED_CERTIFICATE;
                goto f_err;
                }
@@ -1562,7 +1641,7 @@ static int ssl3_get_client_certificate(SSL *s)
                SSL3_ST_SR_CERT_A,
                SSL3_ST_SR_CERT_B,
                -1,
-#if defined(MSDOS) && !defined(WIN32)
+#if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN32)
                1024*30, /* 30k max cert list :-) */
 #else
                1024*100, /* 100k max cert list :-) */
@@ -1692,6 +1771,8 @@ static int ssl3_get_client_certificate(SSL *s)
        if (s->session->sess_cert->cert_chain != NULL)
                sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
        s->session->sess_cert->cert_chain=sk;
+       /* Inconsistency alert: cert_chain does *not* include the
+        * peer's own certificate, while we do include it in s3_clnt.c */
 
        sk=NULL;
 
@@ -1715,9 +1796,13 @@ int ssl3_send_server_certificate(SSL *s)
        if (s->state == SSL3_ST_SW_CERT_A)
                {
                x=ssl_get_server_send_cert(s);
-               if (x == NULL)
+               if (x == NULL &&
+                        /* VRS: allow null cert if auth == KRB5 */
+                        (s->s3->tmp.new_cipher->algorithms
+                                & (SSL_MKEY_MASK|SSL_AUTH_MASK))
+                        != (SSL_aKRB5|SSL_kKRB5))
                        {
-                       SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,SSL_R_INTERNAL_ERROR);
+                       SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR);
                        return(0);
                        }