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

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