@browsercore/tls 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +65 -0
  3. package/dist/certificates/certificates.d.ts +78 -0
  4. package/dist/certificates/certificates.d.ts.map +1 -0
  5. package/dist/certificates/certificates.js +692 -0
  6. package/dist/certificates/certificates.js.map +1 -0
  7. package/dist/crypto/keySchedule.d.ts +65 -0
  8. package/dist/crypto/keySchedule.d.ts.map +1 -0
  9. package/dist/crypto/keySchedule.js +197 -0
  10. package/dist/crypto/keySchedule.js.map +1 -0
  11. package/dist/errors.d.ts +53 -0
  12. package/dist/errors.d.ts.map +1 -0
  13. package/dist/errors.js +72 -0
  14. package/dist/errors.js.map +1 -0
  15. package/dist/extensions/extensions.d.ts +89 -0
  16. package/dist/extensions/extensions.d.ts.map +1 -0
  17. package/dist/extensions/extensions.js +141 -0
  18. package/dist/extensions/extensions.js.map +1 -0
  19. package/dist/handshake/handshake.d.ts +98 -0
  20. package/dist/handshake/handshake.d.ts.map +1 -0
  21. package/dist/handshake/handshake.js +366 -0
  22. package/dist/handshake/handshake.js.map +1 -0
  23. package/dist/index.d.ts +24 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +18 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/profiles/profiles.d.ts +28 -0
  28. package/dist/profiles/profiles.d.ts.map +1 -0
  29. package/dist/profiles/profiles.js +68 -0
  30. package/dist/profiles/profiles.js.map +1 -0
  31. package/dist/record/record.d.ts +80 -0
  32. package/dist/record/record.d.ts.map +1 -0
  33. package/dist/record/record.js +135 -0
  34. package/dist/record/record.js.map +1 -0
  35. package/dist/tls.d.ts +56 -0
  36. package/dist/tls.d.ts.map +1 -0
  37. package/dist/tls.js +102 -0
  38. package/dist/tls.js.map +1 -0
  39. package/dist/types.d.ts +157 -0
  40. package/dist/types.d.ts.map +1 -0
  41. package/dist/types.js +12 -0
  42. package/dist/types.js.map +1 -0
  43. package/dist/utils.d.ts +15 -0
  44. package/dist/utils.d.ts.map +1 -0
  45. package/dist/utils.js +20 -0
  46. package/dist/utils.js.map +1 -0
  47. package/package.json +51 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Jens Verneuer
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE
package/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # @browsercore/tls
2
+
3
+ A TLS 1.3 (and 1.2 fallback) client implemented entirely in TypeScript.
4
+
5
+ ## Responsibility
6
+
7
+ Owns the full TLS handshake, record layer, key schedule, and X.509 certificate
8
+ validation. Provides an encrypted byte stream over an existing
9
+ `@browsercore/transport` connection so higher layers never touch plaintext on the wire.
10
+
11
+ ## What it does NOT know about
12
+
13
+ - HTTP (any version)
14
+ - Browser fingerprints
15
+ - Cookies
16
+
17
+ It knows about byte streams (`@browsercore/transport`) and cryptographic primitives
18
+ (`@browsercore/crypto`). It **never** imports `node:crypto` directly — that boundary
19
+ is `@browsercore/crypto`'s job, which keeps the crypto backend replaceable.
20
+
21
+ ## Public API
22
+
23
+ ```ts
24
+ import { connect } from "@browsercore/transport";
25
+ import { connectTls, resolveProfile, TlsHandshakeError } from "@browsercore/tls";
26
+
27
+ const transport = await connect({ host: "example.com", port: 443 });
28
+
29
+ const tls = await connectTls({
30
+ transport,
31
+ serverName: "example.com",
32
+ profile: resolveProfile("modern-tls13", "example.com"),
33
+ alpnProtocols: ["h2", "http/1.1"],
34
+ handshakeTimeoutMs: 10_000,
35
+ });
36
+
37
+ const response = await tls.read();
38
+ await tls.write(new TextEncoder().encode("GET / HTTP/1.1\r\n"));
39
+ await tls.close();
40
+ ```
41
+
42
+ ## Types
43
+
44
+ | Export | Kind | Purpose |
45
+ | --- | --- | --- |
46
+ | `TlsConnection` | interface | Public contract higher layers depend on |
47
+ | `connectTls()` | function | Perform the TLS handshake over a transport |
48
+ | `TlsState` | discriminated union | `connecting \| handshaking \| open \| closed` |
49
+ | `CloseReason` | discriminated union | Why a TLS connection closed |
50
+ | `ProtocolVersion` | discriminated union | `TLS 1.2` / `TLS 1.3` with wire codes |
51
+ | `CipherSuite` | string-literal union | Negotiated AEAD + hash |
52
+ | `ClientHelloConfig` | interface | ClientHello configuration (placeholder for @browsercore/profiles) |
53
+ | `TlsError` | class | Base typed error |
54
+ | `TlsHandshakeError` | class | Handshake failure at a specific phase |
55
+ | `TlsDecryptError` | class | Record decryption / auth failure |
56
+ | `TlsAlertError` class | TLS alert with level + description |
57
+
58
+ ## Dependency graph
59
+
60
+ ```
61
+ @browsercore/tls
62
+ └─ @browsercore/transport @browsercore/crypto
63
+ ```
64
+
65
+ No other `@browsercore/*` packages are imported.
@@ -0,0 +1,78 @@
1
+ /**
2
+ * X.509 certificate handling (RFC 8446 §4.4.2, RFC 5280).
3
+ *
4
+ * Parses DER-encoded certificates, validates hostnames against SAN/CN, and
5
+ * verifies certificate chains against trust anchors. The cryptographic
6
+ * signature verification itself is delegated to @browsercore/crypto — this module
7
+ * owns ASN.1 layout and validation policy only.
8
+ */
9
+ import type { SignatureScheme } from "../types.js";
10
+ /** A parsed X.509 certificate (validation-relevant fields only). */
11
+ export interface Certificate {
12
+ /** DER-encoded TBSCertificate bytes, needed for signature verification. */
13
+ readonly tbsBytes: Uint8Array;
14
+ /** DER-encoded SubjectPublicKeyInfo. */
15
+ readonly subjectPublicKeyInfo: Uint8Array;
16
+ /** Subject alternative DNS names (from the SAN extension), if any. */
17
+ readonly subjectAltNames: readonly string[];
18
+ /** Common Name from the subject DN, if present (legacy hostname match). */
19
+ readonly commonName?: string;
20
+ /** Not-before timestamp (epoch seconds). */
21
+ readonly notBefore: number;
22
+ /** Not-after timestamp (epoch seconds). */
23
+ readonly notAfter: number;
24
+ /** True if the certificate has the keyUsage bit for digital signature. */
25
+ readonly keyUsageDigitalSignature: boolean;
26
+ /** True if the certificate has the keyUsage bit for key encipherment/agreement. */
27
+ readonly keyUsageKeyEncipherment: boolean;
28
+ /** Signature algorithm used by the issuer over the TBSCertificate. */
29
+ readonly signatureScheme: SignatureScheme;
30
+ /** Raw signature value from the issuer. */
31
+ readonly signatureValue: Uint8Array;
32
+ /** DER-encoded SubjectPublicKeyInfo of the issuer (for chain building). */
33
+ readonly issuer: string;
34
+ /** True for a CA certificate. */
35
+ readonly isCa: boolean;
36
+ }
37
+ /** An ordered certificate chain: leaf first, intermediates follow, root last. */
38
+ export interface CertificateChain {
39
+ readonly leaf: Certificate;
40
+ readonly intermediates: readonly Certificate[];
41
+ readonly root: Certificate;
42
+ }
43
+ /** A trust anchor: a root certificate we trust a priori. */
44
+ export interface TrustAnchor {
45
+ readonly subjectPublicKeyInfo: Uint8Array;
46
+ readonly subject: string;
47
+ }
48
+ /**
49
+ * Parse a single DER-encoded X.509 certificate.
50
+ *
51
+ * Decodes the outer Certificate SEQUENCE into its TBSCertificate, signature
52
+ * algorithm, and signature value, then extracts the validation-relevant fields.
53
+ * `tbsBytes` is captured as the EXACT DER span of the TBSCertificate so that
54
+ * signature verification over it is byte-correct.
55
+ *
56
+ * Throws {@link TlsHandshakeError} with phase "certificate" on malformed input.
57
+ */
58
+ export declare function parseCertificate(buf: Uint8Array): Certificate;
59
+ /**
60
+ * Validate that a certificate is valid for the given hostname per RFC 6125.
61
+ *
62
+ * If the cert carries SAN DNS names, one must match (wildcard-aware, matching
63
+ * a single left-most label and NOT crossing dots). Otherwise we fall back to
64
+ * the legacy CommonName. IP-literal SANs are not matched here (server certs
65
+ * virtually always use DNS names).
66
+ */
67
+ export declare function validateHostname(cert: Certificate, hostname: string): boolean;
68
+ /**
69
+ * Verify a certificate chain: each cert is signed by the next, the root is in
70
+ * the trust anchors, and the leaf is valid for the hostname and not expired.
71
+ *
72
+ * Signature verification is delegated to @browsercore/crypto. Throws
73
+ * {@link TlsHandshakeError} with phase "certificate" on any failure.
74
+ */
75
+ export declare function verifyChain(chain: CertificateChain, trustAnchors: readonly TrustAnchor[], hostname: string, now: number): Promise<void>;
76
+ /** Parse a PEM block (base64 between BEGIN/END CERTIFICATE) into DER bytes. */
77
+ export declare function pemToDer(pem: string): Uint8Array;
78
+ //# sourceMappingURL=certificates.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"certificates.d.ts","sourceRoot":"","sources":["../../src/certificates/certificates.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAgNnD,oEAAoE;AACpE,MAAM,WAAW,WAAW;IACxB,2EAA2E;IAC3E,QAAQ,CAAC,QAAQ,EAAE,UAAU,CAAC;IAC9B,wCAAwC;IACxC,QAAQ,CAAC,oBAAoB,EAAE,UAAU,CAAC;IAC1C,sEAAsE;IACtE,QAAQ,CAAC,eAAe,EAAE,SAAS,MAAM,EAAE,CAAC;IAC5C,2EAA2E;IAC3E,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,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