@hiroosy/baileys-lite 0.0.4 → 0.3.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/LICENSE +21 -0
- package/README.md +658 -10
- 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 +716 -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
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { generateKeyPairSync, createSign, randomBytes } from 'node:crypto';
|
|
2
|
+
import { der } from './der.js';
|
|
3
|
+
|
|
4
|
+
const EC_PUBLIC_KEY_OID = '1.2.840.10045.2.1';
|
|
5
|
+
const PRIME256V1_OID = '1.2.840.10045.3.1.7';
|
|
6
|
+
const ECDSA_WITH_SHA256_OID = '1.2.840.10045.4.3.2';
|
|
7
|
+
const COMMON_NAME_OID = '2.5.4.3';
|
|
8
|
+
|
|
9
|
+
function rdnSequence(commonName) {
|
|
10
|
+
return der.sequence(
|
|
11
|
+
der.set(
|
|
12
|
+
der.sequence(
|
|
13
|
+
der.objectIdentifier(COMMON_NAME_OID),
|
|
14
|
+
der.utf8String(commonName),
|
|
15
|
+
),
|
|
16
|
+
),
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function algorithmIdentifierEcdsaSha256() {
|
|
21
|
+
return der.sequence(der.objectIdentifier(ECDSA_WITH_SHA256_OID));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function subjectPublicKeyInfo(spkiDer) {
|
|
25
|
+
|
|
26
|
+
return spkiDer;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function ecdsaSignatureFromNode(sig) {
|
|
30
|
+
return sig;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function generateSelfSignedCert(commonName = 'wa-voip') {
|
|
34
|
+
const { publicKey, privateKey } = generateKeyPairSync('ec', { namedCurve: 'prime256v1' });
|
|
35
|
+
const spkiDer = publicKey.export({ format: 'der', type: 'spki' });
|
|
36
|
+
|
|
37
|
+
const serialNumber = randomBytes(16);
|
|
38
|
+
const notBefore = new Date(Date.now() - 24 * 60 * 60 * 1000);
|
|
39
|
+
const notAfter = new Date(Date.now() + 24 * 60 * 60 * 1000);
|
|
40
|
+
|
|
41
|
+
const tbsCertificate = der.sequence(
|
|
42
|
+
der.contextConstructed(0, der.integer(2)),
|
|
43
|
+
der.integer(serialNumber),
|
|
44
|
+
algorithmIdentifierEcdsaSha256(),
|
|
45
|
+
rdnSequence(commonName),
|
|
46
|
+
der.sequence(der.utcTime(notBefore), der.utcTime(notAfter)),
|
|
47
|
+
rdnSequence(commonName),
|
|
48
|
+
subjectPublicKeyInfo(spkiDer),
|
|
49
|
+
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
const signer = createSign('SHA256');
|
|
53
|
+
signer.update(tbsCertificate);
|
|
54
|
+
signer.end();
|
|
55
|
+
const signature = ecdsaSignatureFromNode(signer.sign(privateKey));
|
|
56
|
+
|
|
57
|
+
const certDer = der.sequence(
|
|
58
|
+
tbsCertificate,
|
|
59
|
+
algorithmIdentifierEcdsaSha256(),
|
|
60
|
+
der.bitString(signature),
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
const spkiParsed = spkiOuterBitStringContent(spkiDer);
|
|
64
|
+
const publicKeyPoint = spkiParsed.subarray(1);
|
|
65
|
+
|
|
66
|
+
return { certDer, privateKey, publicKeyPoint };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function spkiOuterBitStringContent(spkiDer) {
|
|
70
|
+
|
|
71
|
+
let offset = 0;
|
|
72
|
+
const tag = spkiDer.readUInt8(offset);
|
|
73
|
+
if (tag !== 0x30) throw new Error('generateSelfSignedCert: unexpected SPKI outer tag');
|
|
74
|
+
|
|
75
|
+
offset = skipTlvHeader(spkiDer, offset).next === undefined ? offset : offset;
|
|
76
|
+
const outer = readOneTlv(spkiDer, 0);
|
|
77
|
+
|
|
78
|
+
const algId = readOneTlv(outer.content, 0);
|
|
79
|
+
const bitString = readOneTlv(outer.content, algId.next);
|
|
80
|
+
if (bitString.tag !== 0x03) throw new Error('generateSelfSignedCert: expected BIT STRING after AlgorithmIdentifier');
|
|
81
|
+
return bitString.content;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function skipTlvHeader(buf, offset) {
|
|
85
|
+
return readOneTlv(buf, offset);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function readOneTlv(buf, offset) {
|
|
89
|
+
const tag = buf.readUInt8(offset);
|
|
90
|
+
let lenByte = buf.readUInt8(offset + 1);
|
|
91
|
+
let lenStart = offset + 2;
|
|
92
|
+
let length;
|
|
93
|
+
if (lenByte & 0x80) {
|
|
94
|
+
const numBytes = lenByte & 0x7f;
|
|
95
|
+
length = 0;
|
|
96
|
+
for (let i = 0; i < numBytes; i++) length = (length << 8) | buf.readUInt8(lenStart + i);
|
|
97
|
+
lenStart += numBytes;
|
|
98
|
+
} else {
|
|
99
|
+
length = lenByte;
|
|
100
|
+
}
|
|
101
|
+
return { tag, content: buf.subarray(lenStart, lenStart + length), next: lenStart + length };
|
|
102
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
|
|
2
|
+
function encodeLength(len) {
|
|
3
|
+
if (len < 0x80) return Buffer.from([len]);
|
|
4
|
+
const bytes = [];
|
|
5
|
+
let n = len;
|
|
6
|
+
while (n > 0) {
|
|
7
|
+
bytes.unshift(n & 0xff);
|
|
8
|
+
n >>>= 8;
|
|
9
|
+
}
|
|
10
|
+
return Buffer.from([0x80 | bytes.length, ...bytes]);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function tlv(tag, content) {
|
|
14
|
+
return Buffer.concat([Buffer.from([tag]), encodeLength(content.length), content]);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const der = {
|
|
18
|
+
sequence: (...parts) => tlv(0x30, Buffer.concat(parts)),
|
|
19
|
+
set: (...parts) => tlv(0x31, Buffer.concat(parts)),
|
|
20
|
+
contextConstructed: (tagNumber, ...parts) => tlv(0xa0 | tagNumber, Buffer.concat(parts)),
|
|
21
|
+
|
|
22
|
+
integer: (value) => {
|
|
23
|
+
|
|
24
|
+
let bytes;
|
|
25
|
+
if (Buffer.isBuffer(value)) {
|
|
26
|
+
bytes = value;
|
|
27
|
+
} else {
|
|
28
|
+
let n = BigInt(value);
|
|
29
|
+
if (n === 0n) {
|
|
30
|
+
bytes = Buffer.from([0]);
|
|
31
|
+
} else {
|
|
32
|
+
const out = [];
|
|
33
|
+
while (n > 0n) {
|
|
34
|
+
out.unshift(Number(n & 0xffn));
|
|
35
|
+
n >>= 8n;
|
|
36
|
+
}
|
|
37
|
+
bytes = Buffer.from(out);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (bytes[0] & 0x80) bytes = Buffer.concat([Buffer.from([0]), bytes]);
|
|
42
|
+
return tlv(0x02, bytes);
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
bitString: (content, unusedBits = 0) => tlv(0x03, Buffer.concat([Buffer.from([unusedBits]), content])),
|
|
46
|
+
octetString: (content) => tlv(0x04, content),
|
|
47
|
+
nullValue: () => Buffer.from([0x05, 0x00]),
|
|
48
|
+
objectIdentifier: (dotted) => {
|
|
49
|
+
const parts = dotted.split('.').map(Number);
|
|
50
|
+
const first = parts[0] * 40 + parts[1];
|
|
51
|
+
const bytes = [first];
|
|
52
|
+
for (const part of parts.slice(2)) {
|
|
53
|
+
if (part < 0x80) {
|
|
54
|
+
bytes.push(part);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
const chunk = [];
|
|
58
|
+
let n = part;
|
|
59
|
+
chunk.unshift(n & 0x7f);
|
|
60
|
+
n >>>= 7;
|
|
61
|
+
while (n > 0) {
|
|
62
|
+
chunk.unshift((n & 0x7f) | 0x80);
|
|
63
|
+
n >>>= 7;
|
|
64
|
+
}
|
|
65
|
+
bytes.push(...chunk);
|
|
66
|
+
}
|
|
67
|
+
return tlv(0x06, Buffer.from(bytes));
|
|
68
|
+
},
|
|
69
|
+
utf8String: (str) => tlv(0x0c, Buffer.from(str, 'utf8')),
|
|
70
|
+
printableString: (str) => tlv(0x13, Buffer.from(str, 'ascii')),
|
|
71
|
+
|
|
72
|
+
utcTime: (date) => {
|
|
73
|
+
const yy = String(date.getUTCFullYear() % 100).padStart(2, '0');
|
|
74
|
+
const mm = String(date.getUTCMonth() + 1).padStart(2, '0');
|
|
75
|
+
const dd = String(date.getUTCDate()).padStart(2, '0');
|
|
76
|
+
const hh = String(date.getUTCHours()).padStart(2, '0');
|
|
77
|
+
const mi = String(date.getUTCMinutes()).padStart(2, '0');
|
|
78
|
+
const ss = String(date.getUTCSeconds()).padStart(2, '0');
|
|
79
|
+
return tlv(0x17, Buffer.from(`${yy}${mm}${dd}${hh}${mi}${ss}Z`, 'ascii'));
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export function readTlv(buf, offset = 0) {
|
|
84
|
+
const tag = buf.readUInt8(offset);
|
|
85
|
+
let lenByte = buf.readUInt8(offset + 1);
|
|
86
|
+
let lenStart = offset + 2;
|
|
87
|
+
let length;
|
|
88
|
+
if (lenByte & 0x80) {
|
|
89
|
+
const numBytes = lenByte & 0x7f;
|
|
90
|
+
length = 0;
|
|
91
|
+
for (let i = 0; i < numBytes; i++) length = (length << 8) | buf.readUInt8(lenStart + i);
|
|
92
|
+
lenStart += numBytes;
|
|
93
|
+
} else {
|
|
94
|
+
length = lenByte;
|
|
95
|
+
}
|
|
96
|
+
const content = buf.subarray(lenStart, lenStart + length);
|
|
97
|
+
return { tag, content, next: lenStart + length };
|
|
98
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { createECDH, createSign, createVerify } from 'node:crypto';
|
|
2
|
+
|
|
3
|
+
export const NAMED_CURVE_SECP256R1 = 23;
|
|
4
|
+
|
|
5
|
+
export function generateEcdheKeypair() {
|
|
6
|
+
const ecdh = createECDH('prime256v1');
|
|
7
|
+
ecdh.generateKeys();
|
|
8
|
+
return {
|
|
9
|
+
publicKeyPoint: ecdh.getPublicKey(),
|
|
10
|
+
computeSharedSecret: (peerPublicKeyPoint) => ecdh.computeSecret(peerPublicKeyPoint),
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function buildEcdheSignedParams(clientRandom, serverRandom, publicKeyPoint) {
|
|
15
|
+
return Buffer.concat([
|
|
16
|
+
clientRandom,
|
|
17
|
+
serverRandom,
|
|
18
|
+
Buffer.from([3]),
|
|
19
|
+
Buffer.from([0, NAMED_CURVE_SECP256R1]),
|
|
20
|
+
Buffer.from([publicKeyPoint.length]),
|
|
21
|
+
publicKeyPoint,
|
|
22
|
+
]);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function signEcdheParams(privateKey, signedParams) {
|
|
26
|
+
const signer = createSign('SHA256');
|
|
27
|
+
signer.update(signedParams);
|
|
28
|
+
signer.end();
|
|
29
|
+
return signer.sign(privateKey);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function verifyEcdheParams(peerPublicKey, signedParams, signature) {
|
|
33
|
+
const verifier = createVerify('SHA256');
|
|
34
|
+
verifier.update(signedParams);
|
|
35
|
+
verifier.end();
|
|
36
|
+
return verifier.verify(peerPublicKey, signature);
|
|
37
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
|
|
2
|
+
export const HandshakeType = Object.freeze({
|
|
3
|
+
HELLO_REQUEST: 0,
|
|
4
|
+
CLIENT_HELLO: 1,
|
|
5
|
+
SERVER_HELLO: 2,
|
|
6
|
+
HELLO_VERIFY_REQUEST: 3,
|
|
7
|
+
CERTIFICATE: 11,
|
|
8
|
+
SERVER_KEY_EXCHANGE: 12,
|
|
9
|
+
CERTIFICATE_REQUEST: 13,
|
|
10
|
+
SERVER_HELLO_DONE: 14,
|
|
11
|
+
CERTIFICATE_VERIFY: 15,
|
|
12
|
+
CLIENT_KEY_EXCHANGE: 16,
|
|
13
|
+
FINISHED: 20,
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
const HANDSHAKE_HEADER_LEN = 12;
|
|
17
|
+
|
|
18
|
+
function writeUint24BE(buf, offset, value) {
|
|
19
|
+
buf.writeUInt8((value >> 16) & 0xff, offset);
|
|
20
|
+
buf.writeUInt8((value >> 8) & 0xff, offset + 1);
|
|
21
|
+
buf.writeUInt8(value & 0xff, offset + 2);
|
|
22
|
+
}
|
|
23
|
+
function readUint24BE(buf, offset) {
|
|
24
|
+
return (buf.readUInt8(offset) << 16) | (buf.readUInt8(offset + 1) << 8) | buf.readUInt8(offset + 2);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function encodeHandshakeMessage({ msgType, messageSeq, body, maxFragmentSize = 1200 }) {
|
|
28
|
+
const totalLength = body.length;
|
|
29
|
+
if (totalLength <= maxFragmentSize) {
|
|
30
|
+
const header = Buffer.alloc(HANDSHAKE_HEADER_LEN);
|
|
31
|
+
header.writeUInt8(msgType, 0);
|
|
32
|
+
writeUint24BE(header, 1, totalLength);
|
|
33
|
+
header.writeUInt16BE(messageSeq, 4);
|
|
34
|
+
writeUint24BE(header, 6, 0);
|
|
35
|
+
writeUint24BE(header, 9, totalLength);
|
|
36
|
+
return [Buffer.concat([header, body])];
|
|
37
|
+
}
|
|
38
|
+
const fragments = [];
|
|
39
|
+
let offset = 0;
|
|
40
|
+
while (offset < totalLength) {
|
|
41
|
+
const fragLen = Math.min(maxFragmentSize, totalLength - offset);
|
|
42
|
+
const header = Buffer.alloc(HANDSHAKE_HEADER_LEN);
|
|
43
|
+
header.writeUInt8(msgType, 0);
|
|
44
|
+
writeUint24BE(header, 1, totalLength);
|
|
45
|
+
header.writeUInt16BE(messageSeq, 4);
|
|
46
|
+
writeUint24BE(header, 6, offset);
|
|
47
|
+
writeUint24BE(header, 9, fragLen);
|
|
48
|
+
fragments.push(Buffer.concat([header, body.subarray(offset, offset + fragLen)]));
|
|
49
|
+
offset += fragLen;
|
|
50
|
+
}
|
|
51
|
+
return fragments;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function decodeHandshakeFragment(buf) {
|
|
55
|
+
const msgType = buf.readUInt8(0);
|
|
56
|
+
const length = readUint24BE(buf, 1);
|
|
57
|
+
const messageSeq = buf.readUInt16BE(4);
|
|
58
|
+
const fragmentOffset = readUint24BE(buf, 6);
|
|
59
|
+
const fragmentLength = readUint24BE(buf, 9);
|
|
60
|
+
const body = buf.subarray(HANDSHAKE_HEADER_LEN, HANDSHAKE_HEADER_LEN + fragmentLength);
|
|
61
|
+
return {
|
|
62
|
+
msgType, length, messageSeq, fragmentOffset, fragmentLength, body,
|
|
63
|
+
consumed: HANDSHAKE_HEADER_LEN + fragmentLength,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export class HandshakeReassembler {
|
|
68
|
+
#pending = new Map();
|
|
69
|
+
|
|
70
|
+
addFragment({ msgType, length, messageSeq, fragmentOffset, fragmentLength, body }) {
|
|
71
|
+
if (fragmentOffset === 0 && fragmentLength === length) {
|
|
72
|
+
|
|
73
|
+
return { msgType, messageSeq, body: Buffer.from(body) };
|
|
74
|
+
}
|
|
75
|
+
let entry = this.#pending.get(messageSeq);
|
|
76
|
+
if (!entry) {
|
|
77
|
+
entry = { length, msgType, chunks: new Map() };
|
|
78
|
+
this.#pending.set(messageSeq, entry);
|
|
79
|
+
}
|
|
80
|
+
entry.chunks.set(fragmentOffset, Buffer.from(body));
|
|
81
|
+
|
|
82
|
+
let covered = 0;
|
|
83
|
+
for (const chunk of entry.chunks.values()) covered += chunk.length;
|
|
84
|
+
if (covered < entry.length) return null;
|
|
85
|
+
const sortedOffsets = [...entry.chunks.keys()].sort((a, b) => a - b);
|
|
86
|
+
const assembled = Buffer.concat(sortedOffsets.map((off) => entry.chunks.get(off)));
|
|
87
|
+
this.#pending.delete(messageSeq);
|
|
88
|
+
return { msgType: entry.msgType, messageSeq, body: assembled.subarray(0, entry.length) };
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function logicalHandshakeBytes({ msgType, messageSeq, body }) {
|
|
93
|
+
const header = Buffer.alloc(HANDSHAKE_HEADER_LEN);
|
|
94
|
+
header.writeUInt8(msgType, 0);
|
|
95
|
+
writeUint24BE(header, 1, body.length);
|
|
96
|
+
header.writeUInt16BE(messageSeq, 4);
|
|
97
|
+
writeUint24BE(header, 6, 0);
|
|
98
|
+
writeUint24BE(header, 9, body.length);
|
|
99
|
+
return Buffer.concat([header, body]);
|
|
100
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { der, readTlv } from './der.js';
|
|
2
|
+
|
|
3
|
+
export const CIPHER_SUITE_ECDHE_ECDSA_AES_128_GCM_SHA256 = 0xc02b;
|
|
4
|
+
export const COMPRESSION_METHOD_NULL = 0;
|
|
5
|
+
export const SIGNATURE_SCHEME_ECDSA_SECP256R1_SHA256 = 0x0403;
|
|
6
|
+
export const EXTENSION_SUPPORTED_GROUPS = 10;
|
|
7
|
+
export const EXTENSION_SIGNATURE_ALGORITHMS = 13;
|
|
8
|
+
export const NAMED_CURVE_SECP256R1 = 23;
|
|
9
|
+
|
|
10
|
+
function u16(n) { const b = Buffer.alloc(2); b.writeUInt16BE(n, 0); return b; }
|
|
11
|
+
function u8LenPrefixed(buf) { return Buffer.concat([Buffer.from([buf.length]), buf]); }
|
|
12
|
+
function u16LenPrefixed(buf) { return Buffer.concat([u16(buf.length), buf]); }
|
|
13
|
+
|
|
14
|
+
export function buildClientHello({ clientRandom, sessionId = Buffer.alloc(0), cookie = Buffer.alloc(0) }) {
|
|
15
|
+
const cipherSuites = u16LenPrefixed(u16(CIPHER_SUITE_ECDHE_ECDSA_AES_128_GCM_SHA256));
|
|
16
|
+
const compressionMethods = u8LenPrefixed(Buffer.from([COMPRESSION_METHOD_NULL]));
|
|
17
|
+
|
|
18
|
+
const supportedGroupsExt = Buffer.concat([
|
|
19
|
+
u16(EXTENSION_SUPPORTED_GROUPS),
|
|
20
|
+
u16LenPrefixed(u16LenPrefixed(u16(NAMED_CURVE_SECP256R1))),
|
|
21
|
+
]);
|
|
22
|
+
const sigAlgsExt = Buffer.concat([
|
|
23
|
+
u16(EXTENSION_SIGNATURE_ALGORITHMS),
|
|
24
|
+
u16LenPrefixed(u16LenPrefixed(u16(SIGNATURE_SCHEME_ECDSA_SECP256R1_SHA256))),
|
|
25
|
+
]);
|
|
26
|
+
const extensions = u16LenPrefixed(Buffer.concat([supportedGroupsExt, sigAlgsExt]));
|
|
27
|
+
|
|
28
|
+
return Buffer.concat([
|
|
29
|
+
Buffer.from([0xfe, 0xfd]),
|
|
30
|
+
clientRandom,
|
|
31
|
+
u8LenPrefixed(sessionId),
|
|
32
|
+
u8LenPrefixed(cookie),
|
|
33
|
+
cipherSuites,
|
|
34
|
+
compressionMethods,
|
|
35
|
+
extensions,
|
|
36
|
+
]);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function parseHelloVerifyRequest(body) {
|
|
40
|
+
const cookieLen = body.readUInt8(2);
|
|
41
|
+
const cookie = body.subarray(3, 3 + cookieLen);
|
|
42
|
+
return { cookie };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function parseServerHello(body) {
|
|
46
|
+
let offset = 2;
|
|
47
|
+
const serverRandom = body.subarray(offset, offset + 32);
|
|
48
|
+
offset += 32;
|
|
49
|
+
const sessionIdLen = body.readUInt8(offset);
|
|
50
|
+
offset += 1 + sessionIdLen;
|
|
51
|
+
const cipherSuite = body.readUInt16BE(offset);
|
|
52
|
+
offset += 2;
|
|
53
|
+
const compressionMethod = body.readUInt8(offset);
|
|
54
|
+
offset += 1;
|
|
55
|
+
|
|
56
|
+
return { serverRandom, cipherSuite, compressionMethod };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function parseCertificate(body) {
|
|
60
|
+
const listLen = (body.readUInt8(0) << 16) | (body.readUInt8(1) << 8) | body.readUInt8(2);
|
|
61
|
+
const certificates = [];
|
|
62
|
+
let offset = 3;
|
|
63
|
+
const end = 3 + listLen;
|
|
64
|
+
while (offset < end) {
|
|
65
|
+
const certLen = (body.readUInt8(offset) << 16) | (body.readUInt8(offset + 1) << 8) | body.readUInt8(offset + 2);
|
|
66
|
+
offset += 3;
|
|
67
|
+
certificates.push(body.subarray(offset, offset + certLen));
|
|
68
|
+
offset += certLen;
|
|
69
|
+
}
|
|
70
|
+
return { certificates };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function buildCertificate(certDer) {
|
|
74
|
+
const oneCert = Buffer.concat([
|
|
75
|
+
Buffer.from([(certDer.length >> 16) & 0xff, (certDer.length >> 8) & 0xff, certDer.length & 0xff]),
|
|
76
|
+
certDer,
|
|
77
|
+
]);
|
|
78
|
+
const listLen = oneCert.length;
|
|
79
|
+
return Buffer.concat([
|
|
80
|
+
Buffer.from([(listLen >> 16) & 0xff, (listLen >> 8) & 0xff, listLen & 0xff]),
|
|
81
|
+
oneCert,
|
|
82
|
+
]);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function parseClientHello(body) {
|
|
86
|
+
let offset = 2;
|
|
87
|
+
const clientRandom = body.subarray(offset, offset + 32);
|
|
88
|
+
offset += 32;
|
|
89
|
+
const sessionIdLen = body.readUInt8(offset); offset += 1;
|
|
90
|
+
const sessionId = body.subarray(offset, offset + sessionIdLen); offset += sessionIdLen;
|
|
91
|
+
const cookieLen = body.readUInt8(offset); offset += 1;
|
|
92
|
+
const cookie = body.subarray(offset, offset + cookieLen); offset += cookieLen;
|
|
93
|
+
const cipherSuitesLen = body.readUInt16BE(offset); offset += 2;
|
|
94
|
+
const cipherSuites = [];
|
|
95
|
+
for (let i = 0; i < cipherSuitesLen; i += 2) cipherSuites.push(body.readUInt16BE(offset + i));
|
|
96
|
+
offset += cipherSuitesLen;
|
|
97
|
+
const compressionMethodsLen = body.readUInt8(offset); offset += 1;
|
|
98
|
+
const compressionMethods = Array.from(body.subarray(offset, offset + compressionMethodsLen));
|
|
99
|
+
return { clientRandom, sessionId, cookie, cipherSuites, compressionMethods };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function buildHelloVerifyRequest({ cookie }) {
|
|
103
|
+
return Buffer.concat([
|
|
104
|
+
Buffer.from([0xfe, 0xfd]),
|
|
105
|
+
u8LenPrefixed(cookie),
|
|
106
|
+
]);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function buildServerHello({ serverRandom, sessionId = Buffer.alloc(0), cipherSuite, compressionMethod = COMPRESSION_METHOD_NULL }) {
|
|
110
|
+
return Buffer.concat([
|
|
111
|
+
Buffer.from([0xfe, 0xfd]),
|
|
112
|
+
serverRandom,
|
|
113
|
+
u8LenPrefixed(sessionId),
|
|
114
|
+
u16(cipherSuite),
|
|
115
|
+
Buffer.from([compressionMethod]),
|
|
116
|
+
]);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function buildServerHelloDone() {
|
|
120
|
+
return Buffer.alloc(0);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function parseServerKeyExchange(body) {
|
|
124
|
+
let offset = 0;
|
|
125
|
+
const curveType = body.readUInt8(offset); offset += 1;
|
|
126
|
+
const namedCurve = body.readUInt16BE(offset); offset += 2;
|
|
127
|
+
const pointLen = body.readUInt8(offset); offset += 1;
|
|
128
|
+
const publicKeyPoint = body.subarray(offset, offset + pointLen); offset += pointLen;
|
|
129
|
+
const signatureScheme = body.readUInt16BE(offset); offset += 2;
|
|
130
|
+
const sigLen = body.readUInt16BE(offset); offset += 2;
|
|
131
|
+
const signature = body.subarray(offset, offset + sigLen); offset += sigLen;
|
|
132
|
+
return { curveType, namedCurve, publicKeyPoint, signatureScheme, signature };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function buildServerKeyExchange({ publicKeyPoint, signature }) {
|
|
136
|
+
return Buffer.concat([
|
|
137
|
+
Buffer.from([3]),
|
|
138
|
+
u16(NAMED_CURVE_SECP256R1),
|
|
139
|
+
u8LenPrefixed(publicKeyPoint),
|
|
140
|
+
u16(SIGNATURE_SCHEME_ECDSA_SECP256R1_SHA256),
|
|
141
|
+
u16LenPrefixed(signature),
|
|
142
|
+
]);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function buildClientKeyExchange(publicKeyPoint) {
|
|
146
|
+
return u8LenPrefixed(publicKeyPoint);
|
|
147
|
+
}
|
|
148
|
+
export function parseClientKeyExchange(body) {
|
|
149
|
+
const len = body.readUInt8(0);
|
|
150
|
+
return { publicKeyPoint: body.subarray(1, 1 + len) };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function buildFinished(verifyData) { return verifyData; }
|
|
154
|
+
export function parseFinished(body) { return { verifyData: body }; }
|
|
155
|
+
|
|
156
|
+
export function buildCertificateVerify({ signatureScheme, signature }) {
|
|
157
|
+
return Buffer.concat([u16(signatureScheme), u16LenPrefixed(signature)]);
|
|
158
|
+
}
|