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.

487 lines
17 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. // FILE : contman.h //
  3. // DESCRIPTION : include file //
  4. // AUTHOR : //
  5. // HISTORY : //
  6. // Mar 16 1998 jeffspel Created //
  7. // //
  8. // Copyright (C) 1998 Microsoft Corporation All Rights Reserved //
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef __CONTMAN_H__
  11. #define __CONTMAN_H__
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. extern LPVOID ContAlloc(ULONG cbLen);
  16. extern LPVOID ContRealloc(LPVOID pvMem, ULONG cbLen);
  17. extern void ContFree(LPVOID pvMem);
  18. // Display Strings
  19. typedef struct _CSP_STRINGS_ {
  20. // RSA
  21. LPWSTR pwszSignWExch;
  22. LPWSTR pwszCreateRSASig;
  23. LPWSTR pwszCreateRSAExch;
  24. LPWSTR pwszRSASigDescr;
  25. LPWSTR pwszRSAExchDescr;
  26. LPWSTR pwszImportSimple;
  27. // DSS-DH
  28. LPWSTR pwszCreateDSS;
  29. LPWSTR pwszCreateDH;
  30. LPWSTR pwszImportDHPub;
  31. LPWSTR pwszDSSSigDescr;
  32. LPWSTR pwszDHExchDescr;
  33. // BOTH
  34. LPWSTR pwszSigning;
  35. LPWSTR pwszMigrKeys;
  36. LPWSTR pwszImportPrivSig;
  37. LPWSTR pwszImportPrivExch;
  38. LPWSTR pwszExportPrivSig;
  39. LPWSTR pwszExportPrivExch;
  40. LPWSTR pwszDeleteSig;
  41. LPWSTR pwszDeleteMigrSig;
  42. LPWSTR pwszDeleteExch;
  43. LPWSTR pwszDeleteMigrExch;
  44. LPWSTR pwszAuditCapiKey;
  45. } CSP_STRINGS, *PCSP_STRINGS;
  46. #define SZLOCALMACHINECRYPTO "Software\\Microsoft\\Cryptography"
  47. #define SZCRYPTOMACHINEGUID "MachineGuid"
  48. #define KEY_CONTAINER_FILE_FORMAT_VER 2
  49. #define STUFF_TO_GO_INTO_MIX "Hj1diQ6kpUx7VC4m"
  50. typedef struct _KEY_EXPORTABILITY_LENS_ {
  51. DWORD cbSigExportability;
  52. DWORD cbExchExportability;
  53. } KEY_EXPORTABILITY_LENS, *PKEY_EXPORTABILITY_LENS;
  54. typedef struct _KEY_CONTAINER_LENS_ {
  55. DWORD dwUIOnKey;
  56. DWORD cbName;
  57. DWORD cbSigPub;
  58. DWORD cbSigEncPriv;
  59. DWORD cbExchPub;
  60. DWORD cbExchEncPriv;
  61. DWORD cbRandom; // length of Random number seed
  62. } KEY_CONTAINER_LENS, *PKEY_CONTAINER_LENS;
  63. typedef struct _KEY_CONTAINER_INFO_ {
  64. DWORD dwVersion;
  65. KEY_CONTAINER_LENS ContLens;
  66. BOOL fCryptSilent;
  67. BYTE *pbSigPub;
  68. BYTE *pbSigEncPriv;
  69. BOOL fSigExportable;
  70. BYTE *pbExchPub;
  71. BYTE *pbExchEncPriv;
  72. BOOL fExchExportable;
  73. BYTE *pbRandom;
  74. LPSTR pszUserName;
  75. WCHAR rgwszFileName[80];
  76. HANDLE hFind; // for enuming containers
  77. DWORD dwiRegEntry; // for enuming containers
  78. DWORD cMaxRegEntry; // for enuming containers
  79. DWORD cbRegEntry; // for enuming containers
  80. CHAR *pchEnumRegEntries; // for enuming containers
  81. BOOL fCryptFirst; // for enuming containers
  82. BOOL fNoMoreFiles; // for enuming containers
  83. DWORD cbOldMachKeyEntry; // for enuming containers
  84. DWORD dwiOldMachKeyEntry; // for enuming containers
  85. DWORD cMaxOldMachKeyEntry; // for enuming containers
  86. CHAR *pchEnumOldMachKeyEntries; // for enuming containers
  87. BOOL fForceHighKeyProtection;
  88. // Context items required for caching private keys
  89. BOOL fCachePrivateKeys;
  90. DWORD cMaxKeyLifetime; // in milliseconds
  91. DWORD dwSigKeyTimestamp;
  92. DWORD dwKeyXKeyTimestamp;
  93. } KEY_CONTAINER_INFO, *PKEY_CONTAINER_INFO;
  94. // define flag for leaving old keys in the registry if they are in the .Default
  95. // hive but are user keys
  96. #define LEAVE_OLD_KEYS 1
  97. // define flag indicating that the thread cannot get the SACL info from the
  98. // old registry key when migrating keys
  99. #define PRIVILEDGE_FOR_SACL 2
  100. #define MY_RTL_ENCRYPT_MEMORY_SIZE \
  101. (2 * RTL_ENCRYPT_MEMORY_SIZE)
  102. DWORD GetKeySizeForEncryptMemory(
  103. IN DWORD cbKey);
  104. DWORD MyRtlEncryptMemory(
  105. IN PVOID pvMem,
  106. IN DWORD cbMem);
  107. DWORD MyRtlDecryptMemory(
  108. IN PVOID pvMem,
  109. IN DWORD cbMem);
  110. //
  111. // Just tries to use DPAPI to make sure it works before creating a key
  112. // container.
  113. //
  114. DWORD TryDPAPI();
  115. DWORD
  116. MyCryptProtectData(
  117. IN DATA_BLOB* pDataIn,
  118. IN LPCWSTR szDataDescr,
  119. IN OPTIONAL DATA_BLOB* pOptionalEntropy,
  120. IN PVOID pvReserved,
  121. IN OPTIONAL CRYPTPROTECT_PROMPTSTRUCT* pPromptStruct,
  122. IN DWORD dwFlags,
  123. OUT DATA_BLOB* pDataOut // out encr blob
  124. );
  125. DWORD
  126. MyCryptUnprotectData(
  127. IN DATA_BLOB* pDataIn, // in encr blob
  128. OUT OPTIONAL LPWSTR* ppszDataDescr, // out
  129. IN OPTIONAL DATA_BLOB* pOptionalEntropy,
  130. IN PVOID pvReserved,
  131. IN OPTIONAL CRYPTPROTECT_PROMPTSTRUCT* pPromptStruct,
  132. IN DWORD dwFlags,
  133. OUT DATA_BLOB* pDataOut,
  134. OUT LPDWORD pdwReprotectFlags);
  135. void FreeEnumOldMachKeyEntries(
  136. PKEY_CONTAINER_INFO pInfo
  137. );
  138. void FreeEnumRegEntries(
  139. PKEY_CONTAINER_INFO pInfo
  140. );
  141. void FreeContainerInfo(
  142. PKEY_CONTAINER_INFO pInfo
  143. );
  144. BOOL WINAPI FIsWinNT(void);
  145. DWORD
  146. IsLocalSystem(
  147. BOOL *pfIsLocalSystem
  148. );
  149. DWORD
  150. GetUserTextualSidA(
  151. LPSTR lpBuffer,
  152. LPDWORD nSize
  153. );
  154. DWORD
  155. GetUserTextualSidW(
  156. LPWSTR lpBuffer,
  157. LPDWORD nSize
  158. );
  159. DWORD SetMachineGUID();
  160. DWORD SetContainerUserName(
  161. IN LPSTR pszUserName,
  162. IN PKEY_CONTAINER_INFO pContInfo
  163. );
  164. DWORD ReadContainerInfo(
  165. IN DWORD dwProvType,
  166. IN LPSTR pszContainerName,
  167. IN BOOL fMachineKeyset,
  168. IN DWORD dwFlags,
  169. OUT PKEY_CONTAINER_INFO pContInfo
  170. );
  171. DWORD WriteContainerInfo(
  172. IN DWORD dwProvType,
  173. IN LPWSTR pwszFileName,
  174. IN BOOL fMachineKeyset,
  175. IN PKEY_CONTAINER_INFO pContInfo
  176. );
  177. DWORD DeleteContainerInfo(
  178. IN DWORD dwProvType,
  179. IN LPSTR pszFileName,
  180. IN BOOL fMachineKeyset
  181. );
  182. DWORD GetUniqueContainerName(
  183. IN KEY_CONTAINER_INFO *pContInfo,
  184. OUT BYTE *pbData,
  185. OUT DWORD *pcbData
  186. );
  187. DWORD GetNextContainer(
  188. IN DWORD dwProvType,
  189. IN BOOL fMachineKeyset,
  190. IN DWORD dwFlags,
  191. OUT LPSTR pszNextContainer,
  192. IN OUT DWORD *pcbNextContainer,
  193. IN OUT HANDLE *phFind
  194. );
  195. DWORD SetSecurityOnContainer(
  196. IN LPCWSTR wszFileName,
  197. IN DWORD dwProvType,
  198. IN DWORD fMachineKeyset,
  199. IN SECURITY_INFORMATION SecurityInformation,
  200. IN PSECURITY_DESCRIPTOR pSecurityDescriptor
  201. );
  202. //+ ===========================================================================
  203. //
  204. // The function adjusts the token priviledges so that SACL information
  205. // may be set on a key container. If the token priviledges may be set
  206. // indicated by the pUser->dwOldKeyFlags having the PRIVILEDGE_FOR_SACL value set.
  207. // value set then the token privilege is adjusted before the security
  208. // descriptor is set on the container. This is needed for the key
  209. // migration case when keys are being migrated from the registry to files.
  210. //- ============================================================================
  211. DWORD SetSecurityOnContainerWithTokenPriviledges(
  212. IN DWORD dwOldKeyFlags,
  213. IN LPCWSTR wszFileName,
  214. IN DWORD dwProvType,
  215. IN DWORD fMachineKeyset,
  216. IN SECURITY_INFORMATION SecurityInformation,
  217. IN PSECURITY_DESCRIPTOR pSecurityDescriptor
  218. );
  219. DWORD GetSecurityOnContainer(
  220. IN LPCWSTR wszFileName,
  221. IN DWORD dwProvType,
  222. IN DWORD fMachineKeyset,
  223. IN SECURITY_INFORMATION RequestedInformation,
  224. OUT PSECURITY_DESCRIPTOR pSecurityDescriptor,
  225. IN OUT DWORD *pcbSecurityDescriptor
  226. );
  227. // Converts to UNICODE and uses RegOpenKeyExW
  228. DWORD MyRegOpenKeyEx(IN HKEY hRegKey,
  229. IN LPSTR pszKeyName,
  230. IN DWORD dwReserved,
  231. IN REGSAM SAMDesired,
  232. OUT HKEY *phNewRegKey);
  233. // Converts to UNICODE and uses RegDeleteKeyW
  234. DWORD MyRegDeleteKey(IN HKEY hRegKey,
  235. IN LPSTR pszKeyName);
  236. DWORD AllocAndSetLocationBuff(
  237. BOOL fMachineKeySet,
  238. DWORD dwProvType,
  239. CONST char *pszUserID,
  240. HKEY *phTopRegKey,
  241. TCHAR **ppszLocBuff,
  242. BOOL fUserKeys,
  243. BOOL *pfLeaveOldKeys,
  244. LPDWORD pcbBuff);
  245. //
  246. // Enumerates the old machine keys in the file system
  247. // keys were in this location in Beta 2 and Beta 3 of NT5/Win2K
  248. //
  249. DWORD EnumOldMachineKeys(
  250. IN DWORD dwProvType,
  251. IN OUT PKEY_CONTAINER_INFO pContInfo
  252. );
  253. DWORD GetNextEnumedOldMachKeys(
  254. IN PKEY_CONTAINER_INFO pContInfo,
  255. IN BOOL fMachineKeyset,
  256. OUT BYTE *pbData,
  257. OUT DWORD *pcbData
  258. );
  259. //
  260. // Enumerates the keys in the registry into a list of entries
  261. //
  262. DWORD EnumRegKeys(
  263. IN OUT PKEY_CONTAINER_INFO pContInfo,
  264. IN BOOL fMachineKeySet,
  265. IN DWORD dwProvType,
  266. OUT BYTE *pbData,
  267. IN OUT DWORD *pcbData
  268. );
  269. DWORD GetNextEnumedRegKeys(
  270. IN PKEY_CONTAINER_INFO pContInfo,
  271. OUT BYTE *pbData,
  272. OUT DWORD *pcbData
  273. );
  274. //+ ===========================================================================
  275. //
  276. // The function adjusts the token priviledges so that SACL information
  277. // may be gotten and then opens the indicated registry key. If the token
  278. // priviledges may be set then the reg key is opened anyway but the
  279. // flags field will not have the PRIVILEDGE_FOR_SACL value set.
  280. //
  281. //- ============================================================================
  282. DWORD OpenRegKeyWithTokenPriviledges(
  283. IN HKEY hTopRegKey,
  284. IN LPSTR pszRegKey,
  285. OUT HKEY *phRegKey,
  286. OUT DWORD *pdwFlags);
  287. DWORD LoadStrings();
  288. void UnloadStrings();
  289. typedef struct _EXPO_OFFLOAD_STRUCT {
  290. DWORD dwVersion;
  291. HMODULE hInst;
  292. PFN_OFFLOAD_MOD_EXPO pExpoFunc;
  293. } EXPO_OFFLOAD_STRUCT, *PEXPO_OFFLOAD_STRUCT;
  294. //
  295. // Function : FreeOffloadInfo
  296. //
  297. // Description : The function takes a pointer to Offload Information as the
  298. // first parameter of the call. The function frees the
  299. // information.
  300. //
  301. void FreeOffloadInfo(
  302. IN OUT PEXPO_OFFLOAD_STRUCT pOffloadInfo
  303. );
  304. //
  305. // Function : InitExpOffloadInfo
  306. //
  307. // Description : The function takes a pointer to Offload Information as the
  308. // first parameter of the call. The function checks in the
  309. // registry to see if an offload module has been registered.
  310. // If a module is registered then it loads the module
  311. // and gets the OffloadModExpo function pointer.
  312. //
  313. BOOL InitExpOffloadInfo(
  314. IN OUT PEXPO_OFFLOAD_STRUCT *ppExpoOffloadInfo
  315. );
  316. //
  317. // Function : ModularExpOffload
  318. //
  319. // Description : This function does the offloading of modular exponentiation.
  320. // The function takes a pointer to Offload Information as the
  321. // first parameter of the call. If this pointer is not NULL
  322. // then the function will use this module and call the function.
  323. // The exponentiation with MOD function will implement
  324. // Y^X MOD P where Y is the buffer pbBase, X is the buffer
  325. // pbExpo and P is the buffer pbModulus. The length of the
  326. // buffer pbExpo is cbExpo and the length of pbBase and
  327. // pbModulus is cbModulus. The resulting value is output
  328. // in the pbResult buffer and has length cbModulus.
  329. // The pReserved and dwFlags parameters are currently ignored.
  330. // If any of these functions fail then the function fails and
  331. // returns FALSE. If successful then the function returns
  332. // TRUE. If the function fails then most likely the caller
  333. // should fall back to using hard linked modular exponentiation.
  334. //
  335. BOOL ModularExpOffload(
  336. IN PEXPO_OFFLOAD_STRUCT pOffloadInfo,
  337. IN BYTE *pbBase,
  338. IN BYTE *pbExpo,
  339. IN DWORD cbExpo,
  340. IN BYTE *pbModulus,
  341. IN DWORD cbModulus,
  342. OUT BYTE *pbResult,
  343. IN VOID *pReserved,
  344. IN DWORD dwFlags
  345. );
  346. #ifdef USE_HW_RNG
  347. #ifdef _M_IX86
  348. // stuff for INTEL RNG usage
  349. //
  350. // Function : GetRNGDriverHandle
  351. //
  352. // Description : Gets the handle to the INTEL RNG driver if available, then
  353. // checks if the chipset supports the hardware RNG. If so
  354. // the previous driver handle is closed if necessary and the
  355. // new handle is assigned to the passed in parameter.
  356. //
  357. extern DWORD
  358. GetRNGDriverHandle(
  359. IN OUT HANDLE *phDriver);
  360. //
  361. // Function : CheckIfRNGAvailable
  362. //
  363. // Description : Checks if the INTEL RNG driver is available, if so then
  364. // checks if the chipset supports the hardware RNG.
  365. //
  366. extern DWORD
  367. CheckIfRNGAvailable(
  368. void);
  369. //
  370. // Function : HWRNGGenRandom
  371. //
  372. // Description : Uses the passed in handle to the INTEL RNG driver
  373. // to fill the buffer with random bits. Actually uses
  374. // XOR to fill the buffer so that the passed in buffer
  375. // is also mixed in.
  376. //
  377. DWORD
  378. HWRNGGenRandom(
  379. IN HANDLE hRNGDriver,
  380. IN OUT BYTE *pbBuffer,
  381. IN DWORD dwLen);
  382. #ifdef TEST_HW_RNG
  383. //
  384. // Function : SetupHWRNGIfRegistered
  385. //
  386. // Description : Checks if there is a registry setting indicating the HW RNG
  387. // is to be used. If the registry entry is there then it attempts
  388. // to get the HW RNG driver handle.
  389. //
  390. extern DWORD
  391. SetupHWRNGIfRegistered(
  392. OUT HANDLE *phRNGDriver);
  393. #endif // TEST_HW_RNG
  394. #endif // _M_IX86
  395. #endif // USE_HW_RNG
  396. //
  397. // Function for managing Force High Key Protection
  398. //
  399. BOOL IsForceHighProtectionEnabled(
  400. IN PKEY_CONTAINER_INFO pContInfo);
  401. DWORD InitializeForceHighProtection(
  402. IN OUT PKEY_CONTAINER_INFO pContInfo);
  403. //
  404. // Functions for managing cached private keys.
  405. //
  406. BOOL IsPrivateKeyCachingEnabled(
  407. IN PKEY_CONTAINER_INFO pContInfo);
  408. BOOL IsCachedKeyValid(
  409. IN PKEY_CONTAINER_INFO pContInfo,
  410. IN BOOL fSigKey);
  411. DWORD SetCachedKeyTimestamp(
  412. IN PKEY_CONTAINER_INFO pContInfo,
  413. IN BOOL fSigKey);
  414. DWORD InitializeKeyCacheInfo(
  415. IN OUT PKEY_CONTAINER_INFO pContInfo);
  416. #ifdef __cplusplus
  417. }
  418. #endif
  419. #endif // __CONTMAN_H__