Skip to content

Commit 23b8e99

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

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

pkg/git/libgit2/transport.go

+22-4
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ package libgit2
1919
import (
2020
"bufio"
2121
"bytes"
22+
"crypto/md5"
2223
"crypto/sha1"
24+
"crypto/sha256"
2325
"crypto/x509"
2426
"fmt"
27+
"hash"
2528
"net"
2629
"net/url"
2730
"strings"
@@ -157,7 +160,7 @@ func (s *PublicKeyAuth) Method(secret corev1.Secret) (*git.Auth, error) {
157160
// is an entry for the hostname _and_ port.
158161
host = knownhosts.Normalize(s.host)
159162
for _, k := range kk {
160-
if k.matches(host, cert.Hostkey.HashSHA1[:]) {
163+
if k.matches(host, cert.Hostkey) {
161164
return git2go.ErrOk
162165
}
163166
}
@@ -195,13 +198,28 @@ func parseKnownHosts(s string) ([]knownKey, error) {
195198
return knownHosts, nil
196199
}
197200

198-
func (k knownKey) matches(host string, key []byte) bool {
201+
func (k knownKey) matches(host string, hostkey git2go.HostkeyCertificate) bool {
199202
if !containsHost(k.hosts, host) {
200203
return false
201204
}
202205

203-
hash := sha1.Sum(k.key.Marshal())
204-
if bytes.Compare(hash[:], key) != 0 {
206+
var fingerprint []byte
207+
var hasher hash.Hash
208+
switch hostkey.Kind {
209+
case git2go.HostkeyMD5:
210+
fingerprint = hostkey.HashMD5[:]
211+
hasher = md5.New()
212+
case git2go.HostkeySHA1:
213+
fingerprint = hostkey.HashSHA1[:]
214+
hasher = sha1.New()
215+
case git2go.HostkeySHA256:
216+
fingerprint = hostkey.HashSHA256[:]
217+
hasher = sha256.New()
218+
default:
219+
return false
220+
}
221+
hasher.Write(k.key.Marshal())
222+
if bytes.Compare(hasher.Sum(nil), fingerprint) != 0 {
205223
return false
206224
}
207225

0 commit comments

Comments
 (0)