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
15 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 2001
  3. Module Name:
  4. csplib.h
  5. General Cryptographic Service Provider Library
  6. Abstract:
  7. Author:
  8. Dan Griffin
  9. Notes:
  10. --*/
  11. #ifndef __CSP__LIB__H__
  12. #define __CSP__LIB__H__
  13. #include <windows.h>
  14. #include <wincrypt.h>
  15. #include <cspdk.h>
  16. //
  17. // Hash OID Encodings for PKCS #1 Signing
  18. //
  19. // Reverse ASN.1 Encodings of possible hash identifiers. The leading byte is
  20. // the length of the remaining byte string. The lists of possible identifiers
  21. // is terminated with a '\x00' entry.
  22. //
  23. static const BYTE
  24. *md2Encodings[]
  25. // 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
  26. = { (CONST BYTE *)"\x12\x10\x04\x00\x05\x02\x02\x0d\xf7\x86\x48\x86\x2a\x08\x06\x0c\x30\x20\x30",
  27. (CONST BYTE *)"\x10\x10\x04\x02\x02\x0d\xf7\x86\x48\x86\x2a\x08\x06\x0a\x30\x1e\x30",
  28. (CONST BYTE *)"\x00" },
  29. *md4Encodings[]
  30. = { (CONST BYTE *)"\x12\x10\x04\x00\x05\x04\x02\x0d\xf7\x86\x48\x86\x2a\x08\x06\x0c\x30\x20\x30",
  31. (CONST BYTE *)"\x10\x10\x04\x04\x02\x0d\xf7\x86\x48\x86\x2a\x08\x06\x0a\x30\x1e\x30",
  32. (CONST BYTE *)"\x00" },
  33. *md5Encodings[]
  34. = { (CONST BYTE *)"\x12\x10\x04\x00\x05\x05\x02\x0d\xf7\x86\x48\x86\x2a\x08\x06\x0c\x30\x20\x30",
  35. (CONST BYTE *)"\x10\x10\x04\x05\x02\x0d\xf7\x86\x48\x86\x2a\x08\x06\x0a\x30\x1e\x30",
  36. // The following encoding which excludes the digest algorithm was added
  37. // for: Nortel V1 Cert Signatures
  38. //
  39. // It can be removed when these type of certificates no longer exist.
  40. //
  41. // Since we only allow the digest OID to be omitted for MD5 there
  42. // isn't a compromise where another algorithm could be substituted.
  43. (CONST BYTE *)"\x02\x10\x04",
  44. (CONST BYTE *)"\x00" },
  45. *shaEncodings[]
  46. = { (CONST BYTE *)"\x0f\x14\x04\x00\x05\x1a\x02\x03\x0e\x2b\x05\x06\x09\x30\x21\x30",
  47. (CONST BYTE *)"\x0d\x14\x04\x1a\x02\x03\x0e\x2b\x05\x06\x07\x30\x1f\x30",
  48. (CONST BYTE *)"\x00"},
  49. *sha256Encodings[]
  50. = { (CONST BYTE *)"\x13\x20\x04\x00\x05\x01\x02\x04\x03\x65\x01\x48\x86\x60\x09\x06\x0d\x30\x31\x30",
  51. (CONST BYTE *)"\x11\x20\x04\x01\x02\x04\x03\x65\x01\x48\x86\x60\x09\x06\x0b\x30\x2f\x30",
  52. (CONST BYTE *)"\x00"},
  53. *sha384Encodings[]
  54. = { (CONST BYTE *)"\x13\x30\x04\x00\x05\x02\x02\x04\x03\x65\x01\x48\x86\x60\x09\x06\x0d\x30\x41\x30",
  55. (CONST BYTE *)"\x11\x30\x04\x02\x02\x04\x03\x65\x01\x48\x86\x60\x09\x06\x0b\x30\x3f\x30",
  56. (CONST BYTE *)"\x00"},
  57. *sha512Encodings[]
  58. = { (CONST BYTE *)"\x13\x40\x04\x00\x05\x03\x02\x04\x03\x65\x01\x48\x86\x60\x09\x06\x0d\x30\x51\x30",
  59. (CONST BYTE *)"\x11\x40\x04\x03\x02\x04\x03\x65\x01\x48\x86\x60\x09\x06\x0b\x30\x4f\x30",
  60. (CONST BYTE *)"\x00"},
  61. *endEncodings[]
  62. = { (CONST BYTE *)"\x00" };
  63. //
  64. // Type: USER_CONTEXT
  65. //
  66. typedef struct _USER_CONTEXT
  67. {
  68. HCRYPTPROV hSupportProv;
  69. //
  70. // The csplib will set this to the string value passed by the
  71. // caller to CryptAcquireContext. For a smartcard CSP, it might
  72. // include a reader name.
  73. //
  74. LPWSTR wszContainerNameFromCaller;
  75. //
  76. // The CSP allocates this string (using CspAllocH) and sets it to
  77. // the name of the key container being used for this context.
  78. //
  79. // The csplib will free this value on CryptReleaseContext.
  80. //
  81. LPWSTR wszBaseContainerName;
  82. BOOL fBaseContainerNameIsRpcUuid;
  83. //
  84. // The CSP allocates this string (using CspAllocH) and sets it to
  85. // the expanded representation of the container name. This may be the
  86. // same as the wszBaseContainerName value.
  87. //
  88. // The csplib will free this value on CryptReleaseContext.
  89. //
  90. LPWSTR wszUniqueContainerName;
  91. DWORD dwFlags;
  92. PVTableProvStrucW pVTableW;
  93. PVOID pvLocalUserContext;
  94. } USER_CONTEXT, *PUSER_CONTEXT;
  95. //
  96. // Type: KEY_CONTEXT
  97. //
  98. typedef struct _KEY_CONTEXT
  99. {
  100. PUSER_CONTEXT pUserContext;
  101. HCRYPTKEY hSupportKey;
  102. DWORD dwFlags;
  103. DWORD cKeyBits;
  104. ALG_ID Algid;
  105. PVOID pvLocalKeyContext;
  106. } KEY_CONTEXT, *PKEY_CONTEXT;
  107. //
  108. // Type: HASH_CONTEXT
  109. //
  110. typedef struct _HASH_CONTEXT
  111. {
  112. PUSER_CONTEXT pUserContext;
  113. HCRYPTHASH hSupportHash;
  114. DWORD dwFlags;
  115. ALG_ID Algid;
  116. PVOID pvLocalHashContext;
  117. } HASH_CONTEXT, *PHASH_CONTEXT;
  118. //
  119. // Type: LOCAL_CALL_INFO
  120. //
  121. typedef BOOL LOCAL_CALL_INFO, *PLOCAL_CALL_INFO;
  122. //
  123. // Function: LocalAcquireContext
  124. //
  125. typedef DWORD (WINAPI *PFN_LOCAL_ACQUIRE_CONTEXT)(
  126. IN OUT PUSER_CONTEXT pUserContext,
  127. OUT PLOCAL_CALL_INFO pLocalCallInfo);
  128. //
  129. // Function: LocalReleaseContext
  130. //
  131. typedef DWORD (WINAPI *PFN_LOCAL_RELEASE_CONTEXT)(
  132. IN OUT PUSER_CONTEXT pUserContext,
  133. IN DWORD dwFlags,
  134. OUT PLOCAL_CALL_INFO pLocalCallInfo);
  135. //
  136. // Function: LocalGenKey
  137. //
  138. typedef DWORD (WINAPI *PFN_LOCAL_GEN_KEY)(
  139. IN OUT PKEY_CONTEXT pKeyContext,
  140. OUT PLOCAL_CALL_INFO pLocalCallInfo);
  141. //
  142. // Function: LocalDeriveKey
  143. //
  144. typedef DWORD (WINAPI *PFN_LOCAL_DERIVE_KEY)(
  145. IN OUT PKEY_CONTEXT pKeyContext,
  146. IN PHASH_CONTEXT pHashContext,
  147. OUT PLOCAL_CALL_INFO pLocalCallInfo);
  148. //
  149. // Function: LocalDestroyKey
  150. //
  151. typedef DWORD (WINAPI *PFN_LOCAL_DESTROY_KEY)(
  152. IN OUT PKEY_CONTEXT pKeyContext,
  153. OUT PLOCAL_CALL_INFO pLocalCallInfo);
  154. //
  155. // Function: LocalSetKeyParam
  156. //
  157. typedef DWORD (WINAPI *PFN_LOCAL_SET_KEY_PARAM)(
  158. IN PKEY_CONTEXT pKeyContext,
  159. IN DWORD dwParam,
  160. IN PBYTE pbData,
  161. IN DWORD dwFlags,
  162. OUT PLOCAL_CALL_INFO pLocalCallInfo);
  163. //
  164. // Function: LocalGetKeyParam
  165. //
  166. typedef DWORD (WINAPI *PFN_LOCAL_GET_KEY_PARAM)(
  167. IN PKEY_CONTEXT pKeyContext,
  168. IN DWORD dwParam,
  169. OUT PBYTE pbData,
  170. IN OUT PDWORD pcbDataLen,
  171. IN DWORD dwFlags,
  172. OUT PLOCAL_CALL_INFO pLocalCallInfo);
  173. //
  174. // Function: LocalSetProvParam
  175. //
  176. typedef DWORD (WINAPI *PFN_LOCAL_SET_PROV_PARAM)(
  177. IN PUSER_CONTEXT pUserContext,
  178. IN DWORD dwParam,
  179. IN PBYTE pbData,
  180. IN DWORD dwFlags,
  181. OUT PLOCAL_CALL_INFO pLocalCallInfo);
  182. //
  183. // Function: LocalGetProvParam
  184. //
  185. typedef DWORD (WINAPI *PFN_LOCAL_GET_PROV_PARAM)(
  186. IN PUSER_CONTEXT pUserContext,
  187. IN DWORD dwParam,
  188. OUT PBYTE pbData,
  189. IN OUT PDWORD pcbDataLen,
  190. IN DWORD dwFlags,
  191. OUT PLOCAL_CALL_INFO pLocalCallInfo);
  192. //
  193. // Function: LocalSetHashParam
  194. //
  195. typedef DWORD (WINAPI *PFN_LOCAL_SET_HASH_PARAM)(
  196. IN PHASH_CONTEXT pHashContext,
  197. IN DWORD dwParam,
  198. IN PBYTE pbData,
  199. IN DWORD dwFlags,
  200. OUT PLOCAL_CALL_INFO pLocalCallInfo);
  201. //
  202. // Function: LocalGetHashParam
  203. //
  204. typedef DWORD (WINAPI *PFN_LOCAL_GET_HASH_PARAM)(
  205. IN PHASH_CONTEXT pHashContext,
  206. IN DWORD dwParam,
  207. OUT PBYTE pbData,
  208. IN OUT PDWORD pcbDataLen,
  209. IN DWORD dwFlags,
  210. OUT PLOCAL_CALL_INFO pLocalCallInfo);
  211. //
  212. // Function: LocalExportKey
  213. //
  214. typedef DWORD (WINAPI *PFN_LOCAL_EXPORT_KEY)(
  215. IN PKEY_CONTEXT pKeyContext,
  216. IN PKEY_CONTEXT pPubKey,
  217. IN DWORD dwBlobType,
  218. IN DWORD dwFlags,
  219. OUT PBYTE pbData,
  220. IN OUT PDWORD pcbDataLen,
  221. OUT PLOCAL_CALL_INFO pLocalCallInfo);
  222. //
  223. // Function: LocalImportKey
  224. //
  225. typedef DWORD (WINAPI *PFN_LOCAL_IMPORT_KEY)(
  226. IN PKEY_CONTEXT pKeyContext,
  227. IN PBYTE pbData,
  228. IN DWORD cbDataLen,
  229. IN PKEY_CONTEXT pPubKey,
  230. OUT PLOCAL_CALL_INFO pLocalCallInfo);
  231. //
  232. // Function: LocalEncrypt
  233. //
  234. typedef DWORD (WINAPI *PFN_LOCAL_ENCRYPT)(
  235. IN PKEY_CONTEXT pKeyContext,
  236. IN PHASH_CONTEXT pHashContext,
  237. IN BOOL fFinal,
  238. IN DWORD dwFlags,
  239. IN OUT LPBYTE pbData,
  240. IN OUT LPDWORD pcbDataLen,
  241. IN DWORD cbBufLen,
  242. OUT PLOCAL_CALL_INFO pLocalCallInfo);
  243. //
  244. // Function: LocalDecrypt
  245. //
  246. typedef DWORD (WINAPI *PFN_LOCAL_DECRYPT)(
  247. IN PKEY_CONTEXT pKeyContext,
  248. IN PHASH_CONTEXT pHashContext,
  249. IN BOOL fFinal,
  250. IN DWORD dwFlags,
  251. IN OUT LPBYTE pbData,
  252. IN OUT LPDWORD pcbDataLen,
  253. OUT PLOCAL_CALL_INFO pLocalCallInfo);
  254. //
  255. // Function: LocalCreateHash
  256. //
  257. typedef DWORD (WINAPI *PFN_LOCAL_CREATE_HASH)(
  258. IN PHASH_CONTEXT pHashContext,
  259. IN PKEY_CONTEXT pKeyContext,
  260. OUT PLOCAL_CALL_INFO pLocalCallInfo);
  261. //
  262. // Function: LocalHashData
  263. //
  264. typedef DWORD (WINAPI *PFN_LOCAL_HASH_DATA)(
  265. IN PHASH_CONTEXT pHashContext,
  266. IN CONST BYTE *pbData,
  267. IN DWORD cbDataLen,
  268. IN DWORD dwFlags,
  269. OUT PLOCAL_CALL_INFO pLocalCallInfo);
  270. //
  271. // Function: LocalHashSessionKey
  272. //
  273. typedef DWORD (WINAPI *PFN_LOCAL_HASH_SESSION_KEY)(
  274. IN PHASH_CONTEXT pHashContext,
  275. IN PKEY_CONTEXT pKeyContext,
  276. IN DWORD dwFlags,
  277. OUT PLOCAL_CALL_INFO pLocalCallInfo);
  278. //
  279. // Function: LocalSignHash
  280. //
  281. typedef DWORD (WINAPI *PFN_LOCAL_SIGN_HASH)(
  282. IN PHASH_CONTEXT pHashContext,
  283. IN DWORD dwKeySpec,
  284. IN DWORD dwFlags,
  285. OUT LPBYTE pbSignature,
  286. IN OUT LPDWORD pcbSigLen,
  287. OUT PLOCAL_CALL_INFO pLocalCallInfo);
  288. //
  289. // Function: LocalDestroyHash
  290. //
  291. typedef DWORD (WINAPI *PFN_LOCAL_DESTROY_HASH)(
  292. IN PHASH_CONTEXT pHashContext,
  293. OUT PLOCAL_CALL_INFO pLocalCallInfo);
  294. //
  295. // Function: LocalVerifySignature
  296. //
  297. typedef DWORD (WINAPI *PFN_LOCAL_VERIFY_SIGNATURE)(
  298. IN PHASH_CONTEXT pHashContext,
  299. IN CONST BYTE *pbSignature,
  300. IN DWORD cbSigLen,
  301. IN PKEY_CONTEXT pPubKey,
  302. IN DWORD dwFlags,
  303. OUT PLOCAL_CALL_INFO pLocalCallInfo);
  304. //
  305. // Function: LocalGenRandom
  306. //
  307. typedef DWORD (WINAPI *PFN_LOCAL_GEN_RANDOM)(
  308. IN PUSER_CONTEXT pUserContext,
  309. IN DWORD cbLen,
  310. OUT LPBYTE pbBuffer,
  311. OUT PLOCAL_CALL_INFO pLocalCallInfo);
  312. //
  313. // Function: LocalGetUserKey
  314. //
  315. typedef DWORD (WINAPI *PFN_LOCAL_GET_USER_KEY)(
  316. IN PKEY_CONTEXT pKeyContext,
  317. OUT PLOCAL_CALL_INFO pLocalCallInfo);
  318. //
  319. // Function: LocalDuplicateHash
  320. //
  321. typedef DWORD (WINAPI *PFN_LOCAL_DUPLICATE_HASH)(
  322. IN PHASH_CONTEXT pHashContext,
  323. IN LPDWORD pdwReserved,
  324. IN PHASH_CONTEXT pNewHashContext,
  325. OUT PLOCAL_CALL_INFO pLocalCallInfo);
  326. //
  327. // Function: LocalDuplicateKey
  328. //
  329. typedef DWORD (WINAPI *PFN_LOCAL_DUPLICATE_KEY)(
  330. IN PKEY_CONTEXT pKeyContext,
  331. IN LPDWORD pdwReserved,
  332. IN PKEY_CONTEXT pNewKeyContext,
  333. OUT PLOCAL_CALL_INFO pLocalCallInfo);
  334. //
  335. // Function: LocalDllInitialize
  336. //
  337. typedef BOOL (WINAPI *PFN_LOCAL_DLL_INITIALIZE)(
  338. IN PVOID hmod,
  339. IN ULONG Reason,
  340. IN PCONTEXT Context);
  341. //
  342. // Function: LocalDllRegisterServer
  343. //
  344. typedef DWORD (WINAPI *PFN_LOCAL_DLL_REGISTER_SERVER)(void);
  345. //
  346. // Function: LocalDllUnregisterServer
  347. //
  348. typedef DWORD (WINAPI *PFN_LOCAL_DLL_UNREGISTER_SERVER)(void);
  349. //
  350. // Type: LOCAL_CSP_INFO
  351. //
  352. typedef struct _LOCAL_CSP_INFO
  353. {
  354. //
  355. // Function pointers for the "local" CSP implementation to fill
  356. // in, and be called by the CSP library.
  357. //
  358. PFN_LOCAL_ACQUIRE_CONTEXT pfnLocalAcquireContext; // Required
  359. PFN_LOCAL_RELEASE_CONTEXT pfnLocalReleaseContext; // Required
  360. PFN_LOCAL_GEN_KEY pfnLocalGenKey;
  361. PFN_LOCAL_DERIVE_KEY pfnLocalDeriveKey;
  362. PFN_LOCAL_DESTROY_KEY pfnLocalDestroyKey;
  363. PFN_LOCAL_SET_KEY_PARAM pfnLocalSetKeyParam;
  364. PFN_LOCAL_GET_KEY_PARAM pfnLocalGetKeyParam;
  365. PFN_LOCAL_SET_PROV_PARAM pfnLocalSetProvParam;
  366. PFN_LOCAL_GET_PROV_PARAM pfnLocalGetProvParam;
  367. PFN_LOCAL_SET_HASH_PARAM pfnLocalSetHashParam;
  368. PFN_LOCAL_GET_HASH_PARAM pfnLocalGetHashParam;
  369. PFN_LOCAL_EXPORT_KEY pfnLocalExportKey;
  370. PFN_LOCAL_IMPORT_KEY pfnLocalImportKey;
  371. PFN_LOCAL_ENCRYPT pfnLocalEncrypt;
  372. PFN_LOCAL_DECRYPT pfnLocalDecrypt;
  373. PFN_LOCAL_CREATE_HASH pfnLocalCreateHash;
  374. PFN_LOCAL_HASH_DATA pfnLocalHashData;
  375. PFN_LOCAL_HASH_SESSION_KEY pfnLocalHashSessionKey;
  376. PFN_LOCAL_SIGN_HASH pfnLocalSignHash;
  377. PFN_LOCAL_DESTROY_HASH pfnLocalDestroyHash;
  378. PFN_LOCAL_VERIFY_SIGNATURE pfnLocalVerifySignature;
  379. PFN_LOCAL_GEN_RANDOM pfnLocalGenRandom;
  380. PFN_LOCAL_GET_USER_KEY pfnLocalGetUserKey;
  381. PFN_LOCAL_DUPLICATE_HASH pfnLocalDuplicateHash;
  382. PFN_LOCAL_DUPLICATE_KEY pfnLocalDuplicateKey;
  383. PFN_LOCAL_DLL_INITIALIZE pfnLocalDllInitialize;
  384. PFN_LOCAL_DLL_REGISTER_SERVER pfnLocalDllRegisterServer;
  385. PFN_LOCAL_DLL_UNREGISTER_SERVER pfnLocalDllUnregisterServer;
  386. //
  387. // Static data describing the local CSP.
  388. //
  389. LPWSTR wszProviderName;
  390. DWORD dwProviderType;
  391. DWORD dwImplementationType;
  392. //
  393. // Description of the support CSP to be used.
  394. //
  395. LPWSTR wszSupportProviderName;
  396. DWORD dwSupportProviderType;
  397. } LOCAL_CSP_INFO, *PLOCAL_CSP_INFO;
  398. //
  399. // General Wrappers
  400. //
  401. LPVOID WINAPI CspAllocH(
  402. IN SIZE_T cBytes);
  403. LPVOID WINAPI CspReAllocH(
  404. IN LPVOID pMem,
  405. IN SIZE_T cBytes);
  406. void WINAPI CspFreeH(
  407. IN LPVOID pMem);
  408. DWORD CspInitializeCriticalSection(
  409. IN CRITICAL_SECTION *pcs);
  410. DWORD CspEnterCriticalSection(
  411. IN CRITICAL_SECTION *pcs);
  412. void CspLeaveCriticalSection(
  413. IN CRITICAL_SECTION *pcs);
  414. void CspDeleteCriticalSection(
  415. IN CRITICAL_SECTION *pcs);
  416. DWORD WINAPI RegOpenProviderKey(
  417. IN OUT HKEY *phProviderKey,
  418. IN REGSAM samDesired);
  419. void SetLocalCallInfo(
  420. IN OUT PLOCAL_CALL_INFO pLocalCallInfo,
  421. IN BOOL fContinue);
  422. DWORD WINAPI CreateUuidContainerName(
  423. IN PUSER_CONTEXT pUserCtx);
  424. DWORD WINAPI ApplyPKCS1SigningFormat(
  425. IN ALG_ID HashAlgid,
  426. IN BYTE *pbHash,
  427. IN DWORD cbHash,
  428. IN DWORD dwFlags,
  429. IN DWORD cbModulus,
  430. OUT PBYTE *ppbPKCS1Format);
  431. #ifndef PKCS_BLOCKTYPE_2
  432. #define PKCS_BLOCKTYPE_2 2
  433. #endif
  434. DWORD WINAPI VerifyPKCS2Padding(
  435. IN PBYTE pbPaddedData,
  436. IN DWORD cbModulus,
  437. OUT PBYTE *ppbData,
  438. OUT PDWORD pcbData);
  439. #endif