void SlotManagerTests::testNoExistingTokens()
{
	// Create an empty object store
#ifndef _WIN32
	ObjectStore store("./testdir");
#else
	ObjectStore store(".\\testdir");
#endif

	// Create the slot manager
	SlotManager slotManager(&store);

	CPPUNIT_ASSERT(slotManager.getSlots().size() == 1);

	// Test C_GetSlotList
	CK_SLOT_ID testList[10];
	CK_ULONG ulCount = 10;

	CPPUNIT_ASSERT(slotManager.getSlotList(CK_FALSE, testList, &ulCount) == CKR_OK);
	CPPUNIT_ASSERT(ulCount == 1);

	ulCount = 10;

	CPPUNIT_ASSERT(slotManager.getSlotList(CK_TRUE, testList, &ulCount) == CKR_OK);
	CPPUNIT_ASSERT(ulCount == 1);
	CPPUNIT_ASSERT(slotManager.getSlots()[0]->getSlotID() == testList[0]);

	// Retrieve slot information about the first slot
	CK_SLOT_INFO slotInfo;

	CPPUNIT_ASSERT(slotManager.getSlots()[0]->getSlotInfo(&slotInfo) == CKR_OK);

	CPPUNIT_ASSERT((slotInfo.flags & CKF_TOKEN_PRESENT) == CKF_TOKEN_PRESENT);

	// Retrieve token information about the token in the first slot
	CK_TOKEN_INFO tokenInfo;

	CPPUNIT_ASSERT(slotManager.getSlots()[0]->getToken() != NULL);
	CPPUNIT_ASSERT(slotManager.getSlots()[0]->getToken()->getTokenInfo(&tokenInfo) == CKR_OK);

	CPPUNIT_ASSERT((tokenInfo.flags & CKF_TOKEN_INITIALIZED) != CKF_TOKEN_INITIALIZED);
}
void SlotManagerTests::testReinitialiseExistingToken()
{
	// Create an empty object store
#ifndef _WIN32
	ObjectStore store("./testdir");
#else
	ObjectStore store(".\\testdir");
#endif

	// Create two tokens
	ByteString label1 = "DEADBEEF";
	ByteString label2 = "DEADC0FFEE";

	CPPUNIT_ASSERT(store.newToken(label1) != NULL);
	CPPUNIT_ASSERT(store.newToken(label2) != NULL);

	// Now attach the slot manager
	SlotManager slotManager(&store);

	CPPUNIT_ASSERT(slotManager.getSlots().size() == 3);

	// Test C_GetSlotList
	CK_SLOT_ID testList[10];
	CK_ULONG ulCount = 10;

	CPPUNIT_ASSERT(slotManager.getSlotList(CK_FALSE, testList, &ulCount) == CKR_OK);
	CPPUNIT_ASSERT(ulCount == 3);

	ulCount = 10;

	CPPUNIT_ASSERT(slotManager.getSlotList(CK_TRUE, testList, &ulCount) == CKR_OK);
	CPPUNIT_ASSERT(ulCount == 3);
	CPPUNIT_ASSERT(slotManager.getSlots()[0]->getSlotID() == testList[0]);
	CPPUNIT_ASSERT(slotManager.getSlots()[1]->getSlotID() == testList[1]);
	CPPUNIT_ASSERT(slotManager.getSlots()[2]->getSlotID() == testList[2]);

	// Retrieve slot information about the first slot
	CK_SLOT_INFO slotInfo;

	CPPUNIT_ASSERT(slotManager.getSlots()[0]->getSlotInfo(&slotInfo) == CKR_OK);

	CPPUNIT_ASSERT((slotInfo.flags & CKF_TOKEN_PRESENT) == CKF_TOKEN_PRESENT);

	// Retrieve token information about the token in the first slot
	CK_TOKEN_INFO tokenInfo;

	CPPUNIT_ASSERT(slotManager.getSlots()[0]->getToken() != NULL);
	CPPUNIT_ASSERT(slotManager.getSlots()[0]->getToken()->getTokenInfo(&tokenInfo) == CKR_OK);

	CPPUNIT_ASSERT((tokenInfo.flags & CKF_TOKEN_INITIALIZED) == CKF_TOKEN_INITIALIZED);
	CPPUNIT_ASSERT(!memcmp(tokenInfo.label, &label1[0], label1.size()) || 
	               !memcmp(tokenInfo.label, &label2[0], label2.size()));

	// Retrieve slot information about the second slot
	CPPUNIT_ASSERT(slotManager.getSlots()[1]->getSlotInfo(&slotInfo) == CKR_OK);

	CPPUNIT_ASSERT((slotInfo.flags & CKF_TOKEN_PRESENT) == CKF_TOKEN_PRESENT);

	// Retrieve token information about the token in the second slot
	CPPUNIT_ASSERT(slotManager.getSlots()[1]->getToken() != NULL);
	CPPUNIT_ASSERT(slotManager.getSlots()[1]->getToken()->getTokenInfo(&tokenInfo) == CKR_OK);

	CPPUNIT_ASSERT((tokenInfo.flags & CKF_TOKEN_INITIALIZED) == CKF_TOKEN_INITIALIZED);
	CPPUNIT_ASSERT(!memcmp(tokenInfo.label, &label1[0], label1.size()) || 
	               !memcmp(tokenInfo.label, &label2[0], label2.size()));

	// Retrieve slot information about the third slot
	CPPUNIT_ASSERT(slotManager.getSlots()[2]->getSlotInfo(&slotInfo) == CKR_OK);

	CPPUNIT_ASSERT((slotInfo.flags & CKF_TOKEN_PRESENT) == CKF_TOKEN_PRESENT);

	// Retrieve token information about the token in the third slot
	CPPUNIT_ASSERT(slotManager.getSlots()[2]->getToken() != NULL);
	CPPUNIT_ASSERT(slotManager.getSlots()[2]->getToken()->getTokenInfo(&tokenInfo) == CKR_OK);

	CPPUNIT_ASSERT((tokenInfo.flags & CKF_TOKEN_INITIALIZED) != CKF_TOKEN_INITIALIZED);

	// Now reinitialise the token in the second slot
	ByteString soPIN((unsigned char*)"1234", 4);
	CK_UTF8CHAR label[33] = "My test token                   ";

	CPPUNIT_ASSERT(slotManager.getSlots()[1]->initToken(soPIN, label) == CKR_OK);

	// Retrieve slot information about the first slot
	CPPUNIT_ASSERT(slotManager.getSlots()[1]->getSlotInfo(&slotInfo) == CKR_OK);

	CPPUNIT_ASSERT((slotInfo.flags & CKF_TOKEN_PRESENT) == CKF_TOKEN_PRESENT);

	// Retrieve token information about the token in the first slot
	CPPUNIT_ASSERT(slotManager.getSlots()[1]->getToken() != NULL);
	CPPUNIT_ASSERT(slotManager.getSlots()[1]->getToken()->getTokenInfo(&tokenInfo) == CKR_OK);

	CPPUNIT_ASSERT((tokenInfo.flags & CKF_TOKEN_INITIALIZED) == CKF_TOKEN_INITIALIZED);
	CPPUNIT_ASSERT(!memcmp(tokenInfo.label, label, 32));
}
Esempio n. 3
0
void SessionManagerTests::testOpenClose()
{
	// Create an empty object store
#ifndef _WIN32
	ObjectStore store("./testdir");
#else
	ObjectStore store(".\\testdir");
#endif

	// Create the managers
	SlotManager slotManager(&store);
	SessionManager sessionManager;

	// Get a slot
	CK_SLOT_ID slotID = 0;
	Slot* slot = slotManager.getSlot(slotID);

	// Use some bad data
	CK_SESSION_HANDLE hSession;
	CK_RV rv = sessionManager.openSession(NULL, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSession);
	CPPUNIT_ASSERT(rv == CKR_SLOT_ID_INVALID);
	rv = sessionManager.openSession(slot, 0, NULL_PTR, NULL_PTR, &hSession);
	CPPUNIT_ASSERT(rv == CKR_SESSION_PARALLEL_NOT_SUPPORTED);
	rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, NULL_PTR);
	CPPUNIT_ASSERT(rv == CKR_ARGUMENTS_BAD);

	// Try open a slot with an uninitialized token
	rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSession);
	CPPUNIT_ASSERT(rv == CKR_TOKEN_NOT_RECOGNIZED);

	// Initialize the token
	ByteString soPIN((unsigned char*)"1234", 4);
	CK_UTF8CHAR label[33] = "My test token                   ";
	CPPUNIT_ASSERT(slot->initToken(soPIN, label) == CKR_OK);

	// Open a session
	bool haveSession = sessionManager.haveSession(slotID);
	CPPUNIT_ASSERT(haveSession == false);
	bool haveROSession = sessionManager.haveROSession(slotID);
	CPPUNIT_ASSERT(haveROSession == false);
	rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSession);
	CPPUNIT_ASSERT(rv == CKR_OK);
	haveSession = sessionManager.haveSession(slotID);
	CPPUNIT_ASSERT(haveSession == true);
	haveROSession = sessionManager.haveROSession(slotID);
	CPPUNIT_ASSERT(haveROSession == true);

	// Close session
	rv = sessionManager.closeSession(CK_INVALID_HANDLE);
	CPPUNIT_ASSERT(rv == CKR_SESSION_HANDLE_INVALID);
	rv = sessionManager.closeSession(hSession);
	CPPUNIT_ASSERT(rv == CKR_OK);
	rv = sessionManager.closeSession(hSession);
	CPPUNIT_ASSERT(rv == CKR_SESSION_HANDLE_INVALID);
	haveSession = sessionManager.haveSession(slotID);
	CPPUNIT_ASSERT(haveSession == false);
	haveROSession = sessionManager.haveROSession(slotID);
	CPPUNIT_ASSERT(haveROSession == false);

	// Try open a Read-Only session when in SO mode
	rv = slot->getToken()->loginSO(soPIN);
	CPPUNIT_ASSERT(rv == CKR_OK);
	rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSession);
	CPPUNIT_ASSERT(rv == CKR_SESSION_READ_WRITE_SO_EXISTS);
	rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSession);
	CPPUNIT_ASSERT(rv == CKR_OK);
	haveSession = sessionManager.haveSession(slotID);
	CPPUNIT_ASSERT(haveSession == true);
	haveROSession = sessionManager.haveROSession(slotID);
	CPPUNIT_ASSERT(haveROSession == false);

	// Close session and check that we are logged out
	bool isLoggedIn = slot->getToken()->isSOLoggedIn();
	CPPUNIT_ASSERT(isLoggedIn == true);
	rv = sessionManager.closeSession(hSession);
	CPPUNIT_ASSERT(rv == CKR_OK);
	isLoggedIn = slot->getToken()->isSOLoggedIn();
	CPPUNIT_ASSERT(isLoggedIn == false);
	haveSession = sessionManager.haveSession(slotID);
	CPPUNIT_ASSERT(haveSession == false);
	haveROSession = sessionManager.haveROSession(slotID);
	CPPUNIT_ASSERT(haveROSession == false);

	// Open a new logged in session
	rv = slot->getToken()->loginSO(soPIN);
	CPPUNIT_ASSERT(rv == CKR_OK);
	rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSession);
	CPPUNIT_ASSERT(rv == CKR_OK);

	// Close all sessions and check that we are logged out
	isLoggedIn = slot->getToken()->isSOLoggedIn();
	CPPUNIT_ASSERT(isLoggedIn == true);
	rv = sessionManager.closeAllSessions(NULL);
	CPPUNIT_ASSERT(rv == CKR_SLOT_ID_INVALID);
	rv = sessionManager.closeAllSessions(slot);
	CPPUNIT_ASSERT(rv == CKR_OK);
	isLoggedIn = slot->getToken()->isSOLoggedIn();
	CPPUNIT_ASSERT(isLoggedIn == false);
}
void SlotManagerTests::testInitialiseTokenInLastSlot()
{
	{
		// Create an empty object store
#ifndef _WIN32
		ObjectStore store("./testdir");
#else
		ObjectStore store(".\\testdir");
#endif
	
		// Create the slot manager
		SlotManager slotManager(&store);
	
		CPPUNIT_ASSERT(slotManager.getSlots().size() == 1);
	
		// Test C_GetSlotList
		CK_SLOT_ID testList[10];
		CK_ULONG ulCount = 10;
	
		CPPUNIT_ASSERT(slotManager.getSlotList(CK_FALSE, testList, &ulCount) == CKR_OK);
		CPPUNIT_ASSERT(ulCount == 1);
	
		ulCount = 10;
	
		CPPUNIT_ASSERT(slotManager.getSlotList(CK_TRUE, testList, &ulCount) == CKR_OK);
		CPPUNIT_ASSERT(ulCount == 1);
		CPPUNIT_ASSERT(slotManager.getSlots()[0]->getSlotID() == testList[0]);
	
		// Retrieve slot information about the first slot
		CK_SLOT_INFO slotInfo;
	
		CPPUNIT_ASSERT(slotManager.getSlots()[0]->getSlotInfo(&slotInfo) == CKR_OK);
	
		CPPUNIT_ASSERT((slotInfo.flags & CKF_TOKEN_PRESENT) == CKF_TOKEN_PRESENT);
	
		// Retrieve token information about the token in the first slot
		CK_TOKEN_INFO tokenInfo;
	
		CPPUNIT_ASSERT(slotManager.getSlots()[0]->getToken() != NULL);
		CPPUNIT_ASSERT(slotManager.getSlots()[0]->getToken()->getTokenInfo(&tokenInfo) == CKR_OK);
	
		CPPUNIT_ASSERT((tokenInfo.flags & CKF_TOKEN_INITIALIZED) != CKF_TOKEN_INITIALIZED);
	
		// Now initialise the token in the first slot
		ByteString soPIN((unsigned char*)"1234", 4);
		CK_UTF8CHAR label[33] = "My test token                   ";
	
		CPPUNIT_ASSERT(slotManager.getSlots()[0]->initToken(soPIN, label) == CKR_OK);
	
		// Retrieve slot information about the first slot
		CPPUNIT_ASSERT(slotManager.getSlots()[0]->getSlotInfo(&slotInfo) == CKR_OK);
	
		CPPUNIT_ASSERT((slotInfo.flags & CKF_TOKEN_PRESENT) == CKF_TOKEN_PRESENT);
	
		// Retrieve token information about the token in the first slot
		CPPUNIT_ASSERT(slotManager.getSlots()[0]->getToken() != NULL);
		CPPUNIT_ASSERT(slotManager.getSlots()[0]->getToken()->getTokenInfo(&tokenInfo) == CKR_OK);
	
		CPPUNIT_ASSERT((tokenInfo.flags & CKF_TOKEN_INITIALIZED) == CKF_TOKEN_INITIALIZED);
		CPPUNIT_ASSERT(!memcmp(tokenInfo.label, label, 32));
	}

	// Attach a fresh slot manager
#ifndef _WIN32
	ObjectStore store("./testdir");
#else
	ObjectStore store(".\\testdir");
#endif
	SlotManager slotManager(&store);

	CPPUNIT_ASSERT(slotManager.getSlots().size() == 2);

	// Test C_GetSlotList
	CK_SLOT_ID testList[10];
	CK_ULONG ulCount = 10;

	CPPUNIT_ASSERT(slotManager.getSlotList(CK_FALSE, testList, &ulCount) == CKR_OK);
	CPPUNIT_ASSERT(ulCount == 2);

	ulCount = 10;

	CPPUNIT_ASSERT(slotManager.getSlotList(CK_TRUE, testList, &ulCount) == CKR_OK);
	CPPUNIT_ASSERT(ulCount == 2);
	CPPUNIT_ASSERT(slotManager.getSlots()[0]->getSlotID() == testList[0]);
	CPPUNIT_ASSERT(slotManager.getSlots()[1]->getSlotID() == testList[1]);

	// Retrieve slot information about the first slot
	CK_SLOT_INFO slotInfo;

	CPPUNIT_ASSERT(slotManager.getSlots()[0]->getSlotInfo(&slotInfo) == CKR_OK);

	CPPUNIT_ASSERT((slotInfo.flags & CKF_TOKEN_PRESENT) == CKF_TOKEN_PRESENT);

	// Retrieve token information about the token in the first slot
	CK_TOKEN_INFO tokenInfo;

	CPPUNIT_ASSERT(slotManager.getSlots()[0]->getToken() != NULL);
	CPPUNIT_ASSERT(slotManager.getSlots()[0]->getToken()->getTokenInfo(&tokenInfo) == CKR_OK);

	CPPUNIT_ASSERT((tokenInfo.flags & CKF_TOKEN_INITIALIZED) == CKF_TOKEN_INITIALIZED);

	CK_UTF8CHAR label[33] = "My test token                   ";
	CPPUNIT_ASSERT(!memcmp(tokenInfo.label, label, 32));

	// Retrieve slot information about the second slot
	CPPUNIT_ASSERT(slotManager.getSlots()[1]->getSlotInfo(&slotInfo) == CKR_OK);

	CPPUNIT_ASSERT((slotInfo.flags & CKF_TOKEN_PRESENT) == CKF_TOKEN_PRESENT);

	// Retrieve token information about the token in the second slot
	CPPUNIT_ASSERT(slotManager.getSlots()[1]->getToken() != NULL);
	CPPUNIT_ASSERT(slotManager.getSlots()[1]->getToken()->getTokenInfo(&tokenInfo) == CKR_OK);

	CPPUNIT_ASSERT((tokenInfo.flags & CKF_TOKEN_INITIALIZED) != CKF_TOKEN_INITIALIZED);
}
Esempio n. 5
0
void SessionManagerTests::testSessionInfo()
{
	// Create an empty object store
#ifndef _WIN32
	ObjectStore store("./testdir");
#else
	ObjectStore store(".\\testdir");
#endif

	// Create the managers
	SlotManager slotManager(&store);
	SessionManager sessionManager;

	// Get a slot
	CK_SLOT_ID slotID = 0;
	Slot* slot = slotManager.getSlot(slotID);

	// Initialize the token
	ByteString soPIN((unsigned char*)"1234", 4);
	ByteString userPIN((unsigned char*)"1234", 4);
	CK_UTF8CHAR label[33] = "My test token                   ";
	CPPUNIT_ASSERT(slot->initToken(soPIN, label) == CKR_OK);
	CPPUNIT_ASSERT(slot->getToken()->loginSO(soPIN) == CKR_OK);
	CPPUNIT_ASSERT(slot->getToken()->initUserPIN(userPIN) == CKR_OK);
	slot->getToken()->logout();

	// Get a session
	CK_SESSION_HANDLE hSession;
	CK_RV rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSession);
	CPPUNIT_ASSERT(rv == CKR_OK);

	// Get session info
	CK_SESSION_INFO info;
	rv = sessionManager.getSessionInfo(CK_INVALID_HANDLE, &info);
	CPPUNIT_ASSERT(rv == CKR_SESSION_HANDLE_INVALID);
	rv = sessionManager.getSessionInfo(hSession, NULL_PTR);
	CPPUNIT_ASSERT(rv == CKR_ARGUMENTS_BAD);
	rv = sessionManager.getSessionInfo(hSession, &info);
	CPPUNIT_ASSERT(rv == CKR_OK);

	// Public RO session info
	CPPUNIT_ASSERT(info.slotID == slotID);
	CPPUNIT_ASSERT(info.state == CKS_RO_PUBLIC_SESSION);
	CPPUNIT_ASSERT(info.flags == CKF_SERIAL_SESSION);

	rv = sessionManager.closeSession(hSession);
	CPPUNIT_ASSERT(rv == CKR_OK);

	// Public RW session info
	rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSession);
	CPPUNIT_ASSERT(rv == CKR_OK);
	Session* session = sessionManager.getSession(CK_INVALID_HANDLE);
	CPPUNIT_ASSERT(session == NULL);
	session = sessionManager.getSession(hSession);
	CPPUNIT_ASSERT(session != NULL);
	rv = session->getInfo(&info);
	CPPUNIT_ASSERT(rv == CKR_OK);
	CPPUNIT_ASSERT(info.state == CKS_RW_PUBLIC_SESSION);
	CPPUNIT_ASSERT(info.flags == (CKF_SERIAL_SESSION | CKF_RW_SESSION));

	rv = sessionManager.closeSession(hSession);
	CPPUNIT_ASSERT(rv == CKR_OK);

	// User RO session info
	rv = slot->getToken()->loginUser(userPIN);
	CPPUNIT_ASSERT(rv == CKR_OK);
	rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSession);
	CPPUNIT_ASSERT(rv == CKR_OK);
	rv = sessionManager.getSessionInfo(hSession, &info);
	CPPUNIT_ASSERT(rv == CKR_OK);
	CPPUNIT_ASSERT(info.state == CKS_RO_USER_FUNCTIONS);
	CPPUNIT_ASSERT(info.flags == CKF_SERIAL_SESSION);

	rv = sessionManager.closeSession(hSession);
	CPPUNIT_ASSERT(rv == CKR_OK);

	// User RW session info
	rv = slot->getToken()->loginUser(userPIN);
	CPPUNIT_ASSERT(rv == CKR_OK);
	rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSession);
	CPPUNIT_ASSERT(rv == CKR_OK);
	rv = sessionManager.getSessionInfo(hSession, &info);
	CPPUNIT_ASSERT(rv == CKR_OK);
	CPPUNIT_ASSERT(info.state == CKS_RW_USER_FUNCTIONS);
	CPPUNIT_ASSERT(info.flags == (CKF_SERIAL_SESSION | CKF_RW_SESSION));

	rv = sessionManager.closeSession(hSession);
	CPPUNIT_ASSERT(rv == CKR_OK);

	// SO RW session info
	rv = slot->getToken()->loginSO(soPIN);
	CPPUNIT_ASSERT(rv == CKR_OK);
	rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSession);
	CPPUNIT_ASSERT(rv == CKR_OK);
	rv = sessionManager.getSessionInfo(hSession, &info);
	CPPUNIT_ASSERT(rv == CKR_OK);
	CPPUNIT_ASSERT(info.state == CKS_RW_SO_FUNCTIONS);
	CPPUNIT_ASSERT(info.flags == (CKF_SERIAL_SESSION | CKF_RW_SESSION));

	rv = sessionManager.closeSession(hSession);
	CPPUNIT_ASSERT(rv == CKR_OK);
}