micro509 0.9.0 → 0.10.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 (107) hide show
  1. package/dist/index.d.ts +3 -3
  2. package/dist/index.js +15 -15
  3. package/dist/internal/asn1/asn1.js +145 -186
  4. package/dist/internal/asn1/asn1.js.map +1 -0
  5. package/dist/internal/asn1/der.js +210 -227
  6. package/dist/internal/asn1/der.js.map +1 -0
  7. package/dist/internal/asn1/oids.js +297 -101
  8. package/dist/internal/asn1/oids.js.map +1 -0
  9. package/dist/internal/asn1/oids2.js +69 -0
  10. package/dist/internal/asn1/oids2.js.map +1 -0
  11. package/dist/internal/crypto/algorithm-names.js +37 -35
  12. package/dist/internal/crypto/algorithm-names.js.map +1 -0
  13. package/dist/internal/crypto/ecdsa.js +25 -28
  14. package/dist/internal/crypto/ecdsa.js.map +1 -0
  15. package/dist/internal/crypto/hash.js +56 -117
  16. package/dist/internal/crypto/hash.js.map +1 -0
  17. package/dist/internal/crypto/pbes2.js +122 -133
  18. package/dist/internal/crypto/pbes2.js.map +1 -0
  19. package/dist/internal/crypto/rsa-pss.js +118 -124
  20. package/dist/internal/crypto/rsa-pss.js.map +1 -0
  21. package/dist/internal/crypto/sig-verify.js +87 -99
  22. package/dist/internal/crypto/sig-verify.js.map +1 -0
  23. package/dist/internal/crypto/signing.d.ts +12 -8
  24. package/dist/internal/crypto/signing.js +53 -55
  25. package/dist/internal/crypto/signing.js.map +1 -0
  26. package/dist/internal/crypto/webcrypto.js +5 -3
  27. package/dist/internal/crypto/webcrypto.js.map +1 -0
  28. package/dist/internal/shared/base64.js +10 -9
  29. package/dist/internal/shared/base64.js.map +1 -0
  30. package/dist/internal/shared/dn.js +50 -57
  31. package/dist/internal/shared/dn.js.map +1 -0
  32. package/dist/internal/shared/ip.js +43 -53
  33. package/dist/internal/shared/ip.js.map +1 -0
  34. package/dist/internal/verify/name-constraints-engine.js +211 -239
  35. package/dist/internal/verify/name-constraints-engine.js.map +1 -0
  36. package/dist/internal/verify/policy-engine.js +228 -247
  37. package/dist/internal/verify/policy-engine.js.map +1 -0
  38. package/dist/internal/verify/verify-path.js +213 -228
  39. package/dist/internal/verify/verify-path.js.map +1 -0
  40. package/dist/internal/x509/extension-bits.js +62 -59
  41. package/dist/internal/x509/extension-bits.js.map +1 -0
  42. package/dist/internal/x509/extension-registry.js +133 -164
  43. package/dist/internal/x509/extension-registry.js.map +1 -0
  44. package/dist/internal/x509/name-fields.js +35 -41
  45. package/dist/internal/x509/name-fields.js.map +1 -0
  46. package/dist/keys/keys.d.ts +583 -547
  47. package/dist/keys/keys.js +519 -431
  48. package/dist/keys/keys.js.map +1 -0
  49. package/dist/keys.js +2 -2
  50. package/dist/pem/pem.d.ts +46 -46
  51. package/dist/pem/pem.js +62 -81
  52. package/dist/pem/pem.js.map +1 -0
  53. package/dist/pem.js +2 -2
  54. package/dist/pkcs/pfx.d.ts +96 -80
  55. package/dist/pkcs/pfx.js +191 -231
  56. package/dist/pkcs/pfx.js.map +1 -0
  57. package/dist/pkcs/pkcs12-mac.d.ts +20 -18
  58. package/dist/pkcs/pkcs12-mac.js +90 -111
  59. package/dist/pkcs/pkcs12-mac.js.map +1 -0
  60. package/dist/pkcs/pkcs7.d.ts +118 -69
  61. package/dist/pkcs/pkcs7.js +401 -443
  62. package/dist/pkcs/pkcs7.js.map +1 -0
  63. package/dist/pkcs.d.ts +2 -2
  64. package/dist/pkcs.js +4 -4
  65. package/dist/result/result.d.ts +37 -32
  66. package/dist/result/result.js +46 -44
  67. package/dist/result/result.js.map +1 -0
  68. package/dist/result.js +2 -2
  69. package/dist/revocation/chain.d.ts +138 -119
  70. package/dist/revocation/chain.js +248 -281
  71. package/dist/revocation/chain.js.map +1 -0
  72. package/dist/revocation/crl.d.ts +125 -123
  73. package/dist/revocation/crl.js +653 -745
  74. package/dist/revocation/crl.js.map +1 -0
  75. package/dist/revocation/ocsp.d.ts +183 -177
  76. package/dist/revocation/ocsp.js +579 -636
  77. package/dist/revocation/ocsp.js.map +1 -0
  78. package/dist/revocation/revocation.d.ts +26 -26
  79. package/dist/revocation/revocation.js +104 -125
  80. package/dist/revocation/revocation.js.map +1 -0
  81. package/dist/revocation.js +5 -5
  82. package/dist/verify/identity.d.ts +42 -42
  83. package/dist/verify/identity.js +158 -167
  84. package/dist/verify/identity.js.map +1 -0
  85. package/dist/verify/name-constraints.d.ts +7 -7
  86. package/dist/verify/policy.d.ts +10 -10
  87. package/dist/verify/verify.d.ts +163 -163
  88. package/dist/verify/verify.js +396 -392
  89. package/dist/verify/verify.js.map +1 -0
  90. package/dist/verify.js +3 -3
  91. package/dist/x509/certificate.d.ts +117 -117
  92. package/dist/x509/certificate.js +51 -60
  93. package/dist/x509/certificate.js.map +1 -0
  94. package/dist/x509/csr.d.ts +25 -25
  95. package/dist/x509/csr.js +26 -30
  96. package/dist/x509/csr.js.map +1 -0
  97. package/dist/x509/extensions.d.ts +228 -184
  98. package/dist/x509/extensions.js +175 -200
  99. package/dist/x509/extensions.js.map +1 -0
  100. package/dist/x509/name.d.ts +94 -93
  101. package/dist/x509/name.js +30 -29
  102. package/dist/x509/name.js.map +1 -0
  103. package/dist/x509/parse.d.ts +185 -185
  104. package/dist/x509/parse.js +638 -676
  105. package/dist/x509/parse.js.map +1 -0
  106. package/dist/x509.js +6 -6
  107. package/package.json +79 -41
@@ -1,53 +1,52 @@
1
- import { readRootElement } from "../asn1/der.js";
2
- import { childrenOf, decodeObjectIdentifier, decodeString, hexToBytes, requireElement, toHex } from "../asn1/asn1.js";
3
- import { OIDS } from "../asn1/oids.js";
4
- import { allOnesMaskForIpAddress, decodeIpAddress, parseIpAddressToBytes } from "../shared/ip.js";
5
- import { nameFieldKeyFromOid } from "../x509/name-fields.js";
6
- import { compareDistinguishedNames, isWithinDirectoryNameSubtree } from "../shared/dn.js";
1
+ import { readRootElement as e } from "../asn1/der.js";
2
+ import { childrenOf as t, decodeObjectIdentifier as n, decodeString as r, hexToBytes as i, requireElement as a, toHex as o } from "../asn1/asn1.js";
3
+ import { OIDS as s } from "../asn1/oids2.js";
4
+ import { allOnesMaskForIpAddress as c, decodeIpAddress as l, parseIpAddressToBytes as u } from "../shared/ip.js";
5
+ import { nameFieldKeyFromOid as d } from "../x509/name-fields.js";
6
+ import { compareDistinguishedNames as f, isWithinDirectoryNameSubtree as p } from "../shared/dn.js";
7
7
  //#region src/internal/verify/name-constraints-engine.ts
8
8
  /**
9
9
  * Internal name-constraints evaluation engine.
10
10
  *
11
- * Accumulates and evaluates the shipped RFC 5280 §4.2.1.10 / §6.1
11
+ * Accumulates and evaluates the shipped RFC 5280
12
+ * [§4.2.1.10](https://datatracker.ietf.org/doc/html/rfc5280#section-4.2.1.10) / [§6.1](https://datatracker.ietf.org/doc/html/rfc5280#section-6.1)
12
13
  * name-constraint subset during certificate path validation.
13
14
  *
14
15
  * @module
15
16
  */
16
17
  /** Constructs a {@linkcode NameConstraintValidationFailure} with optional details. */
17
- function nameConstraintFailure(code, message, index, details) {
18
+ function nameConstraintFailure(e, t, n, r) {
18
19
  return {
19
- ok: false,
20
- code,
21
- message,
22
- index,
23
- ...details === void 0 ? {} : { details }
20
+ ok: !1,
21
+ code: e,
22
+ message: t,
23
+ index: n,
24
+ ...r === void 0 ? {} : { details: r }
24
25
  };
25
26
  }
26
27
  /** Strips undefined fields and returns `undefined` when all fields are empty. */
27
- function nameConstraintDetails(input) {
28
- const details = {
29
- ...input.subjectCommonName === void 0 ? {} : { subjectCommonName: input.subjectCommonName },
30
- ...input.actual === void 0 ? {} : { actual: input.actual }
28
+ function nameConstraintDetails(e) {
29
+ let t = {
30
+ ...e.subjectCommonName === void 0 ? {} : { subjectCommonName: e.subjectCommonName },
31
+ ...e.actual === void 0 ? {} : { actual: e.actual }
31
32
  };
32
- return Object.keys(details).length === 0 ? void 0 : details;
33
+ return Object.keys(t).length === 0 ? void 0 : t;
33
34
  }
34
35
  /** A certificate is self-issued when subject and issuer DNs are semantically equal (RFC 5280 §7.1). */
35
- function isSelfIssued(certificate) {
36
- return compareDistinguishedNames(certificate.subject, certificate.issuer);
36
+ function isSelfIssued(e) {
37
+ return f(e.subject, e.issuer);
37
38
  }
38
39
  /**
39
40
  * Initializes validation state from caller-supplied initial constraints.
40
41
  *
41
42
  * Call once before {@linkcode evaluateNameConstraints}.
42
43
  */
43
- function createNameConstraintValidationState(input) {
44
+ function createNameConstraintValidationState(e) {
44
45
  return {
45
- initialPermittedSubtrees: input.permittedSubtrees?.map((subtree) => subtree.base) ?? [],
46
- initialExcludedSubtrees: input.excludedSubtrees?.map((subtree) => subtree.base) ?? []
46
+ initialPermittedSubtrees: e.permittedSubtrees?.map((e) => e.base) ?? [],
47
+ initialExcludedSubtrees: e.excludedSubtrees?.map((e) => e.base) ?? []
47
48
  };
48
49
  }
49
- /** Empty SEQUENCE DER hex — represents an empty subject DN. */
50
- const EMPTY_SEQUENCE_HEX = "3000";
51
50
  /**
52
51
  * Walks the chain root-to-leaf, accumulating nameConstraints from CA
53
52
  * certificates and checking each non-self-issued certificate's names
@@ -55,30 +54,29 @@ const EMPTY_SEQUENCE_HEX = "3000";
55
54
  *
56
55
  * RFC 5280 §6.1.3(b)–(c) for intermediates, §6.1.5(g) for the leaf.
57
56
  */
58
- function evaluateNameConstraints(chain, state) {
59
- let accumulated = seedInitialNameConstraints(state);
60
- const root = chain[chain.length - 1];
61
- if (root?.nameConstraints !== void 0) accumulated = accumulateConstraints(accumulated, root.nameConstraints, hasCriticalNameConstraintsExtension(root));
62
- for (let index = chain.length - 2; index >= 0; index -= 1) {
63
- const current = chain[index];
64
- if (current === void 0) throw new Error(`Certificate chain contains undefined at index ${String(index)}`);
65
- if (!isSelfIssued(current) || index === 0) {
66
- const nameCheckResult = checkCertificateNames(current, accumulated, index);
67
- if (!nameCheckResult.ok) return nameCheckResult;
57
+ function evaluateNameConstraints(e, t) {
58
+ let n = seedInitialNameConstraints(t), r = e[e.length - 1];
59
+ r?.nameConstraints !== void 0 && (n = accumulateConstraints(n, r.nameConstraints, hasCriticalNameConstraintsExtension(r)));
60
+ for (let t = e.length - 2; t >= 0; --t) {
61
+ let r = e[t];
62
+ if (r === void 0) throw Error(`Certificate chain contains undefined at index ${String(t)}`);
63
+ if (!isSelfIssued(r) || t === 0) {
64
+ let e = checkCertificateNames(r, n, t);
65
+ if (!e.ok) return e;
68
66
  }
69
- if (current.nameConstraints !== void 0) accumulated = accumulateConstraints(accumulated, current.nameConstraints, hasCriticalNameConstraintsExtension(current));
67
+ r.nameConstraints !== void 0 && (n = accumulateConstraints(n, r.nameConstraints, hasCriticalNameConstraintsExtension(r)));
70
68
  }
71
- return { ok: true };
69
+ return { ok: !0 };
72
70
  }
73
71
  /** True when the certificate carries a critical nameConstraints extension. */
74
- function hasCriticalNameConstraintsExtension(certificate) {
75
- return certificate.extensions.some((entry) => entry.oid === OIDS.nameConstraints && entry.critical);
72
+ function hasCriticalNameConstraintsExtension(e) {
73
+ return e.extensions.some((e) => e.oid === s.nameConstraints && e.critical);
76
74
  }
77
75
  /** Converts initial state into the starting accumulated-constraints snapshot. */
78
- function seedInitialNameConstraints(state) {
76
+ function seedInitialNameConstraints(e) {
79
77
  return {
80
- permittedLevels: state.initialPermittedSubtrees.length > 0 ? [state.initialPermittedSubtrees] : [],
81
- excluded: state.initialExcludedSubtrees,
78
+ permittedLevels: e.initialPermittedSubtrees.length > 0 ? [e.initialPermittedSubtrees] : [],
79
+ excluded: e.initialExcludedSubtrees,
82
80
  unsupportedCriticalForms: /* @__PURE__ */ new Set()
83
81
  };
84
82
  }
@@ -91,84 +89,92 @@ function seedInitialNameConstraints(state) {
91
89
  * dropped — the RFC's rejection requirement is scoped to critical
92
90
  * extensions (conforming CAs MUST mark nameConstraints critical anyway).
93
91
  */
94
- function accumulateConstraints(current, constraints, critical) {
95
- const permittedLevels = constraints.permittedSubtrees !== void 0 && constraints.permittedSubtrees.length > 0 ? [...current.permittedLevels, constraints.permittedSubtrees.flatMap((subtree) => isSupportedNameConstraintForm(subtree.base) ? [subtree.base] : [])] : current.permittedLevels;
96
- const excluded = constraints.excludedSubtrees !== void 0 && constraints.excludedSubtrees.length > 0 ? [...current.excluded, ...constraints.excludedSubtrees.flatMap((subtree) => isSupportedNameConstraintForm(subtree.base) ? [subtree.base] : [])] : current.excluded;
97
- const newUnsupported = critical ? listUnsupportedNameConstraintTypes(constraints) : [];
92
+ function accumulateConstraints(e, t, n) {
93
+ let r = t.permittedSubtrees !== void 0 && t.permittedSubtrees.length > 0 ? [...e.permittedLevels, t.permittedSubtrees.flatMap((e) => isSupportedNameConstraintForm(e.base) ? [e.base] : [])] : e.permittedLevels, i = t.excludedSubtrees !== void 0 && t.excludedSubtrees.length > 0 ? [...e.excluded, ...t.excludedSubtrees.flatMap((e) => isSupportedNameConstraintForm(e.base) ? [e.base] : [])] : e.excluded, a = n ? listUnsupportedNameConstraintTypes(t) : [];
98
94
  return {
99
- permittedLevels,
100
- excluded,
101
- unsupportedCriticalForms: newUnsupported.length > 0 ? /* @__PURE__ */ new Set([...current.unsupportedCriticalForms, ...newUnsupported]) : current.unsupportedCriticalForms
95
+ permittedLevels: r,
96
+ excluded: i,
97
+ unsupportedCriticalForms: a.length > 0 ? /* @__PURE__ */ new Set([...e.unsupportedCriticalForms, ...a]) : e.unsupportedCriticalForms
102
98
  };
103
99
  }
104
100
  /** Collects the distinct unsupported GeneralName form types from a nameConstraints extension. */
105
- function listUnsupportedNameConstraintTypes(constraints) {
106
- const unsupportedTypes = /* @__PURE__ */ new Set();
107
- for (const subtree of constraints.permittedSubtrees ?? []) if (!isSupportedNameConstraintForm(subtree.base)) unsupportedTypes.add(subtree.base.type);
108
- for (const subtree of constraints.excludedSubtrees ?? []) if (!isSupportedNameConstraintForm(subtree.base)) unsupportedTypes.add(subtree.base.type);
109
- return [...unsupportedTypes];
101
+ function listUnsupportedNameConstraintTypes(e) {
102
+ let t = /* @__PURE__ */ new Set();
103
+ for (let n of e.permittedSubtrees ?? []) isSupportedNameConstraintForm(n.base) || t.add(n.base.type);
104
+ for (let n of e.excludedSubtrees ?? []) isSupportedNameConstraintForm(n.base) || t.add(n.base.type);
105
+ return [...t];
110
106
  }
111
107
  /** True for name forms this engine can evaluate: dns, email, uri, ip, directoryName. */
112
- function isSupportedNameConstraintForm(form) {
113
- switch (form.type) {
108
+ function isSupportedNameConstraintForm(e) {
109
+ switch (e.type) {
114
110
  case "dns":
115
111
  case "email":
116
112
  case "uri":
117
113
  case "ip":
118
- case "directoryName": return true;
114
+ case "directoryName": return !0;
119
115
  case "otherName":
120
116
  case "x400Address":
121
117
  case "ediPartyName":
122
- case "registeredID": return false;
123
- default: throw new Error(`Unhandled NameConstraintForm type: ${String(form)}`);
118
+ case "registeredID": return !1;
119
+ default: throw Error(`Unhandled NameConstraintForm type: ${String(e)}`);
124
120
  }
125
121
  }
126
122
  /**
127
123
  * Checks a certificate's subject DN and SANs against accumulated
128
124
  * name constraints. Returns a failure if any name violates constraints.
129
125
  */
130
- function checkCertificateNames(certificate, accumulated, index) {
131
- if (certificate.subject.derHex !== EMPTY_SEQUENCE_HEX) {
132
- if (!isNamePermitted({
133
- type: "directoryName",
134
- derHex: certificate.subject.derHex
135
- }, accumulated)) return nameConstraintFailure("name_constraints_violated", "subject distinguished name violates name constraints", index, nameConstraintDetails({ subjectCommonName: certificate.subject.values.commonName }));
136
- }
137
- if (certificate.subjectAltNames !== void 0) for (const san of certificate.subjectAltNames) {
138
- const unsupportedForm = sanUnsupportedFormType(san);
139
- if (unsupportedForm !== void 0 && accumulated.unsupportedCriticalForms.has(unsupportedForm)) return nameConstraintFailure("unsupported_name_constraints", `critical name constraints impose ${unsupportedForm} constraints that cannot be processed, and the certificate contains a ${unsupportedForm} subject alternative name`, index, nameConstraintDetails({
140
- subjectCommonName: certificate.subject.values.commonName,
141
- actual: unsupportedForm
142
- }));
143
- const checkableResult = sanToConstraintCheckable(san);
144
- if (!checkableResult.ok) return nameConstraintFailure("name_constraints_violated", `SAN ${checkableResult.actual} is malformed and cannot be checked against name constraints`, index, nameConstraintDetails({
145
- subjectCommonName: certificate.subject.values.commonName,
146
- actual: checkableResult.actual
147
- }));
148
- const checkable = checkableResult.value;
149
- if (checkable === void 0) continue;
150
- if (!isNamePermitted(checkable, accumulated)) return nameConstraintFailure("name_constraints_violated", `SAN ${formatConstraintForm(checkable)} violates name constraints`, index, nameConstraintDetails({
151
- subjectCommonName: certificate.subject.values.commonName,
152
- actual: formatConstraintForm(checkable)
153
- }));
154
- }
155
- if (accumulatedHasEmailConstraints(accumulated)) {
156
- if (!(certificate.subjectAltNames?.some((san) => san.type === "email") ?? false) && certificate.subject.values.emailAddress !== void 0) {
157
- if (!isNamePermitted({
158
- type: "email",
159
- value: certificate.subject.values.emailAddress
160
- }, accumulated)) return nameConstraintFailure("name_constraints_violated", `subject emailAddress ${certificate.subject.values.emailAddress} violates name constraints`, index, nameConstraintDetails({
161
- subjectCommonName: certificate.subject.values.commonName,
162
- actual: certificate.subject.values.emailAddress
163
- }));
164
- }
126
+ function checkCertificateNames(e, t, n) {
127
+ let r = checkCertificateSubjectName(e, t, n);
128
+ if (!r.ok) return r;
129
+ let i = checkCertificateSubjectAltNames(e, t, n);
130
+ if (!i.ok) return i;
131
+ let a = checkCertificateSubjectEmailFallback(e, t, n);
132
+ return a.ok ? { ok: !0 } : a;
133
+ }
134
+ /** Checks a certificate subject DN against the accumulated directory-name constraints. */
135
+ function checkCertificateSubjectName(e, t, n) {
136
+ return e.subject.derHex === "3000" || isNamePermitted({
137
+ type: "directoryName",
138
+ derHex: e.subject.derHex
139
+ }, t) ? { ok: !0 } : nameConstraintFailure("name_constraints_violated", "subject distinguished name violates name constraints", n, nameConstraintDetails({ subjectCommonName: e.subject.values.commonName }));
140
+ }
141
+ function checkCertificateSubjectAltNames(e, t, n) {
142
+ for (let r of e.subjectAltNames ?? []) {
143
+ let i = checkCertificateSubjectAltName(e, t, r, n);
144
+ if (!i.ok) return i;
165
145
  }
166
- return { ok: true };
146
+ return { ok: !0 };
147
+ }
148
+ function checkCertificateSubjectAltName(e, t, n, r) {
149
+ let i = sanUnsupportedFormType(n);
150
+ if (i !== void 0 && t.unsupportedCriticalForms.has(i)) return nameConstraintFailure("unsupported_name_constraints", `critical name constraints impose ${i} constraints that cannot be processed, and the certificate contains a ${i} subject alternative name`, r, nameConstraintDetails({
151
+ subjectCommonName: e.subject.values.commonName,
152
+ actual: i
153
+ }));
154
+ let a = sanToConstraintCheckable(n);
155
+ if (!a.ok) return nameConstraintFailure("name_constraints_violated", `SAN ${a.actual} is malformed and cannot be checked against name constraints`, r, nameConstraintDetails({
156
+ subjectCommonName: e.subject.values.commonName,
157
+ actual: a.actual
158
+ }));
159
+ let o = a.value;
160
+ return o === void 0 || isNamePermitted(o, t) ? { ok: !0 } : nameConstraintFailure("name_constraints_violated", `SAN ${formatConstraintForm(o)} violates name constraints`, r, nameConstraintDetails({
161
+ subjectCommonName: e.subject.values.commonName,
162
+ actual: formatConstraintForm(o)
163
+ }));
164
+ }
165
+ function checkCertificateSubjectEmailFallback(e, t, n) {
166
+ return accumulatedHasEmailConstraints(t) && !(e.subjectAltNames?.some((e) => e.type === "email") ?? !1) && e.subject.values.emailAddress !== void 0 && !isNamePermitted({
167
+ type: "email",
168
+ value: e.subject.values.emailAddress
169
+ }, t) ? nameConstraintFailure("name_constraints_violated", `subject emailAddress ${e.subject.values.emailAddress} violates name constraints`, n, nameConstraintDetails({
170
+ subjectCommonName: e.subject.values.commonName,
171
+ actual: e.subject.values.emailAddress
172
+ })) : { ok: !0 };
167
173
  }
168
174
  /** True when any level of accumulated constraints addresses the email name form. */
169
- function accumulatedHasEmailConstraints(accumulated) {
170
- for (const level of accumulated.permittedLevels) if (level.some((c) => c.type === "email")) return true;
171
- return accumulated.excluded.some((c) => c.type === "email");
175
+ function accumulatedHasEmailConstraints(e) {
176
+ for (let t of e.permittedLevels) if (t.some((e) => e.type === "email")) return !0;
177
+ return e.excluded.some((e) => e.type === "email");
172
178
  }
173
179
  /**
174
180
  * Maps a SAN to the unsupported constraint form it instantiates, if any.
@@ -177,10 +183,9 @@ function accumulatedHasEmailConstraints(accumulated) {
177
183
  * tag byte: 0xa0 otherName [0], 0xa3 x400Address [3], 0xa5 ediPartyName [5],
178
184
  * 0x88 registeredID [8].
179
185
  */
180
- function sanUnsupportedFormType(san) {
181
- if (san.type === "srv") return "otherName";
182
- if (san.type !== "unknown") return;
183
- switch (san.tag) {
186
+ function sanUnsupportedFormType(e) {
187
+ if (e.type === "srv") return "otherName";
188
+ if (e.type === "unknown") switch (e.tag) {
184
189
  case 160: return "otherName";
185
190
  case 163: return "x400Address";
186
191
  case 165: return "ediPartyName";
@@ -193,60 +198,60 @@ function sanUnsupportedFormType(san) {
193
198
  * Returns `undefined` for name forms that don't participate in
194
199
  * constraint checking (unknown tags).
195
200
  */
196
- function sanToConstraintCheckable(san) {
197
- switch (san.type) {
201
+ function sanToConstraintCheckable(e) {
202
+ switch (e.type) {
198
203
  case "dns": return {
199
- ok: true,
204
+ ok: !0,
200
205
  value: {
201
206
  type: "dns",
202
- value: san.value
207
+ value: e.value
203
208
  }
204
209
  };
205
210
  case "email": return {
206
- ok: true,
211
+ ok: !0,
207
212
  value: {
208
213
  type: "email",
209
- value: san.value
214
+ value: e.value
210
215
  }
211
216
  };
212
217
  case "uri": return {
213
- ok: true,
218
+ ok: !0,
214
219
  value: {
215
220
  type: "uri",
216
- value: san.value
221
+ value: e.value
217
222
  }
218
223
  };
219
224
  case "srv": return {
220
- ok: true,
225
+ ok: !0,
221
226
  value: void 0
222
227
  };
223
228
  case "ip": try {
224
229
  return {
225
- ok: true,
230
+ ok: !0,
226
231
  value: {
227
232
  type: "ip",
228
- addressBytes: parseIpAddressToBytes(san.value),
229
- maskBytes: allOnesMaskForIpAddress(san.value)
233
+ addressBytes: u(e.value),
234
+ maskBytes: c(e.value)
230
235
  }
231
236
  };
232
237
  } catch {
233
238
  return {
234
- ok: false,
235
- actual: `ip:${san.value}`
239
+ ok: !1,
240
+ actual: `ip:${e.value}`
236
241
  };
237
242
  }
238
243
  case "directoryName": return {
239
- ok: true,
244
+ ok: !0,
240
245
  value: {
241
246
  type: "directoryName",
242
- derHex: san.derHex
247
+ derHex: e.derHex
243
248
  }
244
249
  };
245
250
  case "unknown": return {
246
- ok: true,
251
+ ok: !0,
247
252
  value: void 0
248
253
  };
249
- default: throw new Error(`Unhandled SubjectAltName type: ${String(san)}`);
254
+ default: throw Error(`Unhandled SubjectAltName type: ${String(e)}`);
250
255
  }
251
256
  }
252
257
  /**
@@ -256,35 +261,26 @@ function sanToConstraintCheckable(san) {
256
261
  * 2. For every permitted level that contains constraints of the same
257
262
  * name form, it matches at least one.
258
263
  */
259
- function isNamePermitted(name, accumulated) {
260
- for (const constraint of accumulated.excluded) if (nameMatchesConstraint(name, constraint)) return false;
261
- for (const level of accumulated.permittedLevels) {
262
- const relevant = level.filter((constraint) => constraint.type === name.type);
263
- if (relevant.length === 0) continue;
264
- if (!relevant.some((constraint) => nameMatchesConstraint(name, constraint))) return false;
264
+ function isNamePermitted(e, t) {
265
+ for (let n of t.excluded) if (nameMatchesConstraint(e, n)) return !1;
266
+ for (let n of t.permittedLevels) {
267
+ let t = n.filter((t) => t.type === e.type);
268
+ if (t.length !== 0 && !t.some((t) => nameMatchesConstraint(e, t))) return !1;
265
269
  }
266
- return true;
270
+ return !0;
267
271
  }
268
272
  /** Dispatches to the type-specific matching function for the name form. */
269
- function nameMatchesConstraint(name, constraint) {
270
- if (name.type === "dns" && constraint.type === "dns") return matchesDnsConstraint(name.value, constraint.value);
271
- if (name.type === "email" && constraint.type === "email") return matchesEmailConstraint(name.value, constraint.value);
272
- if (name.type === "uri" && constraint.type === "uri") return matchesUriConstraint(name.value, constraint.value);
273
- if (name.type === "ip" && constraint.type === "ip") return matchesIpConstraint(name.addressBytes, constraint.addressBytes, constraint.maskBytes);
274
- if (name.type === "directoryName" && constraint.type === "directoryName") return matchesDnConstraint(name.derHex, constraint.derHex);
275
- return false;
273
+ function nameMatchesConstraint(e, t) {
274
+ return e.type === "dns" && t.type === "dns" ? matchesDnsConstraint(e.value, t.value) : e.type === "email" && t.type === "email" ? matchesEmailConstraint(e.value, t.value) : e.type === "uri" && t.type === "uri" ? matchesUriConstraint(e.value, t.value) : e.type === "ip" && t.type === "ip" ? matchesIpConstraint(e.addressBytes, t.addressBytes, t.maskBytes) : e.type === "directoryName" && t.type === "directoryName" && matchesDnConstraint(e.derHex, t.derHex);
276
275
  }
277
276
  /**
278
277
  * RFC 5280 §4.2.1.10: DNS name constraint matching.
279
278
  * Constraint "example.com" matches "example.com" and any subdomain.
280
279
  * Constraint ".example.com" matches only subdomains, not "example.com" itself.
281
280
  */
282
- function matchesDnsConstraint(name, constraint) {
283
- const lowerName = name.toLowerCase();
284
- const lowerConstraint = constraint.toLowerCase();
285
- if (lowerConstraint.length === 0) return true;
286
- if (lowerConstraint.startsWith(".")) return lowerName.endsWith(lowerConstraint);
287
- return lowerName === lowerConstraint || lowerName.endsWith(`.${lowerConstraint}`);
281
+ function matchesDnsConstraint(e, t) {
282
+ let n = e.toLowerCase(), r = t.toLowerCase();
283
+ return r.length === 0 ? !0 : r.startsWith(".") ? n.endsWith(r) : n === r || n.endsWith(`.${r}`);
288
284
  }
289
285
  /**
290
286
  * RFC 5280 §4.2.1.10: Email constraint matching.
@@ -292,15 +288,13 @@ function matchesDnsConstraint(name, constraint) {
292
288
  * - "example.com" matches any address @example.com.
293
289
  * - ".example.com" matches any address @subdomain.example.com.
294
290
  */
295
- function matchesEmailConstraint(name, constraint) {
296
- const lowerName = name.toLowerCase();
297
- const lowerConstraint = constraint.toLowerCase();
298
- if (lowerConstraint.includes("@")) return lowerName === lowerConstraint;
299
- const atIndex = lowerName.indexOf("@");
300
- if (atIndex < 0) return false;
301
- const host = lowerName.slice(atIndex + 1);
302
- if (lowerConstraint.startsWith(".")) return host.endsWith(lowerConstraint);
303
- return host === lowerConstraint;
291
+ function matchesEmailConstraint(e, t) {
292
+ let n = e.toLowerCase(), r = t.toLowerCase();
293
+ if (r.includes("@")) return n === r;
294
+ let i = n.indexOf("@");
295
+ if (i < 0) return !1;
296
+ let a = n.slice(i + 1);
297
+ return r.startsWith(".") ? a.endsWith(r) : a === r;
304
298
  }
305
299
  /**
306
300
  * RFC 5280 §4.2.1.10: URI constraint matching.
@@ -309,19 +303,16 @@ function matchesEmailConstraint(name, constraint) {
309
303
  * - Constraint "example.com" matches ONLY that exact host (no subdomain
310
304
  * expansion, unlike DNS constraints).
311
305
  */
312
- function matchesUriConstraint(uri, constraint) {
313
- const host = extractUriHost(uri);
314
- if (host === void 0) return false;
315
- const lowerHost = host.toLowerCase();
316
- const lowerConstraint = constraint.toLowerCase();
317
- if (lowerConstraint.length === 0) return true;
318
- if (lowerConstraint.startsWith(".")) return lowerHost.endsWith(lowerConstraint);
319
- return lowerHost === lowerConstraint;
306
+ function matchesUriConstraint(e, t) {
307
+ let n = extractUriHost(e);
308
+ if (n === void 0) return !1;
309
+ let r = n.toLowerCase(), i = t.toLowerCase();
310
+ return i.length === 0 ? !0 : i.startsWith(".") ? r.endsWith(i) : r === i;
320
311
  }
321
312
  /** Extracts the host (reg-name) portion of a URI, stripping scheme, userinfo, port, and path. */
322
- function extractUriHost(uri) {
313
+ function extractUriHost(e) {
323
314
  try {
324
- return new URL(uri).hostname;
315
+ return new URL(e).hostname;
325
316
  } catch {
326
317
  return;
327
318
  }
@@ -330,51 +321,40 @@ function extractUriHost(uri) {
330
321
  * RFC 5280 §4.2.1.10: IP constraint matching.
331
322
  * (nameiP & mask) == (constraintiP & mask)
332
323
  */
333
- function matchesIpConstraint(nameBytes, constraintAddr, constraintMask) {
334
- if (nameBytes.length !== constraintAddr.length) return false;
335
- for (let i = 0; i < nameBytes.length; i += 1) {
336
- const nameByte = nameBytes[i] ?? 0;
337
- const addrByte = constraintAddr[i] ?? 0;
338
- const maskByte = constraintMask[i] ?? 0;
339
- if ((nameByte & maskByte) !== (addrByte & maskByte)) return false;
324
+ function matchesIpConstraint(e, t, n) {
325
+ if (e.length !== t.length) return !1;
326
+ for (let r = 0; r < e.length; r += 1) {
327
+ let i = e[r] ?? 0, a = t[r] ?? 0, o = n[r] ?? 0;
328
+ if ((i & o) !== (a & o)) return !1;
340
329
  }
341
- return true;
330
+ return !0;
342
331
  }
343
332
  /**
344
333
  * RFC 5280 §4.2.1.10: DirectoryName constraint matching.
345
334
  * The subject DN must equal or be subordinate to the constraint DN,
346
335
  * using RFC 5280 section 7.1 name comparison semantics.
347
336
  */
348
- function matchesDnConstraint(subjectDerHex, constraintDerHex) {
349
- const subjectName = parseDirectoryNameDerHex(subjectDerHex);
350
- const constraintName = parseDirectoryNameDerHex(constraintDerHex);
351
- if (subjectName === void 0 || constraintName === void 0) return false;
352
- return isWithinDirectoryNameSubtree(subjectName, constraintName);
337
+ function matchesDnConstraint(e, t) {
338
+ let n = parseDirectoryNameDerHex(e), r = parseDirectoryNameDerHex(t);
339
+ return n === void 0 || r === void 0 ? !1 : p(n, r);
353
340
  }
354
341
  /** Re-parses a hex-encoded DER Name for RDN-by-RDN comparison. Returns `undefined` on malformed input. */
355
- function parseDirectoryNameDerHex(derHex) {
356
- if (!/^(?:[0-9a-fA-F]{2})+$/.test(derHex)) return;
357
- try {
358
- const bytes = hexToBytes(derHex);
359
- const element = unwrapDirectoryNameElement(bytes, readRootElement(bytes, { maxDepth: 64 }));
360
- if (element.tag !== 48) return;
361
- const rdns = [];
362
- const attributes = [];
363
- const values = {};
364
- for (const setElement of childrenOf(bytes, element)) {
365
- const rdn = parseDirectoryNameRdn(bytes, setElement);
366
- if (rdn === void 0) return;
367
- rdns.push(rdn);
368
- for (const attribute of rdn.attributes) {
369
- attributes.push(attribute);
370
- if (attribute.key !== void 0 && values[attribute.key] === void 0) values[attribute.key] = attribute.value;
371
- }
342
+ function parseDirectoryNameDerHex(n) {
343
+ if (/^(?:[0-9a-fA-F]{2})+$/.test(n)) try {
344
+ let r = i(n), a = unwrapDirectoryNameElement(r, e(r, { maxDepth: 64 }));
345
+ if (a.tag !== 48) return;
346
+ let s = [], c = [], l = {};
347
+ for (let e of t(r, a)) {
348
+ let t = parseDirectoryNameRdn(r, e);
349
+ if (t === void 0) return;
350
+ s.push(t);
351
+ for (let e of t.attributes) c.push(e), e.key !== void 0 && l[e.key] === void 0 && (l[e.key] = e.value);
372
352
  }
373
353
  return {
374
- derHex: toHex(bytes),
375
- rdns,
376
- attributes,
377
- values
354
+ derHex: o(r),
355
+ rdns: s,
356
+ attributes: c,
357
+ values: l
378
358
  };
379
359
  } catch {
380
360
  return;
@@ -386,65 +366,57 @@ function parseDirectoryNameDerHex(derHex) {
386
366
  * Some encoders produce `[4] { SEQ { SEQ { SET... } } }` instead of `[4] { SEQ { SET... } }`.
387
367
  * A single SEQUENCE child indicates the extra wrapper (valid Names have SET children).
388
368
  */
389
- function unwrapDirectoryNameElement(source, element) {
390
- if (element.tag !== 48) return element;
391
- const children = childrenOf(source, element);
392
- const child = children[0];
393
- if (children.length === 1 && child?.tag === 48) return child;
394
- return element;
369
+ function unwrapDirectoryNameElement(e, n) {
370
+ if (n.tag !== 48) return n;
371
+ let r = t(e, n), i = r[0];
372
+ return r.length === 1 && i?.tag === 48 ? i : n;
395
373
  }
396
374
  /** Parses one SET element (a single RDN) from the DER Name SEQUENCE. */
397
- function parseDirectoryNameRdn(source, setElement) {
398
- if (setElement.tag !== 49) return;
399
- const children = childrenOf(source, setElement);
400
- if (children.length === 0) return;
401
- const attributes = [];
402
- const values = {};
403
- for (const attributeSequence of children) {
404
- if (attributeSequence.tag !== 48) return;
405
- const parts = childrenOf(source, attributeSequence);
406
- const oidElement = parts[0];
407
- const valueElement = parts[1];
408
- if (oidElement === void 0 || valueElement === void 0 || parts.length !== 2) return;
409
- if (oidElement.tag !== 6) return;
410
- const oid = decodeObjectIdentifier(requireElement(oidElement, "directoryName OID").value);
411
- let fieldValue;
375
+ function parseDirectoryNameRdn(e, i) {
376
+ if (i.tag !== 49) return;
377
+ let s = t(e, i);
378
+ if (s.length === 0) return;
379
+ let c = [], l = {};
380
+ for (let i of s) {
381
+ if (i.tag !== 48) return;
382
+ let o = t(e, i), s = o[0], u = o[1];
383
+ if (s === void 0 || u === void 0 || o.length !== 2 || s.tag !== 6) return;
384
+ let f = n(a(s, "directoryName OID").value), p;
412
385
  try {
413
- fieldValue = decodeString(valueElement.tag, requireElement(valueElement, "directoryName value").value);
386
+ p = r(u.tag, a(u, "directoryName value").value);
414
387
  } catch {
415
388
  return;
416
389
  }
417
- const fieldKey = nameFieldKeyFromOid(oid);
418
- const attribute = fieldKey !== void 0 ? {
419
- oid,
420
- key: fieldKey,
421
- valueTag: valueElement.tag,
422
- value: fieldValue
390
+ let m = d(f), h = m === void 0 ? {
391
+ oid: f,
392
+ valueTag: u.tag,
393
+ value: p
423
394
  } : {
424
- oid,
425
- valueTag: valueElement.tag,
426
- value: fieldValue
395
+ oid: f,
396
+ key: m,
397
+ valueTag: u.tag,
398
+ value: p
427
399
  };
428
- attributes.push(attribute);
429
- if (fieldKey !== void 0 && values[fieldKey] === void 0) values[fieldKey] = fieldValue;
400
+ c.push(h), m !== void 0 && l[m] === void 0 && (l[m] = p);
430
401
  }
431
- if (attributes.length === 0) return;
432
- return {
433
- derHex: toHex(source.slice(setElement.start - setElement.headerLength, setElement.end)),
434
- attributes,
435
- values
402
+ if (c.length !== 0) return {
403
+ derHex: o(e.slice(i.start - i.headerLength, i.end)),
404
+ attributes: c,
405
+ values: l
436
406
  };
437
407
  }
438
408
  /** Human-readable label for a constraint form, used in error messages. */
439
- function formatConstraintForm(form) {
440
- switch (form.type) {
441
- case "dns": return `dns:${form.value}`;
442
- case "email": return `email:${form.value}`;
443
- case "uri": return `uri:${form.value}`;
444
- case "ip": return `ip:${decodeIpAddress(form.addressBytes)}`;
445
- case "directoryName": return `dn:${form.derHex.slice(0, 20)}${form.derHex.length > 20 ? "..." : ""}`;
446
- default: throw new Error(`Unhandled NameConstraintForm type: ${String(form)}`);
409
+ function formatConstraintForm(e) {
410
+ switch (e.type) {
411
+ case "dns": return `dns:${e.value}`;
412
+ case "email": return `email:${e.value}`;
413
+ case "uri": return `uri:${e.value}`;
414
+ case "ip": return `ip:${l(e.addressBytes)}`;
415
+ case "directoryName": return `dn:${e.derHex.slice(0, 20)}${e.derHex.length > 20 ? "..." : ""}`;
416
+ default: throw Error(`Unhandled NameConstraintForm type: ${String(e)}`);
447
417
  }
448
418
  }
449
419
  //#endregion
450
420
  export { createNameConstraintValidationState, evaluateNameConstraints };
421
+
422
+ //# sourceMappingURL=name-constraints-engine.js.map