Commit message: Fixed GHSL-2020-101 missing NULL check

(cherry picked from commit b207dbba35c505bbc3ad5aadc10b34980c6b7e8e)
CVE: CVE-2020-13397
CWE: CWE-787
Vulnerability description: An issue was discovered in FreeRDP before 2.1.1. An out-of-bounds (OOB) read vulnerability has been detected in security_fips_decrypt in libfreerdp/core/security.c due to an uninitialized value.

Project: FreeRDP
File: security.c

static void fips_expand_key_bits(BYTE* in, BYTE* out)
{
	BYTE buf[21], c;
	int i, b, p, r;

	/* reverse every byte in the key */
	for (i = 0; i < 21; i++)
		buf[i] = fips_reverse_table[in[i]];

	/* insert a zero-bit after every 7th bit */
	for (i = 0, b = 0; i < 24; i++, b += 7)
	{
		p = b / 8;
		r = b % 8;

		if (r <= 1)
		{
			out[i] = (buf[p] << r) & 0xfe;
		}
		else
		{
			/* c is accumulator */
			c = buf[p] << r;
			c |= buf[p + 1] >> (8 - r);
			out[i] = c & 0xfe;
		}
	}

	/* reverse every byte */
	/* alter lsb so the byte has odd parity */
	for (i = 0; i < 24; i++)
		out[i] = fips_oddparity_table[fips_reverse_table[out[i]]];
}

Project: FreeRDP
File: security.c

static BOOL security_salted_hash(const BYTE* salt, const BYTE* input, int length, const BYTE* salt1,
                                 const BYTE* salt2, BYTE* output)
{
	WINPR_DIGEST_CTX* sha1 = NULL;
	WINPR_DIGEST_CTX* md5 = NULL;
	BYTE sha1_digest[WINPR_SHA1_DIGEST_LENGTH];
	BOOL result = FALSE;

	/* SaltedHash(Salt, Input, Salt1, Salt2) = MD5(S + SHA1(Input + Salt + Salt1 + Salt2)) */

	/* SHA1_Digest = SHA1(Input + Salt + Salt1 + Salt2) */
	if (!(sha1 = winpr_Digest_New()))
		goto out;

	if (!winpr_Digest_Init(sha1, WINPR_MD_SHA1))
		goto out;

	if (!winpr_Digest_Update(sha1, input, length)) /* Input */
		goto out;

	if (!winpr_Digest_Update(sha1, salt, 48)) /* Salt (48 bytes) */
		goto out;

	if (!winpr_Digest_Update(sha1, salt1, 32)) /* Salt1 (32 bytes) */
		goto out;

	if (!winpr_Digest_Update(sha1, salt2, 32)) /* Salt2 (32 bytes) */
		goto out;

	if (!winpr_Digest_Final(sha1, sha1_digest, sizeof(sha1_digest)))
		goto out;

	/* SaltedHash(Salt, Input, Salt1, Salt2) = MD5(S + SHA1_Digest) */
	if (!(md5 = winpr_Digest_New()))
		goto out;

	/* Allow FIPS override for use of MD5 here, this is used for creating hashes of the
	 * premaster_secret and master_secret */
	/* used for RDP licensing as described in MS-RDPELE. This is for RDP licensing packets */
	/* which will already be encrypted under FIPS, so the use of MD5 here is not for sensitive data
	 * protection. */
	if (!winpr_Digest_Init_Allow_FIPS(md5, WINPR_MD_MD5))
		goto out;

	if (!winpr_Digest_Update(md5, salt, 48)) /* Salt (48 bytes) */
		goto out;

	if (!winpr_Digest_Update(md5, sha1_digest, sizeof(sha1_digest))) /* SHA1_Digest */
		goto out;

	if (!winpr_Digest_Final(md5, output, WINPR_MD5_DIGEST_LENGTH))
		goto out;

	result = TRUE;
out:
	winpr_Digest_Free(sha1);
	winpr_Digest_Free(md5);
	return result;
}

Project: FreeRDP
File: security.c

BOOL security_fips_check_signature(const BYTE* data, size_t length, const BYTE* sig, rdpRdp* rdp)
{
	BYTE buf[WINPR_SHA1_DIGEST_LENGTH];
	BYTE use_count_le[4];
	WINPR_HMAC_CTX* hmac;
	BOOL result = FALSE;
	security_UINT32_le(use_count_le, rdp->decrypt_use_count);

	if (!(hmac = winpr_HMAC_New()))
		return FALSE;

	if (!winpr_HMAC_Init(hmac, WINPR_MD_SHA1, rdp->fips_sign_key, WINPR_SHA1_DIGEST_LENGTH))
		goto out;

	if (!winpr_HMAC_Update(hmac, data, length))
		goto out;

	if (!winpr_HMAC_Update(hmac, use_count_le, 4))
		goto out;

	if (!winpr_HMAC_Final(hmac, buf, WINPR_SHA1_DIGEST_LENGTH))
		goto out;

	rdp->decrypt_use_count++;

	if (!memcmp(sig, buf, 8))
		result = TRUE;

out:
	winpr_HMAC_Free(hmac);
	return result;
}

Project: FreeRDP
File: security.c

BOOL security_mac_signature(rdpRdp* rdp, const BYTE* data, UINT32 length, BYTE* output)
{
	WINPR_DIGEST_CTX* sha1 = NULL;
	WINPR_DIGEST_CTX* md5 = NULL;
	BYTE length_le[4];
	BYTE md5_digest[WINPR_MD5_DIGEST_LENGTH];
	BYTE sha1_digest[WINPR_SHA1_DIGEST_LENGTH];
	BOOL result = FALSE;
	security_UINT32_le(length_le, length); /* length must be little-endian */

	/* SHA1_Digest = SHA1(MACKeyN + pad1 + length + data) */
	if (!(sha1 = winpr_Digest_New()))
		goto out;

	if (!winpr_Digest_Init(sha1, WINPR_MD_SHA1))
		goto out;

	if (!winpr_Digest_Update(sha1, rdp->sign_key, rdp->rc4_key_len)) /* MacKeyN */
		goto out;

	if (!winpr_Digest_Update(sha1, pad1, sizeof(pad1))) /* pad1 */
		goto out;

	if (!winpr_Digest_Update(sha1, length_le, sizeof(length_le))) /* length */
		goto out;

	if (!winpr_Digest_Update(sha1, data, length)) /* data */
		goto out;

	if (!winpr_Digest_Final(sha1, sha1_digest, sizeof(sha1_digest)))
		goto out;

	/* MACSignature = First64Bits(MD5(MACKeyN + pad2 + SHA1_Digest)) */
	if (!(md5 = winpr_Digest_New()))
		goto out;

	if (!winpr_Digest_Init(md5, WINPR_MD_MD5))
		goto out;

	if (!winpr_Digest_Update(md5, rdp->sign_key, rdp->rc4_key_len)) /* MacKeyN */
		goto out;

	if (!winpr_Digest_Update(md5, pad2, sizeof(pad2))) /* pad2 */
		goto out;

	if (!winpr_Digest_Update(md5, sha1_digest, sizeof(sha1_digest))) /* SHA1_Digest */
		goto out;

	if (!winpr_Digest_Final(md5, md5_digest, sizeof(md5_digest)))
		goto out;

	memcpy(output, md5_digest, 8);
	result = TRUE;
out:
	winpr_Digest_Free(sha1);
	winpr_Digest_Free(md5);
	return result;
}

Project: FreeRDP
File: security.c

BOOL security_mac_data(const BYTE* mac_salt_key, const BYTE* data, UINT32 length, BYTE* output)
{
	WINPR_DIGEST_CTX* sha1 = NULL;
	WINPR_DIGEST_CTX* md5 = NULL;
	BYTE length_le[4];
	BYTE sha1_digest[WINPR_SHA1_DIGEST_LENGTH];
	BOOL result = FALSE;
	/* MacData = MD5(MacSaltKey + pad2 + SHA1(MacSaltKey + pad1 + length + data)) */
	security_UINT32_le(length_le, length); /* length must be little-endian */

	/* SHA1_Digest = SHA1(MacSaltKey + pad1 + length + data) */
	if (!(sha1 = winpr_Digest_New()))
		goto out;

	if (!winpr_Digest_Init(sha1, WINPR_MD_SHA1))
		goto out;

	if (!winpr_Digest_Update(sha1, mac_salt_key, 16)) /* MacSaltKey */
		goto out;

	if (!winpr_Digest_Update(sha1, pad1, sizeof(pad1))) /* pad1 */
		goto out;

	if (!winpr_Digest_Update(sha1, length_le, sizeof(length_le))) /* length */
		goto out;

	if (!winpr_Digest_Update(sha1, data, length)) /* data */
		goto out;

	if (!winpr_Digest_Final(sha1, sha1_digest, sizeof(sha1_digest)))
		goto out;

	/* MacData = MD5(MacSaltKey + pad2 + SHA1_Digest) */
	if (!(md5 = winpr_Digest_New()))
		goto out;

	/* Allow FIPS override for use of MD5 here, this is only used for creating the MACData field of
	 * the */
	/* Client Platform Challenge Response packet (from MS-RDPELE section 2.2.2.5). This is for RDP
	 * licensing packets */
	/* which will already be encrypted under FIPS, so the use of MD5 here is not for sensitive data
	 * protection. */
	if (!winpr_Digest_Init_Allow_FIPS(md5, WINPR_MD_MD5))
		goto out;

	if (!winpr_Digest_Update(md5, mac_salt_key, 16)) /* MacSaltKey */
		goto out;

	if (!winpr_Digest_Update(md5, pad2, sizeof(pad2))) /* pad2 */
		goto out;

	if (!winpr_Digest_Update(md5, sha1_digest, sizeof(sha1_digest))) /* SHA1_Digest */
		goto out;

	if (!winpr_Digest_Final(md5, output, WINPR_MD5_DIGEST_LENGTH))
		goto out;

	result = TRUE;
out:
	winpr_Digest_Free(sha1);
	winpr_Digest_Free(md5);
	return result;
}

Project: FreeRDP
File: security.c

static BOOL security_premaster_hash(const char* input, int length, const BYTE* premaster_secret,
                                    const BYTE* client_random, const BYTE* server_random,
                                    BYTE* output)
{
	/* PremasterHash(Input) = SaltedHash(PremasterSecret, Input, ClientRandom, ServerRandom) */
	return security_salted_hash(premaster_secret, (BYTE*)input, length, client_random,
	                            server_random, output);
}

Project: FreeRDP
File: security.c

static BOOL security_A(BYTE* master_secret, const BYTE* client_random, BYTE* server_random,
                       BYTE* output)
{
	return security_premaster_hash("A", 1, master_secret, client_random, server_random,
	                               &output[0]) &&
	       security_premaster_hash("BB", 2, master_secret, client_random, server_random,
	                               &output[16]) &&
	       security_premaster_hash("CCC", 3, master_secret, client_random, server_random,
	                               &output[32]);
}

Project: FreeRDP
File: security.c

static BOOL security_md5_16_32_32_Allow_FIPS(const BYTE* in0, const BYTE* in1, const BYTE* in2,
                                             BYTE* output)
{
	WINPR_DIGEST_CTX* md5 = NULL;
	BOOL result = FALSE;

	if (!(md5 = winpr_Digest_New()))
		return FALSE;
	if (!winpr_Digest_Init_Allow_FIPS(md5, WINPR_MD_MD5))
		goto out;
	if (!winpr_Digest_Update(md5, in0, 16))
		goto out;
	if (!winpr_Digest_Update(md5, in1, 32))
		goto out;
	if (!winpr_Digest_Update(md5, in2, 32))
		goto out;
	if (!winpr_Digest_Final(md5, output, WINPR_MD5_DIGEST_LENGTH))
		goto out;

	result = TRUE;
out:
	winpr_Digest_Free(md5);
	return result;
}

Project: FreeRDP
File: security.c

BOOL security_decrypt(BYTE* data, size_t length, rdpRdp* rdp)
{
	if (rdp->rc4_decrypt_key == NULL)
		return FALSE;

	if (rdp->decrypt_use_count >= 4096)
	{
		if (!security_key_update(rdp->decrypt_key, rdp->decrypt_update_key, rdp->rc4_key_len, rdp))
			return FALSE;

		winpr_RC4_Free(rdp->rc4_decrypt_key);
		rdp->rc4_decrypt_key = winpr_RC4_New(rdp->decrypt_key, rdp->rc4_key_len);

		if (!rdp->rc4_decrypt_key)
			return FALSE;

		rdp->decrypt_use_count = 0;
	}

	if (!winpr_RC4_Update(rdp->rc4_decrypt_key, length, data, data))
		return FALSE;

	rdp->decrypt_use_count += 1;
	rdp->decrypt_checksum_use_count++;
	return TRUE;
}

Project: FreeRDP
File: security.c

BOOL security_establish_keys(const BYTE* client_random, rdpRdp* rdp)
{
	BYTE pre_master_secret[48];
	BYTE master_secret[48];
	BYTE session_key_blob[48];
	BYTE* server_random;
	BYTE salt[] = { 0xD1, 0x26, 0x9E }; /* 40 bits: 3 bytes, 56 bits: 1 byte */
	rdpSettings* settings;
	BOOL status;
	settings = rdp->settings;
	server_random = settings->ServerRandom;

	if (settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS)
	{
		WINPR_DIGEST_CTX* sha1;
		BYTE client_encrypt_key_t[WINPR_SHA1_DIGEST_LENGTH + 1];
		BYTE client_decrypt_key_t[WINPR_SHA1_DIGEST_LENGTH + 1];

		if (!(sha1 = winpr_Digest_New()))
			return FALSE;

		if (!winpr_Digest_Init(sha1, WINPR_MD_SHA1) ||
		    !winpr_Digest_Update(sha1, client_random + 16, 16) ||
		    !winpr_Digest_Update(sha1, server_random + 16, 16) ||
		    !winpr_Digest_Final(sha1, client_encrypt_key_t, sizeof(client_encrypt_key_t)))
		{
			winpr_Digest_Free(sha1);
			return FALSE;
		}

		client_encrypt_key_t[20] = client_encrypt_key_t[0];

		if (!winpr_Digest_Init(sha1, WINPR_MD_SHA1) ||
		    !winpr_Digest_Update(sha1, client_random, 16) ||
		    !winpr_Digest_Update(sha1, server_random, 16) ||
		    !winpr_Digest_Final(sha1, client_decrypt_key_t, sizeof(client_decrypt_key_t)))
		{
			winpr_Digest_Free(sha1);
			return FALSE;
		}

		client_decrypt_key_t[20] = client_decrypt_key_t[0];

		if (!winpr_Digest_Init(sha1, WINPR_MD_SHA1) ||
		    !winpr_Digest_Update(sha1, client_decrypt_key_t, WINPR_SHA1_DIGEST_LENGTH) ||
		    !winpr_Digest_Update(sha1, client_encrypt_key_t, WINPR_SHA1_DIGEST_LENGTH) ||
		    !winpr_Digest_Final(sha1, rdp->fips_sign_key, WINPR_SHA1_DIGEST_LENGTH))
		{
			winpr_Digest_Free(sha1);
			return FALSE;
		}

		winpr_Digest_Free(sha1);

		if (rdp->settings->ServerMode)
		{
			fips_expand_key_bits(client_encrypt_key_t, rdp->fips_decrypt_key);
			fips_expand_key_bits(client_decrypt_key_t, rdp->fips_encrypt_key);
		}
		else
		{
			fips_expand_key_bits(client_encrypt_key_t, rdp->fips_encrypt_key);
			fips_expand_key_bits(client_decrypt_key_t, rdp->fips_decrypt_key);
		}
	}

	memcpy(pre_master_secret, client_random, 24);
	memcpy(pre_master_secret + 24, server_random, 24);

	if (!security_A(pre_master_secret, client_random, server_random, master_secret) ||
	    !security_X(master_secret, client_random, server_random, session_key_blob))
	{
		return FALSE;
	}

	memcpy(rdp->sign_key, session_key_blob, 16);

	if (rdp->settings->ServerMode)
	{
		status = security_md5_16_32_32(&session_key_blob[16], client_random, server_random,
		                               rdp->encrypt_key);
		status &= security_md5_16_32_32(&session_key_blob[32], client_random, server_random,
		                                rdp->decrypt_key);
	}
	else
	{
		/* Allow FIPS use of MD5 here, this is just used for generation of the SessionKeyBlob as
		 * described in MS-RDPELE. */
		/* This is for RDP licensing packets which will already be encrypted under FIPS, so the use
		 * of MD5 here is not */
		/* for sensitive data protection. */
		status = security_md5_16_32_32_Allow_FIPS(&session_key_blob[16], client_random,
		                                          server_random, rdp->decrypt_key);
		status &= security_md5_16_32_32_Allow_FIPS(&session_key_blob[32], client_random,
		                                           server_random, rdp->encrypt_key);
	}

	if (!status)
		return FALSE;

	if (settings->EncryptionMethods == ENCRYPTION_METHOD_40BIT)
	{
		memcpy(rdp->sign_key, salt, 3);
		memcpy(rdp->decrypt_key, salt, 3);
		memcpy(rdp->encrypt_key, salt, 3);
		rdp->rc4_key_len = 8;
	}
	else if (settings->EncryptionMethods == ENCRYPTION_METHOD_56BIT)
	{
		memcpy(rdp->sign_key, salt, 1);
		memcpy(rdp->decrypt_key, salt, 1);
		memcpy(rdp->encrypt_key, salt, 1);
		rdp->rc4_key_len = 8;
	}
	else if (settings->EncryptionMethods == ENCRYPTION_METHOD_128BIT)
	{
		rdp->rc4_key_len = 16;
	}

	memcpy(rdp->decrypt_update_key, rdp->decrypt_key, 16);
	memcpy(rdp->encrypt_update_key, rdp->encrypt_key, 16);
	rdp->decrypt_use_count = 0;
	rdp->decrypt_checksum_use_count = 0;
	rdp->encrypt_use_count = 0;
	rdp->encrypt_checksum_use_count = 0;
	return TRUE;
}

Project: FreeRDP
File: security.c

BOOL security_hmac_signature(const BYTE* data, size_t length, BYTE* output, rdpRdp* rdp)
{
	BYTE buf[WINPR_SHA1_DIGEST_LENGTH];
	BYTE use_count_le[4];
	WINPR_HMAC_CTX* hmac;
	BOOL result = FALSE;
	security_UINT32_le(use_count_le, rdp->encrypt_use_count);

	if (!(hmac = winpr_HMAC_New()))
		return FALSE;

	if (!winpr_HMAC_Init(hmac, WINPR_MD_SHA1, rdp->fips_sign_key, WINPR_SHA1_DIGEST_LENGTH))
		goto out;

	if (!winpr_HMAC_Update(hmac, data, length))
		goto out;

	if (!winpr_HMAC_Update(hmac, use_count_le, 4))
		goto out;

	if (!winpr_HMAC_Final(hmac, buf, WINPR_SHA1_DIGEST_LENGTH))
		goto out;

	memmove(output, buf, 8);
	result = TRUE;
out:
	winpr_HMAC_Free(hmac);
	return result;
}

Project: FreeRDP
File: security.c

BOOL security_encrypt(BYTE* data, size_t length, rdpRdp* rdp)
{
	BOOL rc = FALSE;
	EnterCriticalSection(&rdp->critical);
	if (rdp->encrypt_use_count >= 4096)
	{
		if (!security_key_update(rdp->encrypt_key, rdp->encrypt_update_key, rdp->rc4_key_len, rdp))
			goto fail;

		winpr_RC4_Free(rdp->rc4_encrypt_key);
		rdp->rc4_encrypt_key = winpr_RC4_New(rdp->encrypt_key, rdp->rc4_key_len);

		if (!rdp->rc4_encrypt_key)
			goto fail;

		rdp->encrypt_use_count = 0;
	}

	if (!winpr_RC4_Update(rdp->rc4_encrypt_key, length, data, data))
		goto fail;

	rdp->encrypt_use_count++;
	rdp->encrypt_checksum_use_count++;
	rc = TRUE;
fail:
	LeaveCriticalSection(&rdp->critical);
	return rc;
}

Project: FreeRDP
File: security.c

BOOL security_salted_mac_signature(rdpRdp* rdp, const BYTE* data, UINT32 length, BOOL encryption,
                                   BYTE* output)
{
	WINPR_DIGEST_CTX* sha1 = NULL;
	WINPR_DIGEST_CTX* md5 = NULL;
	BYTE length_le[4];
	BYTE use_count_le[4];
	BYTE md5_digest[WINPR_MD5_DIGEST_LENGTH];
	BYTE sha1_digest[WINPR_SHA1_DIGEST_LENGTH];
	BOOL result = FALSE;
	security_UINT32_le(length_le, length); /* length must be little-endian */

	if (encryption)
	{
		security_UINT32_le(use_count_le, rdp->encrypt_checksum_use_count);
	}
	else
	{
		/*
		 * We calculate checksum on plain text, so we must have already
		 * decrypt it, which means decrypt_checksum_use_count is off by one.
		 */
		security_UINT32_le(use_count_le, rdp->decrypt_checksum_use_count - 1);
	}

	/* SHA1_Digest = SHA1(MACKeyN + pad1 + length + data) */
	if (!(sha1 = winpr_Digest_New()))
		goto out;

	if (!winpr_Digest_Init(sha1, WINPR_MD_SHA1))
		goto out;

	if (!winpr_Digest_Update(sha1, rdp->sign_key, rdp->rc4_key_len)) /* MacKeyN */
		goto out;

	if (!winpr_Digest_Update(sha1, pad1, sizeof(pad1))) /* pad1 */
		goto out;

	if (!winpr_Digest_Update(sha1, length_le, sizeof(length_le))) /* length */
		goto out;

	if (!winpr_Digest_Update(sha1, data, length)) /* data */
		goto out;

	if (!winpr_Digest_Update(sha1, use_count_le, sizeof(use_count_le))) /* encryptionCount */
		goto out;

	if (!winpr_Digest_Final(sha1, sha1_digest, sizeof(sha1_digest)))
		goto out;

	/* MACSignature = First64Bits(MD5(MACKeyN + pad2 + SHA1_Digest)) */
	if (!(md5 = winpr_Digest_New()))
		goto out;

	if (!winpr_Digest_Init(md5, WINPR_MD_MD5))
		goto out;

	if (!winpr_Digest_Update(md5, rdp->sign_key, rdp->rc4_key_len)) /* MacKeyN */
		goto out;

	if (!winpr_Digest_Update(md5, pad2, sizeof(pad2))) /* pad2 */
		goto out;

	if (!winpr_Digest_Update(md5, sha1_digest, sizeof(sha1_digest))) /* SHA1_Digest */
		goto out;

	if (!winpr_Digest_Final(md5, md5_digest, sizeof(md5_digest)))
		goto out;

	memcpy(output, md5_digest, 8);
	result = TRUE;
out:
	winpr_Digest_Free(sha1);
	winpr_Digest_Free(md5);
	return result;
}

Project: FreeRDP
File: security.c

BOOL security_session_key_blob(const BYTE* master_secret, const BYTE* client_random,
                               const BYTE* server_random, BYTE* output)
{
	/* MasterHash = MasterHash('A') + MasterHash('BB') + MasterHash('CCC') */
	return security_master_hash("A", 1, master_secret, client_random, server_random, &output[0]) &&
	       security_master_hash("BB", 2, master_secret, client_random, server_random,
	                            &output[16]) &&
	       security_master_hash("CCC", 3, master_secret, client_random, server_random, &output[32]);
}

Project: FreeRDP
File: security.c

BOOL security_fips_decrypt(BYTE* data, size_t length, rdpRdp* rdp)
{
	size_t olen;

	if (!rdp || !rdp->fips_decrypt)
		return FALSE;

	if (!winpr_Cipher_Update(rdp->fips_decrypt, data, length, data, &olen))
		return FALSE;

	return TRUE;
}

Project: FreeRDP
File: security.c

void security_mac_salt_key(const BYTE* session_key_blob, const BYTE* client_random,
                           const BYTE* server_random, BYTE* output)
{
	/* MacSaltKey = First128Bits(SessionKeyBlob) */
	memcpy(output, session_key_blob, 16);
}

Project: FreeRDP
File: security.c

BOOL security_licensing_encryption_key(const BYTE* session_key_blob, const BYTE* client_random,
                                       const BYTE* server_random, BYTE* output)
{
	/* LicensingEncryptionKey = MD5(Second128Bits(SessionKeyBlob) + ClientRandom + ServerRandom))
	 * Allow FIPS use of MD5 here, this is just used for creating the licensing encryption key as
	 * described in MS-RDPELE. This is for RDP licensing packets which will already be encrypted
	 * under FIPS, so the use of MD5 here is not for sensitive data protection. */
	return security_md5_16_32_32_Allow_FIPS(&session_key_blob[16], client_random, server_random,
	                                        output);
}

Project: FreeRDP
File: security.c

static BOOL security_key_update(BYTE* key, BYTE* update_key, int key_len, rdpRdp* rdp)
{
	BYTE sha1h[WINPR_SHA1_DIGEST_LENGTH];
	WINPR_DIGEST_CTX* sha1 = NULL;
	WINPR_DIGEST_CTX* md5 = NULL;
	WINPR_RC4_CTX* rc4 = NULL;
	BYTE salt[] = { 0xD1, 0x26, 0x9E }; /* 40 bits: 3 bytes, 56 bits: 1 byte */
	BOOL result = FALSE;
	WLog_DBG(TAG, "updating RDP key");

	if (!(sha1 = winpr_Digest_New()))
		goto out;

	if (!winpr_Digest_Init(sha1, WINPR_MD_SHA1))
		goto out;

	if (!winpr_Digest_Update(sha1, update_key, key_len))
		goto out;

	if (!winpr_Digest_Update(sha1, pad1, sizeof(pad1)))
		goto out;

	if (!winpr_Digest_Update(sha1, key, key_len))
		goto out;

	if (!winpr_Digest_Final(sha1, sha1h, sizeof(sha1h)))
		goto out;

	if (!(md5 = winpr_Digest_New()))
		goto out;

	if (!winpr_Digest_Init(md5, WINPR_MD_MD5))
		goto out;

	if (!winpr_Digest_Update(md5, update_key, key_len))
		goto out;

	if (!winpr_Digest_Update(md5, pad2, sizeof(pad2)))
		goto out;

	if (!winpr_Digest_Update(md5, sha1h, sizeof(sha1h)))
		goto out;

	if (!winpr_Digest_Final(md5, key, WINPR_MD5_DIGEST_LENGTH))
		goto out;

	if (!(rc4 = winpr_RC4_New(key, key_len)))
		goto out;

	if (!winpr_RC4_Update(rc4, key_len, key, key))
		goto out;

	if (rdp->settings->EncryptionMethods == ENCRYPTION_METHOD_40BIT)
		memcpy(key, salt, 3);
	else if (rdp->settings->EncryptionMethods == ENCRYPTION_METHOD_56BIT)
		memcpy(key, salt, 1);

	result = TRUE;
out:
	winpr_Digest_Free(sha1);
	winpr_Digest_Free(md5);
	winpr_RC4_Free(rc4);
	return result;
}

Project: FreeRDP
File: security.c

static BOOL security_master_hash(const char* input, int length, const BYTE* master_secret,
                                 const BYTE* client_random, const BYTE* server_random, BYTE* output)
{
	/* MasterHash(Input) = SaltedHash(MasterSecret, Input, ServerRandom, ClientRandom) */
	return security_salted_hash(master_secret, (const BYTE*)input, length, server_random,
	                            client_random, output);
}

Project: FreeRDP
File: security.c

static void security_UINT32_le(BYTE* output, UINT32 value)
{
	output[0] = (value)&0xFF;
	output[1] = (value >> 8) & 0xFF;
	output[2] = (value >> 16) & 0xFF;
	output[3] = (value >> 24) & 0xFF;
}

Project: FreeRDP
File: security.c

BOOL security_master_secret(const BYTE* premaster_secret, const BYTE* client_random,
                            const BYTE* server_random, BYTE* output)
{
	/* MasterSecret = PremasterHash('A') + PremasterHash('BB') + PremasterHash('CCC') */
	return security_premaster_hash("A", 1, premaster_secret, client_random, server_random,
	                               &output[0]) &&
	       security_premaster_hash("BB", 2, premaster_secret, client_random, server_random,
	                               &output[16]) &&
	       security_premaster_hash("CCC", 3, premaster_secret, client_random, server_random,
	                               &output[32]);
}

Project: FreeRDP
File: security.c

static BOOL security_X(BYTE* master_secret, const BYTE* client_random, BYTE* server_random,
                       BYTE* output)
{
	return security_premaster_hash("X", 1, master_secret, client_random, server_random,
	                               &output[0]) &&
	       security_premaster_hash("YY", 2, master_secret, client_random, server_random,
	                               &output[16]) &&
	       security_premaster_hash("ZZZ", 3, master_secret, client_random, server_random,
	                               &output[32]);
}

Project: FreeRDP
File: security.c

BOOL security_fips_encrypt(BYTE* data, size_t length, rdpRdp* rdp)
{
	BOOL rc = FALSE;
	size_t olen;

	EnterCriticalSection(&rdp->critical);
	if (!winpr_Cipher_Update(rdp->fips_encrypt, data, length, data, &olen))
		goto fail;

	rdp->encrypt_use_count++;
	rc = TRUE;
fail:
	LeaveCriticalSection(&rdp->critical);
	return rc;
}

Project: FreeRDP
File: security.c

static BOOL security_md5_16_32_32(const BYTE* in0, const BYTE* in1, const BYTE* in2, BYTE* output)
{
	WINPR_DIGEST_CTX* md5 = NULL;
	BOOL result = FALSE;

	if (!(md5 = winpr_Digest_New()))
		return FALSE;

	if (!winpr_Digest_Init(md5, WINPR_MD_MD5))
		goto out;

	if (!winpr_Digest_Update(md5, in0, 16))
		goto out;

	if (!winpr_Digest_Update(md5, in1, 32))
		goto out;

	if (!winpr_Digest_Update(md5, in2, 32))
		goto out;

	if (!winpr_Digest_Final(md5, output, WINPR_MD5_DIGEST_LENGTH))
		goto out;

	result = TRUE;
out:
	winpr_Digest_Free(md5);
	return result;
}