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