Skip to content

Commit f0fbfab

Browse files
authored
Merge pull request #178 from LinuxJedi/f-fixes7
Static code analysis fixes
2 parents 998d738 + 8dbbb2a commit f0fbfab

10 files changed

Lines changed: 1113 additions & 80 deletions

File tree

src/crypto.c

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3960,7 +3960,10 @@ CK_RV C_DigestInit(CK_SESSION_HANDLE hSession,
39603960
WP11_Session_SetOpInitialized(session, init);
39613961
}
39623962

3963-
rv = ret;
3963+
if (ret != 0 && ret != (int)CKR_MECHANISM_INVALID)
3964+
rv = CKR_FUNCTION_FAILED;
3965+
else
3966+
rv = ret;
39643967
WOLFPKCS11_LEAVE("C_DigestInit", rv);
39653968
return rv;
39663969
}
@@ -4063,7 +4066,9 @@ CK_RV C_DigestUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
40634066

40644067
ret = WP11_Digest_Update(pPart, (word32)ulPartLen, session);
40654068

4066-
return ret;
4069+
if (ret < 0)
4070+
return CKR_FUNCTION_FAILED;
4071+
return CKR_OK;
40674072
}
40684073

40694074
/**
@@ -4106,7 +4111,11 @@ CK_RV C_DigestKey(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hKey)
41064111

41074112
ret = WP11_Digest_Key(obj, session);
41084113

4109-
return ret;
4114+
if (ret < 0)
4115+
return CKR_FUNCTION_FAILED;
4116+
if (ret > 0)
4117+
return (CK_RV)ret;
4118+
return CKR_OK;
41104119
}
41114120

41124121
/**
@@ -6347,7 +6356,7 @@ CK_RV C_VerifyRecoverInit(CK_SESSION_HANDLE hSession,
63476356
return CKR_KEY_TYPE_INCONSISTENT;
63486357
}
63496358

6350-
ret = CheckOpSupported(obj, CKA_VERIFY);
6359+
ret = CheckOpSupported(obj, CKA_VERIFY_RECOVER);
63516360
if (ret != CKR_OK)
63526361
return ret;
63536362

@@ -6717,9 +6726,6 @@ CK_RV C_GenerateKey(CK_SESSION_HANDLE hSession,
67176726
CK_RV rv = CKR_OK;
67186727
WP11_Session* session = NULL;
67196728
WP11_Object* key = NULL;
6720-
CK_BBOOL trueVar = CK_TRUE;
6721-
CK_BBOOL getVar;
6722-
CK_ULONG getVarLen = sizeof(CK_BBOOL);
67236729
CK_KEY_TYPE keyType;
67246730

67256731
WOLFPKCS11_ENTER("C_GenerateKey");
@@ -6928,18 +6934,22 @@ CK_RV C_GenerateKey(CK_SESSION_HANDLE hSession,
69286934

69296935
ret = WP11_Object_SetSecretKey(pbkdf2Key, secretKeyData, secretKeyLen);
69306936
if (ret == 0) {
6931-
rv = AddObject(session, pbkdf2Key, pTemplate, ulCount, phKey);
6932-
if (rv != CKR_OK) {
6933-
WP11_Object_Free(pbkdf2Key);
6934-
}
6937+
WP11_Object_SetKeyGeneration(pbkdf2Key, pMechanism->mechanism);
6938+
rv = SetInitialStates(pbkdf2Key);
69356939
} else {
6936-
WP11_Object_Free(pbkdf2Key);
69376940
rv = CKR_FUNCTION_FAILED;
69386941
}
6942+
if (rv == CKR_OK) {
6943+
rv = AddObject(session, pbkdf2Key, pTemplate, ulCount, phKey);
6944+
}
6945+
if (rv != CKR_OK) {
6946+
WP11_Object_Free(pbkdf2Key);
6947+
}
69396948
}
69406949

69416950
wc_ForceZero(derivedKey, derivedKeyLen);
69426951
XFREE(derivedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
6952+
69436953
return rv;
69446954
}
69456955
#ifdef WOLFPKCS11_NSS
@@ -7025,18 +7035,22 @@ CK_RV C_GenerateKey(CK_SESSION_HANDLE hSession,
70257035

70267036
ret = WP11_Object_SetSecretKey(pbeKey, secretKeyData, secretKeyLen);
70277037
if (ret == 0) {
7028-
rv = AddObject(session, pbeKey, pTemplate, ulCount, phKey);
7029-
if (rv != CKR_OK) {
7030-
WP11_Object_Free(pbeKey);
7031-
}
7038+
WP11_Object_SetKeyGeneration(pbeKey, pMechanism->mechanism);
7039+
rv = SetInitialStates(pbeKey);
70327040
} else {
7033-
WP11_Object_Free(pbeKey);
70347041
rv = CKR_FUNCTION_FAILED;
70357042
}
7043+
if (rv == CKR_OK) {
7044+
rv = AddObject(session, pbeKey, pTemplate, ulCount, phKey);
7045+
}
7046+
if (rv != CKR_OK) {
7047+
WP11_Object_Free(pbeKey);
7048+
}
70367049
}
70377050

70387051
wc_ForceZero(derivedKey, derivedKeyLen);
70397052
XFREE(derivedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
7053+
70407054
return rv;
70417055
}
70427056
#endif
@@ -7064,31 +7078,19 @@ CK_RV C_GenerateKey(CK_SESSION_HANDLE hSession,
70647078
&key);
70657079
if (rv == CKR_OK) {
70667080
int ret = WP11_GenerateRandomKey(key,
7067-
WP11_Session_GetSlot(session));
7081+
WP11_Session_GetSlot(session),
7082+
pMechanism->mechanism);
70687083
if (ret != 0) {
7069-
WP11_Object_Free(key);
70707084
rv = CKR_FUNCTION_FAILED;
70717085
}
7072-
else {
7073-
rv = AddObject(session, key, pTemplate, ulCount, phKey);
7074-
if (rv != CKR_OK) {
7075-
WP11_Object_Free(key);
7076-
}
7077-
}
7078-
}
7079-
}
7080-
if (rv == CKR_OK) {
7081-
rv = WP11_Object_GetAttr(key, CKA_SENSITIVE, &getVar, &getVarLen);
7082-
if ((rv == CKR_OK) && (getVar == CK_TRUE)) {
7083-
rv = WP11_Object_SetAttr(key, CKA_ALWAYS_SENSITIVE, &trueVar,
7084-
sizeof(CK_BBOOL));
70857086
}
7087+
if (rv == CKR_OK)
7088+
rv = SetInitialStates(key);
70867089
if (rv == CKR_OK) {
7087-
rv = WP11_Object_GetAttr(key, CKA_EXTRACTABLE, &getVar, &getVarLen);
7088-
if ((rv == CKR_OK) && (getVar == CK_FALSE)) {
7089-
rv = WP11_Object_SetAttr(key, CKA_NEVER_EXTRACTABLE, &trueVar,
7090-
sizeof(CK_BBOOL));
7091-
}
7090+
rv = AddObject(session, key, pTemplate, ulCount, phKey);
7091+
}
7092+
if (rv != CKR_OK && key != NULL) {
7093+
WP11_Object_Free(key);
70927094
}
70937095
}
70947096

@@ -9053,11 +9055,11 @@ CK_RV C_EncapsulateKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
90539055
secretKeyLen);
90549056
if (ret != 0)
90559057
rv = CKR_FUNCTION_FAILED;
9058+
if (rv == CKR_OK)
9059+
rv = SetInitialStates(secretObj);
90569060
if (rv == CKR_OK)
90579061
rv = AddObject(session, secretObj, pTemplate, ulAttributeCount,
90589062
phKey);
9059-
if (rv == CKR_OK)
9060-
rv = SetInitialStates(secretObj);
90619063
}
90629064
if (rv != CKR_OK && secretObj != NULL) {
90639065
WP11_Object_Free(secretObj);
@@ -9159,11 +9161,11 @@ CK_RV C_DecapsulateKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
91599161
secretKeyLen);
91609162
if (ret != 0)
91619163
rv = CKR_FUNCTION_FAILED;
9164+
if (rv == CKR_OK)
9165+
rv = SetInitialStates(secretObj);
91629166
if (rv == CKR_OK)
91639167
rv = AddObject(session, secretObj, pTemplate, ulAttributeCount,
91649168
phKey);
9165-
if (rv == CKR_OK)
9166-
rv = SetInitialStates(secretObj);
91679169
}
91689170
if (rv != CKR_OK && secretObj != NULL) {
91699171
WP11_Object_Free(secretObj);

src/internal.c

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2730,6 +2730,15 @@ int WP11_Object_Copy(WP11_Object *src, WP11_Object *dest)
27302730
else
27312731
#endif
27322732
{
2733+
#ifndef WOLFPKCS11_NO_STORE
2734+
/* When the source key is encoded (encrypted at rest), the crypto
2735+
* key struct has been freed. The keyData blob is already copied
2736+
* above via OBJ_COPY_DATA, so skip the deep key copy. */
2737+
if (src->encoded) {
2738+
dest->type = src->type;
2739+
}
2740+
else
2741+
#endif
27332742
switch (src->type) {
27342743
#ifndef NO_RSA
27352744
case CKK_RSA: {
@@ -4927,10 +4936,9 @@ static int MlKemKeyTryDecode(MlKemKey* key, int level, byte* data, word32 len,
49274936
else {
49284937
ret = wc_MlKemKey_DecodePublicKey(key, data, len);
49294938
}
4930-
}
4931-
4932-
if (ret != 0) {
4933-
wc_MlKemKey_Free(key);
4939+
if (ret != 0) {
4940+
wc_MlKemKey_Free(key);
4941+
}
49344942
}
49354943

49364944
return ret;
@@ -4953,7 +4961,7 @@ static int wp11_Object_Decode_MlKemKey(WP11_Object* object)
49534961
unsigned char* der;
49544962
int len;
49554963

4956-
if (object->keyDataLen < AES_BLOCK_SIZE)
4964+
if (object->keyDataLen <= AES_BLOCK_SIZE)
49574965
return BAD_FUNC_ARG;
49584966
len = object->keyDataLen - AES_BLOCK_SIZE;
49594967

@@ -9442,7 +9450,9 @@ int WP11_Object_SetMlKemKey(WP11_Object* object, unsigned char** data,
94429450
object->devId);
94439451
break;
94449452
default:
9445-
ret = ASN_PARSE_E;
9453+
if (object->onToken)
9454+
WP11_Lock_UnlockRW(object->lock);
9455+
return ASN_PARSE_E;
94469456
}
94479457

94489458
/* Set seed (only for private keys). */
@@ -11496,6 +11506,19 @@ int WP11_Object_SetAttr(WP11_Object* object, CK_ATTRIBUTE_TYPE type, byte* data,
1149611506
return ret;
1149711507
}
1149811508

11509+
/**
11510+
* Mark an object as locally generated and record the mechanism used.
11511+
*
11512+
* @param object [in] Object to update.
11513+
* @param mechanism [in] Generation mechanism.
11514+
*/
11515+
void WP11_Object_SetKeyGeneration(WP11_Object* object,
11516+
CK_MECHANISM_TYPE mechanism)
11517+
{
11518+
object->local = 1;
11519+
object->keyGenMech = mechanism;
11520+
}
11521+
1149911522
/**
1150011523
* Check whether the attribute matches in the object.
1150111524
*
@@ -13205,12 +13228,14 @@ int WP11_Mldsa_Verify(unsigned char* sig, word32 sigLen, unsigned char* data,
1320513228
/**
1320613229
* Generate a secret key.
1320713230
*
13208-
* @param secret [in] Secret object.
13209-
* @param slot [in] Slot operation is performed on.
13231+
* @param secret [in] Secret object.
13232+
* @param slot [in] Slot operation is performed on.
13233+
* @param mechanism [in] Key generation mechanism.
1321013234
* @return -ve on random number generation failure.
1321113235
* 0 on success.
1321213236
*/
13213-
int WP11_GenerateRandomKey(WP11_Object* secret, WP11_Slot* slot)
13237+
int WP11_GenerateRandomKey(WP11_Object* secret, WP11_Slot* slot,
13238+
CK_MECHANISM_TYPE mechanism)
1321413239
{
1321513240
int ret;
1321613241
WP11_Data* key = secret->data.symmKey;
@@ -13219,6 +13244,11 @@ int WP11_GenerateRandomKey(WP11_Object* secret, WP11_Slot* slot)
1321913244
ret = wc_RNG_GenerateBlock(&slot->token.rng, key->data, key->len);
1322013245
WP11_Lock_UnlockRW(&slot->token.rngLock);
1322113246

13247+
if (ret == 0) {
13248+
secret->local = 1;
13249+
secret->keyGenMech = mechanism;
13250+
}
13251+
1322213252
return ret;
1322313253
}
1322413254
#endif /* WOLFPKCS11_HKDF || !NO_AES */
@@ -13424,6 +13454,13 @@ int WP11_MlKem_GenerateKeyPair(WP11_Object* pub, WP11_Object* priv,
1342413454
ret = wc_MlKemKey_EncodePublicKey(priv->data.mlKemKey, pubKeyBytes,
1342513455
pubKeyLen);
1342613456
}
13457+
if (ret == 0) {
13458+
/* Re-init the public key before decoding into it since it was
13459+
* already Init'd during NewObject -> WP11_Object_SetMlKemKey. */
13460+
wc_MlKemKey_Free(pub->data.mlKemKey);
13461+
ret = wc_MlKemKey_Init(pub->data.mlKemKey, priv->data.mlKemKey->type,
13462+
NULL, pub->devId);
13463+
}
1342713464
if (ret == 0) {
1342813465
ret = wc_MlKemKey_DecodePublicKey(pub->data.mlKemKey, pubKeyBytes,
1342913466
pubKeyLen);
@@ -13460,7 +13497,7 @@ int WP11_MlKem_Encapsulate(WP11_Object* pub, unsigned char** sharedSecret,
1346013497
int ret;
1346113498
int rngInit = 0;
1346213499
WC_RNG rng;
13463-
MlKemKey* mlKemKey = pub->data.mlKemKey;
13500+
MlKemKey* mlKemKey;
1346413501
word32 ctLen = 0;
1346513502

1346613503
*sharedSecret = NULL;
@@ -13473,6 +13510,7 @@ int WP11_MlKem_Encapsulate(WP11_Object* pub, unsigned char** sharedSecret,
1347313510
if (pub->onToken)
1347413511
WP11_Lock_LockRO(pub->lock);
1347513512

13513+
mlKemKey = pub->data.mlKemKey;
1347613514
ret = wc_MlKemKey_CipherTextSize(mlKemKey, &ctLen);
1347713515
if (ret == 0) {
1347813516
if (pCiphertext == NULL) {
@@ -13538,7 +13576,7 @@ int WP11_MlKem_Decapsulate(WP11_Object* priv, unsigned char** sharedSecret,
1353813576
CK_ULONG ulCiphertextLen)
1353913577
{
1354013578
int ret;
13541-
MlKemKey* mlKemKey = priv->data.mlKemKey;
13579+
MlKemKey* mlKemKey;
1354213580

1354313581
*sharedSecret = NULL;
1354413582

@@ -13550,6 +13588,7 @@ int WP11_MlKem_Decapsulate(WP11_Object* priv, unsigned char** sharedSecret,
1355013588
if (priv->onToken)
1355113589
WP11_Lock_LockRO(priv->lock);
1355213590

13591+
mlKemKey = priv->data.mlKemKey;
1355313592
ret = wc_MlKemKey_SharedSecretSize(mlKemKey, ssLen);
1355413593
if (ret == 0) {
1355513594
*sharedSecret = (unsigned char*)XMALLOC(*ssLen, NULL,

src/slot.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ static CK_RV checkPinLen(CK_ULONG pinLen)
154154
#else
155155
if (pinLen > WP11_MAX_PIN_LEN)
156156
#endif
157-
return CKR_PIN_INCORRECT;
157+
return CKR_PIN_LEN_RANGE;
158158
return CKR_OK;
159159
}
160160

@@ -1277,8 +1277,8 @@ CK_RV C_InitToken(CK_SLOT_ID slotID, CK_UTF8CHAR_PTR pPin,
12771277
return rv;
12781278
}
12791279

1280-
if (checkPinLen(ulPinLen) != CKR_OK) {
1281-
rv = CKR_PIN_INCORRECT;
1280+
rv = checkPinLen(ulPinLen);
1281+
if (rv != CKR_OK) {
12821282
WOLFPKCS11_LEAVE("C_InitToken", rv);
12831283
return rv;
12841284
}
@@ -1361,8 +1361,8 @@ CK_RV C_InitPIN(CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pPin,
13611361
return rv;
13621362
}
13631363

1364-
if (checkPinLen(ulPinLen) != CKR_OK) {
1365-
rv = CKR_PIN_INCORRECT;
1364+
rv = checkPinLen(ulPinLen);
1365+
if (rv != CKR_OK) {
13661366
WOLFPKCS11_LEAVE("C_InitPIN", rv);
13671367
return rv;
13681368
}
@@ -1436,8 +1436,8 @@ CK_RV C_SetPIN(CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pOldPin,
14361436
WOLFPKCS11_LEAVE("C_SetPIN", rv);
14371437
return rv;
14381438
}
1439-
if (checkPinLen(ulNewLen) != CKR_OK) {
1440-
rv = CKR_PIN_INCORRECT;
1439+
rv = checkPinLen(ulNewLen);
1440+
if (rv != CKR_OK) {
14411441
WOLFPKCS11_LEAVE("C_SetPIN", rv);
14421442
return rv;
14431443
}

0 commit comments

Comments
 (0)