fix: incorrect enc msg detection

This commit is contained in:
Adhiraj Singh
2021-12-19 00:33:33 +05:30
parent 89a159aac3
commit af0804048c

View File

@@ -30,39 +30,42 @@ export const decodeWAMessage = (
if(typeof data === 'string' || !possiblyEnc) { if(typeof data === 'string' || !possiblyEnc) {
json = JSON.parse(data.toString()) // parse the JSON json = JSON.parse(data.toString()) // parse the JSON
} else { } else {
try {
const { macKey, encKey } = auth || {} json = JSON.parse(data.toString())
if (!macKey || !encKey) { } catch {
throw new Boom('recieved encrypted buffer when auth creds unavailable', { data: message, statusCode: DisconnectReason.badSession }) const { macKey, encKey } = auth || {}
} if (!macKey || !encKey) {
/* throw new Boom('recieved encrypted buffer when auth creds unavailable', { data: message, statusCode: DisconnectReason.badSession })
If the data recieved was not a JSON, then it must be an encrypted message. }
Such a message can only be decrypted if we're connected successfully to the servers & have encryption keys /*
*/ If the data recieved was not a JSON, then it must be an encrypted message.
if (fromMe) { Such a message can only be decrypted if we're connected successfully to the servers & have encryption keys
tags = [data[0], data[1]] */
data = data.slice(2, data.length) if (fromMe) {
} tags = [data[0], data[1]]
data = data.slice(2, data.length)
const checksum = data.slice(0, 32) // the first 32 bytes of the buffer are the HMAC sign of the message }
data = data.slice(32, data.length) // the actual message
const computedChecksum = hmacSign(data, macKey) // compute the sign of the message we recieved using our macKey const checksum = data.slice(0, 32) // the first 32 bytes of the buffer are the HMAC sign of the message
data = data.slice(32, data.length) // the actual message
if (checksum.equals(computedChecksum)) { const computedChecksum = hmacSign(data, macKey) // compute the sign of the message we recieved using our macKey
// the checksum the server sent, must match the one we computed for the message to be valid
const decrypted = aesDecrypt(data, encKey) // decrypt using AES if (checksum.equals(computedChecksum)) {
json = decodeBinaryNodeLegacy(decrypted, { index: 0 }) // decode the binary message into a JSON array // the checksum the server sent, must match the one we computed for the message to be valid
} else { const decrypted = aesDecrypt(data, encKey) // decrypt using AES
throw new Boom('Bad checksum', { json = decodeBinaryNodeLegacy(decrypted, { index: 0 }) // decode the binary message into a JSON array
data: { } else {
received: checksum.toString('hex'), throw new Boom('Bad checksum', {
computed: computedChecksum.toString('hex'), data: {
data: data.slice(0, 80).toString(), received: checksum.toString('hex'),
tag: messageTag, computed: computedChecksum.toString('hex'),
message: message.slice(0, 80).toString() data: data.slice(0, 80).toString(),
}, tag: messageTag,
statusCode: DisconnectReason.badSession message: message.slice(0, 80).toString()
}) },
statusCode: DisconnectReason.badSession
})
}
} }
} }
} }