From 7fb72ccf7bf3ca2ff3979b7ffbb690eed41ddb5f Mon Sep 17 00:00:00 2001 From: Frank Morgner Date: Fri, 14 Jun 2019 07:54:39 +0200 Subject: [PATCH] pkcs11: fixed slotIDs when a new slot list is requested fixes https://github.com/OpenSC/OpenSC/issues/1706 regression of 24b7507a69704f69e371834953cf78cffec104b0 --- src/pkcs11/pkcs11-global.c | 22 +++++++++++++++++++++- src/pkcs11/slot.c | 2 +- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/pkcs11/pkcs11-global.c b/src/pkcs11/pkcs11-global.c index c3cf2591..d8c0df5f 100644 --- a/src/pkcs11/pkcs11-global.c +++ b/src/pkcs11/pkcs11-global.c @@ -451,8 +451,13 @@ CK_RV C_GetSlotList(CK_BBOOL tokenPresent, /* only slots with token prese pSlotList==NULL_PTR? "plug-n-play":"refresh"); /* Slot list can only change in v2.20 */ - if (pSlotList == NULL_PTR) + if (pSlotList == NULL_PTR) { sc_ctx_detect_readers(context); + for (i=0; iflags &= ~SC_PKCS11_SLOT_FLAG_SEEN; + } + } card_detect_all(); @@ -483,6 +488,21 @@ CK_RV C_GetSlotList(CK_BBOOL tokenPresent, /* only slots with token prese prev_reader = slot->reader; } + /* Slot list can only change in v2.20 */ + if (pSlotList == NULL_PTR) { + /* slot->id is derived from its location in the list virtual_slots. + * When the slot list changes, so does slot->id, so we reindex the + * slots here the same way it is done in `create_slot()` + * + * TODO use a persistent CK_SLOT_ID, e.g. by using something like + * `slot->id = sc_crc32(slot, sizeof *slot);` (this example, however, + * is currently not thread safe). */ + for (i=0; iid = (CK_SLOT_ID) list_locate(&virtual_slots, slot); + } + } + if (pSlotList == NULL_PTR) { sc_log(context, "was only a size inquiry (%lu)\n", numMatches); *pulCount = numMatches; diff --git a/src/pkcs11/slot.c b/src/pkcs11/slot.c index 44b041ee..c74f2c0a 100644 --- a/src/pkcs11/slot.c +++ b/src/pkcs11/slot.c @@ -115,7 +115,6 @@ CK_RV create_slot(sc_reader_t *reader) } slot->login_user = -1; - slot->id = (CK_SLOT_ID) list_locate(&virtual_slots, slot); init_slot_info(&slot->slot_info, reader); sc_log(context, "Initializing slot with id 0x%lx", slot->id); @@ -126,6 +125,7 @@ CK_RV create_slot(sc_reader_t *reader) slot->slot_info.hardwareVersion.major = reader->version_major; slot->slot_info.hardwareVersion.minor = reader->version_minor; } + slot->id = (CK_SLOT_ID) list_locate(&virtual_slots, slot); return CKR_OK; }