1 |
/* |
/* -*- c-basic-offset: 8 -*- |
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-2002 |
Copyright (C) Matthew Chapman 1999-2002 |
25 |
#include <openssl/md5.h> |
#include <openssl/md5.h> |
26 |
#include <openssl/sha.h> |
#include <openssl/sha.h> |
27 |
#include <openssl/bn.h> |
#include <openssl/bn.h> |
28 |
|
#include <openssl/x509v3.h> |
29 |
#else |
#else |
30 |
#include "crypto/rc4.h" |
#include "crypto/rc4.h" |
31 |
#include "crypto/md5.h" |
#include "crypto/md5.h" |
39 |
extern int keylayout; |
extern int keylayout; |
40 |
extern BOOL encryption; |
extern BOOL encryption; |
41 |
extern BOOL licence_issued; |
extern BOOL licence_issued; |
42 |
|
extern BOOL use_rdp5; |
43 |
extern int server_bpp; |
extern int server_bpp; |
44 |
|
|
45 |
static int rc4_key_len; |
static int rc4_key_len; |
46 |
static RC4_KEY rc4_decrypt_key; |
static RC4_KEY rc4_decrypt_key; |
47 |
static RC4_KEY rc4_encrypt_key; |
static RC4_KEY rc4_encrypt_key; |
48 |
|
static RSA *server_public_key; |
49 |
|
|
50 |
static uint8 sec_sign_key[16]; |
static uint8 sec_sign_key[16]; |
51 |
static uint8 sec_decrypt_key[16]; |
static uint8 sec_decrypt_key[16]; |
54 |
static uint8 sec_encrypt_update_key[16]; |
static uint8 sec_encrypt_update_key[16]; |
55 |
static uint8 sec_crypted_random[SEC_MODULUS_SIZE]; |
static uint8 sec_crypted_random[SEC_MODULUS_SIZE]; |
56 |
|
|
57 |
|
uint16 server_rdp_version = 0; |
58 |
|
|
59 |
/* |
/* |
60 |
* General purpose 48-byte transformation, using two 32-byte salts (generally, |
* General purpose 48-byte transformation, using two 32-byte salts (generally, |
61 |
* a client and server salt) and a global salt value used for padding. |
* a client and server salt) and a global salt value used for padding. |
146 |
} |
} |
147 |
else |
else |
148 |
{ |
{ |
149 |
DEBUG(("128-bit encryption enabled\n")); |
DEBUG(("rc_4_key_size == %d, 128-bit encryption enabled\n", rc4_key_size)); |
150 |
rc4_key_len = 16; |
rc4_key_len = 16; |
151 |
} |
} |
152 |
|
|
257 |
} |
} |
258 |
|
|
259 |
/* Decrypt data using RC4 */ |
/* Decrypt data using RC4 */ |
260 |
static void |
void |
261 |
sec_decrypt(uint8 * data, int length) |
sec_decrypt(uint8 * data, int length) |
262 |
{ |
{ |
263 |
static int use_count; |
static int use_count; |
390 |
sec_out_mcs_data(STREAM s) |
sec_out_mcs_data(STREAM s) |
391 |
{ |
{ |
392 |
int hostlen = 2 * strlen(hostname); |
int hostlen = 2 * strlen(hostname); |
393 |
|
int length = 158 + 76 + 12 + 4 + 20; |
394 |
|
|
395 |
if (hostlen > 30) |
if (hostlen > 30) |
396 |
hostlen = 30; |
hostlen = 30; |
400 |
out_uint8(s, 0x7c); |
out_uint8(s, 0x7c); |
401 |
out_uint16_be(s, 1); |
out_uint16_be(s, 1); |
402 |
|
|
403 |
out_uint16_be(s, (158 | 0x8000)); /* remaining length */ |
out_uint16_be(s, (length | 0x8000)); /* remaining length */ |
404 |
|
|
405 |
out_uint16_be(s, 8); /* length? */ |
out_uint16_be(s, 8); /* length? */ |
406 |
out_uint16_be(s, 16); |
out_uint16_be(s, 16); |
409 |
out_uint8(s, 0); |
out_uint8(s, 0); |
410 |
|
|
411 |
out_uint32_le(s, 0x61637544); /* "Duca" ?! */ |
out_uint32_le(s, 0x61637544); /* "Duca" ?! */ |
412 |
out_uint16_be(s, (144 | 0x8000)); /* remaining length */ |
out_uint16_be(s, ((length - 14) | 0x8000)); /* remaining length */ |
413 |
|
|
414 |
/* Client information */ |
/* Client information */ |
415 |
out_uint16_le(s, SEC_TAG_CLI_INFO); |
out_uint16_le(s, SEC_TAG_CLI_INFO); |
416 |
out_uint16_le(s, 136); /* length */ |
out_uint16_le(s, 212); /* length */ |
417 |
out_uint16_le(s, 1); |
out_uint16_le(s, use_rdp5 ? 4 : 1); /* RDP version. 1 == RDP4, 4 == RDP5. */ |
418 |
out_uint16_le(s, 8); |
out_uint16_le(s, 8); |
419 |
out_uint16_le(s, width); |
out_uint16_le(s, width); |
420 |
out_uint16_le(s, height); |
out_uint16_le(s, height); |
421 |
out_uint16_le(s, 0xca01); |
out_uint16_le(s, 0xca01); |
422 |
out_uint16_le(s, 0xaa03); |
out_uint16_le(s, 0xaa03); |
423 |
out_uint32_le(s, keylayout); |
out_uint32_le(s, keylayout); |
424 |
out_uint32_le(s, 419); /* client build? we are 419 compatible :-) */ |
out_uint32_le(s, 2600); /* Client build. We are now 2600 compatible :-) */ |
425 |
|
|
426 |
/* Unicode name of client, padded to 32 bytes */ |
/* Unicode name of client, padded to 32 bytes */ |
427 |
rdp_out_unistr(s, hostname, hostlen); |
rdp_out_unistr(s, hostname, hostlen); |
447 |
out_uint16_le(s, 0xca04); |
out_uint16_le(s, 0xca04); |
448 |
break; |
break; |
449 |
} |
} |
450 |
out_uint16(s, 0); |
out_uint16(s, 1); |
451 |
|
|
452 |
|
out_uint32(s, 0); |
453 |
|
out_uint32_le(s, 0x070008); |
454 |
|
out_uint32_le(s, 1); |
455 |
|
out_uint8s(s, 64); /* End of client info */ |
456 |
|
|
457 |
|
out_uint16_le(s, SEC_TAG_CLI_4); |
458 |
|
out_uint16_le(s, 12); |
459 |
|
out_uint32_le(s, 9); |
460 |
|
out_uint32_le(s, 0); |
461 |
|
|
462 |
/* Client encryption settings */ |
/* Client encryption settings */ |
463 |
out_uint16_le(s, SEC_TAG_CLI_CRYPT); |
out_uint16_le(s, SEC_TAG_CLI_CRYPT); |
464 |
out_uint16_le(s, 8); /* length */ |
out_uint16_le(s, 12); /* length */ |
465 |
out_uint32_le(s, encryption ? 0x3 : 0); /* encryption supported, 128-bit supported */ |
out_uint32_le(s, encryption ? 0x3 : 0); /* encryption supported, 128-bit supported */ |
466 |
|
out_uint32_le(s, 0); /* Unknown */ |
467 |
|
|
468 |
|
out_uint16_le(s, SEC_TAG_CLI_CHANNELS); |
469 |
|
out_uint16_le(s, 20); /* length */ |
470 |
|
out_uint32_le(s, 1); /* number of virtual channels */ |
471 |
|
out_uint8p(s, "cliprdr", 8); /* name padded to 8(?) */ |
472 |
|
out_uint16(s, 0); |
473 |
|
out_uint16_le(s, 0xc0a0); /* Flags. Rumours tell this is documented in MSDN. */ |
474 |
|
|
475 |
s_mark_end(s); |
s_mark_end(s); |
476 |
} |
} |
477 |
|
|
503 |
return s_check(s); |
return s_check(s); |
504 |
} |
} |
505 |
|
|
506 |
|
static BOOL |
507 |
|
sec_parse_x509_key(X509 * cert) |
508 |
|
{ |
509 |
|
EVP_PKEY *epk = NULL; |
510 |
|
/* By some reason, Microsoft sets the OID of the Public RSA key to |
511 |
|
the oid for "MD5 with RSA Encryption" instead of "RSA Encryption" |
512 |
|
|
513 |
|
Kudos to Richard Levitte for the following (. intiutive .) |
514 |
|
lines of code that resets the OID and let's us extract the key. */ |
515 |
|
if (OBJ_obj2nid(cert->cert_info->key->algor->algorithm) == NID_md5WithRSAEncryption) |
516 |
|
{ |
517 |
|
DEBUG_RDP5(("Re-setting algorithm type to RSA in server certificate\n")); |
518 |
|
cert->cert_info->key->algor->algorithm = OBJ_nid2obj(NID_rsaEncryption); |
519 |
|
} |
520 |
|
epk = X509_get_pubkey(cert); |
521 |
|
if (NULL == epk) |
522 |
|
{ |
523 |
|
error("Failed to extract public key from certificate\n"); |
524 |
|
return False; |
525 |
|
} |
526 |
|
|
527 |
|
server_public_key = (RSA *) epk->pkey.ptr; |
528 |
|
|
529 |
|
return True; |
530 |
|
} |
531 |
|
|
532 |
|
|
533 |
/* Parse a crypto information structure */ |
/* Parse a crypto information structure */ |
534 |
static BOOL |
static BOOL |
535 |
sec_parse_crypt_info(STREAM s, uint32 * rc4_key_size, |
sec_parse_crypt_info(STREAM s, uint32 * rc4_key_size, |
536 |
uint8 ** server_random, uint8 ** modulus, uint8 ** exponent) |
uint8 ** server_random, uint8 ** modulus, uint8 ** exponent) |
537 |
{ |
{ |
538 |
uint32 crypt_level, random_len, rsa_info_len; |
uint32 crypt_level, random_len, rsa_info_len; |
539 |
|
uint32 cacert_len, cert_len; |
540 |
|
X509 *cacert, *server_cert; |
541 |
uint16 tag, length; |
uint16 tag, length; |
542 |
uint8 *next_tag, *end; |
uint8 *next_tag, *end; |
543 |
|
|
544 |
in_uint32_le(s, *rc4_key_size); /* 1 = 40-bit, 2 = 128-bit */ |
in_uint32_le(s, *rc4_key_size); /* 1 = 40-bit, 2 = 128-bit */ |
545 |
in_uint32_le(s, crypt_level); /* 1 = low, 2 = medium, 3 = high */ |
in_uint32_le(s, crypt_level); /* 1 = low, 2 = medium, 3 = high */ |
546 |
if (crypt_level == 0) /* no encryptation */ |
if (crypt_level == 0) /* no encryption */ |
547 |
return False; |
return False; |
548 |
in_uint32_le(s, random_len); |
in_uint32_le(s, random_len); |
549 |
in_uint32_le(s, rsa_info_len); |
in_uint32_le(s, rsa_info_len); |
550 |
|
|
551 |
if (random_len != SEC_RANDOM_SIZE) |
if (random_len != SEC_RANDOM_SIZE) |
552 |
{ |
{ |
553 |
error("random len %d\n", random_len); |
error("random len %d, expected %d\n", random_len, SEC_RANDOM_SIZE); |
554 |
return False; |
return False; |
555 |
} |
} |
556 |
|
|
561 |
if (end > s->end) |
if (end > s->end) |
562 |
return False; |
return False; |
563 |
|
|
564 |
in_uint8s(s, 12); /* unknown */ |
if (!use_rdp5 || 1 == server_rdp_version) |
565 |
|
{ |
566 |
|
DEBUG_RDP5(("We're going for the RDP4-style encryption\n")); |
567 |
|
in_uint8s(s, 12); /* unknown */ |
568 |
|
|
569 |
|
while (s->p < end) |
570 |
|
{ |
571 |
|
in_uint16_le(s, tag); |
572 |
|
in_uint16_le(s, length); |
573 |
|
|
574 |
|
next_tag = s->p + length; |
575 |
|
|
576 |
|
switch (tag) |
577 |
|
{ |
578 |
|
case SEC_TAG_PUBKEY: |
579 |
|
if (!sec_parse_public_key(s, modulus, exponent)) |
580 |
|
return False; |
581 |
|
DEBUG_RDP5(("Got Public key, RDP4-style\n")); |
582 |
|
|
583 |
|
break; |
584 |
|
|
585 |
|
case SEC_TAG_KEYSIG: |
586 |
|
/* Is this a Microsoft key that we just got? */ |
587 |
|
/* Care factor: zero! */ |
588 |
|
/* Actually, it would probably be a good idea to check if the public key is signed with this key, and then store this |
589 |
|
key as a known key of the hostname. This would prevent some MITM-attacks. */ |
590 |
|
break; |
591 |
|
|
592 |
|
default: |
593 |
|
unimpl("crypt tag 0x%x\n", tag); |
594 |
|
} |
595 |
|
|
596 |
while (s->p < end) |
s->p = next_tag; |
597 |
|
} |
598 |
|
} |
599 |
|
else if (4 == server_rdp_version) |
600 |
{ |
{ |
601 |
in_uint16_le(s, tag); |
DEBUG_RDP5(("We're going for the RDP5-style encryption\n")); |
602 |
in_uint16_le(s, length); |
in_uint8s(s, 8); /* Unknown */ |
603 |
|
|
604 |
next_tag = s->p + length; |
/* Do da funky X.509 stuffy |
605 |
|
|
606 |
switch (tag) |
"How did I find out about this? I looked up and saw a |
607 |
|
bright light and when I came to I had a scar on my forehead |
608 |
|
and knew about X.500" |
609 |
|
- Peter Gutman in a early version of |
610 |
|
http://www.cs.auckland.ac.nz/~pgut001/pubs/x509guide.txt |
611 |
|
*/ |
612 |
|
|
613 |
|
in_uint32_le(s, cacert_len); |
614 |
|
cacert = d2i_X509(NULL, &(s->p), cacert_len); |
615 |
|
/* Note: We don't need to move s->p here - d2i_X509 is |
616 |
|
"kind" enough to do it for us */ |
617 |
|
if (NULL == cacert) |
618 |
|
{ |
619 |
|
error("Couldn't load CA Certificate from server\n"); |
620 |
|
return False; |
621 |
|
} |
622 |
|
|
623 |
|
/* Currently, we don't use the CA Certificate. |
624 |
|
FIXME: |
625 |
|
*) Verify the server certificate (server_cert) with the |
626 |
|
CA certificate. |
627 |
|
*) Store the CA Certificate with the hostname of the |
628 |
|
server we are connecting to as key, and compare it |
629 |
|
when we connect the next time, in order to prevent |
630 |
|
MITM-attacks. |
631 |
|
*/ |
632 |
|
|
633 |
|
in_uint32_le(s, cert_len); |
634 |
|
server_cert = d2i_X509(NULL, &(s->p), cert_len); |
635 |
|
if (NULL == server_cert) |
636 |
{ |
{ |
637 |
case SEC_TAG_PUBKEY: |
error("Couldn't load Certificate from server\n"); |
638 |
if (!sec_parse_public_key(s, modulus, exponent)) |
return False; |
639 |
return False; |
} |
640 |
|
|
641 |
break; |
in_uint8s(s, 16); /* Padding */ |
642 |
|
|
643 |
case SEC_TAG_KEYSIG: |
/* Note: Verifying the server certificate must be done here, |
644 |
/* Is this a Microsoft key that we just got? */ |
before sec_parse_public_key since we'll have to apply |
645 |
/* Care factor: zero! */ |
serious violence to the key after this */ |
|
break; |
|
646 |
|
|
647 |
default: |
if (!sec_parse_x509_key(server_cert)) |
648 |
unimpl("crypt tag 0x%x\n", tag); |
{ |
649 |
|
DEBUG_RDP5(("Didn't parse X509 correctly\n")); |
650 |
|
return False; |
651 |
} |
} |
652 |
|
return True; /* There's some garbage here we don't care about */ |
653 |
s->p = next_tag; |
} |
654 |
|
else |
655 |
|
{ |
656 |
|
error("Unknown Server RDP version %d", server_rdp_version); |
657 |
|
return False; |
658 |
} |
} |
|
|
|
659 |
return s_check_end(s); |
return s_check_end(s); |
660 |
} |
} |
661 |
|
|
666 |
uint8 *server_random, *modulus, *exponent; |
uint8 *server_random, *modulus, *exponent; |
667 |
uint8 client_random[SEC_RANDOM_SIZE]; |
uint8 client_random[SEC_RANDOM_SIZE]; |
668 |
uint32 rc4_key_size; |
uint32 rc4_key_size; |
669 |
|
uint8 inr[SEC_MODULUS_SIZE]; |
670 |
|
|
671 |
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)) |
672 |
|
{ |
673 |
|
DEBUG(("Failed to parse crypt info\n")); |
674 |
return; |
return; |
675 |
|
} |
676 |
|
|
677 |
|
DEBUG(("Generating client random\n")); |
678 |
/* Generate a client random, and hence determine encryption keys */ |
/* Generate a client random, and hence determine encryption keys */ |
679 |
|
generate_random(inr); |
680 |
|
// This is what the MS client do: |
681 |
|
// memset(inr, 0, SEC_RANDOM_SIZE); |
682 |
|
// *ARIGL!* |
683 |
generate_random(client_random); |
generate_random(client_random); |
684 |
sec_rsa_encrypt(sec_crypted_random, client_random, SEC_RANDOM_SIZE, modulus, exponent); |
if (NULL != server_public_key) |
685 |
|
{ /* Which means we should use |
686 |
|
RDP5-style encryption */ |
687 |
|
|
688 |
|
memcpy(inr + SEC_RANDOM_SIZE, client_random, SEC_RANDOM_SIZE); |
689 |
|
reverse(inr + SEC_RANDOM_SIZE, SEC_RANDOM_SIZE); |
690 |
|
|
691 |
|
RSA_public_encrypt(SEC_MODULUS_SIZE, |
692 |
|
inr, sec_crypted_random, server_public_key, RSA_NO_PADDING); |
693 |
|
|
694 |
|
reverse(sec_crypted_random, SEC_MODULUS_SIZE); |
695 |
|
|
696 |
|
} |
697 |
|
else |
698 |
|
{ /* RDP4-style encryption */ |
699 |
|
sec_rsa_encrypt(sec_crypted_random, |
700 |
|
client_random, SEC_RANDOM_SIZE, modulus, exponent); |
701 |
|
} |
702 |
sec_generate_keys(client_random, server_random, rc4_key_size); |
sec_generate_keys(client_random, server_random, rc4_key_size); |
703 |
} |
} |
704 |
|
|
705 |
/* Process connect response data blob */ |
|
706 |
|
/* Process SRV_INFO, find RDP version supported by server */ |
707 |
static void |
static void |
708 |
|
sec_process_srv_info(STREAM s) |
709 |
|
{ |
710 |
|
in_uint16_le(s, server_rdp_version); |
711 |
|
DEBUG_RDP5(("Server RDP version is %d\n", server_rdp_version)); |
712 |
|
} |
713 |
|
|
714 |
|
|
715 |
|
/* Process connect response data blob */ |
716 |
|
void |
717 |
sec_process_mcs_data(STREAM s) |
sec_process_mcs_data(STREAM s) |
718 |
{ |
{ |
719 |
uint16 tag, length; |
uint16 tag, length; |
720 |
uint8 *next_tag; |
uint8 *next_tag; |
721 |
uint8 len; |
uint8 len; |
722 |
|
|
723 |
in_uint8s(s, 21); /* header */ |
in_uint8s(s, 21); /* header (T.124 stuff, probably) */ |
724 |
in_uint8(s, len); |
in_uint8(s, len); |
725 |
if (len & 0x80) |
if (len & 0x80) |
726 |
in_uint8(s, len); |
in_uint8(s, len); |
738 |
switch (tag) |
switch (tag) |
739 |
{ |
{ |
740 |
case SEC_TAG_SRV_INFO: |
case SEC_TAG_SRV_INFO: |
741 |
|
sec_process_srv_info(s); |
742 |
|
break; |
743 |
|
|
744 |
case SEC_TAG_SRV_3: |
case SEC_TAG_SRV_3: |
745 |
break; |
break; |
746 |
|
|
761 |
sec_recv(void) |
sec_recv(void) |
762 |
{ |
{ |
763 |
uint32 sec_flags; |
uint32 sec_flags; |
764 |
|
uint16 channel; |
765 |
STREAM s; |
STREAM s; |
766 |
|
|
767 |
while ((s = mcs_recv()) != NULL) |
while ((s = mcs_recv(&channel)) != NULL) |
768 |
{ |
{ |
769 |
if (encryption || !licence_issued) |
if (encryption || !licence_issued) |
770 |
{ |
{ |
783 |
} |
} |
784 |
} |
} |
785 |
|
|
786 |
return s; |
if (MCS_GLOBAL_CHANNEL == channel) |
787 |
|
{ |
788 |
|
return s; |
789 |
|
} |
790 |
|
else |
791 |
|
rdp5_process_channel(s, channel); |
792 |
|
|
793 |
} |
} |
794 |
|
|
795 |
return NULL; |
return NULL; |
797 |
|
|
798 |
/* Establish a secure connection */ |
/* Establish a secure connection */ |
799 |
BOOL |
BOOL |
800 |
sec_connect(char *server) |
sec_connect(char *server, char *username) |
801 |
{ |
{ |
802 |
struct stream mcs_data; |
struct stream mcs_data; |
803 |
|
|
806 |
mcs_data.p = mcs_data.data = xmalloc(mcs_data.size); |
mcs_data.p = mcs_data.data = xmalloc(mcs_data.size); |
807 |
sec_out_mcs_data(&mcs_data); |
sec_out_mcs_data(&mcs_data); |
808 |
|
|
809 |
if (!mcs_connect(server, &mcs_data)) |
if (!mcs_connect(server, &mcs_data, username)) |
810 |
return False; |
return False; |
811 |
|
|
812 |
sec_process_mcs_data(&mcs_data); |
// sec_process_mcs_data(&mcs_data); |
813 |
if (encryption) |
if (encryption) |
814 |
sec_establish_key(); |
sec_establish_key(); |
815 |
xfree(mcs_data.data); |
xfree(mcs_data.data); |