@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
package/dist/tls.d.ts
CHANGED
|
@@ -9,35 +9,45 @@
|
|
|
9
9
|
* TLS 1.2 fallback is intentionally NOT implemented: this client speaks TLS 1.3
|
|
10
10
|
* only. Requesting a TLS 1.2 (only) handshake is rejected up front with a typed
|
|
11
11
|
* error rather than failing silently mid-flight.
|
|
12
|
+
*
|
|
13
|
+
* The connection class is intentionally a thin coordinator: it owns the mutable
|
|
14
|
+
* connection state (read buffer, sequence counters, traffic secrets, transcript)
|
|
15
|
+
* and the public API, while every pure computation — record framing, key exchange,
|
|
16
|
+
* transcript hashing, alert translation, and the handshake choreography itself —
|
|
17
|
+
* lives in a focused module under `./connection/`. Those modules thread state
|
|
18
|
+
* through explicit parameters and the {@link HandshakeContext} this class
|
|
19
|
+
* implements, which keeps the byte-level logic unit-testable in isolation and
|
|
20
|
+
* this file focused on the public surface.
|
|
12
21
|
*/
|
|
13
|
-
import
|
|
14
|
-
import type {
|
|
22
|
+
import { type HashId } from "@browsercore/crypto";
|
|
23
|
+
import type { Transport } from "@browsercore/transport";
|
|
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";
|
|
26
|
+
import { type encryptRecord } from "./record/record.js";
|
|
15
27
|
import type { Certificate } from "./certificates/certificates.js";
|
|
28
|
+
import type { ServerHello } from "./handshake/handshake.js";
|
|
29
|
+
import { type HandshakeContext } from "./connection/index.js";
|
|
16
30
|
/**
|
|
17
31
|
* Establish a TLS 1.3 connection over the given transport.
|
|
18
32
|
*
|
|
19
33
|
* Performs the full TLS 1.3 handshake, derives traffic secrets, and returns a
|
|
20
34
|
* {@link TlsConnection} that transparently encrypts/decrypts application data.
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* const transport = await connect({ host: "example.com", port: 443 });
|
|
25
|
-
* const tls = await connectTls({
|
|
26
|
-
* transport,
|
|
27
|
-
* serverName: "example.com",
|
|
28
|
-
* profile: resolveProfile("modern-tls13", "example.com"),
|
|
29
|
-
* alpnProtocols: ["h2", "http/1.1"],
|
|
30
|
-
* });
|
|
31
|
-
* await tls.write(new TextEncoder().encode("GET / HTTP/1.1\r\n"));
|
|
32
|
-
* const chunk = await tls.read();
|
|
33
|
-
* await tls.close();
|
|
34
|
-
* ```
|
|
35
|
+
* Callers pass an already-connected {@link Transport}, the server name (for SNI
|
|
36
|
+
* + certificate validation), and a resolved profile (cipher suites, key-share
|
|
37
|
+
* groups, signature algorithms).
|
|
35
38
|
*/
|
|
36
39
|
export declare function connectTls(options: TlsOptions): Promise<TlsConnection>;
|
|
37
40
|
/** Generate key shares for the requested groups (delegates to @browsercore/crypto). */
|
|
38
41
|
export declare function generateKeyShares(groups: readonly string[]): Promise<KeyPair[]>;
|
|
39
|
-
/**
|
|
40
|
-
|
|
42
|
+
/**
|
|
43
|
+
* Concrete TLS 1.3 connection implementation.
|
|
44
|
+
*
|
|
45
|
+
* Implements {@link HandshakeContext} so the handshake driver in
|
|
46
|
+
* `./connection/handshake-driver.ts` can read and mutate its fields directly
|
|
47
|
+
* (read buffer, transcript, traffic secrets, sequence counters) without the
|
|
48
|
+
* choreography of the handshake living inline in this class.
|
|
49
|
+
*/
|
|
50
|
+
export declare class TlsConnectionImpl implements TlsConnection, HandshakeContext {
|
|
41
51
|
readonly id: TlsSessionId;
|
|
42
52
|
state: TlsState;
|
|
43
53
|
cipherSuite: CipherSuite;
|
|
@@ -45,103 +55,54 @@ export declare class TlsConnectionImpl implements TlsConnection {
|
|
|
45
55
|
alpnProtocol?: string;
|
|
46
56
|
/** The peer's leaf certificate, once the handshake has validated it. */
|
|
47
57
|
peerCertificate?: Certificate;
|
|
48
|
-
|
|
49
|
-
private readonly
|
|
50
|
-
private readonly
|
|
51
|
-
private readonly
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
private
|
|
65
|
-
private
|
|
66
|
-
private
|
|
67
|
-
private _clientAppSeq;
|
|
68
|
-
private _serverAppSeq;
|
|
58
|
+
readonly transport: Transport;
|
|
59
|
+
private readonly profile;
|
|
60
|
+
private readonly serverName;
|
|
61
|
+
private readonly trustAnchors;
|
|
62
|
+
readBuffer: Uint8Array;
|
|
63
|
+
readonly transcript: Uint8Array[];
|
|
64
|
+
aead: Parameters<typeof encryptRecord>[4];
|
|
65
|
+
hash: HashId;
|
|
66
|
+
serverHello: ServerHello;
|
|
67
|
+
clientHsTraffic: TrafficSecrets;
|
|
68
|
+
serverHsTraffic: TrafficSecrets;
|
|
69
|
+
clientHsTrafficSecret: Uint8Array;
|
|
70
|
+
serverHsTrafficSecret: Uint8Array;
|
|
71
|
+
masterSecret: Uint8Array;
|
|
72
|
+
clientHsSeq: number;
|
|
73
|
+
serverHsSeq: number;
|
|
74
|
+
private applicationSecrets;
|
|
75
|
+
private clientAppSeq;
|
|
76
|
+
private serverAppSeq;
|
|
69
77
|
/** Decrypted application payloads awaiting consumption by read(). */
|
|
70
|
-
private
|
|
78
|
+
private appReadQueue;
|
|
71
79
|
/** Lifecycle observers. */
|
|
72
|
-
private
|
|
73
|
-
private
|
|
80
|
+
private closeListeners;
|
|
81
|
+
private errorListeners;
|
|
74
82
|
constructor(options?: TlsOptions);
|
|
75
83
|
/**
|
|
76
84
|
* Drive the TLS 1.3 handshake to completion. Idempotent: calling it more than
|
|
77
85
|
* once on an open connection is a no-op.
|
|
78
86
|
*/
|
|
79
87
|
handshake(timeoutMs?: number): Promise<void>;
|
|
88
|
+
/**
|
|
89
|
+
* Read the next chunk of decrypted application data. Blocks until a record
|
|
90
|
+
* arrives; non-application records (alerts, post-handshake messages) are
|
|
91
|
+
* handled inline.
|
|
92
|
+
*/
|
|
80
93
|
read(): Promise<ApplicationData>;
|
|
81
94
|
write(data: Uint8Array): Promise<void>;
|
|
82
95
|
close(): Promise<void>;
|
|
83
96
|
on(event: "close" | "error", listener: (arg: CloseReason | TlsError) => void): this;
|
|
84
|
-
private _performHandshake;
|
|
85
|
-
/**
|
|
86
|
-
* Read the server's encrypted second flight message-by-message, advancing the
|
|
87
|
-
* handshake state machine and updating the transcript. Server Finished is
|
|
88
|
-
* verified against the transcript as it stood *before* the Finished message.
|
|
89
|
-
*/
|
|
90
|
-
private _consumeServerFlight;
|
|
91
|
-
/** Write an unencrypted record (used for the initial ClientHello/ServerHello). */
|
|
92
|
-
private _writeRecord;
|
|
93
|
-
/**
|
|
94
|
-
* Read the 5-byte record header and return it raw (needed as AEAD AAD) plus
|
|
95
|
-
* the parsed length. The caller is responsible for consuming the fragment.
|
|
96
|
-
*/
|
|
97
|
-
private _readHeaderBytes;
|
|
98
|
-
/** Read a complete record given its already-consumed header. */
|
|
99
|
-
private _readRawRecord;
|
|
100
|
-
/**
|
|
101
|
-
* Read and decrypt one record, returning its inner content type and content.
|
|
102
|
-
* TLS 1.3 wraps encrypted handshake messages in records whose outer type is
|
|
103
|
-
* application_data; the real type is the last non-zero byte of the plaintext.
|
|
104
|
-
*/
|
|
105
|
-
private _readEncryptedRecord;
|
|
106
|
-
/** Pull bytes from the transport until at least `n` are buffered. */
|
|
107
|
-
private _ensureBytes;
|
|
108
|
-
/** Compute the (EC)DHE shared secret from the server's selected key share. */
|
|
109
|
-
private _computeSharedSecret;
|
|
110
|
-
/** Hash the current handshake transcript with the negotiated cipher's hash. */
|
|
111
|
-
private _transcriptHash;
|
|
112
|
-
/** Read one encrypted handshake message, decrypting and stripping the inner type. */
|
|
113
|
-
private _readEncryptedHandshakeMessage;
|
|
114
|
-
/** Extract the negotiated ALPN protocol from EncryptedExtensions, if any. */
|
|
115
|
-
private _handleEncryptedExtensions;
|
|
116
|
-
/** Parse the Certificate message, validate hostname, and verify the chain. */
|
|
117
|
-
private _handleCertificate;
|
|
118
|
-
/**
|
|
119
|
-
* Parse a Certificate handshake message (RFC 8446 §4.4.2) into a chain. The
|
|
120
|
-
* body is: certificate_request_context (length-prefixed, len 1) then a
|
|
121
|
-
* length-prefixed list of CertificateEntry { cert_data, extensions }.
|
|
122
|
-
*/
|
|
123
|
-
private _parseCertificateMessage;
|
|
124
|
-
/**
|
|
125
|
-
* Verify the server's Finished message: HMAC(finished_key, transcript), where
|
|
126
|
-
* finished_key = HKDF-Expand-Label(server_traffic_secret, "finished", "", Hash.length)
|
|
127
|
-
* and the transcript is ClientHello..CertificateVerify (everything before Finished).
|
|
128
|
-
*/
|
|
129
|
-
private _verifyServerFinished;
|
|
130
|
-
/** Build and send the client Finished under the client handshake traffic key. */
|
|
131
|
-
private _sendClientFinished;
|
|
132
97
|
/**
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
98
|
+
* Run the handshake via the driver module. The driver mutates this object's
|
|
99
|
+
* HandshakeContext fields and returns the derived application secrets, which
|
|
100
|
+
* we store for read()/write(). `now` is read here (in the orchestrator) so
|
|
101
|
+
* certificate validation stays pure and testable.
|
|
136
102
|
*/
|
|
137
|
-
private
|
|
103
|
+
private performHandshake;
|
|
138
104
|
/** Handle a non-application record encountered while reading application data. */
|
|
139
|
-
private
|
|
140
|
-
|
|
141
|
-
private _handleAlert;
|
|
142
|
-
private _withTimeout;
|
|
143
|
-
private _ensureOpen;
|
|
144
|
-
private _transition;
|
|
145
|
-
private _emitError;
|
|
105
|
+
private handlePostHandshakeRecord;
|
|
106
|
+
private transition;
|
|
146
107
|
}
|
|
147
108
|
//# 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
|
|
1
|
+
{"version":3,"file":"tls.d.ts","sourceRoot":"","sources":["../src/tls.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAU,KAAK,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC1D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACxD,OAAO,EAEH,KAAK,eAAe,EAEpB,KAAK,WAAW,EAEhB,KAAK,WAAW,EAChB,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,EAAqC,KAAK,QAAQ,EAAE,MAAM,aAAa,CAAC;AAE/E,OAAO,EAAe,KAAK,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACrE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gCAAgC,CAAC;AAClE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,EAUH,KAAK,gBAAgB,EACxB,MAAM,uBAAuB,CAAC;AAK/B;;;;;;;;GAQG;AACH,wBAAsB,UAAU,CAAC,OAAO,EAAE,UAAU,GAAG,OAAO,CAAC,aAAa,CAAC,CAI5E;AAED,uFAAuF;AACvF,wBAAgB,iBAAi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