/src/FreeRDP/winpr/libwinpr/sspi/NTLM/ntlm_compute.c
Line | Count | Source |
1 | | /** |
2 | | * WinPR: Windows Portable Runtime |
3 | | * NTLM Security Package (Compute) |
4 | | * |
5 | | * Copyright 2011-2014 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 | | |
20 | | #include <winpr/config.h> |
21 | | |
22 | | #include <winpr/assert.h> |
23 | | |
24 | | #include "ntlm.h" |
25 | | #include "../sspi.h" |
26 | | |
27 | | #include <winpr/crt.h> |
28 | | #include <winpr/sam.h> |
29 | | #include <winpr/ntlm.h> |
30 | | #include <winpr/print.h> |
31 | | #include <winpr/crypto.h> |
32 | | #include <winpr/sysinfo.h> |
33 | | |
34 | | #include "ntlm_compute.h" |
35 | | |
36 | | #include "../../log.h" |
37 | 1.58k | #define TAG WINPR_TAG("sspi.NTLM") |
38 | | |
39 | | #define NTLM_CheckAndLogRequiredCapacity(tag, s, nmemb, what) \ |
40 | 0 | Stream_CheckAndLogRequiredCapacityEx(tag, WLOG_WARN, s, nmemb, 1, "%s(%s:%" PRIuz ") " what, \ |
41 | 0 | __func__, __FILE__, (size_t)__LINE__) |
42 | | |
43 | | static char NTLM_CLIENT_SIGN_MAGIC[] = "session key to client-to-server signing key magic constant"; |
44 | | static char NTLM_SERVER_SIGN_MAGIC[] = "session key to server-to-client signing key magic constant"; |
45 | | static char NTLM_CLIENT_SEAL_MAGIC[] = "session key to client-to-server sealing key magic constant"; |
46 | | static char NTLM_SERVER_SEAL_MAGIC[] = "session key to server-to-client sealing key magic constant"; |
47 | | |
48 | | static const BYTE NTLM_NULL_BUFFER[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
49 | | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
50 | | |
51 | | /** |
52 | | * Populate VERSION structure msdn{cc236654} |
53 | | * @param versionInfo A pointer to the version struct |
54 | | * |
55 | | * @return \b TRUE for success, \b FALSE for failure |
56 | | */ |
57 | | |
58 | | BOOL ntlm_get_version_info(NTLM_VERSION_INFO* versionInfo) |
59 | 1.59k | { |
60 | 1.59k | WINPR_ASSERT(versionInfo); |
61 | | |
62 | | #if defined(WITH_WINPR_DEPRECATED) |
63 | | OSVERSIONINFOA osVersionInfo = WINPR_C_ARRAY_INIT; |
64 | | osVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA); |
65 | | if (!GetVersionExA(&osVersionInfo)) |
66 | | return FALSE; |
67 | | versionInfo->ProductMajorVersion = (UINT8)osVersionInfo.dwMajorVersion; |
68 | | versionInfo->ProductMinorVersion = (UINT8)osVersionInfo.dwMinorVersion; |
69 | | versionInfo->ProductBuild = (UINT16)osVersionInfo.dwBuildNumber; |
70 | | #else |
71 | | /* Always return fixed version number. |
72 | | * |
73 | | * ProductVersion is fixed since windows 10 to Major 10, Minor 0 |
74 | | * ProductBuild taken from https://en.wikipedia.org/wiki/Windows_11_version_history |
75 | | * with most recent (pre) release build number |
76 | | */ |
77 | 1.59k | versionInfo->ProductMajorVersion = 10; |
78 | 1.59k | versionInfo->ProductMinorVersion = 0; |
79 | 1.59k | versionInfo->ProductBuild = 22631; |
80 | 1.59k | #endif |
81 | 1.59k | ZeroMemory(versionInfo->Reserved, sizeof(versionInfo->Reserved)); |
82 | 1.59k | versionInfo->NTLMRevisionCurrent = NTLMSSP_REVISION_W2K3; |
83 | 1.59k | return TRUE; |
84 | 1.59k | } |
85 | | |
86 | | /** |
87 | | * Read VERSION structure. msdn{cc236654} |
88 | | * @param s A pointer to a stream to read |
89 | | * @param versionInfo A pointer to the struct to read data to |
90 | | * |
91 | | * @return \b TRUE for success, \b FALSE for failure |
92 | | */ |
93 | | |
94 | | BOOL ntlm_read_version_info(wStream* s, NTLM_VERSION_INFO* versionInfo) |
95 | 775 | { |
96 | 775 | WINPR_ASSERT(s); |
97 | 775 | WINPR_ASSERT(versionInfo); |
98 | | |
99 | 775 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 8)) |
100 | 8 | return FALSE; |
101 | | |
102 | 767 | Stream_Read_UINT8(s, versionInfo->ProductMajorVersion); /* ProductMajorVersion (1 byte) */ |
103 | 767 | Stream_Read_UINT8(s, versionInfo->ProductMinorVersion); /* ProductMinorVersion (1 byte) */ |
104 | 767 | Stream_Read_UINT16(s, versionInfo->ProductBuild); /* ProductBuild (2 bytes) */ |
105 | 767 | Stream_Read(s, versionInfo->Reserved, sizeof(versionInfo->Reserved)); /* Reserved (3 bytes) */ |
106 | 767 | Stream_Read_UINT8(s, versionInfo->NTLMRevisionCurrent); /* NTLMRevisionCurrent (1 byte) */ |
107 | 767 | return TRUE; |
108 | 775 | } |
109 | | |
110 | | /** |
111 | | * Write VERSION structure. msdn{cc236654} |
112 | | * @param s A pointer to the stream to write to |
113 | | * @param versionInfo A pointer to the buffer to read the data from |
114 | | * |
115 | | * @return \b TRUE for success, \b FALSE for failure |
116 | | */ |
117 | | |
118 | | BOOL ntlm_write_version_info(wStream* s, const NTLM_VERSION_INFO* versionInfo) |
119 | 1.58k | { |
120 | 1.58k | WINPR_ASSERT(s); |
121 | 1.58k | WINPR_ASSERT(versionInfo); |
122 | | |
123 | 1.58k | if (!Stream_CheckAndLogRequiredCapacityEx( |
124 | 1.58k | TAG, WLOG_WARN, s, 5ull + sizeof(versionInfo->Reserved), 1ull, |
125 | 1.58k | "%s(%s:%" PRIuz ") NTLM_VERSION_INFO", __func__, __FILE__, (size_t)__LINE__)) |
126 | 0 | return FALSE; |
127 | | |
128 | 1.58k | Stream_Write_UINT8(s, versionInfo->ProductMajorVersion); /* ProductMajorVersion (1 byte) */ |
129 | 1.58k | Stream_Write_UINT8(s, versionInfo->ProductMinorVersion); /* ProductMinorVersion (1 byte) */ |
130 | 1.58k | Stream_Write_UINT16(s, versionInfo->ProductBuild); /* ProductBuild (2 bytes) */ |
131 | 1.58k | Stream_Write(s, versionInfo->Reserved, sizeof(versionInfo->Reserved)); /* Reserved (3 bytes) */ |
132 | 1.58k | Stream_Write_UINT8(s, versionInfo->NTLMRevisionCurrent); /* NTLMRevisionCurrent (1 byte) */ |
133 | 1.58k | return TRUE; |
134 | 1.58k | } |
135 | | |
136 | | /** |
137 | | * Print VERSION structure. msdn{cc236654} |
138 | | * @param versionInfo A pointer to the struct containing the data to print |
139 | | */ |
140 | | #ifdef WITH_DEBUG_NTLM |
141 | | void ntlm_print_version_info(const NTLM_VERSION_INFO* versionInfo) |
142 | | { |
143 | | WINPR_ASSERT(versionInfo); |
144 | | |
145 | | WLog_VRB(TAG, "VERSION ={"); |
146 | | WLog_VRB(TAG, "\tProductMajorVersion: %" PRIu8 "", versionInfo->ProductMajorVersion); |
147 | | WLog_VRB(TAG, "\tProductMinorVersion: %" PRIu8 "", versionInfo->ProductMinorVersion); |
148 | | WLog_VRB(TAG, "\tProductBuild: %" PRIu16 "", versionInfo->ProductBuild); |
149 | | WLog_VRB(TAG, "\tReserved: 0x%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "", versionInfo->Reserved[0], |
150 | | versionInfo->Reserved[1], versionInfo->Reserved[2]); |
151 | | WLog_VRB(TAG, "\tNTLMRevisionCurrent: 0x%02" PRIX8 "", versionInfo->NTLMRevisionCurrent); |
152 | | } |
153 | | #endif |
154 | | |
155 | | static BOOL ntlm_read_ntlm_v2_client_challenge(wStream* s, NTLMv2_CLIENT_CHALLENGE* challenge) |
156 | 200 | { |
157 | 200 | size_t size = 0; |
158 | 200 | WINPR_ASSERT(s); |
159 | 200 | WINPR_ASSERT(challenge); |
160 | | |
161 | 200 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 28)) |
162 | 3 | return FALSE; |
163 | | |
164 | 197 | Stream_Read_UINT8(s, challenge->RespType); |
165 | 197 | Stream_Read_UINT8(s, challenge->HiRespType); |
166 | 197 | Stream_Read_UINT16(s, challenge->Reserved1); |
167 | 197 | Stream_Read_UINT32(s, challenge->Reserved2); |
168 | 197 | Stream_Read(s, challenge->Timestamp, 8); |
169 | 197 | Stream_Read(s, challenge->ClientChallenge, 8); |
170 | 197 | Stream_Read_UINT32(s, challenge->Reserved3); |
171 | 197 | size = Stream_Length(s) - Stream_GetPosition(s); |
172 | | |
173 | 197 | if (size > UINT32_MAX) |
174 | 0 | { |
175 | 0 | WLog_ERR(TAG, "NTLMv2_CLIENT_CHALLENGE::cbAvPairs too large, got %" PRIuz "bytes", size); |
176 | 0 | return FALSE; |
177 | 0 | } |
178 | | |
179 | 197 | challenge->cbAvPairs = (UINT32)size; |
180 | 197 | challenge->AvPairs = (NTLM_AV_PAIR*)malloc(challenge->cbAvPairs); |
181 | | |
182 | 197 | if (!challenge->AvPairs) |
183 | 0 | { |
184 | 0 | WLog_ERR(TAG, "NTLMv2_CLIENT_CHALLENGE::AvPairs failed to allocate %" PRIu32 "bytes", |
185 | 0 | challenge->cbAvPairs); |
186 | 0 | return FALSE; |
187 | 0 | } |
188 | | |
189 | 197 | Stream_Read(s, challenge->AvPairs, size); |
190 | 197 | return TRUE; |
191 | 197 | } |
192 | | |
193 | | static BOOL ntlm_write_ntlm_v2_client_challenge(wStream* s, |
194 | | const NTLMv2_CLIENT_CHALLENGE* challenge) |
195 | 0 | { |
196 | 0 | ULONG length = 0; |
197 | |
|
198 | 0 | WINPR_ASSERT(s); |
199 | 0 | WINPR_ASSERT(challenge); |
200 | |
|
201 | 0 | if (!NTLM_CheckAndLogRequiredCapacity(TAG, s, 28, "NTLMv2_CLIENT_CHALLENGE")) |
202 | 0 | return FALSE; |
203 | | |
204 | 0 | Stream_Write_UINT8(s, challenge->RespType); |
205 | 0 | Stream_Write_UINT8(s, challenge->HiRespType); |
206 | 0 | Stream_Write_UINT16(s, challenge->Reserved1); |
207 | 0 | Stream_Write_UINT32(s, challenge->Reserved2); |
208 | 0 | Stream_Write(s, challenge->Timestamp, 8); |
209 | 0 | Stream_Write(s, challenge->ClientChallenge, 8); |
210 | 0 | Stream_Write_UINT32(s, challenge->Reserved3); |
211 | 0 | length = ntlm_av_pair_list_length(challenge->AvPairs, challenge->cbAvPairs); |
212 | |
|
213 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, length)) |
214 | 0 | return FALSE; |
215 | | |
216 | 0 | Stream_Write(s, challenge->AvPairs, length); |
217 | 0 | return TRUE; |
218 | 0 | } |
219 | | |
220 | | BOOL ntlm_read_ntlm_v2_response(wStream* s, NTLMv2_RESPONSE* response) |
221 | 203 | { |
222 | 203 | WINPR_ASSERT(s); |
223 | 203 | WINPR_ASSERT(response); |
224 | | |
225 | 203 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 16)) |
226 | 3 | return FALSE; |
227 | | |
228 | 200 | Stream_Read(s, response->Response, 16); |
229 | 200 | return ntlm_read_ntlm_v2_client_challenge(s, &(response->Challenge)); |
230 | 203 | } |
231 | | |
232 | | BOOL ntlm_write_ntlm_v2_response(wStream* s, const NTLMv2_RESPONSE* response) |
233 | 0 | { |
234 | 0 | WINPR_ASSERT(s); |
235 | 0 | WINPR_ASSERT(response); |
236 | |
|
237 | 0 | if (!NTLM_CheckAndLogRequiredCapacity(TAG, s, 16ull, "NTLMv2_RESPONSE")) |
238 | 0 | return FALSE; |
239 | | |
240 | 0 | Stream_Write(s, response->Response, 16); |
241 | 0 | return ntlm_write_ntlm_v2_client_challenge(s, &(response->Challenge)); |
242 | 0 | } |
243 | | |
244 | | /** |
245 | | * Get current time, in tenths of microseconds since midnight of January 1, 1601. |
246 | | * @param[out] timestamp 64-bit little-endian timestamp |
247 | | */ |
248 | | |
249 | | static void ntlm_current_time(BYTE* timestamp, WINPR_ATTR_UNUSED size_t size) |
250 | 589 | { |
251 | 589 | FILETIME ft = WINPR_C_ARRAY_INIT; |
252 | | |
253 | 589 | WINPR_ASSERT(timestamp); |
254 | 589 | WINPR_ASSERT(size >= sizeof(ft)); |
255 | | |
256 | 589 | GetSystemTimeAsFileTime(&ft); |
257 | 589 | CopyMemory(timestamp, &(ft), sizeof(ft)); |
258 | 589 | } |
259 | | |
260 | | /** |
261 | | * Generate timestamp for AUTHENTICATE_MESSAGE. |
262 | | * |
263 | | * @param context A pointer to the NTLM context |
264 | | */ |
265 | | |
266 | | void ntlm_generate_timestamp(NTLM_CONTEXT* context) |
267 | 596 | { |
268 | 596 | WINPR_ASSERT(context); |
269 | | |
270 | 596 | if (memcmp(context->ChallengeTimestamp, NTLM_NULL_BUFFER, 8) != 0) |
271 | 7 | CopyMemory(context->Timestamp, context->ChallengeTimestamp, 8); |
272 | 589 | else |
273 | 589 | ntlm_current_time(context->Timestamp, sizeof(context->Timestamp)); |
274 | 596 | } |
275 | | |
276 | | static BOOL ntlm_fetch_ntlm_v2_hash(NTLM_CONTEXT* context, BYTE* hash) |
277 | 0 | { |
278 | 0 | BOOL rc = FALSE; |
279 | 0 | WINPR_SAM_ENTRY* entry = nullptr; |
280 | |
|
281 | 0 | WINPR_ASSERT(context); |
282 | 0 | WINPR_ASSERT(hash); |
283 | |
|
284 | 0 | SSPI_CREDENTIALS* credentials = context->credentials; |
285 | 0 | WINPR_SAM* sam = SamOpen(context->SamFile, TRUE); |
286 | |
|
287 | 0 | if (!sam) |
288 | 0 | goto fail; |
289 | | |
290 | 0 | entry = SamLookupUserW( |
291 | 0 | sam, (LPWSTR)credentials->identity.User, credentials->identity.UserLength * sizeof(WCHAR), |
292 | 0 | (LPWSTR)credentials->identity.Domain, credentials->identity.DomainLength * sizeof(WCHAR)); |
293 | |
|
294 | 0 | if (!entry) |
295 | 0 | { |
296 | 0 | entry = SamLookupUserW(sam, (LPWSTR)credentials->identity.User, |
297 | 0 | credentials->identity.UserLength * sizeof(WCHAR), nullptr, 0); |
298 | 0 | } |
299 | |
|
300 | 0 | if (!entry) |
301 | 0 | goto fail; |
302 | | |
303 | | #ifdef WITH_DEBUG_NTLM |
304 | | WLog_VRB(TAG, "NTLM Hash:"); |
305 | | winpr_HexDump(TAG, WLOG_DEBUG, entry->NtHash, 16); |
306 | | #endif |
307 | 0 | rc = NTOWFv2FromHashW(entry->NtHash, (LPWSTR)credentials->identity.User, |
308 | 0 | credentials->identity.UserLength * sizeof(WCHAR), |
309 | 0 | (LPWSTR)credentials->identity.Domain, |
310 | 0 | credentials->identity.DomainLength * sizeof(WCHAR), hash); |
311 | |
|
312 | 0 | fail: |
313 | 0 | SamFreeEntry(sam, entry); |
314 | 0 | SamClose(sam); |
315 | 0 | if (!rc) |
316 | 0 | WLog_ERR(TAG, "Error: Could not find user in SAM database"); |
317 | |
|
318 | 0 | return rc; |
319 | 0 | } |
320 | | |
321 | | static int hexchar2nibble(WCHAR wc) |
322 | 0 | { |
323 | | #if defined(__BIG_ENDIAN__) |
324 | | union |
325 | | { |
326 | | BYTE b[2]; |
327 | | WCHAR w; |
328 | | } cnv; |
329 | | cnv.w = wc; |
330 | | const BYTE b = cnv.b[0]; |
331 | | cnv.b[0] = cnv.b[1]; |
332 | | cnv.b[1] = b; |
333 | | wc = cnv.w; |
334 | | #endif |
335 | |
|
336 | 0 | switch (wc) |
337 | 0 | { |
338 | 0 | case L'0': |
339 | 0 | case L'1': |
340 | 0 | case L'2': |
341 | 0 | case L'3': |
342 | 0 | case L'4': |
343 | 0 | case L'5': |
344 | 0 | case L'6': |
345 | 0 | case L'7': |
346 | 0 | case L'8': |
347 | 0 | case L'9': |
348 | 0 | return wc - L'0'; |
349 | 0 | case L'a': |
350 | 0 | case L'b': |
351 | 0 | case L'c': |
352 | 0 | case L'd': |
353 | 0 | case L'e': |
354 | 0 | case L'f': |
355 | 0 | return wc - L'a' + 10; |
356 | 0 | case L'A': |
357 | 0 | case L'B': |
358 | 0 | case L'C': |
359 | 0 | case L'D': |
360 | 0 | case L'E': |
361 | 0 | case L'F': |
362 | 0 | return wc - L'A' + 10; |
363 | 0 | default: |
364 | 0 | return -1; |
365 | 0 | } |
366 | 0 | } |
367 | | static int ntlm_convert_password_hash(NTLM_CONTEXT* context, BYTE* hash, size_t hashlen) |
368 | 0 | { |
369 | 0 | const size_t required_len = 2ull * hashlen; |
370 | |
|
371 | 0 | WINPR_ASSERT(context); |
372 | 0 | WINPR_ASSERT(hash); |
373 | |
|
374 | 0 | SSPI_CREDENTIALS* credentials = context->credentials; |
375 | | /* Password contains a password hash of length (PasswordLength - |
376 | | * SSPI_CREDENTIALS_HASH_LENGTH_OFFSET) */ |
377 | 0 | const ULONG PasswordHashLength = credentials->identity.PasswordLength - |
378 | 0 | /* Macro [globalScope] */ SSPI_CREDENTIALS_HASH_LENGTH_OFFSET; |
379 | |
|
380 | 0 | if (PasswordHashLength != required_len) |
381 | 0 | { |
382 | 0 | WLog_ERR(TAG, |
383 | 0 | "PasswordHash has invalid length %" PRIu32 " must be exactly %" PRIuz " bytes", |
384 | 0 | PasswordHashLength, required_len); |
385 | 0 | return -1; |
386 | 0 | } |
387 | | |
388 | 0 | const WCHAR* PasswordHash = credentials->identity.Password; |
389 | 0 | for (size_t x = 0; x < hashlen; x++) |
390 | 0 | { |
391 | 0 | const int hi = hexchar2nibble(PasswordHash[2 * x]); |
392 | 0 | if (hi < 0) |
393 | 0 | { |
394 | 0 | WLog_ERR(TAG, "PasswordHash has an invalid value at position %" PRIuz, 2 * x); |
395 | 0 | return -1; |
396 | 0 | } |
397 | 0 | const int lo = hexchar2nibble(PasswordHash[2 * x + 1]); |
398 | 0 | if (lo < 0) |
399 | 0 | { |
400 | 0 | WLog_ERR(TAG, "PasswordHash has an invalid value at position %" PRIuz, 2 * x + 1); |
401 | 0 | return -1; |
402 | 0 | } |
403 | 0 | const BYTE val = (BYTE)((hi << 4) | lo); |
404 | 0 | hash[x] = val; |
405 | 0 | } |
406 | | |
407 | 0 | return 1; |
408 | 0 | } |
409 | | |
410 | | static BOOL ntlm_compute_ntlm_v2_hash(NTLM_CONTEXT* context, BYTE* hash) |
411 | 620 | { |
412 | 620 | WINPR_ASSERT(context); |
413 | 620 | WINPR_ASSERT(hash); |
414 | | |
415 | 620 | SSPI_CREDENTIALS* credentials = context->credentials; |
416 | | #ifdef WITH_DEBUG_NTLM |
417 | | |
418 | | if (credentials) |
419 | | { |
420 | | WLog_VRB(TAG, "Password (length = %" PRIu32 ")", credentials->identity.PasswordLength * 2); |
421 | | winpr_HexDump(TAG, WLOG_TRACE, (BYTE*)credentials->identity.Password, |
422 | | credentials->identity.PasswordLength * 2); |
423 | | WLog_VRB(TAG, "Username (length = %" PRIu32 ")", credentials->identity.UserLength * 2); |
424 | | winpr_HexDump(TAG, WLOG_TRACE, (BYTE*)credentials->identity.User, |
425 | | credentials->identity.UserLength * 2); |
426 | | WLog_VRB(TAG, "Domain (length = %" PRIu32 ")", credentials->identity.DomainLength * 2); |
427 | | winpr_HexDump(TAG, WLOG_TRACE, (BYTE*)credentials->identity.Domain, |
428 | | credentials->identity.DomainLength * 2); |
429 | | } |
430 | | else |
431 | | WLog_VRB(TAG, "Strange, NTLM_CONTEXT is missing valid credentials..."); |
432 | | |
433 | | WLog_VRB(TAG, "Workstation (length = %" PRIu16 ")", context->Workstation.Length); |
434 | | winpr_HexDump(TAG, WLOG_TRACE, (BYTE*)context->Workstation.Buffer, context->Workstation.Length); |
435 | | WLog_VRB(TAG, "NTOWFv2, NTLMv2 Hash"); |
436 | | winpr_HexDump(TAG, WLOG_TRACE, context->NtlmV2Hash, WINPR_MD5_DIGEST_LENGTH); |
437 | | #endif |
438 | | |
439 | 620 | if (memcmp(context->NtlmV2Hash, NTLM_NULL_BUFFER, 16) != 0) |
440 | 553 | return TRUE; |
441 | | |
442 | 67 | if (!credentials) |
443 | 0 | return FALSE; |
444 | 67 | else if (memcmp(context->NtlmHash, NTLM_NULL_BUFFER, 16) != 0) |
445 | 0 | { |
446 | 0 | return NTOWFv2FromHashW(context->NtlmHash, (LPWSTR)credentials->identity.User, |
447 | 0 | credentials->identity.UserLength * 2, |
448 | 0 | (LPWSTR)credentials->identity.Domain, |
449 | 0 | credentials->identity.DomainLength * 2, hash); |
450 | 0 | } |
451 | 67 | else if (credentials->identity.PasswordLength > SSPI_CREDENTIALS_HASH_LENGTH_OFFSET) |
452 | 0 | { |
453 | | /* Special case for WinPR: password hash */ |
454 | 0 | if (ntlm_convert_password_hash(context, context->NtlmHash, sizeof(context->NtlmHash)) < 0) |
455 | 0 | return FALSE; |
456 | | |
457 | 0 | return NTOWFv2FromHashW(context->NtlmHash, (LPWSTR)credentials->identity.User, |
458 | 0 | credentials->identity.UserLength * 2, |
459 | 0 | (LPWSTR)credentials->identity.Domain, |
460 | 0 | credentials->identity.DomainLength * 2, hash); |
461 | 0 | } |
462 | 67 | else if (credentials->identity.Password) |
463 | 67 | { |
464 | 67 | return NTOWFv2W( |
465 | 67 | (LPWSTR)credentials->identity.Password, credentials->identity.PasswordLength * 2, |
466 | 67 | (LPWSTR)credentials->identity.User, credentials->identity.UserLength * 2, |
467 | 67 | (LPWSTR)credentials->identity.Domain, credentials->identity.DomainLength * 2, hash); |
468 | 67 | } |
469 | 0 | else if (context->HashCallback) |
470 | 0 | { |
471 | 0 | SecBuffer proofValue = WINPR_C_ARRAY_INIT; |
472 | 0 | SecBuffer micValue = WINPR_C_ARRAY_INIT; |
473 | |
|
474 | 0 | if (ntlm_computeProofValue(context, &proofValue) != SEC_E_OK) |
475 | 0 | return FALSE; |
476 | | |
477 | 0 | if (ntlm_computeMicValue(context, &micValue) != SEC_E_OK) |
478 | 0 | { |
479 | 0 | sspi_SecBufferFree(&proofValue); |
480 | 0 | return FALSE; |
481 | 0 | } |
482 | | |
483 | 0 | const SECURITY_STATUS ret = context->HashCallback( |
484 | 0 | context->HashCallbackArg, &credentials->identity, &proofValue, |
485 | 0 | context->EncryptedRandomSessionKey, context->AUTHENTICATE_MESSAGE.MessageIntegrityCheck, |
486 | 0 | &micValue, hash); |
487 | 0 | sspi_SecBufferFree(&proofValue); |
488 | 0 | sspi_SecBufferFree(&micValue); |
489 | 0 | return ret == SEC_E_OK; |
490 | 0 | } |
491 | 0 | else if (context->UseSamFileDatabase) |
492 | 0 | { |
493 | 0 | return ntlm_fetch_ntlm_v2_hash(context, hash); |
494 | 0 | } |
495 | | |
496 | 0 | return TRUE; |
497 | 67 | } |
498 | | |
499 | | SECURITY_STATUS ntlm_compute_lm_v2_response(NTLM_CONTEXT* context) |
500 | 310 | { |
501 | 310 | BYTE* response = nullptr; |
502 | 310 | BYTE value[WINPR_MD5_DIGEST_LENGTH] = WINPR_C_ARRAY_INIT; |
503 | | |
504 | 310 | WINPR_ASSERT(context); |
505 | | |
506 | 310 | if (context->LmCompatibilityLevel < 2) |
507 | 0 | { |
508 | 0 | if (!sspi_SecBufferAlloc(&context->LmChallengeResponse, 24)) |
509 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
510 | | |
511 | 0 | ZeroMemory(context->LmChallengeResponse.pvBuffer, 24); |
512 | 0 | return SEC_E_OK; |
513 | 0 | } |
514 | | |
515 | | /* Compute the NTLMv2 hash */ |
516 | | |
517 | 310 | if (!ntlm_compute_ntlm_v2_hash(context, context->NtlmV2Hash)) |
518 | 0 | return SEC_E_NO_CREDENTIALS; |
519 | | |
520 | | /* Concatenate the server and client challenges */ |
521 | 310 | CopyMemory(value, context->ServerChallenge, 8); |
522 | 310 | CopyMemory(&value[8], context->ClientChallenge, 8); |
523 | | |
524 | 310 | if (!sspi_SecBufferAlloc(&context->LmChallengeResponse, 24)) |
525 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
526 | | |
527 | 310 | response = (BYTE*)context->LmChallengeResponse.pvBuffer; |
528 | | /* Compute the HMAC-MD5 hash of the resulting value using the NTLMv2 hash as the key */ |
529 | 310 | if (!winpr_HMAC(WINPR_MD_MD5, (void*)context->NtlmV2Hash, WINPR_MD5_DIGEST_LENGTH, (BYTE*)value, |
530 | 310 | WINPR_MD5_DIGEST_LENGTH, response, WINPR_MD5_DIGEST_LENGTH)) |
531 | 0 | return SEC_E_ALGORITHM_MISMATCH; |
532 | | |
533 | | /* Concatenate the resulting HMAC-MD5 hash and the client challenge, giving us the LMv2 response |
534 | | * (24 bytes) */ |
535 | 310 | CopyMemory(&response[16], context->ClientChallenge, 8); |
536 | 310 | return SEC_E_OK; |
537 | 310 | } |
538 | | |
539 | | /** |
540 | | * Compute NTLMv2 Response. |
541 | | * |
542 | | * NTLMv2_RESPONSE msdn{cc236653} |
543 | | * NTLMv2 Authentication msdn{cc236700} |
544 | | * |
545 | | * @param context A pointer to the NTLM context |
546 | | * @return \b TRUE for success, \b FALSE for failure |
547 | | */ |
548 | | |
549 | | SECURITY_STATUS ntlm_compute_ntlm_v2_response(NTLM_CONTEXT* context) |
550 | 310 | { |
551 | 310 | SecBuffer ntlm_v2_temp = WINPR_C_ARRAY_INIT; |
552 | 310 | SecBuffer ntlm_v2_temp_chal = WINPR_C_ARRAY_INIT; |
553 | | |
554 | 310 | WINPR_ASSERT(context); |
555 | | |
556 | 310 | PSecBuffer TargetInfo = &context->ChallengeTargetInfo; |
557 | 310 | SECURITY_STATUS ret = SEC_E_INSUFFICIENT_MEMORY; |
558 | | |
559 | 310 | if (!sspi_SecBufferAlloc(&ntlm_v2_temp, TargetInfo->cbBuffer + 28)) |
560 | 0 | goto exit; |
561 | | |
562 | 310 | ZeroMemory(ntlm_v2_temp.pvBuffer, ntlm_v2_temp.cbBuffer); |
563 | 310 | { |
564 | 310 | BYTE* blob = (BYTE*)ntlm_v2_temp.pvBuffer; |
565 | | |
566 | | /* Compute the NTLMv2 hash */ |
567 | 310 | ret = SEC_E_NO_CREDENTIALS; |
568 | 310 | if (!ntlm_compute_ntlm_v2_hash(context, (BYTE*)context->NtlmV2Hash)) |
569 | 0 | goto exit; |
570 | | |
571 | | /* Construct temp */ |
572 | 310 | blob[0] = 1; /* RespType (1 byte) */ |
573 | 310 | blob[1] = 1; /* HighRespType (1 byte) */ |
574 | | /* Reserved1 (2 bytes) */ |
575 | | /* Reserved2 (4 bytes) */ |
576 | 310 | CopyMemory(&blob[8], context->Timestamp, 8); /* Timestamp (8 bytes) */ |
577 | 310 | CopyMemory(&blob[16], context->ClientChallenge, 8); /* ClientChallenge (8 bytes) */ |
578 | | /* Reserved3 (4 bytes) */ |
579 | 310 | CopyMemory(&blob[28], TargetInfo->pvBuffer, TargetInfo->cbBuffer); |
580 | | #ifdef WITH_DEBUG_NTLM |
581 | | WLog_VRB(TAG, "NTLMv2 Response Temp Blob"); |
582 | | winpr_HexDump(TAG, WLOG_TRACE, ntlm_v2_temp.pvBuffer, ntlm_v2_temp.cbBuffer); |
583 | | #endif |
584 | 310 | } |
585 | | /* Concatenate server challenge with temp */ |
586 | 310 | ret = SEC_E_INSUFFICIENT_MEMORY; |
587 | 310 | if (!sspi_SecBufferAlloc(&ntlm_v2_temp_chal, ntlm_v2_temp.cbBuffer + 8)) |
588 | 0 | goto exit; |
589 | | |
590 | 310 | { |
591 | 310 | BYTE* blob = (BYTE*)ntlm_v2_temp_chal.pvBuffer; |
592 | 310 | CopyMemory(blob, context->ServerChallenge, 8); |
593 | 310 | CopyMemory(&blob[8], ntlm_v2_temp.pvBuffer, ntlm_v2_temp.cbBuffer); |
594 | 310 | if (!winpr_HMAC(WINPR_MD_MD5, (BYTE*)context->NtlmV2Hash, WINPR_MD5_DIGEST_LENGTH, |
595 | 310 | (BYTE*)ntlm_v2_temp_chal.pvBuffer, ntlm_v2_temp_chal.cbBuffer, |
596 | 310 | context->NtProofString, WINPR_MD5_DIGEST_LENGTH)) |
597 | 0 | goto exit; |
598 | 310 | } |
599 | | |
600 | | /* NtChallengeResponse, Concatenate NTProofStr with temp */ |
601 | | |
602 | 310 | if (!sspi_SecBufferAlloc(&context->NtChallengeResponse, ntlm_v2_temp.cbBuffer + 16)) |
603 | 0 | goto exit; |
604 | | |
605 | 310 | { |
606 | 310 | BYTE* blob = (BYTE*)context->NtChallengeResponse.pvBuffer; |
607 | 310 | CopyMemory(blob, context->NtProofString, WINPR_MD5_DIGEST_LENGTH); |
608 | 310 | CopyMemory(&blob[16], ntlm_v2_temp.pvBuffer, ntlm_v2_temp.cbBuffer); |
609 | 310 | } |
610 | | /* Compute SessionBaseKey, the HMAC-MD5 hash of NTProofStr using the NTLMv2 hash as the key */ |
611 | 310 | if (!winpr_HMAC(WINPR_MD_MD5, (BYTE*)context->NtlmV2Hash, WINPR_MD5_DIGEST_LENGTH, |
612 | 310 | context->NtProofString, WINPR_MD5_DIGEST_LENGTH, context->SessionBaseKey, |
613 | 310 | WINPR_MD5_DIGEST_LENGTH)) |
614 | 0 | goto exit; |
615 | 310 | ret = SEC_E_OK; |
616 | 310 | exit: |
617 | 310 | sspi_SecBufferFree(&ntlm_v2_temp); |
618 | 310 | sspi_SecBufferFree(&ntlm_v2_temp_chal); |
619 | 310 | return ret; |
620 | 310 | } |
621 | | |
622 | | /** |
623 | | * Encrypt the given plain text using RC4 and the given key. |
624 | | * @param key RC4 key |
625 | | * @param length text length |
626 | | * @param plaintext plain text |
627 | | * @param ciphertext cipher text |
628 | | */ |
629 | | |
630 | | BOOL ntlm_rc4k(BYTE* key, size_t length, BYTE* plaintext, BYTE* ciphertext) |
631 | 71 | { |
632 | 71 | WINPR_RC4_CTX* rc4 = winpr_RC4_New(key, 16); |
633 | | |
634 | 71 | if (!rc4) |
635 | 0 | return FALSE; |
636 | | |
637 | 71 | const BOOL rc = winpr_RC4_Update(rc4, length, plaintext, ciphertext); |
638 | 71 | winpr_RC4_Free(rc4); |
639 | 71 | return rc; |
640 | 71 | } |
641 | | |
642 | | /** |
643 | | * Generate client challenge (8-byte nonce). |
644 | | * @param context A pointer to the NTLM context |
645 | | */ |
646 | | |
647 | | BOOL ntlm_generate_client_challenge(NTLM_CONTEXT* context) |
648 | 520 | { |
649 | 520 | WINPR_ASSERT(context); |
650 | | |
651 | | /* ClientChallenge is used in computation of LMv2 and NTLMv2 responses */ |
652 | 520 | if (memcmp(context->ClientChallenge, NTLM_NULL_BUFFER, sizeof(context->ClientChallenge)) != 0) |
653 | 0 | return TRUE; |
654 | | |
655 | 520 | return winpr_RAND(context->ClientChallenge, sizeof(context->ClientChallenge)) >= 0; |
656 | 520 | } |
657 | | |
658 | | /** |
659 | | * Generate server challenge (8-byte nonce). |
660 | | * @param context A pointer to the NTLM context |
661 | | */ |
662 | | |
663 | | BOOL ntlm_generate_server_challenge(NTLM_CONTEXT* context) |
664 | 529 | { |
665 | 529 | WINPR_ASSERT(context); |
666 | | |
667 | 529 | if (memcmp(context->ServerChallenge, NTLM_NULL_BUFFER, sizeof(context->ServerChallenge)) != 0) |
668 | 0 | return TRUE; |
669 | | |
670 | 529 | return winpr_RAND(context->ServerChallenge, sizeof(context->ServerChallenge)) >= 0; |
671 | 529 | } |
672 | | |
673 | | /** |
674 | | * Generate KeyExchangeKey (the 128-bit SessionBaseKey). msdn{cc236710} |
675 | | * @param context A pointer to the NTLM context |
676 | | */ |
677 | | |
678 | | BOOL ntlm_generate_key_exchange_key(NTLM_CONTEXT* context) |
679 | 310 | { |
680 | 310 | WINPR_ASSERT(context); |
681 | 310 | WINPR_ASSERT(sizeof(context->KeyExchangeKey) == sizeof(context->SessionBaseKey)); |
682 | | |
683 | | /* In NTLMv2, KeyExchangeKey is the 128-bit SessionBaseKey */ |
684 | 310 | CopyMemory(context->KeyExchangeKey, context->SessionBaseKey, sizeof(context->KeyExchangeKey)); |
685 | 310 | return TRUE; |
686 | 310 | } |
687 | | |
688 | | /** |
689 | | * Generate RandomSessionKey (16-byte nonce). |
690 | | * @param context A pointer to the NTLM context |
691 | | */ |
692 | | |
693 | | BOOL ntlm_generate_random_session_key(NTLM_CONTEXT* context) |
694 | 67 | { |
695 | 67 | WINPR_ASSERT(context); |
696 | 67 | return winpr_RAND(context->RandomSessionKey, sizeof(context->RandomSessionKey)) >= 0; |
697 | 67 | } |
698 | | |
699 | | /** |
700 | | * Generate ExportedSessionKey (the RandomSessionKey, exported) |
701 | | * @param context A pointer to the NTLM context |
702 | | */ |
703 | | |
704 | | BOOL ntlm_generate_exported_session_key(NTLM_CONTEXT* context) |
705 | 310 | { |
706 | 310 | WINPR_ASSERT(context); |
707 | 310 | WINPR_ASSERT(sizeof(context->ExportedSessionKey) >= sizeof(context->RandomSessionKey)); |
708 | | |
709 | 310 | CopyMemory(context->ExportedSessionKey, context->RandomSessionKey, |
710 | 310 | sizeof(context->ExportedSessionKey)); |
711 | 310 | return TRUE; |
712 | 310 | } |
713 | | |
714 | | /** |
715 | | * Encrypt RandomSessionKey (RC4-encrypted RandomSessionKey, using KeyExchangeKey as the key). |
716 | | * @param context A pointer to the NTLM context |
717 | | */ |
718 | | |
719 | | BOOL ntlm_encrypt_random_session_key(NTLM_CONTEXT* context) |
720 | 67 | { |
721 | | /* In NTLMv2, EncryptedRandomSessionKey is the ExportedSessionKey RC4-encrypted with the |
722 | | * KeyExchangeKey */ |
723 | 67 | WINPR_ASSERT(context); |
724 | 67 | return ntlm_rc4k(context->KeyExchangeKey, 16, context->RandomSessionKey, |
725 | 67 | context->EncryptedRandomSessionKey); |
726 | 67 | } |
727 | | |
728 | | /** |
729 | | * Decrypt RandomSessionKey (RC4-encrypted RandomSessionKey, using KeyExchangeKey as the key). |
730 | | * @param context A pointer to the NTLM context |
731 | | */ |
732 | | |
733 | | BOOL ntlm_decrypt_random_session_key(NTLM_CONTEXT* context) |
734 | 243 | { |
735 | 243 | WINPR_ASSERT(context); |
736 | | |
737 | | /* In NTLMv2, EncryptedRandomSessionKey is the ExportedSessionKey RC4-encrypted with the |
738 | | * KeyExchangeKey */ |
739 | | |
740 | | /** |
741 | | * if (NegotiateFlags & NTLMSSP_NEGOTIATE_KEY_EXCH) |
742 | | * Set RandomSessionKey to RC4K(KeyExchangeKey, |
743 | | * AUTHENTICATE_MESSAGE.EncryptedRandomSessionKey) else Set RandomSessionKey to KeyExchangeKey |
744 | | */ |
745 | 243 | if (context->NegotiateKeyExchange) |
746 | 4 | { |
747 | 4 | WINPR_ASSERT(sizeof(context->EncryptedRandomSessionKey) == |
748 | 4 | sizeof(context->RandomSessionKey)); |
749 | 4 | return ntlm_rc4k(context->KeyExchangeKey, sizeof(context->EncryptedRandomSessionKey), |
750 | 4 | context->EncryptedRandomSessionKey, context->RandomSessionKey); |
751 | 4 | } |
752 | 239 | else |
753 | 239 | { |
754 | 239 | WINPR_ASSERT(sizeof(context->RandomSessionKey) == sizeof(context->KeyExchangeKey)); |
755 | 239 | CopyMemory(context->RandomSessionKey, context->KeyExchangeKey, |
756 | 239 | sizeof(context->RandomSessionKey)); |
757 | 239 | } |
758 | 239 | return TRUE; |
759 | 243 | } |
760 | | |
761 | | /** |
762 | | * Generate signing key msdn{cc236711} |
763 | | * |
764 | | * @param exported_session_key ExportedSessionKey |
765 | | * @param sign_magic Sign magic string |
766 | | * @param signing_key Destination signing key |
767 | | * |
768 | | * @return \b TRUE for success, \b FALSE for failure |
769 | | */ |
770 | | |
771 | | static BOOL ntlm_generate_signing_key(BYTE* exported_session_key, const SecBuffer* sign_magic, |
772 | | BYTE* signing_key) |
773 | 268 | { |
774 | 268 | BOOL rc = FALSE; |
775 | | |
776 | 268 | WINPR_ASSERT(exported_session_key); |
777 | 268 | WINPR_ASSERT(sign_magic); |
778 | 268 | WINPR_ASSERT(signing_key); |
779 | | |
780 | 268 | const size_t length = WINPR_MD5_DIGEST_LENGTH + sign_magic->cbBuffer; |
781 | 268 | BYTE* value = (BYTE*)malloc(length); |
782 | | |
783 | 268 | if (!value) |
784 | 0 | goto out; |
785 | | |
786 | | /* Concatenate ExportedSessionKey with sign magic */ |
787 | 268 | CopyMemory(value, exported_session_key, WINPR_MD5_DIGEST_LENGTH); |
788 | 268 | CopyMemory(&value[WINPR_MD5_DIGEST_LENGTH], sign_magic->pvBuffer, sign_magic->cbBuffer); |
789 | | |
790 | 268 | rc = winpr_Digest(WINPR_MD_MD5, value, length, signing_key, WINPR_MD5_DIGEST_LENGTH); |
791 | | |
792 | 268 | out: |
793 | 268 | free(value); |
794 | 268 | return rc; |
795 | 268 | } |
796 | | |
797 | | /** |
798 | | * Generate client signing key (ClientSigningKey). msdn{cc236711} |
799 | | * @param context A pointer to the NTLM context |
800 | | * |
801 | | * @return \b TRUE for success, \b FALSE for failure |
802 | | */ |
803 | | |
804 | | BOOL ntlm_generate_client_signing_key(NTLM_CONTEXT* context) |
805 | 67 | { |
806 | 67 | const SecBuffer signMagic = { sizeof(NTLM_CLIENT_SIGN_MAGIC), 0, NTLM_CLIENT_SIGN_MAGIC }; |
807 | | |
808 | 67 | WINPR_ASSERT(context); |
809 | 67 | return ntlm_generate_signing_key(context->ExportedSessionKey, &signMagic, |
810 | 67 | context->ClientSigningKey); |
811 | 67 | } |
812 | | |
813 | | /** |
814 | | * Generate server signing key (ServerSigningKey). msdn{cc236711} |
815 | | * @param context A pointer to the NTLM context |
816 | | * |
817 | | * @return \b TRUE for success, \b FALSE for failure |
818 | | */ |
819 | | |
820 | | BOOL ntlm_generate_server_signing_key(NTLM_CONTEXT* context) |
821 | 67 | { |
822 | 67 | const SecBuffer signMagic = { sizeof(NTLM_SERVER_SIGN_MAGIC), 0, NTLM_SERVER_SIGN_MAGIC }; |
823 | | |
824 | 67 | WINPR_ASSERT(context); |
825 | 67 | return ntlm_generate_signing_key(context->ExportedSessionKey, &signMagic, |
826 | 67 | context->ServerSigningKey); |
827 | 67 | } |
828 | | |
829 | | /** |
830 | | * Generate client sealing key (ClientSealingKey). msdn{cc236712} |
831 | | * @param context A pointer to the NTLM context |
832 | | * |
833 | | * @return \b TRUE for success, \b FALSE for failure |
834 | | */ |
835 | | |
836 | | BOOL ntlm_generate_client_sealing_key(NTLM_CONTEXT* context) |
837 | 67 | { |
838 | 67 | const SecBuffer sealMagic = { sizeof(NTLM_CLIENT_SEAL_MAGIC), 0, NTLM_CLIENT_SEAL_MAGIC }; |
839 | | |
840 | 67 | WINPR_ASSERT(context); |
841 | 67 | return ntlm_generate_signing_key(context->ExportedSessionKey, &sealMagic, |
842 | 67 | context->ClientSealingKey); |
843 | 67 | } |
844 | | |
845 | | /** |
846 | | * Generate server sealing key (ServerSealingKey). msdn{cc236712} |
847 | | * @param context A pointer to the NTLM context |
848 | | * |
849 | | * @return \b TRUE for success, \b FALSE for failure |
850 | | */ |
851 | | |
852 | | BOOL ntlm_generate_server_sealing_key(NTLM_CONTEXT* context) |
853 | 67 | { |
854 | 67 | const SecBuffer sealMagic = { sizeof(NTLM_SERVER_SEAL_MAGIC), 0, NTLM_SERVER_SEAL_MAGIC }; |
855 | | |
856 | 67 | WINPR_ASSERT(context); |
857 | 67 | return ntlm_generate_signing_key(context->ExportedSessionKey, &sealMagic, |
858 | 67 | context->ServerSealingKey); |
859 | 67 | } |
860 | | |
861 | | /** |
862 | | * Initialize RC4 stream cipher states for sealing. |
863 | | * @param context A pointer to the NTLM context |
864 | | */ |
865 | | |
866 | | BOOL ntlm_init_rc4_seal_states(NTLM_CONTEXT* context) |
867 | 67 | { |
868 | 67 | WINPR_ASSERT(context); |
869 | 67 | if (context->server) |
870 | 0 | { |
871 | 0 | context->SendSigningKey = context->ServerSigningKey; |
872 | 0 | context->RecvSigningKey = context->ClientSigningKey; |
873 | 0 | context->SendSealingKey = context->ClientSealingKey; |
874 | 0 | context->RecvSealingKey = context->ServerSealingKey; |
875 | 0 | context->SendRc4Seal = |
876 | 0 | winpr_RC4_New(context->ServerSealingKey, sizeof(context->ServerSealingKey)); |
877 | 0 | context->RecvRc4Seal = |
878 | 0 | winpr_RC4_New(context->ClientSealingKey, sizeof(context->ClientSealingKey)); |
879 | 0 | } |
880 | 67 | else |
881 | 67 | { |
882 | 67 | context->SendSigningKey = context->ClientSigningKey; |
883 | 67 | context->RecvSigningKey = context->ServerSigningKey; |
884 | 67 | context->SendSealingKey = context->ServerSealingKey; |
885 | 67 | context->RecvSealingKey = context->ClientSealingKey; |
886 | 67 | context->SendRc4Seal = |
887 | 67 | winpr_RC4_New(context->ClientSealingKey, sizeof(context->ClientSealingKey)); |
888 | 67 | context->RecvRc4Seal = |
889 | 67 | winpr_RC4_New(context->ServerSealingKey, sizeof(context->ServerSealingKey)); |
890 | 67 | } |
891 | 67 | if (!context->SendRc4Seal) |
892 | 0 | { |
893 | 0 | WLog_ERR(TAG, "Failed to allocate context->SendRc4Seal"); |
894 | 0 | return FALSE; |
895 | 0 | } |
896 | 67 | if (!context->RecvRc4Seal) |
897 | 0 | { |
898 | 0 | WLog_ERR(TAG, "Failed to allocate context->RecvRc4Seal"); |
899 | 0 | return FALSE; |
900 | 0 | } |
901 | 67 | return TRUE; |
902 | 67 | } |
903 | | |
904 | | BOOL ntlm_compute_message_integrity_check(NTLM_CONTEXT* context, BYTE* mic, UINT32 size) |
905 | 146 | { |
906 | 146 | BOOL rc = FALSE; |
907 | | /* |
908 | | * Compute the HMAC-MD5 hash of ConcatenationOf(NEGOTIATE_MESSAGE, |
909 | | * CHALLENGE_MESSAGE, AUTHENTICATE_MESSAGE) using the ExportedSessionKey |
910 | | */ |
911 | 146 | WINPR_HMAC_CTX* hmac = winpr_HMAC_New(); |
912 | | |
913 | 146 | WINPR_ASSERT(context); |
914 | 146 | WINPR_ASSERT(mic); |
915 | 146 | WINPR_ASSERT(size >= WINPR_MD5_DIGEST_LENGTH); |
916 | | |
917 | 146 | memset(mic, 0, size); |
918 | 146 | if (!hmac) |
919 | 0 | return FALSE; |
920 | | |
921 | 146 | if (!winpr_HMAC_Init(hmac, WINPR_MD_MD5, context->ExportedSessionKey, WINPR_MD5_DIGEST_LENGTH)) |
922 | 0 | goto fail; |
923 | | |
924 | 146 | if (!winpr_HMAC_Update(hmac, (BYTE*)context->NegotiateMessage.pvBuffer, |
925 | 146 | context->NegotiateMessage.cbBuffer)) |
926 | 0 | goto fail; |
927 | 146 | if (!winpr_HMAC_Update(hmac, (BYTE*)context->ChallengeMessage.pvBuffer, |
928 | 146 | context->ChallengeMessage.cbBuffer)) |
929 | 0 | goto fail; |
930 | | |
931 | 146 | if (context->MessageIntegrityCheckOffset > 0) |
932 | 146 | { |
933 | 146 | const BYTE* auth = (BYTE*)context->AuthenticateMessage.pvBuffer; |
934 | 146 | const BYTE data[WINPR_MD5_DIGEST_LENGTH] = WINPR_C_ARRAY_INIT; |
935 | 146 | const size_t rest = context->MessageIntegrityCheckOffset + sizeof(data); |
936 | | |
937 | 146 | if (rest > context->AuthenticateMessage.cbBuffer) |
938 | 12 | goto fail; |
939 | 134 | if (!winpr_HMAC_Update(hmac, &auth[0], context->MessageIntegrityCheckOffset)) |
940 | 0 | goto fail; |
941 | 134 | if (!winpr_HMAC_Update(hmac, data, sizeof(data))) |
942 | 0 | goto fail; |
943 | 134 | if (!winpr_HMAC_Update(hmac, &auth[rest], context->AuthenticateMessage.cbBuffer - rest)) |
944 | 0 | goto fail; |
945 | 134 | } |
946 | 0 | else |
947 | 0 | { |
948 | 0 | if (!winpr_HMAC_Update(hmac, (BYTE*)context->AuthenticateMessage.pvBuffer, |
949 | 0 | context->AuthenticateMessage.cbBuffer)) |
950 | 0 | goto fail; |
951 | 0 | } |
952 | 134 | rc = winpr_HMAC_Final(hmac, mic, WINPR_MD5_DIGEST_LENGTH); |
953 | | |
954 | 146 | fail: |
955 | 146 | winpr_HMAC_Free(hmac); |
956 | 146 | return rc; |
957 | 134 | } |