mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-08-23 05:40:41 +02:00
The existing cryptohash facility was causing problems in some code paths
related to MD5 (frontend and backend) that relied on the fact that the
only type of error that could happen would be an OOM, as the MD5
implementation used in PostgreSQL ~13 (the in-core implementation is
used when compiling with or without OpenSSL in those older versions),
could fail only under this circumstance.
The new cryptohash facilities can fail for reasons other than OOMs, like
attempting MD5 when FIPS is enabled (upstream OpenSSL allows that up to
1.0.2, Fedora and Photon patch OpenSSL 1.1.1 to allow that), so this
would cause incorrect reports to show up.
This commit extends the cryptohash APIs so as callers of those routines
can fetch more context when an error happens, by using a new routine
called pg_cryptohash_error(). The error states are stored within each
implementation's internal context data, so as it is possible to extend
the logic depending on what's suited for an implementation. The default
implementation requires few error states, but OpenSSL could report
various issues depending on its internal state so more is needed in
cryptohash_openssl.c, and the code is shaped so as we are always able to
grab the necessary information.
The core code is changed to adapt to the new error routine, painting
more "const" across the call stack where the static errors are stored,
particularly in authentication code paths on variables that provide
log details. This way, any future changes would warn if attempting to
free these strings. The MD5 authentication code was also a bit blurry
about the handling of "logdetail" (LOG sent to the postmaster), so
improve the comments related that, while on it.
The origin of the problem is 87ae969
, that introduced the centralized
cryptohash facility. Extra changes are done for pgcrypto in v14 for the
non-OpenSSL code path to cope with the improvements done by this
commit.
Reported-by: Michael Mühlbeyer
Author: Michael Paquier
Reviewed-by: Tom Lane
Discussion: https://postgr.es/m/89B7F072-5BBE-4C92-903E-D83E865D9367@trivadis.com
Backpatch-through: 14
210 lines
4.5 KiB
C
210 lines
4.5 KiB
C
/*
|
|
* internal.c
|
|
* Wrapper for builtin functions
|
|
*
|
|
* Copyright (c) 2001 Marko Kreen
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
* SUCH DAMAGE.
|
|
*
|
|
* contrib/pgcrypto/internal-sha2.c
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
#include <time.h>
|
|
|
|
#include "common/cryptohash.h"
|
|
#include "common/sha2.h"
|
|
#include "px.h"
|
|
|
|
void init_sha224(PX_MD *h);
|
|
void init_sha256(PX_MD *h);
|
|
void init_sha384(PX_MD *h);
|
|
void init_sha512(PX_MD *h);
|
|
|
|
/* SHA224 */
|
|
static unsigned
|
|
int_sha224_len(PX_MD *h)
|
|
{
|
|
return PG_SHA224_DIGEST_LENGTH;
|
|
}
|
|
|
|
static unsigned
|
|
int_sha224_block_len(PX_MD *h)
|
|
{
|
|
return PG_SHA224_BLOCK_LENGTH;
|
|
}
|
|
|
|
/* SHA256 */
|
|
static unsigned
|
|
int_sha256_len(PX_MD *h)
|
|
{
|
|
return PG_SHA256_DIGEST_LENGTH;
|
|
}
|
|
|
|
static unsigned
|
|
int_sha256_block_len(PX_MD *h)
|
|
{
|
|
return PG_SHA256_BLOCK_LENGTH;
|
|
}
|
|
|
|
/* SHA384 */
|
|
static unsigned
|
|
int_sha384_len(PX_MD *h)
|
|
{
|
|
return PG_SHA384_DIGEST_LENGTH;
|
|
}
|
|
|
|
static unsigned
|
|
int_sha384_block_len(PX_MD *h)
|
|
{
|
|
return PG_SHA384_BLOCK_LENGTH;
|
|
}
|
|
|
|
/* SHA512 */
|
|
static unsigned
|
|
int_sha512_len(PX_MD *h)
|
|
{
|
|
return PG_SHA512_DIGEST_LENGTH;
|
|
}
|
|
|
|
static unsigned
|
|
int_sha512_block_len(PX_MD *h)
|
|
{
|
|
return PG_SHA512_BLOCK_LENGTH;
|
|
}
|
|
|
|
/* Generic interface for all SHA2 methods */
|
|
static void
|
|
int_sha2_update(PX_MD *h, const uint8 *data, unsigned dlen)
|
|
{
|
|
pg_cryptohash_ctx *ctx = (pg_cryptohash_ctx *) h->p.ptr;
|
|
|
|
if (pg_cryptohash_update(ctx, data, dlen) < 0)
|
|
elog(ERROR, "could not update %s context: %s", "SHA2",
|
|
pg_cryptohash_error(ctx));
|
|
}
|
|
|
|
static void
|
|
int_sha2_reset(PX_MD *h)
|
|
{
|
|
pg_cryptohash_ctx *ctx = (pg_cryptohash_ctx *) h->p.ptr;
|
|
|
|
if (pg_cryptohash_init(ctx) < 0)
|
|
elog(ERROR, "could not initialize %s context: %s", "SHA2",
|
|
pg_cryptohash_error(ctx));
|
|
}
|
|
|
|
static void
|
|
int_sha2_finish(PX_MD *h, uint8 *dst)
|
|
{
|
|
pg_cryptohash_ctx *ctx = (pg_cryptohash_ctx *) h->p.ptr;
|
|
|
|
if (pg_cryptohash_final(ctx, dst, h->result_size(h)) < 0)
|
|
elog(ERROR, "could not finalize %s context: %s", "SHA2",
|
|
pg_cryptohash_error(ctx));
|
|
}
|
|
|
|
static void
|
|
int_sha2_free(PX_MD *h)
|
|
{
|
|
pg_cryptohash_ctx *ctx = (pg_cryptohash_ctx *) h->p.ptr;
|
|
|
|
pg_cryptohash_free(ctx);
|
|
pfree(h);
|
|
}
|
|
|
|
/* init functions */
|
|
|
|
void
|
|
init_sha224(PX_MD *md)
|
|
{
|
|
pg_cryptohash_ctx *ctx;
|
|
|
|
ctx = pg_cryptohash_create(PG_SHA224);
|
|
md->p.ptr = ctx;
|
|
|
|
md->result_size = int_sha224_len;
|
|
md->block_size = int_sha224_block_len;
|
|
md->reset = int_sha2_reset;
|
|
md->update = int_sha2_update;
|
|
md->finish = int_sha2_finish;
|
|
md->free = int_sha2_free;
|
|
|
|
md->reset(md);
|
|
}
|
|
|
|
void
|
|
init_sha256(PX_MD *md)
|
|
{
|
|
pg_cryptohash_ctx *ctx;
|
|
|
|
ctx = pg_cryptohash_create(PG_SHA256);
|
|
md->p.ptr = ctx;
|
|
|
|
md->result_size = int_sha256_len;
|
|
md->block_size = int_sha256_block_len;
|
|
md->reset = int_sha2_reset;
|
|
md->update = int_sha2_update;
|
|
md->finish = int_sha2_finish;
|
|
md->free = int_sha2_free;
|
|
|
|
md->reset(md);
|
|
}
|
|
|
|
void
|
|
init_sha384(PX_MD *md)
|
|
{
|
|
pg_cryptohash_ctx *ctx;
|
|
|
|
ctx = pg_cryptohash_create(PG_SHA384);
|
|
md->p.ptr = ctx;
|
|
|
|
md->result_size = int_sha384_len;
|
|
md->block_size = int_sha384_block_len;
|
|
md->reset = int_sha2_reset;
|
|
md->update = int_sha2_update;
|
|
md->finish = int_sha2_finish;
|
|
md->free = int_sha2_free;
|
|
|
|
md->reset(md);
|
|
}
|
|
|
|
void
|
|
init_sha512(PX_MD *md)
|
|
{
|
|
pg_cryptohash_ctx *ctx;
|
|
|
|
ctx = pg_cryptohash_create(PG_SHA512);
|
|
md->p.ptr = ctx;
|
|
|
|
md->result_size = int_sha512_len;
|
|
md->block_size = int_sha512_block_len;
|
|
md->reset = int_sha2_reset;
|
|
md->update = int_sha2_update;
|
|
md->finish = int_sha2_finish;
|
|
md->free = int_sha2_free;
|
|
|
|
md->reset(md);
|
|
}
|