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

da7b65
diff -up openssl-1.1.1i/crypto/fips/fips.c.fips-post-rand openssl-1.1.1i/crypto/fips/fips.c
da7b65
--- openssl-1.1.1i/crypto/fips/fips.c.fips-post-rand	2020-12-09 10:26:41.634106328 +0100
da7b65
+++ openssl-1.1.1i/crypto/fips/fips.c	2020-12-09 10:26:41.652106475 +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
     }
da7b65
diff -up openssl-1.1.1i/crypto/rand/drbg_lib.c.fips-post-rand openssl-1.1.1i/crypto/rand/drbg_lib.c
da7b65
--- openssl-1.1.1i/crypto/rand/drbg_lib.c.fips-post-rand	2020-12-08 14:20:59.000000000 +0100
da7b65
+++ openssl-1.1.1i/crypto/rand/drbg_lib.c	2020-12-09 10:26:41.652106475 +0100
da7b65
@@ -1005,6 +1005,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
 {
da7b65
diff -up openssl-1.1.1i/crypto/rand/rand_unix.c.fips-post-rand openssl-1.1.1i/crypto/rand/rand_unix.c
da7b65
--- openssl-1.1.1i/crypto/rand/rand_unix.c.fips-post-rand	2020-12-08 14:20:59.000000000 +0100
da7b65
+++ openssl-1.1.1i/crypto/rand/rand_unix.c	2020-12-09 10:36:59.531221903 +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>
da7b65
@@ -344,7 +346,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
da7b65
@@ -369,6 +371,7 @@ static ssize_t syscall_random(void *buf,
da7b65
      * Note: Sometimes getentropy() can be provided but not implemented
da7b65
      * internally. So we need to check errno for ENOSYS
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
 
da7b65
@@ -394,10 +397,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
da7b65
@@ -633,6 +636,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;
da7b65
@@ -643,7 +649,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;
da7b65
@@ -678,8 +684,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);
da7b65
@@ -742,7 +750,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
 }
da7b65
diff -up openssl-1.1.1i/include/crypto/fips.h.fips-post-rand openssl-1.1.1i/include/crypto/fips.h
da7b65
--- openssl-1.1.1i/include/crypto/fips.h.fips-post-rand	2020-12-09 10:26:41.639106369 +0100
da7b65
+++ openssl-1.1.1i/include/crypto/fips.h	2020-12-09 10:26:41.657106516 +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,
da7b65
diff -up openssl-1.1.1i/include/crypto/rand.h.fips-post-rand openssl-1.1.1i/include/crypto/rand.h
da7b65
--- openssl-1.1.1i/include/crypto/rand.h.fips-post-rand	2020-12-08 14:20:59.000000000 +0100
da7b65
+++ openssl-1.1.1i/include/crypto/rand.h	2020-12-09 10:26:41.657106516 +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