Update from stable branch.
[openssl.git] / ssl / ssl_lib.c
index 1c28daa2b8c5beaab69de428c407abaa43c7b5af..ccfe399bbe879e4a81580de85e9c1b9a63c99424 100644 (file)
 #include <openssl/objects.h>
 #include <openssl/lhash.h>
 #include <openssl/x509v3.h>
+#include <openssl/rand.h>
+#include <openssl/ocsp.h>
 #ifndef OPENSSL_NO_DH
 #include <openssl/dh.h>
 #endif
@@ -164,9 +166,9 @@ SSL3_ENC_METHOD ssl3_undef_enc_method={
        ssl_undefined_function,
        (int (*)(SSL *, unsigned char *, unsigned char *, int))ssl_undefined_function,
        (int (*)(SSL*, int))ssl_undefined_function,
-       (int (*)(SSL *, EVP_MD_CTX *, EVP_MD_CTX *, const char*, int, unsigned char *))ssl_undefined_function,
+       (int (*)(SSL *,  const char*, int, unsigned char *))ssl_undefined_function,
        0,      /* finish_mac_length */
-       (int (*)(SSL *, EVP_MD_CTX *, unsigned char *))ssl_undefined_function,
+       (int (*)(SSL *, int, unsigned char *))ssl_undefined_function,
        NULL,   /* client_finished_label */
        0,      /* client_finished_label_len */
        NULL,   /* server_finished_label */
@@ -225,6 +227,8 @@ int SSL_clear(SSL *s)
                }
 
        ssl_clear_cipher_ctx(s);
+       ssl_clear_hash_ctx(&s->read_hash);
+       ssl_clear_hash_ctx(&s->write_hash);
 
        s->first_packet=0;
 
@@ -334,6 +338,15 @@ SSL *SSL_new(SSL_CTX *ctx)
        CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
        s->ctx=ctx;
 #ifndef OPENSSL_NO_TLSEXT
+       s->tlsext_debug_cb = 0;
+       s->tlsext_debug_arg = NULL;
+       s->tlsext_ticket_expected = 0;
+       s->tlsext_status_type = -1;
+       s->tlsext_status_expected = 0;
+       s->tlsext_ocsp_ids = NULL;
+       s->tlsext_ocsp_exts = NULL;
+       s->tlsext_ocsp_resp = NULL;
+       s->tlsext_ocsp_resplen = -1;
        CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
        s->initial_ctx=ctx;
 #endif
@@ -523,6 +536,8 @@ void SSL_free(SSL *s)
                }
 
        ssl_clear_cipher_ctx(s);
+       ssl_clear_hash_ctx(&s->read_hash);
+       ssl_clear_hash_ctx(&s->write_hash);
 
        if (s->cert != NULL) ssl_cert_free(s->cert);
        /* Free up if allocated */
@@ -534,6 +549,14 @@ void SSL_free(SSL *s)
        if (s->tlsext_ecpointformatlist) OPENSSL_free(s->tlsext_ecpointformatlist);
        if (s->tlsext_ellipticcurvelist) OPENSSL_free(s->tlsext_ellipticcurvelist);
 #endif /* OPENSSL_NO_EC */
+       if (s->tlsext_opaque_prf_input) OPENSSL_free(s->tlsext_opaque_prf_input);
+       if (s->tlsext_ocsp_exts)
+               sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
+                                               X509_EXTENSION_free);
+       if (s->tlsext_ocsp_ids)
+               sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free);
+       if (s->tlsext_ocsp_resp)
+               OPENSSL_free(s->tlsext_ocsp_resp);
 #endif
 
        if (s->client_CA != NULL)
@@ -1254,7 +1277,6 @@ int SSL_set_cipher_list(SSL *s,const char *str)
 char *SSL_get_shared_ciphers(const SSL *s,char *buf,int len)
        {
        char *p;
-       const char *cp;
        STACK_OF(SSL_CIPHER) *sk;
        SSL_CIPHER *c;
        int i;
@@ -1267,20 +1289,21 @@ char *SSL_get_shared_ciphers(const SSL *s,char *buf,int len)
        sk=s->session->ciphers;
        for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
                {
-               /* Decrement for either the ':' or a '\0' */
-               len--;
+               int n;
+
                c=sk_SSL_CIPHER_value(sk,i);
-               for (cp=c->name; *cp; )
+               n=strlen(c->name);
+               if (n+1 > len)
                        {
-                       if (len-- <= 0)
-                               {
-                               *p='\0';
-                               return(buf);
-                               }
-                       else
-                               *(p++)= *(cp++);
+                       if (p != buf)
+                               --p;
+                       *p='\0';
+                       return buf;
                        }
+               strcpy(p,c->name);
+               p+=n;
                *(p++)=':';
+               len-=n+1;
                }
        p[-1]='\0';
        return(buf);
@@ -1364,7 +1387,7 @@ err:
        }
 
 
-#ifndef OPENSSL_TLSEXT
+#ifndef OPENSSL_NO_TLSEXT
 /** return a servername extension value if provided in Client Hello, or NULL.
  * So far, only host_name types are defined (RFC 3546).
  */
@@ -1541,6 +1564,15 @@ SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
 #ifndef OPENSSL_NO_TLSEXT
        ret->tlsext_servername_callback = 0;
        ret->tlsext_servername_arg = NULL;
+       /* Setup RFC4507 ticket keys */
+       if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0)
+               || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)
+               || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))
+               ret->options |= SSL_OP_NO_TICKET;
+
+       ret->tlsext_status_cb = 0;
+       ret->tlsext_status_arg = NULL;
+
 #endif
 #ifndef OPENSSL_NO_PSK
        ret->psk_identity_hint=NULL;
@@ -1710,11 +1742,24 @@ void ssl_set_cert_masks(CERT *c, SSL_CIPHER *cipher)
        emask_k=0;
        emask_a=0;
 
+       
+
 #ifdef CIPHER_DEBUG
        printf("rt=%d rte=%d dht=%d ecdht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n",
-               rsa_tmp,rsa_tmp_export,dh_tmp,ecdh_tmp,
+               rsa_tmp,rsa_tmp_export,dh_tmp,have_ecdh_tmp,
                rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
 #endif
+       
+       cpk = &(c->pkeys[SSL_PKEY_GOST01]);
+       if (cpk->x509 != NULL && cpk->privatekey !=NULL) {
+               mask_k |= SSL_kGOST;
+               mask_a |= SSL_aGOST01;
+       }
+       cpk = &(c->pkeys[SSL_PKEY_GOST94]);
+       if (cpk->x509 != NULL && cpk->privatekey !=NULL) {
+               mask_k |= SSL_kGOST;
+               mask_a |= SSL_aGOST94;
+       }
 
        if (rsa_enc || (rsa_tmp && rsa_sign))
                mask_k|=SSL_kRSA;
@@ -1946,7 +1991,7 @@ X509 *ssl_get_server_send_cert(SSL *s)
        alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
        alg_a = s->s3->tmp.new_cipher->algorithm_auth;
 
-       if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
+       if (alg_k & (SSL_kECDHr|SSL_kECDHe))
                {
                /* we don't need to look at SSL_kEECDH
                 * since no certificate is needed for
@@ -1983,6 +2028,10 @@ X509 *ssl_get_server_send_cert(SSL *s)
                /* VRS something else here? */
                return(NULL);
                }
+       else if (alg_a & SSL_aGOST94) 
+               i=SSL_PKEY_GOST94;
+       else if (alg_a & SSL_aGOST01)
+               i=SSL_PKEY_GOST01;
        else /* if (alg_a & SSL_aNULL) */
                {
                SSLerr(SSL_F_SSL_GET_SERVER_SEND_CERT,ERR_R_INTERNAL_ERROR);
@@ -2203,6 +2252,8 @@ void SSL_set_accept_state(SSL *s)
        s->handshake_func=s->method->ssl_accept;
        /* clear the current cipher */
        ssl_clear_cipher_ctx(s);
+       ssl_clear_hash_ctx(&s->read_hash);
+       ssl_clear_hash_ctx(&s->write_hash);
        }
 
 void SSL_set_connect_state(SSL *s)
@@ -2213,6 +2264,8 @@ void SSL_set_connect_state(SSL *s)
        s->handshake_func=s->method->ssl_connect;
        /* clear the current cipher */
        ssl_clear_cipher_ctx(s);
+       ssl_clear_hash_ctx(&s->read_hash);
+       ssl_clear_hash_ctx(&s->write_hash);
        }
 
 int ssl_undefined_function(SSL *s)
@@ -2555,8 +2608,10 @@ SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
        {
        if (ssl->ctx == ctx)
                return ssl->ctx;
+#ifndef OPENSSL_NO_TLSEXT
        if (ctx == NULL)
                ctx = ssl->initial_ctx;
+#endif
        if (ssl->cert != NULL)
                ssl_cert_free(ssl->cert);
        ssl->cert = ssl_cert_dup(ctx->cert);
@@ -2836,7 +2891,25 @@ void SSL_set_msg_callback(SSL *ssl, void (*cb)(int write_p, int version, int con
        SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
        }
 
-
+/* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
+ * vairable, freeing  EVP_MD_CTX previously stored in that variable, if
+ * any. If EVP_MD pointer is passed, initializes ctx with this md
+ * Returns newly allocated ctx;
+ */ 
+
+EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md) 
+{
+       ssl_clear_hash_ctx(hash);
+       *hash = EVP_MD_CTX_create();
+       if (md) EVP_DigestInit_ex(*hash,md,NULL);
+       return *hash;
+}
+void ssl_clear_hash_ctx(EVP_MD_CTX **hash) 
+{
+
+       if (*hash) EVP_MD_CTX_destroy(*hash);
+       *hash=NULL;
+}
 
 #if defined(_WINDLL) && defined(OPENSSL_SYS_WIN16)
 #include "../crypto/bio/bss_file.c"