Add entirely untested secondary device init
This commit is contained in:
parent
379115d902
commit
a6b0d1f84b
9 changed files with 425 additions and 198 deletions
98
js/api.js
98
js/api.js
|
@ -26,6 +26,7 @@ window.textsecure.api = function() {
|
|||
************************************************/
|
||||
// Staging server
|
||||
var URL_BASE = "https://textsecure-service-ca.whispersystems.org:4433";
|
||||
self.relay = "textsecure-service-staging.whispersystems.org";
|
||||
|
||||
// This is the real server
|
||||
//var URL_BASE = "https://textsecure-service.whispersystems.org";
|
||||
|
@ -35,6 +36,7 @@ window.textsecure.api = function() {
|
|||
URL_CALLS['devices'] = "/v1/devices";
|
||||
URL_CALLS['keys'] = "/v2/keys";
|
||||
URL_CALLS['push'] = "/v1/websocket";
|
||||
URL_CALLS['temp_push'] = "/v1/temp_websocket";
|
||||
URL_CALLS['messages'] = "/v1/messages";
|
||||
URL_CALLS['attachment'] = "/v1/attachments";
|
||||
|
||||
|
@ -279,15 +281,93 @@ window.textsecure.api = function() {
|
|||
});
|
||||
};
|
||||
|
||||
self.getWebsocket = function() {
|
||||
var user = textsecure.storage.getUnencrypted("number_id");
|
||||
var password = textsecure.storage.getEncrypted("password");
|
||||
var URL = URL_BASE.replace(/^http/g, 'ws') + URL_CALLS['push'] + '/?';
|
||||
var params = $.param({
|
||||
login: '+' + getString(user).substring(1),
|
||||
password: getString(password)
|
||||
});
|
||||
return new WebSocket(URL+params);
|
||||
var getWebsocket = function(url, auth, reconnectTimeout) {
|
||||
var URL = URL_BASE.replace(/^http/g, 'ws') + url + '/?';
|
||||
if (auth) {
|
||||
var user = textsecure.storage.getUnencrypted("number_id");
|
||||
var password = textsecure.storage.getEncrypted("password");
|
||||
var params = $.param({
|
||||
login: '+' + getString(user).substring(1),
|
||||
password: getString(password)
|
||||
});
|
||||
} else
|
||||
var params = $.param({});
|
||||
|
||||
var reconnectSemaphore = 0;
|
||||
var pingInterval;
|
||||
|
||||
var socketWrapper = { onmessage: function() {}, ondisconnect: function() {}, onconnect: function() {} };
|
||||
|
||||
var connect = function() {
|
||||
reconnectSemaphore++;
|
||||
if (reconnectSemaphore <= 0)
|
||||
return;
|
||||
|
||||
var socket = new WebSocket(URL+params);
|
||||
|
||||
socket.onerror = function(socketEvent) {
|
||||
console.log('Server is down :(');
|
||||
clearInterval(pingInterval);
|
||||
reconnectSemaphore--;
|
||||
setTimeout(function() { connect(); }, reconnectTimeout);
|
||||
socketWrapper.ondisconnect();
|
||||
};
|
||||
|
||||
socket.onclose = function(socketEvent) {
|
||||
console.log('Server closed :(');
|
||||
clearInterval(pingInterval);
|
||||
reconnectSemaphore--;
|
||||
setTimeout(function() { connect(); }, reconnectTimeout);
|
||||
socketWrapper.ondisconnect();
|
||||
};
|
||||
|
||||
socket.onopen = function(socketEvent) {
|
||||
console.log('Connected to server!');
|
||||
pingInterval = setInterval(function() {
|
||||
console.log("Sending server ping message.");
|
||||
if (socket.readyState == socket.CLOSED || socket.readyState == socket.CLOSING) {
|
||||
socket.close();
|
||||
socket.onclose();
|
||||
} else
|
||||
socket.send(JSON.stringify({type: 2}));
|
||||
}, reconnectTimeout / 2);
|
||||
socketWrapper.onconnect();
|
||||
};
|
||||
|
||||
//TODO: wrap onmessage so that we reconnect on missing pong
|
||||
socket.onmessage = function(response) {
|
||||
try {
|
||||
var message = JSON.parse(response.data);
|
||||
} catch (e) {
|
||||
console.log('Error parsing server JSON message: ' + response);
|
||||
return;
|
||||
}
|
||||
|
||||
if (message.type == 3)
|
||||
console.log("Got pong message");
|
||||
else if ((message.type === undefined && message.id !== undefined) || message.type === 4)
|
||||
socketWrapper.onmessage(message);
|
||||
else
|
||||
console.log("Got invalid message from server: " + message);
|
||||
};
|
||||
};
|
||||
connect();
|
||||
|
||||
return socketWrapper;
|
||||
}
|
||||
|
||||
self.getMessageWebsocket = function() {
|
||||
return getWebsocket(URL_CALLS['push'], true, 60000);
|
||||
}
|
||||
|
||||
self.getTempWebsocket = function() {
|
||||
//XXX
|
||||
var socketWrapper = { onmessage: function() {}, ondisconnect: function() {}, onconnect: function() {} };
|
||||
setTimeout(function() {
|
||||
socketWrapper.onmessage({type: 4, message: "404-42-magic"});
|
||||
}, 1000);
|
||||
return socketWrapper;
|
||||
//return getWebsocket(URL_CALLS['temp_push'], false, 5000);
|
||||
}
|
||||
|
||||
return self;
|
||||
|
|
51
js/crypto.js
51
js/crypto.js
|
@ -110,9 +110,13 @@ window.textsecure.crypto = function() {
|
|||
***************************/
|
||||
var crypto_storage = {};
|
||||
|
||||
crypto_storage.putKeyPair = function(keyName, keyPair) {
|
||||
textsecure.storage.putEncrypted("25519Key" + keyName, keyPair);
|
||||
}
|
||||
|
||||
crypto_storage.getNewStoredKeyPair = function(keyName, isIdentity) {
|
||||
return createNewKeyPair(isIdentity).then(function(keyPair) {
|
||||
textsecure.storage.putEncrypted("25519Key" + keyName, keyPair);
|
||||
crypto_storage.putKeyPair(keyName, keyPair);
|
||||
return keyPair;
|
||||
});
|
||||
}
|
||||
|
@ -736,10 +740,10 @@ window.textsecure.crypto = function() {
|
|||
|
||||
var iv = decodedMessage.slice(1, 1 + 16);
|
||||
var ciphertext = decodedMessage.slice(1 + 16, decodedMessage.byteLength - 10);
|
||||
var ivAndCiphertext = decodedMessage.slice(1, decodedMessage.byteLength - 10);
|
||||
var ivAndCiphertext = decodedMessage.slice(0, decodedMessage.byteLength - 10);
|
||||
var mac = decodedMessage.slice(decodedMessage.byteLength - 10, decodedMessage.byteLength);
|
||||
|
||||
return verifyMACWithVersionByte(ivAndCiphertext, mac_key, mac).then(function() {
|
||||
return verifyMAC(ivAndCiphertext, mac_key, mac).then(function() {
|
||||
return window.crypto.subtle.decrypt({name: "AES-CBC", iv: iv}, aes_key, ciphertext);
|
||||
});
|
||||
};
|
||||
|
@ -935,6 +939,47 @@ window.textsecure.crypto = function() {
|
|||
|
||||
self.Ed25519Verify = Ed25519Verify;
|
||||
|
||||
self.prepareTempWebsocket = function() {
|
||||
var socketInfo = {};
|
||||
var keyPair;
|
||||
|
||||
socketInfo.decryptAndHandleDeviceInit = function(deviceInit) {
|
||||
var masterEphemeral = toArrayBuffer(deviceInit.masterEphemeralPubKey);
|
||||
var message = toArrayBuffer(deviceInit.identityKeyMessage);
|
||||
|
||||
return ECDHE(masterEphemeral, keyPair.privKey).then(function(ecRes) {
|
||||
return HKDF(ecRes, masterEphemeral, "WhisperDeviceInit").then(function(keys) {
|
||||
if (new Uint8Array(message)[0] != (3 << 4) | 3)
|
||||
throw new Error("Bad version number on IdentityKeyMessage");
|
||||
|
||||
var iv = message.slice(1, 16 + 1);
|
||||
var mac = message.slice(message.length - 32, message.length);
|
||||
var ivAndCiphertext = message.slice(0, message.length - 32);
|
||||
var ciphertext = message.slice(16 + 1, message.length - 32);
|
||||
|
||||
return verifyMAC(ivAndCiphertext, ecRes[1], mac).then(function() {
|
||||
window.crypto.subtle.decrypt({name: "AES-CBC", iv: iv}, ecRes[0], ciphertext).then(function(plaintext) {
|
||||
var identityKeyMsg = textsecure.protos.decodeIdentityKeyProtobuf(getString(plaintext));
|
||||
|
||||
privToPub(toArrayBuffer(identityKeyMsg.identityKey)).then(function(identityKeyPair) {
|
||||
crypto_storage.putKeyPair("identityKey", identityKeyPair);
|
||||
identityKeyMsg.identityKey = null;
|
||||
|
||||
return identityKeyMsg;
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return createNewKeyPair(false).then(function(newKeyPair) {
|
||||
keyPair = newKeyPair;
|
||||
socketInfo.pubKey = keyPair.pubKey;
|
||||
return socketInfo;
|
||||
});
|
||||
}
|
||||
|
||||
self.testing_only = testing_only;
|
||||
return self;
|
||||
}();
|
||||
|
|
307
js/helpers.js
307
js/helpers.js
|
@ -195,9 +195,19 @@ window.textsecure.protos = function() {
|
|||
return self.PreKeyWhisperMessageProtobuf.decode(btoa(string));
|
||||
}
|
||||
|
||||
self.KeyExchangeMessageProtobuf = dcodeIO.ProtoBuf.loadProtoFile("protos/WhisperTextProtocol.proto").build("textsecure.KeyExchangeMessage");
|
||||
self.decodeKeyExchangeMessageProtobuf = function(string) {
|
||||
return self.KeyExchangeMessageProtobuf.decode(btoa(string));
|
||||
self.DeviceInitProtobuf = dcodeIO.ProtoBuf.loadProtoFile("protos/DeviceMessages.proto").build("textsecure.DeviceInit");
|
||||
self.decodeDeviceInitProtobuf = function(string) {
|
||||
return self.DeviceInitProtobuf.decode(btoa(string));
|
||||
}
|
||||
|
||||
self.IdentityKeyProtobuf = dcodeIO.ProtoBuf.loadProtoFile("protos/DeviceMessages.proto").build("textsecure.IdentityKey");
|
||||
self.decodeIdentityKeyProtobuf = function(string) {
|
||||
return self.IdentityKeyProtobuf.decode(btoa(string));
|
||||
}
|
||||
|
||||
self.DeviceControlProtobuf = dcodeIO.ProtoBuf.loadProtoFile("protos/DeviceMessages.proto").build("textsecure.DeviceControl");
|
||||
self.decodeDeviceControlProtobuf = function(string) {
|
||||
return self.DeviceControlProtobuf.decode(btoa(string));
|
||||
}
|
||||
|
||||
return self;
|
||||
|
@ -635,154 +645,109 @@ window.textsecure.replay = function() {
|
|||
}();
|
||||
|
||||
// message_callback({message: decryptedMessage, pushMessage: server-providedPushMessage})
|
||||
window.textsecure.subscribeToPush = function() {
|
||||
var subscribeToPushMessageSemaphore = 0;
|
||||
return function(message_callback) {
|
||||
subscribeToPushMessageSemaphore++;
|
||||
if (subscribeToPushMessageSemaphore <= 0)
|
||||
return;
|
||||
window.textsecure.subscribeToPush = function(message_callback) {
|
||||
var socket = textsecure.api.getMessageWebsocket();
|
||||
|
||||
var socket = textsecure.api.getWebsocket();
|
||||
socket.onmessage = function(message) {
|
||||
textsecure.crypto.decryptWebsocketMessage(message.message).then(function(plaintext) {
|
||||
var proto = textsecure.protos.decodeIncomingPushMessageProtobuf(getString(plaintext));
|
||||
// After this point, a) decoding errors are not the server's fault, and
|
||||
// b) we should handle them gracefully and tell the user they received an invalid message
|
||||
console.log("Successfully decoded message with id: " + message.id);
|
||||
socket.send(JSON.stringify({type: 1, id: message.id}));
|
||||
return textsecure.crypto.handleIncomingPushMessageProto(proto).then(function(decrypted) {
|
||||
// Now that its decrypted, validate the message and clean it up for consumer processing
|
||||
// Note that messages may (generally) only perform one action and we ignore remaining fields
|
||||
// after the first action.
|
||||
|
||||
var pingInterval;
|
||||
if (decrypted.flags == null)
|
||||
decrypted.flags = 0;
|
||||
|
||||
//TODO: GUI
|
||||
socket.onerror = function(socketEvent) {
|
||||
console.log('Server is down :(');
|
||||
clearInterval(pingInterval);
|
||||
subscribeToPushMessageSemaphore--;
|
||||
setTimeout(function() { textsecure.subscribeToPush(message_callback); }, 60000);
|
||||
};
|
||||
socket.onclose = function(socketEvent) {
|
||||
console.log('Server closed :(');
|
||||
clearInterval(pingInterval);
|
||||
subscribeToPushMessageSemaphore--;
|
||||
setTimeout(function() { textsecure.subscribeToPush(message_callback); }, 60000);
|
||||
};
|
||||
socket.onopen = function(socketEvent) {
|
||||
console.log('Connected to server!');
|
||||
pingInterval = setInterval(function() {
|
||||
console.log("Sending server ping message.");
|
||||
if (socket.readyState == socket.CLOSED || socket.readyState == socket.CLOSING) {
|
||||
socket.close();
|
||||
socket.onclose();
|
||||
} else
|
||||
socket.send(JSON.stringify({type: 2}));
|
||||
}, 30000);
|
||||
};
|
||||
if ((decrypted.flags & textsecure.protos.PushMessageContentProtobuf.Flags.END_SESSION)
|
||||
== textsecure.protos.PushMessageContentProtobuf.Flags.END_SESSION)
|
||||
return;
|
||||
if (decrypted.flags != 0)
|
||||
throw new Error("Unknown flags in message");
|
||||
|
||||
socket.onmessage = function(response) {
|
||||
try {
|
||||
var message = JSON.parse(response.data);
|
||||
} catch (e) {
|
||||
console.log('Error parsing server JSON message: ' + response.responseBody.split("|")[1]);
|
||||
return;
|
||||
}
|
||||
|
||||
if (message.type == 3) {
|
||||
console.log("Got pong message");
|
||||
} else if (message.type === undefined && message.id !== undefined) {
|
||||
textsecure.crypto.decryptWebsocketMessage(message.message).then(function(plaintext) {
|
||||
var proto = textsecure.protos.decodeIncomingPushMessageProtobuf(getString(plaintext));
|
||||
// After this point, a) decoding errors are not the server's fault, and
|
||||
// b) we should handle them gracefully and tell the user they received an invalid message
|
||||
console.log("Successfully decoded message with id: " + message.id);
|
||||
socket.send(JSON.stringify({type: 1, id: message.id}));
|
||||
return textsecure.crypto.handleIncomingPushMessageProto(proto).then(function(decrypted) {
|
||||
// Now that its decrypted, validate the message and clean it up for consumer processing
|
||||
// Note that messages may (generally) only perform one action and we ignore remaining fields
|
||||
// after the first action.
|
||||
|
||||
if (decrypted.flags == null)
|
||||
decrypted.flags = 0;
|
||||
|
||||
if ((decrypted.flags & textsecure.protos.PushMessageContentProtobuf.Flags.END_SESSION)
|
||||
== textsecure.protos.PushMessageContentProtobuf.Flags.END_SESSION)
|
||||
return;
|
||||
if (decrypted.flags != 0)
|
||||
throw new Error("Unknown flags in message");
|
||||
|
||||
var handleAttachment = function(attachment) {
|
||||
return textsecure.api.getAttachment(attachment.id).then(function(encryptedBin) {
|
||||
return textsecure.crypto.decryptAttachment(encryptedBin, toArrayBuffer(attachment.key)).then(function(decryptedBin) {
|
||||
attachment.decrypted = decryptedBin;
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
var promises = [];
|
||||
|
||||
if (decrypted.group !== null) {
|
||||
var existingGroup = textsecure.storage.groups.getNumbers(decrypted.group.id);
|
||||
if (existingGroup === undefined) {
|
||||
if (decrypted.group.type != textsecure.protos.PushMessageContentProtobuf.GroupContext.UPDATE)
|
||||
throw new Error("Got message for unknown group");
|
||||
textsecure.storage.groups.createNewGroup(decrypted.group.members, decrypted.group.id);
|
||||
} else {
|
||||
var fromIndex = existingGroup.indexOf(proto.source);
|
||||
|
||||
if (fromIndex < 0) //TODO: This could be indication of a race...
|
||||
throw new Error("Sender was not a member of the group they were sending from");
|
||||
|
||||
switch(decrypted.group.type) {
|
||||
case textsecure.protos.PushMessageContentProtobuf.GroupContext.UPDATE:
|
||||
if (decrypted.group.avatar !== null)
|
||||
promises.push(handleAttachment(decrypted.group.avatar));
|
||||
|
||||
if (existingGroup.filter(function(number) { decrypted.group.members.indexOf(number) < 0 }).length != 0)
|
||||
throw new Error("Attempted to remove numbers from group with an UPDATE");
|
||||
decrypted.group.added = decrypted.group.members.filter(function(number) { return existingGroup.indexOf(number) < 0; });
|
||||
|
||||
var newGroup = textsecure.storage.groups.addNumbers(decrypted.group.id, decrypted.group.added);
|
||||
if (newGroup.length != decrypted.group.members.length ||
|
||||
newGroup.filter(function(number) { return decrypted.group.members.indexOf(number) < 0; }).length != 0)
|
||||
throw new Error("Error calculating group member difference");
|
||||
|
||||
//TODO: Also follow this path if avatar + name haven't changed (ie we should start storing those)
|
||||
if (decrypted.group.avatar === null && decrypted.group.added.length == 0 && decrypted.group.name === null)
|
||||
return;
|
||||
|
||||
//TODO: Strictly verify all numbers (ie dont let verifyNumber do any user-magic tweaking)
|
||||
|
||||
decrypted.body = null;
|
||||
decrypted.attachments = [];
|
||||
|
||||
break;
|
||||
case textsecure.protos.PushMessageContentProtobuf.GroupContext.QUIT:
|
||||
textsecure.storage.groups.removeNumber(decrypted.group.id, proto.source);
|
||||
|
||||
decrypted.body = null;
|
||||
decrypted.attachments = [];
|
||||
case textsecure.protos.PushMessageContentProtobuf.GroupContext.DELIVER:
|
||||
decrypted.group.name = null;
|
||||
decrypted.group.members = [];
|
||||
decrypted.group.avatar = null;
|
||||
|
||||
break;
|
||||
default:
|
||||
throw new Error("Unknown group message type");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var i in decrypted.attachments)
|
||||
promises.push(handleAttachment(decrypted.attachments[i]));
|
||||
return Promise.all(promises).then(function() {
|
||||
message_callback({pushMessage: proto, message: decrypted});
|
||||
var handleAttachment = function(attachment) {
|
||||
return textsecure.api.getAttachment(attachment.id).then(function(encryptedBin) {
|
||||
return textsecure.crypto.decryptAttachment(encryptedBin, toArrayBuffer(attachment.key)).then(function(decryptedBin) {
|
||||
attachment.decrypted = decryptedBin;
|
||||
});
|
||||
})
|
||||
}).catch(function(e) {
|
||||
// TODO: Show "Invalid message" messages?
|
||||
console.log("Error handling incoming message: ");
|
||||
console.log(e);
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
}();
|
||||
});
|
||||
};
|
||||
|
||||
window.textsecure.register = function() {
|
||||
return function(number, verificationCode, singleDevice, stepDone) {
|
||||
var promises = [];
|
||||
|
||||
if (decrypted.group !== null) {
|
||||
var existingGroup = textsecure.storage.groups.getNumbers(decrypted.group.id);
|
||||
if (existingGroup === undefined) {
|
||||
if (decrypted.group.type != textsecure.protos.PushMessageContentProtobuf.GroupContext.UPDATE)
|
||||
throw new Error("Got message for unknown group");
|
||||
textsecure.storage.groups.createNewGroup(decrypted.group.members, decrypted.group.id);
|
||||
} else {
|
||||
var fromIndex = existingGroup.indexOf(proto.source);
|
||||
|
||||
if (fromIndex < 0) //TODO: This could be indication of a race...
|
||||
throw new Error("Sender was not a member of the group they were sending from");
|
||||
|
||||
switch(decrypted.group.type) {
|
||||
case textsecure.protos.PushMessageContentProtobuf.GroupContext.UPDATE:
|
||||
if (decrypted.group.avatar !== null)
|
||||
promises.push(handleAttachment(decrypted.group.avatar));
|
||||
|
||||
if (existingGroup.filter(function(number) { decrypted.group.members.indexOf(number) < 0 }).length != 0)
|
||||
throw new Error("Attempted to remove numbers from group with an UPDATE");
|
||||
decrypted.group.added = decrypted.group.members.filter(function(number) { return existingGroup.indexOf(number) < 0; });
|
||||
|
||||
var newGroup = textsecure.storage.groups.addNumbers(decrypted.group.id, decrypted.group.added);
|
||||
if (newGroup.length != decrypted.group.members.length ||
|
||||
newGroup.filter(function(number) { return decrypted.group.members.indexOf(number) < 0; }).length != 0)
|
||||
throw new Error("Error calculating group member difference");
|
||||
|
||||
//TODO: Also follow this path if avatar + name haven't changed (ie we should start storing those)
|
||||
if (decrypted.group.avatar === null && decrypted.group.added.length == 0 && decrypted.group.name === null)
|
||||
return;
|
||||
|
||||
//TODO: Strictly verify all numbers (ie dont let verifyNumber do any user-magic tweaking)
|
||||
|
||||
decrypted.body = null;
|
||||
decrypted.attachments = [];
|
||||
|
||||
break;
|
||||
case textsecure.protos.PushMessageContentProtobuf.GroupContext.QUIT:
|
||||
textsecure.storage.groups.removeNumber(decrypted.group.id, proto.source);
|
||||
|
||||
decrypted.body = null;
|
||||
decrypted.attachments = [];
|
||||
case textsecure.protos.PushMessageContentProtobuf.GroupContext.DELIVER:
|
||||
decrypted.group.name = null;
|
||||
decrypted.group.members = [];
|
||||
decrypted.group.avatar = null;
|
||||
|
||||
break;
|
||||
default:
|
||||
throw new Error("Unknown group message type");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var i in decrypted.attachments)
|
||||
promises.push(handleAttachment(decrypted.attachments[i]));
|
||||
return Promise.all(promises).then(function() {
|
||||
message_callback({pushMessage: proto, message: decrypted});
|
||||
});
|
||||
})
|
||||
}).catch(function(e) {
|
||||
// TODO: Show "Invalid message" messages?
|
||||
console.log("Error handling incoming message: ");
|
||||
console.log(e);
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
window.textsecure.registerSingleDevice = function() {
|
||||
return function(number, verificationCode, stepDone) {
|
||||
var signalingKey = textsecure.crypto.getRandomBytes(32 + 20);
|
||||
textsecure.storage.putEncrypted('signaling_key', signalingKey);
|
||||
|
||||
|
@ -794,25 +759,55 @@ window.textsecure.register = function() {
|
|||
registrationId = registrationId & 0x3fff;
|
||||
textsecure.storage.putUnencrypted("registrationId", registrationId);
|
||||
|
||||
return textsecure.api.confirmCode(number, verificationCode, password, signalingKey, registrationId, singleDevice).then(function(response) {
|
||||
if (singleDevice)
|
||||
response = 1;
|
||||
var numberId = number + "." + response;
|
||||
return textsecure.api.confirmCode(number, verificationCode, password, signalingKey, registrationId, true).then(function() {
|
||||
var numberId = number + ".1";
|
||||
textsecure.storage.putUnencrypted("number_id", numberId);
|
||||
textsecure.storage.putUnencrypted("regionCode", textsecure.utils.getRegionCodeForNumber(number));
|
||||
stepDone(1);
|
||||
|
||||
if (!singleDevice) {
|
||||
//TODO: Do things???
|
||||
stepDone(2);
|
||||
}
|
||||
|
||||
return textsecure.crypto.generateKeys().then(function(keys) {
|
||||
stepDone(3);
|
||||
stepDone(2);
|
||||
return textsecure.api.registerKeys(keys).then(function() {
|
||||
stepDone(4);
|
||||
stepDone(3);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}();
|
||||
|
||||
window.textsecure.registerSecondDevice = function(encodedDeviceInit, cryptoInfo, stepDone) {
|
||||
var deviceInit = textsecure.protos.decodeDeviceInit(encodedDeviceInit);
|
||||
return cryptoInfo.decryptAndHandleDeviceInit(deviceInit).then(function(identityKey) {
|
||||
if (identityKey.server != textsecure.api.relay)
|
||||
throw new Error("Unknown relay used by master");
|
||||
var number = identityKey.phoneNumber;
|
||||
|
||||
stepDone(1);
|
||||
|
||||
var signalingKey = textsecure.crypto.getRandomBytes(32 + 20);
|
||||
textsecure.storage.putEncrypted('signaling_key', signalingKey);
|
||||
|
||||
var password = btoa(getString(textsecure.crypto.getRandomBytes(16)));
|
||||
password = password.substring(0, password.length - 2);
|
||||
textsecure.storage.putEncrypted("password", password);
|
||||
|
||||
var registrationId = new Uint16Array(textsecure.crypto.getRandomBytes(2))[0];
|
||||
registrationId = registrationId & 0x3fff;
|
||||
textsecure.storage.putUnencrypted("registrationId", registrationId);
|
||||
|
||||
return textsecure.api.confirmCode(number, identityKey.provisioningCode, password, signalingKey, registrationId, false).then(function(result) {
|
||||
var numberId = number + "." + result;
|
||||
textsecure.storage.putUnencrypted("number_id", numberId);
|
||||
textsecure.storage.putUnencrypted("regionCode", textsecure.utils.getRegionCodeForNumber(number));
|
||||
stepDone(2);
|
||||
|
||||
return textsecure.crypto.generateKeys().then(function(keys) {
|
||||
stepDone(3);
|
||||
return textsecure.api.registerKeys(keys).then(function() {
|
||||
stepDone(4);
|
||||
//TODO: Send DeviceControl.NEW_DEVICE_REGISTERED to all other devices
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
|
|
@ -73,26 +73,23 @@ $('#init-go').click(function() {
|
|||
return;
|
||||
}
|
||||
|
||||
|
||||
$('#init-setup').hide();
|
||||
$('#verify1done').text('');
|
||||
$('#verify2').hide();
|
||||
$('#verify1').hide();
|
||||
$('#verify2done').text('');
|
||||
$('#verify3done').text('');
|
||||
$('#verify4done').text('');
|
||||
$('#verify5').hide();
|
||||
$('#verify').show();
|
||||
|
||||
textsecure.register(parsedNumber, $('#code').val(), single_device, function(step) {
|
||||
textsecure.registerSingleDevice(parsedNumber, $('#code').val(), function(step) {
|
||||
switch(step) {
|
||||
case 1:
|
||||
$('#verify1done').text('done');
|
||||
break;
|
||||
case 2:
|
||||
$('#verify2done').text('done');
|
||||
break;
|
||||
case 3:
|
||||
case 2:
|
||||
$('#verify3done').text('done');
|
||||
break;
|
||||
case 4:
|
||||
case 3:
|
||||
$('#complete-number').text(parsedNumber);
|
||||
$('#verify').hide();
|
||||
$('#setup-complete').show();
|
||||
|
@ -136,6 +133,61 @@ textsecure.registerOnLoadFunction(function() {
|
|||
$('#regionCode').val(textsecure.utils.getRegionCodeForCountryCode($('#countrycode').val()));
|
||||
updateNumberColors();
|
||||
|
||||
textsecure.crypto.prepareTempWebsocket().then(function(cryptoInfo) {
|
||||
var qrCode = new QRCode(document.getElementById('setup-qr'));
|
||||
var socket = textsecure.api.getTempWebsocket();
|
||||
|
||||
socket.onmessage = function(message) {
|
||||
//TODO: Get a server format for this
|
||||
if (message.type === 4) {
|
||||
qrCode.makeCode('textsecure-device-init:/' +
|
||||
'?channel_uuid=' + message.message +
|
||||
'&channel_server=' + textsecure.api.relay +
|
||||
'&publicKey=' + btoa(getString(cryptoInfo.publicKey)));
|
||||
$('img').removeAttr('style');
|
||||
$('#left-connecting').hide();
|
||||
$('#left-setup').show();
|
||||
$('#left-reconnecting').hide();
|
||||
} else {
|
||||
$('#init-setup').hide();
|
||||
$('#verify1done').text('');
|
||||
$('#verify2done').text('');
|
||||
$('#verify3done').text('');
|
||||
$('#verify4done').text('');
|
||||
$('#verify5done').text('');
|
||||
$('#verify').show();
|
||||
|
||||
|
||||
textsecure.registerSecondDevice(cryptoInfo, message.message, function(step) {
|
||||
switch(step) {
|
||||
case 1:
|
||||
$('#verify1done').text('done');
|
||||
break;
|
||||
case 2:
|
||||
$('#verify2done').text('done');
|
||||
break;
|
||||
case 3:
|
||||
$('#verify3done').text('done');
|
||||
break;
|
||||
case 4:
|
||||
//TODO: User needs to verify number before we continue
|
||||
$('#complete-number').text(parsedNumber);
|
||||
$('#verify4done').text('done');
|
||||
case 5:
|
||||
$('#verify').hide();
|
||||
$('#setup-complete').show();
|
||||
registrationDone();
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
socket.ondisconnect = function() {
|
||||
$('#left-connecting').hide();
|
||||
$('#left-setup').hide();
|
||||
$('#left-reconnecting').show();
|
||||
};
|
||||
});
|
||||
} else {
|
||||
$('#complete-number').text(textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0]);//TODO: no
|
||||
$('#setup-complete').show();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue