Skip to content

Commit e73fcfc

Browse files
authored
Merge pull request skeema#6 from trzsz/main
HostKeyAlgorithms: add rsa-sha2-256 and rsa-sha2-512 for ssh-rsa
2 parents f2b518c + bd8e67e commit e73fcfc

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

knownhosts.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,23 @@ func (hkcb HostKeyCallback) HostKeyAlgorithms(hostWithPort string) (algos []stri
7676
// example by https://github.com/golang/crypto/pull/254.
7777
hostKeys := hkcb.HostKeys(hostWithPort)
7878
seen := make(map[string]struct{}, len(hostKeys))
79-
for _, key := range hostKeys {
80-
typ := key.Type()
79+
addAlgo := func(typ string) {
8180
if _, already := seen[typ]; !already {
8281
algos = append(algos, typ)
8382
seen[typ] = struct{}{}
8483
}
8584
}
85+
for _, key := range hostKeys {
86+
typ := key.Type()
87+
if typ == ssh.KeyAlgoRSA {
88+
// KeyAlgoRSASHA256 and KeyAlgoRSASHA512 are only public key algorithms,
89+
// not public key formats, so they can't appear as a PublicKey.Type.
90+
// The corresponding PublicKey.Type is KeyAlgoRSA. See RFC 8332, Section 2.
91+
addAlgo(ssh.KeyAlgoRSASHA512)
92+
addAlgo(ssh.KeyAlgoRSASHA256)
93+
}
94+
addAlgo(typ)
95+
}
8696
return algos
8797
}
8898

knownhosts_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ func TestHostKeyAlgorithms(t *testing.T) {
4242
}
4343

4444
expectedAlgorithms := map[string][]string{
45-
"only-rsa.example.test:22": {"ssh-rsa"},
45+
"only-rsa.example.test:22": {"rsa-sha2-512", "rsa-sha2-256", "ssh-rsa"},
4646
"only-ecdsa.example.test:22": {"ecdsa-sha2-nistp256"},
4747
"only-ed25519.example.test:22": {"ssh-ed25519"},
48-
"multi.example.test:2233": {"ssh-rsa", "ecdsa-sha2-nistp256", "ssh-ed25519"},
48+
"multi.example.test:2233": {"rsa-sha2-512", "rsa-sha2-256", "ssh-rsa", "ecdsa-sha2-nistp256", "ssh-ed25519"},
4949
"192.168.1.102:2222": {"ecdsa-sha2-nistp256", "ssh-ed25519"},
5050
"unknown-host.example.test": {}, // host not in file
5151
"multi.example.test:22": {}, // different port than entry in file

0 commit comments

Comments
 (0)