Coverage Report

Created: 2026-02-26 06:54

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