Skip to content

Commit df8c70b

Browse files
committed
Add more skoll / fenrir fixes
1 parent c73ab1b commit df8c70b

3 files changed

Lines changed: 27 additions & 9 deletions

File tree

src/crypto.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -514,11 +514,9 @@ static CK_RV SetAttributeDefaults(WP11_Object* obj, CK_OBJECT_CLASS keyType,
514514
ulCount);
515515
break;
516516
case CKO_SECRET_KEY:
517-
#ifndef WOLFPKCS11_NSS
518517
if (ret == CKR_OK)
519518
ret = SetIfNotFound(obj, CKA_SENSITIVE, trueVal, pTemplate,
520519
ulCount);
521-
#endif
522520
if (ret == CKR_OK)
523521
ret = SetIfNotFound(obj, CKA_EXTRACTABLE, trueVal, pTemplate,
524522
ulCount);
@@ -544,7 +542,11 @@ static CK_RV SetAttributeDefaults(WP11_Object* obj, CK_OBJECT_CLASS keyType,
544542
ret = SetIfNotFound(obj, CKA_EXTRACTABLE, falseVal, pTemplate,
545543
ulCount);
546544
#else
547-
/* NSS needs extractable private keys as internal crypto module */
545+
/* NSS needs extractable private keys as internal crypto module.
546+
* CKA_SENSITIVE is still set to prevent plaintext key readout. */
547+
if (ret == CKR_OK)
548+
ret = SetIfNotFound(obj, CKA_SENSITIVE, trueVal, pTemplate,
549+
ulCount);
548550
if (ret == CKR_OK)
549551
ret = SetIfNotFound(obj, CKA_EXTRACTABLE, trueVal, pTemplate,
550552
ulCount);
@@ -2286,6 +2288,9 @@ CK_RV C_Encrypt(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData,
22862288
}
22872289
if (!CK_ULONG_FITS_WORD32(ulDataLen))
22882290
return CKR_DATA_LEN_RANGE;
2291+
/* Ensure padded result fits in word32 */
2292+
if (ulDataLen > (CK_ULONG)(0xFFFFFFFF - AES_BLOCK_SIZE))
2293+
return CKR_DATA_LEN_RANGE;
22892294

22902295
/* PKCS#7 padding always adds at least 1 byte */
22912296
encDataLen = (word32)((ulDataLen / AES_BLOCK_SIZE) + 1) *

src/internal.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ static void wp11_Session_Final(WP11_Session* session)
958958
if ((session->init & ~WP11_INIT_DIGEST_MASK) == WP11_INIT_HMAC_SIGN ||
959959
(session->init & ~WP11_INIT_DIGEST_MASK) == WP11_INIT_HMAC_VERIFY) {
960960
wc_HmacFree(&session->params.hmac.hmac);
961-
session->init = 0;
961+
session->init &= WP11_INIT_DIGEST_MASK;
962962
}
963963
#endif
964964
#ifdef HAVE_AESCMAC
@@ -970,14 +970,16 @@ static void wp11_Session_Final(WP11_Session* session)
970970
wc_ForceZero(&session->params.cmac.cmac,
971971
sizeof(session->params.cmac.cmac));
972972
#endif
973-
session->init = 0;
973+
session->init &= WP11_INIT_DIGEST_MASK;
974974
}
975975
#endif
976976
if ((session->init & ~WP11_INIT_DIGEST_MASK) == WP11_INIT_DIGEST) {
977977
wc_HashFree(&session->params.digest.hash,
978978
session->params.digest.hashType);
979-
session->init = 0;
979+
session->init &= ~WP11_INIT_DIGEST_MASK;
980980
}
981+
/* Ensure no stale bits remain after all cleanup. */
982+
session->init = 0;
981983
}
982984

983985
#ifndef WOLFPKCS11_NO_STORE
@@ -6971,8 +6973,8 @@ int WP11_Slot_IsLoggedIn(WP11_Slot* slot)
69716973

69726974
void WP11_Slot_Logout(WP11_Slot* slot)
69736975
{
6974-
#ifndef WOLFPKCS11_NO_STORE
69756976
int state;
6977+
#ifndef WOLFPKCS11_NO_STORE
69766978
int ret = 0;
69776979
#endif
69786980

@@ -6986,9 +6988,15 @@ void WP11_Slot_Logout(WP11_Slot* slot)
69866988
ret = wp11_Object_Encode(object, 1);
69876989
object = object->next;
69886990
}
6989-
wc_ForceZero(slot->token.key, sizeof(slot->token.key));
69906991
}
6992+
#else
6993+
state = slot->token.loginState;
69916994
#endif
6995+
/* Zero token key only on user logout — SO logout must preserve it
6996+
* for subsequent object encryption (e.g., empty-PIN flow). */
6997+
if (state == WP11_APP_STATE_RO_USER || state == WP11_APP_STATE_RW_USER) {
6998+
wc_ForceZero(slot->token.key, sizeof(slot->token.key));
6999+
}
69927000
slot->token.loginState = WP11_APP_STATE_RW_PUBLIC;
69937001

69947002
WP11_Lock_UnlockRW(&slot->lock);
@@ -14261,8 +14269,11 @@ int WP11_Digest_Single(unsigned char* data, word32 dataLen,
1426114269
WP11_Digest* digest = &session->params.digest;
1426214270

1426314271
blockLen = wc_HashGetDigestSize(digest->hashType);
14264-
if (blockLen < 0)
14272+
if (blockLen < 0) {
14273+
wc_HashFree(&digest->hash, digest->hashType);
14274+
session->init = 0;
1426514275
return CKR_FUNCTION_FAILED;
14276+
}
1426614277

1426714278
if (dataOut == NULL) {
1426814279
*dataOutLen = (word32)blockLen;

tests/pkcs11test.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10875,6 +10875,8 @@ static CK_RV test_aes_cbc_pad_block_aligned_size(void* args)
1087510875
}
1087610876
}
1087710877

10878+
funcList->C_DestroyObject(session, key);
10879+
1087810880
return ret;
1087910881
}
1088010882

0 commit comments

Comments
 (0)