1 |
/* |
/* |
2 |
rdesktop: A Remote Desktop Protocol client. |
rdesktop: A Remote Desktop Protocol client. |
3 |
Protocol services - RDP encryption and licensing |
Protocol services - RDP encryption and licensing |
4 |
Copyright (C) Matthew Chapman 1999-2000 |
Copyright (C) Matthew Chapman 1999-2001 |
5 |
|
|
6 |
This program is free software; you can redistribute it and/or modify |
This program is free software; you can redistribute it and/or modify |
7 |
it under the terms of the GNU General Public License as published by |
it under the terms of the GNU General Public License as published by |
19 |
*/ |
*/ |
20 |
|
|
21 |
#include "rdesktop.h" |
#include "rdesktop.h" |
22 |
|
|
23 |
|
#ifdef WITH_OPENSSL |
24 |
|
#include <openssl/rc4.h> |
25 |
|
#include <openssl/md5.h> |
26 |
|
#include <openssl/sha.h> |
27 |
|
#include <openssl/bn.h> |
28 |
|
#else |
29 |
#include "crypto/rc4.h" |
#include "crypto/rc4.h" |
30 |
#include "crypto/md5.h" |
#include "crypto/md5.h" |
31 |
#include "crypto/sha.h" |
#include "crypto/sha.h" |
32 |
#include "crypto/arith.h" |
#include "crypto/bn.h" |
33 |
|
#endif |
34 |
|
|
35 |
extern char hostname[16]; |
extern char hostname[16]; |
36 |
extern int width; |
extern int width; |
37 |
extern int height; |
extern int height; |
38 |
extern int keylayout; |
extern int keylayout; |
39 |
extern BOOL use_encryption; |
extern BOOL encryption; |
40 |
extern BOOL licence_issued; |
extern BOOL licence_issued; |
41 |
|
|
42 |
static int rc4_key_len; |
static int rc4_key_len; |
43 |
static RC4_KEY rc4_decrypt_key; |
static RC4_KEY rc4_decrypt_key; |
44 |
static RC4_KEY rc4_encrypt_key; |
static RC4_KEY rc4_encrypt_key; |
45 |
|
|
46 |
static uint8 sec_sign_key[8]; |
static uint8 sec_sign_key[16]; |
47 |
static uint8 sec_decrypt_key[16]; |
static uint8 sec_decrypt_key[16]; |
48 |
static uint8 sec_encrypt_key[16]; |
static uint8 sec_encrypt_key[16]; |
49 |
static uint8 sec_decrypt_update_key[8]; |
static uint8 sec_decrypt_update_key[16]; |
50 |
static uint8 sec_encrypt_update_key[8]; |
static uint8 sec_encrypt_update_key[16]; |
51 |
static uint8 sec_crypted_random[64]; |
static uint8 sec_crypted_random[SEC_MODULUS_SIZE]; |
52 |
|
|
53 |
/* |
/* |
54 |
* General purpose 48-byte transformation, using two 32-byte salts (generally, |
* General purpose 48-byte transformation, using two 32-byte salts (generally, |
56 |
* Both SHA1 and MD5 algorithms are used. |
* Both SHA1 and MD5 algorithms are used. |
57 |
*/ |
*/ |
58 |
void |
void |
59 |
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) |
60 |
{ |
{ |
61 |
uint8 shasig[20]; |
uint8 shasig[20]; |
62 |
uint8 pad[4]; |
uint8 pad[4]; |
87 |
* only using a single round of MD5. |
* only using a single round of MD5. |
88 |
*/ |
*/ |
89 |
void |
void |
90 |
sec_hash_16(uint8 *out, uint8 *in, uint8 *salt1, uint8 *salt2) |
sec_hash_16(uint8 * out, uint8 * in, uint8 * salt1, uint8 * salt2) |
91 |
{ |
{ |
92 |
MD5_CTX md5; |
MD5_CTX md5; |
93 |
|
|
100 |
|
|
101 |
/* Reduce key entropy from 64 to 40 bits */ |
/* Reduce key entropy from 64 to 40 bits */ |
102 |
static void |
static void |
103 |
sec_make_40bit(uint8 *key) |
sec_make_40bit(uint8 * key) |
104 |
{ |
{ |
105 |
key[0] = 0xd1; |
key[0] = 0xd1; |
106 |
key[1] = 0x26; |
key[1] = 0x26; |
109 |
|
|
110 |
/* Generate a session key and RC4 keys, given client and server randoms */ |
/* Generate a session key and RC4 keys, given client and server randoms */ |
111 |
static void |
static void |
112 |
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) |
113 |
{ |
{ |
114 |
uint8 session_key[48]; |
uint8 session_key[48]; |
115 |
uint8 temp_hash[48]; |
uint8 temp_hash[48]; |
123 |
sec_hash_48(temp_hash, input, client_key, server_key, 65); |
sec_hash_48(temp_hash, input, client_key, server_key, 65); |
124 |
sec_hash_48(session_key, temp_hash, client_key, server_key, 88); |
sec_hash_48(session_key, temp_hash, client_key, server_key, 88); |
125 |
|
|
126 |
/* Store first 8 bytes of session key, for generating signatures */ |
/* Store first 16 bytes of session key, for generating signatures */ |
127 |
memcpy(sec_sign_key, session_key, 8); |
memcpy(sec_sign_key, session_key, 16); |
128 |
|
|
129 |
/* Generate RC4 keys */ |
/* Generate RC4 keys */ |
130 |
sec_hash_16(sec_decrypt_key, &session_key[16], client_key, |
sec_hash_16(sec_decrypt_key, &session_key[16], client_key, server_key); |
131 |
server_key); |
sec_hash_16(sec_encrypt_key, &session_key[32], client_key, server_key); |
|
sec_hash_16(sec_encrypt_key, &session_key[32], client_key, |
|
|
server_key); |
|
132 |
|
|
133 |
if (rc4_key_size == 1) |
if (rc4_key_size == 1) |
134 |
{ |
{ |
135 |
DEBUG("40-bit encryption enabled\n"); |
DEBUG(("40-bit encryption enabled\n")); |
136 |
sec_make_40bit(sec_sign_key); |
sec_make_40bit(sec_sign_key); |
137 |
sec_make_40bit(sec_decrypt_key); |
sec_make_40bit(sec_decrypt_key); |
138 |
sec_make_40bit(sec_encrypt_key); |
sec_make_40bit(sec_encrypt_key); |
140 |
} |
} |
141 |
else |
else |
142 |
{ |
{ |
143 |
DEBUG("128-bit encryption enabled\n"); |
DEBUG(("128-bit encryption enabled\n")); |
144 |
rc4_key_len = 16; |
rc4_key_len = 16; |
145 |
} |
} |
146 |
|
|
147 |
/* Store first 8 bytes of RC4 keys as update keys */ |
/* Save initial RC4 keys as update keys */ |
148 |
memcpy(sec_decrypt_update_key, sec_decrypt_key, 8); |
memcpy(sec_decrypt_update_key, sec_decrypt_key, 16); |
149 |
memcpy(sec_encrypt_update_key, sec_encrypt_key, 8); |
memcpy(sec_encrypt_update_key, sec_encrypt_key, 16); |
150 |
|
|
151 |
/* Initialise RC4 state arrays */ |
/* Initialise RC4 state arrays */ |
152 |
RC4_set_key(&rc4_decrypt_key, rc4_key_len, sec_decrypt_key); |
RC4_set_key(&rc4_decrypt_key, rc4_key_len, sec_decrypt_key); |
169 |
|
|
170 |
/* Output a uint32 into a buffer (little-endian) */ |
/* Output a uint32 into a buffer (little-endian) */ |
171 |
void |
void |
172 |
buf_out_uint32(uint8 *buffer, uint32 value) |
buf_out_uint32(uint8 * buffer, uint32 value) |
173 |
{ |
{ |
174 |
buffer[0] = (value) & 0xff; |
buffer[0] = (value) & 0xff; |
175 |
buffer[1] = (value >> 8) & 0xff; |
buffer[1] = (value >> 8) & 0xff; |
179 |
|
|
180 |
/* Generate a signature hash, using a combination of SHA1 and MD5 */ |
/* Generate a signature hash, using a combination of SHA1 and MD5 */ |
181 |
void |
void |
182 |
sec_sign(uint8 *signature, uint8 *session_key, int length, |
sec_sign(uint8 * signature, int siglen, uint8 * session_key, int keylen, uint8 * data, int datalen) |
|
uint8 *data, int datalen) |
|
183 |
{ |
{ |
184 |
uint8 shasig[20]; |
uint8 shasig[20]; |
185 |
uint8 md5sig[16]; |
uint8 md5sig[16]; |
190 |
buf_out_uint32(lenhdr, datalen); |
buf_out_uint32(lenhdr, datalen); |
191 |
|
|
192 |
SHA1_Init(&sha); |
SHA1_Init(&sha); |
193 |
SHA1_Update(&sha, session_key, length); |
SHA1_Update(&sha, session_key, keylen); |
194 |
SHA1_Update(&sha, pad_54, 40); |
SHA1_Update(&sha, pad_54, 40); |
195 |
SHA1_Update(&sha, lenhdr, 4); |
SHA1_Update(&sha, lenhdr, 4); |
196 |
SHA1_Update(&sha, data, datalen); |
SHA1_Update(&sha, data, datalen); |
197 |
SHA1_Final(shasig, &sha); |
SHA1_Final(shasig, &sha); |
198 |
|
|
199 |
MD5_Init(&md5); |
MD5_Init(&md5); |
200 |
MD5_Update(&md5, session_key, length); |
MD5_Update(&md5, session_key, keylen); |
201 |
MD5_Update(&md5, pad_92, 48); |
MD5_Update(&md5, pad_92, 48); |
202 |
MD5_Update(&md5, shasig, 20); |
MD5_Update(&md5, shasig, 20); |
203 |
MD5_Final(md5sig, &md5); |
MD5_Final(md5sig, &md5); |
204 |
|
|
205 |
memcpy(signature, md5sig, length); |
memcpy(signature, md5sig, siglen); |
206 |
} |
} |
207 |
|
|
208 |
/* Update an encryption key - similar to the signing process */ |
/* Update an encryption key - similar to the signing process */ |
209 |
static void |
static void |
210 |
sec_update(uint8 *key, uint8 *update_key) |
sec_update(uint8 * key, uint8 * update_key) |
211 |
{ |
{ |
212 |
uint8 shasig[20]; |
uint8 shasig[20]; |
213 |
SHA_CTX sha; |
SHA_CTX sha; |
215 |
RC4_KEY update; |
RC4_KEY update; |
216 |
|
|
217 |
SHA1_Init(&sha); |
SHA1_Init(&sha); |
218 |
SHA1_Update(&sha, update_key, 8); |
SHA1_Update(&sha, update_key, rc4_key_len); |
219 |
SHA1_Update(&sha, pad_54, 40); |
SHA1_Update(&sha, pad_54, 40); |
220 |
SHA1_Update(&sha, key, 8); |
SHA1_Update(&sha, key, rc4_key_len); |
221 |
SHA1_Final(shasig, &sha); |
SHA1_Final(shasig, &sha); |
222 |
|
|
223 |
MD5_Init(&md5); |
MD5_Init(&md5); |
224 |
MD5_Update(&md5, update_key, 8); |
MD5_Update(&md5, update_key, rc4_key_len); |
225 |
MD5_Update(&md5, pad_92, 48); |
MD5_Update(&md5, pad_92, 48); |
226 |
MD5_Update(&md5, shasig, 20); |
MD5_Update(&md5, shasig, 20); |
227 |
MD5_Final(key, &md5); |
MD5_Final(key, &md5); |
235 |
|
|
236 |
/* Encrypt data using RC4 */ |
/* Encrypt data using RC4 */ |
237 |
static void |
static void |
238 |
sec_encrypt(uint8 *data, int length) |
sec_encrypt(uint8 * data, int length) |
239 |
{ |
{ |
240 |
static int use_count; |
static int use_count; |
241 |
|
|
252 |
|
|
253 |
/* Decrypt data using RC4 */ |
/* Decrypt data using RC4 */ |
254 |
static void |
static void |
255 |
sec_decrypt(uint8 *data, int length) |
sec_decrypt(uint8 * data, int length) |
256 |
{ |
{ |
257 |
static int use_count; |
static int use_count; |
258 |
|
|
267 |
use_count++; |
use_count++; |
268 |
} |
} |
269 |
|
|
|
/* Read in a NUMBER from a buffer */ |
|
|
static void |
|
|
sec_read_number(NUMBER * num, uint8 *buffer, int len) |
|
|
{ |
|
|
INT *data = num->n_part; |
|
|
int i, j; |
|
|
|
|
|
for (i = 0, j = 0; j < len; i++, j += 2) |
|
|
data[i] = buffer[j] | (buffer[j + 1] << 8); |
|
|
|
|
|
num->n_len = i; |
|
|
} |
|
|
|
|
|
/* Write a NUMBER to a buffer */ |
|
270 |
static void |
static void |
271 |
sec_write_number(NUMBER * num, uint8 *buffer, int len) |
reverse(uint8 * p, int len) |
272 |
{ |
{ |
|
INT *data = num->n_part; |
|
273 |
int i, j; |
int i, j; |
274 |
|
uint8 temp; |
275 |
|
|
276 |
for (i = 0, j = 0; j < len; i++, j += 2) |
for (i = 0, j = len - 1; i < j; i++, j--) |
277 |
{ |
{ |
278 |
buffer[j] = data[i] & 0xff; |
temp = p[i]; |
279 |
buffer[j + 1] = data[i] >> 8; |
p[i] = p[j]; |
280 |
|
p[j] = temp; |
281 |
} |
} |
282 |
} |
} |
283 |
|
|
284 |
/* Perform an RSA public key encryption operation */ |
/* Perform an RSA public key encryption operation */ |
285 |
static void |
static void |
286 |
sec_rsa_encrypt(uint8 *out, uint8 *in, int len, |
sec_rsa_encrypt(uint8 * out, uint8 * in, int len, uint8 * modulus, uint8 * exponent) |
|
uint8 *modulus, uint8 *exponent) |
|
287 |
{ |
{ |
288 |
NUMBER data, key; |
BN_CTX ctx; |
289 |
|
BIGNUM mod, exp, x, y; |
290 |
/* Set modulus for arithmetic */ |
uint8 inr[SEC_MODULUS_SIZE]; |
291 |
sec_read_number(&key, modulus, SEC_MODULUS_SIZE); |
int outlen; |
292 |
m_init(&key, NULL); |
|
293 |
|
reverse(modulus, SEC_MODULUS_SIZE); |
294 |
/* Exponentiate */ |
reverse(exponent, SEC_EXPONENT_SIZE); |
295 |
sec_read_number(&data, in, len); |
memcpy(inr, in, len); |
296 |
sec_read_number(&key, exponent, SEC_EXPONENT_SIZE); |
reverse(inr, len); |
297 |
m_exp(&data, &key, &data); |
|
298 |
sec_write_number(&data, out, SEC_MODULUS_SIZE); |
BN_CTX_init(&ctx); |
299 |
|
BN_init(&mod); |
300 |
|
BN_init(&exp); |
301 |
|
BN_init(&x); |
302 |
|
BN_init(&y); |
303 |
|
|
304 |
|
BN_bin2bn(modulus, SEC_MODULUS_SIZE, &mod); |
305 |
|
BN_bin2bn(exponent, SEC_EXPONENT_SIZE, &exp); |
306 |
|
BN_bin2bn(inr, len, &x); |
307 |
|
BN_mod_exp(&y, &x, &exp, &mod, &ctx); |
308 |
|
outlen = BN_bn2bin(&y, out); |
309 |
|
reverse(out, outlen); |
310 |
|
if (outlen < SEC_MODULUS_SIZE) |
311 |
|
memset(out + outlen, 0, SEC_MODULUS_SIZE - outlen); |
312 |
|
|
313 |
|
BN_free(&y); |
314 |
|
BN_clear_free(&x); |
315 |
|
BN_free(&exp); |
316 |
|
BN_free(&mod); |
317 |
|
BN_CTX_free(&ctx); |
318 |
} |
} |
319 |
|
|
320 |
/* Initialise secure transport packet */ |
/* Initialise secure transport packet */ |
349 |
flags &= ~SEC_ENCRYPT; |
flags &= ~SEC_ENCRYPT; |
350 |
datalen = s->end - s->p - 8; |
datalen = s->end - s->p - 8; |
351 |
|
|
352 |
#if RDP_DEBUG |
#if WITH_DEBUG |
353 |
DEBUG("Sending encrypted packet:\n"); |
DEBUG(("Sending encrypted packet:\n")); |
354 |
hexdump(s->p + 8, datalen); |
hexdump(s->p + 8, datalen); |
355 |
#endif |
#endif |
356 |
|
|
357 |
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); |
358 |
sec_encrypt(s->p + 8, datalen); |
sec_encrypt(s->p + 8, datalen); |
359 |
} |
} |
360 |
|
|
428 |
/* Client encryption settings */ |
/* Client encryption settings */ |
429 |
out_uint16_le(s, SEC_TAG_CLI_CRYPT); |
out_uint16_le(s, SEC_TAG_CLI_CRYPT); |
430 |
out_uint16(s, 8); /* length */ |
out_uint16(s, 8); /* length */ |
431 |
out_uint32_le(s, use_encryption ? 1 : 0); /* encryption enabled */ |
out_uint32_le(s, encryption ? 0x3 : 0); /* encryption supported, 128-bit supported */ |
432 |
s_mark_end(s); |
s_mark_end(s); |
433 |
} |
} |
434 |
|
|
435 |
/* Parse a public key structure */ |
/* Parse a public key structure */ |
436 |
static BOOL |
static BOOL |
437 |
sec_parse_public_key(STREAM s, uint8 **modulus, uint8 **exponent) |
sec_parse_public_key(STREAM s, uint8 ** modulus, uint8 ** exponent) |
438 |
{ |
{ |
439 |
uint32 magic, modulus_len; |
uint32 magic, modulus_len; |
440 |
|
|
441 |
in_uint32_le(s, magic); |
in_uint32_le(s, magic); |
442 |
if (magic != SEC_RSA_MAGIC) |
if (magic != SEC_RSA_MAGIC) |
443 |
{ |
{ |
444 |
ERROR("RSA magic 0x%x\n", magic); |
error("RSA magic 0x%x\n", magic); |
445 |
return False; |
return False; |
446 |
} |
} |
447 |
|
|
448 |
in_uint32_le(s, modulus_len); |
in_uint32_le(s, modulus_len); |
449 |
if (modulus_len != SEC_MODULUS_SIZE + SEC_PADDING_SIZE) |
if (modulus_len != SEC_MODULUS_SIZE + SEC_PADDING_SIZE) |
450 |
{ |
{ |
451 |
ERROR("modulus len 0x%x\n", modulus_len); |
error("modulus len 0x%x\n", modulus_len); |
452 |
return False; |
return False; |
453 |
} |
} |
454 |
|
|
462 |
|
|
463 |
/* Parse a crypto information structure */ |
/* Parse a crypto information structure */ |
464 |
static BOOL |
static BOOL |
465 |
sec_parse_crypt_info(STREAM s, uint32 *rc4_key_size, |
sec_parse_crypt_info(STREAM s, uint32 * rc4_key_size, |
466 |
uint8 **server_random, uint8 **modulus, uint8 **exponent) |
uint8 ** server_random, uint8 ** modulus, uint8 ** exponent) |
467 |
{ |
{ |
468 |
uint32 crypt_level, random_len, rsa_info_len; |
uint32 crypt_level, random_len, rsa_info_len; |
469 |
uint16 tag, length; |
uint16 tag, length; |
476 |
|
|
477 |
if (random_len != SEC_RANDOM_SIZE) |
if (random_len != SEC_RANDOM_SIZE) |
478 |
{ |
{ |
479 |
ERROR("random len %d\n", random_len); |
error("random len %d\n", random_len); |
480 |
return False; |
return False; |
481 |
} |
} |
482 |
|
|
499 |
switch (tag) |
switch (tag) |
500 |
{ |
{ |
501 |
case SEC_TAG_PUBKEY: |
case SEC_TAG_PUBKEY: |
502 |
if (!sec_parse_public_key |
if (!sec_parse_public_key(s, modulus, exponent)) |
|
(s, modulus, exponent)) |
|
503 |
return False; |
return False; |
504 |
|
|
505 |
break; |
break; |
510 |
break; |
break; |
511 |
|
|
512 |
default: |
default: |
513 |
NOTIMP("crypt tag 0x%x\n", tag); |
unimpl("crypt tag 0x%x\n", tag); |
514 |
} |
} |
515 |
|
|
516 |
s->p = next_tag; |
s->p = next_tag; |
527 |
uint8 client_random[SEC_RANDOM_SIZE]; |
uint8 client_random[SEC_RANDOM_SIZE]; |
528 |
uint32 rc4_key_size; |
uint32 rc4_key_size; |
529 |
|
|
530 |
if (!sec_parse_crypt_info(s, &rc4_key_size, &server_random, |
if (!sec_parse_crypt_info(s, &rc4_key_size, &server_random, &modulus, &exponent)) |
|
&modulus, &exponent)) |
|
531 |
return; |
return; |
532 |
|
|
533 |
/* Generate a client random, and hence determine encryption keys */ |
/* Generate a client random, and hence determine encryption keys */ |
534 |
generate_random(client_random); |
generate_random(client_random); |
535 |
sec_rsa_encrypt(sec_crypted_random, client_random, |
sec_rsa_encrypt(sec_crypted_random, client_random, SEC_RANDOM_SIZE, modulus, exponent); |
|
SEC_RANDOM_SIZE, modulus, exponent); |
|
536 |
sec_generate_keys(client_random, server_random, rc4_key_size); |
sec_generate_keys(client_random, server_random, rc4_key_size); |
537 |
} |
} |
538 |
|
|
566 |
break; |
break; |
567 |
|
|
568 |
default: |
default: |
569 |
NOTIMP("response tag 0x%x\n", tag); |
unimpl("response tag 0x%x\n", tag); |
570 |
} |
} |
571 |
|
|
572 |
s->p = next_tag; |
s->p = next_tag; |
582 |
|
|
583 |
while ((s = mcs_recv()) != NULL) |
while ((s = mcs_recv()) != NULL) |
584 |
{ |
{ |
585 |
if (use_encryption || !licence_issued) |
if (encryption || !licence_issued) |
586 |
{ |
{ |
587 |
in_uint32_le(s, sec_flags); |
in_uint32_le(s, sec_flags); |
588 |
|
|
620 |
return False; |
return False; |
621 |
|
|
622 |
sec_process_mcs_data(&mcs_data); |
sec_process_mcs_data(&mcs_data); |
623 |
if (use_encryption) |
if (encryption) |
624 |
sec_establish_key(); |
sec_establish_key(); |
625 |
return True; |
return True; |
626 |
} |
} |