Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

fix: wallet - incorrect crypto for EDV with WebKMS profiles #2849

Merged
merged 1 commit into from
Jun 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 16 additions & 9 deletions pkg/wallet/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"

"github.com/bluele/gcache"
"github.com/hyperledger/aries-framework-go/pkg/crypto/webkms"

"github.com/hyperledger/aries-framework-go/component/storage/edv"

Expand Down Expand Up @@ -44,7 +46,7 @@ func (s *storageProvider) OpenStore(auth string, opts *unlockOpts, config storag
var err error

if s.profile.EDVConf != nil {
provider, err = createEDVStorageProvider(auth, s.profile.EDVConf, opts)
provider, err = createEDVStorageProvider(auth, s.profile, opts)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -72,8 +74,8 @@ func (s *storageProvider) OpenStore(auth string, opts *unlockOpts, config storag

// TODO (#2815): find a way to allow EDV to be used without importing the edv package, since it causes the main Aries
// module to depend on edv
func createEDVStorageProvider(auth string, conf *edvConf, opts *unlockOpts) (storage.Provider, error) {
if conf.EncryptionKeyID == "" || conf.MACKeyID == "" {
func createEDVStorageProvider(auth string, profile *profile, opts *unlockOpts) (storage.Provider, error) {
if profile.EDVConf.EncryptionKeyID == "" || profile.EDVConf.MACKeyID == "" {
return nil, errors.New("invalid EDV configuration found in wallet profile, key IDs for encryption and MAC operations are missing") //nolint: lll
}

Expand All @@ -88,13 +90,18 @@ func createEDVStorageProvider(auth string, conf *edvConf, opts *unlockOpts) (sto
}

// get crypto
cryptoImpl, err := tinkcrypto.New()
if err != nil {
return nil, fmt.Errorf("failed to create crypto: %w", err)
var cryptoImpl crypto.Crypto
if profile.KeyServerURL != "" {
cryptoImpl = webkms.New(profile.KeyServerURL, http.DefaultClient, opts.webkmsOpts...)
} else {
cryptoImpl, err = tinkcrypto.New()
if err != nil {
return nil, fmt.Errorf("failed to create crypto: %w", err)
}
}

// get jwe encrypter
jweEncrypter, err := getJWSEncrypter(conf.EncryptionKeyID, keyMgr, cryptoImpl)
jweEncrypter, err := getJWSEncrypter(profile.EDVConf.EncryptionKeyID, keyMgr, cryptoImpl)
if err != nil {
return nil, fmt.Errorf("failed to create JWE encrypter: %w", err)
}
Expand All @@ -103,7 +110,7 @@ func createEDVStorageProvider(auth string, conf *edvConf, opts *unlockOpts) (sto
jweDecrypter := jose.NewJWEDecrypt(nil, cryptoImpl, keyMgr)

// get MAC crypto
macCrypto, err := getMacCrypto(conf.MACKeyID, keyMgr, cryptoImpl)
macCrypto, err := getMacCrypto(profile.EDVConf.MACKeyID, keyMgr, cryptoImpl)
if err != nil {
return nil, fmt.Errorf("failed to create mac crypto: %w", err)
}
Expand All @@ -114,7 +121,7 @@ func createEDVStorageProvider(auth string, conf *edvConf, opts *unlockOpts) (sto
}

// create EDV provider
return edv.NewRESTProvider(conf.ServerURL, conf.VaultID,
return edv.NewRESTProvider(profile.EDVConf.ServerURL, profile.EDVConf.VaultID,
edv.NewEncryptedFormatter(jweEncrypter, jweDecrypter, macCrypto, edv.WithDeterministicDocumentIDs()),
edvOpts...), nil
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/wallet/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func TestStorageProvider_OpenStore(t *testing.T) {
ServerURL: "sample-server",
VaultID: "sample-vault-ID",
},
KeyServerURL: sampleKeyServerURL,
}

kmgr, err := keyManager().getKeyManger(token)
Expand Down Expand Up @@ -87,7 +88,7 @@ func TestStorageProvider_OpenStore(t *testing.T) {
require.NotEmpty(t, store)

// no edv opts
store, err = wsp.OpenStore(token, nil,
store, err = wsp.OpenStore(token, &unlockOpts{},
storage.StoreConfiguration{TagNames: []string{Credential.Name()}})
require.NoError(t, err)
require.NotEmpty(t, store)
Expand Down