-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
mtp_authorization.go
115 lines (99 loc) · 2.88 KB
/
mtp_authorization.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
package tdesktop
import (
"github.com/go-faster/errors"
"github.com/gotd/td/bin"
"github.com/gotd/td/internal/crypto"
)
// MTPAuthorization is a Telegram Desktop storage structure which stores MTProto session info.
//
// See https://github.com/telegramdesktop/tdesktop/blob/dev/Telegram/SourceFiles/main/main_account.cpp#L359.
type MTPAuthorization struct {
// UserID is a Telegram user ID.
UserID uint64
// MainDC is a main DC ID of this user.
MainDC int
// Key is a map of keys per DC ID.
Keys map[int]crypto.Key // DC ID -> Key
}
// See https://github.com/telegramdesktop/tdesktop/blob/v2.9.8/Telegram/SourceFiles/storage/storage_account.cpp#L898.
func readMTPData(tgf *tdesktopFile, localKey crypto.Key) (MTPAuthorization, error) {
encrypted, err := tgf.readArray()
if err != nil {
return MTPAuthorization{}, errors.Wrap(err, "read encrypted data")
}
decrypted, err := decryptLocal(encrypted, localKey)
if err != nil {
return MTPAuthorization{}, errors.Wrap(err, "decrypt data")
}
// Skip decrypted data length (uint32).
decrypted = decrypted[4:]
r := qtReader{buf: bin.Buffer{Buf: decrypted}}
// TODO(tdakkota): support other IDs.
var m MTPAuthorization
if err := m.deserialize(&r); err != nil {
return MTPAuthorization{}, errors.Wrap(err, "deserialize MTPAuthorization")
}
return m, err
}
func readKey(r *qtReader, k *crypto.Key) (uint32, error) {
dcID, err := r.readUint32()
if err != nil {
return 0, errors.Wrap(err, "read DC ID")
}
if err := r.consumeN(k[:], 256); err != nil {
return 0, errors.Wrap(err, "read auth key")
}
return dcID, nil
}
func (m *MTPAuthorization) deserialize(r *qtReader) error {
id, err := r.readUint32()
if err != nil {
return errors.Wrap(err, "read dbi ID")
}
if id != dbiMtpAuthorization {
return errors.Errorf("unexpected id %d", id)
}
if err := r.skip(4); err != nil {
return errors.Wrap(err, "read mainLength")
}
legacyUserID, err := r.readUint32()
if err != nil {
return errors.Wrap(err, "read legacyUserID")
}
legacyMainDCID, err := r.readUint32()
if err != nil {
return errors.Wrap(err, "read legacyMainDCID")
}
if (uint64(legacyUserID)<<32)|uint64(legacyMainDCID) == kWideIdsTag {
userID, err := r.readUint64()
if err != nil {
return errors.Wrap(err, "read userID")
}
mainDC, err := r.readUint32()
if err != nil {
return errors.Wrap(err, "read mainDcID")
}
m.UserID = userID
m.MainDC = int(mainDC)
} else {
m.UserID = uint64(legacyUserID)
m.MainDC = int(legacyMainDCID)
}
keys, err := r.readUint32()
if err != nil {
return errors.Wrap(err, "read keys length")
}
if m.Keys == nil {
m.Keys = make(map[int]crypto.Key, keys)
}
for i := 0; i < int(keys); i++ {
var key crypto.Key
dcID, err := readKey(r, &key)
if err != nil {
return errors.Wrapf(err, "read key %d", i)
}
// FIXME(tdakkota): what if there is more than one session per DC?
m.Keys[int(dcID)] = key
}
return nil
}