From 60f57667edc89f2c95c3d99882c29d15b16903dd Mon Sep 17 00:00:00 2001 From: Daniel Gustafsson Date: Wed, 28 Oct 2020 11:24:02 +0100 Subject: [PATCH v24 3/6] NSS pg_strong_random support --- configure | 1 + configure.ac | 1 + src/include/pg_config.h.in | 3 +++ src/port/pg_strong_random.c | 50 ++++++++++++++++++++++++++++++++++++- 4 files changed, 54 insertions(+), 1 deletion(-) diff --git a/configure b/configure index 143726c79c..2b83914289 100755 --- a/configure +++ b/configure @@ -18422,6 +18422,7 @@ $as_echo "NSS" >&6; } elif test x"$PORTNAME" = x"win32" ; then { $as_echo "$as_me:${as_lineno-$LINENO}: result: Windows native" >&5 $as_echo "Windows native" >&6; } + else { $as_echo "$as_me:${as_lineno-$LINENO}: result: /dev/urandom" >&5 $as_echo "/dev/urandom" >&6; } diff --git a/configure.ac b/configure.ac index 59657a6010..6533e10ffd 100644 --- a/configure.ac +++ b/configure.ac @@ -2197,6 +2197,7 @@ elif test x"$with_ssl" = x"nss" ; then AC_MSG_RESULT([NSS]) elif test x"$PORTNAME" = x"win32" ; then AC_MSG_RESULT([Windows native]) + else AC_MSG_RESULT([/dev/urandom]) AC_CHECK_FILE([/dev/urandom], [], []) diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in index 8856a33590..fa5a977617 100644 --- a/src/include/pg_config.h.in +++ b/src/include/pg_config.h.in @@ -908,6 +908,9 @@ /* Define to build with NSS support (--with-ssl=nss) */ #undef USE_NSS +/* Define to use NSS for random number generation */ +#undef USE_NSS_RANDOM + /* Define to 1 to use software CRC-32C implementation (slicing-by-8). */ #undef USE_SLICING_BY_8_CRC32C diff --git a/src/port/pg_strong_random.c b/src/port/pg_strong_random.c index 07f24c0089..40de237628 100644 --- a/src/port/pg_strong_random.c +++ b/src/port/pg_strong_random.c @@ -137,7 +137,55 @@ pg_strong_random(void *buf, size_t len) return false; } -#else /* not USE_OPENSSL or WIN32 */ +#elif defined(USE_NSS) + +#define pg_BITS_PER_BYTE BITS_PER_BYTE +#undef BITS_PER_BYTE +#define NO_NSPR_10_SUPPORT +#include +#include +#if defined(BITS_PER_BYTE) +#if BITS_PER_BYTE != pg_BITS_PER_BYTE +#error "incompatible byte widths between NSPR and postgres" +#endif +#else +#define BITS_PER_BYTE pg_BITS_PER_BYTE +#endif +#undef pg_BITS_PER_BYTE + +void +pg_strong_random_init(void) +{ + /* No initialization needed on NSS */ +} + +bool +pg_strong_random(void *buf, size_t len) +{ + NSSInitParameters params; + NSSInitContext *nss_context; + SECStatus status; + + memset(¶ms, 0, sizeof(params)); + params.length = sizeof(params); + nss_context = NSS_InitContext("", "", "", "", ¶ms, + NSS_INIT_READONLY | NSS_INIT_NOCERTDB | + NSS_INIT_NOMODDB | NSS_INIT_FORCEOPEN | + NSS_INIT_NOROOTINIT | NSS_INIT_PK11RELOAD); + + if (!nss_context) + return false; + + status = PK11_GenerateRandom(buf, len); + NSS_ShutdownContext(nss_context); + + if (status == SECSuccess) + return true; + + return false; +} + +#else /* not USE_OPENSSL, USE_NSS or WIN32 */ /* * Without OpenSSL or Win32 support, just read /dev/urandom ourselves. -- 2.21.1 (Apple Git-122.3)