Skip to content

Commit e92a852

Browse files
committed
Check hostkey type when validating hostkey
Signed-off-by: Philip Laine <philip.laine@gmail.com>
1 parent c32d11c commit e92a852

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

pkg/git/libgit2/transport.go

+21-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ package libgit2
1919
import (
2020
"bufio"
2121
"bytes"
22+
"crypto/md5"
2223
"crypto/sha1"
24+
"crypto/sha256"
2325
"crypto/x509"
2426
"fmt"
2527
"net/url"
@@ -135,7 +137,7 @@ func (s *PublicKeyAuth) Method(secret corev1.Secret) (*git.Auth, error) {
135137
}
136138
certCallback := func(cert *git2go.Certificate, valid bool, hostname string) git2go.ErrorCode {
137139
for _, k := range kk {
138-
if k.matches(hostname, cert.Hostkey.HashSHA1[:]) {
140+
if k.matches(hostname, cert.Hostkey) {
139141
return git2go.ErrOk
140142
}
141143
}
@@ -173,13 +175,28 @@ func parseKnownHosts(s string) ([]knownKey, error) {
173175
return knownHosts, nil
174176
}
175177

176-
func (k knownKey) matches(host string, key []byte) bool {
178+
func (k knownKey) matches(host string, hostkey git2go.HostkeyCertificate) bool {
177179
if !containsHost(k.hosts, host) {
178180
return false
179181
}
180182

181-
hash := sha1.Sum([]byte(k.key.Marshal()))
182-
if bytes.Compare(hash[:], key) != 0 {
183+
var hash []byte
184+
var key []byte
185+
switch hostkey.Kind {
186+
case git2go.HostkeyMD5:
187+
sum := md5.Sum(k.key.Marshal())
188+
hash = sum[:]
189+
key = hostkey.HashMD5[:]
190+
case git2go.HostkeySHA1:
191+
sum := sha1.Sum(k.key.Marshal())
192+
hash = sum[:]
193+
key = hostkey.HashSHA1[:]
194+
case git2go.HostkeySHA256:
195+
sum := sha256.Sum256(k.key.Marshal())
196+
hash = sum[:]
197+
key = hostkey.HashSHA256[:]
198+
}
199+
if bytes.Compare(hash, key) != 0 {
183200
return false
184201
}
185202

0 commit comments

Comments
 (0)