/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 | 0 | #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 | 0 | { |
60 | 0 | 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 | 0 | versionInfo->ProductMajorVersion = 10; |
78 | 0 | versionInfo->ProductMinorVersion = 0; |
79 | 0 | versionInfo->ProductBuild = 22631; |
80 | 0 | #endif |
81 | 0 | ZeroMemory(versionInfo->Reserved, sizeof(versionInfo->Reserved)); |
82 | 0 | versionInfo->NTLMRevisionCurrent = NTLMSSP_REVISION_W2K3; |
83 | 0 | return TRUE; |
84 | 0 | } |
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 | 0 | { |
96 | 0 | WINPR_ASSERT(s); |
97 | 0 | WINPR_ASSERT(versionInfo); |
98 | |
|
99 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 8)) |
100 | 0 | return FALSE; |
101 | | |
102 | 0 | Stream_Read_UINT8(s, versionInfo->ProductMajorVersion); /* ProductMajorVersion (1 byte) */ |
103 | 0 | Stream_Read_UINT8(s, versionInfo->ProductMinorVersion); /* ProductMinorVersion (1 byte) */ |
104 | 0 | Stream_Read_UINT16(s, versionInfo->ProductBuild); /* ProductBuild (2 bytes) */ |
105 | 0 | Stream_Read(s, versionInfo->Reserved, sizeof(versionInfo->Reserved)); /* Reserved (3 bytes) */ |
106 | 0 | Stream_Read_UINT8(s, versionInfo->NTLMRevisionCurrent); /* NTLMRevisionCurrent (1 byte) */ |
107 | 0 | return TRUE; |
108 | 0 | } |
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 | 0 | { |
120 | 0 | WINPR_ASSERT(s); |
121 | 0 | WINPR_ASSERT(versionInfo); |
122 | |
|
123 | 0 | if (!Stream_CheckAndLogRequiredCapacityEx( |
124 | 0 | TAG, WLOG_WARN, s, 5ull + sizeof(versionInfo->Reserved), 1ull, |
125 | 0 | "%s(%s:%" PRIuz ") NTLM_VERSION_INFO", __func__, __FILE__, (size_t)__LINE__)) |
126 | 0 | return FALSE; |
127 | | |
128 | 0 | Stream_Write_UINT8(s, versionInfo->ProductMajorVersion); /* ProductMajorVersion (1 byte) */ |
129 | 0 | Stream_Write_UINT8(s, versionInfo->ProductMinorVersion); /* ProductMinorVersion (1 byte) */ |
130 | 0 | Stream_Write_UINT16(s, versionInfo->ProductBuild); /* ProductBuild (2 bytes) */ |
131 | 0 | Stream_Write(s, versionInfo->Reserved, sizeof(versionInfo->Reserved)); /* Reserved (3 bytes) */ |
132 | 0 | Stream_Write_UINT8(s, versionInfo->NTLMRevisionCurrent); /* NTLMRevisionCurrent (1 byte) */ |
133 | 0 | return TRUE; |
134 | 0 | } |
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 | 0 | { |
157 | 0 | size_t size = 0; |
158 | 0 | WINPR_ASSERT(s); |
159 | 0 | WINPR_ASSERT(challenge); |
160 | |
|
161 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 28)) |
162 | 0 | return FALSE; |
163 | | |
164 | 0 | Stream_Read_UINT8(s, challenge->RespType); |
165 | 0 | Stream_Read_UINT8(s, challenge->HiRespType); |
166 | 0 | Stream_Read_UINT16(s, challenge->Reserved1); |
167 | 0 | Stream_Read_UINT32(s, challenge->Reserved2); |
168 | 0 | Stream_Read(s, challenge->Timestamp, 8); |
169 | 0 | Stream_Read(s, challenge->ClientChallenge, 8); |
170 | 0 | Stream_Read_UINT32(s, challenge->Reserved3); |
171 | 0 | size = Stream_Length(s) - Stream_GetPosition(s); |
172 | |
|
173 | 0 | 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 | 0 | challenge->cbAvPairs = (UINT32)size; |
180 | 0 | challenge->AvPairs = (NTLM_AV_PAIR*)malloc(challenge->cbAvPairs); |
181 | |
|
182 | 0 | 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 | 0 | Stream_Read(s, challenge->AvPairs, size); |
190 | 0 | return TRUE; |
191 | 0 | } |
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 | 0 | { |
222 | 0 | WINPR_ASSERT(s); |
223 | 0 | WINPR_ASSERT(response); |
224 | |
|
225 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 16)) |
226 | 0 | return FALSE; |
227 | | |
228 | 0 | Stream_Read(s, response->Response, 16); |
229 | 0 | return ntlm_read_ntlm_v2_client_challenge(s, &(response->Challenge)); |
230 | 0 | } |
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 | 0 | { |
251 | 0 | FILETIME ft = WINPR_C_ARRAY_INIT; |
252 | |
|
253 | 0 | WINPR_ASSERT(timestamp); |
254 | 0 | WINPR_ASSERT(size >= sizeof(ft)); |
255 | |
|
256 | 0 | GetSystemTimeAsFileTime(&ft); |
257 | 0 | CopyMemory(timestamp, &(ft), sizeof(ft)); |
258 | 0 | } |
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 | 0 | { |
268 | 0 | WINPR_ASSERT(context); |
269 | |
|
270 | 0 | if (memcmp(context->ChallengeTimestamp, NTLM_NULL_BUFFER, 8) != 0) |
271 | 0 | CopyMemory(context->Timestamp, context->ChallengeTimestamp, 8); |
272 | 0 | else |
273 | 0 | ntlm_current_time(context->Timestamp, sizeof(context->Timestamp)); |
274 | 0 | } |
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* sam = nullptr; |
280 | 0 | WINPR_SAM_ENTRY* entry = nullptr; |
281 | 0 | SSPI_CREDENTIALS* credentials = nullptr; |
282 | |
|
283 | 0 | WINPR_ASSERT(context); |
284 | 0 | WINPR_ASSERT(hash); |
285 | |
|
286 | 0 | credentials = context->credentials; |
287 | 0 | sam = SamOpen(context->SamFile, TRUE); |
288 | |
|
289 | 0 | if (!sam) |
290 | 0 | goto fail; |
291 | | |
292 | 0 | entry = SamLookupUserW( |
293 | 0 | sam, (LPWSTR)credentials->identity.User, credentials->identity.UserLength * sizeof(WCHAR), |
294 | 0 | (LPWSTR)credentials->identity.Domain, credentials->identity.DomainLength * sizeof(WCHAR)); |
295 | |
|
296 | 0 | if (!entry) |
297 | 0 | { |
298 | 0 | entry = SamLookupUserW(sam, (LPWSTR)credentials->identity.User, |
299 | 0 | credentials->identity.UserLength * sizeof(WCHAR), nullptr, 0); |
300 | 0 | } |
301 | |
|
302 | 0 | if (!entry) |
303 | 0 | goto fail; |
304 | | |
305 | | #ifdef WITH_DEBUG_NTLM |
306 | | WLog_VRB(TAG, "NTLM Hash:"); |
307 | | winpr_HexDump(TAG, WLOG_DEBUG, entry->NtHash, 16); |
308 | | #endif |
309 | 0 | rc = NTOWFv2FromHashW(entry->NtHash, (LPWSTR)credentials->identity.User, |
310 | 0 | credentials->identity.UserLength * sizeof(WCHAR), |
311 | 0 | (LPWSTR)credentials->identity.Domain, |
312 | 0 | credentials->identity.DomainLength * sizeof(WCHAR), hash); |
313 | |
|
314 | 0 | fail: |
315 | 0 | SamFreeEntry(sam, entry); |
316 | 0 | SamClose(sam); |
317 | 0 | if (!rc) |
318 | 0 | WLog_ERR(TAG, "Error: Could not find user in SAM database"); |
319 | |
|
320 | 0 | return rc; |
321 | 0 | } |
322 | | |
323 | | static int hexchar2nibble(WCHAR wc) |
324 | 0 | { |
325 | | #if defined(__BIG_ENDIAN__) |
326 | | union |
327 | | { |
328 | | BYTE b[2]; |
329 | | WCHAR w; |
330 | | } cnv; |
331 | | cnv.w = wc; |
332 | | const BYTE b = cnv.b[0]; |
333 | | cnv.b[0] = cnv.b[1]; |
334 | | cnv.b[1] = b; |
335 | | wc = cnv.w; |
336 | | #endif |
337 | |
|
338 | 0 | switch (wc) |
339 | 0 | { |
340 | 0 | case L'0': |
341 | 0 | case L'1': |
342 | 0 | case L'2': |
343 | 0 | case L'3': |
344 | 0 | case L'4': |
345 | 0 | case L'5': |
346 | 0 | case L'6': |
347 | 0 | case L'7': |
348 | 0 | case L'8': |
349 | 0 | case L'9': |
350 | 0 | return wc - L'0'; |
351 | 0 | case L'a': |
352 | 0 | case L'b': |
353 | 0 | case L'c': |
354 | 0 | case L'd': |
355 | 0 | case L'e': |
356 | 0 | case L'f': |
357 | 0 | return wc - L'a' + 10; |
358 | 0 | case L'A': |
359 | 0 | case L'B': |
360 | 0 | case L'C': |
361 | 0 | case L'D': |
362 | 0 | case L'E': |
363 | 0 | case L'F': |
364 | 0 | return wc - L'A' + 10; |
365 | 0 | default: |
366 | 0 | return -1; |
367 | 0 | } |
368 | 0 | } |
369 | | static int ntlm_convert_password_hash(NTLM_CONTEXT* context, BYTE* hash, size_t hashlen) |
370 | 0 | { |
371 | 0 | const size_t required_len = 2ull * hashlen; |
372 | |
|
373 | 0 | WINPR_ASSERT(context); |
374 | 0 | WINPR_ASSERT(hash); |
375 | |
|
376 | 0 | SSPI_CREDENTIALS* credentials = context->credentials; |
377 | | /* Password contains a password hash of length (PasswordLength - |
378 | | * SSPI_CREDENTIALS_HASH_LENGTH_OFFSET) */ |
379 | 0 | const ULONG PasswordHashLength = credentials->identity.PasswordLength - |
380 | 0 | /* Macro [globalScope] */ SSPI_CREDENTIALS_HASH_LENGTH_OFFSET; |
381 | |
|
382 | 0 | if (PasswordHashLength != required_len) |
383 | 0 | { |
384 | 0 | WLog_ERR(TAG, |
385 | 0 | "PasswordHash has invalid length %" PRIu32 " must be exactly %" PRIuz " bytes", |
386 | 0 | PasswordHashLength, required_len); |
387 | 0 | return -1; |
388 | 0 | } |
389 | | |
390 | 0 | const WCHAR* PasswordHash = credentials->identity.Password; |
391 | 0 | for (size_t x = 0; x < hashlen; x++) |
392 | 0 | { |
393 | 0 | const int hi = hexchar2nibble(PasswordHash[2 * x]); |
394 | 0 | if (hi < 0) |
395 | 0 | { |
396 | 0 | WLog_ERR(TAG, "PasswordHash has an invalid value at position %" PRIuz, 2 * x); |
397 | 0 | return -1; |
398 | 0 | } |
399 | 0 | const int lo = hexchar2nibble(PasswordHash[2 * x + 1]); |
400 | 0 | if (lo < 0) |
401 | 0 | { |
402 | 0 | WLog_ERR(TAG, "PasswordHash has an invalid value at position %" PRIuz, 2 * x + 1); |
403 | 0 | return -1; |
404 | 0 | } |
405 | 0 | const BYTE val = (BYTE)((hi << 4) | lo); |
406 | 0 | hash[x] = val; |
407 | 0 | } |
408 | | |
409 | 0 | return 1; |
410 | 0 | } |
411 | | |
412 | | static BOOL ntlm_compute_ntlm_v2_hash(NTLM_CONTEXT* context, BYTE* hash) |
413 | 0 | { |
414 | 0 | SSPI_CREDENTIALS* credentials = nullptr; |
415 | |
|
416 | 0 | WINPR_ASSERT(context); |
417 | 0 | WINPR_ASSERT(hash); |
418 | |
|
419 | 0 | credentials = context->credentials; |
420 | | #ifdef WITH_DEBUG_NTLM |
421 | | |
422 | | if (credentials) |
423 | | { |
424 | | WLog_VRB(TAG, "Password (length = %" PRIu32 ")", credentials->identity.PasswordLength * 2); |
425 | | winpr_HexDump(TAG, WLOG_TRACE, (BYTE*)credentials->identity.Password, |
426 | | credentials->identity.PasswordLength * 2); |
427 | | WLog_VRB(TAG, "Username (length = %" PRIu32 ")", credentials->identity.UserLength * 2); |
428 | | winpr_HexDump(TAG, WLOG_TRACE, (BYTE*)credentials->identity.User, |
429 | | credentials->identity.UserLength * 2); |
430 | | WLog_VRB(TAG, "Domain (length = %" PRIu32 ")", credentials->identity.DomainLength * 2); |
431 | | winpr_HexDump(TAG, WLOG_TRACE, (BYTE*)credentials->identity.Domain, |
432 | | credentials->identity.DomainLength * 2); |
433 | | } |
434 | | else |
435 | | WLog_VRB(TAG, "Strange, NTLM_CONTEXT is missing valid credentials..."); |
436 | | |
437 | | WLog_VRB(TAG, "Workstation (length = %" PRIu16 ")", context->Workstation.Length); |
438 | | winpr_HexDump(TAG, WLOG_TRACE, (BYTE*)context->Workstation.Buffer, context->Workstation.Length); |
439 | | WLog_VRB(TAG, "NTOWFv2, NTLMv2 Hash"); |
440 | | winpr_HexDump(TAG, WLOG_TRACE, context->NtlmV2Hash, WINPR_MD5_DIGEST_LENGTH); |
441 | | #endif |
442 | |
|
443 | 0 | if (memcmp(context->NtlmV2Hash, NTLM_NULL_BUFFER, 16) != 0) |
444 | 0 | return TRUE; |
445 | | |
446 | 0 | if (!credentials) |
447 | 0 | return FALSE; |
448 | 0 | else if (memcmp(context->NtlmHash, NTLM_NULL_BUFFER, 16) != 0) |
449 | 0 | { |
450 | 0 | return NTOWFv2FromHashW(context->NtlmHash, (LPWSTR)credentials->identity.User, |
451 | 0 | credentials->identity.UserLength * 2, |
452 | 0 | (LPWSTR)credentials->identity.Domain, |
453 | 0 | credentials->identity.DomainLength * 2, hash); |
454 | 0 | } |
455 | 0 | else if (credentials->identity.PasswordLength > SSPI_CREDENTIALS_HASH_LENGTH_OFFSET) |
456 | 0 | { |
457 | | /* Special case for WinPR: password hash */ |
458 | 0 | if (ntlm_convert_password_hash(context, context->NtlmHash, sizeof(context->NtlmHash)) < 0) |
459 | 0 | return FALSE; |
460 | | |
461 | 0 | return NTOWFv2FromHashW(context->NtlmHash, (LPWSTR)credentials->identity.User, |
462 | 0 | credentials->identity.UserLength * 2, |
463 | 0 | (LPWSTR)credentials->identity.Domain, |
464 | 0 | credentials->identity.DomainLength * 2, hash); |
465 | 0 | } |
466 | 0 | else if (credentials->identity.Password) |
467 | 0 | { |
468 | 0 | return NTOWFv2W( |
469 | 0 | (LPWSTR)credentials->identity.Password, credentials->identity.PasswordLength * 2, |
470 | 0 | (LPWSTR)credentials->identity.User, credentials->identity.UserLength * 2, |
471 | 0 | (LPWSTR)credentials->identity.Domain, credentials->identity.DomainLength * 2, hash); |
472 | 0 | } |
473 | 0 | else if (context->HashCallback) |
474 | 0 | { |
475 | 0 | SecBuffer proofValue = WINPR_C_ARRAY_INIT; |
476 | 0 | SecBuffer micValue = WINPR_C_ARRAY_INIT; |
477 | |
|
478 | 0 | if (ntlm_computeProofValue(context, &proofValue) != SEC_E_OK) |
479 | 0 | return FALSE; |
480 | | |
481 | 0 | if (ntlm_computeMicValue(context, &micValue) != SEC_E_OK) |
482 | 0 | { |
483 | 0 | sspi_SecBufferFree(&proofValue); |
484 | 0 | return FALSE; |
485 | 0 | } |
486 | | |
487 | 0 | const SECURITY_STATUS ret = context->HashCallback( |
488 | 0 | context->HashCallbackArg, &credentials->identity, &proofValue, |
489 | 0 | context->EncryptedRandomSessionKey, context->AUTHENTICATE_MESSAGE.MessageIntegrityCheck, |
490 | 0 | &micValue, hash); |
491 | 0 | sspi_SecBufferFree(&proofValue); |
492 | 0 | sspi_SecBufferFree(&micValue); |
493 | 0 | return ret == SEC_E_OK; |
494 | 0 | } |
495 | 0 | else if (context->UseSamFileDatabase) |
496 | 0 | { |
497 | 0 | return ntlm_fetch_ntlm_v2_hash(context, hash); |
498 | 0 | } |
499 | | |
500 | 0 | return TRUE; |
501 | 0 | } |
502 | | |
503 | | SECURITY_STATUS ntlm_compute_lm_v2_response(NTLM_CONTEXT* context) |
504 | 0 | { |
505 | 0 | BYTE* response = nullptr; |
506 | 0 | BYTE value[WINPR_MD5_DIGEST_LENGTH] = WINPR_C_ARRAY_INIT; |
507 | |
|
508 | 0 | WINPR_ASSERT(context); |
509 | |
|
510 | 0 | if (context->LmCompatibilityLevel < 2) |
511 | 0 | { |
512 | 0 | if (!sspi_SecBufferAlloc(&context->LmChallengeResponse, 24)) |
513 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
514 | | |
515 | 0 | ZeroMemory(context->LmChallengeResponse.pvBuffer, 24); |
516 | 0 | return SEC_E_OK; |
517 | 0 | } |
518 | | |
519 | | /* Compute the NTLMv2 hash */ |
520 | | |
521 | 0 | if (!ntlm_compute_ntlm_v2_hash(context, context->NtlmV2Hash)) |
522 | 0 | return SEC_E_NO_CREDENTIALS; |
523 | | |
524 | | /* Concatenate the server and client challenges */ |
525 | 0 | CopyMemory(value, context->ServerChallenge, 8); |
526 | 0 | CopyMemory(&value[8], context->ClientChallenge, 8); |
527 | |
|
528 | 0 | if (!sspi_SecBufferAlloc(&context->LmChallengeResponse, 24)) |
529 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
530 | | |
531 | 0 | response = (BYTE*)context->LmChallengeResponse.pvBuffer; |
532 | | /* Compute the HMAC-MD5 hash of the resulting value using the NTLMv2 hash as the key */ |
533 | 0 | if (!winpr_HMAC(WINPR_MD_MD5, (void*)context->NtlmV2Hash, WINPR_MD5_DIGEST_LENGTH, (BYTE*)value, |
534 | 0 | WINPR_MD5_DIGEST_LENGTH, response, WINPR_MD5_DIGEST_LENGTH)) |
535 | 0 | return SEC_E_ALGORITHM_MISMATCH; |
536 | | |
537 | | /* Concatenate the resulting HMAC-MD5 hash and the client challenge, giving us the LMv2 response |
538 | | * (24 bytes) */ |
539 | 0 | CopyMemory(&response[16], context->ClientChallenge, 8); |
540 | 0 | return SEC_E_OK; |
541 | 0 | } |
542 | | |
543 | | /** |
544 | | * Compute NTLMv2 Response. |
545 | | * |
546 | | * NTLMv2_RESPONSE msdn{cc236653} |
547 | | * NTLMv2 Authentication msdn{cc236700} |
548 | | * |
549 | | * @param context A pointer to the NTLM context |
550 | | * @return \b TRUE for success, \b FALSE for failure |
551 | | */ |
552 | | |
553 | | SECURITY_STATUS ntlm_compute_ntlm_v2_response(NTLM_CONTEXT* context) |
554 | 0 | { |
555 | 0 | SecBuffer ntlm_v2_temp = WINPR_C_ARRAY_INIT; |
556 | 0 | SecBuffer ntlm_v2_temp_chal = WINPR_C_ARRAY_INIT; |
557 | |
|
558 | 0 | WINPR_ASSERT(context); |
559 | |
|
560 | 0 | PSecBuffer TargetInfo = &context->ChallengeTargetInfo; |
561 | 0 | SECURITY_STATUS ret = SEC_E_INSUFFICIENT_MEMORY; |
562 | |
|
563 | 0 | if (!sspi_SecBufferAlloc(&ntlm_v2_temp, TargetInfo->cbBuffer + 28)) |
564 | 0 | goto exit; |
565 | | |
566 | 0 | ZeroMemory(ntlm_v2_temp.pvBuffer, ntlm_v2_temp.cbBuffer); |
567 | 0 | { |
568 | 0 | BYTE* blob = (BYTE*)ntlm_v2_temp.pvBuffer; |
569 | | |
570 | | /* Compute the NTLMv2 hash */ |
571 | 0 | ret = SEC_E_NO_CREDENTIALS; |
572 | 0 | if (!ntlm_compute_ntlm_v2_hash(context, (BYTE*)context->NtlmV2Hash)) |
573 | 0 | goto exit; |
574 | | |
575 | | /* Construct temp */ |
576 | 0 | blob[0] = 1; /* RespType (1 byte) */ |
577 | 0 | blob[1] = 1; /* HighRespType (1 byte) */ |
578 | | /* Reserved1 (2 bytes) */ |
579 | | /* Reserved2 (4 bytes) */ |
580 | 0 | CopyMemory(&blob[8], context->Timestamp, 8); /* Timestamp (8 bytes) */ |
581 | 0 | CopyMemory(&blob[16], context->ClientChallenge, 8); /* ClientChallenge (8 bytes) */ |
582 | | /* Reserved3 (4 bytes) */ |
583 | 0 | CopyMemory(&blob[28], TargetInfo->pvBuffer, TargetInfo->cbBuffer); |
584 | | #ifdef WITH_DEBUG_NTLM |
585 | | WLog_VRB(TAG, "NTLMv2 Response Temp Blob"); |
586 | | winpr_HexDump(TAG, WLOG_TRACE, ntlm_v2_temp.pvBuffer, ntlm_v2_temp.cbBuffer); |
587 | | #endif |
588 | 0 | } |
589 | | /* Concatenate server challenge with temp */ |
590 | 0 | ret = SEC_E_INSUFFICIENT_MEMORY; |
591 | 0 | if (!sspi_SecBufferAlloc(&ntlm_v2_temp_chal, ntlm_v2_temp.cbBuffer + 8)) |
592 | 0 | goto exit; |
593 | | |
594 | 0 | { |
595 | 0 | BYTE* blob = (BYTE*)ntlm_v2_temp_chal.pvBuffer; |
596 | 0 | CopyMemory(blob, context->ServerChallenge, 8); |
597 | 0 | CopyMemory(&blob[8], ntlm_v2_temp.pvBuffer, ntlm_v2_temp.cbBuffer); |
598 | 0 | if (!winpr_HMAC(WINPR_MD_MD5, (BYTE*)context->NtlmV2Hash, WINPR_MD5_DIGEST_LENGTH, |
599 | 0 | (BYTE*)ntlm_v2_temp_chal.pvBuffer, ntlm_v2_temp_chal.cbBuffer, |
600 | 0 | context->NtProofString, WINPR_MD5_DIGEST_LENGTH)) |
601 | 0 | goto exit; |
602 | 0 | } |
603 | | |
604 | | /* NtChallengeResponse, Concatenate NTProofStr with temp */ |
605 | | |
606 | 0 | if (!sspi_SecBufferAlloc(&context->NtChallengeResponse, ntlm_v2_temp.cbBuffer + 16)) |
607 | 0 | goto exit; |
608 | | |
609 | 0 | { |
610 | 0 | BYTE* blob = (BYTE*)context->NtChallengeResponse.pvBuffer; |
611 | 0 | CopyMemory(blob, context->NtProofString, WINPR_MD5_DIGEST_LENGTH); |
612 | 0 | CopyMemory(&blob[16], ntlm_v2_temp.pvBuffer, ntlm_v2_temp.cbBuffer); |
613 | 0 | } |
614 | | /* Compute SessionBaseKey, the HMAC-MD5 hash of NTProofStr using the NTLMv2 hash as the key */ |
615 | 0 | if (!winpr_HMAC(WINPR_MD_MD5, (BYTE*)context->NtlmV2Hash, WINPR_MD5_DIGEST_LENGTH, |
616 | 0 | context->NtProofString, WINPR_MD5_DIGEST_LENGTH, context->SessionBaseKey, |
617 | 0 | WINPR_MD5_DIGEST_LENGTH)) |
618 | 0 | goto exit; |
619 | 0 | ret = SEC_E_OK; |
620 | 0 | exit: |
621 | 0 | sspi_SecBufferFree(&ntlm_v2_temp); |
622 | 0 | sspi_SecBufferFree(&ntlm_v2_temp_chal); |
623 | 0 | return ret; |
624 | 0 | } |
625 | | |
626 | | /** |
627 | | * Encrypt the given plain text using RC4 and the given key. |
628 | | * @param key RC4 key |
629 | | * @param length text length |
630 | | * @param plaintext plain text |
631 | | * @param ciphertext cipher text |
632 | | */ |
633 | | |
634 | | BOOL ntlm_rc4k(BYTE* key, size_t length, BYTE* plaintext, BYTE* ciphertext) |
635 | 0 | { |
636 | 0 | WINPR_RC4_CTX* rc4 = winpr_RC4_New(key, 16); |
637 | |
|
638 | 0 | if (!rc4) |
639 | 0 | return FALSE; |
640 | | |
641 | 0 | const BOOL rc = winpr_RC4_Update(rc4, length, plaintext, ciphertext); |
642 | 0 | winpr_RC4_Free(rc4); |
643 | 0 | return rc; |
644 | 0 | } |
645 | | |
646 | | /** |
647 | | * Generate client challenge (8-byte nonce). |
648 | | * @param context A pointer to the NTLM context |
649 | | */ |
650 | | |
651 | | BOOL ntlm_generate_client_challenge(NTLM_CONTEXT* context) |
652 | 0 | { |
653 | 0 | WINPR_ASSERT(context); |
654 | | |
655 | | /* ClientChallenge is used in computation of LMv2 and NTLMv2 responses */ |
656 | 0 | if (memcmp(context->ClientChallenge, NTLM_NULL_BUFFER, sizeof(context->ClientChallenge)) != 0) |
657 | 0 | return TRUE; |
658 | | |
659 | 0 | return winpr_RAND(context->ClientChallenge, sizeof(context->ClientChallenge)) >= 0; |
660 | 0 | } |
661 | | |
662 | | /** |
663 | | * Generate server challenge (8-byte nonce). |
664 | | * @param context A pointer to the NTLM context |
665 | | */ |
666 | | |
667 | | BOOL ntlm_generate_server_challenge(NTLM_CONTEXT* context) |
668 | 0 | { |
669 | 0 | WINPR_ASSERT(context); |
670 | |
|
671 | 0 | if (memcmp(context->ServerChallenge, NTLM_NULL_BUFFER, sizeof(context->ServerChallenge)) != 0) |
672 | 0 | return TRUE; |
673 | | |
674 | 0 | return winpr_RAND(context->ServerChallenge, sizeof(context->ServerChallenge)) >= 0; |
675 | 0 | } |
676 | | |
677 | | /** |
678 | | * Generate KeyExchangeKey (the 128-bit SessionBaseKey). msdn{cc236710} |
679 | | * @param context A pointer to the NTLM context |
680 | | */ |
681 | | |
682 | | BOOL ntlm_generate_key_exchange_key(NTLM_CONTEXT* context) |
683 | 0 | { |
684 | 0 | WINPR_ASSERT(context); |
685 | 0 | WINPR_ASSERT(sizeof(context->KeyExchangeKey) == sizeof(context->SessionBaseKey)); |
686 | | |
687 | | /* In NTLMv2, KeyExchangeKey is the 128-bit SessionBaseKey */ |
688 | 0 | CopyMemory(context->KeyExchangeKey, context->SessionBaseKey, sizeof(context->KeyExchangeKey)); |
689 | 0 | return TRUE; |
690 | 0 | } |
691 | | |
692 | | /** |
693 | | * Generate RandomSessionKey (16-byte nonce). |
694 | | * @param context A pointer to the NTLM context |
695 | | */ |
696 | | |
697 | | BOOL ntlm_generate_random_session_key(NTLM_CONTEXT* context) |
698 | 0 | { |
699 | 0 | WINPR_ASSERT(context); |
700 | 0 | return winpr_RAND(context->RandomSessionKey, sizeof(context->RandomSessionKey)) >= 0; |
701 | 0 | } |
702 | | |
703 | | /** |
704 | | * Generate ExportedSessionKey (the RandomSessionKey, exported) |
705 | | * @param context A pointer to the NTLM context |
706 | | */ |
707 | | |
708 | | BOOL ntlm_generate_exported_session_key(NTLM_CONTEXT* context) |
709 | 0 | { |
710 | 0 | WINPR_ASSERT(context); |
711 | 0 | WINPR_ASSERT(sizeof(context->ExportedSessionKey) >= sizeof(context->RandomSessionKey)); |
712 | |
|
713 | 0 | CopyMemory(context->ExportedSessionKey, context->RandomSessionKey, |
714 | 0 | sizeof(context->ExportedSessionKey)); |
715 | 0 | return TRUE; |
716 | 0 | } |
717 | | |
718 | | /** |
719 | | * Encrypt RandomSessionKey (RC4-encrypted RandomSessionKey, using KeyExchangeKey as the key). |
720 | | * @param context A pointer to the NTLM context |
721 | | */ |
722 | | |
723 | | BOOL ntlm_encrypt_random_session_key(NTLM_CONTEXT* context) |
724 | 0 | { |
725 | | /* In NTLMv2, EncryptedRandomSessionKey is the ExportedSessionKey RC4-encrypted with the |
726 | | * KeyExchangeKey */ |
727 | 0 | WINPR_ASSERT(context); |
728 | 0 | return ntlm_rc4k(context->KeyExchangeKey, 16, context->RandomSessionKey, |
729 | 0 | context->EncryptedRandomSessionKey); |
730 | 0 | } |
731 | | |
732 | | /** |
733 | | * Decrypt RandomSessionKey (RC4-encrypted RandomSessionKey, using KeyExchangeKey as the key). |
734 | | * @param context A pointer to the NTLM context |
735 | | */ |
736 | | |
737 | | BOOL ntlm_decrypt_random_session_key(NTLM_CONTEXT* context) |
738 | 0 | { |
739 | 0 | WINPR_ASSERT(context); |
740 | | |
741 | | /* In NTLMv2, EncryptedRandomSessionKey is the ExportedSessionKey RC4-encrypted with the |
742 | | * KeyExchangeKey */ |
743 | | |
744 | | /** |
745 | | * if (NegotiateFlags & NTLMSSP_NEGOTIATE_KEY_EXCH) |
746 | | * Set RandomSessionKey to RC4K(KeyExchangeKey, |
747 | | * AUTHENTICATE_MESSAGE.EncryptedRandomSessionKey) else Set RandomSessionKey to KeyExchangeKey |
748 | | */ |
749 | 0 | if (context->NegotiateKeyExchange) |
750 | 0 | { |
751 | 0 | WINPR_ASSERT(sizeof(context->EncryptedRandomSessionKey) == |
752 | 0 | sizeof(context->RandomSessionKey)); |
753 | 0 | return ntlm_rc4k(context->KeyExchangeKey, sizeof(context->EncryptedRandomSessionKey), |
754 | 0 | context->EncryptedRandomSessionKey, context->RandomSessionKey); |
755 | 0 | } |
756 | 0 | else |
757 | 0 | { |
758 | 0 | WINPR_ASSERT(sizeof(context->RandomSessionKey) == sizeof(context->KeyExchangeKey)); |
759 | 0 | CopyMemory(context->RandomSessionKey, context->KeyExchangeKey, |
760 | 0 | sizeof(context->RandomSessionKey)); |
761 | 0 | } |
762 | 0 | return TRUE; |
763 | 0 | } |
764 | | |
765 | | /** |
766 | | * Generate signing key msdn{cc236711} |
767 | | * |
768 | | * @param exported_session_key ExportedSessionKey |
769 | | * @param sign_magic Sign magic string |
770 | | * @param signing_key Destination signing key |
771 | | * |
772 | | * @return \b TRUE for success, \b FALSE for failure |
773 | | */ |
774 | | |
775 | | static BOOL ntlm_generate_signing_key(BYTE* exported_session_key, const SecBuffer* sign_magic, |
776 | | BYTE* signing_key) |
777 | 0 | { |
778 | 0 | BOOL rc = FALSE; |
779 | 0 | size_t length = 0; |
780 | 0 | BYTE* value = nullptr; |
781 | |
|
782 | 0 | WINPR_ASSERT(exported_session_key); |
783 | 0 | WINPR_ASSERT(sign_magic); |
784 | 0 | WINPR_ASSERT(signing_key); |
785 | |
|
786 | 0 | length = WINPR_MD5_DIGEST_LENGTH + sign_magic->cbBuffer; |
787 | 0 | value = (BYTE*)malloc(length); |
788 | |
|
789 | 0 | if (!value) |
790 | 0 | goto out; |
791 | | |
792 | | /* Concatenate ExportedSessionKey with sign magic */ |
793 | 0 | CopyMemory(value, exported_session_key, WINPR_MD5_DIGEST_LENGTH); |
794 | 0 | CopyMemory(&value[WINPR_MD5_DIGEST_LENGTH], sign_magic->pvBuffer, sign_magic->cbBuffer); |
795 | |
|
796 | 0 | rc = winpr_Digest(WINPR_MD_MD5, value, length, signing_key, WINPR_MD5_DIGEST_LENGTH); |
797 | |
|
798 | 0 | out: |
799 | 0 | free(value); |
800 | 0 | return rc; |
801 | 0 | } |
802 | | |
803 | | /** |
804 | | * Generate client signing key (ClientSigningKey). msdn{cc236711} |
805 | | * @param context A pointer to the NTLM context |
806 | | * |
807 | | * @return \b TRUE for success, \b FALSE for failure |
808 | | */ |
809 | | |
810 | | BOOL ntlm_generate_client_signing_key(NTLM_CONTEXT* context) |
811 | 0 | { |
812 | 0 | const SecBuffer signMagic = { sizeof(NTLM_CLIENT_SIGN_MAGIC), 0, NTLM_CLIENT_SIGN_MAGIC }; |
813 | |
|
814 | 0 | WINPR_ASSERT(context); |
815 | 0 | return ntlm_generate_signing_key(context->ExportedSessionKey, &signMagic, |
816 | 0 | context->ClientSigningKey); |
817 | 0 | } |
818 | | |
819 | | /** |
820 | | * Generate server signing key (ServerSigningKey). msdn{cc236711} |
821 | | * @param context A pointer to the NTLM context |
822 | | * |
823 | | * @return \b TRUE for success, \b FALSE for failure |
824 | | */ |
825 | | |
826 | | BOOL ntlm_generate_server_signing_key(NTLM_CONTEXT* context) |
827 | 0 | { |
828 | 0 | const SecBuffer signMagic = { sizeof(NTLM_SERVER_SIGN_MAGIC), 0, NTLM_SERVER_SIGN_MAGIC }; |
829 | |
|
830 | 0 | WINPR_ASSERT(context); |
831 | 0 | return ntlm_generate_signing_key(context->ExportedSessionKey, &signMagic, |
832 | 0 | context->ServerSigningKey); |
833 | 0 | } |
834 | | |
835 | | /** |
836 | | * Generate client sealing key (ClientSealingKey). msdn{cc236712} |
837 | | * @param context A pointer to the NTLM context |
838 | | * |
839 | | * @return \b TRUE for success, \b FALSE for failure |
840 | | */ |
841 | | |
842 | | BOOL ntlm_generate_client_sealing_key(NTLM_CONTEXT* context) |
843 | 0 | { |
844 | 0 | const SecBuffer sealMagic = { sizeof(NTLM_CLIENT_SEAL_MAGIC), 0, NTLM_CLIENT_SEAL_MAGIC }; |
845 | |
|
846 | 0 | WINPR_ASSERT(context); |
847 | 0 | return ntlm_generate_signing_key(context->ExportedSessionKey, &sealMagic, |
848 | 0 | context->ClientSealingKey); |
849 | 0 | } |
850 | | |
851 | | /** |
852 | | * Generate server sealing key (ServerSealingKey). msdn{cc236712} |
853 | | * @param context A pointer to the NTLM context |
854 | | * |
855 | | * @return \b TRUE for success, \b FALSE for failure |
856 | | */ |
857 | | |
858 | | BOOL ntlm_generate_server_sealing_key(NTLM_CONTEXT* context) |
859 | 0 | { |
860 | 0 | const SecBuffer sealMagic = { sizeof(NTLM_SERVER_SEAL_MAGIC), 0, NTLM_SERVER_SEAL_MAGIC }; |
861 | |
|
862 | 0 | WINPR_ASSERT(context); |
863 | 0 | return ntlm_generate_signing_key(context->ExportedSessionKey, &sealMagic, |
864 | 0 | context->ServerSealingKey); |
865 | 0 | } |
866 | | |
867 | | /** |
868 | | * Initialize RC4 stream cipher states for sealing. |
869 | | * @param context A pointer to the NTLM context |
870 | | */ |
871 | | |
872 | | BOOL ntlm_init_rc4_seal_states(NTLM_CONTEXT* context) |
873 | 0 | { |
874 | 0 | WINPR_ASSERT(context); |
875 | 0 | if (context->server) |
876 | 0 | { |
877 | 0 | context->SendSigningKey = context->ServerSigningKey; |
878 | 0 | context->RecvSigningKey = context->ClientSigningKey; |
879 | 0 | context->SendSealingKey = context->ClientSealingKey; |
880 | 0 | context->RecvSealingKey = context->ServerSealingKey; |
881 | 0 | context->SendRc4Seal = |
882 | 0 | winpr_RC4_New(context->ServerSealingKey, sizeof(context->ServerSealingKey)); |
883 | 0 | context->RecvRc4Seal = |
884 | 0 | winpr_RC4_New(context->ClientSealingKey, sizeof(context->ClientSealingKey)); |
885 | 0 | } |
886 | 0 | else |
887 | 0 | { |
888 | 0 | context->SendSigningKey = context->ClientSigningKey; |
889 | 0 | context->RecvSigningKey = context->ServerSigningKey; |
890 | 0 | context->SendSealingKey = context->ServerSealingKey; |
891 | 0 | context->RecvSealingKey = context->ClientSealingKey; |
892 | 0 | context->SendRc4Seal = |
893 | 0 | winpr_RC4_New(context->ClientSealingKey, sizeof(context->ClientSealingKey)); |
894 | 0 | context->RecvRc4Seal = |
895 | 0 | winpr_RC4_New(context->ServerSealingKey, sizeof(context->ServerSealingKey)); |
896 | 0 | } |
897 | 0 | if (!context->SendRc4Seal) |
898 | 0 | { |
899 | 0 | WLog_ERR(TAG, "Failed to allocate context->SendRc4Seal"); |
900 | 0 | return FALSE; |
901 | 0 | } |
902 | 0 | if (!context->RecvRc4Seal) |
903 | 0 | { |
904 | 0 | WLog_ERR(TAG, "Failed to allocate context->RecvRc4Seal"); |
905 | 0 | return FALSE; |
906 | 0 | } |
907 | 0 | return TRUE; |
908 | 0 | } |
909 | | |
910 | | BOOL ntlm_compute_message_integrity_check(NTLM_CONTEXT* context, BYTE* mic, UINT32 size) |
911 | 0 | { |
912 | 0 | BOOL rc = FALSE; |
913 | | /* |
914 | | * Compute the HMAC-MD5 hash of ConcatenationOf(NEGOTIATE_MESSAGE, |
915 | | * CHALLENGE_MESSAGE, AUTHENTICATE_MESSAGE) using the ExportedSessionKey |
916 | | */ |
917 | 0 | WINPR_HMAC_CTX* hmac = winpr_HMAC_New(); |
918 | |
|
919 | 0 | WINPR_ASSERT(context); |
920 | 0 | WINPR_ASSERT(mic); |
921 | 0 | WINPR_ASSERT(size >= WINPR_MD5_DIGEST_LENGTH); |
922 | |
|
923 | 0 | memset(mic, 0, size); |
924 | 0 | if (!hmac) |
925 | 0 | return FALSE; |
926 | | |
927 | 0 | if (!winpr_HMAC_Init(hmac, WINPR_MD_MD5, context->ExportedSessionKey, WINPR_MD5_DIGEST_LENGTH)) |
928 | 0 | goto fail; |
929 | | |
930 | 0 | if (!winpr_HMAC_Update(hmac, (BYTE*)context->NegotiateMessage.pvBuffer, |
931 | 0 | context->NegotiateMessage.cbBuffer)) |
932 | 0 | goto fail; |
933 | 0 | if (!winpr_HMAC_Update(hmac, (BYTE*)context->ChallengeMessage.pvBuffer, |
934 | 0 | context->ChallengeMessage.cbBuffer)) |
935 | 0 | goto fail; |
936 | | |
937 | 0 | if (context->MessageIntegrityCheckOffset > 0) |
938 | 0 | { |
939 | 0 | const BYTE* auth = (BYTE*)context->AuthenticateMessage.pvBuffer; |
940 | 0 | const BYTE data[WINPR_MD5_DIGEST_LENGTH] = WINPR_C_ARRAY_INIT; |
941 | 0 | const size_t rest = context->MessageIntegrityCheckOffset + sizeof(data); |
942 | |
|
943 | 0 | WINPR_ASSERT(rest <= context->AuthenticateMessage.cbBuffer); |
944 | 0 | if (!winpr_HMAC_Update(hmac, &auth[0], context->MessageIntegrityCheckOffset)) |
945 | 0 | goto fail; |
946 | 0 | if (!winpr_HMAC_Update(hmac, data, sizeof(data))) |
947 | 0 | goto fail; |
948 | 0 | if (!winpr_HMAC_Update(hmac, &auth[rest], context->AuthenticateMessage.cbBuffer - rest)) |
949 | 0 | goto fail; |
950 | 0 | } |
951 | 0 | else |
952 | 0 | { |
953 | 0 | if (!winpr_HMAC_Update(hmac, (BYTE*)context->AuthenticateMessage.pvBuffer, |
954 | 0 | context->AuthenticateMessage.cbBuffer)) |
955 | 0 | goto fail; |
956 | 0 | } |
957 | 0 | rc = winpr_HMAC_Final(hmac, mic, WINPR_MD5_DIGEST_LENGTH); |
958 | |
|
959 | 0 | fail: |
960 | 0 | winpr_HMAC_Free(hmac); |
961 | 0 | return rc; |
962 | 0 | } |