micro509 0.14.0 → 0.15.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 (161) hide show
  1. package/CHANGELOG.md +2064 -0
  2. package/README.md +105 -225
  3. package/dist/crypto/crypto.d.ts +42 -11
  4. package/dist/crypto/crypto.js +53 -22
  5. package/dist/crypto/crypto.js.map +1 -1
  6. package/dist/crypto.d.ts +2 -2
  7. package/dist/crypto.js +1 -2
  8. package/dist/der/der.d.ts +41 -33
  9. package/dist/der/der.js +26 -24
  10. package/dist/der/der.js.map +1 -1
  11. package/dist/index.d.ts +10 -10
  12. package/dist/index.js +19 -19
  13. package/dist/internal/asn1/asn1.d.ts +3 -6
  14. package/dist/internal/asn1/asn1.js +343 -43
  15. package/dist/internal/asn1/asn1.js.map +1 -1
  16. package/dist/internal/asn1/ber.js +146 -0
  17. package/dist/internal/asn1/ber.js.map +1 -0
  18. package/dist/internal/asn1/decode-refusal.js +29 -0
  19. package/dist/internal/asn1/decode-refusal.js.map +1 -0
  20. package/dist/internal/asn1/der.d.ts +38 -41
  21. package/dist/internal/asn1/der.js +131 -52
  22. package/dist/internal/asn1/der.js.map +1 -1
  23. package/dist/internal/asn1/oids.js +83 -53
  24. package/dist/internal/asn1/oids.js.map +1 -1
  25. package/dist/internal/asn1/oids2.js +1 -2
  26. package/dist/internal/asn1/oids2.js.map +1 -1
  27. package/dist/internal/crypto/algorithm-names.js +1 -2
  28. package/dist/internal/crypto/algorithm-names.js.map +1 -1
  29. package/dist/internal/crypto/ecdsa.js +13 -8
  30. package/dist/internal/crypto/ecdsa.js.map +1 -1
  31. package/dist/internal/crypto/hash.js +1 -2
  32. package/dist/internal/crypto/hash.js.map +1 -1
  33. package/dist/internal/crypto/pbes2.d.ts +24 -11
  34. package/dist/internal/crypto/pbes2.js +79 -26
  35. package/dist/internal/crypto/pbes2.js.map +1 -1
  36. package/dist/internal/crypto/rsa-pss.js +46 -44
  37. package/dist/internal/crypto/rsa-pss.js.map +1 -1
  38. package/dist/internal/crypto/sig-verify.d.ts +4 -7
  39. package/dist/internal/crypto/sig-verify.js +107 -60
  40. package/dist/internal/crypto/sig-verify.js.map +1 -1
  41. package/dist/internal/crypto/signing.d.ts +1 -4
  42. package/dist/internal/crypto/signing.js +28 -29
  43. package/dist/internal/crypto/signing.js.map +1 -1
  44. package/dist/internal/crypto/webcrypto.js +1 -2
  45. package/dist/internal/crypto/webcrypto.js.map +1 -1
  46. package/dist/internal/shared/base64.js +1 -2
  47. package/dist/internal/shared/base64.js.map +1 -1
  48. package/dist/internal/shared/dn.d.ts +3 -6
  49. package/dist/internal/shared/dn.js +99 -53
  50. package/dist/internal/shared/dn.js.map +1 -1
  51. package/dist/internal/shared/idna-tables.js +7217 -0
  52. package/dist/internal/shared/idna-tables.js.map +1 -0
  53. package/dist/internal/shared/idna.js +296 -0
  54. package/dist/internal/shared/idna.js.map +1 -0
  55. package/dist/internal/shared/ip.d.ts +4 -7
  56. package/dist/internal/shared/ip.js +1 -2
  57. package/dist/internal/shared/ip.js.map +1 -1
  58. package/dist/internal/shared/mailbox.js +26 -0
  59. package/dist/internal/shared/mailbox.js.map +1 -0
  60. package/dist/internal/shared/punycode.js +101 -0
  61. package/dist/internal/shared/punycode.js.map +1 -0
  62. package/dist/internal/shared/rfc3454-tables.js +1 -2
  63. package/dist/internal/shared/rfc3454-tables.js.map +1 -1
  64. package/dist/internal/shared/rfc4518-tables.js +119 -0
  65. package/dist/internal/shared/rfc4518-tables.js.map +1 -0
  66. package/dist/internal/shared/uri-host.js +102 -0
  67. package/dist/internal/shared/uri-host.js.map +1 -0
  68. package/dist/internal/verify/name-constraints-engine.js +246 -131
  69. package/dist/internal/verify/name-constraints-engine.js.map +1 -1
  70. package/dist/internal/verify/policy-engine.js +6 -21
  71. package/dist/internal/verify/policy-engine.js.map +1 -1
  72. package/dist/internal/verify/verify-path.d.ts +1 -4
  73. package/dist/internal/verify/verify-path.js +267 -131
  74. package/dist/internal/verify/verify-path.js.map +1 -1
  75. package/dist/internal/x509/directory-name.js +1 -2
  76. package/dist/internal/x509/directory-name.js.map +1 -1
  77. package/dist/internal/x509/extension-bits.js +2 -3
  78. package/dist/internal/x509/extension-bits.js.map +1 -1
  79. package/dist/internal/x509/extension-errors.d.ts +1 -4
  80. package/dist/internal/x509/extension-errors.js +1 -2
  81. package/dist/internal/x509/extension-errors.js.map +1 -1
  82. package/dist/internal/x509/extension-registry.js +107 -89
  83. package/dist/internal/x509/extension-registry.js.map +1 -1
  84. package/dist/internal/x509/general-name-profile.js +129 -0
  85. package/dist/internal/x509/general-name-profile.js.map +1 -0
  86. package/dist/internal/x509/general-name-tags.js +1 -2
  87. package/dist/internal/x509/general-name-tags.js.map +1 -1
  88. package/dist/internal/x509/general-name.js +128 -42
  89. package/dist/internal/x509/general-name.js.map +1 -1
  90. package/dist/internal/x509/name-fields.js +1 -2
  91. package/dist/internal/x509/name-fields.js.map +1 -1
  92. package/dist/internal/x509/or-address.js +217 -0
  93. package/dist/internal/x509/or-address.js.map +1 -0
  94. package/dist/keys/keys.d.ts +112 -97
  95. package/dist/keys/keys.js +211 -204
  96. package/dist/keys/keys.js.map +1 -1
  97. package/dist/keys.d.ts +3 -3
  98. package/dist/pem/pem.d.ts +14 -17
  99. package/dist/pem/pem.js +16 -17
  100. package/dist/pem/pem.js.map +1 -1
  101. package/dist/pem.js +1 -2
  102. package/dist/pkcs/pfx.d.ts +57 -32
  103. package/dist/pkcs/pfx.js +209 -173
  104. package/dist/pkcs/pfx.js.map +1 -1
  105. package/dist/pkcs/pkcs12-mac.d.ts +104 -27
  106. package/dist/pkcs/pkcs12-mac.js +266 -79
  107. package/dist/pkcs/pkcs12-mac.js.map +1 -1
  108. package/dist/pkcs/pkcs7.d.ts +43 -37
  109. package/dist/pkcs/pkcs7.js +218 -217
  110. package/dist/pkcs/pkcs7.js.map +1 -1
  111. package/dist/pkcs.d.ts +4 -3
  112. package/dist/result/result.d.ts +27 -20
  113. package/dist/result/result.js +8 -3
  114. package/dist/result/result.js.map +1 -1
  115. package/dist/result.d.ts +2 -2
  116. package/dist/result.js +1 -2
  117. package/dist/revocation/chain.d.ts +101 -23
  118. package/dist/revocation/chain.js +436 -182
  119. package/dist/revocation/chain.js.map +1 -1
  120. package/dist/revocation/crl.d.ts +131 -67
  121. package/dist/revocation/crl.js +444 -323
  122. package/dist/revocation/crl.js.map +1 -1
  123. package/dist/revocation/ocsp.d.ts +94 -59
  124. package/dist/revocation/ocsp.js +331 -293
  125. package/dist/revocation/ocsp.js.map +1 -1
  126. package/dist/revocation/revocation.d.ts +47 -35
  127. package/dist/revocation/revocation.js +88 -57
  128. package/dist/revocation/revocation.js.map +1 -1
  129. package/dist/revocation.d.ts +3 -3
  130. package/dist/verify/identity.d.ts +16 -19
  131. package/dist/verify/identity.js +118 -89
  132. package/dist/verify/identity.js.map +1 -1
  133. package/dist/verify/name-constraints.d.ts +1 -4
  134. package/dist/verify/policy.d.ts +9 -7
  135. package/dist/verify/verify.d.ts +84 -46
  136. package/dist/verify/verify.js +300 -152
  137. package/dist/verify/verify.js.map +1 -1
  138. package/dist/verify.d.ts +2 -2
  139. package/dist/x509/certificate.d.ts +20 -21
  140. package/dist/x509/certificate.js +62 -46
  141. package/dist/x509/certificate.js.map +1 -1
  142. package/dist/x509/csr.d.ts +5 -7
  143. package/dist/x509/csr.js +10 -11
  144. package/dist/x509/csr.js.map +1 -1
  145. package/dist/x509/extensions.d.ts +154 -66
  146. package/dist/x509/extensions.js +317 -151
  147. package/dist/x509/extensions.js.map +1 -1
  148. package/dist/x509/fingerprint.d.ts +4 -7
  149. package/dist/x509/fingerprint.js +1 -2
  150. package/dist/x509/fingerprint.js.map +1 -1
  151. package/dist/x509/name-text.d.ts +7 -9
  152. package/dist/x509/name-text.js +18 -4
  153. package/dist/x509/name-text.js.map +1 -1
  154. package/dist/x509/name.d.ts +9 -11
  155. package/dist/x509/name.js +18 -19
  156. package/dist/x509/name.js.map +1 -1
  157. package/dist/x509/parse.d.ts +84 -69
  158. package/dist/x509/parse.js +421 -400
  159. package/dist/x509/parse.js.map +1 -1
  160. package/dist/x509.d.ts +2 -2
  161. package/package.json +76 -59
@@ -1,10 +1,16 @@
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
- //#region src/internal/verify/name-constraints-engine.ts
1
+ import { DECODE_REFUSAL_CODES as e } from "../../result/result.js";
2
+ import { decodeRefusalOf as t, rethrowDecodeRefusal as n } from "../asn1/decode-refusal.js";
3
+ import { readRootElement as r } from "../asn1/der.js";
4
+ import { childrenOf as i, decodeObjectIdentifier as a, decodeString as o, hexToBytes as s, requireElement as c, toHex as l } from "../asn1/asn1.js";
5
+ import { OIDS as u } from "../asn1/oids2.js";
6
+ import { domainToAscii as d } from "../shared/idna.js";
7
+ import { allOnesMaskForIpAddress as f, decodeIpAddress as p, parseIpAddressToBytes as m } from "../shared/ip.js";
8
+ import { isMailboxDomain as h, presentedSmtpUtf8MailboxDomain as g } from "../shared/mailbox.js";
9
+ import { uriAuthorityHost as _ } from "../shared/uri-host.js";
10
+ import { parsePresentedSrvName as v, parseSrvNameRestriction as y, parseUriNameConstraint as b } from "../x509/general-name-profile.js";
11
+ import { nameFieldKeyFromOid as x } from "../x509/name-fields.js";
12
+ import { compareDistinguishedNames as S, directoryNameSubtreeMatch as C } from "../shared/dn.js";
13
+ // src/internal/verify/name-constraints-engine.ts
8
14
  /**
9
15
  * Internal name-constraints evaluation engine.
10
16
  *
@@ -34,7 +40,7 @@ function nameConstraintDetails(e) {
34
40
  }
35
41
  /** A certificate is self-issued when subject and issuer DNs are semantically equal (RFC 5280 §7.1). */
36
42
  function isSelfIssued(e) {
37
- return f(e.subject, e.issuer);
43
+ return S(e.subject, e.issuer);
38
44
  }
39
45
  /**
40
46
  * Initializes validation state from caller-supplied initial constraints.
@@ -70,14 +76,15 @@ function evaluateNameConstraints(e, t) {
70
76
  }
71
77
  /** True when the certificate carries a critical nameConstraints extension. */
72
78
  function hasCriticalNameConstraintsExtension(e) {
73
- return e.extensions.some((e) => e.oid === s.nameConstraints && e.critical);
79
+ return e.extensions.some((e) => e.oid === u.nameConstraints && e.critical);
74
80
  }
75
81
  /** Converts initial state into the starting accumulated-constraints snapshot. */
76
82
  function seedInitialNameConstraints(e) {
77
83
  return {
78
84
  permittedLevels: e.initialPermittedSubtrees.length > 0 ? [e.initialPermittedSubtrees] : [],
79
85
  excluded: e.initialExcludedSubtrees,
80
- unsupportedCriticalForms: /* @__PURE__ */ new Set()
86
+ unsupportedCriticalForms: /* @__PURE__ */ new Set(),
87
+ unsupportedCriticalOtherNames: /* @__PURE__ */ new Set()
81
88
  };
82
89
  }
83
90
  /**
@@ -90,28 +97,27 @@ function seedInitialNameConstraints(e) {
90
97
  * extensions (conforming CAs MUST mark nameConstraints critical anyway).
91
98
  */
92
99
  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) : [];
100
+ 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 ? listUnsupportedNameConstraintForms(t) : [];
94
101
  return {
95
102
  permittedLevels: r,
96
103
  excluded: i,
97
- unsupportedCriticalForms: a.length > 0 ? /* @__PURE__ */ new Set([...e.unsupportedCriticalForms, ...a]) : e.unsupportedCriticalForms
104
+ unsupportedCriticalForms: /* @__PURE__ */ new Set([...e.unsupportedCriticalForms, ...a.flatMap((e) => e.type === "otherName" ? [] : [e.type])]),
105
+ unsupportedCriticalOtherNames: /* @__PURE__ */ new Set([...e.unsupportedCriticalOtherNames, ...a.flatMap((e) => e.type === "otherName" ? [e.typeId] : [])])
98
106
  };
99
107
  }
100
- /** Collects the distinct unsupported GeneralName form types from a nameConstraints extension. */
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];
108
+ /** Collects the unsupported GeneralName forms from a nameConstraints extension. */
109
+ function listUnsupportedNameConstraintForms(e) {
110
+ return [...e.permittedSubtrees ?? [], ...e.excludedSubtrees ?? []].flatMap((e) => isSupportedNameConstraintForm(e.base) ? [] : [e.base]);
106
111
  }
107
- /** True for name forms this engine can evaluate: dns, email, uri, ip, directoryName. */
112
+ /** True for name forms this engine can evaluate: dns, email, uri, ip, directoryName, srv. */
108
113
  function isSupportedNameConstraintForm(e) {
109
114
  switch (e.type) {
110
115
  case "dns":
111
116
  case "email":
112
117
  case "uri":
113
118
  case "ip":
114
- case "directoryName": return !0;
119
+ case "directoryName":
120
+ case "srv": return !0;
115
121
  case "otherName":
116
122
  case "x400Address":
117
123
  case "ediPartyName":
@@ -123,13 +129,20 @@ function isSupportedNameConstraintForm(e) {
123
129
  * Checks a certificate's subject DN and SANs against accumulated
124
130
  * name constraints. Returns a failure if any name violates constraints.
125
131
  */
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;
132
+ function checkCertificateNames(n, r, i) {
133
+ try {
134
+ let e = checkCertificateSubjectName(n, r, i);
135
+ if (!e.ok) return e;
136
+ let t = checkCertificateSubjectAltNames(n, r, i);
137
+ return t.ok ? checkCertificateSubjectEmailFallback(n, r, i) : t;
138
+ } catch (r) {
139
+ let a = t(r, e);
140
+ if (a === void 0) throw r;
141
+ return nameConstraintFailure(a.code, a.message, i, nameConstraintDetails({
142
+ subjectCommonName: n.subject.values.commonName,
143
+ actual: a.message
144
+ }));
145
+ }
133
146
  }
134
147
  /** Checks a certificate subject DN against the accumulated directory-name constraints. */
135
148
  function checkCertificateSubjectName(e, t, n) {
@@ -146,18 +159,22 @@ function checkCertificateSubjectAltNames(e, t, n) {
146
159
  return { ok: !0 };
147
160
  }
148
161
  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({
162
+ let i = unprocessableConstraintFormOf(n, t);
163
+ if (i !== void 0) 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
164
  subjectCommonName: e.subject.values.commonName,
152
165
  actual: i
153
166
  }));
167
+ if (n.type === "smtpUtf8Mailbox") return checkSmtpUtf8Mailbox(e, t, n.value, r);
154
168
  let a = sanToConstraintCheckable(n);
155
169
  if (!a.ok) return nameConstraintFailure("name_constraints_violated", `SAN ${a.actual} is malformed and cannot be checked against name constraints`, r, nameConstraintDetails({
156
170
  subjectCommonName: e.subject.values.commonName,
157
171
  actual: a.actual
158
172
  }));
159
173
  let o = a.value;
160
- return o?.type === "uri" && accumulatedHasUriConstraints(t) && uriAuthorityLacksFqdn(o.value) ? nameConstraintFailure("name_constraints_violated", `SAN uri:${o.value} has no FQDN authority and cannot be evaluated against URI name constraints`, r, nameConstraintDetails({
174
+ return o?.type === "srv" && accumulatedHasConstraintsOfType("srv", t) && v(asciiLowercase(o.value)) === void 0 ? nameConstraintFailure("name_constraints_violated", `SAN srv:${o.value} is not _Service.Name and cannot be evaluated against SRVName constraints`, r, nameConstraintDetails({
175
+ subjectCommonName: e.subject.values.commonName,
176
+ actual: `srv:${o.value}`
177
+ })) : o?.type === "uri" && accumulatedHasConstraintsOfType("uri", t) && uriConstraintHost(o.value) === void 0 ? nameConstraintFailure("name_constraints_violated", `SAN uri:${o.value} has no FQDN authority host and cannot be evaluated against URI name constraints`, r, nameConstraintDetails({
161
178
  subjectCommonName: e.subject.values.commonName,
162
179
  actual: `uri:${o.value}`
163
180
  })) : o === void 0 || isNamePermitted(o, t) ? { ok: !0 } : nameConstraintFailure("name_constraints_violated", `SAN ${formatConstraintForm(o)} violates name constraints`, r, nameConstraintDetails({
@@ -165,6 +182,67 @@ function checkCertificateSubjectAltName(e, t, n, r) {
165
182
  actual: formatConstraintForm(o)
166
183
  }));
167
184
  }
185
+ /**
186
+ * RFC 9598 §6 applies rfc822Name constraints to a SmtpUTF8Mailbox by its domain
187
+ * alone: the Local-part and "@" are stripped from the name and the constraint,
188
+ * and the remaining domains compare octet for octet after lowercasing, as a
189
+ * suffix when the constraint starts with ".". A mailbox that breaks the §3
190
+ * syntax cannot be compared, so it fails whenever rfc822Name constraints are
191
+ * in force.
192
+ */
193
+ function checkSmtpUtf8Mailbox(e, t, n, r) {
194
+ return !accumulatedHasEmailConstraints(t) || isSmtpUtf8MailboxDomainPermitted(n, t) ? { ok: !0 } : nameConstraintFailure("name_constraints_violated", `SAN smtpUtf8Mailbox:${n} violates name constraints`, r, nameConstraintDetails({
195
+ subjectCommonName: e.subject.values.commonName,
196
+ actual: `smtpUtf8Mailbox:${n}`
197
+ }));
198
+ }
199
+ /** RFC 9598 §5: a presented SmtpUTF8Mailbox whose domain the email constraints permit. */
200
+ function isSmtpUtf8MailboxDomainPermitted(e, t) {
201
+ let n = g(e);
202
+ return n !== void 0 && isMailboxDomainPermitted(n, t);
203
+ }
204
+ function isMailboxDomainPermitted(e, t) {
205
+ if (hasMalformedConstraint("email", t)) return !1;
206
+ let matches = (t) => t.type === "email" && matchesMailboxDomainConstraint(e, t.value);
207
+ return !t.excluded.some(matches) && t.permittedLevels.every((e) => !e.some((e) => e.type === "email") || e.some(matches));
208
+ }
209
+ /**
210
+ * A dNSName or rfc822Name constraint whose domain is not a well-formed name
211
+ * cannot be compared, so no name of its type is permitted while one is in force.
212
+ */
213
+ function hasMalformedConstraint(e, t) {
214
+ return [...t.excluded, ...t.permittedLevels.flat()].some((t) => t.type === e && !isWellFormedConstraint(t));
215
+ }
216
+ function isWellFormedConstraint(e) {
217
+ switch (e.type) {
218
+ case "dns": {
219
+ let t = asciiLowercase(e.value);
220
+ return t.length === 0 || isComparableDomain(t.startsWith(".") ? t.slice(1) : t);
221
+ }
222
+ case "email": return constraintMailboxDomain(e.value) !== void 0;
223
+ case "srv": return y(asciiLowercase(e.value)) !== void 0;
224
+ case "directoryName": return parseDirectoryNameDerHex(e.derHex) !== void 0;
225
+ case "uri": return b(e.value) !== void 0;
226
+ case "ip": return !0;
227
+ default: throw Error(`Unhandled NameConstraintForm type: ${String(e)}`);
228
+ }
229
+ }
230
+ /** Labels of 1 to 63 letters, digits, hyphens or underscores, with each `xn--` label an A-label. */
231
+ function isComparableDomain(e) {
232
+ return e.length <= 253 && e.split(".").every((e) => /^[a-z0-9_-]{1,63}$/.test(e)) && d(e, "lookup").ok;
233
+ }
234
+ /** RFC 9598 §6: the lowercased domain of an rfc822Name constraint, keeping a leading ".". */
235
+ function constraintMailboxDomain(e) {
236
+ let t = asciiLowercase(e.slice(e.lastIndexOf("@") + 1)), n = t.startsWith(".") ? t.slice(1) : t;
237
+ return h(n, "lookup") ? t : void 0;
238
+ }
239
+ function matchesMailboxDomainConstraint(e, t) {
240
+ let n = constraintMailboxDomain(t);
241
+ return n?.startsWith(".") === !0 ? e.endsWith(n) : e === n;
242
+ }
243
+ function asciiLowercase(e) {
244
+ return e.replace(/[A-Z]/g, (e) => e.toLowerCase());
245
+ }
168
246
  function checkCertificateSubjectEmailFallback(e, t, n) {
169
247
  return accumulatedHasEmailConstraints(t) && !(e.subjectAltNames?.some((e) => e.type === "email") ?? !1) && e.subject.values.emailAddress !== void 0 && !isNamePermitted({
170
248
  type: "email",
@@ -179,27 +257,45 @@ function accumulatedHasEmailConstraints(e) {
179
257
  for (let t of e.permittedLevels) if (t.some((e) => e.type === "email")) return !0;
180
258
  return e.excluded.some((e) => e.type === "email");
181
259
  }
182
- /** True when any level of accumulated constraints addresses the URI name form. */
183
- function accumulatedHasUriConstraints(e) {
184
- for (let t of e.permittedLevels) if (t.some((e) => e.type === "uri")) return !0;
185
- return e.excluded.some((e) => e.type === "uri");
260
+ /** True when any level of accumulated constraints addresses the given name form. */
261
+ function accumulatedHasConstraintsOfType(e, t) {
262
+ return [...t.excluded, ...t.permittedLevels.flat()].some((t) => t.type === e);
186
263
  }
187
264
  /**
188
- * Maps a SAN to the unsupported constraint form it instantiates, if any.
189
- *
190
- * SRV-ID SANs are otherName [0] instances. Unknown SANs carry the full DER
191
- * tag byte: 0xa0 otherName [0], 0xa3 x400Address [3], 0xa5 ediPartyName [5],
192
- * 0x88 registeredID [8].
265
+ * The unprocessable critical constraint form a SAN instantiates, if one is in
266
+ * force. An otherName is its own form per type-id. Raw `unknown` input carries
267
+ * the DER tag byte, and an unknown otherName meets every otherName type-id.
193
268
  */
194
- function sanUnsupportedFormType(e) {
195
- if (e.type === "srv") return "otherName";
196
- if (e.type === "unknown") switch (e.tag) {
197
- case 160: return "otherName";
198
- case 163: return "x400Address";
199
- case 165: return "ediPartyName";
200
- case 136: return "registeredID";
269
+ function unprocessableConstraintFormOf(e, t) {
270
+ let otherName = (e) => t.unsupportedCriticalOtherNames.has(e) ? `otherName ${e}` : void 0, direct = (e) => t.unsupportedCriticalForms.has(e) ? e : void 0;
271
+ switch (e.type) {
272
+ case "otherName": return otherName(e.typeId);
273
+ case "smtpUtf8Mailbox": return otherName(u.idOnSmtpUtf8Mailbox);
274
+ case "upn": return otherName(u.ntPrincipalName);
275
+ case "krb5PrincipalName": return otherName(u.idPkinitSan);
276
+ case "x400Address":
277
+ case "ediPartyName":
278
+ case "registeredID": return direct(e.type);
279
+ case "unknown": return unknownTagForm(e.tag, t);
280
+ default: return;
281
+ }
282
+ }
283
+ function unknownTagForm(e, t) {
284
+ let n;
285
+ switch (e) {
286
+ case 163:
287
+ n = "x400Address";
288
+ break;
289
+ case 165:
290
+ n = "ediPartyName";
291
+ break;
292
+ case 136:
293
+ n = "registeredID";
294
+ break;
295
+ case 160: return t.unsupportedCriticalOtherNames.size > 0 ? "otherName" : void 0;
201
296
  default: return;
202
297
  }
298
+ return t.unsupportedCriticalForms.has(n) ? n : void 0;
203
299
  }
204
300
  /**
205
301
  * Converts a SubjectAltName to a NameConstraintForm for checking.
@@ -230,6 +326,19 @@ function sanToConstraintCheckable(e) {
230
326
  }
231
327
  };
232
328
  case "srv": return {
329
+ ok: !0,
330
+ value: {
331
+ type: "srv",
332
+ value: e.value
333
+ }
334
+ };
335
+ case "smtpUtf8Mailbox":
336
+ case "upn":
337
+ case "krb5PrincipalName":
338
+ case "otherName":
339
+ case "x400Address":
340
+ case "ediPartyName":
341
+ case "registeredID": return {
233
342
  ok: !0,
234
343
  value: void 0
235
344
  };
@@ -238,8 +347,8 @@ function sanToConstraintCheckable(e) {
238
347
  ok: !0,
239
348
  value: {
240
349
  type: "ip",
241
- addressBytes: u(e.value),
242
- maskBytes: c(e.value)
350
+ addressBytes: m(e.value),
351
+ maskBytes: f(e.value)
243
352
  }
244
353
  };
245
354
  } catch {
@@ -270,23 +379,42 @@ function sanToConstraintCheckable(e) {
270
379
  * name form, it matches at least one.
271
380
  */
272
381
  function isNamePermitted(e, t) {
273
- for (let n of t.excluded) if (nameMatchesConstraint(e, n)) return !1;
382
+ if (hasMalformedConstraint(e.type, t) || endsInRootDot(e) && accumulatedHasConstraintsOfType(e.type, t)) return !1;
383
+ for (let n of t.excluded) if (nameConstraintMatch(e, n) !== "mismatch") return !1;
274
384
  for (let n of t.permittedLevels) {
275
385
  let t = n.filter((t) => t.type === e.type);
276
- if (t.length !== 0 && !t.some((t) => nameMatchesConstraint(e, t))) return !1;
386
+ if (t.length !== 0 && !t.some((t) => nameConstraintMatch(e, t) === "match")) return !1;
277
387
  }
278
388
  return !0;
279
389
  }
390
+ /** RFC 5280 §4.2.1.6: a dNSName or rfc822Name host in RFC 1034 §3.5 preferred name syntax has no trailing root dot. */
391
+ function endsInRootDot(e) {
392
+ return (e.type === "dns" || e.type === "email") && e.value.endsWith(".");
393
+ }
394
+ /** RFC 4518 §2: a comparison that string preparation cannot perform is Undefined. */
395
+ function nameConstraintMatch(e, t) {
396
+ return e.type === "directoryName" && t.type === "directoryName" ? matchesDnConstraint(e.derHex, t.derHex) : nameMatchesConstraint(e, t) ? "match" : "mismatch";
397
+ }
280
398
  /** Dispatches to the type-specific matching function for the name form. */
281
399
  function nameMatchesConstraint(e, t) {
282
- 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);
400
+ 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 === "srv" && t.type === "srv" && matchesSrvConstraint(e.value, t.value);
401
+ }
402
+ /**
403
+ * RFC 4985 §4: a restriction's service, when present, must equal the SRVName's
404
+ * service case-insensitively (§2), and its Name, when present, is satisfied by
405
+ * that domain or any subdomain added to the left.
406
+ */
407
+ function matchesSrvConstraint(e, t) {
408
+ let n = v(asciiLowercase(e)), r = y(asciiLowercase(t));
409
+ return n !== void 0 && r !== void 0 && (r.service.length === 0 || n.service === r.service) && (r.name.length === 0 || n.name === r.name || n.name.endsWith(`.${r.name}`));
283
410
  }
284
411
  /**
285
412
  * DNS name constraint matching. RFC 5280 §4.2.1.10 adds zero or more labels on
286
413
  * the left; §7.2 compares label by label case-insensitively. Constraint
287
- * "example.com" matches "example.com" and any subdomain. The leading-period
288
- * form ".example.com" restricts to subdomains, following the convention shared
289
- * by OpenSSL, Go and NSS rather than the RFC.
414
+ * "example.com" matches "example.com" and any subdomain. RFC 5280 defines no
415
+ * leading-period dNSName form; ".example.com" matches subdomains only, as in
416
+ * OpenSSL, BoringSSL, Go, NSS, mozilla::pkix and rustls-webpki (RFC 5280
417
+ * erratum 5997).
290
418
  */
291
419
  function matchesDnsConstraint(e, t) {
292
420
  let n = e.toLowerCase(), r = t.toLowerCase();
@@ -311,43 +439,28 @@ function matchesEmailConstraint(e, t) {
311
439
  return i.startsWith(".") ? r.endsWith(i) : r === i;
312
440
  }
313
441
  /**
314
- * RFC 5280 §4.2.1.10: URI constraint matching.
315
- * Applied to the host part of the URI.
316
- * - Constraint ".example.com" matches subdomains only.
317
- * - Constraint "example.com" matches ONLY that exact host (no subdomain
318
- * expansion, unlike DNS constraints).
442
+ * RFC 5280 §4.2.1.10: a URI constraint applies to the host part, a constraint
443
+ * without a leading period names one host, and ".example.com" is satisfied by
444
+ * its subdomains only.
319
445
  */
320
446
  function matchesUriConstraint(e, t) {
321
- let n = extractUriHost(e);
322
- if (n === void 0) return !1;
323
- let r = n.toLowerCase(), i = t.toLowerCase();
324
- return i.length === 0 ? !0 : i.startsWith(".") ? r.endsWith(i) : r === i;
325
- }
326
- /** Extracts the host (reg-name) portion of a URI, stripping scheme, userinfo, port, and path. */
327
- function extractUriHost(e) {
328
- try {
329
- return new URL(e).hostname;
330
- } catch {
331
- return;
447
+ let n = uriConstraintHost(e), r = b(t);
448
+ if (n === void 0 || r === void 0) return !1;
449
+ switch (r.type) {
450
+ case "any": return !0;
451
+ case "host": return n === r.name;
452
+ case "subdomains": return n.endsWith(`.${r.name}`);
453
+ default: throw Error(`Unhandled UriNameConstraint type: ${String(r)}`);
332
454
  }
333
455
  }
334
456
  /**
335
- * RFC 5280 §4.2.1.10: a URI subject to a uniformResourceIdentifier constraint
336
- * MUST be rejected when its authority component has no FQDN host. True for a
337
- * missing or empty authority, a bracketed IPv6 literal, an IPv4 literal, or a
338
- * single-label host such as `localhost` (an FQDN has at least two labels).
457
+ * RFC 5280 §4.2.1.10: the domain name of a URI's authority host, which a URI
458
+ * constraint in force requires. `undefined` for no authority, an IP address, a
459
+ * reg-name that is not a domain name, or a single-label host.
339
460
  */
340
- function uriAuthorityLacksFqdn(e) {
341
- let t = extractUriHost(e);
342
- return t === void 0 || t.length === 0 || t.startsWith("[") || isIpLiteral(t) ? !0 : t.split(".").filter((e) => e.length > 0).length < 2;
343
- }
344
- /** True when `host` parses as an IPv4 or IPv6 literal. */
345
- function isIpLiteral(e) {
346
- try {
347
- return u(e), !0;
348
- } catch {
349
- return !1;
350
- }
461
+ function uriConstraintHost(e) {
462
+ let t = _(e, "presented");
463
+ return t.type === "dns" && t.name.includes(".") ? t.name : void 0;
351
464
  }
352
465
  /**
353
466
  * RFC 5280 §4.2.1.10: IP constraint matching.
@@ -368,27 +481,28 @@ function matchesIpConstraint(e, t, n) {
368
481
  */
369
482
  function matchesDnConstraint(e, t) {
370
483
  let n = parseDirectoryNameDerHex(e), r = parseDirectoryNameDerHex(t);
371
- return n === void 0 || r === void 0 ? !1 : p(n, r);
484
+ return n === void 0 || r === void 0 ? "mismatch" : C(n, r);
372
485
  }
373
486
  /** Re-parses a hex-encoded DER Name for RDN-by-RDN comparison. Returns `undefined` on malformed input. */
374
- function parseDirectoryNameDerHex(n) {
375
- if (/^(?:[0-9a-fA-F]{2})+$/.test(n)) try {
376
- let r = i(n), a = unwrapDirectoryNameElement(r, e(r, { maxDepth: 64 }));
377
- if (a.tag !== 48) return;
378
- let s = [], c = [], l = {};
379
- for (let e of t(r, a)) {
380
- let t = parseDirectoryNameRdn(r, e);
381
- if (t === void 0) return;
382
- s.push(t);
383
- for (let e of t.attributes) c.push(e), e.key !== void 0 && l[e.key] === void 0 && (l[e.key] = e.value);
487
+ function parseDirectoryNameDerHex(t) {
488
+ if (/^(?:[0-9a-fA-F]{2})+$/.test(t)) try {
489
+ let e = s(t), n = unwrapDirectoryNameElement(e, r(e, { maxDepth: 64 }));
490
+ if (n.tag !== 48) return;
491
+ let a = [], o = [], c = {};
492
+ for (let t of i(e, n)) {
493
+ let n = parseDirectoryNameRdn(e, t);
494
+ if (n === void 0) return;
495
+ a.push(n);
496
+ for (let e of n.attributes) o.push(e), e.key !== void 0 && c[e.key] === void 0 && (c[e.key] = e.value);
384
497
  }
385
498
  return {
386
- derHex: o(r),
387
- rdns: s,
388
- attributes: c,
389
- values: l
499
+ derHex: l(e),
500
+ rdns: a,
501
+ attributes: o,
502
+ values: c
390
503
  };
391
- } catch {
504
+ } catch (t) {
505
+ n(t, e);
392
506
  return;
393
507
  }
394
508
  }
@@ -398,43 +512,44 @@ function parseDirectoryNameDerHex(n) {
398
512
  * Some encoders produce `[4] { SEQ { SEQ { SET... } } }` instead of `[4] { SEQ { SET... } }`.
399
513
  * A single SEQUENCE child indicates the extra wrapper (valid Names have SET children).
400
514
  */
401
- function unwrapDirectoryNameElement(e, n) {
402
- if (n.tag !== 48) return n;
403
- let r = t(e, n), i = r[0];
404
- return r.length === 1 && i?.tag === 48 ? i : n;
515
+ function unwrapDirectoryNameElement(e, t) {
516
+ if (t.tag !== 48) return t;
517
+ let n = i(e, t), r = n[0];
518
+ return n.length === 1 && r?.tag === 48 ? r : t;
405
519
  }
406
520
  /** Parses one SET element (a single RDN) from the DER Name SEQUENCE. */
407
- function parseDirectoryNameRdn(e, i) {
408
- if (i.tag !== 49) return;
409
- let s = t(e, i);
521
+ function parseDirectoryNameRdn(t, r) {
522
+ if (r.tag !== 49) return;
523
+ let s = i(t, r);
410
524
  if (s.length === 0) return;
411
- let c = [], l = {};
412
- for (let i of s) {
413
- if (i.tag !== 48) return;
414
- let o = t(e, i), s = o[0], u = o[1];
415
- if (s === void 0 || u === void 0 || o.length !== 2 || s.tag !== 6) return;
416
- let f = n(a(s, "directoryName OID").value), p;
525
+ let u = [], d = {};
526
+ for (let r of s) {
527
+ if (r.tag !== 48) return;
528
+ let s = i(t, r), l = s[0], f = s[1];
529
+ if (l === void 0 || f === void 0 || s.length !== 2 || l.tag !== 6) return;
530
+ let p = a(c(l, "directoryName OID").value), m;
417
531
  try {
418
- p = r(u.tag, a(u, "directoryName value").value);
419
- } catch {
532
+ m = o(f.tag, c(f, "directoryName value").value);
533
+ } catch (t) {
534
+ n(t, e);
420
535
  return;
421
536
  }
422
- let m = d(f), h = m === void 0 ? {
423
- oid: f,
424
- valueTag: u.tag,
425
- value: p
537
+ let h = x(p), g = h === void 0 ? {
538
+ oid: p,
539
+ valueTag: f.tag,
540
+ value: m
426
541
  } : {
427
- oid: f,
428
- key: m,
429
- valueTag: u.tag,
430
- value: p
542
+ oid: p,
543
+ key: h,
544
+ valueTag: f.tag,
545
+ value: m
431
546
  };
432
- c.push(h), m !== void 0 && l[m] === void 0 && (l[m] = p);
547
+ u.push(g), h !== void 0 && d[h] === void 0 && (d[h] = m);
433
548
  }
434
- if (c.length !== 0) return {
435
- derHex: o(e.slice(i.start - i.headerLength, i.end)),
436
- attributes: c,
437
- values: l
549
+ if (u.length !== 0) return {
550
+ derHex: l(t.slice(r.start - r.headerLength, r.end)),
551
+ attributes: u,
552
+ values: d
438
553
  };
439
554
  }
440
555
  /** Human-readable label for a constraint form, used in error messages. */
@@ -443,12 +558,12 @@ function formatConstraintForm(e) {
443
558
  case "dns": return `dns:${e.value}`;
444
559
  case "email": return `email:${e.value}`;
445
560
  case "uri": return `uri:${e.value}`;
446
- case "ip": return `ip:${l(e.addressBytes)}`;
561
+ case "ip": return `ip:${p(e.addressBytes)}`;
447
562
  case "directoryName": return `dn:${e.derHex.slice(0, 20)}${e.derHex.length > 20 ? "..." : ""}`;
563
+ case "srv": return `srv:${e.value}`;
448
564
  default: throw Error(`Unhandled NameConstraintForm type: ${String(e)}`);
449
565
  }
450
566
  }
451
- //#endregion
452
567
  export { createNameConstraintValidationState, evaluateNameConstraints };
453
568
 
454
569
  //# sourceMappingURL=name-constraints-engine.js.map