Skip to content

Commit ac013f0

Browse files
committed
swap to PKCS8 form
this allows storing different keys
1 parent bde153e commit ac013f0

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

modules/ssh/ssh.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ import (
5555

5656
const giteaPermissionExtensionKeyID = "gitea-perm-ext-key-id"
5757

58+
type KeyType string
59+
60+
const (
61+
RSA KeyType = "rsa"
62+
)
63+
5864
func getExitStatusFromError(err error) int {
5965
if err == nil {
6066
return 0
@@ -373,7 +379,7 @@ func Listen(host string, port int, ciphers, keyExchanges, macs []string) {
373379
log.Error("Failed to create dir %s: %v", filePath, err)
374380
}
375381

376-
err := GenKeyPair(setting.SSH.ServerHostKeys[0])
382+
err := GenKeyPair(setting.SSH.ServerHostKeys[0], RSA)
377383
if err != nil {
378384
log.Fatal("Failed to generate private key: %v", err)
379385
}
@@ -388,7 +394,6 @@ func Listen(host string, port int, ciphers, keyExchanges, macs []string) {
388394
log.Error("Failed to set Host Key. %s", err)
389395
}
390396
}
391-
392397
go func() {
393398
_, _, finished := process.GetManager().AddTypedContext(graceful.GetManager().HammerContext(), "Service: Built-in SSH server", process.SystemProcessType, true)
394399
defer finished()
@@ -399,13 +404,18 @@ func Listen(host string, port int, ciphers, keyExchanges, macs []string) {
399404
// GenKeyPair make a pair of public and private keys for SSH access.
400405
// Public key is encoded in the format for inclusion in an OpenSSH authorized_keys file.
401406
// Private Key generated is PEM encoded
402-
func GenKeyPair(keyPath string) error {
403-
privateKey, err := rsa.GenerateKey(rand.Reader, 4096)
407+
func GenKeyPair(keyPath string, keyType KeyType) error {
408+
privateKey, publicKey, err := keyGen(keyType)
404409
if err != nil {
405410
return err
406411
}
407412

408-
privateKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}
413+
privateKeyPKCS8, err := x509.MarshalPKCS8PrivateKey(privateKey)
414+
if err != nil {
415+
return err
416+
}
417+
418+
privateKeyPEM := &pem.Block{Type: "PRIVATE KEY", Bytes: privateKeyPKCS8}
409419
f, err := os.OpenFile(keyPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o600)
410420
if err != nil {
411421
return err
@@ -421,7 +431,7 @@ func GenKeyPair(keyPath string) error {
421431
}
422432

423433
// generate public key
424-
pub, err := gossh.NewPublicKey(&privateKey.PublicKey)
434+
pub, err := gossh.NewPublicKey(publicKey)
425435
if err != nil {
426436
return err
427437
}
@@ -439,3 +449,16 @@ func GenKeyPair(keyPath string) error {
439449
_, err = p.Write(public)
440450
return err
441451
}
452+
453+
func keyGen(keytype KeyType) (any, any, error) {
454+
switch keytype {
455+
case RSA:
456+
privateKey, err := rsa.GenerateKey(rand.Reader, 4096)
457+
if err != nil {
458+
return nil, nil, err
459+
}
460+
return privateKey, &privateKey.PublicKey, nil
461+
default:
462+
return nil, nil, errors.New("unknown keyType")
463+
}
464+
}

modules/ssh/ssh_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ssh_test
22

33
import (
4+
"crypto/rsa"
45
"crypto/x509"
56
"encoding/pem"
67
"io"
@@ -14,7 +15,7 @@ import (
1415

1516
func TestGenKeyPair(t *testing.T) {
1617
path := t.TempDir() + "/gitea.rsa"
17-
ssh.GenKeyPair(path)
18+
require.NoError(t, ssh.GenKeyPair(path, ssh.RSA))
1819

1920
file, err := os.Open(path)
2021
require.NoError(t, err)
@@ -24,9 +25,9 @@ func TestGenKeyPair(t *testing.T) {
2425

2526
block, _ := pem.Decode(bytes)
2627
require.NotNil(t, block)
27-
assert.Equal(t, "RSA PRIVATE KEY", block.Type)
28+
assert.Equal(t, "PRIVATE KEY", block.Type)
2829

29-
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
30+
privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
3031
require.NoError(t, err)
31-
assert.NotNil(t, privateKey)
32+
assert.IsType(t, &rsa.PrivateKey{}, privateKey)
3233
}

0 commit comments

Comments
 (0)