First tentative impementation of Kerberos 5 cryptos and keys for SSL/TLS. Implemente...
[openssl.git] / ssl / s3_srvr.c
index 85b0f5e9ef5fb54592abc5feea367a20bc27f12a..4704dfb59867a918d817e04d5cb51b8ca479912c 100644 (file)
@@ -57,6 +57,8 @@
  */
 
 #define REUSE_CIPHER_BUG
+#define NETSCAPE_HANG_BUG
+
 
 #include <stdio.h>
 #include <openssl/buffer.h>
 #include <openssl/x509.h>
 #include "ssl_locl.h"
 
+#ifndef NO_KRB5
+#include "kssl.h"
+#endif /* 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);
@@ -75,9 +81,9 @@ static int ssl3_send_server_hello(SSL *s);
 static int ssl3_send_server_key_exchange(SSL *s);
 static int ssl3_send_certificate_request(SSL *s);
 static int ssl3_send_server_done(SSL *s);
-static int ssl3_get_cert_verify(SSL *s);
 static int ssl3_get_client_key_exchange(SSL *s);
 static int ssl3_get_client_certificate(SSL *s);
+static int ssl3_get_cert_verify(SSL *s);
 static int ssl3_send_hello_request(SSL *s);
 
 static SSL_METHOD *ssl3_get_server_method(int ver)
@@ -113,7 +119,7 @@ int ssl3_accept(SSL *s)
        int ret= -1;
        int new_state,state,skip=0;
 
-       RAND_seed(&Time,sizeof(Time));
+       RAND_add(&Time,sizeof(Time),0);
        ERR_clear_error();
        clear_sys_error();
 
@@ -142,7 +148,6 @@ int ssl3_accept(SSL *s)
                        s->new_session=1;
                        /* s->state=SSL_ST_ACCEPT; */
 
-               case SSL3_ST_SR_MS_SGC:
                case SSL_ST_BEFORE:
                case SSL_ST_ACCEPT:
                case SSL_ST_BEFORE|SSL_ST_ACCEPT:
@@ -152,8 +157,10 @@ int ssl3_accept(SSL *s)
                        if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
 
                        if ((s->version>>8) != 3)
-                               abort();
-                       /* s->version=SSL3_VERSION; */
+                               {
+                               SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_INTERNAL_ERROR);
+                               return -1;
+                               }
                        s->type=SSL_ST_ACCEPT;
 
                        if (s->init_buf == NULL)
@@ -186,7 +193,7 @@ int ssl3_accept(SSL *s)
 
                        if (s->state != SSL_ST_RENEGOTIATE)
                                {
-                               if(s->state != SSL3_ST_SR_MS_SGC) ssl3_init_finished_mac(s);
+                               ssl3_init_finished_mac(s);
                                s->state=SSL3_ST_SR_CLNT_HELLO_A;
                                s->ctx->stats.sess_accept++;
                                }
@@ -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 NO_KRB5
+                               && !(l & SSL_KRB5)
+#endif /* NO_KRB5 */
+                               )
                                s->s3->tmp.use_rsa_tmp=1;
                        else
                                s->s3->tmp.use_rsa_tmp=0;
@@ -270,8 +281,8 @@ int ssl3_accept(SSL *s)
                            || (l & (SSL_DH|SSL_kFZA))
                            || ((l & SSL_kRSA)
                                && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
-                                   || (SSL_IS_EXPORT(l)
-                                       && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_EXPORT_PKEYLENGTH(l)
+                                   || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
+                                       && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
                                        )
                                    )
                                )
@@ -313,7 +324,12 @@ int ssl3_accept(SSL *s)
                                s->s3->tmp.cert_request=1;
                                ret=ssl3_send_certificate_request(s);
                                if (ret <= 0) goto end;
+#ifndef NETSCAPE_HANG_BUG
                                s->state=SSL3_ST_SW_SRVR_DONE_A;
+#else
+                               s->state=SSL3_ST_SW_FLUSH;
+                               s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
+#endif
                                s->init_num=0;
                                }
                        break;
@@ -343,10 +359,12 @@ int ssl3_accept(SSL *s)
 
                case SSL3_ST_SR_CERT_A:
                case SSL3_ST_SR_CERT_B:
-                       /* Check for second client hello if MS SGC */
+                       /* Check for second client hello (MS SGC) */
                        ret = ssl3_check_client_hello(s);
-                       if(ret <= 0) goto end;
-                       if(ret == 2) s->state = SSL3_ST_SR_MS_SGC;
+                       if (ret <= 0)
+                               goto end;
+                       if (ret == 2)
+                               s->state = SSL3_ST_SR_CLNT_HELLO_C;
                        else {
                                /* could be sent for a DH cert, even if we
                                 * have not asked for it :-) */
@@ -503,7 +521,7 @@ static int ssl3_send_hello_request(SSL *s)
        if (s->state == SSL3_ST_SW_HELLO_REQ_A)
                {
                p=(unsigned char *)s->init_buf->data;
-               *(p++)=SSL3_MT_CLIENT_REQUEST;
+               *(p++)=SSL3_MT_HELLO_REQUEST;
                *(p++)=0;
                *(p++)=0;
                *(p++)=0;
@@ -531,7 +549,21 @@ static int ssl3_check_client_hello(SSL *s)
                &ok);
        if (!ok) return((int)n);
        s->s3->tmp.reuse_message = 1;
-       if(s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO) return 2;
+       if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO)
+               {
+               /* Throw away what we have done so far in the current handshake,
+                * 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
+               if (s->s3->tmp.dh != NULL)
+                       {
+                       DH_free(s->s3->tmp.dh);
+                       s->s3->tmp.dh = NULL;
+                       }
+#endif
+               return 2;
+               }
        return 1;
 }
 
@@ -788,7 +820,7 @@ static int ssl3_get_client_hello(SSL *s)
         * compression          - basically ignored right now
         * ssl version is set   - sslv3
         * s->session           - The ssl session has been setup.
-        * s->hit               - sesson reuse flag
+        * s->hit               - session reuse flag
         * s->tmp.new_cipher    - the new cipher to use.
         */
 
@@ -816,7 +848,7 @@ static int ssl3_send_server_hello(SSL *s)
                p=s->s3->server_random;
                Time=time(NULL);                        /* Time */
                l2n(Time,p);
-               RAND_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
+               RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
                /* Do the message type and length last */
                d=p= &(buf[4]);
 
@@ -903,7 +935,7 @@ static int ssl3_send_server_key_exchange(SSL *s)
        unsigned int u;
 #endif
 #ifndef NO_DH
-       DH *dh,*dhp;
+       DH *dh=NULL,*dhp;
 #endif
        EVP_PKEY *pkey;
        unsigned char *p,*d;
@@ -934,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;
                                }
@@ -955,7 +993,7 @@ static int ssl3_send_server_key_exchange(SSL *s)
                        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)
                                {
@@ -963,6 +1001,14 @@ static int ssl3_send_server_key_exchange(SSL *s)
                                SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
                                goto f_err;
                                }
+
+                       if (s->s3->tmp.dh != NULL)
+                               {
+                               DH_free(dh);
+                               SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, SSL_R_INTERNAL_ERROR);
+                               goto err;
+                               }
+
                        if ((dh=DHparams_dup(dhp)) == NULL)
                                {
                                SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
@@ -1109,7 +1155,7 @@ static int ssl3_send_server_key_exchange(SSL *s)
                s->init_off=0;
                }
 
-       /* SSL3_ST_SW_KEY_EXCH_B */
+       s->state = SSL3_ST_SW_KEY_EXCH_B;
        return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
 f_err:
        ssl3_send_alert(s,SSL3_AL_FATAL,al);
@@ -1186,6 +1232,17 @@ static int ssl3_send_certificate_request(SSL *s)
 
                s->init_num=n+4;
                s->init_off=0;
+#ifdef NETSCAPE_HANG_BUG
+               p=(unsigned char *)s->init_buf->data + s->init_num;
+
+               /* do the header */
+               *(p++)=SSL3_MT_SERVER_DONE;
+               *(p++)=0;
+               *(p++)=0;
+               *(p++)=0;
+               s->init_num += 4;
+#endif
+
                }
 
        /* SSL3_ST_SW_CERT_REQ_B */
@@ -1208,12 +1265,15 @@ static int ssl3_get_client_key_exchange(SSL *s)
        BIGNUM *pub=NULL;
        DH *dh_srvr;
 #endif
+#ifndef NO_KRB5
+        KSSL_ERR kssl_err;
+#endif /* 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);
@@ -1273,31 +1333,6 @@ static int ssl3_get_client_key_exchange(SSL *s)
 
                i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING);
 
-#if 0
-               /* If a bad decrypt, use a random master key */
-               if ((i != SSL_MAX_MASTER_KEY_LENGTH) ||
-                       ((p[0] != (s->client_version>>8)) ||
-                        (p[1] != (s->client_version & 0xff))))
-                       {
-                       int bad=1;
-
-                       if ((i == SSL_MAX_MASTER_KEY_LENGTH) &&
-                               (p[0] == (s->version>>8)) &&
-                               (p[1] == 0))
-                               {
-                               if (s->options & SSL_OP_TLS_ROLLBACK_BUG)
-                                       bad=0;
-                               }
-                       if (bad)
-                               {
-                               p[0]=(s->version>>8);
-                               p[1]=(s->version & 0xff);
-                               RAND_bytes(&(p[2]),SSL_MAX_MASTER_KEY_LENGTH-2);
-                               i=SSL_MAX_MASTER_KEY_LENGTH;
-                               }
-                       /* else, an SSLeay bug, ssl only server, tls client */
-                       }
-#else
                if (i != SSL_MAX_MASTER_KEY_LENGTH)
                        {
                        al=SSL_AD_DECODE_ERROR;
@@ -1305,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,
@@ -1383,6 +1428,53 @@ static int ssl3_get_client_key_exchange(SSL *s)
                }
        else
 #endif
+#ifndef 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 /* NO_KRB5 */
                {
                al=SSL_AD_HANDSHAKE_FAILURE;
                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNKNOWN_CIPHER_TYPE);
@@ -1678,6 +1770,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;
 
@@ -1701,7 +1795,11 @@ 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);
                        return(0);