status-go/protocol/v1/status_message.go

180 lines
5.4 KiB
Go
Raw Permalink Normal View History

package protocol
import (
"crypto/ecdsa"
"encoding/json"
"github.com/golang/protobuf/proto"
"github.com/jinzhu/copier"
"github.com/pkg/errors"
"go.uber.org/zap"
2020-01-02 09:10:19 +00:00
"github.com/ethereum/go-ethereum/common/hexutil"
2023-11-15 15:58:15 +00:00
utils "github.com/status-im/status-go/common"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/logutils"
"github.com/status-im/status-go/protocol/encryption"
2020-07-31 12:22:05 +00:00
"github.com/status-im/status-go/protocol/encryption/multidevice"
"github.com/status-im/status-go/protocol/encryption/sharedsecret"
"github.com/status-im/status-go/protocol/protobuf"
)
// TransportLayer is the lowest layer and represents waku message.
type TransportLayer struct {
// Payload as received from the transport layer
Payload []byte `json:"-"`
Hash []byte `json:"-"`
SigPubKey *ecdsa.PublicKey `json:"-"`
Dst *ecdsa.PublicKey
Message *types.Message `json:"message"`
}
// EncryptionLayer handles optional encryption.
// It is not mandatory and can be omitted,
// also its presence does not guarantee encryption.
type EncryptionLayer struct {
// Payload after having been processed by the encryption layer
Payload []byte `json:"-"`
Installations []*multidevice.Installation
SharedSecrets []*sharedsecret.Secret
2022-09-21 16:05:29 +00:00
HashRatchetInfo []*encryption.HashRatchetInfo
}
// ApplicationLayer is the topmost layer and represents the application message.
type ApplicationLayer struct {
// Payload after having been unwrapped from the application layer
Payload []byte `json:"-"`
ID types.HexBytes `json:"id"`
SigPubKey *ecdsa.PublicKey `json:"-"`
Type protobuf.ApplicationMetadataMessage_Type `json:"-"`
}
// StatusMessage encapsulates all layers of the protocol
type StatusMessage struct {
TransportLayer TransportLayer `json:"transportLayer"`
EncryptionLayer EncryptionLayer `json:"encryptionLayer"`
ApplicationLayer ApplicationLayer `json:"applicationLayer"`
}
// Temporary JSON marshaling for those messages that are not yet processed
// by the go code
func (m *StatusMessage) MarshalJSON() ([]byte, error) {
item := struct {
ID types.HexBytes `json:"id"`
Payload string `json:"payload"`
From types.HexBytes `json:"from"`
Timestamp uint32 `json:"timestamp"`
}{
ID: m.ApplicationLayer.ID,
Payload: string(m.ApplicationLayer.Payload),
Timestamp: m.TransportLayer.Message.Timestamp,
From: m.TransportLayer.Message.Sig,
}
return json.Marshal(item)
}
// SigPubKey returns the most important signature, from the application layer to transport
func (m *StatusMessage) SigPubKey() *ecdsa.PublicKey {
if m.ApplicationLayer.SigPubKey != nil {
return m.ApplicationLayer.SigPubKey
}
return m.TransportLayer.SigPubKey
}
func (m *StatusMessage) Clone() (*StatusMessage, error) {
copy := &StatusMessage{}
err := copier.Copy(&copy, m)
return copy, err
}
func (m *StatusMessage) HandleTransportLayer(wakuMessage *types.Message) error {
publicKey, err := crypto.UnmarshalPubkey(wakuMessage.Sig)
if err != nil {
return errors.Wrap(err, "failed to get signature")
}
m.TransportLayer.Message = wakuMessage
m.TransportLayer.Hash = wakuMessage.Hash
m.TransportLayer.SigPubKey = publicKey
m.TransportLayer.Payload = wakuMessage.Payload
if wakuMessage.Dst != nil {
publicKey, err := crypto.UnmarshalPubkey(wakuMessage.Dst)
2020-07-03 10:08:47 +00:00
if err != nil {
return err
}
m.TransportLayer.Dst = publicKey
2020-07-03 10:08:47 +00:00
}
return nil
}
func (m *StatusMessage) HandleEncryptionLayer(myKey *ecdsa.PrivateKey, senderKey *ecdsa.PublicKey, enc *encryption.Protocol, skipNegotiation bool) error {
// As we handle non-encrypted messages, we make sure that DecryptPayload
// is set regardless of whether this step is successful
m.EncryptionLayer.Payload = m.TransportLayer.Payload
// Nothing to do
if skipNegotiation {
return nil
}
var protocolMessage encryption.ProtocolMessage
err := proto.Unmarshal(m.TransportLayer.Payload, &protocolMessage)
if err != nil {
return errors.Wrap(err, "failed to unmarshal ProtocolMessage")
}
2020-07-31 12:22:05 +00:00
response, err := enc.HandleMessage(
myKey,
senderKey,
&protocolMessage,
m.TransportLayer.Hash,
)
2022-09-21 16:05:29 +00:00
if err == encryption.ErrHashRatchetGroupIDNotFound {
if response != nil {
m.EncryptionLayer.HashRatchetInfo = response.HashRatchetInfo
2022-09-21 16:05:29 +00:00
}
return err
}
if err != nil {
return errors.Wrap(err, "failed to handle Encryption message")
}
m.EncryptionLayer.Payload = response.DecryptedMessage
m.EncryptionLayer.Installations = response.Installations
m.EncryptionLayer.SharedSecrets = response.SharedSecrets
m.EncryptionLayer.HashRatchetInfo = response.HashRatchetInfo
return nil
}
func (m *StatusMessage) HandleApplicationLayer() error {
2023-11-15 15:58:15 +00:00
message, err := protobuf.Unmarshal(m.EncryptionLayer.Payload)
if err != nil {
return err
}
2023-11-15 15:58:15 +00:00
recoveredKey, err := utils.RecoverKey(message)
if err != nil {
return err
}
m.ApplicationLayer.SigPubKey = recoveredKey
// Calculate ID using the wrapped record
m.ApplicationLayer.ID = MessageID(recoveredKey, m.EncryptionLayer.Payload)
logutils.ZapLogger().Debug("calculated ID for envelope",
zap.String("envelopeHash", hexutil.Encode(m.TransportLayer.Hash)),
zap.String("messageId", hexutil.Encode(m.ApplicationLayer.ID)),
)
m.ApplicationLayer.Payload = message.Payload
m.ApplicationLayer.Type = message.Type
return nil
}