--- sourceforge.net/trunk/rdesktop/secure.c 2003/05/19 21:36:33 376 +++ sourceforge.net/trunk/rdesktop/secure.c 2005/08/08 19:15:57 977 @@ -1,7 +1,7 @@ /* -*- c-basic-offset: 8 -*- rdesktop: A Remote Desktop Protocol client. Protocol services - RDP encryption and licensing - Copyright (C) Matthew Chapman 1999-2002 + Copyright (C) Matthew Chapman 1999-2005 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 @@ -20,27 +20,27 @@ #include "rdesktop.h" -#ifdef WITH_OPENSSL #include #include #include #include #include -#else -#include "crypto/rc4.h" -#include "crypto/md5.h" -#include "crypto/sha.h" -#include "crypto/bn.h" -#endif -extern char hostname[16]; -extern int width; -extern int height; -extern int keylayout; -extern BOOL encryption; -extern BOOL licence_issued; -extern BOOL use_rdp5; -extern int server_bpp; +extern char g_hostname[16]; +extern int g_width; +extern int g_height; +extern int g_keylayout; +extern int g_keyboard_type; +extern int g_keyboard_subtype; +extern int g_keyboard_functionkeys; +extern BOOL g_encryption; +extern BOOL g_licence_issued; +extern BOOL g_use_rdp5; +extern BOOL g_console_session; +extern int g_server_bpp; +extern uint16 mcs_userid; +extern VCHANNEL g_channels[]; +extern unsigned int g_num_channels; static int rc4_key_len; static RC4_KEY rc4_decrypt_key; @@ -54,11 +54,24 @@ static uint8 sec_encrypt_update_key[16]; static uint8 sec_crypted_random[SEC_MODULUS_SIZE]; -uint16 server_rdp_version = 0; +uint16 g_server_rdp_version = 0; + +/* These values must be available to reset state - Session Directory */ +static int sec_encrypt_use_count = 0; +static int sec_decrypt_use_count = 0; + +/* + * I believe this is based on SSLv3 with the following differences: + * MAC algorithm (5.2.3.1) uses only 32-bit length in place of seq_num/type/length fields + * MAC algorithm uses SHA1 and MD5 for the two hash functions instead of one or other + * key_block algorithm (6.2.2) uses 'X', 'YY', 'ZZZ' instead of 'A', 'BB', 'CCC' + * key_block partitioning is different (16 bytes each: MAC secret, decrypt key, encrypt key) + * encryption/decryption keys updated every 4096 packets + * See http://wp.netscape.com/eng/ssl3/draft302.txt + */ /* - * General purpose 48-byte transformation, using two 32-byte salts (generally, - * a client and server salt) and a global salt value used for padding. + * 48-byte transformation used to generate master secret (6.1) and key material (6.2.2). * Both SHA1 and MD5 algorithms are used. */ void @@ -89,8 +102,7 @@ } /* - * Weaker 16-byte transformation, also using two 32-byte salts, but - * only using a single round of MD5. + * 16-byte transformation used to generate export keys (6.2.2). */ void sec_hash_16(uint8 * out, uint8 * in, uint8 * salt1, uint8 * salt2) @@ -113,28 +125,28 @@ key[2] = 0x9e; } -/* Generate a session key and RC4 keys, given client and server randoms */ +/* Generate encryption 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_random, uint8 * server_random, int rc4_key_size) { - uint8 session_key[48]; - uint8 temp_hash[48]; - uint8 input[48]; - - /* Construct input data to hash */ - memcpy(input, client_key, 24); - memcpy(input + 24, server_key, 24); - - /* Generate session key - two rounds of sec_hash_48 */ - 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 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); + uint8 pre_master_secret[48]; + uint8 master_secret[48]; + uint8 key_block[48]; + + /* Construct pre-master secret */ + memcpy(pre_master_secret, client_random, 24); + memcpy(pre_master_secret + 24, server_random, 24); + + /* Generate master secret and then key material */ + sec_hash_48(master_secret, pre_master_secret, client_random, server_random, 'A'); + sec_hash_48(key_block, master_secret, client_random, server_random, 'X'); + + /* First 16 bytes of key material is MAC secret */ + memcpy(sec_sign_key, key_block, 16); + + /* Generate export keys from next two blocks of 16 bytes */ + sec_hash_16(sec_decrypt_key, &key_block[16], client_random, server_random); + sec_hash_16(sec_encrypt_key, &key_block[32], client_random, server_random); if (rc4_key_size == 1) { @@ -183,7 +195,7 @@ buffer[3] = (value >> 24) & 0xff; } -/* Generate a signature hash, using a combination of SHA1 and MD5 */ +/* Generate a MAC hash (5.2.3.1), using a combination of SHA1 and MD5 */ void sec_sign(uint8 * signature, int siglen, uint8 * session_key, int keylen, uint8 * data, int datalen) { @@ -211,7 +223,7 @@ memcpy(signature, md5sig, siglen); } -/* Update an encryption key - similar to the signing process */ +/* Update an encryption key */ static void sec_update(uint8 * key, uint8 * update_key) { @@ -243,34 +255,30 @@ static void sec_encrypt(uint8 * data, int length) { - static int use_count; - - if (use_count == 4096) + if (sec_encrypt_use_count == 4096) { sec_update(sec_encrypt_key, sec_encrypt_update_key); RC4_set_key(&rc4_encrypt_key, rc4_key_len, sec_encrypt_key); - use_count = 0; + sec_encrypt_use_count = 0; } RC4(&rc4_encrypt_key, length, data, data); - use_count++; + sec_encrypt_use_count++; } /* Decrypt data using RC4 */ void sec_decrypt(uint8 * data, int length) { - static int use_count; - - if (use_count == 4096) + if (sec_decrypt_use_count == 4096) { sec_update(sec_decrypt_key, sec_decrypt_update_key); RC4_set_key(&rc4_decrypt_key, rc4_key_len, sec_decrypt_key); - use_count = 0; + sec_decrypt_use_count = 0; } RC4(&rc4_decrypt_key, length, data, data); - use_count++; + sec_decrypt_use_count++; } static void @@ -330,7 +338,7 @@ int hdrlen; STREAM s; - if (!licence_issued) + if (!g_licence_issued) hdrlen = (flags & SEC_ENCRYPT) ? 12 : 4; else hdrlen = (flags & SEC_ENCRYPT) ? 12 : 0; @@ -340,14 +348,14 @@ return s; } -/* Transmit secure transport packet */ +/* Transmit secure transport packet over specified channel */ void -sec_send(STREAM s, uint32 flags) +sec_send_to_channel(STREAM s, uint32 flags, uint16 channel) { int datalen; s_pop_layer(s, sec_hdr); - if (!licence_issued || (flags & SEC_ENCRYPT)) + if (!g_licence_issued || (flags & SEC_ENCRYPT)) out_uint32_le(s, flags); if (flags & SEC_ENCRYPT) @@ -364,9 +372,18 @@ sec_encrypt(s->p + 8, datalen); } - mcs_send(s); + mcs_send_to_channel(s, channel); +} + +/* Transmit secure transport packet */ + +void +sec_send(STREAM s, uint32 flags) +{ + sec_send_to_channel(s, flags, MCS_GLOBAL_CHANNEL); } + /* Transfer the client random to the server */ static void sec_establish_key(void) @@ -389,13 +406,18 @@ static void sec_out_mcs_data(STREAM s) { - int hostlen = 2 * strlen(hostname); - int length = 158 + 76 + 12 + 4 + 20; + int hostlen = 2 * strlen(g_hostname); + int length = 158 + 76 + 12 + 4; + unsigned int i; + + if (g_num_channels > 0) + length += g_num_channels * 12 + 8; if (hostlen > 30) hostlen = 30; - out_uint16_be(s, 5); /* unknown */ + /* Generic Conference Control (T.124) ConferenceCreateRequest */ + out_uint16_be(s, 5); out_uint16_be(s, 0x14); out_uint8(s, 0x7c); out_uint16_be(s, 1); @@ -408,49 +430,36 @@ out_uint16_le(s, 0xc001); out_uint8(s, 0); - out_uint32_le(s, 0x61637544); /* "Duca" ?! */ + out_uint32_le(s, 0x61637544); /* OEM ID: "Duca", as in Ducati. */ out_uint16_be(s, ((length - 14) | 0x8000)); /* remaining length */ /* Client information */ out_uint16_le(s, SEC_TAG_CLI_INFO); out_uint16_le(s, 212); /* length */ - out_uint16_le(s, use_rdp5 ? 4 : 1); /* RDP version. 1 == RDP4, 4 == RDP5. */ + out_uint16_le(s, g_use_rdp5 ? 4 : 1); /* RDP version. 1 == RDP4, 4 == RDP5. */ out_uint16_le(s, 8); - out_uint16_le(s, width); - out_uint16_le(s, height); + out_uint16_le(s, g_width); + out_uint16_le(s, g_height); out_uint16_le(s, 0xca01); out_uint16_le(s, 0xaa03); - out_uint32_le(s, keylayout); + out_uint32_le(s, g_keylayout); out_uint32_le(s, 2600); /* Client build. We are now 2600 compatible :-) */ /* Unicode name of client, padded to 32 bytes */ - rdp_out_unistr(s, hostname, hostlen); + rdp_out_unistr(s, g_hostname, hostlen); out_uint8s(s, 30 - hostlen); - out_uint32_le(s, 4); - out_uint32(s, 0); - out_uint32_le(s, 12); + /* See + http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wceddk40/html/cxtsksupportingremotedesktopprotocol.asp */ + out_uint32_le(s, g_keyboard_type); + out_uint32_le(s, g_keyboard_subtype); + out_uint32_le(s, g_keyboard_functionkeys); out_uint8s(s, 64); /* reserved? 4 + 12 doublewords */ - - switch (server_bpp) - { - case 8: - out_uint16_le(s, 0xca01); - break; - case 15: - out_uint16_le(s, 0xca02); - break; - case 16: - out_uint16_le(s, 0xca03); - break; - case 24: - out_uint16_le(s, 0xca04); - break; - } + out_uint16_le(s, 0xca01); /* colour depth? */ out_uint16_le(s, 1); out_uint32(s, 0); - out_uint8(s, server_bpp); + out_uint8(s, g_server_bpp); out_uint16_le(s, 0x0700); out_uint8(s, 0); out_uint32_le(s, 1); @@ -458,21 +467,28 @@ out_uint16_le(s, SEC_TAG_CLI_4); out_uint16_le(s, 12); - out_uint32_le(s, 9); + out_uint32_le(s, g_console_session ? 0xb : 9); out_uint32(s, 0); /* Client encryption settings */ out_uint16_le(s, SEC_TAG_CLI_CRYPT); out_uint16_le(s, 12); /* length */ - out_uint32_le(s, encryption ? 0x3 : 0); /* encryption supported, 128-bit supported */ + out_uint32_le(s, g_encryption ? 0x3 : 0); /* encryption supported, 128-bit supported */ out_uint32(s, 0); /* Unknown */ - out_uint16_le(s, SEC_TAG_CLI_CHANNELS); - out_uint16_le(s, 20); /* length */ - out_uint32_le(s, 1); /* number of virtual channels */ - out_uint8p(s, "cliprdr", 8); /* name padded to 8(?) */ - out_uint16(s, 0); - out_uint16_le(s, 0xc0a0); /* Flags. Rumours tell this is documented in MSDN. */ + DEBUG_RDP5(("g_num_channels is %d\n", g_num_channels)); + if (g_num_channels > 0) + { + out_uint16_le(s, SEC_TAG_CLI_CHANNELS); + out_uint16_le(s, g_num_channels * 12 + 8); /* length */ + out_uint32_le(s, g_num_channels); /* number of virtual channels */ + for (i = 0; i < g_num_channels; i++) + { + DEBUG_RDP5(("Requesting channel %s\n", g_channels[i].name)); + out_uint8a(s, g_channels[i].name, 8); + out_uint32_be(s, g_channels[i].flags); + } + } s_mark_end(s); } @@ -563,7 +579,7 @@ if (end > s->end) return False; - in_uint32_le(s, flags); /* 1 = RDP4-style, 0x80000002 = X.509 */ + in_uint32_le(s, flags); /* 1 = RDP4-style, 0x80000002 = X.509 */ if (flags & 1) { DEBUG_RDP5(("We're going for the RDP4-style encryption\n")); @@ -601,8 +617,38 @@ } else { + uint32 certcount; + DEBUG_RDP5(("We're going for the RDP5-style encryption\n")); - in_uint8s(s, 4); /* Number of certificates */ + in_uint32_le(s, certcount); /* Number of certificates */ + + if (certcount < 2) + { + error("Server didn't send enough X509 certificates\n"); + return False; + } + + for (; certcount > 2; certcount--) + { /* ignore all the certificates between the root and the signing CA */ + uint32 ignorelen; + X509 *ignorecert; + + DEBUG_RDP5(("Ignored certs left: %d\n", certcount)); + + in_uint32_le(s, ignorelen); + DEBUG_RDP5(("Ignored Certificate length is %d\n", ignorelen)); + ignorecert = d2i_X509(NULL, &(s->p), ignorelen); + + if (ignorecert == NULL) + { /* XXX: error out? */ + DEBUG_RDP5(("got a bad cert: this will probably screw up the rest of the communication\n")); + } + +#ifdef WITH_DEBUG_RDP5 + DEBUG_RDP5(("cert #%d (ignored):\n", certcount)); + X509_print_fp(stdout, ignorecert); +#endif + } /* Do da funky X.509 stuffy @@ -614,6 +660,7 @@ */ in_uint32_le(s, cacert_len); + DEBUG_RDP5(("CA Certificate length is %d\n", cacert_len)); cacert = d2i_X509(NULL, &(s->p), cacert_len); /* Note: We don't need to move s->p here - d2i_X509 is "kind" enough to do it for us */ @@ -634,6 +681,7 @@ */ in_uint32_le(s, cert_len); + DEBUG_RDP5(("Certificate length is %d\n", cert_len)); server_cert = d2i_X509(NULL, &(s->p), cert_len); if (NULL == server_cert) { @@ -674,10 +722,16 @@ DEBUG(("Generating client random\n")); /* Generate a client random, and hence determine encryption keys */ - generate_random(inr); - // This is what the MS client do: - // memset(inr, 0, SEC_RANDOM_SIZE); - // *ARIGL!* + /* 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 @@ -705,8 +759,13 @@ static void sec_process_srv_info(STREAM s) { - in_uint16_le(s, server_rdp_version); - DEBUG_RDP5(("Server RDP version is %d\n", server_rdp_version)); + in_uint16_le(s, g_server_rdp_version); + DEBUG_RDP5(("Server RDP version is %d\n", g_server_rdp_version)); + if (1 == g_server_rdp_version) + { + g_use_rdp5 = 0; + g_server_bpp = 8; + } } @@ -718,7 +777,7 @@ uint8 *next_tag; uint8 len; - in_uint8s(s, 21); /* header (T.124 stuff, probably) */ + in_uint8s(s, 21); /* header (T.124 ConferenceCreateResponse) */ in_uint8(s, len); if (len & 0x80) in_uint8(s, len); @@ -739,13 +798,16 @@ sec_process_srv_info(s); break; - case SEC_TAG_SRV_3: - break; - case SEC_TAG_SRV_CRYPT: sec_process_crypt_info(s); break; + case SEC_TAG_SRV_CHANNELS: + /* FIXME: We should parse this information and + use it to map RDP5 channels to MCS + channels */ + break; + default: unimpl("response tag 0x%x\n", tag); } @@ -756,41 +818,86 @@ /* Receive secure transport packet */ STREAM -sec_recv(void) +sec_recv(uint8 * rdpver) { uint32 sec_flags; uint16 channel; STREAM s; - while ((s = mcs_recv(&channel)) != NULL) + while ((s = mcs_recv(&channel, rdpver)) != NULL) { - if (encryption || !licence_issued) + if (rdpver != NULL) + { + if (*rdpver != 3) + { + if (*rdpver & 0x80) + { + in_uint8s(s, 8); /* signature */ + sec_decrypt(s->p, s->end - s->p); + } + return s; + } + } + if (g_encryption || !g_licence_issued) { in_uint32_le(s, sec_flags); + if (sec_flags & SEC_ENCRYPT) + { + in_uint8s(s, 8); /* signature */ + sec_decrypt(s->p, s->end - s->p); + } + if (sec_flags & SEC_LICENCE_NEG) { - if (sec_flags & SEC_ENCRYPT) { - DEBUG_RDP5(("Encrypted license detected\n")); - } licence_process(s); continue; } - if (sec_flags & SEC_ENCRYPT) + if (sec_flags & 0x0400) /* SEC_REDIRECT_ENCRYPT */ { + uint8 swapbyte; + in_uint8s(s, 8); /* signature */ sec_decrypt(s->p, s->end - s->p); + + /* Check for a redirect packet, starts with 00 04 */ + if (s->p[0] == 0 && s->p[1] == 4) + { + /* for some reason the PDU and the length seem to be swapped. + This isn't good, but we're going to do a byte for byte + swap. So the first foure value appear as: 00 04 XX YY, + where XX YY is the little endian length. We're going to + use 04 00 as the PDU type, so after our swap this will look + like: XX YY 04 00 */ + swapbyte = s->p[0]; + s->p[0] = s->p[2]; + s->p[2] = swapbyte; + + swapbyte = s->p[1]; + s->p[1] = s->p[3]; + s->p[3] = swapbyte; + + swapbyte = s->p[2]; + s->p[2] = s->p[3]; + s->p[3] = swapbyte; + } +#ifdef WITH_DEBUG + /* warning! this debug statement will show passwords in the clear! */ + hexdump(s->p, s->end - s->p); +#endif } + } - if (MCS_GLOBAL_CHANNEL == channel) + if (channel != MCS_GLOBAL_CHANNEL) { + channel_process(s, channel); + *rdpver = 0xff; return s; } - else - rdp5_process_channel(s, channel); + return s; } return NULL; @@ -804,14 +911,35 @@ /* We exchange some RDP data during the MCS-Connect */ mcs_data.size = 512; - mcs_data.p = mcs_data.data = (uint8*)xmalloc(mcs_data.size); + mcs_data.p = mcs_data.data = (uint8 *) xmalloc(mcs_data.size); sec_out_mcs_data(&mcs_data); if (!mcs_connect(server, &mcs_data, username)) return False; - // sec_process_mcs_data(&mcs_data); - if (encryption) + /* sec_process_mcs_data(&mcs_data); */ + if (g_encryption) + sec_establish_key(); + xfree(mcs_data.data); + return True; +} + +/* Establish a secure connection */ +BOOL +sec_reconnect(char *server) +{ + struct stream mcs_data; + + /* We exchange some RDP data during the MCS-Connect */ + mcs_data.size = 512; + mcs_data.p = mcs_data.data = (uint8 *) xmalloc(mcs_data.size); + sec_out_mcs_data(&mcs_data); + + if (!mcs_reconnect(server, &mcs_data)) + return False; + + /* sec_process_mcs_data(&mcs_data); */ + if (g_encryption) sec_establish_key(); xfree(mcs_data.data); return True; @@ -823,3 +951,13 @@ { mcs_disconnect(); } + +/* reset the state of the sec layer */ +void +sec_reset_state(void) +{ + g_server_rdp_version = 0; + sec_encrypt_use_count = 0; + sec_decrypt_use_count = 0; + mcs_reset_state(); +}