2020-08-14 12:51:10 +00:00
|
|
|
package signature
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2021-05-17 12:56:17 +00:00
|
|
|
"errors"
|
2020-08-14 12:51:10 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-08-14 16:00:16 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/accounting"
|
2020-08-19 13:06:03 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/container"
|
2020-10-08 11:22:28 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/netmap"
|
2020-08-19 13:06:03 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/object"
|
2020-08-20 09:43:47 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2021-03-24 08:10:36 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/reputation"
|
2020-08-19 13:06:03 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/session"
|
2021-11-16 17:30:55 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/util/signature"
|
2020-08-14 12:51:10 +00:00
|
|
|
)
|
|
|
|
|
2020-08-18 13:01:40 +00:00
|
|
|
type serviceRequest interface {
|
2020-08-20 10:41:55 +00:00
|
|
|
GetMetaHeader() *session.RequestMetaHeader
|
|
|
|
GetVerificationHeader() *session.RequestVerificationHeader
|
|
|
|
SetVerificationHeader(*session.RequestVerificationHeader)
|
2020-08-14 12:51:10 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 13:01:40 +00:00
|
|
|
type serviceResponse interface {
|
2020-08-20 10:41:55 +00:00
|
|
|
GetMetaHeader() *session.ResponseMetaHeader
|
|
|
|
GetVerificationHeader() *session.ResponseVerificationHeader
|
|
|
|
SetVerificationHeader(*session.ResponseVerificationHeader)
|
2020-08-18 13:01:40 +00:00
|
|
|
}
|
|
|
|
|
2020-08-14 12:51:10 +00:00
|
|
|
type stableMarshaler interface {
|
2022-04-05 08:24:34 +00:00
|
|
|
StableMarshal([]byte) []byte
|
2020-08-14 12:51:10 +00:00
|
|
|
StableSize() int
|
|
|
|
}
|
|
|
|
|
2020-08-25 11:47:55 +00:00
|
|
|
type StableMarshalerWrapper struct {
|
|
|
|
SM stableMarshaler
|
2020-08-14 12:51:10 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 13:01:40 +00:00
|
|
|
type metaHeader interface {
|
|
|
|
stableMarshaler
|
|
|
|
getOrigin() metaHeader
|
|
|
|
}
|
|
|
|
|
|
|
|
type verificationHeader interface {
|
|
|
|
stableMarshaler
|
|
|
|
|
2020-08-20 09:43:47 +00:00
|
|
|
GetBodySignature() *refs.Signature
|
|
|
|
SetBodySignature(*refs.Signature)
|
|
|
|
GetMetaSignature() *refs.Signature
|
|
|
|
SetMetaSignature(*refs.Signature)
|
|
|
|
GetOriginSignature() *refs.Signature
|
|
|
|
SetOriginSignature(*refs.Signature)
|
2020-08-18 13:01:40 +00:00
|
|
|
|
|
|
|
setOrigin(stableMarshaler)
|
|
|
|
getOrigin() verificationHeader
|
|
|
|
}
|
|
|
|
|
|
|
|
type requestMetaHeader struct {
|
2020-08-20 10:41:55 +00:00
|
|
|
*session.RequestMetaHeader
|
2020-08-18 13:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type responseMetaHeader struct {
|
2020-08-20 10:41:55 +00:00
|
|
|
*session.ResponseMetaHeader
|
2020-08-18 13:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type requestVerificationHeader struct {
|
2020-08-20 10:41:55 +00:00
|
|
|
*session.RequestVerificationHeader
|
2020-08-18 13:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type responseVerificationHeader struct {
|
2020-08-20 10:41:55 +00:00
|
|
|
*session.ResponseVerificationHeader
|
2020-08-18 13:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *requestMetaHeader) getOrigin() metaHeader {
|
|
|
|
return &requestMetaHeader{
|
|
|
|
RequestMetaHeader: h.GetOrigin(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *responseMetaHeader) getOrigin() metaHeader {
|
|
|
|
return &responseMetaHeader{
|
|
|
|
ResponseMetaHeader: h.GetOrigin(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *requestVerificationHeader) getOrigin() verificationHeader {
|
|
|
|
if origin := h.GetOrigin(); origin != nil {
|
|
|
|
return &requestVerificationHeader{
|
|
|
|
RequestVerificationHeader: origin,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *requestVerificationHeader) setOrigin(m stableMarshaler) {
|
|
|
|
if m != nil {
|
2020-08-20 10:41:55 +00:00
|
|
|
h.SetOrigin(m.(*session.RequestVerificationHeader))
|
2020-08-18 13:01:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *responseVerificationHeader) getOrigin() verificationHeader {
|
|
|
|
if origin := r.GetOrigin(); origin != nil {
|
|
|
|
return &responseVerificationHeader{
|
|
|
|
ResponseVerificationHeader: origin,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *responseVerificationHeader) setOrigin(m stableMarshaler) {
|
|
|
|
if m != nil {
|
2020-08-20 10:41:55 +00:00
|
|
|
r.SetOrigin(m.(*session.ResponseVerificationHeader))
|
2020-08-18 13:01:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-25 11:47:55 +00:00
|
|
|
func (s StableMarshalerWrapper) ReadSignedData(buf []byte) ([]byte, error) {
|
|
|
|
if s.SM != nil {
|
2022-04-05 08:24:34 +00:00
|
|
|
return s.SM.StableMarshal(buf), nil
|
2020-08-18 13:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
2020-08-14 12:51:10 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 11:47:55 +00:00
|
|
|
func (s StableMarshalerWrapper) SignedDataSize() int {
|
|
|
|
if s.SM != nil {
|
|
|
|
return s.SM.StableSize()
|
2020-08-18 13:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
2020-08-14 12:51:10 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 13:01:40 +00:00
|
|
|
func SignServiceMessage(key *ecdsa.PrivateKey, msg interface{}) error {
|
|
|
|
var (
|
|
|
|
body, meta, verifyOrigin stableMarshaler
|
|
|
|
verifyHdr verificationHeader
|
|
|
|
verifyHdrSetter func(verificationHeader)
|
|
|
|
)
|
2020-08-14 12:51:10 +00:00
|
|
|
|
2020-08-18 13:01:40 +00:00
|
|
|
switch v := msg.(type) {
|
|
|
|
case nil:
|
2020-08-14 12:51:10 +00:00
|
|
|
return nil
|
2020-08-18 13:01:40 +00:00
|
|
|
case serviceRequest:
|
|
|
|
body = serviceMessageBody(v)
|
|
|
|
meta = v.GetMetaHeader()
|
2020-08-20 10:41:55 +00:00
|
|
|
verifyHdr = &requestVerificationHeader{new(session.RequestVerificationHeader)}
|
2020-08-18 13:01:40 +00:00
|
|
|
verifyHdrSetter = func(h verificationHeader) {
|
|
|
|
v.SetVerificationHeader(h.(*requestVerificationHeader).RequestVerificationHeader)
|
|
|
|
}
|
2020-08-14 12:51:10 +00:00
|
|
|
|
2020-08-18 13:01:40 +00:00
|
|
|
if h := v.GetVerificationHeader(); h != nil {
|
|
|
|
verifyOrigin = h
|
|
|
|
}
|
|
|
|
case serviceResponse:
|
|
|
|
body = serviceMessageBody(v)
|
|
|
|
meta = v.GetMetaHeader()
|
2020-08-20 10:41:55 +00:00
|
|
|
verifyHdr = &responseVerificationHeader{new(session.ResponseVerificationHeader)}
|
2020-08-18 13:01:40 +00:00
|
|
|
verifyHdrSetter = func(h verificationHeader) {
|
|
|
|
v.SetVerificationHeader(h.(*responseVerificationHeader).ResponseVerificationHeader)
|
|
|
|
}
|
2020-08-14 12:51:10 +00:00
|
|
|
|
2020-08-18 13:01:40 +00:00
|
|
|
if h := v.GetVerificationHeader(); h != nil {
|
|
|
|
verifyOrigin = h
|
|
|
|
}
|
|
|
|
default:
|
2020-08-20 10:41:55 +00:00
|
|
|
panic(fmt.Sprintf("unsupported session message %T", v))
|
2020-08-18 13:01:40 +00:00
|
|
|
}
|
2020-08-14 12:51:10 +00:00
|
|
|
|
2020-08-18 13:01:40 +00:00
|
|
|
if verifyOrigin == nil {
|
2020-08-20 10:41:55 +00:00
|
|
|
// sign session message body
|
2020-08-18 13:01:40 +00:00
|
|
|
if err := signServiceMessagePart(key, body, verifyHdr.SetBodySignature); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return fmt.Errorf("could not sign body: %w", err)
|
2020-08-18 13:01:40 +00:00
|
|
|
}
|
2020-08-14 12:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// sign meta header
|
2020-08-18 13:01:40 +00:00
|
|
|
if err := signServiceMessagePart(key, meta, verifyHdr.SetMetaSignature); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return fmt.Errorf("could not sign meta header: %w", err)
|
2020-08-14 12:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// sign verification header origin
|
2020-08-18 13:01:40 +00:00
|
|
|
if err := signServiceMessagePart(key, verifyOrigin, verifyHdr.SetOriginSignature); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return fmt.Errorf("could not sign origin of verification header: %w", err)
|
2020-08-14 12:51:10 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 13:01:40 +00:00
|
|
|
// wrap origin verification header
|
|
|
|
verifyHdr.setOrigin(verifyOrigin)
|
|
|
|
|
|
|
|
// update matryoshka verification header
|
|
|
|
verifyHdrSetter(verifyHdr)
|
2020-08-14 12:51:10 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-20 09:43:47 +00:00
|
|
|
func signServiceMessagePart(key *ecdsa.PrivateKey, part stableMarshaler, sigWrite func(*refs.Signature)) error {
|
|
|
|
sig := new(refs.Signature)
|
2020-08-14 12:51:10 +00:00
|
|
|
|
|
|
|
// sign part
|
|
|
|
if err := signature.SignDataWithHandler(
|
|
|
|
key,
|
2020-08-25 11:47:55 +00:00
|
|
|
&StableMarshalerWrapper{part},
|
2022-02-22 11:25:43 +00:00
|
|
|
func(s *refs.Signature) {
|
|
|
|
*sig = *s
|
|
|
|
},
|
2020-08-14 12:51:10 +00:00
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// write part signature
|
|
|
|
sigWrite(sig)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-18 13:01:40 +00:00
|
|
|
func VerifyServiceMessage(msg interface{}) error {
|
|
|
|
var (
|
|
|
|
meta metaHeader
|
|
|
|
verify verificationHeader
|
|
|
|
)
|
|
|
|
|
|
|
|
switch v := msg.(type) {
|
|
|
|
case nil:
|
|
|
|
return nil
|
|
|
|
case serviceRequest:
|
|
|
|
meta = &requestMetaHeader{
|
|
|
|
RequestMetaHeader: v.GetMetaHeader(),
|
|
|
|
}
|
|
|
|
|
|
|
|
verify = &requestVerificationHeader{
|
|
|
|
RequestVerificationHeader: v.GetVerificationHeader(),
|
|
|
|
}
|
|
|
|
case serviceResponse:
|
|
|
|
meta = &responseMetaHeader{
|
|
|
|
ResponseMetaHeader: v.GetMetaHeader(),
|
|
|
|
}
|
|
|
|
|
|
|
|
verify = &responseVerificationHeader{
|
|
|
|
ResponseVerificationHeader: v.GetVerificationHeader(),
|
|
|
|
}
|
|
|
|
default:
|
2020-08-20 10:41:55 +00:00
|
|
|
panic(fmt.Sprintf("unsupported session message %T", v))
|
2020-08-18 13:01:40 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 11:23:10 +00:00
|
|
|
body := serviceMessageBody(msg)
|
|
|
|
size := body.StableSize()
|
|
|
|
if sz := meta.StableSize(); sz > size {
|
|
|
|
size = sz
|
|
|
|
}
|
|
|
|
if sz := verify.StableSize(); sz > size {
|
|
|
|
size = sz
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, 0, size)
|
|
|
|
return verifyMatryoshkaLevel(body, meta, verify, buf)
|
2020-08-18 13:01:40 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 11:23:10 +00:00
|
|
|
func verifyMatryoshkaLevel(body stableMarshaler, meta metaHeader, verify verificationHeader, buf []byte) error {
|
|
|
|
if err := verifyServiceMessagePart(meta, verify.GetMetaSignature, buf); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return fmt.Errorf("could not verify meta header: %w", err)
|
2020-08-18 13:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
origin := verify.getOrigin()
|
2020-08-14 12:51:10 +00:00
|
|
|
|
2022-04-11 11:23:10 +00:00
|
|
|
if err := verifyServiceMessagePart(origin, verify.GetOriginSignature, buf); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return fmt.Errorf("could not verify origin of verification header: %w", err)
|
2020-08-14 12:51:10 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 13:01:40 +00:00
|
|
|
if origin == nil {
|
2022-04-11 11:23:10 +00:00
|
|
|
if err := verifyServiceMessagePart(body, verify.GetBodySignature, buf); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return fmt.Errorf("could not verify body: %w", err)
|
2020-08-18 13:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-08-14 12:51:10 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 13:01:40 +00:00
|
|
|
if verify.GetBodySignature() != nil {
|
|
|
|
return errors.New("body signature at the matryoshka upper level")
|
2020-08-14 12:51:10 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 11:23:10 +00:00
|
|
|
return verifyMatryoshkaLevel(body, meta.getOrigin(), origin, buf)
|
2020-08-14 12:51:10 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 11:23:10 +00:00
|
|
|
func verifyServiceMessagePart(part stableMarshaler, sigRdr func() *refs.Signature, buf []byte) error {
|
2020-08-18 13:01:40 +00:00
|
|
|
return signature.VerifyDataWithSource(
|
2020-08-25 11:47:55 +00:00
|
|
|
&StableMarshalerWrapper{part},
|
2022-02-22 11:25:43 +00:00
|
|
|
sigRdr,
|
2022-04-11 11:23:10 +00:00
|
|
|
signature.WithBuffer(buf),
|
2020-08-18 13:01:40 +00:00
|
|
|
)
|
|
|
|
}
|
2020-08-14 12:51:10 +00:00
|
|
|
|
2020-08-18 13:01:40 +00:00
|
|
|
func serviceMessageBody(req interface{}) stableMarshaler {
|
|
|
|
switch v := req.(type) {
|
2020-08-19 13:06:03 +00:00
|
|
|
default:
|
2020-08-20 10:41:55 +00:00
|
|
|
panic(fmt.Sprintf("unsupported session message %T", req))
|
2020-08-19 13:06:03 +00:00
|
|
|
|
|
|
|
/* Accounting */
|
2020-08-18 13:01:40 +00:00
|
|
|
case *accounting.BalanceRequest:
|
|
|
|
return v.GetBody()
|
|
|
|
case *accounting.BalanceResponse:
|
|
|
|
return v.GetBody()
|
2020-08-19 13:06:03 +00:00
|
|
|
|
|
|
|
/* Session */
|
|
|
|
case *session.CreateRequest:
|
|
|
|
return v.GetBody()
|
|
|
|
case *session.CreateResponse:
|
|
|
|
return v.GetBody()
|
|
|
|
|
|
|
|
/* Container */
|
|
|
|
case *container.PutRequest:
|
|
|
|
return v.GetBody()
|
|
|
|
case *container.PutResponse:
|
|
|
|
return v.GetBody()
|
|
|
|
case *container.DeleteRequest:
|
|
|
|
return v.GetBody()
|
|
|
|
case *container.DeleteResponse:
|
|
|
|
return v.GetBody()
|
|
|
|
case *container.GetRequest:
|
|
|
|
return v.GetBody()
|
|
|
|
case *container.GetResponse:
|
|
|
|
return v.GetBody()
|
|
|
|
case *container.ListRequest:
|
|
|
|
return v.GetBody()
|
|
|
|
case *container.ListResponse:
|
|
|
|
return v.GetBody()
|
|
|
|
case *container.SetExtendedACLRequest:
|
|
|
|
return v.GetBody()
|
|
|
|
case *container.SetExtendedACLResponse:
|
|
|
|
return v.GetBody()
|
|
|
|
case *container.GetExtendedACLRequest:
|
|
|
|
return v.GetBody()
|
|
|
|
case *container.GetExtendedACLResponse:
|
|
|
|
return v.GetBody()
|
2021-01-21 14:57:04 +00:00
|
|
|
case *container.AnnounceUsedSpaceRequest:
|
|
|
|
return v.GetBody()
|
|
|
|
case *container.AnnounceUsedSpaceResponse:
|
|
|
|
return v.GetBody()
|
2020-08-19 13:06:03 +00:00
|
|
|
|
|
|
|
/* Object */
|
|
|
|
case *object.PutRequest:
|
|
|
|
return v.GetBody()
|
|
|
|
case *object.PutResponse:
|
|
|
|
return v.GetBody()
|
|
|
|
case *object.GetRequest:
|
|
|
|
return v.GetBody()
|
|
|
|
case *object.GetResponse:
|
|
|
|
return v.GetBody()
|
|
|
|
case *object.HeadRequest:
|
|
|
|
return v.GetBody()
|
|
|
|
case *object.HeadResponse:
|
|
|
|
return v.GetBody()
|
|
|
|
case *object.SearchRequest:
|
|
|
|
return v.GetBody()
|
|
|
|
case *object.SearchResponse:
|
|
|
|
return v.GetBody()
|
|
|
|
case *object.DeleteRequest:
|
|
|
|
return v.GetBody()
|
|
|
|
case *object.DeleteResponse:
|
|
|
|
return v.GetBody()
|
|
|
|
case *object.GetRangeRequest:
|
|
|
|
return v.GetBody()
|
|
|
|
case *object.GetRangeResponse:
|
|
|
|
return v.GetBody()
|
|
|
|
case *object.GetRangeHashRequest:
|
|
|
|
return v.GetBody()
|
|
|
|
case *object.GetRangeHashResponse:
|
|
|
|
return v.GetBody()
|
2020-10-08 11:22:28 +00:00
|
|
|
|
|
|
|
/* Netmap */
|
|
|
|
case *netmap.LocalNodeInfoRequest:
|
|
|
|
return v.GetBody()
|
|
|
|
case *netmap.LocalNodeInfoResponse:
|
|
|
|
return v.GetBody()
|
2021-02-19 07:16:22 +00:00
|
|
|
case *netmap.NetworkInfoRequest:
|
|
|
|
return v.GetBody()
|
|
|
|
case *netmap.NetworkInfoResponse:
|
|
|
|
return v.GetBody()
|
2021-03-24 08:10:36 +00:00
|
|
|
|
|
|
|
/* Reputation */
|
2021-05-07 09:34:24 +00:00
|
|
|
case *reputation.AnnounceLocalTrustRequest:
|
2021-03-24 08:10:36 +00:00
|
|
|
return v.GetBody()
|
2021-05-07 09:34:24 +00:00
|
|
|
case *reputation.AnnounceLocalTrustResponse:
|
2021-03-24 08:10:36 +00:00
|
|
|
return v.GetBody()
|
2021-05-07 09:34:24 +00:00
|
|
|
case *reputation.AnnounceIntermediateResultRequest:
|
2021-04-01 14:54:16 +00:00
|
|
|
return v.GetBody()
|
2021-05-07 09:34:24 +00:00
|
|
|
case *reputation.AnnounceIntermediateResultResponse:
|
2021-04-01 14:54:16 +00:00
|
|
|
return v.GetBody()
|
2020-08-18 13:01:40 +00:00
|
|
|
}
|
2020-08-14 12:51:10 +00:00
|
|
|
}
|