mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-08-25 06:47:19 +02:00
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
63 lines
2.5 KiB
Plaintext
63 lines
2.5 KiB
Plaintext
--
|
|
-- PGP Public Key Encryption
|
|
--
|
|
-- ensure consistent test output regardless of the default bytea format
|
|
SET bytea_output TO escape;
|
|
-- successful encrypt/decrypt
|
|
select pgp_pub_decrypt(
|
|
pgp_pub_encrypt('Secret msg', dearmor(pubkey)),
|
|
dearmor(seckey))
|
|
from keytbl where keytbl.id=1;
|
|
ERROR: pg_random_bytes() is not supported by this build
|
|
DETAIL: This functionality requires a source of strong random numbers
|
|
HINT: You need to rebuild PostgreSQL using --enable-strong-random
|
|
select pgp_pub_decrypt(
|
|
pgp_pub_encrypt('Secret msg', dearmor(pubkey)),
|
|
dearmor(seckey))
|
|
from keytbl where keytbl.id=2;
|
|
ERROR: pg_random_bytes() is not supported by this build
|
|
DETAIL: This functionality requires a source of strong random numbers
|
|
HINT: You need to rebuild PostgreSQL using --enable-strong-random
|
|
select pgp_pub_decrypt(
|
|
pgp_pub_encrypt('Secret msg', dearmor(pubkey)),
|
|
dearmor(seckey))
|
|
from keytbl where keytbl.id=3;
|
|
ERROR: pg_random_bytes() is not supported by this build
|
|
DETAIL: This functionality requires a source of strong random numbers
|
|
HINT: You need to rebuild PostgreSQL using --enable-strong-random
|
|
select pgp_pub_decrypt(
|
|
pgp_pub_encrypt('Secret msg', dearmor(pubkey)),
|
|
dearmor(seckey))
|
|
from keytbl where keytbl.id=6;
|
|
ERROR: pg_random_bytes() is not supported by this build
|
|
DETAIL: This functionality requires a source of strong random numbers
|
|
HINT: You need to rebuild PostgreSQL using --enable-strong-random
|
|
-- try with rsa-sign only
|
|
select pgp_pub_decrypt(
|
|
pgp_pub_encrypt('Secret msg', dearmor(pubkey)),
|
|
dearmor(seckey))
|
|
from keytbl where keytbl.id=4;
|
|
ERROR: No encryption key found
|
|
-- try with secret key
|
|
select pgp_pub_decrypt(
|
|
pgp_pub_encrypt('Secret msg', dearmor(seckey)),
|
|
dearmor(seckey))
|
|
from keytbl where keytbl.id=1;
|
|
ERROR: Refusing to encrypt with secret key
|
|
-- does text-to-bytea works
|
|
select pgp_pub_decrypt_bytea(
|
|
pgp_pub_encrypt('Secret msg', dearmor(pubkey)),
|
|
dearmor(seckey))
|
|
from keytbl where keytbl.id=1;
|
|
ERROR: pg_random_bytes() is not supported by this build
|
|
DETAIL: This functionality requires a source of strong random numbers
|
|
HINT: You need to rebuild PostgreSQL using --enable-strong-random
|
|
-- and bytea-to-text?
|
|
select pgp_pub_decrypt(
|
|
pgp_pub_encrypt_bytea('Secret msg', dearmor(pubkey)),
|
|
dearmor(seckey))
|
|
from keytbl where keytbl.id=1;
|
|
ERROR: pg_random_bytes() is not supported by this build
|
|
DETAIL: This functionality requires a source of strong random numbers
|
|
HINT: You need to rebuild PostgreSQL using --enable-strong-random
|