2012-11-15 19:00:07 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* NTLM over HTTP
|
|
|
|
*
|
|
|
|
* Copyright 2012 Fujitsu Technology Solutions GmbH
|
|
|
|
* Copyright 2012 Dmitrij Jasnov <dmitrij.jasnov@ts.fujitsu.com>
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/tchar.h>
|
|
|
|
#include <winpr/dsparse.h>
|
|
|
|
|
2014-09-12 16:36:29 +04:00
|
|
|
#include <freerdp/log.h>
|
2012-11-15 19:00:07 +04:00
|
|
|
#include <openssl/rand.h>
|
|
|
|
|
|
|
|
#include "http.h"
|
|
|
|
|
|
|
|
#include "ntlm.h"
|
|
|
|
|
2014-09-12 16:36:29 +04:00
|
|
|
#define TAG FREERDP_TAG("core.gateway.ntlm")
|
|
|
|
|
2013-01-09 09:20:08 +04:00
|
|
|
BOOL ntlm_client_init(rdpNtlm* ntlm, BOOL http, char* user, char* domain, char* password, SecPkgContext_Bindings* Bindings)
|
2012-11-15 19:00:07 +04:00
|
|
|
{
|
|
|
|
SECURITY_STATUS status;
|
|
|
|
|
2013-01-09 09:20:08 +04:00
|
|
|
ntlm->http = http;
|
|
|
|
ntlm->Bindings = Bindings;
|
|
|
|
|
2015-01-29 22:07:02 +03:00
|
|
|
ntlm->table = InitSecurityInterfaceEx(SSPI_INTERFACE_WINPR);
|
2012-11-15 19:00:07 +04:00
|
|
|
|
2015-02-02 19:50:56 +03:00
|
|
|
if (!ntlm->table)
|
|
|
|
return FALSE;
|
|
|
|
|
2012-11-15 19:00:07 +04:00
|
|
|
sspi_SetAuthIdentity(&(ntlm->identity), user, domain, password);
|
|
|
|
|
|
|
|
status = ntlm->table->QuerySecurityPackageInfo(NTLMSP_NAME, &ntlm->pPackageInfo);
|
|
|
|
|
|
|
|
if (status != SEC_E_OK)
|
|
|
|
{
|
2015-01-29 22:07:02 +03:00
|
|
|
WLog_ERR(TAG, "QuerySecurityPackageInfo status: 0x%08X", status);
|
2012-11-15 19:00:07 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ntlm->cbMaxToken = ntlm->pPackageInfo->cbMaxToken;
|
|
|
|
|
|
|
|
status = ntlm->table->AcquireCredentialsHandle(NULL, NTLMSP_NAME,
|
2015-01-29 22:07:02 +03:00
|
|
|
SECPKG_CRED_OUTBOUND, NULL, &ntlm->identity, NULL, NULL,
|
|
|
|
&ntlm->credentials, &ntlm->expiration);
|
2012-11-15 19:00:07 +04:00
|
|
|
|
|
|
|
if (status != SEC_E_OK)
|
|
|
|
{
|
2015-01-29 22:07:02 +03:00
|
|
|
WLog_ERR(TAG, "AcquireCredentialsHandle status: 0x%08X", status);
|
2012-11-15 19:00:07 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ntlm->haveContext = FALSE;
|
|
|
|
ntlm->haveInputBuffer = FALSE;
|
|
|
|
ZeroMemory(&ntlm->inputBuffer, sizeof(SecBuffer));
|
|
|
|
ZeroMemory(&ntlm->outputBuffer, sizeof(SecBuffer));
|
|
|
|
ZeroMemory(&ntlm->ContextSizes, sizeof(SecPkgContext_Sizes));
|
|
|
|
|
|
|
|
ntlm->fContextReq = 0;
|
|
|
|
|
2013-01-09 09:20:08 +04:00
|
|
|
if (ntlm->http)
|
2012-11-15 19:00:07 +04:00
|
|
|
{
|
|
|
|
/* flags for HTTP authentication */
|
|
|
|
ntlm->fContextReq |= ISC_REQ_CONFIDENTIALITY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* flags for RPC authentication:
|
|
|
|
* RPC_C_AUTHN_LEVEL_PKT_INTEGRITY:
|
|
|
|
* ISC_REQ_USE_DCE_STYLE | ISC_REQ_DELEGATE | ISC_REQ_MUTUAL_AUTH |
|
|
|
|
* ISC_REQ_REPLAY_DETECT | ISC_REQ_SEQUENCE_DETECT
|
|
|
|
*/
|
|
|
|
|
|
|
|
ntlm->fContextReq |= ISC_REQ_USE_DCE_STYLE;
|
|
|
|
ntlm->fContextReq |= ISC_REQ_DELEGATE | ISC_REQ_MUTUAL_AUTH;
|
|
|
|
ntlm->fContextReq |= ISC_REQ_REPLAY_DETECT | ISC_REQ_SEQUENCE_DETECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL ntlm_client_make_spn(rdpNtlm* ntlm, LPCTSTR ServiceClass, char* hostname)
|
|
|
|
{
|
2015-01-29 22:07:02 +03:00
|
|
|
DWORD SpnLength = 0;
|
|
|
|
LPTSTR hostnameX = NULL;
|
2012-11-15 19:00:07 +04:00
|
|
|
|
|
|
|
#ifdef UNICODE
|
2015-01-29 22:07:02 +03:00
|
|
|
ConvertToUnicode(CP_UTF8, 0, hostname, -1, (LPWSTR*) &hostnameX, 0);
|
2012-11-15 19:00:07 +04:00
|
|
|
#else
|
2015-01-29 22:07:02 +03:00
|
|
|
hostnameX = _strdup(hostname);
|
2012-11-15 19:00:07 +04:00
|
|
|
#endif
|
|
|
|
|
2015-01-29 22:07:02 +03:00
|
|
|
if (!hostnameX)
|
|
|
|
return FALSE;
|
|
|
|
|
2012-11-28 01:37:56 +04:00
|
|
|
if (!ServiceClass)
|
|
|
|
{
|
|
|
|
ntlm->ServicePrincipalName = (LPTSTR) _tcsdup(hostnameX);
|
2015-01-29 22:07:02 +03:00
|
|
|
|
2015-02-02 01:06:50 +03:00
|
|
|
free(hostnameX);
|
|
|
|
|
2014-04-10 23:07:53 +04:00
|
|
|
if (!ntlm->ServicePrincipalName)
|
|
|
|
return FALSE;
|
2015-01-29 22:07:02 +03:00
|
|
|
|
2012-11-28 01:37:56 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-01-29 22:07:02 +03:00
|
|
|
if (DsMakeSpn(ServiceClass, hostnameX, NULL, 0, NULL, &SpnLength, NULL) != ERROR_BUFFER_OVERFLOW)
|
2012-11-15 19:00:07 +04:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
ntlm->ServicePrincipalName = (LPTSTR) malloc(SpnLength * sizeof(TCHAR));
|
2015-01-29 22:07:02 +03:00
|
|
|
|
2014-04-10 23:07:53 +04:00
|
|
|
if (!ntlm->ServicePrincipalName)
|
|
|
|
return FALSE;
|
2012-11-15 19:00:07 +04:00
|
|
|
|
2015-01-29 22:07:02 +03:00
|
|
|
if (DsMakeSpn(ServiceClass, hostnameX, NULL, 0, NULL, &SpnLength, ntlm->ServicePrincipalName) != ERROR_SUCCESS)
|
2013-12-13 19:11:36 +04:00
|
|
|
return FALSE;
|
2012-11-15 19:00:07 +04:00
|
|
|
|
2015-01-29 22:07:02 +03:00
|
|
|
free(hostnameX);
|
|
|
|
|
2012-11-15 19:00:07 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-11-28 07:03:05 +04:00
|
|
|
/**
|
|
|
|
* SSPI Client Ceremony
|
|
|
|
*
|
|
|
|
* --------------
|
|
|
|
* ( Client Begin )
|
|
|
|
* --------------
|
|
|
|
* |
|
|
|
|
* |
|
|
|
|
* \|/
|
|
|
|
* -----------+--------------
|
|
|
|
* | AcquireCredentialsHandle |
|
|
|
|
* --------------------------
|
|
|
|
* |
|
|
|
|
* |
|
|
|
|
* \|/
|
|
|
|
* -------------+--------------
|
|
|
|
* +---------------> / InitializeSecurityContext /
|
|
|
|
* | ----------------------------
|
|
|
|
* | |
|
|
|
|
* | |
|
|
|
|
* | \|/
|
|
|
|
* --------------------------- ---------+------------- ----------------------
|
|
|
|
* / Receive blob from server / < Received security blob? > --Yes-> / Send blob to server /
|
|
|
|
* -------------+------------- ----------------------- ----------------------
|
|
|
|
* /|\ | |
|
|
|
|
* | No |
|
|
|
|
* Yes \|/ |
|
|
|
|
* | ------------+----------- |
|
|
|
|
* +---------------- < Received Continue Needed > <-----------------+
|
|
|
|
* ------------------------
|
|
|
|
* |
|
|
|
|
* No
|
|
|
|
* \|/
|
|
|
|
* ------+-------
|
|
|
|
* ( Client End )
|
|
|
|
* --------------
|
|
|
|
*/
|
|
|
|
|
2012-11-15 19:00:07 +04:00
|
|
|
BOOL ntlm_authenticate(rdpNtlm* ntlm)
|
|
|
|
{
|
|
|
|
SECURITY_STATUS status;
|
|
|
|
|
2013-01-09 09:20:08 +04:00
|
|
|
if (ntlm->outputBuffer[0].pvBuffer)
|
2012-12-13 07:03:40 +04:00
|
|
|
{
|
2013-01-09 09:20:08 +04:00
|
|
|
free(ntlm->outputBuffer[0].pvBuffer);
|
|
|
|
ntlm->outputBuffer[0].pvBuffer = NULL;
|
2012-12-13 07:03:40 +04:00
|
|
|
}
|
|
|
|
|
2012-11-15 19:00:07 +04:00
|
|
|
ntlm->outputBufferDesc.ulVersion = SECBUFFER_VERSION;
|
|
|
|
ntlm->outputBufferDesc.cBuffers = 1;
|
2013-01-09 09:20:08 +04:00
|
|
|
ntlm->outputBufferDesc.pBuffers = ntlm->outputBuffer;
|
|
|
|
ntlm->outputBuffer[0].BufferType = SECBUFFER_TOKEN;
|
|
|
|
ntlm->outputBuffer[0].cbBuffer = ntlm->cbMaxToken;
|
|
|
|
ntlm->outputBuffer[0].pvBuffer = malloc(ntlm->outputBuffer[0].cbBuffer);
|
2014-12-11 19:25:34 +03:00
|
|
|
|
2014-04-10 23:07:53 +04:00
|
|
|
if (!ntlm->outputBuffer[0].pvBuffer)
|
|
|
|
return FALSE;
|
2012-11-15 19:00:07 +04:00
|
|
|
|
|
|
|
if (ntlm->haveInputBuffer)
|
|
|
|
{
|
|
|
|
ntlm->inputBufferDesc.ulVersion = SECBUFFER_VERSION;
|
|
|
|
ntlm->inputBufferDesc.cBuffers = 1;
|
2013-01-09 09:20:08 +04:00
|
|
|
ntlm->inputBufferDesc.pBuffers = ntlm->inputBuffer;
|
|
|
|
ntlm->inputBuffer[0].BufferType = SECBUFFER_TOKEN;
|
|
|
|
|
|
|
|
if (ntlm->Bindings)
|
|
|
|
{
|
|
|
|
ntlm->inputBufferDesc.cBuffers++;
|
|
|
|
ntlm->inputBuffer[1].BufferType = SECBUFFER_CHANNEL_BINDINGS;
|
|
|
|
ntlm->inputBuffer[1].cbBuffer = ntlm->Bindings->BindingsLength;
|
|
|
|
ntlm->inputBuffer[1].pvBuffer = (void*) ntlm->Bindings->Bindings;
|
|
|
|
}
|
2012-11-15 19:00:07 +04:00
|
|
|
}
|
|
|
|
|
2013-12-07 07:15:45 +04:00
|
|
|
if ((!ntlm) || (!ntlm->table))
|
|
|
|
{
|
2015-01-29 22:07:02 +03:00
|
|
|
WLog_ERR(TAG, "ntlm_authenticate: invalid ntlm context");
|
2013-12-07 07:15:45 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-11-15 19:00:07 +04:00
|
|
|
status = ntlm->table->InitializeSecurityContext(&ntlm->credentials,
|
|
|
|
(ntlm->haveContext) ? &ntlm->context : NULL,
|
|
|
|
(ntlm->ServicePrincipalName) ? ntlm->ServicePrincipalName : NULL,
|
|
|
|
ntlm->fContextReq, 0, SECURITY_NATIVE_DREP,
|
|
|
|
(ntlm->haveInputBuffer) ? &ntlm->inputBufferDesc : NULL,
|
|
|
|
0, &ntlm->context, &ntlm->outputBufferDesc,
|
|
|
|
&ntlm->pfContextAttr, &ntlm->expiration);
|
|
|
|
|
|
|
|
if ((status == SEC_I_COMPLETE_AND_CONTINUE) || (status == SEC_I_COMPLETE_NEEDED) || (status == SEC_E_OK))
|
|
|
|
{
|
2015-01-29 22:07:02 +03:00
|
|
|
if (ntlm->table->CompleteAuthToken)
|
2012-11-15 19:00:07 +04:00
|
|
|
ntlm->table->CompleteAuthToken(&ntlm->context, &ntlm->outputBufferDesc);
|
|
|
|
|
|
|
|
if (ntlm->table->QueryContextAttributes(&ntlm->context, SECPKG_ATTR_SIZES, &ntlm->ContextSizes) != SEC_E_OK)
|
|
|
|
{
|
2015-01-29 22:07:02 +03:00
|
|
|
WLog_ERR(TAG, "QueryContextAttributes SECPKG_ATTR_SIZES failure");
|
2012-11-28 07:03:05 +04:00
|
|
|
return FALSE;
|
2012-11-15 19:00:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (status == SEC_I_COMPLETE_NEEDED)
|
|
|
|
status = SEC_E_OK;
|
|
|
|
else if (status == SEC_I_COMPLETE_AND_CONTINUE)
|
|
|
|
status = SEC_I_CONTINUE_NEEDED;
|
|
|
|
}
|
|
|
|
|
2012-12-13 00:55:42 +04:00
|
|
|
if (ntlm->haveInputBuffer)
|
|
|
|
{
|
2013-01-09 09:20:08 +04:00
|
|
|
free(ntlm->inputBuffer[0].pvBuffer);
|
2015-01-29 22:07:02 +03:00
|
|
|
ntlm->inputBuffer[0].pvBuffer = NULL;
|
2012-12-13 00:55:42 +04:00
|
|
|
}
|
|
|
|
|
2012-11-15 19:00:07 +04:00
|
|
|
ntlm->haveInputBuffer = TRUE;
|
|
|
|
ntlm->haveContext = TRUE;
|
|
|
|
|
2012-11-28 07:03:05 +04:00
|
|
|
return (status == SEC_I_CONTINUE_NEEDED) ? TRUE : FALSE;
|
2012-11-15 19:00:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ntlm_client_uninit(rdpNtlm* ntlm)
|
|
|
|
{
|
2012-12-12 09:49:15 +04:00
|
|
|
free(ntlm->identity.User);
|
2015-01-29 22:07:02 +03:00
|
|
|
ntlm->identity.User = NULL;
|
|
|
|
|
2012-12-12 09:49:15 +04:00
|
|
|
free(ntlm->identity.Domain);
|
2015-01-29 22:07:02 +03:00
|
|
|
ntlm->identity.Domain = NULL;
|
|
|
|
|
2012-12-12 09:49:15 +04:00
|
|
|
free(ntlm->identity.Password);
|
2015-01-29 22:07:02 +03:00
|
|
|
ntlm->identity.Password = NULL;
|
|
|
|
|
2012-12-12 09:49:15 +04:00
|
|
|
free(ntlm->ServicePrincipalName);
|
2015-01-29 22:07:02 +03:00
|
|
|
ntlm->ServicePrincipalName = NULL;
|
2012-12-12 09:49:15 +04:00
|
|
|
|
2013-12-07 07:15:45 +04:00
|
|
|
if (ntlm->table)
|
|
|
|
{
|
|
|
|
ntlm->table->FreeCredentialsHandle(&ntlm->credentials);
|
|
|
|
ntlm->table->FreeContextBuffer(ntlm->pPackageInfo);
|
|
|
|
ntlm->table->DeleteSecurityContext(&ntlm->context);
|
2015-02-02 19:50:56 +03:00
|
|
|
ntlm->table = NULL;
|
2013-12-07 07:15:45 +04:00
|
|
|
}
|
2012-11-15 19:00:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
rdpNtlm* ntlm_new()
|
|
|
|
{
|
2015-02-02 19:50:56 +03:00
|
|
|
rdpNtlm* ntlm;
|
|
|
|
|
|
|
|
ntlm = (rdpNtlm*) calloc(1, sizeof(rdpNtlm));
|
|
|
|
|
|
|
|
return ntlm;
|
2012-11-15 19:00:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ntlm_free(rdpNtlm* ntlm)
|
|
|
|
{
|
2015-02-02 19:50:56 +03:00
|
|
|
if (!ntlm)
|
|
|
|
return;
|
2014-12-11 19:25:34 +03:00
|
|
|
|
2015-02-02 19:50:56 +03:00
|
|
|
if (ntlm->outputBuffer[0].pvBuffer)
|
|
|
|
{
|
|
|
|
free(ntlm->outputBuffer[0].pvBuffer);
|
|
|
|
ntlm->outputBuffer[0].pvBuffer = NULL;
|
2012-11-15 19:00:07 +04:00
|
|
|
}
|
2015-02-02 19:50:56 +03:00
|
|
|
|
2015-03-17 23:54:45 +03:00
|
|
|
ntlm_client_uninit(ntlm);
|
|
|
|
|
2015-02-02 19:50:56 +03:00
|
|
|
free(ntlm);
|
2012-11-15 19:00:07 +04:00
|
|
|
}
|