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.

535 lines
9.9 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. secdat.hxx
  5. Abstract:
  6. This module contains the declarations for the ADM_SECURE_DATA class.
  7. Author:
  8. Keith Moore (keithmo) 17-Feb-1997
  9. Revision History:
  10. --*/
  11. #ifndef _SECDAT_HXX_
  12. #define _SECDAT_HXX_
  13. #include <reftrace.h>
  14. #include <seccom.hxx>
  15. class ADM_GUID_MAP {
  16. public:
  17. //
  18. // Usual constructor/destructor stuff.
  19. //
  20. ADM_GUID_MAP(
  21. IN IUnknown * Object,
  22. IN GUID guidServer);
  23. ~ADM_GUID_MAP();
  24. //
  25. // Static initializers.
  26. //
  27. static
  28. VOID
  29. Initialize( VOID );
  30. static
  31. VOID
  32. Terminate( VOID );
  33. //
  34. // Find and reference the ADM_SECURE_DATA object associatd with the
  35. // given object; create if not found.
  36. //
  37. static
  38. ADM_GUID_MAP *
  39. FindAndReferenceGuidMap(
  40. IN IUnknown * Object );
  41. //
  42. // Reference/dereference methods.
  43. //
  44. VOID
  45. Reference() {
  46. LONG result = InterlockedIncrement( &m_ReferenceCount );
  47. #if DBG
  48. if( sm_RefTraceLog != NULL ) {
  49. WriteRefTraceLog(
  50. sm_RefTraceLog,
  51. result,
  52. (PVOID)this
  53. );
  54. }
  55. #endif
  56. return (VOID)result;
  57. }
  58. VOID
  59. Dereference() {
  60. LONG result = InterlockedDecrement( &m_ReferenceCount );
  61. #if DBG
  62. if( sm_RefTraceLog != NULL ) {
  63. WriteRefTraceLog(
  64. sm_RefTraceLog,
  65. result,
  66. (PVOID)this
  67. );
  68. }
  69. #endif
  70. if( result == 0 ) {
  71. delete this;
  72. }
  73. }
  74. GUID GetGuid() {
  75. return m_guidServer;
  76. }
  77. private:
  78. //
  79. // Global list of ADM_GUID_MAP objects.
  80. //
  81. static LIST_ENTRY sm_GuidMapListHead;
  82. //
  83. // Lock protecting the global list.
  84. //
  85. static CRITICAL_SECTION sm_GuidMapDataLock;
  86. #if DBG
  87. static DWORD sm_GuidMapLockOwner;
  88. static PTRACE_LOG sm_RefTraceLog;
  89. #endif
  90. //
  91. // This object's links onto the global list.
  92. //
  93. LIST_ENTRY m_GuidMapListEntry;
  94. //
  95. // Pointer to the associated object.
  96. //
  97. IUnknown * m_Object;
  98. //
  99. // This object's reference count.
  100. //
  101. LONG m_ReferenceCount;
  102. //
  103. // A lock protecting this object.
  104. //
  105. CRITICAL_SECTION m_ObjectLock;
  106. //
  107. // Server's GUID
  108. //
  109. GUID m_guidServer;
  110. //
  111. // Lock manipulators.
  112. //
  113. static
  114. VOID
  115. AcquireDataLock() {
  116. EnterCriticalSection( &sm_GuidMapDataLock );
  117. #if DBG
  118. sm_GuidMapLockOwner = GetCurrentThreadId();
  119. #endif
  120. }
  121. static
  122. VOID
  123. ReleaseDataLock() {
  124. #if DBG
  125. sm_GuidMapLockOwner = 0;
  126. #endif
  127. LeaveCriticalSection( &sm_GuidMapDataLock );
  128. }
  129. VOID
  130. LockThis() {
  131. EnterCriticalSection( &m_ObjectLock );
  132. }
  133. VOID
  134. UnlockThis() {
  135. LeaveCriticalSection( &m_ObjectLock );
  136. }
  137. };
  138. //
  139. // We need to associate some data with each client-side DCOM object. The
  140. // following class will manage this data.
  141. //
  142. class ADM_SECURE_DATA {
  143. public:
  144. //
  145. // Usual constructor/destructor stuff.
  146. //
  147. ADM_SECURE_DATA(
  148. IN IUnknown * Object,
  149. IN GUID guidServer,
  150. IN BOOL bServer);
  151. ~ADM_SECURE_DATA();
  152. //
  153. // Static initializers.
  154. //
  155. static
  156. BOOL
  157. Initialize(
  158. HINSTANCE hDll
  159. );
  160. static
  161. VOID
  162. Terminate();
  163. //
  164. // Find and reference the ADM_SECURE_DATA object associatd with the
  165. // given object; create if not found.
  166. //
  167. static
  168. ADM_SECURE_DATA *
  169. FindAndReferenceServerSecureData(
  170. IN IUnknown * Object,
  171. IN BOOL CreateIfNotFound
  172. );
  173. static
  174. ADM_SECURE_DATA *
  175. FindAndReferenceClientSecureData(
  176. IN IUnknown * Object );
  177. static
  178. ADM_SECURE_DATA *
  179. FindAndReferenceClientSecureData(
  180. IN ADM_GUID_MAP *pguidmapRelated );
  181. static
  182. ADM_SECURE_DATA *
  183. FindOrAddAndReferenceClientSecureData(
  184. IN IUnknown * Object );
  185. //
  186. // Query the send and receive data encryption objects.
  187. //
  188. HRESULT
  189. GetClientSendCryptoStorage(
  190. OUT IIS_CRYPTO_STORAGE ** SendCryptoStorage,
  191. IUnknown * Object
  192. );
  193. HRESULT
  194. GetClientReceiveCryptoStorage(
  195. OUT IIS_CRYPTO_STORAGE ** ReceiveCryptoStorage,
  196. IUnknown * Object
  197. );
  198. HRESULT
  199. GetServerSendCryptoStorage(
  200. OUT IIS_CRYPTO_STORAGE ** SendCryptoStorage
  201. );
  202. HRESULT
  203. GetServerReceiveCryptoStorage(
  204. OUT IIS_CRYPTO_STORAGE ** ReceiveCryptoStorage
  205. );
  206. //
  207. // Reference/dereference methods.
  208. //
  209. VOID
  210. Reference() {
  211. LONG result = InterlockedIncrement( &m_ReferenceCount );
  212. #if DBG
  213. if( sm_RefTraceLog != NULL ) {
  214. WriteRefTraceLog(
  215. sm_RefTraceLog,
  216. result,
  217. (PVOID)this
  218. );
  219. }
  220. #endif
  221. return (VOID)result;
  222. }
  223. VOID
  224. Dereference() {
  225. LONG result = InterlockedDecrement( &m_ReferenceCount );
  226. #if DBG
  227. if( sm_RefTraceLog != NULL ) {
  228. WriteRefTraceLog(
  229. sm_RefTraceLog,
  230. result,
  231. (PVOID)this
  232. );
  233. }
  234. #endif
  235. if( result == 0 ) {
  236. delete this;
  237. }
  238. }
  239. //
  240. // Server-side key exchange.
  241. //
  242. HRESULT
  243. DoServerSideKeyExchangePhase1(
  244. IN PIIS_CRYPTO_BLOB pClientKeyExchangeKeyBlob,
  245. IN PIIS_CRYPTO_BLOB pClientSignatureKeyBlob,
  246. OUT PIIS_CRYPTO_BLOB * ppServerKeyExchangeKeyBlob,
  247. OUT PIIS_CRYPTO_BLOB * ppServerSignatureKeyBlob,
  248. OUT PIIS_CRYPTO_BLOB * ppServerSessionKeyBlob
  249. );
  250. HRESULT
  251. DoServerSideKeyExchangePhase2(
  252. IN PIIS_CRYPTO_BLOB pClientSessionKeyBlob,
  253. IN PIIS_CRYPTO_BLOB pClientHashBlob,
  254. OUT PIIS_CRYPTO_BLOB * ppServerHashBlob
  255. );
  256. GUID
  257. GetGuid()
  258. {
  259. return m_guidServer;
  260. }
  261. private:
  262. //
  263. // Global list of ADM_SECURE_DATA objects.
  264. //
  265. static LIST_ENTRY sm_ServerSecureDataListHead;
  266. static LIST_ENTRY sm_ClientSecureDataListHead;
  267. //
  268. // Lock protecting the global list.
  269. //
  270. static CRITICAL_SECTION sm_ServerSecureDataLock;
  271. static CRITICAL_SECTION sm_ClientSecureDataLock;
  272. #if DBG
  273. static DWORD sm_ServerSecureDataLockOwner;
  274. static DWORD sm_ClientSecureDataLockOwner;
  275. static PTRACE_LOG sm_RefTraceLog;
  276. #endif
  277. //
  278. // Cached crypto providers. We only open these once, to avoid
  279. // the huge amount of DLL thrashing that takes place when a
  280. // provider is opened.
  281. //
  282. static HCRYPTPROV sm_ServerCryptoProvider;
  283. static HCRYPTPROV sm_ClientCryptoProvider;
  284. //
  285. // This object's links onto the global list.
  286. //
  287. LIST_ENTRY m_SecureDataListEntry;
  288. //
  289. // Pointer to the associated object.
  290. //
  291. IUnknown * m_Object;
  292. //
  293. // This object's reference count.
  294. //
  295. LONG m_ReferenceCount;
  296. //
  297. // Crypto stuff.
  298. //
  299. IIS_CRYPTO_EXCHANGE_CLIENT * m_KeyExchangeClient;
  300. IIS_CRYPTO_EXCHANGE_SERVER * m_KeyExchangeServer;
  301. IIS_CRYPTO_STORAGE * m_SendCryptoStorage;
  302. IIS_CRYPTO_STORAGE * m_ReceiveCryptoStorage;
  303. //
  304. // A lock protecting this object.
  305. //
  306. CRITICAL_SECTION m_ObjectLock;
  307. //
  308. // Server's CLSID
  309. //
  310. GUID m_guidServer;
  311. BOOL m_bIsServer;
  312. //
  313. // Lock manipulators.
  314. //
  315. static
  316. VOID
  317. AcquireServerDataLock() {
  318. EnterCriticalSection( &sm_ServerSecureDataLock );
  319. #if DBG
  320. sm_ServerSecureDataLockOwner = GetCurrentThreadId();
  321. #endif
  322. }
  323. static
  324. VOID
  325. AcquireClientDataLock() {
  326. EnterCriticalSection( &sm_ClientSecureDataLock );
  327. #if DBG
  328. sm_ClientSecureDataLockOwner = GetCurrentThreadId();
  329. #endif
  330. }
  331. static
  332. VOID
  333. ReleaseServerDataLock() {
  334. #if DBG
  335. sm_ServerSecureDataLockOwner = 0;
  336. #endif
  337. LeaveCriticalSection( &sm_ServerSecureDataLock );
  338. }
  339. static
  340. VOID
  341. ReleaseClientDataLock() {
  342. #if DBG
  343. sm_ClientSecureDataLockOwner = 0;
  344. #endif
  345. LeaveCriticalSection( &sm_ClientSecureDataLock );
  346. }
  347. VOID
  348. LockThis() {
  349. EnterCriticalSection( &m_ObjectLock );
  350. }
  351. VOID
  352. UnlockThis() {
  353. LeaveCriticalSection( &m_ObjectLock );
  354. }
  355. //
  356. // Crypto provider accessors.
  357. //
  358. HRESULT
  359. GetCryptoProviderHelper(
  360. OUT HCRYPTPROV * UserProvider,
  361. OUT HCRYPTPROV * CachedProvider,
  362. IN LPTSTR ContainerName,
  363. IN DWORD CryptoFlags
  364. );
  365. HRESULT
  366. GetServerCryptoProvider(
  367. OUT HCRYPTPROV * Provider
  368. )
  369. {
  370. if( sm_ServerCryptoProvider != CRYPT_NULL ) {
  371. *Provider = sm_ServerCryptoProvider;
  372. return NO_ERROR;
  373. }
  374. return GetCryptoProviderHelper(
  375. Provider,
  376. &sm_ServerCryptoProvider,
  377. DCOM_SERVER_CONTAINER,
  378. CRYPT_MACHINE_KEYSET
  379. );
  380. }
  381. HRESULT
  382. GetClientCryptoProvider(
  383. OUT HCRYPTPROV * Provider
  384. );
  385. HRESULT
  386. GenerateNameForContainer(
  387. IN OUT PCHAR pszContainerName,
  388. IN OUT DWORD dwBufferLen
  389. );
  390. HRESULT
  391. GetTextualSid(
  392. PSID pSid,
  393. LPTSTR TextualSid,
  394. LPDWORD cchSidSize
  395. );
  396. VOID
  397. CleanupCryptoData();
  398. //
  399. // Key exchange engine.
  400. //
  401. HRESULT
  402. DoClientSideKeyExchange(
  403. IUnknown * Object
  404. );
  405. };
  406. #endif // _SECDAT_HXX_