-
Notifications
You must be signed in to change notification settings - Fork 12
/
keystore.go
197 lines (161 loc) · 4.37 KB
/
keystore.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package sshkeys
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"io"
"io/ioutil"
"os"
"golang.org/x/crypto/ssh"
)
// A KeyStore stores a single private key and the matching public key. It provides various methods to access
// both public and private key.
type KeyStore interface {
// Authentication methods configured to use this KeyStore.
Auth() []ssh.AuthMethod
// The raw private key bytes, as stored on disk.
KeyBytes() []byte
// Path to the private key
KeyPath() string
// The public key bytes, as stored on disk
PublicKey() []byte
}
type keyStore struct {
privateKey ssh.Signer
privateKeyBytes []byte
publicKeyBytes []byte
privateKeyPath string
}
// Creates a new keystore by reading the private and public key from the given path
// If the paths do not exist, the keys will be created at these locations
func NewKeyStore(privateKeyPath string, publicKeyPath string) (KeyStore, error) {
privateKey, privateKeyBuf, err := loadPrivateKeyAt(privateKeyPath)
if err != nil {
return nil, err
}
publicKey, err := loadPublicKeyAt(privateKey, publicKeyPath)
if err != nil {
return nil, err
}
return &keyStore{
privateKey: privateKey,
privateKeyBytes: privateKeyBuf,
publicKeyBytes: publicKey,
privateKeyPath: privateKeyPath,
}, nil
}
func (store *keyStore) Auth() []ssh.AuthMethod {
algo, ok := store.privateKey.(ssh.AlgorithmSigner)
if ok && algo.PublicKey().Type() == ssh.KeyAlgoRSA {
return []ssh.AuthMethod{
ssh.PublicKeys(
&algoSigner{signer: algo, ty: ssh.SigAlgoRSASHA2512},
&algoSigner{signer: algo, ty: ssh.SigAlgoRSASHA2256},
&algoSigner{signer: algo},
),
}
}
return []ssh.AuthMethod{
ssh.PublicKeys(store.privateKey),
}
}
// algoSigner adds support for non-default signature algorithms when authenticating.
type algoSigner struct {
signer ssh.AlgorithmSigner
ty string
}
func (w *algoSigner) PublicKey() ssh.PublicKey {
return &algoPublicKey{key: w.signer.PublicKey(), ty: w.ty}
}
func (w *algoSigner) Sign(rand io.Reader, data []byte) (*ssh.Signature, error) {
return w.signer.SignWithAlgorithm(rand, data, w.ty)
}
type algoPublicKey struct {
key ssh.PublicKey
ty string
}
func (s *algoPublicKey) Type() string {
if s.ty == "" {
return s.key.Type()
}
return s.ty
}
func (s *algoPublicKey) Marshal() []byte {
return s.key.Marshal()
}
func (s *algoPublicKey) Verify(data []byte, sig *ssh.Signature) error {
return s.key.Verify(data, sig)
}
func (store *keyStore) KeyBytes() []byte {
return store.privateKeyBytes
}
func (store *keyStore) KeyPath() string {
return store.privateKeyPath
}
func (store *keyStore) PublicKey() []byte {
return store.publicKeyBytes
}
func loadPrivateKeyAt(path string) (ssh.Signer, []byte, error) {
exists, err := pathExists(path)
if err != nil {
return nil, nil, fmt.Errorf("error checking for existence of private key: %w", err)
}
if !exists {
err := generatePrivateKeyAt(path)
if err != nil {
return nil, nil, fmt.Errorf("error generating private key: %w", err)
}
}
keyBuf, err := ioutil.ReadFile(path)
if err != nil {
return nil, nil, fmt.Errorf("error reading private key: %w", err)
}
// Ensure it can be parsed
signer, err := ssh.ParsePrivateKey(keyBuf)
if err != nil {
return nil, nil, fmt.Errorf("error parsing private key: %w", err)
}
return signer, keyBuf, nil
}
func loadPublicKeyAt(key ssh.Signer, path string) ([]byte, error) {
exists, err := pathExists(path)
if err != nil {
return nil, fmt.Errorf("error checking for existence of public key: %w", err)
}
if !exists {
err = ioutil.WriteFile(path, ssh.MarshalAuthorizedKey(key.PublicKey()), 0644)
if err != nil {
return nil, fmt.Errorf("error writing public key: %w", err)
}
}
pubBuf, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("error reading public key: %w", err)
}
return pubBuf, nil
}
func pathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if !os.IsNotExist(err) {
return false, err
}
return false, nil
}
func generatePrivateKeyAt(path string) error {
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return err
}
privateKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}
pemBuf := pem.EncodeToMemory(privateKeyPEM)
err = ioutil.WriteFile(path, pemBuf, 0600)
if err != nil {
return err
}
return nil
}