Skip to content

Commit 70fac49

Browse files
authored
Add sha256 support (#3306)
1 parent 5a988bc commit 70fac49

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

util/clissh/ssh.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/sha1"
66
"crypto/sha256"
77
"encoding/base64"
8+
"encoding/hex"
89
"errors"
910
"fmt"
1011
"io"
@@ -28,6 +29,7 @@ const (
2829
md5FingerprintLength = 47 // inclusive of space between bytes
2930
hexSha1FingerprintLength = 59 // inclusive of space between bytes
3031
base64Sha256FingerprintLength = 43
32+
sha256FingerprintLength = 64
3133

3234
DefaultKeepAliveInterval = 30 * time.Second
3335
)
@@ -331,9 +333,12 @@ func (c *SecureShell) terminalType() string {
331333
return term
332334
}
333335

334-
func base64Sha256Fingerprint(key ssh.PublicKey) string {
336+
func sha256Fingerprint(key ssh.PublicKey, encode bool) string {
335337
sum := sha256.Sum256(key.Marshal())
336-
return base64.RawStdEncoding.EncodeToString(sum[:])
338+
if encode {
339+
return base64.RawStdEncoding.EncodeToString(sum[:])
340+
}
341+
return hex.EncodeToString(sum[:])
337342
}
338343

339344
func copyAndClose(wg *sync.WaitGroup, dest io.WriteCloser, src io.Reader) {
@@ -364,8 +369,10 @@ func fingerprintCallback(skipHostValidation bool, expectedFingerprint string) ss
364369
var fingerprint string
365370

366371
switch len(expectedFingerprint) {
372+
case sha256FingerprintLength:
373+
fingerprint = sha256Fingerprint(key, false)
367374
case base64Sha256FingerprintLength:
368-
fingerprint = base64Sha256Fingerprint(key)
375+
fingerprint = sha256Fingerprint(key, true)
369376
case hexSha1FingerprintLength:
370377
fingerprint = hexSha1Fingerprint(key)
371378
case md5FingerprintLength:

util/clissh/ssh_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,28 @@ var _ = Describe("CLI SSH", Serial, FlakeAttempts(9), func() {
253253
})
254254
})
255255

256+
Context("when the sha256 fingerprint matches", func() {
257+
BeforeEach(func() {
258+
sshEndpointFingerprint = "b29fe3acbba3ebaafecab2c350a65d254e6d773b789aafd469288d063a60afef"
259+
})
260+
261+
It("does not return an error", func() {
262+
Expect(callback("", addr, TestHostKey.PublicKey())).ToNot(HaveOccurred())
263+
})
264+
})
265+
266+
When("the SHA256 fingerprint does not match", func() {
267+
BeforeEach(func() {
268+
sshEndpointFingerprint = "0000000000000000000000000000000000000000000000000000000000000000"
269+
})
270+
271+
It("returns an error'", func() {
272+
err := callback("", addr, TestHostKey.PublicKey())
273+
Expect(err).To(MatchError(MatchRegexp(`Host key verification failed\.`)))
274+
Expect(err).To(MatchError(MatchRegexp("The fingerprint of the received key was \".*\"")))
275+
})
276+
})
277+
256278
When("the base64 SHA256 fingerprint does not match", func() {
257279
BeforeEach(func() {
258280
sshEndpointFingerprint = "0000000000000000000000000000000000000000000"

0 commit comments

Comments
 (0)