Blame winpr/libwinpr/sspi/Negotiate/negotiate.c

Packit 1fb8d4
/**
Packit 1fb8d4
 * WinPR: Windows Portable Runtime
Packit 1fb8d4
 * Negotiate Security Package
Packit 1fb8d4
 *
Packit 1fb8d4
 * Copyright 2011-2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
Packit 1fb8d4
 * Copyright 2017 Dorian Ducournau <dorian.ducournau@gmail.com>
Packit 1fb8d4
 *
Packit 1fb8d4
 * Licensed under the Apache License, Version 2.0 (the "License");
Packit 1fb8d4
 * you may not use this file except in compliance with the License.
Packit 1fb8d4
 * You may obtain a copy of the License at
Packit 1fb8d4
 *
Packit 1fb8d4
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 1fb8d4
 *
Packit 1fb8d4
 * Unless required by applicable law or agreed to in writing, software
Packit 1fb8d4
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 1fb8d4
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 1fb8d4
 * See the License for the specific language governing permissions and
Packit 1fb8d4
 * limitations under the License.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#ifdef HAVE_CONFIG_H
Packit 1fb8d4
#include "config.h"
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
#include <winpr/crt.h>
Packit 1fb8d4
#include <winpr/sspi.h>
Packit 1fb8d4
#include <winpr/tchar.h>
Packit 1fb8d4
Packit 1fb8d4
#include "negotiate.h"
Packit 1fb8d4
Packit 1fb8d4
#include "../sspi.h"
Packit 1fb8d4
#include "../log.h"
Packit 1fb8d4
#define TAG WINPR_TAG("negotiate")
Packit 1fb8d4
Packit 1fb8d4
extern const SecurityFunctionTableA NTLM_SecurityFunctionTableA;
Packit 1fb8d4
extern const SecurityFunctionTableW NTLM_SecurityFunctionTableW;
Packit 1fb8d4
Packit 1fb8d4
extern const SecurityFunctionTableA KERBEROS_SecurityFunctionTableA;
Packit 1fb8d4
extern const SecurityFunctionTableW KERBEROS_SecurityFunctionTableW;
Packit 1fb8d4
Packit 1fb8d4
#ifdef WITH_GSSAPI
Packit 1fb8d4
static BOOL ErrorInitContextKerberos = FALSE;
Packit 1fb8d4
#else
Packit 1fb8d4
static BOOL ErrorInitContextKerberos = TRUE;
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit Service 5a9772
const SecPkgInfoA NEGOTIATE_SecPkgInfoA = {
Packit Service 5a9772
	0x00083BB3,                    /* fCapabilities */
Packit Service 5a9772
	1,                             /* wVersion */
Packit Service 5a9772
	0x0009,                        /* wRPCID */
Packit Service 5a9772
	0x00002FE0,                    /* cbMaxToken */
Packit Service 5a9772
	"Negotiate",                   /* Name */
Packit 1fb8d4
	"Microsoft Package Negotiator" /* Comment */
Packit 1fb8d4
};
Packit 1fb8d4
Packit 1fb8d4
static WCHAR NEGOTIATE_SecPkgInfoW_Name[] = { 'N', 'e', 'g', 'o', 't', 'i', 'a', 't', 'e', '\0' };
Packit 1fb8d4
Packit Service 5a9772
static WCHAR NEGOTIATE_SecPkgInfoW_Comment[] = { 'M', 'i', 'c', 'r', 'o', 's', 'o', 'f', 't', ' ',
Packit Service 5a9772
	                                             'P', 'a', 'c', 'k', 'a', 'g', 'e', ' ', 'N', 'e',
Packit Service 5a9772
	                                             'g', 'o', 't', 'i', 'a', 't', 'o', 'r', '\0' };
Packit 1fb8d4
Packit Service 5a9772
const SecPkgInfoW NEGOTIATE_SecPkgInfoW = {
Packit Service 5a9772
	0x00083BB3,                   /* fCapabilities */
Packit Service 5a9772
	1,                            /* wVersion */
Packit Service 5a9772
	0x0009,                       /* wRPCID */
Packit Service 5a9772
	0x00002FE0,                   /* cbMaxToken */
Packit Service 5a9772
	NEGOTIATE_SecPkgInfoW_Name,   /* Name */
Packit 1fb8d4
	NEGOTIATE_SecPkgInfoW_Comment /* Comment */
Packit 1fb8d4
};
Packit 1fb8d4
Packit 1fb8d4
static void negotiate_SetSubPackage(NEGOTIATE_CONTEXT* context, const TCHAR* name)
Packit 1fb8d4
{
Packit 1fb8d4
	if (_tcsnccmp(name, KERBEROS_SSP_NAME, ARRAYSIZE(KERBEROS_SSP_NAME)) == 0)
Packit 1fb8d4
	{
Packit Service 5a9772
		context->sspiA = (SecurityFunctionTableA*)&KERBEROS_SecurityFunctionTableA;
Packit Service 5a9772
		context->sspiW = (SecurityFunctionTableW*)&KERBEROS_SecurityFunctionTableW;
Packit 1fb8d4
		context->kerberos = TRUE;
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit Service 5a9772
		context->sspiA = (SecurityFunctionTableA*)&NTLM_SecurityFunctionTableA;
Packit Service 5a9772
		context->sspiW = (SecurityFunctionTableW*)&NTLM_SecurityFunctionTableW;
Packit 1fb8d4
		context->kerberos = FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static NEGOTIATE_CONTEXT* negotiate_ContextNew(void)
Packit 1fb8d4
{
Packit 1fb8d4
	NEGOTIATE_CONTEXT* context;
Packit Service 5a9772
	context = (NEGOTIATE_CONTEXT*)calloc(1, sizeof(NEGOTIATE_CONTEXT));
Packit 1fb8d4
Packit 1fb8d4
	if (!context)
Packit 1fb8d4
		return NULL;
Packit 1fb8d4
Packit 1fb8d4
	context->NegotiateFlags = 0;
Packit 1fb8d4
	context->state = NEGOTIATE_STATE_INITIAL;
Packit 1fb8d4
	SecInvalidateHandle(&(context->SubContext));
Packit 1fb8d4
	negotiate_SetSubPackage(context, KERBEROS_SSP_NAME);
Packit 1fb8d4
	return context;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static void negotiate_ContextFree(NEGOTIATE_CONTEXT* context)
Packit 1fb8d4
{
Packit 1fb8d4
	free(context);
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static SECURITY_STATUS SEC_ENTRY negotiate_InitializeSecurityContextW(
Packit Service 5a9772
    PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR* pszTargetName, ULONG fContextReq,
Packit Service 5a9772
    ULONG Reserved1, ULONG TargetDataRep, PSecBufferDesc pInput, ULONG Reserved2,
Packit Service 5a9772
    PCtxtHandle phNewContext, PSecBufferDesc pOutput, PULONG pfContextAttr, PTimeStamp ptsExpiry)
Packit 1fb8d4
{
Packit 1fb8d4
	SECURITY_STATUS status;
Packit 1fb8d4
	NEGOTIATE_CONTEXT* context;
Packit Service 5a9772
	context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
Packit 1fb8d4
Packit 1fb8d4
	if (!context)
Packit 1fb8d4
	{
Packit 1fb8d4
		context = negotiate_ContextNew();
Packit 1fb8d4
Packit 1fb8d4
		if (!context)
Packit 1fb8d4
			return SEC_E_INTERNAL_ERROR;
Packit 1fb8d4
Packit 1fb8d4
		sspi_SecureHandleSetLowerPointer(phNewContext, context);
Packit Service 5a9772
		sspi_SecureHandleSetUpperPointer(phNewContext, (void*)NEGO_SSP_NAME);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* if Kerberos has previously failed or WITH_GSSAPI is not defined, we use NTLM directly */
Packit 1fb8d4
	if (ErrorInitContextKerberos == FALSE)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (!pInput)
Packit 1fb8d4
		{
Packit 1fb8d4
			negotiate_SetSubPackage(context, KERBEROS_SSP_NAME);
Packit 1fb8d4
		}
Packit 1fb8d4
Packit Service 5a9772
		status = context->sspiW->InitializeSecurityContextW(
Packit Service 5a9772
		    phCredential, &(context->SubContext), pszTargetName, fContextReq, Reserved1,
Packit Service 5a9772
		    TargetDataRep, pInput, Reserved2, &(context->SubContext), pOutput, pfContextAttr,
Packit Service 5a9772
		    ptsExpiry);
Packit 1fb8d4
Packit 1fb8d4
		if (status == SEC_E_NO_CREDENTIALS)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_WARN(TAG, "No Kerberos credentials. Retry with NTLM");
Packit 1fb8d4
			ErrorInitContextKerberos = TRUE;
Packit 1fb8d4
			context->sspiA->DeleteSecurityContext(&(context->SubContext));
Packit 1fb8d4
			negotiate_ContextFree(context);
Packit 1fb8d4
			return status;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
		if (!pInput)
Packit 1fb8d4
		{
Packit 1fb8d4
			context->sspiA->DeleteSecurityContext(&(context->SubContext));
Packit 1fb8d4
			negotiate_SetSubPackage(context, NTLM_SSP_NAME);
Packit 1fb8d4
		}
Packit 1fb8d4
Packit Service 5a9772
		status = context->sspiW->InitializeSecurityContextW(
Packit Service 5a9772
		    phCredential, &(context->SubContext), pszTargetName, fContextReq, Reserved1,
Packit Service 5a9772
		    TargetDataRep, pInput, Reserved2, &(context->SubContext), pOutput, pfContextAttr,
Packit Service 5a9772
		    ptsExpiry);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static SECURITY_STATUS SEC_ENTRY negotiate_InitializeSecurityContextA(
Packit Service 5a9772
    PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR* pszTargetName, ULONG fContextReq,
Packit Service 5a9772
    ULONG Reserved1, ULONG TargetDataRep, PSecBufferDesc pInput, ULONG Reserved2,
Packit Service 5a9772
    PCtxtHandle phNewContext, PSecBufferDesc pOutput, PULONG pfContextAttr, PTimeStamp ptsExpiry)
Packit 1fb8d4
{
Packit 1fb8d4
	SECURITY_STATUS status;
Packit 1fb8d4
	NEGOTIATE_CONTEXT* context;
Packit Service 5a9772
	context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
Packit 1fb8d4
Packit 1fb8d4
	if (!context)
Packit 1fb8d4
	{
Packit 1fb8d4
		context = negotiate_ContextNew();
Packit 1fb8d4
Packit 1fb8d4
		if (!context)
Packit 1fb8d4
			return SEC_E_INTERNAL_ERROR;
Packit 1fb8d4
Packit 1fb8d4
		sspi_SecureHandleSetLowerPointer(phNewContext, context);
Packit Service 5a9772
		sspi_SecureHandleSetUpperPointer(phNewContext, (void*)NEGO_SSP_NAME);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* if Kerberos has previously failed or WITH_GSSAPI is not defined, we use NTLM directly */
Packit 1fb8d4
	if (ErrorInitContextKerberos == FALSE)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (!pInput)
Packit 1fb8d4
		{
Packit 1fb8d4
			negotiate_SetSubPackage(context, KERBEROS_SSP_NAME);
Packit 1fb8d4
		}
Packit 1fb8d4
Packit Service 5a9772
		status = context->sspiA->InitializeSecurityContextA(
Packit Service 5a9772
		    phCredential, &(context->SubContext), pszTargetName, fContextReq, Reserved1,
Packit Service 5a9772
		    TargetDataRep, pInput, Reserved2, &(context->SubContext), pOutput, pfContextAttr,
Packit Service 5a9772
		    ptsExpiry);
Packit 1fb8d4
Packit 1fb8d4
		if (status == SEC_E_NO_CREDENTIALS)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_WARN(TAG, "No Kerberos credentials. Retry with NTLM");
Packit 1fb8d4
			ErrorInitContextKerberos = TRUE;
Packit 1fb8d4
			context->sspiA->DeleteSecurityContext(&(context->SubContext));
Packit 1fb8d4
			negotiate_ContextFree(context);
Packit 1fb8d4
			return status;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
		if (!pInput)
Packit 1fb8d4
		{
Packit 1fb8d4
			context->sspiA->DeleteSecurityContext(&(context->SubContext));
Packit 1fb8d4
			negotiate_SetSubPackage(context, NTLM_SSP_NAME);
Packit 1fb8d4
		}
Packit 1fb8d4
Packit Service 5a9772
		status = context->sspiA->InitializeSecurityContextA(
Packit Service 5a9772
		    phCredential, &(context->SubContext), pszTargetName, fContextReq, Reserved1,
Packit Service 5a9772
		    TargetDataRep, pInput, Reserved2, &(context->SubContext), pOutput, pfContextAttr,
Packit Service 5a9772
		    ptsExpiry);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static SECURITY_STATUS SEC_ENTRY negotiate_AcceptSecurityContext(
Packit Service 5a9772
    PCredHandle phCredential, PCtxtHandle phContext, PSecBufferDesc pInput, ULONG fContextReq,
Packit Service 5a9772
    ULONG TargetDataRep, PCtxtHandle phNewContext, PSecBufferDesc pOutput, PULONG pfContextAttr,
Packit Service 5a9772
    PTimeStamp ptsTimeStamp)
Packit 1fb8d4
{
Packit 1fb8d4
	SECURITY_STATUS status;
Packit 1fb8d4
	NEGOTIATE_CONTEXT* context;
Packit Service 5a9772
	context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
Packit 1fb8d4
Packit 1fb8d4
	if (!context)
Packit 1fb8d4
	{
Packit 1fb8d4
		context = negotiate_ContextNew();
Packit 1fb8d4
Packit 1fb8d4
		if (!context)
Packit 1fb8d4
			return SEC_E_INTERNAL_ERROR;
Packit 1fb8d4
Packit 1fb8d4
		sspi_SecureHandleSetLowerPointer(phNewContext, context);
Packit Service 5a9772
		sspi_SecureHandleSetUpperPointer(phNewContext, (void*)NEGO_SSP_NAME);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	negotiate_SetSubPackage(context, NTLM_SSP_NAME); /* server-side Kerberos not yet implemented */
Packit Service 5a9772
	status = context->sspiA->AcceptSecurityContext(
Packit Service 5a9772
	    phCredential, &(context->SubContext), pInput, fContextReq, TargetDataRep,
Packit Service 5a9772
	    &(context->SubContext), pOutput, pfContextAttr, ptsTimeStamp);
Packit 1fb8d4
Packit 1fb8d4
	if (status != SEC_E_OK)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_WARN(TAG, "AcceptSecurityContext status %s [0x%08" PRIX32 "]",
Packit 1fb8d4
		          GetSecurityStatusString(status), status);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static SECURITY_STATUS SEC_ENTRY negotiate_CompleteAuthToken(PCtxtHandle phContext,
Packit Service 5a9772
                                                             PSecBufferDesc pToken)
Packit 1fb8d4
{
Packit 1fb8d4
	NEGOTIATE_CONTEXT* context;
Packit 1fb8d4
	SECURITY_STATUS status = SEC_E_OK;
Packit Service 5a9772
	context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
Packit 1fb8d4
Packit 1fb8d4
	if (!context)
Packit 1fb8d4
		return SEC_E_INVALID_HANDLE;
Packit 1fb8d4
Packit 1fb8d4
	if (context->sspiW->CompleteAuthToken)
Packit 1fb8d4
		status = context->sspiW->CompleteAuthToken(&(context->SubContext), pToken);
Packit 1fb8d4
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static SECURITY_STATUS SEC_ENTRY negotiate_DeleteSecurityContext(PCtxtHandle phContext)
Packit 1fb8d4
{
Packit 1fb8d4
	NEGOTIATE_CONTEXT* context;
Packit 1fb8d4
	SECURITY_STATUS status = SEC_E_OK;
Packit Service 5a9772
	context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
Packit 1fb8d4
Packit 1fb8d4
	if (!context)
Packit 1fb8d4
		return SEC_E_INVALID_HANDLE;
Packit 1fb8d4
Packit 1fb8d4
	if (context->sspiW->DeleteSecurityContext)
Packit 1fb8d4
		status = context->sspiW->DeleteSecurityContext(&(context->SubContext));
Packit 1fb8d4
Packit 1fb8d4
	negotiate_ContextFree(context);
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static SECURITY_STATUS SEC_ENTRY negotiate_ImpersonateSecurityContext(PCtxtHandle phContext)
Packit 1fb8d4
{
Packit 1fb8d4
	NEGOTIATE_CONTEXT* context;
Packit 1fb8d4
	SECURITY_STATUS status = SEC_E_OK;
Packit Service 5a9772
	context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
Packit 1fb8d4
Packit 1fb8d4
	if (!phContext)
Packit 1fb8d4
		return SEC_E_INVALID_HANDLE;
Packit 1fb8d4
Packit 1fb8d4
	if (context->sspiW->ImpersonateSecurityContext)
Packit 1fb8d4
		status = context->sspiW->ImpersonateSecurityContext(&(context->SubContext));
Packit 1fb8d4
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static SECURITY_STATUS SEC_ENTRY negotiate_RevertSecurityContext(PCtxtHandle phContext)
Packit 1fb8d4
{
Packit 1fb8d4
	NEGOTIATE_CONTEXT* context;
Packit 1fb8d4
	SECURITY_STATUS status = SEC_E_OK;
Packit Service 5a9772
	context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
Packit 1fb8d4
Packit 1fb8d4
	if (!phContext)
Packit 1fb8d4
		return SEC_E_INVALID_HANDLE;
Packit 1fb8d4
Packit 1fb8d4
	if (context->sspiW->RevertSecurityContext)
Packit 1fb8d4
		status = context->sspiW->RevertSecurityContext(&(context->SubContext));
Packit 1fb8d4
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static SECURITY_STATUS SEC_ENTRY negotiate_QueryContextAttributesW(PCtxtHandle phContext,
Packit Service 5a9772
                                                                   ULONG ulAttribute, void* pBuffer)
Packit 1fb8d4
{
Packit 1fb8d4
	NEGOTIATE_CONTEXT* context;
Packit 1fb8d4
	SECURITY_STATUS status = SEC_E_OK;
Packit Service 5a9772
	context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
Packit 1fb8d4
Packit 1fb8d4
	if (!phContext)
Packit 1fb8d4
		return SEC_E_INVALID_HANDLE;
Packit 1fb8d4
Packit 1fb8d4
	if (!pBuffer)
Packit 1fb8d4
		return SEC_E_INSUFFICIENT_MEMORY;
Packit 1fb8d4
Packit 1fb8d4
	if (context->sspiW->QueryContextAttributesW)
Packit Service 5a9772
		status =
Packit Service 5a9772
		    context->sspiW->QueryContextAttributesW(&(context->SubContext), ulAttribute, pBuffer);
Packit 1fb8d4
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static SECURITY_STATUS SEC_ENTRY negotiate_QueryContextAttributesA(PCtxtHandle phContext,
Packit Service 5a9772
                                                                   ULONG ulAttribute, void* pBuffer)
Packit 1fb8d4
{
Packit 1fb8d4
	NEGOTIATE_CONTEXT* context;
Packit 1fb8d4
	SECURITY_STATUS status = SEC_E_OK;
Packit Service 5a9772
	context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
Packit 1fb8d4
Packit 1fb8d4
	if (!phContext)
Packit 1fb8d4
		return SEC_E_INVALID_HANDLE;
Packit 1fb8d4
Packit 1fb8d4
	if (!pBuffer)
Packit 1fb8d4
		return SEC_E_INSUFFICIENT_MEMORY;
Packit 1fb8d4
Packit 1fb8d4
	if (context->sspiA->QueryContextAttributesA)
Packit Service 5a9772
		status =
Packit Service 5a9772
		    context->sspiA->QueryContextAttributesA(&(context->SubContext), ulAttribute, pBuffer);
Packit 1fb8d4
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static SECURITY_STATUS SEC_ENTRY negotiate_SetContextAttributesW(PCtxtHandle phContext,
Packit Service 5a9772
                                                                 ULONG ulAttribute, void* pBuffer,
Packit Service 5a9772
                                                                 ULONG cbBuffer)
Packit 1fb8d4
{
Packit 1fb8d4
	NEGOTIATE_CONTEXT* context;
Packit 1fb8d4
	SECURITY_STATUS status = SEC_E_OK;
Packit Service 5a9772
	context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
Packit 1fb8d4
Packit 1fb8d4
	if (!phContext)
Packit 1fb8d4
		return SEC_E_INVALID_HANDLE;
Packit 1fb8d4
Packit 1fb8d4
	if (!pBuffer)
Packit 1fb8d4
		return SEC_E_INSUFFICIENT_MEMORY;
Packit 1fb8d4
Packit 1fb8d4
	if (context->sspiW->SetContextAttributesW)
Packit 1fb8d4
		status = context->sspiW->SetContextAttributesW(&(context->SubContext), ulAttribute, pBuffer,
Packit Service 5a9772
		                                               cbBuffer);
Packit 1fb8d4
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static SECURITY_STATUS SEC_ENTRY negotiate_SetContextAttributesA(PCtxtHandle phContext,
Packit Service 5a9772
                                                                 ULONG ulAttribute, void* pBuffer,
Packit Service 5a9772
                                                                 ULONG cbBuffer)
Packit 1fb8d4
{
Packit 1fb8d4
	NEGOTIATE_CONTEXT* context;
Packit 1fb8d4
	SECURITY_STATUS status = SEC_E_OK;
Packit Service 5a9772
	context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
Packit 1fb8d4
Packit 1fb8d4
	if (!phContext)
Packit 1fb8d4
		return SEC_E_INVALID_HANDLE;
Packit 1fb8d4
Packit 1fb8d4
	if (!pBuffer)
Packit 1fb8d4
		return SEC_E_INSUFFICIENT_MEMORY;
Packit 1fb8d4
Packit 1fb8d4
	if (context->sspiA->SetContextAttributesA)
Packit 1fb8d4
		status = context->sspiA->SetContextAttributesA(&(context->SubContext), ulAttribute, pBuffer,
Packit Service 5a9772
		                                               cbBuffer);
Packit 1fb8d4
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static SECURITY_STATUS SEC_ENTRY negotiate_AcquireCredentialsHandleW(
Packit Service 5a9772
    SEC_WCHAR* pszPrincipal, SEC_WCHAR* pszPackage, ULONG fCredentialUse, void* pvLogonID,
Packit Service 5a9772
    void* pAuthData, SEC_GET_KEY_FN pGetKeyFn, void* pvGetKeyArgument, PCredHandle phCredential,
Packit Service 5a9772
    PTimeStamp ptsExpiry)
Packit 1fb8d4
{
Packit 1fb8d4
	SSPI_CREDENTIALS* credentials;
Packit 1fb8d4
	SEC_WINNT_AUTH_IDENTITY* identity;
Packit 1fb8d4
Packit Service 5a9772
	if ((fCredentialUse != SECPKG_CRED_OUTBOUND) && (fCredentialUse != SECPKG_CRED_INBOUND) &&
Packit 1fb8d4
	    (fCredentialUse != SECPKG_CRED_BOTH))
Packit 1fb8d4
	{
Packit 1fb8d4
		return SEC_E_INVALID_PARAMETER;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	credentials = sspi_CredentialsNew();
Packit 1fb8d4
Packit 1fb8d4
	if (!credentials)
Packit 1fb8d4
		return SEC_E_INTERNAL_ERROR;
Packit 1fb8d4
Packit 1fb8d4
	credentials->fCredentialUse = fCredentialUse;
Packit 1fb8d4
	credentials->pGetKeyFn = pGetKeyFn;
Packit 1fb8d4
	credentials->pvGetKeyArgument = pvGetKeyArgument;
Packit Service 5a9772
	identity = (SEC_WINNT_AUTH_IDENTITY*)pAuthData;
Packit 1fb8d4
Packit 1fb8d4
	if (identity)
Packit 1fb8d4
		sspi_CopyAuthIdentity(&(credentials->identity), identity);
Packit 1fb8d4
Packit Service 5a9772
	sspi_SecureHandleSetLowerPointer(phCredential, (void*)credentials);
Packit Service 5a9772
	sspi_SecureHandleSetUpperPointer(phCredential, (void*)NEGO_SSP_NAME);
Packit 1fb8d4
	return SEC_E_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static SECURITY_STATUS SEC_ENTRY negotiate_AcquireCredentialsHandleA(
Packit Service 5a9772
    SEC_CHAR* pszPrincipal, SEC_CHAR* pszPackage, ULONG fCredentialUse, void* pvLogonID,
Packit Service 5a9772
    void* pAuthData, SEC_GET_KEY_FN pGetKeyFn, void* pvGetKeyArgument, PCredHandle phCredential,
Packit Service 5a9772
    PTimeStamp ptsExpiry)
Packit 1fb8d4
{
Packit 1fb8d4
	SSPI_CREDENTIALS* credentials;
Packit 1fb8d4
	SEC_WINNT_AUTH_IDENTITY* identity;
Packit 1fb8d4
Packit Service 5a9772
	if ((fCredentialUse != SECPKG_CRED_OUTBOUND) && (fCredentialUse != SECPKG_CRED_INBOUND) &&
Packit 1fb8d4
	    (fCredentialUse != SECPKG_CRED_BOTH))
Packit 1fb8d4
	{
Packit 1fb8d4
		return SEC_E_INVALID_PARAMETER;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	credentials = sspi_CredentialsNew();
Packit 1fb8d4
Packit 1fb8d4
	if (!credentials)
Packit 1fb8d4
		return SEC_E_INTERNAL_ERROR;
Packit 1fb8d4
Packit 1fb8d4
	credentials->fCredentialUse = fCredentialUse;
Packit 1fb8d4
	credentials->pGetKeyFn = pGetKeyFn;
Packit 1fb8d4
	credentials->pvGetKeyArgument = pvGetKeyArgument;
Packit Service 5a9772
	identity = (SEC_WINNT_AUTH_IDENTITY*)pAuthData;
Packit 1fb8d4
Packit 1fb8d4
	if (identity)
Packit 1fb8d4
		sspi_CopyAuthIdentity(&(credentials->identity), identity);
Packit 1fb8d4
Packit Service 5a9772
	sspi_SecureHandleSetLowerPointer(phCredential, (void*)credentials);
Packit Service 5a9772
	sspi_SecureHandleSetUpperPointer(phCredential, (void*)NEGO_SSP_NAME);
Packit 1fb8d4
	return SEC_E_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static SECURITY_STATUS SEC_ENTRY negotiate_QueryCredentialsAttributesW(PCredHandle phCredential,
Packit Service 5a9772
                                                                       ULONG ulAttribute,
Packit Service 5a9772
                                                                       void* pBuffer)
Packit 1fb8d4
{
Packit 1fb8d4
	return SEC_E_UNSUPPORTED_FUNCTION;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static SECURITY_STATUS SEC_ENTRY negotiate_QueryCredentialsAttributesA(PCredHandle phCredential,
Packit Service 5a9772
                                                                       ULONG ulAttribute,
Packit Service 5a9772
                                                                       void* pBuffer)
Packit 1fb8d4
{
Packit 1fb8d4
	return SEC_E_UNSUPPORTED_FUNCTION;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static SECURITY_STATUS SEC_ENTRY negotiate_FreeCredentialsHandle(PCredHandle phCredential)
Packit 1fb8d4
{
Packit 1fb8d4
	SSPI_CREDENTIALS* credentials;
Packit 1fb8d4
Packit 1fb8d4
	if (!phCredential)
Packit 1fb8d4
		return SEC_E_INVALID_HANDLE;
Packit 1fb8d4
Packit Service 5a9772
	credentials = (SSPI_CREDENTIALS*)sspi_SecureHandleGetLowerPointer(phCredential);
Packit 1fb8d4
Packit 1fb8d4
	if (!credentials)
Packit 1fb8d4
		return SEC_E_INVALID_HANDLE;
Packit 1fb8d4
Packit 1fb8d4
	sspi_CredentialsFree(credentials);
Packit 1fb8d4
	sspi_SecureHandleInvalidate(phCredential);
Packit 1fb8d4
	return SEC_E_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static SECURITY_STATUS SEC_ENTRY negotiate_EncryptMessage(PCtxtHandle phContext, ULONG fQOP,
Packit Service 5a9772
                                                          PSecBufferDesc pMessage,
Packit Service 5a9772
                                                          ULONG MessageSeqNo)
Packit 1fb8d4
{
Packit 1fb8d4
	NEGOTIATE_CONTEXT* context;
Packit 1fb8d4
	SECURITY_STATUS status = SEC_E_UNSUPPORTED_FUNCTION;
Packit Service 5a9772
	context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
Packit 1fb8d4
Packit 1fb8d4
	if (context->sspiW->EncryptMessage)
Packit Service 5a9772
		status =
Packit Service 5a9772
		    context->sspiW->EncryptMessage(&(context->SubContext), fQOP, pMessage, MessageSeqNo);
Packit 1fb8d4
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static SECURITY_STATUS SEC_ENTRY negotiate_DecryptMessage(PCtxtHandle phContext,
Packit Service 5a9772
                                                          PSecBufferDesc pMessage,
Packit Service 5a9772
                                                          ULONG MessageSeqNo, ULONG* pfQOP)
Packit 1fb8d4
{
Packit 1fb8d4
	NEGOTIATE_CONTEXT* context;
Packit 1fb8d4
	SECURITY_STATUS status = SEC_E_UNSUPPORTED_FUNCTION;
Packit Service 5a9772
	context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
Packit 1fb8d4
Packit 1fb8d4
	if (context->sspiW->DecryptMessage)
Packit Service 5a9772
		status =
Packit Service 5a9772
		    context->sspiW->DecryptMessage(&(context->SubContext), pMessage, MessageSeqNo, pfQOP);
Packit 1fb8d4
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static SECURITY_STATUS SEC_ENTRY negotiate_MakeSignature(PCtxtHandle phContext, ULONG fQOP,
Packit Service 5a9772
                                                         PSecBufferDesc pMessage,
Packit Service 5a9772
                                                         ULONG MessageSeqNo)
Packit 1fb8d4
{
Packit 1fb8d4
	NEGOTIATE_CONTEXT* context;
Packit 1fb8d4
	SECURITY_STATUS status = SEC_E_UNSUPPORTED_FUNCTION;
Packit Service 5a9772
	context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
Packit 1fb8d4
Packit 1fb8d4
	if (context->sspiW->MakeSignature)
Packit Service 5a9772
		status =
Packit Service 5a9772
		    context->sspiW->MakeSignature(&(context->SubContext), fQOP, pMessage, MessageSeqNo);
Packit 1fb8d4
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static SECURITY_STATUS SEC_ENTRY negotiate_VerifySignature(PCtxtHandle phContext,
Packit Service 5a9772
                                                           PSecBufferDesc pMessage,
Packit Service 5a9772
                                                           ULONG MessageSeqNo, ULONG* pfQOP)
Packit 1fb8d4
{
Packit 1fb8d4
	NEGOTIATE_CONTEXT* context;
Packit 1fb8d4
	SECURITY_STATUS status = SEC_E_UNSUPPORTED_FUNCTION;
Packit Service 5a9772
	context = (NEGOTIATE_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
Packit 1fb8d4
Packit 1fb8d4
	if (context->sspiW->VerifySignature)
Packit Service 5a9772
		status =
Packit Service 5a9772
		    context->sspiW->VerifySignature(&(context->SubContext), pMessage, MessageSeqNo, pfQOP);
Packit 1fb8d4
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
const SecurityFunctionTableA NEGOTIATE_SecurityFunctionTableA = {
Packit Service 5a9772
	1,                                     /* dwVersion */
Packit Service 5a9772
	NULL,                                  /* EnumerateSecurityPackages */
Packit 1fb8d4
	negotiate_QueryCredentialsAttributesA, /* QueryCredentialsAttributes */
Packit Service 5a9772
	negotiate_AcquireCredentialsHandleA,   /* AcquireCredentialsHandle */
Packit Service 5a9772
	negotiate_FreeCredentialsHandle,       /* FreeCredentialsHandle */
Packit Service 5a9772
	NULL,                                  /* Reserved2 */
Packit Service 5a9772
	negotiate_InitializeSecurityContextA,  /* InitializeSecurityContext */
Packit Service 5a9772
	negotiate_AcceptSecurityContext,       /* AcceptSecurityContext */
Packit Service 5a9772
	negotiate_CompleteAuthToken,           /* CompleteAuthToken */
Packit Service 5a9772
	negotiate_DeleteSecurityContext,       /* DeleteSecurityContext */
Packit Service 5a9772
	NULL,                                  /* ApplyControlToken */
Packit Service 5a9772
	negotiate_QueryContextAttributesA,     /* QueryContextAttributes */
Packit Service 5a9772
	negotiate_ImpersonateSecurityContext,  /* ImpersonateSecurityContext */
Packit Service 5a9772
	negotiate_RevertSecurityContext,       /* RevertSecurityContext */
Packit Service 5a9772
	negotiate_MakeSignature,               /* MakeSignature */
Packit Service 5a9772
	negotiate_VerifySignature,             /* VerifySignature */
Packit Service 5a9772
	NULL,                                  /* FreeContextBuffer */
Packit Service 5a9772
	NULL,                                  /* QuerySecurityPackageInfo */
Packit Service 5a9772
	NULL,                                  /* Reserved3 */
Packit Service 5a9772
	NULL,                                  /* Reserved4 */
Packit Service 5a9772
	NULL,                                  /* ExportSecurityContext */
Packit Service 5a9772
	NULL,                                  /* ImportSecurityContext */
Packit Service 5a9772
	NULL,                                  /* AddCredentials */
Packit Service 5a9772
	NULL,                                  /* Reserved8 */
Packit Service 5a9772
	NULL,                                  /* QuerySecurityContextToken */
Packit Service 5a9772
	negotiate_EncryptMessage,              /* EncryptMessage */
Packit Service 5a9772
	negotiate_DecryptMessage,              /* DecryptMessage */
Packit Service 5a9772
	negotiate_SetContextAttributesA,       /* SetContextAttributes */
Packit 1fb8d4
};
Packit 1fb8d4
Packit Service 5a9772
const SecurityFunctionTableW NEGOTIATE_SecurityFunctionTableW = {
Packit Service 5a9772
	1,                                     /* dwVersion */
Packit Service 5a9772
	NULL,                                  /* EnumerateSecurityPackages */
Packit 1fb8d4
	negotiate_QueryCredentialsAttributesW, /* QueryCredentialsAttributes */
Packit Service 5a9772
	negotiate_AcquireCredentialsHandleW,   /* AcquireCredentialsHandle */
Packit Service 5a9772
	negotiate_FreeCredentialsHandle,       /* FreeCredentialsHandle */
Packit Service 5a9772
	NULL,                                  /* Reserved2 */
Packit Service 5a9772
	negotiate_InitializeSecurityContextW,  /* InitializeSecurityContext */
Packit Service 5a9772
	negotiate_AcceptSecurityContext,       /* AcceptSecurityContext */
Packit Service 5a9772
	negotiate_CompleteAuthToken,           /* CompleteAuthToken */
Packit Service 5a9772
	negotiate_DeleteSecurityContext,       /* DeleteSecurityContext */
Packit Service 5a9772
	NULL,                                  /* ApplyControlToken */
Packit Service 5a9772
	negotiate_QueryContextAttributesW,     /* QueryContextAttributes */
Packit Service 5a9772
	negotiate_ImpersonateSecurityContext,  /* ImpersonateSecurityContext */
Packit Service 5a9772
	negotiate_RevertSecurityContext,       /* RevertSecurityContext */
Packit Service 5a9772
	negotiate_MakeSignature,               /* MakeSignature */
Packit Service 5a9772
	negotiate_VerifySignature,             /* VerifySignature */
Packit Service 5a9772
	NULL,                                  /* FreeContextBuffer */
Packit Service 5a9772
	NULL,                                  /* QuerySecurityPackageInfo */
Packit Service 5a9772
	NULL,                                  /* Reserved3 */
Packit Service 5a9772
	NULL,                                  /* Reserved4 */
Packit Service 5a9772
	NULL,                                  /* ExportSecurityContext */
Packit Service 5a9772
	NULL,                                  /* ImportSecurityContext */
Packit Service 5a9772
	NULL,                                  /* AddCredentials */
Packit Service 5a9772
	NULL,                                  /* Reserved8 */
Packit Service 5a9772
	NULL,                                  /* QuerySecurityContextToken */
Packit Service 5a9772
	negotiate_EncryptMessage,              /* EncryptMessage */
Packit Service 5a9772
	negotiate_DecryptMessage,              /* DecryptMessage */
Packit Service 5a9772
	negotiate_SetContextAttributesW,       /* SetContextAttributes */
Packit 1fb8d4
};