Fix key creation for media cluster.

This commit is contained in:
John Preston 2019-12-09 17:32:51 +03:00
parent 41e13e39bc
commit e60a9f0943
3 changed files with 29 additions and 16 deletions

View File

@ -104,9 +104,6 @@ void Dcenter::setConnectionInited(bool connectionInited) {
CreatingKeyType Dcenter::acquireKeyCreation(TemporaryKeyType type) { CreatingKeyType Dcenter::acquireKeyCreation(TemporaryKeyType type) {
QReadLocker lock(&_mutex); QReadLocker lock(&_mutex);
if (type == TemporaryKeyType::MediaCluster) {
int a = 0;
}
const auto index = IndexByType(type); const auto index = IndexByType(type);
auto &key = _temporaryKeys[index]; auto &key = _temporaryKeys[index];
if (key != nullptr) { if (key != nullptr) {

View File

@ -2245,15 +2245,8 @@ void SessionPrivate::updateAuthKey() {
DEBUG_LOG(("AuthKey Info: Connection updating key from Session, dc %1" DEBUG_LOG(("AuthKey Info: Connection updating key from Session, dc %1"
).arg(_shiftedDcId)); ).arg(_shiftedDcId));
const auto myKeyType = TemporaryKeyTypeByDcType(_currentDcType); applyAuthKey(_sessionData->getTemporaryKey(
applyAuthKey(_sessionData->getTemporaryKey(myKeyType)); TemporaryKeyTypeByDcType(_currentDcType)));
if (_connection
&& !_encryptionKey
&& myKeyType == TemporaryKeyType::MediaCluster
&& _sessionData->getTemporaryKey(TemporaryKeyType::Regular)) {
restart();
}
} }
void SessionPrivate::setCurrentKeyId(uint64 newKeyId) { void SessionPrivate::setCurrentKeyId(uint64 newKeyId) {
@ -2287,16 +2280,31 @@ void SessionPrivate::applyAuthKey(AuthKeyPtr &&encryptionKey) {
setCurrentKeyId(newKeyId); setCurrentKeyId(newKeyId);
Assert(!_connection->sentEncryptedWithKeyId()); Assert(!_connection->sentEncryptedWithKeyId());
DEBUG_LOG(("AuthKey Info: Connection update key from Session, dc %1 result: %2").arg(_shiftedDcId).arg(Logs::mb(&_keyId, sizeof(_keyId)).str())); DEBUG_LOG(("AuthKey Info: Connection update key from Session, "
"dc %1 result: %2"
).arg(_shiftedDcId
).arg(Logs::mb(&_keyId, sizeof(_keyId)).str()));
if (_keyId) { if (_keyId) {
return authKeyChecked(); return authKeyChecked();
} }
if (_instance->isKeysDestroyer()) { if (_instance->isKeysDestroyer()) {
// We are here to destroy an old key, so we're done. // We are here to destroy an old key, so we're done.
LOG(("MTP Error: No key %1 in updateAuthKey() for destroying.").arg(_shiftedDcId)); LOG(("MTP Error: No key %1 in updateAuthKey() for destroying."
).arg(_shiftedDcId));
_instance->keyWasPossiblyDestroyed(_shiftedDcId); _instance->keyWasPossiblyDestroyed(_shiftedDcId);
} else if (_keyCreator) { } else if (noMediaKeyWithExistingRegularKey()) {
DEBUG_LOG(("AuthKey Info: No key in updateAuthKey() for media, "
"but someone has created regular, trying to acquire."));
const auto dcType = tryAcquireKeyCreation();
if (_keyCreator && dcType != _currentDcType) {
DEBUG_LOG(("AuthKey Info: "
"Dc type changed for creation, restarting."));
restart();
return;
}
}
if (_keyCreator) {
DEBUG_LOG(("AuthKey Info: No key in updateAuthKey(), creating.")); DEBUG_LOG(("AuthKey Info: No key in updateAuthKey(), creating."));
_keyCreator->start( _keyCreator->start(
BareDcId(_shiftedDcId), BareDcId(_shiftedDcId),
@ -2304,10 +2312,17 @@ void SessionPrivate::applyAuthKey(AuthKeyPtr &&encryptionKey) {
_connection.get(), _connection.get(),
_instance->dcOptions()); _instance->dcOptions());
} else { } else {
DEBUG_LOG(("AuthKey Info: No key in updateAuthKey(), but someone is creating already.")); DEBUG_LOG(("AuthKey Info: No key in updateAuthKey(), "
"but someone is creating already, waiting."));
} }
} }
bool SessionPrivate::noMediaKeyWithExistingRegularKey() const {
return (TemporaryKeyTypeByDcType(_currentDcType)
== TemporaryKeyType::MediaCluster)
&& _sessionData->getTemporaryKey(TemporaryKeyType::Regular);
}
bool SessionPrivate::destroyOldEnoughPersistentKey() { bool SessionPrivate::destroyOldEnoughPersistentKey() {
Expects(_keyCreator != nullptr); Expects(_keyCreator != nullptr);

View File

@ -157,6 +157,7 @@ private:
void clearUnboundKeyCreator(); void clearUnboundKeyCreator();
void releaseKeyCreationOnFail(); void releaseKeyCreationOnFail();
void applyAuthKey(AuthKeyPtr &&encryptionKey); void applyAuthKey(AuthKeyPtr &&encryptionKey);
[[nodiscard]] bool noMediaKeyWithExistingRegularKey() const;
bool destroyOldEnoughPersistentKey(); bool destroyOldEnoughPersistentKey();
void setCurrentKeyId(uint64 newKeyId); void setCurrentKeyId(uint64 newKeyId);