@browsercore/tls 0.4.3 → 0.4.5
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 +4 -5
- package/dist/connection/handshake-driver.d.ts +29 -0
- package/dist/connection/handshake-driver.d.ts.map +1 -1
- package/dist/connection/handshake-driver.js +129 -20
- package/dist/connection/handshake-driver.js.map +1 -1
- package/dist/connection/handshake-messages.d.ts +51 -1
- package/dist/connection/handshake-messages.d.ts.map +1 -1
- package/dist/connection/handshake-messages.js +154 -12
- package/dist/connection/handshake-messages.js.map +1 -1
- package/dist/connection/index.d.ts +3 -1
- package/dist/connection/index.d.ts.map +1 -1
- package/dist/connection/index.js +2 -1
- package/dist/connection/index.js.map +1 -1
- package/dist/connection/key-exchange.d.ts.map +1 -1
- package/dist/connection/key-exchange.js +12 -7
- package/dist/connection/key-exchange.js.map +1 -1
- package/dist/connection/lifecycle.d.ts +18 -5
- package/dist/connection/lifecycle.d.ts.map +1 -1
- package/dist/connection/lifecycle.js +20 -7
- package/dist/connection/lifecycle.js.map +1 -1
- package/dist/connection/record-layer.d.ts +5 -5
- package/dist/connection/record-layer.d.ts.map +1 -1
- package/dist/connection/record-layer.js +19 -12
- package/dist/connection/record-layer.js.map +1 -1
- package/dist/crypto/keySchedule.d.ts +18 -2
- package/dist/crypto/keySchedule.d.ts.map +1 -1
- package/dist/crypto/keySchedule.js +13 -1
- package/dist/crypto/keySchedule.js.map +1 -1
- package/dist/errors.d.ts +31 -2
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +20 -0
- package/dist/errors.js.map +1 -1
- package/dist/handshake/client-hello.d.ts +1 -1
- package/dist/handshake/client-hello.d.ts.map +1 -1
- package/dist/handshake/client-hello.js +115 -41
- package/dist/handshake/client-hello.js.map +1 -1
- package/dist/handshake/handshake.d.ts +1 -0
- package/dist/handshake/handshake.d.ts.map +1 -1
- package/dist/handshake/handshake.js +1 -0
- package/dist/handshake/handshake.js.map +1 -1
- package/dist/handshake/hello-retry-request.d.ts +89 -0
- package/dist/handshake/hello-retry-request.d.ts.map +1 -0
- package/dist/handshake/hello-retry-request.js +152 -0
- package/dist/handshake/hello-retry-request.js.map +1 -0
- package/dist/handshake/server-hello.d.ts.map +1 -1
- package/dist/handshake/server-hello.js +34 -0
- package/dist/handshake/server-hello.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/record/record-padding.d.ts +35 -0
- package/dist/record/record-padding.d.ts.map +1 -0
- package/dist/record/record-padding.js +52 -0
- package/dist/record/record-padding.js.map +1 -0
- package/dist/session/session-cache.d.ts +84 -0
- package/dist/session/session-cache.d.ts.map +1 -0
- package/dist/session/session-cache.js +189 -0
- package/dist/session/session-cache.js.map +1 -0
- package/dist/tls.d.ts +78 -4
- package/dist/tls.d.ts.map +1 -1
- package/dist/tls.js +225 -20
- package/dist/tls.js.map +1 -1
- package/dist/types.d.ts +34 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
import { TlsHandshakeError } from "../errors.js";
|
|
10
10
|
import { ExtensionType, findExtension, parseExtensions } from "../extensions/extensions.js";
|
|
11
11
|
import { assertCipherSuiteOffered, assertVersionSupported } from "../crypto/keySchedule.js";
|
|
12
|
+
import { constantTimeEqual } from "../utils.js";
|
|
12
13
|
/**
|
|
13
14
|
* Common TLS 1.2 cipher suites (IANA registry). The client only speaks TLS 1.3,
|
|
14
15
|
* so a server negotiating any of these is a protocol downgrade. Stored as a Set
|
|
@@ -22,6 +23,35 @@ const TLS12_CIPHER_SUITES = new Set([
|
|
|
22
23
|
0xc02b, 0xc02c, 0xc02f, 0xc030, 0xcca8, 0xcca9,
|
|
23
24
|
0xccaa, 0x0004, 0x0005, 0x000a, 0x0009, 0x0016,
|
|
24
25
|
]);
|
|
26
|
+
/**
|
|
27
|
+
* Downgrade sentinels in the last 8 bytes of ServerHello.random (RFC 8446
|
|
28
|
+
* §4.1.3). A TLS 1.3 server MUST NOT set these; their presence means the
|
|
29
|
+
* negotiating peer is a TLS 1.2 (or earlier) implementation that intentionally
|
|
30
|
+
* inserted the sentinel to prevent downgrade attacks. If a client negotiating
|
|
31
|
+
* TLS 1.3 sees them, it MUST abort with {@link TlsHandshakeError} — the server
|
|
32
|
+
* is either buggy or malicious.
|
|
33
|
+
*
|
|
34
|
+
* - `DOWNGRD\x01` (`44 4F 57 4E 47 52 44 01`): TLS 1.2 downgrade sentinel
|
|
35
|
+
* - `DOWNGRD\x00` (`44 4F 57 4E 47 52 44 00`): TLS 1.1 or below downgrade sentinel
|
|
36
|
+
*/
|
|
37
|
+
const DOWNGRADE_SENTINEL_TLS12 = new Uint8Array([0x44, 0x4f, 0x57, 0x4e, 0x47, 0x52, 0x44, 0x01]);
|
|
38
|
+
const DOWNGRADE_SENTINEL_TLS11 = new Uint8Array([0x44, 0x4f, 0x57, 0x4e, 0x47, 0x52, 0x44, 0x00]);
|
|
39
|
+
/**
|
|
40
|
+
* Check the last 8 bytes of ServerHello.random for a downgrade sentinel
|
|
41
|
+
* (RFC 8446 §4.1.3). Throws {@link TlsHandshakeError} with phase "server_hello"
|
|
42
|
+
* if either sentinel is present — the client MUST abort with `illegal_parameter`.
|
|
43
|
+
*/
|
|
44
|
+
function checkDowngradeSentinel(random) {
|
|
45
|
+
const tail = random.subarray(24, 32);
|
|
46
|
+
if (constantTimeEqual(tail, DOWNGRADE_SENTINEL_TLS12) ||
|
|
47
|
+
constantTimeEqual(tail, DOWNGRADE_SENTINEL_TLS11)) {
|
|
48
|
+
throw new TlsHandshakeError("server_hello", {
|
|
49
|
+
cause: new Error("ServerHello.random contains a downgrade sentinel — the server is " +
|
|
50
|
+
"attempting to negotiate TLS 1.2 or below in a TLS 1.3 handshake " +
|
|
51
|
+
"(RFC 8446 §4.1.3 requires the client to abort with illegal_parameter)"),
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
25
55
|
/** Invert the cipher-suite wire values; throws on unknown values. */
|
|
26
56
|
function wireToCipherSuite(wire) {
|
|
27
57
|
switch (wire) {
|
|
@@ -97,6 +127,10 @@ export function parseServerHello(buf, offered) {
|
|
|
97
127
|
const protocolVersion = (readByte() << 8) | readByte();
|
|
98
128
|
const random = buf.subarray(o, o + 32);
|
|
99
129
|
o += 32;
|
|
130
|
+
// RFC 8446 §4.1.3: check the last 8 bytes of ServerHello.random for a
|
|
131
|
+
// downgrade sentinel. A TLS 1.3 server MUST NOT set these — their presence
|
|
132
|
+
// means the peer is actually TLS 1.2 or below, and the client MUST abort.
|
|
133
|
+
checkDowngradeSentinel(random);
|
|
100
134
|
const sessionIdLen = readByte();
|
|
101
135
|
expect(sessionIdLen);
|
|
102
136
|
const sessionId = buf.subarray(o, o + sessionIdLen);
|
|
@@ -1 +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;
|
|
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;AAC5F,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAehD;;;;;GAKG;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C;QACnB,MAAM,IAAI,iBAAiB,CAAC,cAAc,EAAE;YACxC,KAAK,EAAE,IAAI,KAAK,CAAC,2DAA2D,CAAC;SAChF,CAAC,CAAC;IACP,CAAC;IACD,IAAI,EAAE,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACvB,MAAM,IAAI,iBAAiB,CAAC,cAAc,EAAE;YACxC,KAAK,EAAE,IAAI,KAAK,CAAC,sDAAsD,EAAE,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;SAC3F,CAAC,CAAC;IACP,CAAC;IACD,MAAM,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACtB,MAAM,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACtB,IAAI,EAAE,KAAK,SAAS,IAAI,EAAE,KAAK,SAAS,EAAE,CAAC;QACvC,MAAM,IAAI,iBAAiB,CAAC,cAAc,EAAE;YACxC,KAAK,EAAE,IAAI,KAAK,CAAC,6CAA6C,CAAC;SAClE,CAAC,CAAC;IACP,CAAC;IACD,MAAM,IAAI,GAAG,CAAC,EAAE,IAAI,CAAC,CAAC,GAAG,EAAE,CAAC;IAC5B,MAAM,QAAQ,GAAG,aAAa,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IAC9C,sBAAsB,CAAC,QAAQ,CAAC,CAAC;IACjC,OAAO,QAAQ,CAAC;AACpB,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -21,6 +21,8 @@ export { ExtensionType, parseExtensions, findExtension, wireToNamedGroup } from
|
|
|
21
21
|
export type { TlsExtension } from "./extensions/extensions.js";
|
|
22
22
|
export { parseCertificate, validateHostname, verifyChain, pemToDer } from "./certificates/certificates.js";
|
|
23
23
|
export type { Certificate, CertificateChain, TrustAnchor } from "./certificates/certificates.js";
|
|
24
|
+
export { SessionCache } from "./session/session-cache.js";
|
|
25
|
+
export type { ResumptionTicket } from "./session/session-cache.js";
|
|
24
26
|
export { hkdfExpandLabel, deriveHandshakeSecrets, deriveHandshakeTrafficSecrets, deriveApplicationSecrets, } from "./crypto/keySchedule.js";
|
|
25
27
|
export { assertNever } from "./utils.js";
|
|
26
28
|
export { runHandshake, type HandshakeContext } from "./connection/index.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAC5E,YAAY,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAIhD,YAAY,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAE1D,OAAO,EACH,QAAQ,EACR,iBAAiB,EACjB,eAAe,EACf,aAAa,EACb,cAAc,GACjB,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAE9D,OAAO,EACH,KAAK,eAAe,EACpB,KAAK,yBAAyB,EAC9B,KAAK,WAAW,EAChB,KAAK,iBAAiB,EACtB,KAAK,WAAW,EAChB,KAAK,KAAK,EACV,KAAK,OAAO,EACZ,KAAK,UAAU,EACf,KAAK,eAAe,EACpB,KAAK,eAAe,EACpB,KAAK,UAAU,EACf,KAAK,YAAY,EACjB,KAAK,QAAQ,EACb,KAAK,cAAc,EACnB,OAAO,EACP,OAAO,EACP,WAAW,GACd,MAAM,YAAY,CAAC;AAIpB,YAAY,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAEhD,OAAO,EAAE,KAAK,UAAU,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAElI,OAAO,EACH,WAAW,EACX,iBAAiB,EACjB,qBAAqB,EACrB,iBAAiB,EACjB,kBAAkB,GACrB,MAAM,oBAAoB,CAAC;AAC5B,Y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EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,gCAAgC,CAAC;AAC3G,YAAY,EAAE,WAAW,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,gCAAgC,CAAC;AAEjG,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC1D,YAAY,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAEnE,OAAO,EACH,eAAe,EACf,sBAAsB,EACtB,6BAA6B,EAC7B,wBAAwB,GAC3B,MAAM,yBAAyB,CAAC;AAEjC,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAIzC,OAAO,EAAE,YAAY,EAAE,KAAK,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAG5E,OAAO,EACH,yBAAyB,EACzB,kBAAkB,EAClB,iBAAiB,EACjB,sBAAsB,EACtB,aAAa,GAChB,MAAM,iBAAiB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -13,6 +13,7 @@ export { ContentType, parseRecordHeader, serializeRecordHeader, cipherSuiteToAea
|
|
|
13
13
|
export { HandshakeType, buildClientHello, parseServerHello, cipherSuiteToWire, GREASE_VALUES, generateGreaseValue, isGreaseValue, } from "./handshake/handshake.js";
|
|
14
14
|
export { ExtensionType, parseExtensions, findExtension, wireToNamedGroup } from "./extensions/extensions.js";
|
|
15
15
|
export { parseCertificate, validateHostname, verifyChain, pemToDer } from "./certificates/certificates.js";
|
|
16
|
+
export { SessionCache } from "./session/session-cache.js";
|
|
16
17
|
export { hkdfExpandLabel, deriveHandshakeSecrets, deriveHandshakeTrafficSecrets, deriveApplicationSecrets, } from "./crypto/keySchedule.js";
|
|
17
18
|
export { assertNever } from "./utils.js";
|
|
18
19
|
// Re-exported for downstream consumers (e.g. @browsercore/quic) that run the
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAE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*
|
|
4
|
+
* Real browsers pad the ClientHello to a fixed length (512 bytes for Chrome) so
|
|
5
|
+
* that the fingerprint does not leak the exact number of extensions or cipher
|
|
6
|
+
* suites offered. The padding is applied via the PADDING extension (type 21),
|
|
7
|
+
* whose body is a run of zero bytes.
|
|
8
|
+
*
|
|
9
|
+
* This module owns the padding *computation*: given the current ClientHello
|
|
10
|
+
* message size and the target length, it returns how many zero bytes the
|
|
11
|
+
* PADDING extension body must contain. The actual byte emission happens in the
|
|
12
|
+
* ClientHello builder.
|
|
13
|
+
*/
|
|
14
|
+
import type { ClientHelloConfig } from "../types.js";
|
|
15
|
+
/**
|
|
16
|
+
* Compute the number of zero bytes needed in the PADDING extension body to
|
|
17
|
+
* reach the target ClientHello message length.
|
|
18
|
+
*
|
|
19
|
+
* The function accounts for whether the PADDING extension is already in the
|
|
20
|
+
* profile's `extensionOrder`. If it is, the probe message size already includes
|
|
21
|
+
* the 4-byte PADDING extension header (type + length) with a 0-byte body, so
|
|
22
|
+
* the body just needs `targetSize - probeSize` bytes. If it is not, the
|
|
23
|
+
* PADDING extension will be appended, adding a 4-byte header that must be
|
|
24
|
+
* subtracted from the available body budget.
|
|
25
|
+
*
|
|
26
|
+
* @param probeMessageSize The total ClientHello message size with a 0-byte
|
|
27
|
+
* PADDING extension body (or no PADDING extension if it's absent from
|
|
28
|
+
* the extension order).
|
|
29
|
+
* @param config The ClientHello configuration carrying
|
|
30
|
+
* `recordPadding` and `extensionOrder`.
|
|
31
|
+
* @returns Number of zero bytes for the PADING body; 0 when no padding is
|
|
32
|
+
* needed (target not set, already at/above target, or negative body).
|
|
33
|
+
*/
|
|
34
|
+
export declare function computePaddingExtensionBody(probeMessageSize: number, config: ClientHelloConfig): number;
|
|
35
|
+
//# sourceMappingURL=record-padding.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"record-padding.d.ts","sourceRoot":"","sources":["../../src/record/record-padding.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAGrD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,2BAA2B,CACvC,gBAAgB,EAAE,MAAM,EACxB,MAAM,EAAE,iBAAiB,GAC1B,MAAM,CAmBR"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ClientHello record padding (RFC 7685 / RFC 8446 §4.1.2).
|
|
3
|
+
*
|
|
4
|
+
* Real browsers pad the ClientHello to a fixed length (512 bytes for Chrome) so
|
|
5
|
+
* that the fingerprint does not leak the exact number of extensions or cipher
|
|
6
|
+
* suites offered. The padding is applied via the PADDING extension (type 21),
|
|
7
|
+
* whose body is a run of zero bytes.
|
|
8
|
+
*
|
|
9
|
+
* This module owns the padding *computation*: given the current ClientHello
|
|
10
|
+
* message size and the target length, it returns how many zero bytes the
|
|
11
|
+
* PADDING extension body must contain. The actual byte emission happens in the
|
|
12
|
+
* ClientHello builder.
|
|
13
|
+
*/
|
|
14
|
+
import { ExtensionType } from "../extensions/extensions.js";
|
|
15
|
+
/**
|
|
16
|
+
* Compute the number of zero bytes needed in the PADDING extension body to
|
|
17
|
+
* reach the target ClientHello message length.
|
|
18
|
+
*
|
|
19
|
+
* The function accounts for whether the PADDING extension is already in the
|
|
20
|
+
* profile's `extensionOrder`. If it is, the probe message size already includes
|
|
21
|
+
* the 4-byte PADDING extension header (type + length) with a 0-byte body, so
|
|
22
|
+
* the body just needs `targetSize - probeSize` bytes. If it is not, the
|
|
23
|
+
* PADDING extension will be appended, adding a 4-byte header that must be
|
|
24
|
+
* subtracted from the available body budget.
|
|
25
|
+
*
|
|
26
|
+
* @param probeMessageSize The total ClientHello message size with a 0-byte
|
|
27
|
+
* PADDING extension body (or no PADDING extension if it's absent from
|
|
28
|
+
* the extension order).
|
|
29
|
+
* @param config The ClientHello configuration carrying
|
|
30
|
+
* `recordPadding` and `extensionOrder`.
|
|
31
|
+
* @returns Number of zero bytes for the PADING body; 0 when no padding is
|
|
32
|
+
* needed (target not set, already at/above target, or negative body).
|
|
33
|
+
*/
|
|
34
|
+
export function computePaddingExtensionBody(probeMessageSize, config) {
|
|
35
|
+
if (config.recordPadding === undefined) {
|
|
36
|
+
return 0;
|
|
37
|
+
}
|
|
38
|
+
if (probeMessageSize >= config.recordPadding) {
|
|
39
|
+
return 0;
|
|
40
|
+
}
|
|
41
|
+
const paddingInOrder = config.extensionOrder.includes(ExtensionType.PADDING);
|
|
42
|
+
if (paddingInOrder) {
|
|
43
|
+
// The probe already counts the 4-byte extension header + 0-byte body.
|
|
44
|
+
// Every additional body byte increases the total by exactly one.
|
|
45
|
+
return config.recordPadding - probeMessageSize;
|
|
46
|
+
}
|
|
47
|
+
// The PADDING extension will be appended: its 4-byte header (type + length)
|
|
48
|
+
// is NOT in the probe, so reserve space for it.
|
|
49
|
+
const bodySize = config.recordPadding - probeMessageSize - 4;
|
|
50
|
+
return Math.max(bodySize, 0);
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=record-padding.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"record-padding.js","sourceRoot":"","sources":["../../src/record/record-padding.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAGH,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAE5D;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,2BAA2B,CACvC,gBAAwB,EACxB,MAAyB;IAEzB,IAAI,MAAM,CAAC,aAAa,KAAK,SAAS,EAAE,CAAC;QACrC,OAAO,CAAC,CAAC;IACb,CAAC;IACD,IAAI,gBAAgB,IAAI,MAAM,CAAC,aAAa,EAAE,CAAC;QAC3C,OAAO,CAAC,CAAC;IACb,CAAC;IAED,MAAM,cAAc,GAAG,MAAM,CAAC,cAAc,CAAC,QAAQ,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;IAC7E,IAAI,cAAc,EAAE,CAAC;QACjB,sEAAsE;QACtE,iEAAiE;QACjE,OAAO,MAAM,CAAC,aAAa,GAAG,gBAAgB,CAAC;IACnD,CAAC;IAED,4EAA4E;IAC5E,gDAAgD;IAChD,MAAM,QAAQ,GAAG,MAAM,CAAC,aAAa,GAAG,gBAAgB,GAAG,CAAC,CAAC;IAC7D,OAAO,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC;AACjC,CAAC"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TLS 1.3 session resumption cache (RFC 8446 §4.6.1, §7.5).
|
|
3
|
+
*
|
|
4
|
+
* When the server sends a NewSessionTicket post-handshake, the client derives a
|
|
5
|
+
* resumption PSK from the resumption_master_secret and stores it alongside the
|
|
6
|
+
* ticket for future 0-RTT / PSK-based session resumption. This module owns the
|
|
7
|
+
* storage and the ticket parsing/PSK derivation; the PSK offer in a subsequent
|
|
8
|
+
* ClientHello is a separate concern.
|
|
9
|
+
*
|
|
10
|
+
* The cache is a simple `Map` keyed by the SNI server name — sufficient for the
|
|
11
|
+
* single-connection resumption scope this package supports. A production-grade
|
|
12
|
+
* implementation would add TTL expiry and LRU eviction, but those are policy
|
|
13
|
+
* decisions that belong in the caller, not here.
|
|
14
|
+
*/
|
|
15
|
+
import type { CryptoProvider, HashId } from "@browsercore/crypto";
|
|
16
|
+
import type { CipherSuite } from "../types.js";
|
|
17
|
+
/** A parsed NewSessionTicket plus the derived resumption PSK. */
|
|
18
|
+
export interface ResumptionTicket {
|
|
19
|
+
/** The opaque ticket identity the server issued (to echo in a future ClientHello). */
|
|
20
|
+
readonly ticket: Uint8Array;
|
|
21
|
+
/** Obfuscated age addend from the ticket (RFC 8446 §4.6.1). */
|
|
22
|
+
readonly ticketAgeAdd: number;
|
|
23
|
+
/** Maximum early-data size in bytes, or 0 if the ticket disallows 0-RTT. */
|
|
24
|
+
readonly maxEarlyDataSize: number;
|
|
25
|
+
/** Derived resumption PSK (HKDF-Expand-Label of the resumption_master_secret). */
|
|
26
|
+
readonly psk: Uint8Array;
|
|
27
|
+
/** Cipher suite under which the original handshake negotiated the ticket. */
|
|
28
|
+
readonly cipherSuite: CipherSuite;
|
|
29
|
+
/** Epoch milliseconds when the ticket was received (for TTL enforcement). */
|
|
30
|
+
readonly receivedAt: number;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* A simple session resumption cache.
|
|
34
|
+
*
|
|
35
|
+
* Keyed by the server name (SNI) — one outstanding ticket per host. Thread-safe
|
|
36
|
+
* by virtue of JavaScript's single-threaded model; concurrent connections to the
|
|
37
|
+
* same host simply overwrite the stored ticket (the most recent one wins).
|
|
38
|
+
*/
|
|
39
|
+
export declare class SessionCache {
|
|
40
|
+
private readonly entries;
|
|
41
|
+
/** Store a resumption ticket for the given server name. */
|
|
42
|
+
store(serverName: string, ticket: ResumptionTicket): void;
|
|
43
|
+
/** Retrieve the most recent ticket for a server name, or undefined. */
|
|
44
|
+
get(serverName: string): ResumptionTicket | undefined;
|
|
45
|
+
/** Return true if a ticket exists for the given server name. */
|
|
46
|
+
has(serverName: string): boolean;
|
|
47
|
+
/** Remove the stored ticket for a server name (no-op if absent). */
|
|
48
|
+
delete(serverName: string): void;
|
|
49
|
+
/** Remove all stored tickets. */
|
|
50
|
+
clear(): void;
|
|
51
|
+
/** Number of cached tickets. */
|
|
52
|
+
get size(): number;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Result of parsing a NewSessionTicket: the key under which to store it plus
|
|
56
|
+
* the typed ticket. Returned by {@link parseNewSessionTicket} so the caller can
|
|
57
|
+
* decide whether to store it.
|
|
58
|
+
*/
|
|
59
|
+
export interface ParsedNewSessionTicket {
|
|
60
|
+
readonly serverName: string;
|
|
61
|
+
readonly ticket: ResumptionTicket;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Parse a NewSessionTicket (RFC 8446 §4.6.1) body and derive the resumption PSK.
|
|
65
|
+
*
|
|
66
|
+
* The resumption_master_secret is derived from the master secret and the full
|
|
67
|
+
* transcript (ClientHello..client Finished). The PSK is
|
|
68
|
+
* HKDF-Expand-Label(resumption_master_secret, "resumption", ticket_nonce,
|
|
69
|
+
* Hash.length).
|
|
70
|
+
*
|
|
71
|
+
* @param body The NewSessionTicket message body (after the 4-byte
|
|
72
|
+
* handshake header).
|
|
73
|
+
* @param masterSecret The master secret from the handshake key schedule.
|
|
74
|
+
* @param transcript The full handshake transcript (must include the client
|
|
75
|
+
* Finished message per RFC 8446 §7.5).
|
|
76
|
+
* @param hash The negotiated cipher's hash (SHA-256 or SHA-384).
|
|
77
|
+
* @param cipherSuite The negotiated cipher suite.
|
|
78
|
+
* @param serverName The SNI server name (cache key).
|
|
79
|
+
* @param receivedAt Epoch milliseconds when the ticket was received.
|
|
80
|
+
* @param provider The injected crypto provider.
|
|
81
|
+
* @returns The parsed ticket + cache key, or undefined if the body is malformed.
|
|
82
|
+
*/
|
|
83
|
+
export declare function parseNewSessionTicket(body: Uint8Array, masterSecret: Uint8Array, transcript: readonly Uint8Array[], hash: HashId, cipherSuite: CipherSuite, serverName: string, receivedAt: number, provider: CryptoProvider): ParsedNewSessionTicket | undefined;
|
|
84
|
+
//# sourceMappingURL=session-cache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session-cache.d.ts","sourceRoot":"","sources":["../../src/session/session-cache.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAClE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAK/C,iEAAiE;AACjE,MAAM,WAAW,gBAAgB;IAC7B,sFAAsF;IACtF,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAC;IAC5B,+DAA+D;IAC/D,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,4EAA4E;IAC5E,QAAQ,CA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6.1, §7.5).
|
|
3
|
+
*
|
|
4
|
+
* When the server sends a NewSessionTicket post-handshake, the client derives a
|
|
5
|
+
* resumption PSK from the resumption_master_secret and stores it alongside the
|
|
6
|
+
* ticket for future 0-RTT / PSK-based session resumption. This module owns the
|
|
7
|
+
* storage and the ticket parsing/PSK derivation; the PSK offer in a subsequent
|
|
8
|
+
* ClientHello is a separate concern.
|
|
9
|
+
*
|
|
10
|
+
* The cache is a simple `Map` keyed by the SNI server name — sufficient for the
|
|
11
|
+
* single-connection resumption scope this package supports. A production-grade
|
|
12
|
+
* implementation would add TTL expiry and LRU eviction, but those are policy
|
|
13
|
+
* decisions that belong in the caller, not here.
|
|
14
|
+
*/
|
|
15
|
+
import { TlsHandshakeError } from "../errors.js";
|
|
16
|
+
import { hkdfExpandLabel, hashLengthFor } from "../crypto/keySchedule.js";
|
|
17
|
+
import { transcriptHash } from "../connection/key-exchange.js";
|
|
18
|
+
/**
|
|
19
|
+
* A simple session resumption cache.
|
|
20
|
+
*
|
|
21
|
+
* Keyed by the server name (SNI) — one outstanding ticket per host. Thread-safe
|
|
22
|
+
* by virtue of JavaScript's single-threaded model; concurrent connections to the
|
|
23
|
+
* same host simply overwrite the stored ticket (the most recent one wins).
|
|
24
|
+
*/
|
|
25
|
+
export class SessionCache {
|
|
26
|
+
entries = new Map();
|
|
27
|
+
/** Store a resumption ticket for the given server name. */
|
|
28
|
+
store(serverName, ticket) {
|
|
29
|
+
this.entries.set(serverName, ticket);
|
|
30
|
+
}
|
|
31
|
+
/** Retrieve the most recent ticket for a server name, or undefined. */
|
|
32
|
+
get(serverName) {
|
|
33
|
+
return this.entries.get(serverName);
|
|
34
|
+
}
|
|
35
|
+
/** Return true if a ticket exists for the given server name. */
|
|
36
|
+
has(serverName) {
|
|
37
|
+
return this.entries.has(serverName);
|
|
38
|
+
}
|
|
39
|
+
/** Remove the stored ticket for a server name (no-op if absent). */
|
|
40
|
+
delete(serverName) {
|
|
41
|
+
this.entries.delete(serverName);
|
|
42
|
+
}
|
|
43
|
+
/** Remove all stored tickets. */
|
|
44
|
+
clear() {
|
|
45
|
+
this.entries.clear();
|
|
46
|
+
}
|
|
47
|
+
/** Number of cached tickets. */
|
|
48
|
+
get size() {
|
|
49
|
+
return this.entries.size;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/** early_data extension type (RFC 8446 §4.2.10), used in NewSessionTicket. */
|
|
53
|
+
const EARLY_DATA_EXTENSION_TYPE = 42;
|
|
54
|
+
/**
|
|
55
|
+
* Parse a NewSessionTicket (RFC 8446 §4.6.1) body and derive the resumption PSK.
|
|
56
|
+
*
|
|
57
|
+
* The resumption_master_secret is derived from the master secret and the full
|
|
58
|
+
* transcript (ClientHello..client Finished). The PSK is
|
|
59
|
+
* HKDF-Expand-Label(resumption_master_secret, "resumption", ticket_nonce,
|
|
60
|
+
* Hash.length).
|
|
61
|
+
*
|
|
62
|
+
* @param body The NewSessionTicket message body (after the 4-byte
|
|
63
|
+
* handshake header).
|
|
64
|
+
* @param masterSecret The master secret from the handshake key schedule.
|
|
65
|
+
* @param transcript The full handshake transcript (must include the client
|
|
66
|
+
* Finished message per RFC 8446 §7.5).
|
|
67
|
+
* @param hash The negotiated cipher's hash (SHA-256 or SHA-384).
|
|
68
|
+
* @param cipherSuite The negotiated cipher suite.
|
|
69
|
+
* @param serverName The SNI server name (cache key).
|
|
70
|
+
* @param receivedAt Epoch milliseconds when the ticket was received.
|
|
71
|
+
* @param provider The injected crypto provider.
|
|
72
|
+
* @returns The parsed ticket + cache key, or undefined if the body is malformed.
|
|
73
|
+
*/
|
|
74
|
+
export function parseNewSessionTicket(body, masterSecret, transcript, hash, cipherSuite, serverName, receivedAt, provider) {
|
|
75
|
+
if (body.length < 8) {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
// ticket_lifetime(4) — not needed for PSK derivation; skip 4 bytes.
|
|
79
|
+
// ticket_age_add(4)
|
|
80
|
+
const ticketAgeAdd = readUint32(body, 4);
|
|
81
|
+
let o = 8;
|
|
82
|
+
// ticket_nonce<0..255>: 1-byte length + nonce bytes
|
|
83
|
+
const nonceResult = readOpaque8(body, o);
|
|
84
|
+
if (nonceResult === undefined) {
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
const ticketNonce = nonceResult.data;
|
|
88
|
+
o = nonceResult.nextOffset;
|
|
89
|
+
// ticket<1..2^16-1>: 2-byte length + ticket bytes
|
|
90
|
+
const ticketResult = readOpaque16(body, o);
|
|
91
|
+
if (ticketResult === undefined) {
|
|
92
|
+
return undefined;
|
|
93
|
+
}
|
|
94
|
+
const ticket = ticketResult.data;
|
|
95
|
+
o = ticketResult.nextOffset;
|
|
96
|
+
// extensions<8..2^16-2>: scan for early_data (type 42) to get max_early_data_size.
|
|
97
|
+
let maxEarlyDataSize = 0;
|
|
98
|
+
const extHeader = readUint16(body, o);
|
|
99
|
+
if (extHeader !== undefined) {
|
|
100
|
+
const extLen = extHeader.value;
|
|
101
|
+
o = extHeader.nextOffset;
|
|
102
|
+
const extEnd = Math.min(o + extLen, body.length);
|
|
103
|
+
let scanning = true;
|
|
104
|
+
while (scanning && o + 4 <= extEnd) {
|
|
105
|
+
const eType = readUint16(body, o);
|
|
106
|
+
const eLen = readUint16(body, o + 2);
|
|
107
|
+
if (eType === undefined || eLen === undefined) {
|
|
108
|
+
scanning = false;
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
o += 4;
|
|
112
|
+
if (o + eLen.value > extEnd) {
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
if (eType.value === EARLY_DATA_EXTENSION_TYPE && eLen.value >= 4) {
|
|
116
|
+
maxEarlyDataSize = readUint32(body, o);
|
|
117
|
+
}
|
|
118
|
+
o += eLen.value;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
// Derive resumption_master_secret (RFC 8446 §7.5).
|
|
122
|
+
const resumptionTranscript = transcriptHash(transcript, hash, provider);
|
|
123
|
+
const hashLen = hashLengthFor(hash);
|
|
124
|
+
const resumptionMasterSecret = hkdfExpandLabel(masterSecret, "res master", resumptionTranscript, hashLen, hash, provider);
|
|
125
|
+
// Derive PSK (RFC 8446 §7.5).
|
|
126
|
+
const psk = hkdfExpandLabel(resumptionMasterSecret, "resumption", ticketNonce, hashLen, hash, provider);
|
|
127
|
+
return {
|
|
128
|
+
serverName,
|
|
129
|
+
ticket: {
|
|
130
|
+
ticket: new Uint8Array(ticket),
|
|
131
|
+
ticketAgeAdd,
|
|
132
|
+
maxEarlyDataSize,
|
|
133
|
+
psk,
|
|
134
|
+
cipherSuite,
|
|
135
|
+
receivedAt,
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
/** Read a big-endian uint32 at the given offset. Caller guarantees bounds. */
|
|
140
|
+
function readUint32(buf, offset) {
|
|
141
|
+
const b0 = buf[offset];
|
|
142
|
+
const b1 = buf[offset + 1];
|
|
143
|
+
const b2 = buf[offset + 2];
|
|
144
|
+
const b3 = buf[offset + 3];
|
|
145
|
+
if (b0 === undefined || b1 === undefined || b2 === undefined || b3 === undefined) {
|
|
146
|
+
throw new TlsHandshakeError("application", {
|
|
147
|
+
cause: new Error("readUint32: buffer truncated"),
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
// `>>> 0` converts the signed 32-bit bitwise result to unsigned uint32.
|
|
151
|
+
return ((b0 << 24) | (b1 << 16) | (b2 << 8) | b3) >>> 0;
|
|
152
|
+
}
|
|
153
|
+
/** Read a big-endian uint16 at the given offset; returns undefined if truncated. */
|
|
154
|
+
function readUint16(buf, offset) {
|
|
155
|
+
const hi = buf[offset];
|
|
156
|
+
const lo = buf[offset + 1];
|
|
157
|
+
if (hi === undefined || lo === undefined) {
|
|
158
|
+
return undefined;
|
|
159
|
+
}
|
|
160
|
+
return { value: (hi << 8) | lo, nextOffset: offset + 2 };
|
|
161
|
+
}
|
|
162
|
+
/** Read a 1-byte-length-prefixed opaque blob; returns the data and next offset. */
|
|
163
|
+
function readOpaque8(buf, offset) {
|
|
164
|
+
const lenByte = buf[offset];
|
|
165
|
+
if (lenByte === undefined) {
|
|
166
|
+
return undefined;
|
|
167
|
+
}
|
|
168
|
+
const len = lenByte;
|
|
169
|
+
const start = offset + 1;
|
|
170
|
+
if (start + len > buf.length) {
|
|
171
|
+
return undefined;
|
|
172
|
+
}
|
|
173
|
+
return { data: buf.subarray(start, start + len), nextOffset: start + len };
|
|
174
|
+
}
|
|
175
|
+
/** Read a 2-byte-length-prefixed opaque blob; returns the data and next offset. */
|
|
176
|
+
function readOpaque16(buf, offset) {
|
|
177
|
+
const hi = buf[offset];
|
|
178
|
+
const lo = buf[offset + 1];
|
|
179
|
+
if (hi === undefined || lo === undefined) {
|
|
180
|
+
return undefined;
|
|
181
|
+
}
|
|
182
|
+
const len = (hi << 8) | lo;
|
|
183
|
+
const start = offset + 2;
|
|
184
|
+
if (start + len > buf.length) {
|
|
185
|
+
return undefined;
|
|
186
|
+
}
|
|
187
|
+
return { data: buf.subarray(start, start + len), nextOffset: start + len };
|
|
188
|
+
}
|
|
189
|
+
//# sourceMappingURL=session-cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session-cache.js","sourceRoot":"","sources":["../../src/session/session-cache.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAIH,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAC1E,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAkB/D;;;;;;GAMG;AACH,MAAM,OAAO,YAAY;IACJ,OAAO,GAAG,IAAI,GAAG,EAA4B,CAAC;IAE/D,2DAA2D;IAC3D,KAAK,CAAC,UAAkB,EAAE,MAAwB;QAC9C,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;IACzC,CAAC;IAED,uEAAuE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AG,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IAC3B,IAAI,EAAE,KAAK,SAAS,IAAI,EAAE,KAAK,SAAS,EAAE,CAAC;QACvC,OAAO,SAAS,CAAC;IACrB,CAAC;IACD,MAAM,GAAG,GAAG,CAAC,EAAE,IAAI,CAAC,CAAC,GAAG,EAAE,CAAC;IAC3B,MAAM,KAAK,GAAG,MAAM,GAAG,CAAC,CAAC;IACzB,IAAI,KAAK,GAAG,GAAG,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC;QAC3B,OAAO,SAAS,CAAC;IACrB,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,GAAG,CAAC,QAAQ,CAAC,KAAK,EAAE,KAAK,GAAG,GAAG,CAAC,EAAE,UAAU,EAAE,KAAK,GAAG,GAAG,EAAE,CAAC;AAC/E,CAAC"}
|
package/dist/tls.d.ts
CHANGED
|
@@ -22,11 +22,11 @@
|
|
|
22
22
|
import type { CryptoProvider, HashId } from "@browsercore/crypto";
|
|
23
23
|
import type { Transport } from "@browsercore/transport";
|
|
24
24
|
import { type ApplicationData, type CipherSuite, type CloseReason, type KeyPair, type ProtocolVersion, type TlsConnection, type TlsOptions, type TlsSessionId, type TlsState, type TrafficSecrets } from "./types.js";
|
|
25
|
-
import { type TlsError } from "./errors.js";
|
|
25
|
+
import { type AlertLevel, type HandshakePhase, type TlsError } from "./errors.js";
|
|
26
26
|
import { type encryptRecord } from "./record/record.js";
|
|
27
|
-
import type
|
|
28
|
-
import type
|
|
29
|
-
import { type
|
|
27
|
+
import { type ServerHello } from "./handshake/handshake.js";
|
|
28
|
+
import { type Certificate, type HandshakeContext } from "./connection/index.js";
|
|
29
|
+
import { type ResumptionTicket } from "./session/session-cache.js";
|
|
30
30
|
/**
|
|
31
31
|
* Establish a TLS 1.3 connection over the given transport.
|
|
32
32
|
*
|
|
@@ -75,6 +75,24 @@ export declare class TlsConnectionImpl implements TlsConnection, HandshakeContex
|
|
|
75
75
|
masterSecret: Uint8Array;
|
|
76
76
|
clientHsSeq: number;
|
|
77
77
|
serverHsSeq: number;
|
|
78
|
+
/**
|
|
79
|
+
* Raw application traffic secrets, retained for KeyUpdate key rotation
|
|
80
|
+
* (RFC 8446 §4.6.3). Set by the driver when application secrets are derived.
|
|
81
|
+
*/
|
|
82
|
+
clientAppSecret: Uint8Array;
|
|
83
|
+
serverAppSecret: Uint8Array;
|
|
84
|
+
/** True once handshake traffic keys are derived — gates sendAlert during handshake. */
|
|
85
|
+
hsTrafficReady: boolean;
|
|
86
|
+
/** Mutable: current handshake phase, updated by the driver for timeout attribution. */
|
|
87
|
+
currentPhase: HandshakePhase;
|
|
88
|
+
/** Optional debug trace callback, set from TlsOptions.onDebug. */
|
|
89
|
+
onDebug: ((msg: string) => void) | undefined;
|
|
90
|
+
/**
|
|
91
|
+
* Session resumption cache. Stores parsed NewSessionTicket messages and
|
|
92
|
+
* their derived PSKs so a future connection to the same host can offer
|
|
93
|
+
* PSK-based resumption. Lazily populated post-handshake.
|
|
94
|
+
*/
|
|
95
|
+
private readonly sessionCache;
|
|
78
96
|
private applicationSecrets;
|
|
79
97
|
private clientAppSeq;
|
|
80
98
|
private serverAppSeq;
|
|
@@ -115,10 +133,66 @@ export declare class TlsConnectionImpl implements TlsConnection, HandshakeContex
|
|
|
115
133
|
* HandshakeContext fields and returns the derived application secrets, which
|
|
116
134
|
* we store for read()/write(). `now` is read here (in the orchestrator) so
|
|
117
135
|
* certificate validation stays pure and testable.
|
|
136
|
+
*
|
|
137
|
+
* Error cleanup (RFC 8446 §6): on any failure the method sends a fatal
|
|
138
|
+
* alert (best-effort), closes the transport to prevent socket leaks, and
|
|
139
|
+
* transitions to the `closed` terminal state before re-throwing.
|
|
118
140
|
*/
|
|
119
141
|
private performHandshake;
|
|
142
|
+
/**
|
|
143
|
+
* Send a TLS alert (RFC 8446 §6) to the peer.
|
|
144
|
+
*
|
|
145
|
+
* Best-effort: if the traffic keys are not yet derived (early handshake
|
|
146
|
+
* failure), or the transport write fails, the alert is silently dropped.
|
|
147
|
+
* The caller's error-handling path (transport close) is the fallback.
|
|
148
|
+
*
|
|
149
|
+
* During the handshake, the alert is sent under the client handshake traffic
|
|
150
|
+
* key; after the handshake, under the client application traffic key.
|
|
151
|
+
*/
|
|
152
|
+
sendAlert(level: AlertLevel, description: number): Promise<void>;
|
|
153
|
+
/**
|
|
154
|
+
* Map a typed TLS error to the appropriate alert description for sending
|
|
155
|
+
* before connection teardown (RFC 8446 §6).
|
|
156
|
+
*
|
|
157
|
+
* - `handshake_failure` (40): bad cipher suite / version negotiation
|
|
158
|
+
* - `bad_certificate` (42): certificate chain or hostname validation failure
|
|
159
|
+
* - `decrypt_error` (51): signature verification or Finished verify_data mismatch
|
|
160
|
+
* - `illegal_parameter` (47): downgrade sentinel, unexpected message
|
|
161
|
+
* - `internal_error` (80): anything not covered above
|
|
162
|
+
*/
|
|
163
|
+
private errorToAlertDescription;
|
|
120
164
|
/** Handle a non-application record encountered while reading application data. */
|
|
121
165
|
private handlePostHandshakeRecord;
|
|
166
|
+
/**
|
|
167
|
+
* Split a decrypted post-handshake handshake record into individual messages
|
|
168
|
+
* and dispatch each by type. KeyUpdate rotates traffic keys; NewSessionTicket
|
|
169
|
+
* is parsed and stored in the session cache for future resumption.
|
|
170
|
+
*/
|
|
171
|
+
private handlePostHandshakeMessage;
|
|
172
|
+
/**
|
|
173
|
+
* Parse a NewSessionTicket (RFC 8446 §4.6.1) and store the derived PSK.
|
|
174
|
+
*
|
|
175
|
+
* Delegates parsing and PSK derivation to {@link parseNewSessionTicket};
|
|
176
|
+
* the result is stored in the session cache keyed by the server name.
|
|
177
|
+
* Storage only — the PSK is not yet offered in a subsequent ClientHello.
|
|
178
|
+
*/
|
|
179
|
+
private handleNewSessionTicket;
|
|
180
|
+
/**
|
|
181
|
+
* Handle a received KeyUpdate (RFC 8446 §4.6.3).
|
|
182
|
+
*
|
|
183
|
+
* Upon receiving KeyUpdate, the receiver MUST immediately update its
|
|
184
|
+
* *receiving* keys (the keys for reading records from the sender) and reset
|
|
185
|
+
* the corresponding sequence counter to zero. If `request_update` is
|
|
186
|
+
* `update_requested` (1), the receiver MUST send its own KeyUpdate (with
|
|
187
|
+
* `update_not_requested`) and then update its *sending* keys.
|
|
188
|
+
*/
|
|
189
|
+
private handleKeyUpdate;
|
|
122
190
|
private transition;
|
|
191
|
+
/**
|
|
192
|
+
* Retrieve a cached resumption ticket for the given server name, or
|
|
193
|
+
* undefined if no NewSessionTicket has been received for this connection.
|
|
194
|
+
* Used by higher layers to offer PSK-based session resumption.
|
|
195
|
+
*/
|
|
196
|
+
getResumptionTicket(serverName: string): ResumptionTicket | undefined;
|
|
123
197
|
}
|
|
124
198
|
//# sourceMappingURL=tls.d.ts.map
|
package/dist/tls.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tls.d.ts","sourceRoot":"","sources":["../src/tls.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAGH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAClE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACxD,OAAO,EAGH,KAAK,eAAe,EAEpB,KAAK,WAAW,EAEhB,KAAK,WAAW,EAEhB,KAAK,OAAO,EACZ,KAAK,eAAe,EACpB,KAAK,aAAa,EAClB,KAAK,UAAU,EACf,KAAK,YAAY,EACjB,KAAK,QAAQ,EACb,KAAK,cAAc,EACtB,MAAM,YAAY,CAAC;AACpB,OAAO,
|
|
1
|
+
{"version":3,"file":"tls.d.ts","sourceRoot":"","sources":["../src/tls.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAGH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAClE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACxD,OAAO,EAGH,KAA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