2019-04-03 09:46:21 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package uplink
|
|
|
|
|
|
|
|
import (
|
2020-04-03 16:13:27 +01:00
|
|
|
"strings"
|
|
|
|
|
2019-06-27 18:36:51 +01:00
|
|
|
"github.com/btcsuite/btcutil/base58"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/encryption"
|
|
|
|
"storj.io/common/macaroon"
|
|
|
|
"storj.io/common/paths"
|
|
|
|
"storj.io/common/pb"
|
|
|
|
"storj.io/common/storj"
|
2019-04-03 09:46:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultCipher = storj.EncAESGCM
|
|
|
|
)
|
|
|
|
|
2019-06-28 14:40:37 +01:00
|
|
|
// EncryptionAccess represents an encryption access context. It holds
|
|
|
|
// information about how various buckets and objects should be
|
|
|
|
// encrypted and decrypted.
|
2019-06-28 06:18:24 +01:00
|
|
|
type EncryptionAccess struct {
|
2019-06-27 18:36:51 +01:00
|
|
|
store *encryption.Store
|
|
|
|
}
|
|
|
|
|
2019-06-28 14:40:37 +01:00
|
|
|
// NewEncryptionAccess creates an encryption access context
|
2019-06-28 06:18:24 +01:00
|
|
|
func NewEncryptionAccess() *EncryptionAccess {
|
2020-01-30 14:35:14 +00:00
|
|
|
store := encryption.NewStore()
|
|
|
|
return &EncryptionAccess{store: store}
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
|
|
|
|
2019-06-28 14:40:37 +01:00
|
|
|
// NewEncryptionAccessWithDefaultKey creates an encryption access context with
|
|
|
|
// a default key set.
|
|
|
|
// Use (*Project).SaltedKeyFromPassphrase to generate a default key
|
2019-06-28 06:18:24 +01:00
|
|
|
func NewEncryptionAccessWithDefaultKey(defaultKey storj.Key) *EncryptionAccess {
|
|
|
|
ec := NewEncryptionAccess()
|
2019-06-27 18:36:51 +01:00
|
|
|
ec.SetDefaultKey(defaultKey)
|
|
|
|
return ec
|
|
|
|
}
|
|
|
|
|
2020-04-30 23:42:29 +01:00
|
|
|
// validate returns an error if the EncryptionAccess is not valid to be used.
|
|
|
|
func (s *EncryptionAccess) validate() error {
|
|
|
|
if s == nil {
|
|
|
|
return errs.New("invalid nil encryption access")
|
|
|
|
}
|
|
|
|
if s.store == nil {
|
|
|
|
return errs.New("invalid encryption access: no store")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-28 14:40:37 +01:00
|
|
|
// Store returns the underlying encryption store for the access context.
|
2019-06-28 06:18:24 +01:00
|
|
|
func (s *EncryptionAccess) Store() *encryption.Store {
|
2019-06-27 18:36:51 +01:00
|
|
|
return s.store
|
|
|
|
}
|
|
|
|
|
2019-06-28 14:40:37 +01:00
|
|
|
// SetDefaultKey sets the default key for the encryption access context.
|
|
|
|
// Use (*Project).SaltedKeyFromPassphrase to generate a default key
|
2019-06-28 06:18:24 +01:00
|
|
|
func (s *EncryptionAccess) SetDefaultKey(defaultKey storj.Key) {
|
2019-06-27 18:36:51 +01:00
|
|
|
s.store.SetDefaultKey(&defaultKey)
|
|
|
|
}
|
|
|
|
|
2020-02-11 08:25:31 +00:00
|
|
|
// SetDefaultPathCipher sets the default path cipher for the encryption access context.
|
|
|
|
func (s *EncryptionAccess) SetDefaultPathCipher(defaultPathCipher storj.CipherSuite) {
|
|
|
|
s.store.SetDefaultPathCipher(defaultPathCipher)
|
|
|
|
}
|
|
|
|
|
2019-06-28 14:40:37 +01:00
|
|
|
// Import merges the other encryption access context into this one. In cases
|
|
|
|
// of conflicting path decryption settings (including if both accesses have
|
2019-06-27 18:36:51 +01:00
|
|
|
// a default key), the new settings are kept.
|
2019-06-28 06:18:24 +01:00
|
|
|
func (s *EncryptionAccess) Import(other *EncryptionAccess) error {
|
2019-06-27 18:36:51 +01:00
|
|
|
if key := other.store.GetDefaultKey(); key != nil {
|
|
|
|
s.store.SetDefaultKey(key)
|
|
|
|
}
|
2020-02-11 08:25:31 +00:00
|
|
|
s.store.SetDefaultPathCipher(other.store.GetDefaultPathCipher())
|
2019-06-27 18:36:51 +01:00
|
|
|
return other.store.Iterate(s.store.Add)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncryptionRestriction represents a scenario where some set of objects
|
|
|
|
// may need to be encrypted/decrypted
|
|
|
|
type EncryptionRestriction struct {
|
|
|
|
Bucket string
|
|
|
|
PathPrefix storj.Path
|
|
|
|
}
|
|
|
|
|
2019-06-28 06:18:24 +01:00
|
|
|
// Restrict creates a new EncryptionAccess with no default key, where the key material
|
2019-06-28 14:40:37 +01:00
|
|
|
// in the new access is just enough to allow someone to access all of the given
|
2019-06-27 18:36:51 +01:00
|
|
|
// restrictions but no more.
|
2019-06-28 06:18:24 +01:00
|
|
|
func (s *EncryptionAccess) Restrict(apiKey APIKey, restrictions ...EncryptionRestriction) (APIKey, *EncryptionAccess, error) {
|
2019-06-27 18:36:51 +01:00
|
|
|
if len(restrictions) == 0 {
|
|
|
|
// Should the function signature be
|
2019-06-28 06:18:24 +01:00
|
|
|
// func (s *EncryptionAccess) Restrict(apiKey APIKey, restriction EncryptionRestriction, restrictions ...EncryptionRestriction) (APIKey, *EncryptionAccess, error) {
|
2019-06-27 18:36:51 +01:00
|
|
|
// so we don't have to do this test?
|
|
|
|
return APIKey{}, nil, errs.New("at least one restriction required")
|
|
|
|
}
|
|
|
|
|
|
|
|
caveat := macaroon.Caveat{}
|
2020-02-11 08:25:31 +00:00
|
|
|
|
2019-07-02 16:45:23 +01:00
|
|
|
access := NewEncryptionAccess()
|
2020-02-13 14:00:05 +00:00
|
|
|
access.SetDefaultPathCipher(s.store.GetDefaultPathCipher())
|
2020-04-03 16:13:27 +01:00
|
|
|
if len(restrictions) == 0 {
|
|
|
|
access.Store().SetDefaultKey(s.store.GetDefaultKey())
|
|
|
|
}
|
2019-06-27 18:36:51 +01:00
|
|
|
|
|
|
|
for _, res := range restrictions {
|
2020-04-03 16:13:27 +01:00
|
|
|
// If the share prefix ends in a `/` we need to remove this final slash.
|
|
|
|
// Otherwise, if we the shared prefix is `/bob/`, the encrypted shared
|
|
|
|
// prefix results in `enc("")/enc("bob")/enc("")`. This is an incorrect
|
|
|
|
// encrypted prefix, what we really want is `enc("")/enc("bob")`.
|
|
|
|
unencPath := paths.NewUnencrypted(strings.TrimSuffix(res.PathPrefix, "/"))
|
2019-06-27 18:36:51 +01:00
|
|
|
|
2020-02-13 14:00:05 +00:00
|
|
|
encPath, err := encryption.EncryptPathWithStoreCipher(res.Bucket, unencPath, s.store)
|
2019-06-27 18:36:51 +01:00
|
|
|
if err != nil {
|
|
|
|
return APIKey{}, nil, err
|
|
|
|
}
|
2019-07-05 09:36:35 +01:00
|
|
|
derivedKey, err := encryption.DerivePathKey(res.Bucket, unencPath, s.store)
|
2019-06-27 18:36:51 +01:00
|
|
|
if err != nil {
|
|
|
|
return APIKey{}, nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-02 16:45:23 +01:00
|
|
|
if err := access.store.Add(res.Bucket, unencPath, encPath, *derivedKey); err != nil {
|
2019-06-27 18:36:51 +01:00
|
|
|
return APIKey{}, nil, err
|
|
|
|
}
|
|
|
|
caveat.AllowedPaths = append(caveat.AllowedPaths, &macaroon.Caveat_Path{
|
|
|
|
Bucket: []byte(res.Bucket),
|
|
|
|
EncryptedPathPrefix: []byte(encPath.Raw()),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-11 08:25:31 +00:00
|
|
|
restrictedAPIKey, err := apiKey.Restrict(caveat)
|
2019-06-27 18:36:51 +01:00
|
|
|
if err != nil {
|
|
|
|
return APIKey{}, nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-11 08:25:31 +00:00
|
|
|
return restrictedAPIKey, access, nil
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
|
|
|
|
2019-06-28 06:18:24 +01:00
|
|
|
// Serialize turns an EncryptionAccess into base58
|
|
|
|
func (s *EncryptionAccess) Serialize() (string, error) {
|
2019-07-02 16:45:23 +01:00
|
|
|
p, err := s.toProto()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-04-08 13:08:57 +01:00
|
|
|
data, err := pb.Marshal(p)
|
2019-07-02 16:45:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", errs.New("unable to marshal encryption access: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return base58.CheckEncode(data, 0), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EncryptionAccess) toProto() (*pb.EncryptionAccess, error) {
|
2019-06-28 06:18:24 +01:00
|
|
|
var storeEntries []*pb.EncryptionAccess_StoreEntry
|
2020-02-11 08:25:31 +00:00
|
|
|
err := s.store.IterateWithCipher(func(bucket string, unenc paths.Unencrypted, enc paths.Encrypted, key storj.Key, pathCipher storj.CipherSuite) error {
|
2019-06-28 06:18:24 +01:00
|
|
|
storeEntries = append(storeEntries, &pb.EncryptionAccess_StoreEntry{
|
2019-06-27 18:36:51 +01:00
|
|
|
Bucket: []byte(bucket),
|
|
|
|
UnencryptedPath: []byte(unenc.Raw()),
|
|
|
|
EncryptedPath: []byte(enc.Raw()),
|
|
|
|
Key: key[:],
|
2020-02-11 08:25:31 +00:00
|
|
|
PathCipher: pb.CipherSuite(pathCipher),
|
2019-06-27 18:36:51 +01:00
|
|
|
})
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-07-02 16:45:23 +01:00
|
|
|
return nil, err
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var defaultKey []byte
|
|
|
|
if key := s.store.GetDefaultKey(); key != nil {
|
|
|
|
defaultKey = key[:]
|
|
|
|
}
|
|
|
|
|
2019-07-02 16:45:23 +01:00
|
|
|
return &pb.EncryptionAccess{
|
2020-02-11 08:25:31 +00:00
|
|
|
DefaultKey: defaultKey,
|
|
|
|
StoreEntries: storeEntries,
|
|
|
|
DefaultPathCipher: pb.CipherSuite(s.store.GetDefaultPathCipher()),
|
2019-07-02 16:45:23 +01:00
|
|
|
}, nil
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
|
|
|
|
2019-06-28 14:40:37 +01:00
|
|
|
// ParseEncryptionAccess parses a base58 serialized encryption access into a working one.
|
|
|
|
func ParseEncryptionAccess(serialized string) (*EncryptionAccess, error) {
|
|
|
|
data, version, err := base58.CheckDecode(serialized)
|
2019-06-27 18:36:51 +01:00
|
|
|
if err != nil || version != 0 {
|
2019-06-28 14:40:37 +01:00
|
|
|
return nil, errs.New("invalid encryption access format")
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
|
|
|
|
2019-06-28 06:18:24 +01:00
|
|
|
p := new(pb.EncryptionAccess)
|
2020-04-08 13:08:57 +01:00
|
|
|
if err := pb.Unmarshal(data, p); err != nil {
|
2019-06-28 14:40:37 +01:00
|
|
|
return nil, errs.New("unable to unmarshal encryption access: %v", err)
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
|
|
|
|
2019-07-02 16:45:23 +01:00
|
|
|
return parseEncryptionAccessFromProto(p)
|
|
|
|
}
|
2019-06-27 18:36:51 +01:00
|
|
|
|
2019-07-02 16:45:23 +01:00
|
|
|
func parseEncryptionAccessFromProto(p *pb.EncryptionAccess) (*EncryptionAccess, error) {
|
|
|
|
access := NewEncryptionAccess()
|
2019-06-27 18:36:51 +01:00
|
|
|
if len(p.DefaultKey) > 0 {
|
|
|
|
if len(p.DefaultKey) != len(storj.Key{}) {
|
2019-06-28 14:40:37 +01:00
|
|
|
return nil, errs.New("invalid default key in encryption access")
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
|
|
|
var defaultKey storj.Key
|
|
|
|
copy(defaultKey[:], p.DefaultKey)
|
2019-07-02 16:45:23 +01:00
|
|
|
access.SetDefaultKey(defaultKey)
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
|
|
|
|
2020-02-11 08:25:31 +00:00
|
|
|
access.SetDefaultPathCipher(storj.CipherSuite(p.DefaultPathCipher))
|
|
|
|
if p.DefaultPathCipher == pb.CipherSuite_ENC_UNSPECIFIED {
|
|
|
|
access.SetDefaultPathCipher(storj.EncAESGCM)
|
|
|
|
}
|
|
|
|
|
2019-06-27 18:36:51 +01:00
|
|
|
for _, entry := range p.StoreEntries {
|
|
|
|
if len(entry.Key) != len(storj.Key{}) {
|
2019-06-28 14:40:37 +01:00
|
|
|
return nil, errs.New("invalid key in encryption access entry")
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
|
|
|
var key storj.Key
|
|
|
|
copy(key[:], entry.Key)
|
|
|
|
|
2020-02-11 08:25:31 +00:00
|
|
|
err := access.store.AddWithCipher(
|
2019-06-27 18:36:51 +01:00
|
|
|
string(entry.Bucket),
|
|
|
|
paths.NewUnencrypted(string(entry.UnencryptedPath)),
|
|
|
|
paths.NewEncrypted(string(entry.EncryptedPath)),
|
2020-02-11 08:25:31 +00:00
|
|
|
key,
|
|
|
|
storj.CipherSuite(entry.PathCipher),
|
|
|
|
)
|
2019-06-27 18:36:51 +01:00
|
|
|
if err != nil {
|
2019-06-28 14:40:37 +01:00
|
|
|
return nil, errs.New("invalid encryption access entry: %v", err)
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-02 16:45:23 +01:00
|
|
|
return access, nil
|
2019-04-03 09:46:21 +01:00
|
|
|
}
|