1 |
/* -*- c-basic-offset: 8 -*- |
/* -*- 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-2007 |
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 |
|
#include "ssl.h" |
23 |
|
|
24 |
#ifdef WITH_OPENSSL |
extern char g_hostname[16]; |
|
#include <openssl/rc4.h> |
|
|
#include <openssl/md5.h> |
|
|
#include <openssl/sha.h> |
|
|
#include <openssl/bn.h> |
|
|
#include <openssl/x509v3.h> |
|
|
#else |
|
|
#include "crypto/rc4.h" |
|
|
#include "crypto/md5.h" |
|
|
#include "crypto/sha.h" |
|
|
#include "crypto/bn.h" |
|
|
#endif |
|
|
|
|
|
extern char hostname[16]; |
|
25 |
extern int g_width; |
extern int g_width; |
26 |
extern int g_height; |
extern int g_height; |
27 |
extern int keylayout; |
extern unsigned int g_keylayout; |
28 |
extern BOOL g_encryption; |
extern int g_keyboard_type; |
29 |
extern BOOL g_licence_issued; |
extern int g_keyboard_subtype; |
30 |
extern BOOL g_use_rdp5; |
extern int g_keyboard_functionkeys; |
31 |
extern BOOL g_console_session; |
extern RD_BOOL g_encryption; |
32 |
extern int g_server_bpp; |
extern RD_BOOL g_licence_issued; |
33 |
extern uint16 mcs_userid; |
extern RD_BOOL g_use_rdp5; |
34 |
|
extern RD_BOOL g_console_session; |
35 |
|
extern int g_server_depth; |
36 |
extern VCHANNEL g_channels[]; |
extern VCHANNEL g_channels[]; |
37 |
extern unsigned int g_num_channels; |
extern unsigned int g_num_channels; |
38 |
|
|
39 |
static int rc4_key_len; |
static int g_rc4_key_len; |
40 |
static RC4_KEY rc4_decrypt_key; |
static SSL_RC4 g_rc4_decrypt_key; |
41 |
static RC4_KEY rc4_encrypt_key; |
static SSL_RC4 g_rc4_encrypt_key; |
42 |
static RSA *server_public_key; |
static uint32 g_server_public_key_len; |
43 |
|
|
44 |
static uint8 sec_sign_key[16]; |
static uint8 g_sec_sign_key[16]; |
45 |
static uint8 sec_decrypt_key[16]; |
static uint8 g_sec_decrypt_key[16]; |
46 |
static uint8 sec_encrypt_key[16]; |
static uint8 g_sec_encrypt_key[16]; |
47 |
static uint8 sec_decrypt_update_key[16]; |
static uint8 g_sec_decrypt_update_key[16]; |
48 |
static uint8 sec_encrypt_update_key[16]; |
static uint8 g_sec_encrypt_update_key[16]; |
49 |
static uint8 sec_crypted_random[SEC_MODULUS_SIZE]; |
static uint8 g_sec_crypted_random[SEC_MAX_MODULUS_SIZE]; |
50 |
|
|
51 |
uint16 g_server_rdp_version = 0; |
uint16 g_server_rdp_version = 0; |
52 |
|
|
53 |
|
/* These values must be available to reset state - Session Directory */ |
54 |
|
static int g_sec_encrypt_use_count = 0; |
55 |
|
static int g_sec_decrypt_use_count = 0; |
56 |
|
|
57 |
/* |
/* |
58 |
* General purpose 48-byte transformation, using two 32-byte salts (generally, |
* I believe this is based on SSLv3 with the following differences: |
59 |
* a client and server salt) and a global salt value used for padding. |
* MAC algorithm (5.2.3.1) uses only 32-bit length in place of seq_num/type/length fields |
60 |
|
* MAC algorithm uses SHA1 and MD5 for the two hash functions instead of one or other |
61 |
|
* key_block algorithm (6.2.2) uses 'X', 'YY', 'ZZZ' instead of 'A', 'BB', 'CCC' |
62 |
|
* key_block partitioning is different (16 bytes each: MAC secret, decrypt key, encrypt key) |
63 |
|
* encryption/decryption keys updated every 4096 packets |
64 |
|
* See http://wp.netscape.com/eng/ssl3/draft302.txt |
65 |
|
*/ |
66 |
|
|
67 |
|
/* |
68 |
|
* 48-byte transformation used to generate master secret (6.1) and key material (6.2.2). |
69 |
* Both SHA1 and MD5 algorithms are used. |
* Both SHA1 and MD5 algorithms are used. |
70 |
*/ |
*/ |
71 |
void |
void |
73 |
{ |
{ |
74 |
uint8 shasig[20]; |
uint8 shasig[20]; |
75 |
uint8 pad[4]; |
uint8 pad[4]; |
76 |
SHA_CTX sha; |
SSL_SHA1 sha1; |
77 |
MD5_CTX md5; |
SSL_MD5 md5; |
78 |
int i; |
int i; |
79 |
|
|
80 |
for (i = 0; i < 3; i++) |
for (i = 0; i < 3; i++) |
81 |
{ |
{ |
82 |
memset(pad, salt + i, i + 1); |
memset(pad, salt + i, i + 1); |
83 |
|
|
84 |
SHA1_Init(&sha); |
ssl_sha1_init(&sha1); |
85 |
SHA1_Update(&sha, pad, i + 1); |
ssl_sha1_update(&sha1, pad, i + 1); |
86 |
SHA1_Update(&sha, in, 48); |
ssl_sha1_update(&sha1, in, 48); |
87 |
SHA1_Update(&sha, salt1, 32); |
ssl_sha1_update(&sha1, salt1, 32); |
88 |
SHA1_Update(&sha, salt2, 32); |
ssl_sha1_update(&sha1, salt2, 32); |
89 |
SHA1_Final(shasig, &sha); |
ssl_sha1_final(&sha1, shasig); |
90 |
|
|
91 |
MD5_Init(&md5); |
ssl_md5_init(&md5); |
92 |
MD5_Update(&md5, in, 48); |
ssl_md5_update(&md5, in, 48); |
93 |
MD5_Update(&md5, shasig, 20); |
ssl_md5_update(&md5, shasig, 20); |
94 |
MD5_Final(&out[i * 16], &md5); |
ssl_md5_final(&md5, &out[i * 16]); |
95 |
} |
} |
96 |
} |
} |
97 |
|
|
98 |
/* |
/* |
99 |
* Weaker 16-byte transformation, also using two 32-byte salts, but |
* 16-byte transformation used to generate export keys (6.2.2). |
|
* only using a single round of MD5. |
|
100 |
*/ |
*/ |
101 |
void |
void |
102 |
sec_hash_16(uint8 * out, uint8 * in, uint8 * salt1, uint8 * salt2) |
sec_hash_16(uint8 * out, uint8 * in, uint8 * salt1, uint8 * salt2) |
103 |
{ |
{ |
104 |
MD5_CTX md5; |
SSL_MD5 md5; |
105 |
|
|
106 |
MD5_Init(&md5); |
ssl_md5_init(&md5); |
107 |
MD5_Update(&md5, in, 16); |
ssl_md5_update(&md5, in, 16); |
108 |
MD5_Update(&md5, salt1, 32); |
ssl_md5_update(&md5, salt1, 32); |
109 |
MD5_Update(&md5, salt2, 32); |
ssl_md5_update(&md5, salt2, 32); |
110 |
MD5_Final(out, &md5); |
ssl_md5_final(&md5, out); |
111 |
} |
} |
112 |
|
|
113 |
/* Reduce key entropy from 64 to 40 bits */ |
/* Reduce key entropy from 64 to 40 bits */ |
119 |
key[2] = 0x9e; |
key[2] = 0x9e; |
120 |
} |
} |
121 |
|
|
122 |
/* Generate a session key and RC4 keys, given client and server randoms */ |
/* Generate encryption keys given client and server randoms */ |
123 |
static void |
static void |
124 |
sec_generate_keys(uint8 * client_key, uint8 * server_key, int rc4_key_size) |
sec_generate_keys(uint8 * client_random, uint8 * server_random, int rc4_key_size) |
125 |
{ |
{ |
126 |
uint8 session_key[48]; |
uint8 pre_master_secret[48]; |
127 |
uint8 temp_hash[48]; |
uint8 master_secret[48]; |
128 |
uint8 input[48]; |
uint8 key_block[48]; |
129 |
|
|
130 |
/* Construct input data to hash */ |
/* Construct pre-master secret */ |
131 |
memcpy(input, client_key, 24); |
memcpy(pre_master_secret, client_random, 24); |
132 |
memcpy(input + 24, server_key, 24); |
memcpy(pre_master_secret + 24, server_random, 24); |
133 |
|
|
134 |
/* Generate session key - two rounds of sec_hash_48 */ |
/* Generate master secret and then key material */ |
135 |
sec_hash_48(temp_hash, input, client_key, server_key, 65); |
sec_hash_48(master_secret, pre_master_secret, client_random, server_random, 'A'); |
136 |
sec_hash_48(session_key, temp_hash, client_key, server_key, 88); |
sec_hash_48(key_block, master_secret, client_random, server_random, 'X'); |
137 |
|
|
138 |
/* Store first 16 bytes of session key, for generating signatures */ |
/* First 16 bytes of key material is MAC secret */ |
139 |
memcpy(sec_sign_key, session_key, 16); |
memcpy(g_sec_sign_key, key_block, 16); |
140 |
|
|
141 |
/* Generate RC4 keys */ |
/* Generate export keys from next two blocks of 16 bytes */ |
142 |
sec_hash_16(sec_decrypt_key, &session_key[16], client_key, server_key); |
sec_hash_16(g_sec_decrypt_key, &key_block[16], client_random, server_random); |
143 |
sec_hash_16(sec_encrypt_key, &session_key[32], client_key, server_key); |
sec_hash_16(g_sec_encrypt_key, &key_block[32], client_random, server_random); |
144 |
|
|
145 |
if (rc4_key_size == 1) |
if (rc4_key_size == 1) |
146 |
{ |
{ |
147 |
DEBUG(("40-bit encryption enabled\n")); |
DEBUG(("40-bit encryption enabled\n")); |
148 |
sec_make_40bit(sec_sign_key); |
sec_make_40bit(g_sec_sign_key); |
149 |
sec_make_40bit(sec_decrypt_key); |
sec_make_40bit(g_sec_decrypt_key); |
150 |
sec_make_40bit(sec_encrypt_key); |
sec_make_40bit(g_sec_encrypt_key); |
151 |
rc4_key_len = 8; |
g_rc4_key_len = 8; |
152 |
} |
} |
153 |
else |
else |
154 |
{ |
{ |
155 |
DEBUG(("rc_4_key_size == %d, 128-bit encryption enabled\n", rc4_key_size)); |
DEBUG(("rc_4_key_size == %d, 128-bit encryption enabled\n", rc4_key_size)); |
156 |
rc4_key_len = 16; |
g_rc4_key_len = 16; |
157 |
} |
} |
158 |
|
|
159 |
/* Save initial RC4 keys as update keys */ |
/* Save initial RC4 keys as update keys */ |
160 |
memcpy(sec_decrypt_update_key, sec_decrypt_key, 16); |
memcpy(g_sec_decrypt_update_key, g_sec_decrypt_key, 16); |
161 |
memcpy(sec_encrypt_update_key, sec_encrypt_key, 16); |
memcpy(g_sec_encrypt_update_key, g_sec_encrypt_key, 16); |
162 |
|
|
163 |
/* Initialise RC4 state arrays */ |
/* Initialise RC4 state arrays */ |
164 |
RC4_set_key(&rc4_decrypt_key, rc4_key_len, sec_decrypt_key); |
ssl_rc4_set_key(&g_rc4_decrypt_key, g_sec_decrypt_key, g_rc4_key_len); |
165 |
RC4_set_key(&rc4_encrypt_key, rc4_key_len, sec_encrypt_key); |
ssl_rc4_set_key(&g_rc4_encrypt_key, g_sec_encrypt_key, g_rc4_key_len); |
166 |
} |
} |
167 |
|
|
168 |
static uint8 pad_54[40] = { |
static uint8 pad_54[40] = { |
189 |
buffer[3] = (value >> 24) & 0xff; |
buffer[3] = (value >> 24) & 0xff; |
190 |
} |
} |
191 |
|
|
192 |
/* Generate a signature hash, using a combination of SHA1 and MD5 */ |
/* Generate a MAC hash (5.2.3.1), using a combination of SHA1 and MD5 */ |
193 |
void |
void |
194 |
sec_sign(uint8 * signature, int siglen, uint8 * session_key, int keylen, uint8 * data, int datalen) |
sec_sign(uint8 * signature, int siglen, uint8 * session_key, int keylen, uint8 * data, int datalen) |
195 |
{ |
{ |
196 |
uint8 shasig[20]; |
uint8 shasig[20]; |
197 |
uint8 md5sig[16]; |
uint8 md5sig[16]; |
198 |
uint8 lenhdr[4]; |
uint8 lenhdr[4]; |
199 |
SHA_CTX sha; |
SSL_SHA1 sha1; |
200 |
MD5_CTX md5; |
SSL_MD5 md5; |
201 |
|
|
202 |
buf_out_uint32(lenhdr, datalen); |
buf_out_uint32(lenhdr, datalen); |
203 |
|
|
204 |
SHA1_Init(&sha); |
ssl_sha1_init(&sha1); |
205 |
SHA1_Update(&sha, session_key, keylen); |
ssl_sha1_update(&sha1, session_key, keylen); |
206 |
SHA1_Update(&sha, pad_54, 40); |
ssl_sha1_update(&sha1, pad_54, 40); |
207 |
SHA1_Update(&sha, lenhdr, 4); |
ssl_sha1_update(&sha1, lenhdr, 4); |
208 |
SHA1_Update(&sha, data, datalen); |
ssl_sha1_update(&sha1, data, datalen); |
209 |
SHA1_Final(shasig, &sha); |
ssl_sha1_final(&sha1, shasig); |
210 |
|
|
211 |
MD5_Init(&md5); |
ssl_md5_init(&md5); |
212 |
MD5_Update(&md5, session_key, keylen); |
ssl_md5_update(&md5, session_key, keylen); |
213 |
MD5_Update(&md5, pad_92, 48); |
ssl_md5_update(&md5, pad_92, 48); |
214 |
MD5_Update(&md5, shasig, 20); |
ssl_md5_update(&md5, shasig, 20); |
215 |
MD5_Final(md5sig, &md5); |
ssl_md5_final(&md5, md5sig); |
216 |
|
|
217 |
memcpy(signature, md5sig, siglen); |
memcpy(signature, md5sig, siglen); |
218 |
} |
} |
219 |
|
|
220 |
/* Update an encryption key - similar to the signing process */ |
/* Update an encryption key */ |
221 |
static void |
static void |
222 |
sec_update(uint8 * key, uint8 * update_key) |
sec_update(uint8 * key, uint8 * update_key) |
223 |
{ |
{ |
224 |
uint8 shasig[20]; |
uint8 shasig[20]; |
225 |
SHA_CTX sha; |
SSL_SHA1 sha1; |
226 |
MD5_CTX md5; |
SSL_MD5 md5; |
227 |
RC4_KEY update; |
SSL_RC4 update; |
228 |
|
|
229 |
SHA1_Init(&sha); |
ssl_sha1_init(&sha1); |
230 |
SHA1_Update(&sha, update_key, rc4_key_len); |
ssl_sha1_update(&sha1, update_key, g_rc4_key_len); |
231 |
SHA1_Update(&sha, pad_54, 40); |
ssl_sha1_update(&sha1, pad_54, 40); |
232 |
SHA1_Update(&sha, key, rc4_key_len); |
ssl_sha1_update(&sha1, key, g_rc4_key_len); |
233 |
SHA1_Final(shasig, &sha); |
ssl_sha1_final(&sha1, shasig); |
234 |
|
|
235 |
MD5_Init(&md5); |
ssl_md5_init(&md5); |
236 |
MD5_Update(&md5, update_key, rc4_key_len); |
ssl_md5_update(&md5, update_key, g_rc4_key_len); |
237 |
MD5_Update(&md5, pad_92, 48); |
ssl_md5_update(&md5, pad_92, 48); |
238 |
MD5_Update(&md5, shasig, 20); |
ssl_md5_update(&md5, shasig, 20); |
239 |
MD5_Final(key, &md5); |
ssl_md5_final(&md5, key); |
240 |
|
|
241 |
RC4_set_key(&update, rc4_key_len, key); |
ssl_rc4_set_key(&update, key, g_rc4_key_len); |
242 |
RC4(&update, rc4_key_len, key, key); |
ssl_rc4_crypt(&update, key, key, g_rc4_key_len); |
243 |
|
|
244 |
if (rc4_key_len == 8) |
if (g_rc4_key_len == 8) |
245 |
sec_make_40bit(key); |
sec_make_40bit(key); |
246 |
} |
} |
247 |
|
|
249 |
static void |
static void |
250 |
sec_encrypt(uint8 * data, int length) |
sec_encrypt(uint8 * data, int length) |
251 |
{ |
{ |
252 |
static int use_count; |
if (g_sec_encrypt_use_count == 4096) |
|
|
|
|
if (use_count == 4096) |
|
253 |
{ |
{ |
254 |
sec_update(sec_encrypt_key, sec_encrypt_update_key); |
sec_update(g_sec_encrypt_key, g_sec_encrypt_update_key); |
255 |
RC4_set_key(&rc4_encrypt_key, rc4_key_len, sec_encrypt_key); |
ssl_rc4_set_key(&g_rc4_encrypt_key, g_sec_encrypt_key, g_rc4_key_len); |
256 |
use_count = 0; |
g_sec_encrypt_use_count = 0; |
257 |
} |
} |
258 |
|
|
259 |
RC4(&rc4_encrypt_key, length, data, data); |
ssl_rc4_crypt(&g_rc4_encrypt_key, data, data, length); |
260 |
use_count++; |
g_sec_encrypt_use_count++; |
261 |
} |
} |
262 |
|
|
263 |
/* Decrypt data using RC4 */ |
/* Decrypt data using RC4 */ |
264 |
void |
void |
265 |
sec_decrypt(uint8 * data, int length) |
sec_decrypt(uint8 * data, int length) |
266 |
{ |
{ |
267 |
static int use_count; |
if (g_sec_decrypt_use_count == 4096) |
|
|
|
|
if (use_count == 4096) |
|
268 |
{ |
{ |
269 |
sec_update(sec_decrypt_key, sec_decrypt_update_key); |
sec_update(g_sec_decrypt_key, g_sec_decrypt_update_key); |
270 |
RC4_set_key(&rc4_decrypt_key, rc4_key_len, sec_decrypt_key); |
ssl_rc4_set_key(&g_rc4_decrypt_key, g_sec_decrypt_key, g_rc4_key_len); |
271 |
use_count = 0; |
g_sec_decrypt_use_count = 0; |
272 |
} |
} |
273 |
|
|
274 |
RC4(&rc4_decrypt_key, length, data, data); |
ssl_rc4_crypt(&g_rc4_decrypt_key, data, data, length); |
275 |
use_count++; |
g_sec_decrypt_use_count++; |
|
} |
|
|
|
|
|
static void |
|
|
reverse(uint8 * p, int len) |
|
|
{ |
|
|
int i, j; |
|
|
uint8 temp; |
|
|
|
|
|
for (i = 0, j = len - 1; i < j; i++, j--) |
|
|
{ |
|
|
temp = p[i]; |
|
|
p[i] = p[j]; |
|
|
p[j] = temp; |
|
|
} |
|
276 |
} |
} |
277 |
|
|
278 |
/* Perform an RSA public key encryption operation */ |
/* Perform an RSA public key encryption operation */ |
279 |
static void |
static void |
280 |
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, |
281 |
|
uint8 * exponent) |
282 |
{ |
{ |
283 |
BN_CTX *ctx; |
ssl_rsa_encrypt(out, in, len, modulus_size, modulus, exponent); |
|
BIGNUM mod, exp, x, y; |
|
|
uint8 inr[SEC_MODULUS_SIZE]; |
|
|
int outlen; |
|
|
|
|
|
reverse(modulus, SEC_MODULUS_SIZE); |
|
|
reverse(exponent, SEC_EXPONENT_SIZE); |
|
|
memcpy(inr, in, len); |
|
|
reverse(inr, len); |
|
|
|
|
|
ctx = BN_CTX_new(); |
|
|
BN_init(&mod); |
|
|
BN_init(&exp); |
|
|
BN_init(&x); |
|
|
BN_init(&y); |
|
|
|
|
|
BN_bin2bn(modulus, SEC_MODULUS_SIZE, &mod); |
|
|
BN_bin2bn(exponent, SEC_EXPONENT_SIZE, &exp); |
|
|
BN_bin2bn(inr, len, &x); |
|
|
BN_mod_exp(&y, &x, &exp, &mod, ctx); |
|
|
outlen = BN_bn2bin(&y, out); |
|
|
reverse(out, outlen); |
|
|
if (outlen < SEC_MODULUS_SIZE) |
|
|
memset(out + outlen, 0, SEC_MODULUS_SIZE - outlen); |
|
|
|
|
|
BN_free(&y); |
|
|
BN_clear_free(&x); |
|
|
BN_free(&exp); |
|
|
BN_free(&mod); |
|
|
BN_CTX_free(ctx); |
|
284 |
} |
} |
285 |
|
|
286 |
/* Initialise secure transport packet */ |
/* Initialise secure transport packet */ |
306 |
{ |
{ |
307 |
int datalen; |
int datalen; |
308 |
|
|
309 |
|
#ifdef WITH_SCARD |
310 |
|
scard_lock(SCARD_LOCK_SEC); |
311 |
|
#endif |
312 |
|
|
313 |
s_pop_layer(s, sec_hdr); |
s_pop_layer(s, sec_hdr); |
314 |
if (!g_licence_issued || (flags & SEC_ENCRYPT)) |
if (!g_licence_issued || (flags & SEC_ENCRYPT)) |
315 |
out_uint32_le(s, flags); |
out_uint32_le(s, flags); |
324 |
hexdump(s->p + 8, datalen); |
hexdump(s->p + 8, datalen); |
325 |
#endif |
#endif |
326 |
|
|
327 |
sec_sign(s->p, 8, sec_sign_key, rc4_key_len, s->p + 8, datalen); |
sec_sign(s->p, 8, g_sec_sign_key, g_rc4_key_len, s->p + 8, datalen); |
328 |
sec_encrypt(s->p + 8, datalen); |
sec_encrypt(s->p + 8, datalen); |
329 |
} |
} |
330 |
|
|
331 |
mcs_send_to_channel(s, channel); |
mcs_send_to_channel(s, channel); |
332 |
|
|
333 |
|
#ifdef WITH_SCARD |
334 |
|
scard_unlock(SCARD_LOCK_SEC); |
335 |
|
#endif |
336 |
} |
} |
337 |
|
|
338 |
/* Transmit secure transport packet */ |
/* Transmit secure transport packet */ |
348 |
static void |
static void |
349 |
sec_establish_key(void) |
sec_establish_key(void) |
350 |
{ |
{ |
351 |
uint32 length = SEC_MODULUS_SIZE + SEC_PADDING_SIZE; |
uint32 length = g_server_public_key_len + SEC_PADDING_SIZE; |
352 |
uint32 flags = SEC_CLIENT_RANDOM; |
uint32 flags = SEC_CLIENT_RANDOM; |
353 |
STREAM s; |
STREAM s; |
354 |
|
|
355 |
s = sec_init(flags, 76); |
s = sec_init(flags, length + 4); |
356 |
|
|
357 |
out_uint32_le(s, length); |
out_uint32_le(s, length); |
358 |
out_uint8p(s, sec_crypted_random, SEC_MODULUS_SIZE); |
out_uint8p(s, g_sec_crypted_random, g_server_public_key_len); |
359 |
out_uint8s(s, SEC_PADDING_SIZE); |
out_uint8s(s, SEC_PADDING_SIZE); |
360 |
|
|
361 |
s_mark_end(s); |
s_mark_end(s); |
366 |
static void |
static void |
367 |
sec_out_mcs_data(STREAM s) |
sec_out_mcs_data(STREAM s) |
368 |
{ |
{ |
369 |
int hostlen = 2 * strlen(hostname); |
int hostlen = 2 * strlen(g_hostname); |
370 |
int length = 158 + 76 + 12 + 4; |
int length = 158 + 76 + 12 + 4; |
371 |
unsigned int i; |
unsigned int i; |
372 |
|
|
402 |
out_uint16_le(s, g_height); |
out_uint16_le(s, g_height); |
403 |
out_uint16_le(s, 0xca01); |
out_uint16_le(s, 0xca01); |
404 |
out_uint16_le(s, 0xaa03); |
out_uint16_le(s, 0xaa03); |
405 |
out_uint32_le(s, keylayout); |
out_uint32_le(s, g_keylayout); |
406 |
out_uint32_le(s, 2600); /* Client build. We are now 2600 compatible :-) */ |
out_uint32_le(s, 2600); /* Client build. We are now 2600 compatible :-) */ |
407 |
|
|
408 |
/* Unicode name of client, padded to 32 bytes */ |
/* Unicode name of client, padded to 32 bytes */ |
409 |
rdp_out_unistr(s, hostname, hostlen); |
rdp_out_unistr(s, g_hostname, hostlen); |
410 |
out_uint8s(s, 30 - hostlen); |
out_uint8s(s, 30 - hostlen); |
411 |
|
|
412 |
out_uint32_le(s, 4); |
/* See |
413 |
out_uint32(s, 0); |
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wceddk40/html/cxtsksupportingremotedesktopprotocol.asp */ |
414 |
out_uint32_le(s, 12); |
out_uint32_le(s, g_keyboard_type); |
415 |
|
out_uint32_le(s, g_keyboard_subtype); |
416 |
|
out_uint32_le(s, g_keyboard_functionkeys); |
417 |
out_uint8s(s, 64); /* reserved? 4 + 12 doublewords */ |
out_uint8s(s, 64); /* reserved? 4 + 12 doublewords */ |
418 |
out_uint16_le(s, 0xca01); /* colour depth? */ |
out_uint16_le(s, 0xca01); /* colour depth? */ |
419 |
out_uint16_le(s, 1); |
out_uint16_le(s, 1); |
420 |
|
|
421 |
out_uint32(s, 0); |
out_uint32(s, 0); |
422 |
out_uint8(s, g_server_bpp); |
out_uint8(s, g_server_depth); |
423 |
out_uint16_le(s, 0x0700); |
out_uint16_le(s, 0x0700); |
424 |
out_uint8(s, 0); |
out_uint8(s, 0); |
425 |
out_uint32_le(s, 1); |
out_uint32_le(s, 1); |
454 |
} |
} |
455 |
|
|
456 |
/* Parse a public key structure */ |
/* Parse a public key structure */ |
457 |
static BOOL |
static RD_BOOL |
458 |
sec_parse_public_key(STREAM s, uint8 ** modulus, uint8 ** exponent) |
sec_parse_public_key(STREAM s, uint8 * modulus, uint8 * exponent) |
459 |
{ |
{ |
460 |
uint32 magic, modulus_len; |
uint32 magic, modulus_len; |
461 |
|
|
467 |
} |
} |
468 |
|
|
469 |
in_uint32_le(s, modulus_len); |
in_uint32_le(s, modulus_len); |
470 |
if (modulus_len != SEC_MODULUS_SIZE + SEC_PADDING_SIZE) |
modulus_len -= SEC_PADDING_SIZE; |
471 |
|
if ((modulus_len < SEC_MODULUS_SIZE) || (modulus_len > SEC_MAX_MODULUS_SIZE)) |
472 |
{ |
{ |
473 |
error("modulus len 0x%x\n", modulus_len); |
error("Bad server public key size (%u bits)\n", modulus_len * 8); |
474 |
return False; |
return False; |
475 |
} |
} |
476 |
|
|
477 |
in_uint8s(s, 8); /* modulus_bits, unknown */ |
in_uint8s(s, 8); /* modulus_bits, unknown */ |
478 |
in_uint8p(s, *exponent, SEC_EXPONENT_SIZE); |
in_uint8a(s, exponent, SEC_EXPONENT_SIZE); |
479 |
in_uint8p(s, *modulus, SEC_MODULUS_SIZE); |
in_uint8a(s, modulus, modulus_len); |
480 |
in_uint8s(s, SEC_PADDING_SIZE); |
in_uint8s(s, SEC_PADDING_SIZE); |
481 |
|
g_server_public_key_len = modulus_len; |
482 |
|
|
483 |
return s_check(s); |
return s_check(s); |
484 |
} |
} |
485 |
|
|
486 |
static BOOL |
/* Parse a public signature structure */ |
487 |
sec_parse_x509_key(X509 * cert) |
static RD_BOOL |
488 |
{ |
sec_parse_public_sig(STREAM s, uint32 len, uint8 * modulus, uint8 * exponent) |
489 |
EVP_PKEY *epk = NULL; |
{ |
490 |
/* By some reason, Microsoft sets the OID of the Public RSA key to |
uint8 signature[SEC_MAX_MODULUS_SIZE]; |
491 |
the oid for "MD5 with RSA Encryption" instead of "RSA Encryption" |
uint32 sig_len; |
492 |
|
|
493 |
Kudos to Richard Levitte for the following (. intiutive .) |
if (len != 72) |
494 |
lines of code that resets the OID and let's us extract the key. */ |
{ |
495 |
if (OBJ_obj2nid(cert->cert_info->key->algor->algorithm) == NID_md5WithRSAEncryption) |
return True; |
496 |
{ |
} |
497 |
DEBUG_RDP5(("Re-setting algorithm type to RSA in server certificate\n")); |
memset(signature, 0, sizeof(signature)); |
498 |
cert->cert_info->key->algor->algorithm = OBJ_nid2obj(NID_rsaEncryption); |
sig_len = len - 8; |
499 |
} |
in_uint8a(s, signature, sig_len); |
500 |
epk = X509_get_pubkey(cert); |
return ssl_sig_ok(exponent, SEC_EXPONENT_SIZE, modulus, g_server_public_key_len, |
501 |
if (NULL == epk) |
signature, sig_len); |
|
{ |
|
|
error("Failed to extract public key from certificate\n"); |
|
|
return False; |
|
|
} |
|
|
|
|
|
server_public_key = (RSA *) epk->pkey.ptr; |
|
|
|
|
|
return True; |
|
502 |
} |
} |
503 |
|
|
|
|
|
504 |
/* Parse a crypto information structure */ |
/* Parse a crypto information structure */ |
505 |
static BOOL |
static RD_BOOL |
506 |
sec_parse_crypt_info(STREAM s, uint32 * rc4_key_size, |
sec_parse_crypt_info(STREAM s, uint32 * rc4_key_size, |
507 |
uint8 ** server_random, uint8 ** modulus, uint8 ** exponent) |
uint8 ** server_random, uint8 * modulus, uint8 * exponent) |
508 |
{ |
{ |
509 |
uint32 crypt_level, random_len, rsa_info_len; |
uint32 crypt_level, random_len, rsa_info_len; |
510 |
uint32 cacert_len, cert_len, flags; |
uint32 cacert_len, cert_len, flags; |
511 |
X509 *cacert, *server_cert; |
SSL_CERT *cacert, *server_cert; |
512 |
|
SSL_RKEY *server_public_key; |
513 |
uint16 tag, length; |
uint16 tag, length; |
514 |
uint8 *next_tag, *end; |
uint8 *next_tag, *end; |
515 |
|
|
556 |
break; |
break; |
557 |
|
|
558 |
case SEC_TAG_KEYSIG: |
case SEC_TAG_KEYSIG: |
559 |
/* Is this a Microsoft key that we just got? */ |
if (!sec_parse_public_sig(s, length, modulus, exponent)) |
560 |
/* Care factor: zero! */ |
return False; |
|
/* Actually, it would probably be a good idea to check if the public key is signed with this key, and then store this |
|
|
key as a known key of the hostname. This would prevent some MITM-attacks. */ |
|
561 |
break; |
break; |
562 |
|
|
563 |
default: |
default: |
573 |
|
|
574 |
DEBUG_RDP5(("We're going for the RDP5-style encryption\n")); |
DEBUG_RDP5(("We're going for the RDP5-style encryption\n")); |
575 |
in_uint32_le(s, certcount); /* Number of certificates */ |
in_uint32_le(s, certcount); /* Number of certificates */ |
|
|
|
576 |
if (certcount < 2) |
if (certcount < 2) |
577 |
{ |
{ |
578 |
error("Server didn't send enough X509 certificates\n"); |
error("Server didn't send enough X509 certificates\n"); |
579 |
return False; |
return False; |
580 |
} |
} |
|
|
|
581 |
for (; certcount > 2; certcount--) |
for (; certcount > 2; certcount--) |
582 |
{ /* ignore all the certificates between the root and the signing CA */ |
{ /* ignore all the certificates between the root and the signing CA */ |
583 |
uint32 ignorelen; |
uint32 ignorelen; |
584 |
X509 *ignorecert; |
SSL_CERT *ignorecert; |
585 |
|
|
586 |
DEBUG_RDP5(("Ignored certs left: %d\n", certcount)); |
DEBUG_RDP5(("Ignored certs left: %d\n", certcount)); |
|
|
|
587 |
in_uint32_le(s, ignorelen); |
in_uint32_le(s, ignorelen); |
588 |
DEBUG_RDP5(("Ignored Certificate length is %d\n", ignorelen)); |
DEBUG_RDP5(("Ignored Certificate length is %d\n", ignorelen)); |
589 |
ignorecert = d2i_X509(NULL, &(s->p), ignorelen); |
ignorecert = ssl_cert_read(s->p, ignorelen); |
590 |
|
in_uint8s(s, ignorelen); |
591 |
if (ignorecert == NULL) |
if (ignorecert == NULL) |
592 |
{ /* XXX: error out? */ |
{ /* XXX: error out? */ |
593 |
DEBUG_RDP5(("got a bad cert: this will probably screw up the rest of the communication\n")); |
DEBUG_RDP5(("got a bad cert: this will probably screw up the rest of the communication\n")); |
595 |
|
|
596 |
#ifdef WITH_DEBUG_RDP5 |
#ifdef WITH_DEBUG_RDP5 |
597 |
DEBUG_RDP5(("cert #%d (ignored):\n", certcount)); |
DEBUG_RDP5(("cert #%d (ignored):\n", certcount)); |
598 |
X509_print_fp(stdout, ignorecert); |
ssl_cert_print_fp(stdout, ignorecert); |
599 |
#endif |
#endif |
600 |
} |
} |
601 |
|
/* Do da funky X.509 stuffy |
|
/* Do da funky X.509 stuffy |
|
602 |
|
|
603 |
"How did I find out about this? I looked up and saw a |
"How did I find out about this? I looked up and saw a |
604 |
bright light and when I came to I had a scar on my forehead |
bright light and when I came to I had a scar on my forehead |
606 |
- Peter Gutman in a early version of |
- Peter Gutman in a early version of |
607 |
http://www.cs.auckland.ac.nz/~pgut001/pubs/x509guide.txt |
http://www.cs.auckland.ac.nz/~pgut001/pubs/x509guide.txt |
608 |
*/ |
*/ |
|
|
|
609 |
in_uint32_le(s, cacert_len); |
in_uint32_le(s, cacert_len); |
610 |
DEBUG_RDP5(("CA Certificate length is %d\n", cacert_len)); |
DEBUG_RDP5(("CA Certificate length is %d\n", cacert_len)); |
611 |
cacert = d2i_X509(NULL, &(s->p), cacert_len); |
cacert = ssl_cert_read(s->p, cacert_len); |
612 |
/* Note: We don't need to move s->p here - d2i_X509 is |
in_uint8s(s, cacert_len); |
|
"kind" enough to do it for us */ |
|
613 |
if (NULL == cacert) |
if (NULL == cacert) |
614 |
{ |
{ |
615 |
error("Couldn't load CA Certificate from server\n"); |
error("Couldn't load CA Certificate from server\n"); |
616 |
return False; |
return False; |
617 |
} |
} |
|
|
|
|
/* Currently, we don't use the CA Certificate. |
|
|
FIXME: |
|
|
*) Verify the server certificate (server_cert) with the |
|
|
CA certificate. |
|
|
*) Store the CA Certificate with the hostname of the |
|
|
server we are connecting to as key, and compare it |
|
|
when we connect the next time, in order to prevent |
|
|
MITM-attacks. |
|
|
*/ |
|
|
|
|
618 |
in_uint32_le(s, cert_len); |
in_uint32_le(s, cert_len); |
619 |
DEBUG_RDP5(("Certificate length is %d\n", cert_len)); |
DEBUG_RDP5(("Certificate length is %d\n", cert_len)); |
620 |
server_cert = d2i_X509(NULL, &(s->p), cert_len); |
server_cert = ssl_cert_read(s->p, cert_len); |
621 |
|
in_uint8s(s, cert_len); |
622 |
if (NULL == server_cert) |
if (NULL == server_cert) |
623 |
{ |
{ |
624 |
|
ssl_cert_free(cacert); |
625 |
error("Couldn't load Certificate from server\n"); |
error("Couldn't load Certificate from server\n"); |
626 |
return False; |
return False; |
627 |
} |
} |
628 |
|
if (!ssl_certs_ok(server_cert, cacert)) |
629 |
|
{ |
630 |
|
ssl_cert_free(server_cert); |
631 |
|
ssl_cert_free(cacert); |
632 |
|
error("Security error CA Certificate invalid\n"); |
633 |
|
return False; |
634 |
|
} |
635 |
|
ssl_cert_free(cacert); |
636 |
in_uint8s(s, 16); /* Padding */ |
in_uint8s(s, 16); /* Padding */ |
637 |
|
server_public_key = ssl_cert_to_rkey(server_cert, &g_server_public_key_len); |
638 |
/* Note: Verifying the server certificate must be done here, |
if (NULL == server_public_key) |
|
before sec_parse_public_key since we'll have to apply |
|
|
serious violence to the key after this */ |
|
|
|
|
|
if (!sec_parse_x509_key(server_cert)) |
|
639 |
{ |
{ |
640 |
DEBUG_RDP5(("Didn't parse X509 correctly\n")); |
DEBUG_RDP5(("Didn't parse X509 correctly\n")); |
641 |
|
ssl_cert_free(server_cert); |
642 |
return False; |
return False; |
643 |
} |
} |
644 |
|
ssl_cert_free(server_cert); |
645 |
|
if ((g_server_public_key_len < SEC_MODULUS_SIZE) || |
646 |
|
(g_server_public_key_len > SEC_MAX_MODULUS_SIZE)) |
647 |
|
{ |
648 |
|
error("Bad server public key size (%u bits)\n", g_server_public_key_len * 8); |
649 |
|
ssl_rkey_free(server_public_key); |
650 |
|
return False; |
651 |
|
} |
652 |
|
if (ssl_rkey_get_exp_mod(server_public_key, exponent, SEC_EXPONENT_SIZE, |
653 |
|
modulus, SEC_MAX_MODULUS_SIZE) != 0) |
654 |
|
{ |
655 |
|
error("Problem extracting RSA exponent, modulus"); |
656 |
|
ssl_rkey_free(server_public_key); |
657 |
|
return False; |
658 |
|
} |
659 |
|
ssl_rkey_free(server_public_key); |
660 |
return True; /* There's some garbage here we don't care about */ |
return True; /* There's some garbage here we don't care about */ |
661 |
} |
} |
662 |
return s_check_end(s); |
return s_check_end(s); |
666 |
static void |
static void |
667 |
sec_process_crypt_info(STREAM s) |
sec_process_crypt_info(STREAM s) |
668 |
{ |
{ |
669 |
uint8 *server_random, *modulus, *exponent; |
uint8 *server_random = NULL; |
670 |
uint8 client_random[SEC_RANDOM_SIZE]; |
uint8 client_random[SEC_RANDOM_SIZE]; |
671 |
|
uint8 modulus[SEC_MAX_MODULUS_SIZE]; |
672 |
|
uint8 exponent[SEC_EXPONENT_SIZE]; |
673 |
uint32 rc4_key_size; |
uint32 rc4_key_size; |
|
uint8 inr[SEC_MODULUS_SIZE]; |
|
674 |
|
|
675 |
if (!sec_parse_crypt_info(s, &rc4_key_size, &server_random, &modulus, &exponent)) |
memset(modulus, 0, sizeof(modulus)); |
676 |
|
memset(exponent, 0, sizeof(exponent)); |
677 |
|
if (!sec_parse_crypt_info(s, &rc4_key_size, &server_random, modulus, exponent)) |
678 |
{ |
{ |
679 |
DEBUG(("Failed to parse crypt info\n")); |
DEBUG(("Failed to parse crypt info\n")); |
680 |
return; |
return; |
681 |
} |
} |
|
|
|
682 |
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! |
|
|
*/ |
|
|
|
|
683 |
generate_random(client_random); |
generate_random(client_random); |
684 |
if (NULL != server_public_key) |
sec_rsa_encrypt(g_sec_crypted_random, client_random, SEC_RANDOM_SIZE, |
685 |
{ /* Which means we should use |
g_server_public_key_len, modulus, exponent); |
|
RDP5-style encryption */ |
|
|
|
|
|
memcpy(inr + SEC_RANDOM_SIZE, client_random, SEC_RANDOM_SIZE); |
|
|
reverse(inr + SEC_RANDOM_SIZE, SEC_RANDOM_SIZE); |
|
|
|
|
|
RSA_public_encrypt(SEC_MODULUS_SIZE, |
|
|
inr, sec_crypted_random, server_public_key, RSA_NO_PADDING); |
|
|
|
|
|
reverse(sec_crypted_random, SEC_MODULUS_SIZE); |
|
|
|
|
|
} |
|
|
else |
|
|
{ /* RDP4-style encryption */ |
|
|
sec_rsa_encrypt(sec_crypted_random, |
|
|
client_random, SEC_RANDOM_SIZE, modulus, exponent); |
|
|
} |
|
686 |
sec_generate_keys(client_random, server_random, rc4_key_size); |
sec_generate_keys(client_random, server_random, rc4_key_size); |
687 |
} |
} |
688 |
|
|
696 |
if (1 == g_server_rdp_version) |
if (1 == g_server_rdp_version) |
697 |
{ |
{ |
698 |
g_use_rdp5 = 0; |
g_use_rdp5 = 0; |
699 |
g_server_bpp = 8; |
g_server_depth = 8; |
700 |
} |
} |
701 |
} |
} |
702 |
|
|
750 |
|
|
751 |
/* Receive secure transport packet */ |
/* Receive secure transport packet */ |
752 |
STREAM |
STREAM |
753 |
sec_recv(void) |
sec_recv(uint8 * rdpver) |
754 |
{ |
{ |
755 |
uint32 sec_flags; |
uint32 sec_flags; |
756 |
uint16 channel; |
uint16 channel; |
757 |
STREAM s; |
STREAM s; |
758 |
|
|
759 |
while ((s = mcs_recv(&channel)) != NULL) |
while ((s = mcs_recv(&channel, rdpver)) != NULL) |
760 |
{ |
{ |
761 |
|
if (rdpver != NULL) |
762 |
|
{ |
763 |
|
if (*rdpver != 3) |
764 |
|
{ |
765 |
|
if (*rdpver & 0x80) |
766 |
|
{ |
767 |
|
in_uint8s(s, 8); /* signature */ |
768 |
|
sec_decrypt(s->p, s->end - s->p); |
769 |
|
} |
770 |
|
return s; |
771 |
|
} |
772 |
|
} |
773 |
if (g_encryption || !g_licence_issued) |
if (g_encryption || !g_licence_issued) |
774 |
{ |
{ |
775 |
in_uint32_le(s, sec_flags); |
in_uint32_le(s, sec_flags); |
785 |
licence_process(s); |
licence_process(s); |
786 |
continue; |
continue; |
787 |
} |
} |
788 |
|
|
789 |
|
if (sec_flags & 0x0400) /* SEC_REDIRECT_ENCRYPT */ |
790 |
|
{ |
791 |
|
uint8 swapbyte; |
792 |
|
|
793 |
|
in_uint8s(s, 8); /* signature */ |
794 |
|
sec_decrypt(s->p, s->end - s->p); |
795 |
|
|
796 |
|
/* Check for a redirect packet, starts with 00 04 */ |
797 |
|
if (s->p[0] == 0 && s->p[1] == 4) |
798 |
|
{ |
799 |
|
/* for some reason the PDU and the length seem to be swapped. |
800 |
|
This isn't good, but we're going to do a byte for byte |
801 |
|
swap. So the first foure value appear as: 00 04 XX YY, |
802 |
|
where XX YY is the little endian length. We're going to |
803 |
|
use 04 00 as the PDU type, so after our swap this will look |
804 |
|
like: XX YY 04 00 */ |
805 |
|
swapbyte = s->p[0]; |
806 |
|
s->p[0] = s->p[2]; |
807 |
|
s->p[2] = swapbyte; |
808 |
|
|
809 |
|
swapbyte = s->p[1]; |
810 |
|
s->p[1] = s->p[3]; |
811 |
|
s->p[3] = swapbyte; |
812 |
|
|
813 |
|
swapbyte = s->p[2]; |
814 |
|
s->p[2] = s->p[3]; |
815 |
|
s->p[3] = swapbyte; |
816 |
|
} |
817 |
|
#ifdef WITH_DEBUG |
818 |
|
/* warning! this debug statement will show passwords in the clear! */ |
819 |
|
hexdump(s->p, s->end - s->p); |
820 |
|
#endif |
821 |
|
} |
822 |
|
|
823 |
} |
} |
824 |
|
|
825 |
if (channel != MCS_GLOBAL_CHANNEL) |
if (channel != MCS_GLOBAL_CHANNEL) |
826 |
{ |
{ |
827 |
channel_process(s, channel); |
channel_process(s, channel); |
828 |
continue; |
*rdpver = 0xff; |
829 |
|
return s; |
830 |
} |
} |
831 |
|
|
832 |
return s; |
return s; |
836 |
} |
} |
837 |
|
|
838 |
/* Establish a secure connection */ |
/* Establish a secure connection */ |
839 |
BOOL |
RD_BOOL |
840 |
sec_connect(char *server, char *username) |
sec_connect(char *server, char *username) |
841 |
{ |
{ |
842 |
struct stream mcs_data; |
struct stream mcs_data; |
856 |
return True; |
return True; |
857 |
} |
} |
858 |
|
|
859 |
|
/* Establish a secure connection */ |
860 |
|
RD_BOOL |
861 |
|
sec_reconnect(char *server) |
862 |
|
{ |
863 |
|
struct stream mcs_data; |
864 |
|
|
865 |
|
/* We exchange some RDP data during the MCS-Connect */ |
866 |
|
mcs_data.size = 512; |
867 |
|
mcs_data.p = mcs_data.data = (uint8 *) xmalloc(mcs_data.size); |
868 |
|
sec_out_mcs_data(&mcs_data); |
869 |
|
|
870 |
|
if (!mcs_reconnect(server, &mcs_data)) |
871 |
|
return False; |
872 |
|
|
873 |
|
/* sec_process_mcs_data(&mcs_data); */ |
874 |
|
if (g_encryption) |
875 |
|
sec_establish_key(); |
876 |
|
xfree(mcs_data.data); |
877 |
|
return True; |
878 |
|
} |
879 |
|
|
880 |
/* Disconnect a connection */ |
/* Disconnect a connection */ |
881 |
void |
void |
882 |
sec_disconnect(void) |
sec_disconnect(void) |
883 |
{ |
{ |
884 |
mcs_disconnect(); |
mcs_disconnect(); |
885 |
} |
} |
886 |
|
|
887 |
|
/* reset the state of the sec layer */ |
888 |
|
void |
889 |
|
sec_reset_state(void) |
890 |
|
{ |
891 |
|
g_server_rdp_version = 0; |
892 |
|
g_sec_encrypt_use_count = 0; |
893 |
|
g_sec_decrypt_use_count = 0; |
894 |
|
mcs_reset_state(); |
895 |
|
} |