-
Notifications
You must be signed in to change notification settings - Fork 128
/
key.go
319 lines (286 loc) · 8.34 KB
/
key.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
/*
* 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 (
"fmt"
"time"
"github.com/cossacklabs/acra/keystore/v2/keystore/api"
"github.com/cossacklabs/acra/keystore/v2/keystore/asn1"
log "github.com/sirupsen/logrus"
)
//
// Construction
//
func (r *KeyRing) newKey(k api.KeyDescription) (*asn1.Key, error) {
nextSeqnum := r.nextSeqnum()
key := &asn1.Key{
Seqnum: nextSeqnum,
State: asn1.KeyPreActive,
ValidSince: k.ValidSince,
ValidUntil: k.ValidUntil,
Data: make([]asn1.KeyData, 0, 1),
}
if k.ValidSince.After(k.ValidUntil) {
return nil, api.ErrInvalidCryptoperiod
}
if len(k.Data) == 0 {
return nil, api.ErrNoKeyData
}
for _, data := range k.Data {
err := r.addKeyData(data, key)
if err != nil {
log.WithError(err).Debug("failed to add key data")
return nil, err
}
}
return key, nil
}
func (r *KeyRing) copyKey(other *asn1.Key) (*asn1.Key, error) {
if other.ValidSince.After(other.ValidUntil) {
return nil, api.ErrInvalidCryptoperiod
}
if len(other.Data) == 0 {
return nil, api.ErrNoKeyData
}
key := *other
// Other key's data is currently in plaintext. We need to encrypt it.
key.Data = make([]asn1.KeyData, 0, len(other.Data))
for _, otherKey := range other.Data {
data := api.KeyData{
Format: api.KeyFormat(otherKey.Format),
PublicKey: otherKey.PublicKey,
PrivateKey: otherKey.PrivateKey,
SymmetricKey: otherKey.SymmetricKey,
}
err := r.addKeyData(data, &key)
if err != nil {
return nil, err
}
}
return &key, nil
}
func (r *KeyRing) keyDataByFormat(seqnum int, format api.KeyFormat) (*asn1.KeyData, error) {
key := r.keyDataBySeqnum(seqnum)
if key == nil {
return nil, api.ErrKeyNotExist
}
// If the key data has been destroyed then there is no point in looking for it.
if api.KeyState(key.State) == api.KeyDestroyed {
return nil, api.ErrKeyDestroyed
}
for i := range key.Data {
if api.KeyFormat(key.Data[i].Format) == format {
return &key.Data[i], nil
}
}
return nil, api.ErrFormatMissing
}
//
// KeyAccess & MutableKeyAccess interface
//
// State of the key right now.
func (r *KeyRing) State(seqnum int) (api.KeyState, error) {
key := r.keyDataBySeqnum(seqnum)
if key == nil {
return api.KeyDestroyed, api.ErrKeyNotExist
}
return api.KeyState(key.State), nil
}
// SetState changes key State to the given one, if allowed.
func (r *KeyRing) SetState(seqnum int, newState api.KeyState) error {
key := r.keyDataBySeqnum(seqnum)
if key == nil {
return api.ErrKeyNotExist
}
oldState := api.KeyState(key.State)
log := r.log.WithField("oldState", oldState).WithField("newState", newState)
if !api.KeyStateTransitionValid(oldState, newState) {
log.Debug("invalid state transition requested")
return api.ErrInvalidState
}
err := r.changeKeyState(seqnum, oldState, newState)
if err != nil {
log.WithError(err).Debug("failed to change key state")
return err
}
log.Trace("changed key state")
return nil
}
// SetCurrent makes this key current in its key ring.
func (r *KeyRing) SetCurrent(seqnum int) error {
err := r.setCurrent(seqnum)
if err != nil {
r.log.WithError(err).Debug("failed to set key current")
return err
}
r.log.WithField("seqnum", seqnum).Trace("set key current")
return nil
}
// DestroyKey erases key data (but keeps the key in the key ring).
func (r *KeyRing) DestroyKey(seqnum int) error {
log := r.log.WithField("seqnum", seqnum)
key := r.keyDataBySeqnum(seqnum)
if key == nil {
return api.ErrKeyNotExist
}
oldState := api.KeyState(key.State)
if !api.KeyStateTransitionValid(oldState, api.KeyDestroyed) {
log.WithField("oldState", oldState).Debug("Not allowed to destroy key")
return api.ErrInvalidState
}
err := r.destroyKey(seqnum, oldState)
if err != nil {
log.WithError(err).Debug("Failed to destroy key")
return err
}
log.Trace("Key destroyed")
return nil
}
// ValidSince returns the time before which the key cannot be used.
func (r *KeyRing) ValidSince(seqnum int) (time.Time, error) {
key := r.keyDataBySeqnum(seqnum)
if key == nil {
return time.Time{}, api.ErrKeyNotExist
}
return key.ValidSince, nil
}
// ValidUntil returns the time since which the key should not be used.
func (r *KeyRing) ValidUntil(seqnum int) (time.Time, error) {
key := r.keyDataBySeqnum(seqnum)
if key == nil {
return time.Time{}, api.ErrKeyNotExist
}
return key.ValidUntil, nil
}
// Formats available for this key.
func (r *KeyRing) Formats(seqnum int) ([]api.KeyFormat, error) {
key := r.keyDataBySeqnum(seqnum)
if key == nil {
return nil, api.ErrKeyNotExist
}
formats := make([]api.KeyFormat, 0, len(key.Data))
for _, data := range key.Data {
formats = append(formats, api.KeyFormat(data.Format))
}
return formats, nil
}
// PublicKey data in given format, if available.
func (r *KeyRing) PublicKey(seqnum int, format api.KeyFormat) ([]byte, error) {
data, err := r.keyDataByFormat(seqnum, format)
if err != nil {
return nil, err
}
key := data.PublicKey
if len(key) == 0 {
return nil, api.ErrInvalidFormat
}
return key, nil
}
// PrivateKey data in given format, if available.
func (r *KeyRing) PrivateKey(seqnum int, format api.KeyFormat) ([]byte, error) {
data, err := r.keyDataByFormat(seqnum, format)
if err != nil {
return nil, err
}
if len(data.PrivateKey) == 0 {
return nil, api.ErrNoKeyData
}
decryptedKey, err := r.decryptPrivateKey(seqnum, data.PrivateKey)
if err != nil {
r.log.WithError(err).Warn("failed to decrypt private key")
return nil, err
}
return decryptedKey, nil
}
// SymmetricKey data in given format, if available.
func (r *KeyRing) SymmetricKey(seqnum int, format api.KeyFormat) ([]byte, error) {
data, err := r.keyDataByFormat(seqnum, format)
if err != nil {
return nil, err
}
if len(data.SymmetricKey) == 0 {
return nil, api.ErrInvalidFormat
}
decryptedKey, err := r.decryptSymmetricKey(seqnum, data.SymmetricKey)
if err != nil {
r.log.WithError(err).Warn("failed to decrypt symmetric key")
return nil, err
}
return decryptedKey, nil
}
//
// Key data encryption
//
func (r *KeyRing) privateKeyContext(seqnum int) []byte {
return []byte(fmt.Sprintf("private key %d", seqnum))
}
func (r *KeyRing) encryptPrivateKey(seqnum int, data []byte) ([]byte, error) {
return r.encrypt(data, r.privateKeyContext(seqnum))
}
func (r *KeyRing) decryptPrivateKey(seqnum int, data []byte) ([]byte, error) {
return r.decrypt(data, r.privateKeyContext(seqnum))
}
func (r *KeyRing) symmetricKeyContext(seqnum int) []byte {
return []byte(fmt.Sprintf("symmetric key %d", seqnum))
}
func (r *KeyRing) encryptSymmetricKey(seqnum int, data []byte) ([]byte, error) {
return r.encrypt(data, r.symmetricKeyContext(seqnum))
}
func (r *KeyRing) decryptSymmetricKey(seqnum int, data []byte) ([]byte, error) {
return r.decrypt(data, r.symmetricKeyContext(seqnum))
}
//
// Internal utilities
//
func (r *KeyRing) addKeyData(data api.KeyData, key *asn1.Key) error {
newData := asn1.KeyData{
Format: asn1.KeyFormat(data.Format),
}
for _, data := range key.Data {
if data.Format == newData.Format {
return api.ErrFormatDuplicated
}
}
switch data.Format {
case api.ThemisKeyPairFormat:
if len(data.PublicKey) == 0 {
return api.ErrNoKeyData
}
newData.PublicKey = data.PublicKey
if len(data.PrivateKey) != 0 {
encryptedPrivateKey, err := r.encryptPrivateKey(key.Seqnum, data.PrivateKey)
if err != nil {
log.WithError(err).Warn("failed to encrypt private key")
return err
}
newData.PrivateKey = encryptedPrivateKey
}
case api.ThemisSymmetricKeyFormat:
if len(data.SymmetricKey) == 0 {
return api.ErrNoKeyData
}
encryptedSymmetricKey, err := r.encryptSymmetricKey(key.Seqnum, data.SymmetricKey)
if err != nil {
log.WithError(err).Warn("failed to encrypt symmetric key")
return err
}
newData.SymmetricKey = encryptedSymmetricKey
default:
return api.ErrInvalidFormat
}
key.Data = append(key.Data, newData)
return nil
}