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

b3353e
diff -up openssl-1.1.1i/crypto/fips/fips.c.fips-post-rand openssl-1.1.1i/crypto/fips/fips.c
b3353e
--- openssl-1.1.1i/crypto/fips/fips.c.fips-post-rand	2020-12-09 10:26:41.634106328 +0100
b3353e
+++ openssl-1.1.1i/crypto/fips/fips.c	2020-12-09 10:26:41.652106475 +0100
acdedc
@@ -68,6 +68,7 @@
acdedc
 
acdedc
 # include <openssl/fips.h>
acdedc
 # include "internal/thread_once.h"
7d1228
+# include "crypto/rand.h"
acdedc
 
acdedc
 # ifndef PATH_MAX
acdedc
 #  define PATH_MAX 1024
acdedc
@@ -76,6 +77,7 @@
acdedc
 static int fips_selftest_fail = 0;
acdedc
 static int fips_mode = 0;
acdedc
 static int fips_started = 0;
acdedc
+static int fips_post = 0;
acdedc
 
acdedc
 static int fips_is_owning_thread(void);
acdedc
 static int fips_set_owning_thread(void);
acdedc
@@ -158,6 +160,11 @@ void fips_set_selftest_fail(void)
acdedc
     fips_selftest_fail = 1;
acdedc
 }
acdedc
 
acdedc
+int fips_in_post(void)
acdedc
+{
acdedc
+    return fips_post;
acdedc
+}
acdedc
+
acdedc
 /* we implement what libfipscheck does ourselves */
acdedc
 
acdedc
 static int
acdedc
@@ -445,6 +452,8 @@ int FIPS_module_mode_set(int onoff)
acdedc
         }
acdedc
 # endif
acdedc
 
acdedc
+        fips_post = 1;
acdedc
+
acdedc
         if (!FIPS_selftest()) {
acdedc
             fips_selftest_fail = 1;
acdedc
             ret = 0;
acdedc
@@ -459,7 +468,12 @@ int FIPS_module_mode_set(int onoff)
acdedc
             goto end;
acdedc
         }
acdedc
 
acdedc
+        fips_post = 0;
acdedc
+
acdedc
         fips_set_mode(onoff);
acdedc
+        /* force RNG reseed with entropy from getrandom() on next call */
7d1228
+        rand_force_reseed();
acdedc
+
acdedc
         ret = 1;
acdedc
         goto end;
acdedc
     }
b3353e
diff -up openssl-1.1.1i/crypto/rand/drbg_lib.c.fips-post-rand openssl-1.1.1i/crypto/rand/drbg_lib.c
b3353e
--- openssl-1.1.1i/crypto/rand/drbg_lib.c.fips-post-rand	2020-12-08 14:20:59.000000000 +0100
b3353e
+++ openssl-1.1.1i/crypto/rand/drbg_lib.c	2020-12-09 10:26:41.652106475 +0100
b3353e
@@ -1005,6 +1005,20 @@ size_t rand_drbg_seedlen(RAND_DRBG *drbg
7d1228
     return min_entropy > min_entropylen ? min_entropy : min_entropylen;
7d1228
 }
acdedc
 
7d1228
+void rand_force_reseed(void)
7d1228
+{
7d1228
+    RAND_DRBG *drbg;
acdedc
+
7d1228
+    drbg = RAND_DRBG_get0_master();
7d1228
+    drbg->fork_id = 0;
7d1228
+
7d1228
+    drbg = RAND_DRBG_get0_private();
7d1228
+    drbg->fork_id = 0;
7d1228
+
7d1228
+    drbg = RAND_DRBG_get0_public();
7d1228
+    drbg->fork_id = 0;
7d1228
+}
7d1228
+
7d1228
 /* Implements the default OpenSSL RAND_add() method */
7d1228
 static int drbg_add(const void *buf, int num, double randomness)
7d1228
 {
b3353e
diff -up openssl-1.1.1i/crypto/rand/rand_unix.c.fips-post-rand openssl-1.1.1i/crypto/rand/rand_unix.c
b3353e
--- openssl-1.1.1i/crypto/rand/rand_unix.c.fips-post-rand	2020-12-08 14:20:59.000000000 +0100
b3353e
+++ openssl-1.1.1i/crypto/rand/rand_unix.c	2020-12-09 10:36:59.531221903 +0100
7d1228
@@ -17,10 +17,12 @@
7d1228
 #include <openssl/crypto.h>
7d1228
 #include "rand_local.h"
7d1228
 #include "crypto/rand.h"
7d1228
+#include "crypto/fips.h"
acdedc
 #include <stdio.h>
acdedc
 #include "internal/dso.h"
7d1228
 #ifdef __linux
7d1228
 # include <sys/syscall.h>
acdedc
+# include <sys/random.h>
7d1228
 # ifdef DEVRANDOM_WAIT
7d1228
 #  include <sys/shm.h>
7d1228
 #  include <sys/utsname.h>
b3353e
@@ -344,7 +346,7 @@ static ssize_t sysctl_random(char *buf,
acdedc
  * syscall_random(): Try to get random data using a system call
acdedc
  * returns the number of bytes returned in buf, or < 0 on error.
acdedc
  */
acdedc
-static ssize_t syscall_random(void *buf, size_t buflen)
acdedc
+static ssize_t syscall_random(void *buf, size_t buflen, int nonblock)
acdedc
 {
acdedc
     /*
acdedc
      * Note: 'buflen' equals the size of the buffer which is used by the
b3353e
@@ -369,6 +371,7 @@ static ssize_t syscall_random(void *buf,
b3353e
      * Note: Sometimes getentropy() can be provided but not implemented
b3353e
      * internally. So we need to check errno for ENOSYS
acdedc
      */
acdedc
+#  if 0
acdedc
 #  if defined(__GNUC__) && __GNUC__>=2 && defined(__ELF__) && !defined(__hpux)
acdedc
     extern int getentropy(void *buffer, size_t length) __attribute__((weak));
acdedc
 
b3353e
@@ -394,10 +397,10 @@ static ssize_t syscall_random(void *buf,
acdedc
     if (p_getentropy.p != NULL)
acdedc
         return p_getentropy.f(buf, buflen) == 0 ? (ssize_t)buflen : -1;
acdedc
 #  endif
acdedc
-
acdedc
+#  endif
acdedc
     /* Linux supports this since version 3.17 */
acdedc
-#  if defined(__linux) && defined(__NR_getrandom)
acdedc
-    return syscall(__NR_getrandom, buf, buflen, 0);
acdedc
+#  if defined(__linux) && defined(SYS_getrandom)
acdedc
+    return syscall(SYS_getrandom, buf, buflen, nonblock?GRND_NONBLOCK:0);
acdedc
 #  elif (defined(__FreeBSD__) || defined(__NetBSD__)) && defined(KERN_ARND)
acdedc
     return sysctl_random(buf, buflen);
acdedc
 #  else
b3353e
@@ -633,6 +636,9 @@ size_t rand_pool_acquire_entropy(RAND_PO
7d1228
     size_t entropy_available;
7d1228
 
acdedc
 #   if defined(OPENSSL_RAND_SEED_GETRANDOM)
acdedc
+    int in_post;
acdedc
+
acdedc
+    for (in_post = fips_in_post(); in_post >= 0; --in_post) {
acdedc
     {
7d1228
         size_t bytes_needed;
7d1228
         unsigned char *buffer;
b3353e
@@ -643,7 +649,7 @@ size_t rand_pool_acquire_entropy(RAND_PO
acdedc
         bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
acdedc
         while (bytes_needed != 0 && attempts-- > 0) {
acdedc
             buffer = rand_pool_add_begin(pool, bytes_needed);
acdedc
-            bytes = syscall_random(buffer, bytes_needed);
acdedc
+            bytes = syscall_random(buffer, bytes_needed, in_post);
acdedc
             if (bytes > 0) {
acdedc
                 rand_pool_add_end(pool, bytes, 8 * bytes);
acdedc
                 bytes_needed -= bytes;
b3353e
@@ -678,8 +684,10 @@ size_t rand_pool_acquire_entropy(RAND_PO
acdedc
             int attempts = 3;
acdedc
             const int fd = get_random_device(i);
acdedc
 
acdedc
-            if (fd == -1)
acdedc
+            if (fd == -1) {
acdedc
+                OPENSSL_showfatal("Random device %s cannot be opened.\n", random_device_paths[i]);
acdedc
                 continue;
acdedc
+            }
acdedc
 
acdedc
             while (bytes_needed != 0 && attempts-- > 0) {
acdedc
                 buffer = rand_pool_add_begin(pool, bytes_needed);
b3353e
@@ -742,7 +750,9 @@ size_t rand_pool_acquire_entropy(RAND_PO
7d1228
             return entropy_available;
acdedc
     }
acdedc
 #   endif
acdedc
-
acdedc
+#   ifdef OPENSSL_RAND_SEED_GETRANDOM
acdedc
+    }
acdedc
+#   endif
acdedc
     return rand_pool_entropy_available(pool);
acdedc
 #  endif
acdedc
 }
b3353e
diff -up openssl-1.1.1i/include/crypto/fips.h.fips-post-rand openssl-1.1.1i/include/crypto/fips.h
b3353e
--- openssl-1.1.1i/include/crypto/fips.h.fips-post-rand	2020-12-09 10:26:41.639106369 +0100
b3353e
+++ openssl-1.1.1i/include/crypto/fips.h	2020-12-09 10:26:41.657106516 +0100
7d1228
@@ -77,6 +77,8 @@ int FIPS_selftest_hmac(void);
7d1228
 int FIPS_selftest_drbg(void);
7d1228
 int FIPS_selftest_cmac(void);
7d1228
 
7d1228
+int fips_in_post(void);
7d1228
+
7d1228
 int fips_pkey_signature_test(EVP_PKEY *pkey,
7d1228
                                  const unsigned char *tbs, int tbslen,
7d1228
                                  const unsigned char *kat,
b3353e
diff -up openssl-1.1.1i/include/crypto/rand.h.fips-post-rand openssl-1.1.1i/include/crypto/rand.h
b3353e
--- openssl-1.1.1i/include/crypto/rand.h.fips-post-rand	2020-12-08 14:20:59.000000000 +0100
b3353e
+++ openssl-1.1.1i/include/crypto/rand.h	2020-12-09 10:26:41.657106516 +0100
7d1228
@@ -24,6 +24,7 @@
7d1228
 typedef struct rand_pool_st RAND_POOL;
7d1228
 
7d1228
 void rand_cleanup_int(void);
7d1228
+void rand_force_reseed(void);
7d1228
 void rand_drbg_cleanup_int(void);
7d1228
 void drbg_delete_thread_state(void);
7d1228