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