Skip to content

Commit

Permalink
Clean up UUID-handling to prepare for future
Browse files Browse the repository at this point in the history
  • Loading branch information
scottnonnenberg-signal committed Dec 9, 2020
1 parent 5369950 commit 3f58a9b
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 32 deletions.
66 changes: 43 additions & 23 deletions js/modules/refresh_sender_certificate.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,32 @@ function initialize({ events, storage, navigator, logger }) {
// Keeping this entrypoint around so more inialize() calls just kick the timing
scheduleNext = scheduleNextRotation;

async function saveCert({ certificate, key }) {
const arrayBuffer = window.Signal.Crypto.base64ToArrayBuffer(certificate);
const decodedContainer = textsecure.protobuf.SenderCertificate.decode(
arrayBuffer
);
const decodedCert = textsecure.protobuf.SenderCertificate.Certificate.decode(
decodedContainer.certificate
);

// We don't want to send a protobuf-generated object across IPC, so we make
// our own object.
const toSave = {
expires: decodedCert.expires.toNumber(),
serialized: arrayBuffer,
};
await storage.put(key, toSave);
}

async function removeOldKey() {
const oldCertKey = 'senderCertificateWithUuid';
const oldUuidCert = storage.get(oldCertKey);
if (oldUuidCert) {
await storage.remove(oldCertKey);
}
}

async function run() {
logger.info('refreshSenderCertificate: Getting new certificate...');
try {
Expand All @@ -76,29 +102,23 @@ function initialize({ events, storage, navigator, logger }) {
password: PASSWORD,
});

const { certificate } = await server.getSenderCertificate();
const arrayBuffer = window.Signal.Crypto.base64ToArrayBuffer(certificate);
const decodedContainer = textsecure.protobuf.SenderCertificate.decode(
arrayBuffer
);
const decodedCert = textsecure.protobuf.SenderCertificate.Certificate.decode(
decodedContainer.certificate
);

// We don't want to send a protobuf-generated object across IPC, so we make
// our own object.
const toSave = {
expires: decodedCert.expires.toNumber(),
serialized: arrayBuffer,
};

storage.put('senderCertificate', toSave);

const oldCertKey = 'senderCertificateWithUuid';
const oldUuidCert = storage.get(oldCertKey);
if (oldUuidCert) {
await storage.remove(oldCertKey);
}
const omitE164 = true;
const [
{ certificate },
{ certificate: certificateWithNoE164 },
] = await Promise.all([
server.getSenderCertificate(),
server.getSenderCertificate(omitE164),
]);

await Promise.all([
saveCert({ certificate, key: 'senderCertificate' }),
saveCert({
certificate: certificateWithNoE164,
key: 'senderCertificateNoE164',
}),
removeOldKey(),
]);

scheduledTime = null;
scheduleNextRotation();
Expand Down
5 changes: 3 additions & 2 deletions ts/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1810,11 +1810,12 @@ type WhatIsThis = import('./window.d').WhatIsThis;
!c.isEverUnregistered()
)
)
.map(c => c.get('e164'));
.map(c => c.get('e164'))
.filter(Boolean) as Array<string>;

if (lonelyE164s.length > 0) {
const lookup = await window.textsecure.messaging.getUuidsForE164s(
lonelyE164s as WhatIsThis
lonelyE164s
);
const e164s = Object.keys(lookup);
e164s.forEach(e164 => {
Expand Down
2 changes: 0 additions & 2 deletions ts/models/conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2428,7 +2428,6 @@ export class ConversationModel extends window.Backbone.Model<
: '';

return {
author: contact.get('e164'),
authorUuid: contact.get('uuid'),
bodyRanges: quotedMessage.get('bodyRanges'),
id: quotedMessage.get('sent_at'),
Expand Down Expand Up @@ -2594,7 +2593,6 @@ export class ConversationModel extends window.Backbone.Model<
async sendReactionMessage(
reaction: { emoji: string; remove: boolean },
target: {
targetAuthorE164: string;
targetAuthorUuid: string;
targetTimestamp: number;
}
Expand Down
1 change: 1 addition & 0 deletions ts/sql/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ async function removeAllSignedPreKeys() {
const ITEM_KEYS: { [key: string]: Array<string> | undefined } = {
identityKey: ['value.pubKey', 'value.privKey'],
senderCertificate: ['value.serialized'],
senderCertificateNoE164: ['value.serialized'],
signaling_key: ['value'],
profileKey: ['value'],
};
Expand Down
9 changes: 7 additions & 2 deletions ts/textsecure/WebAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1083,13 +1083,18 @@ export function initialize({
);
}

async function getSenderCertificate() {
async function getSenderCertificate(omitE164?: boolean) {
const baseParameters = '?includeUuid=true';
const urlParameters = `${baseParameters}${
omitE164 ? '&includeE164=false' : ''
}`;

return _ajax({
call: 'deliveryCert',
httpType: 'GET',
responseType: 'json',
validateResponse: { certificate: 'string' },
urlParameters: '?includeUuid=true',
urlParameters,
});
}

Expand Down
4 changes: 2 additions & 2 deletions ts/util/lint/exceptions.json
Original file line number Diff line number Diff line change
Expand Up @@ -15173,15 +15173,15 @@
"rule": "jQuery-wrap(",
"path": "ts/textsecure/WebAPI.js",
"line": " const byteBuffer = window.dcodeIO.ByteBuffer.wrap(quote, 'binary', window.dcodeIO.ByteBuffer.LITTLE_ENDIAN);",
"lineNumber": 1263,
"lineNumber": 1265,
"reasonCategory": "falseMatch",
"updated": "2020-09-08T23:07:22.682Z"
},
{
"rule": "jQuery-wrap(",
"path": "ts/textsecure/WebAPI.ts",
"line": " const byteBuffer = window.dcodeIO.ByteBuffer.wrap(",
"lineNumber": 2172,
"lineNumber": 2177,
"reasonCategory": "falseMatch",
"updated": "2020-09-08T23:07:22.682Z"
}
Expand Down
1 change: 0 additions & 1 deletion ts/views/conversation_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2931,7 +2931,6 @@ Whisper.ConversationView = Whisper.View.extend({

try {
await this.model.sendReactionMessage(reaction, {
targetAuthorE164: messageModel.getSource(),
targetAuthorUuid: messageModel.getSourceUuid(),
targetTimestamp: messageModel.get('sent_at'),
});
Expand Down

0 comments on commit 3f58a9b

Please sign in to comment.