Cryptoki: a cryptographic Token Interface


Slot and token management functions



Download 360.55 Kb.
Page81/196
Date22.12.2023
Size360.55 Kb.
#63026
1   ...   77   78   79   80   81   82   83   84   ...   196
v201-95
pkcs11-base-v2.40-cos01

10.5. Slot and token management functions


Cryptoki provides the following functions for slot and token management:
  • C_GetSlotList


CK_DEFINE_FUNCTION(CK_RV, C_GetSlotList)(
CK_BBOOL tokenPresent,
CK_SLOT_ID_PTR pSlotList,
CK_ULONG_PTR pulCount
);
C_GetSlotList is used to obtain a list of slots in the system. tokenPresent indicates whether the list obtained includes only those slots with a token present (TRUE), or all slots (FALSE); pulCount points to the location that receives the number of slots.
There are two ways for an application to call C_GetSlotList:

  1. If pSlotList is NULL_PTR, then all that C_GetSlotList does is return (in *pulCount) the number of slots, without actually returning a list of slots. The contents of the buffer pointed to by pulCount on entry to C_GetSlotList has no meaning in this case, and the call returns the value CKR_OK.

  2. If pSlotList is not NULL_PTR, then *pulCount must contain the size (in terms of CK_SLOT_ID elements) of the buffer pointed to by pSlotList. If that buffer is large enough to hold the list of slots, then the list is returned in it, and CKR_OK is returned. If not, then the call to C_GetSlotList returns the value CKR_BUFFER_TOO_SMALL. In either case, the value *pulCount is set to hold the number of slots.

Because C_GetSlotList does not allocate any space of its own, an application will often call C_GetSlotList twice (or sometimes even more times—if an application is trying to get a list of all slots with a token present, then the number of such slots can (unfortunately) change between when the application asks for how many such slots there are and when the application asks for the slots themselves). However, multiple calls to C_GetSlotList are by no means required.
All slots which C_GetSlotList reports must be able to be queried as valid slots by C_GetSlotInfo. Furthermore, the set of slots accessible through a Cryptoki library is fixed at the time that C_Initialize is called. If an application calls C_Initialize and C_GetSlotList, and then the user hooks up a new hardware device, that device cannot suddenly appear as a new slot if C_GetSlotList is called again. To recognize the new device, C_Initialize needs to be called again (and to be able to call C_Initialize successfully, C_Finalize needs to be called first). Even if C_Initialize is successfully called, it may or may not be the case that the new device will then be successfully recognized. On some platforms, it may be necessary to restart the entire system.
Return values: CKR_BUFFER_TOO_SMALL, CKR_CRYPTOKI_NOT_INITIALIZED, CKR_FUNCTION_FAILED, CKR_GENERAL_ERROR, CKR_HOST_MEMORY, CKR_OK.
Example:
CK_ULONG ulSlotCount, ulSlotWithTokenCount;
CK_SLOT_ID_PTR pSlotList, pSlotWithTokenList;
CK_RV rv;

/* Get list of all slots */


rv = C_GetSlotList(FALSE, NULL_PTR, &ulSlotCount);
if (rv == CKR_OK) {
pSlotList =
(CK_SLOT_ID_PTR) malloc(ulSlotCount*sizeof(CK_SLOT_ID));
rv = C_GetSlotList(FALSE, pSlotList, &ulSlotCount);
if (rv == CKR_OK) {
/* Now use that list of all slots */
.
.
.
}

free(pSlotList);


}

/* Get list of all slots with a token present */


pSlotWithTokenList = (CK_SLOT_ID_PTR) malloc(0);
ulSlotWithTokenCount = 0;
while (1) {
rv = C_GetSlotList(
TRUE, pSlotWithTokenList, ulSlotWithTokenCount);
if (rv != CKR_BUFFER_TOO_SMALL)
break;
pSlotWithTokenList = realloc(
pSlotWithTokenList,
ulSlotWithTokenList*sizeof(CK_SLOT_ID));
}

if (rv == CKR_OK) {


/* Now use that list of all slots with a token present */
.
.
.
}

free(pSlotWithTokenList);


1   ...   77   78   79   80   81   82   83   84   ...   196




The database is protected by copyright ©ininet.org 2024
send message

    Main page