Skip to content

Commit 0898c45

Browse files
committed
message: fix decrypting msmsg's from @bot users
1 parent 306b451 commit 0898c45

File tree

2 files changed

+22
-32
lines changed

2 files changed

+22
-32
lines changed

message.go

+21-30
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ func (cli *Client) handlePlaintextMessage(info *types.MessageInfo, node *waBinar
216216
}
217217
}
218218
cli.dispatchEvent(evt.UnwrapRaw())
219-
return
220219
}
221220

222221
func (cli *Client) decryptMessages(info *types.MessageInfo, node *waBinary.Node) {
@@ -250,41 +249,33 @@ func (cli *Client) decryptMessages(info *types.MessageInfo, node *waBinary.Node)
250249
} else if info.IsGroup && encType == "skmsg" {
251250
decrypted, err = cli.decryptGroupMsg(&child, info.Sender, info.Chat)
252251
} else if encType == "msmsg" && info.Sender.IsBot() {
253-
// Meta AI / other bots (biz?):
254-
255-
// step 1: get message secret
256252
targetSenderJID := info.MsgMetaInfo.TargetSender
253+
messageSecretSenderJID := targetSenderJID
257254
if targetSenderJID.User == "" {
258-
// if no targetSenderJID in <meta> this must be ourselves (one-one-one mode)
259-
targetSenderJID = cli.getOwnID()
255+
if info.Sender.Server == types.BotServer {
256+
targetSenderJID = cli.Store.LID
257+
} else {
258+
targetSenderJID = cli.getOwnID()
259+
}
260+
messageSecretSenderJID = cli.getOwnID()
260261
}
261-
262-
messageSecret, err := cli.Store.MsgSecrets.GetMessageSecret(info.Chat, targetSenderJID, info.MsgMetaInfo.TargetID)
263-
if err != nil || messageSecret == nil {
264-
cli.Log.Warnf("Error getting message secret for bot msg with id %s", node.AttrGetter().String("id"))
265-
continue
262+
var decryptMessageID string
263+
if info.MsgBotInfo.EditType == types.EditTypeInner || info.MsgBotInfo.EditType == types.EditTypeLast {
264+
decryptMessageID = info.MsgBotInfo.EditTargetID
265+
} else {
266+
decryptMessageID = info.ID
266267
}
267-
268-
// step 2: get MessageSecretMessage
269-
byteContents := child.Content.([]byte) // <enc> contents
270268
var msMsg waE2E.MessageSecretMessage
271-
272-
err = proto.Unmarshal(byteContents, &msMsg)
273-
if err != nil {
274-
cli.Log.Warnf("Error decoding MessageSecretMesage protobuf %v", err)
275-
continue
276-
}
277-
278-
// step 3: determine best message id for decryption
279-
var messageID string
280-
if info.MsgBotInfo.EditType == types.EditTypeInner || info.MsgBotInfo.EditType == types.EditTypeLast {
281-
messageID = info.MsgBotInfo.EditTargetID
269+
var messageSecret []byte
270+
if messageSecret, err = cli.Store.MsgSecrets.GetMessageSecret(info.Chat, messageSecretSenderJID, info.MsgMetaInfo.TargetID); err != nil {
271+
err = fmt.Errorf("failed to get message secret for %s: %v", info.MsgMetaInfo.TargetID, err)
272+
} else if messageSecret == nil {
273+
err = fmt.Errorf("message secret for %s not found", info.MsgMetaInfo.TargetID)
274+
} else if err = proto.Unmarshal(child.Content.([]byte), &msMsg); err != nil {
275+
err = fmt.Errorf("failed to unmarshal MessageSecretMessage protobuf: %v", err)
282276
} else {
283-
messageID = info.ID
277+
decrypted, err = cli.decryptBotMessage(messageSecret, &msMsg, decryptMessageID, targetSenderJID, info)
284278
}
285-
286-
// step 4: decrypt and voila
287-
decrypted, err = cli.decryptBotMessage(messageSecret, &msMsg, messageID, targetSenderJID, info)
288279
} else {
289280
cli.Log.Warnf("Unhandled encrypted message (type %s) from %s", encType, info.SourceString())
290281
continue
@@ -294,7 +285,7 @@ func (cli *Client) decryptMessages(info *types.MessageInfo, node *waBinary.Node)
294285
cli.Log.Warnf("Error decrypting message from %s: %v", info.SourceString(), err)
295286
isUnavailable := encType == "skmsg" && !containsDirectMsg && errors.Is(err, signalerror.ErrNoSenderKeyForUser)
296287
// TODO figure out why @bot messages fail to decrypt
297-
if info.Chat.Server != types.BotServer {
288+
if info.Chat.Server != types.BotServer && encType != "msmsg" {
298289
go cli.sendRetryReceipt(node, info, isUnavailable)
299290
}
300291
cli.dispatchEvent(&events.UndecryptableMessage{

msgsecret.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func generateMsgSecretKey(
4949
useCaseSecret = append(useCaseSecret, modificationType...)
5050

5151
secretKey := hkdfutil.SHA256(origMsgSecret, nil, useCaseSecret, 32)
52-
additionalData := []byte(fmt.Sprintf("%s\x00%s", origMsgID, modificationSenderStr))
52+
additionalData := fmt.Appendf(nil, "%s\x00%s", origMsgID, modificationSenderStr)
5353

5454
return secretKey, additionalData
5555
}
@@ -129,7 +129,6 @@ func (cli *Client) encryptMsgSecret(chat, origSender types.JID, origMsgID types.
129129
}
130130

131131
func (cli *Client) decryptBotMessage(messageSecret []byte, msMsg messageEncryptedSecret, messageID types.MessageID, targetSenderJID types.JID, info *types.MessageInfo) ([]byte, error) {
132-
// gcm decrypt key generation
133132
newKey, additionalData := generateMsgSecretKey("", info.Sender, messageID, targetSenderJID, applyBotMessageHKDF(messageSecret))
134133

135134
plaintext, err := gcmutil.Decrypt(newKey, msMsg.GetEncIV(), msMsg.GetEncPayload(), additionalData)

0 commit comments

Comments
 (0)