Skip to content

Commit

Permalink
Unbreak negative mechanism lists in slots.mechanisms + testcase
Browse files Browse the repository at this point in the history
Previously, when the list for slots.mechanisms was prefixed with
minus sign "-", the first mechanism was skipped as invalid and
therefore the tool was presenting wrong list of algorithms.

This fixes the initial index for selection of first algorithm
and adds unit test for this scenario.
  • Loading branch information
Jakuje committed Jan 12, 2021
1 parent 20a53bd commit cfe1f7f
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ src/lib/test/p11test
src/lib/test/softhsm2-alt.conf
src/lib/test/softhsm2-reset-on-fork.conf
src/lib/test/softhsm2-mech.conf
src/lib/test/softhsm2-negative-mech.conf
src/lib/test/softhsm2.conf
src/lib/test/tokens/64d6c3fe-1575-1736-1d26-5ccb28440ea7/
src/lib/test/tokens/dummy
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ AC_CONFIG_FILES([
src/lib/test/softhsm2-alt.conf
src/lib/test/softhsm2-reset-on-fork.conf
src/lib/test/softhsm2-mech.conf
src/lib/test/softhsm2-negative-mech.conf
src/lib/test/tokens/dummy
src/bin/Makefile
src/bin/common/Makefile
Expand Down
9 changes: 7 additions & 2 deletions src/lib/SoftHSM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -791,12 +791,17 @@ void SoftHSM::prepareSupportedMecahnisms(std::map<std::string, CK_MECHANISM_TYPE
if (mechs != "ALL")
{
bool negative = (mechs[0] == '-');
if (!negative)
size_t pos = 0, prev = 0;
if (negative)
{
/* Skip the minus sign */
prev = 1;
}
else
{
/* For positive list, we remove everything */
supportedMechanisms.clear();
}
size_t pos = 0, prev = 0;
std::string token;
do
{
Expand Down
70 changes: 68 additions & 2 deletions src/lib/test/InfoTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,9 @@ void InfoTests::testGetMechanismListConfig()
CK_MECHANISM_TYPE_PTR pMechanismList;

#ifndef _WIN32
setenv("SOFTHSM2_CONF", "./softhsm2-mech.conf", 1);
setenv("SOFTHSM2_CONF", "./softhsm2-mech.conf", 1);
#else
setenv("SOFTHSM2_CONF", ".\\softhsm2-mech.conf", 1);
setenv("SOFTHSM2_CONF", ".\\softhsm2-mech.conf", 1);
#endif

// Just make sure that we finalize any previous failed tests
Expand Down Expand Up @@ -363,6 +363,72 @@ void InfoTests::testGetMechanismListConfig()
#endif
}

void InfoTests::testGetMechanismNegativeListConfig()
{
CK_RV rv;
CK_ULONG ulMechCount = 0;
CK_MECHANISM_TYPE_PTR pMechanismList;
CK_ULONG allMechsCount = 0;

// Just make sure that we finalize any previous failed tests
CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );

// First of all, try to get the default list
rv = CRYPTOKI_F_PTR( C_GetMechanismList(m_initializedTokenSlotID, NULL_PTR, &ulMechCount) );
CPPUNIT_ASSERT(rv == CKR_CRYPTOKI_NOT_INITIALIZED);

rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) );
CPPUNIT_ASSERT(rv == CKR_OK);

// Get the size of the buffer
rv = CRYPTOKI_F_PTR( C_GetMechanismList(m_initializedTokenSlotID, NULL_PTR, &ulMechCount) );
CPPUNIT_ASSERT(rv == CKR_OK);
pMechanismList = (CK_MECHANISM_TYPE_PTR)malloc(ulMechCount * sizeof(CK_MECHANISM_TYPE_PTR));
/* Remember how many mechanisms are supported */
allMechsCount = ulMechCount;

// Get the mechanism list
rv = CRYPTOKI_F_PTR( C_GetMechanismList(m_initializedTokenSlotID, pMechanismList, &ulMechCount) );
CPPUNIT_ASSERT(rv == CKR_OK);
CPPUNIT_ASSERT_EQUAL(allMechsCount, ulMechCount);
free(pMechanismList);

CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
/* Now try with configuration having negative list */
#ifndef _WIN32
setenv("SOFTHSM2_CONF", "./softhsm2-negative-mech.conf", 1);
#else
setenv("SOFTHSM2_CONF", ".\\softhsm2-negative-mech.conf", 1);
#endif

rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) );
CPPUNIT_ASSERT(rv == CKR_OK);

// Get the size of the buffer
rv = CRYPTOKI_F_PTR( C_GetMechanismList(m_initializedTokenSlotID, NULL_PTR, &ulMechCount) );
CPPUNIT_ASSERT(rv == CKR_OK);
/* We should get 2 shorter */
//CPPUNIT_ASSERT_EQUAL(allMechsCount - 2, ulMechCount);
pMechanismList = (CK_MECHANISM_TYPE_PTR)malloc(ulMechCount * sizeof(CK_MECHANISM_TYPE_PTR));

// Get the mechanism list
rv = CRYPTOKI_F_PTR( C_GetMechanismList(m_initializedTokenSlotID, pMechanismList, &ulMechCount) );
CPPUNIT_ASSERT(rv == CKR_OK);
//CPPUNIT_ASSERT_EQUAL(allMechsCount - 2, ulMechCount);
for (unsigned long i = 0; i < ulMechCount; i++) {
CPPUNIT_ASSERT(pMechanismList[i] != CKM_RSA_X_509);
CPPUNIT_ASSERT(pMechanismList[i] != CKM_RSA_PKCS);
}
free(pMechanismList);

CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
#ifndef _WIN32
setenv("SOFTHSM2_CONF", "./softhsm2.conf", 1);
#else
setenv("SOFTHSM2_CONF", ".\\softhsm2.conf", 1);
#endif
}

void InfoTests::testWaitForSlotEvent()
{
CK_RV rv;
Expand Down
2 changes: 2 additions & 0 deletions src/lib/test/InfoTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class InfoTests : public TestsNoPINInitBase
CPPUNIT_TEST(testGetMechanismInfo);
CPPUNIT_TEST(testGetSlotInfoAlt);
CPPUNIT_TEST(testGetMechanismListConfig);
CPPUNIT_TEST(testGetMechanismNegativeListConfig);
CPPUNIT_TEST(testWaitForSlotEvent);
CPPUNIT_TEST_SUITE_END();

Expand All @@ -62,6 +63,7 @@ class InfoTests : public TestsNoPINInitBase
void testGetMechanismInfo();
void testGetSlotInfoAlt();
void testGetMechanismListConfig();
void testGetMechanismNegativeListConfig();
void testWaitForSlotEvent();
};

Expand Down
1 change: 1 addition & 0 deletions src/lib/test/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ EXTRA_DIST = $(srcdir)/CMakeLists.txt \
$(srcdir)/*.h \
$(srcdir)/softhsm2-alt.conf.win32 \
$(srcdir)/softhsm2-reset-on-fork.conf.win32 \
$(srcdir)/softhsm2-negative-mech.conf.win32 \
$(srcdir)/softhsm2-mech.conf.win32 \
$(srcdir)/softhsm2.conf.win32 \
$(srcdir)/tokens/dummy.in
8 changes: 8 additions & 0 deletions src/lib/test/softhsm2-negative-mech.conf.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# SoftHSM v2 configuration file

directories.tokendir = @builddir@/tokens
objectstore.backend = file
log.level = INFO
slots.removable = false
slots.mechanisms = -CKM_RSA_X_509,CKM_RSA_PKCS

7 changes: 7 additions & 0 deletions src/lib/test/softhsm2-negative-mech.conf.win32
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SoftHSM v2 configuration file

directories.tokendir = .\tokens
objectstore.backend = file
log.level = INFO
slots.removable = false
slots.mechanisms = -CKM_RSA_X_509,CKM_RSA_PKCS
2 changes: 2 additions & 0 deletions win32/p11test/p11test.vcxproj.in
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ copy ..\..\src\lib\test\softhsm2.conf.win32 "$(TargetDir)\softhsm2.conf"
copy ..\..\src\lib\test\softhsm2-alt.conf.win32 "$(TargetDir)\softhsm2-alt.conf"
copy ..\..\src\lib\test\softhsm2-reset-on-fork.conf.win32 "$(TargetDir)\softhsm2-reset-on-fork.conf"
copy ..\..\src\lib\test\softhsm2-mech.conf.win32 "$(TargetDir)\softhsm2-mech.conf"
copy ..\..\src\lib\test\softhsm2-negative-mech.conf.win32 "$(TargetDir)\softhsm2-negative-mech.conf"
mkdir "$(TargetDir)\tokens" 2&gt; nul
copy ..\..\src\lib\test\tokens\dummy.in "$(TargetDir)\tokens\dummy"
</Command>
Expand Down Expand Up @@ -99,6 +100,7 @@ copy ..\..\src\lib\test\softhsm2.conf.win32 "$(TargetDir)\softhsm2.conf"
copy ..\..\src\lib\test\softhsm2-alt.conf.win32 "$(TargetDir)\softhsm2-alt.conf"
copy ..\..\src\lib\test\softhsm2-reset-on-fork.conf.win32 "$(TargetDir)\softhsm2-reset-on-fork.conf"
copy ..\..\src\lib\test\softhsm2-mech.conf.win32 "$(TargetDir)\softhsm2-mech.conf"
copy ..\..\src\lib\test\softhsm2-negative-mech.conf.win32 "$(TargetDir)\softhsm2-negative-mech.conf"
mkdir "$(TargetDir)\tokens" 2&gt; nul
copy ..\..\src\lib\test\tokens\dummy.in "$(TargetDir)\tokens\dummy"
</Command>
Expand Down

0 comments on commit cfe1f7f

Please sign in to comment.