Esempio n. 1
0
void testlogbytes(PKCS11TEST_LOG_LEVEL loglevel,unsigned char *pValue, unsigned long ulValueLen)
{
    unsigned long counter = 0;
    //testlog(loglevel,":");
    while (counter < ulValueLen)
    {
        testlog(LVL_NOLEVEL,"0x%.2x ", *pValue);
        counter++;
        pValue++;
    }
    testlog(LVL_NOLEVEL,"\n");
}
Esempio n. 2
0
CK_RV test_finalize_initialize() {
	void *handle;
	CK_FUNCTION_LIST_PTR functions;
    DWORD   dwThreadId;
	HANDLE  hThreadHandle; 
	int retVal = 0;
	struct test_finalize_initialize_threadvars threadVars;
	
	testlog(LVL_INFO, "test_finalize_initialize enter\n");
	handle = dlopen(PKCS11_LIB, RTLD_LAZY); // RTLD_NOW is slower
	if (NULL == handle) {
	    testlog(LVL_ERROR, "dlopen error\n");
 		testlog(LVL_INFO, "test_finalize_initialize leave\n");
		return 1;
	}
	GetPKCS11FunctionList(&functions, handle);

	readslots(functions);

	threadVars.functions = functions;
	threadVars.threadRetVal = 0;

	// Create pkcs11 thread
    hThreadHandle = CreateThread( 
        NULL,                   // default security attributes
        0,                      // use default stack size  
        pkcs11Thread,			// thread function name
        &threadVars,				// argument to thread function 
        0,                      // use default creation flags 
        &dwThreadId);			// returns the thread identifier 

	if (hThreadHandle == NULL)
	{
		retVal = -1;
	}
	else
	{
		// Wait until pkcs11 thread is terminated.
		if (WAIT_OBJECT_0 != WaitForSingleObject(hThreadHandle, INFINITE))
		{
			testlog(LVL_ERROR, "WaitForSingleObject failed\n");
		}	
	}
	dlclose(handle);

	testlog(LVL_DEBUG, "second thread returned %d\n",threadVars.threadRetVal);
	testlog(LVL_INFO, "test_finalize_initialize leave\n");

	return retVal;
}
Esempio n. 3
0
//int main()
//{
int	_tmain(int argc, _TCHAR* argv[]){
	clock_t startTime = clock();
	clock_t duration = 0;
	int msecDuration = 0;

	int i = 0;
	int nrofTests = 0;
	errno_t error;

	eIDTest eIDTests[] = { \
//	{{0,TEST_SKIPPED},"Tests C_getinfo results in a single thread",&test_getinfo},
//#ifdef WIN32
//	{{0,TEST_SKIPPED},"Tests multiple finalize/initialize sessions in multiple threads with different args",&test_finalize_initialize},
//	{{0,TEST_SKIPPED},"Tests multiple finalize/initialize sessions in a single thread",&test_finalize_initialize_st},
//	{{0,TEST_SKIPPED},"Tests C_initialize when bad parameters are supplied 1",&test_initialize_ownmutex},
//	{{0,TEST_SKIPPED},"Tests C_initialize when bad parameters are supplied",&test_initialize_preserved},
//	{{0,TEST_SKIPPED},"Tests C_finalize when bad parameters are supplied",&test_finalize_preserved},
//#endif //WIN32
//	{{0,TEST_SKIPPED},"Tests C_getslotlist in single and two call usage",&test_getslotlist},
//	{{0,TEST_SKIPPED},"Tests C_getslotlist when insufficient memory is reserved",&test_getslotlist_multiple_slots},
//	{{0,TEST_SKIPPED},"Tests opening and closing of a session in a single thread",&test_open_close_session},
//	{{0,TEST_SKIPPED},"Tests getting slotID and slotdescription",&test_open_close_session_info},
//	{{0,TEST_SKIPPED},"Tests C_opensession with wrong parameters supplied",&test_open_close_session_bad_params},
//	{{0,TEST_SKIPPED},"Tests how many sessions can be opened",&test_open_close_session_limits},
//#ifdef WIN32
//{{0,TEST_SKIPPED},"tests getting all the objects from the card",&test_logslotevents_blocking},

//  #ifdef USER_INTERACTION
//	{{0,TEST_SKIPPED},"Tests waitforslot event (needs user interaction)",&test_waitforslotevent_userinteraction},//
//	{{0,TEST_SKIPPED},"tests signing with the card",&test_sign},
//		{{0,TEST_SKIPPED},"tests signing sequence with all mechanisms on the card",&test_sign_allmechs},
//	{{0,TEST_SKIPPED},"Tests waiting for slot event blocking",&test_waitforslotevent_blocking},
//  #endif
//	{{0,TEST_SKIPPED},"Tests waiting for slot event non-blocking",&test_waitforslotevent_nonblocking},
//	{{0,TEST_SKIPPED},"Tests waiting for slot event blocking, while another thread calls c_finalize",&test_waitforslotevent_whilefinalize},
//#endif //WIN32
//	{{0,TEST_SKIPPED},"Tests the retrieval of slot and token info",&test_get_token_info},
//	{{0,TEST_SKIPPED},"Shows info on the mechanisms supported by the card",&test_getmechanisms},
//	{{0,TEST_SKIPPED},"tests getting the keys from the card",&test_getkeys},
//	{{0,TEST_SKIPPED},"tests the return values of the getattributes function",&test_getkeys_retvals},
	//{{0,TEST_SKIPPED},"tests getting all the objects from the card",&test_getallobjects},
		{0,CKR_OK,"tests initialize/finalize",&init_finalize},
			{0,CKR_OK,"tests getinfo",&getinfo},
			//{0,CKR_OK,"tests initialize/finalize",&funclist},
			{0,CKR_OK,"tests slotlist",&slotlist},
			{0,CKR_OK,"tests slotinfo",&slotinfo},
			{0,CKR_OK,"tests tkinfo",&tkinfo},
			{0,CKR_OK,"tests double_init",&double_init},
			{0,CKR_OK,"tests slotevent",&slotevent},
			{0,CKR_OK,"tests mechlist",&mechlist},
	

		{0,CKR_OK,"tests mechinfo",&mechinfo},
			{0,CKR_OK,"tests sessions",&sessions},
			{0,CKR_OK,"tests sessions_nocard",&sessions_nocard},
			{0,CKR_OK,"tests sessioninfo",&sessioninfo},
			{0,CKR_OK,"tests login",&login},
			{0,CKR_OK,"tests nonsensible",&nonsensible},
			{0,CKR_OK,"tests objects",&objects},
			{0,CKR_OK,"tests readdata",&readdata},
			{0,CKR_OK,"tests digest",&digest},
			{0,CKR_OK,"tests sign_state",&sign_state},

			//{0,CKR_OK,"tests initialize/finalize",&decode_photo},


//		{{0,TEST_SKIPPED},"tests getting all the objects from the card",&test_getallidobjects},
	
//	{{0,TEST_SKIPPED},"tests getting object's attributes from the card",&test_getattributevalue_all},
//	{{0,TEST_SKIPPED},"tests getting object's attributes from the card",&test_getattributevalue_rrn},
	
//	{{0,TEST_SKIPPED},"tests getting the lastname object's attributes from the card",&test_getattributevalue_lastname},	
//#ifdef WIN32
//	{{0,TEST_SKIPPED},"tests adding and removing readers",&test_add_remove_readers},
//	{{0,TEST_SKIPPED},"tests waiting for card and reader events",&test_add_remove_readerevents},
//#endif //WIN32
//	{{0,TEST_SKIPPED},"tests getting the private object's from the card without logging in",&test_findPrivateKeyWithoutLoginShouldFail},	
//	{{0,TEST_SKIPPED},"tests the return value of C_FindObjectsFinal when called without C_FindObjectsInit",&test_findObjectsFinalNotInitialized},
	};

	if(argc >=2)
		eid_robot_style = argv[1];

	if(argc >=3)
		eid_dialogs_style = argv[2];

	error = initLog();
	if(error != 0)
	{
		printf("Can't open logfile");
		return -1;
	}
	nrofTests = (sizeof(eIDTests)/sizeof(eIDTest));

	while (i < nrofTests )
	{
		eIDTests[i].result = eIDTests[i].test_function_ptr();
		i++;
	}


	//testlog(LVL_NOLEVEL,"\n\n_______________________________________________\n");
	for (i = 0; i < nrofTests; i++)
	{
		if (eIDTests[i].testDescription != NULL)
		{
			testlog(LVL_NOLEVEL,"_______________________________________________\n");
			testlog(LVL_NOLEVEL,"Test %d %s \n", i, eIDTests[i].testDescription);
		}
		if(eIDTests[i].result != eIDTests[i].expectresult)
		{
			testlog(LVL_NOLEVEL,"FAILED : Result = 0x%.8x Expected Result\n", eIDTests[i].result, eIDTests[i].expectresult );
		}
		/*
		else
		{
			switch(eIDTests[i].result.basetestrv)
			{
			case TEST_PASSED:
				testlog(LVL_NOLEVEL,"PASSED\n");
				break;
			case TEST_SKIPPED:
				testlog(LVL_NOLEVEL,"SKIPPED\n");
				break;
			case TEST_WARNING:
				testlog(LVL_NOLEVEL,"WARNING\n");
				break;
			case TEST_ERROR:
			case TEST_FAILED:
				testlog(LVL_NOLEVEL,"FAILED : Result = 0x%.8x \n", eIDTests[i].result.pkcs11rv);
				break;
			default:
				testlog(LVL_NOLEVEL,"UNKNOWN : Result = 0x%.8x \n", eIDTests[i].result.pkcs11rv);
				break;
			};
		}*/
		//testlog(LVL_NOLEVEL,"\n_______________________________________________\n");
	}

	duration = clock() - startTime;

	msecDuration = (duration * 1000) / CLOCKS_PER_SEC;
	printf("Duration: %d,%d seconds", msecDuration/1000, msecDuration%1000);

	testlog(LVL_NOLEVEL,"\n===============================================\n");

	//short summary
	/*for (i = 0; i < nrofTests; i++)
	{
		if(eIDTests[i].result.pkcs11rv != CKR_OK)
		{
			testlog(LVL_NOLEVEL," F ");
		}
		else
		{
			switch(eIDTests[i].result.basetestrv)
			{
			case TEST_PASSED:
				testlog(LVL_NOLEVEL,"P");
				break;
			case TEST_SKIPPED:
				testlog(LVL_NOLEVEL," S ");
				break;
			case TEST_WARNING:
				testlog(LVL_NOLEVEL," W ");
				break;
			case TEST_ERROR:
			case TEST_FAILED:
				testlog(LVL_NOLEVEL," F ");
				break;
			default:
				testlog(LVL_NOLEVEL," X ");
				break;
			};
		}
	}*/
	testlog(LVL_NOLEVEL,"\n===============================================\n");

	endLog();
	testlog(LVL_NOLEVEL,"press a key to end the test\n");
	// Wait for user to end this test
#ifdef WIN32
	getchar();
#endif
}
Esempio n. 4
0
testRet test_getattributevalue(CK_ATTRIBUTE_PTR psearchtemplate, CK_ULONG templateLen) {
  void *handle;						//handle to the pkcs11 library
  CK_FUNCTION_LIST_PTR functions;		// list of the pkcs11 function pointers

  testRet retVal = {CKR_OK,TEST_PASSED};	//return values of this test
  CK_RV frv = CKR_OK;						//return value of last pkcs11 function called

  CK_SESSION_HANDLE session_handle;
  long slot_count;
  CK_SLOT_ID_PTR slotIds;
  int slotIdx;
  CK_ULONG ulObjectCount=1;

  testlog(LVL_INFO, "test_getattributevalue enter\n");
  if (InitializeTest(&handle,&functions))
  {
	frv = (*functions->C_Initialize) (NULL);
	if (ReturnedSuccesfull(frv,&(retVal.pkcs11rv), "C_Initialize", "test_getattributevalue" ))
	{		
	  frv = (*functions->C_GetSlotList) (0, 0, &slot_count);
	  if (ReturnedSuccesfull(frv,&(retVal.pkcs11rv), "C_GetSlotList", "test_getattributevalue" ))
	  {
		testlog(LVL_INFO,"slot count: %i\n", slot_count);
		slotIds = malloc(slot_count * sizeof(CK_SLOT_INFO));
		if(slotIds != NULL)
		{
		  frv = (*functions->C_GetSlotList) (CK_FALSE, slotIds, &slot_count);
		  if (ReturnedSuccesfull(frv,&(retVal.pkcs11rv), "C_GetSlotList (X2)", "test_getattributevalue" ))
		  {
			if(slot_count == 0)
			{
			  retVal.basetestrv = TEST_SKIPPED;
			}
			for (slotIdx = 0; slotIdx < slot_count; slotIdx++) 
			{
			  CK_OBJECT_HANDLE hObject;

			  frv = (*functions->C_OpenSession)(slotIds[slotIdx], CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &session_handle);
			  if (ReturnedSuccesfull(frv,&(retVal.pkcs11rv), "C_OpenSession", "test_getattributevalue" ))
			  {
				//CK_ATTRIBUTE attributes[1] = {CKA_CLASS,&data,sizeof(CK_ULONG)};
				CK_ATTRIBUTE_PTR attributes = psearchtemplate;
				frv = (*functions->C_FindObjectsInit)(session_handle, attributes, templateLen); 
				if (ReturnedSuccesfull(frv,&(retVal.pkcs11rv), "C_FindObjectsInit", "test_getattributevalue" ))
				{
				  frv = (*functions->C_FindObjects)(session_handle, &hObject,1,&ulObjectCount); 
				  ReturnedSuccesfull(frv,&(retVal.pkcs11rv), "C_FindObjects", "test_getattributevalue" );

				  while (ulObjectCount > 0)
				  {
					CK_ULONG retValueLen = 0;
					CK_ATTRIBUTE attr_templ[] = {{CKA_LABEL,NULL_PTR,0},
					{CKA_VALUE,NULL_PTR,0},
					{CKA_VALUE_LEN,&retValueLen,sizeof(CK_ULONG)}};
					testlog(LVL_INFO, "found object, CK_OBJECT_HANDLE = %d\n",hObject);

					frv = (*functions->C_GetAttributeValue)(session_handle,hObject,attr_templ,3);
					if (ReturnedSuccesfull(frv,&(retVal.pkcs11rv), "C_GetAttributeValue", "test_getattributevalue" ))
					{
					  CK_VOID_PTR pLabel = malloc (attr_templ[0].ulValueLen);
					  if(pLabel != NULL)
					  {
						CK_VOID_PTR pValue = malloc (attr_templ[1].ulValueLen);
						attr_templ[0].pValue = pLabel;						
						if (pValue != NULL )
						{
						  attr_templ[1].pValue = pValue;
						  frv = (*functions->C_GetAttributeValue)(session_handle,hObject,attr_templ,3);
						  if (ReturnedSuccesfull(frv,&(retVal.pkcs11rv), "C_GetAttributeValue X1", "test_getattributevalue" ))
						  {
							frv = (*functions->C_GetAttributeValue)(session_handle,hObject,attr_templ,3);
							if (ReturnedSuccesfull(frv,&(retVal.pkcs11rv), "C_GetAttributeValue X2", "test_getattributevalue" ))
							{
							  CK_ULONG_PTR pretValueLen = attr_templ[2].pValue;
							  testlog(LVL_INFO,"key CKA_LABEL = ");
							  testlogbytes(LVL_INFO,(CK_BYTE_PTR)(attr_templ[0].pValue), attr_templ[0].ulValueLen);
							  testlog(LVL_INFO,"key CKA_VALUE = ");
							  testlogbytes(LVL_INFO,(CK_BYTE_PTR)(attr_templ[1].pValue), attr_templ[1].ulValueLen);
							  testlog(LVL_INFO,"key CKA_VALUE_LEN = %d",*pretValueLen);
							  if(*pretValueLen != attr_templ[1].ulValueLen)
							  {
								testlog(LVL_ERROR,"C_GetAttributeValue : key CKA_VALUE_LEN doesn't match CKA_VALUE .ulValueLen\n");
							  }
							}
						  }						  
						  else
						  {
							testlog(LVL_ERROR,"test_getattributevalue malloc failed\n");
							retVal.basetestrv = TEST_ERROR;
						  }
						  free (pLabel);						  
						}
						else
						{
						  testlog(LVL_ERROR,"test_getattributevalue malloc failed\n");
						  retVal.basetestrv = TEST_ERROR;
						}	
						free (pValue);
					  }
					}
					frv = (*functions->C_FindObjects)(session_handle, &hObject,1,&ulObjectCount); 
					ReturnedSuccesfull(frv,&(retVal.pkcs11rv), "C_FindObjects", "test_getattributevalue" );
				  }
				  frv = (*functions->C_FindObjectsFinal)(session_handle); 
				  ReturnedSuccesfull(frv,&(retVal.pkcs11rv), "C_FindObjectsFinal", "test_getattributevalue" );
				}
			  }
			  frv = (*functions->C_CloseSession) (session_handle);
			  ReturnedSuccesfull(frv,&(retVal.pkcs11rv), "C_CloseSession", "test_getattributevalue" );
			}
		  }
		  free(slotIds);
		}
		else //malloc failed
		{
		  testlog(LVL_INFO,"malloc failed");
		  retVal.basetestrv = TEST_ERROR;
		}
	  }
	  frv = (*functions->C_Finalize) (NULL_PTR);
	  ReturnedSuccesfull(frv,&(retVal.pkcs11rv), "C_Finalize", "test_getattributevalue" );
	}
	dlclose(handle);
  }
  else
  {
	retVal.basetestrv = TEST_ERROR;
  }
  testlog(LVL_INFO, "test_getattributevalue leave\n");
  return retVal;
} 
Esempio n. 5
0
int readslots(CK_FUNCTION_LIST_PTR functions) {

	CK_RV frv;
	CK_C_INITIALIZE_ARGS init_args;
	long slot_count;
	CK_SLOT_ID_PTR slotIds;
	int slotIdx;
	CK_INFO info;
	CK_SESSION_HANDLE session_handle;

	testlog(LVL_INFO,"readslots enter\n");
	init_args.flags = CKF_OS_LOCKING_OK;
	init_args.pReserved = NULL;

	// C_Initialize
	frv = (*functions->C_Initialize) ( (CK_VOID_PTR)&init_args );
	if (CKR_OK != frv) {
	    testlog(LVL_ERROR, "C_Initialize error\n");
	    testlog(LVL_INFO,"readslots leave\n");
		return 1;  
	}
	// C_GetInfo
	frv = (*functions->C_GetInfo) (&info);
	if (CKR_OK != frv) {
		testlog(LVL_ERROR, "C_GetInfo error\n");
		goto finalize;
	}
	testlog(LVL_INFO,"library version: %d.%d\n", info.libraryVersion.major, info.libraryVersion.minor);
	testlog(LVL_INFO,"PKCS#11 version: %d.%d\n", info.cryptokiVersion.major, info.cryptokiVersion.minor);

	// C_GetSlotList
	frv = (*functions->C_GetSlotList) (0, 0, &slot_count);
	if (CKR_OK != frv) {
		testlog(LVL_ERROR, "C_GetSlotList error\n");
		goto finalize;
	}
	testlog(LVL_DEBUG,"slot count: %i\n", slot_count);
	slotIds = malloc(slot_count * sizeof(CK_SLOT_INFO));
	frv = (*functions->C_GetSlotList) (CK_FALSE, slotIds, &slot_count);
	if (CKR_OK != frv) {
		testlog(LVL_ERROR, "C_GetSlotList (2) error\n");
		goto finalize;
	}

	for (slotIdx = 0; slotIdx < slot_count; slotIdx++) {
		CK_SLOT_INFO slotInfo;		
		CK_SLOT_ID slotId = slotIds[slotIdx];
		int idx;
		frv = (*functions->C_GetSlotInfo) (slotId, &slotInfo);
		if (CKR_OK != frv) {
			testlog(LVL_ERROR, "C_GetSlotInfo error\n");
			goto finalize;		
		}
		for (idx = 64 - 1; idx > 0; idx--) {
			if (slotInfo.slotDescription[idx] == ' ') {
				slotInfo.slotDescription[idx] = '\0';			
			} else {
				break;
			}		
		}
		testlog(LVL_DEBUG,"slot Id: %d\n", slotId);
		testlog(LVL_DEBUG,"slot description: %s\n", slotInfo.slotDescription);
		// C_OpenSession
			frv = (*functions->C_OpenSession)(slotId, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &session_handle);
			if (CKR_OK != frv) {
				testlog(LVL_ERROR, "C_OpenSession error\n");
				goto finalize;
			}
		// C_CloseSession
		frv = (*functions->C_CloseSession) (session_handle);
		if (CKR_OK != frv) {
			testlog(LVL_ERROR, "C_CloseSession error\n");
			goto finalize;
		}
	}//end of for

	// C_Finalize
finalize:
	frv = (*functions->C_Finalize) (NULL_PTR);
	if (CKR_OK != frv) {
		testlog(LVL_ERROR, "C_Finalize error\n");
		exit(1);
	}
	testlog(LVL_INFO,"readslots leave\n");
	return 0;
}