forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keystore.go
264 lines (240 loc) · 6.79 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
Copyright 2016 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package client
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"time"
"github.com/gravitational/teleport/lib/sshutils"
log "github.com/Sirupsen/logrus"
"github.com/gravitational/trace"
"golang.org/x/crypto/ssh"
)
const (
defaultKeyDir = ".tsh"
sessionKeyDir = "sessions"
fileNameCert = "cert"
fileNameKey = "key"
fileNamePub = "pub"
fileNameKnownHosts = "known_hosts"
)
// FSLocalKeyStore implements LocalKeyStore interface using the filesystem
// Here's the file layout for the FS store:
// ~/.tsh/
// ├── known_hosts --> trusted certificate authorities (their keys) in a format similar to known_hosts
// └── sessions --> server-signed session keys
// └── host-a
// | ├── cert
// | ├── key
// | └── pub
// └── host-b
// ├── cert
// ├── key
// └── pub
type FSLocalKeyStore struct {
LocalKeyStore
// KeyDir is the directory where all keys are stored
KeyDir string
}
// NewFSLocalKeyStore creates a new filesystem-based local keystore object
// and initializes it.
//
// if dirPath is empty, sets it to ~/.tsh
func NewFSLocalKeyStore(dirPath string) (s *FSLocalKeyStore, err error) {
log.Infof("using FSLocalKeyStore")
dirPath, err = initKeysDir(dirPath)
if err != nil {
return nil, trace.Wrap(err)
}
return &FSLocalKeyStore{
KeyDir: dirPath,
}, nil
}
// GetKeys returns all user session keys stored in the store
func (fs *FSLocalKeyStore) GetKeys() (keys []Key, err error) {
dirPath := filepath.Join(fs.KeyDir, sessionKeyDir)
if !isDir(dirPath) {
return make([]Key, 0), nil
}
dirEntries, err := ioutil.ReadDir(dirPath)
if err != nil {
return nil, trace.Wrap(err)
}
for _, fi := range dirEntries {
if !fi.IsDir() {
continue
}
k, err := fs.GetKey(fi.Name())
if err != nil {
// if a key is reported as 'not found' it's probably because it expired
if !trace.IsNotFound(err) {
return nil, trace.Wrap(err)
}
continue
}
keys = append(keys, *k)
}
return keys, nil
}
// AddKey adds a new key to the session store. If a key for the host is already
// stored, overwrites it.
func (fs *FSLocalKeyStore) AddKey(host string, key *Key) error {
dirPath, err := fs.dirFor(host)
if err != nil {
return trace.Wrap(err)
}
writeBytes := func(fname string, data []byte) error {
fp := filepath.Join(dirPath, fname)
err := ioutil.WriteFile(fp, data, 0640)
if err != nil {
log.Error(err)
}
return err
}
if err = writeBytes(fileNameCert, key.Cert); err != nil {
return trace.Wrap(err)
}
if err = writeBytes(fileNamePub, key.Pub); err != nil {
return trace.Wrap(err)
}
if err = writeBytes(fileNameKey, key.Priv); err != nil {
return trace.Wrap(err)
}
return nil
}
// GetKey returns a key for a given host. If the key is not found,
// returns trace.NotFound error.
func (fs *FSLocalKeyStore) GetKey(host string) (*Key, error) {
dirPath, err := fs.dirFor(host)
if err != nil {
return nil, trace.Wrap(err)
}
certFile := filepath.Join(dirPath, fileNameCert)
cert, err := ioutil.ReadFile(certFile)
if err != nil {
log.Error(err)
return nil, trace.Wrap(err)
}
pub, err := ioutil.ReadFile(filepath.Join(dirPath, fileNamePub))
if err != nil {
log.Error(err)
return nil, trace.Wrap(err)
}
priv, err := ioutil.ReadFile(filepath.Join(dirPath, fileNameKey))
if err != nil {
log.Error(err)
return nil, trace.Wrap(err)
}
key := &Key{Pub: pub, Priv: priv, Cert: cert}
// expired certificate? this key won't be accepted anymore, lets delete it:
certExpiration, err := key.CertValidBefore()
if err != nil {
return nil, trace.Wrap(err)
}
log.Infof("returning cert %v valid until %v", certFile, certExpiration)
if certExpiration.Before(time.Now().UTC()) {
log.Infof("TTL expired (%v) for session key %v", certExpiration, dirPath)
os.RemoveAll(dirPath)
return nil, trace.NotFound("session keys for %s are not found", host)
}
return key, nil
}
// AddKnownHost adds a new entry to 'known_CAs' file
func (fs *FSLocalKeyStore) AddKnownCA(domainName string, hostKeys []ssh.PublicKey) error {
fp, err := os.OpenFile(filepath.Join(fs.KeyDir, fileNameKnownHosts), os.O_CREATE|os.O_APPEND|os.O_RDWR, 0640)
if err != nil {
return trace.Wrap(err)
}
defer fp.Close()
for i := range hostKeys {
bytes := ssh.MarshalAuthorizedKey(hostKeys[i])
log.Infof("adding known CA %v %v", domainName, sshutils.Fingerprint(hostKeys[i]))
fmt.Fprintf(fp, "%s %s\n", domainName, bytes)
}
return nil
}
// GetKnownHost returns public keys of all trusted CAs
func (fs *FSLocalKeyStore) GetKnownCAs() ([]ssh.PublicKey, error) {
bytes, err := ioutil.ReadFile(filepath.Join(fs.KeyDir, fileNameKnownHosts))
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, trace.Wrap(err)
}
var (
pubKey ssh.PublicKey
retval []ssh.PublicKey = make([]ssh.PublicKey, 0)
)
for err == nil {
_, _, pubKey, _, bytes, err = ssh.ParseKnownHosts(bytes)
if err == nil {
retval = append(retval, pubKey)
}
}
if err != io.EOF {
return nil, trace.Wrap(err)
}
return retval, nil
}
// dirFor is a helper function. It returns a directory where session keys
// for a given host are stored
func (fs *FSLocalKeyStore) dirFor(hostname string) (string, error) {
dirPath := filepath.Join(fs.KeyDir, sessionKeyDir, hostname)
if !isDir(dirPath) {
if err := os.MkdirAll(dirPath, 0777); err != nil {
log.Error(err)
return "", trace.Wrap(err)
}
}
return dirPath, nil
}
// initKeysDir initializes the keystore root directory. Usually it is ~/.tsh
func initKeysDir(dirPath string) (string, error) {
var err error
// not specified? use `~/.tsh`
if dirPath == "" {
u, err := user.Current()
if err != nil {
dirPath = os.TempDir()
} else {
dirPath = u.HomeDir
}
dirPath = filepath.Join(dirPath, defaultKeyDir)
}
// create if doesn't exist:
_, err = os.Stat(dirPath)
if err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(dirPath, os.ModeDir|0777)
if err != nil {
return "", trace.Wrap(err)
}
} else {
return "", trace.Wrap(err)
}
}
return dirPath, nil
}
// isDir is a helper function to quickly check if a given path is a valid directory
func isDir(dirPath string) bool {
fi, err := os.Stat(dirPath)
if err == nil {
return fi.IsDir()
}
return false
}