Skip to content

Commit 9de2cce

Browse files
committed
reuse generate module
1 parent 1ef0194 commit 9de2cce

File tree

1 file changed

+2
-34
lines changed

1 file changed

+2
-34
lines changed

modules/ssh/ssh.go

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ package ssh
66
import (
77
"bytes"
88
"context"
9-
"crypto/ecdsa"
10-
"crypto/ed25519"
11-
"crypto/elliptic"
12-
"crypto/rand"
13-
"crypto/rsa"
14-
"crypto/x509"
159
"encoding/pem"
1610
"errors"
1711
"fmt"
@@ -27,6 +21,7 @@ import (
2721
"syscall"
2822

2923
asymkey_model "code.gitea.io/gitea/models/asymkey"
24+
"code.gitea.io/gitea/modules/generate"
3025
"code.gitea.io/gitea/modules/graceful"
3126
"code.gitea.io/gitea/modules/log"
3227
"code.gitea.io/gitea/modules/process"
@@ -411,17 +406,11 @@ func Listen(host string, port int, ciphers, keyExchanges, macs []string) {
411406
// Public key is encoded in the format for inclusion in an OpenSSH authorized_keys file.
412407
// Private Key generated is PEM encoded
413408
func GenKeyPair(keyPath string) error {
414-
privateKey, publicKey, err := keyGen(filepath.Ext(keyPath))
409+
publicKey, privateKeyPEM, err := generate.NewSSHKey("rsa", 4096)
415410
if err != nil {
416411
return err
417412
}
418413

419-
privateKeyPKCS8, err := x509.MarshalPKCS8PrivateKey(privateKey)
420-
if err != nil {
421-
return err
422-
}
423-
424-
privateKeyPEM := &pem.Block{Type: "PRIVATE KEY", Bytes: privateKeyPKCS8}
425414
f, err := os.OpenFile(keyPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o600)
426415
if err != nil {
427416
return err
@@ -455,24 +444,3 @@ func GenKeyPair(keyPath string) error {
455444
_, err = p.Write(public)
456445
return err
457446
}
458-
459-
func keyGen(keytype string) (any, any, error) {
460-
switch keytype {
461-
case ".rsa":
462-
privateKey, err := rsa.GenerateKey(rand.Reader, 4096)
463-
if err != nil {
464-
return nil, nil, err
465-
}
466-
return privateKey, &privateKey.PublicKey, nil
467-
case ".ed25519":
468-
pub, priv, err := ed25519.GenerateKey(rand.Reader)
469-
return priv, pub, err
470-
default:
471-
// case ".ecdsa":
472-
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
473-
if err != nil {
474-
return nil, nil, err
475-
}
476-
return priv, &priv.PublicKey, nil
477-
}
478-
}

0 commit comments

Comments
 (0)