@hiroosy/baileys-lite 0.0.3 → 0.3.0
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/LICENSE +21 -0
- package/README.md +660 -12
- package/WAProto/index.js +2 -2558
- package/engine-requirements.js +3 -4
- package/package.json +8 -3
- package/src/binary/wa-binary.js +3 -25
- package/src/constants.js +7 -15
- package/src/defaults.js +2 -5
- package/src/foundation/ai-rich.js +530 -0
- package/src/foundation/audio-duration.js +43 -103
- package/src/foundation/audio-samples.js +19 -37
- package/src/foundation/boom.js +0 -7
- package/src/foundation/cache.js +4 -26
- package/src/foundation/concurrency.js +1 -19
- package/src/foundation/curve25519.js +7 -39
- package/src/foundation/link-preview.js +15 -57
- package/src/foundation/logger.js +0 -11
- package/src/foundation/long.js +4 -14
- package/src/foundation/protobuf-minimal.js +21 -53
- package/src/foundation/qrcode-terminal.js +273 -0
- package/src/foundation/sql-database.js +702 -0
- package/src/foundation/wa-crypto.js +2 -31
- package/src/foundation/zip.js +25 -41
- package/src/index.js +8 -18
- package/src/signal/libsignal.js +23 -78
- package/src/signal/signal-group.js +0 -35
- package/src/signal/signal-repository.js +10 -29
- package/src/socket/business-communities.js +12 -31
- package/src/socket/chats.js +5 -8
- package/src/socket/groups-newsletter.js +18 -17
- package/src/socket/messages-recv.js +81 -127
- package/src/socket/messages-send.js +372 -139
- package/src/socket/socket-core.js +227 -157
- package/src/socket/usync.js +2 -6
- package/src/socket-client/native-websocket.js +344 -0
- package/src/socket-client/websocket-client.js +84 -38
- package/src/utils/auth-state-core.js +14 -56
- package/src/utils/auth-state-storage.js +7 -42
- package/src/utils/chat-sync.js +4 -26
- package/src/utils/media.js +15 -71
- package/src/utils/message-compose.js +208 -85
- package/src/utils/message-processing.js +10 -65
- package/src/utils/username.js +19 -0
- package/src/utils/wa-protocol-core.js +71 -127
- package/src/vendor/jpeg-js/decoder.js +75 -136
- package/src/vendor/jpeg-js/encoder.js +179 -264
- package/src/vendor/pngjs/bitmapper.js +5 -27
- package/src/vendor/pngjs/bitpacker.js +4 -12
- package/src/vendor/pngjs/constants.js +3 -11
- package/src/vendor/pngjs/crc.js +0 -6
- package/src/vendor/pngjs/filter-pack.js +1 -7
- package/src/vendor/pngjs/filter-parse-sync.js +0 -6
- package/src/vendor/pngjs/filter-parse.js +1 -11
- package/src/vendor/pngjs/format-normaliser.js +5 -11
- package/src/vendor/pngjs/interlace.js +7 -24
- package/src/vendor/pngjs/packer-sync.js +0 -11
- package/src/vendor/pngjs/packer.js +7 -15
- package/src/vendor/pngjs/paeth-predictor.js +0 -6
- package/src/vendor/pngjs/parser-sync.js +0 -8
- package/src/vendor/pngjs/parser.js +5 -29
- package/src/vendor/pngjs/png-sync.js +0 -7
- package/src/vendor/pngjs/sync-inflate.js +7 -16
- package/src/vendor/pngjs/sync-reader.js +4 -11
- package/src/voip/WaVoipCoordinator.js +158 -0
- package/src/voip/app-data/WaAppDataStream.js +193 -0
- package/src/voip/app-data/protocol.js +172 -0
- package/src/voip/bytes.js +63 -0
- package/src/voip/call/WaCallManager.js +442 -0
- package/src/voip/call/WaCallMediaSession.js +1705 -0
- package/src/voip/call/call-state.js +203 -0
- package/src/voip/crypto/encryption.js +13 -0
- package/src/voip/crypto/primitives.js +21 -0
- package/src/voip/crypto/rtcp.js +66 -0
- package/src/voip/crypto/srtp.js +180 -0
- package/src/voip/crypto/ssrc.js +15 -0
- package/src/voip/index.js +628 -0
- package/src/voip/media/WaAudioEngine.js +486 -0
- package/src/voip/media/WaVideoEngine.js +275 -0
- package/src/voip/media/audio-codec.js +0 -0
- package/src/voip/media/audio-reorder.js +107 -0
- package/src/voip/media/h264.js +246 -0
- package/src/voip/media/mlow-codec.js +465 -0
- package/src/voip/media/rtcp.js +68 -0
- package/src/voip/media/rtp.js +242 -0
- package/src/voip/protobuf.js +30 -0
- package/src/voip/relay/WaManualRelay.js +609 -0
- package/src/voip/relay/datachannel/pre-negotiated.js +25 -0
- package/src/voip/relay/dtls/aead.js +46 -0
- package/src/voip/relay/dtls/cert-builder.js +102 -0
- package/src/voip/relay/dtls/der.js +98 -0
- package/src/voip/relay/dtls/ecdhe.js +37 -0
- package/src/voip/relay/dtls/handshake-framing.js +100 -0
- package/src/voip/relay/dtls/handshake-messages.js +158 -0
- package/src/voip/relay/dtls/handshake.js +582 -0
- package/src/voip/relay/dtls/prf.js +39 -0
- package/src/voip/relay/dtls/record.js +56 -0
- package/src/voip/relay/relay-ack.js +141 -0
- package/src/voip/relay/sctp/association.js +434 -0
- package/src/voip/relay/sctp/crc32c.js +21 -0
- package/src/voip/relay/sctp/wire.js +226 -0
- package/src/voip/relay/stun.js +463 -0
- package/src/voip/shim/baileys-resolve.js +7 -0
- package/src/voip/shim/core.js +66 -0
- package/src/voip/shim/crypto.js +6 -0
- package/src/voip/shim/proto.js +10 -0
- package/src/voip/shim/protocol.js +130 -0
- package/src/voip/shim/transport.js +82 -0
- package/src/voip/shim/util.js +207 -0
- package/src/voip/signaling/bridge.js +102 -0
- package/src/voip/signaling/screen-share.js +60 -0
- package/src/voip/signaling/signaling.js +491 -0
- package/src/voip/types.js +69 -0
- package/src/voip/voip-deps.js +243 -0
- package/src/voip/voipClient.js +314 -0
- package/src/wam/wam-constants.js +0 -1
- package/src/wam/wam.js +2 -7
|
@@ -1,34 +1,15 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Native replacements for the four `whatsapp-rust-bridge` (WASM) functions
|
|
3
|
-
* that baileys-lite used: md5, hkdf, expandAppStateKeys, LTHashAntiTampering.
|
|
4
|
-
*
|
|
5
|
-
* All built on `node:crypto`. Behavior (return types, error conditions, byte
|
|
6
|
-
* output) verified against the WASM module in test/native-deps.mjs.
|
|
7
|
-
*/
|
|
8
1
|
import { createHash, hkdfSync } from 'node:crypto'
|
|
9
2
|
|
|
10
3
|
const u8 = b => new Uint8Array(b.buffer, b.byteOffset, b.byteLength)
|
|
11
4
|
|
|
12
|
-
/** MD5 digest, returned as Uint8Array (same as the bridge). */
|
|
13
5
|
export const md5 = buffer => u8(createHash('md5').update(buffer).digest())
|
|
14
6
|
|
|
15
|
-
/**
|
|
16
|
-
* HKDF-SHA256 (RFC 5869).
|
|
17
|
-
* @param {Uint8Array} buffer input keying material
|
|
18
|
-
* @param {number} expandedLength output length in bytes (max 255 * 32)
|
|
19
|
-
* @param {{ salt?: Uint8Array, info?: string }} [options] `info` is UTF-8 encoded; missing salt = zero salt
|
|
20
|
-
* @returns {Uint8Array}
|
|
21
|
-
*/
|
|
22
7
|
export const hkdf = (buffer, expandedLength, { salt, info } = {}) => {
|
|
23
8
|
if (expandedLength > 255 * 32) throw new Error('HKDF expansion failed')
|
|
24
|
-
if (expandedLength === 0) return new Uint8Array(0)
|
|
9
|
+
if (expandedLength === 0) return new Uint8Array(0)
|
|
25
10
|
return u8(Buffer.from(hkdfSync('sha256', buffer, salt ?? Buffer.alloc(0), Buffer.from(info ?? '', 'utf8'), expandedLength)))
|
|
26
11
|
}
|
|
27
12
|
|
|
28
|
-
/**
|
|
29
|
-
* Derive the five WhatsApp app-state (syncd) keys from a 32-byte key.
|
|
30
|
-
* Keys are Uint8Array views, in the order WhatsApp Web uses.
|
|
31
|
-
*/
|
|
32
13
|
export const expandAppStateKeys = keyData => {
|
|
33
14
|
const expanded = hkdf(keyData, 160, { info: 'WhatsApp Mutation Keys' })
|
|
34
15
|
const part = i => expanded.slice(i * 32, i * 32 + 32)
|
|
@@ -41,21 +22,11 @@ export const expandAppStateKeys = keyData => {
|
|
|
41
22
|
}
|
|
42
23
|
}
|
|
43
24
|
|
|
44
|
-
/**
|
|
45
|
-
* LT Hash (lattice/summation hash): each value MAC is HKDF-expanded to 128 bytes
|
|
46
|
-
* and added to / subtracted from the running hash as 64 little-endian uint16 lanes.
|
|
47
|
-
*/
|
|
48
25
|
export class LTHashAntiTampering {
|
|
49
26
|
constructor() {
|
|
50
27
|
this.salt = 'WhatsApp Patch Integrity'
|
|
51
28
|
}
|
|
52
29
|
|
|
53
|
-
/**
|
|
54
|
-
* @param {Uint8Array} base current 128-byte hash
|
|
55
|
-
* @param {Uint8Array[]} subtract value MACs to remove
|
|
56
|
-
* @param {Uint8Array[]} add value MACs to add
|
|
57
|
-
* @returns {Uint8Array} new 128-byte hash (inputs are not mutated)
|
|
58
|
-
*/
|
|
59
30
|
subtractThenAdd(base, subtract, add) {
|
|
60
31
|
if (base.length !== 128) throw new Error(`Base hash must be 128 bytes, got ${base.length}`)
|
|
61
32
|
const lanes = new Uint16Array(64)
|
|
@@ -64,7 +35,7 @@ export class LTHashAntiTampering {
|
|
|
64
35
|
const apply = (items, sign) => {
|
|
65
36
|
for (const item of items) {
|
|
66
37
|
const ex = Buffer.from(hkdf(item, 128, { info: this.salt }))
|
|
67
|
-
for (let i = 0; i < 64; i++) lanes[i] += sign * ex.readUInt16LE(i * 2)
|
|
38
|
+
for (let i = 0; i < 64; i++) lanes[i] += sign * ex.readUInt16LE(i * 2)
|
|
68
39
|
}
|
|
69
40
|
}
|
|
70
41
|
apply(subtract, -1)
|
package/src/foundation/zip.js
CHANGED
|
@@ -1,17 +1,5 @@
|
|
|
1
|
-
import { deflateRawSync
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Replaces fflate's `zip()`. Baileys only uses it once, to build a WhatsApp
|
|
5
|
-
* sticker-pack .zip from `{ filename: [Uint8Array, { level }] }`, always with
|
|
6
|
-
* `level: 0` (store, no compression) for the entries it controls. This
|
|
7
|
-
* implementation supports store (level 0) and deflate (level > 0) so it's a
|
|
8
|
-
* drop-in for the one call site and any similar future one.
|
|
9
|
-
*
|
|
10
|
-
* Produces a standard ZIP: local file headers + data, then a central
|
|
11
|
-
* directory + end-of-central-directory record. No zip64, no encryption,
|
|
12
|
-
* no directory entries — matches what `zip()` from fflate produces for a
|
|
13
|
-
* flat file map.
|
|
14
|
-
*/
|
|
1
|
+
import { deflateRawSync } from 'node:zlib'
|
|
2
|
+
import * as zlib from 'node:zlib'
|
|
15
3
|
|
|
16
4
|
function dosDateTime(date = new Date()) {
|
|
17
5
|
const dosTime = (date.getHours() << 11) | (date.getMinutes() << 5) | (date.getSeconds() >> 1)
|
|
@@ -20,8 +8,8 @@ function dosDateTime(date = new Date()) {
|
|
|
20
8
|
}
|
|
21
9
|
|
|
22
10
|
function crc32Of(buf) {
|
|
23
|
-
|
|
24
|
-
if (typeof crc32 === 'function') return crc32(buf) >>> 0
|
|
11
|
+
|
|
12
|
+
if (typeof zlib.crc32 === 'function') return zlib.crc32(buf) >>> 0
|
|
25
13
|
return crc32Fallback(buf)
|
|
26
14
|
}
|
|
27
15
|
|
|
@@ -42,10 +30,6 @@ function crc32Fallback(buf) {
|
|
|
42
30
|
return (crc ^ 0xffffffff) >>> 0
|
|
43
31
|
}
|
|
44
32
|
|
|
45
|
-
/**
|
|
46
|
-
* @param files {Record<string, [Uint8Array, { level?: number }?]>}
|
|
47
|
-
* @param callback {(err: Error|null, data?: Uint8Array) => void}
|
|
48
|
-
*/
|
|
49
33
|
export function zip(files, callback) {
|
|
50
34
|
try {
|
|
51
35
|
const localChunks = []
|
|
@@ -59,14 +43,14 @@ export function zip(files, callback) {
|
|
|
59
43
|
const level = opts.level ?? 6
|
|
60
44
|
const store = level === 0
|
|
61
45
|
const compressed = store ? data : deflateRawSync(data, { level })
|
|
62
|
-
const method = store ? 0 : 8
|
|
46
|
+
const method = store ? 0 : 8
|
|
63
47
|
const crc = crc32Of(data)
|
|
64
48
|
const nameBuf = Buffer.from(filename, 'utf8')
|
|
65
49
|
|
|
66
50
|
const localHeader = Buffer.alloc(30)
|
|
67
|
-
localHeader.writeUInt32LE(0x04034b50, 0)
|
|
68
|
-
localHeader.writeUInt16LE(20, 4)
|
|
69
|
-
localHeader.writeUInt16LE(0, 6)
|
|
51
|
+
localHeader.writeUInt32LE(0x04034b50, 0)
|
|
52
|
+
localHeader.writeUInt16LE(20, 4)
|
|
53
|
+
localHeader.writeUInt16LE(0, 6)
|
|
70
54
|
localHeader.writeUInt16LE(method, 8)
|
|
71
55
|
localHeader.writeUInt16LE(dosTime, 10)
|
|
72
56
|
localHeader.writeUInt16LE(dosDate, 12)
|
|
@@ -74,15 +58,15 @@ export function zip(files, callback) {
|
|
|
74
58
|
localHeader.writeUInt32LE(compressed.length, 18)
|
|
75
59
|
localHeader.writeUInt32LE(data.length, 22)
|
|
76
60
|
localHeader.writeUInt16LE(nameBuf.length, 26)
|
|
77
|
-
localHeader.writeUInt16LE(0, 28)
|
|
61
|
+
localHeader.writeUInt16LE(0, 28)
|
|
78
62
|
|
|
79
63
|
localChunks.push(localHeader, nameBuf, compressed)
|
|
80
64
|
|
|
81
65
|
const centralHeader = Buffer.alloc(46)
|
|
82
|
-
centralHeader.writeUInt32LE(0x02014b50, 0)
|
|
83
|
-
centralHeader.writeUInt16LE(20, 4)
|
|
84
|
-
centralHeader.writeUInt16LE(20, 6)
|
|
85
|
-
centralHeader.writeUInt16LE(0, 8)
|
|
66
|
+
centralHeader.writeUInt32LE(0x02014b50, 0)
|
|
67
|
+
centralHeader.writeUInt16LE(20, 4)
|
|
68
|
+
centralHeader.writeUInt16LE(20, 6)
|
|
69
|
+
centralHeader.writeUInt16LE(0, 8)
|
|
86
70
|
centralHeader.writeUInt16LE(method, 10)
|
|
87
71
|
centralHeader.writeUInt16LE(dosTime, 12)
|
|
88
72
|
centralHeader.writeUInt16LE(dosDate, 14)
|
|
@@ -90,12 +74,12 @@ export function zip(files, callback) {
|
|
|
90
74
|
centralHeader.writeUInt32LE(compressed.length, 20)
|
|
91
75
|
centralHeader.writeUInt32LE(data.length, 24)
|
|
92
76
|
centralHeader.writeUInt16LE(nameBuf.length, 28)
|
|
93
|
-
centralHeader.writeUInt16LE(0, 30)
|
|
94
|
-
centralHeader.writeUInt16LE(0, 32)
|
|
95
|
-
centralHeader.writeUInt16LE(0, 34)
|
|
96
|
-
centralHeader.writeUInt16LE(0, 36)
|
|
97
|
-
centralHeader.writeUInt32LE(0, 38)
|
|
98
|
-
centralHeader.writeUInt32LE(offset, 42)
|
|
77
|
+
centralHeader.writeUInt16LE(0, 30)
|
|
78
|
+
centralHeader.writeUInt16LE(0, 32)
|
|
79
|
+
centralHeader.writeUInt16LE(0, 34)
|
|
80
|
+
centralHeader.writeUInt16LE(0, 36)
|
|
81
|
+
centralHeader.writeUInt32LE(0, 38)
|
|
82
|
+
centralHeader.writeUInt32LE(offset, 42)
|
|
99
83
|
|
|
100
84
|
centralChunks.push(centralHeader, nameBuf)
|
|
101
85
|
|
|
@@ -107,14 +91,14 @@ export function zip(files, callback) {
|
|
|
107
91
|
const fileCount = Object.keys(files).length
|
|
108
92
|
|
|
109
93
|
const eocd = Buffer.alloc(22)
|
|
110
|
-
eocd.writeUInt32LE(0x06054b50, 0)
|
|
111
|
-
eocd.writeUInt16LE(0, 4)
|
|
112
|
-
eocd.writeUInt16LE(0, 6)
|
|
113
|
-
eocd.writeUInt16LE(fileCount, 8)
|
|
114
|
-
eocd.writeUInt16LE(fileCount, 10)
|
|
94
|
+
eocd.writeUInt32LE(0x06054b50, 0)
|
|
95
|
+
eocd.writeUInt16LE(0, 4)
|
|
96
|
+
eocd.writeUInt16LE(0, 6)
|
|
97
|
+
eocd.writeUInt16LE(fileCount, 8)
|
|
98
|
+
eocd.writeUInt16LE(fileCount, 10)
|
|
115
99
|
eocd.writeUInt32LE(centralDirSize, 12)
|
|
116
100
|
eocd.writeUInt32LE(centralDirOffset, 16)
|
|
117
|
-
eocd.writeUInt16LE(0, 20)
|
|
101
|
+
eocd.writeUInt16LE(0, 20)
|
|
118
102
|
|
|
119
103
|
const result = Buffer.concat([...localChunks, ...centralChunks, eocd])
|
|
120
104
|
callback(null, result)
|
package/src/index.js
CHANGED
|
@@ -1,41 +1,31 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* baileys-lite entry point. Mirrors the original lib/index.js export surface
|
|
3
|
-
* (Socket/index.js + the Utils/Types/Defaults/WABinary/WAM/WAUSync barrels),
|
|
4
|
-
* pointing at the consolidated files.
|
|
5
|
-
*/
|
|
6
1
|
import { DEFAULT_CONNECTION_CONFIG } from './defaults.js'
|
|
7
2
|
import { makeCommunitiesSocket } from './socket/business-communities.js'
|
|
8
3
|
|
|
9
|
-
// export the last socket layer (Socket/index.js in the original)
|
|
10
4
|
const makeWASocket = config => {
|
|
11
5
|
const newConfig = {
|
|
12
6
|
...DEFAULT_CONNECTION_CONFIG,
|
|
13
7
|
...config,
|
|
14
|
-
|
|
15
|
-
// caller passing `logger: undefined` (e.g. a stale/missing logger ref
|
|
16
|
-
// on reconnect) would silently wipe out DEFAULT_CONNECTION_CONFIG's
|
|
17
|
-
// logger and crash later in makeNoiseHandler's `logger.child(...)`.
|
|
18
|
-
// Guard against that here, once, at the single entry point.
|
|
8
|
+
|
|
19
9
|
logger: config?.logger ?? DEFAULT_CONNECTION_CONFIG.logger
|
|
20
10
|
}
|
|
21
11
|
return makeCommunitiesSocket(newConfig)
|
|
22
12
|
}
|
|
23
13
|
|
|
24
|
-
// protobuf (was ../WAProto/index.js)
|
|
25
14
|
export * from '../WAProto/index.js'
|
|
26
15
|
|
|
27
|
-
// was Types/
|
|
28
16
|
export * from './constants.js'
|
|
29
|
-
|
|
17
|
+
|
|
30
18
|
export * from './defaults.js'
|
|
31
|
-
|
|
19
|
+
|
|
32
20
|
export * from './binary/wa-binary.js'
|
|
33
|
-
|
|
21
|
+
|
|
34
22
|
export * from './wam/wam.js'
|
|
35
23
|
export * from './wam/wam-constants.js'
|
|
36
|
-
|
|
24
|
+
|
|
37
25
|
export * from './socket/usync.js'
|
|
38
|
-
|
|
26
|
+
|
|
27
|
+
export * from './foundation/ai-rich.js'
|
|
28
|
+
export * from './foundation/qrcode-terminal.js'
|
|
39
29
|
export * from './utils/wa-protocol-core.js'
|
|
40
30
|
export * from './utils/auth-state-core.js'
|
|
41
31
|
export * from './utils/auth-state-storage.js'
|
package/src/signal/libsignal.js
CHANGED
|
@@ -1,20 +1,6 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Native Signal protocol implementation (X3DH + Double Ratchet, 1:1 sessions).
|
|
3
|
-
*
|
|
4
|
-
* Replaces the `libsignal` npm package (WhiskeySockets/libsignal-node 6.0.0),
|
|
5
|
-
* together with its dependencies `curve25519-js` and `protobufjs`.
|
|
6
|
-
* It is a faithful port: same wire format, same session serialization, same
|
|
7
|
-
* error classes and same public API, so existing auth-state / session data
|
|
8
|
-
* stays compatible. Verified against the original package in
|
|
9
|
-
* test/native-deps.mjs (cross-encrypt / decrypt in both directions).
|
|
10
|
-
*/
|
|
11
1
|
import * as nodeCrypto from 'node:crypto'
|
|
12
2
|
import { publicFromPrivate, randomX25519, sharedKey, sign as xeddsaSign, verify as xeddsaVerify } from '../foundation/curve25519.js'
|
|
13
3
|
|
|
14
|
-
/* ------------------------------------------------------------------ */
|
|
15
|
-
/* errors */
|
|
16
|
-
/* ------------------------------------------------------------------ */
|
|
17
|
-
|
|
18
4
|
export class SignalError extends Error {}
|
|
19
5
|
|
|
20
6
|
export class UntrustedIdentityKeyError extends SignalError {
|
|
@@ -47,10 +33,6 @@ export class PreKeyError extends SessionError {
|
|
|
47
33
|
}
|
|
48
34
|
}
|
|
49
35
|
|
|
50
|
-
/* ------------------------------------------------------------------ */
|
|
51
|
-
/* crypto */
|
|
52
|
-
/* ------------------------------------------------------------------ */
|
|
53
|
-
|
|
54
36
|
const assertBuffer = value => {
|
|
55
37
|
if (!(value instanceof Buffer)) throw TypeError(`Expected Buffer instead of: ${value.constructor.name}`)
|
|
56
38
|
return value
|
|
@@ -80,9 +62,9 @@ export const crypto = {
|
|
|
80
62
|
assertBuffer(data)
|
|
81
63
|
return nodeCrypto.createHash('sha512').update(data).digest()
|
|
82
64
|
},
|
|
83
|
-
|
|
65
|
+
|
|
84
66
|
deriveSecrets(input, salt, info, chunks) {
|
|
85
|
-
|
|
67
|
+
|
|
86
68
|
assertBuffer(input)
|
|
87
69
|
assertBuffer(salt)
|
|
88
70
|
assertBuffer(info)
|
|
@@ -113,10 +95,6 @@ export const crypto = {
|
|
|
113
95
|
}
|
|
114
96
|
}
|
|
115
97
|
|
|
116
|
-
/* ------------------------------------------------------------------ */
|
|
117
|
-
/* curve (33-byte "DJB type 5" public keys, 32-byte private keys) */
|
|
118
|
-
/* ------------------------------------------------------------------ */
|
|
119
|
-
|
|
120
98
|
const KEY_BUNDLE_TYPE = Buffer.from([5])
|
|
121
99
|
const prefixKeyInPublicKey = pubKey => Buffer.concat([KEY_BUNDLE_TYPE, pubKey])
|
|
122
100
|
|
|
@@ -138,9 +116,9 @@ const scrubPubKeyFormat = pubKey => {
|
|
|
138
116
|
|
|
139
117
|
const unclampEd25519PrivateKey = clampedSk => {
|
|
140
118
|
const unclampedSk = new Uint8Array(clampedSk)
|
|
141
|
-
unclampedSk[0] |= 6
|
|
142
|
-
unclampedSk[31] |= 128
|
|
143
|
-
unclampedSk[31] &= ~64
|
|
119
|
+
unclampedSk[0] |= 6
|
|
120
|
+
unclampedSk[31] |= 128
|
|
121
|
+
unclampedSk[31] &= ~64
|
|
144
122
|
return unclampedSk
|
|
145
123
|
}
|
|
146
124
|
|
|
@@ -172,10 +150,6 @@ export const curve = {
|
|
|
172
150
|
}
|
|
173
151
|
}
|
|
174
152
|
|
|
175
|
-
/* ------------------------------------------------------------------ */
|
|
176
|
-
/* keyhelper */
|
|
177
|
-
/* ------------------------------------------------------------------ */
|
|
178
|
-
|
|
179
153
|
const isNonNegativeInteger = n => typeof n === 'number' && n % 1 === 0 && n >= 0
|
|
180
154
|
|
|
181
155
|
export const keyhelper = {
|
|
@@ -204,10 +178,6 @@ export const keyhelper = {
|
|
|
204
178
|
}
|
|
205
179
|
}
|
|
206
180
|
|
|
207
|
-
/* ------------------------------------------------------------------ */
|
|
208
|
-
/* minimal protobuf (proto2 varint + length-delimited only) */
|
|
209
|
-
/* ------------------------------------------------------------------ */
|
|
210
|
-
|
|
211
181
|
const writeVarint = (out, n) => {
|
|
212
182
|
n = n >>> 0
|
|
213
183
|
while (n > 127) {
|
|
@@ -224,7 +194,7 @@ class PbReader {
|
|
|
224
194
|
this.len = buf.length
|
|
225
195
|
}
|
|
226
196
|
varint() {
|
|
227
|
-
|
|
197
|
+
|
|
228
198
|
let value = 0
|
|
229
199
|
for (let i = 0; i < 5; i++) {
|
|
230
200
|
if (this.pos >= this.len) throw RangeError(`index out of range: ${this.pos} + 1 > ${this.len}`)
|
|
@@ -232,7 +202,7 @@ class PbReader {
|
|
|
232
202
|
value = (value | ((b & 127) << (7 * i))) >>> 0
|
|
233
203
|
if (b < 128) return value
|
|
234
204
|
}
|
|
235
|
-
|
|
205
|
+
|
|
236
206
|
for (let i = 5; i < 10; i++) {
|
|
237
207
|
if (this.pos >= this.len) throw RangeError(`index out of range: ${this.pos} + 1 > ${this.len}`)
|
|
238
208
|
if (this.buf[this.pos++] < 128) return value
|
|
@@ -256,7 +226,7 @@ class PbReader {
|
|
|
256
226
|
this.pos += 8
|
|
257
227
|
break
|
|
258
228
|
case 2: {
|
|
259
|
-
|
|
229
|
+
|
|
260
230
|
const skip = this.varint()
|
|
261
231
|
this.pos += skip
|
|
262
232
|
break
|
|
@@ -271,7 +241,6 @@ class PbReader {
|
|
|
271
241
|
}
|
|
272
242
|
}
|
|
273
243
|
|
|
274
|
-
/** Build a tiny message class: fields = [[name, fieldNo, 'bytes'|'uint32', encodeOrder]] */
|
|
275
244
|
const defineMessage = (fields, defaults) => {
|
|
276
245
|
const Msg = function (props) {
|
|
277
246
|
if (props) for (const k of Object.keys(props)) if (props[k] != null) this[k] = props[k]
|
|
@@ -281,7 +250,7 @@ const defineMessage = (fields, defaults) => {
|
|
|
281
250
|
Msg.encode = message => {
|
|
282
251
|
const out = []
|
|
283
252
|
for (const f of fields) {
|
|
284
|
-
|
|
253
|
+
|
|
285
254
|
if (message[f.name] != null && Object.prototype.hasOwnProperty.call(message, f.name)) {
|
|
286
255
|
if (f.type === 'uint32') {
|
|
287
256
|
writeVarint(out, (f.no << 3) | 0)
|
|
@@ -312,7 +281,6 @@ const defineMessage = (fields, defaults) => {
|
|
|
312
281
|
return Msg
|
|
313
282
|
}
|
|
314
283
|
|
|
315
|
-
// textsecure.WhisperMessage
|
|
316
284
|
export const WhisperMessage = defineMessage(
|
|
317
285
|
[
|
|
318
286
|
{ name: 'ephemeralKey', no: 1, type: 'bytes' },
|
|
@@ -323,7 +291,6 @@ export const WhisperMessage = defineMessage(
|
|
|
323
291
|
{ ephemeralKey: Buffer.alloc(0), counter: 0, previousCounter: 0, ciphertext: Buffer.alloc(0) }
|
|
324
292
|
)
|
|
325
293
|
|
|
326
|
-
// textsecure.PreKeyWhisperMessage
|
|
327
294
|
export const PreKeyWhisperMessage = defineMessage(
|
|
328
295
|
[
|
|
329
296
|
{ name: 'preKeyId', no: 1, type: 'uint32' },
|
|
@@ -343,10 +310,6 @@ export const PreKeyWhisperMessage = defineMessage(
|
|
|
343
310
|
}
|
|
344
311
|
)
|
|
345
312
|
|
|
346
|
-
/* ------------------------------------------------------------------ */
|
|
347
|
-
/* ProtocolAddress */
|
|
348
|
-
/* ------------------------------------------------------------------ */
|
|
349
|
-
|
|
350
313
|
export class ProtocolAddress {
|
|
351
314
|
static from(encodedAddress) {
|
|
352
315
|
if (typeof encodedAddress !== 'string' || !encodedAddress.match(/.*\.\d+/)) throw new Error('Invalid address encoding')
|
|
@@ -369,17 +332,13 @@ export class ProtocolAddress {
|
|
|
369
332
|
}
|
|
370
333
|
}
|
|
371
334
|
|
|
372
|
-
/* ------------------------------------------------------------------ */
|
|
373
|
-
/* job queue (serializes session I/O per address) */
|
|
374
|
-
/* ------------------------------------------------------------------ */
|
|
375
|
-
|
|
376
335
|
const _queueAsyncBuckets = new Map()
|
|
377
336
|
const _gcLimit = 10000
|
|
378
337
|
|
|
379
338
|
async function _asyncQueueExecutor(queue, cleanup) {
|
|
380
339
|
let offt = 0
|
|
381
340
|
while (true) {
|
|
382
|
-
const limit = Math.min(queue.length, _gcLimit)
|
|
341
|
+
const limit = Math.min(queue.length, _gcLimit)
|
|
383
342
|
for (let i = offt; i < limit; i++) {
|
|
384
343
|
const job = queue[i]
|
|
385
344
|
try {
|
|
@@ -389,7 +348,7 @@ async function _asyncQueueExecutor(queue, cleanup) {
|
|
|
389
348
|
}
|
|
390
349
|
}
|
|
391
350
|
if (limit < queue.length) {
|
|
392
|
-
|
|
351
|
+
|
|
393
352
|
if (limit >= _gcLimit) {
|
|
394
353
|
queue.splice(0, limit)
|
|
395
354
|
offt = 0
|
|
@@ -415,10 +374,6 @@ const queueJob = (bucket, awaitable) => {
|
|
|
415
374
|
return job
|
|
416
375
|
}
|
|
417
376
|
|
|
418
|
-
/* ------------------------------------------------------------------ */
|
|
419
|
-
/* SessionRecord / SessionEntry */
|
|
420
|
-
/* ------------------------------------------------------------------ */
|
|
421
|
-
|
|
422
377
|
const BaseKeyType = { OURS: 1, THEIRS: 2 }
|
|
423
378
|
const ChainType = { SENDING: 1, RECEIVING: 2 }
|
|
424
379
|
const CLOSED_SESSIONS_MAX = 40
|
|
@@ -617,7 +572,7 @@ export class SessionRecord {
|
|
|
617
572
|
this.sessions[session.indexInfo.baseKey.toString('base64')] = session
|
|
618
573
|
}
|
|
619
574
|
getSessions() {
|
|
620
|
-
|
|
575
|
+
|
|
621
576
|
return Array.from(Object.values(this.sessions)).sort((a, b) => {
|
|
622
577
|
const aUsed = a.indexInfo.used || 0
|
|
623
578
|
const bUsed = b.indexInfo.used || 0
|
|
@@ -663,10 +618,6 @@ export class SessionRecord {
|
|
|
663
618
|
}
|
|
664
619
|
}
|
|
665
620
|
|
|
666
|
-
/* ------------------------------------------------------------------ */
|
|
667
|
-
/* SessionBuilder (X3DH) */
|
|
668
|
-
/* ------------------------------------------------------------------ */
|
|
669
|
-
|
|
670
621
|
export class SessionBuilder {
|
|
671
622
|
constructor(storage, protocolAddress) {
|
|
672
623
|
this.addr = protocolAddress
|
|
@@ -711,7 +662,7 @@ export class SessionBuilder {
|
|
|
711
662
|
throw new UntrustedIdentityKeyError(this.addr.id, message.identityKey)
|
|
712
663
|
}
|
|
713
664
|
if (record.getSession(message.baseKey)) {
|
|
714
|
-
|
|
665
|
+
|
|
715
666
|
return
|
|
716
667
|
}
|
|
717
668
|
const preKeyPair = await this.storage.loadPreKey(message.preKeyId)
|
|
@@ -775,8 +726,7 @@ export class SessionBuilder {
|
|
|
775
726
|
closed: -1
|
|
776
727
|
}
|
|
777
728
|
if (isInitiator) {
|
|
778
|
-
|
|
779
|
-
// otherwise we figure it out when we first maybeStepRatchet with the remote's ephemeral key
|
|
729
|
+
|
|
780
730
|
this.calculateSendingRatchet(session, theirSignedPubKey)
|
|
781
731
|
}
|
|
782
732
|
return session
|
|
@@ -795,10 +745,6 @@ export class SessionBuilder {
|
|
|
795
745
|
}
|
|
796
746
|
}
|
|
797
747
|
|
|
798
|
-
/* ------------------------------------------------------------------ */
|
|
799
|
-
/* SessionCipher (Double Ratchet) */
|
|
800
|
-
/* ------------------------------------------------------------------ */
|
|
801
|
-
|
|
802
748
|
const VERSION = 3
|
|
803
749
|
|
|
804
750
|
export class SessionCipher {
|
|
@@ -872,7 +818,7 @@ export class SessionCipher {
|
|
|
872
818
|
await this.storeRecord(record)
|
|
873
819
|
let type, body
|
|
874
820
|
if (session.pendingPreKey) {
|
|
875
|
-
type = 3
|
|
821
|
+
type = 3
|
|
876
822
|
const preKeyMsg = PreKeyWhisperMessage.create({
|
|
877
823
|
identityKey: ourIdentityKey.pubKey,
|
|
878
824
|
registrationId: await this.storage.getOurRegistrationId(),
|
|
@@ -886,7 +832,7 @@ export class SessionCipher {
|
|
|
886
832
|
Buffer.from(PreKeyWhisperMessage.encode(preKeyMsg).finish())
|
|
887
833
|
])
|
|
888
834
|
} else {
|
|
889
|
-
type = 1
|
|
835
|
+
type = 1
|
|
890
836
|
body = result
|
|
891
837
|
}
|
|
892
838
|
return { type, body, registrationId: session.registrationId }
|
|
@@ -894,8 +840,7 @@ export class SessionCipher {
|
|
|
894
840
|
}
|
|
895
841
|
|
|
896
842
|
async decryptWithSessions(data, sessions) {
|
|
897
|
-
|
|
898
|
-
// Stop and return the result if we get a valid result.
|
|
843
|
+
|
|
899
844
|
if (!sessions.length) throw new SessionError('No sessions available')
|
|
900
845
|
const errs = []
|
|
901
846
|
for (const session of sessions) {
|
|
@@ -924,7 +869,7 @@ export class SessionCipher {
|
|
|
924
869
|
throw new UntrustedIdentityKeyError(this.addr.id, remoteIdentityKey)
|
|
925
870
|
}
|
|
926
871
|
if (record.isClosed(result.session)) {
|
|
927
|
-
|
|
872
|
+
|
|
928
873
|
console.warn('Decrypted message with closed session.')
|
|
929
874
|
}
|
|
930
875
|
await this.storeRecord(record)
|
|
@@ -965,7 +910,7 @@ export class SessionCipher {
|
|
|
965
910
|
if (chain.chainType === ChainType.SENDING) throw new Error('Tried to decrypt on a sending chain')
|
|
966
911
|
this.fillMessageKeys(chain, message.counter)
|
|
967
912
|
if (!Object.prototype.hasOwnProperty.call(chain.messageKeys, message.counter)) {
|
|
968
|
-
|
|
913
|
+
|
|
969
914
|
throw new MessageCounterError('Key used already or never filled')
|
|
970
915
|
}
|
|
971
916
|
const messageKey = chain.messageKeys[message.counter]
|
|
@@ -977,7 +922,7 @@ export class SessionCipher {
|
|
|
977
922
|
macInput.set(ourIdentityKey.pubKey, 33)
|
|
978
923
|
macInput[33 * 2] = this._encodeTupleByte(VERSION, VERSION)
|
|
979
924
|
macInput.set(messageProto, 33 * 2 + 1)
|
|
980
|
-
|
|
925
|
+
|
|
981
926
|
crypto.verifyMAC(macInput, keys[1], messageBuffer.slice(-8), 8)
|
|
982
927
|
const plaintext = crypto.decrypt(keys[0], message.ciphertext, keys[2].slice(0, 16))
|
|
983
928
|
delete session.pendingPreKey
|
|
@@ -1001,10 +946,10 @@ export class SessionCipher {
|
|
|
1001
946
|
const previousRatchet = session.getChain(ratchet.lastRemoteEphemeralKey)
|
|
1002
947
|
if (previousRatchet) {
|
|
1003
948
|
this.fillMessageKeys(previousRatchet, previousCounter)
|
|
1004
|
-
delete previousRatchet.chainKey.key
|
|
949
|
+
delete previousRatchet.chainKey.key
|
|
1005
950
|
}
|
|
1006
951
|
this.calculateRatchet(session, remoteKey, false)
|
|
1007
|
-
|
|
952
|
+
|
|
1008
953
|
const prevCounter = session.getChain(ratchet.ephemeralKeyPair.pubKey)
|
|
1009
954
|
if (prevCounter) {
|
|
1010
955
|
ratchet.previousCounter = prevCounter.chainKey.counter
|
|
@@ -1018,7 +963,7 @@ export class SessionCipher {
|
|
|
1018
963
|
calculateRatchet(session, remoteKey, sending) {
|
|
1019
964
|
const ratchet = session.currentRatchet
|
|
1020
965
|
const sharedSecret = curve.calculateAgreement(remoteKey, ratchet.ephemeralKeyPair.privKey)
|
|
1021
|
-
const masterKey = crypto.deriveSecrets(sharedSecret, ratchet.rootKey, Buffer.from('WhisperRatchet'),
|
|
966
|
+
const masterKey = crypto.deriveSecrets(sharedSecret, ratchet.rootKey, Buffer.from('WhisperRatchet'), 2)
|
|
1022
967
|
const chainKey = sending ? ratchet.ephemeralKeyPair.pubKey : remoteKey
|
|
1023
968
|
session.addChain(chainKey, {
|
|
1024
969
|
messageKeys: {},
|
|
@@ -1,10 +1,3 @@
|
|
|
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
1
|
import * as nodeCrypto from 'node:crypto'
|
|
9
2
|
import { crypto as signalCrypto, curve as signalCurve } from './libsignal.js'
|
|
10
3
|
|
|
@@ -13,10 +6,6 @@ const { calculateSignature, generateKeyPair, verifySignature } = signalCurve
|
|
|
13
6
|
import { proto } from '../../WAProto/index.js'
|
|
14
7
|
import { BufferJSON } from '../utils/wa-protocol-core.js'
|
|
15
8
|
|
|
16
|
-
/* ------------------------------------------------------------------ */
|
|
17
|
-
/* CiphertextMessage — shared constants base class */
|
|
18
|
-
/* ------------------------------------------------------------------ */
|
|
19
|
-
|
|
20
9
|
export class CiphertextMessage {
|
|
21
10
|
constructor() {
|
|
22
11
|
this.UNSUPPORTED_VERSION = 1
|
|
@@ -29,10 +18,6 @@ export class CiphertextMessage {
|
|
|
29
18
|
}
|
|
30
19
|
}
|
|
31
20
|
|
|
32
|
-
/* ------------------------------------------------------------------ */
|
|
33
|
-
/* keyhelper */
|
|
34
|
-
/* ------------------------------------------------------------------ */
|
|
35
|
-
|
|
36
21
|
export const keyhelper = {
|
|
37
22
|
generateSenderKey() {
|
|
38
23
|
return nodeCrypto.randomBytes(32)
|
|
@@ -49,10 +34,6 @@ export const keyhelper = {
|
|
|
49
34
|
}
|
|
50
35
|
}
|
|
51
36
|
|
|
52
|
-
/* ------------------------------------------------------------------ */
|
|
53
|
-
/* SenderKeyName */
|
|
54
|
-
/* ------------------------------------------------------------------ */
|
|
55
|
-
|
|
56
37
|
function isNull(str) {
|
|
57
38
|
return str === null || str === ''
|
|
58
39
|
}
|
|
@@ -99,10 +80,6 @@ export class SenderKeyName {
|
|
|
99
80
|
}
|
|
100
81
|
}
|
|
101
82
|
|
|
102
|
-
/* ------------------------------------------------------------------ */
|
|
103
|
-
/* SenderMessageKey / SenderChainKey */
|
|
104
|
-
/* ------------------------------------------------------------------ */
|
|
105
|
-
|
|
106
83
|
export class SenderMessageKey {
|
|
107
84
|
constructor(iteration, seed) {
|
|
108
85
|
const derivative = deriveSecrets(seed, Buffer.alloc(32), Buffer.from('WhisperGroup'))
|
|
@@ -152,10 +129,6 @@ export class SenderChainKey {
|
|
|
152
129
|
}
|
|
153
130
|
}
|
|
154
131
|
|
|
155
|
-
/* ------------------------------------------------------------------ */
|
|
156
|
-
/* SenderKeyState / SenderKeyRecord */
|
|
157
|
-
/* ------------------------------------------------------------------ */
|
|
158
|
-
|
|
159
132
|
export class SenderKeyState {
|
|
160
133
|
constructor(id, iteration, chainKey, signatureKeyPair, signatureKeyPublic, signatureKeyPrivate, senderKeyStateStructure) {
|
|
161
134
|
this.MAX_MESSAGE_KEYS = 2000
|
|
@@ -272,10 +245,6 @@ export class SenderKeyRecord {
|
|
|
272
245
|
}
|
|
273
246
|
}
|
|
274
247
|
|
|
275
|
-
/* ------------------------------------------------------------------ */
|
|
276
|
-
/* SenderKeyDistributionMessage / SenderKeyMessage (wire formats) */
|
|
277
|
-
/* ------------------------------------------------------------------ */
|
|
278
|
-
|
|
279
248
|
export class SenderKeyDistributionMessage extends CiphertextMessage {
|
|
280
249
|
constructor(id, iteration, chainKey, signatureKey, serialized) {
|
|
281
250
|
super()
|
|
@@ -385,10 +354,6 @@ export class SenderKeyMessage extends CiphertextMessage {
|
|
|
385
354
|
}
|
|
386
355
|
}
|
|
387
356
|
|
|
388
|
-
/* ------------------------------------------------------------------ */
|
|
389
|
-
/* GroupSessionBuilder / GroupCipher */
|
|
390
|
-
/* ------------------------------------------------------------------ */
|
|
391
|
-
|
|
392
357
|
export class GroupSessionBuilder {
|
|
393
358
|
constructor(senderKeyStore) {
|
|
394
359
|
this.senderKeyStore = senderKeyStore
|