CVS: rdesktop constants.h, 1.46, 1.47 secure.c, 1.56, 1.57

Matt Chapman <[email protected]> Wed, 14 Jun 2006 01:26:02 -0700
Newsgroups gmane.network.rdesktop.cvs
Message-ID <[email protected]>
Update of /cvsroot/rdesktop/rdesktop
In directory sc8-pr-cvs9.sourceforge.net:/tmp/cvs-serv16689

Modified Files:
	constants.h secure.c 
Log Message:
Allow server public key to be anywhere from 512 bits to 2048 bits.
(Vista Beta 2 uses a 2048 bit key)


Index: constants.h
===================================================================
RCS file: /cvsroot/rdesktop/rdesktop/constants.h,v
retrieving revision 1.46
retrieving revision 1.47
diff -C2 -d -r1.46 -r1.47
*** constants.h	27 Mar 2006 08:17:33 -0000	1.46
--- constants.h	14 Jun 2006 08:26:00 -0000	1.47
***************
*** 63,66 ****
--- 63,67 ----
  #define SEC_RANDOM_SIZE		32
  #define SEC_MODULUS_SIZE	64
+ #define SEC_MAX_MODULUS_SIZE	256
  #define SEC_PADDING_SIZE	8
  #define SEC_EXPONENT_SIZE	4

Index: secure.c
===================================================================
RCS file: /cvsroot/rdesktop/rdesktop/secure.c,v
retrieving revision 1.56
retrieving revision 1.57
diff -C2 -d -r1.56 -r1.57
*** secure.c	27 Mar 2006 08:17:33 -0000	1.56
--- secure.c	14 Jun 2006 08:26:00 -0000	1.57
***************
*** 47,50 ****
--- 47,51 ----
  static RC4_KEY rc4_encrypt_key;
  static RSA *server_public_key;
+ static uint32 server_public_key_len;
  
  static uint8 sec_sign_key[16];
***************
*** 53,57 ****
  static uint8 sec_decrypt_update_key[16];
  static uint8 sec_encrypt_update_key[16];
! static uint8 sec_crypted_random[SEC_MODULUS_SIZE];
  
  uint16 g_server_rdp_version = 0;
--- 54,58 ----
  static uint8 sec_decrypt_update_key[16];
  static uint8 sec_encrypt_update_key[16];
! static uint8 sec_crypted_random[SEC_MAX_MODULUS_SIZE];
  
  uint16 g_server_rdp_version = 0;
***************
*** 298,309 ****
  /* Perform an RSA public key encryption operation */
  static void
! sec_rsa_encrypt(uint8 * out, uint8 * in, int len, uint8 * modulus, uint8 * exponent)
  {
  	BN_CTX *ctx;
  	BIGNUM mod, exp, x, y;
! 	uint8 inr[SEC_MODULUS_SIZE];
  	int outlen;
  
! 	reverse(modulus, SEC_MODULUS_SIZE);
  	reverse(exponent, SEC_EXPONENT_SIZE);
  	memcpy(inr, in, len);
--- 299,310 ----
  /* Perform an RSA public key encryption operation */
  static void
! sec_rsa_encrypt(uint8 * out, uint8 * in, int len, uint32 modulus_size, uint8 * modulus, uint8 * exponent)
  {
  	BN_CTX *ctx;
  	BIGNUM mod, exp, x, y;
! 	uint8 inr[SEC_MAX_MODULUS_SIZE];
  	int outlen;
  
! 	reverse(modulus, modulus_size);
  	reverse(exponent, SEC_EXPONENT_SIZE);
  	memcpy(inr, in, len);
***************
*** 316,320 ****
  	BN_init(&y);
  
! 	BN_bin2bn(modulus, SEC_MODULUS_SIZE, &mod);
  	BN_bin2bn(exponent, SEC_EXPONENT_SIZE, &exp);
  	BN_bin2bn(inr, len, &x);
--- 317,321 ----
  	BN_init(&y);
  
! 	BN_bin2bn(modulus, modulus_size, &mod);
  	BN_bin2bn(exponent, SEC_EXPONENT_SIZE, &exp);
  	BN_bin2bn(inr, len, &x);
***************
*** 322,327 ****
  	outlen = BN_bn2bin(&y, out);
  	reverse(out, outlen);
! 	if (outlen < SEC_MODULUS_SIZE)
! 		memset(out + outlen, 0, SEC_MODULUS_SIZE - outlen);
  
  	BN_free(&y);
--- 323,328 ----
  	outlen = BN_bn2bin(&y, out);
  	reverse(out, outlen);
! 	if (outlen < modulus_size)
! 		memset(out + outlen, 0, modulus_size - outlen);
  
  	BN_free(&y);
***************
*** 389,400 ****
  sec_establish_key(void)
  {
! 	uint32 length = SEC_MODULUS_SIZE + SEC_PADDING_SIZE;
  	uint32 flags = SEC_CLIENT_RANDOM;
  	STREAM s;
  
! 	s = sec_init(flags, 76);
  
  	out_uint32_le(s, length);
! 	out_uint8p(s, sec_crypted_random, SEC_MODULUS_SIZE);
  	out_uint8s(s, SEC_PADDING_SIZE);
  
--- 390,401 ----
  sec_establish_key(void)
  {
! 	uint32 length = server_public_key_len + SEC_PADDING_SIZE;
  	uint32 flags = SEC_CLIENT_RANDOM;
  	STREAM s;
  
! 	s = sec_init(flags, length+4);
  
  	out_uint32_le(s, length);
! 	out_uint8p(s, sec_crypted_random, server_public_key_len);
  	out_uint8s(s, SEC_PADDING_SIZE);
  
***************
*** 508,514 ****
  
  	in_uint32_le(s, modulus_len);
! 	if (modulus_len != SEC_MODULUS_SIZE + SEC_PADDING_SIZE)
  	{
! 		error("modulus len 0x%x\n", modulus_len);
  		return False;
  	}
--- 509,516 ----
  
  	in_uint32_le(s, modulus_len);
! 	modulus_len -= SEC_PADDING_SIZE;
! 	if ((modulus_len < 64) || (modulus_len > SEC_MAX_MODULUS_SIZE))
  	{
! 		error("Bad server public key size (%u bits)\n", modulus_len*8);
  		return False;
  	}
***************
*** 516,521 ****
  	in_uint8s(s, 8);	/* modulus_bits, unknown */
  	in_uint8p(s, *exponent, SEC_EXPONENT_SIZE);
! 	in_uint8p(s, *modulus, SEC_MODULUS_SIZE);
  	in_uint8s(s, SEC_PADDING_SIZE);
  
  	return s_check(s);
--- 518,524 ----
  	in_uint8s(s, 8);	/* modulus_bits, unknown */
  	in_uint8p(s, *exponent, SEC_EXPONENT_SIZE);
! 	in_uint8p(s, *modulus, modulus_len);
  	in_uint8s(s, SEC_PADDING_SIZE);
+ 	server_public_key_len = modulus_len;
  
  	return s_check(s);
***************
*** 545,551 ****
  
  	server_public_key = RSAPublicKey_dup((RSA *) epk->pkey.ptr);
- 
  	EVP_PKEY_free(epk);
  
  	return True;
  }
--- 548,560 ----
  
  	server_public_key = RSAPublicKey_dup((RSA *) epk->pkey.ptr);
  	EVP_PKEY_free(epk);
  
+ 	server_public_key_len = RSA_size(server_public_key);
+ 	if ((server_public_key_len < 64) || (server_public_key_len > SEC_MAX_MODULUS_SIZE))
+ 	{
+ 		error("Bad server public key size (%u bits)\n", server_public_key_len*8);
+ 		return False;
+ 	}
+ 
  	return True;
  }
***************
*** 720,724 ****
  	uint8 client_random[SEC_RANDOM_SIZE];
  	uint32 rc4_key_size;
- 	uint8 inr[SEC_MODULUS_SIZE];
  
  	if (!sec_parse_crypt_info(s, &rc4_key_size, &server_random, &modulus, &exponent))
--- 729,732 ----
***************
*** 729,755 ****
  
  	DEBUG(("Generating client random\n"));
- 	/* Generate a client random, and hence determine encryption keys */
- 	/* This is what the MS client do: */
- 	memset(inr, 0, SEC_RANDOM_SIZE);
- 	/*  *ARIGL!* Plaintext attack, anyone?
- 	   I tried doing:
- 	   generate_random(inr);
- 	   ..but that generates connection errors now and then (yes, 
- 	   "now and then". Something like 0 to 3 attempts needed before a 
- 	   successful connection. Nice. Not! 
- 	 */
- 
  	generate_random(client_random);
  	if (NULL != server_public_key)
  	{			/* Which means we should use 
  				   RDP5-style encryption */
  
! 		memcpy(inr + SEC_RANDOM_SIZE, client_random, SEC_RANDOM_SIZE);
! 		reverse(inr + SEC_RANDOM_SIZE, SEC_RANDOM_SIZE);
  
! 		RSA_public_encrypt(SEC_MODULUS_SIZE,
  				   inr, sec_crypted_random, server_public_key, RSA_NO_PADDING);
  
! 		reverse(sec_crypted_random, SEC_MODULUS_SIZE);
  
  		RSA_free(server_public_key);
--- 737,764 ----
  
  	DEBUG(("Generating client random\n"));
  	generate_random(client_random);
+ 
  	if (NULL != server_public_key)
  	{			/* Which means we should use 
  				   RDP5-style encryption */
+ 		uint8 inr[SEC_MAX_MODULUS_SIZE];
+ 		uint32 padding_len = server_public_key_len - SEC_RANDOM_SIZE;
  
! 		/* This is what the MS client do: */
! 		memset(inr, 0, padding_len);
! 		/*  *ARIGL!* Plaintext attack, anyone?
! 		   I tried doing:
! 	   	   generate_random(inr);
! 	   	   ..but that generates connection errors now and then (yes, 
! 	   	   "now and then". Something like 0 to 3 attempts needed before a 
! 	   	   successful connection. Nice. Not! 
! 		 */
! 		memcpy(inr + padding_len, client_random, SEC_RANDOM_SIZE);
! 		reverse(inr + padding_len, SEC_RANDOM_SIZE);
  
! 		RSA_public_encrypt(server_public_key_len,
  				   inr, sec_crypted_random, server_public_key, RSA_NO_PADDING);
  
! 		reverse(sec_crypted_random, server_public_key_len);
  
  		RSA_free(server_public_key);
***************
*** 759,763 ****
  	{			/* RDP4-style encryption */
  		sec_rsa_encrypt(sec_crypted_random,
! 				client_random, SEC_RANDOM_SIZE, modulus, exponent);
  	}
  	sec_generate_keys(client_random, server_random, rc4_key_size);
--- 768,772 ----
  	{			/* RDP4-style encryption */
  		sec_rsa_encrypt(sec_crypted_random,
! 				client_random, SEC_RANDOM_SIZE, server_public_key_len, modulus, exponent);
  	}
  	sec_generate_keys(client_random, server_random, rc4_key_size);