2017-06-26 20:38:16 +03:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 13:23:14 +03:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-06-26 20:38:16 +03:00
|
|
|
|
2018-01-03 13:23:14 +03:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-06-26 20:38:16 +03:00
|
|
|
*/
|
|
|
|
#include "mtproto/special_config_request.h"
|
|
|
|
|
|
|
|
#include "mtproto/rsa_public_key.h"
|
|
|
|
#include "mtproto/dc_options.h"
|
|
|
|
#include "mtproto/auth_key.h"
|
|
|
|
#include "base/openssl_help.h"
|
|
|
|
#include <openssl/aes.h>
|
|
|
|
|
|
|
|
namespace MTP {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kPublicKey = str_const("\
|
|
|
|
-----BEGIN RSA PUBLIC KEY-----\n\
|
|
|
|
MIIBCgKCAQEAyr+18Rex2ohtVy8sroGPBwXD3DOoKCSpjDqYoXgCqB7ioln4eDCF\n\
|
|
|
|
fOBUlfXUEvM/fnKCpF46VkAftlb4VuPDeQSS/ZxZYEGqHaywlroVnXHIjgqoxiAd\n\
|
|
|
|
192xRGreuXIaUKmkwlM9JID9WS2jUsTpzQ91L8MEPLJ/4zrBwZua8W5fECwCCh2c\n\
|
|
|
|
9G5IzzBm+otMS/YKwmR1olzRCyEkyAEjXWqBI9Ftv5eG8m0VkBzOG655WIYdyV0H\n\
|
|
|
|
fDK/NWcvGqa0w/nriMD6mDjKOryamw0OP9QuYgMN0C9xMW9y8SmP4h92OAWodTYg\n\
|
|
|
|
Y1hZCxdv6cs5UnW9+PWvS+WIbkh+GaWYxwIDAQAB\n\
|
|
|
|
-----END RSA PUBLIC KEY-----\
|
|
|
|
");
|
|
|
|
|
2018-05-02 22:27:03 +03:00
|
|
|
bool CheckPhoneByPrefixesRules(const QString &phone, const QString &rules) {
|
|
|
|
auto result = false;
|
|
|
|
for (const auto &prefix : rules.split(',')) {
|
|
|
|
if (prefix.isEmpty()) {
|
|
|
|
result = true;
|
|
|
|
} else if (prefix[0] == '+' && phone.startsWith(prefix.mid(1))) {
|
|
|
|
result = true;
|
|
|
|
} else if (prefix[0] == '-' && phone.startsWith(prefix.mid(1))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-06-26 20:38:16 +03:00
|
|
|
} // namespace
|
|
|
|
|
2018-03-20 18:21:36 +04:00
|
|
|
SpecialConfigRequest::SpecialConfigRequest(
|
|
|
|
base::lambda<void(
|
|
|
|
DcId dcId,
|
|
|
|
const std::string &ip,
|
2018-05-02 22:27:03 +03:00
|
|
|
int port,
|
|
|
|
bytes::const_span secret)> callback,
|
|
|
|
const QString &phone)
|
|
|
|
: _callback(std::move(callback))
|
|
|
|
, _phone(phone) {
|
|
|
|
performAppRequest();
|
2017-06-26 20:38:16 +03:00
|
|
|
performDnsRequest();
|
|
|
|
}
|
|
|
|
|
2018-05-02 22:27:03 +03:00
|
|
|
void SpecialConfigRequest::performAppRequest() {
|
2017-06-26 20:38:16 +03:00
|
|
|
auto appUrl = QUrl();
|
|
|
|
appUrl.setScheme(qsl("https"));
|
2018-03-11 22:51:29 +03:00
|
|
|
appUrl.setHost(qsl("software-download.microsoft.com"));
|
|
|
|
appUrl.setPath(cTestMode()
|
|
|
|
? qsl("/test/config.txt")
|
2018-05-02 22:27:03 +03:00
|
|
|
: qsl("/prodv2/config.txt"));
|
2017-06-26 20:38:16 +03:00
|
|
|
auto appRequest = QNetworkRequest(appUrl);
|
2018-03-11 22:51:29 +03:00
|
|
|
appRequest.setRawHeader("Host", "tcdnb.azureedge.net");
|
2018-05-02 22:27:03 +03:00
|
|
|
_appReply.reset(_manager.get(appRequest));
|
|
|
|
connect(_appReply.get(), &QNetworkReply::finished, this, [=] {
|
|
|
|
appFinished();
|
2018-03-20 18:21:36 +04:00
|
|
|
});
|
2017-06-26 20:38:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpecialConfigRequest::performDnsRequest() {
|
|
|
|
auto dnsUrl = QUrl();
|
|
|
|
dnsUrl.setScheme(qsl("https"));
|
2018-05-02 22:27:03 +03:00
|
|
|
dnsUrl.setHost(qsl("www.google.com"));
|
2017-06-26 20:38:16 +03:00
|
|
|
dnsUrl.setPath(qsl("/resolve"));
|
2018-03-20 18:21:36 +04:00
|
|
|
dnsUrl.setQuery(
|
|
|
|
qsl("name=%1.stel.com&type=16").arg(
|
2018-05-02 22:27:03 +03:00
|
|
|
cTestMode() ? qsl("tap") : qsl("apv2")));
|
2017-06-26 20:38:16 +03:00
|
|
|
auto dnsRequest = QNetworkRequest(QUrl(dnsUrl));
|
|
|
|
dnsRequest.setRawHeader("Host", "dns.google.com");
|
|
|
|
_dnsReply.reset(_manager.get(dnsRequest));
|
2018-03-20 18:21:36 +04:00
|
|
|
connect(_dnsReply.get(), &QNetworkReply::finished, this, [this] {
|
|
|
|
dnsFinished();
|
|
|
|
});
|
2017-06-26 20:38:16 +03:00
|
|
|
}
|
|
|
|
|
2018-05-02 22:27:03 +03:00
|
|
|
void SpecialConfigRequest::appFinished() {
|
|
|
|
if (!_appReply) {
|
2017-06-26 20:38:16 +03:00
|
|
|
return;
|
|
|
|
}
|
2018-05-02 22:27:03 +03:00
|
|
|
auto result = _appReply->readAll();
|
|
|
|
_appReply.release()->deleteLater();
|
2017-06-26 20:38:16 +03:00
|
|
|
handleResponse(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpecialConfigRequest::dnsFinished() {
|
|
|
|
if (!_dnsReply) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-04 11:33:33 +04:00
|
|
|
if (_dnsReply->error() != QNetworkReply::NoError) {
|
|
|
|
LOG(("Config Error: Failed to get dns response JSON, error: %1 (%2)").arg(_dnsReply->errorString()).arg(_dnsReply->error()));
|
|
|
|
}
|
2017-06-26 20:38:16 +03:00
|
|
|
auto result = _dnsReply->readAll();
|
|
|
|
_dnsReply.release()->deleteLater();
|
|
|
|
|
|
|
|
// Read and store to "entries" map all the data bytes from this response:
|
|
|
|
// { .., "Answer": [ { .., "data": "bytes1", .. }, { .., "data": "bytes2", .. } ], .. }
|
|
|
|
auto entries = QMap<int, QString>();
|
|
|
|
auto error = QJsonParseError { 0, QJsonParseError::NoError };
|
|
|
|
auto document = QJsonDocument::fromJson(result, &error);
|
|
|
|
if (error.error != QJsonParseError::NoError) {
|
2017-07-07 00:44:37 +03:00
|
|
|
LOG(("Config Error: Failed to parse dns response JSON, error: %1").arg(error.errorString()));
|
2017-06-26 20:38:16 +03:00
|
|
|
} else if (!document.isObject()) {
|
|
|
|
LOG(("Config Error: Not an object received in dns response JSON."));
|
|
|
|
} else {
|
|
|
|
auto response = document.object();
|
|
|
|
auto answerIt = response.find(qsl("Answer"));
|
|
|
|
if (answerIt == response.constEnd()) {
|
|
|
|
LOG(("Config Error: Could not find Answer in dns response JSON."));
|
2017-06-28 10:31:39 +03:00
|
|
|
} else if (!(*answerIt).isArray()) {
|
2017-06-26 20:38:16 +03:00
|
|
|
LOG(("Config Error: Not an array received in Answer in dns response JSON."));
|
|
|
|
} else {
|
2017-06-28 10:31:39 +03:00
|
|
|
for (auto elem : (*answerIt).toArray()) {
|
2017-06-26 20:38:16 +03:00
|
|
|
if (!elem.isObject()) {
|
|
|
|
LOG(("Config Error: Not an object found in Answer array in dns response JSON."));
|
|
|
|
} else {
|
|
|
|
auto object = elem.toObject();
|
|
|
|
auto dataIt = object.find(qsl("data"));
|
|
|
|
if (dataIt == object.constEnd()) {
|
|
|
|
LOG(("Config Error: Could not find data in Answer array entry in dns response JSON."));
|
2017-06-28 10:31:39 +03:00
|
|
|
} else if (!(*dataIt).isString()) {
|
2017-06-26 20:38:16 +03:00
|
|
|
LOG(("Config Error: Not a string data found in Answer array entry in dns response JSON."));
|
|
|
|
} else {
|
2017-06-28 10:31:39 +03:00
|
|
|
auto data = (*dataIt).toString();
|
2017-06-26 20:38:16 +03:00
|
|
|
entries.insertMulti(INT_MAX - data.size(), data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto text = QStringList(entries.values()).join(QString());
|
|
|
|
handleResponse(text.toLatin1());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SpecialConfigRequest::decryptSimpleConfig(const QByteArray &bytes) {
|
|
|
|
auto cleanBytes = bytes;
|
|
|
|
auto removeFrom = std::remove_if(cleanBytes.begin(), cleanBytes.end(), [](char ch) {
|
|
|
|
auto isGoodBase64 = (ch == '+') || (ch == '=') || (ch == '/')
|
|
|
|
|| (ch >= 'a' && ch <= 'z')
|
|
|
|
|| (ch >= 'A' && ch <= 'Z')
|
|
|
|
|| (ch >= '0' && ch <= '9');
|
|
|
|
return !isGoodBase64;
|
|
|
|
});
|
|
|
|
if (removeFrom != cleanBytes.end()) {
|
|
|
|
cleanBytes.remove(removeFrom - cleanBytes.begin(), cleanBytes.end() - removeFrom);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr auto kGoodSizeBase64 = 344;
|
|
|
|
if (cleanBytes.size() != kGoodSizeBase64) {
|
|
|
|
LOG(("Config Error: Bad data size %1 required %2").arg(cleanBytes.size()).arg(kGoodSizeBase64));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
constexpr auto kGoodSizeData = 256;
|
|
|
|
auto decodedBytes = QByteArray::fromBase64(cleanBytes, QByteArray::Base64Encoding);
|
|
|
|
if (decodedBytes.size() != kGoodSizeData) {
|
|
|
|
LOG(("Config Error: Bad data size %1 required %2").arg(decodedBytes.size()).arg(kGoodSizeData));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-08 19:24:09 +04:00
|
|
|
auto publicKey = internal::RSAPublicKey(gsl::as_bytes(gsl::make_span(
|
|
|
|
kPublicKey.c_str(),
|
|
|
|
kPublicKey.size())));
|
2017-06-26 20:38:16 +03:00
|
|
|
auto decrypted = publicKey.decrypt(gsl::as_bytes(gsl::make_span(decodedBytes)));
|
|
|
|
auto decryptedBytes = gsl::make_span(decrypted);
|
|
|
|
|
|
|
|
constexpr auto kAesKeySize = CTRState::KeySize;
|
|
|
|
constexpr auto kAesIvecSize = CTRState::IvecSize;
|
|
|
|
auto aesEncryptedBytes = decryptedBytes.subspan(kAesKeySize);
|
|
|
|
base::byte_array<kAesIvecSize> aesivec;
|
|
|
|
base::copy_bytes(aesivec, decryptedBytes.subspan(CTRState::KeySize - CTRState::IvecSize, CTRState::IvecSize));
|
|
|
|
AES_KEY aeskey;
|
|
|
|
AES_set_decrypt_key(reinterpret_cast<const unsigned char*>(decryptedBytes.data()), kAesKeySize * CHAR_BIT, &aeskey);
|
|
|
|
AES_cbc_encrypt(reinterpret_cast<const unsigned char*>(aesEncryptedBytes.data()), reinterpret_cast<unsigned char*>(aesEncryptedBytes.data()), aesEncryptedBytes.size(), &aeskey, reinterpret_cast<unsigned char*>(aesivec.data()), AES_DECRYPT);
|
|
|
|
|
|
|
|
constexpr auto kDigestSize = 16;
|
|
|
|
auto dataSize = aesEncryptedBytes.size() - kDigestSize;
|
|
|
|
auto data = aesEncryptedBytes.subspan(0, dataSize);
|
|
|
|
auto hash = openssl::Sha256(data);
|
|
|
|
if (base::compare_bytes(gsl::make_span(hash).subspan(0, kDigestSize), aesEncryptedBytes.subspan(dataSize)) != 0) {
|
|
|
|
LOG(("Config Error: Bad digest."));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mtpBuffer buffer;
|
|
|
|
buffer.resize(data.size() / sizeof(mtpPrime));
|
|
|
|
base::copy_bytes(gsl::as_writeable_bytes(gsl::make_span(buffer)), data);
|
|
|
|
auto from = &*buffer.cbegin();
|
|
|
|
auto end = from + buffer.size();
|
|
|
|
auto realLength = *from++;
|
|
|
|
if (realLength <= 0 || realLength > dataSize || (realLength & 0x03)) {
|
|
|
|
LOG(("Config Error: Bad length %1.").arg(realLength));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
_simpleConfig.read(from, end);
|
|
|
|
} catch (...) {
|
|
|
|
LOG(("Config Error: Could not read configSimple."));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((end - from) * sizeof(mtpPrime) != (dataSize - realLength)) {
|
|
|
|
LOG(("Config Error: Bad read length %1, should be %2.").arg((end - from) * sizeof(mtpPrime)).arg(dataSize - realLength));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpecialConfigRequest::handleResponse(const QByteArray &bytes) {
|
|
|
|
if (!decryptSimpleConfig(bytes)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-08-17 12:06:26 +03:00
|
|
|
Assert(_simpleConfig.type() == mtpc_help_configSimple);
|
2017-06-26 20:38:16 +03:00
|
|
|
auto &config = _simpleConfig.c_help_configSimple();
|
|
|
|
auto now = unixtime();
|
|
|
|
if (now < config.vdate.v || now > config.vexpires.v) {
|
|
|
|
LOG(("Config Error: Bad date frame for simple config: %1-%2, our time is %3.").arg(config.vdate.v).arg(config.vexpires.v).arg(now));
|
|
|
|
return;
|
|
|
|
}
|
2018-05-02 22:27:03 +03:00
|
|
|
if (config.vrules.v.empty()) {
|
2017-06-26 20:38:16 +03:00
|
|
|
LOG(("Config Error: Empty simple config received."));
|
|
|
|
return;
|
|
|
|
}
|
2018-05-02 22:27:03 +03:00
|
|
|
for (auto &rule : config.vrules.v) {
|
|
|
|
Assert(rule.type() == mtpc_accessPointRule);
|
|
|
|
auto &data = rule.c_accessPointRule();
|
|
|
|
const auto phoneRules = qs(data.vphone_prefix_rules);
|
|
|
|
if (!CheckPhoneByPrefixesRules(_phone, phoneRules)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto dcId = data.vdc_id.v;
|
|
|
|
for (const auto &address : data.vips.v) {
|
|
|
|
const auto parseIp = [](const MTPint &ipv4) {
|
|
|
|
const auto ip = *reinterpret_cast<const uint32*>(&ipv4.v);
|
|
|
|
return qsl("%1.%2.%3.%4"
|
|
|
|
).arg((ip >> 24) & 0xFF
|
|
|
|
).arg((ip >> 16) & 0xFF
|
|
|
|
).arg((ip >> 8) & 0xFF
|
|
|
|
).arg(ip & 0xFF).toStdString();
|
|
|
|
};
|
|
|
|
switch (address.type()) {
|
|
|
|
case mtpc_ipPort: {
|
|
|
|
const auto &fields = address.c_ipPort();
|
|
|
|
_callback(dcId, parseIp(fields.vipv4), fields.vport.v, {});
|
|
|
|
} break;
|
|
|
|
case mtpc_ipPortSecret: {
|
|
|
|
const auto &fields = address.c_ipPortSecret();
|
|
|
|
_callback(
|
|
|
|
dcId,
|
|
|
|
parseIp(fields.vipv4),
|
|
|
|
fields.vport.v,
|
|
|
|
bytes::make_span(fields.vsecret.v));
|
|
|
|
} break;
|
|
|
|
default: Unexpected("Type in simpleConfig ips.");
|
|
|
|
}
|
|
|
|
}
|
2017-06-26 20:38:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SpecialConfigRequest::~SpecialConfigRequest() {
|
2018-05-02 22:27:03 +03:00
|
|
|
if (_appReply) {
|
|
|
|
_appReply->abort();
|
2017-06-26 20:38:16 +03:00
|
|
|
}
|
|
|
|
if (_dnsReply) {
|
|
|
|
_dnsReply->abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace MTP
|