7
7
package whatsmeow
8
8
9
9
import (
10
+ "context"
10
11
"crypto/sha256"
11
12
"fmt"
12
13
"time"
@@ -65,6 +66,7 @@ func generateMsgSecretKey(
65
66
func getOrigSenderFromKey (msg * events.Message , key * waCommon.MessageKey ) (types.JID , error ) {
66
67
if key .GetFromMe () {
67
68
// 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
68
70
return msg .Info .Sender , nil
69
71
} else if msg .Info .Chat .Server == types .DefaultUserServer || msg .Info .Chat .Server == types .HiddenUserServer {
70
72
sender , err := types .ParseJID (key .GetRemoteJID ())
@@ -93,30 +95,41 @@ func (cli *Client) decryptMsgSecret(msg *events.Message, useCase MsgSecretType,
93
95
if cli == nil {
94
96
return nil , ErrClientIsNil
95
97
}
96
- pollSender , err := getOrigSenderFromKey (msg , origMsgKey )
98
+ origSender , err := getOrigSenderFromKey (msg , origMsgKey )
97
99
if err != nil {
98
100
return nil , err
99
101
}
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 ())
101
103
if err != nil {
102
104
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 {
104
119
return nil , ErrOriginalMessageSecretNotFound
105
120
}
106
- secretKey , additionalData := generateMsgSecretKey (useCase , msg .Info .Sender , origMsgKey .GetID (), pollSender , baseEncKey )
121
+ secretKey , additionalData := generateMsgSecretKey (useCase , msg .Info .Sender , origMsgKey .GetID (), origSender , baseEncKey )
107
122
plaintext , err := gcmutil .Decrypt (secretKey , encrypted .GetEncIV (), encrypted .GetEncPayload (), additionalData )
108
123
if err != nil {
109
124
return nil , fmt .Errorf ("failed to decrypt secret message: %w" , err )
110
125
}
111
126
return plaintext , nil
112
127
}
113
128
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 ) {
115
130
if cli == nil {
116
131
return nil , nil , ErrClientIsNil
117
- }
118
- ownID := cli .getOwnID ()
119
- if ownID .IsEmpty () {
132
+ } else if ownID .IsEmpty () {
120
133
return nil , nil , ErrNotLoggedIn
121
134
}
122
135
@@ -305,7 +318,7 @@ func (cli *Client) EncryptPollVote(pollInfo *types.MessageInfo, vote *waE2E.Poll
305
318
if err != nil {
306
319
return nil , fmt .Errorf ("failed to marshal poll vote protobuf: %w" , err )
307
320
}
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 )
309
322
if err != nil {
310
323
return nil , fmt .Errorf ("failed to encrypt poll vote: %w" , err )
311
324
}
@@ -324,16 +337,18 @@ func (cli *Client) EncryptComment(rootMsgInfo *types.MessageInfo, comment *waE2E
324
337
if err != nil {
325
338
return nil , fmt .Errorf ("failed to marshal comment protobuf: %w" , err )
326
339
}
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 )
328
342
if err != nil {
329
343
return nil , fmt .Errorf ("failed to encrypt comment: %w" , err )
330
344
}
331
345
return & waE2E.Message {
332
346
EncCommentMessage : & waE2E.EncCommentMessage {
333
347
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 ),
337
352
},
338
353
EncPayload : ciphertext ,
339
354
EncIV : iv ,
@@ -342,21 +357,19 @@ func (cli *Client) EncryptComment(rootMsgInfo *types.MessageInfo, comment *waE2E
342
357
}
343
358
344
359
func (cli * Client ) EncryptReaction (rootMsgInfo * types.MessageInfo , reaction * waE2E.ReactionMessage ) (* waE2E.EncReactionMessage , error ) {
360
+ reactionKey := reaction .Key
361
+ reaction .Key = nil
345
362
plaintext , err := proto .Marshal (reaction )
346
363
if err != nil {
347
364
return nil , fmt .Errorf ("failed to marshal reaction protobuf: %w" , err )
348
365
}
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 )
350
367
if err != nil {
351
368
return nil , fmt .Errorf ("failed to encrypt reaction: %w" , err )
352
369
}
353
370
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 ,
361
374
}, nil
362
375
}
0 commit comments