Skip to content
This repository was archived by the owner on Aug 5, 2021. It is now read-only.

Commit e68e17c

Browse files
committed
Test SessionCipher for encrypt/decrypt identity checks
In order to facilitate generation of session state in these tests, move some helpers from SessionBuilderTest.js into global namespace of the test page
1 parent 2657f00 commit e68e17c

File tree

4 files changed

+158
-46
lines changed

4 files changed

+158
-46
lines changed

test/SessionBuilderTest.js

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,5 @@
11
describe('SessionBuilder', function() {
22
this.timeout(5000);
3-
var KeyHelper = libsignal.KeyHelper;
4-
5-
function generateIdentity(store) {
6-
return Promise.all([
7-
KeyHelper.generateIdentityKeyPair(),
8-
KeyHelper.generateRegistrationId(),
9-
]).then(function(result) {
10-
store.put('identityKey', result[0]);
11-
store.put('registrationId', result[1]);
12-
});
13-
}
14-
15-
function generatePreKeyBundle(store, preKeyId, signedPreKeyId) {
16-
return Promise.all([
17-
store.getIdentityKeyPair(),
18-
store.getLocalRegistrationId()
19-
]).then(function(result) {
20-
var identity = result[0];
21-
var registrationId = result[1];
22-
23-
return Promise.all([
24-
KeyHelper.generatePreKey(preKeyId),
25-
KeyHelper.generateSignedPreKey(identity, signedPreKeyId),
26-
]).then(function(keys) {
27-
var preKey = keys[0]
28-
var signedPreKey = keys[1];
29-
30-
store.storePreKey(preKeyId, preKey.keyPair);
31-
store.storeSignedPreKey(signedPreKeyId, signedPreKey.keyPair);
32-
33-
return {
34-
identityKey: identity.pubKey,
35-
registrationId : registrationId,
36-
preKey: {
37-
keyId : preKeyId,
38-
publicKey : preKey.keyPair.pubKey
39-
},
40-
signedPreKey: {
41-
keyId : signedPreKeyId,
42-
publicKey : signedPreKey.keyPair.pubKey,
43-
signature : signedPreKey.signature
44-
}
45-
};
46-
});
47-
});
48-
}
493

504
var ALICE_ADDRESS = new SignalProtocolAddress("+14151111111", 1);
515
var BOB_ADDRESS = new SignalProtocolAddress("+14152222222", 1);

test/SessionCipherTest.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,4 +365,68 @@ describe('SessionCipher', function() {
365365
});
366366
});
367367
});
368+
369+
describe("key changes", function() {
370+
var ALICE_ADDRESS = new SignalProtocolAddress("+14151111111", 1);
371+
var BOB_ADDRESS = new SignalProtocolAddress("+14152222222", 1);
372+
var originalMessage = util.toArrayBuffer("L'homme est condamné à être libre");
373+
374+
var aliceStore = new SignalProtocolStore();
375+
376+
var bobStore = new SignalProtocolStore();
377+
var bobPreKeyId = 1337;
378+
var bobSignedKeyId = 1;
379+
380+
var Curve = libsignal.Curve;
381+
382+
var bobSessionCipher = new libsignal.SessionCipher(bobStore, ALICE_ADDRESS);
383+
384+
before(function(done) {
385+
Promise.all(
386+
[aliceStore, bobStore].map(generateIdentity)
387+
).then(function() {
388+
return generatePreKeyBundle(bobStore, bobPreKeyId, bobSignedKeyId);
389+
}).then(function(preKeyBundle) {
390+
var builder = new libsignal.SessionBuilder(aliceStore, BOB_ADDRESS);
391+
return builder.processPreKey(preKeyBundle).then(function() {
392+
var aliceSessionCipher = new libsignal.SessionCipher(aliceStore, BOB_ADDRESS);
393+
return aliceSessionCipher.encrypt(originalMessage);
394+
}).then(function(ciphertext) {
395+
return bobSessionCipher.decryptPreKeyWhisperMessage(ciphertext.body, 'binary');
396+
}).then(function() {
397+
done();
398+
});
399+
}).catch(done);
400+
});
401+
402+
403+
describe("When bob's identity changes", function() {
404+
var messageFromBob;
405+
before(function(done) {
406+
return bobSessionCipher.encrypt(originalMessage).then(function(ciphertext) {
407+
messageFromBob = ciphertext;
408+
}).then(function() {
409+
return generateIdentity(bobStore);
410+
}).then(function() {
411+
return aliceStore.saveIdentity(BOB_ADDRESS.getName(), bobStore.get('identityKey').pubKey);
412+
}).then(function() {
413+
done();
414+
});
415+
});
416+
417+
it('alice cannot encrypt with the old session', function(done) {
418+
var aliceSessionCipher = new libsignal.SessionCipher(aliceStore, BOB_ADDRESS);
419+
return aliceSessionCipher.encrypt(originalMessage).catch(function(e) {
420+
assert.strictEqual(e.message, 'Identity key changed');
421+
}).then(done,done);
422+
});
423+
424+
it('alice cannot decrypt from the old session', function(done) {
425+
var aliceSessionCipher = new libsignal.SessionCipher(aliceStore, BOB_ADDRESS);
426+
return aliceSessionCipher.decryptWhisperMessage(messageFromBob.body, 'binary').catch(function(e) {
427+
assert.strictEqual(e.message, 'Identity key changed');
428+
}).then(done, done);
429+
});
430+
});
431+
});
368432
});

test/_test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,50 @@ function hexToArrayBuffer(str) {
5252
array[i] = parseInt(str.substr(i*2, 2), 16);
5353
return ret;
5454
};
55+
56+
var KeyHelper = libsignal.KeyHelper;
57+
58+
function generateIdentity(store) {
59+
return Promise.all([
60+
KeyHelper.generateIdentityKeyPair(),
61+
KeyHelper.generateRegistrationId(),
62+
]).then(function(result) {
63+
store.put('identityKey', result[0]);
64+
store.put('registrationId', result[1]);
65+
});
66+
}
67+
68+
function generatePreKeyBundle(store, preKeyId, signedPreKeyId) {
69+
return Promise.all([
70+
store.getIdentityKeyPair(),
71+
store.getLocalRegistrationId()
72+
]).then(function(result) {
73+
var identity = result[0];
74+
var registrationId = result[1];
75+
76+
return Promise.all([
77+
KeyHelper.generatePreKey(preKeyId),
78+
KeyHelper.generateSignedPreKey(identity, signedPreKeyId),
79+
]).then(function(keys) {
80+
var preKey = keys[0]
81+
var signedPreKey = keys[1];
82+
83+
store.storePreKey(preKeyId, preKey.keyPair);
84+
store.storeSignedPreKey(signedPreKeyId, signedPreKey.keyPair);
85+
86+
return {
87+
identityKey: identity.pubKey,
88+
registrationId : registrationId,
89+
preKey: {
90+
keyId : preKeyId,
91+
publicKey : preKey.keyPair.pubKey
92+
},
93+
signedPreKey: {
94+
keyId : signedPreKeyId,
95+
publicKey : signedPreKey.keyPair.pubKey,
96+
signature : signedPreKey.signature
97+
}
98+
};
99+
});
100+
});
101+
}

test/test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34453,3 +34453,50 @@ function hexToArrayBuffer(str) {
3445334453
array[i] = parseInt(str.substr(i*2, 2), 16);
3445434454
return ret;
3445534455
};
34456+
34457+
var KeyHelper = libsignal.KeyHelper;
34458+
34459+
function generateIdentity(store) {
34460+
return Promise.all([
34461+
KeyHelper.generateIdentityKeyPair(),
34462+
KeyHelper.generateRegistrationId(),
34463+
]).then(function(result) {
34464+
store.put('identityKey', result[0]);
34465+
store.put('registrationId', result[1]);
34466+
});
34467+
}
34468+
34469+
function generatePreKeyBundle(store, preKeyId, signedPreKeyId) {
34470+
return Promise.all([
34471+
store.getIdentityKeyPair(),
34472+
store.getLocalRegistrationId()
34473+
]).then(function(result) {
34474+
var identity = result[0];
34475+
var registrationId = result[1];
34476+
34477+
return Promise.all([
34478+
KeyHelper.generatePreKey(preKeyId),
34479+
KeyHelper.generateSignedPreKey(identity, signedPreKeyId),
34480+
]).then(function(keys) {
34481+
var preKey = keys[0]
34482+
var signedPreKey = keys[1];
34483+
34484+
store.storePreKey(preKeyId, preKey.keyPair);
34485+
store.storeSignedPreKey(signedPreKeyId, signedPreKey.keyPair);
34486+
34487+
return {
34488+
identityKey: identity.pubKey,
34489+
registrationId : registrationId,
34490+
preKey: {
34491+
keyId : preKeyId,
34492+
publicKey : preKey.keyPair.pubKey
34493+
},
34494+
signedPreKey: {
34495+
keyId : signedPreKeyId,
34496+
publicKey : signedPreKey.keyPair.pubKey,
34497+
signature : signedPreKey.signature
34498+
}
34499+
};
34500+
});
34501+
});
34502+
}

0 commit comments

Comments
 (0)