Skip to content

Commit ed9d84f

Browse files
author
dkostic
committed
Add NULL checks to EVP_MD_CTX_cleanse/cleanup
1 parent d4f233c commit ed9d84f

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

crypto/fipsmodule/digest/digest.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,15 @@ EVP_MD_CTX *EVP_MD_CTX_new(void) {
9898
EVP_MD_CTX *EVP_MD_CTX_create(void) { return EVP_MD_CTX_new(); }
9999

100100
int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx) {
101+
if (ctx == NULL) {
102+
return 1;
103+
}
104+
101105
OPENSSL_free(ctx->md_data);
102106

103107
assert(ctx->pctx == NULL || ctx->pctx_ops != NULL);
104108
// |pctx| should be freed by the user of |EVP_MD_CTX| if
105-
// |EVP_MD_CTX_FLAG_KEEP_PKEY_CTX| is set. Everything other than the external
106-
// |pctx| that |ctx->pctx| was pointing to is cleaned up when the flag is set.
109+
// |EVP_MD_CTX_FLAG_KEEP_PKEY_CTX| is set. Everything other than the external |pctx| that |ctx->pctx| was pointing to is cleaned up when the flag is set.
107110
if (ctx->pctx_ops && !(ctx->flags & EVP_MD_CTX_FLAG_KEEP_PKEY_CTX)) {
108111
ctx->pctx_ops->free(ctx->pctx);
109112
}
@@ -114,6 +117,9 @@ int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx) {
114117
}
115118

116119
void EVP_MD_CTX_cleanse(EVP_MD_CTX *ctx) {
120+
if (ctx == NULL || ctx->md_data == NULL || ctx->digest == NULL) {
121+
return;
122+
}
117123
OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
118124
EVP_MD_CTX_cleanup(ctx);
119125
}

0 commit comments

Comments
 (0)