0c7016
From 25d31015255cf1b80dae76a3654ba0d62c4d71d5 Mon Sep 17 00:00:00 2001
0c7016
From: Ondrej Holy <oholy@redhat.com>
0c7016
Date: Wed, 27 Sep 2017 15:56:35 +0200
0c7016
Subject: [PATCH 1/2] Fix NTLM on big endian
0c7016
0c7016
Based on fixes from master:
0c7016
https://github.com/FreeRDP/FreeRDP/pull/3284
0c7016
---
0c7016
 libfreerdp-core/ntlmssp.c | 39 +++++++++++++++++++++++++++++----------
0c7016
 1 file changed, 29 insertions(+), 10 deletions(-)
0c7016
0c7016
diff --git a/libfreerdp-core/ntlmssp.c b/libfreerdp-core/ntlmssp.c
0c7016
index 84ad319c3..8f69878cf 100644
0c7016
--- a/libfreerdp-core/ntlmssp.c
0c7016
+++ b/libfreerdp-core/ntlmssp.c
0c7016
@@ -126,6 +126,13 @@ static const char* const AV_PAIRS_STRINGS[] =
0c7016
 	"MsvChannelBindings"
0c7016
 };
0c7016
 
0c7016
+#define Data_Write_UINT32(_d, _v) do { \
0c7016
+       *((uint8*) _d) = (_v) & 0xFF; \
0c7016
+       *((uint8*) _d + 1) = ((_v) >> 8) & 0xFF; \
0c7016
+       *((uint8*) _d + 2) = ((_v) >> 16) & 0xFF; \
0c7016
+       *((uint8*) _d + 3) = ((_v) >> 24) & 0xFF; \
0c7016
+} while (0)
0c7016
+
0c7016
 /**
0c7016
  * Set NTLMSSP username.
0c7016
  * @param ntlmssp
0c7016
@@ -138,7 +145,9 @@ void ntlmssp_set_username(NTLMSSP* ntlmssp, char* username)
0c7016
 
0c7016
 	if (username != NULL)
0c7016
 	{
0c7016
-		ntlmssp->username.data = freerdp_uniconv_out(ntlmssp->uniconv, username, (size_t*) &(ntlmssp->username.length));
0c7016
+		size_t length;
0c7016
+		ntlmssp->username.data = freerdp_uniconv_out(ntlmssp->uniconv, username, &length);
0c7016
+		ntlmssp->username.length = length;
0c7016
 	}
0c7016
 }
0c7016
 
0c7016
@@ -154,7 +163,9 @@ void ntlmssp_set_domain(NTLMSSP* ntlmssp, char* domain)
0c7016
 
0c7016
 	if (domain != NULL)
0c7016
 	{
0c7016
-		ntlmssp->domain.data = freerdp_uniconv_out(ntlmssp->uniconv, domain, (size_t*) &(ntlmssp->domain.length));
0c7016
+		size_t length;
0c7016
+		ntlmssp->domain.data = freerdp_uniconv_out(ntlmssp->uniconv, domain, &length);
0c7016
+		ntlmssp->domain.length = length;
0c7016
 	}
0c7016
 }
0c7016
 
0c7016
@@ -170,7 +181,9 @@ void ntlmssp_set_password(NTLMSSP* ntlmssp, char* password)
0c7016
 
0c7016
 	if (password != NULL)
0c7016
 	{
0c7016
-		ntlmssp->password.data = freerdp_uniconv_out(ntlmssp->uniconv, password, (size_t*) &(ntlmssp->password.length));
0c7016
+		size_t length;
0c7016
+		ntlmssp->password.data = freerdp_uniconv_out(ntlmssp->uniconv, password, &length);
0c7016
+		ntlmssp->password.length = length;
0c7016
 	}
0c7016
 }
0c7016
 
0c7016
@@ -186,7 +199,9 @@ void ntlmssp_set_workstation(NTLMSSP* ntlmssp, char* workstation)
0c7016
 
0c7016
 	if (workstation != NULL)
0c7016
 	{
0c7016
-		ntlmssp->workstation.data = freerdp_uniconv_out(ntlmssp->uniconv, workstation, (size_t*) &(ntlmssp->workstation.length));
0c7016
+		size_t length;
0c7016
+		ntlmssp->workstation.data = freerdp_uniconv_out(ntlmssp->uniconv, workstation, &length);
0c7016
+		ntlmssp->workstation.length = length;
0c7016
 	}
0c7016
 }
0c7016
 
0c7016
@@ -1185,11 +1200,13 @@ void ntlmssp_encrypt_message(NTLMSSP* ntlmssp, rdpBlob* msg, rdpBlob* encrypted_
0c7016
 	uint8 digest[16];
0c7016
 	uint8 checksum[8];
0c7016
 	uint32 version = 1;
0c7016
+	uint32 value;
0c7016
 
0c7016
 	/* Compute the HMAC-MD5 hash of ConcatenationOf(seq_num,msg) using the client signing key */
0c7016
 	HMAC_CTX_init(&hmac_ctx);
0c7016
 	HMAC_Init_ex(&hmac_ctx, ntlmssp->client_signing_key, 16, EVP_md5(), NULL);
0c7016
-	HMAC_Update(&hmac_ctx, (void*) &ntlmssp->send_seq_num, 4);
0c7016
+	Data_Write_UINT32(&value, ntlmssp->send_seq_num);
0c7016
+	HMAC_Update(&hmac_ctx, (void*) &value, 4);
0c7016
 	HMAC_Update(&hmac_ctx, msg->data, msg->length);
0c7016
 	HMAC_Final(&hmac_ctx, digest, NULL);
0c7016
 
0c7016
@@ -1203,9 +1220,9 @@ void ntlmssp_encrypt_message(NTLMSSP* ntlmssp, rdpBlob* msg, rdpBlob* encrypted_
0c7016
 	crypto_rc4(ntlmssp->send_rc4_seal, 8, digest, checksum);
0c7016
 
0c7016
 	/* Concatenate version, ciphertext and sequence number to build signature */
0c7016
-	memcpy(signature, (void*) &version, 4);
0c7016
+	Data_Write_UINT32(signature, version);
0c7016
 	memcpy(&signature[4], (void*) checksum, 8);
0c7016
-	memcpy(&signature[12], (void*) &(ntlmssp->send_seq_num), 4);
0c7016
+	Data_Write_UINT32(&signature[12], ntlmssp->send_seq_num);
0c7016
 
0c7016
 	HMAC_CTX_cleanup(&hmac_ctx);
0c7016
 
0c7016
@@ -1230,6 +1247,7 @@ int ntlmssp_decrypt_message(NTLMSSP* ntlmssp, rdpBlob* encrypted_msg, rdpBlob* m
0c7016
 	uint8 checksum[8];
0c7016
 	uint32 version = 1;
0c7016
 	uint8 expected_signature[16];
0c7016
+	uint32 value;
0c7016
 
0c7016
 	/* Allocate space for encrypted message */
0c7016
 	freerdp_blob_alloc(msg, encrypted_msg->length);
0c7016
@@ -1240,7 +1258,8 @@ int ntlmssp_decrypt_message(NTLMSSP* ntlmssp, rdpBlob* encrypted_msg, rdpBlob* m
0c7016
 	/* Compute the HMAC-MD5 hash of ConcatenationOf(seq_num,msg) using the client signing key */
0c7016
 	HMAC_CTX_init(&hmac_ctx);
0c7016
 	HMAC_Init_ex(&hmac_ctx, ntlmssp->server_signing_key, 16, EVP_md5(), NULL);
0c7016
-	HMAC_Update(&hmac_ctx, (void*) &ntlmssp->recv_seq_num, 4);
0c7016
+	Data_Write_UINT32(&value, ntlmssp->recv_seq_num);
0c7016
+	HMAC_Update(&hmac_ctx, (void*) &value, 4);
0c7016
 	HMAC_Update(&hmac_ctx, msg->data, msg->length);
0c7016
 	HMAC_Final(&hmac_ctx, digest, NULL);
0c7016
 
0c7016
@@ -1248,9 +1267,9 @@ int ntlmssp_decrypt_message(NTLMSSP* ntlmssp, rdpBlob* encrypted_msg, rdpBlob* m
0c7016
 	crypto_rc4(ntlmssp->recv_rc4_seal, 8, digest, checksum);
0c7016
 
0c7016
 	/* Concatenate version, ciphertext and sequence number to build signature */
0c7016
-	memcpy(expected_signature, (void*) &version, 4);
0c7016
+	Data_Write_UINT32(expected_signature, version);
0c7016
 	memcpy(&expected_signature[4], (void*) checksum, 8);
0c7016
-	memcpy(&expected_signature[12], (void*) &(ntlmssp->recv_seq_num), 4);
0c7016
+	Data_Write_UINT32(&expected_signature[12], ntlmssp->recv_seq_num);
0c7016
 
0c7016
 	if (memcmp(signature, expected_signature, 16) != 0)
0c7016
 	{
0c7016
-- 
0c7016
2.14.2
0c7016