@mikudev/libsignal-node 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,325 @@
1
+ // vim: ts=4:sw=4
2
+
3
+ const BaseKeyType = require('./base_key_type');
4
+
5
+ const CLOSED_SESSIONS_MAX = 40;
6
+ const SESSION_RECORD_VERSION = 'v1';
7
+
8
+ function assertBuffer(value) {
9
+ if (!Buffer.isBuffer(value)) {
10
+ throw new TypeError("Buffer required");
11
+ }
12
+ }
13
+
14
+
15
+ class SessionEntry {
16
+
17
+ constructor() {
18
+ this._chains = {};
19
+ }
20
+
21
+ toString() {
22
+ const baseKey = this.indexInfo && this.indexInfo.baseKey &&
23
+ this.indexInfo.baseKey.toString('base64');
24
+ return `<SessionEntry [baseKey=${baseKey}]>`;
25
+ }
26
+
27
+ inspect() {
28
+ return this.toString();
29
+ }
30
+
31
+ addChain(key, value) {
32
+ assertBuffer(key);
33
+ const id = key.toString('base64');
34
+ if (this._chains.hasOwnProperty(id)) {
35
+ throw new Error("Overwrite attempt");
36
+ }
37
+ this._chains[id] = value;
38
+ }
39
+
40
+ getChain(key) {
41
+ assertBuffer(key);
42
+ return this._chains[key.toString('base64')];
43
+ }
44
+
45
+ deleteChain(key) {
46
+ assertBuffer(key);
47
+ const id = key.toString('base64');
48
+ if (!this._chains.hasOwnProperty(id)) {
49
+ throw new ReferenceError("Not Found");
50
+ }
51
+ delete this._chains[id];
52
+ }
53
+
54
+ *chains() {
55
+ for (const [k, v] of Object.entries(this._chains)) {
56
+ yield [Buffer.from(k, 'base64'), v];
57
+ }
58
+ }
59
+
60
+ serialize() {
61
+ const data = {
62
+ registrationId: this.registrationId,
63
+ currentRatchet: {
64
+ ephemeralKeyPair: {
65
+ pubKey: this.currentRatchet.ephemeralKeyPair.pubKey.toString('base64'),
66
+ privKey: this.currentRatchet.ephemeralKeyPair.privKey.toString('base64')
67
+ },
68
+ lastRemoteEphemeralKey: this.currentRatchet.lastRemoteEphemeralKey.toString('base64'),
69
+ previousCounter: this.currentRatchet.previousCounter,
70
+ rootKey: this.currentRatchet.rootKey.toString('base64')
71
+ },
72
+ indexInfo: {
73
+ baseKey: this.indexInfo.baseKey.toString('base64'),
74
+ baseKeyType: this.indexInfo.baseKeyType,
75
+ closed: this.indexInfo.closed,
76
+ used: this.indexInfo.used,
77
+ created: this.indexInfo.created,
78
+ remoteIdentityKey: this.indexInfo.remoteIdentityKey.toString('base64')
79
+ },
80
+ _chains: this._serialize_chains(this._chains)
81
+ };
82
+ if (this.pendingPreKey) {
83
+ data.pendingPreKey = Object.assign({}, this.pendingPreKey);
84
+ data.pendingPreKey.baseKey = this.pendingPreKey.baseKey.toString('base64');
85
+ }
86
+ return data;
87
+ }
88
+
89
+ static deserialize(data) {
90
+ const obj = new this();
91
+ obj.registrationId = data.registrationId;
92
+ obj.currentRatchet = {
93
+ ephemeralKeyPair: {
94
+ pubKey: Buffer.from(data.currentRatchet.ephemeralKeyPair.pubKey, 'base64'),
95
+ privKey: Buffer.from(data.currentRatchet.ephemeralKeyPair.privKey, 'base64')
96
+ },
97
+ lastRemoteEphemeralKey: Buffer.from(data.currentRatchet.lastRemoteEphemeralKey, 'base64'),
98
+ previousCounter: data.currentRatchet.previousCounter,
99
+ rootKey: Buffer.from(data.currentRatchet.rootKey, 'base64')
100
+ };
101
+ obj.indexInfo = {
102
+ baseKey: Buffer.from(data.indexInfo.baseKey, 'base64'),
103
+ baseKeyType: data.indexInfo.baseKeyType,
104
+ closed: data.indexInfo.closed,
105
+ used: data.indexInfo.used,
106
+ created: data.indexInfo.created,
107
+ remoteIdentityKey: Buffer.from(data.indexInfo.remoteIdentityKey, 'base64')
108
+ };
109
+ obj._chains = this._deserialize_chains(data._chains);
110
+ if (data.pendingPreKey) {
111
+ obj.pendingPreKey = Object.assign({}, data.pendingPreKey);
112
+ obj.pendingPreKey.baseKey = Buffer.from(data.pendingPreKey.baseKey, 'base64');
113
+ }
114
+ return obj;
115
+ }
116
+
117
+ _serialize_chains(chains) {
118
+ const r = {};
119
+ for (const key of Object.keys(chains)) {
120
+ const c = chains[key];
121
+ const messageKeys = {};
122
+ for (const [idx, key] of Object.entries(c.messageKeys)) {
123
+ messageKeys[idx] = key.toString('base64');
124
+ }
125
+ r[key] = {
126
+ chainKey: {
127
+ counter: c.chainKey.counter,
128
+ key: c.chainKey.key && c.chainKey.key.toString('base64')
129
+ },
130
+ chainType: c.chainType,
131
+ messageKeys: messageKeys
132
+ };
133
+ }
134
+ return r;
135
+ }
136
+
137
+ static _deserialize_chains(chains_data) {
138
+ const r = {};
139
+ for (const key of Object.keys(chains_data)) {
140
+ const c = chains_data[key];
141
+ const messageKeys = {};
142
+ for (const [idx, key] of Object.entries(c.messageKeys)) {
143
+ messageKeys[idx] = Buffer.from(key, 'base64');
144
+ }
145
+ r[key] = {
146
+ chainKey: {
147
+ counter: c.chainKey.counter,
148
+ key: c.chainKey.key && Buffer.from(c.chainKey.key, 'base64')
149
+ },
150
+ chainType: c.chainType,
151
+ messageKeys: messageKeys
152
+ };
153
+ }
154
+ return r;
155
+ }
156
+
157
+ }
158
+
159
+
160
+ const migrations = [{
161
+ version: 'v1',
162
+ migrate: function migrateV1(data) {
163
+ const sessions = data._sessions;
164
+ if (data.registrationId) {
165
+ for (const key in sessions) {
166
+ if (!sessions[key].registrationId) {
167
+ sessions[key].registrationId = data.registrationId;
168
+ }
169
+ }
170
+ } else {
171
+ for (const key in sessions) {
172
+ if (sessions[key].indexInfo.closed === -1) {
173
+ /*console.error('V1 session storage migration error: registrationId',
174
+ data.registrationId, 'for open session version',
175
+ data.version);*/
176
+ }
177
+ }
178
+ }
179
+ }
180
+ }];
181
+
182
+
183
+ class SessionRecord {
184
+
185
+ static createEntry() {
186
+ return new SessionEntry();
187
+ }
188
+
189
+ static migrate(data) {
190
+ let run = (data.version === undefined);
191
+ for (let i = 0; i < migrations.length; ++i) {
192
+ if (run) {
193
+ //console.info("Migrating session to:", migrations[i].version);
194
+ migrations[i].migrate(data);
195
+ } else if (migrations[i].version === data.version) {
196
+ run = true;
197
+ }
198
+ }
199
+ if (!run) {
200
+ throw new Error("Error migrating SessionRecord");
201
+ }
202
+ }
203
+
204
+ static deserialize(data) {
205
+ if (data.version !== SESSION_RECORD_VERSION) {
206
+ this.migrate(data);
207
+ }
208
+ const obj = new this();
209
+ if (data._sessions) {
210
+ for (const [key, entry] of Object.entries(data._sessions)) {
211
+ obj.sessions[key] = SessionEntry.deserialize(entry);
212
+ }
213
+ }
214
+ return obj;
215
+ }
216
+
217
+ constructor() {
218
+ this.sessions = {};
219
+ this.version = SESSION_RECORD_VERSION;
220
+ }
221
+
222
+ serialize() {
223
+ const _sessions = {};
224
+ for (const [key, entry] of Object.entries(this.sessions)) {
225
+ _sessions[key] = entry.serialize();
226
+ }
227
+ return {
228
+ _sessions,
229
+ version: this.version
230
+ };
231
+ }
232
+
233
+ haveOpenSession() {
234
+ const openSession = this.getOpenSession();
235
+ return (!!openSession && typeof openSession.registrationId === 'number');
236
+ }
237
+
238
+ getSession(key) {
239
+ assertBuffer(key);
240
+ const session = this.sessions[key.toString('base64')];
241
+ if (session && session.indexInfo.baseKeyType === BaseKeyType.OURS) {
242
+ throw new Error("Tried to lookup a session using our basekey");
243
+ }
244
+ return session;
245
+ }
246
+
247
+ getOpenSession() {
248
+ for (const session of Object.values(this.sessions)) {
249
+ if (!this.isClosed(session)) {
250
+ return session;
251
+ }
252
+ }
253
+ }
254
+
255
+ setSession(session) {
256
+ this.sessions[session.indexInfo.baseKey.toString('base64')] = session;
257
+ }
258
+
259
+ getSessions() {
260
+ // Return sessions ordered with most recently used first.
261
+ return Array.from(Object.values(this.sessions)).sort((a, b) => {
262
+ const aUsed = a.indexInfo.used || 0;
263
+ const bUsed = b.indexInfo.used || 0;
264
+ return aUsed === bUsed ? 0 : aUsed < bUsed ? 1 : -1;
265
+ });
266
+ }
267
+
268
+ closeSession(session) {
269
+ if (this.isClosed(session)) {
270
+ //console.warn("Session already closed", session);
271
+ return;
272
+ }
273
+ //console.info("Closing session:", session);
274
+ session.indexInfo.closed = Date.now();
275
+ }
276
+
277
+ openSession(session) {
278
+ if (!this.isClosed(session)) {
279
+ //console.warn("Session already open");
280
+ }
281
+ //console.info("Opening session:", session);
282
+ session.indexInfo.closed = -1;
283
+ }
284
+
285
+ isClosed(session) {
286
+ return session.indexInfo.closed !== -1;
287
+ }
288
+
289
+ removeOldSessions() {
290
+ const sessionKeys = Object.keys(this.sessions);
291
+ const sessionsLength = sessionKeys.length;
292
+
293
+ for (let i = 0; i < sessionsLength && sessionsLength > CLOSED_SESSIONS_MAX; i++) {
294
+ let oldestKey;
295
+ let oldestSession;
296
+ for (const key of sessionKeys) {
297
+ if (!key) continue
298
+ const session = this.sessions[key];
299
+ if (session.indexInfo.closed !== -1 &&
300
+ (!oldestSession || session.indexInfo.closed < oldestSession.indexInfo.closed)) {
301
+ oldestKey = key;
302
+ oldestSession = session;
303
+ break;
304
+ }
305
+ }
306
+
307
+ if (oldestKey) {
308
+ //console.info("Removing old closed session:", oldestSession);
309
+ delete this.sessions[oldestKey];
310
+ sessionKeys.splice(sessionKeys.indexOf(oldestKey), 1);
311
+ } else {
312
+ continue
313
+ // throw new Error('Corrupt sessions object');
314
+ }
315
+ }
316
+ }
317
+
318
+ deleteAllSessions() {
319
+ for (const key of Object.keys(this.sessions)) {
320
+ delete this.sessions[key];
321
+ }
322
+ }
323
+ }
324
+
325
+ module.exports = SessionRecord;