forked from adiwajshing/libsignal-node
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathsession_cipher.js
More file actions
359 lines (330 loc) · 15 KB
/
session_cipher.js
File metadata and controls
359 lines (330 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// vim: ts=4:sw=4:expandtab
const ChainType = require('./chain_type');
const ProtocolAddress = require('./protocol_address');
const SessionBuilder = require('./session_builder');
const SessionRecord = require('./session_record');
const Util = require('./util');
const crypto = require('./crypto');
const curve = require('./curve');
const errors = require('./errors');
const protobufs = require('./protobufs');
const queueJob = require('./queue_job');
const VERSION = 3;
function assertBuffer(value) {
if (!(value instanceof Buffer)) {
throw TypeError(`Expected Buffer instead of: ${value.constructor.name}`);
}
return value;
}
class SessionCipher {
constructor(storage, protocolAddress) {
if (!(protocolAddress instanceof ProtocolAddress)) {
throw new TypeError("protocolAddress must be a ProtocolAddress");
}
this.addr = protocolAddress;
this.storage = storage;
}
_encodeTupleByte(number1, number2) {
if (number1 > 15 || number2 > 15) {
throw TypeError("Numbers must be 4 bits or less");
}
return (number1 << 4) | number2;
}
_decodeTupleByte(byte) {
return [byte >> 4, byte & 0xf];
}
toString() {
return `<SessionCipher(${this.addr.toString()})>`;
}
/** @returns {Promise<import('./session_record')>} */
async getRecord() {
const record = await this.storage.loadSession(this.addr.toString());
if (record && !(record instanceof SessionRecord)) {
throw new TypeError('SessionRecord type expected from loadSession');
}
return record;
}
async storeRecord(record) {
record.removeOldSessions();
await this.storage.storeSession(this.addr.toString(), record);
}
async queueJob(awaitable) {
return await queueJob(this.addr.toString(), awaitable);
}
async encrypt(data) {
assertBuffer(data);
return await this.queueJob(async () => {
const [ourIdentityKey, ourRegistrationId, record] = await Promise.all([
this.storage.getOurIdentity(),
this.storage.getOurRegistrationId(),
this.getRecord()
]);
if (!record) {
throw new errors.SessionError("No sessions");
}
const session = record.getOpenSession();
if (!session) {
throw new errors.SessionError("No open session");
}
const chain = session.getChain(session.currentRatchet.ephemeralKeyPair.pubKey);
if (chain.chainType === ChainType.RECEIVING) {
throw new Error("Tried to encrypt on a receiving chain");
}
this.fillMessageKeys(chain, chain.chainKey.counter + 1);
const keys = crypto.deriveSecrets(chain.messageKeys[chain.chainKey.counter],
Buffer.alloc(32), Buffer.from("WhisperMessageKeys"));
delete chain.messageKeys[chain.chainKey.counter];
const msg = protobufs.WhisperMessage.create();
msg.ephemeralKey = session.currentRatchet.ephemeralKeyPair.pubKey;
msg.counter = chain.chainKey.counter;
msg.previousCounter = session.currentRatchet.previousCounter;
msg.ciphertext = crypto.encrypt(keys[0], data, keys[2].slice(0, 16));
const msgBuf = protobufs.WhisperMessage.encode(msg).finish();
const macInput = Buffer.alloc(msgBuf.byteLength + (33 * 2) + 1);
macInput.set(ourIdentityKey.pubKey);
macInput.set(session.indexInfo.remoteIdentityKey, 33);
macInput[33 * 2] = this._encodeTupleByte(VERSION, VERSION); // 51
macInput.set(msgBuf, (33 * 2) + 1);
const mac = crypto.calculateMAC(keys[1], macInput);
const result = Buffer.alloc(msgBuf.byteLength + 9);
result[0] = this._encodeTupleByte(VERSION, VERSION);
result.set(msgBuf, 1);
result.set(mac.slice(0, 8), msgBuf.byteLength + 1);
const remoteIdentityKey = session.indexInfo.remoteIdentityKey;
if (!await this.storage.isTrustedIdentity(this.addr.id, remoteIdentityKey)) {
throw new errors.UntrustedIdentityKeyError(this.addr.id, remoteIdentityKey);
}
// this.storage.saveIdentity(session.indexInfo.remoteIdentityKey)
record.updateSessionState(session);
await this.storeRecord(record);
let type, body;
if (session.pendingPreKey) {
type = 3; // prekey bundle
const preKeyMsg = protobufs.PreKeyWhisperMessage.create({
identityKey: ourIdentityKey.pubKey,
registrationId: ourRegistrationId,
baseKey: session.pendingPreKey.baseKey,
signedPreKeyId: session.pendingPreKey.signedKeyId,
message: result
});
if (session.pendingPreKey.preKeyId) {
preKeyMsg.preKeyId = session.pendingPreKey.preKeyId;
}
body = Buffer.concat([
Buffer.from([this._encodeTupleByte(VERSION, VERSION)]),
Buffer.from(
protobufs.PreKeyWhisperMessage.encode(preKeyMsg).finish()
)
]);
} else {
type = 1; // normal
body = result;
}
return {
type,
body,
registrationId: session.registrationId
};
});
}
async decryptWithSessions(data, sessions, errors = []) {
// Iterate through the sessions, attempting to decrypt using each one.
// Stop and return the result if we get a valid result.
if (!sessions.length) {
throw new errors.SessionError(errors[0] || "No sessions available");
}
const session = sessions.pop();
try {
const plaintext = await this.doDecryptWhisperMessage(data, session);
session.indexInfo.used = Date.now();
return {
session,
plaintext
};
} catch (e) {
if (e.name === "MessageCounterError")
throw e;
errors.push(e);
return await this.decryptWithSessions(data, sessions, errors);
}
}
async decryptWhisperMessage(data) {
assertBuffer(data);
return await this.queueJob(async () => {
const record = await this.getRecord();
if (!record) {
throw new errors.SessionError("No session record");
}
const result = await this.decryptWithSessions(data, record.getSessions());
const session = (await this.getRecord()).getOpenSession();
if (result.session.indexInfo.baseKey != session.indexInfo.baseKey) {
record.archiveCurrentState();
record.openSession(result.session);
}
const remoteIdentityKey = result.session.indexInfo.remoteIdentityKey;
if (!await this.storage.isTrustedIdentity(this.addr.id, remoteIdentityKey)) {
throw new errors.UntrustedIdentityKeyError(this.addr.id, remoteIdentityKey);
}
if (record.isClosed(result.session)) {
// It's possible for this to happen when processing a backlog of messages.
// The message was, hopefully, just sent back in a time when this session
// was the most current. Simply make a note of it and continue. If our
// actual open session is for reason invalid, that must be handled via
// a full SessionError response.
console.warn("Decrypted message with closed session.");
}
// this.storage.saveIdentity
record.updateSessionState(result.session);
await this.storeRecord(record);
return result.plaintext;
});
}
async decryptPreKeyWhisperMessage(data) {
assertBuffer(data);
const versions = this._decodeTupleByte(data[0]);
if (versions[1] > 3 || versions[0] < 3) { // min version > 3 or max version < 3
throw new Error("Incompatible version number on PreKeyWhisperMessage");
}
return await this.queueJob(async () => {
let record = await this.getRecord();
const preKeyProto = protobufs.PreKeyWhisperMessage.decode(data.slice(1));
if (!record) {
if (preKeyProto.registrationId == null) {
throw new Error("No registrationId");
}
record = new SessionRecord();
}
const builder = new SessionBuilder(this.storage, this.addr);
const preKeyId = await builder.initIncoming(record, preKeyProto);
const session = record.getSession(preKeyProto.baseKey);
const plaintext = await this.doDecryptWhisperMessage(preKeyProto.message, session);
record.updateSessionState(session);
const openSession = record.getOpenSession();
if (session && openSession && !Util.isEqual(session.indexInfo.remoteIdentityKey, openSession.indexInfo.remoteIdentityKey)) {
console.warn("Promote the old session and update identity");
record.archiveCurrentState();
record.openSession(session);
// this.storage.saveIdentity
}
await this.storeRecord(record);
if (preKeyId) {
await this.storage.removePreKey(preKeyId);
}
return plaintext;
});
}
async doDecryptWhisperMessage(messageBuffer, session) {
assertBuffer(messageBuffer);
if (!session) {
throw new Error("No session found to decrypt message from " + this.addr.toString())
}
const versions = this._decodeTupleByte(messageBuffer[0]);
if (versions[1] > 3 || versions[0] < 3) { // min version > 3 or max version < 3
throw new Error("Incompatible version number on WhisperMessage");
}
const messageProto = messageBuffer.slice(1, -8);
const message = protobufs.WhisperMessage.decode(messageProto);
await this.maybeStepRatchet(session, message.ephemeralKey, message.previousCounter);
const chain = session.getChain(message.ephemeralKey);
if (chain.chainType === ChainType.SENDING) {
throw new Error("Tried to decrypt on a sending chain");
}
this.fillMessageKeys(chain, message.counter);
if (!chain.messageKeys.hasOwnProperty(message.counter)) {
// Most likely the message was already decrypted and we are trying to process
// twice. This can happen if the user restarts before the server gets an ACK.
throw new errors.MessageCounterError("Message key not found. The counter was repeated or the key was not filled.");
}
const messageKey = chain.messageKeys[message.counter];
delete chain.messageKeys[message.counter];
const keys = crypto.deriveSecrets(messageKey, Buffer.alloc(32),
Buffer.from("WhisperMessageKeys"));
const ourIdentityKey = await this.storage.getOurIdentity();
const macInput = Buffer.alloc(messageProto.byteLength + (33 * 2) + 1);
macInput.set(session.indexInfo.remoteIdentityKey);
macInput.set(ourIdentityKey.pubKey, 33);
macInput[33 * 2] = this._encodeTupleByte(VERSION, VERSION);
macInput.set(messageProto, (33 * 2) + 1);
// This is where we most likely fail if the session is not a match.
// Don't misinterpret this as corruption.
crypto.verifyMAC(macInput, keys[1], messageBuffer.slice(-8), 8);
const plaintext = crypto.decrypt(keys[0], message.ciphertext, keys[2].slice(0, 16));
delete session.pendingPreKey;
return plaintext;
}
fillMessageKeys(chain, counter) {
if (chain.chainKey.counter >= counter) {
return;
}
if (counter - chain.chainKey.counter > 2000) {
throw new errors.SessionError("Over 2000 messages into the future!");
}
if (chain.chainKey.key === undefined) {
throw new errors.SessionError("Got invalid request to extend chain after it was already closed");
}
const key = chain.chainKey.key;
chain.messageKeys[chain.chainKey.counter + 1] = crypto.calculateMAC(key, Buffer.from([1]));
chain.chainKey.key = crypto.calculateMAC(key, Buffer.from([2]));
chain.chainKey.counter += 1;
return this.fillMessageKeys(chain, counter);
}
async maybeStepRatchet(session, remoteKey, previousCounter) {
if (session.getChain(remoteKey)) {
return;
}
const ratchet = session.currentRatchet;
let previousRatchet = session.getChain(ratchet.lastRemoteEphemeralKey);
if (previousRatchet) {
this.fillMessageKeys(previousRatchet, previousCounter);
delete previousRatchet.chainKey.key; // Close
}
this.calculateRatchet(session, remoteKey, false);
// Now swap the ephemeral key and calculate the new sending chain
const prevCounter = session.getChain(ratchet.ephemeralKeyPair.pubKey);
if (prevCounter) {
ratchet.previousCounter = prevCounter.chainKey.counter;
session.deleteChain(ratchet.ephemeralKeyPair.pubKey);
}
ratchet.ephemeralKeyPair = curve.generateKeyPair();
this.calculateRatchet(session, remoteKey, true);
ratchet.lastRemoteEphemeralKey = remoteKey;
}
calculateRatchet(session, remoteKey, sending) {
let ratchet = session.currentRatchet;
const sharedSecret = curve.calculateAgreement(remoteKey, ratchet.ephemeralKeyPair.privKey);
const masterKey = crypto.deriveSecrets(sharedSecret, ratchet.rootKey,
Buffer.from("WhisperRatchet"), /*chunks*/ 2);
const chainKey = sending ? ratchet.ephemeralKeyPair.pubKey : remoteKey;
session.addChain(chainKey, {
messageKeys: {},
chainKey: {
counter: -1,
key: masterKey[1]
},
chainType: sending ? ChainType.SENDING : ChainType.RECEIVING
});
ratchet.rootKey = masterKey[0];
}
async hasOpenSession() {
return await this.queueJob(async () => {
const record = await this.getRecord();
if (!record) {
return false;
}
return record.haveOpenSession();
});
}
async closeOpenSession() {
return await this.queueJob(async () => {
const record = await this.getRecord();
if (record) {
const openSession = record.getOpenSession();
if (openSession) {
record.archiveCurrentState();
await this.storeRecord(record);
}
}
});
}
}
module.exports = SessionCipher;