2019-03-18 10:55:06 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package piecestore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/pb"
|
2019-09-19 05:46:39 +01:00
|
|
|
"storj.io/storj/pkg/rpc/rpcstatus"
|
2019-07-28 06:55:36 +01:00
|
|
|
"storj.io/storj/pkg/signing"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/errs2"
|
2019-03-18 10:55:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrVerifyNotAuthorized is returned when the one submitting the action is not authorized to perform that action.
|
|
|
|
ErrVerifyNotAuthorized = errs.Class("not authorized")
|
|
|
|
// ErrVerifyUntrusted is returned when action is not trusted.
|
|
|
|
ErrVerifyUntrusted = errs.Class("untrusted")
|
|
|
|
// ErrVerifyDuplicateRequest is returned when serial number has been already used to submit an action.
|
|
|
|
ErrVerifyDuplicateRequest = errs.Class("duplicate request")
|
|
|
|
)
|
|
|
|
|
|
|
|
// VerifyOrderLimit verifies that the order limit is properly signed and has sane values.
|
|
|
|
// It also verifies that the serial number has not been used.
|
2019-07-30 17:58:08 +01:00
|
|
|
func (endpoint *Endpoint) verifyOrderLimit(ctx context.Context, limit *pb.OrderLimit) (err error) {
|
2019-06-04 13:31:39 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
// sanity checks
|
2019-07-09 22:16:30 +01:00
|
|
|
now := time.Now()
|
2019-03-18 10:55:06 +00:00
|
|
|
switch {
|
|
|
|
case limit.Limit < 0:
|
2019-09-19 05:46:39 +01:00
|
|
|
return rpcstatus.Error(rpcstatus.InvalidArgument, "order limit is negative")
|
2019-03-18 10:55:06 +00:00
|
|
|
case endpoint.signer.ID() != limit.StorageNodeId:
|
2019-09-19 05:46:39 +01:00
|
|
|
return rpcstatus.Errorf(rpcstatus.InvalidArgument, "order intended for other storagenode: %v", limit.StorageNodeId)
|
2019-07-09 22:54:00 +01:00
|
|
|
case endpoint.IsExpired(limit.PieceExpiration):
|
2019-09-19 05:46:39 +01:00
|
|
|
return rpcstatus.Errorf(rpcstatus.InvalidArgument, "piece expired: %v", limit.PieceExpiration)
|
2019-03-18 10:55:06 +00:00
|
|
|
case endpoint.IsExpired(limit.OrderExpiration):
|
2019-09-19 05:46:39 +01:00
|
|
|
return rpcstatus.Errorf(rpcstatus.InvalidArgument, "order expired: %v", limit.OrderExpiration)
|
2019-07-09 22:16:30 +01:00
|
|
|
case now.Sub(limit.OrderCreation) > endpoint.config.OrderLimitGracePeriod:
|
2019-10-08 12:09:46 +01:00
|
|
|
return rpcstatus.Errorf(rpcstatus.InvalidArgument, "order created too long ago: OrderCreation %v < SystemClock %v", limit.OrderCreation, now)
|
|
|
|
case limit.OrderCreation.Sub(now) > endpoint.config.OrderLimitGracePeriod:
|
|
|
|
return rpcstatus.Errorf(rpcstatus.InvalidArgument, "order created too far in the future: OrderCreation %v > SystemClock %v", limit.OrderCreation, now)
|
2019-03-18 10:55:06 +00:00
|
|
|
case limit.SatelliteId.IsZero():
|
2019-09-19 05:46:39 +01:00
|
|
|
return rpcstatus.Errorf(rpcstatus.InvalidArgument, "missing satellite id")
|
2019-07-11 21:51:40 +01:00
|
|
|
case limit.UplinkPublicKey.IsZero():
|
2019-09-19 05:46:39 +01:00
|
|
|
return rpcstatus.Errorf(rpcstatus.InvalidArgument, "missing uplink public key")
|
2019-03-18 10:55:06 +00:00
|
|
|
case len(limit.SatelliteSignature) == 0:
|
2019-09-19 05:46:39 +01:00
|
|
|
return rpcstatus.Errorf(rpcstatus.InvalidArgument, "missing satellite signature")
|
2019-03-20 21:12:00 +00:00
|
|
|
case limit.PieceId.IsZero():
|
2019-09-19 05:46:39 +01:00
|
|
|
return rpcstatus.Errorf(rpcstatus.InvalidArgument, "missing piece id")
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := endpoint.trust.VerifySatelliteID(ctx, limit.SatelliteId); err != nil {
|
2019-09-19 05:46:39 +01:00
|
|
|
return rpcstatus.Errorf(rpcstatus.PermissionDenied, "untrusted: %+v", err)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := endpoint.VerifyOrderLimitSignature(ctx, limit); err != nil {
|
2019-07-30 17:58:08 +01:00
|
|
|
if errs2.IsCanceled(err) {
|
2019-09-19 05:46:39 +01:00
|
|
|
return rpcstatus.Error(rpcstatus.Canceled, "context has been canceled")
|
2019-04-12 15:28:27 +01:00
|
|
|
}
|
2019-07-30 17:58:08 +01:00
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
return rpcstatus.Errorf(rpcstatus.Unauthenticated, "untrusted: %+v", err)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 22:54:00 +01:00
|
|
|
serialExpiration := limit.OrderExpiration
|
2019-07-09 22:16:30 +01:00
|
|
|
|
|
|
|
// Expire the serial earlier if the grace period is smaller than the serial expiration.
|
|
|
|
if graceExpiration := now.Add(endpoint.config.OrderLimitGracePeriod); graceExpiration.Before(serialExpiration) {
|
|
|
|
serialExpiration = graceExpiration
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
if err := endpoint.usedSerials.Add(ctx, limit.SatelliteId, limit.SerialNumber, serialExpiration); err != nil {
|
2019-09-19 05:46:39 +01:00
|
|
|
return rpcstatus.Errorf(rpcstatus.Unauthenticated, "serial number is already used: %+v", err)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyOrder verifies that the order corresponds to the order limit and has all the necessary fields.
|
2019-07-11 21:51:40 +01:00
|
|
|
func (endpoint *Endpoint) VerifyOrder(ctx context.Context, limit *pb.OrderLimit, order *pb.Order, largestOrderAmount int64) (err error) {
|
2019-06-04 13:31:39 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-03-18 13:08:24 +00:00
|
|
|
if order.SerialNumber != limit.SerialNumber {
|
2019-09-19 05:46:39 +01:00
|
|
|
return ErrProtocol.New("order serial number changed during upload") // TODO: report rpc status bad message
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
// TODO: add check for minimum allocation step
|
|
|
|
if order.Amount < largestOrderAmount {
|
2019-09-19 05:46:39 +01:00
|
|
|
return ErrProtocol.New("order contained smaller amount=%v, previous=%v", order.Amount, largestOrderAmount) // TODO: report rpc status bad message
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
if order.Amount > limit.Limit {
|
2019-09-19 05:46:39 +01:00
|
|
|
return ErrProtocol.New("order exceeded allowed amount=%v, limit=%v", order.Amount, limit.Limit) // TODO: report rpc status bad message
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 21:51:40 +01:00
|
|
|
if err := signing.VerifyUplinkOrderSignature(ctx, limit.UplinkPublicKey, order); err != nil {
|
|
|
|
return ErrVerifyUntrusted.Wrap(err)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyPieceHash verifies whether the piece hash is properly signed and matches the locally computed hash.
|
2019-07-11 21:51:40 +01:00
|
|
|
func (endpoint *Endpoint) VerifyPieceHash(ctx context.Context, limit *pb.OrderLimit, hash *pb.PieceHash, expectedHash []byte) (err error) {
|
2019-06-04 13:31:39 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-07-11 21:51:40 +01:00
|
|
|
if limit == nil || hash == nil || len(expectedHash) == 0 {
|
2019-03-18 10:55:06 +00:00
|
|
|
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 ErrProtocol.New("hashes don't match") // TODO: report rpc status bad message
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 21:51:40 +01:00
|
|
|
if err := signing.VerifyUplinkPieceHashSignature(ctx, limit.UplinkPublicKey, hash); err != nil {
|
2019-09-19 05:46:39 +01:00
|
|
|
return ErrVerifyUntrusted.New("invalid piece hash signature") // TODO: report rpc status bad message
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyOrderLimitSignature verifies that the order limit signature is valid.
|
2019-07-01 16:54:11 +01:00
|
|
|
func (endpoint *Endpoint) VerifyOrderLimitSignature(ctx context.Context, limit *pb.OrderLimit) (err error) {
|
2019-06-04 13:31:39 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
signee, err := endpoint.trust.GetSignee(ctx, limit.SatelliteId)
|
|
|
|
if err != nil {
|
2019-06-26 16:30:37 +01:00
|
|
|
if errs2.IsCanceled(err) {
|
2019-04-12 15:28:27 +01:00
|
|
|
return err
|
|
|
|
}
|
2019-09-19 05:46:39 +01:00
|
|
|
return ErrVerifyUntrusted.New("unable to get signee: %v", err) // 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.VerifyOrderLimitSignature(ctx, signee, limit); err != nil {
|
2019-09-19 05:46:39 +01:00
|
|
|
return ErrVerifyUntrusted.New("invalid order limit signature: %v", err) // TODO: report rpc status bad message
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsExpired checks whether the date has already expired (with a threshold) at the time of calling this function.
|
2019-07-09 22:54:00 +01:00
|
|
|
func (endpoint *Endpoint) IsExpired(expiration time.Time) bool {
|
|
|
|
if expiration.IsZero() {
|
|
|
|
return false
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: return specific error about either exceeding the expiration completely or just the grace period
|
2019-07-09 22:54:00 +01:00
|
|
|
return expiration.Before(time.Now().Add(-endpoint.config.ExpirationGracePeriod))
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|