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.

453 lines
11 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1999 - 1999
  3. Module Name:
  4. Database
  5. Abstract:
  6. The ISCardDatabase interface provides the methods for performing the smart
  7. card resource manager's database operations. These operations include
  8. listing known smart cards, readers, and reader groups, plus retrieving the
  9. interfaces supported by a smart card and its primary service provider.
  10. Author:
  11. Doug Barlow (dbarlow) 6/21/1999
  12. Notes:
  13. The identifier of the primary service provider is a COM GUID that can be
  14. used to instantiate and use the COM objects for a specific card.
  15. This is a rewrite of the original code by Mike Gallagher and Chris Dudley.
  16. --*/
  17. #ifndef WIN32_LEAN_AND_MEAN
  18. #define WIN32_LEAN_AND_MEAN
  19. #endif
  20. #include "stdafx.h"
  21. #include "Conversion.h"
  22. #include "Database.h"
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CSCardDatabase
  25. /*++
  26. GetProviderCardId:
  27. The GetProviderCardId method retrieves the identifier (GUID) of the primary
  28. service provider for the specified smart card.
  29. Arguments:
  30. bstrCardName [in] Name of the smart card.
  31. ppguidProviderId [out, retval] Pointer to the primary service provider's
  32. identifier (GUID) if successful; NULL if operation failed.
  33. Return Value:
  34. The return value is an HRESULT. A value of S_OK indicates the call was
  35. successful.
  36. Remarks:
  37. To retrieve all known smart cards, readers and reader groups call ListCard,
  38. ListReaders, and ListReaderGroups respectively.
  39. For a list of all the methods provided by the ISCardDatabase interface, see
  40. ISCardDatabase.
  41. In addition to the COM error codes listed above, this interface may return
  42. a smart card error code if a smart card function was called to complete the
  43. request. For information on smart card error codes, see Error Codes.
  44. Author:
  45. Doug Barlow (dbarlow) 6/21/1999
  46. --*/
  47. #undef __SUBROUTINE__
  48. #define __SUBROUTINE__ TEXT("CSCardDatabase::GetProviderCardId")
  49. STDMETHODIMP
  50. CSCardDatabase::GetProviderCardId(
  51. /* [in] */ BSTR bstrCardName,
  52. /* [retval][out] */ LPGUID __RPC_FAR *ppguidProviderId)
  53. {
  54. HRESULT hReturn = S_OK;
  55. try
  56. {
  57. DWORD dwSts;
  58. CTextString tzCard;
  59. tzCard = bstrCardName;
  60. dwSts = SCardGetProviderId(
  61. NULL,
  62. tzCard,
  63. *ppguidProviderId);
  64. hReturn = HRESULT_FROM_WIN32(dwSts);
  65. }
  66. catch (DWORD dwError)
  67. {
  68. hReturn = HRESULT_FROM_WIN32(dwError);
  69. }
  70. catch (HRESULT hError)
  71. {
  72. hReturn = hError;
  73. }
  74. catch (...)
  75. {
  76. hReturn = E_INVALIDARG;
  77. }
  78. return hReturn;
  79. }
  80. /*++
  81. CSCardDatabase::ListCardInterfaces:
  82. The ListCardInterfaces method retrieves the identifiers (GUIDs) of all the
  83. interfaces supported for the specified smart card.
  84. Arguments:
  85. bstrCardName [in] Name of the smart card.
  86. ppInterfaceGuids [out, retval] Pointer to the interface GUIDs if
  87. successful; NULL if operation failed.
  88. Return Value:
  89. The return value is an HRESULT. A value of S_OK indicates the call was
  90. successful.
  91. Remarks:
  92. To retrieve the primary service provider of the smart card, call
  93. GetProviderCardId.
  94. To retrieve all known smart cards, readers, and reader groups call
  95. ListCard, ListReaders, and ListReaderGroups respectively.
  96. For a list of all the methods provided by the ISCardDatabase interface, see
  97. ISCardDatabase.
  98. In addition to the COM error codes listed above, this interface may return
  99. a smart card error code if a smart card function was called to complete the
  100. request. For information on smart card error codes, see Error Codes.
  101. Author:
  102. Doug Barlow (dbarlow) 6/21/1999
  103. --*/
  104. #undef __SUBROUTINE__
  105. #define __SUBROUTINE__ TEXT("CSCardDatabase::ListCardInterfaces")
  106. STDMETHODIMP
  107. CSCardDatabase::ListCardInterfaces(
  108. /* [in] */ BSTR bstrCardName,
  109. /* [retval][out] */ LPSAFEARRAY __RPC_FAR *ppInterfaceGuids)
  110. {
  111. HRESULT hReturn = S_OK;
  112. LPGUID pGuids = NULL;
  113. try
  114. {
  115. DWORD dwSts;
  116. DWORD cguid = SCARD_AUTOALLOCATE;
  117. CTextString tzCard;
  118. tzCard = bstrCardName;
  119. dwSts = SCardListInterfaces(
  120. NULL,
  121. tzCard,
  122. (LPGUID)&pGuids,
  123. &cguid);
  124. if (SCARD_S_SUCCESS == dwSts)
  125. {
  126. GuidArrayToSafeArray(pGuids, cguid, ppInterfaceGuids);
  127. }
  128. hReturn = HRESULT_FROM_WIN32(dwSts);
  129. }
  130. catch (DWORD dwError)
  131. {
  132. hReturn = HRESULT_FROM_WIN32(dwError);
  133. }
  134. catch (HRESULT hError)
  135. {
  136. hReturn = hError;
  137. }
  138. catch (...)
  139. {
  140. hReturn = E_INVALIDARG;
  141. }
  142. if (NULL != pGuids)
  143. SCardFreeMemory(NULL, pGuids);
  144. return hReturn;
  145. }
  146. /*++
  147. CSCardDatabase::ListCards:
  148. The ListCards method retrieves all of the smart card names that match the
  149. specified interface identifiers (GUIDs), the specified ATR string, or both.
  150. Arguments:
  151. pAtr [in, defaultvalue(NULL) ] Pointer to a smart card ATR string. The ATR
  152. string must be packaged into an IByteBuffer.
  153. pInterfaceGuids [in, defaultvalue(NULL)] Pointer to a SAFEARRAY of COM
  154. interface identifiers (GUIDs) in BSTR format.
  155. localeId [in, lcid, defaultvalue(0x0409)] Language localization identifier.
  156. ppCardNames [out, retval] Pointer to a SAFEARRAY of BSTRs that contains the
  157. names of the smart cards that satisfied the search parameters if
  158. successful; NULL if the operation failed.
  159. Return Value:
  160. The return value is an HRESULT. A value of S_OK indicates the call was
  161. successful.
  162. Remarks:
  163. To retrieve all known readers or reader groups, call ListReaders or
  164. ListReaderGroups respectively.
  165. To retrieve the primary service provider or the interfaces of a specific
  166. card GetProviderCardId or ListCardInterfaces respectively.
  167. For a list of all the methods provided by the ISCardDatabase interface, see
  168. ISCardDatabase.
  169. In addition to the COM error codes listed above, this interface may return
  170. a smart card error code if a smart card function was called to complete the
  171. request. For information on smart card error codes, see Error Codes.
  172. Author:
  173. Doug Barlow (dbarlow) 6/21/1999
  174. --*/
  175. #undef __SUBROUTINE__
  176. #define __SUBROUTINE__ TEXT("CSCardDatabase::ListCards")
  177. STDMETHODIMP
  178. CSCardDatabase::ListCards(
  179. /* [defaultvalue][in] */ LPBYTEBUFFER pAtr,
  180. /* [defaultvalue][in] */ LPSAFEARRAY pInterfaceGuids,
  181. /* [defaultvalue][lcid][in] */ long localeId,
  182. /* [retval][out] */ LPSAFEARRAY __RPC_FAR *ppCardNames)
  183. {
  184. HRESULT hReturn = S_OK;
  185. LPTSTR szCards = NULL;
  186. try
  187. {
  188. LONG lSts;
  189. CBuffer bfGuids, bfAtr(36);
  190. LPCGUID pGuids = NULL;
  191. LPCBYTE pbAtr = NULL;
  192. DWORD cguids = 0;
  193. DWORD dwLen;
  194. if (NULL != pInterfaceGuids)
  195. {
  196. SafeArrayToGuidArray(pInterfaceGuids, bfGuids, &cguids);
  197. pGuids = (LPCGUID)bfGuids.Access();
  198. }
  199. ByteBufferToBuffer(pAtr, bfAtr);
  200. dwLen = SCARD_AUTOALLOCATE;
  201. lSts = SCardListCards(
  202. NULL,
  203. pbAtr,
  204. pGuids,
  205. cguids,
  206. (LPTSTR)&szCards,
  207. &dwLen);
  208. if (SCARD_S_SUCCESS != lSts)
  209. throw (HRESULT)HRESULT_FROM_WIN32(lSts);
  210. MultiStringToSafeArray(szCards, ppCardNames);
  211. }
  212. catch (DWORD dwError)
  213. {
  214. hReturn = HRESULT_FROM_WIN32(dwError);
  215. }
  216. catch (HRESULT hError)
  217. {
  218. hReturn = hError;
  219. }
  220. catch (...)
  221. {
  222. hReturn = E_INVALIDARG;
  223. }
  224. if (NULL != szCards)
  225. SCardFreeMemory(NULL, szCards);
  226. return hReturn;
  227. }
  228. /*++
  229. CSCardDatabase::ListReaderGroups:
  230. The ListReaderGroups method retrieves the names of the reader groups
  231. registered in the smart card database.
  232. Arguments:
  233. localeId [in, lcid, defaultvalue(0x0409)] Language localization ID.
  234. ppReaderGroups [out, retval] Pointer to a SAFEARRAY of BSTRs that contains
  235. the names of the smart card reader groups that satisfied the search
  236. parameters if successful; NULL if the operation failed.
  237. Return Value:
  238. The return value is an HRESULT. A value of S_OK indicates the call was
  239. successful.
  240. Remarks:
  241. ?Remarks?
  242. Author:
  243. Doug Barlow (dbarlow) 6/21/1999
  244. --*/
  245. #undef __SUBROUTINE__
  246. #define __SUBROUTINE__ TEXT("CSCardDatabase::ListReaderGroups")
  247. STDMETHODIMP
  248. CSCardDatabase::ListReaderGroups(
  249. /* [defaultvalue][lcid][in] */ long localeId,
  250. /* [retval][out] */ LPSAFEARRAY __RPC_FAR *ppReaderGroups)
  251. {
  252. HRESULT hReturn = S_OK;
  253. LPTSTR szGroups = NULL;
  254. try
  255. {
  256. LONG lSts;
  257. DWORD dwLen;
  258. dwLen = SCARD_AUTOALLOCATE;
  259. lSts = SCardListReaderGroups(NULL, (LPTSTR)&szGroups, &dwLen);
  260. if (SCARD_S_SUCCESS != lSts)
  261. throw (HRESULT)HRESULT_FROM_WIN32(lSts);
  262. MultiStringToSafeArray(szGroups, ppReaderGroups);
  263. }
  264. catch (DWORD dwError)
  265. {
  266. hReturn = HRESULT_FROM_WIN32(dwError);
  267. }
  268. catch (HRESULT hError)
  269. {
  270. hReturn = hError;
  271. }
  272. catch (...)
  273. {
  274. hReturn = E_INVALIDARG;
  275. }
  276. if (NULL != szGroups)
  277. SCardFreeMemory(NULL, szGroups);
  278. return hReturn;
  279. }
  280. /*++
  281. CSCardDatabase::ListReaders:
  282. The ListReaders method retrieves the names of the smart card readers
  283. registered in the smart card database.
  284. Arguments:
  285. localeId [in, lcid, defaultvalue(0x0409)] Language localization ID.
  286. ppReaders [out, retval] Pointer to a SAFEARRAY of BSTRs that contains the
  287. names of the smart card readers if successful; NULL if the operation
  288. failed.
  289. Return Value:
  290. The return value is an HRESULT. A value of S_OK indicates the call was
  291. successful.
  292. Remarks:
  293. ?Remarks?
  294. Author:
  295. Doug Barlow (dbarlow) 6/21/1999
  296. --*/
  297. #undef __SUBROUTINE__
  298. #define __SUBROUTINE__ TEXT("CSCardDatabase::ListReaders")
  299. STDMETHODIMP
  300. CSCardDatabase::ListReaders(
  301. /* [defaultvalue][lcid][in] */ long localeId,
  302. /* [retval][out] */ LPSAFEARRAY __RPC_FAR *ppReaders)
  303. {
  304. HRESULT hReturn = S_OK;
  305. LPTSTR szReaders = NULL;
  306. try
  307. {
  308. LONG lSts;
  309. DWORD dwLen;
  310. dwLen = SCARD_AUTOALLOCATE;
  311. lSts = SCardListReaders(NULL, NULL, (LPTSTR)&szReaders, &dwLen);
  312. if (SCARD_S_SUCCESS != lSts)
  313. throw (HRESULT)HRESULT_FROM_WIN32(lSts);
  314. MultiStringToSafeArray(szReaders, ppReaders);
  315. }
  316. catch (DWORD dwError)
  317. {
  318. hReturn = HRESULT_FROM_WIN32(dwError);
  319. }
  320. catch (HRESULT hError)
  321. {
  322. hReturn = hError;
  323. }
  324. catch (...)
  325. {
  326. hReturn = E_INVALIDARG;
  327. }
  328. if (NULL != szReaders)
  329. SCardFreeMemory(NULL, szReaders);
  330. return hReturn;
  331. }