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; |
static RSA *server_public_key; |
49 |
|
static uint32 server_public_key_len; |
50 |
|
|
51 |
static uint8 sec_sign_key[16]; |
static uint8 sec_sign_key[16]; |
52 |
static uint8 sec_decrypt_key[16]; |
static uint8 sec_decrypt_key[16]; |
53 |
static uint8 sec_encrypt_key[16]; |
static uint8 sec_encrypt_key[16]; |
54 |
static uint8 sec_decrypt_update_key[16]; |
static uint8 sec_decrypt_update_key[16]; |
55 |
static uint8 sec_encrypt_update_key[16]; |
static uint8 sec_encrypt_update_key[16]; |
56 |
static uint8 sec_crypted_random[SEC_MODULUS_SIZE]; |
static uint8 sec_crypted_random[SEC_MAX_MODULUS_SIZE]; |
57 |
|
|
58 |
uint16 g_server_rdp_version = 0; |
uint16 g_server_rdp_version = 0; |
59 |
|
|
298 |
|
|
299 |
/* Perform an RSA public key encryption operation */ |
/* Perform an RSA public key encryption operation */ |
300 |
static void |
static void |
301 |
sec_rsa_encrypt(uint8 * out, uint8 * in, int len, uint8 * modulus, uint8 * exponent) |
sec_rsa_encrypt(uint8 * out, uint8 * in, int len, uint32 modulus_size, uint8 * modulus, uint8 * exponent) |
302 |
{ |
{ |
303 |
BN_CTX *ctx; |
BN_CTX *ctx; |
304 |
BIGNUM mod, exp, x, y; |
BIGNUM mod, exp, x, y; |
305 |
uint8 inr[SEC_MODULUS_SIZE]; |
uint8 inr[SEC_MAX_MODULUS_SIZE]; |
306 |
int outlen; |
int outlen; |
307 |
|
|
308 |
reverse(modulus, SEC_MODULUS_SIZE); |
reverse(modulus, modulus_size); |
309 |
reverse(exponent, SEC_EXPONENT_SIZE); |
reverse(exponent, SEC_EXPONENT_SIZE); |
310 |
memcpy(inr, in, len); |
memcpy(inr, in, len); |
311 |
reverse(inr, len); |
reverse(inr, len); |
316 |
BN_init(&x); |
BN_init(&x); |
317 |
BN_init(&y); |
BN_init(&y); |
318 |
|
|
319 |
BN_bin2bn(modulus, SEC_MODULUS_SIZE, &mod); |
BN_bin2bn(modulus, modulus_size, &mod); |
320 |
BN_bin2bn(exponent, SEC_EXPONENT_SIZE, &exp); |
BN_bin2bn(exponent, SEC_EXPONENT_SIZE, &exp); |
321 |
BN_bin2bn(inr, len, &x); |
BN_bin2bn(inr, len, &x); |
322 |
BN_mod_exp(&y, &x, &exp, &mod, ctx); |
BN_mod_exp(&y, &x, &exp, &mod, ctx); |
323 |
outlen = BN_bn2bin(&y, out); |
outlen = BN_bn2bin(&y, out); |
324 |
reverse(out, outlen); |
reverse(out, outlen); |
325 |
if (outlen < SEC_MODULUS_SIZE) |
if (outlen < modulus_size) |
326 |
memset(out + outlen, 0, SEC_MODULUS_SIZE - outlen); |
memset(out + outlen, 0, modulus_size - outlen); |
327 |
|
|
328 |
BN_free(&y); |
BN_free(&y); |
329 |
BN_clear_free(&x); |
BN_clear_free(&x); |
389 |
static void |
static void |
390 |
sec_establish_key(void) |
sec_establish_key(void) |
391 |
{ |
{ |
392 |
uint32 length = SEC_MODULUS_SIZE + SEC_PADDING_SIZE; |
uint32 length = server_public_key_len + SEC_PADDING_SIZE; |
393 |
uint32 flags = SEC_CLIENT_RANDOM; |
uint32 flags = SEC_CLIENT_RANDOM; |
394 |
STREAM s; |
STREAM s; |
395 |
|
|
396 |
s = sec_init(flags, 76); |
s = sec_init(flags, length+4); |
397 |
|
|
398 |
out_uint32_le(s, length); |
out_uint32_le(s, length); |
399 |
out_uint8p(s, sec_crypted_random, SEC_MODULUS_SIZE); |
out_uint8p(s, sec_crypted_random, server_public_key_len); |
400 |
out_uint8s(s, SEC_PADDING_SIZE); |
out_uint8s(s, SEC_PADDING_SIZE); |
401 |
|
|
402 |
s_mark_end(s); |
s_mark_end(s); |
508 |
} |
} |
509 |
|
|
510 |
in_uint32_le(s, modulus_len); |
in_uint32_le(s, modulus_len); |
511 |
if (modulus_len != SEC_MODULUS_SIZE + SEC_PADDING_SIZE) |
modulus_len -= SEC_PADDING_SIZE; |
512 |
|
if ((modulus_len < 64) || (modulus_len > SEC_MAX_MODULUS_SIZE)) |
513 |
{ |
{ |
514 |
error("modulus len 0x%x\n", modulus_len); |
error("Bad server public key size (%u bits)\n", modulus_len*8); |
515 |
return False; |
return False; |
516 |
} |
} |
517 |
|
|
518 |
in_uint8s(s, 8); /* modulus_bits, unknown */ |
in_uint8s(s, 8); /* modulus_bits, unknown */ |
519 |
in_uint8p(s, *exponent, SEC_EXPONENT_SIZE); |
in_uint8p(s, *exponent, SEC_EXPONENT_SIZE); |
520 |
in_uint8p(s, *modulus, SEC_MODULUS_SIZE); |
in_uint8p(s, *modulus, modulus_len); |
521 |
in_uint8s(s, SEC_PADDING_SIZE); |
in_uint8s(s, SEC_PADDING_SIZE); |
522 |
|
server_public_key_len = modulus_len; |
523 |
|
|
524 |
return s_check(s); |
return s_check(s); |
525 |
} |
} |
547 |
} |
} |
548 |
|
|
549 |
server_public_key = RSAPublicKey_dup((RSA *) epk->pkey.ptr); |
server_public_key = RSAPublicKey_dup((RSA *) epk->pkey.ptr); |
|
|
|
550 |
EVP_PKEY_free(epk); |
EVP_PKEY_free(epk); |
551 |
|
|
552 |
|
server_public_key_len = RSA_size(server_public_key); |
553 |
|
if ((server_public_key_len < 64) || (server_public_key_len > SEC_MAX_MODULUS_SIZE)) |
554 |
|
{ |
555 |
|
error("Bad server public key size (%u bits)\n", server_public_key_len*8); |
556 |
|
return False; |
557 |
|
} |
558 |
|
|
559 |
return True; |
return True; |
560 |
} |
} |
561 |
|
|
728 |
uint8 *server_random, *modulus, *exponent; |
uint8 *server_random, *modulus, *exponent; |
729 |
uint8 client_random[SEC_RANDOM_SIZE]; |
uint8 client_random[SEC_RANDOM_SIZE]; |
730 |
uint32 rc4_key_size; |
uint32 rc4_key_size; |
|
uint8 inr[SEC_MODULUS_SIZE]; |
|
731 |
|
|
732 |
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)) |
733 |
{ |
{ |
736 |
} |
} |
737 |
|
|
738 |
DEBUG(("Generating client random\n")); |
DEBUG(("Generating client random\n")); |
|
/* Generate a client random, and hence determine encryption keys */ |
|
|
/* 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! |
|
|
*/ |
|
|
|
|
739 |
generate_random(client_random); |
generate_random(client_random); |
740 |
|
|
741 |
if (NULL != server_public_key) |
if (NULL != server_public_key) |
742 |
{ /* Which means we should use |
{ /* Which means we should use |
743 |
RDP5-style encryption */ |
RDP5-style encryption */ |
744 |
|
uint8 inr[SEC_MAX_MODULUS_SIZE]; |
745 |
|
uint32 padding_len = server_public_key_len - SEC_RANDOM_SIZE; |
746 |
|
|
747 |
memcpy(inr + SEC_RANDOM_SIZE, client_random, SEC_RANDOM_SIZE); |
/* This is what the MS client do: */ |
748 |
reverse(inr + SEC_RANDOM_SIZE, SEC_RANDOM_SIZE); |
memset(inr, 0, padding_len); |
749 |
|
/* *ARIGL!* Plaintext attack, anyone? |
750 |
|
I tried doing: |
751 |
|
generate_random(inr); |
752 |
|
..but that generates connection errors now and then (yes, |
753 |
|
"now and then". Something like 0 to 3 attempts needed before a |
754 |
|
successful connection. Nice. Not! |
755 |
|
*/ |
756 |
|
memcpy(inr + padding_len, client_random, SEC_RANDOM_SIZE); |
757 |
|
reverse(inr + padding_len, SEC_RANDOM_SIZE); |
758 |
|
|
759 |
RSA_public_encrypt(SEC_MODULUS_SIZE, |
RSA_public_encrypt(server_public_key_len, |
760 |
inr, sec_crypted_random, server_public_key, RSA_NO_PADDING); |
inr, sec_crypted_random, server_public_key, RSA_NO_PADDING); |
761 |
|
|
762 |
reverse(sec_crypted_random, SEC_MODULUS_SIZE); |
reverse(sec_crypted_random, server_public_key_len); |
763 |
|
|
764 |
RSA_free(server_public_key); |
RSA_free(server_public_key); |
765 |
server_public_key = NULL; |
server_public_key = NULL; |
767 |
else |
else |
768 |
{ /* RDP4-style encryption */ |
{ /* RDP4-style encryption */ |
769 |
sec_rsa_encrypt(sec_crypted_random, |
sec_rsa_encrypt(sec_crypted_random, |
770 |
client_random, SEC_RANDOM_SIZE, modulus, exponent); |
client_random, SEC_RANDOM_SIZE, server_public_key_len, modulus, exponent); |
771 |
} |
} |
772 |
sec_generate_keys(client_random, server_random, rc4_key_size); |
sec_generate_keys(client_random, server_random, rc4_key_size); |
773 |
} |
} |