⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 devicelist.cpp

📁 对PKCS11接口的封装
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*===========================================================================


		说明:

			通用函数 支持平台:WIN98/2K/XP...

		文件名:	

			DeviceList.cpp
---------------------------------------------------------------------------
		描述:		

			

---------------------------------------------------------------------------
		修改:

*===========================================================================*/



#include "stdafx.h"
#include "DeviceList.h"
#include "KeyCommon.h"
#include "KeyExport.h"

CDeviceList &GetDeviceListHandle()
{
	static CDeviceList theDeviceLayer;
	return theDeviceLayer;
}

CDeviceList::CDeviceList(void)
{
	m_hSession = NULL_PTR;
	m_hAppletSession = NULL_PTR;
}

CDeviceList::~CDeviceList(void)
{
	if(m_hAppletSession)
	{
		CloseAppletSession();
		m_hAppletSession = NULL_PTR;
	}

	if(m_hSession)
	{
		//CloseTokenSession(m_hSession);
		m_hSession = NULL_PTR;
	}
}

//slot
long CDeviceList::GetSlotCount(ULONG &ulCount)
{
	DebugOutFunc("GetSlotCout");
	
	CK_SLOT_ID ckSlotID[10] = {0};

	m_dwErrCode = CKR_OK;

	m_dwErrCode = RefreshSlotList(ulCount, ckSlotID);
	if(CKR_OK != m_dwErrCode)
	{
		DebugOutPosition(__FILE__, __LINE__);		
	}
	
	DebugOutRet(m_dwErrCode);
}

long CDeviceList::RefreshSlotList(ULONG &ulCount, CK_SLOT_ID ckSlotID[])
{
	DebugOutFunc("RefreshSlotList");

	m_dwErrCode = CKR_OK;

	if(!CheckPkcs11FuncList())
	{
		m_dwErrCode = USBKEY_INVALID_LIBRARY_HANDLE;
		DebugOutPosition(__FILE__, __LINE__);
		DebugOutRet(m_dwErrCode);
	}
	
	m_dwErrCode = m_Pkcs11FuncList->C_GetSlotList(TRUE, ckSlotID, &ulCount);
	if(CKR_OK != m_dwErrCode)
	{
		DebugOutPosition(__FILE__, __LINE__);
		goto end;
	}

	if(ulCount <= 0)
	{
		m_dwErrCode = USBKEY_KEY_REMOVED;
		DebugOutPosition(__FILE__, __LINE__);
		goto end;
	}
end:
	DebugOutRet(m_dwErrCode);
}


long CDeviceList::GetTokenInfo(CK_SLOT_ID ulSlotID, PVOID pTokenInfo)
{
	DebugOutFunc("GetTokenSpecialInfo");

	m_dwErrCode = CKR_OK;
	CK_TOKEN_INFO_PTR pToken = (CK_TOKEN_INFO_PTR)pTokenInfo;

	if(pToken == NULL_PTR)
	{
		m_dwErrCode = USBKEY_INVALID_PARAMETER;
		DebugOutPosition(__FILE__, __LINE__);
		DebugOutRet(m_dwErrCode);
	}

	if(!CheckPkcs11FuncList())
	{
		m_dwErrCode = USBKEY_INVALID_LIBRARY_PATH;
		DebugOutPosition(__FILE__, __LINE__);
		DebugOutRet(m_dwErrCode);
	}

	m_dwErrCode = m_Pkcs11FuncList->C_GetTokenInfo(ulSlotID, pToken);
	if(CKR_OK != m_dwErrCode)
	{
		DebugOutPosition(__FILE__, __LINE__);
		DebugOutRet(m_dwErrCode);
	}

	DebugOutRet(m_dwErrCode);
}

long CDeviceList::GetTokenAuxInfo(CK_SLOT_ID ckSlotId, PVOID pAuxInfo)
{
	DebugOutFunc("GetTokenAuxInfo");

	m_dwErrCode = CKR_OK;

	AUX_PIN_INFO_PTR pPinInfo = (AUX_PIN_INFO_PTR) pAuxInfo;
	if(!CheckPkcs11FuncList())
	{
		m_dwErrCode = USBKEY_INVALID_LIBRARY_HANDLE;
		DebugOutPosition(__FILE__, __LINE__);
		DebugOutRet(m_dwErrCode);
	}

	m_dwErrCode = ((EP_GetPinInfo)(m_AuxPkcs11FuncList->pFunc[EP_GET_PIN_INFO]))(ckSlotId, pPinInfo);
	if(CKR_OK != m_dwErrCode)
	{
		DebugOutPosition(__FILE__, __LINE__);
	}

	DebugOutRet(m_dwErrCode);
}

//session 

long CDeviceList::OpenAppletSession(CK_SLOT_ID ckSlotID)
{
	m_dwErrCode = CKR_OK;

	if(m_hSession == NULL_PTR)
	{
		m_dwErrCode = OpenTokenSession(m_hSession, ckSlotID);
		if(CKR_OK != m_dwErrCode)
		{
			DebugOutPosition(__FILE__, __LINE__);
			goto end;
		}
		
		m_dwErrCode = m_Pkcs11FuncList->C_OpenSession(ckSlotID,  
			CKF_RW_SESSION | CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &m_hAppletSession);
		if(CKR_OK != m_dwErrCode)
		{
			DebugOutPosition(__FILE__, __LINE__);
			goto end;
		}
	}
	else if(m_hAppletSession == NULL_PTR)
	{
		m_dwErrCode = m_Pkcs11FuncList->C_OpenSession(ckSlotID,  
			CKF_RW_SESSION | CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &m_hAppletSession);
		if(CKR_OK != m_dwErrCode)
		{
			DebugOutPosition(__FILE__, __LINE__);
			goto end;
		}	
	}
	else
		m_dwErrCode = CKR_OK;
end:
	DebugOutRet(m_dwErrCode);
}

long CDeviceList::CloseAppletSession()
{
	if(m_hAppletSession != NULL_PTR)
	{		
		m_dwErrCode = m_Pkcs11FuncList->C_CloseSession(m_hAppletSession);
		m_hAppletSession = NULL_PTR;
	}
	DebugOutRet(CKR_OK);
}

long CDeviceList::GetAppletSessionInfo(CK_USER_TYPE& ulType, BOOL& bLoged)
{
	if(m_hAppletSession != NULL_PTR)
	{		
		m_dwErrCode = GetSessionInfo(m_hAppletSession, ulType, bLoged);
		if(m_dwErrCode != CKR_OK)
		{
			DebugOutPosition(__FILE__, __LINE__);
			DebugOutRet(m_dwErrCode);
		}
	}
	DebugOutRet(CKR_OK);
}

long CDeviceList::OpenTokenSession(CK_SESSION_HANDLE& hSession, CK_SLOT_ID hOnLineSlotId)
{
	DebugOutFunc("OpenTokenSession");

	m_dwErrCode = CKR_OK;

	if(0 == hOnLineSlotId)
	{
		m_dwErrCode = USBKEY_INVALID_PARAMETER;
		DebugOutPosition(__FILE__, __LINE__);
		goto end;
	}

	if(!CheckPkcs11FuncList())
	{
		m_dwErrCode = USBKEY_INVALID_LIBRARY_PATH;
		DebugOutPosition(__FILE__, __LINE__);
		goto end;
	}

	if(m_hSession != NULL)
	{
		DebugOutRet(CKR_OK);
	}
	
	m_dwErrCode = m_Pkcs11FuncList->C_OpenSession(hOnLineSlotId,  
		CKF_RW_SESSION | CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSession);
	if(CKR_OK != m_dwErrCode)
	{
		DebugOutPosition(__FILE__, __LINE__);
	}

end:
	DebugOutRet(m_dwErrCode);
}

long CDeviceList::CloseTokenSession(CK_SESSION_HANDLE hSession)
{
	DebugOutFunc("CloseTokenSession");

	m_dwErrCode = CKR_OK;
	
	if(!CheckPkcs11FuncList())
	{
		m_dwErrCode = USBKEY_INVALID_LIBRARY_PATH;
		DebugOutPosition(__FILE__, __LINE__);
		goto end;
	}

	if(hSession == NULL_PTR)
	{
		m_dwErrCode = USBKEY_INVALID_PARAMETER;
		DebugOutPosition(__FILE__, __LINE__);
		goto end;
	}

	m_dwErrCode = m_Pkcs11FuncList->C_CloseSession(hSession);
	if(m_dwErrCode != CKR_OK)
	{
		DebugOutPosition(__FILE__, __LINE__);
	}
end:
	DebugOutRet(m_dwErrCode);
}

long CDeviceList::GetSessionInfo(CK_SESSION_HANDLE hSession, CK_USER_TYPE& ulType, BOOL& bLoged)
{
	DebugOutFunc("GetSessionInfo");

	m_dwErrCode = CKR_OK;

	CK_SESSION_INFO SessionInfo;

	if(!CheckPkcs11FuncList())
	{
		m_dwErrCode = USBKEY_INVALID_LIBRARY_PATH;
		DebugOutPosition(__FILE__, __LINE__);
		DebugOutRet(m_dwErrCode);
	}

	if(hSession == NULL_PTR)
	{
		m_dwErrCode = USBKEY_INVALID_PARAMETER;
		DebugOutPosition(__FILE__, __LINE__);
		goto end;
	}

	m_dwErrCode = m_Pkcs11FuncList->C_GetSessionInfo(hSession, &SessionInfo);
	if(CKR_OK != m_dwErrCode)
	{
		DebugOutPosition(__FILE__, __LINE__);
		goto end;
	}

	if(CKS_RW_USER_FUNCTIONS == SessionInfo.state)
	{
		ulType = CKU_USER;
		bLoged = TRUE;
		DebugOutStr("session [0X%08x] state is logged by [user]", hSession);
	}
	else if(CKS_RW_SO_FUNCTIONS == SessionInfo.state)
	{
		ulType = CKU_SO;
		bLoged = TRUE;
		DebugOutStr("session [0X%08x] state is logged by [so]", hSession);
	}
	else
	{
		ulType = 3;
		bLoged = FALSE;
		DebugOutStr("session [0X%08x] state isnot logged by [anyone]", hSession);
	}

end:
	DebugOutRet(m_dwErrCode);
}

long CDeviceList::AppletSessionLogIn(CK_USER_TYPE ulType, PUCHAR pbPIN, ULONG ulPINLen)
{
	DebugOutFunc("AppletSessionLogIn");
	
	m_dwErrCode = CKR_OK;
	
	if(!CheckPkcs11FuncList())
	{
		m_dwErrCode = USBKEY_INVALID_LIBRARY_PATH;
		DebugOutPosition(__FILE__, __LINE__);
		DebugOutRet(m_dwErrCode);
	}
	
	if(m_hAppletSession == NULL_PTR)
	{
		m_dwErrCode = USBKEY_INVALID_PARAMETER;
		DebugOutPosition(__FILE__, __LINE__);
		goto end;
	}
	
	m_dwErrCode = m_Pkcs11FuncList->C_Login(m_hAppletSession, ulType, (CK_UTF8CHAR_PTR)pbPIN, ulPINLen);
	if(CKR_OK != m_dwErrCode)
	{
		if(CKR_USER_ALREADY_LOGGED_IN == m_dwErrCode)
		{
			m_dwErrCode = CKR_OK;
			DebugOutStr("session [0X%08x] user already logged in", m_hAppletSession);
		}
		else
		{
			DebugOutPosition(__FILE__, __LINE__);
			goto end;
		}
	}
	
end:
	
	DebugOutRet(m_dwErrCode);
}

long CDeviceList::AppletSessionLogOut()
{
	DebugOutFunc("AppletSessionLogOut");
	
	m_dwErrCode = CKR_OK;
	
	if(!CheckPkcs11FuncList())
	{
		m_dwErrCode = USBKEY_INVALID_LIBRARY_PATH;
		DebugOutPosition(__FILE__, __LINE__);
		DebugOutRet(m_dwErrCode);
	}
	
	if(m_hAppletSession == NULL_PTR )
	{
		m_dwErrCode = USBKEY_USER_NOT_LOG_IN;
		DebugOutPosition(__FILE__, __LINE__);
		goto end;
	}
	
	m_dwErrCode = m_Pkcs11FuncList->C_Logout(m_hAppletSession);
	if(CKR_OK != m_dwErrCode)
	{	
		DebugOutPosition(__FILE__, __LINE__);
		goto end;
	}
	
end:
	
	DebugOutRet(m_dwErrCode);
}

long CDeviceList::LogIn(CK_SESSION_HANDLE hSession, CK_USER_TYPE ulType, PUCHAR pbPIN, ULONG ulPINLen)
{
	DebugOutFunc("LogIn");

	m_dwErrCode = CKR_OK;

	if(!CheckPkcs11FuncList())
	{
		m_dwErrCode = USBKEY_INVALID_LIBRARY_PATH;
		DebugOutPosition(__FILE__, __LINE__);
		DebugOutRet(m_dwErrCode);
	}

	if(hSession == NULL_PTR)
	{
		m_dwErrCode = USBKEY_INVALID_PARAMETER;
		DebugOutPosition(__FILE__, __LINE__);
		goto end;
	}

	m_dwErrCode = m_Pkcs11FuncList->C_Login(hSession, ulType, (CK_UTF8CHAR_PTR)pbPIN, ulPINLen);
	if(CKR_OK != m_dwErrCode)
	{
		if(CKR_USER_ALREADY_LOGGED_IN == m_dwErrCode)
		{
			m_dwErrCode = CKR_OK;
			DebugOutStr("session [0X%08x] user already logged in", hSession);
		}
		else
		{
			DebugOutPosition(__FILE__, __LINE__);
			goto end;
		}
	}

end:

	DebugOutRet(m_dwErrCode);
}

long CDeviceList::LogOut(CK_SESSION_HANDLE hSession)
{
	DebugOutFunc("LogOut");

	m_dwErrCode = CKR_OK;

	if(!CheckPkcs11FuncList())
	{
		m_dwErrCode = USBKEY_INVALID_LIBRARY_PATH;
		DebugOutPosition(__FILE__, __LINE__);
		DebugOutRet(m_dwErrCode);
	}

	if(hSession == NULL_PTR )
	{
		m_dwErrCode = USBKEY_USER_NOT_LOG_IN;
		DebugOutPosition(__FILE__, __LINE__);
		goto end;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -