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.

528 lines
13 KiB

  1. //+-----------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (c) Microsoft Corporation 1992 - 1996
  6. //
  7. // File: ntlmutil.cxx
  8. //
  9. // Contents: Utility functions for NtLm package:
  10. // NtLmDuplicateUnicodeString
  11. // NtLmDuplicateSid
  12. // NtLmAllocate
  13. // NtLmFree
  14. //
  15. //
  16. // History: ChandanS 25-Jul-1996 Stolen from kerberos\client2\kerbutil.cxx
  17. //
  18. //------------------------------------------------------------------------
  19. #include <global.h>
  20. //+-------------------------------------------------------------------------
  21. //
  22. // Function: NtLmDuplicateUnicodeString
  23. //
  24. // Synopsis: Duplicates a UNICODE_STRING. If the source string buffer is
  25. // NULL the destionation will be too.
  26. //
  27. // Effects: allocates memory with LsaFunctions.AllocateLsaHeap
  28. //
  29. // Arguments: DestinationString - Receives a copy of the source string
  30. // SourceString - String to copy
  31. //
  32. // Requires:
  33. //
  34. // Returns: SEC_E_OK - the copy succeeded
  35. // SEC_E_INSUFFICIENT_MEMORY - the call to allocate
  36. // memory failed.
  37. //
  38. // Notes:
  39. //
  40. //
  41. //--------------------------------------------------------------------------
  42. NTSTATUS
  43. NtLmDuplicateUnicodeString(
  44. OUT PUNICODE_STRING DestinationString,
  45. IN OPTIONAL PUNICODE_STRING SourceString
  46. )
  47. {
  48. SspPrint((SSP_MISC, "Entering NtLmDuplicateUnicodeString\n"));
  49. NTSTATUS Status = STATUS_SUCCESS;
  50. DestinationString->Buffer = NULL;
  51. DestinationString->Length =
  52. DestinationString->MaximumLength =
  53. 0;
  54. if ((ARGUMENT_PRESENT(SourceString)) &&
  55. (SourceString->Buffer != NULL))
  56. {
  57. DestinationString->Buffer = (LPWSTR) NtLmAllocatePrivateHeap(
  58. SourceString->Length + sizeof(WCHAR));
  59. if (DestinationString->Buffer != NULL)
  60. {
  61. DestinationString->Length = SourceString->Length;
  62. DestinationString->MaximumLength = SourceString->Length + sizeof(WCHAR);
  63. RtlCopyMemory(
  64. DestinationString->Buffer,
  65. SourceString->Buffer,
  66. SourceString->Length
  67. );
  68. DestinationString->Buffer[SourceString->Length/sizeof(WCHAR)] = L'\0';
  69. }
  70. else
  71. {
  72. Status = STATUS_NO_MEMORY;
  73. SspPrint((SSP_CRITICAL, "NtLmDuplicateUnicodeString, NtLmAllocate returns NULL\n"));
  74. goto CleanUp;
  75. }
  76. }
  77. CleanUp:
  78. SspPrint((SSP_MISC, "Leaving NtLmDuplicateUnicodeString\n"));
  79. return(Status);
  80. }
  81. //+-------------------------------------------------------------------------
  82. //
  83. // Function: NtLmDuplicateString
  84. //
  85. // Synopsis: Duplicates a STRING. If the source string buffer is
  86. // NULL the destionation will be too.
  87. //
  88. // Effects: allocates memory with LsaFunctions.AllocatePrivateHeap
  89. //
  90. // Arguments: DestinationString - Receives a copy of the source string
  91. // SourceString - String to copy
  92. //
  93. // Requires:
  94. //
  95. // Returns: SEC_E_OK - the copy succeeded
  96. // SEC_E_INSUFFICIENT_MEMORY - the call to allocate
  97. // memory failed.
  98. //
  99. // Notes:
  100. //
  101. //
  102. //--------------------------------------------------------------------------
  103. NTSTATUS
  104. NtLmDuplicateString(
  105. OUT PSTRING DestinationString,
  106. IN OPTIONAL PSTRING SourceString
  107. )
  108. {
  109. SspPrint((SSP_MISC, "Entering NtLmDuplicateString\n"));
  110. NTSTATUS Status = STATUS_SUCCESS;
  111. DestinationString->Buffer = NULL;
  112. DestinationString->Length =
  113. DestinationString->MaximumLength =
  114. 0;
  115. if ((ARGUMENT_PRESENT(SourceString)) &&
  116. (SourceString->Buffer != NULL))
  117. {
  118. DestinationString->Buffer = (LPSTR) NtLmAllocatePrivateHeap(
  119. SourceString->Length + sizeof(CHAR));
  120. if (DestinationString->Buffer != NULL)
  121. {
  122. DestinationString->Length = SourceString->Length;
  123. DestinationString->MaximumLength = SourceString->Length + sizeof(CHAR);
  124. RtlCopyMemory(
  125. DestinationString->Buffer,
  126. SourceString->Buffer,
  127. SourceString->Length
  128. );
  129. DestinationString->Buffer[SourceString->Length/sizeof(CHAR)] = '\0';
  130. }
  131. else
  132. {
  133. Status = STATUS_NO_MEMORY;
  134. SspPrint((SSP_CRITICAL, "NtLmDuplicateString, NtLmAllocate returns NULL\n"));
  135. goto CleanUp;
  136. }
  137. }
  138. CleanUp:
  139. SspPrint((SSP_MISC, "Leaving NtLmDuplicateString\n"));
  140. return(Status);
  141. }
  142. //+-------------------------------------------------------------------------
  143. //
  144. // Function: NtLmDuplicatePassword
  145. //
  146. // Synopsis: Duplicates a UNICODE_STRING. If the source string buffer is
  147. // NULL the destionation will be too. The MaximumLength contains
  148. // room for encryption padding data.
  149. //
  150. // Effects: allocates memory with LsaFunctions.AllocatePrivateHeap
  151. //
  152. // Arguments: DestinationString - Receives a copy of the source string
  153. // SourceString - String to copy
  154. //
  155. // Requires:
  156. //
  157. // Returns: SEC_E_OK - the copy succeeded
  158. // SEC_E_INSUFFICIENT_MEMORY - the call to allocate
  159. // memory failed.
  160. //
  161. // Notes:
  162. //
  163. //
  164. //--------------------------------------------------------------------------
  165. NTSTATUS
  166. NtLmDuplicatePassword(
  167. OUT PUNICODE_STRING DestinationString,
  168. IN OPTIONAL PUNICODE_STRING SourceString
  169. )
  170. {
  171. SspPrint((SSP_MISC, "Entering NtLmDuplicatePassword\n"));
  172. NTSTATUS Status = STATUS_SUCCESS;
  173. DestinationString->Buffer = NULL;
  174. DestinationString->Length =
  175. DestinationString->MaximumLength =
  176. 0;
  177. if ((ARGUMENT_PRESENT(SourceString)) &&
  178. (SourceString->Buffer != NULL))
  179. {
  180. USHORT PaddingLength;
  181. PaddingLength = DESX_BLOCKLEN - (SourceString->Length % DESX_BLOCKLEN);
  182. if( PaddingLength == DESX_BLOCKLEN )
  183. {
  184. PaddingLength = 0;
  185. }
  186. DestinationString->Buffer = (LPWSTR) NtLmAllocatePrivateHeap(
  187. SourceString->Length +
  188. PaddingLength
  189. );
  190. if (DestinationString->Buffer != NULL)
  191. {
  192. DestinationString->Length = SourceString->Length;
  193. DestinationString->MaximumLength = SourceString->Length + PaddingLength;
  194. if( DestinationString->MaximumLength == SourceString->MaximumLength )
  195. {
  196. //
  197. // duplicating an already padded buffer -- pickup the original
  198. // pad.
  199. //
  200. RtlCopyMemory(
  201. DestinationString->Buffer,
  202. SourceString->Buffer,
  203. SourceString->MaximumLength
  204. );
  205. } else {
  206. //
  207. // duplicating an unpadded buffer -- pickup only the string
  208. // and fill the rest with the boot time pad.
  209. //
  210. RtlCopyMemory(
  211. DestinationString->Buffer,
  212. SourceString->Buffer,
  213. SourceString->Length
  214. );
  215. //
  216. // CRYPTNOTE: consideration for ultra-paranoid:
  217. // use a boot time random pad for fill.
  218. //
  219. }
  220. }
  221. else
  222. {
  223. Status = STATUS_NO_MEMORY;
  224. SspPrint((SSP_CRITICAL, "NtLmDuplicatePassword, NtLmAllocate returns NULL\n"));
  225. }
  226. }
  227. SspPrint((SSP_MISC, "Leaving NtLmDuplicatePassword\n"));
  228. return(Status);
  229. }
  230. //+-------------------------------------------------------------------------
  231. //
  232. // Function: NtLmDuplicateSid
  233. //
  234. // Synopsis: Duplicates a SID
  235. //
  236. // Effects: allocates memory with LsaFunctions.AllocatePrivateHeap
  237. //
  238. // Arguments: DestinationSid - Receives a copy of the SourceSid
  239. // SourceSid - SID to copy
  240. //
  241. // Requires:
  242. //
  243. // Returns: STATUS_SUCCESS - the copy succeeded
  244. // STATUS_INSUFFICIENT_RESOURCES - the call to allocate memory
  245. // failed
  246. //
  247. // Notes:
  248. //
  249. //
  250. //--------------------------------------------------------------------------
  251. NTSTATUS
  252. NtLmDuplicateSid(
  253. OUT PSID * DestinationSid,
  254. IN PSID SourceSid
  255. )
  256. {
  257. SspPrint((SSP_MISC, "Entering NtLmDuplicateSid\n"));
  258. NTSTATUS Status = STATUS_SUCCESS;
  259. ULONG SidSize;
  260. ASSERT(RtlValidSid(SourceSid));
  261. SidSize = RtlLengthSid(SourceSid);
  262. *DestinationSid = (PSID) NtLmAllocatePrivateHeap( SidSize );
  263. if (ARGUMENT_PRESENT(*DestinationSid))
  264. {
  265. RtlCopyMemory(
  266. *DestinationSid,
  267. SourceSid,
  268. SidSize
  269. );
  270. }
  271. else
  272. {
  273. Status = STATUS_INSUFFICIENT_RESOURCES;
  274. SspPrint((SSP_CRITICAL, "NtLmDuplicateSid, NtLmAllocate returns NULL\n"));
  275. goto CleanUp;
  276. }
  277. CleanUp:
  278. SspPrint((SSP_MISC, "Leaving NtLmDuplicateSid\n"));
  279. return(Status);
  280. }
  281. //+-------------------------------------------------------------------------
  282. //
  283. // Function: NtLmAllocate
  284. //
  285. // Synopsis: Allocate memory in either lsa mode or user mode
  286. //
  287. // Effects: Allocated chunk is zeroed out
  288. //
  289. // Arguments:
  290. //
  291. // Requires:
  292. //
  293. // Returns:
  294. //
  295. // Notes:
  296. //
  297. //
  298. //--------------------------------------------------------------------------
  299. PVOID
  300. NtLmAllocate(
  301. IN SIZE_T BufferSize
  302. )
  303. {
  304. PVOID Buffer;
  305. if (NtLmState == NtLmLsaMode)
  306. {
  307. ASSERT ((LsaFunctions->AllocatePrivateHeap));
  308. Buffer = LsaFunctions->AllocatePrivateHeap(BufferSize);
  309. // note: Lsa allocator zeroes the memory for us.
  310. }
  311. else
  312. {
  313. ASSERT(NtLmState == NtLmUserMode);
  314. Buffer = LocalAlloc(LPTR, BufferSize);
  315. // Buffer = CryptMemoryAlloc(BufferSize);
  316. }
  317. return Buffer;
  318. }
  319. //+-------------------------------------------------------------------------
  320. //
  321. // Function: NtLmFree
  322. //
  323. // Synopsis: Free memory in either lsa mode or user mode
  324. //
  325. // Effects:
  326. //
  327. // Arguments:
  328. //
  329. // Requires:
  330. //
  331. // Returns:
  332. //
  333. // Notes:
  334. //
  335. //
  336. //--------------------------------------------------------------------------
  337. VOID
  338. NtLmFree(
  339. IN PVOID Buffer
  340. )
  341. {
  342. if (ARGUMENT_PRESENT(Buffer))
  343. {
  344. if (NtLmState == NtLmLsaMode)
  345. {
  346. ASSERT ((LsaFunctions->FreePrivateHeap));
  347. LsaFunctions->FreePrivateHeap(Buffer);
  348. }
  349. else
  350. {
  351. ASSERT(NtLmState == NtLmUserMode);
  352. LocalFree(Buffer);
  353. // CryptMemoryFree(Buffer);
  354. }
  355. }
  356. }
  357. PVOID
  358. NtLmAllocateLsaHeap(
  359. IN ULONG BufferSize // should update to SIZE_T once lsa allocator def updated
  360. )
  361. {
  362. PVOID Buffer;
  363. if (NtLmState == NtLmLsaMode)
  364. {
  365. ASSERT ((LsaFunctions->AllocateLsaHeap));
  366. Buffer = LsaFunctions->AllocateLsaHeap(BufferSize);
  367. // note: Lsa allocator zeroes the memory for us.
  368. }
  369. else
  370. {
  371. ASSERT(NtLmState == NtLmUserMode);
  372. Buffer = LocalAlloc(LPTR, BufferSize);
  373. // Buffer = CryptMemoryAlloc(BufferSize);
  374. }
  375. return Buffer;
  376. }
  377. VOID
  378. NtLmFreeLsaHeap(
  379. IN PVOID Buffer
  380. )
  381. {
  382. if (ARGUMENT_PRESENT(Buffer))
  383. {
  384. if (NtLmState == NtLmLsaMode)
  385. {
  386. ASSERT ((LsaFunctions->FreeLsaHeap));
  387. LsaFunctions->FreeLsaHeap(Buffer);
  388. }
  389. else
  390. {
  391. ASSERT(NtLmState == NtLmUserMode);
  392. LocalFree(Buffer);
  393. // CryptMemoryFree(Buffer);
  394. }
  395. }
  396. }
  397. #if DBG
  398. PVOID
  399. NtLmAllocatePrivateHeap(
  400. IN SIZE_T BufferSize
  401. )
  402. {
  403. ASSERT (NtLmState == NtLmLsaMode);
  404. ASSERT ((LsaFunctions->AllocatePrivateHeap));
  405. // note: Lsa allocator zeroes the memory for us.
  406. return LsaFunctions->AllocatePrivateHeap(BufferSize);
  407. }
  408. VOID
  409. NtLmFreePrivateHeap(
  410. IN PVOID Buffer
  411. )
  412. {
  413. ASSERT (NtLmState == NtLmLsaMode);
  414. ASSERT ((LsaFunctions->FreePrivateHeap));
  415. if (ARGUMENT_PRESENT(Buffer))
  416. {
  417. LsaFunctions->FreePrivateHeap(Buffer);
  418. }
  419. }
  420. PVOID
  421. I_NtLmAllocate(
  422. IN SIZE_T BufferSize
  423. )
  424. {
  425. ASSERT (NtLmState == NtLmLsaMode);
  426. ASSERT ((Lsa.AllocatePrivateHeap));
  427. // note: Lsa allocator zeroes the memory for us.
  428. return Lsa.AllocatePrivateHeap(BufferSize);
  429. }
  430. VOID
  431. I_NtLmFree(
  432. IN PVOID Buffer
  433. )
  434. {
  435. ASSERT (NtLmState == NtLmLsaMode);
  436. ASSERT ((Lsa.FreePrivateHeap));
  437. if (ARGUMENT_PRESENT(Buffer))
  438. {
  439. Lsa.FreePrivateHeap(Buffer);
  440. }
  441. }
  442. #endif