Skip to content

Commit 8dbbb2a

Browse files
committed
Fix build issues and findings
1 parent 504a9f8 commit 8dbbb2a

6 files changed

Lines changed: 216 additions & 97 deletions

File tree

src/crypto.c

Lines changed: 26 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4093,6 +4093,8 @@ CK_RV C_DigestKey(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hKey)
40934093

40944094
if (ret < 0)
40954095
return CKR_FUNCTION_FAILED;
4096+
if (ret > 0)
4097+
return (CK_RV)ret;
40964098
return CKR_OK;
40974099
}
40984100

@@ -6704,9 +6706,6 @@ CK_RV C_GenerateKey(CK_SESSION_HANDLE hSession,
67046706
CK_RV rv = CKR_OK;
67056707
WP11_Session* session = NULL;
67066708
WP11_Object* key = NULL;
6707-
CK_BBOOL trueVar = CK_TRUE;
6708-
CK_BBOOL getVar;
6709-
CK_ULONG getVarLen = sizeof(CK_BBOOL);
67106709
CK_KEY_TYPE keyType;
67116710

67126711
WOLFPKCS11_ENTER("C_GenerateKey");
@@ -6916,37 +6915,21 @@ CK_RV C_GenerateKey(CK_SESSION_HANDLE hSession,
69166915
ret = WP11_Object_SetSecretKey(pbkdf2Key, secretKeyData, secretKeyLen);
69176916
if (ret == 0) {
69186917
WP11_Object_SetKeyGeneration(pbkdf2Key, pMechanism->mechanism);
6919-
rv = AddObject(session, pbkdf2Key, pTemplate, ulCount, phKey);
6920-
if (rv != CKR_OK) {
6921-
WP11_Object_Free(pbkdf2Key);
6922-
}
6918+
rv = SetInitialStates(pbkdf2Key);
69236919
} else {
6924-
WP11_Object_Free(pbkdf2Key);
69256920
rv = CKR_FUNCTION_FAILED;
69266921
}
6922+
if (rv == CKR_OK) {
6923+
rv = AddObject(session, pbkdf2Key, pTemplate, ulCount, phKey);
6924+
}
6925+
if (rv != CKR_OK) {
6926+
WP11_Object_Free(pbkdf2Key);
6927+
}
69276928
}
69286929

69296930
wc_ForceZero(derivedKey, derivedKeyLen);
69306931
XFREE(derivedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
69316932

6932-
if (rv == CKR_OK) {
6933-
rv = WP11_Object_GetAttr(pbkdf2Key, CKA_SENSITIVE, &getVar,
6934-
&getVarLen);
6935-
if ((rv == CKR_OK) && (getVar == CK_TRUE)) {
6936-
rv = WP11_Object_SetAttr(pbkdf2Key, CKA_ALWAYS_SENSITIVE,
6937-
&trueVar, sizeof(CK_BBOOL));
6938-
}
6939-
if (rv == CKR_OK) {
6940-
rv = WP11_Object_GetAttr(pbkdf2Key, CKA_EXTRACTABLE,
6941-
&getVar, &getVarLen);
6942-
if ((rv == CKR_OK) && (getVar == CK_FALSE)) {
6943-
rv = WP11_Object_SetAttr(pbkdf2Key,
6944-
CKA_NEVER_EXTRACTABLE,
6945-
&trueVar, sizeof(CK_BBOOL));
6946-
}
6947-
}
6948-
}
6949-
69506933
return rv;
69516934
}
69526935
#ifdef WOLFPKCS11_NSS
@@ -7033,37 +7016,21 @@ CK_RV C_GenerateKey(CK_SESSION_HANDLE hSession,
70337016
ret = WP11_Object_SetSecretKey(pbeKey, secretKeyData, secretKeyLen);
70347017
if (ret == 0) {
70357018
WP11_Object_SetKeyGeneration(pbeKey, pMechanism->mechanism);
7036-
rv = AddObject(session, pbeKey, pTemplate, ulCount, phKey);
7037-
if (rv != CKR_OK) {
7038-
WP11_Object_Free(pbeKey);
7039-
}
7019+
rv = SetInitialStates(pbeKey);
70407020
} else {
7041-
WP11_Object_Free(pbeKey);
70427021
rv = CKR_FUNCTION_FAILED;
70437022
}
7023+
if (rv == CKR_OK) {
7024+
rv = AddObject(session, pbeKey, pTemplate, ulCount, phKey);
7025+
}
7026+
if (rv != CKR_OK) {
7027+
WP11_Object_Free(pbeKey);
7028+
}
70447029
}
70457030

70467031
wc_ForceZero(derivedKey, derivedKeyLen);
70477032
XFREE(derivedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
70487033

7049-
if (rv == CKR_OK) {
7050-
rv = WP11_Object_GetAttr(pbeKey, CKA_SENSITIVE, &getVar,
7051-
&getVarLen);
7052-
if ((rv == CKR_OK) && (getVar == CK_TRUE)) {
7053-
rv = WP11_Object_SetAttr(pbeKey, CKA_ALWAYS_SENSITIVE,
7054-
&trueVar, sizeof(CK_BBOOL));
7055-
}
7056-
if (rv == CKR_OK) {
7057-
rv = WP11_Object_GetAttr(pbeKey, CKA_EXTRACTABLE,
7058-
&getVar, &getVarLen);
7059-
if ((rv == CKR_OK) && (getVar == CK_FALSE)) {
7060-
rv = WP11_Object_SetAttr(pbeKey,
7061-
CKA_NEVER_EXTRACTABLE,
7062-
&trueVar, sizeof(CK_BBOOL));
7063-
}
7064-
}
7065-
}
7066-
70677034
return rv;
70687035
}
70697036
#endif
@@ -7094,29 +7061,16 @@ CK_RV C_GenerateKey(CK_SESSION_HANDLE hSession,
70947061
WP11_Session_GetSlot(session),
70957062
pMechanism->mechanism);
70967063
if (ret != 0) {
7097-
WP11_Object_Free(key);
70987064
rv = CKR_FUNCTION_FAILED;
70997065
}
7100-
else {
7101-
rv = AddObject(session, key, pTemplate, ulCount, phKey);
7102-
if (rv != CKR_OK) {
7103-
WP11_Object_Free(key);
7104-
}
7105-
}
7106-
}
7107-
}
7108-
if (rv == CKR_OK) {
7109-
rv = WP11_Object_GetAttr(key, CKA_SENSITIVE, &getVar, &getVarLen);
7110-
if ((rv == CKR_OK) && (getVar == CK_TRUE)) {
7111-
rv = WP11_Object_SetAttr(key, CKA_ALWAYS_SENSITIVE, &trueVar,
7112-
sizeof(CK_BBOOL));
71137066
}
7067+
if (rv == CKR_OK)
7068+
rv = SetInitialStates(key);
71147069
if (rv == CKR_OK) {
7115-
rv = WP11_Object_GetAttr(key, CKA_EXTRACTABLE, &getVar, &getVarLen);
7116-
if ((rv == CKR_OK) && (getVar == CK_FALSE)) {
7117-
rv = WP11_Object_SetAttr(key, CKA_NEVER_EXTRACTABLE, &trueVar,
7118-
sizeof(CK_BBOOL));
7119-
}
7070+
rv = AddObject(session, key, pTemplate, ulCount, phKey);
7071+
}
7072+
if (rv != CKR_OK && key != NULL) {
7073+
WP11_Object_Free(key);
71207074
}
71217075
}
71227076

@@ -9081,11 +9035,11 @@ CK_RV C_EncapsulateKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
90819035
secretKeyLen);
90829036
if (ret != 0)
90839037
rv = CKR_FUNCTION_FAILED;
9038+
if (rv == CKR_OK)
9039+
rv = SetInitialStates(secretObj);
90849040
if (rv == CKR_OK)
90859041
rv = AddObject(session, secretObj, pTemplate, ulAttributeCount,
90869042
phKey);
9087-
if (rv == CKR_OK)
9088-
rv = SetInitialStates(secretObj);
90899043
}
90909044
if (rv != CKR_OK && secretObj != NULL) {
90919045
WP11_Object_Free(secretObj);
@@ -9187,11 +9141,11 @@ CK_RV C_DecapsulateKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
91879141
secretKeyLen);
91889142
if (ret != 0)
91899143
rv = CKR_FUNCTION_FAILED;
9144+
if (rv == CKR_OK)
9145+
rv = SetInitialStates(secretObj);
91909146
if (rv == CKR_OK)
91919147
rv = AddObject(session, secretObj, pTemplate, ulAttributeCount,
91929148
phKey);
9193-
if (rv == CKR_OK)
9194-
rv = SetInitialStates(secretObj);
91959149
}
91969150
if (rv != CKR_OK && secretObj != NULL) {
91979151
WP11_Object_Free(secretObj);

src/internal.c

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2706,6 +2706,15 @@ int WP11_Object_Copy(WP11_Object *src, WP11_Object *dest)
27062706
else
27072707
#endif
27082708
{
2709+
#ifndef WOLFPKCS11_NO_STORE
2710+
/* When the source key is encoded (encrypted at rest), the crypto
2711+
* key struct has been freed. The keyData blob is already copied
2712+
* above via OBJ_COPY_DATA, so skip the deep key copy. */
2713+
if (src->encoded) {
2714+
dest->type = src->type;
2715+
}
2716+
else
2717+
#endif
27092718
switch (src->type) {
27102719
#ifndef NO_RSA
27112720
case CKK_RSA: {
@@ -4903,10 +4912,9 @@ static int MlKemKeyTryDecode(MlKemKey* key, int level, byte* data, word32 len,
49034912
else {
49044913
ret = wc_MlKemKey_DecodePublicKey(key, data, len);
49054914
}
4906-
}
4907-
4908-
if (ret != 0) {
4909-
wc_MlKemKey_Free(key);
4915+
if (ret != 0) {
4916+
wc_MlKemKey_Free(key);
4917+
}
49104918
}
49114919

49124920
return ret;
@@ -4929,7 +4937,7 @@ static int wp11_Object_Decode_MlKemKey(WP11_Object* object)
49294937
unsigned char* der;
49304938
int len;
49314939

4932-
if (object->keyDataLen < AES_BLOCK_SIZE)
4940+
if (object->keyDataLen <= AES_BLOCK_SIZE)
49334941
return BAD_FUNC_ARG;
49344942
len = object->keyDataLen - AES_BLOCK_SIZE;
49354943

@@ -9412,7 +9420,9 @@ int WP11_Object_SetMlKemKey(WP11_Object* object, unsigned char** data,
94129420
object->devId);
94139421
break;
94149422
default:
9415-
ret = ASN_PARSE_E;
9423+
if (object->onToken)
9424+
WP11_Lock_UnlockRW(object->lock);
9425+
return ASN_PARSE_E;
94169426
}
94179427

94189428
/* Set seed (only for private keys). */
@@ -13396,6 +13406,13 @@ int WP11_MlKem_GenerateKeyPair(WP11_Object* pub, WP11_Object* priv,
1339613406
ret = wc_MlKemKey_EncodePublicKey(priv->data.mlKemKey, pubKeyBytes,
1339713407
pubKeyLen);
1339813408
}
13409+
if (ret == 0) {
13410+
/* Re-init the public key before decoding into it since it was
13411+
* already Init'd during NewObject -> WP11_Object_SetMlKemKey. */
13412+
wc_MlKemKey_Free(pub->data.mlKemKey);
13413+
ret = wc_MlKemKey_Init(pub->data.mlKemKey, priv->data.mlKemKey->type,
13414+
NULL, pub->devId);
13415+
}
1339913416
if (ret == 0) {
1340013417
ret = wc_MlKemKey_DecodePublicKey(pub->data.mlKemKey, pubKeyBytes,
1340113418
pubKeyLen);
@@ -13432,7 +13449,7 @@ int WP11_MlKem_Encapsulate(WP11_Object* pub, unsigned char** sharedSecret,
1343213449
int ret;
1343313450
int rngInit = 0;
1343413451
WC_RNG rng;
13435-
MlKemKey* mlKemKey = pub->data.mlKemKey;
13452+
MlKemKey* mlKemKey;
1343613453
word32 ctLen = 0;
1343713454

1343813455
*sharedSecret = NULL;
@@ -13445,6 +13462,7 @@ int WP11_MlKem_Encapsulate(WP11_Object* pub, unsigned char** sharedSecret,
1344513462
if (pub->onToken)
1344613463
WP11_Lock_LockRO(pub->lock);
1344713464

13465+
mlKemKey = pub->data.mlKemKey;
1344813466
ret = wc_MlKemKey_CipherTextSize(mlKemKey, &ctLen);
1344913467
if (ret == 0) {
1345013468
if (pCiphertext == NULL) {
@@ -13510,7 +13528,7 @@ int WP11_MlKem_Decapsulate(WP11_Object* priv, unsigned char** sharedSecret,
1351013528
CK_ULONG ulCiphertextLen)
1351113529
{
1351213530
int ret;
13513-
MlKemKey* mlKemKey = priv->data.mlKemKey;
13531+
MlKemKey* mlKemKey;
1351413532

1351513533
*sharedSecret = NULL;
1351613534

@@ -13522,6 +13540,7 @@ int WP11_MlKem_Decapsulate(WP11_Object* priv, unsigned char** sharedSecret,
1352213540
if (priv->onToken)
1352313541
WP11_Lock_LockRO(priv->lock);
1352413542

13543+
mlKemKey = priv->data.mlKemKey;
1352513544
ret = wc_MlKemKey_SharedSecretSize(mlKemKey, ssLen);
1352613545
if (ret == 0) {
1352713546
*sharedSecret = (unsigned char*)XMALLOC(*ssLen, NULL,

tests/pbkdf2_keygen_attrs_test.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
#include "testdata.h"
4747

48-
#ifndef NO_PWDBASED
48+
#if !defined(NO_PWDBASED) && !defined(NO_HMAC)
4949

5050
#define TEST_DIR "./store/pbkdf2_keygen_attrs_test"
5151
#define WOLFPKCS11_TOKEN_FILENAME "wp11_token_0000000000000001"
@@ -428,14 +428,14 @@ int main(int argc, char* argv[])
428428
return (test_failed == 0) ? 0 : 1;
429429
}
430430

431-
#else /* NO_PWDBASED */
431+
#else /* NO_PWDBASED || NO_HMAC */
432432

433433
int main(int argc, char* argv[])
434434
{
435435
(void)argc;
436436
(void)argv;
437437

438-
printf("PWDBASED not available, skipping PBKDF2 keygen attributes test\n");
438+
printf("PWDBASED/HMAC not available, skipping PBKDF2 keygen attributes test\n");
439439
return 0;
440440
}
441441

0 commit comments

Comments
 (0)