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.

498 lines
11 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 <afx.h>
  19. #include <afxtempl.h>
  20. #include <winioctl.h>
  21. #include "winsmcrd.h"
  22. #include "ifdtest.h"
  23. void MyCardEntry(class CCardProvider& in_CCardProvider);
  24. //
  25. // Create a card provider object
  26. // Note: all global varibales and all functions have to be static
  27. //
  28. static class CCardProvider MyCard(MyCardEntry);
  29. static ULONG
  30. MyCardSetProtocol(
  31. class CCardProvider& in_CCardProvider,
  32. class CReader& in_CReader
  33. )
  34. /*++
  35. Routine Description:
  36. This function will be called after the card has been correctly
  37. identified. We should here set the protocol that we need
  38. for further transmissions
  39. Arguments:
  40. in_CCardProvider - ref. to our card provider object
  41. in_CReader - ref. to the reader object
  42. Return Value:
  43. IFDSTATUS_FAILED - we were unable to set the protocol correctly
  44. IFDSTATUS_SUCCESS - protocol set correctly
  45. --*/
  46. {
  47. ULONG l_lResult;
  48. TestStart("Try to set incorrect protocol T=1");
  49. l_lResult = in_CReader.SetProtocol(SCARD_PROTOCOL_T1);
  50. // The test MUST fail with the incorrect protocol
  51. TEST_CHECK_NOT_SUPPORTED("Set protocol failed", l_lResult);
  52. TestEnd();
  53. // Now set the correct protocol
  54. TestStart("Set protocol T=0");
  55. l_lResult = in_CReader.SetProtocol(SCARD_PROTOCOL_T0);
  56. TEST_CHECK_SUCCESS("Set protocol failed", l_lResult);
  57. TestEnd();
  58. if (l_lResult != ERROR_SUCCESS) {
  59. return IFDSTATUS_FAILED;
  60. }
  61. return IFDSTATUS_SUCCESS;
  62. }
  63. static ULONG
  64. MyCardTest(
  65. class CCardProvider& in_CCardProvider,
  66. class CReader& in_CReader
  67. )
  68. /*++
  69. Routine Description:
  70. This serves as the test function for a particular smart card
  71. Arguments:
  72. in_CReader - ref. to class that provides all information for the test
  73. Return Value:
  74. IFDSTATUS value
  75. --*/
  76. {
  77. ULONG l_auNumBytes[] = { 1 , 25, 50, 75, 100, 125, 150, 175, 200, 225, 254 };
  78. ULONG l_uNumBytes = l_auNumBytes[10];
  79. ULONG l_lResult;
  80. ULONG l_uResultLength, l_uIndex;
  81. PUCHAR l_pchResult;
  82. UCHAR l_rgchBuffer[512];
  83. ULONG l_uTest;
  84. UCHAR Buf_Tempo[9];
  85. ULONG l_Tempo;
  86. ULONG Adresse;
  87. switch (in_CCardProvider.GetTestNo()) {
  88. case 1:
  89. TestStart("Buffer boundary test");
  90. //
  91. // Check if the reader correctly determines that
  92. // our receive buffer is too small
  93. //
  94. in_CReader.SetReplyBufferSize(9);
  95. l_lResult = in_CReader.Transmit(
  96. (PUCHAR) "\xBC\x84\x00\x00\x08",
  97. 5,
  98. &l_pchResult,
  99. &l_uResultLength
  100. );
  101. TestCheck(
  102. l_lResult == ERROR_INSUFFICIENT_BUFFER,
  103. "Transmit should fail due to too small buffer"
  104. );
  105. TestEnd();
  106. in_CReader.SetReplyBufferSize(2048);
  107. break;
  108. case 2:
  109. TestStart("3 byte APDU");
  110. l_lResult = in_CReader.Transmit(
  111. (PUCHAR) "\xBC\xC4\x00",
  112. 3,
  113. &l_pchResult,
  114. &l_uResultLength
  115. );
  116. TestCheck(
  117. l_lResult, "==", ERROR_INVALID_PARAMETER,
  118. 0, 0,
  119. 0, 0, 0, 0,
  120. NULL, NULL, NULL
  121. );
  122. TEST_END();
  123. break;
  124. case 3:
  125. // Get Challenge
  126. TestStart("GET CHALLENGE");
  127. l_lResult = in_CReader.Transmit(
  128. (PUCHAR) "\xBC\xC4\x00\x00\x08",
  129. 5,
  130. &l_pchResult,
  131. &l_uResultLength
  132. );
  133. TestCheck(
  134. l_lResult, "==", ERROR_SUCCESS,
  135. l_uResultLength, 10,
  136. l_pchResult[8], l_pchResult[9], 0x90, 0x00,
  137. NULL, NULL, NULL
  138. );
  139. TEST_END();
  140. //
  141. // Submit Alternate Identification Code (AID)
  142. //
  143. TestStart("VERIFY PIN");
  144. l_lResult = in_CReader.Transmit(
  145. (PUCHAR) "\xBC\x38\x00\x00\x0A\x01\x02\x03\x04\x05\x06\x07\x08\x09\0x0A",
  146. 15,
  147. &l_pchResult,
  148. &l_uResultLength
  149. );
  150. TestCheck(
  151. l_lResult, "==", ERROR_SUCCESS,
  152. l_uResultLength, 2,
  153. l_pchResult[0], l_pchResult[1], 0x90, 0x08,
  154. NULL, NULL, NULL
  155. );
  156. TEST_END();
  157. break;
  158. case 4:
  159. // Translate of 4 byte APDU (Search for next blank word)
  160. TestStart("SEARCH BLANK WORD");
  161. l_lResult = in_CReader.Transmit(
  162. (PUCHAR) "\xBC\xA0\x00\x00", // Search for next blank word
  163. 4,
  164. &l_pchResult,
  165. &l_uResultLength
  166. );
  167. TestCheck(
  168. l_lResult, "==", ERROR_SUCCESS,
  169. l_uResultLength, 2,
  170. l_pchResult[0], l_pchResult[1], 0x90, 0x00,
  171. NULL, NULL, NULL
  172. );
  173. TEST_END();
  174. // Read result of Search for next blank word
  175. TestStart("GET RESPONSE");
  176. l_lResult = in_CReader.Transmit(
  177. (PUCHAR) "\xBC\xC0\x00\x00\x08", // Read Result command
  178. 5,
  179. &l_pchResult,
  180. &l_uResultLength
  181. );
  182. TestCheck(
  183. l_lResult, "==", ERROR_SUCCESS,
  184. l_uResultLength, 10,
  185. l_pchResult[8], l_pchResult[9], 0x90, 0x00,
  186. NULL, NULL, NULL
  187. );
  188. TEST_END();
  189. break;
  190. case 5:
  191. // Select Working File 2F01
  192. TestStart("Lc byte incorrect");
  193. l_lResult = in_CReader.Transmit(
  194. (PUCHAR) "\xBC\xA4\x00\x00\x02\x2F",
  195. 6,
  196. &l_pchResult,
  197. &l_uResultLength
  198. );
  199. TestCheck(
  200. l_lResult, "==", ERROR_INVALID_PARAMETER,
  201. 0, 0,
  202. 0, 0, 0, 0,
  203. NULL, NULL, NULL
  204. );
  205. TEST_END();
  206. break;
  207. case 6:
  208. //
  209. // Select Working File 2F01
  210. //
  211. TestStart("SELECT FILE");
  212. l_lResult = in_CReader.Transmit(
  213. (PUCHAR) "\xBC\xA4\x00\x00\x02\x2F\x01",
  214. 7,
  215. &l_pchResult,
  216. &l_uResultLength
  217. );
  218. TestCheck(
  219. l_lResult, "==", ERROR_SUCCESS,
  220. l_uResultLength, 2,
  221. l_pchResult[0], l_pchResult[1], 0x90, 0x00,
  222. NULL, NULL, NULL
  223. );
  224. TEST_END();
  225. // Erase memory with restart of work waiting time
  226. TestStart("ERASE BINARY");
  227. l_lResult = in_CReader.Transmit(
  228. (PUCHAR) "\xBC\x0E\x00\x00\x02\x00\x78",
  229. 7,
  230. &l_pchResult,
  231. &l_uResultLength
  232. );
  233. TestCheck(
  234. l_lResult, "==", ERROR_SUCCESS,
  235. l_uResultLength, 2,
  236. l_pchResult[0], l_pchResult[1], 0x90, 0x00,
  237. NULL, NULL, NULL
  238. );
  239. TEST_END();
  240. // Generate a 'test' pattern which will be written to the card
  241. for (l_uIndex = 0; l_uIndex < 256; l_uIndex++) {
  242. l_rgchBuffer[l_uIndex] = (UCHAR) l_uIndex;
  243. }
  244. // Tpdu for write binary. TB100L can write only 4 byte 4 byte
  245. memcpy(Buf_Tempo, "\xBC\xD0", 2); // writting order
  246. Buf_Tempo[4] = 0x4; //write 4 bytes in the card
  247. // This is the amount of bytes we write to the card
  248. l_uTest = 0;
  249. Adresse = 0;
  250. while (l_uTest < 256) {
  251. for(l_Tempo=5 ; l_Tempo < 9; l_Tempo++){
  252. Buf_Tempo[l_Tempo] = l_rgchBuffer[l_uTest++];
  253. }
  254. Buf_Tempo[2] = 00; // Writting address
  255. Buf_Tempo[3] = (UCHAR) Adresse++;
  256. //
  257. // Write
  258. //
  259. TestStart("WRITE BINARY - 4 bytes (%03d)",Adresse);
  260. //
  261. // Append number of bytes (note: the buffer contains the pattern already)
  262. //
  263. l_lResult = in_CReader.Transmit(
  264. Buf_Tempo,
  265. 9,
  266. &l_pchResult,
  267. &l_uResultLength
  268. );
  269. TestCheck(
  270. l_lResult, "==", ERROR_SUCCESS,
  271. l_uResultLength, 2,
  272. l_pchResult[0], l_pchResult[1], 0x90, 0x00,
  273. NULL, NULL, NULL
  274. );
  275. TEST_END();
  276. }
  277. // Read 256 bytes
  278. TestStart("READ BINARY - 256 bytes");
  279. l_lResult = in_CReader.Transmit(
  280. (PUCHAR) "\xBC\xB0\x00\x00\x00",
  281. 5,
  282. &l_pchResult,
  283. &l_uResultLength
  284. );
  285. TestCheck(
  286. l_lResult, "==", ERROR_SUCCESS,
  287. l_uResultLength, l_uNumBytes + 4,
  288. l_pchResult[256], l_pchResult[257], 0x90, 0x00,
  289. l_pchResult, l_rgchBuffer , l_uNumBytes + 2
  290. );
  291. TEST_END();
  292. break;
  293. case 7:
  294. //
  295. // Command with Slave Mode
  296. // Data bytes transferred subsequently (INS')
  297. //
  298. TestStart("GENERATE TEMP KEY");
  299. l_lResult = in_CReader.Transmit(
  300. (PUCHAR) "\xBC\x80\x00\x00\x02\x12\x00",
  301. 7,
  302. &l_pchResult,
  303. &l_uResultLength
  304. );
  305. TestCheck(
  306. l_lResult, "==", ERROR_SUCCESS,
  307. l_uResultLength, 2,
  308. l_pchResult[0], l_pchResult[1], 0x90, 0x08,
  309. NULL, NULL, NULL
  310. );
  311. TEST_END();
  312. break;
  313. case 8:
  314. // Select Master File 3F00
  315. TestStart("SELECT FILE");
  316. l_lResult = in_CReader.Transmit(
  317. (PUCHAR) "\xBC\xA4\x00\x00\x02\x3F\x00",
  318. 7,
  319. &l_pchResult,
  320. &l_uResultLength
  321. );
  322. TestCheck(
  323. l_lResult, "==", ERROR_SUCCESS,
  324. l_uResultLength, 2,
  325. l_pchResult[0], l_pchResult[1], 0x90, 0x00,
  326. NULL, NULL, NULL
  327. );
  328. TEST_END();
  329. // Erase memory on an invalid file => mute card
  330. TestStart("ERASE BINARY");
  331. l_lResult = in_CReader.Transmit(
  332. (PUCHAR) "\xBC\x0E\x00\x00\x02\x00\x78",
  333. 7,
  334. &l_pchResult,
  335. &l_uResultLength
  336. );
  337. TestCheck(
  338. l_lResult, "==", ERROR_SEM_TIMEOUT,
  339. NULL, NULL,
  340. NULL, NULL,
  341. NULL, NULL,
  342. NULL, NULL, NULL
  343. );
  344. TEST_END();
  345. return IFDSTATUS_END;
  346. default:
  347. return IFDSTATUS_FAILED;
  348. }
  349. return IFDSTATUS_SUCCESS;
  350. }
  351. static void
  352. MyCardEntry(
  353. class CCardProvider& in_CCardProvider
  354. )
  355. /*++
  356. Routine Description:
  357. This function registers all callbacks from the test suite
  358. Arguments:
  359. CCardProvider - ref. to card provider class
  360. Return Value:
  361. -
  362. --*/
  363. {
  364. // Set protocol callback
  365. in_CCardProvider.SetProtocol(MyCardSetProtocol);
  366. // Card test callback
  367. in_CCardProvider.SetCardTest(MyCardTest);
  368. // Name of our card
  369. in_CCardProvider.SetCardName("Bull");
  370. // Maximum number of tests
  371. in_CCardProvider.SetAtr((PBYTE) "\x3f\x67\x25\x00\x21\x20\x00\x0F\x68\x90\x00", 11);
  372. }