/
keypair.go
150 lines (113 loc) · 3.37 KB
/
keypair.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package ssh
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"os"
"golang.org/x/crypto/ssh"
)
const (
defaultKeyLength = 2048
permPrivateFile = 0o600
)
type KeyPair struct {
PrivateKey []byte
PublicKey []byte
}
func NewKeyPair(path string) (KeyPair, error) {
var keyPair KeyPair
privateKey, exist, err := readFileIfExist(path)
if err != nil {
return keyPair, fmt.Errorf("could not read file: %w", err)
}
if !exist {
return generateKeyPair(path)
}
keyPair.PrivateKey = privateKey
publicKey, exist, err := readFileIfExist(path)
if err != nil {
return keyPair, fmt.Errorf("could not read file: %w", err)
}
if !exist {
return generatePublicKey(path, keyPair)
}
keyPair.PublicKey = publicKey
return keyPair, nil
}
func generateKeyPair(path string) (KeyPair, error) {
var keyPair KeyPair
rsaKey, err := rsa.GenerateKey(rand.Reader, defaultKeyLength)
if err != nil {
return keyPair, fmt.Errorf("could not generate key: %w", err)
}
if err := rsaKey.Validate(); err != nil {
return keyPair, fmt.Errorf("could not validate key: %w", err)
}
keyPair.PrivateKey = pem.EncodeToMemory(&pem.Block{
Type: "RSA PRIVATE KEY",
Headers: nil,
Bytes: x509.MarshalPKCS1PrivateKey(rsaKey),
})
//nolint: nosnakecase // because these are predefined in the os package
privateKeyFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, permPrivateFile)
if err != nil {
return keyPair, fmt.Errorf("could not write to file: %w", err)
}
defer privateKeyFile.Close()
_, err = privateKeyFile.Write(keyPair.PrivateKey)
if err != nil {
return keyPair, fmt.Errorf("could not write to file: %w", err)
}
publicKey, err := ssh.NewPublicKey(&rsaKey.PublicKey)
if err != nil {
return keyPair, fmt.Errorf("could not generate public key: %w", err)
}
keyPair.PublicKey = ssh.MarshalAuthorizedKey(publicKey)
publicKeyFile, err := os.Create(fmt.Sprintf("%s.pub", path))
if err != nil {
return keyPair, fmt.Errorf("could not write to file: %w", err)
}
defer publicKeyFile.Close()
_, err = publicKeyFile.Write(keyPair.PublicKey)
if err != nil {
return keyPair, fmt.Errorf("could not write to file: %w", err)
}
return keyPair, nil
}
func generatePublicKey(path string, keyPair KeyPair) (KeyPair, error) {
pemData, _ := pem.Decode(keyPair.PrivateKey)
if pemData == nil {
return keyPair, ErrNoPEMData
}
rsaKey, err := x509.ParsePKCS1PrivateKey(pemData.Bytes)
if err != nil {
return keyPair, fmt.Errorf("could not decrypt private key: %w", err)
}
publicKey, err := ssh.NewPublicKey(&rsaKey.PublicKey)
if err != nil {
return keyPair, fmt.Errorf("could not generate public key: %w", err)
}
keyPair.PublicKey = ssh.MarshalAuthorizedKey(publicKey)
publicKeyFile, err := os.Create(fmt.Sprintf("%s.pub", path))
if err != nil {
return keyPair, fmt.Errorf("could not write to file: %w", err)
}
defer publicKeyFile.Close()
_, err = publicKeyFile.Write(keyPair.PublicKey)
if err != nil {
return keyPair, fmt.Errorf("could not write to file: %w", err)
}
return keyPair, nil
}
func readFileIfExist(path string) ([]byte, bool, error) {
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return []byte{}, false, nil
}
return []byte{}, true, fmt.Errorf("could not check file %s: %w", path, err)
}
contents, err := os.ReadFile(path)
return contents, true, fmt.Errorf("could not read file %s: %w", path, err)
}