Attached are two patches. One adds C_Finalize in the closing section of testcases to properly exit the Cryptoki session. Another is a rewrite of /testcases/pkcs11/misc_func.c to use the modern testcases style and remove currently non-functional tests. Later patches will add to the functionality of this file.

Ian Davis
IBM LTC Security
diff --git a/testcases/pkcs11/api_func.c b/testcases/pkcs11/api_func.c
new file mode 100644
index 0000000..c14134d
--- /dev/null
+++ b/testcases/pkcs11/api_func.c
@@ -0,0 +1,401 @@
+#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/crypto/aes_func.c b/testcases/crypto/aes_func.c
index dcfd1a0..664a2ed 100644
--- a/testcases/crypto/aes_func.c
+++ b/testcases/crypto/aes_func.c
@@ -1853,6 +1853,8 @@ int main  (int argc, char **argv) {
 	rv = aes_funcs();
 	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/crypto/des3_func.c b/testcases/crypto/des3_func.c
index 26aaceb..4d97769 100644
--- a/testcases/crypto/des3_func.c
+++ b/testcases/crypto/des3_func.c
@@ -1234,6 +1234,9 @@ int main  (int argc, char **argv){
 	testcase_setup(0); //TODO
 	rc = des3_funcs();
 	testcase_print_result();
+	
+	funcs->C_Finalize(NULL_PTR);
+	
 	return rc;
 }
 
diff --git a/testcases/crypto/des_func.c b/testcases/crypto/des_func.c
index b887d1e..dc4559a 100644
--- a/testcases/crypto/des_func.c
+++ b/testcases/crypto/des_func.c
@@ -1223,5 +1223,8 @@ int main  (int argc, char **argv){
 	testcase_setup(0); //TODO
 	rc = des_funcs();
 	testcase_print_result();
+	
+	funcs->C_Finalize(NULL_PTR);
+
 	return rc;
 }
diff --git a/testcases/crypto/dh_func.c b/testcases/crypto/dh_func.c
index 37f473d..75715f9 100644
--- a/testcases/crypto/dh_func.c
+++ b/testcases/crypto/dh_func.c
@@ -729,6 +729,9 @@ int main(int argc, char **argv)
 	}
 
 	rv = dh_functions();
+	
+	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/crypto/digest_func.c b/testcases/crypto/digest_func.c
index 2134958..d4f5e8e 100644
--- a/testcases/crypto/digest_func.c
+++ b/testcases/crypto/digest_func.c
@@ -698,6 +698,9 @@ int main(int argc, char **argv)
 	testcase_setup(0); //TODO
 	rv = digest_funcs();
 	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/crypto/dsa_func.c b/testcases/crypto/dsa_func.c
index 5a6c6ca..7307286 100644
--- a/testcases/crypto/dsa_func.c
+++ b/testcases/crypto/dsa_func.c
@@ -331,6 +331,9 @@ int main(int argc, char **argv)
 	}
 
 	rv = dsa_functions();
+
+	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/crypto/ec_func.c b/testcases/crypto/ec_func.c
index 381578a..9c12402 100644
--- a/testcases/crypto/ec_func.c
+++ b/testcases/crypto/ec_func.c
@@ -416,6 +416,8 @@ main(int argc, char **argv)
 
 	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/crypto/rsa_func.c b/testcases/crypto/rsa_func.c
index 637d3e5..64296cb 100644
--- a/testcases/crypto/rsa_func.c
+++ b/testcases/crypto/rsa_func.c
@@ -1547,5 +1547,8 @@ int main  (int argc, char **argv){
 	testcase_setup(0);
 	rv = rsa_funcs();
 	testcase_print_result();
+
+	funcs->C_Finalize(NULL_PTR);
+
 	return rv;
 }
diff --git a/testcases/crypto/rsaupdate_func.c b/testcases/crypto/rsaupdate_func.c
index c5706ed..7bf6cbd 100644
--- a/testcases/crypto/rsaupdate_func.c
+++ b/testcases/crypto/rsaupdate_func.c
@@ -1081,5 +1081,8 @@ int main  (int argc, char **argv){
 	testcase_setup(0);
 	rv = rsa_funcs();
 	testcase_print_result();
+
+	funcs->C_Finalize(NULL_PTR);
+
 	return rv;
 }
diff --git a/testcases/crypto/ssl3_func.c b/testcases/crypto/ssl3_func.c
index abf063e..33c75fd 100644
--- a/testcases/crypto/ssl3_func.c
+++ b/testcases/crypto/ssl3_func.c
@@ -689,6 +689,8 @@ int main(int argc, char **argv)
 	rv = ssl3_functions();
 	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/Makefile.am b/testcases/pkcs11/Makefile.am
index 475f204..81f58ab 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
+noinst_PROGRAMS=hw_fn misc_tests sess_mgmt_tests sess_bench attribute findobjects destroyobjects copyobjects api_tests
 
 AM_CFLAGS=-I. -I../../usr/include/pkcs11 -I../include -I../common -I../../usr/lib/pkcs11/common -Wall
 
@@ -12,3 +12,4 @@ attribute_SOURCES = attribute.c
 findobjects_SOURCES = findobjects.c
 destroyobjects_SOURCES = destroyobjects.c
 copyobjects_SOURCES = copyobjects.c
+api_tests_SOURCES = api_func.c
diff --git a/testcases/pkcs11/attribute.c b/testcases/pkcs11/attribute.c
index 45b1b1b..18c2377 100644
--- a/testcases/pkcs11/attribute.c
+++ b/testcases/pkcs11/attribute.c
@@ -167,6 +167,8 @@ int main  (int argc, char **argv) {
 	rc = do_TestAttributes();
 	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/copyobjects.c b/testcases/pkcs11/copyobjects.c
index 9945cc1..919886d 100644
--- a/testcases/pkcs11/copyobjects.c
+++ b/testcases/pkcs11/copyobjects.c
@@ -237,6 +237,8 @@ int main(int argc, char **argv)
 	rc = do_CopyObjects();
 	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/destroyobjects.c b/testcases/pkcs11/destroyobjects.c
index 6c90191..084a505 100644
--- a/testcases/pkcs11/destroyobjects.c
+++ b/testcases/pkcs11/destroyobjects.c
@@ -239,6 +239,8 @@ main(int argc, char **argv)
 	rc = do_DestroyObjects();
 	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/findobjects.c b/testcases/pkcs11/findobjects.c
index 3b0f9c2..55b0b92 100644
--- a/testcases/pkcs11/findobjects.c
+++ b/testcases/pkcs11/findobjects.c
@@ -269,6 +269,8 @@ main(int argc, char **argv)
 	rc = do_FindObjects();
 	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);
 }
------------------------------------------------------------------------------
_______________________________________________
Opencryptoki-tech mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/opencryptoki-tech

Reply via email to