mirror of
https://github.com/postgres/postgres.git
synced 2025-06-06 00:02:36 -04: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
160 lines
3.7 KiB
C
160 lines
3.7 KiB
C
/*-------------------------------------------------------------------------
|
|
*
|
|
* md5_common.c
|
|
* Routines shared between all MD5 implementations used for encrypted
|
|
* passwords.
|
|
*
|
|
* Sverre H. Huseby <sverrehu@online.no>
|
|
*
|
|
* Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
*
|
|
* IDENTIFICATION
|
|
* src/common/md5_common.c
|
|
*
|
|
*-------------------------------------------------------------------------
|
|
*/
|
|
|
|
#ifndef FRONTEND
|
|
#include "postgres.h"
|
|
#else
|
|
#include "postgres_fe.h"
|
|
#endif
|
|
|
|
#include "common/cryptohash.h"
|
|
#include "common/md5.h"
|
|
|
|
static void
|
|
bytesToHex(uint8 b[16], char *s)
|
|
{
|
|
static const char *hex = "0123456789abcdef";
|
|
int q,
|
|
w;
|
|
|
|
for (q = 0, w = 0; q < 16; q++)
|
|
{
|
|
s[w++] = hex[(b[q] >> 4) & 0x0F];
|
|
s[w++] = hex[b[q] & 0x0F];
|
|
}
|
|
s[w] = '\0';
|
|
}
|
|
|
|
/*
|
|
* pg_md5_hash
|
|
*
|
|
* Calculates the MD5 sum of the bytes in a buffer.
|
|
*
|
|
* SYNOPSIS #include "md5.h"
|
|
* int pg_md5_hash(const void *buff, size_t len, char *hexsum)
|
|
*
|
|
* INPUT buff the buffer containing the bytes that you want
|
|
* the MD5 sum of.
|
|
* len number of bytes in the buffer.
|
|
*
|
|
* OUTPUT hexsum the MD5 sum as a '\0'-terminated string of
|
|
* hexadecimal digits. an MD5 sum is 16 bytes long.
|
|
* each byte is represented by two hexadecimal
|
|
* characters. you thus need to provide an array
|
|
* of 33 characters, including the trailing '\0'.
|
|
*
|
|
* RETURNS false on failure (out of memory for internal buffers
|
|
* or MD5 computation failure) or true on success.
|
|
*
|
|
* STANDARDS MD5 is described in RFC 1321.
|
|
*
|
|
* AUTHOR Sverre H. Huseby <sverrehu@online.no>
|
|
*
|
|
*/
|
|
|
|
bool
|
|
pg_md5_hash(const void *buff, size_t len, char *hexsum, const char **errstr)
|
|
{
|
|
uint8 sum[MD5_DIGEST_LENGTH];
|
|
pg_cryptohash_ctx *ctx;
|
|
|
|
ctx = pg_cryptohash_create(PG_MD5);
|
|
if (ctx == NULL)
|
|
return false;
|
|
|
|
if (pg_cryptohash_init(ctx) < 0 ||
|
|
pg_cryptohash_update(ctx, buff, len) < 0 ||
|
|
pg_cryptohash_final(ctx, sum, sizeof(sum)) < 0)
|
|
{
|
|
*errstr = pg_cryptohash_error(ctx);
|
|
pg_cryptohash_free(ctx);
|
|
return false;
|
|
}
|
|
|
|
bytesToHex(sum, hexsum);
|
|
pg_cryptohash_free(ctx);
|
|
return true;
|
|
}
|
|
|
|
bool
|
|
pg_md5_binary(const void *buff, size_t len, void *outbuf, const char **errstr)
|
|
{
|
|
pg_cryptohash_ctx *ctx;
|
|
|
|
*errstr = NULL;
|
|
ctx = pg_cryptohash_create(PG_MD5);
|
|
if (ctx == NULL)
|
|
{
|
|
*errstr = pg_cryptohash_error(NULL); /* returns OOM */
|
|
return false;
|
|
}
|
|
|
|
if (pg_cryptohash_init(ctx) < 0 ||
|
|
pg_cryptohash_update(ctx, buff, len) < 0 ||
|
|
pg_cryptohash_final(ctx, outbuf, MD5_DIGEST_LENGTH) < 0)
|
|
{
|
|
*errstr = pg_cryptohash_error(ctx);
|
|
pg_cryptohash_free(ctx);
|
|
return false;
|
|
}
|
|
|
|
pg_cryptohash_free(ctx);
|
|
return true;
|
|
}
|
|
|
|
|
|
/*
|
|
* Computes MD5 checksum of "passwd" (a null-terminated string) followed
|
|
* by "salt" (which need not be null-terminated).
|
|
*
|
|
* Output format is "md5" followed by a 32-hex-digit MD5 checksum.
|
|
* Hence, the output buffer "buf" must be at least 36 bytes long.
|
|
*
|
|
* Returns true if okay, false on error with *errstr providing some
|
|
* error context.
|
|
*/
|
|
bool
|
|
pg_md5_encrypt(const char *passwd, const char *salt, size_t salt_len,
|
|
char *buf, const char **errstr)
|
|
{
|
|
size_t passwd_len = strlen(passwd);
|
|
|
|
/* +1 here is just to avoid risk of unportable malloc(0) */
|
|
char *crypt_buf = malloc(passwd_len + salt_len + 1);
|
|
bool ret;
|
|
|
|
if (!crypt_buf)
|
|
{
|
|
*errstr = _("out of memory");
|
|
return false;
|
|
}
|
|
|
|
/*
|
|
* Place salt at the end because it may be known by users trying to crack
|
|
* the MD5 output.
|
|
*/
|
|
memcpy(crypt_buf, passwd, passwd_len);
|
|
memcpy(crypt_buf + passwd_len, salt, salt_len);
|
|
|
|
strcpy(buf, "md5");
|
|
ret = pg_md5_hash(crypt_buf, passwd_len + salt_len, buf + 3, errstr);
|
|
|
|
free(crypt_buf);
|
|
|
|
return ret;
|
|
}
|