isaacpittman-hitachi / rpms / openssl

Forked from rpms/openssl 2 years ago
Clone

Blame SOURCES/0009-Add-Kernel-FIPS-mode-flag-support.patch

6ed7c9
diff -up openssl-3.0.0-alpha13/crypto/context.c.kernel-fips openssl-3.0.0-alpha13/crypto/context.c
6ed7c9
--- openssl-3.0.0-alpha13/crypto/context.c.kernel-fips	2021-03-16 00:09:55.814826432 +0100
6ed7c9
+++ openssl-3.0.0-alpha13/crypto/context.c	2021-03-16 00:15:55.129043811 +0100
6ed7c9
@@ -12,11 +12,46 @@
6ed7c9
 #include "internal/bio.h"
6ed7c9
 #include "internal/provider.h"
6ed7c9
 
6ed7c9
+# include <sys/types.h>
6ed7c9
+# include <sys/stat.h>
6ed7c9
+# include <fcntl.h>
6ed7c9
+# include <unistd.h>
6ed7c9
+# include <openssl/evp.h>
6ed7c9
+
6ed7c9
 struct ossl_lib_ctx_onfree_list_st {
6ed7c9
     ossl_lib_ctx_onfree_fn *fn;
6ed7c9
     struct ossl_lib_ctx_onfree_list_st *next;
6ed7c9
 };
6ed7c9
 
6ed7c9
+# define FIPS_MODE_SWITCH_FILE "/proc/sys/crypto/fips_enabled"
6ed7c9
+
6ed7c9
+static int kernel_fips_flag;
6ed7c9
+
6ed7c9
+static void read_kernel_fips_flag(void)
6ed7c9
+{
6ed7c9
+	char buf[2] = "0";
6ed7c9
+	int fd;
6ed7c9
+
6ed7c9
+	if (ossl_safe_getenv("OPENSSL_FORCE_FIPS_MODE") != NULL) {
6ed7c9
+		buf[0] = '1';
6ed7c9
+	} else if ((fd = open(FIPS_MODE_SWITCH_FILE, O_RDONLY)) >= 0) {
6ed7c9
+		while (read(fd, buf, sizeof(buf)) < 0 && errno == EINTR) ;
6ed7c9
+		close(fd);
6ed7c9
+	}
6ed7c9
+
6ed7c9
+	if (buf[0] == '1') {
6ed7c9
+		kernel_fips_flag = 1;
6ed7c9
+	}
6ed7c9
+
6ed7c9
+		return;
6ed7c9
+}
6ed7c9
+
6ed7c9
+int ossl_get_kernel_fips_flag()
6ed7c9
+{
6ed7c9
+	return kernel_fips_flag;
6ed7c9
+}
6ed7c9
+
6ed7c9
+
6ed7c9
 struct ossl_lib_ctx_st {
6ed7c9
     CRYPTO_RWLOCK *lock;
6ed7c9
     CRYPTO_EX_DATA data;
6ed7c9
@@ -121,6 +170,7 @@ static CRYPTO_THREAD_LOCAL default_conte
6ed7c9
 
6ed7c9
 DEFINE_RUN_ONCE_STATIC(default_context_do_init)
6ed7c9
 {
6ed7c9
+	 read_kernel_fips_flag();
6ed7c9
     return CRYPTO_THREAD_init_local(&default_context_thread_local, NULL)
6ed7c9
         && context_init(&default_context_int);
6ed7c9
 }
6ed7c9
diff -up openssl-3.0.0/include/internal/provider.h.embed-fips openssl-3.0.0/include/internal/provider.h
6ed7c9
--- openssl-3.0.0/include/internal/provider.h.embed-fips	2021-11-12 12:18:36.215333452 +0100
6ed7c9
+++ openssl-3.0.0/include/internal/provider.h	2021-11-12 12:22:41.298409269 +0100
6ed7c9
@@ -109,6 +109,9 @@ int ossl_provider_init_as_child(OSSL_LIB
6ed7c9
                                 const OSSL_CORE_HANDLE *handle,
6ed7c9
                                 const OSSL_DISPATCH *in);
6ed7c9
 
6ed7c9
+/* FIPS flag access */
6ed7c9
+int ossl_get_kernel_fips_flag(void);
6ed7c9
+
6ed7c9
 # ifdef __cplusplus
6ed7c9
 }
6ed7c9
 # endif