/src/FreeRDP/winpr/libwinpr/sspi/NTLM/ntlm.c
Line | Count | Source |
1 | | /** |
2 | | * WinPR: Windows Portable Runtime |
3 | | * NTLM Security Package |
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/crt.h> |
23 | | #include <winpr/assert.h> |
24 | | #include <winpr/sspi.h> |
25 | | #include <winpr/print.h> |
26 | | #include <winpr/string.h> |
27 | | #include <winpr/tchar.h> |
28 | | #include <winpr/sysinfo.h> |
29 | | #include <winpr/registry.h> |
30 | | #include <winpr/endian.h> |
31 | | #include <winpr/build-config.h> |
32 | | |
33 | | #include "ntlm.h" |
34 | | #include "ntlm_export.h" |
35 | | #include "../sspi.h" |
36 | | |
37 | | #include "ntlm_message.h" |
38 | | |
39 | | #include "../../utils.h" |
40 | | |
41 | | #include "../../log.h" |
42 | 0 | #define TAG WINPR_TAG("sspi.NTLM") |
43 | | |
44 | | #ifndef MIN |
45 | 0 | #define MIN(a, b) ((a) < (b)) ? (a) : (b) |
46 | | #endif |
47 | | |
48 | 0 | #define WINPR_KEY "Software\\%s\\WinPR\\NTLM" |
49 | | |
50 | | static char* NTLM_PACKAGE_NAME = "NTLM"; |
51 | | |
52 | 0 | #define check_context(ctx) check_context_((ctx), __FILE__, __func__, __LINE__) |
53 | | |
54 | | WINPR_ATTR_NODISCARD |
55 | | static BOOL check_context_(NTLM_CONTEXT* context, const char* file, const char* fkt, size_t line) |
56 | 0 | { |
57 | 0 | BOOL rc = TRUE; |
58 | 0 | wLog* log = WLog_Get(TAG); |
59 | 0 | const DWORD log_level = WLOG_ERROR; |
60 | |
|
61 | 0 | if (!context) |
62 | 0 | { |
63 | 0 | if (WLog_IsLevelActive(log, log_level)) |
64 | 0 | WLog_PrintTextMessage(log, log_level, line, file, fkt, "invalid context"); |
65 | |
|
66 | 0 | return FALSE; |
67 | 0 | } |
68 | | |
69 | 0 | if (!context->RecvRc4Seal) |
70 | 0 | { |
71 | 0 | if (WLog_IsLevelActive(log, log_level)) |
72 | 0 | WLog_PrintTextMessage(log, log_level, line, file, fkt, "invalid context->RecvRc4Seal"); |
73 | 0 | rc = FALSE; |
74 | 0 | } |
75 | 0 | if (!context->SendRc4Seal) |
76 | 0 | { |
77 | 0 | if (WLog_IsLevelActive(log, log_level)) |
78 | 0 | WLog_PrintTextMessage(log, log_level, line, file, fkt, "invalid context->SendRc4Seal"); |
79 | 0 | rc = FALSE; |
80 | 0 | } |
81 | |
|
82 | 0 | if (!context->SendSigningKey) |
83 | 0 | { |
84 | 0 | if (WLog_IsLevelActive(log, log_level)) |
85 | 0 | WLog_PrintTextMessage(log, log_level, line, file, fkt, |
86 | 0 | "invalid context->SendSigningKey"); |
87 | 0 | rc = FALSE; |
88 | 0 | } |
89 | 0 | if (!context->RecvSigningKey) |
90 | 0 | { |
91 | 0 | if (WLog_IsLevelActive(log, log_level)) |
92 | 0 | WLog_PrintTextMessage(log, log_level, line, file, fkt, |
93 | 0 | "invalid context->RecvSigningKey"); |
94 | 0 | rc = FALSE; |
95 | 0 | } |
96 | 0 | if (!context->SendSealingKey) |
97 | 0 | { |
98 | 0 | if (WLog_IsLevelActive(log, log_level)) |
99 | 0 | WLog_PrintTextMessage(log, log_level, line, file, fkt, |
100 | 0 | "invalid context->SendSealingKey"); |
101 | 0 | rc = FALSE; |
102 | 0 | } |
103 | 0 | if (!context->RecvSealingKey) |
104 | 0 | { |
105 | 0 | if (WLog_IsLevelActive(log, log_level)) |
106 | 0 | WLog_PrintTextMessage(log, log_level, line, file, fkt, |
107 | 0 | "invalid context->RecvSealingKey"); |
108 | 0 | rc = FALSE; |
109 | 0 | } |
110 | 0 | return rc; |
111 | 0 | } |
112 | | |
113 | | WINPR_ATTR_MALLOC(free, 1) |
114 | | static char* get_computer_name(COMPUTER_NAME_FORMAT type, size_t* pSize) |
115 | 0 | { |
116 | 0 | DWORD nSize = 0; |
117 | |
|
118 | 0 | if (pSize) |
119 | 0 | *pSize = 0; |
120 | |
|
121 | 0 | if (GetComputerNameExA(type, nullptr, &nSize)) |
122 | 0 | return nullptr; |
123 | | |
124 | 0 | if (GetLastError() != ERROR_MORE_DATA) |
125 | 0 | return nullptr; |
126 | | |
127 | 0 | char* computerName = calloc(1, nSize); |
128 | |
|
129 | 0 | if (!computerName) |
130 | 0 | return nullptr; |
131 | | |
132 | 0 | if (!GetComputerNameExA(type, computerName, &nSize)) |
133 | 0 | { |
134 | 0 | free(computerName); |
135 | 0 | return nullptr; |
136 | 0 | } |
137 | | |
138 | 0 | if (pSize) |
139 | 0 | *pSize = nSize; |
140 | 0 | return computerName; |
141 | 0 | } |
142 | | |
143 | | WINPR_ATTR_NODISCARD |
144 | | SECURITY_STATUS ntlm_SetContextWorkstationX(NTLM_CONTEXT* context, BOOL unicode, const void* data, |
145 | | size_t length) |
146 | 0 | { |
147 | 0 | WINPR_ASSERT(context); |
148 | 0 | ntlm_free_unicode_string(&context->Workstation); |
149 | |
|
150 | 0 | if (length == 0) |
151 | 0 | return SEC_E_OK; |
152 | | |
153 | 0 | WINPR_ASSERT(data); |
154 | 0 | if (unicode) |
155 | 0 | context->Workstation = ntlm_from_unicode_string_w(data, length / sizeof(WCHAR)); |
156 | 0 | else |
157 | 0 | context->Workstation = ntlm_from_unicode_string_utf8(data, length); |
158 | |
|
159 | 0 | if (ntlm_is_unicode_string_empty(&context->Workstation)) |
160 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
161 | | |
162 | 0 | return SEC_E_OK; |
163 | 0 | } |
164 | | |
165 | | WINPR_ATTR_NODISCARD |
166 | | static int ntlm_SetContextWorkstation(NTLM_CONTEXT* context, const char* Workstation) |
167 | 0 | { |
168 | 0 | const char* ws = Workstation; |
169 | 0 | CHAR* computerName = nullptr; |
170 | |
|
171 | 0 | if (!Workstation) |
172 | 0 | { |
173 | 0 | computerName = get_computer_name(ComputerNameNetBIOS, nullptr); |
174 | 0 | if (!computerName) |
175 | 0 | return -1; |
176 | 0 | ws = computerName; |
177 | 0 | } |
178 | | |
179 | 0 | const size_t len = strlen(ws); |
180 | 0 | const SECURITY_STATUS status = ntlm_SetContextWorkstationX(context, FALSE, ws, len); |
181 | 0 | free(computerName); |
182 | |
|
183 | 0 | return (status == SEC_E_OK) ? 1 : -1; |
184 | 0 | } |
185 | | |
186 | | WINPR_ATTR_NODISCARD |
187 | | static int ntlm_SetContextServicePrincipalNameW(NTLM_CONTEXT* context, LPWSTR ServicePrincipalName) |
188 | 0 | { |
189 | 0 | WINPR_ASSERT(context); |
190 | |
|
191 | 0 | ntlm_free_unicode_string(&context->ServicePrincipalName); |
192 | 0 | if (!ServicePrincipalName) |
193 | 0 | return 1; |
194 | | |
195 | 0 | const size_t len = _wcslen(ServicePrincipalName); |
196 | 0 | context->ServicePrincipalName = ntlm_from_unicode_string_w(ServicePrincipalName, len); |
197 | 0 | if (ntlm_is_unicode_string_empty(&context->ServicePrincipalName)) |
198 | 0 | return -1; |
199 | | |
200 | 0 | return 1; |
201 | 0 | } |
202 | | |
203 | | WINPR_ATTR_NODISCARD |
204 | | static int ntlm_SetContextTargetName(NTLM_CONTEXT* context, char* TargetName) |
205 | 0 | { |
206 | 0 | char* name = TargetName; |
207 | 0 | WINPR_ASSERT(context); |
208 | |
|
209 | 0 | if (!name) |
210 | 0 | { |
211 | 0 | size_t nSize = 0; |
212 | 0 | char* computerName = get_computer_name(ComputerNameNetBIOS, &nSize); |
213 | |
|
214 | 0 | if (!computerName) |
215 | 0 | return -1; |
216 | | |
217 | 0 | if (nSize > MAX_COMPUTERNAME_LENGTH) |
218 | 0 | computerName[MAX_COMPUTERNAME_LENGTH] = '\0'; |
219 | |
|
220 | 0 | name = computerName; |
221 | |
|
222 | 0 | if (!name) |
223 | 0 | return -1; |
224 | | |
225 | 0 | CharUpperA(name); |
226 | 0 | } |
227 | | |
228 | 0 | size_t len = 0; |
229 | 0 | sspi_SecBufferFree(&context->TargetName); |
230 | 0 | context->TargetName.pvBuffer = ConvertUtf8ToWCharAlloc(name, &len); |
231 | |
|
232 | 0 | if (!context->TargetName.pvBuffer || (len > UINT16_MAX / sizeof(WCHAR))) |
233 | 0 | { |
234 | 0 | free(context->TargetName.pvBuffer); |
235 | 0 | context->TargetName.pvBuffer = nullptr; |
236 | |
|
237 | 0 | if (!TargetName) |
238 | 0 | free(name); |
239 | |
|
240 | 0 | return -1; |
241 | 0 | } |
242 | | |
243 | 0 | context->TargetName.cbBuffer = (USHORT)(len * sizeof(WCHAR)); |
244 | |
|
245 | 0 | if (!TargetName) |
246 | 0 | free(name); |
247 | |
|
248 | 0 | return 1; |
249 | 0 | } |
250 | | |
251 | | static void ntlm_ContextFree(NTLM_CONTEXT* context) |
252 | 0 | { |
253 | 0 | if (!context) |
254 | 0 | return; |
255 | | |
256 | 0 | winpr_RC4_Free(context->SendRc4Seal); |
257 | 0 | winpr_RC4_Free(context->RecvRc4Seal); |
258 | 0 | sspi_SecBufferFree(&context->NegotiateMessage); |
259 | 0 | sspi_SecBufferFree(&context->ChallengeMessage); |
260 | 0 | sspi_SecBufferFree(&context->AuthenticateMessage); |
261 | 0 | sspi_SecBufferFree(&context->ChallengeTargetInfo); |
262 | 0 | sspi_SecBufferFree(&context->AuthenticateTargetInfo); |
263 | 0 | sspi_SecBufferFree(&context->TargetName); |
264 | 0 | sspi_SecBufferFree(&context->NtChallengeResponse); |
265 | 0 | sspi_SecBufferFree(&context->LmChallengeResponse); |
266 | 0 | ntlm_free_unicode_string(&context->ServicePrincipalName); |
267 | 0 | ntlm_free_unicode_string(&context->Workstation); |
268 | 0 | ntlm_free_unicode_string(&context->NbComputerName); |
269 | 0 | ntlm_free_unicode_string(&context->NbDomainName); |
270 | 0 | ntlm_free_unicode_string(&context->DnsComputerName); |
271 | 0 | ntlm_free_unicode_string(&context->DnsDomainName); |
272 | |
|
273 | 0 | ntlm_free_messages(context); |
274 | | |
275 | | /* Zero sensitive key material before freeing the context */ |
276 | 0 | memset(context->NtlmHash, 0, sizeof(context->NtlmHash)); |
277 | 0 | memset(context->NtlmV2Hash, 0, sizeof(context->NtlmV2Hash)); |
278 | 0 | memset(context->SessionBaseKey, 0, sizeof(context->SessionBaseKey)); |
279 | 0 | memset(context->KeyExchangeKey, 0, sizeof(context->KeyExchangeKey)); |
280 | 0 | memset(context->RandomSessionKey, 0, sizeof(context->RandomSessionKey)); |
281 | 0 | memset(context->ExportedSessionKey, 0, sizeof(context->ExportedSessionKey)); |
282 | 0 | memset(context->EncryptedRandomSessionKey, 0, sizeof(context->EncryptedRandomSessionKey)); |
283 | 0 | memset(context->NtProofString, 0, sizeof(context->NtProofString)); |
284 | 0 | free(context); |
285 | 0 | } |
286 | | |
287 | | WINPR_ATTR_NODISCARD |
288 | | static int ntlm_get_target_computer_name(PUNICODE_STRING pName, |
289 | | WINPR_ATTR_UNUSED COMPUTER_NAME_FORMAT type) |
290 | 0 | { |
291 | 0 | WINPR_ASSERT(pName); |
292 | 0 | ntlm_free_unicode_string(pName); |
293 | |
|
294 | 0 | size_t len = 0; |
295 | 0 | char* name = get_computer_name(ComputerNameNetBIOS, &len); |
296 | 0 | if (!name) |
297 | 0 | return -1; |
298 | | |
299 | 0 | CharUpperA(name); |
300 | |
|
301 | 0 | *pName = ntlm_from_unicode_string_utf8(name, len); |
302 | 0 | free(name); |
303 | |
|
304 | 0 | return !ntlm_is_unicode_string_empty(pName); |
305 | 0 | } |
306 | | |
307 | | WINPR_ATTR_NODISCARD |
308 | | static BOOL ntlm_ContextFillDefaultNames(NTLM_CONTEXT* context) |
309 | 0 | { |
310 | 0 | WINPR_ASSERT(context); |
311 | |
|
312 | 0 | if (ntlm_SetContextWorkstation(context, nullptr) < 0) |
313 | 0 | return FALSE; |
314 | | |
315 | 0 | if (ntlm_get_target_computer_name(&context->NbDomainName, ComputerNameNetBIOS) < 0) |
316 | 0 | return FALSE; |
317 | | |
318 | 0 | if (ntlm_get_target_computer_name(&context->NbComputerName, ComputerNameNetBIOS) < 0) |
319 | 0 | return FALSE; |
320 | | |
321 | 0 | if (ntlm_get_target_computer_name(&context->DnsDomainName, ComputerNameDnsDomain) < 0) |
322 | 0 | return FALSE; |
323 | | |
324 | 0 | if (ntlm_get_target_computer_name(&context->DnsComputerName, ComputerNameDnsHostname) < 0) |
325 | 0 | return FALSE; |
326 | 0 | return TRUE; |
327 | 0 | } |
328 | | |
329 | | WINPR_ATTR_NODISCARD |
330 | | static BOOL ntlm_try_set_from_registry(HKEY hKey, const char* key, UNICODE_STRING* ustr) |
331 | 0 | { |
332 | 0 | WINPR_ASSERT(hKey); |
333 | 0 | WINPR_ASSERT(key); |
334 | |
|
335 | 0 | UNICODE_STRING str = WINPR_C_ARRAY_INIT; |
336 | |
|
337 | 0 | WCHAR wkey[64] = WINPR_C_ARRAY_INIT; |
338 | 0 | const SSIZE_T res = ConvertUtf8ToWChar(key, wkey, ARRAYSIZE(wkey)); |
339 | 0 | if (res < 0) |
340 | 0 | goto fail; |
341 | 0 | WINPR_ASSERT((size_t)res < ARRAYSIZE(wkey)); |
342 | |
|
343 | 0 | DWORD dwSize = 0; |
344 | 0 | DWORD dwType = 0; |
345 | 0 | if (RegQueryValueExW(hKey, wkey, nullptr, &dwType, nullptr, &dwSize) != ERROR_SUCCESS) |
346 | 0 | goto fail; |
347 | | |
348 | 0 | if ((dwSize > UINT16_MAX) || ((dwSize % 2) != 0)) |
349 | 0 | goto fail; |
350 | | |
351 | 0 | str.Buffer = calloc(dwSize / sizeof(WCHAR) + 1, sizeof(WCHAR)); |
352 | 0 | if (!str.Buffer) |
353 | 0 | goto fail; |
354 | 0 | str.Length = WINPR_ASSERTING_INT_CAST(UINT16, dwSize); |
355 | 0 | str.MaximumLength = WINPR_ASSERTING_INT_CAST(UINT16, dwSize); |
356 | |
|
357 | 0 | const LONG rc = RegQueryValueExW(hKey, wkey, nullptr, &dwType, (BYTE*)str.Buffer, &dwSize); |
358 | 0 | if (rc != ERROR_SUCCESS) |
359 | 0 | goto fail; |
360 | 0 | ntlm_free_unicode_string(ustr); |
361 | 0 | *ustr = str; |
362 | 0 | return TRUE; |
363 | | |
364 | 0 | fail: |
365 | 0 | ntlm_free_unicode_string(&str); |
366 | 0 | return FALSE; |
367 | 0 | } |
368 | | |
369 | | WINPR_ATTR_NODISCARD |
370 | | static BOOL ntlm_ContextFromConfig(NTLM_CONTEXT* context) |
371 | 0 | { |
372 | 0 | { |
373 | 0 | WINPR_ASSERT(context); |
374 | |
|
375 | 0 | char* key = winpr_getApplicatonDetailsRegKey(WINPR_KEY); |
376 | 0 | if (key) |
377 | 0 | { |
378 | 0 | HKEY hKey = nullptr; |
379 | |
|
380 | 0 | const LONG status = |
381 | 0 | RegOpenKeyExA(HKEY_LOCAL_MACHINE, key, 0, KEY_READ | KEY_WOW64_64KEY, &hKey); |
382 | 0 | free(key); |
383 | |
|
384 | 0 | if (status == ERROR_SUCCESS) |
385 | 0 | { |
386 | 0 | DWORD dwValue = 0; |
387 | 0 | DWORD dwSize = 0; |
388 | 0 | DWORD dwType = 0; |
389 | |
|
390 | 0 | if (RegQueryValueEx(hKey, _T("NTLMv2"), nullptr, &dwType, (BYTE*)&dwValue, |
391 | 0 | &dwSize) == ERROR_SUCCESS) |
392 | 0 | context->NTLMv2 = dwValue ? 1 : 0; |
393 | |
|
394 | 0 | if (RegQueryValueEx(hKey, _T("UseMIC"), nullptr, &dwType, (BYTE*)&dwValue, |
395 | 0 | &dwSize) == ERROR_SUCCESS) |
396 | 0 | context->UseMIC = dwValue ? 1 : 0; |
397 | |
|
398 | 0 | if (RegQueryValueEx(hKey, _T("SendVersionInfo"), nullptr, &dwType, (BYTE*)&dwValue, |
399 | 0 | &dwSize) == ERROR_SUCCESS) |
400 | 0 | context->SendVersionInfo = dwValue ? 1 : 0; |
401 | |
|
402 | 0 | if (RegQueryValueEx(hKey, _T("SendSingleHostData"), nullptr, &dwType, |
403 | 0 | (BYTE*)&dwValue, &dwSize) == ERROR_SUCCESS) |
404 | 0 | context->SendSingleHostData = dwValue ? 1 : 0; |
405 | |
|
406 | 0 | if (RegQueryValueEx(hKey, _T("SendWorkstationName"), nullptr, &dwType, |
407 | 0 | (BYTE*)&dwValue, &dwSize) == ERROR_SUCCESS) |
408 | 0 | context->SendWorkstationName = dwValue ? 1 : 0; |
409 | |
|
410 | 0 | (void)ntlm_try_set_from_registry(hKey, "WorkstationName", &context->Workstation); |
411 | 0 | (void)ntlm_try_set_from_registry(hKey, "NbDomainName", &context->NbDomainName); |
412 | 0 | (void)ntlm_try_set_from_registry(hKey, "NbComputerName", &context->NbComputerName); |
413 | 0 | (void)ntlm_try_set_from_registry(hKey, "DnsDomainName", &context->DnsDomainName); |
414 | 0 | (void)ntlm_try_set_from_registry(hKey, "DnsComputerName", |
415 | 0 | &context->DnsComputerName); |
416 | |
|
417 | 0 | RegCloseKey(hKey); |
418 | 0 | } |
419 | 0 | } |
420 | 0 | } |
421 | |
|
422 | 0 | HKEY hKey = nullptr; |
423 | 0 | const LONG status = |
424 | 0 | RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("System\\CurrentControlSet\\Control\\LSA"), 0, |
425 | 0 | KEY_READ | KEY_WOW64_64KEY, &hKey); |
426 | |
|
427 | 0 | if (status == ERROR_SUCCESS) |
428 | 0 | { |
429 | 0 | DWORD dwType = 0; |
430 | 0 | DWORD dwSize = 0; |
431 | 0 | DWORD dwValue = 0; |
432 | 0 | if (RegQueryValueEx(hKey, _T("SuppressExtendedProtection"), nullptr, &dwType, |
433 | 0 | (BYTE*)&dwValue, &dwSize) == ERROR_SUCCESS) |
434 | 0 | context->SuppressExtendedProtection = dwValue ? 1 : 0; |
435 | |
|
436 | 0 | RegCloseKey(hKey); |
437 | 0 | } |
438 | | |
439 | | /* |
440 | | * Extended Protection is enabled by default in Windows 7, |
441 | | * but enabling it in WinPR breaks TS Gateway at this point |
442 | | */ |
443 | 0 | context->SuppressExtendedProtection = FALSE; |
444 | 0 | return TRUE; |
445 | 0 | } |
446 | | |
447 | | WINPR_ATTR_MALLOC(ntlm_ContextFree, 1) |
448 | | static NTLM_CONTEXT* ntlm_ContextNew(void) |
449 | 0 | { |
450 | 0 | NTLM_CONTEXT* context = (NTLM_CONTEXT*)calloc(1, sizeof(NTLM_CONTEXT)); |
451 | |
|
452 | 0 | if (!context) |
453 | 0 | return nullptr; |
454 | | |
455 | 0 | context->NTLMv2 = TRUE; |
456 | 0 | context->UseMIC = FALSE; |
457 | 0 | context->SendVersionInfo = TRUE; |
458 | 0 | context->SendSingleHostData = FALSE; |
459 | 0 | context->SendWorkstationName = TRUE; |
460 | 0 | context->NegotiateKeyExchange = TRUE; |
461 | 0 | context->UseSamFileDatabase = TRUE; |
462 | |
|
463 | 0 | context->NegotiateFlags = 0; |
464 | 0 | context->LmCompatibilityLevel = 3; |
465 | 0 | ntlm_change_state(context, NTLM_STATE_INITIAL); |
466 | 0 | FillMemory(context->MachineID, sizeof(context->MachineID), 0xAA); |
467 | |
|
468 | 0 | if (context->NTLMv2) |
469 | 0 | context->UseMIC = TRUE; |
470 | |
|
471 | 0 | if (!ntlm_ContextFillDefaultNames(context)) |
472 | 0 | goto fail; |
473 | 0 | if (!ntlm_ContextFromConfig(context)) |
474 | 0 | goto fail; |
475 | | |
476 | 0 | return context; |
477 | | |
478 | 0 | fail: |
479 | 0 | ntlm_ContextFree(context); |
480 | 0 | return nullptr; |
481 | 0 | } |
482 | | |
483 | | WINPR_ATTR_NODISCARD |
484 | | static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleW( |
485 | | WINPR_ATTR_UNUSED SEC_WCHAR* pszPrincipal, WINPR_ATTR_UNUSED SEC_WCHAR* pszPackage, |
486 | | ULONG fCredentialUse, WINPR_ATTR_UNUSED void* pvLogonID, void* pAuthData, |
487 | | SEC_GET_KEY_FN pGetKeyFn, void* pvGetKeyArgument, PCredHandle phCredential, |
488 | | WINPR_ATTR_UNUSED PTimeStamp ptsExpiry) |
489 | 0 | { |
490 | 0 | SEC_WINPR_NTLM_SETTINGS* settings = nullptr; |
491 | |
|
492 | 0 | if ((fCredentialUse != SECPKG_CRED_OUTBOUND) && (fCredentialUse != SECPKG_CRED_INBOUND) && |
493 | 0 | (fCredentialUse != SECPKG_CRED_BOTH)) |
494 | 0 | { |
495 | 0 | return SEC_E_INVALID_PARAMETER; |
496 | 0 | } |
497 | | |
498 | 0 | SSPI_CREDENTIALS* credentials = sspi_CredentialsNew(); |
499 | |
|
500 | 0 | if (!credentials) |
501 | 0 | return SEC_E_INTERNAL_ERROR; |
502 | | |
503 | 0 | credentials->fCredentialUse = fCredentialUse; |
504 | 0 | credentials->pGetKeyFn = pGetKeyFn; |
505 | 0 | credentials->pvGetKeyArgument = pvGetKeyArgument; |
506 | |
|
507 | 0 | if (pAuthData) |
508 | 0 | { |
509 | 0 | UINT32 identityFlags = sspi_GetAuthIdentityFlags(pAuthData); |
510 | |
|
511 | 0 | if (sspi_CopyAuthIdentity(&(credentials->identity), |
512 | 0 | (const SEC_WINNT_AUTH_IDENTITY_INFO*)pAuthData) < 0) |
513 | 0 | { |
514 | 0 | sspi_CredentialsFree(credentials); |
515 | 0 | return SEC_E_INVALID_PARAMETER; |
516 | 0 | } |
517 | | |
518 | 0 | if (identityFlags & SEC_WINNT_AUTH_IDENTITY_EXTENDED) |
519 | 0 | settings = (((SEC_WINNT_AUTH_IDENTITY_WINPR*)pAuthData)->ntlmSettings); |
520 | 0 | } |
521 | | |
522 | 0 | if (settings) |
523 | 0 | { |
524 | 0 | if (settings->samFile) |
525 | 0 | { |
526 | 0 | credentials->ntlmSettings.samFile = _strdup(settings->samFile); |
527 | 0 | if (!credentials->ntlmSettings.samFile) |
528 | 0 | { |
529 | 0 | sspi_CredentialsFree(credentials); |
530 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
531 | 0 | } |
532 | 0 | } |
533 | 0 | credentials->ntlmSettings.hashCallback = settings->hashCallback; |
534 | 0 | credentials->ntlmSettings.hashCallbackArg = settings->hashCallbackArg; |
535 | 0 | } |
536 | | |
537 | 0 | sspi_SecureHandleSetLowerPointer(phCredential, (void*)credentials); |
538 | 0 | sspi_SecureHandleSetUpperPointer(phCredential, (void*)NTLM_PACKAGE_NAME); |
539 | 0 | return SEC_E_OK; |
540 | 0 | } |
541 | | |
542 | | WINPR_ATTR_NODISCARD |
543 | | static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleA( |
544 | | SEC_CHAR* pszPrincipal, SEC_CHAR* pszPackage, ULONG fCredentialUse, void* pvLogonID, |
545 | | void* pAuthData, SEC_GET_KEY_FN pGetKeyFn, void* pvGetKeyArgument, PCredHandle phCredential, |
546 | | PTimeStamp ptsExpiry) |
547 | 0 | { |
548 | 0 | SECURITY_STATUS status = SEC_E_INSUFFICIENT_MEMORY; |
549 | 0 | SEC_WCHAR* principal = nullptr; |
550 | 0 | SEC_WCHAR* package = nullptr; |
551 | |
|
552 | 0 | if (pszPrincipal) |
553 | 0 | { |
554 | 0 | principal = ConvertUtf8ToWCharAlloc(pszPrincipal, nullptr); |
555 | 0 | if (!principal) |
556 | 0 | goto fail; |
557 | 0 | } |
558 | 0 | if (pszPackage) |
559 | 0 | { |
560 | 0 | package = ConvertUtf8ToWCharAlloc(pszPackage, nullptr); |
561 | 0 | if (!package) |
562 | 0 | goto fail; |
563 | 0 | } |
564 | | |
565 | 0 | status = |
566 | 0 | ntlm_AcquireCredentialsHandleW(principal, package, fCredentialUse, pvLogonID, pAuthData, |
567 | 0 | pGetKeyFn, pvGetKeyArgument, phCredential, ptsExpiry); |
568 | |
|
569 | 0 | fail: |
570 | 0 | free(principal); |
571 | 0 | free(package); |
572 | |
|
573 | 0 | return status; |
574 | 0 | } |
575 | | |
576 | | WINPR_ATTR_NODISCARD |
577 | | static SECURITY_STATUS SEC_ENTRY ntlm_FreeCredentialsHandle(PCredHandle phCredential) |
578 | 0 | { |
579 | 0 | if (!phCredential) |
580 | 0 | return SEC_E_INVALID_HANDLE; |
581 | | |
582 | 0 | SSPI_CREDENTIALS* credentials = |
583 | 0 | (SSPI_CREDENTIALS*)sspi_SecureHandleGetLowerPointer(phCredential); |
584 | 0 | sspi_SecureHandleInvalidate(phCredential); |
585 | 0 | if (!credentials) |
586 | 0 | return SEC_E_INVALID_HANDLE; |
587 | | |
588 | 0 | sspi_CredentialsFree(credentials); |
589 | 0 | return SEC_E_OK; |
590 | 0 | } |
591 | | |
592 | | WINPR_ATTR_NODISCARD |
593 | | static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesW( |
594 | | WINPR_ATTR_UNUSED PCredHandle phCredential, WINPR_ATTR_UNUSED ULONG ulAttribute, |
595 | | WINPR_ATTR_UNUSED void* pBuffer) |
596 | 0 | { |
597 | 0 | if (ulAttribute == SECPKG_CRED_ATTR_NAMES) |
598 | 0 | { |
599 | 0 | return SEC_E_OK; |
600 | 0 | } |
601 | | |
602 | 0 | WLog_ERR(TAG, "TODO: Implement"); |
603 | 0 | return SEC_E_UNSUPPORTED_FUNCTION; |
604 | 0 | } |
605 | | |
606 | | WINPR_ATTR_NODISCARD |
607 | | static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesA(PCredHandle phCredential, |
608 | | ULONG ulAttribute, void* pBuffer) |
609 | 0 | { |
610 | 0 | return ntlm_QueryCredentialsAttributesW(phCredential, ulAttribute, pBuffer); |
611 | 0 | } |
612 | | |
613 | | /** |
614 | | * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa374707 |
615 | | */ |
616 | | WINPR_ATTR_NODISCARD |
617 | | static SECURITY_STATUS SEC_ENTRY ntlm_AcceptSecurityContext( |
618 | | PCredHandle phCredential, PCtxtHandle phContext, PSecBufferDesc pInput, ULONG fContextReq, |
619 | | WINPR_ATTR_UNUSED ULONG TargetDataRep, PCtxtHandle phNewContext, PSecBufferDesc pOutput, |
620 | | WINPR_ATTR_UNUSED PULONG pfContextAttr, WINPR_ATTR_UNUSED PTimeStamp ptsTimeStamp) |
621 | 0 | { |
622 | 0 | SECURITY_STATUS status = 0; |
623 | 0 | SSPI_CREDENTIALS* credentials = nullptr; |
624 | 0 | PSecBuffer input_buffer = nullptr; |
625 | 0 | PSecBuffer output_buffer = nullptr; |
626 | | |
627 | | /* behave like windows SSPIs that don't want empty context */ |
628 | 0 | if (phContext && !phContext->dwLower && !phContext->dwUpper) |
629 | 0 | return SEC_E_INVALID_HANDLE; |
630 | | |
631 | 0 | NTLM_CONTEXT* context = (NTLM_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext); |
632 | |
|
633 | 0 | if (!context) |
634 | 0 | { |
635 | 0 | context = ntlm_ContextNew(); |
636 | |
|
637 | 0 | if (!context) |
638 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
639 | | |
640 | 0 | context->server = TRUE; |
641 | |
|
642 | 0 | if (fContextReq & ASC_REQ_CONFIDENTIALITY) |
643 | 0 | context->confidentiality = TRUE; |
644 | |
|
645 | 0 | credentials = (SSPI_CREDENTIALS*)sspi_SecureHandleGetLowerPointer(phCredential); |
646 | 0 | context->credentials = credentials; |
647 | 0 | context->SamFile = credentials->ntlmSettings.samFile; |
648 | 0 | context->HashCallback = credentials->ntlmSettings.hashCallback; |
649 | 0 | context->HashCallbackArg = credentials->ntlmSettings.hashCallbackArg; |
650 | |
|
651 | 0 | if (!ntlm_SetContextTargetName(context, nullptr)) |
652 | 0 | return SEC_E_INVALID_HANDLE; |
653 | 0 | sspi_SecureHandleSetLowerPointer(phNewContext, context); |
654 | 0 | sspi_SecureHandleSetUpperPointer(phNewContext, (void*)NTLM_PACKAGE_NAME); |
655 | 0 | } |
656 | | |
657 | 0 | switch (ntlm_get_state(context)) |
658 | 0 | { |
659 | 0 | case NTLM_STATE_INITIAL: |
660 | 0 | { |
661 | 0 | ntlm_change_state(context, NTLM_STATE_NEGOTIATE); |
662 | |
|
663 | 0 | if (!pInput) |
664 | 0 | return SEC_E_INVALID_TOKEN; |
665 | | |
666 | 0 | if (pInput->cBuffers < 1) |
667 | 0 | return SEC_E_INVALID_TOKEN; |
668 | | |
669 | 0 | input_buffer = sspi_FindSecBuffer(pInput, SECBUFFER_TOKEN); |
670 | |
|
671 | 0 | if (!input_buffer) |
672 | 0 | return SEC_E_INVALID_TOKEN; |
673 | | |
674 | 0 | if (input_buffer->cbBuffer < 1) |
675 | 0 | return SEC_E_INVALID_TOKEN; |
676 | | |
677 | 0 | status = ntlm_read_NegotiateMessage(context, input_buffer); |
678 | 0 | if (status != SEC_I_CONTINUE_NEEDED) |
679 | 0 | return status; |
680 | | |
681 | 0 | if (ntlm_get_state(context) == NTLM_STATE_CHALLENGE) |
682 | 0 | { |
683 | 0 | if (!pOutput) |
684 | 0 | return SEC_E_INVALID_TOKEN; |
685 | | |
686 | 0 | if (pOutput->cBuffers < 1) |
687 | 0 | return SEC_E_INVALID_TOKEN; |
688 | | |
689 | 0 | output_buffer = sspi_FindSecBuffer(pOutput, SECBUFFER_TOKEN); |
690 | |
|
691 | 0 | if (!output_buffer->BufferType) |
692 | 0 | return SEC_E_INVALID_TOKEN; |
693 | | |
694 | 0 | if (output_buffer->cbBuffer < 1) |
695 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
696 | | |
697 | 0 | return ntlm_write_ChallengeMessage(context, output_buffer); |
698 | 0 | } |
699 | | |
700 | 0 | return SEC_E_OUT_OF_SEQUENCE; |
701 | 0 | } |
702 | | |
703 | 0 | case NTLM_STATE_AUTHENTICATE: |
704 | 0 | { |
705 | 0 | if (!pInput) |
706 | 0 | return SEC_E_INVALID_TOKEN; |
707 | | |
708 | 0 | if (pInput->cBuffers < 1) |
709 | 0 | return SEC_E_INVALID_TOKEN; |
710 | | |
711 | 0 | input_buffer = sspi_FindSecBuffer(pInput, SECBUFFER_TOKEN); |
712 | |
|
713 | 0 | if (!input_buffer) |
714 | 0 | return SEC_E_INVALID_TOKEN; |
715 | | |
716 | 0 | if (input_buffer->cbBuffer < 1) |
717 | 0 | return SEC_E_INVALID_TOKEN; |
718 | | |
719 | 0 | status = ntlm_read_AuthenticateMessage(context, input_buffer); |
720 | |
|
721 | 0 | if (pOutput) |
722 | 0 | { |
723 | 0 | for (ULONG i = 0; i < pOutput->cBuffers; i++) |
724 | 0 | { |
725 | 0 | pOutput->pBuffers[i].cbBuffer = 0; |
726 | 0 | pOutput->pBuffers[i].BufferType = SECBUFFER_TOKEN; |
727 | 0 | } |
728 | 0 | } |
729 | |
|
730 | 0 | return status; |
731 | 0 | } |
732 | | |
733 | 0 | default: |
734 | 0 | return SEC_E_OUT_OF_SEQUENCE; |
735 | 0 | } |
736 | 0 | } |
737 | | |
738 | | WINPR_ATTR_NODISCARD |
739 | | static SECURITY_STATUS SEC_ENTRY |
740 | | ntlm_ImpersonateSecurityContext(WINPR_ATTR_UNUSED PCtxtHandle phContext) |
741 | 0 | { |
742 | 0 | return SEC_E_OK; |
743 | 0 | } |
744 | | |
745 | | WINPR_ATTR_NODISCARD |
746 | | static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextW( |
747 | | PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR* pszTargetName, ULONG fContextReq, |
748 | | WINPR_ATTR_UNUSED ULONG Reserved1, WINPR_ATTR_UNUSED ULONG TargetDataRep, PSecBufferDesc pInput, |
749 | | WINPR_ATTR_UNUSED ULONG Reserved2, PCtxtHandle phNewContext, PSecBufferDesc pOutput, |
750 | | WINPR_ATTR_UNUSED PULONG pfContextAttr, WINPR_ATTR_UNUSED PTimeStamp ptsExpiry) |
751 | 0 | { |
752 | 0 | SECURITY_STATUS status = 0; |
753 | 0 | SSPI_CREDENTIALS* credentials = nullptr; |
754 | 0 | PSecBuffer input_buffer = nullptr; |
755 | 0 | PSecBuffer output_buffer = nullptr; |
756 | | |
757 | | /* behave like windows SSPIs that don't want empty context */ |
758 | 0 | if (phContext && !phContext->dwLower && !phContext->dwUpper) |
759 | 0 | return SEC_E_INVALID_HANDLE; |
760 | | |
761 | 0 | NTLM_CONTEXT* context = (NTLM_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext); |
762 | |
|
763 | 0 | if (pInput) |
764 | 0 | { |
765 | 0 | input_buffer = sspi_FindSecBuffer(pInput, SECBUFFER_TOKEN); |
766 | 0 | } |
767 | |
|
768 | 0 | if (!context) |
769 | 0 | { |
770 | 0 | context = ntlm_ContextNew(); |
771 | |
|
772 | 0 | if (!context) |
773 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
774 | | |
775 | 0 | if (fContextReq & ISC_REQ_CONFIDENTIALITY) |
776 | 0 | context->confidentiality = TRUE; |
777 | |
|
778 | 0 | credentials = (SSPI_CREDENTIALS*)sspi_SecureHandleGetLowerPointer(phCredential); |
779 | 0 | context->credentials = credentials; |
780 | |
|
781 | 0 | if (ntlm_SetContextServicePrincipalNameW(context, pszTargetName) < 0) |
782 | 0 | { |
783 | 0 | ntlm_ContextFree(context); |
784 | 0 | return SEC_E_INTERNAL_ERROR; |
785 | 0 | } |
786 | | |
787 | 0 | sspi_SecureHandleSetLowerPointer(phNewContext, context); |
788 | 0 | sspi_SecureHandleSetUpperPointer(phNewContext, NTLM_SSP_NAME); |
789 | 0 | } |
790 | | |
791 | 0 | if ((!input_buffer) || (ntlm_get_state(context) == NTLM_STATE_AUTHENTICATE)) |
792 | 0 | { |
793 | 0 | if (!pOutput) |
794 | 0 | return SEC_E_INVALID_TOKEN; |
795 | | |
796 | 0 | if (pOutput->cBuffers < 1) |
797 | 0 | return SEC_E_INVALID_TOKEN; |
798 | | |
799 | 0 | output_buffer = sspi_FindSecBuffer(pOutput, SECBUFFER_TOKEN); |
800 | |
|
801 | 0 | if (!output_buffer) |
802 | 0 | return SEC_E_INVALID_TOKEN; |
803 | | |
804 | 0 | if (output_buffer->cbBuffer < 1) |
805 | 0 | return SEC_E_INVALID_TOKEN; |
806 | | |
807 | 0 | if (ntlm_get_state(context) == NTLM_STATE_INITIAL) |
808 | 0 | ntlm_change_state(context, NTLM_STATE_NEGOTIATE); |
809 | |
|
810 | 0 | if (ntlm_get_state(context) == NTLM_STATE_NEGOTIATE) |
811 | 0 | return ntlm_write_NegotiateMessage(context, output_buffer); |
812 | | |
813 | 0 | return SEC_E_OUT_OF_SEQUENCE; |
814 | 0 | } |
815 | 0 | else |
816 | 0 | { |
817 | 0 | if (!input_buffer) |
818 | 0 | return SEC_E_INVALID_TOKEN; |
819 | | |
820 | 0 | if (input_buffer->cbBuffer < 1) |
821 | 0 | return SEC_E_INVALID_TOKEN; |
822 | | |
823 | 0 | PSecBuffer channel_bindings = sspi_FindSecBuffer(pInput, SECBUFFER_CHANNEL_BINDINGS); |
824 | |
|
825 | 0 | if (channel_bindings) |
826 | 0 | { |
827 | 0 | context->Bindings.BindingsLength = channel_bindings->cbBuffer; |
828 | 0 | context->Bindings.Bindings = (SEC_CHANNEL_BINDINGS*)channel_bindings->pvBuffer; |
829 | 0 | } |
830 | |
|
831 | 0 | if (ntlm_get_state(context) == NTLM_STATE_CHALLENGE) |
832 | 0 | { |
833 | 0 | status = ntlm_read_ChallengeMessage(context, input_buffer); |
834 | |
|
835 | 0 | if (status != SEC_I_CONTINUE_NEEDED) |
836 | 0 | return status; |
837 | | |
838 | 0 | if (!pOutput) |
839 | 0 | return SEC_E_INVALID_TOKEN; |
840 | | |
841 | 0 | if (pOutput->cBuffers < 1) |
842 | 0 | return SEC_E_INVALID_TOKEN; |
843 | | |
844 | 0 | output_buffer = sspi_FindSecBuffer(pOutput, SECBUFFER_TOKEN); |
845 | |
|
846 | 0 | if (!output_buffer) |
847 | 0 | return SEC_E_INVALID_TOKEN; |
848 | | |
849 | 0 | if (output_buffer->cbBuffer < 1) |
850 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
851 | | |
852 | 0 | if (ntlm_get_state(context) == NTLM_STATE_AUTHENTICATE) |
853 | 0 | return ntlm_write_AuthenticateMessage(context, output_buffer); |
854 | 0 | } |
855 | | |
856 | 0 | return SEC_E_OUT_OF_SEQUENCE; |
857 | 0 | } |
858 | | |
859 | 0 | return SEC_E_OUT_OF_SEQUENCE; |
860 | 0 | } |
861 | | |
862 | | /** |
863 | | * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa375512%28v=vs.85%29.aspx |
864 | | */ |
865 | | WINPR_ATTR_NODISCARD |
866 | | static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextA( |
867 | | PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR* pszTargetName, ULONG fContextReq, |
868 | | ULONG Reserved1, ULONG TargetDataRep, PSecBufferDesc pInput, ULONG Reserved2, |
869 | | PCtxtHandle phNewContext, PSecBufferDesc pOutput, PULONG pfContextAttr, PTimeStamp ptsExpiry) |
870 | 0 | { |
871 | 0 | SECURITY_STATUS status = 0; |
872 | 0 | SEC_WCHAR* pszTargetNameW = nullptr; |
873 | |
|
874 | 0 | if (pszTargetName) |
875 | 0 | { |
876 | 0 | pszTargetNameW = ConvertUtf8ToWCharAlloc(pszTargetName, nullptr); |
877 | 0 | if (!pszTargetNameW) |
878 | 0 | return SEC_E_INTERNAL_ERROR; |
879 | 0 | } |
880 | | |
881 | 0 | status = ntlm_InitializeSecurityContextW(phCredential, phContext, pszTargetNameW, fContextReq, |
882 | 0 | Reserved1, TargetDataRep, pInput, Reserved2, |
883 | 0 | phNewContext, pOutput, pfContextAttr, ptsExpiry); |
884 | 0 | free(pszTargetNameW); |
885 | 0 | return status; |
886 | 0 | } |
887 | | |
888 | | /* http://msdn.microsoft.com/en-us/library/windows/desktop/aa375354 */ |
889 | | WINPR_ATTR_NODISCARD |
890 | | static SECURITY_STATUS SEC_ENTRY ntlm_DeleteSecurityContext(PCtxtHandle phContext) |
891 | 0 | { |
892 | 0 | NTLM_CONTEXT* context = (NTLM_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext); |
893 | 0 | sspi_SecureHandleInvalidate(phContext); |
894 | 0 | ntlm_ContextFree(context); |
895 | 0 | return SEC_E_OK; |
896 | 0 | } |
897 | | |
898 | | SECURITY_STATUS ntlm_computeProofValue(NTLM_CONTEXT* ntlm, SecBuffer* ntproof) |
899 | 0 | { |
900 | 0 | BYTE* blob = nullptr; |
901 | 0 | SecBuffer* target = nullptr; |
902 | |
|
903 | 0 | WINPR_ASSERT(ntlm); |
904 | 0 | WINPR_ASSERT(ntproof); |
905 | |
|
906 | 0 | target = &ntlm->ChallengeTargetInfo; |
907 | |
|
908 | 0 | if (!sspi_SecBufferAlloc(ntproof, 36 + target->cbBuffer)) |
909 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
910 | | |
911 | 0 | blob = (BYTE*)ntproof->pvBuffer; |
912 | 0 | CopyMemory(blob, ntlm->ServerChallenge, 8); /* Server challenge. */ |
913 | 0 | blob[8] = 1; /* Response version. */ |
914 | 0 | blob[9] = 1; /* Highest response version understood by the client. */ |
915 | | /* Reserved 6B. */ |
916 | 0 | CopyMemory(&blob[16], ntlm->Timestamp, 8); /* Time. */ |
917 | 0 | CopyMemory(&blob[24], ntlm->ClientChallenge, 8); /* Client challenge. */ |
918 | | /* Reserved 4B. */ |
919 | | /* Server name. */ |
920 | 0 | CopyMemory(&blob[36], target->pvBuffer, target->cbBuffer); |
921 | 0 | return SEC_E_OK; |
922 | 0 | } |
923 | | |
924 | | SECURITY_STATUS ntlm_computeMicValue(NTLM_CONTEXT* ntlm, SecBuffer* micvalue) |
925 | 0 | { |
926 | 0 | BYTE* blob = nullptr; |
927 | 0 | ULONG msgSize = 0; |
928 | |
|
929 | 0 | WINPR_ASSERT(ntlm); |
930 | 0 | WINPR_ASSERT(micvalue); |
931 | |
|
932 | 0 | msgSize = ntlm->NegotiateMessage.cbBuffer + ntlm->ChallengeMessage.cbBuffer + |
933 | 0 | ntlm->AuthenticateMessage.cbBuffer; |
934 | |
|
935 | 0 | if (!sspi_SecBufferAlloc(micvalue, msgSize)) |
936 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
937 | | |
938 | 0 | blob = (BYTE*)micvalue->pvBuffer; |
939 | 0 | CopyMemory(blob, ntlm->NegotiateMessage.pvBuffer, ntlm->NegotiateMessage.cbBuffer); |
940 | 0 | blob += ntlm->NegotiateMessage.cbBuffer; |
941 | 0 | CopyMemory(blob, ntlm->ChallengeMessage.pvBuffer, ntlm->ChallengeMessage.cbBuffer); |
942 | 0 | blob += ntlm->ChallengeMessage.cbBuffer; |
943 | 0 | CopyMemory(blob, ntlm->AuthenticateMessage.pvBuffer, ntlm->AuthenticateMessage.cbBuffer); |
944 | 0 | blob += ntlm->MessageIntegrityCheckOffset; |
945 | 0 | ZeroMemory(blob, 16); |
946 | 0 | return SEC_E_OK; |
947 | 0 | } |
948 | | |
949 | | WINPR_ATTR_NODISCARD |
950 | | static bool identityToAuthIdentity(const SEC_WINNT_AUTH_IDENTITY* identity, |
951 | | SecPkgContext_AuthIdentity* pAuthIdentity) |
952 | 0 | { |
953 | 0 | WINPR_ASSERT(identity); |
954 | |
|
955 | 0 | if (!pAuthIdentity) |
956 | 0 | return false; |
957 | | |
958 | 0 | const SecPkgContext_AuthIdentity empty = WINPR_C_ARRAY_INIT; |
959 | 0 | *pAuthIdentity = empty; |
960 | |
|
961 | 0 | if ((identity->Flags & SEC_WINNT_AUTH_IDENTITY_UNICODE) != 0) |
962 | 0 | { |
963 | 0 | if (identity->UserLength > 0) |
964 | 0 | { |
965 | 0 | if (ConvertWCharNToUtf8(identity->User, identity->UserLength, pAuthIdentity->User, |
966 | 0 | ARRAYSIZE(pAuthIdentity->User)) <= 0) |
967 | 0 | return false; |
968 | 0 | } |
969 | | |
970 | 0 | if (identity->DomainLength > 0) |
971 | 0 | { |
972 | 0 | if (ConvertWCharNToUtf8(identity->Domain, identity->DomainLength, pAuthIdentity->Domain, |
973 | 0 | ARRAYSIZE(pAuthIdentity->Domain)) <= 0) |
974 | 0 | return false; |
975 | 0 | } |
976 | 0 | } |
977 | 0 | else if ((identity->Flags & SEC_WINNT_AUTH_IDENTITY_ANSI) != 0) |
978 | 0 | { |
979 | 0 | if (identity->UserLength > 0) |
980 | 0 | { |
981 | 0 | const size_t len = MIN(ARRAYSIZE(pAuthIdentity->User) - 1, identity->UserLength); |
982 | 0 | strncpy(pAuthIdentity->User, (char*)identity->User, len); |
983 | 0 | pAuthIdentity->User[len] = '\0'; |
984 | 0 | } |
985 | |
|
986 | 0 | if (identity->DomainLength > 0) |
987 | 0 | { |
988 | 0 | const size_t len = MIN(ARRAYSIZE(pAuthIdentity->Domain) - 1, identity->DomainLength); |
989 | 0 | strncpy(pAuthIdentity->Domain, (char*)identity->Domain, len); |
990 | 0 | pAuthIdentity->Domain[len] = '\0'; |
991 | 0 | } |
992 | 0 | } |
993 | 0 | else |
994 | 0 | return false; |
995 | 0 | return true; |
996 | 0 | } |
997 | | |
998 | | WINPR_ATTR_NODISCARD |
999 | | static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesCommon(PCtxtHandle phContext, |
1000 | | ULONG ulAttribute, void* pBuffer) |
1001 | 0 | { |
1002 | 0 | if (!phContext) |
1003 | 0 | return SEC_E_INVALID_HANDLE; |
1004 | | |
1005 | 0 | if (!pBuffer) |
1006 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
1007 | | |
1008 | 0 | NTLM_CONTEXT* context = (NTLM_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext); |
1009 | 0 | if (!check_context(context)) |
1010 | 0 | return SEC_E_INVALID_HANDLE; |
1011 | | |
1012 | 0 | switch (ulAttribute) |
1013 | 0 | { |
1014 | 0 | case SECPKG_ATTR_AUTH_IDENTITY: |
1015 | 0 | { |
1016 | 0 | SecPkgContext_AuthIdentity* AuthIdentity = (SecPkgContext_AuthIdentity*)pBuffer; |
1017 | 0 | SSPI_CREDENTIALS* credentials = context->credentials; |
1018 | 0 | if (!credentials) |
1019 | 0 | return SEC_E_INTERNAL_ERROR; |
1020 | 0 | if (!identityToAuthIdentity(&credentials->identity, AuthIdentity)) |
1021 | 0 | return SEC_E_INTERNAL_ERROR; |
1022 | 0 | context->UseSamFileDatabase = FALSE; |
1023 | 0 | return SEC_E_OK; |
1024 | 0 | } |
1025 | 0 | case SECPKG_ATTR_SIZES: |
1026 | 0 | { |
1027 | 0 | SecPkgContext_Sizes* ContextSizes = (SecPkgContext_Sizes*)pBuffer; |
1028 | 0 | ContextSizes->cbMaxToken = 2010; |
1029 | 0 | ContextSizes->cbMaxSignature = 16; /* the size of expected signature is 16 bytes */ |
1030 | 0 | ContextSizes->cbBlockSize = 0; /* no padding */ |
1031 | 0 | ContextSizes->cbSecurityTrailer = 16; /* no security trailer appended in NTLM |
1032 | | contrary to Kerberos */ |
1033 | 0 | return SEC_E_OK; |
1034 | 0 | } |
1035 | 0 | case SECPKG_ATTR_AUTH_NTLM_NTPROOF_VALUE: |
1036 | 0 | return ntlm_computeProofValue(context, (SecBuffer*)pBuffer); |
1037 | | |
1038 | 0 | case SECPKG_ATTR_AUTH_NTLM_RANDKEY: |
1039 | 0 | { |
1040 | 0 | SecBuffer* randkey = (SecBuffer*)pBuffer; |
1041 | |
|
1042 | 0 | if (!sspi_SecBufferAlloc(randkey, 16)) |
1043 | 0 | return (SEC_E_INSUFFICIENT_MEMORY); |
1044 | | |
1045 | 0 | CopyMemory(randkey->pvBuffer, context->EncryptedRandomSessionKey, 16); |
1046 | 0 | return (SEC_E_OK); |
1047 | 0 | } |
1048 | | |
1049 | 0 | case SECPKG_ATTR_AUTH_NTLM_MIC: |
1050 | 0 | { |
1051 | 0 | SecBuffer* mic = (SecBuffer*)pBuffer; |
1052 | 0 | NTLM_AUTHENTICATE_MESSAGE* message = &context->AUTHENTICATE_MESSAGE; |
1053 | |
|
1054 | 0 | if (!sspi_SecBufferAlloc(mic, 16)) |
1055 | 0 | return (SEC_E_INSUFFICIENT_MEMORY); |
1056 | | |
1057 | 0 | CopyMemory(mic->pvBuffer, message->MessageIntegrityCheck, 16); |
1058 | 0 | return (SEC_E_OK); |
1059 | 0 | } |
1060 | | |
1061 | 0 | case SECPKG_ATTR_AUTH_NTLM_MIC_VALUE: |
1062 | 0 | return ntlm_computeMicValue(context, (SecBuffer*)pBuffer); |
1063 | | |
1064 | 0 | default: |
1065 | 0 | WLog_ERR(TAG, "TODO: Implement ulAttribute=0x%08" PRIx32, ulAttribute); |
1066 | 0 | return SEC_E_UNSUPPORTED_FUNCTION; |
1067 | 0 | } |
1068 | 0 | } |
1069 | | |
1070 | | /* http://msdn.microsoft.com/en-us/library/windows/desktop/aa379337/ */ |
1071 | | WINPR_ATTR_NODISCARD |
1072 | | static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesW(PCtxtHandle phContext, |
1073 | | ULONG ulAttribute, void* pBuffer) |
1074 | 0 | { |
1075 | 0 | if (!phContext) |
1076 | 0 | return SEC_E_INVALID_HANDLE; |
1077 | | |
1078 | 0 | if (!pBuffer) |
1079 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
1080 | | |
1081 | 0 | NTLM_CONTEXT* context = (NTLM_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext); |
1082 | 0 | if (!check_context(context)) |
1083 | 0 | return SEC_E_INVALID_HANDLE; |
1084 | | |
1085 | 0 | switch (ulAttribute) |
1086 | 0 | { |
1087 | 0 | case SECPKG_ATTR_AUTH_NTLM_HOSTNAME: |
1088 | 0 | { |
1089 | 0 | memcpy(pBuffer, context->Workstation.Buffer, context->Workstation.Length); |
1090 | 0 | return SEC_E_OK; |
1091 | 0 | } |
1092 | 0 | case SECPKG_ATTR_AUTH_NTLM_NB_DOMAIN_NAME: |
1093 | 0 | { |
1094 | 0 | memcpy(pBuffer, context->NbDomainName.Buffer, context->NbDomainName.Length); |
1095 | 0 | return SEC_E_OK; |
1096 | 0 | } |
1097 | 0 | case SECPKG_ATTR_AUTH_NTLM_NB_COMPUTER_NAME: |
1098 | 0 | { |
1099 | 0 | memcpy(pBuffer, context->NbComputerName.Buffer, context->NbComputerName.Length); |
1100 | 0 | return SEC_E_OK; |
1101 | 0 | } |
1102 | 0 | case SECPKG_ATTR_AUTH_NTLM_DNS_DOMAIN_NAME: |
1103 | 0 | { |
1104 | 0 | memcpy(pBuffer, context->DnsDomainName.Buffer, context->DnsDomainName.Length); |
1105 | 0 | return SEC_E_OK; |
1106 | 0 | } |
1107 | 0 | case SECPKG_ATTR_AUTH_NTLM_DNS_COMPUTER_NAME: |
1108 | 0 | { |
1109 | 0 | memcpy(pBuffer, context->DnsComputerName.Buffer, context->DnsComputerName.Length); |
1110 | 0 | return SEC_E_OK; |
1111 | 0 | } |
1112 | | |
1113 | 0 | case SECPKG_ATTR_PACKAGE_INFO: |
1114 | 0 | { |
1115 | 0 | SecPkgContext_PackageInfoW* PackageInfo = (SecPkgContext_PackageInfoW*)pBuffer; |
1116 | 0 | size_t size = sizeof(SecPkgInfoW); |
1117 | 0 | SecPkgInfoW* pPackageInfo = |
1118 | 0 | (SecPkgInfoW*)sspi_ContextBufferAlloc(QuerySecurityPackageInfoIndex, size); |
1119 | |
|
1120 | 0 | if (!pPackageInfo) |
1121 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
1122 | | |
1123 | 0 | pPackageInfo->fCapabilities = NTLM_SecPkgInfoW.fCapabilities; |
1124 | 0 | pPackageInfo->wVersion = NTLM_SecPkgInfoW.wVersion; |
1125 | 0 | pPackageInfo->wRPCID = NTLM_SecPkgInfoW.wRPCID; |
1126 | 0 | pPackageInfo->cbMaxToken = NTLM_SecPkgInfoW.cbMaxToken; |
1127 | 0 | pPackageInfo->Name = _wcsdup(NTLM_SecPkgInfoW.Name); |
1128 | 0 | pPackageInfo->Comment = _wcsdup(NTLM_SecPkgInfoW.Comment); |
1129 | |
|
1130 | 0 | if (!pPackageInfo->Name || !pPackageInfo->Comment) |
1131 | 0 | { |
1132 | 0 | sspi_ContextBufferFree(pPackageInfo); |
1133 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
1134 | 0 | } |
1135 | 0 | PackageInfo->PackageInfo = pPackageInfo; |
1136 | 0 | return SEC_E_OK; |
1137 | 0 | } |
1138 | 0 | default: |
1139 | 0 | return ntlm_QueryContextAttributesCommon(phContext, ulAttribute, pBuffer); |
1140 | 0 | } |
1141 | 0 | } |
1142 | | |
1143 | | WINPR_ATTR_NODISCARD |
1144 | | static SECURITY_STATUS utf8len(const UNICODE_STRING* str, void* pBuffer) |
1145 | 0 | { |
1146 | 0 | WINPR_ASSERT(str); |
1147 | 0 | WINPR_ASSERT(pBuffer); |
1148 | 0 | ULONG* val = (ULONG*)pBuffer; |
1149 | 0 | const SSIZE_T rc = ConvertWCharNToUtf8(str->Buffer, str->Length, nullptr, 0); |
1150 | 0 | if (rc < 0) |
1151 | 0 | return SEC_E_INVALID_PARAMETER; |
1152 | 0 | *val = WINPR_ASSERTING_INT_CAST(ULONG, rc); |
1153 | 0 | return SEC_E_OK; |
1154 | 0 | } |
1155 | | |
1156 | | WINPR_ATTR_NODISCARD |
1157 | | static SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesA(PCtxtHandle phContext, |
1158 | | ULONG ulAttribute, void* pBuffer) |
1159 | 0 | { |
1160 | 0 | if (!phContext) |
1161 | 0 | return SEC_E_INVALID_HANDLE; |
1162 | | |
1163 | 0 | if (!pBuffer) |
1164 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
1165 | | |
1166 | 0 | NTLM_CONTEXT* context = (NTLM_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext); |
1167 | |
|
1168 | 0 | switch (ulAttribute) |
1169 | 0 | { |
1170 | 0 | case SECPKG_ATTR_AUTH_NTLM_HOSTNAME_LEN: |
1171 | 0 | return utf8len(&context->Workstation, pBuffer); |
1172 | 0 | case SECPKG_ATTR_AUTH_NTLM_NB_DOMAIN_NAME_LEN: |
1173 | 0 | return utf8len(&context->NbDomainName, pBuffer); |
1174 | 0 | case SECPKG_ATTR_AUTH_NTLM_NB_COMPUTER_NAME_LEN: |
1175 | 0 | return utf8len(&context->NbComputerName, pBuffer); |
1176 | 0 | case SECPKG_ATTR_AUTH_NTLM_DNS_DOMAIN_NAME_LEN: |
1177 | 0 | return utf8len(&context->DnsDomainName, pBuffer); |
1178 | 0 | case SECPKG_ATTR_AUTH_NTLM_DNS_COMPUTER_NAME_LEN: |
1179 | 0 | return utf8len(&context->DnsComputerName, pBuffer); |
1180 | 0 | case SECPKG_ATTR_AUTH_NTLM_HOSTNAME: |
1181 | 0 | { |
1182 | 0 | ConvertWCharNToUtf8(context->Workstation.Buffer, context->Workstation.Length, pBuffer, |
1183 | 0 | context->Workstation.Length); |
1184 | 0 | return SEC_E_OK; |
1185 | 0 | } |
1186 | 0 | case SECPKG_ATTR_AUTH_NTLM_NB_DOMAIN_NAME: |
1187 | 0 | { |
1188 | 0 | ConvertWCharNToUtf8(context->NbDomainName.Buffer, context->NbDomainName.Length, pBuffer, |
1189 | 0 | context->NbDomainName.Length); |
1190 | 0 | return SEC_E_OK; |
1191 | 0 | } |
1192 | 0 | case SECPKG_ATTR_AUTH_NTLM_NB_COMPUTER_NAME: |
1193 | 0 | { |
1194 | 0 | ConvertWCharNToUtf8(context->NbComputerName.Buffer, context->NbComputerName.Length, |
1195 | 0 | pBuffer, context->NbComputerName.Length); |
1196 | 0 | return SEC_E_OK; |
1197 | 0 | } |
1198 | 0 | case SECPKG_ATTR_AUTH_NTLM_DNS_DOMAIN_NAME: |
1199 | 0 | { |
1200 | 0 | ConvertWCharNToUtf8(context->DnsDomainName.Buffer, context->DnsDomainName.Length, |
1201 | 0 | pBuffer, context->DnsDomainName.Length); |
1202 | 0 | return SEC_E_OK; |
1203 | 0 | } |
1204 | 0 | case SECPKG_ATTR_AUTH_NTLM_DNS_COMPUTER_NAME: |
1205 | 0 | { |
1206 | 0 | ConvertWCharNToUtf8(context->DnsComputerName.Buffer, context->DnsComputerName.Length, |
1207 | 0 | pBuffer, context->DnsComputerName.Length); |
1208 | 0 | return SEC_E_OK; |
1209 | 0 | } |
1210 | 0 | case SECPKG_ATTR_PACKAGE_INFO: |
1211 | 0 | { |
1212 | 0 | SecPkgContext_PackageInfoA* PackageInfo = (SecPkgContext_PackageInfoA*)pBuffer; |
1213 | 0 | size_t size = sizeof(SecPkgInfoA); |
1214 | 0 | SecPkgInfoA* pPackageInfo = |
1215 | 0 | (SecPkgInfoA*)sspi_ContextBufferAlloc(QuerySecurityPackageInfoIndex, size); |
1216 | |
|
1217 | 0 | if (!pPackageInfo) |
1218 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
1219 | | |
1220 | 0 | pPackageInfo->fCapabilities = NTLM_SecPkgInfoA.fCapabilities; |
1221 | 0 | pPackageInfo->wVersion = NTLM_SecPkgInfoA.wVersion; |
1222 | 0 | pPackageInfo->wRPCID = NTLM_SecPkgInfoA.wRPCID; |
1223 | 0 | pPackageInfo->cbMaxToken = NTLM_SecPkgInfoA.cbMaxToken; |
1224 | 0 | pPackageInfo->Name = _strdup(NTLM_SecPkgInfoA.Name); |
1225 | 0 | pPackageInfo->Comment = _strdup(NTLM_SecPkgInfoA.Comment); |
1226 | |
|
1227 | 0 | if (!pPackageInfo->Name || !pPackageInfo->Comment) |
1228 | 0 | { |
1229 | 0 | sspi_ContextBufferFree(pPackageInfo); |
1230 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
1231 | 0 | } |
1232 | 0 | PackageInfo->PackageInfo = pPackageInfo; |
1233 | 0 | return SEC_E_OK; |
1234 | 0 | } |
1235 | | |
1236 | 0 | default: |
1237 | 0 | return ntlm_QueryContextAttributesCommon(phContext, ulAttribute, pBuffer); |
1238 | 0 | } |
1239 | 0 | } |
1240 | | |
1241 | | WINPR_ATTR_NODISCARD |
1242 | | static SECURITY_STATUS SEC_ENTRY ntlm_SetContextAttributesCommon(PCtxtHandle phContext, |
1243 | | ULONG ulAttribute, void* pBuffer, |
1244 | | ULONG cbBuffer) |
1245 | 0 | { |
1246 | 0 | if (!phContext) |
1247 | 0 | return SEC_E_INVALID_HANDLE; |
1248 | | |
1249 | 0 | if (!pBuffer) |
1250 | 0 | return SEC_E_INVALID_PARAMETER; |
1251 | | |
1252 | 0 | NTLM_CONTEXT* context = (NTLM_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext); |
1253 | 0 | if (!context) |
1254 | 0 | return SEC_E_INVALID_HANDLE; |
1255 | | |
1256 | 0 | switch (ulAttribute) |
1257 | 0 | { |
1258 | 0 | case SECPKG_ATTR_AUTH_NTLM_HASH: |
1259 | 0 | { |
1260 | 0 | SecPkgContext_AuthNtlmHash* AuthNtlmHash = (SecPkgContext_AuthNtlmHash*)pBuffer; |
1261 | |
|
1262 | 0 | if (cbBuffer < sizeof(SecPkgContext_AuthNtlmHash)) |
1263 | 0 | return SEC_E_INVALID_PARAMETER; |
1264 | | |
1265 | 0 | if (AuthNtlmHash->Version == 1) |
1266 | 0 | CopyMemory(context->NtlmHash, AuthNtlmHash->NtlmHash, 16); |
1267 | 0 | else if (AuthNtlmHash->Version == 2) |
1268 | 0 | CopyMemory(context->NtlmV2Hash, AuthNtlmHash->NtlmHash, 16); |
1269 | |
|
1270 | 0 | return SEC_E_OK; |
1271 | 0 | } |
1272 | | |
1273 | 0 | case SECPKG_ATTR_AUTH_NTLM_MESSAGE: |
1274 | 0 | { |
1275 | 0 | SecPkgContext_AuthNtlmMessage* AuthNtlmMessage = |
1276 | 0 | (SecPkgContext_AuthNtlmMessage*)pBuffer; |
1277 | |
|
1278 | 0 | if (cbBuffer < sizeof(SecPkgContext_AuthNtlmMessage)) |
1279 | 0 | return SEC_E_INVALID_PARAMETER; |
1280 | | |
1281 | 0 | if (AuthNtlmMessage->type == 1) |
1282 | 0 | { |
1283 | 0 | if (!ntlm_SecBufferRealloc(&context->NegotiateMessage, AuthNtlmMessage->length)) |
1284 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
1285 | | |
1286 | 0 | CopyMemory(context->NegotiateMessage.pvBuffer, AuthNtlmMessage->buffer, |
1287 | 0 | AuthNtlmMessage->length); |
1288 | 0 | } |
1289 | 0 | else if (AuthNtlmMessage->type == 2) |
1290 | 0 | { |
1291 | 0 | if (!ntlm_SecBufferRealloc(&context->ChallengeMessage, AuthNtlmMessage->length)) |
1292 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
1293 | | |
1294 | 0 | CopyMemory(context->ChallengeMessage.pvBuffer, AuthNtlmMessage->buffer, |
1295 | 0 | AuthNtlmMessage->length); |
1296 | 0 | } |
1297 | 0 | else if (AuthNtlmMessage->type == 3) |
1298 | 0 | { |
1299 | 0 | if (!ntlm_SecBufferRealloc(&context->AuthenticateMessage, AuthNtlmMessage->length)) |
1300 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
1301 | | |
1302 | 0 | CopyMemory(context->AuthenticateMessage.pvBuffer, AuthNtlmMessage->buffer, |
1303 | 0 | AuthNtlmMessage->length); |
1304 | 0 | } |
1305 | | |
1306 | 0 | return SEC_E_OK; |
1307 | 0 | } |
1308 | | |
1309 | 0 | case SECPKG_ATTR_AUTH_NTLM_TIMESTAMP: |
1310 | 0 | { |
1311 | 0 | SecPkgContext_AuthNtlmTimestamp* AuthNtlmTimestamp = |
1312 | 0 | (SecPkgContext_AuthNtlmTimestamp*)pBuffer; |
1313 | |
|
1314 | 0 | if (cbBuffer < sizeof(SecPkgContext_AuthNtlmTimestamp)) |
1315 | 0 | return SEC_E_INVALID_PARAMETER; |
1316 | | |
1317 | 0 | if (AuthNtlmTimestamp->ChallengeOrResponse) |
1318 | 0 | CopyMemory(context->ChallengeTimestamp, AuthNtlmTimestamp->Timestamp, 8); |
1319 | 0 | else |
1320 | 0 | CopyMemory(context->Timestamp, AuthNtlmTimestamp->Timestamp, 8); |
1321 | |
|
1322 | 0 | return SEC_E_OK; |
1323 | 0 | } |
1324 | | |
1325 | 0 | case SECPKG_ATTR_AUTH_NTLM_CLIENT_CHALLENGE: |
1326 | 0 | { |
1327 | 0 | SecPkgContext_AuthNtlmClientChallenge* AuthNtlmClientChallenge = |
1328 | 0 | (SecPkgContext_AuthNtlmClientChallenge*)pBuffer; |
1329 | |
|
1330 | 0 | if (cbBuffer < sizeof(SecPkgContext_AuthNtlmClientChallenge)) |
1331 | 0 | return SEC_E_INVALID_PARAMETER; |
1332 | | |
1333 | 0 | CopyMemory(context->ClientChallenge, AuthNtlmClientChallenge->ClientChallenge, 8); |
1334 | 0 | return SEC_E_OK; |
1335 | 0 | } |
1336 | | |
1337 | 0 | case SECPKG_ATTR_AUTH_NTLM_SERVER_CHALLENGE: |
1338 | 0 | { |
1339 | 0 | SecPkgContext_AuthNtlmServerChallenge* AuthNtlmServerChallenge = |
1340 | 0 | (SecPkgContext_AuthNtlmServerChallenge*)pBuffer; |
1341 | |
|
1342 | 0 | if (cbBuffer < sizeof(SecPkgContext_AuthNtlmServerChallenge)) |
1343 | 0 | return SEC_E_INVALID_PARAMETER; |
1344 | | |
1345 | 0 | CopyMemory(context->ServerChallenge, AuthNtlmServerChallenge->ServerChallenge, 8); |
1346 | 0 | return SEC_E_OK; |
1347 | 0 | } |
1348 | | |
1349 | 0 | default: |
1350 | 0 | WLog_ERR(TAG, "TODO: Implement ulAttribute=%08" PRIx32, ulAttribute); |
1351 | 0 | return SEC_E_UNSUPPORTED_FUNCTION; |
1352 | 0 | } |
1353 | 0 | } |
1354 | | |
1355 | | WINPR_ATTR_NODISCARD |
1356 | | static SECURITY_STATUS ntml_setUnicodeStringW(UNICODE_STRING* str, const WCHAR* val, size_t bytelen) |
1357 | 0 | { |
1358 | 0 | WINPR_ASSERT(str); |
1359 | 0 | ntlm_free_unicode_string(str); |
1360 | 0 | *str = ntlm_from_unicode_string_w(val, bytelen / sizeof(WCHAR)); |
1361 | 0 | if (ntlm_is_unicode_string_empty(str)) |
1362 | 0 | return SEC_E_INVALID_PARAMETER; |
1363 | 0 | return SEC_E_OK; |
1364 | 0 | } |
1365 | | |
1366 | | WINPR_ATTR_NODISCARD |
1367 | | static SECURITY_STATUS utf16len(const UNICODE_STRING* str, void* pBuffer) |
1368 | 0 | { |
1369 | 0 | WINPR_ASSERT(str); |
1370 | 0 | WINPR_ASSERT(pBuffer); |
1371 | 0 | ULONG* val = (ULONG*)pBuffer; |
1372 | 0 | *val = str->Length; |
1373 | 0 | return SEC_E_OK; |
1374 | 0 | } |
1375 | | |
1376 | | WINPR_ATTR_NODISCARD |
1377 | | static SECURITY_STATUS SEC_ENTRY ntlm_SetContextAttributesW(PCtxtHandle phContext, |
1378 | | ULONG ulAttribute, void* pBuffer, |
1379 | | ULONG cbBuffer) |
1380 | 0 | { |
1381 | 0 | if (!phContext) |
1382 | 0 | return SEC_E_INVALID_HANDLE; |
1383 | | |
1384 | 0 | if (!pBuffer) |
1385 | 0 | return SEC_E_INVALID_PARAMETER; |
1386 | | |
1387 | 0 | NTLM_CONTEXT* context = (NTLM_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext); |
1388 | 0 | if (!context) |
1389 | 0 | return SEC_E_INVALID_HANDLE; |
1390 | | |
1391 | 0 | switch (ulAttribute) |
1392 | 0 | { |
1393 | 0 | case SECPKG_ATTR_AUTH_NTLM_HOSTNAME_LEN: |
1394 | 0 | return utf16len(&context->Workstation, pBuffer); |
1395 | 0 | case SECPKG_ATTR_AUTH_NTLM_NB_DOMAIN_NAME_LEN: |
1396 | 0 | return utf16len(&context->NbDomainName, pBuffer); |
1397 | 0 | case SECPKG_ATTR_AUTH_NTLM_NB_COMPUTER_NAME_LEN: |
1398 | 0 | return utf16len(&context->NbComputerName, pBuffer); |
1399 | 0 | case SECPKG_ATTR_AUTH_NTLM_DNS_DOMAIN_NAME_LEN: |
1400 | 0 | return utf16len(&context->DnsDomainName, pBuffer); |
1401 | 0 | case SECPKG_ATTR_AUTH_NTLM_DNS_COMPUTER_NAME_LEN: |
1402 | 0 | return utf16len(&context->DnsComputerName, pBuffer); |
1403 | 0 | case SECPKG_ATTR_AUTH_NTLM_HOSTNAME: |
1404 | 0 | return ntml_setUnicodeStringW(&context->Workstation, pBuffer, cbBuffer); |
1405 | 0 | case SECPKG_ATTR_AUTH_NTLM_NB_DOMAIN_NAME: |
1406 | 0 | return ntml_setUnicodeStringW(&context->NbDomainName, pBuffer, cbBuffer); |
1407 | 0 | case SECPKG_ATTR_AUTH_NTLM_NB_COMPUTER_NAME: |
1408 | 0 | return ntml_setUnicodeStringW(&context->NbComputerName, pBuffer, cbBuffer); |
1409 | 0 | case SECPKG_ATTR_AUTH_NTLM_DNS_DOMAIN_NAME: |
1410 | 0 | return ntml_setUnicodeStringW(&context->DnsDomainName, pBuffer, cbBuffer); |
1411 | 0 | case SECPKG_ATTR_AUTH_NTLM_DNS_COMPUTER_NAME: |
1412 | 0 | return ntml_setUnicodeStringW(&context->DnsComputerName, pBuffer, cbBuffer); |
1413 | | |
1414 | 0 | default: |
1415 | 0 | return ntlm_SetContextAttributesCommon(phContext, ulAttribute, pBuffer, cbBuffer); |
1416 | 0 | } |
1417 | 0 | } |
1418 | | |
1419 | | WINPR_ATTR_NODISCARD |
1420 | | static SECURITY_STATUS ntml_setUnicodeStringA(UNICODE_STRING* str, const char* val, size_t charlen) |
1421 | 0 | { |
1422 | 0 | WINPR_ASSERT(str); |
1423 | 0 | ntlm_free_unicode_string(str); |
1424 | 0 | *str = ntlm_from_unicode_string_utf8(val, charlen); |
1425 | 0 | if (ntlm_is_unicode_string_empty(str)) |
1426 | 0 | return SEC_E_INVALID_PARAMETER; |
1427 | 0 | return SEC_E_OK; |
1428 | 0 | } |
1429 | | |
1430 | | WINPR_ATTR_NODISCARD |
1431 | | static SECURITY_STATUS SEC_ENTRY ntlm_SetContextAttributesA(PCtxtHandle phContext, |
1432 | | ULONG ulAttribute, void* pBuffer, |
1433 | | ULONG cbBuffer) |
1434 | 0 | { |
1435 | 0 | if (!phContext) |
1436 | 0 | return SEC_E_INVALID_HANDLE; |
1437 | | |
1438 | 0 | if (!pBuffer) |
1439 | 0 | return SEC_E_INVALID_PARAMETER; |
1440 | | |
1441 | 0 | NTLM_CONTEXT* context = (NTLM_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext); |
1442 | 0 | if (!context) |
1443 | 0 | return SEC_E_INVALID_HANDLE; |
1444 | | |
1445 | 0 | switch (ulAttribute) |
1446 | 0 | { |
1447 | 0 | case SECPKG_ATTR_AUTH_NTLM_HOSTNAME: |
1448 | 0 | return ntml_setUnicodeStringA(&context->Workstation, pBuffer, cbBuffer); |
1449 | 0 | case SECPKG_ATTR_AUTH_NTLM_NB_DOMAIN_NAME: |
1450 | 0 | return ntml_setUnicodeStringA(&context->NbDomainName, pBuffer, cbBuffer); |
1451 | 0 | case SECPKG_ATTR_AUTH_NTLM_NB_COMPUTER_NAME: |
1452 | 0 | return ntml_setUnicodeStringA(&context->NbComputerName, pBuffer, cbBuffer); |
1453 | 0 | case SECPKG_ATTR_AUTH_NTLM_DNS_DOMAIN_NAME: |
1454 | 0 | return ntml_setUnicodeStringA(&context->DnsDomainName, pBuffer, cbBuffer); |
1455 | 0 | case SECPKG_ATTR_AUTH_NTLM_DNS_COMPUTER_NAME: |
1456 | 0 | return ntml_setUnicodeStringA(&context->DnsComputerName, pBuffer, cbBuffer); |
1457 | 0 | default: |
1458 | 0 | return ntlm_SetContextAttributesCommon(phContext, ulAttribute, pBuffer, cbBuffer); |
1459 | 0 | } |
1460 | 0 | } |
1461 | | |
1462 | | WINPR_ATTR_NODISCARD |
1463 | | static SECURITY_STATUS SEC_ENTRY ntlm_SetCredentialsAttributesW( |
1464 | | WINPR_ATTR_UNUSED PCredHandle phCredential, WINPR_ATTR_UNUSED ULONG ulAttribute, |
1465 | | WINPR_ATTR_UNUSED void* pBuffer, WINPR_ATTR_UNUSED ULONG cbBuffer) |
1466 | 0 | { |
1467 | 0 | return SEC_E_UNSUPPORTED_FUNCTION; |
1468 | 0 | } |
1469 | | |
1470 | | WINPR_ATTR_NODISCARD |
1471 | | static SECURITY_STATUS SEC_ENTRY ntlm_SetCredentialsAttributesA( |
1472 | | WINPR_ATTR_UNUSED PCredHandle phCredential, WINPR_ATTR_UNUSED ULONG ulAttribute, |
1473 | | WINPR_ATTR_UNUSED void* pBuffer, WINPR_ATTR_UNUSED ULONG cbBuffer) |
1474 | 0 | { |
1475 | 0 | return SEC_E_UNSUPPORTED_FUNCTION; |
1476 | 0 | } |
1477 | | |
1478 | | WINPR_ATTR_NODISCARD |
1479 | | static SECURITY_STATUS SEC_ENTRY ntlm_RevertSecurityContext(WINPR_ATTR_UNUSED PCtxtHandle phContext) |
1480 | 0 | { |
1481 | 0 | return SEC_E_OK; |
1482 | 0 | } |
1483 | | |
1484 | | WINPR_ATTR_NODISCARD |
1485 | | static SECURITY_STATUS SEC_ENTRY ntlm_EncryptMessage(PCtxtHandle phContext, |
1486 | | WINPR_ATTR_UNUSED ULONG fQOP, |
1487 | | PSecBufferDesc pMessage, ULONG MessageSeqNo) |
1488 | 0 | { |
1489 | 0 | const UINT32 SeqNo = MessageSeqNo; |
1490 | 0 | UINT32 value = 0; |
1491 | 0 | BYTE digest[WINPR_MD5_DIGEST_LENGTH] = WINPR_C_ARRAY_INIT; |
1492 | 0 | BYTE checksum[8] = WINPR_C_ARRAY_INIT; |
1493 | 0 | ULONG version = 1; |
1494 | 0 | PSecBuffer data_buffer = nullptr; |
1495 | 0 | PSecBuffer signature_buffer = nullptr; |
1496 | 0 | NTLM_CONTEXT* context = (NTLM_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext); |
1497 | 0 | if (!check_context(context)) |
1498 | 0 | return SEC_E_INVALID_HANDLE; |
1499 | | |
1500 | 0 | for (ULONG index = 0; index < pMessage->cBuffers; index++) |
1501 | 0 | { |
1502 | 0 | SecBuffer* cur = &pMessage->pBuffers[index]; |
1503 | |
|
1504 | 0 | if (cur->BufferType & SECBUFFER_DATA) |
1505 | 0 | data_buffer = cur; |
1506 | 0 | else if (cur->BufferType & SECBUFFER_TOKEN) |
1507 | 0 | signature_buffer = cur; |
1508 | 0 | } |
1509 | |
|
1510 | 0 | if (!data_buffer) |
1511 | 0 | return SEC_E_INVALID_TOKEN; |
1512 | | |
1513 | 0 | if (!signature_buffer) |
1514 | 0 | return SEC_E_INVALID_TOKEN; |
1515 | | |
1516 | | /* Copy original data buffer */ |
1517 | 0 | ULONG length = data_buffer->cbBuffer; |
1518 | 0 | void* data = malloc(length); |
1519 | |
|
1520 | 0 | if (!data) |
1521 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
1522 | | |
1523 | 0 | CopyMemory(data, data_buffer->pvBuffer, length); |
1524 | | /* Compute the HMAC-MD5 hash of ConcatenationOf(seq_num,data) using the client signing key */ |
1525 | 0 | WINPR_HMAC_CTX* hmac = winpr_HMAC_New(); |
1526 | |
|
1527 | 0 | BOOL success = FALSE; |
1528 | 0 | { |
1529 | 0 | if (!hmac) |
1530 | 0 | goto hmac_fail; |
1531 | 0 | if (!winpr_HMAC_Init(hmac, WINPR_MD_MD5, context->SendSigningKey, WINPR_MD5_DIGEST_LENGTH)) |
1532 | 0 | goto hmac_fail; |
1533 | | |
1534 | 0 | winpr_Data_Write_UINT32(&value, SeqNo); |
1535 | |
|
1536 | 0 | if (!winpr_HMAC_Update(hmac, (void*)&value, 4)) |
1537 | 0 | goto hmac_fail; |
1538 | 0 | if (!winpr_HMAC_Update(hmac, data, length)) |
1539 | 0 | goto hmac_fail; |
1540 | 0 | if (!winpr_HMAC_Final(hmac, digest, WINPR_MD5_DIGEST_LENGTH)) |
1541 | 0 | goto hmac_fail; |
1542 | 0 | } |
1543 | | |
1544 | 0 | success = TRUE; |
1545 | |
|
1546 | 0 | hmac_fail: |
1547 | 0 | winpr_HMAC_Free(hmac); |
1548 | 0 | if (!success) |
1549 | 0 | { |
1550 | 0 | free(data); |
1551 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
1552 | 0 | } |
1553 | | |
1554 | | /* Encrypt message using with RC4, result overwrites original buffer */ |
1555 | 0 | if ((data_buffer->BufferType & SECBUFFER_READONLY) == 0) |
1556 | 0 | { |
1557 | 0 | if (context->confidentiality) |
1558 | 0 | { |
1559 | 0 | if (!winpr_RC4_Update(context->SendRc4Seal, length, (BYTE*)data, |
1560 | 0 | (BYTE*)data_buffer->pvBuffer)) |
1561 | 0 | { |
1562 | 0 | free(data); |
1563 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
1564 | 0 | } |
1565 | 0 | } |
1566 | 0 | else |
1567 | 0 | CopyMemory(data_buffer->pvBuffer, data, length); |
1568 | 0 | } |
1569 | | |
1570 | | #ifdef WITH_DEBUG_NTLM |
1571 | | WLog_DBG(TAG, "Data Buffer (length = %" PRIu32 ")", length); |
1572 | | winpr_HexDump(TAG, WLOG_DEBUG, data, length); |
1573 | | WLog_DBG(TAG, "Encrypted Data Buffer (length = %" PRIu32 ")", data_buffer->cbBuffer); |
1574 | | winpr_HexDump(TAG, WLOG_DEBUG, data_buffer->pvBuffer, data_buffer->cbBuffer); |
1575 | | #endif |
1576 | 0 | free(data); |
1577 | | /* RC4-encrypt first 8 bytes of digest */ |
1578 | 0 | if (!winpr_RC4_Update(context->SendRc4Seal, 8, digest, checksum)) |
1579 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
1580 | 0 | if ((signature_buffer->BufferType & SECBUFFER_READONLY) == 0) |
1581 | 0 | { |
1582 | 0 | BYTE* signature = signature_buffer->pvBuffer; |
1583 | | /* Concatenate version, ciphertext and sequence number to build signature */ |
1584 | 0 | winpr_Data_Write_UINT32(signature, version); |
1585 | 0 | CopyMemory(&signature[4], (void*)checksum, 8); |
1586 | 0 | winpr_Data_Write_UINT32(&signature[12], SeqNo); |
1587 | 0 | } |
1588 | 0 | context->SendSeqNum++; |
1589 | | #ifdef WITH_DEBUG_NTLM |
1590 | | WLog_DBG(TAG, "Signature (length = %" PRIu32 ")", signature_buffer->cbBuffer); |
1591 | | winpr_HexDump(TAG, WLOG_DEBUG, signature_buffer->pvBuffer, signature_buffer->cbBuffer); |
1592 | | #endif |
1593 | 0 | return SEC_E_OK; |
1594 | 0 | } |
1595 | | |
1596 | | static SECURITY_STATUS SEC_ENTRY ntlm_DecryptMessage(PCtxtHandle phContext, PSecBufferDesc pMessage, |
1597 | | ULONG MessageSeqNo, |
1598 | | WINPR_ATTR_UNUSED PULONG pfQOP) |
1599 | 0 | { |
1600 | 0 | const UINT32 SeqNo = (UINT32)MessageSeqNo; |
1601 | 0 | UINT32 value = 0; |
1602 | 0 | BYTE digest[WINPR_MD5_DIGEST_LENGTH] = WINPR_C_ARRAY_INIT; |
1603 | 0 | BYTE checksum[8] = WINPR_C_ARRAY_INIT; |
1604 | 0 | UINT32 version = 1; |
1605 | 0 | BYTE expected_signature[WINPR_MD5_DIGEST_LENGTH] = WINPR_C_ARRAY_INIT; |
1606 | 0 | PSecBuffer data_buffer = nullptr; |
1607 | 0 | PSecBuffer signature_buffer = nullptr; |
1608 | 0 | NTLM_CONTEXT* context = (NTLM_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext); |
1609 | 0 | if (!check_context(context)) |
1610 | 0 | return SEC_E_INVALID_HANDLE; |
1611 | | |
1612 | 0 | for (ULONG index = 0; index < pMessage->cBuffers; index++) |
1613 | 0 | { |
1614 | 0 | if (pMessage->pBuffers[index].BufferType == SECBUFFER_DATA) |
1615 | 0 | data_buffer = &pMessage->pBuffers[index]; |
1616 | 0 | else if (pMessage->pBuffers[index].BufferType == SECBUFFER_TOKEN) |
1617 | 0 | signature_buffer = &pMessage->pBuffers[index]; |
1618 | 0 | } |
1619 | |
|
1620 | 0 | if (!data_buffer) |
1621 | 0 | return SEC_E_INVALID_TOKEN; |
1622 | | |
1623 | 0 | if (!signature_buffer) |
1624 | 0 | return SEC_E_INVALID_TOKEN; |
1625 | | |
1626 | | /* Copy original data buffer */ |
1627 | 0 | const ULONG length = data_buffer->cbBuffer; |
1628 | 0 | void* data = malloc(length); |
1629 | |
|
1630 | 0 | if (!data) |
1631 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
1632 | | |
1633 | 0 | CopyMemory(data, data_buffer->pvBuffer, length); |
1634 | | |
1635 | | /* Decrypt message using with RC4, result overwrites original buffer */ |
1636 | |
|
1637 | 0 | if (context->confidentiality) |
1638 | 0 | { |
1639 | 0 | if (!winpr_RC4_Update(context->RecvRc4Seal, length, (BYTE*)data, |
1640 | 0 | (BYTE*)data_buffer->pvBuffer)) |
1641 | 0 | { |
1642 | 0 | free(data); |
1643 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
1644 | 0 | } |
1645 | 0 | } |
1646 | 0 | else |
1647 | 0 | CopyMemory(data_buffer->pvBuffer, data, length); |
1648 | | |
1649 | | /* Compute the HMAC-MD5 hash of ConcatenationOf(seq_num,data) using the client signing key */ |
1650 | 0 | WINPR_HMAC_CTX* hmac = winpr_HMAC_New(); |
1651 | |
|
1652 | 0 | BOOL success = FALSE; |
1653 | 0 | { |
1654 | 0 | if (!hmac) |
1655 | 0 | goto hmac_fail; |
1656 | | |
1657 | 0 | if (!winpr_HMAC_Init(hmac, WINPR_MD_MD5, context->RecvSigningKey, WINPR_MD5_DIGEST_LENGTH)) |
1658 | 0 | goto hmac_fail; |
1659 | | |
1660 | 0 | winpr_Data_Write_UINT32(&value, SeqNo); |
1661 | |
|
1662 | 0 | if (!winpr_HMAC_Update(hmac, (void*)&value, 4)) |
1663 | 0 | goto hmac_fail; |
1664 | 0 | if (!winpr_HMAC_Update(hmac, data_buffer->pvBuffer, data_buffer->cbBuffer)) |
1665 | 0 | goto hmac_fail; |
1666 | 0 | if (!winpr_HMAC_Final(hmac, digest, WINPR_MD5_DIGEST_LENGTH)) |
1667 | 0 | goto hmac_fail; |
1668 | | |
1669 | 0 | success = TRUE; |
1670 | 0 | } |
1671 | 0 | hmac_fail: |
1672 | 0 | winpr_HMAC_Free(hmac); |
1673 | 0 | if (!success) |
1674 | 0 | { |
1675 | 0 | free(data); |
1676 | 0 | return SEC_E_INSUFFICIENT_MEMORY; |
1677 | 0 | } |
1678 | | |
1679 | | #ifdef WITH_DEBUG_NTLM |
1680 | | WLog_DBG(TAG, "Encrypted Data Buffer (length = %" PRIu32 ")", length); |
1681 | | winpr_HexDump(TAG, WLOG_DEBUG, data, length); |
1682 | | WLog_DBG(TAG, "Data Buffer (length = %" PRIu32 ")", data_buffer->cbBuffer); |
1683 | | winpr_HexDump(TAG, WLOG_DEBUG, data_buffer->pvBuffer, data_buffer->cbBuffer); |
1684 | | #endif |
1685 | 0 | free(data); |
1686 | | /* RC4-encrypt first 8 bytes of digest */ |
1687 | 0 | if (!winpr_RC4_Update(context->RecvRc4Seal, 8, digest, checksum)) |
1688 | 0 | return SEC_E_MESSAGE_ALTERED; |
1689 | | |
1690 | | /* Concatenate version, ciphertext and sequence number to build signature */ |
1691 | 0 | winpr_Data_Write_UINT32(expected_signature, version); |
1692 | 0 | CopyMemory(&expected_signature[4], (void*)checksum, 8); |
1693 | 0 | winpr_Data_Write_UINT32(&expected_signature[12], SeqNo); |
1694 | 0 | context->RecvSeqNum++; |
1695 | |
|
1696 | 0 | if (memcmp(signature_buffer->pvBuffer, expected_signature, 16) != 0) |
1697 | 0 | { |
1698 | | /* signature verification failed! */ |
1699 | 0 | WLog_ERR(TAG, "signature verification failed, something nasty is going on!"); |
1700 | | #ifdef WITH_DEBUG_NTLM |
1701 | | WLog_ERR(TAG, "Expected Signature:"); |
1702 | | winpr_HexDump(TAG, WLOG_ERROR, expected_signature, 16); |
1703 | | WLog_ERR(TAG, "Actual Signature:"); |
1704 | | winpr_HexDump(TAG, WLOG_ERROR, (BYTE*)signature_buffer->pvBuffer, 16); |
1705 | | #endif |
1706 | 0 | return SEC_E_MESSAGE_ALTERED; |
1707 | 0 | } |
1708 | | |
1709 | 0 | return SEC_E_OK; |
1710 | 0 | } |
1711 | | |
1712 | | static SECURITY_STATUS SEC_ENTRY ntlm_MakeSignature(PCtxtHandle phContext, |
1713 | | WINPR_ATTR_UNUSED ULONG fQOP, |
1714 | | PSecBufferDesc pMessage, ULONG MessageSeqNo) |
1715 | 0 | { |
1716 | 0 | SECURITY_STATUS status = SEC_E_INTERNAL_ERROR; |
1717 | 0 | PSecBuffer data_buffer = nullptr; |
1718 | 0 | PSecBuffer sig_buffer = nullptr; |
1719 | 0 | UINT32 seq_no = 0; |
1720 | 0 | BYTE digest[WINPR_MD5_DIGEST_LENGTH] = WINPR_C_ARRAY_INIT; |
1721 | 0 | BYTE checksum[8] = WINPR_C_ARRAY_INIT; |
1722 | |
|
1723 | 0 | NTLM_CONTEXT* context = sspi_SecureHandleGetLowerPointer(phContext); |
1724 | 0 | if (!check_context(context)) |
1725 | 0 | return SEC_E_INVALID_HANDLE; |
1726 | | |
1727 | 0 | for (ULONG i = 0; i < pMessage->cBuffers; i++) |
1728 | 0 | { |
1729 | 0 | if (pMessage->pBuffers[i].BufferType == SECBUFFER_DATA) |
1730 | 0 | data_buffer = &pMessage->pBuffers[i]; |
1731 | 0 | else if (pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN) |
1732 | 0 | sig_buffer = &pMessage->pBuffers[i]; |
1733 | 0 | } |
1734 | |
|
1735 | 0 | if (!data_buffer || !sig_buffer) |
1736 | 0 | return SEC_E_INVALID_TOKEN; |
1737 | | |
1738 | 0 | WINPR_HMAC_CTX* hmac = winpr_HMAC_New(); |
1739 | |
|
1740 | 0 | if (!winpr_HMAC_Init(hmac, WINPR_MD_MD5, context->SendSigningKey, WINPR_MD5_DIGEST_LENGTH)) |
1741 | 0 | goto fail; |
1742 | | |
1743 | 0 | winpr_Data_Write_UINT32(&seq_no, MessageSeqNo); |
1744 | 0 | if (!winpr_HMAC_Update(hmac, (BYTE*)&seq_no, 4)) |
1745 | 0 | goto fail; |
1746 | 0 | if (!winpr_HMAC_Update(hmac, data_buffer->pvBuffer, data_buffer->cbBuffer)) |
1747 | 0 | goto fail; |
1748 | 0 | if (!winpr_HMAC_Final(hmac, digest, WINPR_MD5_DIGEST_LENGTH)) |
1749 | 0 | goto fail; |
1750 | | |
1751 | 0 | if (!winpr_RC4_Update(context->SendRc4Seal, 8, digest, checksum)) |
1752 | 0 | goto fail; |
1753 | | |
1754 | 0 | BYTE* signature = sig_buffer->pvBuffer; |
1755 | 0 | winpr_Data_Write_UINT32(signature, 1L); |
1756 | 0 | CopyMemory(&signature[4], checksum, 8); |
1757 | 0 | winpr_Data_Write_UINT32(&signature[12], seq_no); |
1758 | 0 | sig_buffer->cbBuffer = 16; |
1759 | |
|
1760 | 0 | status = SEC_E_OK; |
1761 | |
|
1762 | 0 | fail: |
1763 | 0 | winpr_HMAC_Free(hmac); |
1764 | 0 | return status; |
1765 | 0 | } |
1766 | | |
1767 | | WINPR_ATTR_NODISCARD |
1768 | | static SECURITY_STATUS SEC_ENTRY ntlm_VerifySignature(PCtxtHandle phContext, |
1769 | | PSecBufferDesc pMessage, ULONG MessageSeqNo, |
1770 | | WINPR_ATTR_UNUSED PULONG pfQOP) |
1771 | 0 | { |
1772 | 0 | SECURITY_STATUS status = SEC_E_INTERNAL_ERROR; |
1773 | 0 | PSecBuffer data_buffer = nullptr; |
1774 | 0 | PSecBuffer sig_buffer = nullptr; |
1775 | 0 | UINT32 seq_no = 0; |
1776 | 0 | BYTE digest[WINPR_MD5_DIGEST_LENGTH] = WINPR_C_ARRAY_INIT; |
1777 | 0 | BYTE checksum[8] = WINPR_C_ARRAY_INIT; |
1778 | 0 | BYTE signature[16] = WINPR_C_ARRAY_INIT; |
1779 | |
|
1780 | 0 | NTLM_CONTEXT* context = sspi_SecureHandleGetLowerPointer(phContext); |
1781 | 0 | if (!check_context(context)) |
1782 | 0 | return SEC_E_INVALID_HANDLE; |
1783 | | |
1784 | 0 | for (ULONG i = 0; i < pMessage->cBuffers; i++) |
1785 | 0 | { |
1786 | 0 | if (pMessage->pBuffers[i].BufferType == SECBUFFER_DATA) |
1787 | 0 | data_buffer = &pMessage->pBuffers[i]; |
1788 | 0 | else if (pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN) |
1789 | 0 | sig_buffer = &pMessage->pBuffers[i]; |
1790 | 0 | } |
1791 | |
|
1792 | 0 | if (!data_buffer || !sig_buffer) |
1793 | 0 | return SEC_E_INVALID_TOKEN; |
1794 | | |
1795 | 0 | WINPR_HMAC_CTX* hmac = winpr_HMAC_New(); |
1796 | |
|
1797 | 0 | if (!winpr_HMAC_Init(hmac, WINPR_MD_MD5, context->RecvSigningKey, WINPR_MD5_DIGEST_LENGTH)) |
1798 | 0 | goto fail; |
1799 | | |
1800 | 0 | winpr_Data_Write_UINT32(&seq_no, MessageSeqNo); |
1801 | 0 | if (!winpr_HMAC_Update(hmac, (BYTE*)&seq_no, 4)) |
1802 | 0 | goto fail; |
1803 | 0 | if (!winpr_HMAC_Update(hmac, data_buffer->pvBuffer, data_buffer->cbBuffer)) |
1804 | 0 | goto fail; |
1805 | 0 | if (!winpr_HMAC_Final(hmac, digest, WINPR_MD5_DIGEST_LENGTH)) |
1806 | 0 | goto fail; |
1807 | | |
1808 | 0 | if (!winpr_RC4_Update(context->RecvRc4Seal, 8, digest, checksum)) |
1809 | 0 | goto fail; |
1810 | | |
1811 | 0 | winpr_Data_Write_UINT32(signature, 1L); |
1812 | 0 | CopyMemory(&signature[4], checksum, 8); |
1813 | 0 | winpr_Data_Write_UINT32(&signature[12], seq_no); |
1814 | |
|
1815 | 0 | status = SEC_E_OK; |
1816 | 0 | if (memcmp(sig_buffer->pvBuffer, signature, 16) != 0) |
1817 | 0 | status = SEC_E_MESSAGE_ALTERED; |
1818 | |
|
1819 | 0 | fail: |
1820 | 0 | winpr_HMAC_Free(hmac); |
1821 | 0 | return status; |
1822 | 0 | } |
1823 | | |
1824 | | const SecurityFunctionTableA NTLM_SecurityFunctionTableA = { |
1825 | | 3, /* dwVersion */ |
1826 | | nullptr, /* EnumerateSecurityPackages */ |
1827 | | ntlm_QueryCredentialsAttributesA, /* QueryCredentialsAttributes */ |
1828 | | ntlm_AcquireCredentialsHandleA, /* AcquireCredentialsHandle */ |
1829 | | ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */ |
1830 | | nullptr, /* Reserved2 */ |
1831 | | ntlm_InitializeSecurityContextA, /* InitializeSecurityContext */ |
1832 | | ntlm_AcceptSecurityContext, /* AcceptSecurityContext */ |
1833 | | nullptr, /* CompleteAuthToken */ |
1834 | | ntlm_DeleteSecurityContext, /* DeleteSecurityContext */ |
1835 | | nullptr, /* ApplyControlToken */ |
1836 | | ntlm_QueryContextAttributesA, /* QueryContextAttributes */ |
1837 | | ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */ |
1838 | | ntlm_RevertSecurityContext, /* RevertSecurityContext */ |
1839 | | ntlm_MakeSignature, /* MakeSignature */ |
1840 | | ntlm_VerifySignature, /* VerifySignature */ |
1841 | | nullptr, /* FreeContextBuffer */ |
1842 | | nullptr, /* QuerySecurityPackageInfo */ |
1843 | | nullptr, /* Reserved3 */ |
1844 | | nullptr, /* Reserved4 */ |
1845 | | nullptr, /* ExportSecurityContext */ |
1846 | | nullptr, /* ImportSecurityContext */ |
1847 | | nullptr, /* AddCredentials */ |
1848 | | nullptr, /* Reserved8 */ |
1849 | | nullptr, /* QuerySecurityContextToken */ |
1850 | | ntlm_EncryptMessage, /* EncryptMessage */ |
1851 | | ntlm_DecryptMessage, /* DecryptMessage */ |
1852 | | ntlm_SetContextAttributesA, /* SetContextAttributes */ |
1853 | | ntlm_SetCredentialsAttributesA, /* SetCredentialsAttributes */ |
1854 | | }; |
1855 | | |
1856 | | const SecurityFunctionTableW NTLM_SecurityFunctionTableW = { |
1857 | | 3, /* dwVersion */ |
1858 | | nullptr, /* EnumerateSecurityPackages */ |
1859 | | ntlm_QueryCredentialsAttributesW, /* QueryCredentialsAttributes */ |
1860 | | ntlm_AcquireCredentialsHandleW, /* AcquireCredentialsHandle */ |
1861 | | ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */ |
1862 | | nullptr, /* Reserved2 */ |
1863 | | ntlm_InitializeSecurityContextW, /* InitializeSecurityContext */ |
1864 | | ntlm_AcceptSecurityContext, /* AcceptSecurityContext */ |
1865 | | nullptr, /* CompleteAuthToken */ |
1866 | | ntlm_DeleteSecurityContext, /* DeleteSecurityContext */ |
1867 | | nullptr, /* ApplyControlToken */ |
1868 | | ntlm_QueryContextAttributesW, /* QueryContextAttributes */ |
1869 | | ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */ |
1870 | | ntlm_RevertSecurityContext, /* RevertSecurityContext */ |
1871 | | ntlm_MakeSignature, /* MakeSignature */ |
1872 | | ntlm_VerifySignature, /* VerifySignature */ |
1873 | | nullptr, /* FreeContextBuffer */ |
1874 | | nullptr, /* QuerySecurityPackageInfo */ |
1875 | | nullptr, /* Reserved3 */ |
1876 | | nullptr, /* Reserved4 */ |
1877 | | nullptr, /* ExportSecurityContext */ |
1878 | | nullptr, /* ImportSecurityContext */ |
1879 | | nullptr, /* AddCredentials */ |
1880 | | nullptr, /* Reserved8 */ |
1881 | | nullptr, /* QuerySecurityContextToken */ |
1882 | | ntlm_EncryptMessage, /* EncryptMessage */ |
1883 | | ntlm_DecryptMessage, /* DecryptMessage */ |
1884 | | ntlm_SetContextAttributesW, /* SetContextAttributes */ |
1885 | | ntlm_SetCredentialsAttributesW, /* SetCredentialsAttributes */ |
1886 | | }; |
1887 | | |
1888 | | const SecPkgInfoA NTLM_SecPkgInfoA = { |
1889 | | 0x00082B37, /* fCapabilities */ |
1890 | | 1, /* wVersion */ |
1891 | | 0x000A, /* wRPCID */ |
1892 | | 0x00000B48, /* cbMaxToken */ |
1893 | | "NTLM", /* Name */ |
1894 | | "NTLM Security Package" /* Comment */ |
1895 | | }; |
1896 | | |
1897 | | static WCHAR NTLM_SecPkgInfoW_NameBuffer[32] = WINPR_C_ARRAY_INIT; |
1898 | | static WCHAR NTLM_SecPkgInfoW_CommentBuffer[32] = WINPR_C_ARRAY_INIT; |
1899 | | |
1900 | | const SecPkgInfoW NTLM_SecPkgInfoW = { |
1901 | | 0x00082B37, /* fCapabilities */ |
1902 | | 1, /* wVersion */ |
1903 | | 0x000A, /* wRPCID */ |
1904 | | 0x00000B48, /* cbMaxToken */ |
1905 | | NTLM_SecPkgInfoW_NameBuffer, /* Name */ |
1906 | | NTLM_SecPkgInfoW_CommentBuffer /* Comment */ |
1907 | | }; |
1908 | | |
1909 | | char* ntlm_negotiate_flags_string(char* buffer, size_t size, UINT32 flags) |
1910 | 0 | { |
1911 | 0 | if (!buffer || (size == 0)) |
1912 | 0 | return buffer; |
1913 | | |
1914 | 0 | (void)_snprintf(buffer, size, "[0x%08" PRIx32 "] ", flags); |
1915 | |
|
1916 | 0 | for (int x = 0; x < 31; x++) |
1917 | 0 | { |
1918 | 0 | const UINT32 mask = 1u << x; |
1919 | 0 | size_t len = strnlen(buffer, size); |
1920 | 0 | if (flags & mask) |
1921 | 0 | { |
1922 | 0 | const char* str = ntlm_get_negotiate_string(mask); |
1923 | 0 | const size_t flen = strlen(str); |
1924 | |
|
1925 | 0 | if ((len > 0) && (buffer[len - 1] != ' ')) |
1926 | 0 | { |
1927 | 0 | if (size - len < 1) |
1928 | 0 | break; |
1929 | 0 | winpr_str_append("|", buffer, size, nullptr); |
1930 | 0 | len++; |
1931 | 0 | } |
1932 | | |
1933 | 0 | if (size - len < flen) |
1934 | 0 | break; |
1935 | 0 | winpr_str_append(str, buffer, size, nullptr); |
1936 | 0 | } |
1937 | 0 | } |
1938 | |
|
1939 | 0 | return buffer; |
1940 | 0 | } |
1941 | | |
1942 | | const char* ntlm_message_type_string(UINT32 messageType) |
1943 | 0 | { |
1944 | 0 | switch (messageType) |
1945 | 0 | { |
1946 | 0 | case MESSAGE_TYPE_NEGOTIATE: |
1947 | 0 | return "MESSAGE_TYPE_NEGOTIATE"; |
1948 | 0 | case MESSAGE_TYPE_CHALLENGE: |
1949 | 0 | return "MESSAGE_TYPE_CHALLENGE"; |
1950 | 0 | case MESSAGE_TYPE_AUTHENTICATE: |
1951 | 0 | return "MESSAGE_TYPE_AUTHENTICATE"; |
1952 | 0 | default: |
1953 | 0 | return "MESSAGE_TYPE_UNKNOWN"; |
1954 | 0 | } |
1955 | 0 | } |
1956 | | |
1957 | | const char* ntlm_state_string(NTLM_STATE state) |
1958 | 0 | { |
1959 | 0 | switch (state) |
1960 | 0 | { |
1961 | 0 | case NTLM_STATE_INITIAL: |
1962 | 0 | return "NTLM_STATE_INITIAL"; |
1963 | 0 | case NTLM_STATE_NEGOTIATE: |
1964 | 0 | return "NTLM_STATE_NEGOTIATE"; |
1965 | 0 | case NTLM_STATE_CHALLENGE: |
1966 | 0 | return "NTLM_STATE_CHALLENGE"; |
1967 | 0 | case NTLM_STATE_AUTHENTICATE: |
1968 | 0 | return "NTLM_STATE_AUTHENTICATE"; |
1969 | 0 | case NTLM_STATE_FINAL: |
1970 | 0 | return "NTLM_STATE_FINAL"; |
1971 | 0 | default: |
1972 | 0 | return "NTLM_STATE_UNKNOWN"; |
1973 | 0 | } |
1974 | 0 | } |
1975 | | void ntlm_change_state(NTLM_CONTEXT* ntlm, NTLM_STATE state) |
1976 | 0 | { |
1977 | 0 | WINPR_ASSERT(ntlm); |
1978 | 0 | WLog_DBG(TAG, "change state from %s to %s", ntlm_state_string(ntlm->state), |
1979 | 0 | ntlm_state_string(state)); |
1980 | 0 | ntlm->state = state; |
1981 | 0 | } |
1982 | | |
1983 | | NTLM_STATE ntlm_get_state(NTLM_CONTEXT* ntlm) |
1984 | 0 | { |
1985 | 0 | WINPR_ASSERT(ntlm); |
1986 | 0 | return ntlm->state; |
1987 | 0 | } |
1988 | | |
1989 | | BOOL ntlm_reset_cipher_state(PSecHandle phContext) |
1990 | 0 | { |
1991 | 0 | NTLM_CONTEXT* context = sspi_SecureHandleGetLowerPointer(phContext); |
1992 | |
|
1993 | 0 | if (context) |
1994 | 0 | { |
1995 | 0 | if (!check_context(context)) |
1996 | 0 | return FALSE; |
1997 | | |
1998 | 0 | winpr_RC4_Free(context->SendRc4Seal); |
1999 | 0 | winpr_RC4_Free(context->RecvRc4Seal); |
2000 | 0 | context->SendRc4Seal = winpr_RC4_New(context->RecvSealingKey, 16); |
2001 | 0 | context->RecvRc4Seal = winpr_RC4_New(context->SendSealingKey, 16); |
2002 | |
|
2003 | 0 | if (!context->SendRc4Seal) |
2004 | 0 | { |
2005 | 0 | WLog_ERR(TAG, "Failed to allocate context->SendRc4Seal"); |
2006 | 0 | return FALSE; |
2007 | 0 | } |
2008 | 0 | if (!context->RecvRc4Seal) |
2009 | 0 | { |
2010 | 0 | WLog_ERR(TAG, "Failed to allocate context->RecvRc4Seal"); |
2011 | 0 | return FALSE; |
2012 | 0 | } |
2013 | 0 | } |
2014 | | |
2015 | 0 | return TRUE; |
2016 | 0 | } |
2017 | | |
2018 | | BOOL NTLM_init(void) |
2019 | 0 | { |
2020 | 0 | InitializeConstWCharFromUtf8(NTLM_SecPkgInfoA.Name, NTLM_SecPkgInfoW_NameBuffer, |
2021 | 0 | ARRAYSIZE(NTLM_SecPkgInfoW_NameBuffer)); |
2022 | 0 | InitializeConstWCharFromUtf8(NTLM_SecPkgInfoA.Comment, NTLM_SecPkgInfoW_CommentBuffer, |
2023 | 0 | ARRAYSIZE(NTLM_SecPkgInfoW_CommentBuffer)); |
2024 | |
|
2025 | 0 | return TRUE; |
2026 | 0 | } |
2027 | | |
2028 | | BOOL ntlm_SecBufferRealloc(SecBuffer* buffer, ULONG len) |
2029 | 0 | { |
2030 | 0 | sspi_SecBufferFree(buffer); |
2031 | 0 | return sspi_SecBufferAlloc(buffer, len) != nullptr; |
2032 | 0 | } |