Skip to content

Commit 8d1d004

Browse files
committed
ssh: restore support for non-RSA host keys
As I had before upstream refactored the SSH server. By default look for OpenSSH-style ssh_host_*_key paths, falling back to gogs.rsa if none were found. If no keys at all are found, generate gogs.rsa.
1 parent 64352ae commit 8d1d004

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

modules/ssh/ssh.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,24 @@ func Listen(host string, port int, ciphers []string, keyExchanges []string, macs
163163
},
164164
}
165165

166-
keyPath := filepath.Join(setting.AppDataPath, "ssh/gogs.rsa")
167-
if !com.IsExist(keyPath) {
166+
// look for all supported ssh_host_*_key formats
167+
keyFiles := make([]string, 0, 1)
168+
for _, keyType := range [...]string{"rsa", "dsa", "ecdsa", "ed25519"} {
169+
keyPath := filepath.Join(setting.AppDataPath, "ssh/ssh_host_"+keyType+"_key")
170+
if com.IsExist(keyPath) {
171+
keyFiles = append(keyFiles, keyPath)
172+
}
173+
}
174+
175+
// also check for legacy gogs.rsa, only if no openssh-named keys were found
176+
oldKeyFile := filepath.Join(setting.AppDataPath, "ssh/gogs.rsa")
177+
if len(keyFiles) == 0 && com.IsExist(oldKeyFile) {
178+
keyFiles = append(keyFiles, oldKeyFile)
179+
}
180+
181+
// if no keys found, create an RSA key
182+
if len(keyFiles) == 0 {
183+
keyPath := filepath.Join(setting.AppDataPath, "ssh/ssh_host_rsa_key")
168184
filePath := filepath.Dir(keyPath)
169185

170186
if err := os.MkdirAll(filePath, os.ModePerm); err != nil {
@@ -175,12 +191,17 @@ func Listen(host string, port int, ciphers []string, keyExchanges []string, macs
175191
if err != nil {
176192
log.Fatal("Failed to generate private key: %v", err)
177193
}
178-
log.Trace("New private key is generated: %s", keyPath)
194+
log.Trace("SSH: New private key is generateed: %s", keyPath)
195+
keyFiles = append(keyFiles, keyPath)
179196
}
180197

181-
err := srv.SetOption(ssh.HostKeyFile(keyPath))
182-
if err != nil {
183-
log.Error("Failed to set Host Key. %s", err)
198+
for _, keyPath := range keyFiles {
199+
err := srv.SetOption(ssh.HostKeyFile(keyPath))
200+
if err != nil {
201+
log.Error("Failed to set SSH Host Key %s: %s", keyPath, err)
202+
} else {
203+
log.Trace("SSH: loaded host key %s", keyPath)
204+
}
184205
}
185206

186207
go func() {

0 commit comments

Comments
 (0)