@browsercore/tls 0.4.19 → 0.4.20

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 (45) hide show
  1. package/README.md +95 -22
  2. package/dist/certificates/ca-constraints.d.ts +15 -13
  3. package/dist/certificates/ca-constraints.d.ts.map +1 -1
  4. package/dist/certificates/ca-constraints.js +41 -59
  5. package/dist/certificates/ca-constraints.js.map +1 -1
  6. package/dist/certificates/cert-extensions.d.ts +59 -9
  7. package/dist/certificates/cert-extensions.d.ts.map +1 -1
  8. package/dist/certificates/cert-extensions.js +104 -50
  9. package/dist/certificates/cert-extensions.js.map +1 -1
  10. package/dist/certificates/certificate-path.d.ts +66 -7
  11. package/dist/certificates/certificate-path.d.ts.map +1 -1
  12. package/dist/certificates/certificate-path.js +125 -11
  13. package/dist/certificates/certificate-path.js.map +1 -1
  14. package/dist/certificates/certificates.d.ts.map +1 -1
  15. package/dist/certificates/certificates.js +107 -29
  16. package/dist/certificates/certificates.js.map +1 -1
  17. package/dist/certificates/extension-policy.d.ts +16 -6
  18. package/dist/certificates/extension-policy.d.ts.map +1 -1
  19. package/dist/certificates/extension-policy.js +17 -6
  20. package/dist/certificates/extension-policy.js.map +1 -1
  21. package/dist/certificates/extension-values.d.ts +131 -0
  22. package/dist/certificates/extension-values.d.ts.map +1 -0
  23. package/dist/certificates/extension-values.js +443 -0
  24. package/dist/certificates/extension-values.js.map +1 -0
  25. package/dist/certificates/general-names.d.ts +142 -0
  26. package/dist/certificates/general-names.d.ts.map +1 -0
  27. package/dist/certificates/general-names.js +354 -0
  28. package/dist/certificates/general-names.js.map +1 -0
  29. package/dist/certificates/name-constraints.d.ts +245 -0
  30. package/dist/certificates/name-constraints.d.ts.map +1 -0
  31. package/dist/certificates/name-constraints.js +692 -0
  32. package/dist/certificates/name-constraints.js.map +1 -0
  33. package/dist/certificates/name-match.d.ts +59 -0
  34. package/dist/certificates/name-match.d.ts.map +1 -0
  35. package/dist/certificates/name-match.js +271 -0
  36. package/dist/certificates/name-match.js.map +1 -0
  37. package/dist/errors.d.ts +47 -1
  38. package/dist/errors.d.ts.map +1 -1
  39. package/dist/errors.js +32 -0
  40. package/dist/errors.js.map +1 -1
  41. package/dist/index.d.ts +1 -1
  42. package/dist/index.d.ts.map +1 -1
  43. package/dist/index.js +1 -1
  44. package/dist/index.js.map +1 -1
  45. package/package.json +1 -1
package/README.md CHANGED
@@ -230,7 +230,9 @@ Exactly what is checked, and when:
230
230
  Usage is NOT processed for end-entity certificates … however it is
231
231
  considered 'handled' here in order to allow being marked as critical"),
232
232
  extendedKeyUsage (2.5.29.37, processed — rule 7), subjectAltName
233
- (2.5.29.17, processed — the hostname check), policyMappings (2.5.29.33),
233
+ (2.5.29.17, processed — the hostname check), nameConstraints (2.5.29.30,
234
+ processed — rule 9; BoringSSL's branch is unconditional `return true`
235
+ BECAUSE it processes the constraints), policyMappings (2.5.29.33),
234
236
  policyConstraints (2.5.29.36) and inhibitAnyPolicy (2.5.29.54) (accepted
235
237
  like BoringSSL's unconditional `return true`; policy ENFORCEMENT is a
236
238
  documented limitation below), certificatePolicies (2.5.29.32) when the
@@ -255,33 +257,104 @@ Exactly what is checked, and when:
255
257
  rejected, and a present-but-malformed EKU value rejected
256
258
  (`kFailedParsingEku`).
257
259
 
260
+ 9. **nameConstraints (RFC 5280 §4.2.1.10)** — on the built path, processed
261
+ EXACTLY as BoringSSL pki/name_constraints.cc processes it for TLS server
262
+ authentication at the pinned revision. The extension VALUE is parsed for
263
+ EVERY certificate carrying it, critical or not (`kFailedParsingName
264
+ Constraints` rejects the certificate): the extnValue is one SEQUENCE {
265
+ permittedSubtrees [0] OPTIONAL, excludedSubtrees [1] OPTIONAL }, at least
266
+ one side present ("Conforming CAs MUST NOT issue certificates where name
267
+ constraints is an empty sequence"), each side SEQUENCE SIZE (1..MAX) OF
268
+ GeneralSubtree { base } with NO minimum/maximum (name_constraints.cc
269
+ 179-181 rejects any element after the base — including an explicitly
270
+ encoded DEFAULT minimum), an iPAddress base of exactly 8 or 32 octets with
271
+ a contiguous-ones CIDR netmask, ASCII dNSName/rfc822Name bases, and no
272
+ unknown GeneralName tag. CONSTRAINTS apply to every certificate BELOW the
273
+ carrier — the trust anchor's included (Chrome arms caller-supplied
274
+ constrained anchors with `.WithEnforceAnchorConstraints()`) and never the
275
+ carrier itself ("Don't do any other checks for root certificates";
276
+ name_constraints_list_ is consumed by the NEXT certificates). A
277
+ SELF-ISSUED non-target certificate is skipped ("If certificate i is
278
+ self-issued and it is not the final certificate in the path, skip this
279
+ step for certificate i"). The per-name rules: dNSName suffix matching,
280
+ case-insensitive, label-boundary enforced ("foobar.com is not a match for
281
+ bar.com"), a leading-dot constraint permits SUBDOMAINS ONLY ("foo.bar.com
282
+ matches constraint .bar.com, but bar.com doesn't"), one trailing dot
283
+ stripped from BOTH sides, the EMPTY constraint matches everything,
284
+ wildcards are literal strings with BoringSSL's two special cases
285
+ (exclusions: "*.bar.com" matches constraint "foo.bar.com" — any expansion;
286
+ permissions: it matches only a constraint matching EVERY expansion);
287
+ iPAddress by netmask with no cross-family matching (an IPv6 SAN under
288
+ IPv4-only permitted subtrees is not permitted); directoryName by
289
+ RFC 5280 §7.1 subtree match (prefix-of-RDNs-in-order, per-attribute
290
+ normalization — verify_name_match.cc) applied to SAN directoryNames AND
291
+ the subject DN (unless the subject is empty with a SAN — RFC 5280 §4.1.2.6);
292
+ rfc822Name mailbox/domain forms plus the subject's emailAddress attributes
293
+ when NO SAN exists (RFC 5280 §4.2.1.10 legacy rule). commonName is NEVER
294
+ consulted: "this method does not check hostname or IP address in
295
+ commonName, which is deprecated (crbug.com/308330)"
296
+ (name_constraints.h:59-60) — and there is NO empty-SAN rejection rule at
297
+ the pinned revision (every check iterates the names actually present).
298
+ Constraints on forms BoringSSL does not process (URI, otherName,
299
+ x400Address, ediPartyName, registeredID) are ignored when the extension is
300
+ NON-critical (`constrained_name_types_ & kSupportedNameTypes` mask) and —
301
+ when critical — reject any certificate whose SAN carries that form
302
+ (name_constraints.cc:450-454, RFC 5280's MUST-process-or-reject). A DoS
303
+ ceiling caps name × constraint combinations at 2^20
304
+ (`kMaxChecks = 1048576`). Violations fail with
305
+ `name-constraint-violation`, naming the constrained certificate (wire
306
+ index + issuer DN), the constraint holder, the offending name and whether
307
+ it matched an excludedSubtrees entry or no permittedSubtrees entry.
308
+
309
+ 10. **Malformed NON-critical extension values are rejected (Part B parity)** —
310
+ BoringSSL's ParsedCertificate::Create parses the VALUE of basic
311
+ Constraints, keyUsage, extendedKeyUsage, subjectAltName, nameConstraints,
312
+ authorityInfoAccess, certificatePolicies, policyConstraints,
313
+ policyMappings, inhibitAnyPolicy, subjectKeyIdentifier and
314
+ authorityKeyIdentifier at certificate-parse time — critical or NOT — and
315
+ rejects the whole certificate on a malformed value (`kFailedParsing…`).
316
+ This verifier does the same with a typed `TlsMalformedExtensionError`
317
+ naming the extension OID. Faults per extension follow BoringSSL exactly
318
+ (e.g. an empty AIA list, an empty certificatePolicies sequence, duplicate
319
+ policy OIDs, qualifiers under anyPolicy restricted to CPS/userNotice —
320
+ unknown qualifier OIDs under OTHER policies are TOLERATED at parse time —
321
+ an empty policyConstraints sequence, an inhibitAnyPolicy that is not one
322
+ uint8 INTEGER, an authorityKeyIdentifier with only one of [1]/[2]);
323
+ explicitly-encoded cA=FALSE in basicConstraints is tolerated (BoringSSL's
324
+ own TODO — real certificates use the broken encoding), and an EMPTY
325
+ AuthorityKeyIdentifier SEQUENCE is legal. cRLDistributionPoints is
326
+ deliberately NOT value-parsed: `ParseCrlDistributionPoints` has no caller
327
+ in the parse path at the pinned revision, so a malformed value does not
328
+ reject the certificate in Chrome — and does not here.
329
+
258
330
  Every certificate-phase failure is a `TlsCertificateError` (a
259
331
  `TlsHandshakeError` with phase `"certificate"`) carrying a discriminated
260
332
  `reason`: `no-anchors` (verifyChain called with an empty anchor list — never a
261
333
  vacuous success), `no-issuer`, `bad-signature`, `expired`, `not-yet-valid`,
262
334
  `not-a-ca`, `missing-key-cert-sign`, `path-too-long`,
263
- `unsupported-critical-extension`, `missing-server-auth-eku`. The message names
264
- the offending certificate (wire index + issuer DN — and, for the extension and
265
- EKU failures, the offending OID) so an operator can identify it without
266
- re-running the handshake.
267
-
268
- Explicit limitations — current scope, tracked as a separate follow-up subset
335
+ `unsupported-critical-extension`, `missing-server-auth-eku`,
336
+ `name-constraint-violation`. The message names the offending certificate (wire
337
+ index + issuer DN — and, for the extension, EKU and name-constraint failures,
338
+ the offending OID / offending name) so an operator can identify it without
339
+ re-running the handshake. A malformed extension VALUE at PARSE time throws a
340
+ `TlsMalformedExtensionError` (a `TlsHandshakeError` subclass with phase
341
+ "certificate" carrying the `extensionOid`).
342
+
343
+ Explicit limitations — current scope, tracked as separate follow-up subsets
269
344
  (not permanent design decisions): no revocation checking of any kind (no
270
- CRL, no OCSP — neither stapled nor fetched); NO NAME CONSTRAINTS (RFC 5280
271
- §4.2.1.10) — a certificate carrying a CRITICAL nameConstraints extension is
272
- REJECTED (`unsupported-critical-extension`) until the name-constraints subset
273
- lands, because this verifier cannot process it and RFC 5280 §4.2 requires
274
- rejecting unprocessed critical extensions (the same certificate WITHOUT the
275
- critical flag is accepted; openssl, which processes name constraints, accepts
276
- both); no AIA fetching; no policy constraints / policy mappings /
277
- certificate-policies ENFORCEMENT (the critical-acceptance of those OIDs above
278
- only mirrors BoringSSL's parseability bar — the constraint VALUES are not
279
- applied, and non-critical policy extensions are not parsed at all: a
280
- malformed VALUE of an extension this verifier does not process is ignored
281
- where Chrome rejects the certificate at parse time); no IP-address SAN
282
- matching. Additionally, DN comparison is byte-exact: a path whose
283
- issuer/subject encodings differ for the same DN (rare; real CAs copy the DER
284
- bytes) fails with `no-issuer` rather than being leniently matched, and the
345
+ CRL, no OCSP — neither stapled nor fetched); no AIA fetching; no policy
346
+ constraints / policy mappings / certificate-policies ENFORCEMENT (the
347
+ critical-acceptance of those OIDs mirrors BoringSSL's parseability bar and the
348
+ values are parsed per rule 10 — but the policy GRAPH is not evaluated, exactly
349
+ like BoringSSL's own policy processing which this verifier does not implement;
350
+ under Chrome's SERVER_AUTH verification the initial policy set is {anyPolicy},
351
+ so these cannot make Chrome reject a path this verifier would accept); no
352
+ IP-address SAN matching for the HOSTNAME check (name-constraint iPAddress
353
+ checking IS implemented — rule 9). Additionally, DN comparison for path
354
+ BUILDING is byte-exact: a path whose issuer/subject encodings differ for the
355
+ same DN (rare; real CAs copy the DER bytes) fails with `no-issuer` rather than
356
+ being leniently matched (the name-constraint DN matcher, by contrast, is
357
+ BoringSSL's normalized per-attribute comparison — rule 9), and the
285
358
  inner/outer AlgorithmIdentifier equality of RFC 5280 §4.1.2.2 is not enforced
286
359
  (the TBSCertificate identifier is authoritative).
287
360
 
@@ -23,19 +23,21 @@ export declare const KEY_USAGE_KEY_CERT_SIGN_BIT = 5;
23
23
  */
24
24
  export declare function hasKeyUsageBit(value: Uint8Array, bitIndex: number): boolean;
25
25
  /**
26
- * Parse BasicConstraints (unwrapped extnValue: SEQUENCE { cA BOOLEAN DEFAULT
27
- * FALSE, pathLenConstraint INTEGER? }) and return the pathLenConstraint, or
28
- * null when it is absent.
26
+ * Parse the pathLenConstraint INTEGER element (RFC 5280 §4.2.1.9,
27
+ * `intTlv` already read inside the BasicConstraints SEQUENCE) under
28
+ * BoringSSL der::ParseUint8's rule — the same test ParseBasicConstraints
29
+ * applies at certificate-parse time, whose failure rejects the whole
30
+ * certificate (kFailedParsingBasicConstraints): the INTEGER must be
31
+ * non-negative (no high-bit-set leading byte), minimally encoded (a leading
32
+ * 0x00 is allowed only before a byte with the high bit set), and its value
33
+ * must fit one unsigned byte (≤ 255).
29
34
  *
30
- * Lenient about structurally odd extension VALUES (non-SEQUENCE content,
31
- * no INTEGER element, an over-long INTEGER all yield null — "no usable
32
- * constraint"). A sign-violating or NON-MINIMAL INTEGER, however, is a
33
- * malformed certificate and THROWS a typed error: RFC 5280 §4.2.1.9 defines
34
- * pathLenConstraint as a non-negative INTEGER, so a negative value (DER
35
- * two's-complement, high bit of the first content byte set) or a redundant
36
- * padding octet (the non-minimal encodings BoringSSL also rejects) cannot be
37
- * interpreted as "no constraint" — silently ignoring them would turn a
38
- * malformed constraint into an unlimited one.
35
+ * Returns the constraint value, or null when the element is absent. Throws
36
+ * the typed malformed-extension error otherwise.
39
37
  */
40
- export declare function parsePathLenConstraint(value: Uint8Array): number | null;
38
+ export declare function parsePathLenConstraint(value: Uint8Array, intTlv: {
39
+ tag: number;
40
+ valueStart: number;
41
+ end: number;
42
+ }, extensionOid: string): number | null;
41
43
  //# sourceMappingURL=ca-constraints.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ca-constraints.d.ts","sourceRoot":"","sources":["../../src/certificates/ca-constraints.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAKH;;;;GAIG;AACH,eAAO,MAAM,2BAA2B,IAAI,CAAC;AAE7C;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAe3E;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,GAAG,IAAI,CAiDvE"}
1
+ {"version":3,"file":"ca-constraints.d.ts","sourceRoot":"","sources":["../../src/certificates/ca-constraints.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAKH;;;;GAIG;AACH,eAAO,MAAM,2BAA2B,IAAI,CAAC;AAE7C;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAe3E;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,sBAAsB,CAClC,KAAK,EAAE,UAAU,EACjB,MAAM,EAAE;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,EACxD,YAAY,EAAE,MAAM,GACrB,MAAM,GAAG,IAAI,CAgDf"}
@@ -10,7 +10,7 @@
10
10
  * unwrapped extension values to both parsers.
11
11
  */
12
12
  import { readTlv } from "./der.js";
13
- import { TlsHandshakeError } from "../errors.js";
13
+ import { TlsMalformedExtensionError } from "../errors.js";
14
14
  /**
15
15
  * keyUsage BIT STRING bit position of keyCertSign (RFC 5280 §4.2.1.3):
16
16
  * digitalSignature(0), nonRepudiation(1), keyEncipherment(2),
@@ -40,68 +40,50 @@ export function hasKeyUsageBit(value, bitIndex) {
40
40
  return (byte & (1 << (7 - bitInByte))) !== 0;
41
41
  }
42
42
  /**
43
- * Parse BasicConstraints (unwrapped extnValue: SEQUENCE { cA BOOLEAN DEFAULT
44
- * FALSE, pathLenConstraint INTEGER? }) and return the pathLenConstraint, or
45
- * null when it is absent.
43
+ * Parse the pathLenConstraint INTEGER element (RFC 5280 §4.2.1.9,
44
+ * `intTlv` already read inside the BasicConstraints SEQUENCE) under
45
+ * BoringSSL der::ParseUint8's rule — the same test ParseBasicConstraints
46
+ * applies at certificate-parse time, whose failure rejects the whole
47
+ * certificate (kFailedParsingBasicConstraints): the INTEGER must be
48
+ * non-negative (no high-bit-set leading byte), minimally encoded (a leading
49
+ * 0x00 is allowed only before a byte with the high bit set), and its value
50
+ * must fit one unsigned byte (≤ 255).
46
51
  *
47
- * Lenient about structurally odd extension VALUES (non-SEQUENCE content,
48
- * no INTEGER element, an over-long INTEGER all yield null — "no usable
49
- * constraint"). A sign-violating or NON-MINIMAL INTEGER, however, is a
50
- * malformed certificate and THROWS a typed error: RFC 5280 §4.2.1.9 defines
51
- * pathLenConstraint as a non-negative INTEGER, so a negative value (DER
52
- * two's-complement, high bit of the first content byte set) or a redundant
53
- * padding octet (the non-minimal encodings BoringSSL also rejects) cannot be
54
- * interpreted as "no constraint" — silently ignoring them would turn a
55
- * malformed constraint into an unlimited one.
52
+ * Returns the constraint value, or null when the element is absent. Throws
53
+ * the typed malformed-extension error otherwise.
56
54
  */
57
- export function parsePathLenConstraint(value) {
58
- const seq = readTlv(value, 0);
59
- if (seq.tag !== 0x30) {
60
- return null;
55
+ export function parsePathLenConstraint(value, intTlv, extensionOid) {
56
+ const content = value.subarray(intTlv.valueStart, intTlv.end);
57
+ const first = content[0];
58
+ if (first === undefined) {
59
+ throw new TlsMalformedExtensionError(extensionOid, "pathLenConstraint INTEGER has no content bytes (der::ParseUint8)");
61
60
  }
62
- let o = seq.valueStart;
63
- while (o < seq.end) {
64
- const element = readTlv(value, o);
65
- if (element.tag === 0x02) {
66
- // INTEGER: big-endian two's complement; a pathLenConstraint is
67
- // always non-negative (0..MAX) and fits in a few bytes.
68
- const content = value.subarray(element.valueStart, element.end);
69
- if (content.length === 0 || content.length > 4) {
70
- return null;
71
- }
72
- const first = content[0];
73
- if (first === undefined) {
74
- return null;
75
- }
76
- if ((first & 0x80) !== 0) {
77
- // High bit of the first byte set: a negative two's-complement
78
- // INTEGER — never a valid pathLenConstraint.
79
- throw new TlsHandshakeError("certificate", {
80
- cause: new Error("malformed BasicConstraints pathLenConstraint: negative INTEGER"),
81
- });
82
- }
83
- if (content.length > 1) {
84
- const second = content[1] ?? 0;
85
- if (first === 0x00 && (second & 0x80) === 0) {
86
- // Redundant positive padding: DER requires the minimal
87
- // encoding (02 02 00 05 is not a valid encoding of 5).
88
- throw new TlsHandshakeError("certificate", {
89
- cause: new Error("malformed BasicConstraints pathLenConstraint: non-minimal INTEGER encoding"),
90
- });
91
- }
92
- }
93
- let decoded = 0;
94
- // Iteration over the slice yields plain numbers — no
95
- // noUncheckedIndexedAccess branch, and the bounds are validated by
96
- // readTlv above.
97
- for (const byte of content) {
98
- decoded = (decoded << 8) | byte;
99
- }
100
- return decoded;
61
+ if ((first & 0x80) !== 0) {
62
+ // High bit of the first byte set: a negative two's-complement
63
+ // INTEGER — never a valid pathLenConstraint.
64
+ throw new TlsMalformedExtensionError(extensionOid, "pathLenConstraint must be a non-negative INTEGER; the leading byte makes it negative (der::ParseUint8)");
65
+ }
66
+ if (content.length === 1) {
67
+ return first;
68
+ }
69
+ const second = content[1] ?? 0;
70
+ if (first === 0x00) {
71
+ // Padded form: DER minimality permits the 0x00 only when the next
72
+ // byte's high bit is set (otherwise the padding octet is redundant —
73
+ // CBS_is_valid_asn1_integer rejects it, so der::ParseUint8 does too).
74
+ if ((second & 0x80) === 0) {
75
+ throw new TlsMalformedExtensionError(extensionOid, "pathLenConstraint INTEGER is not minimally encoded (a leading 0x00 is only valid before a byte with the high bit set)");
76
+ }
77
+ if (content.length > 2) {
78
+ // [0x00, 0x80, ...] — the value is ≥ 2^15, far above one byte.
79
+ throw new TlsMalformedExtensionError(extensionOid, "pathLenConstraint must fit one unsigned byte — der::ParseUint8 (ParseUint64) rejects values above 255");
101
80
  }
102
- // The cA BOOLEAN (or anything unexpected) precedes the INTEGER — walk on.
103
- o = element.end;
81
+ // [0x00, 0x80..0xFF] — the two's-complement leading zero makes the
82
+ // value the unsigned second byte (128..255).
83
+ return second;
104
84
  }
105
- return null;
85
+ // first in 0x01..0x7F with ≥ 2 content bytes: a minimal encoding whose
86
+ // value is ≥ 256 — above the uint8 range ParseUint8 accepts.
87
+ throw new TlsMalformedExtensionError(extensionOid, "pathLenConstraint must fit one unsigned byte — der::ParseUint8 (ParseUint64) rejects values above 255");
106
88
  }
107
89
  //# sourceMappingURL=ca-constraints.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"ca-constraints.js","sourceRoot":"","sources":["../../src/certificates/ca-constraints.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AACnC,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEjD;;;;GAIG;AACH,MAAM,CAAC,MAAM,2BAA2B,GAAG,CAAC,CAAC;AAE7C;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAC,KAAiB,EAAE,QAAgB;IAC9D,MAAM,SAAS,GAAG,OAAO,CAAC,KAAK,EAAE,CAA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IACzB,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QACtB,MAAM,IAAI,0BAA0B,CAChC,YAAY,EACZ,kEAAkE,CACrE,CAAC;IACN,CAAC;IACD,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;QACvB,8DAA8D;QAC9D,6CAA6C;QAC7C,MAAM,IAAI,0BAA0B,CAChC,YAAY,EACZ,wGAAwG,CAC3G,CAAC;IACN,CAAC;IACD,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACvB,OAAO,KAAK,CAAC;IACjB,CAAC;IACD,MAAM,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;IAC/B,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QACjB,kEAAkE;QAClE,qEAAqE;QACrE,sEAAsE;QACtE,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;YACxB,MAAM,IAAI,0BAA0B,CAChC,YAAY,EACZ,uHAAuH,CAC1H,CAAC;QACN,CAAC;QACD,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACrB,+DAA+D;YAC/D,MAAM,IAAI,0BAA0B,CAChC,YAAY,EACZ,uGAAuG,CAC1G,CAAC;QACN,CAAC;QACD,mEAAmE;QACnE,6CAA6C;QAC7C,OAAO,MAAM,CAAC;IAClB,CAAC;IACD,uEAAuE;IACvE,6DAA6D;IAC7D,MAAM,IAAI,0BAA0B,CAChC,YAAY,EACZ,uGAAuG,CAC1G,CAAC;AACN,CAAC"}
@@ -11,6 +11,12 @@
11
11
  * extension-policy.ts — this layer only turns bytes into structured fields.
12
12
  */
13
13
  import type { CertificateExtension } from "./certificate-path.js";
14
+ import { parseGeneralNames, type GeneralNames } from "./general-names.js";
15
+ import { parseNameConstraints, type NameConstraints } from "./name-constraints.js";
16
+ import { BASIC_CONSTRAINTS_OID, EXTENDED_KEY_USAGE_OID, KEY_USAGE_OID, SUBJECT_ALT_NAME_OID } from "./extension-policy.js";
17
+ export { parseGeneralNames, parseNameConstraints };
18
+ export type { GeneralNames, NameConstraints };
19
+ export { BASIC_CONSTRAINTS_OID, EXTENDED_KEY_USAGE_OID, KEY_USAGE_OID, SUBJECT_ALT_NAME_OID };
14
20
  /**
15
21
  * Parse the subject Name and return a readable DN string (RFC 2253-ish).
16
22
  * Used only for the `issuer` field and debug-friendliness.
@@ -61,23 +67,67 @@ export declare function parseExtensionsBlock(buf: Uint8Array, start: number, _en
61
67
  */
62
68
  export declare function parseExtendedKeyUsage(value: Uint8Array): readonly string[];
63
69
  /**
64
- * Parse the SAN extension value (already unwrapped from its OCTET STRING).
65
- * The value is a SEQUENCE OF GeneralName; we only extract dNSName entries
66
- * (context tag [2]).
70
+ * Parse the SAN extension value (already unwrapped from its OCTET STRING) —
71
+ * the STRICT GeneralNames::Create twin (see general-names.ts for the exact
72
+ * fault list): BoringSSL parses the SAN value for EVERY certificate carrying
73
+ * the extension, critical or not, and a fault rejects the whole certificate
74
+ * (kFailedParsingSubjectAltName). Returns the full parsed name set (the DNS
75
+ * names the hostname check uses, plus the rfc822 / iPAddress / directoryName
76
+ * forms the name-constraint engine consumes).
67
77
  */
68
- export declare function parseSubjectAltNames(value: Uint8Array): readonly string[];
78
+ export declare function parseSubjectAltNames(value: Uint8Array): GeneralNames;
69
79
  /**
70
80
  * Parse the KeyUsage extension value (unwrapped OCTET STRING holding a BIT
71
- * STRING). Returns the digitalSignature (bit 0) and keyEncipherment (bit 2)
72
- * flags.
81
+ * STRING) — the STRICT ParseKeyUsage twin (pki/parse_certificate.cc:794):
82
+ * BoringSSL runs this for every keyUsage extension, critical or not, and a
83
+ * fault rejects the certificate (kFailedParsingKeyUsage):
84
+ *
85
+ * - the value must be exactly one BIT STRING (nothing before or after —
86
+ * "By definition the input was a single BIT STRING");
87
+ * - the unused-bits byte must be < 8 and a BIT STRING with no data bytes
88
+ * must declare 0 unused bits (CBS_asn1_bitstring rules);
89
+ * - "When the keyUsage extension appears in a certificate, at least one of
90
+ * the bits MUST be set to 1" (RFC 5280 §4.2.1.3) — an all-zero BIT STRING
91
+ * is the parse failure (`if (BitStringIsAllZeros(...)) { return false; }`).
92
+ *
93
+ * Returns the digitalSignature (bit 0) and keyEncipherment (bit 2) flags.
73
94
  */
74
95
  export declare function parseKeyUsage(value: Uint8Array): {
75
96
  readonly digitalSignature: boolean;
76
97
  readonly keyEncipherment: boolean;
77
98
  };
78
99
  /**
79
- * Parse the BasicConstraints extension value (unwrapped OCTET STRING holding a
80
- * SEQUENCE { cA BOOLEAN DEFAULT FALSE, pathLenConstraint INTEGER? }).
100
+ * The strict BasicConstraints parse result (see {@link parseBasicConstraints}).
101
+ */
102
+ export interface BasicConstraints {
103
+ readonly isCa: boolean;
104
+ /**
105
+ * pathLenConstraint (RFC 5280 §4.2.1.9), or null when absent. A value
106
+ * outside 0..255 or with a non-minimal / negative DER encoding is a
107
+ * malformed certificate (BoringSSL der::ParseUint8 fails →
108
+ * kFailedParsingBasicConstraints) and THROWS.
109
+ */
110
+ readonly pathLenConstraint: number | null;
111
+ }
112
+ /**
113
+ * Parse the BasicConstraints extension value (unwrapped OCTET STRING holding
114
+ * SEQUENCE { cA BOOLEAN DEFAULT FALSE, pathLenConstraint INTEGER? }) — the
115
+ * STRICT ParseBasicConstraints twin (pki/parse_certificate.cc:735): BoringSSL
116
+ * runs this for every basicConstraints extension, critical or not, and a
117
+ * fault rejects the certificate (kFailedParsingBasicConstraints):
118
+ *
119
+ * - the value must be exactly one SEQUENCE (nothing trailing);
120
+ * - the cA BOOLEAN, when present, is der::ParseBool — one byte, 0x00 or
121
+ * 0xFF (X.690 §11.1); an explicitly-encoded FALSE is TOLERATED
122
+ * (BoringSSL's own TODO: "Should reject if CA was set to false… In
123
+ * practice however there are a lot of certificates that use the broken
124
+ * encoding");
125
+ * - the pathLenConstraint INTEGER, when present, must decode as uint8 —
126
+ * non-negative, minimally encoded, value ≤ 255 (der::ParseUint8; a larger
127
+ * constraint cannot appear in a certificate Chrome accepts);
128
+ * - nothing may follow inside the SEQUENCE.
129
+ *
130
+ * An empty SEQUENCE parses fine with cA = FALSE (the DEFAULT applies).
81
131
  */
82
- export declare function parseBasicConstraints(value: Uint8Array): boolean;
132
+ export declare function parseBasicConstraints(value: Uint8Array): BasicConstraints;
83
133
  //# sourceMappingURL=cert-extensions.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"cert-extensions.d.ts","sourceRoot":"","sources":["../../src/certificates/cert-extensions.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAIH,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAElE;;;GAGG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAiC9E;AAED,oEAAoE;AACpE,wBAAgB,eAAe,CAAC,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CA8BhG;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,SAAS,oBAAoB,EAAE,CAgFlH;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,UAAU,GAAG,SAAS,MAAM,EAAE,CAoB1E;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,UAAU,GAAG,SAAS,MAAM,EAAE,CAoBzE;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,UAAU,GAAG;IAC9C,QAAQ,CAAC,gBAAgB,EAAE,OAAO,CAAC;IACnC,QAAQ,CAAC,eAAe,EAAE,OAAO,CAAC;CACrC,CAiCA;AAED;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B,oBAAoB,CAAC,KAAK,EAAE,UAAU,GAAG,YAAY,CAEpE;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,UAAU,GAAG;IAC9C,QAAQ,CAAC,gBAAgB,EAAE,OAAO,CAAC;IACnC,QAAQ,CAAC,eAAe,EAAE,OAAO,CAAC;CACrC,CAiDA;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC7B,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IACvB;;;;;OAKG;IACH,QAAQ,CAAC,iBAAiB,EAAE,MAAM,GAAG,IAAI,CAAC;CAC7C;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,UAAU,GAAG,gBAAgB,CAuCzE"}
@@ -11,7 +11,17 @@
11
11
  * extension-policy.ts — this layer only turns bytes into structured fields.
12
12
  */
13
13
  import { readTlv, peekTag, parseOid } from "./der.js";
14
- import { TlsHandshakeError } from "../errors.js";
14
+ import { TlsHandshakeError, TlsMalformedExtensionError } from "../errors.js";
15
+ import { parseGeneralNames } from "./general-names.js";
16
+ import { parseNameConstraints } from "./name-constraints.js";
17
+ import { parsePathLenConstraint } from "./ca-constraints.js";
18
+ import { BASIC_CONSTRAINTS_OID, EXTENDED_KEY_USAGE_OID, KEY_USAGE_OID, SUBJECT_ALT_NAME_OID } from "./extension-policy.js";
19
+ // ONE-definition plumbing (code-standards §22): certificates.ts keeps its
20
+ // import budget by reaching the name-constraint engine through this module
21
+ // (the extension-parsing module it already depends on) instead of adding a
22
+ // direct dependency.
23
+ export { parseGeneralNames, parseNameConstraints };
24
+ export { BASIC_CONSTRAINTS_OID, EXTENDED_KEY_USAGE_OID, KEY_USAGE_OID, SUBJECT_ALT_NAME_OID };
15
25
  /**
16
26
  * Parse the subject Name and return a readable DN string (RFC 2253-ish).
17
27
  * Used only for the `issuer` field and debug-friendliness.
@@ -202,18 +212,14 @@ export function parseExtensionsBlock(buf, start, _end) {
202
212
  export function parseExtendedKeyUsage(value) {
203
213
  const seq = readTlv(value, 0);
204
214
  if (seq.tag !== 0x30) {
205
- throw new TlsHandshakeError("certificate", {
206
- cause: new Error(`expected extendedKeyUsage SEQUENCE, got tag 0x${seq.tag.toString(16)}`),
207
- });
215
+ throw new TlsMalformedExtensionError(EXTENDED_KEY_USAGE_OID, `expected extendedKeyUsage SEQUENCE, got tag 0x${seq.tag.toString(16)}`);
208
216
  }
209
217
  const oids = [];
210
218
  let o = seq.valueStart;
211
219
  while (o < seq.end) {
212
220
  const oidTlv = readTlv(value, o);
213
221
  if (oidTlv.tag !== 0x06) {
214
- throw new TlsHandshakeError("certificate", {
215
- cause: new Error(`expected KeyPurposeId OBJECT IDENTIFIER in extendedKeyUsage, got tag 0x${oidTlv.tag.toString(16)}`),
216
- });
222
+ throw new TlsMalformedExtensionError(EXTENDED_KEY_USAGE_OID, `expected KeyPurposeId OBJECT IDENTIFIER in extendedKeyUsage, got tag 0x${oidTlv.tag.toString(16)}`);
217
223
  }
218
224
  oids.push(parseOid(value, oidTlv.valueStart, oidTlv.end));
219
225
  o = oidTlv.end;
@@ -221,56 +227,63 @@ export function parseExtendedKeyUsage(value) {
221
227
  return Object.freeze(oids);
222
228
  }
223
229
  /**
224
- * Parse the SAN extension value (already unwrapped from its OCTET STRING).
225
- * The value is a SEQUENCE OF GeneralName; we only extract dNSName entries
226
- * (context tag [2]).
230
+ * Parse the SAN extension value (already unwrapped from its OCTET STRING) —
231
+ * the STRICT GeneralNames::Create twin (see general-names.ts for the exact
232
+ * fault list): BoringSSL parses the SAN value for EVERY certificate carrying
233
+ * the extension, critical or not, and a fault rejects the whole certificate
234
+ * (kFailedParsingSubjectAltName). Returns the full parsed name set (the DNS
235
+ * names the hostname check uses, plus the rfc822 / iPAddress / directoryName
236
+ * forms the name-constraint engine consumes).
227
237
  */
228
238
  export function parseSubjectAltNames(value) {
229
- const seq = readTlv(value, 0);
230
- if (seq.tag !== 0x30) {
231
- return Object.freeze([]);
232
- }
233
- const names = [];
234
- const textDecoder = new TextDecoder();
235
- let o = seq.valueStart;
236
- while (o < seq.end) {
237
- const tlv = readTlv(value, o);
238
- // GeneralName: context-specific tag. dNSName is [2] (0x82), iPAddress [7].
239
- const tag = tlv.tag;
240
- const tagClass = tag & 0xc0;
241
- const tagNumber = tag & 0x1f;
242
- if (tagClass === 0x80 && tagNumber === 2) {
243
- names.push(textDecoder.decode(value.subarray(tlv.valueStart, tlv.end)));
244
- }
245
- o = tlv.end;
246
- }
247
- return Object.freeze(names);
239
+ return parseGeneralNames(value, SUBJECT_ALT_NAME_OID);
248
240
  }
249
241
  /**
250
242
  * Parse the KeyUsage extension value (unwrapped OCTET STRING holding a BIT
251
- * STRING). Returns the digitalSignature (bit 0) and keyEncipherment (bit 2)
252
- * flags.
243
+ * STRING) — the STRICT ParseKeyUsage twin (pki/parse_certificate.cc:794):
244
+ * BoringSSL runs this for every keyUsage extension, critical or not, and a
245
+ * fault rejects the certificate (kFailedParsingKeyUsage):
246
+ *
247
+ * - the value must be exactly one BIT STRING (nothing before or after —
248
+ * "By definition the input was a single BIT STRING");
249
+ * - the unused-bits byte must be < 8 and a BIT STRING with no data bytes
250
+ * must declare 0 unused bits (CBS_asn1_bitstring rules);
251
+ * - "When the keyUsage extension appears in a certificate, at least one of
252
+ * the bits MUST be set to 1" (RFC 5280 §4.2.1.3) — an all-zero BIT STRING
253
+ * is the parse failure (`if (BitStringIsAllZeros(...)) { return false; }`).
254
+ *
255
+ * Returns the digitalSignature (bit 0) and keyEncipherment (bit 2) flags.
253
256
  */
254
257
  export function parseKeyUsage(value) {
255
258
  const bitString = readTlv(value, 0);
256
259
  if (bitString.tag !== 0x03) {
257
- return { digitalSignature: false, keyEncipherment: false };
260
+ throw new TlsMalformedExtensionError(KEY_USAGE_OID, "expected a single BIT STRING");
261
+ }
262
+ if (bitString.end !== value.length) {
263
+ throw new TlsMalformedExtensionError(KEY_USAGE_OID, "trailing data after the BIT STRING");
258
264
  }
259
- // BIT STRING content: first byte = number of unused bits; then the bits.
260
265
  const content = value.subarray(bitString.valueStart, bitString.end);
261
- if (content.length < 2) {
262
- return { digitalSignature: false, keyEncipherment: false };
266
+ const unusedBits = content[0];
267
+ if (unusedBits === undefined) {
268
+ throw new TlsMalformedExtensionError(KEY_USAGE_OID, "BIT STRING without an unused-bits byte");
263
269
  }
264
- // Pack the used bits into an integer (big-endian bit order).
265
- const usedBytes = content.subarray(1); // skip the unused-bits count.
266
- let bits = 0;
270
+ if (unusedBits > 7 || (content.length === 1 && unusedBits !== 0)) {
271
+ throw new TlsMalformedExtensionError(KEY_USAGE_OID, "BIT STRING unused-bits byte must be 0..7 (and 0 when there is no data byte)");
272
+ }
273
+ const usedBytes = content.subarray(1);
274
+ let allZero = true;
267
275
  for (const byte of usedBytes) {
268
- bits = (bits << 8) | byte;
276
+ if (byte !== 0) {
277
+ allZero = false;
278
+ break;
279
+ }
280
+ }
281
+ if (allZero) {
282
+ // RFC 5280 §4.2.1.3 — "at least one of the bits MUST be set to 1".
283
+ throw new TlsMalformedExtensionError(KEY_USAGE_OID, "BIT STRING is all zero — RFC 5280 §4.2.1.3 requires at least one asserted bit (BoringSSL ParseKeyUsage fails the certificate)");
269
284
  }
270
285
  // The bits are left-aligned; the real bit 0 is the most significant bit of
271
286
  // the first used byte. digitalSignature = bit 0, keyEncipherment = bit 2.
272
- // bitIndex therefore maps directly to a big-endian bit position (0 = MSB of
273
- // the first byte), so no reversal is applied.
274
287
  const getBit = (index) => {
275
288
  const byteIndex = Math.floor(index / 8);
276
289
  const bitInByte = index % 8;
@@ -286,19 +299,60 @@ export function parseKeyUsage(value) {
286
299
  };
287
300
  }
288
301
  /**
289
- * Parse the BasicConstraints extension value (unwrapped OCTET STRING holding a
290
- * SEQUENCE { cA BOOLEAN DEFAULT FALSE, pathLenConstraint INTEGER? }).
302
+ * Parse the BasicConstraints extension value (unwrapped OCTET STRING holding
303
+ * SEQUENCE { cA BOOLEAN DEFAULT FALSE, pathLenConstraint INTEGER? }) — the
304
+ * STRICT ParseBasicConstraints twin (pki/parse_certificate.cc:735): BoringSSL
305
+ * runs this for every basicConstraints extension, critical or not, and a
306
+ * fault rejects the certificate (kFailedParsingBasicConstraints):
307
+ *
308
+ * - the value must be exactly one SEQUENCE (nothing trailing);
309
+ * - the cA BOOLEAN, when present, is der::ParseBool — one byte, 0x00 or
310
+ * 0xFF (X.690 §11.1); an explicitly-encoded FALSE is TOLERATED
311
+ * (BoringSSL's own TODO: "Should reject if CA was set to false… In
312
+ * practice however there are a lot of certificates that use the broken
313
+ * encoding");
314
+ * - the pathLenConstraint INTEGER, when present, must decode as uint8 —
315
+ * non-negative, minimally encoded, value ≤ 255 (der::ParseUint8; a larger
316
+ * constraint cannot appear in a certificate Chrome accepts);
317
+ * - nothing may follow inside the SEQUENCE.
318
+ *
319
+ * An empty SEQUENCE parses fine with cA = FALSE (the DEFAULT applies).
291
320
  */
292
321
  export function parseBasicConstraints(value) {
293
322
  const seq = readTlv(value, 0);
294
- if (seq.tag !== 0x30 || seq.valueStart >= seq.end) {
295
- return false;
323
+ if (seq.tag !== 0x30) {
324
+ throw new TlsMalformedExtensionError(BASIC_CONSTRAINTS_OID, "expected a single SEQUENCE");
325
+ }
326
+ if (seq.end !== value.length) {
327
+ throw new TlsMalformedExtensionError(BASIC_CONSTRAINTS_OID, "trailing data after the SEQUENCE");
328
+ }
329
+ let o = seq.valueStart;
330
+ let isCa = false;
331
+ if (o < seq.end) {
332
+ const boolTlv = readTlv(value, o);
333
+ if (boolTlv.tag === 0x01) {
334
+ const content = value.subarray(boolTlv.valueStart, boolTlv.end);
335
+ // der::ParseBool: exactly one byte; FALSE is 0x00, DER TRUE is 0xFF.
336
+ const byte = content[0];
337
+ if (content.length !== 1 || (byte !== 0x00 && byte !== 0xff)) {
338
+ throw new TlsMalformedExtensionError(BASIC_CONSTRAINTS_OID, "cA BOOLEAN must be exactly one byte, 0x00 or 0xFF (der::ParseBool)");
339
+ }
340
+ isCa = byte === 0xff;
341
+ o = boolTlv.end;
342
+ }
343
+ }
344
+ let pathLenConstraint = null;
345
+ if (o < seq.end) {
346
+ const intTlv = readTlv(value, o);
347
+ if (intTlv.tag === 0x02) {
348
+ pathLenConstraint = parsePathLenConstraint(value, intTlv, BASIC_CONSTRAINTS_OID);
349
+ o = intTlv.end;
350
+ }
296
351
  }
297
- if (peekTag(value, seq.valueStart) !== 0x01) {
298
- return false;
352
+ if (o !== seq.end) {
353
+ // "There shouldn't be any unconsumed data in the extension."
354
+ throw new TlsMalformedExtensionError(BASIC_CONSTRAINTS_OID, "unexpected element after cA/pathLenConstraint");
299
355
  }
300
- const boolTlv = readTlv(value, seq.valueStart);
301
- const boolByte = value[boolTlv.valueStart];
302
- return boolByte !== undefined && boolByte !== 0;
356
+ return { isCa, pathLenConstraint };
303
357
  }
304
358
  //# sourceMappingURL=cert-extensions.js.map