347681c
diff -up openssl-3.0.0-alpha13/crypto/context.c.kernel-fips openssl-3.0.0-alpha13/crypto/context.c
347681c
--- openssl-3.0.0-alpha13/crypto/context.c.kernel-fips	2021-03-16 00:09:55.814826432 +0100
347681c
+++ openssl-3.0.0-alpha13/crypto/context.c	2021-03-16 00:15:55.129043811 +0100
347681c
@@ -12,11 +12,54 @@
347681c
 #include "internal/bio.h"
347681c
 #include "internal/provider.h"
347681c
 
347681c
+#ifndef FIPS_MODULE
347681c
+# include <sys/types.h>
347681c
+# include <sys/stat.h>
347681c
+# include <fcntl.h>
347681c
+# include <unistd.h>
347681c
+# include <openssl/evp.h>
347681c
+#endif
347681c
+
347681c
 struct ossl_lib_ctx_onfree_list_st {
347681c
     ossl_lib_ctx_onfree_fn *fn;
347681c
     struct ossl_lib_ctx_onfree_list_st *next;
347681c
 };
347681c
 
347681c
+# ifndef FIPS_MODULE
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
+
347681c
+static int apply_kernel_fips_flag(OSSL_LIB_CTX *ctx)
347681c
+{
347681c
+	if (kernel_fips_flag) {
347681c
+		return EVP_default_properties_enable_fips(ctx, 1);
347681c
+	}
347681c
+
347681c
+	return 1;
347681c
+}
347681c
+# endif
347681c
+
347681c
+
347681c
 struct ossl_lib_ctx_st {
347681c
     CRYPTO_RWLOCK *lock;
347681c
     CRYPTO_EX_DATA data;
347681c
@@ -74,6 +117,12 @@ static int context_init(OSSL_LIB_CTX *ct
347681c
     if (!ossl_property_parse_init(ctx))
347681c
         goto err;
347681c
 
347681c
+# ifndef FIPS_MODULE
347681c
+	/* Preset the fips=yes default property with kernel FIPS mode */
347681c
+	if (!apply_kernel_fips_flag(ctx))
347681c
+		goto err;
347681c
+# endif
347681c
+
347681c
     return 1;
347681c
  err:
347681c
     if (exdata_done)
347681c
@@ -121,6 +170,7 @@ static CRYPTO_THREAD_LOCAL default_conte
347681c
 
347681c
 DEFINE_RUN_ONCE_STATIC(default_context_do_init)
347681c
 {
347681c
+	 read_kernel_fips_flag();
347681c
     return CRYPTO_THREAD_init_local(&default_context_thread_local, NULL)
347681c
         && context_init(&default_context_int);
347681c
 }