1eb7adc
From aa3aebf132959e7e44876042efaf9ff24ffe0f2b Mon Sep 17 00:00:00 2001
1eb7adc
From: rpm-build <rpm-build>
1eb7adc
Date: Mon, 31 Jul 2023 09:41:27 +0200
1eb7adc
Subject: [PATCH 09/35] 0009-Add-Kernel-FIPS-mode-flag-support.patch
1eb7adc
1eb7adc
Patch-name: 0009-Add-Kernel-FIPS-mode-flag-support.patch
1eb7adc
Patch-id: 9
1eb7adc
Patch-status: |
1eb7adc
    # Add check to see if fips flag is enabled in kernel
1eb7adc
From-dist-git-commit: 9409bc7044cf4b5773639cce20f51399888c45fd
1eb7adc
---
1eb7adc
 crypto/context.c            | 36 ++++++++++++++++++++++++++++++++++++
1eb7adc
 include/internal/provider.h |  3 +++
1eb7adc
 2 files changed, 39 insertions(+)
1eb7adc
1eb7adc
diff --git a/crypto/context.c b/crypto/context.c
1eb7adc
index e294ea1512..51002ba79a 100644
1eb7adc
--- a/crypto/context.c
1eb7adc
+++ b/crypto/context.c
1eb7adc
@@ -16,6 +16,41 @@
9409bc7
 #include "internal/provider.h"
9409bc7
 #include "crypto/context.h"
347681c
 
347681c
+# include <sys/types.h>
347681c
+# include <sys/stat.h>
347681c
+# include <fcntl.h>
347681c
+# include <unistd.h>
347681c
+# include <openssl/evp.h>
347681c
+
347681c
+# define FIPS_MODE_SWITCH_FILE "/proc/sys/crypto/fips_enabled"
347681c
+
347681c
+static int kernel_fips_flag;
347681c
+
347681c
+static void read_kernel_fips_flag(void)
347681c
+{
347681c
+	char buf[2] = "0";
347681c
+	int fd;
347681c
+
347681c
+	if (ossl_safe_getenv("OPENSSL_FORCE_FIPS_MODE") != NULL) {
347681c
+		buf[0] = '1';
347681c
+	} else if ((fd = open(FIPS_MODE_SWITCH_FILE, O_RDONLY)) >= 0) {
347681c
+		while (read(fd, buf, sizeof(buf)) < 0 && errno == EINTR) ;
347681c
+		close(fd);
347681c
+	}
347681c
+
347681c
+	if (buf[0] == '1') {
347681c
+		kernel_fips_flag = 1;
347681c
+	}
347681c
+
347681c
+		return;
347681c
+}
347681c
+
080143c
+int ossl_get_kernel_fips_flag()
347681c
+{
080143c
+	return kernel_fips_flag;
347681c
+}
347681c
+
347681c
+
347681c
 struct ossl_lib_ctx_st {
9409bc7
     CRYPTO_RWLOCK *lock, *rand_crngt_lock;
9409bc7
     OSSL_EX_DATA_GLOBAL global;
1eb7adc
@@ -336,6 +371,7 @@ static int default_context_inited = 0;
347681c
 
347681c
 DEFINE_RUN_ONCE_STATIC(default_context_do_init)
347681c
 {
347681c
+	 read_kernel_fips_flag();
9409bc7
     if (!CRYPTO_THREAD_init_local(&default_context_thread_local, NULL))
9409bc7
         goto err;
9409bc7
 
1eb7adc
diff --git a/include/internal/provider.h b/include/internal/provider.h
1eb7adc
index 18937f84c7..1446bf7afb 100644
1eb7adc
--- a/include/internal/provider.h
1eb7adc
+++ b/include/internal/provider.h
1eb7adc
@@ -112,6 +112,9 @@ int ossl_provider_init_as_child(OSSL_LIB_CTX *ctx,
080143c
                                 const OSSL_DISPATCH *in);
080143c
 void ossl_provider_deinit_child(OSSL_LIB_CTX *ctx);
080143c
 
080143c
+/* FIPS flag access */
080143c
+int ossl_get_kernel_fips_flag(void);
080143c
+
080143c
 # ifdef __cplusplus
080143c
 }
080143c
 # endif
1eb7adc
-- 
1eb7adc
2.41.0
1eb7adc