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