@browsercore/tls 0.1.0 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/tls.d.ts CHANGED
@@ -1,19 +1,23 @@
1
1
  /**
2
2
  * @browsercore/tls — public entry point.
3
3
  *
4
- * Establishes a TLS 1.3 (with 1.2 fallback) connection over an existing
5
- * byte-stream transport. Wires together the record layer, handshake state
6
- * machine, key schedule, and certificate validation — consuming @browsercore/transport
7
- * and @browsercore/crypto, never node:crypto directly.
4
+ * Establishes a TLS 1.3 connection over an existing byte-stream transport.
5
+ * Wires together the record layer, handshake state machine, key schedule, and
6
+ * certificate validation — consuming @browsercore/transport and @browsercore/crypto,
7
+ * never node:crypto directly.
8
+ *
9
+ * TLS 1.2 fallback is intentionally NOT implemented: this client speaks TLS 1.3
10
+ * only. Requesting a TLS 1.2 (only) handshake is rejected up front with a typed
11
+ * error rather than failing silently mid-flight.
8
12
  */
9
- import type { CloseReason, KeyPair, TlsConnection, TlsOptions, TlsSessionId, TlsState } from "./types.js";
13
+ import type { ApplicationData, CipherSuite, CloseReason, KeyPair, ProtocolVersion, TlsConnection, TlsOptions, TlsSessionId, TlsState } from "./types.js";
10
14
  import type { TlsError } from "./errors.js";
15
+ import type { Certificate } from "./certificates/certificates.js";
11
16
  /**
12
- * Establish a TLS connection over the given transport.
17
+ * Establish a TLS 1.3 connection over the given transport.
13
18
  *
14
- * Performs the full TLS 1.3 handshake (falling back to 1.2 if the server does
15
- * not support 1.3), derives traffic secrets, and returns a {@link TlsConnection}
16
- * that transparently encrypts/decrypts application data.
19
+ * Performs the full TLS 1.3 handshake, derives traffic secrets, and returns a
20
+ * {@link TlsConnection} that transparently encrypts/decrypts application data.
17
21
  *
18
22
  * @example
19
23
  * ```ts
@@ -30,27 +34,114 @@ import type { TlsError } from "./errors.js";
30
34
  * ```
31
35
  */
32
36
  export declare function connectTls(options: TlsOptions): Promise<TlsConnection>;
33
- /** Concrete TLS connection implementation (filled in per PLAN.md). */
37
+ /** Generate key shares for the requested groups (delegates to @browsercore/crypto). */
38
+ export declare function generateKeyShares(groups: readonly string[]): Promise<KeyPair[]>;
39
+ /** Concrete TLS 1.3 connection implementation. */
34
40
  export declare class TlsConnectionImpl implements TlsConnection {
35
41
  readonly id: TlsSessionId;
36
42
  state: TlsState;
37
- readonly protocolVersion: {
38
- readonly name: "TLS 1.3";
39
- readonly wire: 772;
40
- };
41
- readonly cipherSuite: "TLS_AES_128_GCM_SHA256";
42
- readonly alpnProtocol?: string;
43
+ cipherSuite: CipherSuite;
44
+ protocolVersion: ProtocolVersion;
45
+ alpnProtocol?: string;
46
+ /** The peer's leaf certificate, once the handshake has validated it. */
47
+ peerCertificate?: Certificate;
43
48
  private readonly _transport;
44
- private readonly _applicationSecrets;
45
- constructor();
46
- read(): Promise<{
47
- payload: Uint8Array;
48
- }>;
49
- write(_data: Uint8Array): Promise<void>;
49
+ private readonly _profile;
50
+ private readonly _serverName;
51
+ private readonly _trustAnchors;
52
+ /** Buffered bytes not yet consumed by the record framer. */
53
+ private _readBuffer;
54
+ /** Running handshake transcript: full handshake messages (with 4-byte headers). */
55
+ private _transcript;
56
+ private _aead;
57
+ private _hash;
58
+ private _serverHello;
59
+ private _clientHsTraffic;
60
+ private _serverHsTraffic;
61
+ private _clientHsTrafficSecret;
62
+ private _serverHsTrafficSecret;
63
+ private _masterSecret;
64
+ private _applicationSecrets;
65
+ private _clientHsSeq;
66
+ private _serverHsSeq;
67
+ private _clientAppSeq;
68
+ private _serverAppSeq;
69
+ /** Decrypted application payloads awaiting consumption by read(). */
70
+ private _appReadQueue;
71
+ /** Lifecycle observers. */
72
+ private _closeListeners;
73
+ private _errorListeners;
74
+ constructor(options?: TlsOptions);
75
+ /**
76
+ * Drive the TLS 1.3 handshake to completion. Idempotent: calling it more than
77
+ * once on an open connection is a no-op.
78
+ */
79
+ handshake(timeoutMs?: number): Promise<void>;
80
+ read(): Promise<ApplicationData>;
81
+ write(data: Uint8Array): Promise<void>;
50
82
  close(): Promise<void>;
51
- on(_event: "close" | "error", _listener: (arg: CloseReason | TlsError) => void): this;
83
+ 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
+ /**
133
+ * Encrypt a payload under `traffic` and write it as an encrypted record whose
134
+ * outer type is application_data. The inner content type byte is appended to
135
+ * the plaintext before encryption.
136
+ */
137
+ private _writeEncryptedRecord;
138
+ /** Handle a non-application record encountered while reading application data. */
139
+ private _handlePostHandshakeRecord;
140
+ /** Translate a received alert into a typed error and close the connection. */
141
+ private _handleAlert;
142
+ private _withTimeout;
52
143
  private _ensureOpen;
144
+ private _transition;
145
+ private _emitError;
53
146
  }
54
- /** Generate key shares for the requested groups (delegates to @browsercore/crypto). */
55
- export declare function generateKeyShares(groups: readonly string[]): Promise<KeyPair[]>;
56
147
  //# 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;;;;;;;GAOG;AAGH,OAAO,KAAK,EAER,WAAW,EACX,OAAO,EACP,aAAa,EACb,UAAU,EACV,YAAY,EACZ,QAAQ,EACX,MAAM,YAAY,CAAC;AACpB,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAS5C;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAsB,UAAU,CAAC,OAAO,EAAE,UAAU,GAAG,OAAO,CAAC,aAAa,CAAC,CAqB5E;AAED,sEAAsE;AACtE,qBAAa,iBAAkB,YAAW,aAAa;IACnD,SAAgB,EAAE,EAAE,YAAY,CAAmC;IAC5D,KAAK,EAAE,QAAQ,CAA2B;IACjD,SAAgB,eAAe;;;MAAC;IAChC,SAAgB,WAAW,2BAAC;IAC5B,SAAgB,YAAY,CAAC,EAAE,MAAM,CAAC;IAItC,iBAAyB,UAAU,CAAY;IAC/C,iBAAyB,mBAAmB,CAA4B;;IAQ3D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