1 /* -*- c-basic-offset: 8 -*-
2 rdesktop: A Remote Desktop Protocol client.
3 Protocol services - RDP encryption and licensing
4 Copyright (C) Matthew Chapman 1999-2005
6 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
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 ssl_sha1_info_create(void);
26 ssl_sha1_info_delete(void * sha1_info
);
28 ssl_sha1_clear(void * sha1_info
);
30 ssl_sha1_transform(void * sha1_info
, char * data
, int len
);
32 ssl_sha1_complete(void * sha1_info
, char * data
);
34 ssl_md5_info_create(void);
36 ssl_md5_info_delete(void * md5_info
);
38 ssl_md5_info_create(void);
40 ssl_md5_info_delete(void * md5_info
);
42 ssl_md5_clear(void * md5_info
);
44 ssl_md5_transform(void * md5_info
, char * data
, int len
);
46 ssl_md5_complete(void * md5_info
, char * data
);
48 ssl_rc4_info_create(void);
50 ssl_rc4_info_delete(void * rc4_info
);
52 ssl_rc4_set_key(void * rc4_info
, char * key
, int len
);
54 ssl_rc4_crypt(void * rc4_info
, char * in_data
, char * out_data
, int len
);
56 ssl_mod_exp(char* out
, int out_len
, char* in
, int in_len
,
57 char* mod
, int mod_len
, char* exp
, int exp_len
);
59 extern char g_hostname
[];
62 extern unsigned int g_keylayout
;
63 extern int g_keyboard_type
;
64 extern int g_keyboard_subtype
;
65 extern int g_keyboard_functionkeys
;
66 extern BOOL g_encryption
;
67 extern BOOL g_licence_issued
;
68 extern BOOL g_use_rdp5
;
69 extern BOOL g_console_session
;
70 extern int g_server_depth
;
71 extern uint16 mcs_userid
;
72 extern VCHANNEL g_channels
[];
73 extern unsigned int g_num_channels
;
75 static int rc4_key_len
;
76 static void * rc4_decrypt_key
= 0;
77 static void * rc4_encrypt_key
= 0;
78 //static RSA *server_public_key;
79 static void * server_public_key
;
81 static uint8 sec_sign_key
[16];
82 static uint8 sec_decrypt_key
[16];
83 static uint8 sec_encrypt_key
[16];
84 static uint8 sec_decrypt_update_key
[16];
85 static uint8 sec_encrypt_update_key
[16];
86 static uint8 sec_crypted_random
[SEC_MODULUS_SIZE
];
88 uint16 g_server_rdp_version
= 0;
90 /* These values must be available to reset state - Session Directory */
91 static int sec_encrypt_use_count
= 0;
92 static int sec_decrypt_use_count
= 0;
95 * I believe this is based on SSLv3 with the following differences:
96 * MAC algorithm (5.2.3.1) uses only 32-bit length in place of seq_num/type/length fields
97 * MAC algorithm uses SHA1 and MD5 for the two hash functions instead of one or other
98 * key_block algorithm (6.2.2) uses 'X', 'YY', 'ZZZ' instead of 'A', 'BB', 'CCC'
99 * key_block partitioning is different (16 bytes each: MAC secret, decrypt key, encrypt key)
100 * encryption/decryption keys updated every 4096 packets
101 * See http://wp.netscape.com/eng/ssl3/draft302.txt
105 * 48-byte transformation used to generate master secret (6.1) and key material (6.2.2).
106 * Both SHA1 and MD5 algorithms are used.
109 sec_hash_48(uint8
* out
, uint8
* in
, uint8
* salt1
, uint8
* salt2
, uint8 salt
)
117 for (i
= 0; i
< 3; i
++)
119 memset(pad
, salt
+ i
, i
+ 1);
120 sha
= ssl_sha1_info_create();
122 ssl_sha1_transform(sha
, (char *)pad
, i
+ 1);
123 ssl_sha1_transform(sha
, (char *)in
, 48);
124 ssl_sha1_transform(sha
, (char *)salt1
, 32);
125 ssl_sha1_transform(sha
, (char *)salt2
, 32);
126 ssl_sha1_complete(sha
, (char *)shasig
);
127 ssl_sha1_info_delete(sha
);
128 md5
= ssl_md5_info_create();
130 ssl_md5_transform(md5
, (char *)in
, 48);
131 ssl_md5_transform(md5
, (char *)shasig
, 20);
132 ssl_md5_complete(md5
, (char *)out
+ i
* 16);
133 ssl_md5_info_delete(md5
);
138 * 16-byte transformation used to generate export keys (6.2.2).
141 sec_hash_16(uint8
* out
, uint8
* in
, uint8
* salt1
, uint8
* salt2
)
145 md5
= ssl_md5_info_create();
147 ssl_md5_transform(md5
, (char *)in
, 16);
148 ssl_md5_transform(md5
, (char *)salt1
, 32);
149 ssl_md5_transform(md5
, (char *)salt2
, 32);
150 ssl_md5_complete(md5
, (char *)out
);
151 ssl_md5_info_delete(md5
);
154 /* Reduce key entropy from 64 to 40 bits */
156 sec_make_40bit(uint8
* key
)
163 /* Generate encryption keys given client and server randoms */
165 sec_generate_keys(uint8
* client_random
, uint8
* server_random
, int rc4_key_size
)
167 uint8 pre_master_secret
[48];
168 uint8 master_secret
[48];
171 /* Construct pre-master secret */
172 memcpy(pre_master_secret
, client_random
, 24);
173 memcpy(pre_master_secret
+ 24, server_random
, 24);
175 /* Generate master secret and then key material */
176 sec_hash_48(master_secret
, pre_master_secret
, client_random
, server_random
, 'A');
177 sec_hash_48(key_block
, master_secret
, client_random
, server_random
, 'X');
179 /* First 16 bytes of key material is MAC secret */
180 memcpy(sec_sign_key
, key_block
, 16);
182 /* Generate export keys from next two blocks of 16 bytes */
183 sec_hash_16(sec_decrypt_key
, &key_block
[16], client_random
, server_random
);
184 sec_hash_16(sec_encrypt_key
, &key_block
[32], client_random
, server_random
);
186 if (rc4_key_size
== 1)
188 DEBUG(("40-bit encryption enabled\n"));
189 sec_make_40bit(sec_sign_key
);
190 sec_make_40bit(sec_decrypt_key
);
191 sec_make_40bit(sec_encrypt_key
);
196 DEBUG(("rc_4_key_size == %d, 128-bit encryption enabled\n", rc4_key_size
));
200 /* Save initial RC4 keys as update keys */
201 memcpy(sec_decrypt_update_key
, sec_decrypt_key
, 16);
202 memcpy(sec_encrypt_update_key
, sec_encrypt_key
, 16);
204 /* Initialise RC4 state arrays */
206 ssl_rc4_info_delete(rc4_decrypt_key
);
207 rc4_decrypt_key
= ssl_rc4_info_create();
208 ssl_rc4_set_key(rc4_decrypt_key
, (char *)sec_decrypt_key
, rc4_key_len
);
210 ssl_rc4_info_delete(rc4_encrypt_key
);
211 rc4_encrypt_key
= ssl_rc4_info_create();
212 ssl_rc4_set_key(rc4_encrypt_key
, (char *)sec_encrypt_key
, rc4_key_len
);
215 static uint8 pad_54
[40] = {
216 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54,
218 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54,
222 static uint8 pad_92
[48] = {
223 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92,
224 92, 92, 92, 92, 92, 92, 92,
225 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92,
226 92, 92, 92, 92, 92, 92, 92
229 /* Output a uint32 into a buffer (little-endian) */
231 buf_out_uint32(uint8
* buffer
, uint32 value
)
233 buffer
[0] = (value
) & 0xff;
234 buffer
[1] = (value
>> 8) & 0xff;
235 buffer
[2] = (value
>> 16) & 0xff;
236 buffer
[3] = (value
>> 24) & 0xff;
239 /* Generate a MAC hash (5.2.3.1), using a combination of SHA1 and MD5 */
241 sec_sign(uint8
* signature
, int siglen
, uint8
* session_key
, int keylen
, uint8
* data
, int datalen
)
249 buf_out_uint32(lenhdr
, datalen
);
251 sha
= ssl_sha1_info_create();
253 ssl_sha1_transform(sha
, (char *)session_key
, keylen
);
254 ssl_sha1_transform(sha
, (char *)pad_54
, 40);
255 ssl_sha1_transform(sha
, (char *)lenhdr
, 4);
256 ssl_sha1_transform(sha
, (char *)data
, datalen
);
257 ssl_sha1_complete(sha
, (char *)shasig
);
258 ssl_sha1_info_delete(sha
);
260 md5
= ssl_md5_info_create();
262 ssl_md5_transform(md5
, (char *)session_key
, keylen
);
263 ssl_md5_transform(md5
, (char *)pad_92
, 48);
264 ssl_md5_transform(md5
, (char *)shasig
, 20);
265 ssl_md5_complete(md5
, (char *)md5sig
);
266 ssl_md5_info_delete(md5
);
268 memcpy(signature
, md5sig
, siglen
);
271 /* Update an encryption key */
273 sec_update(uint8
* key
, uint8
* update_key
)
280 sha
= ssl_sha1_info_create();
282 ssl_sha1_transform(sha
, (char *)update_key
, rc4_key_len
);
283 ssl_sha1_transform(sha
, (char *)pad_54
, 40);
284 ssl_sha1_transform(sha
, (char *)key
, rc4_key_len
);
285 ssl_sha1_complete(sha
, (char *)shasig
);
286 ssl_sha1_info_delete(sha
);
288 md5
= ssl_md5_info_create();
290 ssl_md5_transform(md5
, (char *)update_key
, rc4_key_len
);
291 ssl_md5_transform(md5
, (char *)pad_92
, 48);
292 ssl_md5_transform(md5
, (char *)shasig
, 20);
293 ssl_md5_complete(md5
, (char *)key
);
294 ssl_md5_info_delete(md5
);
297 update
= ssl_rc4_info_create();
298 ssl_rc4_set_key(update
, (char *)key
, rc4_key_len
);
299 ssl_rc4_crypt(update
, (char *)key
, (char *)key
, rc4_key_len
);
300 ssl_rc4_info_delete(update
);
302 if (rc4_key_len
== 8)
306 /* Encrypt data using RC4 */
308 sec_encrypt(uint8
* data
, int length
)
310 if (sec_encrypt_use_count
== 4096)
312 sec_update(sec_encrypt_key
, sec_encrypt_update_key
);
313 ssl_rc4_set_key(rc4_encrypt_key
, (char *)sec_encrypt_key
, rc4_key_len
);
314 sec_encrypt_use_count
= 0;
316 ssl_rc4_crypt(rc4_encrypt_key
, (char *)data
, (char *)data
, length
);
317 sec_encrypt_use_count
++;
320 /* Decrypt data using RC4 */
322 sec_decrypt(uint8
* data
, int length
)
324 if (sec_decrypt_use_count
== 4096)
326 sec_update(sec_decrypt_key
, sec_decrypt_update_key
);
327 ssl_rc4_set_key(rc4_decrypt_key
, (char *)sec_decrypt_key
, rc4_key_len
);
328 sec_decrypt_use_count
= 0;
330 ssl_rc4_crypt(rc4_decrypt_key
, (char *)data
, (char *)data
, length
);
331 sec_decrypt_use_count
++;
335 reverse(uint8 * p, int len)
340 for (i = 0, j = len - 1; i < j; i++, j--)
348 /* Perform an RSA public key encryption operation */
350 sec_rsa_encrypt(uint8
* out
, uint8
* in
, int len
, uint8
* modulus
, uint8
* exponent
)
352 ssl_mod_exp((char *)out
, 64, (char *)in
, 32, (char *)modulus
, 64, (char *)exponent
, 4);
355 BIGNUM mod, exp, x, y;
356 uint8 inr[SEC_MODULUS_SIZE];
359 reverse(modulus, SEC_MODULUS_SIZE);
360 reverse(exponent, SEC_EXPONENT_SIZE);
361 memcpy(inr, in, len);
370 BN_bin2bn(modulus, SEC_MODULUS_SIZE, &mod);
371 BN_bin2bn(exponent, SEC_EXPONENT_SIZE, &exp);
372 BN_bin2bn(inr, len, &x);
373 BN_mod_exp(&y, &x, &exp, &mod, ctx);
374 outlen = BN_bn2bin(&y, out);
375 reverse(out, outlen);
376 if (outlen < SEC_MODULUS_SIZE)
377 memset(out + outlen, 0, SEC_MODULUS_SIZE - outlen);
386 /* Initialise secure transport packet */
388 sec_init(uint32 flags
, int maxlen
)
393 if (!g_licence_issued
)
394 hdrlen
= (flags
& SEC_ENCRYPT
) ? 12 : 4;
396 hdrlen
= (flags
& SEC_ENCRYPT
) ? 12 : 0;
397 s
= mcs_init(maxlen
+ hdrlen
);
398 s_push_layer(s
, sec_hdr
, hdrlen
);
403 /* Transmit secure transport packet over specified channel */
405 sec_send_to_channel(STREAM s
, uint32 flags
, uint16 channel
)
409 s_pop_layer(s
, sec_hdr
);
410 if (!g_licence_issued
|| (flags
& SEC_ENCRYPT
))
411 out_uint32_le(s
, flags
);
413 if (flags
& SEC_ENCRYPT
)
415 flags
&= ~SEC_ENCRYPT
;
416 datalen
= s
->end
- s
->p
- 8;
419 DEBUG(("Sending encrypted packet:\n"));
420 hexdump(s
->p
+ 8, datalen
);
423 sec_sign(s
->p
, 8, sec_sign_key
, rc4_key_len
, s
->p
+ 8, datalen
);
424 sec_encrypt(s
->p
+ 8, datalen
);
427 mcs_send_to_channel(s
, channel
);
430 /* Transmit secure transport packet */
433 sec_send(STREAM s
, uint32 flags
)
435 sec_send_to_channel(s
, flags
, MCS_GLOBAL_CHANNEL
);
439 /* Transfer the client random to the server */
441 sec_establish_key(void)
443 uint32 length
= SEC_MODULUS_SIZE
+ SEC_PADDING_SIZE
;
444 uint32 flags
= SEC_CLIENT_RANDOM
;
447 s
= sec_init(flags
, 76);
449 out_uint32_le(s
, length
);
450 out_uint8p(s
, sec_crypted_random
, SEC_MODULUS_SIZE
);
451 out_uint8s(s
, SEC_PADDING_SIZE
);
457 /* Output connect initial data blob */
459 sec_out_mcs_data(STREAM s
)
461 int hostlen
= 2 * strlen(g_hostname
);
462 int length
= 158 + 76 + 12 + 4;
465 if (g_num_channels
> 0)
466 length
+= g_num_channels
* 12 + 8;
471 /* Generic Conference Control (T.124) ConferenceCreateRequest */
473 out_uint16_be(s
, 0x14);
477 out_uint16_be(s
, (length
| 0x8000)); /* remaining length */
479 out_uint16_be(s
, 8); /* length? */
480 out_uint16_be(s
, 16);
482 out_uint16_le(s
, 0xc001);
485 out_uint32_le(s
, 0x61637544); /* OEM ID: "Duca", as in Ducati. */
486 out_uint16_be(s
, ((length
- 14) | 0x8000)); /* remaining length */
488 /* Client information */
489 out_uint16_le(s
, SEC_TAG_CLI_INFO
);
490 out_uint16_le(s
, 212); /* length */
491 out_uint16_le(s
, g_use_rdp5
? 4 : 1); /* RDP version. 1 == RDP4, 4 == RDP5. */
493 out_uint16_le(s
, g_width
);
494 out_uint16_le(s
, g_height
);
495 out_uint16_le(s
, 0xca01);
496 out_uint16_le(s
, 0xaa03);
497 out_uint32_le(s
, g_keylayout
);
498 out_uint32_le(s
, 2600); /* Client build. We are now 2600 compatible :-) */
500 /* Unicode name of client, padded to 32 bytes */
501 rdp_out_unistr(s
, g_hostname
, hostlen
);
502 out_uint8s(s
, 30 - hostlen
);
505 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wceddk40/html/cxtsksupportingremotedesktopprotocol.asp */
506 out_uint32_le(s
, g_keyboard_type
);
507 out_uint32_le(s
, g_keyboard_subtype
);
508 out_uint32_le(s
, g_keyboard_functionkeys
);
509 out_uint8s(s
, 64); /* reserved? 4 + 12 doublewords */
510 out_uint16_le(s
, 0xca01); /* colour depth? */
514 out_uint8(s
, g_server_depth
);
515 out_uint16_le(s
, 0x0700);
518 out_uint8s(s
, 64); /* End of client info */
520 out_uint16_le(s
, SEC_TAG_CLI_4
);
521 out_uint16_le(s
, 12);
522 out_uint32_le(s
, g_console_session
? 0xb : 9);
525 /* Client encryption settings */
526 out_uint16_le(s
, SEC_TAG_CLI_CRYPT
);
527 out_uint16_le(s
, 12); /* length */
528 out_uint32_le(s
, g_encryption
? 0x3 : 0); /* encryption supported, 128-bit supported */
529 out_uint32(s
, 0); /* Unknown */
531 DEBUG_RDP5(("g_num_channels is %d\n", g_num_channels
));
532 if (g_num_channels
> 0)
534 out_uint16_le(s
, SEC_TAG_CLI_CHANNELS
);
535 out_uint16_le(s
, g_num_channels
* 12 + 8); /* length */
536 out_uint32_le(s
, g_num_channels
); /* number of virtual channels */
537 for (i
= 0; i
< g_num_channels
; i
++)
539 DEBUG_RDP5(("Requesting channel %s\n", g_channels
[i
].name
));
540 out_uint8a(s
, g_channels
[i
].name
, 8);
541 out_uint32_be(s
, g_channels
[i
].flags
);
548 /* Parse a public key structure */
550 sec_parse_public_key(STREAM s
, uint8
** modulus
, uint8
** exponent
)
552 uint32 magic
, modulus_len
;
554 in_uint32_le(s
, magic
);
555 if (magic
!= SEC_RSA_MAGIC
)
557 error("RSA magic 0x%x\n", magic
);
561 in_uint32_le(s
, modulus_len
);
562 if (modulus_len
!= SEC_MODULUS_SIZE
+ SEC_PADDING_SIZE
)
564 error("modulus len 0x%x\n", modulus_len
);
568 in_uint8s(s
, 8); /* modulus_bits, unknown */
569 in_uint8p(s
, *exponent
, SEC_EXPONENT_SIZE
);
570 in_uint8p(s
, *modulus
, SEC_MODULUS_SIZE
);
571 in_uint8s(s
, SEC_PADDING_SIZE
);
576 /* Parse a crypto information structure */
578 sec_parse_crypt_info(STREAM s
, uint32
* rc4_key_size
,
579 uint8
** server_random
, uint8
** modulus
, uint8
** exponent
)
581 uint32 crypt_level
, random_len
, rsa_info_len
;
582 uint32
/*cacert_len, cert_len,*/ flags
;
583 //X509 *cacert, *server_cert;
585 uint8
*next_tag
, *end
;
587 in_uint32_le(s
, *rc4_key_size
); /* 1 = 40-bit, 2 = 128-bit */
588 in_uint32_le(s
, crypt_level
); /* 1 = low, 2 = medium, 3 = high */
589 if (crypt_level
== 0) /* no encryption */
591 in_uint32_le(s
, random_len
);
592 in_uint32_le(s
, rsa_info_len
);
594 if (random_len
!= SEC_RANDOM_SIZE
)
596 error("random len %d, expected %d\n", random_len
, SEC_RANDOM_SIZE
);
600 in_uint8p(s
, *server_random
, random_len
);
603 end
= s
->p
+ rsa_info_len
;
607 in_uint32_le(s
, flags
); /* 1 = RDP4-style, 0x80000002 = X.509 */
610 DEBUG_RDP5(("We're going for the RDP4-style encryption\n"));
611 in_uint8s(s
, 8); /* unknown */
615 in_uint16_le(s
, tag
);
616 in_uint16_le(s
, length
);
618 next_tag
= s
->p
+ length
;
623 if (!sec_parse_public_key(s
, modulus
, exponent
))
625 DEBUG_RDP5(("Got Public key, RDP4-style\n"));
630 /* Is this a Microsoft key that we just got? */
631 /* Care factor: zero! */
632 /* Actually, it would probably be a good idea to check if the public key is signed with this key, and then store this
633 key as a known key of the hostname. This would prevent some MITM-attacks. */
637 unimpl("crypt tag 0x%x\n", tag
);
648 DEBUG_RDP5(("We're going for the RDP5-style encryption\n"));
649 in_uint32_le(s
, certcount
); /* Number of certificates */
653 error("Server didn't send enough X509 certificates\n");
657 for (; certcount
> 2; certcount
--)
658 { /* ignore all the certificates between the root and the signing CA */
662 DEBUG_RDP5(("Ignored certs left: %d\n", certcount
));
664 in_uint32_le(s
, ignorelen
);
665 DEBUG_RDP5(("Ignored Certificate length is %d\n", ignorelen
));
666 ignorecert
= d2i_X509(NULL
, &(s
->p
), ignorelen
);
668 if (ignorecert
== NULL
)
669 { /* XXX: error out? */
670 DEBUG_RDP5(("got a bad cert: this will probably screw up the rest of the communication\n"));
673 #ifdef WITH_DEBUG_RDP5
674 DEBUG_RDP5(("cert #%d (ignored):\n", certcount
));
675 X509_print_fp(stdout
, ignorecert
);
679 /* Do da funky X.509 stuffy
681 "How did I find out about this? I looked up and saw a
682 bright light and when I came to I had a scar on my forehead
683 and knew about X.500"
684 - Peter Gutman in a early version of
685 http://www.cs.auckland.ac.nz/~pgut001/pubs/x509guide.txt
688 in_uint32_le(s
, cacert_len
);
689 DEBUG_RDP5(("CA Certificate length is %d\n", cacert_len
));
690 cacert
= d2i_X509(NULL
, &(s
->p
), cacert_len
);
691 /* Note: We don't need to move s->p here - d2i_X509 is
692 "kind" enough to do it for us */
695 error("Couldn't load CA Certificate from server\n");
699 /* Currently, we don't use the CA Certificate.
701 *) Verify the server certificate (server_cert) with the
703 *) Store the CA Certificate with the hostname of the
704 server we are connecting to as key, and compare it
705 when we connect the next time, in order to prevent
711 in_uint32_le(s
, cert_len
);
712 DEBUG_RDP5(("Certificate length is %d\n", cert_len
));
713 server_cert
= d2i_X509(NULL
, &(s
->p
), cert_len
);
714 if (NULL
== server_cert
)
716 error("Couldn't load Certificate from server\n");
720 in_uint8s(s
, 16); /* Padding */
722 /* Note: Verifying the server certificate must be done here,
723 before sec_parse_public_key since we'll have to apply
724 serious violence to the key after this */
726 if (!sec_parse_x509_key(server_cert
))
728 DEBUG_RDP5(("Didn't parse X509 correctly\n"));
729 X509_free(server_cert
);
732 X509_free(server_cert
);
733 return True
; /* There's some garbage here we don't care about */
736 return s_check_end(s
);
739 /* Process crypto information blob */
741 sec_process_crypt_info(STREAM s
)
743 uint8
*server_random
, *modulus
= NULL
, *exponent
= NULL
;
744 uint8 client_random
[SEC_RANDOM_SIZE
];
746 uint8 inr
[SEC_MODULUS_SIZE
];
748 if (!sec_parse_crypt_info(s
, &rc4_key_size
, &server_random
, &modulus
, &exponent
))
750 DEBUG(("Failed to parse crypt info\n"));
754 DEBUG(("Generating client random\n"));
755 /* Generate a client random, and hence determine encryption keys */
756 /* This is what the MS client do: */
757 memset(inr
, 0, SEC_RANDOM_SIZE
);
758 /* *ARIGL!* Plaintext attack, anyone?
760 generate_random(inr);
761 ..but that generates connection errors now and then (yes,
762 "now and then". Something like 0 to 3 attempts needed before a
763 successful connection. Nice. Not!
766 generate_random(client_random
);
767 if (NULL
!= server_public_key
)
768 { /* Which means we should use
769 RDP5-style encryption */
771 memcpy(inr
+ SEC_RANDOM_SIZE
, client_random
, SEC_RANDOM_SIZE
);
772 reverse(inr
+ SEC_RANDOM_SIZE
, SEC_RANDOM_SIZE
);
774 RSA_public_encrypt(SEC_MODULUS_SIZE
,
775 inr
, sec_crypted_random
, server_public_key
, RSA_NO_PADDING
);
777 reverse(sec_crypted_random
, SEC_MODULUS_SIZE
);
779 RSA_free(server_public_key
);
780 server_public_key
= NULL
;
784 { /* RDP4-style encryption */
785 sec_rsa_encrypt(sec_crypted_random
,
786 client_random
, SEC_RANDOM_SIZE
, modulus
, exponent
);
788 sec_generate_keys(client_random
, server_random
, rc4_key_size
);
792 /* Process SRV_INFO, find RDP version supported by server */
794 sec_process_srv_info(STREAM s
)
796 in_uint16_le(s
, g_server_rdp_version
);
797 DEBUG_RDP5(("Server RDP version is %d\n", g_server_rdp_version
));
798 if (1 == g_server_rdp_version
)
806 /* Process connect response data blob */
808 sec_process_mcs_data(STREAM s
)
814 in_uint8s(s
, 21); /* header (T.124 ConferenceCreateResponse) */
819 while (s
->p
< s
->end
)
821 in_uint16_le(s
, tag
);
822 in_uint16_le(s
, length
);
827 next_tag
= s
->p
+ length
- 4;
831 case SEC_TAG_SRV_INFO
:
832 sec_process_srv_info(s
);
835 case SEC_TAG_SRV_CRYPT
:
836 sec_process_crypt_info(s
);
839 case SEC_TAG_SRV_CHANNELS
:
840 /* FIXME: We should parse this information and
841 use it to map RDP5 channels to MCS
846 unimpl("response tag 0x%x\n", tag
);
853 /* Receive secure transport packet */
855 sec_recv(uint8
* rdpver
)
861 while ((s
= mcs_recv(&channel
, rdpver
)) != NULL
)
869 in_uint8s(s
, 8); /* signature */
870 sec_decrypt(s
->p
, s
->end
- s
->p
);
875 if (g_encryption
|| !g_licence_issued
)
877 in_uint32_le(s
, sec_flags
);
879 if (sec_flags
& SEC_ENCRYPT
)
881 in_uint8s(s
, 8); /* signature */
882 sec_decrypt(s
->p
, s
->end
- s
->p
);
885 if (sec_flags
& SEC_LICENCE_NEG
)
891 if (sec_flags
& 0x0400) /* SEC_REDIRECT_ENCRYPT */
895 in_uint8s(s
, 8); /* signature */
896 sec_decrypt(s
->p
, s
->end
- s
->p
);
898 /* Check for a redirect packet, starts with 00 04 */
899 if (s
->p
[0] == 0 && s
->p
[1] == 4)
901 /* for some reason the PDU and the length seem to be swapped.
902 This isn't good, but we're going to do a byte for byte
903 swap. So the first foure value appear as: 00 04 XX YY,
904 where XX YY is the little endian length. We're going to
905 use 04 00 as the PDU type, so after our swap this will look
920 /* warning! this debug statement will show passwords in the clear! */
921 hexdump(s
->p
, s
->end
- s
->p
);
927 if (channel
!= MCS_GLOBAL_CHANNEL
)
929 channel_process(s
, channel
);
940 /* Establish a secure connection */
942 sec_connect(char *server
, char *username
)
944 struct stream mcs_data
;
946 /* We exchange some RDP data during the MCS-Connect */
948 mcs_data
.p
= mcs_data
.data
= (uint8
*) xmalloc(mcs_data
.size
);
949 sec_out_mcs_data(&mcs_data
);
951 if (!mcs_connect(server
, &mcs_data
, username
))
954 /* sec_process_mcs_data(&mcs_data); */
957 xfree(mcs_data
.data
);
961 /* Establish a secure connection */
963 sec_reconnect(char *server
)
965 struct stream mcs_data
;
967 /* We exchange some RDP data during the MCS-Connect */
969 mcs_data
.p
= mcs_data
.data
= (uint8
*) xmalloc(mcs_data
.size
);
970 sec_out_mcs_data(&mcs_data
);
972 if (!mcs_reconnect(server
, &mcs_data
))
975 /* sec_process_mcs_data(&mcs_data); */
978 xfree(mcs_data
.data
);
982 /* Disconnect a connection */
989 /* reset the state of the sec layer */
991 sec_reset_state(void)
993 g_server_rdp_version
= 0;
994 sec_encrypt_use_count
= 0;
995 sec_decrypt_use_count
= 0;