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; |
134 |
|
|
135 |
if (rc4_key_size == 1) |
if (rc4_key_size == 1) |
136 |
{ |
{ |
137 |
DEBUG("40-bit encryption enabled\n"); |
DEBUG(("40-bit encryption enabled\n")); |
138 |
sec_make_40bit(sec_sign_key); |
sec_make_40bit(sec_sign_key); |
139 |
sec_make_40bit(sec_decrypt_key); |
sec_make_40bit(sec_decrypt_key); |
140 |
sec_make_40bit(sec_encrypt_key); |
sec_make_40bit(sec_encrypt_key); |
142 |
} |
} |
143 |
else |
else |
144 |
{ |
{ |
145 |
DEBUG("128-bit encryption enabled\n"); |
DEBUG(("128-bit encryption enabled\n")); |
146 |
rc4_key_len = 16; |
rc4_key_len = 16; |
147 |
} |
} |
148 |
|
|
270 |
use_count++; |
use_count++; |
271 |
} |
} |
272 |
|
|
|
/* 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 */ |
|
273 |
static void |
static void |
274 |
sec_write_number(NUMBER * num, uint8 *buffer, int len) |
reverse(uint8 *p, int len) |
275 |
{ |
{ |
|
INT *data = num->n_part; |
|
276 |
int i, j; |
int i, j; |
277 |
|
uint8 temp; |
278 |
|
|
279 |
for (i = 0, j = 0; j < len; i++, j += 2) |
for (i = 0, j = len-1; i < j; i++, j--) |
280 |
{ |
{ |
281 |
buffer[j] = data[i] & 0xff; |
temp = p[i]; |
282 |
buffer[j + 1] = data[i] >> 8; |
p[i] = p[j]; |
283 |
|
p[j] = temp; |
284 |
} |
} |
285 |
} |
} |
286 |
|
|
289 |
sec_rsa_encrypt(uint8 *out, uint8 *in, int len, |
sec_rsa_encrypt(uint8 *out, uint8 *in, int len, |
290 |
uint8 *modulus, uint8 *exponent) |
uint8 *modulus, uint8 *exponent) |
291 |
{ |
{ |
292 |
NUMBER data, key; |
BN_CTX ctx; |
293 |
|
BIGNUM mod, exp, x, y; |
294 |
/* Set modulus for arithmetic */ |
uint8 inr[SEC_MODULUS_SIZE]; |
295 |
sec_read_number(&key, modulus, SEC_MODULUS_SIZE); |
int outlen; |
296 |
m_init(&key, NULL); |
|
297 |
|
reverse(modulus, SEC_MODULUS_SIZE); |
298 |
/* Exponentiate */ |
reverse(exponent, SEC_EXPONENT_SIZE); |
299 |
sec_read_number(&data, in, len); |
memcpy(inr, in, len); |
300 |
sec_read_number(&key, exponent, SEC_EXPONENT_SIZE); |
reverse(inr, len); |
301 |
m_exp(&data, &key, &data); |
|
302 |
sec_write_number(&data, out, SEC_MODULUS_SIZE); |
BN_CTX_init(&ctx); |
303 |
|
BN_init(&mod); |
304 |
|
BN_init(&exp); |
305 |
|
BN_init(&x); |
306 |
|
BN_init(&y); |
307 |
|
|
308 |
|
BN_bin2bn(modulus, SEC_MODULUS_SIZE, &mod); |
309 |
|
BN_bin2bn(exponent, SEC_EXPONENT_SIZE, &exp); |
310 |
|
BN_bin2bn(inr, len, &x); |
311 |
|
BN_mod_exp(&y, &x, &exp, &mod, &ctx); |
312 |
|
outlen = BN_bn2bin(&y, out); |
313 |
|
reverse(out, outlen); |
314 |
|
if (outlen < SEC_MODULUS_SIZE) |
315 |
|
memset(out+outlen, 0, SEC_MODULUS_SIZE-outlen); |
316 |
|
|
317 |
|
BN_free(&y); |
318 |
|
BN_clear_free(&x); |
319 |
|
BN_free(&exp); |
320 |
|
BN_free(&mod); |
321 |
|
BN_CTX_free(&ctx); |
322 |
} |
} |
323 |
|
|
324 |
/* Initialise secure transport packet */ |
/* Initialise secure transport packet */ |
353 |
flags &= ~SEC_ENCRYPT; |
flags &= ~SEC_ENCRYPT; |
354 |
datalen = s->end - s->p - 8; |
datalen = s->end - s->p - 8; |
355 |
|
|
356 |
#if RDP_DEBUG |
#if WITH_DEBUG |
357 |
DEBUG("Sending encrypted packet:\n"); |
DEBUG(("Sending encrypted packet:\n")); |
358 |
hexdump(s->p + 8, datalen); |
hexdump(s->p + 8, datalen); |
359 |
#endif |
#endif |
360 |
|
|
432 |
/* Client encryption settings */ |
/* Client encryption settings */ |
433 |
out_uint16_le(s, SEC_TAG_CLI_CRYPT); |
out_uint16_le(s, SEC_TAG_CLI_CRYPT); |
434 |
out_uint16(s, 8); /* length */ |
out_uint16(s, 8); /* length */ |
435 |
out_uint32_le(s, use_encryption ? 1 : 0); /* encryption enabled */ |
out_uint32_le(s, encryption ? 1 : 0); /* encryption enabled */ |
436 |
s_mark_end(s); |
s_mark_end(s); |
437 |
} |
} |
438 |
|
|
445 |
in_uint32_le(s, magic); |
in_uint32_le(s, magic); |
446 |
if (magic != SEC_RSA_MAGIC) |
if (magic != SEC_RSA_MAGIC) |
447 |
{ |
{ |
448 |
ERROR("RSA magic 0x%x\n", magic); |
error("RSA magic 0x%x\n", magic); |
449 |
return False; |
return False; |
450 |
} |
} |
451 |
|
|
452 |
in_uint32_le(s, modulus_len); |
in_uint32_le(s, modulus_len); |
453 |
if (modulus_len != SEC_MODULUS_SIZE + SEC_PADDING_SIZE) |
if (modulus_len != SEC_MODULUS_SIZE + SEC_PADDING_SIZE) |
454 |
{ |
{ |
455 |
ERROR("modulus len 0x%x\n", modulus_len); |
error("modulus len 0x%x\n", modulus_len); |
456 |
return False; |
return False; |
457 |
} |
} |
458 |
|
|
480 |
|
|
481 |
if (random_len != SEC_RANDOM_SIZE) |
if (random_len != SEC_RANDOM_SIZE) |
482 |
{ |
{ |
483 |
ERROR("random len %d\n", random_len); |
error("random len %d\n", random_len); |
484 |
return False; |
return False; |
485 |
} |
} |
486 |
|
|
515 |
break; |
break; |
516 |
|
|
517 |
default: |
default: |
518 |
NOTIMP("crypt tag 0x%x\n", tag); |
unimpl("crypt tag 0x%x\n", tag); |
519 |
} |
} |
520 |
|
|
521 |
s->p = next_tag; |
s->p = next_tag; |
573 |
break; |
break; |
574 |
|
|
575 |
default: |
default: |
576 |
NOTIMP("response tag 0x%x\n", tag); |
unimpl("response tag 0x%x\n", tag); |
577 |
} |
} |
578 |
|
|
579 |
s->p = next_tag; |
s->p = next_tag; |
589 |
|
|
590 |
while ((s = mcs_recv()) != NULL) |
while ((s = mcs_recv()) != NULL) |
591 |
{ |
{ |
592 |
if (use_encryption || !licence_issued) |
if (encryption || !licence_issued) |
593 |
{ |
{ |
594 |
in_uint32_le(s, sec_flags); |
in_uint32_le(s, sec_flags); |
595 |
|
|
627 |
return False; |
return False; |
628 |
|
|
629 |
sec_process_mcs_data(&mcs_data); |
sec_process_mcs_data(&mcs_data); |
630 |
if (use_encryption) |
if (encryption) |
631 |
sec_establish_key(); |
sec_establish_key(); |
632 |
return True; |
return True; |
633 |
} |
} |