forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypted_key_storage.go
276 lines (222 loc) · 7.29 KB
/
encrypted_key_storage.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
package keysutil
import (
"context"
"encoding/base64"
"errors"
paths "path"
"sort"
"strings"
"github.com/hashicorp/golang-lru"
"github.com/hashicorp/vault/helper/base62"
"github.com/hashicorp/vault/logical"
)
const (
// DefaultCacheSize is used if no cache size is specified for
// NewEncryptedKeyStorage. This value is the number of cache entries to
// store, not the size in bytes of the cache.
DefaultCacheSize = 16 * 1024
// DefaultPrefix is used if no prefix is specified for
// NewEncryptedKeyStorage. Prefix must be defined so we can provide context
// for the base folder.
DefaultPrefix = "encryptedkeys/"
// EncryptedKeyPolicyVersionTpl is a template that can be used to minimize
// the amount of data that's stored with the ciphertext.
EncryptedKeyPolicyVersionTpl = "{{version}}:"
)
var (
// ErrPolicyDerivedKeys is returned if the provided policy does not use
// derived keys. This is a requirement for this storage implementation.
ErrPolicyDerivedKeys = errors.New("key policy must use derived keys")
// ErrPolicyConvergentEncryption is returned if the provided policy does not use
// convergent encryption. This is a requirement for this storage implementation.
ErrPolicyConvergentEncryption = errors.New("key policy must use convergent encryption")
// ErrPolicyConvergentVersion is returned if the provided policy does not use
// a new enough convergent version. This is a requirement for this storage
// implementation.
ErrPolicyConvergentVersion = errors.New("key policy must use convergent version > 2")
// ErrNilStorage is returned if the provided storage is nil.
ErrNilStorage = errors.New("nil storage provided")
// ErrNilPolicy is returned if the provided policy is nil.
ErrNilPolicy = errors.New("nil policy provided")
)
// EncryptedKeyStorageConfig is used to configure an EncryptedKeyStorage object.
type EncryptedKeyStorageConfig struct {
// Policy is the key policy to use to encrypt the key paths.
Policy *Policy
// Prefix is the storage prefix for this instance of the EncryptedKeyStorage
// object. This is stored in plaintext. If not set the DefaultPrefix will be
// used.
Prefix string
// CacheSize is the number of elements to cache. If not set the
// DetaultCacheSize will be used.
CacheSize int
}
// NewEncryptedKeyStorageWrapper takes an EncryptedKeyStorageConfig and returns a new
// EncryptedKeyStorage object.
func NewEncryptedKeyStorageWrapper(config EncryptedKeyStorageConfig) (*EncryptedKeyStorageWrapper, error) {
if config.Policy == nil {
return nil, ErrNilPolicy
}
if !config.Policy.Derived {
return nil, ErrPolicyDerivedKeys
}
if !config.Policy.ConvergentEncryption {
return nil, ErrPolicyConvergentEncryption
}
if config.Prefix == "" {
config.Prefix = DefaultPrefix
}
if !strings.HasSuffix(config.Prefix, "/") {
config.Prefix += "/"
}
size := config.CacheSize
if size <= 0 {
size = DefaultCacheSize
}
cache, err := lru.New2Q(size)
if err != nil {
return nil, err
}
return &EncryptedKeyStorageWrapper{
policy: config.Policy,
prefix: config.Prefix,
lru: cache,
}, nil
}
type EncryptedKeyStorageWrapper struct {
policy *Policy
lru *lru.TwoQueueCache
prefix string
}
func (f *EncryptedKeyStorageWrapper) Wrap(s logical.Storage) logical.Storage {
return &encryptedKeyStorage{
policy: f.policy,
s: s,
prefix: f.prefix,
lru: f.lru,
}
}
// EncryptedKeyStorage implements the logical.Storage interface and ensures the
// storage paths are encrypted in the underlying storage.
type encryptedKeyStorage struct {
policy *Policy
s logical.Storage
lru *lru.TwoQueueCache
prefix string
}
func ensureTailingSlash(path string) string {
if !strings.HasSuffix(path, "/") {
return path + "/"
}
return path
}
// List implements the logical.Storage List method, and decrypts all the items
// in a path prefix. This can only operate on full folder structures so the
// prefix should end in a "/".
func (s *encryptedKeyStorage) List(ctx context.Context, prefix string) ([]string, error) {
encPrefix, err := s.encryptPath(prefix)
if err != nil {
return nil, err
}
keys, err := s.s.List(ctx, ensureTailingSlash(encPrefix))
if err != nil {
return keys, err
}
decryptedKeys := make([]string, len(keys))
// The context for the decryption operations will be the object's prefix
// joined with the provided prefix. Join cleans the path ensuring there
// isn't a trailing "/".
context := []byte(paths.Join(s.prefix, prefix))
for i, k := range keys {
raw, ok := s.lru.Get(k)
if ok {
// cache HIT, we can bail early and skip the decode & decrypt operations.
decryptedKeys[i] = raw.(string)
continue
}
// If a folder is included in the keys it will have a trailing "/".
// We need to remove this before decoding/decrypting and add it back
// later.
appendSlash := strings.HasSuffix(k, "/")
if appendSlash {
k = strings.TrimSuffix(k, "/")
}
decoded := base62.Decode(k)
if len(decoded) == 0 {
return nil, errors.New("could not decode key")
}
// Decrypt the data with the object's key policy.
encodedPlaintext, err := s.policy.Decrypt(context, nil, string(decoded[:]))
if err != nil {
return nil, err
}
// The plaintext is still base64 encoded, decode it.
decoded, err = base64.StdEncoding.DecodeString(encodedPlaintext)
if err != nil {
return nil, err
}
plaintext := string(decoded[:])
// Add the slash back to the plaintext value
if appendSlash {
plaintext += "/"
k += "/"
}
// We want to store the unencoded version of the key in the cache.
// This will make it more performent when it's a HIT.
s.lru.Add(k, plaintext)
decryptedKeys[i] = plaintext
}
sort.Strings(decryptedKeys)
return decryptedKeys, nil
}
// Get implements the logical.Storage Get method.
func (s *encryptedKeyStorage) Get(ctx context.Context, path string) (*logical.StorageEntry, error) {
encPath, err := s.encryptPath(path)
if err != nil {
return nil, err
}
return s.s.Get(ctx, encPath)
}
// Put implements the logical.Storage Put method.
func (s *encryptedKeyStorage) Put(ctx context.Context, entry *logical.StorageEntry) error {
encPath, err := s.encryptPath(entry.Key)
if err != nil {
return err
}
e := &logical.StorageEntry{}
*e = *entry
e.Key = encPath
return s.s.Put(ctx, e)
}
// Delete implements the logical.Storage Delete method.
func (s *encryptedKeyStorage) Delete(ctx context.Context, path string) error {
encPath, err := s.encryptPath(path)
if err != nil {
return err
}
return s.s.Delete(ctx, encPath)
}
// encryptPath takes a plaintext path and encrypts each path section (separated
// by "/") with the object's key policy. The context for each encryption is the
// plaintext path prefix for the key.
func (s *encryptedKeyStorage) encryptPath(path string) (string, error) {
if path == "" || path == "/" {
return s.prefix, nil
}
path = paths.Clean(path)
// Trim the prefix if it starts with a "/"
path = strings.TrimPrefix(path, "/")
parts := strings.Split(path, "/")
encPath := s.prefix
context := strings.TrimSuffix(s.prefix, "/")
for _, p := range parts {
encoded := base64.StdEncoding.EncodeToString([]byte(p))
ciphertext, err := s.policy.Encrypt(0, []byte(context), nil, encoded)
if err != nil {
return "", err
}
encPath = paths.Join(encPath, base62.Encode([]byte(ciphertext)))
context = paths.Join(context, p)
}
return encPath, nil
}