Commit 28d1601a authored by Michael Paquier's avatar Michael Paquier

pgcrypto: Detect errors with EVP calls from OpenSSL

The following routines are called within pgcrypto when handling digests
but there were no checks for failures:
- EVP_MD_CTX_size (can fail with -1 as of 3.0.0)
- EVP_MD_CTX_block_size (can fail with -1 as of 3.0.0)
- EVP_DigestInit_ex
- EVP_DigestUpdate
- EVP_DigestFinal_ex

A set of elog(ERROR) is added by this commit to detect such failures,
that should never happen except in the event of a processing failure
internal to OpenSSL.

Note that it would be possible to use ERR_reason_error_string() to get
more context about such errors, but these refer mainly to the internals
of OpenSSL, so it is not really obvious how useful that would be.  This
is left out for simplicity.

Per report from Coverity.  Thanks to Tom Lane for the discussion.

Backpatch-through: 9.5
parent 5da871bf
...@@ -114,16 +114,24 @@ static unsigned ...@@ -114,16 +114,24 @@ static unsigned
digest_result_size(PX_MD *h) digest_result_size(PX_MD *h)
{ {
OSSLDigest *digest = (OSSLDigest *) h->p.ptr; OSSLDigest *digest = (OSSLDigest *) h->p.ptr;
int result = EVP_MD_CTX_size(digest->ctx);
return EVP_MD_CTX_size(digest->ctx); if (result < 0)
elog(ERROR, "EVP_MD_CTX_size() failed");
return result;
} }
static unsigned static unsigned
digest_block_size(PX_MD *h) digest_block_size(PX_MD *h)
{ {
OSSLDigest *digest = (OSSLDigest *) h->p.ptr; OSSLDigest *digest = (OSSLDigest *) h->p.ptr;
int result = EVP_MD_CTX_block_size(digest->ctx);
if (result < 0)
elog(ERROR, "EVP_MD_CTX_block_size() failed");
return EVP_MD_CTX_block_size(digest->ctx); return result;
} }
static void static void
...@@ -131,7 +139,8 @@ digest_reset(PX_MD *h) ...@@ -131,7 +139,8 @@ digest_reset(PX_MD *h)
{ {
OSSLDigest *digest = (OSSLDigest *) h->p.ptr; OSSLDigest *digest = (OSSLDigest *) h->p.ptr;
EVP_DigestInit_ex(digest->ctx, digest->algo, NULL); if (!EVP_DigestInit_ex(digest->ctx, digest->algo, NULL))
elog(ERROR, "EVP_DigestInit_ex() failed");
} }
static void static void
...@@ -139,7 +148,8 @@ digest_update(PX_MD *h, const uint8 *data, unsigned dlen) ...@@ -139,7 +148,8 @@ digest_update(PX_MD *h, const uint8 *data, unsigned dlen)
{ {
OSSLDigest *digest = (OSSLDigest *) h->p.ptr; OSSLDigest *digest = (OSSLDigest *) h->p.ptr;
EVP_DigestUpdate(digest->ctx, data, dlen); if (!EVP_DigestUpdate(digest->ctx, data, dlen))
elog(ERROR, "EVP_DigestUpdate() failed");
} }
static void static void
...@@ -147,7 +157,8 @@ digest_finish(PX_MD *h, uint8 *dst) ...@@ -147,7 +157,8 @@ digest_finish(PX_MD *h, uint8 *dst)
{ {
OSSLDigest *digest = (OSSLDigest *) h->p.ptr; OSSLDigest *digest = (OSSLDigest *) h->p.ptr;
EVP_DigestFinal_ex(digest->ctx, dst, NULL); if (!EVP_DigestFinal_ex(digest->ctx, dst, NULL))
elog(ERROR, "EVP_DigestFinal_ex() failed");
} }
static void static void
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment