session-desktop/libtextsecure/outgoing_message.js

511 lines
17 KiB
JavaScript
Raw Normal View History

2018-11-01 01:08:41 +01:00
/* global
textsecure,
libsignal,
window,
ConversationController,
libloki,
StringView,
dcodeIO,
log,
2019-01-31 01:58:43 +01:00
lokiMessageAPI,
*/
2018-07-21 23:51:20 +02:00
/* eslint-disable more/no-then */
/* eslint-disable no-unreachable */
2018-07-21 23:51:20 +02:00
2018-05-02 18:51:22 +02:00
function OutgoingMessage(
server,
timestamp,
numbers,
message,
silent,
2018-11-07 05:16:49 +01:00
callback,
options = {}
2018-05-02 18:51:22 +02:00
) {
if (message instanceof textsecure.protobuf.DataMessage) {
2018-07-21 23:51:20 +02:00
const content = new textsecure.protobuf.Content();
2018-05-02 18:51:22 +02:00
content.dataMessage = message;
2018-07-21 23:51:20 +02:00
// eslint-disable-next-line no-param-reassign
2018-05-02 18:51:22 +02:00
message = content;
}
this.server = server;
this.timestamp = timestamp;
this.numbers = numbers;
this.message = message; // ContentMessage proto
this.callback = callback;
this.silent = silent;
this.numbersCompleted = 0;
this.errors = [];
this.successfulNumbers = [];
this.fallBackEncryption = false;
2018-11-07 05:16:49 +01:00
this.failoverNumbers = [];
this.unidentifiedDeliveries = [];
const { numberInfo, senderCertificate, online, messageType } = options || {};
2018-11-07 05:16:49 +01:00
this.numberInfo = numberInfo;
this.senderCertificate = senderCertificate;
2018-11-14 20:10:32 +01:00
this.online = online;
2018-12-17 01:38:06 +01:00
this.messageType = messageType || 'outgoing';
}
OutgoingMessage.prototype = {
2018-05-02 18:51:22 +02:00
constructor: OutgoingMessage,
2018-07-21 23:51:20 +02:00
numberCompleted() {
this.numbersCompleted += 1;
2018-05-02 18:51:22 +02:00
if (this.numbersCompleted >= this.numbers.length) {
this.callback({
successfulNumbers: this.successfulNumbers,
2018-11-07 05:16:49 +01:00
failoverNumbers: this.failoverNumbers,
2018-05-02 18:51:22 +02:00
errors: this.errors,
2018-11-07 05:16:49 +01:00
unidentifiedDeliveries: this.unidentifiedDeliveries,
messageType: this.messageType,
2018-05-02 18:51:22 +02:00
});
}
},
2018-07-21 23:51:20 +02:00
registerError(number, reason, error) {
2018-05-02 18:51:22 +02:00
if (!error || (error.name === 'HTTPError' && error.code !== 404)) {
2018-07-21 23:51:20 +02:00
// eslint-disable-next-line no-param-reassign
2018-05-02 18:51:22 +02:00
error = new textsecure.OutgoingMessageError(
number,
this.message.toArrayBuffer(),
this.timestamp,
error
);
}
2018-07-21 23:51:20 +02:00
// eslint-disable-next-line no-param-reassign
2018-05-02 18:51:22 +02:00
error.number = number;
2018-07-21 23:51:20 +02:00
// eslint-disable-next-line no-param-reassign
2018-05-02 18:51:22 +02:00
error.reason = reason;
this.errors[this.errors.length] = error;
this.numberCompleted();
},
2018-07-21 23:51:20 +02:00
reloadDevicesAndSend(number, recurse) {
return () =>
textsecure.storage.protocol.getDeviceIds(number).then(deviceIds => {
if (deviceIds.length === 0) {
2018-11-01 01:08:41 +01:00
// eslint-disable-next-line no-param-reassign
deviceIds = [1];
// return this.registerError(
// number,
// 'Got empty device list when loading device keys',
// null
// );
2018-07-21 23:51:20 +02:00
}
return this.doSendMessage(number, deviceIds, recurse);
});
},
getKeysForNumber(number, updateDevices) {
const handleResult = response =>
Promise.all(
response.devices.map(device => {
// eslint-disable-next-line no-param-reassign
device.identityKey = response.identityKey;
if (
updateDevices === undefined ||
updateDevices.indexOf(device.deviceId) > -1
) {
const address = new libsignal.SignalProtocolAddress(
2018-05-02 18:51:22 +02:00
number,
2018-07-21 23:51:20 +02:00
device.deviceId
2018-05-02 18:51:22 +02:00
);
2018-07-21 23:51:20 +02:00
const builder = new libsignal.SessionBuilder(
textsecure.storage.protocol,
address
);
if (device.registrationId === 0) {
window.log.info('device registrationId 0!');
}
2019-01-16 05:44:13 +01:00
return builder
.processPreKey(device)
.then(async () => {
// TODO: only remove the keys that were used above!
2019-01-31 01:58:43 +01:00
await libloki.storage.removeContactPreKeyBundle(number);
2019-01-16 05:44:13 +01:00
return true;
})
.catch(error => {
if (error.message === 'Identity key changed') {
// eslint-disable-next-line no-param-reassign
error.timestamp = this.timestamp;
// eslint-disable-next-line no-param-reassign
error.originalMessage = this.message.toArrayBuffer();
// eslint-disable-next-line no-param-reassign
error.identityKey = device.identityKey;
}
throw error;
});
2018-05-02 18:51:22 +02:00
}
return false;
2018-07-21 23:51:20 +02:00
})
2018-05-02 18:51:22 +02:00
);
// TODO: check if still applicable
// if (updateDevices === undefined) {
// return this.server.getKeysForNumber(number, '*').then(handleResult);
// }
let promise = Promise.resolve(true);
2018-07-21 23:51:20 +02:00
updateDevices.forEach(device => {
promise = promise.then(() =>
Promise.all([
textsecure.storage.protocol.loadContactPreKey(number),
2018-11-01 01:08:41 +01:00
textsecure.storage.protocol.loadContactSignedPreKey(number),
])
.then(keys => {
const [preKey, signedPreKey] = keys;
if (preKey === undefined || signedPreKey === undefined) {
return false;
}
const identityKey = StringView.hexToArrayBuffer(number);
2018-11-01 01:08:41 +01:00
return handleResult({
identityKey,
devices: [
{ deviceId: device, preKey, signedPreKey, registrationId: 0 },
],
}).then(results => results.every(value => value === true));
2018-11-01 01:08:41 +01:00
})
2018-07-21 23:51:20 +02:00
.catch(e => {
if (e.name === 'HTTPError' && e.code === 404) {
if (device !== 1) {
return this.removeDeviceIdsForNumber(number, [device]);
}
throw new textsecure.UnregisteredUserError(number, e);
} else {
throw e;
}
})
2018-05-02 18:51:22 +02:00
);
2018-07-21 23:51:20 +02:00
});
2018-05-02 18:51:22 +02:00
2018-07-21 23:51:20 +02:00
return promise;
2018-05-02 18:51:22 +02:00
},
// Default ttl to 24 hours if no value provided
async transmitMessage(number, data, timestamp, ttl = 24 * 60 * 60) {
const pubKey = number;
try {
2019-01-31 01:58:43 +01:00
await lokiMessageAPI.sendMessage(pubKey, data, timestamp, ttl);
2018-11-01 01:08:41 +01:00
} catch (e) {
if (e.name === 'HTTPError' && (e.code !== 409 && e.code !== 410)) {
// 409 and 410 should bubble and be handled by doSendMessage
// 404 should throw UnregisteredUserError
// all other network errors can be retried later.
if (e.code === 404) {
throw new textsecure.UnregisteredUserError(number, e);
}
2018-11-01 01:08:41 +01:00
throw new textsecure.SendMessageNetworkError(number, '', e, timestamp);
2018-12-16 23:45:26 +01:00
} else if (e.name === 'TimedOutError') {
throw new textsecure.PoWError(number, e);
}
throw e;
}
2018-05-02 18:51:22 +02:00
},
2018-07-21 23:51:20 +02:00
getPaddedMessageLength(messageLength) {
const messageLengthWithTerminator = messageLength + 1;
let messagePartCount = Math.floor(messageLengthWithTerminator / 160);
2018-05-02 18:51:22 +02:00
if (messageLengthWithTerminator % 160 !== 0) {
2018-07-21 23:51:20 +02:00
messagePartCount += 1;
2018-05-02 18:51:22 +02:00
}
2018-05-02 18:51:22 +02:00
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;
},
2018-07-21 23:51:20 +02:00
getPlaintext() {
2018-05-02 18:51:22 +02:00
if (!this.plaintext) {
this.plaintext = this.convertMessageToText(this.message);
2018-05-02 18:51:22 +02:00
}
return this.plaintext;
},
async wrapInWebsocketMessage(outgoingObject) {
const messageEnvelope = new textsecure.protobuf.Envelope({
type: outgoingObject.type,
source: outgoingObject.ourKey,
sourceDevice: outgoingObject.sourceDevice,
timestamp: this.timestamp,
content: outgoingObject.content,
});
const requestMessage = new textsecure.protobuf.WebSocketRequestMessage({
2018-11-01 01:08:41 +01:00
id: new Uint8Array(libsignal.crypto.getRandomBytes(1))[0], // random ID for now
verb: 'PUT',
path: '/api/v1/message',
body: messageEnvelope.encode().toArrayBuffer(),
});
2018-10-08 06:03:32 +02:00
const websocketMessage = new textsecure.protobuf.WebSocketMessage({
type: textsecure.protobuf.WebSocketMessage.Type.REQUEST,
2018-11-01 01:08:41 +01:00
request: requestMessage,
});
2018-11-01 01:08:41 +01:00
const bytes = new Uint8Array(websocketMessage.encode().toArrayBuffer());
log.info(bytes.toString()); // print bytes for debugging purposes: can be injected in mock socket server
return bytes;
},
2018-07-21 23:51:20 +02:00
doSendMessage(number, deviceIds, recurse) {
const ciphers = {};
2018-11-08 00:35:06 +01:00
/* Disabled because i'm not sure how senderCertificate works :thinking:
2018-11-07 05:16:49 +01:00
const { numberInfo, senderCertificate } = this;
const info = numberInfo && numberInfo[number] ? numberInfo[number] : {};
const { accessKey } = info || {};
if (accessKey && !senderCertificate) {
return Promise.reject(
new Error(
'OutgoingMessage.doSendMessage: accessKey was provided, ' +
'but senderCertificate was not'
2018-11-07 05:16:49 +01:00
)
);
}
const sealedSender = Boolean(accessKey && senderCertificate);
// We don't send to ourselves if unless sealedSender is enabled
const ourNumber = textsecure.storage.user.getNumber();
const ourDeviceId = textsecure.storage.user.getDeviceId();
if (number === ourNumber && !sealedSender) {
// eslint-disable-next-line no-param-reassign
deviceIds = _.reject(
deviceIds,
deviceId =>
// because we store our own device ID as a string at least sometimes
deviceId === ourDeviceId || deviceId === parseInt(ourDeviceId, 10)
);
}
2018-11-08 00:35:06 +01:00
*/
2018-11-07 05:16:49 +01:00
2018-05-02 18:51:22 +02:00
return Promise.all(
deviceIds.map(async deviceId => {
2018-07-21 23:51:20 +02:00
const address = new libsignal.SignalProtocolAddress(number, deviceId);
const ourKey = textsecure.storage.user.getNumber();
2018-07-21 23:51:20 +02:00
const options = {};
2019-01-16 05:44:13 +01:00
const fallBackCipher = new libloki.crypto.FallBackSessionCipher(
address
);
// Check if we need to attach the preKeys
let sessionCipher;
2018-11-30 04:02:32 +01:00
const isFriendRequest = this.messageType === 'friend-request';
2019-01-16 05:44:13 +01:00
const flags = this.message.dataMessage
? this.message.dataMessage.get_flags()
: null;
const isEndSession =
flags === textsecure.protobuf.DataMessage.Flags.END_SESSION;
2018-11-30 04:02:32 +01:00
if (isFriendRequest || isEndSession) {
// Encrypt them with the fallback
2019-01-10 00:26:25 +01:00
const pkb = await libloki.storage.getPreKeyBundleForContact(number);
2019-01-16 05:44:13 +01:00
const preKeyBundleMessage = new textsecure.protobuf.PreKeyBundleMessage(
pkb
);
this.message.preKeyBundleMessage = preKeyBundleMessage;
window.log.info('attaching prekeys to outgoing message');
2018-11-30 04:02:32 +01:00
}
if (isFriendRequest) {
sessionCipher = fallBackCipher;
} else {
sessionCipher = new libsignal.SessionCipher(
textsecure.storage.protocol,
address,
options
);
}
const plaintext = this.getPlaintext();
// No limit on message keys if we're communicating with our other devices
if (ourKey === number) {
options.messageKeysLimit = false;
}
2018-07-21 23:51:20 +02:00
ciphers[address.getDeviceId()] = sessionCipher;
// Encrypt our plain text
const ciphertext = await sessionCipher.encrypt(plaintext);
if (!this.fallBackEncryption) {
// eslint-disable-next-line no-param-reassign
ciphertext.body = new Uint8Array(
2019-01-16 05:44:13 +01:00
dcodeIO.ByteBuffer.wrap(ciphertext.body, 'binary').toArrayBuffer()
);
}
let ttl;
if (this.messageType === 'friend-request') {
ttl = 4 * 24 * 60 * 60; // 4 days for friend request message
} else if (this.messageType === 'onlineBroadcast') {
ttl = 10 * 60; // 10 minutes for online broadcast message
} else {
const hours = window.getMessageTTL() || 24; // 1 day default for any other message
ttl = hours * 60 * 60;
}
return {
type: ciphertext.type, // FallBackSessionCipher sets this to FRIEND_REQUEST
ttl,
ourKey,
sourceDevice: 1,
destinationRegistrationId: ciphertext.registrationId,
content: ciphertext.body,
};
2018-07-21 23:51:20 +02:00
})
2018-05-02 18:51:22 +02:00
)
.then(async outgoingObjects => {
// TODO: handle multiple devices/messages per transmit
const outgoingObject = outgoingObjects[0];
const socketMessage = await this.wrapInWebsocketMessage(outgoingObject);
await this.transmitMessage(
number,
socketMessage,
this.timestamp,
outgoingObject.ttl
);
this.successfulNumbers[this.successfulNumbers.length] = number;
this.numberCompleted();
2018-11-01 01:08:41 +01:00
})
2018-07-21 23:51:20 +02:00
.catch(error => {
// TODO(loki): handle http errors properly
// - retry later if 400
// - ignore if 409 (conflict) means the hash already exists
throw error;
2018-07-21 23:51:20 +02:00
if (
error instanceof Error &&
error.name === 'HTTPError' &&
(error.code === 410 || error.code === 409)
) {
if (!recurse)
return this.registerError(
number,
'Hit retry limit attempting to reload device list',
error
2018-05-02 18:51:22 +02:00
);
2018-07-21 23:51:20 +02:00
let p;
if (error.code === 409) {
p = this.removeDeviceIdsForNumber(
2018-05-02 18:51:22 +02:00
number,
2018-07-21 23:51:20 +02:00
error.response.extraDevices
2018-05-02 18:51:22 +02:00
);
} else {
2018-07-21 23:51:20 +02:00
p = Promise.all(
error.response.staleDevices.map(deviceId =>
2018-11-07 05:16:49 +01:00
ciphers[deviceId].closeOpenSessionForDevice(
new libsignal.SignalProtocolAddress(number, deviceId)
)
2018-07-21 23:51:20 +02:00
)
2018-05-02 18:51:22 +02:00
);
}
2018-07-21 23:51:20 +02:00
return p.then(() => {
const resetDevices =
error.code === 410
? error.response.staleDevices
: error.response.missingDevices;
return this.getKeysForNumber(number, resetDevices).then(
2018-11-07 05:16:49 +01:00
// We continue to retry as long as the error code was 409; the assumption is
// that we'll request new device info and the next request will succeed.
2018-07-21 23:51:20 +02:00
this.reloadDevicesAndSend(number, error.code === 409)
2018-05-02 18:51:22 +02:00
);
2018-07-21 23:51:20 +02:00
});
} else if (error.message === 'Identity key changed') {
// eslint-disable-next-line no-param-reassign
error.timestamp = this.timestamp;
// eslint-disable-next-line no-param-reassign
error.originalMessage = this.message.toArrayBuffer();
window.log.error(
'Got "key changed" error from encrypt - no identityKey for application layer',
number,
deviceIds
);
throw error;
} else {
this.registerError(number, 'Failed to create or send message', error);
}
return null;
2018-05-02 18:51:22 +02:00
});
},
2018-07-21 23:51:20 +02:00
getStaleDeviceIdsForNumber(number) {
return textsecure.storage.protocol.getDeviceIds(number).then(deviceIds => {
if (deviceIds.length === 0) {
return [1];
}
const updateDevices = [];
return Promise.all(
deviceIds.map(deviceId => {
const address = new libsignal.SignalProtocolAddress(number, deviceId);
const sessionCipher = new libsignal.SessionCipher(
textsecure.storage.protocol,
address
);
return sessionCipher.hasOpenSession().then(hasSession => {
if (!hasSession) {
updateDevices.push(deviceId);
}
});
})
).then(() => updateDevices);
});
},
removeDeviceIdsForNumber(number, deviceIdsToRemove) {
let promise = Promise.resolve();
// eslint-disable-next-line no-restricted-syntax, guard-for-in
for (const j in deviceIdsToRemove) {
promise = promise.then(() => {
const encodedNumber = `${number}.${deviceIdsToRemove[j]}`;
2018-05-02 18:51:22 +02:00
return textsecure.storage.protocol.removeSession(encodedNumber);
});
}
2018-05-02 18:51:22 +02:00
return promise;
},
2018-07-21 23:51:20 +02:00
sendToNumber(number) {
let conversation;
try {
conversation = ConversationController.get(number);
2018-11-01 01:08:41 +01:00
} catch (e) {
// do nothing
}
return this.getStaleDeviceIdsForNumber(number).then(updateDevices =>
this.getKeysForNumber(number, updateDevices)
2018-11-01 01:08:41 +01:00
.then(async keysFound => {
if (!keysFound) {
log.info('Fallback encryption enabled');
this.fallBackEncryption = true;
}
2018-11-01 01:08:41 +01:00
})
.then(this.reloadDevicesAndSend(number, true))
.catch(error => {
conversation.resetPendingSend();
if (error.message === 'Identity key changed') {
// eslint-disable-next-line no-param-reassign
error = new textsecure.OutgoingIdentityKeyError(
number,
error.originalMessage,
error.timestamp,
error.identityKey
);
this.registerError(number, 'Identity key changed', error);
} else {
this.registerError(
number,
`Failed to retrieve new device keys for number ${number}`,
error
);
}
})
);
2018-05-02 18:51:22 +02:00
},
};
window.textsecure = window.textsecure || {};
window.textsecure.OutgoingMessage = OutgoingMessage;