-
Notifications
You must be signed in to change notification settings - Fork 59
/
keygen.go
110 lines (89 loc) · 2.55 KB
/
keygen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package sshd
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"github.com/TNK-Studio/gortal/utils"
"github.com/TNK-Studio/gortal/utils/logger"
"golang.org/x/crypto/ssh"
)
// GenKey gen ssh private and public key
func GenKey(keyFilePath string) (string, string, error) {
savePrivateFileTo := utils.FilePath(keyFilePath)
savePublicFileTo := fmt.Sprintf("%s.pub", savePrivateFileTo)
bitSize := 4096
privateKey, err := generatePrivateKey(bitSize)
if err != nil {
logger.Logger.Error(err.Error())
return "", "", err
}
publicKeyBytes, err := generatePublicKey(&privateKey.PublicKey)
if err != nil {
logger.Logger.Error(err.Error())
return "", "", err
}
privateKeyBytes := encodePrivateKeyToPEM(privateKey)
err = writeKeyToFile(privateKeyBytes, savePrivateFileTo)
if err != nil {
logger.Logger.Error(err.Error())
return "", "", err
}
err = writeKeyToFile([]byte(publicKeyBytes), savePublicFileTo)
if err != nil {
logger.Logger.Error(err.Error())
return "", "", err
}
return savePrivateFileTo, savePublicFileTo, nil
}
// generatePrivateKey creates a RSA Private Key of specified byte size
func generatePrivateKey(bitSize int) (*rsa.PrivateKey, error) {
// Private Key generation
privateKey, err := rsa.GenerateKey(rand.Reader, bitSize)
if err != nil {
return nil, err
}
// Validate Private Key
err = privateKey.Validate()
if err != nil {
return nil, err
}
logger.Logger.Info("Private Key generated")
return privateKey, nil
}
// encodePrivateKeyToPEM encodes Private Key from RSA to PEM format
func encodePrivateKeyToPEM(privateKey *rsa.PrivateKey) []byte {
// Get ASN.1 DER format
privDER := x509.MarshalPKCS1PrivateKey(privateKey)
// pem.Block
privBlock := pem.Block{
Type: "RSA PRIVATE KEY",
Headers: nil,
Bytes: privDER,
}
// Private key in PEM format
privatePEM := pem.EncodeToMemory(&privBlock)
return privatePEM
}
// generatePublicKey take a rsa.PublicKey and return bytes suitable for writing to .pub file
// returns in the format "ssh-rsa ..."
func generatePublicKey(privatekey *rsa.PublicKey) ([]byte, error) {
publicRsaKey, err := ssh.NewPublicKey(privatekey)
if err != nil {
return nil, err
}
pubKeyBytes := ssh.MarshalAuthorizedKey(publicRsaKey)
logger.Logger.Info("Public key generated")
return pubKeyBytes, nil
}
// writePemToFile writes keys to a file
func writeKeyToFile(keyBytes []byte, saveFileTo string) error {
err := ioutil.WriteFile(saveFileTo, keyBytes, 0600)
if err != nil {
return err
}
logger.Logger.Info("Key saved to: %s", saveFileTo)
return nil
}