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.

270 lines
9.2 KiB

  1. /*++
  2. Copyright (c) 1996, 1997 Microsoft Corporation
  3. Module Name:
  4. keyman.h
  5. Abstract:
  6. This module contains routines to manage master keys in the cliet. This
  7. includes retrieval, backup and restore.
  8. Author:
  9. Scott Field (sfield) 09-Sep-97
  10. --*/
  11. #ifndef __KEYMAN_H__
  12. #define __KEYMAN_H__
  13. #if 0
  14. The layout of the registry is as follows:
  15. HKEY_CURRENT_LUSER\...\Cryptography\Protect\<UserId>\
  16. note: Protect key and all subkeys Acl'd for Local System access
  17. MasterKeys\
  18. Policy = (REG_DWORD value), policy bits of master keys; eg, don't backup, local only backup, local + DC recovery)
  19. Preferred = (REG_BINARY value containing MASTERKEY_PREFERRED_INFO),
  20. Indicates the GUID of the preferred master key, and
  21. when the key expires.
  22. <GUID> = (Subkey) Textual form of a master key, identified by GUID.
  23. LK = (REG_BINARY data), randomly generated local backup key, created if policy permits. [obfuscated]
  24. MK = (REG_BINARY data), master key data, encrypted with logon credential (WinNT) or obfuscated (Win95)
  25. BK = (REG_BINARY data), master key data, encrypted with LK, if policy permits.
  26. BBK = (REG_BINARY data), master key data, encrypted with LK and DC recovery key, if policy permits.
  27. <GUID...> = any number of additional subkeys representing master keys and associated data.
  28. #endif // 0
  29. #define REGVAL_PREFERRED_MK L"Preferred"
  30. #define REGVAL_POLICY_MK L"ProtectionPolicy"
  31. #define REGVAL_MK_DEFAULT_ITERATION_COUNT L"MasterKeyIterationCount"
  32. #define REGVAL_MK_LEGACY_COMPLIANCE L"MasterKeyLegacyCompliance"
  33. #define REGVAL_MK_LEGACY_NT4_DOMAIN L"MasterKeyLegacyNt4Domain"
  34. #define REGVAL_DISTRIBUTE_BACKUP_KEY L"DistributeBackupKey"
  35. // MasterKeys\<GUID>\<value>
  36. #define REGVAL_MASTER_KEY 0 // L"MK" // masterkey, encrypted with user credential
  37. #define REGVAL_LOCAL_KEY 1 // L"LK" // phase one backup blob encryption key
  38. #define REGVAL_BACKUP_LCL_KEY 2 // L"BK" // phase one backup blob
  39. #define REGVAL_BACKUP_DC_KEY 3 // L"BBK" // phase two backup blob
  40. #define MK_DISP_OK 0 // normal disposition, no backup/restore occured
  41. #define MK_DISP_BCK_LCL 1 // local backup/restore took place
  42. #define MK_DISP_BCK_DC 2 // DC based backup/restore took place
  43. #define MK_DISP_STORAGE_ERR 3 // error retrieving key from storage
  44. #define MK_DISP_DELEGATION_ERR 4 // Recovery failure because delegation disabled
  45. #define MK_DISP_UNKNOWN_ERR 5 // unknown error
  46. // Policy bit for local only (no DC) backup
  47. #define POLICY_LOCAL_BACKUP 0x1
  48. // Policy bit for NO backup (Win95)
  49. #define POLICY_NO_BACKUP 0x2
  50. // Use the DPAPI One way function of the password (SHA_1(pw))
  51. #define POLICY_DPAPI_OWF 0x4
  52. #define MASTERKEY_MATERIAL_SIZE (64) // size of the masterkey key material
  53. #define LOCALKEY_MATERIAL_SIZE (32) // size of the localkey key material
  54. #define MASTERKEY_R2_LEN (16)
  55. #define MASTERKEY_R3_LEN (16)
  56. #define DEFAULT_MASTERKEY_ITERATION_COUNT (4000) // 4000 == ~100ms on 400 MHz machine
  57. //
  58. // the MASTERKEY_STORED structure depicts all the data that may be associated
  59. // with a single master key entity.
  60. //
  61. typedef struct {
  62. DWORD dwVersion;
  63. BOOL fModified; // have contents been modified, deeming a persist operation?
  64. LPWSTR szFilePath; // path (not including filename) to the file for persist operation
  65. WCHAR wszguidMasterKey[MAX_GUID_SZ_CHARS]; // filename (GUID based)
  66. DWORD dwPolicy; // policy bits on this key
  67. DWORD cbMK; // count of bytes associated with pbMK (Zero if not present)
  68. PBYTE pbMK; // MasterKey data. NULL if not present
  69. DWORD cbLK; // count of bytes associated with pbLK (Zero if not present)
  70. PBYTE pbLK; // LocalKey data. NULL if not present
  71. DWORD cbBK; // count of bytes associated with pbBK (Zero if not present)
  72. PBYTE pbBK; // BackupLocalKey data. NULL if not present
  73. DWORD cbBBK; // count of bytes associated with pbBBK (Zero if not present)
  74. PBYTE pbBBK; // BackupDCKey data. NULL if not present
  75. } MASTERKEY_STORED, *PMASTERKEY_STORED, *LPMASTERKEY_STORED;
  76. //
  77. // the on-disk version of the structure is neccessary to allow 64bit and 32bit
  78. // platform interop with upgraded systems or roaming files.
  79. // pointers are changed to 32bit offsets
  80. //
  81. typedef struct {
  82. DWORD dwVersion;
  83. BOOL fModified; // have contents been modified, deeming a persist operation?
  84. DWORD szFilePath; // invalid on disk
  85. WCHAR wszguidMasterKey[MAX_GUID_SZ_CHARS]; // filename (GUID based)
  86. DWORD dwPolicy; // policy bits on this key
  87. DWORD cbMK; // count of bytes associated with pbMK (Zero if not present)
  88. DWORD pbMK; // invalid on disk
  89. DWORD cbLK; // count of bytes associated with pbLK (Zero if not present)
  90. DWORD pbLK; // invalid on disk
  91. DWORD cbBK; // count of bytes associated with pbBK (Zero if not present)
  92. DWORD pbBK; // invalid on disk
  93. DWORD cbBBK; // count of bytes associated with pbBBK (Zero if not present)
  94. DWORD pbBBK; // invalid on disk
  95. } MASTERKEY_STORED_ON_DISK, *PMASTERKEY_STORED_ON_DISK, *LPMASTERKEY_STORED_ON_DISK;
  96. //
  97. // VERSION1: LK is not encrypted with LSA Secret when POLICY_LOCAL_BACKUP is set
  98. // VERSION2: LK is encrypted with LSA Secret when POLICY_LOCAL_BACKUP is set
  99. //#define MASTERKEY_STORED_VERSION 1
  100. #define MASTERKEY_STORED_VERSION 2
  101. typedef struct {
  102. DWORD dwVersion; // version of structure (MASTERKEY_BLOB_VERSION)
  103. BYTE R2[MASTERKEY_R2_LEN]; // random data used during HMAC to derive symetric key
  104. DWORD IterationCount; // PKCS5 iteration count
  105. DWORD KEYGENAlg; // PKCS5 Key Generation Alg, in CAPI ALG_ID form
  106. DWORD EncryptionAlg; // Encryption Alg, in CAPI ALG_ID form
  107. } MASTERKEY_BLOB, *PMASTERKEY_BLOB, *LPMASTERKEY_BLOB;
  108. typedef struct {
  109. BYTE R3[MASTERKEY_R3_LEN]; // random data used to derive MAC key
  110. BYTE MAC[A_SHA_DIGEST_LEN]; // MAC(R3, pbMasterKey)
  111. DWORD Padding; // Padding to make masterkey inner blob divisable by
  112. // 3DES_BLOCKLEN
  113. } MASTERKEY_INNER_BLOB, *PMASTERKEY_INNER_BLOB, *LPMASTERKEY_INNER_BLOB;
  114. typedef struct {
  115. DWORD dwVersion; // version of structure MASTERKEY_BLOB_LOCALKEY_BACKUP
  116. GUID CredentialID; // indicates the credential id used to protect the
  117. // master key.
  118. } LOCAL_BACKUP_DATA, *PLOCAL_BACKUP_DATA, *LPLOCAL_BACKUP_DATA;
  119. //
  120. // 90 day masterkey expiration
  121. //
  122. #define MASTERKEY_EXPIRES_DAYS (90)
  123. typedef struct {
  124. GUID guidPreferredKey;
  125. FILETIME ftPreferredKeyExpires;
  126. } MASTERKEY_PREFERRED_INFO, *PMASTERKEY_PREFERRED_INFO, *LPMASTERKEY_PREFERRED_INFO;
  127. //
  128. // deferred backup structure.
  129. //
  130. typedef struct {
  131. DWORD cbSize; // sizeof(QUEUED_BACKUP)
  132. MASTERKEY_STORED hMasterKey;
  133. HANDLE hToken; // client access token
  134. PBYTE pbLocalKey;
  135. DWORD cbLocalKey;
  136. PBYTE pbMasterKey;
  137. DWORD cbMasterKey;
  138. HANDLE hEventThread; // Event that signals thread finished processing
  139. HANDLE hEventSuccess; // Event signalled indicates thread did successful backup
  140. } QUEUED_BACKUP, *PQUEUED_BACKUP, *LPQUEUED_BACKUP;
  141. //
  142. // deferred key sync structure.
  143. //
  144. typedef struct {
  145. DWORD cbSize; // sizeof(QUEUED_SYNC)
  146. PVOID pvContext; // duplicated server context
  147. } QUEUED_SYNC, *PQUEUED_SYNC, *LPQUEUED_SYNC;
  148. DWORD
  149. GetSpecifiedMasterKey(
  150. IN PVOID pvContext, // server context
  151. IN OUT GUID *pguidMasterKey,
  152. OUT LPBYTE *ppbMasterKey,
  153. OUT DWORD *pcbMasterKey,
  154. IN BOOL fSpecified // get specified pguidMasterKey key ?
  155. );
  156. DWORD
  157. InitiateSynchronizeMasterKeys(
  158. IN PVOID pvContext // server context
  159. );
  160. DWORD
  161. WINAPI
  162. SynchronizeMasterKeys(
  163. IN PVOID pvContext,
  164. IN DWORD dwFlags);
  165. VOID
  166. DPAPISynchronizeMasterKeys(
  167. IN HANDLE hUserToken);
  168. BOOL
  169. InitializeKeyManagement(
  170. VOID
  171. );
  172. BOOL
  173. TeardownKeyManagement(
  174. VOID
  175. );
  176. DWORD
  177. DpapiUpdateLsaSecret(
  178. IN PVOID pvContext);
  179. DWORD
  180. OpenFileInStorageArea(
  181. IN PVOID pvContext, // if NULL, caller is assumed to be impersonating
  182. IN DWORD dwDesiredAccess,
  183. IN LPCWSTR szUserStorageArea,
  184. IN LPCWSTR szFileName,
  185. IN OUT HANDLE *phFile
  186. );
  187. HANDLE
  188. CreateFileWithRetries(
  189. IN LPCWSTR lpFileName,
  190. IN DWORD dwDesiredAccess,
  191. IN DWORD dwShareMode,
  192. IN LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  193. IN DWORD dwCreationDisposition,
  194. IN DWORD dwFlagsAndAttributes,
  195. IN HANDLE hTemplateFile
  196. );
  197. #endif // __KEYMAN_H__