2011-07-01 02:51:46 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Client
|
|
|
|
* Credential Security Support Provider (CredSSP)
|
|
|
|
*
|
|
|
|
* Copyright 2010 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
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>
|
|
|
|
#include "ntlmssp.h"
|
|
|
|
|
|
|
|
#include "credssp.h"
|
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
|
|
|
/**
|
|
|
|
* Initialize NTLMSSP authentication module.
|
|
|
|
* @param credssp
|
|
|
|
*/
|
|
|
|
|
2011-07-07 19:49:57 +04:00
|
|
|
void credssp_ntlmssp_init(rdpCredssp* credssp)
|
2011-07-01 02:51:46 +04:00
|
|
|
{
|
|
|
|
NTLMSSP *ntlmssp = credssp->ntlmssp;
|
2011-07-06 07:18:00 +04:00
|
|
|
rdpSettings *settings = credssp->transport->settings;
|
2011-07-01 02:51:46 +04:00
|
|
|
|
|
|
|
ntlmssp_set_password(ntlmssp, settings->password);
|
|
|
|
ntlmssp_set_username(ntlmssp, settings->username);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
ntlmssp->ntlm_v2 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get TLS public key.
|
|
|
|
* @param credssp
|
|
|
|
*/
|
|
|
|
|
2011-07-07 19:49:57 +04:00
|
|
|
int credssp_get_public_key(rdpCredssp* credssp)
|
2011-07-01 02:51:46 +04:00
|
|
|
{
|
|
|
|
int ret;
|
2011-07-06 07:18:00 +04:00
|
|
|
CryptoCert cert;
|
2011-08-29 00:46:36 +04:00
|
|
|
|
2011-07-06 07:18:00 +04:00
|
|
|
cert = tls_get_certificate(credssp->transport->tls);
|
2011-07-01 02:51:46 +04:00
|
|
|
|
|
|
|
if (cert == NULL)
|
|
|
|
{
|
|
|
|
printf("credssp_get_public_key: tls_get_certificate failed to return the server certificate.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2011-08-29 00:56:34 +04:00
|
|
|
if(tls_verify_certificate(cert,credssp->transport->settings->hostname))
|
|
|
|
tls_disconnect(credssp->transport->tls);
|
2011-07-01 02:51:46 +04:00
|
|
|
ret = crypto_cert_get_public_key(cert, &credssp->public_key);
|
|
|
|
crypto_cert_free(cert);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Authenticate with server using CredSSP.
|
|
|
|
* @param credssp
|
|
|
|
* @return 1 if authentication is successful
|
|
|
|
*/
|
|
|
|
|
2011-07-07 19:49:57 +04:00
|
|
|
int credssp_authenticate(rdpCredssp* credssp)
|
2011-07-01 02:51:46 +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);
|
|
|
|
|
|
|
|
credssp_ntlmssp_init(credssp);
|
|
|
|
|
|
|
|
if (credssp_get_public_key(credssp) == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* NTLMSSP NEGOTIATE MESSAGE */
|
2011-07-06 07:18:00 +04:00
|
|
|
s->p = s->data = negoTokenBuffer;
|
2011-07-01 02:51:46 +04:00
|
|
|
ntlmssp_send(ntlmssp, s);
|
|
|
|
credssp->negoToken.data = s->data;
|
2011-07-06 07:18:00 +04:00
|
|
|
credssp->negoToken.length = s->p - s->data;
|
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;
|
|
|
|
|
|
|
|
s->p = s->data = credssp->negoToken.data;
|
|
|
|
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 */
|
2011-07-06 07:18:00 +04:00
|
|
|
s->p = s->data = negoTokenBuffer;
|
2011-07-01 02:51:46 +04:00
|
|
|
ntlmssp_send(ntlmssp, s);
|
|
|
|
|
|
|
|
/* The last NTLMSSP message is sent with the encrypted public key */
|
|
|
|
credssp->negoToken.data = s->data;
|
2011-07-06 07:18:00 +04:00
|
|
|
credssp->negoToken.length = s->p - s->data;
|
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);
|
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);
|
2011-07-01 02:51:46 +04:00
|
|
|
|
|
|
|
xfree(s);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
|
|
|
uint8 *p;
|
|
|
|
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;
|
|
|
|
|
2011-07-07 19:49:57 +04:00
|
|
|
freerdp_blob_alloc(d, credssp->public_key.length + 16);
|
2011-07-01 02:51:46 +04:00
|
|
|
ntlmssp_encrypt_message(ntlmssp, &credssp->public_key, &encrypted_public_key, signature);
|
|
|
|
|
|
|
|
#ifdef WITH_DEBUG_NLA
|
|
|
|
printf("Public Key (length = %d)\n", credssp->public_key.length);
|
2011-07-07 19:27:24 +04:00
|
|
|
freerdp_hexdump(credssp->public_key.data, credssp->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;
|
|
|
|
uint8 *signature;
|
2011-08-16 01:05:48 +04:00
|
|
|
rdpBlob public_key;
|
|
|
|
rdpBlob encrypted_public_key;
|
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);
|
|
|
|
|
|
|
|
p1 = (uint8*) credssp->public_key.data;
|
|
|
|
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
|
|
|
{
|
|
|
|
uint8 *p;
|
|
|
|
uint8 signature[16];
|
2011-08-16 01:05:48 +04:00
|
|
|
rdpBlob encrypted_ts_credentials;
|
2011-07-01 02:51:46 +04:00
|
|
|
NTLMSSP *ntlmssp = credssp->ntlmssp;
|
|
|
|
|
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) */
|
|
|
|
ber_write_contextual_tag(s, 0, credssp->ntlmssp->domain.length + 2, True);
|
|
|
|
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) */
|
|
|
|
ber_write_contextual_tag(s, 1, credssp->ntlmssp->username.length + 2, True);
|
|
|
|
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) */
|
|
|
|
ber_write_contextual_tag(s, 2, credssp->ntlmssp->password.length + 2, True);
|
|
|
|
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) */
|
|
|
|
length -= ber_write_contextual_tag(s, 0, 3, True);
|
|
|
|
length -= ber_write_integer(s, 1);
|
|
|
|
|
|
|
|
/* [1] credentials (OCTET STRING) */
|
|
|
|
length -= 1;
|
|
|
|
length -= ber_write_contextual_tag(s, 1, length, True);
|
|
|
|
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);
|
|
|
|
s->p = s->data = credssp->ts_credentials.data;
|
|
|
|
s->size = length;
|
2011-07-01 02:51:46 +04:00
|
|
|
|
2011-07-19 02:43:23 +04:00
|
|
|
credssp_write_ts_credentials(credssp, 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-07-18 10:34:28 +04:00
|
|
|
ber_write_contextual_tag(s, 0, 3, True); /* [0] version */
|
|
|
|
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);
|
|
|
|
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-07-19 00:16:37 +04:00
|
|
|
length -= ber_write_contextual_tag(s, 0, length, True); /* [0] negoToken */
|
|
|
|
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);
|
|
|
|
length -= ber_write_contextual_tag(s, 2, length, True);
|
|
|
|
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);
|
|
|
|
length -= ber_write_contextual_tag(s, 3, length, True);
|
|
|
|
ber_write_octet_string(s, pubKeyAuth->data, length);
|
|
|
|
}
|
2011-07-18 10:34:28 +04:00
|
|
|
|
|
|
|
transport_write(credssp->transport, s);
|
|
|
|
}
|
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
|
|
|
|
2011-07-10 23:34:43 +04:00
|
|
|
s = transport_recv_stream_init(credssp->transport, 2048);
|
|
|
|
status = transport_read(credssp->transport, 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);
|
|
|
|
ber_read_contextual_tag(s, 0, &length, True);
|
|
|
|
ber_read_integer(s, &version);
|
2011-07-01 02:51:46 +04:00
|
|
|
|
2011-07-19 01:02:06 +04:00
|
|
|
/* [1] negoTokens (NegoData) */
|
|
|
|
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 */
|
|
|
|
ber_read_contextual_tag(s, 0, &length, True); /* [0] negoToken */
|
|
|
|
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) */
|
|
|
|
if (ber_read_contextual_tag(s, 2, &length, True) != False)
|
|
|
|
{
|
|
|
|
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) */
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2011-07-07 23:35:09 +04:00
|
|
|
rdpCredssp* credssp_new(rdpTransport* transport)
|
2011-07-01 02:51:46 +04:00
|
|
|
{
|
2011-07-07 19:49:57 +04:00
|
|
|
rdpCredssp* self;
|
|
|
|
|
|
|
|
self = (rdpCredssp*) xzalloc(sizeof(rdpCredssp));
|
2011-07-01 02:51:46 +04:00
|
|
|
|
|
|
|
if (self != NULL)
|
|
|
|
{
|
2011-07-06 07:18:00 +04:00
|
|
|
self->transport = transport;
|
2011-07-01 02:51:46 +04:00
|
|
|
self->send_seq_num = 0;
|
|
|
|
self->ntlmssp = ntlmssp_new();
|
|
|
|
}
|
2011-07-07 19:49:57 +04:00
|
|
|
|
2011-07-01 02:51:46 +04:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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->public_key);
|
|
|
|
freerdp_blob_free(&credssp->ts_credentials);
|
2011-07-01 02:51:46 +04:00
|
|
|
|
|
|
|
ntlmssp_free(credssp->ntlmssp);
|
|
|
|
xfree(credssp);
|
|
|
|
}
|
|
|
|
}
|