Changes crypto/evp/ and ssl/ code from directly incrementing reference
[openssl.git] / ssl / s3_srvr.c
index 2a9e115e9c5306cb43d71140ed3f2ca80005dcf5..af65e2e68aee9416e39e65f34c904a194e3500fe 100644 (file)
 #include <openssl/buffer.h>
 #include <openssl/rand.h>
 #include <openssl/objects.h>
-#include <openssl/md5.h>
-#include <openssl/sha.h>
 #include <openssl/evp.h>
 #include <openssl/x509.h>
+#include <openssl/krb5_asn.h>
 #include "ssl_locl.h"
+#ifndef OPENSSL_NO_KRB5
+#include "kssl_lcl.h"
+#endif /* OPENSSL_NO_KRB5 */
+#include <openssl/md5.h>
 
 static SSL_METHOD *ssl3_get_server_method(int ver);
 static int ssl3_get_client_hello(SSL *s);
@@ -153,7 +156,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 +265,15 @@ 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 */
+                               )
+                               /* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key
+                                * even when forbidden by protocol specs
+                                * (handshake may fail as clients are not required to
+                                * be able to handle this) */
                                s->s3->tmp.use_rsa_tmp=1;
                        else
                                s->s3->tmp.use_rsa_tmp=0;
@@ -301,7 +315,9 @@ int ssl3_accept(SSL *s)
                                ((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) &&
                                 /* ... except when the application insists on verification
                                  * (against the specs, but s3_clnt.c accepts this for SSL 3) */
-                                !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)))
+                                !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
+                                 /* never request cert in Kerberos ciphersuites */
+                                (s->s3->tmp.new_cipher->algorithms & SSL_aKRB5))
                                {
                                /* no cert request */
                                skip=1;
@@ -534,7 +550,11 @@ static int ssl3_check_client_hello(SSL *s)
                SSL3_ST_SR_CERT_A,
                SSL3_ST_SR_CERT_B,
                -1,
-               SSL3_RT_MAX_PLAIN_LENGTH,
+#if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN32)
+               1024*30, /* 30k max cert list :-) */
+#else
+               1024*100, /* 100k max cert list :-) */
+#endif
                &ok);
        if (!ok) return((int)n);
        s->s3->tmp.reuse_message = 1;
@@ -544,7 +564,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 +764,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 +781,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 +936,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;
@@ -937,6 +957,7 @@ static int ssl3_send_server_key_exchange(SSL *s)
        BUF_MEM *buf;
        EVP_MD_CTX md_ctx;
 
+       EVP_MD_CTX_init(&md_ctx);
        if (s->state == SSL3_ST_SW_KEY_EXCH_A)
                {
                type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK;
@@ -946,7 +967,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,7 +976,13 @@ 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));
-                               CRYPTO_add(&rsa->references,1,CRYPTO_LOCK_RSA);
+                               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;
+                               }
+                               RSA_up(rsa);
                                cert->rsa_tmp=rsa;
                                }
                        if (rsa == NULL)
@@ -970,13 +997,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 +1015,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 +1101,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 +1129,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 */
@@ -1139,10 +1166,12 @@ static int ssl3_send_server_key_exchange(SSL *s)
                }
 
        s->state = SSL3_ST_SW_KEY_EXCH_B;
+       EVP_MD_CTX_cleanup(&md_ctx);
        return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
 f_err:
        ssl3_send_alert(s,SSL3_AL_FATAL,al);
 err:
+       EVP_MD_CTX_cleanup(&md_ctx);
        return(-1);
        }
 
@@ -1240,20 +1269,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 +1293,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 */
@@ -1313,21 +1345,53 @@ static int ssl3_get_client_key_exchange(SSL *s)
 
                i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING);
 
+               al = -1;
+               
                if (i != SSL_MAX_MASTER_KEY_LENGTH)
                        {
                        al=SSL_AD_DECODE_ERROR;
                        SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
-                       goto f_err;
                        }
 
-               if ((p[0] != (s->client_version>>8)) || (p[1] != (s->client_version & 0xff)))
+               if ((al == -1) && !((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
 
+               if (al != -1)
+                       {
+#if 0
+                       goto f_err;
+#else
+                       /* Some decryption failure -- use random value instead as countermeasure
+                        * against Bleichenbacher's attack on PKCS #1 v1.5 RSA padding
+                        * (see RFC 2246, section 7.4.7.1).
+                        * But note that due to length and protocol version checking, the
+                        * attack is impractical anyway (see section 5 in D. Bleichenbacher:
+                        * "Chosen Ciphertext Attacks Against Protocols Based on the RSA
+                        * Encryption Standard PKCS #1", CRYPTO '98, LNCS 1462, pp. 1-12).
+                        */
+                       ERR_clear_error();
+                       i = SSL_MAX_MASTER_KEY_LENGTH;
+                       p[0] = s->client_version >> 8;
+                       p[1] = s->client_version & 0xff;
+                       RAND_pseudo_bytes(p+2, i-2); /* should be RAND_bytes, but we cannot work around a failure */
+#endif
+                       }
+       
                s->session->master_key_length=
                        s->method->ssl3_enc->generate_master_secret(s,
                                s->session->master_key,
@@ -1335,7 +1399,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,19 +1459,167 @@ 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;
+               krb5_data               enc_ticket;
+               krb5_data               authenticator;
+               krb5_data               enc_pms;
+                KSSL_CTX               *kssl_ctx = s->kssl_ctx;
+               EVP_CIPHER_CTX          ciph_ctx;
+               EVP_CIPHER              *enc = NULL;
+               unsigned char           iv[EVP_MAX_IV_LENGTH];
+               unsigned char           pms[SSL_MAX_MASTER_KEY_LENGTH
+                                               + EVP_MAX_IV_LENGTH + 1];
+               int                     padl, outl = sizeof(pms);
+               krb5_timestamp          authtime = 0;
+               krb5_ticket_times       ttimes;
+
+                if (!kssl_ctx)  kssl_ctx = kssl_ctx_new();
+
+               n2s(p,i);
+               enc_ticket.length = i;
+               enc_ticket.data = p;
+               p+=enc_ticket.length;
+
+               n2s(p,i);
+               authenticator.length = i;
+               authenticator.data = p;
+               p+=authenticator.length;
+
+               n2s(p,i);
+               enc_pms.length = i;
+               enc_pms.data = p;
+               p+=enc_pms.length;
+
+               if ((unsigned long)n != enc_ticket.length + authenticator.length +
+                                               enc_pms.length + 6)
+                       {
+                       SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
+                               SSL_R_DATA_LENGTH_TOO_LONG);
+                       goto err;
+                       }
+
+                if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,
+                                       &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;
+                        }
+
+               /*  Note: no authenticator is not considered an error,
+               **  but will return authtime == 0.
+               */
+               if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator,
+                                       &authtime, &kssl_err)) != 0)
+                       {
+#ifdef KSSL_DEBUG
+                        printf("kssl_check_authent 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;
+                       }
+
+               if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0)
+                       {
+                       SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, krb5rc);
+                        goto err;
+                       }
+
+#ifdef KSSL_DEBUG
+                kssl_ctx_show(kssl_ctx);
+#endif /* KSSL_DEBUG */
+
+               enc = kssl_map_enc(kssl_ctx->enctype);
+                if (enc == NULL)
+                    goto err;
+
+               memset(iv, 0, EVP_MAX_IV_LENGTH);       /* per RFC 1510 */
+
+               if (!EVP_DecryptInit(&ciph_ctx,enc,kssl_ctx->key,iv))
+                       {
+                       SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
+                               SSL_R_DECRYPTION_FAILED);
+                       goto err;
+                       }
+               if (!EVP_DecryptUpdate(&ciph_ctx, pms,&outl,
+                                       enc_pms.data, enc_pms.length))
+                       {
+                       SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
+                               SSL_R_DECRYPTION_FAILED);
+                       goto err;
+                       }
+               if (outl > SSL_MAX_MASTER_KEY_LENGTH)
+                       {
+                       SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
+                               SSL_R_DATA_LENGTH_TOO_LONG);
+                       goto err;
+                       }
+               if (!EVP_DecryptFinal(&ciph_ctx,&(pms[outl]),&padl))
+                       {
+                       SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
+                               SSL_R_DECRYPTION_FAILED);
+                       goto err;
+                       }
+               outl += padl;
+               if (outl > SSL_MAX_MASTER_KEY_LENGTH)
+                       {
+                       SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
+                               SSL_R_DATA_LENGTH_TOO_LONG);
+                       goto err;
+                       }
+               EVP_CIPHER_CTX_cleanup(&ciph_ctx);
+
+                s->session->master_key_length=
+                        s->method->ssl3_enc->generate_master_secret(s,
+                                s->session->master_key, pms, outl);
+
+                if (kssl_ctx->client_princ)
+                        {
+                        int len = strlen(kssl_ctx->client_princ);
+                        if ( len < SSL_MAX_KRB5_PRINCIPAL_LENGTH ) 
+                                {
+                                s->session->krb5_client_princ_len = len;
+                                memcpy(s->session->krb5_client_princ,kssl_ctx->client_princ,len);
+                                }
+                        }
+
+
+                /*  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);
+               SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
+                               SSL_R_UNKNOWN_CIPHER_TYPE);
                goto f_err;
                }
 
        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 +1708,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 +1729,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 +1746,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 +1775,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 +1905,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 +1930,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);
                        }