-
Notifications
You must be signed in to change notification settings - Fork 63
/
sshKeyGenerator.go
130 lines (103 loc) · 2.6 KB
/
sshKeyGenerator.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package util
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"github.com/youmark/pkcs8"
"log"
"strings"
"golang.org/x/crypto/ssh"
)
const (
RsaPrivKeyType = "RSA PRIVATE KEY"
)
func generatePrivKey(bitSize int) (*rsa.PrivateKey, error) {
privKey, err := rsa.GenerateKey(rand.Reader, bitSize)
if err != nil {
return nil, err
}
err = privKey.Validate()
if err != nil {
return nil, err
}
log.Println("Private Key was generated")
return privKey, nil
}
func encodePrivKeyToPEM(privateKey *rsa.PrivateKey, keyPassword string, format ...string) ([]byte, error) {
var err error
var privBlock *pem.Block
var privDER []byte
if keyPassword != "" {
if format[0] == LegacyPem {
privDER := x509.MarshalPKCS1PrivateKey(privateKey)
privBlock, err = X509EncryptPEMBlock(rand.Reader, RsaPrivKeyType, privDER, []byte(keyPassword), PEMCipherDES)
if err != nil {
return nil, err
}
} else {
privDER, err = pkcs8.MarshalPrivateKey(privateKey, []byte(keyPassword), nil)
if err != nil {
return nil, err
}
privBlock = &pem.Block{
Type: "ENCRYPTED PRIVATE KEY",
Headers: nil,
Bytes: privDER,
}
}
} else {
if format[0] == LegacyPem {
privDER = x509.MarshalPKCS1PrivateKey(privateKey)
privBlock = &pem.Block{
Type: RsaPrivKeyType,
Headers: nil,
Bytes: privDER,
}
} else {
privDER, err := pkcs8.MarshalPrivateKey(privateKey, nil, nil)
if err != nil {
return nil, err
}
privBlock = &pem.Block{
Type: "PRIVATE KEY",
Headers: nil,
Bytes: privDER,
}
}
}
privatePEM := pem.EncodeToMemory(privBlock)
return privatePEM, nil
}
func generatePublicKey(key *rsa.PublicKey) ([]byte, error) {
publicRsaKey, err := ssh.NewPublicKey(key)
if err != nil {
return nil, err
}
pubKeyBytes := ssh.MarshalAuthorizedKey(publicRsaKey)
log.Println("Public key was generated")
return pubKeyBytes, nil
}
func GenerateSshKeyPair(bitSize int, keyPassword, certId string, format ...string) ([]byte, []byte, error) {
currentFormat := ""
if len(format) > 0 && format[0] != "" {
currentFormat = format[0]
}
privateKey, err := generatePrivKey(bitSize)
if err != nil {
return nil, nil, err
}
publicKeyBytes, err := generatePublicKey(&privateKey.PublicKey)
if err != nil {
return nil, nil, err
}
privateKeyBytes, err := encodePrivKeyToPEM(privateKey, keyPassword, currentFormat)
if err != nil {
return nil, nil, err
}
sPubKey := string(publicKeyBytes)
sPubKey = strings.TrimRight(sPubKey, "\r\n")
sPubKey = fmt.Sprint(sPubKey, " ", certId)
return privateKeyBytes, []byte(sPubKey), nil
}