@browsercore/tls 0.1.2 → 0.1.9

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 (104) hide show
  1. package/README.md +104 -4
  2. package/dist/certificates/cert-extensions.d.ts +46 -0
  3. package/dist/certificates/cert-extensions.d.ts.map +1 -0
  4. package/dist/certificates/cert-extensions.js +203 -0
  5. package/dist/certificates/cert-extensions.js.map +1 -0
  6. package/dist/certificates/certificates.d.ts +11 -9
  7. package/dist/certificates/certificates.d.ts.map +1 -1
  8. package/dist/certificates/certificates.js +75 -503
  9. package/dist/certificates/certificates.js.map +1 -1
  10. package/dist/certificates/der.d.ts +43 -0
  11. package/dist/certificates/der.d.ts.map +1 -0
  12. package/dist/certificates/der.js +204 -0
  13. package/dist/certificates/der.js.map +1 -0
  14. package/dist/certificates/hostname.d.ts +21 -0
  15. package/dist/certificates/hostname.d.ts.map +1 -0
  16. package/dist/certificates/hostname.js +46 -0
  17. package/dist/certificates/hostname.js.map +1 -0
  18. package/dist/certificates/pem.d.ts +11 -0
  19. package/dist/certificates/pem.d.ts.map +1 -0
  20. package/dist/certificates/pem.js +69 -0
  21. package/dist/certificates/pem.js.map +1 -0
  22. package/dist/connection/handshake-driver.d.ts +59 -0
  23. package/dist/connection/handshake-driver.d.ts.map +1 -0
  24. package/dist/connection/handshake-driver.js +137 -0
  25. package/dist/connection/handshake-driver.js.map +1 -0
  26. package/dist/connection/handshake-messages.d.ts +45 -0
  27. package/dist/connection/handshake-messages.d.ts.map +1 -0
  28. package/dist/connection/handshake-messages.js +170 -0
  29. package/dist/connection/handshake-messages.js.map +1 -0
  30. package/dist/connection/index.d.ts +19 -0
  31. package/dist/connection/index.d.ts.map +1 -0
  32. package/dist/connection/index.js +19 -0
  33. package/dist/connection/index.js.map +1 -0
  34. package/dist/connection/key-exchange.d.ts +24 -0
  35. package/dist/connection/key-exchange.d.ts.map +1 -0
  36. package/dist/connection/key-exchange.js +104 -0
  37. package/dist/connection/key-exchange.js.map +1 -0
  38. package/dist/connection/lifecycle.d.ts +38 -0
  39. package/dist/connection/lifecycle.d.ts.map +1 -0
  40. package/dist/connection/lifecycle.js +101 -0
  41. package/dist/connection/lifecycle.js.map +1 -0
  42. package/dist/connection/record-layer.d.ts +65 -0
  43. package/dist/connection/record-layer.d.ts.map +1 -0
  44. package/dist/connection/record-layer.js +140 -0
  45. package/dist/connection/record-layer.js.map +1 -0
  46. package/dist/crypto/keySchedule.d.ts +18 -3
  47. package/dist/crypto/keySchedule.d.ts.map +1 -1
  48. package/dist/crypto/keySchedule.js +82 -11
  49. package/dist/crypto/keySchedule.js.map +1 -1
  50. package/dist/errors.d.ts +43 -0
  51. package/dist/errors.d.ts.map +1 -1
  52. package/dist/errors.js +60 -2
  53. package/dist/errors.js.map +1 -1
  54. package/dist/extensions/extensions.d.ts +26 -25
  55. package/dist/extensions/extensions.d.ts.map +1 -1
  56. package/dist/extensions/extensions.js +92 -49
  57. package/dist/extensions/extensions.js.map +1 -1
  58. package/dist/handshake/client-hello.d.ts +30 -0
  59. package/dist/handshake/client-hello.d.ts.map +1 -0
  60. package/dist/handshake/client-hello.js +464 -0
  61. package/dist/handshake/client-hello.js.map +1 -0
  62. package/dist/handshake/grease.d.ts +70 -0
  63. package/dist/handshake/grease.d.ts.map +1 -0
  64. package/dist/handshake/grease.js +293 -0
  65. package/dist/handshake/grease.js.map +1 -0
  66. package/dist/handshake/handshake-types.d.ts +84 -0
  67. package/dist/handshake/handshake-types.d.ts.map +1 -0
  68. package/dist/handshake/handshake-types.js +31 -0
  69. package/dist/handshake/handshake-types.js.map +1 -0
  70. package/dist/handshake/handshake.d.ts +19 -118
  71. package/dist/handshake/handshake.d.ts.map +1 -1
  72. package/dist/handshake/handshake.js +19 -412
  73. package/dist/handshake/handshake.js.map +1 -1
  74. package/dist/handshake/server-hello.d.ts +33 -0
  75. package/dist/handshake/server-hello.d.ts.map +1 -0
  76. package/dist/handshake/server-hello.js +134 -0
  77. package/dist/handshake/server-hello.js.map +1 -0
  78. package/dist/handshake/state-machine.d.ts +34 -0
  79. package/dist/handshake/state-machine.d.ts.map +1 -0
  80. package/dist/handshake/state-machine.js +105 -0
  81. package/dist/handshake/state-machine.js.map +1 -0
  82. package/dist/index.d.ts +1 -1
  83. package/dist/index.d.ts.map +1 -1
  84. package/dist/index.js +1 -1
  85. package/dist/index.js.map +1 -1
  86. package/dist/profiles/profiles.d.ts +12 -2
  87. package/dist/profiles/profiles.d.ts.map +1 -1
  88. package/dist/profiles/profiles.js +22 -7
  89. package/dist/profiles/profiles.js.map +1 -1
  90. package/dist/record/record.d.ts +18 -1
  91. package/dist/record/record.d.ts.map +1 -1
  92. package/dist/record/record.js +79 -11
  93. package/dist/record/record.js.map +1 -1
  94. package/dist/tls.d.ts +62 -101
  95. package/dist/tls.d.ts.map +1 -1
  96. package/dist/tls.js +166 -566
  97. package/dist/tls.js.map +1 -1
  98. package/dist/types.d.ts +40 -6
  99. package/dist/types.d.ts.map +1 -1
  100. package/dist/utils.d.ts +7 -0
  101. package/dist/utils.d.ts.map +1 -1
  102. package/dist/utils.js +23 -0
  103. package/dist/utils.js.map +1 -1
  104. package/package.json +53 -48
@@ -0,0 +1,293 @@
1
+ /**
2
+ * RFC 8701 GREASE (Generate Random Extensions And Sustain Extensibility).
3
+ *
4
+ * Implements the reserved sentinel values that TLS clients advertise to ensure
5
+ * peers correctly handle unknown values. A buggy server that rejects unknowns
6
+ * fails visibly instead of rusting shut undetected.
7
+ *
8
+ * GREASE must appear in: cipher suites (first position for Chrome/Edge/Safari),
9
+ * extension types, supported_versions, key_share groups, and supported_groups.
10
+ * Firefox does not grease (profile.grease = false).
11
+ *
12
+ * Reference: https://www.rfc-editor.org/rfc/rfc8701
13
+ * Verified against: curl-impersonate (lwthiker/curl-impersonate) which enables
14
+ * GREASE via `SSL_CTX_set_grease_enabled(ctx, 1)` for Chrome impersonation.
15
+ */
16
+ import { ExtensionType } from "../extensions/extensions.js";
17
+ /**
18
+ * The 16 GREASE sentinel values reserved by RFC 8701 §2.
19
+ *
20
+ * Each value has identical bytes (high === low) with low nibble 0xA, giving
21
+ * the pattern 0x?A?A. Reserved across cipher suites, ALPN identifiers,
22
+ * extension types, named groups, signature algorithms, and versions.
23
+ */
24
+ export const GREASE_VALUES = Object.freeze([
25
+ 0x0a0a, 0x1a1a, 0x2a2a, 0x3a3a,
26
+ 0x4a4a, 0x5a5a, 0x6a6a, 0x7a7a,
27
+ 0x8a8a, 0x9a9a, 0xaaaa, 0xbaba,
28
+ 0xcaca, 0xdada, 0xeaea, 0xfafa,
29
+ ]);
30
+ /** Pick a random element from a non-empty read-only array. */
31
+ function pickRandom(values, random) {
32
+ const index = Math.floor(random() * values.length);
33
+ const value = values[index];
34
+ if (value === undefined) {
35
+ // Length > 0 is guaranteed by every caller; this is an invariant guard.
36
+ throw new Error("pickRandom: unreachable — values array is empty");
37
+ }
38
+ return value;
39
+ }
40
+ /**
41
+ * Pick a GREASE value not already present in `exclude`.
42
+ *
43
+ * RFC 8701 §5 forbids duplicate extension types within a block; the same
44
+ * rule is applied uniformly so a GREASE value never collides with an
45
+ * existing entry. Throws if every sentinel is excluded — a degenerate case
46
+ * that signals a programming error in the caller.
47
+ */
48
+ function pickUniqueGrease(exclude, random) {
49
+ const available = GREASE_VALUES.filter((v) => !exclude.includes(v));
50
+ return pickRandom(available, random);
51
+ }
52
+ /**
53
+ * Generate a random GREASE cipher suite value.
54
+ *
55
+ * Returns one of the 16 RFC 8701 sentinels. The random source is injectable
56
+ * for deterministic testing; defaults to Math.random for production use.
57
+ */
58
+ export function generateGreaseCipherSuite(random = Math.random) {
59
+ return pickRandom(GREASE_VALUES, random);
60
+ }
61
+ /**
62
+ * Generate a random GREASE extension type value.
63
+ *
64
+ * The extension-type sentinels share the same numeric space as the cipher
65
+ * suite sentinels (RFC 8701 §2), so this draws from the same set.
66
+ */
67
+ export function generateGreaseExtensionType(random = Math.random) {
68
+ return pickRandom(GREASE_VALUES, random);
69
+ }
70
+ /**
71
+ * Insert a GREASE value at the head of a list.
72
+ *
73
+ * Chrome/Edge/Safari place the GREASE cipher suite first in cipher_suites so
74
+ * that servers conditioning on position 0 still see a GREASE value. The
75
+ * returned GREASE value never duplicates an existing entry (RFC 8701 §5).
76
+ */
77
+ export function insertGrease(values, random = Math.random) {
78
+ const grease = pickUniqueGrease(values, random);
79
+ return [grease, ...values];
80
+ }
81
+ /**
82
+ * Parse a ClientHello extensions block (length-prefixed list) into raw entries.
83
+ *
84
+ * Tolerant of unknown types — returns the raw uint16 instead of throwing — so
85
+ * it can round-trip GREASE extensions that are not in the ExtensionType enum.
86
+ *
87
+ * Reads bytes one at a time (mirroring extensions.ts) so truncation mid-block
88
+ * is caught by the per-byte bounds check instead of reading past the buffer.
89
+ * A declared length that exceeds the actual buffer triggers the readByte throw.
90
+ */
91
+ function parseExtensionsRaw(buf) {
92
+ if (buf.length < 2) {
93
+ return [];
94
+ }
95
+ let offset = 0;
96
+ const readByte = () => {
97
+ const byte = buf[offset];
98
+ if (byte === undefined) {
99
+ // Declared length exceeds actual buffer — surface as a parse error
100
+ // rather than silently producing corrupt output.
101
+ throw new Error(`extension byte truncated at offset ${offset}`);
102
+ }
103
+ offset++;
104
+ return byte;
105
+ };
106
+ const totalLen = (readByte() << 8) | readByte();
107
+ const result = [];
108
+ // NOTE: intentionally NOT capped at buf.length — a too-large totalLen makes
109
+ // the loop attempt to read past the buffer, triggering the readByte throw.
110
+ const end = 2 + totalLen;
111
+ while (offset < end) {
112
+ if (offset + 4 > end) {
113
+ // Not enough bytes for a full extension header — stop.
114
+ break;
115
+ }
116
+ const type = (readByte() << 8) | readByte();
117
+ const dataLen = (readByte() << 8) | readByte();
118
+ const dataStart = offset;
119
+ const dataEnd = dataStart + dataLen;
120
+ if (dataEnd > end) {
121
+ // Declared data length exceeds the block — stop before over-read.
122
+ break;
123
+ }
124
+ result.push({ type, data: buf.subarray(dataStart, dataEnd) });
125
+ offset = dataEnd;
126
+ }
127
+ return result;
128
+ }
129
+ /** Serialize raw extensions back into a length-prefixed ClientHello block. */
130
+ function serializeExtensionsRaw(extensions) {
131
+ let total = 0;
132
+ for (const ext of extensions) {
133
+ total += 4 + ext.data.length;
134
+ }
135
+ const out = new Uint8Array(2 + total);
136
+ out[0] = (total >> 8) & 0xff;
137
+ out[1] = total & 0xff;
138
+ let offset = 2;
139
+ for (const ext of extensions) {
140
+ out[offset++] = (ext.type >> 8) & 0xff;
141
+ out[offset++] = ext.type & 0xff;
142
+ out[offset++] = (ext.data.length >> 8) & 0xff;
143
+ out[offset++] = ext.data.length & 0xff;
144
+ out.set(ext.data, offset);
145
+ offset += ext.data.length;
146
+ }
147
+ return out;
148
+ }
149
+ /**
150
+ * Read a uint16 from `data` at byte `offset`, or 0 if too short.
151
+ *
152
+ * The defensive fallback (returning 0) is exercised only when the caller
153
+ * passes a truncated body — handled explicitly in tests.
154
+ */
155
+ function readUint16(data, offset) {
156
+ const hi = data[offset];
157
+ const lo = data[offset + 1];
158
+ if (hi === undefined || lo === undefined) {
159
+ return 0;
160
+ }
161
+ return (hi << 8) | lo;
162
+ }
163
+ /**
164
+ * Prepend a 2-byte value to a 1-byte-length-prefixed list (supported_versions).
165
+ *
166
+ * Layout: length(1) || values*(2). The length byte counts only the bytes that
167
+ * follow it. Uses the same read-twice-then-check pattern as key-exchange.ts /
168
+ * server-hello.ts so the indexed access satisfies noUncheckedIndexedAccess and
169
+ * the defensive branch stays reachable (empty body → fallback to 0).
170
+ */
171
+ function prependToVersionList(data, value) {
172
+ const lenByte = data[0];
173
+ const listLen = lenByte ?? 0;
174
+ const out = new Uint8Array(1 + listLen + 2);
175
+ out[0] = (listLen + 2) & 0xff;
176
+ out[1] = (value >> 8) & 0xff;
177
+ out[2] = value & 0xff;
178
+ out.set(data.subarray(1), 3);
179
+ return out;
180
+ }
181
+ /**
182
+ * Prepend a 2-byte value to a 2-byte-length-prefixed list (supported_groups).
183
+ *
184
+ * Layout: length(2) || values*(2). The length field counts only the bytes that
185
+ * follow it.
186
+ */
187
+ function prependToGroupList(data, value) {
188
+ const listLen = readUint16(data, 0);
189
+ const out = new Uint8Array(2 + listLen + 2);
190
+ out[0] = ((listLen + 2) >> 8) & 0xff;
191
+ out[1] = (listLen + 2) & 0xff;
192
+ out[2] = (value >> 8) & 0xff;
193
+ out[3] = value & 0xff;
194
+ out.set(data.subarray(2), 4);
195
+ return out;
196
+ }
197
+ /**
198
+ * Prepend a key share entry to a 2-byte-length-prefixed key_share list.
199
+ *
200
+ * Layout: length(2) || entries*(group(2) + key_len(2) + key_exchange). The
201
+ * GREASE entry carries an empty key_exchange (length 0) — RFC 8701 §3.1 says
202
+ * the key_exchange field MAY be any value for GREASE entries.
203
+ */
204
+ function prependKeyShare(data, group) {
205
+ const listLen = readUint16(data, 0);
206
+ const keyLen = 0;
207
+ const entryLen = 2 + 2 + keyLen;
208
+ const out = new Uint8Array(2 + entryLen + listLen);
209
+ out[0] = ((listLen + entryLen) >> 8) & 0xff;
210
+ out[1] = (listLen + entryLen) & 0xff;
211
+ // GREASE entry at the body head.
212
+ out[2] = (group >> 8) & 0xff;
213
+ out[3] = group & 0xff;
214
+ out[4] = (keyLen >> 8) & 0xff;
215
+ out[5] = keyLen & 0xff;
216
+ // Existing entries follow.
217
+ out.set(data.subarray(2), 2 + entryLen);
218
+ return out;
219
+ }
220
+ /**
221
+ * Apply GREASE to a ClientHello extensions block.
222
+ *
223
+ * Mutates the block in three ways:
224
+ * - supported_versions (if present): prepend a GREASE version sentinel.
225
+ * - supported_groups (if present): prepend a GREASE group sentinel.
226
+ * - key_share (if present): prepend a GREASE key share entry.
227
+ * - append 1–2 GREASE extension types with empty data.
228
+ */
229
+ function applyGreaseToExtensions(buf, random) {
230
+ const extensions = parseExtensionsRaw(buf);
231
+ const greased = [];
232
+ const used = [];
233
+ for (const ext of extensions) {
234
+ switch (ext.type) {
235
+ case ExtensionType.SUPPORTED_VERSIONS: {
236
+ const g = pickUniqueGrease(used, random);
237
+ used.push(g);
238
+ greased.push({ type: ext.type, data: prependToVersionList(ext.data, g) });
239
+ break;
240
+ }
241
+ case ExtensionType.SUPPORTED_GROUPS: {
242
+ const g = pickUniqueGrease(used, random);
243
+ used.push(g);
244
+ greased.push({ type: ext.type, data: prependToGroupList(ext.data, g) });
245
+ break;
246
+ }
247
+ case ExtensionType.KEY_SHARE: {
248
+ const g = pickUniqueGrease(used, random);
249
+ used.push(g);
250
+ greased.push({ type: ext.type, data: prependKeyShare(ext.data, g) });
251
+ break;
252
+ }
253
+ default:
254
+ // Unknown / non-GREASE-sensitive extension — pass through.
255
+ greased.push(ext);
256
+ break;
257
+ }
258
+ }
259
+ // Append 1–2 GREASE extension types. Chrome alternates between one and two.
260
+ const extraCount = 1 + Math.floor(random() * 2);
261
+ for (let i = 0; i < extraCount; i++) {
262
+ const g = pickUniqueGrease(used, random);
263
+ used.push(g);
264
+ greased.push({ type: g, data: new Uint8Array(0) });
265
+ }
266
+ return serializeExtensionsRaw(greased);
267
+ }
268
+ /**
269
+ * Apply all GREASE transformations to a ClientHello.
270
+ *
271
+ * When `profile.grease` is false (Firefox), returns the input unchanged.
272
+ * When true (Chrome/Edge/Safari), returns a new ClientHello with:
273
+ * - a GREASE cipher suite prepended to cipherSuites,
274
+ * - GREASE values injected into supported_versions / supported_groups /
275
+ * key_share extensions (when present),
276
+ * - 1–2 GREASE extension types appended to the extensions block.
277
+ *
278
+ * The original ClientHello is never mutated (immutable data rule).
279
+ */
280
+ export function applyGreaseToClientHello(hello, profile, random = Math.random) {
281
+ if (!profile.grease) {
282
+ return hello;
283
+ }
284
+ return {
285
+ protocolVersion: hello.protocolVersion,
286
+ random: hello.random,
287
+ sessionId: hello.sessionId,
288
+ cipherSuites: insertGrease(hello.cipherSuites, random),
289
+ compressionMethods: hello.compressionMethods,
290
+ extensions: applyGreaseToExtensions(hello.extensions, random),
291
+ };
292
+ }
293
+ //# sourceMappingURL=grease.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"grease.js","sourceRoot":"","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C,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;QAClB,OAAO,KAAK,CAAC;IACjB,CAAC;IAED,OAAO;QACH,eAAe,EAAE,KAAK,CAAC,eAAe;QACtC,MAAM,EAAE,KAAK,CAAC,MAAM;QACpB,SAAS,EAAE,KAAK,CAAC,SAAS;QAC1B,YAAY,EAAE,YAAY,CAAC,KAAK,CAAC,YAAY,EAAE,MAAM,CAAC;QACtD,kBAAkB,EAAE,KAAK,CAAC,kBAAkB;QAC5C,UAAU,EAAE,uBAAuB,CAAC,KAAK,CAAC,UAAU,EAAE,MAAM,CAAC;KAChE,CAAC;AACN,CAAC"}
@@ -0,0 +1,84 @@
1
+ /**
2
+ * TLS handshake message types and shared shapes (RFC 8446 §4).
3
+ *
4
+ * The wire-format constants (HandshakeType) and the structural types every
5
+ * handshake submodule agrees on (ClientHello, ServerHello, HandshakePhase) live
6
+ * here so client-hello.ts (serialization), server-hello.ts (parsing), and
7
+ * state-machine.ts (transitions) can depend on them without creating a cycle
8
+ * through the barrel.
9
+ */
10
+ import type { CipherSuite, ProtocolVersion } from "../types.js";
11
+ /** TLS handshake message types, per RFC 8446 §4. */
12
+ export declare const HandshakeType: {
13
+ readonly HELLO_REQUEST: 0;
14
+ readonly CLIENT_HELLO: 1;
15
+ readonly SERVER_HELLO: 2;
16
+ readonly NEW_SESSION_TICKET: 4;
17
+ readonly END_OF_EARLY_DATA: 5;
18
+ readonly ENCRYPTED_EXTENSIONS: 8;
19
+ readonly CERTIFICATE: 11;
20
+ readonly SERVER_KEY_EXCHANGE: 12;
21
+ readonly CERTIFICATE_REQUEST: 13;
22
+ readonly SERVER_HELLO_DONE: 14;
23
+ readonly CERTIFICATE_VERIFY: 15;
24
+ readonly CLIENT_KEY_EXCHANGE: 16;
25
+ readonly FINISHED: 20;
26
+ readonly KEY_UPDATE: 24;
27
+ readonly MESSAGE_HASH: 254;
28
+ };
29
+ /** Union of valid handshake message-type values. */
30
+ export type HandshakeType = (typeof HandshakeType)[keyof typeof HandshakeType];
31
+ /**
32
+ * A parsed ClientHello (sent by the client, parsed when acting as server — not
33
+ * used client-side but typed for completeness).
34
+ */
35
+ export interface ClientHello {
36
+ readonly protocolVersion: number;
37
+ readonly random: Uint8Array;
38
+ readonly sessionId: Uint8Array;
39
+ readonly cipherSuites: readonly number[];
40
+ readonly compressionMethods: readonly number[];
41
+ readonly extensions: Uint8Array;
42
+ }
43
+ /** A parsed ServerHello — the first handshake message the server sends. */
44
+ export interface ServerHello {
45
+ /**
46
+ * Legacy protocol version (always 0x0303 for TLS 1.3, for middlebox
47
+ * compatibility). The real negotiated version is {@link selectedVersion}.
48
+ */
49
+ readonly protocolVersion: number;
50
+ readonly random: Uint8Array;
51
+ readonly sessionId: Uint8Array;
52
+ readonly cipherSuite: CipherSuite;
53
+ readonly compressionMethod: number;
54
+ /**
55
+ * The protocol version the server actually negotiated, extracted from the
56
+ * supported_versions extension. This is the authoritative version.
57
+ */
58
+ readonly selectedVersion: ProtocolVersion;
59
+ readonly extensions: Uint8Array;
60
+ }
61
+ /**
62
+ * Phases of the client-side handshake state machine. Discriminated union so
63
+ * every transition is exhaustive: adding a phase forces every handler in
64
+ * {@link advanceHandshake} to compile-error until handled.
65
+ */
66
+ export type HandshakePhase = {
67
+ readonly phase: "start";
68
+ } | {
69
+ readonly phase: "client_hello_sent";
70
+ } | {
71
+ readonly phase: "server_hello_received";
72
+ readonly serverHello: ServerHello;
73
+ } | {
74
+ readonly phase: "encrypted_extensions_received";
75
+ } | {
76
+ readonly phase: "certificate_received";
77
+ } | {
78
+ readonly phase: "certificate_verify_received";
79
+ } | {
80
+ readonly phase: "finished_received";
81
+ } | {
82
+ readonly phase: "complete";
83
+ };
84
+ //# sourceMappingURL=handshake-types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handshake-types.d.ts","sourceRoot":"","sources":["../../src/handshake/handshake-types.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,W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AAC;IAC1C,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC;CACnC;AAED;;;;GAIG;AACH,MAAM,MAAM,cAAc,GACpB;IAAE,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAA;CAAE,GAC3B;IAAE,QAAQ,CAAC,KAAK,EAAE,mBAAmB,CAAA;CAAE,GACvC;IAAE,QAAQ,CAAC,KAAK,EAAE,uBAAuB,CAAC;IAAC,QAAQ,CAAC,WAAW,EAAE,WAAW,CAAA;CAAE,GAC9E;IAAE,QAAQ,CAAC,KAAK,EAAE,+BAA+B,CAAA;CAAE,GACnD;IAAE,QAAQ,CAAC,KAAK,EAAE,sBAAsB,CAAA;CAAE,GAC1C;IAAE,QAAQ,CAAC,KAAK,EAAE,6BAA6B,CAAA;CAAE,GACjD;IAAE,QAAQ,CAAC,KAAK,EAAE,mBAAmB,CAAA;CAAE,GACvC;IAAE,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAA;CAAE,CAAC"}
@@ -0,0 +1,31 @@
1
+ /**
2
+ * TLS handshake message types and shared shapes (RFC 8446 §4).
3
+ *
4
+ * The wire-format constants (HandshakeType) and the structural types every
5
+ * handshake submodule agrees on (ClientHello, ServerHello, HandshakePhase) live
6
+ * here so client-hello.ts (serialization), server-hello.ts (parsing), and
7
+ * state-machine.ts (transitions) can depend on them without creating a cycle
8
+ * through the barrel.
9
+ */
10
+ // Re-export the HandshakeType constants + union from a single source. Keeping
11
+ // the const object in this module (rather than importing it) means callers have
12
+ // one stable path for both the runtime values and the type.
13
+ /** TLS handshake message types, per RFC 8446 §4. */
14
+ export const HandshakeType = {
15
+ HELLO_REQUEST: 0,
16
+ CLIENT_HELLO: 1,
17
+ SERVER_HELLO: 2,
18
+ NEW_SESSION_TICKET: 4,
19
+ END_OF_EARLY_DATA: 5,
20
+ ENCRYPTED_EXTENSIONS: 8,
21
+ CERTIFICATE: 11,
22
+ SERVER_KEY_EXCHANGE: 12,
23
+ CERTIFICATE_REQUEST: 13,
24
+ SERVER_HELLO_DONE: 14,
25
+ CERTIFICATE_VERIFY: 15,
26
+ CLIENT_KEY_EXCHANGE: 16,
27
+ FINISHED: 20,
28
+ KEY_UPDATE: 24,
29
+ MESSAGE_HASH: 254,
30
+ };
31
+ //# sourceMappingURL=handshake-types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handshake-types.js","sourceRoot":"","sources":["../../src/handshake/handshake-types.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,8EAA8E;AAC9E,gFAAgF;AAChF,4DAA4D;AAC5D,oDAAoD;AACpD,MAAM,CAAC,MAAM,aAAa,GAAG;IACzB,aAAa,EAAE,CAAC;IAChB,YAAY,EAAE,CAAC;IACf,YAAY,EAAE,CAAC;IACf,kBAAkB,EAAE,CAAC;IACrB,iBAAiB,EAAE,CAAC;IACpB,oBAAoB,EAAE,CAAC;IACvB,WAAW,EAAE,EAAE;IACf,mBAAmB,EAAE,EAAE;IACvB,mBAAmB,EAAE,EAAE;IACvB,iBAAiB,EAAE,EAAE;IACrB,kBAAkB,EAAE,EAAE;IACtB,mBAAmB,EAAE,EAAE;IACvB,QAAQ,EAAE,EAAE;IACZ,UAAU,EAAE,EAAE;IACd,YAAY,EAAE,GAAG;CACX,CAAC"}
@@ -1,124 +1,25 @@
1
1
  /**
2
- * TLS handshake protocol (RFC 8446 §4, RFC 5246 §7).
2
+ * TLS handshake protocol (RFC 8446 §4, RFC 5246 §7) — module barrel.
3
3
  *
4
- * Builds and parses the handshake messages and drives the handshake state machine.
5
- * Cryptographic operations (key generation, signing, verification) are delegated
6
- * to @browsercore/crypto; this module owns message layout and state transitions.
7
- */
8
- import type { CipherSuite, ClientHelloConfig, KeyPair, NamedGroup, ProtocolVersion } from "../types.js";
9
- /** TLS handshake message types, per RFC 8446 §4. */
10
- export declare const HandshakeType: {
11
- readonly HELLO_REQUEST: 0;
12
- readonly CLIENT_HELLO: 1;
13
- readonly SERVER_HELLO: 2;
14
- readonly NEW_SESSION_TICKET: 4;
15
- readonly END_OF_EARLY_DATA: 5;
16
- readonly ENCRYPTED_EXTENSIONS: 8;
17
- readonly CERTIFICATE: 11;
18
- readonly SERVER_KEY_EXCHANGE: 12;
19
- readonly CERTIFICATE_REQUEST: 13;
20
- readonly SERVER_HELLO_DONE: 14;
21
- readonly CERTIFICATE_VERIFY: 15;
22
- readonly CLIENT_KEY_EXCHANGE: 16;
23
- readonly FINISHED: 20;
24
- readonly KEY_UPDATE: 24;
25
- readonly MESSAGE_HASH: 254;
26
- };
27
- /** Union of valid handshake message-type values. */
28
- export type HandshakeType = (typeof HandshakeType)[keyof typeof HandshakeType];
29
- /** A parsed ClientHello (sent by the client, parsed when acting as server — not used client-side but typed for completeness). */
30
- export interface ClientHello {
31
- readonly protocolVersion: number;
32
- readonly random: Uint8Array;
33
- readonly sessionId: Uint8Array;
34
- readonly cipherSuites: readonly number[];
35
- readonly compressionMethods: readonly number[];
36
- readonly extensions: Uint8Array;
37
- }
38
- /** A parsed ServerHello — the first handshake message the server sends. */
39
- export interface ServerHello {
40
- /**
41
- * Legacy protocol version (always 0x0303 for TLS 1.3, for middlebox
42
- * compatibility). The real negotiated version is {@link selectedVersion}.
43
- */
44
- readonly protocolVersion: number;
45
- readonly random: Uint8Array;
46
- readonly sessionId: Uint8Array;
47
- readonly cipherSuite: CipherSuite;
48
- readonly compressionMethod: number;
49
- /**
50
- * The protocol version the server actually negotiated, extracted from the
51
- * supported_versions extension. This is the authoritative version.
52
- */
53
- readonly selectedVersion: ProtocolVersion;
54
- readonly extensions: Uint8Array;
55
- }
56
- /** Phases of the client-side handshake state machine. */
57
- export type HandshakePhase = {
58
- readonly phase: "start";
59
- } | {
60
- readonly phase: "client_hello_sent";
61
- } | {
62
- readonly phase: "server_hello_received";
63
- readonly serverHello: ServerHello;
64
- } | {
65
- readonly phase: "encrypted_extensions_received";
66
- } | {
67
- readonly phase: "certificate_received";
68
- } | {
69
- readonly phase: "certificate_verify_received";
70
- } | {
71
- readonly phase: "finished_received";
72
- } | {
73
- readonly phase: "complete";
74
- };
75
- /**
76
- * Build a ClientHello handshake message from the given config.
4
+ * Builds and parses the handshake messages and drives the handshake state
5
+ * machine. Cryptographic operations (key generation, signing, verification) are
6
+ * delegated to @browsercore/crypto; this module owns message layout and state
7
+ * transitions only.
77
8
  *
78
- * Serializes the full handshake message (header + body) with extensions for SNI,
79
- * supported_versions, key_share, signature_algorithms, and (optionally) ALPN.
80
- * The (EC)DHE public keys come from `keyPairs` (already generated by the caller
81
- * via @browsercore/crypto).
82
- */
83
- export declare function buildClientHello(config: ClientHelloConfig, keyPairs: readonly KeyPair[]): Uint8Array;
84
- /**
85
- * What the client offered in its ClientHello, needed so the ServerHello can be
86
- * validated against it. Only the fields that {@link parseServerHello} checks are
87
- * required — everything else is irrelevant to ServerHello parsing.
88
- */
89
- export interface ServerHelloValidation {
90
- /** Cipher suites the client advertised, most-preferred first. */
91
- readonly cipherSuites: readonly CipherSuite[];
92
- /** Protocol versions the client advertised via supported_versions. */
93
- readonly supportedVersions: readonly ProtocolVersion[];
94
- }
95
- /**
96
- * Parse a ServerHello from a handshake message body (without the 4-byte handshake header).
9
+ * The work is split across focused submodules, each responsible for one layer:
10
+ * - `handshake-types.ts` — shared types (ClientHello, ServerHello, HandshakePhase)
11
+ * and the HandshakeType constants. Imported by every other submodule so they
12
+ * agree on shapes without cycling through this barrel.
13
+ * - `client-hello.ts` — ClientHello serialization (the only place that knows
14
+ * the client's opening-message wire layout).
15
+ * - `server-hello.ts` — ServerHello parsing + version/cipher negotiation.
16
+ * - `state-machine.ts` — the handshake phase transitions and predicates.
97
17
  *
98
- * Validates that the selected cipher suite was actually offered and that the
99
- * negotiated protocol version (from the supported_versions extension) is one we
100
- * support — failing fast makes an unacceptable negotiation unrepresentable.
101
- *
102
- * Throws {@link TlsHandshakeError} with phase "server_hello" on malformed input
103
- * or a failed validation.
104
- */
105
- export declare function parseServerHello(buf: Uint8Array, offered: ServerHelloValidation): ServerHello;
106
- /**
107
- * Transition the handshake state machine given the next received message type.
108
- * Returns the next phase, or throws {@link TlsHandshakeError} on an invalid
109
- * transition (making invalid state sequences unrepresentable).
110
- */
111
- export declare function advanceHandshake(current: HandshakePhase, received: HandshakeType): HandshakePhase;
112
- /**
113
- * Record the parsed ServerHello and transition from "client_hello_sent" to
114
- * "server_hello_received". Separated from {@link advanceHandshake} because the
115
- * state machine needs to carry the parsed ServerHello forward.
18
+ * This file re-exports them so callers have a single stable import path
19
+ * (`./handshake/handshake.js`); it deliberately holds no logic of its own.
116
20
  */
117
- export declare function recordServerHello(_current: HandshakePhase, serverHello: ServerHello): HandshakePhase;
118
- /** Mark the handshake as complete (after client Finished is sent). */
119
- export declare function completeHandshake(_current: HandshakePhase): HandshakePhase;
120
- /** True if the named group is a (EC)DHE group usable for TLS 1.3 key share. */
121
- export declare function isKeyShareGroup(group: NamedGroup): boolean;
122
- /** True if the protocol version is TLS 1.3. */
123
- export declare function isTls13(version: ProtocolVersion): boolean;
21
+ export { HandshakeType, type ClientHello, type ServerHello, type HandshakePhase, } from "./handshake-types.js";
22
+ export { buildClientHello, cipherSuiteToWire } from "./client-hello.js";
23
+ export { parseServerHello, type ServerHelloValidation } from "./server-hello.js";
24
+ export { advanceHandshake, recordServerHello, completeHandshake, isKeyShareGroup, isTls13, } from "./state-machine.js";
124
25
  //# sourceMappingURL=handshake.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"handshake.d.ts","sourceRoot":"","sources":["../../src/handshake/handshake.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,KAAK,EACR,WAAW,EACX,iBAAiB,EACjB,OAAO,EACP,UAAU,EACV,eAAe,EAElB,MAAM,aAAa,CAAC;AAYrB,oDAAoD;AACpD,eAAO,MAAM,aAAa;;;;;;;;;;;;;;;;CAgBhB,CAAC;AAEX,oDAAoD;AACpD,MAAM,MAAM,aAAa,GAAG,CAAC,OAAO,aAAa,CAAC,CAAC,MAAM,OAAO,aAAa,CAAC,CAAC;AAE/E,iI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