From 95b6b4ccd3e0d973f56d89acd5ccff64965da90d Mon Sep 17 00:00:00 2001 From: Viktor Tarasov Date: Thu, 5 Dec 2013 09:13:28 +0100 Subject: [PATCH] pkcs11: changes in framework API to prepare future enhancements and in the sake of uniformity in 'struct sc_pkcs11_framework_ops' the 'struct sc_pkcs11_card' argument of 'init_token' handler is changed for 'struct sc_pkcs11_slot' coding style issues --- src/pkcs11/framework-pkcs15.c | 3 ++- src/pkcs11/pkcs11-global.c | 42 +++++++++++++++++++++++------------ src/pkcs11/sc-pkcs11.h | 6 ++--- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/pkcs11/framework-pkcs15.c b/src/pkcs11/framework-pkcs15.c index 474ab5c8..3e835130 100644 --- a/src/pkcs11/framework-pkcs15.c +++ b/src/pkcs11/framework-pkcs15.c @@ -1644,10 +1644,11 @@ pkcs15_change_pin(struct sc_pkcs11_slot *slot, #ifdef USE_PKCS15_INIT static CK_RV -pkcs15_initialize(struct sc_pkcs11_card *p11card, void *ptr, +pkcs15_initialize(struct sc_pkcs11_slot *slot, void *ptr, CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen, CK_UTF8CHAR_PTR pLabel) { + struct sc_pkcs11_card *p11card = slot->card; struct sc_cardctl_pkcs11_init_token args; int rv; diff --git a/src/pkcs11/pkcs11-global.c b/src/pkcs11/pkcs11-global.c index 4c4f7275..f3f5465f 100644 --- a/src/pkcs11/pkcs11-global.c +++ b/src/pkcs11/pkcs11-global.c @@ -368,9 +368,9 @@ CK_RV C_GetSlotList(CK_BBOOL tokenPresent, /* only slots with token prese if (pulCount == NULL_PTR) return CKR_ARGUMENTS_BAD; - if ((rv = sc_pkcs11_lock()) != CKR_OK) { + rv = sc_pkcs11_lock(); + if (rv != CKR_OK) return rv; - } sc_log(context, "C_GetSlotList(token=%d, %s)", tokenPresent, (pSlotList==NULL_PTR && sc_pkcs11_conf.plug_and_play)? "plug-n-play":"refresh"); @@ -480,26 +480,35 @@ CK_RV C_GetSlotInfo(CK_SLOT_ID slotID, CK_SLOT_INFO_PTR pInfo) sc_log(context, "C_GetSlotInfo(0x%lx)", slotID); rv = slot_get_slot(slotID, &slot); - if (rv == CKR_OK){ - if (slot->reader == NULL) + sc_log(context, "C_GetSlotInfo() get slot rv %i", rv); + if (rv == CKR_OK) { + if (slot->reader == NULL) { rv = CKR_TOKEN_NOT_PRESENT; + } else { now = get_current_time(); if (now >= slot->slot_state_expires || now == 0) { /* Update slot status */ rv = card_detect(slot->reader); + sc_log(context, "C_GetSlotInfo() card detect rv 0x%X", rv); + + if (rv == CKR_TOKEN_NOT_RECOGNIZED || rv == CKR_OK) + slot->slot_info.flags |= CKF_TOKEN_PRESENT; + /* Don't ask again within the next second */ slot->slot_state_expires = now + 1000; } } } + if (rv == CKR_TOKEN_NOT_PRESENT || rv == CKR_TOKEN_NOT_RECOGNIZED) rv = CKR_OK; if (rv == CKR_OK) memcpy(pInfo, &slot->slot_info, sizeof(CK_SLOT_INFO)); - sc_log(context, "C_GetSlotInfo(0x%lx) = %s", slotID, lookup_enum ( RV_T, rv )); + sc_log(context, "C_GetSlotInfo() flags 0x%X", pInfo->flags); + sc_log(context, "C_GetSlotInfo(0x%lx) = %s", slotID, lookup_enum( RV_T, rv)); sc_pkcs11_unlock(); return rv; } @@ -558,13 +567,22 @@ CK_RV C_InitToken(CK_SLOT_ID slotID, CK_RV rv; unsigned int i; + sc_log(context, "C_InitToken(pLabel='%s') called", pLabel); rv = sc_pkcs11_lock(); if (rv != CKR_OK) return rv; rv = slot_get_token(slotID, &slot); - if (rv != CKR_OK) + if (rv != CKR_OK) { + sc_log(context, "C_InitToken() get token error 0x%lX", rv); goto out; + } + + if (slot->card->framework->init_token == NULL) { + sc_log(context, "C_InitToken() not supported by framework"); + rv = CKR_FUNCTION_NOT_SUPPORTED; + goto out; + } /* Make sure there's no open session for this token */ for (i=0; icard->framework->init_token == NULL) { - rv = CKR_FUNCTION_NOT_SUPPORTED; - goto out; - } - rv = slot->card->framework->init_token(slot->card, - slot->fw_data, pPin, ulPinLen, pLabel); - + rv = slot->card->framework->init_token(slot,slot->fw_data, pPin, ulPinLen, pLabel); if (rv == CKR_OK) { /* Now we should re-bind all tokens so they get the * corresponding function vector and flags */ } -out: sc_pkcs11_unlock(); +out: + sc_pkcs11_unlock(); + sc_log(context, "C_InitToken(pLabel='%s') returns 0x%lX", pLabel, rv); return rv; } diff --git a/src/pkcs11/sc-pkcs11.h b/src/pkcs11/sc-pkcs11.h index 48c74e4a..aaa40ed1 100644 --- a/src/pkcs11/sc-pkcs11.h +++ b/src/pkcs11/sc-pkcs11.h @@ -159,12 +159,10 @@ struct sc_pkcs11_framework_ops { CK_RV (*change_pin)(struct sc_pkcs11_slot *, CK_CHAR_PTR, CK_ULONG, CK_CHAR_PTR, CK_ULONG); - /* - * In future: functions to create new objects - * (ie. certificates, private keys) + * In future: functions to create new objects (ie. certificates, private keys) */ - CK_RV (*init_token)(struct sc_pkcs11_card *, void *, + CK_RV (*init_token)(struct sc_pkcs11_slot *, void *, CK_UTF8CHAR_PTR, CK_ULONG, CK_UTF8CHAR_PTR); CK_RV (*init_pin)(struct sc_pkcs11_slot *,