Ensure we look at EVP_MD_CTX_FLAG_KEEP_PKEY_CTX in non-legacy code
authorMatt Caswell <matt@openssl.org>
Tue, 24 Sep 2019 09:23:41 +0000 (10:23 +0100)
committerMatt Caswell <matt@openssl.org>
Thu, 3 Oct 2019 08:47:12 +0000 (09:47 +0100)
This flag is still relevant even for non-legacy code so we should check
it where appropriate.

Reviewed-by: Paul Dale <paul.dale@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/10013)

crypto/evp/digest.c

index 4b9395d58bf6ae37c1652f969c84900bd2c8aac5..2af28c6157f21bd1e4c2f6d92951d4c5995214c8 100644 (file)
@@ -24,6 +24,16 @@ int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
     if (ctx == NULL)
         return 1;
 
     if (ctx == NULL)
         return 1;
 
+#ifndef FIPS_MODE
+    /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
+    /*
+     * pctx should be freed by the user of EVP_MD_CTX
+     * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set
+     */
+    if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
+        EVP_PKEY_CTX_free(ctx->pctx);
+#endif
+
     if (ctx->digest == NULL || ctx->digest->prov == NULL)
         goto legacy;
 
     if (ctx->digest == NULL || ctx->digest->prov == NULL)
         goto legacy;
 
@@ -53,18 +63,9 @@ int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
         && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) {
         OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
     }
         && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) {
         OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
     }
-    /*
-     * pctx should be freed by the user of EVP_MD_CTX
-     * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set
-     */
-#ifndef FIPS_MODE
-    /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
-    if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
-        EVP_PKEY_CTX_free(ctx->pctx);
 
 
-# ifndef OPENSSL_NO_ENGINE
+#if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE)
     ENGINE_finish(ctx->engine);
     ENGINE_finish(ctx->engine);
-# endif
 #endif
     OPENSSL_cleanse(ctx, sizeof(*ctx));
 
 #endif
     OPENSSL_cleanse(ctx, sizeof(*ctx));