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.

374 lines
9.6 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. AMMITestCardEntry(
  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 AMMITestCard(AMMITestCardEntry);
  33. static ULONG
  34. AMMITestCardSetProtocol(
  35. class CCardProvider& in_CCardProvider,
  36. class CReader& in_CReader
  37. )
  38. /*++
  39. Routine Description:
  40. This function will be called after the card has been correctly
  41. identified. We should here set the protocol that we need
  42. for further transmissions
  43. Arguments:
  44. in_CCardProvider - ref. to our card provider object
  45. in_CReader - ref. to the reader object
  46. Return Value:
  47. IFDSTATUS_FAILED - we were unable to set the protocol correctly
  48. IFDSTATUS_SUCCESS - protocol set correctly
  49. --*/
  50. {
  51. ULONG l_lResult;
  52. TestStart("Try to set incorrect protocol T=1");
  53. l_lResult = in_CReader.SetProtocol(SCARD_PROTOCOL_T1);
  54. // The test MUST fail with the incorrect protocol
  55. TEST_CHECK_NOT_SUPPORTED("Set protocol failed", l_lResult);
  56. TestEnd();
  57. // Now set the correct protocol
  58. TestStart("Set protocol T=0");
  59. l_lResult = in_CReader.SetProtocol(SCARD_PROTOCOL_T0);
  60. TEST_CHECK_SUCCESS("Set protocol failed", l_lResult);
  61. TestEnd();
  62. if (l_lResult != ERROR_SUCCESS) {
  63. return IFDSTATUS_FAILED;
  64. }
  65. return IFDSTATUS_SUCCESS;
  66. }
  67. static
  68. ULONG
  69. AMMITestCardTest(
  70. class CCardProvider& in_CCardProvider,
  71. class CReader& in_CReader
  72. )
  73. /*++
  74. Routine Description:
  75. This serves as the test function for a particular smart card
  76. Arguments:
  77. in_CReader - ref. to class that provides all information for the test
  78. Return Value:
  79. IFDSTATUS value
  80. --*/
  81. {
  82. ULONG l_lResult, l_uResultLength, l_uIndex;
  83. PUCHAR l_pbResult;
  84. UCHAR l_rgbBuffer[512];
  85. // Generate a 'test' pattern which will be written to the card
  86. for (l_uIndex = 0; l_uIndex < 256; l_uIndex++) {
  87. l_rgbBuffer[l_uIndex + 5] = (UCHAR) l_uIndex;
  88. }
  89. switch (in_CCardProvider.GetTestNo()) {
  90. case 1: {
  91. // select a file
  92. TestStart("SELECT FILE EFptsDataCheck");
  93. l_lResult = in_CReader.Transmit(
  94. (PBYTE) "\x00\xa4\x00\x00\x02\x00\x10",
  95. 7,
  96. &l_pbResult,
  97. &l_uResultLength
  98. );
  99. TestCheck(
  100. l_lResult, "==", ERROR_SUCCESS,
  101. l_uResultLength, 2,
  102. l_pbResult[0], l_pbResult[1], 0x61, 0x15,
  103. NULL, NULL, NULL
  104. );
  105. TEST_END();
  106. // Test read of 256 bytes
  107. ULONG l_uNumBytes = 256;
  108. TestStart("READ BINARY %3d Byte(s)", l_uNumBytes);
  109. l_lResult = in_CReader.Transmit(
  110. (PBYTE) "\x00\xb0\x00\x00\x00",
  111. 5,
  112. &l_pbResult,
  113. &l_uResultLength
  114. );
  115. TestCheck(
  116. l_lResult, "==", ERROR_SUCCESS,
  117. l_uResultLength, l_uNumBytes + 2,
  118. l_pbResult[l_uNumBytes], l_pbResult[l_uNumBytes + 1], 0x90, 0x00,
  119. l_pbResult, l_rgbBuffer + 5, l_uNumBytes
  120. );
  121. TEST_END();
  122. break;
  123. }
  124. case 2: {
  125. // select a file
  126. TestStart("SELECT FILE EFptsDataCheck");
  127. l_lResult = in_CReader.Transmit(
  128. (PBYTE) "\x00\xa4\x00\x00\x02\x00\x10",
  129. 7,
  130. &l_pbResult,
  131. &l_uResultLength
  132. );
  133. TestCheck(
  134. l_lResult, "==", ERROR_SUCCESS,
  135. l_uResultLength, 2,
  136. l_pbResult[0], l_pbResult[1], 0x61, 0x15,
  137. NULL, NULL, NULL
  138. );
  139. TEST_END();
  140. // Test write of 255 bytes
  141. ULONG l_uNumBytes = 255;
  142. TestStart("WRITE BINARY %3d Byte(s)", l_uNumBytes);
  143. // set the number of bytes we want to write to the card
  144. memcpy(l_rgbBuffer, "\x00\xd6\x00\x00", 4);
  145. l_rgbBuffer[4] = (BYTE) l_uNumBytes;
  146. l_lResult = in_CReader.Transmit(
  147. l_rgbBuffer,
  148. l_uNumBytes + 5,
  149. &l_pbResult,
  150. &l_uResultLength
  151. );
  152. TestCheck(
  153. l_lResult, "==", ERROR_SUCCESS,
  154. l_uResultLength, 2,
  155. l_pbResult[0], l_pbResult[1], 0x90, 0x00,
  156. NULL, NULL, 0
  157. );
  158. TEST_END();
  159. break;
  160. }
  161. case 3: {
  162. //
  163. // Read the result file from the smart card.
  164. // The card stores results of each test in
  165. // a special file
  166. //
  167. TestStart("SELECT FILE EFresult");
  168. l_lResult = in_CReader.Transmit(
  169. (PUCHAR) "\x00\xa4\x00\x00\x02\xa0\x00",
  170. 7,
  171. &l_pbResult,
  172. &l_uResultLength
  173. );
  174. TestCheck(
  175. l_lResult, "==", ERROR_SUCCESS,
  176. l_uResultLength, 2,
  177. l_pbResult[0], l_pbResult[1], 0x61, 0x15,
  178. NULL, NULL, NULL
  179. );
  180. TEST_END();
  181. // Read
  182. TestStart("READ BINARY FILE EFresult");
  183. // apdu for read binary
  184. memcpy(l_rgbBuffer, "\x00\xb0\x00\x00", 4);
  185. // Append number of bytes we want to read
  186. l_rgbBuffer[4] = (BYTE) sizeof(T0_RESULT_FILE_HEADER);
  187. // read in the header of the result file
  188. l_lResult = in_CReader.Transmit(
  189. l_rgbBuffer,
  190. 5,
  191. &l_pbResult,
  192. &l_uResultLength
  193. );
  194. TestCheck(
  195. l_lResult, "==", ERROR_SUCCESS,
  196. l_uResultLength, sizeof(T0_RESULT_FILE_HEADER) + 2,
  197. l_pbResult[sizeof(T0_RESULT_FILE_HEADER)],
  198. l_pbResult[sizeof(T0_RESULT_FILE_HEADER) + 1],
  199. 0x90, 0x00,
  200. NULL, NULL, NULL
  201. );
  202. // get the card reset count
  203. PT0_RESULT_FILE_HEADER l_pCResultFileHeader;
  204. l_pCResultFileHeader = (PT0_RESULT_FILE_HEADER) l_pbResult;
  205. BYTE l_bCardResetCount = l_pCResultFileHeader->CardResetCount;
  206. // set the offset from where we want to read
  207. l_rgbBuffer[3] = (BYTE) l_pCResultFileHeader->Offset;
  208. // Append number of bytes
  209. l_rgbBuffer[4] = (BYTE) sizeof(T0_RESULT_FILE);
  210. // read in the result data of the result file
  211. l_lResult = in_CReader.Transmit(
  212. l_rgbBuffer,
  213. 5,
  214. &l_pbResult,
  215. &l_uResultLength
  216. );
  217. TestCheck(
  218. l_lResult, "==", ERROR_SUCCESS,
  219. l_uResultLength, sizeof(T0_RESULT_FILE) + 2,
  220. l_pbResult[sizeof(T0_RESULT_FILE)],
  221. l_pbResult[sizeof(T0_RESULT_FILE) + 1],
  222. 0x90, 0x00,
  223. NULL, NULL, NULL
  224. );
  225. TEST_END();
  226. // Now check the result file.
  227. PT0_RESULT_FILE l_pCResultFile = (PT0_RESULT_FILE) l_pbResult;
  228. // check if the card received a proper PTS
  229. TestStart("'PTS'");
  230. TestCheck(
  231. l_pCResultFile->PTS.ResetCount == l_bCardResetCount,
  232. "Test not performed"
  233. );
  234. TestCheck(
  235. (l_pCResultFile->PTS.Result & 0x01) != 1,
  236. "Smart card received not PTS1"
  237. );
  238. TEST_END();
  239. TestStart("'PTS data check'");
  240. TestCheck(
  241. l_pCResultFile->PTSDataCheck.ResetCount == l_bCardResetCount,
  242. "Test not performed"
  243. );
  244. TestCheck(
  245. (l_pCResultFile->PTSDataCheck.Result) == 0,
  246. "Smart card received incorrect data"
  247. );
  248. TEST_END();
  249. return IFDSTATUS_END;
  250. }
  251. default:
  252. return IFDSTATUS_FAILED;
  253. }
  254. return IFDSTATUS_SUCCESS;
  255. }
  256. static void
  257. AMMITestCardEntry(
  258. class CCardProvider& in_CCardProvider
  259. )
  260. /*++
  261. Routine Description:
  262. This function registers all callbacks from the test suite
  263. Arguments:
  264. CCardProvider - ref. to card provider class
  265. Return Value:
  266. -
  267. --*/
  268. {
  269. // Set protocol callback
  270. in_CCardProvider.SetProtocol(AMMITestCardSetProtocol);
  271. // Card test callback
  272. in_CCardProvider.SetCardTest(AMMITestCardTest);
  273. // Name of our card
  274. in_CCardProvider.SetCardName("AMMI");
  275. // Name of our card
  276. in_CCardProvider.SetAtr(
  277. (PBYTE) "\x3b\x7e\x13\x00\x00\x80\x53\xff\xff\xff\x62\x00\xff\x71\xbf\x83\x03\x90\x00",
  278. 19
  279. );
  280. }