Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2492 lines
67 KiB

  1. /*
  2. ** Proxied Application Program Interface (API) for Windows Card
  3. **
  4. */
  5. #ifndef WIN32_LEAN_AND_MEAN
  6. #define WIN32_LEAN_AND_MEAN
  7. #endif
  8. #include <windows.h>
  9. #include "MarshalPC.h"
  10. #include "carddbg.h"
  11. LONG WINAPI SCWTransmit(SCARDHANDLE hCard, LPCBYTE lpbIn, DWORD dwIn, LPBYTE lpBOut, LPDWORD pdwOut);
  12. // Buffers for APDU exchange
  13. #define MAX_APDU 255
  14. // Command header helpers
  15. #define CLA(cla) UINT82XSCM(&phTmp->xSCM, cla, TYPE_NOTYPE_NOCOUNT)
  16. #define INS(ins) UINT82XSCM(&phTmp->xSCM, ins, TYPE_NOTYPE_NOCOUNT)
  17. #define P1(p1) UINT82XSCM(&phTmp->xSCM, p1, TYPE_NOTYPE_NOCOUNT)
  18. #define P2(p2) UINT82XSCM(&phTmp->xSCM, p2, TYPE_NOTYPE_NOCOUNT)
  19. #define Lc(lc) (phTmp->pbLc = GetSCMCrtPointer(&phTmp->xSCM), UINT82XSCM(&phTmp->xSCM, 0, TYPE_NOTYPE_NOCOUNT)) // We don't know at this time
  20. #define UPDATE_Lc(lc) *phTmp->pbLc = lc
  21. static SCODE ExtractSCODE(LPMYSCARDHANDLE phTmp, LPCBYTE abRAPDU, DWORD dwOut);
  22. //*****************************************************************************
  23. // EXPORTED API
  24. //*****************************************************************************
  25. /*
  26. ** AnA
  27. */
  28. SCODE WINAPI hScwGetPrincipalUID(SCARDHANDLE hCard, WCSTR principalName, TUID *principalUID)
  29. {
  30. SCODE ret;
  31. DWORD dwOut, dwRet;
  32. BYTE abCAPDU[5+MAX_APDU];
  33. BYTE abRAPDU[MAX_APDU];
  34. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  35. LOG_BEGIN_PROXY(hScwGetPrincipalUID);
  36. // Marshaling
  37. __try {
  38. if (phTmp == NULL)
  39. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  40. if ((phTmp->dwFlags & FLAG_ISPROXY) == 0)
  41. return SCW_E_NOTIMPLEMENTED;
  42. InitXSCM(phTmp, abCAPDU, phTmp->bResLen);
  43. CLA(0x00);
  44. INS(phTmp->byINS);
  45. P1(2);
  46. P2(1);
  47. Lc(0);
  48. UINT82XSCM(&phTmp->xSCM, 2, TYPE_NOTYPE_COUNT); // Number of parameters
  49. String2XSCM(&phTmp->xSCM, principalName, phTmp->dwFlags & FLAG_BIGENDIAN);
  50. UINT8BYREF2XSCM(&phTmp->xSCM, principalUID);
  51. // API transfer
  52. UPDATE_Lc((UINT8)(GetSCMBufferLength(&phTmp->xSCM)-5));
  53. dwOut = MAX_APDU;
  54. dwRet = SCWTransmit(hCard, abCAPDU, (DWORD)GetSCMBufferLength(&phTmp->xSCM), abRAPDU, &dwOut);
  55. if (dwRet == 0)
  56. { // Return code UnMarshaling
  57. ret = ExtractSCODE(phTmp, abRAPDU, dwOut);
  58. }
  59. else
  60. ret = (SCODE)dwRet;
  61. // Parameters UnMarshaling
  62. if (ret == S_OK)
  63. {
  64. *principalUID = XSCM2UINT8(&phTmp->xSCM);
  65. }
  66. }
  67. __except(EXCEPTION_EXECUTE_HANDLER) {
  68. switch(GetExceptionCode())
  69. {
  70. case STATUS_INVALID_PARAM:
  71. ret = SCW_E_INVALIDPARAM;
  72. break;
  73. case STATUS_INSUFFICIENT_MEM:
  74. ret = SCW_E_CANTMARSHAL;
  75. break;
  76. case STATUS_INTERNAL_ERROR:
  77. ret = SCARD_F_INTERNAL_ERROR;
  78. break;
  79. default:
  80. ret = SCARD_F_UNKNOWN_ERROR;
  81. }
  82. }
  83. return ret;
  84. }
  85. SCODE WINAPI hScwAuthenticateName(SCARDHANDLE hCard, WCSTR name , BYTE *supportData, TCOUNT supportDataLength)
  86. {
  87. SCODE ret;
  88. DWORD dwOut, dwRet;
  89. BYTE abCAPDU[5+MAX_APDU];
  90. BYTE abRAPDU[MAX_APDU];
  91. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  92. LOG_BEGIN_PROXY(hScwAuthenticateName);
  93. // Marshaling
  94. __try {
  95. if (phTmp == NULL)
  96. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  97. if ((phTmp->dwFlags & FLAG_ISPROXY) == 0)
  98. return SCW_E_NOTIMPLEMENTED;
  99. InitXSCM(phTmp, abCAPDU, phTmp->bResLen);
  100. CLA(0x00);
  101. INS(phTmp->byINS);
  102. P1(2);
  103. P2(2);
  104. Lc(0);
  105. UINT82XSCM(&phTmp->xSCM, 2, TYPE_NOTYPE_COUNT); // Number of parameters
  106. String2XSCM(&phTmp->xSCM, name, phTmp->dwFlags & FLAG_BIGENDIAN);
  107. ByteArray2XSCM(&phTmp->xSCM, supportData, supportDataLength);
  108. // API transfer
  109. UPDATE_Lc((UINT8)(GetSCMBufferLength(&phTmp->xSCM)-5));
  110. dwOut = 255;
  111. dwRet = SCWTransmit(hCard, abCAPDU, (DWORD)GetSCMBufferLength(&phTmp->xSCM), abRAPDU, &dwOut);
  112. if (dwRet == 0)
  113. { // Return code UnMarshaling
  114. ret = ExtractSCODE(phTmp, abRAPDU, dwOut);
  115. }
  116. else
  117. ret = (SCODE)dwRet;
  118. // Parameters UnMarshaling
  119. if (ret == S_OK)
  120. {
  121. }
  122. }
  123. __except(EXCEPTION_EXECUTE_HANDLER) {
  124. switch(GetExceptionCode())
  125. {
  126. case STATUS_INVALID_PARAM:
  127. ret = SCW_E_INVALIDPARAM;
  128. break;
  129. case STATUS_INSUFFICIENT_MEM:
  130. ret = SCW_E_CANTMARSHAL;
  131. break;
  132. case STATUS_INTERNAL_ERROR:
  133. ret = SCARD_F_INTERNAL_ERROR;
  134. break;
  135. default:
  136. ret = SCARD_F_UNKNOWN_ERROR;
  137. }
  138. }
  139. return ret;
  140. }
  141. SCODE WINAPI hScwAuthenticateUID(SCARDHANDLE hCard, TUID uid, BYTE *supportData, TCOUNT supportDataLength)
  142. {
  143. SCODE ret;
  144. DWORD dwOut, dwRet;
  145. BYTE abCAPDU[5+MAX_APDU];
  146. BYTE abRAPDU[MAX_APDU];
  147. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  148. LOG_BEGIN_PROXY(hScwAuthenticateUID);
  149. // Marshaling
  150. __try {
  151. if (phTmp == NULL)
  152. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  153. if ((phTmp->dwFlags & FLAG_ISPROXY) == 0)
  154. return SCW_E_NOTIMPLEMENTED;
  155. InitXSCM(phTmp, abCAPDU, phTmp->bResLen);
  156. CLA(0x00);
  157. INS(phTmp->byINS);
  158. P1(2);
  159. P2(3);
  160. Lc(0);
  161. UINT82XSCM(&phTmp->xSCM, 2, TYPE_NOTYPE_COUNT); // Number of parameters
  162. UINT82XSCM(&phTmp->xSCM, uid, TYPE_TYPED);
  163. ByteArray2XSCM(&phTmp->xSCM, supportData, supportDataLength);
  164. // API transfer
  165. UPDATE_Lc((UINT8)(GetSCMBufferLength(&phTmp->xSCM)-5));
  166. dwOut = MAX_APDU;
  167. dwRet = SCWTransmit(hCard, abCAPDU, (DWORD)GetSCMBufferLength(&phTmp->xSCM), abRAPDU, &dwOut);
  168. if (dwRet == 0)
  169. { // Return code UnMarshaling
  170. ret = ExtractSCODE(phTmp, abRAPDU, dwOut);
  171. }
  172. else
  173. ret = (SCODE)dwRet;
  174. // Parameters UnMarshaling
  175. if (ret == S_OK)
  176. {
  177. }
  178. }
  179. __except(EXCEPTION_EXECUTE_HANDLER) {
  180. switch(GetExceptionCode())
  181. {
  182. case STATUS_INVALID_PARAM:
  183. ret = SCW_E_INVALIDPARAM;
  184. break;
  185. case STATUS_INSUFFICIENT_MEM:
  186. ret = SCW_E_CANTMARSHAL;
  187. break;
  188. case STATUS_INTERNAL_ERROR:
  189. ret = SCARD_F_INTERNAL_ERROR;
  190. break;
  191. default:
  192. ret = SCARD_F_UNKNOWN_ERROR;
  193. }
  194. }
  195. return ret;
  196. }
  197. SCODE WINAPI hScwDeauthenticateName(SCARDHANDLE hCard, WCSTR principalName)
  198. {
  199. SCODE ret;
  200. DWORD dwOut, dwRet;
  201. BYTE abCAPDU[5+MAX_APDU];
  202. BYTE abRAPDU[MAX_APDU];
  203. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  204. LOG_BEGIN_PROXY(hScwDeauthenticateName);
  205. // Marshaling
  206. __try {
  207. if (phTmp == NULL)
  208. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  209. if ((phTmp->dwFlags & FLAG_ISPROXY) == 0)
  210. return SCW_E_NOTIMPLEMENTED;
  211. InitXSCM(phTmp, abCAPDU, phTmp->bResLen);
  212. CLA(0x00);
  213. INS(phTmp->byINS);
  214. P1(2);
  215. P2(4);
  216. Lc(0);
  217. UINT82XSCM(&phTmp->xSCM, 1, TYPE_NOTYPE_COUNT); // Number of parameters
  218. String2XSCM(&phTmp->xSCM, principalName, phTmp->dwFlags & FLAG_BIGENDIAN);
  219. // API transfer
  220. UPDATE_Lc((UINT8)(GetSCMBufferLength(&phTmp->xSCM)-5));
  221. dwOut = MAX_APDU;
  222. dwRet = SCWTransmit(hCard, abCAPDU, (DWORD)GetSCMBufferLength(&phTmp->xSCM), abRAPDU, &dwOut);
  223. if (dwRet == 0)
  224. { // Return code UnMarshaling
  225. ret = ExtractSCODE(phTmp, abRAPDU, dwOut);
  226. }
  227. else
  228. ret = (SCODE)dwRet;
  229. // Parameters UnMarshaling
  230. if (ret == S_OK)
  231. {
  232. }
  233. }
  234. __except(EXCEPTION_EXECUTE_HANDLER) {
  235. switch(GetExceptionCode())
  236. {
  237. case STATUS_INVALID_PARAM:
  238. ret = SCW_E_INVALIDPARAM;
  239. break;
  240. case STATUS_INSUFFICIENT_MEM:
  241. ret = SCW_E_CANTMARSHAL;
  242. break;
  243. case STATUS_INTERNAL_ERROR:
  244. ret = SCARD_F_INTERNAL_ERROR;
  245. break;
  246. default:
  247. ret = SCARD_F_UNKNOWN_ERROR;
  248. }
  249. }
  250. return ret;
  251. }
  252. SCODE WINAPI hScwDeauthenticateUID(SCARDHANDLE hCard, TUID uid)
  253. {
  254. SCODE ret;
  255. DWORD dwOut, dwRet;
  256. BYTE abCAPDU[5+MAX_APDU];
  257. BYTE abRAPDU[MAX_APDU];
  258. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  259. LOG_BEGIN_PROXY(hScwDeauthenticateUID);
  260. // Marshaling
  261. __try {
  262. if (phTmp == NULL)
  263. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  264. if ((phTmp->dwFlags & FLAG_ISPROXY) == 0)
  265. return SCW_E_NOTIMPLEMENTED;
  266. InitXSCM(phTmp, abCAPDU, phTmp->bResLen);
  267. CLA(0x00);
  268. INS(phTmp->byINS);
  269. P1(2);
  270. P2(5);
  271. Lc(0);
  272. UINT82XSCM(&phTmp->xSCM, 1, TYPE_NOTYPE_COUNT); // Number of parameters
  273. UINT82XSCM(&phTmp->xSCM, uid, TYPE_TYPED);
  274. // API transfer
  275. UPDATE_Lc((UINT8)(GetSCMBufferLength(&phTmp->xSCM)-5));
  276. dwOut = MAX_APDU;
  277. dwRet = SCWTransmit(hCard, abCAPDU, (DWORD)GetSCMBufferLength(&phTmp->xSCM), abRAPDU, &dwOut);
  278. if (dwRet == 0)
  279. { // Return code UnMarshaling
  280. ret = ExtractSCODE(phTmp, abRAPDU, dwOut);
  281. }
  282. else
  283. ret = (SCODE)dwRet;
  284. // Parameters UnMarshaling
  285. if (ret == S_OK)
  286. {
  287. }
  288. }
  289. __except(EXCEPTION_EXECUTE_HANDLER) {
  290. switch(GetExceptionCode())
  291. {
  292. case STATUS_INVALID_PARAM:
  293. ret = SCW_E_INVALIDPARAM;
  294. break;
  295. case STATUS_INSUFFICIENT_MEM:
  296. ret = SCW_E_CANTMARSHAL;
  297. break;
  298. case STATUS_INTERNAL_ERROR:
  299. ret = SCARD_F_INTERNAL_ERROR;
  300. break;
  301. default:
  302. ret = SCARD_F_UNKNOWN_ERROR;
  303. }
  304. }
  305. return ret;
  306. }
  307. SCODE WINAPI hScwIsAuthenticatedName(SCARDHANDLE hCard, WCSTR principalName)
  308. {
  309. SCODE ret;
  310. DWORD dwOut, dwRet;
  311. BYTE abCAPDU[5+MAX_APDU];
  312. BYTE abRAPDU[MAX_APDU];
  313. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  314. LOG_BEGIN_PROXY(hScwIsAuthenticatedName);
  315. // Marshaling
  316. __try {
  317. if (phTmp == NULL)
  318. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  319. if ((phTmp->dwFlags & FLAG_ISPROXY) == 0)
  320. return SCW_E_NOTIMPLEMENTED;
  321. InitXSCM(phTmp, abCAPDU, phTmp->bResLen);
  322. CLA(0x00);
  323. INS(phTmp->byINS);
  324. P1(2);
  325. P2(6);
  326. Lc(0);
  327. UINT82XSCM(&phTmp->xSCM, 1, TYPE_NOTYPE_COUNT); // Number of parameters
  328. String2XSCM(&phTmp->xSCM, principalName, phTmp->dwFlags & FLAG_BIGENDIAN);
  329. // API transfer
  330. UPDATE_Lc((UINT8)(GetSCMBufferLength(&phTmp->xSCM)-5));
  331. dwOut = MAX_APDU;
  332. dwRet = SCWTransmit(hCard, abCAPDU, (DWORD)GetSCMBufferLength(&phTmp->xSCM), abRAPDU, &dwOut);
  333. if (dwRet == 0)
  334. { // Return code UnMarshaling
  335. ret = ExtractSCODE(phTmp, abRAPDU, dwOut);
  336. }
  337. else
  338. ret = (SCODE)dwRet;
  339. // Parameters UnMarshaling
  340. if (ret == S_OK)
  341. {
  342. }
  343. }
  344. __except(EXCEPTION_EXECUTE_HANDLER) {
  345. switch(GetExceptionCode())
  346. {
  347. case STATUS_INVALID_PARAM:
  348. ret = SCW_E_INVALIDPARAM;
  349. break;
  350. case STATUS_INSUFFICIENT_MEM:
  351. ret = SCW_E_CANTMARSHAL;
  352. break;
  353. case STATUS_INTERNAL_ERROR:
  354. ret = SCARD_F_INTERNAL_ERROR;
  355. break;
  356. default:
  357. ret = SCARD_F_UNKNOWN_ERROR;
  358. }
  359. }
  360. return ret;
  361. }
  362. SCODE WINAPI hScwIsAuthenticatedUID(SCARDHANDLE hCard, TUID uid)
  363. {
  364. SCODE ret;
  365. DWORD dwOut, dwRet;
  366. BYTE abCAPDU[5+MAX_APDU];
  367. BYTE abRAPDU[MAX_APDU];
  368. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  369. LOG_BEGIN_PROXY(hScwIsAuthenticatedUID);
  370. // Marshaling
  371. __try {
  372. if (phTmp == NULL)
  373. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  374. if ((phTmp->dwFlags & FLAG_ISPROXY) == 0)
  375. return SCW_E_NOTIMPLEMENTED;
  376. InitXSCM(phTmp, abCAPDU, phTmp->bResLen);
  377. CLA(0x00);
  378. INS(phTmp->byINS);
  379. P1(2);
  380. P2(7);
  381. Lc(0);
  382. UINT82XSCM(&phTmp->xSCM, 1, TYPE_NOTYPE_COUNT); // Number of parameters
  383. UINT82XSCM(&phTmp->xSCM, uid, TYPE_TYPED);
  384. // API transfer
  385. UPDATE_Lc((UINT8)(GetSCMBufferLength(&phTmp->xSCM)-5));
  386. dwOut = MAX_APDU;
  387. dwRet = SCWTransmit(hCard, abCAPDU, (DWORD)GetSCMBufferLength(&phTmp->xSCM), abRAPDU, &dwOut);
  388. if (dwRet == 0)
  389. { // Return code UnMarshaling
  390. ret = ExtractSCODE(phTmp, abRAPDU, dwOut);
  391. }
  392. else
  393. ret = (SCODE)dwRet;
  394. // Parameters UnMarshaling
  395. if (ret == S_OK)
  396. {
  397. }
  398. }
  399. __except(EXCEPTION_EXECUTE_HANDLER) {
  400. switch(GetExceptionCode())
  401. {
  402. case STATUS_INVALID_PARAM:
  403. ret = SCW_E_INVALIDPARAM;
  404. break;
  405. case STATUS_INSUFFICIENT_MEM:
  406. ret = SCW_E_CANTMARSHAL;
  407. break;
  408. case STATUS_INTERNAL_ERROR:
  409. ret = SCARD_F_INTERNAL_ERROR;
  410. break;
  411. default:
  412. ret = SCARD_F_UNKNOWN_ERROR;
  413. }
  414. }
  415. return ret;
  416. }
  417. SCODE WINAPI hScwIsAuthorized(SCARDHANDLE hCard, WCSTR resourceName, BYTE operation)
  418. {
  419. SCODE ret;
  420. DWORD dwOut, dwRet;
  421. BYTE abCAPDU[5+MAX_APDU];
  422. BYTE abRAPDU[MAX_APDU];
  423. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  424. LOG_BEGIN_PROXY(hScwIsAuthorized);
  425. // Marshaling
  426. __try {
  427. if (phTmp == NULL)
  428. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  429. if ((phTmp->dwFlags & FLAG_ISPROXY) == 0)
  430. return SCW_E_NOTIMPLEMENTED;
  431. InitXSCM(phTmp, abCAPDU, phTmp->bResLen);
  432. CLA(0x00);
  433. INS(phTmp->byINS);
  434. P1(2);
  435. P2(8);
  436. Lc(0);
  437. // UINT82XSCM(&phTmp->xSCM, 3, TYPE_NOTYPE_COUNT); // Number of parameters
  438. UINT82XSCM(&phTmp->xSCM, 2, TYPE_NOTYPE_COUNT); // Number of parameters
  439. // UINT82XSCM(&phTmp->xSCM, resourceType, TYPE_TYPED);
  440. String2XSCM(&phTmp->xSCM, resourceName, phTmp->dwFlags & FLAG_BIGENDIAN);
  441. UINT82XSCM(&phTmp->xSCM, operation, TYPE_TYPED);
  442. // API transfer
  443. UPDATE_Lc((UINT8)(GetSCMBufferLength(&phTmp->xSCM)-5));
  444. dwOut = MAX_APDU;
  445. dwRet = SCWTransmit(hCard, abCAPDU, (DWORD)GetSCMBufferLength(&phTmp->xSCM), abRAPDU, &dwOut);
  446. if (dwRet == 0)
  447. { // Return code UnMarshaling
  448. ret = ExtractSCODE(phTmp, abRAPDU, dwOut);
  449. }
  450. else
  451. ret = (SCODE)dwRet;
  452. // Parameters UnMarshaling
  453. if (ret == S_OK)
  454. {
  455. }
  456. }
  457. __except(EXCEPTION_EXECUTE_HANDLER) {
  458. switch(GetExceptionCode())
  459. {
  460. case STATUS_INVALID_PARAM:
  461. ret = SCW_E_INVALIDPARAM;
  462. break;
  463. case STATUS_INSUFFICIENT_MEM:
  464. ret = SCW_E_CANTMARSHAL;
  465. break;
  466. case STATUS_INTERNAL_ERROR:
  467. ret = SCARD_F_INTERNAL_ERROR;
  468. break;
  469. default:
  470. ret = SCARD_F_UNKNOWN_ERROR;
  471. }
  472. }
  473. return ret;
  474. }
  475. /*
  476. ** File System
  477. */
  478. SCODE WINAPI hScwCreateFile(SCARDHANDLE hCard, WCSTR fileName, WCSTR aclFileName, HFILE *phFile)
  479. {
  480. SCODE ret;
  481. DWORD dwOut, dwRet;
  482. BYTE abCAPDU[5+MAX_APDU];
  483. BYTE abRAPDU[MAX_APDU];
  484. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  485. LOG_BEGIN_PROXY(hScwCreateFile);
  486. // Marshaling
  487. __try {
  488. if (phTmp == NULL)
  489. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  490. if ((phTmp->dwFlags & FLAG_ISPROXY) == 0)
  491. return SCW_E_NOTIMPLEMENTED;
  492. InitXSCM(phTmp, abCAPDU, phTmp->bResLen);
  493. CLA(0x00);
  494. INS(phTmp->byINS);
  495. P1(3);
  496. P2(1);
  497. Lc(0);
  498. UINT82XSCM(&phTmp->xSCM, 3, TYPE_NOTYPE_COUNT); // Number of parameters
  499. String2XSCM(&phTmp->xSCM, fileName, phTmp->dwFlags & FLAG_BIGENDIAN);
  500. String2XSCM(&phTmp->xSCM, aclFileName, phTmp->dwFlags & FLAG_BIGENDIAN);
  501. HFILEBYREF2XSCM(&phTmp->xSCM, phFile);
  502. // API transfer
  503. UPDATE_Lc((UINT8)(GetSCMBufferLength(&phTmp->xSCM)-5));
  504. dwOut = MAX_APDU;
  505. dwRet = SCWTransmit(hCard, abCAPDU, (DWORD)GetSCMBufferLength(&phTmp->xSCM), abRAPDU, &dwOut);
  506. if (dwRet == 0)
  507. { // Return code UnMarshaling
  508. ret = ExtractSCODE(phTmp, abRAPDU, dwOut);
  509. }
  510. else
  511. ret = (SCODE)dwRet;
  512. // Parameters UnMarshaling
  513. if (ret == S_OK)
  514. {
  515. if (phFile)
  516. *phFile = XSCM2HFILE(&phTmp->xSCM);
  517. }
  518. }
  519. __except(EXCEPTION_EXECUTE_HANDLER) {
  520. switch(GetExceptionCode())
  521. {
  522. case STATUS_INVALID_PARAM:
  523. ret = SCW_E_INVALIDPARAM;
  524. break;
  525. case STATUS_INSUFFICIENT_MEM:
  526. ret = SCW_E_CANTMARSHAL;
  527. break;
  528. case STATUS_INTERNAL_ERROR:
  529. ret = SCARD_F_INTERNAL_ERROR;
  530. break;
  531. default:
  532. ret = SCARD_F_UNKNOWN_ERROR;
  533. }
  534. }
  535. return ret;
  536. }
  537. SCODE WINAPI hScwDeleteFile(SCARDHANDLE hCard, WCSTR fileName)
  538. {
  539. SCODE ret;
  540. DWORD dwOut, dwRet;
  541. BYTE abCAPDU[5+MAX_APDU];
  542. BYTE abRAPDU[MAX_APDU];
  543. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  544. LOG_BEGIN_PROXY(hScwDeleteFile);
  545. // Marshaling
  546. __try {
  547. if (phTmp == NULL)
  548. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  549. if ((phTmp->dwFlags & FLAG_ISPROXY) == 0)
  550. return SCW_E_NOTIMPLEMENTED;
  551. InitXSCM(phTmp, abCAPDU, phTmp->bResLen);
  552. CLA(0x00);
  553. INS(phTmp->byINS);
  554. P1(3);
  555. P2(2);
  556. Lc(0);
  557. UINT82XSCM(&phTmp->xSCM, 1, TYPE_NOTYPE_COUNT); // Number of parameters
  558. String2XSCM(&phTmp->xSCM, fileName, phTmp->dwFlags & FLAG_BIGENDIAN);
  559. // API transfer
  560. UPDATE_Lc((UINT8)(GetSCMBufferLength(&phTmp->xSCM)-5));
  561. dwOut = MAX_APDU;
  562. dwRet = SCWTransmit(hCard, abCAPDU, (DWORD)GetSCMBufferLength(&phTmp->xSCM), abRAPDU, &dwOut);
  563. if (dwRet == 0)
  564. { // Return code UnMarshaling
  565. ret = ExtractSCODE(phTmp, abRAPDU, dwOut);
  566. }
  567. else
  568. ret = (SCODE)dwRet;
  569. // Parameters UnMarshaling
  570. if (ret == S_OK)
  571. {
  572. }
  573. }
  574. __except(EXCEPTION_EXECUTE_HANDLER) {
  575. switch(GetExceptionCode())
  576. {
  577. case STATUS_INVALID_PARAM:
  578. ret = SCW_E_INVALIDPARAM;
  579. break;
  580. case STATUS_INSUFFICIENT_MEM:
  581. ret = SCW_E_CANTMARSHAL;
  582. break;
  583. case STATUS_INTERNAL_ERROR:
  584. ret = SCARD_F_INTERNAL_ERROR;
  585. break;
  586. default:
  587. ret = SCARD_F_UNKNOWN_ERROR;
  588. }
  589. }
  590. return ret;
  591. }
  592. SCODE WINAPI hScwCloseFile(SCARDHANDLE hCard, HFILE hFile)
  593. {
  594. SCODE ret;
  595. DWORD dwOut, dwRet;
  596. BYTE abCAPDU[5+MAX_APDU];
  597. BYTE abRAPDU[MAX_APDU];
  598. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  599. LOG_BEGIN_PROXY(hScwCloseFile);
  600. // Marshaling
  601. __try {
  602. if (phTmp == NULL)
  603. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  604. if ((phTmp->dwFlags & FLAG_ISPROXY) == 0)
  605. return SCW_E_NOTIMPLEMENTED;
  606. InitXSCM(phTmp, abCAPDU, phTmp->bResLen);
  607. CLA(0x00);
  608. INS(phTmp->byINS);
  609. P1(3);
  610. P2(4);
  611. Lc(0);
  612. UINT82XSCM(&phTmp->xSCM, 1, TYPE_NOTYPE_COUNT); // Number of parameters
  613. HFILE2XSCM(&phTmp->xSCM, hFile);
  614. // API transfer
  615. UPDATE_Lc((UINT8)(GetSCMBufferLength(&phTmp->xSCM)-5));
  616. dwOut = MAX_APDU;
  617. dwRet = SCWTransmit(hCard, abCAPDU, (DWORD)GetSCMBufferLength(&phTmp->xSCM), abRAPDU, &dwOut);
  618. if (dwRet == 0)
  619. { // Return code UnMarshaling
  620. ret = ExtractSCODE(phTmp, abRAPDU, dwOut);
  621. }
  622. else
  623. ret = (SCODE)dwRet;
  624. // Parameters UnMarshaling
  625. if (ret == S_OK)
  626. {
  627. }
  628. }
  629. __except(EXCEPTION_EXECUTE_HANDLER) {
  630. switch(GetExceptionCode())
  631. {
  632. case STATUS_INVALID_PARAM:
  633. ret = SCW_E_INVALIDPARAM;
  634. break;
  635. case STATUS_INSUFFICIENT_MEM:
  636. ret = SCW_E_CANTMARSHAL;
  637. break;
  638. case STATUS_INTERNAL_ERROR:
  639. ret = SCARD_F_INTERNAL_ERROR;
  640. break;
  641. default:
  642. ret = SCARD_F_UNKNOWN_ERROR;
  643. }
  644. }
  645. return ret;
  646. }
  647. SCODE __hScwReadFile(SCARDHANDLE hCard, HFILE hFile, BYTE *buffer, TCOUNT length, TCOUNT *bytesRead)
  648. {
  649. SCODE ret;
  650. DWORD dwOut, dwRet;
  651. BYTE abCAPDU[5+MAX_APDU];
  652. BYTE abRAPDU[MAX_APDU];
  653. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  654. LOG_BEGIN_PROXY(hScwReadFile);
  655. // Marshaling
  656. __try {
  657. if (phTmp == NULL)
  658. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  659. if ((phTmp->dwFlags & FLAG_ISPROXY) == 0)
  660. return SCW_E_NOTIMPLEMENTED;
  661. InitXSCM(phTmp, abCAPDU, phTmp->bResLen);
  662. CLA(0x00);
  663. INS(phTmp->byINS);
  664. P1(3);
  665. P2(5);
  666. Lc(0);
  667. UINT82XSCM(&phTmp->xSCM, 3, TYPE_NOTYPE_COUNT); // Number of parameters
  668. HFILE2XSCM(&phTmp->xSCM, hFile);
  669. ByteArrayOut2XSCM(&phTmp->xSCM, buffer, length);
  670. UINT8BYREF2XSCM(&phTmp->xSCM, bytesRead);
  671. // API transfer
  672. UPDATE_Lc((UINT8)(GetSCMBufferLength(&phTmp->xSCM)-5));
  673. dwOut = MAX_APDU;
  674. dwRet = SCWTransmit(hCard, abCAPDU, (DWORD)GetSCMBufferLength(&phTmp->xSCM), abRAPDU, &dwOut);
  675. if (dwRet == 0)
  676. { // Return code UnMarshaling
  677. ret = ExtractSCODE(phTmp, abRAPDU, dwOut);
  678. }
  679. else
  680. ret = (SCODE)dwRet;
  681. // Parameters UnMarshaling
  682. if (ret == S_OK)
  683. {
  684. BYTE *_pbBuffer;
  685. UINT8 len;
  686. len = XSCM2ByteArray(&phTmp->xSCM, &_pbBuffer);
  687. *bytesRead = XSCM2UINT8(&phTmp->xSCM);
  688. memcpy(buffer, _pbBuffer, *bytesRead);
  689. }
  690. }
  691. __except(EXCEPTION_EXECUTE_HANDLER) {
  692. switch(GetExceptionCode())
  693. {
  694. case STATUS_INVALID_PARAM:
  695. ret = SCW_E_INVALIDPARAM;
  696. break;
  697. case STATUS_INSUFFICIENT_MEM:
  698. ret = SCW_E_CANTMARSHAL;
  699. break;
  700. case STATUS_INTERNAL_ERROR:
  701. ret = SCARD_F_INTERNAL_ERROR;
  702. break;
  703. default:
  704. ret = SCARD_F_UNKNOWN_ERROR;
  705. }
  706. }
  707. return ret;
  708. }
  709. SCODE WINAPI hScwReadFile32(SCARDHANDLE hCard, HFILE hFile, BYTE *pbBuffer, DWORD nRequestedBytes, DWORD *pnActualBytes)
  710. {
  711. SCODE ret;
  712. TCOUNT nNow, nOpt, nRead;
  713. DWORD nOverall = 0;
  714. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  715. // Marshaling
  716. __try {
  717. if ((phTmp == NULL) || (pnActualBytes == NULL))
  718. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  719. // v1.0 IN: #param | 8 | HFILE | a | L | 108 | Read / OUT: RC | L | Data | Read | SW (already deducted so max = bResLen-10)
  720. // v1.1 IN: #param | 8 | HFILE | a | L | 108 | Read / OUT: L | Data | Read | SW (already deducted so max = bResLen-9)
  721. if (FLAG2VERSION(phTmp->dwFlags) == VERSION_1_0)
  722. {
  723. if (phTmp->bResLen < 10)
  724. RaiseException( STATUS_INSUFFICIENT_MEM, 0, 0, 0);
  725. }
  726. else if (FLAG2VERSION(phTmp->dwFlags) == VERSION_1_1)
  727. {
  728. if (phTmp->bResLen < 9)
  729. RaiseException( STATUS_INSUFFICIENT_MEM, 0, 0, 0);
  730. }
  731. else
  732. RaiseException(STATUS_INTERNAL_ERROR, 0, 0, 0);
  733. nOpt = phTmp->bResLen - 9; // Biggest possible
  734. if (FLAG2VERSION(phTmp->dwFlags) == VERSION_1_0)
  735. nOpt--;
  736. do
  737. {
  738. nNow = (TCOUNT)((nRequestedBytes - nOverall > nOpt) ? nOpt : nRequestedBytes - nOverall);
  739. ret = __hScwReadFile(hCard, hFile, pbBuffer+nOverall, nNow, &nRead);
  740. if (FAILED(ret))
  741. break;
  742. nOverall += nRead;
  743. } while ((nOverall < nRequestedBytes) && (nRead == nNow));
  744. if (!(FAILED(ret)))
  745. *pnActualBytes = nOverall;
  746. }
  747. __except(EXCEPTION_EXECUTE_HANDLER) {
  748. switch(GetExceptionCode())
  749. {
  750. case STATUS_INVALID_PARAM:
  751. ret = SCW_E_INVALIDPARAM;
  752. break;
  753. case STATUS_INSUFFICIENT_MEM:
  754. ret = SCW_E_CANTMARSHAL;
  755. break;
  756. case STATUS_INTERNAL_ERROR:
  757. ret = SCARD_F_INTERNAL_ERROR;
  758. break;
  759. default:
  760. ret = SCARD_F_UNKNOWN_ERROR;
  761. }
  762. }
  763. return ret;
  764. }
  765. SCODE WINAPI hScwReadFile(SCARDHANDLE hCard, HFILE hFile, BYTE *pbBuffer, TCOUNT nRequestedBytes, TCOUNT *pnActualBytes)
  766. {
  767. DWORD cbActual;
  768. SCODE ret;
  769. if (IsBadWritePtr(pnActualBytes, 1))
  770. ret = hScwReadFile32(hCard, hFile, pbBuffer, (DWORD)nRequestedBytes, NULL);
  771. else
  772. {
  773. ret = hScwReadFile32(hCard, hFile, pbBuffer, (DWORD)nRequestedBytes, &cbActual);
  774. if (!FAILED(ret))
  775. *pnActualBytes = (TCOUNT)cbActual;
  776. }
  777. return ret;
  778. }
  779. SCODE __hScwWriteFile(SCARDHANDLE hCard, HFILE hFile, BYTE *buffer, TCOUNT length, TCOUNT *bytesWritten)
  780. {
  781. SCODE ret;
  782. DWORD dwOut, dwRet;
  783. BYTE abCAPDU[5+MAX_APDU];
  784. BYTE abRAPDU[MAX_APDU];
  785. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  786. LOG_BEGIN_PROXY(hScwWriteFile);
  787. // Marshaling
  788. __try {
  789. if (phTmp == NULL)
  790. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  791. if ((phTmp->dwFlags & FLAG_ISPROXY) == 0)
  792. return SCW_E_NOTIMPLEMENTED;
  793. InitXSCM(phTmp, abCAPDU, phTmp->bResLen);
  794. CLA(0x00);
  795. INS(phTmp->byINS);
  796. P1(3);
  797. P2(6);
  798. Lc(0);
  799. UINT82XSCM(&phTmp->xSCM, 3, TYPE_NOTYPE_COUNT); // Number of parameters
  800. HFILE2XSCM(&phTmp->xSCM, hFile);
  801. ByteArray2XSCM(&phTmp->xSCM, buffer, length);
  802. UINT8BYREF2XSCM(&phTmp->xSCM, bytesWritten);
  803. // API transfer
  804. UPDATE_Lc((UINT8)(GetSCMBufferLength(&phTmp->xSCM)-5));
  805. dwOut = MAX_APDU;
  806. dwRet = SCWTransmit(hCard, abCAPDU, (DWORD)GetSCMBufferLength(&phTmp->xSCM), abRAPDU, &dwOut);
  807. if (dwRet == 0)
  808. { // Return code UnMarshaling
  809. ret = ExtractSCODE(phTmp, abRAPDU, dwOut);
  810. }
  811. else
  812. ret = (SCODE)dwRet;
  813. // Parameters UnMarshaling
  814. if (ret == S_OK)
  815. {
  816. *bytesWritten = XSCM2UINT8(&phTmp->xSCM);
  817. }
  818. }
  819. __except(EXCEPTION_EXECUTE_HANDLER) {
  820. switch(GetExceptionCode())
  821. {
  822. case STATUS_INVALID_PARAM:
  823. ret = SCW_E_INVALIDPARAM;
  824. break;
  825. case STATUS_INSUFFICIENT_MEM:
  826. ret = SCW_E_CANTMARSHAL;
  827. break;
  828. case STATUS_INTERNAL_ERROR:
  829. ret = SCARD_F_INTERNAL_ERROR;
  830. break;
  831. default:
  832. ret = SCARD_F_UNKNOWN_ERROR;
  833. }
  834. }
  835. return ret;
  836. }
  837. SCODE WINAPI hScwWriteFile32(SCARDHANDLE hCard, HFILE hFile, BYTE *pbBuffer, DWORD nRequestedBytes, DWORD *pnActualBytes)
  838. {
  839. SCODE ret;
  840. TCOUNT nNow, nOpt, nWritten;
  841. DWORD nOverall = 0;
  842. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  843. // Marshaling
  844. __try {
  845. if ((phTmp == NULL) || (pnActualBytes == NULL))
  846. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  847. // v1.0 IN: #param | 8 | HFILE | A | L | Data | 108 | Written / OUT: RC | Written | SW (already deducted so max = bResLen-9)
  848. // v1.1 IN: #param | 8 | HFILE | A | L | Data | 108 | Written / OUT: Written | SW (already deducted so max = bResLen-8)
  849. if (FLAG2VERSION(phTmp->dwFlags) == VERSION_1_0)
  850. {
  851. if (phTmp->bResLen < 9)
  852. RaiseException( STATUS_INSUFFICIENT_MEM, 0, 0, 0);
  853. }
  854. else if (FLAG2VERSION(phTmp->dwFlags) == VERSION_1_1)
  855. {
  856. if (phTmp->bResLen < 8)
  857. RaiseException( STATUS_INSUFFICIENT_MEM, 0, 0, 0);
  858. }
  859. else
  860. RaiseException(STATUS_INTERNAL_ERROR, 0, 0, 0);
  861. nOpt = phTmp->bResLen - 8; // Biggest possible
  862. if (FLAG2VERSION(phTmp->dwFlags) == VERSION_1_0)
  863. nOpt--;
  864. do
  865. {
  866. nNow = (TCOUNT)((nRequestedBytes - nOverall > (TCOUNT)nOpt) ? nOpt : nRequestedBytes - nOverall);
  867. ret = __hScwWriteFile(hCard, hFile, pbBuffer+nOverall, nNow, &nWritten);
  868. if (FAILED(ret))
  869. break;
  870. nOverall += nWritten;
  871. } while ((nOverall < nRequestedBytes) && (nWritten == nNow));
  872. if (!(FAILED(ret)))
  873. *pnActualBytes = nOverall;
  874. }
  875. __except(EXCEPTION_EXECUTE_HANDLER) {
  876. switch(GetExceptionCode())
  877. {
  878. case STATUS_INVALID_PARAM:
  879. ret = SCW_E_INVALIDPARAM;
  880. break;
  881. case STATUS_INSUFFICIENT_MEM:
  882. ret = SCW_E_CANTMARSHAL;
  883. break;
  884. case STATUS_INTERNAL_ERROR:
  885. ret = SCARD_F_INTERNAL_ERROR;
  886. break;
  887. default:
  888. ret = SCARD_F_UNKNOWN_ERROR;
  889. }
  890. }
  891. return ret;
  892. }
  893. SCODE WINAPI hScwWriteFile(SCARDHANDLE hCard, HFILE hFile, BYTE *pbBuffer, TCOUNT nRequestedBytes, TCOUNT *pnActualBytes)
  894. {
  895. DWORD cbActual;
  896. SCODE ret;
  897. if (IsBadWritePtr(pnActualBytes, 1))
  898. ret = hScwWriteFile32(hCard, hFile, pbBuffer, (DWORD)nRequestedBytes, NULL);
  899. else
  900. {
  901. ret = hScwWriteFile32(hCard, hFile, pbBuffer, (DWORD)nRequestedBytes, &cbActual);
  902. if (!FAILED(ret))
  903. *pnActualBytes = (TCOUNT)cbActual;
  904. }
  905. return ret;
  906. }
  907. SCODE WINAPI hScwGetFileLength(SCARDHANDLE hCard, HFILE hFile, TOFFSET *fileSize)
  908. {
  909. SCODE ret;
  910. DWORD dwOut, dwRet;
  911. BYTE abCAPDU[5+MAX_APDU];
  912. BYTE abRAPDU[MAX_APDU];
  913. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  914. LOG_BEGIN_PROXY(hScwGetFileLength);
  915. // Marshaling
  916. __try {
  917. if (phTmp == NULL)
  918. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  919. if ((phTmp->dwFlags & FLAG_ISPROXY) == 0)
  920. return SCW_E_NOTIMPLEMENTED;
  921. InitXSCM(phTmp, abCAPDU, phTmp->bResLen);
  922. CLA(0x00);
  923. INS(phTmp->byINS);
  924. P1(3);
  925. P2(7);
  926. Lc(0);
  927. UINT82XSCM(&phTmp->xSCM, 2, TYPE_NOTYPE_COUNT); // Number of parameters
  928. HFILE2XSCM(&phTmp->xSCM, hFile);
  929. UINT16BYREF2XSCM(&phTmp->xSCM, fileSize, phTmp->dwFlags & FLAG_BIGENDIAN);
  930. // API transfer
  931. UPDATE_Lc((UINT8)(GetSCMBufferLength(&phTmp->xSCM)-5));
  932. dwOut = MAX_APDU;
  933. dwRet = SCWTransmit(hCard, abCAPDU, (DWORD)GetSCMBufferLength(&phTmp->xSCM), abRAPDU, &dwOut);
  934. if (dwRet == 0)
  935. { // Return code UnMarshaling
  936. ret = ExtractSCODE(phTmp, abRAPDU, dwOut);
  937. }
  938. else
  939. ret = (SCODE)dwRet;
  940. // Parameters UnMarshaling
  941. if (ret == S_OK)
  942. {
  943. *fileSize = XSCM2UINT16(&phTmp->xSCM, phTmp->dwFlags & FLAG_BIGENDIAN);
  944. }
  945. }
  946. __except(EXCEPTION_EXECUTE_HANDLER) {
  947. switch(GetExceptionCode())
  948. {
  949. case STATUS_INVALID_PARAM:
  950. ret = SCW_E_INVALIDPARAM;
  951. break;
  952. case STATUS_INSUFFICIENT_MEM:
  953. ret = SCW_E_CANTMARSHAL;
  954. break;
  955. case STATUS_INTERNAL_ERROR:
  956. ret = SCARD_F_INTERNAL_ERROR;
  957. break;
  958. default:
  959. ret = SCARD_F_UNKNOWN_ERROR;
  960. }
  961. }
  962. return ret;
  963. }
  964. SCODE WINAPI hScwSetFileLength(SCARDHANDLE hCard, HFILE hFile, TOFFSET fileSize)
  965. {
  966. SCODE ret;
  967. DWORD dwOut, dwRet;
  968. BYTE abCAPDU[5+MAX_APDU];
  969. BYTE abRAPDU[MAX_APDU];
  970. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  971. LOG_BEGIN_PROXY(hScwSetFileLength);
  972. // Marshaling
  973. __try {
  974. if (phTmp == NULL)
  975. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  976. if ((phTmp->dwFlags & FLAG_ISPROXY) == 0)
  977. return SCW_E_NOTIMPLEMENTED;
  978. InitXSCM(phTmp, abCAPDU, phTmp->bResLen);
  979. CLA(0x00);
  980. INS(phTmp->byINS);
  981. P1(3);
  982. P2(8);
  983. Lc(0);
  984. UINT82XSCM(&phTmp->xSCM, 2, TYPE_NOTYPE_COUNT); // Number of parameters
  985. HFILE2XSCM(&phTmp->xSCM, hFile);
  986. UINT162XSCM(&phTmp->xSCM, fileSize, phTmp->dwFlags & FLAG_BIGENDIAN);
  987. // API transfer
  988. UPDATE_Lc((UINT8)(GetSCMBufferLength(&phTmp->xSCM)-5));
  989. dwOut = MAX_APDU;
  990. dwRet = SCWTransmit(hCard, abCAPDU, (DWORD)GetSCMBufferLength(&phTmp->xSCM), abRAPDU, &dwOut);
  991. if (dwRet == 0)
  992. { // Return code UnMarshaling
  993. ret = ExtractSCODE(phTmp, abRAPDU, dwOut);
  994. }
  995. else
  996. ret = (SCODE)dwRet;
  997. // Parameters UnMarshaling
  998. if (ret == S_OK)
  999. {
  1000. }
  1001. }
  1002. __except(EXCEPTION_EXECUTE_HANDLER) {
  1003. switch(GetExceptionCode())
  1004. {
  1005. case STATUS_INVALID_PARAM:
  1006. ret = SCW_E_INVALIDPARAM;
  1007. break;
  1008. case STATUS_INSUFFICIENT_MEM:
  1009. ret = SCW_E_CANTMARSHAL;
  1010. break;
  1011. case STATUS_INTERNAL_ERROR:
  1012. ret = SCARD_F_INTERNAL_ERROR;
  1013. break;
  1014. default:
  1015. ret = SCARD_F_UNKNOWN_ERROR;
  1016. }
  1017. }
  1018. return ret;
  1019. }
  1020. SCODE WINAPI hScwSetFilePointer(SCARDHANDLE hCard, HFILE hFile, INT16 offset, BYTE mode)
  1021. {
  1022. SCODE ret;
  1023. DWORD dwOut, dwRet;
  1024. BYTE abCAPDU[5+MAX_APDU];
  1025. BYTE abRAPDU[MAX_APDU];
  1026. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  1027. LOG_BEGIN_PROXY(hScwSetFilePointer);
  1028. // Marshaling
  1029. __try {
  1030. if (phTmp == NULL)
  1031. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  1032. if ((phTmp->dwFlags & FLAG_ISPROXY) == 0)
  1033. return SCW_E_NOTIMPLEMENTED;
  1034. InitXSCM(phTmp, abCAPDU, phTmp->bResLen);
  1035. CLA(0x00);
  1036. INS(phTmp->byINS);
  1037. P1(3);
  1038. P2(9);
  1039. Lc(0);
  1040. UINT82XSCM(&phTmp->xSCM, 3, TYPE_NOTYPE_COUNT); // Number of parameters
  1041. HFILE2XSCM(&phTmp->xSCM, hFile);
  1042. UINT162XSCM(&phTmp->xSCM, (UINT16)offset, phTmp->dwFlags & FLAG_BIGENDIAN);
  1043. UINT82XSCM(&phTmp->xSCM, mode, TYPE_TYPED);
  1044. // API transfer
  1045. UPDATE_Lc((UINT8)(GetSCMBufferLength(&phTmp->xSCM)-5));
  1046. dwOut = MAX_APDU;
  1047. dwRet = SCWTransmit(hCard, abCAPDU, (DWORD)GetSCMBufferLength(&phTmp->xSCM), abRAPDU, &dwOut);
  1048. if (dwRet == 0)
  1049. { // Return code UnMarshaling
  1050. ret = ExtractSCODE(phTmp, abRAPDU, dwOut);
  1051. }
  1052. else
  1053. ret = (SCODE)dwRet;
  1054. // Parameters UnMarshaling
  1055. if (ret == S_OK)
  1056. {
  1057. }
  1058. }
  1059. __except(EXCEPTION_EXECUTE_HANDLER) {
  1060. switch(GetExceptionCode())
  1061. {
  1062. case STATUS_INVALID_PARAM:
  1063. ret = SCW_E_INVALIDPARAM;
  1064. break;
  1065. case STATUS_INSUFFICIENT_MEM:
  1066. ret = SCW_E_CANTMARSHAL;
  1067. break;
  1068. case STATUS_INTERNAL_ERROR:
  1069. ret = SCARD_F_INTERNAL_ERROR;
  1070. break;
  1071. default:
  1072. ret = SCARD_F_UNKNOWN_ERROR;
  1073. }
  1074. }
  1075. return ret;
  1076. }
  1077. SCODE WINAPI hScwGetFileAttributes(SCARDHANDLE hCard, WCSTR fileName, UINT16 *attributeValue)
  1078. {
  1079. SCODE ret;
  1080. DWORD dwOut, dwRet;
  1081. BYTE abCAPDU[5+MAX_APDU];
  1082. BYTE abRAPDU[MAX_APDU];
  1083. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  1084. LOG_BEGIN_PROXY(hScwGetFileAttributes);
  1085. // Marshaling
  1086. __try {
  1087. if (phTmp == NULL)
  1088. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  1089. if ((phTmp->dwFlags & FLAG_ISPROXY) == 0)
  1090. return SCW_E_NOTIMPLEMENTED;
  1091. InitXSCM(phTmp, abCAPDU, phTmp->bResLen);
  1092. CLA(0x00);
  1093. INS(phTmp->byINS);
  1094. P1(3);
  1095. P2(11);
  1096. Lc(0);
  1097. UINT82XSCM(&phTmp->xSCM, 2, TYPE_NOTYPE_COUNT); // Number of parameters
  1098. String2XSCM(&phTmp->xSCM, fileName, phTmp->dwFlags & FLAG_BIGENDIAN);
  1099. UINT16BYREF2XSCM(&phTmp->xSCM, attributeValue, phTmp->dwFlags & FLAG_BIGENDIAN);
  1100. // API transfer
  1101. UPDATE_Lc((UINT8)(GetSCMBufferLength(&phTmp->xSCM)-5));
  1102. dwOut = MAX_APDU;
  1103. dwRet = SCWTransmit(hCard, abCAPDU, (DWORD)GetSCMBufferLength(&phTmp->xSCM), abRAPDU, &dwOut);
  1104. if (dwRet == 0)
  1105. { // Return code UnMarshaling
  1106. ret = ExtractSCODE(phTmp, abRAPDU, dwOut);
  1107. }
  1108. else
  1109. ret = (SCODE)dwRet;
  1110. // Parameters UnMarshaling
  1111. if (ret == S_OK)
  1112. {
  1113. *attributeValue = XSCM2UINT16(&phTmp->xSCM, phTmp->dwFlags & FLAG_BIGENDIAN);
  1114. }
  1115. }
  1116. __except(EXCEPTION_EXECUTE_HANDLER) {
  1117. switch(GetExceptionCode())
  1118. {
  1119. case STATUS_INVALID_PARAM:
  1120. ret = SCW_E_INVALIDPARAM;
  1121. break;
  1122. case STATUS_INSUFFICIENT_MEM:
  1123. ret = SCW_E_CANTMARSHAL;
  1124. break;
  1125. case STATUS_INTERNAL_ERROR:
  1126. ret = SCARD_F_INTERNAL_ERROR;
  1127. break;
  1128. default:
  1129. ret = SCARD_F_UNKNOWN_ERROR;
  1130. }
  1131. }
  1132. return ret;
  1133. }
  1134. SCODE WINAPI hScwSetFileAttributes(SCARDHANDLE hCard, WCSTR fileName, UINT16 attributeValue)
  1135. {
  1136. SCODE ret;
  1137. DWORD dwOut, dwRet;
  1138. BYTE abCAPDU[5+MAX_APDU];
  1139. BYTE abRAPDU[MAX_APDU];
  1140. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  1141. LOG_BEGIN_PROXY(hScwSetFileAttributes);
  1142. // Marshaling
  1143. __try {
  1144. if (phTmp == NULL)
  1145. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  1146. if ((phTmp->dwFlags & FLAG_ISPROXY) == 0)
  1147. return SCW_E_NOTIMPLEMENTED;
  1148. InitXSCM(phTmp, abCAPDU, phTmp->bResLen);
  1149. CLA(0x00);
  1150. INS(phTmp->byINS);
  1151. P1(3);
  1152. P2(12);
  1153. Lc(0);
  1154. UINT82XSCM(&phTmp->xSCM, 2, TYPE_NOTYPE_COUNT); // Number of parameters
  1155. String2XSCM(&phTmp->xSCM, fileName, phTmp->dwFlags & FLAG_BIGENDIAN);
  1156. UINT162XSCM(&phTmp->xSCM, attributeValue, phTmp->dwFlags & FLAG_BIGENDIAN);
  1157. // API transfer
  1158. UPDATE_Lc((UINT8)(GetSCMBufferLength(&phTmp->xSCM)-5));
  1159. dwOut = MAX_APDU;
  1160. dwRet = SCWTransmit(hCard, abCAPDU, (DWORD)GetSCMBufferLength(&phTmp->xSCM), abRAPDU, &dwOut);
  1161. if (dwRet == 0)
  1162. { // Return code UnMarshaling
  1163. ret = ExtractSCODE(phTmp, abRAPDU, dwOut);
  1164. }
  1165. else
  1166. ret = (SCODE)dwRet;
  1167. // Parameters UnMarshaling
  1168. if (ret == S_OK)
  1169. {
  1170. }
  1171. }
  1172. __except(EXCEPTION_EXECUTE_HANDLER) {
  1173. switch(GetExceptionCode())
  1174. {
  1175. case STATUS_INVALID_PARAM:
  1176. ret = SCW_E_INVALIDPARAM;
  1177. break;
  1178. case STATUS_INSUFFICIENT_MEM:
  1179. ret = SCW_E_CANTMARSHAL;
  1180. break;
  1181. case STATUS_INTERNAL_ERROR:
  1182. ret = SCARD_F_INTERNAL_ERROR;
  1183. break;
  1184. default:
  1185. ret = SCARD_F_UNKNOWN_ERROR;
  1186. }
  1187. }
  1188. return ret;
  1189. }
  1190. SCODE WINAPI hScwSetFileACL(SCARDHANDLE hCard, WCSTR fileName, WCSTR aclFileName)
  1191. {
  1192. SCODE ret;
  1193. DWORD dwOut, dwRet;
  1194. BYTE abCAPDU[5+MAX_APDU];
  1195. BYTE abRAPDU[MAX_APDU];
  1196. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  1197. LOG_BEGIN_PROXY(hScwSetFileACL);
  1198. // Marshaling
  1199. __try {
  1200. if (phTmp == NULL)
  1201. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  1202. if ((phTmp->dwFlags & FLAG_ISPROXY) == 0)
  1203. return SCW_E_NOTIMPLEMENTED;
  1204. InitXSCM(phTmp, abCAPDU, phTmp->bResLen);
  1205. CLA(0x00);
  1206. INS(phTmp->byINS);
  1207. P1(3);
  1208. P2(13);
  1209. Lc(0);
  1210. UINT82XSCM(&phTmp->xSCM, 2, TYPE_NOTYPE_COUNT); // Number of parameters
  1211. String2XSCM(&phTmp->xSCM, fileName, phTmp->dwFlags & FLAG_BIGENDIAN);
  1212. String2XSCM(&phTmp->xSCM, aclFileName, phTmp->dwFlags & FLAG_BIGENDIAN);
  1213. // API transfer
  1214. UPDATE_Lc((UINT8)(GetSCMBufferLength(&phTmp->xSCM)-5));
  1215. dwOut = MAX_APDU;
  1216. dwRet = SCWTransmit(hCard, abCAPDU, (DWORD)GetSCMBufferLength(&phTmp->xSCM), abRAPDU, &dwOut);
  1217. if (dwRet == 0)
  1218. { // Return code UnMarshaling
  1219. ret = ExtractSCODE(phTmp, abRAPDU, dwOut);
  1220. }
  1221. else
  1222. ret = (SCODE)dwRet;
  1223. // Parameters UnMarshaling
  1224. if (ret == S_OK)
  1225. {
  1226. }
  1227. }
  1228. __except(EXCEPTION_EXECUTE_HANDLER) {
  1229. switch(GetExceptionCode())
  1230. {
  1231. case STATUS_INVALID_PARAM:
  1232. ret = SCW_E_INVALIDPARAM;
  1233. break;
  1234. case STATUS_INSUFFICIENT_MEM:
  1235. ret = SCW_E_CANTMARSHAL;
  1236. break;
  1237. case STATUS_INTERNAL_ERROR:
  1238. ret = SCARD_F_INTERNAL_ERROR;
  1239. break;
  1240. default:
  1241. ret = SCARD_F_UNKNOWN_ERROR;
  1242. }
  1243. }
  1244. return ret;
  1245. }
  1246. SCODE WINAPI hScwGetFileAclHandle(SCARDHANDLE hCard, WCSTR fileName, HFILE *phFile)
  1247. {
  1248. SCODE ret;
  1249. DWORD dwOut, dwRet;
  1250. BYTE abCAPDU[5+MAX_APDU];
  1251. BYTE abRAPDU[MAX_APDU];
  1252. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  1253. LOG_BEGIN_PROXY(hScwGetFileAclHandle);
  1254. // Marshaling
  1255. __try {
  1256. if (phTmp == NULL)
  1257. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  1258. if ((phTmp->dwFlags & FLAG_ISPROXY) == 0)
  1259. return SCW_E_NOTIMPLEMENTED;
  1260. InitXSCM(phTmp, abCAPDU, phTmp->bResLen);
  1261. CLA(0x00);
  1262. INS(phTmp->byINS);
  1263. P1(3);
  1264. P2(14);
  1265. Lc(0);
  1266. UINT82XSCM(&phTmp->xSCM, 2, TYPE_NOTYPE_COUNT); // Number of parameters
  1267. String2XSCM(&phTmp->xSCM, fileName, phTmp->dwFlags & FLAG_BIGENDIAN);
  1268. HFILEBYREF2XSCM(&phTmp->xSCM, phFile);
  1269. // API transfer
  1270. UPDATE_Lc((UINT8)(GetSCMBufferLength(&phTmp->xSCM)-5));
  1271. dwOut = MAX_APDU;
  1272. dwRet = SCWTransmit(hCard, abCAPDU, (DWORD)GetSCMBufferLength(&phTmp->xSCM), abRAPDU, &dwOut);
  1273. if (dwRet == 0)
  1274. { // Return code UnMarshaling
  1275. ret = ExtractSCODE(phTmp, abRAPDU, dwOut);
  1276. }
  1277. else
  1278. ret = (SCODE)dwRet;
  1279. // Parameters UnMarshaling
  1280. if (ret == S_OK)
  1281. {
  1282. if (phFile)
  1283. *phFile = XSCM2HFILE(&phTmp->xSCM);
  1284. }
  1285. }
  1286. __except(EXCEPTION_EXECUTE_HANDLER) {
  1287. switch(GetExceptionCode())
  1288. {
  1289. case STATUS_INVALID_PARAM:
  1290. ret = SCW_E_INVALIDPARAM;
  1291. break;
  1292. case STATUS_INSUFFICIENT_MEM:
  1293. ret = SCW_E_CANTMARSHAL;
  1294. break;
  1295. case STATUS_INTERNAL_ERROR:
  1296. ret = SCARD_F_INTERNAL_ERROR;
  1297. break;
  1298. default:
  1299. ret = SCARD_F_UNKNOWN_ERROR;
  1300. }
  1301. }
  1302. return ret;
  1303. }
  1304. SCODE WINAPI hScwEnumFile(SCARDHANDLE hCard, WCSTR directoryName, UINT16 *fileCookie, WSTR fileName, TCOUNT fileNameLength)
  1305. {
  1306. SCODE ret;
  1307. DWORD dwOut, dwRet;
  1308. BYTE abCAPDU[5+MAX_APDU];
  1309. BYTE abRAPDU[MAX_APDU];
  1310. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  1311. LOG_BEGIN_PROXY(hScwEnumFile);
  1312. // Marshaling
  1313. __try {
  1314. if (phTmp == NULL)
  1315. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  1316. if ((phTmp->dwFlags & FLAG_ISPROXY) == 0)
  1317. return SCW_E_NOTIMPLEMENTED;
  1318. InitXSCM(phTmp, abCAPDU, phTmp->bResLen);
  1319. CLA(0x00);
  1320. INS(phTmp->byINS);
  1321. P1(3);
  1322. P2(15);
  1323. Lc(0);
  1324. UINT82XSCM(&phTmp->xSCM, 3, TYPE_NOTYPE_COUNT); // Number of parameters
  1325. String2XSCM(&phTmp->xSCM, directoryName, phTmp->dwFlags & FLAG_BIGENDIAN);
  1326. UINT16BYREF2XSCM(&phTmp->xSCM, fileCookie, phTmp->dwFlags & FLAG_BIGENDIAN);
  1327. StringOut2XSCM(&phTmp->xSCM, fileName, fileNameLength, phTmp->dwFlags & FLAG_BIGENDIAN);
  1328. // API transfer
  1329. UPDATE_Lc((UINT8)(GetSCMBufferLength(&phTmp->xSCM)-5));
  1330. dwOut = MAX_APDU;
  1331. dwRet = SCWTransmit(hCard, abCAPDU, (DWORD)GetSCMBufferLength(&phTmp->xSCM), abRAPDU, &dwOut);
  1332. if (dwRet == 0)
  1333. { // Return code UnMarshaling
  1334. ret = ExtractSCODE(phTmp, abRAPDU, dwOut);
  1335. }
  1336. else
  1337. ret = (SCODE)dwRet;
  1338. // Parameters UnMarshaling
  1339. if (ret == S_OK)
  1340. {
  1341. WCSTR wsz;
  1342. UINT8 len;
  1343. *fileCookie = XSCM2UINT16(&phTmp->xSCM, phTmp->dwFlags & FLAG_BIGENDIAN);
  1344. wsz = XSCM2String(&phTmp->xSCM, &len, phTmp->dwFlags & FLAG_BIGENDIAN);
  1345. if (len > fileNameLength)
  1346. ret = SCW_E_BUFFERTOOSMALL;
  1347. else
  1348. wcscpy(fileName, wsz);
  1349. }
  1350. }
  1351. __except(EXCEPTION_EXECUTE_HANDLER) {
  1352. switch(GetExceptionCode())
  1353. {
  1354. case STATUS_INVALID_PARAM:
  1355. ret = SCW_E_INVALIDPARAM;
  1356. break;
  1357. case STATUS_INSUFFICIENT_MEM:
  1358. ret = SCW_E_CANTMARSHAL;
  1359. break;
  1360. case STATUS_INTERNAL_ERROR:
  1361. ret = SCARD_F_INTERNAL_ERROR;
  1362. break;
  1363. default:
  1364. ret = SCARD_F_UNKNOWN_ERROR;
  1365. }
  1366. }
  1367. return ret;
  1368. }
  1369. SCODE WINAPI hScwCreateDirectory(SCARDHANDLE hCard, WCSTR fileName, WCSTR aclFileName)
  1370. {
  1371. SCODE ret;
  1372. DWORD dwOut, dwRet;
  1373. BYTE abCAPDU[5+MAX_APDU];
  1374. BYTE abRAPDU[MAX_APDU];
  1375. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  1376. LOG_BEGIN_PROXY(hScwCreateDirectory);
  1377. // Marshaling
  1378. __try {
  1379. if (phTmp == NULL)
  1380. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  1381. if ((phTmp->dwFlags & FLAG_ISPROXY) == 0)
  1382. return SCW_E_NOTIMPLEMENTED;
  1383. InitXSCM(phTmp, abCAPDU, phTmp->bResLen);
  1384. CLA(0x00);
  1385. INS(phTmp->byINS);
  1386. P1(3);
  1387. P2(16);
  1388. Lc(0);
  1389. UINT82XSCM(&phTmp->xSCM, 2, TYPE_NOTYPE_COUNT); // Number of parameters
  1390. String2XSCM(&phTmp->xSCM, fileName, phTmp->dwFlags & FLAG_BIGENDIAN);
  1391. String2XSCM(&phTmp->xSCM, aclFileName, phTmp->dwFlags & FLAG_BIGENDIAN);
  1392. // API transfer
  1393. UPDATE_Lc((UINT8)(GetSCMBufferLength(&phTmp->xSCM)-5));
  1394. dwOut = MAX_APDU;
  1395. dwRet = SCWTransmit(hCard, abCAPDU, (DWORD)GetSCMBufferLength(&phTmp->xSCM), abRAPDU, &dwOut);
  1396. if (dwRet == 0)
  1397. { // Return code UnMarshaling
  1398. ret = ExtractSCODE(phTmp, abRAPDU, dwOut);
  1399. }
  1400. else
  1401. ret = (SCODE)dwRet;
  1402. // Parameters UnMarshaling
  1403. if (ret == S_OK)
  1404. {
  1405. }
  1406. }
  1407. __except(EXCEPTION_EXECUTE_HANDLER) {
  1408. switch(GetExceptionCode())
  1409. {
  1410. case STATUS_INVALID_PARAM:
  1411. ret = SCW_E_INVALIDPARAM;
  1412. break;
  1413. case STATUS_INSUFFICIENT_MEM:
  1414. ret = SCW_E_CANTMARSHAL;
  1415. break;
  1416. case STATUS_INTERNAL_ERROR:
  1417. ret = SCARD_F_INTERNAL_ERROR;
  1418. break;
  1419. default:
  1420. ret = SCARD_F_UNKNOWN_ERROR;
  1421. }
  1422. }
  1423. return ret;
  1424. }
  1425. SCODE WINAPI hScwSetDispatchTable(SCARDHANDLE hCard, WCSTR wszFileName)
  1426. {
  1427. SCODE ret;
  1428. DWORD dwOut, dwRet;
  1429. BYTE abCAPDU[5+MAX_APDU];
  1430. BYTE abRAPDU[MAX_APDU];
  1431. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  1432. LOG_BEGIN_PROXY(hScwSetDispatchTable);
  1433. // Marshaling
  1434. __try {
  1435. if (phTmp == NULL)
  1436. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  1437. if ((phTmp->dwFlags & FLAG_ISPROXY) == 0)
  1438. return SCW_E_NOTIMPLEMENTED;
  1439. InitXSCM(phTmp, abCAPDU, phTmp->bResLen);
  1440. CLA(0x00);
  1441. INS(phTmp->byINS);
  1442. P1(3);
  1443. P2(17);
  1444. Lc(0);
  1445. UINT82XSCM(&phTmp->xSCM, 1, TYPE_NOTYPE_COUNT); // Number of parameters
  1446. String2XSCM(&phTmp->xSCM, wszFileName, phTmp->dwFlags & FLAG_BIGENDIAN);
  1447. // API transfer
  1448. UPDATE_Lc((UINT8)(GetSCMBufferLength(&phTmp->xSCM)-5));
  1449. dwOut = MAX_APDU;
  1450. dwRet = SCWTransmit(hCard, abCAPDU, (DWORD)GetSCMBufferLength(&phTmp->xSCM), abRAPDU, &dwOut);
  1451. if (dwRet == 0)
  1452. { // Return code UnMarshaling
  1453. ret = ExtractSCODE(phTmp, abRAPDU, dwOut);
  1454. }
  1455. else
  1456. ret = (SCODE)dwRet;
  1457. // Parameters UnMarshaling
  1458. if (ret == S_OK)
  1459. {
  1460. }
  1461. }
  1462. __except(EXCEPTION_EXECUTE_HANDLER) {
  1463. switch(GetExceptionCode())
  1464. {
  1465. case STATUS_INVALID_PARAM:
  1466. ret = SCW_E_INVALIDPARAM;
  1467. break;
  1468. case STATUS_INSUFFICIENT_MEM:
  1469. ret = SCW_E_CANTMARSHAL;
  1470. break;
  1471. case STATUS_INTERNAL_ERROR:
  1472. ret = SCARD_F_INTERNAL_ERROR;
  1473. break;
  1474. default:
  1475. ret = SCARD_F_UNKNOWN_ERROR;
  1476. }
  1477. }
  1478. return ret;
  1479. }
  1480. /*
  1481. ** Cryptography
  1482. */
  1483. SCODE WINAPI hScwCryptoInitialize(SCARDHANDLE hCard, BYTE mechanism, BYTE *key)
  1484. {
  1485. SCODE ret;
  1486. DWORD dwOut, dwRet;
  1487. BYTE abCAPDU[5+MAX_APDU];
  1488. BYTE abRAPDU[MAX_APDU];
  1489. TCOUNT len = 0;
  1490. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  1491. LOG_BEGIN_PROXY(hScwCryptoInitialize);
  1492. // Marshaling
  1493. __try {
  1494. if (phTmp == NULL)
  1495. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  1496. if ((phTmp->dwFlags & FLAG_ISPROXY) == 0)
  1497. return SCW_E_NOTIMPLEMENTED;
  1498. InitXSCM(phTmp, abCAPDU, phTmp->bResLen);
  1499. CLA(0x00);
  1500. INS(phTmp->byINS);
  1501. P1(5);
  1502. P2(1);
  1503. Lc(0);
  1504. UINT82XSCM(&phTmp->xSCM, 2, TYPE_NOTYPE_COUNT); // Number of parameters
  1505. UINT82XSCM(&phTmp->xSCM, mechanism, TYPE_TYPED);
  1506. if (key)
  1507. len = 2 + key[1]; // T+L+V
  1508. ByteArray2XSCM(&phTmp->xSCM, key, len);
  1509. // API transfer
  1510. UPDATE_Lc((UINT8)(GetSCMBufferLength(&phTmp->xSCM)-5));
  1511. dwOut = MAX_APDU;
  1512. dwRet = SCWTransmit(hCard, abCAPDU, (DWORD)GetSCMBufferLength(&phTmp->xSCM), abRAPDU, &dwOut);
  1513. if (dwRet == 0)
  1514. { // Return code UnMarshaling
  1515. ret = ExtractSCODE(phTmp, abRAPDU, dwOut);
  1516. }
  1517. else
  1518. ret = (SCODE)dwRet;
  1519. // Parameters UnMarshaling
  1520. if (ret == S_OK)
  1521. {
  1522. phTmp->byCryptoM = mechanism; // Store the last mechanism for 1024 hack
  1523. }
  1524. }
  1525. __except(EXCEPTION_EXECUTE_HANDLER) {
  1526. switch(GetExceptionCode())
  1527. {
  1528. case STATUS_INVALID_PARAM:
  1529. ret = SCW_E_INVALIDPARAM;
  1530. break;
  1531. case STATUS_INSUFFICIENT_MEM:
  1532. ret = SCW_E_CANTMARSHAL;
  1533. break;
  1534. case STATUS_INTERNAL_ERROR:
  1535. ret = SCARD_F_INTERNAL_ERROR;
  1536. break;
  1537. default:
  1538. ret = SCARD_F_UNKNOWN_ERROR;
  1539. }
  1540. }
  1541. return ret;
  1542. }
  1543. SCODE WINAPI hScwCryptoAction(SCARDHANDLE hCard, BYTE *dataIn, TCOUNT dataInLength, BYTE *dataOut, TCOUNT *dataOutLength)
  1544. {
  1545. BOOL fHack = FALSE;
  1546. SCODE ret;
  1547. DWORD dwOut, dwRet;
  1548. BYTE abCAPDU[5+MAX_APDU];
  1549. BYTE abRAPDU[MAX_APDU];
  1550. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  1551. LOG_BEGIN_PROXY(hScwCryptoAction);
  1552. // Marshaling
  1553. __try {
  1554. if (phTmp == NULL)
  1555. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  1556. if ((phTmp->dwFlags & FLAG_ISPROXY) == 0)
  1557. return SCW_E_NOTIMPLEMENTED;
  1558. InitXSCM(phTmp, abCAPDU, phTmp->bResLen);
  1559. CLA(0x00);
  1560. INS(phTmp->byINS);
  1561. if ((((phTmp->byCryptoM & CM_CRYPTO_NAME) == CM_RSA) || ((phTmp->byCryptoM & CM_CRYPTO_NAME) == CM_RSA_CRT)) &&
  1562. ((phTmp->byCryptoM & CM_DATA_INFILE) != CM_DATA_INFILE))
  1563. { // Hack for 1024 RSA
  1564. P1(0xFE);
  1565. P2(0);
  1566. Lc(0);
  1567. ByteArray2XSCM(&phTmp->xSCM, dataIn, dataInLength);
  1568. if (dataOutLength == NULL)
  1569. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  1570. abCAPDU[5] = *dataOutLength;
  1571. fHack = TRUE;
  1572. }
  1573. else
  1574. {
  1575. P1(5);
  1576. P2(2);
  1577. Lc(0);
  1578. UINT82XSCM(&phTmp->xSCM, 3, TYPE_NOTYPE_COUNT); // Number of parameters
  1579. ByteArray2XSCM(&phTmp->xSCM, dataIn, dataInLength);
  1580. ByteArrayOut2XSCM(&phTmp->xSCM, dataOut, (TCOUNT)(dataOutLength == 0 ? 0 : *dataOutLength));
  1581. UINT8BYREF2XSCM(&phTmp->xSCM, dataOutLength);
  1582. }
  1583. // API transfer
  1584. UPDATE_Lc((UINT8)(GetSCMBufferLength(&phTmp->xSCM)-5));
  1585. dwOut = MAX_APDU;
  1586. dwRet = SCWTransmit(hCard, abCAPDU, (DWORD)GetSCMBufferLength(&phTmp->xSCM), abRAPDU, &dwOut);
  1587. if (dwRet == 0)
  1588. { // Return code UnMarshaling
  1589. ret = ExtractSCODE(phTmp, abRAPDU, dwOut);
  1590. }
  1591. else
  1592. ret = (SCODE)dwRet;
  1593. // Parameters UnMarshaling
  1594. if (ret == S_OK)
  1595. {
  1596. BYTE *pb;
  1597. TCOUNT len;
  1598. len = XSCM2ByteArray(&phTmp->xSCM, &pb);
  1599. if (len > *dataOutLength)
  1600. ret = SCW_E_BUFFERTOOSMALL;
  1601. else
  1602. {
  1603. memcpy(dataOut, pb, len);
  1604. if (fHack)
  1605. *dataOutLength = len;
  1606. else
  1607. *dataOutLength = XSCM2UINT8(&phTmp->xSCM);
  1608. }
  1609. }
  1610. }
  1611. __except(EXCEPTION_EXECUTE_HANDLER) {
  1612. switch(GetExceptionCode())
  1613. {
  1614. case STATUS_INVALID_PARAM:
  1615. ret = SCW_E_INVALIDPARAM;
  1616. break;
  1617. case STATUS_INSUFFICIENT_MEM:
  1618. ret = SCW_E_CANTMARSHAL;
  1619. break;
  1620. case STATUS_INTERNAL_ERROR:
  1621. ret = SCARD_F_INTERNAL_ERROR;
  1622. break;
  1623. default:
  1624. ret = SCARD_F_UNKNOWN_ERROR;
  1625. }
  1626. }
  1627. return ret;
  1628. }
  1629. SCODE WINAPI hScwCryptoUpdate(SCARDHANDLE hCard, BYTE *dataIn, TCOUNT dataInLength)
  1630. {
  1631. SCODE ret;
  1632. DWORD dwOut, dwRet;
  1633. BYTE abCAPDU[5+MAX_APDU];
  1634. BYTE abRAPDU[MAX_APDU];
  1635. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  1636. LOG_BEGIN_PROXY(hScwCryptoUpdate);
  1637. // Marshaling
  1638. __try {
  1639. if (phTmp == NULL)
  1640. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  1641. if ((phTmp->dwFlags & FLAG_ISPROXY) == 0)
  1642. return SCW_E_NOTIMPLEMENTED;
  1643. InitXSCM(phTmp, abCAPDU, phTmp->bResLen);
  1644. CLA(0x00);
  1645. INS(phTmp->byINS);
  1646. P1(5);
  1647. P2(3);
  1648. Lc(0);
  1649. UINT82XSCM(&phTmp->xSCM, 1, TYPE_NOTYPE_COUNT); // Number of parameters
  1650. ByteArray2XSCM(&phTmp->xSCM, dataIn, dataInLength);
  1651. // API transfer
  1652. UPDATE_Lc((UINT8)(GetSCMBufferLength(&phTmp->xSCM)-5));
  1653. dwOut = MAX_APDU;
  1654. dwRet = SCWTransmit(hCard, abCAPDU, (DWORD)GetSCMBufferLength(&phTmp->xSCM), abRAPDU, &dwOut);
  1655. if (dwRet == 0)
  1656. { // Return code UnMarshaling
  1657. ret = ExtractSCODE(phTmp, abRAPDU, dwOut);
  1658. }
  1659. else
  1660. ret = (SCODE)dwRet;
  1661. // Parameters UnMarshaling
  1662. if (ret == S_OK)
  1663. {
  1664. }
  1665. }
  1666. __except(EXCEPTION_EXECUTE_HANDLER) {
  1667. switch(GetExceptionCode())
  1668. {
  1669. case STATUS_INVALID_PARAM:
  1670. ret = SCW_E_INVALIDPARAM;
  1671. break;
  1672. case STATUS_INSUFFICIENT_MEM:
  1673. ret = SCW_E_CANTMARSHAL;
  1674. break;
  1675. case STATUS_INTERNAL_ERROR:
  1676. ret = SCARD_F_INTERNAL_ERROR;
  1677. break;
  1678. default:
  1679. ret = SCARD_F_UNKNOWN_ERROR;
  1680. }
  1681. }
  1682. return ret;
  1683. }
  1684. SCODE WINAPI hScwCryptoFinalize(SCARDHANDLE hCard, BYTE *dataOut, TCOUNT *dataOutLength)
  1685. {
  1686. SCODE ret;
  1687. DWORD dwOut, dwRet;
  1688. BYTE abCAPDU[5+MAX_APDU];
  1689. BYTE abRAPDU[MAX_APDU];
  1690. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  1691. LOG_BEGIN_PROXY(hScwCryptoFinalize);
  1692. // Marshaling
  1693. __try {
  1694. if (phTmp == NULL)
  1695. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  1696. if ((phTmp->dwFlags & FLAG_ISPROXY) == 0)
  1697. return SCW_E_NOTIMPLEMENTED;
  1698. InitXSCM(phTmp, abCAPDU, phTmp->bResLen);
  1699. CLA(0x00);
  1700. INS(phTmp->byINS);
  1701. P1(5);
  1702. P2(4);
  1703. Lc(0);
  1704. UINT82XSCM(&phTmp->xSCM, 2, TYPE_NOTYPE_COUNT); // Number of parameters
  1705. ByteArrayOut2XSCM(&phTmp->xSCM, dataOut, (TCOUNT)(dataOutLength == 0 ? 0 : *dataOutLength));
  1706. UINT8BYREF2XSCM(&phTmp->xSCM, dataOutLength);
  1707. // API transfer
  1708. UPDATE_Lc((UINT8)(GetSCMBufferLength(&phTmp->xSCM)-5));
  1709. dwOut = MAX_APDU;
  1710. dwRet = SCWTransmit(hCard, abCAPDU, (DWORD)GetSCMBufferLength(&phTmp->xSCM), abRAPDU, &dwOut);
  1711. if (dwRet == 0)
  1712. { // Return code UnMarshaling
  1713. ret = ExtractSCODE(phTmp, abRAPDU, dwOut);
  1714. }
  1715. else
  1716. ret = (SCODE)dwRet;
  1717. // Parameters UnMarshaling
  1718. if (ret == S_OK)
  1719. {
  1720. BYTE *pb;
  1721. TCOUNT len;
  1722. len = XSCM2ByteArray(&phTmp->xSCM, &pb);
  1723. if (len > *dataOutLength)
  1724. ret = SCW_E_BUFFERTOOSMALL;
  1725. else
  1726. {
  1727. memcpy(dataOut, pb, len);
  1728. *dataOutLength = XSCM2UINT8(&phTmp->xSCM);
  1729. }
  1730. }
  1731. }
  1732. __except(EXCEPTION_EXECUTE_HANDLER) {
  1733. switch(GetExceptionCode())
  1734. {
  1735. case STATUS_INVALID_PARAM:
  1736. ret = SCW_E_INVALIDPARAM;
  1737. break;
  1738. case STATUS_INSUFFICIENT_MEM:
  1739. ret = SCW_E_CANTMARSHAL;
  1740. break;
  1741. case STATUS_INTERNAL_ERROR:
  1742. ret = SCARD_F_INTERNAL_ERROR;
  1743. break;
  1744. default:
  1745. ret = SCARD_F_UNKNOWN_ERROR;
  1746. }
  1747. }
  1748. return ret;
  1749. }
  1750. SCODE WINAPI hScwGenerateRandom(SCARDHANDLE hCard, BYTE *dataOut, TCOUNT dataOutLength)
  1751. {
  1752. SCODE ret;
  1753. DWORD dwOut, dwRet;
  1754. BYTE abCAPDU[5+MAX_APDU];
  1755. BYTE abRAPDU[MAX_APDU];
  1756. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  1757. LOG_BEGIN_PROXY(hScwGenerateRandom);
  1758. // Marshaling
  1759. __try {
  1760. if (phTmp == NULL)
  1761. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  1762. if ((phTmp->dwFlags & FLAG_ISPROXY) == 0)
  1763. return SCW_E_NOTIMPLEMENTED;
  1764. InitXSCM(phTmp, abCAPDU, phTmp->bResLen);
  1765. CLA(0x00);
  1766. INS(phTmp->byINS);
  1767. P1(5);
  1768. P2(5);
  1769. Lc(0);
  1770. UINT82XSCM(&phTmp->xSCM, 2, TYPE_NOTYPE_COUNT); // Number of parameters
  1771. ByteArrayOut2XSCM(&phTmp->xSCM, dataOut, dataOutLength);
  1772. UINT8BYREF2XSCM(&phTmp->xSCM, &dataOutLength);
  1773. // API transfer
  1774. UPDATE_Lc((UINT8)(GetSCMBufferLength(&phTmp->xSCM)-5));
  1775. dwOut = MAX_APDU;
  1776. dwRet = SCWTransmit(hCard, abCAPDU, (DWORD)GetSCMBufferLength(&phTmp->xSCM), abRAPDU, &dwOut);
  1777. if (dwRet == 0)
  1778. { // Return code UnMarshaling
  1779. ret = ExtractSCODE(phTmp, abRAPDU, dwOut);
  1780. }
  1781. else
  1782. ret = (SCODE)dwRet;
  1783. // Parameters UnMarshaling
  1784. if (ret == S_OK)
  1785. {
  1786. BYTE *pb;
  1787. TCOUNT len;
  1788. len = XSCM2ByteArray(&phTmp->xSCM, &pb);
  1789. if (len != dataOutLength)
  1790. ret = SCW_E_BUFFERTOOSMALL;
  1791. else
  1792. memcpy(dataOut, pb, len);
  1793. }
  1794. }
  1795. __except(EXCEPTION_EXECUTE_HANDLER) {
  1796. switch(GetExceptionCode())
  1797. {
  1798. case STATUS_INVALID_PARAM:
  1799. ret = SCW_E_INVALIDPARAM;
  1800. break;
  1801. case STATUS_INSUFFICIENT_MEM:
  1802. ret = SCW_E_CANTMARSHAL;
  1803. break;
  1804. case STATUS_INTERNAL_ERROR:
  1805. ret = SCARD_F_INTERNAL_ERROR;
  1806. break;
  1807. default:
  1808. ret = SCARD_F_UNKNOWN_ERROR;
  1809. }
  1810. }
  1811. return ret;
  1812. }
  1813. /*
  1814. ** Runtime Environment
  1815. */
  1816. SCODE WINAPI hScwRTEExecute(SCARDHANDLE hCard, WCSTR wszCodeFileName, WCSTR wszDataFileName, UINT8 bRestart)
  1817. {
  1818. SCODE ret;
  1819. DWORD dwOut, dwRet;
  1820. BYTE abCAPDU[5+MAX_APDU];
  1821. BYTE abRAPDU[MAX_APDU];
  1822. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  1823. LOG_BEGIN_PROXY(hScwRTEExecute);
  1824. // Marshaling
  1825. __try {
  1826. if (phTmp == NULL)
  1827. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  1828. if ((phTmp->dwFlags & FLAG_ISPROXY) == 0)
  1829. return SCW_E_NOTIMPLEMENTED;
  1830. InitXSCM(phTmp, abCAPDU, phTmp->bResLen);
  1831. CLA(0x00);
  1832. INS(phTmp->byINS);
  1833. P1(1);
  1834. P2(1);
  1835. Lc(0);
  1836. UINT82XSCM(&phTmp->xSCM, 3, TYPE_NOTYPE_COUNT); // Number of parameters
  1837. String2XSCM(&phTmp->xSCM, wszCodeFileName, phTmp->dwFlags & FLAG_BIGENDIAN);
  1838. String2XSCM(&phTmp->xSCM, wszDataFileName, phTmp->dwFlags & FLAG_BIGENDIAN);
  1839. UINT82XSCM(&phTmp->xSCM, bRestart, TYPE_TYPED);
  1840. // API transfer
  1841. UPDATE_Lc((UINT8)(GetSCMBufferLength(&phTmp->xSCM)-5));
  1842. dwOut = MAX_APDU;
  1843. dwRet = SCWTransmit(hCard, abCAPDU, (DWORD)GetSCMBufferLength(&phTmp->xSCM), abRAPDU, &dwOut);
  1844. if (dwRet == 0)
  1845. { // Return code UnMarshaling
  1846. ret = ExtractSCODE(phTmp, abRAPDU, dwOut);
  1847. }
  1848. else
  1849. ret = (SCODE)dwRet;
  1850. // Parameters UnMarshaling
  1851. if (ret == S_OK)
  1852. {
  1853. }
  1854. }
  1855. __except(EXCEPTION_EXECUTE_HANDLER) {
  1856. switch(GetExceptionCode())
  1857. {
  1858. case STATUS_INVALID_PARAM:
  1859. ret = SCW_E_INVALIDPARAM;
  1860. break;
  1861. case STATUS_INSUFFICIENT_MEM:
  1862. ret = SCW_E_CANTMARSHAL;
  1863. break;
  1864. case STATUS_INTERNAL_ERROR:
  1865. ret = SCARD_F_INTERNAL_ERROR;
  1866. break;
  1867. default:
  1868. ret = SCARD_F_UNKNOWN_ERROR;
  1869. }
  1870. }
  1871. return ret;
  1872. }
  1873. /*
  1874. ScwExecute:
  1875. I-: lpxHdr (points to 4 bytes (CLA, INS, P1, P2))
  1876. I-: InBuf (Incoming data from card's perspective (NULL -> no data in))
  1877. I-: InBufLen (length of data pointed by InBuf)
  1878. -O: OutBuf (Buffer that will receive the R-APDU (NULL -> no expected data))
  1879. IO: pOutBufLen (I -> Size of OutBuf, O -> Number of bytes written in OutBuf)
  1880. -O: pwSW (Card Status Word)
  1881. */
  1882. SCODE WINAPI hScwExecute(SCARDHANDLE hCard, LPISO_HEADER lpxHdr, BYTE *InBuf, TCOUNT InBufLen, BYTE *OutBuf, TCOUNT *pOutBufLen, UINT16 *pwSW)
  1883. {
  1884. SCODE ret;
  1885. DWORD dwIn, dwOut, dwRet;
  1886. BYTE abCAPDU[5+MAX_APDU];
  1887. BYTE abRAPDU[MAX_APDU];
  1888. LPMYSCARDHANDLE phTmp = (LPMYSCARDHANDLE)hCard;
  1889. LOG_BEGIN_PROXY(hScwExecute);
  1890. __try {
  1891. if ((lpxHdr == NULL) || (pwSW == NULL) || ((OutBuf != NULL) && (pOutBufLen == NULL)))
  1892. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  1893. if (phTmp == NULL)
  1894. RaiseException(STATUS_INVALID_PARAM, 0, 0, 0);
  1895. abCAPDU[0] = lpxHdr->CLA;
  1896. abCAPDU[1] = lpxHdr->INS;
  1897. abCAPDU[2] = lpxHdr->P1;
  1898. abCAPDU[3] = lpxHdr->P2;
  1899. if ((InBuf != NULL) && (InBufLen != 0))
  1900. {
  1901. abCAPDU[4] = (BYTE)InBufLen;
  1902. memcpy(abCAPDU+5, InBuf, InBufLen);
  1903. dwIn = 5 + InBufLen;
  1904. // We don't care about out data yet
  1905. }
  1906. else
  1907. { // No in data. How much data out then?
  1908. dwIn = 5;
  1909. if (OutBuf == NULL) // No data out either
  1910. {
  1911. abCAPDU[4] = 0;
  1912. if (phTmp->dwProtocol == SCARD_PROTOCOL_T0)
  1913. dwIn = 4; // To indicate a case 1 command
  1914. }
  1915. else
  1916. abCAPDU[4] = (BYTE)(*pOutBufLen);
  1917. }
  1918. // API transfer
  1919. dwOut = MAX_APDU;
  1920. dwRet = SCWTransmit(hCard, abCAPDU, dwIn, abRAPDU, &dwOut);
  1921. if (dwRet == 0)
  1922. {
  1923. if (dwOut < 2)
  1924. ret = SCARD_F_INTERNAL_ERROR;
  1925. else
  1926. {
  1927. *pwSW = MAKEWORD(abRAPDU[dwOut-1], abRAPDU[dwOut-2]);
  1928. dwOut -= 2;
  1929. ret = 0;
  1930. if (OutBuf != NULL)
  1931. {
  1932. if (dwOut <= (DWORD)(*pOutBufLen))
  1933. {
  1934. memcpy(OutBuf, abRAPDU, dwOut);
  1935. *pOutBufLen = (TCOUNT)dwOut;
  1936. }
  1937. else
  1938. ret = SCW_E_BUFFERTOOSMALL;
  1939. }
  1940. }
  1941. }
  1942. else
  1943. ret = (SCODE)dwRet;
  1944. }
  1945. __except(EXCEPTION_EXECUTE_HANDLER) {
  1946. switch(GetExceptionCode())
  1947. {
  1948. case STATUS_INVALID_PARAM:
  1949. ret = SCW_E_INVALIDPARAM;
  1950. break;
  1951. case STATUS_INSUFFICIENT_MEM:
  1952. ret = SCW_E_CANTMARSHAL;
  1953. break;
  1954. case STATUS_INTERNAL_ERROR:
  1955. ret = SCARD_F_INTERNAL_ERROR;
  1956. break;
  1957. default:
  1958. ret = SCARD_F_UNKNOWN_ERROR;
  1959. }
  1960. }
  1961. return ret;
  1962. }
  1963. static SCODE ExtractSCODE(LPMYSCARDHANDLE phTmp, LPCBYTE abRAPDU, DWORD dwOut)
  1964. {
  1965. if (FLAG2VERSION(phTmp->dwFlags) == VERSION_1_0)
  1966. {
  1967. if ((dwOut < 2) || (abRAPDU[dwOut-2] != 0x90) || (abRAPDU[dwOut-1] != 0x00))
  1968. RaiseException(STATUS_INTERNAL_ERROR, 0, 0, 0);
  1969. InitXSCM(phTmp, abRAPDU, (WORD)(dwOut-2)); // Doesn't take SW into account
  1970. return XSCM2SCODE(&phTmp->xSCM);
  1971. }
  1972. else if (FLAG2VERSION(phTmp->dwFlags) == VERSION_1_1)
  1973. {
  1974. if ((dwOut < 2) || (abRAPDU[dwOut-2] != 0x90))
  1975. RaiseException(STATUS_INTERNAL_ERROR, 0, 0, 0);
  1976. InitXSCM(phTmp, abRAPDU, (WORD)(dwOut-2)); // Doesn't take SW into account
  1977. return MAKESCODE(abRAPDU[dwOut-1]);
  1978. }
  1979. else
  1980. RaiseException(STATUS_INTERNAL_ERROR, 0, 0, 0);
  1981. return SCW_S_OK; // to please the compiler
  1982. }