2011-07-01 02:51:46 +04:00
|
|
|
/**
|
2012-02-21 01:17:57 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-07-01 02:51:46 +04:00
|
|
|
* Credential Security Support Provider (CredSSP)
|
|
|
|
*
|
2012-02-21 01:17:57 +04:00
|
|
|
* Copyright 2010-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2011-07-01 02:51: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
|
|
|
|
*
|
2011-08-29 00:46:36 +04:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-07-01 02:51:46 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <time.h>
|
2012-02-19 21:50:30 +04:00
|
|
|
#include <freerdp/crypto/tls.h>
|
2012-02-18 02:12:21 +04:00
|
|
|
#include <freerdp/auth/ntlmssp.h>
|
2012-02-19 21:50:30 +04:00
|
|
|
#include <freerdp/utils/stream.h>
|
2012-02-18 02:12:21 +04:00
|
|
|
|
2012-02-23 08:41:22 +04:00
|
|
|
#include <freerdp/auth/sspi.h>
|
2012-02-18 02:12:21 +04:00
|
|
|
#include <freerdp/auth/credssp.h>
|
2011-07-01 02:51:46 +04:00
|
|
|
|
2012-02-25 02:17:38 +04:00
|
|
|
//#define WITH_SSPI 1
|
|
|
|
|
2011-07-18 10:34:28 +04:00
|
|
|
/**
|
|
|
|
* TSRequest ::= SEQUENCE {
|
2011-08-29 09:55:16 +04:00
|
|
|
* version [0] INTEGER,
|
2011-07-18 10:34:28 +04:00
|
|
|
* negoTokens [1] NegoData OPTIONAL,
|
2011-08-29 09:55:16 +04:00
|
|
|
* authInfo [2] OCTET STRING OPTIONAL,
|
2011-07-18 10:34:28 +04:00
|
|
|
* pubKeyAuth [3] OCTET STRING OPTIONAL
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* NegoData ::= SEQUENCE OF NegoDataItem
|
|
|
|
*
|
|
|
|
* NegoDataItem ::= SEQUENCE {
|
|
|
|
* negoToken [0] OCTET STRING
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* TSCredentials ::= SEQUENCE {
|
2011-08-29 09:55:16 +04:00
|
|
|
* credType [0] INTEGER,
|
2011-07-18 10:34:28 +04:00
|
|
|
* credentials [1] OCTET STRING
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* TSPasswordCreds ::= SEQUENCE {
|
2011-08-29 09:55:16 +04:00
|
|
|
* domainName [0] OCTET STRING,
|
|
|
|
* userName [1] OCTET STRING,
|
|
|
|
* password [2] OCTET STRING
|
2011-07-18 10:34:28 +04:00
|
|
|
* }
|
|
|
|
*
|
|
|
|
* TSSmartCardCreds ::= SEQUENCE {
|
2011-08-29 09:55:16 +04:00
|
|
|
* pin [0] OCTET STRING,
|
|
|
|
* cspData [1] TSCspDataDetail,
|
|
|
|
* userHint [2] OCTET STRING OPTIONAL,
|
2011-07-18 10:34:28 +04:00
|
|
|
* domainHint [3] OCTET STRING OPTIONAL
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* TSCspDataDetail ::= SEQUENCE {
|
2011-08-29 09:55:16 +04:00
|
|
|
* keySpec [0] INTEGER,
|
|
|
|
* cardName [1] OCTET STRING OPTIONAL,
|
|
|
|
* readerName [2] OCTET STRING OPTIONAL,
|
2011-07-18 10:34:28 +04:00
|
|
|
* containerName [3] OCTET STRING OPTIONAL,
|
2011-08-29 09:55:16 +04:00
|
|
|
* cspName [4] OCTET STRING OPTIONAL
|
2011-07-18 10:34:28 +04:00
|
|
|
* }
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-07-01 02:51:46 +04:00
|
|
|
/**
|
2012-02-14 07:27:59 +04:00
|
|
|
* Initialize NTLMSSP authentication module (client).
|
2011-07-01 02:51:46 +04:00
|
|
|
* @param credssp
|
|
|
|
*/
|
|
|
|
|
2012-02-14 07:27:59 +04:00
|
|
|
int credssp_ntlmssp_client_init(rdpCredssp* credssp)
|
2011-07-01 02:51:46 +04:00
|
|
|
{
|
2011-09-29 09:03:07 +04:00
|
|
|
freerdp* instance;
|
|
|
|
NTLMSSP* ntlmssp = credssp->ntlmssp;
|
2012-02-18 02:12:21 +04:00
|
|
|
rdpSettings* settings = credssp->settings;
|
2011-09-29 09:03:07 +04:00
|
|
|
instance = (freerdp*) settings->instance;
|
|
|
|
|
2011-10-21 19:18:11 +04:00
|
|
|
if ((settings->password == NULL) || (settings->username == NULL))
|
2011-09-29 09:03:07 +04:00
|
|
|
{
|
2012-02-14 07:27:59 +04:00
|
|
|
if (instance->Authenticate)
|
2011-10-21 15:02:46 +04:00
|
|
|
{
|
|
|
|
boolean proceed = instance->Authenticate(instance,
|
|
|
|
&settings->username, &settings->password, &settings->domain);
|
2011-11-30 03:12:42 +04:00
|
|
|
if (!proceed)
|
2011-10-21 15:02:46 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2011-09-29 09:03:07 +04:00
|
|
|
}
|
2011-07-01 02:51:46 +04:00
|
|
|
|
2011-12-12 01:53:24 +04:00
|
|
|
if (settings->ntlm_version == 2)
|
|
|
|
ntlmssp->ntlm_v2 = 1;
|
|
|
|
|
2011-07-01 02:51:46 +04:00
|
|
|
ntlmssp_set_password(ntlmssp, settings->password);
|
|
|
|
ntlmssp_set_username(ntlmssp, settings->username);
|
|
|
|
|
2011-12-12 01:53:24 +04:00
|
|
|
if (ntlmssp->ntlm_v2)
|
|
|
|
{
|
|
|
|
ntlmssp_set_workstation(ntlmssp, "WORKSTATION");
|
|
|
|
}
|
|
|
|
|
2011-07-01 02:51:46 +04:00
|
|
|
if (settings->domain != NULL)
|
|
|
|
{
|
|
|
|
if (strlen(settings->domain) > 0)
|
|
|
|
ntlmssp_set_domain(ntlmssp, settings->domain);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ntlmssp_set_domain(ntlmssp, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
ntlmssp_generate_client_challenge(ntlmssp);
|
|
|
|
ntlmssp_generate_random_session_key(ntlmssp);
|
|
|
|
ntlmssp_generate_exported_session_key(ntlmssp);
|
2011-10-21 15:02:46 +04:00
|
|
|
|
|
|
|
return 1;
|
2011-07-01 02:51:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-14 07:27:59 +04:00
|
|
|
* Initialize NTLMSSP authentication module (server).
|
|
|
|
* @param credssp
|
|
|
|
*/
|
|
|
|
|
|
|
|
int credssp_ntlmssp_server_init(rdpCredssp* credssp)
|
|
|
|
{
|
2012-02-17 08:11:59 +04:00
|
|
|
NTLMSSP* ntlmssp = credssp->ntlmssp;
|
|
|
|
|
|
|
|
ntlmssp_generate_server_challenge(ntlmssp);
|
|
|
|
|
2012-02-14 07:27:59 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Authenticate with server using CredSSP (client).
|
2011-07-01 02:51:46 +04:00
|
|
|
* @param credssp
|
|
|
|
* @return 1 if authentication is successful
|
|
|
|
*/
|
|
|
|
|
2012-02-25 02:17:38 +04:00
|
|
|
#ifndef WITH_SSPI
|
|
|
|
|
2012-02-14 07:27:59 +04:00
|
|
|
int credssp_client_authenticate(rdpCredssp* credssp)
|
2011-07-01 02:51:46 +04:00
|
|
|
{
|
2011-09-29 09:03:07 +04:00
|
|
|
NTLMSSP* ntlmssp = credssp->ntlmssp;
|
2011-07-07 19:27:24 +04:00
|
|
|
STREAM* s = stream_new(0);
|
2011-07-01 02:51:46 +04:00
|
|
|
uint8* negoTokenBuffer = (uint8*) xmalloc(2048);
|
|
|
|
|
2012-02-14 07:27:59 +04:00
|
|
|
if (credssp_ntlmssp_client_init(credssp) == 0)
|
2011-10-21 15:02:46 +04:00
|
|
|
return 0;
|
2011-07-01 02:51:46 +04:00
|
|
|
|
|
|
|
/* NTLMSSP NEGOTIATE MESSAGE */
|
2012-01-31 16:34:26 +04:00
|
|
|
stream_attach(s, negoTokenBuffer, 2048);
|
2011-07-01 02:51:46 +04:00
|
|
|
ntlmssp_send(ntlmssp, s);
|
2012-01-31 16:34:26 +04:00
|
|
|
credssp->negoToken.data = stream_get_head(s);
|
|
|
|
credssp->negoToken.length = stream_get_length(s);
|
2011-07-01 02:51:46 +04:00
|
|
|
credssp_send(credssp, &credssp->negoToken, NULL, NULL);
|
|
|
|
|
|
|
|
/* NTLMSSP CHALLENGE MESSAGE */
|
|
|
|
if (credssp_recv(credssp, &credssp->negoToken, NULL, NULL) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2012-01-31 16:34:26 +04:00
|
|
|
stream_attach(s, credssp->negoToken.data, credssp->negoToken.length);
|
2011-07-01 02:51:46 +04:00
|
|
|
ntlmssp_recv(ntlmssp, s);
|
|
|
|
|
2011-07-07 19:49:57 +04:00
|
|
|
freerdp_blob_free(&credssp->negoToken);
|
2011-07-01 02:51:46 +04:00
|
|
|
|
|
|
|
/* NTLMSSP AUTHENTICATE MESSAGE */
|
2012-01-31 16:34:26 +04:00
|
|
|
stream_attach(s, negoTokenBuffer, 2048);
|
2011-07-01 02:51:46 +04:00
|
|
|
ntlmssp_send(ntlmssp, s);
|
|
|
|
|
|
|
|
/* The last NTLMSSP message is sent with the encrypted public key */
|
2012-01-31 16:34:26 +04:00
|
|
|
credssp->negoToken.data = stream_get_head(s);
|
|
|
|
credssp->negoToken.length = stream_get_length(s);
|
2011-07-01 02:51:46 +04:00
|
|
|
credssp_encrypt_public_key(credssp, &credssp->pubKeyAuth);
|
2011-07-19 00:16:37 +04:00
|
|
|
credssp_send(credssp, &credssp->negoToken, NULL, &credssp->pubKeyAuth);
|
2012-02-02 03:42:20 +04:00
|
|
|
freerdp_blob_free(&credssp->pubKeyAuth);
|
2011-07-01 02:51:46 +04:00
|
|
|
|
|
|
|
/* Encrypted Public Key +1 */
|
2011-07-19 00:16:37 +04:00
|
|
|
if (credssp_recv(credssp, &credssp->negoToken, NULL, &credssp->pubKeyAuth) < 0)
|
2011-07-01 02:51:46 +04:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (credssp_verify_public_key(credssp, &credssp->pubKeyAuth) == 0)
|
|
|
|
{
|
|
|
|
/* Failed to verify server public key echo */
|
|
|
|
return 0; /* DO NOT SEND CREDENTIALS! */
|
|
|
|
}
|
|
|
|
|
2011-07-07 19:49:57 +04:00
|
|
|
freerdp_blob_free(&credssp->negoToken);
|
|
|
|
freerdp_blob_free(&credssp->pubKeyAuth);
|
2011-07-01 02:51:46 +04:00
|
|
|
|
|
|
|
/* Send encrypted credentials */
|
|
|
|
credssp_encode_ts_credentials(credssp);
|
|
|
|
credssp_encrypt_ts_credentials(credssp, &credssp->authInfo);
|
2011-07-19 00:16:37 +04:00
|
|
|
credssp_send(credssp, NULL, &credssp->authInfo, NULL);
|
2012-02-02 03:42:20 +04:00
|
|
|
freerdp_blob_free(&credssp->authInfo);
|
2011-07-01 02:51:46 +04:00
|
|
|
|
|
|
|
xfree(s);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-02-25 02:17:38 +04:00
|
|
|
#else
|
|
|
|
|
|
|
|
#define NTLM_PACKAGE_NAME "NTLM"
|
|
|
|
|
|
|
|
int credssp_client_authenticate(rdpCredssp* credssp)
|
|
|
|
{
|
|
|
|
uint32 cbMaxLen;
|
|
|
|
uint32 fContextReq;
|
|
|
|
void* output_buffer;
|
|
|
|
CTXT_HANDLE context;
|
|
|
|
uint32 pfContextAttr;
|
|
|
|
SECURITY_STATUS status;
|
|
|
|
CRED_HANDLE credentials;
|
|
|
|
SEC_TIMESTAMP expiration;
|
|
|
|
SEC_PKG_INFO* pPackageInfo;
|
|
|
|
SEC_AUTH_IDENTITY identity;
|
|
|
|
SECURITY_FUNCTION_TABLE* table;
|
|
|
|
SEC_BUFFER* p_sec_buffer;
|
|
|
|
SEC_BUFFER output_sec_buffer;
|
|
|
|
SEC_BUFFER_DESC output_sec_buffer_desc;
|
|
|
|
SEC_BUFFER input_sec_buffer;
|
|
|
|
SEC_BUFFER_DESC input_sec_buffer_desc;
|
|
|
|
rdpSettings* settings = credssp->settings;
|
|
|
|
|
|
|
|
sspi_GlobalInit();
|
|
|
|
|
|
|
|
if (credssp_ntlmssp_client_init(credssp) == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
table = InitSecurityInterface();
|
|
|
|
|
|
|
|
status = QuerySecurityPackageInfo(NTLM_PACKAGE_NAME, &pPackageInfo);
|
|
|
|
|
|
|
|
if (status != SEC_E_OK)
|
|
|
|
{
|
|
|
|
printf("QuerySecurityPackageInfo status: 0x%08X\n", status);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
cbMaxLen = pPackageInfo->cbMaxToken;
|
|
|
|
|
|
|
|
identity.User = (uint16*) xstrdup(settings->username);
|
|
|
|
identity.UserLength = strlen(settings->username);
|
|
|
|
|
|
|
|
if (settings->domain)
|
|
|
|
{
|
|
|
|
identity.Domain = (uint16*) xstrdup(settings->domain);
|
|
|
|
identity.DomainLength = strlen(settings->domain);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
identity.Domain = (uint16*) NULL;
|
|
|
|
identity.DomainLength = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
identity.Password = (uint16*) xstrdup(settings->password);
|
|
|
|
identity.PasswordLength = strlen(settings->password);
|
|
|
|
|
|
|
|
identity.Flags = SEC_AUTH_IDENTITY_ANSI;
|
|
|
|
|
|
|
|
status = table->AcquireCredentialsHandle(NULL, NTLM_PACKAGE_NAME,
|
|
|
|
SECPKG_CRED_OUTBOUND, NULL, &identity, NULL, NULL, &credentials, &expiration);
|
|
|
|
|
|
|
|
if (status != SEC_E_OK)
|
|
|
|
{
|
|
|
|
printf("AcquireCredentialsHandle status: 0x%08X\n", status);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fContextReq = ISC_REQ_REPLAY_DETECT | ISC_REQ_SEQUENCE_DETECT | ISC_REQ_CONFIDENTIALITY | ISC_REQ_DELEGATE;
|
|
|
|
|
|
|
|
output_buffer = xmalloc(cbMaxLen);
|
|
|
|
|
|
|
|
printf("First Call to InitializeSecurityContext()\n");
|
|
|
|
|
|
|
|
output_sec_buffer_desc.ulVersion = 0;
|
|
|
|
output_sec_buffer_desc.cBuffers = 1;
|
|
|
|
output_sec_buffer_desc.pBuffers = &output_sec_buffer;
|
|
|
|
|
|
|
|
output_sec_buffer.cbBuffer = cbMaxLen;
|
|
|
|
output_sec_buffer.BufferType = SECBUFFER_TOKEN;
|
|
|
|
output_sec_buffer.pvBuffer = output_buffer;
|
|
|
|
|
|
|
|
status = table->InitializeSecurityContext(&credentials, NULL, NULL, fContextReq, 0, 0, NULL, 0,
|
|
|
|
&context, &output_sec_buffer_desc, &pfContextAttr, &expiration);
|
|
|
|
|
|
|
|
if (status != SEC_I_CONTINUE_NEEDED)
|
|
|
|
{
|
|
|
|
printf("InitializeSecurityContext status: 0x%08X\n", status);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_sec_buffer = &output_sec_buffer_desc.pBuffers[0];
|
|
|
|
|
|
|
|
credssp->negoToken.data = p_sec_buffer->pvBuffer;
|
|
|
|
credssp->negoToken.length = p_sec_buffer->cbBuffer;
|
|
|
|
|
|
|
|
/* NTLMSSP NEGOTIATE MESSAGE */
|
|
|
|
credssp_send(credssp, &credssp->negoToken, NULL, NULL);
|
|
|
|
|
|
|
|
printf("sent NTLMSSP_NEGOTIATE_MESSAGE\n");
|
|
|
|
|
|
|
|
/* NTLMSSP CHALLENGE MESSAGE */
|
|
|
|
if (credssp_recv(credssp, &credssp->negoToken, NULL, NULL) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
printf("received NTLM CHALLENGE MESSAGE\n");
|
|
|
|
|
|
|
|
input_sec_buffer_desc.ulVersion = 0;
|
|
|
|
input_sec_buffer_desc.cBuffers = 1;
|
|
|
|
input_sec_buffer_desc.pBuffers = &input_sec_buffer;
|
|
|
|
|
|
|
|
input_sec_buffer.cbBuffer = credssp->negoToken.length;
|
|
|
|
input_sec_buffer.BufferType = SECBUFFER_TOKEN;
|
|
|
|
input_sec_buffer.pvBuffer = credssp->negoToken.data;
|
|
|
|
|
|
|
|
output_sec_buffer_desc.ulVersion = 0;
|
|
|
|
output_sec_buffer_desc.cBuffers = 1;
|
|
|
|
output_sec_buffer_desc.pBuffers = &output_sec_buffer;
|
|
|
|
|
|
|
|
output_sec_buffer.cbBuffer = cbMaxLen;
|
|
|
|
output_sec_buffer.BufferType = SECBUFFER_TOKEN;
|
|
|
|
output_sec_buffer.pvBuffer = output_buffer;
|
|
|
|
|
|
|
|
printf("Second Call to InitializeSecurityContext()\n");
|
|
|
|
|
|
|
|
status = table->InitializeSecurityContext(&credentials, &context, NULL, fContextReq, 0, 0,
|
|
|
|
&input_sec_buffer_desc, 0, &context, &output_sec_buffer_desc, &pfContextAttr, &expiration);
|
|
|
|
|
|
|
|
if (status != SEC_I_CONTINUE_NEEDED)
|
|
|
|
{
|
|
|
|
printf("InitializeSecurityContext status: 0x%08X\n", status);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
FreeCredentialsHandle(&credentials);
|
|
|
|
FreeContextBuffer(pPackageInfo);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-02-14 07:27:59 +04:00
|
|
|
/**
|
|
|
|
* Authenticate with client using CredSSP (server).
|
|
|
|
* @param credssp
|
|
|
|
* @return 1 if authentication is successful
|
|
|
|
*/
|
|
|
|
|
|
|
|
int credssp_server_authenticate(rdpCredssp* credssp)
|
|
|
|
{
|
2012-02-17 08:11:59 +04:00
|
|
|
STREAM* s = stream_new(0);
|
|
|
|
NTLMSSP* ntlmssp = credssp->ntlmssp;
|
|
|
|
uint8* negoTokenBuffer = (uint8*) xmalloc(2048);
|
|
|
|
|
2012-02-14 07:27:59 +04:00
|
|
|
if (credssp_ntlmssp_server_init(credssp) == 0)
|
|
|
|
return 0;
|
|
|
|
|
2012-02-17 08:11:59 +04:00
|
|
|
/* NTLMSSP NEGOTIATE MESSAGE */
|
|
|
|
if (credssp_recv(credssp, &credssp->negoToken, NULL, NULL) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
stream_attach(s, credssp->negoToken.data, credssp->negoToken.length);
|
|
|
|
ntlmssp_recv(ntlmssp, s);
|
|
|
|
|
|
|
|
freerdp_blob_free(&credssp->negoToken);
|
|
|
|
|
|
|
|
/* NTLMSSP CHALLENGE MESSAGE */
|
|
|
|
stream_attach(s, negoTokenBuffer, 2048);
|
|
|
|
ntlmssp_send(ntlmssp, s);
|
|
|
|
credssp->negoToken.data = stream_get_head(s);
|
|
|
|
credssp->negoToken.length = stream_get_length(s);
|
|
|
|
credssp_send(credssp, &credssp->negoToken, NULL, NULL);
|
|
|
|
|
|
|
|
/* NTLMSSP AUTHENTICATE MESSAGE */
|
|
|
|
if (credssp_recv(credssp, &credssp->negoToken, NULL, &credssp->pubKeyAuth) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
stream_attach(s, credssp->negoToken.data, credssp->negoToken.length);
|
|
|
|
ntlmssp_recv(ntlmssp, s);
|
|
|
|
|
2012-02-14 07:27:59 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Authenticate using CredSSP.
|
|
|
|
* @param credssp
|
|
|
|
* @return 1 if authentication is successful
|
|
|
|
*/
|
|
|
|
|
|
|
|
int credssp_authenticate(rdpCredssp* credssp)
|
|
|
|
{
|
|
|
|
if (credssp->server)
|
|
|
|
return credssp_server_authenticate(credssp);
|
|
|
|
else
|
|
|
|
return credssp_client_authenticate(credssp);
|
|
|
|
}
|
|
|
|
|
2011-07-01 02:51:46 +04:00
|
|
|
/**
|
|
|
|
* Encrypt TLS public key using CredSSP.
|
|
|
|
* @param credssp
|
|
|
|
* @param s
|
|
|
|
*/
|
|
|
|
|
2011-08-16 01:05:48 +04:00
|
|
|
void credssp_encrypt_public_key(rdpCredssp* credssp, rdpBlob* d)
|
2011-07-01 02:51:46 +04:00
|
|
|
{
|
2011-09-29 09:03:07 +04:00
|
|
|
uint8* p;
|
2012-02-12 21:46:53 +04:00
|
|
|
rdpTls* tls;
|
2011-07-01 02:51:46 +04:00
|
|
|
uint8 signature[16];
|
2011-08-16 01:05:48 +04:00
|
|
|
rdpBlob encrypted_public_key;
|
2011-07-01 02:51:46 +04:00
|
|
|
NTLMSSP *ntlmssp = credssp->ntlmssp;
|
2012-02-18 02:12:21 +04:00
|
|
|
tls = credssp->tls;
|
2011-07-01 02:51:46 +04:00
|
|
|
|
2012-02-12 21:46:53 +04:00
|
|
|
freerdp_blob_alloc(d, tls->public_key.length + 16);
|
|
|
|
ntlmssp_encrypt_message(ntlmssp, &tls->public_key, &encrypted_public_key, signature);
|
2011-07-01 02:51:46 +04:00
|
|
|
|
|
|
|
#ifdef WITH_DEBUG_NLA
|
2012-02-12 21:46:53 +04:00
|
|
|
printf("Public Key (length = %d)\n", tls->public_key.length);
|
|
|
|
freerdp_hexdump(tls->public_key.data, tls->public_key.length);
|
2011-07-01 02:51:46 +04:00
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
printf("Encrypted Public Key (length = %d)\n", encrypted_public_key.length);
|
2011-07-07 19:27:24 +04:00
|
|
|
freerdp_hexdump(encrypted_public_key.data, encrypted_public_key.length);
|
2011-07-01 02:51:46 +04:00
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
printf("Signature\n");
|
2011-07-07 19:27:24 +04:00
|
|
|
freerdp_hexdump(signature, 16);
|
2011-07-01 02:51:46 +04:00
|
|
|
printf("\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
p = (uint8*) d->data;
|
|
|
|
memcpy(p, signature, 16); /* Message Signature */
|
|
|
|
memcpy(&p[16], encrypted_public_key.data, encrypted_public_key.length); /* Encrypted Public Key */
|
|
|
|
|
2011-07-07 19:49:57 +04:00
|
|
|
freerdp_blob_free(&encrypted_public_key);
|
2011-07-01 02:51:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verify TLS public key using CredSSP.
|
|
|
|
* @param credssp
|
|
|
|
* @param s
|
|
|
|
* @return 1 if verification is successful, 0 otherwise
|
|
|
|
*/
|
|
|
|
|
2011-08-16 01:05:48 +04:00
|
|
|
int credssp_verify_public_key(rdpCredssp* credssp, rdpBlob* d)
|
2011-07-01 02:51:46 +04:00
|
|
|
{
|
|
|
|
uint8 *p1, *p2;
|
2011-09-29 09:03:07 +04:00
|
|
|
uint8* signature;
|
2011-08-16 01:05:48 +04:00
|
|
|
rdpBlob public_key;
|
|
|
|
rdpBlob encrypted_public_key;
|
2012-02-18 02:12:21 +04:00
|
|
|
rdpTls* tls = credssp->tls;
|
2011-07-01 02:51:46 +04:00
|
|
|
|
|
|
|
signature = d->data;
|
|
|
|
encrypted_public_key.data = (void*) (signature + 16);
|
|
|
|
encrypted_public_key.length = d->length - 16;
|
|
|
|
|
|
|
|
ntlmssp_decrypt_message(credssp->ntlmssp, &encrypted_public_key, &public_key, signature);
|
|
|
|
|
2012-02-12 21:46:53 +04:00
|
|
|
p1 = (uint8*) tls->public_key.data;
|
2011-07-01 02:51:46 +04:00
|
|
|
p2 = (uint8*) public_key.data;
|
|
|
|
|
|
|
|
p2[0]--;
|
|
|
|
|
|
|
|
if (memcmp(p1, p2, public_key.length) != 0)
|
|
|
|
{
|
|
|
|
printf("Could not verify server's public key echo\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
p2[0]++;
|
2011-07-07 19:49:57 +04:00
|
|
|
freerdp_blob_free(&public_key);
|
2011-07-01 02:51:46 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encrypt and sign TSCredentials structure.
|
|
|
|
* @param credssp
|
|
|
|
* @param s
|
|
|
|
*/
|
|
|
|
|
2011-08-16 01:05:48 +04:00
|
|
|
void credssp_encrypt_ts_credentials(rdpCredssp* credssp, rdpBlob* d)
|
2011-07-01 02:51:46 +04:00
|
|
|
{
|
2011-09-29 09:03:07 +04:00
|
|
|
uint8* p;
|
2011-07-01 02:51:46 +04:00
|
|
|
uint8 signature[16];
|
2011-08-16 01:05:48 +04:00
|
|
|
rdpBlob encrypted_ts_credentials;
|
2011-09-29 09:03:07 +04:00
|
|
|
NTLMSSP* ntlmssp = credssp->ntlmssp;
|
2011-07-01 02:51:46 +04:00
|
|
|
|
2011-07-07 19:49:57 +04:00
|
|
|
freerdp_blob_alloc(d, credssp->ts_credentials.length + 16);
|
2011-07-01 02:51:46 +04:00
|
|
|
ntlmssp_encrypt_message(ntlmssp, &credssp->ts_credentials, &encrypted_ts_credentials, signature);
|
|
|
|
|
|
|
|
#ifdef WITH_DEBUG_NLA
|
|
|
|
printf("TSCredentials (length = %d)\n", credssp->ts_credentials.length);
|
2011-07-07 19:27:24 +04:00
|
|
|
freerdp_hexdump(credssp->ts_credentials.data, credssp->ts_credentials.length);
|
2011-07-01 02:51:46 +04:00
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
printf("Encrypted TSCredentials (length = %d)\n", encrypted_ts_credentials.length);
|
2011-07-07 19:27:24 +04:00
|
|
|
freerdp_hexdump(encrypted_ts_credentials.data, encrypted_ts_credentials.length);
|
2011-07-01 02:51:46 +04:00
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
printf("Signature\n");
|
2011-07-07 19:27:24 +04:00
|
|
|
freerdp_hexdump(signature, 16);
|
2011-07-01 02:51:46 +04:00
|
|
|
printf("\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
p = (uint8*) d->data;
|
|
|
|
memcpy(p, signature, 16); /* Message Signature */
|
|
|
|
memcpy(&p[16], encrypted_ts_credentials.data, encrypted_ts_credentials.length); /* Encrypted TSCredentials */
|
|
|
|
|
2011-07-07 19:49:57 +04:00
|
|
|
freerdp_blob_free(&encrypted_ts_credentials);
|
2011-07-01 02:51:46 +04:00
|
|
|
}
|
|
|
|
|
2011-07-19 02:43:23 +04:00
|
|
|
int credssp_skip_ts_password_creds(rdpCredssp* credssp)
|
|
|
|
{
|
|
|
|
int length;
|
|
|
|
int ts_password_creds_length = 0;
|
2011-07-01 02:51:46 +04:00
|
|
|
|
2011-07-19 02:43:23 +04:00
|
|
|
length = ber_skip_octet_string(credssp->ntlmssp->domain.length);
|
|
|
|
length += ber_skip_contextual_tag(length);
|
|
|
|
ts_password_creds_length += length;
|
|
|
|
|
|
|
|
length = ber_skip_octet_string(credssp->ntlmssp->username.length);
|
|
|
|
length += ber_skip_contextual_tag(length);
|
|
|
|
ts_password_creds_length += length;
|
|
|
|
|
|
|
|
length = ber_skip_octet_string(credssp->ntlmssp->password.length);
|
|
|
|
length += ber_skip_contextual_tag(length);
|
|
|
|
ts_password_creds_length += length;
|
|
|
|
|
|
|
|
length = ber_skip_sequence(ts_password_creds_length);
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
void credssp_write_ts_password_creds(rdpCredssp* credssp, STREAM* s)
|
2011-07-01 02:51:46 +04:00
|
|
|
{
|
2011-07-19 02:43:23 +04:00
|
|
|
int length;
|
2011-07-01 02:51:46 +04:00
|
|
|
|
2011-07-19 02:43:23 +04:00
|
|
|
length = credssp_skip_ts_password_creds(credssp);
|
2011-07-01 02:51:46 +04:00
|
|
|
|
2011-07-19 02:43:23 +04:00
|
|
|
/* TSPasswordCreds (SEQUENCE) */
|
|
|
|
length = ber_get_content_length(length);
|
|
|
|
ber_write_sequence_tag(s, length);
|
2011-07-01 02:51:46 +04:00
|
|
|
|
2011-07-19 02:43:23 +04:00
|
|
|
/* [0] domainName (OCTET STRING) */
|
2011-11-19 21:19:16 +04:00
|
|
|
ber_write_contextual_tag(s, 0, credssp->ntlmssp->domain.length + 2, true);
|
2011-07-19 02:43:23 +04:00
|
|
|
ber_write_octet_string(s, credssp->ntlmssp->domain.data, credssp->ntlmssp->domain.length);
|
2011-07-01 02:51:46 +04:00
|
|
|
|
2011-07-19 02:43:23 +04:00
|
|
|
/* [1] userName (OCTET STRING) */
|
2011-11-19 21:19:16 +04:00
|
|
|
ber_write_contextual_tag(s, 1, credssp->ntlmssp->username.length + 2, true);
|
2011-07-19 02:43:23 +04:00
|
|
|
ber_write_octet_string(s, credssp->ntlmssp->username.data, credssp->ntlmssp->username.length);
|
2011-07-01 02:51:46 +04:00
|
|
|
|
2011-07-19 02:43:23 +04:00
|
|
|
/* [2] password (OCTET STRING) */
|
2011-11-19 21:19:16 +04:00
|
|
|
ber_write_contextual_tag(s, 2, credssp->ntlmssp->password.length + 2, true);
|
2011-07-19 02:43:23 +04:00
|
|
|
ber_write_octet_string(s, credssp->ntlmssp->password.data, credssp->ntlmssp->password.length);
|
|
|
|
}
|
2011-07-01 02:51:46 +04:00
|
|
|
|
2011-07-19 02:43:23 +04:00
|
|
|
int credssp_skip_ts_credentials(rdpCredssp* credssp)
|
|
|
|
{
|
|
|
|
int length;
|
|
|
|
int ts_password_creds_length;
|
|
|
|
int ts_credentials_length = 0;
|
2011-07-01 02:51:46 +04:00
|
|
|
|
2011-07-19 02:43:23 +04:00
|
|
|
length = ber_skip_integer(0);
|
|
|
|
length += ber_skip_contextual_tag(length);
|
|
|
|
ts_credentials_length += length;
|
2011-07-01 02:51:46 +04:00
|
|
|
|
2011-07-19 02:43:23 +04:00
|
|
|
ts_password_creds_length = credssp_skip_ts_password_creds(credssp);
|
|
|
|
length = ber_skip_octet_string(ts_password_creds_length);
|
|
|
|
length += ber_skip_contextual_tag(length);
|
|
|
|
ts_credentials_length += length;
|
|
|
|
|
|
|
|
length = ber_skip_sequence(ts_credentials_length);
|
2011-07-01 02:51:46 +04:00
|
|
|
|
2011-07-19 02:43:23 +04:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
void credssp_write_ts_credentials(rdpCredssp* credssp, STREAM* s)
|
|
|
|
{
|
|
|
|
int length;
|
|
|
|
int ts_password_creds_length;
|
2011-07-01 02:51:46 +04:00
|
|
|
|
2011-07-19 02:43:23 +04:00
|
|
|
length = credssp_skip_ts_credentials(credssp);
|
|
|
|
ts_password_creds_length = credssp_skip_ts_password_creds(credssp);
|
2011-07-01 02:51:46 +04:00
|
|
|
|
2011-07-19 02:43:23 +04:00
|
|
|
/* TSCredentials (SEQUENCE) */
|
|
|
|
length = ber_get_content_length(length);
|
|
|
|
length -= ber_write_sequence_tag(s, length);
|
2011-07-01 02:51:46 +04:00
|
|
|
|
2011-07-19 02:43:23 +04:00
|
|
|
/* [0] credType (INTEGER) */
|
2011-11-19 21:19:16 +04:00
|
|
|
length -= ber_write_contextual_tag(s, 0, 3, true);
|
2011-07-19 02:43:23 +04:00
|
|
|
length -= ber_write_integer(s, 1);
|
|
|
|
|
|
|
|
/* [1] credentials (OCTET STRING) */
|
|
|
|
length -= 1;
|
2011-11-19 21:19:16 +04:00
|
|
|
length -= ber_write_contextual_tag(s, 1, length, true);
|
2011-07-19 02:43:23 +04:00
|
|
|
length -= ber_write_octet_string_tag(s, ts_password_creds_length);
|
|
|
|
|
|
|
|
credssp_write_ts_password_creds(credssp, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode TSCredentials structure.
|
|
|
|
* @param credssp
|
|
|
|
*/
|
|
|
|
|
|
|
|
void credssp_encode_ts_credentials(rdpCredssp* credssp)
|
|
|
|
{
|
|
|
|
STREAM* s;
|
|
|
|
int length;
|
|
|
|
|
|
|
|
s = stream_new(0);
|
|
|
|
length = credssp_skip_ts_credentials(credssp);
|
|
|
|
freerdp_blob_alloc(&credssp->ts_credentials, length);
|
2012-02-02 06:11:46 +04:00
|
|
|
stream_attach(s, credssp->ts_credentials.data, length);
|
2011-07-01 02:51:46 +04:00
|
|
|
|
2011-07-19 02:43:23 +04:00
|
|
|
credssp_write_ts_credentials(credssp, s);
|
2012-02-02 06:11:46 +04:00
|
|
|
stream_detach(s);
|
|
|
|
stream_free(s);
|
2011-07-01 02:51:46 +04:00
|
|
|
}
|
|
|
|
|
2011-07-19 00:16:37 +04:00
|
|
|
int credssp_skip_nego_token(int length)
|
2011-07-01 02:51:46 +04:00
|
|
|
{
|
2011-07-19 00:16:37 +04:00
|
|
|
length = ber_skip_octet_string(length);
|
|
|
|
length += ber_skip_contextual_tag(length);
|
|
|
|
return length;
|
|
|
|
}
|
2011-07-01 02:51:46 +04:00
|
|
|
|
2011-07-19 00:16:37 +04:00
|
|
|
int credssp_skip_nego_tokens(int length)
|
|
|
|
{
|
|
|
|
length = credssp_skip_nego_token(length);
|
|
|
|
length += ber_skip_sequence_tag(length);
|
|
|
|
length += ber_skip_sequence_tag(length);
|
|
|
|
length += ber_skip_contextual_tag(length);
|
|
|
|
return length;
|
2011-07-01 02:51:46 +04:00
|
|
|
}
|
2011-07-18 22:56:08 +04:00
|
|
|
|
2011-07-19 00:16:37 +04:00
|
|
|
int credssp_skip_pub_key_auth(int length)
|
2011-07-18 22:56:08 +04:00
|
|
|
{
|
2011-07-19 00:16:37 +04:00
|
|
|
length = ber_skip_octet_string(length);
|
|
|
|
length += ber_skip_contextual_tag(length);
|
|
|
|
return length;
|
2011-07-18 22:56:08 +04:00
|
|
|
}
|
|
|
|
|
2011-07-19 00:16:37 +04:00
|
|
|
int credssp_skip_auth_info(int length)
|
2011-07-18 22:56:08 +04:00
|
|
|
{
|
2011-07-19 00:16:37 +04:00
|
|
|
length = ber_skip_octet_string(length);
|
2011-07-18 22:56:08 +04:00
|
|
|
length += ber_skip_contextual_tag(length);
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
int credssp_skip_ts_request(int length)
|
|
|
|
{
|
|
|
|
length += ber_skip_integer(2);
|
|
|
|
length += ber_skip_contextual_tag(3);
|
|
|
|
length += ber_skip_sequence_tag(length);
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2011-07-19 00:16:37 +04:00
|
|
|
/**
|
|
|
|
* Send CredSSP message.
|
|
|
|
* @param credssp
|
|
|
|
* @param negoToken
|
|
|
|
* @param authInfo
|
2011-07-19 01:02:06 +04:00
|
|
|
* @param pubKeyAuth
|
2011-07-19 00:16:37 +04:00
|
|
|
*/
|
|
|
|
|
2011-08-16 01:05:48 +04:00
|
|
|
void credssp_send(rdpCredssp* credssp, rdpBlob* negoToken, rdpBlob* authInfo, rdpBlob* pubKeyAuth)
|
2011-07-18 10:34:28 +04:00
|
|
|
{
|
|
|
|
STREAM* s;
|
|
|
|
int length;
|
2011-07-19 00:16:37 +04:00
|
|
|
int ts_request_length;
|
|
|
|
int nego_tokens_length;
|
|
|
|
int pub_key_auth_length;
|
|
|
|
int auth_info_length;
|
2011-07-18 10:34:28 +04:00
|
|
|
|
2011-07-19 00:16:37 +04:00
|
|
|
nego_tokens_length = (negoToken != NULL) ? credssp_skip_nego_tokens(negoToken->length) : 0;
|
|
|
|
pub_key_auth_length = (pubKeyAuth != NULL) ? credssp_skip_pub_key_auth(pubKeyAuth->length) : 0;
|
|
|
|
auth_info_length = (authInfo != NULL) ? credssp_skip_auth_info(authInfo->length) : 0;
|
2011-07-18 10:34:28 +04:00
|
|
|
|
2011-07-19 00:16:37 +04:00
|
|
|
length = nego_tokens_length + pub_key_auth_length + auth_info_length;
|
|
|
|
ts_request_length = credssp_skip_ts_request(length);
|
2011-07-18 22:56:08 +04:00
|
|
|
|
2011-07-19 00:16:37 +04:00
|
|
|
s = stream_new(ts_request_length);
|
2011-07-18 22:56:08 +04:00
|
|
|
|
2011-07-18 10:34:28 +04:00
|
|
|
/* TSRequest */
|
2011-07-19 00:16:37 +04:00
|
|
|
length = ber_get_content_length(ts_request_length);
|
|
|
|
ber_write_sequence_tag(s, length); /* SEQUENCE */
|
2011-11-19 21:19:16 +04:00
|
|
|
ber_write_contextual_tag(s, 0, 3, true); /* [0] version */
|
2011-07-18 10:34:28 +04:00
|
|
|
ber_write_integer(s, 2); /* INTEGER */
|
|
|
|
|
2011-07-19 00:16:37 +04:00
|
|
|
/* [1] negoTokens (NegoData) */
|
|
|
|
if (nego_tokens_length > 0)
|
|
|
|
{
|
|
|
|
length = ber_get_content_length(nego_tokens_length);
|
2011-11-19 21:19:16 +04:00
|
|
|
length -= ber_write_contextual_tag(s, 1, length, true); /* NegoData */
|
2011-07-19 01:02:06 +04:00
|
|
|
length -= ber_write_sequence_tag(s, length); /* SEQUENCE OF NegoDataItem */
|
|
|
|
length -= ber_write_sequence_tag(s, length); /* NegoDataItem */
|
2011-11-19 21:19:16 +04:00
|
|
|
length -= ber_write_contextual_tag(s, 0, length, true); /* [0] negoToken */
|
2011-07-19 00:16:37 +04:00
|
|
|
ber_write_octet_string(s, negoToken->data, length); /* OCTET STRING */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [2] authInfo (OCTET STRING) */
|
|
|
|
if (auth_info_length > 0)
|
|
|
|
{
|
|
|
|
length = ber_get_content_length(auth_info_length);
|
2011-11-19 21:19:16 +04:00
|
|
|
length -= ber_write_contextual_tag(s, 2, length, true);
|
2011-07-19 00:16:37 +04:00
|
|
|
ber_write_octet_string(s, authInfo->data, authInfo->length);
|
|
|
|
}
|
2011-07-18 10:34:28 +04:00
|
|
|
|
2011-07-19 00:16:37 +04:00
|
|
|
/* [3] pubKeyAuth (OCTET STRING) */
|
|
|
|
if (pub_key_auth_length > 0)
|
|
|
|
{
|
|
|
|
length = ber_get_content_length(pub_key_auth_length);
|
2011-11-19 21:19:16 +04:00
|
|
|
length -= ber_write_contextual_tag(s, 3, length, true);
|
2011-07-19 00:16:37 +04:00
|
|
|
ber_write_octet_string(s, pubKeyAuth->data, length);
|
|
|
|
}
|
2011-07-18 10:34:28 +04:00
|
|
|
|
2012-02-19 21:50:30 +04:00
|
|
|
tls_write(credssp->tls, s->data, stream_get_length(s));
|
2012-02-02 03:42:20 +04:00
|
|
|
stream_free(s);
|
2011-07-18 10:34:28 +04:00
|
|
|
}
|
2011-07-01 02:51:46 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Receive CredSSP message.
|
|
|
|
* @param credssp
|
|
|
|
* @param negoToken
|
|
|
|
* @param authInfo
|
2011-07-19 01:02:06 +04:00
|
|
|
* @param pubKeyAuth
|
2011-07-01 02:51:46 +04:00
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
|
2011-08-16 01:05:48 +04:00
|
|
|
int credssp_recv(rdpCredssp* credssp, rdpBlob* negoToken, rdpBlob* authInfo, rdpBlob* pubKeyAuth)
|
2011-07-01 02:51:46 +04:00
|
|
|
{
|
2011-07-10 23:34:43 +04:00
|
|
|
STREAM* s;
|
2011-07-19 01:02:06 +04:00
|
|
|
int length;
|
2011-07-10 23:34:43 +04:00
|
|
|
int status;
|
2011-07-19 01:02:06 +04:00
|
|
|
uint32 version;
|
2011-07-01 02:51:46 +04:00
|
|
|
|
2012-02-19 21:50:30 +04:00
|
|
|
s = stream_new(2048);
|
|
|
|
status = tls_read(credssp->tls, s->data, stream_get_left(s));
|
2011-07-01 02:51:46 +04:00
|
|
|
|
2011-07-10 23:34:43 +04:00
|
|
|
if (status < 0)
|
2011-07-01 02:51:46 +04:00
|
|
|
return -1;
|
|
|
|
|
2011-07-19 01:02:06 +04:00
|
|
|
/* TSRequest */
|
|
|
|
ber_read_sequence_tag(s, &length);
|
2011-11-19 21:19:16 +04:00
|
|
|
ber_read_contextual_tag(s, 0, &length, true);
|
2011-07-19 01:02:06 +04:00
|
|
|
ber_read_integer(s, &version);
|
2011-07-01 02:51:46 +04:00
|
|
|
|
2011-07-19 01:02:06 +04:00
|
|
|
/* [1] negoTokens (NegoData) */
|
2011-11-19 21:19:16 +04:00
|
|
|
if (ber_read_contextual_tag(s, 1, &length, true) != false)
|
2011-07-01 02:51:46 +04:00
|
|
|
{
|
2011-07-19 01:02:06 +04:00
|
|
|
ber_read_sequence_tag(s, &length); /* SEQUENCE OF NegoDataItem */
|
|
|
|
ber_read_sequence_tag(s, &length); /* NegoDataItem */
|
2011-11-19 21:19:16 +04:00
|
|
|
ber_read_contextual_tag(s, 0, &length, true); /* [0] negoToken */
|
2011-07-19 01:02:06 +04:00
|
|
|
ber_read_octet_string(s, &length); /* OCTET STRING */
|
|
|
|
freerdp_blob_alloc(negoToken, length);
|
|
|
|
stream_read(s, negoToken->data, length);
|
|
|
|
}
|
2011-07-01 02:51:46 +04:00
|
|
|
|
2011-07-19 01:02:06 +04:00
|
|
|
/* [2] authInfo (OCTET STRING) */
|
2011-11-19 21:19:16 +04:00
|
|
|
if (ber_read_contextual_tag(s, 2, &length, true) != false)
|
2011-07-19 01:02:06 +04:00
|
|
|
{
|
|
|
|
ber_read_octet_string(s, &length); /* OCTET STRING */
|
|
|
|
freerdp_blob_alloc(authInfo, length);
|
|
|
|
stream_read(s, authInfo->data, length);
|
2011-07-01 02:51:46 +04:00
|
|
|
}
|
2011-07-19 01:02:06 +04:00
|
|
|
|
|
|
|
/* [3] pubKeyAuth (OCTET STRING) */
|
2011-11-19 21:19:16 +04:00
|
|
|
if (ber_read_contextual_tag(s, 3, &length, true) != false)
|
2011-07-01 02:51:46 +04:00
|
|
|
{
|
2011-07-19 01:02:06 +04:00
|
|
|
ber_read_octet_string(s, &length); /* OCTET STRING */
|
|
|
|
freerdp_blob_alloc(pubKeyAuth, length);
|
|
|
|
stream_read(s, pubKeyAuth->data, length);
|
2011-07-01 02:51:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encrypt the given plain text using RC4 and the given key.
|
|
|
|
* @param key RC4 key
|
|
|
|
* @param length text length
|
|
|
|
* @param plaintext plain text
|
|
|
|
* @param ciphertext cipher text
|
|
|
|
*/
|
|
|
|
|
|
|
|
void credssp_rc4k(uint8* key, int length, uint8* plaintext, uint8* ciphertext)
|
|
|
|
{
|
|
|
|
CryptoRc4 rc4;
|
|
|
|
|
|
|
|
/* Initialize RC4 cipher with key */
|
|
|
|
rc4 = crypto_rc4_init((void*) key, 16);
|
|
|
|
|
|
|
|
/* Encrypt plaintext with key */
|
|
|
|
crypto_rc4(rc4, length, (void*) plaintext, (void*) ciphertext);
|
|
|
|
|
|
|
|
/* Free RC4 Cipher */
|
|
|
|
crypto_rc4_free(rc4);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get current time, in tenths of microseconds since midnight of January 1, 1601.
|
|
|
|
* @param[out] timestamp 64-bit little-endian timestamp
|
|
|
|
*/
|
|
|
|
|
|
|
|
void credssp_current_time(uint8* timestamp)
|
|
|
|
{
|
|
|
|
uint64 time64;
|
|
|
|
|
|
|
|
/* Timestamp (8 bytes), represented as the number of tenths of microseconds since midnight of January 1, 1601 */
|
|
|
|
time64 = time(NULL) + 11644473600LL; /* Seconds since January 1, 1601 */
|
|
|
|
time64 *= 10000000; /* Convert timestamp to tenths of a microsecond */
|
|
|
|
|
|
|
|
memcpy(timestamp, &time64, 8); /* Copy into timestamp in little-endian */
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create new CredSSP state machine.
|
2011-07-06 07:18:00 +04:00
|
|
|
* @param transport
|
2011-07-01 02:51:46 +04:00
|
|
|
* @return new CredSSP state machine.
|
|
|
|
*/
|
|
|
|
|
2012-02-18 02:12:21 +04:00
|
|
|
rdpCredssp* credssp_new(freerdp* instance, rdpTls* tls, rdpSettings* settings)
|
2011-07-01 02:51:46 +04:00
|
|
|
{
|
2012-02-14 07:27:59 +04:00
|
|
|
rdpCredssp* credssp;
|
2011-07-07 19:49:57 +04:00
|
|
|
|
2012-02-14 07:27:59 +04:00
|
|
|
credssp = (rdpCredssp*) xzalloc(sizeof(rdpCredssp));
|
2011-07-01 02:51:46 +04:00
|
|
|
|
2012-02-14 07:27:59 +04:00
|
|
|
if (credssp != NULL)
|
2011-07-01 02:51:46 +04:00
|
|
|
{
|
2012-02-18 02:12:21 +04:00
|
|
|
credssp->instance = instance;
|
|
|
|
credssp->settings = settings;
|
|
|
|
credssp->server = settings->server_mode;
|
|
|
|
credssp->tls = tls;
|
2012-02-14 07:27:59 +04:00
|
|
|
|
2012-02-18 02:12:21 +04:00
|
|
|
credssp->send_seq_num = 0;
|
2012-02-14 07:27:59 +04:00
|
|
|
|
|
|
|
if (credssp->server)
|
|
|
|
credssp->ntlmssp = ntlmssp_server_new();
|
|
|
|
else
|
|
|
|
credssp->ntlmssp = ntlmssp_client_new();
|
2011-07-01 02:51:46 +04:00
|
|
|
}
|
2011-07-07 19:49:57 +04:00
|
|
|
|
2012-02-14 07:27:59 +04:00
|
|
|
return credssp;
|
2011-07-01 02:51:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free CredSSP state machine.
|
|
|
|
* @param credssp
|
|
|
|
*/
|
|
|
|
|
2011-07-07 23:35:09 +04:00
|
|
|
void credssp_free(rdpCredssp* credssp)
|
2011-07-01 02:51:46 +04:00
|
|
|
{
|
|
|
|
if (credssp != NULL)
|
|
|
|
{
|
2011-07-07 19:49:57 +04:00
|
|
|
freerdp_blob_free(&credssp->ts_credentials);
|
2011-07-01 02:51:46 +04:00
|
|
|
|
|
|
|
ntlmssp_free(credssp->ntlmssp);
|
|
|
|
xfree(credssp);
|
|
|
|
}
|
|
|
|
}
|
2012-02-23 08:41:22 +04:00
|
|
|
|
|
|
|
/* SSPI */
|
|
|
|
|
|
|
|
const SECURITY_FUNCTION_TABLE CREDSSP_SECURITY_FUNCTION_TABLE =
|
|
|
|
{
|
|
|
|
1, /* dwVersion */
|
|
|
|
NULL, /* EnumerateSecurityPackages */
|
|
|
|
NULL, /* Reserved1 */
|
|
|
|
NULL, /* QueryCredentialsAttributes */
|
|
|
|
NULL, /* AcquireCredentialsHandle */
|
|
|
|
NULL, /* FreeCredentialsHandle */
|
|
|
|
NULL, /* Reserved2 */
|
|
|
|
NULL, /* InitializeSecurityContext */
|
|
|
|
NULL, /* AcceptSecurityContext */
|
|
|
|
NULL, /* CompleteAuthToken */
|
|
|
|
NULL, /* DeleteSecurityContext */
|
|
|
|
NULL, /* ApplyControlToken */
|
|
|
|
NULL, /* QueryContextAttributes */
|
|
|
|
NULL, /* ImpersonateSecurityContext */
|
|
|
|
NULL, /* RevertSecurityContext */
|
|
|
|
NULL, /* MakeSignature */
|
|
|
|
NULL, /* VerifySignature */
|
|
|
|
NULL, /* FreeContextBuffer */
|
|
|
|
NULL, /* QuerySecurityPackageInfo */
|
|
|
|
NULL, /* Reserved3 */
|
|
|
|
NULL, /* Reserved4 */
|
|
|
|
NULL, /* ExportSecurityContext */
|
|
|
|
NULL, /* ImportSecurityContext */
|
|
|
|
NULL, /* AddCredentials */
|
|
|
|
NULL, /* Reserved8 */
|
|
|
|
NULL, /* QuerySecurityContextToken */
|
|
|
|
NULL, /* EncryptMessage */
|
|
|
|
NULL, /* DecryptMessage */
|
|
|
|
NULL, /* SetContextAttributes */
|
|
|
|
};
|
|
|
|
|
|
|
|
const SEC_PKG_INFO CREDSSP_SEC_PKG_INFO =
|
|
|
|
{
|
|
|
|
0x000110733, /* fCapabilities */
|
|
|
|
1, /* wVersion */
|
|
|
|
0xFFFF, /* wRPCID */
|
|
|
|
0x000090A8, /* cbMaxToken */
|
|
|
|
"CREDSSP", /* Name */
|
|
|
|
"Microsoft CredSSP Security Provider" /* Comment */
|
|
|
|
};
|