From 0832806bd9db8d5cd4e810c6b9537c37997778a9 Mon Sep 17 00:00:00 2001 From: sachaaaaa Date: Wed, 22 Aug 2018 17:02:58 +1000 Subject: [PATCH] wip: added helper to create mock contacts, almost got a session working --- js/modules/loki_message_api.js | 3 ++- js/signal_protocol_store.js | 27 ++++++++++++++------------- libtextsecure/account_manager.js | 16 ++++++++++++++++ libtextsecure/outgoing_message.js | 18 ++++++++++++++---- libtextsecure/stringview.js | 6 ++++++ 5 files changed, 52 insertions(+), 18 deletions(-) diff --git a/js/modules/loki_message_api.js b/js/modules/loki_message_api.js index 1e45342aa..e769076c1 100644 --- a/js/modules/loki_message_api.js +++ b/js/modules/loki_message_api.js @@ -90,8 +90,9 @@ function HTTPError(message, providedCode, response, stack) { const e = new Error(`${message}; code: ${code}`); e.name = 'HTTPError'; e.code = code; - if (stack) + if (stack) { e.stack += `\nOriginal stack:\n${stack}`; + } if (response) { e.response = response; } diff --git a/js/signal_protocol_store.js b/js/signal_protocol_store.js index 9032f0fcc..881d9d76a 100644 --- a/js/signal_protocol_store.js +++ b/js/signal_protocol_store.js @@ -228,8 +228,8 @@ () => { window.log.info('Successfully fetched contact prekey:', pubKey); resolve({ - pubKey: prekey.get('publicKey'), - privKey: prekey.get('privateKey'), + keyId: prekey.get('keyId'), + publicKey: prekey.get('publicKey') }); }, () => { @@ -239,11 +239,11 @@ ); }); }, - storeContactPreKey(pubKey, keyPair) { + storeContactPreKey(pubKey, preKey) { const prekey = new ContactPreKey({ id: pubKey, - publicKey: keyPair.pubKey, - privateKey: keyPair.privKey, + publicKey: preKey.publicKey, + keyId: preKey.keyId, }); return new Promise(resolve => { prekey.save().always(() => { @@ -315,7 +315,7 @@ }); }); }, - loadSignedPreKey(pubKey) { + loadContactSignedPreKey(pubKey) { const prekey = new ContactSignedPreKey({ id: pubKey }); return new Promise(resolve => { prekey @@ -326,10 +326,10 @@ prekey.get('id') ); resolve({ - pubKey: prekey.get('publicKey'), - privKey: prekey.get('privateKey'), + publicKey: prekey.get('publicKey'), + signature: prekey.get('signature'), created_at: prekey.get('created_at'), - keyId: prekey.get('id'), + keyId: prekey.get('keyId'), confirmed: prekey.get('confirmed'), }); }) @@ -374,13 +374,14 @@ }); }); }, - storeContactSignedPreKey(pubKey, keyPair, confirmed) { + storeContactSignedPreKey(pubKey, signedPreKey) { const prekey = new ContactSignedPreKey({ id: pubKey, - publicKey: keyPair.pubKey, - privateKey: keyPair.privKey, + keyId: signedPreKey.keyId, + publicKey: signedPreKey.publicKey, + signature: signedPreKey.signature, created_at: Date.now(), - confirmed: Boolean(confirmed), + confirmed: false, }); return new Promise(resolve => { prekey.save().always(() => { diff --git a/libtextsecure/account_manager.js b/libtextsecure/account_manager.js index cf48e3dc7..e37bd902a 100644 --- a/libtextsecure/account_manager.js +++ b/libtextsecure/account_manager.js @@ -62,6 +62,22 @@ }) ); }, + addMockContact() { + libsignal.KeyHelper.generateIdentityKeyPair().then(keyPair => { + const pubKey = StringView.arrayBufferToHex(keyPair.pubKey); + const keyId = Math.floor((Math.random() * 1000) + 1); + const signedKeyId = Math.floor((Math.random() * 1000) + 1); + Promise.all([ + libsignal.KeyHelper.generatePreKey(keyId), + libsignal.KeyHelper.generateSignedPreKey(keyPair, signedKeyId) + ]).then((keys) => { + const [preKey, signedPreKey] = keys; + textsecure.storage.protocol.storeContactPreKey(pubKey, { publicKey: preKey.keyPair.pubKey, keyId: keyId }); + textsecure.storage.protocol.storeContactSignedPreKey(pubKey, { publicKey: signedPreKey.keyPair.pubKey, signature: signedPreKey.signature, keyId: signedPreKey.keyId }); + log.info("Added mock contact with pubkey " + pubKey) + }); + }); + }, registerSecondDevice(setProvisioningUrl, confirmNumber, progressCallback) { const createAccount = this.createAccount.bind(this); const clearSessionsAndPreKeys = this.clearSessionsAndPreKeys.bind(this); diff --git a/libtextsecure/outgoing_message.js b/libtextsecure/outgoing_message.js index f01cf5733..daadfff03 100644 --- a/libtextsecure/outgoing_message.js +++ b/libtextsecure/outgoing_message.js @@ -117,9 +117,19 @@ OutgoingMessage.prototype = { let promise = Promise.resolve(); updateDevices.forEach(device => { promise = promise.then(() => - this.server - .getKeysForNumber(number, device) - .then(handleResult) + Promise.all([ + textsecure.storage.protocol.loadContactPreKey(number), + textsecure.storage.protocol.loadContactSignedPreKey(number) + ]).then((keys) => { + const [preKey, signedPreKey] = keys; + if (preKey == undefined || signedPreKey == undefined) { + log.error("Will need to request keys!") + } + else { + const identityKey = StringView.hexToArrayBuffer(number); + return handleResult({ identityKey, devices: [{ deviceId: device, preKey, signedPreKey, registrationId: 0}] }) + } + }) .catch(e => { if (e.name === 'HTTPError' && e.code === 404) { if (device !== 1) { @@ -308,7 +318,7 @@ OutgoingMessage.prototype = { }, sendToNumber(number) { - if (true /* skipEncryption */) + if (false /* skipEncryption */) { const pubKey = number; const data = StringView.bytesToBase64(this.getPlaintext()); diff --git a/libtextsecure/stringview.js b/libtextsecure/stringview.js index 9b6a7d266..b777b31ce 100644 --- a/libtextsecure/stringview.js +++ b/libtextsecure/stringview.js @@ -102,5 +102,11 @@ arrayBufferToHex(aArrayBuffer) { return Array.prototype.map.call(new Uint8Array(aArrayBuffer), x => ('00' + x.toString(16)).slice(-2)).join(''); }, + + hexToArrayBuffer(aString) { + return new Uint8Array(aString.match(/[\da-f]{2}/gi).map(function (h) { + return parseInt(h, 16) + })).buffer + }, }; })();