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.

396 lines
8.6 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Abstract:
  4. This module contains the common header information for the EFS
  5. file system filter driver.
  6. Author:
  7. Robert Gu (robertg) 29-Oct-1996
  8. Enviroment:
  9. Kernel Mode Only
  10. Revision History:
  11. --*/
  12. #ifndef EFS_H
  13. #define EFS_H
  14. #include "ntifs.h"
  15. //
  16. // BYTE is required by des.h
  17. // PBYTE is required by des3.h
  18. //
  19. typedef unsigned char BYTE;
  20. typedef unsigned long DWORD;
  21. typedef unsigned char *PBYTE;
  22. #include "fipsapi.h"
  23. //#include "des.h"
  24. //#include "tripldes.h"
  25. #include "aes.h"
  26. #include "ntfsexp.h"
  27. #include "efsstruc.h"
  28. #if DBG
  29. #define EFSTRACEALL 0x00000001
  30. #define EFSTRACELIGHT 0x00000002
  31. #define EFSTRACEMED 0x00000004
  32. #define EFSSTOPALL 0x00000010
  33. #define EFSSTOPLIGHT 0x00000020
  34. #define EFSSTOPMED 0x00000040
  35. #endif // DBG
  36. #ifndef CALG_DES
  37. //
  38. // Definition from sdk\inc\wincrypt.h
  39. // Including wincrypt.h causes too much work.
  40. //
  41. #define ALG_CLASS_DATA_ENCRYPT (3 << 13)
  42. #define ALG_TYPE_BLOCK (3 << 9)
  43. #define ALG_SID_DES 1
  44. #define ALG_SID_3DES 3
  45. #define ALG_SID_DESX 4
  46. #define ALG_SID_AES_256 16
  47. #define ALG_SID_AES 17
  48. #define CALG_DES (ALG_CLASS_DATA_ENCRYPT|ALG_TYPE_BLOCK|ALG_SID_DES)
  49. #define CALG_DESX (ALG_CLASS_DATA_ENCRYPT|ALG_TYPE_BLOCK|ALG_SID_DESX)
  50. #define CALG_3DES (ALG_CLASS_DATA_ENCRYPT|ALG_TYPE_BLOCK|ALG_SID_3DES)
  51. #define CALG_AES_256 (ALG_CLASS_DATA_ENCRYPT|ALG_TYPE_BLOCK|ALG_SID_AES_256)
  52. #define CALG_AES (ALG_CLASS_DATA_ENCRYPT|ALG_TYPE_BLOCK|ALG_SID_AES)
  53. #endif
  54. //
  55. // Define the device extension structure for this driver's extensions.
  56. //
  57. #define EFSFILTER_DEVICE_TYPE 0x1309
  58. #define EFS_EVENTDEPTH 3
  59. #define EFS_CONTEXTDEPTH 5
  60. #define EFS_KEYDEPTH 30
  61. #define EFS_ALGDEPTH 3
  62. //
  63. // Define the constants used in Open Cache
  64. //
  65. #define DefaultTimeExpirePeriod 5 * 10000000 // 5 seconds
  66. #define MINCACHEPERIOD 2
  67. #define MAXCACHEPERIOD 30
  68. #define EFS_CACHEDEPTH 5
  69. #define EFS_STREAM_NORMAL 0
  70. #define EFS_STREAM_TRANSITION 1
  71. #define EFS_STRNAME_LENGTH 6
  72. #define EFS_FSCTL_HEADER_LENGTH 3 * sizeof( ULONG )
  73. //
  74. // Define test MACRO
  75. //
  76. #define CheckValidKeyBlock(PContext, Msg)
  77. /*
  78. #define CheckValidKeyBlock(PContext, Msg) { \
  79. if (PContext) { \
  80. if (((PKEY_BLOB) PContext)->KeyLength != DESX_KEY_BLOB_LENGTH){ \
  81. DbgPrint(Msg); \
  82. } \
  83. ASSERT(((PKEY_BLOB) PContext)->KeyLength == DESX_KEY_BLOB_LENGTH); \
  84. } \
  85. }
  86. */
  87. #define FreeMemoryBlock(PContext) { \
  88. RtlSecureZeroMemory(&(((PKEY_BLOB)(*PContext))->Key[0]), ((PKEY_BLOB)(*PContext))->KeyLength - KEYBLOB_HEAD_LENGTH); \
  89. ExFreeToNPagedLookasideList(((PKEY_BLOB)(*PContext))->MemSource, *PContext); \
  90. *PContext = NULL; \
  91. }
  92. /*
  93. #define FreeMemoryBlock(PContext) { \
  94. PNPAGED_LOOKASIDE_LIST MemSource; \
  95. MemSource = ((PKEY_BLOB)(*PContext))->MemSource; \
  96. RtlFillMemory(*PContext, DESX_KEY_BLOB_LENGTH, 0x45);\
  97. ExFreeToNPagedLookasideList(MemSource, *PContext); \
  98. *PContext = NULL; \
  99. }
  100. */
  101. typedef CSHORT NODE_TYPE_CODE, *PNODE_TYPE_CODE;
  102. typedef CSHORT NODE_BYTE_SIZE, *PNODE_BYTE_SIZE;
  103. #define NTC_UNDEFINED ((NODE_TYPE_CODE)0x0000)
  104. #define EFS_NTC_DATA_HEADER ((NODE_TYPE_CODE)0x0E04)
  105. #define KEYBLOB_HEAD_LENGTH (2 * sizeof(ULONG) + sizeof(PNPAGED_LOOKASIDE_LIST))
  106. #define DES_KEY_BLOB_LENGTH (KEYBLOB_HEAD_LENGTH + DES_TABLESIZE)
  107. #define DESX_KEY_BLOB_LENGTH (KEYBLOB_HEAD_LENGTH + DESX_TABLESIZE)
  108. #define DES3_KEY_BLOB_LENGTH (KEYBLOB_HEAD_LENGTH + DES3_TABLESIZE)
  109. #define AES_KEY_BLOB_LENGTH_256 (KEYBLOB_HEAD_LENGTH + AES_TABLESIZE_256)
  110. //
  111. // EFS device object extension
  112. //
  113. typedef struct _DEVICE_EXTENSION {
  114. CSHORT Type;
  115. CSHORT Size;
  116. PDEVICE_OBJECT FileSystemDeviceObject;
  117. PDEVICE_OBJECT RealDeviceObject;
  118. BOOLEAN Attached;
  119. } DEVICE_EXTENSION, *PDEVICE_EXTENSION;
  120. //
  121. // EFS context block. Attached to CREATE Irp
  122. //
  123. typedef struct _EFS_CONTEXT {
  124. //
  125. // Status information
  126. //
  127. ULONG Status;
  128. ULONG Flags;
  129. PVOID EfsStreamData;
  130. KEVENT FinishEvent;
  131. } EFS_CONTEXT, *PEFS_CONTEXT;
  132. //
  133. // The keyBlob.
  134. //
  135. typedef struct _KEY_BLOB {
  136. ULONG KeyLength;
  137. //
  138. // Indicate what kind of encryption used
  139. //
  140. ULONG AlgorithmID;
  141. //
  142. // Where the memory comes from
  143. //
  144. PNPAGED_LOOKASIDE_LIST MemSource;
  145. UCHAR Key[1];
  146. } KEY_BLOB, *PKEY_BLOB;
  147. typedef struct _KEY_BLOB_RAMPOOL {
  148. ULONG AlgorithmID;
  149. PNPAGED_LOOKASIDE_LIST MemSourceList;
  150. LIST_ENTRY MemSourceChain;
  151. } KEY_BLOB_RAMPOOL, *PKEY_BLOB_RAMPOOL;
  152. //
  153. // EFS Open Cache Node
  154. //
  155. typedef struct _OPEN_CACHE {
  156. GUID EfsId;
  157. PTOKEN_USER UserId;
  158. LARGE_INTEGER TimeStamp;
  159. LIST_ENTRY CacheChain;
  160. } OPEN_CACHE, *POPEN_CACHE;
  161. //
  162. // The EFS_DATA keeps global data in the EFS file system in-memory
  163. // This structure must be allocated from non-paged pool.
  164. //
  165. typedef struct _EFS_DATA {
  166. //
  167. // The type and size of this record (must be EFS_NTC_DATA_HEADER)
  168. //
  169. NODE_TYPE_CODE NodeTypeCode;
  170. NODE_BYTE_SIZE NodeByteSize;
  171. DWORD EfsDriverCacheLength; // Cache valid length 2 - 30 seconds
  172. //
  173. // A Lookaside List for event object
  174. // The event object are used in synchronization.
  175. //
  176. NPAGED_LOOKASIDE_LIST EfsEventPool;
  177. //
  178. // A Lookaside List for EFS context
  179. // The EFS context is used in Create Irp.
  180. //
  181. NPAGED_LOOKASIDE_LIST EfsContextPool;
  182. //
  183. // A lookaside list for open operation cache
  184. //
  185. PAGED_LOOKASIDE_LIST EfsOpenCachePool;
  186. LIST_ENTRY EfsOpenCacheList;
  187. FAST_MUTEX EfsOpenCacheMutex;
  188. //
  189. // Lookaside Lists for key blob
  190. //
  191. LIST_ENTRY EfsKeyLookAsideList;
  192. FAST_MUTEX EfsKeyBlobMemSrcMutex;
  193. PAGED_LOOKASIDE_LIST EfsMemSourceItem;
  194. NPAGED_LOOKASIDE_LIST EfsLookAside;
  195. //
  196. // Session key.
  197. // Used to decrypt the FSCTL input buffer.
  198. //
  199. UCHAR SessionKey[DES_KEYSIZE];
  200. UCHAR SessionDesTable[DES_TABLESIZE];
  201. PRKPROCESS LsaProcess;
  202. //
  203. // Flag indicate EFS is ready
  204. //
  205. BOOLEAN EfsInitialized;
  206. BOOLEAN AllocMaxBuffer;
  207. HANDLE InitEventHandle;
  208. //PDEVICE_OBJECT FipsDeviceObject;
  209. PFILE_OBJECT FipsFileObject;
  210. FIPS_FUNCTION_TABLE FipsFunctionTable;
  211. //
  212. // Efs special attribute name
  213. //
  214. UNICODE_STRING EfsName;
  215. } EFS_DATA, *PEFS_DATA;
  216. //
  217. // This macro returns TRUE if a flag in a set of flags is on and FALSE
  218. // otherwise
  219. //
  220. //#ifndef BooleanFlagOn
  221. //#define BooleanFlagOn(F,SF) ( \
  222. // (BOOLEAN)(((F) & (SF)) != 0) \
  223. //)
  224. //#endif
  225. //#ifndef SetFlag
  226. //#define SetFlag(Flags,SingleFlag) { \
  227. // (Flags) |= (SingleFlag); \
  228. //}
  229. //#endif
  230. //#ifndef ClearFlag
  231. //#define ClearFlag(Flags,SingleFlag) { \
  232. // (Flags) &= ~(SingleFlag); \
  233. //}
  234. //#endif
  235. //
  236. // Function prototypes
  237. //
  238. //
  239. // Define driver entry routine.
  240. //
  241. NTSTATUS
  242. EfsInitialization(
  243. void
  244. );
  245. NTSTATUS
  246. EFSCreate(
  247. IN PDEVICE_OBJECT DeviceObject,
  248. IN PIRP Irp,
  249. IN PFILE_OBJECT FileObject
  250. );
  251. DWORD
  252. GetKeyBlobLength(
  253. ULONG AlgID
  254. );
  255. PKEY_BLOB
  256. GetKeyBlobBuffer(
  257. ULONG AlgID
  258. );
  259. BOOLEAN
  260. SetKeyTable(
  261. PKEY_BLOB KeyBlob,
  262. PEFS_KEY EfsKey
  263. );
  264. NTSTATUS
  265. EFSFsControl(
  266. IN PDEVICE_OBJECT DeviceObject,
  267. IN PIRP Irp,
  268. IN PFILE_OBJECT FileObject
  269. );
  270. NTSTATUS
  271. EFSPostCreate(
  272. IN PDEVICE_OBJECT DeviceObject,
  273. IN PIRP Irp,
  274. IN PEFS_CONTEXT EfsContext,
  275. IN ULONG OpenType
  276. );
  277. NTSTATUS
  278. EFSFilePostCreate(
  279. IN PDEVICE_OBJECT VolDo,
  280. IN PIRP Irp,
  281. IN PFILE_OBJECT FileObject,
  282. IN NTSTATUS Status,
  283. IN OUT PVOID *PCreateContext
  284. );
  285. VOID
  286. EfsGetSessionKey(
  287. IN PVOID StartContext
  288. );
  289. BOOLEAN
  290. EfsInitFips(
  291. VOID
  292. );
  293. NTKERNELAPI
  294. NTSTATUS
  295. PsLookupProcessByProcessId(
  296. IN HANDLE ProcessId,
  297. OUT PEPROCESS *Process
  298. );
  299. #endif