-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
e2ee-register.go
226 lines (213 loc) · 8.15 KB
/
e2ee-register.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
// mautrix-meta - A Matrix-Facebook Messenger and Instagram DM puppeting bridge.
// Copyright (C) 2024 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package messagix
import (
"bytes"
"context"
"crypto/sha256"
"encoding/base64"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"slices"
"strconv"
"strings"
"time"
"github.com/google/uuid"
"github.com/rs/zerolog"
"go.mau.fi/libsignal/ecc"
"google.golang.org/protobuf/proto"
"go.mau.fi/whatsmeow/binary/armadillo/waArmadilloICDC"
waProto "go.mau.fi/whatsmeow/binary/proto"
"go.mau.fi/whatsmeow/store"
"go.mau.fi/whatsmeow/types"
)
type ICDCFetchResponse struct {
DeviceIdentities []string `json:"device_identities"`
ICDCSeq int `json:"icdc_seq"`
Status int `json:"status"`
}
func (ifr *ICDCFetchResponse) DeviceIdentityBytes() (deviceIdentityBytes [][32]byte, err error) {
deviceIdentityBytes = make([][32]byte, len(ifr.DeviceIdentities))
for i, deviceIdentity := range ifr.DeviceIdentities {
var ident []byte
ident, err = base64.RawURLEncoding.DecodeString(deviceIdentity)
if err != nil {
break
} else if len(ident) != 32 {
err = fmt.Errorf("device identity is not 32 bytes long")
break
}
deviceIdentityBytes[i] = [32]byte(ident)
}
return
}
type ICDCRegisterResponse struct {
ICDCSuccess bool `json:"icdc_success"` // not always present?
Product string `json:"product"` // msgr
Status int `json:"status"` // 200
Type string `json:"type"` // "new"
WADeviceID int `json:"wa_device_id"`
}
func (c *Client) doE2EERequest(ctx context.Context, endpoint string, body url.Values, into any) error {
addr := c.getEndpoint(endpoint)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, addr, strings.NewReader(body.Encode()))
if err != nil {
return fmt.Errorf("failed to prepare request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
req.Header.Set("accept-language", "en-US,en;q=0.9")
req.Header.Set("User-Agent", UserAgent)
req.Header.Set("sec-fetch-dest", "empty")
req.Header.Set("sec-fetch-mode", "cors")
req.Header.Set("sec-fetch-site", "cross-site")
req.Header.Set("origin", c.getEndpoint("base_url"))
req.Header.Set("referer", c.getEndpoint("messages")+"/")
zerolog.Ctx(ctx).Trace().
Str("url", addr).
Any("body", body).
Msg("ICDC request")
resp, err := c.http.Do(req)
if err != nil {
return fmt.Errorf("failed to send request: %w", err)
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
logEvt := zerolog.Ctx(ctx).Trace().
Str("url", addr).
Int("status_code", resp.StatusCode)
if json.Valid(respBody) {
logEvt.RawJSON("response_body", respBody)
} else {
logEvt.Str("response_body", base64.StdEncoding.EncodeToString(respBody))
}
logEvt.Msg("ICDC response")
if err != nil {
return fmt.Errorf("failed to read response body: %w", err)
} else if resp.StatusCode >= 300 || resp.StatusCode < 200 {
return fmt.Errorf("unexpected status code %d", resp.StatusCode)
} else if err = json.Unmarshal(respBody, into); err != nil {
return fmt.Errorf("failed to unmarshal response: %w", err)
}
return nil
}
func (c *Client) fetchICDC(ctx context.Context, fbid int64, deviceUUID uuid.UUID) (*ICDCFetchResponse, error) {
formBody := url.Values{
"fbid": {strconv.FormatInt(fbid, 10)},
"fb_cat": {c.configs.browserConfigTable.MessengerWebInitData.CryptoAuthToken.EncryptedSerializedCat},
"app_id": {strconv.FormatInt(c.configs.browserConfigTable.MessengerWebInitData.AppID, 10)},
"device_id": {deviceUUID.String()},
}
var icdcResp ICDCFetchResponse
err := c.doE2EERequest(ctx, "icdc_fetch", formBody, &icdcResp)
return &icdcResp, err
}
func calculateIdentitiesHash(identities [][32]byte) []byte {
identitiesSlice := sliceifyIdentities(identities)
slices.SortFunc(identitiesSlice, bytes.Compare)
ihash := sha256.Sum256(bytes.Join(identitiesSlice, nil))
return ihash[:10]
}
func sliceifyIdentities(identities [][32]byte) [][]byte {
sliceIdentities := make([][]byte, len(identities))
for i, identity := range identities {
sliceIdentities[i] = identity[:]
}
return sliceIdentities
}
func (c *Client) SetDevice(dev *store.Device) {
c.device = dev
}
func (c *Client) RegisterE2EE(ctx context.Context, fbid int64) error {
if c.device == nil {
return fmt.Errorf("cannot register for E2EE without a device")
}
if c.device.FacebookUUID == uuid.Nil {
c.device.FacebookUUID = uuid.New()
}
icdcMeta, err := c.fetchICDC(ctx, fbid, c.device.FacebookUUID)
if err != nil {
return fmt.Errorf("failed to fetch ICDC metadata: %w", err)
}
deviceIdentities, err := icdcMeta.DeviceIdentityBytes()
ownIdentityIndex := slices.Index(deviceIdentities, *c.device.IdentityKey.Pub)
if ownIdentityIndex == -1 {
ownIdentityIndex = len(deviceIdentities)
deviceIdentities = append(deviceIdentities, *c.device.IdentityKey.Pub)
// TODO does this need to be incremented when reregistering?
icdcMeta.ICDCSeq++
}
icdcTS := time.Now().Unix()
unsignedList, err := proto.Marshal(&waArmadilloICDC.ICDCIdentityList{
Seq: int32(icdcMeta.ICDCSeq),
Timestamp: icdcTS,
Devices: sliceifyIdentities(deviceIdentities),
SigningDeviceIndex: int32(ownIdentityIndex),
})
if err != nil {
return fmt.Errorf("failed to marshal ICDC identity list: %w", err)
}
signature := ecc.CalculateSignature(ecc.NewDjbECPrivateKey(*c.device.IdentityKey.Priv), unsignedList)
signedList, err := proto.Marshal(&waArmadilloICDC.SignedICDCIdentityList{
Details: unsignedList,
Signature: signature[:],
})
if err != nil {
return fmt.Errorf("failed to marshal signed ICDC identity list: %w", err)
}
formBody := url.Values{
"fbid": {strconv.FormatInt(fbid, 10)},
"fb_cat": {c.configs.browserConfigTable.MessengerWebInitData.CryptoAuthToken.EncryptedSerializedCat},
"app_id": {strconv.FormatInt(c.configs.browserConfigTable.MessengerWebInitData.AppID, 10)},
"device_id": {c.device.FacebookUUID.String()},
"e_regid": {base64.StdEncoding.EncodeToString(binary.BigEndian.AppendUint32(nil, c.device.RegistrationID))},
"e_keytype": {base64.StdEncoding.EncodeToString([]byte{ecc.DjbType})},
"e_ident": {base64.StdEncoding.EncodeToString(c.device.IdentityKey.Pub[:])},
"e_skey_id": {base64.StdEncoding.EncodeToString(binary.BigEndian.AppendUint32(nil, c.device.SignedPreKey.KeyID)[1:])},
"e_skey_val": {base64.StdEncoding.EncodeToString(c.device.SignedPreKey.Pub[:])},
"e_skey_sig": {base64.StdEncoding.EncodeToString(c.device.SignedPreKey.Signature[:])},
"icdc_list": {base64.StdEncoding.EncodeToString(signedList)},
"icdc_ts": {strconv.FormatInt(icdcTS, 10)},
"icdc_seq": {strconv.Itoa(icdcMeta.ICDCSeq)},
"ihash": {base64.StdEncoding.EncodeToString(calculateIdentitiesHash(deviceIdentities))},
}
var icdcResp ICDCRegisterResponse
err = c.doE2EERequest(ctx, "icdc_register", formBody, &icdcResp)
if err != nil {
return err
} else if icdcResp.Status != 200 {
return fmt.Errorf("ICDC registration returned non-200 inner status %d", icdcResp.Status)
}
c.device.ID = &types.JID{
User: strconv.FormatInt(fbid, 10),
Device: uint16(icdcResp.WADeviceID),
Server: types.MessengerServer,
}
// This is a hack since currently whatsmeow requires it to be set
c.device.Account = &waProto.ADVSignedDeviceIdentity{
Details: make([]byte, 0),
AccountSignatureKey: make([]byte, 32),
AccountSignature: make([]byte, 64),
DeviceSignature: make([]byte, 64),
}
zerolog.Ctx(ctx).Info().
Stringer("jid", c.device.ID).
Msg("ICDC registration successful")
return err
}