diff --git a/js/modules/data.js b/js/modules/data.js index 330e579a0..69c46f107 100644 --- a/js/modules/data.js +++ b/js/modules/data.js @@ -664,13 +664,14 @@ async function searchConversations(query, { ConversationCollection }) { } // Message - +const MESSAGE_PRE_KEYS = ['identityKey', 'preKey', 'signature', 'signedKey'].map(k => `preKeyBundle.${k}`); async function getMessageCount() { return channels.getMessageCount(); } async function saveMessage(data, { forceSave, Message } = {}) { - const id = await channels.saveMessage(_cleanData(data), { forceSave }); + const updated = keysFromArrayBuffer(MESSAGE_PRE_KEYS, data); + const id = await channels.saveMessage(_cleanData(updated), { forceSave }); Message.refreshExpirationTimer(); return id; } @@ -713,7 +714,8 @@ async function saveLegacyMessage(data) { } async function saveMessages(arrayOfMessages, { forceSave } = {}) { - await channels.saveMessages(_cleanData(arrayOfMessages), { forceSave }); + const updated = arrayOfMessages.map(m => keysFromArrayBuffer(MESSAGE_PRE_KEYS, m)); + await channels.saveMessages(_cleanData(updated), { forceSave }); } async function removeMessage(id, { Message }) { @@ -738,13 +740,16 @@ async function getMessageById(id, { Message }) { return null; } - return new Message(message); + const encoded = keysToArrayBuffer(MESSAGE_PRE_KEYS, message); + + return new Message(encoded); } // For testing only async function getAllMessages({ MessageCollection }) { const messages = await channels.getAllMessages(); - return new MessageCollection(messages); + const encoded = messages.map(m => keysToArrayBuffer(MESSAGE_PRE_KEYS, m)); + return new MessageCollection(encoded); } async function getAllMessageIds() { @@ -766,7 +771,9 @@ async function getMessageBySender( return null; } - return new Message(messages[0]); + const encoded = keysToArrayBuffer(MESSAGE_PRE_KEYS, messages[0]); + + return new Message(encoded); } async function getUnreadByConversation(conversationId, { MessageCollection }) { @@ -784,7 +791,9 @@ async function getMessagesByConversation( type, }); - return new MessageCollection(messages); + const encoded = messages.map(m => keysToArrayBuffer(MESSAGE_PRE_KEYS, m)); + + return new MessageCollection(encoded); } async function removeAllMessagesInConversation( @@ -819,22 +828,26 @@ async function removeAllMessagesInConversation( async function getMessagesBySentAt(sentAt, { MessageCollection }) { const messages = await channels.getMessagesBySentAt(sentAt); - return new MessageCollection(messages); + const encoded = messages.map(m => keysToArrayBuffer(MESSAGE_PRE_KEYS, m)); + return new MessageCollection(encoded); } async function getExpiredMessages({ MessageCollection }) { const messages = await channels.getExpiredMessages(); - return new MessageCollection(messages); + const encoded = messages.map(m => keysToArrayBuffer(MESSAGE_PRE_KEYS, m)); + return new MessageCollection(encoded); } async function getOutgoingWithoutExpiresAt({ MessageCollection }) { const messages = await channels.getOutgoingWithoutExpiresAt(); - return new MessageCollection(messages); + const encoded = messages.map(m => keysToArrayBuffer(MESSAGE_PRE_KEYS, m)); + return new MessageCollection(encoded); } async function getNextExpiringMessage({ MessageCollection }) { const messages = await channels.getNextExpiringMessage(); - return new MessageCollection(messages); + const encoded = messages.map(m => keysToArrayBuffer(MESSAGE_PRE_KEYS, m)); + return new MessageCollection(encoded); } // Unprocessed diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index d721bc22e..ed912030a 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -934,11 +934,11 @@ MessageReceiver.prototype.extend({ return this.innerHandleContentMessage(envelope, plaintext); }); }, - promptUserToAcceptFriendRequest(envelope, message, preKeyBundle) { + promptUserToAcceptFriendRequest(envelope, message, preKeyBundleMessage) { window.Whisper.events.trigger('showFriendRequest', { pubKey: envelope.source, message, - preKeyBundle, + preKeyBundle: this.decodePreKeyBundleMessage(preKeyBundleMessage), options: { source: envelope.source, sourceDevice: envelope.sourceDevice, @@ -989,7 +989,7 @@ MessageReceiver.prototype.extend({ this.promptUserToAcceptFriendRequest( envelope, content.dataMessage.body, - content.preKeyBundle, + content.preKeyBundleMessage, ); } @@ -998,7 +998,7 @@ MessageReceiver.prototype.extend({ } // Check if our friend request got accepted - if (content.preKeyBundle) { + if (content.preKeyBundleMessage) { // By default we don't want to save the preKey let savePreKey = false; @@ -1020,7 +1020,7 @@ MessageReceiver.prototype.extend({ if (savePreKey && conversation) { await this.handlePreKeyBundleMessage( envelope.source, - content.preKeyBundle + this.decodePreKeyBundleMessage(content.preKeyBundleMessage), ); // Update the conversation @@ -1244,8 +1244,7 @@ MessageReceiver.prototype.extend({ return this.removeFromCache(envelope); }, - async handlePreKeyBundleMessage(pubKey, preKeyBundleMessage) { - const { preKeyId, signedKeyId } = preKeyBundleMessage; + decodePreKeyBundleMessage(preKeyBundleMessage) { const [identityKey, preKey, signedKey, signature] = [ preKeyBundleMessage.identityKey, preKeyBundleMessage.preKey, @@ -1253,6 +1252,24 @@ MessageReceiver.prototype.extend({ preKeyBundleMessage.signature, ].map(k => dcodeIO.ByteBuffer.wrap(k).toArrayBuffer()); + return { + ...preKeyBundleMessage, + identityKey, + preKey, + signedKey, + signature, + }; + }, + async handlePreKeyBundleMessage(pubKey, preKeyBundleMessage) { + const { + preKeyId, + signedKeyId, + identityKey, + preKey, + signedKey, + signature, + } = preKeyBundleMessage; + if (pubKey != StringView.arrayBufferToHex(identityKey)) { throw new Error( 'Error in handlePreKeyBundleMessage: envelope pubkey does not match pubkey in prekey bundle'