Coverage Report

Created: 2026-01-10 06:30

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