v22-0003-NSS-pg_strong_random-support.patch
application/octet-stream
Filename: v22-0003-NSS-pg_strong_random-support.patch
Type: application/octet-stream
Part: 2
Patch
Format: format-patch
Series: patch v22-0003
Subject: NSS pg_strong_random support
| File | + | − |
|---|---|---|
| configure | 2 | 1 |
| configure.ac | 2 | 1 |
| src/include/pg_config.h.in | 3 | 0 |
| src/port/pg_strong_random.c | 49 | 1 |
From f8994dff2e9e49916a23e9fe66dfd0f9fd97841a Mon Sep 17 00:00:00 2001
From: Daniel Gustafsson <daniel@yesql.se>
Date: Wed, 28 Oct 2020 11:24:02 +0100
Subject: [PATCH v22 3/6] NSS pg_strong_random support
---
configure | 3 ++-
configure.ac | 3 ++-
src/include/pg_config.h.in | 3 +++
src/port/pg_strong_random.c | 50 ++++++++++++++++++++++++++++++++++++-
4 files changed, 56 insertions(+), 3 deletions(-)
diff --git a/configure b/configure
index cc6088b545..876a0ea839 100755
--- a/configure
+++ b/configure
@@ -18441,6 +18441,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; }
@@ -18467,7 +18468,7 @@ fi
if test x"$ac_cv_file__dev_urandom" = x"no" ; then
as_fn_error $? "
no source of strong random numbers was found
-PostgreSQL can use OpenSSL, native Windows API or /dev/urandom as a source of random numbers." "$LINENO" 5
+PostgreSQL can use OpenSSL, NSS, native Windows API or /dev/urandom as a source of random numbers." "$LINENO" 5
fi
fi
diff --git a/configure.ac b/configure.ac
index 0da8cc9b0b..6b6641a094 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2203,6 +2203,7 @@ elif test x"$with_nss" = x"yes" ; 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], [], [])
@@ -2210,7 +2211,7 @@ else
if test x"$ac_cv_file__dev_urandom" = x"no" ; then
AC_MSG_ERROR([
no source of strong random numbers was found
-PostgreSQL can use OpenSSL, native Windows API or /dev/urandom as a source of random numbers.])
+PostgreSQL can use OpenSSL, NSS, native Windows API or /dev/urandom as a source of random numbers.])
fi
fi
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index 67a9f90e5e..0894ff2bef 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-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 <nss/nss.h>
+#include <nss/pk11pub.h>
+#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)