Coverage Report

Created: 2025-07-01 06:46

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