Coverage Report

Created: 2023-09-25 06:56

/src/FreeRDP/winpr/libwinpr/sspi/Negotiate/negotiate.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * WinPR: Windows Portable Runtime
3
 * Negotiate Security Package
4
 *
5
 * Copyright 2011-2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 * Copyright 2017 Dorian Ducournau <dorian.ducournau@gmail.com>
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
#include <winpr/config.h>
22
23
#include <winpr/crt.h>
24
#include <winpr/wtypes.h>
25
#include <winpr/assert.h>
26
#include <winpr/sspi.h>
27
#include <winpr/tchar.h>
28
#include <winpr/assert.h>
29
#include <winpr/registry.h>
30
#include <winpr/build-config.h>
31
#include <winpr/asn1.h>
32
33
#include "negotiate.h"
34
35
#include "../NTLM/ntlm.h"
36
#include "../NTLM/ntlm_export.h"
37
#include "../Kerberos/kerberos.h"
38
#include "../sspi.h"
39
#include "../../log.h"
40
#define TAG WINPR_TAG("negotiate")
41
42
static const char NEGO_REG_KEY[] =
43
    "Software\\" WINPR_VENDOR_STRING "\\" WINPR_PRODUCT_STRING "\\SSPI\\Negotiate";
44
45
typedef struct
46
{
47
  const TCHAR* name;
48
  const SecurityFunctionTableA* table;
49
  const SecurityFunctionTableW* table_w;
50
} SecPkg;
51
52
struct Mech_st
53
{
54
  const WinPrAsn1_OID* oid;
55
  const SecPkg* pkg;
56
  const UINT flags;
57
  const BOOL preferred;
58
};
59
60
typedef struct
61
{
62
  const Mech* mech;
63
  CredHandle cred;
64
  BOOL valid;
65
} MechCred;
66
67
const SecPkgInfoA NEGOTIATE_SecPkgInfoA = {
68
  0x00083BB3,                    /* fCapabilities */
69
  1,                             /* wVersion */
70
  0x0009,                        /* wRPCID */
71
  0x00002FE0,                    /* cbMaxToken */
72
  "Negotiate",                   /* Name */
73
  "Microsoft Package Negotiator" /* Comment */
74
};
75
76
static WCHAR NEGOTIATE_SecPkgInfoW_Name[] = { 'N', 'e', 'g', 'o', 't', 'i', 'a', 't', 'e', '\0' };
77
78
static WCHAR NEGOTIATE_SecPkgInfoW_Comment[] = { 'M', 'i', 'c', 'r', 'o', 's', 'o', 'f', 't', ' ',
79
                                               'P', 'a', 'c', 'k', 'a', 'g', 'e', ' ', 'N', 'e',
80
                                               'g', 'o', 't', 'i', 'a', 't', 'o', 'r', '\0' };
81
82
const SecPkgInfoW NEGOTIATE_SecPkgInfoW = {
83
  0x00083BB3,                   /* fCapabilities */
84
  1,                            /* wVersion */
85
  0x0009,                       /* wRPCID */
86
  0x00002FE0,                   /* cbMaxToken */
87
  NEGOTIATE_SecPkgInfoW_Name,   /* Name */
88
  NEGOTIATE_SecPkgInfoW_Comment /* Comment */
89
};
90
91
static const WinPrAsn1_OID spnego_OID = { 6, (BYTE*)"\x2b\x06\x01\x05\x05\x02" };
92
static const WinPrAsn1_OID kerberos_u2u_OID = { 10,
93
                                              (BYTE*)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x02\x03" };
94
static const WinPrAsn1_OID kerberos_OID = { 9, (BYTE*)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x02" };
95
static const WinPrAsn1_OID kerberos_wrong_OID = { 9,
96
                                                (BYTE*)"\x2a\x86\x48\x82\xf7\x12\x01\x02\x02" };
97
static const WinPrAsn1_OID ntlm_OID = { 10, (BYTE*)"\x2b\x06\x01\x04\x01\x82\x37\x02\x02\x0a" };
98
99
#ifdef WITH_KRB5
100
static const SecPkg SecPkgTable[] = {
101
  { KERBEROS_SSP_NAME, &KERBEROS_SecurityFunctionTableA, &KERBEROS_SecurityFunctionTableW },
102
  { NTLM_SSP_NAME, &NTLM_SecurityFunctionTableA, &NTLM_SecurityFunctionTableW }
103
};
104
105
static const Mech MechTable[] = {
106
  { &kerberos_u2u_OID, &SecPkgTable[0], ISC_REQ_INTEGRITY | ISC_REQ_USE_SESSION_KEY, TRUE },
107
  { &kerberos_OID, &SecPkgTable[0], ISC_REQ_INTEGRITY, TRUE },
108
  { &ntlm_OID, &SecPkgTable[1], 0, FALSE },
109
};
110
#else
111
static const SecPkg SecPkgTable[] = { { NTLM_SSP_NAME, &NTLM_SecurityFunctionTableA,
112
                                      &NTLM_SecurityFunctionTableW } };
113
114
static const Mech MechTable[] = {
115
  { &ntlm_OID, &SecPkgTable[0], 0, FALSE },
116
};
117
#endif
118
119
static const size_t MECH_COUNT = sizeof(MechTable) / sizeof(Mech);
120
121
enum NegState
122
{
123
  NOSTATE = -1,
124
  ACCEPT_COMPLETED,
125
  ACCEPT_INCOMPLETE,
126
  REJECT,
127
  REQUEST_MIC
128
};
129
130
typedef struct
131
{
132
  enum NegState negState;
133
  BOOL init;
134
  WinPrAsn1_OID supportedMech;
135
  SecBuffer mechTypes;
136
  SecBuffer mechToken;
137
  SecBuffer mic;
138
} NegToken;
139
140
static const NegToken empty_neg_token = { NOSTATE,        FALSE,          { 0, NULL },
141
                                        { 0, 0, NULL }, { 0, 0, NULL }, { 0, 0, NULL } };
142
143
static NEGOTIATE_CONTEXT* negotiate_ContextNew(NEGOTIATE_CONTEXT* init_context)
144
0
{
145
0
  NEGOTIATE_CONTEXT* context = NULL;
146
147
0
  WINPR_ASSERT(init_context);
148
149
0
  context = calloc(1, sizeof(NEGOTIATE_CONTEXT));
150
0
  if (!context)
151
0
    return NULL;
152
153
0
  if (init_context->spnego)
154
0
  {
155
0
    init_context->mechTypes.pvBuffer = malloc(init_context->mechTypes.cbBuffer);
156
0
    if (!init_context->mechTypes.pvBuffer)
157
0
    {
158
0
      free(context);
159
0
      return NULL;
160
0
    }
161
0
  }
162
163
0
  *context = *init_context;
164
165
0
  return context;
166
0
}
167
168
static void negotiate_ContextFree(NEGOTIATE_CONTEXT* context)
169
0
{
170
0
  WINPR_ASSERT(context);
171
172
0
  if (context->mechTypes.pvBuffer)
173
0
    free(context->mechTypes.pvBuffer);
174
0
  free(context);
175
0
}
176
177
static const char* negotiate_mech_name(const WinPrAsn1_OID* oid)
178
0
{
179
0
  if (sspi_gss_oid_compare(oid, &spnego_OID))
180
0
    return "SPNEGO (1.3.6.1.5.5.2)";
181
0
  else if (sspi_gss_oid_compare(oid, &kerberos_u2u_OID))
182
0
    return "Kerberos user to user (1.2.840.113554.1.2.2.3)";
183
0
  else if (sspi_gss_oid_compare(oid, &kerberos_OID))
184
0
    return "Kerberos (1.2.840.113554.1.2.2)";
185
0
  else if (sspi_gss_oid_compare(oid, &kerberos_wrong_OID))
186
0
    return "Kerberos [wrong OID] (1.2.840.48018.1.2.2)";
187
0
  else if (sspi_gss_oid_compare(oid, &ntlm_OID))
188
0
    return "NTLM (1.3.6.1.4.1.311.2.2.10)";
189
0
  else
190
0
    return "Unknown mechanism";
191
0
}
192
193
static const Mech* negotiate_GetMechByOID(const WinPrAsn1_OID* oid)
194
0
{
195
0
  WINPR_ASSERT(oid);
196
197
0
  WinPrAsn1_OID testOid = *oid;
198
199
0
  if (sspi_gss_oid_compare(&testOid, &kerberos_wrong_OID))
200
0
  {
201
0
    testOid.len = kerberos_OID.len;
202
0
    testOid.data = kerberos_OID.data;
203
0
  }
204
205
0
  for (size_t i = 0; i < MECH_COUNT; i++)
206
0
  {
207
0
    if (sspi_gss_oid_compare(&testOid, MechTable[i].oid))
208
0
      return &MechTable[i];
209
0
  }
210
0
  return NULL;
211
0
}
212
213
static PSecHandle negotiate_FindCredential(MechCred* creds, const Mech* mech)
214
0
{
215
0
  WINPR_ASSERT(creds);
216
217
0
  if (!mech)
218
0
    return NULL;
219
220
0
  for (size_t i = 0; i < MECH_COUNT; i++)
221
0
  {
222
0
    MechCred* cred = &creds[i];
223
224
0
    if (cred->mech == mech)
225
0
    {
226
0
      if (cred->valid)
227
0
        return &cred->cred;
228
0
      return NULL;
229
0
    }
230
0
  }
231
232
0
  return NULL;
233
0
}
234
235
static BOOL negotiate_get_dword(HKEY hKey, const char* subkey, DWORD* pdwValue)
236
0
{
237
0
  DWORD dwValue = 0, dwType = 0;
238
0
  DWORD dwSize = sizeof(dwValue);
239
0
  LONG rc = RegQueryValueExA(hKey, subkey, NULL, &dwType, (BYTE*)&dwValue, &dwSize);
240
241
0
  if (rc != ERROR_SUCCESS)
242
0
    return FALSE;
243
0
  if (dwType != REG_DWORD)
244
0
    return FALSE;
245
246
0
  *pdwValue = dwValue;
247
0
  return TRUE;
248
0
}
249
250
static BOOL negotiate_get_config_from_auth_package_list(void* pAuthData, BOOL* kerberos, BOOL* ntlm)
251
0
{
252
0
  char* tok_ctx = NULL;
253
0
  char* tok_ptr = NULL;
254
0
  char* PackageList = NULL;
255
256
0
  if (!sspi_CopyAuthPackageListA((const SEC_WINNT_AUTH_IDENTITY_INFO*)pAuthData, &PackageList))
257
0
    return FALSE;
258
259
0
  tok_ptr = strtok_s(PackageList, ",", &tok_ctx);
260
261
0
  while (tok_ptr)
262
0
  {
263
0
    char* PackageName = tok_ptr;
264
0
    BOOL PackageInclude = TRUE;
265
266
0
    if (PackageName[0] == '!')
267
0
    {
268
0
      PackageName = &PackageName[1];
269
0
      PackageInclude = FALSE;
270
0
    }
271
272
0
    if (!_stricmp(PackageName, "ntlm"))
273
0
    {
274
0
      *ntlm = PackageInclude;
275
0
    }
276
0
    else if (!_stricmp(PackageName, "kerberos"))
277
0
    {
278
0
      *kerberos = PackageInclude;
279
0
    }
280
0
    else
281
0
    {
282
0
      WLog_WARN(TAG, "Unknown authentication package name: %s", PackageName);
283
0
    }
284
285
0
    tok_ptr = strtok_s(NULL, ",", &tok_ctx);
286
0
  }
287
288
0
  free(PackageList);
289
0
  return TRUE;
290
0
}
291
292
static BOOL negotiate_get_config(void* pAuthData, BOOL* kerberos, BOOL* ntlm)
293
0
{
294
0
  HKEY hKey = NULL;
295
0
  LONG rc;
296
297
0
  WINPR_ASSERT(kerberos);
298
0
  WINPR_ASSERT(ntlm);
299
300
0
#if !defined(WITH_KRB5_NO_NTLM_FALLBACK)
301
0
  *ntlm = TRUE;
302
#else
303
  *ntlm = FALSE;
304
#endif
305
0
  *kerberos = TRUE;
306
307
0
  if (negotiate_get_config_from_auth_package_list(pAuthData, kerberos, ntlm))
308
0
  {
309
0
    return TRUE; // use explicit authentication package list
310
0
  }
311
312
0
  rc = RegOpenKeyExA(HKEY_LOCAL_MACHINE, NEGO_REG_KEY, 0, KEY_READ | KEY_WOW64_64KEY, &hKey);
313
0
  if (rc == ERROR_SUCCESS)
314
0
  {
315
0
    DWORD dwValue;
316
317
0
    if (negotiate_get_dword(hKey, "kerberos", &dwValue))
318
0
      *kerberos = (dwValue != 0) ? TRUE : FALSE;
319
320
0
#if !defined(WITH_KRB5_NO_NTLM_FALLBACK)
321
0
    if (negotiate_get_dword(hKey, "ntlm", &dwValue))
322
0
      *ntlm = (dwValue != 0) ? TRUE : FALSE;
323
0
#endif
324
325
0
    RegCloseKey(hKey);
326
0
  }
327
328
0
  return TRUE;
329
0
}
330
331
static BOOL negotiate_write_neg_token(PSecBuffer output_buffer, NegToken* token)
332
0
{
333
0
  WINPR_ASSERT(output_buffer);
334
0
  WINPR_ASSERT(token);
335
336
0
  BOOL ret = FALSE;
337
0
  WinPrAsn1Encoder* enc = NULL;
338
0
  WinPrAsn1_MemoryChunk mechTypes = { token->mechTypes.cbBuffer, token->mechTypes.pvBuffer };
339
0
  WinPrAsn1_OctetString mechToken = { token->mechToken.cbBuffer, token->mechToken.pvBuffer };
340
0
  WinPrAsn1_OctetString mechListMic = { token->mic.cbBuffer, token->mic.pvBuffer };
341
0
  wStream s;
342
0
  size_t len;
343
344
0
  enc = WinPrAsn1Encoder_New(WINPR_ASN1_DER);
345
0
  if (!enc)
346
0
    return FALSE;
347
348
  /* For NegTokenInit wrap in an initialContextToken */
349
0
  if (token->init)
350
0
  {
351
    /* InitialContextToken [APPLICATION 0] IMPLICIT SEQUENCE */
352
0
    if (!WinPrAsn1EncAppContainer(enc, 0))
353
0
      goto cleanup;
354
355
    /* thisMech MechType OID */
356
0
    if (!WinPrAsn1EncOID(enc, &spnego_OID))
357
0
      goto cleanup;
358
0
  }
359
360
  /* innerContextToken [0] NegTokenInit or [1] NegTokenResp */
361
0
  if (!WinPrAsn1EncContextualSeqContainer(enc, token->init ? 0 : 1))
362
0
    goto cleanup;
363
364
0
  WLog_DBG(TAG, token->init ? "Writing negTokenInit..." : "Writing negTokenResp...");
365
366
  /* mechTypes [0] MechTypeList (mechTypes already contains the SEQUENCE tag) */
367
0
  if (token->init)
368
0
  {
369
0
    if (!WinPrAsn1EncContextualRawContent(enc, 0, &mechTypes))
370
0
      goto cleanup;
371
0
    WLog_DBG(TAG, "\tmechTypes [0] (%li bytes)", token->mechTypes.cbBuffer);
372
0
  }
373
  /* negState [0] ENUMERATED */
374
0
  else if (token->negState != NOSTATE)
375
0
  {
376
0
    if (!WinPrAsn1EncContextualEnumerated(enc, 0, token->negState))
377
0
      goto cleanup;
378
0
    WLog_DBG(TAG, "\tnegState [0] (%d)", token->negState);
379
0
  }
380
381
  /* supportedMech [1] OID */
382
0
  if (token->supportedMech.len)
383
0
  {
384
0
    if (!WinPrAsn1EncContextualOID(enc, 1, &token->supportedMech))
385
0
      goto cleanup;
386
0
    WLog_DBG(TAG, "\tsupportedMech [1] (%s)", negotiate_mech_name(&token->supportedMech));
387
0
  }
388
389
  /* mechToken [2] OCTET STRING */
390
0
  if (token->mechToken.cbBuffer)
391
0
  {
392
0
    if (WinPrAsn1EncContextualOctetString(enc, 2, &mechToken) == 0)
393
0
      goto cleanup;
394
0
    WLog_DBG(TAG, "\tmechToken [2] (%li bytes)", token->mechToken.cbBuffer);
395
0
  }
396
397
  /* mechListMIC [3] OCTET STRING */
398
0
  if (token->mic.cbBuffer)
399
0
  {
400
0
    if (WinPrAsn1EncContextualOctetString(enc, 3, &mechListMic) == 0)
401
0
      goto cleanup;
402
0
    WLog_DBG(TAG, "\tmechListMIC [3] (%li bytes)", token->mic.cbBuffer);
403
0
  }
404
405
  /* NegTokenInit or NegTokenResp */
406
0
  if (!WinPrAsn1EncEndContainer(enc))
407
0
    goto cleanup;
408
409
0
  if (token->init)
410
0
  {
411
    /* initialContextToken */
412
0
    if (!WinPrAsn1EncEndContainer(enc))
413
0
      goto cleanup;
414
0
  }
415
416
0
  if (!WinPrAsn1EncStreamSize(enc, &len) || len > output_buffer->cbBuffer)
417
0
    goto cleanup;
418
419
0
  Stream_StaticInit(&s, output_buffer->pvBuffer, len);
420
421
0
  if (WinPrAsn1EncToStream(enc, &s))
422
0
  {
423
0
    output_buffer->cbBuffer = len;
424
0
    ret = TRUE;
425
0
  }
426
427
0
cleanup:
428
0
  WinPrAsn1Encoder_Free(&enc);
429
0
  return ret;
430
0
}
431
432
static BOOL negotiate_read_neg_token(PSecBuffer input, NegToken* token)
433
0
{
434
0
  WinPrAsn1Decoder dec;
435
0
  WinPrAsn1Decoder dec2;
436
0
  WinPrAsn1_OID oid;
437
0
  WinPrAsn1_tagId contextual;
438
0
  WinPrAsn1_tag tag;
439
0
  size_t len;
440
0
  WinPrAsn1_OctetString octet_string;
441
0
  BOOL err;
442
443
0
  WINPR_ASSERT(input);
444
0
  WINPR_ASSERT(token);
445
446
0
  WinPrAsn1Decoder_InitMem(&dec, WINPR_ASN1_DER, input->pvBuffer, input->cbBuffer);
447
448
0
  if (!WinPrAsn1DecPeekTag(&dec, &tag))
449
0
    return FALSE;
450
451
0
  if (tag == 0x60)
452
0
  {
453
    /* initialContextToken [APPLICATION 0] */
454
0
    if (!WinPrAsn1DecReadApp(&dec, &tag, &dec2) || tag != 0)
455
0
      return FALSE;
456
0
    dec = dec2;
457
458
    /* thisMech OID */
459
0
    if (!WinPrAsn1DecReadOID(&dec, &oid, FALSE))
460
0
      return FALSE;
461
462
0
    if (!sspi_gss_oid_compare(&spnego_OID, &oid))
463
0
      return FALSE;
464
465
    /* [0] NegTokenInit */
466
0
    if (!WinPrAsn1DecReadContextualSequence(&dec, 0, &err, &dec2))
467
0
      return FALSE;
468
469
0
    token->init = TRUE;
470
0
  }
471
  /* [1] NegTokenResp */
472
0
  else if (!WinPrAsn1DecReadContextualSequence(&dec, 1, &err, &dec2))
473
0
    return FALSE;
474
0
  dec = dec2;
475
476
0
  WLog_DBG(TAG, token->init ? "Reading negTokenInit..." : "Reading negTokenResp...");
477
478
  /* Read NegTokenResp sequence members */
479
0
  do
480
0
  {
481
0
    if (!WinPrAsn1DecReadContextualTag(&dec, &contextual, &dec2))
482
0
      return FALSE;
483
484
0
    switch (contextual)
485
0
    {
486
0
      case 0:
487
0
        if (token->init)
488
0
        {
489
          /* mechTypes [0] MechTypeList */
490
0
          wStream s = WinPrAsn1DecGetStream(&dec2);
491
0
          token->mechTypes.BufferType = SECBUFFER_TOKEN;
492
0
          token->mechTypes.cbBuffer = Stream_Length(&s);
493
0
          token->mechTypes.pvBuffer = Stream_Buffer(&s);
494
0
          WLog_DBG(TAG, "\tmechTypes [0] (%li bytes)", token->mechTypes.cbBuffer);
495
0
        }
496
0
        else
497
0
        {
498
          /* negState [0] ENUMERATED */
499
0
          WinPrAsn1_ENUMERATED rd;
500
0
          if (!WinPrAsn1DecReadEnumerated(&dec2, &rd))
501
0
            return FALSE;
502
0
          token->negState = rd;
503
0
          WLog_DBG(TAG, "\tnegState [0] (%d)", token->negState);
504
0
        }
505
0
        break;
506
0
      case 1:
507
0
        if (token->init)
508
0
        {
509
          /* reqFlags [1] ContextFlags BIT STRING (ignored) */
510
0
          if (!WinPrAsn1DecPeekTagAndLen(&dec2, &tag, &len) || (tag != ER_TAG_BIT_STRING))
511
0
            return FALSE;
512
0
          WLog_DBG(TAG, "\treqFlags [1] (%li bytes)", len);
513
0
        }
514
0
        else
515
0
        {
516
          /* supportedMech [1] MechType */
517
0
          if (!WinPrAsn1DecReadOID(&dec2, &token->supportedMech, FALSE))
518
0
            return FALSE;
519
0
          WLog_DBG(TAG, "\tsupportedMech [1] (%s)",
520
0
                   negotiate_mech_name(&token->supportedMech));
521
0
        }
522
0
        break;
523
0
      case 2:
524
        /* mechToken [2] OCTET STRING */
525
0
        if (!WinPrAsn1DecReadOctetString(&dec2, &octet_string, FALSE))
526
0
          return FALSE;
527
0
        token->mechToken.cbBuffer = octet_string.len;
528
0
        token->mechToken.pvBuffer = octet_string.data;
529
0
        token->mechToken.BufferType = SECBUFFER_TOKEN;
530
0
        WLog_DBG(TAG, "\tmechToken [2] (%li bytes)", octet_string.len);
531
0
        break;
532
0
      case 3:
533
        /* mechListMic [3] OCTET STRING */
534
0
        if (!WinPrAsn1DecReadOctetString(&dec2, &octet_string, FALSE))
535
0
          return FALSE;
536
0
        token->mic.cbBuffer = octet_string.len;
537
0
        token->mic.pvBuffer = octet_string.data;
538
0
        token->mic.BufferType = SECBUFFER_TOKEN;
539
0
        WLog_DBG(TAG, "\tmechListMIC [3] (%li bytes)", octet_string.len);
540
0
        break;
541
0
      default:
542
0
        WLog_ERR(TAG, "unknown contextual item %d", contextual);
543
0
        return FALSE;
544
0
    }
545
0
  } while (WinPrAsn1DecPeekTag(&dec, &tag));
546
547
0
  return TRUE;
548
0
}
549
550
static SECURITY_STATUS negotiate_mic_exchange(NEGOTIATE_CONTEXT* context, NegToken* input_token,
551
                                              NegToken* output_token, PSecBuffer output_buffer)
552
0
{
553
0
  SecBuffer mic_buffers[2] = { 0 };
554
0
  SecBufferDesc mic_buffer_desc = { SECBUFFER_VERSION, 2, mic_buffers };
555
0
  SECURITY_STATUS status;
556
557
0
  WINPR_ASSERT(context);
558
0
  WINPR_ASSERT(input_token);
559
0
  WINPR_ASSERT(output_token);
560
0
  WINPR_ASSERT(context->mech);
561
0
  WINPR_ASSERT(context->mech->pkg);
562
563
0
  const SecurityFunctionTableA* table = context->mech->pkg->table;
564
0
  WINPR_ASSERT(table);
565
566
0
  mic_buffers[0] = context->mechTypes;
567
568
  /* Verify MIC if we received one */
569
0
  if (input_token->mic.cbBuffer > 0)
570
0
  {
571
0
    mic_buffers[1] = input_token->mic;
572
573
0
    status = table->VerifySignature(&context->sub_context, &mic_buffer_desc, 0, 0);
574
0
    if (status != SEC_E_OK)
575
0
      return status;
576
577
0
    output_token->negState = ACCEPT_COMPLETED;
578
0
  }
579
580
  /* If peer expects a MIC then generate it */
581
0
  if (input_token->negState != ACCEPT_COMPLETED)
582
0
  {
583
    /* Store the mic token after the mech token in the output buffer */
584
0
    output_token->mic.BufferType = SECBUFFER_TOKEN;
585
0
    output_token->mic.cbBuffer = output_buffer->cbBuffer - output_token->mechToken.cbBuffer;
586
0
    output_token->mic.pvBuffer =
587
0
        (BYTE*)output_buffer->pvBuffer + output_token->mechToken.cbBuffer;
588
589
0
    mic_buffers[1] = output_token->mic;
590
591
0
    status = table->MakeSignature(&context->sub_context, 0, &mic_buffer_desc, 0);
592
0
    if (status != SEC_E_OK)
593
0
      return status;
594
595
0
    output_token->mic = mic_buffers[1];
596
0
  }
597
598
  /* When using NTLM cipher states need to be reset after mic exchange */
599
0
  if (_tcscmp(sspi_SecureHandleGetUpperPointer(&context->sub_context), NTLM_SSP_NAME) == 0)
600
0
  {
601
0
    if (!ntlm_reset_cipher_state(&context->sub_context))
602
0
      return SEC_E_INTERNAL_ERROR;
603
0
  }
604
605
0
  return SEC_E_OK;
606
0
}
607
608
static SECURITY_STATUS SEC_ENTRY negotiate_InitializeSecurityContextW(
609
    PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR* pszTargetName, ULONG fContextReq,
610
    ULONG Reserved1, ULONG TargetDataRep, PSecBufferDesc pInput, ULONG Reserved2,
611
    PCtxtHandle phNewContext, PSecBufferDesc pOutput, PULONG pfContextAttr, PTimeStamp ptsExpiry)
612
0
{
613
0
  NEGOTIATE_CONTEXT* context = NULL;
614
0
  NEGOTIATE_CONTEXT init_context = { 0 };
615
0
  MechCred* creds;
616
0
  PCtxtHandle sub_context = NULL;
617
0
  PCredHandle sub_cred = NULL;
618
0
  NegToken input_token = empty_neg_token;
619
0
  NegToken output_token = empty_neg_token;
620
0
  PSecBuffer input_buffer = NULL;
621
0
  PSecBuffer output_buffer = NULL;
622
0
  PSecBuffer bindings_buffer = NULL;
623
0
  SecBuffer mech_input_buffers[2] = { 0 };
624
0
  SecBufferDesc mech_input = { SECBUFFER_VERSION, 2, mech_input_buffers };
625
0
  SecBufferDesc mech_output = { SECBUFFER_VERSION, 1, &output_token.mechToken };
626
0
  SECURITY_STATUS status = SEC_E_INTERNAL_ERROR;
627
0
  SECURITY_STATUS sub_status = SEC_E_INTERNAL_ERROR;
628
0
  WinPrAsn1Encoder* enc = NULL;
629
0
  wStream s;
630
0
  const Mech* mech;
631
632
0
  if (!phCredential || !SecIsValidHandle(phCredential))
633
0
    return SEC_E_NO_CREDENTIALS;
634
635
0
  creds = sspi_SecureHandleGetLowerPointer(phCredential);
636
637
  /* behave like windows SSPIs that don't want empty context */
638
0
  if (phContext && !phContext->dwLower && !phContext->dwUpper)
639
0
    return SEC_E_INVALID_HANDLE;
640
641
0
  context = sspi_SecureHandleGetLowerPointer(phContext);
642
643
0
  if (pInput)
644
0
  {
645
0
    input_buffer = sspi_FindSecBuffer(pInput, SECBUFFER_TOKEN);
646
0
    bindings_buffer = sspi_FindSecBuffer(pInput, SECBUFFER_CHANNEL_BINDINGS);
647
0
  }
648
0
  if (pOutput)
649
0
    output_buffer = sspi_FindSecBuffer(pOutput, SECBUFFER_TOKEN);
650
651
0
  if (!context)
652
0
  {
653
0
    enc = WinPrAsn1Encoder_New(WINPR_ASN1_DER);
654
0
    if (!enc)
655
0
      return SEC_E_INSUFFICIENT_MEMORY;
656
657
0
    if (!WinPrAsn1EncSeqContainer(enc))
658
0
      goto cleanup;
659
660
0
    for (size_t i = 0; i < MECH_COUNT; i++)
661
0
    {
662
0
      MechCred* cred = &creds[i];
663
0
      const SecPkg* pkg = MechTable[i].pkg;
664
0
      WINPR_ASSERT(pkg);
665
0
      WINPR_ASSERT(pkg->table_w);
666
667
0
      if (!cred->valid)
668
0
        continue;
669
670
      /* Send an optimistic token for the first valid mechanism */
671
0
      if (!init_context.mech)
672
0
      {
673
        /* Use the output buffer to store the optimistic token */
674
0
        CopyMemory(&output_token.mechToken, output_buffer, sizeof(SecBuffer));
675
676
0
        if (bindings_buffer)
677
0
          mech_input_buffers[0] = *bindings_buffer;
678
679
0
        WINPR_ASSERT(pkg->table_w->InitializeSecurityContextW);
680
0
        sub_status = pkg->table_w->InitializeSecurityContextW(
681
0
            &cred->cred, NULL, pszTargetName, fContextReq | cred->mech->flags, Reserved1,
682
0
            TargetDataRep, &mech_input, Reserved2, &init_context.sub_context, &mech_output,
683
0
            pfContextAttr, ptsExpiry);
684
685
        /* If the mechanism failed we can't use it; skip */
686
0
        if (IsSecurityStatusError(sub_status))
687
0
        {
688
0
          if (SecIsValidHandle(&init_context.sub_context))
689
0
          {
690
0
            WINPR_ASSERT(pkg->table_w->DeleteSecurityContext);
691
0
            pkg->table_w->DeleteSecurityContext(&init_context.sub_context);
692
0
          }
693
0
          cred->valid = FALSE;
694
0
          continue;
695
0
        }
696
697
0
        init_context.mech = cred->mech;
698
0
      }
699
700
0
      if (!WinPrAsn1EncOID(enc, cred->mech->oid))
701
0
        goto cleanup;
702
0
      WLog_DBG(TAG, "Available mechanism: %s", negotiate_mech_name(cred->mech->oid));
703
0
    }
704
705
    /* No usable mechanisms were found */
706
0
    if (!init_context.mech)
707
0
      goto cleanup;
708
709
    /* If the only available mech is NTLM use it directly otherwise use spnego */
710
0
    if (init_context.mech->oid == &ntlm_OID)
711
0
    {
712
0
      init_context.spnego = FALSE;
713
0
      output_buffer->cbBuffer = output_token.mechToken.cbBuffer;
714
0
      WLog_DBG(TAG, "Using direct NTLM");
715
0
    }
716
0
    else
717
0
    {
718
0
      init_context.spnego = TRUE;
719
0
      init_context.mechTypes.BufferType = SECBUFFER_DATA;
720
0
      init_context.mechTypes.cbBuffer = WinPrAsn1EncEndContainer(enc);
721
0
    }
722
723
    /* Allocate memory for the new context */
724
0
    context = negotiate_ContextNew(&init_context);
725
0
    if (!context)
726
0
    {
727
0
      init_context.mech->pkg->table->DeleteSecurityContext(&init_context.sub_context);
728
0
      WinPrAsn1Encoder_Free(&enc);
729
0
      return SEC_E_INSUFFICIENT_MEMORY;
730
0
    }
731
732
0
    sspi_SecureHandleSetUpperPointer(phNewContext, NEGO_SSP_NAME);
733
0
    sspi_SecureHandleSetLowerPointer(phNewContext, context);
734
735
0
    if (!context->spnego)
736
0
    {
737
0
      status = sub_status;
738
0
      goto cleanup;
739
0
    }
740
741
    /* Write mechTypesList */
742
0
    Stream_StaticInit(&s, context->mechTypes.pvBuffer, context->mechTypes.cbBuffer);
743
0
    if (!WinPrAsn1EncToStream(enc, &s))
744
0
      goto cleanup;
745
746
0
    output_token.mechTypes.cbBuffer = context->mechTypes.cbBuffer;
747
0
    output_token.mechTypes.pvBuffer = context->mechTypes.pvBuffer;
748
0
    output_token.init = TRUE;
749
750
0
    if (sub_status == SEC_E_OK)
751
0
      context->state = NEGOTIATE_STATE_FINAL_OPTIMISTIC;
752
0
  }
753
0
  else
754
0
  {
755
0
    if (!input_buffer)
756
0
      return SEC_E_INVALID_TOKEN;
757
758
0
    sub_context = &context->sub_context;
759
0
    sub_cred = negotiate_FindCredential(creds, context->mech);
760
761
0
    if (!context->spnego)
762
0
    {
763
0
      return context->mech->pkg->table_w->InitializeSecurityContextW(
764
0
          sub_cred, sub_context, pszTargetName, fContextReq | context->mech->flags, Reserved1,
765
0
          TargetDataRep, pInput, Reserved2, sub_context, pOutput, pfContextAttr, ptsExpiry);
766
0
    }
767
768
0
    if (!negotiate_read_neg_token(input_buffer, &input_token))
769
0
      return SEC_E_INVALID_TOKEN;
770
771
    /* On first response check if the server doesn't like out prefered mech */
772
0
    if (context->state < NEGOTIATE_STATE_NEGORESP && input_token.supportedMech.len &&
773
0
        !sspi_gss_oid_compare(&input_token.supportedMech, context->mech->oid))
774
0
    {
775
0
      mech = negotiate_GetMechByOID(&input_token.supportedMech);
776
0
      if (!mech)
777
0
        return SEC_E_INVALID_TOKEN;
778
779
      /* Make sure the specified mech is supported and get the appropriate credential */
780
0
      sub_cred = negotiate_FindCredential(creds, mech);
781
0
      if (!sub_cred)
782
0
        return SEC_E_INVALID_TOKEN;
783
784
      /* Clean up the optimistic mech */
785
0
      context->mech->pkg->table_w->DeleteSecurityContext(&context->sub_context);
786
0
      sub_context = NULL;
787
788
0
      context->mech = mech;
789
0
      context->mic = TRUE;
790
0
    }
791
792
    /* Check neg_state (required on first response) */
793
0
    if (context->state < NEGOTIATE_STATE_NEGORESP)
794
0
    {
795
0
      switch (input_token.negState)
796
0
      {
797
0
        case NOSTATE:
798
0
          return SEC_E_INVALID_TOKEN;
799
0
        case REJECT:
800
0
          return SEC_E_LOGON_DENIED;
801
0
        case REQUEST_MIC:
802
0
          context->mic = TRUE;
803
          /* fallthrough */
804
0
          WINPR_FALLTHROUGH
805
0
        case ACCEPT_INCOMPLETE:
806
0
          context->state = NEGOTIATE_STATE_NEGORESP;
807
0
          break;
808
0
        case ACCEPT_COMPLETED:
809
0
          if (context->state == NEGOTIATE_STATE_INITIAL)
810
0
            context->state = NEGOTIATE_STATE_NEGORESP;
811
0
          else
812
0
            context->state = NEGOTIATE_STATE_FINAL;
813
0
          break;
814
0
      }
815
816
0
      WLog_DBG(TAG, "Negotiated mechanism: %s", negotiate_mech_name(context->mech->oid));
817
0
    }
818
819
0
    if (context->state == NEGOTIATE_STATE_NEGORESP)
820
0
    {
821
      /* Store the mech token in the output buffer */
822
0
      CopyMemory(&output_token.mechToken, output_buffer, sizeof(SecBuffer));
823
824
0
      mech_input_buffers[0] = input_token.mechToken;
825
0
      if (bindings_buffer)
826
0
        mech_input_buffers[1] = *bindings_buffer;
827
828
0
      status = context->mech->pkg->table_w->InitializeSecurityContextW(
829
0
          sub_cred, sub_context, pszTargetName, fContextReq | context->mech->flags, Reserved1,
830
0
          TargetDataRep, input_token.mechToken.cbBuffer ? &mech_input : NULL, Reserved2,
831
0
          &context->sub_context, &mech_output, pfContextAttr, ptsExpiry);
832
833
0
      if (IsSecurityStatusError(status))
834
0
        return status;
835
0
    }
836
837
0
    if (status == SEC_E_OK)
838
0
    {
839
0
      if (output_token.mechToken.cbBuffer > 0)
840
0
        context->state = NEGOTIATE_STATE_MIC;
841
0
      else
842
0
        context->state = NEGOTIATE_STATE_FINAL;
843
0
    }
844
845
    /* Check if the acceptor sent its final token without a mic */
846
0
    if (context->state == NEGOTIATE_STATE_FINAL && input_token.mic.cbBuffer == 0)
847
0
    {
848
0
      if (context->mic || input_token.negState != ACCEPT_COMPLETED)
849
0
        return SEC_E_INVALID_TOKEN;
850
851
0
      output_buffer->cbBuffer = 0;
852
0
      return SEC_E_OK;
853
0
    }
854
855
0
    if ((context->state == NEGOTIATE_STATE_MIC && context->mic) ||
856
0
        context->state == NEGOTIATE_STATE_FINAL)
857
0
    {
858
0
      status = negotiate_mic_exchange(context, &input_token, &output_token, output_buffer);
859
0
      if (status != SEC_E_OK)
860
0
        return status;
861
0
    }
862
0
  }
863
864
0
  if (input_token.negState == ACCEPT_COMPLETED)
865
0
  {
866
0
    output_buffer->cbBuffer = 0;
867
0
    return SEC_E_OK;
868
0
  }
869
870
0
  if (output_token.negState == ACCEPT_COMPLETED)
871
0
    status = SEC_E_OK;
872
0
  else
873
0
    status = SEC_I_CONTINUE_NEEDED;
874
875
0
  if (!negotiate_write_neg_token(output_buffer, &output_token))
876
0
    status = SEC_E_INTERNAL_ERROR;
877
878
0
cleanup:
879
0
  WinPrAsn1Encoder_Free(&enc);
880
0
  return status;
881
0
}
882
883
static SECURITY_STATUS SEC_ENTRY negotiate_InitializeSecurityContextA(
884
    PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR* pszTargetName, ULONG fContextReq,
885
    ULONG Reserved1, ULONG TargetDataRep, PSecBufferDesc pInput, ULONG Reserved2,
886
    PCtxtHandle phNewContext, PSecBufferDesc pOutput, PULONG pfContextAttr, PTimeStamp ptsExpiry)
887
0
{
888
0
  SECURITY_STATUS status;
889
0
  SEC_WCHAR* pszTargetNameW = NULL;
890
891
0
  if (pszTargetName)
892
0
  {
893
0
    pszTargetNameW = ConvertUtf8ToWCharAlloc(pszTargetName, NULL);
894
0
    if (!pszTargetNameW)
895
0
      return SEC_E_INTERNAL_ERROR;
896
0
  }
897
898
0
  status = negotiate_InitializeSecurityContextW(
899
0
      phCredential, phContext, pszTargetNameW, fContextReq, Reserved1, TargetDataRep, pInput,
900
0
      Reserved2, phNewContext, pOutput, pfContextAttr, ptsExpiry);
901
0
  free(pszTargetNameW);
902
0
  return status;
903
0
}
904
905
static const Mech* guessMech(PSecBuffer input_buffer, BOOL* spNego, WinPrAsn1_OID* oid)
906
0
{
907
0
  WinPrAsn1Decoder decoder;
908
0
  WinPrAsn1Decoder appDecoder;
909
0
  WinPrAsn1_tagId tag;
910
911
0
  *spNego = FALSE;
912
913
  /* Check for NTLM token */
914
0
  if (input_buffer->cbBuffer >= 8 && strncmp(input_buffer->pvBuffer, "NTLMSSP", 8) == 0)
915
0
  {
916
0
    *oid = ntlm_OID;
917
0
    return negotiate_GetMechByOID(&ntlm_OID);
918
0
  }
919
920
  /* Read initialContextToken or raw Kerberos token */
921
0
  WinPrAsn1Decoder_InitMem(&decoder, WINPR_ASN1_DER, input_buffer->pvBuffer,
922
0
                           input_buffer->cbBuffer);
923
924
0
  if (!WinPrAsn1DecReadApp(&decoder, &tag, &appDecoder) || tag != 0)
925
0
    return NULL;
926
927
0
  if (!WinPrAsn1DecReadOID(&appDecoder, oid, FALSE))
928
0
    return NULL;
929
930
0
  if (sspi_gss_oid_compare(oid, &spnego_OID))
931
0
  {
932
0
    *spNego = TRUE;
933
0
    return NULL;
934
0
  }
935
936
0
  return negotiate_GetMechByOID(oid);
937
0
}
938
939
static SECURITY_STATUS SEC_ENTRY negotiate_AcceptSecurityContext(
940
    PCredHandle phCredential, PCtxtHandle phContext, PSecBufferDesc pInput, ULONG fContextReq,
941
    ULONG TargetDataRep, PCtxtHandle phNewContext, PSecBufferDesc pOutput, PULONG pfContextAttr,
942
    PTimeStamp ptsTimeStamp)
943
0
{
944
0
  NEGOTIATE_CONTEXT* context = NULL;
945
0
  NEGOTIATE_CONTEXT init_context = { 0 };
946
0
  MechCred* creds;
947
0
  PCredHandle sub_cred = NULL;
948
0
  NegToken input_token = empty_neg_token;
949
0
  NegToken output_token = empty_neg_token;
950
0
  PSecBuffer input_buffer = NULL;
951
0
  PSecBuffer output_buffer = NULL;
952
0
  SecBufferDesc mech_input = { SECBUFFER_VERSION, 1, &input_token.mechToken };
953
0
  SecBufferDesc mech_output = { SECBUFFER_VERSION, 1, &output_token.mechToken };
954
0
  SECURITY_STATUS status = SEC_E_INTERNAL_ERROR;
955
0
  WinPrAsn1Decoder dec, dec2;
956
0
  WinPrAsn1_tagId tag;
957
0
  WinPrAsn1_OID oid = { 0 };
958
0
  const Mech* first_mech = NULL;
959
960
0
  if (!phCredential || !SecIsValidHandle(phCredential))
961
0
    return SEC_E_NO_CREDENTIALS;
962
963
0
  creds = sspi_SecureHandleGetLowerPointer(phCredential);
964
965
0
  if (!pInput)
966
0
    return SEC_E_INVALID_TOKEN;
967
968
  /* behave like windows SSPIs that don't want empty context */
969
0
  if (phContext && !phContext->dwLower && !phContext->dwUpper)
970
0
    return SEC_E_INVALID_HANDLE;
971
972
0
  context = sspi_SecureHandleGetLowerPointer(phContext);
973
974
0
  input_buffer = sspi_FindSecBuffer(pInput, SECBUFFER_TOKEN);
975
0
  if (pOutput)
976
0
    output_buffer = sspi_FindSecBuffer(pOutput, SECBUFFER_TOKEN);
977
978
0
  if (!context)
979
0
  {
980
0
    init_context.mech = guessMech(input_buffer, &init_context.spnego, &oid);
981
0
    if (!init_context.mech && !init_context.spnego)
982
0
      return SEC_E_INVALID_TOKEN;
983
984
0
    WLog_DBG(TAG, "Mechanism: %s", negotiate_mech_name(&oid));
985
986
0
    if (init_context.spnego)
987
0
    {
988
      /* Process spnego token */
989
0
      if (!negotiate_read_neg_token(input_buffer, &input_token))
990
0
        return SEC_E_INVALID_TOKEN;
991
992
      /* First token must be negoTokenInit and must contain a mechList */
993
0
      if (!input_token.init || input_token.mechTypes.cbBuffer == 0)
994
0
        return SEC_E_INVALID_TOKEN;
995
996
0
      init_context.mechTypes.BufferType = SECBUFFER_DATA;
997
0
      init_context.mechTypes.cbBuffer = input_token.mechTypes.cbBuffer;
998
999
      /* Prepare to read mechList */
1000
0
      WinPrAsn1Decoder_InitMem(&dec, WINPR_ASN1_DER, input_token.mechTypes.pvBuffer,
1001
0
                               input_token.mechTypes.cbBuffer);
1002
1003
0
      if (!WinPrAsn1DecReadSequence(&dec, &dec2))
1004
0
        return SEC_E_INVALID_TOKEN;
1005
0
      dec = dec2;
1006
1007
      /* If an optimistic token was provided pass it into the first mech */
1008
0
      if (input_token.mechToken.cbBuffer)
1009
0
      {
1010
0
        if (!WinPrAsn1DecReadOID(&dec, &oid, FALSE))
1011
0
          return SEC_E_INVALID_TOKEN;
1012
1013
0
        init_context.mech = negotiate_GetMechByOID(&oid);
1014
1015
0
        if (init_context.mech)
1016
0
        {
1017
0
          output_token.mechToken = *output_buffer;
1018
0
          WLog_DBG(TAG, "Requested mechanism: %s",
1019
0
                   negotiate_mech_name(init_context.mech->oid));
1020
0
        }
1021
0
      }
1022
0
    }
1023
1024
0
    if (init_context.mech)
1025
0
    {
1026
0
      sub_cred = negotiate_FindCredential(creds, init_context.mech);
1027
1028
0
      status = init_context.mech->pkg->table->AcceptSecurityContext(
1029
0
          sub_cred, NULL, init_context.spnego ? &mech_input : pInput, fContextReq,
1030
0
          TargetDataRep, &init_context.sub_context,
1031
0
          init_context.spnego ? &mech_output : pOutput, pfContextAttr, ptsTimeStamp);
1032
0
    }
1033
1034
0
    if (IsSecurityStatusError(status))
1035
0
    {
1036
0
      if (!init_context.spnego)
1037
0
        return status;
1038
1039
0
      init_context.mic = TRUE;
1040
0
      first_mech = init_context.mech;
1041
0
      init_context.mech = NULL;
1042
0
      output_token.mechToken.cbBuffer = 0;
1043
0
    }
1044
1045
0
    while (!init_context.mech && WinPrAsn1DecPeekTag(&dec, &tag))
1046
0
    {
1047
      /* Read each mechanism */
1048
0
      if (!WinPrAsn1DecReadOID(&dec, &oid, FALSE))
1049
0
        return SEC_E_INVALID_TOKEN;
1050
1051
0
      init_context.mech = negotiate_GetMechByOID(&oid);
1052
0
      WLog_DBG(TAG, "Requested mechanism: %s", negotiate_mech_name(init_context.mech->oid));
1053
1054
      /* Microsoft may send two versions of the kerberos OID */
1055
0
      if (init_context.mech == first_mech)
1056
0
        init_context.mech = NULL;
1057
1058
0
      if (init_context.mech && !negotiate_FindCredential(creds, init_context.mech))
1059
0
        init_context.mech = NULL;
1060
0
    }
1061
1062
0
    if (!init_context.mech)
1063
0
      return SEC_E_INTERNAL_ERROR;
1064
1065
0
    context = negotiate_ContextNew(&init_context);
1066
0
    if (!context)
1067
0
    {
1068
0
      if (!IsSecurityStatusError(status))
1069
0
        init_context.mech->pkg->table->DeleteSecurityContext(&init_context.sub_context);
1070
0
      return SEC_E_INSUFFICIENT_MEMORY;
1071
0
    }
1072
1073
0
    sspi_SecureHandleSetUpperPointer(phNewContext, NEGO_SSP_NAME);
1074
0
    sspi_SecureHandleSetLowerPointer(phNewContext, context);
1075
1076
0
    if (!init_context.spnego)
1077
0
      return status;
1078
1079
0
    CopyMemory(init_context.mechTypes.pvBuffer, input_token.mechTypes.pvBuffer,
1080
0
               input_token.mechTypes.cbBuffer);
1081
1082
0
    if (!context->mech->preferred)
1083
0
    {
1084
0
      output_token.negState = REQUEST_MIC;
1085
0
      context->mic = TRUE;
1086
0
    }
1087
0
    else
1088
0
    {
1089
0
      output_token.negState = ACCEPT_INCOMPLETE;
1090
0
    }
1091
1092
0
    if (status == SEC_E_OK)
1093
0
      context->state = NEGOTIATE_STATE_FINAL;
1094
0
    else
1095
0
      context->state = NEGOTIATE_STATE_NEGORESP;
1096
1097
0
    output_token.supportedMech = oid;
1098
0
    WLog_DBG(TAG, "Accepted mechanism: %s", negotiate_mech_name(&output_token.supportedMech));
1099
0
  }
1100
0
  else
1101
0
  {
1102
0
    sub_cred = negotiate_FindCredential(creds, context->mech);
1103
0
    if (!sub_cred)
1104
0
      return SEC_E_NO_CREDENTIALS;
1105
1106
0
    if (!context->spnego)
1107
0
    {
1108
0
      return context->mech->pkg->table->AcceptSecurityContext(
1109
0
          sub_cred, &context->sub_context, pInput, fContextReq, TargetDataRep,
1110
0
          &context->sub_context, pOutput, pfContextAttr, ptsTimeStamp);
1111
0
    }
1112
1113
0
    if (!negotiate_read_neg_token(input_buffer, &input_token))
1114
0
      return SEC_E_INVALID_TOKEN;
1115
1116
    /* Process the mechanism token */
1117
0
    if (input_token.mechToken.cbBuffer > 0)
1118
0
    {
1119
0
      if (context->state != NEGOTIATE_STATE_NEGORESP)
1120
0
        return SEC_E_INVALID_TOKEN;
1121
1122
      /* Use the output buffer to store the optimistic token */
1123
0
      CopyMemory(&output_token.mechToken, output_buffer, sizeof(SecBuffer));
1124
1125
0
      status = context->mech->pkg->table->AcceptSecurityContext(
1126
0
          sub_cred, &context->sub_context, &mech_input, fContextReq | context->mech->flags,
1127
0
          TargetDataRep, &context->sub_context, &mech_output, pfContextAttr, ptsTimeStamp);
1128
1129
0
      if (IsSecurityStatusError(status))
1130
0
        return status;
1131
1132
0
      if (status == SEC_E_OK)
1133
0
        context->state = NEGOTIATE_STATE_FINAL;
1134
0
    }
1135
0
    else if (context->state == NEGOTIATE_STATE_NEGORESP)
1136
0
      return SEC_E_INVALID_TOKEN;
1137
0
  }
1138
1139
0
  if (context->state == NEGOTIATE_STATE_FINAL)
1140
0
  {
1141
    /* Check if initiator sent the last mech token without a mic and a mic was required */
1142
0
    if (context->mic && output_token.mechToken.cbBuffer == 0 && input_token.mic.cbBuffer == 0)
1143
0
      return SEC_E_INVALID_TOKEN;
1144
1145
0
    if (context->mic || input_token.mic.cbBuffer > 0)
1146
0
    {
1147
0
      status = negotiate_mic_exchange(context, &input_token, &output_token, output_buffer);
1148
0
      if (status != SEC_E_OK)
1149
0
        return status;
1150
0
    }
1151
0
    else
1152
0
      output_token.negState = ACCEPT_COMPLETED;
1153
0
  }
1154
1155
0
  if (input_token.negState == ACCEPT_COMPLETED)
1156
0
  {
1157
0
    output_buffer->cbBuffer = 0;
1158
0
    return SEC_E_OK;
1159
0
  }
1160
1161
0
  if (output_token.negState == ACCEPT_COMPLETED)
1162
0
    status = SEC_E_OK;
1163
0
  else
1164
0
    status = SEC_I_CONTINUE_NEEDED;
1165
1166
0
  if (!negotiate_write_neg_token(output_buffer, &output_token))
1167
0
    return SEC_E_INTERNAL_ERROR;
1168
1169
0
  return status;
1170
0
}
1171
1172
static SECURITY_STATUS SEC_ENTRY negotiate_CompleteAuthToken(PCtxtHandle phContext,
1173
                                                             PSecBufferDesc pToken)
1174
0
{
1175
0
  NEGOTIATE_CONTEXT* context;
1176
0
  SECURITY_STATUS status = SEC_E_OK;
1177
0
  context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
1178
1179
0
  if (!context)
1180
0
    return SEC_E_INVALID_HANDLE;
1181
1182
0
  WINPR_ASSERT(context->mech);
1183
0
  WINPR_ASSERT(context->mech->pkg);
1184
0
  WINPR_ASSERT(context->mech->pkg->table);
1185
0
  if (context->mech->pkg->table->CompleteAuthToken)
1186
0
    status = context->mech->pkg->table->CompleteAuthToken(&context->sub_context, pToken);
1187
1188
0
  return status;
1189
0
}
1190
1191
static SECURITY_STATUS SEC_ENTRY negotiate_DeleteSecurityContext(PCtxtHandle phContext)
1192
0
{
1193
0
  NEGOTIATE_CONTEXT* context;
1194
0
  SECURITY_STATUS status = SEC_E_OK;
1195
0
  context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
1196
0
  const SecPkg* pkg;
1197
1198
0
  if (!context)
1199
0
    return SEC_E_INVALID_HANDLE;
1200
1201
0
  WINPR_ASSERT(context->mech);
1202
0
  WINPR_ASSERT(context->mech->pkg);
1203
0
  WINPR_ASSERT(context->mech->pkg->table);
1204
0
  pkg = context->mech->pkg;
1205
1206
0
  if (pkg->table->DeleteSecurityContext)
1207
0
    status = pkg->table->DeleteSecurityContext(&context->sub_context);
1208
1209
0
  negotiate_ContextFree(context);
1210
0
  return status;
1211
0
}
1212
1213
static SECURITY_STATUS SEC_ENTRY negotiate_ImpersonateSecurityContext(PCtxtHandle phContext)
1214
0
{
1215
0
  return SEC_E_OK;
1216
0
}
1217
1218
static SECURITY_STATUS SEC_ENTRY negotiate_RevertSecurityContext(PCtxtHandle phContext)
1219
0
{
1220
0
  return SEC_E_OK;
1221
0
}
1222
1223
static SECURITY_STATUS SEC_ENTRY negotiate_QueryContextAttributesW(PCtxtHandle phContext,
1224
                                                                   ULONG ulAttribute, void* pBuffer)
1225
0
{
1226
0
  NEGOTIATE_CONTEXT* context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
1227
1228
0
  if (!context)
1229
0
    return SEC_E_INVALID_HANDLE;
1230
1231
0
  WINPR_ASSERT(context->mech);
1232
0
  WINPR_ASSERT(context->mech->pkg);
1233
0
  WINPR_ASSERT(context->mech->pkg->table_w);
1234
0
  if (context->mech->pkg->table_w->QueryContextAttributesW)
1235
0
    return context->mech->pkg->table_w->QueryContextAttributesW(&context->sub_context,
1236
0
                                                                ulAttribute, pBuffer);
1237
1238
0
  return SEC_E_UNSUPPORTED_FUNCTION;
1239
0
}
1240
1241
static SECURITY_STATUS SEC_ENTRY negotiate_QueryContextAttributesA(PCtxtHandle phContext,
1242
                                                                   ULONG ulAttribute, void* pBuffer)
1243
0
{
1244
0
  NEGOTIATE_CONTEXT* context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
1245
1246
0
  if (!context)
1247
0
    return SEC_E_INVALID_HANDLE;
1248
1249
0
  WINPR_ASSERT(context->mech);
1250
0
  WINPR_ASSERT(context->mech->pkg);
1251
0
  WINPR_ASSERT(context->mech->pkg->table);
1252
0
  if (context->mech->pkg->table->QueryContextAttributesA)
1253
0
    return context->mech->pkg->table->QueryContextAttributesA(&context->sub_context,
1254
0
                                                              ulAttribute, pBuffer);
1255
1256
0
  return SEC_E_UNSUPPORTED_FUNCTION;
1257
0
}
1258
1259
static SECURITY_STATUS SEC_ENTRY negotiate_SetContextAttributesW(PCtxtHandle phContext,
1260
                                                                 ULONG ulAttribute, void* pBuffer,
1261
                                                                 ULONG cbBuffer)
1262
0
{
1263
0
  NEGOTIATE_CONTEXT* context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
1264
1265
0
  if (!context)
1266
0
    return SEC_E_INVALID_HANDLE;
1267
1268
0
  WINPR_ASSERT(context->mech);
1269
0
  WINPR_ASSERT(context->mech->pkg);
1270
0
  WINPR_ASSERT(context->mech->pkg->table_w);
1271
0
  if (context->mech->pkg->table_w->SetContextAttributesW)
1272
0
    return context->mech->pkg->table_w->SetContextAttributesW(&context->sub_context,
1273
0
                                                              ulAttribute, pBuffer, cbBuffer);
1274
1275
0
  return SEC_E_UNSUPPORTED_FUNCTION;
1276
0
}
1277
1278
static SECURITY_STATUS SEC_ENTRY negotiate_SetContextAttributesA(PCtxtHandle phContext,
1279
                                                                 ULONG ulAttribute, void* pBuffer,
1280
                                                                 ULONG cbBuffer)
1281
0
{
1282
0
  NEGOTIATE_CONTEXT* context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
1283
1284
0
  if (!context)
1285
0
    return SEC_E_INVALID_HANDLE;
1286
1287
0
  WINPR_ASSERT(context->mech);
1288
0
  WINPR_ASSERT(context->mech->pkg);
1289
0
  WINPR_ASSERT(context->mech->pkg->table);
1290
0
  if (context->mech->pkg->table->SetContextAttributesA)
1291
0
    return context->mech->pkg->table->SetContextAttributesA(&context->sub_context, ulAttribute,
1292
0
                                                            pBuffer, cbBuffer);
1293
1294
0
  return SEC_E_UNSUPPORTED_FUNCTION;
1295
0
}
1296
1297
static SECURITY_STATUS SEC_ENTRY negotiate_SetCredentialsAttributesW(PCredHandle phCredential,
1298
                                                                     ULONG ulAttribute,
1299
                                                                     void* pBuffer, ULONG cbBuffer)
1300
0
{
1301
0
  MechCred* creds;
1302
0
  BOOL success = FALSE;
1303
0
  SECURITY_STATUS secStatus;
1304
1305
0
  creds = sspi_SecureHandleGetLowerPointer(phCredential);
1306
1307
0
  if (!creds)
1308
0
    return SEC_E_INVALID_HANDLE;
1309
1310
0
  for (size_t i = 0; i < MECH_COUNT; i++)
1311
0
  {
1312
0
    MechCred* cred = &creds[i];
1313
1314
0
    WINPR_ASSERT(cred->mech);
1315
0
    WINPR_ASSERT(cred->mech->pkg);
1316
0
    WINPR_ASSERT(cred->mech->pkg->table);
1317
0
    WINPR_ASSERT(cred->mech->pkg->table_w->SetCredentialsAttributesW);
1318
0
    secStatus = cred->mech->pkg->table_w->SetCredentialsAttributesW(&cred->cred, ulAttribute,
1319
0
                                                                    pBuffer, cbBuffer);
1320
1321
0
    if (secStatus == SEC_E_OK)
1322
0
    {
1323
0
      success = TRUE;
1324
0
    }
1325
0
  }
1326
1327
  // return success if at least one submodule accepts the credential attribute
1328
0
  return (success ? SEC_E_OK : SEC_E_UNSUPPORTED_FUNCTION);
1329
0
}
1330
1331
static SECURITY_STATUS SEC_ENTRY negotiate_SetCredentialsAttributesA(PCredHandle phCredential,
1332
                                                                     ULONG ulAttribute,
1333
                                                                     void* pBuffer, ULONG cbBuffer)
1334
0
{
1335
0
  MechCred* creds;
1336
0
  BOOL success = FALSE;
1337
0
  SECURITY_STATUS secStatus;
1338
1339
0
  creds = sspi_SecureHandleGetLowerPointer(phCredential);
1340
1341
0
  if (!creds)
1342
0
    return SEC_E_INVALID_HANDLE;
1343
1344
0
  for (size_t i = 0; i < MECH_COUNT; i++)
1345
0
  {
1346
0
    MechCred* cred = &creds[i];
1347
1348
0
    if (!cred->valid)
1349
0
      continue;
1350
1351
0
    WINPR_ASSERT(cred->mech);
1352
0
    WINPR_ASSERT(cred->mech->pkg);
1353
0
    WINPR_ASSERT(cred->mech->pkg->table);
1354
0
    WINPR_ASSERT(cred->mech->pkg->table->SetCredentialsAttributesA);
1355
0
    secStatus = cred->mech->pkg->table->SetCredentialsAttributesA(&cred->cred, ulAttribute,
1356
0
                                                                  pBuffer, cbBuffer);
1357
1358
0
    if (secStatus == SEC_E_OK)
1359
0
    {
1360
0
      success = TRUE;
1361
0
    }
1362
0
  }
1363
1364
  // return success if at least one submodule accepts the credential attribute
1365
0
  return (success ? SEC_E_OK : SEC_E_UNSUPPORTED_FUNCTION);
1366
0
}
1367
1368
static SECURITY_STATUS SEC_ENTRY negotiate_AcquireCredentialsHandleW(
1369
    SEC_WCHAR* pszPrincipal, SEC_WCHAR* pszPackage, ULONG fCredentialUse, void* pvLogonID,
1370
    void* pAuthData, SEC_GET_KEY_FN pGetKeyFn, void* pvGetKeyArgument, PCredHandle phCredential,
1371
    PTimeStamp ptsExpiry)
1372
0
{
1373
0
  BOOL kerberos, ntlm;
1374
1375
0
  if (!negotiate_get_config(pAuthData, &kerberos, &ntlm))
1376
0
    return SEC_E_INTERNAL_ERROR;
1377
1378
0
  MechCred* creds = calloc(MECH_COUNT, sizeof(MechCred));
1379
1380
0
  if (!creds)
1381
0
    return SEC_E_INTERNAL_ERROR;
1382
1383
0
  for (size_t i = 0; i < MECH_COUNT; i++)
1384
0
  {
1385
0
    MechCred* cred = &creds[i];
1386
0
    const SecPkg* pkg = MechTable[i].pkg;
1387
0
    cred->mech = &MechTable[i];
1388
1389
0
    if (!kerberos && _tcscmp(pkg->name, KERBEROS_SSP_NAME) == 0)
1390
0
      continue;
1391
0
    if (!ntlm && _tcscmp(SecPkgTable[i].name, NTLM_SSP_NAME) == 0)
1392
0
      continue;
1393
1394
0
    WINPR_ASSERT(pkg->table_w);
1395
0
    WINPR_ASSERT(pkg->table_w->AcquireCredentialsHandleW);
1396
0
    if (pkg->table_w->AcquireCredentialsHandleW(
1397
0
            pszPrincipal, pszPackage, fCredentialUse, pvLogonID, pAuthData, pGetKeyFn,
1398
0
            pvGetKeyArgument, &cred->cred, ptsExpiry) != SEC_E_OK)
1399
0
      continue;
1400
1401
0
    cred->valid = TRUE;
1402
0
  }
1403
1404
0
  sspi_SecureHandleSetLowerPointer(phCredential, (void*)creds);
1405
0
  sspi_SecureHandleSetUpperPointer(phCredential, (void*)NEGO_SSP_NAME);
1406
0
  return SEC_E_OK;
1407
0
}
1408
1409
static SECURITY_STATUS SEC_ENTRY negotiate_AcquireCredentialsHandleA(
1410
    SEC_CHAR* pszPrincipal, SEC_CHAR* pszPackage, ULONG fCredentialUse, void* pvLogonID,
1411
    void* pAuthData, SEC_GET_KEY_FN pGetKeyFn, void* pvGetKeyArgument, PCredHandle phCredential,
1412
    PTimeStamp ptsExpiry)
1413
0
{
1414
0
  BOOL kerberos, ntlm;
1415
1416
0
  if (!negotiate_get_config(pAuthData, &kerberos, &ntlm))
1417
0
    return SEC_E_INTERNAL_ERROR;
1418
1419
0
  MechCred* creds = calloc(MECH_COUNT, sizeof(MechCred));
1420
1421
0
  if (!creds)
1422
0
    return SEC_E_INTERNAL_ERROR;
1423
1424
0
  for (size_t i = 0; i < MECH_COUNT; i++)
1425
0
  {
1426
0
    const SecPkg* pkg = MechTable[i].pkg;
1427
0
    MechCred* cred = &creds[i];
1428
1429
0
    cred->mech = &MechTable[i];
1430
1431
0
    if (!kerberos && _tcscmp(pkg->name, KERBEROS_SSP_NAME) == 0)
1432
0
      continue;
1433
0
    if (!ntlm && _tcscmp(SecPkgTable[i].name, NTLM_SSP_NAME) == 0)
1434
0
      continue;
1435
1436
0
    WINPR_ASSERT(pkg->table);
1437
0
    WINPR_ASSERT(pkg->table->AcquireCredentialsHandleA);
1438
0
    if (pkg->table->AcquireCredentialsHandleA(pszPrincipal, pszPackage, fCredentialUse,
1439
0
                                              pvLogonID, pAuthData, pGetKeyFn, pvGetKeyArgument,
1440
0
                                              &cred->cred, ptsExpiry) != SEC_E_OK)
1441
0
      continue;
1442
1443
0
    cred->valid = TRUE;
1444
0
  }
1445
1446
0
  sspi_SecureHandleSetLowerPointer(phCredential, (void*)creds);
1447
0
  sspi_SecureHandleSetUpperPointer(phCredential, (void*)NEGO_SSP_NAME);
1448
0
  return SEC_E_OK;
1449
0
}
1450
1451
static SECURITY_STATUS SEC_ENTRY negotiate_QueryCredentialsAttributesW(PCredHandle phCredential,
1452
                                                                       ULONG ulAttribute,
1453
                                                                       void* pBuffer)
1454
0
{
1455
0
  WLog_ERR(TAG, "TODO: Implement");
1456
0
  return SEC_E_UNSUPPORTED_FUNCTION;
1457
0
}
1458
1459
static SECURITY_STATUS SEC_ENTRY negotiate_QueryCredentialsAttributesA(PCredHandle phCredential,
1460
                                                                       ULONG ulAttribute,
1461
                                                                       void* pBuffer)
1462
0
{
1463
0
  WLog_ERR(TAG, "TODO: Implement");
1464
0
  return SEC_E_UNSUPPORTED_FUNCTION;
1465
0
}
1466
1467
static SECURITY_STATUS SEC_ENTRY negotiate_FreeCredentialsHandle(PCredHandle phCredential)
1468
0
{
1469
0
  MechCred* creds;
1470
1471
0
  creds = sspi_SecureHandleGetLowerPointer(phCredential);
1472
0
  if (!creds)
1473
0
    return SEC_E_INVALID_HANDLE;
1474
1475
0
  for (size_t i = 0; i < MECH_COUNT; i++)
1476
0
  {
1477
0
    MechCred* cred = &creds[i];
1478
1479
0
    WINPR_ASSERT(cred->mech);
1480
0
    WINPR_ASSERT(cred->mech->pkg);
1481
0
    WINPR_ASSERT(cred->mech->pkg->table);
1482
0
    WINPR_ASSERT(cred->mech->pkg->table->FreeCredentialsHandle);
1483
0
    cred->mech->pkg->table->FreeCredentialsHandle(&cred->cred);
1484
0
  }
1485
0
  free(creds);
1486
1487
0
  return SEC_E_OK;
1488
0
}
1489
1490
static SECURITY_STATUS SEC_ENTRY negotiate_EncryptMessage(PCtxtHandle phContext, ULONG fQOP,
1491
                                                          PSecBufferDesc pMessage,
1492
                                                          ULONG MessageSeqNo)
1493
0
{
1494
0
  NEGOTIATE_CONTEXT* context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
1495
1496
0
  if (!context)
1497
0
    return SEC_E_INVALID_HANDLE;
1498
1499
0
  if (context->mic)
1500
0
    MessageSeqNo++;
1501
1502
0
  WINPR_ASSERT(context->mech);
1503
0
  WINPR_ASSERT(context->mech->pkg);
1504
0
  WINPR_ASSERT(context->mech->pkg->table);
1505
0
  if (context->mech->pkg->table->EncryptMessage)
1506
0
    return context->mech->pkg->table->EncryptMessage(&context->sub_context, fQOP, pMessage,
1507
0
                                                     MessageSeqNo);
1508
1509
0
  return SEC_E_UNSUPPORTED_FUNCTION;
1510
0
}
1511
1512
static SECURITY_STATUS SEC_ENTRY negotiate_DecryptMessage(PCtxtHandle phContext,
1513
                                                          PSecBufferDesc pMessage,
1514
                                                          ULONG MessageSeqNo, ULONG* pfQOP)
1515
0
{
1516
0
  NEGOTIATE_CONTEXT* context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
1517
1518
0
  if (!context)
1519
0
    return SEC_E_INVALID_HANDLE;
1520
1521
0
  if (context->mic)
1522
0
    MessageSeqNo++;
1523
1524
0
  WINPR_ASSERT(context->mech);
1525
0
  WINPR_ASSERT(context->mech->pkg);
1526
0
  WINPR_ASSERT(context->mech->pkg->table);
1527
0
  if (context->mech->pkg->table->DecryptMessage)
1528
0
    return context->mech->pkg->table->DecryptMessage(&context->sub_context, pMessage,
1529
0
                                                     MessageSeqNo, pfQOP);
1530
1531
0
  return SEC_E_UNSUPPORTED_FUNCTION;
1532
0
}
1533
1534
static SECURITY_STATUS SEC_ENTRY negotiate_MakeSignature(PCtxtHandle phContext, ULONG fQOP,
1535
                                                         PSecBufferDesc pMessage,
1536
                                                         ULONG MessageSeqNo)
1537
0
{
1538
0
  NEGOTIATE_CONTEXT* context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
1539
1540
0
  if (!context)
1541
0
    return SEC_E_INVALID_HANDLE;
1542
1543
0
  if (context->mic)
1544
0
    MessageSeqNo++;
1545
1546
0
  WINPR_ASSERT(context->mech);
1547
0
  WINPR_ASSERT(context->mech->pkg);
1548
0
  WINPR_ASSERT(context->mech->pkg->table);
1549
0
  if (context->mech->pkg->table->MakeSignature)
1550
0
    return context->mech->pkg->table->MakeSignature(&context->sub_context, fQOP, pMessage,
1551
0
                                                    MessageSeqNo);
1552
1553
0
  return SEC_E_UNSUPPORTED_FUNCTION;
1554
0
}
1555
1556
static SECURITY_STATUS SEC_ENTRY negotiate_VerifySignature(PCtxtHandle phContext,
1557
                                                           PSecBufferDesc pMessage,
1558
                                                           ULONG MessageSeqNo, ULONG* pfQOP)
1559
0
{
1560
0
  NEGOTIATE_CONTEXT* context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
1561
1562
0
  if (!context)
1563
0
    return SEC_E_INVALID_HANDLE;
1564
1565
0
  if (context->mic)
1566
0
    MessageSeqNo++;
1567
1568
0
  WINPR_ASSERT(context->mech);
1569
0
  WINPR_ASSERT(context->mech->pkg);
1570
0
  WINPR_ASSERT(context->mech->pkg->table);
1571
0
  if (context->mech->pkg->table->VerifySignature)
1572
0
    return context->mech->pkg->table->VerifySignature(&context->sub_context, pMessage,
1573
0
                                                      MessageSeqNo, pfQOP);
1574
1575
0
  return SEC_E_UNSUPPORTED_FUNCTION;
1576
0
}
1577
1578
const SecurityFunctionTableA NEGOTIATE_SecurityFunctionTableA = {
1579
  3,                                     /* dwVersion */
1580
  NULL,                                  /* EnumerateSecurityPackages */
1581
  negotiate_QueryCredentialsAttributesA, /* QueryCredentialsAttributes */
1582
  negotiate_AcquireCredentialsHandleA,   /* AcquireCredentialsHandle */
1583
  negotiate_FreeCredentialsHandle,       /* FreeCredentialsHandle */
1584
  NULL,                                  /* Reserved2 */
1585
  negotiate_InitializeSecurityContextA,  /* InitializeSecurityContext */
1586
  negotiate_AcceptSecurityContext,       /* AcceptSecurityContext */
1587
  negotiate_CompleteAuthToken,           /* CompleteAuthToken */
1588
  negotiate_DeleteSecurityContext,       /* DeleteSecurityContext */
1589
  NULL,                                  /* ApplyControlToken */
1590
  negotiate_QueryContextAttributesA,     /* QueryContextAttributes */
1591
  negotiate_ImpersonateSecurityContext,  /* ImpersonateSecurityContext */
1592
  negotiate_RevertSecurityContext,       /* RevertSecurityContext */
1593
  negotiate_MakeSignature,               /* MakeSignature */
1594
  negotiate_VerifySignature,             /* VerifySignature */
1595
  NULL,                                  /* FreeContextBuffer */
1596
  NULL,                                  /* QuerySecurityPackageInfo */
1597
  NULL,                                  /* Reserved3 */
1598
  NULL,                                  /* Reserved4 */
1599
  NULL,                                  /* ExportSecurityContext */
1600
  NULL,                                  /* ImportSecurityContext */
1601
  NULL,                                  /* AddCredentials */
1602
  NULL,                                  /* Reserved8 */
1603
  NULL,                                  /* QuerySecurityContextToken */
1604
  negotiate_EncryptMessage,              /* EncryptMessage */
1605
  negotiate_DecryptMessage,              /* DecryptMessage */
1606
  negotiate_SetContextAttributesA,       /* SetContextAttributes */
1607
  negotiate_SetCredentialsAttributesA,   /* SetCredentialsAttributes */
1608
};
1609
1610
const SecurityFunctionTableW NEGOTIATE_SecurityFunctionTableW = {
1611
  3,                                     /* dwVersion */
1612
  NULL,                                  /* EnumerateSecurityPackages */
1613
  negotiate_QueryCredentialsAttributesW, /* QueryCredentialsAttributes */
1614
  negotiate_AcquireCredentialsHandleW,   /* AcquireCredentialsHandle */
1615
  negotiate_FreeCredentialsHandle,       /* FreeCredentialsHandle */
1616
  NULL,                                  /* Reserved2 */
1617
  negotiate_InitializeSecurityContextW,  /* InitializeSecurityContext */
1618
  negotiate_AcceptSecurityContext,       /* AcceptSecurityContext */
1619
  negotiate_CompleteAuthToken,           /* CompleteAuthToken */
1620
  negotiate_DeleteSecurityContext,       /* DeleteSecurityContext */
1621
  NULL,                                  /* ApplyControlToken */
1622
  negotiate_QueryContextAttributesW,     /* QueryContextAttributes */
1623
  negotiate_ImpersonateSecurityContext,  /* ImpersonateSecurityContext */
1624
  negotiate_RevertSecurityContext,       /* RevertSecurityContext */
1625
  negotiate_MakeSignature,               /* MakeSignature */
1626
  negotiate_VerifySignature,             /* VerifySignature */
1627
  NULL,                                  /* FreeContextBuffer */
1628
  NULL,                                  /* QuerySecurityPackageInfo */
1629
  NULL,                                  /* Reserved3 */
1630
  NULL,                                  /* Reserved4 */
1631
  NULL,                                  /* ExportSecurityContext */
1632
  NULL,                                  /* ImportSecurityContext */
1633
  NULL,                                  /* AddCredentials */
1634
  NULL,                                  /* Reserved8 */
1635
  NULL,                                  /* QuerySecurityContextToken */
1636
  negotiate_EncryptMessage,              /* EncryptMessage */
1637
  negotiate_DecryptMessage,              /* DecryptMessage */
1638
  negotiate_SetContextAttributesW,       /* SetContextAttributes */
1639
  negotiate_SetCredentialsAttributesW,   /* SetCredentialsAttributes */
1640
};