| 48 |
#define DEATTACK_DETECTED 1 |
#define DEATTACK_DETECTED 1 |
| 49 |
|
|
| 50 |
/* |
/* |
| 51 |
* $Id: crypt.c,v 1.11 2007-08-15 04:03:26 maya Exp $ Cryptographic attack |
* $Id: crypt.c,v 1.12 2007-08-20 14:21:57 maya Exp $ Cryptographic attack |
| 52 |
* detector for ssh - source code (C)1998 CORE-SDI, Buenos Aires Argentina |
* detector for ssh - source code (C)1998 CORE-SDI, Buenos Aires Argentina |
| 53 |
* Ariel Futoransky(futo@core-sdi.com) <http://www.core-sdi.com> |
* Ariel Futoransky(futo@core-sdi.com) <http://www.core-sdi.com> |
| 54 |
*/ |
*/ |
| 84 |
*/ |
*/ |
| 85 |
|
|
| 86 |
static int check_crc(unsigned char FAR * S, unsigned char FAR * buf, |
static int check_crc(unsigned char FAR * S, unsigned char FAR * buf, |
| 87 |
uint32 len, unsigned char FAR * IV) |
uint32 len, unsigned char FAR * IV) |
| 88 |
{ |
{ |
| 89 |
uint32 crc; |
uint32 crc; |
| 90 |
unsigned char FAR *c; |
unsigned char FAR *c; |
| 113 |
Detects a crc32 compensation attack on a packet |
Detects a crc32 compensation attack on a packet |
| 114 |
*/ |
*/ |
| 115 |
static int detect_attack(CRYPTDetectAttack FAR * statics, |
static int detect_attack(CRYPTDetectAttack FAR * statics, |
| 116 |
unsigned char FAR * buf, uint32 len, |
unsigned char FAR * buf, uint32 len, |
| 117 |
unsigned char *FAR IV) |
unsigned char *FAR IV) |
| 118 |
{ |
{ |
| 119 |
uint32 FAR *h = statics->h; |
uint32 FAR *h = statics->h; |
| 120 |
uint32 n = statics->n; |
uint32 n = statics->n; |
| 188 |
} |
} |
| 189 |
|
|
| 190 |
BOOL CRYPT_detect_attack(PTInstVar pvar, unsigned char FAR * buf, |
BOOL CRYPT_detect_attack(PTInstVar pvar, unsigned char FAR * buf, |
| 191 |
int bytes) |
int bytes) |
| 192 |
{ |
{ |
| 193 |
if (SSHv1(pvar)) { |
if (SSHv1(pvar)) { |
| 194 |
switch (pvar->crypt_state.sender_cipher) { |
switch (pvar->crypt_state.sender_cipher) { |
| 196 |
return FALSE; |
return FALSE; |
| 197 |
case SSH_CIPHER_IDEA: |
case SSH_CIPHER_IDEA: |
| 198 |
return detect_attack(&pvar->crypt_state.detect_attack_statics, |
return detect_attack(&pvar->crypt_state.detect_attack_statics, |
| 199 |
buf, bytes, |
buf, bytes, |
| 200 |
pvar->crypt_state.dec.cIDEA.ivec) == |
pvar->crypt_state.dec.cIDEA.ivec) == |
| 201 |
DEATTACK_DETECTED; |
DEATTACK_DETECTED; |
| 202 |
default: |
default: |
| 203 |
return detect_attack(&pvar->crypt_state.detect_attack_statics, |
return detect_attack(&pvar->crypt_state.detect_attack_statics, |
| 204 |
buf, bytes, NULL) == DEATTACK_DETECTED; |
buf, bytes, NULL) == DEATTACK_DETECTED; |
| 205 |
} |
} |
| 206 |
} else { |
} else { |
| 207 |
return FALSE; |
return FALSE; |
| 215 |
|
|
| 216 |
// for SSH2(yutaka) |
// for SSH2(yutaka) |
| 217 |
static void cAES128_encrypt(PTInstVar pvar, unsigned char FAR * buf, |
static void cAES128_encrypt(PTInstVar pvar, unsigned char FAR * buf, |
| 218 |
int bytes) |
int bytes) |
| 219 |
{ |
{ |
| 220 |
// unsigned char key[AES128_KEYLEN], iv[AES128_IVLEN]; |
// unsigned char key[AES128_KEYLEN], iv[AES128_IVLEN]; |
| 221 |
unsigned char *newbuf = malloc(bytes); |
unsigned char *newbuf = malloc(bytes); |
| 231 |
if (bytes % block_size) { |
if (bytes % block_size) { |
| 232 |
char tmp[80]; |
char tmp[80]; |
| 233 |
UTIL_get_lang_msg("MSG_AES128_ENCRYPT_ERROR1", pvar, |
UTIL_get_lang_msg("MSG_AES128_ENCRYPT_ERROR1", pvar, |
| 234 |
"AES128 encrypt error(1): bytes %d (%d)"); |
"AES128 encrypt error(1): bytes %d (%d)"); |
| 235 |
_snprintf_s(tmp, sizeof(tmp), _TRUNCATE, |
_snprintf_s(tmp, sizeof(tmp), _TRUNCATE, |
| 236 |
pvar->ts->UIMsg, bytes, block_size); |
pvar->ts->UIMsg, bytes, block_size); |
| 237 |
notify_fatal_error(pvar, tmp); |
notify_fatal_error(pvar, tmp); |
| 238 |
goto error; |
goto error; |
| 239 |
} |
} |
| 241 |
if (EVP_Cipher(&pvar->evpcip[MODE_OUT], newbuf, buf, bytes) == 0) { |
if (EVP_Cipher(&pvar->evpcip[MODE_OUT], newbuf, buf, bytes) == 0) { |
| 242 |
// TODO: failure |
// TODO: failure |
| 243 |
UTIL_get_lang_msg("MSG_AES128_ENCRYPT_ERROR2", pvar, |
UTIL_get_lang_msg("MSG_AES128_ENCRYPT_ERROR2", pvar, |
| 244 |
"AES128 encrypt error(1): bytes %d (%d)"); |
"AES128 encrypt error(1): bytes %d (%d)"); |
| 245 |
notify_fatal_error(pvar, pvar->ts->UIMsg); |
notify_fatal_error(pvar, pvar->ts->UIMsg); |
| 246 |
|
|
| 247 |
} else { |
} else { |
| 262 |
} |
} |
| 263 |
|
|
| 264 |
static void cAES128_decrypt(PTInstVar pvar, unsigned char FAR * buf, |
static void cAES128_decrypt(PTInstVar pvar, unsigned char FAR * buf, |
| 265 |
int bytes) |
int bytes) |
| 266 |
{ |
{ |
| 267 |
// unsigned char key[AES128_KEYLEN], iv[AES128_IVLEN]; |
// unsigned char key[AES128_KEYLEN], iv[AES128_IVLEN]; |
| 268 |
unsigned char *newbuf = malloc(bytes); |
unsigned char *newbuf = malloc(bytes); |
| 278 |
if (bytes % block_size) { |
if (bytes % block_size) { |
| 279 |
char tmp[80]; |
char tmp[80]; |
| 280 |
UTIL_get_lang_msg("MSG_AES128_DECRYPT_ERROR1", pvar, |
UTIL_get_lang_msg("MSG_AES128_DECRYPT_ERROR1", pvar, |
| 281 |
"AES128 decrypt error(1): bytes %d (%d)"); |
"AES128 decrypt error(1): bytes %d (%d)"); |
| 282 |
_snprintf_s(tmp, sizeof(tmp), _TRUNCATE, pvar->ts->UIMsg, bytes, block_size); |
_snprintf_s(tmp, sizeof(tmp), _TRUNCATE, pvar->ts->UIMsg, bytes, block_size); |
| 283 |
notify_fatal_error(pvar, tmp); |
notify_fatal_error(pvar, tmp); |
| 284 |
goto error; |
goto error; |
| 287 |
if (EVP_Cipher(&pvar->evpcip[MODE_IN], newbuf, buf, bytes) == 0) { |
if (EVP_Cipher(&pvar->evpcip[MODE_IN], newbuf, buf, bytes) == 0) { |
| 288 |
// TODO: |
// TODO: |
| 289 |
UTIL_get_lang_msg("MSG_AES128_DECRYPT_ERROR2", pvar, |
UTIL_get_lang_msg("MSG_AES128_DECRYPT_ERROR2", pvar, |
| 290 |
"AES128 decrypt error(2)"); |
"AES128 decrypt error(2)"); |
| 291 |
notify_fatal_error(pvar, pvar->ts->UIMsg); |
notify_fatal_error(pvar, pvar->ts->UIMsg); |
| 292 |
|
|
| 293 |
} else { |
} else { |
| 317 |
|
|
| 318 |
// for SSH2(yutaka) |
// for SSH2(yutaka) |
| 319 |
static void c3DES_CBC_encrypt(PTInstVar pvar, unsigned char FAR * buf, |
static void c3DES_CBC_encrypt(PTInstVar pvar, unsigned char FAR * buf, |
| 320 |
int bytes) |
int bytes) |
| 321 |
{ |
{ |
| 322 |
unsigned char key[24], iv[8]; |
unsigned char key[24], iv[8]; |
| 323 |
unsigned char *newbuf = malloc(bytes); |
unsigned char *newbuf = malloc(bytes); |
| 376 |
} |
} |
| 377 |
|
|
| 378 |
static void c3DES_CBC_decrypt(PTInstVar pvar, unsigned char FAR * buf, |
static void c3DES_CBC_decrypt(PTInstVar pvar, unsigned char FAR * buf, |
| 379 |
int bytes) |
int bytes) |
| 380 |
{ |
{ |
| 381 |
unsigned char key[24], iv[8]; |
unsigned char key[24], iv[8]; |
| 382 |
unsigned char *newbuf = malloc(bytes); |
unsigned char *newbuf = malloc(bytes); |
| 434 |
|
|
| 435 |
|
|
| 436 |
static void c3DES_encrypt(PTInstVar pvar, unsigned char FAR * buf, |
static void c3DES_encrypt(PTInstVar pvar, unsigned char FAR * buf, |
| 437 |
int bytes) |
int bytes) |
| 438 |
{ |
{ |
| 439 |
Cipher3DESState FAR *encryptstate = &pvar->crypt_state.enc.c3DES; |
Cipher3DESState FAR *encryptstate = &pvar->crypt_state.enc.c3DES; |
| 440 |
|
|
| 441 |
DES_ncbc_encrypt(buf, buf, bytes, |
DES_ncbc_encrypt(buf, buf, bytes, |
| 442 |
&encryptstate->k1, &encryptstate->ivec1, DES_ENCRYPT); |
&encryptstate->k1, &encryptstate->ivec1, DES_ENCRYPT); |
| 443 |
DES_ncbc_encrypt(buf, buf, bytes, |
DES_ncbc_encrypt(buf, buf, bytes, |
| 444 |
&encryptstate->k2, &encryptstate->ivec2, DES_DECRYPT); |
&encryptstate->k2, &encryptstate->ivec2, DES_DECRYPT); |
| 445 |
DES_ncbc_encrypt(buf, buf, bytes, |
DES_ncbc_encrypt(buf, buf, bytes, |
| 446 |
&encryptstate->k3, &encryptstate->ivec3, DES_ENCRYPT); |
&encryptstate->k3, &encryptstate->ivec3, DES_ENCRYPT); |
| 447 |
} |
} |
| 448 |
|
|
| 449 |
static void c3DES_decrypt(PTInstVar pvar, unsigned char FAR * buf, |
static void c3DES_decrypt(PTInstVar pvar, unsigned char FAR * buf, |
| 450 |
int bytes) |
int bytes) |
| 451 |
{ |
{ |
| 452 |
Cipher3DESState FAR *decryptstate = &pvar->crypt_state.dec.c3DES; |
Cipher3DESState FAR *decryptstate = &pvar->crypt_state.dec.c3DES; |
| 453 |
|
|
| 454 |
DES_ncbc_encrypt(buf, buf, bytes, |
DES_ncbc_encrypt(buf, buf, bytes, |
| 455 |
&decryptstate->k3, &decryptstate->ivec3, DES_DECRYPT); |
&decryptstate->k3, &decryptstate->ivec3, DES_DECRYPT); |
| 456 |
DES_ncbc_encrypt(buf, buf, bytes, |
DES_ncbc_encrypt(buf, buf, bytes, |
| 457 |
&decryptstate->k2, &decryptstate->ivec2, DES_ENCRYPT); |
&decryptstate->k2, &decryptstate->ivec2, DES_ENCRYPT); |
| 458 |
DES_ncbc_encrypt(buf, buf, bytes, |
DES_ncbc_encrypt(buf, buf, bytes, |
| 459 |
&decryptstate->k1, &decryptstate->ivec1, DES_DECRYPT); |
&decryptstate->k1, &decryptstate->ivec1, DES_DECRYPT); |
| 460 |
} |
} |
| 461 |
|
|
| 462 |
static void cDES_encrypt(PTInstVar pvar, unsigned char FAR * buf, |
static void cDES_encrypt(PTInstVar pvar, unsigned char FAR * buf, |
| 465 |
CipherDESState FAR *encryptstate = &pvar->crypt_state.enc.cDES; |
CipherDESState FAR *encryptstate = &pvar->crypt_state.enc.cDES; |
| 466 |
|
|
| 467 |
DES_ncbc_encrypt(buf, buf, bytes, |
DES_ncbc_encrypt(buf, buf, bytes, |
| 468 |
&encryptstate->k, &encryptstate->ivec, DES_ENCRYPT); |
&encryptstate->k, &encryptstate->ivec, DES_ENCRYPT); |
| 469 |
} |
} |
| 470 |
|
|
| 471 |
static void cDES_decrypt(PTInstVar pvar, unsigned char FAR * buf, |
static void cDES_decrypt(PTInstVar pvar, unsigned char FAR * buf, |
| 474 |
CipherDESState FAR *decryptstate = &pvar->crypt_state.dec.cDES; |
CipherDESState FAR *decryptstate = &pvar->crypt_state.dec.cDES; |
| 475 |
|
|
| 476 |
DES_ncbc_encrypt(buf, buf, bytes, |
DES_ncbc_encrypt(buf, buf, bytes, |
| 477 |
&decryptstate->k, &decryptstate->ivec, DES_DECRYPT); |
&decryptstate->k, &decryptstate->ivec, DES_DECRYPT); |
| 478 |
} |
} |
| 479 |
|
|
| 480 |
static void cIDEA_encrypt(PTInstVar pvar, unsigned char FAR * buf, |
static void cIDEA_encrypt(PTInstVar pvar, unsigned char FAR * buf, |
| 481 |
int bytes) |
int bytes) |
| 482 |
{ |
{ |
| 483 |
CipherIDEAState FAR *encryptstate = &pvar->crypt_state.enc.cIDEA; |
CipherIDEAState FAR *encryptstate = &pvar->crypt_state.enc.cIDEA; |
| 484 |
int num = 0; |
int num = 0; |
| 485 |
|
|
| 486 |
idea_cfb64_encrypt(buf, buf, bytes, &encryptstate->k, |
idea_cfb64_encrypt(buf, buf, bytes, &encryptstate->k, |
| 487 |
encryptstate->ivec, &num, IDEA_ENCRYPT); |
encryptstate->ivec, &num, IDEA_ENCRYPT); |
| 488 |
} |
} |
| 489 |
|
|
| 490 |
static void cIDEA_decrypt(PTInstVar pvar, unsigned char FAR * buf, |
static void cIDEA_decrypt(PTInstVar pvar, unsigned char FAR * buf, |
| 491 |
int bytes) |
int bytes) |
| 492 |
{ |
{ |
| 493 |
CipherIDEAState FAR *decryptstate = &pvar->crypt_state.dec.cIDEA; |
CipherIDEAState FAR *decryptstate = &pvar->crypt_state.dec.cIDEA; |
| 494 |
int num = 0; |
int num = 0; |
| 495 |
|
|
| 496 |
idea_cfb64_encrypt(buf, buf, bytes, &decryptstate->k, |
idea_cfb64_encrypt(buf, buf, bytes, &decryptstate->k, |
| 497 |
decryptstate->ivec, &num, IDEA_DECRYPT); |
decryptstate->ivec, &num, IDEA_DECRYPT); |
| 498 |
} |
} |
| 499 |
|
|
| 500 |
static void flip_endianness(unsigned char FAR * cbuf, int bytes) |
static void flip_endianness(unsigned char FAR * cbuf, int bytes) |
| 506 |
uint32 w = *buf; |
uint32 w = *buf; |
| 507 |
|
|
| 508 |
*buf = ((w << 24) & 0xFF000000) | ((w << 8) & 0x00FF0000) |
*buf = ((w << 24) & 0xFF000000) | ((w << 8) & 0x00FF0000) |
| 509 |
| ((w >> 8) & 0x0000FF00) | ((w >> 24) & 0x000000FF); |
| ((w >> 8) & 0x0000FF00) | ((w >> 24) & 0x000000FF); |
| 510 |
count--; |
count--; |
| 511 |
buf++; |
buf++; |
| 512 |
} |
} |
| 513 |
} |
} |
| 514 |
|
|
| 515 |
static void cBlowfish_encrypt(PTInstVar pvar, unsigned char FAR * buf, |
static void cBlowfish_encrypt(PTInstVar pvar, unsigned char FAR * buf, |
| 516 |
int bytes) |
int bytes) |
| 517 |
{ |
{ |
| 518 |
CipherBlowfishState FAR *encryptstate = |
CipherBlowfishState FAR *encryptstate = |
| 519 |
&pvar->crypt_state.enc.cBlowfish; |
&pvar->crypt_state.enc.cBlowfish; |
| 520 |
|
|
| 521 |
flip_endianness(buf, bytes); |
flip_endianness(buf, bytes); |
| 522 |
BF_cbc_encrypt(buf, buf, bytes, &encryptstate->k, encryptstate->ivec, |
BF_cbc_encrypt(buf, buf, bytes, &encryptstate->k, encryptstate->ivec, |
| 523 |
BF_ENCRYPT); |
BF_ENCRYPT); |
| 524 |
flip_endianness(buf, bytes); |
flip_endianness(buf, bytes); |
| 525 |
} |
} |
| 526 |
|
|
| 527 |
static void cBlowfish_decrypt(PTInstVar pvar, unsigned char FAR * buf, |
static void cBlowfish_decrypt(PTInstVar pvar, unsigned char FAR * buf, |
| 528 |
int bytes) |
int bytes) |
| 529 |
{ |
{ |
| 530 |
CipherBlowfishState FAR *decryptstate = |
CipherBlowfishState FAR *decryptstate = |
| 531 |
&pvar->crypt_state.dec.cBlowfish; |
&pvar->crypt_state.dec.cBlowfish; |
| 532 |
|
|
| 533 |
flip_endianness(buf, bytes); |
flip_endianness(buf, bytes); |
| 534 |
BF_cbc_encrypt(buf, buf, bytes, &decryptstate->k, decryptstate->ivec, |
BF_cbc_encrypt(buf, buf, bytes, &decryptstate->k, decryptstate->ivec, |
| 535 |
BF_DECRYPT); |
BF_DECRYPT); |
| 536 |
flip_endianness(buf, bytes); |
flip_endianness(buf, bytes); |
| 537 |
} |
} |
| 538 |
|
|
| 539 |
static void cRC4_encrypt(PTInstVar pvar, unsigned char FAR * buf, |
static void cRC4_encrypt(PTInstVar pvar, unsigned char FAR * buf, |
| 540 |
int bytes) |
int bytes) |
| 541 |
{ |
{ |
| 542 |
CipherRC4State FAR *encryptstate = &pvar->crypt_state.enc.cRC4; |
CipherRC4State FAR *encryptstate = &pvar->crypt_state.enc.cRC4; |
| 543 |
int num = 0; |
int num = 0; |
| 546 |
} |
} |
| 547 |
|
|
| 548 |
static void cRC4_decrypt(PTInstVar pvar, unsigned char FAR * buf, |
static void cRC4_decrypt(PTInstVar pvar, unsigned char FAR * buf, |
| 549 |
int bytes) |
int bytes) |
| 550 |
{ |
{ |
| 551 |
CipherRC4State FAR *decryptstate = &pvar->crypt_state.dec.cRC4; |
CipherRC4State FAR *decryptstate = &pvar->crypt_state.dec.cRC4; |
| 552 |
int num = 0; |
int num = 0; |
| 555 |
} |
} |
| 556 |
|
|
| 557 |
void CRYPT_set_random_data(PTInstVar pvar, unsigned char FAR * buf, |
void CRYPT_set_random_data(PTInstVar pvar, unsigned char FAR * buf, |
| 558 |
int bytes) |
int bytes) |
| 559 |
{ |
{ |
| 560 |
RAND_bytes(buf, bytes); |
RAND_bytes(buf, bytes); |
| 561 |
} |
} |
| 574 |
|
|
| 575 |
// make_key()を fingerprint 生成でも利用するので、staticを削除。(2006.3.27 yutaka) |
// make_key()を fingerprint 生成でも利用するので、staticを削除。(2006.3.27 yutaka) |
| 576 |
RSA FAR *make_key(PTInstVar pvar, |
RSA FAR *make_key(PTInstVar pvar, |
| 577 |
int bits, unsigned char FAR * exp, |
int bits, unsigned char FAR * exp, |
| 578 |
unsigned char FAR * mod) |
unsigned char FAR * mod) |
| 579 |
{ |
{ |
| 580 |
RSA FAR *key = RSA_new(); |
RSA FAR *key = RSA_new(); |
| 581 |
|
|
| 586 |
|
|
| 587 |
if (key == NULL || key->e == NULL || key->n == NULL) { |
if (key == NULL || key->e == NULL || key->n == NULL) { |
| 588 |
UTIL_get_lang_msg("MSG_RSAKEY_SETUP_ERROR", pvar, |
UTIL_get_lang_msg("MSG_RSAKEY_SETUP_ERROR", pvar, |
| 589 |
"Error setting up RSA keys"); |
"Error setting up RSA keys"); |
| 590 |
notify_fatal_error(pvar, pvar->ts->UIMsg); |
notify_fatal_error(pvar, pvar->ts->UIMsg); |
| 591 |
|
|
| 592 |
if (key != NULL) { |
if (key != NULL) { |
| 611 |
memcpy(pvar->crypt_state.server_cookie, cookie, SSH_COOKIE_LENGTH); |
memcpy(pvar->crypt_state.server_cookie, cookie, SSH_COOKIE_LENGTH); |
| 612 |
} else { |
} else { |
| 613 |
memcpy(pvar->crypt_state.server_cookie, cookie, |
memcpy(pvar->crypt_state.server_cookie, cookie, |
| 614 |
SSH2_COOKIE_LENGTH); |
SSH2_COOKIE_LENGTH); |
| 615 |
} |
} |
| 616 |
} |
} |
| 617 |
|
|
| 619 |
{ |
{ |
| 620 |
if (SSHv2(pvar)) { |
if (SSHv2(pvar)) { |
| 621 |
memcpy(pvar->crypt_state.client_cookie, cookie, |
memcpy(pvar->crypt_state.client_cookie, cookie, |
| 622 |
SSH2_COOKIE_LENGTH); |
SSH2_COOKIE_LENGTH); |
| 623 |
} |
} |
| 624 |
} |
} |
| 625 |
|
|
| 626 |
BOOL CRYPT_set_server_RSA_key(PTInstVar pvar, |
BOOL CRYPT_set_server_RSA_key(PTInstVar pvar, |
| 627 |
int bits, unsigned char FAR * exp, |
int bits, unsigned char FAR * exp, |
| 628 |
unsigned char FAR * mod) |
unsigned char FAR * mod) |
| 629 |
{ |
{ |
| 630 |
pvar->crypt_state.server_key.RSA_key = make_key(pvar, bits, exp, mod); |
pvar->crypt_state.server_key.RSA_key = make_key(pvar, bits, exp, mod); |
| 631 |
|
|
| 633 |
} |
} |
| 634 |
|
|
| 635 |
BOOL CRYPT_set_host_RSA_key(PTInstVar pvar, |
BOOL CRYPT_set_host_RSA_key(PTInstVar pvar, |
| 636 |
int bits, unsigned char FAR * exp, |
int bits, unsigned char FAR * exp, |
| 637 |
unsigned char FAR * mod) |
unsigned char FAR * mod) |
| 638 |
{ |
{ |
| 639 |
pvar->crypt_state.host_key.RSA_key = make_key(pvar, bits, exp, mod); |
pvar->crypt_state.host_key.RSA_key = make_key(pvar, bits, exp, mod); |
| 640 |
|
|
| 642 |
} |
} |
| 643 |
|
|
| 644 |
BOOL CRYPT_set_supported_ciphers(PTInstVar pvar, int sender_ciphers, |
BOOL CRYPT_set_supported_ciphers(PTInstVar pvar, int sender_ciphers, |
| 645 |
int receiver_ciphers) |
int receiver_ciphers) |
| 646 |
{ |
{ |
| 647 |
int cipher_mask; |
int cipher_mask; |
| 648 |
|
|
| 649 |
if (SSHv1(pvar)) { |
if (SSHv1(pvar)) { |
| 650 |
cipher_mask = (1 << SSH_CIPHER_DES) |
cipher_mask = (1 << SSH_CIPHER_DES) |
| 651 |
| (1 << SSH_CIPHER_3DES) |
| (1 << SSH_CIPHER_3DES) |
| 652 |
| (1 << SSH_CIPHER_BLOWFISH); |
| (1 << SSH_CIPHER_BLOWFISH); |
| 653 |
|
|
| 654 |
} else { // for SSH2(yutaka) |
} else { // for SSH2(yutaka) |
| 655 |
// SSH2がサポートするデータ通信用アルゴリズム(公開鍵交換用とは別) |
// SSH2がサポートするデータ通信用アルゴリズム(公開鍵交換用とは別) |
| 664 |
|
|
| 665 |
if (sender_ciphers == 0) { |
if (sender_ciphers == 0) { |
| 666 |
UTIL_get_lang_msg("MSG_UNAVAILABLE_CIPHERS_ERROR", pvar, |
UTIL_get_lang_msg("MSG_UNAVAILABLE_CIPHERS_ERROR", pvar, |
| 667 |
"The server does not support any of the TTSSH encryption algorithms.\n" |
"The server does not support any of the TTSSH encryption algorithms.\n" |
| 668 |
"A secure connection cannot be made in the TTSSH-to-server direction.\n" |
"A secure connection cannot be made in the TTSSH-to-server direction.\n" |
| 669 |
"The connection will be closed."); |
"The connection will be closed."); |
| 670 |
notify_fatal_error(pvar, pvar->ts->UIMsg); |
notify_fatal_error(pvar, pvar->ts->UIMsg); |
| 671 |
return FALSE; |
return FALSE; |
| 672 |
} else if (receiver_ciphers == 0) { |
} else if (receiver_ciphers == 0) { |
| 673 |
UTIL_get_lang_msg("MSG_UNAVAILABLE_CIPHERS_ERROR", pvar, |
UTIL_get_lang_msg("MSG_UNAVAILABLE_CIPHERS_ERROR", pvar, |
| 674 |
"The server does not support any of the TTSSH encryption algorithms.\n" |
"The server does not support any of the TTSSH encryption algorithms.\n" |
| 675 |
"A secure connection cannot be made in the TTSSH-to-server direction.\n" |
"A secure connection cannot be made in the TTSSH-to-server direction.\n" |
| 676 |
"The connection will be closed."); |
"The connection will be closed."); |
| 677 |
notify_fatal_error(pvar, pvar->ts->UIMsg); |
notify_fatal_error(pvar, pvar->ts->UIMsg); |
| 678 |
return FALSE; |
return FALSE; |
| 679 |
} else { |
} else { |
| 724 |
// ※本関数は SSH2 でのみ使用される。 |
// ※本関数は SSH2 でのみ使用される。 |
| 725 |
// (2004.12.17 yutaka) |
// (2004.12.17 yutaka) |
| 726 |
BOOL CRYPT_verify_receiver_MAC(PTInstVar pvar, uint32 sequence_number, |
BOOL CRYPT_verify_receiver_MAC(PTInstVar pvar, uint32 sequence_number, |
| 727 |
char FAR * data, int len, char FAR * MAC) |
char FAR * data, int len, char FAR * MAC) |
| 728 |
{ |
{ |
| 729 |
HMAC_CTX c; |
HMAC_CTX c; |
| 730 |
unsigned char m[EVP_MAX_MD_SIZE]; |
unsigned char m[EVP_MAX_MD_SIZE]; |
| 731 |
unsigned char b[4]; |
unsigned char b[4]; |
| 732 |
struct Mac *mac; |
struct Mac *mac; |
| 733 |
|
|
| 734 |
mac = &pvar->ssh2_keys[MODE_IN].mac; |
mac = &pvar->ssh2_keys[MODE_IN].mac; |
| 737 |
if (mac == NULL || mac->enabled == 0) |
if (mac == NULL || mac->enabled == 0) |
| 738 |
return TRUE; |
return TRUE; |
| 739 |
|
|
| 740 |
if (mac->key == NULL) |
if (mac->key == NULL) |
| 741 |
goto error; |
goto error; |
| 742 |
|
|
| 743 |
if ((u_int)mac->mac_len > sizeof(m)) |
if ((u_int)mac->mac_len > sizeof(m)) |
| 744 |
goto error; |
goto error; |
| 745 |
|
|
| 746 |
HMAC_Init(&c, mac->key, mac->key_len, mac->md); |
HMAC_Init(&c, mac->key, mac->key_len, mac->md); |
| 747 |
set_uint32_MSBfirst(b, sequence_number); |
set_uint32_MSBfirst(b, sequence_number); |
| 748 |
HMAC_Update(&c, b, sizeof(b)); |
HMAC_Update(&c, b, sizeof(b)); |
| 749 |
HMAC_Update(&c, data, len); |
HMAC_Update(&c, data, len); |
| 750 |
HMAC_Final(&c, m, NULL); |
HMAC_Final(&c, m, NULL); |
| 751 |
HMAC_cleanup(&c); |
HMAC_cleanup(&c); |
| 752 |
|
|
| 753 |
if (memcmp(m, MAC, mac->mac_len)) { |
if (memcmp(m, MAC, mac->mac_len)) { |
| 754 |
goto error; |
goto error; |
| 777 |
|
|
| 778 |
// for SSH2 |
// for SSH2 |
| 779 |
BOOL CRYPT_build_sender_MAC(PTInstVar pvar, uint32 sequence_number, |
BOOL CRYPT_build_sender_MAC(PTInstVar pvar, uint32 sequence_number, |
| 780 |
char FAR * data, int len, char FAR * MAC) |
char FAR * data, int len, char FAR * MAC) |
| 781 |
{ |
{ |
| 782 |
HMAC_CTX c; |
HMAC_CTX c; |
| 783 |
static u_char m[EVP_MAX_MD_SIZE]; |
static u_char m[EVP_MAX_MD_SIZE]; |
| 784 |
u_char b[4]; |
u_char b[4]; |
| 785 |
struct Mac *mac; |
struct Mac *mac; |
| 786 |
|
|
| 788 |
mac = &pvar->ssh2_keys[MODE_OUT].mac; |
mac = &pvar->ssh2_keys[MODE_OUT].mac; |
| 789 |
if (mac == NULL || mac->enabled == 0) |
if (mac == NULL || mac->enabled == 0) |
| 790 |
return FALSE; |
return FALSE; |
| 791 |
|
|
| 792 |
HMAC_Init(&c, mac->key, mac->key_len, mac->md); |
HMAC_Init(&c, mac->key, mac->key_len, mac->md); |
| 793 |
set_uint32_MSBfirst(b, sequence_number); |
set_uint32_MSBfirst(b, sequence_number); |
| 794 |
HMAC_Update(&c, b, sizeof(b)); |
HMAC_Update(&c, b, sizeof(b)); |
| 795 |
HMAC_Update(&c, data, len); |
HMAC_Update(&c, data, len); |
| 828 |
{ |
{ |
| 829 |
if (SSHv1(pvar)) { |
if (SSHv1(pvar)) { |
| 830 |
pvar->crypt_state.sender_cipher = choose_cipher(pvar, |
pvar->crypt_state.sender_cipher = choose_cipher(pvar, |
| 831 |
pvar->crypt_state. |
pvar->crypt_state. |
| 832 |
supported_sender_ciphers); |
supported_sender_ciphers); |
| 833 |
pvar->crypt_state.receiver_cipher = |
pvar->crypt_state.receiver_cipher = |
| 834 |
choose_cipher(pvar, pvar->crypt_state.supported_receiver_ciphers); |
choose_cipher(pvar, pvar->crypt_state.supported_receiver_ciphers); |
| 835 |
|
|
| 842 |
if (pvar->crypt_state.sender_cipher == SSH_CIPHER_NONE |
if (pvar->crypt_state.sender_cipher == SSH_CIPHER_NONE |
| 843 |
|| pvar->crypt_state.receiver_cipher == SSH_CIPHER_NONE) { |
|| pvar->crypt_state.receiver_cipher == SSH_CIPHER_NONE) { |
| 844 |
UTIL_get_lang_msg("MSG_CHIPHER_NONE_ERROR", pvar, |
UTIL_get_lang_msg("MSG_CHIPHER_NONE_ERROR", pvar, |
| 845 |
"All the encryption algorithms that this program and the server both understand have been disabled.\n" |
"All the encryption algorithms that this program and the server both understand have been disabled.\n" |
| 846 |
"To communicate with this server, you will have to enable some more ciphers\n" |
"To communicate with this server, you will have to enable some more ciphers\n" |
| 847 |
"in the TTSSH Setup dialog box when you run Teraterm again.\n" |
"in the TTSSH Setup dialog box when you run Teraterm again.\n" |
| 848 |
"This connection will now close."); |
"This connection will now close."); |
| 849 |
notify_fatal_error(pvar, pvar->ts->UIMsg); |
notify_fatal_error(pvar, pvar->ts->UIMsg); |
| 850 |
return FALSE; |
return FALSE; |
| 851 |
} else { |
} else { |
| 869 |
} |
} |
| 870 |
|
|
| 871 |
int CRYPT_choose_session_key(PTInstVar pvar, |
int CRYPT_choose_session_key(PTInstVar pvar, |
| 872 |
unsigned char FAR * encrypted_key_buf) |
unsigned char FAR * encrypted_key_buf) |
| 873 |
{ |
{ |
| 874 |
int server_key_bits = |
int server_key_bits = |
| 875 |
BN_num_bits(pvar->crypt_state.server_key.RSA_key->n); |
BN_num_bits(pvar->crypt_state.server_key.RSA_key->n); |
| 889 |
|
|
| 890 |
if (bit_delta < 128 || server_key_bits < 512 || host_key_bits < 512) { |
if (bit_delta < 128 || server_key_bits < 512 || host_key_bits < 512) { |
| 891 |
UTIL_get_lang_msg("MSG_RASKEY_TOOWEAK_ERROR", pvar, |
UTIL_get_lang_msg("MSG_RASKEY_TOOWEAK_ERROR", pvar, |
| 892 |
"Server RSA keys are too weak. A secure connection cannot be established."); |
"Server RSA keys are too weak. A secure connection cannot be established."); |
| 893 |
notify_fatal_error(pvar, pvar->ts->UIMsg); |
notify_fatal_error(pvar, pvar->ts->UIMsg); |
| 894 |
return 0; |
return 0; |
| 895 |
} else { |
} else { |
| 902 |
|
|
| 903 |
BN_bn2bin(pvar->crypt_state.host_key.RSA_key->n, session_buf); |
BN_bn2bin(pvar->crypt_state.host_key.RSA_key->n, session_buf); |
| 904 |
BN_bn2bin(pvar->crypt_state.server_key.RSA_key->n, |
BN_bn2bin(pvar->crypt_state.server_key.RSA_key->n, |
| 905 |
session_buf + host_key_bytes); |
session_buf + host_key_bytes); |
| 906 |
memcpy(session_buf + server_key_bytes + host_key_bytes, |
memcpy(session_buf + server_key_bytes + host_key_bytes, |
| 907 |
pvar->crypt_state.server_cookie, 8); |
pvar->crypt_state.server_cookie, 8); |
| 908 |
MD5(session_buf, session_buf_len, session_id); |
MD5(session_buf, session_buf_len, session_id); |
| 909 |
|
|
| 910 |
free(session_buf); |
free(session_buf); |
| 911 |
|
|
| 912 |
RAND_bytes(pvar->crypt_state.sender_cipher_key, |
RAND_bytes(pvar->crypt_state.sender_cipher_key, |
| 913 |
SSH_SESSION_KEY_LENGTH); |
SSH_SESSION_KEY_LENGTH); |
| 914 |
memcpy(pvar->crypt_state.receiver_cipher_key, |
memcpy(pvar->crypt_state.receiver_cipher_key, |
| 915 |
pvar->crypt_state.sender_cipher_key, |
pvar->crypt_state.sender_cipher_key, |
| 916 |
SSH_SESSION_KEY_LENGTH); |
SSH_SESSION_KEY_LENGTH); |
| 917 |
|
|
| 918 |
memcpy(encrypted_key_buf + encrypted_key_bytes - |
memcpy(encrypted_key_buf + encrypted_key_bytes - |
| 919 |
SSH_SESSION_KEY_LENGTH, pvar->crypt_state.sender_cipher_key, |
SSH_SESSION_KEY_LENGTH, pvar->crypt_state.sender_cipher_key, |
| 920 |
SSH_SESSION_KEY_LENGTH); |
SSH_SESSION_KEY_LENGTH); |
| 921 |
for (i = 0; i < sizeof(session_id); i++) { |
for (i = 0; i < sizeof(session_id); i++) { |
| 922 |
encrypted_key_buf[encrypted_key_bytes - |
encrypted_key_buf[encrypted_key_bytes - |
| 923 |
SSH_SESSION_KEY_LENGTH + i] |
SSH_SESSION_KEY_LENGTH + i] |
| 924 |
^= session_id[i]; |
^= session_id[i]; |
| 925 |
} |
} |
| 926 |
|
|
| 927 |
if (host_key_bits > server_key_bits) { |
if (host_key_bits > server_key_bits) { |
| 928 |
if (RSA_public_encrypt(SSH_SESSION_KEY_LENGTH, |
if (RSA_public_encrypt(SSH_SESSION_KEY_LENGTH, |
| 929 |
encrypted_key_buf + |
encrypted_key_buf + |
| 930 |
encrypted_key_bytes - |
encrypted_key_bytes - |
| 931 |
SSH_SESSION_KEY_LENGTH, |
SSH_SESSION_KEY_LENGTH, |
| 932 |
encrypted_key_buf + |
encrypted_key_buf + |
| 933 |
encrypted_key_bytes - server_key_bytes, |
encrypted_key_bytes - server_key_bytes, |
| 934 |
pvar->crypt_state.server_key.RSA_key, |
pvar->crypt_state.server_key.RSA_key, |
| 935 |
RSA_PKCS1_PADDING) < 0) |
RSA_PKCS1_PADDING) < 0) |
| 936 |
return 0; |
return 0; |
| 937 |
|
|
| 938 |
if (RSA_public_encrypt(server_key_bytes, |
if (RSA_public_encrypt(server_key_bytes, |
| 939 |
encrypted_key_buf + |
encrypted_key_buf + |
| 940 |
encrypted_key_bytes - server_key_bytes, |
encrypted_key_bytes - server_key_bytes, |
| 941 |
encrypted_key_buf, |
encrypted_key_buf, |
| 942 |
pvar->crypt_state.host_key.RSA_key, |
pvar->crypt_state.host_key.RSA_key, |
| 943 |
RSA_PKCS1_PADDING) < 0) |
RSA_PKCS1_PADDING) < 0) |
| 944 |
return 0; |
return 0; |
| 945 |
} else { |
} else { |
| 946 |
if (RSA_public_encrypt(SSH_SESSION_KEY_LENGTH, |
if (RSA_public_encrypt(SSH_SESSION_KEY_LENGTH, |
| 947 |
encrypted_key_buf + |
encrypted_key_buf + |
| 948 |
encrypted_key_bytes - |
encrypted_key_bytes - |
| 949 |
SSH_SESSION_KEY_LENGTH, |
SSH_SESSION_KEY_LENGTH, |
| 950 |
encrypted_key_buf + |
encrypted_key_buf + |
| 951 |
encrypted_key_bytes - host_key_bytes, |
encrypted_key_bytes - host_key_bytes, |
| 952 |
pvar->crypt_state.host_key.RSA_key, |
pvar->crypt_state.host_key.RSA_key, |
| 953 |
RSA_PKCS1_PADDING) < 0) |
RSA_PKCS1_PADDING) < 0) |
| 954 |
return 0; |
return 0; |
| 955 |
|
|
| 956 |
if (RSA_public_encrypt(host_key_bytes, |
if (RSA_public_encrypt(host_key_bytes, |
| 957 |
encrypted_key_buf + |
encrypted_key_buf + |
| 958 |
encrypted_key_bytes - host_key_bytes, |
encrypted_key_bytes - host_key_bytes, |
| 959 |
encrypted_key_buf, |
encrypted_key_buf, |
| 960 |
pvar->crypt_state.server_key.RSA_key, |
pvar->crypt_state.server_key.RSA_key, |
| 961 |
RSA_PKCS1_PADDING) < 0) |
RSA_PKCS1_PADDING) < 0) |
| 962 |
return 0; |
return 0; |
| 963 |
} |
} |
| 964 |
} |
} |
| 967 |
} |
} |
| 968 |
|
|
| 969 |
int CRYPT_generate_RSA_challenge_response(PTInstVar pvar, |
int CRYPT_generate_RSA_challenge_response(PTInstVar pvar, |
| 970 |
unsigned char FAR * challenge, |
unsigned char FAR * challenge, |
| 971 |
int challenge_len, |
int challenge_len, |
| 972 |
unsigned char FAR * response) |
unsigned char FAR * response) |
| 973 |
{ |
{ |
| 974 |
int server_key_bits = |
int server_key_bits = |
| 975 |
BN_num_bits(pvar->crypt_state.server_key.RSA_key->n); |
BN_num_bits(pvar->crypt_state.server_key.RSA_key->n); |
| 983 |
|
|
| 984 |
decrypted_challenge_len = |
decrypted_challenge_len = |
| 985 |
RSA_private_decrypt(challenge_len, challenge, challenge, |
RSA_private_decrypt(challenge_len, challenge, challenge, |
| 986 |
AUTH_get_cur_cred(pvar)->key_pair->RSA_key, |
AUTH_get_cur_cred(pvar)->key_pair->RSA_key, |
| 987 |
RSA_PKCS1_PADDING); |
RSA_PKCS1_PADDING); |
| 988 |
if (decrypted_challenge_len < 0) { |
if (decrypted_challenge_len < 0) { |
| 989 |
free(session_buf); |
free(session_buf); |
| 990 |
return 0; |
return 0; |
| 991 |
} |
} |
| 992 |
if (decrypted_challenge_len >= SSH_RSA_CHALLENGE_LENGTH) { |
if (decrypted_challenge_len >= SSH_RSA_CHALLENGE_LENGTH) { |
| 993 |
memcpy(decrypted_challenge, |
memcpy(decrypted_challenge, |
| 994 |
challenge + decrypted_challenge_len - |
challenge + decrypted_challenge_len - |
| 995 |
SSH_RSA_CHALLENGE_LENGTH, SSH_RSA_CHALLENGE_LENGTH); |
SSH_RSA_CHALLENGE_LENGTH, SSH_RSA_CHALLENGE_LENGTH); |
| 996 |
} else { |
} else { |
| 997 |
memset(decrypted_challenge, 0, |
memset(decrypted_challenge, 0, |
| 998 |
SSH_RSA_CHALLENGE_LENGTH - decrypted_challenge_len); |
SSH_RSA_CHALLENGE_LENGTH - decrypted_challenge_len); |
| 999 |
memcpy(decrypted_challenge + SSH_RSA_CHALLENGE_LENGTH - |
memcpy(decrypted_challenge + SSH_RSA_CHALLENGE_LENGTH - |
| 1000 |
decrypted_challenge_len, challenge, |
decrypted_challenge_len, challenge, |
| 1001 |
decrypted_challenge_len); |
decrypted_challenge_len); |
| 1002 |
} |
} |
| 1003 |
|
|
| 1004 |
BN_bn2bin(pvar->crypt_state.host_key.RSA_key->n, session_buf); |
BN_bn2bin(pvar->crypt_state.host_key.RSA_key->n, session_buf); |
| 1005 |
BN_bn2bin(pvar->crypt_state.server_key.RSA_key->n, |
BN_bn2bin(pvar->crypt_state.server_key.RSA_key->n, |
| 1006 |
session_buf + host_key_bytes); |
session_buf + host_key_bytes); |
| 1007 |
memcpy(session_buf + server_key_bytes + host_key_bytes, |
memcpy(session_buf + server_key_bytes + host_key_bytes, |
| 1008 |
pvar->crypt_state.server_cookie, 8); |
pvar->crypt_state.server_cookie, 8); |
| 1009 |
MD5(session_buf, session_buf_len, decrypted_challenge + 32); |
MD5(session_buf, session_buf_len, decrypted_challenge + 32); |
| 1010 |
|
|
| 1011 |
free(session_buf); |
free(session_buf); |
| 1038 |
} |
} |
| 1039 |
|
|
| 1040 |
static void cBlowfish_init(char FAR * session_key, |
static void cBlowfish_init(char FAR * session_key, |
| 1041 |
CipherBlowfishState FAR * state) |
CipherBlowfishState FAR * state) |
| 1042 |
{ |
{ |
| 1043 |
BF_set_key(&state->k, 32, session_key); |
BF_set_key(&state->k, 32, session_key); |
| 1044 |
memset(state->ivec, 0, 8); |
memset(state->ivec, 0, 8); |
| 1050 |
// |
// |
| 1051 |
// for SSH2(yutaka) |
// for SSH2(yutaka) |
| 1052 |
// |
// |
| 1053 |
void cipher_init_SSH2( |
void cipher_init_SSH2(EVP_CIPHER_CTX *evp, |
| 1054 |
EVP_CIPHER_CTX *evp, |
const u_char *key, u_int keylen, |
| 1055 |
const u_char *key, u_int keylen, |
const u_char *iv, u_int ivlen, |
| 1056 |
const u_char *iv, u_int ivlen, |
int encrypt, |
| 1057 |
int encrypt, |
const EVP_CIPHER *(*func)(void)) |
|
const EVP_CIPHER *(*func)(void) |
|
|
) |
|
| 1058 |
{ |
{ |
| 1059 |
EVP_CIPHER *type; |
EVP_CIPHER *type; |
| 1060 |
int klen; |
int klen; |
| 1092 |
struct Enc *enc; |
struct Enc *enc; |
| 1093 |
|
|
| 1094 |
enc = &pvar->ssh2_keys[MODE_OUT].enc; |
enc = &pvar->ssh2_keys[MODE_OUT].enc; |
| 1095 |
cipher_init_SSH2(&pvar->evpcip[MODE_OUT], |
cipher_init_SSH2(&pvar->evpcip[MODE_OUT], |
| 1096 |
enc->key, 24, enc->iv, 8, |
enc->key, 24, enc->iv, 8, |
| 1097 |
CIPHER_ENCRYPT, |
CIPHER_ENCRYPT, |
| 1098 |
EVP_des_ede3_cbc); |
EVP_des_ede3_cbc); |
| 1099 |
|
|
| 1100 |
//debug_print(10, enc->key, 24); |
//debug_print(10, enc->key, 24); |
| 1101 |
//debug_print(11, enc->iv, 24); |
//debug_print(11, enc->iv, 24); |
| 1111 |
|
|
| 1112 |
enc = &pvar->ssh2_keys[MODE_OUT].enc; |
enc = &pvar->ssh2_keys[MODE_OUT].enc; |
| 1113 |
cipher_init_SSH2(&pvar->evpcip[MODE_OUT], |
cipher_init_SSH2(&pvar->evpcip[MODE_OUT], |
| 1114 |
enc->key, 16, enc->iv, 16, |
enc->key, 16, enc->iv, 16, |
| 1115 |
CIPHER_ENCRYPT, |
CIPHER_ENCRYPT, |
| 1116 |
EVP_aes_128_cbc); |
EVP_aes_128_cbc); |
| 1117 |
|
|
| 1118 |
//debug_print(10, enc->key, 24); |
//debug_print(10, enc->key, 24); |
| 1119 |
//debug_print(11, enc->iv, 24); |
//debug_print(11, enc->iv, 24); |
| 1145 |
} |
} |
| 1146 |
case SSH_CIPHER_BLOWFISH:{ |
case SSH_CIPHER_BLOWFISH:{ |
| 1147 |
cBlowfish_init(encryption_key, |
cBlowfish_init(encryption_key, |
| 1148 |
&pvar->crypt_state.enc.cBlowfish); |
&pvar->crypt_state.enc.cBlowfish); |
| 1149 |
pvar->crypt_state.encrypt = cBlowfish_encrypt; |
pvar->crypt_state.encrypt = cBlowfish_encrypt; |
| 1150 |
break; |
break; |
| 1151 |
} |
} |
| 1164 |
|
|
| 1165 |
enc = &pvar->ssh2_keys[MODE_IN].enc; |
enc = &pvar->ssh2_keys[MODE_IN].enc; |
| 1166 |
cipher_init_SSH2(&pvar->evpcip[MODE_IN], |
cipher_init_SSH2(&pvar->evpcip[MODE_IN], |
| 1167 |
enc->key, 24, enc->iv, 8, |
enc->key, 24, enc->iv, 8, |
| 1168 |
CIPHER_DECRYPT, |
CIPHER_DECRYPT, |
| 1169 |
EVP_des_ede3_cbc); |
EVP_des_ede3_cbc); |
| 1170 |
|
|
| 1171 |
//debug_print(12, enc->key, 24); |
//debug_print(12, enc->key, 24); |
| 1172 |
//debug_print(13, enc->iv, 24); |
//debug_print(13, enc->iv, 24); |
| 1182 |
|
|
| 1183 |
enc = &pvar->ssh2_keys[MODE_IN].enc; |
enc = &pvar->ssh2_keys[MODE_IN].enc; |
| 1184 |
cipher_init_SSH2(&pvar->evpcip[MODE_IN], |
cipher_init_SSH2(&pvar->evpcip[MODE_IN], |
| 1185 |
enc->key, 16, enc->iv, 16, |
enc->key, 16, enc->iv, 16, |
| 1186 |
CIPHER_DECRYPT, |
CIPHER_DECRYPT, |
| 1187 |
EVP_aes_128_cbc); |
EVP_aes_128_cbc); |
| 1188 |
|
|
| 1189 |
//debug_print(12, enc->key, 24); |
//debug_print(12, enc->key, 24); |
| 1190 |
//debug_print(13, enc->iv, 24); |
//debug_print(13, enc->iv, 24); |
| 1215 |
} |
} |
| 1216 |
case SSH_CIPHER_BLOWFISH:{ |
case SSH_CIPHER_BLOWFISH:{ |
| 1217 |
cBlowfish_init(decryption_key, |
cBlowfish_init(decryption_key, |
| 1218 |
&pvar->crypt_state.dec.cBlowfish); |
&pvar->crypt_state.dec.cBlowfish); |
| 1219 |
pvar->crypt_state.decrypt = cBlowfish_decrypt; |
pvar->crypt_state.decrypt = cBlowfish_decrypt; |
| 1220 |
break; |
break; |
| 1221 |
} |
} |
| 1227 |
|
|
| 1228 |
if (!isOK) { |
if (!isOK) { |
| 1229 |
UTIL_get_lang_msg("MSG_CHPHER_NOTSELECTED_ERROR", pvar, |
UTIL_get_lang_msg("MSG_CHPHER_NOTSELECTED_ERROR", pvar, |
| 1230 |
"No cipher selected!"); |
"No cipher selected!"); |
| 1231 |
notify_fatal_error(pvar, pvar->ts->UIMsg); |
notify_fatal_error(pvar, pvar->ts->UIMsg); |
| 1232 |
return FALSE; |
return FALSE; |
| 1233 |
} else { |
} else { |
| 1281 |
void CRYPT_get_cipher_info(PTInstVar pvar, char FAR * dest, int len) |
void CRYPT_get_cipher_info(PTInstVar pvar, char FAR * dest, int len) |
| 1282 |
{ |
{ |
| 1283 |
UTIL_get_lang_msg("DLG_ABOUT_CIPHER_INFO", pvar, |
UTIL_get_lang_msg("DLG_ABOUT_CIPHER_INFO", pvar, |
| 1284 |
"%s to server, %s from server"); |
"%s to server, %s from server"); |
| 1285 |
_snprintf_s(dest, len, _TRUNCATE, pvar->ts->UIMsg, |
_snprintf_s(dest, len, _TRUNCATE, pvar->ts->UIMsg, |
| 1286 |
get_cipher_name(pvar->crypt_state.sender_cipher), |
get_cipher_name(pvar->crypt_state.sender_cipher), |
| 1287 |
get_cipher_name(pvar->crypt_state.receiver_cipher)); |
get_cipher_name(pvar->crypt_state.receiver_cipher)); |
| 1288 |
} |
} |
| 1289 |
|
|
| 1290 |
void CRYPT_get_server_key_info(PTInstVar pvar, char FAR * dest, int len) |
void CRYPT_get_server_key_info(PTInstVar pvar, char FAR * dest, int len) |
| 1291 |
{ |
{ |
| 1292 |
if (SSHv1(pvar)) { |
if (SSHv1(pvar)) { |
| 1293 |
if (pvar->crypt_state.server_key.RSA_key == NULL |
if (pvar->crypt_state.server_key.RSA_key == NULL |
| 1294 |
|| pvar->crypt_state.host_key.RSA_key == NULL) { |
|| pvar->crypt_state.host_key.RSA_key == NULL) { |
| 1295 |
UTIL_get_lang_msg("DLG_ABOUT_KEY_NONE", pvar, "None"); |
UTIL_get_lang_msg("DLG_ABOUT_KEY_NONE", pvar, "None"); |
| 1296 |
strncpy_s(dest, len, pvar->ts->UIMsg, _TRUNCATE); |
strncpy_s(dest, len, pvar->ts->UIMsg, _TRUNCATE); |
| 1297 |
} else { |
} else { |
| 1298 |
UTIL_get_lang_msg("DLG_ABOUT_KEY_INFO", pvar, |
UTIL_get_lang_msg("DLG_ABOUT_KEY_INFO", pvar, |
| 1299 |
"%d-bit server key, %d-bit host key"); |
"%d-bit server key, %d-bit host key"); |
| 1300 |
_snprintf_s(dest, len, _TRUNCATE, pvar->ts->UIMsg, |
_snprintf_s(dest, len, _TRUNCATE, pvar->ts->UIMsg, |
| 1301 |
BN_num_bits(pvar->crypt_state.server_key.RSA_key->n), |
BN_num_bits(pvar->crypt_state.server_key.RSA_key->n), |
| 1302 |
BN_num_bits(pvar->crypt_state.host_key.RSA_key->n)); |
BN_num_bits(pvar->crypt_state.host_key.RSA_key->n)); |
| 1303 |
} |
} |
| 1304 |
} else { // SSH2 |
} else { // SSH2 |
| 1305 |
UTIL_get_lang_msg("DLG_ABOUT_KEY_INFO", pvar, |
UTIL_get_lang_msg("DLG_ABOUT_KEY_INFO", pvar, |
| 1306 |
"%d-bit server key, %d-bit host key"); |
"%d-bit server key, %d-bit host key"); |
| 1307 |
_snprintf_s(dest, len, _TRUNCATE, pvar->ts->UIMsg, |
_snprintf_s(dest, len, _TRUNCATE, pvar->ts->UIMsg, |
| 1308 |
pvar->server_key_bits, |
pvar->server_key_bits, |
| 1309 |
pvar->client_key_bits); |
pvar->client_key_bits); |
| 1310 |
} |
} |
| 1311 |
} |
} |
| 1312 |
|
|
| 1331 |
|
|
| 1332 |
if (pvar->crypt_state.detect_attack_statics.h != NULL) { |
if (pvar->crypt_state.detect_attack_statics.h != NULL) { |
| 1333 |
memset(pvar->crypt_state.detect_attack_statics.h, 0, |
memset(pvar->crypt_state.detect_attack_statics.h, 0, |
| 1334 |
pvar->crypt_state.detect_attack_statics.n * HASH_ENTRYSIZE); |
pvar->crypt_state.detect_attack_statics.n * HASH_ENTRYSIZE); |
| 1335 |
free(pvar->crypt_state.detect_attack_statics.h); |
free(pvar->crypt_state.detect_attack_statics.h); |
| 1336 |
} |
} |
| 1337 |
|
|
| 1338 |
memset(pvar->crypt_state.sender_cipher_key, 0, |
memset(pvar->crypt_state.sender_cipher_key, 0, |
| 1339 |
sizeof(pvar->crypt_state.sender_cipher_key)); |
sizeof(pvar->crypt_state.sender_cipher_key)); |
| 1340 |
memset(pvar->crypt_state.receiver_cipher_key, 0, |
memset(pvar->crypt_state.receiver_cipher_key, 0, |
| 1341 |
sizeof(pvar->crypt_state.receiver_cipher_key)); |
sizeof(pvar->crypt_state.receiver_cipher_key)); |
| 1342 |
memset(pvar->crypt_state.server_cookie, 0, |
memset(pvar->crypt_state.server_cookie, 0, |
| 1343 |
sizeof(pvar->crypt_state.server_cookie)); |
sizeof(pvar->crypt_state.server_cookie)); |
| 1344 |
memset(pvar->crypt_state.client_cookie, 0, |
memset(pvar->crypt_state.client_cookie, 0, |
| 1345 |
sizeof(pvar->crypt_state.client_cookie)); |
sizeof(pvar->crypt_state.client_cookie)); |
| 1346 |
memset(&pvar->crypt_state.enc, 0, sizeof(pvar->crypt_state.enc)); |
memset(&pvar->crypt_state.enc, 0, sizeof(pvar->crypt_state.enc)); |
| 1347 |
memset(&pvar->crypt_state.dec, 0, sizeof(pvar->crypt_state.dec)); |
memset(&pvar->crypt_state.dec, 0, sizeof(pvar->crypt_state.dec)); |
| 1348 |
} |
} |
| 1349 |
|
|
| 1350 |
int CRYPT_passphrase_decrypt(int cipher, char FAR * passphrase, |
int CRYPT_passphrase_decrypt(int cipher, char FAR * passphrase, |
| 1351 |
char FAR * buf, int bytes) |
char FAR * buf, int bytes) |
| 1352 |
{ |
{ |
| 1353 |
unsigned char passphrase_key[16]; |
unsigned char passphrase_key[16]; |
| 1354 |
|
|
| 1359 |
Cipher3DESState state; |
Cipher3DESState state; |
| 1360 |
|
|
| 1361 |
DES_set_key((const_DES_cblock FAR *) passphrase_key, |
DES_set_key((const_DES_cblock FAR *) passphrase_key, |
| 1362 |
&state.k1); |
&state.k1); |
| 1363 |
DES_set_key((const_DES_cblock FAR *) (passphrase_key + 8), |
DES_set_key((const_DES_cblock FAR *) (passphrase_key + 8), |
| 1364 |
&state.k2); |
&state.k2); |
| 1365 |
DES_set_key((const_DES_cblock FAR *) passphrase_key, |
DES_set_key((const_DES_cblock FAR *) passphrase_key, |
| 1366 |
&state.k3); |
&state.k3); |
| 1367 |
memset(state.ivec1, 0, 8); |
memset(state.ivec1, 0, 8); |
| 1368 |
memset(state.ivec2, 0, 8); |
memset(state.ivec2, 0, 8); |
| 1369 |
memset(state.ivec3, 0, 8); |
memset(state.ivec3, 0, 8); |
| 1370 |
DES_ncbc_encrypt(buf, buf, bytes, |
DES_ncbc_encrypt(buf, buf, bytes, |
| 1371 |
&state.k3, &state.ivec3, DES_DECRYPT); |
&state.k3, &state.ivec3, DES_DECRYPT); |
| 1372 |
DES_ncbc_encrypt(buf, buf, bytes, |
DES_ncbc_encrypt(buf, buf, bytes, |
| 1373 |
&state.k2, &state.ivec2, DES_ENCRYPT); |
&state.k2, &state.ivec2, DES_ENCRYPT); |
| 1374 |
DES_ncbc_encrypt(buf, buf, bytes, |
DES_ncbc_encrypt(buf, buf, bytes, |
| 1375 |
&state.k1, &state.ivec1, DES_DECRYPT); |
&state.k1, &state.ivec1, DES_DECRYPT); |
| 1376 |
break; |
break; |
| 1377 |
} |
} |
| 1378 |
|
|
| 1382 |
|
|
| 1383 |
cIDEA_init(passphrase_key, &state); |
cIDEA_init(passphrase_key, &state); |
| 1384 |
idea_cfb64_encrypt(buf, buf, bytes, &state.k, state.ivec, |
idea_cfb64_encrypt(buf, buf, bytes, &state.k, state.ivec, |
| 1385 |
&num, IDEA_DECRYPT); |
&num, IDEA_DECRYPT); |
| 1386 |
break; |
break; |
| 1387 |
} |
} |
| 1388 |
|
|
| 1391 |
|
|
| 1392 |
cDES_init(passphrase_key, &state); |
cDES_init(passphrase_key, &state); |
| 1393 |
DES_ncbc_encrypt(buf, buf, bytes, |
DES_ncbc_encrypt(buf, buf, bytes, |
| 1394 |
&state.k, &state.ivec, DES_DECRYPT); |
&state.k, &state.ivec, DES_DECRYPT); |
| 1395 |
break; |
break; |
| 1396 |
} |
} |
| 1397 |
|
|