2024-01-07 10:12:43 +00:00
|
|
|
package signed
|
2024-01-06 11:33:46 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ed25519"
|
|
|
|
"errors"
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
2024-01-07 08:58:22 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/interfaces"
|
2024-01-06 11:33:46 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/net"
|
2024-01-07 10:12:43 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/protocol/base"
|
2024-01-06 11:33:46 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
|
|
|
"github.com/vmihailenco/msgpack/v5"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-01-07 10:12:43 +00:00
|
|
|
_ base.IncomingMessageTyped = (*SignedMessage)(nil)
|
|
|
|
_ msgpack.CustomDecoder = (*signedMessagePayoad)(nil)
|
2024-01-07 14:07:37 +00:00
|
|
|
_ msgpack.CustomEncoder = (*SignedMessage)(nil)
|
2024-01-06 11:33:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errInvalidSignature = errors.New("Invalid signature found")
|
|
|
|
)
|
|
|
|
|
|
|
|
type SignedMessage struct {
|
|
|
|
nodeId *encoding.NodeId
|
|
|
|
signature []byte
|
|
|
|
message []byte
|
2024-01-07 10:12:43 +00:00
|
|
|
base.IncomingMessageTypedImpl
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
|
|
|
|
2024-01-07 14:07:37 +00:00
|
|
|
func (s *SignedMessage) SetNodeId(nodeId *encoding.NodeId) {
|
|
|
|
s.nodeId = nodeId
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SignedMessage) SetSignature(signature []byte) {
|
|
|
|
s.signature = signature
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SignedMessage) SetMessage(message []byte) {
|
|
|
|
s.message = message
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSignedMessageRequest(message []byte) *SignedMessage {
|
|
|
|
return &SignedMessage{message: message}
|
|
|
|
}
|
|
|
|
|
2024-01-06 11:33:46 +00:00
|
|
|
type signedMessagePayoad struct {
|
|
|
|
kind int
|
|
|
|
message msgpack.RawMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *signedMessagePayoad) DecodeMsgpack(dec *msgpack.Decoder) error {
|
|
|
|
kind, err := dec.DecodeInt()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.kind = kind
|
|
|
|
|
|
|
|
message, err := dec.DecodeRaw()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.message = message
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSignedMessage() *SignedMessage {
|
|
|
|
return &SignedMessage{}
|
|
|
|
}
|
|
|
|
|
2024-01-07 11:35:41 +00:00
|
|
|
func (s *SignedMessage) HandleMessage(node interfaces.Node, peer net.Peer, verifyId bool) error {
|
2024-01-06 11:33:46 +00:00
|
|
|
var payload signedMessagePayoad
|
|
|
|
|
|
|
|
err := msgpack.Unmarshal(s.message, &payload)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-07 10:12:43 +00:00
|
|
|
if msgHandler, valid := GetMessageType(types.ProtocolMethod(payload.kind)); valid {
|
2024-01-06 11:33:46 +00:00
|
|
|
msgHandler.SetIncomingMessage(s)
|
|
|
|
err := msgpack.Unmarshal(payload.message, &msgHandler)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = msgHandler.HandleMessage(node, peer, verifyId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SignedMessage) DecodeMessage(dec *msgpack.Decoder) error {
|
|
|
|
nodeId, err := dec.DecodeBytes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.nodeId = encoding.NewNodeId(nodeId)
|
|
|
|
|
|
|
|
signature, err := dec.DecodeBytes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.signature = signature
|
|
|
|
|
|
|
|
message, err := dec.DecodeBytes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.message = message
|
|
|
|
|
2024-01-08 03:38:19 +00:00
|
|
|
if !ed25519.Verify(s.nodeId.Raw()[1:], s.message, s.signature) {
|
2024-01-06 11:33:46 +00:00
|
|
|
return errInvalidSignature
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2024-01-07 14:07:37 +00:00
|
|
|
}
|
|
|
|
func (s *SignedMessage) EncodeMsgpack(enc *msgpack.Encoder) error {
|
|
|
|
err := enc.EncodeInt(int64(types.ProtocolMethodSignedMessage))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = enc.EncodeBytes(s.nodeId.Raw())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = enc.EncodeBytes(s.signature)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = enc.EncodeBytes(s.message)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (s *SignedMessage) Sign(node interfaces.Node) error {
|
|
|
|
if s.nodeId == nil {
|
|
|
|
panic("nodeId is nil")
|
|
|
|
}
|
2024-01-06 11:33:46 +00:00
|
|
|
|
2024-01-07 14:07:37 +00:00
|
|
|
if s.message == nil {
|
|
|
|
panic("message is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
s.signature = ed25519.Sign(node.Config().KeyPair.ExtractBytes(), s.message)
|
|
|
|
|
|
|
|
return nil
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|