@@ -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
1327413281bool SoftHSM::detectFork (void ) {
0 commit comments