2012-03-06 02:03:49 +04:00
|
|
|
/**
|
2012-05-22 06:48:33 +04:00
|
|
|
* WinPR: Windows Portable Runtime
|
2012-03-06 02:03:49 +04:00
|
|
|
* Negotiate Security Package
|
|
|
|
*
|
|
|
|
* Copyright 2011-2012 Jiten Pathy
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:20:53 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2012-05-06 06:39:00 +04:00
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/sspi.h>
|
2012-03-06 02:03:49 +04:00
|
|
|
|
|
|
|
#include "negotiate.h"
|
|
|
|
|
|
|
|
#include "../sspi.h"
|
|
|
|
|
|
|
|
char* NEGOTIATE_PACKAGE_NAME = "Negotiate";
|
|
|
|
|
2012-03-24 23:47:16 +04:00
|
|
|
SECURITY_STATUS SEC_ENTRY negotiate_InitializeSecurityContextW(PCredHandle phCredential, PCtxtHandle phContext,
|
2012-05-22 04:39:45 +04:00
|
|
|
SEC_WCHAR* pszTargetName, ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
|
|
|
|
PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
|
|
|
|
PSecBufferDesc pOutput, PULONG pfContextAttr, PTimeStamp ptsExpiry)
|
2012-03-24 23:47:16 +04:00
|
|
|
{
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
SECURITY_STATUS SEC_ENTRY negotiate_InitializeSecurityContextA(PCredHandle phCredential, PCtxtHandle phContext,
|
2012-05-22 04:39:45 +04:00
|
|
|
SEC_CHAR* pszTargetName, ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
|
|
|
|
PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
|
|
|
|
PSecBufferDesc pOutput, PULONG pfContextAttr, PTimeStamp ptsExpiry)
|
2012-03-06 02:03:49 +04:00
|
|
|
{
|
|
|
|
NEGOTIATE_CONTEXT* context;
|
|
|
|
CREDENTIALS* credentials;
|
2012-03-24 08:14:45 +04:00
|
|
|
PSecBuffer output_SecBuffer;
|
2012-03-06 02:03:49 +04:00
|
|
|
|
|
|
|
context = sspi_SecureHandleGetLowerPointer(phContext);
|
|
|
|
|
|
|
|
if (!context)
|
|
|
|
{
|
|
|
|
context = negotiate_ContextNew();
|
|
|
|
|
2012-05-22 04:39:45 +04:00
|
|
|
credentials = (CREDENTIALS*) sspi_SecureHandleGetLowerPointer(phCredential);
|
2012-05-25 06:20:51 +04:00
|
|
|
sspi_CopyAuthIdentity(&context->identity, &credentials->identity);
|
2012-03-06 02:03:49 +04:00
|
|
|
|
|
|
|
sspi_SecureHandleSetLowerPointer(phNewContext, context);
|
|
|
|
sspi_SecureHandleSetUpperPointer(phNewContext, (void*) NEGOTIATE_PACKAGE_NAME);
|
|
|
|
}
|
|
|
|
|
2012-05-22 04:39:45 +04:00
|
|
|
if ((!pInput) && (context->state == NEGOTIATE_STATE_INITIAL))
|
2012-03-06 02:03:49 +04:00
|
|
|
{
|
|
|
|
if (!pOutput)
|
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
|
|
|
|
if (pOutput->cBuffers < 1)
|
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
|
2012-03-16 04:37:38 +04:00
|
|
|
output_SecBuffer = &pOutput->pBuffers[0];
|
2012-03-06 02:03:49 +04:00
|
|
|
|
2012-03-16 04:37:38 +04:00
|
|
|
if (output_SecBuffer->cbBuffer < 1)
|
2012-03-06 02:03:49 +04:00
|
|
|
return SEC_E_INSUFFICIENT_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SEC_E_OK;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NEGOTIATE_CONTEXT* negotiate_ContextNew()
|
|
|
|
{
|
|
|
|
NEGOTIATE_CONTEXT* context;
|
|
|
|
|
2012-05-06 06:39:00 +04:00
|
|
|
context = (NEGOTIATE_CONTEXT*) calloc(1, sizeof(NEGOTIATE_CONTEXT));
|
2012-03-06 02:03:49 +04:00
|
|
|
|
|
|
|
if (context != NULL)
|
|
|
|
{
|
|
|
|
context->NegotiateFlags = 0;
|
|
|
|
context->state = NEGOTIATE_STATE_INITIAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
void negotiate_ContextFree(NEGOTIATE_CONTEXT* context)
|
|
|
|
{
|
|
|
|
if (!context)
|
|
|
|
return;
|
|
|
|
|
2012-05-06 06:39:00 +04:00
|
|
|
free(context);
|
2012-03-06 02:03:49 +04:00
|
|
|
}
|
|
|
|
|
2012-05-22 04:39:45 +04:00
|
|
|
SECURITY_STATUS SEC_ENTRY negotiate_QueryContextAttributes(PCtxtHandle phContext, ULONG ulAttribute, void* pBuffer)
|
2012-03-06 02:03:49 +04:00
|
|
|
{
|
|
|
|
if (!phContext)
|
|
|
|
return SEC_E_INVALID_HANDLE;
|
|
|
|
|
|
|
|
if (!pBuffer)
|
|
|
|
return SEC_E_INSUFFICIENT_MEMORY;
|
|
|
|
|
|
|
|
return SEC_E_UNSUPPORTED_FUNCTION;
|
|
|
|
}
|
|
|
|
|
2012-03-24 23:47:16 +04:00
|
|
|
SECURITY_STATUS SEC_ENTRY negotiate_AcquireCredentialsHandleW(SEC_WCHAR* pszPrincipal, SEC_WCHAR* pszPackage,
|
2012-08-23 06:38:27 +04:00
|
|
|
ULONG fCredentialUse, void* pvLogonID, void* pAuthData, SEC_GET_KEY_FN pGetKeyFn,
|
2012-03-24 23:47:16 +04:00
|
|
|
void* pvGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
|
|
|
|
{
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
SECURITY_STATUS SEC_ENTRY negotiate_AcquireCredentialsHandleA(SEC_CHAR* pszPrincipal, SEC_CHAR* pszPackage,
|
2012-08-23 06:38:27 +04:00
|
|
|
ULONG fCredentialUse, void* pvLogonID, void* pAuthData, SEC_GET_KEY_FN pGetKeyFn,
|
2012-03-24 23:47:16 +04:00
|
|
|
void* pvGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
|
2012-03-06 02:03:49 +04:00
|
|
|
{
|
|
|
|
CREDENTIALS* credentials;
|
2012-03-16 04:37:38 +04:00
|
|
|
SEC_WINNT_AUTH_IDENTITY* identity;
|
2012-03-06 02:03:49 +04:00
|
|
|
|
|
|
|
if (fCredentialUse == SECPKG_CRED_OUTBOUND)
|
|
|
|
{
|
|
|
|
credentials = sspi_CredentialsNew();
|
2012-03-16 04:37:38 +04:00
|
|
|
identity = (SEC_WINNT_AUTH_IDENTITY*) pAuthData;
|
2012-03-06 02:03:49 +04:00
|
|
|
|
2012-03-16 04:37:38 +04:00
|
|
|
memcpy(&(credentials->identity), identity, sizeof(SEC_WINNT_AUTH_IDENTITY));
|
2012-03-06 02:03:49 +04:00
|
|
|
|
|
|
|
sspi_SecureHandleSetLowerPointer(phCredential, (void*) credentials);
|
|
|
|
sspi_SecureHandleSetUpperPointer(phCredential, (void*) NEGOTIATE_PACKAGE_NAME);
|
|
|
|
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
2012-05-22 04:39:45 +04:00
|
|
|
SECURITY_STATUS SEC_ENTRY negotiate_QueryCredentialsAttributesW(PCredHandle phCredential, ULONG ulAttribute, void* pBuffer)
|
2012-03-24 23:47:16 +04:00
|
|
|
{
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
2012-05-22 04:39:45 +04:00
|
|
|
SECURITY_STATUS SEC_ENTRY negotiate_QueryCredentialsAttributesA(PCredHandle phCredential, ULONG ulAttribute, void* pBuffer)
|
2012-03-06 02:03:49 +04:00
|
|
|
{
|
|
|
|
if (ulAttribute == SECPKG_CRED_ATTR_NAMES)
|
|
|
|
{
|
|
|
|
CREDENTIALS* credentials;
|
|
|
|
|
|
|
|
credentials = (CREDENTIALS*) sspi_SecureHandleGetLowerPointer(phCredential);
|
|
|
|
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SEC_E_UNSUPPORTED_FUNCTION;
|
|
|
|
}
|
|
|
|
|
2012-03-24 08:14:45 +04:00
|
|
|
SECURITY_STATUS SEC_ENTRY negotiate_FreeCredentialsHandle(PCredHandle phCredential)
|
2012-03-06 02:03:49 +04:00
|
|
|
{
|
|
|
|
CREDENTIALS* credentials;
|
|
|
|
|
|
|
|
if (!phCredential)
|
|
|
|
return SEC_E_INVALID_HANDLE;
|
|
|
|
|
|
|
|
credentials = (CREDENTIALS*) sspi_SecureHandleGetLowerPointer(phCredential);
|
|
|
|
|
|
|
|
if (!credentials)
|
|
|
|
return SEC_E_INVALID_HANDLE;
|
|
|
|
|
|
|
|
sspi_CredentialsFree(credentials);
|
|
|
|
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
2012-05-22 04:39:45 +04:00
|
|
|
SECURITY_STATUS SEC_ENTRY negotiate_EncryptMessage(PCtxtHandle phContext, ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
|
2012-03-06 02:03:49 +04:00
|
|
|
{
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
2012-05-22 04:39:45 +04:00
|
|
|
SECURITY_STATUS SEC_ENTRY negotiate_DecryptMessage(PCtxtHandle phContext, PSecBufferDesc pMessage, ULONG MessageSeqNo, ULONG* pfQOP)
|
2012-03-06 02:03:49 +04:00
|
|
|
{
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
2012-05-22 04:39:45 +04:00
|
|
|
SECURITY_STATUS SEC_ENTRY negotiate_MakeSignature(PCtxtHandle phContext, ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
|
2012-03-06 02:03:49 +04:00
|
|
|
{
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
2012-05-22 04:39:45 +04:00
|
|
|
SECURITY_STATUS SEC_ENTRY negotiate_VerifySignature(PCtxtHandle phContext, PSecBufferDesc pMessage, ULONG MessageSeqNo, ULONG* pfQOP)
|
2012-03-06 02:03:49 +04:00
|
|
|
{
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-24 23:47:16 +04:00
|
|
|
const SecurityFunctionTableA NEGOTIATE_SecurityFunctionTableA =
|
2012-03-06 02:03:49 +04:00
|
|
|
{
|
|
|
|
1, /* dwVersion */
|
|
|
|
NULL, /* EnumerateSecurityPackages */
|
2012-03-24 09:01:28 +04:00
|
|
|
negotiate_QueryCredentialsAttributesA, /* QueryCredentialsAttributes */
|
2012-03-24 23:47:16 +04:00
|
|
|
negotiate_AcquireCredentialsHandleA, /* AcquireCredentialsHandle */
|
|
|
|
negotiate_FreeCredentialsHandle, /* FreeCredentialsHandle */
|
|
|
|
NULL, /* Reserved2 */
|
|
|
|
negotiate_InitializeSecurityContextA, /* InitializeSecurityContext */
|
|
|
|
NULL, /* AcceptSecurityContext */
|
|
|
|
NULL, /* CompleteAuthToken */
|
|
|
|
NULL, /* DeleteSecurityContext */
|
|
|
|
NULL, /* ApplyControlToken */
|
|
|
|
negotiate_QueryContextAttributes, /* QueryContextAttributes */
|
|
|
|
NULL, /* ImpersonateSecurityContext */
|
|
|
|
NULL, /* RevertSecurityContext */
|
|
|
|
negotiate_MakeSignature, /* MakeSignature */
|
|
|
|
negotiate_VerifySignature, /* VerifySignature */
|
|
|
|
NULL, /* FreeContextBuffer */
|
|
|
|
NULL, /* QuerySecurityPackageInfo */
|
|
|
|
NULL, /* Reserved3 */
|
|
|
|
NULL, /* Reserved4 */
|
|
|
|
NULL, /* ExportSecurityContext */
|
|
|
|
NULL, /* ImportSecurityContext */
|
|
|
|
NULL, /* AddCredentials */
|
|
|
|
NULL, /* Reserved8 */
|
|
|
|
NULL, /* QuerySecurityContextToken */
|
|
|
|
negotiate_EncryptMessage, /* EncryptMessage */
|
|
|
|
negotiate_DecryptMessage, /* DecryptMessage */
|
|
|
|
NULL, /* SetContextAttributes */
|
|
|
|
};
|
|
|
|
|
|
|
|
const SecurityFunctionTableW NEGOTIATE_SecurityFunctionTableW =
|
|
|
|
{
|
|
|
|
1, /* dwVersion */
|
|
|
|
NULL, /* EnumerateSecurityPackages */
|
|
|
|
negotiate_QueryCredentialsAttributesW, /* QueryCredentialsAttributes */
|
|
|
|
negotiate_AcquireCredentialsHandleW, /* AcquireCredentialsHandle */
|
2012-03-06 02:03:49 +04:00
|
|
|
negotiate_FreeCredentialsHandle, /* FreeCredentialsHandle */
|
|
|
|
NULL, /* Reserved2 */
|
2012-03-24 23:47:16 +04:00
|
|
|
negotiate_InitializeSecurityContextW, /* InitializeSecurityContext */
|
2012-03-06 02:03:49 +04:00
|
|
|
NULL, /* AcceptSecurityContext */
|
|
|
|
NULL, /* CompleteAuthToken */
|
|
|
|
NULL, /* DeleteSecurityContext */
|
|
|
|
NULL, /* ApplyControlToken */
|
|
|
|
negotiate_QueryContextAttributes, /* QueryContextAttributes */
|
|
|
|
NULL, /* ImpersonateSecurityContext */
|
|
|
|
NULL, /* RevertSecurityContext */
|
|
|
|
negotiate_MakeSignature, /* MakeSignature */
|
|
|
|
negotiate_VerifySignature, /* VerifySignature */
|
|
|
|
NULL, /* FreeContextBuffer */
|
|
|
|
NULL, /* QuerySecurityPackageInfo */
|
|
|
|
NULL, /* Reserved3 */
|
|
|
|
NULL, /* Reserved4 */
|
|
|
|
NULL, /* ExportSecurityContext */
|
|
|
|
NULL, /* ImportSecurityContext */
|
|
|
|
NULL, /* AddCredentials */
|
|
|
|
NULL, /* Reserved8 */
|
|
|
|
NULL, /* QuerySecurityContextToken */
|
|
|
|
negotiate_EncryptMessage, /* EncryptMessage */
|
|
|
|
negotiate_DecryptMessage, /* DecryptMessage */
|
|
|
|
NULL, /* SetContextAttributes */
|
|
|
|
};
|
2012-06-04 03:59:35 +04:00
|
|
|
|
|
|
|
const SecPkgInfoA NEGOTIATE_SecPkgInfoA =
|
|
|
|
{
|
|
|
|
0x00083BB3, /* fCapabilities */
|
|
|
|
1, /* wVersion */
|
|
|
|
0x0009, /* wRPCID */
|
|
|
|
0x00002FE0, /* cbMaxToken */
|
|
|
|
"Negotiate", /* Name */
|
|
|
|
"Microsoft Package Negotiator" /* Comment */
|
|
|
|
};
|
|
|
|
|
|
|
|
WCHAR NEGOTIATE_SecPkgInfoW_Name[] = { 'N','e','g','o','t','i','a','t','e','\0' };
|
|
|
|
|
|
|
|
WCHAR NEGOTIATE_SecPkgInfoW_Comment[] =
|
|
|
|
{
|
|
|
|
'M','i','c','r','o','s','o','f','t',' ',
|
|
|
|
'P','a','c','k','a','g','e',' ',
|
|
|
|
'N','e','g','o','t','i','a','t','o','r','\0'
|
|
|
|
};
|
|
|
|
|
|
|
|
const SecPkgInfoW NEGOTIATE_SecPkgInfoW =
|
|
|
|
{
|
|
|
|
0x00083BB3, /* fCapabilities */
|
|
|
|
1, /* wVersion */
|
|
|
|
0x0009, /* wRPCID */
|
|
|
|
0x00002FE0, /* cbMaxToken */
|
|
|
|
NEGOTIATE_SecPkgInfoW_Name, /* Name */
|
|
|
|
NEGOTIATE_SecPkgInfoW_Comment /* Comment */
|
|
|
|
};
|