2019-11-21 16:19:22 +00:00
|
|
|
package protocol
|
2019-07-26 07:17:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
"encoding/json"
|
2019-07-26 07:17:29 +00:00
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/jinzhu/copier"
|
2019-09-26 07:01:17 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
2021-11-11 16:13:55 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2023-11-15 15:58:15 +00:00
|
|
|
utils "github.com/status-im/status-go/common"
|
2019-11-23 17:57:05 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2019-11-21 16:19:22 +00:00
|
|
|
"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"
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
2019-07-26 07:17:29 +00:00
|
|
|
)
|
|
|
|
|
2023-11-08 18:05:33 +00:00
|
|
|
// 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"`
|
|
|
|
}
|
2019-11-06 16:23:11 +00:00
|
|
|
|
2023-11-08 18:05:33 +00:00
|
|
|
// 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
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
}
|
|
|
|
|
2023-11-08 18:05:33 +00:00
|
|
|
// 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"`
|
|
|
|
}
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
// 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"`
|
|
|
|
}{
|
2023-11-08 18:05:33 +00:00
|
|
|
ID: m.ApplicationLayer.ID,
|
|
|
|
Payload: string(m.ApplicationLayer.Payload),
|
|
|
|
Timestamp: m.TransportLayer.Message.Timestamp,
|
|
|
|
From: m.TransportLayer.Message.Sig,
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
}
|
|
|
|
return json.Marshal(item)
|
2019-07-26 07:17:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SigPubKey returns the most important signature, from the application layer to transport
|
2019-11-23 17:57:05 +00:00
|
|
|
func (m *StatusMessage) SigPubKey() *ecdsa.PublicKey {
|
2023-11-08 18:05:33 +00:00
|
|
|
if m.ApplicationLayer.SigPubKey != nil {
|
|
|
|
return m.ApplicationLayer.SigPubKey
|
2019-07-26 07:17:29 +00:00
|
|
|
}
|
|
|
|
|
2023-11-08 18:05:33 +00:00
|
|
|
return m.TransportLayer.SigPubKey
|
2019-07-26 07:17:29 +00:00
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (m *StatusMessage) Clone() (*StatusMessage, error) {
|
2019-07-26 07:17:29 +00:00
|
|
|
copy := &StatusMessage{}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
err := copier.Copy(©, m)
|
2019-07-26 07:17:29 +00:00
|
|
|
return copy, err
|
|
|
|
}
|
|
|
|
|
2023-11-08 18:05:33 +00:00
|
|
|
func (m *StatusMessage) HandleTransportLayer(wakuMessage *types.Message) error {
|
|
|
|
publicKey, err := crypto.UnmarshalPubkey(wakuMessage.Sig)
|
2019-07-26 07:17:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to get signature")
|
|
|
|
}
|
|
|
|
|
2023-11-08 18:05:33 +00:00
|
|
|
m.TransportLayer.Message = wakuMessage
|
|
|
|
m.TransportLayer.Hash = wakuMessage.Hash
|
|
|
|
m.TransportLayer.SigPubKey = publicKey
|
|
|
|
m.TransportLayer.Payload = wakuMessage.Payload
|
2019-07-26 07:17:29 +00:00
|
|
|
|
2023-11-08 18:05:33 +00:00
|
|
|
if wakuMessage.Dst != nil {
|
|
|
|
publicKey, err := crypto.UnmarshalPubkey(wakuMessage.Dst)
|
2020-07-03 10:08:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-08 18:05:33 +00:00
|
|
|
m.TransportLayer.Dst = publicKey
|
2020-07-03 10:08:47 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 07:17:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-08 18:05:33 +00:00
|
|
|
func (m *StatusMessage) HandleEncryptionLayer(myKey *ecdsa.PrivateKey, senderKey *ecdsa.PublicKey, enc *encryption.Protocol, skipNegotiation bool) error {
|
2019-07-26 07:17:29 +00:00
|
|
|
// As we handle non-encrypted messages, we make sure that DecryptPayload
|
|
|
|
// is set regardless of whether this step is successful
|
2023-11-08 18:05:33 +00:00
|
|
|
m.EncryptionLayer.Payload = m.TransportLayer.Payload
|
2020-07-21 15:41:10 +00:00
|
|
|
// Nothing to do
|
|
|
|
if skipNegotiation {
|
|
|
|
return nil
|
|
|
|
}
|
2019-07-26 07:17:29 +00:00
|
|
|
|
|
|
|
var protocolMessage encryption.ProtocolMessage
|
2023-11-08 18:05:33 +00:00
|
|
|
err := proto.Unmarshal(m.TransportLayer.Payload, &protocolMessage)
|
2019-07-26 07:17:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to unmarshal ProtocolMessage")
|
|
|
|
}
|
|
|
|
|
2020-07-31 12:22:05 +00:00
|
|
|
response, err := enc.HandleMessage(
|
2019-07-26 07:17:29 +00:00
|
|
|
myKey,
|
|
|
|
senderKey,
|
|
|
|
&protocolMessage,
|
2023-11-08 18:05:33 +00:00
|
|
|
m.TransportLayer.Hash,
|
2019-07-26 07:17:29 +00:00
|
|
|
)
|
|
|
|
|
2022-09-21 16:05:29 +00:00
|
|
|
if err == encryption.ErrHashRatchetGroupIDNotFound {
|
|
|
|
|
|
|
|
if response != nil {
|
2023-11-08 18:05:33 +00:00
|
|
|
m.EncryptionLayer.HashRatchetInfo = response.HashRatchetInfo
|
2022-09-21 16:05:29 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-26 07:17:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to handle Encryption message")
|
|
|
|
}
|
|
|
|
|
2023-11-08 18:05:33 +00:00
|
|
|
m.EncryptionLayer.Payload = response.DecryptedMessage
|
|
|
|
m.EncryptionLayer.Installations = response.Installations
|
|
|
|
m.EncryptionLayer.SharedSecrets = response.SharedSecrets
|
|
|
|
m.EncryptionLayer.HashRatchetInfo = response.HashRatchetInfo
|
2019-07-26 07:17:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-08 18:05:33 +00:00
|
|
|
func (m *StatusMessage) HandleApplicationLayer() error {
|
2023-11-15 15:58:15 +00:00
|
|
|
|
2023-11-08 18:05:33 +00:00
|
|
|
message, err := protobuf.Unmarshal(m.EncryptionLayer.Payload)
|
2019-07-26 07:17:29 +00:00
|
|
|
if err != nil {
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
return err
|
2019-07-26 07:17:29 +00:00
|
|
|
}
|
|
|
|
|
2023-11-15 15:58:15 +00:00
|
|
|
recoveredKey, err := utils.RecoverKey(message)
|
2019-07-26 07:17:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-08 18:05:33 +00:00
|
|
|
m.ApplicationLayer.SigPubKey = recoveredKey
|
2019-08-27 12:04:15 +00:00
|
|
|
// Calculate ID using the wrapped record
|
2023-11-08 18:05:33 +00:00
|
|
|
m.ApplicationLayer.ID = MessageID(recoveredKey, m.EncryptionLayer.Payload)
|
|
|
|
log.Debug("calculated ID for envelope", "envelopeHash", hexutil.Encode(m.TransportLayer.Hash), "messageId", hexutil.Encode(m.ApplicationLayer.ID))
|
2021-11-11 16:13:55 +00:00
|
|
|
|
2023-11-08 18:05:33 +00:00
|
|
|
m.ApplicationLayer.Payload = message.Payload
|
|
|
|
m.ApplicationLayer.Type = message.Type
|
2019-07-26 07:17:29 +00:00
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|