2021-01-13 13:46:39 +00:00
|
|
|
package control
|
2021-01-13 12:49:46 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"errors"
|
2022-05-16 13:15:31 +00:00
|
|
|
"fmt"
|
2021-01-13 12:49:46 +00:00
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-api-go/v2/refs"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/services/control"
|
|
|
|
frostfscrypto "github.com/TrueCloudLab/frostfs-sdk-go/crypto"
|
|
|
|
frostfsecdsa "github.com/TrueCloudLab/frostfs-sdk-go/crypto/ecdsa"
|
2021-01-13 12:49:46 +00:00
|
|
|
)
|
|
|
|
|
2021-01-13 13:46:39 +00:00
|
|
|
// SignedMessage is an interface of Control service message.
|
2021-01-13 12:49:46 +00:00
|
|
|
type SignedMessage interface {
|
2022-05-16 13:15:31 +00:00
|
|
|
ReadSignedData([]byte) ([]byte, error)
|
2021-01-13 13:46:39 +00:00
|
|
|
GetSignature() *control.Signature
|
|
|
|
SetSignature(*control.Signature)
|
2021-01-13 12:49:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var errDisallowedKey = errors.New("key is not in the allowed list")
|
|
|
|
|
|
|
|
func (s *Server) isValidRequest(req SignedMessage) error {
|
2022-05-16 13:15:31 +00:00
|
|
|
sign := req.GetSignature()
|
|
|
|
if sign == nil {
|
|
|
|
// TODO(@cthulhu-rider): #1387 use "const" error
|
|
|
|
return errors.New("missing signature")
|
|
|
|
}
|
|
|
|
|
2021-01-13 12:49:46 +00:00
|
|
|
var (
|
|
|
|
key = sign.GetKey()
|
|
|
|
allowed = false
|
|
|
|
)
|
|
|
|
|
|
|
|
// check if key is allowed
|
|
|
|
for i := range s.allowedKeys {
|
|
|
|
if allowed = bytes.Equal(s.allowedKeys[i], key); allowed {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !allowed {
|
|
|
|
return errDisallowedKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify signature
|
2022-05-16 13:15:31 +00:00
|
|
|
binBody, err := req.ReadSignedData(nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("marshal request body: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(@cthulhu-rider): #1387 use Signature message from NeoFS API to avoid conversion
|
|
|
|
var sigV2 refs.Signature
|
|
|
|
sigV2.SetKey(sign.GetKey())
|
|
|
|
sigV2.SetSign(sign.GetSign())
|
|
|
|
sigV2.SetScheme(refs.ECDSA_SHA512)
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
var sig frostfscrypto.Signature
|
2022-07-22 14:04:37 +00:00
|
|
|
if err := sig.ReadFromV2(sigV2); err != nil {
|
|
|
|
return fmt.Errorf("can't read signature: %w", err)
|
|
|
|
}
|
2022-05-16 13:15:31 +00:00
|
|
|
|
|
|
|
if !sig.Verify(binBody) {
|
|
|
|
// TODO(@cthulhu-rider): #1387 use "const" error
|
|
|
|
return errors.New("invalid signature")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-01-13 12:49:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 13:46:39 +00:00
|
|
|
// SignMessage signs Control service message with private key.
|
2021-01-13 12:49:46 +00:00
|
|
|
func SignMessage(key *ecdsa.PrivateKey, msg SignedMessage) error {
|
2022-05-16 13:15:31 +00:00
|
|
|
binBody, err := msg.ReadSignedData(nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("marshal request body: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
var sig frostfscrypto.Signature
|
2022-05-16 13:15:31 +00:00
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
err = sig.Calculate(frostfsecdsa.Signer(*key), binBody)
|
2022-05-16 13:15:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("calculate signature: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(@cthulhu-rider): #1387 use Signature message from NeoFS API to avoid conversion
|
|
|
|
var sigV2 refs.Signature
|
|
|
|
sig.WriteToV2(&sigV2)
|
|
|
|
|
|
|
|
var sigControl control.Signature
|
|
|
|
sigControl.SetKey(sigV2.GetKey())
|
|
|
|
sigControl.SetSign(sigV2.GetSign())
|
|
|
|
|
|
|
|
msg.SetSignature(&sigControl)
|
2021-01-13 12:49:46 +00:00
|
|
|
|
2022-05-16 13:15:31 +00:00
|
|
|
return nil
|
2021-01-13 12:49:46 +00:00
|
|
|
}
|