2012-05-25 09:50:46 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Network Level Authentication (NLA)
|
|
|
|
*
|
|
|
|
* Copyright 2010-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.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2012-05-25 09:50:46 +04:00
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2014-08-19 20:26:39 +04:00
|
|
|
#include <freerdp/log.h>
|
2012-05-25 09:50:46 +04:00
|
|
|
#include <freerdp/crypto/tls.h>
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/sspi.h>
|
|
|
|
#include <winpr/print.h>
|
2012-08-11 02:05:37 +04:00
|
|
|
#include <winpr/tchar.h>
|
2014-02-12 09:43:02 +04:00
|
|
|
#include <winpr/dsparse.h>
|
2012-08-11 02:05:37 +04:00
|
|
|
#include <winpr/library.h>
|
|
|
|
#include <winpr/registry.h>
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2012-11-15 06:19:17 +04:00
|
|
|
#include "nla.h"
|
|
|
|
|
2014-08-19 20:26:39 +04:00
|
|
|
#define TAG FREERDP_TAG("core")
|
|
|
|
|
2012-05-25 09:50:46 +04:00
|
|
|
/**
|
|
|
|
* TSRequest ::= SEQUENCE {
|
|
|
|
* version [0] INTEGER,
|
|
|
|
* negoTokens [1] NegoData OPTIONAL,
|
|
|
|
* authInfo [2] OCTET STRING OPTIONAL,
|
|
|
|
* pubKeyAuth [3] OCTET STRING OPTIONAL
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* NegoData ::= SEQUENCE OF NegoDataItem
|
|
|
|
*
|
|
|
|
* NegoDataItem ::= SEQUENCE {
|
|
|
|
* negoToken [0] OCTET STRING
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* TSCredentials ::= SEQUENCE {
|
|
|
|
* credType [0] INTEGER,
|
|
|
|
* credentials [1] OCTET STRING
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* TSPasswordCreds ::= SEQUENCE {
|
|
|
|
* domainName [0] OCTET STRING,
|
|
|
|
* userName [1] OCTET STRING,
|
|
|
|
* password [2] OCTET STRING
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* TSSmartCardCreds ::= SEQUENCE {
|
|
|
|
* pin [0] OCTET STRING,
|
|
|
|
* cspData [1] TSCspDataDetail,
|
|
|
|
* userHint [2] OCTET STRING OPTIONAL,
|
|
|
|
* domainHint [3] OCTET STRING OPTIONAL
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* TSCspDataDetail ::= SEQUENCE {
|
|
|
|
* keySpec [0] INTEGER,
|
|
|
|
* cardName [1] OCTET STRING OPTIONAL,
|
|
|
|
* readerName [2] OCTET STRING OPTIONAL,
|
|
|
|
* containerName [3] OCTET STRING OPTIONAL,
|
|
|
|
* cspName [4] OCTET STRING OPTIONAL
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef WITH_DEBUG_NLA
|
|
|
|
#define WITH_DEBUG_CREDSSP
|
|
|
|
#endif
|
|
|
|
|
2014-06-08 00:26:57 +04:00
|
|
|
#define NLA_PKG_NAME NEGOSSP_NAME
|
2012-06-18 09:01:08 +04:00
|
|
|
|
2012-06-07 03:59:17 +04:00
|
|
|
#define TERMSRV_SPN_PREFIX "TERMSRV/"
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void credssp_send(rdpCredssp* credssp);
|
|
|
|
int credssp_recv(rdpCredssp* credssp);
|
|
|
|
void credssp_buffer_print(rdpCredssp* credssp);
|
|
|
|
void credssp_buffer_free(rdpCredssp* credssp);
|
|
|
|
SECURITY_STATUS credssp_encrypt_public_key_echo(rdpCredssp* credssp);
|
|
|
|
SECURITY_STATUS credssp_decrypt_public_key_echo(rdpCredssp* credssp);
|
|
|
|
SECURITY_STATUS credssp_encrypt_ts_credentials(rdpCredssp* credssp);
|
|
|
|
SECURITY_STATUS credssp_decrypt_ts_credentials(rdpCredssp* credssp);
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2013-05-21 19:06:00 +04:00
|
|
|
#define ber_sizeof_sequence_octet_string(length) ber_sizeof_contextual_tag(ber_sizeof_octet_string(length)) + ber_sizeof_octet_string(length)
|
|
|
|
#define ber_write_sequence_octet_string(stream, context, value, length) ber_write_contextual_tag(stream, context, ber_sizeof_octet_string(length), TRUE) + ber_write_octet_string(stream, value, length)
|
|
|
|
|
2012-05-25 09:50:46 +04:00
|
|
|
/**
|
|
|
|
* Initialize NTLMSSP authentication module (client).
|
|
|
|
* @param credssp
|
|
|
|
*/
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
int credssp_ntlm_client_init(rdpCredssp* credssp)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
char* spn;
|
2012-06-07 03:59:17 +04:00
|
|
|
int length;
|
2013-11-06 19:02:58 +04:00
|
|
|
BOOL PromptPassword;
|
2014-08-18 21:34:47 +04:00
|
|
|
rdpTls* tls = NULL;
|
|
|
|
freerdp* instance;
|
|
|
|
rdpSettings* settings;
|
2013-11-06 19:02:58 +04:00
|
|
|
PromptPassword = FALSE;
|
2012-06-07 03:59:17 +04:00
|
|
|
settings = credssp->settings;
|
2014-08-18 21:34:47 +04:00
|
|
|
instance = (freerdp*) settings->instance;
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2014-02-14 09:43:31 +04:00
|
|
|
if (settings->RestrictedAdminModeRequired)
|
|
|
|
settings->DisableCredentialsDelegation = TRUE;
|
|
|
|
|
2013-11-06 19:02:58 +04:00
|
|
|
if ((!settings->Password) || (!settings->Username)
|
2013-10-19 00:23:29 +04:00
|
|
|
|| (!strlen(settings->Password)) || (!strlen(settings->Username)))
|
2013-11-06 19:02:58 +04:00
|
|
|
{
|
|
|
|
PromptPassword = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2013-11-06 19:02:58 +04:00
|
|
|
if (PromptPassword)
|
|
|
|
{
|
|
|
|
if (settings->RestrictedAdminModeRequired)
|
|
|
|
{
|
|
|
|
if ((settings->PasswordHash) && (strlen(settings->PasswordHash) > 0))
|
|
|
|
PromptPassword = FALSE;
|
|
|
|
}
|
|
|
|
}
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2013-11-06 19:02:58 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (PromptPassword)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
|
|
|
if (instance->Authenticate)
|
|
|
|
{
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL proceed = instance->Authenticate(instance,
|
2014-08-18 19:22:43 +04:00
|
|
|
&settings->Username, &settings->Password, &settings->Domain);
|
2013-11-06 19:02:58 +04:00
|
|
|
|
2012-05-25 09:50:46 +04:00
|
|
|
if (!proceed)
|
2013-12-13 19:11:36 +04:00
|
|
|
{
|
|
|
|
connectErrorCode = CANCELEDBYUSER;
|
2014-03-21 21:45:43 +04:00
|
|
|
freerdp_set_last_error(instance->context, FREERDP_ERROR_CONNECT_CANCELLED);
|
2012-05-25 09:50:46 +04:00
|
|
|
return 0;
|
2013-12-13 19:11:36 +04:00
|
|
|
}
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-08 00:13:14 +04:00
|
|
|
sspi_SetAuthIdentity(&(credssp->identity), settings->Username, settings->Domain, settings->Password);
|
2013-11-06 19:02:58 +04:00
|
|
|
#ifndef _WIN32
|
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
SEC_WINNT_AUTH_IDENTITY* identity = &(credssp->identity);
|
2013-11-06 19:02:58 +04:00
|
|
|
|
|
|
|
if (settings->RestrictedAdminModeRequired)
|
|
|
|
{
|
|
|
|
if (settings->PasswordHash)
|
|
|
|
{
|
|
|
|
if (strlen(settings->PasswordHash) == 32)
|
|
|
|
{
|
|
|
|
if (identity->Password)
|
|
|
|
free(identity->Password);
|
|
|
|
|
|
|
|
identity->PasswordLength = ConvertToUnicode(CP_UTF8, 0,
|
2014-08-18 19:22:43 +04:00
|
|
|
settings->PasswordHash, -1, &identity->Password, 0) - 1;
|
2013-11-06 19:02:58 +04:00
|
|
|
/**
|
|
|
|
* Multiply password hash length by 64 to obtain a length exceeding
|
|
|
|
* the maximum (256) and use it this for hash identification in WinPR.
|
|
|
|
*/
|
|
|
|
identity->PasswordLength = 32 * 64; /* 2048 */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2012-08-30 00:45:39 +04:00
|
|
|
#ifdef WITH_DEBUG_NLA
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_DBG(TAG, "User: %s Domain: %s Password: %s",
|
|
|
|
(char*) credssp->identity.User, (char*) credssp->identity.Domain, (char*) credssp->identity.Password);
|
2012-08-29 02:01:42 +04:00
|
|
|
#endif
|
|
|
|
|
2013-10-31 01:56:44 +04:00
|
|
|
if (credssp->transport->layer == TRANSPORT_LAYER_TLS)
|
|
|
|
{
|
2013-10-29 00:39:10 +04:00
|
|
|
tls = credssp->transport->TlsIn;
|
2013-10-31 01:56:44 +04:00
|
|
|
}
|
|
|
|
else if (credssp->transport->layer == TRANSPORT_LAYER_TSG_TLS)
|
|
|
|
{
|
2013-10-29 00:39:10 +04:00
|
|
|
tls = credssp->transport->TsgTls;
|
2013-10-31 01:56:44 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "Unknown NLA transport layer");
|
2013-10-29 00:39:10 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sspi_SecBufferAlloc(&credssp->PublicKey, tls->PublicKeyLength);
|
|
|
|
CopyMemory(credssp->PublicKey.pvBuffer, tls->PublicKey, tls->PublicKeyLength);
|
2012-11-08 03:23:25 +04:00
|
|
|
length = sizeof(TERMSRV_SPN_PREFIX) + strlen(settings->ServerHostname);
|
2014-08-18 21:34:47 +04:00
|
|
|
spn = (SEC_CHAR*) malloc(length + 1);
|
2012-11-08 03:23:25 +04:00
|
|
|
sprintf(spn, "%s%s", TERMSRV_SPN_PREFIX, settings->ServerHostname);
|
2012-06-07 03:59:17 +04:00
|
|
|
#ifdef UNICODE
|
2012-06-07 08:14:47 +04:00
|
|
|
credssp->ServicePrincipalName = (LPTSTR) malloc(length * 2 + 2);
|
2012-12-17 08:00:40 +04:00
|
|
|
MultiByteToWideChar(CP_UTF8, 0, spn, length,
|
2014-08-18 19:22:43 +04:00
|
|
|
(LPWSTR) credssp->ServicePrincipalName, length);
|
2012-06-07 03:59:17 +04:00
|
|
|
free(spn);
|
|
|
|
#else
|
|
|
|
credssp->ServicePrincipalName = spn;
|
|
|
|
#endif
|
2012-05-25 09:50:46 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize NTLMSSP authentication module (server).
|
|
|
|
* @param credssp
|
|
|
|
*/
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
int credssp_ntlm_server_init(rdpCredssp* credssp)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
freerdp* instance;
|
|
|
|
rdpSettings* settings = credssp->settings;
|
|
|
|
instance = (freerdp*) settings->instance;
|
2012-11-15 06:30:21 +04:00
|
|
|
sspi_SecBufferAlloc(&credssp->PublicKey, credssp->transport->TlsIn->PublicKeyLength);
|
|
|
|
CopyMemory(credssp->PublicKey.pvBuffer, credssp->transport->TlsIn->PublicKey, credssp->transport->TlsIn->PublicKeyLength);
|
2012-05-25 09:50:46 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
int credssp_client_authenticate(rdpCredssp* credssp)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
|
|
|
ULONG cbMaxToken;
|
|
|
|
ULONG fContextReq;
|
|
|
|
ULONG pfContextAttr;
|
|
|
|
SECURITY_STATUS status;
|
|
|
|
CredHandle credentials;
|
|
|
|
TimeStamp expiration;
|
|
|
|
PSecPkgInfo pPackageInfo;
|
|
|
|
SecBuffer input_buffer;
|
|
|
|
SecBuffer output_buffer;
|
|
|
|
SecBufferDesc input_buffer_desc;
|
|
|
|
SecBufferDesc output_buffer_desc;
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL have_context;
|
|
|
|
BOOL have_input_buffer;
|
|
|
|
BOOL have_pub_key_auth;
|
2012-05-25 09:50:46 +04:00
|
|
|
sspi_GlobalInit();
|
|
|
|
|
|
|
|
if (credssp_ntlm_client_init(credssp) == 0)
|
|
|
|
return 0;
|
|
|
|
|
2014-06-07 18:50:51 +04:00
|
|
|
credssp->table = InitSecurityInterfaceEx(0);
|
2012-06-18 09:01:08 +04:00
|
|
|
status = credssp->table->QuerySecurityPackageInfo(NLA_PKG_NAME, &pPackageInfo);
|
2012-05-25 09:50:46 +04:00
|
|
|
|
|
|
|
if (status != SEC_E_OK)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "QuerySecurityPackageInfo status: 0x%08X", status);
|
2012-05-25 09:50:46 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
cbMaxToken = pPackageInfo->cbMaxToken;
|
2012-06-18 09:01:08 +04:00
|
|
|
status = credssp->table->AcquireCredentialsHandle(NULL, NLA_PKG_NAME,
|
2014-08-18 19:22:43 +04:00
|
|
|
SECPKG_CRED_OUTBOUND, NULL, &credssp->identity, NULL, NULL, &credentials, &expiration);
|
2012-05-25 09:50:46 +04:00
|
|
|
|
|
|
|
if (status != SEC_E_OK)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "AcquireCredentialsHandle status: 0x%08X", status);
|
2012-05-25 09:50:46 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
have_context = FALSE;
|
|
|
|
have_input_buffer = FALSE;
|
|
|
|
have_pub_key_auth = FALSE;
|
2012-06-16 06:39:12 +04:00
|
|
|
ZeroMemory(&input_buffer, sizeof(SecBuffer));
|
|
|
|
ZeroMemory(&output_buffer, sizeof(SecBuffer));
|
|
|
|
ZeroMemory(&credssp->ContextSizes, sizeof(SecPkgContext_Sizes));
|
2013-05-21 19:06:00 +04:00
|
|
|
/*
|
2012-06-18 09:01:08 +04:00
|
|
|
* from tspkg.dll: 0x00000132
|
|
|
|
* ISC_REQ_MUTUAL_AUTH
|
|
|
|
* ISC_REQ_CONFIDENTIALITY
|
|
|
|
* ISC_REQ_USE_SESSION_KEY
|
|
|
|
* ISC_REQ_ALLOCATE_MEMORY
|
|
|
|
*/
|
|
|
|
fContextReq = ISC_REQ_MUTUAL_AUTH | ISC_REQ_CONFIDENTIALITY | ISC_REQ_USE_SESSION_KEY;
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
while (TRUE)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
|
|
|
output_buffer_desc.ulVersion = SECBUFFER_VERSION;
|
|
|
|
output_buffer_desc.cBuffers = 1;
|
|
|
|
output_buffer_desc.pBuffers = &output_buffer;
|
|
|
|
output_buffer.BufferType = SECBUFFER_TOKEN;
|
|
|
|
output_buffer.cbBuffer = cbMaxToken;
|
|
|
|
output_buffer.pvBuffer = malloc(output_buffer.cbBuffer);
|
|
|
|
status = credssp->table->InitializeSecurityContext(&credentials,
|
2014-08-18 19:22:43 +04:00
|
|
|
(have_context) ? &credssp->context : NULL,
|
|
|
|
credssp->ServicePrincipalName, fContextReq, 0,
|
|
|
|
SECURITY_NATIVE_DREP, (have_input_buffer) ? &input_buffer_desc : NULL,
|
|
|
|
0, &credssp->context, &output_buffer_desc, &pfContextAttr, &expiration);
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2014-06-08 00:26:57 +04:00
|
|
|
if (have_input_buffer && (input_buffer.pvBuffer))
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2012-07-18 13:36:33 +04:00
|
|
|
free(input_buffer.pvBuffer);
|
2012-05-25 09:50:46 +04:00
|
|
|
input_buffer.pvBuffer = NULL;
|
|
|
|
}
|
|
|
|
|
2014-06-10 22:38:17 +04:00
|
|
|
if ((status == SEC_I_COMPLETE_AND_CONTINUE) || (status == SEC_I_COMPLETE_NEEDED))
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2014-06-08 00:26:57 +04:00
|
|
|
if (credssp->table->CompleteAuthToken)
|
2012-05-25 09:50:46 +04:00
|
|
|
credssp->table->CompleteAuthToken(&credssp->context, &output_buffer_desc);
|
|
|
|
|
2014-06-10 22:38:17 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status == SEC_E_OK)
|
|
|
|
{
|
2012-10-09 10:31:28 +04:00
|
|
|
have_pub_key_auth = TRUE;
|
2012-05-25 09:50:46 +04:00
|
|
|
|
|
|
|
if (credssp->table->QueryContextAttributes(&credssp->context, SECPKG_ATTR_SIZES, &credssp->ContextSizes) != SEC_E_OK)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "QueryContextAttributes SECPKG_ATTR_SIZES failure");
|
2012-05-25 09:50:46 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-16 06:39:12 +04:00
|
|
|
credssp_encrypt_public_key_echo(credssp);
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* send authentication token to server */
|
|
|
|
|
|
|
|
if (output_buffer.cbBuffer > 0)
|
|
|
|
{
|
2012-08-29 02:01:42 +04:00
|
|
|
credssp->negoToken.pvBuffer = output_buffer.pvBuffer;
|
|
|
|
credssp->negoToken.cbBuffer = output_buffer.cbBuffer;
|
2012-05-25 09:50:46 +04:00
|
|
|
#ifdef WITH_DEBUG_CREDSSP
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_DBG(TAG, "Sending Authentication Token");
|
2014-08-19 20:26:39 +04:00
|
|
|
winpr_HexDump(TAG, WLOG_DEBUG, credssp->negoToken.pvBuffer, credssp->negoToken.cbBuffer);
|
2012-05-25 09:50:46 +04:00
|
|
|
#endif
|
|
|
|
credssp_send(credssp);
|
|
|
|
credssp_buffer_free(credssp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status != SEC_I_CONTINUE_NEEDED)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* receive server response and place in input buffer */
|
|
|
|
input_buffer_desc.ulVersion = SECBUFFER_VERSION;
|
|
|
|
input_buffer_desc.cBuffers = 1;
|
|
|
|
input_buffer_desc.pBuffers = &input_buffer;
|
|
|
|
input_buffer.BufferType = SECBUFFER_TOKEN;
|
|
|
|
|
|
|
|
if (credssp_recv(credssp) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
#ifdef WITH_DEBUG_CREDSSP
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_DBG(TAG, "Receiving Authentication Token (%d)", (int) credssp->negoToken.cbBuffer);
|
2014-08-19 20:26:39 +04:00
|
|
|
winpr_HexDump(TAG, WLOG_DEBUG, credssp->negoToken.pvBuffer, credssp->negoToken.cbBuffer);
|
2012-05-25 09:50:46 +04:00
|
|
|
#endif
|
2012-08-29 02:01:42 +04:00
|
|
|
input_buffer.pvBuffer = credssp->negoToken.pvBuffer;
|
|
|
|
input_buffer.cbBuffer = credssp->negoToken.cbBuffer;
|
2012-10-09 10:31:28 +04:00
|
|
|
have_input_buffer = TRUE;
|
|
|
|
have_context = TRUE;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Encrypted Public Key +1 */
|
|
|
|
if (credssp_recv(credssp) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Verify Server Public Key Echo */
|
2012-06-16 06:39:12 +04:00
|
|
|
status = credssp_decrypt_public_key_echo(credssp);
|
2012-05-25 09:50:46 +04:00
|
|
|
credssp_buffer_free(credssp);
|
|
|
|
|
|
|
|
if (status != SEC_E_OK)
|
2012-06-16 06:39:12 +04:00
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "Could not verify public key echo!");
|
2012-06-16 06:39:12 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2012-05-25 09:50:46 +04:00
|
|
|
|
|
|
|
/* Send encrypted credentials */
|
|
|
|
status = credssp_encrypt_ts_credentials(credssp);
|
|
|
|
|
|
|
|
if (status != SEC_E_OK)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "credssp_encrypt_ts_credentials status: 0x%08X", status);
|
2012-05-25 09:50:46 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
credssp_send(credssp);
|
|
|
|
credssp_buffer_free(credssp);
|
|
|
|
/* Free resources */
|
|
|
|
credssp->table->FreeCredentialsHandle(&credentials);
|
|
|
|
credssp->table->FreeContextBuffer(pPackageInfo);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Authenticate with client using CredSSP (server).
|
|
|
|
* @param credssp
|
|
|
|
* @return 1 if authentication is successful
|
|
|
|
*/
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
int credssp_server_authenticate(rdpCredssp* credssp)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
|
|
|
UINT32 cbMaxToken;
|
|
|
|
ULONG fContextReq;
|
|
|
|
ULONG pfContextAttr;
|
|
|
|
SECURITY_STATUS status;
|
|
|
|
CredHandle credentials;
|
|
|
|
TimeStamp expiration;
|
|
|
|
PSecPkgInfo pPackageInfo;
|
|
|
|
SecBuffer input_buffer;
|
|
|
|
SecBuffer output_buffer;
|
|
|
|
SecBufferDesc input_buffer_desc;
|
|
|
|
SecBufferDesc output_buffer_desc;
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL have_context;
|
|
|
|
BOOL have_input_buffer;
|
|
|
|
BOOL have_pub_key_auth;
|
2012-05-25 09:50:46 +04:00
|
|
|
sspi_GlobalInit();
|
|
|
|
|
|
|
|
if (credssp_ntlm_server_init(credssp) == 0)
|
|
|
|
return 0;
|
|
|
|
|
2012-08-11 02:05:37 +04:00
|
|
|
if (credssp->SspiModule)
|
|
|
|
{
|
|
|
|
HMODULE hSSPI;
|
|
|
|
INIT_SECURITY_INTERFACE pInitSecurityInterface;
|
|
|
|
hSSPI = LoadLibrary(credssp->SspiModule);
|
|
|
|
|
|
|
|
if (!hSSPI)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "Failed to load SSPI module: %s", credssp->SspiModule);
|
2012-08-11 02:05:37 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef UNICODE
|
|
|
|
pInitSecurityInterface = (INIT_SECURITY_INTERFACE) GetProcAddress(hSSPI, "InitSecurityInterfaceW");
|
|
|
|
#else
|
|
|
|
pInitSecurityInterface = (INIT_SECURITY_INTERFACE) GetProcAddress(hSSPI, "InitSecurityInterfaceA");
|
|
|
|
#endif
|
2014-06-07 18:50:51 +04:00
|
|
|
credssp->table = pInitSecurityInterface();
|
2012-08-11 02:05:37 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-07 18:50:51 +04:00
|
|
|
credssp->table = InitSecurityInterfaceEx(0);
|
2012-08-11 02:05:37 +04:00
|
|
|
}
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2012-06-18 09:01:08 +04:00
|
|
|
status = credssp->table->QuerySecurityPackageInfo(NLA_PKG_NAME, &pPackageInfo);
|
2012-05-25 09:50:46 +04:00
|
|
|
|
|
|
|
if (status != SEC_E_OK)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "QuerySecurityPackageInfo status: 0x%08X", status);
|
2012-05-25 09:50:46 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
cbMaxToken = pPackageInfo->cbMaxToken;
|
2012-06-18 09:01:08 +04:00
|
|
|
status = credssp->table->AcquireCredentialsHandle(NULL, NLA_PKG_NAME,
|
2014-08-18 19:22:43 +04:00
|
|
|
SECPKG_CRED_INBOUND, NULL, NULL, NULL, NULL, &credentials, &expiration);
|
2012-05-25 09:50:46 +04:00
|
|
|
|
|
|
|
if (status != SEC_E_OK)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "AcquireCredentialsHandle status: 0x%08X", status);
|
2012-05-25 09:50:46 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
have_context = FALSE;
|
|
|
|
have_input_buffer = FALSE;
|
|
|
|
have_pub_key_auth = FALSE;
|
2012-06-13 07:09:30 +04:00
|
|
|
ZeroMemory(&input_buffer, sizeof(SecBuffer));
|
|
|
|
ZeroMemory(&output_buffer, sizeof(SecBuffer));
|
2012-08-24 00:09:46 +04:00
|
|
|
ZeroMemory(&input_buffer_desc, sizeof(SecBufferDesc));
|
|
|
|
ZeroMemory(&output_buffer_desc, sizeof(SecBufferDesc));
|
2012-06-13 07:09:30 +04:00
|
|
|
ZeroMemory(&credssp->ContextSizes, sizeof(SecPkgContext_Sizes));
|
2013-05-21 19:06:00 +04:00
|
|
|
/*
|
2012-06-18 09:01:08 +04:00
|
|
|
* from tspkg.dll: 0x00000112
|
|
|
|
* ASC_REQ_MUTUAL_AUTH
|
|
|
|
* ASC_REQ_CONFIDENTIALITY
|
|
|
|
* ASC_REQ_ALLOCATE_MEMORY
|
|
|
|
*/
|
2012-06-18 23:56:40 +04:00
|
|
|
fContextReq = 0;
|
|
|
|
fContextReq |= ASC_REQ_MUTUAL_AUTH;
|
|
|
|
fContextReq |= ASC_REQ_CONFIDENTIALITY;
|
|
|
|
fContextReq |= ASC_REQ_CONNECTION;
|
|
|
|
fContextReq |= ASC_REQ_USE_SESSION_KEY;
|
|
|
|
fContextReq |= ASC_REQ_REPLAY_DETECT;
|
|
|
|
fContextReq |= ASC_REQ_SEQUENCE_DETECT;
|
|
|
|
fContextReq |= ASC_REQ_EXTENDED_ERROR;
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
while (TRUE)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
|
|
|
input_buffer_desc.ulVersion = SECBUFFER_VERSION;
|
|
|
|
input_buffer_desc.cBuffers = 1;
|
|
|
|
input_buffer_desc.pBuffers = &input_buffer;
|
|
|
|
input_buffer.BufferType = SECBUFFER_TOKEN;
|
|
|
|
/* receive authentication token */
|
|
|
|
input_buffer_desc.ulVersion = SECBUFFER_VERSION;
|
|
|
|
input_buffer_desc.cBuffers = 1;
|
|
|
|
input_buffer_desc.pBuffers = &input_buffer;
|
|
|
|
input_buffer.BufferType = SECBUFFER_TOKEN;
|
|
|
|
|
|
|
|
if (credssp_recv(credssp) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
#ifdef WITH_DEBUG_CREDSSP
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_DBG(TAG, "Receiving Authentication Token");
|
2012-05-25 09:50:46 +04:00
|
|
|
credssp_buffer_print(credssp);
|
|
|
|
#endif
|
2012-08-24 00:09:46 +04:00
|
|
|
input_buffer.pvBuffer = credssp->negoToken.pvBuffer;
|
|
|
|
input_buffer.cbBuffer = credssp->negoToken.cbBuffer;
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2012-06-13 07:09:30 +04:00
|
|
|
if (credssp->negoToken.cbBuffer < 1)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "CredSSP: invalid negoToken!");
|
2012-06-13 07:09:30 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-05-25 09:50:46 +04:00
|
|
|
output_buffer_desc.ulVersion = SECBUFFER_VERSION;
|
|
|
|
output_buffer_desc.cBuffers = 1;
|
|
|
|
output_buffer_desc.pBuffers = &output_buffer;
|
|
|
|
output_buffer.BufferType = SECBUFFER_TOKEN;
|
|
|
|
output_buffer.cbBuffer = cbMaxToken;
|
|
|
|
output_buffer.pvBuffer = malloc(output_buffer.cbBuffer);
|
|
|
|
status = credssp->table->AcceptSecurityContext(&credentials,
|
2014-08-18 19:22:43 +04:00
|
|
|
have_context? &credssp->context: NULL,
|
|
|
|
&input_buffer_desc, fContextReq, SECURITY_NATIVE_DREP, &credssp->context,
|
|
|
|
&output_buffer_desc, &pfContextAttr, &expiration);
|
2012-08-24 00:09:46 +04:00
|
|
|
credssp->negoToken.pvBuffer = output_buffer.pvBuffer;
|
|
|
|
credssp->negoToken.cbBuffer = output_buffer.cbBuffer;
|
2012-05-25 09:50:46 +04:00
|
|
|
|
|
|
|
if ((status == SEC_I_COMPLETE_AND_CONTINUE) || (status == SEC_I_COMPLETE_NEEDED))
|
|
|
|
{
|
2014-06-08 00:26:57 +04:00
|
|
|
if (credssp->table->CompleteAuthToken)
|
2012-05-25 09:50:46 +04:00
|
|
|
credssp->table->CompleteAuthToken(&credssp->context, &output_buffer_desc);
|
|
|
|
|
2012-08-01 21:13:17 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status == SEC_E_OK)
|
|
|
|
{
|
2012-10-09 10:31:28 +04:00
|
|
|
have_pub_key_auth = TRUE;
|
2012-05-25 09:50:46 +04:00
|
|
|
|
|
|
|
if (credssp->table->QueryContextAttributes(&credssp->context, SECPKG_ATTR_SIZES, &credssp->ContextSizes) != SEC_E_OK)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "QueryContextAttributes SECPKG_ATTR_SIZES failure");
|
2012-05-25 09:50:46 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-27 13:10:17 +04:00
|
|
|
if (credssp_decrypt_public_key_echo(credssp) != SEC_E_OK)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "Error: could not verify client's public key echo");
|
2012-06-27 13:10:17 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2012-09-16 23:30:11 +04:00
|
|
|
sspi_SecBufferFree(&credssp->negoToken);
|
2012-05-25 09:50:46 +04:00
|
|
|
credssp->negoToken.pvBuffer = NULL;
|
|
|
|
credssp->negoToken.cbBuffer = 0;
|
2012-06-16 06:39:12 +04:00
|
|
|
credssp_encrypt_public_key_echo(credssp);
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2012-07-02 03:43:13 +04:00
|
|
|
if ((status != SEC_E_OK) && (status != SEC_I_CONTINUE_NEEDED))
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "AcceptSecurityContext status: 0x%08X", status);
|
2013-12-19 04:44:18 +04:00
|
|
|
return -1; /* Access Denied */
|
2012-07-02 03:43:13 +04:00
|
|
|
}
|
|
|
|
|
2012-05-25 09:50:46 +04:00
|
|
|
/* send authentication token */
|
|
|
|
#ifdef WITH_DEBUG_CREDSSP
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_DBG(TAG, "Sending Authentication Token");
|
2012-05-25 09:50:46 +04:00
|
|
|
credssp_buffer_print(credssp);
|
|
|
|
#endif
|
|
|
|
credssp_send(credssp);
|
2012-09-16 23:30:11 +04:00
|
|
|
credssp_buffer_free(credssp);
|
2012-05-25 09:50:46 +04:00
|
|
|
|
|
|
|
if (status != SEC_I_CONTINUE_NEEDED)
|
|
|
|
break;
|
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
have_context = TRUE;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Receive encrypted credentials */
|
|
|
|
|
|
|
|
if (credssp_recv(credssp) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2012-09-16 23:30:11 +04:00
|
|
|
if (credssp_decrypt_ts_credentials(credssp) != SEC_E_OK)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "Could not decrypt TSCredentials status: 0x%08X", status);
|
2012-09-16 23:30:11 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2012-05-25 09:50:46 +04:00
|
|
|
|
|
|
|
if (status != SEC_E_OK)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "AcceptSecurityContext status: 0x%08X", status);
|
2012-05-25 09:50:46 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = credssp->table->ImpersonateSecurityContext(&credssp->context);
|
|
|
|
|
|
|
|
if (status != SEC_E_OK)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "ImpersonateSecurityContext status: 0x%08X", status);
|
2012-05-25 09:50:46 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = credssp->table->RevertSecurityContext(&credssp->context);
|
|
|
|
|
|
|
|
if (status != SEC_E_OK)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "RevertSecurityContext status: 0x%08X", status);
|
2012-05-25 09:50:46 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
credssp->table->FreeContextBuffer(pPackageInfo);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Authenticate using CredSSP.
|
|
|
|
* @param credssp
|
|
|
|
* @return 1 if authentication is successful
|
|
|
|
*/
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
int credssp_authenticate(rdpCredssp* credssp)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
|
|
|
if (credssp->server)
|
|
|
|
return credssp_server_authenticate(credssp);
|
|
|
|
else
|
|
|
|
return credssp_client_authenticate(credssp);
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void ap_integer_increment_le(BYTE* number, int size)
|
2012-06-27 13:10:17 +04:00
|
|
|
{
|
|
|
|
int index;
|
|
|
|
|
|
|
|
for (index = 0; index < size; index++)
|
|
|
|
{
|
|
|
|
if (number[index] < 0xFF)
|
|
|
|
{
|
|
|
|
number[index]++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
number[index] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void ap_integer_decrement_le(BYTE* number, int size)
|
2012-06-27 13:10:17 +04:00
|
|
|
{
|
|
|
|
int index;
|
|
|
|
|
|
|
|
for (index = 0; index < size; index++)
|
|
|
|
{
|
|
|
|
if (number[index] > 0)
|
|
|
|
{
|
|
|
|
number[index]--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
number[index] = 0xFF;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
SECURITY_STATUS credssp_encrypt_public_key_echo(rdpCredssp* credssp)
|
2012-06-16 06:39:12 +04:00
|
|
|
{
|
|
|
|
SecBuffer Buffers[2];
|
|
|
|
SecBufferDesc Message;
|
|
|
|
SECURITY_STATUS status;
|
2012-06-27 13:10:17 +04:00
|
|
|
int public_key_length;
|
|
|
|
public_key_length = credssp->PublicKey.cbBuffer;
|
2012-06-16 06:39:12 +04:00
|
|
|
Buffers[0].BufferType = SECBUFFER_TOKEN; /* Signature */
|
|
|
|
Buffers[1].BufferType = SECBUFFER_DATA; /* TLS Public Key */
|
2012-06-27 13:10:17 +04:00
|
|
|
sspi_SecBufferAlloc(&credssp->pubKeyAuth, credssp->ContextSizes.cbMaxSignature + public_key_length);
|
2012-06-16 06:39:12 +04:00
|
|
|
Buffers[0].cbBuffer = credssp->ContextSizes.cbMaxSignature;
|
2012-06-18 09:01:08 +04:00
|
|
|
Buffers[0].pvBuffer = credssp->pubKeyAuth.pvBuffer;
|
2012-06-27 13:10:17 +04:00
|
|
|
Buffers[1].cbBuffer = public_key_length;
|
2014-08-18 21:34:47 +04:00
|
|
|
Buffers[1].pvBuffer = ((BYTE*) credssp->pubKeyAuth.pvBuffer) + credssp->ContextSizes.cbMaxSignature;
|
2012-06-16 06:39:12 +04:00
|
|
|
CopyMemory(Buffers[1].pvBuffer, credssp->PublicKey.pvBuffer, Buffers[1].cbBuffer);
|
|
|
|
|
|
|
|
if (credssp->server)
|
|
|
|
{
|
2012-06-18 09:01:08 +04:00
|
|
|
/* server echos the public key +1 */
|
2014-08-18 21:34:47 +04:00
|
|
|
ap_integer_increment_le((BYTE*) Buffers[1].pvBuffer, Buffers[1].cbBuffer);
|
2012-06-16 06:39:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Message.cBuffers = 2;
|
|
|
|
Message.ulVersion = SECBUFFER_VERSION;
|
|
|
|
Message.pBuffers = (PSecBuffer) &Buffers;
|
|
|
|
status = credssp->table->EncryptMessage(&credssp->context, 0, &Message, credssp->send_seq_num++);
|
|
|
|
|
|
|
|
if (status != SEC_E_OK)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "EncryptMessage status: 0x%08X", status);
|
2012-06-16 06:39:12 +04:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
SECURITY_STATUS credssp_decrypt_public_key_echo(rdpCredssp* credssp)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
|
|
|
int length;
|
2014-08-18 21:34:47 +04:00
|
|
|
BYTE* buffer;
|
2013-08-28 18:44:54 +04:00
|
|
|
ULONG pfQOP = 0;
|
2014-08-18 21:34:47 +04:00
|
|
|
BYTE* public_key1;
|
|
|
|
BYTE* public_key2;
|
2012-05-25 09:50:46 +04:00
|
|
|
int public_key_length;
|
|
|
|
SecBuffer Buffers[2];
|
|
|
|
SecBufferDesc Message;
|
|
|
|
SECURITY_STATUS status;
|
|
|
|
|
2012-06-18 23:56:40 +04:00
|
|
|
if (credssp->PublicKey.cbBuffer + credssp->ContextSizes.cbMaxSignature != credssp->pubKeyAuth.cbBuffer)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "unexpected pubKeyAuth buffer size:%d", (int) credssp->pubKeyAuth.cbBuffer);
|
2012-06-18 23:56:40 +04:00
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
}
|
|
|
|
|
2012-05-25 09:50:46 +04:00
|
|
|
length = credssp->pubKeyAuth.cbBuffer;
|
2014-08-18 21:34:47 +04:00
|
|
|
buffer = (BYTE*) malloc(length);
|
2012-06-18 23:56:40 +04:00
|
|
|
CopyMemory(buffer, credssp->pubKeyAuth.pvBuffer, length);
|
2012-05-25 09:50:46 +04:00
|
|
|
public_key_length = credssp->PublicKey.cbBuffer;
|
|
|
|
Buffers[0].BufferType = SECBUFFER_TOKEN; /* Signature */
|
|
|
|
Buffers[1].BufferType = SECBUFFER_DATA; /* Encrypted TLS Public Key */
|
|
|
|
Buffers[0].cbBuffer = credssp->ContextSizes.cbMaxSignature;
|
2012-06-18 23:56:40 +04:00
|
|
|
Buffers[0].pvBuffer = buffer;
|
|
|
|
Buffers[1].cbBuffer = length - credssp->ContextSizes.cbMaxSignature;
|
|
|
|
Buffers[1].pvBuffer = buffer + credssp->ContextSizes.cbMaxSignature;
|
2012-05-25 09:50:46 +04:00
|
|
|
Message.cBuffers = 2;
|
|
|
|
Message.ulVersion = SECBUFFER_VERSION;
|
|
|
|
Message.pBuffers = (PSecBuffer) &Buffers;
|
2012-06-20 02:06:43 +04:00
|
|
|
status = credssp->table->DecryptMessage(&credssp->context, &Message, credssp->recv_seq_num++, &pfQOP);
|
2012-05-25 09:50:46 +04:00
|
|
|
|
|
|
|
if (status != SEC_E_OK)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "DecryptMessage failure: 0x%08X", status);
|
2012-05-25 09:50:46 +04:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
public_key1 = (BYTE*) credssp->PublicKey.pvBuffer;
|
|
|
|
public_key2 = (BYTE*) Buffers[1].pvBuffer;
|
2012-05-25 09:50:46 +04:00
|
|
|
|
|
|
|
if (!credssp->server)
|
2012-06-18 09:01:08 +04:00
|
|
|
{
|
|
|
|
/* server echos the public key +1 */
|
2012-06-27 13:10:17 +04:00
|
|
|
ap_integer_decrement_le(public_key2, public_key_length);
|
2012-06-18 09:01:08 +04:00
|
|
|
}
|
2012-05-25 09:50:46 +04:00
|
|
|
|
|
|
|
if (memcmp(public_key1, public_key2, public_key_length) != 0)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "Could not verify server's public key echo");
|
|
|
|
WLog_ERR(TAG, "Expected (length = %d):", public_key_length);
|
2014-08-18 19:22:43 +04:00
|
|
|
winpr_HexDump(TAG, WLOG_ERROR, public_key1, public_key_length);
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "Actual (length = %d):", public_key_length);
|
2014-08-18 19:22:43 +04:00
|
|
|
winpr_HexDump(TAG, WLOG_ERROR, public_key2, public_key_length);
|
2012-05-25 09:50:46 +04:00
|
|
|
return SEC_E_MESSAGE_ALTERED; /* DO NOT SEND CREDENTIALS! */
|
|
|
|
}
|
|
|
|
|
2012-06-18 23:56:40 +04:00
|
|
|
free(buffer);
|
2012-05-25 09:50:46 +04:00
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
int credssp_sizeof_ts_password_creds(rdpCredssp* credssp)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2013-05-22 17:20:26 +04:00
|
|
|
int length = 0;
|
|
|
|
length += ber_sizeof_sequence_octet_string(credssp->identity.DomainLength * 2);
|
|
|
|
length += ber_sizeof_sequence_octet_string(credssp->identity.UserLength * 2);
|
|
|
|
length += ber_sizeof_sequence_octet_string(credssp->identity.PasswordLength * 2);
|
2012-05-25 09:50:46 +04:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void credssp_read_ts_password_creds(rdpCredssp* credssp, wStream* s)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
|
|
|
int length;
|
|
|
|
/* TSPasswordCreds (SEQUENCE) */
|
|
|
|
ber_read_sequence_tag(s, &length);
|
|
|
|
/* [0] domainName (OCTET STRING) */
|
2012-10-09 10:31:28 +04:00
|
|
|
ber_read_contextual_tag(s, 0, &length, TRUE);
|
2012-05-25 09:50:46 +04:00
|
|
|
ber_read_octet_string_tag(s, &length);
|
|
|
|
credssp->identity.DomainLength = (UINT32) length;
|
2014-08-18 21:34:47 +04:00
|
|
|
credssp->identity.Domain = (UINT16*) malloc(length);
|
2013-05-15 20:14:26 +04:00
|
|
|
CopyMemory(credssp->identity.Domain, Stream_Pointer(s), credssp->identity.DomainLength);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(s, credssp->identity.DomainLength);
|
2012-06-07 08:14:47 +04:00
|
|
|
credssp->identity.DomainLength /= 2;
|
2012-05-25 09:50:46 +04:00
|
|
|
/* [1] userName (OCTET STRING) */
|
2012-10-09 10:31:28 +04:00
|
|
|
ber_read_contextual_tag(s, 1, &length, TRUE);
|
2012-05-25 09:50:46 +04:00
|
|
|
ber_read_octet_string_tag(s, &length);
|
|
|
|
credssp->identity.UserLength = (UINT32) length;
|
2014-08-18 21:34:47 +04:00
|
|
|
credssp->identity.User = (UINT16*) malloc(length);
|
2013-05-15 20:14:26 +04:00
|
|
|
CopyMemory(credssp->identity.User, Stream_Pointer(s), credssp->identity.UserLength);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(s, credssp->identity.UserLength);
|
2012-06-07 08:14:47 +04:00
|
|
|
credssp->identity.UserLength /= 2;
|
2012-05-25 09:50:46 +04:00
|
|
|
/* [2] password (OCTET STRING) */
|
2012-10-09 10:31:28 +04:00
|
|
|
ber_read_contextual_tag(s, 2, &length, TRUE);
|
2012-05-25 09:50:46 +04:00
|
|
|
ber_read_octet_string_tag(s, &length);
|
|
|
|
credssp->identity.PasswordLength = (UINT32) length;
|
2014-08-18 21:34:47 +04:00
|
|
|
credssp->identity.Password = (UINT16*) malloc(length);
|
2013-05-15 20:14:26 +04:00
|
|
|
CopyMemory(credssp->identity.Password, Stream_Pointer(s), credssp->identity.PasswordLength);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(s, credssp->identity.PasswordLength);
|
2012-06-07 08:14:47 +04:00
|
|
|
credssp->identity.PasswordLength /= 2;
|
2012-09-16 23:30:11 +04:00
|
|
|
credssp->identity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
int credssp_write_ts_password_creds(rdpCredssp* credssp, wStream* s)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2013-05-21 19:06:00 +04:00
|
|
|
int size = 0;
|
|
|
|
int innerSize = credssp_sizeof_ts_password_creds(credssp);
|
2012-05-25 09:50:46 +04:00
|
|
|
/* TSPasswordCreds (SEQUENCE) */
|
2013-05-21 19:06:00 +04:00
|
|
|
size += ber_write_sequence_tag(s, innerSize);
|
2012-05-25 09:50:46 +04:00
|
|
|
/* [0] domainName (OCTET STRING) */
|
2014-08-18 21:34:47 +04:00
|
|
|
size += ber_write_sequence_octet_string(s, 0, (BYTE*) credssp->identity.Domain, credssp->identity.DomainLength * 2);
|
2012-05-25 09:50:46 +04:00
|
|
|
/* [1] userName (OCTET STRING) */
|
2014-08-18 21:34:47 +04:00
|
|
|
size += ber_write_sequence_octet_string(s, 1, (BYTE*) credssp->identity.User, credssp->identity.UserLength * 2);
|
2012-05-25 09:50:46 +04:00
|
|
|
/* [2] password (OCTET STRING) */
|
2014-08-18 21:34:47 +04:00
|
|
|
size += ber_write_sequence_octet_string(s, 2, (BYTE*) credssp->identity.Password, credssp->identity.PasswordLength * 2);
|
2013-05-21 19:06:00 +04:00
|
|
|
return size;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
int credssp_sizeof_ts_credentials(rdpCredssp* credssp)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2013-05-21 19:06:00 +04:00
|
|
|
int size = 0;
|
|
|
|
size += ber_sizeof_integer(1);
|
|
|
|
size += ber_sizeof_contextual_tag(ber_sizeof_integer(1));
|
|
|
|
size += ber_sizeof_sequence_octet_string(ber_sizeof_sequence(credssp_sizeof_ts_password_creds(credssp)));
|
|
|
|
return size;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void credssp_read_ts_credentials(rdpCredssp* credssp, PSecBuffer ts_credentials)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
wStream* s;
|
2012-05-25 09:50:46 +04:00
|
|
|
int length;
|
|
|
|
int ts_password_creds_length;
|
2013-05-09 01:48:30 +04:00
|
|
|
s = Stream_New(ts_credentials->pvBuffer, ts_credentials->cbBuffer);
|
2012-05-25 09:50:46 +04:00
|
|
|
/* TSCredentials (SEQUENCE) */
|
|
|
|
ber_read_sequence_tag(s, &length);
|
|
|
|
/* [0] credType (INTEGER) */
|
2012-10-09 10:31:28 +04:00
|
|
|
ber_read_contextual_tag(s, 0, &length, TRUE);
|
2012-05-25 09:50:46 +04:00
|
|
|
ber_read_integer(s, NULL);
|
|
|
|
/* [1] credentials (OCTET STRING) */
|
2012-10-09 10:31:28 +04:00
|
|
|
ber_read_contextual_tag(s, 1, &length, TRUE);
|
2012-05-25 09:50:46 +04:00
|
|
|
ber_read_octet_string_tag(s, &ts_password_creds_length);
|
|
|
|
credssp_read_ts_password_creds(credssp, s);
|
2013-05-09 01:48:30 +04:00
|
|
|
Stream_Free(s, FALSE);
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
int credssp_write_ts_credentials(rdpCredssp* credssp, wStream* s)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2013-05-21 19:06:00 +04:00
|
|
|
int size = 0;
|
|
|
|
int innerSize = credssp_sizeof_ts_credentials(credssp);
|
2013-05-24 00:34:34 +04:00
|
|
|
int passwordSize;
|
2012-05-25 09:50:46 +04:00
|
|
|
/* TSCredentials (SEQUENCE) */
|
2013-05-21 19:06:00 +04:00
|
|
|
size += ber_write_sequence_tag(s, innerSize);
|
2012-05-25 09:50:46 +04:00
|
|
|
/* [0] credType (INTEGER) */
|
2013-05-21 19:06:00 +04:00
|
|
|
size += ber_write_contextual_tag(s, 0, ber_sizeof_integer(1), TRUE);
|
|
|
|
size += ber_write_integer(s, 1);
|
2012-05-25 09:50:46 +04:00
|
|
|
/* [1] credentials (OCTET STRING) */
|
2013-05-24 00:34:34 +04:00
|
|
|
passwordSize = ber_sizeof_sequence(credssp_sizeof_ts_password_creds(credssp));
|
2013-05-21 19:06:00 +04:00
|
|
|
size += ber_write_contextual_tag(s, 1, ber_sizeof_octet_string(passwordSize), TRUE);
|
|
|
|
size += ber_write_octet_string_tag(s, passwordSize);
|
|
|
|
size += credssp_write_ts_password_creds(credssp, s);
|
|
|
|
return size;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode TSCredentials structure.
|
|
|
|
* @param credssp
|
|
|
|
*/
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void credssp_encode_ts_credentials(rdpCredssp* credssp)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
wStream* s;
|
2012-05-25 09:50:46 +04:00
|
|
|
int length;
|
2013-11-06 10:51:55 +04:00
|
|
|
int DomainLength;
|
|
|
|
int UserLength;
|
|
|
|
int PasswordLength;
|
|
|
|
DomainLength = credssp->identity.DomainLength;
|
|
|
|
UserLength = credssp->identity.UserLength;
|
|
|
|
PasswordLength = credssp->identity.PasswordLength;
|
|
|
|
|
2014-02-14 09:43:31 +04:00
|
|
|
if (credssp->settings->DisableCredentialsDelegation)
|
2013-11-06 10:51:55 +04:00
|
|
|
{
|
|
|
|
credssp->identity.DomainLength = 0;
|
|
|
|
credssp->identity.UserLength = 0;
|
|
|
|
credssp->identity.PasswordLength = 0;
|
|
|
|
}
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2013-05-21 19:06:00 +04:00
|
|
|
length = ber_sizeof_sequence(credssp_sizeof_ts_credentials(credssp));
|
2012-05-25 09:50:46 +04:00
|
|
|
sspi_SecBufferAlloc(&credssp->ts_credentials, length);
|
2014-08-18 21:34:47 +04:00
|
|
|
s = Stream_New((BYTE*) credssp->ts_credentials.pvBuffer, length);
|
2012-05-25 09:50:46 +04:00
|
|
|
credssp_write_ts_credentials(credssp, s);
|
2013-05-09 01:48:30 +04:00
|
|
|
|
2014-02-14 09:43:31 +04:00
|
|
|
if (credssp->settings->DisableCredentialsDelegation)
|
2013-11-06 10:51:55 +04:00
|
|
|
{
|
|
|
|
credssp->identity.DomainLength = DomainLength;
|
|
|
|
credssp->identity.UserLength = UserLength;
|
|
|
|
credssp->identity.PasswordLength = PasswordLength;
|
|
|
|
}
|
|
|
|
|
2013-05-09 01:48:30 +04:00
|
|
|
Stream_Free(s, FALSE);
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
SECURITY_STATUS credssp_encrypt_ts_credentials(rdpCredssp* credssp)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
|
|
|
SecBuffer Buffers[2];
|
|
|
|
SecBufferDesc Message;
|
|
|
|
SECURITY_STATUS status;
|
|
|
|
credssp_encode_ts_credentials(credssp);
|
|
|
|
Buffers[0].BufferType = SECBUFFER_TOKEN; /* Signature */
|
|
|
|
Buffers[1].BufferType = SECBUFFER_DATA; /* TSCredentials */
|
2012-06-20 02:06:43 +04:00
|
|
|
sspi_SecBufferAlloc(&credssp->authInfo, credssp->ContextSizes.cbMaxSignature + credssp->ts_credentials.cbBuffer);
|
|
|
|
Buffers[0].cbBuffer = credssp->ContextSizes.cbMaxSignature;
|
|
|
|
Buffers[0].pvBuffer = credssp->authInfo.pvBuffer;
|
|
|
|
ZeroMemory(Buffers[0].pvBuffer, Buffers[0].cbBuffer);
|
2012-05-25 09:50:46 +04:00
|
|
|
Buffers[1].cbBuffer = credssp->ts_credentials.cbBuffer;
|
2014-08-18 21:34:47 +04:00
|
|
|
Buffers[1].pvBuffer = &((BYTE*) credssp->authInfo.pvBuffer)[Buffers[0].cbBuffer];
|
2012-05-25 09:50:46 +04:00
|
|
|
CopyMemory(Buffers[1].pvBuffer, credssp->ts_credentials.pvBuffer, Buffers[1].cbBuffer);
|
|
|
|
Message.cBuffers = 2;
|
|
|
|
Message.ulVersion = SECBUFFER_VERSION;
|
|
|
|
Message.pBuffers = (PSecBuffer) &Buffers;
|
2012-06-16 06:39:12 +04:00
|
|
|
status = credssp->table->EncryptMessage(&credssp->context, 0, &Message, credssp->send_seq_num++);
|
2012-05-25 09:50:46 +04:00
|
|
|
|
|
|
|
if (status != SEC_E_OK)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
SECURITY_STATUS credssp_decrypt_ts_credentials(rdpCredssp* credssp)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2012-06-20 02:06:43 +04:00
|
|
|
int length;
|
2014-08-18 21:34:47 +04:00
|
|
|
BYTE* buffer;
|
2012-06-16 06:39:12 +04:00
|
|
|
ULONG pfQOP;
|
2012-05-25 09:50:46 +04:00
|
|
|
SecBuffer Buffers[2];
|
|
|
|
SecBufferDesc Message;
|
|
|
|
SECURITY_STATUS status;
|
|
|
|
Buffers[0].BufferType = SECBUFFER_TOKEN; /* Signature */
|
|
|
|
Buffers[1].BufferType = SECBUFFER_DATA; /* TSCredentials */
|
|
|
|
|
2012-06-16 06:39:12 +04:00
|
|
|
if (credssp->authInfo.cbBuffer < 1)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "credssp_decrypt_ts_credentials missing authInfo buffer");
|
2012-06-16 06:39:12 +04:00
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
}
|
|
|
|
|
2012-06-20 02:06:43 +04:00
|
|
|
length = credssp->authInfo.cbBuffer;
|
2014-08-18 21:34:47 +04:00
|
|
|
buffer = (BYTE*) malloc(length);
|
2012-06-20 02:06:43 +04:00
|
|
|
CopyMemory(buffer, credssp->authInfo.pvBuffer, length);
|
|
|
|
Buffers[0].cbBuffer = credssp->ContextSizes.cbMaxSignature;
|
|
|
|
Buffers[0].pvBuffer = buffer;
|
|
|
|
Buffers[1].cbBuffer = length - credssp->ContextSizes.cbMaxSignature;
|
|
|
|
Buffers[1].pvBuffer = &buffer[credssp->ContextSizes.cbMaxSignature];
|
2012-05-25 09:50:46 +04:00
|
|
|
Message.cBuffers = 2;
|
|
|
|
Message.ulVersion = SECBUFFER_VERSION;
|
|
|
|
Message.pBuffers = (PSecBuffer) &Buffers;
|
2012-06-16 06:39:12 +04:00
|
|
|
status = credssp->table->DecryptMessage(&credssp->context, &Message, credssp->recv_seq_num++, &pfQOP);
|
2012-05-25 09:50:46 +04:00
|
|
|
|
|
|
|
if (status != SEC_E_OK)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
credssp_read_ts_credentials(credssp, &Buffers[1]);
|
2012-06-20 02:06:43 +04:00
|
|
|
free(buffer);
|
2012-05-25 09:50:46 +04:00
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
2013-05-21 19:06:00 +04:00
|
|
|
int credssp_sizeof_nego_token(int length)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2013-05-21 19:06:00 +04:00
|
|
|
length = ber_sizeof_octet_string(length);
|
|
|
|
length += ber_sizeof_contextual_tag(length);
|
2012-05-25 09:50:46 +04:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2013-05-21 19:06:00 +04:00
|
|
|
int credssp_sizeof_nego_tokens(int length)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2013-05-21 19:06:00 +04:00
|
|
|
length = credssp_sizeof_nego_token(length);
|
|
|
|
length += ber_sizeof_sequence_tag(length);
|
|
|
|
length += ber_sizeof_sequence_tag(length);
|
|
|
|
length += ber_sizeof_contextual_tag(length);
|
2012-05-25 09:50:46 +04:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2013-05-21 19:06:00 +04:00
|
|
|
int credssp_sizeof_pub_key_auth(int length)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2013-05-21 19:06:00 +04:00
|
|
|
length = ber_sizeof_octet_string(length);
|
|
|
|
length += ber_sizeof_contextual_tag(length);
|
2012-05-25 09:50:46 +04:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2013-05-21 19:06:00 +04:00
|
|
|
int credssp_sizeof_auth_info(int length)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2013-05-21 19:06:00 +04:00
|
|
|
length = ber_sizeof_octet_string(length);
|
|
|
|
length += ber_sizeof_contextual_tag(length);
|
2012-05-25 09:50:46 +04:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2013-05-21 19:06:00 +04:00
|
|
|
int credssp_sizeof_ts_request(int length)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2013-05-21 19:06:00 +04:00
|
|
|
length += ber_sizeof_integer(2);
|
|
|
|
length += ber_sizeof_contextual_tag(3);
|
2012-05-25 09:50:46 +04:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send CredSSP message.
|
|
|
|
* @param credssp
|
|
|
|
*/
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void credssp_send(rdpCredssp* credssp)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
wStream* s;
|
2012-05-25 09:50:46 +04:00
|
|
|
int length;
|
|
|
|
int ts_request_length;
|
|
|
|
int nego_tokens_length;
|
|
|
|
int pub_key_auth_length;
|
|
|
|
int auth_info_length;
|
2013-05-21 19:06:00 +04:00
|
|
|
nego_tokens_length = (credssp->negoToken.cbBuffer > 0) ? credssp_sizeof_nego_tokens(credssp->negoToken.cbBuffer) : 0;
|
|
|
|
pub_key_auth_length = (credssp->pubKeyAuth.cbBuffer > 0) ? credssp_sizeof_pub_key_auth(credssp->pubKeyAuth.cbBuffer) : 0;
|
|
|
|
auth_info_length = (credssp->authInfo.cbBuffer > 0) ? credssp_sizeof_auth_info(credssp->authInfo.cbBuffer) : 0;
|
2012-05-25 09:50:46 +04:00
|
|
|
length = nego_tokens_length + pub_key_auth_length + auth_info_length;
|
2013-05-21 19:06:00 +04:00
|
|
|
ts_request_length = credssp_sizeof_ts_request(length);
|
|
|
|
s = Stream_New(NULL, ber_sizeof_sequence(ts_request_length));
|
2012-05-25 09:50:46 +04:00
|
|
|
/* TSRequest */
|
2013-05-21 19:06:00 +04:00
|
|
|
ber_write_sequence_tag(s, ts_request_length); /* SEQUENCE */
|
2012-06-18 23:56:40 +04:00
|
|
|
/* [0] version */
|
2012-10-09 10:31:28 +04:00
|
|
|
ber_write_contextual_tag(s, 0, 3, TRUE);
|
2012-05-25 09:50:46 +04:00
|
|
|
ber_write_integer(s, 2); /* INTEGER */
|
|
|
|
|
|
|
|
/* [1] negoTokens (NegoData) */
|
|
|
|
if (nego_tokens_length > 0)
|
|
|
|
{
|
2013-02-27 00:32:36 +04:00
|
|
|
length = nego_tokens_length;
|
2013-05-21 19:06:00 +04:00
|
|
|
length -= ber_write_contextual_tag(s, 1, ber_sizeof_sequence(ber_sizeof_sequence(ber_sizeof_sequence_octet_string(credssp->negoToken.cbBuffer))), TRUE); /* NegoData */
|
|
|
|
length -= ber_write_sequence_tag(s, ber_sizeof_sequence(ber_sizeof_sequence_octet_string(credssp->negoToken.cbBuffer))); /* SEQUENCE OF NegoDataItem */
|
|
|
|
length -= ber_write_sequence_tag(s, ber_sizeof_sequence_octet_string(credssp->negoToken.cbBuffer)); /* NegoDataItem */
|
2014-08-18 21:34:47 +04:00
|
|
|
length -= ber_write_sequence_octet_string(s, 0, (BYTE*) credssp->negoToken.pvBuffer, credssp->negoToken.cbBuffer); /* OCTET STRING */
|
2013-05-22 17:20:26 +04:00
|
|
|
// assert length == 0
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* [2] authInfo (OCTET STRING) */
|
|
|
|
if (auth_info_length > 0)
|
|
|
|
{
|
2013-02-27 00:32:36 +04:00
|
|
|
length = auth_info_length;
|
2013-05-21 19:06:00 +04:00
|
|
|
length -= ber_write_sequence_octet_string(s, 2, credssp->authInfo.pvBuffer, credssp->authInfo.cbBuffer);
|
2013-05-22 17:20:26 +04:00
|
|
|
// assert length == 0
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* [3] pubKeyAuth (OCTET STRING) */
|
|
|
|
if (pub_key_auth_length > 0)
|
|
|
|
{
|
2013-02-27 00:32:36 +04:00
|
|
|
length = pub_key_auth_length;
|
2013-05-21 19:06:00 +04:00
|
|
|
length -= ber_write_sequence_octet_string(s, 3, credssp->pubKeyAuth.pvBuffer, credssp->pubKeyAuth.cbBuffer);
|
2013-05-22 17:20:26 +04:00
|
|
|
// assert length == 0
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2013-05-15 20:14:26 +04:00
|
|
|
Stream_SealLength(s);
|
2012-11-15 06:30:21 +04:00
|
|
|
transport_write(credssp->transport, s);
|
2013-05-09 01:48:30 +04:00
|
|
|
Stream_Free(s, TRUE);
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Receive CredSSP message.
|
|
|
|
* @param credssp
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
int credssp_recv(rdpCredssp* credssp)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
wStream* s;
|
2012-05-25 09:50:46 +04:00
|
|
|
int length;
|
|
|
|
int status;
|
|
|
|
UINT32 version;
|
2013-05-09 01:48:30 +04:00
|
|
|
s = Stream_New(NULL, 4096);
|
2014-07-22 13:19:38 +04:00
|
|
|
status = transport_read_pdu(credssp->transport, s);
|
2012-05-25 09:50:46 +04:00
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "credssp_recv() error: %d", status);
|
2013-05-09 01:48:30 +04:00
|
|
|
Stream_Free(s, TRUE);
|
2012-05-25 09:50:46 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TSRequest */
|
2014-08-18 19:22:43 +04:00
|
|
|
if (!ber_read_sequence_tag(s, &length) ||
|
|
|
|
!ber_read_contextual_tag(s, 0, &length, TRUE) ||
|
|
|
|
!ber_read_integer(s, &version))
|
2013-08-28 18:44:54 +04:00
|
|
|
{
|
|
|
|
Stream_Free(s, TRUE);
|
2013-01-11 04:27:19 +04:00
|
|
|
return -1;
|
2013-08-28 18:44:54 +04:00
|
|
|
}
|
2012-05-25 09:50:46 +04:00
|
|
|
|
|
|
|
/* [1] negoTokens (NegoData) */
|
2012-10-09 10:31:28 +04:00
|
|
|
if (ber_read_contextual_tag(s, 1, &length, TRUE) != FALSE)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2013-01-11 04:27:19 +04:00
|
|
|
if (!ber_read_sequence_tag(s, &length) || /* SEQUENCE OF NegoDataItem */
|
2014-08-18 19:22:43 +04:00
|
|
|
!ber_read_sequence_tag(s, &length) || /* NegoDataItem */
|
|
|
|
!ber_read_contextual_tag(s, 0, &length, TRUE) || /* [0] negoToken */
|
|
|
|
!ber_read_octet_string_tag(s, &length) || /* OCTET STRING */
|
|
|
|
((int) Stream_GetRemainingLength(s)) < length)
|
2013-08-28 18:44:54 +04:00
|
|
|
{
|
|
|
|
Stream_Free(s, TRUE);
|
2013-01-11 04:27:19 +04:00
|
|
|
return -1;
|
2013-08-28 18:44:54 +04:00
|
|
|
}
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2012-05-25 09:50:46 +04:00
|
|
|
sspi_SecBufferAlloc(&credssp->negoToken, length);
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read(s, credssp->negoToken.pvBuffer, length);
|
2012-05-25 09:50:46 +04:00
|
|
|
credssp->negoToken.cbBuffer = length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [2] authInfo (OCTET STRING) */
|
2012-10-09 10:31:28 +04:00
|
|
|
if (ber_read_contextual_tag(s, 2, &length, TRUE) != FALSE)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2014-02-11 07:12:13 +04:00
|
|
|
if (!ber_read_octet_string_tag(s, &length) || /* OCTET STRING */
|
2014-08-18 19:22:43 +04:00
|
|
|
((int) Stream_GetRemainingLength(s)) < length)
|
2013-08-28 18:44:54 +04:00
|
|
|
{
|
|
|
|
Stream_Free(s, TRUE);
|
2013-01-11 04:27:19 +04:00
|
|
|
return -1;
|
2013-08-28 18:44:54 +04:00
|
|
|
}
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2012-05-25 09:50:46 +04:00
|
|
|
sspi_SecBufferAlloc(&credssp->authInfo, length);
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read(s, credssp->authInfo.pvBuffer, length);
|
2012-05-25 09:50:46 +04:00
|
|
|
credssp->authInfo.cbBuffer = length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [3] pubKeyAuth (OCTET STRING) */
|
2012-10-09 10:31:28 +04:00
|
|
|
if (ber_read_contextual_tag(s, 3, &length, TRUE) != FALSE)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2014-02-11 07:12:13 +04:00
|
|
|
if (!ber_read_octet_string_tag(s, &length) || /* OCTET STRING */
|
2014-08-18 19:22:43 +04:00
|
|
|
((int) Stream_GetRemainingLength(s)) < length)
|
2013-08-28 18:44:54 +04:00
|
|
|
{
|
|
|
|
Stream_Free(s, TRUE);
|
2013-01-11 04:27:19 +04:00
|
|
|
return -1;
|
2013-08-28 18:44:54 +04:00
|
|
|
}
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2012-05-25 09:50:46 +04:00
|
|
|
sspi_SecBufferAlloc(&credssp->pubKeyAuth, length);
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read(s, credssp->pubKeyAuth.pvBuffer, length);
|
2012-05-25 09:50:46 +04:00
|
|
|
credssp->pubKeyAuth.cbBuffer = length;
|
|
|
|
}
|
|
|
|
|
2013-05-09 01:48:30 +04:00
|
|
|
Stream_Free(s, TRUE);
|
2012-05-25 09:50:46 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void credssp_buffer_print(rdpCredssp* credssp)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
|
|
|
if (credssp->negoToken.cbBuffer > 0)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "CredSSP.negoToken (length = %d):", (int) credssp->negoToken.cbBuffer);
|
2014-08-18 19:22:43 +04:00
|
|
|
winpr_HexDump(TAG, WLOG_ERROR, credssp->negoToken.pvBuffer,
|
|
|
|
credssp->negoToken.cbBuffer);
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (credssp->pubKeyAuth.cbBuffer > 0)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "CredSSP.pubKeyAuth (length = %d):", (int) credssp->pubKeyAuth.cbBuffer);
|
2014-08-18 19:22:43 +04:00
|
|
|
winpr_HexDump(TAG, WLOG_ERROR, credssp->pubKeyAuth.pvBuffer,
|
|
|
|
credssp->pubKeyAuth.cbBuffer);
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (credssp->authInfo.cbBuffer > 0)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "CredSSP.authInfo (length = %d):", (int) credssp->authInfo.cbBuffer);
|
2014-08-18 19:22:43 +04:00
|
|
|
winpr_HexDump(TAG, WLOG_ERROR, credssp->authInfo.pvBuffer,
|
|
|
|
credssp->authInfo.cbBuffer);
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void credssp_buffer_free(rdpCredssp* credssp)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
|
|
|
sspi_SecBufferFree(&credssp->negoToken);
|
|
|
|
sspi_SecBufferFree(&credssp->pubKeyAuth);
|
|
|
|
sspi_SecBufferFree(&credssp->authInfo);
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
LPTSTR credssp_make_spn(const char* ServiceClass, const char* hostname)
|
2014-02-12 09:43:02 +04:00
|
|
|
{
|
|
|
|
DWORD status;
|
|
|
|
DWORD SpnLength;
|
|
|
|
LPTSTR hostnameX = NULL;
|
|
|
|
LPTSTR ServiceClassX = NULL;
|
|
|
|
LPTSTR ServicePrincipalName = NULL;
|
|
|
|
#ifdef UNICODE
|
|
|
|
ConvertToUnicode(CP_UTF8, 0, hostname, -1, &hostnameX, 0);
|
|
|
|
ConvertToUnicode(CP_UTF8, 0, ServiceClass, -1, &ServiceClassX, 0);
|
|
|
|
#else
|
|
|
|
hostnameX = _strdup(hostname);
|
|
|
|
ServiceClassX = _strdup(ServiceClass);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!ServiceClass)
|
|
|
|
{
|
|
|
|
ServicePrincipalName = (LPTSTR) _tcsdup(hostnameX);
|
|
|
|
free(ServiceClassX);
|
|
|
|
free(hostnameX);
|
|
|
|
return ServicePrincipalName;
|
|
|
|
}
|
|
|
|
|
|
|
|
SpnLength = 0;
|
|
|
|
status = DsMakeSpn(ServiceClassX, hostnameX, NULL, 0, NULL, &SpnLength, NULL);
|
|
|
|
|
|
|
|
if (status != ERROR_BUFFER_OVERFLOW)
|
|
|
|
{
|
|
|
|
free(ServiceClassX);
|
|
|
|
free(hostnameX);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ServicePrincipalName = (LPTSTR) malloc(SpnLength * sizeof(TCHAR));
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-04-10 23:07:53 +04:00
|
|
|
if (!ServicePrincipalName)
|
|
|
|
return NULL;
|
2014-02-12 09:43:02 +04:00
|
|
|
|
|
|
|
status = DsMakeSpn(ServiceClassX, hostnameX, NULL, 0, NULL, &SpnLength, ServicePrincipalName);
|
|
|
|
|
|
|
|
if (status != ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
free(ServicePrincipalName);
|
|
|
|
free(ServiceClassX);
|
|
|
|
free(hostnameX);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(ServiceClassX);
|
|
|
|
free(hostnameX);
|
|
|
|
return ServicePrincipalName;
|
|
|
|
}
|
|
|
|
|
2012-05-25 09:50:46 +04:00
|
|
|
/**
|
|
|
|
* Create new CredSSP state machine.
|
|
|
|
* @param transport
|
|
|
|
* @return new CredSSP state machine.
|
|
|
|
*/
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
rdpCredssp* credssp_new(freerdp* instance, rdpTransport* transport, rdpSettings* settings)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
rdpCredssp* credssp;
|
|
|
|
credssp = (rdpCredssp*) calloc(1, sizeof(rdpCredssp));
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2014-02-12 09:43:02 +04:00
|
|
|
if (credssp)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2012-08-11 02:05:37 +04:00
|
|
|
HKEY hKey;
|
|
|
|
LONG status;
|
|
|
|
DWORD dwType;
|
|
|
|
DWORD dwSize;
|
2012-05-25 09:50:46 +04:00
|
|
|
credssp->instance = instance;
|
|
|
|
credssp->settings = settings;
|
2012-11-08 03:23:25 +04:00
|
|
|
credssp->server = settings->ServerMode;
|
2012-11-15 06:30:21 +04:00
|
|
|
credssp->transport = transport;
|
2012-05-25 09:50:46 +04:00
|
|
|
credssp->send_seq_num = 0;
|
|
|
|
credssp->recv_seq_num = 0;
|
2012-06-16 06:39:12 +04:00
|
|
|
ZeroMemory(&credssp->negoToken, sizeof(SecBuffer));
|
|
|
|
ZeroMemory(&credssp->pubKeyAuth, sizeof(SecBuffer));
|
|
|
|
ZeroMemory(&credssp->authInfo, sizeof(SecBuffer));
|
2013-07-01 21:24:19 +04:00
|
|
|
SecInvalidateHandle(&credssp->context);
|
2012-08-11 02:05:37 +04:00
|
|
|
|
2012-08-31 01:37:12 +04:00
|
|
|
if (credssp->server)
|
2012-08-11 02:05:37 +04:00
|
|
|
{
|
2012-08-31 01:37:12 +04:00
|
|
|
status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\FreeRDP\\Server"),
|
2014-08-18 19:22:43 +04:00
|
|
|
0, KEY_READ | KEY_WOW64_64KEY, &hKey);
|
2012-08-11 02:05:37 +04:00
|
|
|
|
|
|
|
if (status == ERROR_SUCCESS)
|
|
|
|
{
|
2012-08-31 01:37:12 +04:00
|
|
|
status = RegQueryValueEx(hKey, _T("SspiModule"), NULL, &dwType, NULL, &dwSize);
|
2012-08-11 02:05:37 +04:00
|
|
|
|
|
|
|
if (status == ERROR_SUCCESS)
|
|
|
|
{
|
2012-08-31 01:37:12 +04:00
|
|
|
credssp->SspiModule = (LPTSTR) malloc(dwSize + sizeof(TCHAR));
|
|
|
|
status = RegQueryValueEx(hKey, _T("SspiModule"), NULL, &dwType,
|
2014-08-18 21:34:47 +04:00
|
|
|
(BYTE*) credssp->SspiModule, &dwSize);
|
2012-08-31 01:37:12 +04:00
|
|
|
|
|
|
|
if (status == ERROR_SUCCESS)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_INFO(TAG, "Using SSPI Module: %s", credssp->SspiModule);
|
2012-08-31 01:37:12 +04:00
|
|
|
RegCloseKey(hKey);
|
|
|
|
}
|
2012-08-11 02:05:37 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return credssp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free CredSSP state machine.
|
|
|
|
* @param credssp
|
|
|
|
*/
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void credssp_free(rdpCredssp* credssp)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2014-02-12 09:43:02 +04:00
|
|
|
if (credssp)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
|
|
|
if (credssp->table)
|
|
|
|
credssp->table->DeleteSecurityContext(&credssp->context);
|
|
|
|
|
|
|
|
sspi_SecBufferFree(&credssp->PublicKey);
|
|
|
|
sspi_SecBufferFree(&credssp->ts_credentials);
|
2012-11-19 22:26:56 +04:00
|
|
|
free(credssp->ServicePrincipalName);
|
2012-05-25 09:50:46 +04:00
|
|
|
free(credssp->identity.User);
|
|
|
|
free(credssp->identity.Domain);
|
|
|
|
free(credssp->identity.Password);
|
2012-09-16 23:30:11 +04:00
|
|
|
free(credssp);
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
}
|