Skip to content

Commit 7ff177d

Browse files
authored
Fix C_GetMechanismInfo to fail on non-allowed mechanisms (#648)
* Respect allowed mechanisms also in C_GetMechanismInfo * tests: Verify the C_GetMechanismInfo does fails for not allowed mechanisms Signed-off-by: Jakub Jelen <[email protected]>
1 parent 7ec2a06 commit 7ff177d

File tree

3 files changed

+40
-18
lines changed

3 files changed

+40
-18
lines changed

src/lib/SoftHSM.cpp

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,8 @@ CK_RV SoftHSM::C_GetMechanismInfo(CK_SLOT_ID slotID, CK_MECHANISM_TYPE type, CK_
938938
{
939939
return CKR_SLOT_ID_INVALID;
940940
}
941+
if (!isMechanismPermitted(NULL, type))
942+
return CKR_MECHANISM_INVALID;
941943

942944
AsymmetricAlgorithm* rsa = CryptoFactory::i()->getAsymmetricAlgorithm(AsymAlgo::RSA);
943945
if (rsa != NULL)
@@ -2205,7 +2207,7 @@ CK_RV SoftHSM::SymEncryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMech
22052207
return CKR_KEY_FUNCTION_NOT_PERMITTED;
22062208

22072209
// Check if the specified mechanism is allowed for the key
2208-
if (!isMechanismPermitted(key, pMechanism))
2210+
if (!isMechanismPermitted(key, pMechanism->mechanism))
22092211
return CKR_MECHANISM_INVALID;
22102212

22112213
// Get key info
@@ -2935,7 +2937,7 @@ CK_RV SoftHSM::SymDecryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMech
29352937

29362938

29372939
// Check if the specified mechanism is allowed for the key
2938-
if (!isMechanismPermitted(key, pMechanism))
2940+
if (!isMechanismPermitted(key, pMechanism->mechanism))
29392941
return CKR_MECHANISM_INVALID;
29402942

29412943
// Get key info
@@ -3183,7 +3185,7 @@ CK_RV SoftHSM::AsymDecryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMec
31833185
return CKR_KEY_FUNCTION_NOT_PERMITTED;
31843186

31853187
// Check if the specified mechanism is allowed for the key
3186-
if (!isMechanismPermitted(key, pMechanism))
3188+
if (!isMechanismPermitted(key, pMechanism->mechanism))
31873189
return CKR_MECHANISM_INVALID;
31883190

31893191
// Get key info
@@ -3985,7 +3987,7 @@ CK_RV SoftHSM::MacSignInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechani
39853987
return CKR_KEY_FUNCTION_NOT_PERMITTED;
39863988

39873989
// Check if the specified mechanism is allowed for the key
3988-
if (!isMechanismPermitted(key, pMechanism))
3990+
if (!isMechanismPermitted(key, pMechanism->mechanism))
39893991
return CKR_MECHANISM_INVALID;
39903992

39913993
// Get key info
@@ -4137,7 +4139,7 @@ CK_RV SoftHSM::AsymSignInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechan
41374139
return CKR_KEY_FUNCTION_NOT_PERMITTED;
41384140

41394141
// Check if the specified mechanism is allowed for the key
4140-
if (!isMechanismPermitted(key, pMechanism))
4142+
if (!isMechanismPermitted(key, pMechanism->mechanism))
41414143
return CKR_MECHANISM_INVALID;
41424144

41434145
// Get the asymmetric algorithm matching the mechanism
@@ -4988,7 +4990,7 @@ CK_RV SoftHSM::MacVerifyInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMecha
49884990
return CKR_KEY_FUNCTION_NOT_PERMITTED;
49894991

49904992
// Check if the specified mechanism is allowed for the key
4991-
if (!isMechanismPermitted(key, pMechanism))
4993+
if (!isMechanismPermitted(key, pMechanism->mechanism))
49924994
return CKR_MECHANISM_INVALID;
49934995

49944996
// Get key info
@@ -5140,7 +5142,7 @@ CK_RV SoftHSM::AsymVerifyInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMech
51405142
return CKR_KEY_FUNCTION_NOT_PERMITTED;
51415143

51425144
// Check if the specified mechanism is allowed for the key
5143-
if (!isMechanismPermitted(key, pMechanism))
5145+
if (!isMechanismPermitted(key, pMechanism->mechanism))
51445146
return CKR_MECHANISM_INVALID;
51455147

51465148
// Get the asymmetric algorithm matching the mechanism
@@ -6721,7 +6723,7 @@ CK_RV SoftHSM::C_WrapKey
67216723
return CKR_KEY_FUNCTION_NOT_PERMITTED;
67226724

67236725
// Check if the specified mechanism is allowed for the wrapping key
6724-
if (!isMechanismPermitted(wrapKey, pMechanism))
6726+
if (!isMechanismPermitted(wrapKey, pMechanism->mechanism))
67256727
return CKR_MECHANISM_INVALID;
67266728

67276729
// Check the to be wrapped key handle.
@@ -7350,7 +7352,7 @@ CK_RV SoftHSM::C_UnwrapKey
73507352
return CKR_KEY_FUNCTION_NOT_PERMITTED;
73517353

73527354
// Check if the specified mechanism is allowed for the unwrap key
7353-
if (!isMechanismPermitted(unwrapKey, pMechanism))
7355+
if (!isMechanismPermitted(unwrapKey, pMechanism->mechanism))
73547356
return CKR_MECHANISM_INVALID;
73557357

73567358
// Extract information from the template that is needed to create the object.
@@ -7645,7 +7647,7 @@ CK_RV SoftHSM::C_DeriveKey
76457647
return CKR_KEY_FUNCTION_NOT_PERMITTED;
76467648

76477649
// Check if the specified mechanism is allowed for the key
7648-
if (!isMechanismPermitted(key, pMechanism))
7650+
if (!isMechanismPermitted(key, pMechanism->mechanism))
76497651
return CKR_MECHANISM_INVALID;
76507652

76517653
// Extract information from the template that is needed to create the object.
@@ -13253,22 +13255,27 @@ CK_RV SoftHSM::MechParamCheckRSAAESKEYWRAP(CK_MECHANISM_PTR pMechanism)
1325313255
return CKR_OK;
1325413256
}
1325513257

13256-
bool SoftHSM::isMechanismPermitted(OSObject* key, CK_MECHANISM_PTR pMechanism)
13258+
bool SoftHSM::isMechanismPermitted(OSObject* key, CK_MECHANISM_TYPE mechanism)
1325713259
{
1325813260
std::list<CK_MECHANISM_TYPE> mechs = supportedMechanisms;
1325913261
/* First check if the algorithm is enabled in the global configuration */
13260-
auto it = std::find(mechs.begin(), mechs.end(), pMechanism->mechanism);
13262+
auto it = std::find(mechs.begin(), mechs.end(), mechanism);
1326113263
if (it == mechs.end())
1326213264
return false;
1326313265

13264-
OSAttribute attribute = key->getAttribute(CKA_ALLOWED_MECHANISMS);
13265-
std::set<CK_MECHANISM_TYPE> allowed = attribute.getMechanismTypeSetValue();
13266+
/* If we have object, consult also its allowed mechanisms */
13267+
if (key) {
13268+
OSAttribute attribute = key->getAttribute(CKA_ALLOWED_MECHANISMS);
13269+
std::set<CK_MECHANISM_TYPE> allowed = attribute.getMechanismTypeSetValue();
1326613270

13267-
if (allowed.empty()) {
13271+
/* empty allow list means we allowing everything that is built-in */
13272+
if (allowed.empty()) {
13273+
return true;
13274+
}
13275+
return allowed.find(mechanism) != allowed.end();
13276+
} else {
1326813277
return true;
1326913278
}
13270-
13271-
return allowed.find(pMechanism->mechanism) != allowed.end();
1327213279
}
1327313280

1327413281
bool SoftHSM::detectFork(void) {

src/lib/SoftHSM.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ class SoftHSM
510510
CK_RV MechParamCheckRSAPKCSOAEP(CK_MECHANISM_PTR pMechanism);
511511
CK_RV MechParamCheckRSAAESKEYWRAP(CK_MECHANISM_PTR pMechanism);
512512

513-
bool isMechanismPermitted(OSObject* key, CK_MECHANISM_PTR pMechanism);
513+
bool isMechanismPermitted(OSObject* key, CK_MECHANISM_TYPE mechanism);
514514
void prepareSupportedMechanisms(std::map<std::string, CK_MECHANISM_TYPE> &t);
515515
bool detectFork(void);
516516
};

src/lib/test/InfoTests.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ void InfoTests::testGetMechanismListConfig()
329329
CK_RV rv;
330330
CK_ULONG ulMechCount = 0;
331331
CK_MECHANISM_TYPE_PTR pMechanismList;
332+
CK_MECHANISM_INFO info;
332333

333334
#ifndef _WIN32
334335
setenv("SOFTHSM2_CONF", "./softhsm2-mech.conf", 1);
@@ -358,6 +359,20 @@ void InfoTests::testGetMechanismListConfig()
358359
CPPUNIT_ASSERT(pMechanismList[1] == CKM_RSA_PKCS);
359360
free(pMechanismList);
360361

362+
/* Get good mechanism info */
363+
rv = CRYPTOKI_F_PTR( C_GetMechanismInfo(m_initializedTokenSlotID, CKM_RSA_X_509, &info) );
364+
CPPUNIT_ASSERT(rv == CKR_OK);
365+
CPPUNIT_ASSERT(info.flags & CKF_SIGN);
366+
rv = CRYPTOKI_F_PTR( C_GetMechanismInfo(m_initializedTokenSlotID, CKM_RSA_PKCS, &info) );
367+
CPPUNIT_ASSERT(rv == CKR_OK);
368+
CPPUNIT_ASSERT(info.flags & CKF_SIGN);
369+
370+
/* Get bad mechanism info */
371+
rv = CRYPTOKI_F_PTR( C_GetMechanismInfo(m_initializedTokenSlotID, CKM_ECDSA, &info) );
372+
CPPUNIT_ASSERT(rv == CKR_MECHANISM_INVALID);
373+
rv = CRYPTOKI_F_PTR( C_GetMechanismInfo(m_initializedTokenSlotID, CKM_DSA, &info) );
374+
CPPUNIT_ASSERT(rv == CKR_MECHANISM_INVALID);
375+
361376
CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
362377
#ifndef _WIN32
363378
setenv("SOFTHSM2_CONF", "./softhsm2.conf", 1);

0 commit comments

Comments
 (0)