7a04f298d2
- update to latest telegram layer - remove some references to fields in tg.Entities that don't exist in the schema - originally added here: https://github.com/beeper/td/commit/820929062a2ba0104397bc01235ab58a9cff780e - referenced here - https://github.com/mautrix/telegramgo/commit/124f0967ed195b5a380c9bd02e170ada9710dde3 - https://github.com/mautrix/telegramgo/commit/4205047aab2e0639217148b5d125bfaab668bd8e
72 lines
1.9 KiB
Go
72 lines
1.9 KiB
Go
package tdesktop
|
|
|
|
import (
|
|
"encoding/binary"
|
|
|
|
"github.com/go-faster/errors"
|
|
|
|
"go.mau.fi/mautrix-telegram/pkg/gotd/crypto"
|
|
)
|
|
|
|
type keyData struct {
|
|
localKey crypto.Key
|
|
accountsIDx []uint32
|
|
}
|
|
|
|
// See https://github.com/telegramdesktop/tdesktop/blob/v2.9.8/Telegram/SourceFiles/storage/storage_domain.cpp#L119-L159.
|
|
func readKeyData(tgf *tdesktopFile, passcode []byte) (_ keyData, rErr error) {
|
|
salt, err := tgf.readArray()
|
|
if err != nil {
|
|
return keyData{}, errors.Wrap(err, "read salt")
|
|
}
|
|
if l := len(salt); l != localEncryptSaltSize {
|
|
return keyData{}, errors.Errorf("invalid salt length %d", l)
|
|
}
|
|
|
|
passcodeKey := createLocalKey(passcode, salt)
|
|
keyEncrypted, err := tgf.readArray()
|
|
if err != nil {
|
|
return keyData{}, errors.Wrap(err, "read keyEncrypted")
|
|
}
|
|
keyInnerData, err := decryptLocal(keyEncrypted, passcodeKey)
|
|
if err != nil {
|
|
return keyData{}, errors.Wrap(err, "decrypt keyEncrypted")
|
|
}
|
|
key, _, err := readArray(keyInnerData, binary.LittleEndian)
|
|
if err != nil {
|
|
return keyData{}, errors.Wrap(err, "read key")
|
|
}
|
|
|
|
if l := len(key); l < len(crypto.Key{}) {
|
|
return keyData{}, errors.Errorf("key too small (%d)", l)
|
|
}
|
|
var localKey crypto.Key
|
|
copy(localKey[:], key)
|
|
|
|
infoEncrypted, err := tgf.readArray()
|
|
if err != nil {
|
|
return keyData{}, errors.Wrap(err, "read infoEncrypted")
|
|
}
|
|
infoDecrypted, err := decryptLocal(infoEncrypted, localKey)
|
|
if err != nil {
|
|
return keyData{}, ErrKeyInfoDecrypt
|
|
}
|
|
// Skip decrypted data length.
|
|
infoDecrypted = infoDecrypted[4:]
|
|
// Read count of accounts.
|
|
count := int(binary.BigEndian.Uint32(infoDecrypted))
|
|
infoDecrypted = infoDecrypted[4:]
|
|
|
|
// Preallocate accountsIDx.
|
|
accountsIDx := make([]uint32, 0, count)
|
|
for i := 0; i < count; i++ {
|
|
idx := binary.BigEndian.Uint32(infoDecrypted[i*4:])
|
|
accountsIDx = append(accountsIDx, idx)
|
|
}
|
|
|
|
return keyData{
|
|
localKey: localKey,
|
|
accountsIDx: accountsIDx,
|
|
}, nil
|
|
}
|