Blame SOURCES/gnutls-3.7.6-fips-run-selftests.patch

cd0318
From 036fb360e5775f01ef25f5e712024a29930c462e Mon Sep 17 00:00:00 2001
cd0318
From: Daiki Ueno <ueno@gnu.org>
cd0318
Date: Fri, 3 Jun 2022 15:43:00 +0900
cd0318
Subject: [PATCH] fips: provide function to manually run FIPS self-tests
cd0318
cd0318
FIPS140-3 IG 10.3.E Periodic Self-Testing says:
cd0318
cd0318
  At security levels 1 and 2, acceptable means for initiating the
cd0318
  periodic self-tests include a provided service, resetting, rebooting
cd0318
  or power cycling.
cd0318
cd0318
Neither resetting, rebooting, nor power-cycling is suitable because
cd0318
those involve operations outside of the module.  Therefore this patch
cd0318
adds a new API to manually run the substance of FIPS140 self-tests.
cd0318
cd0318
Suggeested by Richard Costa and Stephan Mueller in:
cd0318
https://gitlab.com/gnutls/gnutls/-/issues/1364
cd0318
cd0318
Signed-off-by: Daiki Ueno <ueno@gnu.org>
cd0318
---
cd0318
 NEWS                            |   5 ++
cd0318
 devel/libgnutls.abignore        |   2 +
cd0318
 devel/symbols.last              |   2 +
cd0318
 doc/Makefile.am                 |   2 +
cd0318
 doc/manpages/Makefile.am        |   1 +
cd0318
 lib/fips.c                      | 139 ++++++++++++++++----------------
cd0318
 lib/global.c                    |  14 +++-
cd0318
 lib/includes/gnutls/gnutls.h.in |   2 +
cd0318
 lib/libgnutls.map               |   8 ++
cd0318
 tests/fips-test.c               |   7 ++
cd0318
 10 files changed, 110 insertions(+), 72 deletions(-)
cd0318
cd0318
diff --git a/NEWS b/NEWS
cd0318
index 70dd8a12b5..389be8acaa 100644
cd0318
--- a/NEWS
cd0318
+++ b/NEWS
cd0318
@@ -5,6 +5,11 @@ Copyright (C) 2000-2016 Free Software Foundation, Inc.
cd0318
 Copyright (C) 2013-2019 Nikos Mavrogiannopoulos
cd0318
 See the end for copying conditions.
cd0318
 
cd0318
+* Version 3.7.7 (unreleased)
cd0318
+
cd0318
+** API and ABI modifications:
cd0318
+gnutls_fips140_run_self_tests: New function
cd0318
+
cd0318
 * Version 3.7.6 (released 2022-05-27)
cd0318
 
cd0318
 ** libgnutls: Fixed invalid write when gnutls_realloc_zero()
cd0318
diff --git a/doc/Makefile.am b/doc/Makefile.am
cd0318
index d20a021d97..34ef43866c 100644
cd0318
--- a/doc/Makefile.am
cd0318
+++ b/doc/Makefile.am
cd0318
@@ -1096,6 +1096,8 @@ FUNCS += functions/gnutls_fips140_pop_context
cd0318
 FUNCS += functions/gnutls_fips140_pop_context.short
cd0318
 FUNCS += functions/gnutls_fips140_push_context
cd0318
 FUNCS += functions/gnutls_fips140_push_context.short
cd0318
+FUNCS += functions/gnutls_fips140_run_self_tests
cd0318
+FUNCS += functions/gnutls_fips140_run_self_tests.short
cd0318
 FUNCS += functions/gnutls_fips140_set_mode
cd0318
 FUNCS += functions/gnutls_fips140_set_mode.short
cd0318
 FUNCS += functions/gnutls_get_library_config
cd0318
diff --git a/doc/manpages/Makefile.am b/doc/manpages/Makefile.am
cd0318
index d8c5f2854d..90906b0574 100644
cd0318
--- a/doc/manpages/Makefile.am
cd0318
+++ b/doc/manpages/Makefile.am
cd0318
@@ -380,6 +380,7 @@ APIMANS += gnutls_fips140_get_operation_state.3
cd0318
 APIMANS += gnutls_fips140_mode_enabled.3
cd0318
 APIMANS += gnutls_fips140_pop_context.3
cd0318
 APIMANS += gnutls_fips140_push_context.3
cd0318
+APIMANS += gnutls_fips140_run_self_tests.3
cd0318
 APIMANS += gnutls_fips140_set_mode.3
cd0318
 APIMANS += gnutls_get_library_config.3
cd0318
 APIMANS += gnutls_get_system_config_file.3
cd0318
diff --git a/lib/fips.c b/lib/fips.c
cd0318
index e9c27f6df6..656d43e74a 100644
cd0318
--- a/lib/fips.c
cd0318
+++ b/lib/fips.c
cd0318
@@ -419,8 +419,6 @@ int _gnutls_fips_perform_self_checks1(void)
cd0318
 {
cd0318
 	int ret;
cd0318
 
cd0318
-	_gnutls_switch_lib_state(LIB_STATE_SELFTEST);
cd0318
-
cd0318
 	/* Tests the FIPS algorithms used by nettle internally.
cd0318
 	 * In our case we test AES-CBC since nettle's AES is used by
cd0318
 	 * the DRBG-AES.
cd0318
@@ -429,193 +427,153 @@ int _gnutls_fips_perform_self_checks1(void)
cd0318
 	/* ciphers - one test per cipher */
cd0318
 	ret = gnutls_cipher_self_test(0, GNUTLS_CIPHER_AES_128_CBC);
cd0318
 	if (ret < 0) {
cd0318
-		gnutls_assert();
cd0318
-		goto error;
cd0318
+		return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
cd0318
 	}
cd0318
 
cd0318
 	return 0;
cd0318
-
cd0318
-error:
cd0318
-	_gnutls_switch_lib_state(LIB_STATE_ERROR);
cd0318
-	_gnutls_audit_log(NULL, "FIPS140-2 self testing part1 failed\n");
cd0318
-
cd0318
-	return GNUTLS_E_SELF_TEST_ERROR;
cd0318
 }
cd0318
 
cd0318
 int _gnutls_fips_perform_self_checks2(void)
cd0318
 {
cd0318
 	int ret;
cd0318
 
cd0318
-	_gnutls_switch_lib_state(LIB_STATE_SELFTEST);
cd0318
-
cd0318
 	/* Tests the FIPS algorithms */
cd0318
 
cd0318
 	/* ciphers - one test per cipher */
cd0318
 	ret = gnutls_cipher_self_test(0, GNUTLS_CIPHER_3DES_CBC);
cd0318
 	if (ret < 0) {
cd0318
-		gnutls_assert();
cd0318
-		goto error;
cd0318
+		return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
cd0318
 	}
cd0318
 
cd0318
 	ret = gnutls_cipher_self_test(0, GNUTLS_CIPHER_AES_256_CBC);
cd0318
 	if (ret < 0) {
cd0318
-		gnutls_assert();
cd0318
-		goto error;
cd0318
+		return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
cd0318
 	}
cd0318
 
cd0318
 	ret = gnutls_cipher_self_test(0, GNUTLS_CIPHER_AES_256_GCM);
cd0318
 	if (ret < 0) {
cd0318
-		gnutls_assert();
cd0318
-		goto error;
cd0318
+		return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
cd0318
 	}
cd0318
 
cd0318
 	ret = gnutls_cipher_self_test(0, GNUTLS_CIPHER_AES_256_XTS);
cd0318
 	if (ret < 0) {
cd0318
-		gnutls_assert();
cd0318
-		goto error;
cd0318
+		return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
cd0318
 	}
cd0318
 
cd0318
 	ret = gnutls_cipher_self_test(0, GNUTLS_CIPHER_AES_256_CFB8);
cd0318
 	if (ret < 0) {
cd0318
-		gnutls_assert();
cd0318
-		goto error;
cd0318
+		return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
cd0318
 	}
cd0318
 
cd0318
 	/* Digest tests */
cd0318
 	ret = gnutls_digest_self_test(0, GNUTLS_DIG_SHA3_224);
cd0318
 	if (ret < 0) {
cd0318
-		gnutls_assert();
cd0318
-		goto error;
cd0318
+		return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
cd0318
 	}
cd0318
 
cd0318
 	ret = gnutls_digest_self_test(0, GNUTLS_DIG_SHA3_256);
cd0318
 	if (ret < 0) {
cd0318
-		gnutls_assert();
cd0318
-		goto error;
cd0318
+		return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
cd0318
 	}
cd0318
 
cd0318
 	ret = gnutls_digest_self_test(0, GNUTLS_DIG_SHA3_384);
cd0318
 	if (ret < 0) {
cd0318
-		gnutls_assert();
cd0318
-		goto error;
cd0318
+		return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
cd0318
 	}
cd0318
 
cd0318
 	ret = gnutls_digest_self_test(0, GNUTLS_DIG_SHA3_512);
cd0318
 	if (ret < 0) {
cd0318
-		gnutls_assert();
cd0318
-		goto error;
cd0318
+		return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
cd0318
 	}
cd0318
 
cd0318
 	/* MAC (includes message digest test) */
cd0318
 	ret = gnutls_mac_self_test(0, GNUTLS_MAC_SHA1);
cd0318
 	if (ret < 0) {
cd0318
-		gnutls_assert();
cd0318
-		goto error;
cd0318
+		return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
cd0318
 	}
cd0318
 
cd0318
 	ret = gnutls_mac_self_test(0, GNUTLS_MAC_SHA224);
cd0318
 	if (ret < 0) {
cd0318
-		gnutls_assert();
cd0318
-		goto error;
cd0318
+		return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
cd0318
 	}
cd0318
 
cd0318
 	ret = gnutls_mac_self_test(0, GNUTLS_MAC_SHA256);
cd0318
 	if (ret < 0) {
cd0318
-		gnutls_assert();
cd0318
-		goto error;
cd0318
+		return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
cd0318
 	}
cd0318
 
cd0318
 	ret = gnutls_mac_self_test(0, GNUTLS_MAC_SHA384);
cd0318
 	if (ret < 0) {
cd0318
-		gnutls_assert();
cd0318
-		goto error;
cd0318
+		return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
cd0318
 	}
cd0318
 
cd0318
 	ret = gnutls_mac_self_test(0, GNUTLS_MAC_SHA512);
cd0318
 	if (ret < 0) {
cd0318
-		gnutls_assert();
cd0318
-		goto error;
cd0318
+		return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
cd0318
 	}
cd0318
 
cd0318
 	ret = gnutls_mac_self_test(0, GNUTLS_MAC_AES_CMAC_256);
cd0318
 	if (ret < 0) {
cd0318
-		gnutls_assert();
cd0318
-		goto error;
cd0318
+		return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
cd0318
 	}
cd0318
 
cd0318
 	/* PK */
cd0318
 	ret = gnutls_pk_self_test(0, GNUTLS_PK_RSA);
cd0318
 	if (ret < 0) {
cd0318
-		gnutls_assert();
cd0318
-		goto error;
cd0318
+		return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
cd0318
 	}
cd0318
 
cd0318
 	ret = gnutls_pk_self_test(0, GNUTLS_PK_DSA);
cd0318
 	if (ret < 0) {
cd0318
-		gnutls_assert();
cd0318
-		goto error;
cd0318
+		return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
cd0318
 	}
cd0318
 
cd0318
 	ret = gnutls_pk_self_test(0, GNUTLS_PK_EC);
cd0318
 	if (ret < 0) {
cd0318
-		gnutls_assert();
cd0318
-		goto error;
cd0318
+		return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
cd0318
 	}
cd0318
 
cd0318
 	ret = gnutls_pk_self_test(0, GNUTLS_PK_DH);
cd0318
 	if (ret < 0) {
cd0318
-		gnutls_assert();
cd0318
-		goto error;
cd0318
+		return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
cd0318
 	}
cd0318
 
cd0318
 	/* HKDF */
cd0318
 	ret = gnutls_hkdf_self_test(0, GNUTLS_MAC_SHA256);
cd0318
 	if (ret < 0) {
cd0318
-		gnutls_assert();
cd0318
-		goto error;
cd0318
+		return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
cd0318
 	}
cd0318
 
cd0318
 	/* PBKDF2 */
cd0318
 	ret = gnutls_pbkdf2_self_test(0, GNUTLS_MAC_SHA256);
cd0318
 	if (ret < 0) {
cd0318
-		gnutls_assert();
cd0318
-		goto error;
cd0318
+		return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
cd0318
 	}
cd0318
 
cd0318
 	/* TLS-PRF */
cd0318
 	ret = gnutls_tlsprf_self_test(0, GNUTLS_MAC_SHA256);
cd0318
 	if (ret < 0) {
cd0318
-		gnutls_assert();
cd0318
-		goto error;
cd0318
+		return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
cd0318
 	}
cd0318
 
cd0318
 	if (_gnutls_rnd_ops.self_test == NULL) {
cd0318
-		gnutls_assert();
cd0318
-		goto error;
cd0318
+		return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
cd0318
 	}
cd0318
 
cd0318
 	/* this does not require rng initialization */
cd0318
 	ret = _gnutls_rnd_ops.self_test();
cd0318
 	if (ret < 0) {
cd0318
-		gnutls_assert();
cd0318
-		goto error;
cd0318
+		return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
cd0318
 	}
cd0318
 
cd0318
 	if (_skip_integrity_checks == 0) {
cd0318
 		ret = check_binary_integrity();
cd0318
 		if (ret < 0) {
cd0318
-			gnutls_assert();
cd0318
-			goto error;
cd0318
+			return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
cd0318
 		}
cd0318
 	}
cd0318
 
cd0318
 	return 0;
cd0318
-
cd0318
-error:
cd0318
-	_gnutls_switch_lib_state(LIB_STATE_ERROR);
cd0318
-	_gnutls_audit_log(NULL, "FIPS140-2 self testing part 2 failed\n");
cd0318
-
cd0318
-	return GNUTLS_E_SELF_TEST_ERROR;
cd0318
 }
cd0318
 #endif
cd0318
 
cd0318
@@ -894,3 +852,48 @@ _gnutls_switch_fips_state(gnutls_fips140_operation_state_t state)
cd0318
 	(void)state;
cd0318
 #endif
cd0318
 }
cd0318
+
cd0318
+/**
cd0318
+ * gnutls_fips140_run_self_tests:
cd0318
+ *
cd0318
+ * Manually perform the second round of the FIPS140 self-tests,
cd0318
+ * including:
cd0318
+ *
cd0318
+ * - Known answer tests (KAT) for the selected set of symmetric
cd0318
+ *   cipher, MAC, public key, KDF, and DRBG
cd0318
+ * - Library integrity checks
cd0318
+ *
cd0318
+ * Upon failure with FIPS140 mode enabled, it makes the library
cd0318
+ * unusable.  This function is not thread-safe.
cd0318
+ *
cd0318
+ * Returns: 0 upon success, a negative error code otherwise
cd0318
+ *
cd0318
+ * Since: 3.7.7
cd0318
+ */
cd0318
+int
cd0318
+gnutls_fips140_run_self_tests(void)
cd0318
+{
cd0318
+#ifdef ENABLE_FIPS140
cd0318
+	int ret;
cd0318
+	unsigned prev_lib_state;
cd0318
+
cd0318
+	/* Temporarily switch to LIB_STATE_SELFTEST as some of the
cd0318
+	 * algorithms are implemented using special constructs in
cd0318
+	 * self-tests (such as deterministic variants) */
cd0318
+	prev_lib_state = _gnutls_get_lib_state();
cd0318
+	_gnutls_switch_lib_state(LIB_STATE_SELFTEST);
cd0318
+
cd0318
+	ret = _gnutls_fips_perform_self_checks2();
cd0318
+	if (gnutls_fips140_mode_enabled() != GNUTLS_FIPS140_DISABLED &&
cd0318
+	    ret < 0) {
cd0318
+		_gnutls_switch_lib_state(LIB_STATE_ERROR);
cd0318
+		_gnutls_audit_log(NULL, "FIPS140-2 self testing part 2 failed\n");
cd0318
+	} else {
cd0318
+		/* Restore the previous library state */
cd0318
+		_gnutls_switch_lib_state(prev_lib_state);
cd0318
+	}
cd0318
+	return ret;
cd0318
+#else
cd0318
+	return 0;
cd0318
+#endif
cd0318
+}
cd0318
diff --git a/lib/global.c b/lib/global.c
cd0318
index faa7f0afb2..1b372c15bd 100644
cd0318
--- a/lib/global.c
cd0318
+++ b/lib/global.c
cd0318
@@ -336,9 +336,12 @@ static int _gnutls_global_init(unsigned constructor)
cd0318
 
cd0318
 		/* first round of self checks, these are done on the
cd0318
 		 * nettle algorithms which are used internally */
cd0318
+		_gnutls_switch_lib_state(LIB_STATE_SELFTEST);
cd0318
 		ret = _gnutls_fips_perform_self_checks1();
cd0318
-		if (res != 2) {
cd0318
-			if (ret < 0) {
cd0318
+		if (ret < 0) {
cd0318
+			_gnutls_switch_lib_state(LIB_STATE_ERROR);
cd0318
+			_gnutls_audit_log(NULL, "FIPS140-2 self testing part1 failed\n");
cd0318
+			if (res != 2) {
cd0318
 				gnutls_assert();
cd0318
 				goto out;
cd0318
 			}
cd0318
@@ -355,9 +358,12 @@ static int _gnutls_global_init(unsigned constructor)
cd0318
 	 * (e.g., AESNI overridden AES). They are after _gnutls_register_accel_crypto()
cd0318
 	 * intentionally */
cd0318
 	if (res != 0) {
cd0318
+		_gnutls_switch_lib_state(LIB_STATE_SELFTEST);
cd0318
 		ret = _gnutls_fips_perform_self_checks2();
cd0318
-		if (res != 2) {
cd0318
-			if (ret < 0) {
cd0318
+		if (ret < 0) {
cd0318
+			_gnutls_switch_lib_state(LIB_STATE_ERROR);
cd0318
+			_gnutls_audit_log(NULL, "FIPS140-2 self testing part 2 failed\n");
cd0318
+			if (res != 2) {
cd0318
 				gnutls_assert();
cd0318
 				goto out;
cd0318
 			}
cd0318
diff --git a/lib/includes/gnutls/gnutls.h.in b/lib/includes/gnutls/gnutls.h.in
cd0318
index f7fc5d114a..5840f331e9 100644
cd0318
--- a/lib/includes/gnutls/gnutls.h.in
cd0318
+++ b/lib/includes/gnutls/gnutls.h.in
cd0318
@@ -3416,6 +3416,8 @@ gnutls_fips140_get_operation_state(gnutls_fips140_context_t context);
cd0318
 int gnutls_fips140_push_context(gnutls_fips140_context_t context);
cd0318
 int gnutls_fips140_pop_context(void);
cd0318
 
cd0318
+int gnutls_fips140_run_self_tests(void);
cd0318
+
cd0318
   /* Gnutls error codes. The mapping to a TLS alert is also shown in
cd0318
    * comments.
cd0318
    */
cd0318
diff --git a/lib/libgnutls.map b/lib/libgnutls.map
cd0318
index 0241946c8a..f42d5f9fae 100644
cd0318
--- a/lib/libgnutls.map
cd0318
+++ b/lib/libgnutls.map
cd0318
@@ -1399,6 +1399,14 @@ GNUTLS_3_7_5
cd0318
 	*;
cd0318
 } GNUTLS_3_7_4;
cd0318
 
cd0318
+GNUTLS_3_7_7
cd0318
+{
cd0318
+ global:
cd0318
+	gnutls_fips140_run_self_tests;
cd0318
+ local:
cd0318
+	*;
cd0318
+} GNUTLS_3_7_5;
cd0318
+
cd0318
 GNUTLS_FIPS140_3_4 {
cd0318
   global:
cd0318
 	gnutls_cipher_self_test;
cd0318
diff --git a/tests/fips-test.c b/tests/fips-test.c
cd0318
index a6a283fa67..31a5e26111 100644
cd0318
--- a/tests/fips-test.c
cd0318
+++ b/tests/fips-test.c
cd0318
@@ -525,6 +525,13 @@ void doit(void)
cd0318
 	}
cd0318
 
cd0318
 	gnutls_fips140_context_deinit(fips_context);
cd0318
+
cd0318
+	/* run self-tests manually */
cd0318
+	ret = gnutls_fips140_run_self_tests();
cd0318
+	if (ret < 0) {
cd0318
+		fail("gnutls_fips140_run_self_tests failed\n");
cd0318
+	}
cd0318
+
cd0318
 	gnutls_global_deinit();
cd0318
 	return;
cd0318
 }
cd0318
-- 
cd0318
2.36.1
cd0318
cd0318
From 354027c0c09db60d3083fa48ae791046d336957b Mon Sep 17 00:00:00 2001
cd0318
From: Alexander Sosedkin <asosedkin@redhat.com>
cd0318
Date: Tue, 28 Jun 2022 17:22:36 +0200
cd0318
Subject: [PATCH] tests/fips-test: minor extension
cd0318
cd0318
Signed-off-by: Alexander Sosedkin <asosedkin@redhat.com>
cd0318
---
cd0318
 tests/fips-test.c | 36 +++++++++++++++++++++++++++---------
cd0318
 1 file changed, 27 insertions(+), 9 deletions(-)
cd0318
cd0318
diff --git a/tests/fips-test.c b/tests/fips-test.c
cd0318
index 31a5e26111..f9bd34586a 100644
cd0318
--- a/tests/fips-test.c
cd0318
+++ b/tests/fips-test.c
cd0318
@@ -427,34 +427,43 @@ void doit(void)
cd0318
 	rsa_import_keypair(&privkey, &pubkey, "rsa-2432.pem");
cd0318
 	FIPS_POP_CONTEXT(INITIAL);
cd0318
 
cd0318
-	/* Create a signature with SHA256; approved */
cd0318
+	/* Create a signature with 2432-bit RSA and SHA256; approved */
cd0318
 	FIPS_PUSH_CONTEXT();
cd0318
 	ret = gnutls_privkey_sign_data(privkey, GNUTLS_DIG_SHA256, 0,
cd0318
 				       &data, &signature);
cd0318
 	if (ret < 0) {
cd0318
 		fail("gnutls_privkey_sign_data failed\n");
cd0318
 	}
cd0318
-	gnutls_free(signature.data);
cd0318
 	FIPS_POP_CONTEXT(APPROVED);
cd0318
 
cd0318
-	/* Create a signature with SHA-1; not approved */
cd0318
+	/* Verify a signature with 2432-bit RSA and SHA256; approved */
cd0318
+	FIPS_PUSH_CONTEXT();
cd0318
+	ret = gnutls_pubkey_verify_data2(pubkey, GNUTLS_SIGN_RSA_SHA256, 0,
cd0318
+	                                 &data, &signature);
cd0318
+	if (ret < 0) {
cd0318
+		fail("gnutls_pubkey_verify_data2 failed\n");
cd0318
+	}
cd0318
+	FIPS_POP_CONTEXT(APPROVED);
cd0318
+	gnutls_free(signature.data);
cd0318
+
cd0318
+	/* Create a signature with 2432-bit RSA and SHA-1; not approved */
cd0318
 	FIPS_PUSH_CONTEXT();
cd0318
 	ret = gnutls_privkey_sign_data(privkey, GNUTLS_DIG_SHA1, 0,
cd0318
 				       &data, &signature);
cd0318
 	if (ret < 0) {
cd0318
 		fail("gnutls_privkey_sign_data failed\n");
cd0318
 	}
cd0318
-	gnutls_free(signature.data);
cd0318
 	FIPS_POP_CONTEXT(NOT_APPROVED);
cd0318
 
cd0318
-	/* Verify a signature created with SHA-1; approved */
cd0318
+	/* Verify a signature created with 2432-bit RSA and SHA-1; approved */
cd0318
 	FIPS_PUSH_CONTEXT();
cd0318
-	ret = gnutls_pubkey_verify_data2(pubkey, GNUTLS_SIGN_RSA_SHA1, 0, &data,
cd0318
-					 &rsa2342_sha1_sig);
cd0318
+	ret = gnutls_pubkey_verify_data2(pubkey, GNUTLS_SIGN_RSA_SHA1, 0,
cd0318
+	                                 &data, &rsa2342_sha1_sig);
cd0318
 	if (ret < 0) {
cd0318
 		fail("gnutls_pubkey_verify_data2 failed\n");
cd0318
 	}
cd0318
 	FIPS_POP_CONTEXT(APPROVED);
cd0318
+	gnutls_free(signature.data);
cd0318
 	gnutls_pubkey_deinit(pubkey);
cd0318
 	gnutls_privkey_deinit(privkey);
cd0318
 
cd0318
@@ -463,15 +472,24 @@ void doit(void)
cd0318
 	rsa_import_keypair(&privkey, &pubkey, "rsa-512.pem");
cd0318
 	FIPS_POP_CONTEXT(INITIAL);
cd0318
 
cd0318
-	/* Create a signature; not approved */
cd0318
+	/* Create a signature with 512-bit RSA and SHA256; not approved */
cd0318
 	FIPS_PUSH_CONTEXT();
cd0318
 	ret = gnutls_privkey_sign_data(privkey, GNUTLS_DIG_SHA256, 0,
cd0318
 				       &data, &signature);
cd0318
 	if (ret < 0) {
cd0318
 		fail("gnutls_privkey_sign_data failed\n");
cd0318
 	}
cd0318
-	gnutls_free(signature.data);
cd0318
 	FIPS_POP_CONTEXT(NOT_APPROVED);
cd0318
+
cd0318
+	/* Verify a signature with 512-bit RSA and SHA256; not approved */
cd0318
+	FIPS_PUSH_CONTEXT();
cd0318
+	ret = gnutls_pubkey_verify_data2(pubkey, GNUTLS_SIGN_RSA_SHA256, 0,
cd0318
+	                                 &data, &signature);
cd0318
+	if (ret < 0) {
cd0318
+		fail("gnutls_pubkey_verify_data2 failed\n");
cd0318
+	}
cd0318
+	FIPS_POP_CONTEXT(NOT_APPROVED);
cd0318
+	gnutls_free(signature.data);
cd0318
 	gnutls_pubkey_deinit(pubkey);
cd0318
 	gnutls_privkey_deinit(privkey);
cd0318
 
cd0318
-- 
cd0318
2.37.3
cd0318
cd0318
From 5a745120148861d873f47c1428c8c6dcadcf109b Mon Sep 17 00:00:00 2001
cd0318
From: Richard Costa <richard.costa@suse.com>
cd0318
Date: Sat, 9 Jul 2022 00:50:21 +0000
cd0318
Subject: [PATCH] Add self-test code inside a FIPS context
cd0318
cd0318
Self-test code exercise lots of different FIPS-related code with
cd0318
side-effects. So, in order to prevent it from losing information when
cd0318
executing inside another context, we create an appropriated one.
cd0318
cd0318
If the self-test fails, then the library is placed in error state, so it
cd0318
doesn't matter for other contexts.
cd0318
cd0318
Signed-off-by: Richard Maciel Costa <richard.costa@suse.com>
cd0318
---
cd0318
 lib/fips.c        | 19 +++++++++++++++++++
cd0318
 tests/fips-test.c | 20 ++++++++++++--------
cd0318
 2 files changed, 31 insertions(+), 8 deletions(-)
cd0318
cd0318
diff --git a/lib/fips.c b/lib/fips.c
cd0318
index 31a52a990f..7d143e608e 100644
cd0318
--- a/lib/fips.c
cd0318
+++ b/lib/fips.c
cd0318
@@ -902,6 +902,16 @@ gnutls_fips140_run_self_tests(void)
cd0318
 #ifdef ENABLE_FIPS140
cd0318
 	int ret;
cd0318
 	unsigned prev_lib_state;
cd0318
+	gnutls_fips140_context_t fips_context = NULL;
cd0318
+
cd0318
+	/* Save the FIPS context, because self tests change it */
cd0318
+	if (gnutls_fips140_mode_enabled() != GNUTLS_FIPS140_DISABLED) {
cd0318
+		if (gnutls_fips140_context_init(&fips_context) < 0 ||
cd0318
+		    gnutls_fips140_push_context(fips_context) < 0) {
cd0318
+			gnutls_fips140_context_deinit(fips_context);
cd0318
+			fips_context = NULL;
cd0318
+		}
cd0318
+	}
cd0318
 
cd0318
 	/* Temporarily switch to LIB_STATE_SELFTEST as some of the
cd0318
 	 * algorithms are implemented using special constructs in
cd0318
@@ -918,6 +928,15 @@ gnutls_fips140_run_self_tests(void)
cd0318
 		/* Restore the previous library state */
cd0318
 		_gnutls_switch_lib_state(prev_lib_state);
cd0318
 	}
cd0318
+
cd0318
+	/* Restore the previous FIPS context */
cd0318
+	if (gnutls_fips140_mode_enabled() != GNUTLS_FIPS140_DISABLED && fips_context) {
cd0318
+		if (gnutls_fips140_pop_context() < 0) {
cd0318
+			_gnutls_switch_lib_state(LIB_STATE_ERROR);
cd0318
+			_gnutls_audit_log(NULL, "FIPS140-2 context restoration failed\n");
cd0318
+		}
cd0318
+		gnutls_fips140_context_deinit(fips_context);
cd0318
+	}
cd0318
 	return ret;
cd0318
 #else
cd0318
 	return 0;
cd0318
diff --git a/tests/fips-test.c b/tests/fips-test.c
cd0318
index f9bd34586a..475b739197 100644
cd0318
--- a/tests/fips-test.c
cd0318
+++ b/tests/fips-test.c
cd0318
@@ -457,8 +457,9 @@ void doit(void)
cd0318
 
cd0318
 	/* Verify a signature created with 2432-bit RSA and SHA-1; approved */
cd0318
 	FIPS_PUSH_CONTEXT();
cd0318
-	ret = gnutls_pubkey_verify_data2(pubkey, GNUTLS_SIGN_RSA_SHA1, 0,
cd0318
-	                                 &data, &rsa2342_sha1_sig);
cd0318
+	ret = gnutls_pubkey_verify_data2(pubkey, GNUTLS_SIGN_RSA_SHA1,
cd0318
+					 GNUTLS_VERIFY_ALLOW_SIGN_WITH_SHA1, &data,
cd0318
+					 &rsa2342_sha1_sig);
cd0318
 	if (ret < 0) {
cd0318
 		fail("gnutls_pubkey_verify_data2 failed\n");
cd0318
 	}
cd0318
@@ -501,6 +502,15 @@ void doit(void)
cd0318
 	}
cd0318
 	FIPS_POP_CONTEXT(APPROVED);
cd0318
 
cd0318
+        /* run self-tests manually */
cd0318
+	FIPS_PUSH_CONTEXT();
cd0318
+	ret = gnutls_rnd(GNUTLS_RND_RANDOM, key16, sizeof(key16));
cd0318
+	ret = gnutls_fips140_run_self_tests();
cd0318
+	if (ret < 0) {
cd0318
+		fail("gnutls_fips140_run_self_tests failed\n");
cd0318
+	}
cd0318
+	FIPS_POP_CONTEXT(APPROVED);
cd0318
+
cd0318
 	/* Test when FIPS140 is set to error state */
cd0318
 	_gnutls_lib_simulate_error();
cd0318
 
cd0318
@@ -544,12 +554,6 @@ void doit(void)
cd0318
 
cd0318
 	gnutls_fips140_context_deinit(fips_context);
cd0318
 
cd0318
-	/* run self-tests manually */
cd0318
-	ret = gnutls_fips140_run_self_tests();
cd0318
-	if (ret < 0) {
cd0318
-		fail("gnutls_fips140_run_self_tests failed\n");
cd0318
-	}
cd0318
-
cd0318
 	gnutls_global_deinit();
cd0318
 	return;
cd0318
 }
cd0318
-- 
cd0318
2.37.3
cd0318