2022-05-23 16:26:27 +00:00
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
|
|
|
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
|
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
|
|
|
|
controlSvc "github.com/nspcc-dev/neofs-node/pkg/services/control/server"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/client"
|
|
|
|
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func signRequest(cmd *cobra.Command, pk *ecdsa.PrivateKey, req controlSvc.SignedMessage) {
|
|
|
|
err := controlSvc.SignMessage(pk, req)
|
|
|
|
common.ExitOnErr(cmd, "could not sign request: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyResponse(cmd *cobra.Command,
|
|
|
|
sigControl interface {
|
|
|
|
GetKey() []byte
|
|
|
|
GetSign() []byte
|
|
|
|
},
|
|
|
|
body interface {
|
2022-05-31 17:00:41 +00:00
|
|
|
StableMarshal([]byte) []byte
|
2022-05-23 16:26:27 +00:00
|
|
|
},
|
|
|
|
) {
|
|
|
|
if sigControl == nil {
|
|
|
|
common.ExitOnErr(cmd, "", errors.New("missing response signature"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(@cthulhu-rider): #1387 use Signature message from NeoFS API to avoid conversion
|
|
|
|
var sigV2 refs.Signature
|
|
|
|
sigV2.SetScheme(refs.ECDSA_SHA512)
|
|
|
|
sigV2.SetKey(sigControl.GetKey())
|
|
|
|
sigV2.SetSign(sigControl.GetSign())
|
|
|
|
|
|
|
|
var sig neofscrypto.Signature
|
2022-07-22 14:04:37 +00:00
|
|
|
common.ExitOnErr(cmd, "can't read signature: %w", sig.ReadFromV2(sigV2))
|
2022-05-23 16:26:27 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
if !sig.Verify(body.StableMarshal(nil)) {
|
2022-05-23 16:26:27 +00:00
|
|
|
common.ExitOnErr(cmd, "", errors.New("invalid response signature"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getClient(cmd *cobra.Command, pk *ecdsa.PrivateKey) *client.Client {
|
2022-05-24 08:38:45 +00:00
|
|
|
return internalclient.GetSDKClientByFlag(cmd, pk, controlRPC)
|
2022-05-23 16:26:27 +00:00
|
|
|
}
|