45 |
* 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. |
46 |
* Both SHA1 and MD5 algorithms are used. |
* Both SHA1 and MD5 algorithms are used. |
47 |
*/ |
*/ |
48 |
void sec_hash_48(uint8 *out, uint8 *in, uint8 *salt1, uint8 *salt2, uint8 salt) |
void |
49 |
|
sec_hash_48(uint8 *out, uint8 *in, uint8 *salt1, uint8 *salt2, uint8 salt) |
50 |
{ |
{ |
51 |
uint8 shasig[20]; |
uint8 shasig[20]; |
52 |
uint8 pad[4]; |
uint8 pad[4]; |
56 |
|
|
57 |
for (i = 0; i < 3; i++) |
for (i = 0; i < 3; i++) |
58 |
{ |
{ |
59 |
memset(pad, salt+i, i+1); |
memset(pad, salt + i, i + 1); |
60 |
|
|
61 |
SHA1_Init(&sha); |
SHA1_Init(&sha); |
62 |
SHA1_Update(&sha, pad, i+1); |
SHA1_Update(&sha, pad, i + 1); |
63 |
SHA1_Update(&sha, in, 48); |
SHA1_Update(&sha, in, 48); |
64 |
SHA1_Update(&sha, salt1, 32); |
SHA1_Update(&sha, salt1, 32); |
65 |
SHA1_Update(&sha, salt2, 32); |
SHA1_Update(&sha, salt2, 32); |
68 |
MD5_Init(&md5); |
MD5_Init(&md5); |
69 |
MD5_Update(&md5, in, 48); |
MD5_Update(&md5, in, 48); |
70 |
MD5_Update(&md5, shasig, 20); |
MD5_Update(&md5, shasig, 20); |
71 |
MD5_Final(&out[i*16], &md5); |
MD5_Final(&out[i * 16], &md5); |
72 |
} |
} |
73 |
} |
} |
74 |
|
|
76 |
* Weaker 16-byte transformation, also using two 32-byte salts, but |
* Weaker 16-byte transformation, also using two 32-byte salts, but |
77 |
* only using a single round of MD5. |
* only using a single round of MD5. |
78 |
*/ |
*/ |
79 |
void sec_hash_16(uint8 *out, uint8 *in, uint8 *salt1, uint8 *salt2) |
void |
80 |
|
sec_hash_16(uint8 *out, uint8 *in, uint8 *salt1, uint8 *salt2) |
81 |
{ |
{ |
82 |
MD5_CTX md5; |
MD5_CTX md5; |
83 |
|
|
89 |
} |
} |
90 |
|
|
91 |
/* Reduce key entropy from 64 to 40 bits */ |
/* Reduce key entropy from 64 to 40 bits */ |
92 |
static void sec_make_40bit(uint8 *key) |
static void |
93 |
|
sec_make_40bit(uint8 *key) |
94 |
{ |
{ |
95 |
key[0] = 0xd1; |
key[0] = 0xd1; |
96 |
key[1] = 0x26; |
key[1] = 0x26; |
98 |
} |
} |
99 |
|
|
100 |
/* Generate a session key and RC4 keys, given client and server randoms */ |
/* Generate a session key and RC4 keys, given client and server randoms */ |
101 |
static void sec_generate_keys(uint8 *client_key, uint8 *server_key, |
static void |
102 |
int rc4_key_size) |
sec_generate_keys(uint8 *client_key, uint8 *server_key, int rc4_key_size) |
103 |
{ |
{ |
104 |
uint8 session_key[48]; |
uint8 session_key[48]; |
105 |
uint8 temp_hash[48]; |
uint8 temp_hash[48]; |
106 |
uint8 input[48]; |
uint8 input[48]; |
107 |
|
|
108 |
/* Construct input data to hash */ |
/* Construct input data to hash */ |
109 |
memcpy(input, client_key, 24); |
memcpy(input, client_key, 24); |
110 |
memcpy(input+24, server_key, 24); |
memcpy(input + 24, server_key, 24); |
111 |
|
|
112 |
/* Generate session key - two rounds of sec_hash_48 */ |
/* Generate session key - two rounds of sec_hash_48 */ |
113 |
sec_hash_48(temp_hash, input, client_key, server_key, 65); |
sec_hash_48(temp_hash, input, client_key, server_key, 65); |
114 |
sec_hash_48(session_key, temp_hash, client_key, server_key, 88); |
sec_hash_48(session_key, temp_hash, client_key, server_key, 88); |
115 |
|
|
116 |
/* Store first 8 bytes of session key, for generating signatures */ |
/* Store first 8 bytes of session key, for generating signatures */ |
117 |
memcpy(sec_sign_key, session_key, 8); |
memcpy(sec_sign_key, session_key, 8); |
118 |
|
|
119 |
/* Generate RC4 keys */ |
/* Generate RC4 keys */ |
120 |
sec_hash_16(sec_decrypt_key, &session_key[16], client_key, server_key); |
sec_hash_16(sec_decrypt_key, &session_key[16], client_key, |
121 |
sec_hash_16(sec_encrypt_key, &session_key[32], client_key, server_key); |
server_key); |
122 |
|
sec_hash_16(sec_encrypt_key, &session_key[32], client_key, |
123 |
|
server_key); |
124 |
|
|
125 |
if (rc4_key_size == 1) |
if (rc4_key_size == 1) |
126 |
{ |
{ |
145 |
RC4_set_key(&rc4_encrypt_key, rc4_key_len, sec_encrypt_key); |
RC4_set_key(&rc4_encrypt_key, rc4_key_len, sec_encrypt_key); |
146 |
} |
} |
147 |
|
|
148 |
static uint8 pad_54[40] = |
static uint8 pad_54[40] = { |
149 |
{ |
54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, |
150 |
54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54, |
54, 54, 54, |
151 |
54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54 |
54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, |
152 |
|
54, 54, 54 |
153 |
}; |
}; |
154 |
|
|
155 |
static uint8 pad_92[48] = |
static uint8 pad_92[48] = { |
156 |
{ |
92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, |
157 |
92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92, |
92, 92, 92, 92, 92, 92, 92, |
158 |
92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92 |
92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, |
159 |
|
92, 92, 92, 92, 92, 92, 92 |
160 |
}; |
}; |
161 |
|
|
162 |
/* Output a uint32 into a buffer (little-endian) */ |
/* Output a uint32 into a buffer (little-endian) */ |
163 |
void buf_out_uint32(uint8 *buffer, uint32 value) |
void |
164 |
|
buf_out_uint32(uint8 *buffer, uint32 value) |
165 |
{ |
{ |
166 |
buffer[0] = (value) & 0xff; |
buffer[0] = (value) & 0xff; |
167 |
buffer[1] = (value >> 8) & 0xff; |
buffer[1] = (value >> 8) & 0xff; |
170 |
} |
} |
171 |
|
|
172 |
/* Generate a signature hash, using a combination of SHA1 and MD5 */ |
/* Generate a signature hash, using a combination of SHA1 and MD5 */ |
173 |
void sec_sign(uint8 *signature, uint8 *session_key, int length, |
void |
174 |
uint8 *data, int datalen) |
sec_sign(uint8 *signature, uint8 *session_key, int length, |
175 |
|
uint8 *data, int datalen) |
176 |
{ |
{ |
177 |
uint8 shasig[20]; |
uint8 shasig[20]; |
178 |
uint8 md5sig[16]; |
uint8 md5sig[16]; |
180 |
SHA_CTX sha; |
SHA_CTX sha; |
181 |
MD5_CTX md5; |
MD5_CTX md5; |
182 |
|
|
183 |
buf_out_uint32(lenhdr, datalen); |
buf_out_uint32(lenhdr, datalen); |
184 |
|
|
185 |
SHA1_Init(&sha); |
SHA1_Init(&sha); |
186 |
SHA1_Update(&sha, session_key, length); |
SHA1_Update(&sha, session_key, length); |
199 |
} |
} |
200 |
|
|
201 |
/* Update an encryption key - similar to the signing process */ |
/* Update an encryption key - similar to the signing process */ |
202 |
static void sec_update(uint8 *key, uint8 *update_key) |
static void |
203 |
|
sec_update(uint8 *key, uint8 *update_key) |
204 |
{ |
{ |
205 |
uint8 shasig[20]; |
uint8 shasig[20]; |
206 |
SHA_CTX sha; |
SHA_CTX sha; |
227 |
} |
} |
228 |
|
|
229 |
/* Encrypt data using RC4 */ |
/* Encrypt data using RC4 */ |
230 |
static void sec_encrypt(uint8 *data, int length) |
static void |
231 |
|
sec_encrypt(uint8 *data, int length) |
232 |
{ |
{ |
233 |
static int use_count; |
static int use_count; |
234 |
|
|
244 |
} |
} |
245 |
|
|
246 |
/* Decrypt data using RC4 */ |
/* Decrypt data using RC4 */ |
247 |
static void sec_decrypt(uint8 *data, int length) |
static void |
248 |
|
sec_decrypt(uint8 *data, int length) |
249 |
{ |
{ |
250 |
static int use_count; |
static int use_count; |
251 |
|
|
261 |
} |
} |
262 |
|
|
263 |
/* Read in a NUMBER from a buffer */ |
/* Read in a NUMBER from a buffer */ |
264 |
static void sec_read_number(NUMBER *num, uint8 *buffer, int len) |
static void |
265 |
|
sec_read_number(NUMBER * num, uint8 *buffer, int len) |
266 |
{ |
{ |
267 |
INT *data = num->n_part; |
INT *data = num->n_part; |
268 |
int i, j; |
int i, j; |
269 |
|
|
270 |
for (i = 0, j = 0; j < len; i++, j += 2) |
for (i = 0, j = 0; j < len; i++, j += 2) |
271 |
data[i] = buffer[j] | (buffer[j+1] << 8); |
data[i] = buffer[j] | (buffer[j + 1] << 8); |
272 |
|
|
273 |
num->n_len = i; |
num->n_len = i; |
274 |
} |
} |
275 |
|
|
276 |
/* Write a NUMBER to a buffer */ |
/* Write a NUMBER to a buffer */ |
277 |
static void sec_write_number(NUMBER *num, uint8 *buffer, int len) |
static void |
278 |
|
sec_write_number(NUMBER * num, uint8 *buffer, int len) |
279 |
{ |
{ |
280 |
INT *data = num->n_part; |
INT *data = num->n_part; |
281 |
int i, j; |
int i, j; |
283 |
for (i = 0, j = 0; j < len; i++, j += 2) |
for (i = 0, j = 0; j < len; i++, j += 2) |
284 |
{ |
{ |
285 |
buffer[j] = data[i] & 0xff; |
buffer[j] = data[i] & 0xff; |
286 |
buffer[j+1] = data[i] >> 8; |
buffer[j + 1] = data[i] >> 8; |
287 |
} |
} |
288 |
} |
} |
289 |
|
|
290 |
/* Perform an RSA public key encryption operation */ |
/* Perform an RSA public key encryption operation */ |
291 |
static void sec_rsa_encrypt(uint8 *out, uint8 *in, int len, |
static void |
292 |
uint8 *modulus, uint8 *exponent) |
sec_rsa_encrypt(uint8 *out, uint8 *in, int len, |
293 |
|
uint8 *modulus, uint8 *exponent) |
294 |
{ |
{ |
295 |
NUMBER data, key; |
NUMBER data, key; |
296 |
|
|
306 |
} |
} |
307 |
|
|
308 |
/* Initialise secure transport packet */ |
/* Initialise secure transport packet */ |
309 |
STREAM sec_init(uint32 flags, int maxlen) |
STREAM |
310 |
|
sec_init(uint32 flags, int maxlen) |
311 |
{ |
{ |
312 |
int hdrlen; |
int hdrlen; |
313 |
STREAM s; |
STREAM s; |
314 |
|
|
315 |
hdrlen = (flags & SEC_ENCRYPT) ? 12 : 4; |
hdrlen = (flags & SEC_ENCRYPT) ? 12 : 4; |
316 |
s = mcs_init(maxlen + hdrlen); |
s = mcs_init(maxlen + hdrlen); |
317 |
s_push_layer(s, sec_hdr, hdrlen); |
s_push_layer(s, sec_hdr, hdrlen); |
318 |
|
|
319 |
return s; |
return s; |
320 |
} |
} |
321 |
|
|
322 |
/* Transmit secure transport packet */ |
/* Transmit secure transport packet */ |
323 |
void sec_send(STREAM s, uint32 flags) |
void |
324 |
|
sec_send(STREAM s, uint32 flags) |
325 |
{ |
{ |
326 |
int datalen; |
int datalen; |
327 |
|
|
335 |
|
|
336 |
#if RDP_DEBUG |
#if RDP_DEBUG |
337 |
DEBUG("Sending encrypted packet:\n"); |
DEBUG("Sending encrypted packet:\n"); |
338 |
hexdump(s->p+8, datalen); |
hexdump(s->p + 8, datalen); |
339 |
#endif |
#endif |
340 |
|
|
341 |
sec_sign(s->p, sec_sign_key, 8, s->p+8, datalen); |
sec_sign(s->p, sec_sign_key, 8, s->p + 8, datalen); |
342 |
sec_encrypt(s->p+8, datalen); |
sec_encrypt(s->p + 8, datalen); |
343 |
} |
} |
344 |
|
|
345 |
mcs_send(s); |
mcs_send(s); |
346 |
} |
} |
347 |
|
|
348 |
/* Transfer the client random to the server */ |
/* Transfer the client random to the server */ |
349 |
static void sec_establish_key() |
static void |
350 |
|
sec_establish_key() |
351 |
{ |
{ |
352 |
uint32 length = SEC_MODULUS_SIZE + SEC_PADDING_SIZE; |
uint32 length = SEC_MODULUS_SIZE + SEC_PADDING_SIZE; |
353 |
uint32 flags = SEC_CLIENT_RANDOM; |
uint32 flags = SEC_CLIENT_RANDOM; |
364 |
} |
} |
365 |
|
|
366 |
/* Output connect initial data blob */ |
/* Output connect initial data blob */ |
367 |
static void sec_out_mcs_data(STREAM s) |
static void |
368 |
|
sec_out_mcs_data(STREAM s) |
369 |
{ |
{ |
370 |
int hostlen = 2 * strlen(hostname); |
int hostlen = 2 * strlen(hostname); |
371 |
|
|
374 |
out_uint8(s, 0x7c); |
out_uint8(s, 0x7c); |
375 |
out_uint16_be(s, 1); |
out_uint16_be(s, 1); |
376 |
|
|
377 |
out_uint16_be(s, (158 | 0x8000)); /* remaining length */ |
out_uint16_be(s, (158 | 0x8000)); /* remaining length */ |
378 |
|
|
379 |
out_uint16_be(s, 8); /* length? */ |
out_uint16_be(s, 8); /* length? */ |
380 |
out_uint16_be(s, 16); |
out_uint16_be(s, 16); |
382 |
out_uint16_le(s, 0xc001); |
out_uint16_le(s, 0xc001); |
383 |
out_uint8(s, 0); |
out_uint8(s, 0); |
384 |
|
|
385 |
out_uint32_le(s, 0x61637544); /* "Duca" ?! */ |
out_uint32_le(s, 0x61637544); /* "Duca" ?! */ |
386 |
out_uint16_be(s, (144 | 0x8000)); /* remaining length */ |
out_uint16_be(s, (144 | 0x8000)); /* remaining length */ |
387 |
|
|
388 |
/* Client information */ |
/* Client information */ |
389 |
out_uint16_le(s, SEC_TAG_CLI_INFO); |
out_uint16_le(s, SEC_TAG_CLI_INFO); |
395 |
out_uint16_le(s, 0xca01); |
out_uint16_le(s, 0xca01); |
396 |
out_uint16_le(s, 0xaa03); |
out_uint16_le(s, 0xaa03); |
397 |
out_uint32_le(s, keylayout); |
out_uint32_le(s, keylayout); |
398 |
out_uint32_le(s, 419); /* client build? we are 419 compatible :-) */ |
out_uint32_le(s, 419); /* client build? we are 419 compatible :-) */ |
399 |
|
|
400 |
/* Unicode name of client, padded to 32 bytes */ |
/* Unicode name of client, padded to 32 bytes */ |
401 |
rdp_out_unistr(s, hostname, hostlen); |
rdp_out_unistr(s, hostname, hostlen); |
402 |
out_uint8s(s, 30-hostlen); |
out_uint8s(s, 30 - hostlen); |
403 |
|
|
404 |
out_uint32_le(s, 4); |
out_uint32_le(s, 4); |
405 |
out_uint32(s, 0); |
out_uint32(s, 0); |
406 |
out_uint32_le(s, 12); |
out_uint32_le(s, 12); |
407 |
out_uint8s(s, 64); /* reserved? 4 + 12 doublewords */ |
out_uint8s(s, 64); /* reserved? 4 + 12 doublewords */ |
408 |
|
|
409 |
out_uint16(s, 0xca01); |
out_uint16(s, 0xca01); |
410 |
out_uint16(s, 0); |
out_uint16(s, 0); |
417 |
} |
} |
418 |
|
|
419 |
/* Parse a public key structure */ |
/* Parse a public key structure */ |
420 |
static BOOL sec_parse_public_key(STREAM s, uint8 **modulus, uint8 **exponent) |
static BOOL |
421 |
|
sec_parse_public_key(STREAM s, uint8 **modulus, uint8 **exponent) |
422 |
{ |
{ |
423 |
uint32 magic, modulus_len; |
uint32 magic, modulus_len; |
424 |
|
|
436 |
return False; |
return False; |
437 |
} |
} |
438 |
|
|
439 |
in_uint8s(s, 8); /* modulus_bits, unknown */ |
in_uint8s(s, 8); /* modulus_bits, unknown */ |
440 |
in_uint8p(s, *exponent, SEC_EXPONENT_SIZE); |
in_uint8p(s, *exponent, SEC_EXPONENT_SIZE); |
441 |
in_uint8p(s, *modulus, SEC_MODULUS_SIZE); |
in_uint8p(s, *modulus, SEC_MODULUS_SIZE); |
442 |
in_uint8s(s, SEC_PADDING_SIZE); |
in_uint8s(s, SEC_PADDING_SIZE); |
445 |
} |
} |
446 |
|
|
447 |
/* Parse a crypto information structure */ |
/* Parse a crypto information structure */ |
448 |
static BOOL sec_parse_crypt_info(STREAM s, uint32 *rc4_key_size, |
static BOOL |
449 |
uint8 **server_random, uint8 **modulus, uint8 **exponent) |
sec_parse_crypt_info(STREAM s, uint32 *rc4_key_size, |
450 |
|
uint8 **server_random, uint8 **modulus, uint8 **exponent) |
451 |
{ |
{ |
452 |
uint32 crypt_level, random_len, rsa_info_len; |
uint32 crypt_level, random_len, rsa_info_len; |
453 |
uint16 tag, length; |
uint16 tag, length; |
454 |
uint8 *next_tag, *end; |
uint8 *next_tag, *end; |
455 |
|
|
456 |
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 */ |
457 |
in_uint32_le(s, crypt_level); /* 1 = low, 2 = medium, 3 = high */ |
in_uint32_le(s, crypt_level); /* 1 = low, 2 = medium, 3 = high */ |
458 |
in_uint32_le(s, random_len); |
in_uint32_le(s, random_len); |
459 |
in_uint32_le(s, rsa_info_len); |
in_uint32_le(s, rsa_info_len); |
460 |
|
|
471 |
if (end > s->end) |
if (end > s->end) |
472 |
return False; |
return False; |
473 |
|
|
474 |
in_uint8s(s, 12); /* unknown */ |
in_uint8s(s, 12); /* unknown */ |
475 |
|
|
476 |
while (s->p < end) |
while (s->p < end) |
477 |
{ |
{ |
483 |
switch (tag) |
switch (tag) |
484 |
{ |
{ |
485 |
case SEC_TAG_PUBKEY: |
case SEC_TAG_PUBKEY: |
486 |
if (!sec_parse_public_key(s, modulus, exponent)) |
if (!sec_parse_public_key |
487 |
|
(s, modulus, exponent)) |
488 |
return False; |
return False; |
489 |
|
|
490 |
break; |
break; |
505 |
} |
} |
506 |
|
|
507 |
/* Process crypto information blob */ |
/* Process crypto information blob */ |
508 |
static void sec_process_crypt_info(STREAM s) |
static void |
509 |
|
sec_process_crypt_info(STREAM s) |
510 |
{ |
{ |
511 |
uint8 *server_random, *modulus, *exponent; |
uint8 *server_random, *modulus, *exponent; |
512 |
uint8 client_random[SEC_RANDOM_SIZE]; |
uint8 client_random[SEC_RANDOM_SIZE]; |
513 |
uint32 rc4_key_size; |
uint32 rc4_key_size; |
514 |
|
|
515 |
if (!sec_parse_crypt_info(s, &rc4_key_size, &server_random, |
if (!sec_parse_crypt_info(s, &rc4_key_size, &server_random, |
516 |
&modulus, &exponent)) |
&modulus, &exponent)) |
517 |
return; |
return; |
518 |
|
|
519 |
/* Generate a client random, and hence determine encryption keys */ |
/* Generate a client random, and hence determine encryption keys */ |
520 |
generate_random(client_random); |
generate_random(client_random); |
521 |
sec_rsa_encrypt(sec_crypted_random, client_random, |
sec_rsa_encrypt(sec_crypted_random, client_random, |
522 |
SEC_RANDOM_SIZE, modulus, exponent); |
SEC_RANDOM_SIZE, modulus, exponent); |
523 |
sec_generate_keys(client_random, server_random, rc4_key_size); |
sec_generate_keys(client_random, server_random, rc4_key_size); |
524 |
} |
} |
525 |
|
|
526 |
/* Process connect response data blob */ |
/* Process connect response data blob */ |
527 |
static void sec_process_mcs_data(STREAM s) |
static void |
528 |
|
sec_process_mcs_data(STREAM s) |
529 |
{ |
{ |
530 |
uint16 tag, length; |
uint16 tag, length; |
531 |
uint8 *next_tag; |
uint8 *next_tag; |
532 |
|
|
533 |
in_uint8s(s, 23); /* header */ |
in_uint8s(s, 23); /* header */ |
534 |
|
|
535 |
while (s->p < s->end) |
while (s->p < s->end) |
536 |
{ |
{ |
561 |
} |
} |
562 |
|
|
563 |
/* Receive secure transport packet */ |
/* Receive secure transport packet */ |
564 |
STREAM sec_recv() |
STREAM |
565 |
|
sec_recv() |
566 |
{ |
{ |
567 |
uint32 sec_flags; |
uint32 sec_flags; |
568 |
STREAM s; |
STREAM s; |
579 |
|
|
580 |
if (sec_flags & SEC_ENCRYPT) |
if (sec_flags & SEC_ENCRYPT) |
581 |
{ |
{ |
582 |
in_uint8s(s, 8); /* signature */ |
in_uint8s(s, 8); /* signature */ |
583 |
sec_decrypt(s->p, s->end - s->p); |
sec_decrypt(s->p, s->end - s->p); |
584 |
} |
} |
585 |
|
|
590 |
} |
} |
591 |
|
|
592 |
/* Establish a secure connection */ |
/* Establish a secure connection */ |
593 |
BOOL sec_connect(char *server) |
BOOL |
594 |
|
sec_connect(char *server) |
595 |
{ |
{ |
596 |
struct stream mcs_data; |
struct stream mcs_data; |
597 |
|
|
609 |
} |
} |
610 |
|
|
611 |
/* Disconnect a connection */ |
/* Disconnect a connection */ |
612 |
void sec_disconnect() |
void |
613 |
|
sec_disconnect() |
614 |
{ |
{ |
615 |
mcs_disconnect(); |
mcs_disconnect(); |
616 |
} |
} |