19 |
*/ |
*/ |
20 |
|
|
21 |
#include "rdesktop.h" |
#include "rdesktop.h" |
22 |
|
|
23 |
|
#ifdef WITH_OPENSSL |
24 |
|
#include <openssl/rc4.h> |
25 |
|
#include <openssl/md5.h> |
26 |
|
#include <openssl/sha.h> |
27 |
|
#include <openssl/bn.h> |
28 |
|
#else |
29 |
#include "crypto/rc4.h" |
#include "crypto/rc4.h" |
30 |
#include "crypto/md5.h" |
#include "crypto/md5.h" |
31 |
#include "crypto/sha.h" |
#include "crypto/sha.h" |
32 |
#include "crypto/arith.h" |
#include "crypto/bn.h" |
33 |
|
#endif |
34 |
|
|
35 |
extern char hostname[16]; |
extern char hostname[16]; |
36 |
extern int width; |
extern int width; |
43 |
static RC4_KEY rc4_decrypt_key; |
static RC4_KEY rc4_decrypt_key; |
44 |
static RC4_KEY rc4_encrypt_key; |
static RC4_KEY rc4_encrypt_key; |
45 |
|
|
46 |
static uint8 sec_sign_key[8]; |
static uint8 sec_sign_key[16]; |
47 |
static uint8 sec_decrypt_key[16]; |
static uint8 sec_decrypt_key[16]; |
48 |
static uint8 sec_encrypt_key[16]; |
static uint8 sec_encrypt_key[16]; |
49 |
static uint8 sec_decrypt_update_key[8]; |
static uint8 sec_decrypt_update_key[16]; |
50 |
static uint8 sec_encrypt_update_key[8]; |
static uint8 sec_encrypt_update_key[16]; |
51 |
static uint8 sec_crypted_random[64]; |
static uint8 sec_crypted_random[SEC_MODULUS_SIZE]; |
52 |
|
|
53 |
/* |
/* |
54 |
* General purpose 48-byte transformation, using two 32-byte salts (generally, |
* General purpose 48-byte transformation, using two 32-byte salts (generally, |
123 |
sec_hash_48(temp_hash, input, client_key, server_key, 65); |
sec_hash_48(temp_hash, input, client_key, server_key, 65); |
124 |
sec_hash_48(session_key, temp_hash, client_key, server_key, 88); |
sec_hash_48(session_key, temp_hash, client_key, server_key, 88); |
125 |
|
|
126 |
/* Store first 8 bytes of session key, for generating signatures */ |
/* Store first 16 bytes of session key, for generating signatures */ |
127 |
memcpy(sec_sign_key, session_key, 8); |
memcpy(sec_sign_key, session_key, 16); |
128 |
|
|
129 |
/* Generate RC4 keys */ |
/* Generate RC4 keys */ |
130 |
sec_hash_16(sec_decrypt_key, &session_key[16], client_key, |
sec_hash_16(sec_decrypt_key, &session_key[16], client_key, |
146 |
rc4_key_len = 16; |
rc4_key_len = 16; |
147 |
} |
} |
148 |
|
|
149 |
/* Store first 8 bytes of RC4 keys as update keys */ |
/* Save initial RC4 keys as update keys */ |
150 |
memcpy(sec_decrypt_update_key, sec_decrypt_key, 8); |
memcpy(sec_decrypt_update_key, sec_decrypt_key, 16); |
151 |
memcpy(sec_encrypt_update_key, sec_encrypt_key, 8); |
memcpy(sec_encrypt_update_key, sec_encrypt_key, 16); |
152 |
|
|
153 |
/* Initialise RC4 state arrays */ |
/* Initialise RC4 state arrays */ |
154 |
RC4_set_key(&rc4_decrypt_key, rc4_key_len, sec_decrypt_key); |
RC4_set_key(&rc4_decrypt_key, rc4_key_len, sec_decrypt_key); |
181 |
|
|
182 |
/* Generate a signature hash, using a combination of SHA1 and MD5 */ |
/* Generate a signature hash, using a combination of SHA1 and MD5 */ |
183 |
void |
void |
184 |
sec_sign(uint8 *signature, uint8 *session_key, int length, |
sec_sign(uint8 *signature, int siglen, uint8 *session_key, int keylen, |
185 |
uint8 *data, int datalen) |
uint8 *data, int datalen) |
186 |
{ |
{ |
187 |
uint8 shasig[20]; |
uint8 shasig[20]; |
193 |
buf_out_uint32(lenhdr, datalen); |
buf_out_uint32(lenhdr, datalen); |
194 |
|
|
195 |
SHA1_Init(&sha); |
SHA1_Init(&sha); |
196 |
SHA1_Update(&sha, session_key, length); |
SHA1_Update(&sha, session_key, keylen); |
197 |
SHA1_Update(&sha, pad_54, 40); |
SHA1_Update(&sha, pad_54, 40); |
198 |
SHA1_Update(&sha, lenhdr, 4); |
SHA1_Update(&sha, lenhdr, 4); |
199 |
SHA1_Update(&sha, data, datalen); |
SHA1_Update(&sha, data, datalen); |
200 |
SHA1_Final(shasig, &sha); |
SHA1_Final(shasig, &sha); |
201 |
|
|
202 |
MD5_Init(&md5); |
MD5_Init(&md5); |
203 |
MD5_Update(&md5, session_key, length); |
MD5_Update(&md5, session_key, keylen); |
204 |
MD5_Update(&md5, pad_92, 48); |
MD5_Update(&md5, pad_92, 48); |
205 |
MD5_Update(&md5, shasig, 20); |
MD5_Update(&md5, shasig, 20); |
206 |
MD5_Final(md5sig, &md5); |
MD5_Final(md5sig, &md5); |
207 |
|
|
208 |
memcpy(signature, md5sig, length); |
memcpy(signature, md5sig, siglen); |
209 |
} |
} |
210 |
|
|
211 |
/* Update an encryption key - similar to the signing process */ |
/* Update an encryption key - similar to the signing process */ |
218 |
RC4_KEY update; |
RC4_KEY update; |
219 |
|
|
220 |
SHA1_Init(&sha); |
SHA1_Init(&sha); |
221 |
SHA1_Update(&sha, update_key, 8); |
SHA1_Update(&sha, update_key, rc4_key_len); |
222 |
SHA1_Update(&sha, pad_54, 40); |
SHA1_Update(&sha, pad_54, 40); |
223 |
SHA1_Update(&sha, key, 8); |
SHA1_Update(&sha, key, rc4_key_len); |
224 |
SHA1_Final(shasig, &sha); |
SHA1_Final(shasig, &sha); |
225 |
|
|
226 |
MD5_Init(&md5); |
MD5_Init(&md5); |
227 |
MD5_Update(&md5, update_key, 8); |
MD5_Update(&md5, update_key, rc4_key_len); |
228 |
MD5_Update(&md5, pad_92, 48); |
MD5_Update(&md5, pad_92, 48); |
229 |
MD5_Update(&md5, shasig, 20); |
MD5_Update(&md5, shasig, 20); |
230 |
MD5_Final(key, &md5); |
MD5_Final(key, &md5); |
270 |
use_count++; |
use_count++; |
271 |
} |
} |
272 |
|
|
|
/* Read in a NUMBER from a buffer */ |
|
|
static void |
|
|
sec_read_number(NUMBER * num, uint8 *buffer, int len) |
|
|
{ |
|
|
INT *data = num->n_part; |
|
|
int i, j; |
|
|
|
|
|
for (i = 0, j = 0; j < len; i++, j += 2) |
|
|
data[i] = buffer[j] | (buffer[j + 1] << 8); |
|
|
|
|
|
num->n_len = i; |
|
|
} |
|
|
|
|
|
/* Write a NUMBER to a buffer */ |
|
273 |
static void |
static void |
274 |
sec_write_number(NUMBER * num, uint8 *buffer, int len) |
reverse(uint8 *p, int len) |
275 |
{ |
{ |
|
INT *data = num->n_part; |
|
276 |
int i, j; |
int i, j; |
277 |
|
uint8 temp; |
278 |
|
|
279 |
for (i = 0, j = 0; j < len; i++, j += 2) |
for (i = 0, j = len-1; i < j; i++, j--) |
280 |
{ |
{ |
281 |
buffer[j] = data[i] & 0xff; |
temp = p[i]; |
282 |
buffer[j + 1] = data[i] >> 8; |
p[i] = p[j]; |
283 |
|
p[j] = temp; |
284 |
} |
} |
285 |
} |
} |
286 |
|
|
289 |
sec_rsa_encrypt(uint8 *out, uint8 *in, int len, |
sec_rsa_encrypt(uint8 *out, uint8 *in, int len, |
290 |
uint8 *modulus, uint8 *exponent) |
uint8 *modulus, uint8 *exponent) |
291 |
{ |
{ |
292 |
NUMBER data, key; |
BN_CTX ctx; |
293 |
|
BIGNUM mod, exp, x, y; |
294 |
/* Set modulus for arithmetic */ |
uint8 inr[SEC_MODULUS_SIZE]; |
295 |
sec_read_number(&key, modulus, SEC_MODULUS_SIZE); |
int outlen; |
296 |
m_init(&key, NULL); |
|
297 |
|
reverse(modulus, SEC_MODULUS_SIZE); |
298 |
/* Exponentiate */ |
reverse(exponent, SEC_EXPONENT_SIZE); |
299 |
sec_read_number(&data, in, len); |
memcpy(inr, in, len); |
300 |
sec_read_number(&key, exponent, SEC_EXPONENT_SIZE); |
reverse(inr, len); |
301 |
m_exp(&data, &key, &data); |
|
302 |
sec_write_number(&data, out, SEC_MODULUS_SIZE); |
BN_CTX_init(&ctx); |
303 |
|
BN_init(&mod); |
304 |
|
BN_init(&exp); |
305 |
|
BN_init(&x); |
306 |
|
BN_init(&y); |
307 |
|
|
308 |
|
BN_bin2bn(modulus, SEC_MODULUS_SIZE, &mod); |
309 |
|
BN_bin2bn(exponent, SEC_EXPONENT_SIZE, &exp); |
310 |
|
BN_bin2bn(inr, len, &x); |
311 |
|
BN_mod_exp(&y, &x, &exp, &mod, &ctx); |
312 |
|
outlen = BN_bn2bin(&y, out); |
313 |
|
reverse(out, outlen); |
314 |
|
if (outlen < SEC_MODULUS_SIZE) |
315 |
|
memset(out+outlen, 0, SEC_MODULUS_SIZE-outlen); |
316 |
|
|
317 |
|
BN_free(&y); |
318 |
|
BN_clear_free(&x); |
319 |
|
BN_free(&exp); |
320 |
|
BN_free(&mod); |
321 |
|
BN_CTX_free(&ctx); |
322 |
} |
} |
323 |
|
|
324 |
/* Initialise secure transport packet */ |
/* Initialise secure transport packet */ |
358 |
hexdump(s->p + 8, datalen); |
hexdump(s->p + 8, datalen); |
359 |
#endif |
#endif |
360 |
|
|
361 |
sec_sign(s->p, sec_sign_key, 8, s->p + 8, datalen); |
sec_sign(s->p, 8, sec_sign_key, rc4_key_len, s->p + 8, datalen); |
362 |
sec_encrypt(s->p + 8, datalen); |
sec_encrypt(s->p + 8, datalen); |
363 |
} |
} |
364 |
|
|
432 |
/* Client encryption settings */ |
/* Client encryption settings */ |
433 |
out_uint16_le(s, SEC_TAG_CLI_CRYPT); |
out_uint16_le(s, SEC_TAG_CLI_CRYPT); |
434 |
out_uint16(s, 8); /* length */ |
out_uint16(s, 8); /* length */ |
435 |
out_uint32_le(s, encryption ? 1 : 0); /* encryption enabled */ |
out_uint32_le(s, encryption ? 0x3 : 0); /* encryption supported, 128-bit supported */ |
436 |
s_mark_end(s); |
s_mark_end(s); |
437 |
} |
} |
438 |
|
|