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.

517 lines
12 KiB

  1. #ifndef __BASECSP_CAPI__H__
  2. #define __BASECSP_CAPI__H__
  3. #include <windows.h>
  4. #include "cardmod.h"
  5. #include "datacach.h"
  6. #include "csplib.h"
  7. #include "sccache.h"
  8. #include "resource.h"
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. //
  13. // Maximum length card ATR that we'll handle in the Base CSP
  14. //
  15. #define cbATR_BUFFER 32
  16. //
  17. // Maximum length pin that we'll handle
  18. //
  19. #define cchMAX_PIN_LENGTH 8
  20. //
  21. // Registry Information
  22. //
  23. #define wszREG_DEFAULT_KEY_LEN L"DefaultPrivateKeyLenBits"
  24. #define wszREG_REQUIRE_CARD_KEY_GEN L"RequireOnCardPrivateKeyGen"
  25. typedef struct _REG_CONFIG_VALUES
  26. {
  27. LPWSTR wszValueName;
  28. DWORD dwDefValue;
  29. } REG_CONFIG_VALUES, *PREG_CONFIG_VALUES;
  30. static REG_CONFIG_VALUES RegConfigValues [] =
  31. {
  32. { wszREG_DEFAULT_KEY_LEN, 1024 },
  33. { wszREG_REQUIRE_CARD_KEY_GEN, 0 }
  34. };
  35. typedef struct _CSP_REG_SETTINGS
  36. {
  37. DWORD cDefaultPrivateKeyLenBits;
  38. BOOL fRequireOnCardPrivateKeyGen;
  39. } CSP_REG_SETTINGS, *PCSP_REG_SETTINGS;
  40. DWORD WINAPI RegConfigAddEntries(
  41. IN HKEY hKey);
  42. DWORD WINAPI RegConfigGetSettings(
  43. IN OUT PCSP_REG_SETTINGS pRegSettings);
  44. //
  45. // General Wrappers
  46. //
  47. DWORD CountCharsInMultiSz(
  48. IN LPWSTR mwszStrings);
  49. // Display Strings
  50. typedef struct _CSP_STRING
  51. {
  52. LPWSTR wszString;
  53. DWORD dwResource;
  54. } CSP_STRING, *PCSP_STRING;
  55. enum CSP_STRINGS_INDEX
  56. {
  57. StringNewPinMismatch,
  58. StringPinMessageBoxTitle,
  59. StringWrongPin,
  60. StringPinRetries
  61. };
  62. typedef struct _CSP_STATE
  63. {
  64. CRITICAL_SECTION cs;
  65. DWORD dwRefCount;
  66. CACHEHANDLE hCache;
  67. HMODULE hCspModule;
  68. } CSP_STATE, *PCSP_STATE;
  69. //
  70. // Type: CONTAINER_MAP_RECORD
  71. //
  72. // This structure describes the format of the Base CSP's container map file,
  73. // stored on the card. This is well-known logical file wszCONTAINER_MAP_FILE.
  74. // The file consists of zero or more of these records.
  75. //
  76. #define MAX_CONTAINER_NAME_LEN 40
  77. // This flag is set in the CONTAINER_MAP_RECORD bFlags member if the
  78. // corresponding container is valid and currently exists on the card.
  79. // If the container is deleted, its bFlags field must be cleared.
  80. #define CONTAINER_MAP_VALID_CONTAINER 1
  81. // This flag is set in the CONTAINER_MAP_RECORD bFlags
  82. // member if the corresponding container is the default container on the card.
  83. #define CONTAINER_MAP_DEFAULT_CONTAINER 2
  84. typedef struct _CONTAINER_MAP_RECORD
  85. {
  86. WCHAR wszGuid [MAX_CONTAINER_NAME_LEN];
  87. BYTE bFlags;
  88. WORD wSigKeySizeBits;
  89. WORD wKeyExchangeKeySizeBits;
  90. } CONTAINER_MAP_RECORD, *PCONTAINER_MAP_RECORD;
  91. //
  92. // Type: CARD_CACHE_FILE_FORMAT
  93. //
  94. // This struct is used as the file format of the cache file,
  95. // as stored on the card.
  96. //
  97. #define CARD_CACHE_FILE_CURRENT_VERSION 1
  98. typedef struct _CARD_CACHE_FILE_FORMAT
  99. {
  100. BYTE bVersion;
  101. BYTE bPinsFreshness;
  102. WORD wContainersFreshness;
  103. WORD wFilesFreshness;
  104. } CARD_CACHE_FILE_FORMAT, *PCARD_CACHE_FILE_FORMAT;
  105. //
  106. // Type: CARD_STATE
  107. //
  108. #define CARD_STATE_CURRENT_VERSION 1
  109. typedef struct _CARD_STATE
  110. {
  111. DWORD dwVersion;
  112. PCARD_DATA pCardData;
  113. HMODULE hCardModule;
  114. PFN_CARD_ACQUIRE_CONTEXT pfnCardAcquireContext;
  115. WCHAR wszSerialNumber[MAX_PATH];
  116. PINCACHE_HANDLE hPinCache;
  117. // This flag is set every time the pin is successfully presented
  118. // to the card. If the flag is set when EndTransaction is called on the
  119. // card, the card will be deauthenticated (or Reset) and the flag cleared.
  120. // Otherwise, EndTransaction will simply leave the card.
  121. BOOL fAuthenticated;
  122. // A copy of the card cache file is kept in the CARD_STATE. The cache
  123. // file need only be read from the card once per transaction, although
  124. // it must also be updated on card writes.
  125. CARD_CACHE_FILE_FORMAT CacheFile;
  126. BOOL fCacheFileValid;
  127. CRITICAL_SECTION cs;
  128. BOOL fInitializedCS;
  129. CACHEHANDLE hCache;
  130. CACHEHANDLE hCacheCardModuleData;
  131. PFN_SCARD_CACHE_LOOKUP_ITEM pfnCacheLookup;
  132. PFN_SCARD_CACHE_ADD_ITEM pfnCacheAdd;
  133. HMODULE hWinscard;
  134. } CARD_STATE, *PCARD_STATE;
  135. //
  136. // Type: CARD_MATCH_DATA
  137. //
  138. #define CARD_MATCH_TYPE_READER_AND_CONTAINER 1
  139. #define CARD_MATCH_TYPE_SERIAL_NUMBER 2
  140. typedef struct _CARD_MATCH_DATA
  141. {
  142. //
  143. // Input parameters.
  144. //
  145. PCSP_STATE pCspState;
  146. DWORD dwCtxFlags;
  147. DWORD dwMatchType;
  148. DWORD dwUIFlags;
  149. DWORD cchMatchedReader;
  150. DWORD cchMatchedCard;
  151. DWORD cchMatchedSerialNumber;
  152. DWORD dwShareMode;
  153. DWORD dwPreferredProtocols;
  154. // Used in Reader and Container match requests
  155. LPWSTR pwszReaderName;
  156. LPWSTR pwszContainerName;
  157. BOOL fFreeContainerName;
  158. // Used in Serial Number match requests
  159. LPWSTR pwszSerialNumber;
  160. //
  161. // Internal parameters
  162. //
  163. PCARD_STATE pCardState;
  164. // Will be set when the current thread holds the transaction on the matched
  165. // card. This allows us to reduce the number of transactions required to
  166. // find a matching card (and complete the CryptAcquireContext call),
  167. // which reduces the number of times we have to read the cache file.
  168. //
  169. // The transaction will always be released before the select card check
  170. // callback returns.
  171. BOOL fTransacted;
  172. //
  173. // Output parameters
  174. //
  175. // Result of successful Card Search is
  176. // a valid, matching CARD_STATE structure.
  177. SCARDCONTEXT hSCardCtx;
  178. SCARDHANDLE hSCard;
  179. BYTE bContainerIndex;
  180. PCARD_STATE pUIMatchedCardState;
  181. WCHAR wszMatchedReader[MAX_PATH];
  182. WCHAR wszMatchedCard[MAX_PATH];
  183. DWORD dwActiveProtocol;
  184. // Result of an unsuccessful Card Search is that this
  185. // should be set to an appropriate error code.
  186. DWORD dwError;
  187. } CARD_MATCH_DATA, *PCARD_MATCH_DATA;
  188. DWORD FindCard(
  189. IN OUT PCARD_MATCH_DATA pCardMatchData);
  190. //
  191. // Defines for Card Specific Modules
  192. //
  193. // This value should be passed to
  194. //
  195. // SCardSetCardTypeProviderName
  196. // SCardGetCardTypeProviderName
  197. //
  198. // in order to query and set the Card Specific Module to be used
  199. // for a given card.
  200. #define SCARD_PROVIDER_CARD_MODULE 0x80000001
  201. //
  202. // Defines for Card Interface Layer operations
  203. //
  204. //
  205. // Function: InitializeCardState
  206. //
  207. DWORD InitializeCardState(PCARD_STATE pCardState);
  208. //
  209. // Function: DeleteCardState
  210. //
  211. void DeleteCardState(PCARD_STATE pCardState);
  212. //
  213. // Function: InitializeCardData
  214. //
  215. DWORD InitializeCardData(PCARD_DATA pCardData);
  216. //
  217. // Function: IntializeCspCaching
  218. //
  219. DWORD InitializeCspCaching(IN OUT PCARD_STATE pCardState);
  220. //
  221. // Function: CleanupCardData
  222. //
  223. void CleanupCardData(PCARD_DATA pCardData);
  224. //
  225. // Function: ValidateReconnectCardHandle
  226. //
  227. DWORD ValidateCardHandle(
  228. IN PCARD_STATE pCardState,
  229. IN BOOL fMayReleaseContextHandle,
  230. OUT OPTIONAL BOOL *pfFlushPinCache);
  231. //
  232. // Function: CspBeginTransaction
  233. //
  234. DWORD CspBeginTransaction(
  235. IN PCARD_STATE pCardState);
  236. //
  237. // Function: CspEndTransaction
  238. //
  239. DWORD CspEndTransaction(
  240. IN PCARD_STATE pCardState);
  241. //
  242. // Function: CspQueryCapabilities
  243. //
  244. DWORD
  245. WINAPI
  246. CspQueryCapabilities(
  247. IN PCARD_STATE pCardState,
  248. IN OUT PCARD_CAPABILITIES pCardCapabilities);
  249. //
  250. // Function: CspDeleteContainer
  251. //
  252. DWORD
  253. WINAPI
  254. CspDeleteContainer(
  255. IN PCARD_STATE pCardState,
  256. IN BYTE bContainerIndex,
  257. IN DWORD dwReserved);
  258. //
  259. // Function: CspCreateContainer
  260. //
  261. DWORD
  262. WINAPI
  263. CspCreateContainer(
  264. IN PCARD_STATE pCardState,
  265. IN BYTE bContainerIndex,
  266. IN DWORD dwFlags,
  267. IN DWORD dwKeySpec,
  268. IN DWORD dwKeySize,
  269. IN PBYTE pbKeyData);
  270. //
  271. // Function: CspGetContainerInfo
  272. //
  273. DWORD
  274. WINAPI
  275. CspGetContainerInfo(
  276. IN PCARD_STATE pCardState,
  277. IN BYTE bContainerIndex,
  278. IN DWORD dwFlags,
  279. IN OUT PCONTAINER_INFO pContainerInfo);
  280. //
  281. // Function: CspRemoveCachedPin
  282. //
  283. void
  284. WINAPI
  285. CspRemoveCachedPin(
  286. IN PCARD_STATE pCardState,
  287. IN LPWSTR pwszUserId);
  288. //
  289. // Function: CspChangeAuthenticator
  290. //
  291. DWORD
  292. WINAPI
  293. CspChangeAuthenticator(
  294. IN PCARD_STATE pCardState,
  295. IN LPWSTR pwszUserId,
  296. IN PBYTE pbCurrentAuthenticator,
  297. IN DWORD cbCurrentAuthenticator,
  298. IN PBYTE pbNewAuthenticator,
  299. IN DWORD cbNewAuthenticator,
  300. IN DWORD cRetryCount,
  301. OUT OPTIONAL PDWORD pcAttemptsRemaining);
  302. //
  303. // Function: CspSubmitPin
  304. //
  305. DWORD
  306. WINAPI
  307. CspSubmitPin(
  308. IN PCARD_STATE pCardState,
  309. IN LPWSTR pwszUserId,
  310. IN PBYTE pbPin,
  311. IN DWORD cbPin,
  312. OUT OPTIONAL PDWORD pcAttemptsRemaining);
  313. //
  314. // Function: CspCreateFile
  315. //
  316. DWORD
  317. WINAPI
  318. CspCreateFile(
  319. IN PCARD_STATE pCardState,
  320. IN LPWSTR pwszFileName,
  321. IN CARD_FILE_ACCESS_CONDITION AccessCondition);
  322. //
  323. // Function: CspReadFile
  324. //
  325. DWORD
  326. WINAPI
  327. CspReadFile(
  328. IN PCARD_STATE pCardState,
  329. IN LPWSTR pwszFileName,
  330. IN DWORD dwFlags,
  331. OUT PBYTE *ppbData,
  332. OUT PDWORD pcbData);
  333. //
  334. // Function: CspWriteFile
  335. //
  336. DWORD
  337. WINAPI
  338. CspWriteFile(
  339. IN PCARD_STATE pCardState,
  340. IN LPWSTR pwszFileName,
  341. IN DWORD dwFlags,
  342. IN PBYTE pbData,
  343. IN DWORD cbData);
  344. //
  345. // Function: CspDeleteFile
  346. //
  347. DWORD
  348. WINAPI
  349. CspDeleteFile(
  350. IN PCARD_STATE pCardState,
  351. IN DWORD dwReserved,
  352. IN LPWSTR pwszFileName);
  353. //
  354. // Function: CspEnumFiles
  355. //
  356. DWORD
  357. WINAPI
  358. CspEnumFiles(
  359. IN PCARD_STATE pCardState,
  360. IN DWORD dwFlags,
  361. IN OUT LPWSTR *pmwszFileName);
  362. //
  363. // Function: CspQueryFreeSpace
  364. //
  365. DWORD
  366. WINAPI
  367. CspQueryFreeSpace(
  368. IN PCARD_STATE pCardState,
  369. IN DWORD dwFlags,
  370. OUT PCARD_FREE_SPACE_INFO pCardFreeSpaceInfo);
  371. //
  372. // Function: CspPrivateKeyDecrypt
  373. //
  374. DWORD
  375. WINAPI
  376. CspPrivateKeyDecrypt(
  377. IN PCARD_STATE pCardState,
  378. IN PCARD_PRIVATE_KEY_DECRYPT_INFO pInfo);
  379. //
  380. // Function: CspQueryKeySizes
  381. //
  382. DWORD
  383. WINAPI
  384. CspQueryKeySizes(
  385. IN PCARD_STATE pCardState,
  386. IN DWORD dwKeySpec,
  387. IN DWORD dwReserved,
  388. OUT PCARD_KEY_SIZES pKeySizes);
  389. //
  390. // Container Map Functions
  391. //
  392. DWORD ContainerMapEnumContainers(
  393. IN PCARD_STATE pCardState,
  394. OUT PBYTE pcContainers,
  395. OUT OPTIONAL LPWSTR *mwszContainers);
  396. DWORD ContainerMapFindContainer(
  397. IN PCARD_STATE pCardState,
  398. IN OUT PCONTAINER_MAP_RECORD pContainer,
  399. OUT OPTIONAL PBYTE pbContainerIndex);
  400. DWORD ContainerMapGetDefaultContainer(
  401. IN PCARD_STATE pCardState,
  402. OUT PCONTAINER_MAP_RECORD pContainer,
  403. OUT OPTIONAL PBYTE pbContainerIndex);
  404. DWORD ContainerMapSetDefaultContainer(
  405. IN PCARD_STATE pCardState,
  406. IN LPWSTR pwszContainerGuid);
  407. DWORD ContainerMapAddContainer(
  408. IN PCARD_STATE pCardState,
  409. IN LPWSTR pwszContainerGuid,
  410. IN DWORD cKeySizeBits,
  411. IN DWORD dwKeySpec,
  412. IN BOOL fGetNameOnly,
  413. OUT PBYTE pbContainerIndex);
  414. DWORD ContainerMapDeleteContainer(
  415. IN PCARD_STATE pCardState,
  416. IN LPWSTR pwszContainerGuid,
  417. OUT PBYTE pbContainerIndex);
  418. //
  419. // UI Functions
  420. //
  421. typedef struct _CSP_PROMPT_FOR_PIN_INFO
  422. {
  423. IN LPWSTR wszUser;
  424. OUT LPWSTR wszPin;
  425. } CSP_PROMPT_FOR_PIN_INFO, *PCSP_PROMPT_FOR_PIN_INFO;
  426. DWORD
  427. WINAPI
  428. CspPromptForPin(
  429. IN OUT PCSP_PROMPT_FOR_PIN_INFO pInfo);
  430. #ifdef __cplusplus
  431. }
  432. #endif
  433. #endif