2018-11-05 15:23:54 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package bwagreement
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-11-12 21:59:30 +00:00
|
|
|
"crypto"
|
|
|
|
"crypto/ecdsa"
|
2018-11-15 19:06:09 +00:00
|
|
|
"crypto/x509"
|
2018-11-05 15:23:54 +00:00
|
|
|
|
2018-11-12 21:59:30 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/gtank/cryptopasta"
|
2018-11-05 15:23:54 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2018-11-15 19:06:09 +00:00
|
|
|
"storj.io/storj/pkg/bwagreement/database-manager"
|
2018-11-05 15:23:54 +00:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-11-12 21:59:30 +00:00
|
|
|
"storj.io/storj/pkg/peertls"
|
2018-11-05 15:23:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Server is an implementation of the pb.BandwidthServer interface
|
|
|
|
type Server struct {
|
2018-11-15 19:06:09 +00:00
|
|
|
dbm *dbmanager.DBManager
|
2018-11-12 21:59:30 +00:00
|
|
|
pkey crypto.PublicKey
|
|
|
|
logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
2018-11-08 13:20:23 +00:00
|
|
|
// Agreement is a struct that contains a bandwidth agreement and the associated signature
|
|
|
|
type Agreement struct {
|
|
|
|
Agreement []byte
|
|
|
|
Signature []byte
|
|
|
|
}
|
|
|
|
|
2018-11-12 21:59:30 +00:00
|
|
|
// NewServer creates instance of Server
|
2018-11-15 19:06:09 +00:00
|
|
|
func NewServer(dbm *dbmanager.DBManager, logger *zap.Logger, pkey crypto.PublicKey) (*Server, error) {
|
2018-11-05 15:23:54 +00:00
|
|
|
return &Server{
|
2018-11-12 21:59:30 +00:00
|
|
|
dbm: dbm,
|
2018-11-05 15:23:54 +00:00
|
|
|
logger: logger,
|
2018-11-12 21:59:30 +00:00
|
|
|
pkey: pkey,
|
2018-11-05 15:23:54 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-11-15 19:06:09 +00:00
|
|
|
// BandwidthAgreements receives and stores bandwidth agreements from storage nodes
|
|
|
|
func (s *Server) BandwidthAgreements(ctx context.Context, agreement *pb.RenterBandwidthAllocation) (reply *pb.AgreementsSummary, err error) {
|
2018-11-05 15:23:54 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2018-11-15 19:06:09 +00:00
|
|
|
s.logger.Debug("Received Agreement...")
|
2018-11-05 15:23:54 +00:00
|
|
|
|
2018-11-15 19:06:09 +00:00
|
|
|
reply = &pb.AgreementsSummary{
|
|
|
|
Status: pb.AgreementsSummary_FAIL,
|
2018-11-05 15:23:54 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 19:06:09 +00:00
|
|
|
if err = s.verifySignature(ctx, agreement); err != nil {
|
|
|
|
return reply, err
|
|
|
|
}
|
2018-11-12 21:59:30 +00:00
|
|
|
|
2018-11-15 19:06:09 +00:00
|
|
|
_, err = s.dbm.Create(ctx, agreement)
|
|
|
|
if err != nil {
|
|
|
|
return reply, err
|
2018-11-05 15:23:54 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 19:06:09 +00:00
|
|
|
reply.Status = pb.AgreementsSummary_OK
|
|
|
|
|
|
|
|
s.logger.Debug("Stored Agreement...")
|
|
|
|
|
|
|
|
return reply, nil
|
2018-11-05 15:23:54 +00:00
|
|
|
}
|
2018-11-08 13:20:23 +00:00
|
|
|
|
2018-11-12 21:59:30 +00:00
|
|
|
func (s *Server) verifySignature(ctx context.Context, ba *pb.RenterBandwidthAllocation) error {
|
|
|
|
// TODO(security): detect replay attacks
|
2018-11-15 19:06:09 +00:00
|
|
|
|
|
|
|
//Deserealize RenterBandwidthAllocation.GetData() so we can get public key
|
|
|
|
rbad := &pb.RenterBandwidthAllocation_Data{}
|
|
|
|
if err := proto.Unmarshal(ba.GetData(), rbad); err != nil {
|
|
|
|
return BwAgreementError.New("Failed to unmarshal RenterBandwidthAllocation: %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract renter's public key from RenterBandwidthAllocation_Data
|
|
|
|
// TODO: Look this public key up in a database
|
|
|
|
pubkey, err := x509.ParsePKIXPublicKey(rbad.GetPubKey())
|
2018-11-12 21:59:30 +00:00
|
|
|
if err != nil {
|
2018-11-15 19:06:09 +00:00
|
|
|
return BwAgreementError.New("Failed to extract Public Key from RenterBandwidthAllocation: %+v", err)
|
2018-11-12 21:59:30 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 19:06:09 +00:00
|
|
|
// Typecast public key
|
|
|
|
k, ok := pubkey.(*ecdsa.PublicKey)
|
2018-11-12 21:59:30 +00:00
|
|
|
if !ok {
|
2018-11-15 19:06:09 +00:00
|
|
|
return peertls.ErrUnsupportedKey.New("%T", pubkey)
|
2018-11-12 21:59:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// verify Renter's (uplink) signature
|
|
|
|
if ok := cryptopasta.Verify(ba.GetData(), ba.GetSignature(), k); !ok {
|
|
|
|
return BwAgreementError.New("Failed to verify Renter's Signature")
|
|
|
|
}
|
|
|
|
|
|
|
|
k, ok = s.pkey.(*ecdsa.PublicKey)
|
|
|
|
if !ok {
|
|
|
|
return peertls.ErrUnsupportedKey.New("%T", s.pkey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify Payer's (satellite) signature
|
2018-11-15 19:06:09 +00:00
|
|
|
if ok := cryptopasta.Verify(rbad.GetPayerAllocation().GetData(), rbad.GetPayerAllocation().GetSignature(), k); !ok {
|
2018-11-12 21:59:30 +00:00
|
|
|
return BwAgreementError.New("Failed to verify Payer's Signature")
|
|
|
|
}
|
|
|
|
return nil
|
2018-11-08 13:20:23 +00:00
|
|
|
}
|