Allow creating temporary keys.

This commit is contained in:
John Preston 2019-11-14 10:13:17 +03:00
parent 08bfe6f1c1
commit 1524b4a930
8 changed files with 381 additions and 345 deletions

View File

@ -21,6 +21,8 @@ p_q_inner_data_dc#a9f55f95 pq:string p:string q:string nonce:int128 server_nonce
p_q_inner_data_temp#3c6a84d4 pq:string p:string q:string nonce:int128 server_nonce:int128 new_nonce:int256 expires_in:int = P_Q_inner_data;
p_q_inner_data_temp_dc#56fddf88 pq:string p:string q:string nonce:int128 server_nonce:int128 new_nonce:int256 dc:int expires_in:int = P_Q_inner_data;
bind_auth_key_inner#75a3f765 nonce:long temp_auth_key_id:long perm_auth_key_id:long temp_session_id:long expires_at:int = BindAuthKeyInner;
server_DH_params_fail#79cb045d nonce:int128 server_nonce:int128 new_nonce_hash:int128 = Server_DH_Params;
server_DH_params_ok#d0e8075c nonce:int128 server_nonce:int128 encrypted_answer:string = Server_DH_Params;

File diff suppressed because it is too large Load Diff

View File

@ -17,6 +17,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
namespace MTP {
namespace details {
class DcKeyCreator;
class DcKeyChecker;
} // namespace details
// How much time to wait for some more requests, when sending msg acks.
@ -240,9 +241,10 @@ private:
base::Timer _waitForBetterTimer;
crl::time _waitForReceived = 0;
crl::time _waitForConnected = 0;
crl::time firstSentAt = -1;
crl::time _firstSentAt = -1;
QVector<MTPlong> ackRequestData, resendRequestData;
QVector<MTPlong> _ackRequestData;
QVector<MTPlong> _resendRequestData;
mtpPingId _pingId = 0;
mtpPingId _pingIdToSend = 0;
@ -250,15 +252,15 @@ private:
mtpMsgId _pingMsgId = 0;
base::Timer _pingSender;
bool restarted = false;
bool _restarted = false;
bool _finished = false;
uint64 keyId = 0;
QReadWriteLock sessionDataMutex;
SessionData *sessionData = nullptr;
uint64 _keyId = 0;
QReadWriteLock _sessionDataMutex;
SessionData *_sessionData = nullptr;
std::unique_ptr<ConnectionOptions> _connectionOptions;
bool myKeyLock = false;
bool _myKeyLock = false;
std::unique_ptr<details::DcKeyCreator> _keyCreator;

View File

@ -42,6 +42,7 @@ constexpr auto kLogoutDcShift = 0x02;
constexpr auto kUpdaterDcShift = 0x03;
constexpr auto kExportDcShift = 0x04;
constexpr auto kExportMediaDcShift = 0x05;
constexpr auto kCheckKeyDcShift = 0x06;
constexpr auto kMaxMediaDcCount = 0x10;
constexpr auto kBaseDownloadDcShift = 0x10;
constexpr auto kBaseUploadDcShift = 0x20;

View File

@ -69,8 +69,9 @@ struct ParsedPQ {
return { pStr, qStr };
}
template <typename PQInnerData>
[[nodiscard]] bytes::vector EncryptPQInnerRSA(
const MTPP_Q_inner_data &data,
const PQInnerData &data,
const RSAPublicKey &key) {
constexpr auto kSkipPrimes = 6;
constexpr auto kMaxPrimes = 65; // 260 bytes
@ -150,12 +151,15 @@ DcKeyCreator::DcKeyCreator(
int16 protocolDcId,
not_null<AbstractConnection*> connection,
not_null<DcOptions*> dcOptions,
Delegate delegate)
Delegate delegate,
TimeId expireIn)
: _connection(connection)
, _dcOptions(dcOptions)
, _dcId(dcId)
, _protocolDcId(protocolDcId)
, _expireIn(expireIn)
, _delegate(std::move(delegate)) {
Expects(_expireIn >= 0);
Expects(_delegate.done != nullptr);
_data.nonce = openssl::RandomValue<MTPint128>();
@ -219,15 +223,30 @@ void DcKeyCreator::pqAnswered() {
return failed();
}
auto p_q_inner = MTP_p_q_inner_data_dc(
res_pq_data.vpq(),
MTP_bytes(parsed.p),
MTP_bytes(parsed.q),
_data.nonce,
_data.server_nonce,
_data.new_nonce,
MTP_int(_protocolDcId));
const auto dhEncString = EncryptPQInnerRSA(p_q_inner, rsaKey);
const auto dhEncString = [&] {
return (_expireIn == 0)
? EncryptPQInnerRSA(
MTP_p_q_inner_data_dc(
res_pq_data.vpq(),
MTP_bytes(parsed.p),
MTP_bytes(parsed.q),
_data.nonce,
_data.server_nonce,
_data.new_nonce,
MTP_int(_protocolDcId)),
rsaKey)
: EncryptPQInnerRSA(
MTP_p_q_inner_data_temp_dc(
res_pq_data.vpq(),
MTP_bytes(parsed.p),
MTP_bytes(parsed.q),
_data.nonce,
_data.server_nonce,
_data.new_nonce,
MTP_int(_protocolDcId),
MTP_int(_expireIn)),
rsaKey);
}();
if (dhEncString.empty()) {
return failed();
}
@ -241,8 +260,8 @@ void DcKeyCreator::pqAnswered() {
sendNotSecureRequest(MTPReq_DH_params(
_data.nonce,
_data.server_nonce,
p_q_inner.c_p_q_inner_data_dc().vp(),
p_q_inner.c_p_q_inner_data_dc().vq(),
MTP_bytes(parsed.p),
MTP_bytes(parsed.q),
MTP_long(rsaKey.fingerprint()),
MTP_bytes(dhEncString)));
}

View File

@ -42,7 +42,8 @@ public:
int16 protocolDcId,
not_null<AbstractConnection*> connection,
not_null<DcOptions*> dcOptions,
Delegate delegate);
Delegate delegate,
TimeId expireIn = 0); // 0 - persistent, > 0 - temporary
~DcKeyCreator();
private:
@ -87,8 +88,9 @@ private:
const not_null<AbstractConnection*> _connection;
const not_null<DcOptions*> _dcOptions;
const DcId _dcId;
const DcId _dcId = 0;
const int16 _protocolDcId = 0;
const TimeId _expireIn = 0;
Delegate _delegate;
Data _data;

View File

@ -64,13 +64,13 @@ ConnectionOptions::ConnectionOptions(
void SessionData::setKey(const AuthKeyPtr &key) {
if (_authKey != key) {
uint64 session = rand_value<uint64>();
const auto sessionId = rand_value<uint64>();
_authKey = key;
DEBUG_LOG(("MTP Info: new auth key set in SessionData, id %1, setting random server_session %2").arg(key ? key->keyId() : 0).arg(session));
DEBUG_LOG(("MTP Info: new auth key set in SessionData, id %1, setting random server_session %2").arg(key ? key->keyId() : 0).arg(sessionId));
QWriteLocker locker(&_lock);
if (_session != session) {
_session = session;
if (_sessionId != sessionId) {
_sessionId = sessionId;
_messagesSent = 0;
}
_layerInited = false;
@ -135,13 +135,14 @@ void SessionData::clear(Instance *instance) {
instance->clearCallbacksDelayed(std::move(clearCallbacks));
}
Session::Session(not_null<Instance*> instance, ShiftedDcId shiftedDcId) : QObject()
Session::Session(not_null<Instance*> instance, ShiftedDcId shiftedDcId)
: QObject()
, _instance(instance)
, data(this)
, dcWithShift(shiftedDcId)
, sender([=] { needToResumeAndSend(); }) {
connect(&timeouter, SIGNAL(timeout()), this, SLOT(checkRequestsByTimer()));
timeouter.start(1000);
, _data(this)
, _shiftedDcId(shiftedDcId)
, _sender([=] { needToResumeAndSend(); }) {
connect(&_timeouter, SIGNAL(timeout()), this, SLOT(checkRequestsByTimer()));
_timeouter.start(1000);
refreshOptions();
}
@ -149,26 +150,26 @@ Session::Session(not_null<Instance*> instance, ShiftedDcId shiftedDcId) : QObjec
void Session::start() {
createDcData();
_connection = std::make_unique<Connection>(_instance);
_connection->start(&data, dcWithShift);
_connection->start(&_data, _shiftedDcId);
if (_instance->isKeysDestroyer()) {
_instance->scheduleKeyDestroy(dcWithShift);
_instance->scheduleKeyDestroy(_shiftedDcId);
}
}
void Session::createDcData() {
if (dc) {
if (_dc) {
return;
}
dc = _instance->getDcById(dcWithShift);
_dc = _instance->getDcById(_shiftedDcId);
if (auto lock = ReadLockerAttempt(keyMutex())) {
data.setKey(dc->getKey());
if (dc->connectionInited()) {
data.setConnectionInited();
_data.setKey(_dc->getKey());
if (_dc->connectionInited()) {
_data.setConnectionInited();
}
}
connect(dc.get(), SIGNAL(authKeyCreated()), this, SLOT(authKeyCreatedForDC()), Qt::QueuedConnection);
connect(dc.get(), SIGNAL(connectionWasInited()), this, SLOT(connectionWasInitedForDC()), Qt::QueuedConnection);
connect(_dc.get(), SIGNAL(authKeyCreated()), this, SLOT(authKeyCreatedForDC()), Qt::QueuedConnection);
connect(_dc.get(), SIGNAL(connectionWasInited()), this, SLOT(connectionWasInitedForDC()), Qt::QueuedConnection);
}
bool Session::rpcErrorOccured(
@ -197,7 +198,7 @@ void Session::refreshOptions() {
const auto useHttp = (proxyType != ProxyData::Type::Mtproto);
const auto useIPv4 = true;
const auto useIPv6 = Global::TryIPv6();
data.applyConnectionOptions(ConnectionOptions(
_data.applyConnectionOptions(ConnectionOptions(
_instance->systemLangCode(),
_instance->cloudLangCode(),
_instance->langPackName(),
@ -211,8 +212,8 @@ void Session::refreshOptions() {
}
void Session::reInitConnection() {
dc->setConnectionInited(false);
data.setConnectionInited(false);
_dc->setConnectionInited(false);
_data.setConnectionInited(false);
restart();
}
@ -221,7 +222,7 @@ void Session::stop() {
DEBUG_LOG(("Session Error: can't kill a killed session"));
return;
}
DEBUG_LOG(("Session Info: stopping session dcWithShift %1").arg(dcWithShift));
DEBUG_LOG(("Session Info: stopping session dcWithShift %1").arg(_shiftedDcId));
if (_connection) {
_connection->kill();
_instance->queueQuittingConnection(std::move(_connection));
@ -231,7 +232,7 @@ void Session::stop() {
void Session::kill() {
stop();
_killed = true;
DEBUG_LOG(("Session Info: marked session dcWithShift %1 as killed").arg(dcWithShift));
DEBUG_LOG(("Session Info: marked session dcWithShift %1 as killed").arg(_shiftedDcId));
}
void Session::unpaused() {
@ -246,27 +247,27 @@ void Session::sendAnything(qint64 msCanWait) {
DEBUG_LOG(("Session Error: can't send anything in a killed session"));
return;
}
auto ms = crl::now();
if (msSendCall) {
if (ms > msSendCall + msWait) {
msWait = 0;
const auto ms = crl::now();
if (_msSendCall) {
if (ms > _msSendCall + _msWait) {
_msWait = 0;
} else {
msWait = (msSendCall + msWait) - ms;
if (msWait > msCanWait) {
msWait = msCanWait;
_msWait = (_msSendCall + _msWait) - ms;
if (_msWait > msCanWait) {
_msWait = msCanWait;
}
}
} else {
msWait = msCanWait;
_msWait = msCanWait;
}
if (msWait) {
DEBUG_LOG(("MTP Info: dcWithShift %1 can wait for %2ms from current %3").arg(dcWithShift).arg(msWait).arg(msSendCall));
msSendCall = ms;
sender.callOnce(msWait);
if (_msWait) {
DEBUG_LOG(("MTP Info: dcWithShift %1 can wait for %2ms from current %3").arg(_shiftedDcId).arg(_msWait).arg(_msSendCall));
_msSendCall = ms;
_sender.callOnce(_msWait);
} else {
DEBUG_LOG(("MTP Info: dcWithShift %1 stopped send timer, can wait for %2ms from current %3").arg(dcWithShift).arg(msWait).arg(msSendCall));
sender.cancel();
msSendCall = 0;
DEBUG_LOG(("MTP Info: dcWithShift %1 stopped send timer, can wait for %2ms from current %3").arg(_shiftedDcId).arg(_msWait).arg(_msSendCall));
_sender.cancel();
_msSendCall = 0;
needToResumeAndSend();
}
}
@ -277,10 +278,10 @@ void Session::needToResumeAndSend() {
return;
}
if (!_connection) {
DEBUG_LOG(("Session Info: resuming session dcWithShift %1").arg(dcWithShift));
DEBUG_LOG(("Session Info: resuming session dcWithShift %1").arg(_shiftedDcId));
createDcData();
_connection = std::make_unique<Connection>(_instance);
_connection->start(&data, dcWithShift);
_connection->start(&_data, _shiftedDcId);
}
if (_ping) {
_ping = false;
@ -292,7 +293,7 @@ void Session::needToResumeAndSend() {
void Session::sendPong(quint64 msgId, quint64 pingId) {
_instance->sendProtocolMessage(
dcWithShift,
_shiftedDcId,
MTPPong(MTP_pong(MTP_long(msgId), MTP_long(pingId))));
}
@ -303,7 +304,7 @@ void Session::sendMsgsStateInfo(quint64 msgId, QByteArray data) {
bytes::copy(info, bytes::make_span(data));
}
_instance->sendProtocolMessage(
dcWithShift,
_shiftedDcId,
MTPMsgsStateInfo(
MTP_msgs_state_info(MTP_long(msgId), MTP_bytes(data))));
}
@ -314,8 +315,8 @@ void Session::checkRequestsByTimer() {
QVector<mtpMsgId> stateRequestIds;
{
QReadLocker locker(data.haveSentMutex());
auto &haveSent = data.haveSentMap();
QReadLocker locker(_data.haveSentMutex());
auto &haveSent = _data.haveSentMap();
const auto haveSentCount = haveSent.size();
auto ms = crl::now();
for (auto i = haveSent.begin(), e = haveSent.end(); i != e; ++i) {
@ -342,9 +343,9 @@ void Session::checkRequestsByTimer() {
if (stateRequestIds.size()) {
DEBUG_LOG(("MTP Info: requesting state of msgs: %1").arg(LogIds(stateRequestIds)));
{
QWriteLocker locker(data.stateRequestMutex());
QWriteLocker locker(_data.stateRequestMutex());
for (uint32 i = 0, l = stateRequestIds.size(); i < l; ++i) {
data.stateRequestMap().insert(stateRequestIds[i], true);
_data.stateRequestMap().insert(stateRequestIds[i], true);
}
}
sendAnything(kCheckResendWaiting);
@ -358,8 +359,8 @@ void Session::checkRequestsByTimer() {
if (!removingIds.isEmpty()) {
auto clearCallbacks = std::vector<RPCCallbackClear>();
{
QWriteLocker locker(data.haveSentMutex());
auto &haveSent = data.haveSentMap();
QWriteLocker locker(_data.haveSentMutex());
auto &haveSent = _data.haveSentMap();
for (uint32 i = 0, l = removingIds.size(); i < l; ++i) {
auto j = haveSent.find(removingIds[i]);
if (j != haveSent.cend()) {
@ -375,21 +376,21 @@ void Session::checkRequestsByTimer() {
}
void Session::onConnectionStateChange(qint32 newState) {
_instance->onStateChange(dcWithShift, newState);
_instance->onStateChange(_shiftedDcId, newState);
}
void Session::onResetDone() {
_instance->onSessionReset(dcWithShift);
_instance->onSessionReset(_shiftedDcId);
}
void Session::cancel(mtpRequestId requestId, mtpMsgId msgId) {
if (requestId) {
QWriteLocker locker(data.toSendMutex());
data.toSendMap().remove(requestId);
QWriteLocker locker(_data.toSendMutex());
_data.toSendMap().remove(requestId);
}
if (msgId) {
QWriteLocker locker(data.haveSentMutex());
data.haveSentMap().remove(msgId);
QWriteLocker locker(_data.haveSentMutex());
_data.haveSentMap().remove(msgId);
}
}
@ -421,8 +422,8 @@ int32 Session::requestState(mtpRequestId requestId) const {
}
if (!requestId) return MTP::RequestSent;
QWriteLocker locker(data.toSendMutex());
const auto &toSend = data.toSendMap();
QWriteLocker locker(_data.toSendMutex());
const auto &toSend = _data.toSendMap();
const auto i = toSend.constFind(requestId);
if (i != toSend.cend()) {
return MTP::RequestSending;
@ -461,8 +462,8 @@ QString Session::transport() const {
mtpRequestId Session::resend(quint64 msgId, qint64 msCanWait, bool forceContainer, bool sendMsgStateInfo) {
SecureRequest request;
{
QWriteLocker locker(data.haveSentMutex());
auto &haveSent = data.haveSentMap();
QWriteLocker locker(_data.haveSentMutex());
auto &haveSent = _data.haveSentMap();
auto i = haveSent.find(msgId);
if (i == haveSent.end()) {
@ -472,7 +473,7 @@ mtpRequestId Session::resend(quint64 msgId, qint64 msCanWait, bool forceContaine
auto info = std::string(cantResend, cantResend + 1);
return _instance->sendProtocolMessage(
dcWithShift,
_shiftedDcId,
MTPMsgsStateInfo(
MTP_msgs_state_info(
MTP_long(msgId),
@ -495,8 +496,8 @@ mtpRequestId Session::resend(quint64 msgId, qint64 msCanWait, bool forceContaine
request->msDate = forceContainer ? 0 : crl::now();
sendPrepared(request, msCanWait, false);
{
QWriteLocker locker(data.toResendMutex());
data.toResendMap().insert(msgId, request->requestId);
QWriteLocker locker(_data.toResendMutex());
_data.toResendMap().insert(msgId, request->requestId);
}
return request->requestId;
} else {
@ -513,8 +514,8 @@ void Session::resendMany(QVector<quint64> msgIds, qint64 msCanWait, bool forceCo
void Session::resendAll() {
QVector<mtpMsgId> toResend;
{
QReadLocker locker(data.haveSentMutex());
const auto &haveSent = data.haveSentMap();
QReadLocker locker(_data.haveSentMutex());
const auto &haveSent = _data.haveSentMap();
toResend.reserve(haveSent.size());
for (auto i = haveSent.cbegin(), e = haveSent.cend(); i != e; ++i) {
if (i.value()->requestId) {
@ -534,8 +535,8 @@ void Session::sendPrepared(
DEBUG_LOG(("MTP Info: adding request to toSendMap, msCanWait %1"
).arg(msCanWait));
{
QWriteLocker locker(data.toSendMutex());
data.toSendMap().insert(request->requestId, request);
QWriteLocker locker(_data.toSendMutex());
_data.toSendMap().insert(request->requestId, request);
if (newRequest) {
*(mtpMsgId*)(request->data() + 4) = 0;
@ -549,45 +550,47 @@ void Session::sendPrepared(
}
QReadWriteLock *Session::keyMutex() const {
return dc->keyMutex();
return _dc->keyMutex();
}
void Session::authKeyCreatedForDC() {
DEBUG_LOG(("AuthKey Info: Session::authKeyCreatedForDC slot, emitting authKeyCreated(), dcWithShift %1").arg(dcWithShift));
data.setKey(dc->getKey());
DEBUG_LOG(("AuthKey Info: Session::authKeyCreatedForDC slot, emitting authKeyCreated(), dcWithShift %1").arg(_shiftedDcId));
_data.setKey(_dc->getKey());
emit authKeyCreated();
}
void Session::notifyKeyCreated(AuthKeyPtr &&key) {
DEBUG_LOG(("AuthKey Info: Session::keyCreated(), setting, dcWithShift %1").arg(dcWithShift));
dc->setKey(std::move(key));
DEBUG_LOG(("AuthKey Info: Session::keyCreated(), setting, dcWithShift %1").arg(_shiftedDcId));
_dc->setKey(std::move(key));
}
void Session::connectionWasInitedForDC() {
DEBUG_LOG(("MTP Info: Session::connectionWasInitedForDC slot, dcWithShift %1").arg(dcWithShift));
data.setConnectionInited();
DEBUG_LOG(("MTP Info: Session::connectionWasInitedForDC slot, dcWithShift %1").arg(_shiftedDcId));
_data.setConnectionInited();
}
void Session::notifyDcConnectionInited() {
DEBUG_LOG(("MTP Info: emitting MTProtoDC::connectionWasInited(), dcWithShift %1").arg(dcWithShift));
dc->setConnectionInited();
emit dc->connectionWasInited();
DEBUG_LOG(("MTP Info: emitting MTProtoDC::connectionWasInited(), dcWithShift %1").arg(_shiftedDcId));
_dc->setConnectionInited();
emit _dc->connectionWasInited();
}
void Session::destroyKey() {
if (!dc) return;
if (!_dc) {
return;
}
if (data.getKey()) {
DEBUG_LOG(("MTP Info: destroying auth_key for dcWithShift %1").arg(dcWithShift));
if (data.getKey() == dc->getKey()) {
dc->destroyKey();
if (_data.getKey()) {
DEBUG_LOG(("MTP Info: destroying auth_key for dcWithShift %1").arg(_shiftedDcId));
if (_data.getKey() == _dc->getKey()) {
_dc->destroyKey();
}
data.setKey(AuthKeyPtr());
_data.setKey(AuthKeyPtr());
}
}
int32 Session::getDcWithShift() const {
return dcWithShift;
return _shiftedDcId;
}
void Session::tryToReceive() {
@ -604,11 +607,11 @@ void Session::tryToReceive() {
auto isUpdate = false;
auto message = SerializedMessage();
{
QWriteLocker locker(data.haveReceivedMutex());
auto &responses = data.haveReceivedResponses();
QWriteLocker locker(_data.haveReceivedMutex());
auto &responses = _data.haveReceivedResponses();
auto response = responses.begin();
if (response == responses.cend()) {
auto &updates = data.haveReceivedUpdates();
auto &updates = _data.haveReceivedUpdates();
auto update = updates.begin();
if (update == updates.cend()) {
return;
@ -624,7 +627,7 @@ void Session::tryToReceive() {
}
}
if (isUpdate) {
if (dcWithShift == BareDcId(dcWithShift)) { // call globalCallback only in main session
if (_shiftedDcId == BareDcId(_shiftedDcId)) { // call globalCallback only in main session
_instance->globalCallback(message.constData(), message.constData() + message.size());
}
} else {

View File

@ -141,18 +141,18 @@ public:
SessionData(not_null<Session*> creator) : _owner(creator) {
}
void setSession(uint64 session) {
DEBUG_LOG(("MTP Info: setting server_session: %1").arg(session));
void setSessionId(uint64 sessionId) {
DEBUG_LOG(("MTP Info: setting server_session: %1").arg(sessionId));
QWriteLocker locker(&_lock);
if (_session != session) {
_session = session;
if (_sessionId != sessionId) {
_sessionId = sessionId;
_messagesSent = 0;
}
}
uint64 getSession() const {
uint64 getSessionId() const {
QReadLocker locker(&_lock);
return _session;
return _sessionId;
}
void setConnectionInited(bool inited = true) {
QWriteLocker locker(&_lock);
@ -283,7 +283,7 @@ public:
void clear(Instance *instance);
private:
uint64 _session = 0;
uint64 _sessionId = 0;
uint64 _salt = 0;
uint32 _messagesSent = 0;
@ -389,18 +389,18 @@ private:
bool _killed = false;
bool _needToReceive = false;
SessionData data;
SessionData _data;
ShiftedDcId dcWithShift = 0;
std::shared_ptr<Dcenter> dc;
ShiftedDcId _shiftedDcId = 0;
std::shared_ptr<Dcenter> _dc;
crl::time msSendCall = 0;
crl::time msWait = 0;
crl::time _msSendCall = 0;
crl::time _msWait = 0;
bool _ping = false;
QTimer timeouter;
base::Timer sender;
QTimer _timeouter;
base::Timer _sender;
};