2019-03-18 10:55:06 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package piecestore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2019-10-15 15:07:18 +01:00
|
|
|
"time"
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/identity"
|
|
|
|
"storj.io/storj/pkg/pb"
|
2019-07-28 06:55:36 +01:00
|
|
|
"storj.io/storj/pkg/signing"
|
2019-03-18 10:55:06 +00:00
|
|
|
)
|
|
|
|
|
2019-10-16 13:47:08 +01:00
|
|
|
const pieceHashExpiration = 24 * time.Hour
|
2019-10-15 15:07:18 +01:00
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
var (
|
|
|
|
// ErrInternal is an error class for internal errors.
|
|
|
|
ErrInternal = errs.Class("internal")
|
|
|
|
// ErrProtocol is an error class for unexpected protocol sequence.
|
|
|
|
ErrProtocol = errs.Class("protocol")
|
|
|
|
// ErrVerifyUntrusted is an error in case there is a trust issue.
|
|
|
|
ErrVerifyUntrusted = errs.Class("untrusted")
|
2019-10-15 15:07:18 +01:00
|
|
|
// ErrStorageNodeInvalidResponse is an error when a storage node returns a response with invalid data
|
|
|
|
ErrStorageNodeInvalidResponse = errs.Class("storage node has returned an invalid response")
|
2019-03-18 10:55:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// VerifyPieceHash verifies piece hash which is sent by peer.
|
2019-07-01 16:54:11 +01:00
|
|
|
func (client *Client) VerifyPieceHash(ctx context.Context, peer *identity.PeerIdentity, limit *pb.OrderLimit, hash *pb.PieceHash, expectedHash []byte) (err error) {
|
2019-06-05 16:03:11 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-03-18 10:55:06 +00:00
|
|
|
if peer == nil || limit == nil || hash == nil || len(expectedHash) == 0 {
|
|
|
|
return ErrProtocol.New("invalid arguments")
|
|
|
|
}
|
|
|
|
if limit.PieceId != hash.PieceId {
|
2019-09-19 05:46:39 +01:00
|
|
|
return ErrProtocol.New("piece id changed") // TODO: report rpc status bad message
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
if !bytes.Equal(hash.Hash, expectedHash) {
|
2019-09-19 05:46:39 +01:00
|
|
|
return ErrVerifyUntrusted.New("hashes don't match") // TODO: report rpc status bad message
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 14:47:01 +01:00
|
|
|
if err := signing.VerifyPieceHashSignature(ctx, signing.SigneeFromPeerIdentity(peer), hash); err != nil {
|
2019-09-19 05:46:39 +01:00
|
|
|
return ErrVerifyUntrusted.New("invalid hash signature: %v", err) // TODO: report rpc status bad message
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2019-10-15 15:07:18 +01:00
|
|
|
if hash.Timestamp.Before(time.Now().Add(-pieceHashExpiration)) {
|
|
|
|
return ErrStorageNodeInvalidResponse.New("piece has timestamp is too old (%v). Required to be not older than %s",
|
|
|
|
hash.Timestamp, pieceHashExpiration,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
return nil
|
|
|
|
}
|