@browsercore/quic 0.2.0 → 0.2.2
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 +14 -9
- package/bin/coverage-md.mjs +99 -0
- package/dist/connection.d.ts +159 -24
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +490 -60
- package/dist/connection.js.map +1 -1
- package/dist/crypto/hash-length.d.ts +11 -0
- package/dist/crypto/hash-length.d.ts.map +1 -0
- package/dist/crypto/hash-length.js +20 -0
- package/dist/crypto/hash-length.js.map +1 -0
- package/dist/crypto/index.d.ts +8 -0
- package/dist/crypto/index.d.ts.map +1 -0
- package/dist/crypto/index.js +8 -0
- package/dist/crypto/index.js.map +1 -0
- package/dist/crypto/key-derivation.d.ts +86 -0
- package/dist/crypto/key-derivation.d.ts.map +1 -0
- package/dist/crypto/key-derivation.js +123 -0
- package/dist/crypto/key-derivation.js.map +1 -0
- package/dist/errors.d.ts +26 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +35 -0
- package/dist/errors.js.map +1 -1
- package/dist/frame/frame.d.ts.map +1 -1
- package/dist/frame/frame.js +2 -2
- package/dist/frame/frame.js.map +1 -1
- package/dist/handshake/index.d.ts +10 -0
- package/dist/handshake/index.d.ts.map +1 -0
- package/dist/handshake/index.js +9 -0
- package/dist/handshake/index.js.map +1 -0
- package/dist/handshake/quic-handshake.d.ts +73 -0
- package/dist/handshake/quic-handshake.d.ts.map +1 -0
- package/dist/handshake/quic-handshake.js +213 -0
- package/dist/handshake/quic-handshake.js.map +1 -0
- package/dist/handshake/quic-transport-adapter.d.ts +78 -0
- package/dist/handshake/quic-transport-adapter.d.ts.map +1 -0
- package/dist/handshake/quic-transport-adapter.js +140 -0
- package/dist/handshake/quic-transport-adapter.js.map +1 -0
- package/dist/index.d.ts +9 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -4
- package/dist/index.js.map +1 -1
- package/dist/packet/packet-protection.d.ts +158 -0
- package/dist/packet/packet-protection.d.ts.map +1 -0
- package/dist/packet/packet-protection.js +313 -0
- package/dist/packet/packet-protection.js.map +1 -0
- package/dist/packet/packet.d.ts +2 -2
- package/dist/packet/packet.d.ts.map +1 -1
- package/dist/packet/packet.js +4 -4
- package/dist/packet/packet.js.map +1 -1
- package/dist/stream/stream.d.ts.map +1 -1
- package/dist/stream/stream.js +10 -6
- package/dist/stream/stream.js.map +1 -1
- package/dist/types.d.ts +98 -41
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +27 -20
- package/dist/types.js.map +1 -1
- package/package.json +8 -4
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* QUIC packet protection (RFC 9001 §5).
|
|
3
|
+
*
|
|
4
|
+
* Applies and removes QUIC's two-layer packet protection:
|
|
5
|
+
*
|
|
6
|
+
* 1. Payload encryption (RFC 9001 §5.3): AEAD with quic_key + quic_iv.
|
|
7
|
+
* The per-record nonce is the XOR of the (zero-padded, big-endian)
|
|
8
|
+
* packet number into the static IV. The AAD is the unprotected header
|
|
9
|
+
* (everything up to and including the unencrypted packet number).
|
|
10
|
+
*
|
|
11
|
+
* 2. Header protection (RFC 9001 §5.4): the low bits of the first byte
|
|
12
|
+
* and the packet number are masked with a mask derived from
|
|
13
|
+
* AES-ECB(quic_hp, sample). The sample is 16 bytes of ciphertext
|
|
14
|
+
* starting 4 bytes into the protected payload.
|
|
15
|
+
*
|
|
16
|
+
* This module is pure wire-format logic — no I/O, no key management. It
|
|
17
|
+
* operates over @browsercore/crypto for the AEAD and AES-ECB primitives.
|
|
18
|
+
*
|
|
19
|
+
* Honest limitations:
|
|
20
|
+
* - Only AES-128-GCM, AES-256-GCM, and ChaCha20-Poly1305 are wired for
|
|
21
|
+
* payload AEAD. AES-128-CCM is defined by TLS 1.3 but never negotiated
|
|
22
|
+
* by QUIC v1, so it is rejected here.
|
|
23
|
+
* - 0-RTT, KeyUpdate, and key-phase-bit handling are out of scope for now
|
|
24
|
+
* — the connection layer passes the correct keys for the packet's phase.
|
|
25
|
+
*/
|
|
26
|
+
import { type CryptoProvider } from "@browsercore/crypto";
|
|
27
|
+
import { type QuicProtectionSecrets } from "../crypto/key-derivation.js";
|
|
28
|
+
/**
|
|
29
|
+
* AEAD algorithm identifiers (subset of TLS AEAD algorithms used by QUIC v1).
|
|
30
|
+
* Mirrors @browsercore/tls's AeadAlgorithm naming ("CHACHA20_POLY1305" with
|
|
31
|
+
* underscores). The aeadAlgorithmId helper maps to @browsercore/crypto's
|
|
32
|
+
* "ChaCha20-Poly1305" (hyphens) when calling the crypto provider.
|
|
33
|
+
*/
|
|
34
|
+
export type QuicAead = "AES-128-GCM" | "AES-256-GCM" | "CHACHA20-POLY1305";
|
|
35
|
+
/**
|
|
36
|
+
* Construct the per-packet AEAD nonce from the static IV and the packet number.
|
|
37
|
+
*
|
|
38
|
+
* The packet number is encoded as a zero-padded big-endian integer in an
|
|
39
|
+
* 8-byte buffer, then XOR'd into the IV's low 8 bytes (RFC 9001 §5.3).
|
|
40
|
+
*
|
|
41
|
+
* @param iv Static IV (12 bytes) derived from the traffic secret.
|
|
42
|
+
* @param pn Packet number (0..2^62-1).
|
|
43
|
+
*/
|
|
44
|
+
export declare function constructNonce(iv: Uint8Array, pn: bigint): Uint8Array;
|
|
45
|
+
/**
|
|
46
|
+
* AEAD-encrypt a payload with the given key and nonce.
|
|
47
|
+
*
|
|
48
|
+
* @param aead AEAD algorithm (AES-128/256-GCM or ChaCha20-Poly1305).
|
|
49
|
+
* @param key AEAD key (16 bytes for AES-128, 32 bytes for AES-256 / ChaCha20).
|
|
50
|
+
* @param nonce Per-packet nonce (12 bytes), computed via {@link constructNonce}.
|
|
51
|
+
* @param plaintext The unprotected payload bytes.
|
|
52
|
+
* @param aad The unprotected header (everything up to and including the
|
|
53
|
+
* unencrypted packet number).
|
|
54
|
+
* @param provider Cryptographic provider.
|
|
55
|
+
* @returns Ciphertext with the 16-byte authentication tag appended.
|
|
56
|
+
*/
|
|
57
|
+
export declare function encryptPayload(aead: QuicAead, key: Uint8Array, nonce: Uint8Array, plaintext: Uint8Array, aad: Uint8Array, provider?: CryptoProvider): Uint8Array;
|
|
58
|
+
/**
|
|
59
|
+
* AEAD-decrypt a payload with the given key and nonce.
|
|
60
|
+
*
|
|
61
|
+
* @param aead AEAD algorithm.
|
|
62
|
+
* @param key AEAD key.
|
|
63
|
+
* @param nonce Per-packet nonce (12 bytes), computed via {@link constructNonce}.
|
|
64
|
+
* @param ciphertext Ciphertext with the 16-byte authentication tag appended.
|
|
65
|
+
* @param aad The unprotected header (everything up to and including the
|
|
66
|
+
* packet number after header protection removal).
|
|
67
|
+
* @param provider Cryptographic provider.
|
|
68
|
+
* @returns Decrypted plaintext.
|
|
69
|
+
* @throws Error on authentication failure.
|
|
70
|
+
*/
|
|
71
|
+
export declare function decryptPayload(aead: QuicAead, key: Uint8Array, nonce: Uint8Array, ciphertext: Uint8Array, aad: Uint8Array, provider?: CryptoProvider): Uint8Array;
|
|
72
|
+
/**
|
|
73
|
+
* Derive the header-protection mask for a given sample (RFC 9001 §5.4.1).
|
|
74
|
+
*
|
|
75
|
+
* mask = AES-ECB(hp_key, sample)
|
|
76
|
+
*
|
|
77
|
+
* The sample is the 16 bytes of (protected) payload starting 4 bytes past
|
|
78
|
+
* the start of the packet number. For short headers (1-RTT), the first byte
|
|
79
|
+
* of the mask is used to protect the low 5 bits of the first header byte
|
|
80
|
+
* (after header protection is removed, these encode the header form + key
|
|
81
|
+
* phase + reserved bits). For long headers, the first byte protects the low
|
|
82
|
+
* 4 bits (header form + fixed + long packet type). Bytes 1–5 of the mask
|
|
83
|
+
* protect the encoded packet number.
|
|
84
|
+
*
|
|
85
|
+
* @param hpKey Header-protection key (16 bytes for AES-128, 32 bytes for AES-256).
|
|
86
|
+
* @param sample 16-byte sample from the protected payload.
|
|
87
|
+
* @param provider Cryptographic provider.
|
|
88
|
+
* @returns A 16-byte mask; the low 5 (short header) or 4 (long header) bits of
|
|
89
|
+
* byte 0 protect the first header byte, bytes 1–5 protect the packet
|
|
90
|
+
* number.
|
|
91
|
+
*/
|
|
92
|
+
export declare function computeHeaderProtectionMask(hpKey: Uint8Array, sample: Uint8Array, provider?: CryptoProvider): Uint8Array;
|
|
93
|
+
/**
|
|
94
|
+
* Apply header protection to the first byte and packet number (RFC 9001 §5.4).
|
|
95
|
+
*
|
|
96
|
+
* @param firstByte The first header byte (already serialized).
|
|
97
|
+
* @param packetNumber The packet number to protect.
|
|
98
|
+
* @param pnLength Number of bytes the packet number occupies on the wire (1–4).
|
|
99
|
+
* @param mask Header-protection mask (from {@link computeHeaderProtectionMask}).
|
|
100
|
+
* @param isLongHeader Whether this is a long header (affects which low bits are masked).
|
|
101
|
+
* @returns The masked first byte and masked packet-number bytes.
|
|
102
|
+
*/
|
|
103
|
+
export declare function applyHeaderProtection(firstByte: number, packetNumber: bigint, pnLength: number, mask: Uint8Array, isLongHeader: boolean): {
|
|
104
|
+
firstByte: number;
|
|
105
|
+
packetNumberBytes: Uint8Array;
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Remove header protection from the first byte and packet number (RFC 9001 §5.4).
|
|
109
|
+
*
|
|
110
|
+
* @param firstByte The first header byte (as received, still masked).
|
|
111
|
+
* @param pnBytes The masked packet-number bytes (as received).
|
|
112
|
+
* @param pnLength Number of bytes the packet number occupies (1–4).
|
|
113
|
+
* @param mask Header-protection mask (from {@link computeHeaderProtectionMask}).
|
|
114
|
+
* @param isLongHeader Whether this is a long header.
|
|
115
|
+
* @returns The unmasked first byte and packet number.
|
|
116
|
+
*/
|
|
117
|
+
export declare function removeHeaderProtection(firstByte: number, pnBytes: Uint8Array, pnLength: number, mask: Uint8Array, isLongHeader: boolean): {
|
|
118
|
+
firstByte: number;
|
|
119
|
+
packetNumber: bigint;
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* Protect a QUIC packet payload: AEAD-encrypt the payload and apply header
|
|
123
|
+
* protection to the first byte + packet number (RFC 9001 §5.3, §5.4).
|
|
124
|
+
*
|
|
125
|
+
* @param payload The unprotected frame payload.
|
|
126
|
+
* @param packetNumber The packet number to assign.
|
|
127
|
+
* @param pnLength Number of bytes the packet number occupies on the wire (1–4).
|
|
128
|
+
* @param firstByte The first header byte (already serialized, unprotected).
|
|
129
|
+
* @param aead AEAD algorithm.
|
|
130
|
+
* @param secrets QUIC protection secrets (key, iv, hp) for this direction + phase.
|
|
131
|
+
* @param isLongHeader Whether this is a long header.
|
|
132
|
+
* @param provider Cryptographic provider.
|
|
133
|
+
* @returns The AEAD-protected payload (with tag appended) — the caller prepends
|
|
134
|
+
* the masked first byte and masked packet number to form the full packet.
|
|
135
|
+
*/
|
|
136
|
+
export declare function protectPayload(payload: Uint8Array, packetNumber: bigint, pnLength: number, firstByte: number, aead: QuicAead, secrets: QuicProtectionSecrets, isLongHeader: boolean, provider?: CryptoProvider): {
|
|
137
|
+
protectedPayload: Uint8Array;
|
|
138
|
+
maskedFirstByte: number;
|
|
139
|
+
maskedPacketNumber: Uint8Array;
|
|
140
|
+
};
|
|
141
|
+
/**
|
|
142
|
+
* Unprotect a QUIC packet payload: remove header protection, then AEAD-decrypt.
|
|
143
|
+
*
|
|
144
|
+
* @param firstByte The first header byte (as received, still masked).
|
|
145
|
+
* @param pnBytes The masked packet-number bytes (as received).
|
|
146
|
+
* @param pnLength Number of bytes the packet number occupies (1–4).
|
|
147
|
+
* @param protectedPayload The AEAD-protected payload (with tag appended).
|
|
148
|
+
* @param isLongHeader Whether this is a long header.
|
|
149
|
+
* @param aead AEAD algorithm.
|
|
150
|
+
* @param secrets QUIC protection secrets (key, iv, hp) for this direction + phase.
|
|
151
|
+
* @param provider Cryptographic provider.
|
|
152
|
+
* @returns The decrypted frame payload and the decoded packet number.
|
|
153
|
+
*/
|
|
154
|
+
export declare function unprotectPayload(firstByte: number, pnBytes: Uint8Array, pnLength: number, protectedPayload: Uint8Array, isLongHeader: boolean, aead: QuicAead, secrets: QuicProtectionSecrets, provider?: CryptoProvider): {
|
|
155
|
+
payload: Uint8Array;
|
|
156
|
+
packetNumber: bigint;
|
|
157
|
+
};
|
|
158
|
+
//# sourceMappingURL=packet-protection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"packet-protection.d.ts","sourceRoot":"","sources":["../../src/packet/packet-protection.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,EAAE,KAAK,cAAc,EAAU,MAAM,qBAAqB,CAAC;AAClE,OAAO,EAAkB,KAAK,qBAAqB,EAAE,MAAM,6BAA6B,CAAC;AAGzF;;;;;GAKG;AACH,MAAM,MAAM,QAAQ,GAAG,aAAa,GAAG,aAAa,GAAG,mBAAmB,CAAC;AAoB3E;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAAC,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,MAAM,GAAG,UAAU,CAgBrE;AAoBD;;;;;;;;;;;GAWG;AACH,wBAAgB,cAAc,CAC1B,IAAI,EAAE,QAAQ,EACd,GAAG,EAAE,UAAU,EACf,KAAK,EAAE,UAAU,EACjB,SAAS,EAAE,UAAU,EACrB,GAAG,EAAE,UAAU,EACf,QAAQ,GAAE,cAAuB,GAClC,UAAU,CAEZ;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,cAAc,CAC1B,IAAI,EAAE,QAAQ,EACd,GAAG,EAAE,UAAU,EACf,KAAK,EAAE,UAAU,EACjB,UAAU,EAAE,UAAU,EACtB,GAAG,EAAE,UAAU,EACf,QAAQ,GAAE,cAAuB,GAClC,UAAU,CAEZ;AA8CD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,2BAA2B,CACvC,KAAK,EAAE,UAAU,EACjB,MAAM,EAAE,UAAU,EAClB,QAAQ,GAAE,cAAuB,GAClC,UAAU,CAQZ;AAED;;;;;;;;;GASG;AACH,wBAAgB,qBAAqB,CACjC,SAAS,EAAE,MAAM,EACjB,YAAY,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packet protection:
|
|
5
|
+
*
|
|
6
|
+
* 1. Payload encryption (RFC 9001 §5.3): AEAD with quic_key + quic_iv.
|
|
7
|
+
* The per-record nonce is the XOR of the (zero-padded, big-endian)
|
|
8
|
+
* packet number into the static IV. The AAD is the unprotected header
|
|
9
|
+
* (everything up to and including the unencrypted packet number).
|
|
10
|
+
*
|
|
11
|
+
* 2. Header protection (RFC 9001 §5.4): the low bits of the first byte
|
|
12
|
+
* and the packet number are masked with a mask derived from
|
|
13
|
+
* AES-ECB(quic_hp, sample). The sample is 16 bytes of ciphertext
|
|
14
|
+
* starting 4 bytes into the protected payload.
|
|
15
|
+
*
|
|
16
|
+
* This module is pure wire-format logic — no I/O, no key management. It
|
|
17
|
+
* operates over @browsercore/crypto for the AEAD and AES-ECB primitives.
|
|
18
|
+
*
|
|
19
|
+
* Honest limitations:
|
|
20
|
+
* - Only AES-128-GCM, AES-256-GCM, and ChaCha20-Poly1305 are wired for
|
|
21
|
+
* payload AEAD. AES-128-CCM is defined by TLS 1.3 but never negotiated
|
|
22
|
+
* by QUIC v1, so it is rejected here.
|
|
23
|
+
* - 0-RTT, KeyUpdate, and key-phase-bit handling are out of scope for now
|
|
24
|
+
* — the connection layer passes the correct keys for the packet's phase.
|
|
25
|
+
*/
|
|
26
|
+
import { crypto } from "@browsercore/crypto";
|
|
27
|
+
import { QUIC_IV_LENGTH } from "../crypto/key-derivation.js";
|
|
28
|
+
import { concat, assertNever } from "../utils.js";
|
|
29
|
+
/** AEAD authentication tag length for every cipher QUIC v1 uses (bytes). */
|
|
30
|
+
const AEAD_TAG_LENGTH = 16;
|
|
31
|
+
/** Header-protection sample length (bytes) per RFC 9001 §5.4.1. */
|
|
32
|
+
const HP_SAMPLE_LENGTH = 16;
|
|
33
|
+
/** Offset into the protected payload where the sample starts (RFC 9001 §5.4.1). */
|
|
34
|
+
const HP_SAMPLE_OFFSET = 4;
|
|
35
|
+
/** The byte offsets into the AES-ECB output used for the header-protection mask. */
|
|
36
|
+
const HP_MASK_FIRST_BYTE_LEN = 1; // 5 bits (low 5 bits of the first mask byte)
|
|
37
|
+
const HP_MASK_PN_OFFSET = 0; // packet number mask starts at byte 0
|
|
38
|
+
/** Mask for the low 5 bits of the first header byte (header form, type, reserved, key-phase). */
|
|
39
|
+
const FIRST_BYTE_LOW5_MASK = 0x1f;
|
|
40
|
+
/** Mask for the low 4 bits of the first header byte (long header: form + fixed + type + pn-len). */
|
|
41
|
+
const FIRST_BYTE_LOW4_MASK = 0x0f;
|
|
42
|
+
/**
|
|
43
|
+
* Construct the per-packet AEAD nonce from the static IV and the packet number.
|
|
44
|
+
*
|
|
45
|
+
* The packet number is encoded as a zero-padded big-endian integer in an
|
|
46
|
+
* 8-byte buffer, then XOR'd into the IV's low 8 bytes (RFC 9001 §5.3).
|
|
47
|
+
*
|
|
48
|
+
* @param iv Static IV (12 bytes) derived from the traffic secret.
|
|
49
|
+
* @param pn Packet number (0..2^62-1).
|
|
50
|
+
*/
|
|
51
|
+
export function constructNonce(iv, pn) {
|
|
52
|
+
if (iv.length !== QUIC_IV_LENGTH) {
|
|
53
|
+
throw new RangeError(`QUIC IV must be ${QUIC_IV_LENGTH} bytes, got ${iv.length}`);
|
|
54
|
+
}
|
|
55
|
+
const nonce = Uint8Array.from(iv);
|
|
56
|
+
// XOR the packet number (big-endian, low 8 bytes) into the IV's low bytes.
|
|
57
|
+
let remaining = pn;
|
|
58
|
+
for (let i = nonce.length - 1; i >= nonce.length - 8 && remaining > 0n; i--) {
|
|
59
|
+
const byte = nonce[i];
|
|
60
|
+
if (byte === undefined) {
|
|
61
|
+
throw new RangeError(`nonce index ${i} out of bounds (iv length ${iv.length})`);
|
|
62
|
+
}
|
|
63
|
+
nonce[i] = byte ^ Number(remaining & 0xffn);
|
|
64
|
+
remaining >>= 8n;
|
|
65
|
+
}
|
|
66
|
+
return nonce;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Map a QUIC AEAD algorithm name to the @browsercore/crypto symmetric cipher id.
|
|
70
|
+
* QUIC uses "CHACHA20_POLY1305" (underscores, matching @browsercore/tls);
|
|
71
|
+
* @browsercore/crypto expects "ChaCha20-Poly1305" (hyphens).
|
|
72
|
+
*/
|
|
73
|
+
function aeadAlgorithmId(aead) {
|
|
74
|
+
switch (aead) {
|
|
75
|
+
case "AES-128-GCM":
|
|
76
|
+
return "AES-128-GCM";
|
|
77
|
+
case "AES-256-GCM":
|
|
78
|
+
return "AES-256-GCM";
|
|
79
|
+
case "CHACHA20-POLY1305":
|
|
80
|
+
return "ChaCha20-Poly1305";
|
|
81
|
+
default:
|
|
82
|
+
return assertNever(aead);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* AEAD-encrypt a payload with the given key and nonce.
|
|
87
|
+
*
|
|
88
|
+
* @param aead AEAD algorithm (AES-128/256-GCM or ChaCha20-Poly1305).
|
|
89
|
+
* @param key AEAD key (16 bytes for AES-128, 32 bytes for AES-256 / ChaCha20).
|
|
90
|
+
* @param nonce Per-packet nonce (12 bytes), computed via {@link constructNonce}.
|
|
91
|
+
* @param plaintext The unprotected payload bytes.
|
|
92
|
+
* @param aad The unprotected header (everything up to and including the
|
|
93
|
+
* unencrypted packet number).
|
|
94
|
+
* @param provider Cryptographic provider.
|
|
95
|
+
* @returns Ciphertext with the 16-byte authentication tag appended.
|
|
96
|
+
*/
|
|
97
|
+
export function encryptPayload(aead, key, nonce, plaintext, aad, provider = crypto) {
|
|
98
|
+
return aeadEncrypt(aead, key, nonce, plaintext, aad, provider);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* AEAD-decrypt a payload with the given key and nonce.
|
|
102
|
+
*
|
|
103
|
+
* @param aead AEAD algorithm.
|
|
104
|
+
* @param key AEAD key.
|
|
105
|
+
* @param nonce Per-packet nonce (12 bytes), computed via {@link constructNonce}.
|
|
106
|
+
* @param ciphertext Ciphertext with the 16-byte authentication tag appended.
|
|
107
|
+
* @param aad The unprotected header (everything up to and including the
|
|
108
|
+
* packet number after header protection removal).
|
|
109
|
+
* @param provider Cryptographic provider.
|
|
110
|
+
* @returns Decrypted plaintext.
|
|
111
|
+
* @throws Error on authentication failure.
|
|
112
|
+
*/
|
|
113
|
+
export function decryptPayload(aead, key, nonce, ciphertext, aad, provider = crypto) {
|
|
114
|
+
return aeadDecrypt(aead, key, nonce, ciphertext, aad, provider);
|
|
115
|
+
}
|
|
116
|
+
/** AEAD-encrypt with a concrete @browsercore/crypto cipher id. */
|
|
117
|
+
function aeadEncrypt(algorithm, key, nonce, plaintext, aad, provider) {
|
|
118
|
+
const id = aeadAlgorithmId(algorithm);
|
|
119
|
+
switch (id) {
|
|
120
|
+
case "AES-128-GCM":
|
|
121
|
+
return provider.aes128GcmEncrypt(key, nonce, plaintext, aad);
|
|
122
|
+
case "AES-256-GCM":
|
|
123
|
+
return provider.aes256GcmEncrypt(key, nonce, plaintext, aad);
|
|
124
|
+
case "ChaCha20-Poly1305":
|
|
125
|
+
return provider.chacha20Poly1305Encrypt(key, nonce, plaintext, aad);
|
|
126
|
+
default:
|
|
127
|
+
return assertNever(id);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/** AEAD-decrypt with a concrete @browsercore/crypto cipher id. */
|
|
131
|
+
function aeadDecrypt(algorithm, key, nonce, ciphertext, aad, provider) {
|
|
132
|
+
const id = aeadAlgorithmId(algorithm);
|
|
133
|
+
switch (id) {
|
|
134
|
+
case "AES-128-GCM":
|
|
135
|
+
return provider.aes128GcmDecrypt(key, nonce, ciphertext, aad);
|
|
136
|
+
case "AES-256-GCM":
|
|
137
|
+
return provider.aes256GcmDecrypt(key, nonce, ciphertext, aad);
|
|
138
|
+
case "ChaCha20-Poly1305":
|
|
139
|
+
return provider.chacha20Poly1305Decrypt(key, nonce, ciphertext, aad);
|
|
140
|
+
default:
|
|
141
|
+
return assertNever(id);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Derive the header-protection mask for a given sample (RFC 9001 §5.4.1).
|
|
146
|
+
*
|
|
147
|
+
* mask = AES-ECB(hp_key, sample)
|
|
148
|
+
*
|
|
149
|
+
* The sample is the 16 bytes of (protected) payload starting 4 bytes past
|
|
150
|
+
* the start of the packet number. For short headers (1-RTT), the first byte
|
|
151
|
+
* of the mask is used to protect the low 5 bits of the first header byte
|
|
152
|
+
* (after header protection is removed, these encode the header form + key
|
|
153
|
+
* phase + reserved bits). For long headers, the first byte protects the low
|
|
154
|
+
* 4 bits (header form + fixed + long packet type). Bytes 1–5 of the mask
|
|
155
|
+
* protect the encoded packet number.
|
|
156
|
+
*
|
|
157
|
+
* @param hpKey Header-protection key (16 bytes for AES-128, 32 bytes for AES-256).
|
|
158
|
+
* @param sample 16-byte sample from the protected payload.
|
|
159
|
+
* @param provider Cryptographic provider.
|
|
160
|
+
* @returns A 16-byte mask; the low 5 (short header) or 4 (long header) bits of
|
|
161
|
+
* byte 0 protect the first header byte, bytes 1–5 protect the packet
|
|
162
|
+
* number.
|
|
163
|
+
*/
|
|
164
|
+
export function computeHeaderProtectionMask(hpKey, sample, provider = crypto) {
|
|
165
|
+
if (sample.length < HP_SAMPLE_LENGTH) {
|
|
166
|
+
throw new RangeError(`Header-protection sample must be at least ${HP_SAMPLE_LENGTH} bytes, got ${sample.length}`);
|
|
167
|
+
}
|
|
168
|
+
const block = sample.subarray(0, HP_SAMPLE_LENGTH);
|
|
169
|
+
return provider.aesEcbEncrypt(hpKey, block);
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Apply header protection to the first byte and packet number (RFC 9001 §5.4).
|
|
173
|
+
*
|
|
174
|
+
* @param firstByte The first header byte (already serialized).
|
|
175
|
+
* @param packetNumber The packet number to protect.
|
|
176
|
+
* @param pnLength Number of bytes the packet number occupies on the wire (1–4).
|
|
177
|
+
* @param mask Header-protection mask (from {@link computeHeaderProtectionMask}).
|
|
178
|
+
* @param isLongHeader Whether this is a long header (affects which low bits are masked).
|
|
179
|
+
* @returns The masked first byte and masked packet-number bytes.
|
|
180
|
+
*/
|
|
181
|
+
export function applyHeaderProtection(firstByte, packetNumber, pnLength, mask, isLongHeader) {
|
|
182
|
+
const firstMaskByte = mask[HP_MASK_PN_OFFSET];
|
|
183
|
+
if (firstMaskByte === undefined) {
|
|
184
|
+
throw new RangeError("header-protection mask is empty");
|
|
185
|
+
}
|
|
186
|
+
const firstByteMask = isLongHeader ? FIRST_BYTE_LOW4_MASK : FIRST_BYTE_LOW5_MASK;
|
|
187
|
+
const maskedFirstByte = firstByte ^ (firstMaskByte & firstByteMask);
|
|
188
|
+
// The packet number is masked with the low (pnLength * 8) bits of mask bytes 1..5.
|
|
189
|
+
const pnBytes = encodeBigInt(packetNumber, pnLength);
|
|
190
|
+
const maskedPnBytes = new Uint8Array(pnLength);
|
|
191
|
+
for (let i = 0; i < pnLength; i++) {
|
|
192
|
+
const maskByte = mask[HP_MASK_FIRST_BYTE_LEN + i];
|
|
193
|
+
if (maskByte === undefined) {
|
|
194
|
+
throw new RangeError(`header-protection mask too short for ${pnLength}-byte packet number`);
|
|
195
|
+
}
|
|
196
|
+
const pnByte = pnBytes[i];
|
|
197
|
+
if (pnByte === undefined) {
|
|
198
|
+
throw new RangeError(`packet number byte ${i} out of bounds`);
|
|
199
|
+
}
|
|
200
|
+
maskedPnBytes[i] = pnByte ^ maskByte;
|
|
201
|
+
}
|
|
202
|
+
return { firstByte: maskedFirstByte, packetNumberBytes: maskedPnBytes };
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Remove header protection from the first byte and packet number (RFC 9001 §5.4).
|
|
206
|
+
*
|
|
207
|
+
* @param firstByte The first header byte (as received, still masked).
|
|
208
|
+
* @param pnBytes The masked packet-number bytes (as received).
|
|
209
|
+
* @param pnLength Number of bytes the packet number occupies (1–4).
|
|
210
|
+
* @param mask Header-protection mask (from {@link computeHeaderProtectionMask}).
|
|
211
|
+
* @param isLongHeader Whether this is a long header.
|
|
212
|
+
* @returns The unmasked first byte and packet number.
|
|
213
|
+
*/
|
|
214
|
+
export function removeHeaderProtection(firstByte, pnBytes, pnLength, mask, isLongHeader) {
|
|
215
|
+
const firstMaskByte = mask[HP_MASK_PN_OFFSET];
|
|
216
|
+
if (firstMaskByte === undefined) {
|
|
217
|
+
throw new RangeError("header-protection mask is empty");
|
|
218
|
+
}
|
|
219
|
+
const firstByteMask = isLongHeader ? FIRST_BYTE_LOW4_MASK : FIRST_BYTE_LOW5_MASK;
|
|
220
|
+
const unmaskedFirstByte = firstByte ^ (firstMaskByte & firstByteMask);
|
|
221
|
+
const pnLengthActual = Math.min(pnLength, pnBytes.length);
|
|
222
|
+
let packetNumber = 0n;
|
|
223
|
+
for (let i = 0; i < pnLengthActual; i++) {
|
|
224
|
+
const maskByte = mask[HP_MASK_FIRST_BYTE_LEN + i];
|
|
225
|
+
const pnByte = pnBytes[i];
|
|
226
|
+
if (maskByte === undefined || pnByte === undefined) {
|
|
227
|
+
throw new RangeError(`header-protection mask / pn byte ${i} out of bounds`);
|
|
228
|
+
}
|
|
229
|
+
packetNumber = (packetNumber << 8n) | BigInt(pnByte ^ maskByte);
|
|
230
|
+
}
|
|
231
|
+
return { firstByte: unmaskedFirstByte, packetNumber };
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Protect a QUIC packet payload: AEAD-encrypt the payload and apply header
|
|
235
|
+
* protection to the first byte + packet number (RFC 9001 §5.3, §5.4).
|
|
236
|
+
*
|
|
237
|
+
* @param payload The unprotected frame payload.
|
|
238
|
+
* @param packetNumber The packet number to assign.
|
|
239
|
+
* @param pnLength Number of bytes the packet number occupies on the wire (1–4).
|
|
240
|
+
* @param firstByte The first header byte (already serialized, unprotected).
|
|
241
|
+
* @param aead AEAD algorithm.
|
|
242
|
+
* @param secrets QUIC protection secrets (key, iv, hp) for this direction + phase.
|
|
243
|
+
* @param isLongHeader Whether this is a long header.
|
|
244
|
+
* @param provider Cryptographic provider.
|
|
245
|
+
* @returns The AEAD-protected payload (with tag appended) — the caller prepends
|
|
246
|
+
* the masked first byte and masked packet number to form the full packet.
|
|
247
|
+
*/
|
|
248
|
+
export function protectPayload(payload, packetNumber, pnLength, firstByte, aead, secrets, isLongHeader, provider = crypto) {
|
|
249
|
+
// 1. AEAD-encrypt the payload with the header as AAD.
|
|
250
|
+
const nonce = constructNonce(secrets.iv, packetNumber);
|
|
251
|
+
// The AAD is the first byte + packet number bytes (before header protection).
|
|
252
|
+
// Header protection is applied AFTER encryption, so the AAD is the raw header.
|
|
253
|
+
const rawPnBytes = encodeBigInt(packetNumber, pnLength);
|
|
254
|
+
const aad = concat(new Uint8Array([firstByte]), rawPnBytes);
|
|
255
|
+
const protectedPayload = encryptPayload(aead, secrets.key, nonce, payload, aad, provider);
|
|
256
|
+
// 2. Sample the protected payload to derive the header-protection mask.
|
|
257
|
+
const sampleStart = HP_SAMPLE_OFFSET;
|
|
258
|
+
const sampleEnd = sampleStart + HP_SAMPLE_LENGTH;
|
|
259
|
+
if (protectedPayload.length < sampleEnd) {
|
|
260
|
+
throw new RangeError(`Protected payload too short for header-protection sample (need ${sampleEnd}, got ${protectedPayload.length})`);
|
|
261
|
+
}
|
|
262
|
+
const sample = protectedPayload.subarray(sampleStart, sampleEnd);
|
|
263
|
+
const mask = computeHeaderProtectionMask(secrets.hp, sample, provider);
|
|
264
|
+
// 3. Apply header protection to the first byte + packet number.
|
|
265
|
+
const { firstByte: maskedFirstByte, packetNumberBytes: maskedPacketNumber } = applyHeaderProtection(firstByte, packetNumber, pnLength, mask, isLongHeader);
|
|
266
|
+
return { protectedPayload, maskedFirstByte, maskedPacketNumber };
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Unprotect a QUIC packet payload: remove header protection, then AEAD-decrypt.
|
|
270
|
+
*
|
|
271
|
+
* @param firstByte The first header byte (as received, still masked).
|
|
272
|
+
* @param pnBytes The masked packet-number bytes (as received).
|
|
273
|
+
* @param pnLength Number of bytes the packet number occupies (1–4).
|
|
274
|
+
* @param protectedPayload The AEAD-protected payload (with tag appended).
|
|
275
|
+
* @param isLongHeader Whether this is a long header.
|
|
276
|
+
* @param aead AEAD algorithm.
|
|
277
|
+
* @param secrets QUIC protection secrets (key, iv, hp) for this direction + phase.
|
|
278
|
+
* @param provider Cryptographic provider.
|
|
279
|
+
* @returns The decrypted frame payload and the decoded packet number.
|
|
280
|
+
*/
|
|
281
|
+
export function unprotectPayload(firstByte, pnBytes, pnLength, protectedPayload, isLongHeader, aead, secrets, provider = crypto) {
|
|
282
|
+
// 1. Sample the protected payload to derive the header-protection mask.
|
|
283
|
+
const sampleStart = HP_SAMPLE_OFFSET;
|
|
284
|
+
const sampleEnd = sampleStart + HP_SAMPLE_LENGTH;
|
|
285
|
+
if (protectedPayload.length < sampleEnd) {
|
|
286
|
+
throw new RangeError(`Protected payload too short for header-protection sample (need ${sampleEnd}, got ${protectedPayload.length})`);
|
|
287
|
+
}
|
|
288
|
+
const sample = protectedPayload.subarray(sampleStart, sampleEnd);
|
|
289
|
+
const mask = computeHeaderProtectionMask(secrets.hp, sample, provider);
|
|
290
|
+
// 2. Remove header protection to recover the first byte + packet number.
|
|
291
|
+
const { firstByte: unmaskedFirstByte, packetNumber } = removeHeaderProtection(firstByte, pnBytes, pnLength, mask, isLongHeader);
|
|
292
|
+
// 3. AEAD-decrypt the payload with the unprotected header as AAD.
|
|
293
|
+
const nonce = constructNonce(secrets.iv, packetNumber);
|
|
294
|
+
const rawPnBytes = encodeBigInt(packetNumber, pnLength);
|
|
295
|
+
const aad = concat(new Uint8Array([unmaskedFirstByte]), rawPnBytes);
|
|
296
|
+
const payload = decryptPayload(aead, secrets.key, nonce, protectedPayload, aad, provider);
|
|
297
|
+
return { payload, packetNumber };
|
|
298
|
+
}
|
|
299
|
+
void AEAD_TAG_LENGTH;
|
|
300
|
+
/** Encode a non-negative BigInt as a big-endian byte array of the given length. */
|
|
301
|
+
function encodeBigInt(value, length) {
|
|
302
|
+
if (value < 0n) {
|
|
303
|
+
throw new RangeError(`Cannot encode negative value as unsigned: ${value}`);
|
|
304
|
+
}
|
|
305
|
+
const bytes = new Uint8Array(length);
|
|
306
|
+
let remaining = value;
|
|
307
|
+
for (let i = length - 1; i >= 0; i--) {
|
|
308
|
+
bytes[i] = Number(remaining & 0xffn);
|
|
309
|
+
remaining >>= 8n;
|
|
310
|
+
}
|
|
311
|
+
return bytes;
|
|
312
|
+
}
|
|
313
|
+
//# sourceMappingURL=packet-protection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"packet-protection.js","sourceRoot":"","sources":["../../src/packet/packet-protection.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,EAAuB,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAClE,OAAO,EAAE,cAAc,EAA8B,MAAM,6BAA6B,CAAC;AACzF,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAUlD,4EAA4E;AAC5E,MAAM,eAAe,GAAG,EAAE,CAAC;AAE3B,mEAAmE;AACnE,MAAM,gBAAgB,GAAG,EAAE,CAAC;AAE5B,mFAAmF;AACnF,MAAM,gBAAgB,GAAG,CAAC,CAAC;AAE3B,oFAAoF;AACpF,MAAM,sBAAsB,GAAG,CAAC,CAAC,CAAC,6CAA6C;AAC/E,MAAM,iBAAiB,GAAG,CAAC,CAAC,CAAC,sCAAsC;AAEnE,iGAAiG;AACjG,MAAM,oBAAoB,GAAG,IAAI,CAAC;AAClC,oGAAoG;AACpG,MAAM,oBAAoB,GAAG,IAAI,CAAC;AAElC;;;;;;;;GAQG;AACH,MAAM,UAAU,cAAc,CAAC,EAAc,EAAE,EAAU;IACrD,IAAI,EAAE,CAAC,MAAM,KAAK,cAAc,EAAE,CAAC;QAC/B,MAAM,IAAI,UAAU,CAAC,mBAAmB,cAAc,eAAe,EAAE,CAAC,MAAM,EAAE,CAAC,CAAC;IACtF,CAAC;IACD,MAAM,KAAK,GAAG,UAAU,CAAC,IAAI,C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C,OAAO,CAAC,EAAE,EAAE,YAAY,CAAC,CAAC;IACvD,MAAM,UAAU,GAAG,YAAY,CAAC,YAAY,EAAE,QAAQ,CAAC,CAAC;IACxD,MAAM,GAAG,GAAG,MAAM,CAAC,IAAI,UAAU,CAAC,CAAC,iBAAiB,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC;IACpE,MAAM,OAAO,GAAG,cAAc,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,EAAE,KAAK,EAAE,gBAAgB,EAAE,GAAG,EAAE,QAAQ,CAAC,CAAC;IAE1F,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,CAAC;AACrC,CAAC;AAED,KAAK,eAAe,CAAC;AAErB,mFAAmF;AACnF,SAAS,YAAY,CAAC,KAAa,EAAE,MAAc;IAC/C,IAAI,KAAK,GAAG,EAAE,EAAE,CAAC;QACb,MAAM,IAAI,UAAU,CAAC,6CAA6C,KAAK,EAAE,CAAC,CAAC;IAC/E,CAAC;IACD,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;IACrC,IAAI,SAAS,GAAG,KAAK,CAAC;IACtB,KAAK,IAAI,CAAC,GAAG,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QACnC,KAAK,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,SAAS,GAAG,KAAK,CAAC,CAAC;QACrC,SAAS,KAAK,EAAE,CAAC;IACrB,CAAC;IACD,OAAO,KAAK,CAAC;AACjB,CAAC"}
|
package/dist/packet/packet.d.ts
CHANGED
|
@@ -43,9 +43,9 @@ export interface ShortHeader {
|
|
|
43
43
|
}
|
|
44
44
|
export type PacketHeader = LongHeader | ShortHeader;
|
|
45
45
|
/** Serialize a long header (without payload / packet number — caller appends those). */
|
|
46
|
-
export declare function serializeLongHeader(type: LongPacketTypeValue, version: number, dcid:
|
|
46
|
+
export declare function serializeLongHeader(type: LongPacketTypeValue, version: number, dcid: Uint8Array, scid: Uint8Array, packetNumberLength: number, extra?: Uint8Array): Uint8Array;
|
|
47
47
|
/** Serialize a short (1-RTT) header (without payload / packet number). */
|
|
48
|
-
export declare function serializeShortHeader(dcid:
|
|
48
|
+
export declare function serializeShortHeader(dcid: Uint8Array, packetNumberLength: number, spinBit: boolean, keyPhase: boolean): Uint8Array;
|
|
49
49
|
/**
|
|
50
50
|
* Parse a packet header from the start of a buffer. Returns the header and the
|
|
51
51
|
* number of header bytes consumed. Throws PacketParseError on malformed input.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"packet.d.ts","sourceRoot":"","sources":["../../src/packet/packet.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EACH,gBAAgB,EAChB,iBAAiB,
|
|
1
|
+
{"version":3,"file":"packet.d.ts","sourceRoot":"","sources":["../../src/packet/packet.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EACH,gBAAgB,EAChB,iBAAiB,EAGjB,KAAK,YAAY,EACjB,KAAK,mBAAmB,EAC3B,MAAM,aAAa,CAAC;AAIrB,OAAO,EACH,cAAc,EACd,KAAK,mBAAmB,GAC3B,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAEhD,kDAAkD;AAClD,MAAM,WAAW,UAAU;IACvB,QAAQ,CAAC,IAAI,EAAE,OAAO,gBAAgB,CAAC;IACvC,QAAQ,CAAC,IAAI,EAAE,mBAAmB,CAAC;IACnC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAC5B,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAC5B,wDAAwD;IACxD,QAAQ,CAAC,kBAAkB,EAAE,MAAM,CAAC;IACpC,wEAAwE;IACxE,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;CACjC;AAED,2DAA2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16,7 @@
|
|
|
16
16
|
* first byte (long) or the byte after the DCID (short) and tells the caller how
|
|
17
17
|
* many bytes of packet number follow.
|
|
18
18
|
*/
|
|
19
|
-
import { HEADER_FORM_LONG, HEADER_FORM_SHORT, LONG_PACKET_TYPE_MASK, } from "../types.js";
|
|
19
|
+
import { HEADER_FORM_LONG, HEADER_FORM_SHORT, LONG_PACKET_TYPE_MASK, makeConnectionId, } from "../types.js";
|
|
20
20
|
import { decodeVarint, encodeVarint } from "../frame/varint.js";
|
|
21
21
|
import { concatAll } from "../utils.js";
|
|
22
22
|
export { LongPacketType, } from "../types.js";
|
|
@@ -86,7 +86,7 @@ function parseLongHeader(first, buf) {
|
|
|
86
86
|
if (buf.length < 6 + dcidLen + 1) {
|
|
87
87
|
throw new RangeError("Buffer too short for DCID");
|
|
88
88
|
}
|
|
89
|
-
const dcid = buf.subarray(6, 6 + dcidLen);
|
|
89
|
+
const dcid = makeConnectionId(buf.subarray(6, 6 + dcidLen));
|
|
90
90
|
const scidLen = buf[6 + dcidLen];
|
|
91
91
|
if (scidLen === undefined) {
|
|
92
92
|
throw new RangeError("Buffer too short for SCID");
|
|
@@ -94,7 +94,7 @@ function parseLongHeader(first, buf) {
|
|
|
94
94
|
if (buf.length < 6 + dcidLen + 1 + scidLen) {
|
|
95
95
|
throw new RangeError("Buffer too short for SCID");
|
|
96
96
|
}
|
|
97
|
-
const scid = buf.subarray(6 + dcidLen + 1, 6 + dcidLen + 1 + scidLen);
|
|
97
|
+
const scid = makeConnectionId(buf.subarray(6 + dcidLen + 1, 6 + dcidLen + 1 + scidLen));
|
|
98
98
|
const headerLength = 6 + dcidLen + 1 + scidLen;
|
|
99
99
|
return {
|
|
100
100
|
form: HEADER_FORM_LONG,
|
|
@@ -117,7 +117,7 @@ function parseShortHeader(first, _buf) {
|
|
|
117
117
|
form: HEADER_FORM_SHORT,
|
|
118
118
|
spinBit,
|
|
119
119
|
keyPhase,
|
|
120
|
-
dcid: new Uint8Array(0),
|
|
120
|
+
dcid: makeConnectionId(new Uint8Array(0)),
|
|
121
121
|
packetNumberLength,
|
|
122
122
|
headerLength: 1,
|
|
123
123
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"packet.js","sourceRoot":"","sources":["../../src/packet/packet.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EACH,gBAAgB,EAChB,iBAAiB,EACjB,qBAAqB,
|
|
1
|
+
{"version":3,"file":"packet.js","sourceRoot":"","sources":["../../src/packet/packet.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EACH,gBAAgB,EAChB,iBAAiB,EACjB,qBAAqB,EACrB,gBAAgB,GAGnB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAChE,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,OAAO,EACH,cAAc,GAEjB,MAAM,aAAa,CAAC;AA6BrB,wFAAwF;AACxF,MAAM,UAAU,mBAAmB,CAC/B,IAAyB,EACzB,OAAe,EACf,IAAgB,EAChB,IAAgB,EAChB,kBAA0B,EAC1B,KAAK,GAAe,IAAI,UAAU,CAAC,CAAC,CAAC;IAErC,MAAM,KAAK,GAAG,CAAC,gBAAgB,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,GAAG,qBAAqB,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,kBAAkB,GAAG,CAAC,CAAC,CAAC;IACpH,MAAM,YAAY,GAAG,IAAI,UAAU,CAAC,CAAC,CAAC,CAAC;IACvC,IAAI,QAAQ,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC,SAAS,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;IACxD,OAAO,SAAS,CAAC;QACb,IAAI,UAAU,CAAC,CAAC,KAAK,CAAC,CAAC;QACvB,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K,EAAE,SAAS,KAAK,IAAI,CAAC;IACtC,+CAA+C;IAC/C,eAAe,EAAE,uBAAuB,CAAC;IACzC,6DAA6D;IAC7D,cAAc,EAAE,uBAAuB,CAAC;CAC3C;AAED;;;;;;;;;GASG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,iBAAiB,GAAG,aAAa,GAAG,YAAY,CA2XzF"}
|
package/dist/stream/stream.js
CHANGED
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
*/
|
|
21
21
|
import { EventEmitter } from "node:events";
|
|
22
22
|
import { QuicFrameType, firstStreamId, makeStreamId, nextStreamId, streamIdIsBidirectional, streamIdIsClientInitiated, } from "../types.js";
|
|
23
|
-
import { ResetStreamError, StopSendingError } from "../errors.js";
|
|
24
|
-
import { concat } from "../utils.js";
|
|
23
|
+
import { ConnectionClosingError, ResetStreamError, StopSendingError } from "../errors.js";
|
|
24
|
+
import { assertNever, concat } from "../utils.js";
|
|
25
25
|
/** Empty byte array constant. */
|
|
26
26
|
const EMPTY = new Uint8Array(0);
|
|
27
27
|
// ---------------------------------------------------------------------------
|
|
@@ -318,8 +318,8 @@ class ManagedStream {
|
|
|
318
318
|
/** Force-close (connection teardown). */
|
|
319
319
|
forceClose(reason) {
|
|
320
320
|
this.state = { state: "closed", reason };
|
|
321
|
-
this.rejectReaders(new
|
|
322
|
-
this.rejectWriters(new
|
|
321
|
+
this.rejectReaders(new ConnectionClosingError("connection closed"));
|
|
322
|
+
this.rejectWriters(new ConnectionClosingError("connection closed"));
|
|
323
323
|
}
|
|
324
324
|
/** The offset up to which we have sent (for STREAM frame offset + final size). */
|
|
325
325
|
sendFinOffset = 0n;
|
|
@@ -384,7 +384,7 @@ export function createStreamManager(deps) {
|
|
|
384
384
|
// --- stream lifecycle ------------------------------------------------------
|
|
385
385
|
function openStream(bidirectional) {
|
|
386
386
|
if (closing || closed) {
|
|
387
|
-
throw new
|
|
387
|
+
throw new ConnectionClosingError();
|
|
388
388
|
}
|
|
389
389
|
const id = bidirectional ? nextBidi : nextUni;
|
|
390
390
|
if (bidirectional) {
|
|
@@ -399,7 +399,7 @@ export function createStreamManager(deps) {
|
|
|
399
399
|
}
|
|
400
400
|
function acceptStream(bidirectional) {
|
|
401
401
|
if (closing || closed) {
|
|
402
|
-
return Promise.reject(new
|
|
402
|
+
return Promise.reject(new ConnectionClosingError());
|
|
403
403
|
}
|
|
404
404
|
// Already have an incoming stream of this type waiting?
|
|
405
405
|
for (const stream of streams.values()) {
|
|
@@ -530,6 +530,10 @@ export function createStreamManager(deps) {
|
|
|
530
530
|
// handshake layer concern — informational / relay only. A full
|
|
531
531
|
// implementation would pace. We surface nothing here.
|
|
532
532
|
break;
|
|
533
|
+
default:
|
|
534
|
+
// Every QuicFrame variant is handled above; this is only reachable
|
|
535
|
+
// if a new frame type is added without a handler.
|
|
536
|
+
assertNever(frame);
|
|
533
537
|
}
|
|
534
538
|
}
|
|
535
539
|
function handleStream(frame) {
|