Coverage Report

Created: 2025-10-10 06:50

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
          token->negState = rd;
545
0
          WLog_DBG(TAG, "\tnegState [0] (%d)", token->negState);
546
0
        }
547
0
        break;
548
0
      case 1:
549
0
        if (token->init)
550
0
        {
551
          /* reqFlags [1] ContextFlags BIT STRING (ignored) */
552
0
          if (!WinPrAsn1DecPeekTagAndLen(&dec2, &tag, &len) || (tag != ER_TAG_BIT_STRING))
553
0
            return FALSE;
554
0
          WLog_DBG(TAG, "\treqFlags [1] (%li bytes)", len);
555
0
        }
556
0
        else
557
0
        {
558
          /* supportedMech [1] MechType */
559
0
          if (!WinPrAsn1DecReadOID(&dec2, &token->supportedMech, FALSE))
560
0
            return FALSE;
561
0
          WLog_DBG(TAG, "\tsupportedMech [1] (%s)",
562
0
                   negotiate_mech_name(&token->supportedMech));
563
0
        }
564
0
        break;
565
0
      case 2:
566
        /* mechToken [2] OCTET STRING */
567
0
        if (!WinPrAsn1DecReadOctetString(&dec2, &octet_string, FALSE))
568
0
          return FALSE;
569
0
        if (octet_string.len > UINT32_MAX)
570
0
          return FALSE;
571
0
        token->mechToken.cbBuffer = (UINT32)octet_string.len;
572
0
        token->mechToken.pvBuffer = octet_string.data;
573
0
        token->mechToken.BufferType = SECBUFFER_TOKEN;
574
0
        WLog_DBG(TAG, "\tmechToken [2] (%li bytes)", octet_string.len);
575
0
        break;
576
0
      case 3:
577
        /* mechListMic [3] OCTET STRING */
578
0
        if (!WinPrAsn1DecReadOctetString(&dec2, &octet_string, FALSE))
579
0
          return FALSE;
580
0
        if (octet_string.len > UINT32_MAX)
581
0
          return FALSE;
582
0
        token->mic.cbBuffer = (UINT32)octet_string.len;
583
0
        token->mic.pvBuffer = octet_string.data;
584
0
        token->mic.BufferType = SECBUFFER_TOKEN;
585
0
        WLog_DBG(TAG, "\tmechListMIC [3] (%li bytes)", octet_string.len);
586
0
        break;
587
0
      default:
588
0
        WLog_ERR(TAG, "unknown contextual item %d", contextual);
589
0
        return FALSE;
590
0
    }
591
0
  } while (WinPrAsn1DecPeekTag(&dec, &tag));
592
593
0
  return TRUE;
594
0
}
595
596
static SECURITY_STATUS negotiate_mic_exchange(NEGOTIATE_CONTEXT* context, NegToken* input_token,
597
                                              NegToken* output_token, PSecBuffer output_buffer)
598
0
{
599
0
  SecBuffer mic_buffers[2] = { 0 };
600
0
  SecBufferDesc mic_buffer_desc = { SECBUFFER_VERSION, 2, mic_buffers };
601
0
  SECURITY_STATUS status = 0;
602
603
0
  WINPR_ASSERT(context);
604
0
  WINPR_ASSERT(input_token);
605
0
  WINPR_ASSERT(output_token);
606
0
  WINPR_ASSERT(context->mech);
607
0
  WINPR_ASSERT(context->mech->pkg);
608
609
0
  const SecurityFunctionTableA* table = context->mech->pkg->table;
610
0
  WINPR_ASSERT(table);
611
612
0
  mic_buffers[0] = context->mechTypes;
613
614
  /* Verify MIC if we received one */
615
0
  if (input_token->mic.cbBuffer > 0)
616
0
  {
617
0
    mic_buffers[1] = input_token->mic;
618
619
0
    status = table->VerifySignature(&context->sub_context, &mic_buffer_desc, 0, 0);
620
0
    if (status != SEC_E_OK)
621
0
      return status;
622
623
0
    output_token->negState = ACCEPT_COMPLETED;
624
0
  }
625
626
  /* If peer expects a MIC then generate it */
627
0
  if (input_token->negState != ACCEPT_COMPLETED)
628
0
  {
629
    /* Store the mic token after the mech token in the output buffer */
630
0
    output_token->mic.BufferType = SECBUFFER_TOKEN;
631
0
    if (output_buffer)
632
0
    {
633
0
      output_token->mic.cbBuffer = output_buffer->cbBuffer - output_token->mechToken.cbBuffer;
634
0
      output_token->mic.pvBuffer =
635
0
          (BYTE*)output_buffer->pvBuffer + output_token->mechToken.cbBuffer;
636
0
    }
637
0
    mic_buffers[1] = output_token->mic;
638
639
0
    status = table->MakeSignature(&context->sub_context, 0, &mic_buffer_desc, 0);
640
0
    if (status != SEC_E_OK)
641
0
      return status;
642
643
0
    output_token->mic = mic_buffers[1];
644
0
  }
645
646
  /* When using NTLM cipher states need to be reset after mic exchange */
647
0
  const TCHAR* name = sspi_SecureHandleGetUpperPointer(&context->sub_context);
648
0
  if (!name)
649
0
    return SEC_E_INTERNAL_ERROR;
650
651
0
  if (_tcsncmp(name, NTLM_SSP_NAME, ARRAYSIZE(NTLM_SSP_NAME)) == 0)
652
0
  {
653
0
    if (!ntlm_reset_cipher_state(&context->sub_context))
654
0
      return SEC_E_INTERNAL_ERROR;
655
0
  }
656
657
0
  return SEC_E_OK;
658
0
}
659
660
static SECURITY_STATUS SEC_ENTRY negotiate_InitializeSecurityContextW(
661
    PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR* pszTargetName, ULONG fContextReq,
662
    ULONG Reserved1, ULONG TargetDataRep, PSecBufferDesc pInput, ULONG Reserved2,
663
    PCtxtHandle phNewContext, PSecBufferDesc pOutput, PULONG pfContextAttr, PTimeStamp ptsExpiry)
664
0
{
665
0
  NEGOTIATE_CONTEXT* context = NULL;
666
0
  NEGOTIATE_CONTEXT init_context = { 0 };
667
0
  MechCred* creds = NULL;
668
0
  PCtxtHandle sub_context = NULL;
669
0
  PCredHandle sub_cred = NULL;
670
0
  NegToken input_token = empty_neg_token;
671
0
  NegToken output_token = empty_neg_token;
672
0
  PSecBuffer input_buffer = NULL;
673
0
  PSecBuffer output_buffer = NULL;
674
0
  PSecBuffer bindings_buffer = NULL;
675
0
  SecBuffer mech_input_buffers[2] = { 0 };
676
0
  SecBufferDesc mech_input = { SECBUFFER_VERSION, 2, mech_input_buffers };
677
0
  SecBufferDesc mech_output = { SECBUFFER_VERSION, 1, &output_token.mechToken };
678
0
  SECURITY_STATUS status = SEC_E_INTERNAL_ERROR;
679
0
  SECURITY_STATUS sub_status = SEC_E_INTERNAL_ERROR;
680
0
  WinPrAsn1Encoder* enc = NULL;
681
0
  wStream s;
682
0
  const Mech* mech = NULL;
683
684
0
  if (!phCredential || !SecIsValidHandle(phCredential))
685
0
    return SEC_E_NO_CREDENTIALS;
686
687
0
  creds = sspi_SecureHandleGetLowerPointer(phCredential);
688
689
  /* behave like windows SSPIs that don't want empty context */
690
0
  if (phContext && !phContext->dwLower && !phContext->dwUpper)
691
0
    return SEC_E_INVALID_HANDLE;
692
693
0
  context = sspi_SecureHandleGetLowerPointer(phContext);
694
695
0
  if (pInput)
696
0
  {
697
0
    input_buffer = sspi_FindSecBuffer(pInput, SECBUFFER_TOKEN);
698
0
    bindings_buffer = sspi_FindSecBuffer(pInput, SECBUFFER_CHANNEL_BINDINGS);
699
0
  }
700
0
  if (pOutput)
701
0
    output_buffer = sspi_FindSecBuffer(pOutput, SECBUFFER_TOKEN);
702
703
0
  if (!context)
704
0
  {
705
0
    enc = WinPrAsn1Encoder_New(WINPR_ASN1_DER);
706
0
    if (!enc)
707
0
      return SEC_E_INSUFFICIENT_MEMORY;
708
709
0
    if (!WinPrAsn1EncSeqContainer(enc))
710
0
      goto cleanup;
711
712
0
    for (size_t i = 0; i < MECH_COUNT; i++)
713
0
    {
714
0
      MechCred* cred = &creds[i];
715
0
      const SecPkg* pkg = MechTable[i].pkg;
716
0
      WINPR_ASSERT(pkg);
717
0
      WINPR_ASSERT(pkg->table_w);
718
719
0
      if (!cred->valid)
720
0
      {
721
0
        WLog_DBG(TAG, "Unavailable mechanism: %s", negotiate_mech_name(cred->mech->oid));
722
0
        continue;
723
0
      }
724
725
      /* Send an optimistic token for the first valid mechanism */
726
0
      if (!init_context.mech)
727
0
      {
728
        /* Use the output buffer to store the optimistic token */
729
0
        if (!output_buffer)
730
0
          goto cleanup;
731
732
0
        CopyMemory(&output_token.mechToken, output_buffer, sizeof(SecBuffer));
733
734
0
        if (bindings_buffer)
735
0
          mech_input_buffers[0] = *bindings_buffer;
736
737
0
        WINPR_ASSERT(pkg->table_w->InitializeSecurityContextW);
738
0
        sub_status = pkg->table_w->InitializeSecurityContextW(
739
0
            &cred->cred, NULL, pszTargetName, fContextReq | cred->mech->flags, Reserved1,
740
0
            TargetDataRep, &mech_input, Reserved2, &init_context.sub_context, &mech_output,
741
0
            pfContextAttr, ptsExpiry);
742
743
        /* If the mechanism failed we can't use it; skip */
744
0
        if (IsSecurityStatusError(sub_status))
745
0
        {
746
0
          if (SecIsValidHandle(&init_context.sub_context))
747
0
          {
748
0
            WINPR_ASSERT(pkg->table_w->DeleteSecurityContext);
749
0
            pkg->table_w->DeleteSecurityContext(&init_context.sub_context);
750
0
          }
751
0
          cred->valid = FALSE;
752
0
          continue;
753
0
        }
754
755
0
        init_context.mech = cred->mech;
756
0
      }
757
758
0
      if (!WinPrAsn1EncOID(enc, cred->mech->oid))
759
0
        goto cleanup;
760
0
      WLog_DBG(TAG, "Available mechanism: %s", negotiate_mech_name(cred->mech->oid));
761
0
    }
762
763
    /* No usable mechanisms were found */
764
0
    if (!init_context.mech)
765
0
      goto cleanup;
766
767
    /* If the only available mech is NTLM use it directly otherwise use spnego */
768
0
    if (init_context.mech->oid == &ntlm_OID)
769
0
    {
770
0
      init_context.spnego = FALSE;
771
0
      output_buffer->cbBuffer = output_token.mechToken.cbBuffer;
772
0
      WLog_DBG(TAG, "Using direct NTLM");
773
0
    }
774
0
    else
775
0
    {
776
0
      init_context.spnego = TRUE;
777
0
      init_context.mechTypes.BufferType = SECBUFFER_DATA;
778
0
      const size_t cb = WinPrAsn1EncEndContainer(enc);
779
0
      WINPR_ASSERT(cb <= UINT32_MAX);
780
0
      init_context.mechTypes.cbBuffer = (UINT32)cb;
781
0
    }
782
783
    /* Allocate memory for the new context */
784
0
    context = negotiate_ContextNew(&init_context);
785
0
    if (!context)
786
0
    {
787
0
      init_context.mech->pkg->table->DeleteSecurityContext(&init_context.sub_context);
788
0
      WinPrAsn1Encoder_Free(&enc);
789
0
      return SEC_E_INSUFFICIENT_MEMORY;
790
0
    }
791
792
0
    sspi_SecureHandleSetUpperPointer(phNewContext, NEGO_SSP_NAME);
793
0
    sspi_SecureHandleSetLowerPointer(phNewContext, context);
794
795
0
    if (!context->spnego)
796
0
    {
797
0
      status = sub_status;
798
0
      goto cleanup;
799
0
    }
800
801
    /* Write mechTypesList */
802
0
    Stream_StaticInit(&s, context->mechTypes.pvBuffer, context->mechTypes.cbBuffer);
803
0
    if (!WinPrAsn1EncToStream(enc, &s))
804
0
      goto cleanup;
805
806
0
    output_token.mechTypes.cbBuffer = context->mechTypes.cbBuffer;
807
0
    output_token.mechTypes.pvBuffer = context->mechTypes.pvBuffer;
808
0
    output_token.init = TRUE;
809
810
0
    if (sub_status == SEC_E_OK)
811
0
      context->state = NEGOTIATE_STATE_FINAL_OPTIMISTIC;
812
0
  }
813
0
  else
814
0
  {
815
0
    if (!input_buffer)
816
0
      return SEC_E_INVALID_TOKEN;
817
818
0
    sub_context = &context->sub_context;
819
0
    sub_cred = negotiate_FindCredential(creds, context->mech);
820
821
0
    if (!context->spnego)
822
0
    {
823
0
      return context->mech->pkg->table_w->InitializeSecurityContextW(
824
0
          sub_cred, sub_context, pszTargetName, fContextReq | context->mech->flags, Reserved1,
825
0
          TargetDataRep, pInput, Reserved2, sub_context, pOutput, pfContextAttr, ptsExpiry);
826
0
    }
827
828
0
    if (!negotiate_read_neg_token(input_buffer, &input_token))
829
0
      return SEC_E_INVALID_TOKEN;
830
831
    /* On first response check if the server doesn't like out preferred mech */
832
0
    if (context->state < NEGOTIATE_STATE_NEGORESP && input_token.supportedMech.len &&
833
0
        !sspi_gss_oid_compare(&input_token.supportedMech, context->mech->oid))
834
0
    {
835
0
      mech = negotiate_GetMechByOID(&input_token.supportedMech);
836
0
      if (!mech)
837
0
        return SEC_E_INVALID_TOKEN;
838
839
      /* Make sure the specified mech is supported and get the appropriate credential */
840
0
      sub_cred = negotiate_FindCredential(creds, mech);
841
0
      if (!sub_cred)
842
0
        return SEC_E_INVALID_TOKEN;
843
844
      /* Clean up the optimistic mech */
845
0
      context->mech->pkg->table_w->DeleteSecurityContext(&context->sub_context);
846
0
      sub_context = NULL;
847
848
0
      context->mech = mech;
849
0
      context->mic = TRUE;
850
0
    }
851
852
    /* Check neg_state (required on first response) */
853
0
    if (context->state < NEGOTIATE_STATE_NEGORESP)
854
0
    {
855
0
      switch (input_token.negState)
856
0
      {
857
0
        case NOSTATE:
858
0
          return SEC_E_INVALID_TOKEN;
859
0
        case REJECT:
860
0
          return SEC_E_LOGON_DENIED;
861
0
        case REQUEST_MIC:
862
0
          context->mic = TRUE;
863
          /* fallthrough */
864
0
          WINPR_FALLTHROUGH
865
0
        case ACCEPT_INCOMPLETE:
866
0
          context->state = NEGOTIATE_STATE_NEGORESP;
867
0
          break;
868
0
        case ACCEPT_COMPLETED:
869
0
          if (context->state == NEGOTIATE_STATE_INITIAL)
870
0
            context->state = NEGOTIATE_STATE_NEGORESP;
871
0
          else
872
0
            context->state = NEGOTIATE_STATE_FINAL;
873
0
          break;
874
0
        default:
875
0
          break;
876
0
      }
877
878
0
      WLog_DBG(TAG, "Negotiated mechanism: %s", negotiate_mech_name(context->mech->oid));
879
0
    }
880
881
0
    if (context->state == NEGOTIATE_STATE_NEGORESP)
882
0
    {
883
      /* Store the mech token in the output buffer */
884
0
      if (!output_buffer)
885
0
        goto cleanup;
886
0
      CopyMemory(&output_token.mechToken, output_buffer, sizeof(SecBuffer));
887
888
0
      mech_input_buffers[0] = input_token.mechToken;
889
0
      if (bindings_buffer)
890
0
        mech_input_buffers[1] = *bindings_buffer;
891
892
0
      status = context->mech->pkg->table_w->InitializeSecurityContextW(
893
0
          sub_cred, sub_context, pszTargetName, fContextReq | context->mech->flags, Reserved1,
894
0
          TargetDataRep, input_token.mechToken.cbBuffer ? &mech_input : NULL, Reserved2,
895
0
          &context->sub_context, &mech_output, pfContextAttr, ptsExpiry);
896
897
0
      if (IsSecurityStatusError(status))
898
0
        return status;
899
0
    }
900
901
0
    if (status == SEC_E_OK)
902
0
    {
903
0
      if (output_token.mechToken.cbBuffer > 0)
904
0
        context->state = NEGOTIATE_STATE_MIC;
905
0
      else
906
0
        context->state = NEGOTIATE_STATE_FINAL;
907
0
    }
908
909
    /* Check if the acceptor sent its final token without a mic */
910
0
    if (context->state == NEGOTIATE_STATE_FINAL && input_token.mic.cbBuffer == 0)
911
0
    {
912
0
      if (context->mic || input_token.negState != ACCEPT_COMPLETED)
913
0
        return SEC_E_INVALID_TOKEN;
914
915
0
      if (output_buffer)
916
0
        output_buffer->cbBuffer = 0;
917
0
      return SEC_E_OK;
918
0
    }
919
920
0
    if ((context->state == NEGOTIATE_STATE_MIC && context->mic) ||
921
0
        context->state == NEGOTIATE_STATE_FINAL)
922
0
    {
923
0
      status = negotiate_mic_exchange(context, &input_token, &output_token, output_buffer);
924
0
      if (status != SEC_E_OK)
925
0
        return status;
926
0
    }
927
0
  }
928
929
0
  if (input_token.negState == ACCEPT_COMPLETED)
930
0
  {
931
0
    if (output_buffer)
932
0
      output_buffer->cbBuffer = 0;
933
0
    return SEC_E_OK;
934
0
  }
935
936
0
  if (output_token.negState == ACCEPT_COMPLETED)
937
0
    status = SEC_E_OK;
938
0
  else
939
0
    status = SEC_I_CONTINUE_NEEDED;
940
941
0
  if (!negotiate_write_neg_token(output_buffer, &output_token))
942
0
    status = SEC_E_INTERNAL_ERROR;
943
944
0
cleanup:
945
0
  WinPrAsn1Encoder_Free(&enc);
946
0
  return status;
947
0
}
948
949
static SECURITY_STATUS SEC_ENTRY negotiate_InitializeSecurityContextA(
950
    PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR* pszTargetName, ULONG fContextReq,
951
    ULONG Reserved1, ULONG TargetDataRep, PSecBufferDesc pInput, ULONG Reserved2,
952
    PCtxtHandle phNewContext, PSecBufferDesc pOutput, PULONG pfContextAttr, PTimeStamp ptsExpiry)
953
0
{
954
0
  SECURITY_STATUS status = 0;
955
0
  SEC_WCHAR* pszTargetNameW = NULL;
956
957
0
  if (pszTargetName)
958
0
  {
959
0
    pszTargetNameW = ConvertUtf8ToWCharAlloc(pszTargetName, NULL);
960
0
    if (!pszTargetNameW)
961
0
      return SEC_E_INTERNAL_ERROR;
962
0
  }
963
964
0
  status = negotiate_InitializeSecurityContextW(
965
0
      phCredential, phContext, pszTargetNameW, fContextReq, Reserved1, TargetDataRep, pInput,
966
0
      Reserved2, phNewContext, pOutput, pfContextAttr, ptsExpiry);
967
0
  free(pszTargetNameW);
968
0
  return status;
969
0
}
970
971
static const Mech* guessMech(PSecBuffer input_buffer, BOOL* spNego, WinPrAsn1_OID* oid)
972
0
{
973
0
  WinPrAsn1Decoder decoder;
974
0
  WinPrAsn1Decoder appDecoder;
975
0
  WinPrAsn1_tagId tag = 0;
976
0
  const char ssp[] = "NTLMSSP";
977
978
0
  *spNego = FALSE;
979
980
  /* Check for NTLM token */
981
0
  if (input_buffer->cbBuffer >= 8 && strncmp(input_buffer->pvBuffer, ssp, sizeof(ssp)) == 0)
982
0
  {
983
0
    *oid = ntlm_OID;
984
0
    return negotiate_GetMechByOID(&ntlm_OID);
985
0
  }
986
987
  /* Read initialContextToken or raw Kerberos token */
988
0
  WinPrAsn1Decoder_InitMem(&decoder, WINPR_ASN1_DER, input_buffer->pvBuffer,
989
0
                           input_buffer->cbBuffer);
990
991
0
  if (!WinPrAsn1DecReadApp(&decoder, &tag, &appDecoder) || tag != 0)
992
0
    return NULL;
993
994
0
  if (!WinPrAsn1DecReadOID(&appDecoder, oid, FALSE))
995
0
    return NULL;
996
997
0
  if (sspi_gss_oid_compare(oid, &spnego_OID))
998
0
  {
999
0
    *spNego = TRUE;
1000
0
    return NULL;
1001
0
  }
1002
1003
0
  return negotiate_GetMechByOID(oid);
1004
0
}
1005
1006
static SECURITY_STATUS SEC_ENTRY negotiate_AcceptSecurityContext(
1007
    PCredHandle phCredential, PCtxtHandle phContext, PSecBufferDesc pInput, ULONG fContextReq,
1008
    ULONG TargetDataRep, PCtxtHandle phNewContext, PSecBufferDesc pOutput, PULONG pfContextAttr,
1009
    PTimeStamp ptsTimeStamp)
1010
0
{
1011
0
  NEGOTIATE_CONTEXT* context = NULL;
1012
0
  NEGOTIATE_CONTEXT init_context = { 0 };
1013
0
  MechCred* creds = NULL;
1014
0
  PCredHandle sub_cred = NULL;
1015
0
  NegToken input_token = empty_neg_token;
1016
0
  NegToken output_token = empty_neg_token;
1017
0
  PSecBuffer input_buffer = NULL;
1018
0
  PSecBuffer output_buffer = NULL;
1019
0
  SecBufferDesc mech_input = { SECBUFFER_VERSION, 1, &input_token.mechToken };
1020
0
  SecBufferDesc mech_output = { SECBUFFER_VERSION, 1, &output_token.mechToken };
1021
0
  SECURITY_STATUS status = SEC_E_INTERNAL_ERROR;
1022
0
  WinPrAsn1Decoder dec;
1023
0
  WinPrAsn1Decoder dec2;
1024
0
  WinPrAsn1_tagId tag = 0;
1025
0
  WinPrAsn1_OID oid = { 0 };
1026
0
  const Mech* first_mech = NULL;
1027
1028
0
  if (!phCredential || !SecIsValidHandle(phCredential))
1029
0
    return SEC_E_NO_CREDENTIALS;
1030
1031
0
  creds = sspi_SecureHandleGetLowerPointer(phCredential);
1032
1033
0
  if (!pInput)
1034
0
    return SEC_E_INVALID_TOKEN;
1035
1036
  /* behave like windows SSPIs that don't want empty context */
1037
0
  if (phContext && !phContext->dwLower && !phContext->dwUpper)
1038
0
    return SEC_E_INVALID_HANDLE;
1039
1040
0
  context = sspi_SecureHandleGetLowerPointer(phContext);
1041
1042
0
  input_buffer = sspi_FindSecBuffer(pInput, SECBUFFER_TOKEN);
1043
0
  if (pOutput)
1044
0
    output_buffer = sspi_FindSecBuffer(pOutput, SECBUFFER_TOKEN);
1045
1046
0
  if (!context)
1047
0
  {
1048
0
    init_context.mech = guessMech(input_buffer, &init_context.spnego, &oid);
1049
0
    if (!init_context.mech && !init_context.spnego)
1050
0
      return SEC_E_INVALID_TOKEN;
1051
1052
0
    WLog_DBG(TAG, "Mechanism: %s", negotiate_mech_name(&oid));
1053
1054
0
    if (init_context.spnego)
1055
0
    {
1056
      /* Process spnego token */
1057
0
      if (!negotiate_read_neg_token(input_buffer, &input_token))
1058
0
        return SEC_E_INVALID_TOKEN;
1059
1060
      /* First token must be negoTokenInit and must contain a mechList */
1061
0
      if (!input_token.init || input_token.mechTypes.cbBuffer == 0)
1062
0
        return SEC_E_INVALID_TOKEN;
1063
1064
0
      init_context.mechTypes.BufferType = SECBUFFER_DATA;
1065
0
      init_context.mechTypes.cbBuffer = input_token.mechTypes.cbBuffer;
1066
1067
      /* Prepare to read mechList */
1068
0
      WinPrAsn1Decoder_InitMem(&dec, WINPR_ASN1_DER, input_token.mechTypes.pvBuffer,
1069
0
                               input_token.mechTypes.cbBuffer);
1070
1071
0
      if (!WinPrAsn1DecReadSequence(&dec, &dec2))
1072
0
        return SEC_E_INVALID_TOKEN;
1073
0
      dec = dec2;
1074
1075
      /* If an optimistic token was provided pass it into the first mech */
1076
0
      if (input_token.mechToken.cbBuffer)
1077
0
      {
1078
0
        if (!WinPrAsn1DecReadOID(&dec, &oid, FALSE))
1079
0
          return SEC_E_INVALID_TOKEN;
1080
1081
0
        init_context.mech = negotiate_GetMechByOID(&oid);
1082
1083
0
        if (init_context.mech)
1084
0
        {
1085
0
          if (output_buffer)
1086
0
            output_token.mechToken = *output_buffer;
1087
0
          WLog_DBG(TAG, "Requested mechanism: %s",
1088
0
                   negotiate_mech_name(init_context.mech->oid));
1089
0
        }
1090
0
      }
1091
0
    }
1092
1093
0
    if (init_context.mech)
1094
0
    {
1095
0
      sub_cred = negotiate_FindCredential(creds, init_context.mech);
1096
1097
0
      status = init_context.mech->pkg->table->AcceptSecurityContext(
1098
0
          sub_cred, NULL, init_context.spnego ? &mech_input : pInput, fContextReq,
1099
0
          TargetDataRep, &init_context.sub_context,
1100
0
          init_context.spnego ? &mech_output : pOutput, pfContextAttr, ptsTimeStamp);
1101
0
    }
1102
1103
0
    if (IsSecurityStatusError(status))
1104
0
    {
1105
0
      if (!init_context.spnego)
1106
0
        return status;
1107
1108
0
      init_context.mic = TRUE;
1109
0
      first_mech = init_context.mech;
1110
0
      init_context.mech = NULL;
1111
0
      output_token.mechToken.cbBuffer = 0;
1112
0
    }
1113
1114
0
    while (!init_context.mech && WinPrAsn1DecPeekTag(&dec, &tag))
1115
0
    {
1116
      /* Read each mechanism */
1117
0
      if (!WinPrAsn1DecReadOID(&dec, &oid, FALSE))
1118
0
        return SEC_E_INVALID_TOKEN;
1119
1120
0
      init_context.mech = negotiate_GetMechByOID(&oid);
1121
0
      WLog_DBG(TAG, "Requested mechanism: %s", negotiate_mech_name(&oid));
1122
1123
      /* Microsoft may send two versions of the kerberos OID */
1124
0
      if (init_context.mech == first_mech)
1125
0
        init_context.mech = NULL;
1126
1127
0
      if (init_context.mech && !negotiate_FindCredential(creds, init_context.mech))
1128
0
        init_context.mech = NULL;
1129
0
    }
1130
1131
0
    if (!init_context.mech)
1132
0
      return SEC_E_INTERNAL_ERROR;
1133
1134
0
    context = negotiate_ContextNew(&init_context);
1135
0
    if (!context)
1136
0
    {
1137
0
      if (!IsSecurityStatusError(status))
1138
0
        init_context.mech->pkg->table->DeleteSecurityContext(&init_context.sub_context);
1139
0
      return SEC_E_INSUFFICIENT_MEMORY;
1140
0
    }
1141
1142
0
    sspi_SecureHandleSetUpperPointer(phNewContext, NEGO_SSP_NAME);
1143
0
    sspi_SecureHandleSetLowerPointer(phNewContext, context);
1144
1145
0
    if (!init_context.spnego)
1146
0
      return status;
1147
1148
0
    CopyMemory(init_context.mechTypes.pvBuffer, input_token.mechTypes.pvBuffer,
1149
0
               input_token.mechTypes.cbBuffer);
1150
1151
0
    if (!context->mech->preferred)
1152
0
    {
1153
0
      output_token.negState = REQUEST_MIC;
1154
0
      context->mic = TRUE;
1155
0
    }
1156
0
    else
1157
0
    {
1158
0
      output_token.negState = ACCEPT_INCOMPLETE;
1159
0
    }
1160
1161
0
    if (status == SEC_E_OK)
1162
0
      context->state = NEGOTIATE_STATE_FINAL;
1163
0
    else
1164
0
      context->state = NEGOTIATE_STATE_NEGORESP;
1165
1166
0
    output_token.supportedMech = oid;
1167
0
    WLog_DBG(TAG, "Accepted mechanism: %s", negotiate_mech_name(&output_token.supportedMech));
1168
0
  }
1169
0
  else
1170
0
  {
1171
0
    sub_cred = negotiate_FindCredential(creds, context->mech);
1172
0
    if (!sub_cred)
1173
0
      return SEC_E_NO_CREDENTIALS;
1174
1175
0
    if (!context->spnego)
1176
0
    {
1177
0
      return context->mech->pkg->table->AcceptSecurityContext(
1178
0
          sub_cred, &context->sub_context, pInput, fContextReq, TargetDataRep,
1179
0
          &context->sub_context, pOutput, pfContextAttr, ptsTimeStamp);
1180
0
    }
1181
1182
0
    if (!negotiate_read_neg_token(input_buffer, &input_token))
1183
0
      return SEC_E_INVALID_TOKEN;
1184
1185
    /* Process the mechanism token */
1186
0
    if (input_token.mechToken.cbBuffer > 0)
1187
0
    {
1188
0
      if (context->state != NEGOTIATE_STATE_NEGORESP)
1189
0
        return SEC_E_INVALID_TOKEN;
1190
1191
      /* Use the output buffer to store the optimistic token */
1192
0
      if (output_buffer)
1193
0
        CopyMemory(&output_token.mechToken, output_buffer, sizeof(SecBuffer));
1194
1195
0
      status = context->mech->pkg->table->AcceptSecurityContext(
1196
0
          sub_cred, &context->sub_context, &mech_input, fContextReq | context->mech->flags,
1197
0
          TargetDataRep, &context->sub_context, &mech_output, pfContextAttr, ptsTimeStamp);
1198
1199
0
      if (IsSecurityStatusError(status))
1200
0
        return status;
1201
1202
0
      if (status == SEC_E_OK)
1203
0
        context->state = NEGOTIATE_STATE_FINAL;
1204
0
    }
1205
0
    else if (context->state == NEGOTIATE_STATE_NEGORESP)
1206
0
      return SEC_E_INVALID_TOKEN;
1207
0
  }
1208
1209
0
  if (context->state == NEGOTIATE_STATE_FINAL)
1210
0
  {
1211
    /* Check if initiator sent the last mech token without a mic and a mic was required */
1212
0
    if (context->mic && output_token.mechToken.cbBuffer == 0 && input_token.mic.cbBuffer == 0)
1213
0
      return SEC_E_INVALID_TOKEN;
1214
1215
0
    if (context->mic || input_token.mic.cbBuffer > 0)
1216
0
    {
1217
0
      status = negotiate_mic_exchange(context, &input_token, &output_token, output_buffer);
1218
0
      if (status != SEC_E_OK)
1219
0
        return status;
1220
0
    }
1221
0
    else
1222
0
      output_token.negState = ACCEPT_COMPLETED;
1223
0
  }
1224
1225
0
  if (input_token.negState == ACCEPT_COMPLETED)
1226
0
  {
1227
0
    if (output_buffer)
1228
0
      output_buffer->cbBuffer = 0;
1229
0
    return SEC_E_OK;
1230
0
  }
1231
1232
0
  if (output_token.negState == ACCEPT_COMPLETED)
1233
0
    status = SEC_E_OK;
1234
0
  else
1235
0
    status = SEC_I_CONTINUE_NEEDED;
1236
1237
0
  if (!negotiate_write_neg_token(output_buffer, &output_token))
1238
0
    return SEC_E_INTERNAL_ERROR;
1239
1240
0
  return status;
1241
0
}
1242
1243
static SECURITY_STATUS SEC_ENTRY negotiate_CompleteAuthToken(PCtxtHandle phContext,
1244
                                                             PSecBufferDesc pToken)
1245
0
{
1246
0
  NEGOTIATE_CONTEXT* context = NULL;
1247
0
  SECURITY_STATUS status = SEC_E_OK;
1248
0
  context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
1249
1250
0
  if (!context)
1251
0
    return SEC_E_INVALID_HANDLE;
1252
1253
0
  WINPR_ASSERT(context->mech);
1254
0
  WINPR_ASSERT(context->mech->pkg);
1255
0
  WINPR_ASSERT(context->mech->pkg->table);
1256
0
  if (context->mech->pkg->table->CompleteAuthToken)
1257
0
    status = context->mech->pkg->table->CompleteAuthToken(&context->sub_context, pToken);
1258
1259
0
  return status;
1260
0
}
1261
1262
static SECURITY_STATUS SEC_ENTRY negotiate_DeleteSecurityContext(PCtxtHandle phContext)
1263
0
{
1264
0
  NEGOTIATE_CONTEXT* context = NULL;
1265
0
  SECURITY_STATUS status = SEC_E_OK;
1266
0
  context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
1267
0
  const SecPkg* pkg = NULL;
1268
1269
0
  if (!context)
1270
0
    return SEC_E_INVALID_HANDLE;
1271
1272
0
  WINPR_ASSERT(context->mech);
1273
0
  WINPR_ASSERT(context->mech->pkg);
1274
0
  WINPR_ASSERT(context->mech->pkg->table);
1275
0
  pkg = context->mech->pkg;
1276
1277
0
  if (pkg->table->DeleteSecurityContext)
1278
0
    status = pkg->table->DeleteSecurityContext(&context->sub_context);
1279
1280
0
  negotiate_ContextFree(context);
1281
0
  return status;
1282
0
}
1283
1284
static SECURITY_STATUS SEC_ENTRY
1285
negotiate_ImpersonateSecurityContext(WINPR_ATTR_UNUSED PCtxtHandle phContext)
1286
0
{
1287
0
  return SEC_E_OK;
1288
0
}
1289
1290
static SECURITY_STATUS SEC_ENTRY
1291
negotiate_RevertSecurityContext(WINPR_ATTR_UNUSED PCtxtHandle phContext)
1292
0
{
1293
0
  return SEC_E_OK;
1294
0
}
1295
1296
static SECURITY_STATUS SEC_ENTRY negotiate_QueryContextAttributesW(PCtxtHandle phContext,
1297
                                                                   ULONG ulAttribute, void* pBuffer)
1298
0
{
1299
0
  NEGOTIATE_CONTEXT* context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
1300
1301
0
  if (!context)
1302
0
    return SEC_E_INVALID_HANDLE;
1303
1304
0
  WINPR_ASSERT(context->mech);
1305
0
  WINPR_ASSERT(context->mech->pkg);
1306
0
  WINPR_ASSERT(context->mech->pkg->table_w);
1307
0
  if (context->mech->pkg->table_w->QueryContextAttributesW)
1308
0
    return context->mech->pkg->table_w->QueryContextAttributesW(&context->sub_context,
1309
0
                                                                ulAttribute, pBuffer);
1310
1311
0
  return SEC_E_UNSUPPORTED_FUNCTION;
1312
0
}
1313
1314
static SECURITY_STATUS SEC_ENTRY negotiate_QueryContextAttributesA(PCtxtHandle phContext,
1315
                                                                   ULONG ulAttribute, void* pBuffer)
1316
0
{
1317
0
  NEGOTIATE_CONTEXT* context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
1318
1319
0
  if (!context)
1320
0
    return SEC_E_INVALID_HANDLE;
1321
1322
0
  WINPR_ASSERT(context->mech);
1323
0
  WINPR_ASSERT(context->mech->pkg);
1324
0
  WINPR_ASSERT(context->mech->pkg->table);
1325
0
  if (context->mech->pkg->table->QueryContextAttributesA)
1326
0
    return context->mech->pkg->table->QueryContextAttributesA(&context->sub_context,
1327
0
                                                              ulAttribute, pBuffer);
1328
1329
0
  return SEC_E_UNSUPPORTED_FUNCTION;
1330
0
}
1331
1332
static SECURITY_STATUS SEC_ENTRY negotiate_SetContextAttributesW(PCtxtHandle phContext,
1333
                                                                 ULONG ulAttribute, void* pBuffer,
1334
                                                                 ULONG cbBuffer)
1335
0
{
1336
0
  NEGOTIATE_CONTEXT* context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
1337
1338
0
  if (!context)
1339
0
    return SEC_E_INVALID_HANDLE;
1340
1341
0
  WINPR_ASSERT(context->mech);
1342
0
  WINPR_ASSERT(context->mech->pkg);
1343
0
  WINPR_ASSERT(context->mech->pkg->table_w);
1344
0
  if (context->mech->pkg->table_w->SetContextAttributesW)
1345
0
    return context->mech->pkg->table_w->SetContextAttributesW(&context->sub_context,
1346
0
                                                              ulAttribute, pBuffer, cbBuffer);
1347
1348
0
  return SEC_E_UNSUPPORTED_FUNCTION;
1349
0
}
1350
1351
static SECURITY_STATUS SEC_ENTRY negotiate_SetContextAttributesA(PCtxtHandle phContext,
1352
                                                                 ULONG ulAttribute, void* pBuffer,
1353
                                                                 ULONG cbBuffer)
1354
0
{
1355
0
  NEGOTIATE_CONTEXT* context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
1356
1357
0
  if (!context)
1358
0
    return SEC_E_INVALID_HANDLE;
1359
1360
0
  WINPR_ASSERT(context->mech);
1361
0
  WINPR_ASSERT(context->mech->pkg);
1362
0
  WINPR_ASSERT(context->mech->pkg->table);
1363
0
  if (context->mech->pkg->table->SetContextAttributesA)
1364
0
    return context->mech->pkg->table->SetContextAttributesA(&context->sub_context, ulAttribute,
1365
0
                                                            pBuffer, cbBuffer);
1366
1367
0
  return SEC_E_UNSUPPORTED_FUNCTION;
1368
0
}
1369
1370
static SECURITY_STATUS SEC_ENTRY negotiate_SetCredentialsAttributesW(PCredHandle phCredential,
1371
                                                                     ULONG ulAttribute,
1372
                                                                     void* pBuffer, ULONG cbBuffer)
1373
0
{
1374
0
  MechCred* creds = NULL;
1375
0
  BOOL success = FALSE;
1376
0
  SECURITY_STATUS secStatus = 0;
1377
1378
0
  creds = sspi_SecureHandleGetLowerPointer(phCredential);
1379
1380
0
  if (!creds)
1381
0
    return SEC_E_INVALID_HANDLE;
1382
1383
0
  for (size_t i = 0; i < MECH_COUNT; i++)
1384
0
  {
1385
0
    MechCred* cred = &creds[i];
1386
1387
0
    WINPR_ASSERT(cred->mech);
1388
0
    WINPR_ASSERT(cred->mech->pkg);
1389
0
    WINPR_ASSERT(cred->mech->pkg->table);
1390
0
    WINPR_ASSERT(cred->mech->pkg->table_w->SetCredentialsAttributesW);
1391
0
    secStatus = cred->mech->pkg->table_w->SetCredentialsAttributesW(&cred->cred, ulAttribute,
1392
0
                                                                    pBuffer, cbBuffer);
1393
1394
0
    if (secStatus == SEC_E_OK)
1395
0
    {
1396
0
      success = TRUE;
1397
0
    }
1398
0
  }
1399
1400
  // return success if at least one submodule accepts the credential attribute
1401
0
  return (success ? SEC_E_OK : SEC_E_UNSUPPORTED_FUNCTION);
1402
0
}
1403
1404
static SECURITY_STATUS SEC_ENTRY negotiate_SetCredentialsAttributesA(PCredHandle phCredential,
1405
                                                                     ULONG ulAttribute,
1406
                                                                     void* pBuffer, ULONG cbBuffer)
1407
0
{
1408
0
  MechCred* creds = NULL;
1409
0
  BOOL success = FALSE;
1410
0
  SECURITY_STATUS secStatus = 0;
1411
1412
0
  creds = sspi_SecureHandleGetLowerPointer(phCredential);
1413
1414
0
  if (!creds)
1415
0
    return SEC_E_INVALID_HANDLE;
1416
1417
0
  for (size_t i = 0; i < MECH_COUNT; i++)
1418
0
  {
1419
0
    MechCred* cred = &creds[i];
1420
1421
0
    if (!cred->valid)
1422
0
      continue;
1423
1424
0
    WINPR_ASSERT(cred->mech);
1425
0
    WINPR_ASSERT(cred->mech->pkg);
1426
0
    WINPR_ASSERT(cred->mech->pkg->table);
1427
0
    WINPR_ASSERT(cred->mech->pkg->table->SetCredentialsAttributesA);
1428
0
    secStatus = cred->mech->pkg->table->SetCredentialsAttributesA(&cred->cred, ulAttribute,
1429
0
                                                                  pBuffer, cbBuffer);
1430
1431
0
    if (secStatus == SEC_E_OK)
1432
0
    {
1433
0
      success = TRUE;
1434
0
    }
1435
0
  }
1436
1437
  // return success if at least one submodule accepts the credential attribute
1438
0
  return (success ? SEC_E_OK : SEC_E_UNSUPPORTED_FUNCTION);
1439
0
}
1440
1441
static SECURITY_STATUS SEC_ENTRY negotiate_AcquireCredentialsHandleW(
1442
    SEC_WCHAR* pszPrincipal, SEC_WCHAR* pszPackage, ULONG fCredentialUse, void* pvLogonID,
1443
    void* pAuthData, SEC_GET_KEY_FN pGetKeyFn, void* pvGetKeyArgument, PCredHandle phCredential,
1444
    PTimeStamp ptsExpiry)
1445
0
{
1446
0
  BOOL kerberos = FALSE;
1447
0
  BOOL ntlm = FALSE;
1448
0
  BOOL u2u = FALSE;
1449
1450
0
  if (!negotiate_get_config(pAuthData, &kerberos, &ntlm, &u2u))
1451
0
    return SEC_E_INTERNAL_ERROR;
1452
1453
0
  MechCred* creds = calloc(MECH_COUNT, sizeof(MechCred));
1454
1455
0
  if (!creds)
1456
0
    return SEC_E_INTERNAL_ERROR;
1457
1458
0
  for (size_t i = 0; i < MECH_COUNT; i++)
1459
0
  {
1460
0
    MechCred* cred = &creds[i];
1461
0
    const SecPkg* pkg = MechTable[i].pkg;
1462
0
    cred->mech = &MechTable[i];
1463
1464
0
    if (!kerberos && sspi_gss_oid_compare(MechTable[i].oid, &kerberos_OID))
1465
0
      continue;
1466
0
    if (!u2u && sspi_gss_oid_compare(MechTable[i].oid, &kerberos_u2u_OID))
1467
0
      continue;
1468
0
    if (!ntlm && _tcsncmp(SecPkgTable[i].name, NTLM_SSP_NAME, ARRAYSIZE(NTLM_SSP_NAME)) == 0)
1469
0
      continue;
1470
1471
0
    WINPR_ASSERT(pkg->table_w);
1472
0
    WINPR_ASSERT(pkg->table_w->AcquireCredentialsHandleW);
1473
0
    if (pkg->table_w->AcquireCredentialsHandleW(
1474
0
            pszPrincipal, pszPackage, fCredentialUse, pvLogonID, pAuthData, pGetKeyFn,
1475
0
            pvGetKeyArgument, &cred->cred, ptsExpiry) != SEC_E_OK)
1476
0
      continue;
1477
1478
0
    cred->valid = TRUE;
1479
0
  }
1480
1481
0
  sspi_SecureHandleSetLowerPointer(phCredential, (void*)creds);
1482
0
  sspi_SecureHandleSetUpperPointer(phCredential, (void*)NEGO_SSP_NAME);
1483
0
  return SEC_E_OK;
1484
0
}
1485
1486
static SECURITY_STATUS SEC_ENTRY negotiate_AcquireCredentialsHandleA(
1487
    SEC_CHAR* pszPrincipal, SEC_CHAR* pszPackage, ULONG fCredentialUse, void* pvLogonID,
1488
    void* pAuthData, SEC_GET_KEY_FN pGetKeyFn, void* pvGetKeyArgument, PCredHandle phCredential,
1489
    PTimeStamp ptsExpiry)
1490
0
{
1491
0
  BOOL kerberos = FALSE;
1492
0
  BOOL ntlm = FALSE;
1493
0
  BOOL u2u = FALSE;
1494
1495
0
  if (!negotiate_get_config(pAuthData, &kerberos, &ntlm, &u2u))
1496
0
    return SEC_E_INTERNAL_ERROR;
1497
1498
0
  MechCred* creds = calloc(MECH_COUNT, sizeof(MechCred));
1499
1500
0
  if (!creds)
1501
0
    return SEC_E_INTERNAL_ERROR;
1502
1503
0
  for (size_t i = 0; i < MECH_COUNT; i++)
1504
0
  {
1505
0
    const SecPkg* pkg = MechTable[i].pkg;
1506
0
    MechCred* cred = &creds[i];
1507
1508
0
    cred->mech = &MechTable[i];
1509
1510
0
    if (!kerberos && sspi_gss_oid_compare(MechTable[i].oid, &kerberos_OID))
1511
0
      continue;
1512
0
    if (!u2u && sspi_gss_oid_compare(MechTable[i].oid, &kerberos_u2u_OID))
1513
0
      continue;
1514
0
    if (!ntlm && _tcsncmp(SecPkgTable[i].name, NTLM_SSP_NAME, ARRAYSIZE(NTLM_SSP_NAME)) == 0)
1515
0
      continue;
1516
1517
0
    WINPR_ASSERT(pkg->table);
1518
0
    WINPR_ASSERT(pkg->table->AcquireCredentialsHandleA);
1519
0
    if (pkg->table->AcquireCredentialsHandleA(pszPrincipal, pszPackage, fCredentialUse,
1520
0
                                              pvLogonID, pAuthData, pGetKeyFn, pvGetKeyArgument,
1521
0
                                              &cred->cred, ptsExpiry) != SEC_E_OK)
1522
0
      continue;
1523
1524
0
    cred->valid = TRUE;
1525
0
  }
1526
1527
0
  sspi_SecureHandleSetLowerPointer(phCredential, (void*)creds);
1528
0
  sspi_SecureHandleSetUpperPointer(phCredential, (void*)NEGO_SSP_NAME);
1529
0
  return SEC_E_OK;
1530
0
}
1531
1532
static SECURITY_STATUS SEC_ENTRY negotiate_QueryCredentialsAttributesW(
1533
    WINPR_ATTR_UNUSED PCredHandle phCredential, WINPR_ATTR_UNUSED ULONG ulAttribute,
1534
    WINPR_ATTR_UNUSED void* pBuffer)
1535
0
{
1536
0
  WLog_ERR(TAG, "TODO: Implement");
1537
0
  return SEC_E_UNSUPPORTED_FUNCTION;
1538
0
}
1539
1540
static SECURITY_STATUS SEC_ENTRY negotiate_QueryCredentialsAttributesA(
1541
    WINPR_ATTR_UNUSED PCredHandle phCredential, WINPR_ATTR_UNUSED ULONG ulAttribute,
1542
    WINPR_ATTR_UNUSED void* pBuffer)
1543
0
{
1544
0
  WLog_ERR(TAG, "TODO: Implement");
1545
0
  return SEC_E_UNSUPPORTED_FUNCTION;
1546
0
}
1547
1548
static SECURITY_STATUS SEC_ENTRY negotiate_FreeCredentialsHandle(PCredHandle phCredential)
1549
0
{
1550
0
  MechCred* creds = NULL;
1551
1552
0
  creds = sspi_SecureHandleGetLowerPointer(phCredential);
1553
0
  if (!creds)
1554
0
    return SEC_E_INVALID_HANDLE;
1555
1556
0
  for (size_t i = 0; i < MECH_COUNT; i++)
1557
0
  {
1558
0
    MechCred* cred = &creds[i];
1559
1560
0
    WINPR_ASSERT(cred->mech);
1561
0
    WINPR_ASSERT(cred->mech->pkg);
1562
0
    WINPR_ASSERT(cred->mech->pkg->table);
1563
0
    WINPR_ASSERT(cred->mech->pkg->table->FreeCredentialsHandle);
1564
0
    cred->mech->pkg->table->FreeCredentialsHandle(&cred->cred);
1565
0
  }
1566
0
  free(creds);
1567
1568
0
  return SEC_E_OK;
1569
0
}
1570
1571
static SECURITY_STATUS SEC_ENTRY negotiate_EncryptMessage(PCtxtHandle phContext, ULONG fQOP,
1572
                                                          PSecBufferDesc pMessage,
1573
                                                          ULONG MessageSeqNo)
1574
0
{
1575
0
  NEGOTIATE_CONTEXT* context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
1576
1577
0
  if (!context)
1578
0
    return SEC_E_INVALID_HANDLE;
1579
1580
0
  if (context->mic)
1581
0
    MessageSeqNo++;
1582
1583
0
  WINPR_ASSERT(context->mech);
1584
0
  WINPR_ASSERT(context->mech->pkg);
1585
0
  WINPR_ASSERT(context->mech->pkg->table);
1586
0
  if (context->mech->pkg->table->EncryptMessage)
1587
0
    return context->mech->pkg->table->EncryptMessage(&context->sub_context, fQOP, pMessage,
1588
0
                                                     MessageSeqNo);
1589
1590
0
  return SEC_E_UNSUPPORTED_FUNCTION;
1591
0
}
1592
1593
static SECURITY_STATUS SEC_ENTRY negotiate_DecryptMessage(PCtxtHandle phContext,
1594
                                                          PSecBufferDesc pMessage,
1595
                                                          ULONG MessageSeqNo, ULONG* pfQOP)
1596
0
{
1597
0
  NEGOTIATE_CONTEXT* context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
1598
1599
0
  if (!context)
1600
0
    return SEC_E_INVALID_HANDLE;
1601
1602
0
  if (context->mic)
1603
0
    MessageSeqNo++;
1604
1605
0
  WINPR_ASSERT(context->mech);
1606
0
  WINPR_ASSERT(context->mech->pkg);
1607
0
  WINPR_ASSERT(context->mech->pkg->table);
1608
0
  if (context->mech->pkg->table->DecryptMessage)
1609
0
    return context->mech->pkg->table->DecryptMessage(&context->sub_context, pMessage,
1610
0
                                                     MessageSeqNo, pfQOP);
1611
1612
0
  return SEC_E_UNSUPPORTED_FUNCTION;
1613
0
}
1614
1615
static SECURITY_STATUS SEC_ENTRY negotiate_MakeSignature(PCtxtHandle phContext, ULONG fQOP,
1616
                                                         PSecBufferDesc pMessage,
1617
                                                         ULONG MessageSeqNo)
1618
0
{
1619
0
  NEGOTIATE_CONTEXT* context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
1620
1621
0
  if (!context)
1622
0
    return SEC_E_INVALID_HANDLE;
1623
1624
0
  if (context->mic)
1625
0
    MessageSeqNo++;
1626
1627
0
  WINPR_ASSERT(context->mech);
1628
0
  WINPR_ASSERT(context->mech->pkg);
1629
0
  WINPR_ASSERT(context->mech->pkg->table);
1630
0
  if (context->mech->pkg->table->MakeSignature)
1631
0
    return context->mech->pkg->table->MakeSignature(&context->sub_context, fQOP, pMessage,
1632
0
                                                    MessageSeqNo);
1633
1634
0
  return SEC_E_UNSUPPORTED_FUNCTION;
1635
0
}
1636
1637
static SECURITY_STATUS SEC_ENTRY negotiate_VerifySignature(PCtxtHandle phContext,
1638
                                                           PSecBufferDesc pMessage,
1639
                                                           ULONG MessageSeqNo, ULONG* pfQOP)
1640
0
{
1641
0
  NEGOTIATE_CONTEXT* context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
1642
1643
0
  if (!context)
1644
0
    return SEC_E_INVALID_HANDLE;
1645
1646
0
  if (context->mic)
1647
0
    MessageSeqNo++;
1648
1649
0
  WINPR_ASSERT(context->mech);
1650
0
  WINPR_ASSERT(context->mech->pkg);
1651
0
  WINPR_ASSERT(context->mech->pkg->table);
1652
0
  if (context->mech->pkg->table->VerifySignature)
1653
0
    return context->mech->pkg->table->VerifySignature(&context->sub_context, pMessage,
1654
0
                                                      MessageSeqNo, pfQOP);
1655
1656
0
  return SEC_E_UNSUPPORTED_FUNCTION;
1657
0
}
1658
1659
const SecurityFunctionTableA NEGOTIATE_SecurityFunctionTableA = {
1660
  3,                                     /* dwVersion */
1661
  NULL,                                  /* EnumerateSecurityPackages */
1662
  negotiate_QueryCredentialsAttributesA, /* QueryCredentialsAttributes */
1663
  negotiate_AcquireCredentialsHandleA,   /* AcquireCredentialsHandle */
1664
  negotiate_FreeCredentialsHandle,       /* FreeCredentialsHandle */
1665
  NULL,                                  /* Reserved2 */
1666
  negotiate_InitializeSecurityContextA,  /* InitializeSecurityContext */
1667
  negotiate_AcceptSecurityContext,       /* AcceptSecurityContext */
1668
  negotiate_CompleteAuthToken,           /* CompleteAuthToken */
1669
  negotiate_DeleteSecurityContext,       /* DeleteSecurityContext */
1670
  NULL,                                  /* ApplyControlToken */
1671
  negotiate_QueryContextAttributesA,     /* QueryContextAttributes */
1672
  negotiate_ImpersonateSecurityContext,  /* ImpersonateSecurityContext */
1673
  negotiate_RevertSecurityContext,       /* RevertSecurityContext */
1674
  negotiate_MakeSignature,               /* MakeSignature */
1675
  negotiate_VerifySignature,             /* VerifySignature */
1676
  NULL,                                  /* FreeContextBuffer */
1677
  NULL,                                  /* QuerySecurityPackageInfo */
1678
  NULL,                                  /* Reserved3 */
1679
  NULL,                                  /* Reserved4 */
1680
  NULL,                                  /* ExportSecurityContext */
1681
  NULL,                                  /* ImportSecurityContext */
1682
  NULL,                                  /* AddCredentials */
1683
  NULL,                                  /* Reserved8 */
1684
  NULL,                                  /* QuerySecurityContextToken */
1685
  negotiate_EncryptMessage,              /* EncryptMessage */
1686
  negotiate_DecryptMessage,              /* DecryptMessage */
1687
  negotiate_SetContextAttributesA,       /* SetContextAttributes */
1688
  negotiate_SetCredentialsAttributesA,   /* SetCredentialsAttributes */
1689
};
1690
1691
const SecurityFunctionTableW NEGOTIATE_SecurityFunctionTableW = {
1692
  3,                                     /* dwVersion */
1693
  NULL,                                  /* EnumerateSecurityPackages */
1694
  negotiate_QueryCredentialsAttributesW, /* QueryCredentialsAttributes */
1695
  negotiate_AcquireCredentialsHandleW,   /* AcquireCredentialsHandle */
1696
  negotiate_FreeCredentialsHandle,       /* FreeCredentialsHandle */
1697
  NULL,                                  /* Reserved2 */
1698
  negotiate_InitializeSecurityContextW,  /* InitializeSecurityContext */
1699
  negotiate_AcceptSecurityContext,       /* AcceptSecurityContext */
1700
  negotiate_CompleteAuthToken,           /* CompleteAuthToken */
1701
  negotiate_DeleteSecurityContext,       /* DeleteSecurityContext */
1702
  NULL,                                  /* ApplyControlToken */
1703
  negotiate_QueryContextAttributesW,     /* QueryContextAttributes */
1704
  negotiate_ImpersonateSecurityContext,  /* ImpersonateSecurityContext */
1705
  negotiate_RevertSecurityContext,       /* RevertSecurityContext */
1706
  negotiate_MakeSignature,               /* MakeSignature */
1707
  negotiate_VerifySignature,             /* VerifySignature */
1708
  NULL,                                  /* FreeContextBuffer */
1709
  NULL,                                  /* QuerySecurityPackageInfo */
1710
  NULL,                                  /* Reserved3 */
1711
  NULL,                                  /* Reserved4 */
1712
  NULL,                                  /* ExportSecurityContext */
1713
  NULL,                                  /* ImportSecurityContext */
1714
  NULL,                                  /* AddCredentials */
1715
  NULL,                                  /* Reserved8 */
1716
  NULL,                                  /* QuerySecurityContextToken */
1717
  negotiate_EncryptMessage,              /* EncryptMessage */
1718
  negotiate_DecryptMessage,              /* DecryptMessage */
1719
  negotiate_SetContextAttributesW,       /* SetContextAttributes */
1720
  negotiate_SetCredentialsAttributesW,   /* SetCredentialsAttributes */
1721
};
1722
1723
BOOL NEGOTIATE_init(void)
1724
0
{
1725
0
  InitializeConstWCharFromUtf8(NEGOTIATE_SecPkgInfoA.Name, NEGOTIATE_SecPkgInfoW_NameBuffer,
1726
0
                               ARRAYSIZE(NEGOTIATE_SecPkgInfoW_NameBuffer));
1727
0
  InitializeConstWCharFromUtf8(NEGOTIATE_SecPkgInfoA.Comment, NEGOTIATE_SecPkgInfoW_CommentBuffer,
1728
0
                               ARRAYSIZE(NEGOTIATE_SecPkgInfoW_CommentBuffer));
1729
1730
  return TRUE;
1731
0
}