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, |
void |
49 |
uint8 salt) |
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]; |
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]; |
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, |
54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, |
150 |
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, |
152 |
54, 54, 54 |
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, |
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, |
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, |
159 |
92, 92, 92, 92, 92, 92, 92 |
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]; |
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; |
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; |
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; |
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 |
|
|
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 |
|
|
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 |
|
|
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, |
sec_parse_crypt_info(STREAM s, uint32 *rc4_key_size, |
450 |
uint8 **exponent) |
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; |
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]; |
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; |
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; |
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 |
} |
} |