Coverage Report

Created: 2026-05-11 06:55

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