2020-08-14 12:51:10 +00:00
|
|
|
package signature
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/util/signature"
|
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"
|
|
|
|
"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"
|
2020-08-19 13:06:03 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/session"
|
2020-08-14 12:51:10 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
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 {
|
|
|
|
StableMarshal([]byte) ([]byte, error)
|
|
|
|
StableSize() int
|
|
|
|
}
|
|
|
|
|
|
|
|
type stableMarshalerWrapper struct {
|
|
|
|
sm stableMarshaler
|
|
|
|
}
|
|
|
|
|
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-14 12:51:10 +00:00
|
|
|
func (s stableMarshalerWrapper) ReadSignedData(buf []byte) ([]byte, error) {
|
2020-08-18 13:01:40 +00:00
|
|
|
if s.sm != nil {
|
|
|
|
return s.sm.StableMarshal(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
2020-08-14 12:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s stableMarshalerWrapper) SignedDataSize() int {
|
2020-08-18 13:01:40 +00:00
|
|
|
if s.sm != nil {
|
|
|
|
return s.sm.StableSize()
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
2020-08-14 12:51:10 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 09:43:47 +00:00
|
|
|
func keySignatureHandler(s *refs.Signature) signature.KeySignatureHandler {
|
2020-08-14 12:51:10 +00:00
|
|
|
return func(key []byte, sig []byte) {
|
|
|
|
s.SetKey(key)
|
|
|
|
s.SetSign(sig)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-20 09:43:47 +00:00
|
|
|
func keySignatureSource(s *refs.Signature) signature.KeySignatureSource {
|
2020-08-14 12:51:10 +00:00
|
|
|
return func() ([]byte, []byte) {
|
|
|
|
return s.GetKey(), s.GetSign()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return errors.Wrap(err, "could not sign body")
|
|
|
|
}
|
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 {
|
|
|
|
return errors.Wrap(err, "could not sign meta header")
|
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 {
|
|
|
|
return errors.Wrap(err, "could not sign origin of verification header")
|
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,
|
|
|
|
&stableMarshalerWrapper{part},
|
|
|
|
keySignatureHandler(sig),
|
|
|
|
); 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
|
|
|
}
|
|
|
|
|
|
|
|
return verifyMatryoshkaLevel(serviceMessageBody(msg), meta, verify)
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyMatryoshkaLevel(body stableMarshaler, meta metaHeader, verify verificationHeader) error {
|
|
|
|
if err := verifyServiceMessagePart(meta, verify.GetMetaSignature); err != nil {
|
|
|
|
return errors.Wrap(err, "could not verify meta header")
|
|
|
|
}
|
|
|
|
|
|
|
|
origin := verify.getOrigin()
|
2020-08-14 12:51:10 +00:00
|
|
|
|
2020-08-18 13:01:40 +00:00
|
|
|
if err := verifyServiceMessagePart(origin, verify.GetOriginSignature); err != nil {
|
|
|
|
return errors.Wrap(err, "could not verify origin of verification header")
|
2020-08-14 12:51:10 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 13:01:40 +00:00
|
|
|
if origin == nil {
|
|
|
|
if err := verifyServiceMessagePart(body, verify.GetBodySignature); err != nil {
|
|
|
|
return errors.Wrap(err, "could not verify body")
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-08-18 13:01:40 +00:00
|
|
|
return verifyMatryoshkaLevel(body, meta.getOrigin(), origin)
|
2020-08-14 12:51:10 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 09:43:47 +00:00
|
|
|
func verifyServiceMessagePart(part stableMarshaler, sigRdr func() *refs.Signature) error {
|
2020-08-18 13:01:40 +00:00
|
|
|
return signature.VerifyDataWithSource(
|
2020-08-14 12:51:10 +00:00
|
|
|
&stableMarshalerWrapper{part},
|
|
|
|
keySignatureSource(sigRdr()),
|
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()
|
|
|
|
|
|
|
|
/* 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-08-18 13:01:40 +00:00
|
|
|
}
|
2020-08-14 12:51:10 +00:00
|
|
|
}
|