b6b438
From 290c078652ffcacd69b0b00ea5e5413515c5de22 Mon Sep 17 00:00:00 2001
b6b438
From: Andreas Schneider <asn@samba.org>
b6b438
Date: Mon, 18 Mar 2019 17:03:30 +0100
b6b438
Subject: [PATCH 005/187] lib:util: Use GnuTLS random number generator in
b6b438
 genrand.c
b6b438
b6b438
FIPS requires that a random number generator from a certified crypto
b6b438
library is used.
b6b438
b6b438
Signed-off-by: Andreas Schneider <asn@samba.org>
b6b438
Reviewed-by: Jeremy Allison <jra@samba.org>
b6b438
b6b438
Autobuild-User(master): Jeremy Allison <jra@samba.org>
b6b438
Autobuild-Date(master): Thu Jul 18 01:30:20 UTC 2019 on sn-devel-184
b6b438
b6b438
(cherry picked from commit 664eed2e926f8f572b81e6d7c8e09b7ccbafb908)
b6b438
---
b6b438
 lib/util/genrand.c     | 31 +++++++------------------------
b6b438
 lib/util/genrand.h     | 11 ++++-------
b6b438
 lib/util/wscript_build |  2 +-
b6b438
 3 files changed, 12 insertions(+), 32 deletions(-)
b6b438
b6b438
diff --git a/lib/util/genrand.c b/lib/util/genrand.c
b6b438
index a775535c49e..55997c3dd55 100644
b6b438
--- a/lib/util/genrand.c
b6b438
+++ b/lib/util/genrand.c
b6b438
@@ -20,35 +20,17 @@
b6b438
 */
b6b438
 
b6b438
 #include "replace.h"
b6b438
-#include "system/filesys.h"
b6b438
 #include "lib/util/genrand.h"
b6b438
-#include "sys_rw_data.h"
b6b438
-#include "lib/util/blocking.h"
b6b438
 
b6b438
-static int urand_fd = -1;
b6b438
+#include <gnutls/gnutls.h>
b6b438
+#include <gnutls/crypto.h>
b6b438
 
b6b438
-static void open_urandom(void)
b6b438
-{
b6b438
-	if (urand_fd != -1) {
b6b438
-		return;
b6b438
-	}
b6b438
-	urand_fd = open( "/dev/urandom", O_RDONLY,0);
b6b438
-	if (urand_fd == -1) {
b6b438
-		abort();
b6b438
-	}
b6b438
-	smb_set_close_on_exec(urand_fd);
b6b438
-}
b6b438
+/* TODO: Add API for generating nonce or use gnutls_rnd directly everywhere. */
b6b438
 
b6b438
 _PUBLIC_ void generate_random_buffer(uint8_t *out, int len)
b6b438
 {
b6b438
-	ssize_t rw_ret;
b6b438
-
b6b438
-	open_urandom();
b6b438
-
b6b438
-	rw_ret = read_data(urand_fd, out, len);
b6b438
-	if (rw_ret != len) {
b6b438
-		abort();
b6b438
-	}
b6b438
+	/* Thread and fork safe random number generator for temporary keys. */
b6b438
+	gnutls_rnd(GNUTLS_RND_RANDOM, out, len);
b6b438
 }
b6b438
 
b6b438
 /*
b6b438
@@ -57,5 +39,6 @@ _PUBLIC_ void generate_random_buffer(uint8_t *out, int len)
b6b438
  */
b6b438
 _PUBLIC_ void generate_secret_buffer(uint8_t *out, int len)
b6b438
 {
b6b438
-	generate_random_buffer(out, len);
b6b438
+	/* Thread and fork safe random number generator for long term keys. */
b6b438
+	gnutls_rnd(GNUTLS_RND_KEY, out, len);
b6b438
 }
b6b438
diff --git a/lib/util/genrand.h b/lib/util/genrand.h
b6b438
index ef6bbc64157..899ce8badc0 100644
b6b438
--- a/lib/util/genrand.h
b6b438
+++ b/lib/util/genrand.h
b6b438
@@ -20,14 +20,11 @@
b6b438
 */
b6b438
 
b6b438
 /**
b6b438
- Interface to the (hopefully) good crypto random number generator.
b6b438
- Will use our internal PRNG if more than 40 bytes of random generation
b6b438
- has been requested, otherwise tries to read from /dev/random
b6b438
-**/
b6b438
+ * Thread and fork safe random number generator for temporary keys.
b6b438
+ */
b6b438
 void generate_random_buffer(uint8_t *out, int len);
b6b438
 
b6b438
 /**
b6b438
- Interface to the (hopefully) good crypto random number generator.
b6b438
- Will always use /dev/urandom if available.
b6b438
-**/
b6b438
+ * Thread and fork safe random number generator for long term keys.
b6b438
+ */
b6b438
 void generate_secret_buffer(uint8_t *out, int len);
b6b438
diff --git a/lib/util/wscript_build b/lib/util/wscript_build
b6b438
index ff1c76e3686..5f005c41e49 100644
b6b438
--- a/lib/util/wscript_build
b6b438
+++ b/lib/util/wscript_build
b6b438
@@ -104,7 +104,7 @@ else:
b6b438
 
b6b438
     bld.SAMBA_LIBRARY('genrand',
b6b438
                       source='genrand.c',
b6b438
-                      deps='replace socket-blocking sys_rw',
b6b438
+                      deps='replace gnutls',
b6b438
                       local_include=False,
b6b438
                       private_library=True)
b6b438
 
b6b438
-- 
b6b438
2.23.0
b6b438