X-Git-Url: https://git.openssl.org/?p=openssl.git;a=blobdiff_plain;f=crypto%2Fevp%2Fdigest.c;h=6567921a0ebf681bf504c45be3b7ca8ae701545e;hp=cd930ebd7a8eecdaa8e118edd0aacf758da4253f;hb=fe5c5cb85197aec7d68ab095b866ed22076850d0;hpb=e3477d3e5ccd971da3d8a90a7d5096b47372d288 diff --git a/crypto/evp/digest.c b/crypto/evp/digest.c index cd930ebd7a..6567921a0e 100644 --- a/crypto/evp/digest.c +++ b/crypto/evp/digest.c @@ -141,6 +141,20 @@ void EVP_MD_CTX_free(EVP_MD_CTX *ctx) OPENSSL_free(ctx); } +int evp_md_ctx_free_algctx(EVP_MD_CTX *ctx) +{ + if (ctx->algctx != NULL) { + if (!ossl_assert(ctx->digest != NULL)) { + ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); + return 0; + } + if (ctx->digest->freectx != NULL) + ctx->digest->freectx(ctx->algctx); + ctx->algctx = NULL; + } + return 1; +} + static int evp_md_init_internal(EVP_MD_CTX *ctx, const EVP_MD *type, const OSSL_PARAM params[], ENGINE *impl) { @@ -169,16 +183,6 @@ static int evp_md_init_internal(EVP_MD_CTX *ctx, const EVP_MD *type, EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED); - if (ctx->algctx != NULL) { - if (!ossl_assert(ctx->digest != NULL)) { - ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); - return 0; - } - if (ctx->digest->freectx != NULL) - ctx->digest->freectx(ctx->algctx); - ctx->algctx = NULL; - } - if (type != NULL) { ctx->reqdigest = type; } else { @@ -227,6 +231,10 @@ static int evp_md_init_internal(EVP_MD_CTX *ctx, const EVP_MD *type, #endif || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0 || type->origin == EVP_ORIG_METH) { + /* If we were using provided hash before, cleanup algctx */ + if (!evp_md_ctx_free_algctx(ctx)) + return 0; + if (ctx->digest == ctx->fetched_digest) ctx->digest = NULL; EVP_MD_free(ctx->fetched_digest); @@ -237,6 +245,8 @@ static int evp_md_init_internal(EVP_MD_CTX *ctx, const EVP_MD *type, cleanup_old_md_data(ctx, 1); /* Start of non-legacy code below */ + if (ctx->digest != type && !evp_md_ctx_free_algctx(ctx)) + return 0; if (type->prov == NULL) { #ifdef FIPS_MODULE @@ -259,11 +269,6 @@ static int evp_md_init_internal(EVP_MD_CTX *ctx, const EVP_MD *type, #endif } - if (ctx->algctx != NULL && ctx->digest != NULL && ctx->digest != type) { - if (ctx->digest->freectx != NULL) - ctx->digest->freectx(ctx->algctx); - ctx->algctx = NULL; - } if (type->prov != NULL && ctx->fetched_digest != type) { if (!EVP_MD_up_ref((EVP_MD *)type)) { ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);