2021-01-13 13:46:39 +00:00
|
|
|
package control
|
2021-01-13 12:49:46 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2022-05-16 13:15:31 +00:00
|
|
|
"fmt"
|
2021-01-13 12:49:46 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs"
|
2024-05-16 09:11:57 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control/server/ctrlmessage"
|
2023-03-07 13:38:26 +00:00
|
|
|
frostfscrypto "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto"
|
2021-01-13 12:49:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var errDisallowedKey = errors.New("key is not in the allowed list")
|
|
|
|
|
2024-05-16 09:11:57 +00:00
|
|
|
func (s *Server) isValidRequest(req ctrlmessage.SignedMessage) error {
|
2022-05-16 13:15:31 +00:00
|
|
|
sign := req.GetSignature()
|
|
|
|
if sign == nil {
|
2023-06-26 13:18:39 +00:00
|
|
|
// TODO(@cthulhu-rider): #468 use "const" error
|
2022-05-16 13:15:31 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-06-26 13:18:39 +00:00
|
|
|
// TODO(@cthulhu-rider): #468 use Signature message from FrostFS API to avoid conversion
|
2022-05-16 13:15:31 +00:00
|
|
|
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) {
|
2023-06-26 13:18:39 +00:00
|
|
|
// TODO(@cthulhu-rider): #468 use "const" error
|
2022-05-16 13:15:31 +00:00
|
|
|
return errors.New("invalid signature")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-01-13 12:49:46 +00:00
|
|
|
}
|