@browsercore/quic 0.2.1 → 0.2.3

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.
Files changed (61) hide show
  1. package/README.md +19 -1
  2. package/dist/connection.d.ts +149 -23
  3. package/dist/connection.d.ts.map +1 -1
  4. package/dist/connection.js +543 -66
  5. package/dist/connection.js.map +1 -1
  6. package/dist/crypto/hash-length.d.ts +11 -0
  7. package/dist/crypto/hash-length.d.ts.map +1 -0
  8. package/dist/crypto/hash-length.js +20 -0
  9. package/dist/crypto/hash-length.js.map +1 -0
  10. package/dist/crypto/index.d.ts +8 -0
  11. package/dist/crypto/index.d.ts.map +1 -0
  12. package/dist/crypto/index.js +8 -0
  13. package/dist/crypto/index.js.map +1 -0
  14. package/dist/crypto/key-derivation.d.ts +87 -0
  15. package/dist/crypto/key-derivation.d.ts.map +1 -0
  16. package/dist/crypto/key-derivation.js +123 -0
  17. package/dist/crypto/key-derivation.js.map +1 -0
  18. package/dist/errors.d.ts +27 -0
  19. package/dist/errors.d.ts.map +1 -1
  20. package/dist/errors.js +36 -1
  21. package/dist/errors.js.map +1 -1
  22. package/dist/frame/frame.d.ts.map +1 -1
  23. package/dist/frame/frame.js +2 -2
  24. package/dist/frame/frame.js.map +1 -1
  25. package/dist/handshake/index.d.ts +10 -0
  26. package/dist/handshake/index.d.ts.map +1 -0
  27. package/dist/handshake/index.js +9 -0
  28. package/dist/handshake/index.js.map +1 -0
  29. package/dist/handshake/quic-handshake.d.ts +119 -0
  30. package/dist/handshake/quic-handshake.d.ts.map +1 -0
  31. package/dist/handshake/quic-handshake.js +213 -0
  32. package/dist/handshake/quic-handshake.js.map +1 -0
  33. package/dist/handshake/quic-transport-adapter.d.ts +86 -0
  34. package/dist/handshake/quic-transport-adapter.d.ts.map +1 -0
  35. package/dist/handshake/quic-transport-adapter.js +166 -0
  36. package/dist/handshake/quic-transport-adapter.js.map +1 -0
  37. package/dist/index.d.ts +8 -5
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +10 -4
  40. package/dist/index.js.map +1 -1
  41. package/dist/packet/packet-protection.d.ts +158 -0
  42. package/dist/packet/packet-protection.d.ts.map +1 -0
  43. package/dist/packet/packet-protection.js +312 -0
  44. package/dist/packet/packet-protection.js.map +1 -0
  45. package/dist/packet/packet.d.ts +2 -2
  46. package/dist/packet/packet.d.ts.map +1 -1
  47. package/dist/packet/packet.js +4 -4
  48. package/dist/packet/packet.js.map +1 -1
  49. package/dist/stream/stream.d.ts +7 -11
  50. package/dist/stream/stream.d.ts.map +1 -1
  51. package/dist/stream/stream.js +16 -28
  52. package/dist/stream/stream.js.map +1 -1
  53. package/dist/transport-params.d.ts +4 -0
  54. package/dist/transport-params.d.ts.map +1 -1
  55. package/dist/transport-params.js +8 -1
  56. package/dist/transport-params.js.map +1 -1
  57. package/dist/types.d.ts +129 -41
  58. package/dist/types.d.ts.map +1 -1
  59. package/dist/types.js +8 -20
  60. package/dist/types.js.map +1 -1
  61. package/package.json +7 -5
@@ -0,0 +1,9 @@
1
+ /**
2
+ * QUIC TLS handshake (RFC 9001 §4, §8).
3
+ *
4
+ * Barrel re-export so consumers can import the handshake orchestration and the
5
+ * QUIC→TLS transport adapter from a single path.
6
+ */
7
+ export { runQuicHandshake } from "./quic-handshake.js";
8
+ export { adaptQuicStreamToTransport, QuicTransportAdapter } from "./quic-transport-adapter.js";
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/handshake/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAOvD,OAAO,EAAE,0BAA0B,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC"}
@@ -0,0 +1,119 @@
1
+ /**
2
+ * QUIC TLS handshake orchestration (RFC 9001 §4, §8).
3
+ *
4
+ * Runs the @browsercore/tls 1.3 handshake over a QUIC bidirectional stream
5
+ * (stream 0), capturing the TLS traffic secrets at each key phase so the QUIC
6
+ * layer can derive its packet-protection keys (RFC 9001 §5.1). The three key
7
+ * phases map to QUIC key phases:
8
+ *
9
+ * Initial DCID + version salt → QUIC initial keys
10
+ * TLS handshake traffic secrets (c/s hs) → QUIC handshake keys
11
+ * TLS application traffic secrets (c/s ap) → QUIC application (1-RTT) keys
12
+ *
13
+ * The @browsercore/tls `runHandshake` returns only the final
14
+ * `ApplicationTrafficSecrets`, but the `HandshakeContext` it mutates holds the
15
+ * intermediate secrets (clientHsTrafficSecret, serverHsTrafficSecret,
16
+ * masterSecret). We implement `HandshakeContext` so we can read them after the
17
+ * handshake completes, then derive the QUIC packet-protection secrets.
18
+ */
19
+ import type { CryptoProvider } from "@browsercore/contracts";
20
+ import { type HashId } from "@browsercore/crypto";
21
+ import type { Transport } from "@browsercore/transport";
22
+ import { type AeadAlgorithm, type CipherSuite, type ClientHelloConfig, type ServerHello, type Certificate, type TrafficSecrets, type HandshakeContext } from "@browsercore/tls";
23
+ import { type QuicProtectionSecrets } from "../crypto/key-derivation.js";
24
+ /** A QUIC key phase — maps to the TLS secrets that feed QUIC's key derivation. */
25
+ export type QuicKeyPhase = "initial" | "handshake" | "application";
26
+ /**
27
+ * The QUIC packet-protection secrets derived from a TLS key phase, plus the raw
28
+ * traffic secrets that fed the derivation (so callers can re-derive if needed).
29
+ */
30
+ export interface QuicPhaseSecrets {
31
+ /** QUIC key phase these secrets belong to. */
32
+ readonly phase: QuicKeyPhase;
33
+ /** Client traffic secret (hash-length bytes) for this phase. */
34
+ readonly clientTrafficSecret: Uint8Array;
35
+ /** Server traffic secret (hash-length bytes) for this phase. */
36
+ readonly serverTrafficSecret: Uint8Array;
37
+ /** Derived QUIC packet-protection secrets for the client (→ write direction). */
38
+ readonly clientProtection: QuicProtectionSecrets;
39
+ /** Derived QUIC packet-protection secrets for the server (← read direction). */
40
+ readonly serverProtection: QuicProtectionSecrets;
41
+ }
42
+ /**
43
+ * The full result of the QUIC TLS handshake — the traffic secrets at every
44
+ * phase, the negotiated AEAD + hash (so the QUIC layer can size keys), the
45
+ * negotiated ALPN, and the peer certificate.
46
+ */
47
+ export interface QuicHandshakeResult {
48
+ /** Secrets at each key phase, in derivation order. */
49
+ readonly phases: readonly QuicPhaseSecrets[];
50
+ /** Negotiated AEAD algorithm (determines key length). */
51
+ readonly aead: AeadAlgorithm;
52
+ /** Negotiated hash function (determines traffic-secret length). */
53
+ readonly hash: "SHA-256" | "SHA-384";
54
+ /** Negotiated cipher suite. */
55
+ readonly cipherSuite: CipherSuite;
56
+ /** ALPN protocol the server selected, if any. */
57
+ readonly alpnProtocol?: string;
58
+ /** Peer leaf certificate, once validated. */
59
+ readonly peerCertificate: unknown;
60
+ }
61
+ /**
62
+ * A minimal HandshakeContext implementation that delegates to @browsercore/tls's
63
+ * `runHandshake` and captures the intermediate traffic secrets for QUIC key
64
+ * derivation.
65
+ */
66
+ export declare class QuicHandshakeContext implements HandshakeContext {
67
+ readonly transport: Transport;
68
+ readonly crypto: CryptoProvider;
69
+ readBuffer: Uint8Array;
70
+ readonly transcript: Uint8Array[];
71
+ cipherSuite: CipherSuite;
72
+ aead: AeadAlgorithm;
73
+ hash: HashId;
74
+ serverHello: ServerHello;
75
+ clientHsTraffic: TrafficSecrets;
76
+ serverHsTraffic: TrafficSecrets;
77
+ clientHsTrafficSecret: Uint8Array;
78
+ serverHsTrafficSecret: Uint8Array;
79
+ masterSecret: Uint8Array;
80
+ clientHsSeq: number;
81
+ serverHsSeq: number;
82
+ alpnProtocol?: string;
83
+ peerCertificate?: Certificate;
84
+ constructor(transport: Transport, cryptoProvider: CryptoProvider);
85
+ }
86
+ /**
87
+ * Map a TLS cipher suite to its AEAD algorithm name.
88
+ * (Local copy — @browsercore/tls exports cipherSuiteToAead but we want to avoid
89
+ * a dependency on the exact export path.)
90
+ */
91
+ export declare function mapCipherSuite(cipherSuite: CipherSuite): AeadAlgorithm;
92
+ /**
93
+ * Map a TLS cipher suite to its HKDF hash function. Only AES-256-GCM /
94
+ * ChaCha20-256 use SHA-384; every other QUIC suite uses SHA-256. The default
95
+ * is the common case so non-QUIC suites still derive a valid (if mismatched) hash.
96
+ */
97
+ export declare function hashForCipherSuite(cipherSuite: CipherSuite): HashId;
98
+ /**
99
+ * AEAD key length in bytes for a cipher suite. 16 bytes for 128-bit keys,
100
+ * 32 bytes for 256-bit keys. Defaults to 16 (the common case) for any suite
101
+ * not explicitly listed.
102
+ */
103
+ export declare function cipherSuiteKeyLength(cipherSuite: CipherSuite): number;
104
+ /**
105
+ * Run the TLS 1.3 handshake over the given transport, capturing QUIC packet-
106
+ * protection secrets at each key phase.
107
+ *
108
+ * @param transport A TLS Transport (e.g. a {@link QuicTransportAdapter} wrapping QUIC stream 0).
109
+ * @param profile ClientHello configuration (cipher suites, key-share groups, etc.).
110
+ * @param serverName SNI hostname (for SNI + certificate validation).
111
+ * @param initialDcid The client's initial DCID — used to derive the initial QUIC secrets.
112
+ * @param trustAnchors Trust anchors (PEM/DER) for certificate validation. Defaults to system roots.
113
+ * @param clockNow Current epoch seconds (injected for deterministic cert validation).
114
+ * @param cryptoProvider Cryptographic provider for the QUIC key schedule
115
+ * (RFC 9001). Injected via QuicOptions.crypto or
116
+ * QuicOptions.platform.crypto.provider — required.
117
+ */
118
+ export declare function runQuicHandshake(transport: Transport, profile: ClientHelloConfig, serverName: string, initialDcid: Uint8Array, trustAnchors: readonly Uint8Array[] | undefined, clockNow: number | undefined, cryptoProvider: CryptoProvider): Promise<QuicHandshakeResult>;
119
+ //# sourceMappingURL=quic-handshake.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"quic-handshake.d.ts","sourceRoot":"","sources":["../../src/handshake/quic-handshake.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAC7D,OAAO,EAAW,KAAK,MAAM,EAAE,MAAM,qBAAqB,CA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ional stream
5
+ * (stream 0), capturing the TLS traffic secrets at each key phase so the QUIC
6
+ * layer can derive its packet-protection keys (RFC 9001 §5.1). The three key
7
+ * phases map to QUIC key phases:
8
+ *
9
+ * Initial DCID + version salt → QUIC initial keys
10
+ * TLS handshake traffic secrets (c/s hs) → QUIC handshake keys
11
+ * TLS application traffic secrets (c/s ap) → QUIC application (1-RTT) keys
12
+ *
13
+ * The @browsercore/tls `runHandshake` returns only the final
14
+ * `ApplicationTrafficSecrets`, but the `HandshakeContext` it mutates holds the
15
+ * intermediate secrets (clientHsTrafficSecret, serverHsTrafficSecret,
16
+ * masterSecret). We implement `HandshakeContext` so we can read them after the
17
+ * handshake completes, then derive the QUIC packet-protection secrets.
18
+ */
19
+ import { SHA_256 } from "@browsercore/crypto";
20
+ import { generateKeyShares, cipherSuiteToAead, runHandshake, } from "@browsercore/tls";
21
+ import { deriveInitialSecrets, deriveQuicSecrets, INITIAL_SALT_V1, } from "../crypto/key-derivation.js";
22
+ import { hashLengthFor } from "../crypto/hash-length.js";
23
+ /**
24
+ * A minimal HandshakeContext implementation that delegates to @browsercore/tls's
25
+ * `runHandshake` and captures the intermediate traffic secrets for QUIC key
26
+ * derivation.
27
+ */
28
+ export class QuicHandshakeContext {
29
+ transport;
30
+ crypto;
31
+ readBuffer = new Uint8Array(0);
32
+ transcript = [];
33
+ cipherSuite = "TLS_AES_128_GCM_SHA256";
34
+ aead = "AES-128-GCM";
35
+ hash = SHA_256;
36
+ serverHello = undefined;
37
+ clientHsTraffic = { key: new Uint8Array(0), iv: new Uint8Array(0) };
38
+ serverHsTraffic = { key: new Uint8Array(0), iv: new Uint8Array(0) };
39
+ clientHsTrafficSecret = new Uint8Array(0);
40
+ serverHsTrafficSecret = new Uint8Array(0);
41
+ masterSecret = new Uint8Array(0);
42
+ clientHsSeq = 0;
43
+ serverHsSeq = 0;
44
+ alpnProtocol;
45
+ peerCertificate;
46
+ constructor(transport, cryptoProvider) {
47
+ this.transport = transport;
48
+ this.crypto = cryptoProvider;
49
+ }
50
+ }
51
+ /**
52
+ * Map a TLS cipher suite to its AEAD algorithm name.
53
+ * (Local copy — @browsercore/tls exports cipherSuiteToAead but we want to avoid
54
+ * a dependency on the exact export path.)
55
+ */
56
+ // Lenient by design: QUIC only negotiates AEAD cipher suites, but the TLS
57
+ // handshake may report others. Known QUIC suites map explicitly; everything
58
+ // else falls back to @browsercore/tls's mapping (which is itself exhaustive).
59
+ export function mapCipherSuite(cipherSuite) {
60
+ // oxlint-disable-next-line typescript/switch-exhaustiveness-check -- intentional fallback for non-AEAD suites
61
+ switch (cipherSuite) {
62
+ case "TLS_AES_128_GCM_SHA256":
63
+ return "AES-128-GCM";
64
+ case "TLS_AES_128_CCM_SHA256":
65
+ return "AES-128-CCM";
66
+ case "TLS_AES_256_GCM_SHA384":
67
+ return "AES-256-GCM";
68
+ case "TLS_CHACHA20_POLY1305_SHA256":
69
+ return "CHACHA20-POLY1305";
70
+ default:
71
+ return cipherSuiteToAead(cipherSuite);
72
+ }
73
+ }
74
+ /**
75
+ * Map a TLS cipher suite to its HKDF hash function. Only AES-256-GCM /
76
+ * ChaCha20-256 use SHA-384; every other QUIC suite uses SHA-256. The default
77
+ * is the common case so non-QUIC suites still derive a valid (if mismatched) hash.
78
+ */
79
+ export function hashForCipherSuite(cipherSuite) {
80
+ // oxlint-disable-next-line typescript/switch-exhaustiveness-check -- SHA-256 is the correct default for all non-AES-256 suites
81
+ switch (cipherSuite) {
82
+ case "TLS_AES_256_GCM_SHA384":
83
+ return "SHA-384";
84
+ default:
85
+ return "SHA-256";
86
+ }
87
+ }
88
+ /**
89
+ * AEAD key length in bytes for a cipher suite. 16 bytes for 128-bit keys,
90
+ * 32 bytes for 256-bit keys. Defaults to 16 (the common case) for any suite
91
+ * not explicitly listed.
92
+ */
93
+ export function cipherSuiteKeyLength(cipherSuite) {
94
+ // oxlint-disable-next-line typescript/switch-exhaustiveness-check -- 16 bytes is a safe minimum default
95
+ switch (cipherSuite) {
96
+ case "TLS_AES_128_GCM_SHA256":
97
+ case "TLS_AES_128_CCM_SHA256":
98
+ case "TLS_CHACHA20_POLY1305_SHA256":
99
+ return 16;
100
+ case "TLS_AES_256_GCM_SHA384":
101
+ return 32;
102
+ default:
103
+ return 16;
104
+ }
105
+ }
106
+ /**
107
+ * Run the TLS 1.3 handshake over the given transport, capturing QUIC packet-
108
+ * protection secrets at each key phase.
109
+ *
110
+ * @param transport A TLS Transport (e.g. a {@link QuicTransportAdapter} wrapping QUIC stream 0).
111
+ * @param profile ClientHello configuration (cipher suites, key-share groups, etc.).
112
+ * @param serverName SNI hostname (for SNI + certificate validation).
113
+ * @param initialDcid The client's initial DCID — used to derive the initial QUIC secrets.
114
+ * @param trustAnchors Trust anchors (PEM/DER) for certificate validation. Defaults to system roots.
115
+ * @param clockNow Current epoch seconds (injected for deterministic cert validation).
116
+ * @param cryptoProvider Cryptographic provider for the QUIC key schedule
117
+ * (RFC 9001). Injected via QuicOptions.crypto or
118
+ * QuicOptions.platform.crypto.provider — required.
119
+ */
120
+ export async function runQuicHandshake(transport, profile, serverName, initialDcid, trustAnchors = [], clockNow = Math.floor(Date.now() / 1000), cryptoProvider) {
121
+ // --- Phase 0: derive initial QUIC secrets from the DCID (RFC 9001 §5.2) --
122
+ const initial = deriveInitialSecrets(initialDcid, INITIAL_SALT_V1, SHA_256, cryptoProvider);
123
+ const initialKeyLen = 16; // AES-128-GCM is the QUIC v1 default
124
+ const initialProtection = deriveQuicProtectionBoth(initial.clientInitialSecret, initial.serverInitialSecret, initialKeyLen, SHA_256, cryptoProvider);
125
+ // --- TLS handshake --------------------------------------------------------
126
+ const ctx = new QuicHandshakeContext(transport, cryptoProvider);
127
+ await runHandshake(ctx, profile, serverName, trustAnchors, (groups) => generateKeyShares(groups, cryptoProvider), clockNow);
128
+ // --- Phase 1: handshake QUIC keys ---------------------------------------
129
+ const negotiatedKeyLen = cipherSuiteKeyLength(ctx.cipherSuite);
130
+ const handshakeProtection = deriveQuicProtectionBoth(ctx.clientHsTrafficSecret, ctx.serverHsTrafficSecret, negotiatedKeyLen, ctx.hash, ctx.crypto);
131
+ // --- Phase 2: application (1-RTT) QUIC keys -----------------------------
132
+ // The application traffic secrets are derived from the master secret via
133
+ // HKDF-Expand-Label. We replicate the TLS key schedule's derivation here
134
+ // using the local hash-length helper + the QUIC HKDF-Expand-Label.
135
+ const hashLen = hashLengthFor(ctx.hash);
136
+ const apClientSecret = quicHkdfExpandLabelForTls(ctx.masterSecret, "c ap traffic", new Uint8Array(0), hashLen, ctx.hash, ctx.crypto);
137
+ const apServerSecret = quicHkdfExpandLabelForTls(ctx.masterSecret, "s ap traffic", new Uint8Array(0), hashLen, ctx.hash, ctx.crypto);
138
+ const applicationProtection = deriveQuicProtectionBoth(apClientSecret, apServerSecret, negotiatedKeyLen, ctx.hash, ctx.crypto);
139
+ const initialPhaseSecrets = {
140
+ phase: "initial",
141
+ clientTrafficSecret: initial.clientInitialSecret,
142
+ serverTrafficSecret: initial.serverInitialSecret,
143
+ clientProtection: initialProtection.clientProtection,
144
+ serverProtection: initialProtection.serverProtection,
145
+ };
146
+ const handshakePhaseSecrets = {
147
+ phase: "handshake",
148
+ clientTrafficSecret: ctx.clientHsTrafficSecret,
149
+ serverTrafficSecret: ctx.serverHsTrafficSecret,
150
+ clientProtection: handshakeProtection.clientProtection,
151
+ serverProtection: handshakeProtection.serverProtection,
152
+ };
153
+ const applicationPhaseSecrets = {
154
+ phase: "application",
155
+ clientTrafficSecret: apClientSecret,
156
+ serverTrafficSecret: apServerSecret,
157
+ clientProtection: applicationProtection.clientProtection,
158
+ serverProtection: applicationProtection.serverProtection,
159
+ };
160
+ return {
161
+ phases: [initialPhaseSecrets, handshakePhaseSecrets, applicationPhaseSecrets],
162
+ aead: mapCipherSuite(ctx.cipherSuite),
163
+ hash: ctx.hash === "SHA-384" ? "SHA-384" : "SHA-256",
164
+ cipherSuite: ctx.cipherSuite,
165
+ ...(ctx.alpnProtocol === undefined ? {} : { alpnProtocol: ctx.alpnProtocol }),
166
+ peerCertificate: ctx.peerCertificate,
167
+ };
168
+ }
169
+ /**
170
+ * HKDF-Expand-Label per RFC 8446 §7.1 (TLS 1.3, "tls13 " prefix).
171
+ * Used to derive the application traffic secrets from the master secret.
172
+ */
173
+ function quicHkdfExpandLabelForTls(secret, label, context, length, hash, provider) {
174
+ const prefix = "tls13 ";
175
+ const labelBytes = new TextEncoder().encode(prefix + label);
176
+ const hkdfLabel = new Uint8Array(2 + 1 + labelBytes.length + 1 + context.length);
177
+ let o = 0;
178
+ hkdfLabel[o++] = (length >> 8) & 0xff;
179
+ hkdfLabel[o++] = length & 0xff;
180
+ hkdfLabel[o++] = labelBytes.length & 0xff;
181
+ hkdfLabel.set(labelBytes, o);
182
+ o += labelBytes.length;
183
+ hkdfLabel[o++] = context.length & 0xff;
184
+ hkdfLabel.set(context, o);
185
+ return hkdfExpandLocal(hash, secret, hkdfLabel, length, provider);
186
+ }
187
+ /** Local HKDF-Expand on top of the HMAC primitive. */
188
+ function hkdfExpandLocal(hash, prk, info, length, provider) {
189
+ const hashLen = hashLengthFor(hash);
190
+ const n = Math.ceil(length / hashLen);
191
+ if (n > 255) {
192
+ throw new RangeError(`HKDF-Expand length ${length} exceeds maximum for hash (255 * ${hashLen})`);
193
+ }
194
+ const okm = new Uint8Array(n * hashLen);
195
+ let t = new Uint8Array(0);
196
+ for (let i = 1; i <= n; i++) {
197
+ const block = new Uint8Array(t.length + info.length + 1);
198
+ block.set(t, 0);
199
+ block.set(info, t.length);
200
+ block[block.length - 1] = i;
201
+ t = provider.hmac(hash, prk, block);
202
+ okm.set(t, (i - 1) * hashLen);
203
+ }
204
+ return okm.subarray(0, length);
205
+ }
206
+ /** Derive QUIC protection secrets (client + server) from a phase's traffic secrets. */
207
+ function deriveQuicProtectionBoth(clientTrafficSecret, serverTrafficSecret, keyLength, hash, provider) {
208
+ return {
209
+ clientProtection: deriveQuicSecrets(clientTrafficSecret, keyLength, hash, provider),
210
+ serverProtection: deriveQuicSecrets(serverTrafficSecret, keyLength, hash, provider),
211
+ };
212
+ }
213
+ //# sourceMappingURL=quic-handshake.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"quic-handshake.js","sourceRoot":"","sources":["../../src/handshake/quic-handshake.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAGH,OAAO,EAAE,OAAO,EAAe,MAAM,qBAAqB,CAAC;AAG3D,OAAO,EACH,iBAAiB,EACjB,iBAAiB,EACjB,YAAY,GAQf,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACH,oBAAoB,EACpB,iBAAiB,EACjB,eAAe,GAElB,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AA0CzD;;;;GAIG;AACH,MAAM,OAAO,oBAAoB;IACb,SAAS,CAAY;IACrB,MAAM,CAAiB;IAChC,UAAU,GAAe,IAAI,UAAU,CAAC,CAAC,CAAC,CAAC;IAClC,UAAU,GAAiB,EAAE,CAAC;IACvC,WAAW,GAAgB,wBAAwB,CAAC;IACpD,IAAI,GAAkB,aAAa,CAAC;IACpC,IAAI,GAAW,OAAO,CAAC;IACvB,WAAW,GAAgB,SAAmC,CAAC;IAC/D,eAAe,GAAmB,EAAE,GAAG,EAAE,IAAI,UAAU,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,IAAI,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC;IACpF,eAAe,GAAmB,EAAE,GAAG,EAAE,IAAI,UAAU,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,IAAI,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC;IACpF,qBAAqB,GAAe,IAAI,UAAU,CAAC,CAAC,CAAC,CAAC;IACtD,qBAAqB,GAAe,IAAI,UAAU,CAAC,CAAC,CAAC,CAAC;IACtD,YAAY,GAAe,IAAI,UAAU,CAAC,CAAC,CAAC,CAAC;IAC7C,WAAW,GAAG,CAAC,CAAC;IAChB,WAAW,GAAG,CAAC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AE,SAAS,EAAE,IAAI,EAAE,QAAQ,CAAC;QACnF,gBAAgB,EAAE,iBAAiB,CAAC,mBAAmB,EAAE,SAAS,EAAE,IAAI,EAAE,QAAQ,CAAC;KACtF,CAAC;AACN,CAAC"}
@@ -0,0 +1,86 @@
1
+ /**
2
+ * Bridges a QUIC bidirectional stream to the TLS Transport interface.
3
+ *
4
+ * @browsercore/tls's handshake driver reads and writes TLS records over a
5
+ * `Transport` (a reliable ordered byte stream). QUIC carries TLS handshake
6
+ * bytes inside CRYPTO frames on a single bidirectional stream (stream 0 for
7
+ * the client). This adapter implements the `Transport` interface on top of a
8
+ * `QuicStream` so the existing TLS handshake driver runs unchanged over QUIC.
9
+ *
10
+ * The TLS `Transport` interface is an EventEmitter with `read`, `write`, and
11
+ * `close` methods, plus `id` and `state` fields. QUIC streams expose the same
12
+ * three operations; the adapter is a thin wiring layer that adds the minimal
13
+ * EventEmitter + `id`/`state` surface the handshake driver's record framer
14
+ * requires.
15
+ *
16
+ * Read semantics: TLS's record-layer framer pulls bytes incrementally with
17
+ * `transport.read()` and buffers what it doesn't immediately consume. QUIC's
18
+ * `QuicStream.read()` returns the next reassembled chunk (which may be any
19
+ * size — STREAM frames can be any length). The adapter reads chunks and drains
20
+ * them byte-by-byte via an internal buffer so the TLS record framer sees a
21
+ * smooth byte stream.
22
+ */
23
+ import type { EventProvider } from "@browsercore/contracts";
24
+ import type { QuicStream } from "../types.js";
25
+ import type { CloseReason, Transport, TransportId, TransportState } from "@browsercore/transport";
26
+ /**
27
+ * Adapt a QUIC bidirectional stream into a TLS `Transport`.
28
+ *
29
+ * @param stream The QUIC bidirectional stream carrying TLS handshake bytes
30
+ * (stream 0 for the client, per RFC 9000 §2.1).
31
+ * @param events Injected EventProvider (decouples from node:events). MUST be
32
+ * provided by the composition root (browsersmith).
33
+ * @returns A Transport with `read`, `write`, `close`, `id`, `state`.
34
+ */
35
+ export declare function adaptQuicStreamToTransport(stream: QuicStream, events: EventProvider): QuicTransportAdapter;
36
+ /**
37
+ * Concrete adapter. QuicStream is NOT an EventEmitter, so we compose an
38
+ * injected EventProvider to satisfy the `Transport` interface.
39
+ */
40
+ export declare class QuicTransportAdapter implements Transport {
41
+ private readonly events;
42
+ /** The underlying QUIC stream. */
43
+ private readonly stream;
44
+ /** True once close() has been called. */
45
+ private closed;
46
+ /** Chunks read from the QUIC stream that haven't been consumed by TLS yet. */
47
+ private readonly pending;
48
+ /** A reader waiting for the next chunk from the QUIC stream. */
49
+ private waitingReader;
50
+ /** A reader that was rejected (stream closed) — read() should reject. */
51
+ private waitingReject;
52
+ readonly id: TransportId;
53
+ readonly state: TransportState;
54
+ on(event: string, listener: (...args: unknown[]) => void): void;
55
+ once(event: string, listener: (...args: unknown[]) => void): void;
56
+ off(event: string, listener: (...args: unknown[]) => void): void;
57
+ removeListener(event: string, listener: (...args: unknown[]) => void): void;
58
+ addListener(event: string, listener: (...args: unknown[]) => void): void;
59
+ emit(event: string, ...args: unknown[]): boolean;
60
+ listenerCount(event: string): number;
61
+ removeAllListeners(event?: string): void;
62
+ constructor(stream: QuicStream, events: EventProvider);
63
+ /**
64
+ * Read the next chunk of bytes from the QUIC stream.
65
+ *
66
+ * The TLS record-layer framer calls this repeatedly; each call returns the
67
+ * next reassembled chunk (which may be smaller or larger than a TLS record
68
+ * — the framer buffers internally). Resolves with an empty Uint8Array when
69
+ * the stream has reached its FIN (TLS treats that as a transport-level close).
70
+ */
71
+ read(): Promise<Uint8Array>;
72
+ /** Pull the next chunk from the QUIC stream and dispatch to the waiting reader. */
73
+ private readNextChunk;
74
+ /**
75
+ * Write bytes to the QUIC stream. Resolves when the QUIC layer has accepted
76
+ * the bytes (buffered, not necessarily on the wire).
77
+ */
78
+ write(data: Uint8Array): Promise<void>;
79
+ /**
80
+ * Close the QUIC stream (sends FIN / RESET_STREAM depending on the stream
81
+ * state). Rejects any in-flight read so the TLS record framer surfaces the
82
+ * close promptly.
83
+ */
84
+ close(_reason?: CloseReason): Promise<void>;
85
+ }
86
+ //# sourceMappingURL=quic-transport-adapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"quic-transport-adapter.d.ts","sourceRoot":"","sources":["../../src/handshake/quic-transport-adapter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAC5D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAElG;;;;;;;;GAQG;AACH,wBAAgB,0BAA0B,CAAC,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,aAAa,GAAG,oBAAoB,CAE1G;AAED;;;GAGG;AACH,qBAAa,oBAAqB,YAAW,SAAS;IAClD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAgB;IACvC,kCAAkC;IAClC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAa;IACpC,yCAAyC;IACzC,OAAO,CAAC,MAAM,CAAS;IACvB,8EAA8E;IAC9E,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAoB;IAC5C,gEAAgE;IAChE,OAAO,CAAC,aAAa,CAA4C;IACjE,yEAAyE;IACzE,OAAO,CAAC,aAAa,CAAqC;IAE1D,SAAgB,EAAE,EAAE,WAAW,CAAC;IAChC,SAAgB,KAAK,EAAE,cAAc,CAAC;IAM/B,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,IAAI,GAAG,IAAI,CAErE;IAEM,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,IAAI,GAAG,IAAI,CAEvE;IAEM,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,IAAI,GAAG,IAAI,CAEtE;IAEM,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,IAAI,GAAG,IAAI,CAEjF;IAEM,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,IAAI,GAAG,IAAI,CAE9E;IAEM,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,OAAO,CAEtD;IAEM,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAE1C;IAEM,kBAAkB,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAE9C;IAED,YAAmB,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,aAAa,EAK3D;IAED;;;;;;;OAOG;IACI,IAAI,IAAI,OAAO,CAAC,UAAU,CAAC,CAejC;IAED,mFAAmF;YACrE,aAAa;IA0B3B;;;OAGG;IACI,KAAK,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAK5C;IAED;;;;OAIG;IACI,KAAK,CAAC,OAAO,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAYjD;CACJ"}
@@ -0,0 +1,166 @@
1
+ /**
2
+ * Bridges a QUIC bidirectional stream to the TLS Transport interface.
3
+ *
4
+ * @browsercore/tls's handshake driver reads and writes TLS records over a
5
+ * `Transport` (a reliable ordered byte stream). QUIC carries TLS handshake
6
+ * bytes inside CRYPTO frames on a single bidirectional stream (stream 0 for
7
+ * the client). This adapter implements the `Transport` interface on top of a
8
+ * `QuicStream` so the existing TLS handshake driver runs unchanged over QUIC.
9
+ *
10
+ * The TLS `Transport` interface is an EventEmitter with `read`, `write`, and
11
+ * `close` methods, plus `id` and `state` fields. QUIC streams expose the same
12
+ * three operations; the adapter is a thin wiring layer that adds the minimal
13
+ * EventEmitter + `id`/`state` surface the handshake driver's record framer
14
+ * requires.
15
+ *
16
+ * Read semantics: TLS's record-layer framer pulls bytes incrementally with
17
+ * `transport.read()` and buffers what it doesn't immediately consume. QUIC's
18
+ * `QuicStream.read()` returns the next reassembled chunk (which may be any
19
+ * size — STREAM frames can be any length). The adapter reads chunks and drains
20
+ * them byte-by-byte via an internal buffer so the TLS record framer sees a
21
+ * smooth byte stream.
22
+ */
23
+ /**
24
+ * Adapt a QUIC bidirectional stream into a TLS `Transport`.
25
+ *
26
+ * @param stream The QUIC bidirectional stream carrying TLS handshake bytes
27
+ * (stream 0 for the client, per RFC 9000 §2.1).
28
+ * @param events Injected EventProvider (decouples from node:events). MUST be
29
+ * provided by the composition root (browsersmith).
30
+ * @returns A Transport with `read`, `write`, `close`, `id`, `state`.
31
+ */
32
+ export function adaptQuicStreamToTransport(stream, events) {
33
+ return new QuicTransportAdapter(stream, events);
34
+ }
35
+ /**
36
+ * Concrete adapter. QuicStream is NOT an EventEmitter, so we compose an
37
+ * injected EventProvider to satisfy the `Transport` interface.
38
+ */
39
+ export class QuicTransportAdapter {
40
+ events;
41
+ /** The underlying QUIC stream. */
42
+ stream;
43
+ /** True once close() has been called. */
44
+ closed = false;
45
+ /** Chunks read from the QUIC stream that haven't been consumed by TLS yet. */
46
+ pending = [];
47
+ /** A reader waiting for the next chunk from the QUIC stream. */
48
+ waitingReader;
49
+ /** A reader that was rejected (stream closed) — read() should reject. */
50
+ waitingReject;
51
+ id;
52
+ state;
53
+ // -------------------------------------------------------------------------
54
+ // EventProvider delegation — decouples the adapter from node:events.
55
+ // -------------------------------------------------------------------------
56
+ on(event, listener) {
57
+ this.events.on(event, listener);
58
+ }
59
+ once(event, listener) {
60
+ this.events.once(event, listener);
61
+ }
62
+ off(event, listener) {
63
+ this.events.off(event, listener);
64
+ }
65
+ removeListener(event, listener) {
66
+ this.events.removeListener(event, listener);
67
+ }
68
+ addListener(event, listener) {
69
+ this.events.on(event, listener);
70
+ }
71
+ emit(event, ...args) {
72
+ return this.events.emit(event, ...args);
73
+ }
74
+ listenerCount(event) {
75
+ return this.events.listenerCount(event);
76
+ }
77
+ removeAllListeners(event) {
78
+ this.events.removeAllListeners(event);
79
+ }
80
+ constructor(stream, events) {
81
+ this.stream = stream;
82
+ this.events = events;
83
+ this.id = `quic-stream-${stream.id}`;
84
+ this.state = { state: "open" };
85
+ }
86
+ /**
87
+ * Read the next chunk of bytes from the QUIC stream.
88
+ *
89
+ * The TLS record-layer framer calls this repeatedly; each call returns the
90
+ * next reassembled chunk (which may be smaller or larger than a TLS record
91
+ * — the framer buffers internally). Resolves with an empty Uint8Array when
92
+ * the stream has reached its FIN (TLS treats that as a transport-level close).
93
+ */
94
+ read() {
95
+ if (this.closed) {
96
+ return Promise.reject(new Error("quic transport adapter is closed"));
97
+ }
98
+ // If we have buffered chunks, return the next one.
99
+ const next = this.pending.shift();
100
+ if (next !== undefined) {
101
+ return Promise.resolve(next);
102
+ }
103
+ // Otherwise, pull the next chunk from the QUIC stream and buffer it.
104
+ return new Promise((resolve, reject) => {
105
+ this.waitingReader = resolve;
106
+ this.waitingReject = reject;
107
+ void this.readNextChunk();
108
+ });
109
+ }
110
+ /** Pull the next chunk from the QUIC stream and dispatch to the waiting reader. */
111
+ async readNextChunk() {
112
+ try {
113
+ const chunk = await this.stream.read();
114
+ if (this.waitingReader === undefined) {
115
+ // We were already rejected (closed) — buffer the chunk
116
+ // (it's actually a no-op in practice because we're closing).
117
+ this.pending.push(chunk);
118
+ return;
119
+ }
120
+ const reader = this.waitingReader;
121
+ this.waitingReader = undefined;
122
+ this.waitingReject = undefined;
123
+ // An empty chunk signals FIN on the QUIC stream — the TLS
124
+ // transport layer treats that as end-of-stream.
125
+ reader(chunk);
126
+ }
127
+ catch (err) {
128
+ if (this.waitingReject === undefined) {
129
+ return;
130
+ }
131
+ const rejecter = this.waitingReject;
132
+ this.waitingReader = undefined;
133
+ this.waitingReject = undefined;
134
+ rejecter(err);
135
+ }
136
+ }
137
+ /**
138
+ * Write bytes to the QUIC stream. Resolves when the QUIC layer has accepted
139
+ * the bytes (buffered, not necessarily on the wire).
140
+ */
141
+ write(data) {
142
+ if (this.closed) {
143
+ return Promise.reject(new Error("quic transport adapter is closed"));
144
+ }
145
+ return this.stream.write(data);
146
+ }
147
+ /**
148
+ * Close the QUIC stream (sends FIN / RESET_STREAM depending on the stream
149
+ * state). Rejects any in-flight read so the TLS record framer surfaces the
150
+ * close promptly.
151
+ */
152
+ close(_reason) {
153
+ if (this.closed) {
154
+ return Promise.resolve();
155
+ }
156
+ this.closed = true;
157
+ const rejecter = this.waitingReject;
158
+ if (rejecter !== undefined) {
159
+ this.waitingReader = undefined;
160
+ this.waitingReject = undefined;
161
+ rejecter(new Error("quic transport closed"));
162
+ }
163
+ return this.stream.close();
164
+ }
165
+ }
166
+ //# sourceMappingURL=quic-transport-adapter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"quic-transport-adapter.js","sourceRoot":"","sources":["../../src/handshake/quic-transport-adapter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAMH;;;;;;;;GAQG;AACH,MAAM,UAAU,0BAA0B,CAAC,MAAkB,EAAE,MAAqB;IAChF,OAAO,IAAI,oBAAoB,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AACpD,CAAC;AAED;;;GAGG;AACH,MAAM,OAAO,oBAAoB;IACZ,MAAM,CAAgB;IACvC,kCAAkC;IACjB,MAAM,CAAa;IACpC,yCAAyC;IACjC,MAAM,GAAG,KAAK,CAAC;IACvB,8EAA8E;IAC7D,OAAO,GAAiB,EAAE,CAAC;IAC5C,gEAAgE;IACxD,aAAa,CAA4C;IACjE,yEAAyE;IACjE,aAAa,CAAqC;IAE1C,EAAE,CAAc;IAChB,KAAK,CAAiB;IAEtC,4EAA4E;IAC5E,qEAAqE;IACrE,4EAA4E;IAErE,EAAE,CAAC,KAAa,EAAE,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