Support TLS extensions (specifically, HostName)
[openssl.git] / ssl / s3_srvr.c
index a34fc729be021738a9a6d557c920df3b86a7f970..25b56fa6fac1007245f897c83cfa670a71e7e799 100644 (file)
 #include <openssl/objects.h>
 #include <openssl/evp.h>
 #include <openssl/x509.h>
+#ifndef OPENSSL_NO_DH
 #include <openssl/dh.h>
+#endif
 #include <openssl/bn.h>
 #ifndef OPENSSL_NO_KRB5
 #include <openssl/krb5_asn.h>
 #endif
 #include <openssl/md5.h>
 
-static SSL_METHOD *ssl3_get_server_method(int ver);
+static const SSL_METHOD *ssl3_get_server_method(int ver);
 
 #ifndef OPENSSL_NO_ECDH
 static int nid2curve_id(int nid);
 #endif
 
-static SSL_METHOD *ssl3_get_server_method(int ver)
+static const SSL_METHOD *ssl3_get_server_method(int ver)
        {
        if (ver == SSL3_VERSION)
                return(SSLv3_server_method());
@@ -154,33 +156,15 @@ static SSL_METHOD *ssl3_get_server_method(int ver)
                return(NULL);
        }
 
-SSL_METHOD *SSLv3_server_method(void)
-       {
-       static int init=1;
-       static SSL_METHOD SSLv3_server_data;
-
-       if (init)
-               {
-               CRYPTO_w_lock(CRYPTO_LOCK_SSL_METHOD);
-
-               if (init)
-                       {
-                       memcpy((char *)&SSLv3_server_data,(char *)sslv3_base_method(),
-                               sizeof(SSL_METHOD));
-                       SSLv3_server_data.ssl_accept=ssl3_accept;
-                       SSLv3_server_data.get_ssl_method=ssl3_get_server_method;
-                       init=0;
-                       }
-                       
-               CRYPTO_w_unlock(CRYPTO_LOCK_SSL_METHOD);
-               }
-       return(&SSLv3_server_data);
-       }
+IMPLEMENT_ssl3_meth_func(SSLv3_server_method,
+                       ssl3_accept,
+                       ssl_undefined_function,
+                       ssl3_get_server_method)
 
 int ssl3_accept(SSL *s)
        {
        BUF_MEM *buf;
-       unsigned long l,Time=time(NULL);
+       unsigned long l,Time=(unsigned long)time(NULL);
        void (*cb)(const SSL *ssl,int type,int val)=NULL;
        long num1;
        int ret= -1;
@@ -297,6 +281,17 @@ int ssl3_accept(SSL *s)
                        s->shutdown=0;
                        ret=ssl3_get_client_hello(s);
                        if (ret <= 0) goto end;
+#ifndef OPENSSL_NO_TLSEXT
+                       {
+                               int extension_error = 0,al;
+                               if ((al = ssl_check_Hello_TLS_extensions(s,&extension_error)) != SSL_ERROR_NONE){
+                                       ret = -1;
+                                       SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_CLIENTHELLO_TLS_EXT);
+                                       ssl3_send_alert(s,al,extension_error);
+                                       goto end;
+                               }
+                       }
+#endif
                        s->new_session = 2;
                        s->state=SSL3_ST_SW_SRVR_HELLO_A;
                        s->init_num=0;
@@ -682,7 +677,9 @@ int ssl3_get_client_hello(SSL *s)
        unsigned long id;
        unsigned char *p,*d,*q;
        SSL_CIPHER *c;
+#ifndef OPENSSL_NO_COMP
        SSL_COMP *comp=NULL;
+#endif
        STACK_OF(SSL_CIPHER) *ciphers=NULL;
 
        /* We do this so that we will respond with our native type.
@@ -913,7 +910,8 @@ int ssl3_get_client_hello(SSL *s)
         * options, we will now look for them.  We have i-1 compression
         * algorithms from the client, starting at q. */
        s->s3->tmp.new_compression=NULL;
-       if (s->ctx->comp_methods != NULL)
+#ifndef OPENSSL_NO_COMP
+       if (!(s->options & SSL_OP_NO_COMPRESSION) && s->ctx->comp_methods)
                { /* See if we have a match */
                int m,nn,o,v,done=0;
 
@@ -937,6 +935,7 @@ int ssl3_get_client_hello(SSL *s)
                else
                        comp=NULL;
                }
+#endif
 
        /* TLS does not mind if there is extra stuff */
 #if 0   /* SSL 3.0 does not mind either, so we should disable this test
@@ -954,13 +953,28 @@ int ssl3_get_client_hello(SSL *s)
                        }
                }
 #endif
+#ifndef OPENSSL_NO_TLSEXT
+       /* TLS extensions*/
+       if (s->version > SSL3_VERSION)
+       {
+               if ((al = ssl_parse_ClientHello_TLS_extensions(s,&p,d,n)) != SSL_ERROR_NONE){
+                       SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_PARSE_TLS_EXT);
+                       ssl3_send_alert(s,SSL3_AL_WARNING,al);
+                       return (ret = al);
+               }
+       }
+#endif
 
        /* Given s->session->ciphers and SSL_get_ciphers, we must
         * pick a cipher */
 
        if (!s->hit)
                {
+#ifdef OPENSSL_NO_COMP
+               s->session->compress_meth=0;
+#else
                s->session->compress_meth=(comp == NULL)?0:comp->id;
+#endif
                if (s->session->ciphers != NULL)
                        sk_SSL_CIPHER_free(s->session->ciphers);
                s->session->ciphers=ciphers;
@@ -1046,7 +1060,7 @@ int ssl3_send_server_hello(SSL *s)
                {
                buf=(unsigned char *)s->init_buf->data;
                p=s->s3->server_random;
-               Time=time(NULL);                        /* Time */
+               Time=(unsigned long)time(NULL);                 /* Time */
                l2n(Time,p);
                if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
                        return -1;
@@ -1086,10 +1100,21 @@ int ssl3_send_server_hello(SSL *s)
                p+=i;
 
                /* put the compression method */
+#ifdef OPENSSL_NO_COMP
+                       *(p++)=0;
+#else
                if (s->s3->tmp.new_compression == NULL)
                        *(p++)=0;
                else
                        *(p++)=s->s3->tmp.new_compression->id;
+#endif
+#ifndef OPENSSL_NO_TLSEXT
+               if ((p = ssl_add_ServerHello_TLS_extensions(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL)
+               {
+                       SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO,ERR_R_INTERNAL_ERROR);
+                       return -1;
+               }
+#endif
 
                /* do the header */
                l=(p-d);
@@ -1261,6 +1286,8 @@ int ssl3_send_server_key_exchange(SSL *s)
 #ifndef OPENSSL_NO_ECDH
                        if (type & SSL_kECDHE)
                        {
+                       const EC_GROUP *group;
+
                        ecdhp=cert->ecdh_tmp;
                        if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL))
                                {
@@ -1296,8 +1323,8 @@ int ssl3_send_server_key_exchange(SSL *s)
                        ecdh = ecdhp;
 
                        s->s3->tmp.ecdh=ecdh;
-                       if ((ecdh->pub_key == NULL) ||
-                           (ecdh->priv_key == NULL) ||
+                       if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
+                           (EC_KEY_get0_private_key(ecdh) == NULL) ||
                            (s->options & SSL_OP_SINGLE_ECDH_USE))
                                {
                                if(!EC_KEY_generate_key(ecdh))
@@ -1307,16 +1334,16 @@ int ssl3_send_server_key_exchange(SSL *s)
                                    }
                                }
 
-                       if ((ecdh->group == NULL) ||
-                           (ecdh->pub_key == NULL) ||
-                           (ecdh->priv_key == NULL))
+                       if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
+                           (EC_KEY_get0_public_key(ecdh)  == NULL) ||
+                           (EC_KEY_get0_private_key(ecdh) == NULL))
                                {
                                SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
                                goto err;
                                }
 
                        if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
-                           (EC_GROUP_get_degree(ecdh->group) > 163)) 
+                           (EC_GROUP_get_degree(group) > 163)) 
                                {
                                SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
                                goto err;
@@ -1327,7 +1354,7 @@ int ssl3_send_server_key_exchange(SSL *s)
                         * supported named curves, curve_id is non-zero.
                         */
                        if ((curve_id = 
-                           nid2curve_id(EC_GROUP_get_curve_name(ecdh->group)))
+                           nid2curve_id(EC_GROUP_get_curve_name(group)))
                            == 0)
                                {
                                SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
@@ -1338,8 +1365,8 @@ int ssl3_send_server_key_exchange(SSL *s)
                         * First check the size of encoding and
                         * allocate memory accordingly.
                         */
-                       encodedlen = EC_POINT_point2oct(ecdh->group, 
-                           ecdh->pub_key, 
+                       encodedlen = EC_POINT_point2oct(group, 
+                           EC_KEY_get0_public_key(ecdh),
                            POINT_CONVERSION_UNCOMPRESSED, 
                            NULL, 0, NULL);
 
@@ -1353,8 +1380,8 @@ int ssl3_send_server_key_exchange(SSL *s)
                                }
 
 
-                       encodedlen = EC_POINT_point2oct(ecdh->group, 
-                           ecdh->pub_key
+                       encodedlen = EC_POINT_point2oct(group, 
+                           EC_KEY_get0_public_key(ecdh)
                            POINT_CONVERSION_UNCOMPRESSED, 
                            encodedPoint, encodedlen, bn_ctx);
 
@@ -1368,11 +1395,11 @@ int ssl3_send_server_key_exchange(SSL *s)
 
                        /* XXX: For now, we only support named (not 
                         * generic) curves in ECDH ephemeral key exchanges.
-                        * In this situation, we need three additional bytes
+                        * In this situation, we need four additional bytes
                         * to encode the entire ServerECDHParams
                         * structure. 
                         */
-                       n = 3 + encodedlen;
+                       n = 4 + encodedlen;
 
                        /* We'll generate the serverKeyExchange message
                         * explicitly so we can set these to NULLs
@@ -1380,6 +1407,7 @@ int ssl3_send_server_key_exchange(SSL *s)
                        r[0]=NULL;
                        r[1]=NULL;
                        r[2]=NULL;
+                       r[3]=NULL;
                        }
                else 
 #endif /* !OPENSSL_NO_ECDH */
@@ -1430,12 +1458,14 @@ int ssl3_send_server_key_exchange(SSL *s)
                        {
                        /* XXX: For now, we only support named (not generic) curves.
                         * In this situation, the serverKeyExchange message has:
-                        * [1 byte CurveType], [1 byte CurveName]
+                        * [1 byte CurveType], [2 byte CurveName]
                         * [1 byte length of encoded point], followed by
                         * the actual encoded point itself
                         */
                        *p = NAMED_CURVE_TYPE;
                        p += 1;
+                       *p = 0;
+                       p += 1;
                        *p = curve_id;
                        p += 1;
                        *p = encodedlen;
@@ -1639,21 +1669,6 @@ err:
        return(-1);
        }
 
-
-static const int KDF1_SHA1_len = 20;
-static void *KDF1_SHA1(const void *in, size_t inlen, void *out, size_t *outlen)
-       {
-#ifndef OPENSSL_NO_SHA
-       if (*outlen < SHA_DIGEST_LENGTH)
-               return NULL;
-       else
-               *outlen = SHA_DIGEST_LENGTH;
-       return SHA1(in, inlen, out);
-#else
-       return NULL;
-#endif
-       }
-
 int ssl3_get_client_key_exchange(SSL *s)
        {
        int i,al,ok;
@@ -2040,6 +2055,9 @@ int ssl3_get_client_key_exchange(SSL *s)
                {
                int ret = 1;
                int field_size = 0;
+               const EC_KEY   *tkey;
+               const EC_GROUP *group;
+               const BIGNUM *priv_key;
 
                 /* initialize structures for server's ECDH key pair */
                if ((srvr_ecdh = EC_KEY_new()) == NULL) 
@@ -2053,23 +2071,29 @@ int ssl3_get_client_key_exchange(SSL *s)
                if (l & SSL_kECDH) 
                        { 
                         /* use the certificate */
-                       srvr_ecdh->group = s->cert->key->privatekey-> \
-                           pkey.eckey->group;
-                       srvr_ecdh->priv_key = s->cert->key->privatekey-> \
-                           pkey.eckey->priv_key;
+                       tkey = s->cert->key->privatekey->pkey.ec;
                        }
                else
                        {
                        /* use the ephermeral values we saved when
                         * generating the ServerKeyExchange msg.
                         */
-                       srvr_ecdh->group = s->s3->tmp.ecdh->group;
-                       srvr_ecdh->priv_key = s->s3->tmp.ecdh->priv_key;
+                       tkey = s->s3->tmp.ecdh;
+                       }
+
+               group    = EC_KEY_get0_group(tkey);
+               priv_key = EC_KEY_get0_private_key(tkey);
+
+               if (!EC_KEY_set_group(srvr_ecdh, group) ||
+                   !EC_KEY_set_private_key(srvr_ecdh, priv_key))
+                       {
+                       SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
+                              ERR_R_EC_LIB);
+                       goto err;
                        }
 
                /* Let's get client's public key */
-               if ((clnt_ecpoint = EC_POINT_new(srvr_ecdh->group))
-                   == NULL) 
+               if ((clnt_ecpoint = EC_POINT_new(group)) == NULL)
                        {
                        SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
                            ERR_R_MALLOC_FAILURE);
@@ -2108,7 +2132,7 @@ int ssl3_get_client_key_exchange(SSL *s)
                                }
 
                        EC_POINT_copy(clnt_ecpoint,
-                           clnt_pub_pkey->pkey.eckey->pub_key);
+                           EC_KEY_get0_public_key(clnt_pub_pkey->pkey.ec));
                         ret = 2; /* Skip certificate verify processing */
                         }
                 else
@@ -2126,7 +2150,7 @@ int ssl3_get_client_key_exchange(SSL *s)
                         /* Get encoded point length */
                         i = *p; 
                        p += 1;
-                        if (EC_POINT_oct2point(srvr_ecdh->group, 
+                        if (EC_POINT_oct2point(group, 
                            clnt_ecpoint, p, i, bn_ctx) == 0)
                                {
                                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
@@ -2140,21 +2164,14 @@ int ssl3_get_client_key_exchange(SSL *s)
                         }
 
                /* Compute the shared pre-master secret */
-               field_size = EC_GROUP_get_degree(srvr_ecdh->group);
+               field_size = EC_GROUP_get_degree(group);
                if (field_size <= 0)
                        {
                        SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 
                               ERR_R_ECDH_LIB);
                        goto err;
                        }
-               /* If field size is not more than 24 octets, then use SHA-1 hash of result;
-                * otherwise, use result (see section 4.8 of draft-ietf-tls-ecc-03.txt;
-                * this is new with this version of the Internet Draft).
-                */
-               if (field_size <= 24 * 8)
-                   i = ECDH_compute_key(p, KDF1_SHA1_len, clnt_ecpoint, srvr_ecdh, KDF1_SHA1);
-               else
-                   i = ECDH_compute_key(p, (field_size+7)/8, clnt_ecpoint, srvr_ecdh, NULL);
+               i = ECDH_compute_key(p, (field_size+7)/8, clnt_ecpoint, srvr_ecdh, NULL);
                 if (i <= 0)
                         {
                         SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
@@ -2165,11 +2182,7 @@ int ssl3_get_client_key_exchange(SSL *s)
                EVP_PKEY_free(clnt_pub_pkey);
                EC_POINT_free(clnt_ecpoint);
                if (srvr_ecdh != NULL) 
-                       {
-                       srvr_ecdh->priv_key = NULL;
-                       srvr_ecdh->group = NULL;
                        EC_KEY_free(srvr_ecdh);
-                       }
                BN_CTX_free(bn_ctx);
 
                /* Compute the master secret */
@@ -2198,11 +2211,7 @@ err:
        EVP_PKEY_free(clnt_pub_pkey);
        EC_POINT_free(clnt_ecpoint);
        if (srvr_ecdh != NULL) 
-               {
-               srvr_ecdh->priv_key = NULL;
-               srvr_ecdh->group = NULL;
                EC_KEY_free(srvr_ecdh);
-               }
        BN_CTX_free(bn_ctx);
 #endif
        return(-1);
@@ -2333,7 +2342,7 @@ int ssl3_get_cert_verify(SSL *s)
                {
                j=ECDSA_verify(pkey->save_type,
                        &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
-                       SHA_DIGEST_LENGTH,p,i,pkey->pkey.eckey);
+                       SHA_DIGEST_LENGTH,p,i,pkey->pkey.ec);
                if (j <= 0)
                        {
                        /* bad signature */