forked from signalapp/Signal-Desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaxolotl_wrapper.js
96 lines (89 loc) · 4.61 KB
/
axolotl_wrapper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//TODO: Remove almost everything here...
'use strict';
;(function() {
window.axolotl = window.axolotl || {};
window.axolotl.api = {
getMyIdentifier: function() {
return textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0];
},
getMyRegistrationId: function() {
return textsecure.storage.getUnencrypted("registrationId");
},
isIdentifierSane: function(identifier) {
return textsecure.utils.isNumberSane(identifier);
},
storage: {
put: function(key, value) {
return textsecure.storage.putEncrypted(key, value);
},
get: function(key, defaultValue) {
return textsecure.storage.getEncrypted(key, defaultValue);
},
remove: function(key) {
return textsecure.storage.removeEncrypted(key);
},
},
updateKeys: function(keys) {
return textsecure.api.registerKeys(keys).catch(function(e) {
//TODO: Notify the user somehow?
console.error(e);
});
},
};
var decodeMessageContents = function(res) {
var finalMessage = textsecure.protobuf.PushMessageContent.decode(res[0]);
//TODO
/*if ((finalMessage.flags & textsecure.protobuf.PushMessageContent.Flags.END_SESSION)
== textsecure.protobuf.PushMessageContent.Flags.END_SESSION)
axolotl.protocol.closeSession(res[1], true);*/
return finalMessage;
}
window.textsecure = window.textsecure || {};
window.textsecure.protocol_wrapper = {
handleIncomingPushMessageProto: function(proto) {
switch(proto.type) {
case textsecure.protobuf.IncomingPushMessageSignal.Type.PLAINTEXT:
return Promise.resolve(textsecure.protobuf.PushMessageContent.decode(proto.message));
case textsecure.protobuf.IncomingPushMessageSignal.Type.CIPHERTEXT:
var from = proto.source + "." + (proto.sourceDevice == null ? 0 : proto.sourceDevice);
return axolotl.protocol.decryptWhisperMessage(from, getString(proto.message)).then(decodeMessageContents);
case textsecure.protobuf.IncomingPushMessageSignal.Type.PREKEY_BUNDLE:
if (proto.message.readUint8() != ((3 << 4) | 3))
throw new Error("Bad version byte");
var from = proto.source + "." + (proto.sourceDevice == null ? 0 : proto.sourceDevice);
return axolotl.protocol.handlePreKeyWhisperMessage(from, getString(proto.message)).then(decodeMessageContents);
case textsecure.protobuf.IncomingPushMessageSignal.Type.RECEIPT:
return Promise.resolve(null);
case textsecure.protobuf.IncomingPushMessageSignal.Type.PREKEY_BUNDLE_DEVICE_CONTROL:
if (proto.message.readUint8() != ((3 << 4) | 3))
throw new Error("Bad version byte");
var from = proto.source + "." + (proto.sourceDevice == null ? 0 : proto.sourceDevice);
return axolotl.protocol.handlePreKeyWhisperMessage(from, getString(proto.message)).then(function(res) {
return textsecure.protobuf.DeviceControl.decode(res[0]);
});
case textsecure.protobuf.IncomingPushMessageSignal.Type.DEVICE_CONTROL:
var from = proto.source + "." + (proto.sourceDevice == null ? 0 : proto.sourceDevice);
return axolotl.protocol.decryptWhisperMessage(from, getString(proto.message)).then(function(res) {
return textsecure.protobuf.DeviceControl.decode(res[0]);
});
default:
return new Promise(function(resolve, reject) { reject(new Error("Unknown message type")); });
}
}
};
var wipeIdentityAndTryMessageAgain = function(from, encodedMessage, message_id) {
// Wipe identity key!
//TODO: Encapsuate with the rest of textsecure.storage.devices
textsecure.storage.removeEncrypted("devices" + from.split('.')[0]);
//TODO: Probably breaks with a devicecontrol message
return axolotl.protocol.handlePreKeyWhisperMessage(from, encodedMessage).then(decodeMessageContents).then(
function(pushMessageContent) {
extension.trigger('message:decrypted', {
message_id : message_id,
data : pushMessageContent
});
}
);
}
textsecure.replay.registerFunction(wipeIdentityAndTryMessageAgain, textsecure.replay.Type.INIT_SESSION);
})();