Source code of Windows XP (NT5)
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.

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