Check EVP_Cipher return values for SSL2
[openssl.git] / ssl / s2_enc.c
index 12e17bf66880939e639b335e19d0514603963ae4..95d6eef6da8b7758815f4e513d5bcb8995e74218 100644 (file)
@@ -59,7 +59,6 @@
 #include "ssl_locl.h"
 #ifndef OPENSSL_NO_SSL2
 #include <stdio.h>
-#include "cryptlib.h"
 
 int ssl2_enc_init(SSL *s, int client)
        {
@@ -69,29 +68,31 @@ int ssl2_enc_init(SSL *s, int client)
        const EVP_MD *md;
        int num;
 
-       if (!ssl_cipher_get_evp(s->session,&c,&md,NULL))
+       if (!ssl_cipher_get_evp(s->session,&c,&md,NULL,NULL,NULL, 0))
                {
                ssl2_return_error(s,SSL2_PE_NO_CIPHER);
                SSLerr(SSL_F_SSL2_ENC_INIT,SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS);
                return(0);
                }
-
-       s->read_hash=md;
-       s->write_hash=md;
+       ssl_replace_hash(&s->read_hash,md);
+       ssl_replace_hash(&s->write_hash,md);
 
        if ((s->enc_read_ctx == NULL) &&
                ((s->enc_read_ctx=(EVP_CIPHER_CTX *)
                OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL))
                goto err;
+
+       /* make sure it's intialized in case the malloc for enc_write_ctx fails
+        * and we exit with an error */
+       rs= s->enc_read_ctx;
+       EVP_CIPHER_CTX_init(rs);
+
        if ((s->enc_write_ctx == NULL) &&
                ((s->enc_write_ctx=(EVP_CIPHER_CTX *)
                OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL))
                goto err;
 
-       rs= s->enc_read_ctx;
        ws= s->enc_write_ctx;
-
-       EVP_CIPHER_CTX_init(rs);
        EVP_CIPHER_CTX_init(ws);
 
        num=c->key_len;
@@ -116,8 +117,9 @@ err:
 
 /* read/writes from s->s2->mac_data using length for encrypt and 
  * decrypt.  It sets s->s2->padding and s->[rw]length
- * if we are encrypting */
-void ssl2_enc(SSL *s, int send)
+ * if we are encrypting
+ * Returns 0 on error and 1 on success */
+int ssl2_enc(SSL *s, int send)
        {
        EVP_CIPHER_CTX *ds;
        unsigned long l;
@@ -144,7 +146,10 @@ void ssl2_enc(SSL *s, int send)
        if (bs == 8)
                l=(l+7)/8*8;
 
-       EVP_Cipher(ds,s->s2->mac_data,s->s2->mac_data,l);
+       if(EVP_Cipher(ds,s->s2->mac_data,s->s2->mac_data,l) < 1)
+               return 0;
+
+       return 1;
        }
 
 void ssl2_mac(SSL *s, unsigned char *md, int send)
@@ -174,7 +179,7 @@ void ssl2_mac(SSL *s, unsigned char *md, int send)
 
        /* There has to be a MAC algorithm. */
        EVP_MD_CTX_init(&c);
-       EVP_DigestInit_ex(&c, s->read_hash, NULL);
+       EVP_MD_CTX_copy(&c, s->read_hash);
        EVP_DigestUpdate(&c,sec,
                EVP_CIPHER_CTX_key_length(s->enc_read_ctx));
        EVP_DigestUpdate(&c,act,len);