diff --git a/libloki/libloki-protocol.js b/libloki/libloki-protocol.js index 3ae45e1ed..204a36c9c 100644 --- a/libloki/libloki-protocol.js +++ b/libloki/libloki-protocol.js @@ -143,7 +143,7 @@ await Promise.all([signedKeyPromise, preKeyPromise]); } - async function sendEmptyMessageWithPreKeys(pubKey) { + async function sendFriendRequestAccepted(pubKey) { // empty content message const content = new textsecure.protobuf.Content(); @@ -155,6 +155,9 @@ log.info('empty message sent successfully'); } }; + const options = { + preKeyBundleType: textsecure.protobuf.PreKeyBundleMessage.Type.FRIEND_REQUEST_ACCEPT, + }; // send an empty message. The logic in ougoing_message will attach the prekeys. const outgoingMessage = new textsecure.OutgoingMessage( null, // server @@ -162,7 +165,8 @@ [pubKey], // numbers content, // message true, // silent - callback // callback + callback, // callback + options ); await outgoingMessage.sendToNumber(pubKey); } @@ -171,5 +175,5 @@ window.libloki.getPreKeyBundleForNumber = getPreKeyBundleForNumber; window.libloki.FallBackDecryptionError = FallBackDecryptionError; window.libloki.savePreKeyBundleForNumber = savePreKeyBundleForNumber; - window.libloki.sendEmptyMessageWithPreKeys = sendEmptyMessageWithPreKeys; + window.libloki.sendFriendRequestAccepted = sendFriendRequestAccepted; })(); diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index 52711848b..28b3ad11c 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -1026,7 +1026,7 @@ MessageReceiver.prototype.extend({ } // Send a reply back - libloki.sendEmptyMessageWithPreKeys(pubKey); + libloki.sendFriendRequestAccepted(pubKey); } window.log.info(`Friend request for ${pubKey} was ${message.friendStatus}`, message); }, @@ -1040,6 +1040,15 @@ MessageReceiver.prototype.extend({ window.log.info('Error getting conversation: ', envelope.source); } + // Check if the other user accepted our friend request + if ( + envelope.preKeyBundleMessage && + envelope.preKeyBundleMessage.type === textsecure.protobuf.PreKeyBundleMessage.Type.FRIEND_REQUEST_ACCEPT && + conversation + ) { + await conversation.onFriendRequestAccepted(); + } + if (envelope.type === textsecure.protobuf.Envelope.Type.FRIEND_REQUEST) { return this.handleFriendRequestMessage(envelope, content.dataMessage); } else if ( @@ -1048,12 +1057,10 @@ MessageReceiver.prototype.extend({ // ref: libsignal-protocol.js:36120 envelope.type === textsecure.protobuf.Envelope.Type.PREKEY_BUNDLE ) { - // We know for sure that keys are exchanged - if (conversation) { + // If we get a cipher and we're already friends + // then we set our key exchange to complete + if (conversation && conversation.isFriend()) { await conversation.setKeyExchangeCompleted(true); - - // TODO: We should probably set this based on the PKB type - await conversation.onFriendRequestAccepted(); } } diff --git a/libtextsecure/outgoing_message.js b/libtextsecure/outgoing_message.js index 39fbfab87..8fa8aab9e 100644 --- a/libtextsecure/outgoing_message.js +++ b/libtextsecure/outgoing_message.js @@ -43,9 +43,10 @@ function OutgoingMessage( this.failoverNumbers = []; this.unidentifiedDeliveries = []; - const { numberInfo, senderCertificate } = options; + const { numberInfo, senderCertificate, preKeyBundleType } = options; this.numberInfo = numberInfo; this.senderCertificate = senderCertificate; + this.preKeyBundleType = preKeyBundleType || textsecure.protobuf.PreKeyBundleMessage.Type.UNKOWN; } OutgoingMessage.prototype = { @@ -290,6 +291,12 @@ OutgoingMessage.prototype = { if (this.attachPrekeys) { // Encrypt them with the fallback const preKeyBundleMessage = await libloki.getPreKeyBundleForNumber(number); + preKeyBundleMessage.type = this.preKeyBundleType; + + // If we have to use fallback encryption then this must be a friend request + if (this.fallBackEncryption) + preKeyBundleMessage.type = textsecure.protobuf.PreKeyBundleMessage.Type.FRIEND_REQUEST; + const textBundle = this.convertMessageToText(preKeyBundleMessage); const encryptedBundle = await fallBackEncryption.encrypt(textBundle); preKeys = { preKeyBundleMessage: encryptedBundle.body };