-
Notifications
You must be signed in to change notification settings - Fork 5
/
decode.go
307 lines (284 loc) · 10.5 KB
/
decode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Copyright © 2023 whatsmeow
// Redeveloped by Amirul Dev
package appstate
import (
"bytes"
"crypto/sha256"
"encoding/json"
"fmt"
"google.golang.org/protobuf/proto"
waBinary "github.com/amiruldev20/waSocket/binary"
waProto "github.com/amiruldev20/waSocket/binary/proto"
"github.com/amiruldev20/waSocket/store"
"github.com/amiruldev20/waSocket/util/cbcutil"
)
// PatchList represents a decoded response to getting app state patches from the WhatsApp servers.
type PatchList struct {
Name WAPatchName
HasMorePatches bool
Patches []*waProto.SyncdPatch
Snapshot *waProto.SyncdSnapshot
}
// DownloadExternalFunc is a function that can download a blob of external app state patches.
type DownloadExternalFunc func(*waProto.ExternalBlobReference) ([]byte, error)
func parseSnapshotInternal(collection *waBinary.Node, downloadExternal DownloadExternalFunc) (*waProto.SyncdSnapshot, error) {
snapshotNode := collection.GetChildByTag("snapshot")
rawSnapshot, ok := snapshotNode.Content.([]byte)
if snapshotNode.Tag != "snapshot" || !ok {
return nil, nil
}
var snapshot waProto.ExternalBlobReference
err := proto.Unmarshal(rawSnapshot, &snapshot)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal snapshot: %w", err)
}
var rawData []byte
rawData, err = downloadExternal(&snapshot)
if err != nil {
return nil, fmt.Errorf("failed to download external mutations: %w", err)
}
var downloaded waProto.SyncdSnapshot
err = proto.Unmarshal(rawData, &downloaded)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal mutation list: %w", err)
}
return &downloaded, nil
}
func parsePatchListInternal(collection *waBinary.Node, downloadExternal DownloadExternalFunc) ([]*waProto.SyncdPatch, error) {
patchesNode := collection.GetChildByTag("patches")
patchNodes := patchesNode.GetChildren()
patches := make([]*waProto.SyncdPatch, 0, len(patchNodes))
for i, patchNode := range patchNodes {
rawPatch, ok := patchNode.Content.([]byte)
if patchNode.Tag != "patch" || !ok {
continue
}
var patch waProto.SyncdPatch
err := proto.Unmarshal(rawPatch, &patch)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal patch #%d: %w", i+1, err)
}
if patch.GetExternalMutations() != nil && downloadExternal != nil {
var rawData []byte
rawData, err = downloadExternal(patch.GetExternalMutations())
if err != nil {
return nil, fmt.Errorf("failed to download external mutations: %w", err)
}
var downloaded waProto.SyncdMutations
err = proto.Unmarshal(rawData, &downloaded)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal mutation list: %w", err)
} else if len(downloaded.GetMutations()) == 0 {
return nil, fmt.Errorf("didn't get any mutations from download")
}
patch.Mutations = downloaded.Mutations
}
patches = append(patches, &patch)
}
return patches, nil
}
// ParsePatchList will decode an XML node containing app state patches, including downloading any external blobs.
func ParsePatchList(node *waBinary.Node, downloadExternal DownloadExternalFunc) (*PatchList, error) {
collection := node.GetChildByTag("sync", "collection")
ag := collection.AttrGetter()
snapshot, err := parseSnapshotInternal(&collection, downloadExternal)
if err != nil {
return nil, err
}
patches, err := parsePatchListInternal(&collection, downloadExternal)
if err != nil {
return nil, err
}
list := &PatchList{
Name: WAPatchName(ag.String("name")),
HasMorePatches: ag.OptionalBool("has_more_patches"),
Patches: patches,
Snapshot: snapshot,
}
return list, ag.Error()
}
type patchOutput struct {
RemovedMACs [][]byte
AddedMACs []store.AppStateMutationMAC
Mutations []Mutation
}
func (proc *Processor) decodeMutations(mutations []*waProto.SyncdMutation, out *patchOutput, validateMACs bool) error {
for i, mutation := range mutations {
keyID := mutation.GetRecord().GetKeyId().GetId()
keys, err := proc.getAppStateKey(keyID)
if err != nil {
return fmt.Errorf("failed to get key %X to decode mutation: %w", keyID, err)
}
content := mutation.GetRecord().GetValue().GetBlob()
content, valueMAC := content[:len(content)-32], content[len(content)-32:]
if validateMACs {
expectedValueMAC := generateContentMAC(mutation.GetOperation(), content, keyID, keys.ValueMAC)
if !bytes.Equal(expectedValueMAC, valueMAC) {
return fmt.Errorf("failed to verify mutation #%d: %w", i+1, ErrMismatchingContentMAC)
}
}
iv, content := content[:16], content[16:]
plaintext, err := cbcutil.Decrypt(keys.ValueEncryption, iv, content)
if err != nil {
return fmt.Errorf("failed to decrypt mutation #%d: %w", i+1, err)
}
var syncAction waProto.SyncActionData
err = proto.Unmarshal(plaintext, &syncAction)
if err != nil {
return fmt.Errorf("failed to unmarshal mutation #%d: %w", i+1, err)
}
indexMAC := mutation.GetRecord().GetIndex().GetBlob()
if validateMACs {
expectedIndexMAC := concatAndHMAC(sha256.New, keys.Index, syncAction.Index)
if !bytes.Equal(expectedIndexMAC, indexMAC) {
return fmt.Errorf("failed to verify mutation #%d: %w", i+1, ErrMismatchingIndexMAC)
}
}
var index []string
err = json.Unmarshal(syncAction.GetIndex(), &index)
if err != nil {
return fmt.Errorf("failed to unmarshal index of mutation #%d: %w", i+1, err)
}
if mutation.GetOperation() == waProto.SyncdMutation_REMOVE {
out.RemovedMACs = append(out.RemovedMACs, indexMAC)
} else if mutation.GetOperation() == waProto.SyncdMutation_SET {
out.AddedMACs = append(out.AddedMACs, store.AppStateMutationMAC{
IndexMAC: indexMAC,
ValueMAC: valueMAC,
})
}
out.Mutations = append(out.Mutations, Mutation{
Operation: mutation.GetOperation(),
Action: syncAction.GetValue(),
Index: index,
IndexMAC: indexMAC,
ValueMAC: valueMAC,
})
}
return nil
}
func (proc *Processor) storeMACs(name WAPatchName, currentState HashState, out *patchOutput) {
err := proc.Store.AppState.PutAppStateVersion(string(name), currentState.Version, currentState.Hash)
if err != nil {
proc.Log.Errorf("Failed to update app state version in the database: %v", err)
}
err = proc.Store.AppState.DeleteAppStateMutationMACs(string(name), out.RemovedMACs)
if err != nil {
proc.Log.Errorf("Failed to remove deleted mutation MACs from the database: %v", err)
}
err = proc.Store.AppState.PutAppStateMutationMACs(string(name), currentState.Version, out.AddedMACs)
if err != nil {
proc.Log.Errorf("Failed to insert added mutation MACs to the database: %v", err)
}
}
func (proc *Processor) validateSnapshotMAC(name WAPatchName, currentState HashState, keyID, expectedSnapshotMAC []byte) (keys ExpandedAppStateKeys, err error) {
keys, err = proc.getAppStateKey(keyID)
if err != nil {
err = fmt.Errorf("failed to get key %X to verify patch v%d MACs: %w", keyID, currentState.Version, err)
return
}
snapshotMAC := currentState.generateSnapshotMAC(name, keys.SnapshotMAC)
if !bytes.Equal(snapshotMAC, expectedSnapshotMAC) {
err = fmt.Errorf("failed to verify patch v%d: %w", currentState.Version, ErrMismatchingLTHash)
}
return
}
func (proc *Processor) decodeSnapshot(name WAPatchName, ss *waProto.SyncdSnapshot, initialState HashState, validateMACs bool, newMutationsInput []Mutation) (newMutations []Mutation, currentState HashState, err error) {
currentState = initialState
currentState.Version = ss.GetVersion().GetVersion()
encryptedMutations := make([]*waProto.SyncdMutation, len(ss.GetRecords()))
for i, record := range ss.GetRecords() {
encryptedMutations[i] = &waProto.SyncdMutation{
Operation: waProto.SyncdMutation_SET.Enum(),
Record: record,
}
}
var warn []error
warn, err = currentState.updateHash(encryptedMutations, func(indexMAC []byte, maxIndex int) ([]byte, error) {
return nil, nil
})
if len(warn) > 0 {
proc.Log.Warnf("Warnings while updating hash for %s: %+v", name, warn)
}
if err != nil {
err = fmt.Errorf("failed to update state hash: %w", err)
return
}
if validateMACs {
_, err = proc.validateSnapshotMAC(name, currentState, ss.GetKeyId().GetId(), ss.GetMac())
if err != nil {
return
}
}
var out patchOutput
out.Mutations = newMutationsInput
err = proc.decodeMutations(encryptedMutations, &out, validateMACs)
if err != nil {
err = fmt.Errorf("failed to decode snapshot of v%d: %w", currentState.Version, err)
return
}
proc.storeMACs(name, currentState, &out)
newMutations = out.Mutations
return
}
// DecodePatches will decode all the patches in a PatchList into a list of app state mutations.
func (proc *Processor) DecodePatches(list *PatchList, initialState HashState, validateMACs bool) (newMutations []Mutation, currentState HashState, err error) {
currentState = initialState
var expectedLength int
if list.Snapshot != nil {
expectedLength = len(list.Snapshot.GetRecords())
}
for _, patch := range list.Patches {
expectedLength += len(patch.GetMutations())
}
newMutations = make([]Mutation, 0, expectedLength)
if list.Snapshot != nil {
newMutations, currentState, err = proc.decodeSnapshot(list.Name, list.Snapshot, currentState, validateMACs, newMutations)
if err != nil {
return
}
}
for _, patch := range list.Patches {
version := patch.GetVersion().GetVersion()
currentState.Version = version
var warn []error
warn, err = currentState.updateHash(patch.GetMutations(), func(indexMAC []byte, maxIndex int) ([]byte, error) {
for i := maxIndex - 1; i >= 0; i-- {
if bytes.Equal(patch.Mutations[i].GetRecord().GetIndex().GetBlob(), indexMAC) {
value := patch.Mutations[i].GetRecord().GetValue().GetBlob()
return value[len(value)-32:], nil
}
}
// Previous value not found in current patch, look in the database
return proc.Store.AppState.GetAppStateMutationMAC(string(list.Name), indexMAC)
})
if len(warn) > 0 {
proc.Log.Warnf("Warnings while updating hash for %s: %+v", list.Name, warn)
}
if err != nil {
err = fmt.Errorf("failed to update state hash: %w", err)
return
}
if validateMACs {
var keys ExpandedAppStateKeys
keys, err = proc.validateSnapshotMAC(list.Name, currentState, patch.GetKeyId().GetId(), patch.GetSnapshotMac())
if err != nil {
return
}
patchMAC := generatePatchMAC(patch, list.Name, keys.PatchMAC, patch.GetVersion().GetVersion())
if !bytes.Equal(patchMAC, patch.GetPatchMac()) {
err = fmt.Errorf("failed to verify patch v%d: %w", version, ErrMismatchingPatchMAC)
return
}
}
var out patchOutput
out.Mutations = newMutations
err = proc.decodeMutations(patch.GetMutations(), &out, validateMACs)
if err != nil {
return
}
proc.storeMACs(list.Name, currentState, &out)
newMutations = out.Mutations
}
return
}