| 632 | } |
| 633 | |
| 634 | int FTPClientWrapperSSH::verify_knownhost(ssh_session session) { |
| 635 | int state, rc; |
| 636 | int result = -1; |
| 637 | unsigned char *hash = NULL; |
| 638 | char * hashHex; |
| 639 | size_t hlen; |
| 640 | ssh_key srv_pubkey; |
| 641 | TCHAR errMessage[512]; |
| 642 | |
| 643 | state = ssh_session_is_known_server(session); |
| 644 | |
| 645 | rc = ssh_get_server_publickey(session, &srv_pubkey); |
| 646 | if (rc < 0) |
| 647 | return -1; |
| 648 | |
| 649 | const char *keytype = ssh_key_type_to_char(ssh_key_type(srv_pubkey)); |
| 650 | |
| 651 | rc = ssh_get_publickey_hash(srv_pubkey, SSH_PUBLICKEY_HASH_SHA1, &hash, &hlen); |
| 652 | ssh_key_free(srv_pubkey); |
| 653 | if (rc < 0) |
| 654 | return -1; |
| 655 | |
| 656 | hashHex = ssh_get_hexa(hash, hlen); |
| 657 | |
| 658 | bool askSavekey = false; |
| 659 | |
| 660 | switch(state){ |
| 661 | case SSH_SERVER_KNOWN_OK: |
| 662 | OutMsg("[SFTP] Host key accepted"); |
| 663 | result = 0; |
| 664 | break; /* ok */ |
| 665 | case SSH_SERVER_FILE_NOT_FOUND: |
| 666 | OutMsg("[SFTP] Creating known hosts file."); |
| 667 | /* fallback to SSH_SERVER_NOT_KNOWN behavior */ |
| 668 | case SSH_SERVER_NOT_KNOWN: { |
| 669 | SU::TSprintf(errMessage, 512, TEXT("The server is unknown. Do you trust the host key\r\n%s %s ?"), keytype, hashHex); |
| 670 | askSavekey = true; |
| 671 | break; } |
| 672 | case SSH_SERVER_KNOWN_CHANGED: { |
| 673 | SU::TSprintf(errMessage, 512, TEXT("The host key had changed, and is now: %s %s\r\nDo you trust this new host key?"), keytype, hashHex); |
| 674 | askSavekey = true; |
| 675 | break; } |
| 676 | case SSH_SERVER_FOUND_OTHER: |
| 677 | SU::TSprintf(errMessage, 512, TEXT("A different type of host key was returned by the server than that was stored, and now reads: %s %s\r\nDo you trust this different host key?"), keytype, hashHex); |
| 678 | askSavekey = true; |
| 679 | break; |
| 680 | case SSH_SERVER_ERROR: |
| 681 | default: |
| 682 | OutErr("[SFTP] SSH_SERVER_ERROR: %s",ssh_get_error(session)); |
| 683 | result = -1; |
| 684 | break; |
| 685 | } |
| 686 | |
| 687 | if (askSavekey) { |
| 688 | int res = ::MessageBox(_MainOutputWindow, errMessage, TEXT("SFTP authentication"), MB_YESNO|MB_DEFBUTTON2); |
| 689 | if (res == IDYES) { |
| 690 | if (ssh_session_update_known_hosts(session) < 0) { |
| 691 | OutErr("[SFTP] Writing known hosts file failed: %s", strerror(errno)); |
nothing calls this directly
no outgoing calls
no test coverage detected