|
81 | 81 | #include "select.h"
|
82 | 82 | #include "warnless.h"
|
83 | 83 | #include "curl_path.h"
|
| 84 | +#include "strcase.h" |
| 85 | + |
| 86 | +#include <curl_base64.h> /* for base64 encoding/decoding */ |
| 87 | +#include <curl_sha256.h> |
| 88 | + |
84 | 89 |
|
85 | 90 | /* The last 3 #include files should be in this order */
|
86 | 91 | #include "curl_printf.h"
|
@@ -615,40 +620,142 @@ static CURLcode ssh_check_fingerprint(struct Curl_easy *data)
|
615 | 620 | struct connectdata *conn = data->conn;
|
616 | 621 | struct ssh_conn *sshc = &conn->proto.sshc;
|
617 | 622 | const char *pubkey_md5 = data->set.str[STRING_SSH_HOST_PUBLIC_KEY_MD5];
|
618 |
| - char md5buffer[33]; |
| 623 | + const char *pubkey_sha256 = data->set.str[STRING_SSH_HOST_PUBLIC_KEY_SHA256]; |
| 624 | + |
| 625 | + infof(data, "SSH MD5 public key: %s", |
| 626 | + pubkey_md5 != NULL ? pubkey_md5 : "NULL"); |
| 627 | + infof(data, "SSH SHA256 public key: %s", |
| 628 | + pubkey_sha256 != NULL ? pubkey_sha256 : "NULL"); |
619 | 629 |
|
620 |
| - const char *fingerprint = libssh2_hostkey_hash(sshc->ssh_session, |
621 |
| - LIBSSH2_HOSTKEY_HASH_MD5); |
| 630 | + if(pubkey_sha256) { |
| 631 | + const char *fingerprint = NULL; |
| 632 | + char *fingerprint_b64 = NULL; |
| 633 | + size_t fingerprint_b64_len; |
| 634 | + size_t pub_pos = 0; |
| 635 | + size_t b64_pos = 0; |
622 | 636 |
|
623 |
| - if(fingerprint) { |
| 637 | +#ifdef LIBSSH2_HOSTKEY_HASH_SHA256 |
624 | 638 | /* The fingerprint points to static storage (!), don't free() it. */
|
625 |
| - int i; |
626 |
| - for(i = 0; i < 16; i++) |
627 |
| - msnprintf(&md5buffer[i*2], 3, "%02x", (unsigned char) fingerprint[i]); |
628 |
| - infof(data, "SSH MD5 fingerprint: %s", md5buffer); |
629 |
| - } |
| 639 | + fingerprint = libssh2_hostkey_hash(sshc->ssh_session, |
| 640 | + LIBSSH2_HOSTKEY_HASH_SHA256); |
| 641 | +#else |
| 642 | + const char *hostkey; |
| 643 | + size_t len = 0; |
| 644 | + unsigned char hash[32]; |
| 645 | + |
| 646 | + hostkey = libssh2_session_hostkey(sshc->ssh_session, &len, NULL); |
| 647 | + if(hostkey) { |
| 648 | + Curl_sha256it(hash, (const unsigned char *) hostkey, len); |
| 649 | + fingerprint = (char *) hash; |
| 650 | + } |
| 651 | +#endif |
630 | 652 |
|
631 |
| - /* Before we authenticate we check the hostkey's MD5 fingerprint |
632 |
| - * against a known fingerprint, if available. |
633 |
| - */ |
634 |
| - if(pubkey_md5 && strlen(pubkey_md5) == 32) { |
635 |
| - if(!fingerprint || !strcasecompare(md5buffer, pubkey_md5)) { |
636 |
| - if(fingerprint) |
637 |
| - failf(data, |
638 |
| - "Denied establishing ssh session: mismatch md5 fingerprint. " |
639 |
| - "Remote %s is not equal to %s", md5buffer, pubkey_md5); |
640 |
| - else |
641 |
| - failf(data, |
642 |
| - "Denied establishing ssh session: md5 fingerprint not available"); |
| 653 | + if(!fingerprint) { |
| 654 | + failf(data, |
| 655 | + "Denied establishing ssh session: sha256 fingerprint " |
| 656 | + "not available"); |
| 657 | + state(data, SSH_SESSION_FREE); |
| 658 | + sshc->actualcode = CURLE_PEER_FAILED_VERIFICATION; |
| 659 | + return sshc->actualcode; |
| 660 | + } |
| 661 | + |
| 662 | + /* The length of fingerprint is 32 bytes for SHA256. |
| 663 | + * See libssh2_hostkey_hash documentation. */ |
| 664 | + if(Curl_base64_encode (data, fingerprint, 32, &fingerprint_b64, |
| 665 | + &fingerprint_b64_len) != CURLE_OK) { |
| 666 | + state(data, SSH_SESSION_FREE); |
| 667 | + sshc->actualcode = CURLE_PEER_FAILED_VERIFICATION; |
| 668 | + return sshc->actualcode; |
| 669 | + } |
| 670 | + |
| 671 | + if(!fingerprint_b64) { |
| 672 | + failf(data, |
| 673 | + "sha256 fingerprint could not be encoded"); |
| 674 | + state(data, SSH_SESSION_FREE); |
| 675 | + sshc->actualcode = CURLE_PEER_FAILED_VERIFICATION; |
| 676 | + return sshc->actualcode; |
| 677 | + } |
| 678 | + |
| 679 | + infof(data, "SSH SHA256 fingerprint: %s", fingerprint_b64); |
| 680 | + |
| 681 | + /* Find the position of any = padding characters in the public key */ |
| 682 | + while((pubkey_sha256[pub_pos] != '=') && pubkey_sha256[pub_pos]) { |
| 683 | + pub_pos++; |
| 684 | + } |
| 685 | + |
| 686 | + /* Find the position of any = padding characters in the base64 coded |
| 687 | + * hostkey fingerprint */ |
| 688 | + while((fingerprint_b64[b64_pos] != '=') && fingerprint_b64[b64_pos]) { |
| 689 | + b64_pos++; |
| 690 | + } |
| 691 | + |
| 692 | + /* Before we authenticate we check the hostkey's sha256 fingerprint |
| 693 | + * against a known fingerprint, if available. |
| 694 | + */ |
| 695 | + if((pub_pos != b64_pos) || |
| 696 | + Curl_strncasecompare(fingerprint_b64, pubkey_sha256, pub_pos) != 1) { |
| 697 | + free(fingerprint_b64); |
| 698 | + |
| 699 | + failf(data, |
| 700 | + "Denied establishing ssh session: mismatch sha256 fingerprint. " |
| 701 | + "Remote %s is not equal to %s", fingerprint, pubkey_sha256); |
643 | 702 | state(data, SSH_SESSION_FREE);
|
644 | 703 | sshc->actualcode = CURLE_PEER_FAILED_VERIFICATION;
|
645 | 704 | return sshc->actualcode;
|
646 | 705 | }
|
647 |
| - infof(data, "MD5 checksum match!"); |
| 706 | + |
| 707 | + free(fingerprint_b64); |
| 708 | + |
| 709 | + infof(data, "SHA256 checksum match!"); |
| 710 | + } |
| 711 | + |
| 712 | + if(pubkey_md5) { |
| 713 | + char md5buffer[33]; |
| 714 | + const char *fingerprint = NULL; |
| 715 | + |
| 716 | + fingerprint = libssh2_hostkey_hash(sshc->ssh_session, |
| 717 | + LIBSSH2_HOSTKEY_HASH_MD5); |
| 718 | + |
| 719 | + if(fingerprint) { |
| 720 | + /* The fingerprint points to static storage (!), don't free() it. */ |
| 721 | + int i; |
| 722 | + for(i = 0; i < 16; i++) { |
| 723 | + msnprintf(&md5buffer[i*2], 3, "%02x", (unsigned char) fingerprint[i]); |
| 724 | + } |
| 725 | + |
| 726 | + infof(data, "SSH MD5 fingerprint: %s", md5buffer); |
| 727 | + } |
| 728 | + |
| 729 | + /* Before we authenticate we check the hostkey's MD5 fingerprint |
| 730 | + * against a known fingerprint, if available. |
| 731 | + */ |
| 732 | + if(pubkey_md5 && strlen(pubkey_md5) == 32) { |
| 733 | + if(!fingerprint || !strcasecompare(md5buffer, pubkey_md5)) { |
| 734 | + if(fingerprint) { |
| 735 | + failf(data, |
| 736 | + "Denied establishing ssh session: mismatch md5 fingerprint. " |
| 737 | + "Remote %s is not equal to %s", md5buffer, pubkey_md5); |
| 738 | + } |
| 739 | + else { |
| 740 | + failf(data, |
| 741 | + "Denied establishing ssh session: md5 fingerprint " |
| 742 | + "not available"); |
| 743 | + } |
| 744 | + state(data, SSH_SESSION_FREE); |
| 745 | + sshc->actualcode = CURLE_PEER_FAILED_VERIFICATION; |
| 746 | + return sshc->actualcode; |
| 747 | + } |
| 748 | + infof(data, "MD5 checksum match!"); |
| 749 | + } |
| 750 | + } |
| 751 | + |
| 752 | + if(!pubkey_md5 && !pubkey_sha256) { |
| 753 | + return ssh_knownhost(data); |
| 754 | + } |
| 755 | + else { |
648 | 756 | /* as we already matched, we skip the check for known hosts */
|
649 | 757 | return CURLE_OK;
|
650 | 758 | }
|
651 |
| - return ssh_knownhost(data); |
652 | 759 | }
|
653 | 760 |
|
654 | 761 | /*
|
|
0 commit comments