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