Attached is a patch file. Renamed api_func.c to slot_token.c. Goal is to
create testcases for every function subset, such as general functions,
slot/token, session, etc.t a/testcases/pkcs11/Makefile.am b/testcases/pkcs11/Makefile.am
index 81f58ab..bd0a948 100644
--- a/testcases/pkcs11/Makefile.am
+++ b/testcases/pkcs11/Makefile.am
@@ -1,4 +1,4 @@
-noinst_PROGRAMS=hw_fn misc_tests sess_mgmt_tests sess_bench attribute findobjects destroyobjects copyobjects api_tests
+noinst_PROGRAMS=hw_fn misc_tests sess_mgmt_tests sess_bench attribute findobjects destroyobjects copyobjects slot_token_tests
AM_CFLAGS=-I. -I../../usr/include/pkcs11 -I../include -I../common -I../../usr/lib/pkcs11/common -Wall
@@ -12,4 +12,4 @@ attribute_SOURCES = attribute.c
findobjects_SOURCES = findobjects.c
destroyobjects_SOURCES = destroyobjects.c
copyobjects_SOURCES = copyobjects.c
-api_tests_SOURCES = api_func.c
+slot_token_tests_SOURCES = slot_token.c
diff --git a/testcases/pkcs11/api_func.c b/testcases/pkcs11/api_func.c
deleted file mode 100644
index c14134d..0000000
--- a/testcases/pkcs11/api_func.c
+++ /dev/null
@@ -1,401 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <memory.h>
-
-#include "pkcs11types.h"
-#include "regress.h"
-#include "common.c"
-
-CK_RV do_GetInfo(void)
-{
-
- CK_FLAGS flags;
- CK_SESSION_HANDLE session;
- CK_RV rc = 0;
- CK_BYTE user_pin[PKCS11_MAX_PIN_LEN];
- CK_ULONG user_pin_len;
- CK_INFO info;
-
- // Do some setup and login to the token
- testcase_begin("C_GetInfo function check");
- testcase_rw_session();
- testcase_user_login();
-
- testcase_new_assertion();
-
- rc = funcs->C_GetInfo(&info);
-
- if(rc != CKR_OK){
- testcase_fail("C_GetInfo() rc=%s", p11_get_ckr(rc));
- goto testcase_cleanup;
- }
-
- printf("Cryptoki Version: %d.%d\n", info.cryptokiVersion.major, info.cryptokiVersion.minor);
- printf("Manufacturer ID: %s\n", info.manufacturerID);
- printf("Library Description: %s\n", info.libraryDescription);
- printf("Library Version: %d.%d\n", info.libraryVersion.major, info.libraryVersion.minor);
-
-
- testcase_pass();
-
-testcase_cleanup:
- testcase_user_logout();
- rc = funcs->C_CloseSession(session);
- if (rc != CKR_OK) {
- testcase_error("C_CloseSessions rc=%s", p11_get_ckr(rc));
- }
-
- return rc;
-
-}
-
-CK_RV do_GetSlotList(void)
-{
- CK_FLAGS flags;
- CK_SESSION_HANDLE session;
- CK_RV rc = 0;
- CK_BYTE user_pin[PKCS11_MAX_PIN_LEN];
- CK_ULONG user_pin_len;
-
- CK_BBOOL tokenPresent;
- CK_SLOT_ID_PTR pSlotList;
- CK_ULONG ulCount;
-
- tokenPresent = TRUE;
-
- testcase_begin("C_GetSlotList function check");
- testcase_rw_session();
- testcase_user_login();
-
- testcase_new_assertion();
-
- rc = funcs->C_GetSlotList( tokenPresent, NULL, &ulCount );
- if (rc != CKR_OK) {
- testcase_fail("C_GetSlotList rc=%s", p11_get_ckr(rc));
- return rc;
- }
-
- pSlotList = (CK_SLOT_ID *)malloc( ulCount * sizeof(CK_SLOT_ID) );
- if (!pSlotList) {
- testcase_fail("DRIVER ERROR: CANNOT ALLOCATE MEMORY FOR SLOT LIST\n");
- return -1;
- }
-
- rc = funcs->C_GetSlotList( tokenPresent, pSlotList, &ulCount );
- if (rc != CKR_OK) {
- testcase_fail("C_GetSlotList rc=%s", p11_get_ckr(rc));
- return rc;
- }
-
- testcase_pass();
-
- free(pSlotList);
-
-testcase_cleanup:
- testcase_user_logout();
- rc = funcs->C_CloseSession(session);
- if (rc != CKR_OK) {
- testcase_error("C_CloseSessions rc=%s", p11_get_ckr(rc));
- }
-
- return rc;
-
-}
-
-CK_RV do_GetSlotInfo(void)
-{
- CK_FLAGS flags;
- CK_SESSION_HANDLE session;
- CK_RV rc = 0;
- CK_BYTE user_pin[PKCS11_MAX_PIN_LEN];
- CK_ULONG user_pin_len;
-
- CK_SLOT_ID slot_id = SLOT_ID;
- CK_SLOT_INFO info;
-
- testcase_begin("Display C_GetSlotInfo return data");
- testcase_rw_session();
- testcase_user_login();
-
- testcase_new_assertion();
-
- rc = funcs->C_GetSlotInfo(slot_id, &info);
- if(rc != CKR_OK){
- testcase_fail("C_GetSlotInfo() rc = %s", p11_get_ckr(rc));
- goto testcase_cleanup;
- }
-
- printf(" CK_SLOT_INFO for slot %d:\n", (int)slot_id);
- printf(" slotDescription: %s\n", info.slotDescription );
- printf(" manufacturerID: %s\n", info.manufacturerID );
- printf(" flags: %p\n", (void *)info.flags );
- printf(" hardwareVersion: %d.%d\n", info.hardwareVersion.major, info.hardwareVersion.minor );
- printf(" firmwareVersion: %d.%d\n", info.firmwareVersion.major, info.firmwareVersion.minor );
-
- testcase_pass();
-
-testcase_cleanup:
- testcase_user_logout();
- rc = funcs->C_CloseSession(session);
- if (rc != CKR_OK) {
- testcase_error("C_CloseSessions rc=%s", p11_get_ckr(rc));
- }
-
- return rc;
-
-}
-
-CK_RV do_GetTokenInfo(void)
-{
-
- CK_FLAGS flags;
- CK_SESSION_HANDLE session;
- CK_RV rc = 0;
- CK_BYTE user_pin[PKCS11_MAX_PIN_LEN];
- CK_ULONG user_pin_len;
-
- CK_SLOT_ID slot_id = SLOT_ID;
- CK_TOKEN_INFO info;
-
- testcase_begin("Display C_GetTokenInfo() return data");
- testcase_rw_session();
- testcase_user_login();
-
- testcase_new_assertion();
-
- rc = funcs->C_GetTokenInfo( slot_id, &info );
- if (rc != CKR_OK) {
- testcase_fail("C_GetTokenInfo rc=%s", p11_get_ckr(rc));
- return rc;
- }
-
- printf(" CK_TOKEN_INFO for slot #%d:\n", (int)slot_id);
- printf(" label: %s\n", info.label );
- printf(" manufacturerID: %s\n", info.manufacturerID );
- printf(" model: %s\n", info.model );
- printf(" serialNumber: %s\n", info.serialNumber );
- printf(" flags: %p\n", (void *)info.flags );
- printf(" ulMaxSessionCount: %ld\n", info.ulMaxSessionCount );
- printf(" ulSessionCount: %ld\n", info.ulSessionCount );
- printf(" ulMaxRwSessionCount: %ld\n", info.ulMaxRwSessionCount );
- printf(" ulRwSessionCount: %ld\n", info.ulRwSessionCount );
- printf(" ulMaxPinLen: %ld\n", info.ulMaxPinLen );
- printf(" ulMinPinLen: %ld\n", info.ulMinPinLen );
- printf(" ulTotalPublicMemory: %ld\n", info.ulTotalPublicMemory );
- printf(" ulFreePublicMemory: %ld\n", info.ulFreePublicMemory );
- printf(" ulTotalPrivateMemory: %ld\n", info.ulTotalPrivateMemory );
- printf(" ulFreePrivateMemory: %ld\n", info.ulFreePrivateMemory );
- printf(" hardwareVersion: %d.%d\n", info.hardwareVersion.major, info.hardwareVersion.minor );
- printf(" firmwareVersion: %d.%d\n", info.firmwareVersion.major, info.firmwareVersion.minor );
- printf(" time: %s\n", info.utcTime );
-
- testcase_pass();
-
-testcase_cleanup:
- testcase_user_logout();
- rc = funcs->C_CloseSession(session);
- if (rc != CKR_OK) {
- testcase_error("C_CloseSessions rc=%s", p11_get_ckr(rc));
- }
-
- return rc;
-
-}
-
-CK_RV do_GetMechanismList(void)
-{
- CK_FLAGS flags;
- CK_SESSION_HANDLE session;
- CK_RV rc = 0;
- CK_BYTE user_pin[PKCS11_MAX_PIN_LEN];
- CK_ULONG user_pin_len;
-
- CK_SLOT_ID slot_id = SLOT_ID;
- CK_ULONG count;
- CK_MECHANISM_TYPE *mech_list;
-
- testcase_begin("Function check and could display of C_GetMechanismList");
- testcase_rw_session();
- testcase_user_login();
-
- testcase_new_assertion();
-
- rc = funcs->C_GetMechanismList(slot_id, NULL, &count);
- if (rc != CKR_OK) {
- testcase_fail("C_GetMechanismList 1 rc=%s",p11_get_ckr(rc));
- return rc;
- }
-
- printf("C_GetMechanismList #1 returned %ld mechanisms\n", count );
-
- mech_list = (CK_MECHANISM_TYPE *)malloc( count * sizeof(CK_MECHANISM_TYPE) );
- if (!mech_list){
- testcase_fail();
- return CKR_HOST_MEMORY;
- }
- rc = funcs->C_GetMechanismList(slot_id, mech_list, &count);
- if (rc != CKR_OK) {
- testcase_fail("C_GetMechanismList 2 rc=%s", p11_get_ckr(rc));
- return rc;
- }
-
- free( mech_list );
-
- testcase_pass();
-
-testcase_cleanup:
- testcase_user_logout();
- rc = funcs->C_CloseSession(session);
- if (rc != CKR_OK) {
- testcase_error("C_CloseSessions rc=%s", p11_get_ckr(rc));
- }
-
- return rc;
-
-}
-
-CK_RV do_GetMechanismInfo(void)
-{
- CK_FLAGS flags;
- CK_SESSION_HANDLE session;
- CK_RV rc = 0;
- CK_BYTE user_pin[PKCS11_MAX_PIN_LEN];
- CK_ULONG user_pin_len;
-
- CK_SLOT_ID slot_id = SLOT_ID;
- CK_MECHANISM_INFO info;
- CK_ULONG i, count;
- CK_MECHANISM_TYPE *mech_list;
-
- testcase_begin("Gather list of mechanisms via C_GetMechanismList");
- testcase_rw_session();
- testcase_user_login();
-
- testcase_new_assertion();
-
- rc = funcs->C_GetMechanismList( slot_id, NULL, &count );
- if (rc != CKR_OK) {
- testcase_fail("C_GetMechanismList #1 rc=%s", p11_get_ckr(rc));
- return rc;
- }
-
- testcase_pass();
- testcase_begin("Display information of each mechanism");
- testcase_new_assertion();
-
- mech_list = (CK_MECHANISM_TYPE *)malloc( count * sizeof(CK_MECHANISM_TYPE) );
- if (!mech_list){
- testcase_fail();
- return CKR_HOST_MEMORY;
- }
- rc = funcs->C_GetMechanismList( slot_id, mech_list, &count );
- if (rc != CKR_OK) {
- testcase_fail("C_GetMechanismList #2 rc=%s", p11_get_ckr(rc));
- return rc;
- }
-
- for (i=0; i < count; i++) {
- rc = funcs->C_GetMechanismInfo( slot_id, mech_list[i], &info );
- if (rc != CKR_OK) {
- testcase_fail("C_GetMechanismInfo rc=%s", p11_get_ckr(rc));
- testcase_error("Failed to get info on mechanism # %ld\n", mech_list[i]);
- return rc;
- }
-
- printf(" Mechanism #%ld %s\n", mech_list[i], p11_get_ckm(mech_list[i]));
- printf(" ulMinKeySize: %ld\n", info.ulMinKeySize );
- printf(" ulMaxKeySize: %ld\n", info.ulMaxKeySize );
- printf(" flags: %p\n", (void *)info.flags );
- }
- free( mech_list );
-
- testcase_pass();
-
-testcase_cleanup:
- testcase_user_logout();
- rc = funcs->C_CloseSession(session);
- if (rc != CKR_OK) {
- testcase_error("C_CloseSessions rc=%s", p11_get_ckr(rc));
- }
-
- return rc;
-
-}
-
-CK_RV api_driver(void)
-{
-
- CK_RV rc;
-
- rc = do_GetInfo();
- if (rc && !no_stop)
- return rc;
-
- rc = do_GetSlotList();
- if(rc && !no_stop)
- return rc;
-
- rc = do_GetSlotInfo();
- if(rc && !no_stop)
- return rc;
-
- rc = do_GetTokenInfo();
- if(rc && !no_stop)
- return rc;
-
- rc = do_GetMechanismList();
- if(rc && !no_stop)
- return rc;
-
- rc = do_GetMechanismInfo();
- if(rc && !no_stop)
- return rc;
-
- return rc;
-}
-
-int main(int argc, char **argv)
-{
- int rc;
- CK_C_INITIALIZE_ARGS cinit_args;
- CK_RV rv = 0;
-
- rc = do_ParseArgs(argc, argv);
- if (rc != 1)
- return rc;
-
- printf("Using slot #%lu...\n\n", SLOT_ID);
- printf("With option: nostop: %d\n", no_stop);
-
- rc = do_GetFunctionList();
- if (!rc) {
- testcase_error("do_getFunctionList(), rc=%s", p11_get_ckr(rc));
- return rc;
- }
-
- memset(&cinit_args, 0x0, sizeof(cinit_args));
- cinit_args.flags = CKF_OS_LOCKING_OK;
-
- funcs->C_Initialize(&cinit_args);
-
- {
- CK_SESSION_HANDLE hsess = 0;
-
- rc = funcs->C_GetFunctionStatus(hsess);
- if (rc != CKR_FUNCTION_NOT_PARALLEL)
- return rc;
-
- rc = funcs->C_CancelFunction(hsess);
- if (rc != CKR_FUNCTION_NOT_PARALLEL)
- return rc;
- }
-
- rv = api_driver();
- testcase_print_result();
-
- funcs->C_Finalize(NULL_PTR);
-
- /* make sure we return non-zero if rv is non-zero */
- return ((rv == 0) || (rv % 256) ? rv : -1);
-}
diff --git a/testcases/pkcs11/slot_token.c b/testcases/pkcs11/slot_token.c
new file mode 100644
index 0000000..9c884fb
--- /dev/null
+++ b/testcases/pkcs11/slot_token.c
@@ -0,0 +1,751 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <memory.h>
+
+#include "pkcs11types.h"
+#include "regress.h"
+#include "common.c"
+
+CK_RV do_GetInfo(void)
+{
+
+ CK_FLAGS flags;
+ CK_SESSION_HANDLE session;
+ CK_RV rc = 0;
+ CK_BYTE user_pin[PKCS11_MAX_PIN_LEN];
+ CK_ULONG user_pin_len;
+ CK_INFO info;
+
+ // Do some setup and login to the token
+ testcase_begin("C_GetInfo function check");
+ testcase_rw_session();
+ testcase_user_login();
+
+ testcase_new_assertion();
+
+ rc = funcs->C_GetInfo(&info);
+
+ if(rc != CKR_OK){
+ testcase_fail("C_GetInfo() rc=%s", p11_get_ckr(rc));
+ goto testcase_cleanup;
+ }
+
+ printf("Cryptoki Version: %d.%d\n", info.cryptokiVersion.major, info.cryptokiVersion.minor);
+ printf("Manufacturer ID: %s\n", info.manufacturerID);
+ printf("Library Description: %s\n", info.libraryDescription);
+ printf("Library Version: %d.%d\n", info.libraryVersion.major, info.libraryVersion.minor);
+
+
+ testcase_pass("Library info successfully sourced");
+
+testcase_cleanup:
+ testcase_user_logout();
+ rc = funcs->C_CloseSession(session);
+ if (rc != CKR_OK) {
+ testcase_error("C_CloseSessions rc=%s", p11_get_ckr(rc));
+ }
+
+ return rc;
+
+}
+
+CK_RV do_GetSlotList(void)
+{
+ CK_FLAGS flags;
+ CK_SESSION_HANDLE session;
+ CK_RV rc = 0;
+ CK_BYTE user_pin[PKCS11_MAX_PIN_LEN];
+ CK_ULONG user_pin_len;
+
+ CK_BBOOL tokenPresent;
+ CK_SLOT_ID_PTR pSlotList;
+ CK_ULONG ulCount;
+
+ tokenPresent = TRUE;
+
+ testcase_begin("C_GetSlotList function check");
+ testcase_rw_session();
+ testcase_user_login();
+
+ testcase_new_assertion();
+
+ rc = funcs->C_GetSlotList( tokenPresent, NULL, &ulCount );
+ if (rc != CKR_OK) {
+ testcase_fail("C_GetSlotList rc=%s", p11_get_ckr(rc));
+ return rc;
+ }
+
+ pSlotList = (CK_SLOT_ID *)malloc( ulCount * sizeof(CK_SLOT_ID) );
+ if (!pSlotList) {
+ testcase_fail("DRIVER ERROR: CANNOT ALLOCATE MEMORY FOR SLOT LIST\n");
+ return -1;
+ }
+
+ rc = funcs->C_GetSlotList( tokenPresent, pSlotList, &ulCount );
+ if (rc != CKR_OK) {
+ testcase_fail("C_GetSlotList rc=%s", p11_get_ckr(rc));
+ return rc;
+ }
+
+ testcase_pass("Slot list returned successfully");
+
+ free(pSlotList);
+
+testcase_cleanup:
+ testcase_user_logout();
+ rc = funcs->C_CloseSession(session);
+ if (rc != CKR_OK) {
+ testcase_error("C_CloseSessions rc=%s", p11_get_ckr(rc));
+ }
+
+ return rc;
+
+}
+
+CK_RV do_GetSlotInfo(void)
+{
+ CK_FLAGS flags;
+ CK_SESSION_HANDLE session;
+ CK_RV rc = 0;
+ CK_BYTE user_pin[PKCS11_MAX_PIN_LEN];
+ CK_ULONG user_pin_len;
+
+ CK_SLOT_ID slot_id = SLOT_ID;
+ CK_SLOT_INFO info;
+
+ testcase_begin("Display C_GetSlotInfo return data");
+ testcase_rw_session();
+ testcase_user_login();
+
+
+ /* Test expected values */
+ testcase_new_assertion();
+
+ rc = funcs->C_GetSlotInfo(slot_id, &info);
+ if(rc != CKR_OK){
+ testcase_fail("C_GetSlotInfo() rc = %s", p11_get_ckr(rc));
+ goto testcase_cleanup;
+ }
+
+ printf(" CK_SLOT_INFO for slot %d:\n", (int)slot_id);
+ printf(" slotDescription: %s\n", info.slotDescription );
+ printf(" manufacturerID: %s\n", info.manufacturerID );
+ printf(" flags: %p\n", (void *)info.flags );
+ printf(" hardwareVersion: %d.%d\n", info.hardwareVersion.major, info.hardwareVersion.minor );
+ printf(" firmwareVersion: %d.%d\n", info.firmwareVersion.major, info.firmwareVersion.minor );
+
+ testcase_pass("Slot info of in-use slot received successfully");
+
+
+ /* Test to see if definite wrong fails */
+ testcase_new_assertion();
+
+ rc = funcs->C_GetSlotInfo(999, &info);
+
+ if(rc != CKR_SLOT_ID_INVALID){
+ testcase_fail("C_GetSlotInfo() rc = %s", p11_get_ckr(rc));
+ goto testcase_cleanup;
+ }
+
+ testcase_pass("Correctly returned failed request on unavailable slot");
+
+testcase_cleanup:
+ testcase_user_logout();
+ rc = funcs->C_CloseSession(session);
+ if (rc != CKR_OK) {
+ testcase_error("C_CloseSessions rc=%s", p11_get_ckr(rc));
+ }
+
+ return rc;
+
+}
+
+CK_RV do_GetTokenInfo(void)
+{
+
+ CK_FLAGS flags;
+ CK_SESSION_HANDLE session;
+ CK_RV rc = 0;
+ CK_BYTE user_pin[PKCS11_MAX_PIN_LEN];
+ CK_ULONG user_pin_len;
+
+ CK_SLOT_ID slot_id = SLOT_ID;
+ CK_TOKEN_INFO info;
+
+ testcase_begin("Display C_GetTokenInfo() return data");
+ testcase_rw_session();
+ testcase_user_login();
+
+ testcase_new_assertion();
+
+ rc = funcs->C_GetTokenInfo( slot_id, &info );
+ if (rc != CKR_OK) {
+ testcase_fail("C_GetTokenInfo rc=%s", p11_get_ckr(rc));
+ return rc;
+ }
+
+ printf(" CK_TOKEN_INFO for slot #%d:\n", (int)slot_id);
+ printf(" label: %s\n", info.label );
+ printf(" manufacturerID: %s\n", info.manufacturerID );
+ printf(" model: %s\n", info.model );
+ printf(" serialNumber: %s\n", info.serialNumber );
+ printf(" flags: %p\n", (void *)info.flags );
+ printf(" ulMaxSessionCount: %ld\n", info.ulMaxSessionCount );
+ printf(" ulSessionCount: %ld\n", info.ulSessionCount );
+ printf(" ulMaxRwSessionCount: %ld\n", info.ulMaxRwSessionCount );
+ printf(" ulRwSessionCount: %ld\n", info.ulRwSessionCount );
+ printf(" ulMaxPinLen: %ld\n", info.ulMaxPinLen );
+ printf(" ulMinPinLen: %ld\n", info.ulMinPinLen );
+ printf(" ulTotalPublicMemory: %ld\n", info.ulTotalPublicMemory );
+ printf(" ulFreePublicMemory: %ld\n", info.ulFreePublicMemory );
+ printf(" ulTotalPrivateMemory: %ld\n", info.ulTotalPrivateMemory );
+ printf(" ulFreePrivateMemory: %ld\n", info.ulFreePrivateMemory );
+ printf(" hardwareVersion: %d.%d\n", info.hardwareVersion.major, info.hardwareVersion.minor );
+ printf(" firmwareVersion: %d.%d\n", info.firmwareVersion.major, info.firmwareVersion.minor );
+ printf(" time: %s\n", info.utcTime );
+
+ testcase_pass("Returned information on chosen slot token");
+
+ /* Test to see if definite wrong fails */
+ testcase_new_assertion();
+
+ rc = funcs->C_GetTokenInfo(999, &info);
+
+ if(rc != CKR_SLOT_ID_INVALID){
+ testcase_fail("C_GetTokenInfo() rc = %s", p11_get_ckr(rc));
+ goto testcase_cleanup;
+ }
+
+ testcase_pass("Correctly failed to gather information on inaccessible token");
+
+testcase_cleanup:
+ testcase_user_logout();
+ rc = funcs->C_CloseSession(session);
+ if (rc != CKR_OK) {
+ testcase_error("C_CloseSessions rc=%s", p11_get_ckr(rc));
+ }
+
+ return rc;
+
+}
+
+CK_RV do_GetMechanismList(void)
+{
+ CK_FLAGS flags;
+ CK_SESSION_HANDLE session;
+ CK_RV rc = 0;
+ CK_BYTE user_pin[PKCS11_MAX_PIN_LEN];
+ CK_ULONG user_pin_len;
+
+ CK_SLOT_ID slot_id = SLOT_ID;
+ CK_ULONG count;
+ CK_MECHANISM_TYPE *mech_list;
+
+ testcase_begin("Function check and could display of C_GetMechanismList");
+ testcase_rw_session();
+ testcase_user_login();
+
+ testcase_new_assertion();
+
+ rc = funcs->C_GetMechanismList(slot_id, NULL, &count);
+ if (rc != CKR_OK) {
+ testcase_fail("C_GetMechanismList 1 rc=%s",p11_get_ckr(rc));
+ return rc;
+ }
+
+ printf("C_GetMechanismList #1 returned %ld mechanisms\n", count );
+
+ mech_list = (CK_MECHANISM_TYPE *)malloc( count * sizeof(CK_MECHANISM_TYPE) );
+ if (!mech_list){
+ testcase_fail();
+ return CKR_HOST_MEMORY;
+ }
+ rc = funcs->C_GetMechanismList(slot_id, mech_list, &count);
+ if (rc != CKR_OK) {
+ testcase_fail("C_GetMechanismList 2 rc=%s", p11_get_ckr(rc));
+ return rc;
+ }
+
+ free( mech_list );
+
+ testcase_pass("Mechanism listing from current slot");
+
+ /* Test to see if definite wrong slot fails */
+ testcase_new_assertion();
+
+ rc = funcs->C_GetMechanismList(999, NULL, &count);
+
+ if(rc != CKR_SLOT_ID_INVALID){
+ testcase_fail("C_GetMechanismList() rc = %s", p11_get_ckr(rc));
+ goto testcase_cleanup;
+ }
+
+ testcase_pass("Mechanism listing from known incorrect slot");
+
+testcase_cleanup:
+ testcase_user_logout();
+ rc = funcs->C_CloseSession(session);
+ if (rc != CKR_OK) {
+ testcase_error("C_CloseSessions rc=%s", p11_get_ckr(rc));
+ }
+
+ return rc;
+
+}
+
+CK_RV do_GetMechanismInfo(void)
+{
+ CK_FLAGS flags;
+ CK_SESSION_HANDLE session;
+ CK_RV rc = 0;
+ CK_BYTE user_pin[PKCS11_MAX_PIN_LEN];
+ CK_ULONG user_pin_len;
+
+ CK_SLOT_ID slot_id = SLOT_ID;
+ CK_MECHANISM_INFO info;
+ CK_ULONG i, count;
+ CK_MECHANISM_TYPE *mech_list;
+
+ testcase_begin("Gather list of mechanisms via C_GetMechanismList");
+ testcase_rw_session();
+ testcase_user_login();
+
+ testcase_new_assertion();
+
+ rc = funcs->C_GetMechanismList( slot_id, NULL, &count );
+ if (rc != CKR_OK) {
+ testcase_fail("C_GetMechanismList #1 rc=%s", p11_get_ckr(rc));
+ return rc;
+ }
+
+ testcase_pass();
+ testcase_begin("Display information of each mechanism");
+ testcase_new_assertion();
+
+ mech_list = (CK_MECHANISM_TYPE *)malloc( count * sizeof(CK_MECHANISM_TYPE) );
+ if (!mech_list){
+ testcase_fail();
+ return CKR_HOST_MEMORY;
+ }
+ rc = funcs->C_GetMechanismList( slot_id, mech_list, &count );
+ if (rc != CKR_OK) {
+ testcase_fail("C_GetMechanismList #2 rc=%s", p11_get_ckr(rc));
+ return rc;
+ }
+
+ for (i=0; i < count; i++) {
+ rc = funcs->C_GetMechanismInfo( slot_id, mech_list[i], &info );
+ if (rc != CKR_OK) {
+ testcase_fail("C_GetMechanismInfo rc=%s", p11_get_ckr(rc));
+ testcase_error("Failed to get info on mechanism # %ld\n", mech_list[i]);
+ return rc;
+ }
+
+ printf(" Mechanism #%ld %s\n", mech_list[i], p11_get_ckm(mech_list[i]));
+ printf(" ulMinKeySize: %ld\n", info.ulMinKeySize );
+ printf(" ulMaxKeySize: %ld\n", info.ulMaxKeySize );
+ printf(" flags: %p\n", (void *)info.flags );
+ }
+ free( mech_list );
+
+ testcase_pass();
+
+testcase_cleanup:
+ testcase_user_logout();
+ rc = funcs->C_CloseSession(session);
+ if (rc != CKR_OK) {
+ testcase_error("C_CloseSessions rc=%s", p11_get_ckr(rc));
+ }
+
+ return rc;
+
+}
+
+CK_RV do_InitToken( void )
+{
+ CK_BYTE label[32];
+ int len;
+ CK_CHAR so_pin[PKCS11_MAX_PIN_LEN];
+
+ CK_RV rc = 0;
+
+ testcase_begin("Invalids tests for C_InitToken");
+
+ memcpy( label, "L13 ", 32 );
+
+ //label in this case
+ for (len = 0; len <31;len++){
+ if (label[len] == '\0'){
+ label[len] = ' ';
+ break;
+ }
+ }
+
+ testcase_new_assertion();
+
+ rc = funcs->C_InitToken( SLOT_ID, NULL, strlen((char *)so_pin), label );
+ if (rc != CKR_ARGUMENTS_BAD) {
+ testcase_fail("C_InitToken rc=%s", p11_get_ckr(rc));
+ goto testcase_cleanup;
+ }
+
+ testcase_pass("Testing using null SO pin errored as expected");
+ testcase_new_assertion();
+
+ rc = funcs->C_InitToken( 99, so_pin, strlen((char *)so_pin), label );
+ if (rc != CKR_SLOT_ID_INVALID) {
+ testcase_fail("C_InitToken rc=%s", p11_get_ckr(rc));
+ goto testcase_cleanup;
+ }
+
+ testcase_pass("Testing using impossible slot errored as expected");
+
+testcase_cleanup:
+ return 0;
+}
+
+CK_RV do_InitPIN( void )
+{
+ CK_SLOT_ID slot_id;
+ CK_FLAGS flags;
+ CK_SESSION_HANDLE session;
+ CK_CHAR so_pin[PKCS11_MAX_PIN_LEN];
+ CK_CHAR user_pin[PKCS11_MAX_PIN_LEN];
+ CK_ULONG so_pin_len;
+ CK_ULONG user_pin_len;
+ CK_RV rc;
+
+ testcase_begin("Testing multiple functions of C_InitPIN");
+
+ if (get_user_pin(user_pin))
+ return CKR_FUNCTION_FAILED;
+ user_pin_len = (CK_ULONG)strlen((char *)user_pin);
+
+ if (get_so_pin(so_pin))
+ return CKR_FUNCTION_FAILED;
+ so_pin_len = (CK_ULONG)strlen((char *)so_pin);
+
+ slot_id = SLOT_ID;
+ flags = CKF_SERIAL_SESSION | CKF_RW_SESSION;
+
+ // try to call C_InitPIN from a public session
+ testcase_new_assertion();
+ rc = funcs->C_OpenSession( slot_id, flags, NULL, NULL, &session );
+ if (rc != CKR_OK) {
+ testcase_fail("C_OpenSession rc=%s", p11_get_ckr(rc));
+ return rc;
+ }
+
+ rc = funcs->C_InitPIN( session, user_pin, user_pin_len );
+ if (rc != CKR_USER_NOT_LOGGED_IN) {
+ testcase_fail("C_InitPIN #1 rc=%s", p11_get_ckr(rc));
+ testcase_error("Expected CKR_USER_NOT_LOGGED_IN\n" );
+ return rc;
+ }
+ testcase_pass("Public session passed");
+
+ // try to call C_InitPIN from an SO session
+ testcase_new_assertion();
+ rc = funcs->C_Login( session, CKU_SO, so_pin, so_pin_len );
+ if (rc != CKR_OK) {
+ testcase_fail("C_Login #2 rc=%s", p11_get_ckr(rc));
+ return rc;
+ }
+
+ rc = funcs->C_InitPIN( session, user_pin, user_pin_len );
+ if (rc != CKR_OK) {
+ testcase_fail("C_InitPIN #2 rc=%s", p11_get_ckr(rc));
+ return rc;
+ }
+
+ rc = funcs->C_Logout( session );
+ if (rc != CKR_OK) {
+ testcase_fail("C_Logout #1 rc=%s", p11_get_ckr(rc));
+ return rc;
+ }
+ testcase_pass("SO session passed");
+
+ // try to call C_InitPIN from a normal user session
+ testcase_new_assertion();
+ rc = funcs->C_Login( session, CKU_USER, user_pin, user_pin_len );
+ if (rc != CKR_OK) {
+ testcase_fail("C_Login #1 rc=%s", p11_get_ckr(rc));
+ return rc;
+ }
+
+ rc = funcs->C_InitPIN( session, user_pin, user_pin_len );
+ if (rc != CKR_USER_NOT_LOGGED_IN) {
+ testcase_fail("C_InitPIN #2 rc=%s", p11_get_ckr(rc));
+ testcase_error("Expected CKR_USER_NOT_LOGGED_IN\n" );
+ return rc;
+ }
+
+ rc = funcs->C_Logout( session );
+ if (rc != CKR_OK) {
+ testcase_fail("C_Logout #2 rc=%s", p11_get_ckr(rc));
+ return rc;
+ }
+
+ rc = funcs->C_CloseAllSessions( slot_id );
+ if (rc != CKR_OK) {
+ testcase_fail("C_CloseAllSessions #1 rc=%s", p11_get_ckr(rc));
+ return rc;
+ }
+
+ testcase_pass("User session passed");
+
+ return rc;
+}
+
+CK_RV do_SetPIN( void )
+{
+ CK_SLOT_ID slot_id;
+ CK_FLAGS flags;
+ CK_SESSION_HANDLE session;
+ CK_CHAR old_pin[PKCS11_MAX_PIN_LEN];
+ CK_CHAR new_pin[PKCS11_MAX_PIN_LEN];
+ CK_ULONG old_len;
+ CK_ULONG new_len;
+ CK_RV rc;
+
+ testcase_begin("Testing multiple functions of C_InitPIN");
+
+ // first, try to set the user PIN
+ if (get_user_pin(old_pin))
+ return CKR_FUNCTION_FAILED;
+ old_len = (CK_ULONG)strlen((char *)old_pin);
+
+ memcpy( new_pin, "ABCDEF", 6 );
+ new_len = 6;
+
+ slot_id = SLOT_ID;
+ flags = CKF_SERIAL_SESSION | CKF_RW_SESSION;
+
+
+ // try to call C_SetPIN from a public session
+ testcase_new_assertion();
+ rc = funcs->C_OpenSession( slot_id, flags, NULL, NULL, &session );
+ if (rc != CKR_OK) {
+ testcase_fail("C_OpenSession #1 rc=%s", p11_get_ckr(rc));
+ return rc;
+ }
+
+ rc = funcs->C_SetPIN( session, old_pin, old_len, new_pin, new_len );
+ if (rc != CKR_OK) {
+ testcase_fail("C_SetPIN #1 rc=%s", p11_get_ckr(rc));
+ return rc;
+ }
+
+ rc = funcs->C_SetPIN( session, new_pin, new_len, old_pin, old_len );
+ if (rc != CKR_OK) {
+ testcase_fail("C_SetPIN #3 rc=%s", p11_get_ckr(rc));
+ return rc;
+ }
+
+ testcase_pass("public test passed");
+
+ // try to call C_SetPIN from a normal user session
+ testcase_new_assertion();
+ rc = funcs->C_Login( session, CKU_USER, old_pin, old_len );
+ if (rc != CKR_OK) {
+ testcase_fail("C_Login #1 %s", p11_get_ckr(rc));
+ return rc;
+ }
+
+ rc = funcs->C_SetPIN( session, old_pin, old_len, new_pin, new_len );
+ if (rc != CKR_OK) {
+ testcase_fail("C_SetPIN #2 rc=%s", p11_get_ckr(rc));
+ return rc;
+ }
+
+ rc = funcs->C_Logout( session );
+ if (rc != CKR_OK) {
+ testcase_fail("C_Logout #1 rc=%s", p11_get_ckr(rc));
+ return rc;
+ }
+ testcase_pass("user PIN set successfully");
+
+ // now, try to log in with the old PIN
+ testcase_new_assertion();
+ rc = funcs->C_Login( session, CKU_USER, old_pin, old_len );
+ if (rc != CKR_PIN_INCORRECT) {
+ testcase_fail("C_Login #2 rc=%s", p11_get_ckr(rc));
+ testcase_error("Expected CKR_PIN_INCORRECT\n");
+ return rc;
+ }
+
+ rc = funcs->C_Login( session, CKU_USER, new_pin, new_len );
+ if (rc != CKR_OK) {
+ testcase_fail("C_Login #3 rc=%s", p11_get_ckr(rc));
+ return rc;
+ }
+ testcase_pass("login with old PIN failed as expected");
+
+ // change the PIN back to the original so the rest of this program
+ // doesn't break
+ testcase_new_assertion();
+ rc = funcs->C_SetPIN( session, new_pin, new_len, old_pin, old_len );
+ if (rc != CKR_OK) {
+ testcase_fail("C_SetPIN #3 rc=%s", p11_get_ckr(rc));
+ return rc;
+ }
+
+ rc = funcs->C_Logout( session );
+ if (rc != CKR_OK) {
+ testcase_fail("C_Logout #2 rc=%s", p11_get_ckr(rc));
+ return rc;
+ }
+ testcase_pass("Pin reset to original");
+
+ // done with user tests...now try with the SO
+ if (get_so_pin(old_pin))
+ return CKR_FUNCTION_FAILED;
+
+
+ // try to call C_SetPIN from a normal user session
+ testcase_new_assertion();
+ rc = funcs->C_Login( session, CKU_SO, old_pin, old_len );
+ if (rc != CKR_OK) {
+ testcase_fail("C_Login #3 rc=%s", p11_get_ckr(rc));
+ return rc;
+ }
+
+ rc = funcs->C_SetPIN( session, old_pin, old_len, new_pin, new_len );
+ if (rc != CKR_OK) {
+ testcase_fail("C_SetPIN #4 rc=%s", p11_get_ckr(rc));
+ return rc;
+ }
+
+ rc = funcs->C_Logout( session );
+ if (rc != CKR_OK) {
+ testcase_fail("C_Logout #3 rc=%s", p11_get_ckr(rc));
+ return rc;
+ }
+ testcase_pass("user session passed");
+
+ // now, try to log in with the old PIN
+ testcase_new_assertion();
+ rc = funcs->C_Login( session, CKU_SO, old_pin, old_len );
+ if (rc != CKR_PIN_INCORRECT) {
+ testcase_fail("C_Login #4 rc=%s", p11_get_ckr(rc));
+ testcase_error("Expected CKR_PIN_INCORRECT\n");
+ return rc;
+ }
+
+ rc = funcs->C_Login( session, CKU_SO, new_pin, new_len );
+ if (rc != CKR_OK) {
+ testcase_fail("C_Login #5 rc=%s", p11_get_ckr(rc));
+ return rc;
+ }
+ testcase_pass("Old pin login after changed");
+
+ // change the PIN back to the original so the rest of this program
+ // doesn't break
+ testcase_new_assertion();
+ rc = funcs->C_SetPIN( session, new_pin, new_len, old_pin, old_len );
+ if (rc != CKR_OK) {
+ testcase_fail("C_SetPIN #5 rc=%s", p11_get_ckr(rc));
+ return rc;
+ }
+
+ rc = funcs->C_Logout( session );
+ if (rc != CKR_OK) {
+ testcase_fail("C_Logout #4 rc=%s", p11_get_ckr(rc));
+ return rc;
+ }
+
+ testcase_pass("Original PIN reset");
+
+ return rc;
+}
+
+
+CK_RV api_driver(void)
+{
+
+ CK_RV rc;
+
+ rc = do_GetInfo();
+ if (rc && !no_stop)
+ return rc;
+
+ rc = do_GetSlotList();
+ if(rc && !no_stop)
+ return rc;
+
+ rc = do_GetSlotInfo();
+ if(rc && !no_stop)
+ return rc;
+
+ rc = do_GetTokenInfo();
+ if(rc && !no_stop)
+ return rc;
+
+ rc = do_GetMechanismList();
+ if(rc && !no_stop)
+ return rc;
+
+ rc = do_GetMechanismInfo();
+ if(rc && !no_stop)
+ return rc;
+
+ rc = do_InitToken();
+ if(rc && !no_stop)
+ return rc;
+
+ rc = do_InitPIN();
+ if(rc && !no_stop)
+ return rc;
+
+ rc = do_SetPIN();
+ if(rc && !no_stop)
+ return rc;
+
+ return rc;
+}
+
+int main(int argc, char **argv)
+{
+ int rc;
+ CK_C_INITIALIZE_ARGS cinit_args;
+ CK_RV rv = 0;
+
+ rc = do_ParseArgs(argc, argv);
+ if (rc != 1)
+ return rc;
+
+ printf("Using slot #%lu...\n\n", SLOT_ID);
+ printf("With option: nostop: %d\n", no_stop);
+
+ rc = do_GetFunctionList();
+ if (!rc) {
+ testcase_error("do_getFunctionList(), rc=%s", p11_get_ckr(rc));
+ return rc;
+ }
+
+ memset(&cinit_args, 0x0, sizeof(cinit_args));
+ cinit_args.flags = CKF_OS_LOCKING_OK;
+
+ funcs->C_Initialize(&cinit_args);
+
+ {
+ CK_SESSION_HANDLE hsess = 0;
+
+ rc = funcs->C_GetFunctionStatus(hsess);
+ if (rc != CKR_FUNCTION_NOT_PARALLEL)
+ return rc;
+
+ rc = funcs->C_CancelFunction(hsess);
+ if (rc != CKR_FUNCTION_NOT_PARALLEL)
+ return rc;
+ }
+
+ rv = api_driver();
+ testcase_print_result();
+
+ funcs->C_Finalize(NULL_PTR);
+
+ /* make sure we return non-zero if rv is non-zero */
+ return ((rv == 0) || (rv % 256) ? rv : -1);
+}
------------------------------------------------------------------------------
Monitor Your Dynamic Infrastructure at Any Scale With Datadog!
Get real-time metrics from all of your servers, apps and tools
in one place.
SourceForge users - Click here to start your Free Trial of Datadog now!
http://pubads.g.doubleclick.net/gampad/clk?id=241902991&iu=/4140
_______________________________________________
Opencryptoki-tech mailing list
Opencryptoki-tech@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/opencryptoki-tech