Blob Blame History Raw
From d1dc655cd2c8ae417381e5f966941c75cfe287ee Mon Sep 17 00:00:00 2001
From: Daiki Ueno <ueno@gnu.org>
Date: Thu, 4 Jun 2020 16:42:07 +0200
Subject: [PATCH] _gnutls_fips_mode_enabled: treat selftest failure as FIPS
 disabled

Previously gnutls_fips140_mode_enabled() returned true, even after
selftests have failed and the library state has switched to error.
While later calls to crypto operations fails, it would be more
convenient to have a function to detect that state.

Signed-off-by: Daiki Ueno <ueno@gnu.org>
---
 lib/fips.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/lib/fips.c b/lib/fips.c
index acdd2ec23..f8b10f750 100644
--- a/lib/fips.c
+++ b/lib/fips.c
@@ -491,8 +491,17 @@ unsigned gnutls_fips140_mode_enabled(void)
 #ifdef ENABLE_FIPS140
 	unsigned ret = _gnutls_fips_mode_enabled();
 
-	if (ret > GNUTLS_FIPS140_DISABLED)
+	if (ret > GNUTLS_FIPS140_DISABLED) {
+		/* If the previous run of selftests has failed, return as if
+		 * the FIPS mode is disabled. We could use HAVE_LIB_ERROR, if
+		 * we can assume that all the selftests run atomically from
+		 * the ELF constructor.
+		 */
+		if (_gnutls_get_lib_state() == LIB_STATE_ERROR)
+			return 0;
+
 		return ret;
+	}
 #endif
 	return 0;
 }
-- 
2.26.2