@hiroosy/baileys-lite 0.0.1
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.
- package/README.md +25 -0
- package/WAProto/index.d.ts +15932 -0
- package/WAProto/index.js +115856 -0
- package/engine-requirements.js +11 -0
- package/package.json +27 -0
- package/src/binary/wa-binary-constants.js +1300 -0
- package/src/binary/wa-binary.js +649 -0
- package/src/constants.js +143 -0
- package/src/defaults.js +102 -0
- package/src/foundation/audio-duration.js +530 -0
- package/src/foundation/audio-samples.js +268 -0
- package/src/foundation/boom.js +46 -0
- package/src/foundation/cache.js +142 -0
- package/src/foundation/concurrency.js +87 -0
- package/src/foundation/curve25519.js +221 -0
- package/src/foundation/link-preview.js +320 -0
- package/src/foundation/logger.js +69 -0
- package/src/foundation/long.js +140 -0
- package/src/foundation/protobuf-minimal.js +619 -0
- package/src/foundation/wa-crypto.js +76 -0
- package/src/foundation/zip.js +124 -0
- package/src/index.js +48 -0
- package/src/signal/libsignal.js +1051 -0
- package/src/signal/signal-group.js +487 -0
- package/src/signal/signal-repository.js +646 -0
- package/src/socket/business-communities.js +830 -0
- package/src/socket/chats.js +674 -0
- package/src/socket/groups-newsletter.js +342 -0
- package/src/socket/messages-recv.js +1835 -0
- package/src/socket/messages-send.js +1202 -0
- package/src/socket/socket-core.js +833 -0
- package/src/socket/usync.js +291 -0
- package/src/socket-client/websocket-client.js +124 -0
- package/src/utils/auth-state-core.js +764 -0
- package/src/utils/auth-state-storage.js +316 -0
- package/src/utils/chat-sync.js +1045 -0
- package/src/utils/media.js +841 -0
- package/src/utils/message-compose.js +1609 -0
- package/src/utils/message-processing.js +958 -0
- package/src/utils/wa-protocol-core.js +1283 -0
- package/src/vendor/jpeg-js/LICENSE +25 -0
- package/src/vendor/jpeg-js/decoder.js +1152 -0
- package/src/vendor/jpeg-js/encoder.js +824 -0
- package/src/vendor/pngjs/LICENSE +20 -0
- package/src/vendor/pngjs/bitmapper.js +273 -0
- package/src/vendor/pngjs/bitpacker.js +167 -0
- package/src/vendor/pngjs/constants.js +38 -0
- package/src/vendor/pngjs/crc.js +47 -0
- package/src/vendor/pngjs/filter-pack.js +179 -0
- package/src/vendor/pngjs/filter-parse-sync.js +27 -0
- package/src/vendor/pngjs/filter-parse.js +185 -0
- package/src/vendor/pngjs/format-normaliser.js +101 -0
- package/src/vendor/pngjs/interlace.js +101 -0
- package/src/vendor/pngjs/packer-sync.js +65 -0
- package/src/vendor/pngjs/packer.js +138 -0
- package/src/vendor/pngjs/paeth-predictor.js +22 -0
- package/src/vendor/pngjs/parser-sync.js +121 -0
- package/src/vendor/pngjs/parser.js +299 -0
- package/src/vendor/pngjs/png-sync.js +19 -0
- package/src/vendor/pngjs/sync-inflate.js +174 -0
- package/src/vendor/pngjs/sync-reader.js +54 -0
- package/src/wam/wam-constants.js +22853 -0
- package/src/wam/wam.js +151 -0
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Signal protocol "Sender Key" group-messaging implementation.
|
|
3
|
+
* Combines what used to be 12 separate files under Signal/Group/.
|
|
4
|
+
*
|
|
5
|
+
* Cryptographic primitives (encrypt/decrypt/MAC/signature/key derivation)
|
|
6
|
+
* come from the native ./libsignal.js port (no npm dependency).
|
|
7
|
+
*/
|
|
8
|
+
import * as nodeCrypto from 'node:crypto'
|
|
9
|
+
import { crypto as signalCrypto, curve as signalCurve } from './libsignal.js'
|
|
10
|
+
|
|
11
|
+
const { calculateMAC, decrypt, deriveSecrets, encrypt } = signalCrypto
|
|
12
|
+
const { calculateSignature, generateKeyPair, verifySignature } = signalCurve
|
|
13
|
+
import { proto } from '../../WAProto/index.js'
|
|
14
|
+
import { BufferJSON } from '../utils/wa-protocol-core.js'
|
|
15
|
+
|
|
16
|
+
/* ------------------------------------------------------------------ */
|
|
17
|
+
/* CiphertextMessage — shared constants base class */
|
|
18
|
+
/* ------------------------------------------------------------------ */
|
|
19
|
+
|
|
20
|
+
export class CiphertextMessage {
|
|
21
|
+
constructor() {
|
|
22
|
+
this.UNSUPPORTED_VERSION = 1
|
|
23
|
+
this.CURRENT_VERSION = 3
|
|
24
|
+
this.WHISPER_TYPE = 2
|
|
25
|
+
this.PREKEY_TYPE = 3
|
|
26
|
+
this.SENDERKEY_TYPE = 4
|
|
27
|
+
this.SENDERKEY_DISTRIBUTION_TYPE = 5
|
|
28
|
+
this.ENCRYPTED_MESSAGE_OVERHEAD = 53
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/* ------------------------------------------------------------------ */
|
|
33
|
+
/* keyhelper */
|
|
34
|
+
/* ------------------------------------------------------------------ */
|
|
35
|
+
|
|
36
|
+
export const keyhelper = {
|
|
37
|
+
generateSenderKey() {
|
|
38
|
+
return nodeCrypto.randomBytes(32)
|
|
39
|
+
},
|
|
40
|
+
generateSenderKeyId() {
|
|
41
|
+
return nodeCrypto.randomInt(2147483647)
|
|
42
|
+
},
|
|
43
|
+
generateSenderSigningKey(key) {
|
|
44
|
+
if (!key) key = generateKeyPair()
|
|
45
|
+
return {
|
|
46
|
+
public: Buffer.from(key.pubKey),
|
|
47
|
+
private: Buffer.from(key.privKey)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/* ------------------------------------------------------------------ */
|
|
53
|
+
/* SenderKeyName */
|
|
54
|
+
/* ------------------------------------------------------------------ */
|
|
55
|
+
|
|
56
|
+
function isNull(str) {
|
|
57
|
+
return str === null || str === ''
|
|
58
|
+
}
|
|
59
|
+
function intValue(num) {
|
|
60
|
+
const MAX_VALUE = 0x7fffffff
|
|
61
|
+
const MIN_VALUE = -0x80000000
|
|
62
|
+
if (num > MAX_VALUE || num < MIN_VALUE) return num & 0xffffffff
|
|
63
|
+
return num
|
|
64
|
+
}
|
|
65
|
+
function hashCode(strKey) {
|
|
66
|
+
let hash = 0
|
|
67
|
+
if (!isNull(strKey)) {
|
|
68
|
+
for (let i = 0; i < strKey.length; i++) {
|
|
69
|
+
hash = hash * 31 + strKey.charCodeAt(i)
|
|
70
|
+
hash = intValue(hash)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return hash
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export class SenderKeyName {
|
|
77
|
+
constructor(groupId, sender) {
|
|
78
|
+
this.groupId = groupId
|
|
79
|
+
this.sender = sender
|
|
80
|
+
}
|
|
81
|
+
getGroupId() {
|
|
82
|
+
return this.groupId
|
|
83
|
+
}
|
|
84
|
+
getSender() {
|
|
85
|
+
return this.sender
|
|
86
|
+
}
|
|
87
|
+
serialize() {
|
|
88
|
+
return `${this.groupId}::${this.sender.id}::${this.sender.deviceId}`
|
|
89
|
+
}
|
|
90
|
+
toString() {
|
|
91
|
+
return this.serialize()
|
|
92
|
+
}
|
|
93
|
+
equals(other) {
|
|
94
|
+
if (other === null) return false
|
|
95
|
+
return this.groupId === other.groupId && this.sender.toString() === other.sender.toString()
|
|
96
|
+
}
|
|
97
|
+
hashCode() {
|
|
98
|
+
return hashCode(this.groupId) ^ hashCode(this.sender.toString())
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/* ------------------------------------------------------------------ */
|
|
103
|
+
/* SenderMessageKey / SenderChainKey */
|
|
104
|
+
/* ------------------------------------------------------------------ */
|
|
105
|
+
|
|
106
|
+
export class SenderMessageKey {
|
|
107
|
+
constructor(iteration, seed) {
|
|
108
|
+
const derivative = deriveSecrets(seed, Buffer.alloc(32), Buffer.from('WhisperGroup'))
|
|
109
|
+
const keys = new Uint8Array(32)
|
|
110
|
+
keys.set(new Uint8Array(derivative[0].slice(16)))
|
|
111
|
+
keys.set(new Uint8Array(derivative[1].slice(0, 16)), 16)
|
|
112
|
+
this.iv = Buffer.from(derivative[0].slice(0, 16))
|
|
113
|
+
this.cipherKey = Buffer.from(keys.buffer)
|
|
114
|
+
this.iteration = iteration
|
|
115
|
+
this.seed = seed
|
|
116
|
+
}
|
|
117
|
+
getIteration() {
|
|
118
|
+
return this.iteration
|
|
119
|
+
}
|
|
120
|
+
getIv() {
|
|
121
|
+
return this.iv
|
|
122
|
+
}
|
|
123
|
+
getCipherKey() {
|
|
124
|
+
return this.cipherKey
|
|
125
|
+
}
|
|
126
|
+
getSeed() {
|
|
127
|
+
return this.seed
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export class SenderChainKey {
|
|
132
|
+
constructor(iteration, chainKey) {
|
|
133
|
+
this.MESSAGE_KEY_SEED = Buffer.from([0x01])
|
|
134
|
+
this.CHAIN_KEY_SEED = Buffer.from([0x02])
|
|
135
|
+
this.iteration = iteration
|
|
136
|
+
this.chainKey = Buffer.from(chainKey)
|
|
137
|
+
}
|
|
138
|
+
getIteration() {
|
|
139
|
+
return this.iteration
|
|
140
|
+
}
|
|
141
|
+
getSenderMessageKey() {
|
|
142
|
+
return new SenderMessageKey(this.iteration, this.getDerivative(this.MESSAGE_KEY_SEED, this.chainKey))
|
|
143
|
+
}
|
|
144
|
+
getNext() {
|
|
145
|
+
return new SenderChainKey(this.iteration + 1, this.getDerivative(this.CHAIN_KEY_SEED, this.chainKey))
|
|
146
|
+
}
|
|
147
|
+
getSeed() {
|
|
148
|
+
return this.chainKey
|
|
149
|
+
}
|
|
150
|
+
getDerivative(seed, key) {
|
|
151
|
+
return calculateMAC(key, seed)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/* ------------------------------------------------------------------ */
|
|
156
|
+
/* SenderKeyState / SenderKeyRecord */
|
|
157
|
+
/* ------------------------------------------------------------------ */
|
|
158
|
+
|
|
159
|
+
export class SenderKeyState {
|
|
160
|
+
constructor(id, iteration, chainKey, signatureKeyPair, signatureKeyPublic, signatureKeyPrivate, senderKeyStateStructure) {
|
|
161
|
+
this.MAX_MESSAGE_KEYS = 2000
|
|
162
|
+
if (senderKeyStateStructure) {
|
|
163
|
+
this.senderKeyStateStructure = {
|
|
164
|
+
...senderKeyStateStructure,
|
|
165
|
+
senderMessageKeys: Array.isArray(senderKeyStateStructure.senderMessageKeys) ? senderKeyStateStructure.senderMessageKeys : []
|
|
166
|
+
}
|
|
167
|
+
} else {
|
|
168
|
+
if (signatureKeyPair) {
|
|
169
|
+
signatureKeyPublic = signatureKeyPair.public
|
|
170
|
+
signatureKeyPrivate = signatureKeyPair.private
|
|
171
|
+
}
|
|
172
|
+
this.senderKeyStateStructure = {
|
|
173
|
+
senderKeyId: id || 0,
|
|
174
|
+
senderChainKey: {
|
|
175
|
+
iteration: iteration || 0,
|
|
176
|
+
seed: Buffer.from(chainKey || [])
|
|
177
|
+
},
|
|
178
|
+
senderSigningKey: {
|
|
179
|
+
public: Buffer.from(signatureKeyPublic || []),
|
|
180
|
+
private: Buffer.from(signatureKeyPrivate || [])
|
|
181
|
+
},
|
|
182
|
+
senderMessageKeys: []
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
getKeyId() {
|
|
187
|
+
return this.senderKeyStateStructure.senderKeyId
|
|
188
|
+
}
|
|
189
|
+
getSenderChainKey() {
|
|
190
|
+
return new SenderChainKey(this.senderKeyStateStructure.senderChainKey.iteration, this.senderKeyStateStructure.senderChainKey.seed)
|
|
191
|
+
}
|
|
192
|
+
setSenderChainKey(chainKey) {
|
|
193
|
+
this.senderKeyStateStructure.senderChainKey = {
|
|
194
|
+
iteration: chainKey.getIteration(),
|
|
195
|
+
seed: chainKey.getSeed()
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
getSigningKeyPublic() {
|
|
199
|
+
const publicKey = Buffer.from(this.senderKeyStateStructure.senderSigningKey.public)
|
|
200
|
+
if (publicKey.length === 32) {
|
|
201
|
+
const fixed = Buffer.alloc(33)
|
|
202
|
+
fixed[0] = 0x05
|
|
203
|
+
publicKey.copy(fixed, 1)
|
|
204
|
+
return fixed
|
|
205
|
+
}
|
|
206
|
+
return publicKey
|
|
207
|
+
}
|
|
208
|
+
getSigningKeyPrivate() {
|
|
209
|
+
const privateKey = this.senderKeyStateStructure.senderSigningKey.private
|
|
210
|
+
return Buffer.from(privateKey || [])
|
|
211
|
+
}
|
|
212
|
+
hasSenderMessageKey(iteration) {
|
|
213
|
+
return this.senderKeyStateStructure.senderMessageKeys.some(key => key.iteration === iteration)
|
|
214
|
+
}
|
|
215
|
+
addSenderMessageKey(senderMessageKey) {
|
|
216
|
+
this.senderKeyStateStructure.senderMessageKeys.push({
|
|
217
|
+
iteration: senderMessageKey.getIteration(),
|
|
218
|
+
seed: senderMessageKey.getSeed()
|
|
219
|
+
})
|
|
220
|
+
if (this.senderKeyStateStructure.senderMessageKeys.length > this.MAX_MESSAGE_KEYS) {
|
|
221
|
+
this.senderKeyStateStructure.senderMessageKeys.shift()
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
removeSenderMessageKey(iteration) {
|
|
225
|
+
const index = this.senderKeyStateStructure.senderMessageKeys.findIndex(key => key.iteration === iteration)
|
|
226
|
+
if (index !== -1) {
|
|
227
|
+
const messageKey = this.senderKeyStateStructure.senderMessageKeys[index]
|
|
228
|
+
this.senderKeyStateStructure.senderMessageKeys.splice(index, 1)
|
|
229
|
+
return new SenderMessageKey(messageKey.iteration, messageKey.seed)
|
|
230
|
+
}
|
|
231
|
+
return null
|
|
232
|
+
}
|
|
233
|
+
getStructure() {
|
|
234
|
+
return this.senderKeyStateStructure
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export class SenderKeyRecord {
|
|
239
|
+
constructor(serialized) {
|
|
240
|
+
this.MAX_STATES = 5
|
|
241
|
+
this.senderKeyStates = []
|
|
242
|
+
if (serialized) {
|
|
243
|
+
for (const structure of serialized) {
|
|
244
|
+
this.senderKeyStates.push(new SenderKeyState(null, null, null, null, null, null, structure))
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
isEmpty() {
|
|
249
|
+
return this.senderKeyStates.length === 0
|
|
250
|
+
}
|
|
251
|
+
getSenderKeyState(keyId) {
|
|
252
|
+
if (keyId === undefined && this.senderKeyStates.length) {
|
|
253
|
+
return this.senderKeyStates[this.senderKeyStates.length - 1]
|
|
254
|
+
}
|
|
255
|
+
return this.senderKeyStates.find(state => state.getKeyId() === keyId)
|
|
256
|
+
}
|
|
257
|
+
addSenderKeyState(id, iteration, chainKey, signatureKey) {
|
|
258
|
+
this.senderKeyStates.push(new SenderKeyState(id, iteration, chainKey, null, signatureKey))
|
|
259
|
+
if (this.senderKeyStates.length > this.MAX_STATES) this.senderKeyStates.shift()
|
|
260
|
+
}
|
|
261
|
+
setSenderKeyState(id, iteration, chainKey, keyPair) {
|
|
262
|
+
this.senderKeyStates.length = 0
|
|
263
|
+
this.senderKeyStates.push(new SenderKeyState(id, iteration, chainKey, keyPair))
|
|
264
|
+
}
|
|
265
|
+
serialize() {
|
|
266
|
+
return this.senderKeyStates.map(state => state.getStructure())
|
|
267
|
+
}
|
|
268
|
+
static deserialize(data) {
|
|
269
|
+
const str = Buffer.from(data).toString('utf-8')
|
|
270
|
+
const parsed = JSON.parse(str, BufferJSON.reviver)
|
|
271
|
+
return new SenderKeyRecord(parsed)
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/* ------------------------------------------------------------------ */
|
|
276
|
+
/* SenderKeyDistributionMessage / SenderKeyMessage (wire formats) */
|
|
277
|
+
/* ------------------------------------------------------------------ */
|
|
278
|
+
|
|
279
|
+
export class SenderKeyDistributionMessage extends CiphertextMessage {
|
|
280
|
+
constructor(id, iteration, chainKey, signatureKey, serialized) {
|
|
281
|
+
super()
|
|
282
|
+
if (serialized) {
|
|
283
|
+
try {
|
|
284
|
+
const message = serialized.slice(1)
|
|
285
|
+
const distributionMessage = proto.SenderKeyDistributionMessage.decode(message).toJSON()
|
|
286
|
+
this.serialized = serialized
|
|
287
|
+
this.id = distributionMessage.id
|
|
288
|
+
this.iteration = distributionMessage.iteration
|
|
289
|
+
this.chainKey =
|
|
290
|
+
typeof distributionMessage.chainKey === 'string' ? Buffer.from(distributionMessage.chainKey, 'base64') : distributionMessage.chainKey
|
|
291
|
+
this.signatureKey =
|
|
292
|
+
typeof distributionMessage.signingKey === 'string' ? Buffer.from(distributionMessage.signingKey, 'base64') : distributionMessage.signingKey
|
|
293
|
+
} catch (e) {
|
|
294
|
+
throw new Error(String(e))
|
|
295
|
+
}
|
|
296
|
+
} else {
|
|
297
|
+
const version = this.intsToByteHighAndLow(this.CURRENT_VERSION, this.CURRENT_VERSION)
|
|
298
|
+
this.id = id
|
|
299
|
+
this.iteration = iteration
|
|
300
|
+
this.chainKey = chainKey
|
|
301
|
+
this.signatureKey = signatureKey
|
|
302
|
+
const message = proto.SenderKeyDistributionMessage.encode(
|
|
303
|
+
proto.SenderKeyDistributionMessage.create({ id, iteration, chainKey, signingKey: this.signatureKey })
|
|
304
|
+
).finish()
|
|
305
|
+
this.serialized = Buffer.concat([Buffer.from([version]), message])
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
intsToByteHighAndLow(highValue, lowValue) {
|
|
309
|
+
return (((highValue << 4) | lowValue) & 0xff) % 256
|
|
310
|
+
}
|
|
311
|
+
serialize() {
|
|
312
|
+
return this.serialized
|
|
313
|
+
}
|
|
314
|
+
getType() {
|
|
315
|
+
return this.SENDERKEY_DISTRIBUTION_TYPE
|
|
316
|
+
}
|
|
317
|
+
getIteration() {
|
|
318
|
+
return this.iteration
|
|
319
|
+
}
|
|
320
|
+
getChainKey() {
|
|
321
|
+
return this.chainKey
|
|
322
|
+
}
|
|
323
|
+
getSignatureKey() {
|
|
324
|
+
return this.signatureKey
|
|
325
|
+
}
|
|
326
|
+
getId() {
|
|
327
|
+
return this.id
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
export class SenderKeyMessage extends CiphertextMessage {
|
|
332
|
+
constructor(keyId, iteration, ciphertext, signatureKey, serialized) {
|
|
333
|
+
super()
|
|
334
|
+
this.SIGNATURE_LENGTH = 64
|
|
335
|
+
if (serialized) {
|
|
336
|
+
const version = serialized[0]
|
|
337
|
+
const message = serialized.slice(1, serialized.length - this.SIGNATURE_LENGTH)
|
|
338
|
+
const signature = serialized.slice(-1 * this.SIGNATURE_LENGTH)
|
|
339
|
+
const senderKeyMessage = proto.SenderKeyMessage.decode(message).toJSON()
|
|
340
|
+
this.serialized = serialized
|
|
341
|
+
this.messageVersion = (version & 0xff) >> 4
|
|
342
|
+
this.keyId = senderKeyMessage.id
|
|
343
|
+
this.iteration = senderKeyMessage.iteration
|
|
344
|
+
this.ciphertext =
|
|
345
|
+
typeof senderKeyMessage.ciphertext === 'string' ? Buffer.from(senderKeyMessage.ciphertext, 'base64') : senderKeyMessage.ciphertext
|
|
346
|
+
this.signature = signature
|
|
347
|
+
} else {
|
|
348
|
+
const version = (((this.CURRENT_VERSION << 4) | this.CURRENT_VERSION) & 0xff) % 256
|
|
349
|
+
const ciphertextBuffer = Buffer.from(ciphertext)
|
|
350
|
+
const message = proto.SenderKeyMessage.encode(
|
|
351
|
+
proto.SenderKeyMessage.create({ id: keyId, iteration, ciphertext: ciphertextBuffer })
|
|
352
|
+
).finish()
|
|
353
|
+
const signature = this.getSignature(signatureKey, Buffer.concat([Buffer.from([version]), message]))
|
|
354
|
+
this.serialized = Buffer.concat([Buffer.from([version]), message, Buffer.from(signature)])
|
|
355
|
+
this.messageVersion = this.CURRENT_VERSION
|
|
356
|
+
this.keyId = keyId
|
|
357
|
+
this.iteration = iteration
|
|
358
|
+
this.ciphertext = ciphertextBuffer
|
|
359
|
+
this.signature = signature
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
getKeyId() {
|
|
363
|
+
return this.keyId
|
|
364
|
+
}
|
|
365
|
+
getIteration() {
|
|
366
|
+
return this.iteration
|
|
367
|
+
}
|
|
368
|
+
getCipherText() {
|
|
369
|
+
return this.ciphertext
|
|
370
|
+
}
|
|
371
|
+
verifySignature(signatureKey) {
|
|
372
|
+
const part1 = this.serialized.slice(0, this.serialized.length - this.SIGNATURE_LENGTH)
|
|
373
|
+
const part2 = this.serialized.slice(-1 * this.SIGNATURE_LENGTH)
|
|
374
|
+
const res = verifySignature(signatureKey, part1, part2)
|
|
375
|
+
if (!res) throw new Error('Invalid signature!')
|
|
376
|
+
}
|
|
377
|
+
getSignature(signatureKey, serialized) {
|
|
378
|
+
return Buffer.from(calculateSignature(signatureKey, serialized))
|
|
379
|
+
}
|
|
380
|
+
serialize() {
|
|
381
|
+
return this.serialized
|
|
382
|
+
}
|
|
383
|
+
getType() {
|
|
384
|
+
return 4
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/* ------------------------------------------------------------------ */
|
|
389
|
+
/* GroupSessionBuilder / GroupCipher */
|
|
390
|
+
/* ------------------------------------------------------------------ */
|
|
391
|
+
|
|
392
|
+
export class GroupSessionBuilder {
|
|
393
|
+
constructor(senderKeyStore) {
|
|
394
|
+
this.senderKeyStore = senderKeyStore
|
|
395
|
+
}
|
|
396
|
+
async process(senderKeyName, senderKeyDistributionMessage) {
|
|
397
|
+
const senderKeyRecord = await this.senderKeyStore.loadSenderKey(senderKeyName)
|
|
398
|
+
senderKeyRecord.addSenderKeyState(
|
|
399
|
+
senderKeyDistributionMessage.getId(),
|
|
400
|
+
senderKeyDistributionMessage.getIteration(),
|
|
401
|
+
senderKeyDistributionMessage.getChainKey(),
|
|
402
|
+
senderKeyDistributionMessage.getSignatureKey()
|
|
403
|
+
)
|
|
404
|
+
await this.senderKeyStore.storeSenderKey(senderKeyName, senderKeyRecord)
|
|
405
|
+
}
|
|
406
|
+
async create(senderKeyName) {
|
|
407
|
+
const senderKeyRecord = await this.senderKeyStore.loadSenderKey(senderKeyName)
|
|
408
|
+
if (senderKeyRecord.isEmpty()) {
|
|
409
|
+
const keyId = keyhelper.generateSenderKeyId()
|
|
410
|
+
const senderKey = keyhelper.generateSenderKey()
|
|
411
|
+
const signingKey = keyhelper.generateSenderSigningKey()
|
|
412
|
+
senderKeyRecord.setSenderKeyState(keyId, 0, senderKey, signingKey)
|
|
413
|
+
await this.senderKeyStore.storeSenderKey(senderKeyName, senderKeyRecord)
|
|
414
|
+
}
|
|
415
|
+
const state = senderKeyRecord.getSenderKeyState()
|
|
416
|
+
if (!state) throw new Error('No session state available')
|
|
417
|
+
return new SenderKeyDistributionMessage(
|
|
418
|
+
state.getKeyId(),
|
|
419
|
+
state.getSenderChainKey().getIteration(),
|
|
420
|
+
state.getSenderChainKey().getSeed(),
|
|
421
|
+
state.getSigningKeyPublic()
|
|
422
|
+
)
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
export class GroupCipher {
|
|
427
|
+
constructor(senderKeyStore, senderKeyName) {
|
|
428
|
+
this.senderKeyStore = senderKeyStore
|
|
429
|
+
this.senderKeyName = senderKeyName
|
|
430
|
+
}
|
|
431
|
+
async encrypt(paddedPlaintext) {
|
|
432
|
+
const record = await this.senderKeyStore.loadSenderKey(this.senderKeyName)
|
|
433
|
+
if (!record) throw new Error('No SenderKeyRecord found for encryption')
|
|
434
|
+
const senderKeyState = record.getSenderKeyState()
|
|
435
|
+
if (!senderKeyState) throw new Error('No session to encrypt message')
|
|
436
|
+
const iteration = senderKeyState.getSenderChainKey().getIteration()
|
|
437
|
+
const senderKey = this.getSenderKey(senderKeyState, iteration === 0 ? 0 : iteration + 1)
|
|
438
|
+
const ciphertext = await this.getCipherText(senderKey.getIv(), senderKey.getCipherKey(), paddedPlaintext)
|
|
439
|
+
const senderKeyMessage = new SenderKeyMessage(senderKeyState.getKeyId(), senderKey.getIteration(), ciphertext, senderKeyState.getSigningKeyPrivate())
|
|
440
|
+
await this.senderKeyStore.storeSenderKey(this.senderKeyName, record)
|
|
441
|
+
return senderKeyMessage.serialize()
|
|
442
|
+
}
|
|
443
|
+
async decrypt(senderKeyMessageBytes) {
|
|
444
|
+
const record = await this.senderKeyStore.loadSenderKey(this.senderKeyName)
|
|
445
|
+
if (!record) throw new Error('No SenderKeyRecord found for decryption')
|
|
446
|
+
const senderKeyMessage = new SenderKeyMessage(null, null, null, null, senderKeyMessageBytes)
|
|
447
|
+
const senderKeyState = record.getSenderKeyState(senderKeyMessage.getKeyId())
|
|
448
|
+
if (!senderKeyState) throw new Error('No session found to decrypt message')
|
|
449
|
+
senderKeyMessage.verifySignature(senderKeyState.getSigningKeyPublic())
|
|
450
|
+
const senderKey = this.getSenderKey(senderKeyState, senderKeyMessage.getIteration())
|
|
451
|
+
const plaintext = await this.getPlainText(senderKey.getIv(), senderKey.getCipherKey(), senderKeyMessage.getCipherText())
|
|
452
|
+
await this.senderKeyStore.storeSenderKey(this.senderKeyName, record)
|
|
453
|
+
return plaintext
|
|
454
|
+
}
|
|
455
|
+
getSenderKey(senderKeyState, iteration) {
|
|
456
|
+
let senderChainKey = senderKeyState.getSenderChainKey()
|
|
457
|
+
if (senderChainKey.getIteration() > iteration) {
|
|
458
|
+
if (senderKeyState.hasSenderMessageKey(iteration)) {
|
|
459
|
+
const messageKey = senderKeyState.removeSenderMessageKey(iteration)
|
|
460
|
+
if (!messageKey) throw new Error('No sender message key found for iteration')
|
|
461
|
+
return messageKey
|
|
462
|
+
}
|
|
463
|
+
throw new Error(`Received message with old counter: ${senderChainKey.getIteration()}, ${iteration}`)
|
|
464
|
+
}
|
|
465
|
+
if (iteration - senderChainKey.getIteration() > 2000) throw new Error('Over 2000 messages into the future!')
|
|
466
|
+
while (senderChainKey.getIteration() < iteration) {
|
|
467
|
+
senderKeyState.addSenderMessageKey(senderChainKey.getSenderMessageKey())
|
|
468
|
+
senderChainKey = senderChainKey.getNext()
|
|
469
|
+
}
|
|
470
|
+
senderKeyState.setSenderChainKey(senderChainKey.getNext())
|
|
471
|
+
return senderChainKey.getSenderMessageKey()
|
|
472
|
+
}
|
|
473
|
+
async getPlainText(iv, key, ciphertext) {
|
|
474
|
+
try {
|
|
475
|
+
return decrypt(key, ciphertext, iv)
|
|
476
|
+
} catch {
|
|
477
|
+
throw new Error('InvalidMessageException')
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
async getCipherText(iv, key, plaintext) {
|
|
481
|
+
try {
|
|
482
|
+
return encrypt(key, plaintext, iv)
|
|
483
|
+
} catch {
|
|
484
|
+
throw new Error('InvalidMessageException')
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
}
|