@@ -55,6 +55,12 @@ import (
55
55
56
56
const giteaPermissionExtensionKeyID = "gitea-perm-ext-key-id"
57
57
58
+ type KeyType string
59
+
60
+ const (
61
+ RSA KeyType = "rsa"
62
+ )
63
+
58
64
func getExitStatusFromError (err error ) int {
59
65
if err == nil {
60
66
return 0
@@ -373,7 +379,7 @@ func Listen(host string, port int, ciphers, keyExchanges, macs []string) {
373
379
log .Error ("Failed to create dir %s: %v" , filePath , err )
374
380
}
375
381
376
- err := GenKeyPair (setting .SSH .ServerHostKeys [0 ])
382
+ err := GenKeyPair (setting .SSH .ServerHostKeys [0 ], RSA )
377
383
if err != nil {
378
384
log .Fatal ("Failed to generate private key: %v" , err )
379
385
}
@@ -388,7 +394,6 @@ func Listen(host string, port int, ciphers, keyExchanges, macs []string) {
388
394
log .Error ("Failed to set Host Key. %s" , err )
389
395
}
390
396
}
391
-
392
397
go func () {
393
398
_ , _ , finished := process .GetManager ().AddTypedContext (graceful .GetManager ().HammerContext (), "Service: Built-in SSH server" , process .SystemProcessType , true )
394
399
defer finished ()
@@ -399,13 +404,18 @@ func Listen(host string, port int, ciphers, keyExchanges, macs []string) {
399
404
// GenKeyPair make a pair of public and private keys for SSH access.
400
405
// Public key is encoded in the format for inclusion in an OpenSSH authorized_keys file.
401
406
// 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 )
404
409
if err != nil {
405
410
return err
406
411
}
407
412
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 }
409
419
f , err := os .OpenFile (keyPath , os .O_RDWR | os .O_CREATE | os .O_TRUNC , 0o600 )
410
420
if err != nil {
411
421
return err
@@ -421,7 +431,7 @@ func GenKeyPair(keyPath string) error {
421
431
}
422
432
423
433
// generate public key
424
- pub , err := gossh .NewPublicKey (& privateKey . PublicKey )
434
+ pub , err := gossh .NewPublicKey (publicKey )
425
435
if err != nil {
426
436
return err
427
437
}
@@ -439,3 +449,16 @@ func GenKeyPair(keyPath string) error {
439
449
_ , err = p .Write (public )
440
450
return err
441
451
}
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
+ }
0 commit comments