This repository has been archived by the owner on Dec 12, 2024. It is now read-only.
generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 55
/
storage.go
342 lines (291 loc) · 10.2 KB
/
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
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
package keystore
import (
"context"
"strings"
"time"
"github.com/TBD54566975/ssi-sdk/crypto"
"github.com/TBD54566975/ssi-sdk/crypto/jwx"
sdkutil "github.com/TBD54566975/ssi-sdk/util"
"github.com/goccy/go-json"
"github.com/google/tink/go/aead"
"github.com/google/tink/go/core/registry"
"github.com/google/tink/go/integration/awskms"
"github.com/google/tink/go/integration/gcpkms"
"github.com/google/tink/go/keyset"
"github.com/google/tink/go/tink"
"github.com/mr-tron/base58"
"github.com/pkg/errors"
"google.golang.org/api/option"
"github.com/tbd54566975/ssi-service/config"
"github.com/tbd54566975/ssi-service/internal/util"
"github.com/tbd54566975/ssi-service/pkg/storage"
)
// StoredKey represents a common data model to store data on all key types
type StoredKey struct {
ID string `json:"id"`
Controller string `json:"controller"`
KeyType crypto.KeyType `json:"keyType"`
Base58Key string `json:"key"`
Revoked bool `json:"revoked"`
RevokedAt string `json:"revokedAt"`
CreatedAt string `json:"createdAt"`
}
// KeyDetails represents a common data model to get information about a key, without revealing the key itself
type KeyDetails struct {
ID string `json:"id"`
Controller string `json:"controller"`
KeyType crypto.KeyType `json:"keyType"`
Revoked bool `json:"revoked"`
RevokedAt string `json:"revokedAt"`
CreatedAt string `json:"createdAt"`
PublicKeyJWK jwx.PublicKeyJWK `json:"publicKeyJwk"`
}
type ServiceKey struct {
Base58Key string
Base58Salt string
}
const (
namespace = "keystore"
publicNamespaceSuffix = ":public-keys"
skKey = "ssi-service-key"
keyNotFoundErrMsg = "key not found"
)
type Storage struct {
db storage.ServiceStorage
encrypter Encrypter
decrypter Decrypter
}
func NewKeyStoreStorage(db storage.ServiceStorage, e Encrypter, d Decrypter) (*Storage, error) {
s := &Storage{
db: db,
encrypter: e,
decrypter: d,
}
return s, nil
}
type wrappedEncrypter struct {
tink.AEAD
}
func (w wrappedEncrypter) Encrypt(_ context.Context, plaintext, contextData []byte) ([]byte, error) {
return w.AEAD.Encrypt(plaintext, contextData)
}
type wrappedDecrypter struct {
tink.AEAD
}
func (w wrappedDecrypter) Decrypt(_ context.Context, ciphertext, contextInfo []byte) ([]byte, error) {
return w.AEAD.Decrypt(ciphertext, contextInfo)
}
const (
gcpKMSScheme = "gcp-kms"
awsKMSScheme = "aws-kms"
)
func NewEncryption(db storage.ServiceStorage, cfg config.KeyStoreServiceConfig) (Encrypter, Decrypter, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if len(cfg.MasterKeyURI) != 0 {
return NewExternalEncrypter(ctx, cfg)
}
// First, generate a service key
serviceKey, serviceKeySalt, err := GenerateServiceKey(cfg.MasterKeyPassword)
if err != nil {
return nil, nil, sdkutil.LoggingErrorMsg(err, "generating service key")
}
key := ServiceKey{
Base58Key: serviceKey,
Base58Salt: serviceKeySalt,
}
if err := storeServiceKey(ctx, db, key); err != nil {
return nil, nil, err
}
return &encrypter{db}, &decrypter{db}, nil
}
func NewExternalEncrypter(ctx context.Context, cfg config.KeyStoreServiceConfig) (Encrypter, Decrypter, error) {
var client registry.KMSClient
var err error
switch {
case strings.HasPrefix(cfg.MasterKeyURI, gcpKMSScheme):
client, err = gcpkms.NewClientWithOptions(ctx, cfg.MasterKeyURI, option.WithCredentialsFile(cfg.KMSCredentialsPath))
if err != nil {
return nil, nil, errors.Wrap(err, "creating gcp kms client")
}
case strings.HasPrefix(cfg.MasterKeyURI, awsKMSScheme):
client, err = awskms.NewClientWithCredentials(cfg.MasterKeyURI, cfg.KMSCredentialsPath)
if err != nil {
return nil, nil, errors.Wrap(err, "creating aws kms client")
}
default:
return nil, nil, errors.Errorf("master_key_uri value %q is not supported", cfg.MasterKeyURI)
}
registry.RegisterKMSClient(client)
dek := aead.AES256GCMKeyTemplate()
kh, err := keyset.NewHandle(aead.KMSEnvelopeAEADKeyTemplate(cfg.MasterKeyURI, dek))
if err != nil {
return nil, nil, errors.Wrap(err, "creating keyset handle")
}
a, err := aead.New(kh)
if err != nil {
return nil, nil, errors.Wrap(err, "creating aead from key handl")
}
return wrappedEncrypter{a}, wrappedDecrypter{a}, nil
}
// TODO(gabe): support more robust service key operations, including rotation, and caching
func storeServiceKey(ctx context.Context, db storage.ServiceStorage, key ServiceKey) error {
keyBytes, err := json.Marshal(key)
if err != nil {
return sdkutil.LoggingErrorMsg(err, "could not marshal service key")
}
if err = db.Write(ctx, namespace, skKey, keyBytes); err != nil {
return sdkutil.LoggingErrorMsg(err, "could store marshal service key")
}
return nil
}
func getServiceKey(ctx context.Context, db storage.ServiceStorage) ([]byte, error) {
storedKeyBytes, err := db.Read(ctx, namespace, skKey)
if err != nil {
return nil, sdkutil.LoggingErrorMsg(err, "could not get service key")
}
if len(storedKeyBytes) == 0 {
return nil, sdkutil.LoggingNewError(keyNotFoundErrMsg)
}
var stored ServiceKey
if err = json.Unmarshal(storedKeyBytes, &stored); err != nil {
return nil, sdkutil.LoggingErrorMsg(err, "could not unmarshal service key")
}
keyBytes, err := base58.Decode(stored.Base58Key)
if err != nil {
return nil, errors.Wrap(err, "could not decode service key")
}
return keyBytes, nil
}
// Encrypter the interface for any encrypter implementation.
type Encrypter interface {
Encrypt(ctx context.Context, plaintext, contextData []byte) ([]byte, error)
}
// Decrypter is the interface for any decrypter. May be AEAD or Hybrid.
type Decrypter interface {
// Decrypt decrypts ciphertext. The second parameter may be treated as associated data for AEAD (as abstracted in
// https://datatracker.ietf.org/doc/html/rfc5116), or as contextInfofor HPKE (https://www.rfc-editor.org/rfc/rfc9180.html)
Decrypt(ctx context.Context, ciphertext, contextInfo []byte) ([]byte, error)
}
type encrypter struct {
db storage.ServiceStorage
}
func (e encrypter) Encrypt(ctx context.Context, plaintext, _ []byte) ([]byte, error) {
// get service key
serviceKey, err := getServiceKey(ctx, e.db)
if err != nil {
return nil, err
}
// encrypt key before storing
encryptedKey, err := util.XChaCha20Poly1305Encrypt(serviceKey, plaintext)
if err != nil {
return nil, sdkutil.LoggingErrorMsgf(err, "could not encrypt key")
}
return encryptedKey, nil
}
type decrypter struct {
db storage.ServiceStorage
}
func (d decrypter) Decrypt(ctx context.Context, ciphertext, _ []byte) ([]byte, error) {
// get service key
serviceKey, err := getServiceKey(ctx, d.db)
if err != nil {
return nil, err
}
// decrypt key before unmarshaling
decryptedKey, err := util.XChaCha20Poly1305Decrypt(serviceKey, ciphertext)
if err != nil {
return nil, sdkutil.LoggingErrorMsgf(err, "could not decrypt key")
}
return decryptedKey, nil
}
func (kss *Storage) StoreKey(ctx context.Context, key StoredKey) error {
// TODO(gabe): conflict checking on key id
id := key.ID
if id == "" {
return sdkutil.LoggingNewError("could not store key without an ID")
}
keyBytes, err := json.Marshal(key)
if err != nil {
return sdkutil.LoggingErrorMsg(err, "deserializing key from base58")
}
skBytes, err := base58.Decode(key.Base58Key)
if err != nil {
return sdkutil.LoggingErrorMsg(err, "deserializing key from base58")
}
secretKey, err := crypto.BytesToPrivKey(skBytes, key.KeyType)
if err != nil {
return sdkutil.LoggingErrorMsg(err, "reconstructing private key from input")
}
publicJWK, _, err := jwx.PrivateKeyToPrivateKeyJWK(key.ID, secretKey)
if err != nil {
return sdkutil.LoggingErrorMsg(err, "reconstructing JWK")
}
publicBytes, err := json.Marshal(publicJWK)
if err != nil {
return sdkutil.LoggingErrorMsg(err, "marshalling JWK")
}
if err := kss.db.Write(ctx, namespace+publicNamespaceSuffix, id, publicBytes); err != nil {
return sdkutil.LoggingErrorMsgf(err, "writing public key")
}
// encrypt key before storing
encryptedKey, err := kss.encrypter.Encrypt(ctx, keyBytes, nil)
if err != nil {
return sdkutil.LoggingErrorMsgf(err, "could not encrypt key: %s", key.ID)
}
return kss.db.Write(ctx, namespace, id, encryptedKey)
}
// RevokeKey revokes a key by setting the revoked flag to true.
func (kss *Storage) RevokeKey(ctx context.Context, id string) error {
key, err := kss.GetKey(ctx, id)
if err != nil {
return err
}
if key == nil {
return sdkutil.LoggingNewErrorf("key not found: %s", id)
}
key.Revoked = true
key.RevokedAt = time.Now().UTC().Format(time.RFC3339)
return kss.StoreKey(ctx, *key)
}
func (kss *Storage) GetKey(ctx context.Context, id string) (*StoredKey, error) {
storedKeyBytes, err := kss.db.Read(ctx, namespace, id)
if err != nil {
return nil, sdkutil.LoggingErrorMsgf(err, "getting key details for key: %s", id)
}
if len(storedKeyBytes) == 0 {
return nil, sdkutil.LoggingNewErrorf("could not find key details for key: %s", id)
}
// decrypt key before unmarshalling
decryptedKey, err := kss.decrypter.Decrypt(ctx, storedKeyBytes, nil)
if err != nil {
return nil, sdkutil.LoggingErrorMsgf(err, "could not decrypt key: %s", id)
}
var stored StoredKey
if err = json.Unmarshal(decryptedKey, &stored); err != nil {
return nil, sdkutil.LoggingErrorMsgf(err, "unmarshalling stored key: %s", id)
}
return &stored, nil
}
func (kss *Storage) GetKeyDetails(ctx context.Context, id string) (*KeyDetails, error) {
stored, err := kss.GetKey(ctx, id)
if err != nil {
return nil, sdkutil.LoggingErrorMsgf(err, "reading details for private key %q", id)
}
storedPublicKeyBytes, err := kss.db.Read(ctx, namespace+publicNamespaceSuffix, id)
if err != nil {
return nil, sdkutil.LoggingErrorMsgf(err, "reading details for public key %q", id)
}
var storedPublicKey jwx.PublicKeyJWK
if err = json.Unmarshal(storedPublicKeyBytes, &storedPublicKey); err != nil {
return nil, sdkutil.LoggingErrorMsgf(err, "unmarshalling public key")
}
return &KeyDetails{
ID: stored.ID,
Controller: stored.Controller,
KeyType: stored.KeyType,
CreatedAt: stored.CreatedAt,
Revoked: stored.Revoked,
PublicKeyJWK: storedPublicKey,
}, nil
}