Fix memory leak.
[openssl.git] / crypto / evp / evp_enc.c
index 9a7a553bef292ebfc368da55918639c064294ee0..e3ed556f2610dabd1c82aa1daf109ecd737ebc46 100644 (file)
@@ -76,8 +76,12 @@ int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
             unsigned char *key, unsigned char *iv, int enc)
        {
        if(enc && (enc != -1)) enc = 1;
-       if (cipher) {
+       if (cipher)
+               {
+               if(ctx->cipher)
+                       EVP_CIPHER_CTX_cleanup(ctx);
                ctx->cipher=cipher;
+               ctx->cipher_data=OPENSSL_malloc(ctx->cipher->ctx_size);
                ctx->key_len = cipher->key_len;
                ctx->flags = 0;
                if(ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
@@ -197,6 +201,8 @@ int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
                        *outl=bl;
                        }
                }
+       else
+               *outl = 0;
        i=inl&(bl-1);
        inl-=i;
        if (inl > 0)
@@ -337,6 +343,7 @@ int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c)
                {
                if(!c->cipher->cleanup(c)) return 0;
                }
+       OPENSSL_free(c->cipher_data);
        memset(c,0,sizeof(EVP_CIPHER_CTX));
        return 1;
        }