2018-11-02 18:02:53 +00:00
|
|
|
/* global libsignal, textsecure */
|
|
|
|
|
|
|
|
describe('Key generation', function thisNeeded() {
|
2018-07-21 21:51:20 +00:00
|
|
|
const count = 10;
|
2018-05-02 16:51:22 +00:00
|
|
|
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-07-21 21:51:20 +00:00
|
|
|
it(`prekey ${keyId} is valid`, () =>
|
|
|
|
textsecure.storage.protocol.loadPreKey(keyId).then(keyPair => {
|
|
|
|
validateStoredKeyPair(keyPair);
|
|
|
|
}));
|
2018-05-02 16:51:22 +00:00
|
|
|
}
|
|
|
|
function itStoresSignedPreKey(keyId) {
|
2018-07-21 21:51:20 +00:00
|
|
|
it(`signed prekey ${keyId} is valid`, () =>
|
|
|
|
textsecure.storage.protocol.loadSignedPreKey(keyId).then(keyPair => {
|
|
|
|
validateStoredKeyPair(keyPair);
|
|
|
|
}));
|
2018-05-02 16:51:22 +00:00
|
|
|
}
|
|
|
|
function validateResultKey(resultKey) {
|
|
|
|
return textsecure.storage.protocol
|
|
|
|
.loadPreKey(resultKey.keyId)
|
2018-07-21 21:51:20 +00:00
|
|
|
.then(keyPair => {
|
2018-05-02 16:51:22 +00:00
|
|
|
assertEqualArrayBuffers(resultKey.publicKey, keyPair.pubKey);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
function validateResultSignedKey(resultSignedKey) {
|
|
|
|
return textsecure.storage.protocol
|
|
|
|
.loadSignedPreKey(resultSignedKey.keyId)
|
2018-07-21 21:51:20 +00:00
|
|
|
.then(keyPair => {
|
2018-05-02 16:51:22 +00:00
|
|
|
assertEqualArrayBuffers(resultSignedKey.publicKey, keyPair.pubKey);
|
|
|
|
});
|
|
|
|
}
|
2015-04-01 20:08:09 +00:00
|
|
|
|
2018-07-21 21:51:20 +00:00
|
|
|
before(() => {
|
2018-05-02 16:51:22 +00:00
|
|
|
localStorage.clear();
|
2018-07-21 21:51:20 +00:00
|
|
|
return libsignal.KeyHelper.generateIdentityKeyPair().then(keyPair =>
|
|
|
|
textsecure.storage.protocol.put('identityKey', keyPair)
|
|
|
|
);
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
|
|
|
|
2018-07-21 21:51:20 +00:00
|
|
|
describe('the first time', () => {
|
|
|
|
let result;
|
2018-05-02 16:51:22 +00:00
|
|
|
/* result should have this format
|
2020-01-08 17:44:54 +00:00
|
|
|
* {
|
|
|
|
* preKeys: [ { keyId, publicKey }, ... ],
|
|
|
|
* signedPreKey: { keyId, publicKey, signature },
|
|
|
|
* identityKey: <ArrayBuffer>
|
|
|
|
* }
|
|
|
|
*/
|
2018-07-21 21:51:20 +00:00
|
|
|
before(() => {
|
|
|
|
const accountManager = new textsecure.AccountManager('');
|
|
|
|
return accountManager.generateKeys(count).then(res => {
|
2018-05-20 03:53:12 +00:00
|
|
|
result = res;
|
|
|
|
});
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
for (let i = 1; i <= count; i += 1) {
|
2018-05-02 16:51:22 +00:00
|
|
|
itStoresPreKey(i);
|
|
|
|
}
|
|
|
|
itStoresSignedPreKey(1);
|
2015-04-01 20:08:09 +00:00
|
|
|
|
2018-07-21 21:51:20 +00:00
|
|
|
it(`result contains ${count} preKeys`, () => {
|
2018-05-02 16:51:22 +00:00
|
|
|
assert.isArray(result.preKeys);
|
|
|
|
assert.lengthOf(result.preKeys, count);
|
2018-11-02 18:02:53 +00:00
|
|
|
for (let i = 0; i < count; i += 1) {
|
2018-05-02 16:51:22 +00:00
|
|
|
assert.isObject(result.preKeys[i]);
|
|
|
|
}
|
|
|
|
});
|
2018-07-21 21:51:20 +00:00
|
|
|
it('result contains the correct keyIds', () => {
|
2018-11-02 18:02:53 +00:00
|
|
|
for (let i = 0; i < count; i += 1) {
|
2018-05-02 16:51:22 +00:00
|
|
|
assert.strictEqual(result.preKeys[i].keyId, i + 1);
|
|
|
|
}
|
|
|
|
});
|
2018-07-21 21:51:20 +00:00
|
|
|
it('result contains the correct public keys', () =>
|
|
|
|
Promise.all(result.preKeys.map(validateResultKey)));
|
|
|
|
it('returns a signed prekey', () => {
|
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
|
|
|
});
|
|
|
|
});
|
2018-07-21 21:51:20 +00:00
|
|
|
describe('the second time', () => {
|
|
|
|
let result;
|
|
|
|
before(() => {
|
|
|
|
const accountManager = new textsecure.AccountManager('');
|
|
|
|
return accountManager.generateKeys(count).then(res => {
|
2018-05-20 03:53:12 +00:00
|
|
|
result = res;
|
|
|
|
});
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
for (let i = 1; i <= 2 * count; i += 1) {
|
2018-05-02 16:51:22 +00:00
|
|
|
itStoresPreKey(i);
|
|
|
|
}
|
|
|
|
itStoresSignedPreKey(1);
|
|
|
|
itStoresSignedPreKey(2);
|
2018-07-21 21:51:20 +00:00
|
|
|
it(`result contains ${count} preKeys`, () => {
|
2018-05-02 16:51:22 +00:00
|
|
|
assert.isArray(result.preKeys);
|
|
|
|
assert.lengthOf(result.preKeys, count);
|
2018-11-02 18:02:53 +00:00
|
|
|
for (let i = 0; i < count; i += 1) {
|
2018-05-02 16:51:22 +00:00
|
|
|
assert.isObject(result.preKeys[i]);
|
|
|
|
}
|
|
|
|
});
|
2018-07-21 21:51:20 +00:00
|
|
|
it('result contains the correct keyIds', () => {
|
2018-11-02 18:02:53 +00:00
|
|
|
for (let i = 1; i <= count; i += 1) {
|
2018-05-02 16:51:22 +00:00
|
|
|
assert.strictEqual(result.preKeys[i - 1].keyId, i + count);
|
|
|
|
}
|
|
|
|
});
|
2018-07-21 21:51:20 +00:00
|
|
|
it('result contains the correct public keys', () =>
|
|
|
|
Promise.all(result.preKeys.map(validateResultKey)));
|
|
|
|
it('returns a signed prekey', () => {
|
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
|
|
|
});
|
|
|
|
});
|
2018-07-21 21:51:20 +00:00
|
|
|
describe('the third time', () => {
|
|
|
|
let result;
|
|
|
|
before(() => {
|
|
|
|
const accountManager = new textsecure.AccountManager('');
|
|
|
|
return accountManager.generateKeys(count).then(res => {
|
2018-05-20 03:53:12 +00:00
|
|
|
result = res;
|
|
|
|
});
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
for (let i = 1; i <= 3 * count; i += 1) {
|
2018-05-02 16:51:22 +00:00
|
|
|
itStoresPreKey(i);
|
|
|
|
}
|
|
|
|
itStoresSignedPreKey(2);
|
|
|
|
itStoresSignedPreKey(3);
|
2018-07-21 21:51:20 +00:00
|
|
|
it(`result contains ${count} preKeys`, () => {
|
2018-05-02 16:51:22 +00:00
|
|
|
assert.isArray(result.preKeys);
|
|
|
|
assert.lengthOf(result.preKeys, count);
|
2018-11-02 18:02:53 +00:00
|
|
|
for (let i = 0; i < count; i += 1) {
|
2018-05-02 16:51:22 +00:00
|
|
|
assert.isObject(result.preKeys[i]);
|
|
|
|
}
|
|
|
|
});
|
2018-07-21 21:51:20 +00:00
|
|
|
it('result contains the correct keyIds', () => {
|
2018-11-02 18:02:53 +00:00
|
|
|
for (let i = 1; i <= count; i += 1) {
|
2018-05-02 16:51:22 +00:00
|
|
|
assert.strictEqual(result.preKeys[i - 1].keyId, i + 2 * count);
|
|
|
|
}
|
|
|
|
});
|
2018-07-21 21:51:20 +00:00
|
|
|
it('result contains the correct public keys', () =>
|
|
|
|
Promise.all(result.preKeys.map(validateResultKey)));
|
|
|
|
it('result contains a signed prekey', () => {
|
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
|
|
|
});
|