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.

318 lines
8.3 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. #define BYTES_PER_BLOCK 64
  25. void
  26. GDTestCardEntry(
  27. class CCardProvider& in_CCardProvider
  28. );
  29. //
  30. // Create a card provider object
  31. // Note: all global varibales and all functions have to be static
  32. //
  33. static class CCardProvider GDTestCard(GDTestCardEntry);
  34. static ULONG
  35. GDTestCardSetProtocol(
  36. class CCardProvider& in_CCardProvider,
  37. class CReader& in_CReader
  38. )
  39. /*++
  40. Routine Description:
  41. This function will be called after the card has been correctly
  42. identified. We should here set the protocol that we need
  43. for further transmissions
  44. Arguments:
  45. in_CCardProvider - ref. to our card provider object
  46. in_CReader - ref. to the reader object
  47. Return Value:
  48. IFDSTATUS_FAILED - we were unable to set the protocol correctly
  49. IFDSTATUS_SUCCESS - protocol set correctly
  50. --*/
  51. {
  52. ULONG l_lResult;
  53. TestStart("Set protocol to T=0 | T=1");
  54. l_lResult = in_CReader.SetProtocol(SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1);
  55. TEST_CHECK_SUCCESS("Set protocol failed", l_lResult);
  56. TestEnd();
  57. if (l_lResult != ERROR_SUCCESS) {
  58. return IFDSTATUS_FAILED;
  59. }
  60. return IFDSTATUS_SUCCESS;
  61. }
  62. static
  63. ULONG
  64. GDTestCardTest(
  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_lResult, l_uResultLength, l_uBlock, l_uIndex;
  78. PUCHAR l_pchResult;
  79. UCHAR l_rgchBuffer[512];
  80. switch (in_CCardProvider.GetTestNo()) {
  81. case 1: {
  82. ULONG l_uNumBytes = 256;
  83. // write some data to the test file using T=0
  84. TestStart("Cold reset");
  85. l_lResult = in_CReader.ColdResetCard();
  86. TEST_CHECK_SUCCESS("Set protocol failed", l_lResult);
  87. TestEnd();
  88. ULONG l_uState;
  89. TestStart("Check reader state");
  90. l_lResult = in_CReader.GetState(&l_uState);
  91. TEST_CHECK_SUCCESS(
  92. "Ioctl IOCTL_SMARTCARD_GET_STATE failed",
  93. l_lResult
  94. );
  95. TestCheck(
  96. l_uState == SCARD_NEGOTIABLE,
  97. "Invalid reader state.\nReturned %d\nExpected %d",
  98. l_uState,
  99. SCARD_NEGOTIABLE
  100. );
  101. TestEnd();
  102. TestStart("Set protocol T=0");
  103. l_lResult = in_CReader.SetProtocol(SCARD_PROTOCOL_T0);
  104. TEST_CHECK_SUCCESS("Set protocol failed", l_lResult);
  105. TestEnd();
  106. // select a file
  107. TestStart("SELECT FILE EFptsDataCheck");
  108. l_lResult = in_CReader.Transmit(
  109. (PUCHAR) "\x00\xa4\x00\x00\x02\x00\x01",
  110. 7,
  111. &l_pchResult,
  112. &l_uResultLength
  113. );
  114. TestCheck(
  115. l_lResult, "==", ERROR_SUCCESS,
  116. l_uResultLength, 2,
  117. l_pchResult[0], l_pchResult[1], 0x61, 0x09,
  118. NULL, NULL, 0
  119. );
  120. TEST_END();
  121. TestStart("WRITE BINARY %3d bytes", l_uNumBytes);
  122. for (l_uBlock = 0; l_uBlock < l_uNumBytes; l_uBlock += BYTES_PER_BLOCK) {
  123. // apdu for write binary
  124. memcpy(l_rgchBuffer, "\x00\xd6\x00", 3);
  125. // offset within the file we want to write to
  126. l_rgchBuffer[3] = (UCHAR) l_uBlock;
  127. // Append number of bytes
  128. l_rgchBuffer[4] = (UCHAR) BYTES_PER_BLOCK;
  129. // append pattern to buffer;
  130. for (l_uIndex = 0; l_uIndex < BYTES_PER_BLOCK; l_uIndex++) {
  131. l_rgchBuffer[5 + l_uIndex] = (UCHAR) (l_uBlock + l_uIndex);
  132. }
  133. l_lResult = in_CReader.Transmit(
  134. l_rgchBuffer,
  135. 5 + BYTES_PER_BLOCK,
  136. &l_pchResult,
  137. &l_uResultLength
  138. );
  139. TestCheck(
  140. l_lResult, "==", ERROR_SUCCESS,
  141. l_uResultLength, 2,
  142. l_pchResult[0], l_pchResult[1], 0x90, 0x00,
  143. NULL, NULL, NULL
  144. );
  145. }
  146. TEST_END();
  147. //
  148. // read the data back using T=1
  149. //
  150. TestStart("Cold reset");
  151. l_lResult = in_CReader.ColdResetCard();
  152. TEST_CHECK_SUCCESS("Cold reset failed", l_lResult);
  153. TestEnd();
  154. TestStart("Set protocol T=1");
  155. l_lResult = in_CReader.SetProtocol(SCARD_PROTOCOL_T1);
  156. TEST_CHECK_SUCCESS("Set protocol failed", l_lResult);
  157. TestEnd();
  158. // select a file
  159. TestStart("SELECT FILE EFptsDataCheck");
  160. l_lResult = in_CReader.Transmit(
  161. (PUCHAR) "\x00\xa4\x00\x00\x02\x00\x01",
  162. 7,
  163. &l_pchResult,
  164. &l_uResultLength
  165. );
  166. TestCheck(
  167. l_lResult, "==", ERROR_SUCCESS,
  168. l_uResultLength, 11,
  169. l_pchResult[9], l_pchResult[10], 0x90, 0x00,
  170. NULL, NULL, NULL
  171. );
  172. TEST_END();
  173. TestStart("READ BINARY %3d Byte(s)", l_uNumBytes);
  174. for (l_uBlock = 0; l_uBlock < l_uNumBytes; l_uBlock += BYTES_PER_BLOCK) {
  175. // apdu for read binary
  176. memcpy(l_rgchBuffer, "\x00\xb0\x00", 3);
  177. // offset within the file we want to read from
  178. l_rgchBuffer[3] = (UCHAR) l_uBlock;
  179. // Append number of bytes (note: the buffer contains the pattern already)
  180. l_rgchBuffer[4] = (UCHAR) BYTES_PER_BLOCK;
  181. l_lResult = in_CReader.Transmit(
  182. l_rgchBuffer,
  183. 5,
  184. &l_pchResult,
  185. &l_uResultLength
  186. );
  187. // append pattern to buffer;
  188. for (l_uIndex = 0; l_uIndex < BYTES_PER_BLOCK; l_uIndex++) {
  189. l_rgchBuffer[l_uIndex] = (UCHAR) (l_uBlock + l_uIndex);
  190. }
  191. TestCheck(
  192. l_lResult, "==", ERROR_SUCCESS,
  193. l_uResultLength, (l_uNumBytes / 4) + 2,
  194. l_pchResult[BYTES_PER_BLOCK], l_pchResult[BYTES_PER_BLOCK + 1], 0x90, 0x00,
  195. l_pchResult, l_rgchBuffer, BYTES_PER_BLOCK
  196. );
  197. }
  198. TEST_END();
  199. return IFDSTATUS_END;
  200. }
  201. default:
  202. return IFDSTATUS_FAILED;
  203. }
  204. return IFDSTATUS_SUCCESS;
  205. }
  206. static void
  207. GDTestCardEntry(
  208. class CCardProvider& in_CCardProvider
  209. )
  210. /*++
  211. Routine Description:
  212. This function registers all callbacks from the test suite
  213. Arguments:
  214. CCardProvider - ref. to card provider class
  215. Return Value:
  216. -
  217. --*/
  218. {
  219. // Set protocol callback
  220. in_CCardProvider.SetProtocol(GDTestCardSetProtocol);
  221. // Card test callback
  222. in_CCardProvider.SetCardTest(GDTestCardTest);
  223. // Name of our card
  224. in_CCardProvider.SetCardName("G & D");
  225. // ATR of our card
  226. in_CCardProvider.SetAtr((PBYTE) "\x3B\xBF\x18\x00\xC0\x20\x31\x70\x52\x53\x54\x41\x52\x43\x4F\x53\x20\x53\x32\x31\x20\x43\x90\x00\x9C", 25);
  227. in_CCardProvider.SetAtr((PBYTE) "\x3b\xbf\x18\x00\x80\x31\x70\x35\x53\x54\x41\x52\x43\x4f\x53\x20\x53\x32\x31\x20\x43\x90\x00\x9b", 24);
  228. }