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.

442 lines
16 KiB

  1. /*++
  2. Copyright (c) 1998-2000 Microsoft Corporation
  3. Module Name:
  4. w32drive
  5. Abstract:
  6. This module defines a child of the client-side RDP
  7. device redirection, the "w32scard" W32SCard to provide
  8. smart card redirection on 32bit windows
  9. Author:
  10. Reid Kuhn 7/25/00
  11. Revision History:
  12. --*/
  13. #ifndef __W32SCARD_H__
  14. #define __W32SCARD_H__
  15. #include <rdpdr.h>
  16. #include <w32scard.h>
  17. #include "drobject.h"
  18. #include "drdevasc.h"
  19. #include "scredir.h"
  20. #ifdef OS_WINCE
  21. #include <winscard.h>
  22. #endif
  23. #ifdef OS_WINCE
  24. typedef WAITORTIMERCALLBACKFUNC WAITORTIMERCALLBACK;
  25. #endif
  26. #ifndef OS_WINCE
  27. typedef LONG (WINAPI * PFN_SCardFreeMemory)(SCARDCONTEXT, LPCVOID);
  28. typedef LONG (WINAPI * PFN_SCardState)(SCARDHANDLE, LPDWORD, LPDWORD, LPBYTE, LPDWORD);
  29. typedef BOOL (WINAPI * PFN_RegisterWaitForSingleObject)(PHANDLE, HANDLE, WAITORTIMERCALLBACK, PVOID, ULONG, ULONG);
  30. typedef BOOL (WINAPI * PFN_UnregisterWaitEx)(HANDLE, HANDLE);
  31. #endif
  32. typedef LONG (WINAPI * PFN_SCardEstablishContext)(DWORD, LPCVOID, LPCVOID, LPSCARDCONTEXT);
  33. typedef LONG (WINAPI * PFN_SCardReleaseContext)(SCARDCONTEXT);
  34. typedef LONG (WINAPI * PFN_SCardIsValidContext)(SCARDCONTEXT);
  35. typedef LONG (WINAPI * PFN_SCardListReaderGroupsA)(SCARDCONTEXT, LPSTR, LPDWORD);
  36. typedef LONG (WINAPI * PFN_SCardListReaderGroupsW)(SCARDCONTEXT, LPWSTR, LPDWORD);
  37. typedef LONG (WINAPI * PFN_SCardListReadersA)(SCARDCONTEXT, LPCSTR, LPSTR, LPDWORD);
  38. typedef LONG (WINAPI * PFN_SCardListReadersW)(SCARDCONTEXT, LPCWSTR, LPWSTR, LPDWORD);
  39. typedef LONG (WINAPI * PFN_SCardIntroduceReaderGroupA)(SCARDCONTEXT, LPCSTR);
  40. typedef LONG (WINAPI * PFN_SCardIntroduceReaderGroupW)(SCARDCONTEXT, LPCWSTR);
  41. typedef LONG (WINAPI * PFN_SCardForgetReaderGroupA)(SCARDCONTEXT, LPCSTR);
  42. typedef LONG (WINAPI * PFN_SCardForgetReaderGroupW)(SCARDCONTEXT, LPCWSTR);
  43. typedef LONG (WINAPI * PFN_SCardIntroduceReaderA)(SCARDCONTEXT, LPCSTR, LPCSTR);
  44. typedef LONG (WINAPI * PFN_SCardIntroduceReaderW)(SCARDCONTEXT, LPCWSTR, LPCWSTR);
  45. typedef LONG (WINAPI * PFN_SCardForgetReaderA)(SCARDCONTEXT, LPCSTR);
  46. typedef LONG (WINAPI * PFN_SCardForgetReaderW)(SCARDCONTEXT, LPCWSTR);
  47. typedef LONG (WINAPI * PFN_SCardAddReaderToGroupA)(SCARDCONTEXT, LPCSTR, LPCSTR);
  48. typedef LONG (WINAPI * PFN_SCardAddReaderToGroupW)(SCARDCONTEXT, LPCWSTR, LPCWSTR);
  49. typedef LONG (WINAPI * PFN_SCardRemoveReaderFromGroupA)(SCARDCONTEXT, LPCSTR, LPCSTR);
  50. typedef LONG (WINAPI * PFN_SCardRemoveReaderFromGroupW)(SCARDCONTEXT, LPCWSTR, LPCWSTR);
  51. typedef LONG (WINAPI * PFN_SCardFreeMemory)(SCARDCONTEXT, LPCVOID);
  52. typedef LONG (WINAPI * PFN_SCardLocateCardsA)(SCARDCONTEXT, LPCSTR, LPSCARD_READERSTATE_A, DWORD);
  53. typedef LONG (WINAPI * PFN_SCardLocateCardsW)(SCARDCONTEXT, LPCWSTR, LPSCARD_READERSTATE_W, DWORD);
  54. typedef LONG (WINAPI * PFN_SCardLocateCardsByATRA)(SCARDCONTEXT, LPSCARD_ATRMASK, DWORD, LPSCARD_READERSTATE_A, DWORD);
  55. typedef LONG (WINAPI * PFN_SCardLocateCardsByATRW)(SCARDCONTEXT, LPSCARD_ATRMASK, DWORD, LPSCARD_READERSTATE_W, DWORD);
  56. typedef LONG (WINAPI * PFN_SCardGetStatusChangeA)(SCARDCONTEXT, DWORD, LPSCARD_READERSTATE_A, DWORD);
  57. typedef LONG (WINAPI * PFN_SCardGetStatusChangeW)(SCARDCONTEXT, DWORD, LPSCARD_READERSTATE_W, DWORD);
  58. typedef LONG (WINAPI * PFN_SCardCancel)(SCARDCONTEXT);
  59. typedef LONG (WINAPI * PFN_SCardConnectA)(SCARDCONTEXT, LPCSTR, DWORD, DWORD, LPSCARDHANDLE, LPDWORD);
  60. typedef LONG (WINAPI * PFN_SCardConnectW)(SCARDCONTEXT, LPCWSTR, DWORD, DWORD, LPSCARDHANDLE, LPDWORD);
  61. typedef LONG (WINAPI * PFN_SCardReconnect)(SCARDHANDLE, DWORD, DWORD, DWORD, LPDWORD);
  62. typedef LONG (WINAPI * PFN_SCardDisconnect)(SCARDHANDLE, DWORD);
  63. typedef LONG (WINAPI * PFN_SCardBeginTransaction)(SCARDHANDLE);
  64. typedef LONG (WINAPI * PFN_SCardEndTransaction)(SCARDHANDLE, DWORD);
  65. typedef LONG (WINAPI * PFN_SCardStatusA)(SCARDHANDLE, LPSTR, LPDWORD, LPDWORD, LPDWORD, LPBYTE, LPDWORD);
  66. typedef LONG (WINAPI * PFN_SCardStatusW)(SCARDHANDLE, LPWSTR, LPDWORD, LPDWORD, LPDWORD, LPBYTE, LPDWORD);
  67. typedef LONG (WINAPI * PFN_SCardTransmit)(SCARDHANDLE, LPCSCARD_IO_REQUEST, LPCBYTE, DWORD, LPSCARD_IO_REQUEST, LPBYTE, LPDWORD);
  68. typedef LONG (WINAPI * PFN_SCardControl)(SCARDHANDLE, DWORD,LPCVOID, DWORD, LPVOID, DWORD, LPDWORD);
  69. typedef LONG (WINAPI * PFN_SCardGetAttrib)(SCARDHANDLE, DWORD, LPBYTE, LPDWORD);
  70. typedef LONG (WINAPI * PFN_SCardSetAttrib)(SCARDHANDLE, DWORD, LPCBYTE, DWORD);
  71. ///////////////////////////////////////////////////////////////
  72. //
  73. // Defines and Macros
  74. //
  75. // The SCard device name, and path
  76. #define SZ_SCARD_DEVICE_NAME (TEXT(DR_SMARTCARD_SUBSYSTEM))
  77. class W32SCard;
  78. typedef struct _SCARDHANDLECALLSTRUCT
  79. {
  80. W32SCard *pTHIS;
  81. DWORD dwCallType;
  82. PRDPDR_IOREQUEST_PACKET pIoRequestPacket;
  83. HMODULE hModExtraRefCount;
  84. HANDLE hThread;
  85. } SCARDHANDLECALLSTRUCT;
  86. //class DrFile;
  87. //////////////////////////////////////////////////////////////
  88. //
  89. // W32SCard Class Declaration
  90. //
  91. //
  92. class W32SCard : public W32DrDeviceAsync
  93. {
  94. private:
  95. DRSTRING _deviceName;
  96. DrFile *_pFileObj;
  97. SCARDCONTEXT *_rgSCardContextList;
  98. DWORD _dwSCardContextListSize;
  99. HANDLE *_rghThreadList;
  100. DWORD _dwThreadListSize;
  101. CRITICAL_SECTION _csContextList;
  102. CRITICAL_SECTION _csThreadList;
  103. CRITICAL_SECTION _csWaitForStartedEvent;
  104. BOOL _fInDestructor;
  105. BOOL _fFlushing;
  106. HMODULE _hModWinscard;
  107. BOOL _fCritSecsInitialized;
  108. #ifndef OS_WINCE
  109. BOOL _fCloseStartedEvent;
  110. HMODULE _hModKernel32;
  111. HANDLE _hStartedEvent;
  112. HANDLE _hRegisterWaitForStartedEvent;
  113. #endif
  114. PRDPDR_IOREQUEST_PACKET *_rgIORequestList;
  115. DWORD _dwIORequestListSize;
  116. protected:
  117. BOOL _fNewFailed;
  118. HMODULE AddRefCurrentModule();
  119. BOOL AddSCardContextToList(SCARDCONTEXT SCardContext);
  120. void RemoveSCardContextFromList(SCARDCONTEXT SCardContext);
  121. HANDLE GetCurrentThreadHandle(void);
  122. BOOL AddThreadToList(HANDLE hThread);
  123. void RemoveThreadFromList(HANDLE hThread);
  124. BOOL AddIORequestToList(PRDPDR_IOREQUEST_PACKET pIORequestPacket);
  125. BOOL BindToSCardFunctions();
  126. //
  127. // Setup device property
  128. //
  129. virtual VOID SetDeviceProperty() { _deviceProperty.SetSeekProperty(FALSE); }
  130. virtual VOID FlushIRPs();
  131. VOID DefaultIORequestMsgHandleWrapper(
  132. IN PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  133. IN NTSTATUS serverReturnStatus
  134. );
  135. //
  136. // Async IO Management Functions
  137. //
  138. virtual HANDLE StartFSFunc(W32DRDEV_ASYNCIO_PARAMS *params,
  139. DWORD *status);
  140. static HANDLE _StartFSFunc(W32DRDEV_ASYNCIO_PARAMS *params,
  141. DWORD *status);
  142. virtual DWORD AsyncDirCtrlFunc(W32DRDEV_ASYNCIO_PARAMS *params);
  143. static DWORD _AsyncDirCtrlFunc(W32DRDEV_ASYNCIO_PARAMS *params);
  144. virtual DWORD AsyncNotifyChangeDir(W32DRDEV_ASYNCIO_PARAMS *params);
  145. LONG AllocateAndCopyATRMasksForCall(
  146. DWORD cAtrs,
  147. LocateCards_ATRMask *rgATRMasksFromDecode,
  148. SCARD_ATRMASK **prgATRMasksForCall);
  149. void LocateCardsByATRA(
  150. PRDPDR_IOREQUEST_PACKET pIoRequestPacket);
  151. void LocateCardsByATRW(
  152. PRDPDR_IOREQUEST_PACKET pIoRequestPacket);
  153. #ifndef OS_WINCE
  154. void State(
  155. SCARDHANDLECALLSTRUCT *pSCardHandleCall);
  156. #endif
  157. void AllocateAndChannelWriteReplyPacket(
  158. PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  159. char *pEncodedBuffer,
  160. unsigned long cbEncodedBuffer);
  161. LONG DecodeContextCall(
  162. PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  163. SCARDCONTEXT *pSCardContext);
  164. LONG DecodeContextAndStringCallA(
  165. PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  166. SCARDCONTEXT *pSCardContext,
  167. LPSTR *ppsz);
  168. LONG DecodeContextAndStringCallW(
  169. PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  170. SCARDCONTEXT *pSCardContext,
  171. LPWSTR *ppwsz);
  172. void EncodeAndChannelWriteLongReturn(
  173. PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  174. LONG lReturn);
  175. void HandleContextCallWithLongReturn(
  176. PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  177. DWORD dwCallType);
  178. void EncodeAndChannelWriteLongAndMultiStringReturn(
  179. PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  180. LONG lReturn,
  181. BYTE *pb,
  182. DWORD cch,
  183. BOOL fUnicode);
  184. void EstablishContext(
  185. PRDPDR_IOREQUEST_PACKET pIoRequestPacket);
  186. void ListReaderGroups(
  187. PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  188. DWORD dwCallType);
  189. void ListReaders(
  190. PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  191. DWORD dwCallType);
  192. void IntroduceReaderGroup(
  193. PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  194. DWORD dwCallType);
  195. void HandleContextAndStringCallWithLongReturn(
  196. PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  197. DWORD dwCallType);
  198. void HandleContextAndTwoStringCallWithLongReturn(
  199. PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  200. DWORD dwCallType);
  201. LONG AllocateAndCopyReaderStateStructsForCallA(
  202. DWORD cReaders,
  203. ReaderStateA *rgReaderStatesFromDecode,
  204. LPSCARD_READERSTATE_A *prgReadersStatesForSCardCall);
  205. LONG AllocateAndCopyReaderStateStructsForCallW(
  206. DWORD cReaders,
  207. ReaderStateW *rgReaderStatesFromDecode,
  208. LPSCARD_READERSTATE_W *prgReadersStatesForSCardCall);
  209. LONG AllocateAndCopyReaderStateStructsForReturnA(
  210. DWORD cReaders,
  211. LPSCARD_READERSTATE_A rgReaderStatesFromSCardCall,
  212. ReaderState_Return **prgReaderStatesForReturn);
  213. LONG AllocateAndCopyReaderStateStructsForReturnW(
  214. DWORD cReaders,
  215. LPSCARD_READERSTATE_W rgReaderStatesFromSCardCall,
  216. ReaderState_Return **prgReaderStatesForReturn);
  217. void LocateCardsA(
  218. PRDPDR_IOREQUEST_PACKET pIoRequestPacket);
  219. void LocateCardsW(
  220. PRDPDR_IOREQUEST_PACKET pIoRequestPacket);
  221. static DWORD WINAPI GetStatusChangeThreadProc(
  222. LPVOID lpParameter);
  223. void GetStatusChangeWrapper(
  224. PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  225. DWORD dwCallType);
  226. void GetStatusChangeA(
  227. PRDPDR_IOREQUEST_PACKET pIoRequestPacket);
  228. void GetStatusChangeW(
  229. PRDPDR_IOREQUEST_PACKET pIoRequestPacket);
  230. void Connect(
  231. SCARDHANDLECALLSTRUCT *pSCardHandleCall,
  232. DWORD dwCallType);
  233. void Reconnect(
  234. SCARDHANDLECALLSTRUCT *pSCardHandleCall);
  235. void HandleHCardAndDispositionCall(
  236. SCARDHANDLECALLSTRUCT *pSCardHandleCall,
  237. DWORD dwCallType);
  238. void Status(
  239. SCARDHANDLECALLSTRUCT *pSCardHandleCall,
  240. DWORD dwCallType);
  241. void Transmit(
  242. SCARDHANDLECALLSTRUCT *pSCardHandleCall);
  243. void Control(
  244. SCARDHANDLECALLSTRUCT *pSCardHandleCall);
  245. void GetAttrib(
  246. SCARDHANDLECALLSTRUCT *pSCardHandleCall);
  247. void SetAttrib(
  248. SCARDHANDLECALLSTRUCT *pSCardHandleCall);
  249. static DWORD WINAPI SCardHandleCall_ThreadProc(
  250. LPVOID lpParameter);
  251. #ifndef OS_WINCE
  252. static DWORD WINAPI WaitForStartedEventThreadProc(
  253. LPVOID lpParameter);
  254. void GetStartedEvent();
  255. void ReleaseStartedEvent();
  256. #endif
  257. void AccessStartedEvent(
  258. PRDPDR_IOREQUEST_PACKET pIoRequestPacket);
  259. public:
  260. //
  261. // Constructor
  262. //
  263. W32SCard(
  264. ProcObj *processObject,
  265. ULONG deviceID,
  266. const TCHAR *deviceName,
  267. const TCHAR *devicePath);
  268. virtual ~W32SCard();
  269. void WaitForStartedEvent(BOOLEAN TimerOrWaitFired);
  270. //
  271. // Add a device announce packet for this device to the input
  272. // buffer.
  273. //
  274. virtual ULONG GetDevAnnounceDataSize();
  275. virtual VOID GetDevAnnounceData(IN PRDPDR_DEVICE_ANNOUNCE buf);
  276. static DWORD Enumerate(ProcObj *procObj, DrDeviceMgr *deviceMgr);
  277. virtual DRSTRING GetName()
  278. {
  279. return _deviceName;
  280. };
  281. // Get the device type. See "Device Types" section of rdpdr.h
  282. virtual ULONG GetDeviceType() { return RDPDR_DTYP_SMARTCARD; }
  283. //
  284. // Return the class name.
  285. //
  286. virtual DRSTRING ClassName() { return TEXT("W32SCard"); }
  287. virtual VOID MsgIrpDeviceControl(
  288. IN PRDPDR_IOREQUEST_PACKET pIoRequestPacket,
  289. IN UINT32 packetLen
  290. );
  291. private:
  292. //
  293. // Dynamically bound SCard function pointers
  294. //
  295. #ifndef OS_WINCE
  296. PFN_SCardFreeMemory pfnSCardFreeMemory;
  297. PFN_SCardState pfnSCardState;
  298. PFN_RegisterWaitForSingleObject pfnRegisterWaitForSingleObject;
  299. PFN_UnregisterWaitEx pfnUnregisterWaitEx;
  300. #endif
  301. PFN_SCardEstablishContext pfnSCardEstablishContext;
  302. PFN_SCardReleaseContext pfnSCardReleaseContext;
  303. PFN_SCardIsValidContext pfnSCardIsValidContext;
  304. PFN_SCardListReaderGroupsA pfnSCardListReaderGroupsA;
  305. PFN_SCardListReaderGroupsW pfnSCardListReaderGroupsW;
  306. PFN_SCardListReadersA pfnSCardListReadersA;
  307. PFN_SCardListReadersW pfnSCardListReadersW;
  308. PFN_SCardIntroduceReaderGroupA pfnSCardIntroduceReaderGroupA;
  309. PFN_SCardIntroduceReaderGroupW pfnSCardIntroduceReaderGroupW;
  310. PFN_SCardForgetReaderGroupA pfnSCardForgetReaderGroupA;
  311. PFN_SCardForgetReaderGroupW pfnSCardForgetReaderGroupW;
  312. PFN_SCardIntroduceReaderA pfnSCardIntroduceReaderA;
  313. PFN_SCardIntroduceReaderW pfnSCardIntroduceReaderW;
  314. PFN_SCardForgetReaderA pfnSCardForgetReaderA;
  315. PFN_SCardForgetReaderW pfnSCardForgetReaderW;
  316. PFN_SCardAddReaderToGroupA pfnSCardAddReaderToGroupA;
  317. PFN_SCardAddReaderToGroupW pfnSCardAddReaderToGroupW;
  318. PFN_SCardRemoveReaderFromGroupA pfnSCardRemoveReaderFromGroupA;
  319. PFN_SCardRemoveReaderFromGroupW pfnSCardRemoveReaderFromGroupW;
  320. PFN_SCardLocateCardsA pfnSCardLocateCardsA;
  321. PFN_SCardLocateCardsW pfnSCardLocateCardsW;
  322. PFN_SCardLocateCardsByATRA pfnSCardLocateCardsByATRA;
  323. PFN_SCardLocateCardsByATRW pfnSCardLocateCardsByATRW;
  324. PFN_SCardGetStatusChangeA pfnSCardGetStatusChangeA;
  325. PFN_SCardGetStatusChangeW pfnSCardGetStatusChangeW;
  326. PFN_SCardCancel pfnSCardCancel;
  327. PFN_SCardConnectA pfnSCardConnectA;
  328. PFN_SCardConnectW pfnSCardConnectW;
  329. PFN_SCardReconnect pfnSCardReconnect;
  330. PFN_SCardDisconnect pfnSCardDisconnect;
  331. PFN_SCardBeginTransaction pfnSCardBeginTransaction;
  332. PFN_SCardEndTransaction pfnSCardEndTransaction;
  333. PFN_SCardStatusA pfnSCardStatusA;
  334. PFN_SCardStatusW pfnSCardStatusW;
  335. PFN_SCardTransmit pfnSCardTransmit;
  336. PFN_SCardControl pfnSCardControl;
  337. PFN_SCardGetAttrib pfnSCardGetAttrib;
  338. PFN_SCardSetAttrib pfnSCardSetAttrib;
  339. #ifndef OS_WINCE
  340. BOOL _fUseRegisterWaitFuncs;
  341. #endif
  342. };
  343. #endif // W32SCARD