micro509 0.12.0 → 0.14.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 (123) hide show
  1. package/README.md +78 -30
  2. package/dist/crypto/crypto.d.ts +73 -0
  3. package/dist/crypto/crypto.js +74 -0
  4. package/dist/crypto/crypto.js.map +1 -0
  5. package/dist/crypto.d.ts +4 -0
  6. package/dist/crypto.js +2 -0
  7. package/dist/der/der.d.ts +127 -0
  8. package/dist/der/der.js +171 -0
  9. package/dist/der/der.js.map +1 -0
  10. package/dist/der.d.ts +4 -0
  11. package/dist/der.js +4 -0
  12. package/dist/index.d.ts +13 -9
  13. package/dist/index.js +17 -15
  14. package/dist/internal/asn1/asn1.d.ts +20 -0
  15. package/dist/internal/asn1/asn1.js +80 -37
  16. package/dist/internal/asn1/asn1.js.map +1 -1
  17. package/dist/internal/asn1/der.d.ts +173 -0
  18. package/dist/internal/asn1/der.js +116 -38
  19. package/dist/internal/asn1/der.js.map +1 -1
  20. package/dist/internal/asn1/oids.js +7 -1
  21. package/dist/internal/asn1/oids2.js +9 -2
  22. package/dist/internal/asn1/oids2.js.map +1 -1
  23. package/dist/internal/crypto/algorithm-names.js +4 -0
  24. package/dist/internal/crypto/algorithm-names.js.map +1 -1
  25. package/dist/internal/crypto/pbes2.d.ts +38 -0
  26. package/dist/internal/crypto/pbes2.js +9 -10
  27. package/dist/internal/crypto/pbes2.js.map +1 -1
  28. package/dist/internal/crypto/rsa-pss.js +2 -0
  29. package/dist/internal/crypto/rsa-pss.js.map +1 -1
  30. package/dist/internal/crypto/sig-verify.d.ts +31 -0
  31. package/dist/internal/crypto/sig-verify.js +10 -8
  32. package/dist/internal/crypto/sig-verify.js.map +1 -1
  33. package/dist/internal/shared/base64.js +7 -1
  34. package/dist/internal/shared/base64.js.map +1 -1
  35. package/dist/internal/shared/dn.d.ts +16 -0
  36. package/dist/internal/shared/dn.js +149 -12
  37. package/dist/internal/shared/dn.js.map +1 -1
  38. package/dist/internal/shared/ip.d.ts +69 -0
  39. package/dist/internal/shared/ip.js +18 -1
  40. package/dist/internal/shared/ip.js.map +1 -1
  41. package/dist/internal/shared/rfc3454-tables.js +1782 -0
  42. package/dist/internal/shared/rfc3454-tables.js.map +1 -0
  43. package/dist/internal/verify/name-constraints-engine.js +47 -15
  44. package/dist/internal/verify/name-constraints-engine.js.map +1 -1
  45. package/dist/internal/verify/policy-engine.js +85 -94
  46. package/dist/internal/verify/policy-engine.js.map +1 -1
  47. package/dist/internal/verify/verify-path.d.ts +7 -0
  48. package/dist/internal/verify/verify-path.js +49 -45
  49. package/dist/internal/verify/verify-path.js.map +1 -1
  50. package/dist/internal/x509/directory-name.js +24 -0
  51. package/dist/internal/x509/directory-name.js.map +1 -0
  52. package/dist/internal/x509/extension-bits.js +14 -11
  53. package/dist/internal/x509/extension-bits.js.map +1 -1
  54. package/dist/internal/x509/extension-errors.d.ts +6 -0
  55. package/dist/internal/x509/extension-errors.js +20 -0
  56. package/dist/internal/x509/extension-errors.js.map +1 -0
  57. package/dist/internal/x509/extension-registry.js +113 -55
  58. package/dist/internal/x509/extension-registry.js.map +1 -1
  59. package/dist/internal/x509/general-name-tags.js +28 -0
  60. package/dist/internal/x509/general-name-tags.js.map +1 -0
  61. package/dist/internal/x509/general-name.js +97 -0
  62. package/dist/internal/x509/general-name.js.map +1 -0
  63. package/dist/internal/x509/name-fields.js +20 -10
  64. package/dist/internal/x509/name-fields.js.map +1 -1
  65. package/dist/keys/keys.d.ts +16 -2
  66. package/dist/keys/keys.js +311 -170
  67. package/dist/keys/keys.js.map +1 -1
  68. package/dist/keys.d.ts +3 -2
  69. package/dist/keys.js +3 -2
  70. package/dist/pem/pem.d.ts +3 -2
  71. package/dist/pem/pem.js +112 -30
  72. package/dist/pem/pem.js.map +1 -1
  73. package/dist/pkcs/pkcs12-mac.js +3 -3
  74. package/dist/pkcs/pkcs12-mac.js.map +1 -1
  75. package/dist/pkcs/pkcs7.d.ts +85 -21
  76. package/dist/pkcs/pkcs7.js +261 -186
  77. package/dist/pkcs/pkcs7.js.map +1 -1
  78. package/dist/pkcs.d.ts +2 -2
  79. package/dist/result/result.d.ts +18 -10
  80. package/dist/result/result.js +20 -12
  81. package/dist/result/result.js.map +1 -1
  82. package/dist/result.d.ts +2 -2
  83. package/dist/result.js +2 -2
  84. package/dist/revocation/chain.js +154 -103
  85. package/dist/revocation/chain.js.map +1 -1
  86. package/dist/revocation/crl.d.ts +13 -2
  87. package/dist/revocation/crl.js +415 -236
  88. package/dist/revocation/crl.js.map +1 -1
  89. package/dist/revocation/ocsp.d.ts +58 -17
  90. package/dist/revocation/ocsp.js +186 -174
  91. package/dist/revocation/ocsp.js.map +1 -1
  92. package/dist/revocation/revocation.d.ts +4 -4
  93. package/dist/revocation/revocation.js +48 -30
  94. package/dist/revocation/revocation.js.map +1 -1
  95. package/dist/revocation.d.ts +4 -4
  96. package/dist/revocation.js +5 -5
  97. package/dist/verify/identity.d.ts +2 -1
  98. package/dist/verify/identity.js +2 -1
  99. package/dist/verify/identity.js.map +1 -1
  100. package/dist/verify/policy.d.ts +6 -1
  101. package/dist/verify/verify.d.ts +18 -3
  102. package/dist/verify/verify.js +66 -41
  103. package/dist/verify/verify.js.map +1 -1
  104. package/dist/verify.d.ts +2 -1
  105. package/dist/verify.js +4 -3
  106. package/dist/x509/certificate.d.ts +36 -13
  107. package/dist/x509/certificate.js +36 -19
  108. package/dist/x509/certificate.js.map +1 -1
  109. package/dist/x509/csr.d.ts +6 -3
  110. package/dist/x509/extensions.d.ts +59 -22
  111. package/dist/x509/extensions.js +384 -117
  112. package/dist/x509/extensions.js.map +1 -1
  113. package/dist/x509/name-text.js +2 -0
  114. package/dist/x509/name-text.js.map +1 -1
  115. package/dist/x509/name.d.ts +8 -5
  116. package/dist/x509/name.js +17 -12
  117. package/dist/x509/name.js.map +1 -1
  118. package/dist/x509/parse.d.ts +104 -18
  119. package/dist/x509/parse.js +127 -163
  120. package/dist/x509/parse.js.map +1 -1
  121. package/dist/x509.d.ts +8 -5
  122. package/dist/x509.js +10 -8
  123. package/package.json +52 -34
@@ -1,4 +1,19 @@
1
+ import { OIDS as e } from "../asn1/oids2.js";
2
+ import { A1_UNASSIGNED_RANGES as t, B2_CASE_FOLD as n, NFKC_3_2_CORRECTIONS as r } from "./rfc3454-tables.js";
1
3
  //#region src/internal/shared/dn.ts
4
+ /**
5
+ * Distinguished name comparison utilities implementing RFC 5280 §7.1, which
6
+ * defers string preparation to the RFC 4518 LDAP profile.
7
+ *
8
+ * {@linkcode prepareNameCompareString} runs the RFC 4518 Map, Normalize,
9
+ * Prohibit, and Insignificant Space steps with RFC 3454 Appendix B.2 case
10
+ * folding. The Prohibit step rejects code points unassigned in the running
11
+ * Unicode version; RFC 3454 Table A.1 pins that set to Unicode 3.2.
12
+ *
13
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc4518 | RFC 4518}
14
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc5280#section-7.1 | RFC 5280 §7.1}
15
+ * @module
16
+ */
2
17
  /** RFC 5280 [§7.1](https://datatracker.ietf.org/doc/html/rfc5280#section-7.1) semantic equality: same RDN count, each pair matches attribute-by-attribute. */
3
18
  function compareDistinguishedNames(e, t) {
4
19
  if (e.rdns.length !== t.rdns.length) return !1;
@@ -44,30 +59,152 @@ function compareRelativeDistinguishedNames(e, t) {
44
59
  return !0;
45
60
  }
46
61
  /** Compares two AttributeTypeAndValue pairs using RFC 5280 [§7.1](https://datatracker.ietf.org/doc/html/rfc5280#section-7.1) string-prep for DirectoryString tags. */
47
- function compareNameAttributeValue(e, t) {
48
- if (e.oid !== t.oid) return !1;
49
- if (isDirectoryStringTag(e.valueTag) && isDirectoryStringTag(t.valueTag)) {
50
- let n = prepareNameCompareString(e.value), r = prepareNameCompareString(t.value);
51
- return n === void 0 || r === void 0 ? !1 : n === r;
62
+ function compareNameAttributeValue(t, n) {
63
+ if (t.oid !== n.oid) return !1;
64
+ if (t.oid === e.domainComponent) return compareIa5AttributeValue(t, n, (e, t) => {
65
+ let n = prepareNameCompareString(e);
66
+ return n !== void 0 && n === prepareNameCompareString(t);
67
+ });
68
+ if (t.oid === e.emailAddress && compareIa5AttributeValue(t, n, (e, t) => e.toLowerCase() === t.toLowerCase())) return !0;
69
+ if (isDirectoryStringTag(t.valueTag) && isDirectoryStringTag(n.valueTag)) {
70
+ let e = prepareNameCompareString(t.value), r = prepareNameCompareString(n.value);
71
+ return e === void 0 || r === void 0 ? !1 : e === r;
52
72
  }
53
- return e.valueTag === t.valueTag && e.value === t.value;
73
+ return t.valueTag === n.valueTag && t.value === n.value;
54
74
  }
55
- /** True for UTF8String (0x0C) and PrintableString (0x13) — the DirectoryString types we normalize. */
75
+ /**
76
+ * Applies `matches` to two IA5String attribute values. A value under any other
77
+ * tag, or a non-ASCII value, is malformed and does not match.
78
+ */
79
+ function compareIa5AttributeValue(e, t, n) {
80
+ return e.valueTag !== 22 || t.valueTag !== 22 || !isAscii(e.value) || !isAscii(t.value) ? !1 : n(e.value, t.value);
81
+ }
82
+ /**
83
+ * True for the DirectoryString encodings the parser decodes to a comparable
84
+ * string: UTF8String (0x0C), PrintableString (0x13), UniversalString (0x1C),
85
+ * and BMPString (0x1E). TeletexString (0x14) is rejected at parse time, so it
86
+ * never reaches comparison.
87
+ */
56
88
  function isDirectoryStringTag(e) {
57
- return e === 12 || e === 19;
89
+ return e === 12 || e === 19 || e === 28 || e === 30;
90
+ }
91
+ /** RFC 4518 §2.2: code points deleted (Cc/Cf controls, joiners, ignorables). */
92
+ const i = [
93
+ [0, 8],
94
+ [14, 31],
95
+ [127, 132],
96
+ [134, 159],
97
+ [173, 173],
98
+ [847, 847],
99
+ [1757, 1757],
100
+ [1807, 1807],
101
+ [6150, 6150],
102
+ [6155, 6158],
103
+ [8203, 8207],
104
+ [8234, 8238],
105
+ [8288, 8291],
106
+ [8298, 8303],
107
+ [65024, 65039],
108
+ [65279, 65279],
109
+ [65529, 65532],
110
+ [119155, 119162],
111
+ [917505, 917505],
112
+ [917536, 917631]
113
+ ], a = [
114
+ [9, 13],
115
+ [133, 133],
116
+ [160, 160],
117
+ [5760, 5760],
118
+ [8192, 8202],
119
+ [8232, 8233],
120
+ [8239, 8239],
121
+ [8287, 8287],
122
+ [12288, 12288]
123
+ ];
124
+ /** True when `codePoint` falls in one of the inclusive `ranges`. */
125
+ function inRanges(e, t) {
126
+ return t.some(([t, n]) => e >= t && e <= n);
127
+ }
128
+ /** True when every code point in `value` is ASCII. */
129
+ function isAscii(e) {
130
+ for (let t of e) {
131
+ let e = t.codePointAt(0);
132
+ if (e === void 0 || e > 127) return !1;
133
+ }
134
+ return !0;
135
+ }
136
+ /**
137
+ * RFC 4518 §2.4 Prohibit against the frozen Unicode 3.2 repertoire: code points
138
+ * unassigned in 3.2 (RFC 3454 Table A.1), private use, surrogate, non-character,
139
+ * and the replacement character. `\p{Cn}` is avoided because it tracks the
140
+ * running Unicode version rather than 3.2.
141
+ */
142
+ function isProhibited(e) {
143
+ return inRanges(e, t) || e >= 57344 && e <= 63743 || e >= 983040 && e <= 1048573 || e >= 1048576 && e <= 1114109 || e >= 55296 && e <= 57343 || e >= 64976 && e <= 65007 || (e & 65535) >= 65534 || e === 65533;
144
+ }
145
+ /**
146
+ * RFC 4518 §2.2 Map against the Unicode 3.2 repertoire: delete ignorable code
147
+ * points, fold separators to SPACE, reject any code point prohibited in 3.2
148
+ * before the host NFKC can reinterpret it, and freeze the CJK compatibility
149
+ * ideographs whose NFKC decomposition Unicode 4.0 later corrected. Returns
150
+ * `undefined` when a prohibited code point is present.
151
+ */
152
+ function mapAndProhibitSource(e) {
153
+ let t = "";
154
+ for (let n of e) {
155
+ let e = n.codePointAt(0);
156
+ if (!(e === void 0 || inRanges(e, i))) {
157
+ if (inRanges(e, a)) {
158
+ t += " ";
159
+ continue;
160
+ }
161
+ if (isProhibited(e)) return;
162
+ t += r.get(n) ?? n;
163
+ }
164
+ }
165
+ return t;
166
+ }
167
+ /** RFC 3454 Appendix B.2 case fold, applied per code point with identity fallback. */
168
+ function caseFold(e) {
169
+ let t = "";
170
+ for (let r of e) t += n.get(r) ?? r;
171
+ return t;
58
172
  }
59
- /** NFKC-normalizes, lowercases, trims, and collapses whitespace for RFC 5280 §7.1 comparison. */
173
+ /**
174
+ * RFC 4518 §2.6.1: collapses insignificant SPACE runs. A U+0020 followed by a
175
+ * combining mark is significant (the base of a combining sequence) and stays.
176
+ */
177
+ function collapseInsignificantSpaces(e) {
178
+ return e.replace(/ +(?!\p{M})/gu, " ").replace(/^ (?!\p{M})/u, "").replace(/ $/u, "");
179
+ }
180
+ /**
181
+ * Prepares a DirectoryString value for RFC 5280 §7.1 comparison through the RFC
182
+ * 4518 Map, Normalize, Prohibit, and Insignificant Space steps against the
183
+ * Unicode 3.2 repertoire. B.2 case folds first, the host NFKC runs once, and
184
+ * the Prohibit set is rechecked on the output. Returns `undefined` when a
185
+ * prohibited code point is present.
186
+ */
60
187
  function prepareNameCompareString(e) {
61
- let t = e.normalize("NFKC");
62
- if (!/[^\P{Cc}\t\n\r]/u.test(t)) return t.toLowerCase().trim().replace(/\s+/gu, " ");
188
+ let t = mapAndProhibitSource(e);
189
+ if (t === void 0) return;
190
+ let n = caseFold(t).normalize("NFKC");
191
+ for (let e of n) {
192
+ let t = e.codePointAt(0);
193
+ if (t !== void 0 && isProhibited(t)) return;
194
+ }
195
+ return collapseInsignificantSpaces(n);
63
196
  }
64
197
  /** Canonical string for a single RDN: attributes sorted, values prepared. */
65
198
  function canonicalRdnKey(e) {
66
199
  return e.attributes.map((e) => {
67
- let t = isDirectoryStringTag(e.valueTag) ? prepareNameCompareString(e.value) ?? `[raw:${e.valueTag}]${e.value}` : `[${String(e.valueTag)}]${e.value}`;
200
+ let t = canonicalAttributeValue(e);
68
201
  return `${e.oid}=${escapeCanonicalDnValue(t)}`;
69
202
  }).sort().join("+");
70
203
  }
204
+ /** Tag-independent canonical value for one attribute, matching {@linkcode compareNameAttributeValue}. */
205
+ function canonicalAttributeValue(t) {
206
+ return t.oid === e.domainComponent && t.valueTag === 22 && isAscii(t.value) ? `[dc]${prepareNameCompareString(t.value) ?? t.value.toLowerCase()}` : t.oid === e.emailAddress && t.valueTag === 22 && isAscii(t.value) ? `[email]${t.value.toLowerCase()}` : isDirectoryStringTag(t.valueTag) ? prepareNameCompareString(t.value) ?? `[raw:${t.valueTag}]${t.value}` : `[${String(t.valueTag)}]${t.value}`;
207
+ }
71
208
  function escapeCanonicalDnValue(e) {
72
209
  return e.replaceAll("\\", "\\\\").replaceAll(",", "\\,").replaceAll("+", "\\+").replaceAll("=", "\\=");
73
210
  }
@@ -1 +1 @@
1
- {"version":3,"file":"dn.js","names":[],"sources":["../../../src/internal/shared/dn.ts"],"sourcesContent":["/**\n * RFC 5280 §7.1 distinguished name comparison utilities.\n *\n * Provides semantic DN equality (case-folding, NFKC normalization,\n * whitespace collapse) and a canonical string key for O(1) Map lookups.\n *\n * @see {@link https://datatracker.ietf.org/doc/html/rfc5280#section-7.1 | RFC 5280 §7.1}. Internationalized Names in Distinguished Names\n * @module\n */\n\nimport type {\n\tParsedName,\n\tParsedNameAttribute,\n\tParsedRelativeDistinguishedName,\n} from '#micro509/x509/parse';\n\n// Public API\n\n/** RFC 5280 [§7.1](https://datatracker.ietf.org/doc/html/rfc5280#section-7.1) semantic equality: same RDN count, each pair matches attribute-by-attribute. */\nexport function compareDistinguishedNames(left: ParsedName, right: ParsedName): boolean {\n\tif (left.rdns.length !== right.rdns.length) {\n\t\treturn false;\n\t}\n\tfor (let index = 0; index < left.rdns.length; index += 1) {\n\t\tconst leftRdn = left.rdns[index];\n\t\tconst rightRdn = right.rdns[index];\n\t\tif (leftRdn === undefined || rightRdn === undefined) {\n\t\t\treturn false;\n\t\t}\n\t\tif (!compareRelativeDistinguishedNames(leftRdn, rightRdn)) {\n\t\t\treturn false;\n\t\t}\n\t}\n\treturn true;\n}\n\n/**\n * Produces a deterministic string key for a parsed DN, suitable as a Map key.\n * Semantically equal DNs (per RFC 5280 [§7.1](https://datatracker.ietf.org/doc/html/rfc5280#section-7.1)) produce identical keys.\n *\n * Format: RDNs joined by `,`, attributes within each RDN sorted by OID then by prepared value, joined by `+`.\n */\nexport function canonicalDnKey(name: ParsedName): string {\n\treturn name.rdns.map(canonicalRdnKey).join(',');\n}\n\n/** True when `subject` equals or is subordinate to `constraint` (RDN prefix match). */\nexport function isWithinDirectoryNameSubtree(subject: ParsedName, constraint: ParsedName): boolean {\n\tif (constraint.rdns.length > subject.rdns.length) {\n\t\treturn false;\n\t}\n\tfor (let index = 0; index < constraint.rdns.length; index += 1) {\n\t\tconst subjectRdn = subject.rdns[index];\n\t\tconst constraintRdn = constraint.rdns[index];\n\t\tif (subjectRdn === undefined || constraintRdn === undefined) {\n\t\t\treturn false;\n\t\t}\n\t\tif (!compareRelativeDistinguishedNames(subjectRdn, constraintRdn)) {\n\t\t\treturn false;\n\t\t}\n\t}\n\treturn true;\n}\n\n// RDN / attribute comparison\n\n/** Order-independent RDN equality: same attribute count, each pair matched exactly once. */\nexport function compareRelativeDistinguishedNames(\n\tleft: ParsedRelativeDistinguishedName,\n\tright: ParsedRelativeDistinguishedName,\n): boolean {\n\tif (left.attributes.length !== right.attributes.length) {\n\t\treturn false;\n\t}\n\tconst matched = Array.from({ length: right.attributes.length }, () => false);\n\tfor (const leftAttribute of left.attributes) {\n\t\tlet found = false;\n\t\tfor (let index = 0; index < right.attributes.length; index += 1) {\n\t\t\tconst rightAttribute = right.attributes[index];\n\t\t\tif (rightAttribute === undefined || matched[index]) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (!compareNameAttributeValue(leftAttribute, rightAttribute)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tmatched[index] = true;\n\t\t\tfound = true;\n\t\t\tbreak;\n\t\t}\n\t\tif (!found) {\n\t\t\treturn false;\n\t\t}\n\t}\n\treturn true;\n}\n\n/** Compares two AttributeTypeAndValue pairs using RFC 5280 [§7.1](https://datatracker.ietf.org/doc/html/rfc5280#section-7.1) string-prep for DirectoryString tags. */\nexport function compareNameAttributeValue(\n\tleft: ParsedNameAttribute,\n\tright: ParsedNameAttribute,\n): boolean {\n\tif (left.oid !== right.oid) {\n\t\treturn false;\n\t}\n\tif (isDirectoryStringTag(left.valueTag) && isDirectoryStringTag(right.valueTag)) {\n\t\tconst preparedLeft = prepareNameCompareString(left.value);\n\t\tconst preparedRight = prepareNameCompareString(right.value);\n\t\tif (preparedLeft === undefined || preparedRight === undefined) {\n\t\t\treturn false;\n\t\t}\n\t\treturn preparedLeft === preparedRight;\n\t}\n\treturn left.valueTag === right.valueTag && left.value === right.value;\n}\n\n// Helpers\n\n/** True for UTF8String (0x0C) and PrintableString (0x13) — the DirectoryString types we normalize. */\nexport function isDirectoryStringTag(tag: number): boolean {\n\treturn tag === 0x0c || tag === 0x13;\n}\n\n/** NFKC-normalizes, lowercases, trims, and collapses whitespace for RFC 5280 §7.1 comparison. */\nexport function prepareNameCompareString(value: string): string | undefined {\n\tconst normalized = value.normalize('NFKC');\n\tif (/[^\\P{Cc}\\t\\n\\r]/u.test(normalized)) {\n\t\treturn undefined;\n\t}\n\treturn normalized.toLowerCase().trim().replace(/\\s+/gu, ' ');\n}\n\n/** Canonical string for a single RDN: attributes sorted, values prepared. */\nfunction canonicalRdnKey(rdn: ParsedRelativeDistinguishedName): string {\n\treturn rdn.attributes\n\t\t.map((attr) => {\n\t\t\tconst val = isDirectoryStringTag(attr.valueTag)\n\t\t\t\t? (prepareNameCompareString(attr.value) ?? `[raw:${attr.valueTag}]${attr.value}`)\n\t\t\t\t: `[${String(attr.valueTag)}]${attr.value}`;\n\t\t\treturn `${attr.oid}=${escapeCanonicalDnValue(val)}`;\n\t\t})\n\t\t.sort()\n\t\t.join('+');\n}\n\nfunction escapeCanonicalDnValue(value: string): string {\n\treturn value\n\t\t.replaceAll('\\\\', '\\\\\\\\')\n\t\t.replaceAll(',', '\\\\,')\n\t\t.replaceAll('+', '\\\\+')\n\t\t.replaceAll('=', '\\\\=');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;CAET;CACA,OAAO;AACR;;AAGA,SAAgB,0BACf,GACA,GACU;CACV,IAAI,EAAK,QAAQ,EAAM,KACtB,OAAO;CAER,IAAI,qBAAqB,EAAK,QAAQ,KAAK,qBAAqB,EAAM,QAAQ,GAAG;EAChF,IAAM,IAAe,yBAAyB,EAAK,KAAK,GAClD,IAAgB,yBAAyB,EAAM,KAAK;EAI1D,OAHI,MAAiB,KAAA,KAAa,MAAkB,KAAA,IAC5C,KAED,MAAiB;CACzB;CACA,OAAO,EAAK,aAAa,EAAM,YAAY,EAAK,UAAU,EAAM;AACjE;;AAKA,SAAgB,qBAAqB,GAAsB;CAC1D,OAAO,MAAQ,MAAQ,MAAQ;AAChC;;AAGA,SAAgB,yBAAyB,GAAmC;CAC3E,IAAM,IAAa,EAAM,UAAU,MAAM;CACrC,wBAAmB,KAAK,CAAU,GAGtC,OAAO,EAAW,YAAY,CAAC,CAAC,KAAK,CAAC,CAAC,QAAQ,SAAS,GAAG;AAC5D;;AAGA,SAAS,gBAAgB,GAA8C;CACtE,OAAO,EAAI,WACT,KAAK,MAAS;EACd,IAAM,IAAM,qBAAqB,EAAK,QAAQ,IAC1C,yBAAyB,EAAK,KAAK,KAAK,QAAQ,EAAK,SAAS,GAAG,EAAK,UACvE,IAAI,OAAO,EAAK,QAAQ,EAAE,GAAG,EAAK;EACrC,OAAO,GAAG,EAAK,IAAI,GAAG,uBAAuB,CAAG;CACjD,CAAC,CAAC,CACD,KAAK,CAAC,CACN,KAAK,GAAG;AACX;AAEA,SAAS,uBAAuB,GAAuB;CACtD,OAAO,EACL,WAAW,MAAM,MAAM,CAAC,CACxB,WAAW,KAAK,KAAK,CAAC,CACtB,WAAW,KAAK,KAAK,CAAC,CACtB,WAAW,KAAK,KAAK;AACxB"}
1
+ {"version":3,"file":"dn.js","names":[],"sources":["../../../src/internal/shared/dn.ts"],"sourcesContent":["/**\n * Distinguished name comparison utilities implementing RFC 5280 §7.1, which\n * defers string preparation to the RFC 4518 LDAP profile.\n *\n * {@linkcode prepareNameCompareString} runs the RFC 4518 Map, Normalize,\n * Prohibit, and Insignificant Space steps with RFC 3454 Appendix B.2 case\n * folding. The Prohibit step rejects code points unassigned in the running\n * Unicode version; RFC 3454 Table A.1 pins that set to Unicode 3.2.\n *\n * @see {@link https://datatracker.ietf.org/doc/html/rfc4518 | RFC 4518}\n * @see {@link https://datatracker.ietf.org/doc/html/rfc5280#section-7.1 | RFC 5280 §7.1}\n * @module\n */\n\nimport { OIDS } from '#micro509/internal/asn1/oids';\nimport {\n\tA1_UNASSIGNED_RANGES,\n\tB2_CASE_FOLD,\n\ttype CodePointRange,\n\tNFKC_3_2_CORRECTIONS,\n} from '#micro509/internal/shared/rfc3454-tables';\nimport type {\n\tParsedName,\n\tParsedNameAttribute,\n\tParsedRelativeDistinguishedName,\n} from '#micro509/x509/parse';\n\n// Public API\n\n/** RFC 5280 [§7.1](https://datatracker.ietf.org/doc/html/rfc5280#section-7.1) semantic equality: same RDN count, each pair matches attribute-by-attribute. */\nexport function compareDistinguishedNames(left: ParsedName, right: ParsedName): boolean {\n\tif (left.rdns.length !== right.rdns.length) {\n\t\treturn false;\n\t}\n\tfor (let index = 0; index < left.rdns.length; index += 1) {\n\t\tconst leftRdn = left.rdns[index];\n\t\tconst rightRdn = right.rdns[index];\n\t\tif (leftRdn === undefined || rightRdn === undefined) {\n\t\t\treturn false;\n\t\t}\n\t\tif (!compareRelativeDistinguishedNames(leftRdn, rightRdn)) {\n\t\t\treturn false;\n\t\t}\n\t}\n\treturn true;\n}\n\n/**\n * Produces a deterministic string key for a parsed DN, suitable as a Map key.\n * Semantically equal DNs (per RFC 5280 [§7.1](https://datatracker.ietf.org/doc/html/rfc5280#section-7.1)) produce identical keys.\n *\n * Format: RDNs joined by `,`, attributes within each RDN sorted by OID then by prepared value, joined by `+`.\n */\nexport function canonicalDnKey(name: ParsedName): string {\n\treturn name.rdns.map(canonicalRdnKey).join(',');\n}\n\n/** True when `subject` equals or is subordinate to `constraint` (RDN prefix match). */\nexport function isWithinDirectoryNameSubtree(subject: ParsedName, constraint: ParsedName): boolean {\n\tif (constraint.rdns.length > subject.rdns.length) {\n\t\treturn false;\n\t}\n\tfor (let index = 0; index < constraint.rdns.length; index += 1) {\n\t\tconst subjectRdn = subject.rdns[index];\n\t\tconst constraintRdn = constraint.rdns[index];\n\t\tif (subjectRdn === undefined || constraintRdn === undefined) {\n\t\t\treturn false;\n\t\t}\n\t\tif (!compareRelativeDistinguishedNames(subjectRdn, constraintRdn)) {\n\t\t\treturn false;\n\t\t}\n\t}\n\treturn true;\n}\n\n// RDN / attribute comparison\n\n/** Order-independent RDN equality: same attribute count, each pair matched exactly once. */\nexport function compareRelativeDistinguishedNames(\n\tleft: ParsedRelativeDistinguishedName,\n\tright: ParsedRelativeDistinguishedName,\n): boolean {\n\tif (left.attributes.length !== right.attributes.length) {\n\t\treturn false;\n\t}\n\tconst matched = Array.from({ length: right.attributes.length }, () => false);\n\tfor (const leftAttribute of left.attributes) {\n\t\tlet found = false;\n\t\tfor (let index = 0; index < right.attributes.length; index += 1) {\n\t\t\tconst rightAttribute = right.attributes[index];\n\t\t\tif (rightAttribute === undefined || matched[index]) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (!compareNameAttributeValue(leftAttribute, rightAttribute)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tmatched[index] = true;\n\t\t\tfound = true;\n\t\t\tbreak;\n\t\t}\n\t\tif (!found) {\n\t\t\treturn false;\n\t\t}\n\t}\n\treturn true;\n}\n\n/** Compares two AttributeTypeAndValue pairs using RFC 5280 [§7.1](https://datatracker.ietf.org/doc/html/rfc5280#section-7.1) string-prep for DirectoryString tags. */\nexport function compareNameAttributeValue(\n\tleft: ParsedNameAttribute,\n\tright: ParsedNameAttribute,\n): boolean {\n\tif (left.oid !== right.oid) {\n\t\treturn false;\n\t}\n\tif (left.oid === OIDS.domainComponent) {\n\t\t// RFC 5280 §7.3 / RFC 4519 caseIgnoreIA5Match: domainComponent is\n\t\t// IA5String, prepared and compared case-insensitively with insignificant\n\t\t// spaces collapsed.\n\t\treturn compareIa5AttributeValue(left, right, (leftValue, rightValue) => {\n\t\t\tconst prepared = prepareNameCompareString(leftValue);\n\t\t\treturn prepared !== undefined && prepared === prepareNameCompareString(rightValue);\n\t\t});\n\t}\n\t// RFC 5280 §4.1.2.6 and RFC 2985 §6.1 pkcs9CaseIgnoreMatch: emailAddress is\n\t// IA5String, matched character by character without regard to case and\n\t// without the RFC 4518 space collapsing a DirectoryString attribute gets.\n\t// Under any other tag the value falls through to the general comparison.\n\tif (\n\t\tleft.oid === OIDS.emailAddress &&\n\t\tcompareIa5AttributeValue(\n\t\t\tleft,\n\t\t\tright,\n\t\t\t(leftValue, rightValue) => leftValue.toLowerCase() === rightValue.toLowerCase(),\n\t\t)\n\t) {\n\t\treturn true;\n\t}\n\tif (isDirectoryStringTag(left.valueTag) && isDirectoryStringTag(right.valueTag)) {\n\t\tconst preparedLeft = prepareNameCompareString(left.value);\n\t\tconst preparedRight = prepareNameCompareString(right.value);\n\t\tif (preparedLeft === undefined || preparedRight === undefined) {\n\t\t\treturn false;\n\t\t}\n\t\treturn preparedLeft === preparedRight;\n\t}\n\treturn left.valueTag === right.valueTag && left.value === right.value;\n}\n\n// Helpers\n\n/**\n * Applies `matches` to two IA5String attribute values. A value under any other\n * tag, or a non-ASCII value, is malformed and does not match.\n */\nfunction compareIa5AttributeValue(\n\tleft: ParsedNameAttribute,\n\tright: ParsedNameAttribute,\n\tmatches: (left: string, right: string) => boolean,\n): boolean {\n\tif (left.valueTag !== 0x16 || right.valueTag !== 0x16) {\n\t\treturn false;\n\t}\n\tif (!isAscii(left.value) || !isAscii(right.value)) {\n\t\treturn false;\n\t}\n\treturn matches(left.value, right.value);\n}\n\n/**\n * True for the DirectoryString encodings the parser decodes to a comparable\n * string: UTF8String (0x0C), PrintableString (0x13), UniversalString (0x1C),\n * and BMPString (0x1E). TeletexString (0x14) is rejected at parse time, so it\n * never reaches comparison.\n */\nexport function isDirectoryStringTag(tag: number): boolean {\n\treturn tag === 0x0c || tag === 0x13 || tag === 0x1c || tag === 0x1e;\n}\n\n/** RFC 4518 §2.2: code points deleted (Cc/Cf controls, joiners, ignorables). */\nconst MAP_TO_NOTHING: readonly CodePointRange[] = [\n\t[0x0000, 0x0008], // C0 controls NUL..BS\n\t[0x000e, 0x001f], // C0 controls SO..US\n\t[0x007f, 0x0084], // DEL, C1 controls\n\t[0x0086, 0x009f], // C1 controls\n\t[0x00ad, 0x00ad], // SOFT HYPHEN\n\t[0x034f, 0x034f], // COMBINING GRAPHEME JOINER\n\t[0x06dd, 0x06dd], // ARABIC END OF AYAH\n\t[0x070f, 0x070f], // SYRIAC ABBREVIATION MARK\n\t[0x1806, 0x1806], // MONGOLIAN TODO SOFT HYPHEN\n\t[0x180b, 0x180e], // variation selectors, MONGOLIAN VOWEL SEPARATOR\n\t[0x200b, 0x200f], // ZWSP, ZWNJ, ZWJ, LRM, RLM\n\t[0x202a, 0x202e], // bidi embedding/override controls\n\t[0x2060, 0x2063], // WORD JOINER, invisible operators\n\t[0x206a, 0x206f], // deprecated format controls\n\t[0xfe00, 0xfe0f], // variation selectors\n\t[0xfeff, 0xfeff], // ZERO WIDTH NO-BREAK SPACE (BOM)\n\t[0xfff9, 0xfffc], // interlinear annotations, OBJECT REPLACEMENT CHARACTER\n\t[0x1d173, 0x1d17a], // musical symbol format controls\n\t[0xe0001, 0xe0001], // LANGUAGE TAG\n\t[0xe0020, 0xe007f], // tag characters\n];\n\n/** RFC 4518 §2.2: code points mapped to SPACE (control whitespace and Zs/Zl/Zp). */\nconst MAP_TO_SPACE: readonly CodePointRange[] = [\n\t[0x0009, 0x000d], // TAB, LF, VT, FF, CR\n\t[0x0085, 0x0085], // NEL\n\t[0x00a0, 0x00a0], // NO-BREAK SPACE\n\t[0x1680, 0x1680], // OGHAM SPACE MARK\n\t[0x2000, 0x200a], // EN QUAD .. HAIR SPACE\n\t[0x2028, 0x2029], // LINE SEPARATOR, PARAGRAPH SEPARATOR\n\t[0x202f, 0x202f], // NARROW NO-BREAK SPACE\n\t[0x205f, 0x205f], // MEDIUM MATHEMATICAL SPACE\n\t[0x3000, 0x3000], // IDEOGRAPHIC SPACE\n];\n\n/** True when `codePoint` falls in one of the inclusive `ranges`. */\nfunction inRanges(codePoint: number, ranges: readonly CodePointRange[]): boolean {\n\treturn ranges.some(([first, last]) => codePoint >= first && codePoint <= last);\n}\n\n/** True when every code point in `value` is ASCII. */\nfunction isAscii(value: string): boolean {\n\tfor (const ch of value) {\n\t\tconst codePoint = ch.codePointAt(0);\n\t\tif (codePoint === undefined || codePoint > 0x7f) {\n\t\t\treturn false;\n\t\t}\n\t}\n\treturn true;\n}\n\n/**\n * RFC 4518 §2.4 Prohibit against the frozen Unicode 3.2 repertoire: code points\n * unassigned in 3.2 (RFC 3454 Table A.1), private use, surrogate, non-character,\n * and the replacement character. `\\p{Cn}` is avoided because it tracks the\n * running Unicode version rather than 3.2.\n */\nfunction isProhibited(codePoint: number): boolean {\n\treturn (\n\t\tinRanges(codePoint, A1_UNASSIGNED_RANGES) ||\n\t\t(codePoint >= 0xe000 && codePoint <= 0xf8ff) ||\n\t\t(codePoint >= 0xf0000 && codePoint <= 0xffffd) ||\n\t\t(codePoint >= 0x100000 && codePoint <= 0x10fffd) ||\n\t\t(codePoint >= 0xd800 && codePoint <= 0xdfff) ||\n\t\t(codePoint >= 0xfdd0 && codePoint <= 0xfdef) ||\n\t\t(codePoint & 0xffff) >= 0xfffe ||\n\t\tcodePoint === 0xfffd\n\t);\n}\n\n/**\n * RFC 4518 §2.2 Map against the Unicode 3.2 repertoire: delete ignorable code\n * points, fold separators to SPACE, reject any code point prohibited in 3.2\n * before the host NFKC can reinterpret it, and freeze the CJK compatibility\n * ideographs whose NFKC decomposition Unicode 4.0 later corrected. Returns\n * `undefined` when a prohibited code point is present.\n */\nfunction mapAndProhibitSource(value: string): string | undefined {\n\tlet mapped = '';\n\tfor (const ch of value) {\n\t\tconst codePoint = ch.codePointAt(0);\n\t\tif (codePoint === undefined || inRanges(codePoint, MAP_TO_NOTHING)) {\n\t\t\tcontinue;\n\t\t}\n\t\tif (inRanges(codePoint, MAP_TO_SPACE)) {\n\t\t\tmapped += ' ';\n\t\t\tcontinue;\n\t\t}\n\t\tif (isProhibited(codePoint)) {\n\t\t\treturn undefined;\n\t\t}\n\t\tmapped += NFKC_3_2_CORRECTIONS.get(ch) ?? ch;\n\t}\n\treturn mapped;\n}\n\n/** RFC 3454 Appendix B.2 case fold, applied per code point with identity fallback. */\nfunction caseFold(value: string): string {\n\tlet out = '';\n\tfor (const ch of value) {\n\t\tout += B2_CASE_FOLD.get(ch) ?? ch;\n\t}\n\treturn out;\n}\n\n/**\n * RFC 4518 §2.6.1: collapses insignificant SPACE runs. A U+0020 followed by a\n * combining mark is significant (the base of a combining sequence) and stays.\n */\nfunction collapseInsignificantSpaces(value: string): string {\n\treturn value\n\t\t.replace(/ +(?!\\p{M})/gu, ' ')\n\t\t.replace(/^ (?!\\p{M})/u, '')\n\t\t.replace(/ $/u, '');\n}\n\n/**\n * Prepares a DirectoryString value for RFC 5280 §7.1 comparison through the RFC\n * 4518 Map, Normalize, Prohibit, and Insignificant Space steps against the\n * Unicode 3.2 repertoire. B.2 case folds first, the host NFKC runs once, and\n * the Prohibit set is rechecked on the output. Returns `undefined` when a\n * prohibited code point is present.\n */\nexport function prepareNameCompareString(value: string): string | undefined {\n\tconst mapped = mapAndProhibitSource(value);\n\tif (mapped === undefined) {\n\t\treturn undefined;\n\t}\n\tconst normalized = caseFold(mapped).normalize('NFKC');\n\tfor (const ch of normalized) {\n\t\tconst codePoint = ch.codePointAt(0);\n\t\tif (codePoint !== undefined && isProhibited(codePoint)) {\n\t\t\treturn undefined;\n\t\t}\n\t}\n\treturn collapseInsignificantSpaces(normalized);\n}\n\n/** Canonical string for a single RDN: attributes sorted, values prepared. */\nfunction canonicalRdnKey(rdn: ParsedRelativeDistinguishedName): string {\n\treturn rdn.attributes\n\t\t.map((attr) => {\n\t\t\tconst val = canonicalAttributeValue(attr);\n\t\t\treturn `${attr.oid}=${escapeCanonicalDnValue(val)}`;\n\t\t})\n\t\t.sort()\n\t\t.join('+');\n}\n\n/** Tag-independent canonical value for one attribute, matching {@linkcode compareNameAttributeValue}. */\nfunction canonicalAttributeValue(attr: ParsedNameAttribute): string {\n\tif (attr.oid === OIDS.domainComponent && attr.valueTag === 0x16 && isAscii(attr.value)) {\n\t\treturn `[dc]${prepareNameCompareString(attr.value) ?? attr.value.toLowerCase()}`;\n\t}\n\tif (attr.oid === OIDS.emailAddress && attr.valueTag === 0x16 && isAscii(attr.value)) {\n\t\treturn `[email]${attr.value.toLowerCase()}`;\n\t}\n\tif (isDirectoryStringTag(attr.valueTag)) {\n\t\treturn prepareNameCompareString(attr.value) ?? `[raw:${attr.valueTag}]${attr.value}`;\n\t}\n\treturn `[${String(attr.valueTag)}]${attr.value}`;\n}\n\nfunction escapeCanonicalDnValue(value: string): string {\n\treturn value\n\t\t.replaceAll('\\\\', '\\\\\\\\')\n\t\t.replaceAll(',', '\\\\,')\n\t\t.replaceAll('+', '\\\\+')\n\t\t.replaceAll('=', '\\\\=');\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AA8BA,SAAgB,0BAA0B,GAAkB,GAA4B;CACvF,IAAI,EAAK,KAAK,WAAW,EAAM,KAAK,QACnC,OAAO;CAER,KAAK,IAAI,IAAQ,GAAG,IAAQ,EAAK,KAAK,QAAQ,KAAS,GAAG;EACzD,IAAM,IAAU,EAAK,KAAK,IACpB,IAAW,EAAM,KAAK;EAI5B,IAHI,MAAY,KAAA,KAAa,MAAa,KAAA,KAGtC,CAAC,kCAAkC,GAAS,CAAQ,GACvD,OAAO;CAET;CACA,OAAO;AACR;;;;;;;AAQA,SAAgB,eAAe,GAA0B;CACxD,OAAO,EAAK,KAAK,IAAI,eAAe,CAAC,CAAC,KAAK,GAAG;AAC/C;;AAGA,SAAgB,6BAA6B,GAAqB,GAAiC;C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Y;EAC5B,IAAM,IAAY,EAAG,YAAY,CAAC;EAClC,IAAI,MAAc,KAAA,KAAa,aAAa,CAAS,GACpD;CAEF;CACA,OAAO,4BAA4B,CAAU;AAC9C;;AAGA,SAAS,gBAAgB,GAA8C;CACtE,OAAO,EAAI,WACT,KAAK,MAAS;EACd,IAAM,IAAM,wBAAwB,CAAI;EACxC,OAAO,GAAG,EAAK,IAAI,GAAG,uBAAuB,CAAG;CACjD,CAAC,CAAC,CACD,KAAK,CAAC,CACN,KAAK,GAAG;AACX;;AAGA,SAAS,wBAAwB,GAAmC;CAUnE,OATI,EAAK,QAAQ,EAAK,mBAAmB,EAAK,aAAa,MAAQ,QAAQ,EAAK,KAAK,IAC7E,OAAO,yBAAyB,EAAK,KAAK,KAAK,EAAK,MAAM,YAAY,MAE1E,EAAK,QAAQ,EAAK,gBAAgB,EAAK,aAAa,MAAQ,QAAQ,EAAK,KAAK,IAC1E,UAAU,EAAK,MAAM,YAAY,MAErC,qBAAqB,EAAK,QAAQ,IAC9B,yBAAyB,EAAK,KAAK,KAAK,QAAQ,EAAK,SAAS,GAAG,EAAK,UAEvE,IAAI,OAAO,EAAK,QAAQ,EAAE,GAAG,EAAK;AAC1C;AAEA,SAAS,uBAAuB,GAAuB;CACtD,OAAO,EACL,WAAW,MAAM,MAAM,CAAC,CACxB,WAAW,KAAK,KAAK,CAAC,CACtB,WAAW,KAAK,KAAK,CAAC,CACtB,WAAW,KAAK,KAAK;AACxB"}
@@ -0,0 +1,69 @@
1
+ //#region src/internal/shared/ip.d.ts
2
+ /**
3
+ * Normalizes an IP address string for comparison.\
4
+ * IPv4 addresses pass through unchanged; IPv6 addresses expand to 8
5
+ * colon-separated zero-padded groups.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * normalizeIpAddress('::1');
10
+ * // '0000:0000:0000:0000:0000:0000:0000:0001'
11
+ * ```
12
+ *
13
+ * @param value IPv4 or IPv6 address string to normalize.
14
+ * @returns The normalized address string.
15
+ */
16
+ declare function normalizeIpAddress(value: string): string;
17
+ /**
18
+ * Parses an IPv4 or IPv6 address string into raw bytes.\
19
+ * Returns 4 bytes for IPv4 and 16 bytes for IPv6.
20
+ *
21
+ * Suitable for encoding into SAN iPAddress octets or name-constraint ranges.
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * Array.from(parseIpAddressToBytes('127.0.0.1'));
26
+ * // [127, 0, 0, 1]
27
+ * ```
28
+ *
29
+ * @param value IPv4 or IPv6 address string to parse.
30
+ * @returns The raw address bytes.
31
+ */
32
+ declare function parseIpAddressToBytes(value: string): Uint8Array;
33
+ /**
34
+ * Converts raw IP address bytes back to a human-readable string.\
35
+ * Returns dotted-decimal IPv4 for 4-byte input and colon-separated hex
36
+ * IPv6 for 16-byte input.
37
+ *
38
+ * Throws on any other length.
39
+ *
40
+ * @example
41
+ * ```ts
42
+ * decodeIpAddress(Uint8Array.of(127, 0, 0, 1));
43
+ * // '127.0.0.1'
44
+ * ```
45
+ *
46
+ * @param bytes Raw IPv4 or IPv6 address bytes.
47
+ * @returns The decoded address string.
48
+ */
49
+ declare function decodeIpAddress(bytes: Uint8Array): string;
50
+ /**
51
+ * Returns an all-ones (`0xff`) mask of the appropriate length for the
52
+ * given IP address string.\
53
+ * Returns 4 bytes for IPv4 and 16 bytes for IPv6.
54
+ *
55
+ * Used to build name-constraint subnet masks that match a single host.
56
+ *
57
+ * @example
58
+ * ```ts
59
+ * Array.from(allOnesMaskForIpAddress('127.0.0.1'));
60
+ * // [255, 255, 255, 255]
61
+ * ```
62
+ *
63
+ * @param value IPv4 or IPv6 address string used to choose mask length.
64
+ * @returns An all-ones mask for the same address family.
65
+ */
66
+ declare function allOnesMaskForIpAddress(value: string): Uint8Array;
67
+ //#endregion
68
+ export { allOnesMaskForIpAddress, decodeIpAddress, normalizeIpAddress, parseIpAddressToBytes };
69
+ //# sourceMappingURL=ip.d.ts.map
@@ -37,6 +37,23 @@
37
37
  /** Matches a single valid IPv6 hex segment (1–4 hex digits). */
38
38
  const e = /^[0-9a-f]{1,4}$/;
39
39
  /**
40
+ * Normalizes an IP address string for comparison.\
41
+ * IPv4 addresses pass through unchanged; IPv6 addresses expand to 8
42
+ * colon-separated zero-padded groups.
43
+ *
44
+ * @example
45
+ * ```ts
46
+ * normalizeIpAddress('::1');
47
+ * // '0000:0000:0000:0000:0000:0000:0000:0001'
48
+ * ```
49
+ *
50
+ * @param value IPv4 or IPv6 address string to normalize.
51
+ * @returns The normalized address string.
52
+ */
53
+ function normalizeIpAddress(e) {
54
+ return e.includes(":") ? expandIpv6(e).join(":") : e;
55
+ }
56
+ /**
40
57
  * Expands an IPv6 address, including `::` shorthand, into exactly 8
41
58
  * zero-padded four-character hex segments.\
42
59
  * Throws on malformed input.
@@ -149,6 +166,6 @@ function parseIpv6ToBytes(e) {
149
166
  }), n;
150
167
  }
151
168
  //#endregion
152
- export { allOnesMaskForIpAddress, decodeIpAddress, expandIpv6, parseIpAddressToBytes };
169
+ export { allOnesMaskForIpAddress, decodeIpAddress, expandIpv6, normalizeIpAddress, parseIpAddressToBytes };
153
170
 
154
171
  //# sourceMappingURL=ip.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"ip.js","names":[],"sources":["../../../src/internal/shared/ip.ts"],"sourcesContent":["/**\n * IP address parsing and normalization helpers shared by parsing,\n * validation, and identity matching flows.\n *\n * These utilities keep IPv4 and IPv6 handling consistent for SAN\n * matching and name constraint evaluation.\n *\n * @example\n * ```ts\n * import {\n * \tallOnesMaskForIpAddress,\n * \tdecodeIpAddress,\n * \texpandIpv6,\n * \tnormalizeIpAddress,\n * \tparseIpAddressToBytes,\n * } from './ip.ts';\n *\n * normalizeIpAddress('::1');\n * // '0000:0000:0000:0000:0000:0000:0000:0001'\n *\n * expandIpv6('2001:db8::1');\n * // ['2001', '0db8', '0000', '0000', '0000', '0000', '0000', '0001']\n *\n * Array.from(parseIpAddressToBytes('127.0.0.1'));\n * // [127, 0, 0, 1]\n *\n * decodeIpAddress(Uint8Array.of(127, 0, 0, 1));\n * // '127.0.0.1'\n *\n * Array.from(allOnesMaskForIpAddress('127.0.0.1'));\n * // [255, 255, 255, 255]\n * ```\n *\n * @module\n */\n\n/** Matches a single valid IPv6 hex segment (1–4 hex digits). */\nconst IPV6_SEGMENT = /^[0-9a-f]{1,4}$/;\n\n/**\n * Normalizes an IP address string for comparison.\\\n * IPv4 addresses pass through unchanged; IPv6 addresses expand to 8\n * colon-separated zero-padded groups.\n *\n * @example\n * ```ts\n * normalizeIpAddress('::1');\n * // '0000:0000:0000:0000:0000:0000:0000:0001'\n * ```\n *\n * @param value IPv4 or IPv6 address string to normalize.\n * @returns The normalized address string.\n */\nexport function normalizeIpAddress(value: string): string {\n\tif (!value.includes(':')) {\n\t\treturn value;\n\t}\n\treturn expandIpv6(value).join(':');\n}\n\n/**\n * Expands an IPv6 address, including `::` shorthand, into exactly 8\n * zero-padded four-character hex segments.\\\n * Throws on malformed input.\n *\n * @example\n * ```ts\n * expandIpv6('2001:db8::1');\n * // ['2001', '0db8', '0000', '0000', '0000', '0000', '0000', '0001']\n * ```\n *\n * @param value IPv6 address string to expand.\n * @returns The 8 normalized IPv6 segments.\n */\nexport function expandIpv6(value: string): readonly string[] {\n\tconst normalized = value.toLowerCase();\n\tconst pieces = normalized.split('::');\n\tconst head = pieces[0] ?? '';\n\tconst tail = pieces[1];\n\tif (tail !== undefined && normalized.indexOf('::') !== normalized.lastIndexOf('::')) {\n\t\tthrow new Error(`Invalid IPv6 address: ${value}`);\n\t}\n\tconst headParts = head.length > 0 ? head.split(':') : [];\n\tconst tailParts = tail !== undefined && tail.length > 0 ? tail.split(':') : [];\n\tconst missing = 8 - (headParts.length + tailParts.length);\n\tif ((tail === undefined && headParts.length !== 8) || missing < 0) {\n\t\tthrow new Error(`Invalid IPv6 address: ${value}`);\n\t}\n\tconst zeroes = Array.from({ length: missing }, () => '0');\n\tconst parts = tail === undefined ? headParts : [...headParts, ...zeroes, ...tailParts];\n\tif (parts.length !== 8) {\n\t\tthrow new Error(`Invalid IPv6 address: ${value}`);\n\t}\n\treturn parts.map((segment) => {\n\t\tif (!IPV6_SEGMENT.test(segment)) {\n\t\t\tthrow new Error(`Invalid IPv6 address: ${value}`);\n\t\t}\n\t\treturn segment.padStart(4, '0');\n\t});\n}\n\n/**\n * Parses an IPv4 or IPv6 address string into raw bytes.\\\n * Returns 4 bytes for IPv4 and 16 bytes for IPv6.\n *\n * Suitable for encoding into SAN iPAddress octets or name-constraint ranges.\n *\n * @example\n * ```ts\n * Array.from(parseIpAddressToBytes('127.0.0.1'));\n * // [127, 0, 0, 1]\n * ```\n *\n * @param value IPv4 or IPv6 address string to parse.\n * @returns The raw address bytes.\n */\nexport function parseIpAddressToBytes(value: string): Uint8Array {\n\tif (value.includes(':')) {\n\t\treturn parseIpv6ToBytes(value);\n\t}\n\tconst segments = value.split('.');\n\tif (segments.length !== 4) {\n\t\tthrow new Error(`Invalid IPv4 address: ${value}`);\n\t}\n\treturn Uint8Array.from(\n\t\tsegments.map((segment) => {\n\t\t\tif (segment.length === 0) {\n\t\t\t\tthrow new Error(`Invalid IPv4 address: ${value}`);\n\t\t\t}\n\t\t\tconst parsed = Number(segment);\n\t\t\tif (!Number.isInteger(parsed) || parsed < 0 || parsed > 255) {\n\t\t\t\tthrow new Error(`Invalid IPv4 address: ${value}`);\n\t\t\t}\n\t\t\treturn parsed;\n\t\t}),\n\t);\n}\n\n/**\n * Converts raw IP address bytes back to a human-readable string.\\\n * Returns dotted-decimal IPv4 for 4-byte input and colon-separated hex\n * IPv6 for 16-byte input.\n *\n * Throws on any other length.\n *\n * @example\n * ```ts\n * decodeIpAddress(Uint8Array.of(127, 0, 0, 1));\n * // '127.0.0.1'\n * ```\n *\n * @param bytes Raw IPv4 or IPv6 address bytes.\n * @returns The decoded address string.\n */\nexport function decodeIpAddress(bytes: Uint8Array): string {\n\tif (bytes.length === 4) {\n\t\treturn Array.from(bytes, (value) => String(value)).join('.');\n\t}\n\tif (bytes.length === 16) {\n\t\tconst groups: string[] = [];\n\t\tfor (let index = 0; index < bytes.length; index += 2) {\n\t\t\tconst left = bytes[index] ?? 0;\n\t\t\tconst right = bytes[index + 1] ?? 0;\n\t\t\tgroups.push(((left << 8) | right).toString(16));\n\t\t}\n\t\treturn groups.join(':');\n\t}\n\tthrow new Error(`Unsupported IP address length: ${bytes.length}`);\n}\n\n/**\n * Returns an all-ones (`0xff`) mask of the appropriate length for the\n * given IP address string.\\\n * Returns 4 bytes for IPv4 and 16 bytes for IPv6.\n *\n * Used to build name-constraint subnet masks that match a single host.\n *\n * @example\n * ```ts\n * Array.from(allOnesMaskForIpAddress('127.0.0.1'));\n * // [255, 255, 255, 255]\n * ```\n *\n * @param value IPv4 or IPv6 address string used to choose mask length.\n * @returns An all-ones mask for the same address family.\n */\nexport function allOnesMaskForIpAddress(value: string): Uint8Array {\n\t// Validate and parse the IP address to determine its type\n\tconst parsed = parseIpAddressToBytes(value);\n\tconst mask = new Uint8Array(parsed.length);\n\tmask.fill(0xff);\n\treturn mask;\n}\n\n/** Parses an IPv6 address string into a 16-byte `Uint8Array`. */\nfunction parseIpv6ToBytes(value: string): Uint8Array {\n\tconst expanded = expandIpv6(value);\n\tconst bytes = new Uint8Array(16);\n\texpanded.forEach((segment, index) => {\n\t\tconst parsed = Number.parseInt(segment, 16);\n\t\tbytes[index * 2] = parsed >> 8;\n\t\tbytes[index * 2 + 1] = parsed & 0xff;\n\t});\n\treturn bytes;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCA,MAAM,IAAe;;;;;;;;;;;;;;;AAqCrB,SAAgB,WAAW,GAAkC;CAC5D,IAAM,IAAa,EAAM,YAAY,GAC/B,IAAS,EAAW,MAAM,IAAI,GAC9B,IAAO,EAAO,MAAM,IACpB,IAAO,EAAO;CACpB,IAAI,MAAS,KAAA,KAAa,EAAW,QAAQ,IAAI,MAAM,EAAW,YAAY,IAAI,GACjF,MAAU,MAAM,yBAAyB,GAAO;CAEjD,IAAM,IAAY,EA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,EAAM,WAAW,IAAI;EACxB,IAAM,IAAmB,CAAC;EAC1B,KAAK,IAAI,IAAQ,GAAG,IAAQ,EAAM,QAAQ,KAAS,GAAG;GACrD,IAAM,IAAO,EAAM,MAAU,GACvB,IAAQ,EAAM,IAAQ,MAAM;GAClC,EAAO,MAAO,KAAQ,IAAK,EAAA,CAAO,SAAS,EAAE,CAAC;EAC/C;EACA,OAAO,EAAO,KAAK,GAAG;CACvB;CACA,MAAU,MAAM,kCAAkC,EAAM,QAAQ;AACjE;;;;;;;;;;;;;;;;;AAkBA,SAAgB,wBAAwB,GAA2B;CAElE,IAAM,IAAS,sBAAsB,CAAK,GACpC,IAAO,IAAI,WAAW,EAAO,MAAM;CAEzC,OADA,EAAK,KAAK,GAAI,GACP;AACR;;AAGA,SAAS,iBAAiB,GAA2B;CACpD,IAAM,IAAW,WAAW,CAAK,GAC3B,oBAAQ,IAAI,WAAW,EAAE;CAM/B,OALA,EAAS,SAAS,GAAS,MAAU;EACpC,IAAM,IAAS,OAAO,SAAS,GAAS,EAAE;EAE1C,AADA,EAAM,IAAQ,KAAK,KAAU,GAC7B,EAAM,IAAQ,IAAI,KAAK,IAAS;CACjC,CAAC,GACM;AACR"}
1
+ {"version":3,"file":"ip.js","names":[],"sources":["../../../src/internal/shared/ip.ts"],"sourcesContent":["/**\n * IP address parsing and normalization helpers shared by parsing,\n * validation, and identity matching flows.\n *\n * These utilities keep IPv4 and IPv6 handling consistent for SAN\n * matching and name constraint evaluation.\n *\n * @example\n * ```ts\n * import {\n * \tallOnesMaskForIpAddress,\n * \tdecodeIpAddress,\n * \texpandIpv6,\n * \tnormalizeIpAddress,\n * \tparseIpAddressToBytes,\n * } from './ip.ts';\n *\n * normalizeIpAddress('::1');\n * // '0000:0000:0000:0000:0000:0000:0000:0001'\n *\n * expandIpv6('2001:db8::1');\n * // ['2001', '0db8', '0000', '0000', '0000', '0000', '0000', '0001']\n *\n * Array.from(parseIpAddressToBytes('127.0.0.1'));\n * // [127, 0, 0, 1]\n *\n * decodeIpAddress(Uint8Array.of(127, 0, 0, 1));\n * // '127.0.0.1'\n *\n * Array.from(allOnesMaskForIpAddress('127.0.0.1'));\n * // [255, 255, 255, 255]\n * ```\n *\n * @module\n */\n\n/** Matches a single valid IPv6 hex segment (1–4 hex digits). */\nconst IPV6_SEGMENT = /^[0-9a-f]{1,4}$/;\n\n/**\n * Normalizes an IP address string for comparison.\\\n * IPv4 addresses pass through unchanged; IPv6 addresses expand to 8\n * colon-separated zero-padded groups.\n *\n * @example\n * ```ts\n * normalizeIpAddress('::1');\n * // '0000:0000:0000:0000:0000:0000:0000:0001'\n * ```\n *\n * @param value IPv4 or IPv6 address string to normalize.\n * @returns The normalized address string.\n */\nexport function normalizeIpAddress(value: string): string {\n\tif (!value.includes(':')) {\n\t\treturn value;\n\t}\n\treturn expandIpv6(value).join(':');\n}\n\n/**\n * Expands an IPv6 address, including `::` shorthand, into exactly 8\n * zero-padded four-character hex segments.\\\n * Throws on malformed input.\n *\n * @example\n * ```ts\n * expandIpv6('2001:db8::1');\n * // ['2001', '0db8', '0000', '0000', '0000', '0000', '0000', '0001']\n * ```\n *\n * @param value IPv6 address string to expand.\n * @returns The 8 normalized IPv6 segments.\n */\nexport function expandIpv6(value: string): readonly string[] {\n\tconst normalized = value.toLowerCase();\n\tconst pieces = normalized.split('::');\n\tconst head = pieces[0] ?? '';\n\tconst tail = pieces[1];\n\tif (tail !== undefined && normalized.indexOf('::') !== normalized.lastIndexOf('::')) {\n\t\tthrow new Error(`Invalid IPv6 address: ${value}`);\n\t}\n\tconst headParts = head.length > 0 ? head.split(':') : [];\n\tconst tailParts = tail !== undefined && tail.length > 0 ? tail.split(':') : [];\n\tconst missing = 8 - (headParts.length + tailParts.length);\n\tif ((tail === undefined && headParts.length !== 8) || missing < 0) {\n\t\tthrow new Error(`Invalid IPv6 address: ${value}`);\n\t}\n\tconst zeroes = Array.from({ length: missing }, () => '0');\n\tconst parts = tail === undefined ? headParts : [...headParts, ...zeroes, ...tailParts];\n\tif (parts.length !== 8) {\n\t\tthrow new Error(`Invalid IPv6 address: ${value}`);\n\t}\n\treturn parts.map((segment) => {\n\t\tif (!IPV6_SEGMENT.test(segment)) {\n\t\t\tthrow new Error(`Invalid IPv6 address: ${value}`);\n\t\t}\n\t\treturn segment.padStart(4, '0');\n\t});\n}\n\n/**\n * Parses an IPv4 or IPv6 address string into raw bytes.\\\n * Returns 4 bytes for IPv4 and 16 bytes for IPv6.\n *\n * Suitable for encoding into SAN iPAddress octets or name-constraint ranges.\n *\n * @example\n * ```ts\n * Array.from(parseIpAddressToBytes('127.0.0.1'));\n * // [127, 0, 0, 1]\n * ```\n *\n * @param value IPv4 or IPv6 address string to parse.\n * @returns The raw address bytes.\n */\nexport function parseIpAddressToBytes(value: string): Uint8Array {\n\tif (value.includes(':')) {\n\t\treturn parseIpv6ToBytes(value);\n\t}\n\tconst segments = value.split('.');\n\tif (segments.length !== 4) {\n\t\tthrow new Error(`Invalid IPv4 address: ${value}`);\n\t}\n\treturn Uint8Array.from(\n\t\tsegments.map((segment) => {\n\t\t\tif (segment.length === 0) {\n\t\t\t\tthrow new Error(`Invalid IPv4 address: ${value}`);\n\t\t\t}\n\t\t\tconst parsed = Number(segment);\n\t\t\tif (!Number.isInteger(parsed) || parsed < 0 || parsed > 255) {\n\t\t\t\tthrow new Error(`Invalid IPv4 address: ${value}`);\n\t\t\t}\n\t\t\treturn parsed;\n\t\t}),\n\t);\n}\n\n/**\n * Converts raw IP address bytes back to a human-readable string.\\\n * Returns dotted-decimal IPv4 for 4-byte input and colon-separated hex\n * IPv6 for 16-byte input.\n *\n * Throws on any other length.\n *\n * @example\n * ```ts\n * decodeIpAddress(Uint8Array.of(127, 0, 0, 1));\n * // '127.0.0.1'\n * ```\n *\n * @param bytes Raw IPv4 or IPv6 address bytes.\n * @returns The decoded address string.\n */\nexport function decodeIpAddress(bytes: Uint8Array): string {\n\tif (bytes.length === 4) {\n\t\treturn Array.from(bytes, (value) => String(value)).join('.');\n\t}\n\tif (bytes.length === 16) {\n\t\tconst groups: string[] = [];\n\t\tfor (let index = 0; index < bytes.length; index += 2) {\n\t\t\tconst left = bytes[index] ?? 0;\n\t\t\tconst right = bytes[index + 1] ?? 0;\n\t\t\tgroups.push(((left << 8) | right).toString(16));\n\t\t}\n\t\treturn groups.join(':');\n\t}\n\tthrow new Error(`Unsupported IP address length: ${bytes.length}`);\n}\n\n/**\n * Returns an all-ones (`0xff`) mask of the appropriate length for the\n * given IP address string.\\\n * Returns 4 bytes for IPv4 and 16 bytes for IPv6.\n *\n * Used to build name-constraint subnet masks that match a single host.\n *\n * @example\n * ```ts\n * Array.from(allOnesMaskForIpAddress('127.0.0.1'));\n * // [255, 255, 255, 255]\n * ```\n *\n * @param value IPv4 or IPv6 address string used to choose mask length.\n * @returns An all-ones mask for the same address family.\n */\nexport function allOnesMaskForIpAddress(value: string): Uint8Array {\n\t// Validate and parse the IP address to determine its type\n\tconst parsed = parseIpAddressToBytes(value);\n\tconst mask = new Uint8Array(parsed.length);\n\tmask.fill(0xff);\n\treturn mask;\n}\n\n/** Parses an IPv6 address string into a 16-byte `Uint8Array`. */\nfunction parseIpv6ToBytes(value: string): Uint8Array {\n\tconst expanded = expandIpv6(value);\n\tconst bytes = new Uint8Array(16);\n\texpanded.forEach((segment, index) => {\n\t\tconst parsed = Number.parseInt(segment, 16);\n\t\tbytes[index * 2] = parsed >> 8;\n\t\tbytes[index * 2 + 1] = parsed & 0xff;\n\t});\n\treturn bytes;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCA,MAAM,IAAe;;;;;;;;;;;;;;;AAgBrB,SAAgB,mBAAmB,GAAuB;CAIzD,OAHK,EAAM,SAAS,GAAG,IAGhB,WAAW,CAAK,CAAC,CAAC,KAAK,GAAG,IAFzB;AAGT;;;;;;;;;;;;;;;AAgBA,SAAgB,WAAW,GAAkC;CAC5D,IAAM,IAAa,EAAM,YAAY,GAC/B,IAAS,EAAW,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