Coverage Report

Created: 2023-09-25 06:56

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