CardAssist.cpp 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818
  1. #include "stdafx.h"
  2. #include <algorithm>
  3. //CSimpleStringA<T> ambigulous
  4. #ifdef RVC_OS_WIN
  5. #define _ATL_NO_AUTOMATIC_NAMESPACE
  6. #include <atltime.h>
  7. #endif // RVC_OS_WIN
  8. #include "CardAssist.h"
  9. #include "SpBase.h"
  10. char TTtmpxx[1024];
  11. char TtestIC[1024];
  12. bool compare_icdata (ICData d1,ICData d2)
  13. {
  14. unsigned int i1 = (d1.tag[0]<<8)+d1.tag[1];
  15. unsigned int i2 = (d2.tag[0]<<8)+d2.tag[1];
  16. return (i1 < i2);
  17. }
  18. bool IsSamePositionSet(LPBYTE p1,LPBYTE p2,int len)
  19. {
  20. int i = 0;
  21. while (i < len) {
  22. if ((*(p1 + i) & *(p2 + i)) != 0) {
  23. return true;
  24. }
  25. i++;
  26. }
  27. return false;
  28. }
  29. bool IsDoubleByteFlag(BYTE data)
  30. {
  31. if ((data&0x1f) == 0x1f)
  32. return true;
  33. else
  34. return false;
  35. }
  36. bool IsForm(LPBYTE data,int flagLen)
  37. {
  38. if (((*data)&0x20) == 0x20)
  39. return true;
  40. else
  41. return false;
  42. //if (flagLen == 1)
  43. //{
  44. // if (*data == 0x6f || *data == 0xa5 || *data == 0x70 || *data == 0x61
  45. // ||*data == 0x73)
  46. // return true;
  47. //}else if (flagLen == 2)
  48. //{
  49. // if ((*data == 0xbf && *(data+1) == 0x0c)
  50. // )
  51. // return true;
  52. //}
  53. //return false;
  54. }
  55. int SplitICData(vector<ICData>& vResult, LPBYTE* origData, int start, int dataLen, int level , int origDataLen)
  56. {
  57. //Dbg("to split IC Data size [%d],start[%d],dataLen[%d],level[%d]",vResult.size(),start,dataLen,level);
  58. //Dbg("%x%x",*((*origData)+start),*((*origData)+start+1));
  59. //ZeroMemory(Ttmpxx,sizeof(Ttmpxx));
  60. LPBYTE pData = *origData;
  61. DWORD len,tagLen,lenLen;
  62. len=tagLen=lenLen=0;
  63. int tmpDataStart = 0;
  64. int tagNum = 0;
  65. do
  66. {
  67. if (start >= origDataLen - 1)
  68. {
  69. Dbg("Wrong start %d,%d,%d", start, origDataLen,dataLen);
  70. return 0;
  71. }
  72. //Dbg("%x,%d,%d", pData[start],start,dataLen);//oiltest 20160107
  73. if (!IsDoubleByteFlag(pData[start]))
  74. {
  75. tagLen = 1;
  76. BYTE dd = DATALENFLAG;
  77. BYTE dd2 = pData[start+tagLen]&DATALENFLAG;
  78. if (DATALENFLAG == (pData[start+tagLen]&DATALENFLAG))
  79. {//长度为'8x'表示此字节表示长度所占的字节
  80. lenLen = pData[start+tagLen]&DATALENVALUE;
  81. len = 0;
  82. memcpy(&len, pData+start+tagLen+1,lenLen);
  83. lenLen++;//加上8x的长度
  84. }
  85. else
  86. {
  87. lenLen = 1;
  88. len = pData[start+1];
  89. //Dbg("single len[%x],[%d]",pData[start+1],len);
  90. }
  91. if (tmpDataStart+tagLen+lenLen+len <= dataLen)
  92. {
  93. if (IsForm(pData+start,1))
  94. {
  95. ICData tmpData(true,pData[start],0x00,level,len);
  96. vResult.push_back(tmpData);
  97. int tmpPos = vResult.size();
  98. int tmpNum = SplitICData(vResult, origData, start + tagLen + lenLen, len, level + 1, origDataLen);
  99. tagNum += tmpNum;
  100. vResult.at(tmpPos-1).num = tagNum;
  101. }
  102. else
  103. {
  104. tagNum++;
  105. //sprintf(Ttmpxx+jxx*2,"L%d",level);
  106. //jxx++;
  107. //char Ttmpxx[512];
  108. //for (int i = start + tagLen+lenLen; i < start + tagLen+lenLen+len; ++i,++jxx)
  109. //{
  110. // sprintf(Ttmpxx+jxx*2,"%02x",pData[i]);
  111. //}
  112. ICData tmpData(false,pData[start],0x00,level,len);
  113. tmpData.value = new BYTE[len];
  114. memcpy(tmpData.value,pData+start+tagLen+lenLen,len);
  115. vResult.push_back(tmpData);
  116. //start += tagLen+lenLen+len;
  117. //return true;
  118. }
  119. tmpDataStart += tagLen+lenLen+len;
  120. }
  121. }
  122. else
  123. {
  124. tagLen = 2;
  125. if (DATALENFLAG == (pData[start+tagLen]&DATALENFLAG))
  126. {//长度为'8x'表示此字节表示长度所占的字节
  127. lenLen = pData[start+tagLen]&DATALENVALUE;
  128. len = 0;
  129. memcpy(&len, pData+start+tagLen+1,lenLen);
  130. lenLen++;//加上8x的长度
  131. }
  132. else
  133. {
  134. lenLen = 1;
  135. len = pData[start+tagLen];
  136. //Dbg("single len[%x],[%d]",pData[start+tagLen],len);
  137. }
  138. if (tmpDataStart+tagLen+lenLen+len <= dataLen)
  139. {
  140. if (IsForm(pData+start,2))
  141. {
  142. ICData tmpData;
  143. tmpData.level = level;
  144. tmpData.bForm = true;
  145. tmpData.tag[0] = pData[start];
  146. tmpData.tag[1] = pData[start+1];
  147. tmpData.lenth = len;
  148. vResult.push_back(tmpData);
  149. int tmpPos = vResult.size();
  150. int tmpNum = SplitICData(vResult, origData, start + tagLen + lenLen, len, level + 1, origDataLen);
  151. tagNum += tmpNum;
  152. vResult.at(tmpPos-1).num = tagNum;
  153. }
  154. else
  155. {
  156. tagNum++;
  157. ICData tmpData;
  158. tmpData.level = level;
  159. tmpData.bForm = false;
  160. tmpData.tag[0] = pData[start];
  161. tmpData.tag[1] = pData[start+1];
  162. tmpData.lenth = len;
  163. tmpData.value = new BYTE[len];
  164. memcpy(tmpData.value,pData+start+tagLen+lenLen,len);
  165. vResult.push_back(tmpData);
  166. //char Ttmpxx[512];
  167. //sprintf(Ttmpxx+jxx*2,"L%d",level);
  168. //jxx++;
  169. //for (int i = start +tagLen+lenLen; i < start +tagLen+lenLen+len; ++i,++jxx)
  170. //{
  171. // sprintf(Ttmpxx+jxx*2,"%02x",pData[i]);
  172. //}
  173. //return true;
  174. }
  175. tmpDataStart += tagLen+lenLen+len;
  176. }
  177. }
  178. start += tagLen+lenLen+len;
  179. }while(tmpDataStart < dataLen);
  180. //Dbg("Ttmpxx[%s]",Ttmpxx);
  181. return tagNum;
  182. }
  183. int SplitFormString(vector<FormUnit> &vResult,const char *pData,int size,char fstP,char sndP)
  184. {
  185. //"ARPC,34A2BE5D78C32EE8|ARC,00|SCRIPT,32423424"
  186. int dataLen = strlen(pData);
  187. if (dataLen <= 0 || dataLen != size)
  188. {
  189. Dbg("Split form string failed.[%d,%s]",size,pData);
  190. return -1;
  191. }
  192. for (int i = 0,j = 0; i < size; ++i)
  193. {
  194. FormUnit formUnit;
  195. char ch = *(pData+i);
  196. if (ch != fstP)
  197. {
  198. int sndLen;
  199. if (i == size-1)
  200. {
  201. sndLen = i-j+1;
  202. formUnit.pSnd = new char[sndLen];
  203. ZeroMemory(formUnit.pSnd,sndLen);
  204. memcpy(formUnit.pSnd,pData+j+1,sndLen-1);
  205. formUnit.sndSize = sndLen-1;
  206. vResult.push_back(formUnit);
  207. break;
  208. }
  209. if (ch == sndP)
  210. {
  211. sndLen = i-j;
  212. formUnit.pSnd = new char[sndLen];
  213. ZeroMemory(formUnit.pSnd,sndLen);
  214. memcpy(formUnit.pSnd,pData+j+1,sndLen-1);
  215. formUnit.sndSize = sndLen-1;
  216. vResult.push_back(formUnit);
  217. j = i;
  218. }
  219. }
  220. else
  221. {
  222. int fstLen,fstStart;
  223. if (j == 0)
  224. {
  225. fstStart = j;
  226. fstLen = i-j+1;
  227. }
  228. else
  229. {
  230. fstStart = j+1;
  231. fstLen = i-j;
  232. }
  233. formUnit.pFst = new char[fstLen];
  234. ZeroMemory(formUnit.pFst,fstLen);
  235. memcpy(formUnit.pFst,pData+fstStart,fstLen-1);
  236. formUnit.fstSize = fstLen-1;
  237. j = i;
  238. }
  239. }
  240. return 0;
  241. }
  242. int char2int(char ch)
  243. {
  244. int ret = 0;
  245. if ('0' <= ch && ch <='9')
  246. ret = atoi(&ch);
  247. else
  248. {
  249. if ('a' <= ch && ch <= 'f')
  250. {
  251. ret = ch - 'a' + 10;
  252. }
  253. else if ('A' <= ch && ch <= 'F')
  254. {
  255. ret = ch - 'A' + 10;
  256. }
  257. }
  258. return ret;
  259. }
  260. CCardProcess::~CCardProcess()
  261. {
  262. }
  263. void CCardProcess::DataInit()
  264. {
  265. //m_cardType = CI_CARDTYPE_MAG;
  266. //data clear
  267. vector<ICData>::iterator itIC;
  268. for (itIC = m_vICData.begin(); itIC != m_vICData.end(); ++itIC)
  269. if (itIC->value != NULL)
  270. delete []itIC->value;
  271. m_vICData.clear();
  272. vector<ICData>::iterator itBus;
  273. for (itBus = m_vBusData.begin(); itBus != m_vBusData.end(); ++itBus)
  274. if (itBus->value != NULL)
  275. delete []itBus->value;
  276. m_vBusData.clear();
  277. vector<ADFRecord>::iterator itAdf;
  278. for (itAdf = m_vADFRec.begin(); itAdf != m_vADFRec.end(); ++itAdf)
  279. {
  280. if (itAdf->name != NULL)
  281. delete []itAdf->name;
  282. if (itAdf->appLabel != NULL)
  283. delete []itAdf->appLabel;
  284. if (itAdf->priName != NULL)
  285. delete []itAdf->priName;
  286. }
  287. m_vADFRec.clear();
  288. vector<FormUnit>::iterator itOnReply; m_vOnlineReplyData;
  289. for (itOnReply = m_vOnlineReplyData.begin(); itOnReply != m_vOnlineReplyData.end(); ++itOnReply)
  290. {
  291. if (itOnReply->pSnd != NULL)
  292. {
  293. delete []itOnReply->pSnd;
  294. itOnReply->pFst = NULL;
  295. itOnReply->pSnd = NULL;
  296. }
  297. }
  298. ZeroMemory(m_AIP,2);
  299. ZeroMemory(m_appVersion,2);
  300. ZeroMemory(m_TSI,2);
  301. ZeroMemory(m_TVR,5);
  302. ZeroMemory(m_IACReject,5);
  303. ZeroMemory(m_IACOnline,5);
  304. ZeroMemory(m_IACDefault,5);
  305. ZeroMemory(m_AuthCode,2);
  306. ZeroMemory(m_randData,4);
  307. ZeroMemory(m_CVR,4);
  308. if (m_pTACReject != NULL)
  309. {
  310. delete []m_pTACReject;
  311. m_pTACReject = NULL;
  312. }
  313. if (m_pIACOnline != NULL)
  314. {
  315. delete []m_pIACOnline;
  316. m_pIACOnline = NULL;
  317. }
  318. if (m_pTACOnline != NULL)
  319. {
  320. delete []m_pTACOnline;
  321. m_pTACOnline = NULL;
  322. }
  323. if (m_pIACDefault != NULL)
  324. {
  325. delete []m_pIACDefault;
  326. m_pIACDefault = NULL;
  327. }
  328. if (m_pTACDefault != NULL)
  329. {
  330. delete []m_pTACDefault;
  331. m_pTACDefault = NULL;
  332. }
  333. ZeroMemory(m_APDUsendBuf,MAX_SEND_BUF);
  334. //m_bCDA = false;
  335. }
  336. bool CCardProcess::DetectIfICCard(CardReadType eType, DeviceBaseClass *pCardX, int &cardType, bool bIssue)
  337. {
  338. Dbg("to detect card type.");
  339. cardType = 0;
  340. ErrorCodeEnum eErr;
  341. CSimpleStringA errMsg(true);
  342. if (eType == CARD_MACHINE_ISSUER)
  343. {
  344. pCardI = dynamic_cast<CardIssuerClass*>(pCardX);
  345. eErr = pCardI->ContactIC();
  346. if (eErr != Error_Succeed) {
  347. QueryLastErr(pCardI, errMsg);
  348. errMsg = CSimpleStringA::Format("DetectIfICCard::ContactIC failed(%s):%s", SpStrError(eErr), errMsg.GetData());
  349. LogWarn(Severity_Middle, Error_Unexpect, CardIssuer_UserErrorCode_ContactIC_Failed, errMsg.GetData());
  350. }
  351. eErr = pCardI->ActiveICCard();
  352. if (eErr != Error_Succeed)
  353. {
  354. QueryLastErr(pCardI, errMsg);
  355. errMsg = CSimpleStringA::Format("DetectIfICCard::ActiveICCard failed(%s):%s", SpStrError(eErr), errMsg.GetData());
  356. if (bIssue)
  357. LogWarn(Severity_Middle, Error_Unexpect, CardIssuer_UserErrorCode_IssueCard_ActiveICCard_Failed, errMsg.GetData());
  358. else
  359. LogWarn(Severity_Middle, Error_Unexpect, CardIssuer_UserErrorCode_ActiveICCard_Failed, errMsg.GetData());
  360. pCardI->DeactivateICCard();
  361. pCardI->ReleaseIC();
  362. return false;
  363. }
  364. }
  365. else if (eType == CARD_MACHINE_SWIPER || eType == CARD_MACHINE_SWIPER_RF)
  366. {
  367. pCardS = dynamic_cast<CardSwiperClass*>(pCardX);
  368. if (eType == CARD_MACHINE_SWIPER)
  369. {
  370. eErr = pCardS->ContactIC();
  371. eErr = pCardS->ActiveICCard();
  372. if (eErr != Error_Succeed)
  373. {
  374. Dbg("the card may be not IC card.");
  375. pCardS->DeactivateICCard();
  376. pCardS->ReleaseIC();
  377. return false;
  378. }
  379. }
  380. else if (eType == CARD_MACHINE_SWIPER_RF)
  381. {
  382. char chType;
  383. eErr = pCardS->ActiveContactlessICCard('A','B','M',chType);
  384. if (eErr != Error_Succeed)
  385. {
  386. Dbg("the card may be not contactless card.");
  387. //pCardS->DeactContactlessICCard();
  388. return false;
  389. }
  390. cardType = chType;
  391. Dbg("Detect CardSwiper(RF) card type %d", cardType);
  392. if (chType == 'A' || chType == 'B')
  393. return true;
  394. }
  395. }
  396. else if (eType == CARD_MACHINE_RFIC)
  397. {
  398. pCardR = dynamic_cast<RFICClass*>(pCardX);
  399. char ch;
  400. eErr = pCardR->ActiveContactlessICCard('A','B','M',ch);
  401. Dbg("Active card return %d",eErr);
  402. if (eErr != Error_Succeed)
  403. {
  404. QueryLastErr(pCardR, errMsg);
  405. errMsg = CSimpleStringA::Format("DetectIfICCard::ActiveContactlessICCard failed(%s):%s", SpStrError(eErr), errMsg.GetData());
  406. LogWarn(Severity_Middle, Error_Unexpect, ContactlessCard_UserErrorCode_ActiveContactlessICCard_Failed, errMsg.GetData());
  407. return false;
  408. }
  409. cardType = ch;
  410. Dbg("Detect contactless card type %d",ch);
  411. }
  412. else if (eType == CARD_MACHINE_ISSUER_RF)
  413. {
  414. pCardI = dynamic_cast<CardIssuerClass*>(pCardX);
  415. char ch;
  416. eErr = pCardI->ActiveContactlessICCard('A', 'B', 'M', ch);
  417. if (eErr != Error_Succeed)
  418. {
  419. QueryLastErr(pCardI, errMsg);
  420. errMsg = CSimpleStringA::Format("(CardIssuer)DetectIfICCard::ActiveContactlessICCard failed(%d):%s", SpStrError(eErr), errMsg.GetData());
  421. LogWarn(Severity_Middle, Error_Unexpect, ContactlessCard_UserErrorCode_ActiveContactlessICCard_Failed, errMsg.GetData());
  422. return false;
  423. }
  424. cardType = ch;
  425. Dbg(CSimpleStringA::Format("(CardIssuer)Detect contactless card type %d", ch));
  426. }
  427. return true;
  428. }
  429. ErrorCodeEnum CCardProcess::BuildSupportedAppList(CardReadType eType,DeviceBaseClass *pCardX,vector<AIDData>& vAIDFromTerm)
  430. {
  431. LOG_FUNCTION();
  432. //MessageBoxA(0,0,0,0);
  433. //首先用PSE选择应用
  434. BYTE zz = 0x00;
  435. BYTE cls = 0x00;
  436. BYTE ins = 0xa4;
  437. BYTE p1 = 0x04;
  438. BYTE p2 = 0x00;
  439. BYTE le = NULL;
  440. BYTE tmpPse1,tmpPse2;
  441. LPBYTE pData;
  442. char *pse1 = "1PAY.SYS.DDF01";
  443. char *pse2 = "2PAY.SYS.DDF01";//oiltmp 20160107 should change to "2PAY.SYS.DDF01"
  444. if (eType == CARD_MACHINE_ISSUER || eType == CARD_MACHINE_SWIPER)
  445. {
  446. tmpPse1 = strlen(pse1);
  447. pData = new BYTE[tmpPse1+1];
  448. ZeroMemory(pData, tmpPse1 + 1);
  449. memcpy(pData, pse1, tmpPse1);
  450. ConstructAPDU(cls, ins, p1, p2, tmpPse1, pData, &le);
  451. }
  452. else if (eType == CARD_MACHINE_RFIC || eType == CARD_MACHINE_SWIPER_RF || eType == CARD_MACHINE_ISSUER_RF)
  453. {
  454. tmpPse2 = strlen(pse2);
  455. pData = new BYTE[tmpPse2+1];
  456. ZeroMemory(pData, tmpPse2 + 1);
  457. memcpy(pData, pse2, tmpPse2);
  458. ConstructAPDU(cls, ins, p1, p2, tmpPse2, pData, &le);
  459. }
  460. else
  461. return Error_Param;
  462. //delete[] pData;//oiltest改成统一清理格式
  463. int lenRecv;
  464. LPBYTE recvBuf = new BYTE[1024];
  465. char* show = new char[MAX_TEST_SHOW];
  466. ZeroMemory(show,MAX_TEST_SHOW);
  467. //int rc = CPU_T0_C_APDU(m_hIDCCOM,m_lenAPDU,m_APDUsendBuf,recvBuf,&lenRecv);
  468. CmdInfo cmdSend,cmdRecv;
  469. cmdSend.dwSize = m_lenAPDU;
  470. memset(cmdSend.data,0,MAX_IC_BUFFER_SIZE);
  471. memcpy(cmdSend.data,m_APDUsendBuf,m_lenAPDU);
  472. CSimpleStringA errMsg(true);
  473. ErrorCodeEnum eErr = Error_Unexpect;
  474. DWORD errICCommand = 0;
  475. if (eType == CARD_MACHINE_ISSUER)
  476. {
  477. pCardI = dynamic_cast<CardIssuerClass*>(pCardX);
  478. eErr = pCardI->ICCommand(cmdSend,cmdRecv);
  479. errICCommand = CardIssuer_UserErrorCode_ICCommand_Failed;
  480. }
  481. else if (eType == CARD_MACHINE_ISSUER_RF)
  482. {
  483. pCardI = dynamic_cast<CardIssuerClass*>(pCardX);
  484. eErr = pCardI->RFTypeABCommand(cmdSend, cmdRecv);
  485. errICCommand = ContactlessCard_UserErrorCode_RFTypeABCommand_Failed;
  486. }
  487. else if (eType == CARD_MACHINE_SWIPER || eType == CARD_MACHINE_SWIPER_RF)
  488. {
  489. pCardS = dynamic_cast<CardSwiperClass*>(pCardX);
  490. if (eType == CARD_MACHINE_SWIPER){
  491. eErr = pCardS->ICCommand(cmdSend,cmdRecv);
  492. //errICCommand = CardSwiper_UserErrorCode_ICCommand_Failed;
  493. }else if (eType == CARD_MACHINE_SWIPER_RF){
  494. eErr = pCardS->RFTypeABCommand(cmdSend,cmdRecv);
  495. //errICCommand = CardSwiper_UserErrorCode_RFTypeABCommand_Failed;
  496. }
  497. }
  498. else if (eType == CARD_MACHINE_RFIC)
  499. {
  500. pCardR = dynamic_cast<RFICClass*>(pCardX);
  501. eErr = pCardR->RFTypeABCommand(cmdSend,cmdRecv);
  502. errICCommand = ContactlessCard_UserErrorCode_RFTypeABCommand_Failed;
  503. }
  504. memset(TtestIC,0,1024);
  505. int showLen = 0;
  506. TtestIC[showLen++] = 'a';
  507. if (eErr == Error_Succeed)
  508. TtestIC[showLen++] = (char)eErr+48;
  509. else
  510. TtestIC[showLen++] = (char)eErr;
  511. if (eErr == Error_Succeed)
  512. {
  513. memcpy(recvBuf,cmdRecv.data,cmdRecv.dwSize);
  514. lenRecv = cmdRecv.dwSize;
  515. TtestIC[showLen++] = 'b';
  516. Dbg("<BuildSupportedAppList>, recv %d len",lenRecv);
  517. if (recvBuf[lenRecv-2] == 0x90 && recvBuf[lenRecv-1] == 0x00)
  518. {
  519. HexBuf2StrBuf(recvBuf,&show,lenRecv);
  520. Dbg("<BuildSupportedAppList>, [%s]", show);
  521. TtestIC[showLen++] = 'c';
  522. TtestIC[showLen++] = 'c';
  523. memcpy(TtestIC+showLen,cmdRecv.data,cmdRecv.dwSize);
  524. vector<ICData> vPSEData;
  525. SplitICData(vPSEData, &recvBuf, 0, lenRecv - 2, 0, lenRecv - 2);
  526. //oilyang add to find aid 20160108
  527. ICData aidData(false, 0x4f, 0x00);
  528. if (ToFindTagValue(vPSEData, aidData, false, 0, vPSEData.size() - 1) != -1)
  529. {
  530. vAIDFromTerm.clear();
  531. AIDData aidFromCard;
  532. aidFromCard.aid = new BYTE[aidData.lenth + 1];
  533. memcpy(aidFromCard.aid, aidData.value, aidData.lenth);
  534. aidFromCard.len = aidData.lenth;
  535. aidFromCard.asiFlag = 1;
  536. vAIDFromTerm.push_back(aidFromCard);
  537. }
  538. LPBYTE pSFI = new BYTE[2];
  539. DWORD lenSFI;
  540. ICData testData(false,0x88,0x00,2);
  541. if (ToFindTagValue(vPSEData,testData,true,0,vPSEData.size()-1) != -1)
  542. {
  543. BYTE tmpSFI = testData.value[0];
  544. BYTE tmpP2 = (tmpSFI<<3)+0x04;
  545. delete[] testData.value;
  546. bool bNoMoreRec = false,bFirstRead = true;
  547. do
  548. {
  549. ConstructAPDU(0x00,0xb2,tmpSFI,tmpP2,NULL,NULL,&zz);
  550. //rc = CPU_T0_C_APDU(m_hIDCCOM,m_lenAPDU,m_APDUsendBuf,recvBuf,&lenRecv);
  551. cmdSend.dwSize = m_lenAPDU;
  552. memset(cmdSend.data,0,MAX_IC_BUFFER_SIZE);
  553. memcpy(cmdSend.data,m_APDUsendBuf,m_lenAPDU);
  554. if (eType == CARD_MACHINE_ISSUER)
  555. {
  556. eErr = pCardI->ICCommand(cmdSend,cmdRecv);
  557. errICCommand = CardIssuer_UserErrorCode_ICCommand_Failed;
  558. }
  559. else if (eType == CARD_MACHINE_ISSUER_RF)
  560. {
  561. eErr = pCardI->RFTypeABCommand(cmdSend, cmdRecv);
  562. errICCommand = ContactlessCard_UserErrorCode_RFTypeABCommand_Failed;
  563. }
  564. else if (eType == CARD_MACHINE_SWIPER || eType == CARD_MACHINE_SWIPER_RF)
  565. {
  566. eErr = pCardS->ICCommand(cmdSend,cmdRecv);
  567. //errICCommand = CardSwiper_UserErrorCode_ICCommand_Failed;
  568. }
  569. else if (eType == CARD_MACHINE_RFIC)
  570. {
  571. eErr = pCardR->RFTypeABCommand(cmdSend,cmdRecv);
  572. errICCommand = ContactlessCard_UserErrorCode_RFTypeABCommand_Failed;
  573. }
  574. else if (eType == CARD_MACHINE_SWIPER_RF)
  575. {
  576. eErr = pCardS->RFTypeABCommand(cmdSend,cmdRecv);
  577. //errICCommand = CardSwiper_UserErrorCode_ICCommand_Failed;
  578. }
  579. if (eErr == Error_Succeed)
  580. {
  581. memcpy(recvBuf,cmdRecv.data,cmdRecv.dwSize);
  582. lenRecv = cmdRecv.dwSize;
  583. HexBuf2StrBuf(recvBuf,&show,lenRecv);
  584. Dbg("<BuildSupportedAppList>show[%s]",show);
  585. if(recvBuf[lenRecv-2] == 0x6A && recvBuf[lenRecv-1] == 0x83)
  586. {
  587. bNoMoreRec = true;
  588. if (bFirstRead)
  589. {
  590. delete[] recvBuf;
  591. return BuildAppListByAIDs(eType,pCardX,vAIDFromTerm);
  592. }
  593. }
  594. else if(recvBuf[lenRecv-2] == 0x90 && recvBuf[lenRecv-1] == 0x00)
  595. {
  596. bFirstRead = false;
  597. vector<ICData> vDirRec;
  598. SplitICData(vDirRec, &recvBuf, 0, lenRecv - 2, 0, lenRecv - 2);
  599. ICData dirData(true,0x61,0x00,1);
  600. int start = 0;
  601. for (int ret = 0; start < vDirRec.size();)
  602. {
  603. //dirData.bForm = true;
  604. //dirData.tag[0] = 0x61;
  605. //dirData.tag[1] = 0x00;
  606. //dirData.level = 1;
  607. ret = ToFindTagValue(vDirRec,dirData,true,start,vDirRec.size()-1);
  608. if (ret == -1 )
  609. break;
  610. start += ret;
  611. if (start >= vDirRec.size()-1)
  612. break;
  613. if (vDirRec.at(start+1).tag[0] == 0x4F && vDirRec.at(start+1).tag[1] == 0x00)
  614. {
  615. ProcessADFRecord(vDirRec,start);
  616. }
  617. else if (vDirRec.at(start+1).tag[0] == 0x9D && vDirRec.at(start+1).tag[1] == 0x00)
  618. {
  619. //oiltmp
  620. ProcessDDFRecord(eType,pCardX,vAIDFromTerm,vDirRec.at(start+1).value,vDirRec.at(start+1).lenth);
  621. }
  622. start++;
  623. }
  624. tmpSFI++;
  625. continue;
  626. }
  627. else
  628. {
  629. delete[] recvBuf;
  630. return BuildAppListByAIDs(eType,pCardX,vAIDFromTerm);
  631. }
  632. }
  633. else
  634. {
  635. QueryLastErr(pCardX, errMsg);
  636. errMsg = CSimpleStringA::Format("BuildSupportedAppList::ICCommand 2 failed(%s):%s", SpStrError(eErr), errMsg.GetData());
  637. LogWarn(Severity_Middle, Error_Unexpect, errICCommand, errMsg.GetData());
  638. return Error_Interact;
  639. }
  640. }while(!bNoMoreRec);
  641. }
  642. }
  643. else {
  644. switch (eType)
  645. {
  646. case CARD_MACHINE_ISSUER:
  647. errICCommand = CardIssuer_UserErrorCode_ICCommand_RecvData_Invalid;
  648. break;
  649. case CARD_MACHINE_ISSUER_RF:
  650. errICCommand = ContactlessCard_UserErrorCode_ICCommand_RecvData_Invalid;
  651. break;
  652. case CARD_MACHINE_SWIPER:
  653. case CARD_MACHINE_SWIPER_RF:
  654. //errICCommand = CardSwiper_UserErrorCode_ICCommand_RecvData_Invalid;
  655. break;
  656. case CARD_MACHINE_RFIC:
  657. errICCommand = ContactlessCard_UserErrorCode_ICCommand_RecvData_Invalid;
  658. break;
  659. }
  660. if (recvBuf[lenRecv - 2] == 0x6a && recvBuf[lenRecv - 1] == 0x81)
  661. {
  662. //6A81 - 卡片被锁或命令不支持;
  663. //以上情况中止交易oiltmp
  664. LogWarn(Severity_Middle, Error_Unexpect, errICCommand, "<BuildSupportedAppList>, Build from PSE return 6a81,to term trans.");
  665. return Error_DevMedia;
  666. //return Error_Interact;//20220526@zjw 这里暂时按照需要循环重试来返回。这里实际应该是卡片数据问题,但部分厂商处理有问题,导致卡片时正常的,但快速拿走时,导致这里的数据异常
  667. }
  668. else
  669. {
  670. //6A82 - 所选的文件未找到;
  671. //——情形 1:PSE 未找到,即卡片不支持目录选择方法;
  672. //——情形 2:P2 设为读取具有相同 AID 前缀的其它应用时卡片中已没有其它应用。
  673. //6283 - 选择文件无效。
  674. errMsg = CSimpleStringA::Format("<BuildSupportedAppList>, Build from PSE failed. recv data:%x,%x", recvBuf[lenRecv - 2], recvBuf[lenRecv - 1]);
  675. LogWarn(Severity_Middle, Error_Unexpect, errICCommand, errMsg.GetData());
  676. delete[] recvBuf;
  677. return BuildAppListByAIDs(eType, pCardX, vAIDFromTerm);
  678. }
  679. }
  680. }
  681. else
  682. {
  683. QueryLastErr(pCardX, errMsg);
  684. errMsg = CSimpleStringA::Format("BuildSupportedAppList::ICCommand failed(%d):%s", eErr, errMsg.GetData());
  685. LogWarn(Severity_Middle, Error_Unexpect, errICCommand, errMsg.GetData());
  686. delete[] recvBuf;
  687. return Error_Interact;
  688. }
  689. return Error_Succeed;
  690. }
  691. ErrorCodeEnum CCardProcess::BuildAppListByAIDs(CardReadType eType,DeviceBaseClass *pCardX,vector<AIDData>& vAIDFromTerm)
  692. {
  693. LOG_FUNCTION();
  694. BYTE zz = 0x00;
  695. BYTE cls = 0x00;
  696. BYTE ins = 0xa4;
  697. BYTE p1 = 0x04;
  698. BYTE p2 = 0x00;
  699. BYTE le = NULL;
  700. vector<AIDData>::iterator it;
  701. CmdInfo cmdSend,cmdRecv;
  702. ErrorCodeEnum eErr = Error_Unexpect;
  703. if (eType == CARD_MACHINE_ISSUER || eType == CARD_MACHINE_ISSUER_RF)
  704. {
  705. pCardI = dynamic_cast<CardIssuerClass*>(pCardX);
  706. }
  707. else if (eType == CARD_MACHINE_SWIPER || eType == CARD_MACHINE_SWIPER_RF)
  708. {
  709. pCardS = dynamic_cast<CardSwiperClass*>(pCardX);
  710. }
  711. else if (eType == CARD_MACHINE_RFIC)
  712. {
  713. pCardR = dynamic_cast<RFICClass*>(pCardX);
  714. }
  715. for (it = vAIDFromTerm.begin(); it != vAIDFromTerm.end(); ++it)
  716. {
  717. BYTE* pData = new BYTE[it->len];
  718. if (pData == NULL)
  719. continue;
  720. memcpy(pData,it->aid,it->len);
  721. Step7:
  722. ConstructAPDU(cls,ins,p1,p2,it->len,pData,&le);
  723. bool bAddADF = false,b9000 = false,b6283 = false;
  724. int lenRecv;
  725. LPBYTE recvBuf = new BYTE[1024];
  726. char* show = new char[MAX_TEST_SHOW];
  727. ZeroMemory(show,MAX_TEST_SHOW);
  728. //int rc = CPU_T0_C_APDU(m_hIDCCOM,m_lenAPDU,m_APDUsendBuf,recvBuf,&lenRecv);
  729. cmdSend.dwSize = m_lenAPDU;
  730. memset(cmdSend.data,0,MAX_IC_BUFFER_SIZE);
  731. memcpy(cmdSend.data,m_APDUsendBuf,m_lenAPDU);
  732. if (eType == CARD_MACHINE_ISSUER)
  733. {
  734. eErr = pCardI->ICCommand(cmdSend,cmdRecv);
  735. }
  736. else if (eType == CARD_MACHINE_ISSUER_RF)
  737. {
  738. eErr = pCardI->RFTypeABCommand(cmdSend, cmdRecv);
  739. }
  740. else if (eType == CARD_MACHINE_SWIPER)
  741. {
  742. eErr = pCardS->ICCommand(cmdSend,cmdRecv);
  743. }
  744. else if (eType == CARD_MACHINE_RFIC)
  745. {
  746. eErr = pCardR->RFTypeABCommand(cmdSend,cmdRecv);
  747. }
  748. else if (eType == CARD_MACHINE_SWIPER_RF)
  749. {
  750. eErr = pCardS->RFTypeABCommand(cmdSend,cmdRecv);
  751. }
  752. if (eErr == 0)
  753. {
  754. memcpy(recvBuf,cmdRecv.data,cmdRecv.dwSize);
  755. lenRecv = cmdRecv.dwSize;
  756. Dbg("<BuildAppListByAIDs>, recv:%d,%x%x",lenRecv,recvBuf[lenRecv-2],recvBuf[lenRecv-1]);
  757. if (recvBuf[lenRecv-2] == 0x6a && recvBuf[lenRecv-1] == 0x81)
  758. return Error_DevMedia;//oiltmp 终止选择过程
  759. if (recvBuf[lenRecv-2] == 0x90 && recvBuf[lenRecv-1] == 0x00)
  760. b9000 = true;
  761. if (recvBuf[lenRecv-2] == 0x62 && recvBuf[lenRecv-1] == 0x83)
  762. b6283 = true;
  763. if (!b9000 && !b6283)
  764. {
  765. Dbg("<BuildAppListByAIDs>, not 9000 or 6283,failed.");
  766. return Error_DevMedia;
  767. }
  768. vector<ICData> vSelectedData;
  769. SplitICData(vSelectedData, &recvBuf, 0, lenRecv - 2, 0, lenRecv - 2);
  770. ICData dfData(false,0x84,0x00,2);
  771. if (ToFindTagValue(vSelectedData,dfData,true,0,vSelectedData.size()-1) == -1)
  772. continue;
  773. if (b9000 || b6283)
  774. {
  775. if (dfData.lenth < it->len)
  776. continue;
  777. if (memcmp(it->aid,dfData.value,it->len) == 0)
  778. {
  779. if (dfData.lenth > it->len)
  780. {
  781. if (it->asiFlag == 1)
  782. {
  783. p2 = 0x02;
  784. goto Step7;
  785. }
  786. else
  787. {
  788. if (!b9000)
  789. {
  790. p2 = 0x02;
  791. goto Step7;
  792. }
  793. else
  794. {
  795. p2 = 0x02;
  796. bAddADF = true;
  797. }
  798. }
  799. }
  800. else
  801. {
  802. if (b6283)
  803. continue;
  804. }
  805. }
  806. ICData data50(false,0x50,0x00),data9f12(false,0x9f,0x12),data87(false,0x87,0x00);
  807. ADFRecord adfRec;
  808. adfRec.name = new BYTE[dfData.lenth+1];
  809. memcpy(adfRec.name,dfData.value,dfData.lenth);
  810. adfRec.nameLen = dfData.lenth;
  811. if (dfData.value != NULL)
  812. delete[] dfData.value;
  813. HexBuf2StrBuf(adfRec.name,&show,adfRec.nameLen);
  814. if (ToFindTagValue(vSelectedData,data50,false,0,vSelectedData.size()-1) != -1)
  815. {
  816. adfRec.appLabel = new BYTE[data50.lenth+1];
  817. memcpy(adfRec.appLabel,data50.value,data50.lenth);
  818. adfRec.appLabelLen = data50.lenth;
  819. if (data50.value != NULL)
  820. delete[] data50.value;
  821. HexBuf2StrBuf(adfRec.appLabel,&show,adfRec.appLabelLen);
  822. }
  823. if (ToFindTagValue(vSelectedData,data9f12,false,0,vSelectedData.size()-1) != -1)
  824. {
  825. adfRec.priName = new BYTE[data9f12.lenth+1];
  826. memcpy(adfRec.priName,data9f12.value,data9f12.lenth);
  827. adfRec.priNameLen = data9f12.lenth;
  828. if(data9f12.value != NULL)
  829. delete[] data9f12.value;
  830. HexBuf2StrBuf(adfRec.priName,&show,adfRec.priNameLen);
  831. }
  832. if (ToFindTagValue(vSelectedData,data87,false,0,vSelectedData.size()-1) != -1)
  833. {
  834. adfRec.appPriID = *data87.value;
  835. adfRec.appPriIDLen = 1;
  836. }
  837. m_vADFRec.push_back(adfRec);
  838. if (bAddADF)
  839. goto Step7;
  840. }
  841. else
  842. continue;
  843. }
  844. else {
  845. CSimpleStringA errMsg("");
  846. QueryLastErr(pCardX, errMsg);
  847. Dbg("<BuildAppListByAIDs>, ICCommand failed with errCode:%d(0x%x), errMsg:%s", eErr, eErr, errMsg.GetData());
  848. return Error_Interact;
  849. }
  850. }
  851. return Error_Succeed;
  852. }
  853. ErrorCodeEnum CCardProcess::AppSelected(CardReadType eType,DeviceBaseClass *pCardX,LPBYTE aid,BYTE lenAID)
  854. {
  855. LOG_FUNCTION();
  856. char* show = new char[MAX_TEST_SHOW];
  857. ZeroMemory(show,MAX_TEST_SHOW);
  858. HexBuf2StrBuf(aid,&show,lenAID);
  859. BYTE zz = 0x00;
  860. //oiltmp添加判断aid是否存在于aid列表?目前先不做
  861. BYTE le = 0x00;
  862. ConstructAPDU(0x00,0xa4,0x04,0x00,lenAID,aid,&le);
  863. int lenRecv;
  864. LPBYTE recvBuf = new BYTE[1024];
  865. //int rc = CPU_T0_C_APDU(m_hIDCCOM,m_lenAPDU,m_APDUsendBuf,recvBuf,&lenRecv);
  866. CmdInfo cmdSend,cmdRecv;
  867. cmdSend.dwSize = m_lenAPDU;
  868. memset(cmdSend.data,0,MAX_IC_BUFFER_SIZE);
  869. memcpy(cmdSend.data,m_APDUsendBuf,m_lenAPDU);
  870. ZeroMemory(show,MAX_TEST_SHOW);
  871. HexBuf2StrBuf(m_APDUsendBuf,&show,m_lenAPDU);
  872. Dbg("<AppSelected>, to appselect[%d][%s][%d]",eType,show,m_lenAPDU);
  873. ErrorCodeEnum eErr = Error_Unexpect;
  874. CSimpleStringA errMsg(true);
  875. DWORD errICCommand = 0;
  876. if (eType == CARD_MACHINE_ISSUER)
  877. {
  878. pCardI = dynamic_cast<CardIssuerClass*>(pCardX);
  879. eErr = pCardI->ICCommand(cmdSend,cmdRecv);
  880. errICCommand = CardIssuer_UserErrorCode_ICCommand_Failed;
  881. }
  882. else if (eType == CARD_MACHINE_ISSUER_RF)
  883. {
  884. pCardI = dynamic_cast<CardIssuerClass*>(pCardX);
  885. eErr = pCardI->RFTypeABCommand(cmdSend, cmdRecv);
  886. errICCommand = ContactlessCard_UserErrorCode_RFTypeABCommand_Failed;
  887. }
  888. else if (eType == CARD_MACHINE_SWIPER || eType == CARD_MACHINE_SWIPER_RF)
  889. {
  890. pCardS = dynamic_cast<CardSwiperClass*>(pCardX);
  891. if (eType == CARD_MACHINE_SWIPER){
  892. eErr = pCardS->ICCommand(cmdSend,cmdRecv);
  893. //errICCommand = CardSwiper_UserErrorCode_ICCommand_Failed;
  894. }
  895. else if (eType == CARD_MACHINE_SWIPER_RF){
  896. eErr = pCardS->RFTypeABCommand(cmdSend,cmdRecv);
  897. //errICCommand = CardSwiper_UserErrorCode_RFTypeABCommand_Failed;
  898. }
  899. }
  900. else if (eType == CARD_MACHINE_RFIC)
  901. {
  902. pCardR = dynamic_cast<RFICClass*>(pCardX);
  903. eErr = pCardR->RFTypeABCommand(cmdSend,cmdRecv);
  904. errICCommand = ContactlessCard_UserErrorCode_RFTypeABCommand_Failed;
  905. }
  906. if (eErr == Error_Succeed)
  907. {
  908. memcpy(recvBuf,cmdRecv.data,cmdRecv.dwSize);
  909. lenRecv = cmdRecv.dwSize;
  910. ZeroMemory(show,MAX_TEST_SHOW);
  911. HexBuf2StrBuf(recvBuf,&show,lenRecv);
  912. Dbg("<AppSelected>, after appselect[%s]",show);
  913. if (recvBuf[lenRecv-2] == 0x90 && recvBuf[lenRecv-1] == 0x00)
  914. {
  915. vector<ICData> vGPOData;
  916. SplitICData(vGPOData, &recvBuf, 0, lenRecv - 2, 0, lenRecv - 2);
  917. ICData pdol(false,0x9f,0x38);
  918. DWORD lenPDOLVal;
  919. LPBYTE pPDOLVal = NULL;
  920. LPBYTE tmpBuf = NULL;
  921. if (ToFindTagValue(vGPOData,pdol,false,0,vGPOData.size()-1) == -1)
  922. {
  923. lenPDOLVal = 0;
  924. }
  925. else
  926. FillPDOL(pdol,&pPDOLVal,lenPDOLVal);
  927. tmpBuf = new BYTE[lenPDOLVal+2];//oiltmp数据长度字段暂时默认1字节
  928. tmpBuf[0] = 0x83;
  929. tmpBuf[1] = (BYTE)lenPDOLVal;
  930. memcpy(tmpBuf+2,pPDOLVal,lenPDOLVal);
  931. ConstructAPDU(0x80,0xa8,0x00,0x00,lenPDOLVal+2,tmpBuf,&le);
  932. //int rc = CPU_T0_C_APDU(m_hIDCCOM,m_lenAPDU,m_APDUsendBuf,recvBuf,&lenRecv);
  933. CmdInfo cmdSend,cmdRecv;
  934. cmdSend.dwSize = m_lenAPDU;
  935. memset(cmdSend.data,0,MAX_IC_BUFFER_SIZE);
  936. memcpy(cmdSend.data,m_APDUsendBuf,m_lenAPDU);
  937. if (eType == CARD_MACHINE_ISSUER)
  938. {
  939. pCardI = dynamic_cast<CardIssuerClass*>(pCardX);
  940. eErr = pCardI->ICCommand(cmdSend,cmdRecv);
  941. errICCommand = CardIssuer_UserErrorCode_ICCommand_Failed;
  942. }
  943. else if (eType == CARD_MACHINE_ISSUER_RF)
  944. {
  945. pCardI = dynamic_cast<CardIssuerClass*>(pCardX);
  946. eErr = pCardI->RFTypeABCommand(cmdSend, cmdRecv);
  947. errICCommand = ContactlessCard_UserErrorCode_RFTypeABCommand_Failed;
  948. }
  949. else if (eType == CARD_MACHINE_SWIPER || eType == CARD_MACHINE_SWIPER_RF)
  950. {
  951. pCardS = dynamic_cast<CardSwiperClass*>(pCardX);
  952. if (eType == CARD_MACHINE_SWIPER){
  953. eErr = pCardS->ICCommand(cmdSend,cmdRecv);
  954. //errICCommand = CardSwiper_UserErrorCode_ICCommand_Failed;
  955. }
  956. else if (eType == CARD_MACHINE_SWIPER_RF){
  957. eErr = pCardS->RFTypeABCommand(cmdSend,cmdRecv);
  958. //errICCommand = CardSwiper_UserErrorCode_RFTypeABCommand_Failed;
  959. }
  960. }
  961. else if (eType == CARD_MACHINE_RFIC)
  962. {
  963. pCardR = dynamic_cast<RFICClass*>(pCardX);
  964. eErr = pCardR->RFTypeABCommand(cmdSend,cmdRecv);
  965. errICCommand = ContactlessCard_UserErrorCode_RFTypeABCommand_Failed;
  966. }
  967. if (eErr == Error_Succeed)
  968. {
  969. memcpy(recvBuf,cmdRecv.data,cmdRecv.dwSize);
  970. lenRecv = cmdRecv.dwSize;
  971. ZeroMemory(show,MAX_TEST_SHOW);
  972. HexBuf2StrBuf(recvBuf,&show,lenRecv);
  973. Dbg("<AppSelected>gpo result [%s]",show);
  974. if (recvBuf[lenRecv-2] == 0x90 && recvBuf[lenRecv-1] == 0x00)
  975. {
  976. if (recvBuf[0] == 0x80)
  977. {
  978. ICData gpoData;
  979. if (gpoData.value != NULL)
  980. delete[] gpoData.value;
  981. gpoData.value = new BYTE[recvBuf[1]];
  982. gpoData.lenth = recvBuf[1];
  983. memcpy(gpoData.value,recvBuf+2,gpoData.lenth);
  984. //pGpo[0] = gpoData.value[0];
  985. //pGpo[1] = gpoData.value[1];
  986. eErr = ReadData(eType,pCardX,gpoData.value,gpoData.lenth);
  987. if (eErr == Error_Succeed)
  988. stable_sort(m_vICData.begin(),m_vICData.end(),compare_icdata);
  989. else
  990. {
  991. Dbg("<AppSelected>, ReadData failed(%d).",eErr);
  992. return Error_Interact;
  993. }
  994. int noneofone = 0;
  995. }
  996. }
  997. }
  998. else {
  999. QueryLastErr(pCardX, errMsg);
  1000. errMsg = CSimpleStringA::Format("AppSelected::ICCommand 2 failed(%d):%s", eErr, errMsg.GetData());
  1001. LogWarn(Severity_Middle, Error_Unexpect, errICCommand, errMsg.GetData());
  1002. return Error_Interact;
  1003. }
  1004. }
  1005. else {
  1006. switch (eType)
  1007. {
  1008. case CARD_MACHINE_ISSUER:
  1009. errICCommand = CardIssuer_UserErrorCode_ICCommand_RecvData_Invalid;
  1010. break;
  1011. case CARD_MACHINE_ISSUER_RF:
  1012. errICCommand = ContactlessCard_UserErrorCode_ICCommand_RecvData_Invalid;
  1013. break;
  1014. case CARD_MACHINE_SWIPER:
  1015. case CARD_MACHINE_SWIPER_RF:
  1016. //errICCommand = CardSwiper_UserErrorCode_ICCommand_RecvData_Invalid;
  1017. break;
  1018. case CARD_MACHINE_RFIC:
  1019. errICCommand = ContactlessCard_UserErrorCode_ICCommand_RecvData_Invalid;
  1020. break;
  1021. }
  1022. errMsg = CSimpleStringA::Format("<AppSelected>, app selected failed. end data not 9000:%x,%x", recvBuf[lenRecv - 2], recvBuf[lenRecv - 1]);
  1023. LogWarn(Severity_Middle, Error_Unexpect, errICCommand, errMsg.GetData());
  1024. return Error_DevCommFailed;
  1025. //return Error_Interact;//这里暂时按照需要循环重试来返回。这里实际应该是卡片数据问题,但部分厂商处理有问题,导致卡片时正常的,但快速拿走时,导致这里的数据异常
  1026. }
  1027. }
  1028. else
  1029. {
  1030. QueryLastErr(pCardX, errMsg);
  1031. errMsg = CSimpleStringA::Format("AppSelected::ICCommand failed(%d):%s", eErr, errMsg.GetData());
  1032. LogWarn(Severity_Middle, Error_Unexpect, errICCommand, errMsg.GetData());
  1033. return Error_Interact;
  1034. }
  1035. return Error_Succeed;
  1036. }
  1037. ErrorCodeEnum CCardProcess::ReadData(CardReadType eType,DeviceBaseClass *pCardX,LPBYTE data,DWORD len)
  1038. {
  1039. int lenRecv;
  1040. LPBYTE recvBuf = new BYTE[1024];
  1041. char* show = new char[MAX_TEST_SHOW];
  1042. ZeroMemory(show,MAX_TEST_SHOW);
  1043. ZeroMemory(recvBuf,1024);
  1044. m_AIP[0] = data[0];
  1045. m_AIP[1] = data[1];
  1046. ErrorCodeEnum eErr;
  1047. if (eType == CARD_MACHINE_ISSUER || eType == CARD_MACHINE_ISSUER_RF)
  1048. {
  1049. pCardI = dynamic_cast<CardIssuerClass*>(pCardX);
  1050. }
  1051. else if (eType == CARD_MACHINE_SWIPER || eType == CARD_MACHINE_SWIPER_RF)
  1052. {
  1053. pCardS = dynamic_cast<CardSwiperClass*>(pCardX);
  1054. }
  1055. else if (eType == CARD_MACHINE_RFIC)
  1056. {
  1057. pCardR = dynamic_cast<RFICClass*>(pCardX);
  1058. }
  1059. for (int i = 2; i < len;)
  1060. {
  1061. BYTE afl[4];
  1062. memcpy(afl,data+i,4);
  1063. i += 4;
  1064. BYTE recordSeq,le=0x00;
  1065. BYTE sfi = afl[0]>>3;
  1066. BYTE p2 = (sfi<<3)+0x04;
  1067. for(recordSeq = afl[1]; recordSeq <= afl[2]; ++recordSeq)
  1068. {
  1069. ConstructAPDU(0x00,0xb2,recordSeq,p2,NULL,NULL,&le);
  1070. //int rc = CPU_T0_C_APDU(m_hIDCCOM,m_lenAPDU,m_APDUsendBuf,recvBuf,&lenRecv);
  1071. CmdInfo cmdSend,cmdRecv;
  1072. cmdSend.dwSize = m_lenAPDU;
  1073. ZeroMemory(cmdSend.data,sizeof(cmdSend.data));
  1074. ZeroMemory(cmdRecv.data,sizeof(cmdRecv.data));
  1075. memcpy(cmdSend.data,m_APDUsendBuf,m_lenAPDU);
  1076. if (eType == CARD_MACHINE_ISSUER)
  1077. {
  1078. eErr = pCardI->ICCommand(cmdSend,cmdRecv);
  1079. }
  1080. else if (eType == CARD_MACHINE_ISSUER_RF)
  1081. {
  1082. eErr = pCardI->RFTypeABCommand(cmdSend, cmdRecv);
  1083. }
  1084. else if (eType == CARD_MACHINE_SWIPER)
  1085. {
  1086. eErr = pCardS->ICCommand(cmdSend,cmdRecv);
  1087. }
  1088. else if (eType == CARD_MACHINE_RFIC)
  1089. {
  1090. eErr = pCardR->RFTypeABCommand(cmdSend,cmdRecv);
  1091. }
  1092. else if (eType == CARD_MACHINE_SWIPER_RF)
  1093. {
  1094. eErr = pCardS->RFTypeABCommand(cmdSend,cmdRecv);
  1095. }
  1096. if (eErr == Error_Succeed)
  1097. {
  1098. memcpy(recvBuf,cmdRecv.data,cmdRecv.dwSize);
  1099. lenRecv = cmdRecv.dwSize;
  1100. HexBuf2StrBuf(recvBuf,&show,lenRecv);
  1101. //Dbg("ReadData[%s]",show);
  1102. Dbg("read record i(%d),p1.record(%d),p2(%d)",i,recordSeq,p2);
  1103. SplitICData(m_vICData, &recvBuf, 0, lenRecv - 2, 0, lenRecv - 2);
  1104. }
  1105. else
  1106. {
  1107. Dbg("read data failed(%d).",eErr);
  1108. return Error_Interact;
  1109. }
  1110. }
  1111. }
  1112. Dbg("read data end");
  1113. delete[] recvBuf;
  1114. delete[] show;
  1115. return Error_Succeed;
  1116. }
  1117. void CCardProcess::ProcessADFRecord(vector<ICData>& record,int start)
  1118. {
  1119. LOG_FUNCTION();
  1120. char* show = new char[MAX_TEST_SHOW];
  1121. ZeroMemory(show,MAX_TEST_SHOW);
  1122. ICData data4f(false,0x4f,0x00),data50(false,0x50,0x00),data9f12(false,0x9f,0x12),data87(false,0x87,0x00);
  1123. if (ToFindTagValue(record,data4f,false,start,start+record.at(start).num) != -1 &&
  1124. ToFindTagValue(record,data50,false,start,start+record.at(start).num) != -1)
  1125. {
  1126. ADFRecord adfRec;
  1127. adfRec.name = new BYTE[data4f.lenth+1];
  1128. memcpy(adfRec.name,data4f.value,data4f.lenth);
  1129. adfRec.nameLen = data4f.lenth;
  1130. if (data4f.value != NULL)
  1131. delete[] data4f.value;
  1132. HexBuf2StrBuf(adfRec.name,&show,adfRec.nameLen);
  1133. adfRec.appLabel = new BYTE[data50.lenth+1];
  1134. memcpy(adfRec.appLabel,data50.value,data50.lenth);
  1135. adfRec.appLabelLen = data50.lenth;
  1136. if (data50.value != NULL)
  1137. delete[] data50.value;
  1138. HexBuf2StrBuf(adfRec.appLabel,&show,adfRec.appLabelLen);
  1139. if (ToFindTagValue(record,data9f12,false,start,start+record.at(start).num) != -1)
  1140. {
  1141. adfRec.priName = new BYTE[data9f12.lenth+1];
  1142. memcpy(adfRec.priName,data9f12.value,data9f12.lenth);
  1143. adfRec.priNameLen = data9f12.lenth;
  1144. if(data9f12.value != NULL)
  1145. delete[] data9f12.value;
  1146. HexBuf2StrBuf(adfRec.priName,&show,adfRec.priNameLen);
  1147. }
  1148. if (ToFindTagValue(record,data87,false,start,start+record.at(start).num) != -1)
  1149. {
  1150. adfRec.appPriID = *data87.value;
  1151. adfRec.appPriIDLen = 1;
  1152. }
  1153. m_vADFRec.push_back(adfRec);
  1154. }
  1155. }
  1156. void CCardProcess::ProcessDDFRecord(CardReadType eType,DeviceBaseClass *pCardX,vector<AIDData>& vAIDFromTerm,LPBYTE pDDF,int length)
  1157. {
  1158. BYTE zz = 0x00;
  1159. BYTE cls = 0x00;
  1160. BYTE ins = 0xa4;
  1161. BYTE p1 = 0x04;
  1162. BYTE p2 = 0x00;
  1163. BYTE le = NULL;
  1164. LPBYTE pData = new BYTE[length];
  1165. if (pData == NULL)
  1166. return ;
  1167. memcpy(pData,pDDF,length);
  1168. ConstructAPDU(cls,ins,p1,p2,length,pData,&le);
  1169. int lenRecv;
  1170. LPBYTE recvBuf = new BYTE[1024];
  1171. char* show = new char[MAX_TEST_SHOW];
  1172. ZeroMemory(show,MAX_TEST_SHOW);
  1173. //int rc = CPU_T0_C_APDU(m_hIDCCOM,m_lenAPDU,m_APDUsendBuf,recvBuf,&lenRecv);
  1174. CmdInfo cmdSend,cmdRecv;
  1175. cmdSend.dwSize = m_lenAPDU;
  1176. memset(cmdSend.data,0,MAX_IC_BUFFER_SIZE);
  1177. memcpy(cmdSend.data,m_APDUsendBuf,m_lenAPDU);
  1178. ErrorCodeEnum eErr;
  1179. if (eType == CARD_MACHINE_ISSUER)
  1180. {
  1181. pCardI = dynamic_cast<CardIssuerClass*>(pCardX);
  1182. eErr = pCardI->ICCommand(cmdSend,cmdRecv);
  1183. }
  1184. else if (eType == CARD_MACHINE_ISSUER_RF)
  1185. {
  1186. pCardI = dynamic_cast<CardIssuerClass*>(pCardX);
  1187. eErr = pCardI->RFTypeABCommand(cmdSend, cmdRecv);
  1188. }
  1189. else if (eType == CARD_MACHINE_SWIPER || eType == CARD_MACHINE_SWIPER_RF)
  1190. {
  1191. pCardS = dynamic_cast<CardSwiperClass*>(pCardX);
  1192. if (eType == CARD_MACHINE_SWIPER)
  1193. eErr = pCardS->ICCommand(cmdSend,cmdRecv);
  1194. else if (eType == CARD_MACHINE_SWIPER_RF)
  1195. eErr = pCardS->RFTypeABCommand(cmdSend,cmdRecv);
  1196. }
  1197. else if (eType == CARD_MACHINE_RFIC)
  1198. {
  1199. pCardR = dynamic_cast<RFICClass*>(pCardX);
  1200. eErr = pCardR->RFTypeABCommand(cmdSend,cmdRecv);
  1201. }
  1202. if (eErr == Error_Succeed)
  1203. {
  1204. memcpy(recvBuf,cmdRecv.data,cmdRecv.dwSize);
  1205. lenRecv = cmdRecv.dwSize;
  1206. if (recvBuf[lenRecv-2] == 0x90 && recvBuf[lenRecv-1] == 0x00)
  1207. {
  1208. HexBuf2StrBuf(recvBuf,&show,lenRecv);
  1209. vector<ICData> vPSEData;
  1210. SplitICData(vPSEData, &recvBuf, 0, lenRecv - 2, 0, lenRecv - 2);
  1211. DWORD lenSFI;
  1212. ICData testData(false,0x88,0x00,2);
  1213. if (ToFindTagValue(vPSEData,testData,true,0,vPSEData.size()-1) != -1)
  1214. {
  1215. BYTE tmpSFI = testData.value[0];
  1216. BYTE tmpP2 = (tmpSFI<<3)+0x04;
  1217. delete[] testData.value;
  1218. bool bNoMoreRec = false,bFirstRead = true;
  1219. do
  1220. {
  1221. ConstructAPDU(0x00,0xb2,tmpSFI,tmpP2,NULL,NULL,&zz);
  1222. //rc = CPU_T0_C_APDU(m_hIDCCOM,m_lenAPDU,m_APDUsendBuf,recvBuf,&lenRecv);
  1223. cmdSend.dwSize = m_lenAPDU;
  1224. memset(cmdSend.data,0,MAX_IC_BUFFER_SIZE);
  1225. memcpy(cmdSend.data,m_APDUsendBuf,m_lenAPDU);
  1226. if (eType == CARD_MACHINE_ISSUER)
  1227. {
  1228. eErr = pCardI->ICCommand(cmdSend,cmdRecv);
  1229. }
  1230. else if (eType == CARD_MACHINE_ISSUER_RF)
  1231. {
  1232. eErr = pCardI->RFTypeABCommand(cmdSend, cmdRecv);
  1233. }
  1234. else if (eType == CARD_MACHINE_SWIPER)
  1235. {
  1236. eErr = pCardS->ICCommand(cmdSend,cmdRecv);
  1237. }
  1238. else if (eType == CARD_MACHINE_RFIC)
  1239. {
  1240. eErr = pCardR->RFTypeABCommand(cmdSend,cmdRecv);
  1241. }
  1242. else if (eType == CARD_MACHINE_SWIPER_RF)
  1243. {
  1244. eErr = pCardS->RFTypeABCommand(cmdSend,cmdRecv);
  1245. }
  1246. if (eErr == Error_Succeed)
  1247. {
  1248. memcpy(recvBuf,cmdRecv.data,cmdRecv.dwSize);
  1249. lenRecv = cmdRecv.dwSize;
  1250. HexBuf2StrBuf(recvBuf,&show,lenRecv);
  1251. if(recvBuf[lenRecv-2] == 0x6A && recvBuf[lenRecv-1] == 0x83)
  1252. {
  1253. bNoMoreRec = true;
  1254. if (bFirstRead)
  1255. break;
  1256. }
  1257. else if(recvBuf[lenRecv-2] == 0x90 && recvBuf[lenRecv-1] == 0x00)
  1258. {
  1259. bFirstRead = false;
  1260. vector<ICData> vDirRec;
  1261. SplitICData(vDirRec, &recvBuf, 0, lenRecv - 2, 0, lenRecv - 2);
  1262. ICData dirData(true,0x61,0x00,1);
  1263. int start = 0;
  1264. for (int ret = 0; start < vDirRec.size();)
  1265. {
  1266. ret = ToFindTagValue(vDirRec,dirData,true,start,vDirRec.size()-1);
  1267. if (ret == -1 )
  1268. break;
  1269. start += ret;
  1270. if (vDirRec.at(start+1).tag[0] == 0x4F && vDirRec.at(start+1).tag[1] == 0x00)
  1271. {
  1272. ProcessADFRecord(vDirRec,start);
  1273. }
  1274. else if (vDirRec.at(start+1).tag[0] == 0x9D && vDirRec.at(start+1).tag[1] == 0x00)
  1275. {
  1276. //oiltmp
  1277. ProcessDDFRecord(eType,pCardX,vAIDFromTerm,vDirRec.at(start+1).value,vDirRec.at(start+1).lenth);
  1278. }
  1279. }
  1280. tmpSFI++;
  1281. continue;
  1282. }
  1283. }
  1284. else {
  1285. CSimpleStringA errMsg("");
  1286. QueryLastErr(pCardX, errMsg);
  1287. Dbg("<ProcessADFRecord>, ICCommand 2 failed with errCode:%d(0x%x), errMsg:%s", eErr, eErr, errMsg.GetData());
  1288. break;
  1289. }
  1290. }while(!bNoMoreRec);
  1291. }
  1292. }
  1293. }
  1294. else {
  1295. CSimpleStringA errMsg("");
  1296. QueryLastErr(pCardX, errMsg);
  1297. Dbg("<ProcessADFRecord>, ICCommand failed with errCode:%d(0x%x), errMsg:%s", eErr, eErr, errMsg.GetData());
  1298. }
  1299. delete[] recvBuf;
  1300. delete[] show;
  1301. return;
  1302. }
  1303. bool CCardProcess::FillPDOL(ICData& pdol,LPBYTE* ppBYTE,DWORD& lenPDOL)
  1304. {
  1305. char* show = new char[MAX_TEST_SHOW];
  1306. ZeroMemory(show,MAX_TEST_SHOW);
  1307. HexBuf2StrBuf(pdol.value,&show,pdol.lenth);
  1308. //oiltmp 20141021 for temp
  1309. ICData data(false,0x9f,0x66);
  1310. data.value = new BYTE[4];
  1311. data.lenth = 4;
  1312. ZeroMemory(data.value,4);
  1313. data.value[0] = 0xff;
  1314. data.value[1] = 0x80;
  1315. data.value[2] = 0x0;
  1316. data.value[3] = 0x0;
  1317. m_vBusData.push_back(data);
  1318. Dbg("to fill pdol [%s]",show);
  1319. //6f458408a000000333010101a539500a50424f432044454249548701019f38099f7a019f0206
  1320. //5f2a025f2d027a689f1101019f120ad5d0d0d0d2bbbfa8cda8bf0c059f4d020b0a9000
  1321. //9f38099f7a019f02065f2a02
  1322. char* test = "000102030405060708";
  1323. char tmpBuf[512];
  1324. lenPDOL = 0;
  1325. for (int i = 0; i < pdol.lenth;)
  1326. {
  1327. if (IsDoubleByteFlag(pdol.value[i]))
  1328. {
  1329. ICData tagValue(false,pdol.value[i],pdol.value[i+1]);
  1330. if (ToFindTagValue(m_vBusData,tagValue,false,0,m_vBusData.size()-1) != -1)
  1331. memcpy(tmpBuf+lenPDOL,tagValue.value,tagValue.lenth);
  1332. else
  1333. memset(tmpBuf+lenPDOL,0,pdol.value[i+2]);
  1334. lenPDOL += pdol.value[i+2];
  1335. i += 3;
  1336. }
  1337. else
  1338. {
  1339. //Dbg("unknown tag...%x",pdol.value[i]);
  1340. memset(tmpBuf+lenPDOL,0,pdol.value[i+1]);
  1341. //memcpy(tmpBuf+lenPDOL,test,pdol.value[i+1]);
  1342. lenPDOL += pdol.value[i+1];
  1343. i += 2;
  1344. }
  1345. }
  1346. *ppBYTE = new BYTE[lenPDOL];
  1347. memcpy(*ppBYTE,tmpBuf,lenPDOL);
  1348. //char* show = new char[MAX_TEST_SHOW];
  1349. ZeroMemory(show,MAX_TEST_SHOW);
  1350. HexBuf2StrBuf(*ppBYTE,&show,lenPDOL);
  1351. Dbg("<FillPDOL>, filled..pdol[%s]",show);
  1352. return true;
  1353. }
  1354. void CCardProcess::ProcessSFIRecord(LPBYTE* rec,int lenRec)
  1355. {
  1356. // char* show = new char[MAX_TEST_SHOW];
  1357. //ZeroMemory(show,MAX_TEST_SHOW);
  1358. //HexBuf2StrBuf(*rec,&show,lenRec-2);
  1359. ////DeleteICData();
  1360. //vector<ICData> vSFIRec;
  1361. //SplitICData(vSFIRec,rec,0,lenRec,0);
  1362. //ICData testData;
  1363. //int start = 0;
  1364. //for (int ret = 0; start < vSFIRec.size();)
  1365. //{
  1366. // testData.bForm = true;
  1367. // testData.tag[0] = 0x61;
  1368. // testData.tag[1] = 0x00;
  1369. // testData.level = 1;
  1370. // ret = FindTagValue(vSFIRec,testData,true,start,vSFIRec.size()-1);
  1371. // if (ret == -1 )
  1372. // break;
  1373. // start = start+ret+1;
  1374. // //oiltest M标签固定排序?
  1375. // if (vICData.at(start).tag[0] == 0x4F && vICData.at(start).tag[1] == 0x00)
  1376. // {
  1377. // //ADF,取相应的数据
  1378. // }
  1379. // else if (vICData.at(start).tag[0] == 0x9D && vICData.at(start).tag[1] == 0x00)
  1380. // {
  1381. // BYTE zz = 0x00;
  1382. // BYTE le = NULL;
  1383. // //DDF
  1384. // ICData ddfData(false,0x9d,0x00);
  1385. // FindTagValue(vICData,ddfData,false,start,start+vICData.at(start-1).num-1);
  1386. // ConstructAPDU(0x00,0xa4,04,00,ddfData.lenth,ddfData.value,&le);
  1387. // int lenRecv;
  1388. // LPBYTE recvBuf = new BYTE[1024];
  1389. // char* show = new char[MAX_TEST_SHOW];
  1390. // ZeroMemory(show,MAX_TEST_SHOW);
  1391. // int rc = CPU_T0_C_APDU(m_hIDCCOM,m_lenAPDU,m_APDUsendBuf,recvBuf,&lenRecv);
  1392. // if (rc == 0)
  1393. // {
  1394. // if (recvBuf[lenRecv-2] == 0x90 && recvBuf[lenRecv-1] == 0x00)
  1395. // {
  1396. // BYTE tmpSFI = ddfData.value[0];
  1397. // BYTE tmpP2 = (tmpSFI<<3)+0x04;
  1398. // delete[] ddfData.value;
  1399. // bool bNoMoreRec = false;
  1400. // do
  1401. // {
  1402. // ConstructAPDU(0x00,0xb2,tmpSFI,tmpP2,NULL,NULL,&zz);
  1403. // rc = CPU_T0_C_APDU(m_hIDCCOM,m_lenAPDU,m_APDUsendBuf,recvBuf,&lenRecv);
  1404. // if (rc == 0)
  1405. // {
  1406. // HexBuf2StrBuf(recvBuf,&show,lenRecv);
  1407. // if(recvBuf[lenRecv-2] == 0x6A && recvBuf[lenRecv-1] == 0x83)
  1408. // bNoMoreRec = true;
  1409. // else if(recvBuf[lenRecv-2] == 0x90 && recvBuf[lenRecv-1] == 0x00)
  1410. // {
  1411. // ProcessSFIRecord(&recvBuf,lenRecv-2);
  1412. // //DeleteICData();
  1413. // tmpSFI++;
  1414. // continue;
  1415. // }
  1416. // }
  1417. // else
  1418. // break;
  1419. // }while(!bNoMoreRec);
  1420. // }
  1421. // }
  1422. // }
  1423. //}
  1424. }
  1425. void CCardProcess::ProcessRestrict()
  1426. {
  1427. LOG_FUNCTION();
  1428. //应用版本号检查
  1429. ICData appVersion(false,0x9f,0x08);
  1430. if (ToFindTagValue(m_vICData,appVersion,false,0,m_vICData.size()-1) == -1)
  1431. {
  1432. return;
  1433. }
  1434. if (m_appVersion[0] != appVersion.value[0] || m_appVersion[1] != appVersion.value[1])
  1435. {
  1436. Dbg("app version not match.%x.%x:%x.%x", m_appVersion[0], m_appVersion[1], appVersion.value[0], appVersion.value[1]);
  1437. m_TVR[1] |= TVR_APP_VERSION_UNMATCH;
  1438. return;
  1439. }
  1440. //AUC检查
  1441. //...
  1442. //发卡行国家代码
  1443. //应用用途控制检查 Opt
  1444. //应用失效日期检查
  1445. ICData appExpiryDate(false,0x5f,0x24);
  1446. if (ToFindTagValue(m_vICData,appExpiryDate,false,0,m_vICData.size()-1) == -1)
  1447. {
  1448. return;
  1449. }
  1450. DWORD exYear = (appExpiryDate.value[0] >> 4) * 10 + (appExpiryDate.value[0] & 0x0f) + 2000;
  1451. DWORD exMonth = (appExpiryDate.value[1] >> 4) * 10 + (appExpiryDate.value[1] & 0x0f);
  1452. DWORD exDay = (appExpiryDate.value[2] >> 4) * 10 + (appExpiryDate.value[2] & 0x0f);
  1453. #if (defined(_WIN32) || defined(_WIN64))
  1454. SYSTEMTIME localTime;
  1455. GetLocalTime(&localTime);
  1456. ATL::CTime currTime(localTime.wYear, localTime.wMonth, localTime.wDay, 0, 0, 0);
  1457. ATL::CTime exTime(exYear, exMonth, exDay, 0, 0, 0);
  1458. if (exTime <= currTime) {
  1459. return;
  1460. }
  1461. #else
  1462. time_t t;
  1463. time(&t);
  1464. struct tm* t1;
  1465. t1 = localtime(&t);
  1466. t1->tm_year += 1900;
  1467. t1->tm_mon += 1; /*TO COMFIRM*/
  1468. if (exYear < t1->tm_year
  1469. || (exYear == t1->tm_year && exMonth < t1->tm_mon)
  1470. || (exYear == t1->tm_year && exMonth == t1->tm_mon && exDay < t1->tm_yday)) {
  1471. return;
  1472. }
  1473. #endif //_WIN32
  1474. //应用生效日期检查 Opt
  1475. }
  1476. void CCardProcess::CardholderVerify()
  1477. {
  1478. LOG_FUNCTION();
  1479. //oiltmp目前只支持联机密码验证
  1480. return;
  1481. //bool bCardhoderVerify = false;
  1482. //if (CARDHOLDERVERIFYFLAG == m_AIP[0]&CARDHOLDERVERIFYFLAG)
  1483. // bCardhoderVerify = true;
  1484. //ICData CVMList(false,0x8e,0x00);
  1485. //int start = 0;
  1486. ////逐一处理CVMList
  1487. //do
  1488. //{
  1489. // int tmpPos = FindTagValue(m_vICData,CVMList,false,start,m_vICData.size()-1);
  1490. // if (tmpPos == -1)
  1491. // {
  1492. // if (start == 1)
  1493. // m_TVR[0] |= TVR_IC_Data_Miss;//设置TVR ‘IC卡数据缺失’
  1494. // break;
  1495. // }
  1496. // do
  1497. // {
  1498. // //oiltest暂时只处理‘总是’选项
  1499. // if (CVMList.value[9] != 0x00)
  1500. // break;
  1501. // if ((CVMList.value[8]&0x3f) == 0x01 || CVMList.value[8]&0x3f) == 0x02 || CVMList.value[8]&0x3f) == 0x1f)
  1502. // {
  1503. // }
  1504. // else
  1505. // {
  1506. // m_TVR[2] |= TVR_CVM_Unkown;
  1507. // break;
  1508. // }
  1509. // }while(false);
  1510. // if (0x00 == (CVMList.value[8]&0x40))
  1511. // {
  1512. // m_TVR[2] |= TVR_CVM_Failed;
  1513. // break;
  1514. // }
  1515. // start = tmpPos;
  1516. //}while(start < m_vICData.size());
  1517. //m_TSI[0] |= TSI_Cardholder;
  1518. }
  1519. void CCardProcess::TermRiskManage()
  1520. {
  1521. LOG_FUNCTION();
  1522. //终端异常文件检查
  1523. ICData pan(false,0x5a,0x00);
  1524. if (ToFindTagValue(m_vICData,pan,false,0,m_vICData.size()-1) == -1)
  1525. {
  1526. }
  1527. //商户强制交易联机
  1528. //最低限额检查
  1529. //随机交易选择
  1530. //频度检查
  1531. //新卡检查
  1532. }
  1533. int CCardProcess::TermActionAnalyze(CardReadType eType,DeviceBaseClass *pCardX,CSimpleStringA &result,bool bOnlineOnly,bool bCDA,BYTE &d9f27)
  1534. {
  1535. LOG_FUNCTION();
  1536. //9F0D缺省 9F0E拒绝 9F0F联机
  1537. ICData iacDefault(false,0x9f,0x0d);
  1538. ICData iacReject(false,0x9f,0x0e);
  1539. ICData iacOnline(false,0x9f,0x0f);
  1540. BYTE p1;
  1541. do
  1542. {
  1543. if (ToFindTagValue(m_vICData,iacReject,0,0,m_vICData.size()-1) == -1)
  1544. memset(m_IACReject,0,5);
  1545. else
  1546. memcpy(m_IACReject,iacReject.value,5);
  1547. if (m_pTACReject == NULL)
  1548. {
  1549. m_pTACReject = new BYTE[5];
  1550. memset(m_pTACReject,0,5);
  1551. }
  1552. if (IsSamePositionSet(m_IACReject,m_TVR,5) || IsSamePositionSet(m_pTACReject,m_TVR,5))
  1553. {
  1554. Dbg("iacreject:%x%x%x%x%x,tvr:%x%x%x%x%x", m_IACReject[0], m_IACReject[1], m_IACReject[2], m_IACReject[3], m_IACReject[4]
  1555. , m_TVR[0], m_TVR[1], m_TVR[2], m_TVR[3], m_TVR[4]);
  1556. m_AuthCode[0] = 'Z';
  1557. m_AuthCode[1] = '1';
  1558. p1 = P1_GENAC_AAC;
  1559. break;
  1560. }
  1561. if (bOnlineOnly)
  1562. {
  1563. p1 = P1_GENAC_ARQC;
  1564. break;
  1565. }
  1566. else
  1567. {
  1568. if (ToFindTagValue(m_vICData,iacOnline,0,0,m_vICData.size()-1) == -1)
  1569. memset(m_IACOnline,0xff,5);
  1570. else
  1571. memcpy(m_IACOnline,iacOnline.value,5);
  1572. if (m_pTACOnline == NULL)
  1573. {
  1574. m_pTACOnline = new BYTE[5];
  1575. memset(m_pTACOnline,0,5);
  1576. }
  1577. if (IsSamePositionSet(m_IACOnline,m_TVR,5) || IsSamePositionSet(m_pTACOnline,m_TVR,5))
  1578. {
  1579. p1 = P1_GENAC_ARQC;
  1580. break;
  1581. }
  1582. if (ToFindTagValue(m_vICData,iacDefault,0,0,m_vICData.size()-1) == -1)
  1583. memset(m_IACDefault,0xff,5);
  1584. else
  1585. memcpy(m_IACDefault,iacDefault.value,5);
  1586. if (m_pTACDefault == NULL)
  1587. {
  1588. m_pTACDefault = new BYTE[5];
  1589. memset(m_pTACDefault,0,5);
  1590. }
  1591. if (IsSamePositionSet(m_IACDefault,m_TVR,5) || IsSamePositionSet(m_pTACDefault,m_TVR,5))
  1592. {
  1593. m_AuthCode[0] = 'Z';
  1594. m_AuthCode[1] = '3';
  1595. p1 = P1_GENAC_AAC;
  1596. break;
  1597. }
  1598. }
  1599. m_AuthCode[0] = 'Y';
  1600. m_AuthCode[1] = '1';
  1601. p1 = P1_GENAC_TC;
  1602. }while(false);
  1603. if (bCDA)
  1604. {
  1605. p1 |= 0x10;//设置执行CDA标识
  1606. }
  1607. d9f27 = p1;
  1608. Dbg("to do 1st gen ac bCDA(%d)(%d)",bCDA,p1);
  1609. //1st genarate ac
  1610. ICData cdol1(false,0x8c,0x00);
  1611. if (ToFindTagValue(m_vICData,cdol1,0,0,m_vICData.size()-1) == -1)
  1612. {
  1613. Dbg("can't find cdol1.");
  1614. return -1;
  1615. }
  1616. PBYTE pCDOL1;
  1617. DWORD lenCDOL1=0;
  1618. FillCDOL(cdol1,pCDOL1,lenCDOL1);
  1619. ConstructAPDU(CLS_GENAC,INS_GENAC,p1,0x00,lenCDOL1,pCDOL1,0x00);
  1620. int lenRecv;
  1621. LPBYTE recvBuf = new BYTE[1024];
  1622. char* show = new char[MAX_TEST_SHOW];
  1623. ZeroMemory(show,MAX_TEST_SHOW);
  1624. //int rc = CPU_T0_C_APDU(m_hIDCCOM,m_lenAPDU,m_APDUsendBuf,recvBuf,&lenRecv);
  1625. CmdInfo cmdSend,cmdRecv;
  1626. cmdSend.dwSize = m_lenAPDU;
  1627. memset(cmdSend.data,0,MAX_IC_BUFFER_SIZE);
  1628. memcpy(cmdSend.data,m_APDUsendBuf,m_lenAPDU);
  1629. HexBuf2StrBuf(cmdSend.data,&show,m_lenAPDU);
  1630. Dbg("1st GenAc cmd[%s]",show);
  1631. ErrorCodeEnum eErr;
  1632. if (eType == CARD_MACHINE_ISSUER)
  1633. {
  1634. pCardI = dynamic_cast<CardIssuerClass*>(pCardX);
  1635. eErr = pCardI->ICCommand(cmdSend,cmdRecv);
  1636. }
  1637. else if (eType == CARD_MACHINE_ISSUER_RF)
  1638. {
  1639. pCardI = dynamic_cast<CardIssuerClass*>(pCardX);
  1640. eErr = pCardI->RFTypeABCommand(cmdSend, cmdRecv);
  1641. }
  1642. else if (eType == CARD_MACHINE_SWIPER || eType == CARD_MACHINE_SWIPER_RF)
  1643. {
  1644. pCardS = dynamic_cast<CardSwiperClass*>(pCardX);
  1645. if (eType == CARD_MACHINE_SWIPER)
  1646. eErr = pCardS->ICCommand(cmdSend,cmdRecv);
  1647. else if (eType == CARD_MACHINE_SWIPER_RF)
  1648. eErr = pCardS->RFTypeABCommand(cmdSend,cmdRecv);
  1649. }
  1650. else if (eType == CARD_MACHINE_RFIC)
  1651. {
  1652. pCardR = dynamic_cast<RFICClass*>(pCardX);
  1653. eErr = pCardR->RFTypeABCommand(cmdSend,cmdRecv);
  1654. }
  1655. Dbg("1st gen ac rc[%d]",eErr);
  1656. bool bFirstGenAC = false;
  1657. if (eErr == Error_Succeed)
  1658. {
  1659. memcpy(recvBuf,cmdRecv.data,cmdRecv.dwSize);
  1660. lenRecv = cmdRecv.dwSize;
  1661. ZeroMemory(show,MAX_TEST_SHOW);
  1662. HexBuf2StrBuf(recvBuf,&show,lenRecv);
  1663. Dbg("1stGenAc recv[%s]",show);
  1664. if (recvBuf[lenRecv-2] == 0x61)
  1665. {
  1666. BYTE btLen = recvBuf[lenRecv-1];
  1667. ConstructAPDU(CLS_GET_RESPONSE,INS_GET_RESPONSE,0x00,0x00,NULL,NULL,&btLen);
  1668. CmdInfo cmdSend,cmdRecv;
  1669. cmdSend.dwSize = m_lenAPDU;
  1670. memset(cmdSend.data,0,MAX_IC_BUFFER_SIZE);
  1671. memcpy(cmdSend.data,m_APDUsendBuf,m_lenAPDU);
  1672. ZeroMemory(show,MAX_TEST_SHOW);
  1673. HexBuf2StrBuf(cmdSend.data,&show,m_lenAPDU);
  1674. Dbg("get response cmd[%s]",show);
  1675. if (eType == CARD_MACHINE_ISSUER)
  1676. {
  1677. eErr = pCardI->ICCommand(cmdSend,cmdRecv);
  1678. }
  1679. else if (eType == CARD_MACHINE_ISSUER_RF)
  1680. {
  1681. eErr = pCardI->RFTypeABCommand(cmdSend, cmdRecv);
  1682. }
  1683. else if (eType == CARD_MACHINE_SWIPER)
  1684. {
  1685. eErr = pCardS->ICCommand(cmdSend,cmdRecv);
  1686. }
  1687. else if (eType == CARD_MACHINE_RFIC)
  1688. {
  1689. eErr = pCardR->RFTypeABCommand(cmdSend,cmdRecv);
  1690. }
  1691. else if (eType == CARD_MACHINE_SWIPER_RF)
  1692. {
  1693. eErr = pCardS->RFTypeABCommand(cmdSend,cmdRecv);
  1694. }
  1695. if (eErr == Error_Succeed)
  1696. {
  1697. ZeroMemory(show,MAX_TEST_SHOW);
  1698. ZeroMemory(recvBuf,1024);
  1699. memcpy(recvBuf,cmdRecv.data,cmdRecv.dwSize);
  1700. lenRecv = cmdRecv.dwSize;
  1701. HexBuf2StrBuf(recvBuf,&show,lenRecv);
  1702. //Dbg("oiltest get response recv[%s]",show);
  1703. if (recvBuf[lenRecv - 2] == 0x90 && recvBuf[lenRecv - 1] == 0x00)
  1704. {
  1705. bFirstGenAC = true;
  1706. Dbg("first generate ac suc.");
  1707. }
  1708. else
  1709. {
  1710. Dbg("first generate ac failed.%x%x", recvBuf[lenRecv - 2], recvBuf[lenRecv - 1]);
  1711. }
  1712. }
  1713. }
  1714. else if (recvBuf[lenRecv-2] == 0x90 && recvBuf[lenRecv-1] == 0x00)
  1715. bFirstGenAC = true;
  1716. else if (recvBuf[lenRecv-2] == 0x69 && recvBuf[lenRecv-1] == 0x85)
  1717. {
  1718. //>2 generate ac
  1719. }
  1720. if (bFirstGenAC)
  1721. {
  1722. //form2 '77'暂不考虑
  1723. //form1 '80'
  1724. if (recvBuf[0] == 0x80)
  1725. {
  1726. //80 1e 80 0008 328ab54bfc986b85 07010103a0b000010a010000000000754048769000
  1727. //密文信息数据 1B,ATC 2B,AC 8B,发卡行数据 opt
  1728. //暂不执行CDA oiltmp
  1729. BYTE cryptInfo = recvBuf[2];
  1730. m_TSI[0] |= TSI_TERM_RISK_MANAGE;
  1731. if (((p1&P1_GENAC_TC) == P1_GENAC_TC || (p1&P1_GENAC_ARQC) == P1_GENAC_ARQC) && ((cryptInfo & 0xc0) == P1_GENAC_ARQC))
  1732. {
  1733. //同时终端有联机能力则执行联机请求
  1734. if ((p1&P1_GENAC_ARQC) == P1_GENAC_ARQC)
  1735. m_P1 = 1;
  1736. else
  1737. m_P1 = 0;
  1738. m_CVR[0] = recvBuf[16];
  1739. m_CVR[1] = recvBuf[17];
  1740. m_CVR[2] = recvBuf[18];
  1741. m_CVR[3] = recvBuf[19];
  1742. result = show;
  1743. return 0;
  1744. }
  1745. else if ((((p1&P1_GENAC_AAC) == P1_GENAC_AAC) && (((cryptInfo & 0xc0) == 0x40) || ((cryptInfo & 0xc0) == 0x80)))
  1746. || (((p1&P1_GENAC_ARQC) == P1_GENAC_ARQC) && ((cryptInfo&0xc0) == 0x40)))
  1747. {
  1748. //终止交易
  1749. return 1;
  1750. }
  1751. else
  1752. {
  1753. //满足
  1754. //1.执行了CDA但结果失败;2.卡片响应AAC或者TC;3.卡片响应ARQC但终端没联机能力
  1755. //之一则执行‘交易结束流程’
  1756. return 2;
  1757. }
  1758. }
  1759. }
  1760. }
  1761. if (recvBuf != NULL)
  1762. delete []recvBuf;
  1763. if (show != NULL)
  1764. delete []show;
  1765. return 0;
  1766. }
  1767. bool CCardProcess::FillCDOL(ICData& cdol,PBYTE &pBYTE,DWORD &lenCDOL)
  1768. {
  1769. Dbg("Fill CDOL");
  1770. char* test = "000102030405060708";
  1771. char tmpBuf[512] = {0};
  1772. char *pShow = new char[32];
  1773. lenCDOL = 0;
  1774. //9f0206 9f0306 9f1a02 9505 5f2a02 9a03 9c01 9f3704 9f2103 9f4e14
  1775. //9f0206授权金额 ( 6 ), 9f0306其它金额 ( 6), 9f1a02终端国家代码( 2)
  1776. //,5f2a02交易货币代码( 2), 9a03交易日期( 3), 9c01交易类型( 1),
  1777. //9f2103交易时间( 3), 9f4e14商户名称( 14,16进制的,换成10进制是20字节)
  1778. for (int i = 0; i < cdol.lenth;)
  1779. {
  1780. if (IsDoubleByteFlag(cdol.value[i]))
  1781. {
  1782. ZeroMemory(pShow,32);
  1783. HexBuf2StrBuf(cdol.value+i,&pShow,2);
  1784. //Dbg("double flag:[%s]",pShow);
  1785. //memcpy(tmpBuf+lenCDOL,test,cdol.value[i+2]);
  1786. ICData authAmount(false,0x9f,0x02),otherAmount(false,0x9f,0x03),ctyCode(false,0x9f,0x1a)
  1787. ,currCode(false,0x5f,0x2a),transTime(false,0x9f,0x21),custName(false,0x9f,0x4e);
  1788. if (cdol.value[i] == 0x9f && cdol.value[i+1] == 0x02)
  1789. {
  1790. if (ToFindTagValue(m_vBusData,authAmount,false,0,m_vBusData.size()-1) != -1)
  1791. memcpy(tmpBuf+lenCDOL,authAmount.value,authAmount.lenth);
  1792. }
  1793. else if (cdol.value[i] == 0x9f && cdol.value[i+1] == 0x03)
  1794. {
  1795. if (ToFindTagValue(m_vBusData,otherAmount,false,0,m_vBusData.size()-1) != -1)
  1796. memcpy(tmpBuf+lenCDOL,otherAmount.value,otherAmount.lenth);
  1797. }
  1798. //terminal country code "0156"
  1799. else if (cdol.value[i] == 0x9f && cdol.value[i+1] == 0x1a)
  1800. {
  1801. if (ToFindTagValue(m_vBusData,ctyCode,false,0,m_vBusData.size()-1) != -1)
  1802. memcpy(tmpBuf+lenCDOL,ctyCode.value,ctyCode.lenth);
  1803. }
  1804. //currency code
  1805. else if (cdol.value[i] == 0x5f && cdol.value[i+1] == 0x2a)
  1806. {
  1807. if (ToFindTagValue(m_vBusData,currCode,false,0,m_vBusData.size()-1) != -1)
  1808. memcpy(tmpBuf+lenCDOL,currCode.value,currCode.lenth);
  1809. }
  1810. else if (cdol.value[i] == 0x9f && cdol.value[i+1] == 0x37)
  1811. {
  1812. unsigned int ram;
  1813. #if (defined(_WIN32) || defined(_WIN64))
  1814. int ret = rand_s(&ram);
  1815. if(ret == 0)
  1816. #else
  1817. ram = rand();
  1818. if(ram != 0)
  1819. #endif //_WIN32
  1820. {
  1821. m_randData[0] = tmpBuf[lenCDOL] = ((ram&0xff000000)>>24);
  1822. m_randData[1] = tmpBuf[lenCDOL+1] = ((ram&0x00ff0000)>>16);
  1823. m_randData[2] = tmpBuf[lenCDOL+2] = ((ram&0x0000ff00)>>8);
  1824. m_randData[3] = tmpBuf[lenCDOL+3] = (ram&0x000000ff);
  1825. }
  1826. else
  1827. {
  1828. Dbg("get ram err %d",GetLastError());
  1829. }
  1830. }
  1831. //trans time
  1832. else if (cdol.value[i] == 0x9f && cdol.value[i+1] == 0x21)
  1833. {
  1834. if (ToFindTagValue(m_vBusData,transTime,false,0,m_vBusData.size()-1) != -1)
  1835. memcpy(tmpBuf+lenCDOL,transTime.value,transTime.lenth);
  1836. }
  1837. else if (cdol.value[i] == 0x9f && cdol.value[i+1] == 0x4e)
  1838. {
  1839. if (ToFindTagValue(m_vBusData,custName,false,0,m_vBusData.size()-1) != -1)
  1840. memcpy(tmpBuf+lenCDOL,custName.value,custName.lenth);
  1841. }
  1842. else
  1843. {
  1844. Dbg("unknown cdol tag...%x%x",cdol.value[i],cdol.value[i+1]);
  1845. memset(tmpBuf+lenCDOL,0,cdol.value[i+2]);
  1846. }
  1847. lenCDOL += cdol.value[i+2];
  1848. i += 3;
  1849. }
  1850. else
  1851. {
  1852. ZeroMemory(pShow,32);
  1853. HexBuf2StrBuf(cdol.value+i,&pShow,1);
  1854. //Dbg("single flag:[%s]",pShow);
  1855. ICData transDate(false,0x9a,0x00),transType(false,0x9c,0x00);
  1856. if (cdol.value[i] == 0x95)
  1857. {
  1858. tmpBuf[lenCDOL] = 0x00;
  1859. tmpBuf[lenCDOL+1] = 0x00;
  1860. tmpBuf[lenCDOL+2] = 0x00;
  1861. tmpBuf[lenCDOL+3] = 0x00;
  1862. tmpBuf[lenCDOL+4] = 0x00;
  1863. }
  1864. else if (cdol.value[i] == 0x9a)
  1865. {
  1866. if (ToFindTagValue(m_vBusData,transDate,false,0,m_vBusData.size()-1) != -1)
  1867. memcpy(tmpBuf+lenCDOL,transDate.value,transDate.lenth);
  1868. }
  1869. else if (cdol.value[i] == 0x9c)
  1870. {
  1871. if (ToFindTagValue(m_vBusData,transType,false,0,m_vBusData.size()-1) != -1)
  1872. memcpy(tmpBuf+lenCDOL,transType.value,transType.lenth);
  1873. }
  1874. else
  1875. {
  1876. Dbg("unknown cdol tag...%x",cdol.value[i]);
  1877. memset(tmpBuf+lenCDOL,0,cdol.value[i+1]);
  1878. }
  1879. //memcpy(tmpBuf+lenCDOL,test,cdol.value[i+1]);
  1880. lenCDOL += cdol.value[i+1];
  1881. i += 2;
  1882. }
  1883. }
  1884. pBYTE = new BYTE[lenCDOL];
  1885. memcpy(pBYTE,tmpBuf,lenCDOL);
  1886. char* show = new char[MAX_TEST_SHOW];
  1887. ZeroMemory(show,MAX_TEST_SHOW);
  1888. HexBuf2StrBuf(pBYTE,&show,lenCDOL);
  1889. Dbg("filled..cdol[%s],len[%d]",show,lenCDOL);
  1890. return true;
  1891. }
  1892. int CCardProcess::IssueBankAuth(CardReadType eType,DeviceBaseClass *pCardX)
  1893. {
  1894. LOG_FUNCTION();
  1895. //如果联机授权响应包含发卡行认证数据&卡和终端支持发卡行认证
  1896. if (AIP_ISSUER_BANK_AUTH != m_AIP[0]&AIP_ISSUER_BANK_AUTH)
  1897. {
  1898. Dbg("Term don't support issuer bank authorize.");
  1899. return -1;//oiltmp ?
  1900. }
  1901. m_TSI[0] |= TSI_ISSUER_BANK_AUTH;
  1902. BYTE p1,p2;
  1903. p1 = p2 = 0x00;
  1904. LPBYTE pData = NULL;
  1905. int iH1,iH2,iL1,iL2,sizeARPC;
  1906. iH1 = iH2 = iL1 = iL2 = sizeARPC = 0;
  1907. vector<FormUnit>::iterator it;
  1908. bool bARPC,bARC;
  1909. bARPC = bARC = false;
  1910. for (it = m_vOnlineReplyData.begin(); it != m_vOnlineReplyData.end(); ++it)
  1911. {
  1912. if (!_strnicmp(it->pFst,"ARPC",it->fstSize))
  1913. {
  1914. pData = new BYTE[it->sndSize/2+1+1];
  1915. ZeroMemory(pData,it->sndSize/2+1+1);
  1916. int ret = StrBuf2HexBuf(it->pSnd,&pData);
  1917. if (ret == 0)
  1918. {
  1919. Dbg("Wrong ARPC size.");
  1920. delete []pData;
  1921. return -1;
  1922. }
  1923. sizeARPC = it->sndSize/2;
  1924. bARPC = true;
  1925. }
  1926. else if (!_strnicmp(it->pFst,"ARC",it->fstSize))
  1927. {
  1928. iH1 = char2int(*(it->pSnd));
  1929. iL1 = char2int(*(it->pSnd+1));
  1930. iH2 = char2int(*(it->pSnd+2));
  1931. iL2 = char2int(*(it->pSnd+3));
  1932. bARC = true;
  1933. }
  1934. }
  1935. if (!bARPC || !bARC)
  1936. {
  1937. Dbg("no arpc or arc(%d)(%d).",bARPC,bARC);
  1938. return -1;
  1939. }
  1940. pData[sizeARPC] = iH1*16 + iL1;
  1941. pData[sizeARPC+1] = iH2*16 + iL2;
  1942. BYTE lc = sizeARPC + 2;
  1943. Dbg("issue bank authorize lc [%d]",lc);
  1944. ConstructAPDU(CLS_EX_AUTH,INS_EX_AUTH,p1,p2,lc,pData,NULL);
  1945. char* show = new char[MAX_TEST_SHOW];
  1946. ZeroMemory(show,MAX_TEST_SHOW);
  1947. int lenRecv;
  1948. BYTE* recvBuf = new BYTE[1024];
  1949. CmdInfo cmdSend,cmdRecv;
  1950. cmdSend.dwSize = m_lenAPDU;
  1951. memset(cmdSend.data,0,MAX_IC_BUFFER_SIZE);
  1952. memcpy(cmdSend.data,m_APDUsendBuf,m_lenAPDU);
  1953. ErrorCodeEnum eErr;
  1954. if (eType == CARD_MACHINE_ISSUER)
  1955. {
  1956. pCardI = dynamic_cast<CardIssuerClass*>(pCardX);
  1957. eErr = pCardI->ICCommand(cmdSend,cmdRecv);
  1958. }
  1959. else if (eType == CARD_MACHINE_ISSUER_RF)
  1960. {
  1961. pCardI = dynamic_cast<CardIssuerClass*>(pCardX);
  1962. eErr = pCardI->RFTypeABCommand(cmdSend, cmdRecv);
  1963. }
  1964. else if (eType == CARD_MACHINE_SWIPER || eType == CARD_MACHINE_SWIPER_RF)
  1965. {
  1966. pCardS = dynamic_cast<CardSwiperClass*>(pCardX);
  1967. if (eType == CARD_MACHINE_SWIPER)
  1968. eErr = pCardS->ICCommand(cmdSend,cmdRecv);
  1969. else if (eType == CARD_MACHINE_SWIPER_RF)
  1970. eErr = pCardS->RFTypeABCommand(cmdSend,cmdRecv);
  1971. }
  1972. else if (eType == CARD_MACHINE_RFIC)
  1973. {
  1974. pCardR = dynamic_cast<RFICClass*>(pCardX);
  1975. eErr = pCardR->RFTypeABCommand(cmdSend,cmdRecv);
  1976. }
  1977. if (eErr == Error_Succeed)
  1978. {
  1979. memcpy(recvBuf,cmdRecv.data,cmdRecv.dwSize);
  1980. lenRecv = cmdRecv.dwSize;
  1981. HexBuf2StrBuf(recvBuf,&show,lenRecv);
  1982. Dbg("issue bank auth[%s]",show);
  1983. if (recvBuf[lenRecv-2] == 0x90 && recvBuf[lenRecv-1] == 0x00)
  1984. {
  1985. }
  1986. else
  1987. {
  1988. Dbg("issue bank auth failed.");
  1989. m_TVR[4] |= TVR_ISSUER_BANK_AUTH_FAIL;
  1990. return -1;
  1991. }
  1992. }
  1993. return 0;
  1994. //if (recvBuf != NULL)
  1995. //{
  1996. // delete []recvBuf;
  1997. // recvBuf = NULL;
  1998. //}
  1999. }
  2000. void CCardProcess::ExecuteIssuerScript(CardReadType eType,DeviceBaseClass *pCardX)
  2001. {
  2002. vector<FormUnit>::iterator it;
  2003. PBYTE pScript = NULL;
  2004. for (it = m_vOnlineReplyData.begin(); it != m_vOnlineReplyData.end(); ++it)
  2005. {
  2006. if (!_strnicmp(it->pFst,"SCRIPT",it->fstSize))
  2007. {
  2008. pScript = new BYTE[it->sndSize/2+1];
  2009. ZeroMemory(pScript,it->sndSize/2+1);
  2010. int ret = StrBuf2HexBuf(it->pSnd,&pScript);
  2011. if (ret == 0)
  2012. {
  2013. Dbg("Wrong issuer script size.");
  2014. delete []pScript;
  2015. return;
  2016. }
  2017. if (pScript[0] != 0x72)
  2018. {
  2019. Dbg("Wrong issuer script.");
  2020. delete []pScript;
  2021. return;
  2022. }
  2023. }
  2024. }
  2025. if (pScript == NULL)
  2026. {
  2027. Dbg("No script?");
  2028. return;
  2029. }
  2030. CardIssuerClass *pCardI;
  2031. CardSwiperClass *pCardS;
  2032. ErrorCodeEnum eErr;
  2033. if (eType == CARD_MACHINE_ISSUER || eType == CARD_MACHINE_ISSUER_RF)
  2034. {
  2035. pCardI = dynamic_cast<CardIssuerClass*>(pCardX);
  2036. }
  2037. else if (eType == CARD_MACHINE_SWIPER || eType == CARD_MACHINE_SWIPER_RF)
  2038. {
  2039. pCardS = dynamic_cast<CardSwiperClass*>(pCardX);
  2040. }
  2041. else if (eType == CARD_MACHINE_RFIC)
  2042. {
  2043. pCardR = dynamic_cast<RFICClass*>(pCardX);
  2044. }
  2045. int len = pScript[1];
  2046. for (int i = 2; i < len;)
  2047. {
  2048. if (pScript[i] == 0x86)
  2049. {
  2050. i++;
  2051. int cmdLen = pScript[i];
  2052. CmdInfo cmdSend,cmdRecv;
  2053. cmdSend.dwSize = cmdLen;
  2054. memset(cmdSend.data,0,MAX_IC_BUFFER_SIZE);
  2055. i++;
  2056. memcpy(cmdSend.data,(void*)(pScript+i),cmdLen);
  2057. //the following just for test oiltmp
  2058. char* show = new char[MAX_TEST_SHOW];
  2059. ZeroMemory(show,MAX_TEST_SHOW);
  2060. HexBuf2StrBuf(cmdSend.data,&show,cmdSend.dwSize);
  2061. Dbg("issuer script[%s].",show);
  2062. delete []show;
  2063. //
  2064. if (eType == CARD_MACHINE_ISSUER)
  2065. {
  2066. eErr = pCardI->ICCommand(cmdSend,cmdRecv);
  2067. }
  2068. else if (eType == CARD_MACHINE_ISSUER_RF)
  2069. {
  2070. eErr = pCardI->RFTypeABCommand(cmdSend, cmdRecv);
  2071. }
  2072. else if (eType == CARD_MACHINE_SWIPER)
  2073. {
  2074. eErr = pCardS->ICCommand(cmdSend,cmdRecv);
  2075. }
  2076. else if (eType == CARD_MACHINE_RFIC)
  2077. {
  2078. eErr = pCardR->RFTypeABCommand(cmdSend,cmdRecv);
  2079. }
  2080. else if (eType == CARD_MACHINE_SWIPER_RF)
  2081. {
  2082. eErr = pCardS->RFTypeABCommand(cmdSend,cmdRecv);
  2083. }
  2084. if (eErr == Error_Succeed)
  2085. {
  2086. if (cmdRecv.data[cmdRecv.dwSize-2] == 0x90 || cmdRecv.data[cmdRecv.dwSize-2] == 0x62)
  2087. {
  2088. Dbg("Execute script command [%d][%d].",cmdRecv.data[cmdRecv.dwSize-2],cmdRecv.data[cmdRecv.dwSize-1]);
  2089. }
  2090. else
  2091. {
  2092. Dbg("Execute script command error [%d][%d].",cmdRecv.data[cmdRecv.dwSize-2],cmdRecv.data[cmdRecv.dwSize-1]);
  2093. }
  2094. }
  2095. else
  2096. {
  2097. CSimpleStringA errMsg;
  2098. QueryLastErr(pCardX, errMsg);
  2099. Dbg("<ExecuteIssuerScript>, Execute script command error [%d][%d].errCode:%d, errMsg:%s",
  2100. cmdRecv.data[cmdRecv.dwSize - 2], cmdRecv.data[cmdRecv.dwSize - 1], eErr, errMsg.GetData());
  2101. }
  2102. i += cmdLen;
  2103. }
  2104. else
  2105. i++;
  2106. }
  2107. if (pScript != NULL)
  2108. delete []pScript;
  2109. }
  2110. int CCardProcess::TransEnd(CardReadType eType,DeviceBaseClass *pCardX,bool bCDA)
  2111. {
  2112. LOG_FUNCTION();
  2113. int procResult = 0;
  2114. //second genarate ac
  2115. ICData cdol2(false,0x8d,0x00);
  2116. if (ToFindTagValue(m_vICData,cdol2,0,0,m_vICData.size()-1) == -1)
  2117. {
  2118. Dbg("can't find cdol2.");
  2119. return -1;
  2120. }
  2121. //find arc to decide P1 (currently no CDA)
  2122. //'00','10','11'-->TC
  2123. //'01','02',other -->AAC
  2124. BYTE p1;
  2125. vector<FormUnit>::iterator it;
  2126. for (it = m_vOnlineReplyData.begin(); it != m_vOnlineReplyData.end(); ++it)
  2127. {
  2128. if (!_strnicmp(it->pFst,"ARC",it->fstSize))
  2129. {
  2130. if (!_strnicmp(it->pSnd,"00",it->sndSize) || !strnicmp(it->pSnd,"10",it->sndSize) || !strnicmp(it->pSnd,"11",it->sndSize))
  2131. p1 = P1_GENAC_TC;
  2132. else
  2133. p1 = P1_GENAC_AAC;
  2134. }
  2135. }
  2136. PBYTE pCDOL2;
  2137. DWORD lenCDOL2=0;
  2138. FillCDOL(cdol2,pCDOL2,lenCDOL2);
  2139. ConstructAPDU(CLS_GENAC,INS_GENAC,p1,0x00,lenCDOL2,pCDOL2,0x00);
  2140. int lenRecv;
  2141. LPBYTE recvBuf = new BYTE[1024];
  2142. char* show = new char[MAX_TEST_SHOW];
  2143. ZeroMemory(show,MAX_TEST_SHOW);
  2144. CmdInfo cmdSend,cmdRecv;
  2145. cmdSend.dwSize = m_lenAPDU;
  2146. memset(cmdSend.data,0,MAX_IC_BUFFER_SIZE);
  2147. memcpy(cmdSend.data,m_APDUsendBuf,m_lenAPDU);
  2148. HexBuf2StrBuf(cmdSend.data,&show,m_lenAPDU);
  2149. Dbg("2nd GenAc cmd[%s]",show);
  2150. ErrorCodeEnum eErr;
  2151. if (eType == CARD_MACHINE_ISSUER)
  2152. {
  2153. pCardI = dynamic_cast<CardIssuerClass*>(pCardX);
  2154. eErr = pCardI->ICCommand(cmdSend,cmdRecv);
  2155. }
  2156. else if (eType == CARD_MACHINE_ISSUER_RF)
  2157. {
  2158. pCardI = dynamic_cast<CardIssuerClass*>(pCardX);
  2159. eErr = pCardI->RFTypeABCommand(cmdSend, cmdRecv);
  2160. }
  2161. else if (eType == CARD_MACHINE_SWIPER || eType == CARD_MACHINE_SWIPER_RF)
  2162. {
  2163. pCardS = dynamic_cast<CardSwiperClass*>(pCardX);
  2164. if (eType == CARD_MACHINE_SWIPER)
  2165. eErr = pCardS->ICCommand(cmdSend,cmdRecv);
  2166. else if (eType == CARD_MACHINE_SWIPER_RF)
  2167. eErr = pCardS->RFTypeABCommand(cmdSend,cmdRecv);
  2168. }
  2169. else if (eType == CARD_MACHINE_RFIC)
  2170. {
  2171. pCardR = dynamic_cast<RFICClass*>(pCardX);
  2172. eErr = pCardR->RFTypeABCommand(cmdSend,cmdRecv);
  2173. }
  2174. Dbg("2nd gen ac rc[%d]",eErr);
  2175. bool bGenAC = false;
  2176. if (eErr == Error_Succeed)
  2177. {
  2178. memcpy(recvBuf,cmdRecv.data,cmdRecv.dwSize);
  2179. lenRecv = cmdRecv.dwSize;
  2180. ZeroMemory(show,MAX_TEST_SHOW);
  2181. HexBuf2StrBuf(recvBuf,&show,lenRecv);
  2182. Dbg("2ndGenAc recv[%s]",show);
  2183. if (recvBuf[lenRecv-2] == 0x61)
  2184. {
  2185. BYTE btLen = recvBuf[lenRecv-1];
  2186. ConstructAPDU(CLS_GET_RESPONSE,INS_GET_RESPONSE,0x00,0x00,NULL,NULL,&btLen);
  2187. CmdInfo cmdSend,cmdRecv;
  2188. cmdSend.dwSize = m_lenAPDU;
  2189. memset(cmdSend.data,0,MAX_IC_BUFFER_SIZE);
  2190. memcpy(cmdSend.data,m_APDUsendBuf,m_lenAPDU);
  2191. ZeroMemory(show,MAX_TEST_SHOW);
  2192. HexBuf2StrBuf(cmdSend.data,&show,m_lenAPDU);
  2193. Dbg("get response cmd[%s]",show);
  2194. if (eType == CARD_MACHINE_ISSUER)
  2195. {
  2196. eErr = pCardI->ICCommand(cmdSend,cmdRecv);
  2197. }
  2198. else if (eType == CARD_MACHINE_ISSUER_RF)
  2199. {
  2200. eErr = pCardI->RFTypeABCommand(cmdSend, cmdRecv);
  2201. }
  2202. else if (eType == CARD_MACHINE_SWIPER)
  2203. {
  2204. eErr = pCardS->ICCommand(cmdSend,cmdRecv);
  2205. }
  2206. else if (eType == CARD_MACHINE_RFIC)
  2207. {
  2208. eErr = pCardR->RFTypeABCommand(cmdSend,cmdRecv);
  2209. }
  2210. else if (eType == CARD_MACHINE_SWIPER_RF)
  2211. {
  2212. eErr = pCardS->RFTypeABCommand(cmdSend,cmdRecv);
  2213. }
  2214. if (eErr == Error_Succeed)
  2215. {
  2216. ZeroMemory(show,MAX_TEST_SHOW);
  2217. ZeroMemory(recvBuf,1024);
  2218. memcpy(recvBuf,cmdRecv.data,cmdRecv.dwSize);
  2219. lenRecv = cmdRecv.dwSize;
  2220. HexBuf2StrBuf(recvBuf,&show,lenRecv);
  2221. //Dbg("get response recv[%s]",show);
  2222. if (recvBuf[lenRecv-2] == 0x90 && recvBuf[lenRecv-1] == 0x00)
  2223. bGenAC = true;
  2224. }
  2225. }
  2226. else if (recvBuf[lenRecv-2] == 0x90 && recvBuf[lenRecv-1] == 0x00)
  2227. bGenAC = true;
  2228. else if (recvBuf[lenRecv-2] == 0x69 && recvBuf[lenRecv-1] == 0x85)
  2229. {
  2230. Dbg("no more than 2 generate ac command");
  2231. return -1;
  2232. }
  2233. if (bGenAC)
  2234. {
  2235. ExecuteIssuerScript(eType,pCardX);
  2236. //to send back issuer script execute result? oiltmp
  2237. //form2 '77'暂不考虑
  2238. //form1 '80'
  2239. if (recvBuf[0] == 0x80)
  2240. {
  2241. //2nd gen ac response:CID,ATC,AC,[ISSUER...],CVR
  2242. //80 1e 80 0008 328ab54bfc986b85 07010103a0b000010a010000000000754048769000
  2243. //密文信息数据 1B,ATC 2B,AC 8B,发卡行数据 opt
  2244. //暂不执行CDA oiltmp
  2245. BYTE cryptInfo = recvBuf[2];
  2246. if (p1 == P1_GENAC_AAC)
  2247. {
  2248. //reject trans
  2249. }
  2250. else if (p1 == P1_GENAC_TC)
  2251. {
  2252. //if response is tc,as no CDA,trans end
  2253. //else if response is aac,reject trans and send reversal
  2254. if ((cryptInfo&0xc0) == P1_GENAC_TC)
  2255. procResult = 0;
  2256. else if ((cryptInfo&0xc0) == P1_GENAC_AAC)
  2257. {
  2258. //to be added... oiltmp
  2259. procResult = 1;
  2260. }
  2261. }
  2262. }
  2263. }
  2264. }
  2265. if (recvBuf != NULL)
  2266. delete []recvBuf;
  2267. if (show != NULL)
  2268. delete []show;
  2269. return procResult;
  2270. }
  2271. void CCardProcess::GetBaseInfoNotInRecord(CardReadType eType,DeviceBaseClass *pCardX)
  2272. {
  2273. char* show = new char[MAX_TEST_SHOW];
  2274. ZeroMemory(show,MAX_TEST_SHOW);
  2275. BYTE zz = 0x00;
  2276. BYTE le = 0x00;
  2277. ConstructAPDU(0x80,0xca,0x9f,0x36,NULL,NULL,&le);
  2278. int lenRecv;
  2279. LPBYTE recvBuf = new BYTE[1024];
  2280. CmdInfo cmdSend,cmdRecv;
  2281. cmdSend.dwSize = m_lenAPDU;
  2282. memset(cmdSend.data,0,MAX_IC_BUFFER_SIZE);
  2283. memcpy(cmdSend.data,m_APDUsendBuf,m_lenAPDU);
  2284. ErrorCodeEnum eErr;
  2285. if (eType == CARD_MACHINE_ISSUER)
  2286. {
  2287. pCardI = dynamic_cast<CardIssuerClass*>(pCardX);
  2288. eErr = pCardI->ICCommand(cmdSend,cmdRecv);
  2289. }
  2290. else if (eType == CARD_MACHINE_ISSUER_RF)
  2291. {
  2292. pCardI = dynamic_cast<CardIssuerClass*>(pCardX);
  2293. eErr = pCardI->RFTypeABCommand(cmdSend, cmdRecv);
  2294. }
  2295. else if (eType == CARD_MACHINE_SWIPER || eType == CARD_MACHINE_SWIPER_RF)
  2296. {
  2297. pCardS = dynamic_cast<CardSwiperClass*>(pCardX);
  2298. if (eType == CARD_MACHINE_SWIPER)
  2299. eErr = pCardS->ICCommand(cmdSend,cmdRecv);
  2300. else if (eType == CARD_MACHINE_SWIPER_RF)
  2301. eErr = pCardS->RFTypeABCommand(cmdSend,cmdRecv);
  2302. }
  2303. else if (eType == CARD_MACHINE_RFIC)
  2304. {
  2305. pCardR = dynamic_cast<RFICClass*>(pCardX);
  2306. eErr = pCardR->RFTypeABCommand(cmdSend,cmdRecv);
  2307. }
  2308. if (eErr == Error_Succeed)
  2309. {
  2310. memcpy(recvBuf,cmdRecv.data,cmdRecv.dwSize);
  2311. lenRecv = cmdRecv.dwSize;
  2312. HexBuf2StrBuf(recvBuf,&show,lenRecv);
  2313. Dbg("base info not in record[%s]",show);
  2314. if (recvBuf[lenRecv-2] == 0x90 && recvBuf[lenRecv-1] == 0x00)
  2315. {
  2316. }
  2317. }
  2318. }
  2319. void CCardProcess::SplitBusinessData(const char *pData,int size)
  2320. {
  2321. //not support ultra-lenth!!!oilyang
  2322. PBYTE pBtData = new BYTE[MAX_IC_BUFFER_SIZE];
  2323. ZeroMemory(pBtData,MAX_IC_BUFFER_SIZE);
  2324. int len = StrBuf2HexBuf(pData,&pBtData);
  2325. for (int i = 0; i < len;)
  2326. {
  2327. if (IsDoubleByteFlag(*(pBtData+i)))
  2328. {
  2329. int dataLen = *(pBtData+i+2);
  2330. ICData data(false,*(pBtData+i),*(pBtData+i+1));
  2331. data.value = new BYTE[dataLen];
  2332. data.lenth = dataLen;
  2333. ZeroMemory(data.value,dataLen);
  2334. memcpy(data.value,pBtData+i+3,dataLen);
  2335. m_vBusData.push_back(data);
  2336. i = i + dataLen + 2 + 1;
  2337. }
  2338. else
  2339. {
  2340. int dataLen = *(pBtData+i+1);
  2341. ICData data(false,*(pBtData+i),0x00);
  2342. data.value = new BYTE[dataLen];
  2343. data.lenth = dataLen;
  2344. ZeroMemory(data.value,dataLen);
  2345. memcpy(data.value,pBtData+i+2,dataLen);
  2346. m_vBusData.push_back(data);
  2347. i = i + dataLen + 1 + 1;
  2348. }
  2349. }
  2350. return ;
  2351. }
  2352. int CCardProcess::SplitOnlineReplyData(const char *pData,int size)
  2353. {
  2354. //"ARPC,34A2BE5D78C32EE8|ARC,00|SCRIPT,32423424"
  2355. m_vOnlineReplyData.clear();
  2356. int ret = SplitFormString(m_vOnlineReplyData,pData,size,',','|');
  2357. if (ret != 0)
  2358. {
  2359. Dbg("Split form string failed.");
  2360. return -1;
  2361. }
  2362. return 0;
  2363. }
  2364. int CCardProcess::DetectAndReadICData(CardReadType eType, DeviceBaseClass* pCardX, CAutoArray<CSimpleStringA>pAIDs, int& cardType, bool bIssue)
  2365. {
  2366. LOG_FUNCTION();
  2367. bool bIC = DetectIfICCard(eType,pCardX,cardType,bIssue);
  2368. if (!bIC)
  2369. {
  2370. Dbg("not ic card.");
  2371. return -1;
  2372. }
  2373. int bGetICData = -3;
  2374. bGetICData = GetICDataFromCard(eType, pCardX, pAIDs);
  2375. if (bGetICData != 0)
  2376. {
  2377. Dbg("GetICDataFromCard failed.");
  2378. return bGetICData;
  2379. }
  2380. GetBaseInfoNotInRecord(eType,pCardX);
  2381. return 0;
  2382. }
  2383. int CCardProcess::GetICDataFromCard(CardReadType eType,DeviceBaseClass *pCardX, CAutoArray<CSimpleStringA> pAIDs)
  2384. {
  2385. LOG_FUNCTION();
  2386. CSimpleStringA errMsg(true);
  2387. DWORD errBuildAppList = 0;
  2388. AIDData aidData;
  2389. aidData.aid = new BYTE[32];//oiltmp
  2390. memset(aidData.aid, 0, 32);
  2391. for (int index = 0; index < pAIDs.GetCount(); ++index)
  2392. {
  2393. int aidLen = StrBuf2HexBuf(pAIDs[index].GetData(), &aidData.aid);
  2394. Dbg("str2hex len[%d]", aidLen);
  2395. aidData.len = aidLen;
  2396. char* aidTest = new char[128];
  2397. int aidStrLen = HexBuf2StrBuf(aidData.aid, &aidTest, 5);
  2398. Dbg("hex2str len[%d]", aidStrLen);
  2399. Dbg("aid[%s][%s]", pAIDs[index].GetData(), aidTest);
  2400. vector<AIDData> vAIDs;
  2401. vAIDs.push_back(aidData);
  2402. ErrorCodeEnum eErr = Error_Unexpect;
  2403. if (eType == CARD_MACHINE_ISSUER)
  2404. {
  2405. pCardI = dynamic_cast<CardIssuerClass*>(pCardX);
  2406. errBuildAppList = CardIssuer_UserErrorCode_BuildSupportedAppList_Failed;
  2407. }
  2408. else if (eType == CARD_MACHINE_ISSUER_RF)
  2409. {
  2410. pCardI = dynamic_cast<CardIssuerClass*>(pCardX);
  2411. errBuildAppList = ContactlessCard_UserErrorCode_BuildSupportedAppList_Failed;
  2412. }
  2413. else if (eType == CARD_MACHINE_SWIPER || eType == CARD_MACHINE_SWIPER_RF)
  2414. {
  2415. pCardS = dynamic_cast<CardSwiperClass*>(pCardX);
  2416. //errBuildAppList = CardSwiper_UserErrorCode_BuildSupportedAppList_Failed;
  2417. }
  2418. else if (eType == CARD_MACHINE_RFIC)
  2419. {
  2420. pCardR = dynamic_cast<RFICClass*>(pCardX);
  2421. errBuildAppList = ContactlessCard_UserErrorCode_BuildSupportedAppList_Failed;
  2422. }
  2423. eErr = BuildSupportedAppList(eType, pCardX, vAIDs);
  2424. if (eErr != Error_Succeed)
  2425. {
  2426. QueryLastErr(pCardX, errMsg);
  2427. errMsg = CSimpleStringA::Format("GetICDataFromCard::BuildSupportedAppList failed(%s):%s.", SpStrError(eErr), errMsg.GetData());
  2428. LogWarn(Severity_Middle, Error_Unexpect, errBuildAppList, errMsg.GetData());
  2429. if(eErr == Error_Interact)
  2430. return -2;
  2431. else
  2432. continue;
  2433. }
  2434. //Dbg("[%s]",testIC);
  2435. eErr = AppSelected(eType, pCardX, vAIDs.at(0).aid, vAIDs.at(0).len);
  2436. if (eErr == Error_Succeed)
  2437. return 0;
  2438. else
  2439. {
  2440. QueryLastErr(pCardX, errMsg);
  2441. errMsg = CSimpleStringA::Format("GetICDataFromCard::AppSelected failed(%s):%s.", SpStrError(eErr), errMsg.GetData());
  2442. LogWarn(Severity_Middle, Error_Unexpect, errBuildAppList, errMsg.GetData());
  2443. if(eErr == Error_Interact)
  2444. return -2;
  2445. else
  2446. continue;
  2447. }
  2448. }
  2449. return -3;
  2450. }
  2451. int CCardProcess::ConstructARQCData(const char *pATC, char *&pDataToARQC, char *&pSomeICData)
  2452. {
  2453. //9f0206 9f0306 9f1a02 9505 5f2a02 9a03 9c01 9f3704 9f2103 9f4e14
  2454. //9f0206授权金额 ( 6 ), 9f0306其它金额 ( 6), 9f1a02终端国家代码( 2)
  2455. //,5f2a02交易货币代码( 2), 9a03交易日期( 3), 9c01交易类型( 1),
  2456. pDataToARQC = new char[1024];
  2457. ZeroMemory(pDataToARQC,1024);
  2458. Dbg("atc %s",pATC);
  2459. ICData authAmount(false,0x9f,0x02),otherAmount(false,0x9f,0x03),ctyCode(false,0x9f,0x1a)
  2460. ,currCode(false,0x5f,0x2a),transTime(false,0x9f,0x21),transDate(false,0x9a,0x00)
  2461. ,transType(false,0x9c,0x00);
  2462. PBYTE tmpARQCData = new BYTE[512];
  2463. PBYTE tmpSomeICData = new BYTE[512];
  2464. ZeroMemory(tmpARQCData,512);
  2465. ZeroMemory(tmpSomeICData, 512);
  2466. int len = 0, iclen = 0;
  2467. tmpSomeICData[iclen++] = 0x9f;
  2468. tmpSomeICData[iclen++] = 0x02;
  2469. tmpSomeICData[iclen++] = 0x06;
  2470. if (ToFindTagValue(m_vBusData, authAmount, false, 0, m_vBusData.size() - 1) != -1)
  2471. {
  2472. memcpy(tmpARQCData + len, authAmount.value, authAmount.lenth);
  2473. memcpy(tmpSomeICData + iclen, authAmount.value, authAmount.lenth);
  2474. }
  2475. else
  2476. {
  2477. memset(tmpARQCData + len, 0, 6);
  2478. memset(tmpSomeICData + iclen, 0, 6);
  2479. }
  2480. len += 6;
  2481. iclen += 6;
  2482. tmpSomeICData[iclen++] = 0x9f;
  2483. tmpSomeICData[iclen++] = 0x03;
  2484. tmpSomeICData[iclen++] = 0x06;
  2485. if (ToFindTagValue(m_vBusData, otherAmount, false, 0, m_vBusData.size() - 1) != -1)
  2486. {
  2487. memcpy(tmpARQCData + len, otherAmount.value, otherAmount.lenth);
  2488. memcpy(tmpSomeICData + iclen, otherAmount.value, otherAmount.lenth);
  2489. }
  2490. else
  2491. {
  2492. memset(tmpARQCData + len, 0, 6);
  2493. memset(tmpSomeICData + iclen, 0, 6);
  2494. }
  2495. len += 6;
  2496. iclen += 6;
  2497. //terminal country code "0156"
  2498. tmpSomeICData[iclen++] = 0x9f;
  2499. tmpSomeICData[iclen++] = 0x1a;
  2500. tmpSomeICData[iclen++] = 0x02;
  2501. if (ToFindTagValue(m_vBusData, ctyCode, false, 0, m_vBusData.size() - 1) != -1)
  2502. {
  2503. memcpy(tmpARQCData + len, ctyCode.value, ctyCode.lenth);
  2504. memcpy(tmpSomeICData + iclen, ctyCode.value, ctyCode.lenth);
  2505. }
  2506. else
  2507. {
  2508. memset(tmpARQCData + len, 0, 2);
  2509. memset(tmpSomeICData + iclen, 0, 2);
  2510. }
  2511. len += 2;
  2512. iclen += 2;
  2513. //TVR
  2514. tmpSomeICData[iclen++] = 0x95;
  2515. tmpSomeICData[iclen++] = 0x05;
  2516. memcpy(tmpARQCData + len,m_TVR,5);
  2517. memcpy(tmpSomeICData + iclen, m_TVR, 5);
  2518. len += 5;
  2519. iclen += 5;
  2520. //currency code
  2521. tmpSomeICData[iclen++] = 0x5f;
  2522. tmpSomeICData[iclen++] = 0x2a;
  2523. tmpSomeICData[iclen++] = 0x02;
  2524. if (ToFindTagValue(m_vBusData, currCode, false, 0, m_vBusData.size() - 1) != -1)
  2525. {
  2526. memcpy(tmpARQCData + len, currCode.value, currCode.lenth);
  2527. memcpy(tmpSomeICData + iclen, currCode.value, currCode.lenth);
  2528. }
  2529. else
  2530. {
  2531. memset(tmpARQCData + len, 0, 2);
  2532. memset(tmpSomeICData + iclen, 0, 2);
  2533. }
  2534. len += 2;
  2535. iclen += 2;
  2536. //trans date
  2537. tmpSomeICData[iclen++] = 0x9a;
  2538. tmpSomeICData[iclen++] = 0x03;
  2539. if (ToFindTagValue(m_vBusData, transDate, false, 0, m_vBusData.size() - 1) != -1)
  2540. {
  2541. memcpy(tmpARQCData + len, transDate.value, transDate.lenth);
  2542. memcpy(tmpSomeICData + iclen, transDate.value, transDate.lenth);
  2543. }
  2544. else
  2545. {
  2546. memset(tmpARQCData + len, 0, 3);
  2547. memset(tmpSomeICData + iclen, 0, 3);
  2548. }
  2549. len += 3;
  2550. iclen += 3;
  2551. //trans type
  2552. tmpSomeICData[iclen++] = 0x9c;
  2553. tmpSomeICData[iclen++] = 0x01;
  2554. if (ToFindTagValue(m_vBusData, transType, false, 0, m_vBusData.size() - 1) != -1)
  2555. {
  2556. memcpy(tmpARQCData + len, transType.value, transType.lenth);
  2557. memcpy(tmpSomeICData + iclen, transType.value, transType.lenth);
  2558. }
  2559. else
  2560. {
  2561. memset(tmpARQCData + len, 0, 1);
  2562. memset(tmpSomeICData + iclen, 0, 1);
  2563. }
  2564. len += 1;
  2565. iclen += 1;
  2566. //for temp oiltmp
  2567. tmpARQCData[len++] = m_randData[0];
  2568. tmpARQCData[len++] = m_randData[1];
  2569. tmpARQCData[len++] = m_randData[2];
  2570. tmpARQCData[len++] = m_randData[3];
  2571. tmpSomeICData[iclen++] = 0x9f;
  2572. tmpSomeICData[iclen++] = 0x37;
  2573. tmpSomeICData[iclen++] = 0x04;
  2574. tmpSomeICData[iclen++] = m_randData[0];
  2575. tmpSomeICData[iclen++] = m_randData[1];
  2576. tmpSomeICData[iclen++] = m_randData[2];
  2577. tmpSomeICData[iclen++] = m_randData[3];
  2578. //aip
  2579. tmpARQCData[len++] = m_AIP[0];
  2580. tmpARQCData[len++] = m_AIP[1];
  2581. tmpSomeICData[iclen++] = 0x82;
  2582. tmpSomeICData[iclen++] = 0x02;
  2583. tmpSomeICData[iclen++] = m_AIP[0];
  2584. tmpSomeICData[iclen++] = m_AIP[1];
  2585. //ATC
  2586. PBYTE tmpATC = new BYTE[2];
  2587. ZeroMemory(tmpATC,2);
  2588. StrBuf2HexBuf(pATC,&tmpATC);
  2589. tmpARQCData[len++] = tmpATC[0];
  2590. tmpARQCData[len++] = tmpATC[1];
  2591. tmpSomeICData[iclen++] = 0x9f;
  2592. tmpSomeICData[iclen++] = 0x36;
  2593. tmpSomeICData[iclen++] = 0x02;
  2594. tmpSomeICData[iclen++] = tmpATC[0];
  2595. tmpSomeICData[iclen++] = tmpATC[1];
  2596. //CVR oiltmp 20140415
  2597. //03A0B000
  2598. tmpARQCData[len++] = m_CVR[0];
  2599. tmpARQCData[len++] = m_CVR[1];
  2600. tmpARQCData[len++] = m_CVR[2];
  2601. tmpARQCData[len++] = m_CVR[3];
  2602. Dbg("term2arqcdata,len[%d]",len);
  2603. HexBuf2StrBuf(tmpARQCData,&pDataToARQC,len);
  2604. HexBuf2StrBuf(tmpSomeICData, &pSomeICData, iclen);
  2605. //Dbg("[%s]",pDataToARQC);
  2606. if (tmpARQCData != NULL)
  2607. {
  2608. delete[]tmpARQCData;
  2609. }
  2610. return len;
  2611. }
  2612. int CCardProcess::FindTagValue(TagVectorType eType,ICData& data,bool bLevel,int start,int end)
  2613. {
  2614. switch(eType)
  2615. {
  2616. case TAG_VECTOR_IC:
  2617. return ToFindTagValue(m_vICData,data,bLevel,start,m_vICData.size()-1);
  2618. break;
  2619. case TAG_VECTOR_BUS:
  2620. return ToFindTagValue(m_vBusData,data,bLevel,start,m_vBusData.size()-1);
  2621. break;
  2622. default:
  2623. break;
  2624. }
  2625. return -1;
  2626. }
  2627. int CCardProcess::ToFindTagValue(vector<ICData>& vData,ICData& data,bool bLevel,int start,int end)
  2628. {
  2629. //LPBYTE pData = *data;
  2630. int ret = -1;
  2631. vector<ICData>::iterator it = vData.begin();
  2632. if (start < 0 || start >= vData.size())
  2633. return ret;
  2634. for (it += start,ret = start; it != vData.end(); ++it,++ret)
  2635. {
  2636. if (ret > end)
  2637. break;
  2638. if (bLevel && data.level != it->level)
  2639. continue;
  2640. if (data.bForm && it->bForm)
  2641. {
  2642. if (data.tag[0] == it->tag[0] && data.tag[1] == it->tag[1])
  2643. {
  2644. data.bForm = true;
  2645. data.level = it->level;
  2646. return ret;
  2647. }
  2648. else
  2649. continue;
  2650. }
  2651. else if(!data.bForm && !it->bForm)
  2652. {
  2653. if (data.tag[0] == it->tag[0] && data.tag[1] == it->tag[1])
  2654. {
  2655. data.bForm = false;
  2656. data.value = new BYTE[it->lenth];
  2657. memcpy(data.value,it->value,it->lenth);
  2658. data.lenth = it->lenth;
  2659. data.level = it->level;
  2660. return ret;
  2661. }
  2662. else
  2663. continue;
  2664. }
  2665. }
  2666. return -1;
  2667. }
  2668. void CCardProcess::ConstructAPDU(BYTE cls,BYTE ins,BYTE p1,BYTE p2,BYTE lc,LPBYTE data,LPBYTE le)
  2669. {
  2670. ZeroMemory(m_APDUsendBuf,sizeof(m_APDUsendBuf));
  2671. m_lenAPDU = 0;
  2672. m_APDUsendBuf[0] = cls;
  2673. m_APDUsendBuf[1] = ins;
  2674. m_APDUsendBuf[2] = p1;
  2675. m_APDUsendBuf[3] = p2;
  2676. m_lenAPDU = 4;
  2677. if (lc != NULL && data != NULL)
  2678. {
  2679. m_APDUsendBuf[4] = lc;
  2680. m_lenAPDU++;
  2681. if (lc != 0)
  2682. {
  2683. memcpy(m_APDUsendBuf+m_lenAPDU,data,lc);
  2684. m_lenAPDU += lc;
  2685. }
  2686. }
  2687. if (le != NULL)
  2688. m_APDUsendBuf[m_lenAPDU++] = (*le);
  2689. if (!data)
  2690. {
  2691. delete[] data;
  2692. data = NULL;
  2693. }
  2694. }
  2695. int StrBuf2HexBuf(LPCTSTR strBuf,PBYTE* hexBuf)
  2696. {
  2697. int len = strlen(strBuf);
  2698. if (len == 0 || len%2 != 0)
  2699. return 0;
  2700. BYTE* buf = new BYTE[len/2];
  2701. if (buf == NULL)
  2702. return 0;
  2703. int j = 0;
  2704. for (int i = 0; i < len;)
  2705. {
  2706. int tmpVal;
  2707. sscanf(strBuf+i,"%2X",&tmpVal);
  2708. buf[j] = tmpVal;
  2709. //buf[j] = char2int(strBuf[i])*16 + char2int(strBuf[i+1]);
  2710. i += 2;
  2711. j++;
  2712. }
  2713. //memcpy(buf,strBuf,len);
  2714. *hexBuf = buf;
  2715. return j;
  2716. }
  2717. int HexBuf2StrBuf(PBYTE hexBuf,char** strBuf,DWORD len)
  2718. {
  2719. char* tmpStr = *strBuf;
  2720. int count = 0;
  2721. for (int i = 0; i < len; ++i)
  2722. {
  2723. sprintf(tmpStr+count,"%0.2X",hexBuf[i]);
  2724. count += 2;
  2725. }
  2726. return 0;
  2727. }