Source code of Windows XP (NT5)
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.

557 lines
15 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1997 - 1999
  3. Module Name:
  4. example.cpp
  5. Abstract:
  6. This is a plug-in for the smart card driver test suite.
  7. This plug-in is smart card dependent
  8. Author:
  9. Klaus U. Schutz
  10. Environment:
  11. Win32 application
  12. Revision History :
  13. Nov. 1997 - initial version
  14. --*/
  15. #include <stdarg.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <time.h>
  19. #include <afx.h>
  20. #include <afxtempl.h>
  21. #include <winioctl.h>
  22. #include <winsmcrd.h>
  23. #include "ifdtest.h"
  24. void
  25. SLBTestCardEntry(
  26. class CCardProvider& in_CCardProvider
  27. );
  28. //
  29. // Create a card provider object
  30. // Note: all global varibales and all functions have to be static
  31. //
  32. static class CCardProvider SLBTestCard(SLBTestCardEntry);
  33. //Pauses for a specified number of milliseconds.
  34. static void
  35. sleep(
  36. clock_t wait
  37. )
  38. {
  39. clock_t goal;
  40. goal = wait + clock();
  41. while( goal > clock() )
  42. ;
  43. }
  44. static ULONG
  45. SLBTestCardSetProtocol(
  46. class CCardProvider& in_CCardProvider,
  47. class CReader& in_CReader
  48. )
  49. /*++
  50. Routine Description:
  51. This function will be called after the card has been correctly
  52. identified. We should here set the protocol that we need
  53. for further transmissions
  54. Arguments:
  55. in_CCardProvider - ref. to our card provider object
  56. in_CReader - ref. to the reader object
  57. Return Value:
  58. IFDSTATUS_FAILED - we were unable to set the protocol correctly
  59. IFDSTATUS_SUCCESS - protocol set correctly
  60. --*/
  61. {
  62. ULONG l_lResult;
  63. // Try to set INCORRECT protocol T=1
  64. TestStart("Try to set incorrect protocol T=1");
  65. l_lResult = in_CReader.SetProtocol(SCARD_PROTOCOL_T1);
  66. // The test MUST fail with the incorrect protocol
  67. TEST_CHECK_NOT_SUPPORTED("Set protocol failed", l_lResult);
  68. TestEnd();
  69. // Now set the correct protocol
  70. TestStart("Set protocol T=0");
  71. l_lResult = in_CReader.SetProtocol(SCARD_PROTOCOL_T0);
  72. TEST_CHECK_SUCCESS("Set protocol failed", l_lResult);
  73. TestEnd();
  74. if (l_lResult != ERROR_SUCCESS) {
  75. return IFDSTATUS_FAILED;
  76. }
  77. return IFDSTATUS_SUCCESS;
  78. }
  79. static
  80. ULONG
  81. SLBTestCardTest(
  82. class CCardProvider& in_CCardProvider,
  83. class CReader& in_CReader
  84. )
  85. /*++
  86. Routine Description:
  87. This serves as the test function for a particular smart card
  88. Arguments:
  89. in_CReader - ref. to class that provides all information for the test
  90. Return Value:
  91. IFDSTATUS value
  92. --*/
  93. {
  94. ULONG l_lResult, l_uResultLength, l_uIndex;
  95. PBYTE l_pbResult;
  96. BYTE l_rgbBuffer[512];
  97. CHAR l_chFileId;
  98. // First select the file
  99. if (in_CCardProvider.GetTestNo() < 6) {
  100. //
  101. // Select the appropriate file for the test
  102. // Each test is tied to a particular file
  103. //
  104. l_chFileId = (CHAR) in_CCardProvider.GetTestNo();
  105. PCHAR l_pchFileDesc[] = {
  106. "transferAllBytes",
  107. "transferNextByte",
  108. "read256Bytes",
  109. "case1Apdu",
  110. "restartWorkWaitingTime"
  111. };
  112. // APDU for select file
  113. memcpy(l_rgbBuffer, "\x00\xa4\x00\x00\x02\x00\x00", 7);
  114. // add file number to select
  115. l_rgbBuffer[6] = l_chFileId;
  116. // select a file
  117. TestStart("SELECT FILE EF%s", l_pchFileDesc[l_chFileId - 1]);
  118. sleep( (clock_t) 1 * (CLOCKS_PER_SEC / 2) );
  119. l_lResult = in_CReader.Transmit(
  120. (PBYTE) l_rgbBuffer,
  121. 7,
  122. &l_pbResult,
  123. &l_uResultLength
  124. );
  125. TestCheck(
  126. l_lResult, "==", ERROR_SUCCESS,
  127. l_uResultLength, 2,
  128. l_pbResult[0], l_pbResult[1], 0x90, 0x00,
  129. NULL, NULL, NULL
  130. );
  131. TEST_END();
  132. //
  133. // Generate a 'test' pattern which will be written to the card
  134. //
  135. for (l_uIndex = 0; l_uIndex < 256; l_uIndex++) {
  136. l_rgbBuffer[5 + l_uIndex] = (UCHAR) l_uIndex;
  137. }
  138. }
  139. switch (in_CCardProvider.GetTestNo()) {
  140. case 1:
  141. case 2: {
  142. //
  143. // Write
  144. //
  145. ULONG l_auNumBytes[] = { 1 , 25 }; //, 50, 75, 100, 125 };
  146. for (ULONG l_uTest = 0;
  147. l_uTest < sizeof(l_auNumBytes) / sizeof(l_auNumBytes[0]);
  148. l_uTest++) {
  149. ULONG l_uNumBytes = l_auNumBytes[l_uTest];
  150. TestStart("WRITE BINARY %3d Byte(s)", l_uNumBytes);
  151. // Tpdu for write binary
  152. memcpy(l_rgbBuffer, "\x00\xd6\x00\x00", 4);
  153. // Append number of bytes (note: the buffer contains the pattern already)
  154. l_rgbBuffer[4] = (UCHAR) l_uNumBytes;
  155. sleep( (clock_t) 1 * (CLOCKS_PER_SEC / 4) );
  156. l_lResult = in_CReader.Transmit(
  157. l_rgbBuffer,
  158. 5 + l_uNumBytes,
  159. &l_pbResult,
  160. &l_uResultLength
  161. );
  162. TestCheck(
  163. l_lResult, "==", ERROR_SUCCESS,
  164. l_uResultLength, 2,
  165. l_pbResult[0], l_pbResult[1], 0x90, 0x00,
  166. NULL, NULL, NULL
  167. );
  168. TEST_END();
  169. }
  170. break;
  171. }
  172. case 3: {
  173. // Test read of 256 bytes
  174. ULONG l_uNumBytes = 256;
  175. TestStart("READ BINARY %3d Byte(s)", l_uNumBytes);
  176. // tpdu for read binary 256 bytes
  177. memcpy(l_rgbBuffer, "\x00\xb0\x00\x00\x00", 5);
  178. sleep((clock_t) 1 * (CLOCKS_PER_SEC / 2) );
  179. l_lResult = in_CReader.Transmit(
  180. l_rgbBuffer,
  181. 5,
  182. &l_pbResult,
  183. &l_uResultLength
  184. );
  185. TestCheck(
  186. l_lResult, "==", ERROR_SUCCESS,
  187. l_uResultLength, l_uNumBytes + 2,
  188. l_pbResult[l_uNumBytes], l_pbResult[l_uNumBytes + 1], 0x90, 0x00,
  189. l_pbResult, l_rgbBuffer + 5, l_uNumBytes
  190. );
  191. TEST_END();
  192. break;
  193. }
  194. case 4: {
  195. // Test write of 0 bytes
  196. TestStart("WRITE BINARY %3d Byte", 0);
  197. // tpdu for write binary
  198. memcpy(l_rgbBuffer, "\x00\xd6\x00\x00", 4);
  199. sleep((clock_t) 1 * (CLOCKS_PER_SEC / 2) );
  200. l_lResult = in_CReader.Transmit(
  201. l_rgbBuffer,
  202. 4,
  203. &l_pbResult,
  204. &l_uResultLength
  205. );
  206. TestCheck(
  207. l_lResult, "==", ERROR_SUCCESS,
  208. l_uResultLength, 2,
  209. l_pbResult[0], l_pbResult[1], 0x90, 0x00,
  210. NULL, NULL, 0
  211. );
  212. TEST_END();
  213. break;
  214. }
  215. case 5: {
  216. // Test restart or work waiting time
  217. ULONG l_auNumBytes[] = { 1, 2, 5, 30 };
  218. for (ULONG l_uTest = 0;
  219. l_uTest < sizeof(l_auNumBytes) / sizeof(l_auNumBytes[0]);
  220. l_uTest++) {
  221. ULONG l_uNumBytes = l_auNumBytes[l_uTest];
  222. TestStart("READ BINARY %3d Byte(s)", l_uNumBytes);
  223. // tpdu for read binary
  224. memcpy(l_rgbBuffer, "\x00\xb0\x00\x00", 4);
  225. // Append number of bytes
  226. l_rgbBuffer[4] = (UCHAR)l_uNumBytes;
  227. sleep( (clock_t) 1 * (CLOCKS_PER_SEC / 2) );
  228. l_lResult = in_CReader.Transmit(
  229. l_rgbBuffer,
  230. 5,
  231. &l_pbResult,
  232. &l_uResultLength
  233. );
  234. TestCheck(
  235. l_lResult, "==", ERROR_SUCCESS,
  236. l_uResultLength, l_uNumBytes + 2,
  237. l_pbResult[l_uNumBytes], l_pbResult[l_uNumBytes + 1], 0x90, 0x00,
  238. l_pbResult, l_rgbBuffer + 5, l_uNumBytes
  239. );
  240. TEST_END();
  241. }
  242. break;
  243. }
  244. case 6: {
  245. //
  246. // Read the result file from the smart card.
  247. // The card stores results of each test in
  248. // a special file
  249. //
  250. TestStart("SELECT FILE EFresult");
  251. sleep( (clock_t) 1 * (CLOCKS_PER_SEC / 2) );
  252. l_lResult = in_CReader.Transmit(
  253. (PBYTE) "\x00\xa4\x00\x00\x02\xa0\x00",
  254. 7,
  255. &l_pbResult,
  256. &l_uResultLength
  257. );
  258. TestCheck(
  259. l_lResult, "==", ERROR_SUCCESS,
  260. l_uResultLength, 2,
  261. l_pbResult[0], l_pbResult[1], 0x90, 0x00,
  262. NULL, NULL, NULL
  263. );
  264. TEST_END();
  265. // Read
  266. TestStart("READ BINARY FILE EFresult");
  267. // apdu for read binary
  268. memcpy(l_rgbBuffer, "\x00\xb0\x00\x00", 4);
  269. // Append number of bytes we want to read
  270. l_rgbBuffer[4] = (UCHAR) sizeof(T0_RESULT_FILE_HEADER);
  271. sleep( (clock_t) 1 * (CLOCKS_PER_SEC / 2) );
  272. l_lResult = in_CReader.Transmit(
  273. l_rgbBuffer,
  274. 5,
  275. &l_pbResult,
  276. &l_uResultLength
  277. );
  278. TestCheck(
  279. l_lResult, "==", ERROR_SUCCESS,
  280. l_uResultLength, sizeof(T0_RESULT_FILE_HEADER) + 2,
  281. l_pbResult[sizeof(T0_RESULT_FILE_HEADER)],
  282. l_pbResult[sizeof(T0_RESULT_FILE_HEADER) + 1],
  283. 0x90, 0x00,
  284. NULL, NULL, NULL
  285. );
  286. // get the card reset count
  287. PT0_RESULT_FILE_HEADER l_pCResultFileHeader;
  288. l_pCResultFileHeader = (PT0_RESULT_FILE_HEADER) l_pbResult;
  289. BYTE l_bCardResetCount = l_pCResultFileHeader->CardResetCount;
  290. // set the offset from where we want to read
  291. l_rgbBuffer[3] = (BYTE) l_pCResultFileHeader->Offset;
  292. // Append number of bytes
  293. l_rgbBuffer[4] = (BYTE) sizeof(T0_RESULT_FILE);
  294. sleep( (clock_t) 1 * (CLOCKS_PER_SEC / 2) );
  295. // read in the result data of the result file
  296. l_lResult = in_CReader.Transmit(
  297. l_rgbBuffer,
  298. 5,
  299. &l_pbResult,
  300. &l_uResultLength
  301. );
  302. TestCheck(
  303. l_lResult, "==", ERROR_SUCCESS,
  304. l_uResultLength, sizeof(T0_RESULT_FILE) + 2,
  305. l_pbResult[sizeof(T0_RESULT_FILE)],
  306. l_pbResult[sizeof(T0_RESULT_FILE) + 1],
  307. 0x90, 0x00,
  308. NULL, NULL, NULL
  309. );
  310. TEST_END();
  311. PT0_RESULT_FILE l_pCResultFile = (PT0_RESULT_FILE) l_pbResult;
  312. //
  313. // Now check the result file.
  314. //
  315. // procedure byte interpretation - write all bytes
  316. TestStart("'Transfer all remaining bytes result'");
  317. TestCheck(
  318. l_pCResultFile->TransferAllBytes.ResetCount == l_bCardResetCount,
  319. "Test not performed"
  320. );
  321. TestCheck(
  322. (l_pCResultFile->TransferAllBytes.Result & 0x01) == 0,
  323. "Smart card received incorrect data"
  324. );
  325. TestCheck(
  326. l_pCResultFile->TransferAllBytes.Result == 0,
  327. "Test failed. Error code %02xH",
  328. l_pCResultFile->TransferAllBytes.Result
  329. );
  330. TestEnd();
  331. // procedure byte interpretation - write single bytes
  332. TestStart("'Transfer next byte result'");
  333. TestCheck(
  334. l_pCResultFile->TransferNextByte.ResetCount == l_bCardResetCount,
  335. "Test not performed"
  336. );
  337. TestCheck(
  338. (l_pCResultFile->TransferNextByte.Result & 0x01) == 0,
  339. "Smart card received incorrect data"
  340. );
  341. TestCheck(
  342. l_pCResultFile->TransferNextByte.Result == 0,
  343. "Test failed. Error code %02xH",
  344. l_pCResultFile->TransferNextByte.Result
  345. );
  346. TestEnd();
  347. // Check read of 256 bytes
  348. TestStart("'Read 256 bytes bytes' result");
  349. TestCheck(
  350. l_pCResultFile->Read256Bytes.ResetCount == l_bCardResetCount,
  351. "Test not performed"
  352. );
  353. TestCheck(
  354. (l_pCResultFile->Read256Bytes.Result & 0x01) == 0,
  355. "Smart card received P3 != 0"
  356. );
  357. TestCheck(
  358. l_pCResultFile->Read256Bytes.Result == 0,
  359. "Test failed. Error code %02xH",
  360. l_pCResultFile->Read256Bytes.Result
  361. );
  362. TestEnd();
  363. // Test of case 1 APDU
  364. TestStart("'Case 1 APDU' result");
  365. TestCheck(
  366. l_pCResultFile->Case1Apdu.ResetCount == l_bCardResetCount,
  367. "Test not performed"
  368. );
  369. TestCheck(
  370. (l_pCResultFile->Case1Apdu.Result & 0x01) == 0,
  371. "Smart card received only 4-byte-TPDU"
  372. );
  373. TestCheck(
  374. (l_pCResultFile->Case1Apdu.Result & 0x02) == 0,
  375. "Smart card received P3 !=0"
  376. );
  377. TestCheck(
  378. l_pCResultFile->Case1Apdu.Result == 0,
  379. "Test failed. Error code %02xH",
  380. l_pCResultFile->Case1Apdu.Result
  381. );
  382. TestEnd();
  383. // Test of restart of work waiting time
  384. TestStart("'Restart of work waiting time' result");
  385. TestCheck(
  386. l_pCResultFile->RestartWorkWaitingTime.ResetCount == l_bCardResetCount,
  387. "Test not performed"
  388. );
  389. TestCheck(
  390. (l_pCResultFile->RestartWorkWaitingTime.Result & 0x01) == 0,
  391. "Smart card received only 4-byte-TPDU"
  392. );
  393. TestCheck(
  394. (l_pCResultFile->RestartWorkWaitingTime.Result & 0x02) == 0,
  395. "Smart card received P3 !=0"
  396. );
  397. TestCheck(
  398. l_pCResultFile->RestartWorkWaitingTime.Result == 0,
  399. "Test failed. Error code %02xH",
  400. l_pCResultFile->RestartWorkWaitingTime.Result
  401. );
  402. TestEnd();
  403. return IFDSTATUS_END;
  404. }
  405. default:
  406. return IFDSTATUS_FAILED;
  407. }
  408. return IFDSTATUS_SUCCESS;
  409. }
  410. static void
  411. SLBTestCardEntry(
  412. class CCardProvider& in_CCardProvider
  413. )
  414. /*++
  415. Routine Description:
  416. This function registers all callbacks from the test suite
  417. Arguments:
  418. CCardProvider - ref. to card provider class
  419. Return Value:
  420. -
  421. --*/
  422. {
  423. // Set protocol callback
  424. in_CCardProvider.SetProtocol(SLBTestCardSetProtocol);
  425. // Card test callback
  426. in_CCardProvider.SetCardTest(SLBTestCardTest);
  427. // Name of our card
  428. in_CCardProvider.SetCardName("SCHLUMBERGER");
  429. // Name of our card
  430. in_CCardProvider.SetAtr((PBYTE) "\x3b\xe2\x00\x00\x40\x20\x99\x01", 8);
  431. }