2021-11-08 10:04:45 +00:00
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/sha256"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
signatureV2 "github.com/nspcc-dev/neofs-api-go/v2/signature"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/checksum"
|
2022-01-25 16:20:32 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2021-11-08 10:04:45 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/signature"
|
|
|
|
sigutil "github.com/nspcc-dev/neofs-sdk-go/util/signature"
|
|
|
|
)
|
|
|
|
|
|
|
|
var errCheckSumMismatch = errors.New("payload checksum mismatch")
|
|
|
|
|
|
|
|
var errIncorrectID = errors.New("incorrect object identifier")
|
|
|
|
|
|
|
|
// CalculatePayloadChecksum calculates and returns checksum of
|
|
|
|
// object payload bytes.
|
|
|
|
func CalculatePayloadChecksum(payload []byte) *checksum.Checksum {
|
|
|
|
res := checksum.New()
|
|
|
|
res.SetSHA256(sha256.Sum256(payload))
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// CalculateAndSetPayloadChecksum calculates checksum of current
|
|
|
|
// object payload and writes it to the object.
|
2022-03-03 11:48:55 +00:00
|
|
|
func CalculateAndSetPayloadChecksum(obj *Object) {
|
2021-11-08 10:04:45 +00:00
|
|
|
obj.SetPayloadChecksum(
|
|
|
|
CalculatePayloadChecksum(obj.Payload()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyPayloadChecksum checks if payload checksum in the object
|
|
|
|
// corresponds to its payload.
|
|
|
|
func VerifyPayloadChecksum(obj *Object) error {
|
|
|
|
actual := CalculatePayloadChecksum(obj.Payload())
|
|
|
|
if !checksum.Equal(obj.PayloadChecksum(), actual) {
|
|
|
|
return errCheckSumMismatch
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CalculateID calculates identifier for the object.
|
2022-01-25 16:20:32 +00:00
|
|
|
func CalculateID(obj *Object) (*oid.ID, error) {
|
2022-05-05 08:00:13 +00:00
|
|
|
data := obj.ToV2().GetHeader().StableMarshal(nil)
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2022-01-25 16:20:32 +00:00
|
|
|
id := oid.NewID()
|
2021-11-08 10:04:45 +00:00
|
|
|
id.SetSHA256(sha256.Sum256(data))
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CalculateAndSetID calculates identifier for the object
|
|
|
|
// and writes the result to it.
|
2022-03-03 11:48:55 +00:00
|
|
|
func CalculateAndSetID(obj *Object) error {
|
|
|
|
id, err := CalculateID(obj)
|
2021-11-08 10:04:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
obj.SetID(id)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyID checks if identifier in the object corresponds to
|
|
|
|
// its structure.
|
|
|
|
func VerifyID(obj *Object) error {
|
|
|
|
id, err := CalculateID(obj)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !id.Equal(obj.ID()) {
|
|
|
|
return errIncorrectID
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-25 16:20:32 +00:00
|
|
|
func CalculateIDSignature(key *ecdsa.PrivateKey, id *oid.ID) (*signature.Signature, error) {
|
2022-02-24 11:45:52 +00:00
|
|
|
return sigutil.SignData(
|
2021-11-08 10:04:45 +00:00
|
|
|
key,
|
|
|
|
signatureV2.StableMarshalerWrapper{
|
|
|
|
SM: id.ToV2(),
|
2022-02-24 11:45:52 +00:00
|
|
|
})
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
func CalculateAndSetSignature(key *ecdsa.PrivateKey, obj *Object) error {
|
2021-11-08 10:04:45 +00:00
|
|
|
sig, err := CalculateIDSignature(key, obj.ID())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
obj.SetSignature(sig)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func VerifyIDSignature(obj *Object) error {
|
2022-02-24 11:45:52 +00:00
|
|
|
return sigutil.VerifyData(
|
2021-11-08 10:04:45 +00:00
|
|
|
signatureV2.StableMarshalerWrapper{
|
|
|
|
SM: obj.ID().ToV2(),
|
|
|
|
},
|
2022-02-24 11:45:52 +00:00
|
|
|
obj.Signature(),
|
2021-11-08 10:04:45 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetIDWithSignature sets object identifier and signature.
|
2022-03-03 11:48:55 +00:00
|
|
|
func SetIDWithSignature(key *ecdsa.PrivateKey, obj *Object) error {
|
2021-11-08 10:04:45 +00:00
|
|
|
if err := CalculateAndSetID(obj); err != nil {
|
|
|
|
return fmt.Errorf("could not set identifier: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := CalculateAndSetSignature(key, obj); err != nil {
|
|
|
|
return fmt.Errorf("could not set signature: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetVerificationFields calculates and sets all verification fields of the object.
|
2022-03-03 11:48:55 +00:00
|
|
|
func SetVerificationFields(key *ecdsa.PrivateKey, obj *Object) error {
|
2021-11-08 10:04:45 +00:00
|
|
|
CalculateAndSetPayloadChecksum(obj)
|
|
|
|
|
|
|
|
return SetIDWithSignature(key, obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckVerificationFields checks all verification fields of the object.
|
|
|
|
func CheckVerificationFields(obj *Object) error {
|
|
|
|
if err := CheckHeaderVerificationFields(obj); err != nil {
|
|
|
|
return fmt.Errorf("invalid header structure: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := VerifyPayloadChecksum(obj); err != nil {
|
|
|
|
return fmt.Errorf("invalid payload checksum: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckHeaderVerificationFields checks all verification fields except payload.
|
|
|
|
func CheckHeaderVerificationFields(obj *Object) error {
|
|
|
|
if err := VerifyIDSignature(obj); err != nil {
|
|
|
|
return fmt.Errorf("invalid signature: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := VerifyID(obj); err != nil {
|
|
|
|
return fmt.Errorf("invalid identifier: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|