mirror of
https://github.com/postgres/postgres.git
synced 2025-07-28 23:42:10 +03:00
Replace PostmasterRandom() with a stronger source, second attempt.
This adds a new routine, pg_strong_random() for generating random bytes, for use in both frontend and backend. At the moment, it's only used in the backend, but the upcoming SCRAM authentication patches need strong random numbers in libpq as well. pg_strong_random() is based on, and replaces, the existing implementation in pgcrypto. It can acquire strong random numbers from a number of sources, depending on what's available: - OpenSSL RAND_bytes(), if built with OpenSSL - On Windows, the native cryptographic functions are used - /dev/urandom Unlike the current pgcrypto function, the source is chosen by configure. That makes it easier to test different implementations, and ensures that we don't accidentally fall back to a less secure implementation, if the primary source fails. All of those methods are quite reliable, it would be pretty surprising for them to fail, so we'd rather find out by failing hard. If no strong random source is available, we fall back to using erand48(), seeded from current timestamp, like PostmasterRandom() was. That isn't cryptographically secure, but allows us to still work on platforms that don't have any of the above stronger sources. Because it's not very secure, the built-in implementation is only used if explicitly requested with --disable-strong-random. This replaces the more complicated Fortuna algorithm we used to have in pgcrypto, which is unfortunate, but all modern platforms have /dev/urandom, so it doesn't seem worth the maintenance effort to keep that. pgcrypto functions that require strong random numbers will be disabled with --disable-strong-random. Original patch by Magnus Hagander, tons of further work by Michael Paquier and me. Discussion: https://www.postgresql.org/message-id/CAB7nPqRy3krN8quR9XujMVVHYtXJ0_60nqgVc6oUk8ygyVkZsA@mail.gmail.com Discussion: https://www.postgresql.org/message-id/CAB7nPqRWkNYRRPJA7-cF+LfroYV10pvjdz6GNvxk-Eee9FypKA@mail.gmail.com
This commit is contained in:
@ -61,65 +61,6 @@ PG_FUNCTION_INFO_V1(pg_armor);
|
||||
PG_FUNCTION_INFO_V1(pg_dearmor);
|
||||
PG_FUNCTION_INFO_V1(pgp_armor_headers);
|
||||
|
||||
/*
|
||||
* Mix a block of data into RNG.
|
||||
*/
|
||||
static void
|
||||
add_block_entropy(PX_MD *md, text *data)
|
||||
{
|
||||
uint8 sha1[20];
|
||||
|
||||
px_md_reset(md);
|
||||
px_md_update(md, (uint8 *) VARDATA(data), VARSIZE(data) - VARHDRSZ);
|
||||
px_md_finish(md, sha1);
|
||||
|
||||
px_add_entropy(sha1, 20);
|
||||
|
||||
px_memset(sha1, 0, 20);
|
||||
}
|
||||
|
||||
/*
|
||||
* Mix user data into RNG. It is for user own interests to have
|
||||
* RNG state shuffled.
|
||||
*/
|
||||
static void
|
||||
add_entropy(text *data1, text *data2, text *data3)
|
||||
{
|
||||
PX_MD *md;
|
||||
uint8 rnd[3];
|
||||
|
||||
if (!data1 && !data2 && !data3)
|
||||
return;
|
||||
|
||||
if (px_get_random_bytes(rnd, 3) < 0)
|
||||
return;
|
||||
|
||||
if (px_find_digest("sha1", &md) < 0)
|
||||
return;
|
||||
|
||||
/*
|
||||
* Try to make the feeding unpredictable.
|
||||
*
|
||||
* Prefer data over keys, as it's rather likely that key is same in
|
||||
* several calls.
|
||||
*/
|
||||
|
||||
/* chance: 7/8 */
|
||||
if (data1 && rnd[0] >= 32)
|
||||
add_block_entropy(md, data1);
|
||||
|
||||
/* chance: 5/8 */
|
||||
if (data2 && rnd[1] >= 160)
|
||||
add_block_entropy(md, data2);
|
||||
|
||||
/* chance: 5/8 */
|
||||
if (data3 && rnd[2] >= 160)
|
||||
add_block_entropy(md, data3);
|
||||
|
||||
px_md_free(md);
|
||||
px_memset(rnd, 0, sizeof(rnd));
|
||||
}
|
||||
|
||||
/*
|
||||
* returns src in case of no conversion or error
|
||||
*/
|
||||
@ -432,11 +373,7 @@ init_work(PGP_Context **ctx_p, int is_text,
|
||||
VARSIZE(args) - VARHDRSZ, ex);
|
||||
|
||||
if (err)
|
||||
{
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
|
||||
errmsg("%s", px_strerror(err))));
|
||||
}
|
||||
px_THROW_ERROR(err);
|
||||
|
||||
if (ex->debug)
|
||||
px_set_debug_handler(show_debug);
|
||||
@ -459,11 +396,6 @@ encrypt_internal(int is_pubenc, int is_text,
|
||||
struct debug_expect ex;
|
||||
text *tmp_data = NULL;
|
||||
|
||||
/*
|
||||
* Add data and key info RNG.
|
||||
*/
|
||||
add_entropy(data, key, NULL);
|
||||
|
||||
init_work(&ctx, is_text, args, &ex);
|
||||
|
||||
if (is_text && pgp_get_unicode_mode(ctx))
|
||||
@ -516,9 +448,7 @@ encrypt_internal(int is_pubenc, int is_text,
|
||||
pgp_free(ctx);
|
||||
mbuf_free(src);
|
||||
mbuf_free(dst);
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
|
||||
errmsg("%s", px_strerror(err))));
|
||||
px_THROW_ERROR(err);
|
||||
}
|
||||
|
||||
/* res_len includes VARHDRSZ */
|
||||
@ -605,9 +535,7 @@ decrypt_internal(int is_pubenc, int need_text, text *data,
|
||||
{
|
||||
px_set_debug_handler(NULL);
|
||||
mbuf_free(dst);
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
|
||||
errmsg("%s", px_strerror(err))));
|
||||
px_THROW_ERROR(err);
|
||||
}
|
||||
|
||||
res_len = mbuf_steal_data(dst, &restmp);
|
||||
@ -629,11 +557,6 @@ decrypt_internal(int is_pubenc, int need_text, text *data,
|
||||
}
|
||||
px_set_debug_handler(NULL);
|
||||
|
||||
/*
|
||||
* add successful decryptions also into RNG
|
||||
*/
|
||||
add_entropy(res, key, keypsw);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
@ -985,9 +908,7 @@ pg_dearmor(PG_FUNCTION_ARGS)
|
||||
|
||||
ret = pgp_armor_decode((uint8 *) VARDATA(data), data_len, &buf);
|
||||
if (ret < 0)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
|
||||
errmsg("%s", px_strerror(ret))));
|
||||
px_THROW_ERROR(ret);
|
||||
res = palloc(VARHDRSZ + buf.len);
|
||||
SET_VARSIZE(res, VARHDRSZ + buf.len);
|
||||
memcpy(VARDATA(res), buf.data, buf.len);
|
||||
@ -1041,9 +962,7 @@ pgp_armor_headers(PG_FUNCTION_ARGS)
|
||||
&state->nheaders, &state->keys,
|
||||
&state->values);
|
||||
if (res < 0)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
|
||||
errmsg("%s", px_strerror(res))));
|
||||
px_THROW_ERROR(res);
|
||||
|
||||
MemoryContextSwitchTo(oldcontext);
|
||||
funcctx->user_fctx = state;
|
||||
@ -1092,9 +1011,7 @@ pgp_key_id_w(PG_FUNCTION_ARGS)
|
||||
res_len = pgp_get_keyid(buf, VARDATA(res));
|
||||
mbuf_free(buf);
|
||||
if (res_len < 0)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
|
||||
errmsg("%s", px_strerror(res_len))));
|
||||
px_THROW_ERROR(res_len);
|
||||
SET_VARSIZE(res, VARHDRSZ + res_len);
|
||||
|
||||
PG_FREE_IF_COPY(data, 0);
|
||||
|
Reference in New Issue
Block a user