56 |
* Both SHA1 and MD5 algorithms are used. |
* Both SHA1 and MD5 algorithms are used. |
57 |
*/ |
*/ |
58 |
void |
void |
59 |
sec_hash_48(uint8 *out, uint8 *in, uint8 *salt1, uint8 *salt2, uint8 salt) |
sec_hash_48(uint8 * out, uint8 * in, uint8 * salt1, uint8 * salt2, uint8 salt) |
60 |
{ |
{ |
61 |
uint8 shasig[20]; |
uint8 shasig[20]; |
62 |
uint8 pad[4]; |
uint8 pad[4]; |
87 |
* only using a single round of MD5. |
* only using a single round of MD5. |
88 |
*/ |
*/ |
89 |
void |
void |
90 |
sec_hash_16(uint8 *out, uint8 *in, uint8 *salt1, uint8 *salt2) |
sec_hash_16(uint8 * out, uint8 * in, uint8 * salt1, uint8 * salt2) |
91 |
{ |
{ |
92 |
MD5_CTX md5; |
MD5_CTX md5; |
93 |
|
|
100 |
|
|
101 |
/* Reduce key entropy from 64 to 40 bits */ |
/* Reduce key entropy from 64 to 40 bits */ |
102 |
static void |
static void |
103 |
sec_make_40bit(uint8 *key) |
sec_make_40bit(uint8 * key) |
104 |
{ |
{ |
105 |
key[0] = 0xd1; |
key[0] = 0xd1; |
106 |
key[1] = 0x26; |
key[1] = 0x26; |
109 |
|
|
110 |
/* Generate a session key and RC4 keys, given client and server randoms */ |
/* Generate a session key and RC4 keys, given client and server randoms */ |
111 |
static void |
static void |
112 |
sec_generate_keys(uint8 *client_key, uint8 *server_key, int rc4_key_size) |
sec_generate_keys(uint8 * client_key, uint8 * server_key, int rc4_key_size) |
113 |
{ |
{ |
114 |
uint8 session_key[48]; |
uint8 session_key[48]; |
115 |
uint8 temp_hash[48]; |
uint8 temp_hash[48]; |
127 |
memcpy(sec_sign_key, session_key, 16); |
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, server_key); |
131 |
server_key); |
sec_hash_16(sec_encrypt_key, &session_key[32], client_key, server_key); |
|
sec_hash_16(sec_encrypt_key, &session_key[32], client_key, |
|
|
server_key); |
|
132 |
|
|
133 |
if (rc4_key_size == 1) |
if (rc4_key_size == 1) |
134 |
{ |
{ |
169 |
|
|
170 |
/* Output a uint32 into a buffer (little-endian) */ |
/* Output a uint32 into a buffer (little-endian) */ |
171 |
void |
void |
172 |
buf_out_uint32(uint8 *buffer, uint32 value) |
buf_out_uint32(uint8 * buffer, uint32 value) |
173 |
{ |
{ |
174 |
buffer[0] = (value) & 0xff; |
buffer[0] = (value) & 0xff; |
175 |
buffer[1] = (value >> 8) & 0xff; |
buffer[1] = (value >> 8) & 0xff; |
179 |
|
|
180 |
/* Generate a signature hash, using a combination of SHA1 and MD5 */ |
/* Generate a signature hash, using a combination of SHA1 and MD5 */ |
181 |
void |
void |
182 |
sec_sign(uint8 *signature, int siglen, uint8 *session_key, int keylen, |
sec_sign(uint8 * signature, int siglen, uint8 * session_key, int keylen, uint8 * data, int datalen) |
|
uint8 *data, int datalen) |
|
183 |
{ |
{ |
184 |
uint8 shasig[20]; |
uint8 shasig[20]; |
185 |
uint8 md5sig[16]; |
uint8 md5sig[16]; |
207 |
|
|
208 |
/* Update an encryption key - similar to the signing process */ |
/* Update an encryption key - similar to the signing process */ |
209 |
static void |
static void |
210 |
sec_update(uint8 *key, uint8 *update_key) |
sec_update(uint8 * key, uint8 * update_key) |
211 |
{ |
{ |
212 |
uint8 shasig[20]; |
uint8 shasig[20]; |
213 |
SHA_CTX sha; |
SHA_CTX sha; |
235 |
|
|
236 |
/* Encrypt data using RC4 */ |
/* Encrypt data using RC4 */ |
237 |
static void |
static void |
238 |
sec_encrypt(uint8 *data, int length) |
sec_encrypt(uint8 * data, int length) |
239 |
{ |
{ |
240 |
static int use_count; |
static int use_count; |
241 |
|
|
252 |
|
|
253 |
/* Decrypt data using RC4 */ |
/* Decrypt data using RC4 */ |
254 |
static void |
static void |
255 |
sec_decrypt(uint8 *data, int length) |
sec_decrypt(uint8 * data, int length) |
256 |
{ |
{ |
257 |
static int use_count; |
static int use_count; |
258 |
|
|
268 |
} |
} |
269 |
|
|
270 |
static void |
static void |
271 |
reverse(uint8 *p, int len) |
reverse(uint8 * p, int len) |
272 |
{ |
{ |
273 |
int i, j; |
int i, j; |
274 |
uint8 temp; |
uint8 temp; |
275 |
|
|
276 |
for (i = 0, j = len-1; i < j; i++, j--) |
for (i = 0, j = len - 1; i < j; i++, j--) |
277 |
{ |
{ |
278 |
temp = p[i]; |
temp = p[i]; |
279 |
p[i] = p[j]; |
p[i] = p[j]; |
283 |
|
|
284 |
/* Perform an RSA public key encryption operation */ |
/* Perform an RSA public key encryption operation */ |
285 |
static void |
static void |
286 |
sec_rsa_encrypt(uint8 *out, uint8 *in, int len, |
sec_rsa_encrypt(uint8 * out, uint8 * in, int len, uint8 * modulus, uint8 * exponent) |
|
uint8 *modulus, uint8 *exponent) |
|
287 |
{ |
{ |
288 |
BN_CTX ctx; |
BN_CTX ctx; |
289 |
BIGNUM mod, exp, x, y; |
BIGNUM mod, exp, x, y; |
308 |
outlen = BN_bn2bin(&y, out); |
outlen = BN_bn2bin(&y, out); |
309 |
reverse(out, outlen); |
reverse(out, outlen); |
310 |
if (outlen < SEC_MODULUS_SIZE) |
if (outlen < SEC_MODULUS_SIZE) |
311 |
memset(out+outlen, 0, SEC_MODULUS_SIZE-outlen); |
memset(out + outlen, 0, SEC_MODULUS_SIZE - outlen); |
312 |
|
|
313 |
BN_free(&y); |
BN_free(&y); |
314 |
BN_clear_free(&x); |
BN_clear_free(&x); |
434 |
|
|
435 |
/* Parse a public key structure */ |
/* Parse a public key structure */ |
436 |
static BOOL |
static BOOL |
437 |
sec_parse_public_key(STREAM s, uint8 **modulus, uint8 **exponent) |
sec_parse_public_key(STREAM s, uint8 ** modulus, uint8 ** exponent) |
438 |
{ |
{ |
439 |
uint32 magic, modulus_len; |
uint32 magic, modulus_len; |
440 |
|
|
462 |
|
|
463 |
/* Parse a crypto information structure */ |
/* Parse a crypto information structure */ |
464 |
static BOOL |
static BOOL |
465 |
sec_parse_crypt_info(STREAM s, uint32 *rc4_key_size, |
sec_parse_crypt_info(STREAM s, uint32 * rc4_key_size, |
466 |
uint8 **server_random, uint8 **modulus, uint8 **exponent) |
uint8 ** server_random, uint8 ** modulus, uint8 ** exponent) |
467 |
{ |
{ |
468 |
uint32 crypt_level, random_len, rsa_info_len; |
uint32 crypt_level, random_len, rsa_info_len; |
469 |
uint16 tag, length; |
uint16 tag, length; |
499 |
switch (tag) |
switch (tag) |
500 |
{ |
{ |
501 |
case SEC_TAG_PUBKEY: |
case SEC_TAG_PUBKEY: |
502 |
if (!sec_parse_public_key |
if (!sec_parse_public_key(s, modulus, exponent)) |
|
(s, modulus, exponent)) |
|
503 |
return False; |
return False; |
504 |
|
|
505 |
break; |
break; |
527 |
uint8 client_random[SEC_RANDOM_SIZE]; |
uint8 client_random[SEC_RANDOM_SIZE]; |
528 |
uint32 rc4_key_size; |
uint32 rc4_key_size; |
529 |
|
|
530 |
if (!sec_parse_crypt_info(s, &rc4_key_size, &server_random, |
if (!sec_parse_crypt_info(s, &rc4_key_size, &server_random, &modulus, &exponent)) |
|
&modulus, &exponent)) |
|
531 |
return; |
return; |
532 |
|
|
533 |
/* Generate a client random, and hence determine encryption keys */ |
/* Generate a client random, and hence determine encryption keys */ |
534 |
generate_random(client_random); |
generate_random(client_random); |
535 |
sec_rsa_encrypt(sec_crypted_random, client_random, |
sec_rsa_encrypt(sec_crypted_random, client_random, SEC_RANDOM_SIZE, modulus, exponent); |
|
SEC_RANDOM_SIZE, modulus, exponent); |
|
536 |
sec_generate_keys(client_random, server_random, rc4_key_size); |
sec_generate_keys(client_random, server_random, rc4_key_size); |
537 |
} |
} |
538 |
|
|