void Session::queryOnResult(InboundPkt &inboundPkt, qint64 msgId) { qCDebug(TG_CORE_SESSION) << "result for query" << QString::number(msgId, 16); qint32 op = inboundPkt.prefetchInt(); qint32 *inPtr = 0; qint32 *inEnd = 0; if (op == (qint32)TL_GZipPacked) { inboundPkt.fetchInt(); qint32 l = inboundPkt.prefetchStrlen(); char *s = inboundPkt.fetchStr(l); static qint32 packedBuffer[MAX_PACKED_SIZE / 4]; qint32 totalOut = Utils::tinflate (s, l, packedBuffer, MAX_PACKED_SIZE); inPtr = inboundPkt.inPtr(); inEnd = inboundPkt.inEnd(); inboundPkt.setInPtr(packedBuffer); inboundPkt.setInEnd(inboundPkt.inPtr() + totalOut / 4); qCDebug(TG_CORE_SESSION) << "unzipped data"; } Query *q = m_pendingQueries.take(msgId); if (!q) { qCWarning(TG_CORE_SESSION) << "No such query"; inboundPkt.setInPtr(inboundPkt.inEnd()); } else { qCDebug(TG_CORE_SESSION) << "acked query with msgId" << QString::number(msgId, 16) << ",pendingQueries:" << m_pendingQueries.size(); q->setAcked(true); Q_EMIT resultReceived(q, inboundPkt); } if (inPtr) { inboundPkt.setInPtr(inPtr); inboundPkt.setInEnd(inEnd); } }
void Session::processRpcMessage(InboundPkt &inboundPkt) { EncryptedMsg *enc = (EncryptedMsg *)inboundPkt.buffer(); qint32 len = inboundPkt.length(); const qint32 MINSZ = offsetof (EncryptedMsg, message); const qint32 UNENCSZ = offsetof (EncryptedMsg, serverSalt); qCDebug(TG_CORE_SESSION) << "processRpcMessage(), len=" << len; if(len < MINSZ || (len & 15) != (UNENCSZ & 15)) return; Q_ASSERT(m_dc->authKeyId()); mAsserter.check(enc->authKeyId == m_dc->authKeyId()); //msg_key is used to compute AES key and to decrypt the received message mCrypto->initAESAuth(m_dc->authKey() + 8, enc->msgKey, AES_DECRYPT); qint32 l = mCrypto->padAESDecrypt((char *)&enc->serverSalt, len - UNENCSZ, (char *)&enc->serverSalt, len - UNENCSZ); Q_UNUSED(l); Q_ASSERT(l == len - UNENCSZ); if( !(!(enc->msgLen & 3) && enc->msgLen > 0 && enc->msgLen <= len - MINSZ && len - MINSZ - enc->msgLen <= 12) ) return; //check msg_key is indeed equal to SHA1 of the plaintext obtained after decription (without final padding bytes). static uchar sha1Buffer[20]; SHA1((uchar *)&enc->serverSalt, enc->msgLen + (MINSZ - UNENCSZ), sha1Buffer); Q_ASSERT(!memcmp (&enc->msgKey, sha1Buffer + 4, 16)); if (m_dc->serverSalt() != enc->serverSalt) { m_dc->setServerSalt(enc->serverSalt); } // check time synchronization qint32 serverTime = enc->msgId >> 32LL; qint32 clientTime = QDateTime::currentDateTime().toTime_t() - mTimeDifference; if (clientTime <= serverTime - 30 || clientTime >= serverTime + 300) { qCDebug(TG_CORE_SESSION) << "salt =" << enc->serverSalt << ", sessionId =" << QString::number(enc->sessionId, 16) << ", msgId =" << QString::number(enc->msgId, 16) << ", seqNo =" << enc->seqNo << ", serverTime =" << serverTime << ", clientTime =" << clientTime; QString alert("Received message has too large difference between client and server dates - "); if (clientTime <= serverTime -30) { alert.append("the message has a date at least 30 seconds later in time than current date"); } else { alert.append("the message was sent at least 300 seconds ago"); } qCWarning(TG_CORE_SESSION) << alert; } inboundPkt.setInPtr(enc->message); inboundPkt.setInEnd(inboundPkt.inPtr() + (enc->msgLen / 4)); qCDebug(TG_CORE_SESSION) << "received message id" << QString::number(enc->msgId, 16); Q_ASSERT(l >= (MINSZ - UNENCSZ) + 8); if (enc->msgId & 1) { addToPendingAcks(enc->msgId); } mAsserter.check(m_sessionId == enc->sessionId); rpcExecuteAnswer(inboundPkt, enc->msgId); mAsserter.check(inboundPkt.inPtr() == inboundPkt.inEnd()); }
void Session::workPacked(InboundPkt &inboundPkt, qint64 msgId) { qCDebug(TG_CORE_SESSION) << "workPacked: msgId =" << QString::number(msgId, 16); mAsserter.check(inboundPkt.fetchInt() == (qint32)TL_GZipPacked); static qint32 buf[MAX_PACKED_SIZE >> 2]; qint32 l = inboundPkt.prefetchStrlen(); char *s = inboundPkt.fetchStr(l); qint32 totalOut = Utils::tinflate(s, l, buf, MAX_PACKED_SIZE); qint32 *inPtr = inboundPkt.inPtr(); qint32 *inEnd = inboundPkt.inEnd(); inboundPkt.setInPtr(buf); inboundPkt.setInEnd(inboundPkt.inPtr() + totalOut / 4); qCDebug(TG_CORE_SESSION) << "Unzipped data"; rpcExecuteAnswer(inboundPkt, msgId); inboundPkt.setInPtr(inPtr); //TODO Not sure about this operations of setting inPtr and inEnd after execute answer completion inboundPkt.setInEnd(inEnd); }
void Session::workContainer (InboundPkt &inboundPkt, qint64 msgId) { qCDebug(TG_CORE_SESSION) << "workContainer: msgId =" << QString::number(msgId, 16); mAsserter.check(inboundPkt.fetchInt() == TL_MsgContainer); qint32 n = inboundPkt.fetchInt(); for (qint32 i = 0; i < n; i++) { // message qint64 id = inboundPkt.fetchLong (); // msg_id if (id & 1) { addToPendingAcks(id); } inboundPkt.fetchInt (); // seq_no qint32 bytes = inboundPkt.fetchInt (); qint32 *t = inboundPkt.inEnd(); inboundPkt.setInEnd(inboundPkt.inPtr() + (bytes / 4)); rpcExecuteAnswer(inboundPkt, id); Q_ASSERT (inboundPkt.inPtr() == inboundPkt.inEnd()); inboundPkt.setInEnd(t); } }