forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keystore.go
252 lines (221 loc) · 5.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
/*
Copyright 2015 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.
Keystore implements functions for saving and loading from hard disc
temporary teleport certificates
*/
package client
import (
"encoding/json"
"io/ioutil"
"net"
"os"
"os/user"
"path/filepath"
"strings"
"time"
"github.com/gravitational/teleport/lib/backend/boltbk"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/sshutils"
log "github.com/Sirupsen/logrus"
"github.com/gravitational/trace"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)
// AddHostSignersToCache takes a list of CAs whom we trust. This list is added to a database
// of "seen" CAs.
//
// Every time we connect to a new host, we'll request its certificaate to be signed by one
// of these trusted CAs.
//
// Why do we trust these CAs? Because we received them from a trusted Teleport Proxy.
// Why do we trust the proxy? Because we've connected to it via HTTPS + username + Password + HOTP.
func AddHostSignersToCache(hostSigners []services.CertAuthority) error {
bk, err := boltbk.New(filepath.Join(getKeysDir(), HostSignersFilename))
if err != nil {
return trace.Wrap(nil)
}
defer bk.Close()
ca := services.NewCAService(bk)
for _, hostSigner := range hostSigners {
err := ca.UpsertCertAuthority(hostSigner, 0)
if err != nil {
return trace.Wrap(nil)
}
}
return nil
}
// CheckHostSignature checks if the given host key was signed by one of the trusted
// certificaate authorities (CAs)
func CheckHostSignature(hostId string, remote net.Addr, key ssh.PublicKey) error {
cert, ok := key.(*ssh.Certificate)
if !ok {
return trace.Errorf("expected certificate")
}
bk, err := boltbk.New(filepath.Join(getKeysDir(), HostSignersFilename))
if err != nil {
return trace.Wrap(nil)
}
defer bk.Close()
ca := services.NewCAService(bk)
cas, err := ca.GetCertAuthorities(services.HostCA)
if err != nil {
return trace.Wrap(err)
}
for i := range cas {
checkers, err := cas[i].Checkers()
if err != nil {
return trace.Wrap(err)
}
for _, checker := range checkers {
if sshutils.KeysEqual(cert.SignatureKey, checker) {
return nil
}
}
}
return trace.Errorf("no matching authority found")
}
// GetLocalAgentKeys returns a list of local keys agents can use
// to authenticate
func GetLocalAgentKeys() ([]agent.AddedKey, error) {
err := initKeysDir()
if err != nil {
return nil, trace.Wrap(err)
}
existingKeys, err := loadAllKeys()
if err != nil {
return nil, trace.Wrap(err)
}
addedKeys := make([]agent.AddedKey, len(existingKeys))
for i, key := range existingKeys {
pcert, _, _, _, err := ssh.ParseAuthorizedKey(key.Cert)
if err != nil {
return nil, trace.Wrap(err)
}
pk, err := ssh.ParseRawPrivateKey(key.Priv)
if err != nil {
return nil, trace.Wrap(err)
}
addedKey := agent.AddedKey{
PrivateKey: pk,
Certificate: pcert.(*ssh.Certificate),
Comment: "",
LifetimeSecs: 0,
ConfirmBeforeUse: false,
}
addedKeys[i] = addedKey
}
return addedKeys, nil
}
// GetLocalAgent loads all the saved teleport certificates and
// creates ssh agent with them
func GetLocalAgent() (agent.Agent, error) {
keys, err := GetLocalAgentKeys()
if err != nil {
return nil, trace.Wrap(err)
}
keyring := agent.NewKeyring()
for _, key := range keys {
if err := keyring.Add(key); err != nil {
return nil, trace.Wrap(err)
}
}
return keyring, nil
}
func initKeysDir() error {
_, err := os.Stat(getKeysDir())
if os.IsNotExist(err) {
err = os.MkdirAll(getKeysDir(), os.ModeDir|0777)
if err != nil {
return trace.Wrap(err)
}
} else {
if err != nil {
return trace.Wrap(err)
}
}
return nil
}
type Key struct {
Priv []byte
Cert []byte
Deadline time.Time
}
func saveKey(key Key, filename string) error {
err := initKeysDir()
if err != nil {
return trace.Wrap(err)
}
bytes, err := json.Marshal(key)
if err != nil {
return trace.Wrap(err)
}
err = ioutil.WriteFile(filename, bytes, 0666)
if err != nil {
return trace.Wrap(err)
}
return nil
}
func loadKey(filename string) (Key, error) {
bytes, err := ioutil.ReadFile(filename)
if err != nil {
return Key{}, trace.Wrap(err)
}
var key Key
err = json.Unmarshal(bytes, &key)
if err != nil {
return Key{}, trace.Wrap(err)
}
return key, nil
}
func loadAllKeys() ([]Key, error) {
keys := make([]Key, 0)
files, err := ioutil.ReadDir(getKeysDir())
if err != nil {
return nil, trace.Wrap(err)
}
for _, file := range files {
if !file.IsDir() && strings.HasPrefix(file.Name(), KeyFilePrefix) &&
strings.HasSuffix(file.Name(), KeyFileSuffix) {
key, err := loadKey(filepath.Join(getKeysDir(), file.Name()))
if err != nil {
log.Errorf(err.Error())
continue
}
if time.Now().Before(key.Deadline) {
keys = append(keys, key)
} else {
// remove old keys
err = os.Remove(filepath.Join(getKeysDir(), file.Name()))
if err != nil {
log.Errorf(err.Error())
}
}
}
}
return keys, nil
}
// getKeysDir() returns the directory where a client can store the temporary keys
func getKeysDir() string {
var baseDir string
u, err := user.Current()
if err != nil {
baseDir = os.TempDir()
} else {
baseDir = u.HomeDir
}
return filepath.Join(baseDir, ".tsh")
}
var (
KeyFilePrefix = "teleport_"
KeyFileSuffix = ".tkey"
HostSignersFilename = "hostsigners.db"
)