Просмотр исходного кода

Z991239-1350 #comment 准入以及初始化实体对safedelete的使用修改

雷志明80280620 5 лет назад
Родитель
Сommit
4eec0dc444
2 измененных файлов с 30 добавлено и 768 удалено
  1. 17 614
      Module/mod_Initializer/mod_Initializer.cpp
  2. 13 154
      Module/mod_accessauth/mod_AccessAuth.cpp

+ 17 - 614
Module/mod_Initializer/mod_Initializer.cpp

@@ -22,12 +22,10 @@ using namespace CardSwiper;
 using namespace MaintainWatcher;
 
 #include "comm.h"
-//#include "base64.h"
 #include <fileutil.h>
-//#include "MyBase64.h"
 #define BUF_SIZE 256
 #define KEY_SIZE 16
-//CAppModule _Module;
+
 
 void CInitializerSession::Handle_Initialize(SpOnewayCallContext<InitializerService_Initialize_Info>::Pointer ctx)
 {
@@ -71,42 +69,17 @@ void CInitializerSession::Handle_InitializeForBlueTooth(SpReqAnsContext<Initiali
 
 void CInitializerEntity::OnStarted()
 { 
-	//auto hRes = _Module.Init(NULL, ModuleBase::GetModuleBase()->GetInstance());
-	//ATLASSERT(SUCCEEDED(hRes));
-
-	////检查runinfo/kmc目录是否存在,存在则删除
-	//CSimpleStringA strKmcPath;
-	//GetFunction()->GetPath("runinfo", strKmcPath);
-	//strKmcPath += "\\kmc\\";
-
-	//if (ExistsDirA(strKmcPath))
-	//{
-	//	RemoveDirRecursiveA(strKmcPath);
-	//}
-
-	//检查dbg/kmc目录是否存在,不存在则创建
+	
 	LOG_FUNCTION();
 	CSimpleStringA strKmclogPath;
 	GetFunction()->GetPath("Dbg", strKmclogPath);
-	strKmclogPath += "\\kmc\\";
+	strKmclogPath += "/kmc/";
 
 	if (!ExistsDirA(strKmclogPath))
 	{
 		CreateDirA(strKmclogPath, TRUE);
 	}
 
-	/*
-	CUUID SubLogID1;
-	auto pFunc = GetFunction();
-	auto rc = pFunc->SubscribeLog(SubLogID1, this, Log_Event, Severity_None, Error_IgnoreAll, -1, "MaintainWatcher");
-	if (rc != Error_Succeed)
-	{
-		//LogError(Severity_Middle, rc, ERROR_INITIALIZER_SUBSCRIBEEVENT, CSimpleStringA::Format("subscribe MaintainWatcher event fail, 0x%x", rc));
-		LogWarn(Severity_Middle,Error_Unexpect,ERR_INITIALIZER_SUBSCRIBE,GetOutPutStr("%s%08X%s%s","SubscribeLog",rc,"Subscribe","MaintainWatcher").c_str());
-	}
-	else
-		Dbg("subscribe MaintainWatcher event succeed");
-   */
 	m_bHasCkCode = false;
 
 	m_FSM.Init(this);
@@ -117,34 +90,10 @@ void CInitializerEntity::OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPoint
 	m_FSM.PostExitEvent();
 
 	CloseGUI();
-	//_Module.Term();
 
 	pTransactionContext->SendAnswer(Error_Succeed); 
 }
 
-//void CInitializerEntity::OnLog(const CAutoArray<CUUID> &SubIDs, const CUUID nLogID,const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
-//	const DWORD dwSysError,const DWORD dwUserCode,const DWORD dwEntityInstanceID, const WORD wEntityDevelID, 
-//	const CAutoArray<DWORD> &Param, const char *pszEntityName, const char *pszModuleName,const char *pszMessage)
-//{
-//	//Dbg("OnLog(entity = %s, syscode = 0x%X, usercode = 0x%X", pszEntityName, dwSysError, dwUserCode);
-//
-//	//收到拔出事件,关闭初始化窗口
-//	if (dwUserCode == EVENT_UKEY_PULLOUT)		// 证书拔出
-//	{
-//		Dbg("收到EVENT_UKEY_PULLOUT");
-//		m_strUserID = "";
-//		/*
-//		if (m_hGUIWindow != 0)
-//		{
-//			PostMessage(m_hGUIWindow, WM_CLOSE_DLG, 0, 0);
-//		}
-//		else
-//		{
-//			Dbg("密钥初始化窗口句柄为空");
-//		}
-//		*/
-//	}
-//}
 
 ErrorCodeEnum CInitializerEntity::StartInitialize(const char *pszUserID, const char *pszPassword)
 {
@@ -180,152 +129,16 @@ ErrorCodeEnum CInitializerEntity::StartInitializeNew(const char *pszAuthServer,
 // KMC初始化
 ErrorCodeEnum CInitializerEntity::InitKMC(int nPinPadCapability)
 {
-	/*
-	CSystemStaticInfo si;
-	ErrorCodeEnum rc = GetFunction()->GetSystemStaticInfo(si);	
-	if (rc != Error_Succeed)
-	{
-		LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_GET_SYSTEM_STATIC_INFO,
-				GetOutPutStr("%s%08X", "GetSystemStaticInfo", rc).c_str());
-		m_strLastErrMsg = "获取配置信息(GetSystemStaticInfo)失败";
-		return rc;
-	}
-
-	int nRet = 0;
-
-	if (HasCkCodeFlg())
-	{
-		CSimpleStringA strKmclogPath;
-		GetFunction()->GetPath("Dbg", strKmclogPath);
-		strKmclogPath += "\\kmc\\";
-
-		//调用新接口,把公钥当成参数传入,日志路径改为dbg/kmc, add by zhangliang 20170225
-		//允许分行服务没有国密,生产上没有国密公钥 add by zhangliang 20170306
-		
-		Dbg("m_cTDESPukey[%s]", m_cTDESPukey);
-		Dbg("m_cSM2Pukey[%s]", m_cSM2Pukey);
-		Dbg("m_iTDESPukeyLen[%d], m_iSM2Pukeylen[%d]", m_iTDESPukeyLen, m_iSM2Pukeylen);
-		if (1 == nPinPadCapability)
-		{
-			if (0 != m_iTDESPukeyLen)
-			{
-				nRet = ::RvcInit((const char*)m_cTDESPukey, m_iTDESPukeyLen, NULL, 0, (char*)strKmclogPath.GetData(), DES);
-			} 
-			else
-			{
-				LogError(Severity_Middle, Error_Unexpect, ERROR_INITIALIZER_INITKMC, "nPinPadCapability=1, m_iTDESPukeyLen=0, 请联系分行技术部检查分行服务RVCPubKey.ini!");
-				m_strLastErrMsg = "nPinPadCapability=1, m_iTDESPukeyLen=0, 请联系分行技术部检查分行服务RVCPubKey.ini!";
-				return Error_Unexpect;
-			}
-			
-		}
-		else if (2 == nPinPadCapability)
-		{
-			if (0 != m_iSM2Pukeylen)
-			{
-				nRet = ::RvcInit(NULL, 0, (const char*)m_cSM2Pukey, m_iSM2Pukeylen, (char*)strKmclogPath.GetData(), SM4);
-			} 
-			else
-			{
-				LogError(Severity_Middle, Error_Unexpect, ERROR_INITIALIZER_INITKMC, "nPinPadCapability=2, m_iSM2Pukeylen=0, 请联系分行技术部检查分行服务RVCSM2PubKey.ini!");
-				m_strLastErrMsg = "nPinPadCapability=1, m_iSM2Pukeylen=0, 请联系分行技术部检查分行服务RVCSM2PubKey.ini!";
-				return Error_Unexpect;
-			}		
-		}
-		else if (3 == nPinPadCapability)
-		{
-			if (0 != m_iSM2Pukeylen && 0 != m_iTDESPukeyLen)
-			{
-				nRet = ::RvcInit((const char*)m_cTDESPukey, m_iTDESPukeyLen, (const char*)m_cSM2Pukey, m_iSM2Pukeylen, (char*)strKmclogPath.GetData(), DES_SM4);
-			} 
-			else
-			{
-				LogError(Severity_Middle, Error_Unexpect, ERROR_INITIALIZER_INITKMC, "nPinPadCapability=3, m_iSM2Pukeylen=0, m_iTDESPukeyLen=0, 请联系分行技术部检查分行服务RVCPubKey.ini和RVCSM2PubKey.ini!");
-				m_strLastErrMsg = "nPinPadCapability=3, m_iSM2Pukeylen=0, m_iTDESPukeyLen=0, 请联系分行技术部检查分行服务RVCPubKey.ini和RVCSM2PubKey.ini!";
-				return Error_Unexpect;
-			}			
-		}
-		else
-		{
-			LogError(Severity_Middle, Error_Unexpect, ERROR_INITIALIZER_INITKMC, CSimpleStringA::Format("nPinPadCapability[%d] is wrong, must be 1/2/3", nPinPadCapability));
-			m_strLastErrMsg = CSimpleStringA::Format("密码键盘支持的密钥类型[%d]错误, 必须是1/2/3,请联系厂商维护人员", nPinPadCapability);
-			return Error_Unexpect;
-		}
-	}
-	else
-	{
-		CSimpleStringA strPath;
-		ErrorCodeEnum rc = GetFunction()->GetPath("RunInfo", strPath);
-		LOG_ASSERT(rc == Error_Succeed);
-
-		strPath.Append("\\kmc");
-		nRet = ::Init((const char*)strPath, strPath.GetLength(), nPinPadCapability);
-	}
-	
-	if (nRet == 1)
-	{
-		Dbg("DES WK need update, kmc init ret = %d", nRet);
-		return Error_Succeed;
-	}
-	else if (nRet == 2)
-	{
-		Dbg("DES MK need init, kmc init ret = %d", nRet);
-		return Error_Succeed;
-	}
-	else if (nRet == 11 || nRet == 12)
-	{
-		Dbg("SM WK need update, kmc init ret = %d", nRet);
-		return Error_Succeed;
-	}
-	else if (nRet == 20 || nRet == 22 || nRet == 21)
-	{
-		Dbg("SM MK need init, kmc init ret = %d", nRet);
-		return Error_Succeed;
-	}
-	else if (nRet != 0)
-	{
-		LogError(Severity_Middle, Error_Unexpect, ERROR_INITIALIZER_INITKMC, CSimpleStringA::Format("kmc init fail, GetKMCLastErrMsg[%s], Init return[%d]", (const char*)GetKMCLastErrMsg(), nRet));
-		m_strLastErrMsg = CSimpleStringA::Format("KMC初始化失败, 错误信息[%s], 返回值[%d]", (const char*)GetKMCLastErrMsg(), nRet);
-		return Error_DevLoadFileFailed;
-	}
-
-	Dbg("kmc init succeed!");
-	*/
 	return Error_Succeed;
 }
 
 CSimpleStringA CInitializerEntity::GetKMCLastErrMsg()
 {
-	/*
-	char szBuf[256];
-	memset(szBuf, 0, 256);
-
-	int nBufLen = 256;
-
-	if (HasCkCodeFlg())
-	{
-		RvcGetLastErrorMsg(szBuf, &nBufLen);
-	}
-	else
-	{
-		GetLastErrorMsg(szBuf, &nBufLen);
-	}
-	
-	return szBuf;
-	*/
 	return "";
 }
 
 ErrorCodeEnum CInitializerEntity::ReleaseKMC()
 {
-	/*if (HasCkCodeFlg())
-	{
-		::RvcRelease();
-	}
-	else
-	{
-		::Release();
-	}*/
 	
 	return Error_Succeed;
 }
@@ -404,335 +217,6 @@ string CInitializerEntity::ByteArrayToHexStr(BYTE *pBuf, int nBufLen)
 // 加载主密钥
 DWORD CInitializerEntity::LoadKeysToPinPad(bool bSM)
 {
-	/*{
-		// 取出主密钥
-		char keyBuf[64];
-		int nKeyBufLen = 64;
-		memset(keyBuf, 0, 64);
-
-		char chkBuf[16];
-		int nChkBufLen = 16;
-		memset(chkBuf, 0, 16);
-
-		char* retKeyBuf = NULL;
-
-		if (HasCkCodeFlg())
-		{
-			if (bSM)
-			{
-				retKeyBuf = ::RvcGetKey(m_pkeys, TMK, keyBuf);
-				if (NULL == retKeyBuf)
-				{
-
-					LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_RVC_GET_KEY,
-						GetOutPutStr("%s%s%s%s", "RvcGetKey", retKeyBuf, "strErrMsg", (const char*)GetKMCLastErrMsg()).c_str());
-					return ERR_INITIALIZER_RVC_GET_KEY;
-				}
-
-				PinPadService_LoadKeysSM_Req req = {};
-				req.initializeflag = true;
-				req.smflag = 1;
-				CSimpleStringA tempMasterkey(m_cSM2TMK, 32);
-				req.masterkey = tempMasterkey;
-				Dbg("req.masterkey[%s]", req.masterkey);
-
-				// 读取工作密钥TPK
-				memset(keyBuf, 0, 64);
-				retKeyBuf = RvcGetKey(m_pkeys, TPK, keyBuf);
-				if (NULL == retKeyBuf)
-				{
-					LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_RVC_GET_KEY,
-						GetOutPutStr("%s%s%s%s", "RvcGetKey", retKeyBuf, "strErrMsg", (const char*)GetKMCLastErrMsg()).c_str());
-					return ERR_INITIALIZER_RVC_GET_KEY;
-				}
-
-				CSimpleStringA tempWorkingkey1(keyBuf, 32);
-				req.workingkey1 = tempWorkingkey1;
-				Dbg("req.workingkey1[%s]", req.workingkey1);
-
-				// 读取加密密钥EDK
-				memset(keyBuf, 0, 64);
-				retKeyBuf = RvcGetKey(m_pkeys, EDK, keyBuf);
-				if (NULL == retKeyBuf)
-				{
-					LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_RVC_GET_KEY,
-						GetOutPutStr("%s%s%s%s", "RvcGetKey", retKeyBuf, "strErrMsg", (const char*)GetKMCLastErrMsg()).c_str());
-					return ERR_INITIALIZER_RVC_GET_KEY;
-				}
-
-				CSimpleStringA tempWorkingkey2(keyBuf, 32);
-				req.workingkey2 = tempWorkingkey2;
-				Dbg("req.workingkey2[%s]", req.workingkey2);
-
-				Dbg("load sm key to pinpad...");
-				PinPadService_ClientBase* pPinPad = new PinPadService_ClientBase(this);
-				DWORD rc = pPinPad->Connect();
-				if (rc == Error_Succeed)
-				{
-					PinPadService_LoadKeysSM_Ans ans = {};
-					rc = pPinPad->LoadKeysSM(req, ans, 30000);
-					if (rc == Error_Succeed)
-						Dbg("load sm key to pinpad succ");
-					else
-					{
-						LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_LOAD_KEYS_TO_PINPAD,
-							GetOutPutStr("%s%08x%s%s", "LoadKeys", rc, "strErrMsg", "加载SM密钥到PinPad失败").c_str());
-						rc = ERR_INITIALIZER_LOAD_KEYS_TO_PINPAD;
-					}
-
-					pPinPad->GetFunction()->CloseSession();
-				}
-				else
-				{
-					LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_CONNECT_PINPAD,
-						GetOutPutStr("%s%08x%s%s", "Connect", rc, "strErrMsg", "连接PinPad实体失败").c_str());
-					rc = ERR_INITIALIZER_CONNECT_PINPAD;
-				}
-				pPinPad->SafeDelete();
-
-				return rc;
-			}
-			else
-			{
-				retKeyBuf = ::RvcGetKey(m_pkeys, TMK, keyBuf);
-				if (NULL == retKeyBuf)
-				{
-					LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_RVC_GET_KEY,
-						GetOutPutStr("%s%s%s%s", "RvcGetKey", retKeyBuf, "strErrMsg", (const char*)GetKMCLastErrMsg()).c_str());
-					return ERR_INITIALIZER_RVC_GET_KEY;
-				}
-
-				PinPadService_LoadKeys_Req req = {};
-				req.initializeflag = true;
-				CSimpleStringA temp(m_cTDESTMK, 32);
-				req.masterkey = temp;
-				Dbg("req.masterkey[%s]", req.masterkey);
-
-				// 读取工作密钥TPK
-				memset(keyBuf, 0, 64);
-				retKeyBuf = RvcGetKey(m_pkeys, TPK, keyBuf);
-				if (NULL == retKeyBuf)
-				{
-					LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_RVC_GET_KEY,
-						GetOutPutStr("%s%s%s%s", "RvcGetKey", retKeyBuf, "strErrMsg", (const char*)GetKMCLastErrMsg()).c_str());
-					return ERR_INITIALIZER_RVC_GET_KEY;
-				}
-
-				CSimpleStringA temp1(keyBuf, 32);
-				req.workingkey1 = temp1;
-				Dbg("req.workingkey1[%s]", req.workingkey1);
-
-				// 读取加密密钥EDK
-				memset(keyBuf, 0, 64);
-				retKeyBuf = RvcGetKey(m_pkeys, EDK, keyBuf);
-				if (NULL == retKeyBuf)
-				{
-					LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_RVC_GET_KEY,
-						GetOutPutStr("%s%s%s%s", "RvcGetKey", retKeyBuf, "strErrMsg", (const char*)GetKMCLastErrMsg()).c_str());
-					return ERR_INITIALIZER_RVC_GET_KEY;
-				}
-
-				CSimpleStringA temp2(keyBuf, 32);
-				req.workingkey2 = temp2;
-				Dbg("req.workingkey2[%s]", req.workingkey2);
-
-				Dbg("load des key to pinpad...");
-				PinPadService_ClientBase* pPinPad = new PinPadService_ClientBase(this);
-				DWORD rc = pPinPad->Connect();
-				if (rc == Error_Succeed)
-				{
-					PinPadService_LoadKeys_Ans ans = {};
-					rc = pPinPad->LoadKeys(req, ans, 30000);
-					if (rc == Error_Succeed)
-						Dbg("load des key to pinpad succ");
-					else
-					{
-						LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_LOAD_KEYS_TO_PINPAD,
-							GetOutPutStr("%s%08x%s%s", "LoadKeys", rc, "strErrMsg", "加载des密钥到PinPad失败").c_str());
-						rc = ERR_INITIALIZER_LOAD_KEYS_TO_PINPAD;
-					}
-
-
-					pPinPad->GetFunction()->CloseSession();
-				}
-				else
-				{
-					LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_CONNECT_PINPAD,
-						GetOutPutStr("%s%08x%s%s", "Connect", rc, "strErrMsg", "连接PinPad实体失败").c_str());
-					rc = ERR_INITIALIZER_CONNECT_PINPAD;
-				}
-				pPinPad->SafeDelete();
-
-				return rc;
-			}
-		}
-		else
-		{
-			if (bSM)
-			{
-				int nRet = ::GetKey(keyBuf, &nKeyBufLen, chkBuf, &nChkBufLen, csTMK);
-				if (nRet != 0)
-				{
-					LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_GET_KEY,
-						GetOutPutStr("%s%s%s%s", "GetKey", retKeyBuf, "strErrMsg", (const char*)GetKMCLastErrMsg()).c_str());
-					return ERR_INITIALIZER_GET_KEY;
-				}
-
-				Dbg("get SM TMK succ, check: [%s]", ByteArrayToHexStr((BYTE*)chkBuf, nChkBufLen).c_str());
-
-				PinPadService_LoadKeysSM_Req req = {};
-				req.initializeflag = true;
-				req.smflag = 1;
-				req.masterkey = ByteArrayToHexStr((BYTE*)keyBuf, nKeyBufLen).c_str();
-
-				// 读取工作密钥TPK
-				nKeyBufLen = 64;
-				memset(keyBuf, 0, 64);
-
-				nChkBufLen = 16;
-				memset(chkBuf, 0, 16);
-
-				nRet = GetKey(keyBuf, &nKeyBufLen, chkBuf, &nChkBufLen, csTPK);
-				if (nRet != 0)
-				{
-					LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_GET_KEY,
-						GetOutPutStr("%s%s%s%s", "GetKey", retKeyBuf, "strErrMsg", (const char*)GetKMCLastErrMsg()).c_str());
-					return ERR_INITIALIZER_GET_KEY;
-				}
-
-				Dbg("get SM TPK succ, check: [%s]", ByteArrayToHexStr((BYTE*)chkBuf, nChkBufLen).c_str());
-				req.workingkey1 = ByteArrayToHexStr((BYTE*)keyBuf, nKeyBufLen).c_str();
-
-				// 读取加密密钥EDK
-				nKeyBufLen = 64;
-				memset(keyBuf, 0, 64);
-
-				nChkBufLen = 16;
-				memset(chkBuf, 0, 16);
-
-				nRet = GetKey(keyBuf, &nKeyBufLen, chkBuf, &nChkBufLen, csEDK);
-				if (nRet != 0)
-				{
-					LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_GET_KEY,
-						GetOutPutStr("%s%s%s%s", "GetKey", retKeyBuf, "strErrMsg", (const char*)GetKMCLastErrMsg()).c_str());
-					return ERR_INITIALIZER_GET_KEY;
-				}
-
-				Dbg("get SM EDK succ, check: [%s]", ByteArrayToHexStr((BYTE*)chkBuf, nChkBufLen).c_str());
-				req.workingkey2 = ByteArrayToHexStr((BYTE*)keyBuf, nKeyBufLen).c_str();
-
-				Dbg("load sm key to pinpad...");
-				PinPadService_ClientBase* pPinPad = new PinPadService_ClientBase(this);
-				DWORD rc = pPinPad->Connect();
-				if (rc == Error_Succeed)
-				{
-					PinPadService_LoadKeysSM_Ans ans = {};
-					rc = pPinPad->LoadKeysSM(req, ans, 30000);
-					if (rc == Error_Succeed)
-						Dbg("load sm key to pinpad succ");
-					else
-					{
-						LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_LOAD_KEYS_TO_PINPAD,
-							GetOutPutStr("%s%08x%s%s", "LoadKeys", rc, "strErrMsg", "加载des密钥到PinPad失败").c_str());
-						rc = ERR_INITIALIZER_LOAD_KEYS_TO_PINPAD;
-					}
-
-
-					pPinPad->GetFunction()->CloseSession();
-				}
-				else
-				{
-					LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_CONNECT_PINPAD,
-						GetOutPutStr("%s%08x%s%s", "Connect", rc, "strErrMsg", "连接PinPad实体失败").c_str());
-					rc = ERR_INITIALIZER_CONNECT_PINPAD;
-				}
-				pPinPad->SafeDelete();
-
-				return rc;
-			}
-			else
-			{
-				int nRet = ::GetKey(keyBuf, &nKeyBufLen, chkBuf, &nChkBufLen, cTMK);
-				if (nRet != 0)
-				{
-					LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_GET_KEY,
-						GetOutPutStr("%s%s%s%s", "GetKey", retKeyBuf, "strErrMsg", (const char*)GetKMCLastErrMsg()).c_str());
-					return ERR_INITIALIZER_GET_KEY;
-				}
-
-				Dbg("get des TMK succ, check: [%s]", ByteArrayToHexStr((BYTE*)chkBuf, nChkBufLen).c_str());
-
-				PinPadService_LoadKeys_Req req = {};
-				req.initializeflag = true;
-				req.masterkey = ByteArrayToHexStr((BYTE*)keyBuf, nKeyBufLen).c_str();
-
-				// 读取工作密钥TPK
-				nKeyBufLen = 64;
-				memset(keyBuf, 0, 64);
-
-				nChkBufLen = 16;
-				memset(chkBuf, 0, 16);
-
-				nRet = GetKey(keyBuf, &nKeyBufLen, chkBuf, &nChkBufLen, cTPK);
-				if (nRet != 0)
-				{
-					LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_GET_KEY,
-						GetOutPutStr("%s%s%s%s", "GetKey", retKeyBuf, "strErrMsg", (const char*)GetKMCLastErrMsg()).c_str());
-					return ERR_INITIALIZER_GET_KEY;
-				}
-
-				Dbg("get des TPK succ, check: [%s]", ByteArrayToHexStr((BYTE*)chkBuf, nChkBufLen).c_str());
-				req.workingkey1 = ByteArrayToHexStr((BYTE*)keyBuf, nKeyBufLen).c_str();
-
-				// 读取加密密钥EDK
-				nKeyBufLen = 64;
-				memset(keyBuf, 0, 64);
-
-				nChkBufLen = 16;
-				memset(chkBuf, 0, 16);
-
-				nRet = GetKey(keyBuf, &nKeyBufLen, chkBuf, &nChkBufLen, cEDK);
-				if (nRet != 0)
-				{
-					LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_GET_KEY,
-						GetOutPutStr("%s%s%s%s", "GetKey", retKeyBuf, "strErrMsg", (const char*)GetKMCLastErrMsg()).c_str());
-					return ERR_INITIALIZER_GET_KEY;
-				}
-
-				Dbg("get des EDK succ, check: [%s]", ByteArrayToHexStr((BYTE*)chkBuf, nChkBufLen).c_str());
-				req.workingkey2 = ByteArrayToHexStr((BYTE*)keyBuf, nKeyBufLen).c_str();
-
-				Dbg("load des key to pinpad...");
-				PinPadService_ClientBase* pPinPad = new PinPadService_ClientBase(this);
-				DWORD rc = pPinPad->Connect();
-				if (rc == Error_Succeed)
-				{
-					PinPadService_LoadKeys_Ans ans = {};
-					rc = pPinPad->LoadKeys(req, ans, 30000);
-					if (rc == Error_Succeed)
-						Dbg("load des key to pinpad succ");
-					else
-					{
-						LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_LOAD_KEYS_TO_PINPAD,
-							GetOutPutStr("%s%08x%s%s", "LoadKeys", rc, "strErrMsg", "加载des密钥到PinPad失败").c_str());
-						rc = ERR_INITIALIZER_LOAD_KEYS_TO_PINPAD;
-					}
-
-					pPinPad->GetFunction()->CloseSession();
-				}
-				else
-				{
-					LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_CONNECT_PINPAD,
-						GetOutPutStr("%s%08x%s%s", "Connect", rc, "strErrMsg", "连接PinPad实体失败").c_str());
-					rc = ERR_INITIALIZER_CONNECT_PINPAD;
-				}
-				pPinPad->SafeDelete();
-
-				return rc;
-			}
-		}
-	}
-	*/
 	LOG_FUNCTION();
 	Dbg("load sm key to pinpad...");
 	
@@ -755,7 +239,6 @@ DWORD CInitializerEntity::LoadKeysToPinPad(bool bSM)
 			LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_GET_KMC_KEY_NULL,
 				GetOutPutStr("%s%s", "m_bGetKMCKey", "False").c_str());
 			rc = ERR_INITIALIZER_GET_KMC_KEY_NULL;
-			//pPinPad->SafeDelete();
 			return rc;
 		}
 		PinPadService_LoadKeysSM_Ans ans = {};
@@ -777,89 +260,19 @@ DWORD CInitializerEntity::LoadKeysToPinPad(bool bSM)
 		LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_CONNECT_PINPAD,
 			GetOutPutStr("%s%08x%s%s", "Connect", rc, "strErrMsg", "连接PinPad实体失败").c_str());
 		rc = ERR_INITIALIZER_CONNECT_PINPAD;
+		pPinPad->SafeDelete();
 	}
-
-	//pPinPad->SafeDelete();
 	return rc;
 }
 
 ErrorCodeEnum CInitializerEntity::CreateInitMKData(CBlob &blob, bool bSM)
 {
-	/*
-	if (blob.m_iLength < 600)
-		blob.Alloc(600);
-
-	char TempKey[64] = {0};
-	memset(TempKey, 0, 64);
-	int TempKeyLen = 64;
-	
-	int nRet = 0;
-
-	if (HasCkCodeFlg())
-	{
-		if (bSM)
-			nRet = RvcCreateInitTMKReq(SM4, (char*)blob.m_pData, &blob.m_iLength);
-		else
-			nRet = RvcCreateInitTMKReq(DES, (char*)blob.m_pData, &blob.m_iLength);
-	}
-	else
-	{
-		if (bSM)
-			nRet = CreateInitSMTMKReq((char*)blob.m_pData, &blob.m_iLength);
-		else
-			nRet = CreateInitTMKReq((char*)blob.m_pData, &blob.m_iLength);
-	}
-
-	if (nRet != 0)
-	{
-		LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_CREATE_INIT_MK,
-				GetOutPutStr("%s%d","CreateInitTMK",nRet).c_str());
-		return Error_Unexpect;
-	}
-	*/
 	return Error_Succeed;
 }
 
 // 解析返回DES主密钥
 ErrorCodeEnum CInitializerEntity::ParseInitMKResult(CBlob &blob, bool bSM)
 {
-	/*
-	// 调用KMC解析返回
-	int nRet = 0;
-		
-	//Dbg("parse %s TMK data: [%s], len: %d", bSM ? "SM" : "DES", blob.m_pData, blob.m_iLength);
-	
-	int nKeyBufLen = 64;
-	memset(m_cTDESTMK, 0, 64);
-	memset(m_cSM2TMK, 0, 64);
-
-	if (HasCkCodeFlg())
-	{
-		if (bSM)
-		{
-			nRet = ::RvcParseTMKRes((char*)blob.m_pData, blob.m_iLength, SM4, m_cSM2TMK, &nKeyBufLen, &m_pkeys);
-		}
-		else
-		{
-			nRet = ::RvcParseTMKRes((char*)blob.m_pData, blob.m_iLength, DES, m_cTDESTMK, &nKeyBufLen, &m_pkeys);
-		}
-	}
-	else
-	{
-		if (bSM)
-			nRet = ::ParseSMTMKRes((char*)blob.m_pData, blob.m_iLength);
-		else
-			nRet = ::ParseTMKRes((char*)blob.m_pData, blob.m_iLength);
-	}
-	
-	if (nRet != 0)
-	{
-		LogError(Severity_Low, Error_Unexpect, ERROR_INITIALIZER_PARSEINITMKRES,  (const char*)CSimpleStringA::Format("parse %s TMK return data fail, GetKMCLastErrMsg[%s], return [%d]", 
-			bSM ? "SM" : "DES",
-			(const char*)GetKMCLastErrMsg(), nRet));
-		return Error_Unexpect;
-	}
-	*/
 	return Error_Succeed;
 }
 
@@ -887,8 +300,6 @@ bool CInitializerEntity::SetAuthAccessAddr(const char *pszAddr, int nPort)
 	// 保存准入服务地址
 	auto strAuthAccessAddr = CSimpleStringA::Format("%s %d", pszAddr, nPort);
 	bool bRet = true;
-	//bRet= !!WritePrivateProfileString("Initializer", "Server", strAuthAccessAddr, strPath);
-	//bRet = bRet && WritePrivateProfileString("AccessAuthorization", "Server", strAuthAccessAddr, strPath);
 	if(!inifile_format_write(strPath.GetData(), "AccessAuthorization", "Server","%s", strAuthAccessAddr.GetData())) bRet = false;
 	if (!inifile_format_write(strPath.GetData(), "Initializer", "Server", "%s", strAuthAccessAddr.GetData())) bRet = false;
 	return bRet;
@@ -1011,7 +422,7 @@ DWORD CInitializerEntity::InitCardSwiper()
 	{
 		LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_CONNECT_CARDSWIPER, GetOutPutStr("%s%s", "Connect", "False").c_str());
 		m_strLastErrMsg = "连接CardSwiper实体失败";
-		//pCardSwiperClient->SafeDelete();
+		pCardSwiperClient->SafeDelete();
 		pCardSwiperClient = NULL;
 		return Error_InvalidState;
 	}
@@ -1035,8 +446,6 @@ DWORD CInitializerEntity::InitCardSwiper()
 	}
 
 	pCardSwiperClient->GetFunction()->CloseSession();
-	//pCardSwiperClient->SafeDelete();
-	pCardSwiperClient = NULL;
 	return rc;
 }
 
@@ -1057,7 +466,7 @@ ErrorCodeEnum CInitializerEntity::DownloadCfg(const char *pszSettingServer, int
 	if (pCenterSettingClient->Connect() != Error_Succeed)
 	{
 		m_strLastErrMsg = "连接集中配置实体失败";
-		//pCenterSettingClient->SafeDelete();
+		pCenterSettingClient->SafeDelete();
 		pCenterSettingClient = NULL;
 		return Error_InvalidState;
 	}
@@ -1072,8 +481,6 @@ ErrorCodeEnum CInitializerEntity::DownloadCfg(const char *pszSettingServer, int
 		m_strLastErrMsg = "集中配置实体Download接口调用失败";
 
 	pCenterSettingClient->GetFunction()->CloseSession();
-	//pCenterSettingClient->SafeDelete();
-	pCenterSettingClient = NULL;
 	return rc;
 }
 
@@ -1145,6 +552,8 @@ void CInitializerEntity::BeginLogin(const char *pszAuthServer, int nAuthPort, co
 				//如果连接电子钥匙失败,以传入的参数进行密钥初始化
 				m_strUserID = pszUserID;
 				m_strPassword = pszPassword;
+				pClient->SafeDelete();
+				pClient = NULL;
 			}
 			else
 			{
@@ -1174,9 +583,6 @@ void CInitializerEntity::BeginLogin(const char *pszAuthServer, int nAuthPort, co
 					(const char*)m_strAuthorizer,
 					(const char*)m_strAuthTime);
 			}
-
-			//pClient->SafeDelete();
-			pClient = NULL;
 		}
 	}	
 
@@ -1872,6 +1278,8 @@ bool CInitializerEntity::HasPinPad()
 				m_ctx_blue->Answer(Error_Succeed);
 			}
 			Dbg("connect PinPad fail, assume no pinpad");
+			pPinPadClient->SafeDelete();
+			pPinPadClient = NULL;
 		}
 		else
 		{
@@ -1897,9 +1305,6 @@ bool CInitializerEntity::HasPinPad()
 
 			pPinPadClient->GetFunction()->CloseSession();
 		}
-
-		//pPinPadClient->SafeDelete();
-		pPinPadClient = NULL;
 		return bPinPadExist;
 	}
 	else
@@ -1927,6 +1332,8 @@ int CInitializerEntity::GetPinPadCapability()
 			m_ctx_blue->Ans.ErrMsg = "连接PinPad实体失败";
 			m_ctx_blue->Answer(Error_Succeed);
 		}
+		pPinPadClient->SafeDelete();
+		pPinPadClient = NULL;
 	}
 	else
 	{
@@ -1950,9 +1357,6 @@ int CInitializerEntity::GetPinPadCapability()
 		pPinPadClient->GetFunction()->CloseSession();
 	}
 
-	//pPinPadClient->SafeDelete();
-	pPinPadClient = NULL;
-
 	if (HasCkCodeFlg())
 	{
 		// 检查公钥缓存是否存在
@@ -2539,11 +1943,10 @@ int CInitializerEntity::GetPinPadIDAndDeviceID(CSimpleStringA &strPinPadID, CSim
 			m_ctx_blue->Ans.ErrMsg = "连接pinpad实体失败";
 			m_ctx_blue->Answer(Error_Succeed);
 		}
+		pPinPadClient->SafeDelete();
+		pPinPadClient = NULL;
 	}
 
-	//pPinPadClient->SafeDelete();
-	pPinPadClient = NULL;
-
 	if (bPinPadID)
 	{
 		if (bVendor)
@@ -2666,10 +2069,10 @@ bool CInitializerEntity::HasCkCodeFlg()
 				m_ctx_blue->Ans.ErrMsg = "连接PinPad实体失败";
 				m_ctx_blue->Answer(Error_Succeed);
 			}
-		}
 
-		//pPinPadClient->SafeDelete();
-		pPinPadClient = NULL;
+			pPinPadClient->SafeDelete();
+			pPinPadClient = NULL;
+		}
 
 		return m_bHasCkCode? true:false;
 	}

+ 13 - 154
Module/mod_accessauth/mod_AccessAuth.cpp

@@ -214,148 +214,11 @@ DWORD CAccessAuthEntity::InitKMC()
 // @nAlgFlag:  1:3des only; 2: sm4 only; 3: both 3des and sm4
 ErrorCodeEnum CAccessAuthEntity::GetKmcWKUpdateData(char *pBuf, int &nLen, int  nAlgFlag)
 {
-	/*
-	Dbg("CreateUpdateReq, algflag: %d", nAlgFlag);
-	CSimpleStringA strErrMsg;
-	CSmartPointer<IEntityFunction> spFunction = this->GetFunction();
-
-	if (HasCkCodeFlg())
-	{
-		//此处调用新接口,先调用pinpad接口生成mk校验码,并把校验码当成参数传入
-		CAutoArray<CSimpleStringA> TmkChk;
-		TmkChk.Init(1);
-		Dbg("get Tmk check code from pinpad...");
-		PinPadService_ClientBase *pPinPad = new PinPadService_ClientBase(this);
-		auto rc = pPinPad->Connect();
-		if (rc == Error_Succeed)
-		{
-			PinPadService_GetCheckCode_Req req;
-			PinPadService_GetCheckCode_Ans ans;
-			req.mSN.Init(1);
-			req.wSN.Init(1);
-			req.wSN[0] = 99;
-
-			if (1 == nAlgFlag)
-			{
-				req.mSN[0] = 0;	
-				rc = pPinPad->GetCheckCode(req,ans,10000);
-			}
-			else if (2 == nAlgFlag)
-			{
-				req.mSN[0] = 1;
-				rc = pPinPad->GetCheckCode(req,ans,10000);
-			}
-			else
-			{
-				//LogError(Severity_Low, rc, 0, "GetKmcWKUpdateData参数错误,nAlgFlag必须为0或1");
-				Dbg("GetKmcWKUpdateData参数错误,nAlgFlag必须为0或1");
-			}
-
-			if (rc == Error_Succeed)
-			{
-				TmkChk[0] = ans.checkcode[0];
-				Dbg("GetTmkCheck from pinpad succ, nAlgFlag[%d], Tmk:[%s]", nAlgFlag, ans.checkcode[0].GetData());
-			}
-			else
-			{
-				strErrMsg = "从PinPad获取主密钥校验码失败,请联系密码键盘厂商维护";
-				SetAuthErrMsg((const char*)strErrMsg);
-				spFunction->SetSysVar("AuthErrMsg", (const char*)strErrMsg, true);
-				//LogError(Severity_Low, rc, 0, "get tmk check code from pinpad fail");
-
-				LogWarn(Severity_Middle, Error_Unexpect, ERR_ACCESSAUTH_CONNECT_PINPAD,
-					GetOutPutStr("%s%08X%s%s", "GetCheckCode", rc, "AuthErrMsg", strErrMsg).c_str());
-			}
-
-			pPinPad->GetFunction()->CloseSession();
-		}
-		else
-		{
-			strErrMsg = "连接PinPad实体失败";
-			SetAuthErrMsg((const char*)strErrMsg);
-			spFunction->SetSysVar("AuthErrMsg", (const char*)strErrMsg, true);
-			//LogError(Severity_Low, rc, 0, "connect to pinpad entity fail");
-			LogWarn(Severity_Middle, Error_Unexpect, ERR_ACCESSAUTH_CONNECT_PINPAD,
-				GetOutPutStr("%s%s%s%s", "Connect", "False", "AuthErrMsg", strErrMsg).c_str());
-		}
-		pPinPad->SafeDelete();
-
-		CSimpleStringA strTmk = TmkChk[0].SubString(0,16);
-		int nRet = RvcCreateUpdateReq((char*)strTmk.GetData(), nAlgFlag-1, pBuf, &nLen);
-		if (nRet != 0)
-		{
-			strErrMsg = CSimpleStringA::Format("调用KMC接口RvcCreateUpdateReq失败, 错误信息[%s], 返回值[%d]", (const char*)GetKMCLastErrMsg(), nRet);
-			//LogError(Severity_Low, Error_Unexpect, ERROR_ACCESSAUTH_CREATEUPDATEREQ, (const char*)strErrMsg);
-			SetAuthErrMsg((const char*)strErrMsg);
-			spFunction->SetSysVar("AuthErrMsg", (const char*)strErrMsg, true);
-			LogWarn(Severity_Middle, Error_Unexpect, ERR_ACCESSAUTH_CREATE_UPDATE_WK_REQ,
-				GetOutPutStr("%s%d%s%s", "RvcCreateUpdateReq", nRet, "AuthErrMsg", (const char*)GetKMCLastErrMsg()).c_str());
-			return Error_Unexpect;
-		}
-	}
-	else
-	{
-		int nRet = CreateUpdateReq(pBuf, &nLen, nAlgFlag);
-		if (nRet != 0)
-		{
-			strErrMsg = CSimpleStringA::Format("调用KMC接口CreateUpdateReq失败, 错误信息[%s], 返回值[%d]", (const char*)GetKMCLastErrMsg(), nRet);
-			//LogError(Severity_Low, Error_Unexpect, ERROR_ACCESSAUTH_CREATEUPDATEREQ, (const char*)strErrMsg);
-			SetAuthErrMsg((const char*)strErrMsg);
-			spFunction->SetSysVar("AuthErrMsg", (const char*)strErrMsg, true);
-			LogWarn(Severity_Middle, Error_Unexpect, ERR_ACCESSAUTH_CREATE_UPDATE_WK_REQ,
-				GetOutPutStr("%s%d%s%s", "CreateUpdateReq", nRet, "AuthErrMsg", (const char*)GetKMCLastErrMsg()).c_str());
-			return Error_Unexpect;
-		}
-	}
-	*/
 	return Error_Succeed;
 }
 
 DWORD CAccessAuthEntity::ParseWKUpdateResult(char *pBuf, int nLen, int nAlgFlag)
 {
-	/*
-	// 调用KMC解析返回
-	Dbg("ParseUpdateRes: [%s], len: %d, alg: %d", pBuf, nLen, nAlgFlag);
-	CSimpleStringA strErrMsg;
-	CSmartPointer<IEntityFunction> spFunction = this->GetFunction();
-
-	int nRet = RvcParseUpdateRes(pBuf, nLen, nAlgFlag-1, &m_pkeys);
-	if (nRet != 0)
-	{
-		strErrMsg = CSimpleStringA::Format("调用KMC接口RvcParseUpdateRes失败, 错误信息[%s], 返回值[%d]", (const char*)GetKMCLastErrMsg(), nRet);
-		SetAuthErrMsg((const char*)strErrMsg);	
-		spFunction->SetSysVar("AuthErrMsg", (const char*)strErrMsg, true);
-
-		LogWarn(Severity_Middle, Error_Unexpect, ERR_ACCESSAUTH_PARSE_UPDATE_RES,
-			GetOutPutStr("%s%d%s%s", "RvcParseUpdateRes", nRet, "strErrMsg", (const char*)GetKMCLastErrMsg()).c_str());
-		return ERR_ACCESSAUTH_PARSE_UPDATE_RES;
-	}
-
-	char* pKeyBuf = RvcGetKey(m_pkeys, TMKCHK, pBuf);
-	if (NULL == pKeyBuf)
-	{
-		strErrMsg = CSimpleStringA::Format("调用KMC接口RvcGetKey(ParseWKUpdateResult)失败, 错误信息[%s], 返回值[%d]", (const char*)GetKMCLastErrMsg(), nRet);		
-		SetAuthErrMsg((const char*)strErrMsg);
-		spFunction->SetSysVar("AuthErrMsg", (const char*)strErrMsg, true);
-	
-		LogWarn(Severity_Middle, Error_Unexpect, ERR_ACCESSAUTH_GET_KEY,
-			GetOutPutStr("%s%s", "RvcGetKey", pKeyBuf).c_str());
-		return ERR_ACCESSAUTH_GET_KEY;
-	}
-
-	//int nCapability = GetPinPadCapability();
-	//if (nCapability == 1 || nCapability == 3)
-	//{
-	//	Dbg("print DES WK after update");
-	//	PrintAllKeys(false);
-	//}
-	//if (nCapability == 2 || nCapability == 3)
-	//{
-	//	Dbg("print SM WK after update");
-	//	PrintAllKeys(true);
-	//}
-	Dbg("parse wk update result succ");
-	*/
 	return Error_Succeed;
 }
 
@@ -473,7 +336,6 @@ DWORD CAccessAuthEntity::LoadPinPadWK(bool bSM)
 			LogWarn(Severity_Middle, Error_Unexpect, ERR_ACCESSAUTH_GET_KMC_NULL,
 				GetOutPutStr("%s%s", "m_bGetKMCKey", "False").c_str());
 			rc = ERR_ACCESSAUTH_GET_KMC_NULL;
-			//pPinPad->SafeDelete();
 			return rc;
 		}
 
@@ -486,9 +348,7 @@ DWORD CAccessAuthEntity::LoadPinPadWK(bool bSM)
 		Dbg("req.reserved3=%s", req.reserved3.GetData());
 
 		PinPadService_LoadKeysSM_Ans ans = {};
-		Dbg("1");
 		rc = pPinPad->LoadKeysSM(req, ans, 30000);
-		Dbg("2");
 		if (rc == Error_Succeed)
 			Dbg("load sm key to pinpad succ");
 		else
@@ -511,9 +371,10 @@ DWORD CAccessAuthEntity::LoadPinPadWK(bool bSM)
 		LogWarn(Severity_Middle, Error_Unexpect, ERR_ACCESSAUTH_CONNECT_PINPAD,
 			GetOutPutStr("%s%08x%s%s", "Connect", rc, "strErrMsg", strErrMsg).c_str());
 		rc = ERR_ACCESSAUTH_CONNECT_PINPAD;
+		//@test 没连接成功调用此接口释放
+		pPinPad->SafeDelete();
 	}
-    //@test 调此接口会挂
-	//pPinPad->SafeDelete();
+  
 	return rc;
 }
 
@@ -633,8 +494,8 @@ DWORD CAccessAuthEntity::EncryptDataWithPinPad(const CBlob &raw, CBlob &enc)
 		LogWarn(Severity_Middle, Error_Unexpect, ERR_ACCESSAUTH_CONNECT_PINPAD,
 			GetOutPutStr("%s%08x%s%s", "Connect", rc, "strErrMsg", strErrMsg).c_str());
 		rc = ERR_ACCESSAUTH_CONNECT_PINPAD;
+		pPinPad->SafeDelete();
 	}
-	//pPinPad->SafeDelete();
 	if (rc != Error_Succeed)
 		return rc;
 
@@ -699,6 +560,7 @@ DWORD CAccessAuthEntity::SaveSM2KeyPair(const CBlob &pubKey, const CBlob &priKey
 		rc = ERR_ACCESSAUTH_CONNECT_TOKEN_SERVICE;
 		LogWarn(Severity_Middle, Error_Unexpect, rc,
 			GetOutPutStr("%s%08X%s%s", "Connect", rc,"strErrMsg", strErrMsg).c_str());
+		pTokenServiceClient->SafeDelete();
 	}
 	else
 	{
@@ -721,7 +583,6 @@ DWORD CAccessAuthEntity::SaveSM2KeyPair(const CBlob &pubKey, const CBlob &priKey
 			Dbg("set sm2 key pair succ");
 	}
 
-	//pTokenServiceClient->SafeDelete();
 	return rc;
 }
 
@@ -741,6 +602,7 @@ ErrorCodeEnum CAccessAuthEntity::SaveTokenAndSharedSK(const CBlob &token, const
 
 		LogWarn(Severity_Middle, Error_Unexpect, ERR_ACCESSAUTH_CONNECT_TOKEN_SERVICE,
 			GetOutPutStr("%s%08X%s%s", "Connect", rc,"strErrMsg", (const char*)strErrMsg).c_str());
+		pTokenServiceClient->SafeDelete();
 	}
 	else
 	{
@@ -779,7 +641,6 @@ ErrorCodeEnum CAccessAuthEntity::SaveTokenAndSharedSK(const CBlob &token, const
 		pTokenServiceClient->GetFunction()->CloseSession();
 	}
 
-	//pTokenServiceClient->SafeDelete();
 	return rc;
 }
 
@@ -823,6 +684,8 @@ bool CAccessAuthEntity::HasPinPad()
 			Dbg("connect PinPad fail, assume no pinpad");
 			LogWarn(Severity_Middle, Error_Unexpect, ERR_ACCESSAUTH_CONNECT_PINPAD,
 				GetOutPutStr("%s%s", "Connect", "False").c_str());
+			pPinPadClient->SafeDelete();
+			pPinPadClient = NULL;
 		}
 		else
 		{
@@ -848,8 +711,6 @@ bool CAccessAuthEntity::HasPinPad()
 			pPinPadClient->GetFunction()->CloseSession();
 		}
 
-		//pPinPadClient->SafeDelete();
-		pPinPadClient = NULL;
 		return bPinPadExist;
 	}
 	else
@@ -898,8 +759,8 @@ int CAccessAuthEntity::GetPinPadCapability()
 	
 		LogWarn(Severity_Middle, Error_Unexpect, ERR_ACCESSAUTH_CONNECT_PINPAD,
 			GetOutPutStr("%s%08X%s%s", "Connect", rc,"AuthErrMsg", "连接PinPad实体失败").c_str());
+		pPinPad->SafeDelete();
 	}
-	//pPinPad->SafeDelete();
 
 	return nCapability;
 }
@@ -1407,11 +1268,10 @@ int CAccessAuthEntity::GetPinPadIDAndDeviceID(CSimpleStringA &strPinPadID, CSimp
 
 		LogWarn(Severity_Middle, Error_Unexpect, ERR_ACCESSAUTH_CONNECT_PINPAD,
 			GetOutPutStr("%s%08X%s%s", "Connect", rc, "strErrMsg", "连接PinPad实体失败").c_str());
+		pPinPadClient->SafeDelete();
+		pPinPadClient = NULL;
 	}
 
-	//pPinPadClient->SafeDelete();
-	pPinPadClient = NULL;
-
 	if (bPinPadID)
 	{
 		if (bVendor)
@@ -1533,11 +1393,10 @@ bool CAccessAuthEntity::HasCkCodeFlg()
 	
 		LogWarn(Severity_Middle, Error_Unexpect, ERR_ACCESSAUTH_CONNECT_PINPAD,
 			GetOutPutStr("%s%s", "strErrMsg", strErrMsg).c_str());
+		pPinPadClient->SafeDelete();
+		pPinPadClient = NULL;
 	}
 
-	//pPinPadClient->SafeDelete();
-	pPinPadClient = NULL;
-
 	return bCheckCode? true:false;
 }