-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
keyring.go
268 lines (232 loc) · 6.36 KB
/
keyring.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
package vault
import (
"bytes"
"encoding/json"
"fmt"
"time"
"github.com/hashicorp/vault/sdk/helper/jsonutil"
)
const (
// 10% shy of the NIST recommended maximum, leaving a buffer to account for
// tracking losses.
absoluteOperationMaximum = int64(3_865_470_566)
absoluteOperationMinimum = int64(1_000_000)
minimumRotationInterval = 24 * time.Hour
)
var (
defaultRotationConfig = KeyRotationConfig{
MaxOperations: absoluteOperationMaximum,
}
disabledRotationConfig = KeyRotationConfig{
Disabled: true,
}
)
// Keyring is used to manage multiple encryption keys used by
// the barrier. New keys can be installed and each has a sequential term.
// The term used to encrypt a key is prefixed to the key written out.
// All data is encrypted with the latest key, but storing the old keys
// allows for decryption of keys written previously. Along with the encryption
// keys, the keyring also tracks the master key. This is necessary so that
// when a new key is added to the keyring, we can encrypt with the master key
// and write out the new keyring.
type Keyring struct {
masterKey []byte
keys map[uint32]*Key
activeTerm uint32
rotationConfig KeyRotationConfig
}
// EncodedKeyring is used for serialization of the keyring
type EncodedKeyring struct {
MasterKey []byte
Keys []*Key
RotationConfig KeyRotationConfig
}
// Key represents a single term, along with the key used.
type Key struct {
Term uint32
Version int
Value []byte
InstallTime time.Time
Encryptions uint64 `json:"encryptions,omitempty"`
}
type KeyRotationConfig struct {
Disabled bool
MaxOperations int64
Interval time.Duration
}
// Serialize is used to create a byte encoded key
func (k *Key) Serialize() ([]byte, error) {
return json.Marshal(k)
}
// DeserializeKey is used to deserialize and return a new key
func DeserializeKey(buf []byte) (*Key, error) {
k := new(Key)
if err := jsonutil.DecodeJSON(buf, k); err != nil {
return nil, fmt.Errorf("deserialization failed: %w", err)
}
return k, nil
}
// NewKeyring creates a new keyring
func NewKeyring() *Keyring {
k := &Keyring{
keys: make(map[uint32]*Key),
activeTerm: 0,
rotationConfig: defaultRotationConfig,
}
return k
}
// Clone returns a new copy of the keyring
func (k *Keyring) Clone() *Keyring {
clone := &Keyring{
masterKey: k.masterKey,
keys: make(map[uint32]*Key, len(k.keys)),
activeTerm: k.activeTerm,
rotationConfig: k.rotationConfig,
}
for idx, key := range k.keys {
clone.keys[idx] = key
}
return clone
}
// AddKey adds a new key to the keyring
func (k *Keyring) AddKey(key *Key) (*Keyring, error) {
// Ensure there is no conflict
if exist, ok := k.keys[key.Term]; ok {
if !bytes.Equal(key.Value, exist.Value) {
return nil, fmt.Errorf("conflicting key for term %d already installed", key.Term)
}
return k, nil
}
// Add a time if none
if key.InstallTime.IsZero() {
key.InstallTime = time.Now()
}
// Make a new keyring
clone := k.Clone()
// Install the new key
clone.keys[key.Term] = key
// Update the active term if newer
if key.Term > clone.activeTerm {
clone.activeTerm = key.Term
}
// Zero out encryption estimates for previous terms
for term, key := range clone.keys {
if term != clone.activeTerm {
key.Encryptions = 0
}
}
return clone, nil
}
// RemoveKey removes a key from the keyring
func (k *Keyring) RemoveKey(term uint32) (*Keyring, error) {
// Ensure this is not the active key
if term == k.activeTerm {
return nil, fmt.Errorf("cannot remove active key")
}
// Check if this term does not exist
if _, ok := k.keys[term]; !ok {
return k, nil
}
// Delete the key
clone := k.Clone()
delete(clone.keys, term)
return clone, nil
}
// ActiveTerm returns the currently active term
func (k *Keyring) ActiveTerm() uint32 {
return k.activeTerm
}
// ActiveKey returns the active encryption key, or nil
func (k *Keyring) ActiveKey() *Key {
return k.keys[k.activeTerm]
}
// TermKey returns the key for the given term, or nil
func (k *Keyring) TermKey(term uint32) *Key {
return k.keys[term]
}
// SetMasterKey is used to update the master key
func (k *Keyring) SetMasterKey(val []byte) *Keyring {
valCopy := make([]byte, len(val))
copy(valCopy, val)
clone := k.Clone()
clone.masterKey = valCopy
return clone
}
// MasterKey returns the master key
func (k *Keyring) MasterKey() []byte {
return k.masterKey
}
// Serialize is used to create a byte encoded keyring
func (k *Keyring) Serialize() ([]byte, error) {
// Create the encoded entry
enc := EncodedKeyring{
MasterKey: k.masterKey,
RotationConfig: k.rotationConfig,
}
for _, key := range k.keys {
enc.Keys = append(enc.Keys, key)
}
// JSON encode the keyring
buf, err := json.Marshal(enc)
return buf, err
}
// DeserializeKeyring is used to deserialize and return a new keyring
func DeserializeKeyring(buf []byte) (*Keyring, error) {
// Deserialize the keyring
var enc EncodedKeyring
if err := jsonutil.DecodeJSON(buf, &enc); err != nil {
return nil, fmt.Errorf("deserialization failed: %w", err)
}
// Create a new keyring
k := NewKeyring()
k.masterKey = enc.MasterKey
k.rotationConfig = enc.RotationConfig
k.rotationConfig.Sanitize()
for _, key := range enc.Keys {
k.keys[key.Term] = key
if key.Term > k.activeTerm {
k.activeTerm = key.Term
}
}
return k, nil
}
// N.B.:
// Since Go 1.5 these are not reliable; see the documentation around the memzero
// function. These are best-effort.
func (k *Keyring) Zeroize(keysToo bool) {
if k == nil {
return
}
if k.masterKey != nil {
memzero(k.masterKey)
}
if !keysToo || k.keys == nil {
return
}
for _, key := range k.keys {
memzero(key.Value)
}
}
func (c KeyRotationConfig) Clone() KeyRotationConfig {
clone := KeyRotationConfig{
MaxOperations: c.MaxOperations,
Interval: c.Interval,
Disabled: c.Disabled,
}
clone.Sanitize()
return clone
}
func (c *KeyRotationConfig) Sanitize() {
if c.MaxOperations == 0 || c.MaxOperations > absoluteOperationMaximum {
c.MaxOperations = absoluteOperationMaximum
}
if c.MaxOperations < absoluteOperationMinimum {
c.MaxOperations = absoluteOperationMinimum
}
if c.Interval > 0 && c.Interval < minimumRotationInterval {
c.Interval = minimumRotationInterval
}
}
func (c *KeyRotationConfig) Equals(config KeyRotationConfig) bool {
return c.MaxOperations == config.MaxOperations && c.Interval == config.Interval
}