Skip to content

Commit

Permalink
Check hostkey type when validating hostkey
Browse files Browse the repository at this point in the history
Signed-off-by: Philip Laine <philip.laine@gmail.com>
  • Loading branch information
phillebaba committed Feb 11, 2021
1 parent c32d11c commit e92a852
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions pkg/git/libgit2/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ package libgit2
import (
"bufio"
"bytes"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/x509"
"fmt"
"net/url"
Expand Down Expand Up @@ -135,7 +137,7 @@ func (s *PublicKeyAuth) Method(secret corev1.Secret) (*git.Auth, error) {
}
certCallback := func(cert *git2go.Certificate, valid bool, hostname string) git2go.ErrorCode {
for _, k := range kk {
if k.matches(hostname, cert.Hostkey.HashSHA1[:]) {
if k.matches(hostname, cert.Hostkey) {
return git2go.ErrOk
}
}
Expand Down Expand Up @@ -173,13 +175,28 @@ func parseKnownHosts(s string) ([]knownKey, error) {
return knownHosts, nil
}

func (k knownKey) matches(host string, key []byte) bool {
func (k knownKey) matches(host string, hostkey git2go.HostkeyCertificate) bool {
if !containsHost(k.hosts, host) {
return false
}

hash := sha1.Sum([]byte(k.key.Marshal()))
if bytes.Compare(hash[:], key) != 0 {
var hash []byte
var key []byte
switch hostkey.Kind {
case git2go.HostkeyMD5:
sum := md5.Sum(k.key.Marshal())
hash = sum[:]
key = hostkey.HashMD5[:]
case git2go.HostkeySHA1:
sum := sha1.Sum(k.key.Marshal())
hash = sum[:]
key = hostkey.HashSHA1[:]
case git2go.HostkeySHA256:
sum := sha256.Sum256(k.key.Marshal())
hash = sum[:]
key = hostkey.HashSHA256[:]
}
if bytes.Compare(hash, key) != 0 {
return false
}

Expand Down

0 comments on commit e92a852

Please sign in to comment.