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

e4b8d1
diff -up openssl-1.1.1/crypto/fips/fips.c.fips-post-rand openssl-1.1.1/crypto/fips/fips.c
e4b8d1
--- openssl-1.1.1/crypto/fips/fips.c.fips-post-rand	2018-10-12 17:40:50.631506976 +0200
e4b8d1
+++ openssl-1.1.1/crypto/fips/fips.c	2018-11-08 17:49:08.091064655 +0100
e4b8d1
@@ -68,6 +68,7 @@
e4b8d1
 
e4b8d1
 # include <openssl/fips.h>
e4b8d1
 # include "internal/thread_once.h"
e4b8d1
+# include "internal/rand_int.h"
e4b8d1
 
e4b8d1
 # ifndef PATH_MAX
e4b8d1
 #  define PATH_MAX 1024
e4b8d1
@@ -76,6 +77,7 @@
e4b8d1
 static int fips_selftest_fail = 0;
e4b8d1
 static int fips_mode = 0;
e4b8d1
 static int fips_started = 0;
e4b8d1
+static int fips_post = 0;
e4b8d1
 
e4b8d1
 static int fips_is_owning_thread(void);
e4b8d1
 static int fips_set_owning_thread(void);
e4b8d1
@@ -158,6 +160,11 @@ void fips_set_selftest_fail(void)
e4b8d1
     fips_selftest_fail = 1;
e4b8d1
 }
e4b8d1
 
e4b8d1
+int fips_in_post(void)
e4b8d1
+{
e4b8d1
+    return fips_post;
e4b8d1
+}
e4b8d1
+
e4b8d1
 /* we implement what libfipscheck does ourselves */
e4b8d1
 
e4b8d1
 static int
e4b8d1
@@ -445,6 +452,8 @@ int FIPS_module_mode_set(int onoff)
e4b8d1
         }
e4b8d1
 # endif
e4b8d1
 
e4b8d1
+        fips_post = 1;
e4b8d1
+
e4b8d1
         if (!FIPS_selftest()) {
e4b8d1
             fips_selftest_fail = 1;
e4b8d1
             ret = 0;
e4b8d1
@@ -459,7 +468,12 @@ int FIPS_module_mode_set(int onoff)
e4b8d1
             goto end;
e4b8d1
         }
e4b8d1
 
e4b8d1
+        fips_post = 0;
e4b8d1
+
e4b8d1
         fips_set_mode(onoff);
e4b8d1
+        /* force RNG reseed with entropy from getrandom() on next call */
e4b8d1
+        rand_fork();
e4b8d1
+
e4b8d1
         ret = 1;
e4b8d1
         goto end;
e4b8d1
     }
e4b8d1
diff -up openssl-1.1.1/crypto/include/internal/fips_int.h.fips-post-rand openssl-1.1.1/crypto/include/internal/fips_int.h
e4b8d1
--- openssl-1.1.1/crypto/include/internal/fips_int.h.fips-post-rand	2018-11-08 17:32:50.806526458 +0100
e4b8d1
+++ openssl-1.1.1/crypto/include/internal/fips_int.h	2018-11-08 17:32:20.533828167 +0100
e4b8d1
@@ -76,6 +76,8 @@ int FIPS_selftest_hmac(void);
e4b8d1
 int FIPS_selftest_drbg(void);
e4b8d1
 int FIPS_selftest_cmac(void);
e4b8d1
 
e4b8d1
+int fips_in_post(void);
e4b8d1
+
e4b8d1
 int fips_pkey_signature_test(EVP_PKEY *pkey,
e4b8d1
                                  const unsigned char *tbs, int tbslen,
e4b8d1
                                  const unsigned char *kat,
e4b8d1
diff -up openssl-1.1.1/crypto/rand/rand_unix.c.fips-post-rand openssl-1.1.1/crypto/rand/rand_unix.c
e4b8d1
--- openssl-1.1.1/crypto/rand/rand_unix.c.fips-post-rand	2018-09-11 14:48:21.000000000 +0200
e4b8d1
+++ openssl-1.1.1/crypto/rand/rand_unix.c	2018-11-09 14:03:48.504301170 +0100
e4b8d1
@@ -16,10 +16,12 @@
e4b8d1
 #include <openssl/rand.h>
e4b8d1
 #include "rand_lcl.h"
e4b8d1
 #include "internal/rand_int.h"
e4b8d1
+#include "internal/fips_int.h"
e4b8d1
 #include <stdio.h>
e4b8d1
 #include "internal/dso.h"
e4b8d1
 #if defined(__linux)
e4b8d1
 # include <sys/syscall.h>
e4b8d1
+# include <sys/random.h>
e4b8d1
 #endif
e4b8d1
 #if defined(__FreeBSD__)
e4b8d1
 # include <sys/types.h>
e4b8d1
@@ -86,7 +88,7 @@ static uint64_t get_timer_bits(void);
e4b8d1
     || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_VXWORKS) \
e4b8d1
     || defined(OPENSSL_SYS_UEFI))
e4b8d1
 
e4b8d1
-static ssize_t syscall_random(void *buf, size_t buflen);
e4b8d1
+static ssize_t syscall_random(void *buf, size_t buflen, int nonblock);
e4b8d1
 
e4b8d1
 # if defined(OPENSSL_SYS_VOS)
e4b8d1
 
e4b8d1
@@ -248,7 +250,7 @@ static ssize_t sysctl_random(char *buf,
e4b8d1
  * syscall_random(): Try to get random data using a system call
e4b8d1
  * returns the number of bytes returned in buf, or < 0 on error.
e4b8d1
  */
e4b8d1
-static ssize_t syscall_random(void *buf, size_t buflen)
e4b8d1
+static ssize_t syscall_random(void *buf, size_t buflen, int nonblock)
e4b8d1
 {
e4b8d1
     /*
e4b8d1
      * Note: 'buflen' equals the size of the buffer which is used by the
e4b8d1
@@ -270,6 +272,7 @@ static ssize_t syscall_random(void *buf,
e4b8d1
      * - Linux since 3.17 with glibc 2.25
e4b8d1
      * - FreeBSD since 12.0 (1200061)
e4b8d1
      */
e4b8d1
+#  if 0
e4b8d1
 #  if defined(__GNUC__) && __GNUC__>=2 && defined(__ELF__) && !defined(__hpux)
e4b8d1
     extern int getentropy(void *buffer, size_t length) __attribute__((weak));
e4b8d1
 
e4b8d1
@@ -291,10 +294,10 @@ static ssize_t syscall_random(void *buf,
e4b8d1
     if (p_getentropy.p != NULL)
e4b8d1
         return p_getentropy.f(buf, buflen) == 0 ? (ssize_t)buflen : -1;
e4b8d1
 #  endif
e4b8d1
-
e4b8d1
+#  endif
e4b8d1
     /* Linux supports this since version 3.17 */
e4b8d1
 #  if defined(__linux) && defined(SYS_getrandom)
e4b8d1
-    return syscall(SYS_getrandom, buf, buflen, 0);
e4b8d1
+    return syscall(SYS_getrandom, buf, buflen, nonblock?GRND_NONBLOCK:0);
e4b8d1
 #  elif (defined(__FreeBSD__) || defined(__NetBSD__)) && defined(KERN_ARND)
e4b8d1
     return sysctl_random(buf, buflen);
e4b8d1
 #  else
e4b8d1
@@ -456,8 +459,10 @@ size_t rand_pool_acquire_entropy(RAND_PO
e4b8d1
     size_t bytes_needed;
e4b8d1
     size_t entropy_available = 0;
e4b8d1
     unsigned char *buffer;
e4b8d1
-
e4b8d1
 #   ifdef OPENSSL_RAND_SEED_GETRANDOM
e4b8d1
+    int in_post;
e4b8d1
+
e4b8d1
+    for (in_post = fips_in_post(); in_post >= 0; --in_post) {
e4b8d1
     {
e4b8d1
         ssize_t bytes;
e4b8d1
         /* Maximum allowed number of consecutive unsuccessful attempts */
e4b8d1
@@ -466,7 +471,7 @@ size_t rand_pool_acquire_entropy(RAND_PO
e4b8d1
         bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
e4b8d1
         while (bytes_needed != 0 && attempts-- > 0) {
e4b8d1
             buffer = rand_pool_add_begin(pool, bytes_needed);
e4b8d1
-            bytes = syscall_random(buffer, bytes_needed);
e4b8d1
+            bytes = syscall_random(buffer, bytes_needed, in_post);
e4b8d1
             if (bytes > 0) {
e4b8d1
                 rand_pool_add_end(pool, bytes, 8 * bytes);
e4b8d1
                 bytes_needed -= bytes;
e4b8d1
@@ -498,8 +503,10 @@ size_t rand_pool_acquire_entropy(RAND_PO
e4b8d1
             int attempts = 3;
e4b8d1
             const int fd = get_random_device(i);
e4b8d1
 
e4b8d1
-            if (fd == -1)
e4b8d1
+            if (fd == -1) {
e4b8d1
+                OPENSSL_showfatal("Random device %s cannot be opened.\n", random_device_paths[i]);
e4b8d1
                 continue;
e4b8d1
+            }
e4b8d1
 
e4b8d1
             while (bytes_needed != 0 && attempts-- > 0) {
e4b8d1
                 buffer = rand_pool_add_begin(pool, bytes_needed);
e4b8d1
@@ -559,7 +566,9 @@ size_t rand_pool_acquire_entropy(RAND_PO
e4b8d1
         }
e4b8d1
     }
e4b8d1
 #   endif
e4b8d1
-
e4b8d1
+#   ifdef OPENSSL_RAND_SEED_GETRANDOM
e4b8d1
+    }
e4b8d1
+#   endif
e4b8d1
     return rand_pool_entropy_available(pool);
e4b8d1
 #  endif
e4b8d1
 }