2012-05-25 09:50:46 +04:00
|
|
|
/**
|
2017-05-11 19:51:45 +03:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2012-05-25 09:50:46 +04:00
|
|
|
* Network Level Authentication (NLA)
|
|
|
|
*
|
|
|
|
* Copyright 2010-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2015-05-29 14:46:50 +03:00
|
|
|
* Copyright 2015 Thincast Technologies GmbH
|
|
|
|
* Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
|
2016-03-18 15:32:13 +03:00
|
|
|
* Copyright 2016 Martin Fleisz <martin.fleisz@thincast.com>
|
2017-05-11 19:51:45 +03:00
|
|
|
* Copyright 2017 Dorian Ducournau <dorian.ducournau@gmail.com>
|
2012-05-25 09:50:46 +04:00
|
|
|
*
|
|
|
|
* 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>
|
2015-11-05 16:02:07 +03:00
|
|
|
#include <freerdp/build-config.h>
|
2017-05-15 19:52:39 +03:00
|
|
|
#include <freerdp/peer.h>
|
2012-05-25 09:50:46 +04:00
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
2015-10-09 20:48:41 +03:00
|
|
|
#include <winpr/sam.h>
|
2012-05-25 09:50:46 +04:00
|
|
|
#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"
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
#define TAG FREERDP_TAG("core.nla")
|
2014-08-19 20:26:39 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
#define SERVER_KEY "Software\\" FREERDP_VENDOR_STRING "\\" FREERDP_PRODUCT_STRING "\\Server"
|
2015-06-24 12:12:59 +03:00
|
|
|
|
2012-05-25 09:50:46 +04:00
|
|
|
/**
|
|
|
|
* TSRequest ::= SEQUENCE {
|
|
|
|
* version [0] INTEGER,
|
|
|
|
* negoTokens [1] NegoData OPTIONAL,
|
|
|
|
* authInfo [2] OCTET STRING OPTIONAL,
|
2016-03-18 15:32:13 +03:00
|
|
|
* pubKeyAuth [3] OCTET STRING OPTIONAL,
|
|
|
|
* errorCode [4] INTEGER OPTIONAL
|
2012-05-25 09:50:46 +04:00
|
|
|
* }
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
#define NLA_PKG_NAME NEGO_SSP_NAME
|
2012-06-07 03:59:17 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
#define TERMSRV_SPN_PREFIX "TERMSRV/"
|
2018-11-20 18:48:59 +03:00
|
|
|
|
|
|
|
struct rdp_nla
|
|
|
|
{
|
|
|
|
BOOL server;
|
|
|
|
NLA_STATE state;
|
|
|
|
int sendSeqNum;
|
|
|
|
int recvSeqNum;
|
|
|
|
freerdp* instance;
|
|
|
|
CtxtHandle context;
|
|
|
|
LPTSTR SspiModule;
|
|
|
|
char* SamFile;
|
|
|
|
rdpSettings* settings;
|
|
|
|
rdpTransport* transport;
|
|
|
|
UINT32 cbMaxToken;
|
|
|
|
#if defined(UNICODE)
|
|
|
|
SEC_WCHAR* packageName;
|
|
|
|
#else
|
|
|
|
SEC_CHAR* packageName;
|
|
|
|
#endif
|
|
|
|
UINT32 version;
|
|
|
|
UINT32 peerVersion;
|
|
|
|
UINT32 errorCode;
|
|
|
|
ULONG fContextReq;
|
|
|
|
ULONG pfContextAttr;
|
|
|
|
BOOL haveContext;
|
|
|
|
BOOL haveInputBuffer;
|
|
|
|
BOOL havePubKeyAuth;
|
|
|
|
SECURITY_STATUS status;
|
|
|
|
CredHandle credentials;
|
|
|
|
TimeStamp expiration;
|
|
|
|
PSecPkgInfo pPackageInfo;
|
|
|
|
SecBuffer inputBuffer;
|
|
|
|
SecBuffer outputBuffer;
|
|
|
|
SecBufferDesc inputBufferDesc;
|
|
|
|
SecBufferDesc outputBufferDesc;
|
|
|
|
SecBuffer negoToken;
|
|
|
|
SecBuffer pubKeyAuth;
|
|
|
|
SecBuffer authInfo;
|
|
|
|
SecBuffer ClientNonce;
|
|
|
|
SecBuffer PublicKey;
|
|
|
|
SecBuffer tsCredentials;
|
|
|
|
LPTSTR ServicePrincipalName;
|
|
|
|
SEC_WINNT_AUTH_IDENTITY* identity;
|
|
|
|
PSecurityFunctionTable table;
|
|
|
|
SecPkgContext_Sizes ContextSizes;
|
|
|
|
};
|
|
|
|
|
2015-06-15 16:03:13 +03:00
|
|
|
static BOOL nla_send(rdpNla* nla);
|
|
|
|
static int nla_recv(rdpNla* nla);
|
|
|
|
static void nla_buffer_print(rdpNla* nla);
|
|
|
|
static void nla_buffer_free(rdpNla* nla);
|
|
|
|
static SECURITY_STATUS nla_encrypt_public_key_echo(rdpNla* nla);
|
2018-03-20 11:57:30 +03:00
|
|
|
static SECURITY_STATUS nla_encrypt_public_key_hash(rdpNla* nla);
|
2015-06-15 16:03:13 +03:00
|
|
|
static SECURITY_STATUS nla_decrypt_public_key_echo(rdpNla* nla);
|
2018-03-20 11:57:30 +03:00
|
|
|
static SECURITY_STATUS nla_decrypt_public_key_hash(rdpNla* nla);
|
2015-06-15 16:03:13 +03:00
|
|
|
static SECURITY_STATUS nla_encrypt_ts_credentials(rdpNla* nla);
|
|
|
|
static SECURITY_STATUS nla_decrypt_ts_credentials(rdpNla* nla);
|
|
|
|
static BOOL nla_read_ts_password_creds(rdpNla* nla, wStream* s);
|
2015-06-23 16:40:37 +03:00
|
|
|
static void nla_identity_free(SEC_WINNT_AUTH_IDENTITY* identity);
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2019-11-06 17:24:51 +03: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)
|
2013-05-21 19:06:00 +04:00
|
|
|
|
2018-03-21 13:30:02 +03:00
|
|
|
/* CredSSP Client-To-Server Binding Hash\0 */
|
2019-11-06 17:24:51 +03:00
|
|
|
static const BYTE ClientServerHashMagic[] = { 0x43, 0x72, 0x65, 0x64, 0x53, 0x53, 0x50, 0x20,
|
|
|
|
0x43, 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x2D, 0x54,
|
|
|
|
0x6F, 0x2D, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72,
|
|
|
|
0x20, 0x42, 0x69, 0x6E, 0x64, 0x69, 0x6E, 0x67,
|
|
|
|
0x20, 0x48, 0x61, 0x73, 0x68, 0x00 };
|
2018-03-21 13:30:02 +03:00
|
|
|
|
|
|
|
/* CredSSP Server-To-Client Binding Hash\0 */
|
2019-11-06 17:24:51 +03:00
|
|
|
static const BYTE ServerClientHashMagic[] = { 0x43, 0x72, 0x65, 0x64, 0x53, 0x53, 0x50, 0x20,
|
|
|
|
0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x2D, 0x54,
|
|
|
|
0x6F, 0x2D, 0x43, 0x6C, 0x69, 0x65, 0x6E, 0x74,
|
|
|
|
0x20, 0x42, 0x69, 0x6E, 0x64, 0x69, 0x6E, 0x67,
|
|
|
|
0x20, 0x48, 0x61, 0x73, 0x68, 0x00 };
|
2018-03-21 13:30:02 +03:00
|
|
|
|
2018-03-20 11:57:30 +03:00
|
|
|
static const UINT32 NonceLength = 32;
|
|
|
|
|
2015-06-23 16:40:37 +03:00
|
|
|
void nla_identity_free(SEC_WINNT_AUTH_IDENTITY* identity)
|
|
|
|
{
|
|
|
|
if (identity)
|
|
|
|
{
|
2017-05-11 19:51:45 +03:00
|
|
|
/* Password authentication */
|
2015-06-26 16:12:33 +03:00
|
|
|
if (identity->User)
|
|
|
|
{
|
2015-07-07 14:46:05 +03:00
|
|
|
memset(identity->User, 0, identity->UserLength * 2);
|
2015-06-26 16:12:33 +03:00
|
|
|
free(identity->User);
|
|
|
|
}
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-06-26 16:12:33 +03:00
|
|
|
if (identity->Password)
|
|
|
|
{
|
2018-05-03 18:49:14 +03:00
|
|
|
size_t len = identity->PasswordLength;
|
|
|
|
|
|
|
|
if (len > LB_PASSWORD_MAX_LENGTH) /* [pth] Password hash */
|
|
|
|
len -= LB_PASSWORD_MAX_LENGTH;
|
|
|
|
|
|
|
|
memset(identity->Password, 0, len * 2);
|
2015-06-26 16:12:33 +03:00
|
|
|
free(identity->Password);
|
|
|
|
}
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-06-26 16:12:33 +03:00
|
|
|
if (identity->Domain)
|
|
|
|
{
|
2015-07-07 14:46:05 +03:00
|
|
|
memset(identity->Domain, 0, identity->DomainLength * 2);
|
2015-06-26 16:12:33 +03:00
|
|
|
free(identity->Domain);
|
|
|
|
}
|
2015-06-23 16:40:37 +03:00
|
|
|
}
|
|
|
|
|
2017-02-20 20:31:58 +03:00
|
|
|
free(identity);
|
2015-06-23 16:40:37 +03:00
|
|
|
}
|
|
|
|
|
2012-05-25 09:50:46 +04:00
|
|
|
/**
|
2017-05-11 19:51:45 +03:00
|
|
|
* Initialize NTLM/Kerberos SSP authentication module (client).
|
2012-05-25 09:50:46 +04:00
|
|
|
* @param credssp
|
|
|
|
*/
|
|
|
|
|
2017-07-18 13:54:00 +03:00
|
|
|
static int nla_client_init(rdpNla* nla)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
char* spn;
|
2018-03-21 13:30:02 +03:00
|
|
|
size_t length;
|
2014-08-18 21:34:47 +04:00
|
|
|
rdpTls* tls = NULL;
|
2015-02-18 05:01:27 +03:00
|
|
|
BOOL PromptPassword = FALSE;
|
|
|
|
freerdp* instance = nla->instance;
|
|
|
|
rdpSettings* settings = nla->settings;
|
2015-10-09 20:48:41 +03:00
|
|
|
WINPR_SAM* sam;
|
|
|
|
WINPR_SAM_ENTRY* entry;
|
2015-02-15 22:54:10 +03:00
|
|
|
nla->state = NLA_STATE_INITIAL;
|
|
|
|
|
2014-02-14 09:43:31 +04:00
|
|
|
if (settings->RestrictedAdminModeRequired)
|
|
|
|
settings->DisableCredentialsDelegation = TRUE;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if ((!settings->Username) || (!strlen(settings->Username)) ||
|
|
|
|
((!settings->Password) && (!settings->RedirectionPassword)))
|
2013-11-06 19:02:58 +04:00
|
|
|
{
|
|
|
|
PromptPassword = TRUE;
|
|
|
|
}
|
|
|
|
|
2015-10-09 20:48:41 +03:00
|
|
|
if (PromptPassword && settings->Username && strlen(settings->Username))
|
|
|
|
{
|
2016-07-22 01:58:24 +03:00
|
|
|
sam = SamOpen(NULL, TRUE);
|
2015-10-09 20:48:41 +03:00
|
|
|
|
|
|
|
if (sam)
|
|
|
|
{
|
|
|
|
entry = SamLookupUserA(sam, settings->Username, strlen(settings->Username), NULL, 0);
|
|
|
|
|
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The user could be found in SAM database.
|
|
|
|
* Use entry in SAM database later instead of prompt
|
|
|
|
*/
|
|
|
|
PromptPassword = FALSE;
|
|
|
|
SamFreeEntry(sam, entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
SamClose(sam);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-06 19:02:58 +04:00
|
|
|
#ifndef _WIN32
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2013-11-06 19:02:58 +04:00
|
|
|
if (PromptPassword)
|
|
|
|
{
|
|
|
|
if (settings->RestrictedAdminModeRequired)
|
|
|
|
{
|
|
|
|
if ((settings->PasswordHash) && (strlen(settings->PasswordHash) > 0))
|
|
|
|
PromptPassword = FALSE;
|
|
|
|
}
|
|
|
|
}
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2013-11-06 19:02:58 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (PromptPassword)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2019-08-20 15:52:39 +03:00
|
|
|
if (!instance->Authenticate)
|
2019-07-15 11:08:39 +03:00
|
|
|
{
|
2020-01-08 19:39:25 +03:00
|
|
|
freerdp_set_last_error_log(instance->context,
|
|
|
|
FREERDP_ERROR_CONNECT_NO_OR_MISSING_CREDENTIALS);
|
2019-07-15 11:08:39 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
BOOL proceed = instance->Authenticate(instance, &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
|
|
|
{
|
2020-01-08 19:39:25 +03:00
|
|
|
freerdp_set_last_error_log(instance->context,
|
|
|
|
FREERDP_ERROR_CONNECT_NO_OR_MISSING_CREDENTIALS);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-15 16:03:13 +03:00
|
|
|
if (!settings->Username)
|
|
|
|
{
|
2015-06-23 16:40:37 +03:00
|
|
|
nla_identity_free(nla->identity);
|
2015-06-15 16:03:13 +03:00
|
|
|
nla->identity = NULL;
|
|
|
|
}
|
|
|
|
else
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
2017-12-06 18:32:56 +03:00
|
|
|
if (settings->RedirectionPassword && settings->RedirectionPasswordLength > 0)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
if (sspi_SetAuthIdentityWithUnicodePassword(
|
|
|
|
nla->identity, settings->Username, settings->Domain,
|
|
|
|
(UINT16*)settings->RedirectionPassword,
|
2017-12-21 13:04:32 +03:00
|
|
|
settings->RedirectionPasswordLength / sizeof(WCHAR) - 1) < 0)
|
2017-12-06 18:32:56 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-05-04 10:54:49 +03:00
|
|
|
BOOL usePassword = TRUE;
|
2015-06-23 13:07:38 +03:00
|
|
|
|
2018-05-04 10:54:49 +03:00
|
|
|
if (settings->RestrictedAdminModeRequired)
|
2013-11-06 19:02:58 +04:00
|
|
|
{
|
2018-05-04 10:54:49 +03:00
|
|
|
if (settings->PasswordHash)
|
2013-11-06 19:02:58 +04:00
|
|
|
{
|
2018-05-04 10:54:49 +03:00
|
|
|
if (strlen(settings->PasswordHash) == 32)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
if (sspi_SetAuthIdentity(nla->identity, settings->Username,
|
|
|
|
settings->Domain, settings->PasswordHash) < 0)
|
2018-05-04 10:54:49 +03:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/**
|
2019-11-06 17:24:51 +03:00
|
|
|
* Increase password hash length by LB_PASSWORD_MAX_LENGTH to obtain a
|
|
|
|
* length exceeding the maximum (LB_PASSWORD_MAX_LENGTH) and use it this for
|
|
|
|
* hash identification in WinPR.
|
2018-05-04 10:54:49 +03:00
|
|
|
*/
|
|
|
|
nla->identity->PasswordLength += LB_PASSWORD_MAX_LENGTH;
|
|
|
|
usePassword = FALSE;
|
|
|
|
}
|
2013-11-06 19:02:58 +04:00
|
|
|
}
|
|
|
|
}
|
2018-05-04 10:54:49 +03:00
|
|
|
|
|
|
|
if (usePassword)
|
|
|
|
{
|
|
|
|
if (sspi_SetAuthIdentity(nla->identity, settings->Username, settings->Domain,
|
|
|
|
settings->Password) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
2013-11-06 19:02:58 +04:00
|
|
|
}
|
|
|
|
}
|
2018-05-04 10:54:49 +03:00
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
tls = nla->transport->tls;
|
2015-02-11 22:27:29 +03:00
|
|
|
|
|
|
|
if (!tls)
|
2013-10-31 01:56:44 +04:00
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "Unknown NLA transport layer");
|
2015-02-15 19:10:14 +03:00
|
|
|
return -1;
|
2013-10-29 00:39:10 +04:00
|
|
|
}
|
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!sspi_SecBufferAlloc(&nla->PublicKey, tls->PublicKeyLength))
|
|
|
|
{
|
2017-05-11 19:51:45 +03:00
|
|
|
WLog_ERR(TAG, "Failed to allocate sspi secBuffer");
|
2015-04-03 17:21:01 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
CopyMemory(nla->PublicKey.pvBuffer, tls->PublicKey, tls->PublicKeyLength);
|
2012-11-08 03:23:25 +04:00
|
|
|
length = sizeof(TERMSRV_SPN_PREFIX) + strlen(settings->ServerHostname);
|
2019-11-06 17:24:51 +03:00
|
|
|
spn = (SEC_CHAR*)malloc(length + 1);
|
2015-02-15 19:10:14 +03:00
|
|
|
|
|
|
|
if (!spn)
|
|
|
|
return -1;
|
|
|
|
|
2018-08-24 11:39:48 +03:00
|
|
|
sprintf_s(spn, length + 1, "%s%s", TERMSRV_SPN_PREFIX, settings->ServerHostname);
|
2012-06-07 03:59:17 +04:00
|
|
|
#ifdef UNICODE
|
2015-02-18 05:01:27 +03:00
|
|
|
nla->ServicePrincipalName = NULL;
|
|
|
|
ConvertToUnicode(CP_UTF8, 0, spn, -1, &nla->ServicePrincipalName, 0);
|
2012-06-07 03:59:17 +04:00
|
|
|
free(spn);
|
|
|
|
#else
|
2015-02-15 19:10:14 +03:00
|
|
|
nla->ServicePrincipalName = spn;
|
2012-06-07 03:59:17 +04:00
|
|
|
#endif
|
2015-06-17 14:23:44 +03:00
|
|
|
nla->table = InitSecurityInterfaceEx(0);
|
2017-05-11 19:51:45 +03:00
|
|
|
#ifdef WITH_GSSAPI /* KERBEROS SSP */
|
|
|
|
nla->status = nla->table->QuerySecurityPackageInfo(KERBEROS_SSP_NAME, &nla->pPackageInfo);
|
|
|
|
|
|
|
|
if (nla->status != SEC_E_OK)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "QuerySecurityPackageInfo status %s [0x%08" PRIX32 "]",
|
2017-05-11 19:51:45 +03:00
|
|
|
GetSecurityStatusString(nla->status), nla->status);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* NTLM SSP */
|
2015-02-15 19:10:14 +03:00
|
|
|
nla->status = nla->table->QuerySecurityPackageInfo(NLA_PKG_NAME, &nla->pPackageInfo);
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
if (nla->status != SEC_E_OK)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "QuerySecurityPackageInfo status %s [0x%08" PRIX32 "]",
|
2017-02-20 20:31:58 +03:00
|
|
|
GetSecurityStatusString(nla->status), nla->status);
|
2015-02-15 19:10:14 +03:00
|
|
|
return -1;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2017-05-11 19:51:45 +03:00
|
|
|
#endif
|
2015-02-15 19:10:14 +03:00
|
|
|
nla->cbMaxToken = nla->pPackageInfo->cbMaxToken;
|
2017-05-11 19:51:45 +03:00
|
|
|
nla->packageName = nla->pPackageInfo->Name;
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_DBG(TAG, "%s %" PRIu32 " : packageName=%ls ; cbMaxToken=%d", __FUNCTION__, __LINE__,
|
2017-05-11 19:51:45 +03:00
|
|
|
nla->packageName, nla->cbMaxToken);
|
2019-11-06 17:24:51 +03:00
|
|
|
nla->status = nla->table->AcquireCredentialsHandle(NULL, NLA_PKG_NAME, SECPKG_CRED_OUTBOUND,
|
|
|
|
NULL, nla->identity, NULL, NULL,
|
|
|
|
&nla->credentials, &nla->expiration);
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
if (nla->status != SEC_E_OK)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "AcquireCredentialsHandle status %s [0x%08" PRIX32 "]",
|
2017-02-20 20:31:58 +03:00
|
|
|
GetSecurityStatusString(nla->status), nla->status);
|
2015-02-15 19:10:14 +03:00
|
|
|
return -1;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
nla->haveContext = FALSE;
|
|
|
|
nla->haveInputBuffer = FALSE;
|
|
|
|
nla->havePubKeyAuth = FALSE;
|
|
|
|
ZeroMemory(&nla->inputBuffer, sizeof(SecBuffer));
|
|
|
|
ZeroMemory(&nla->outputBuffer, sizeof(SecBuffer));
|
|
|
|
ZeroMemory(&nla->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
|
|
|
|
*/
|
2015-02-15 19:10:14 +03:00
|
|
|
nla->fContextReq = ISC_REQ_MUTUAL_AUTH | ISC_REQ_CONFIDENTIALITY | ISC_REQ_USE_SESSION_KEY;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-02-16 00:04:59 +03:00
|
|
|
int nla_client_begin(rdpNla* nla)
|
2015-02-15 19:10:14 +03:00
|
|
|
{
|
2015-02-15 22:54:10 +03:00
|
|
|
if (nla_client_init(nla) < 1)
|
2015-02-15 19:10:14 +03:00
|
|
|
return -1;
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2015-02-16 00:04:59 +03:00
|
|
|
if (nla->state != NLA_STATE_INITIAL)
|
|
|
|
return -1;
|
2015-02-15 19:10:14 +03:00
|
|
|
|
2015-02-16 00:04:59 +03:00
|
|
|
nla->outputBufferDesc.ulVersion = SECBUFFER_VERSION;
|
|
|
|
nla->outputBufferDesc.cBuffers = 1;
|
|
|
|
nla->outputBufferDesc.pBuffers = &nla->outputBuffer;
|
|
|
|
nla->outputBuffer.BufferType = SECBUFFER_TOKEN;
|
|
|
|
nla->outputBuffer.cbBuffer = nla->cbMaxToken;
|
|
|
|
nla->outputBuffer.pvBuffer = malloc(nla->outputBuffer.cbBuffer);
|
2015-02-15 19:10:14 +03:00
|
|
|
|
2015-02-16 00:04:59 +03:00
|
|
|
if (!nla->outputBuffer.pvBuffer)
|
|
|
|
return -1;
|
2015-02-15 19:10:14 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
nla->status = nla->table->InitializeSecurityContext(
|
|
|
|
&nla->credentials, NULL, nla->ServicePrincipalName, nla->fContextReq, 0,
|
|
|
|
SECURITY_NATIVE_DREP, NULL, 0, &nla->context, &nla->outputBufferDesc, &nla->pfContextAttr,
|
|
|
|
&nla->expiration);
|
|
|
|
WLog_VRB(TAG, " InitializeSecurityContext status %s [0x%08" PRIX32 "]",
|
2017-02-20 20:31:58 +03:00
|
|
|
GetSecurityStatusString(nla->status), nla->status);
|
2015-07-07 14:46:05 +03:00
|
|
|
|
2017-05-11 19:51:45 +03:00
|
|
|
/* Handle kerberos context initialization failure.
|
|
|
|
* After kerberos failed initialize NTLM context */
|
|
|
|
if (nla->status == SEC_E_NO_CREDENTIALS)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
nla->status = nla->table->InitializeSecurityContext(
|
|
|
|
&nla->credentials, NULL, nla->ServicePrincipalName, nla->fContextReq, 0,
|
|
|
|
SECURITY_NATIVE_DREP, NULL, 0, &nla->context, &nla->outputBufferDesc,
|
|
|
|
&nla->pfContextAttr, &nla->expiration);
|
|
|
|
WLog_VRB(TAG, " InitializeSecurityContext status %s [0x%08" PRIX32 "]",
|
2017-05-11 19:51:45 +03:00
|
|
|
GetSecurityStatusString(nla->status), nla->status);
|
|
|
|
|
|
|
|
if (nla->status)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
SECURITY_STATUS status =
|
|
|
|
nla->table->QuerySecurityPackageInfo(NTLM_SSP_NAME, &nla->pPackageInfo);
|
2017-05-11 19:51:45 +03:00
|
|
|
|
|
|
|
if (status != SEC_E_OK)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "QuerySecurityPackageInfo status %s [0x%08" PRIX32 "]",
|
2017-05-11 19:51:45 +03:00
|
|
|
GetSecurityStatusString(nla->status), status);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
nla->cbMaxToken = nla->pPackageInfo->cbMaxToken;
|
|
|
|
nla->packageName = nla->pPackageInfo->Name;
|
|
|
|
}
|
|
|
|
}
|
2017-11-14 15:57:48 +03:00
|
|
|
|
2015-02-16 00:04:59 +03:00
|
|
|
if ((nla->status == SEC_I_COMPLETE_AND_CONTINUE) || (nla->status == SEC_I_COMPLETE_NEEDED))
|
|
|
|
{
|
|
|
|
if (nla->table->CompleteAuthToken)
|
2015-07-07 14:46:05 +03:00
|
|
|
{
|
|
|
|
SECURITY_STATUS status;
|
|
|
|
status = nla->table->CompleteAuthToken(&nla->context, &nla->outputBufferDesc);
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-07-07 14:46:05 +03:00
|
|
|
if (status != SEC_E_OK)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_WARN(TAG, "CompleteAuthToken status %s [0x%08" PRIX32 "]",
|
2017-02-20 20:31:58 +03:00
|
|
|
GetSecurityStatusString(status), status);
|
2015-07-09 10:57:45 +03:00
|
|
|
return -1;
|
2015-07-07 14:46:05 +03:00
|
|
|
}
|
|
|
|
}
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2015-02-16 00:04:59 +03:00
|
|
|
if (nla->status == SEC_I_COMPLETE_NEEDED)
|
|
|
|
nla->status = SEC_E_OK;
|
|
|
|
else if (nla->status == SEC_I_COMPLETE_AND_CONTINUE)
|
|
|
|
nla->status = SEC_I_CONTINUE_NEEDED;
|
|
|
|
}
|
2015-02-15 22:54:10 +03:00
|
|
|
|
2015-02-16 00:04:59 +03:00
|
|
|
if (nla->status != SEC_I_CONTINUE_NEEDED)
|
|
|
|
return -1;
|
2015-02-15 22:54:10 +03:00
|
|
|
|
2015-02-16 00:04:59 +03:00
|
|
|
if (nla->outputBuffer.cbBuffer < 1)
|
|
|
|
return -1;
|
2015-02-15 22:54:10 +03:00
|
|
|
|
2015-02-16 00:04:59 +03:00
|
|
|
nla->negoToken.pvBuffer = nla->outputBuffer.pvBuffer;
|
|
|
|
nla->negoToken.cbBuffer = nla->outputBuffer.cbBuffer;
|
|
|
|
WLog_DBG(TAG, "Sending Authentication Token");
|
2019-11-06 17:24:51 +03:00
|
|
|
#if defined(WITH_DEBUG_NLA)
|
2015-02-16 00:04:59 +03:00
|
|
|
winpr_HexDump(TAG, WLOG_DEBUG, nla->negoToken.pvBuffer, nla->negoToken.cbBuffer);
|
2018-11-21 17:36:31 +03:00
|
|
|
#endif
|
2015-02-15 22:54:10 +03:00
|
|
|
|
2015-05-29 14:46:50 +03:00
|
|
|
if (!nla_send(nla))
|
|
|
|
{
|
|
|
|
nla_buffer_free(nla);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-02-16 00:04:59 +03:00
|
|
|
nla_buffer_free(nla);
|
|
|
|
nla->state = NLA_STATE_NEGO_TOKEN;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-07-18 13:54:00 +03:00
|
|
|
static int nla_client_recv(rdpNla* nla)
|
2015-02-16 00:04:59 +03:00
|
|
|
{
|
|
|
|
int status = -1;
|
2015-02-15 22:54:10 +03:00
|
|
|
|
|
|
|
if (nla->state == NLA_STATE_NEGO_TOKEN)
|
|
|
|
{
|
|
|
|
nla->inputBufferDesc.ulVersion = SECBUFFER_VERSION;
|
|
|
|
nla->inputBufferDesc.cBuffers = 1;
|
|
|
|
nla->inputBufferDesc.pBuffers = &nla->inputBuffer;
|
|
|
|
nla->inputBuffer.BufferType = SECBUFFER_TOKEN;
|
|
|
|
nla->inputBuffer.pvBuffer = nla->negoToken.pvBuffer;
|
|
|
|
nla->inputBuffer.cbBuffer = nla->negoToken.cbBuffer;
|
|
|
|
nla->outputBufferDesc.ulVersion = SECBUFFER_VERSION;
|
|
|
|
nla->outputBufferDesc.cBuffers = 1;
|
|
|
|
nla->outputBufferDesc.pBuffers = &nla->outputBuffer;
|
|
|
|
nla->outputBuffer.BufferType = SECBUFFER_TOKEN;
|
|
|
|
nla->outputBuffer.cbBuffer = nla->cbMaxToken;
|
|
|
|
nla->outputBuffer.pvBuffer = malloc(nla->outputBuffer.cbBuffer);
|
|
|
|
|
|
|
|
if (!nla->outputBuffer.pvBuffer)
|
|
|
|
return -1;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
nla->status = nla->table->InitializeSecurityContext(
|
|
|
|
&nla->credentials, &nla->context, nla->ServicePrincipalName, nla->fContextReq, 0,
|
|
|
|
SECURITY_NATIVE_DREP, &nla->inputBufferDesc, 0, &nla->context, &nla->outputBufferDesc,
|
|
|
|
&nla->pfContextAttr, &nla->expiration);
|
|
|
|
WLog_VRB(TAG, "InitializeSecurityContext %s [0x%08" PRIX32 "]",
|
2017-02-20 20:31:58 +03:00
|
|
|
GetSecurityStatusString(nla->status), nla->status);
|
2015-02-15 22:54:10 +03:00
|
|
|
free(nla->inputBuffer.pvBuffer);
|
|
|
|
nla->inputBuffer.pvBuffer = NULL;
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
if ((nla->status == SEC_I_COMPLETE_AND_CONTINUE) || (nla->status == SEC_I_COMPLETE_NEEDED))
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2015-02-15 19:10:14 +03:00
|
|
|
if (nla->table->CompleteAuthToken)
|
2015-07-07 14:46:05 +03:00
|
|
|
{
|
|
|
|
SECURITY_STATUS status;
|
|
|
|
status = nla->table->CompleteAuthToken(&nla->context, &nla->outputBufferDesc);
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-07-07 14:46:05 +03:00
|
|
|
if (status != SEC_E_OK)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_WARN(TAG, "CompleteAuthToken status %s [0x%08" PRIX32 "]",
|
2017-02-20 20:31:58 +03:00
|
|
|
GetSecurityStatusString(status), status);
|
2015-07-09 10:57:45 +03:00
|
|
|
return -1;
|
2015-07-07 14:46:05 +03:00
|
|
|
}
|
|
|
|
}
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
if (nla->status == SEC_I_COMPLETE_NEEDED)
|
|
|
|
nla->status = SEC_E_OK;
|
|
|
|
else if (nla->status == SEC_I_COMPLETE_AND_CONTINUE)
|
|
|
|
nla->status = SEC_I_CONTINUE_NEEDED;
|
2014-06-10 22:38:17 +04:00
|
|
|
}
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
if (nla->status == SEC_E_OK)
|
2014-06-10 22:38:17 +04:00
|
|
|
{
|
2015-02-15 19:10:14 +03:00
|
|
|
nla->havePubKeyAuth = TRUE;
|
2017-02-20 20:31:58 +03:00
|
|
|
nla->status = nla->table->QueryContextAttributes(&nla->context, SECPKG_ATTR_SIZES,
|
2019-11-06 17:24:51 +03:00
|
|
|
&nla->ContextSizes);
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2015-07-07 14:46:05 +03:00
|
|
|
if (nla->status != SEC_E_OK)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG,
|
|
|
|
"QueryContextAttributes SECPKG_ATTR_SIZES failure %s [0x%08" PRIX32 "]",
|
2017-02-20 20:31:58 +03:00
|
|
|
GetSecurityStatusString(nla->status), nla->status);
|
2015-02-15 19:10:14 +03:00
|
|
|
return -1;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2018-03-23 14:12:08 +03:00
|
|
|
if (nla->peerVersion < 5)
|
2018-03-20 11:57:30 +03:00
|
|
|
nla->status = nla_encrypt_public_key_echo(nla);
|
|
|
|
else
|
|
|
|
nla->status = nla_encrypt_public_key_hash(nla);
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-07-08 15:32:01 +03:00
|
|
|
if (nla->status != SEC_E_OK)
|
|
|
|
return -1;
|
2015-07-02 19:50:49 +03:00
|
|
|
}
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2015-02-15 22:54:10 +03:00
|
|
|
nla->negoToken.pvBuffer = nla->outputBuffer.pvBuffer;
|
|
|
|
nla->negoToken.cbBuffer = nla->outputBuffer.cbBuffer;
|
|
|
|
WLog_DBG(TAG, "Sending Authentication Token");
|
2019-11-06 17:24:51 +03:00
|
|
|
#if defined(WITH_DEBUG_NLA)
|
2015-02-15 19:10:14 +03:00
|
|
|
winpr_HexDump(TAG, WLOG_DEBUG, nla->negoToken.pvBuffer, nla->negoToken.cbBuffer);
|
2018-11-21 17:36:31 +03:00
|
|
|
#endif
|
2015-02-15 19:10:14 +03:00
|
|
|
|
2015-05-29 14:46:50 +03:00
|
|
|
if (!nla_send(nla))
|
|
|
|
{
|
|
|
|
nla_buffer_free(nla);
|
|
|
|
return -1;
|
|
|
|
}
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-02-15 22:54:10 +03:00
|
|
|
nla_buffer_free(nla);
|
|
|
|
|
2015-07-02 19:50:49 +03:00
|
|
|
if (nla->status == SEC_E_OK)
|
|
|
|
nla->state = NLA_STATE_PUB_KEY_AUTH;
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-02-16 00:04:59 +03:00
|
|
|
status = 1;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
2015-02-16 00:04:59 +03:00
|
|
|
else if (nla->state == NLA_STATE_PUB_KEY_AUTH)
|
2012-06-16 06:39:12 +04:00
|
|
|
{
|
2015-02-15 22:54:10 +03:00
|
|
|
/* Verify Server Public Key Echo */
|
2018-03-23 14:12:08 +03:00
|
|
|
if (nla->peerVersion < 5)
|
2018-03-20 11:57:30 +03:00
|
|
|
nla->status = nla_decrypt_public_key_echo(nla);
|
|
|
|
else
|
|
|
|
nla->status = nla_decrypt_public_key_hash(nla);
|
|
|
|
|
2015-02-15 22:54:10 +03:00
|
|
|
nla_buffer_free(nla);
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2015-02-15 22:54:10 +03:00
|
|
|
if (nla->status != SEC_E_OK)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "Could not verify public key echo %s [0x%08" PRIX32 "]",
|
2017-02-20 20:31:58 +03:00
|
|
|
GetSecurityStatusString(nla->status), nla->status);
|
2015-02-15 22:54:10 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2015-02-15 22:54:10 +03:00
|
|
|
/* Send encrypted credentials */
|
|
|
|
nla->status = nla_encrypt_ts_credentials(nla);
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2015-02-15 22:54:10 +03:00
|
|
|
if (nla->status != SEC_E_OK)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "nla_encrypt_ts_credentials status %s [0x%08" PRIX32 "]",
|
2017-02-20 20:31:58 +03:00
|
|
|
GetSecurityStatusString(nla->status), nla->status);
|
2015-02-15 22:54:10 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2015-02-15 19:10:14 +03:00
|
|
|
|
2015-05-29 14:46:50 +03:00
|
|
|
if (!nla_send(nla))
|
|
|
|
{
|
|
|
|
nla_buffer_free(nla);
|
|
|
|
return -1;
|
|
|
|
}
|
2015-02-15 22:54:10 +03:00
|
|
|
|
2017-02-20 20:31:58 +03:00
|
|
|
nla_buffer_free(nla);
|
2017-11-14 15:57:48 +03:00
|
|
|
|
2017-09-19 13:33:33 +03:00
|
|
|
if (SecIsValidHandle(&nla->credentials))
|
|
|
|
{
|
|
|
|
nla->table->FreeCredentialsHandle(&nla->credentials);
|
|
|
|
SecInvalidateHandle(&nla->credentials);
|
|
|
|
}
|
2017-11-14 15:57:48 +03:00
|
|
|
|
2015-07-07 14:46:05 +03:00
|
|
|
if (nla->status != SEC_E_OK)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "FreeCredentialsHandle status %s [0x%08" PRIX32 "]",
|
2017-02-20 20:31:58 +03:00
|
|
|
GetSecurityStatusString(nla->status), nla->status);
|
2015-07-07 14:46:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nla->status = nla->table->FreeContextBuffer(nla->pPackageInfo);
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-07-07 14:46:05 +03:00
|
|
|
if (nla->status != SEC_E_OK)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "FreeContextBuffer status %s [0x%08" PRIX32 "]",
|
2017-02-20 20:31:58 +03:00
|
|
|
GetSecurityStatusString(nla->status), nla->status);
|
2015-07-07 14:46:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nla->status != SEC_E_OK)
|
|
|
|
return -1;
|
2015-02-16 00:04:59 +03:00
|
|
|
|
|
|
|
nla->state = NLA_STATE_AUTH_INFO;
|
|
|
|
status = 1;
|
2015-02-15 22:54:10 +03:00
|
|
|
}
|
2015-02-15 19:10:14 +03:00
|
|
|
|
2015-02-16 00:04:59 +03:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2017-07-18 13:54:00 +03:00
|
|
|
static int nla_client_authenticate(rdpNla* nla)
|
2015-02-16 00:04:59 +03:00
|
|
|
{
|
|
|
|
wStream* s;
|
|
|
|
int status;
|
|
|
|
s = Stream_New(NULL, 4096);
|
|
|
|
|
2015-05-29 14:46:50 +03:00
|
|
|
if (!s)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_New failed!");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-02-16 00:04:59 +03:00
|
|
|
if (nla_client_begin(nla) < 1)
|
2016-03-18 15:32:13 +03:00
|
|
|
{
|
|
|
|
Stream_Free(s, TRUE);
|
2015-02-16 00:04:59 +03:00
|
|
|
return -1;
|
2016-03-18 15:32:13 +03:00
|
|
|
}
|
2015-02-16 00:04:59 +03:00
|
|
|
|
|
|
|
while (nla->state < NLA_STATE_AUTH_INFO)
|
|
|
|
{
|
|
|
|
Stream_SetPosition(s, 0);
|
|
|
|
status = transport_read_pdu(nla->transport, s);
|
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "nla_client_authenticate failure");
|
|
|
|
Stream_Free(s, TRUE);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = nla_recv_pdu(nla, s);
|
|
|
|
|
|
|
|
if (status < 0)
|
2016-03-18 15:32:13 +03:00
|
|
|
{
|
|
|
|
Stream_Free(s, TRUE);
|
2015-02-16 00:04:59 +03:00
|
|
|
return -1;
|
2016-03-18 15:32:13 +03:00
|
|
|
}
|
2015-02-16 00:04:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Stream_Free(s, TRUE);
|
2012-05-25 09:50:46 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-15 19:10:14 +03:00
|
|
|
* Initialize NTLMSSP authentication module (server).
|
2012-05-25 09:50:46 +04:00
|
|
|
* @param credssp
|
|
|
|
*/
|
|
|
|
|
2017-07-18 13:54:00 +03:00
|
|
|
static int nla_server_init(rdpNla* nla)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2015-02-15 19:10:14 +03:00
|
|
|
rdpTls* tls = nla->transport->tls;
|
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!sspi_SecBufferAlloc(&nla->PublicKey, tls->PublicKeyLength))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Failed to allocate SecBuffer for public key");
|
|
|
|
return -1;
|
|
|
|
}
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
CopyMemory(nla->PublicKey.pvBuffer, tls->PublicKey, tls->PublicKeyLength);
|
|
|
|
|
|
|
|
if (nla->SspiModule)
|
2012-08-11 02:05:37 +04:00
|
|
|
{
|
|
|
|
HMODULE hSSPI;
|
|
|
|
INIT_SECURITY_INTERFACE pInitSecurityInterface;
|
2015-02-15 19:10:14 +03:00
|
|
|
hSSPI = LoadLibrary(nla->SspiModule);
|
2012-08-11 02:05:37 +04:00
|
|
|
|
|
|
|
if (!hSSPI)
|
|
|
|
{
|
2015-02-15 19:10:14 +03:00
|
|
|
WLog_ERR(TAG, "Failed to load SSPI module: %s", nla->SspiModule);
|
|
|
|
return -1;
|
2012-08-11 02:05:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef UNICODE
|
2019-11-06 17:24:51 +03:00
|
|
|
pInitSecurityInterface =
|
|
|
|
(INIT_SECURITY_INTERFACE)GetProcAddress(hSSPI, "InitSecurityInterfaceW");
|
2012-08-11 02:05:37 +04:00
|
|
|
#else
|
2019-11-06 17:24:51 +03:00
|
|
|
pInitSecurityInterface =
|
|
|
|
(INIT_SECURITY_INTERFACE)GetProcAddress(hSSPI, "InitSecurityInterfaceA");
|
2012-08-11 02:05:37 +04:00
|
|
|
#endif
|
2015-02-15 19:10:14 +03:00
|
|
|
nla->table = pInitSecurityInterface();
|
2012-08-11 02:05:37 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-06-17 14:23:44 +03:00
|
|
|
nla->table = InitSecurityInterfaceEx(0);
|
2012-08-11 02:05:37 +04:00
|
|
|
}
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
nla->status = nla->table->QuerySecurityPackageInfo(NLA_PKG_NAME, &nla->pPackageInfo);
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
if (nla->status != SEC_E_OK)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "QuerySecurityPackageInfo status %s [0x%08" PRIX32 "]",
|
2017-02-20 20:31:58 +03:00
|
|
|
GetSecurityStatusString(nla->status), nla->status);
|
2015-02-15 19:10:14 +03:00
|
|
|
return -1;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
nla->cbMaxToken = nla->pPackageInfo->cbMaxToken;
|
2018-01-17 11:09:58 +03:00
|
|
|
nla->packageName = nla->pPackageInfo->Name;
|
2019-11-06 17:24:51 +03:00
|
|
|
nla->status =
|
|
|
|
nla->table->AcquireCredentialsHandle(NULL, NLA_PKG_NAME, SECPKG_CRED_INBOUND, NULL, NULL,
|
|
|
|
NULL, NULL, &nla->credentials, &nla->expiration);
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
if (nla->status != SEC_E_OK)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "AcquireCredentialsHandle status %s [0x%08" PRIX32 "]",
|
2017-02-20 20:31:58 +03:00
|
|
|
GetSecurityStatusString(nla->status), nla->status);
|
2015-02-15 19:10:14 +03:00
|
|
|
return -1;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
nla->haveContext = FALSE;
|
|
|
|
nla->haveInputBuffer = FALSE;
|
|
|
|
nla->havePubKeyAuth = FALSE;
|
|
|
|
ZeroMemory(&nla->inputBuffer, sizeof(SecBuffer));
|
|
|
|
ZeroMemory(&nla->outputBuffer, sizeof(SecBuffer));
|
|
|
|
ZeroMemory(&nla->inputBufferDesc, sizeof(SecBufferDesc));
|
|
|
|
ZeroMemory(&nla->outputBufferDesc, sizeof(SecBufferDesc));
|
|
|
|
ZeroMemory(&nla->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
|
|
|
|
*/
|
2015-02-15 19:10:14 +03:00
|
|
|
nla->fContextReq = 0;
|
|
|
|
nla->fContextReq |= ASC_REQ_MUTUAL_AUTH;
|
|
|
|
nla->fContextReq |= ASC_REQ_CONFIDENTIALITY;
|
|
|
|
nla->fContextReq |= ASC_REQ_CONNECTION;
|
|
|
|
nla->fContextReq |= ASC_REQ_USE_SESSION_KEY;
|
|
|
|
nla->fContextReq |= ASC_REQ_REPLAY_DETECT;
|
|
|
|
nla->fContextReq |= ASC_REQ_SEQUENCE_DETECT;
|
|
|
|
nla->fContextReq |= ASC_REQ_EXTENDED_ERROR;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Authenticate with client using CredSSP (server).
|
|
|
|
* @param credssp
|
|
|
|
* @return 1 if authentication is successful
|
|
|
|
*/
|
|
|
|
|
2017-07-18 13:54:00 +03:00
|
|
|
static int nla_server_authenticate(rdpNla* nla)
|
2015-02-15 19:10:14 +03:00
|
|
|
{
|
2015-02-15 22:54:10 +03:00
|
|
|
if (nla_server_init(nla) < 1)
|
2015-02-15 19:10:14 +03:00
|
|
|
return -1;
|
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
|
|
|
{
|
|
|
|
/* receive authentication token */
|
2015-02-15 19:10:14 +03:00
|
|
|
nla->inputBufferDesc.ulVersion = SECBUFFER_VERSION;
|
|
|
|
nla->inputBufferDesc.cBuffers = 1;
|
|
|
|
nla->inputBufferDesc.pBuffers = &nla->inputBuffer;
|
|
|
|
nla->inputBuffer.BufferType = SECBUFFER_TOKEN;
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
if (nla_recv(nla) < 0)
|
2012-05-25 09:50:46 +04:00
|
|
|
return -1;
|
|
|
|
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_DBG(TAG, "Receiving Authentication Token");
|
2015-02-15 19:10:14 +03:00
|
|
|
nla_buffer_print(nla);
|
|
|
|
nla->inputBuffer.pvBuffer = nla->negoToken.pvBuffer;
|
|
|
|
nla->inputBuffer.cbBuffer = nla->negoToken.cbBuffer;
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
if (nla->negoToken.cbBuffer < 1)
|
2012-06-13 07:09:30 +04:00
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "CredSSP: invalid negoToken!");
|
2012-06-13 07:09:30 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
nla->outputBufferDesc.ulVersion = SECBUFFER_VERSION;
|
|
|
|
nla->outputBufferDesc.cBuffers = 1;
|
|
|
|
nla->outputBufferDesc.pBuffers = &nla->outputBuffer;
|
|
|
|
nla->outputBuffer.BufferType = SECBUFFER_TOKEN;
|
|
|
|
nla->outputBuffer.cbBuffer = nla->cbMaxToken;
|
|
|
|
nla->outputBuffer.pvBuffer = malloc(nla->outputBuffer.cbBuffer);
|
|
|
|
|
|
|
|
if (!nla->outputBuffer.pvBuffer)
|
|
|
|
return -1;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
nla->status = nla->table->AcceptSecurityContext(
|
|
|
|
&nla->credentials, nla->haveContext ? &nla->context : NULL, &nla->inputBufferDesc,
|
|
|
|
nla->fContextReq, SECURITY_NATIVE_DREP, &nla->context, &nla->outputBufferDesc,
|
|
|
|
&nla->pfContextAttr, &nla->expiration);
|
|
|
|
WLog_VRB(TAG, "AcceptSecurityContext status %s [0x%08" PRIX32 "]",
|
2017-02-20 20:31:58 +03:00
|
|
|
GetSecurityStatusString(nla->status), nla->status);
|
2015-02-15 19:10:14 +03:00
|
|
|
nla->negoToken.pvBuffer = nla->outputBuffer.pvBuffer;
|
|
|
|
nla->negoToken.cbBuffer = nla->outputBuffer.cbBuffer;
|
|
|
|
|
|
|
|
if ((nla->status == SEC_I_COMPLETE_AND_CONTINUE) || (nla->status == SEC_I_COMPLETE_NEEDED))
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2017-05-11 19:51:45 +03:00
|
|
|
freerdp_peer* peer = nla->instance->context->peer;
|
2017-05-15 19:52:39 +03:00
|
|
|
|
|
|
|
if (peer->ComputeNtlmHash)
|
|
|
|
{
|
|
|
|
SECURITY_STATUS status;
|
2019-11-06 17:24:51 +03:00
|
|
|
status = nla->table->SetContextAttributes(
|
|
|
|
&nla->context, SECPKG_ATTR_AUTH_NTLM_HASH_CB, peer->ComputeNtlmHash, 0);
|
2017-05-15 19:52:39 +03:00
|
|
|
|
|
|
|
if (status != SEC_E_OK)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "SetContextAttributesA(hash cb) status %s [0x%08" PRIX32 "]",
|
2017-05-11 19:51:45 +03:00
|
|
|
GetSecurityStatusString(status), status);
|
2017-05-15 19:52:39 +03:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
status = nla->table->SetContextAttributes(
|
|
|
|
&nla->context, SECPKG_ATTR_AUTH_NTLM_HASH_CB_DATA, peer, 0);
|
2017-11-14 15:57:48 +03:00
|
|
|
|
2017-05-15 19:52:39 +03:00
|
|
|
if (status != SEC_E_OK)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "SetContextAttributesA(hash cb data) status %s [0x%08" PRIX32 "]",
|
2017-05-11 19:51:45 +03:00
|
|
|
GetSecurityStatusString(status), status);
|
2017-05-15 19:52:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (nla->SamFile)
|
2016-07-22 01:58:24 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
nla->table->SetContextAttributes(&nla->context, SECPKG_ATTR_AUTH_NTLM_SAM_FILE,
|
|
|
|
nla->SamFile, strlen(nla->SamFile) + 1);
|
2016-07-22 01:58:24 +03:00
|
|
|
}
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
if (nla->table->CompleteAuthToken)
|
2015-07-07 14:46:05 +03:00
|
|
|
{
|
|
|
|
SECURITY_STATUS status;
|
|
|
|
status = nla->table->CompleteAuthToken(&nla->context, &nla->outputBufferDesc);
|
2016-07-22 01:58:24 +03:00
|
|
|
|
2015-07-07 14:46:05 +03:00
|
|
|
if (status != SEC_E_OK)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_WARN(TAG, "CompleteAuthToken status %s [0x%08" PRIX32 "]",
|
2017-02-20 20:31:58 +03:00
|
|
|
GetSecurityStatusString(status), status);
|
2015-07-09 10:57:45 +03:00
|
|
|
return -1;
|
2015-07-07 14:46:05 +03:00
|
|
|
}
|
|
|
|
}
|
2016-07-22 01:58:24 +03:00
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
if (nla->status == SEC_I_COMPLETE_NEEDED)
|
|
|
|
nla->status = SEC_E_OK;
|
|
|
|
else if (nla->status == SEC_I_COMPLETE_AND_CONTINUE)
|
|
|
|
nla->status = SEC_I_CONTINUE_NEEDED;
|
2012-08-01 21:13:17 +04:00
|
|
|
}
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
if (nla->status == SEC_E_OK)
|
2012-08-01 21:13:17 +04:00
|
|
|
{
|
2015-07-02 19:50:49 +03:00
|
|
|
if (nla->outputBuffer.cbBuffer != 0)
|
|
|
|
{
|
|
|
|
if (!nla_send(nla))
|
|
|
|
{
|
|
|
|
nla_buffer_free(nla);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nla_recv(nla) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
WLog_DBG(TAG, "Receiving pubkey Token");
|
|
|
|
nla_buffer_print(nla);
|
|
|
|
}
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
nla->havePubKeyAuth = TRUE;
|
2017-05-15 19:52:39 +03:00
|
|
|
nla->status = nla->table->QueryContextAttributes(&nla->context, SECPKG_ATTR_SIZES,
|
2019-11-06 17:24:51 +03:00
|
|
|
&nla->ContextSizes);
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2015-07-07 14:46:05 +03:00
|
|
|
if (nla->status != SEC_E_OK)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG,
|
|
|
|
"QueryContextAttributes SECPKG_ATTR_SIZES failure %s [0x%08" PRIX32 "]",
|
2017-02-20 20:31:58 +03:00
|
|
|
GetSecurityStatusString(nla->status), nla->status);
|
2015-02-15 19:10:14 +03:00
|
|
|
return -1;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2018-03-23 14:12:08 +03:00
|
|
|
if (nla->peerVersion < 5)
|
2018-03-20 11:57:30 +03:00
|
|
|
nla->status = nla_decrypt_public_key_echo(nla);
|
|
|
|
else
|
|
|
|
nla->status = nla_decrypt_public_key_hash(nla);
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-07-07 14:46:05 +03:00
|
|
|
if (nla->status != SEC_E_OK)
|
2012-06-27 13:10:17 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG,
|
|
|
|
"Error: could not verify client's public key echo %s [0x%08" PRIX32 "]",
|
2017-02-20 20:31:58 +03:00
|
|
|
GetSecurityStatusString(nla->status), nla->status);
|
2012-06-27 13:10:17 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
sspi_SecBufferFree(&nla->negoToken);
|
|
|
|
nla->negoToken.pvBuffer = NULL;
|
|
|
|
nla->negoToken.cbBuffer = 0;
|
2018-03-20 11:57:30 +03:00
|
|
|
|
2018-03-23 14:12:08 +03:00
|
|
|
if (nla->peerVersion < 5)
|
2018-03-20 11:57:30 +03:00
|
|
|
nla->status = nla_encrypt_public_key_echo(nla);
|
|
|
|
else
|
|
|
|
nla->status = nla_encrypt_public_key_hash(nla);
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-07-08 15:32:01 +03:00
|
|
|
if (nla->status != SEC_E_OK)
|
|
|
|
return -1;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
if ((nla->status != SEC_E_OK) && (nla->status != SEC_I_CONTINUE_NEEDED))
|
2012-07-02 03:43:13 +04:00
|
|
|
{
|
2016-03-18 15:32:13 +03:00
|
|
|
/* Special handling of these specific error codes as NTSTATUS_FROM_WIN32
|
|
|
|
unfortunately does not map directly to the corresponding NTSTATUS values
|
|
|
|
*/
|
|
|
|
switch (GetLastError())
|
|
|
|
{
|
2017-11-14 15:57:48 +03:00
|
|
|
case ERROR_PASSWORD_MUST_CHANGE:
|
|
|
|
nla->errorCode = STATUS_PASSWORD_MUST_CHANGE;
|
|
|
|
break;
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2017-11-14 15:57:48 +03:00
|
|
|
case ERROR_PASSWORD_EXPIRED:
|
|
|
|
nla->errorCode = STATUS_PASSWORD_EXPIRED;
|
|
|
|
break;
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2017-11-14 15:57:48 +03:00
|
|
|
case ERROR_ACCOUNT_DISABLED:
|
|
|
|
nla->errorCode = STATUS_ACCOUNT_DISABLED;
|
|
|
|
break;
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2017-11-14 15:57:48 +03:00
|
|
|
default:
|
|
|
|
nla->errorCode = NTSTATUS_FROM_WIN32(GetLastError());
|
|
|
|
break;
|
2016-03-18 15:32:13 +03:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "AcceptSecurityContext status %s [0x%08" PRIX32 "]",
|
2017-02-20 20:31:58 +03:00
|
|
|
GetSecurityStatusString(nla->status), nla->status);
|
2016-03-18 15:32:13 +03:00
|
|
|
nla_send(nla);
|
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 */
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_DBG(TAG, "Sending Authentication Token");
|
2015-02-15 19:10:14 +03:00
|
|
|
nla_buffer_print(nla);
|
|
|
|
|
2015-05-29 14:46:50 +03:00
|
|
|
if (!nla_send(nla))
|
|
|
|
{
|
|
|
|
nla_buffer_free(nla);
|
|
|
|
return -1;
|
|
|
|
}
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
nla_buffer_free(nla);
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
if (nla->status != SEC_I_CONTINUE_NEEDED)
|
2012-05-25 09:50:46 +04:00
|
|
|
break;
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
nla->haveContext = TRUE;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Receive encrypted credentials */
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
if (nla_recv(nla) < 0)
|
2012-05-25 09:50:46 +04:00
|
|
|
return -1;
|
|
|
|
|
2015-07-08 18:40:17 +03:00
|
|
|
nla->status = nla_decrypt_ts_credentials(nla);
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
if (nla->status != SEC_E_OK)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "Could not decrypt TSCredentials status %s [0x%08" PRIX32 "]",
|
2017-02-20 20:31:58 +03:00
|
|
|
GetSecurityStatusString(nla->status), nla->status);
|
2015-02-15 19:10:14 +03:00
|
|
|
return -1;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
nla->status = nla->table->ImpersonateSecurityContext(&nla->context);
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
if (nla->status != SEC_E_OK)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "ImpersonateSecurityContext status %s [0x%08" PRIX32 "]",
|
2017-02-20 20:31:58 +03:00
|
|
|
GetSecurityStatusString(nla->status), nla->status);
|
2015-02-15 19:10:14 +03:00
|
|
|
return -1;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-15 19:10:14 +03:00
|
|
|
nla->status = nla->table->RevertSecurityContext(&nla->context);
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
if (nla->status != SEC_E_OK)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "RevertSecurityContext status %s [0x%08" PRIX32 "]",
|
2017-02-20 20:31:58 +03:00
|
|
|
GetSecurityStatusString(nla->status), nla->status);
|
2015-02-15 19:10:14 +03:00
|
|
|
return -1;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-07 14:46:05 +03:00
|
|
|
nla->status = nla->table->FreeContextBuffer(nla->pPackageInfo);
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-07-07 14:46:05 +03:00
|
|
|
if (nla->status != SEC_E_OK)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "DeleteSecurityContext status %s [0x%08" PRIX32 "]",
|
2017-02-20 20:31:58 +03:00
|
|
|
GetSecurityStatusString(nla->status), nla->status);
|
2015-07-07 14:46:05 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2015-02-15 19:10:14 +03:00
|
|
|
|
2012-05-25 09:50:46 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Authenticate using CredSSP.
|
|
|
|
* @param credssp
|
|
|
|
* @return 1 if authentication is successful
|
|
|
|
*/
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
int nla_authenticate(rdpNla* nla)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2015-02-15 19:10:14 +03:00
|
|
|
if (nla->server)
|
|
|
|
return nla_server_authenticate(nla);
|
2012-05-25 09:50:46 +04:00
|
|
|
else
|
2015-02-15 19:10:14 +03:00
|
|
|
return nla_client_authenticate(nla);
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2017-07-18 13:54:00 +03:00
|
|
|
static 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-18 13:54:00 +03:00
|
|
|
static 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
SECURITY_STATUS nla_encrypt_public_key_echo(rdpNla* nla)
|
2012-06-16 06:39:12 +04:00
|
|
|
{
|
2017-12-21 13:04:32 +03:00
|
|
|
SecBuffer Buffers[2] = { { 0 } };
|
2012-06-16 06:39:12 +04:00
|
|
|
SecBufferDesc Message;
|
|
|
|
SECURITY_STATUS status;
|
2018-03-21 13:30:02 +03:00
|
|
|
ULONG public_key_length;
|
2019-11-06 17:24:51 +03:00
|
|
|
const BOOL krb =
|
|
|
|
(_tcsncmp(nla->packageName, KERBEROS_SSP_NAME, ARRAYSIZE(KERBEROS_SSP_NAME)) == 0);
|
2018-02-13 13:00:56 +03:00
|
|
|
const BOOL nego = (_tcsncmp(nla->packageName, NEGO_SSP_NAME, ARRAYSIZE(NEGO_SSP_NAME)) == 0);
|
2019-11-06 17:24:51 +03:00
|
|
|
const BOOL ntlm = (_tcsncmp(nla->packageName, NTLM_SSP_NAME, ARRAYSIZE(NTLM_SSP_NAME)) == 0);
|
2015-02-15 19:10:14 +03:00
|
|
|
public_key_length = nla->PublicKey.cbBuffer;
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!sspi_SecBufferAlloc(&nla->pubKeyAuth,
|
|
|
|
public_key_length + nla->ContextSizes.cbSecurityTrailer))
|
2015-04-03 17:21:01 +03:00
|
|
|
return SEC_E_INSUFFICIENT_MEMORY;
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2018-01-17 10:17:33 +03:00
|
|
|
if (krb)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
2018-10-05 19:43:00 +03:00
|
|
|
Message.cBuffers = 1;
|
2017-05-11 19:51:45 +03:00
|
|
|
Buffers[0].BufferType = SECBUFFER_DATA; /* TLS Public Key */
|
|
|
|
Buffers[0].cbBuffer = public_key_length;
|
|
|
|
Buffers[0].pvBuffer = nla->pubKeyAuth.pvBuffer;
|
|
|
|
CopyMemory(Buffers[0].pvBuffer, nla->PublicKey.pvBuffer, Buffers[0].cbBuffer);
|
|
|
|
}
|
2018-01-17 10:17:33 +03:00
|
|
|
else if (ntlm || nego)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
2018-10-05 19:43:00 +03:00
|
|
|
Message.cBuffers = 2;
|
2017-05-11 19:51:45 +03:00
|
|
|
Buffers[0].BufferType = SECBUFFER_TOKEN; /* Signature */
|
|
|
|
Buffers[0].cbBuffer = nla->ContextSizes.cbSecurityTrailer;
|
|
|
|
Buffers[0].pvBuffer = nla->pubKeyAuth.pvBuffer;
|
|
|
|
Buffers[1].BufferType = SECBUFFER_DATA; /* TLS Public Key */
|
|
|
|
Buffers[1].cbBuffer = public_key_length;
|
2019-11-06 17:24:51 +03:00
|
|
|
Buffers[1].pvBuffer =
|
|
|
|
((BYTE*)nla->pubKeyAuth.pvBuffer) + nla->ContextSizes.cbSecurityTrailer;
|
2017-05-11 19:51:45 +03:00
|
|
|
CopyMemory(Buffers[1].pvBuffer, nla->PublicKey.pvBuffer, Buffers[1].cbBuffer);
|
|
|
|
}
|
2012-06-16 06:39:12 +04:00
|
|
|
|
2018-04-09 16:04:52 +03:00
|
|
|
if (!krb && nla->server)
|
2012-06-16 06:39:12 +04:00
|
|
|
{
|
2012-06-18 09:01:08 +04:00
|
|
|
/* server echos the public key +1 */
|
2019-11-06 17:24:51 +03:00
|
|
|
ap_integer_increment_le((BYTE*)Buffers[1].pvBuffer, Buffers[1].cbBuffer);
|
2012-06-16 06:39:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Message.ulVersion = SECBUFFER_VERSION;
|
2019-11-06 17:24:51 +03:00
|
|
|
Message.pBuffers = (PSecBuffer)&Buffers;
|
2015-02-15 19:10:14 +03:00
|
|
|
status = nla->table->EncryptMessage(&nla->context, 0, &Message, nla->sendSeqNum++);
|
2012-06-16 06:39:12 +04:00
|
|
|
|
|
|
|
if (status != SEC_E_OK)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "EncryptMessage status %s [0x%08" PRIX32 "]", GetSecurityStatusString(status),
|
|
|
|
status);
|
2012-06-16 06:39:12 +04:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2018-10-05 19:43:00 +03:00
|
|
|
if (Message.cBuffers == 2 && Buffers[0].cbBuffer < nla->ContextSizes.cbSecurityTrailer)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
/* IMPORTANT: EncryptMessage may not use all the signature space, so we need to shrink the
|
|
|
|
* excess between the buffers */
|
2018-11-21 17:36:31 +03:00
|
|
|
MoveMemory(((BYTE*)Buffers[0].pvBuffer) + Buffers[0].cbBuffer, Buffers[1].pvBuffer,
|
|
|
|
Buffers[1].cbBuffer);
|
2018-10-05 19:43:00 +03:00
|
|
|
nla->pubKeyAuth.cbBuffer = Buffers[0].cbBuffer + Buffers[1].cbBuffer;
|
|
|
|
}
|
|
|
|
|
2012-06-16 06:39:12 +04:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2018-03-20 11:57:30 +03:00
|
|
|
SECURITY_STATUS nla_encrypt_public_key_hash(rdpNla* nla)
|
|
|
|
{
|
|
|
|
SecBuffer Buffers[2] = { { 0 } };
|
|
|
|
SecBufferDesc Message;
|
|
|
|
SECURITY_STATUS status = SEC_E_INTERNAL_ERROR;
|
|
|
|
WINPR_DIGEST_CTX* sha256 = NULL;
|
2019-11-06 17:24:51 +03:00
|
|
|
const BOOL krb =
|
|
|
|
(_tcsncmp(nla->packageName, KERBEROS_SSP_NAME, ARRAYSIZE(KERBEROS_SSP_NAME)) == 0);
|
|
|
|
const ULONG auth_data_length =
|
|
|
|
(nla->ContextSizes.cbSecurityTrailer + WINPR_SHA256_DIGEST_LENGTH);
|
2018-03-21 13:30:02 +03:00
|
|
|
const BYTE* hashMagic = nla->server ? ServerClientHashMagic : ClientServerHashMagic;
|
2019-11-06 17:24:51 +03:00
|
|
|
const size_t hashSize =
|
|
|
|
nla->server ? sizeof(ServerClientHashMagic) : sizeof(ClientServerHashMagic);
|
2018-03-20 11:57:30 +03:00
|
|
|
|
|
|
|
if (!sspi_SecBufferAlloc(&nla->pubKeyAuth, auth_data_length))
|
|
|
|
{
|
|
|
|
status = SEC_E_INSUFFICIENT_MEMORY;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generate SHA256 of following data: ClientServerHashMagic, Nonce, SubjectPublicKey */
|
|
|
|
if (!(sha256 = winpr_Digest_New()))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (!winpr_Digest_Init(sha256, WINPR_MD_SHA256))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* include trailing \0 from hashMagic */
|
2018-03-21 13:30:02 +03:00
|
|
|
if (!winpr_Digest_Update(sha256, hashMagic, hashSize))
|
2018-03-20 11:57:30 +03:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (!winpr_Digest_Update(sha256, nla->ClientNonce.pvBuffer, nla->ClientNonce.cbBuffer))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* SubjectPublicKey */
|
|
|
|
if (!winpr_Digest_Update(sha256, nla->PublicKey.pvBuffer, nla->PublicKey.cbBuffer))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (krb)
|
|
|
|
{
|
|
|
|
Message.cBuffers = 1;
|
|
|
|
Buffers[0].BufferType = SECBUFFER_DATA; /* SHA256 hash */
|
|
|
|
Buffers[0].cbBuffer = WINPR_SHA256_DIGEST_LENGTH;
|
|
|
|
Buffers[0].pvBuffer = nla->pubKeyAuth.pvBuffer;
|
|
|
|
|
|
|
|
if (!winpr_Digest_Final(sha256, Buffers[0].pvBuffer, Buffers[0].cbBuffer))
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Message.cBuffers = 2;
|
|
|
|
Buffers[0].BufferType = SECBUFFER_TOKEN; /* Signature */
|
|
|
|
Buffers[0].cbBuffer = nla->ContextSizes.cbSecurityTrailer;
|
|
|
|
Buffers[0].pvBuffer = nla->pubKeyAuth.pvBuffer;
|
|
|
|
Buffers[1].BufferType = SECBUFFER_DATA; /* SHA256 hash */
|
|
|
|
Buffers[1].cbBuffer = WINPR_SHA256_DIGEST_LENGTH;
|
2019-11-06 17:24:51 +03:00
|
|
|
Buffers[1].pvBuffer =
|
|
|
|
((BYTE*)nla->pubKeyAuth.pvBuffer) + nla->ContextSizes.cbSecurityTrailer;
|
2018-03-20 11:57:30 +03:00
|
|
|
|
|
|
|
if (!winpr_Digest_Final(sha256, Buffers[1].pvBuffer, Buffers[1].cbBuffer))
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
Message.pBuffers = (PSecBuffer)&Buffers;
|
2018-10-05 19:43:00 +03:00
|
|
|
Message.ulVersion = SECBUFFER_VERSION;
|
2018-03-20 11:57:30 +03:00
|
|
|
status = nla->table->EncryptMessage(&nla->context, 0, &Message, nla->sendSeqNum++);
|
|
|
|
|
|
|
|
if (status != SEC_E_OK)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "EncryptMessage status %s [0x%08" PRIX32 "]", GetSecurityStatusString(status),
|
|
|
|
status);
|
2018-10-05 19:43:00 +03:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Message.cBuffers == 2 && Buffers[0].cbBuffer < nla->ContextSizes.cbSecurityTrailer)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
/* IMPORTANT: EncryptMessage may not use all the signature space, so we need to shrink the
|
|
|
|
* excess between the buffers */
|
2018-11-21 17:36:31 +03:00
|
|
|
MoveMemory(((BYTE*)Buffers[0].pvBuffer) + Buffers[0].cbBuffer, Buffers[1].pvBuffer,
|
|
|
|
Buffers[1].cbBuffer);
|
2018-10-05 19:43:00 +03:00
|
|
|
nla->pubKeyAuth.cbBuffer = Buffers[0].cbBuffer + Buffers[1].cbBuffer;
|
2018-03-20 11:57:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
winpr_Digest_Free(sha256);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
SECURITY_STATUS nla_decrypt_public_key_echo(rdpNla* nla)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2018-03-21 13:30:02 +03:00
|
|
|
ULONG length;
|
2018-01-17 10:12:54 +03:00
|
|
|
BYTE* buffer = NULL;
|
2013-08-28 18:44:54 +04:00
|
|
|
ULONG pfQOP = 0;
|
2017-11-14 15:57:48 +03:00
|
|
|
BYTE* public_key1 = NULL;
|
|
|
|
BYTE* public_key2 = NULL;
|
2018-03-21 13:30:02 +03:00
|
|
|
ULONG public_key_length = 0;
|
2016-05-16 19:53:38 +03:00
|
|
|
int signature_length;
|
2017-12-21 13:04:32 +03:00
|
|
|
SecBuffer Buffers[2] = { { 0 } };
|
2012-05-25 09:50:46 +04:00
|
|
|
SecBufferDesc Message;
|
2018-01-17 10:12:54 +03:00
|
|
|
BOOL krb, ntlm, nego;
|
|
|
|
SECURITY_STATUS status = SEC_E_INVALID_TOKEN;
|
|
|
|
|
|
|
|
if (!nla)
|
|
|
|
goto fail;
|
|
|
|
|
2018-02-13 13:00:56 +03:00
|
|
|
krb = (_tcsncmp(nla->packageName, KERBEROS_SSP_NAME, ARRAYSIZE(KERBEROS_SSP_NAME)) == 0);
|
|
|
|
nego = (_tcsncmp(nla->packageName, NEGO_SSP_NAME, ARRAYSIZE(NEGO_SSP_NAME)) == 0);
|
2019-11-06 17:24:51 +03:00
|
|
|
ntlm = (_tcsncmp(nla->packageName, NTLM_SSP_NAME, ARRAYSIZE(NTLM_SSP_NAME)) == 0);
|
2016-05-16 19:53:38 +03:00
|
|
|
signature_length = nla->pubKeyAuth.cbBuffer - nla->PublicKey.cbBuffer;
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2019-02-07 16:22:28 +03:00
|
|
|
if ((signature_length < 0) || ((UINT32)signature_length > nla->ContextSizes.cbSecurityTrailer))
|
2012-06-18 23:56:40 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "unexpected pubKeyAuth buffer size: %" PRIu32 "", nla->pubKeyAuth.cbBuffer);
|
2018-01-17 10:12:54 +03:00
|
|
|
goto fail;
|
2012-06-18 23:56:40 +04:00
|
|
|
}
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
length = nla->pubKeyAuth.cbBuffer;
|
2019-11-06 17:24:51 +03:00
|
|
|
buffer = (BYTE*)malloc(length);
|
2015-02-15 19:10:14 +03:00
|
|
|
|
|
|
|
if (!buffer)
|
2018-01-17 10:12:54 +03:00
|
|
|
{
|
|
|
|
status = SEC_E_INSUFFICIENT_MEMORY;
|
|
|
|
goto fail;
|
|
|
|
}
|
2015-02-15 19:10:14 +03:00
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
if (krb)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
|
|
|
CopyMemory(buffer, nla->pubKeyAuth.pvBuffer, length);
|
|
|
|
Buffers[0].BufferType = SECBUFFER_DATA; /* Wrapped and encrypted TLS Public Key */
|
|
|
|
Buffers[0].cbBuffer = length;
|
|
|
|
Buffers[0].pvBuffer = buffer;
|
|
|
|
Message.cBuffers = 1;
|
|
|
|
}
|
2018-01-17 10:12:54 +03:00
|
|
|
else if (ntlm || nego)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
|
|
|
CopyMemory(buffer, nla->pubKeyAuth.pvBuffer, length);
|
|
|
|
public_key_length = nla->PublicKey.cbBuffer;
|
|
|
|
Buffers[0].BufferType = SECBUFFER_TOKEN; /* Signature */
|
|
|
|
Buffers[0].cbBuffer = signature_length;
|
|
|
|
Buffers[0].pvBuffer = buffer;
|
|
|
|
Buffers[1].BufferType = SECBUFFER_DATA; /* Encrypted TLS Public Key */
|
|
|
|
Buffers[1].cbBuffer = length - signature_length;
|
|
|
|
Buffers[1].pvBuffer = buffer + signature_length;
|
|
|
|
Message.cBuffers = 2;
|
|
|
|
}
|
2017-11-14 15:57:48 +03:00
|
|
|
|
2018-10-05 19:43:00 +03:00
|
|
|
Message.ulVersion = SECBUFFER_VERSION;
|
|
|
|
Message.pBuffers = (PSecBuffer)&Buffers;
|
2015-02-15 19:10:14 +03:00
|
|
|
status = nla->table->DecryptMessage(&nla->context, &Message, nla->recvSeqNum++, &pfQOP);
|
2012-05-25 09:50:46 +04:00
|
|
|
|
|
|
|
if (status != SEC_E_OK)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "DecryptMessage failure %s [%08" PRIX32 "]", GetSecurityStatusString(status),
|
|
|
|
status);
|
2018-01-17 10:12:54 +03:00
|
|
|
goto fail;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
if (krb)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
public_key1 = public_key2 = (BYTE*)nla->pubKeyAuth.pvBuffer;
|
2017-05-11 19:51:45 +03:00
|
|
|
public_key_length = length;
|
|
|
|
}
|
2018-01-17 10:12:54 +03:00
|
|
|
else if (ntlm || nego)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
public_key1 = (BYTE*)nla->PublicKey.pvBuffer;
|
|
|
|
public_key2 = (BYTE*)Buffers[1].pvBuffer;
|
2017-05-11 19:51:45 +03:00
|
|
|
}
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
if (!nla->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
|
|
|
|
2017-11-14 15:57:48 +03:00
|
|
|
if (!public_key1 || !public_key2 || memcmp(public_key1, public_key2, public_key_length) != 0)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "Could not verify server's public key echo");
|
2019-11-06 17:24:51 +03:00
|
|
|
#if defined(WITH_DEBUG_NLA)
|
2014-09-12 16:36:29 +04:00
|
|
|
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);
|
2018-11-21 17:36:31 +03:00
|
|
|
#endif
|
2018-01-17 10:12:54 +03:00
|
|
|
status = SEC_E_MESSAGE_ALTERED; /* DO NOT SEND CREDENTIALS! */
|
|
|
|
goto fail;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
status = SEC_E_OK;
|
|
|
|
fail:
|
2012-06-18 23:56:40 +04:00
|
|
|
free(buffer);
|
2018-01-17 10:12:54 +03:00
|
|
|
return status;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2018-03-20 11:57:30 +03:00
|
|
|
SECURITY_STATUS nla_decrypt_public_key_hash(rdpNla* nla)
|
|
|
|
{
|
|
|
|
unsigned long length;
|
|
|
|
BYTE* buffer = NULL;
|
|
|
|
ULONG pfQOP = 0;
|
|
|
|
int signature_length;
|
|
|
|
SecBuffer Buffers[2] = { { 0 } };
|
|
|
|
SecBufferDesc Message;
|
|
|
|
WINPR_DIGEST_CTX* sha256 = NULL;
|
|
|
|
BYTE serverClientHash[WINPR_SHA256_DIGEST_LENGTH];
|
|
|
|
SECURITY_STATUS status = SEC_E_INVALID_TOKEN;
|
2019-11-06 17:24:51 +03:00
|
|
|
const BOOL krb =
|
|
|
|
(_tcsncmp(nla->packageName, KERBEROS_SSP_NAME, ARRAYSIZE(KERBEROS_SSP_NAME)) == 0);
|
2018-03-21 13:30:02 +03:00
|
|
|
const BYTE* hashMagic = nla->server ? ClientServerHashMagic : ServerClientHashMagic;
|
2019-11-06 17:24:51 +03:00
|
|
|
const size_t hashSize =
|
|
|
|
nla->server ? sizeof(ClientServerHashMagic) : sizeof(ServerClientHashMagic);
|
2018-03-20 11:57:30 +03:00
|
|
|
signature_length = nla->pubKeyAuth.cbBuffer - WINPR_SHA256_DIGEST_LENGTH;
|
|
|
|
|
|
|
|
if ((signature_length < 0) || (signature_length > (int)nla->ContextSizes.cbSecurityTrailer))
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "unexpected pubKeyAuth buffer size: %" PRIu32 "", nla->pubKeyAuth.cbBuffer);
|
2018-03-20 11:57:30 +03:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
length = nla->pubKeyAuth.cbBuffer;
|
|
|
|
buffer = (BYTE*)malloc(length);
|
|
|
|
|
|
|
|
if (!buffer)
|
|
|
|
{
|
|
|
|
status = SEC_E_INSUFFICIENT_MEMORY;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (krb)
|
|
|
|
{
|
|
|
|
CopyMemory(buffer, nla->pubKeyAuth.pvBuffer, length);
|
|
|
|
Buffers[0].BufferType = SECBUFFER_DATA; /* Encrypted Hash */
|
|
|
|
Buffers[0].cbBuffer = length;
|
|
|
|
Buffers[0].pvBuffer = buffer;
|
|
|
|
Message.cBuffers = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CopyMemory(buffer, nla->pubKeyAuth.pvBuffer, length);
|
|
|
|
Buffers[0].BufferType = SECBUFFER_TOKEN; /* Signature */
|
|
|
|
Buffers[0].cbBuffer = signature_length;
|
|
|
|
Buffers[0].pvBuffer = buffer;
|
|
|
|
Buffers[1].BufferType = SECBUFFER_DATA; /* Encrypted Hash */
|
|
|
|
Buffers[1].cbBuffer = WINPR_SHA256_DIGEST_LENGTH;
|
|
|
|
Buffers[1].pvBuffer = buffer + signature_length;
|
|
|
|
Message.cBuffers = 2;
|
|
|
|
}
|
|
|
|
|
2018-10-05 19:43:00 +03:00
|
|
|
Message.ulVersion = SECBUFFER_VERSION;
|
2019-11-06 17:24:51 +03:00
|
|
|
Message.pBuffers = (PSecBuffer)&Buffers;
|
2018-03-20 11:57:30 +03:00
|
|
|
status = nla->table->DecryptMessage(&nla->context, &Message, nla->recvSeqNum++, &pfQOP);
|
|
|
|
|
|
|
|
if (status != SEC_E_OK)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "DecryptMessage failure %s [%08" PRIX32 "]", GetSecurityStatusString(status),
|
|
|
|
status);
|
2018-03-20 11:57:30 +03:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generate SHA256 of following data: ServerClientHashMagic, Nonce, SubjectPublicKey */
|
|
|
|
if (!(sha256 = winpr_Digest_New()))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (!winpr_Digest_Init(sha256, WINPR_MD_SHA256))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* include trailing \0 from hashMagic */
|
2018-03-21 13:30:02 +03:00
|
|
|
if (!winpr_Digest_Update(sha256, hashMagic, hashSize))
|
2018-03-20 11:57:30 +03:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (!winpr_Digest_Update(sha256, nla->ClientNonce.pvBuffer, nla->ClientNonce.cbBuffer))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* SubjectPublicKey */
|
|
|
|
if (!winpr_Digest_Update(sha256, nla->PublicKey.pvBuffer, nla->PublicKey.cbBuffer))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (!winpr_Digest_Final(sha256, serverClientHash, sizeof(serverClientHash)))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* verify hash */
|
|
|
|
if (memcmp(serverClientHash, Buffers[krb ? 0 : 1].pvBuffer, WINPR_SHA256_DIGEST_LENGTH) != 0)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Could not verify server's hash");
|
|
|
|
status = SEC_E_MESSAGE_ALTERED; /* DO NOT SEND CREDENTIALS! */
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = SEC_E_OK;
|
|
|
|
fail:
|
|
|
|
free(buffer);
|
|
|
|
winpr_Digest_Free(sha256);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
static size_t nla_sizeof_ts_password_creds(rdpNla* nla)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2018-01-17 10:12:54 +03:00
|
|
|
size_t length = 0;
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-06-15 16:03:13 +03:00
|
|
|
if (nla->identity)
|
|
|
|
{
|
|
|
|
length += ber_sizeof_sequence_octet_string(nla->identity->DomainLength * 2);
|
|
|
|
length += ber_sizeof_sequence_octet_string(nla->identity->UserLength * 2);
|
|
|
|
length += ber_sizeof_sequence_octet_string(nla->identity->PasswordLength * 2);
|
|
|
|
}
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2012-05-25 09:50:46 +04:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
static size_t nla_sizeof_ts_credentials(rdpNla* nla)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
2018-01-17 10:12:54 +03:00
|
|
|
size_t size = 0;
|
2017-05-11 19:51:45 +03:00
|
|
|
size += ber_sizeof_integer(1);
|
|
|
|
size += ber_sizeof_contextual_tag(ber_sizeof_integer(1));
|
2019-11-06 17:24:51 +03:00
|
|
|
size +=
|
|
|
|
ber_sizeof_sequence_octet_string(ber_sizeof_sequence(nla_sizeof_ts_password_creds(nla)));
|
2017-05-11 19:51:45 +03:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2015-06-15 16:03:13 +03:00
|
|
|
BOOL nla_read_ts_password_creds(rdpNla* nla, wStream* s)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2018-01-17 10:12:54 +03:00
|
|
|
size_t length;
|
2015-02-15 19:10:14 +03:00
|
|
|
|
2015-06-15 16:03:13 +03:00
|
|
|
if (!nla->identity)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "nla->identity is NULL!");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-07-01 16:28:35 +03:00
|
|
|
/* TSPasswordCreds (SEQUENCE)
|
|
|
|
* Initialise to default values. */
|
|
|
|
nla->identity->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
|
2019-11-06 17:24:51 +03:00
|
|
|
nla->identity->UserLength = (UINT32)0;
|
2015-07-01 16:28:35 +03:00
|
|
|
nla->identity->User = NULL;
|
2019-11-06 17:24:51 +03:00
|
|
|
nla->identity->DomainLength = (UINT32)0;
|
2015-07-01 16:28:35 +03:00
|
|
|
nla->identity->Domain = NULL;
|
|
|
|
nla->identity->Password = NULL;
|
2019-11-06 17:24:51 +03:00
|
|
|
nla->identity->PasswordLength = (UINT32)0;
|
2015-07-01 16:28:35 +03:00
|
|
|
|
2015-07-01 17:05:11 +03:00
|
|
|
if (!ber_read_sequence_tag(s, &length))
|
|
|
|
return FALSE;
|
|
|
|
|
2015-07-01 16:28:35 +03:00
|
|
|
/* The sequence is empty, return early,
|
|
|
|
* TSPasswordCreds (SEQUENCE) is optional. */
|
2015-07-01 17:05:11 +03:00
|
|
|
if (length == 0)
|
2015-07-01 16:28:35 +03:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
/* [0] domainName (OCTET STRING) */
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!ber_read_contextual_tag(s, 0, &length, TRUE) || !ber_read_octet_string_tag(s, &length))
|
2015-06-16 16:42:07 +03:00
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2015-07-01 16:28:35 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
nla->identity->DomainLength = (UINT32)length;
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-06-23 11:14:11 +03:00
|
|
|
if (nla->identity->DomainLength > 0)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
nla->identity->Domain = (UINT16*)malloc(length);
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-06-23 11:14:11 +03:00
|
|
|
if (!nla->identity->Domain)
|
|
|
|
return FALSE;
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-06-23 11:14:11 +03:00
|
|
|
CopyMemory(nla->identity->Domain, Stream_Pointer(s), nla->identity->DomainLength);
|
|
|
|
Stream_Seek(s, nla->identity->DomainLength);
|
|
|
|
nla->identity->DomainLength /= 2;
|
|
|
|
}
|
2015-02-15 19:10:14 +03:00
|
|
|
|
2012-05-25 09:50:46 +04:00
|
|
|
/* [1] userName (OCTET STRING) */
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!ber_read_contextual_tag(s, 1, &length, TRUE) || !ber_read_octet_string_tag(s, &length))
|
2015-06-16 16:42:07 +03:00
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2015-07-01 16:28:35 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
nla->identity->UserLength = (UINT32)length;
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-07-01 15:31:55 +03:00
|
|
|
if (nla->identity->UserLength > 0)
|
2015-06-23 11:14:11 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
nla->identity->User = (UINT16*)malloc(length);
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-06-23 11:14:11 +03:00
|
|
|
if (!nla->identity->User)
|
|
|
|
return FALSE;
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-06-23 11:14:11 +03:00
|
|
|
CopyMemory(nla->identity->User, Stream_Pointer(s), nla->identity->UserLength);
|
|
|
|
Stream_Seek(s, nla->identity->UserLength);
|
|
|
|
nla->identity->UserLength /= 2;
|
|
|
|
}
|
2015-02-15 19:10:14 +03:00
|
|
|
|
2012-05-25 09:50:46 +04:00
|
|
|
/* [2] password (OCTET STRING) */
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!ber_read_contextual_tag(s, 2, &length, TRUE) || !ber_read_octet_string_tag(s, &length))
|
2015-06-16 16:42:07 +03:00
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2015-07-01 16:28:35 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
nla->identity->PasswordLength = (UINT32)length;
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-06-23 11:14:11 +03:00
|
|
|
if (nla->identity->PasswordLength > 0)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
nla->identity->Password = (UINT16*)malloc(length);
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-06-23 11:14:11 +03:00
|
|
|
if (!nla->identity->Password)
|
|
|
|
return FALSE;
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-06-23 11:14:11 +03:00
|
|
|
CopyMemory(nla->identity->Password, Stream_Pointer(s), nla->identity->PasswordLength);
|
|
|
|
Stream_Seek(s, nla->identity->PasswordLength);
|
|
|
|
nla->identity->PasswordLength /= 2;
|
|
|
|
}
|
2015-06-15 16:03:13 +03:00
|
|
|
|
|
|
|
return TRUE;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2018-03-21 13:30:02 +03:00
|
|
|
static size_t nla_write_ts_password_creds(rdpNla* nla, wStream* s)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2018-01-17 10:12:54 +03:00
|
|
|
size_t size = 0;
|
|
|
|
size_t innerSize = nla_sizeof_ts_password_creds(nla);
|
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);
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-06-15 16:03:13 +03:00
|
|
|
if (nla->identity)
|
|
|
|
{
|
|
|
|
/* [0] domainName (OCTET STRING) */
|
2019-11-06 17:24:51 +03:00
|
|
|
size += ber_write_sequence_octet_string(s, 0, (BYTE*)nla->identity->Domain,
|
|
|
|
nla->identity->DomainLength * 2);
|
2015-06-15 16:03:13 +03:00
|
|
|
/* [1] userName (OCTET STRING) */
|
2019-11-06 17:24:51 +03:00
|
|
|
size += ber_write_sequence_octet_string(s, 1, (BYTE*)nla->identity->User,
|
|
|
|
nla->identity->UserLength * 2);
|
2015-06-15 16:03:13 +03:00
|
|
|
/* [2] password (OCTET STRING) */
|
2019-11-06 17:24:51 +03:00
|
|
|
size += ber_write_sequence_octet_string(s, 2, (BYTE*)nla->identity->Password,
|
|
|
|
nla->identity->PasswordLength * 2);
|
2015-06-15 16:03:13 +03:00
|
|
|
}
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2013-05-21 19:06:00 +04:00
|
|
|
return size;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2015-06-15 16:03:13 +03:00
|
|
|
static BOOL nla_read_ts_credentials(rdpNla* nla, PSecBuffer ts_credentials)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
wStream* s;
|
2018-01-17 10:12:54 +03:00
|
|
|
size_t length;
|
|
|
|
size_t ts_password_creds_length = 0;
|
2015-06-16 16:42:07 +03:00
|
|
|
BOOL ret;
|
2017-11-14 15:57:48 +03:00
|
|
|
|
|
|
|
if (!ts_credentials || !ts_credentials->pvBuffer)
|
|
|
|
return FALSE;
|
|
|
|
|
2013-05-09 01:48:30 +04:00
|
|
|
s = Stream_New(ts_credentials->pvBuffer, ts_credentials->cbBuffer);
|
2015-02-03 01:16:32 +03:00
|
|
|
|
2015-05-29 14:46:50 +03:00
|
|
|
if (!s)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_New failed!");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2017-02-20 20:31:58 +03:00
|
|
|
/* TSCredentials (SEQUENCE) */
|
2015-06-16 16:42:07 +03:00
|
|
|
ret = ber_read_sequence_tag(s, &length) &&
|
2017-02-20 20:31:58 +03:00
|
|
|
/* [0] credType (INTEGER) */
|
2019-11-06 17:24:51 +03:00
|
|
|
ber_read_contextual_tag(s, 0, &length, TRUE) && ber_read_integer(s, NULL) &&
|
2017-11-13 17:50:28 +03:00
|
|
|
/* [1] credentials (OCTET STRING) */
|
|
|
|
ber_read_contextual_tag(s, 1, &length, TRUE) &&
|
2017-02-20 20:31:58 +03:00
|
|
|
ber_read_octet_string_tag(s, &ts_password_creds_length) &&
|
|
|
|
nla_read_ts_password_creds(nla, s);
|
2013-05-09 01:48:30 +04:00
|
|
|
Stream_Free(s, FALSE);
|
2015-06-16 16:42:07 +03:00
|
|
|
return ret;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2018-03-21 13:30:02 +03:00
|
|
|
static size_t nla_write_ts_credentials(rdpNla* nla, wStream* s)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2018-03-21 13:30:02 +03:00
|
|
|
size_t size = 0;
|
|
|
|
size_t passwordSize;
|
|
|
|
size_t innerSize = nla_sizeof_ts_credentials(nla);
|
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) */
|
2015-02-15 19:10:14 +03:00
|
|
|
passwordSize = ber_sizeof_sequence(nla_sizeof_ts_password_creds(nla));
|
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);
|
2015-02-15 19:10:14 +03:00
|
|
|
size += nla_write_ts_password_creds(nla, s);
|
2013-05-21 19:06:00 +04:00
|
|
|
return size;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode TSCredentials structure.
|
|
|
|
* @param credssp
|
|
|
|
*/
|
|
|
|
|
2017-07-18 13:54:00 +03:00
|
|
|
static BOOL nla_encode_ts_credentials(rdpNla* nla)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
wStream* s;
|
2018-03-21 13:30:02 +03:00
|
|
|
size_t length;
|
2015-06-15 16:03:13 +03:00
|
|
|
int DomainLength = 0;
|
|
|
|
int UserLength = 0;
|
|
|
|
int PasswordLength = 0;
|
2015-02-03 01:16:32 +03:00
|
|
|
|
2015-06-15 16:03:13 +03:00
|
|
|
if (nla->identity)
|
|
|
|
{
|
2017-05-11 19:51:45 +03:00
|
|
|
/* TSPasswordCreds */
|
2015-06-15 16:03:13 +03:00
|
|
|
DomainLength = nla->identity->DomainLength;
|
|
|
|
UserLength = nla->identity->UserLength;
|
|
|
|
PasswordLength = nla->identity->PasswordLength;
|
|
|
|
}
|
2013-11-06 10:51:55 +04:00
|
|
|
|
2015-06-15 16:03:13 +03:00
|
|
|
if (nla->settings->DisableCredentialsDelegation && nla->identity)
|
2013-11-06 10:51:55 +04:00
|
|
|
{
|
2017-05-11 19:51:45 +03:00
|
|
|
/* TSPasswordCreds */
|
2015-06-15 16:03:13 +03:00
|
|
|
nla->identity->DomainLength = 0;
|
|
|
|
nla->identity->UserLength = 0;
|
|
|
|
nla->identity->PasswordLength = 0;
|
2013-11-06 10:51:55 +04:00
|
|
|
}
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
length = ber_sizeof_sequence(nla_sizeof_ts_credentials(nla));
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!sspi_SecBufferAlloc(&nla->tsCredentials, length))
|
2015-05-29 14:46:50 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "sspi_SecBufferAlloc failed!");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
s = Stream_New((BYTE*)nla->tsCredentials.pvBuffer, length);
|
2015-05-29 14:46:50 +03:00
|
|
|
|
|
|
|
if (!s)
|
|
|
|
{
|
|
|
|
sspi_SecBufferFree(&nla->tsCredentials);
|
|
|
|
WLog_ERR(TAG, "Stream_New failed!");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
nla_write_ts_credentials(nla, s);
|
2013-05-09 01:48:30 +04:00
|
|
|
|
2018-03-06 17:39:03 +03:00
|
|
|
if (nla->settings->DisableCredentialsDelegation && nla->identity)
|
2013-11-06 10:51:55 +04:00
|
|
|
{
|
2017-05-11 19:51:45 +03:00
|
|
|
/* TSPasswordCreds */
|
2015-06-15 16:03:13 +03:00
|
|
|
nla->identity->DomainLength = DomainLength;
|
|
|
|
nla->identity->UserLength = UserLength;
|
|
|
|
nla->identity->PasswordLength = PasswordLength;
|
2013-11-06 10:51:55 +04:00
|
|
|
}
|
|
|
|
|
2013-05-09 01:48:30 +04:00
|
|
|
Stream_Free(s, FALSE);
|
2015-05-29 14:46:50 +03:00
|
|
|
return TRUE;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2017-07-18 13:54:00 +03:00
|
|
|
static SECURITY_STATUS nla_encrypt_ts_credentials(rdpNla* nla)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2017-12-21 13:04:32 +03:00
|
|
|
SecBuffer Buffers[2] = { { 0 } };
|
2012-05-25 09:50:46 +04:00
|
|
|
SecBufferDesc Message;
|
|
|
|
SECURITY_STATUS status;
|
2019-11-06 17:24:51 +03:00
|
|
|
const BOOL krb =
|
|
|
|
(_tcsncmp(nla->packageName, KERBEROS_SSP_NAME, ARRAYSIZE(KERBEROS_SSP_NAME)) == 0);
|
2018-02-13 13:00:56 +03:00
|
|
|
const BOOL nego = (_tcsncmp(nla->packageName, NEGO_SSP_NAME, ARRAYSIZE(NEGO_SSP_NAME)) == 0);
|
2019-11-06 17:24:51 +03:00
|
|
|
const BOOL ntlm = (_tcsncmp(nla->packageName, NTLM_SSP_NAME, ARRAYSIZE(NTLM_SSP_NAME)) == 0);
|
2015-02-03 01:16:32 +03:00
|
|
|
|
2015-05-29 14:46:50 +03:00
|
|
|
if (!nla_encode_ts_credentials(nla))
|
|
|
|
return SEC_E_INSUFFICIENT_MEMORY;
|
2015-02-03 01:16:32 +03:00
|
|
|
|
2017-02-20 20:31:58 +03:00
|
|
|
if (!sspi_SecBufferAlloc(&nla->authInfo,
|
2017-05-11 19:51:45 +03:00
|
|
|
nla->tsCredentials.cbBuffer + nla->ContextSizes.cbSecurityTrailer))
|
2015-04-03 17:21:01 +03:00
|
|
|
return SEC_E_INSUFFICIENT_MEMORY;
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2018-01-17 10:17:33 +03:00
|
|
|
if (krb)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
|
|
|
Buffers[0].BufferType = SECBUFFER_DATA; /* TSCredentials */
|
|
|
|
Buffers[0].cbBuffer = nla->tsCredentials.cbBuffer;
|
|
|
|
Buffers[0].pvBuffer = nla->authInfo.pvBuffer;
|
|
|
|
CopyMemory(Buffers[0].pvBuffer, nla->tsCredentials.pvBuffer, Buffers[0].cbBuffer);
|
|
|
|
Message.cBuffers = 1;
|
|
|
|
}
|
2018-01-17 10:17:33 +03:00
|
|
|
else if (ntlm || nego)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
|
|
|
Buffers[0].BufferType = SECBUFFER_TOKEN; /* Signature */
|
|
|
|
Buffers[0].cbBuffer = nla->ContextSizes.cbSecurityTrailer;
|
|
|
|
Buffers[0].pvBuffer = nla->authInfo.pvBuffer;
|
|
|
|
MoveMemory(Buffers[0].pvBuffer, nla->authInfo.pvBuffer, Buffers[0].cbBuffer);
|
|
|
|
Buffers[1].BufferType = SECBUFFER_DATA; /* TSCredentials */
|
|
|
|
Buffers[1].cbBuffer = nla->tsCredentials.cbBuffer;
|
2019-11-06 17:24:51 +03:00
|
|
|
Buffers[1].pvBuffer = &((BYTE*)nla->authInfo.pvBuffer)[Buffers[0].cbBuffer];
|
2017-05-11 19:51:45 +03:00
|
|
|
CopyMemory(Buffers[1].pvBuffer, nla->tsCredentials.pvBuffer, Buffers[1].cbBuffer);
|
|
|
|
Message.cBuffers = 2;
|
|
|
|
}
|
2017-11-14 15:57:48 +03:00
|
|
|
|
2018-10-05 19:43:00 +03:00
|
|
|
Message.ulVersion = SECBUFFER_VERSION;
|
2019-11-06 17:24:51 +03:00
|
|
|
Message.pBuffers = (PSecBuffer)&Buffers;
|
2015-02-15 19:10:14 +03:00
|
|
|
status = nla->table->EncryptMessage(&nla->context, 0, &Message, nla->sendSeqNum++);
|
2012-05-25 09:50:46 +04:00
|
|
|
|
|
|
|
if (status != SEC_E_OK)
|
2015-07-07 14:46:05 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "EncryptMessage failure %s [0x%08" PRIX32 "]",
|
2017-02-20 20:31:58 +03:00
|
|
|
GetSecurityStatusString(status), status);
|
2012-05-25 09:50:46 +04:00
|
|
|
return status;
|
2015-07-07 14:46:05 +03:00
|
|
|
}
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2018-10-05 19:43:00 +03:00
|
|
|
if (Message.cBuffers == 2 && Buffers[0].cbBuffer < nla->ContextSizes.cbSecurityTrailer)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
/* IMPORTANT: EncryptMessage may not use all the signature space, so we need to shrink the
|
|
|
|
* excess between the buffers */
|
2018-11-21 17:36:31 +03:00
|
|
|
MoveMemory(((BYTE*)Buffers[0].pvBuffer) + Buffers[0].cbBuffer, Buffers[1].pvBuffer,
|
|
|
|
Buffers[1].cbBuffer);
|
2018-10-05 19:43:00 +03:00
|
|
|
nla->authInfo.cbBuffer = Buffers[0].cbBuffer + Buffers[1].cbBuffer;
|
|
|
|
}
|
|
|
|
|
2012-05-25 09:50:46 +04:00
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-18 13:54:00 +03:00
|
|
|
static SECURITY_STATUS nla_decrypt_ts_credentials(rdpNla* nla)
|
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;
|
2017-12-21 13:04:32 +03:00
|
|
|
SecBuffer Buffers[2] = { { 0 } };
|
2012-05-25 09:50:46 +04:00
|
|
|
SecBufferDesc Message;
|
|
|
|
SECURITY_STATUS status;
|
2019-11-06 17:24:51 +03:00
|
|
|
const BOOL krb =
|
|
|
|
(_tcsncmp(nla->packageName, KERBEROS_SSP_NAME, ARRAYSIZE(KERBEROS_SSP_NAME)) == 0);
|
2018-02-13 13:00:56 +03:00
|
|
|
const BOOL nego = (_tcsncmp(nla->packageName, NEGO_SSP_NAME, ARRAYSIZE(NEGO_SSP_NAME)) == 0);
|
2019-11-06 17:24:51 +03:00
|
|
|
const BOOL ntlm = (_tcsncmp(nla->packageName, NTLM_SSP_NAME, ARRAYSIZE(NTLM_SSP_NAME)) == 0);
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
if (nla->authInfo.cbBuffer < 1)
|
2012-06-16 06:39:12 +04:00
|
|
|
{
|
2015-02-15 19:10:14 +03:00
|
|
|
WLog_ERR(TAG, "nla_decrypt_ts_credentials missing authInfo buffer");
|
2012-06-16 06:39:12 +04:00
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
}
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
length = nla->authInfo.cbBuffer;
|
2019-11-06 17:24:51 +03:00
|
|
|
buffer = (BYTE*)malloc(length);
|
2015-02-15 19:10:14 +03:00
|
|
|
|
|
|
|
if (!buffer)
|
|
|
|
return SEC_E_INSUFFICIENT_MEMORY;
|
|
|
|
|
2018-01-17 10:17:33 +03:00
|
|
|
if (krb)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
|
|
|
CopyMemory(buffer, nla->authInfo.pvBuffer, length);
|
|
|
|
Buffers[0].BufferType = SECBUFFER_DATA; /* Wrapped and encrypted TSCredentials */
|
|
|
|
Buffers[0].cbBuffer = length;
|
|
|
|
Buffers[0].pvBuffer = buffer;
|
|
|
|
Message.cBuffers = 1;
|
|
|
|
}
|
2018-01-17 10:17:33 +03:00
|
|
|
else if (ntlm || nego)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
|
|
|
CopyMemory(buffer, nla->authInfo.pvBuffer, length);
|
|
|
|
Buffers[0].BufferType = SECBUFFER_TOKEN; /* Signature */
|
|
|
|
Buffers[0].cbBuffer = nla->ContextSizes.cbSecurityTrailer;
|
|
|
|
Buffers[0].pvBuffer = buffer;
|
|
|
|
Buffers[1].BufferType = SECBUFFER_DATA; /* TSCredentials */
|
|
|
|
Buffers[1].cbBuffer = length - nla->ContextSizes.cbSecurityTrailer;
|
2019-11-06 17:24:51 +03:00
|
|
|
Buffers[1].pvBuffer = &buffer[Buffers[0].cbBuffer];
|
2017-05-11 19:51:45 +03:00
|
|
|
Message.cBuffers = 2;
|
|
|
|
}
|
2017-11-14 15:57:48 +03:00
|
|
|
|
2018-10-05 19:43:00 +03:00
|
|
|
Message.ulVersion = SECBUFFER_VERSION;
|
2019-11-06 17:24:51 +03:00
|
|
|
Message.pBuffers = (PSecBuffer)&Buffers;
|
2015-02-15 19:10:14 +03:00
|
|
|
status = nla->table->DecryptMessage(&nla->context, &Message, nla->recvSeqNum++, &pfQOP);
|
2012-05-25 09:50:46 +04:00
|
|
|
|
|
|
|
if (status != SEC_E_OK)
|
2015-07-07 14:46:05 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "DecryptMessage failure %s [0x%08" PRIX32 "]",
|
2017-02-20 20:31:58 +03:00
|
|
|
GetSecurityStatusString(status), status);
|
2017-07-18 12:38:59 +03:00
|
|
|
free(buffer);
|
2012-05-25 09:50:46 +04:00
|
|
|
return status;
|
2015-07-07 14:46:05 +03:00
|
|
|
}
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2017-02-20 20:31:58 +03:00
|
|
|
if (!nla_read_ts_credentials(nla, &Buffers[1]))
|
2015-05-29 14:46:50 +03:00
|
|
|
{
|
|
|
|
free(buffer);
|
|
|
|
return SEC_E_INSUFFICIENT_MEMORY;
|
|
|
|
}
|
2015-02-15 19:10:14 +03:00
|
|
|
|
2017-02-20 20:31:58 +03:00
|
|
|
free(buffer);
|
2012-05-25 09:50:46 +04:00
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
static size_t nla_sizeof_nego_token(size_t 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;
|
|
|
|
}
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
static size_t nla_sizeof_nego_tokens(size_t length)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2015-02-15 19:10:14 +03:00
|
|
|
length = nla_sizeof_nego_token(length);
|
2013-05-21 19:06:00 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
static size_t nla_sizeof_pub_key_auth(size_t 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;
|
|
|
|
}
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
static size_t nla_sizeof_auth_info(size_t 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;
|
|
|
|
}
|
|
|
|
|
2018-03-20 11:57:30 +03:00
|
|
|
static size_t nla_sizeof_client_nonce(size_t length)
|
|
|
|
{
|
|
|
|
length = ber_sizeof_octet_string(length);
|
|
|
|
length += ber_sizeof_contextual_tag(length);
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
static size_t nla_sizeof_ts_request(size_t 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
|
|
|
|
*/
|
|
|
|
|
2015-05-29 14:46:50 +03:00
|
|
|
BOOL nla_send(rdpNla* nla)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2018-10-02 14:18:13 +03:00
|
|
|
BOOL rc = TRUE;
|
2014-08-18 21:34:47 +04:00
|
|
|
wStream* s;
|
2018-01-17 10:12:54 +03:00
|
|
|
size_t length;
|
|
|
|
size_t ts_request_length;
|
|
|
|
size_t nego_tokens_length = 0;
|
|
|
|
size_t pub_key_auth_length = 0;
|
|
|
|
size_t auth_info_length = 0;
|
|
|
|
size_t error_code_context_length = 0;
|
|
|
|
size_t error_code_length = 0;
|
2018-03-20 11:57:30 +03:00
|
|
|
size_t client_nonce_length = 0;
|
2019-11-06 17:24:51 +03:00
|
|
|
nego_tokens_length =
|
|
|
|
(nla->negoToken.cbBuffer > 0) ? nla_sizeof_nego_tokens(nla->negoToken.cbBuffer) : 0;
|
|
|
|
pub_key_auth_length =
|
|
|
|
(nla->pubKeyAuth.cbBuffer > 0) ? nla_sizeof_pub_key_auth(nla->pubKeyAuth.cbBuffer) : 0;
|
|
|
|
auth_info_length =
|
|
|
|
(nla->authInfo.cbBuffer > 0) ? nla_sizeof_auth_info(nla->authInfo.cbBuffer) : 0;
|
|
|
|
client_nonce_length =
|
|
|
|
(nla->ClientNonce.cbBuffer > 0) ? nla_sizeof_client_nonce(nla->ClientNonce.cbBuffer) : 0;
|
2016-03-18 15:32:13 +03:00
|
|
|
|
2018-03-23 14:12:08 +03:00
|
|
|
if (nla->peerVersion >= 3 && nla->peerVersion != 5 && nla->errorCode != 0)
|
2016-03-18 15:32:13 +03:00
|
|
|
{
|
|
|
|
error_code_length = ber_sizeof_integer(nla->errorCode);
|
|
|
|
error_code_context_length = ber_sizeof_contextual_tag(error_code_length);
|
|
|
|
}
|
2015-02-15 19:10:14 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
length = nego_tokens_length + pub_key_auth_length + auth_info_length +
|
|
|
|
error_code_context_length + error_code_length + client_nonce_length;
|
2017-02-20 20:31:58 +03:00
|
|
|
ts_request_length = nla_sizeof_ts_request(length);
|
2013-05-21 19:06:00 +04:00
|
|
|
s = Stream_New(NULL, ber_sizeof_sequence(ts_request_length));
|
2015-02-15 19:10:14 +03:00
|
|
|
|
2015-05-29 14:46:50 +03:00
|
|
|
if (!s)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_New failed!");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
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);
|
2016-03-18 15:32:13 +03:00
|
|
|
ber_write_integer(s, nla->version); /* INTEGER */
|
2012-05-25 09:50:46 +04:00
|
|
|
|
|
|
|
/* [1] negoTokens (NegoData) */
|
|
|
|
if (nego_tokens_length > 0)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
length = ber_write_contextual_tag(
|
|
|
|
s, 1,
|
|
|
|
ber_sizeof_sequence(
|
|
|
|
ber_sizeof_sequence(ber_sizeof_sequence_octet_string(nla->negoToken.cbBuffer))),
|
|
|
|
TRUE); /* NegoData */
|
|
|
|
length +=
|
|
|
|
ber_write_sequence_tag(s, ber_sizeof_sequence(ber_sizeof_sequence_octet_string(
|
|
|
|
nla->negoToken.cbBuffer))); /* SEQUENCE OF NegoDataItem */
|
|
|
|
length += ber_write_sequence_tag(
|
|
|
|
s, ber_sizeof_sequence_octet_string(nla->negoToken.cbBuffer)); /* NegoDataItem */
|
|
|
|
length += ber_write_sequence_octet_string(s, 0, (BYTE*)nla->negoToken.pvBuffer,
|
|
|
|
nla->negoToken.cbBuffer); /* OCTET STRING */
|
2017-02-20 20:31:58 +03:00
|
|
|
|
|
|
|
if (length != nego_tokens_length)
|
2018-08-17 17:20:43 +03:00
|
|
|
{
|
|
|
|
Stream_Free(s, TRUE);
|
2017-02-20 20:31:58 +03:00
|
|
|
return FALSE;
|
2018-08-17 17:20:43 +03:00
|
|
|
}
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* [2] authInfo (OCTET STRING) */
|
|
|
|
if (auth_info_length > 0)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
if (ber_write_sequence_octet_string(s, 2, nla->authInfo.pvBuffer, nla->authInfo.cbBuffer) !=
|
|
|
|
auth_info_length)
|
2018-08-17 17:20:43 +03:00
|
|
|
{
|
|
|
|
Stream_Free(s, TRUE);
|
2017-02-20 20:31:58 +03:00
|
|
|
return FALSE;
|
2018-08-17 17:20:43 +03:00
|
|
|
}
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* [3] pubKeyAuth (OCTET STRING) */
|
|
|
|
if (pub_key_auth_length > 0)
|
|
|
|
{
|
2017-02-20 20:31:58 +03:00
|
|
|
if (ber_write_sequence_octet_string(s, 3, nla->pubKeyAuth.pvBuffer,
|
|
|
|
nla->pubKeyAuth.cbBuffer) != pub_key_auth_length)
|
2018-08-17 17:20:43 +03:00
|
|
|
{
|
|
|
|
Stream_Free(s, TRUE);
|
2017-02-20 20:31:58 +03:00
|
|
|
return FALSE;
|
2018-08-17 17:20:43 +03:00
|
|
|
}
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2016-03-18 15:32:13 +03:00
|
|
|
/* [4] errorCode (INTEGER) */
|
|
|
|
if (error_code_length > 0)
|
|
|
|
{
|
|
|
|
ber_write_contextual_tag(s, 4, error_code_length, TRUE);
|
|
|
|
ber_write_integer(s, nla->errorCode);
|
|
|
|
}
|
|
|
|
|
2018-03-20 11:57:30 +03:00
|
|
|
/* [5] clientNonce (OCTET STRING) */
|
|
|
|
if (client_nonce_length > 0)
|
|
|
|
{
|
|
|
|
if (ber_write_sequence_octet_string(s, 5, nla->ClientNonce.pvBuffer,
|
|
|
|
nla->ClientNonce.cbBuffer) != client_nonce_length)
|
2018-08-17 17:20:43 +03:00
|
|
|
{
|
|
|
|
Stream_Free(s, TRUE);
|
2018-03-20 11:57:30 +03:00
|
|
|
return FALSE;
|
2018-08-17 17:20:43 +03:00
|
|
|
}
|
2018-03-20 11:57:30 +03:00
|
|
|
}
|
|
|
|
|
2013-05-15 20:14:26 +04:00
|
|
|
Stream_SealLength(s);
|
2018-10-02 14:18:13 +03:00
|
|
|
|
|
|
|
if (transport_write(nla->transport, s) < 0)
|
|
|
|
rc = FALSE;
|
|
|
|
|
2013-05-09 01:48:30 +04:00
|
|
|
Stream_Free(s, TRUE);
|
2018-10-02 14:18:13 +03:00
|
|
|
return rc;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2017-07-18 13:54:00 +03:00
|
|
|
static int nla_decode_ts_request(rdpNla* nla, wStream* s)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2018-01-17 10:12:54 +03:00
|
|
|
size_t length;
|
2018-03-14 15:39:23 +03:00
|
|
|
UINT32 version = 0;
|
2015-02-03 01:16:32 +03:00
|
|
|
|
2012-05-25 09:50:46 +04:00
|
|
|
/* TSRequest */
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!ber_read_sequence_tag(s, &length) || !ber_read_contextual_tag(s, 0, &length, TRUE) ||
|
2018-03-14 15:39:23 +03:00
|
|
|
!ber_read_integer(s, &version))
|
2013-08-28 18:44:54 +04:00
|
|
|
{
|
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
|
|
|
|
2018-03-23 14:12:08 +03:00
|
|
|
if (nla->peerVersion == 0)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_DBG(TAG, "CredSSP protocol support %" PRIu32 ", peer supports %" PRIu32, nla->version,
|
|
|
|
version);
|
2018-03-23 14:12:08 +03:00
|
|
|
nla->peerVersion = version;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if the peer suddenly changed its version - kick it */
|
|
|
|
if (nla->peerVersion != version)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "CredSSP peer changed protocol version from %" PRIu32 " to %" PRIu32,
|
2018-05-03 18:49:14 +03:00
|
|
|
nla->peerVersion, version);
|
2018-03-23 14:12:08 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2018-03-14 15:39:23 +03: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
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!ber_read_sequence_tag(s, &length) || /* SEQUENCE OF NegoDataItem */
|
|
|
|
!ber_read_sequence_tag(s, &length) || /* NegoDataItem */
|
2017-02-20 20:31:58 +03:00
|
|
|
!ber_read_contextual_tag(s, 0, &length, TRUE) || /* [0] negoToken */
|
2019-11-06 17:24:51 +03:00
|
|
|
!ber_read_octet_string_tag(s, &length) || /* OCTET STRING */
|
2018-03-20 11:57:30 +03:00
|
|
|
Stream_GetRemainingLength(s) < length)
|
2013-08-28 18:44:54 +04:00
|
|
|
{
|
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
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!sspi_SecBufferAlloc(&nla->negoToken, length))
|
|
|
|
return -1;
|
2016-03-18 15:32:13 +03:00
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
Stream_Read(s, nla->negoToken.pvBuffer, length);
|
|
|
|
nla->negoToken.cbBuffer = length;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* [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 */
|
2018-03-20 11:57:30 +03:00
|
|
|
Stream_GetRemainingLength(s) < length)
|
2013-01-11 04:27:19 +04:00
|
|
|
return -1;
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!sspi_SecBufferAlloc(&nla->authInfo, length))
|
|
|
|
return -1;
|
2016-03-18 15:32:13 +03:00
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
Stream_Read(s, nla->authInfo.pvBuffer, length);
|
|
|
|
nla->authInfo.cbBuffer = length;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* [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 */
|
2018-03-20 11:57:30 +03:00
|
|
|
Stream_GetRemainingLength(s) < length)
|
2013-01-11 04:27:19 +04:00
|
|
|
return -1;
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!sspi_SecBufferAlloc(&nla->pubKeyAuth, length))
|
|
|
|
return -1;
|
2016-03-18 15:32:13 +03:00
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
Stream_Read(s, nla->pubKeyAuth.pvBuffer, length);
|
|
|
|
nla->pubKeyAuth.cbBuffer = length;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2016-03-18 15:32:13 +03:00
|
|
|
/* [4] errorCode (INTEGER) */
|
2018-03-23 14:12:08 +03:00
|
|
|
if (nla->peerVersion >= 3)
|
2016-03-18 15:32:13 +03:00
|
|
|
{
|
|
|
|
if (ber_read_contextual_tag(s, 4, &length, TRUE) != FALSE)
|
|
|
|
{
|
|
|
|
if (!ber_read_integer(s, &nla->errorCode))
|
|
|
|
return -1;
|
|
|
|
}
|
2018-03-20 11:57:30 +03:00
|
|
|
|
2018-03-23 14:12:08 +03:00
|
|
|
if (nla->peerVersion >= 5)
|
2018-03-20 11:57:30 +03:00
|
|
|
{
|
|
|
|
if (ber_read_contextual_tag(s, 5, &length, TRUE) != FALSE)
|
|
|
|
{
|
|
|
|
if (!ber_read_octet_string_tag(s, &length) || /* OCTET STRING */
|
|
|
|
Stream_GetRemainingLength(s) < length)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!sspi_SecBufferAlloc(&nla->ClientNonce, length))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
Stream_Read(s, nla->ClientNonce.pvBuffer, length);
|
|
|
|
nla->ClientNonce.cbBuffer = length;
|
|
|
|
}
|
|
|
|
}
|
2016-03-18 15:32:13 +03:00
|
|
|
}
|
|
|
|
|
2015-02-15 22:54:10 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-02-16 00:04:59 +03:00
|
|
|
int nla_recv_pdu(rdpNla* nla, wStream* s)
|
|
|
|
{
|
|
|
|
if (nla_decode_ts_request(nla, s) < 1)
|
|
|
|
return -1;
|
|
|
|
|
2016-03-21 18:58:09 +03:00
|
|
|
if (nla->errorCode)
|
|
|
|
{
|
2017-10-25 10:56:39 +03:00
|
|
|
UINT32 code;
|
2017-11-14 15:57:48 +03:00
|
|
|
|
2017-10-25 10:56:39 +03:00
|
|
|
switch (nla->errorCode)
|
|
|
|
{
|
2017-11-14 15:57:48 +03:00
|
|
|
case STATUS_PASSWORD_MUST_CHANGE:
|
|
|
|
code = FREERDP_ERROR_CONNECT_PASSWORD_MUST_CHANGE;
|
|
|
|
break;
|
2017-10-25 10:56:39 +03:00
|
|
|
|
2017-11-14 15:57:48 +03:00
|
|
|
case STATUS_PASSWORD_EXPIRED:
|
|
|
|
code = FREERDP_ERROR_CONNECT_PASSWORD_EXPIRED;
|
|
|
|
break;
|
2017-10-25 10:56:39 +03:00
|
|
|
|
2017-11-14 15:57:48 +03:00
|
|
|
case STATUS_ACCOUNT_DISABLED:
|
|
|
|
code = FREERDP_ERROR_CONNECT_ACCOUNT_DISABLED;
|
|
|
|
break;
|
2017-10-25 10:56:39 +03:00
|
|
|
|
2017-11-14 15:57:48 +03:00
|
|
|
case STATUS_LOGON_FAILURE:
|
|
|
|
code = FREERDP_ERROR_CONNECT_LOGON_FAILURE;
|
|
|
|
break;
|
2017-11-06 11:49:03 +03:00
|
|
|
|
2017-11-14 15:57:48 +03:00
|
|
|
case STATUS_WRONG_PASSWORD:
|
|
|
|
code = FREERDP_ERROR_CONNECT_WRONG_PASSWORD;
|
|
|
|
break;
|
2017-11-06 11:49:03 +03:00
|
|
|
|
2017-11-14 15:57:48 +03:00
|
|
|
case STATUS_ACCESS_DENIED:
|
|
|
|
code = FREERDP_ERROR_CONNECT_ACCESS_DENIED;
|
|
|
|
break;
|
2017-11-06 11:49:03 +03:00
|
|
|
|
2017-11-14 15:57:48 +03:00
|
|
|
case STATUS_ACCOUNT_RESTRICTION:
|
|
|
|
code = FREERDP_ERROR_CONNECT_ACCOUNT_RESTRICTION;
|
|
|
|
break;
|
2017-11-06 11:49:03 +03:00
|
|
|
|
2017-11-14 15:57:48 +03:00
|
|
|
case STATUS_ACCOUNT_LOCKED_OUT:
|
|
|
|
code = FREERDP_ERROR_CONNECT_ACCOUNT_LOCKED_OUT;
|
|
|
|
break;
|
2017-11-06 11:49:03 +03:00
|
|
|
|
2017-11-14 15:57:48 +03:00
|
|
|
case STATUS_ACCOUNT_EXPIRED:
|
|
|
|
code = FREERDP_ERROR_CONNECT_ACCOUNT_EXPIRED;
|
|
|
|
break;
|
2017-11-06 11:49:03 +03:00
|
|
|
|
2017-11-14 15:57:48 +03:00
|
|
|
case STATUS_LOGON_TYPE_NOT_GRANTED:
|
|
|
|
code = FREERDP_ERROR_CONNECT_LOGON_TYPE_NOT_GRANTED;
|
|
|
|
break;
|
2017-11-06 11:49:03 +03:00
|
|
|
|
2017-11-14 15:57:48 +03:00
|
|
|
default:
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "SPNEGO failed with NTSTATUS: 0x%08" PRIX32 "", nla->errorCode);
|
2017-11-08 13:30:32 +03:00
|
|
|
code = FREERDP_ERROR_AUTHENTICATION_FAILED;
|
2017-11-14 15:57:48 +03:00
|
|
|
break;
|
2017-10-25 10:56:39 +03:00
|
|
|
}
|
2017-11-14 15:57:48 +03:00
|
|
|
|
2020-01-08 19:39:25 +03:00
|
|
|
freerdp_set_last_error_log(nla->instance->context, code);
|
2016-03-21 18:58:09 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-02-16 00:04:59 +03:00
|
|
|
if (nla_client_recv(nla) < 1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-02-15 22:54:10 +03:00
|
|
|
int nla_recv(rdpNla* nla)
|
|
|
|
{
|
|
|
|
wStream* s;
|
|
|
|
int status;
|
|
|
|
s = Stream_New(NULL, 4096);
|
|
|
|
|
2015-05-29 14:46:50 +03:00
|
|
|
if (!s)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_New failed!");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-02-15 22:54:10 +03:00
|
|
|
status = transport_read_pdu(nla->transport, s);
|
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "nla_recv() error: %d", status);
|
|
|
|
Stream_Free(s, TRUE);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-03-23 10:33:14 +03:00
|
|
|
if (nla_decode_ts_request(nla, s) < 1)
|
2016-03-18 15:32:13 +03:00
|
|
|
{
|
|
|
|
Stream_Free(s, TRUE);
|
2015-02-15 22:54:10 +03:00
|
|
|
return -1;
|
2016-03-18 15:32:13 +03:00
|
|
|
}
|
2015-02-15 22:54:10 +03:00
|
|
|
|
2013-05-09 01:48:30 +04:00
|
|
|
Stream_Free(s, TRUE);
|
2015-02-15 19:10:14 +03:00
|
|
|
return 1;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
void nla_buffer_print(rdpNla* nla)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2015-02-15 19:10:14 +03:00
|
|
|
if (nla->negoToken.cbBuffer > 0)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_DBG(TAG, "NLA.negoToken (length = %" PRIu32 "):", nla->negoToken.cbBuffer);
|
|
|
|
#if defined(WITH_DEBUG_NLA)
|
2015-02-15 19:10:14 +03:00
|
|
|
winpr_HexDump(TAG, WLOG_DEBUG, nla->negoToken.pvBuffer, nla->negoToken.cbBuffer);
|
2018-11-21 17:36:31 +03:00
|
|
|
#endif
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
if (nla->pubKeyAuth.cbBuffer > 0)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_DBG(TAG, "NLA.pubKeyAuth (length = %" PRIu32 "):", nla->pubKeyAuth.cbBuffer);
|
|
|
|
#if defined(WITH_DEBUG_NLA)
|
2015-02-15 19:10:14 +03:00
|
|
|
winpr_HexDump(TAG, WLOG_DEBUG, nla->pubKeyAuth.pvBuffer, nla->pubKeyAuth.cbBuffer);
|
2018-11-21 17:36:31 +03:00
|
|
|
#endif
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
if (nla->authInfo.cbBuffer > 0)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_DBG(TAG, "NLA.authInfo (length = %" PRIu32 "):", nla->authInfo.cbBuffer);
|
|
|
|
#if defined(WITH_DEBUG_NLA)
|
2015-02-15 19:10:14 +03:00
|
|
|
winpr_HexDump(TAG, WLOG_DEBUG, nla->authInfo.pvBuffer, nla->authInfo.cbBuffer);
|
2018-11-21 17:36:31 +03:00
|
|
|
#endif
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
void nla_buffer_free(rdpNla* nla)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2015-02-15 19:10:14 +03:00
|
|
|
sspi_SecBufferFree(&nla->negoToken);
|
|
|
|
sspi_SecBufferFree(&nla->pubKeyAuth);
|
|
|
|
sspi_SecBufferFree(&nla->authInfo);
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
LPTSTR nla_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
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
if (!hostnameX || !ServiceClassX)
|
|
|
|
{
|
|
|
|
free(hostnameX);
|
|
|
|
free(ServiceClassX);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-02-12 09:43:02 +04:00
|
|
|
|
|
|
|
if (!ServiceClass)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
ServicePrincipalName = (LPTSTR)_tcsdup(hostnameX);
|
2014-02-12 09:43:02 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
ServicePrincipalName = (LPTSTR)calloc(SpnLength, sizeof(TCHAR));
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-04-10 23:07:53 +04:00
|
|
|
if (!ServicePrincipalName)
|
2016-05-12 11:01:30 +03:00
|
|
|
{
|
|
|
|
free(ServiceClassX);
|
|
|
|
free(hostnameX);
|
2014-04-10 23:07:53 +04:00
|
|
|
return NULL;
|
2016-05-12 11:01:30 +03:00
|
|
|
}
|
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.
|
|
|
|
*/
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
rdpNla* nla_new(freerdp* instance, rdpTransport* transport, rdpSettings* settings)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
rdpNla* nla = (rdpNla*)calloc(1, sizeof(rdpNla));
|
2012-05-25 09:50:46 +04:00
|
|
|
|
2015-06-15 16:03:13 +03:00
|
|
|
if (!nla)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
nla->identity = calloc(1, sizeof(SEC_WINNT_AUTH_IDENTITY));
|
2017-11-14 15:57:48 +03:00
|
|
|
|
2015-06-15 16:03:13 +03:00
|
|
|
if (!nla->identity)
|
|
|
|
{
|
2016-07-22 01:58:24 +03:00
|
|
|
free(nla);
|
2015-06-15 16:03:13 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
nla->instance = instance;
|
|
|
|
nla->settings = settings;
|
|
|
|
nla->server = settings->ServerMode;
|
|
|
|
nla->transport = transport;
|
|
|
|
nla->sendSeqNum = 0;
|
|
|
|
nla->recvSeqNum = 0;
|
2018-03-20 11:57:30 +03:00
|
|
|
nla->version = 6;
|
2018-03-29 16:50:16 +03:00
|
|
|
ZeroMemory(&nla->ClientNonce, sizeof(SecBuffer));
|
|
|
|
ZeroMemory(&nla->negoToken, sizeof(SecBuffer));
|
|
|
|
ZeroMemory(&nla->pubKeyAuth, sizeof(SecBuffer));
|
|
|
|
ZeroMemory(&nla->authInfo, sizeof(SecBuffer));
|
|
|
|
SecInvalidateHandle(&nla->context);
|
|
|
|
|
2016-07-22 01:58:24 +03:00
|
|
|
if (settings->NtlmSamFile)
|
2016-07-22 16:06:07 +03:00
|
|
|
{
|
2016-07-22 01:58:24 +03:00
|
|
|
nla->SamFile = _strdup(settings->NtlmSamFile);
|
|
|
|
|
2016-07-22 16:06:07 +03:00
|
|
|
if (!nla->SamFile)
|
2018-03-29 16:50:16 +03:00
|
|
|
goto cleanup;
|
2016-07-22 16:06:07 +03:00
|
|
|
}
|
|
|
|
|
2018-03-29 16:50:16 +03:00
|
|
|
/* init to 0 or we end up freeing a bad pointer if the alloc fails */
|
|
|
|
if (!sspi_SecBufferAlloc(&nla->ClientNonce, NonceLength))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
/* generate random 32-byte nonce */
|
|
|
|
if (winpr_RAND(nla->ClientNonce.pvBuffer, NonceLength) < 0)
|
|
|
|
goto cleanup;
|
2015-06-15 16:03:13 +03:00
|
|
|
|
|
|
|
if (nla->server)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2015-06-22 19:52:13 +03:00
|
|
|
LONG status;
|
|
|
|
HKEY hKey;
|
|
|
|
DWORD dwType;
|
|
|
|
DWORD dwSize;
|
2019-11-06 17:24:51 +03:00
|
|
|
status =
|
|
|
|
RegOpenKeyExA(HKEY_LOCAL_MACHINE, SERVER_KEY, 0, KEY_READ | KEY_WOW64_64KEY, &hKey);
|
2015-06-15 16:03:13 +03:00
|
|
|
|
2015-06-16 16:42:07 +03:00
|
|
|
if (status != ERROR_SUCCESS)
|
|
|
|
return nla;
|
|
|
|
|
|
|
|
status = RegQueryValueEx(hKey, _T("SspiModule"), NULL, &dwType, NULL, &dwSize);
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-06-16 16:42:07 +03:00
|
|
|
if (status != ERROR_SUCCESS)
|
2015-06-22 19:52:13 +03:00
|
|
|
{
|
|
|
|
RegCloseKey(hKey);
|
2015-06-16 16:42:07 +03:00
|
|
|
return nla;
|
2015-06-22 19:52:13 +03:00
|
|
|
}
|
2015-06-16 16:42:07 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
nla->SspiModule = (LPTSTR)malloc(dwSize + sizeof(TCHAR));
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-06-16 16:42:07 +03:00
|
|
|
if (!nla->SspiModule)
|
2012-08-11 02:05:37 +04:00
|
|
|
{
|
2015-06-22 19:52:13 +03:00
|
|
|
RegCloseKey(hKey);
|
2018-03-29 16:50:16 +03:00
|
|
|
goto cleanup;
|
2015-06-16 16:42:07 +03:00
|
|
|
}
|
2012-08-11 02:05:37 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
status =
|
|
|
|
RegQueryValueEx(hKey, _T("SspiModule"), NULL, &dwType, (BYTE*)nla->SspiModule, &dwSize);
|
2012-08-11 02:05:37 +04:00
|
|
|
|
2015-06-16 16:42:07 +03:00
|
|
|
if (status == ERROR_SUCCESS)
|
|
|
|
WLog_INFO(TAG, "Using SSPI Module: %s", nla->SspiModule);
|
2015-06-22 19:52:13 +03:00
|
|
|
|
|
|
|
RegCloseKey(hKey);
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
return nla;
|
2018-03-29 16:50:16 +03:00
|
|
|
cleanup:
|
|
|
|
nla_free(nla);
|
|
|
|
return NULL;
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free CredSSP state machine.
|
|
|
|
* @param credssp
|
|
|
|
*/
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
void nla_free(rdpNla* nla)
|
2012-05-25 09:50:46 +04:00
|
|
|
{
|
2015-02-15 19:10:14 +03:00
|
|
|
if (!nla)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (nla->table)
|
2015-07-07 14:46:05 +03:00
|
|
|
{
|
|
|
|
SECURITY_STATUS status;
|
2017-11-14 15:57:48 +03:00
|
|
|
|
2017-09-19 13:33:33 +03:00
|
|
|
if (SecIsValidHandle(&nla->credentials))
|
|
|
|
{
|
|
|
|
status = nla->table->FreeCredentialsHandle(&nla->credentials);
|
2017-11-14 15:57:48 +03:00
|
|
|
|
|
|
|
if (status != SEC_E_OK)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_WARN(TAG, "FreeCredentialsHandle status %s [0x%08" PRIX32 "]",
|
2017-11-14 15:57:48 +03:00
|
|
|
GetSecurityStatusString(status), status);
|
|
|
|
}
|
|
|
|
|
2017-09-19 13:33:33 +03:00
|
|
|
SecInvalidateHandle(&nla->credentials);
|
|
|
|
}
|
2017-07-18 14:23:22 +03:00
|
|
|
|
2015-07-07 14:46:05 +03:00
|
|
|
status = nla->table->DeleteSecurityContext(&nla->context);
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-07-07 14:46:05 +03:00
|
|
|
if (status != SEC_E_OK)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_WARN(TAG, "DeleteSecurityContext status %s [0x%08" PRIX32 "]",
|
2017-02-20 20:31:58 +03:00
|
|
|
GetSecurityStatusString(status), status);
|
2015-07-07 14:46:05 +03:00
|
|
|
}
|
|
|
|
}
|
2015-02-15 19:10:14 +03:00
|
|
|
|
2016-07-22 16:06:07 +03:00
|
|
|
free(nla->SamFile);
|
|
|
|
nla->SamFile = NULL;
|
2018-03-20 11:57:30 +03:00
|
|
|
sspi_SecBufferFree(&nla->ClientNonce);
|
2015-02-15 19:10:14 +03:00
|
|
|
sspi_SecBufferFree(&nla->PublicKey);
|
|
|
|
sspi_SecBufferFree(&nla->tsCredentials);
|
|
|
|
free(nla->ServicePrincipalName);
|
2015-06-23 16:40:37 +03:00
|
|
|
nla_identity_free(nla->identity);
|
2015-02-15 19:10:14 +03:00
|
|
|
free(nla);
|
2012-05-25 09:50:46 +04:00
|
|
|
}
|
2018-11-20 18:48:59 +03:00
|
|
|
|
|
|
|
SEC_WINNT_AUTH_IDENTITY* nla_get_identity(rdpNla* nla)
|
|
|
|
{
|
|
|
|
if (!nla)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return nla->identity;
|
|
|
|
}
|
|
|
|
|
|
|
|
NLA_STATE nla_get_state(rdpNla* nla)
|
|
|
|
{
|
|
|
|
if (!nla)
|
|
|
|
return NLA_STATE_FINAL;
|
|
|
|
|
|
|
|
return nla->state;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL nla_set_state(rdpNla* nla, NLA_STATE state)
|
|
|
|
{
|
|
|
|
if (!nla)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
nla->state = state;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL nla_set_service_principal(rdpNla* nla, LPSTR principal)
|
|
|
|
{
|
|
|
|
if (!nla || !principal)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
nla->ServicePrincipalName = principal;
|
|
|
|
return TRUE;
|
|
|
|
}
|
2019-03-08 12:10:43 +03:00
|
|
|
|
|
|
|
BOOL nla_impersonate(rdpNla* nla)
|
|
|
|
{
|
|
|
|
if (!nla)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!nla->table || !nla->table->ImpersonateSecurityContext)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return (nla->table->ImpersonateSecurityContext(&nla->context) == SEC_E_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL nla_revert_to_self(rdpNla* nla)
|
|
|
|
{
|
|
|
|
if (!nla)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!nla->table || !nla->table->RevertSecurityContext)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return (nla->table->RevertSecurityContext(&nla->context) == SEC_E_OK);
|
|
|
|
}
|