improvements for alert handling
[openssl.git] / ssl / s3_clnt.c
index cc9df9186dd8daf382f97c7fdf0d2c647402a39e..d50f588b94bef21d13754f56df8dd1f32f909188 100644 (file)
@@ -56,7 +56,7 @@
  * [including the GNU Public Licence.]
  */
 /* ====================================================================
- * Copyright (c) 1998-2003 The OpenSSL Project.  All rights reserved.
+ * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -159,7 +159,7 @@ IMPLEMENT_ssl3_meth_func(SSLv3_client_method,
 int ssl3_connect(SSL *s)
        {
        BUF_MEM *buf=NULL;
-       unsigned long Time=time(NULL),l;
+       unsigned long Time=(unsigned long)time(NULL),l;
        long num1;
        void (*cb)(const SSL *ssl,int type,int val)=NULL;
        int ret= -1;
@@ -255,6 +255,25 @@ int ssl3_connect(SSL *s)
                case SSL3_ST_CR_SRVR_HELLO_B:
                        ret=ssl3_get_server_hello(s);
                        if (ret <= 0) goto end;
+#ifndef OPENSSL_NO_TLSEXT
+                       {
+                               int al;
+                               switch (ssl_check_tlsext(s,&al))
+                                       {
+                               case SSL_TLSEXT_ERR_ALERT_FATAL:
+                                       ssl3_send_alert(s,SSL3_AL_FATAL,al);
+                                       SSLerr(SSL_F_SSL3_CONNECT,SSL_R_SERVERHELLO_TLS_EXT);
+                                       ret = -1;
+                                       goto end;
+
+                               case SSL_TLSEXT_ERR_ALERT_WARNING:
+                                       ssl3_send_alert(s,SSL3_AL_WARNING,al); 
+                                       
+                               default:
+                                       ;
+                                       }
+                       }
+#endif
                        if (s->hit)
                                s->state=SSL3_ST_CR_FINISHED_A;
                        else
@@ -541,7 +560,7 @@ int ssl3_client_hello(SSL *s)
                /* else use the pre-loaded session */
 
                p=s->s3->client_random;
-               Time=time(NULL);                        /* Time */
+               Time=(unsigned long)time(NULL);                 /* Time */
                l2n(Time,p);
                if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
                        goto err;
@@ -588,7 +607,9 @@ int ssl3_client_hello(SSL *s)
 #ifdef OPENSSL_NO_COMP
                *(p++)=1;
 #else
-               if (s->ctx->comp_methods == NULL)
+
+               if ((s->options & SSL_OP_NO_COMPRESSION)
+                                       || !s->ctx->comp_methods)
                        j=0;
                else
                        j=sk_SSL_COMP_num(s->ctx->comp_methods);
@@ -600,6 +621,13 @@ int ssl3_client_hello(SSL *s)
                        }
 #endif
                *(p++)=0; /* Add the NULL method */
+#ifndef OPENSSL_NO_TLSEXT
+               if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL)
+                       {
+                       SSLerr(SSL_F_SSL3_CLIENT_HELLO,ERR_R_INTERNAL_ERROR);
+                       goto err;
+                       }
+#endif
                
                l=(p-d);
                d=buf;
@@ -768,7 +796,7 @@ int ssl3_get_server_hello(SSL *s)
                }
 #else
        j= *(p++);
-       if (j == 0)
+       if ((j == 0) || (s->options & SSL_OP_NO_COMPRESSION))
                comp=NULL;
        else
                comp=ssl3_comp_find(s->ctx->comp_methods,j);
@@ -784,6 +812,18 @@ int ssl3_get_server_hello(SSL *s)
                s->s3->tmp.new_compression=comp;
                }
 #endif
+#ifndef OPENSSL_NO_TLSEXT
+       /* TLS extensions*/
+       if (s->version > SSL3_VERSION)
+               {
+               if (!ssl_parse_serverhello_tlsext(s,&p,d,n, &al))
+                       {
+                       /* 'al' set by ssl_parse_serverhello_tlsext */
+                       SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_PARSE_TLS_EXT);
+                       goto f_err; 
+                       }
+               }
+#endif
 
        if (p != (d+n))
                {
@@ -1211,12 +1251,12 @@ int ssl3_get_key_exchange(SSL *s)
                 */
 
                /* XXX: For now we only support named (not generic) curves
-                * and the ECParameters in this case is just two bytes.
+                * and the ECParameters in this case is just three bytes.
                 */
-               param_len=2;
+               param_len=3;
                if ((param_len > n) ||
                    (*p != NAMED_CURVE_TYPE) || 
-                   ((curve_nid = curve_id2nid(*(p + 1))) == 0)) 
+                   ((curve_nid = curve_id2nid(*(p + 2))) == 0)) 
                        {
                        al=SSL_AD_INTERNAL_ERROR;
                        SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
@@ -1246,7 +1286,7 @@ int ssl3_get_key_exchange(SSL *s)
                        goto f_err;
                        }
 
-               p+=2;
+               p+=3;
 
                /* Next, get the encoded ECPoint */
                if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
@@ -1614,22 +1654,6 @@ int ssl3_get_server_done(SSL *s)
        }
 
 
-#ifndef OPENSSL_NO_ECDH
-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 /* OPENSSL_NO_SHA */
-       }
-#endif /* OPENSSL_NO_ECDH */
-
 int ssl3_send_client_key_exchange(SSL *s)
        {
        unsigned char *p,*d;
@@ -2027,14 +2051,7 @@ int ssl3_send_client_key_exchange(SSL *s)
                                       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)
-                               n=ECDH_compute_key(p, KDF1_SHA1_len, srvr_ecpoint, clnt_ecdh, KDF1_SHA1);
-                       else
-                               n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL);
+                       n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL);
                        if (n <= 0)
                                {
                                SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,