2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-07-16 21:44:28 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package streams
|
|
|
|
|
|
|
|
import (
|
2018-09-26 14:32:23 +01:00
|
|
|
"bytes"
|
2018-07-16 21:44:28 +01:00
|
|
|
"context"
|
2018-09-26 14:32:23 +01:00
|
|
|
"crypto/rand"
|
2018-08-24 04:56:38 +01:00
|
|
|
"fmt"
|
2018-07-16 21:44:28 +01:00
|
|
|
"io"
|
2018-09-26 14:32:23 +01:00
|
|
|
"io/ioutil"
|
2018-10-25 21:28:16 +01:00
|
|
|
"strings"
|
2018-07-16 21:44:28 +01:00
|
|
|
"time"
|
|
|
|
|
2018-11-20 18:29:07 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2018-08-24 04:56:38 +01:00
|
|
|
"github.com/zeebo/errs"
|
2018-10-04 14:52:12 +01:00
|
|
|
"go.uber.org/zap"
|
2018-08-24 04:56:38 +01:00
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
|
|
|
|
2018-09-26 14:32:23 +01:00
|
|
|
"storj.io/storj/pkg/eestream"
|
2018-10-18 12:10:29 +01:00
|
|
|
"storj.io/storj/pkg/encryption"
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-10-25 21:28:16 +01:00
|
|
|
"storj.io/storj/pkg/ranger"
|
2018-09-12 14:03:31 +01:00
|
|
|
"storj.io/storj/pkg/storage/meta"
|
2018-08-24 04:56:38 +01:00
|
|
|
"storj.io/storj/pkg/storage/segments"
|
2018-10-19 14:38:13 +01:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-10-08 15:19:54 +01:00
|
|
|
"storj.io/storj/storage"
|
2018-07-16 21:44:28 +01:00
|
|
|
)
|
|
|
|
|
2018-08-24 04:56:38 +01:00
|
|
|
var mon = monkit.Package()
|
|
|
|
|
2019-04-02 22:15:31 +01:00
|
|
|
// Meta info about a stream
|
2018-07-30 19:57:50 +01:00
|
|
|
type Meta struct {
|
2019-04-10 23:27:04 +01:00
|
|
|
Modified time.Time
|
|
|
|
Expiration time.Time
|
|
|
|
Size int64
|
|
|
|
Data []byte
|
2018-07-30 19:57:50 +01:00
|
|
|
}
|
|
|
|
|
2018-08-24 04:56:38 +01:00
|
|
|
// convertMeta converts segment metadata to stream metadata
|
2019-04-02 22:15:31 +01:00
|
|
|
func convertMeta(lastSegmentMeta segments.Meta, stream pb.StreamInfo, streamMeta pb.StreamMeta) Meta {
|
2018-08-24 04:56:38 +01:00
|
|
|
return Meta{
|
2019-04-10 23:27:04 +01:00
|
|
|
Modified: lastSegmentMeta.Modified,
|
|
|
|
Expiration: lastSegmentMeta.Expiration,
|
|
|
|
Size: ((stream.NumberOfSegments - 1) * stream.SegmentsSize) + stream.LastSegmentSize,
|
|
|
|
Data: stream.Metadata,
|
2019-04-02 22:15:31 +01:00
|
|
|
}
|
2018-07-30 19:57:50 +01:00
|
|
|
}
|
|
|
|
|
2018-08-24 04:56:38 +01:00
|
|
|
// Store interface methods for streams to satisfy to be a store
|
2018-07-16 21:44:28 +01:00
|
|
|
type Store interface {
|
2018-11-13 12:21:52 +00:00
|
|
|
Meta(ctx context.Context, path storj.Path, pathCipher storj.Cipher) (Meta, error)
|
|
|
|
Get(ctx context.Context, path storj.Path, pathCipher storj.Cipher) (ranger.Ranger, Meta, error)
|
|
|
|
Put(ctx context.Context, path storj.Path, pathCipher storj.Cipher, data io.Reader, metadata []byte, expiration time.Time) (Meta, error)
|
|
|
|
Delete(ctx context.Context, path storj.Path, pathCipher storj.Cipher) error
|
|
|
|
List(ctx context.Context, prefix, startAfter, endBefore storj.Path, pathCipher storj.Cipher, recursive bool, limit int, metaFlags uint32) (items []ListItem, more bool, err error)
|
2018-07-16 21:44:28 +01:00
|
|
|
}
|
2018-08-24 04:56:38 +01:00
|
|
|
|
|
|
|
// streamStore is a store for streams
|
|
|
|
type streamStore struct {
|
2018-09-26 14:32:23 +01:00
|
|
|
segments segments.Store
|
|
|
|
segmentSize int64
|
2018-10-25 21:28:16 +01:00
|
|
|
rootKey *storj.Key
|
2018-09-26 14:32:23 +01:00
|
|
|
encBlockSize int
|
2018-10-19 14:38:13 +01:00
|
|
|
cipher storj.Cipher
|
2018-08-24 04:56:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewStreamStore stuff
|
2018-10-25 21:28:16 +01:00
|
|
|
func NewStreamStore(segments segments.Store, segmentSize int64, rootKey *storj.Key, encBlockSize int, cipher storj.Cipher) (Store, error) {
|
2018-08-24 04:56:38 +01:00
|
|
|
if segmentSize <= 0 {
|
|
|
|
return nil, errs.New("segment size must be larger than 0")
|
|
|
|
}
|
2018-10-25 21:28:16 +01:00
|
|
|
if rootKey == nil {
|
2018-09-26 14:32:23 +01:00
|
|
|
return nil, errs.New("encryption key must not be empty")
|
|
|
|
}
|
|
|
|
if encBlockSize <= 0 {
|
|
|
|
return nil, errs.New("encryption block size must be larger than 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &streamStore{
|
|
|
|
segments: segments,
|
|
|
|
segmentSize: segmentSize,
|
2018-10-25 21:28:16 +01:00
|
|
|
rootKey: rootKey,
|
2018-09-26 14:32:23 +01:00
|
|
|
encBlockSize: encBlockSize,
|
2018-10-19 14:38:13 +01:00
|
|
|
cipher: cipher,
|
2018-09-26 14:32:23 +01:00
|
|
|
}, nil
|
2018-08-24 04:56:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put breaks up data as it comes in into s.segmentSize length pieces, then
|
|
|
|
// store the first piece at s0/<path>, second piece at s1/<path>, and the
|
|
|
|
// *last* piece at l/<path>. Store the given metadata, along with the number
|
|
|
|
// of segments, in a new protobuf, in the metadata of l/<path>.
|
2018-11-13 12:21:52 +00:00
|
|
|
func (s *streamStore) Put(ctx context.Context, path storj.Path, pathCipher storj.Cipher, data io.Reader, metadata []byte, expiration time.Time) (m Meta, err error) {
|
2018-08-24 04:56:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-10-08 15:19:54 +01:00
|
|
|
// previously file uploaded?
|
2018-11-13 12:21:52 +00:00
|
|
|
err = s.Delete(ctx, path, pathCipher)
|
2018-10-08 15:19:54 +01:00
|
|
|
if err != nil && !storage.ErrKeyNotFound.Has(err) {
|
|
|
|
//something wrong happened checking for an existing
|
|
|
|
//file with the same name
|
|
|
|
return Meta{}, err
|
|
|
|
}
|
|
|
|
|
2018-11-13 12:21:52 +00:00
|
|
|
m, lastSegment, err := s.upload(ctx, path, pathCipher, data, metadata, expiration)
|
2018-10-19 15:36:43 +01:00
|
|
|
if err != nil {
|
2018-11-13 12:21:52 +00:00
|
|
|
s.cancelHandler(context.Background(), lastSegment, path, pathCipher)
|
2018-10-19 15:36:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return m, err
|
|
|
|
}
|
|
|
|
|
2018-11-13 12:21:52 +00:00
|
|
|
func (s *streamStore) upload(ctx context.Context, path storj.Path, pathCipher storj.Cipher, data io.Reader, metadata []byte, expiration time.Time) (m Meta, lastSegment int64, err error) {
|
2018-10-19 15:36:43 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2018-10-03 14:05:40 +01:00
|
|
|
var currentSegment int64
|
|
|
|
var streamSize int64
|
|
|
|
var putMeta segments.Meta
|
2018-08-24 04:56:38 +01:00
|
|
|
|
2018-10-04 14:52:12 +01:00
|
|
|
defer func() {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2018-11-13 12:21:52 +00:00
|
|
|
s.cancelHandler(context.Background(), currentSegment, path, pathCipher)
|
2018-10-04 14:52:12 +01:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-10-25 21:28:16 +01:00
|
|
|
derivedKey, err := encryption.DeriveContentKey(path, s.rootKey)
|
2018-09-26 14:32:23 +01:00
|
|
|
if err != nil {
|
2018-10-19 15:36:43 +01:00
|
|
|
return Meta{}, currentSegment, err
|
2018-09-26 14:32:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
eofReader := NewEOFReader(data)
|
|
|
|
|
|
|
|
for !eofReader.isEOF() && !eofReader.hasError() {
|
2018-10-17 12:34:50 +01:00
|
|
|
// generate random key for encrypting the segment's content
|
2018-10-19 14:38:13 +01:00
|
|
|
var contentKey storj.Key
|
2018-10-17 12:34:50 +01:00
|
|
|
_, err = rand.Read(contentKey[:])
|
2018-09-26 14:32:23 +01:00
|
|
|
if err != nil {
|
2018-10-19 15:36:43 +01:00
|
|
|
return Meta{}, currentSegment, err
|
2018-09-26 14:32:23 +01:00
|
|
|
}
|
|
|
|
|
2018-10-17 12:34:50 +01:00
|
|
|
// Initialize the content nonce with the segment's index incremented by 1.
|
|
|
|
// The increment by 1 is to avoid nonce reuse with the metadata encryption,
|
|
|
|
// which is encrypted with the zero nonce.
|
2018-10-19 14:38:13 +01:00
|
|
|
var contentNonce storj.Nonce
|
|
|
|
_, err := encryption.Increment(&contentNonce, currentSegment+1)
|
2018-09-26 14:32:23 +01:00
|
|
|
if err != nil {
|
2018-10-19 15:36:43 +01:00
|
|
|
return Meta{}, currentSegment, err
|
2018-09-26 14:32:23 +01:00
|
|
|
}
|
|
|
|
|
2018-10-19 14:38:13 +01:00
|
|
|
encrypter, err := encryption.NewEncrypter(s.cipher, &contentKey, &contentNonce, s.encBlockSize)
|
2018-09-26 14:32:23 +01:00
|
|
|
if err != nil {
|
2018-10-19 15:36:43 +01:00
|
|
|
return Meta{}, currentSegment, err
|
2018-09-26 14:32:23 +01:00
|
|
|
}
|
|
|
|
|
2018-10-17 12:34:50 +01:00
|
|
|
// generate random nonce for encrypting the content key
|
2018-10-19 14:38:13 +01:00
|
|
|
var keyNonce storj.Nonce
|
2018-10-15 19:58:57 +01:00
|
|
|
_, err = rand.Read(keyNonce[:])
|
|
|
|
if err != nil {
|
2018-10-19 15:36:43 +01:00
|
|
|
return Meta{}, currentSegment, err
|
2018-10-15 19:58:57 +01:00
|
|
|
}
|
|
|
|
|
2018-10-25 21:28:16 +01:00
|
|
|
encryptedKey, err := encryption.EncryptKey(&contentKey, s.cipher, derivedKey, &keyNonce)
|
2018-09-26 14:32:23 +01:00
|
|
|
if err != nil {
|
2018-10-19 15:36:43 +01:00
|
|
|
return Meta{}, currentSegment, err
|
2018-09-26 14:32:23 +01:00
|
|
|
}
|
2018-08-24 04:56:38 +01:00
|
|
|
|
2018-09-26 14:32:23 +01:00
|
|
|
sizeReader := NewSizeReader(eofReader)
|
|
|
|
segmentReader := io.LimitReader(sizeReader, s.segmentSize)
|
|
|
|
peekReader := segments.NewPeekThresholdReader(segmentReader)
|
|
|
|
largeData, err := peekReader.IsLargerThan(encrypter.InBlockSize())
|
|
|
|
if err != nil {
|
2018-10-19 15:36:43 +01:00
|
|
|
return Meta{}, currentSegment, err
|
2018-09-26 14:32:23 +01:00
|
|
|
}
|
|
|
|
var transformedReader io.Reader
|
|
|
|
if largeData {
|
|
|
|
paddedReader := eestream.PadReader(ioutil.NopCloser(peekReader), encrypter.InBlockSize())
|
2018-10-18 12:10:29 +01:00
|
|
|
transformedReader = encryption.TransformReader(paddedReader, encrypter, 0)
|
2018-09-26 14:32:23 +01:00
|
|
|
} else {
|
|
|
|
data, err := ioutil.ReadAll(peekReader)
|
|
|
|
if err != nil {
|
2018-10-19 15:36:43 +01:00
|
|
|
return Meta{}, currentSegment, err
|
2018-09-26 14:32:23 +01:00
|
|
|
}
|
2018-10-19 14:38:13 +01:00
|
|
|
cipherData, err := encryption.Encrypt(data, s.cipher, &contentKey, &contentNonce)
|
2018-09-26 14:32:23 +01:00
|
|
|
if err != nil {
|
2018-10-19 15:36:43 +01:00
|
|
|
return Meta{}, currentSegment, err
|
2018-09-26 14:32:23 +01:00
|
|
|
}
|
|
|
|
transformedReader = bytes.NewReader(cipherData)
|
|
|
|
}
|
2018-08-24 04:56:38 +01:00
|
|
|
|
2018-10-25 21:28:16 +01:00
|
|
|
putMeta, err = s.segments.Put(ctx, transformedReader, expiration, func() (storj.Path, []byte, error) {
|
2019-06-04 12:36:27 +01:00
|
|
|
encPath, err := EncryptAfterBucket(ctx, path, pathCipher, s.rootKey)
|
2018-10-15 16:39:09 +01:00
|
|
|
if err != nil {
|
2018-10-25 21:28:16 +01:00
|
|
|
return "", nil, err
|
2018-10-15 16:39:09 +01:00
|
|
|
}
|
|
|
|
|
2018-10-03 14:05:40 +01:00
|
|
|
if !eofReader.isEOF() {
|
2018-10-15 16:39:09 +01:00
|
|
|
segmentPath := getSegmentPath(encPath, currentSegment)
|
2018-10-15 19:58:57 +01:00
|
|
|
|
2018-10-19 14:38:13 +01:00
|
|
|
if s.cipher == storj.Unencrypted {
|
2018-10-15 19:58:57 +01:00
|
|
|
return segmentPath, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
segmentMeta, err := proto.Marshal(&pb.SegmentMeta{
|
2018-10-17 12:34:50 +01:00
|
|
|
EncryptedKey: encryptedKey,
|
|
|
|
KeyNonce: keyNonce[:],
|
2018-10-15 19:58:57 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2018-10-25 21:28:16 +01:00
|
|
|
return "", nil, err
|
2018-10-15 19:58:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return segmentPath, segmentMeta, nil
|
2018-10-03 14:05:40 +01:00
|
|
|
}
|
|
|
|
|
2018-10-25 21:28:16 +01:00
|
|
|
lastSegmentPath := storj.JoinPaths("l", encPath)
|
2018-10-15 19:58:57 +01:00
|
|
|
|
|
|
|
streamInfo, err := proto.Marshal(&pb.StreamInfo{
|
|
|
|
NumberOfSegments: currentSegment + 1,
|
|
|
|
SegmentsSize: s.segmentSize,
|
|
|
|
LastSegmentSize: sizeReader.Size(),
|
|
|
|
Metadata: metadata,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2018-10-25 21:28:16 +01:00
|
|
|
return "", nil, err
|
2018-10-03 14:05:40 +01:00
|
|
|
}
|
2018-10-15 19:58:57 +01:00
|
|
|
|
2018-10-17 12:34:50 +01:00
|
|
|
// encrypt metadata with the content encryption key and zero nonce
|
2018-10-19 14:38:13 +01:00
|
|
|
encryptedStreamInfo, err := encryption.Encrypt(streamInfo, s.cipher, &contentKey, &storj.Nonce{})
|
2018-10-17 12:34:50 +01:00
|
|
|
if err != nil {
|
2018-10-25 21:28:16 +01:00
|
|
|
return "", nil, err
|
2018-10-17 12:34:50 +01:00
|
|
|
}
|
|
|
|
|
2018-10-15 19:58:57 +01:00
|
|
|
streamMeta := pb.StreamMeta{
|
2018-10-17 12:34:50 +01:00
|
|
|
EncryptedStreamInfo: encryptedStreamInfo,
|
2018-10-19 14:38:13 +01:00
|
|
|
EncryptionType: int32(s.cipher),
|
2018-10-15 19:58:57 +01:00
|
|
|
EncryptionBlockSize: int32(s.encBlockSize),
|
|
|
|
}
|
|
|
|
|
2018-10-19 14:38:13 +01:00
|
|
|
if s.cipher != storj.Unencrypted {
|
2018-10-15 19:58:57 +01:00
|
|
|
streamMeta.LastSegmentMeta = &pb.SegmentMeta{
|
2018-10-17 12:34:50 +01:00
|
|
|
EncryptedKey: encryptedKey,
|
|
|
|
KeyNonce: keyNonce[:],
|
2018-10-15 19:58:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lastSegmentMeta, err := proto.Marshal(&streamMeta)
|
2018-10-03 14:05:40 +01:00
|
|
|
if err != nil {
|
2018-10-25 21:28:16 +01:00
|
|
|
return "", nil, err
|
2018-10-03 14:05:40 +01:00
|
|
|
}
|
2018-10-15 19:58:57 +01:00
|
|
|
|
2018-10-03 14:05:40 +01:00
|
|
|
return lastSegmentPath, lastSegmentMeta, nil
|
|
|
|
})
|
2018-08-24 04:56:38 +01:00
|
|
|
if err != nil {
|
2018-10-19 15:36:43 +01:00
|
|
|
return Meta{}, currentSegment, err
|
2018-08-24 04:56:38 +01:00
|
|
|
}
|
2018-09-26 14:32:23 +01:00
|
|
|
|
2018-10-03 14:05:40 +01:00
|
|
|
currentSegment++
|
|
|
|
streamSize += sizeReader.Size()
|
2018-08-24 04:56:38 +01:00
|
|
|
}
|
2018-10-17 12:34:50 +01:00
|
|
|
|
2018-09-26 14:32:23 +01:00
|
|
|
if eofReader.hasError() {
|
2018-10-19 15:36:43 +01:00
|
|
|
return Meta{}, currentSegment, eofReader.err
|
2018-09-08 16:41:40 +01:00
|
|
|
}
|
2018-08-24 04:56:38 +01:00
|
|
|
|
|
|
|
resultMeta := Meta{
|
2019-04-10 23:27:04 +01:00
|
|
|
Modified: putMeta.Modified,
|
|
|
|
Expiration: expiration,
|
|
|
|
Size: streamSize,
|
|
|
|
Data: metadata,
|
2018-08-24 04:56:38 +01:00
|
|
|
}
|
|
|
|
|
2018-10-19 15:36:43 +01:00
|
|
|
return resultMeta, currentSegment, nil
|
2018-08-24 04:56:38 +01:00
|
|
|
}
|
|
|
|
|
2018-10-03 14:05:40 +01:00
|
|
|
// getSegmentPath returns the unique path for a particular segment
|
2018-10-25 21:28:16 +01:00
|
|
|
func getSegmentPath(path storj.Path, segNum int64) storj.Path {
|
|
|
|
return storj.JoinPaths(fmt.Sprintf("s%d", segNum), path)
|
2018-09-26 14:32:23 +01:00
|
|
|
}
|
|
|
|
|
2018-08-24 04:56:38 +01:00
|
|
|
// Get returns a ranger that knows what the overall size is (from l/<path>)
|
|
|
|
// and then returns the appropriate data from segments s0/<path>, s1/<path>,
|
|
|
|
// ..., l/<path>.
|
2018-11-13 12:21:52 +00:00
|
|
|
func (s *streamStore) Get(ctx context.Context, path storj.Path, pathCipher storj.Cipher) (rr ranger.Ranger, meta Meta, err error) {
|
2018-08-24 04:56:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-06-04 12:36:27 +01:00
|
|
|
encPath, err := EncryptAfterBucket(ctx, path, pathCipher, s.rootKey)
|
2018-10-15 16:39:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, Meta{}, err
|
|
|
|
}
|
|
|
|
|
2018-10-25 21:28:16 +01:00
|
|
|
lastSegmentRanger, lastSegmentMeta, err := s.segments.Get(ctx, storj.JoinPaths("l", encPath))
|
2018-08-24 04:56:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, Meta{}, err
|
|
|
|
}
|
|
|
|
|
2019-04-02 22:15:31 +01:00
|
|
|
streamInfo, streamMeta, err := DecryptStreamInfo(ctx, lastSegmentMeta.Data, path, s.rootKey)
|
2018-08-24 04:56:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, Meta{}, err
|
|
|
|
}
|
|
|
|
|
2018-10-15 19:58:57 +01:00
|
|
|
stream := pb.StreamInfo{}
|
2018-10-17 12:34:50 +01:00
|
|
|
err = proto.Unmarshal(streamInfo, &stream)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Meta{}, err
|
|
|
|
}
|
|
|
|
|
2018-10-25 21:28:16 +01:00
|
|
|
derivedKey, err := encryption.DeriveContentKey(path, s.rootKey)
|
2018-09-26 14:32:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, Meta{}, err
|
|
|
|
}
|
2018-08-24 04:56:38 +01:00
|
|
|
|
2018-09-26 14:32:23 +01:00
|
|
|
var rangers []ranger.Ranger
|
2018-10-15 19:58:57 +01:00
|
|
|
for i := int64(0); i < stream.NumberOfSegments-1; i++ {
|
2018-10-15 16:39:09 +01:00
|
|
|
currentPath := getSegmentPath(encPath, i)
|
2018-10-15 19:58:57 +01:00
|
|
|
size := stream.SegmentsSize
|
2018-10-19 14:38:13 +01:00
|
|
|
var contentNonce storj.Nonce
|
|
|
|
_, err := encryption.Increment(&contentNonce, i+1)
|
2018-09-26 14:32:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, Meta{}, err
|
|
|
|
}
|
2018-09-12 14:06:44 +01:00
|
|
|
rr := &lazySegmentRanger{
|
2018-09-26 14:32:23 +01:00
|
|
|
segments: s.segments,
|
|
|
|
path: currentPath,
|
|
|
|
size: size,
|
2018-10-25 21:28:16 +01:00
|
|
|
derivedKey: derivedKey,
|
2018-10-17 12:34:50 +01:00
|
|
|
startingNonce: &contentNonce,
|
2018-10-15 19:58:57 +01:00
|
|
|
encBlockSize: int(streamMeta.EncryptionBlockSize),
|
2018-10-19 14:38:13 +01:00
|
|
|
cipher: storj.Cipher(streamMeta.EncryptionType),
|
2018-09-12 14:06:44 +01:00
|
|
|
}
|
|
|
|
rangers = append(rangers, rr)
|
2018-08-24 04:56:38 +01:00
|
|
|
}
|
|
|
|
|
2018-10-19 14:38:13 +01:00
|
|
|
var contentNonce storj.Nonce
|
|
|
|
_, err = encryption.Increment(&contentNonce, stream.NumberOfSegments)
|
2018-10-03 14:05:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, Meta{}, err
|
|
|
|
}
|
2018-10-15 19:58:57 +01:00
|
|
|
encryptedKey, keyNonce := getEncryptedKeyAndNonce(streamMeta.LastSegmentMeta)
|
2018-10-03 14:05:40 +01:00
|
|
|
decryptedLastSegmentRanger, err := decryptRanger(
|
|
|
|
ctx,
|
|
|
|
lastSegmentRanger,
|
2018-10-15 19:58:57 +01:00
|
|
|
stream.LastSegmentSize,
|
2018-10-19 14:38:13 +01:00
|
|
|
storj.Cipher(streamMeta.EncryptionType),
|
2018-10-25 21:28:16 +01:00
|
|
|
derivedKey,
|
2018-10-15 19:58:57 +01:00
|
|
|
encryptedKey,
|
|
|
|
keyNonce,
|
2018-10-17 12:34:50 +01:00
|
|
|
&contentNonce,
|
2018-10-15 19:58:57 +01:00
|
|
|
int(streamMeta.EncryptionBlockSize),
|
2018-10-03 14:05:40 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Meta{}, err
|
|
|
|
}
|
|
|
|
|
2019-04-02 22:15:31 +01:00
|
|
|
rangers = append(rangers, decryptedLastSegmentRanger)
|
2018-08-24 04:56:38 +01:00
|
|
|
catRangers := ranger.Concat(rangers...)
|
2019-04-02 22:15:31 +01:00
|
|
|
meta = convertMeta(lastSegmentMeta, stream, streamMeta)
|
2018-10-15 19:58:57 +01:00
|
|
|
return catRangers, meta, nil
|
2018-08-24 04:56:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Meta implements Store.Meta
|
2018-11-13 12:21:52 +00:00
|
|
|
func (s *streamStore) Meta(ctx context.Context, path storj.Path, pathCipher storj.Cipher) (meta Meta, err error) {
|
2018-10-15 16:39:09 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-06-04 12:36:27 +01:00
|
|
|
encPath, err := EncryptAfterBucket(ctx, path, pathCipher, s.rootKey)
|
2018-10-15 16:39:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return Meta{}, err
|
|
|
|
}
|
|
|
|
|
2018-10-25 21:28:16 +01:00
|
|
|
lastSegmentMeta, err := s.segments.Meta(ctx, storj.JoinPaths("l", encPath))
|
2018-08-24 04:56:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return Meta{}, err
|
|
|
|
}
|
|
|
|
|
2019-04-02 22:15:31 +01:00
|
|
|
streamInfo, streamMeta, err := DecryptStreamInfo(ctx, lastSegmentMeta.Data, path, s.rootKey)
|
2018-08-24 04:56:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return Meta{}, err
|
|
|
|
}
|
2019-04-02 22:15:31 +01:00
|
|
|
var stream pb.StreamInfo
|
|
|
|
if err := proto.Unmarshal(streamInfo, &stream); err != nil {
|
2018-10-17 12:34:50 +01:00
|
|
|
return Meta{}, err
|
|
|
|
}
|
|
|
|
|
2019-04-02 22:15:31 +01:00
|
|
|
return convertMeta(lastSegmentMeta, stream, streamMeta), nil
|
2018-08-24 04:56:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all the segments, with the last one last
|
2018-11-13 12:21:52 +00:00
|
|
|
func (s *streamStore) Delete(ctx context.Context, path storj.Path, pathCipher storj.Cipher) (err error) {
|
2018-08-24 04:56:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-06-04 12:36:27 +01:00
|
|
|
encPath, err := EncryptAfterBucket(ctx, path, pathCipher, s.rootKey)
|
2018-10-15 16:39:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-25 21:28:16 +01:00
|
|
|
lastSegmentMeta, err := s.segments.Meta(ctx, storj.JoinPaths("l", encPath))
|
2018-08-24 04:56:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-02 22:15:31 +01:00
|
|
|
streamInfo, _, err := DecryptStreamInfo(ctx, lastSegmentMeta.Data, path, s.rootKey)
|
2018-10-15 19:58:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-02 22:15:31 +01:00
|
|
|
var stream pb.StreamInfo
|
|
|
|
if err := proto.Unmarshal(streamInfo, &stream); err != nil {
|
2018-08-24 04:56:38 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-15 19:58:57 +01:00
|
|
|
for i := 0; i < int(stream.NumberOfSegments-1); i++ {
|
2019-06-04 12:36:27 +01:00
|
|
|
encPath, err = EncryptAfterBucket(ctx, path, pathCipher, s.rootKey)
|
2018-10-15 16:39:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
currentPath := getSegmentPath(encPath, int64(i))
|
2018-09-26 14:32:23 +01:00
|
|
|
err := s.segments.Delete(ctx, currentPath)
|
2018-08-24 04:56:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-25 21:28:16 +01:00
|
|
|
return s.segments.Delete(ctx, storj.JoinPaths("l", encPath))
|
2018-08-24 04:56:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListItem is a single item in a listing
|
|
|
|
type ListItem struct {
|
2018-10-25 21:28:16 +01:00
|
|
|
Path storj.Path
|
2018-09-07 15:20:15 +01:00
|
|
|
Meta Meta
|
|
|
|
IsPrefix bool
|
2018-08-24 04:56:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// List all the paths inside l/, stripping off the l/ prefix
|
2018-11-13 12:21:52 +00:00
|
|
|
func (s *streamStore) List(ctx context.Context, prefix, startAfter, endBefore storj.Path, pathCipher storj.Cipher, recursive bool, limit int, metaFlags uint32) (items []ListItem, more bool, err error) {
|
2018-08-24 04:56:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2018-09-12 14:03:31 +01:00
|
|
|
if metaFlags&meta.Size != 0 {
|
|
|
|
// Calculating the stream's size require also the user-defined metadata,
|
|
|
|
// where stream store keeps info about the number of segments and their size.
|
|
|
|
metaFlags |= meta.UserDefined
|
|
|
|
}
|
|
|
|
|
2018-10-25 21:28:16 +01:00
|
|
|
prefix = strings.TrimSuffix(prefix, "/")
|
|
|
|
|
2019-06-04 12:36:27 +01:00
|
|
|
encPrefix, err := EncryptAfterBucket(ctx, prefix, pathCipher, s.rootKey)
|
2018-10-15 16:39:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
2018-10-25 21:28:16 +01:00
|
|
|
prefixKey, err := encryption.DerivePathKey(prefix, s.rootKey, len(storj.SplitPath(prefix)))
|
2018-10-15 16:39:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
2019-06-04 12:36:27 +01:00
|
|
|
encStartAfter, err := s.encryptMarker(ctx, startAfter, pathCipher, prefixKey)
|
2018-10-15 16:39:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
2018-10-17 12:34:50 +01:00
|
|
|
|
2019-06-04 12:36:27 +01:00
|
|
|
encEndBefore, err := s.encryptMarker(ctx, endBefore, pathCipher, prefixKey)
|
2018-10-15 16:39:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
2018-10-25 21:28:16 +01:00
|
|
|
segments, more, err := s.segments.List(ctx, storj.JoinPaths("l", encPrefix), encStartAfter, encEndBefore, recursive, limit, metaFlags)
|
2018-08-24 04:56:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
2018-09-07 15:20:15 +01:00
|
|
|
items = make([]ListItem, len(segments))
|
|
|
|
for i, item := range segments {
|
2019-06-04 12:36:27 +01:00
|
|
|
path, err := s.decryptMarker(ctx, item.Path, pathCipher, prefixKey)
|
2018-08-24 04:56:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
2018-10-17 12:34:50 +01:00
|
|
|
|
2019-04-02 22:15:31 +01:00
|
|
|
streamInfo, streamMeta, err := DecryptStreamInfo(ctx, item.Meta.Data, storj.JoinPaths(prefix, path), s.rootKey)
|
2018-10-15 16:39:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
2019-04-02 22:15:31 +01:00
|
|
|
var stream pb.StreamInfo
|
|
|
|
if err := proto.Unmarshal(streamInfo, &stream); err != nil {
|
2018-10-17 12:34:50 +01:00
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
2019-04-02 22:15:31 +01:00
|
|
|
newMeta := convertMeta(item.Meta, stream, streamMeta)
|
2018-10-17 12:34:50 +01:00
|
|
|
items[i] = ListItem{Path: path, Meta: newMeta, IsPrefix: item.IsPrefix}
|
2018-08-24 04:56:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return items, more, nil
|
|
|
|
}
|
2018-09-12 14:06:44 +01:00
|
|
|
|
2018-10-15 16:39:09 +01:00
|
|
|
// encryptMarker is a helper method for encrypting startAfter and endBefore markers
|
2019-06-04 12:36:27 +01:00
|
|
|
func (s *streamStore) encryptMarker(ctx context.Context, marker storj.Path, pathCipher storj.Cipher, prefixKey *storj.Key) (_ storj.Path, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-10-25 21:28:16 +01:00
|
|
|
if bytes.Equal(s.rootKey[:], prefixKey[:]) { // empty prefix
|
2019-06-04 12:36:27 +01:00
|
|
|
return EncryptAfterBucket(ctx, marker, pathCipher, s.rootKey)
|
2018-10-15 16:39:09 +01:00
|
|
|
}
|
2018-11-13 12:21:52 +00:00
|
|
|
return encryption.EncryptPath(marker, pathCipher, prefixKey)
|
2018-10-15 16:39:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// decryptMarker is a helper method for decrypting listed path markers
|
2019-06-04 12:36:27 +01:00
|
|
|
func (s *streamStore) decryptMarker(ctx context.Context, marker storj.Path, pathCipher storj.Cipher, prefixKey *storj.Key) (_ storj.Path, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-10-25 21:28:16 +01:00
|
|
|
if bytes.Equal(s.rootKey[:], prefixKey[:]) { // empty prefix
|
2019-06-04 12:36:27 +01:00
|
|
|
return DecryptAfterBucket(ctx, marker, pathCipher, s.rootKey)
|
2018-10-15 16:39:09 +01:00
|
|
|
}
|
2018-11-13 12:21:52 +00:00
|
|
|
return encryption.DecryptPath(marker, pathCipher, prefixKey)
|
2018-10-15 16:39:09 +01:00
|
|
|
}
|
|
|
|
|
2018-09-12 14:06:44 +01:00
|
|
|
type lazySegmentRanger struct {
|
2018-09-26 14:32:23 +01:00
|
|
|
ranger ranger.Ranger
|
|
|
|
segments segments.Store
|
2018-10-25 21:28:16 +01:00
|
|
|
path storj.Path
|
2018-09-26 14:32:23 +01:00
|
|
|
size int64
|
2018-10-19 14:38:13 +01:00
|
|
|
derivedKey *storj.Key
|
|
|
|
startingNonce *storj.Nonce
|
2018-09-26 14:32:23 +01:00
|
|
|
encBlockSize int
|
2018-10-19 14:38:13 +01:00
|
|
|
cipher storj.Cipher
|
2018-09-12 14:06:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Size implements Ranger.Size
|
|
|
|
func (lr *lazySegmentRanger) Size() int64 {
|
|
|
|
return lr.size
|
|
|
|
}
|
|
|
|
|
|
|
|
// Range implements Ranger.Range to be lazily connected
|
2019-06-04 12:36:27 +01:00
|
|
|
func (lr *lazySegmentRanger) Range(ctx context.Context, offset, length int64) (_ io.ReadCloser, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-09-12 14:06:44 +01:00
|
|
|
if lr.ranger == nil {
|
2018-09-26 14:32:23 +01:00
|
|
|
rr, m, err := lr.segments.Get(ctx, lr.path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-15 19:58:57 +01:00
|
|
|
segmentMeta := pb.SegmentMeta{}
|
|
|
|
err = proto.Unmarshal(m.Data, &segmentMeta)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
encryptedKey, keyNonce := getEncryptedKeyAndNonce(&segmentMeta)
|
2018-10-19 14:38:13 +01:00
|
|
|
lr.ranger, err = decryptRanger(ctx, rr, lr.size, lr.cipher, lr.derivedKey, encryptedKey, keyNonce, lr.startingNonce, lr.encBlockSize)
|
2018-09-26 14:32:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-03 14:05:40 +01:00
|
|
|
}
|
|
|
|
return lr.ranger.Range(ctx, offset, length)
|
|
|
|
}
|
|
|
|
|
|
|
|
// decryptRanger returns a decrypted ranger of the given rr ranger
|
2019-04-10 23:27:04 +01:00
|
|
|
func decryptRanger(ctx context.Context, rr ranger.Ranger, decryptedSize int64, cipher storj.Cipher, derivedKey *storj.Key, encryptedKey storj.EncryptedPrivateKey, encryptedKeyNonce, startingNonce *storj.Nonce, encBlockSize int) (decrypted ranger.Ranger, err error) {
|
2019-06-04 12:36:27 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-10-19 14:38:13 +01:00
|
|
|
contentKey, err := encryption.DecryptKey(encryptedKey, cipher, derivedKey, encryptedKeyNonce)
|
2018-10-03 14:05:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-19 14:38:13 +01:00
|
|
|
|
|
|
|
decrypter, err := encryption.NewDecrypter(cipher, contentKey, startingNonce, encBlockSize)
|
2018-10-03 14:05:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var rd ranger.Ranger
|
|
|
|
if rr.Size()%int64(decrypter.InBlockSize()) != 0 {
|
|
|
|
reader, err := rr.Range(ctx, 0, rr.Size())
|
2018-09-12 14:06:44 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-10 23:27:04 +01:00
|
|
|
defer func() { err = errs.Combine(err, reader.Close()) }()
|
2018-10-03 14:05:40 +01:00
|
|
|
cipherData, err := ioutil.ReadAll(reader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-09-26 14:32:23 +01:00
|
|
|
}
|
2018-10-19 14:38:13 +01:00
|
|
|
data, err := encryption.Decrypt(cipherData, cipher, contentKey, startingNonce)
|
2018-10-03 14:05:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ranger.ByteRanger(data), nil
|
2018-09-12 14:06:44 +01:00
|
|
|
}
|
2018-10-03 14:05:40 +01:00
|
|
|
|
2018-10-18 12:10:29 +01:00
|
|
|
rd, err = encryption.Transform(rr, decrypter)
|
2018-10-03 14:05:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return eestream.Unpad(rd, int(rd.Size()-decryptedSize))
|
2018-09-12 14:06:44 +01:00
|
|
|
}
|
2018-10-04 14:52:12 +01:00
|
|
|
|
2018-11-06 11:40:06 +00:00
|
|
|
// EncryptAfterBucket encrypts a path without encrypting its first element
|
2019-06-04 12:36:27 +01:00
|
|
|
func EncryptAfterBucket(ctx context.Context, path storj.Path, cipher storj.Cipher, key *storj.Key) (encrypted storj.Path, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-10-25 21:28:16 +01:00
|
|
|
comps := storj.SplitPath(path)
|
|
|
|
if len(comps) <= 1 {
|
|
|
|
return path, nil
|
2018-10-15 16:39:09 +01:00
|
|
|
}
|
|
|
|
|
2018-11-13 12:21:52 +00:00
|
|
|
encrypted, err = encryption.EncryptPath(path, cipher, key)
|
2018-10-15 16:39:09 +01:00
|
|
|
if err != nil {
|
2018-10-25 21:28:16 +01:00
|
|
|
return "", err
|
2018-10-15 16:39:09 +01:00
|
|
|
}
|
2018-10-25 21:28:16 +01:00
|
|
|
|
|
|
|
// replace the first path component with the unencrypted bucket name
|
|
|
|
return storj.JoinPaths(comps[0], storj.JoinPaths(storj.SplitPath(encrypted)[1:]...)), nil
|
2018-10-15 16:39:09 +01:00
|
|
|
}
|
|
|
|
|
2018-11-06 11:40:06 +00:00
|
|
|
// DecryptAfterBucket decrypts a path without modifying its first element
|
2019-06-04 12:36:27 +01:00
|
|
|
func DecryptAfterBucket(ctx context.Context, path storj.Path, cipher storj.Cipher, key *storj.Key) (decrypted storj.Path, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-10-25 21:28:16 +01:00
|
|
|
comps := storj.SplitPath(path)
|
|
|
|
if len(comps) <= 1 {
|
|
|
|
return path, nil
|
2018-10-15 16:39:09 +01:00
|
|
|
}
|
|
|
|
|
2018-10-25 21:28:16 +01:00
|
|
|
bucket := comps[0]
|
|
|
|
toDecrypt := storj.JoinPaths(comps[1:]...)
|
|
|
|
|
|
|
|
bucketKey, err := encryption.DerivePathKey(path, key, 1)
|
2018-10-15 16:39:09 +01:00
|
|
|
if err != nil {
|
2018-10-25 21:28:16 +01:00
|
|
|
return "", err
|
2018-10-15 16:39:09 +01:00
|
|
|
}
|
2018-10-25 21:28:16 +01:00
|
|
|
|
2018-11-13 12:21:52 +00:00
|
|
|
decPath, err := encryption.DecryptPath(toDecrypt, cipher, bucketKey)
|
2018-10-15 16:39:09 +01:00
|
|
|
if err != nil {
|
2018-10-25 21:28:16 +01:00
|
|
|
return "", err
|
2018-10-15 16:39:09 +01:00
|
|
|
}
|
2018-10-25 21:28:16 +01:00
|
|
|
|
|
|
|
return storj.JoinPaths(bucket, decPath), nil
|
2018-10-15 16:39:09 +01:00
|
|
|
}
|
|
|
|
|
2018-10-04 14:52:12 +01:00
|
|
|
// CancelHandler handles clean up of segments on receiving CTRL+C
|
2018-11-13 12:21:52 +00:00
|
|
|
func (s *streamStore) cancelHandler(ctx context.Context, totalSegments int64, path storj.Path, pathCipher storj.Cipher) {
|
2019-06-04 12:36:27 +01:00
|
|
|
defer mon.Task()(&ctx)(nil)
|
2018-10-04 14:52:12 +01:00
|
|
|
for i := int64(0); i < totalSegments; i++ {
|
2019-06-04 12:36:27 +01:00
|
|
|
encPath, err := EncryptAfterBucket(ctx, path, pathCipher, s.rootKey)
|
2018-10-19 15:36:43 +01:00
|
|
|
if err != nil {
|
|
|
|
zap.S().Warnf("Failed deleting a segment due to encryption path %v %v", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
currentPath := getSegmentPath(encPath, i)
|
|
|
|
err = s.segments.Delete(ctx, currentPath)
|
2018-10-04 14:52:12 +01:00
|
|
|
if err != nil {
|
|
|
|
zap.S().Warnf("Failed deleting a segment %v %v", currentPath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-15 19:58:57 +01:00
|
|
|
|
2018-10-19 14:38:13 +01:00
|
|
|
func getEncryptedKeyAndNonce(m *pb.SegmentMeta) (storj.EncryptedPrivateKey, *storj.Nonce) {
|
2018-10-15 19:58:57 +01:00
|
|
|
if m == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-10-19 14:38:13 +01:00
|
|
|
var nonce storj.Nonce
|
2018-10-17 12:34:50 +01:00
|
|
|
copy(nonce[:], m.KeyNonce)
|
2018-10-15 19:58:57 +01:00
|
|
|
|
|
|
|
return m.EncryptedKey, &nonce
|
|
|
|
}
|
2018-10-17 12:34:50 +01:00
|
|
|
|
2018-11-06 11:40:06 +00:00
|
|
|
// DecryptStreamInfo decrypts stream info
|
2019-04-02 22:15:31 +01:00
|
|
|
func DecryptStreamInfo(ctx context.Context, streamMetaBytes []byte, path storj.Path, rootKey *storj.Key) (
|
|
|
|
streamInfo []byte, streamMeta pb.StreamMeta, err error) {
|
2019-06-04 12:36:27 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-04-02 22:15:31 +01:00
|
|
|
err = proto.Unmarshal(streamMetaBytes, &streamMeta)
|
2018-10-17 12:34:50 +01:00
|
|
|
if err != nil {
|
2019-04-02 22:15:31 +01:00
|
|
|
return nil, pb.StreamMeta{}, err
|
2018-10-17 12:34:50 +01:00
|
|
|
}
|
|
|
|
|
2018-10-25 21:28:16 +01:00
|
|
|
derivedKey, err := encryption.DeriveContentKey(path, rootKey)
|
2018-10-17 12:34:50 +01:00
|
|
|
if err != nil {
|
2019-04-02 22:15:31 +01:00
|
|
|
return nil, pb.StreamMeta{}, err
|
2018-10-17 12:34:50 +01:00
|
|
|
}
|
|
|
|
|
2018-10-19 14:38:13 +01:00
|
|
|
cipher := storj.Cipher(streamMeta.EncryptionType)
|
2018-10-17 12:34:50 +01:00
|
|
|
encryptedKey, keyNonce := getEncryptedKeyAndNonce(streamMeta.LastSegmentMeta)
|
2018-10-25 21:28:16 +01:00
|
|
|
contentKey, err := encryption.DecryptKey(encryptedKey, cipher, derivedKey, keyNonce)
|
2018-10-17 12:34:50 +01:00
|
|
|
if err != nil {
|
2019-04-02 22:15:31 +01:00
|
|
|
return nil, pb.StreamMeta{}, err
|
2018-10-17 12:34:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// decrypt metadata with the content encryption key and zero nonce
|
2019-04-02 22:15:31 +01:00
|
|
|
streamInfo, err = encryption.Decrypt(streamMeta.EncryptedStreamInfo, cipher, contentKey, &storj.Nonce{})
|
|
|
|
return streamInfo, streamMeta, err
|
2018-10-17 12:34:50 +01:00
|
|
|
}
|