/
keyStore.go
335 lines (298 loc) · 10.9 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
* Copyright 2020, Cossack Labs Limited
*
* 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 filesystem
import (
"context"
"errors"
"runtime"
"strings"
"time"
"github.com/go-redis/redis/v7"
log "github.com/sirupsen/logrus"
"github.com/cossacklabs/acra/cmd"
"github.com/cossacklabs/acra/utils/args"
keystoreV1 "github.com/cossacklabs/acra/keystore"
"github.com/cossacklabs/acra/keystore/v2/keystore/api"
"github.com/cossacklabs/acra/keystore/v2/keystore/asn1"
"github.com/cossacklabs/acra/keystore/v2/keystore/crypto"
"github.com/cossacklabs/acra/keystore/v2/keystore/filesystem/backend"
"github.com/cossacklabs/acra/keystore/v2/keystore/signature"
)
const serviceName = "keystore"
// Errors returned by basic keystore.
var (
ErrNotImplemented = errors.New("not implemented")
)
// KeyStore is a filesystem-like keystore which keeps key rings in files.
//
// What exactly is the underlying filesystem is somewhat flexible and controlled by filesystem.Backend.
// Normally this is an actual filesystem but there are alternative implementations.
type KeyStore struct {
encryptor crypto.KeyEncryptor
notary *signature.Notary
log *log.Entry
fs backend.Backend
}
// OpenDirectory opens a read-only keystore located in given directory.
func OpenDirectory(rootDir string, cryptosuite *crypto.KeyStoreSuite) (api.KeyStore, error) {
backend, err := backend.OpenDirectoryBackend(rootDir)
if err != nil {
return nil, err
}
return CustomKeyStore(backend, cryptosuite)
}
// OpenDirectoryRW opens a keystore located in given directory.
// If the directory does not exist it will be created.
func OpenDirectoryRW(rootDir string, cryptosuite *crypto.KeyStoreSuite) (api.MutableKeyStore, error) {
backend, err := backend.CreateDirectoryBackend(rootDir)
if err != nil {
return nil, err
}
return CustomKeyStore(backend, cryptosuite)
}
// IsKeyDirectory checks if the directory contains a keystore version 2.
// This is a conservative check.
// That is, positive return value does not mean that the directory contains *a valid* keystore.
// However, false value means that the directory is definitely not a valid keystore.
// In particular, false is returned if the directory does not exists or cannot be opened.
func IsKeyDirectory(keyDirPath string, extractor *args.ServiceExtractor) bool {
redisParams := cmd.ParseRedisCLIParametersFromFlags(extractor, "")
if redisParams.KeysConfigured() {
redisClient, err := backend.OpenRedisBackend(&backend.RedisConfig{
RootDir: keyDirPath,
Options: &redis.Options{
Addr: redisParams.HostPort,
Password: redisParams.Password,
DB: redisParams.DBKeys,
},
})
if err != nil {
log.WithError(err).Debug("Failed to find keystore v2 in Redis")
return false
}
// If the keystore has been opened successfully, it definitely exists.
redisClient.Close()
return true
}
// Otherwise, check the local filesystem storage provided by Acra CE.
return backend.CheckDirectoryVersion(keyDirPath) == nil
}
// NewInMemory returns a new, empty in-memory keystore.
// This is mostly useful for testing.
func NewInMemory(cryptosuite *crypto.KeyStoreSuite) (api.MutableKeyStore, error) {
return CustomKeyStore(backend.NewInMemory(), cryptosuite)
}
// CustomKeyStore returns a configurable filesystem-based keystore.
// This constructor is useful if you want to provide a custom filesystem backend.
//
// The backend will be closed when this keystore is closed,
// so a backend instance generally cannot be shared between keystores.
func CustomKeyStore(backend backend.Backend, cryptosuite *crypto.KeyStoreSuite) (api.MutableKeyStore, error) {
notary, err := signature.NewNotary(cryptosuite.SignatureAlgorithms)
if err != nil {
return nil, err
}
keystore := &KeyStore{
encryptor: cryptosuite.KeyEncryptor,
notary: notary,
fs: backend,
log: log.WithFields(log.Fields{
"service": serviceName,
}),
}
runtime.SetFinalizer(keystore, (*KeyStore).finalize)
return keystore, nil
}
func (s *KeyStore) finalize() {
s.log.Warn("Close() has not been called for a KeyStore")
err := s.Close()
if err != nil {
s.log.WithError(err).Warn("finalizer failed to close KeyStore")
}
}
//
// KeyStore & MutableKeyStore interface
//
// Close this keystore, releasing associated resources.
func (s *KeyStore) Close() error {
err := s.fs.Close()
// Disarm finalizer since we have been closed manually.
runtime.SetFinalizer(s, nil)
return err
}
// OpenKeyRing opens an existing key ring at given path.
func (s *KeyStore) OpenKeyRing(path string) (api.KeyRing, error) {
ring := newKeyRing(s, path)
err := s.readKeyRing(ring)
if err != nil {
return nil, err
}
return ring, nil
}
// OpenKeyRingRW opens a modifiable key ring at given path.
func (s *KeyStore) OpenKeyRingRW(path string) (api.MutableKeyRing, error) {
ring := newKeyRing(s, path)
err := s.openKeyRing(ring)
if err != nil {
return nil, err
}
return ring, nil
}
// ListKeyRings enumerates all key rings present in this keystore.
func (s *KeyStore) ListKeyRings() (rings []string, err error) {
err = s.fs.RLock()
if err != nil {
s.log.WithError(err).Debug("failed to lock store for reading")
return nil, err
}
defer func() {
err2 := s.fs.RUnlock()
if err2 != nil {
s.log.WithError(err2).Debug("failed to unlock store")
if err == nil {
err = err2
}
}
}()
rings, err = s.fs.ListAll()
if err != nil {
s.log.WithError(err).Debug("failed to list key rings")
return nil, err
}
for i := range rings {
rings[i] = strings.TrimSuffix(rings[i], keyringSuffix)
}
return rings, nil
}
// DescribeKeyRing describes key ring by its purpose path.
func (s *KeyStore) DescribeKeyRing(path string) (*keystoreV1.KeyDescription, error) {
// This is basic keystore which does not define any particular key rings.
// This method will be overridden by actual keystore implementation.
return nil, ErrNotImplemented
}
// DescribeRotatedKeyRing return KeyDescription list of rotated keys
func (s *KeyStore) DescribeRotatedKeyRing(path string) ([]keystoreV1.KeyDescription, error) {
// This is basic keystore which does not define any particular key rings.
// This method will be overridden by actual keystore implementation.
return nil, ErrNotImplemented
}
// ExportKeyRings packages specified key rings for export.
// Key ring data is encrypted and signed using given cryptosuite.
// Resulting container can be imported into existing or different keystore with ImportKeyRings().
func (s *KeyStore) ExportKeyRings(paths []string, cryptosuite *crypto.KeyStoreSuite, mode keystoreV1.ExportMode) ([]byte, error) {
keyRings, err := s.exportKeyRings(paths, mode)
if err != nil {
return nil, err
}
defer zeroizeKeyRings(keyRings)
return s.encryptAndSignKeyRings(keyRings, cryptosuite)
}
// ImportKeyRings unpacks key rings packaged by ExportKeyRings.
// The provided cryptosuite is used to verify the signature on the container and decrypt key ring data.
// Optional delegate can be used to control various aspects of the import process, such as conflict resolution.
// Returns a list of processed key rings.
func (s *KeyStore) ImportKeyRings(exportData []byte, cryptosuite *crypto.KeyStoreSuite, delegate api.KeyRingImportDelegate) ([]string, error) {
if delegate == nil {
delegate = &defaultImportDelegate{}
}
keyRings, err := s.decryptAndVerifyKeyRings(exportData, cryptosuite)
if err != nil {
return nil, err
}
defer zeroizeKeyRings(keyRings)
keyRingIDs := make([]string, len(keyRings))
for i := range keyRings {
err := s.importKeyRing(&keyRings[i], delegate)
if err != nil {
return nil, err
}
keyRingIDs[i] = string(keyRings[i].Purpose)
}
return keyRingIDs, nil
}
type defaultImportDelegate struct{}
func (*defaultImportDelegate) DecideKeyRingOverwrite(currentData, newData *asn1.KeyRing) (api.ImportDecision, error) {
return api.ImportAbort, ErrKeyRingExists
}
//
// Encryption and signatures
//
func (s *KeyStore) keyStoreContext(context []byte) []byte {
c := make([]byte, 0, len("AKSv2 keystore: ")+len(context))
c = append(c, "AKSv2 keystore: "...)
c = append(c, context...)
return c
}
func (s *KeyStore) encrypt(data, ctx []byte) ([]byte, error) {
keyContext := keystoreV1.NewEmptyKeyContext(s.keyStoreContext(ctx))
return s.encryptor.Encrypt(context.Background(), data, keyContext)
}
func (s *KeyStore) decrypt(data, ctx []byte) ([]byte, error) {
keyContext := keystoreV1.NewEmptyKeyContext(s.keyStoreContext(ctx))
return s.encryptor.Decrypt(context.Background(), data, keyContext)
}
func (s *KeyStore) keyRingSignatureContext(path string) []byte {
c := make([]byte, 0, len("key ring signature: ")+len(path))
c = append(c, "key ring signature: "...)
c = append(c, path...)
return s.keyStoreContext(c)
}
// Errors returned by signature verification:
var (
errIncorrectContentType = errors.New("incorrect ASN.1 ContentType")
errUnsupportedVersion = errors.New("unsupported ASN.1 Version")
)
func (s *KeyStore) signKeyRing(ring *asn1.KeyRing, path string) ([]byte, []asn1.Signature, error) {
container := asn1.SignedContainer{Payload: asn1.SignedPayload{
ContentType: asn1.TypeKeyRing,
Version: asn1.KeyRingVersion2,
LastModified: time.Now(),
Data: *ring,
}}
context := s.keyRingSignatureContext(path)
signedData, err := s.notary.Sign(&container, context)
if err != nil {
s.log.WithError(err).Warn("failed to sign key ring data")
return nil, nil, err
}
return signedData, container.Signatures, nil
}
func (s *KeyStore) verifyKeyRing(data []byte, path string) (*asn1.KeyRing, []asn1.Signature, error) {
log := s.log.WithField("path", path)
context := s.keyRingSignatureContext(path)
verified, err := s.notary.Verify(data, context)
if err != nil {
log.WithError(err).Warn("failed to verify key ring data")
return nil, nil, err
}
if verified.Payload.ContentType != asn1.TypeKeyRing {
log.WithField("actual", verified.Payload.ContentType).WithField("expected", asn1.TypeKeyRing).
Debug("incorrect key ring ContentType")
return nil, nil, errIncorrectContentType
}
if verified.Payload.Version != asn1.KeyRingVersion2 {
log.WithField("actual", verified.Payload.Version).WithField("expected", asn1.KeyRingVersion2).
Debug("unsupported key ring Version")
return nil, nil, errUnsupportedVersion
}
ringData, err := asn1.UnmarshalKeyRing(verified.Payload.Data.FullBytes)
if err != nil {
log.WithError(err).Debug("failed to unmarshal key ring data")
}
log.WithField("last-modified", verified.Payload.LastModified).
Trace("loaded key ring")
return ringData, verified.Signatures, nil
}