Refactor MD5 implementations according to new cryptohash infrastructure
This commit heavily reorganizes the MD5 implementations that exist in
the tree in various aspects.
First, MD5 is added to the list of options available in cryptohash.c and
cryptohash_openssl.c. This means that if building with OpenSSL, EVP is
used for MD5 instead of the fallback implementation that Postgres had
for ages. With the recent refactoring work for cryptohash functions,
this change is straight-forward. If not building with OpenSSL, a
fallback implementation internal to src/common/ is used.
Second, this reduces the number of MD5 implementations present in the
tree from two to one, by moving the KAME implementation from pgcrypto to
src/common/, and by removing the implementation that existed in
src/common/. KAME was already structured with an init/update/final set
of routines by pgcrypto (see original pgcrypto/md5.h) for compatibility
with OpenSSL, so moving it to src/common/ has proved to be a
straight-forward move, requiring no actual manipulation of the internals
of each routine. Some benchmarking has not shown any performance gap
between both implementations.
Similarly to the fallback implementation used for SHA2, the fallback
implementation of MD5 is moved to src/common/md5.c with an internal
header called md5_int.h for the init, update and final routines. This
gets then consumed by cryptohash.c.
The original routines used for MD5-hashed passwords are moved to a
separate file called md5_common.c, also in src/common/, aimed at being
shared between all MD5 implementations as utility routines to keep
compatibility with any code relying on them.
Like the SHA2 changes, this commit had its round of tests on both Linux
and Windows, across all versions of OpenSSL supported on HEAD, with and
even without OpenSSL.
Author: Michael Paquier
Reviewed-by: Daniel Gustafsson
Discussion: https://postgr.es/m/20201106073434.GA4961@paquier.xyz
2020-12-10 03:59:10 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* md5_common.c
|
|
|
|
* Routines shared between all MD5 implementations used for encrypted
|
|
|
|
* passwords.
|
|
|
|
*
|
|
|
|
* Sverre H. Huseby <sverrehu@online.no>
|
|
|
|
*
|
2021-01-02 19:06:25 +01:00
|
|
|
* Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
|
Refactor MD5 implementations according to new cryptohash infrastructure
This commit heavily reorganizes the MD5 implementations that exist in
the tree in various aspects.
First, MD5 is added to the list of options available in cryptohash.c and
cryptohash_openssl.c. This means that if building with OpenSSL, EVP is
used for MD5 instead of the fallback implementation that Postgres had
for ages. With the recent refactoring work for cryptohash functions,
this change is straight-forward. If not building with OpenSSL, a
fallback implementation internal to src/common/ is used.
Second, this reduces the number of MD5 implementations present in the
tree from two to one, by moving the KAME implementation from pgcrypto to
src/common/, and by removing the implementation that existed in
src/common/. KAME was already structured with an init/update/final set
of routines by pgcrypto (see original pgcrypto/md5.h) for compatibility
with OpenSSL, so moving it to src/common/ has proved to be a
straight-forward move, requiring no actual manipulation of the internals
of each routine. Some benchmarking has not shown any performance gap
between both implementations.
Similarly to the fallback implementation used for SHA2, the fallback
implementation of MD5 is moved to src/common/md5.c with an internal
header called md5_int.h for the init, update and final routines. This
gets then consumed by cryptohash.c.
The original routines used for MD5-hashed passwords are moved to a
separate file called md5_common.c, also in src/common/, aimed at being
shared between all MD5 implementations as utility routines to keep
compatibility with any code relying on them.
Like the SHA2 changes, this commit had its round of tests on both Linux
and Windows, across all versions of OpenSSL supported on HEAD, with and
even without OpenSSL.
Author: Michael Paquier
Reviewed-by: Daniel Gustafsson
Discussion: https://postgr.es/m/20201106073434.GA4961@paquier.xyz
2020-12-10 03:59:10 +01:00
|
|
|
* 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)
|
|
|
|
{
|
2020-12-14 04:38:13 +01:00
|
|
|
uint8 sum[MD5_DIGEST_LENGTH];
|
Refactor MD5 implementations according to new cryptohash infrastructure
This commit heavily reorganizes the MD5 implementations that exist in
the tree in various aspects.
First, MD5 is added to the list of options available in cryptohash.c and
cryptohash_openssl.c. This means that if building with OpenSSL, EVP is
used for MD5 instead of the fallback implementation that Postgres had
for ages. With the recent refactoring work for cryptohash functions,
this change is straight-forward. If not building with OpenSSL, a
fallback implementation internal to src/common/ is used.
Second, this reduces the number of MD5 implementations present in the
tree from two to one, by moving the KAME implementation from pgcrypto to
src/common/, and by removing the implementation that existed in
src/common/. KAME was already structured with an init/update/final set
of routines by pgcrypto (see original pgcrypto/md5.h) for compatibility
with OpenSSL, so moving it to src/common/ has proved to be a
straight-forward move, requiring no actual manipulation of the internals
of each routine. Some benchmarking has not shown any performance gap
between both implementations.
Similarly to the fallback implementation used for SHA2, the fallback
implementation of MD5 is moved to src/common/md5.c with an internal
header called md5_int.h for the init, update and final routines. This
gets then consumed by cryptohash.c.
The original routines used for MD5-hashed passwords are moved to a
separate file called md5_common.c, also in src/common/, aimed at being
shared between all MD5 implementations as utility routines to keep
compatibility with any code relying on them.
Like the SHA2 changes, this commit had its round of tests on both Linux
and Windows, across all versions of OpenSSL supported on HEAD, with and
even without OpenSSL.
Author: Michael Paquier
Reviewed-by: Daniel Gustafsson
Discussion: https://postgr.es/m/20201106073434.GA4961@paquier.xyz
2020-12-10 03:59:10 +01:00
|
|
|
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 ||
|
Add result size as argument of pg_cryptohash_final() for overflow checks
With its current design, a careless use of pg_cryptohash_final() could
would result in an out-of-bound write in memory as the size of the
destination buffer to store the result digest is not known to the
cryptohash internals, without the caller knowing about that. This
commit adds a new argument to pg_cryptohash_final() to allow such sanity
checks, and implements such defenses.
The internals of SCRAM for HMAC could be tightened a bit more, but as
everything is based on SCRAM_KEY_LEN with uses particular to this code
there is no need to complicate its interface more than necessary, and
this comes back to the refactoring of HMAC in core. Except that, this
minimizes the uses of the existing DIGEST_LENGTH variables, relying
instead on sizeof() for the result sizes. In ossp-uuid, this also makes
the code more defensive, as it already relied on dce_uuid_t being at
least the size of a MD5 digest.
This is in philosophy similar to cfc40d3 for base64.c and aef8948 for
hex.c.
Reported-by: Ranier Vilela
Author: Michael Paquier, Ranier Vilela
Reviewed-by: Kyotaro Horiguchi
Discussion: https://postgr.es/m/CAEudQAoqEGmcff3J4sTSV-R_16Monuz-UpJFbf_dnVH=APr02Q@mail.gmail.com
2021-02-15 02:18:34 +01:00
|
|
|
pg_cryptohash_final(ctx, sum, sizeof(sum)) < 0)
|
Refactor MD5 implementations according to new cryptohash infrastructure
This commit heavily reorganizes the MD5 implementations that exist in
the tree in various aspects.
First, MD5 is added to the list of options available in cryptohash.c and
cryptohash_openssl.c. This means that if building with OpenSSL, EVP is
used for MD5 instead of the fallback implementation that Postgres had
for ages. With the recent refactoring work for cryptohash functions,
this change is straight-forward. If not building with OpenSSL, a
fallback implementation internal to src/common/ is used.
Second, this reduces the number of MD5 implementations present in the
tree from two to one, by moving the KAME implementation from pgcrypto to
src/common/, and by removing the implementation that existed in
src/common/. KAME was already structured with an init/update/final set
of routines by pgcrypto (see original pgcrypto/md5.h) for compatibility
with OpenSSL, so moving it to src/common/ has proved to be a
straight-forward move, requiring no actual manipulation of the internals
of each routine. Some benchmarking has not shown any performance gap
between both implementations.
Similarly to the fallback implementation used for SHA2, the fallback
implementation of MD5 is moved to src/common/md5.c with an internal
header called md5_int.h for the init, update and final routines. This
gets then consumed by cryptohash.c.
The original routines used for MD5-hashed passwords are moved to a
separate file called md5_common.c, also in src/common/, aimed at being
shared between all MD5 implementations as utility routines to keep
compatibility with any code relying on them.
Like the SHA2 changes, this commit had its round of tests on both Linux
and Windows, across all versions of OpenSSL supported on HEAD, with and
even without OpenSSL.
Author: Michael Paquier
Reviewed-by: Daniel Gustafsson
Discussion: https://postgr.es/m/20201106073434.GA4961@paquier.xyz
2020-12-10 03:59:10 +01:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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 ||
|
Add result size as argument of pg_cryptohash_final() for overflow checks
With its current design, a careless use of pg_cryptohash_final() could
would result in an out-of-bound write in memory as the size of the
destination buffer to store the result digest is not known to the
cryptohash internals, without the caller knowing about that. This
commit adds a new argument to pg_cryptohash_final() to allow such sanity
checks, and implements such defenses.
The internals of SCRAM for HMAC could be tightened a bit more, but as
everything is based on SCRAM_KEY_LEN with uses particular to this code
there is no need to complicate its interface more than necessary, and
this comes back to the refactoring of HMAC in core. Except that, this
minimizes the uses of the existing DIGEST_LENGTH variables, relying
instead on sizeof() for the result sizes. In ossp-uuid, this also makes
the code more defensive, as it already relied on dce_uuid_t being at
least the size of a MD5 digest.
This is in philosophy similar to cfc40d3 for base64.c and aef8948 for
hex.c.
Reported-by: Ranier Vilela
Author: Michael Paquier, Ranier Vilela
Reviewed-by: Kyotaro Horiguchi
Discussion: https://postgr.es/m/CAEudQAoqEGmcff3J4sTSV-R_16Monuz-UpJFbf_dnVH=APr02Q@mail.gmail.com
2021-02-15 02:18:34 +01:00
|
|
|
pg_cryptohash_final(ctx, outbuf, MD5_DIGEST_LENGTH) < 0)
|
Refactor MD5 implementations according to new cryptohash infrastructure
This commit heavily reorganizes the MD5 implementations that exist in
the tree in various aspects.
First, MD5 is added to the list of options available in cryptohash.c and
cryptohash_openssl.c. This means that if building with OpenSSL, EVP is
used for MD5 instead of the fallback implementation that Postgres had
for ages. With the recent refactoring work for cryptohash functions,
this change is straight-forward. If not building with OpenSSL, a
fallback implementation internal to src/common/ is used.
Second, this reduces the number of MD5 implementations present in the
tree from two to one, by moving the KAME implementation from pgcrypto to
src/common/, and by removing the implementation that existed in
src/common/. KAME was already structured with an init/update/final set
of routines by pgcrypto (see original pgcrypto/md5.h) for compatibility
with OpenSSL, so moving it to src/common/ has proved to be a
straight-forward move, requiring no actual manipulation of the internals
of each routine. Some benchmarking has not shown any performance gap
between both implementations.
Similarly to the fallback implementation used for SHA2, the fallback
implementation of MD5 is moved to src/common/md5.c with an internal
header called md5_int.h for the init, update and final routines. This
gets then consumed by cryptohash.c.
The original routines used for MD5-hashed passwords are moved to a
separate file called md5_common.c, also in src/common/, aimed at being
shared between all MD5 implementations as utility routines to keep
compatibility with any code relying on them.
Like the SHA2 changes, this commit had its round of tests on both Linux
and Windows, across all versions of OpenSSL supported on HEAD, with and
even without OpenSSL.
Author: Michael Paquier
Reviewed-by: Daniel Gustafsson
Discussion: https://postgr.es/m/20201106073434.GA4961@paquier.xyz
2020-12-10 03:59:10 +01:00
|
|
|
{
|
|
|
|
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 (out of memory).
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
pg_md5_encrypt(const char *passwd, const char *salt, size_t salt_len,
|
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
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);
|
|
|
|
|
|
|
|
free(crypt_buf);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|