Blame SOURCES/openssl-1.1.1-fips-post-rand.patch

c95581
diff -up openssl-1.1.1e/crypto/fips/fips.c.fips-post-rand openssl-1.1.1e/crypto/fips/fips.c
c95581
--- openssl-1.1.1e/crypto/fips/fips.c.fips-post-rand	2020-03-17 18:06:16.822418854 +0100
c95581
+++ openssl-1.1.1e/crypto/fips/fips.c	2020-03-17 18:06:16.861418172 +0100
3a273b
@@ -68,6 +68,7 @@
3a273b
 
3a273b
 # include <openssl/fips.h>
3a273b
 # include "internal/thread_once.h"
c95581
+# include "crypto/rand.h"
3a273b
 
3a273b
 # ifndef PATH_MAX
3a273b
 #  define PATH_MAX 1024
3a273b
@@ -76,6 +77,7 @@
3a273b
 static int fips_selftest_fail = 0;
3a273b
 static int fips_mode = 0;
3a273b
 static int fips_started = 0;
3a273b
+static int fips_post = 0;
3a273b
 
3a273b
 static int fips_is_owning_thread(void);
3a273b
 static int fips_set_owning_thread(void);
3a273b
@@ -158,6 +160,11 @@ void fips_set_selftest_fail(void)
3a273b
     fips_selftest_fail = 1;
3a273b
 }
3a273b
 
3a273b
+int fips_in_post(void)
3a273b
+{
3a273b
+    return fips_post;
3a273b
+}
3a273b
+
3a273b
 /* we implement what libfipscheck does ourselves */
3a273b
 
3a273b
 static int
3a273b
@@ -445,6 +452,8 @@ int FIPS_module_mode_set(int onoff)
3a273b
         }
3a273b
 # endif
3a273b
 
3a273b
+        fips_post = 1;
3a273b
+
3a273b
         if (!FIPS_selftest()) {
3a273b
             fips_selftest_fail = 1;
3a273b
             ret = 0;
3a273b
@@ -459,7 +468,12 @@ int FIPS_module_mode_set(int onoff)
3a273b
             goto end;
3a273b
         }
3a273b
 
3a273b
+        fips_post = 0;
3a273b
+
3a273b
         fips_set_mode(onoff);
3a273b
+        /* force RNG reseed with entropy from getrandom() on next call */
c95581
+        rand_force_reseed();
3a273b
+
3a273b
         ret = 1;
3a273b
         goto end;
3a273b
     }
c95581
diff -up openssl-1.1.1e/crypto/rand/drbg_lib.c.fips-post-rand openssl-1.1.1e/crypto/rand/drbg_lib.c
c95581
--- openssl-1.1.1e/crypto/rand/drbg_lib.c.fips-post-rand	2020-03-17 15:31:17.000000000 +0100
c95581
+++ openssl-1.1.1e/crypto/rand/drbg_lib.c	2020-03-17 18:07:35.305045521 +0100
c95581
@@ -1009,6 +1009,20 @@ size_t rand_drbg_seedlen(RAND_DRBG *drbg
c95581
     return min_entropy > min_entropylen ? min_entropy : min_entropylen;
c95581
 }
3a273b
 
c95581
+void rand_force_reseed(void)
c95581
+{
c95581
+    RAND_DRBG *drbg;
3a273b
+
c95581
+    drbg = RAND_DRBG_get0_master();
c95581
+    drbg->fork_id = 0;
c95581
+
c95581
+    drbg = RAND_DRBG_get0_private();
c95581
+    drbg->fork_id = 0;
c95581
+
c95581
+    drbg = RAND_DRBG_get0_public();
c95581
+    drbg->fork_id = 0;
c95581
+}
c95581
+
c95581
 /* Implements the default OpenSSL RAND_add() method */
c95581
 static int drbg_add(const void *buf, int num, double randomness)
c95581
 {
c95581
diff -up openssl-1.1.1e/crypto/rand/rand_unix.c.fips-post-rand openssl-1.1.1e/crypto/rand/rand_unix.c
c95581
--- openssl-1.1.1e/crypto/rand/rand_unix.c.fips-post-rand	2020-03-17 15:31:17.000000000 +0100
c95581
+++ openssl-1.1.1e/crypto/rand/rand_unix.c	2020-03-17 18:09:01.503537189 +0100
c95581
@@ -17,10 +17,12 @@
c95581
 #include <openssl/crypto.h>
c95581
 #include "rand_local.h"
c95581
 #include "crypto/rand.h"
c95581
+#include "crypto/fips.h"
3a273b
 #include <stdio.h>
3a273b
 #include "internal/dso.h"
c95581
 #ifdef __linux
c95581
 # include <sys/syscall.h>
3a273b
+# include <sys/random.h>
c95581
 # ifdef DEVRANDOM_WAIT
c95581
 #  include <sys/shm.h>
c95581
 #  include <sys/utsname.h>
c95581
@@ -342,7 +344,7 @@ static ssize_t sysctl_random(char *buf,
3a273b
  * syscall_random(): Try to get random data using a system call
3a273b
  * returns the number of bytes returned in buf, or < 0 on error.
3a273b
  */
3a273b
-static ssize_t syscall_random(void *buf, size_t buflen)
3a273b
+static ssize_t syscall_random(void *buf, size_t buflen, int nonblock)
3a273b
 {
3a273b
     /*
3a273b
      * Note: 'buflen' equals the size of the buffer which is used by the
c95581
@@ -364,6 +366,7 @@ static ssize_t syscall_random(void *buf,
3a273b
      * - Linux since 3.17 with glibc 2.25
3a273b
      * - FreeBSD since 12.0 (1200061)
3a273b
      */
3a273b
+#  if 0
3a273b
 #  if defined(__GNUC__) && __GNUC__>=2 && defined(__ELF__) && !defined(__hpux)
3a273b
     extern int getentropy(void *buffer, size_t length) __attribute__((weak));
3a273b
 
c95581
@@ -385,10 +388,10 @@ static ssize_t syscall_random(void *buf,
3a273b
     if (p_getentropy.p != NULL)
3a273b
         return p_getentropy.f(buf, buflen) == 0 ? (ssize_t)buflen : -1;
3a273b
 #  endif
3a273b
-
3a273b
+#  endif
3a273b
     /* Linux supports this since version 3.17 */
3a273b
-#  if defined(__linux) && defined(__NR_getrandom)
3a273b
-    return syscall(__NR_getrandom, buf, buflen, 0);
3a273b
+#  if defined(__linux) && defined(SYS_getrandom)
3a273b
+    return syscall(SYS_getrandom, buf, buflen, nonblock?GRND_NONBLOCK:0);
3a273b
 #  elif (defined(__FreeBSD__) || defined(__NetBSD__)) && defined(KERN_ARND)
3a273b
     return sysctl_random(buf, buflen);
3a273b
 #  else
c95581
@@ -623,6 +626,9 @@ size_t rand_pool_acquire_entropy(RAND_PO
c95581
     size_t entropy_available;
c95581
 
3a273b
 #   if defined(OPENSSL_RAND_SEED_GETRANDOM)
3a273b
+    int in_post;
3a273b
+
3a273b
+    for (in_post = fips_in_post(); in_post >= 0; --in_post) {
3a273b
     {
c95581
         size_t bytes_needed;
c95581
         unsigned char *buffer;
c95581
@@ -633,7 +639,7 @@ size_t rand_pool_acquire_entropy(RAND_PO
3a273b
         bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
3a273b
         while (bytes_needed != 0 && attempts-- > 0) {
3a273b
             buffer = rand_pool_add_begin(pool, bytes_needed);
3a273b
-            bytes = syscall_random(buffer, bytes_needed);
3a273b
+            bytes = syscall_random(buffer, bytes_needed, in_post);
3a273b
             if (bytes > 0) {
3a273b
                 rand_pool_add_end(pool, bytes, 8 * bytes);
3a273b
                 bytes_needed -= bytes;
c95581
@@ -668,8 +674,10 @@ size_t rand_pool_acquire_entropy(RAND_PO
3a273b
             int attempts = 3;
3a273b
             const int fd = get_random_device(i);
3a273b
 
3a273b
-            if (fd == -1)
3a273b
+            if (fd == -1) {
3a273b
+                OPENSSL_showfatal("Random device %s cannot be opened.\n", random_device_paths[i]);
3a273b
                 continue;
3a273b
+            }
3a273b
 
3a273b
             while (bytes_needed != 0 && attempts-- > 0) {
3a273b
                 buffer = rand_pool_add_begin(pool, bytes_needed);
c95581
@@ -732,7 +740,9 @@ size_t rand_pool_acquire_entropy(RAND_PO
c95581
             return entropy_available;
3a273b
     }
3a273b
 #   endif
3a273b
-
3a273b
+#   ifdef OPENSSL_RAND_SEED_GETRANDOM
3a273b
+    }
3a273b
+#   endif
3a273b
     return rand_pool_entropy_available(pool);
3a273b
 #  endif
3a273b
 }
c95581
diff -up openssl-1.1.1e/include/crypto/fips.h.fips-post-rand openssl-1.1.1e/include/crypto/fips.h
c95581
--- openssl-1.1.1e/include/crypto/fips.h.fips-post-rand	2020-03-17 18:06:16.831418696 +0100
c95581
+++ openssl-1.1.1e/include/crypto/fips.h	2020-03-17 18:06:16.861418172 +0100
c95581
@@ -77,6 +77,8 @@ int FIPS_selftest_hmac(void);
c95581
 int FIPS_selftest_drbg(void);
c95581
 int FIPS_selftest_cmac(void);
c95581
 
c95581
+int fips_in_post(void);
c95581
+
c95581
 int fips_pkey_signature_test(EVP_PKEY *pkey,
c95581
                                  const unsigned char *tbs, int tbslen,
c95581
                                  const unsigned char *kat,
c95581
diff -up openssl-1.1.1e/include/crypto/rand.h.fips-post-rand openssl-1.1.1e/include/crypto/rand.h
c95581
--- openssl-1.1.1e/include/crypto/rand.h.fips-post-rand	2020-03-17 15:31:17.000000000 +0100
c95581
+++ openssl-1.1.1e/include/crypto/rand.h	2020-03-17 18:07:35.303045555 +0100
c95581
@@ -24,6 +24,7 @@
c95581
 typedef struct rand_pool_st RAND_POOL;
c95581
 
c95581
 void rand_cleanup_int(void);
c95581
+void rand_force_reseed(void);
c95581
 void rand_drbg_cleanup_int(void);
c95581
 void drbg_delete_thread_state(void);
c95581