Handle token presence in slots.

This commit is contained in:
Alessio Di Mauro
2015-07-14 15:45:03 +02:00
parent dae7297050
commit 0e4569af2c
+28 -17
View File
@@ -5,16 +5,14 @@
#include "vendors.h" #include "vendors.h"
#include "utils.h" #include "utils.h"
#define HAS_TOKEN(x) ((slots[(x)].info.flags) & (CKF_TOKEN_PRESENT))
#define D(x) do { \ #define D(x) do { \
printf ("debug: %s:%d (%s): ", __FILE__, __LINE__, __FUNCTION__); \ printf ("debug: %s:%d (%s): ", __FILE__, __LINE__, __FUNCTION__); \
printf x; \ printf x; \
printf ("\n"); \ printf ("\n"); \
} while (0) } while (0)
#define YKCS11_DBG 0 // General debug, must be either 1 or 0 #define YKCS11_DBG 1 // General debug, must be either 1 or 0
#define YKCS11_DINOUT 0 // Function in/out debug, must be either 1 or 0 #define YKCS11_DINOUT 1 // Function in/out debug, must be either 1 or 0
#define YKCS11_MANUFACTURER "Yubico (www.yubico.com)" #define YKCS11_MANUFACTURER "Yubico (www.yubico.com)"
#define YKCS11_LIBDESC "PKCS#11 PIV Library (SP-800-73)" #define YKCS11_LIBDESC "PKCS#11 PIV Library (SP-800-73)"
@@ -42,6 +40,7 @@ static ykpiv_state *piv_state = NULL;
static ykcs11_slot_t slots[YKCS11_MAX_SLOTS]; static ykcs11_slot_t slots[YKCS11_MAX_SLOTS];
static CK_ULONG n_slots = 0; static CK_ULONG n_slots = 0;
static CK_ULONG n_tokenless_slots = 0;
extern CK_FUNCTION_LIST function_list; extern CK_FUNCTION_LIST function_list;
@@ -70,8 +69,8 @@ CK_DEFINE_FUNCTION(CK_RV, C_Initialize)(
return CKR_FUNCTION_FAILED; return CKR_FUNCTION_FAILED;
} }
parse_readers(readers, len, slots, &n_slots); parse_readers(readers, len, slots, &n_slots, &n_tokenless_slots);
DBG(("FOUND: %lu slots %lu len", n_slots, len)); DBG(("Found %lu slot(s) of which %lu tokenless/unsupported", n_slots, n_tokenless_slots));
DOUT; DOUT;
return CKR_OK; return CKR_OK;
@@ -92,6 +91,8 @@ CK_DEFINE_FUNCTION(CK_RV, C_Finalize)(
return CKR_CRYPTOKI_NOT_INITIALIZED; return CKR_CRYPTOKI_NOT_INITIALIZED;
} }
memset(slots, 0, sizeof(slots));
ykpiv_done(piv_state); // TODO: this calls disconnect... ykpiv_done(piv_state); // TODO: this calls disconnect...
piv_state == NULL; piv_state == NULL;
@@ -146,28 +147,38 @@ CK_DEFINE_FUNCTION(CK_RV, C_GetSlotList)(
{ {
DIN; DIN;
int i; int i;
int j;
// TODO: what about tokenPresent?
//ykpiv_get_reader_slot_number(piv_state, &n_readers, &tot_reader_len); // TODO: maybe refactor this with a reader struct? //ykpiv_get_reader_slot_number(piv_state, &n_readers, &tot_reader_len); // TODO: maybe refactor this with a reader struct?
if (pSlotList == NULL_PTR) { if (pSlotList == NULL_PTR) {
// Just return the number of slots // Just return the number of slots
*pulCount = n_slots; *pulCount = n_slots;
if (tokenPresent)
*pulCount = n_tokenless_slots;
else
*pulCount = n_slots;
DOUT; DOUT;
return CKR_OK; return CKR_OK;
} }
if (*pulCount < n_slots) { if ((tokenPresent && *pulCount < n_tokenless_slots) || (!tokenPresent && *pulCount < n_slots)) {
DBG(("Buffer too small: needed %lu, provided %lu", n_slots, *pulCount)); DBG(("Buffer too small: needed %lu, provided %lu", n_slots, *pulCount));
return CKR_BUFFER_TOO_SMALL; return CKR_BUFFER_TOO_SMALL;
} }
for (i = 0; i < n_slots; i++) { for (j = 0, i = 0; i < n_slots; i++) {
if (tokenPresent) {
if (has_token(slots + i))
pSlotList[j++] = i;
}
else
pSlotList[i] = i; pSlotList[i] = i;
} }
DBG(("%d token", tokenPresent)); DBG(("token present is %d", tokenPresent));
DBG(("%lu count", *pulCount)); DBG(("number of slot(s) is %lu", *pulCount));
DOUT; DOUT;
return CKR_OK; return CKR_OK;
@@ -188,7 +199,7 @@ CK_DEFINE_FUNCTION(CK_RV, C_GetSlotInfo)(
memcpy(pInfo, &slots[slotID].info, sizeof(CK_SLOT_INFO)); memcpy(pInfo, &slots[slotID].info, sizeof(CK_SLOT_INFO));
DBG(("slotID %lu, pInfo %s", slotID, pInfo->slotDescription)); //DBG(("slotID %lu, pInfo %s", slotID, pInfo->slotDescription));
DOUT; DOUT;
return CKR_OK; return CKR_OK;
@@ -220,7 +231,7 @@ CK_DEFINE_FUNCTION(CK_RV, C_GetTokenInfo)(
return CKR_TOKEN_NOT_RECOGNIZED; return CKR_TOKEN_NOT_RECOGNIZED;
} }
if (!HAS_TOKEN(slotID)) { if (!has_token(slots + slotID)) {
DBG(("Slot %lu has no token inserted", slotID)); DBG(("Slot %lu has no token inserted", slotID));
return CKR_TOKEN_NOT_PRESENT; return CKR_TOKEN_NOT_PRESENT;
} }
@@ -330,9 +341,9 @@ CK_DEFINE_FUNCTION(CK_RV, C_GetMechanismInfo)(
CK_MECHANISM_INFO_PTR pInfo CK_MECHANISM_INFO_PTR pInfo
) )
{ {
//DIN; DIN;
//DBG(("TODO!!!")); DBG(("TODO!!!"));
//DOUT; DOUT;
return CKR_OK; return CKR_OK;
} }