--- sourceforge.net/trunk/rdesktop/secure.c 2001/01/06 03:12:10 24 +++ sourceforge.net/trunk/rdesktop/secure.c 2001/06/20 13:54:48 28 @@ -28,6 +28,8 @@ extern int width; extern int height; extern int keylayout; +extern BOOL use_encryption; +extern BOOL licence_issued; static int rc4_key_len; static RC4_KEY rc4_decrypt_key; @@ -45,8 +47,8 @@ * a client and server salt) and a global salt value used for padding. * Both SHA1 and MD5 algorithms are used. */ -void sec_hash_48(uint8 *out, uint8 *in, uint8 *salt1, uint8 *salt2, - uint8 salt) +void +sec_hash_48(uint8 *out, uint8 *in, uint8 *salt1, uint8 *salt2, uint8 salt) { uint8 shasig[20]; uint8 pad[4]; @@ -76,7 +78,8 @@ * Weaker 16-byte transformation, also using two 32-byte salts, but * only using a single round of MD5. */ -void sec_hash_16(uint8 *out, uint8 *in, uint8 *salt1, uint8 *salt2) +void +sec_hash_16(uint8 *out, uint8 *in, uint8 *salt1, uint8 *salt2) { MD5_CTX md5; @@ -88,7 +91,8 @@ } /* Reduce key entropy from 64 to 40 bits */ -static void sec_make_40bit(uint8 *key) +static void +sec_make_40bit(uint8 *key) { key[0] = 0xd1; key[1] = 0x26; @@ -96,8 +100,8 @@ } /* 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) +static void +sec_generate_keys(uint8 *client_key, uint8 *server_key, int rc4_key_size) { uint8 session_key[48]; uint8 temp_hash[48]; @@ -145,20 +149,21 @@ static uint8 pad_54[40] = { 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, - 54, 54, 54, + 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, - 54, 54, 54 + 54, 54, 54 }; static uint8 pad_92[48] = { 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, - 92, 92, 92, 92, 92, 92, 92, + 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, - 92, 92, 92, 92, 92, 92, 92 + 92, 92, 92, 92, 92, 92, 92 }; /* Output a uint32 into a buffer (little-endian) */ -void buf_out_uint32(uint8 *buffer, uint32 value) +void +buf_out_uint32(uint8 *buffer, uint32 value) { buffer[0] = (value) & 0xff; buffer[1] = (value >> 8) & 0xff; @@ -167,8 +172,9 @@ } /* 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) +void +sec_sign(uint8 *signature, uint8 *session_key, int length, + uint8 *data, int datalen) { uint8 shasig[20]; uint8 md5sig[16]; @@ -195,7 +201,8 @@ } /* Update an encryption key - similar to the signing process */ -static void sec_update(uint8 *key, uint8 *update_key) +static void +sec_update(uint8 *key, uint8 *update_key) { uint8 shasig[20]; SHA_CTX sha; @@ -222,7 +229,8 @@ } /* Encrypt data using RC4 */ -static void sec_encrypt(uint8 *data, int length) +static void +sec_encrypt(uint8 *data, int length) { static int use_count; @@ -238,7 +246,8 @@ } /* Decrypt data using RC4 */ -static void sec_decrypt(uint8 *data, int length) +static void +sec_decrypt(uint8 *data, int length) { static int use_count; @@ -254,7 +263,8 @@ } /* Read in a NUMBER from a buffer */ -static void sec_read_number(NUMBER * num, uint8 *buffer, int len) +static void +sec_read_number(NUMBER * num, uint8 *buffer, int len) { INT *data = num->n_part; int i, j; @@ -266,7 +276,8 @@ } /* Write a NUMBER to a buffer */ -static void sec_write_number(NUMBER * num, uint8 *buffer, int len) +static void +sec_write_number(NUMBER * num, uint8 *buffer, int len) { INT *data = num->n_part; int i, j; @@ -279,8 +290,9 @@ } /* Perform an RSA public key encryption operation */ -static void sec_rsa_encrypt(uint8 *out, uint8 *in, int len, - uint8 *modulus, uint8 *exponent) +static void +sec_rsa_encrypt(uint8 *out, uint8 *in, int len, + uint8 *modulus, uint8 *exponent) { NUMBER data, key; @@ -296,12 +308,16 @@ } /* Initialise secure transport packet */ -STREAM sec_init(uint32 flags, int maxlen) +STREAM +sec_init(uint32 flags, int maxlen) { int hdrlen; STREAM s; - hdrlen = (flags & SEC_ENCRYPT) ? 12 : 4; + if (!licence_issued) + hdrlen = (flags & SEC_ENCRYPT) ? 12 : 4; + else + hdrlen = (flags & SEC_ENCRYPT) ? 12 : 0; s = mcs_init(maxlen + hdrlen); s_push_layer(s, sec_hdr, hdrlen); @@ -309,12 +325,14 @@ } /* Transmit secure transport packet */ -void sec_send(STREAM s, uint32 flags) +void +sec_send(STREAM s, uint32 flags) { int datalen; s_pop_layer(s, sec_hdr); - out_uint32_le(s, flags); + if (!licence_issued || (flags & SEC_ENCRYPT)) + out_uint32_le(s, flags); if (flags & SEC_ENCRYPT) { @@ -334,7 +352,8 @@ } /* Transfer the client random to the server */ -static void sec_establish_key() +static void +sec_establish_key() { uint32 length = SEC_MODULUS_SIZE + SEC_PADDING_SIZE; uint32 flags = SEC_CLIENT_RANDOM; @@ -351,7 +370,8 @@ } /* Output connect initial data blob */ -static void sec_out_mcs_data(STREAM s) +static void +sec_out_mcs_data(STREAM s) { int hostlen = 2 * strlen(hostname); @@ -398,12 +418,13 @@ /* Client encryption settings */ out_uint16_le(s, SEC_TAG_CLI_CRYPT); out_uint16(s, 8); /* length */ - out_uint32_le(s, 1); /* encryption enabled */ + out_uint32_le(s, use_encryption ? 1 : 0); /* encryption enabled */ s_mark_end(s); } /* Parse a public key structure */ -static BOOL sec_parse_public_key(STREAM s, uint8 **modulus, uint8 **exponent) +static BOOL +sec_parse_public_key(STREAM s, uint8 **modulus, uint8 **exponent) { uint32 magic, modulus_len; @@ -430,9 +451,9 @@ } /* Parse a crypto information structure */ -static BOOL sec_parse_crypt_info(STREAM s, uint32 *rc4_key_size, - uint8 **server_random, uint8 **modulus, - uint8 **exponent) +static BOOL +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; @@ -490,7 +511,8 @@ } /* Process crypto information blob */ -static void sec_process_crypt_info(STREAM s) +static void +sec_process_crypt_info(STREAM s) { uint8 *server_random, *modulus, *exponent; uint8 client_random[SEC_RANDOM_SIZE]; @@ -508,7 +530,8 @@ } /* Process connect response data blob */ -static void sec_process_mcs_data(STREAM s) +static void +sec_process_mcs_data(STREAM s) { uint16 tag, length; uint8 *next_tag; @@ -544,25 +567,29 @@ } /* Receive secure transport packet */ -STREAM sec_recv() +STREAM +sec_recv() { uint32 sec_flags; STREAM s; while ((s = mcs_recv()) != NULL) { - in_uint32_le(s, sec_flags); - - if (sec_flags & SEC_LICENCE_NEG) + if (use_encryption || !licence_issued) { - licence_process(s); - continue; - } + 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) + { + licence_process(s); + continue; + } + + if (sec_flags & SEC_ENCRYPT) + { + in_uint8s(s, 8); /* signature */ + sec_decrypt(s->p, s->end - s->p); + } } return s; @@ -572,7 +599,8 @@ } /* Establish a secure connection */ -BOOL sec_connect(char *server) +BOOL +sec_connect(char *server) { struct stream mcs_data; @@ -585,12 +613,14 @@ return False; sec_process_mcs_data(&mcs_data); - sec_establish_key(); + if (use_encryption) + sec_establish_key(); return True; } /* Disconnect a connection */ -void sec_disconnect() +void +sec_disconnect() { mcs_disconnect(); }