-
Notifications
You must be signed in to change notification settings - Fork 0
/
d1.go
468 lines (398 loc) · 13.8 KB
/
d1.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// Copyright (C) 2022 CYBERCRYPT
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
/*
D1 is a library that provides easy access to data encryption with built in access control.
*/
package d1
import (
"context"
"errors"
"github.com/gofrs/uuid"
"github.com/rs/zerolog"
"github.com/cybercryptio/d1-lib/v2/crypto"
"github.com/cybercryptio/d1-lib/v2/data"
"github.com/cybercryptio/d1-lib/v2/id"
"github.com/cybercryptio/d1-lib/v2/io"
"github.com/cybercryptio/d1-lib/v2/key"
"github.com/cybercryptio/d1-lib/v2/log"
)
// Error returned if the caller cannot be authenticated by the Identity Provider.
var ErrNotAuthenticated = errors.New("not authenticated")
// Error returned if the caller tries to access data they are not authorized for.
var ErrNotAuthorized = errors.New("not authorized")
// D1 is the entry point to the library. All main functionality is exposed through methods
// on this struct.
type D1 struct {
keyProvider key.Provider
ioProvider io.Provider
idProvider id.Provider
objectCryptor crypto.CryptorInterface
accessCryptor crypto.CryptorInterface
tokenCryptor crypto.CryptorInterface
}
// New creates a new instance of D1 configured with the given providers.
func New(ctx context.Context, keyProvider key.Provider, ioProvider io.Provider, idProvider id.Provider) (D1, error) {
l := zerolog.Ctx(ctx)
l.Debug().Msg("getting keys")
keys, err := keyProvider.GetKeys(ctx)
if err != nil {
return D1{}, err
}
l.Debug().Msg("creating object cryptor")
objectCryptor, err := crypto.NewAESCryptor(keys.KEK)
if err != nil {
return D1{}, err
}
l.Debug().Msg("creating access cryptor")
accessCryptor, err := crypto.NewAESCryptor(keys.AEK)
if err != nil {
return D1{}, err
}
l.Debug().Msg("creating token cryptor")
tokenCryptor, err := crypto.NewAESCryptor(keys.TEK)
if err != nil {
return D1{}, err
}
return D1{
keyProvider: keyProvider,
ioProvider: ioProvider,
idProvider: idProvider,
objectCryptor: &objectCryptor,
accessCryptor: &accessCryptor,
tokenCryptor: &tokenCryptor,
}, nil
}
////////////////////////////////////////////////////////
// Object //
////////////////////////////////////////////////////////
// Encrypt creates a new sealed object containing the provided plaintext data as well as an access
// list that controls access to that data. The Identity of the caller is automatically added to the
// access list. To grant access to other callers either specify them in the optional 'groups'
// argument or see AddGroupsToAccess.
//
// The returned ID is the unique identifier of the sealed object. It is used to identify the object
// and related data about the object to the IO Provider, and needs to be provided when decrypting
// the object.
//
// For all practical purposes, the size of the ciphertext in the SealedObject is len(plaintext) + 48
// bytes.
//
// Required scopes:
// - Encrypt
func (d *D1) Encrypt(ctx context.Context, token string, object *data.Object, groups ...string) (uuid.UUID, error) {
l := zerolog.Ctx(ctx)
log.WithMethod(l, "encrypt")
identity, err := d.verifyAccess(ctx, token, id.ScopeEncrypt)
if err != nil {
return uuid.Nil, err
}
oid, err := uuid.NewV4()
if err != nil {
return uuid.Nil, err
}
log.WithOID(l, oid)
l.Debug().Msg("sealing object")
wrappedOEK, sealedObject, err := object.Seal(oid, d.objectCryptor)
if err != nil {
return uuid.Nil, err
}
l.Debug().Strs("groups", groups).Msg("creating access")
access := data.NewAccess(wrappedOEK)
access.AddGroups(append(groups, identity.ID)...)
l.Debug().Msg("sealing access")
sealedAccess, err := access.Seal(oid, d.accessCryptor)
if err != nil {
return uuid.Nil, err
}
// Write data to IO Provider
if err := d.putSealedObject(ctx, &sealedObject, false); err != nil {
return uuid.Nil, err
}
if err := d.putSealedAccess(ctx, &sealedAccess, false); err != nil {
return uuid.Nil, err
}
return oid, nil
}
// Update creates a new sealed object containing the provided plaintext data but uses a previously
// created access list to control access to that data. The authorizing Identity must be part of the
// provided access list, either directly or through group membership.
//
// The input ID is the identifier obtained by previously calling Encrypt.
//
// Required scopes:
// - Update
func (d *D1) Update(ctx context.Context, token string, oid uuid.UUID, object *data.Object) error {
l := zerolog.Ctx(ctx)
log.WithMethod(l, "update")
log.WithOID(l, oid)
identity, err := d.verifyAccess(ctx, token, id.ScopeUpdate)
if err != nil {
return err
}
access, err := d.getSealedAccess(ctx, oid)
if err != nil {
return err
}
plainAccess, err := d.authorizeAccess(ctx, &identity, id.ScopeUpdate, access)
if err != nil {
return err
}
l.Debug().Msg("sealing object")
wrappedOEK, sealedObject, err := object.Seal(oid, d.objectCryptor)
if err != nil {
return err
}
l.Debug().Msg("sealing access")
plainAccess.WrappedOEK = wrappedOEK
sealedAccess, err := plainAccess.Seal(oid, d.accessCryptor)
if err != nil {
return err
}
// Write data to IO Provider
if err := d.putSealedObject(ctx, &sealedObject, true); err != nil {
return err
}
if err := d.putSealedAccess(ctx, &sealedAccess, true); err != nil {
return err
}
return nil
}
// Decrypt fetches a sealed object and extracts the plaintext. The authorizing Identity must be part of
// the provided access list, either directly or through group membership.
//
// The input ID is the identifier obtained by previously calling Encrypt.
//
// The unsealed object may contain sensitive data.
//
// Required scopes:
// - Decrypt
func (d *D1) Decrypt(ctx context.Context, token string, oid uuid.UUID) (data.Object, error) {
l := zerolog.Ctx(ctx)
log.WithMethod(l, "encrypt")
log.WithOID(l, oid)
identity, err := d.verifyAccess(ctx, token, id.ScopeDecrypt)
if err != nil {
return data.Object{}, err
}
access, err := d.getSealedAccess(ctx, oid)
if err != nil {
return data.Object{}, err
}
plainAccess, err := d.authorizeAccess(ctx, &identity, id.ScopeDecrypt, access)
if err != nil {
return data.Object{}, err
}
object, err := d.getSealedObject(ctx, oid)
if err != nil {
return data.Object{}, err
}
l.Debug().Msg("unsealing object")
return object.Unseal(plainAccess.WrappedOEK, d.objectCryptor)
}
// Delete deletes a sealed object. The authorizing Identity must be part of
// the provided access list, either directly or through group membership.
//
// The input ID is the identifier obtained by previously calling Encrypt.
//
// Required scopes:
// - Delete
func (d *D1) Delete(ctx context.Context, token string, oid uuid.UUID) error {
l := zerolog.Ctx(ctx)
log.WithMethod(l, "delete")
log.WithOID(l, oid)
identity, err := d.verifyAccess(ctx, token, id.ScopeDelete)
if err != nil {
return err
}
access, err := d.getSealedAccess(ctx, oid)
switch err {
case nil:
// Ignore and proceed
case io.ErrNotFound:
l.Debug().Msg("object not found")
// If we can't find the access, that should mean the sealed object
// doesn't exist, either because it never existed, or it has been completely deleted.
// Because the sealed access is deleted last as the step in a deletion
// (see further below) we know that a deleted access entry also implies a deleted object.
// In either case, what the client wanted has already
// been achieved, and so we return with no error.
return nil
default:
return err
}
if _, err = d.authorizeAccess(ctx, &identity, id.ScopeDelete, access); err != nil {
return err
}
// Delete data from IO Provider
// NOTE: It is a conscious decision to delete the sealed object first,
// then the sealed access.
// This way, if the deletion of the sealed object succeeds, but the
// deletion of the sealed access fails, we can retry with another delete
// to get rid of the dangling access entry.
// If we deleted the access first, and then fail to delete the object,
// we would have a dangling object we can't access, and therefore can't delete.
if err = d.deleteSealedObject(ctx, oid); err != nil {
return err
}
if err = d.deleteSealedAccess(ctx, oid); err != nil {
return err
}
return nil
}
////////////////////////////////////////////////////////
// Token //
////////////////////////////////////////////////////////
// CreateToken encapsulates the provided plaintext data in an opaque, self contained token with an
// expiry time given by TokenValidity.
//
// The contents of the token can be validated and retrieved with the GetTokenContents method.
func (d *D1) CreateToken(ctx context.Context, plaintext []byte) (data.SealedToken, error) {
l := zerolog.Ctx(ctx)
log.WithMethod(l, "create token")
token := data.NewToken(plaintext, data.TokenValidity)
l.Debug().Time("expiry", token.ExpiryTime).Msg("token created")
l.Debug().Msg("sealing token")
return token.Seal(d.tokenCryptor)
}
// GetTokenContents extracts the plaintext data from a sealed token, provided that the token has not
// expired.
func (d *D1) GetTokenContents(ctx context.Context, token *data.SealedToken) ([]byte, error) {
l := zerolog.Ctx(ctx)
log.WithMethod(l, "get token contents")
l.Debug().Time("expiry", token.ExpiryTime).Msg("unsealing token")
plainToken, err := token.Unseal(d.tokenCryptor)
if err != nil {
return nil, err
}
return plainToken.Plaintext, nil
}
////////////////////////////////////////////////////////
// Access //
////////////////////////////////////////////////////////
// GetAccessGroups extracts the set of group IDs contained in the object's access list. The
// authorizing Identity must be part of the access list.
//
// The input ID is the identifier obtained by previously calling Encrypt.
//
// The set of group IDs is somewhat sensitive data, as it reveals what Identities have access to
// the associated object.
//
// Required scopes:
// - GetAccessGroups
func (d *D1) GetAccessGroups(ctx context.Context, token string, oid uuid.UUID) (map[string]struct{}, error) {
l := zerolog.Ctx(ctx)
log.WithMethod(l, "get access groups")
log.WithOID(l, oid)
identity, err := d.verifyAccess(ctx, token, id.ScopeGetAccessGroups)
if err != nil {
return nil, err
}
access, err := d.getSealedAccess(ctx, oid)
if err != nil {
return nil, err
}
plainAccess, err := d.authorizeAccess(ctx, &identity, id.ScopeGetAccessGroups, access)
if err != nil {
return nil, err
}
return plainAccess.GetGroups(), nil
}
// AddGroupsToAccess appends the provided groups to the object's access list, giving them access to
// the associated object. The authorizing Identity must be part of the access list.
//
// The input ID is the identifier obtained by previously calling Encrypt.
//
// Required scopes:
// - ModifyAccessGroups
func (d *D1) AddGroupsToAccess(ctx context.Context, token string, oid uuid.UUID, groups ...string) error {
l := zerolog.Ctx(ctx)
log.WithMethod(l, "add groups to access")
log.WithOID(l, oid)
identity, err := d.verifyAccess(ctx, token, id.ScopeModifyAccessGroups)
if err != nil {
return err
}
access, err := d.getSealedAccess(ctx, oid)
if err != nil {
return err
}
plainAccess, err := d.authorizeAccess(ctx, &identity, id.ScopeModifyAccessGroups, access)
if err != nil {
return err
}
l.Debug().Strs("groups", groups).Msg("adding groups")
plainAccess.AddGroups(groups...)
l.Debug().Msg("sealing access")
*access, err = plainAccess.Seal(oid, d.accessCryptor)
if err != nil {
return err
}
return d.putSealedAccess(ctx, access, true)
}
// RemoveGroupsFromAccess removes the provided groups from the object's access list, preventing them
// from accessing the associated object. The authorizing Identity must be part of the access object.
//
// The input ID is the identifier obtained by previously calling Encrypt.
//
// Required scopes:
// - ModifyAccessGroups
func (d *D1) RemoveGroupsFromAccess(ctx context.Context, token string, oid uuid.UUID, groups ...string) error {
l := zerolog.Ctx(ctx)
log.WithMethod(l, "remove groups from access")
log.WithOID(l, oid)
identity, err := d.verifyAccess(ctx, token, id.ScopeModifyAccessGroups)
if err != nil {
return err
}
access, err := d.getSealedAccess(ctx, oid)
if err != nil {
return err
}
plainAccess, err := d.authorizeAccess(ctx, &identity, id.ScopeModifyAccessGroups, access)
if err != nil {
return err
}
l.Debug().Strs("groups", groups).Msg("removing groups")
plainAccess.RemoveGroups(groups...)
l.Debug().Msg("sealing access")
*access, err = plainAccess.Seal(oid, d.accessCryptor)
if err != nil {
return err
}
return d.putSealedAccess(ctx, access, true)
}
// AuthorizeIdentity checks whether the provided Identity is part of the object's access list, i.e. whether
// they are authorized to access the associated object. An error is returned if the Identity is not
// authorized.
//
// The input ID is the identifier obtained by previously calling Encrypt.
//
// Required scopes:
// - GetAccessGroups
func (d *D1) AuthorizeIdentity(ctx context.Context, token string, oid uuid.UUID) error {
l := zerolog.Ctx(ctx)
log.WithMethod(l, "authorize identity")
log.WithOID(l, oid)
identity, err := d.verifyAccess(ctx, token, id.ScopeGetAccessGroups)
if err != nil {
return err
}
access, err := d.getSealedAccess(ctx, oid)
if err != nil {
return err
}
_, err = d.authorizeAccess(ctx, &identity, id.ScopeGetAccessGroups, access)
return err
}