98d53f
From cfe1f7fdd12e202fa2d056c7fd731cfeee378a98 Mon Sep 17 00:00:00 2001
98d53f
From: Jakub Jelen <jjelen@redhat.com>
98d53f
Date: Wed, 15 Jul 2020 18:12:32 +0200
98d53f
Subject: [PATCH] Unbreak negative mechanism lists in slots.mechanisms +
98d53f
 testcase
98d53f
98d53f
Previously, when the list for slots.mechanisms was prefixed with
98d53f
minus sign "-", the first mechanism was skipped as invalid and
98d53f
therefore the tool was presenting wrong list of algorithms.
98d53f
98d53f
This fixes the initial index for selection of first algorithm
98d53f
and adds unit test for this scenario.
98d53f
---
98d53f
 .gitignore                                    |  1 +
98d53f
 configure.ac                                  |  1 +
98d53f
 src/lib/SoftHSM.cpp                           |  9 ++-
98d53f
 src/lib/test/InfoTests.cpp                    | 70 ++++++++++++++++++-
98d53f
 src/lib/test/InfoTests.h                      |  2 +
98d53f
 src/lib/test/Makefile.am                      |  1 +
98d53f
 src/lib/test/softhsm2-negative-mech.conf.in   |  8 +++
98d53f
 .../test/softhsm2-negative-mech.conf.win32    |  7 ++
98d53f
 win32/p11test/p11test.vcxproj.in              |  2 +
98d53f
 9 files changed, 97 insertions(+), 4 deletions(-)
98d53f
 create mode 100644 src/lib/test/softhsm2-negative-mech.conf.in
98d53f
 create mode 100644 src/lib/test/softhsm2-negative-mech.conf.win32
98d53f
98d53f
diff --git a/configure.ac b/configure.ac
98d53f
index d4dad435..c6a51c7a 100644
98d53f
--- a/configure.ac
98d53f
+++ b/configure.ac
98d53f
@@ -217,6 +217,7 @@ AC_CONFIG_FILES([
98d53f
 	src/lib/test/softhsm2-alt.conf
98d53f
 	src/lib/test/softhsm2-reset-on-fork.conf
98d53f
 	src/lib/test/softhsm2-mech.conf
98d53f
+	src/lib/test/softhsm2-negative-mech.conf
98d53f
 	src/lib/test/tokens/dummy
98d53f
 	src/bin/Makefile
98d53f
 	src/bin/common/Makefile
98d53f
diff --git a/src/lib/SoftHSM.cpp b/src/lib/SoftHSM.cpp
98d53f
index 0a0c32cc..cac724e6 100644
98d53f
--- a/src/lib/SoftHSM.cpp
98d53f
+++ b/src/lib/SoftHSM.cpp
98d53f
@@ -791,12 +791,17 @@ void SoftHSM::prepareSupportedMecahnisms(std::map
98d53f
 	if (mechs != "ALL")
98d53f
 	{
98d53f
 		bool negative = (mechs[0] == '-');
98d53f
-		if (!negative)
98d53f
+		size_t pos = 0, prev = 0;
98d53f
+		if (negative)
98d53f
+		{
98d53f
+			/* Skip the minus sign */
98d53f
+			prev = 1;
98d53f
+		}
98d53f
+		else
98d53f
 		{
98d53f
 			/* For positive list, we remove everything */
98d53f
 			supportedMechanisms.clear();
98d53f
 		}
98d53f
-		size_t pos = 0, prev = 0;
98d53f
 		std::string token;
98d53f
 		do
98d53f
 		{
98d53f
diff --git a/src/lib/test/InfoTests.cpp b/src/lib/test/InfoTests.cpp
98d53f
index a07956fb..d2218e34 100644
98d53f
--- a/src/lib/test/InfoTests.cpp
98d53f
+++ b/src/lib/test/InfoTests.cpp
98d53f
@@ -328,9 +328,9 @@ void InfoTests::testGetMechanismListConfig()
98d53f
 	CK_MECHANISM_TYPE_PTR pMechanismList;
98d53f
 
98d53f
 #ifndef _WIN32
98d53f
-    setenv("SOFTHSM2_CONF", "./softhsm2-mech.conf", 1);
98d53f
+	setenv("SOFTHSM2_CONF", "./softhsm2-mech.conf", 1);
98d53f
 #else
98d53f
-    setenv("SOFTHSM2_CONF", ".\\softhsm2-mech.conf", 1);
98d53f
+	setenv("SOFTHSM2_CONF", ".\\softhsm2-mech.conf", 1);
98d53f
 #endif
98d53f
 
98d53f
 	// Just make sure that we finalize any previous failed tests
98d53f
@@ -363,6 +363,72 @@ void InfoTests::testGetMechanismListConfig()
98d53f
 #endif
98d53f
 }
98d53f
 
98d53f
+void InfoTests::testGetMechanismNegativeListConfig()
98d53f
+{
98d53f
+	CK_RV rv;
98d53f
+	CK_ULONG ulMechCount = 0;
98d53f
+	CK_MECHANISM_TYPE_PTR pMechanismList;
98d53f
+	CK_ULONG allMechsCount = 0;
98d53f
+
98d53f
+	// Just make sure that we finalize any previous failed tests
98d53f
+	CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
98d53f
+
98d53f
+	// First of all, try to get the default list
98d53f
+	rv = CRYPTOKI_F_PTR( C_GetMechanismList(m_initializedTokenSlotID, NULL_PTR, &ulMechCount) );
98d53f
+	CPPUNIT_ASSERT(rv == CKR_CRYPTOKI_NOT_INITIALIZED);
98d53f
+
98d53f
+	rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) );
98d53f
+	CPPUNIT_ASSERT(rv == CKR_OK);
98d53f
+
98d53f
+	// Get the size of the buffer
98d53f
+	rv = CRYPTOKI_F_PTR( C_GetMechanismList(m_initializedTokenSlotID, NULL_PTR, &ulMechCount) );
98d53f
+	CPPUNIT_ASSERT(rv == CKR_OK);
98d53f
+	pMechanismList = (CK_MECHANISM_TYPE_PTR)malloc(ulMechCount * sizeof(CK_MECHANISM_TYPE_PTR));
98d53f
+	/* Remember how many mechanisms are supported */
98d53f
+	allMechsCount = ulMechCount;
98d53f
+
98d53f
+	// Get the mechanism list
98d53f
+	rv = CRYPTOKI_F_PTR( C_GetMechanismList(m_initializedTokenSlotID, pMechanismList, &ulMechCount) );
98d53f
+	CPPUNIT_ASSERT(rv == CKR_OK);
98d53f
+	CPPUNIT_ASSERT_EQUAL(allMechsCount, ulMechCount);
98d53f
+	free(pMechanismList);
98d53f
+
98d53f
+	CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
98d53f
+	/* Now try with configuration having negative list */
98d53f
+#ifndef _WIN32
98d53f
+	setenv("SOFTHSM2_CONF", "./softhsm2-negative-mech.conf", 1);
98d53f
+#else
98d53f
+	setenv("SOFTHSM2_CONF", ".\\softhsm2-negative-mech.conf", 1);
98d53f
+#endif
98d53f
+
98d53f
+	rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) );
98d53f
+	CPPUNIT_ASSERT(rv == CKR_OK);
98d53f
+
98d53f
+	// Get the size of the buffer
98d53f
+	rv = CRYPTOKI_F_PTR( C_GetMechanismList(m_initializedTokenSlotID, NULL_PTR, &ulMechCount) );
98d53f
+	CPPUNIT_ASSERT(rv == CKR_OK);
98d53f
+	/* We should get 2 shorter */
98d53f
+	//CPPUNIT_ASSERT_EQUAL(allMechsCount - 2, ulMechCount);
98d53f
+	pMechanismList = (CK_MECHANISM_TYPE_PTR)malloc(ulMechCount * sizeof(CK_MECHANISM_TYPE_PTR));
98d53f
+
98d53f
+	// Get the mechanism list
98d53f
+	rv = CRYPTOKI_F_PTR( C_GetMechanismList(m_initializedTokenSlotID, pMechanismList, &ulMechCount) );
98d53f
+	CPPUNIT_ASSERT(rv == CKR_OK);
98d53f
+	//CPPUNIT_ASSERT_EQUAL(allMechsCount - 2, ulMechCount);
98d53f
+	for (unsigned long i = 0; i < ulMechCount; i++) {
98d53f
+		CPPUNIT_ASSERT(pMechanismList[i] != CKM_RSA_X_509);
98d53f
+		CPPUNIT_ASSERT(pMechanismList[i] != CKM_RSA_PKCS);
98d53f
+	}
98d53f
+	free(pMechanismList);
98d53f
+
98d53f
+	CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
98d53f
+#ifndef _WIN32
98d53f
+	setenv("SOFTHSM2_CONF", "./softhsm2.conf", 1);
98d53f
+#else
98d53f
+	setenv("SOFTHSM2_CONF", ".\\softhsm2.conf", 1);
98d53f
+#endif
98d53f
+}
98d53f
+
98d53f
 void InfoTests::testWaitForSlotEvent()
98d53f
 {
98d53f
 	CK_RV rv;
98d53f
diff --git a/src/lib/test/InfoTests.h b/src/lib/test/InfoTests.h
98d53f
index dfd02953..1cc99ccb 100644
98d53f
--- a/src/lib/test/InfoTests.h
98d53f
+++ b/src/lib/test/InfoTests.h
98d53f
@@ -49,6 +49,7 @@ class InfoTests : public TestsNoPINInitBase
98d53f
 	CPPUNIT_TEST(testGetMechanismInfo);
98d53f
 	CPPUNIT_TEST(testGetSlotInfoAlt);
98d53f
 	CPPUNIT_TEST(testGetMechanismListConfig);
98d53f
+	CPPUNIT_TEST(testGetMechanismNegativeListConfig);
98d53f
 	CPPUNIT_TEST(testWaitForSlotEvent);
98d53f
 	CPPUNIT_TEST_SUITE_END();
98d53f
 
98d53f
@@ -62,6 +63,7 @@ class InfoTests : public TestsNoPINInitBase
98d53f
 	void testGetMechanismInfo();
98d53f
 	void testGetSlotInfoAlt();
98d53f
 	void testGetMechanismListConfig();
98d53f
+	void testGetMechanismNegativeListConfig();
98d53f
 	void testWaitForSlotEvent();
98d53f
 };
98d53f
 
98d53f
diff --git a/src/lib/test/Makefile.am b/src/lib/test/Makefile.am
98d53f
index 17887dd4..a22ce668 100644
98d53f
--- a/src/lib/test/Makefile.am
98d53f
+++ b/src/lib/test/Makefile.am
98d53f
@@ -39,6 +39,7 @@ EXTRA_DIST =			$(srcdir)/CMakeLists.txt \
98d53f
 				$(srcdir)/*.h \
98d53f
 				$(srcdir)/softhsm2-alt.conf.win32 \
98d53f
 				$(srcdir)/softhsm2-reset-on-fork.conf.win32 \
98d53f
+				$(srcdir)/softhsm2-negative-mech.conf.win32 \
98d53f
 				$(srcdir)/softhsm2-mech.conf.win32 \
98d53f
 				$(srcdir)/softhsm2.conf.win32 \
98d53f
 				$(srcdir)/tokens/dummy.in
98d53f
diff --git a/src/lib/test/softhsm2-negative-mech.conf.in b/src/lib/test/softhsm2-negative-mech.conf.in
98d53f
new file mode 100644
98d53f
index 00000000..51f7e6ac
98d53f
--- /dev/null
98d53f
+++ b/src/lib/test/softhsm2-negative-mech.conf.in
98d53f
@@ -0,0 +1,8 @@
98d53f
+# SoftHSM v2 configuration file
98d53f
+
98d53f
+directories.tokendir = @builddir@/tokens
98d53f
+objectstore.backend = file
98d53f
+log.level = INFO
98d53f
+slots.removable = false
98d53f
+slots.mechanisms = -CKM_RSA_X_509,CKM_RSA_PKCS
98d53f
+
98d53f
diff --git a/src/lib/test/softhsm2-negative-mech.conf.win32 b/src/lib/test/softhsm2-negative-mech.conf.win32
98d53f
new file mode 100644
98d53f
index 00000000..a3aefb96
98d53f
--- /dev/null
98d53f
+++ b/src/lib/test/softhsm2-negative-mech.conf.win32
98d53f
@@ -0,0 +1,7 @@
98d53f
+# SoftHSM v2 configuration file
98d53f
+
98d53f
+directories.tokendir = .\tokens
98d53f
+objectstore.backend = file
98d53f
+log.level = INFO
98d53f
+slots.removable = false
98d53f
+slots.mechanisms = -CKM_RSA_X_509,CKM_RSA_PKCS
98d53f
diff --git a/win32/p11test/p11test.vcxproj.in b/win32/p11test/p11test.vcxproj.in
98d53f
index 55dfb087..88859bca 100644
98d53f
--- a/win32/p11test/p11test.vcxproj.in
98d53f
+++ b/win32/p11test/p11test.vcxproj.in
98d53f
@@ -67,6 +67,7 @@ copy ..\..\src\lib\test\softhsm2.conf.win32 "$(TargetDir)\softhsm2.conf"
98d53f
 copy ..\..\src\lib\test\softhsm2-alt.conf.win32 "$(TargetDir)\softhsm2-alt.conf"
98d53f
 copy ..\..\src\lib\test\softhsm2-reset-on-fork.conf.win32 "$(TargetDir)\softhsm2-reset-on-fork.conf"
98d53f
 copy ..\..\src\lib\test\softhsm2-mech.conf.win32 "$(TargetDir)\softhsm2-mech.conf"
98d53f
+copy ..\..\src\lib\test\softhsm2-negative-mech.conf.win32 "$(TargetDir)\softhsm2-negative-mech.conf"
98d53f
 mkdir "$(TargetDir)\tokens" 2> nul
98d53f
 copy ..\..\src\lib\test\tokens\dummy.in "$(TargetDir)\tokens\dummy"
98d53f
       </Command>
98d53f
@@ -99,6 +100,7 @@ copy ..\..\src\lib\test\softhsm2.conf.win32 "$(TargetDir)\softhsm2.conf"
98d53f
 copy ..\..\src\lib\test\softhsm2-alt.conf.win32 "$(TargetDir)\softhsm2-alt.conf"
98d53f
 copy ..\..\src\lib\test\softhsm2-reset-on-fork.conf.win32 "$(TargetDir)\softhsm2-reset-on-fork.conf"
98d53f
 copy ..\..\src\lib\test\softhsm2-mech.conf.win32 "$(TargetDir)\softhsm2-mech.conf"
98d53f
+copy ..\..\src\lib\test\softhsm2-negative-mech.conf.win32 "$(TargetDir)\softhsm2-negative-mech.conf"
98d53f
 mkdir "$(TargetDir)\tokens" 2> nul
98d53f
 copy ..\..\src\lib\test\tokens\dummy.in "$(TargetDir)\tokens\dummy"
98d53f
       </Command>