Also delete chain key

This commit is contained in:
Matt Corallo 2014-05-14 03:33:24 -04:00
parent 08093a206d
commit cdebc8afb4

View file

@ -251,7 +251,7 @@ window.crypto = (function() {
var preKeyPair = crypto_storage.getAndRemovePreKeyPair(message.preKeyId); var preKeyPair = crypto_storage.getAndRemovePreKeyPair(message.preKeyId);
if (preKeyPair === undefined) { if (preKeyPair === undefined) {
if (crypto_storage.getSession(encodedNumber) !== undefined) if (crypto_storage.getSession(encodedNumber) !== undefined)
return new Promise(function(resolve) { resolve() }); return Promise.resolve();
else else
throw new Error("Missing preKey for PreKeyWhisperMessage"); throw new Error("Missing preKey for PreKeyWhisperMessage");
} else } else
@ -260,23 +260,26 @@ window.crypto = (function() {
var fillMessageKeys = function(chain, counter) { var fillMessageKeys = function(chain, counter) {
if (chain.chainKey.counter + 1000 < counter) //TODO: maybe 1000 is too low/high in some cases? if (chain.chainKey.counter + 1000 < counter) //TODO: maybe 1000 is too low/high in some cases?
return new Promise(function(resolve) { resolve() }); // Stalker, much? return Promise.resolve(); // Stalker, much?
if (chain.chainKey.counter < counter) { if (chain.chainKey.counter >= counter)
var key = toArrayBuffer(chain.chainKey.key); return Promise.resolve(); // Already calculated
var byteArray = new Uint8Array(1);
byteArray[0] = 1; if (chain.chainKey.key === undefined)
return HmacSHA256(key, byteArray.buffer).then(function(mac) { throw new Error("Got invalid request to extend chain after it was already closed");
byteArray[0] = 2;
return HmacSHA256(key, byteArray.buffer).then(function(key) { var key = toArrayBuffer(chain.chainKey.key);
chain.messageKeys[chain.chainKey.counter + 1] = mac; var byteArray = new Uint8Array(1);
chain.chainKey.key = key byteArray[0] = 1;
chain.chainKey.counter += 1; return HmacSHA256(key, byteArray.buffer).then(function(mac) {
return fillMessageKeys(chain, counter); byteArray[0] = 2;
}); return HmacSHA256(key, byteArray.buffer).then(function(key) {
chain.messageKeys[chain.chainKey.counter + 1] = mac;
chain.chainKey.key = key
chain.chainKey.counter += 1;
return fillMessageKeys(chain, counter);
}); });
} else });
return new Promise(function(resolve) { resolve() });
} }
var removeOldChains = function(session) { var removeOldChains = function(session) {
@ -300,7 +303,7 @@ window.crypto = (function() {
var maybeStepRatchet = function(session, remoteKey, previousCounter) { var maybeStepRatchet = function(session, remoteKey, previousCounter) {
if (session[getString(remoteKey)] !== undefined) if (session[getString(remoteKey)] !== undefined)
return new Promise(function(resolve) { resolve() }); return Promise.resolve();
var ratchet = session.currentRatchet; var ratchet = session.currentRatchet;
@ -328,6 +331,7 @@ window.crypto = (function() {
var previousRatchet = session[getString(ratchet.lastRemoteEphemeralKey)]; var previousRatchet = session[getString(ratchet.lastRemoteEphemeralKey)];
if (previousRatchet !== undefined) { if (previousRatchet !== undefined) {
return fillMessageKeys(previousRatchet, previousCounter).then(function() { return fillMessageKeys(previousRatchet, previousCounter).then(function() {
delete previousRatchet.chainKey['key'];
if (!objectContainsKeys(previousRatchet.messageKeys)) if (!objectContainsKeys(previousRatchet.messageKeys))
delete session[getString(ratchet.lastRemoteEphemeralKey)]; delete session[getString(ratchet.lastRemoteEphemeralKey)];
else else