Ensure any allocated MAC is freed in the provider code
authorMatt Caswell <matt@openssl.org>
Tue, 23 Jun 2020 15:47:31 +0000 (16:47 +0100)
committerMatt Caswell <matt@openssl.org>
Mon, 6 Jul 2020 08:26:09 +0000 (09:26 +0100)
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/12288)

22 files changed:
providers/implementations/ciphers/cipher_aes.c
providers/implementations/ciphers/cipher_aes_cbc_hmac_sha.c
providers/implementations/ciphers/cipher_aes_ocb.c
providers/implementations/ciphers/cipher_aes_wrp.c
providers/implementations/ciphers/cipher_aes_xts.c
providers/implementations/ciphers/cipher_aria.c
providers/implementations/ciphers/cipher_blowfish.c
providers/implementations/ciphers/cipher_camellia.c
providers/implementations/ciphers/cipher_cast5.c
providers/implementations/ciphers/cipher_chacha20.c
providers/implementations/ciphers/cipher_chacha20_poly1305.c
providers/implementations/ciphers/cipher_des.c
providers/implementations/ciphers/cipher_idea.c
providers/implementations/ciphers/cipher_rc2.c
providers/implementations/ciphers/cipher_rc4.c
providers/implementations/ciphers/cipher_rc4_hmac_md5.c
providers/implementations/ciphers/cipher_rc5.c
providers/implementations/ciphers/cipher_seed.c
providers/implementations/ciphers/cipher_sm4.c
providers/implementations/ciphers/cipher_tdes_common.c
providers/implementations/ciphers/ciphercommon.c
providers/implementations/include/prov/ciphercommon.h

index ea23e1eed9730301705a5b879121deaad1ca2904..decc27517ca6756dbb7dd0df263dc70c673fdb54 100644 (file)
@@ -26,6 +26,7 @@ static void aes_freectx(void *vctx)
 {
     PROV_AES_CTX *ctx = (PROV_AES_CTX *)vctx;
 
+    cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
     OPENSSL_clear_free(ctx,  sizeof(*ctx));
 }
 
index d684914c5ac10360bfc9925a0a9e0ef2bfec1561..046a66c56db5f71126aab8576721ccdf31ad8e04 100644 (file)
@@ -283,8 +283,10 @@ static void aes_cbc_hmac_sha1_freectx(void *vctx)
 {
     PROV_AES_HMAC_SHA1_CTX *ctx = (PROV_AES_HMAC_SHA1_CTX *)vctx;
 
-    if (ctx != NULL)
+    if (ctx != NULL) {
+        cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
         OPENSSL_clear_free(ctx, sizeof(*ctx));
+    }
 }
 
 static void *aes_cbc_hmac_sha256_newctx(void *provctx, size_t kbits,
@@ -304,8 +306,10 @@ static void aes_cbc_hmac_sha256_freectx(void *vctx)
 {
     PROV_AES_HMAC_SHA256_CTX *ctx = (PROV_AES_HMAC_SHA256_CTX *)vctx;
 
-    if (ctx != NULL)
+    if (ctx != NULL) {
+        cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
         OPENSSL_clear_free(ctx, sizeof(*ctx));
+    }
 }
 
 # define IMPLEMENT_CIPHER(nm, sub, kbits, blkbits, ivbits, flags)              \
index 09c38b7ef46b9801d825f1904dab4b579c4649ae..2f30b7ffdf384e443a6e8e871adb17c26ef52874 100644 (file)
@@ -305,6 +305,7 @@ static void aes_ocb_freectx(void *vctx)
 
     if (ctx != NULL) {
         aes_generic_ocb_cleanup(ctx);
+        cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
         OPENSSL_clear_free(ctx,  sizeof(*ctx));
     }
 }
index 9782afa137aa72e64fe025b2aac44b81be1a7f45..5c2ab1c5074b58e92ee0d1bf72559e6691e786af 100644 (file)
@@ -64,6 +64,7 @@ static void aes_wrap_freectx(void *vctx)
 {
     PROV_AES_WRAP_CTX *wctx = (PROV_AES_WRAP_CTX *)vctx;
 
+    cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
     OPENSSL_clear_free(wctx,  sizeof(*wctx));
 }
 
index 96e885e2ca940b8596959bcd575f7ec67c91a9d5..f564075abe7e1248658dd7a263ebd710424390c6 100644 (file)
@@ -120,6 +120,7 @@ static void aes_xts_freectx(void *vctx)
 {
     PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
 
+    cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
     OPENSSL_clear_free(ctx,  sizeof(*ctx));
 }
 
index 67dfe0d35fb63c0441339907da1ea6a26209b42d..a07961792815a7683c0a072c5c7bdeb286e72143 100644 (file)
@@ -19,6 +19,7 @@ static void aria_freectx(void *vctx)
 {
     PROV_ARIA_CTX *ctx = (PROV_ARIA_CTX *)vctx;
 
+    cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
     OPENSSL_clear_free(ctx,  sizeof(*ctx));
 }
 
index bb2fa88f6ae8cdf563cc5614168b0d7cf1895939..3eb4ebead26f0c21e3288b8efed2bcd6d2915ab2 100644 (file)
@@ -27,6 +27,7 @@ static void blowfish_freectx(void *vctx)
 {
     PROV_BLOWFISH_CTX *ctx = (PROV_BLOWFISH_CTX *)vctx;
 
+    cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
     OPENSSL_clear_free(ctx,  sizeof(*ctx));
 }
 
index abb24621a66ed302c3acd8949074c120fb99cd1f..ffb23b475aed11288578e18bd6828dc3fedb91a3 100644 (file)
@@ -25,6 +25,7 @@ static void camellia_freectx(void *vctx)
 {
     PROV_CAMELLIA_CTX *ctx = (PROV_CAMELLIA_CTX *)vctx;
 
+    cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
     OPENSSL_clear_free(ctx,  sizeof(*ctx));
 }
 
index febadfb62b7c4965894c950fdcc96c620ae51907..938b8d20130e4892927eaade09330d429980579d 100644 (file)
@@ -28,6 +28,7 @@ static void cast5_freectx(void *vctx)
 {
     PROV_CAST_CTX *ctx = (PROV_CAST_CTX *)vctx;
 
+    cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
     OPENSSL_clear_free(ctx,  sizeof(*ctx));
 }
 
index 45571180c8fb4d41901b646b75bf6b9b8f691d04..6759b0e0f9399a67ee600f787e115f55e202dd4d 100644 (file)
@@ -55,6 +55,7 @@ static void chacha20_freectx(void *vctx)
     PROV_CHACHA20_CTX *ctx = (PROV_CHACHA20_CTX *)vctx;
 
     if (ctx != NULL) {
+        cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
         OPENSSL_clear_free(ctx, sizeof(*ctx));
     }
 }
index 3fa4684125e72b020a59d5072f0cdf2304d9928c..a93f722551c24bd770a1fe83b026a6be27dc9d74 100644 (file)
@@ -65,8 +65,10 @@ static void chacha20_poly1305_freectx(void *vctx)
 {
     PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx;
 
-    if (ctx != NULL)
+    if (ctx != NULL) {
+        cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
         OPENSSL_clear_free(ctx, sizeof(*ctx));
+    }
 }
 
 static int chacha20_poly1305_get_params(OSSL_PARAM params[])
index 7a7f16e45429f865bde41f9d5366b208150ba2f7..7a60e0501ce09653a8e333a39ccb6fc39fe7758a 100644 (file)
@@ -58,6 +58,7 @@ static void des_freectx(void *vctx)
 {
     PROV_DES_CTX *ctx = (PROV_DES_CTX *)vctx;
 
+    cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
     OPENSSL_clear_free(ctx,  sizeof(*ctx));
 }
 
index 68cca45f92ff923c8241d6bcd216cba3a127bdf7..7fc5d8403dda3ae895f6a935468faaf16c3a1546 100644 (file)
@@ -26,6 +26,7 @@ static void idea_freectx(void *vctx)
 {
     PROV_IDEA_CTX *ctx = (PROV_IDEA_CTX *)vctx;
 
+    cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
     OPENSSL_clear_free(ctx,  sizeof(*ctx));
 }
 
index f2304b7c0f153f7d97db1b4594f750a18949854f..d1558be002c8c96e1d9e10a2b4af8a1570e11171 100644 (file)
@@ -32,6 +32,7 @@ static void rc2_freectx(void *vctx)
 {
     PROV_RC2_CTX *ctx = (PROV_RC2_CTX *)vctx;
 
+    cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
     OPENSSL_clear_free(ctx,  sizeof(*ctx));
 }
 
index 97d66660f0c1848b289d317f773fb0a6532a7e80..4660185d45b24d53b0399b338bb0841cd1053a85 100644 (file)
@@ -28,6 +28,7 @@ static void rc4_freectx(void *vctx)
 {
     PROV_RC4_CTX *ctx = (PROV_RC4_CTX *)vctx;
 
+    cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
     OPENSSL_clear_free(ctx,  sizeof(*ctx));
 }
 
index 836274abb0ad95b5dce8a54794ad9bcb776401f3..d9535e23ce01b0b7eb7b2dd42d4e8d4581633850 100644 (file)
@@ -62,6 +62,7 @@ static void rc4_hmac_md5_freectx(void *vctx)
 {
     PROV_RC4_HMAC_MD5_CTX *ctx = (PROV_RC4_HMAC_MD5_CTX *)vctx;
 
+    cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
     OPENSSL_clear_free(ctx,  sizeof(*ctx));
 }
 
index 4d719279148ae96b07cddd7ffed16b2bef783826..68ce6fdd91551fc90398c371307286bac4bb0f4c 100644 (file)
@@ -28,6 +28,7 @@ static void rc5_freectx(void *vctx)
 {
     PROV_RC5_CTX *ctx = (PROV_RC5_CTX *)vctx;
 
+    cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
     OPENSSL_clear_free(ctx,  sizeof(*ctx));
 }
 
index 3a3e012fe0145ddf5a7f1fcbaba57a432b1e77a8..53520b3c4da16648e092ed596a214883adf18ba7 100644 (file)
@@ -25,6 +25,7 @@ static void seed_freectx(void *vctx)
 {
     PROV_SEED_CTX *ctx = (PROV_SEED_CTX *)vctx;
 
+    cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
     OPENSSL_clear_free(ctx,  sizeof(*ctx));
 }
 
index e7208ad16cb1090e25ca8e4193e9f439d0700546..a5920562fcb4cc7506698c4f1939f4f0455f5e16 100644 (file)
@@ -19,6 +19,7 @@ static void sm4_freectx(void *vctx)
 {
     PROV_SM4_CTX *ctx = (PROV_SM4_CTX *)vctx;
 
+    cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
     OPENSSL_clear_free(ctx,  sizeof(*ctx));
 }
 
index 4e50450e4dcf5ed330ec4fcd9591360f3887b8d3..6cdc88749c0cca69398b3438356cc376c10539a8 100644 (file)
@@ -48,6 +48,7 @@ void tdes_freectx(void *vctx)
 {
     PROV_TDES_CTX *ctx = (PROV_TDES_CTX *)vctx;
 
+    cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
     OPENSSL_clear_free(ctx,  sizeof(*ctx));
 }
 
index 9c71a7df2a2ab45ccab8a067422694146705d3a3..2cd5b6f571b7efc198fd41a5c784f1b4b8d1573b 100644 (file)
@@ -133,6 +133,15 @@ const OSSL_PARAM *cipher_aead_settable_ctx_params(void)
     return cipher_aead_known_settable_ctx_params;
 }
 
+void cipher_generic_reset_ctx(PROV_CIPHER_CTX *ctx)
+{
+    if (ctx != NULL && ctx->alloced) {
+        OPENSSL_free(ctx->tlsmac);
+        ctx->alloced = 0;
+        ctx->tlsmac = NULL;
+    }
+}
+
 static int cipher_generic_init_internal(PROV_CIPHER_CTX *ctx,
                                         const unsigned char *key, size_t keylen,
                                         const unsigned char *iv, size_t ivlen,
@@ -203,8 +212,13 @@ int cipher_generic_block_update(void *vctx, unsigned char *out, size_t *outl,
             return 0;
         }
 
+        if (ctx->alloced) {
+            OPENSSL_free(ctx->tlsmac);
+            ctx->alloced = 0;
+            ctx->tlsmac = NULL;
+        }
+
         /* This only fails if padding is publicly invalid */
-        /* TODO(3.0): FIX ME FIX ME - Figure out aead */
         *outl = inl;
         if (!ctx->enc
                 && !tlsunpadblock(ctx->libctx, ctx->tlsversion, out, outl,
index 83f64e772810d4d1e0ba53fb673f65cab2b29c56..a5ffbc48a1a08f04e69bdc05c23412fe474942ce 100644 (file)
@@ -87,6 +87,7 @@ struct prov_cipher_hw_st {
     void (*copyctx)(PROV_CIPHER_CTX *dst, const PROV_CIPHER_CTX *src);
 };
 
+void cipher_generic_reset_ctx(PROV_CIPHER_CTX *ctx);
 OSSL_FUNC_cipher_encrypt_init_fn cipher_generic_einit;
 OSSL_FUNC_cipher_decrypt_init_fn cipher_generic_dinit;
 OSSL_FUNC_cipher_update_fn cipher_generic_block_update;
@@ -103,6 +104,7 @@ OSSL_FUNC_cipher_set_ctx_params_fn cipher_var_keylen_set_ctx_params;
 OSSL_FUNC_cipher_settable_ctx_params_fn cipher_var_keylen_settable_ctx_params;
 OSSL_FUNC_cipher_gettable_ctx_params_fn cipher_aead_gettable_ctx_params;
 OSSL_FUNC_cipher_settable_ctx_params_fn cipher_aead_settable_ctx_params;
+
 int cipher_generic_get_params(OSSL_PARAM params[], unsigned int md,
                               unsigned long flags,
                               size_t kbits, size_t blkbits, size_t ivbits);