Attach preKeyBundle outside of the message content.

pull/50/head
Mikunj 6 years ago
parent cf3e9716ed
commit 1264630649

@ -202,15 +202,19 @@ OutgoingMessage.prototype = {
return messagePartCount * 160; return messagePartCount * 160;
}, },
convertMessageToText(message) {
const messageBuffer = message.toArrayBuffer();
const plaintext = new Uint8Array(
this.getPaddedMessageLength(messageBuffer.byteLength + 1) - 1
);
plaintext.set(new Uint8Array(messageBuffer));
plaintext[messageBuffer.byteLength] = 0x80;
return plaintext;
},
getPlaintext() { getPlaintext() {
if (!this.plaintext) { if (!this.plaintext) {
const messageBuffer = this.message.toArrayBuffer(); this.plaintext = this.convertMessageToText(this.message);
this.plaintext = new Uint8Array(
this.getPaddedMessageLength(messageBuffer.byteLength + 1) - 1
);
this.plaintext.set(new Uint8Array(messageBuffer));
this.plaintext[messageBuffer.byteLength] = 0x80;
} }
return this.plaintext; return this.plaintext;
}, },
@ -275,6 +279,18 @@ OutgoingMessage.prototype = {
const address = new libsignal.SignalProtocolAddress(number, deviceId); const address = new libsignal.SignalProtocolAddress(number, deviceId);
const ourKey = textsecure.storage.user.getNumber(); const ourKey = textsecure.storage.user.getNumber();
const options = {}; const options = {};
const fallBackEncryption = new libloki.FallBackSessionCipher(address);
// Check if we need to attach the preKeys
let preKeys = {};
if (this.attachPrekeys) {
// Encrypt them with the fallback
const preKeyBundleMessage = await libloki.getPreKeyBundleForNumber(number);
const textBundle = this.convertMessageToText(preKeyBundleMessage);
const encryptedBundle = await fallBackEncryption.encrypt(textBundle);
preKeys = { preKeyBundleMessage: encryptedBundle.body };
window.log.info('attaching prekeys to outgoing message');
}
// No limit on message keys if we're communicating with our other devices // No limit on message keys if we're communicating with our other devices
if (ourKey === number) { if (ourKey === number) {
@ -283,7 +299,7 @@ OutgoingMessage.prototype = {
let sessionCipher; let sessionCipher;
if (this.fallBackEncryption) { if (this.fallBackEncryption) {
sessionCipher = new libloki.FallBackSessionCipher(address); sessionCipher = fallBackEncryption;
} else { } else {
sessionCipher = new libsignal.SessionCipher( sessionCipher = new libsignal.SessionCipher(
textsecure.storage.protocol, textsecure.storage.protocol,
@ -292,26 +308,26 @@ OutgoingMessage.prototype = {
); );
} }
ciphers[address.getDeviceId()] = sessionCipher; ciphers[address.getDeviceId()] = sessionCipher;
return sessionCipher
.encrypt(plaintext) // Encrypt our plain text
.then(ciphertext => { const ciphertext = await sessionCipher.encrypt(plaintext);
if (!this.fallBackEncryption) if (!this.fallBackEncryption) {
// eslint-disable-next-line no-param-reassign // eslint-disable-next-line no-param-reassign
ciphertext.body = new Uint8Array( ciphertext.body = new Uint8Array(
dcodeIO.ByteBuffer.wrap( dcodeIO.ByteBuffer.wrap(
ciphertext.body, ciphertext.body,
'binary' 'binary'
).toArrayBuffer() ).toArrayBuffer()
); );
return ciphertext; }
}) return {
.then(ciphertext => ({ type: ciphertext.type, // FallBackSessionCipher sets this to FRIEND_REQUEST
type: ciphertext.type, ourKey,
ourKey, sourceDevice: 1,
sourceDevice: 1, destinationRegistrationId: ciphertext.registrationId,
destinationRegistrationId: ciphertext.registrationId, content: ciphertext.body,
content: ciphertext.body, ...preKeys,
})); };
}) })
) )
.then(async outgoingObjects => { .then(async outgoingObjects => {
@ -441,14 +457,14 @@ OutgoingMessage.prototype = {
return this.getStaleDeviceIdsForNumber(number).then(updateDevices => return this.getStaleDeviceIdsForNumber(number).then(updateDevices =>
this.getKeysForNumber(number, updateDevices) this.getKeysForNumber(number, updateDevices)
.then(async keysFound => { .then(async keysFound => {
let attachPrekeys = false; this.attachPrekeys = false;
if (!keysFound) { if (!keysFound) {
log.info('Fallback encryption enabled'); log.info('Fallback encryption enabled');
this.fallBackEncryption = true; this.fallBackEncryption = true;
attachPrekeys = true; this.attachPrekeys = true;
} else if (conversation) { } else if (conversation) {
try { try {
attachPrekeys = !conversation.isKeyExchangeCompleted(); this.attachPrekeys = !conversation.isKeyExchangeCompleted();
} catch (e) { } catch (e) {
// do nothing // do nothing
} }
@ -457,13 +473,6 @@ OutgoingMessage.prototype = {
if (this.fallBackEncryption && conversation) { if (this.fallBackEncryption && conversation) {
conversation.onFriendRequestSent(); conversation.onFriendRequestSent();
} }
if (attachPrekeys) {
log.info('attaching prekeys to outgoing message');
this.message.preKeyBundleMessage = await libloki.getPreKeyBundleForNumber(
number
);
}
}) })
.then(this.reloadDevicesAndSend(number, true)) .then(this.reloadDevicesAndSend(number, true))
.catch(error => { .catch(error => {

@ -24,6 +24,7 @@ message Envelope {
optional bytes content = 8; // Contains an encrypted Content optional bytes content = 8; // Contains an encrypted Content
optional string serverGuid = 9; optional string serverGuid = 9;
optional uint64 serverTimestamp = 10; optional uint64 serverTimestamp = 10;
optional PreKeyBundleMessage preKeyBundleMessage = 101;
} }
@ -33,7 +34,6 @@ message Content {
optional CallMessage callMessage = 3; optional CallMessage callMessage = 3;
optional NullMessage nullMessage = 4; optional NullMessage nullMessage = 4;
optional ReceiptMessage receiptMessage = 5; optional ReceiptMessage receiptMessage = 5;
optional PreKeyBundleMessage preKeyBundleMessage = 6;
} }
message PreKeyBundleMessage { message PreKeyBundleMessage {

Loading…
Cancel
Save