diff options
Diffstat (limited to 'src/slot.c')
-rw-r--r-- | src/slot.c | 162 |
1 files changed, 92 insertions, 70 deletions
@@ -7,22 +7,22 @@ #include <stdio.h> #include <string.h> -CK_INFO p11t_slot_global; +CK_INFO slot_global; CK_ULONG p11t_slot_count = 0; -CK_SLOT_ID_PTR p11t_slot_ids = NULL; -CK_SLOT_INFO_PTR p11t_slot_info = NULL; -CK_TOKEN_INFO_PTR p11t_slot_token_info = NULL; +static CK_SLOT_ID_PTR slot_ids = NULL; +static CK_SLOT_INFO_PTR slot_info = NULL; +static CK_TOKEN_INFO_PTR slot_token_info = NULL; -CK_ULONG *p11t_slot_mech_count; -CK_MECHANISM_INFO_PTR *p11t_slot_mech_info; +static CK_ULONG *slot_mech_count; +static CK_MECHANISM_INFO_PTR *slot_mech_info; /* ---------------------------------------------------------------------------------- * TESTS */ void -slot_global(void) +test_slot_global(void) { CK_RV rv; @@ -38,22 +38,22 @@ slot_global(void) } /* Obvious crap fill */ - memset(&p11t_slot_global, 0xFF, sizeof(CK_INFO)); + memset(&slot_global, 0xFF, sizeof(CK_INFO)); /** - Normal call */ - rv = (p11t_module_funcs->C_GetInfo)(&p11t_slot_global); + rv = (p11t_module_funcs->C_GetInfo)(&slot_global); if(p11t_check_returns("C_GetInfo", rv, CKR_OK)) { - memset(&p11t_slot_global, 0, sizeof(CK_INFO)); + memset(&slot_global, 0, sizeof(CK_INFO)); return; } /** - Space padded strings in CK_INFO */ - p11t_check_padded("CK_INFO.manufacturerID", p11t_slot_global.manufacturerID); - p11t_check_padded("CK_INFO.libraryDescription", p11t_slot_global.libraryDescription); + p11t_check_padded("CK_INFO.manufacturerID", slot_global.manufacturerID); + p11t_check_padded("CK_INFO.libraryDescription", slot_global.libraryDescription); /** - No flags set */ - p11t_check_ulong("CK_INFO.flags", p11t_slot_global.flags, 0); + p11t_check_ulong("CK_INFO.flags", slot_global.flags, 0); } int @@ -69,7 +69,7 @@ compar_slot_id(const void *one, const void *two) } void -slot_info(void) +test_slot_info(void) { CK_SLOT_ID_PTR present, only; CK_ULONG n_present, n_only, count; @@ -98,20 +98,20 @@ slot_info(void) } /* Allocate a bit extra. We're going to try and trip up module */ - p11t_slot_ids = calloc(p11t_slot_count + 5, sizeof(CK_SLOT_ID)); - assert(p11t_slot_ids); + slot_ids = calloc(p11t_slot_count + 5, sizeof(CK_SLOT_ID)); + assert(slot_ids); - p11t_slot_info = calloc(p11t_slot_count, sizeof(CK_SLOT_INFO)); - assert(p11t_slot_info); + slot_info = calloc(p11t_slot_count, sizeof(CK_SLOT_INFO)); + assert(slot_info); - p11t_slot_token_info = calloc(p11t_slot_count, sizeof(CK_TOKEN_INFO)); - assert(p11t_slot_token_info); + slot_token_info = calloc(p11t_slot_count, sizeof(CK_TOKEN_INFO)); + assert(slot_token_info); if(p11t_test_unexpected) { /** - Passing buffer space along with zero count. */ count = 0; - rv = (p11t_module_funcs->C_GetSlotList)(FALSE, p11t_slot_ids, &count); + rv = (p11t_module_funcs->C_GetSlotList)(FALSE, slot_ids, &count); p11t_check_returns("C_GetSlotList: zero buffer", rv, CKR_BUFFER_TOO_SMALL); p11t_check_ulong("C_GetSlotList: count invalid zero space passed", count, p11t_slot_count); @@ -119,7 +119,7 @@ slot_info(void) { /** - Passing buffer space along with low count. */ count = 1; - rv = (p11t_module_funcs->C_GetSlotList)(FALSE, p11t_slot_ids, &count); + rv = (p11t_module_funcs->C_GetSlotList)(FALSE, slot_ids, &count); p11t_check_returns("C_GetSlotList: low buffer", rv, CKR_BUFFER_TOO_SMALL); p11t_check_ulong("C_GetSlotList: count invalid when too little space passed", count, p11t_slot_count); } @@ -130,7 +130,7 @@ slot_info(void) count = p11t_slot_count + 5; else count = p11t_slot_count; - rv = (p11t_module_funcs->C_GetSlotList)(FALSE, p11t_slot_ids, &count); + rv = (p11t_module_funcs->C_GetSlotList)(FALSE, slot_ids, &count); if(!p11t_check_returns("C_GetSlotList", rv, CKR_OK)) { p11t_slot_count = 0; @@ -153,49 +153,49 @@ slot_info(void) if(p11t_test_unexpected) { /** - NULL argument */ - rv = (p11t_module_funcs->C_GetSlotInfo)(p11t_slot_ids[i], NULL); + rv = (p11t_module_funcs->C_GetSlotInfo)(slot_ids[i], NULL); p11t_check_returns("C_GetSlotInfo: null argument", rv, CKR_ARGUMENTS_BAD); } /** - Normal call */ - rv = (p11t_module_funcs->C_GetSlotInfo)(p11t_slot_ids[i], &p11t_slot_info[i]); + rv = (p11t_module_funcs->C_GetSlotInfo)(slot_ids[i], &slot_info[i]); p11t_check_returns("C_GetSlotInfo", rv, CKR_OK); /** - Space padded CK_SLOT_INFO fields */ - p11t_check_padded("CK_SLOT_INFO.slotDescription", p11t_slot_info[i].slotDescription); - p11t_check_padded("CK_SLOT_INFO.manufacturerID", p11t_slot_info[i].manufacturerID); + p11t_check_padded("CK_SLOT_INFO.slotDescription", slot_info[i].slotDescription); + p11t_check_padded("CK_SLOT_INFO.manufacturerID", slot_info[i].manufacturerID); /** - CK_SLOT_INFO flags are from valid set */ - p11t_check_mask("CK_SLOT_INFO.flags", p11t_slot_info[i].flags, + p11t_check_mask("CK_SLOT_INFO.flags", slot_info[i].flags, CKF_TOKEN_PRESENT | CKF_REMOVABLE_DEVICE | CKF_HW_SLOT); /** - Track CKF_TOKEN_PRESENT flag and compare to C_GetSlotList(TRUE) */ - if((p11t_slot_info[i].flags & CKF_TOKEN_PRESENT) == CKF_TOKEN_PRESENT) + if((slot_info[i].flags & CKF_TOKEN_PRESENT) == CKF_TOKEN_PRESENT) { /* Note if token is present for later */ - present[n_present++] = p11t_slot_ids[i]; + present[n_present++] = slot_ids[i]; /** C_GetTokenInfo */ if(p11t_test_unexpected) { /** - Null arguments */ - rv = (p11t_module_funcs->C_GetTokenInfo)(p11t_slot_ids[i], NULL); + rv = (p11t_module_funcs->C_GetTokenInfo)(slot_ids[i], NULL); p11t_check_returns("C_GetTokenInfo: null arguments", rv, CKR_ARGUMENTS_BAD); } /** - Normal call */ - rv = (p11t_module_funcs->C_GetTokenInfo)(p11t_slot_ids[i], &p11t_slot_token_info[i]); + rv = (p11t_module_funcs->C_GetTokenInfo)(slot_ids[i], &slot_token_info[i]); p11t_check_returns("C_GetTokenInfo", rv, CKR_OK); /** - Space padded CK_TOKEN_INFO fields */ - p11t_check_padded("CK_TOKEN_INFO.label", p11t_slot_token_info[i].label); - p11t_check_padded("CK_TOKEN_INFO.manufacturerID", p11t_slot_token_info[i].manufacturerID); - p11t_check_padded("CK_TOKEN_INFO.model", p11t_slot_token_info[i].model); - p11t_check_padded("CK_TOKEN_INFO.serialNumber", p11t_slot_token_info[i].serialNumber); + p11t_check_padded("CK_TOKEN_INFO.label", slot_token_info[i].label); + p11t_check_padded("CK_TOKEN_INFO.manufacturerID", slot_token_info[i].manufacturerID); + p11t_check_padded("CK_TOKEN_INFO.model", slot_token_info[i].model); + p11t_check_padded("CK_TOKEN_INFO.serialNumber", slot_token_info[i].serialNumber); /** - CK_TOKEN_INFO flags are from valid set */ - p11t_check_mask("CK_TOKEN_INFO.flags", p11t_slot_token_info[i].flags, + p11t_check_mask("CK_TOKEN_INFO.flags", slot_token_info[i].flags, CKF_RNG | CKF_WRITE_PROTECTED | CKF_LOGIN_REQUIRED | CKF_USER_PIN_INITIALIZED | CKF_RESTORE_KEY_NOT_NEEDED | CKF_CLOCK_ON_TOKEN | CKF_PROTECTED_AUTHENTICATION_PATH | CKF_DUAL_CRYPTO_OPERATIONS | CKF_TOKEN_INITIALIZED | CKF_SECONDARY_AUTHENTICATION | @@ -206,14 +206,14 @@ slot_info(void) /* Can't validate all the statistics, any number is valid */ /** - Validate token time when CKF_CLOCK_ON_TOKEN */ - if((p11t_slot_token_info[i].flags & CKF_CLOCK_ON_TOKEN) == CKF_CLOCK_ON_TOKEN) + if((slot_token_info[i].flags & CKF_CLOCK_ON_TOKEN) == CKF_CLOCK_ON_TOKEN) { int year, month, day, hour, minute, second, extra, n; - char buffer[sizeof(p11t_slot_token_info[i].utcTime) + 1]; + char buffer[sizeof(slot_token_info[i].utcTime) + 1]; - memcpy(buffer, (char*)p11t_slot_token_info[i].utcTime, - sizeof(p11t_slot_token_info[i].utcTime)); - buffer[sizeof(p11t_slot_token_info[i].utcTime)] = 0; + memcpy(buffer, (char*)slot_token_info[i].utcTime, + sizeof(slot_token_info[i].utcTime)); + buffer[sizeof(slot_token_info[i].utcTime)] = 0; n = sscanf(buffer, "%04d%02d%02d%02d%02d%02d%02d", &year, &month, &day, &hour, &minute, &second, &extra); @@ -234,7 +234,7 @@ slot_info(void) else if(p11t_test_unexpected) { /** - Calling on slot without token */ - rv = (p11t_module_funcs->C_GetTokenInfo)(p11t_slot_ids[i], &p11t_slot_token_info[i]); + rv = (p11t_module_funcs->C_GetTokenInfo)(slot_ids[i], &slot_token_info[i]); p11t_check_returns("C_GetSlotInfo: without token", rv, CKR_TOKEN_NOT_PRESENT); } } @@ -251,14 +251,14 @@ slot_info(void) } void -slot_events(void) +test_slot_events(void) { /** C_WaitForSlotEvent */ /** - Not Implemented */ } void -slot_mechanisms(void) +test_slot_mechanisms(void) { CK_MECHANISM_TYPE_PTR mech_list; CK_MECHANISM_INFO_PTR mech_info; @@ -282,16 +282,16 @@ slot_mechanisms(void) p11t_check_returns("C_GetMechanismList: invalid slot id", rv, CKR_SLOT_ID_INVALID); } - p11t_slot_mech_count = calloc(p11t_slot_count, sizeof(CK_ULONG)); - assert(p11t_slot_mech_count); + slot_mech_count = calloc(p11t_slot_count, sizeof(CK_ULONG)); + assert(slot_mech_count); - p11t_slot_mech_info = calloc(p11t_slot_count, sizeof(CK_MECHANISM_INFO_PTR)); - assert(p11t_slot_mech_info); + slot_mech_info = calloc(p11t_slot_count, sizeof(CK_MECHANISM_INFO_PTR)); + assert(slot_mech_info); for(i = 0; i < p11t_slot_count; ++i) { - slot_id = p11t_slot_ids[i]; + slot_id = slot_ids[i]; if(p11t_test_unexpected) { @@ -374,15 +374,15 @@ slot_mechanisms(void) p11t_check_nflag("CK_MECHANISM_INFO.flags", mech_info[i].flags, CKF_EXTENSION); } - p11t_slot_mech_info[i] = mech_info; + slot_mech_info[i] = mech_info; } - p11t_slot_mech_count[i] = mech_count; + slot_mech_count[i] = mech_count; } } void -slot_init_token(void) +test_init_token(void) { /** C_InitToken */ @@ -392,35 +392,57 @@ slot_init_token(void) void p11t_slot_tests(void) { - slot_global(); - slot_info(); - slot_events(); - slot_mechanisms(); + test_slot_global(); + test_slot_info(); + test_slot_events(); + test_init_token(); + test_slot_mechanisms(); } /* ---------------------------------------------------------------------------------- * METHODS */ -CK_TOKEN_INFO_PTR -p11t_slot_get_token_info(CK_SLOT_ID slot) +static int +get_slot_index(CK_SLOT_ID slot) { CK_ULONG i; for(i = 0; i < p11t_slot_count; ++i) { - if(slot == p11t_slot_ids[i]) - { - if(p11t_slot_info[i].flags & CKF_TOKEN_PRESENT) - { - return &p11t_slot_token_info[i]; - } - else - { - return NULL; - } - } + if(slot == slot_ids[i]) + return i; } + return -1; +} + +CK_SLOT_ID +p11t_slot_get_id(int index) +{ + if(index >= p11t_slot_count) + return CK_INVALID; + return slot_ids[index]; +} + +CK_SLOT_INFO_PTR +p11t_slot_get_info(CK_SLOT_ID slot) +{ + int i = get_slot_index(slot); + if(i < 0) + return NULL; + return &slot_info[i]; +} + +CK_TOKEN_INFO_PTR +p11t_slot_get_token_info(CK_SLOT_ID slot) +{ + int i = get_slot_index(slot); + if(i < 0) + return NULL; + + if(slot_info[i].flags & CKF_TOKEN_PRESENT) + return &slot_token_info[i]; + return NULL; } |