Blame SOURCES/cryptsetup-2.4.1-Do-not-load-own-OpenSSL-backend-context-in-FIPS-mode.patch

79c0bd
From f8eb7b225affe8b6b9f02ab6a90fd2e73181a526 Mon Sep 17 00:00:00 2001
79c0bd
From: Milan Broz <gmazyland@gmail.com>
79c0bd
Date: Mon, 13 Sep 2021 19:45:05 +0200
79c0bd
Subject: [PATCH 03/11] Do not load own OpenSSL backend context in FIPS mode.
79c0bd
79c0bd
In the FIPS mode keep configuration up to the system wide config.
79c0bd
---
79c0bd
 lib/crypto_backend/crypto_backend.h |  2 +-
79c0bd
 lib/crypto_backend/crypto_gcrypt.c  |  2 +-
79c0bd
 lib/crypto_backend/crypto_kernel.c  |  2 +-
79c0bd
 lib/crypto_backend/crypto_nettle.c  |  2 +-
79c0bd
 lib/crypto_backend/crypto_nss.c     |  2 +-
79c0bd
 lib/crypto_backend/crypto_openssl.c | 39 +++++++++++++++++------------
79c0bd
 lib/setup.c                         |  2 +-
79c0bd
 lib/utils_fips.c                    |  8 +++---
79c0bd
 lib/utils_fips.h                    |  4 ++-
79c0bd
 tests/crypto-vectors.c              |  2 +-
79c0bd
 10 files changed, 37 insertions(+), 28 deletions(-)
79c0bd
79c0bd
diff --git a/lib/crypto_backend/crypto_backend.h b/lib/crypto_backend/crypto_backend.h
79c0bd
index 5278c345..88cc2d59 100644
79c0bd
--- a/lib/crypto_backend/crypto_backend.h
79c0bd
+++ b/lib/crypto_backend/crypto_backend.h
79c0bd
@@ -31,7 +31,7 @@ struct crypt_hmac;
79c0bd
 struct crypt_cipher;
79c0bd
 struct crypt_storage;
79c0bd
 
79c0bd
-int crypt_backend_init(void);
79c0bd
+int crypt_backend_init(bool fips);
79c0bd
 void crypt_backend_destroy(void);
79c0bd
 
79c0bd
 #define CRYPT_BACKEND_KERNEL (1 << 0)	/* Crypto uses kernel part, for benchmark */
79c0bd
diff --git a/lib/crypto_backend/crypto_gcrypt.c b/lib/crypto_backend/crypto_gcrypt.c
79c0bd
index 2845382e..67f26067 100644
79c0bd
--- a/lib/crypto_backend/crypto_gcrypt.c
79c0bd
+++ b/lib/crypto_backend/crypto_gcrypt.c
79c0bd
@@ -94,7 +94,7 @@ static void crypt_hash_test_whirlpool_bug(void)
79c0bd
 		crypto_backend_whirlpool_bug = 1;
79c0bd
 }
79c0bd
 
79c0bd
-int crypt_backend_init(void)
79c0bd
+int crypt_backend_init(bool fips __attribute__((unused)))
79c0bd
 {
79c0bd
 	int r;
79c0bd
 
79c0bd
diff --git a/lib/crypto_backend/crypto_kernel.c b/lib/crypto_backend/crypto_kernel.c
79c0bd
index 2e3d65b2..ce84cfac 100644
79c0bd
--- a/lib/crypto_backend/crypto_kernel.c
79c0bd
+++ b/lib/crypto_backend/crypto_kernel.c
79c0bd
@@ -117,7 +117,7 @@ static int crypt_kernel_socket_init(struct sockaddr_alg *sa, int *tfmfd, int *op
79c0bd
 	return 0;
79c0bd
 }
79c0bd
 
79c0bd
-int crypt_backend_init(void)
79c0bd
+int crypt_backend_init(bool fips __attribute__((unused)))
79c0bd
 {
79c0bd
 	struct utsname uts;
79c0bd
 	struct sockaddr_alg sa = {
79c0bd
diff --git a/lib/crypto_backend/crypto_nettle.c b/lib/crypto_backend/crypto_nettle.c
79c0bd
index 0860a52d..c9b9f5f8 100644
79c0bd
--- a/lib/crypto_backend/crypto_nettle.c
79c0bd
+++ b/lib/crypto_backend/crypto_nettle.c
79c0bd
@@ -213,7 +213,7 @@ static struct hash_alg *_get_alg(const char *name)
79c0bd
 	return NULL;
79c0bd
 }
79c0bd
 
79c0bd
-int crypt_backend_init(void)
79c0bd
+int crypt_backend_init(bool fips __attribute__((unused)))
79c0bd
 {
79c0bd
 	return 0;
79c0bd
 }
79c0bd
diff --git a/lib/crypto_backend/crypto_nss.c b/lib/crypto_backend/crypto_nss.c
79c0bd
index ebe9de0e..a84d3d65 100644
79c0bd
--- a/lib/crypto_backend/crypto_nss.c
79c0bd
+++ b/lib/crypto_backend/crypto_nss.c
79c0bd
@@ -75,7 +75,7 @@ static struct hash_alg *_get_alg(const char *name)
79c0bd
 	return NULL;
79c0bd
 }
79c0bd
 
79c0bd
-int crypt_backend_init(void)
79c0bd
+int crypt_backend_init(bool fips __attribute__((unused)))
79c0bd
 {
79c0bd
 	int r;
79c0bd
 
79c0bd
diff --git a/lib/crypto_backend/crypto_openssl.c b/lib/crypto_backend/crypto_openssl.c
79c0bd
index 92eeb33c..2a490ce5 100644
79c0bd
--- a/lib/crypto_backend/crypto_openssl.c
79c0bd
+++ b/lib/crypto_backend/crypto_openssl.c
79c0bd
@@ -88,7 +88,7 @@ struct hash_alg {
79c0bd
 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
79c0bd
     (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
79c0bd
 
79c0bd
-static void openssl_backend_init(void)
79c0bd
+static void openssl_backend_init(bool fips __attribute__((unused)))
79c0bd
 {
79c0bd
 	OpenSSL_add_all_algorithms();
79c0bd
 }
79c0bd
@@ -150,7 +150,7 @@ static void openssl_backend_exit(void)
79c0bd
 #endif
79c0bd
 }
79c0bd
 
79c0bd
-static int openssl_backend_init(void)
79c0bd
+static int openssl_backend_init(bool fips)
79c0bd
 {
79c0bd
 /*
79c0bd
  * OpenSSL >= 3.0.0 provides some algorithms in legacy provider
79c0bd
@@ -158,23 +158,30 @@ static int openssl_backend_init(void)
79c0bd
 #if OPENSSL_VERSION_MAJOR >= 3
79c0bd
 	int r;
79c0bd
 
79c0bd
-	ossl_ctx = OSSL_LIB_CTX_new();
79c0bd
-	if (!ossl_ctx)
79c0bd
-		return -EINVAL;
79c0bd
+	/*
79c0bd
+	 * In FIPS mode we keep default OpenSSL context & global config
79c0bd
+	 */
79c0bd
+	if (!fips) {
79c0bd
+		ossl_ctx = OSSL_LIB_CTX_new();
79c0bd
+		if (!ossl_ctx)
79c0bd
+			return -EINVAL;
79c0bd
 
79c0bd
-	ossl_default = OSSL_PROVIDER_try_load(ossl_ctx, "default", 0);
79c0bd
-	if (!ossl_default) {
79c0bd
-		OSSL_LIB_CTX_free(ossl_ctx);
79c0bd
-		return -EINVAL;
79c0bd
-	}
79c0bd
+		ossl_default = OSSL_PROVIDER_try_load(ossl_ctx, "default", 0);
79c0bd
+		if (!ossl_default) {
79c0bd
+			OSSL_LIB_CTX_free(ossl_ctx);
79c0bd
+			return -EINVAL;
79c0bd
+		}
79c0bd
 
79c0bd
-	/* Optional */
79c0bd
-	ossl_legacy = OSSL_PROVIDER_try_load(ossl_ctx, "legacy", 0);
79c0bd
+		/* Optional */
79c0bd
+		ossl_legacy = OSSL_PROVIDER_try_load(ossl_ctx, "legacy", 0);
79c0bd
+	}
79c0bd
 
79c0bd
-	r = snprintf(backend_version, sizeof(backend_version), "%s %s%s",
79c0bd
+	r = snprintf(backend_version, sizeof(backend_version), "%s %s%s%s",
79c0bd
 		OpenSSL_version(OPENSSL_VERSION),
79c0bd
 		ossl_default ? "[default]" : "",
79c0bd
-		ossl_legacy  ? "[legacy]" : "");
79c0bd
+		ossl_legacy  ? "[legacy]" : "",
79c0bd
+		fips  ? "[fips]" : "");
79c0bd
+
79c0bd
 	if (r < 0 || (size_t)r >= sizeof(backend_version)) {
79c0bd
 		openssl_backend_exit();
79c0bd
 		return -EINVAL;
79c0bd
@@ -193,12 +200,12 @@ static const char *openssl_backend_version(void)
79c0bd
 }
79c0bd
 #endif
79c0bd
 
79c0bd
-int crypt_backend_init(void)
79c0bd
+int crypt_backend_init(bool fips)
79c0bd
 {
79c0bd
 	if (crypto_backend_initialised)
79c0bd
 		return 0;
79c0bd
 
79c0bd
-	if (openssl_backend_init())
79c0bd
+	if (openssl_backend_init(fips))
79c0bd
 		return -EINVAL;
79c0bd
 
79c0bd
 	crypto_backend_initialised = 1;
79c0bd
diff --git a/lib/setup.c b/lib/setup.c
79c0bd
index dc5459f5..a5dfd843 100644
79c0bd
--- a/lib/setup.c
79c0bd
+++ b/lib/setup.c
79c0bd
@@ -227,7 +227,7 @@ int init_crypto(struct crypt_device *ctx)
79c0bd
 		return r;
79c0bd
 	}
79c0bd
 
79c0bd
-	r = crypt_backend_init();
79c0bd
+	r = crypt_backend_init(crypt_fips_mode());
79c0bd
 	if (r < 0)
79c0bd
 		log_err(ctx, _("Cannot initialize crypto backend."));
79c0bd
 
79c0bd
diff --git a/lib/utils_fips.c b/lib/utils_fips.c
79c0bd
index 4fa22fb9..0c2b6434 100644
79c0bd
--- a/lib/utils_fips.c
79c0bd
+++ b/lib/utils_fips.c
79c0bd
@@ -24,9 +24,9 @@
79c0bd
 #include "utils_fips.h"
79c0bd
 
79c0bd
 #if !ENABLE_FIPS
79c0bd
-int crypt_fips_mode(void) { return 0; }
79c0bd
+bool crypt_fips_mode(void) { return false; }
79c0bd
 #else
79c0bd
-static int kernel_fips_mode(void)
79c0bd
+static bool kernel_fips_mode(void)
79c0bd
 {
79c0bd
 	int fd;
79c0bd
 	char buf[1] = "";
79c0bd
@@ -36,10 +36,10 @@ static int kernel_fips_mode(void)
79c0bd
 		close(fd);
79c0bd
 	}
79c0bd
 
79c0bd
-	return (buf[0] == '1') ? 1 : 0;
79c0bd
+	return (buf[0] == '1');
79c0bd
 }
79c0bd
 
79c0bd
-int crypt_fips_mode(void)
79c0bd
+bool crypt_fips_mode(void)
79c0bd
 {
79c0bd
 	return kernel_fips_mode() && !access("/etc/system-fips", F_OK);
79c0bd
 }
79c0bd
diff --git a/lib/utils_fips.h b/lib/utils_fips.h
79c0bd
index 51b110b5..13cfc9fb 100644
79c0bd
--- a/lib/utils_fips.h
79c0bd
+++ b/lib/utils_fips.h
79c0bd
@@ -21,6 +21,8 @@
79c0bd
 #ifndef _UTILS_FIPS_H
79c0bd
 #define _UTILS_FIPS_H
79c0bd
 
79c0bd
-int crypt_fips_mode(void);
79c0bd
+#include <stdbool.h>
79c0bd
+
79c0bd
+bool crypt_fips_mode(void);
79c0bd
 
79c0bd
 #endif /* _UTILS_FIPS_H */
79c0bd
diff --git a/tests/crypto-vectors.c b/tests/crypto-vectors.c
79c0bd
index 025585de..6484e97a 100644
79c0bd
--- a/tests/crypto-vectors.c
79c0bd
+++ b/tests/crypto-vectors.c
79c0bd
@@ -1301,7 +1301,7 @@ int main(__attribute__ ((unused)) int argc, __attribute__ ((unused))char *argv[]
79c0bd
 		exit(77);
79c0bd
 	}
79c0bd
 
79c0bd
-	if (crypt_backend_init())
79c0bd
+	if (crypt_backend_init(fips_mode()))
79c0bd
 		exit_test("Crypto backend init error.", EXIT_FAILURE);
79c0bd
 
79c0bd
 	printf("Test vectors using %s crypto backend.\n", crypt_backend_version());
79c0bd
-- 
79c0bd
2.27.0
79c0bd