ntlm.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**
  2. * WinPR: Windows Portable Runtime
  3. * NTLM Utils
  4. *
  5. * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include <winpr/ntlm.h>
  23. #include <winpr/crt.h>
  24. #include <winpr/crypto.h>
  25. /**
  26. * Define NTOWFv1(Password, User, Domain) as
  27. * MD4(UNICODE(Password))
  28. * EndDefine
  29. */
  30. BOOL NTOWFv1W(LPWSTR Password, UINT32 PasswordLength, BYTE* NtHash)
  31. {
  32. if (!Password || !NtHash)
  33. return FALSE;
  34. if (!winpr_Digest(WINPR_MD_MD4, (BYTE*)Password, (size_t)PasswordLength, NtHash,
  35. WINPR_MD4_DIGEST_LENGTH))
  36. return FALSE;
  37. return TRUE;
  38. }
  39. BOOL NTOWFv1A(LPSTR Password, UINT32 PasswordLength, BYTE* NtHash)
  40. {
  41. LPWSTR PasswordW = NULL;
  42. BOOL result = FALSE;
  43. if (!NtHash)
  44. return FALSE;
  45. if (!(PasswordW = (LPWSTR)calloc(PasswordLength, 2)))
  46. return FALSE;
  47. MultiByteToWideChar(CP_ACP, 0, Password, PasswordLength, PasswordW, PasswordLength);
  48. if (!NTOWFv1W(PasswordW, PasswordLength * 2, NtHash))
  49. goto out_fail;
  50. result = TRUE;
  51. out_fail:
  52. free(PasswordW);
  53. return result;
  54. }
  55. /**
  56. * Define NTOWFv2(Password, User, Domain) as
  57. * HMAC_MD5(MD4(UNICODE(Password)),
  58. * UNICODE(ConcatenationOf(UpperCase(User), Domain)))
  59. * EndDefine
  60. */
  61. BOOL NTOWFv2W(LPWSTR Password, UINT32 PasswordLength, LPWSTR User, UINT32 UserLength, LPWSTR Domain,
  62. UINT32 DomainLength, BYTE* NtHash)
  63. {
  64. BYTE* buffer;
  65. BYTE NtHashV1[16];
  66. BOOL result = FALSE;
  67. if ((!User) || (!Password) || (!NtHash))
  68. return FALSE;
  69. if (!NTOWFv1W(Password, PasswordLength, NtHashV1))
  70. return FALSE;
  71. if (!(buffer = (BYTE*)malloc(UserLength + DomainLength)))
  72. return FALSE;
  73. /* Concatenate(UpperCase(User), Domain) */
  74. CopyMemory(buffer, User, UserLength);
  75. CharUpperBuffW((LPWSTR)buffer, UserLength / 2);
  76. CopyMemory(&buffer[UserLength], Domain, DomainLength);
  77. /* Compute the HMAC-MD5 hash of the above value using the NTLMv1 hash as the key, the result is
  78. * the NTLMv2 hash */
  79. if (!winpr_HMAC(WINPR_MD_MD5, NtHashV1, 16, buffer, UserLength + DomainLength, NtHash,
  80. WINPR_MD4_DIGEST_LENGTH))
  81. goto out_fail;
  82. result = TRUE;
  83. out_fail:
  84. free(buffer);
  85. return result;
  86. }
  87. BOOL NTOWFv2A(LPSTR Password, UINT32 PasswordLength, LPSTR User, UINT32 UserLength, LPSTR Domain,
  88. UINT32 DomainLength, BYTE* NtHash)
  89. {
  90. LPWSTR UserW = NULL;
  91. LPWSTR DomainW = NULL;
  92. LPWSTR PasswordW = NULL;
  93. BOOL result = FALSE;
  94. if (!NtHash)
  95. return FALSE;
  96. UserW = (LPWSTR)calloc(UserLength, 2);
  97. DomainW = (LPWSTR)calloc(DomainLength, 2);
  98. PasswordW = (LPWSTR)calloc(PasswordLength, 2);
  99. if (!UserW || !DomainW || !PasswordW)
  100. goto out_fail;
  101. MultiByteToWideChar(CP_ACP, 0, User, UserLength, UserW, UserLength);
  102. MultiByteToWideChar(CP_ACP, 0, Domain, DomainLength, DomainW, DomainLength);
  103. MultiByteToWideChar(CP_ACP, 0, Password, PasswordLength, PasswordW, PasswordLength);
  104. if (!NTOWFv2W(PasswordW, PasswordLength * 2, UserW, UserLength * 2, DomainW, DomainLength * 2,
  105. NtHash))
  106. goto out_fail;
  107. result = TRUE;
  108. out_fail:
  109. free(UserW);
  110. free(DomainW);
  111. free(PasswordW);
  112. return result;
  113. }
  114. BOOL NTOWFv2FromHashW(BYTE* NtHashV1, LPWSTR User, UINT32 UserLength, LPWSTR Domain,
  115. UINT32 DomainLength, BYTE* NtHash)
  116. {
  117. BYTE* buffer;
  118. BYTE result = FALSE;
  119. if (!User || !NtHash)
  120. return FALSE;
  121. if (!(buffer = (BYTE*)malloc(UserLength + DomainLength)))
  122. return FALSE;
  123. /* Concatenate(UpperCase(User), Domain) */
  124. CopyMemory(buffer, User, UserLength);
  125. CharUpperBuffW((LPWSTR)buffer, UserLength / 2);
  126. if (DomainLength > 0)
  127. {
  128. CopyMemory(&buffer[UserLength], Domain, DomainLength);
  129. }
  130. /* Compute the HMAC-MD5 hash of the above value using the NTLMv1 hash as the key, the result is
  131. * the NTLMv2 hash */
  132. if (!winpr_HMAC(WINPR_MD_MD5, NtHashV1, 16, buffer, UserLength + DomainLength, NtHash,
  133. WINPR_MD4_DIGEST_LENGTH))
  134. goto out_fail;
  135. result = TRUE;
  136. out_fail:
  137. free(buffer);
  138. return result;
  139. }
  140. BOOL NTOWFv2FromHashA(BYTE* NtHashV1, LPSTR User, UINT32 UserLength, LPSTR Domain,
  141. UINT32 DomainLength, BYTE* NtHash)
  142. {
  143. LPWSTR UserW = NULL;
  144. LPWSTR DomainW = NULL;
  145. BOOL result = FALSE;
  146. if (!NtHash)
  147. return FALSE;
  148. UserW = (LPWSTR)calloc(UserLength, 2);
  149. DomainW = (LPWSTR)calloc(DomainLength, 2);
  150. if (!UserW || !DomainW)
  151. goto out_fail;
  152. MultiByteToWideChar(CP_ACP, 0, User, UserLength, UserW, UserLength);
  153. MultiByteToWideChar(CP_ACP, 0, Domain, DomainLength, DomainW, DomainLength);
  154. if (!NTOWFv2FromHashW(NtHashV1, UserW, UserLength * 2, DomainW, DomainLength * 2, NtHash))
  155. goto out_fail;
  156. result = TRUE;
  157. out_fail:
  158. free(UserW);
  159. free(DomainW);
  160. return result;
  161. }