Skip to content

Commit 9910d48

Browse files
committed
misc: fix more @lid things
1 parent 5cf619f commit 9910d48

File tree

6 files changed

+105
-65
lines changed

6 files changed

+105
-65
lines changed

Diff for: client.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,14 @@ func (cli *Client) getOwnID() types.JID {
378378
if cli == nil {
379379
return types.EmptyJID
380380
}
381-
id := cli.Store.ID
382-
if id == nil {
381+
return cli.Store.GetJID()
382+
}
383+
384+
func (cli *Client) getOwnLID() types.JID {
385+
if cli == nil {
383386
return types.EmptyJID
384387
}
385-
return *id
388+
return cli.Store.GetLID()
386389
}
387390

388391
func (cli *Client) WaitForConnection(timeout time.Duration) bool {

Diff for: internals.go

+14-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: message.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func (cli *Client) parseMsgMetaInfo(node waBinary.Node) (metaInfo types.MsgMetaI
150150
ag := metaNode.AttrGetter()
151151
metaInfo.TargetID = types.MessageID(ag.OptionalString("target_id"))
152152
metaInfo.TargetSender = ag.OptionalJIDOrEmpty("target_sender_jid")
153-
deprecatedLIDSession, ok := ag.GetBool("lid_session", false)
153+
deprecatedLIDSession, ok := ag.GetBool("deprecated_lid_session", false)
154154
if ok {
155155
metaInfo.DeprecatedLIDSession = &deprecatedLIDSession
156156
}

Diff for: msgsecret.go

+34-21
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package whatsmeow
88

99
import (
10+
"context"
1011
"crypto/sha256"
1112
"fmt"
1213
"time"
@@ -65,6 +66,7 @@ func generateMsgSecretKey(
6566
func getOrigSenderFromKey(msg *events.Message, key *waCommon.MessageKey) (types.JID, error) {
6667
if key.GetFromMe() {
6768
// fromMe always means the poll and vote were sent by the same user
69+
// TODO this is wrong if the message key used @s.whatsapp.net, but the new event is from @lid
6870
return msg.Info.Sender, nil
6971
} else if msg.Info.Chat.Server == types.DefaultUserServer || msg.Info.Chat.Server == types.HiddenUserServer {
7072
sender, err := types.ParseJID(key.GetRemoteJID())
@@ -93,30 +95,41 @@ func (cli *Client) decryptMsgSecret(msg *events.Message, useCase MsgSecretType,
9395
if cli == nil {
9496
return nil, ErrClientIsNil
9597
}
96-
pollSender, err := getOrigSenderFromKey(msg, origMsgKey)
98+
origSender, err := getOrigSenderFromKey(msg, origMsgKey)
9799
if err != nil {
98100
return nil, err
99101
}
100-
baseEncKey, err := cli.Store.MsgSecrets.GetMessageSecret(msg.Info.Chat, pollSender, origMsgKey.GetID())
102+
baseEncKey, err := cli.Store.MsgSecrets.GetMessageSecret(msg.Info.Chat, origSender, origMsgKey.GetID())
101103
if err != nil {
102104
return nil, fmt.Errorf("failed to get original message secret key: %w", err)
103-
} else if baseEncKey == nil {
105+
}
106+
if baseEncKey == nil && origMsgKey.GetFromMe() && origSender.Server == types.HiddenUserServer {
107+
origSender, err = cli.Store.LIDs.GetPNForLID(context.TODO(), origSender)
108+
if err != nil {
109+
return nil, fmt.Errorf("%w (also failed to get PN for LID: %w)", ErrOriginalMessageSecretNotFound, err)
110+
} else if origSender.IsEmpty() {
111+
return nil, fmt.Errorf("%w (PN for LID not found)", ErrOriginalMessageSecretNotFound)
112+
}
113+
baseEncKey, err = cli.Store.MsgSecrets.GetMessageSecret(msg.Info.Chat, origSender, origMsgKey.GetID())
114+
if err != nil {
115+
return nil, fmt.Errorf("failed to get original message secret key with PN: %w", err)
116+
}
117+
}
118+
if baseEncKey == nil {
104119
return nil, ErrOriginalMessageSecretNotFound
105120
}
106-
secretKey, additionalData := generateMsgSecretKey(useCase, msg.Info.Sender, origMsgKey.GetID(), pollSender, baseEncKey)
121+
secretKey, additionalData := generateMsgSecretKey(useCase, msg.Info.Sender, origMsgKey.GetID(), origSender, baseEncKey)
107122
plaintext, err := gcmutil.Decrypt(secretKey, encrypted.GetEncIV(), encrypted.GetEncPayload(), additionalData)
108123
if err != nil {
109124
return nil, fmt.Errorf("failed to decrypt secret message: %w", err)
110125
}
111126
return plaintext, nil
112127
}
113128

114-
func (cli *Client) encryptMsgSecret(chat, origSender types.JID, origMsgID types.MessageID, useCase MsgSecretType, plaintext []byte) (ciphertext, iv []byte, err error) {
129+
func (cli *Client) encryptMsgSecret(ownID, chat, origSender types.JID, origMsgID types.MessageID, useCase MsgSecretType, plaintext []byte) (ciphertext, iv []byte, err error) {
115130
if cli == nil {
116131
return nil, nil, ErrClientIsNil
117-
}
118-
ownID := cli.getOwnID()
119-
if ownID.IsEmpty() {
132+
} else if ownID.IsEmpty() {
120133
return nil, nil, ErrNotLoggedIn
121134
}
122135

@@ -305,7 +318,7 @@ func (cli *Client) EncryptPollVote(pollInfo *types.MessageInfo, vote *waE2E.Poll
305318
if err != nil {
306319
return nil, fmt.Errorf("failed to marshal poll vote protobuf: %w", err)
307320
}
308-
ciphertext, iv, err := cli.encryptMsgSecret(pollInfo.Chat, pollInfo.Sender, pollInfo.ID, EncSecretPollVote, plaintext)
321+
ciphertext, iv, err := cli.encryptMsgSecret(cli.getOwnID(), pollInfo.Chat, pollInfo.Sender, pollInfo.ID, EncSecretPollVote, plaintext)
309322
if err != nil {
310323
return nil, fmt.Errorf("failed to encrypt poll vote: %w", err)
311324
}
@@ -324,16 +337,18 @@ func (cli *Client) EncryptComment(rootMsgInfo *types.MessageInfo, comment *waE2E
324337
if err != nil {
325338
return nil, fmt.Errorf("failed to marshal comment protobuf: %w", err)
326339
}
327-
ciphertext, iv, err := cli.encryptMsgSecret(rootMsgInfo.Chat, rootMsgInfo.Sender, rootMsgInfo.ID, EncSecretComment, plaintext)
340+
// TODO is hardcoding LID here correct? What about polls?
341+
ciphertext, iv, err := cli.encryptMsgSecret(cli.getOwnLID(), rootMsgInfo.Chat, rootMsgInfo.Sender, rootMsgInfo.ID, EncSecretComment, plaintext)
328342
if err != nil {
329343
return nil, fmt.Errorf("failed to encrypt comment: %w", err)
330344
}
331345
return &waE2E.Message{
332346
EncCommentMessage: &waE2E.EncCommentMessage{
333347
TargetMessageKey: &waCommon.MessageKey{
334-
RemoteJID: proto.String(rootMsgInfo.Chat.String()),
335-
FromMe: proto.Bool(rootMsgInfo.IsFromMe),
336-
ID: proto.String(rootMsgInfo.ID),
348+
RemoteJID: proto.String(rootMsgInfo.Chat.String()),
349+
Participant: proto.String(rootMsgInfo.Sender.ToNonAD().String()),
350+
FromMe: proto.Bool(rootMsgInfo.IsFromMe),
351+
ID: proto.String(rootMsgInfo.ID),
337352
},
338353
EncPayload: ciphertext,
339354
EncIV: iv,
@@ -342,21 +357,19 @@ func (cli *Client) EncryptComment(rootMsgInfo *types.MessageInfo, comment *waE2E
342357
}
343358

344359
func (cli *Client) EncryptReaction(rootMsgInfo *types.MessageInfo, reaction *waE2E.ReactionMessage) (*waE2E.EncReactionMessage, error) {
360+
reactionKey := reaction.Key
361+
reaction.Key = nil
345362
plaintext, err := proto.Marshal(reaction)
346363
if err != nil {
347364
return nil, fmt.Errorf("failed to marshal reaction protobuf: %w", err)
348365
}
349-
ciphertext, iv, err := cli.encryptMsgSecret(rootMsgInfo.Chat, rootMsgInfo.Sender, rootMsgInfo.ID, EncSecretReaction, plaintext)
366+
ciphertext, iv, err := cli.encryptMsgSecret(cli.getOwnLID(), rootMsgInfo.Chat, rootMsgInfo.Sender, rootMsgInfo.ID, EncSecretReaction, plaintext)
350367
if err != nil {
351368
return nil, fmt.Errorf("failed to encrypt reaction: %w", err)
352369
}
353370
return &waE2E.EncReactionMessage{
354-
TargetMessageKey: &waCommon.MessageKey{
355-
RemoteJID: proto.String(rootMsgInfo.Chat.String()),
356-
FromMe: proto.Bool(rootMsgInfo.IsFromMe),
357-
ID: proto.String(rootMsgInfo.ID),
358-
},
359-
EncPayload: ciphertext,
360-
EncIV: iv,
371+
TargetMessageKey: reactionKey,
372+
EncPayload: ciphertext,
373+
EncIV: iv,
361374
}, nil
362375
}

0 commit comments

Comments
 (0)