@browsercore/tls 0.1.3 → 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.
- package/README.md +104 -4
- package/dist/certificates/cert-extensions.d.ts +46 -0
- package/dist/certificates/cert-extensions.d.ts.map +1 -0
- package/dist/certificates/cert-extensions.js +203 -0
- package/dist/certificates/cert-extensions.js.map +1 -0
- package/dist/certificates/certificates.d.ts +11 -9
- package/dist/certificates/certificates.d.ts.map +1 -1
- package/dist/certificates/certificates.js +75 -503
- package/dist/certificates/certificates.js.map +1 -1
- package/dist/certificates/der.d.ts +43 -0
- package/dist/certificates/der.d.ts.map +1 -0
- package/dist/certificates/der.js +204 -0
- package/dist/certificates/der.js.map +1 -0
- package/dist/certificates/hostname.d.ts +21 -0
- package/dist/certificates/hostname.d.ts.map +1 -0
- package/dist/certificates/hostname.js +46 -0
- package/dist/certificates/hostname.js.map +1 -0
- package/dist/certificates/pem.d.ts +11 -0
- package/dist/certificates/pem.d.ts.map +1 -0
- package/dist/certificates/pem.js +69 -0
- package/dist/certificates/pem.js.map +1 -0
- package/dist/connection/handshake-driver.d.ts +59 -0
- package/dist/connection/handshake-driver.d.ts.map +1 -0
- package/dist/connection/handshake-driver.js +137 -0
- package/dist/connection/handshake-driver.js.map +1 -0
- package/dist/connection/handshake-messages.d.ts +45 -0
- package/dist/connection/handshake-messages.d.ts.map +1 -0
- package/dist/connection/handshake-messages.js +170 -0
- package/dist/connection/handshake-messages.js.map +1 -0
- package/dist/connection/index.d.ts +19 -0
- package/dist/connection/index.d.ts.map +1 -0
- package/dist/connection/index.js +19 -0
- package/dist/connection/index.js.map +1 -0
- package/dist/connection/key-exchange.d.ts +24 -0
- package/dist/connection/key-exchange.d.ts.map +1 -0
- package/dist/connection/key-exchange.js +104 -0
- package/dist/connection/key-exchange.js.map +1 -0
- package/dist/connection/lifecycle.d.ts +38 -0
- package/dist/connection/lifecycle.d.ts.map +1 -0
- package/dist/connection/lifecycle.js +101 -0
- package/dist/connection/lifecycle.js.map +1 -0
- package/dist/connection/record-layer.d.ts +65 -0
- package/dist/connection/record-layer.d.ts.map +1 -0
- package/dist/connection/record-layer.js +140 -0
- package/dist/connection/record-layer.js.map +1 -0
- package/dist/crypto/keySchedule.d.ts +18 -3
- package/dist/crypto/keySchedule.d.ts.map +1 -1
- package/dist/crypto/keySchedule.js +82 -11
- package/dist/crypto/keySchedule.js.map +1 -1
- package/dist/errors.d.ts +43 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +60 -2
- package/dist/errors.js.map +1 -1
- package/dist/extensions/extensions.d.ts +26 -25
- package/dist/extensions/extensions.d.ts.map +1 -1
- package/dist/extensions/extensions.js +92 -49
- package/dist/extensions/extensions.js.map +1 -1
- package/dist/handshake/client-hello.d.ts +30 -0
- package/dist/handshake/client-hello.d.ts.map +1 -0
- package/dist/handshake/client-hello.js +464 -0
- package/dist/handshake/client-hello.js.map +1 -0
- package/dist/handshake/grease.d.ts +70 -0
- package/dist/handshake/grease.d.ts.map +1 -0
- package/dist/handshake/grease.js +293 -0
- package/dist/handshake/grease.js.map +1 -0
- package/dist/handshake/handshake-types.d.ts +84 -0
- package/dist/handshake/handshake-types.d.ts.map +1 -0
- package/dist/handshake/handshake-types.js +31 -0
- package/dist/handshake/handshake-types.js.map +1 -0
- package/dist/handshake/handshake.d.ts +19 -118
- package/dist/handshake/handshake.d.ts.map +1 -1
- package/dist/handshake/handshake.js +19 -412
- package/dist/handshake/handshake.js.map +1 -1
- package/dist/handshake/server-hello.d.ts +33 -0
- package/dist/handshake/server-hello.d.ts.map +1 -0
- package/dist/handshake/server-hello.js +134 -0
- package/dist/handshake/server-hello.js.map +1 -0
- package/dist/handshake/state-machine.d.ts +34 -0
- package/dist/handshake/state-machine.d.ts.map +1 -0
- package/dist/handshake/state-machine.js +105 -0
- package/dist/handshake/state-machine.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/profiles/profiles.d.ts +12 -2
- package/dist/profiles/profiles.d.ts.map +1 -1
- package/dist/profiles/profiles.js +22 -7
- package/dist/profiles/profiles.js.map +1 -1
- package/dist/record/record.d.ts +18 -1
- package/dist/record/record.d.ts.map +1 -1
- package/dist/record/record.js +79 -11
- package/dist/record/record.js.map +1 -1
- package/dist/tls.d.ts +62 -101
- package/dist/tls.d.ts.map +1 -1
- package/dist/tls.js +166 -566
- package/dist/tls.js.map +1 -1
- package/dist/types.d.ts +40 -6
- package/dist/types.d.ts.map +1 -1
- package/dist/utils.d.ts +7 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +23 -0
- package/dist/utils.js.map +1 -1
- package/package.json +53 -48
|
@@ -1,418 +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
|
|
5
|
-
* Cryptographic operations (key generation, signing, verification) are
|
|
6
|
-
* to @browsercore/crypto; this module owns message layout and state
|
|
7
|
-
|
|
8
|
-
import { crypto } from "@browsercore/crypto";
|
|
9
|
-
import { TlsHandshakeError } from "../errors.js";
|
|
10
|
-
import { assertNever } from "../utils.js";
|
|
11
|
-
import { ExtensionType, findExtension, namedGroupToWire, parseExtensions, signatureSchemeToWire, } from "../extensions/extensions.js";
|
|
12
|
-
import { assertCipherSuiteOffered, assertVersionSupported } from "../crypto/keySchedule.js";
|
|
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
|
-
/** TLS 1.2 wire version used as legacy_version in TLS 1.3 records/handshakes. */
|
|
32
|
-
const TLS_1_2_WIRE_VERSION = 0x0303;
|
|
33
|
-
/** Cipher suite IANA wire values (subset used by TLS 1.3). */
|
|
34
|
-
function cipherSuiteToWire(suite) {
|
|
35
|
-
switch (suite) {
|
|
36
|
-
case "TLS_AES_128_GCM_SHA256":
|
|
37
|
-
return 0x1301;
|
|
38
|
-
case "TLS_AES_256_GCM_SHA384":
|
|
39
|
-
return 0x1302;
|
|
40
|
-
case "TLS_CHACHA20_POLY1305_SHA256":
|
|
41
|
-
return 0x1303;
|
|
42
|
-
case "TLS_AES_128_CCM_SHA256":
|
|
43
|
-
return 0x1304;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
/** Invert {@link cipherSuiteToWire}; throws on unknown values. */
|
|
47
|
-
function wireToCipherSuite(wire) {
|
|
48
|
-
switch (wire) {
|
|
49
|
-
case 0x1301:
|
|
50
|
-
return "TLS_AES_128_GCM_SHA256";
|
|
51
|
-
case 0x1302:
|
|
52
|
-
return "TLS_AES_256_GCM_SHA384";
|
|
53
|
-
case 0x1303:
|
|
54
|
-
return "TLS_CHACHA20_POLY1305_SHA256";
|
|
55
|
-
case 0x1304:
|
|
56
|
-
return "TLS_AES_128_CCM_SHA256";
|
|
57
|
-
default:
|
|
58
|
-
throw new TlsHandshakeError("server_hello", {
|
|
59
|
-
cause: new Error(`unsupported cipher suite wire value: 0x${wire.toString(16)}`),
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* 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.
|
|
65
8
|
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const compressionMethods = new Uint8Array([0x00]);
|
|
75
|
-
const extensions = buildClientHelloExtensions(config, keyPairs);
|
|
76
|
-
// Body length: version(2) + random(32) + session_id_len(1) + session_id +
|
|
77
|
-
// cipher_suites_len(2) + cipher_suites + compression_len(1) + compression +
|
|
78
|
-
// extensions_len(2) + extensions.
|
|
79
|
-
const cipherSuitesBytes = new Uint8Array(config.cipherSuites.length * 2);
|
|
80
|
-
for (let i = 0; i < config.cipherSuites.length; i++) {
|
|
81
|
-
const wire = cipherSuiteToWire(config.cipherSuites[i]);
|
|
82
|
-
cipherSuitesBytes[i * 2] = (wire >> 8) & 0xff;
|
|
83
|
-
cipherSuitesBytes[i * 2 + 1] = wire & 0xff;
|
|
84
|
-
}
|
|
85
|
-
const bodyLen = 2 + random.length + 1 + sessionId.length +
|
|
86
|
-
2 + cipherSuitesBytes.length +
|
|
87
|
-
1 + compressionMethods.length +
|
|
88
|
-
2 + extensions.length;
|
|
89
|
-
// Handshake header: msg_type(1) + length(24-bit) + body.
|
|
90
|
-
const message = new Uint8Array(1 + 3 + bodyLen);
|
|
91
|
-
let o = 0;
|
|
92
|
-
message[o++] = HandshakeType.CLIENT_HELLO;
|
|
93
|
-
message[o++] = (bodyLen >> 16) & 0xff;
|
|
94
|
-
message[o++] = (bodyLen >> 8) & 0xff;
|
|
95
|
-
message[o++] = bodyLen & 0xff;
|
|
96
|
-
// legacy_version = 0x0303 (TLS 1.2 for middlebox compatibility).
|
|
97
|
-
message[o++] = (TLS_1_2_WIRE_VERSION >> 8) & 0xff;
|
|
98
|
-
message[o++] = TLS_1_2_WIRE_VERSION & 0xff;
|
|
99
|
-
message.set(random, o);
|
|
100
|
-
o += random.length;
|
|
101
|
-
// Session ID (length-prefixed; empty for TLS 1.3).
|
|
102
|
-
message[o++] = sessionId.length & 0xff;
|
|
103
|
-
message.set(sessionId, o);
|
|
104
|
-
o += sessionId.length;
|
|
105
|
-
// Cipher suites (length-prefixed).
|
|
106
|
-
message[o++] = (cipherSuitesBytes.length >> 8) & 0xff;
|
|
107
|
-
message[o++] = cipherSuitesBytes.length & 0xff;
|
|
108
|
-
message.set(cipherSuitesBytes, o);
|
|
109
|
-
o += cipherSuitesBytes.length;
|
|
110
|
-
// Compression methods (length-prefixed; single null).
|
|
111
|
-
message[o++] = compressionMethods.length & 0xff;
|
|
112
|
-
message.set(compressionMethods, o);
|
|
113
|
-
o += compressionMethods.length;
|
|
114
|
-
// Extensions (length-prefixed).
|
|
115
|
-
message[o++] = (extensions.length >> 8) & 0xff;
|
|
116
|
-
message[o++] = extensions.length & 0xff;
|
|
117
|
-
message.set(extensions, o);
|
|
118
|
-
return message;
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* Serialize the extensions block for a ClientHello. Each extension is
|
|
122
|
-
* type(2) || data_len(2) || data, all under a single length-prefix.
|
|
123
|
-
*/
|
|
124
|
-
function buildClientHelloExtensions(config, keyPairs) {
|
|
125
|
-
const parts = [
|
|
126
|
-
wrapExtension(ExtensionType.SERVER_NAME, encodeServerNameList(config.serverName)),
|
|
127
|
-
wrapExtension(ExtensionType.SUPPORTED_VERSIONS, encodeSupportedVersionsClient(config.supportedVersions)),
|
|
128
|
-
wrapExtension(ExtensionType.KEY_SHARE, encodeKeyShareClient(keyPairs)),
|
|
129
|
-
wrapExtension(ExtensionType.SIGNATURE_ALGORITHMS, encodeSignatureAlgorithms(config.signatureAlgorithms)),
|
|
130
|
-
...(config.alpnProtocols !== undefined && config.alpnProtocols.length > 0
|
|
131
|
-
? [wrapExtension(ExtensionType.APPLICATION_LAYER_PROTOCOL_NEGOTIATION, encodeAlpn(config.alpnProtocols))]
|
|
132
|
-
: []),
|
|
133
|
-
];
|
|
134
|
-
let total = 0;
|
|
135
|
-
for (const p of parts) {
|
|
136
|
-
total += p.length;
|
|
137
|
-
}
|
|
138
|
-
const out = new Uint8Array(total);
|
|
139
|
-
let o = 0;
|
|
140
|
-
for (const p of parts) {
|
|
141
|
-
out.set(p, o);
|
|
142
|
-
o += p.length;
|
|
143
|
-
}
|
|
144
|
-
return out;
|
|
145
|
-
}
|
|
146
|
-
/** Wrap an extension body with its type + length prefix. */
|
|
147
|
-
function wrapExtension(type, data) {
|
|
148
|
-
const out = new Uint8Array(2 + 2 + data.length);
|
|
149
|
-
out[0] = (type >> 8) & 0xff;
|
|
150
|
-
out[1] = type & 0xff;
|
|
151
|
-
out[2] = (data.length >> 8) & 0xff;
|
|
152
|
-
out[3] = data.length & 0xff;
|
|
153
|
-
out.set(data, 4);
|
|
154
|
-
return out;
|
|
155
|
-
}
|
|
156
|
-
/** Encode the SNI server_name_list body (RFC 6066 §3). */
|
|
157
|
-
function encodeServerNameList(serverName) {
|
|
158
|
-
const nameBytes = new TextEncoder().encode(serverName);
|
|
159
|
-
if (nameBytes.length > 0xffff) {
|
|
160
|
-
throw new TlsHandshakeError("client_hello", {
|
|
161
|
-
cause: new Error("SNI server_name exceeds 65535 bytes"),
|
|
162
|
-
});
|
|
163
|
-
}
|
|
164
|
-
// server_name_list: length(2) + entries. Each entry: type(1)=0 + length(2) + name.
|
|
165
|
-
const entry = new Uint8Array(1 + 2 + nameBytes.length);
|
|
166
|
-
entry[0] = 0; // host_name
|
|
167
|
-
entry[1] = (nameBytes.length >> 8) & 0xff;
|
|
168
|
-
entry[2] = nameBytes.length & 0xff;
|
|
169
|
-
entry.set(nameBytes, 3);
|
|
170
|
-
const out = new Uint8Array(2 + entry.length);
|
|
171
|
-
out[0] = (entry.length >> 8) & 0xff;
|
|
172
|
-
out[1] = entry.length & 0xff;
|
|
173
|
-
out.set(entry, 2);
|
|
174
|
-
return out;
|
|
175
|
-
}
|
|
176
|
-
/** Encode the supported_versions extension body for the client (RFC 8446 §4.2.1). */
|
|
177
|
-
function encodeSupportedVersionsClient(versions) {
|
|
178
|
-
const out = new Uint8Array(1 + versions.length * 2);
|
|
179
|
-
out[0] = (versions.length * 2) & 0xff;
|
|
180
|
-
for (let i = 0; i < versions.length; i++) {
|
|
181
|
-
const wire = versions[i].wire;
|
|
182
|
-
out[1 + i * 2] = (wire >> 8) & 0xff;
|
|
183
|
-
out[1 + i * 2 + 1] = wire & 0xff;
|
|
184
|
-
}
|
|
185
|
-
return out;
|
|
186
|
-
}
|
|
187
|
-
/** Encode the key_share extension body for the client (RFC 8446 §4.2.8). */
|
|
188
|
-
function encodeKeyShareClient(keyPairs) {
|
|
189
|
-
// client_shares: length(2) + entries. Each: group(2) + len(2) + key_exchange.
|
|
190
|
-
let entriesLen = 0;
|
|
191
|
-
for (const kp of keyPairs) {
|
|
192
|
-
entriesLen += 2 + 2 + kp.publicKey.length;
|
|
193
|
-
}
|
|
194
|
-
const out = new Uint8Array(2 + entriesLen);
|
|
195
|
-
out[0] = (entriesLen >> 8) & 0xff;
|
|
196
|
-
out[1] = entriesLen & 0xff;
|
|
197
|
-
let o = 2;
|
|
198
|
-
for (const kp of keyPairs) {
|
|
199
|
-
const groupWire = namedGroupToWire(kp.algorithm);
|
|
200
|
-
out[o++] = (groupWire >> 8) & 0xff;
|
|
201
|
-
out[o++] = groupWire & 0xff;
|
|
202
|
-
out[o++] = (kp.publicKey.length >> 8) & 0xff;
|
|
203
|
-
out[o++] = kp.publicKey.length & 0xff;
|
|
204
|
-
out.set(kp.publicKey, o);
|
|
205
|
-
o += kp.publicKey.length;
|
|
206
|
-
}
|
|
207
|
-
return out;
|
|
208
|
-
}
|
|
209
|
-
/** Encode the signature_algorithms extension body (RFC 8446 §4.2.3). */
|
|
210
|
-
function encodeSignatureAlgorithms(algorithms) {
|
|
211
|
-
const out = new Uint8Array(2 + algorithms.length * 2);
|
|
212
|
-
out[0] = ((algorithms.length * 2) >> 8) & 0xff;
|
|
213
|
-
out[1] = (algorithms.length * 2) & 0xff;
|
|
214
|
-
for (let i = 0; i < algorithms.length; i++) {
|
|
215
|
-
const wire = signatureSchemeToWire(algorithms[i]);
|
|
216
|
-
out[2 + i * 2] = (wire >> 8) & 0xff;
|
|
217
|
-
out[2 + i * 2 + 1] = wire & 0xff;
|
|
218
|
-
}
|
|
219
|
-
return out;
|
|
220
|
-
}
|
|
221
|
-
/** Encode the ALPN extension body (RFC 7301). */
|
|
222
|
-
function encodeAlpn(protocols) {
|
|
223
|
-
let entriesLen = 0;
|
|
224
|
-
for (const proto of protocols) {
|
|
225
|
-
const encoded = new TextEncoder().encode(proto);
|
|
226
|
-
if (encoded.length === 0 || encoded.length > 0xff) {
|
|
227
|
-
throw new TlsHandshakeError("client_hello", {
|
|
228
|
-
cause: new Error(`ALPN protocol must be 1..255 bytes: "${proto}"`),
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
entriesLen += 1 + encoded.length;
|
|
232
|
-
}
|
|
233
|
-
const out = new Uint8Array(2 + entriesLen);
|
|
234
|
-
out[0] = (entriesLen >> 8) & 0xff;
|
|
235
|
-
out[1] = entriesLen & 0xff;
|
|
236
|
-
let o = 2;
|
|
237
|
-
for (const proto of protocols) {
|
|
238
|
-
const encoded = new TextEncoder().encode(proto);
|
|
239
|
-
out[o++] = encoded.length & 0xff;
|
|
240
|
-
out.set(encoded, o);
|
|
241
|
-
o += encoded.length;
|
|
242
|
-
}
|
|
243
|
-
return out;
|
|
244
|
-
}
|
|
245
|
-
/**
|
|
246
|
-
* Map a 16-bit wire version to the branded {@link ProtocolVersion} from the
|
|
247
|
-
* offered list. Throws if the server selected a version we did not offer.
|
|
248
|
-
*/
|
|
249
|
-
function selectVersion(wire, offered) {
|
|
250
|
-
for (const version of offered) {
|
|
251
|
-
if (version.wire === wire) {
|
|
252
|
-
return version;
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
throw new TlsHandshakeError("server_hello", {
|
|
256
|
-
cause: new Error(`server negotiated version we did not offer: 0x${wire.toString(16)}`),
|
|
257
|
-
});
|
|
258
|
-
}
|
|
259
|
-
/**
|
|
260
|
-
* 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.
|
|
261
17
|
*
|
|
262
|
-
*
|
|
263
|
-
*
|
|
264
|
-
* support — failing fast makes an unacceptable negotiation unrepresentable.
|
|
265
|
-
*
|
|
266
|
-
* Throws {@link TlsHandshakeError} with phase "server_hello" on malformed input
|
|
267
|
-
* or a failed validation.
|
|
268
|
-
*/
|
|
269
|
-
export function parseServerHello(buf, offered) {
|
|
270
|
-
let o = 0;
|
|
271
|
-
const expect = (n) => {
|
|
272
|
-
if (o + n > buf.length) {
|
|
273
|
-
throw new TlsHandshakeError("server_hello", {
|
|
274
|
-
cause: new Error(`ServerHello truncated at offset ${o} (need ${n}, have ${buf.length - o})`),
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
|
-
};
|
|
278
|
-
expect(2 + 32 + 1);
|
|
279
|
-
const protocolVersion = (buf[o++] << 8) | buf[o++];
|
|
280
|
-
const random = buf.subarray(o, o + 32);
|
|
281
|
-
o += 32;
|
|
282
|
-
const sessionIdLen = buf[o++];
|
|
283
|
-
expect(sessionIdLen);
|
|
284
|
-
const sessionId = buf.subarray(o, o + sessionIdLen);
|
|
285
|
-
o += sessionIdLen;
|
|
286
|
-
expect(2 + 1 + 2);
|
|
287
|
-
const cipherSuiteWire = (buf[o++] << 8) | buf[o++];
|
|
288
|
-
const cipherSuite = wireToCipherSuite(cipherSuiteWire);
|
|
289
|
-
const compressionMethod = buf[o++];
|
|
290
|
-
if (compressionMethod !== 0x00) {
|
|
291
|
-
throw new TlsHandshakeError("server_hello", {
|
|
292
|
-
cause: new Error(`unsupported compression method: ${compressionMethod}`),
|
|
293
|
-
});
|
|
294
|
-
}
|
|
295
|
-
// The server MUST negotiate a cipher suite we offered.
|
|
296
|
-
assertCipherSuiteOffered(cipherSuite, offered.cipherSuites);
|
|
297
|
-
// Capture the offset before reading extensions_len so the slice retains the
|
|
298
|
-
// 2-byte length prefix. parseExtensions (used both here for the
|
|
299
|
-
// supported_versions extension and later by the key-share extraction in
|
|
300
|
-
// _computeSharedSecret) requires its input to be the length-prefixed block.
|
|
301
|
-
const extensionsStart = o;
|
|
302
|
-
const extensionsLen = (buf[o++] << 8) | buf[o++];
|
|
303
|
-
expect(extensionsLen);
|
|
304
|
-
const extensions = buf.subarray(extensionsStart, o + extensionsLen);
|
|
305
|
-
const selectedVersion = negotiateVersion(extensions, offered.supportedVersions);
|
|
306
|
-
void protocolVersion;
|
|
307
|
-
return { protocolVersion, random, sessionId, cipherSuite, compressionMethod, selectedVersion, extensions };
|
|
308
|
-
}
|
|
309
|
-
/**
|
|
310
|
-
* Extract the version the server negotiated from the supported_versions
|
|
311
|
-
* extension (RFC 8446 §4.2.1) and validate it. The server's extension body is a
|
|
312
|
-
* single uint16 — the one version it selected from our offered list.
|
|
313
|
-
*/
|
|
314
|
-
function negotiateVersion(extensionsRaw, offered) {
|
|
315
|
-
const extensions = parseExtensions(extensionsRaw);
|
|
316
|
-
const sv = findExtension(extensions, ExtensionType.SUPPORTED_VERSIONS);
|
|
317
|
-
if (sv === undefined) {
|
|
318
|
-
throw new TlsHandshakeError("server_hello", {
|
|
319
|
-
cause: new Error("ServerHello missing required supported_versions extension"),
|
|
320
|
-
});
|
|
321
|
-
}
|
|
322
|
-
if (sv.data.length !== 2) {
|
|
323
|
-
throw new TlsHandshakeError("server_hello", {
|
|
324
|
-
cause: new Error(`supported_versions extension has unexpected length ${sv.data.length}`),
|
|
325
|
-
});
|
|
326
|
-
}
|
|
327
|
-
const wire = (sv.data[0] << 8) | sv.data[1];
|
|
328
|
-
const selected = selectVersion(wire, offered);
|
|
329
|
-
assertVersionSupported(selected);
|
|
330
|
-
return selected;
|
|
331
|
-
}
|
|
332
|
-
/**
|
|
333
|
-
* Transition the handshake state machine given the next received message type.
|
|
334
|
-
* Returns the next phase, or throws {@link TlsHandshakeError} on an invalid
|
|
335
|
-
* transition (making invalid state sequences unrepresentable).
|
|
336
|
-
*/
|
|
337
|
-
export function advanceHandshake(current, received) {
|
|
338
|
-
switch (current.phase) {
|
|
339
|
-
case "start":
|
|
340
|
-
throw new TlsHandshakeError("client_hello", {
|
|
341
|
-
cause: new Error("expected client_hello_sent as the first transition"),
|
|
342
|
-
});
|
|
343
|
-
case "client_hello_sent":
|
|
344
|
-
if (received !== HandshakeType.SERVER_HELLO) {
|
|
345
|
-
throw new TlsHandshakeError("server_hello", {
|
|
346
|
-
cause: new Error(`expected SERVER_HELLO after client_hello_sent, got ${received}`),
|
|
347
|
-
});
|
|
348
|
-
}
|
|
349
|
-
// The caller must call `recordServerHello` to attach the parsed ServerHello.
|
|
350
|
-
throw new TlsHandshakeError("server_hello", {
|
|
351
|
-
cause: new Error("use recordServerHello(serverHello) to transition client_hello_sent -> server_hello_received"),
|
|
352
|
-
});
|
|
353
|
-
case "server_hello_received":
|
|
354
|
-
if (received !== HandshakeType.ENCRYPTED_EXTENSIONS) {
|
|
355
|
-
throw new TlsHandshakeError("server_hello", {
|
|
356
|
-
cause: new Error(`expected ENCRYPTED_EXTENSIONS, got ${received}`),
|
|
357
|
-
});
|
|
358
|
-
}
|
|
359
|
-
return { phase: "encrypted_extensions_received" };
|
|
360
|
-
case "encrypted_extensions_received":
|
|
361
|
-
if (received !== HandshakeType.CERTIFICATE) {
|
|
362
|
-
throw new TlsHandshakeError("certificate", {
|
|
363
|
-
cause: new Error(`expected CERTIFICATE, got ${received}`),
|
|
364
|
-
});
|
|
365
|
-
}
|
|
366
|
-
return { phase: "certificate_received" };
|
|
367
|
-
case "certificate_received":
|
|
368
|
-
if (received !== HandshakeType.CERTIFICATE_VERIFY) {
|
|
369
|
-
throw new TlsHandshakeError("certificate", {
|
|
370
|
-
cause: new Error(`expected CERTIFICATE_VERIFY, got ${received}`),
|
|
371
|
-
});
|
|
372
|
-
}
|
|
373
|
-
return { phase: "certificate_verify_received" };
|
|
374
|
-
case "certificate_verify_received":
|
|
375
|
-
if (received !== HandshakeType.FINISHED) {
|
|
376
|
-
throw new TlsHandshakeError("finished", {
|
|
377
|
-
cause: new Error(`expected FINISHED, got ${received}`),
|
|
378
|
-
});
|
|
379
|
-
}
|
|
380
|
-
return { phase: "finished_received" };
|
|
381
|
-
case "finished_received":
|
|
382
|
-
case "complete":
|
|
383
|
-
throw new TlsHandshakeError("finished", {
|
|
384
|
-
cause: new Error(`handshake already in terminal phase: ${current.phase}`),
|
|
385
|
-
});
|
|
386
|
-
default:
|
|
387
|
-
return assertNever(current);
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
/**
|
|
391
|
-
* Record the parsed ServerHello and transition from "client_hello_sent" to
|
|
392
|
-
* "server_hello_received". Separated from {@link advanceHandshake} because the
|
|
393
|
-
* 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.
|
|
394
20
|
*/
|
|
395
|
-
export
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
}
|
|
399
|
-
/** Mark the handshake as complete (after client Finished is sent). */
|
|
400
|
-
export function completeHandshake(_current) {
|
|
401
|
-
void _current;
|
|
402
|
-
return { phase: "complete" };
|
|
403
|
-
}
|
|
404
|
-
/** True if the named group is a (EC)DHE group usable for TLS 1.3 key share. */
|
|
405
|
-
export function isKeyShareGroup(group) {
|
|
406
|
-
switch (group) {
|
|
407
|
-
case "secp256r1":
|
|
408
|
-
case "secp384r1":
|
|
409
|
-
case "x25519":
|
|
410
|
-
case "x448":
|
|
411
|
-
return true;
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
/** True if the protocol version is TLS 1.3. */
|
|
415
|
-
export function isTls13(version) {
|
|
416
|
-
return version.name === "TLS 1.3";
|
|
417
|
-
}
|
|
21
|
+
export { HandshakeType, } from "./handshake-types.js";
|
|
22
|
+
export { buildClientHello, cipherSuiteToWire } from "./client-hello.js";
|
|
23
|
+
export { parseServerHello } from "./server-hello.js";
|
|
24
|
+
export { advanceHandshake, recordServerHello, completeHandshake, isKeyShareGroup, isTls13, } from "./state-machine.js";
|
|
418
25
|
//# sourceMappingURL=handshake.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handshake.js","sourceRoot":"","sources":["../../src/handshake/handshake.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAS7C,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EACH,aAAa,EACb,aAAa,EACb,gBAAgB,EAChB,eAAe,EACf,qBAAqB,GACxB,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,wBAAwB,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAE5F,oDAAoD;AACpD,MAAM,CAAC,MAAM,aAAa,GAAG;IACzB,aAAa,EAAE,CAAC;IAChB,YAAY,EAAE,CAAC;IACf,YAAY,EAAE,CAAC;IACf,k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AW,CAAC;QACjB,KAAK,QAAQ,CAAC;QACd,KAAK,MAAM;YACP,OAAO,IAAI,CAAC;IACpB,CAAC;AACL,CAAC;AAED,+CAA+C;AAC/C,MAAM,UAAU,OAAO,CAAC,OAAwB;IAC5C,OAAO,OAAO,CAAC,IAAI,KAAK,SAAS,CAAC;AACtC,CAAC"}
|
|
1
|
+
{"version":3,"file":"handshake.js","sourceRoot":"","sources":["../../src/handshake/handshake.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EACH,aAAa,GAIhB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACxE,OAAO,EAAE,gBAAgB,EAA8B,MAAM,mBAAmB,CAAC;AACjF,OAAO,EACH,gBAAgB,EAChB,iBAAiB,EACjB,iBAAiB,EACjB,eAAe,EACf,OAAO,GACV,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ServerHello parsing and version negotiation (RFC 8446 §4.2.1).
|
|
3
|
+
*
|
|
4
|
+
* Decodes the server's ServerHello and validates it against what the client
|
|
5
|
+
* offered — the selected cipher suite must be one we advertised, and the
|
|
6
|
+
* negotiated version (from the supported_versions extension) must be one we
|
|
7
|
+
* support. Failing fast here makes an unacceptable negotiation unrepresentable.
|
|
8
|
+
*/
|
|
9
|
+
import type { CipherSuite, ProtocolVersion } from "../types.js";
|
|
10
|
+
import type { ServerHello } from "./handshake-types.js";
|
|
11
|
+
/**
|
|
12
|
+
* What the client offered in its ClientHello, needed so the ServerHello can be
|
|
13
|
+
* validated against it. Only the fields that {@link parseServerHello} checks are
|
|
14
|
+
* required — everything else is irrelevant to ServerHello parsing.
|
|
15
|
+
*/
|
|
16
|
+
export interface ServerHelloValidation {
|
|
17
|
+
/** Cipher suites the client advertised, most-preferred first. */
|
|
18
|
+
readonly cipherSuites: readonly CipherSuite[];
|
|
19
|
+
/** Protocol versions the client advertised via supported_versions. */
|
|
20
|
+
readonly supportedVersions: readonly ProtocolVersion[];
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Parse a ServerHello from a handshake message body (without the 4-byte handshake header).
|
|
24
|
+
*
|
|
25
|
+
* Validates that the selected cipher suite was actually offered and that the
|
|
26
|
+
* negotiated protocol version (from the supported_versions extension) is one we
|
|
27
|
+
* support — failing fast makes an unacceptable negotiation unrepresentable.
|
|
28
|
+
*
|
|
29
|
+
* Throws {@link TlsHandshakeError} with phase "server_hello" on malformed input
|
|
30
|
+
* or a failed validation.
|
|
31
|
+
*/
|
|
32
|
+
export declare function parseServerHello(buf: Uint8Array, offered: ServerHelloValidation): ServerHello;
|
|
33
|
+
//# sourceMappingURL=server-hello.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server-hello.d.ts","sourceRoot":"","sources":["../../src/handshake/server-hello.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAIhE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAExD;;;;GAIG;AACH,MAAM,WAAW,qBAAqB;IAClC,iEAAiE;IACjE,QAAQ,CAAC,YAAY,EAAE,SAAS,WAAW,EAAE,CAAC;IAC9C,sEAAsE;IACtE,QAAQ,CAAC,iBAAiB,EAAE,SAAS,eAAe,EAAE,CAAC;CAC1D;AAmCD;;;;;;;;;GASG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,UAAU,EAAE,OAAO,EAAE,qBAAqB,GAAG,WAAW,CAyD7F"}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ServerHello parsing and version negotiation (RFC 8446 §4.2.1).
|
|
3
|
+
*
|
|
4
|
+
* Decodes the server's ServerHello and validates it against what the client
|
|
5
|
+
* offered — the selected cipher suite must be one we advertised, and the
|
|
6
|
+
* negotiated version (from the supported_versions extension) must be one we
|
|
7
|
+
* support. Failing fast here makes an unacceptable negotiation unrepresentable.
|
|
8
|
+
*/
|
|
9
|
+
import { TlsHandshakeError } from "../errors.js";
|
|
10
|
+
import { ExtensionType, findExtension, parseExtensions } from "../extensions/extensions.js";
|
|
11
|
+
import { assertCipherSuiteOffered, assertVersionSupported } from "../crypto/keySchedule.js";
|
|
12
|
+
/** Invert the cipher-suite wire values; throws on unknown values. */
|
|
13
|
+
function wireToCipherSuite(wire) {
|
|
14
|
+
switch (wire) {
|
|
15
|
+
case 0x1301:
|
|
16
|
+
return "TLS_AES_128_GCM_SHA256";
|
|
17
|
+
case 0x1302:
|
|
18
|
+
return "TLS_AES_256_GCM_SHA384";
|
|
19
|
+
case 0x1303:
|
|
20
|
+
return "TLS_CHACHA20_POLY1305_SHA256";
|
|
21
|
+
case 0x1304:
|
|
22
|
+
return "TLS_AES_128_CCM_SHA256";
|
|
23
|
+
default:
|
|
24
|
+
throw new TlsHandshakeError("server_hello", {
|
|
25
|
+
cause: new Error(`unsupported cipher suite wire value: 0x${wire.toString(16)}`),
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Map a 16-bit wire version to the branded {@link ProtocolVersion} from the
|
|
31
|
+
* offered list. Throws if the server selected a version we did not offer.
|
|
32
|
+
*/
|
|
33
|
+
function selectVersion(wire, offered) {
|
|
34
|
+
for (const version of offered) {
|
|
35
|
+
if (version.wire === wire) {
|
|
36
|
+
return version;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
throw new TlsHandshakeError("server_hello", {
|
|
40
|
+
cause: new Error(`server negotiated version we did not offer: 0x${wire.toString(16)}`),
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Parse a ServerHello from a handshake message body (without the 4-byte handshake header).
|
|
45
|
+
*
|
|
46
|
+
* Validates that the selected cipher suite was actually offered and that the
|
|
47
|
+
* negotiated protocol version (from the supported_versions extension) is one we
|
|
48
|
+
* support — failing fast makes an unacceptable negotiation unrepresentable.
|
|
49
|
+
*
|
|
50
|
+
* Throws {@link TlsHandshakeError} with phase "server_hello" on malformed input
|
|
51
|
+
* or a failed validation.
|
|
52
|
+
*/
|
|
53
|
+
export function parseServerHello(buf, offered) {
|
|
54
|
+
let o = 0;
|
|
55
|
+
const expect = (n) => {
|
|
56
|
+
if (o + n > buf.length) {
|
|
57
|
+
throw new TlsHandshakeError("server_hello", {
|
|
58
|
+
cause: new Error(`ServerHello truncated at offset ${o} (need ${n}, have ${buf.length - o})`),
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
const readByte = () => {
|
|
63
|
+
expect(1);
|
|
64
|
+
const byte = buf[o];
|
|
65
|
+
if (byte === undefined) {
|
|
66
|
+
throw new TlsHandshakeError("server_hello", {
|
|
67
|
+
cause: new Error(`ServerHello byte truncated at offset ${o}`),
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
o++;
|
|
71
|
+
return byte;
|
|
72
|
+
};
|
|
73
|
+
expect(2 + 32 + 1);
|
|
74
|
+
const protocolVersion = (readByte() << 8) | readByte();
|
|
75
|
+
const random = buf.subarray(o, o + 32);
|
|
76
|
+
o += 32;
|
|
77
|
+
const sessionIdLen = readByte();
|
|
78
|
+
expect(sessionIdLen);
|
|
79
|
+
const sessionId = buf.subarray(o, o + sessionIdLen);
|
|
80
|
+
o += sessionIdLen;
|
|
81
|
+
expect(2 + 1 + 2);
|
|
82
|
+
const cipherSuiteWire = (readByte() << 8) | readByte();
|
|
83
|
+
const cipherSuite = wireToCipherSuite(cipherSuiteWire);
|
|
84
|
+
const compressionMethod = readByte();
|
|
85
|
+
if (compressionMethod !== 0x00) {
|
|
86
|
+
throw new TlsHandshakeError("server_hello", {
|
|
87
|
+
cause: new Error(`unsupported compression method: ${compressionMethod}`),
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
// The server MUST negotiate a cipher suite we offered.
|
|
91
|
+
assertCipherSuiteOffered(cipherSuite, offered.cipherSuites);
|
|
92
|
+
// Capture the offset before reading extensions_len so the slice retains the
|
|
93
|
+
// 2-byte length prefix. parseExtensions (used both here for the
|
|
94
|
+
// supported_versions extension and later by the key-share extraction in
|
|
95
|
+
// _computeSharedSecret) requires its input to be the length-prefixed block.
|
|
96
|
+
const extensionsStart = o;
|
|
97
|
+
const extensionsLen = (readByte() << 8) | readByte();
|
|
98
|
+
expect(extensionsLen);
|
|
99
|
+
const extensions = buf.subarray(extensionsStart, o + extensionsLen);
|
|
100
|
+
const selectedVersion = negotiateVersion(extensions, offered.supportedVersions);
|
|
101
|
+
void protocolVersion;
|
|
102
|
+
return { protocolVersion, random, sessionId, cipherSuite, compressionMethod, selectedVersion, extensions };
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Extract the version the server negotiated from the supported_versions
|
|
106
|
+
* extension (RFC 8446 §4.2.1) and validate it. The server's extension body is a
|
|
107
|
+
* single uint16 — the one version it selected from our offered list.
|
|
108
|
+
*/
|
|
109
|
+
function negotiateVersion(extensionsRaw, offered) {
|
|
110
|
+
const extensions = parseExtensions(extensionsRaw);
|
|
111
|
+
const sv = findExtension(extensions, ExtensionType.SUPPORTED_VERSIONS);
|
|
112
|
+
if (sv === undefined) {
|
|
113
|
+
throw new TlsHandshakeError("server_hello", {
|
|
114
|
+
cause: new Error("ServerHello missing required supported_versions extension"),
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
if (sv.data.length !== 2) {
|
|
118
|
+
throw new TlsHandshakeError("server_hello", {
|
|
119
|
+
cause: new Error(`supported_versions extension has unexpected length ${sv.data.length}`),
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
const hi = sv.data[0];
|
|
123
|
+
const lo = sv.data[1];
|
|
124
|
+
if (hi === undefined || lo === undefined) {
|
|
125
|
+
throw new TlsHandshakeError("server_hello", {
|
|
126
|
+
cause: new Error(`supported_versions extension data truncated`),
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
const wire = (hi << 8) | lo;
|
|
130
|
+
const selected = selectVersion(wire, offered);
|
|
131
|
+
assertVersionSupported(selected);
|
|
132
|
+
return selected;
|
|
133
|
+
}
|
|
134
|
+
//# sourceMappingURL=server-hello.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server-hello.js","sourceRoot":"","sources":["../../src/handshake/server-hello.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC5F,OAAO,EAAE,wBAAwB,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAe5F,qEAAqE;AACrE,SAAS,iBAAiB,CAAC,IAAY;IACnC,QAAQ,IAAI,EAAE,CAAC;QACX,KAAK,MAAM;YACP,OAAO,wBAAwB,CAAC;QACpC,KAAK,MAAM;YACP,OAAO,wBAAwB,CAAC;QACpC,KAAK,MAAM;YACP,OAAO,8BAA8B,CAAC;QAC1C,KAAK,MAAM;YACP,OAAO,wBAAwB,CAAC;QACpC;YACI,MAAM,IAAI,iBAAiB,CAAC,cAAc,EAAE;gBACxC,KAAK,EAAE,IAAI,KAAK,CAAC,0CAA0C,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC;aAClF,CAAC,CAAC;IACX,CAAC;AACL,CAAC;AAED;;;GAGG;AACH,SAAS,aAAa,CAAC,IAAY,EAAE,OAAmC;IACpE,KAAK,MAAM,OAAO,IAAI,OAAO,EAAE,CAAC;QAC5B,IAAI,OAAO,CAAC,IAAI,KAAK,IAAI,EAAE,CAAC;YACxB,OAAO,OAAO,CAAC;QACnB,CAAC;IACL,CAAC;IACD,MAAM,IAAI,iBAAiB,CAAC,cAAc,EAAE;QACxC,K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