From: Paul Yang Date: Wed, 5 Sep 2018 14:01:33 +0000 (+0800) Subject: Allow EVP_MD_CTX_set_pkey_ctx to accept NULL pctx X-Git-Tag: OpenSSL_1_1_1~28 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=81c7945388a49799f819f5ca5bfe6acd506840c3;p=oweals%2Fopenssl.git Allow EVP_MD_CTX_set_pkey_ctx to accept NULL pctx Reviewed-by: Tim Hudson Reviewed-by: Matt Caswell (Merged from https://github.com/openssl/openssl/pull/7113) --- diff --git a/crypto/evp/evp_lib.c b/crypto/evp/evp_lib.c index 81a5bf0012..1b3c9840c6 100644 --- a/crypto/evp/evp_lib.c +++ b/crypto/evp/evp_lib.c @@ -462,9 +462,21 @@ EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx) void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx) { + /* + * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so + * we have to deal with the cleanup job here. + */ + if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX)) + EVP_PKEY_CTX_free(ctx->pctx); + ctx->pctx = pctx; - /* make sure pctx is not freed when destroying EVP_MD_CTX */ - EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX); + + if (pctx != NULL) { + /* make sure pctx is not freed when destroying EVP_MD_CTX */ + EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX); + } else { + EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX); + } } void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx) diff --git a/doc/man3/EVP_DigestInit.pod b/doc/man3/EVP_DigestInit.pod index 02e7719274..9b74e41d39 100644 --- a/doc/man3/EVP_DigestInit.pod +++ b/doc/man3/EVP_DigestInit.pod @@ -186,8 +186,10 @@ B structure respectively. Assigns an B to B. This is usually used to provide a customzied B to L or -L. The B passed to this function should -be freed by the caller. +L. The B passed to this function should be freed +by the caller. A null B pointer is also allowed to clear the B +assigned to B. In such case, freeing the cleared B or not +depends on how the B is created. =back @@ -374,7 +376,7 @@ later, so now EVP_sha1() can be used with RSA and DSA. EVP_dss1() was removed in OpenSSL 1.1.0. -EVP_MD_CTX_set_pkey_ctx() is added in 1.1.1. +EVP_MD_CTX_set_pkey_ctx() was added in 1.1.1. =head1 COPYRIGHT