mod_Initializer.cpp 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867
  1. #include "stdafx2.h"
  2. #ifdef RVC_OS_WIN
  3. #include "WMIDeviceQuery.h"
  4. #endif // RVC_OS_WIN
  5. #include "mod_Initializer.h"
  6. #include "DeviceBaseClass.h"
  7. #include "RVCComm.h"
  8. #include "EventCode.h"
  9. #include "iniutil.h"
  10. #include "PinPad_client_g.h"
  11. #include "comm.h"
  12. #include "fileutil.h"
  13. #include "SpUtility.h"
  14. #include "CommEntityUtil.hpp"
  15. using namespace PinPad;
  16. #include "CenterSetting_client_g.h"
  17. using namespace CenterSetting;
  18. #include "CardSwiper_client_g.h"
  19. using namespace CardSwiper;
  20. #include "MaintainWatcher_client_g.h"
  21. using namespace MaintainWatcher;
  22. #define BUF_SIZE 256
  23. #define KEY_SIZE 16
  24. void CInitializerSession::Handle_Initialize(SpOnewayCallContext<InitializerService_Initialize_Info>::Pointer ctx)
  25. {
  26. m_pEntity->StartInitialize(ctx->Info.strUserID, ctx->Info.strPassword);
  27. }
  28. void CInitializerSession::Handle_InitializeNew(SpReqAnsContext<InitializerService_InitializeNew_Req, InitializerService_InitializeNew_Ans>::Pointer ctx)
  29. {
  30. m_pEntity->m_ctx = ctx;
  31. m_pEntity->m_type = Normal;
  32. ErrorCodeEnum er = m_pEntity->StartInitializeNew(ctx->Req.strAuthServer.GetData(), ctx->Req.nAuthPort, ctx->Req.strUserID.GetData(), ctx->Req.strPassword.GetData());
  33. ctx->Ans.Errcode = m_pEntity->m_eErrNum;
  34. ctx->Ans.ErrMsg = m_pEntity->m_strLastErrMsg;
  35. }
  36. void CInitializerSession::Handle_GetAuthServerInfo(SpReqAnsContext<InitializerService_GetAuthServerInfo_Req, InitializerService_GetAuthServerInfo_Ans>::Pointer ctx)
  37. {
  38. if (m_pEntity->GetAuthAccessAddr(ctx->Ans.strIP, ctx->Ans.nPort))
  39. {
  40. ctx->Answer(Error_Succeed);
  41. }
  42. else
  43. {
  44. ctx->Answer(Error_Unexpect);
  45. }
  46. }
  47. void CInitializerSession::Handle_InitializeForBlueTooth(SpReqAnsContext<InitializerService_InitializeForBlueTooth_Req, InitializerService_InitializeForBlueTooth_Ans>::Pointer ctx) {
  48. LOG_FUNCTION();
  49. m_pEntity->m_ctx_blue = ctx;
  50. m_pEntity->m_type = (CardSwiperConnectType)ctx->Req.type;
  51. ErrorCodeEnum er = m_pEntity->StartInitializeNew(ctx->Req.strAuthServer.GetData(), ctx->Req.nAuthPort, ctx->Req.strUserID.GetData(), ctx->Req.strPassword.GetData());
  52. ctx->Ans.Errcode = m_pEntity->m_eErrNum;
  53. ctx->Ans.ErrMsg = m_pEntity->m_strLastErrMsg;
  54. }
  55. void CInitializerEntity::OnStarted()
  56. {
  57. LOG_FUNCTION();
  58. #ifdef RVC_OS_WIN
  59. CSimpleStringA strKmclogPath;
  60. GetFunction()->GetPath("Dbg", strKmclogPath);
  61. strKmclogPath += "\\kmc\\";
  62. if (!ExistsDirA(strKmclogPath))
  63. {
  64. CreateDirA(strKmclogPath, TRUE);
  65. }
  66. #endif // RVC_OS_WIN
  67. m_bHasCkCode = false;
  68. m_FSM.Init(this);
  69. }
  70. void CInitializerEntity::OnPaused()
  71. {
  72. CSimpleStringA strAuthServer;
  73. int nAuthPort = 0;
  74. if (GetAuthAccessAddr(strAuthServer, nAuthPort)) {
  75. Dbg("begin initialize on clicked PAUSE btn.");
  76. StartInitializeNew(strAuthServer, nAuthPort, "admin", "88888888");
  77. }
  78. else {
  79. Dbg("get IP or port failed.");
  80. }
  81. }
  82. void CInitializerEntity::OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
  83. {
  84. m_FSM.PostExitEvent();
  85. pTransactionContext->SendAnswer(Error_Succeed);
  86. }
  87. ErrorCodeEnum CInitializerEntity::StartInitialize(const char *pszUserID, const char *pszPassword)
  88. {
  89. if (!m_FSM.IsInitializeSucc())
  90. {
  91. FSMEvent *pEvent = new FSMEvent(CInitializerFSM::Event_ShowGUI);
  92. m_FSM.PostEventFIFO(pEvent);
  93. }
  94. else
  95. {
  96. Dbg("already init succeed");
  97. }
  98. return Error_Succeed;
  99. }
  100. ErrorCodeEnum CInitializerEntity::StartInitializeNew(const char *pszAuthServer, int nAuthPort, const char *pszUserID, const char *pszPassword)
  101. {
  102. LOG_FUNCTION();
  103. Dbg("pszUserID=%s at StartInitializeNew", pszUserID);
  104. Dbg("pszPassword len=%d at StartInitializeNew", strlen(pszPassword));
  105. m_strAuthServer = pszAuthServer;
  106. m_nAuthPort = nAuthPort;
  107. m_strUserID = pszUserID;
  108. m_strPassword = pszPassword;
  109. FSMEvent* pEvent = new FSMEvent(CInitializerFSM::Event_ShowNewGUI);
  110. m_FSM.PostEventFIFO(pEvent);
  111. return Error_Succeed;
  112. }
  113. // KMC初始化
  114. ErrorCodeEnum CInitializerEntity::InitKMC(int nPinPadCapability)
  115. {
  116. return Error_Succeed;
  117. }
  118. CSimpleStringA CInitializerEntity::GetKMCLastErrMsg()
  119. {
  120. return "";
  121. }
  122. ErrorCodeEnum CInitializerEntity::ReleaseKMC()
  123. {
  124. return Error_Succeed;
  125. }
  126. ErrorCodeEnum CInitializerEntity::CreateInitMKData(CBlob& blob, bool bSM)
  127. {
  128. return Error_Succeed;
  129. }
  130. // 解析返回DES主密钥
  131. ErrorCodeEnum CInitializerEntity::ParseInitMKResult(CBlob& blob, bool bSM)
  132. {
  133. return Error_Succeed;
  134. }
  135. // 将16进制字符串转成BYTE数据
  136. bool CInitializerEntity::HexStrToByteArray(const char* pHex, BYTE *pBuf, int *pBufLen)
  137. {
  138. int nHexLen = strlen(pHex);
  139. if (nHexLen %2 != 0)
  140. {
  141. printf("error hex string length\n");
  142. return false;
  143. }
  144. if (nHexLen /2 > *pBufLen)
  145. {
  146. printf("not enough buf length\n");
  147. return false;
  148. }
  149. for(int i=0; i<nHexLen; i++)
  150. {
  151. BYTE b =0;
  152. char ch1 = pHex[i];
  153. if (ch1 >='0' && ch1<='9')
  154. b = ch1 - '0';
  155. else if (ch1 >='A' && ch1 <='F')
  156. b = ch1 - 'A' + 10;
  157. else
  158. {
  159. printf("invalid hex string\n");
  160. return false;
  161. }
  162. if (i %2 ==0)
  163. {
  164. pBuf[i/2] = b;
  165. }
  166. else
  167. {
  168. pBuf[i/2] = pBuf[i/2] << 4 | b;
  169. }
  170. }
  171. *pBufLen = nHexLen / 2;
  172. return true;
  173. }
  174. string CInitializerEntity::ByteArrayToHexStr(BYTE *pBuf, int nBufLen)
  175. {
  176. char szBuf[1024];
  177. memset(szBuf, 0, sizeof(szBuf));
  178. for(int i=0; i<nBufLen; i++)
  179. {
  180. BYTE b1 = (pBuf[i] >> 4) & 0x0F;
  181. BYTE b2 = pBuf[i] & 0x0F;
  182. if (b1 <= 9)
  183. szBuf[i*2] = '0' + b1;
  184. else
  185. szBuf[i*2] = 'A' + b1 - 10;
  186. if (b2 <= 9)
  187. szBuf[i*2+1] = '0' + b2;
  188. else
  189. szBuf[i*2+1] = 'A' + b2 - 10;
  190. }
  191. return szBuf;
  192. }
  193. // 加载主密钥
  194. DWORD CInitializerEntity::LoadKeysToPinPad(bool bSM)
  195. {
  196. LOG_FUNCTION();
  197. Dbg("load sm key to pinpad...");
  198. PinPadService_ClientBase* pPinPad = new PinPadService_ClientBase(this);
  199. DWORD rc = pPinPad->Connect();
  200. if (rc == Error_Succeed)
  201. {
  202. PinPadService_LoadKeysSM_Req req = {};
  203. req.initializeflag = true;
  204. req.smflag = 1;
  205. if (m_bGetKMCKey) {
  206. Dbg("使用云接口获取的KMC密钥");
  207. req.masterkey = m_TMK.c_str();
  208. req.workingkey1 = m_TPK.c_str();
  209. req.workingkey2 = m_EDK.c_str();
  210. req.reserved3 = m_index.c_str();
  211. }
  212. else {
  213. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_GET_KMC_KEY_NULL,
  214. GetOutPutStr("%s%s", "m_bGetKMCKey", "False").c_str());
  215. rc = ERR_INITIALIZER_GET_KMC_KEY_NULL;
  216. pPinPad->GetFunction()->CloseSession();
  217. return rc;
  218. }
  219. PinPadService_LoadKeysSM_Ans ans = {};
  220. rc = pPinPad->LoadKeysSM(req, ans, 30000);
  221. if (rc == Error_Succeed)
  222. Dbg("load sm key to pinpad succ");
  223. else
  224. {
  225. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_LOAD_KEYS_TO_PINPAD,
  226. GetOutPutStr("%s%08x%s%s", "LoadKeysSM", rc, "strErrMsg", "加载SM密钥到PinPad失败").c_str());
  227. rc = ERR_INITIALIZER_LOAD_KEYS_TO_PINPAD;
  228. }
  229. pPinPad->GetFunction()->CloseSession();
  230. }
  231. else
  232. {
  233. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_CONNECT_PINPAD,
  234. GetOutPutStr("%s%08x%s%s", "Connect", rc, "strErrMsg", "连接PinPad实体失败").c_str());
  235. rc = ERR_INITIALIZER_CONNECT_PINPAD;
  236. pPinPad->SafeDelete();
  237. }
  238. return rc;
  239. }
  240. bool CInitializerEntity::GetAuthAccessAddr(CSimpleStringA &strAddr, int &nPort)
  241. {
  242. CSmartPointer<IConfigInfo> pConfig;
  243. auto rc = GetFunction()->OpenConfig(Config_CenterSetting, pConfig);
  244. assert(rc == Error_Succeed);
  245. CSimpleStringA strValue;
  246. rc = pConfig->ReadConfigValue("Initializer", "Server", strValue);
  247. assert(rc == Error_Succeed);
  248. rc = ParseIPAddress(strValue, strAddr, nPort);
  249. return rc == Error_Succeed;
  250. }
  251. bool CInitializerEntity::SetAuthAccessAddr(const char *pszAddr, int nPort)
  252. {
  253. CSimpleStringA strPath;
  254. // 根据场所获取集中配置文件
  255. GetFunction()->GetPath("CenterSetting", strPath);
  256. // 保存准入服务地址
  257. auto strAuthAccessAddr = CSimpleStringA::Format("%s %d", pszAddr, nPort);
  258. bool bRet = true;
  259. if(!inifile_format_write(strPath.GetData(), "AccessAuthorization", "Server","%s", strAuthAccessAddr.GetData())) bRet = false;
  260. if (!inifile_format_write(strPath.GetData(), "Initializer", "Server", "%s", strAuthAccessAddr.GetData())) bRet = false;
  261. return bRet;
  262. }
  263. bool CInitializerEntity::GetCenterSettingAddr(CSimpleStringA &strAddr, int &nPort)
  264. {
  265. CSmartPointer<IConfigInfo> pConfig;
  266. auto rc = GetFunction()->OpenConfig(Config_CenterSetting, pConfig);
  267. assert(rc == Error_Succeed);
  268. CSimpleStringA strValue;
  269. rc = pConfig->ReadConfigValue("CenterSetting", "Server", strValue);
  270. assert(rc == Error_Succeed);
  271. rc = ParseIPAddress(strValue, strAddr, nPort);
  272. return rc == Error_Succeed;
  273. }
  274. bool CInitializerEntity::SetCenterSettingAddr(const char *pszAddr, int nPort)
  275. {
  276. CSimpleStringA strPath;
  277. // 根据场所获取集中配置文件
  278. GetFunction()->GetPath("CenterSetting", strPath);
  279. auto strAuthAccessAddr = CSimpleStringA::Format("%s %d", pszAddr, nPort);
  280. return inifile_format_write(strPath.GetData(), "CenterSetting", "Server", "%s", strAuthAccessAddr.GetData());
  281. }
  282. bool CInitializerEntity::GetInitUkeyFlg(int &nUkeyFlg)
  283. {
  284. ErrorCodeEnum rc(Error_Succeed);
  285. #if defined(RVC_OS_WIN)
  286. CSmartPointer<IConfigInfo> pConfig;
  287. rc = GetFunction()->OpenConfig(Config_CenterSetting, pConfig);
  288. assert(rc == Error_Succeed);
  289. CSimpleStringA strValue;
  290. rc = pConfig->ReadConfigValueInt("Initializer", "SupportUkey", nUkeyFlg);
  291. Dbg("SupportUkey[%d]", nUkeyFlg);
  292. #else
  293. Dbg("Linux do not support ukey anymore!");
  294. nUkeyFlg = 0;
  295. #endif //RVC_OS_WIN
  296. return rc == Error_Succeed;
  297. }
  298. bool CInitializerEntity::GetInitializedFlg(int &nInitializedFlg)
  299. {
  300. CSimpleStringA strInitState;
  301. ErrorCodeEnum Error = GetFunction()->GetSysVar("InitState",strInitState);
  302. if (Error_NotExist == Error)
  303. {
  304. Dbg("strInitState is not exist, errcode=%d!", Error);
  305. return false;
  306. }
  307. else if (Error_Succeed != Error)
  308. {
  309. Dbg("Get strInitState failed, errcode=%d!", Error);
  310. return false;
  311. }
  312. Dbg("InitState[%s]", strInitState.GetData());
  313. if (strInitState.Compare("0", true) == 0)
  314. {
  315. nInitializedFlg = 0;
  316. }
  317. else if (strInitState.Compare("1", true) == 0)
  318. {
  319. Dbg("this machine has been initialized!", strInitState);
  320. nInitializedFlg = 1;
  321. }
  322. else
  323. {
  324. Dbg("InitState[%s] is wrong, must be 0 or 1!", strInitState);
  325. return false;
  326. }
  327. return true;
  328. }
  329. bool CInitializerEntity::SetInitializedFlg(CSimpleStringA strInitState)
  330. {
  331. ErrorCodeEnum Error = GetFunction()->SetSysVar("InitState",strInitState, false);
  332. if (Error_Succeed != Error)
  333. {
  334. Dbg("set strInitState failed, errcode=%d!", Error);
  335. return false;
  336. }
  337. return true;
  338. }
  339. bool CInitializerEntity::IsIL()
  340. {
  341. CSystemStaticInfo info;
  342. auto rc = GetFunction()->GetSystemStaticInfo(info);
  343. if (rc != Error_Succeed)
  344. {
  345. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_GET_SYSTEM_STATIC_INFO,
  346. GetOutPutStr("%s%08X", "GetSystemStaticInfo", rc).c_str());
  347. Dbg("GetSystemStaticInfo() fail: 0x%X", rc);
  348. return false;
  349. }
  350. Dbg("MachineType[%s]", info.strMachineType.GetData());
  351. if (info.strMachineType.IsStartWith("RVC.IL", true))
  352. {
  353. return true;
  354. }
  355. return false;
  356. }
  357. DWORD CInitializerEntity::InitCardSwiper()
  358. {
  359. // 调用刷卡器实体初始化
  360. auto pCardSwiperClient = new CardSwiperService_ClientBase(this);
  361. if (pCardSwiperClient->Connect() != Error_Succeed)
  362. {
  363. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_CONNECT_CARDSWIPER, GetOutPutStr("%s%s", "Connect", "False").c_str());
  364. m_strLastErrMsg = "连接CardSwiper实体失败";
  365. pCardSwiperClient->SafeDelete();
  366. pCardSwiperClient = NULL;
  367. return Error_InvalidState;
  368. }
  369. CardSwiperService_MagTransferInit_Req req = {};
  370. CardSwiperService_MagTransferInit_Ans ans = {};
  371. DWORD rc = pCardSwiperClient->MagTransferInit(req, ans, 15000);
  372. if (rc != Error_Succeed)
  373. {
  374. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_FROM_CARDSWIPER_TRASF_INIT, GetOutPutStr("%s%08X", "MagTransferInit", rc).c_str());
  375. m_strLastErrMsg = CSimpleStringA::Format("调用CardSwiper实体失败: 0x%x", rc);
  376. rc = ERR_INITIALIZER_FROM_CARDSWIPER_TRASF_INIT;
  377. }
  378. else if (ans.result != 0)
  379. {
  380. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_FROM_CARDSWIPER_NO_RET, GetOutPutStr("%s%08X%s%d", "MagTransferInit", rc,"result",ans.result).c_str());
  381. m_strLastErrMsg = CSimpleStringA::Format("调用CardSwiper实体返回失败: %d", ans.result);
  382. rc = ERR_INITIALIZER_FROM_CARDSWIPER_NO_RET;
  383. }
  384. pCardSwiperClient->GetFunction()->CloseSession();
  385. return rc;
  386. }
  387. ErrorCodeEnum CInitializerEntity::DownloadCfg(const char *pszSettingServer, int nSettingPort)
  388. {
  389. if (!IsServiceAvailable(pszSettingServer, nSettingPort))
  390. {
  391. m_strLastErrMsg = CSimpleStringA::Format("集中配置服务[%s:%d]无法连接", pszSettingServer, nSettingPort);
  392. return Error_Param;
  393. }
  394. // 保存到集中配置中
  395. SetCenterSettingAddr(pszSettingServer, nSettingPort);
  396. // 调用集中配置实体下载配置
  397. auto pCenterSettingClient = new CenterSettingService_ClientBase(this);
  398. if (pCenterSettingClient->Connect() != Error_Succeed)
  399. {
  400. m_strLastErrMsg = "连接集中配置实体失败";
  401. pCenterSettingClient->SafeDelete();
  402. pCenterSettingClient = NULL;
  403. return Error_InvalidState;
  404. }
  405. CenterSettingService_Download_Req req = {};
  406. req.strAddr = pszSettingServer;
  407. req.nPort = nSettingPort;
  408. CenterSettingService_Download_Ans ans = {};
  409. auto rc = pCenterSettingClient->Download(req, ans, 10000);
  410. if (rc != Error_Succeed)
  411. m_strLastErrMsg = "集中配置实体Download接口调用失败";
  412. pCenterSettingClient->GetFunction()->CloseSession();
  413. return rc;
  414. }
  415. void CInitializerEntity::BeginLogin(const char *pszAuthServer, int nAuthPort, const char *pszUserID, const char *pszPassword)
  416. {
  417. LOG_FUNCTION();
  418. DWORD rc = Error_Succeed;
  419. // 修改状态机
  420. FSMEvent *pEvent = new FSMEvent(CInitializerFSM::Event_StartLogin);
  421. m_FSM.PostEventFIFO(pEvent);
  422. CSimpleStringA strOldAuthServer;
  423. int nOldAuthPort = 0;
  424. // 判断用户是否改变准入服务地址
  425. if (GetAuthAccessAddr(strOldAuthServer, nOldAuthPort) &&
  426. (strOldAuthServer.Compare(pszAuthServer) != 0 || nAuthPort != nOldAuthPort))
  427. {
  428. if (!IsServiceAvailable(pszAuthServer, nAuthPort))
  429. {
  430. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_AUTH_SERVER,
  431. GetOutPutStr("%s%s%s%s%s%d", "IsServiceAvailable", "False", "pszAuthServer", pszAuthServer, "nAuthPort", nAuthPort).c_str());
  432. auto strErrMsg = CSimpleStringA::Format("准入服务[%s:%d]无法连接(error:%d), 请确认网络是否正常", pszAuthServer, nAuthPort, WSAGetLastError());
  433. EndLogin(ERR_INITIALIZER_AUTH_SERVER, strErrMsg);
  434. return;
  435. }
  436. // 保存到集中配置中
  437. SetAuthAccessAddr(pszAuthServer, nAuthPort);
  438. }
  439. int nUkeyFlg = 0;
  440. if (!GetInitUkeyFlg(nUkeyFlg))
  441. {
  442. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_GET_EKEY_FLG,
  443. GetOutPutStr("%s%s%s%d", "GetInitUkeyFlg", "False", "GetInitUkeyFlg", nUkeyFlg).c_str());
  444. auto strErrMsg = CSimpleStringA::Format("读出UkeyFlg配置失败, 请联系分行信息部确认集中配置");
  445. EndLogin(ERR_INITIALIZER_GET_EKEY_FLG, strErrMsg);
  446. return;
  447. }
  448. if (0 == nUkeyFlg)
  449. {
  450. m_strUserID = pszUserID;
  451. m_strPassword = pszPassword;
  452. }
  453. else
  454. {
  455. //获取电子钥匙持有人的用户ID
  456. MaintainCertificate_ClientBase *pClient = new MaintainCertificate_ClientBase(this);
  457. rc = pClient->Connect();
  458. if (rc != Error_Succeed)
  459. {
  460. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_CONNECT_MAINTAINWATCHER,
  461. GetOutPutStr("%s%08X%s%s", "Connect", rc , "Desc" ,"在初始化登录阶段连接MaintainWatcher实体失败了").c_str());
  462. EndLogin(ERR_INITIALIZER_CONNECT_MAINTAINWATCHER, "连接MaintainWatcher实体失败。");
  463. pClient->SafeDelete();
  464. return;
  465. }
  466. else
  467. {
  468. MaintainCertificate_GetUserInfo_Req req = {};
  469. MaintainCertificate_GetUserInfo_Ans ans = {};
  470. rc = pClient->GetUserInfo(req, ans, 10000);
  471. if (rc != Error_Succeed)
  472. {
  473. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_FROM_MAINTAINWATCHER_GETUSERINFO,
  474. GetOutPutStr("%s%08X%s%s", "GetUserInfo", rc ,"Desc", "在初始化登录阶段c从MaintainWatcher实体获取用户信息失败了").c_str());
  475. EndLogin(ERR_INITIALIZER_FROM_MAINTAINWATCHER_GETUSERINFO, "从MaintainWatcher实体获取用户信息失败。");
  476. //如果连接电子钥匙失败,以传入的参数进行密钥初始化
  477. m_strUserID = pszUserID;
  478. m_strPassword = pszPassword;
  479. pClient->GetFunction()->CloseSession();
  480. pClient = NULL;
  481. }
  482. else
  483. {
  484. // UserID=SP00000004;UserName=RVC0001;AuthorierID=SP00000001;AuthorizeTime=2014/8/13 18:46:29;RecommenderSAP=80274390;RecommenderOfficeID=274390;
  485. CSimpleStringA &str = ans.UserInfo;
  486. Dbg("get maintainer info: %s", (const char*)str);
  487. auto arr = str.Split(';');
  488. for (int i = 0; i < arr.GetCount(); i++)
  489. {
  490. auto arr2 = arr[i].Split('=');
  491. if (arr2.GetCount() != 2)
  492. continue;
  493. if (arr2[0] == "UserID")
  494. m_strUserID = arr2[1];
  495. else if (arr2[0] == "AuthorierID")
  496. m_strAuthorizer = arr2[1];
  497. else if (arr2[0] == "AuthorizeTime")
  498. m_strAuthTime = arr2[1];
  499. }
  500. m_strPassword = "88888888";
  501. Dbg("UserID=%s;Authorizer=%s;AuthTime=%s",
  502. (const char*)m_strUserID,
  503. (const char*)m_strAuthorizer,
  504. (const char*)m_strAuthTime);
  505. }
  506. pClient->GetFunction()->CloseSession();
  507. }
  508. }
  509. // 开始连接
  510. rc = SecureClientConnect();
  511. if (rc != Error_Succeed)
  512. {
  513. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_NET_CONNECT,
  514. GetOutPutStr("%s%08X", "SecureClientConnect", rc).c_str());
  515. EndLogin(ERR_INITIALIZER_NET_CONNECT, "连接准入服务失败, 请确认网络是否正常");
  516. return;
  517. }
  518. m_strInitUserID = m_strUserID;
  519. // 设定初始化超时(10秒)
  520. GetFunction()->SetTimer(11, this, 10000);
  521. rc = m_pConnection->SendLoginPackage(m_strUserID, m_strPassword);
  522. if (rc != Error_Succeed)
  523. {
  524. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_LOGIN_SEND_PKG,
  525. GetOutPutStr("%s%08X", "SendLoginPackage", rc).c_str());
  526. EndLogin(ERR_INITIALIZER_LOGIN_SEND_PKG, "发送登录请求失败, 请确认网络是否正常");
  527. return;
  528. }
  529. Dbg("send login pkg succ");
  530. return;
  531. }
  532. void CInitializerEntity::AutoBeginLogin(const char *pszAuthServer, int nAuthPort)
  533. {
  534. DWORD rc = Error_Succeed;
  535. // 修改状态机
  536. FSMEvent *pEvent = new FSMEvent(CInitializerFSM::Event_StartLogin);
  537. m_FSM.PostEventFIFO(pEvent);
  538. // 判断用户是否改变准入服务地址
  539. CSimpleStringA strOldAuthServer;
  540. int nOldAuthPort = 0;
  541. if (GetAuthAccessAddr(strOldAuthServer, nOldAuthPort) &&
  542. (strOldAuthServer.Compare(pszAuthServer) != 0 || nAuthPort != nOldAuthPort))
  543. {
  544. if (!IsServiceAvailable(pszAuthServer, nAuthPort))
  545. {
  546. auto strErrMsg = CSimpleStringA::Format("准入服务[%s:%d]无法连接(error:%d), 请确认网络是否正常", pszAuthServer, nAuthPort, WSAGetLastError());
  547. EndLogin(ERR_INITIALIZER_AUTH_SERVER, strErrMsg);
  548. return;
  549. }
  550. // 保存到集中配置中
  551. SetAuthAccessAddr(pszAuthServer, nAuthPort);
  552. }
  553. m_strUserID = "admin";
  554. m_strPassword = "88888888";
  555. Dbg("m_strUserID=%s;m_strPassword=%s",
  556. m_strUserID.GetData(),
  557. m_strPassword.GetData());
  558. // 开始连接
  559. rc = SecureClientConnect();
  560. if (rc != Error_Succeed)
  561. {
  562. EndLogin(ERR_INITIALIZER_NET_CONNECT, "连接准入服务失败, 请确认网络是否正常");
  563. return;
  564. }
  565. m_strInitUserID = m_strUserID;
  566. // 设定初始化超时(10秒)
  567. GetFunction()->SetTimer(11, this, 10000);
  568. rc = m_pConnection->SendLoginPackage(m_strUserID, m_strPassword);
  569. if (rc != Error_Succeed)
  570. {
  571. EndLogin(ERR_INITIALIZER_LOGIN_SEND_PKG, "发送登录请求失败, 请确认网络是否正常");
  572. return;
  573. }
  574. Dbg("send login pkg succ");
  575. return;
  576. }
  577. void CInitializerEntity::EndLogin(DWORD rc, const char *pErrMsg)
  578. {
  579. LOG_FUNCTION();
  580. GetFunction()->KillTimer(11);
  581. // 通知到状态机
  582. FSMEvent *pEvent = new FSMEvent(CInitializerFSM::Event_LoginResult);
  583. pEvent->param1 = ((rc == Error_Succeed) ? 0 : 1);
  584. m_FSM.PostEventFIFO(pEvent);
  585. if (rc != Error_Succeed)
  586. {
  587. ReportInitMKState(m_strInitUserID, rc, pErrMsg);
  588. SecureClientRelease();
  589. LogError(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_LOGIN,
  590. GetOutPutStr("%s%08X%s%s", "EndLogin", rc, "pErrMsg", pErrMsg).c_str());
  591. GetFunction()->ShowFatalError(pErrMsg);
  592. m_eErrNum = rc;
  593. m_strLastErrMsg = pErrMsg;
  594. //判断是否为蓝牙多合一,登录失败,返回给上层
  595. if (m_type != Bluetooth && m_ctx != NULL)
  596. {
  597. //详细错误码,成功rc的值为Error_Succeed
  598. m_ctx->Ans.Errcode = rc;
  599. m_ctx->Ans.ErrMsg = m_strLastErrMsg;
  600. Dbg("$ErrMsg$:%s", m_ctx->Ans.ErrMsg.GetData());
  601. //成功返回Error_Succeed,详细错误由Ans.Errcode体现
  602. m_ctx->Answer(Error_Succeed);
  603. }
  604. //蓝牙多合一自动初始化
  605. else if(m_ctx_blue != NULL){
  606. m_ctx_blue->Ans.Errcode = rc;
  607. m_ctx_blue->Ans.ErrMsg = m_strLastErrMsg;
  608. m_ctx_blue->Answer(Error_Succeed);
  609. }
  610. }
  611. else
  612. {
  613. Dbg("Login successfully!");
  614. m_strLastErrMsg = "";
  615. BeginGetKMCKey();
  616. }
  617. }
  618. void CInitializerEntity::BeginSyncPK()
  619. {
  620. // 立刻同步公钥文件
  621. LOG_FUNCTION();
  622. auto rc = SecureClientConnect();
  623. if (rc != Error_Succeed)
  624. {
  625. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_NET_CONNECT,
  626. GetOutPutStr("%s%08X", "SecureClientConnect", rc).c_str());
  627. EndSyncPK(ERR_INITIALIZER_NET_CONNECT, "连接准入服务失败, 请确认网络是否正常");
  628. return;
  629. }
  630. assert(m_pConnection->IsConnectionOK());
  631. if (m_pConnection->SendSyncPubKeyPackage() != Error_Succeed)
  632. {
  633. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_SYNC_PUBKEY_SEND_PKG,
  634. GetOutPutStr("%s%08X", "SendSyncPubKeyPackage", Error_Unexpect).c_str());
  635. EndSyncPK(ERR_INITIALIZER_SYNC_PUBKEY_SEND_PKG, "发送同步公钥请求失败, 请联系分行技术部");
  636. return;
  637. }
  638. // 设定超时(10秒)
  639. GetFunction()->SetTimer(33, this, 10000);
  640. }
  641. void CInitializerEntity::BeginGetKMCKey()
  642. {
  643. // 立刻同步公钥文件
  644. LOG_FUNCTION();
  645. auto rc = SecureClientConnect();
  646. if (rc != Error_Succeed)
  647. {
  648. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_NET_CONNECT,
  649. GetOutPutStr("%s%08X", "SecureClientConnect", rc).c_str());
  650. return;
  651. }
  652. assert(m_pConnection->IsConnectionOK());
  653. CSystemStaticInfo si;
  654. GetFunction()->GetSystemStaticInfo(si);
  655. if (m_pConnection->SendGetKMCKey(si.strTerminalID.GetData()) != Error_Succeed)
  656. {
  657. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_GET_KMC_KEY_SEND_PKG,
  658. GetOutPutStr("%s%08X", "SendGetKMCKey", Error_Unexpect).c_str());
  659. return;
  660. }
  661. // 设定超时(10秒)
  662. GetFunction()->SetTimer(33, this, 10000);
  663. }
  664. void CInitializerEntity::EndSyncPK(DWORD rc, const char *pszErrMsg)
  665. {
  666. GetFunction()->KillTimer(33);
  667. if (rc != Error_Succeed)
  668. {
  669. // 通知到状态机,同步公钥失败当做初始化主密钥失败状态
  670. FSMEvent *pEvent = new FSMEvent(CInitializerFSM::Event_InitMKResult);
  671. pEvent->param1 = 1;
  672. m_FSM.PostEventFIFO(pEvent);
  673. ReportInitMKState(m_strInitUserID, rc, pszErrMsg);
  674. SecureClientRelease();
  675. std::string errStr = SP::Utility::GBK2UTF8(std::string(pszErrMsg));
  676. LogError(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_SYNC_PUBKEY,
  677. GetOutPutStr("%s%08X%s%s", "EndSyncPK", rc, "pszErrMsg", errStr.c_str()).c_str());
  678. GetFunction()->ShowFatalError(errStr.c_str());
  679. m_eErrNum = rc;
  680. m_strLastErrMsg = pszErrMsg;
  681. //if (m_hGUIWindow == 0)
  682. {
  683. if (m_type == Normal) {
  684. m_ctx->Ans.Errcode = rc;
  685. m_ctx->Ans.ErrMsg = m_strLastErrMsg;
  686. m_ctx->Answer(Error_Succeed);
  687. }
  688. else if (m_type == Bluetooth) {
  689. m_ctx_blue->Ans.Errcode = rc;
  690. m_ctx_blue->Ans.ErrMsg = m_strLastErrMsg;
  691. m_ctx_blue->Answer(Error_Succeed);
  692. }
  693. }
  694. }
  695. else
  696. {
  697. Dbg("同步公钥成功");
  698. m_strLastErrMsg = "";
  699. BeginInitMK();
  700. }
  701. }
  702. void CInitializerEntity::EndGetKMCKey(DWORD rc, const char* pszErrMsg) {
  703. LOG_FUNCTION();
  704. GetFunction()->KillTimer(33);
  705. if (rc != Error_Succeed)
  706. {
  707. // 通知到状态机,获取KMC密钥失败当做初始化主密钥失败状态
  708. FSMEvent* pEvent = new FSMEvent(CInitializerFSM::Event_InitMKResult);
  709. pEvent->param1 = 1;
  710. m_FSM.PostEventFIFO(pEvent);
  711. ReportInitMKState(m_strInitUserID, rc, pszErrMsg);
  712. SecureClientRelease();
  713. std::string errStr = SP::Utility::GBK2UTF8(std::string(pszErrMsg));
  714. LogError(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_SYNC_PUBKEY,
  715. GetOutPutStr("%s%08X%s%s", "EndGetKMCKey", rc, "pszErrMsg", errStr.c_str()).c_str());
  716. GetFunction()->ShowFatalError(errStr.c_str());
  717. m_eErrNum = rc;
  718. m_strLastErrMsg = pszErrMsg;
  719. if (m_type == Normal && m_ctx != NULL) {
  720. m_ctx->Ans.Errcode = rc;
  721. m_ctx->Ans.ErrMsg = m_strLastErrMsg;
  722. m_ctx->Answer(Error_Succeed);
  723. }
  724. else if (m_type == Bluetooth && m_ctx_blue != NULL) {
  725. m_ctx_blue->Ans.Errcode = rc;
  726. m_ctx_blue->Ans.ErrMsg = m_strLastErrMsg;
  727. m_ctx_blue->Answer(Error_Succeed);
  728. }
  729. }
  730. else
  731. {
  732. Dbg("获取KMC密钥成功");
  733. m_strLastErrMsg = "";
  734. BeginInitMK();
  735. }
  736. }
  737. DWORD CInitializerEntity::ReportInitMKState(const char *pszUserID, DWORD dwErrCode, const char *pszErrMsg)
  738. {
  739. LOG_FUNCTION();
  740. auto rc = SecureClientConnect();
  741. if (rc != Error_Succeed)
  742. {
  743. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_NET_CONNECT,
  744. GetOutPutStr("%s%08X", "SecureClientConnect", rc).c_str());
  745. return rc;
  746. }
  747. assert(m_pConnection->IsConnectionOK());
  748. return m_pConnection->SendReportStatePackage("InitMK", pszUserID, dwErrCode, pszErrMsg);
  749. }
  750. void CInitializerEntity::BeginInitMK()
  751. {
  752. LOG_FUNCTION();
  753. // 开始连接
  754. DWORD rc = SecureClientConnect();
  755. if (rc != Error_Succeed)
  756. {
  757. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_NET_CONNECT,
  758. GetOutPutStr("%s%08X", "SecureClientConnect", rc).c_str());
  759. EndInitMK(ERR_INITIALIZER_NET_CONNECT, "连接准入服务失败,请确认网络是否正常!");
  760. return;
  761. }
  762. assert(m_pConnection != NULL && m_pConnection->IsConnectionOK());
  763. rc = m_pConnection->SendInitMKPackage();
  764. if (rc != Error_Succeed)
  765. {
  766. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_INIT_MK_SEND_PKG,
  767. GetOutPutStr("%s%08X", "SendInitMKPackage", rc).c_str());
  768. EndInitMK(ERR_INITIALIZER_INIT_MK_SEND_PKG, (const char*)CSimpleStringA::Format("发送MK初始化请求失败, %s", (const char*)m_strLastErrMsg));
  769. return;
  770. }
  771. Dbg("send InitMK req succ");
  772. // 设定初始化超时(30秒)
  773. GetFunction()->SetTimer(22, this, 30000);
  774. }
  775. void CInitializerEntity::EndInitMK(DWORD rc, const char *pszErrMsg)
  776. {
  777. LOG_FUNCTION();
  778. GetFunction()->KillTimer(22);
  779. m_strLastErrMsg = pszErrMsg;
  780. // 通知到中台
  781. ReportInitMKState(m_strInitUserID, rc, m_strLastErrMsg);
  782. if (rc != Error_Succeed)
  783. {
  784. LogError(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_INIT_MK,
  785. GetOutPutStr("%s%08X%s%s", "EndInitMK", rc,"pszErrMsg", pszErrMsg).c_str());
  786. GetFunction()->ShowFatalError(pszErrMsg);
  787. CSimpleStringA strErrMsg = CSimpleStringA::Format("初始化MK错误:%s", pszErrMsg);
  788. SP::Module::Comm::LogErrorNotiyStruct notifyItem(Error_Unexpect, ERR_INITIALIZER_SYNC_PUBKEY);
  789. SP::Module::Comm::LogNotiyMessageStruct notifyMsg(strErrMsg);
  790. notifyItem.Notify(notifyMsg);
  791. }
  792. else
  793. {
  794. Dbg("初始化成功");
  795. // 初始化刷卡器(只有配置外置PinPad的PAD终端才需要初始化)
  796. CSystemStaticInfo info;
  797. GetFunction()->GetSystemStaticInfo(info);
  798. if (rc != Error_Succeed) {
  799. LogWarn(Severity_Middle, (ErrorCodeEnum) rc, ERR_INITIALIZER_GET_SYSTEM_STATIC_INFO,
  800. GetOutPutStr("%s%08X", "GetSystemStaticInfo", rc).c_str());
  801. rc = ERR_INITIALIZER_FROM_CARDSWIPER;
  802. }
  803. else
  804. {
  805. if (stricmp(info.strMachineType, "RVC.Pad") == 0 && HasPinPad())
  806. {
  807. rc = InitCardSwiper();
  808. if (rc == Error_Succeed)
  809. Dbg("init card swiper succ");
  810. else
  811. {
  812. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_FROM_CARDSWIPER,
  813. GetOutPutStr("%s%08X", "InitCardSwiper", rc).c_str());
  814. //这里不能赋值,不然将覆盖具体错误码
  815. //rc = ERR_INITIALIZER_FROM_CARDSWIPER;
  816. }
  817. }
  818. }
  819. }
  820. m_eErrNum = rc;
  821. // 通知到状态机
  822. bool bSuc = rc == Error_Succeed;
  823. Dbg("m_afterSMIsFirst=%d,bSuc=%d", m_afterSMIsFirst, bSuc);
  824. if (m_afterSMIsFirst && bSuc)
  825. {
  826. //修改初始化状态
  827. Dbg("修改初始化完成标志位为1");
  828. SetInitializedFlg("1");
  829. //不重启应用的情况下
  830. m_afterSMIsFirst = false;
  831. }
  832. FSMEvent *pEvent = new FSMEvent(CInitializerFSM::Event_InitMKResult);
  833. pEvent->param1 = bSuc ? 0 : 1;
  834. m_FSM.PostEventFIFO(pEvent);
  835. int nInitializedFlg = 1;
  836. GetInitializedFlg(nInitializedFlg);
  837. if (IsIL() && 0 == nInitializedFlg && bSuc)
  838. {
  839. //修改初始化状态
  840. SetInitializedFlg("1");
  841. }
  842. else
  843. {
  844. if (m_type != Bluetooth && m_ctx != NULL) {
  845. m_ctx->Ans.Errcode = rc;
  846. m_ctx->Ans.ErrMsg = m_strLastErrMsg;
  847. m_ctx->Answer(Error_Succeed);
  848. }
  849. else if(m_ctx_blue != NULL) {
  850. m_ctx_blue->Ans.Errcode = rc;
  851. m_ctx_blue->Ans.ErrMsg = m_strLastErrMsg;
  852. m_ctx_blue->Answer(Error_Succeed);
  853. }
  854. }
  855. SecureClientRelease();
  856. }
  857. void CInitializerEntity::CloseTask()
  858. {
  859. LOG_FUNCTION();
  860. auto rc = m_pConnection->SendCloseTaskPackage(m_strUserID.GetData());
  861. if (rc != Error_Succeed)
  862. {
  863. Dbg("发送关闭任务请求失败");
  864. }
  865. Dbg("send close task pkg succ");
  866. }
  867. void CInitializerEntity::OnTimeout(DWORD dwTimerID)
  868. {
  869. if (dwTimerID == 11) // 登录超时
  870. {
  871. EndLogin(ERR_INITIALIZER_LOGIN_TIME_OUT, "登录超时");
  872. }
  873. else if (dwTimerID == 22) // 下载MK超时
  874. {
  875. EndInitMK(ERR_INITIALIZER_INIT_MK_TIME_OUT, "初始化主密钥超时");
  876. }
  877. else if (dwTimerID == 33) // 同步公钥超时
  878. {
  879. EndGetKMCKey(ERR_INITIALIZER_GET_KMC_KEY_TIME_OUT,"获取KMC密钥超时");
  880. }
  881. }
  882. ErrorCodeEnum CInitializerEntity::ParseIPAddress(const char *str, CSimpleStringA &ip, int &port)
  883. {
  884. ErrorCodeEnum Error = Error_Unexpect;
  885. if (str) {
  886. char tmp1[32] = {};
  887. char tmp2[16] = {};
  888. sscanf(str, "%s %s", tmp1, tmp2);
  889. if (tmp1[0] != 0 && tmp2[0] != 0)
  890. {
  891. ip = tmp1;
  892. port = atoi(tmp2);
  893. Error = Error_Succeed;
  894. }
  895. else
  896. return Error;
  897. }
  898. return Error;
  899. }
  900. ErrorCodeEnum CInitializerEntity::StartGUI()
  901. {
  902. return Error_Succeed;
  903. }
  904. ErrorCodeEnum CInitializerEntity::StartGUINew()
  905. {
  906. LOG_FUNCTION();
  907. int nInitializedFlg = 1;
  908. GetInitializedFlg(nInitializedFlg);
  909. if (IsIL() && 0 == nInitializedFlg)
  910. {
  911. AutoBeginLogin(m_strAuthServer.GetData(), m_nAuthPort);
  912. }
  913. else
  914. {
  915. CSimpleStringA tmpUserID = m_strUserID;
  916. CSimpleStringA tmpPswd = m_strPassword;
  917. Dbg("tmpUserID=%s at StartGUINew", tmpUserID.GetData());
  918. Dbg("tmpPswd=%s at StartGUINew", tmpPswd.GetData());
  919. BeginLogin(m_strAuthServer, m_nAuthPort, tmpUserID, tmpPswd);
  920. }
  921. return Error_Succeed;
  922. }
  923. ErrorCodeEnum CInitializerEntity::CloseGUI()
  924. {
  925. return Error_Succeed;
  926. }
  927. bool CInitializerEntity::IsServiceAvailable(const char* pszServiceAddr, int nServerPort)
  928. {
  929. if (pszServiceAddr == NULL || strlen(pszServiceAddr) <= 8 || nServerPort <= 0)
  930. return false;
  931. SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  932. if (s == INVALID_SOCKET)
  933. return false;
  934. sockaddr_in addr = { 0 };
  935. addr.sin_family = AF_INET;
  936. addr.sin_addr.s_addr = inet_addr(pszServiceAddr);
  937. addr.sin_port = htons(nServerPort);
  938. bool bRet = (connect(s, (SOCKADDR*)& addr, sizeof(addr)) != SOCKET_ERROR);
  939. closesocket(s);
  940. return bRet;
  941. }
  942. const char* CInitializerEntity::GetLastErrMsg()
  943. {
  944. return m_strLastErrMsg;
  945. }
  946. bool CInitializerEntity::HasPinPad()
  947. {
  948. CSystemStaticInfo info;
  949. auto rc = GetFunction()->GetSystemStaticInfo(info);
  950. if (rc != Error_Succeed)
  951. {
  952. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_GET_SYSTEM_STATIC_INFO,
  953. GetOutPutStr("%s%08X", "GetSystemStaticInfo", rc).c_str());
  954. }
  955. if (info.strMachineType.IsStartWith("RPM", true) || info.strMachineType.IsStartWith("RVC.CardStore", true) || info.strMachineType.IsStartWith("RVC.IL", true)) // 回单打印机、简化版
  956. return false;
  957. else if (stricmp(info.strMachineType, "RVC.PAD") == 0) // Pad机型
  958. {
  959. // 根据PinPad实体状态确定是否连接密码键盘
  960. bool bPinPadExist = false;
  961. auto pPinPadClient = new PinPadService_ClientBase(this);
  962. rc = pPinPadClient->Connect();
  963. if (rc != Error_Succeed)
  964. {
  965. if (m_type == Bluetooth) {
  966. m_ctx_blue->Ans.Errcode = ERR_INITIALIZER_CONNECT_PINPAD;
  967. m_ctx_blue->Ans.ErrMsg = "连接pinpad实体失败";
  968. m_ctx_blue->Answer(Error_Succeed);
  969. }
  970. Dbg("connect PinPad fail, assume no pinpad");
  971. pPinPadClient->SafeDelete();
  972. pPinPadClient = NULL;
  973. }
  974. else
  975. {
  976. PinPadService_GetDevInfo_Req req = {};
  977. PinPadService_GetDevInfo_Ans ans = {};
  978. auto rc = pPinPadClient->GetDevInfo(req, ans, 3000);
  979. if (rc != Error_Succeed)
  980. {
  981. Dbg("PinPad::GetDevInfo() fail: 0x%X", rc);
  982. if (m_type == Bluetooth)
  983. {
  984. m_ctx_blue->Ans.Errcode = ERR_INITIALIZER_FROM_PINPAD_GET_DEV_INFO;
  985. m_ctx_blue->Ans.ErrMsg = "pinpad not exist";
  986. m_ctx_blue->Answer(Error_Succeed);
  987. }
  988. }
  989. else
  990. {
  991. Dbg("PinPad::GetDevInfo() return state: %d", ans.state);
  992. bPinPadExist = ans.state != DEVICE_STATUS_NOT_READY;
  993. }
  994. pPinPadClient->GetFunction()->CloseSession();
  995. }
  996. return bPinPadExist;
  997. }
  998. else
  999. {
  1000. // 其它VTM机型,全部有内置密码键盘
  1001. return true;
  1002. }
  1003. }
  1004. // 1:3des only; 2: sm4 only; 3: both 3des and sm4
  1005. int CInitializerEntity::GetPinPadCapability()
  1006. {
  1007. auto pPinPadClient = new PinPadService_ClientBase(this);
  1008. int nCapability = 1;
  1009. DWORD rc = pPinPadClient->Connect();
  1010. if (rc != Error_Succeed)
  1011. {
  1012. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_CONNECT_PINPAD,
  1013. GetOutPutStr("%s%s", "Connect", "False").c_str());
  1014. rc = ERR_INITIALIZER_CONNECT_PINPAD;
  1015. if (m_type == Bluetooth)
  1016. {
  1017. m_ctx_blue->Ans.Errcode = rc;
  1018. m_ctx_blue->Ans.ErrMsg = "连接PinPad实体失败";
  1019. m_ctx_blue->Answer(Error_Succeed);
  1020. }
  1021. pPinPadClient->SafeDelete();
  1022. pPinPadClient = NULL;
  1023. }
  1024. else
  1025. {
  1026. PinPadService_QueryFunc_Req req = {};
  1027. PinPadService_QueryFunc_Ans ans = {};
  1028. rc = pPinPadClient->QueryFunc(req, ans, 3000);
  1029. if (rc != Error_Succeed)
  1030. {
  1031. LogWarn(Severity_Middle,(ErrorCodeEnum) rc, ERR_INITIALIZER_FROM_PINPAD_QUERY_FUNC,
  1032. GetOutPutStr("%s%08X%s%s", "QueryFunc", rc ,"Desc","调用pinpad实体的QueryFunc失败。。。").c_str());
  1033. }
  1034. else
  1035. {
  1036. // encryptkey 1: 3des only; 2: sm4 only; 3: both 3des and sm4
  1037. Dbg("PinPad::QueryFunc() return: %d", ans.encryptkey);
  1038. nCapability = ans.encryptkey;
  1039. }
  1040. pPinPadClient->GetFunction()->CloseSession();
  1041. }
  1042. if (HasCkCodeFlg())
  1043. {
  1044. // 检查公钥缓存是否存在
  1045. if (nCapability == 3 && (0 == m_iSM2Pukeylen))
  1046. {
  1047. Dbg("not SM2Pukeylen = 0, init des only");
  1048. nCapability = 1;
  1049. }
  1050. }
  1051. else
  1052. {
  1053. // 检查公钥文件是否存在
  1054. CSimpleStringA strKmcPath;
  1055. GetFunction()->GetPath("RunInfo", strKmcPath);
  1056. strKmcPath += "\\kmc\\";
  1057. CSimpleStringA strSm2PubKeyFile = strKmcPath + PUKFILENAME_SM2;
  1058. if (nCapability == 3 && !ExistsFileA(strSm2PubKeyFile))
  1059. {
  1060. Dbg("not find RVCSM2PubKey.ini, init des only");
  1061. nCapability = 1;
  1062. }
  1063. }
  1064. return nCapability;
  1065. }
  1066. ErrorCodeEnum CInitializerEntity::SecureClientConnect()
  1067. {
  1068. if (m_pConnection != NULL && m_pConnection->IsConnectionOK())
  1069. return Error_Succeed;
  1070. SecureClientRelease();
  1071. m_pConnection = new CInitializerConn(this);
  1072. if (m_pConnection->ConnectFromCentralSetting() && m_pConnection->IsConnectionOK())
  1073. return Error_Succeed;
  1074. SecureClientRelease();
  1075. return Error_PeerReject;
  1076. }
  1077. ErrorCodeEnum CInitializerEntity::SecureClientRelease()
  1078. {
  1079. if (m_pConnection != NULL)
  1080. {
  1081. m_pConnection->Close();
  1082. m_pConnection->DecRefCount();
  1083. m_pConnection = NULL;
  1084. }
  1085. return Error_Succeed;
  1086. }
  1087. void CInitializerEntity::OnSecureClientDisconnect()
  1088. {
  1089. m_FSM.PostEventFIFO(new FSMEvent(CInitializerFSM::Event_Disconnect));
  1090. }
  1091. bool CInitializerEntity::GetMD5Hash(const char *pStr, BYTE md5[16])
  1092. {
  1093. return Error_Succeed;
  1094. }
  1095. static char* ConvertBytesToHexStr(BYTE *pBuf, int nLen)
  1096. {
  1097. char *pRet = (char*)malloc(nLen * 2 + 1);
  1098. memset(pRet, 0, nLen * 2 + 1);
  1099. char *p = pRet;
  1100. for (int i = 0; i < nLen; i++)
  1101. {
  1102. BYTE b = pBuf[i];
  1103. BYTE l = (b >> 4) & 0x0F;
  1104. if (l >= 10)
  1105. *p = l - 10 + 'A';
  1106. else
  1107. *p = l + '0';
  1108. p++;
  1109. BYTE r = b & 0x0F;
  1110. if (r >= 10)
  1111. *p = r - 10 + 'A';
  1112. else
  1113. *p = r + '0';
  1114. p++;
  1115. }
  1116. return pRet;
  1117. }
  1118. bool CInitializerEntity::GetTerminalFingerPrint(BYTE *pBuf, int &nBufLen)
  1119. {
  1120. char szTmp[1024] = {0};
  1121. string strTmp;
  1122. int nTmpBufLen = 1024;
  1123. CSimpleStringA strRet;
  1124. #ifdef RVC_OS_WIN
  1125. if (!QueryWMIDevice(Processor, "ProcessorId", szTmp, &nTmpBufLen))
  1126. #else
  1127. CSimpleStringA runInfoPath;
  1128. auto rc = GetFunction()->GetPath("runinfo", runInfoPath);
  1129. if (rc != Error_Succeed) {
  1130. Dbg("GetPath runinfo error=%d.", rc);
  1131. return false;
  1132. }
  1133. runInfoPath += SPLIT_SLASH_STR "runcfg";
  1134. if(!get_cpu_id_by_system(strTmp, runInfoPath.GetData()))
  1135. #endif // RVC_OS_WIN
  1136. {
  1137. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_GET_PROCCESSOR_ID,
  1138. GetOutPutStr("%s%s", "Processor", "False").c_str());
  1139. m_strLastErrMsg = CSimpleStringA::Format("查询CPU ID失败,请重启机器并重新初始化");
  1140. return false;
  1141. }
  1142. #ifdef RVC_OS_WIN
  1143. strRet = szTmp;
  1144. nTmpBufLen = 1024;
  1145. memset(szTmp, 0, sizeof(szTmp));
  1146. if (!QueryWMIDevice(BaseBoard, "SerialNumber", szTmp, &nTmpBufLen))
  1147. #else
  1148. Dbg("cpu id: %s", strTmp.c_str());
  1149. strRet = strTmp.c_str();
  1150. strTmp.clear();
  1151. if(!get_board_serial_by_system(strTmp, runInfoPath.GetData()))
  1152. #endif // RVC_OS_WIN
  1153. {
  1154. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_GET_BASEBOARD_ID,
  1155. GetOutPutStr("%s%s", "BaseBoard", "False").c_str());
  1156. m_strLastErrMsg = CSimpleStringA::Format("查询主板序列号失败, 请重启机器并重新初始化");
  1157. return false;
  1158. }
  1159. strRet += "|";
  1160. #ifdef RVC_OS_WIN
  1161. strRet += szTmp;
  1162. nTmpBufLen = 1024;
  1163. memset(szTmp, 0, sizeof(szTmp));
  1164. if (!QueryWMIDevice(DiskDrive, "SerialNumber", szTmp, &nTmpBufLen))
  1165. #else
  1166. Dbg("baseboard sn: %s", strTmp.c_str());
  1167. strRet += strTmp.c_str();
  1168. vector<string> disk;
  1169. int errCode = 0;
  1170. if(!get_disk_serial_by_system(disk, errCode,runInfoPath.GetData()))
  1171. #endif // RVC_OS_WIN
  1172. {
  1173. Dbg("get_disk_serial_by_system errCode:%d", errCode);
  1174. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_GET_DISKDRIVE_ID,
  1175. GetOutPutStr("%s%s", "DiskDrive", "False").c_str());
  1176. m_strLastErrMsg = CSimpleStringA::Format("查询磁盘序列号失败, 请重启机器并重新初始化");
  1177. return false;
  1178. }
  1179. strRet += "|";
  1180. #ifdef RVC_OS_WIN
  1181. strRet += szTmp;
  1182. #else
  1183. strTmp = "";
  1184. vector<string>::iterator it = disk.begin();
  1185. while (it != disk.end()) {
  1186. strTmp += *it;
  1187. it++;
  1188. }
  1189. Dbg("harddisk sn: %s", strTmp.c_str());
  1190. strRet += strTmp.c_str();
  1191. #endif // RVC_OS_WIN
  1192. Dbg("device info: [%s]", (const char*)strRet);
  1193. // SM3 hash
  1194. BYTE sm3[32] = {};
  1195. if (!SM3Hash((BYTE*)strRet.GetData(),strRet.GetLength(), sm3))
  1196. {
  1197. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_GET_STR_HASH,
  1198. GetOutPutStr("%s%s", "GetMD5Hash", "False").c_str());
  1199. m_strLastErrMsg = "get md5 hash as fingerprint fail";
  1200. return false;
  1201. }
  1202. if (nBufLen < sizeof(sm3))
  1203. {
  1204. LogError(Severity_Low, Error_Unexpect, ERROR_ACCESSAUTH_GETSM3HASH, "GetTerminalFingerPrint, buf len is too small");
  1205. m_strLastErrMsg = "GetTerminalFingerPrint, buf len is too small";
  1206. Dbg("GetTerminalFingerPrint, buf len is too small.");
  1207. return false;
  1208. }
  1209. nBufLen = sizeof(sm3);
  1210. memcpy(pBuf, sm3, nBufLen);
  1211. char *pszSM3 = ConvertBytesToHexStr(sm3, nBufLen);
  1212. Dbg("fringerprint: [%s]", pszSM3);
  1213. free(pszSM3);
  1214. return true;
  1215. }
  1216. int CInitializerEntity::ConvertStr2Byte(string input, BYTE* output,int outputLen) {
  1217. if (input.size() > outputLen) return 1;
  1218. for (int i = 0;i < input.size();i++) {
  1219. output[i] =(BYTE) input[i];
  1220. }
  1221. return 0;
  1222. }
  1223. int CInitializerEntity::GetOrSetIsFirstSM(int type) {
  1224. LOG_FUNCTION();
  1225. CSmartPointer<IConfigInfo> pConfig;
  1226. int isFirst = 0;
  1227. auto rc = GetFunction()->OpenConfig(Config_Run, pConfig);
  1228. if (rc != Error_Succeed) {
  1229. Dbg("OpenConfig Config_Run error=%d.", rc);
  1230. return isFirst;
  1231. }
  1232. CSimpleStringA sIsFirst;
  1233. if (type == 0) {
  1234. rc = pConfig->ReadConfigValue("SM", "IsFirst", sIsFirst);
  1235. if (rc != Error_Succeed || sIsFirst.IsNullOrEmpty()) {
  1236. rc = pConfig->WriteConfigValue("SM", "IsFirst", "Yes");
  1237. if (rc != Error_Succeed) {
  1238. Dbg("WriteConfigValue Config_Run SM IsFirst error.");
  1239. return isFirst;
  1240. }
  1241. isFirst = 1;
  1242. }
  1243. else if (sIsFirst == "Yes") {
  1244. isFirst = 1;
  1245. }
  1246. else isFirst = 0;
  1247. return isFirst;
  1248. }
  1249. else {
  1250. rc = pConfig->WriteConfigValue("SM", "IsFirst", "No");
  1251. if (rc != Error_Succeed) {
  1252. Dbg("WriteConfigValue Config_Run SM IsFirst error.");
  1253. return isFirst;
  1254. }
  1255. else {
  1256. isFirst = 1;
  1257. }
  1258. return isFirst;
  1259. }
  1260. }
  1261. // 生成RSA密钥对,并导出公钥
  1262. bool CInitializerEntity::GetTerminalPublicKey(BYTE *pBuf, int &nBufLen)
  1263. {
  1264. LOG_FUNCTION();
  1265. CSimpleString runInfoPath;
  1266. auto rc = GetFunction()->GetPath("runinfo", runInfoPath);
  1267. if (rc != Error_Succeed) {
  1268. Dbg("GetPath runinfo error=%d.",rc);
  1269. return false;
  1270. }
  1271. runInfoPath += SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR;
  1272. /*if (!boost::filesystem::exists(runInfoPath.GetData())) {
  1273. boost::filesystem::create_directories(runInfoPath.GetData());
  1274. }*/
  1275. #ifdef RVC_OS_WIN
  1276. DWORD dwAttr = GetFileAttributes(runInfoPath.GetData());
  1277. if (dwAttr == 0xFFFFFFFF) //目录不存在则创建
  1278. {
  1279. if (!CreateDirectory(runInfoPath.GetData(), NULL))
  1280. #else
  1281. if(!dir_is_exist(runInfoPath.GetData()))
  1282. {
  1283. if(dir_create(runInfoPath.GetData()) != 0)
  1284. #endif
  1285. {
  1286. Dbg("Create %s dir failed!", runInfoPath.GetData());
  1287. }
  1288. }
  1289. Dbg("Dir=%s", runInfoPath.GetData());
  1290. //CSimpleString runFile = runInfoPath + GetEntityName();
  1291. CSmartPointer<IConfigInfo> pConfig;
  1292. rc = GetFunction()->OpenConfig(Config_Run, pConfig);
  1293. if (rc != Error_Succeed) {
  1294. Dbg("OpenConfig Config_Run error=%d.", rc);
  1295. return false;
  1296. }
  1297. CSimpleString publicKey;
  1298. rc = pConfig->ReadConfigValue("TerminalPD","PublicKey",publicKey);
  1299. if (rc != Error_Succeed || publicKey.IsNullOrEmpty()) {
  1300. Dbg("ReadConfig publicKey error=%d or publicKey is NULL.", rc);
  1301. BYTE btPublicKey[BUF_SIZE] = { 0 }, btPrivateKey[BUF_SIZE] = { 0 };
  1302. int iPublicKeyLen = sizeof(btPublicKey);
  1303. int iPrivateKeyLen = sizeof(btPrivateKey);
  1304. if (!CreateSM2KeyPair(btPublicKey, &iPublicKeyLen, btPrivateKey, &iPrivateKeyLen)) {
  1305. Dbg("Create SM2 key pair error.");
  1306. return false;
  1307. }
  1308. Dbg("iPublicKeyLen=%d,iPrivateKeyLen=%d", iPublicKeyLen, iPrivateKeyLen);
  1309. Dbg("[btPublicKey=%s]", (char*)btPublicKey);
  1310. char* pEncode = Str2Hex((char*)btPublicKey,iPublicKeyLen);
  1311. Dbg("pEncode=%s,%d", pEncode,strlen(pEncode));
  1312. //尝试解码
  1313. /*int pDecodeLen = 0;
  1314. char* pDecode = Hex2Str(pEncode,pDecodeLen);
  1315. Dbg("pDecode=%s", pDecode);
  1316. Dbg("pDecodeLen=%d", pDecodeLen);
  1317. char *ret = Str2Hex(pDecode, pDecodeLen);
  1318. Dbg("ret=%s,%d", ret, strlen(ret));
  1319. if (strcmp(ret, pEncode)!=0) {
  1320. Dbg("No");
  1321. return false;
  1322. }
  1323. Dbg("Yes");
  1324. delete[] pDecode;
  1325. delete[] ret;*/
  1326. rc = pConfig->WriteConfigValue("TerminalPD", "PublicKey", pEncode);
  1327. assert(rc == Error_Succeed);
  1328. Dbg("write public key success.");
  1329. BYTE pCryptPrivateKey[BUF_SIZE] = { 0 };
  1330. int cryptPrivateKeyLen = BUF_SIZE;
  1331. if (!EncWithSM4_ECB("s5da69gnh4!963@6s5da69gnh4!963@6", btPrivateKey, iPrivateKeyLen, pCryptPrivateKey, &cryptPrivateKeyLen)) {
  1332. Dbg("sm4 crypt privateKey error.");
  1333. rc = pConfig->WriteConfigValue("TerminalPD", "PublicKey", "");
  1334. delete[] pEncode;
  1335. return false;
  1336. }
  1337. Dbg("sm4 encrypt pri key success.");
  1338. Dbg("cryptPrivateKeyLen=%d", cryptPrivateKeyLen);
  1339. char* pEncodedCryptPrivateKey = Str2Hex((char*)pCryptPrivateKey, cryptPrivateKeyLen);
  1340. Dbg("encode pri key success.");
  1341. rc = pConfig->WriteConfigValue("TerminalPD", "PrivateKey", pEncodedCryptPrivateKey);
  1342. if (rc != Error_Succeed) {
  1343. rc = pConfig->WriteConfigValue("TerminalPD", "PublicKey", "");
  1344. delete[] pEncodedCryptPrivateKey;
  1345. return false;
  1346. }
  1347. Dbg("write pri key success.");
  1348. publicKey = pEncode;
  1349. delete[] pEncode;
  1350. delete[] pEncodedCryptPrivateKey;
  1351. }
  1352. Dbg("PublicKey: %s(%d)", publicKey.GetData(), publicKey.GetLength());
  1353. char *pDecode = Hex2Str(publicKey.GetData(),nBufLen);
  1354. memcpy(pBuf, pDecode, nBufLen);
  1355. Dbg("pBuf[0]=%02X,nBufLen=%d", pBuf[0], nBufLen);
  1356. return true;
  1357. }
  1358. // 返回1:只有PinPadID;2:只有DeviceID;3:两者都有;0:没有;-1表示失败
  1359. int CInitializerEntity::GetPinPadIDAndDeviceID(CSimpleStringA &strPinPadID, CSimpleStringA &strDeviceID)
  1360. {
  1361. CSystemStaticInfo info;
  1362. auto rc = GetFunction()->GetSystemStaticInfo(info);
  1363. if (rc != Error_Succeed)
  1364. {
  1365. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_GET_SYSTEM_STATIC_INFO,
  1366. GetOutPutStr("%s%08X", "GetSystemStaticInfo", rc).c_str());
  1367. return -1;
  1368. }
  1369. if (info.strMachineType.IsStartWith("RPM", true) || info.strMachineType.IsStartWith("RVC.CardStore", true) || info.strMachineType.IsStartWith("RVC.IL", true)) // 回单打印机、简化版
  1370. return 0;
  1371. int nRet = -1;
  1372. auto pPinPadClient = new PinPadService_ClientBase(this);
  1373. bool bPinPadID = false;
  1374. bool bDeviceID = false;
  1375. bool bVendor = false;
  1376. bool bBluetooth = false;
  1377. CSimpleStringA strBluetoothID;
  1378. CSimpleStringA strVendor;
  1379. CSimpleStringA strPID;
  1380. CSimpleStringA strMID;
  1381. rc = pPinPadClient->Connect();
  1382. if (rc == Error_Succeed)
  1383. {
  1384. PinPadService_GetDevInfo_Req req = {};
  1385. PinPadService_GetDevInfo_Ans ans = {};
  1386. auto rc1 = pPinPadClient->GetDevInfo(req, ans, 3000);
  1387. if (rc1 == Error_Succeed)
  1388. {
  1389. if (ans.state == DEVICE_STATUS_NORMAL)
  1390. {
  1391. nRet = 0;
  1392. Dbg("pinpad model: %s", (const char*)ans.model);
  1393. // CM = V2.0#PM = V1.0#MID = 75500001#PID = 12345678#FWID = V1234567#Vendor = nantian
  1394. // 密码键盘ID,PID,8到16字节; 设备ID,MID,8到16字节; 固件版本号,FWID,8字节
  1395. CSimpleStringA str = ans.model;
  1396. if (!str.IsNullOrEmpty())
  1397. {
  1398. auto arr = str.Split('#');
  1399. if (arr.GetCount() > 0)
  1400. {
  1401. for (int i = 0; i < arr.GetCount(); i++)
  1402. {
  1403. auto arr2 = arr[i].Split('=');
  1404. if (arr2.GetCount() != 2)
  1405. continue;
  1406. //if (arr2[0] == "PID")
  1407. if(!strnicmp((LPCTSTR)arr2[0], "PID", strlen("PID")))
  1408. {
  1409. strPID = arr2[1];
  1410. if (!strPID.IsNullOrEmpty())
  1411. bPinPadID = true;
  1412. }
  1413. //else if (arr2[0] == "MID")
  1414. else if(!strnicmp((LPCTSTR)arr2[0], "MID", strlen("MID")))
  1415. {
  1416. strMID = arr2[1];
  1417. if (!strMID.IsNullOrEmpty())
  1418. bDeviceID = true;
  1419. }
  1420. //else if (arr2[0] == "Vendor")
  1421. else if(!strnicmp((LPCTSTR)arr2[0], "Vendor", strlen("Vendor")))
  1422. {
  1423. strVendor = arr2[1];
  1424. if (!strVendor.IsNullOrEmpty())
  1425. bVendor = true;
  1426. }
  1427. else if (!strnicmp((LPCTSTR)arr2[0], "FWBID", strlen("FWBID")))
  1428. {
  1429. strBluetoothID = arr2[1];
  1430. Dbg("strBluetoothID=%s", strBluetoothID);
  1431. if (!strBluetoothID.IsNullOrEmpty())
  1432. bBluetooth = true;
  1433. }
  1434. }
  1435. }
  1436. }
  1437. }
  1438. else
  1439. {
  1440. if (m_type == Bluetooth)
  1441. {
  1442. m_ctx_blue->Ans.Errcode = ERR_INITIALIZER_FROM_PINPAD_NO_STATE;
  1443. m_ctx_blue->Ans.ErrMsg = "pinpad not exist";
  1444. m_ctx_blue->Answer(Error_Succeed);
  1445. }
  1446. Dbg("pinpad not exist, state: %d", ans.state);
  1447. }
  1448. }
  1449. else
  1450. {
  1451. LogWarn(Severity_Middle, rc1, ERR_INITIALIZER_FROM_PINPAD_GET_DEV_INFO,
  1452. GetOutPutStr("%s%08X%s%s", "GetDevInfo", rc, "strErrMsg", "PinPad::GetDevInfo() fail").c_str());
  1453. if (m_type == Bluetooth)
  1454. {
  1455. m_ctx_blue->Ans.Errcode = ERR_INITIALIZER_FROM_PINPAD_GET_DEV_INFO;
  1456. m_ctx_blue->Ans.ErrMsg = "pinpad not exist";
  1457. m_ctx_blue->Answer(Error_Succeed);
  1458. }
  1459. }
  1460. pPinPadClient->GetFunction()->CloseSession();
  1461. }
  1462. else
  1463. {
  1464. LogWarn(Severity_Middle, rc, ERR_INITIALIZER_CONNECT_PINPAD,
  1465. GetOutPutStr("%s%08X%s%s", "connect", rc,"strErrMsg","连接PinPad实体失败").c_str());
  1466. if (m_type == Bluetooth)
  1467. {
  1468. m_ctx_blue->Ans.Errcode = ERR_INITIALIZER_CONNECT_PINPAD;
  1469. m_ctx_blue->Ans.ErrMsg = "连接pinpad实体失败";
  1470. m_ctx_blue->Answer(Error_Succeed);
  1471. }
  1472. pPinPadClient->SafeDelete();
  1473. pPinPadClient = NULL;
  1474. }
  1475. if (bPinPadID)
  1476. {
  1477. if (bVendor)
  1478. strPinPadID = strVendor + "_" + strPID;
  1479. else
  1480. strPinPadID = strPID;
  1481. nRet += 1;
  1482. }
  1483. if (bDeviceID)
  1484. {
  1485. if (bVendor)
  1486. strDeviceID = strVendor + "_" + strMID;
  1487. else
  1488. strDeviceID = strMID;
  1489. if (bBluetooth)
  1490. strDeviceID = strDeviceID +"_" + strBluetoothID;
  1491. nRet += 2;
  1492. }
  1493. return nRet;
  1494. }
  1495. bool CInitializerEntity::HasCkCodeFlg()
  1496. {
  1497. if (!m_bHasCkCode)
  1498. {
  1499. CSystemStaticInfo info;
  1500. DWORD rc = GetFunction()->GetSystemStaticInfo(info);
  1501. if (rc != Error_Succeed)
  1502. {
  1503. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_GET_SYSTEM_STATIC_INFO,
  1504. GetOutPutStr("%s%08X", "GetSystemStaticInfo", rc).c_str());
  1505. return false;
  1506. }
  1507. if (info.strMachineType.IsStartWith("RPM", true) || info.strMachineType.IsStartWith("RVC.CardStore", true) || info.strMachineType.IsStartWith("RVC.IL", true)) // 回单打印机、简化版
  1508. return false;
  1509. auto pPinPadClient = new PinPadService_ClientBase(this);
  1510. bool bCheckCode = false;
  1511. CSimpleStringA strSpeficiCM;
  1512. rc = pPinPadClient->Connect();
  1513. if (rc == Error_Succeed)
  1514. {
  1515. PinPadService_GetDevInfo_Req req = {};
  1516. PinPadService_GetDevInfo_Ans ans = {};
  1517. rc = pPinPadClient->GetDevInfo(req, ans, 3000);
  1518. if (rc == Error_Succeed)
  1519. {
  1520. if (ans.state == DEVICE_STATUS_NORMAL)
  1521. {
  1522. Dbg("pinpad model: %s", (const char*)ans.model);
  1523. // CM = V2.0#PM = V1.0#MID = 75500001#PID = 12345678#FWID = V1234567#Vendor = nantian
  1524. // 加密通道的(磁条)版本号,CM; 密码加密的版本号,PM,是否支持国密; 设备ID,MID,8到16字节; 密码键盘ID,PID,8到16字节; 固件版本号,FWID,8字节
  1525. CSimpleStringA str = ans.model;
  1526. if (!str.IsNullOrEmpty())
  1527. {
  1528. auto arr = str.Split('#');
  1529. if (arr.GetCount() > 0)
  1530. {
  1531. for (int i = 0; i < arr.GetCount(); i++)
  1532. {
  1533. auto arr2 = arr[i].Split('=');
  1534. if (arr2.GetCount() != 2)
  1535. continue;
  1536. if(!strnicmp((LPCTSTR)arr2[0], "CM", strlen("CM")))
  1537. {
  1538. strSpeficiCM = arr2[1];
  1539. if (strSpeficiCM.GetLength() > 3 && _strnicmp(strSpeficiCM, "V2.0", strlen("V2.0")) == 0)
  1540. {
  1541. //Support checkcode, then operate checkcode routine..
  1542. m_bHasCkCode = true;
  1543. }
  1544. }
  1545. }
  1546. }
  1547. }
  1548. }
  1549. else
  1550. {
  1551. if (m_type == Bluetooth) {
  1552. m_ctx_blue->Ans.Errcode = ERR_INITIALIZER_FROM_PINPAD_NO_STATE;
  1553. m_ctx_blue->Ans.ErrMsg = "pinpad not exist";
  1554. m_ctx_blue->Answer(Error_Succeed);
  1555. }
  1556. Dbg("pinpad not exist, state: %d", ans.state);
  1557. }
  1558. }
  1559. else
  1560. {
  1561. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_FROM_PINPAD_GET_DEV_INFO,
  1562. GetOutPutStr("%s%08X", "GetDevInfo", rc).c_str());
  1563. //LogError(Severity_Middle, rc, ERROR_ACCESSAUTH_GETPINPADINFO, "PinPad::GetDevInfo() fail");
  1564. if (m_type == Bluetooth) {
  1565. m_ctx_blue->Ans.Errcode = rc;
  1566. m_ctx_blue->Ans.ErrMsg = "调用pinpad接口(GetDevInfo)失败";
  1567. m_ctx_blue->Answer(Error_Succeed);
  1568. }
  1569. }
  1570. pPinPadClient->GetFunction()->CloseSession();
  1571. }
  1572. else
  1573. {
  1574. LogWarn(Severity_Middle, Error_Unexpect, ERR_INITIALIZER_CONNECT_PINPAD,
  1575. GetOutPutStr("%s%s", "Connect", "False").c_str());
  1576. rc = ERR_INITIALIZER_CONNECT_PINPAD;
  1577. if (m_type == Bluetooth)
  1578. {
  1579. m_ctx_blue->Ans.Errcode = rc;
  1580. m_ctx_blue->Ans.ErrMsg = "连接PinPad实体失败";
  1581. m_ctx_blue->Answer(Error_Succeed);
  1582. }
  1583. pPinPadClient->SafeDelete();
  1584. pPinPadClient = NULL;
  1585. }
  1586. return m_bHasCkCode? true:false;
  1587. }
  1588. else
  1589. {
  1590. return true;
  1591. }
  1592. }
  1593. //判断是否国密改造后的第一次准入,如果是则自动初始化
  1594. bool CInitializerEntity::GetAccessauthIsFirst()
  1595. {
  1596. CSimpleString runInfoPath;
  1597. auto rc = GetFunction()->GetPath("runinfo", runInfoPath);
  1598. if (rc != Error_Succeed) {
  1599. Dbg("GetPath runinfo error=%d.", rc);
  1600. return false;
  1601. }
  1602. runInfoPath += SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR "AccessAuthorization.ini";
  1603. char* isFirst;
  1604. isFirst = inifile_read_str(runInfoPath.GetData(), "SM", "IsFirst", "");
  1605. Dbg("isFirst=%s,%d", isFirst, strlen(isFirst));
  1606. if (strlen(isFirst) <= 0) {
  1607. Dbg("没有读取到 IsFirst 字段");
  1608. if (isFirst != NULL) delete isFirst;
  1609. return true;
  1610. }
  1611. else if(stricmp(isFirst,"Yes")==0){
  1612. if (isFirst != NULL) delete isFirst;
  1613. return true;
  1614. }
  1615. else {
  1616. if (isFirst != NULL) delete isFirst;
  1617. return false;
  1618. }
  1619. if (isFirst != NULL) delete isFirst;
  1620. return true;
  1621. }
  1622. SP_BEGIN_ENTITY_MAP()
  1623. SP_ENTITY(CInitializerEntity)
  1624. SP_END_ENTITY_MAP()