Skip to content

Commit

Permalink
fix: Handle cases where slotCount or mechanismCount is 0
Browse files Browse the repository at this point in the history
  • Loading branch information
microshine committed Aug 29, 2024
1 parent 1219da7 commit a2c06c4
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/pkcs11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,16 @@ class Pkcs11
CK_RV rv = pkcs11->functionList->C_GetSlotList(ckTokenPresent, nullptr, &slotCount); // get slot count
ASSERT_RV(rv);

// In some cases, the pkcs11 module from Yubico may return slotCount 0 on the first call to C_GetSlotList,
// and a non-zero value on subsequent calls. This can lead to a memory access error when using pSlotList.
// To handle this, we check if slotCount is 0 and return an empty array if no slots are available.
if (slotCount == 0)
{
napi_value result;
napi_create_array(env, &result);
return result;
}

std::vector<CK_SLOT_ID> slotList(slotCount);
CK_SLOT_ID_PTR pSlotList = slotList.data();
rv = pkcs11->functionList->C_GetSlotList(ckTokenPresent, pSlotList, &slotCount);
Expand Down Expand Up @@ -1306,6 +1316,13 @@ class Pkcs11
CK_RV rv = pkcs11->functionList->C_GetMechanismList(slotId, nullptr, &mechanismCount); // get mechanism count
ASSERT_RV(rv);

if (mechanismCount == 0)
{
napi_value result;
napi_create_array(env, &result);
return result;
}

std::vector<CK_MECHANISM_TYPE> mechanismList(mechanismCount);
CK_MECHANISM_TYPE_PTR pMechanismList = mechanismList.data();
rv = pkcs11->functionList->C_GetMechanismList(slotId, pMechanismList, &mechanismCount);
Expand Down

0 comments on commit a2c06c4

Please sign in to comment.