From 5eba65aaa0c584056ada5b3182dcc241cd81e0d1 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 24 Jun 2021 09:55:57 +0400 Subject: [PATCH] Remove unused legacy protocol code. --- .../details/mtproto_serialized_request.cpp | 14 +++-- .../details/mtproto_serialized_request.h | 2 +- .../SourceFiles/mtproto/session_private.cpp | 63 +++---------------- 3 files changed, 19 insertions(+), 60 deletions(-) diff --git a/Telegram/SourceFiles/mtproto/details/mtproto_serialized_request.cpp b/Telegram/SourceFiles/mtproto/details/mtproto_serialized_request.cpp index 5b04acafbb..838bf78da3 100644 --- a/Telegram/SourceFiles/mtproto/details/mtproto_serialized_request.cpp +++ b/Telegram/SourceFiles/mtproto/details/mtproto_serialized_request.cpp @@ -12,8 +12,11 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL namespace MTP::details { namespace { -uint32 CountPaddingPrimesCount(uint32 requestSize, bool extended, bool old) { - if (old) { +uint32 CountPaddingPrimesCount( + uint32 requestSize, + bool extended, + bool forAuthKeyInner) { + if (forAuthKeyInner) { return ((8 + requestSize) & 0x03) ? (4 - ((8 + requestSize) & 0x03)) : 0; @@ -100,12 +103,15 @@ uint32 SerializedRequest::getSeqNo() const { return uint32((*_data)[kSeqNoPosition]); } -void SerializedRequest::addPadding(bool extended, bool old) { +void SerializedRequest::addPadding(bool extended, bool forAuthKeyInner) { Expects(_data != nullptr); Expects(_data->size() > kMessageBodyPosition); const auto requestSize = (tl::count_length(*this) >> 2); - const auto padding = CountPaddingPrimesCount(requestSize, extended, old); + const auto padding = CountPaddingPrimesCount( + requestSize, + extended, + forAuthKeyInner); const auto fullSize = kMessageBodyPosition + requestSize + padding; if (uint32(_data->size()) != fullSize) { _data->resize(fullSize); diff --git a/Telegram/SourceFiles/mtproto/details/mtproto_serialized_request.h b/Telegram/SourceFiles/mtproto/details/mtproto_serialized_request.h index ad9901b2bb..a054a4f2c4 100644 --- a/Telegram/SourceFiles/mtproto/details/mtproto_serialized_request.h +++ b/Telegram/SourceFiles/mtproto/details/mtproto_serialized_request.h @@ -65,7 +65,7 @@ public: void setSeqNo(uint32 seqNo); [[nodiscard]] uint32 getSeqNo() const; - void addPadding(bool extended, bool old); + void addPadding(bool extended, bool forAuthKeyInner); [[nodiscard]] uint32 messageSize() const; [[nodiscard]] bool needAck() const; diff --git a/Telegram/SourceFiles/mtproto/session_private.cpp b/Telegram/SourceFiles/mtproto/session_private.cpp index bd71d26b3f..bdb0252bbb 100644 --- a/Telegram/SourceFiles/mtproto/session_private.cpp +++ b/Telegram/SourceFiles/mtproto/session_private.cpp @@ -1263,17 +1263,16 @@ void SessionPrivate::handleReceived() { return restart(); } + constexpr auto kMinPaddingSize = 12U; + constexpr auto kMaxPaddingSize = 1024U; + auto encryptedInts = ints + kExternalHeaderIntsCount; auto encryptedIntsCount = (intsCount - kExternalHeaderIntsCount) & ~0x03U; auto encryptedBytesCount = encryptedIntsCount * kIntSize; auto decryptedBuffer = QByteArray(encryptedBytesCount, Qt::Uninitialized); auto msgKey = *(MTPint128*)(ints + 2); -#ifdef TDESKTOP_MTPROTO_OLD - aesIgeDecrypt_oldmtp(encryptedInts, decryptedBuffer.data(), encryptedBytesCount, _encryptionKey, msgKey); -#else // TDESKTOP_MTPROTO_OLD aesIgeDecrypt(encryptedInts, decryptedBuffer.data(), encryptedBytesCount, _encryptionKey, msgKey); -#endif // TDESKTOP_MTPROTO_OLD auto decryptedInts = reinterpret_cast(decryptedBuffer.constData()); auto serverSalt = *(uint64*)&decryptedInts[0]; @@ -1283,31 +1282,10 @@ void SessionPrivate::handleReceived() { auto needAck = ((seqNo & 0x01) != 0); auto messageLength = *(uint32*)&decryptedInts[7]; auto fullDataLength = kEncryptedHeaderIntsCount * kIntSize + messageLength; // Without padding. - auto badMessageLength = (messageLength > kMaxMessageLength); // Can underflow, but it is an unsigned type, so we just check the range later. auto paddingSize = static_cast(encryptedBytesCount) - static_cast(fullDataLength); -#ifdef TDESKTOP_MTPROTO_OLD - constexpr auto kMinPaddingSize_oldmtp = 0U; - constexpr auto kMaxPaddingSize_oldmtp = 15U; - badMessageLength |= (/*paddingSize < kMinPaddingSize_oldmtp || */paddingSize > kMaxPaddingSize_oldmtp); - - auto hashedDataLength = badMessageLength ? encryptedBytesCount : fullDataLength; - auto sha1ForMsgKeyCheck = hashSha1(decryptedInts, hashedDataLength); - - constexpr auto kMsgKeyShift_oldmtp = 4U; - if (ConstTimeIsDifferent(&msgKey, sha1ForMsgKeyCheck.data() + kMsgKeyShift_oldmtp, sizeof(msgKey))) { - LOG(("TCP Error: bad SHA1 hash after aesDecrypt in message.")); - TCP_LOG(("TCP Error: bad message %1").arg(Logs::mb(encryptedInts, encryptedBytesCount).str())); - - return restart(); - } -#else // TDESKTOP_MTPROTO_OLD - constexpr auto kMinPaddingSize = 12U; - constexpr auto kMaxPaddingSize = 1024U; - badMessageLength |= (paddingSize < kMinPaddingSize || paddingSize > kMaxPaddingSize); - std::array sha256Buffer = { { 0 } }; SHA256_CTX msgKeyLargeContext; @@ -1323,9 +1301,11 @@ void SessionPrivate::handleReceived() { return restart(); } -#endif // TDESKTOP_MTPROTO_OLD - if (badMessageLength || (messageLength & 0x03)) { + if ((messageLength > kMaxMessageLength) + || (messageLength & 0x03) + || (paddingSize < kMinPaddingSize) + || (paddingSize > kMaxPaddingSize)) { LOG(("TCP Error: bad msg_len received %1, data size: %2").arg(messageLength).arg(encryptedBytesCount)); TCP_LOG(("TCP Error: bad message %1").arg(Logs::mb(encryptedInts, encryptedBytesCount).str())); @@ -2616,12 +2596,7 @@ void SessionPrivate::destroyTemporaryKey() { bool SessionPrivate::sendSecureRequest( SerializedRequest &&request, bool needAnyResponse) { -#ifdef TDESKTOP_MTPROTO_OLD - const auto oldPadding = true; -#else // TDESKTOP_MTPROTO_OLD - const auto oldPadding = false; -#endif // TDESKTOP_MTPROTO_OLD - request.addPadding(_connection->requiresExtendedPadding(), oldPadding); + request.addPadding(_connection->requiresExtendedPadding(), false); uint32 fullSize = request->size(); if (fullSize < 9) { @@ -2643,27 +2618,6 @@ bool SessionPrivate::sendSecureRequest( ).arg(getProtocolDcId() ).arg(_encryptionKey->keyId())); -#ifdef TDESKTOP_MTPROTO_OLD - uint32 padding = fullSize - 4 - messageSize; - - uchar encryptedSHA[20]; - MTPint128 &msgKey(*(MTPint128*)(encryptedSHA + 4)); - hashSha1( - request->constData(), - (fullSize - padding) * sizeof(mtpPrime), - encryptedSHA); - - auto packet = _connection->prepareSecurePacket(_keyId, msgKey, fullSize); - const auto prefix = packet.size(); - packet.resize(prefix + fullSize); - - aesIgeEncrypt_oldmtp( - request->constData(), - &packet[prefix], - fullSize * sizeof(mtpPrime), - _encryptionKey, - msgKey); -#else // TDESKTOP_MTPROTO_OLD uchar encryptedSHA256[32]; MTPint128 &msgKey(*(MTPint128*)(encryptedSHA256 + 8)); @@ -2683,7 +2637,6 @@ bool SessionPrivate::sendSecureRequest( fullSize * sizeof(mtpPrime), _encryptionKey, msgKey); -#endif // TDESKTOP_MTPROTO_OLD DEBUG_LOG(("MTP Info: sending request, size: %1, num: %2, time: %3").arg(fullSize + 6).arg((*request)[4]).arg((*request)[5]));