Skip to content

Commit 49786f9

Browse files
committed
Respect allowed mechanisms also in C_GetMechanismInfo
Signed-off-by: Jakub Jelen <[email protected]>
1 parent a97993c commit 49786f9

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

src/lib/SoftHSM.cpp

+23-17
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,8 @@ CK_RV SoftHSM::C_GetMechanismInfo(CK_SLOT_ID slotID, CK_MECHANISM_TYPE type, CK_
931931
{
932932
return CKR_SLOT_ID_INVALID;
933933
}
934+
if (!isMechanismPermitted(NULL, type))
935+
return CKR_MECHANISM_INVALID;
934936

935937
AsymmetricAlgorithm* rsa = CryptoFactory::i()->getAsymmetricAlgorithm(AsymAlgo::RSA);
936938
if (rsa != NULL)
@@ -2183,7 +2185,7 @@ CK_RV SoftHSM::SymEncryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMech
21832185
return CKR_KEY_FUNCTION_NOT_PERMITTED;
21842186

21852187
// Check if the specified mechanism is allowed for the key
2186-
if (!isMechanismPermitted(key, pMechanism))
2188+
if (!isMechanismPermitted(key, pMechanism->mechanism))
21872189
return CKR_MECHANISM_INVALID;
21882190

21892191
// Get key info
@@ -2903,7 +2905,7 @@ CK_RV SoftHSM::SymDecryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMech
29032905

29042906

29052907
// Check if the specified mechanism is allowed for the key
2906-
if (!isMechanismPermitted(key, pMechanism))
2908+
if (!isMechanismPermitted(key, pMechanism->mechanism))
29072909
return CKR_MECHANISM_INVALID;
29082910

29092911
// Get key info
@@ -3150,7 +3152,7 @@ CK_RV SoftHSM::AsymDecryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMec
31503152
return CKR_KEY_FUNCTION_NOT_PERMITTED;
31513153

31523154
// Check if the specified mechanism is allowed for the key
3153-
if (!isMechanismPermitted(key, pMechanism))
3155+
if (!isMechanismPermitted(key, pMechanism->mechanism))
31543156
return CKR_MECHANISM_INVALID;
31553157

31563158
// Get key info
@@ -3944,7 +3946,7 @@ CK_RV SoftHSM::MacSignInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechani
39443946
return CKR_KEY_FUNCTION_NOT_PERMITTED;
39453947

39463948
// Check if the specified mechanism is allowed for the key
3947-
if (!isMechanismPermitted(key, pMechanism))
3949+
if (!isMechanismPermitted(key, pMechanism->mechanism))
39483950
return CKR_MECHANISM_INVALID;
39493951

39503952
// Get key info
@@ -4096,7 +4098,7 @@ CK_RV SoftHSM::AsymSignInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechan
40964098
return CKR_KEY_FUNCTION_NOT_PERMITTED;
40974099

40984100
// Check if the specified mechanism is allowed for the key
4099-
if (!isMechanismPermitted(key, pMechanism))
4101+
if (!isMechanismPermitted(key, pMechanism->mechanism))
41004102
return CKR_MECHANISM_INVALID;
41014103

41024104
// Get the asymmetric algorithm matching the mechanism
@@ -4922,7 +4924,7 @@ CK_RV SoftHSM::MacVerifyInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMecha
49224924
return CKR_KEY_FUNCTION_NOT_PERMITTED;
49234925

49244926
// Check if the specified mechanism is allowed for the key
4925-
if (!isMechanismPermitted(key, pMechanism))
4927+
if (!isMechanismPermitted(key, pMechanism->mechanism))
49264928
return CKR_MECHANISM_INVALID;
49274929

49284930
// Get key info
@@ -5074,7 +5076,7 @@ CK_RV SoftHSM::AsymVerifyInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMech
50745076
return CKR_KEY_FUNCTION_NOT_PERMITTED;
50755077

50765078
// Check if the specified mechanism is allowed for the key
5077-
if (!isMechanismPermitted(key, pMechanism))
5079+
if (!isMechanismPermitted(key, pMechanism->mechanism))
50785080
return CKR_MECHANISM_INVALID;
50795081

50805082
// Get the asymmetric algorithm matching the mechanism
@@ -6527,7 +6529,7 @@ CK_RV SoftHSM::C_WrapKey
65276529
return CKR_KEY_FUNCTION_NOT_PERMITTED;
65286530

65296531
// Check if the specified mechanism is allowed for the wrapping key
6530-
if (!isMechanismPermitted(wrapKey, pMechanism))
6532+
if (!isMechanismPermitted(wrapKey, pMechanism->mechanism))
65316533
return CKR_MECHANISM_INVALID;
65326534

65336535
// Check the to be wrapped key handle.
@@ -6993,7 +6995,7 @@ CK_RV SoftHSM::C_UnwrapKey
69936995
return CKR_KEY_FUNCTION_NOT_PERMITTED;
69946996

69956997
// Check if the specified mechanism is allowed for the unwrap key
6996-
if (!isMechanismPermitted(unwrapKey, pMechanism))
6998+
if (!isMechanismPermitted(unwrapKey, pMechanism->mechanism))
69976999
return CKR_MECHANISM_INVALID;
69987000

69997001
// Extract information from the template that is needed to create the object.
@@ -7274,7 +7276,7 @@ CK_RV SoftHSM::C_DeriveKey
72747276
return CKR_KEY_FUNCTION_NOT_PERMITTED;
72757277

72767278
// Check if the specified mechanism is allowed for the key
7277-
if (!isMechanismPermitted(key, pMechanism))
7279+
if (!isMechanismPermitted(key, pMechanism->mechanism))
72787280
return CKR_MECHANISM_INVALID;
72797281

72807282
// Extract information from the template that is needed to create the object.
@@ -12829,22 +12831,26 @@ CK_RV SoftHSM::MechParamCheckRSAPKCSOAEP(CK_MECHANISM_PTR pMechanism)
1282912831
return CKR_OK;
1283012832
}
1283112833

12832-
bool SoftHSM::isMechanismPermitted(OSObject* key, CK_MECHANISM_PTR pMechanism)
12834+
bool SoftHSM::isMechanismPermitted(OSObject* key, CK_MECHANISM_TYPE mechanism)
1283312835
{
1283412836
std::list<CK_MECHANISM_TYPE> mechs = supportedMechanisms;
1283512837
/* First check if the algorithm is enabled in the global configuration */
12836-
auto it = std::find(mechs.begin(), mechs.end(), pMechanism->mechanism);
12838+
auto it = std::find(mechs.begin(), mechs.end(), mechanism);
1283712839
if (it == mechs.end())
1283812840
return false;
1283912841

12840-
OSAttribute attribute = key->getAttribute(CKA_ALLOWED_MECHANISMS);
12841-
std::set<CK_MECHANISM_TYPE> allowed = attribute.getMechanismTypeSetValue();
12842+
/* If we have object, consult also its allowed mechanisms */
12843+
if (key) {
12844+
OSAttribute attribute = key->getAttribute(CKA_ALLOWED_MECHANISMS);
12845+
std::set<CK_MECHANISM_TYPE> allowed = attribute.getMechanismTypeSetValue();
1284212846

12843-
if (allowed.empty()) {
12847+
if (allowed.empty()) {
12848+
return true;
12849+
}
12850+
return allowed.find(mechanism) != allowed.end();
12851+
} else {
1284412852
return true;
1284512853
}
12846-
12847-
return allowed.find(pMechanism->mechanism) != allowed.end();
1284812854
}
1284912855

1285012856
bool SoftHSM::detectFork(void) {

src/lib/SoftHSM.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ class SoftHSM
489489

490490
CK_RV MechParamCheckRSAPKCSOAEP(CK_MECHANISM_PTR pMechanism);
491491

492-
bool isMechanismPermitted(OSObject* key, CK_MECHANISM_PTR pMechanism);
492+
bool isMechanismPermitted(OSObject* key, CK_MECHANISM_TYPE pMechanism);
493493
void prepareSupportedMecahnisms(std::map<std::string, CK_MECHANISM_TYPE> &t);
494494
bool detectFork(void);
495495
};

0 commit comments

Comments
 (0)