Add more precondition checks and debug messages.

This commit is contained in:
Alessio Di Mauro
2015-08-20 16:25:22 +02:00
parent 8618469619
commit f27ca3837c
2 changed files with 43 additions and 21 deletions
+2 -2
View File
@@ -1,8 +1,8 @@
#ifndef DEBUG_H
#define DEBUG_H
#define YKCS11_DBG 1 // General debug, must be either 1 or 0
#define YKCS11_DINOUT 1 // Function in/out debug, must be either 1 or 0
#define YKCS11_DBG 0 // General debug, must be either 1 or 0
#define YKCS11_DINOUT 0 // Function in/out debug, must be either 1 or 0
#define D(x) do { \
printf ("debug: %s:%d (%s): ", __FILE__, __LINE__, __FUNCTION__); \
+37 -15
View File
@@ -149,7 +149,11 @@ CK_DEFINE_FUNCTION(CK_RV, C_GetSlotList)(
int i;
int j;
// TODO: check more preconditions
if (piv_state == NULL) {
DBG(("libykpiv is not initialized or already finalized"));
return CKR_CRYPTOKI_NOT_INITIALIZED;
}
if (pSlotList == NULL_PTR) {
// Just return the number of slots
*pulCount = n_slots;
@@ -196,8 +200,10 @@ CK_DEFINE_FUNCTION(CK_RV, C_GetSlotInfo)(
return CKR_CRYPTOKI_NOT_INITIALIZED;
}
if (slotID >= n_slots)
return CKR_ARGUMENTS_BAD;
if (slotID >= n_slots) {
DBG(("Invalid slot ID %lu, slotID"));
return CKR_SLOT_ID_INVALID;
}
memcpy(pInfo, &slots[slotID].info, sizeof(CK_SLOT_INFO));
@@ -220,12 +226,14 @@ CK_DEFINE_FUNCTION(CK_RV, C_GetTokenInfo)(
return CKR_CRYPTOKI_NOT_INITIALIZED;
}
if (slotID >= n_slots)
return CKR_ARGUMENTS_BAD;
if (slotID >= n_slots) {
DBG(("Invalid slot ID %lu, slotID"));
return CKR_SLOT_ID_INVALID;
}
if (slots[slotID].vid == UNKNOWN) {
DBG(("No support for slot %lu", slotID));
return CKR_TOKEN_NOT_RECOGNIZED;
return CKR_SLOT_ID_INVALID;
}
if (!has_token(slots + slotID)) {
@@ -242,7 +250,7 @@ CK_DEFINE_FUNCTION(CK_RV, C_GetTokenInfo)(
memcpy(pInfo, &slots[slotID].token->info, sizeof(CK_TOKEN_INFO));
// Overwrite value that are application specific
// Overwrite values that are application specific
pInfo->ulMaxSessionCount = CK_UNAVAILABLE_INFORMATION; // TODO: should this be 1?
pInfo->ulSessionCount = CK_UNAVAILABLE_INFORMATION; // number of sessions that this application currently has open with the token
@@ -287,8 +295,15 @@ CK_DEFINE_FUNCTION(CK_RV, C_GetMechanismList)(
return CKR_CRYPTOKI_NOT_INITIALIZED;
}
if (slotID > n_slots || pulCount == NULL_PTR)
if (slotID >= n_slots) {
DBG(("Invalid slot ID %lu", slotID));
return CKR_SLOT_ID_INVALID;
}
if (pulCount == NULL_PTR) {
DBG(("Wrong/Missing parameter"));
return CKR_ARGUMENTS_BAD;
}
if (slots[slotID].vid == UNKNOWN) {
DBG(("Slot %lu is tokenless/unsupported", slotID));
@@ -296,7 +311,6 @@ CK_DEFINE_FUNCTION(CK_RV, C_GetMechanismList)(
}
// TODO: check more return values
// TODO: user NULL_PTR more for coherence
token = get_token_vendor(slots[slotID].vid);
@@ -315,8 +329,10 @@ CK_DEFINE_FUNCTION(CK_RV, C_GetMechanismList)(
return CKR_BUFFER_TOO_SMALL;
}
if (token.get_token_mechanism_list(pMechanismList, *pulCount) != CKR_OK)
if (token.get_token_mechanism_list(pMechanismList, *pulCount) != CKR_OK) {
DBG(("Unable to retrieve mechanism list"));
return CKR_FUNCTION_FAILED;
}
DOUT;
return CKR_OK;
@@ -336,21 +352,27 @@ CK_DEFINE_FUNCTION(CK_RV, C_GetMechanismInfo)(
return CKR_CRYPTOKI_NOT_INITIALIZED;
}
if (slotID > n_slots || pInfo == NULL_PTR)
if (slotID >= n_slots) {
DBG(("Invalid slot ID %lu, slotID"));
return CKR_SLOT_ID_INVALID;
}
if (pInfo == NULL_PTR) {
DBG(("Wrong/Missing parameter"));
return CKR_ARGUMENTS_BAD;
}
if (slots[slotID].vid == UNKNOWN) {
DBG(("Slot %lu is tokenless/unsupported", slotID));
return CKR_SLOT_ID_INVALID;
}
// TODO: check more return values
// TODO: user NULL_PTR more for coherence
token = get_token_vendor(slots[slotID].vid);
if (token.get_token_mechanism_info(type, pInfo) != CKR_OK)
if (token.get_token_mechanism_info(type, pInfo) != CKR_OK) {
DBG(("Unable to retrieve mechanism information"));
return CKR_MECHANISM_INVALID;
}
DOUT;
return CKR_OK;