2005-08-17 11:52:30 +02:00
|
|
|
/*
|
2006-08-12 19:19:49 +02:00
|
|
|
* Copyright 2005, 2006 Kai Blin
|
2005-08-17 11:52:30 +02:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2005-08-17 11:52:30 +02:00
|
|
|
*
|
|
|
|
* This file implements the NTLM security provider.
|
|
|
|
*/
|
2005-12-06 21:22:57 +01:00
|
|
|
|
2005-08-17 11:52:30 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdarg.h>
|
2006-08-12 19:19:49 +02:00
|
|
|
#include <stdio.h>
|
2005-08-17 11:52:30 +02:00
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
2005-12-06 21:22:57 +01:00
|
|
|
#include "winnls.h"
|
2008-01-25 15:05:38 +01:00
|
|
|
#include "wincred.h"
|
2005-12-06 21:22:57 +01:00
|
|
|
#include "rpc.h"
|
2005-08-17 11:52:30 +02:00
|
|
|
#include "sspi.h"
|
2005-12-06 21:22:57 +01:00
|
|
|
#include "lm.h"
|
2005-08-17 11:52:30 +02:00
|
|
|
#include "secur32_priv.h"
|
2006-11-08 21:53:23 +01:00
|
|
|
#include "hmac_md5.h"
|
2008-01-25 15:05:38 +01:00
|
|
|
#include "wine/unicode.h"
|
2005-08-17 11:52:30 +02:00
|
|
|
#include "wine/debug.h"
|
|
|
|
|
2007-04-14 17:20:37 +02:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(ntlm);
|
2011-10-26 20:05:00 +02:00
|
|
|
WINE_DECLARE_DEBUG_CHANNEL(winediag);
|
2005-08-17 11:52:30 +02:00
|
|
|
|
2006-06-16 08:59:48 +02:00
|
|
|
#define NTLM_MAX_BUF 1904
|
2006-11-29 10:58:02 +01:00
|
|
|
#define MIN_NTLM_AUTH_MAJOR_VERSION 3
|
|
|
|
#define MIN_NTLM_AUTH_MINOR_VERSION 0
|
2007-02-05 17:35:04 +01:00
|
|
|
#define MIN_NTLM_AUTH_MICRO_VERSION 25
|
2005-08-17 11:52:30 +02:00
|
|
|
|
2006-11-02 17:50:40 +01:00
|
|
|
static CHAR ntlm_auth[] = "ntlm_auth";
|
|
|
|
|
2005-08-17 11:52:30 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* QueryCredentialsAttributesA
|
|
|
|
*/
|
|
|
|
static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesA(
|
|
|
|
PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
|
|
|
|
{
|
|
|
|
SECURITY_STATUS ret;
|
|
|
|
|
2006-10-15 14:51:52 +02:00
|
|
|
TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
|
2005-08-17 11:52:30 +02:00
|
|
|
|
|
|
|
if(ulAttribute == SECPKG_ATTR_NAMES)
|
|
|
|
{
|
|
|
|
FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
|
|
|
|
ret = SEC_E_UNSUPPORTED_FUNCTION;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = SEC_E_UNSUPPORTED_FUNCTION;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* QueryCredentialsAttributesW
|
|
|
|
*/
|
|
|
|
static SECURITY_STATUS SEC_ENTRY ntlm_QueryCredentialsAttributesW(
|
|
|
|
PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
|
|
|
|
{
|
|
|
|
SECURITY_STATUS ret;
|
|
|
|
|
2006-10-15 14:51:52 +02:00
|
|
|
TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
|
2005-08-17 11:52:30 +02:00
|
|
|
|
|
|
|
if(ulAttribute == SECPKG_ATTR_NAMES)
|
|
|
|
{
|
|
|
|
FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
|
|
|
|
ret = SEC_E_UNSUPPORTED_FUNCTION;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = SEC_E_UNSUPPORTED_FUNCTION;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-01-25 15:05:38 +01:00
|
|
|
static char *ntlm_GetUsernameArg(LPCWSTR userW, INT userW_length)
|
|
|
|
{
|
|
|
|
static const char username_arg[] = "--username=";
|
|
|
|
char *user;
|
|
|
|
int unixcp_size;
|
|
|
|
|
|
|
|
unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
|
|
|
|
userW, userW_length, NULL, 0, NULL, NULL) + sizeof(username_arg);
|
|
|
|
user = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
|
|
|
|
if (!user) return NULL;
|
|
|
|
memcpy(user, username_arg, sizeof(username_arg) - 1);
|
|
|
|
WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, userW, userW_length,
|
|
|
|
user + sizeof(username_arg) - 1,
|
|
|
|
unixcp_size - sizeof(username_arg) + 1, NULL, NULL);
|
|
|
|
user[unixcp_size - 1] = '\0';
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *ntlm_GetDomainArg(LPCWSTR domainW, INT domainW_length)
|
|
|
|
{
|
|
|
|
static const char domain_arg[] = "--domain=";
|
|
|
|
char *domain;
|
|
|
|
int unixcp_size;
|
|
|
|
|
|
|
|
unixcp_size = WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
|
|
|
|
domainW, domainW_length, NULL, 0, NULL, NULL) + sizeof(domain_arg);
|
|
|
|
domain = HeapAlloc(GetProcessHeap(), 0, unixcp_size);
|
|
|
|
if (!domain) return NULL;
|
|
|
|
memcpy(domain, domain_arg, sizeof(domain_arg) - 1);
|
|
|
|
WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS, domainW,
|
|
|
|
domainW_length, domain + sizeof(domain_arg) - 1,
|
|
|
|
unixcp_size - sizeof(domain) + 1, NULL, NULL);
|
|
|
|
domain[unixcp_size - 1] = '\0';
|
|
|
|
return domain;
|
|
|
|
}
|
|
|
|
|
2005-12-06 21:22:57 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* AcquireCredentialsHandleW
|
|
|
|
*/
|
2012-05-10 13:06:20 +02:00
|
|
|
SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleW(
|
2005-12-06 21:22:57 +01:00
|
|
|
SEC_WCHAR *pszPrincipal, SEC_WCHAR *pszPackage, ULONG fCredentialUse,
|
|
|
|
PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
|
|
|
|
PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
|
2005-08-17 11:52:30 +02:00
|
|
|
{
|
|
|
|
SECURITY_STATUS ret;
|
2012-05-10 13:05:48 +02:00
|
|
|
PNtlmCredentials ntlm_cred;
|
2006-07-17 23:41:12 +02:00
|
|
|
|
2006-10-15 14:51:52 +02:00
|
|
|
TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
|
2005-12-06 21:22:57 +01:00
|
|
|
debugstr_w(pszPrincipal), debugstr_w(pszPackage), fCredentialUse,
|
|
|
|
pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
|
|
|
|
|
|
|
|
switch(fCredentialUse)
|
2005-08-17 11:52:30 +02:00
|
|
|
{
|
2005-12-06 21:22:57 +01:00
|
|
|
case SECPKG_CRED_INBOUND:
|
2007-08-07 18:12:32 +02:00
|
|
|
ntlm_cred = HeapAlloc(GetProcessHeap(), 0, sizeof(*ntlm_cred));
|
|
|
|
if (!ntlm_cred)
|
|
|
|
ret = SEC_E_INSUFFICIENT_MEMORY;
|
2005-12-06 21:22:57 +01:00
|
|
|
else
|
|
|
|
{
|
2007-08-07 18:12:32 +02:00
|
|
|
ntlm_cred->mode = NTLM_SERVER;
|
|
|
|
ntlm_cred->username_arg = NULL;
|
|
|
|
ntlm_cred->domain_arg = NULL;
|
|
|
|
ntlm_cred->password = NULL;
|
|
|
|
ntlm_cred->pwlen = 0;
|
2012-05-10 13:05:48 +02:00
|
|
|
ntlm_cred->no_cached_credentials = 0;
|
|
|
|
|
2005-12-06 21:22:57 +01:00
|
|
|
phCredential->dwUpper = fCredentialUse;
|
2007-08-07 18:12:32 +02:00
|
|
|
phCredential->dwLower = (ULONG_PTR)ntlm_cred;
|
|
|
|
ret = SEC_E_OK;
|
2005-12-06 21:22:57 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SECPKG_CRED_OUTBOUND:
|
|
|
|
{
|
2007-08-07 18:12:32 +02:00
|
|
|
ntlm_cred = HeapAlloc(GetProcessHeap(), 0, sizeof(*ntlm_cred));
|
|
|
|
if (!ntlm_cred)
|
|
|
|
{
|
|
|
|
ret = SEC_E_INSUFFICIENT_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ntlm_cred->mode = NTLM_CLIENT;
|
2008-01-25 15:05:31 +01:00
|
|
|
ntlm_cred->username_arg = NULL;
|
|
|
|
ntlm_cred->domain_arg = NULL;
|
2007-08-07 18:12:32 +02:00
|
|
|
ntlm_cred->password = NULL;
|
|
|
|
ntlm_cred->pwlen = 0;
|
2012-05-10 13:05:48 +02:00
|
|
|
ntlm_cred->no_cached_credentials = 0;
|
2007-08-07 18:12:32 +02:00
|
|
|
|
|
|
|
if(pAuthData != NULL)
|
2005-12-06 21:22:57 +01:00
|
|
|
{
|
2009-01-22 09:52:28 +01:00
|
|
|
PSEC_WINNT_AUTH_IDENTITY_W auth_data = pAuthData;
|
2008-01-25 15:05:31 +01:00
|
|
|
|
|
|
|
TRACE("Username is %s\n", debugstr_wn(auth_data->User, auth_data->UserLength));
|
|
|
|
TRACE("Domain name is %s\n", debugstr_wn(auth_data->Domain, auth_data->DomainLength));
|
|
|
|
|
2008-01-25 15:05:38 +01:00
|
|
|
ntlm_cred->username_arg = ntlm_GetUsernameArg(auth_data->User, auth_data->UserLength);
|
|
|
|
ntlm_cred->domain_arg = ntlm_GetDomainArg(auth_data->Domain, auth_data->DomainLength);
|
2005-12-06 21:22:57 +01:00
|
|
|
|
2007-08-07 18:12:32 +02:00
|
|
|
if(auth_data->PasswordLength != 0)
|
2005-12-06 21:22:57 +01:00
|
|
|
{
|
2007-08-07 18:12:32 +02:00
|
|
|
ntlm_cred->pwlen = WideCharToMultiByte(CP_UNIXCP,
|
|
|
|
WC_NO_BEST_FIT_CHARS, auth_data->Password,
|
|
|
|
auth_data->PasswordLength, NULL, 0, NULL,
|
|
|
|
NULL);
|
2007-01-24 10:51:14 +01:00
|
|
|
|
2007-08-07 18:12:32 +02:00
|
|
|
ntlm_cred->password = HeapAlloc(GetProcessHeap(), 0,
|
|
|
|
ntlm_cred->pwlen);
|
|
|
|
|
|
|
|
WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
|
|
|
|
auth_data->Password, auth_data->PasswordLength,
|
|
|
|
ntlm_cred->password, ntlm_cred->pwlen, NULL, NULL);
|
|
|
|
}
|
2005-12-06 21:22:57 +01:00
|
|
|
}
|
2007-08-07 18:12:32 +02:00
|
|
|
|
|
|
|
phCredential->dwUpper = fCredentialUse;
|
|
|
|
phCredential->dwLower = (ULONG_PTR)ntlm_cred;
|
|
|
|
TRACE("ACH phCredential->dwUpper: 0x%08lx, dwLower: 0x%08lx\n",
|
|
|
|
phCredential->dwUpper, phCredential->dwLower);
|
2005-12-06 21:22:57 +01:00
|
|
|
ret = SEC_E_OK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SECPKG_CRED_BOTH:
|
|
|
|
FIXME("AcquireCredentialsHandle: SECPKG_CRED_BOTH stub\n");
|
|
|
|
ret = SEC_E_UNSUPPORTED_FUNCTION;
|
|
|
|
phCredential = NULL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
phCredential = NULL;
|
|
|
|
ret = SEC_E_UNKNOWN_CREDENTIALS;
|
2005-08-17 11:52:30 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* AcquireCredentialsHandleA
|
|
|
|
*/
|
|
|
|
static SECURITY_STATUS SEC_ENTRY ntlm_AcquireCredentialsHandleA(
|
|
|
|
SEC_CHAR *pszPrincipal, SEC_CHAR *pszPackage, ULONG fCredentialUse,
|
|
|
|
PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
|
|
|
|
PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
|
|
|
|
{
|
2005-12-06 21:22:57 +01:00
|
|
|
SECURITY_STATUS ret;
|
|
|
|
int user_sizeW, domain_sizeW, passwd_sizeW;
|
|
|
|
|
|
|
|
SEC_WCHAR *user = NULL, *domain = NULL, *passwd = NULL, *package = NULL;
|
|
|
|
|
|
|
|
PSEC_WINNT_AUTH_IDENTITY_W pAuthDataW = NULL;
|
|
|
|
PSEC_WINNT_AUTH_IDENTITY_A identity = NULL;
|
|
|
|
|
2006-10-15 14:51:52 +02:00
|
|
|
TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
|
2005-08-17 11:52:30 +02:00
|
|
|
debugstr_a(pszPrincipal), debugstr_a(pszPackage), fCredentialUse,
|
|
|
|
pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
|
2005-12-06 21:22:57 +01:00
|
|
|
|
|
|
|
if(pszPackage != NULL)
|
|
|
|
{
|
|
|
|
int package_sizeW = MultiByteToWideChar(CP_ACP, 0, pszPackage, -1,
|
|
|
|
NULL, 0);
|
2005-08-17 11:52:30 +02:00
|
|
|
|
2005-12-06 21:22:57 +01:00
|
|
|
package = HeapAlloc(GetProcessHeap(), 0, package_sizeW *
|
|
|
|
sizeof(SEC_WCHAR));
|
|
|
|
MultiByteToWideChar(CP_ACP, 0, pszPackage, -1, package, package_sizeW);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(pAuthData != NULL)
|
|
|
|
{
|
2009-01-22 09:52:28 +01:00
|
|
|
identity = pAuthData;
|
|
|
|
|
2005-12-06 21:22:57 +01:00
|
|
|
if(identity->Flags == SEC_WINNT_AUTH_IDENTITY_ANSI)
|
|
|
|
{
|
|
|
|
pAuthDataW = HeapAlloc(GetProcessHeap(), 0,
|
|
|
|
sizeof(SEC_WINNT_AUTH_IDENTITY_W));
|
|
|
|
|
|
|
|
if(identity->UserLength != 0)
|
|
|
|
{
|
|
|
|
user_sizeW = MultiByteToWideChar(CP_ACP, 0,
|
2007-03-06 14:30:38 +01:00
|
|
|
(LPCSTR)identity->User, identity->UserLength, NULL, 0);
|
2005-12-06 21:22:57 +01:00
|
|
|
user = HeapAlloc(GetProcessHeap(), 0, user_sizeW *
|
|
|
|
sizeof(SEC_WCHAR));
|
|
|
|
MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->User,
|
2007-03-06 14:30:38 +01:00
|
|
|
identity->UserLength, user, user_sizeW);
|
2005-12-06 21:22:57 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
user_sizeW = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(identity->DomainLength != 0)
|
|
|
|
{
|
|
|
|
domain_sizeW = MultiByteToWideChar(CP_ACP, 0,
|
2007-03-06 14:30:38 +01:00
|
|
|
(LPCSTR)identity->Domain, identity->DomainLength, NULL, 0);
|
2005-12-06 21:22:57 +01:00
|
|
|
domain = HeapAlloc(GetProcessHeap(), 0, domain_sizeW
|
|
|
|
* sizeof(SEC_WCHAR));
|
|
|
|
MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Domain,
|
2007-03-06 14:30:38 +01:00
|
|
|
identity->DomainLength, domain, domain_sizeW);
|
2005-12-06 21:22:57 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
domain_sizeW = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(identity->PasswordLength != 0)
|
|
|
|
{
|
|
|
|
passwd_sizeW = MultiByteToWideChar(CP_ACP, 0,
|
2007-01-24 12:05:33 +01:00
|
|
|
(LPCSTR)identity->Password, identity->PasswordLength,
|
2005-12-06 21:22:57 +01:00
|
|
|
NULL, 0);
|
|
|
|
passwd = HeapAlloc(GetProcessHeap(), 0, passwd_sizeW
|
|
|
|
* sizeof(SEC_WCHAR));
|
|
|
|
MultiByteToWideChar(CP_ACP, 0, (LPCSTR)identity->Password,
|
2007-01-24 12:05:33 +01:00
|
|
|
identity->PasswordLength, passwd, passwd_sizeW);
|
2005-12-06 21:22:57 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
passwd_sizeW = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pAuthDataW->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
|
|
|
|
pAuthDataW->User = user;
|
|
|
|
pAuthDataW->UserLength = user_sizeW;
|
|
|
|
pAuthDataW->Domain = domain;
|
|
|
|
pAuthDataW->DomainLength = domain_sizeW;
|
|
|
|
pAuthDataW->Password = passwd;
|
|
|
|
pAuthDataW->PasswordLength = passwd_sizeW;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pAuthDataW = (PSEC_WINNT_AUTH_IDENTITY_W)identity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ntlm_AcquireCredentialsHandleW(NULL, package, fCredentialUse,
|
|
|
|
pLogonID, pAuthDataW, pGetKeyFn, pGetKeyArgument, phCredential,
|
2005-08-17 11:52:30 +02:00
|
|
|
ptsExpiry);
|
2005-12-06 21:22:57 +01:00
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, package);
|
|
|
|
HeapFree(GetProcessHeap(), 0, user);
|
|
|
|
HeapFree(GetProcessHeap(), 0, domain);
|
|
|
|
HeapFree(GetProcessHeap(), 0, passwd);
|
|
|
|
if(pAuthDataW != (PSEC_WINNT_AUTH_IDENTITY_W)identity)
|
|
|
|
HeapFree(GetProcessHeap(), 0, pAuthDataW);
|
|
|
|
|
|
|
|
return ret;
|
2005-08-17 11:52:30 +02:00
|
|
|
}
|
|
|
|
|
2007-05-24 21:04:47 +02:00
|
|
|
/*************************************************************************
|
|
|
|
* ntlm_GetTokenBufferIndex
|
|
|
|
* Calculates the index of the secbuffer with BufferType == SECBUFFER_TOKEN
|
|
|
|
* Returns index if found or -1 if not found.
|
|
|
|
*/
|
|
|
|
static int ntlm_GetTokenBufferIndex(PSecBufferDesc pMessage)
|
|
|
|
{
|
|
|
|
UINT i;
|
|
|
|
|
|
|
|
TRACE("%p\n", pMessage);
|
|
|
|
|
|
|
|
for( i = 0; i < pMessage->cBuffers; ++i )
|
|
|
|
{
|
|
|
|
if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-10-01 11:57:02 +02:00
|
|
|
/*************************************************************************
|
|
|
|
* ntlm_GetDataBufferIndex
|
|
|
|
* Calculates the index of the first secbuffer with BufferType == SECBUFFER_DATA
|
|
|
|
* Returns index if found or -1 if not found.
|
|
|
|
*/
|
|
|
|
static int ntlm_GetDataBufferIndex(PSecBufferDesc pMessage)
|
|
|
|
{
|
|
|
|
UINT i;
|
|
|
|
|
|
|
|
TRACE("%p\n", pMessage);
|
|
|
|
|
|
|
|
for( i = 0; i < pMessage->cBuffers; ++i )
|
|
|
|
{
|
|
|
|
if(pMessage->pBuffers[i].BufferType == SECBUFFER_DATA)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-01-25 15:05:38 +01:00
|
|
|
static BOOL ntlm_GetCachedCredential(const SEC_WCHAR *pszTargetName, PCREDENTIALW *cred)
|
|
|
|
{
|
|
|
|
LPCWSTR p;
|
|
|
|
LPCWSTR pszHost;
|
|
|
|
LPWSTR pszHostOnly;
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
if (!pszTargetName)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* try to get the start of the hostname from service principal name (SPN) */
|
|
|
|
pszHost = strchrW(pszTargetName, '/');
|
|
|
|
if (pszHost)
|
|
|
|
{
|
|
|
|
/* skip slash character */
|
|
|
|
pszHost++;
|
|
|
|
|
|
|
|
/* find end of host by detecting start of instance port or start of referrer */
|
|
|
|
p = strchrW(pszHost, ':');
|
|
|
|
if (!p)
|
|
|
|
p = strchrW(pszHost, '/');
|
|
|
|
if (!p)
|
|
|
|
p = pszHost + strlenW(pszHost);
|
|
|
|
}
|
|
|
|
else /* otherwise not an SPN, just a host */
|
2008-03-10 17:41:55 +01:00
|
|
|
{
|
|
|
|
pszHost = pszTargetName;
|
2008-01-25 15:05:38 +01:00
|
|
|
p = pszHost + strlenW(pszHost);
|
2008-03-10 17:41:55 +01:00
|
|
|
}
|
2008-01-25 15:05:38 +01:00
|
|
|
|
|
|
|
pszHostOnly = HeapAlloc(GetProcessHeap(), 0, (p - pszHost + 1) * sizeof(WCHAR));
|
|
|
|
if (!pszHostOnly)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
memcpy(pszHostOnly, pszHost, (p - pszHost) * sizeof(WCHAR));
|
|
|
|
pszHostOnly[p - pszHost] = '\0';
|
|
|
|
|
|
|
|
ret = CredReadW(pszHostOnly, CRED_TYPE_DOMAIN_PASSWORD, 0, cred);
|
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, pszHostOnly);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-08-17 11:52:30 +02:00
|
|
|
/***********************************************************************
|
2005-12-07 12:51:05 +01:00
|
|
|
* InitializeSecurityContextW
|
2005-08-17 11:52:30 +02:00
|
|
|
*/
|
2012-05-10 13:06:20 +02:00
|
|
|
SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextW(
|
2005-12-07 12:51:05 +01:00
|
|
|
PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR *pszTargetName,
|
2005-08-17 11:52:30 +02:00
|
|
|
ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
|
|
|
|
PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
|
|
|
|
PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
|
|
|
|
{
|
|
|
|
SECURITY_STATUS ret;
|
2012-05-10 13:05:07 +02:00
|
|
|
PNtlmCredentials ntlm_cred;
|
2007-08-07 18:12:32 +02:00
|
|
|
PNegoHelper helper = NULL;
|
2006-05-19 11:47:55 +02:00
|
|
|
ULONG ctxt_attr = 0;
|
2006-08-12 19:19:49 +02:00
|
|
|
char* buffer, *want_flags = NULL;
|
2006-05-19 11:47:55 +02:00
|
|
|
PBYTE bin;
|
|
|
|
int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
|
2007-05-24 21:04:47 +02:00
|
|
|
int token_idx;
|
2008-01-25 15:05:31 +01:00
|
|
|
SEC_CHAR *username = NULL;
|
2008-01-25 15:05:38 +01:00
|
|
|
SEC_CHAR *domain = NULL;
|
|
|
|
SEC_CHAR *password = NULL;
|
2005-08-17 11:52:30 +02:00
|
|
|
|
2010-10-22 01:37:53 +02:00
|
|
|
TRACE("%p %p %s 0x%08x %d %d %p %d %p %p %p %p\n", phCredential, phContext,
|
2005-12-07 12:51:05 +01:00
|
|
|
debugstr_w(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
|
2005-08-17 11:52:30 +02:00
|
|
|
Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
|
2005-12-07 12:51:05 +01:00
|
|
|
|
2006-05-19 11:47:55 +02:00
|
|
|
/****************************************
|
|
|
|
* When communicating with the client, there can be the
|
|
|
|
* following reply packets:
|
|
|
|
* YR <base64 blob> should be sent to the server
|
|
|
|
* PW should be sent back to helper with
|
|
|
|
* base64 encoded password
|
|
|
|
* AF <base64 blob> client is done, blob should be
|
|
|
|
* sent to server with KK prefixed
|
2006-08-12 19:19:49 +02:00
|
|
|
* GF <string list> A string list of negotiated flags
|
|
|
|
* GK <base64 blob> base64 encoded session key
|
2006-05-19 11:47:55 +02:00
|
|
|
* BH <char reason> something broke
|
|
|
|
*/
|
|
|
|
/* The squid cache size is 2010 chars, and that's what ntlm_auth uses */
|
|
|
|
|
|
|
|
if(TargetDataRep == SECURITY_NETWORK_DREP){
|
2006-06-03 10:14:17 +02:00
|
|
|
TRACE("Setting SECURITY_NETWORK_DREP\n");
|
2006-05-19 11:47:55 +02:00
|
|
|
}
|
2005-12-07 12:51:05 +01:00
|
|
|
|
2006-05-19 11:47:55 +02:00
|
|
|
buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
|
|
|
|
bin = HeapAlloc(GetProcessHeap(), 0, sizeof(BYTE) * NTLM_MAX_BUF);
|
2005-12-07 12:51:05 +01:00
|
|
|
|
2006-05-19 11:47:55 +02:00
|
|
|
if((phContext == NULL) && (pInput == NULL))
|
|
|
|
{
|
2007-08-07 18:12:32 +02:00
|
|
|
static char helper_protocol[] = "--helper-protocol=ntlmssp-client-1";
|
2009-08-11 22:08:02 +02:00
|
|
|
static CHAR credentials_argv[] = "--use-cached-creds";
|
2008-01-25 15:05:31 +01:00
|
|
|
SEC_CHAR *client_argv[5];
|
2008-01-25 15:05:38 +01:00
|
|
|
int pwlen = 0;
|
2007-08-07 18:12:32 +02:00
|
|
|
|
2006-05-19 11:47:55 +02:00
|
|
|
TRACE("First time in ISC()\n");
|
2007-05-24 21:03:32 +02:00
|
|
|
|
|
|
|
if(!phCredential)
|
2007-10-03 21:20:42 +02:00
|
|
|
{
|
|
|
|
ret = SEC_E_INVALID_HANDLE;
|
|
|
|
goto isc_end;
|
|
|
|
}
|
2007-05-24 21:03:32 +02:00
|
|
|
|
|
|
|
/* As the server side of sspi never calls this, make sure that
|
|
|
|
* the handler is a client handler.
|
|
|
|
*/
|
2007-08-07 18:12:32 +02:00
|
|
|
ntlm_cred = (PNtlmCredentials)phCredential->dwLower;
|
|
|
|
if(ntlm_cred->mode != NTLM_CLIENT)
|
2007-05-24 21:03:32 +02:00
|
|
|
{
|
2007-08-07 18:12:32 +02:00
|
|
|
TRACE("Cred mode = %d\n", ntlm_cred->mode);
|
2007-10-03 21:20:42 +02:00
|
|
|
ret = SEC_E_INVALID_HANDLE;
|
|
|
|
goto isc_end;
|
2007-05-24 21:03:32 +02:00
|
|
|
}
|
|
|
|
|
2007-08-07 18:12:32 +02:00
|
|
|
client_argv[0] = ntlm_auth;
|
|
|
|
client_argv[1] = helper_protocol;
|
2008-01-25 15:05:31 +01:00
|
|
|
if (!ntlm_cred->username_arg && !ntlm_cred->domain_arg)
|
|
|
|
{
|
|
|
|
LPWKSTA_USER_INFO_1 ui = NULL;
|
|
|
|
NET_API_STATUS status;
|
2008-01-25 15:05:38 +01:00
|
|
|
PCREDENTIALW cred;
|
2008-01-25 15:05:31 +01:00
|
|
|
|
2008-01-25 15:05:38 +01:00
|
|
|
if (ntlm_GetCachedCredential(pszTargetName, &cred))
|
2008-01-25 15:05:31 +01:00
|
|
|
{
|
2008-01-25 15:05:38 +01:00
|
|
|
LPWSTR p;
|
|
|
|
p = strchrW(cred->UserName, '\\');
|
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
domain = ntlm_GetDomainArg(cred->UserName, p - cred->UserName);
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
domain = ntlm_GetDomainArg(NULL, 0);
|
|
|
|
p = cred->UserName;
|
|
|
|
}
|
2008-01-25 15:05:31 +01:00
|
|
|
|
2008-01-25 15:05:38 +01:00
|
|
|
username = ntlm_GetUsernameArg(p, -1);
|
2008-01-25 15:05:31 +01:00
|
|
|
|
2008-01-25 15:05:38 +01:00
|
|
|
if(cred->CredentialBlobSize != 0)
|
|
|
|
{
|
|
|
|
pwlen = WideCharToMultiByte(CP_UNIXCP,
|
|
|
|
WC_NO_BEST_FIT_CHARS, (LPWSTR)cred->CredentialBlob,
|
|
|
|
cred->CredentialBlobSize / sizeof(WCHAR), NULL, 0,
|
|
|
|
NULL, NULL);
|
2008-01-25 15:05:31 +01:00
|
|
|
|
2008-01-25 15:05:38 +01:00
|
|
|
password = HeapAlloc(GetProcessHeap(), 0, pwlen);
|
|
|
|
|
|
|
|
WideCharToMultiByte(CP_UNIXCP, WC_NO_BEST_FIT_CHARS,
|
|
|
|
(LPWSTR)cred->CredentialBlob,
|
|
|
|
cred->CredentialBlobSize / sizeof(WCHAR),
|
|
|
|
password, pwlen, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
CredFree(cred);
|
|
|
|
|
|
|
|
client_argv[2] = username;
|
|
|
|
client_argv[3] = domain;
|
|
|
|
client_argv[4] = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = NetWkstaUserGetInfo(NULL, 1, (LPBYTE *)&ui);
|
2012-05-10 13:05:48 +02:00
|
|
|
if (status != NERR_Success || ui == NULL || ntlm_cred->no_cached_credentials)
|
2008-01-25 15:05:38 +01:00
|
|
|
{
|
|
|
|
ret = SEC_E_NO_CREDENTIALS;
|
|
|
|
goto isc_end;
|
|
|
|
}
|
|
|
|
username = ntlm_GetUsernameArg(ui->wkui1_username, -1);
|
2009-12-29 20:06:23 +01:00
|
|
|
NetApiBufferFree(ui);
|
2008-01-25 15:05:38 +01:00
|
|
|
|
2009-08-11 22:08:02 +02:00
|
|
|
TRACE("using cached credentials\n");
|
2008-01-25 15:05:38 +01:00
|
|
|
|
|
|
|
client_argv[2] = username;
|
2009-08-11 22:08:02 +02:00
|
|
|
client_argv[3] = credentials_argv;
|
2008-01-25 15:05:38 +01:00
|
|
|
client_argv[4] = NULL;
|
|
|
|
}
|
2008-01-25 15:05:31 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
client_argv[2] = ntlm_cred->username_arg;
|
|
|
|
client_argv[3] = ntlm_cred->domain_arg;
|
|
|
|
client_argv[4] = NULL;
|
|
|
|
}
|
2007-08-07 18:12:32 +02:00
|
|
|
|
|
|
|
if((ret = fork_helper(&helper, ntlm_auth, client_argv)) != SEC_E_OK)
|
|
|
|
goto isc_end;
|
|
|
|
|
|
|
|
helper->mode = NTLM_CLIENT;
|
|
|
|
helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
|
|
|
|
if (!helper->session_key)
|
|
|
|
{
|
|
|
|
cleanup_helper(helper);
|
|
|
|
ret = SEC_E_INSUFFICIENT_MEMORY;
|
|
|
|
goto isc_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate the dummy session key = MD4(MD4(password))*/
|
2008-01-25 15:05:38 +01:00
|
|
|
if(password || ntlm_cred->password)
|
2007-08-07 18:12:32 +02:00
|
|
|
{
|
|
|
|
SEC_WCHAR *unicode_password;
|
|
|
|
int passwd_lenW;
|
|
|
|
|
|
|
|
TRACE("Converting password to unicode.\n");
|
|
|
|
passwd_lenW = MultiByteToWideChar(CP_ACP, 0,
|
2009-01-22 09:52:28 +01:00
|
|
|
password ? password : ntlm_cred->password,
|
2008-01-25 15:05:38 +01:00
|
|
|
password ? pwlen : ntlm_cred->pwlen,
|
2007-08-07 18:12:32 +02:00
|
|
|
NULL, 0);
|
|
|
|
unicode_password = HeapAlloc(GetProcessHeap(), 0,
|
|
|
|
passwd_lenW * sizeof(SEC_WCHAR));
|
2009-01-22 09:52:28 +01:00
|
|
|
MultiByteToWideChar(CP_ACP, 0, password ? password : ntlm_cred->password,
|
2008-01-25 15:05:38 +01:00
|
|
|
password ? pwlen : ntlm_cred->pwlen, unicode_password, passwd_lenW);
|
2007-08-07 18:12:32 +02:00
|
|
|
|
2010-04-19 10:15:15 +02:00
|
|
|
SECUR32_CreateNTLM1SessionKey((PBYTE)unicode_password,
|
|
|
|
passwd_lenW * sizeof(SEC_WCHAR), helper->session_key);
|
2007-08-07 18:12:32 +02:00
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, unicode_password);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
memset(helper->session_key, 0, 16);
|
|
|
|
|
2010-04-19 10:15:15 +02:00
|
|
|
/* Allocate space for a maximal string of
|
2006-08-12 19:19:49 +02:00
|
|
|
* "SF NTLMSSP_FEATURE_SIGN NTLMSSP_FEATURE_SEAL
|
|
|
|
* NTLMSSP_FEATURE_SESSION_KEY"
|
|
|
|
*/
|
|
|
|
want_flags = HeapAlloc(GetProcessHeap(), 0, 73);
|
|
|
|
if(want_flags == NULL)
|
|
|
|
{
|
2007-08-07 18:12:32 +02:00
|
|
|
cleanup_helper(helper);
|
2006-08-12 19:19:49 +02:00
|
|
|
ret = SEC_E_INSUFFICIENT_MEMORY;
|
2006-08-14 17:32:33 +02:00
|
|
|
goto isc_end;
|
2006-08-12 19:19:49 +02:00
|
|
|
}
|
|
|
|
lstrcpyA(want_flags, "SF");
|
|
|
|
if(fContextReq & ISC_REQ_CONFIDENTIALITY)
|
|
|
|
{
|
2008-04-24 22:43:45 +02:00
|
|
|
if(strstr(want_flags, "NTLMSSP_FEATURE_SEAL") == NULL)
|
2006-11-13 15:55:39 +01:00
|
|
|
lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
|
2006-08-12 19:19:49 +02:00
|
|
|
}
|
2006-11-13 15:55:39 +01:00
|
|
|
if(fContextReq & ISC_REQ_CONNECTION)
|
|
|
|
ctxt_attr |= ISC_RET_CONNECTION;
|
2006-08-12 19:19:49 +02:00
|
|
|
if(fContextReq & ISC_REQ_EXTENDED_ERROR)
|
2006-11-13 15:55:39 +01:00
|
|
|
ctxt_attr |= ISC_RET_EXTENDED_ERROR;
|
2006-08-12 19:19:49 +02:00
|
|
|
if(fContextReq & ISC_REQ_INTEGRITY)
|
2006-11-13 15:55:39 +01:00
|
|
|
{
|
2008-04-24 22:43:45 +02:00
|
|
|
if(strstr(want_flags, "NTLMSSP_FEATURE_SIGN") == NULL)
|
2006-11-13 15:55:39 +01:00
|
|
|
lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
|
|
|
|
}
|
2006-08-12 19:19:49 +02:00
|
|
|
if(fContextReq & ISC_REQ_MUTUAL_AUTH)
|
2006-11-13 15:55:39 +01:00
|
|
|
ctxt_attr |= ISC_RET_MUTUAL_AUTH;
|
2006-08-12 19:19:49 +02:00
|
|
|
if(fContextReq & ISC_REQ_REPLAY_DETECT)
|
2006-11-13 15:55:39 +01:00
|
|
|
{
|
2008-04-24 22:43:45 +02:00
|
|
|
if(strstr(want_flags, "NTLMSSP_FEATURE_SIGN") == NULL)
|
2006-11-13 15:55:39 +01:00
|
|
|
lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
|
|
|
|
}
|
2006-08-12 19:19:49 +02:00
|
|
|
if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
|
2006-11-13 15:55:39 +01:00
|
|
|
{
|
2008-04-24 22:43:45 +02:00
|
|
|
if(strstr(want_flags, "NTLMSSP_FEATURE_SIGN") == NULL)
|
2006-11-13 15:55:39 +01:00
|
|
|
lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
|
|
|
|
}
|
2006-08-12 19:19:49 +02:00
|
|
|
if(fContextReq & ISC_REQ_STREAM)
|
|
|
|
FIXME("ISC_REQ_STREAM\n");
|
2006-11-13 15:55:39 +01:00
|
|
|
if(fContextReq & ISC_REQ_USE_DCE_STYLE)
|
|
|
|
ctxt_attr |= ISC_RET_USED_DCE_STYLE;
|
|
|
|
if(fContextReq & ISC_REQ_DELEGATE)
|
|
|
|
ctxt_attr |= ISC_RET_DELEGATE;
|
2006-08-12 19:19:49 +02:00
|
|
|
|
2009-08-11 22:08:02 +02:00
|
|
|
/* If no password is given, try to use cached credentials. Fall back to an empty
|
|
|
|
* password if this failed. */
|
2008-01-25 15:05:38 +01:00
|
|
|
if(!password && !ntlm_cred->password)
|
2006-05-19 11:47:55 +02:00
|
|
|
{
|
2006-10-06 12:11:05 +02:00
|
|
|
lstrcpynA(buffer, "OK", max_len-1);
|
|
|
|
if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
|
2007-08-07 18:12:32 +02:00
|
|
|
{
|
|
|
|
cleanup_helper(helper);
|
2006-10-06 12:11:05 +02:00
|
|
|
goto isc_end;
|
2007-08-07 18:12:32 +02:00
|
|
|
}
|
2009-08-11 22:08:02 +02:00
|
|
|
/* If the helper replied with "PW", using cached credentials failed */
|
2006-10-06 12:11:05 +02:00
|
|
|
if(!strncmp(buffer, "PW", 2))
|
|
|
|
{
|
2008-01-25 15:05:34 +01:00
|
|
|
TRACE("Using cached credentials failed.\n");
|
2009-11-07 11:27:54 +01:00
|
|
|
lstrcpynA(buffer, "PW AA==", max_len-1);
|
2006-10-06 12:11:05 +02:00
|
|
|
}
|
2009-08-11 22:08:02 +02:00
|
|
|
else /* Just do a noop on the next run */
|
2006-10-06 12:11:05 +02:00
|
|
|
lstrcpynA(buffer, "OK", max_len-1);
|
2006-05-19 11:47:55 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lstrcpynA(buffer, "PW ", max_len-1);
|
2008-01-25 15:05:38 +01:00
|
|
|
if((ret = encodeBase64(password ? (unsigned char *)password : (unsigned char *)ntlm_cred->password,
|
|
|
|
password ? pwlen : ntlm_cred->pwlen, buffer+3,
|
2006-05-19 11:47:55 +02:00
|
|
|
max_len-3, &buffer_len)) != SEC_E_OK)
|
2007-08-07 18:12:32 +02:00
|
|
|
{
|
|
|
|
cleanup_helper(helper);
|
2006-08-14 17:32:33 +02:00
|
|
|
goto isc_end;
|
2007-08-07 18:12:32 +02:00
|
|
|
}
|
2006-04-27 13:52:58 +02:00
|
|
|
|
2006-05-19 11:47:55 +02:00
|
|
|
}
|
2006-04-27 13:52:58 +02:00
|
|
|
|
2006-05-19 11:47:55 +02:00
|
|
|
TRACE("Sending to helper: %s\n", debugstr_a(buffer));
|
|
|
|
if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
|
2007-08-07 18:12:32 +02:00
|
|
|
{
|
|
|
|
cleanup_helper(helper);
|
2006-08-14 17:32:33 +02:00
|
|
|
goto isc_end;
|
2007-08-07 18:12:32 +02:00
|
|
|
}
|
2006-04-27 13:53:19 +02:00
|
|
|
|
2006-05-19 11:47:55 +02:00
|
|
|
TRACE("Helper returned %s\n", debugstr_a(buffer));
|
2006-08-12 19:19:49 +02:00
|
|
|
|
|
|
|
if(lstrlenA(want_flags) > 2)
|
|
|
|
{
|
2007-01-18 11:40:15 +01:00
|
|
|
TRACE("Want flags are %s\n", debugstr_a(want_flags));
|
2006-08-12 19:19:49 +02:00
|
|
|
lstrcpynA(buffer, want_flags, max_len-1);
|
|
|
|
if((ret = run_helper(helper, buffer, max_len, &buffer_len))
|
|
|
|
!= SEC_E_OK)
|
2011-11-30 02:16:13 +01:00
|
|
|
{
|
|
|
|
cleanup_helper(helper);
|
2006-08-14 17:32:33 +02:00
|
|
|
goto isc_end;
|
2011-11-30 02:16:13 +01:00
|
|
|
}
|
2006-08-12 19:19:49 +02:00
|
|
|
if(!strncmp(buffer, "BH", 2))
|
2006-11-29 10:58:02 +01:00
|
|
|
ERR("Helper doesn't understand new command set. Expect more things to fail.\n");
|
2006-08-12 19:19:49 +02:00
|
|
|
}
|
|
|
|
|
2006-05-19 11:47:55 +02:00
|
|
|
lstrcpynA(buffer, "YR", max_len-1);
|
2005-12-07 12:51:05 +01:00
|
|
|
|
2006-05-19 11:47:55 +02:00
|
|
|
if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
|
2007-08-07 18:12:32 +02:00
|
|
|
{
|
|
|
|
cleanup_helper(helper);
|
2006-08-14 17:32:33 +02:00
|
|
|
goto isc_end;
|
2007-08-07 18:12:32 +02:00
|
|
|
}
|
2006-04-27 13:52:58 +02:00
|
|
|
|
2006-05-19 11:47:55 +02:00
|
|
|
TRACE("%s\n", buffer);
|
2006-04-27 13:52:58 +02:00
|
|
|
|
2006-05-19 11:47:55 +02:00
|
|
|
if(strncmp(buffer, "YR ", 3) != 0)
|
|
|
|
{
|
|
|
|
/* Something borked */
|
|
|
|
TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
|
|
|
|
ret = SEC_E_INTERNAL_ERROR;
|
2007-08-07 18:12:32 +02:00
|
|
|
cleanup_helper(helper);
|
2006-08-14 17:32:33 +02:00
|
|
|
goto isc_end;
|
2006-05-19 11:47:55 +02:00
|
|
|
}
|
|
|
|
if((ret = decodeBase64(buffer+3, buffer_len-3, bin,
|
|
|
|
max_len-1, &bin_len)) != SEC_E_OK)
|
2007-08-07 18:12:32 +02:00
|
|
|
{
|
|
|
|
cleanup_helper(helper);
|
2006-08-14 17:32:33 +02:00
|
|
|
goto isc_end;
|
2007-08-07 18:12:32 +02:00
|
|
|
}
|
2005-12-07 12:51:05 +01:00
|
|
|
|
2006-05-19 11:47:55 +02:00
|
|
|
/* put the decoded client blob into the out buffer */
|
2005-12-07 12:51:05 +01:00
|
|
|
|
2007-05-24 21:03:32 +02:00
|
|
|
phNewContext->dwUpper = ctxt_attr;
|
|
|
|
phNewContext->dwLower = (ULONG_PTR)helper;
|
|
|
|
|
2006-05-19 11:47:55 +02:00
|
|
|
ret = SEC_I_CONTINUE_NEEDED;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-05-24 21:04:47 +02:00
|
|
|
int input_token_idx;
|
|
|
|
|
2006-05-19 11:47:55 +02:00
|
|
|
/* handle second call here */
|
|
|
|
/* encode server data to base64 */
|
2007-05-24 21:04:47 +02:00
|
|
|
if (!pInput || ((input_token_idx = ntlm_GetTokenBufferIndex(pInput)) == -1))
|
2006-05-19 11:47:55 +02:00
|
|
|
{
|
2007-05-24 21:04:47 +02:00
|
|
|
ret = SEC_E_INVALID_TOKEN;
|
2006-08-14 17:32:33 +02:00
|
|
|
goto isc_end;
|
2006-05-19 11:47:55 +02:00
|
|
|
}
|
2005-12-07 12:51:05 +01:00
|
|
|
|
2007-05-24 21:03:32 +02:00
|
|
|
if(!phContext)
|
2007-10-03 21:20:42 +02:00
|
|
|
{
|
|
|
|
ret = SEC_E_INVALID_HANDLE;
|
|
|
|
goto isc_end;
|
|
|
|
}
|
2007-05-24 21:03:32 +02:00
|
|
|
|
|
|
|
/* As the server side of sspi never calls this, make sure that
|
|
|
|
* the handler is a client handler.
|
|
|
|
*/
|
|
|
|
helper = (PNegoHelper)phContext->dwLower;
|
|
|
|
if(helper->mode != NTLM_CLIENT)
|
|
|
|
{
|
|
|
|
TRACE("Helper mode = %d\n", helper->mode);
|
2007-10-03 21:20:42 +02:00
|
|
|
ret = SEC_E_INVALID_HANDLE;
|
|
|
|
goto isc_end;
|
2007-05-24 21:03:32 +02:00
|
|
|
}
|
|
|
|
|
2007-05-24 21:04:47 +02:00
|
|
|
if (!pInput->pBuffers[input_token_idx].pvBuffer)
|
2006-05-19 11:47:55 +02:00
|
|
|
{
|
|
|
|
ret = SEC_E_INTERNAL_ERROR;
|
2006-08-14 17:32:33 +02:00
|
|
|
goto isc_end;
|
2006-05-19 11:47:55 +02:00
|
|
|
}
|
2005-12-07 12:51:05 +01:00
|
|
|
|
2007-05-24 21:04:47 +02:00
|
|
|
if(pInput->pBuffers[input_token_idx].cbBuffer > max_len)
|
2006-05-19 11:47:55 +02:00
|
|
|
{
|
2009-01-06 00:06:04 +01:00
|
|
|
TRACE("pInput->pBuffers[%d].cbBuffer is: %d\n",
|
2007-05-26 01:11:46 +02:00
|
|
|
input_token_idx,
|
2007-05-24 21:04:47 +02:00
|
|
|
pInput->pBuffers[input_token_idx].cbBuffer);
|
2006-05-19 11:47:55 +02:00
|
|
|
ret = SEC_E_INVALID_TOKEN;
|
2006-08-14 17:32:33 +02:00
|
|
|
goto isc_end;
|
2006-05-19 11:47:55 +02:00
|
|
|
}
|
|
|
|
else
|
2007-05-24 21:04:47 +02:00
|
|
|
bin_len = pInput->pBuffers[input_token_idx].cbBuffer;
|
2005-12-07 12:51:05 +01:00
|
|
|
|
2007-05-24 21:04:47 +02:00
|
|
|
memcpy(bin, pInput->pBuffers[input_token_idx].pvBuffer, bin_len);
|
2005-12-07 12:51:05 +01:00
|
|
|
|
2006-05-19 11:47:55 +02:00
|
|
|
lstrcpynA(buffer, "TT ", max_len-1);
|
2005-12-07 12:51:05 +01:00
|
|
|
|
2006-05-19 11:47:55 +02:00
|
|
|
if((ret = encodeBase64(bin, bin_len, buffer+3,
|
|
|
|
max_len-3, &buffer_len)) != SEC_E_OK)
|
2006-08-14 17:32:33 +02:00
|
|
|
goto isc_end;
|
2005-12-07 12:51:05 +01:00
|
|
|
|
2006-05-19 11:47:55 +02:00
|
|
|
TRACE("Server sent: %s\n", debugstr_a(buffer));
|
2006-04-27 13:52:58 +02:00
|
|
|
|
2006-05-19 11:47:55 +02:00
|
|
|
/* send TT base64 blob to ntlm_auth */
|
|
|
|
if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
|
2006-08-14 17:32:33 +02:00
|
|
|
goto isc_end;
|
2006-04-27 13:53:19 +02:00
|
|
|
|
2006-05-19 11:47:55 +02:00
|
|
|
TRACE("Helper replied: %s\n", debugstr_a(buffer));
|
2005-12-07 12:51:05 +01:00
|
|
|
|
2006-05-19 11:47:55 +02:00
|
|
|
if( (strncmp(buffer, "KK ", 3) != 0) &&
|
|
|
|
(strncmp(buffer, "AF ", 3) !=0))
|
|
|
|
{
|
|
|
|
TRACE("Helper returned %c%c\n", buffer[0], buffer[1]);
|
2006-08-12 19:19:49 +02:00
|
|
|
ret = SEC_E_INVALID_TOKEN;
|
2006-08-14 17:32:33 +02:00
|
|
|
goto isc_end;
|
2006-05-19 11:47:55 +02:00
|
|
|
}
|
2005-12-07 12:51:05 +01:00
|
|
|
|
2006-05-19 11:47:55 +02:00
|
|
|
/* decode the blob and send it to server */
|
|
|
|
if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
|
|
|
|
&bin_len)) != SEC_E_OK)
|
|
|
|
{
|
2006-08-14 17:32:33 +02:00
|
|
|
goto isc_end;
|
2006-08-12 19:19:49 +02:00
|
|
|
}
|
|
|
|
|
2006-08-14 19:35:39 +02:00
|
|
|
phNewContext->dwUpper = ctxt_attr;
|
|
|
|
phNewContext->dwLower = (ULONG_PTR)helper;
|
|
|
|
|
|
|
|
ret = SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* put the decoded client blob into the out buffer */
|
|
|
|
|
2007-05-24 21:04:47 +02:00
|
|
|
if (!pOutput || ((token_idx = ntlm_GetTokenBufferIndex(pOutput)) == -1))
|
2006-08-14 19:35:39 +02:00
|
|
|
{
|
2007-05-26 01:11:46 +02:00
|
|
|
TRACE("no SECBUFFER_TOKEN buffer could be found\n");
|
2007-05-24 21:04:47 +02:00
|
|
|
ret = SEC_E_BUFFER_TOO_SMALL;
|
2007-08-07 18:12:32 +02:00
|
|
|
if ((phContext == NULL) && (pInput == NULL))
|
|
|
|
{
|
|
|
|
cleanup_helper(helper);
|
|
|
|
phNewContext->dwUpper = 0;
|
|
|
|
phNewContext->dwLower = 0;
|
|
|
|
}
|
2007-05-24 21:04:47 +02:00
|
|
|
goto isc_end;
|
2006-08-14 19:35:39 +02:00
|
|
|
}
|
|
|
|
|
2007-05-24 21:04:47 +02:00
|
|
|
if (fContextReq & ISC_REQ_ALLOCATE_MEMORY)
|
|
|
|
{
|
2007-12-05 21:52:20 +01:00
|
|
|
pOutput->pBuffers[token_idx].pvBuffer = HeapAlloc(GetProcessHeap(), 0, bin_len);
|
2007-05-24 21:04:47 +02:00
|
|
|
pOutput->pBuffers[token_idx].cbBuffer = bin_len;
|
|
|
|
}
|
|
|
|
else if (pOutput->pBuffers[token_idx].cbBuffer < bin_len)
|
2006-08-14 19:35:39 +02:00
|
|
|
{
|
|
|
|
TRACE("out buffer is NULL or has not enough space\n");
|
|
|
|
ret = SEC_E_BUFFER_TOO_SMALL;
|
2007-08-07 18:12:32 +02:00
|
|
|
if ((phContext == NULL) && (pInput == NULL))
|
|
|
|
{
|
|
|
|
cleanup_helper(helper);
|
|
|
|
phNewContext->dwUpper = 0;
|
|
|
|
phNewContext->dwLower = 0;
|
|
|
|
}
|
2006-08-14 19:35:39 +02:00
|
|
|
goto isc_end;
|
|
|
|
}
|
|
|
|
|
2007-05-24 21:04:47 +02:00
|
|
|
if (!pOutput->pBuffers[token_idx].pvBuffer)
|
2006-08-14 19:35:39 +02:00
|
|
|
{
|
|
|
|
TRACE("out buffer is NULL\n");
|
|
|
|
ret = SEC_E_INTERNAL_ERROR;
|
2007-08-07 18:12:32 +02:00
|
|
|
if ((phContext == NULL) && (pInput == NULL))
|
|
|
|
{
|
|
|
|
cleanup_helper(helper);
|
|
|
|
phNewContext->dwUpper = 0;
|
|
|
|
phNewContext->dwLower = 0;
|
|
|
|
}
|
2006-08-14 19:35:39 +02:00
|
|
|
goto isc_end;
|
|
|
|
}
|
|
|
|
|
2007-05-24 21:04:47 +02:00
|
|
|
pOutput->pBuffers[token_idx].cbBuffer = bin_len;
|
|
|
|
memcpy(pOutput->pBuffers[token_idx].pvBuffer, bin, bin_len);
|
2006-08-14 19:35:39 +02:00
|
|
|
|
|
|
|
if(ret == SEC_E_OK)
|
|
|
|
{
|
2006-08-12 19:19:49 +02:00
|
|
|
TRACE("Getting negotiated flags\n");
|
|
|
|
lstrcpynA(buffer, "GF", max_len - 1);
|
|
|
|
if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
|
2006-08-14 17:32:33 +02:00
|
|
|
goto isc_end;
|
2006-08-12 19:19:49 +02:00
|
|
|
|
|
|
|
if(buffer_len < 3)
|
|
|
|
{
|
2006-11-29 10:58:02 +01:00
|
|
|
TRACE("No flags negotiated.\n");
|
2006-08-17 21:05:14 +02:00
|
|
|
helper->neg_flags = 0l;
|
2006-08-12 19:19:49 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TRACE("Negotiated %s\n", debugstr_a(buffer));
|
2010-05-11 21:28:42 +02:00
|
|
|
sscanf(buffer + 3, "%x", &(helper->neg_flags));
|
|
|
|
TRACE("Stored 0x%08x as flags\n", helper->neg_flags);
|
2006-08-12 19:19:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TRACE("Getting session key\n");
|
|
|
|
lstrcpynA(buffer, "GK", max_len - 1);
|
|
|
|
if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
|
2006-08-14 17:32:33 +02:00
|
|
|
goto isc_end;
|
2006-08-12 19:19:49 +02:00
|
|
|
|
2006-08-17 21:05:14 +02:00
|
|
|
if(strncmp(buffer, "BH", 2) == 0)
|
2006-11-29 10:58:02 +01:00
|
|
|
TRACE("No key negotiated.\n");
|
2006-08-17 21:05:14 +02:00
|
|
|
else if(strncmp(buffer, "GK ", 3) == 0)
|
|
|
|
{
|
|
|
|
if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
|
|
|
|
&bin_len)) != SEC_E_OK)
|
2006-08-12 19:19:49 +02:00
|
|
|
{
|
2006-08-17 21:05:14 +02:00
|
|
|
TRACE("Failed to decode session key\n");
|
2006-08-12 19:19:49 +02:00
|
|
|
}
|
2006-08-17 21:05:14 +02:00
|
|
|
TRACE("Session key is %s\n", debugstr_a(buffer+3));
|
2007-08-07 18:12:32 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, helper->session_key);
|
2006-11-08 12:10:05 +01:00
|
|
|
helper->session_key = HeapAlloc(GetProcessHeap(), 0, bin_len);
|
2006-08-17 21:05:14 +02:00
|
|
|
if(!helper->session_key)
|
2006-08-12 19:19:49 +02:00
|
|
|
{
|
2012-05-10 13:04:53 +02:00
|
|
|
ret = SEC_E_INSUFFICIENT_MEMORY;
|
2006-08-17 21:05:14 +02:00
|
|
|
goto isc_end;
|
2006-08-12 19:19:49 +02:00
|
|
|
}
|
2006-08-17 21:05:14 +02:00
|
|
|
memcpy(helper->session_key, bin, bin_len);
|
2005-12-07 12:51:05 +01:00
|
|
|
}
|
2006-08-17 21:05:14 +02:00
|
|
|
|
2006-08-17 02:11:39 +02:00
|
|
|
helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
|
|
|
|
SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
|
|
|
|
helper->crypt.ntlm.seq_num = 0l;
|
2010-04-19 10:15:15 +02:00
|
|
|
SECUR32_CreateNTLM2SubKeys(helper);
|
2006-11-08 21:53:23 +01:00
|
|
|
helper->crypt.ntlm2.send_a4i = SECUR32_arc4Alloc();
|
|
|
|
helper->crypt.ntlm2.recv_a4i = SECUR32_arc4Alloc();
|
|
|
|
SECUR32_arc4Init(helper->crypt.ntlm2.send_a4i,
|
2008-01-18 22:27:09 +01:00
|
|
|
helper->crypt.ntlm2.send_seal_key, 16);
|
2006-11-08 21:53:23 +01:00
|
|
|
SECUR32_arc4Init(helper->crypt.ntlm2.recv_a4i,
|
2008-01-18 22:27:09 +01:00
|
|
|
helper->crypt.ntlm2.recv_seal_key, 16);
|
2006-11-08 21:53:23 +01:00
|
|
|
helper->crypt.ntlm2.send_seq_no = 0l;
|
|
|
|
helper->crypt.ntlm2.recv_seq_no = 0l;
|
2005-08-17 11:52:30 +02:00
|
|
|
}
|
2006-05-19 11:47:55 +02:00
|
|
|
|
2006-08-14 17:32:33 +02:00
|
|
|
isc_end:
|
2008-01-25 15:05:31 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, username);
|
2008-01-25 15:05:38 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, domain);
|
|
|
|
HeapFree(GetProcessHeap(), 0, password);
|
2006-08-12 19:19:49 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, want_flags);
|
2006-05-19 11:47:55 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, buffer);
|
|
|
|
HeapFree(GetProcessHeap(), 0, bin);
|
2005-08-17 11:52:30 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
2005-12-07 12:51:05 +01:00
|
|
|
* InitializeSecurityContextA
|
2005-08-17 11:52:30 +02:00
|
|
|
*/
|
2005-12-07 12:51:05 +01:00
|
|
|
static SECURITY_STATUS SEC_ENTRY ntlm_InitializeSecurityContextA(
|
|
|
|
PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR *pszTargetName,
|
2005-08-17 11:52:30 +02:00
|
|
|
ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
|
|
|
|
PSecBufferDesc pInput,ULONG Reserved2, PCtxtHandle phNewContext,
|
|
|
|
PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
|
|
|
|
{
|
|
|
|
SECURITY_STATUS ret;
|
2007-05-24 21:03:32 +02:00
|
|
|
SEC_WCHAR *target = NULL;
|
2005-08-17 11:52:30 +02:00
|
|
|
|
2006-10-15 14:51:52 +02:00
|
|
|
TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
|
2005-12-07 12:51:05 +01:00
|
|
|
debugstr_a(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
|
2005-08-17 11:52:30 +02:00
|
|
|
Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
|
2007-05-24 21:03:32 +02:00
|
|
|
|
|
|
|
if(pszTargetName != NULL)
|
2005-08-17 11:52:30 +02:00
|
|
|
{
|
2007-05-24 21:03:32 +02:00
|
|
|
int target_size = MultiByteToWideChar(CP_ACP, 0, pszTargetName,
|
|
|
|
strlen(pszTargetName)+1, NULL, 0);
|
|
|
|
target = HeapAlloc(GetProcessHeap(), 0, target_size *
|
|
|
|
sizeof(SEC_WCHAR));
|
|
|
|
MultiByteToWideChar(CP_ACP, 0, pszTargetName, strlen(pszTargetName)+1,
|
|
|
|
target, target_size);
|
2005-08-17 11:52:30 +02:00
|
|
|
}
|
2007-05-24 21:03:32 +02:00
|
|
|
|
|
|
|
ret = ntlm_InitializeSecurityContextW(phCredential, phContext, target,
|
|
|
|
fContextReq, Reserved1, TargetDataRep, pInput, Reserved2,
|
|
|
|
phNewContext, pOutput, pfContextAttr, ptsExpiry);
|
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, target);
|
2005-08-17 11:52:30 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* AcceptSecurityContext
|
|
|
|
*/
|
2012-05-10 13:06:20 +02:00
|
|
|
SECURITY_STATUS SEC_ENTRY ntlm_AcceptSecurityContext(
|
2005-08-17 11:52:30 +02:00
|
|
|
PCredHandle phCredential, PCtxtHandle phContext, PSecBufferDesc pInput,
|
|
|
|
ULONG fContextReq, ULONG TargetDataRep, PCtxtHandle phNewContext,
|
|
|
|
PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
|
|
|
|
{
|
|
|
|
SECURITY_STATUS ret;
|
2006-08-15 02:02:46 +02:00
|
|
|
char *buffer, *want_flags = NULL;
|
2006-08-14 17:32:33 +02:00
|
|
|
PBYTE bin;
|
|
|
|
int buffer_len, bin_len, max_len = NTLM_MAX_BUF;
|
|
|
|
ULONG ctxt_attr = 0;
|
|
|
|
PNegoHelper helper;
|
2007-08-07 18:12:32 +02:00
|
|
|
PNtlmCredentials ntlm_cred;
|
2005-08-17 11:52:30 +02:00
|
|
|
|
2006-10-15 14:51:52 +02:00
|
|
|
TRACE("%p %p %p %d %d %p %p %p %p\n", phCredential, phContext, pInput,
|
2005-08-17 11:52:30 +02:00
|
|
|
fContextReq, TargetDataRep, phNewContext, pOutput, pfContextAttr,
|
|
|
|
ptsExpiry);
|
2006-08-14 17:32:33 +02:00
|
|
|
|
|
|
|
buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(char) * NTLM_MAX_BUF);
|
|
|
|
bin = HeapAlloc(GetProcessHeap(),0, sizeof(BYTE) * NTLM_MAX_BUF);
|
|
|
|
|
|
|
|
if(TargetDataRep == SECURITY_NETWORK_DREP){
|
|
|
|
TRACE("Using SECURITY_NETWORK_DREP\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(phContext == NULL)
|
|
|
|
{
|
2007-08-07 18:12:32 +02:00
|
|
|
static CHAR server_helper_protocol[] = "--helper-protocol=squid-2.5-ntlmssp";
|
|
|
|
SEC_CHAR *server_argv[] = { ntlm_auth,
|
|
|
|
server_helper_protocol,
|
|
|
|
NULL };
|
|
|
|
|
|
|
|
if (!phCredential)
|
|
|
|
{
|
|
|
|
ret = SEC_E_INVALID_HANDLE;
|
|
|
|
goto asc_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
ntlm_cred = (PNtlmCredentials)phCredential->dwLower;
|
|
|
|
|
|
|
|
if(ntlm_cred->mode != NTLM_SERVER)
|
|
|
|
{
|
|
|
|
ret = SEC_E_INVALID_HANDLE;
|
|
|
|
goto asc_end;
|
|
|
|
}
|
|
|
|
|
2006-08-14 17:32:33 +02:00
|
|
|
/* This is the first call to AcceptSecurityHandle */
|
|
|
|
if(pInput == NULL)
|
2005-12-19 21:20:03 +01:00
|
|
|
{
|
2006-08-14 17:32:33 +02:00
|
|
|
ret = SEC_E_INCOMPLETE_MESSAGE;
|
|
|
|
goto asc_end;
|
2005-12-19 21:20:03 +01:00
|
|
|
}
|
|
|
|
|
2006-08-14 17:32:33 +02:00
|
|
|
if(pInput->cBuffers < 1)
|
|
|
|
{
|
|
|
|
ret = SEC_E_INCOMPLETE_MESSAGE;
|
|
|
|
goto asc_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pInput->pBuffers[0].cbBuffer > max_len)
|
|
|
|
{
|
|
|
|
ret = SEC_E_INVALID_TOKEN;
|
|
|
|
goto asc_end;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
bin_len = pInput->pBuffers[0].cbBuffer;
|
|
|
|
|
2007-08-07 18:12:32 +02:00
|
|
|
if( (ret = fork_helper(&helper, ntlm_auth, server_argv)) !=
|
|
|
|
SEC_E_OK)
|
|
|
|
{
|
|
|
|
ret = SEC_E_INTERNAL_ERROR;
|
|
|
|
goto asc_end;
|
|
|
|
}
|
|
|
|
helper->mode = NTLM_SERVER;
|
|
|
|
|
2005-12-19 21:20:03 +01:00
|
|
|
/* Handle all the flags */
|
2006-08-15 02:02:46 +02:00
|
|
|
want_flags = HeapAlloc(GetProcessHeap(), 0, 73);
|
|
|
|
if(want_flags == NULL)
|
|
|
|
{
|
|
|
|
TRACE("Failed to allocate memory for the want_flags!\n");
|
|
|
|
ret = SEC_E_INSUFFICIENT_MEMORY;
|
2007-08-07 18:12:32 +02:00
|
|
|
cleanup_helper(helper);
|
2006-08-15 02:02:46 +02:00
|
|
|
goto asc_end;
|
|
|
|
}
|
|
|
|
lstrcpyA(want_flags, "SF");
|
2006-08-14 17:32:33 +02:00
|
|
|
if(fContextReq & ASC_REQ_ALLOCATE_MEMORY)
|
2005-12-19 21:20:03 +01:00
|
|
|
{
|
2006-08-14 17:32:33 +02:00
|
|
|
FIXME("ASC_REQ_ALLOCATE_MEMORY stub\n");
|
2005-12-19 21:20:03 +01:00
|
|
|
}
|
2006-08-14 17:32:33 +02:00
|
|
|
if(fContextReq & ASC_REQ_CONFIDENTIALITY)
|
2005-12-19 21:20:03 +01:00
|
|
|
{
|
2006-08-15 02:02:46 +02:00
|
|
|
lstrcatA(want_flags, " NTLMSSP_FEATURE_SEAL");
|
2005-12-19 21:20:03 +01:00
|
|
|
}
|
2006-08-14 17:32:33 +02:00
|
|
|
if(fContextReq & ASC_REQ_CONNECTION)
|
2005-12-19 21:20:03 +01:00
|
|
|
{
|
|
|
|
/* This is default, so we'll enable it */
|
2006-08-15 02:02:46 +02:00
|
|
|
lstrcatA(want_flags, " NTLMSSP_FEATURE_SESSION_KEY");
|
2006-08-14 17:32:33 +02:00
|
|
|
ctxt_attr |= ASC_RET_CONNECTION;
|
2005-12-19 21:20:03 +01:00
|
|
|
}
|
2006-08-14 17:32:33 +02:00
|
|
|
if(fContextReq & ASC_REQ_EXTENDED_ERROR)
|
2005-12-19 21:20:03 +01:00
|
|
|
{
|
2006-08-14 17:32:33 +02:00
|
|
|
FIXME("ASC_REQ_EXTENDED_ERROR stub\n");
|
2005-12-19 21:20:03 +01:00
|
|
|
}
|
2006-08-14 17:32:33 +02:00
|
|
|
if(fContextReq & ASC_REQ_INTEGRITY)
|
2005-12-19 21:20:03 +01:00
|
|
|
{
|
2006-08-15 02:02:46 +02:00
|
|
|
lstrcatA(want_flags, " NTLMSSP_FEATURE_SIGN");
|
2005-12-19 21:20:03 +01:00
|
|
|
}
|
2006-08-14 17:32:33 +02:00
|
|
|
if(fContextReq & ASC_REQ_MUTUAL_AUTH)
|
2005-12-19 21:20:03 +01:00
|
|
|
{
|
2006-08-14 17:32:33 +02:00
|
|
|
FIXME("ASC_REQ_MUTUAL_AUTH stub\n");
|
2005-12-19 21:20:03 +01:00
|
|
|
}
|
2006-08-14 17:32:33 +02:00
|
|
|
if(fContextReq & ASC_REQ_REPLAY_DETECT)
|
2005-12-19 21:20:03 +01:00
|
|
|
{
|
2006-08-14 17:32:33 +02:00
|
|
|
FIXME("ASC_REQ_REPLAY_DETECT stub\n");
|
2005-12-19 21:20:03 +01:00
|
|
|
}
|
|
|
|
if(fContextReq & ISC_REQ_SEQUENCE_DETECT)
|
|
|
|
{
|
2006-08-14 17:32:33 +02:00
|
|
|
FIXME("ASC_REQ_SEQUENCE_DETECT stub\n");
|
2005-12-19 21:20:03 +01:00
|
|
|
}
|
|
|
|
if(fContextReq & ISC_REQ_STREAM)
|
|
|
|
{
|
2006-08-14 17:32:33 +02:00
|
|
|
FIXME("ASC_REQ_STREAM stub\n");
|
2005-12-19 21:20:03 +01:00
|
|
|
}
|
|
|
|
/* Done with the flags */
|
|
|
|
|
2006-08-15 02:02:46 +02:00
|
|
|
if(lstrlenA(want_flags) > 3)
|
|
|
|
{
|
|
|
|
TRACE("Server set want_flags: %s\n", debugstr_a(want_flags));
|
|
|
|
lstrcpynA(buffer, want_flags, max_len - 1);
|
|
|
|
if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
|
|
|
|
SEC_E_OK)
|
2007-08-07 18:12:32 +02:00
|
|
|
{
|
|
|
|
cleanup_helper(helper);
|
2006-08-15 02:02:46 +02:00
|
|
|
goto asc_end;
|
2007-08-07 18:12:32 +02:00
|
|
|
}
|
2006-08-15 02:02:46 +02:00
|
|
|
if(!strncmp(buffer, "BH", 2))
|
|
|
|
TRACE("Helper doesn't understand new command set\n");
|
|
|
|
}
|
|
|
|
|
2006-08-14 17:32:33 +02:00
|
|
|
/* This is the YR request from the client, encode to base64 */
|
|
|
|
|
|
|
|
memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
|
|
|
|
|
|
|
|
lstrcpynA(buffer, "YR ", max_len-1);
|
|
|
|
|
|
|
|
if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
|
|
|
|
&buffer_len)) != SEC_E_OK)
|
2005-12-19 21:20:03 +01:00
|
|
|
{
|
2007-08-07 18:12:32 +02:00
|
|
|
cleanup_helper(helper);
|
2006-08-14 17:32:33 +02:00
|
|
|
goto asc_end;
|
|
|
|
}
|
2005-12-19 21:20:03 +01:00
|
|
|
|
2006-08-14 17:32:33 +02:00
|
|
|
TRACE("Client sent: %s\n", debugstr_a(buffer));
|
2005-12-19 21:20:03 +01:00
|
|
|
|
2006-08-14 17:32:33 +02:00
|
|
|
if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
|
|
|
|
SEC_E_OK)
|
|
|
|
{
|
2007-08-07 18:12:32 +02:00
|
|
|
cleanup_helper(helper);
|
2006-08-14 17:32:33 +02:00
|
|
|
goto asc_end;
|
|
|
|
}
|
2005-12-19 21:20:03 +01:00
|
|
|
|
2006-08-14 17:32:33 +02:00
|
|
|
TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
|
|
|
|
/* The expected answer is TT <base64 blob> */
|
2005-12-19 21:20:03 +01:00
|
|
|
|
2006-08-14 17:32:33 +02:00
|
|
|
if(strncmp(buffer, "TT ", 3) != 0)
|
|
|
|
{
|
|
|
|
ret = SEC_E_INTERNAL_ERROR;
|
2007-08-07 18:12:32 +02:00
|
|
|
cleanup_helper(helper);
|
2006-08-14 17:32:33 +02:00
|
|
|
goto asc_end;
|
|
|
|
}
|
2005-12-19 21:20:03 +01:00
|
|
|
|
2006-08-14 17:32:33 +02:00
|
|
|
if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
|
|
|
|
&bin_len)) != SEC_E_OK)
|
|
|
|
{
|
2007-08-07 18:12:32 +02:00
|
|
|
cleanup_helper(helper);
|
2006-08-14 17:32:33 +02:00
|
|
|
goto asc_end;
|
|
|
|
}
|
2005-12-19 21:20:03 +01:00
|
|
|
|
2006-08-14 17:32:33 +02:00
|
|
|
/* send this to the client */
|
|
|
|
if(pOutput == NULL)
|
|
|
|
{
|
|
|
|
ret = SEC_E_INSUFFICIENT_MEMORY;
|
2007-08-07 18:12:32 +02:00
|
|
|
cleanup_helper(helper);
|
2006-08-14 17:32:33 +02:00
|
|
|
goto asc_end;
|
|
|
|
}
|
2005-12-19 21:20:03 +01:00
|
|
|
|
2006-08-14 17:32:33 +02:00
|
|
|
if(pOutput->cBuffers < 1)
|
|
|
|
{
|
|
|
|
ret = SEC_E_INSUFFICIENT_MEMORY;
|
2007-08-07 18:12:32 +02:00
|
|
|
cleanup_helper(helper);
|
2006-08-14 17:32:33 +02:00
|
|
|
goto asc_end;
|
|
|
|
}
|
2005-12-19 21:20:03 +01:00
|
|
|
|
2006-08-14 17:32:33 +02:00
|
|
|
pOutput->pBuffers[0].cbBuffer = bin_len;
|
|
|
|
pOutput->pBuffers[0].BufferType = SECBUFFER_DATA;
|
|
|
|
memcpy(pOutput->pBuffers[0].pvBuffer, bin, bin_len);
|
|
|
|
ret = SEC_I_CONTINUE_NEEDED;
|
2005-12-19 21:20:03 +01:00
|
|
|
|
2006-08-14 17:32:33 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* we expect a KK request from client */
|
|
|
|
if(pInput == NULL)
|
|
|
|
{
|
|
|
|
ret = SEC_E_INCOMPLETE_MESSAGE;
|
|
|
|
goto asc_end;
|
|
|
|
}
|
2006-08-15 02:02:46 +02:00
|
|
|
|
2006-08-14 17:32:33 +02:00
|
|
|
if(pInput->cBuffers < 1)
|
|
|
|
{
|
|
|
|
ret = SEC_E_INCOMPLETE_MESSAGE;
|
|
|
|
goto asc_end;
|
|
|
|
}
|
|
|
|
|
2007-08-07 18:12:32 +02:00
|
|
|
helper = (PNegoHelper)phContext->dwLower;
|
|
|
|
|
|
|
|
if(helper->mode != NTLM_SERVER)
|
|
|
|
{
|
|
|
|
ret = SEC_E_INVALID_HANDLE;
|
|
|
|
goto asc_end;
|
|
|
|
}
|
|
|
|
|
2006-08-14 17:32:33 +02:00
|
|
|
if(pInput->pBuffers[0].cbBuffer > max_len)
|
|
|
|
{
|
|
|
|
ret = SEC_E_INVALID_TOKEN;
|
|
|
|
goto asc_end;
|
2005-12-19 21:20:03 +01:00
|
|
|
}
|
|
|
|
else
|
2006-08-14 17:32:33 +02:00
|
|
|
bin_len = pInput->pBuffers[0].cbBuffer;
|
|
|
|
|
|
|
|
memcpy(bin, pInput->pBuffers[0].pvBuffer, bin_len);
|
|
|
|
|
|
|
|
lstrcpynA(buffer, "KK ", max_len-1);
|
|
|
|
|
|
|
|
if((ret = encodeBase64(bin, bin_len, buffer+3, max_len-3,
|
|
|
|
&buffer_len)) != SEC_E_OK)
|
2005-12-19 21:20:03 +01:00
|
|
|
{
|
2006-08-14 17:32:33 +02:00
|
|
|
goto asc_end;
|
|
|
|
}
|
2005-12-19 21:20:03 +01:00
|
|
|
|
2006-08-14 17:32:33 +02:00
|
|
|
TRACE("Client sent: %s\n", debugstr_a(buffer));
|
2005-12-19 21:20:03 +01:00
|
|
|
|
2006-08-14 17:32:33 +02:00
|
|
|
if((ret = run_helper(helper, buffer, max_len, &buffer_len)) !=
|
|
|
|
SEC_E_OK)
|
|
|
|
{
|
|
|
|
goto asc_end;
|
|
|
|
}
|
2005-12-19 21:20:03 +01:00
|
|
|
|
2006-08-14 17:32:33 +02:00
|
|
|
TRACE("Reply from ntlm_auth: %s\n", debugstr_a(buffer));
|
2005-12-19 21:20:03 +01:00
|
|
|
|
2007-10-07 00:57:12 +02:00
|
|
|
/* At this point, we get a NA if the user didn't authenticate, but a BH
|
|
|
|
* if ntlm_auth could not connect to winbindd. Apart from running Wine
|
|
|
|
* as root, there is no way to fix this for now, so just handle this as
|
|
|
|
* a failed login. */
|
2006-08-14 17:32:33 +02:00
|
|
|
if(strncmp(buffer, "AF ", 3) != 0)
|
|
|
|
{
|
|
|
|
if(strncmp(buffer, "NA ", 3) == 0)
|
2005-12-19 21:20:03 +01:00
|
|
|
{
|
2006-08-14 17:32:33 +02:00
|
|
|
ret = SEC_E_LOGON_DENIED;
|
|
|
|
goto asc_end;
|
2005-12-19 21:20:03 +01:00
|
|
|
}
|
2006-08-14 17:32:33 +02:00
|
|
|
else
|
2005-12-19 21:20:03 +01:00
|
|
|
{
|
2014-04-12 08:24:20 +02:00
|
|
|
size_t ntlm_pipe_err_v3_len = strlen("BH NT_STATUS_ACCESS_DENIED");
|
|
|
|
size_t ntlm_pipe_err_v4_len = strlen("BH NT_STATUS_UNSUCCESSFUL");
|
2007-10-07 00:57:12 +02:00
|
|
|
|
2014-04-12 08:24:20 +02:00
|
|
|
if( (buffer_len >= ntlm_pipe_err_v3_len &&
|
|
|
|
strncmp(buffer, "BH NT_STATUS_ACCESS_DENIED", ntlm_pipe_err_v3_len) == 0) ||
|
|
|
|
(buffer_len >= ntlm_pipe_err_v4_len &&
|
|
|
|
strncmp(buffer, "BH NT_STATUS_UNSUCCESSFUL", ntlm_pipe_err_v4_len) == 0) )
|
2007-10-07 00:57:12 +02:00
|
|
|
{
|
|
|
|
TRACE("Connection to winbindd failed\n");
|
|
|
|
ret = SEC_E_LOGON_DENIED;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = SEC_E_INTERNAL_ERROR;
|
|
|
|
|
2006-08-14 17:32:33 +02:00
|
|
|
goto asc_end;
|
2005-12-19 21:20:03 +01:00
|
|
|
}
|
|
|
|
}
|
2006-08-14 17:32:33 +02:00
|
|
|
pOutput->pBuffers[0].cbBuffer = 0;
|
2006-08-15 02:02:46 +02:00
|
|
|
|
|
|
|
TRACE("Getting negotiated flags\n");
|
|
|
|
lstrcpynA(buffer, "GF", max_len - 1);
|
|
|
|
if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
|
|
|
|
goto asc_end;
|
|
|
|
|
|
|
|
if(buffer_len < 3)
|
|
|
|
{
|
|
|
|
TRACE("No flags negotiated, or helper does not support GF command\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TRACE("Negotiated %s\n", debugstr_a(buffer));
|
2010-05-11 21:28:42 +02:00
|
|
|
sscanf(buffer + 3, "%x", &(helper->neg_flags));
|
|
|
|
TRACE("Stored 0x%08x as flags\n", helper->neg_flags);
|
2006-08-15 02:02:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TRACE("Getting session key\n");
|
|
|
|
lstrcpynA(buffer, "GK", max_len - 1);
|
|
|
|
if((ret = run_helper(helper, buffer, max_len, &buffer_len)) != SEC_E_OK)
|
|
|
|
goto asc_end;
|
|
|
|
|
|
|
|
if(buffer_len < 3)
|
|
|
|
TRACE("Helper does not support GK command\n");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(strncmp(buffer, "BH ", 3) == 0)
|
|
|
|
{
|
|
|
|
TRACE("Helper sent %s\n", debugstr_a(buffer+3));
|
2012-05-10 13:04:53 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, helper->session_key);
|
2006-08-15 02:02:46 +02:00
|
|
|
helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
|
2012-05-10 13:04:53 +02:00
|
|
|
if (!helper->session_key)
|
|
|
|
{
|
|
|
|
ret = SEC_E_INSUFFICIENT_MEMORY;
|
|
|
|
goto asc_end;
|
|
|
|
}
|
2006-08-15 02:02:46 +02:00
|
|
|
/*FIXME: Generate the dummy session key = MD4(MD4(password))*/
|
|
|
|
memset(helper->session_key, 0 , 16);
|
|
|
|
}
|
|
|
|
else if(strncmp(buffer, "GK ", 3) == 0)
|
|
|
|
{
|
|
|
|
if((ret = decodeBase64(buffer+3, buffer_len-3, bin, max_len,
|
|
|
|
&bin_len)) != SEC_E_OK)
|
|
|
|
{
|
|
|
|
TRACE("Failed to decode session key\n");
|
|
|
|
}
|
|
|
|
TRACE("Session key is %s\n", debugstr_a(buffer+3));
|
2012-05-10 13:04:53 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, helper->session_key);
|
2006-11-08 12:10:05 +01:00
|
|
|
helper->session_key = HeapAlloc(GetProcessHeap(), 0, 16);
|
2006-08-15 02:02:46 +02:00
|
|
|
if(!helper->session_key)
|
|
|
|
{
|
2012-05-10 13:04:53 +02:00
|
|
|
ret = SEC_E_INSUFFICIENT_MEMORY;
|
2006-08-15 02:02:46 +02:00
|
|
|
goto asc_end;
|
|
|
|
}
|
2006-08-17 02:11:39 +02:00
|
|
|
memcpy(helper->session_key, bin, 16);
|
2006-08-15 02:02:46 +02:00
|
|
|
}
|
|
|
|
}
|
2006-08-17 02:11:39 +02:00
|
|
|
helper->crypt.ntlm.a4i = SECUR32_arc4Alloc();
|
|
|
|
SECUR32_arc4Init(helper->crypt.ntlm.a4i, helper->session_key, 16);
|
|
|
|
helper->crypt.ntlm.seq_num = 0l;
|
2005-08-17 11:52:30 +02:00
|
|
|
}
|
2006-08-14 17:32:33 +02:00
|
|
|
|
|
|
|
phNewContext->dwUpper = ctxt_attr;
|
|
|
|
phNewContext->dwLower = (ULONG_PTR)helper;
|
|
|
|
|
|
|
|
asc_end:
|
2006-08-15 02:02:46 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, want_flags);
|
2006-08-14 17:32:33 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, buffer);
|
|
|
|
HeapFree(GetProcessHeap(), 0, bin);
|
2005-08-17 11:52:30 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* CompleteAuthToken
|
|
|
|
*/
|
|
|
|
static SECURITY_STATUS SEC_ENTRY ntlm_CompleteAuthToken(PCtxtHandle phContext,
|
|
|
|
PSecBufferDesc pToken)
|
|
|
|
{
|
2006-08-10 13:06:39 +02:00
|
|
|
/* We never need to call CompleteAuthToken anyway */
|
2005-08-17 11:52:30 +02:00
|
|
|
TRACE("%p %p\n", phContext, pToken);
|
2006-08-10 13:06:39 +02:00
|
|
|
if (!phContext)
|
|
|
|
return SEC_E_INVALID_HANDLE;
|
|
|
|
|
|
|
|
return SEC_E_OK;
|
2005-08-17 11:52:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* DeleteSecurityContext
|
|
|
|
*/
|
2012-05-10 13:06:20 +02:00
|
|
|
SECURITY_STATUS SEC_ENTRY ntlm_DeleteSecurityContext(PCtxtHandle phContext)
|
2005-08-17 11:52:30 +02:00
|
|
|
{
|
2006-11-08 12:10:05 +01:00
|
|
|
PNegoHelper helper;
|
2005-08-17 11:52:30 +02:00
|
|
|
|
|
|
|
TRACE("%p\n", phContext);
|
2006-11-08 12:10:05 +01:00
|
|
|
if (!phContext)
|
|
|
|
return SEC_E_INVALID_HANDLE;
|
|
|
|
|
|
|
|
helper = (PNegoHelper)phContext->dwLower;
|
|
|
|
|
|
|
|
phContext->dwUpper = 0;
|
|
|
|
phContext->dwLower = 0;
|
|
|
|
|
|
|
|
SECUR32_arc4Cleanup(helper->crypt.ntlm.a4i);
|
2006-11-08 21:53:23 +01:00
|
|
|
SECUR32_arc4Cleanup(helper->crypt.ntlm2.send_a4i);
|
|
|
|
SECUR32_arc4Cleanup(helper->crypt.ntlm2.recv_a4i);
|
|
|
|
HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.send_sign_key);
|
|
|
|
HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.send_seal_key);
|
|
|
|
HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.recv_sign_key);
|
|
|
|
HeapFree(GetProcessHeap(), 0, helper->crypt.ntlm2.recv_seal_key);
|
2006-11-08 12:10:05 +01:00
|
|
|
|
2007-08-07 18:12:32 +02:00
|
|
|
cleanup_helper(helper);
|
|
|
|
|
2006-11-08 12:10:05 +01:00
|
|
|
return SEC_E_OK;
|
2005-08-17 11:52:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* QueryContextAttributesW
|
|
|
|
*/
|
2012-05-10 13:06:20 +02:00
|
|
|
SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesW(PCtxtHandle phContext,
|
2006-10-15 14:51:52 +02:00
|
|
|
ULONG ulAttribute, void *pBuffer)
|
2005-08-17 11:52:30 +02:00
|
|
|
{
|
2006-10-15 14:51:52 +02:00
|
|
|
TRACE("%p %d %p\n", phContext, ulAttribute, pBuffer);
|
2006-06-16 08:59:48 +02:00
|
|
|
if (!phContext)
|
|
|
|
return SEC_E_INVALID_HANDLE;
|
|
|
|
|
|
|
|
switch(ulAttribute)
|
2005-08-17 11:52:30 +02:00
|
|
|
{
|
2006-06-16 08:59:48 +02:00
|
|
|
#define _x(x) case (x) : FIXME(#x" stub\n"); break
|
|
|
|
_x(SECPKG_ATTR_ACCESS_TOKEN);
|
|
|
|
_x(SECPKG_ATTR_AUTHORITY);
|
|
|
|
_x(SECPKG_ATTR_DCE_INFO);
|
2006-08-13 14:44:39 +02:00
|
|
|
case SECPKG_ATTR_FLAGS:
|
|
|
|
{
|
|
|
|
PSecPkgContext_Flags spcf = (PSecPkgContext_Flags)pBuffer;
|
|
|
|
PNegoHelper helper = (PNegoHelper)phContext->dwLower;
|
|
|
|
|
|
|
|
spcf->Flags = 0;
|
|
|
|
if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
|
|
|
|
spcf->Flags |= ISC_RET_INTEGRITY;
|
|
|
|
if(helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
|
|
|
|
spcf->Flags |= ISC_RET_CONFIDENTIALITY;
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
2006-06-16 08:59:48 +02:00
|
|
|
_x(SECPKG_ATTR_KEY_INFO);
|
|
|
|
_x(SECPKG_ATTR_LIFESPAN);
|
|
|
|
_x(SECPKG_ATTR_NAMES);
|
|
|
|
_x(SECPKG_ATTR_NATIVE_NAMES);
|
|
|
|
_x(SECPKG_ATTR_NEGOTIATION_INFO);
|
|
|
|
_x(SECPKG_ATTR_PACKAGE_INFO);
|
|
|
|
_x(SECPKG_ATTR_PASSWORD_EXPIRY);
|
|
|
|
_x(SECPKG_ATTR_SESSION_KEY);
|
|
|
|
case SECPKG_ATTR_SIZES:
|
2006-08-13 14:44:39 +02:00
|
|
|
{
|
|
|
|
PSecPkgContext_Sizes spcs = (PSecPkgContext_Sizes)pBuffer;
|
|
|
|
spcs->cbMaxToken = NTLM_MAX_BUF;
|
|
|
|
spcs->cbMaxSignature = 16;
|
|
|
|
spcs->cbBlockSize = 0;
|
|
|
|
spcs->cbSecurityTrailer = 16;
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
2006-06-16 08:59:48 +02:00
|
|
|
_x(SECPKG_ATTR_STREAM_SIZES);
|
|
|
|
_x(SECPKG_ATTR_TARGET_INFORMATION);
|
|
|
|
#undef _x
|
|
|
|
default:
|
2006-10-15 14:51:52 +02:00
|
|
|
TRACE("Unknown value %d passed for ulAttribute\n", ulAttribute);
|
2005-08-17 11:52:30 +02:00
|
|
|
}
|
2006-06-16 08:59:48 +02:00
|
|
|
|
|
|
|
return SEC_E_UNSUPPORTED_FUNCTION;
|
2005-08-17 11:52:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* QueryContextAttributesA
|
|
|
|
*/
|
2012-05-10 13:06:20 +02:00
|
|
|
SECURITY_STATUS SEC_ENTRY ntlm_QueryContextAttributesA(PCtxtHandle phContext,
|
2006-10-15 14:51:52 +02:00
|
|
|
ULONG ulAttribute, void *pBuffer)
|
2005-08-17 11:52:30 +02:00
|
|
|
{
|
|
|
|
return ntlm_QueryContextAttributesW(phContext, ulAttribute, pBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* ImpersonateSecurityContext
|
|
|
|
*/
|
|
|
|
static SECURITY_STATUS SEC_ENTRY ntlm_ImpersonateSecurityContext(PCtxtHandle phContext)
|
|
|
|
{
|
|
|
|
SECURITY_STATUS ret;
|
|
|
|
|
|
|
|
TRACE("%p\n", phContext);
|
|
|
|
if (phContext)
|
|
|
|
{
|
|
|
|
ret = SEC_E_UNSUPPORTED_FUNCTION;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = SEC_E_INVALID_HANDLE;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RevertSecurityContext
|
|
|
|
*/
|
|
|
|
static SECURITY_STATUS SEC_ENTRY ntlm_RevertSecurityContext(PCtxtHandle phContext)
|
|
|
|
{
|
|
|
|
SECURITY_STATUS ret;
|
|
|
|
|
|
|
|
TRACE("%p\n", phContext);
|
|
|
|
if (phContext)
|
|
|
|
{
|
|
|
|
ret = SEC_E_UNSUPPORTED_FUNCTION;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = SEC_E_INVALID_HANDLE;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-11-08 12:10:37 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* ntlm_CreateSignature
|
|
|
|
* As both MakeSignature and VerifySignature need this, but different keys
|
2010-04-19 10:15:15 +02:00
|
|
|
* are needed for NTLM2, the logic goes into a helper function.
|
2006-11-09 11:31:31 +01:00
|
|
|
* To ensure maximal reusability, we can specify the direction as NTLM_SEND for
|
2016-05-16 21:38:02 +02:00
|
|
|
* signing/encrypting and NTLM_RECV for verifying/decrypting. When encrypting,
|
2006-11-09 11:31:31 +01:00
|
|
|
* the signature is encrypted after the message was encrypted, so
|
|
|
|
* CreateSignature shouldn't do it. In this case, encrypt_sig can be set to
|
|
|
|
* false.
|
2006-11-08 12:10:37 +01:00
|
|
|
*/
|
|
|
|
static SECURITY_STATUS ntlm_CreateSignature(PNegoHelper helper, PSecBufferDesc pMessage,
|
2006-11-09 11:31:31 +01:00
|
|
|
int token_idx, SignDirection direction, BOOL encrypt_sig)
|
2006-11-08 12:10:37 +01:00
|
|
|
{
|
|
|
|
ULONG sign_version = 1;
|
|
|
|
UINT i;
|
2006-11-08 21:53:23 +01:00
|
|
|
PBYTE sig;
|
2006-11-09 11:31:31 +01:00
|
|
|
TRACE("%p, %p, %d, %d, %d\n", helper, pMessage, token_idx, direction,
|
|
|
|
encrypt_sig);
|
2006-11-08 21:53:23 +01:00
|
|
|
|
|
|
|
sig = pMessage->pBuffers[token_idx].pvBuffer;
|
2006-11-08 12:10:37 +01:00
|
|
|
|
2006-11-08 21:53:23 +01:00
|
|
|
if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
|
2006-11-08 12:10:37 +01:00
|
|
|
helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
|
2005-08-17 11:52:30 +02:00
|
|
|
{
|
2006-11-08 21:53:23 +01:00
|
|
|
BYTE digest[16];
|
|
|
|
BYTE seq_no[4];
|
|
|
|
HMAC_MD5_CTX hmac_md5_ctx;
|
|
|
|
|
2006-11-09 11:31:31 +01:00
|
|
|
TRACE("Signing NTLM2 style\n");
|
2006-11-08 21:53:23 +01:00
|
|
|
|
|
|
|
if(direction == NTLM_SEND)
|
|
|
|
{
|
|
|
|
seq_no[0] = (helper->crypt.ntlm2.send_seq_no >> 0) & 0xff;
|
|
|
|
seq_no[1] = (helper->crypt.ntlm2.send_seq_no >> 8) & 0xff;
|
|
|
|
seq_no[2] = (helper->crypt.ntlm2.send_seq_no >> 16) & 0xff;
|
|
|
|
seq_no[3] = (helper->crypt.ntlm2.send_seq_no >> 24) & 0xff;
|
|
|
|
|
|
|
|
++(helper->crypt.ntlm2.send_seq_no);
|
|
|
|
|
|
|
|
HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.send_sign_key, 16);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
seq_no[0] = (helper->crypt.ntlm2.recv_seq_no >> 0) & 0xff;
|
|
|
|
seq_no[1] = (helper->crypt.ntlm2.recv_seq_no >> 8) & 0xff;
|
|
|
|
seq_no[2] = (helper->crypt.ntlm2.recv_seq_no >> 16) & 0xff;
|
|
|
|
seq_no[3] = (helper->crypt.ntlm2.recv_seq_no >> 24) & 0xff;
|
|
|
|
|
|
|
|
++(helper->crypt.ntlm2.recv_seq_no);
|
|
|
|
|
|
|
|
HMACMD5Init(&hmac_md5_ctx, helper->crypt.ntlm2.recv_sign_key, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
HMACMD5Update(&hmac_md5_ctx, seq_no, 4);
|
|
|
|
for( i = 0; i < pMessage->cBuffers; ++i )
|
|
|
|
{
|
|
|
|
if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
|
2009-01-22 09:52:28 +01:00
|
|
|
HMACMD5Update(&hmac_md5_ctx, pMessage->pBuffers[i].pvBuffer,
|
2006-11-08 21:53:23 +01:00
|
|
|
pMessage->pBuffers[i].cbBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
HMACMD5Final(&hmac_md5_ctx, digest);
|
|
|
|
|
2006-11-09 11:31:31 +01:00
|
|
|
if(encrypt_sig && helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
|
2006-11-08 21:53:23 +01:00
|
|
|
{
|
|
|
|
if(direction == NTLM_SEND)
|
|
|
|
SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i, digest, 8);
|
|
|
|
else
|
|
|
|
SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i, digest, 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The NTLM2 signature is the sign version */
|
|
|
|
sig[ 0] = (sign_version >> 0) & 0xff;
|
|
|
|
sig[ 1] = (sign_version >> 8) & 0xff;
|
|
|
|
sig[ 2] = (sign_version >> 16) & 0xff;
|
|
|
|
sig[ 3] = (sign_version >> 24) & 0xff;
|
|
|
|
/* The first 8 bytes of the digest */
|
|
|
|
memcpy(sig+4, digest, 8);
|
|
|
|
/* And the sequence number */
|
|
|
|
memcpy(sig+12, seq_no, 4);
|
|
|
|
|
|
|
|
pMessage->pBuffers[token_idx].cbBuffer = 16;
|
|
|
|
|
|
|
|
return SEC_E_OK;
|
2005-08-17 11:52:30 +02:00
|
|
|
}
|
2006-08-12 19:19:49 +02:00
|
|
|
if(helper->neg_flags & NTLMSSP_NEGOTIATE_SIGN)
|
2005-08-17 11:52:30 +02:00
|
|
|
{
|
2006-11-04 02:52:18 +01:00
|
|
|
ULONG crc = 0U;
|
2006-11-09 11:31:31 +01:00
|
|
|
TRACE("Signing NTLM1 style\n");
|
2006-11-04 02:52:18 +01:00
|
|
|
|
|
|
|
for(i=0; i < pMessage->cBuffers; ++i)
|
|
|
|
{
|
|
|
|
if(pMessage->pBuffers[i].BufferType & SECBUFFER_DATA)
|
|
|
|
{
|
|
|
|
crc = ComputeCrc32(pMessage->pBuffers[i].pvBuffer,
|
|
|
|
pMessage->pBuffers[i].cbBuffer, crc);
|
|
|
|
}
|
|
|
|
}
|
2006-08-17 02:11:39 +02:00
|
|
|
|
|
|
|
sig[ 0] = (sign_version >> 0) & 0xff;
|
|
|
|
sig[ 1] = (sign_version >> 8) & 0xff;
|
|
|
|
sig[ 2] = (sign_version >> 16) & 0xff;
|
|
|
|
sig[ 3] = (sign_version >> 24) & 0xff;
|
|
|
|
memset(sig+4, 0, 4);
|
|
|
|
sig[ 8] = (crc >> 0) & 0xff;
|
|
|
|
sig[ 9] = (crc >> 8) & 0xff;
|
|
|
|
sig[10] = (crc >> 16) & 0xff;
|
|
|
|
sig[11] = (crc >> 24) & 0xff;
|
|
|
|
sig[12] = (helper->crypt.ntlm.seq_num >> 0) & 0xff;
|
|
|
|
sig[13] = (helper->crypt.ntlm.seq_num >> 8) & 0xff;
|
|
|
|
sig[14] = (helper->crypt.ntlm.seq_num >> 16) & 0xff;
|
|
|
|
sig[15] = (helper->crypt.ntlm.seq_num >> 24) & 0xff;
|
|
|
|
|
|
|
|
++(helper->crypt.ntlm.seq_num);
|
|
|
|
|
2006-11-09 11:31:31 +01:00
|
|
|
if(encrypt_sig)
|
|
|
|
SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
|
2006-08-17 02:11:39 +02:00
|
|
|
return SEC_E_OK;
|
2005-08-17 11:52:30 +02:00
|
|
|
}
|
2006-08-12 19:19:49 +02:00
|
|
|
|
2006-08-17 21:05:14 +02:00
|
|
|
if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
|
2006-08-12 19:19:49 +02:00
|
|
|
{
|
2006-11-08 12:10:37 +01:00
|
|
|
TRACE("Creating a dummy signature.\n");
|
2006-08-12 19:19:49 +02:00
|
|
|
/* A dummy signature is 0x01 followed by 15 bytes of 0x00 */
|
2006-11-04 02:52:18 +01:00
|
|
|
memset(pMessage->pBuffers[token_idx].pvBuffer, 0, 16);
|
|
|
|
memset(pMessage->pBuffers[token_idx].pvBuffer, 0x01, 1);
|
|
|
|
pMessage->pBuffers[token_idx].cbBuffer = 16;
|
2006-08-12 19:19:49 +02:00
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SEC_E_UNSUPPORTED_FUNCTION;
|
2005-08-17 11:52:30 +02:00
|
|
|
}
|
|
|
|
|
2006-11-08 12:10:37 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* MakeSignature
|
|
|
|
*/
|
2012-05-10 13:06:20 +02:00
|
|
|
SECURITY_STATUS SEC_ENTRY ntlm_MakeSignature(PCtxtHandle phContext,
|
|
|
|
ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
|
2006-11-08 12:10:37 +01:00
|
|
|
{
|
|
|
|
PNegoHelper helper;
|
|
|
|
int token_idx;
|
|
|
|
|
|
|
|
TRACE("%p %d %p %d\n", phContext, fQOP, pMessage, MessageSeqNo);
|
|
|
|
if (!phContext)
|
|
|
|
return SEC_E_INVALID_HANDLE;
|
|
|
|
|
|
|
|
if(fQOP)
|
|
|
|
FIXME("Ignoring fQOP 0x%08x\n", fQOP);
|
|
|
|
|
|
|
|
if(MessageSeqNo)
|
|
|
|
FIXME("Ignoring MessageSeqNo\n");
|
|
|
|
|
|
|
|
if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
|
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
|
|
|
|
/* If we didn't find a SECBUFFER_TOKEN type buffer */
|
|
|
|
if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
|
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
|
|
|
|
if(pMessage->pBuffers[token_idx].cbBuffer < 16)
|
|
|
|
return SEC_E_BUFFER_TOO_SMALL;
|
|
|
|
|
|
|
|
helper = (PNegoHelper)phContext->dwLower;
|
2010-05-11 21:28:42 +02:00
|
|
|
TRACE("Negotiated flags are: 0x%08x\n", helper->neg_flags);
|
2006-11-08 12:10:37 +01:00
|
|
|
|
2006-11-09 11:31:31 +01:00
|
|
|
return ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, TRUE);
|
2006-11-08 12:10:37 +01:00
|
|
|
}
|
|
|
|
|
2005-08-17 11:52:30 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* VerifySignature
|
|
|
|
*/
|
2012-05-10 13:06:20 +02:00
|
|
|
SECURITY_STATUS SEC_ENTRY ntlm_VerifySignature(PCtxtHandle phContext,
|
|
|
|
PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
|
2005-08-17 11:52:30 +02:00
|
|
|
{
|
2006-08-13 12:46:00 +02:00
|
|
|
PNegoHelper helper;
|
2006-11-04 02:52:18 +01:00
|
|
|
UINT i;
|
2006-11-08 12:10:37 +01:00
|
|
|
int token_idx;
|
2006-11-04 12:30:30 +01:00
|
|
|
SECURITY_STATUS ret;
|
2006-11-08 12:10:37 +01:00
|
|
|
SecBufferDesc local_desc;
|
|
|
|
PSecBuffer local_buff;
|
|
|
|
BYTE local_sig[16];
|
2005-08-17 11:52:30 +02:00
|
|
|
|
2006-10-15 14:51:52 +02:00
|
|
|
TRACE("%p %p %d %p\n", phContext, pMessage, MessageSeqNo, pfQOP);
|
2006-08-13 12:46:00 +02:00
|
|
|
if(!phContext)
|
|
|
|
return SEC_E_INVALID_HANDLE;
|
|
|
|
|
2006-11-04 02:52:18 +01:00
|
|
|
if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
|
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
|
2006-11-08 12:10:37 +01:00
|
|
|
if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
|
2006-08-13 12:46:00 +02:00
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
|
2006-11-04 02:52:18 +01:00
|
|
|
if(pMessage->pBuffers[token_idx].cbBuffer < 16)
|
2006-08-13 12:46:00 +02:00
|
|
|
return SEC_E_BUFFER_TOO_SMALL;
|
|
|
|
|
|
|
|
if(MessageSeqNo)
|
|
|
|
FIXME("Ignoring MessageSeqNo\n");
|
|
|
|
|
|
|
|
helper = (PNegoHelper)phContext->dwLower;
|
2010-05-11 21:28:42 +02:00
|
|
|
TRACE("Negotiated flags: 0x%08x\n", helper->neg_flags);
|
2006-08-13 12:46:00 +02:00
|
|
|
|
2006-11-08 12:10:37 +01:00
|
|
|
local_buff = HeapAlloc(GetProcessHeap(), 0, pMessage->cBuffers * sizeof(SecBuffer));
|
2006-08-13 12:46:00 +02:00
|
|
|
|
2006-11-08 12:10:37 +01:00
|
|
|
local_desc.ulVersion = SECBUFFER_VERSION;
|
|
|
|
local_desc.cBuffers = pMessage->cBuffers;
|
|
|
|
local_desc.pBuffers = local_buff;
|
2006-08-17 02:11:39 +02:00
|
|
|
|
2006-11-08 12:10:37 +01:00
|
|
|
for(i=0; i < pMessage->cBuffers; ++i)
|
2006-08-13 12:46:00 +02:00
|
|
|
{
|
2006-11-08 12:10:37 +01:00
|
|
|
if(pMessage->pBuffers[i].BufferType == SECBUFFER_TOKEN)
|
2006-08-13 12:46:00 +02:00
|
|
|
{
|
2006-11-08 12:10:37 +01:00
|
|
|
local_buff[i].BufferType = SECBUFFER_TOKEN;
|
|
|
|
local_buff[i].cbBuffer = 16;
|
|
|
|
local_buff[i].pvBuffer = local_sig;
|
2006-08-13 12:46:00 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-11-08 12:10:37 +01:00
|
|
|
local_buff[i].BufferType = pMessage->pBuffers[i].BufferType;
|
|
|
|
local_buff[i].cbBuffer = pMessage->pBuffers[i].cbBuffer;
|
|
|
|
local_buff[i].pvBuffer = pMessage->pBuffers[i].pvBuffer;
|
2006-08-13 12:46:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-09 11:31:31 +01:00
|
|
|
if((ret = ntlm_CreateSignature(helper, &local_desc, token_idx, NTLM_RECV, TRUE)) != SEC_E_OK)
|
2006-11-08 12:10:37 +01:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
if(memcmp(((PBYTE)local_buff[token_idx].pvBuffer) + 8,
|
|
|
|
((PBYTE)pMessage->pBuffers[token_idx].pvBuffer) + 8, 8))
|
|
|
|
ret = SEC_E_MESSAGE_ALTERED;
|
|
|
|
else
|
|
|
|
ret = SEC_E_OK;
|
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, local_buff);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
2005-08-17 11:52:30 +02:00
|
|
|
}
|
|
|
|
|
2005-12-06 21:22:57 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* FreeCredentialsHandle
|
|
|
|
*/
|
2012-05-10 13:06:20 +02:00
|
|
|
SECURITY_STATUS SEC_ENTRY ntlm_FreeCredentialsHandle(PCredHandle phCredential)
|
2005-12-06 21:22:57 +01:00
|
|
|
{
|
|
|
|
SECURITY_STATUS ret;
|
2005-08-17 11:52:30 +02:00
|
|
|
|
2005-12-06 21:22:57 +01:00
|
|
|
if(phCredential){
|
2007-08-07 18:12:32 +02:00
|
|
|
PNtlmCredentials ntlm_cred = (PNtlmCredentials) phCredential->dwLower;
|
2005-12-06 21:22:57 +01:00
|
|
|
phCredential->dwUpper = 0;
|
|
|
|
phCredential->dwLower = 0;
|
2007-08-07 18:12:32 +02:00
|
|
|
if (ntlm_cred->password)
|
|
|
|
memset(ntlm_cred->password, 0, ntlm_cred->pwlen);
|
|
|
|
HeapFree(GetProcessHeap(), 0, ntlm_cred->password);
|
|
|
|
HeapFree(GetProcessHeap(), 0, ntlm_cred->username_arg);
|
|
|
|
HeapFree(GetProcessHeap(), 0, ntlm_cred->domain_arg);
|
2009-12-13 20:45:38 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, ntlm_cred);
|
2005-12-06 21:22:57 +01:00
|
|
|
ret = SEC_E_OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = SEC_E_OK;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2005-08-17 11:52:30 +02:00
|
|
|
|
2006-06-02 00:50:32 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* EncryptMessage
|
|
|
|
*/
|
2012-05-10 13:06:20 +02:00
|
|
|
SECURITY_STATUS SEC_ENTRY ntlm_EncryptMessage(PCtxtHandle phContext,
|
|
|
|
ULONG fQOP, PSecBufferDesc pMessage, ULONG MessageSeqNo)
|
2006-06-02 00:50:32 +02:00
|
|
|
{
|
2006-08-19 20:02:16 +02:00
|
|
|
PNegoHelper helper;
|
2007-10-01 11:57:02 +02:00
|
|
|
int token_idx, data_idx;
|
2006-11-04 02:52:18 +01:00
|
|
|
|
2006-10-15 14:51:52 +02:00
|
|
|
TRACE("(%p %d %p %d)\n", phContext, fQOP, pMessage, MessageSeqNo);
|
2006-06-02 00:50:32 +02:00
|
|
|
|
|
|
|
if(!phContext)
|
|
|
|
return SEC_E_INVALID_HANDLE;
|
|
|
|
|
2006-08-19 20:02:16 +02:00
|
|
|
if(fQOP)
|
|
|
|
FIXME("Ignoring fQOP\n");
|
|
|
|
|
|
|
|
if(MessageSeqNo)
|
|
|
|
FIXME("Ignoring MessageSeqNo\n");
|
|
|
|
|
2006-11-04 02:52:18 +01:00
|
|
|
if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
|
2006-08-19 20:02:16 +02:00
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
|
2006-11-09 11:31:31 +01:00
|
|
|
if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
|
2006-11-04 02:52:18 +01:00
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
|
2007-10-01 11:57:02 +02:00
|
|
|
if((data_idx = ntlm_GetDataBufferIndex(pMessage)) ==-1 )
|
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
|
2006-11-04 02:52:18 +01:00
|
|
|
if(pMessage->pBuffers[token_idx].cbBuffer < 16)
|
2006-08-19 20:02:16 +02:00
|
|
|
return SEC_E_BUFFER_TOO_SMALL;
|
|
|
|
|
|
|
|
helper = (PNegoHelper) phContext->dwLower;
|
|
|
|
|
2006-11-09 11:31:31 +01:00
|
|
|
if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 &&
|
|
|
|
helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
|
|
|
|
{
|
|
|
|
ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
|
|
|
|
SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
|
2009-01-22 09:52:28 +01:00
|
|
|
pMessage->pBuffers[data_idx].pvBuffer,
|
2007-10-01 11:57:02 +02:00
|
|
|
pMessage->pBuffers[data_idx].cbBuffer);
|
2006-11-09 11:31:31 +01:00
|
|
|
|
|
|
|
if(helper->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCHANGE)
|
|
|
|
SECUR32_arc4Process(helper->crypt.ntlm2.send_a4i,
|
|
|
|
((BYTE *)pMessage->pBuffers[token_idx].pvBuffer)+4, 8);
|
2006-08-19 20:02:16 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-11-09 11:31:31 +01:00
|
|
|
PBYTE sig;
|
|
|
|
ULONG save_flags;
|
2006-08-19 20:02:16 +02:00
|
|
|
|
2006-11-09 11:31:31 +01:00
|
|
|
/* EncryptMessage always produces real signatures, so make sure
|
|
|
|
* NTLMSSP_NEGOTIATE_SIGN is set*/
|
|
|
|
save_flags = helper->neg_flags;
|
|
|
|
helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
|
|
|
|
ntlm_CreateSignature(helper, pMessage, token_idx, NTLM_SEND, FALSE);
|
|
|
|
helper->neg_flags = save_flags;
|
2006-11-04 02:52:18 +01:00
|
|
|
|
2006-11-09 11:31:31 +01:00
|
|
|
sig = pMessage->pBuffers[token_idx].pvBuffer;
|
2006-08-19 20:02:16 +02:00
|
|
|
|
2007-10-01 11:57:02 +02:00
|
|
|
SECUR32_arc4Process(helper->crypt.ntlm.a4i,
|
|
|
|
pMessage->pBuffers[data_idx].pvBuffer,
|
|
|
|
pMessage->pBuffers[data_idx].cbBuffer);
|
2006-08-19 20:02:16 +02:00
|
|
|
SECUR32_arc4Process(helper->crypt.ntlm.a4i, sig+4, 12);
|
|
|
|
|
|
|
|
if(helper->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN || helper->neg_flags == 0)
|
|
|
|
memset(sig+4, 0, 4);
|
|
|
|
}
|
|
|
|
return SEC_E_OK;
|
2006-06-02 00:50:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* DecryptMessage
|
|
|
|
*/
|
2012-05-10 13:06:20 +02:00
|
|
|
SECURITY_STATUS SEC_ENTRY ntlm_DecryptMessage(PCtxtHandle phContext,
|
|
|
|
PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
|
2006-06-02 00:50:32 +02:00
|
|
|
{
|
2006-09-07 17:32:40 +02:00
|
|
|
SECURITY_STATUS ret;
|
|
|
|
ULONG ntlmssp_flags_save;
|
2006-08-19 20:02:16 +02:00
|
|
|
PNegoHelper helper;
|
2007-10-01 11:57:02 +02:00
|
|
|
int token_idx, data_idx;
|
2006-10-15 14:51:52 +02:00
|
|
|
TRACE("(%p %p %d %p)\n", phContext, pMessage, MessageSeqNo, pfQOP);
|
2006-06-02 00:50:32 +02:00
|
|
|
|
|
|
|
if(!phContext)
|
|
|
|
return SEC_E_INVALID_HANDLE;
|
|
|
|
|
2006-08-19 20:02:16 +02:00
|
|
|
if(MessageSeqNo)
|
|
|
|
FIXME("Ignoring MessageSeqNo\n");
|
|
|
|
|
2006-11-04 02:52:18 +01:00
|
|
|
if(!pMessage || !pMessage->pBuffers || pMessage->cBuffers < 2)
|
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
|
2006-11-09 11:31:31 +01:00
|
|
|
if((token_idx = ntlm_GetTokenBufferIndex(pMessage)) == -1)
|
2006-08-19 20:02:16 +02:00
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
|
2007-10-01 11:57:02 +02:00
|
|
|
if((data_idx = ntlm_GetDataBufferIndex(pMessage)) ==-1)
|
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
|
2006-11-04 02:52:18 +01:00
|
|
|
if(pMessage->pBuffers[token_idx].cbBuffer < 16)
|
2006-08-19 20:02:16 +02:00
|
|
|
return SEC_E_BUFFER_TOO_SMALL;
|
|
|
|
|
|
|
|
helper = (PNegoHelper) phContext->dwLower;
|
|
|
|
|
2006-11-09 11:31:31 +01:00
|
|
|
if(helper->neg_flags & NTLMSSP_NEGOTIATE_NTLM2 && helper->neg_flags & NTLMSSP_NEGOTIATE_SEAL)
|
2006-08-19 20:02:16 +02:00
|
|
|
{
|
2006-11-09 11:31:31 +01:00
|
|
|
SECUR32_arc4Process(helper->crypt.ntlm2.recv_a4i,
|
2007-10-01 11:57:02 +02:00
|
|
|
pMessage->pBuffers[data_idx].pvBuffer,
|
|
|
|
pMessage->pBuffers[data_idx].cbBuffer);
|
2006-08-19 20:02:16 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SECUR32_arc4Process(helper->crypt.ntlm.a4i,
|
2007-10-01 11:57:02 +02:00
|
|
|
pMessage->pBuffers[data_idx].pvBuffer,
|
|
|
|
pMessage->pBuffers[data_idx].cbBuffer);
|
2006-08-19 20:02:16 +02:00
|
|
|
}
|
|
|
|
|
2006-09-07 17:32:40 +02:00
|
|
|
/* Make sure we use a session key for the signature check, EncryptMessage
|
|
|
|
* always does that, even in the dummy case */
|
|
|
|
ntlmssp_flags_save = helper->neg_flags;
|
|
|
|
|
|
|
|
helper->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
|
|
|
|
ret = ntlm_VerifySignature(phContext, pMessage, MessageSeqNo, pfQOP);
|
|
|
|
|
|
|
|
helper->neg_flags = ntlmssp_flags_save;
|
|
|
|
|
|
|
|
return ret;
|
2006-06-02 00:50:32 +02:00
|
|
|
}
|
|
|
|
|
2006-12-14 15:47:50 +01:00
|
|
|
static const SecurityFunctionTableA ntlmTableA = {
|
2005-08-17 11:52:30 +02:00
|
|
|
1,
|
|
|
|
NULL, /* EnumerateSecurityPackagesA */
|
|
|
|
ntlm_QueryCredentialsAttributesA, /* QueryCredentialsAttributesA */
|
|
|
|
ntlm_AcquireCredentialsHandleA, /* AcquireCredentialsHandleA */
|
2005-12-06 21:22:57 +01:00
|
|
|
ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
|
2005-08-17 11:52:30 +02:00
|
|
|
NULL, /* Reserved2 */
|
|
|
|
ntlm_InitializeSecurityContextA, /* InitializeSecurityContextA */
|
|
|
|
ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
|
|
|
|
ntlm_CompleteAuthToken, /* CompleteAuthToken */
|
|
|
|
ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
|
2005-12-06 21:22:57 +01:00
|
|
|
NULL, /* ApplyControlToken */
|
2005-08-17 11:52:30 +02:00
|
|
|
ntlm_QueryContextAttributesA, /* QueryContextAttributesA */
|
|
|
|
ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
|
|
|
|
ntlm_RevertSecurityContext, /* RevertSecurityContext */
|
|
|
|
ntlm_MakeSignature, /* MakeSignature */
|
|
|
|
ntlm_VerifySignature, /* VerifySignature */
|
|
|
|
FreeContextBuffer, /* FreeContextBuffer */
|
|
|
|
NULL, /* QuerySecurityPackageInfoA */
|
|
|
|
NULL, /* Reserved3 */
|
|
|
|
NULL, /* Reserved4 */
|
|
|
|
NULL, /* ExportSecurityContext */
|
|
|
|
NULL, /* ImportSecurityContextA */
|
|
|
|
NULL, /* AddCredentialsA */
|
|
|
|
NULL, /* Reserved8 */
|
|
|
|
NULL, /* QuerySecurityContextToken */
|
2006-06-02 00:50:32 +02:00
|
|
|
ntlm_EncryptMessage, /* EncryptMessage */
|
|
|
|
ntlm_DecryptMessage, /* DecryptMessage */
|
2005-08-17 11:52:30 +02:00
|
|
|
NULL, /* SetContextAttributesA */
|
|
|
|
};
|
|
|
|
|
2006-12-14 15:47:50 +01:00
|
|
|
static const SecurityFunctionTableW ntlmTableW = {
|
2005-08-17 11:52:30 +02:00
|
|
|
1,
|
|
|
|
NULL, /* EnumerateSecurityPackagesW */
|
|
|
|
ntlm_QueryCredentialsAttributesW, /* QueryCredentialsAttributesW */
|
|
|
|
ntlm_AcquireCredentialsHandleW, /* AcquireCredentialsHandleW */
|
2005-12-06 21:22:57 +01:00
|
|
|
ntlm_FreeCredentialsHandle, /* FreeCredentialsHandle */
|
2005-08-17 11:52:30 +02:00
|
|
|
NULL, /* Reserved2 */
|
|
|
|
ntlm_InitializeSecurityContextW, /* InitializeSecurityContextW */
|
|
|
|
ntlm_AcceptSecurityContext, /* AcceptSecurityContext */
|
|
|
|
ntlm_CompleteAuthToken, /* CompleteAuthToken */
|
|
|
|
ntlm_DeleteSecurityContext, /* DeleteSecurityContext */
|
2005-12-06 21:22:57 +01:00
|
|
|
NULL, /* ApplyControlToken */
|
2005-08-17 11:52:30 +02:00
|
|
|
ntlm_QueryContextAttributesW, /* QueryContextAttributesW */
|
|
|
|
ntlm_ImpersonateSecurityContext, /* ImpersonateSecurityContext */
|
|
|
|
ntlm_RevertSecurityContext, /* RevertSecurityContext */
|
|
|
|
ntlm_MakeSignature, /* MakeSignature */
|
|
|
|
ntlm_VerifySignature, /* VerifySignature */
|
|
|
|
FreeContextBuffer, /* FreeContextBuffer */
|
|
|
|
NULL, /* QuerySecurityPackageInfoW */
|
|
|
|
NULL, /* Reserved3 */
|
|
|
|
NULL, /* Reserved4 */
|
|
|
|
NULL, /* ExportSecurityContext */
|
|
|
|
NULL, /* ImportSecurityContextW */
|
|
|
|
NULL, /* AddCredentialsW */
|
|
|
|
NULL, /* Reserved8 */
|
|
|
|
NULL, /* QuerySecurityContextToken */
|
2006-06-02 00:50:32 +02:00
|
|
|
ntlm_EncryptMessage, /* EncryptMessage */
|
|
|
|
ntlm_DecryptMessage, /* DecryptMessage */
|
2005-08-17 11:52:30 +02:00
|
|
|
NULL, /* SetContextAttributesW */
|
|
|
|
};
|
|
|
|
|
2006-05-19 11:46:47 +02:00
|
|
|
#define NTLM_COMMENT \
|
|
|
|
{ 'N', 'T', 'L', 'M', ' ', \
|
|
|
|
'S', 'e', 'c', 'u', 'r', 'i', 't', 'y', ' ', \
|
|
|
|
'P', 'a', 'c', 'k', 'a', 'g', 'e', 0}
|
|
|
|
|
|
|
|
static CHAR ntlm_comment_A[] = NTLM_COMMENT;
|
|
|
|
static WCHAR ntlm_comment_W[] = NTLM_COMMENT;
|
|
|
|
|
|
|
|
#define NTLM_NAME {'N', 'T', 'L', 'M', 0}
|
2005-08-17 11:52:30 +02:00
|
|
|
|
2006-05-19 11:46:47 +02:00
|
|
|
static char ntlm_name_A[] = NTLM_NAME;
|
|
|
|
static WCHAR ntlm_name_W[] = NTLM_NAME;
|
|
|
|
|
|
|
|
/* According to Windows, NTLM has the following capabilities. */
|
|
|
|
#define CAPS ( \
|
2012-05-10 13:05:26 +02:00
|
|
|
SECPKG_FLAG_INTEGRITY | \
|
|
|
|
SECPKG_FLAG_PRIVACY | \
|
|
|
|
SECPKG_FLAG_TOKEN_ONLY | \
|
|
|
|
SECPKG_FLAG_CONNECTION | \
|
|
|
|
SECPKG_FLAG_MULTI_REQUIRED | \
|
|
|
|
SECPKG_FLAG_IMPERSONATION | \
|
|
|
|
SECPKG_FLAG_ACCEPT_WIN32_NAME | \
|
|
|
|
SECPKG_FLAG_NEGOTIABLE | \
|
|
|
|
SECPKG_FLAG_LOGON | \
|
|
|
|
SECPKG_FLAG_RESTRICTED_TOKENS )
|
2006-05-19 11:46:47 +02:00
|
|
|
|
|
|
|
static const SecPkgInfoW infoW = {
|
|
|
|
CAPS,
|
|
|
|
1,
|
|
|
|
RPC_C_AUTHN_WINNT,
|
|
|
|
NTLM_MAX_BUF,
|
|
|
|
ntlm_name_W,
|
|
|
|
ntlm_comment_W
|
|
|
|
};
|
|
|
|
|
|
|
|
static const SecPkgInfoA infoA = {
|
|
|
|
CAPS,
|
|
|
|
1,
|
|
|
|
RPC_C_AUTHN_WINNT,
|
|
|
|
NTLM_MAX_BUF,
|
|
|
|
ntlm_name_A,
|
|
|
|
ntlm_comment_A
|
|
|
|
};
|
2005-08-17 11:52:30 +02:00
|
|
|
|
2012-05-10 13:06:20 +02:00
|
|
|
SecPkgInfoA *ntlm_package_infoA = (SecPkgInfoA *)&infoA;
|
|
|
|
SecPkgInfoW *ntlm_package_infoW = (SecPkgInfoW *)&infoW;
|
2009-12-29 16:36:18 +01:00
|
|
|
|
2005-08-17 11:52:30 +02:00
|
|
|
void SECUR32_initNTLMSP(void)
|
2006-07-17 23:41:12 +02:00
|
|
|
{
|
2005-12-06 21:22:57 +01:00
|
|
|
PNegoHelper helper;
|
2006-11-02 17:50:40 +01:00
|
|
|
static CHAR version[] = "--version";
|
2005-12-06 21:22:57 +01:00
|
|
|
|
|
|
|
SEC_CHAR *args[] = {
|
2006-07-17 23:41:12 +02:00
|
|
|
ntlm_auth,
|
|
|
|
version,
|
2005-12-06 21:22:57 +01:00
|
|
|
NULL };
|
|
|
|
|
2008-04-24 22:43:45 +02:00
|
|
|
if(fork_helper(&helper, ntlm_auth, args) != SEC_E_OK)
|
2011-11-30 02:16:32 +01:00
|
|
|
helper = NULL;
|
2005-12-06 21:22:57 +01:00
|
|
|
else
|
|
|
|
check_version(helper);
|
|
|
|
|
2011-11-30 02:16:32 +01:00
|
|
|
if( helper &&
|
|
|
|
((helper->major > MIN_NTLM_AUTH_MAJOR_VERSION) ||
|
|
|
|
(helper->major == MIN_NTLM_AUTH_MAJOR_VERSION &&
|
|
|
|
helper->minor > MIN_NTLM_AUTH_MINOR_VERSION) ||
|
|
|
|
(helper->major == MIN_NTLM_AUTH_MAJOR_VERSION &&
|
|
|
|
helper->minor == MIN_NTLM_AUTH_MINOR_VERSION &&
|
|
|
|
helper->micro >= MIN_NTLM_AUTH_MICRO_VERSION)) )
|
2005-12-06 21:22:57 +01:00
|
|
|
{
|
2006-05-19 11:46:47 +02:00
|
|
|
SecureProvider *provider = SECUR32_addProvider(&ntlmTableA, &ntlmTableW, NULL);
|
2012-05-10 13:06:20 +02:00
|
|
|
SECUR32_addPackages(provider, 1L, ntlm_package_infoA, ntlm_package_infoW);
|
2005-12-06 21:22:57 +01:00
|
|
|
}
|
2006-09-17 11:27:03 +02:00
|
|
|
else
|
|
|
|
{
|
2011-10-26 20:05:00 +02:00
|
|
|
ERR_(winediag)("%s was not found or is outdated. "
|
|
|
|
"Make sure that ntlm_auth >= %d.%d.%d is in your path. "
|
|
|
|
"Usually, you can find it in the winbind package of your distribution.\n",
|
|
|
|
ntlm_auth,
|
|
|
|
MIN_NTLM_AUTH_MAJOR_VERSION,
|
|
|
|
MIN_NTLM_AUTH_MINOR_VERSION,
|
|
|
|
MIN_NTLM_AUTH_MICRO_VERSION);
|
2007-08-29 22:37:27 +02:00
|
|
|
|
2006-09-17 11:27:03 +02:00
|
|
|
}
|
2005-12-06 21:22:57 +01:00
|
|
|
cleanup_helper(helper);
|
2005-08-17 11:52:30 +02:00
|
|
|
}
|