Skip to content

Commit 93ca213

Browse files
authored
Merge pull request wolfSSL#7736 from space88man/fix-pkcs11-slot
wolfcrypt/src/wc_pkcs11.c: iterate correctly over slotId
2 parents 12ba319 + fdd03fa commit 93ca213

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

wolfcrypt/src/wc_pkcs11.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -531,22 +531,38 @@ void wc_Pkcs11_Finalize(Pkcs11Dev* dev)
531531
static int Pkcs11Slot_FindByTokenName(Pkcs11Dev* dev,
532532
const char* tokenName, size_t tokenNameSz)
533533
{
534+
int ret = -1;
534535
CK_RV rv;
535536
CK_ULONG slotCnt = 0;
536537
CK_TOKEN_INFO tinfo;
537-
int slotId = -1;
538+
int index = -1;
539+
CK_SLOT_ID* slot = NULL;
540+
538541
rv = dev->func->C_GetSlotList(CK_TRUE, NULL, &slotCnt);
539542
if (rv == CKR_OK) {
540-
for (slotId = 0; slotId < (int)slotCnt; slotId++) {
541-
rv = dev->func->C_GetTokenInfo(slotId, &tinfo);
543+
slot = (CK_SLOT_ID*)XMALLOC(slotCnt * sizeof(*slot), dev->heap,
544+
DYNAMIC_TYPE_TMP_BUFFER);
545+
if (slot == NULL)
546+
goto out;
547+
rv = dev->func->C_GetSlotList(CK_TRUE, slot, &slotCnt);
548+
if (rv != CKR_OK)
549+
goto out;
550+
for (index = 0; index < (int)slotCnt; index++) {
551+
rv = dev->func->C_GetTokenInfo(slot[index], &tinfo);
542552
PKCS11_RV("C_GetTokenInfo", rv);
543553
if (rv == CKR_OK &&
544554
XMEMCMP(tinfo.label, tokenName, tokenNameSz) == 0) {
545-
return slotId;
555+
ret = slot[index];
556+
break;
546557
}
547558
}
548559
}
549-
return -1;
560+
561+
out:
562+
if (slot != NULL) {
563+
XFREE(slot, dev->heap, DYNAMIC_TYPE_TMP_BUFFER);
564+
}
565+
return ret;
550566
}
551567

552568
/* lookup by slotId or tokenName */

0 commit comments

Comments
 (0)