@nexustechpro/baileys 2.0.1 → 2.0.5
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 +1 -1
- package/README.md +924 -1299
- package/lib/Defaults/baileys-version.json +6 -2
- package/lib/Defaults/index.js +172 -172
- package/lib/Signal/libsignal.js +380 -292
- package/lib/Signal/lid-mapping.js +264 -171
- package/lib/Socket/Client/index.js +2 -2
- package/lib/Socket/Client/types.js +10 -10
- package/lib/Socket/Client/websocket.js +45 -310
- package/lib/Socket/business.js +375 -375
- package/lib/Socket/chats.js +909 -963
- package/lib/Socket/communities.js +430 -430
- package/lib/Socket/groups.js +342 -342
- package/lib/Socket/index.js +22 -22
- package/lib/Socket/messages-recv.js +777 -743
- package/lib/Socket/messages-send.js +667 -393
- package/lib/Socket/mex.js +50 -50
- package/lib/Socket/newsletter.js +148 -148
- package/lib/Socket/nexus-handler.js +75 -261
- package/lib/Socket/socket.js +709 -1201
- package/lib/Store/index.js +5 -5
- package/lib/Store/make-cache-manager-store.js +81 -81
- package/lib/Store/make-in-memory-store.js +416 -416
- package/lib/Store/make-ordered-dictionary.js +81 -81
- package/lib/Store/object-repository.js +30 -30
- package/lib/Types/Auth.js +1 -1
- package/lib/Types/Bussines.js +1 -1
- package/lib/Types/Call.js +1 -1
- package/lib/Types/Chat.js +7 -7
- package/lib/Types/Contact.js +1 -1
- package/lib/Types/Events.js +1 -1
- package/lib/Types/GroupMetadata.js +1 -1
- package/lib/Types/Label.js +24 -24
- package/lib/Types/LabelAssociation.js +6 -6
- package/lib/Types/Message.js +10 -10
- package/lib/Types/Newsletter.js +28 -28
- package/lib/Types/Product.js +1 -1
- package/lib/Types/Signal.js +1 -1
- package/lib/Types/Socket.js +2 -2
- package/lib/Types/State.js +12 -12
- package/lib/Types/USync.js +1 -1
- package/lib/Types/index.js +25 -25
- package/lib/Utils/auth-utils.js +264 -256
- package/lib/Utils/baileys-event-stream.js +55 -55
- package/lib/Utils/browser-utils.js +27 -27
- package/lib/Utils/business.js +228 -230
- package/lib/Utils/chat-utils.js +694 -764
- package/lib/Utils/crypto.js +109 -135
- package/lib/Utils/decode-wa-message.js +310 -314
- package/lib/Utils/event-buffer.js +547 -547
- package/lib/Utils/generics.js +297 -297
- package/lib/Utils/history.js +91 -83
- package/lib/Utils/index.js +21 -20
- package/lib/Utils/key-store.js +17 -0
- package/lib/Utils/link-preview.js +97 -88
- package/lib/Utils/logger.js +2 -2
- package/lib/Utils/lt-hash.js +47 -47
- package/lib/Utils/make-mutex.js +39 -39
- package/lib/Utils/message-retry-manager.js +148 -148
- package/lib/Utils/messages-media.js +534 -532
- package/lib/Utils/messages.js +705 -705
- package/lib/Utils/noise-handler.js +255 -255
- package/lib/Utils/pre-key-manager.js +105 -105
- package/lib/Utils/process-message.js +412 -412
- package/lib/Utils/signal.js +160 -158
- package/lib/Utils/use-multi-file-auth-state.js +120 -120
- package/lib/Utils/validate-connection.js +194 -194
- package/lib/WABinary/constants.js +1300 -1300
- package/lib/WABinary/decode.js +237 -237
- package/lib/WABinary/encode.js +232 -232
- package/lib/WABinary/generic-utils.js +252 -211
- package/lib/WABinary/index.js +5 -5
- package/lib/WABinary/jid-utils.js +279 -95
- package/lib/WABinary/types.js +1 -1
- package/lib/WAM/BinaryInfo.js +9 -9
- package/lib/WAM/constants.js +22852 -22852
- package/lib/WAM/encode.js +149 -149
- package/lib/WAM/index.js +3 -3
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +28 -28
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -53
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -26
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +37 -37
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -50
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +28 -28
- package/lib/WAUSync/Protocols/index.js +4 -4
- package/lib/WAUSync/USyncQuery.js +93 -93
- package/lib/WAUSync/USyncUser.js +22 -22
- package/lib/WAUSync/index.js +3 -3
- package/lib/index.js +66 -66
- package/package.json +171 -144
- package/lib/Signal/Group/ciphertext-message.js +0 -12
- package/lib/Signal/Group/group-session-builder.js +0 -30
- package/lib/Signal/Group/group_cipher.js +0 -100
- package/lib/Signal/Group/index.js +0 -12
- package/lib/Signal/Group/keyhelper.js +0 -18
- package/lib/Signal/Group/sender-chain-key.js +0 -26
- package/lib/Signal/Group/sender-key-distribution-message.js +0 -63
- package/lib/Signal/Group/sender-key-message.js +0 -66
- package/lib/Signal/Group/sender-key-name.js +0 -48
- package/lib/Signal/Group/sender-key-record.js +0 -41
- package/lib/Signal/Group/sender-key-state.js +0 -84
- package/lib/Signal/Group/sender-message-key.js +0 -26
|
@@ -1,256 +1,256 @@
|
|
|
1
|
-
import { Boom } from '@hapi/boom';
|
|
2
|
-
import { proto } from '../../WAProto/index.js';
|
|
3
|
-
import { NOISE_MODE, WA_CERT_DETAILS } from '../Defaults/index.js';
|
|
4
|
-
import { decodeBinaryNode } from '../WABinary/index.js';
|
|
5
|
-
import { aesDecryptGCM, aesEncryptGCM, Curve, hkdf, sha256 } from './crypto.js';
|
|
6
|
-
|
|
7
|
-
const IV_LENGTH = 12;
|
|
8
|
-
const EMPTY_BUFFER = Buffer.alloc(0);
|
|
9
|
-
|
|
10
|
-
const generateIV = (counter) => {
|
|
11
|
-
const iv = new ArrayBuffer(IV_LENGTH);
|
|
12
|
-
new DataView(iv).setUint32(8, counter);
|
|
13
|
-
return new Uint8Array(iv);
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
class TransportState {
|
|
17
|
-
constructor(encKey, decKey) {
|
|
18
|
-
this.encKey = encKey;
|
|
19
|
-
this.decKey = decKey;
|
|
20
|
-
this.readCounter = 0;
|
|
21
|
-
this.writeCounter = 0;
|
|
22
|
-
this.iv = new Uint8Array(IV_LENGTH);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
encrypt(plaintext) {
|
|
26
|
-
const c = this.writeCounter++;
|
|
27
|
-
this.iv[8] = (c >>> 24) & 0xff;
|
|
28
|
-
this.iv[9] = (c >>> 16) & 0xff;
|
|
29
|
-
this.iv[10] = (c >>> 8) & 0xff;
|
|
30
|
-
this.iv[11] = c & 0xff;
|
|
31
|
-
|
|
32
|
-
return aesEncryptGCM(plaintext, this.encKey, this.iv, EMPTY_BUFFER);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
decrypt(ciphertext) {
|
|
36
|
-
const c = this.readCounter++;
|
|
37
|
-
this.iv[8] = (c >>> 24) & 0xff;
|
|
38
|
-
this.iv[9] = (c >>> 16) & 0xff;
|
|
39
|
-
this.iv[10] = (c >>> 8) & 0xff;
|
|
40
|
-
this.iv[11] = c & 0xff;
|
|
41
|
-
|
|
42
|
-
return aesDecryptGCM(ciphertext, this.decKey, this.iv, EMPTY_BUFFER);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export const makeNoiseHandler = ({
|
|
47
|
-
keyPair: { private: privateKey, public: publicKey },
|
|
48
|
-
NOISE_HEADER,
|
|
49
|
-
logger,
|
|
50
|
-
routingInfo
|
|
51
|
-
}) => {
|
|
52
|
-
logger = logger.child({ class: 'ns' });
|
|
53
|
-
|
|
54
|
-
const data = Buffer.from(NOISE_MODE);
|
|
55
|
-
let hash = data.byteLength === 32 ? data : sha256(data);
|
|
56
|
-
let salt = hash;
|
|
57
|
-
let encKey = hash;
|
|
58
|
-
let decKey = hash;
|
|
59
|
-
let counter = 0;
|
|
60
|
-
let sentIntro = false;
|
|
61
|
-
|
|
62
|
-
let inBytes = Buffer.alloc(0);
|
|
63
|
-
|
|
64
|
-
let transport = null;
|
|
65
|
-
let isWaitingForTransport = false;
|
|
66
|
-
let pendingOnFrame = null;
|
|
67
|
-
|
|
68
|
-
let introHeader;
|
|
69
|
-
if (routingInfo) {
|
|
70
|
-
introHeader = Buffer.alloc(7 + routingInfo.byteLength + NOISE_HEADER.length);
|
|
71
|
-
introHeader.write('ED', 0, 'utf8');
|
|
72
|
-
introHeader.writeUint8(0, 2);
|
|
73
|
-
introHeader.writeUint8(1, 3);
|
|
74
|
-
introHeader.writeUint8(routingInfo.byteLength >> 16, 4);
|
|
75
|
-
introHeader.writeUint16BE(routingInfo.byteLength & 65535, 5);
|
|
76
|
-
introHeader.set(routingInfo, 7);
|
|
77
|
-
introHeader.set(NOISE_HEADER, 7 + routingInfo.byteLength);
|
|
78
|
-
} else {
|
|
79
|
-
introHeader = Buffer.from(NOISE_HEADER);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const authenticate = (data) => {
|
|
83
|
-
if (!transport) {
|
|
84
|
-
hash = sha256(Buffer.concat([hash, data]));
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
const encrypt = (plaintext) => {
|
|
89
|
-
if (transport) {
|
|
90
|
-
return transport.encrypt(plaintext);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const result = aesEncryptGCM(plaintext, encKey, generateIV(counter++), hash);
|
|
94
|
-
authenticate(result);
|
|
95
|
-
return result;
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
const decrypt = (ciphertext) => {
|
|
99
|
-
if (transport) {
|
|
100
|
-
return transport.decrypt(ciphertext);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const result = aesDecryptGCM(ciphertext, decKey, generateIV(counter++), hash);
|
|
104
|
-
authenticate(ciphertext);
|
|
105
|
-
return result;
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
const localHKDF = (data) => {
|
|
109
|
-
const key = hkdf(Buffer.from(data), 64, { salt, info: '' });
|
|
110
|
-
return [key.subarray(0, 32), key.subarray(32)];
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
const mixIntoKey = (data) => {
|
|
114
|
-
const [write, read] = localHKDF(data);
|
|
115
|
-
salt = write;
|
|
116
|
-
encKey = read;
|
|
117
|
-
decKey = read;
|
|
118
|
-
counter = 0;
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
const finishInit = async () => {
|
|
122
|
-
isWaitingForTransport = true;
|
|
123
|
-
const [write, read] = localHKDF(new Uint8Array(0));
|
|
124
|
-
transport = new TransportState(write, read);
|
|
125
|
-
isWaitingForTransport = false;
|
|
126
|
-
|
|
127
|
-
logger.trace('Noise handler transitioned to Transport state');
|
|
128
|
-
|
|
129
|
-
if (pendingOnFrame) {
|
|
130
|
-
logger.trace({ length: inBytes.length }, 'Flushing buffered frames after transport ready');
|
|
131
|
-
await processData(pendingOnFrame);
|
|
132
|
-
pendingOnFrame = null;
|
|
133
|
-
}
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
const processData = async (onFrame) => {
|
|
137
|
-
let size;
|
|
138
|
-
|
|
139
|
-
while (true) {
|
|
140
|
-
if (inBytes.length < 3) return;
|
|
141
|
-
|
|
142
|
-
size = (inBytes[0] << 16) | (inBytes[1] << 8) | inBytes[2];
|
|
143
|
-
|
|
144
|
-
if (inBytes.length < size + 3) return;
|
|
145
|
-
|
|
146
|
-
let frame = inBytes.subarray(3, size + 3);
|
|
147
|
-
inBytes = inBytes.subarray(size + 3);
|
|
148
|
-
|
|
149
|
-
if (transport) {
|
|
150
|
-
const result = transport.decrypt(frame);
|
|
151
|
-
frame = await decodeBinaryNode(result);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
if (logger.level === 'trace') {
|
|
155
|
-
logger.trace({ msg: frame?.attrs?.id }, 'recv frame');
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
onFrame(frame);
|
|
159
|
-
}
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
authenticate(NOISE_HEADER);
|
|
163
|
-
authenticate(publicKey);
|
|
164
|
-
|
|
165
|
-
return {
|
|
166
|
-
encrypt,
|
|
167
|
-
decrypt,
|
|
168
|
-
authenticate,
|
|
169
|
-
mixIntoKey,
|
|
170
|
-
finishInit,
|
|
171
|
-
processHandshake: ({ serverHello }, noiseKey) => {
|
|
172
|
-
authenticate(serverHello.ephemeral);
|
|
173
|
-
mixIntoKey(Curve.sharedKey(privateKey, serverHello.ephemeral));
|
|
174
|
-
|
|
175
|
-
const decStaticContent = decrypt(serverHello.static);
|
|
176
|
-
mixIntoKey(Curve.sharedKey(privateKey, decStaticContent));
|
|
177
|
-
|
|
178
|
-
const certDecoded = decrypt(serverHello.payload);
|
|
179
|
-
|
|
180
|
-
const { intermediate: certIntermediate, leaf } = proto.CertChain.decode(certDecoded);
|
|
181
|
-
|
|
182
|
-
// Leaf certificate validation
|
|
183
|
-
if (!leaf?.details || !leaf?.signature) {
|
|
184
|
-
throw new Boom('invalid noise leaf certificate', { statusCode: 400 });
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
if (!certIntermediate?.details || !certIntermediate?.signature) {
|
|
188
|
-
throw new Boom('invalid noise intermediate certificate', { statusCode: 400 });
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
const details = proto.CertChain.NoiseCertificate.Details.decode(certIntermediate.details);
|
|
192
|
-
const { issuerSerial } = details;
|
|
193
|
-
|
|
194
|
-
const verify = Curve.verify(details.key, leaf.details, leaf.signature);
|
|
195
|
-
const verifyIntermediate = Curve.verify(
|
|
196
|
-
WA_CERT_DETAILS.PUBLIC_KEY,
|
|
197
|
-
certIntermediate.details,
|
|
198
|
-
certIntermediate.signature
|
|
199
|
-
);
|
|
200
|
-
|
|
201
|
-
if (!verify) {
|
|
202
|
-
throw new Boom('noise certificate signature invalid', { statusCode: 400 });
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
if (!verifyIntermediate) {
|
|
206
|
-
throw new Boom('noise intermediate certificate signature invalid', { statusCode: 400 });
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
if (issuerSerial !== WA_CERT_DETAILS.SERIAL) {
|
|
210
|
-
throw new Boom('certification match failed', { statusCode: 400 });
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
const keyEnc = encrypt(noiseKey.public);
|
|
214
|
-
mixIntoKey(Curve.sharedKey(noiseKey.private, serverHello.ephemeral));
|
|
215
|
-
|
|
216
|
-
return keyEnc;
|
|
217
|
-
},
|
|
218
|
-
encodeFrame: (data) => {
|
|
219
|
-
if (transport) {
|
|
220
|
-
data = transport.encrypt(data);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
const dataLen = data.byteLength;
|
|
224
|
-
const introSize = sentIntro ? 0 : introHeader.length;
|
|
225
|
-
const frame = Buffer.allocUnsafe(introSize + 3 + dataLen);
|
|
226
|
-
|
|
227
|
-
if (!sentIntro) {
|
|
228
|
-
frame.set(introHeader);
|
|
229
|
-
sentIntro = true;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
frame[introSize] = (dataLen >>> 16) & 0xff;
|
|
233
|
-
frame[introSize + 1] = (dataLen >>> 8) & 0xff;
|
|
234
|
-
frame[introSize + 2] = dataLen & 0xff;
|
|
235
|
-
|
|
236
|
-
frame.set(data, introSize + 3);
|
|
237
|
-
|
|
238
|
-
return frame;
|
|
239
|
-
},
|
|
240
|
-
decodeFrame: async (newData, onFrame) => {
|
|
241
|
-
if (isWaitingForTransport) {
|
|
242
|
-
inBytes = Buffer.concat([inBytes, newData]);
|
|
243
|
-
pendingOnFrame = onFrame;
|
|
244
|
-
return;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
if (inBytes.length === 0) {
|
|
248
|
-
inBytes = Buffer.from(newData);
|
|
249
|
-
} else {
|
|
250
|
-
inBytes = Buffer.concat([inBytes, newData]);
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
await processData(onFrame);
|
|
254
|
-
}
|
|
255
|
-
};
|
|
1
|
+
import { Boom } from '@hapi/boom';
|
|
2
|
+
import { proto } from '../../WAProto/index.js';
|
|
3
|
+
import { NOISE_MODE, WA_CERT_DETAILS } from '../Defaults/index.js';
|
|
4
|
+
import { decodeBinaryNode } from '../WABinary/index.js';
|
|
5
|
+
import { aesDecryptGCM, aesEncryptGCM, Curve, hkdf, sha256 } from './crypto.js';
|
|
6
|
+
|
|
7
|
+
const IV_LENGTH = 12;
|
|
8
|
+
const EMPTY_BUFFER = Buffer.alloc(0);
|
|
9
|
+
|
|
10
|
+
const generateIV = (counter) => {
|
|
11
|
+
const iv = new ArrayBuffer(IV_LENGTH);
|
|
12
|
+
new DataView(iv).setUint32(8, counter);
|
|
13
|
+
return new Uint8Array(iv);
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
class TransportState {
|
|
17
|
+
constructor(encKey, decKey) {
|
|
18
|
+
this.encKey = encKey;
|
|
19
|
+
this.decKey = decKey;
|
|
20
|
+
this.readCounter = 0;
|
|
21
|
+
this.writeCounter = 0;
|
|
22
|
+
this.iv = new Uint8Array(IV_LENGTH);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
encrypt(plaintext) {
|
|
26
|
+
const c = this.writeCounter++;
|
|
27
|
+
this.iv[8] = (c >>> 24) & 0xff;
|
|
28
|
+
this.iv[9] = (c >>> 16) & 0xff;
|
|
29
|
+
this.iv[10] = (c >>> 8) & 0xff;
|
|
30
|
+
this.iv[11] = c & 0xff;
|
|
31
|
+
|
|
32
|
+
return aesEncryptGCM(plaintext, this.encKey, this.iv, EMPTY_BUFFER);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
decrypt(ciphertext) {
|
|
36
|
+
const c = this.readCounter++;
|
|
37
|
+
this.iv[8] = (c >>> 24) & 0xff;
|
|
38
|
+
this.iv[9] = (c >>> 16) & 0xff;
|
|
39
|
+
this.iv[10] = (c >>> 8) & 0xff;
|
|
40
|
+
this.iv[11] = c & 0xff;
|
|
41
|
+
|
|
42
|
+
return aesDecryptGCM(ciphertext, this.decKey, this.iv, EMPTY_BUFFER);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export const makeNoiseHandler = ({
|
|
47
|
+
keyPair: { private: privateKey, public: publicKey },
|
|
48
|
+
NOISE_HEADER,
|
|
49
|
+
logger,
|
|
50
|
+
routingInfo
|
|
51
|
+
}) => {
|
|
52
|
+
logger = logger.child({ class: 'ns' });
|
|
53
|
+
|
|
54
|
+
const data = Buffer.from(NOISE_MODE);
|
|
55
|
+
let hash = data.byteLength === 32 ? data : sha256(data);
|
|
56
|
+
let salt = hash;
|
|
57
|
+
let encKey = hash;
|
|
58
|
+
let decKey = hash;
|
|
59
|
+
let counter = 0;
|
|
60
|
+
let sentIntro = false;
|
|
61
|
+
|
|
62
|
+
let inBytes = Buffer.alloc(0);
|
|
63
|
+
|
|
64
|
+
let transport = null;
|
|
65
|
+
let isWaitingForTransport = false;
|
|
66
|
+
let pendingOnFrame = null;
|
|
67
|
+
|
|
68
|
+
let introHeader;
|
|
69
|
+
if (routingInfo) {
|
|
70
|
+
introHeader = Buffer.alloc(7 + routingInfo.byteLength + NOISE_HEADER.length);
|
|
71
|
+
introHeader.write('ED', 0, 'utf8');
|
|
72
|
+
introHeader.writeUint8(0, 2);
|
|
73
|
+
introHeader.writeUint8(1, 3);
|
|
74
|
+
introHeader.writeUint8(routingInfo.byteLength >> 16, 4);
|
|
75
|
+
introHeader.writeUint16BE(routingInfo.byteLength & 65535, 5);
|
|
76
|
+
introHeader.set(routingInfo, 7);
|
|
77
|
+
introHeader.set(NOISE_HEADER, 7 + routingInfo.byteLength);
|
|
78
|
+
} else {
|
|
79
|
+
introHeader = Buffer.from(NOISE_HEADER);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const authenticate = (data) => {
|
|
83
|
+
if (!transport) {
|
|
84
|
+
hash = sha256(Buffer.concat([hash, data]));
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const encrypt = (plaintext) => {
|
|
89
|
+
if (transport) {
|
|
90
|
+
return transport.encrypt(plaintext);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const result = aesEncryptGCM(plaintext, encKey, generateIV(counter++), hash);
|
|
94
|
+
authenticate(result);
|
|
95
|
+
return result;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const decrypt = (ciphertext) => {
|
|
99
|
+
if (transport) {
|
|
100
|
+
return transport.decrypt(ciphertext);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const result = aesDecryptGCM(ciphertext, decKey, generateIV(counter++), hash);
|
|
104
|
+
authenticate(ciphertext);
|
|
105
|
+
return result;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const localHKDF = (data) => {
|
|
109
|
+
const key = hkdf(Buffer.from(data), 64, { salt, info: '' });
|
|
110
|
+
return [key.subarray(0, 32), key.subarray(32)];
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const mixIntoKey = (data) => {
|
|
114
|
+
const [write, read] = localHKDF(data);
|
|
115
|
+
salt = write;
|
|
116
|
+
encKey = read;
|
|
117
|
+
decKey = read;
|
|
118
|
+
counter = 0;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const finishInit = async () => {
|
|
122
|
+
isWaitingForTransport = true;
|
|
123
|
+
const [write, read] = localHKDF(new Uint8Array(0));
|
|
124
|
+
transport = new TransportState(write, read);
|
|
125
|
+
isWaitingForTransport = false;
|
|
126
|
+
|
|
127
|
+
logger.trace('Noise handler transitioned to Transport state');
|
|
128
|
+
|
|
129
|
+
if (pendingOnFrame) {
|
|
130
|
+
logger.trace({ length: inBytes.length }, 'Flushing buffered frames after transport ready');
|
|
131
|
+
await processData(pendingOnFrame);
|
|
132
|
+
pendingOnFrame = null;
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const processData = async (onFrame) => {
|
|
137
|
+
let size;
|
|
138
|
+
|
|
139
|
+
while (true) {
|
|
140
|
+
if (inBytes.length < 3) return;
|
|
141
|
+
|
|
142
|
+
size = (inBytes[0] << 16) | (inBytes[1] << 8) | inBytes[2];
|
|
143
|
+
|
|
144
|
+
if (inBytes.length < size + 3) return;
|
|
145
|
+
|
|
146
|
+
let frame = inBytes.subarray(3, size + 3);
|
|
147
|
+
inBytes = inBytes.subarray(size + 3);
|
|
148
|
+
|
|
149
|
+
if (transport) {
|
|
150
|
+
const result = transport.decrypt(frame);
|
|
151
|
+
frame = await decodeBinaryNode(result);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (logger.level === 'trace') {
|
|
155
|
+
logger.trace({ msg: frame?.attrs?.id }, 'recv frame');
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
onFrame(frame);
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
authenticate(NOISE_HEADER);
|
|
163
|
+
authenticate(publicKey);
|
|
164
|
+
|
|
165
|
+
return {
|
|
166
|
+
encrypt,
|
|
167
|
+
decrypt,
|
|
168
|
+
authenticate,
|
|
169
|
+
mixIntoKey,
|
|
170
|
+
finishInit,
|
|
171
|
+
processHandshake: ({ serverHello }, noiseKey) => {
|
|
172
|
+
authenticate(serverHello.ephemeral);
|
|
173
|
+
mixIntoKey(Curve.sharedKey(privateKey, serverHello.ephemeral));
|
|
174
|
+
|
|
175
|
+
const decStaticContent = decrypt(serverHello.static);
|
|
176
|
+
mixIntoKey(Curve.sharedKey(privateKey, decStaticContent));
|
|
177
|
+
|
|
178
|
+
const certDecoded = decrypt(serverHello.payload);
|
|
179
|
+
|
|
180
|
+
const { intermediate: certIntermediate, leaf } = proto.CertChain.decode(certDecoded);
|
|
181
|
+
|
|
182
|
+
// Leaf certificate validation
|
|
183
|
+
if (!leaf?.details || !leaf?.signature) {
|
|
184
|
+
throw new Boom('invalid noise leaf certificate', { statusCode: 400 });
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (!certIntermediate?.details || !certIntermediate?.signature) {
|
|
188
|
+
throw new Boom('invalid noise intermediate certificate', { statusCode: 400 });
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const details = proto.CertChain.NoiseCertificate.Details.decode(certIntermediate.details);
|
|
192
|
+
const { issuerSerial } = details;
|
|
193
|
+
|
|
194
|
+
const verify = Curve.verify(details.key, leaf.details, leaf.signature);
|
|
195
|
+
const verifyIntermediate = Curve.verify(
|
|
196
|
+
WA_CERT_DETAILS.PUBLIC_KEY,
|
|
197
|
+
certIntermediate.details,
|
|
198
|
+
certIntermediate.signature
|
|
199
|
+
);
|
|
200
|
+
|
|
201
|
+
if (!verify) {
|
|
202
|
+
throw new Boom('noise certificate signature invalid', { statusCode: 400 });
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (!verifyIntermediate) {
|
|
206
|
+
throw new Boom('noise intermediate certificate signature invalid', { statusCode: 400 });
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (issuerSerial !== WA_CERT_DETAILS.SERIAL) {
|
|
210
|
+
throw new Boom('certification match failed', { statusCode: 400 });
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const keyEnc = encrypt(noiseKey.public);
|
|
214
|
+
mixIntoKey(Curve.sharedKey(noiseKey.private, serverHello.ephemeral));
|
|
215
|
+
|
|
216
|
+
return keyEnc;
|
|
217
|
+
},
|
|
218
|
+
encodeFrame: (data) => {
|
|
219
|
+
if (transport) {
|
|
220
|
+
data = transport.encrypt(data);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const dataLen = data.byteLength;
|
|
224
|
+
const introSize = sentIntro ? 0 : introHeader.length;
|
|
225
|
+
const frame = Buffer.allocUnsafe(introSize + 3 + dataLen);
|
|
226
|
+
|
|
227
|
+
if (!sentIntro) {
|
|
228
|
+
frame.set(introHeader);
|
|
229
|
+
sentIntro = true;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
frame[introSize] = (dataLen >>> 16) & 0xff;
|
|
233
|
+
frame[introSize + 1] = (dataLen >>> 8) & 0xff;
|
|
234
|
+
frame[introSize + 2] = dataLen & 0xff;
|
|
235
|
+
|
|
236
|
+
frame.set(data, introSize + 3);
|
|
237
|
+
|
|
238
|
+
return frame;
|
|
239
|
+
},
|
|
240
|
+
decodeFrame: async (newData, onFrame) => {
|
|
241
|
+
if (isWaitingForTransport) {
|
|
242
|
+
inBytes = Buffer.concat([inBytes, newData]);
|
|
243
|
+
pendingOnFrame = onFrame;
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (inBytes.length === 0) {
|
|
248
|
+
inBytes = Buffer.from(newData);
|
|
249
|
+
} else {
|
|
250
|
+
inBytes = Buffer.concat([inBytes, newData]);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
await processData(onFrame);
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
256
|
};
|