Infer pairing message type from the content.

This should fix device pairing cross platform.
pull/589/head
Mikunj 6 years ago
parent aa66b28ffe
commit 589b3f3233

@ -220,10 +220,7 @@
); );
}; };
try { try {
await verify( await verify(requestSignature, PairingType.REQUEST);
requestSignature,
textsecure.protobuf.PairingAuthorisationMessage.Type.REQUEST
);
} catch (e) { } catch (e) {
window.log.warn( window.log.warn(
'Could not verify pairing request authorisation signature. Ignoring message.' 'Could not verify pairing request authorisation signature. Ignoring message.'
@ -233,10 +230,7 @@
} }
if (isGrant) { if (isGrant) {
try { try {
await verify( await verify(grantSignature, PairingType.GRANT);
grantSignature,
textsecure.protobuf.PairingAuthorisationMessage.Type.GRANT
);
} catch (e) { } catch (e) {
window.log.warn( window.log.warn(
'Could not verify pairing grant authorisation signature. Ignoring message.' 'Could not verify pairing grant authorisation signature. Ignoring message.'
@ -265,12 +259,10 @@
// For REQUEST type message, the secondary device signs the primary device pubkey // For REQUEST type message, the secondary device signs the primary device pubkey
// For GRANT type message, the primary device signs the secondary device pubkey // For GRANT type message, the primary device signs the secondary device pubkey
let issuer; let issuer;
if (type === textsecure.protobuf.PairingAuthorisationMessage.Type.GRANT) { if (type === PairingType.GRANT) {
data.set(new Uint8Array(secondaryPubKeyArrayBuffer)); data.set(new Uint8Array(secondaryPubKeyArrayBuffer));
issuer = primaryDevicePubKeyArrayBuffer; issuer = primaryDevicePubKeyArrayBuffer;
} else if ( } else if (type === PairingType.REQUEST) {
type === textsecure.protobuf.PairingAuthorisationMessage.Type.REQUEST
) {
data.set(new Uint8Array(primaryDevicePubKeyArrayBuffer)); data.set(new Uint8Array(primaryDevicePubKeyArrayBuffer));
issuer = secondaryPubKeyArrayBuffer; issuer = secondaryPubKeyArrayBuffer;
} }
@ -301,6 +293,11 @@
const sha512 = data => crypto.subtle.digest('SHA-512', data); const sha512 = data => crypto.subtle.digest('SHA-512', data);
const PairingType = Object.freeze({
REQUEST: 0,
GRANT: 1,
});
window.libloki.crypto = { window.libloki.crypto = {
DHEncrypt, DHEncrypt,
DHDecrypt, DHDecrypt,
@ -311,6 +308,7 @@
generateSignatureForPairing, generateSignatureForPairing,
verifyPairingSignature, verifyPairingSignature,
validateAuthorisation, validateAuthorisation,
PairingType,
// for testing // for testing
_LokiSnodeChannel: LokiSnodeChannel, _LokiSnodeChannel: LokiSnodeChannel,
_decodeSnodeAddressToPubKey: decodeSnodeAddressToPubKey, _decodeSnodeAddressToPubKey: decodeSnodeAddressToPubKey,

@ -567,17 +567,14 @@
if (primaryDevicePubKey === ourPubKey) { if (primaryDevicePubKey === ourPubKey) {
throw new Error('Cannot request to pair with ourselves'); throw new Error('Cannot request to pair with ourselves');
} }
const requestType =
textsecure.protobuf.PairingAuthorisationMessage.Type.REQUEST;
const requestSignature = await libloki.crypto.generateSignatureForPairing( const requestSignature = await libloki.crypto.generateSignatureForPairing(
primaryDevicePubKey, primaryDevicePubKey,
requestType libloki.crypto.PairingType.REQUEST
); );
const authorisation = { const authorisation = {
primaryDevicePubKey, primaryDevicePubKey,
secondaryDevicePubKey: ourPubKey, secondaryDevicePubKey: ourPubKey,
requestSignature, requestSignature,
type: requestType,
}; };
await libloki.api.sendPairingAuthorisation( await libloki.api.sendPairingAuthorisation(
authorisation, authorisation,
@ -599,11 +596,9 @@
secondaryDevicePubKey, secondaryDevicePubKey,
'private' 'private'
); );
const grantType =
textsecure.protobuf.PairingAuthorisationMessage.Type.GRANT;
const grantSignature = await libloki.crypto.generateSignatureForPairing( const grantSignature = await libloki.crypto.generateSignatureForPairing(
secondaryDevicePubKey, secondaryDevicePubKey,
grantType libloki.crypto.PairingType.GRANT
); );
const existingAuthorisation = await libloki.storage.getAuthorisationForSecondaryPubKey( const existingAuthorisation = await libloki.storage.getAuthorisationForSecondaryPubKey(
secondaryDevicePubKey secondaryDevicePubKey
@ -619,7 +614,6 @@
secondaryDevicePubKey, secondaryDevicePubKey,
requestSignature, requestSignature,
grantSignature, grantSignature,
type: grantType,
}; };
// Update authorisation in database with the new grant signature // Update authorisation in database with the new grant signature
await libloki.storage.savePairingAuthorisation(authorisation); await libloki.storage.savePairingAuthorisation(authorisation);

@ -1095,8 +1095,8 @@ MessageReceiver.prototype.extend({
'Received invalid pairing authorisation for self. Could not verify signature. Ignoring.' 'Received invalid pairing authorisation for self. Could not verify signature. Ignoring.'
); );
} else { } else {
const { type, primaryDevicePubKey } = pairingAuthorisation; const { primaryDevicePubKey, grantSignature } = pairingAuthorisation;
if (type === textsecure.protobuf.PairingAuthorisationMessage.Type.GRANT) { if (grantSignature) {
// Authorisation received to become a secondary device // Authorisation received to become a secondary device
window.log.info( window.log.info(
`Received pairing authorisation from ${primaryDevicePubKey}` `Received pairing authorisation from ${primaryDevicePubKey}`
@ -1168,17 +1168,18 @@ MessageReceiver.prototype.extend({
}, },
async handlePairingAuthorisationMessage(envelope, content) { async handlePairingAuthorisationMessage(envelope, content) {
const { pairingAuthorisation } = content; const { pairingAuthorisation } = content;
const { type, secondaryDevicePubKey } = pairingAuthorisation; const { secondaryDevicePubKey, grantSignature } = pairingAuthorisation;
if (type === textsecure.protobuf.PairingAuthorisationMessage.Type.REQUEST) { const isGrant =
return this.handlePairingRequest(envelope, pairingAuthorisation); grantSignature &&
} else if (secondaryDevicePubKey === textsecure.storage.user.getNumber()) { secondaryDevicePubKey === textsecure.storage.user.getNumber();
if (isGrant) {
return this.handleAuthorisationForSelf( return this.handleAuthorisationForSelf(
envelope, envelope,
pairingAuthorisation, pairingAuthorisation,
content content
); );
} }
return this.handleAuthorisationForContact(envelope); return this.handlePairingRequest(envelope, pairingAuthorisation);
}, },
async handleSecondaryDeviceFriendRequest(pubKey, deviceMapping) { async handleSecondaryDeviceFriendRequest(pubKey, deviceMapping) {

@ -50,16 +50,10 @@ message LokiAddressMessage {
} }
message PairingAuthorisationMessage { message PairingAuthorisationMessage {
enum Type {
REQUEST = 1;
GRANT = 2;
REVOKE = 3;
}
optional string primaryDevicePubKey = 1; optional string primaryDevicePubKey = 1;
optional string secondaryDevicePubKey = 2; optional string secondaryDevicePubKey = 2;
optional bytes requestSignature = 3; optional bytes requestSignature = 3;
optional bytes grantSignature = 4; optional bytes grantSignature = 4;
optional Type type = 5;
} }
message PreKeyBundleMessage { message PreKeyBundleMessage {

Loading…
Cancel
Save