--- sourceforge.net/trunk/rdesktop/secure.c 2001/09/15 09:37:17 32 +++ sourceforge.net/trunk/rdesktop/secure.c 2002/09/26 14:26:46 207 @@ -1,7 +1,7 @@ /* rdesktop: A Remote Desktop Protocol client. Protocol services - RDP encryption and licensing - Copyright (C) Matthew Chapman 1999-2001 + Copyright (C) Matthew Chapman 1999-2002 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -43,12 +43,12 @@ static RC4_KEY rc4_decrypt_key; static RC4_KEY rc4_encrypt_key; -static uint8 sec_sign_key[8]; +static uint8 sec_sign_key[16]; static uint8 sec_decrypt_key[16]; static uint8 sec_encrypt_key[16]; -static uint8 sec_decrypt_update_key[8]; -static uint8 sec_encrypt_update_key[8]; -static uint8 sec_crypted_random[64]; +static uint8 sec_decrypt_update_key[16]; +static uint8 sec_encrypt_update_key[16]; +static uint8 sec_crypted_random[SEC_MODULUS_SIZE]; /* * General purpose 48-byte transformation, using two 32-byte salts (generally, @@ -56,7 +56,7 @@ * Both SHA1 and MD5 algorithms are used. */ void -sec_hash_48(uint8 *out, uint8 *in, uint8 *salt1, uint8 *salt2, uint8 salt) +sec_hash_48(uint8 * out, uint8 * in, uint8 * salt1, uint8 * salt2, uint8 salt) { uint8 shasig[20]; uint8 pad[4]; @@ -87,7 +87,7 @@ * only using a single round of MD5. */ void -sec_hash_16(uint8 *out, uint8 *in, uint8 *salt1, uint8 *salt2) +sec_hash_16(uint8 * out, uint8 * in, uint8 * salt1, uint8 * salt2) { MD5_CTX md5; @@ -100,7 +100,7 @@ /* Reduce key entropy from 64 to 40 bits */ static void -sec_make_40bit(uint8 *key) +sec_make_40bit(uint8 * key) { key[0] = 0xd1; key[1] = 0x26; @@ -109,7 +109,7 @@ /* Generate a session key and RC4 keys, given client and server randoms */ static void -sec_generate_keys(uint8 *client_key, uint8 *server_key, int rc4_key_size) +sec_generate_keys(uint8 * client_key, uint8 * server_key, int rc4_key_size) { uint8 session_key[48]; uint8 temp_hash[48]; @@ -123,14 +123,12 @@ sec_hash_48(temp_hash, input, client_key, server_key, 65); sec_hash_48(session_key, temp_hash, client_key, server_key, 88); - /* Store first 8 bytes of session key, for generating signatures */ - memcpy(sec_sign_key, session_key, 8); + /* Store first 16 bytes of session key, for generating signatures */ + memcpy(sec_sign_key, session_key, 16); /* Generate RC4 keys */ - sec_hash_16(sec_decrypt_key, &session_key[16], client_key, - server_key); - sec_hash_16(sec_encrypt_key, &session_key[32], client_key, - server_key); + sec_hash_16(sec_decrypt_key, &session_key[16], client_key, server_key); + sec_hash_16(sec_encrypt_key, &session_key[32], client_key, server_key); if (rc4_key_size == 1) { @@ -146,9 +144,9 @@ rc4_key_len = 16; } - /* Store first 8 bytes of RC4 keys as update keys */ - memcpy(sec_decrypt_update_key, sec_decrypt_key, 8); - memcpy(sec_encrypt_update_key, sec_encrypt_key, 8); + /* Save initial RC4 keys as update keys */ + memcpy(sec_decrypt_update_key, sec_decrypt_key, 16); + memcpy(sec_encrypt_update_key, sec_encrypt_key, 16); /* Initialise RC4 state arrays */ RC4_set_key(&rc4_decrypt_key, rc4_key_len, sec_decrypt_key); @@ -171,7 +169,7 @@ /* Output a uint32 into a buffer (little-endian) */ void -buf_out_uint32(uint8 *buffer, uint32 value) +buf_out_uint32(uint8 * buffer, uint32 value) { buffer[0] = (value) & 0xff; buffer[1] = (value >> 8) & 0xff; @@ -181,8 +179,7 @@ /* Generate a signature hash, using a combination of SHA1 and MD5 */ void -sec_sign(uint8 *signature, uint8 *session_key, int length, - uint8 *data, int datalen) +sec_sign(uint8 * signature, int siglen, uint8 * session_key, int keylen, uint8 * data, int datalen) { uint8 shasig[20]; uint8 md5sig[16]; @@ -193,24 +190,24 @@ buf_out_uint32(lenhdr, datalen); SHA1_Init(&sha); - SHA1_Update(&sha, session_key, length); + SHA1_Update(&sha, session_key, keylen); SHA1_Update(&sha, pad_54, 40); SHA1_Update(&sha, lenhdr, 4); SHA1_Update(&sha, data, datalen); SHA1_Final(shasig, &sha); MD5_Init(&md5); - MD5_Update(&md5, session_key, length); + MD5_Update(&md5, session_key, keylen); MD5_Update(&md5, pad_92, 48); MD5_Update(&md5, shasig, 20); MD5_Final(md5sig, &md5); - memcpy(signature, md5sig, length); + memcpy(signature, md5sig, siglen); } /* Update an encryption key - similar to the signing process */ static void -sec_update(uint8 *key, uint8 *update_key) +sec_update(uint8 * key, uint8 * update_key) { uint8 shasig[20]; SHA_CTX sha; @@ -218,13 +215,13 @@ RC4_KEY update; SHA1_Init(&sha); - SHA1_Update(&sha, update_key, 8); + SHA1_Update(&sha, update_key, rc4_key_len); SHA1_Update(&sha, pad_54, 40); - SHA1_Update(&sha, key, 8); + SHA1_Update(&sha, key, rc4_key_len); SHA1_Final(shasig, &sha); MD5_Init(&md5); - MD5_Update(&md5, update_key, 8); + MD5_Update(&md5, update_key, rc4_key_len); MD5_Update(&md5, pad_92, 48); MD5_Update(&md5, shasig, 20); MD5_Final(key, &md5); @@ -238,7 +235,7 @@ /* Encrypt data using RC4 */ static void -sec_encrypt(uint8 *data, int length) +sec_encrypt(uint8 * data, int length) { static int use_count; @@ -255,7 +252,7 @@ /* Decrypt data using RC4 */ static void -sec_decrypt(uint8 *data, int length) +sec_decrypt(uint8 * data, int length) { static int use_count; @@ -271,12 +268,12 @@ } static void -reverse(uint8 *p, int len) +reverse(uint8 * p, int len) { int i, j; uint8 temp; - for (i = 0, j = len-1; i < j; i++, j--) + for (i = 0, j = len - 1; i < j; i++, j--) { temp = p[i]; p[i] = p[j]; @@ -286,8 +283,7 @@ /* Perform an RSA public key encryption operation */ static void -sec_rsa_encrypt(uint8 *out, uint8 *in, int len, - uint8 *modulus, uint8 *exponent) +sec_rsa_encrypt(uint8 * out, uint8 * in, int len, uint8 * modulus, uint8 * exponent) { BN_CTX ctx; BIGNUM mod, exp, x, y; @@ -312,7 +308,7 @@ outlen = BN_bn2bin(&y, out); reverse(out, outlen); if (outlen < SEC_MODULUS_SIZE) - memset(out+outlen, 0, SEC_MODULUS_SIZE-outlen); + memset(out + outlen, 0, SEC_MODULUS_SIZE - outlen); BN_free(&y); BN_clear_free(&x); @@ -358,7 +354,7 @@ hexdump(s->p + 8, datalen); #endif - sec_sign(s->p, sec_sign_key, 8, s->p + 8, datalen); + sec_sign(s->p, 8, sec_sign_key, rc4_key_len, s->p + 8, datalen); sec_encrypt(s->p + 8, datalen); } @@ -367,7 +363,7 @@ /* Transfer the client random to the server */ static void -sec_establish_key() +sec_establish_key(void) { uint32 length = SEC_MODULUS_SIZE + SEC_PADDING_SIZE; uint32 flags = SEC_CLIENT_RANDOM; @@ -389,6 +385,9 @@ { int hostlen = 2 * strlen(hostname); + if (hostlen > 30) + hostlen = 30; + out_uint16_be(s, 5); /* unknown */ out_uint16_be(s, 0x14); out_uint8(s, 0x7c); @@ -426,19 +425,19 @@ out_uint32_le(s, 12); out_uint8s(s, 64); /* reserved? 4 + 12 doublewords */ - out_uint16(s, 0xca01); + out_uint16_le(s, 0xca01); out_uint16(s, 0); /* Client encryption settings */ out_uint16_le(s, SEC_TAG_CLI_CRYPT); - out_uint16(s, 8); /* length */ - out_uint32_le(s, encryption ? 1 : 0); /* encryption enabled */ + out_uint16_le(s, 8); /* length */ + out_uint32_le(s, encryption ? 0x3 : 0); /* encryption supported, 128-bit supported */ s_mark_end(s); } /* Parse a public key structure */ static BOOL -sec_parse_public_key(STREAM s, uint8 **modulus, uint8 **exponent) +sec_parse_public_key(STREAM s, uint8 ** modulus, uint8 ** exponent) { uint32 magic, modulus_len; @@ -466,8 +465,8 @@ /* Parse a crypto information structure */ static BOOL -sec_parse_crypt_info(STREAM s, uint32 *rc4_key_size, - uint8 **server_random, uint8 **modulus, uint8 **exponent) +sec_parse_crypt_info(STREAM s, uint32 * rc4_key_size, + uint8 ** server_random, uint8 ** modulus, uint8 ** exponent) { uint32 crypt_level, random_len, rsa_info_len; uint16 tag, length; @@ -475,6 +474,8 @@ in_uint32_le(s, *rc4_key_size); /* 1 = 40-bit, 2 = 128-bit */ in_uint32_le(s, crypt_level); /* 1 = low, 2 = medium, 3 = high */ + if (crypt_level == 0) /* no encryptation */ + return False; in_uint32_le(s, random_len); in_uint32_le(s, rsa_info_len); @@ -503,8 +504,7 @@ switch (tag) { case SEC_TAG_PUBKEY: - if (!sec_parse_public_key - (s, modulus, exponent)) + if (!sec_parse_public_key(s, modulus, exponent)) return False; break; @@ -532,14 +532,12 @@ uint8 client_random[SEC_RANDOM_SIZE]; uint32 rc4_key_size; - if (!sec_parse_crypt_info(s, &rc4_key_size, &server_random, - &modulus, &exponent)) + if (!sec_parse_crypt_info(s, &rc4_key_size, &server_random, &modulus, &exponent)) return; /* Generate a client random, and hence determine encryption keys */ generate_random(client_random); - sec_rsa_encrypt(sec_crypted_random, client_random, - SEC_RANDOM_SIZE, modulus, exponent); + sec_rsa_encrypt(sec_crypted_random, client_random, SEC_RANDOM_SIZE, modulus, exponent); sec_generate_keys(client_random, server_random, rc4_key_size); } @@ -549,8 +547,12 @@ { uint16 tag, length; uint8 *next_tag; + uint8 len; - in_uint8s(s, 23); /* header */ + in_uint8s(s, 21); /* header */ + in_uint8(s, len); + if (len & 0x80) + in_uint8(s, len); while (s->p < s->end) { @@ -582,7 +584,7 @@ /* Receive secure transport packet */ STREAM -sec_recv() +sec_recv(void) { uint32 sec_flags; STREAM s; @@ -634,7 +636,7 @@ /* Disconnect a connection */ void -sec_disconnect() +sec_disconnect(void) { mcs_disconnect(); }