2021-11-08 10:04:45 +00:00
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
2021-03-22 10:56:18 +00:00
|
|
|
"bytes"
|
2021-11-08 10:04:45 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2022-04-05 11:13:34 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/object"
|
2021-11-08 10:04:45 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/checksum"
|
2022-04-05 11:13:34 +00:00
|
|
|
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
|
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
|
|
|
)
|
|
|
|
|
2022-04-12 13:08:09 +00:00
|
|
|
var (
|
|
|
|
errCheckSumMismatch = errors.New("payload checksum mismatch")
|
|
|
|
errCheckSumNotSet = errors.New("payload checksum is not set")
|
|
|
|
errIncorrectID = errors.New("incorrect object identifier")
|
|
|
|
)
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
// CalculatePayloadChecksum calculates and returns checksum of
|
|
|
|
// object payload bytes.
|
2021-03-04 11:38:23 +00:00
|
|
|
func CalculatePayloadChecksum(payload []byte) checksum.Checksum {
|
2021-03-22 10:56:18 +00:00
|
|
|
var res checksum.Checksum
|
|
|
|
checksum.Calculate(&res, checksum.SHA256, payload)
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2021-03-04 11:38:23 +00:00
|
|
|
return res
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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())
|
2022-04-12 13:08:09 +00:00
|
|
|
|
|
|
|
cs, set := obj.PayloadChecksum()
|
|
|
|
if !set {
|
|
|
|
return errCheckSumNotSet
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(cs.Value(), actual.Value()) {
|
2021-11-08 10:04:45 +00:00
|
|
|
return errCheckSumMismatch
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CalculateID calculates identifier for the object.
|
2022-04-11 16:25:14 +00:00
|
|
|
func CalculateID(obj *Object) (oid.ID, error) {
|
|
|
|
var id oid.ID
|
2022-05-30 19:05:35 +00:00
|
|
|
id.SetSHA256(sha256.Sum256(obj.ToV2().GetHeader().StableMarshal(nil)))
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-04-12 16:23:16 +00:00
|
|
|
oID, set := obj.ID()
|
|
|
|
if !set {
|
|
|
|
return errOIDNotSet
|
|
|
|
}
|
|
|
|
|
|
|
|
if !id.Equals(oID) {
|
2021-11-08 10:04:45 +00:00
|
|
|
return errIncorrectID
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
// CalculateAndSetSignature signs id with provided key and sets that signature to
|
|
|
|
// the object.
|
2023-04-25 08:31:27 +00:00
|
|
|
func CalculateAndSetSignature(signer neofscrypto.Signer, obj *Object) error {
|
2022-04-12 16:23:16 +00:00
|
|
|
oID, set := obj.ID()
|
|
|
|
if !set {
|
|
|
|
return errOIDNotSet
|
|
|
|
}
|
|
|
|
|
2023-04-25 08:31:27 +00:00
|
|
|
sig, err := oID.CalculateIDSignature(signer)
|
2021-11-08 10:04:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
obj.SetSignature(&sig)
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
// VerifyIDSignature verifies object ID signature.
|
|
|
|
func (o *Object) VerifyIDSignature() bool {
|
2022-04-05 11:13:34 +00:00
|
|
|
m := (*object.Object)(o)
|
|
|
|
|
|
|
|
sigV2 := m.GetSignature()
|
|
|
|
if sigV2 == nil {
|
2022-04-12 16:23:16 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-04-05 11:13:34 +00:00
|
|
|
idV2 := m.GetObjectID()
|
|
|
|
if idV2 == nil {
|
|
|
|
return false
|
|
|
|
}
|
2022-04-11 16:25:14 +00:00
|
|
|
|
2022-04-05 11:13:34 +00:00
|
|
|
var sig neofscrypto.Signature
|
2022-04-11 16:25:14 +00:00
|
|
|
|
2022-07-07 10:51:05 +00:00
|
|
|
return sig.ReadFromV2(*sigV2) == nil && sig.Verify(idV2.StableMarshal(nil))
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetIDWithSignature sets object identifier and signature.
|
2023-04-25 08:31:27 +00:00
|
|
|
func SetIDWithSignature(signer neofscrypto.Signer, 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)
|
|
|
|
}
|
|
|
|
|
2023-04-25 08:31:27 +00:00
|
|
|
if err := CalculateAndSetSignature(signer, obj); err != nil {
|
2021-11-08 10:04:45 +00:00
|
|
|
return fmt.Errorf("could not set signature: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetVerificationFields calculates and sets all verification fields of the object.
|
2023-04-25 08:31:27 +00:00
|
|
|
func SetVerificationFields(signer neofscrypto.Signer, obj *Object) error {
|
2021-11-08 10:04:45 +00:00
|
|
|
CalculateAndSetPayloadChecksum(obj)
|
|
|
|
|
2023-04-25 08:31:27 +00:00
|
|
|
return SetIDWithSignature(signer, obj)
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
var errInvalidSignature = errors.New("invalid signature")
|
|
|
|
|
2021-11-08 10:04:45 +00:00
|
|
|
// CheckHeaderVerificationFields checks all verification fields except payload.
|
|
|
|
func CheckHeaderVerificationFields(obj *Object) error {
|
2022-04-11 16:25:14 +00:00
|
|
|
if !obj.VerifyIDSignature() {
|
|
|
|
return errInvalidSignature
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := VerifyID(obj); err != nil {
|
|
|
|
return fmt.Errorf("invalid identifier: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|