2015-04-01 20:08:09 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-05-02 16:51:22 +00:00
|
|
|
describe('Key generation', function() {
|
|
|
|
var count = 10;
|
|
|
|
this.timeout(count * 2000);
|
2015-04-01 20:08:09 +00:00
|
|
|
|
2018-05-02 16:51:22 +00:00
|
|
|
function validateStoredKeyPair(keyPair) {
|
|
|
|
/* Ensure the keypair matches the format used internally by libsignal-protocol */
|
|
|
|
assert.isObject(keyPair, 'Stored keyPair is not an object');
|
|
|
|
assert.instanceOf(keyPair.pubKey, ArrayBuffer);
|
|
|
|
assert.instanceOf(keyPair.privKey, ArrayBuffer);
|
|
|
|
assert.strictEqual(keyPair.pubKey.byteLength, 33);
|
|
|
|
assert.strictEqual(new Uint8Array(keyPair.pubKey)[0], 5);
|
|
|
|
assert.strictEqual(keyPair.privKey.byteLength, 32);
|
|
|
|
}
|
|
|
|
function itStoresPreKey(keyId) {
|
2018-05-20 03:53:12 +00:00
|
|
|
it('prekey ' + keyId + ' is valid', function() {
|
2018-05-02 16:51:22 +00:00
|
|
|
return textsecure.storage.protocol
|
|
|
|
.loadPreKey(keyId)
|
|
|
|
.then(function(keyPair) {
|
|
|
|
validateStoredKeyPair(keyPair);
|
2018-05-20 03:53:12 +00:00
|
|
|
});
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
function itStoresSignedPreKey(keyId) {
|
2018-05-20 03:53:12 +00:00
|
|
|
it('signed prekey ' + keyId + ' is valid', function() {
|
2018-05-02 16:51:22 +00:00
|
|
|
return textsecure.storage.protocol
|
|
|
|
.loadSignedPreKey(keyId)
|
|
|
|
.then(function(keyPair) {
|
|
|
|
validateStoredKeyPair(keyPair);
|
2018-05-20 03:53:12 +00:00
|
|
|
});
|
2015-04-01 20:08:09 +00:00
|
|
|
});
|
2018-05-02 16:51:22 +00:00
|
|
|
}
|
|
|
|
function validateResultKey(resultKey) {
|
|
|
|
return textsecure.storage.protocol
|
|
|
|
.loadPreKey(resultKey.keyId)
|
|
|
|
.then(function(keyPair) {
|
|
|
|
assertEqualArrayBuffers(resultKey.publicKey, keyPair.pubKey);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
function validateResultSignedKey(resultSignedKey) {
|
|
|
|
return textsecure.storage.protocol
|
|
|
|
.loadSignedPreKey(resultSignedKey.keyId)
|
|
|
|
.then(function(keyPair) {
|
|
|
|
assertEqualArrayBuffers(resultSignedKey.publicKey, keyPair.pubKey);
|
|
|
|
});
|
|
|
|
}
|
2015-04-01 20:08:09 +00:00
|
|
|
|
2018-05-20 03:53:12 +00:00
|
|
|
before(function() {
|
2018-05-02 16:51:22 +00:00
|
|
|
localStorage.clear();
|
2018-05-20 03:53:12 +00:00
|
|
|
return libsignal.KeyHelper.generateIdentityKeyPair().then(function(
|
|
|
|
keyPair
|
|
|
|
) {
|
|
|
|
return textsecure.storage.protocol.put('identityKey', keyPair);
|
|
|
|
});
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('the first time', function() {
|
|
|
|
var result;
|
|
|
|
/* result should have this format
|
2015-04-01 20:08:09 +00:00
|
|
|
* {
|
|
|
|
* preKeys: [ { keyId, publicKey }, ... ],
|
|
|
|
* signedPreKey: { keyId, publicKey, signature },
|
|
|
|
* identityKey: <ArrayBuffer>
|
|
|
|
* }
|
|
|
|
*/
|
2018-05-20 03:53:12 +00:00
|
|
|
before(function() {
|
2018-05-02 16:51:22 +00:00
|
|
|
var accountManager = new textsecure.AccountManager('');
|
2018-05-20 03:53:12 +00:00
|
|
|
return accountManager.generateKeys(count).then(function(res) {
|
|
|
|
result = res;
|
|
|
|
});
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
|
|
|
for (var i = 1; i <= count; i++) {
|
|
|
|
itStoresPreKey(i);
|
|
|
|
}
|
|
|
|
itStoresSignedPreKey(1);
|
2015-04-01 20:08:09 +00:00
|
|
|
|
2018-05-02 16:51:22 +00:00
|
|
|
it('result contains ' + count + ' preKeys', function() {
|
|
|
|
assert.isArray(result.preKeys);
|
|
|
|
assert.lengthOf(result.preKeys, count);
|
|
|
|
for (var i = 0; i < count; i++) {
|
|
|
|
assert.isObject(result.preKeys[i]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
it('result contains the correct keyIds', function() {
|
|
|
|
for (var i = 0; i < count; i++) {
|
|
|
|
assert.strictEqual(result.preKeys[i].keyId, i + 1);
|
|
|
|
}
|
|
|
|
});
|
2018-05-20 03:53:12 +00:00
|
|
|
it('result contains the correct public keys', function() {
|
|
|
|
return Promise.all(result.preKeys.map(validateResultKey));
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
2018-05-20 03:53:12 +00:00
|
|
|
it('returns a signed prekey', function() {
|
2018-05-02 16:51:22 +00:00
|
|
|
assert.strictEqual(result.signedPreKey.keyId, 1);
|
|
|
|
assert.instanceOf(result.signedPreKey.signature, ArrayBuffer);
|
2018-05-20 03:53:12 +00:00
|
|
|
return validateResultSignedKey(result.signedPreKey);
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('the second time', function() {
|
|
|
|
var result;
|
2018-05-20 03:53:12 +00:00
|
|
|
before(function() {
|
2018-05-02 16:51:22 +00:00
|
|
|
var accountManager = new textsecure.AccountManager('');
|
2018-05-20 03:53:12 +00:00
|
|
|
return accountManager.generateKeys(count).then(function(res) {
|
|
|
|
result = res;
|
|
|
|
});
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
|
|
|
for (var i = 1; i <= 2 * count; i++) {
|
|
|
|
itStoresPreKey(i);
|
|
|
|
}
|
|
|
|
itStoresSignedPreKey(1);
|
|
|
|
itStoresSignedPreKey(2);
|
|
|
|
it('result contains ' + count + ' preKeys', function() {
|
|
|
|
assert.isArray(result.preKeys);
|
|
|
|
assert.lengthOf(result.preKeys, count);
|
|
|
|
for (var i = 0; i < count; i++) {
|
|
|
|
assert.isObject(result.preKeys[i]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
it('result contains the correct keyIds', function() {
|
|
|
|
for (var i = 1; i <= count; i++) {
|
|
|
|
assert.strictEqual(result.preKeys[i - 1].keyId, i + count);
|
|
|
|
}
|
|
|
|
});
|
2018-05-20 03:53:12 +00:00
|
|
|
it('result contains the correct public keys', function() {
|
|
|
|
return Promise.all(result.preKeys.map(validateResultKey));
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
2018-05-20 03:53:12 +00:00
|
|
|
it('returns a signed prekey', function() {
|
2018-05-02 16:51:22 +00:00
|
|
|
assert.strictEqual(result.signedPreKey.keyId, 2);
|
|
|
|
assert.instanceOf(result.signedPreKey.signature, ArrayBuffer);
|
2018-05-20 03:53:12 +00:00
|
|
|
return validateResultSignedKey(result.signedPreKey);
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('the third time', function() {
|
|
|
|
var result;
|
2018-05-20 03:53:12 +00:00
|
|
|
before(function() {
|
2018-05-02 16:51:22 +00:00
|
|
|
var accountManager = new textsecure.AccountManager('');
|
2018-05-20 03:53:12 +00:00
|
|
|
return accountManager.generateKeys(count).then(function(res) {
|
|
|
|
result = res;
|
|
|
|
});
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
|
|
|
for (var i = 1; i <= 3 * count; i++) {
|
|
|
|
itStoresPreKey(i);
|
|
|
|
}
|
|
|
|
itStoresSignedPreKey(2);
|
|
|
|
itStoresSignedPreKey(3);
|
|
|
|
it('result contains ' + count + ' preKeys', function() {
|
|
|
|
assert.isArray(result.preKeys);
|
|
|
|
assert.lengthOf(result.preKeys, count);
|
|
|
|
for (var i = 0; i < count; i++) {
|
|
|
|
assert.isObject(result.preKeys[i]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
it('result contains the correct keyIds', function() {
|
|
|
|
for (var i = 1; i <= count; i++) {
|
|
|
|
assert.strictEqual(result.preKeys[i - 1].keyId, i + 2 * count);
|
|
|
|
}
|
|
|
|
});
|
2018-05-20 03:53:12 +00:00
|
|
|
it('result contains the correct public keys', function() {
|
|
|
|
return Promise.all(result.preKeys.map(validateResultKey));
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
2018-05-20 03:53:12 +00:00
|
|
|
it('result contains a signed prekey', function() {
|
2018-05-02 16:51:22 +00:00
|
|
|
assert.strictEqual(result.signedPreKey.keyId, 3);
|
|
|
|
assert.instanceOf(result.signedPreKey.signature, ArrayBuffer);
|
2018-05-20 03:53:12 +00:00
|
|
|
return validateResultSignedKey(result.signedPreKey);
|
2015-04-01 20:08:09 +00:00
|
|
|
});
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
2015-04-01 20:08:09 +00:00
|
|
|
});
|