@@ -6,12 +6,6 @@ package ssh
6
6
import (
7
7
"bytes"
8
8
"context"
9
- "crypto/ecdsa"
10
- "crypto/ed25519"
11
- "crypto/elliptic"
12
- "crypto/rand"
13
- "crypto/rsa"
14
- "crypto/x509"
15
9
"encoding/pem"
16
10
"errors"
17
11
"fmt"
@@ -27,6 +21,7 @@ import (
27
21
"syscall"
28
22
29
23
asymkey_model "code.gitea.io/gitea/models/asymkey"
24
+ "code.gitea.io/gitea/modules/generate"
30
25
"code.gitea.io/gitea/modules/graceful"
31
26
"code.gitea.io/gitea/modules/log"
32
27
"code.gitea.io/gitea/modules/process"
@@ -411,17 +406,11 @@ func Listen(host string, port int, ciphers, keyExchanges, macs []string) {
411
406
// Public key is encoded in the format for inclusion in an OpenSSH authorized_keys file.
412
407
// Private Key generated is PEM encoded
413
408
func GenKeyPair (keyPath string ) error {
414
- privateKey , publicKey , err := keyGen ( filepath . Ext ( keyPath ) )
409
+ publicKey , privateKeyPEM , err := generate . NewSSHKey ( "rsa" , 4096 )
415
410
if err != nil {
416
411
return err
417
412
}
418
413
419
- privateKeyPKCS8 , err := x509 .MarshalPKCS8PrivateKey (privateKey )
420
- if err != nil {
421
- return err
422
- }
423
-
424
- privateKeyPEM := & pem.Block {Type : "PRIVATE KEY" , Bytes : privateKeyPKCS8 }
425
414
f , err := os .OpenFile (keyPath , os .O_RDWR | os .O_CREATE | os .O_TRUNC , 0o600 )
426
415
if err != nil {
427
416
return err
@@ -455,24 +444,3 @@ func GenKeyPair(keyPath string) error {
455
444
_ , err = p .Write (public )
456
445
return err
457
446
}
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