2021-01-13 13:46:39 +00:00
|
|
|
package control
|
2021-01-13 12:49:46 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/util/signature"
|
2021-01-13 13:46:39 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/control"
|
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 {
|
|
|
|
signature.DataSource
|
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 {
|
|
|
|
var (
|
|
|
|
sign = req.GetSignature()
|
|
|
|
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
|
|
|
|
return signature.VerifyDataWithSource(req, func() ([]byte, []byte) {
|
|
|
|
return key, sign.GetSign()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return signature.SignDataWithHandler(key, msg, func(key []byte, sig []byte) {
|
2021-01-13 13:46:39 +00:00
|
|
|
s := new(control.Signature)
|
2021-01-13 12:49:46 +00:00
|
|
|
s.SetKey(key)
|
|
|
|
s.SetSign(sig)
|
|
|
|
|
|
|
|
msg.SetSignature(s)
|
|
|
|
})
|
|
|
|
}
|