micro509 0.11.0 → 0.13.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.
- package/README.md +17 -3
- package/dist/der/der.d.ts +127 -0
- package/dist/der/der.js +171 -0
- package/dist/der/der.js.map +1 -0
- package/dist/der.d.ts +4 -0
- package/dist/der.js +4 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/internal/asn1/asn1.d.ts +20 -0
- package/dist/internal/asn1/asn1.js +60 -28
- package/dist/internal/asn1/asn1.js.map +1 -1
- package/dist/internal/asn1/der.d.ts +171 -0
- package/dist/internal/asn1/der.js +101 -36
- package/dist/internal/asn1/der.js.map +1 -1
- package/dist/internal/asn1/oids.js +22 -0
- package/dist/internal/asn1/oids2.js +9 -2
- package/dist/internal/asn1/oids2.js.map +1 -1
- package/dist/internal/shared/dn.js +143 -13
- package/dist/internal/shared/dn.js.map +1 -1
- package/dist/internal/shared/rfc3454-tables.js +1782 -0
- package/dist/internal/shared/rfc3454-tables.js.map +1 -0
- package/dist/internal/verify/name-constraints-engine.js +47 -15
- package/dist/internal/verify/name-constraints-engine.js.map +1 -1
- package/dist/internal/verify/policy-engine.js +86 -95
- package/dist/internal/verify/policy-engine.js.map +1 -1
- package/dist/internal/verify/verify-path.js +18 -14
- package/dist/internal/verify/verify-path.js.map +1 -1
- package/dist/internal/x509/extension-bits.js +1 -1
- package/dist/internal/x509/extension-bits.js.map +1 -1
- package/dist/internal/x509/name-fields.js +19 -4
- package/dist/internal/x509/name-fields.js.map +1 -1
- package/dist/revocation/chain.js +110 -55
- package/dist/revocation/chain.js.map +1 -1
- package/dist/revocation/crl.js +2 -2
- package/dist/revocation/crl.js.map +1 -1
- package/dist/revocation/ocsp.js +2 -2
- package/dist/revocation/ocsp.js.map +1 -1
- package/dist/verify/verify.d.ts +14 -1
- package/dist/verify/verify.js +39 -32
- package/dist/verify/verify.js.map +1 -1
- package/dist/x509/extensions.d.ts +1 -1
- package/dist/x509/extensions.js.map +1 -1
- package/dist/x509/fingerprint.d.ts +3 -0
- package/dist/x509/fingerprint.js +3 -0
- package/dist/x509/fingerprint.js.map +1 -1
- package/dist/x509/name-text.d.ts +50 -0
- package/dist/x509/name-text.js +111 -0
- package/dist/x509/name-text.js.map +1 -0
- package/dist/x509/parse.d.ts +3 -0
- package/dist/x509/parse.js +10 -1
- package/dist/x509/parse.js.map +1 -1
- package/dist/x509.d.ts +2 -1
- package/dist/x509.js +2 -1
- package/package.json +46 -25
|
@@ -3,11 +3,10 @@ import { readElement as e } from "./der.js";
|
|
|
3
3
|
/** Shared UTF-8 text decoder for ASN.1 string types. */
|
|
4
4
|
const t = new TextDecoder("utf-8", { fatal: !0 }), n = /^[A-Za-z0-9 '()+,\-./:=?]*$/u;
|
|
5
5
|
/**
|
|
6
|
-
* Decodes a DER-encoded OBJECT IDENTIFIER value into its dotted-decimal string
|
|
7
|
-
* form (e.g. `"1.2.840.113549.1.1.1"`).
|
|
6
|
+
* Decodes a DER-encoded OBJECT IDENTIFIER value into its dotted-decimal string form.
|
|
8
7
|
*
|
|
9
|
-
*
|
|
10
|
-
* continuation octets.
|
|
8
|
+
* @example "1.2.840.113549.1.1.1"
|
|
9
|
+
* @throws on empty input, truncated multi-byte sub-identifiers, or incomplete continuation octets.
|
|
11
10
|
*/
|
|
12
11
|
function decodeObjectIdentifier(e) {
|
|
13
12
|
if (e.length === 0) throw Error("OID is empty");
|
|
@@ -23,7 +22,7 @@ function toHex(e) {
|
|
|
23
22
|
return Array.from(e, (e) => e.toString(16).padStart(2, "0")).join("");
|
|
24
23
|
}
|
|
25
24
|
/**
|
|
26
|
-
* Copies a
|
|
25
|
+
* Copies a {@linkcode Uint8Array} into a standalone {@linkcode ArrayBuffer} suitable for
|
|
27
26
|
* Web Crypto operations that require a non-shared backing buffer.
|
|
28
27
|
*/
|
|
29
28
|
function toArrayBuffer(e) {
|
|
@@ -31,11 +30,10 @@ function toArrayBuffer(e) {
|
|
|
31
30
|
return new Uint8Array(t).set(e), t;
|
|
32
31
|
}
|
|
33
32
|
/**
|
|
34
|
-
* Iterates through all direct child TLV elements inside a constructed
|
|
35
|
-
*
|
|
33
|
+
* Iterates through all direct child TLV elements inside a constructed {@linkcode DerElement}
|
|
34
|
+
* (SEQUENCE, SET, or context-tagged container).
|
|
36
35
|
*
|
|
37
|
-
*
|
|
38
|
-
* leftover data between the last child and the parent end offset.
|
|
36
|
+
* @throws if any child overflows the parent boundary or if there is leftover data between the last child and the parent end offset.
|
|
39
37
|
*/
|
|
40
38
|
function childrenOf(t, n) {
|
|
41
39
|
let r = [], i = n.start;
|
|
@@ -48,7 +46,7 @@ function childrenOf(t, n) {
|
|
|
48
46
|
return r;
|
|
49
47
|
}
|
|
50
48
|
/**
|
|
51
|
-
* Asserts that a value is not `undefined`, returning it narrowed
|
|
49
|
+
* Asserts that a value is not `undefined`, returning it narrowed.\
|
|
52
50
|
* Used to enforce the presence of mandatory ASN.1 fields during parsing.
|
|
53
51
|
*
|
|
54
52
|
* @param label Human-readable field name included in the error message on failure.
|
|
@@ -58,10 +56,11 @@ function requireElement(e, t) {
|
|
|
58
56
|
return e;
|
|
59
57
|
}
|
|
60
58
|
/**
|
|
61
|
-
* Strips the leading "unused bits" octet from a BIT STRING element,
|
|
62
|
-
* returning just the payload bytes. Throws if the tag is not `0x03`.
|
|
59
|
+
* Strips the leading "unused bits" octet from a BIT STRING element, returning just the payload bytes.
|
|
63
60
|
*
|
|
64
61
|
* Used to extract signature values and public keys from their BIT STRING wrapper.
|
|
62
|
+
*
|
|
63
|
+
* @throws if the tag is not `0x03`.
|
|
65
64
|
*/
|
|
66
65
|
function extractBitStringValue(e) {
|
|
67
66
|
if (e.tag !== 3) throw Error("Expected BIT STRING");
|
|
@@ -71,11 +70,35 @@ function extractBitStringValue(e) {
|
|
|
71
70
|
return e.value.slice(1);
|
|
72
71
|
}
|
|
73
72
|
/**
|
|
74
|
-
*
|
|
75
|
-
*
|
|
73
|
+
* Decodes a BIT STRING element into its payload and unused-bit count.
|
|
74
|
+
*
|
|
75
|
+
* Accepts a non-zero unused-bit count, so it reads flag strings such as {@linkcode KeyUsage},
|
|
76
|
+
* which {@linkcode extractBitStringValue} rejects.
|
|
77
|
+
*
|
|
78
|
+
* Padding bits that {@linkcode https://www.itu.int/rec/T-REC-X.690-202102-I/en | X.690 §11.2.1} requires to be zero are reported through
|
|
79
|
+
* {@linkcode DerBitString.nonZeroPadding} rather than rejected, so a caller can decide.
|
|
80
|
+
*/
|
|
81
|
+
function decodeBitString(e) {
|
|
82
|
+
if (e.tag !== 3) throw Error("Expected BIT STRING");
|
|
83
|
+
let t = e.value[0];
|
|
84
|
+
if (t === void 0 || t > 7) throw Error("Invalid BIT STRING");
|
|
85
|
+
let n = e.value.slice(1);
|
|
86
|
+
if (n.length === 0 && t !== 0) throw Error("Invalid BIT STRING");
|
|
87
|
+
let r = (1 << t) - 1;
|
|
88
|
+
return {
|
|
89
|
+
bytes: n,
|
|
90
|
+
unusedBits: t,
|
|
91
|
+
nonZeroPadding: t > 0 && n.length > 0 && ((n[n.length - 1] ?? 0) & r) !== 0
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Parses a {@linkcode DerElement} with tag UTCTime (`0x17`) or GeneralizedTime (`0x18`) into a {@linkcode Date}.
|
|
76
96
|
*
|
|
77
|
-
* For UTCTime, the two-digit year is interpreted per RFC 5280:
|
|
78
|
-
*
|
|
97
|
+
* For UTCTime, the two-digit year is interpreted per RFC 5280:
|
|
98
|
+
* - values >= 50 map to 19xx,
|
|
99
|
+
* - values < 50 map to 20xx.
|
|
100
|
+
*
|
|
101
|
+
* @throws on unrecognized time tags.
|
|
79
102
|
*/
|
|
80
103
|
function parseTime(e) {
|
|
81
104
|
if (e.tag === 23) {
|
|
@@ -94,24 +117,27 @@ function parseTime(e) {
|
|
|
94
117
|
}
|
|
95
118
|
/**
|
|
96
119
|
* Decodes a big-endian unsigned byte sequence into a JavaScript `number`.
|
|
97
|
-
*
|
|
98
|
-
* boundary for lossless arithmetic.
|
|
120
|
+
*
|
|
121
|
+
* @throws if the value exceeds {@linkcode Number.MAX_SAFE_INTEGER}, the boundary for lossless arithmetic.
|
|
99
122
|
*/
|
|
100
123
|
function decodeIntegerNumber(e) {
|
|
101
124
|
let t = e[0];
|
|
102
125
|
if (t === void 0) throw Error("INTEGER is empty");
|
|
103
126
|
if (t & 128) throw Error("INTEGER must be non-negative");
|
|
104
127
|
if (e.length > 1 && t === 0 && !((e[1] ?? 0) & 128)) throw Error("INTEGER must use minimal encoding");
|
|
105
|
-
if (e.length > 6) throw Error(`Integer too large for safe number (${e.length} bytes)`);
|
|
106
128
|
let n = 0;
|
|
107
|
-
for (let t of e)
|
|
129
|
+
for (let t of e) {
|
|
130
|
+
if (n > Math.floor((2 ** 53 - 1 - t) / 256)) throw Error(`Integer too large for safe number (${e.length} bytes)`);
|
|
131
|
+
n = n * 256 + t;
|
|
132
|
+
}
|
|
108
133
|
return n;
|
|
109
134
|
}
|
|
110
135
|
/**
|
|
111
|
-
* Like {@linkcode decodeIntegerNumber}, but optionally rewrites thrown error
|
|
112
|
-
* messages with a caller-specific field label.
|
|
136
|
+
* Like {@linkcode decodeIntegerNumber}, but optionally rewrites thrown error messages with a caller-specific field label.
|
|
113
137
|
*
|
|
138
|
+
* @param bytes DER INTEGER content octets to decode.
|
|
114
139
|
* @param label Field name for error messages (defaults to `"INTEGER"`).
|
|
140
|
+
* @throws if the value is empty, negative, non-minimally encoded, or exceeds {@linkcode Number.MAX_SAFE_INTEGER}.
|
|
115
141
|
*/
|
|
116
142
|
function decodeNonNegativeIntegerNumber(e, t = "INTEGER") {
|
|
117
143
|
if (t === "INTEGER") return decodeIntegerNumber(e);
|
|
@@ -128,7 +154,7 @@ function decodeNonNegativeIntegerNumber(e, t = "INTEGER") {
|
|
|
128
154
|
}
|
|
129
155
|
}
|
|
130
156
|
/**
|
|
131
|
-
* Converts a hex string (even or odd length) to a
|
|
157
|
+
* Converts a hex string (even or odd length) to a {@linkcode Uint8Array}.
|
|
132
158
|
* Odd-length strings are left-padded with a zero nibble.
|
|
133
159
|
*/
|
|
134
160
|
function hexToBytes(e) {
|
|
@@ -170,10 +196,16 @@ function decodeOidSubidentifier(e, t) {
|
|
|
170
196
|
throw Error("Malformed OID: incomplete continuation");
|
|
171
197
|
}
|
|
172
198
|
/**
|
|
173
|
-
* Decodes a DER string element by tag.
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
199
|
+
* Decodes a DER string element by tag.
|
|
200
|
+
*
|
|
201
|
+
* Supports:
|
|
202
|
+
* - UTF8String (`0x0c`),
|
|
203
|
+
* - PrintableString (`0x13`),
|
|
204
|
+
* - IA5String (`0x16`),
|
|
205
|
+
* - UniversalString (`0x1c`), and
|
|
206
|
+
* - BMPString (`0x1e`).
|
|
207
|
+
*
|
|
208
|
+
* @throws on unsupported string tags.
|
|
177
209
|
*/
|
|
178
210
|
function decodeString(e, t) {
|
|
179
211
|
switch (e) {
|
|
@@ -234,6 +266,6 @@ function decodeUniversalString(e) {
|
|
|
234
266
|
return t;
|
|
235
267
|
}
|
|
236
268
|
//#endregion
|
|
237
|
-
export { childrenOf, decodeBoolean, decodeIntegerNumber, decodeNonNegativeIntegerNumber, decodeObjectIdentifier, decodeString, extractBitStringValue, hexToBytes, parseTime, requireElement, toArrayBuffer, toHex };
|
|
269
|
+
export { childrenOf, decodeBitString, decodeBoolean, decodeIntegerNumber, decodeNonNegativeIntegerNumber, decodeObjectIdentifier, decodeString, extractBitStringValue, hexToBytes, parseTime, requireElement, toArrayBuffer, toHex };
|
|
238
270
|
|
|
239
271
|
//# sourceMappingURL=asn1.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asn1.js","names":[],"sources":["../../../src/internal/asn1/asn1.ts"],"sourcesContent":["/**\n * ASN.1 decoding helpers used by the higher-level DER, parse, and crypto modules.\n *\n * These utilities stay intentionally small and strict so malformed input fails early and\n * higher-level modules can build on one consistent decoding layer.\n *\n * @module\n */\n\nimport type { DerElement } from '#micro509/internal/asn1/der';\nimport { readElement } from '#micro509/internal/asn1/der';\n\n/** Shared UTF-8 text decoder for ASN.1 string types. */\nconst textDecoder = new TextDecoder('utf-8', { fatal: true });\n\nconst PRINTABLE_STRING_PATTERN = /^[A-Za-z0-9 '()+,\\-./:=?]*$/u;\n\n/**\n * Decodes a DER-encoded OBJECT IDENTIFIER value into its dotted-decimal string\n * form (e.g. `\"1.2.840.113549.1.1.1\"`).\n *\n * Throws on empty input, truncated multi-byte sub-identifiers, or incomplete\n * continuation octets.\n */\nexport function decodeObjectIdentifier(bytes: Uint8Array): string {\n\tif (bytes.length === 0) {\n\t\tthrow new Error('OID is empty');\n\t}\n\tconst firstSubidentifier = decodeOidSubidentifier(bytes, 0);\n\tlet offset = firstSubidentifier.nextOffset;\n\tconst values =\n\t\tfirstSubidentifier.value < 40\n\t\t\t? [0, firstSubidentifier.value]\n\t\t\t: firstSubidentifier.value < 80\n\t\t\t\t? [1, firstSubidentifier.value - 40]\n\t\t\t\t: [2, firstSubidentifier.value - 80];\n\twhile (offset < bytes.length) {\n\t\tconst subidentifier = decodeOidSubidentifier(bytes, offset);\n\t\tvalues.push(subidentifier.value);\n\t\toffset = subidentifier.nextOffset;\n\t}\n\treturn values.join('.');\n}\n\n/** Converts raw bytes to a lowercase hex string with no separator. */\nexport function toHex(bytes: Uint8Array): string {\n\treturn Array.from(bytes, (value) => value.toString(16).padStart(2, '0')).join('');\n}\n\n/**\n * Copies a `Uint8Array` into a standalone `ArrayBuffer` suitable for\n * Web Crypto operations that require a non-shared backing buffer.\n */\nexport function toArrayBuffer(bytes: Uint8Array): ArrayBuffer {\n\tconst out = new ArrayBuffer(bytes.length);\n\tnew Uint8Array(out).set(bytes);\n\treturn out;\n}\n\n/**\n * Iterates through all direct child TLV elements inside a constructed\n * {@linkcode DerElement} (SEQUENCE, SET, or context-tagged container).\n *\n * Throws if any child overflows the parent boundary or if there is\n * leftover data between the last child and the parent end offset.\n */\nexport function childrenOf(source: Uint8Array, parent: DerElement): DerElement[] {\n\tconst children: DerElement[] = [];\n\tlet offset = parent.start;\n\twhile (offset < parent.end) {\n\t\tconst child = readElement(source, offset);\n\t\tif (child.end > parent.end) {\n\t\t\tthrow new Error('DER child exceeds parent length');\n\t\t}\n\t\tchildren.push(child);\n\t\toffset = child.end;\n\t}\n\tif (offset !== parent.end) {\n\t\tthrow new Error('Malformed DER container');\n\t}\n\treturn children;\n}\n\n/**\n * Asserts that a value is not `undefined`, returning it narrowed.\n * Used to enforce the presence of mandatory ASN.1 fields during parsing.\n *\n * @param label Human-readable field name included in the error message on failure.\n */\nexport function requireElement<T>(value: T | undefined, label: string): T {\n\tif (value === undefined) {\n\t\tthrow new Error(`Missing ${label}`);\n\t}\n\treturn value;\n}\n\n/**\n * Strips the leading \"unused bits\" octet from a BIT STRING element,\n * returning just the payload bytes. Throws if the tag is not `0x03`.\n *\n * Used to extract signature values and public keys from their BIT STRING wrapper.\n */\nexport function extractBitStringValue(element: DerElement): Uint8Array {\n\tif (element.tag !== 0x03) {\n\t\tthrow new Error('Expected BIT STRING');\n\t}\n\tconst unusedBits = element.value[0];\n\tif (unusedBits === undefined || unusedBits > 7) {\n\t\tthrow new Error('Invalid BIT STRING');\n\t}\n\tif (unusedBits !== 0) {\n\t\tthrow new Error('BIT STRING must have zero unused bits');\n\t}\n\treturn element.value.slice(1);\n}\n\n/**\n * Parses a {@linkcode DerElement} with tag UTCTime (`0x17`) or GeneralizedTime (`0x18`)\n * into a `Date`.\n *\n * For UTCTime, the two-digit year is interpreted per RFC 5280: values >= 50 map\n * to 19xx, values < 50 map to 20xx. Throws on unrecognized time tags.\n */\nexport function parseTime(element: DerElement): Date {\n\tif (element.tag === 0x17) {\n\t\tconst value = decodeUtf8Text(element.value, 'UTCTime');\n\t\tconst match = /^(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})Z$/.exec(value);\n\t\tif (match === null) {\n\t\t\tthrow new Error('Invalid UTCTime');\n\t\t}\n\t\tconst yearText = requireElement(match[1], 'UTCTime year');\n\t\tconst monthText = requireElement(match[2], 'UTCTime month');\n\t\tconst dayText = requireElement(match[3], 'UTCTime day');\n\t\tconst hourText = requireElement(match[4], 'UTCTime hour');\n\t\tconst minuteText = requireElement(match[5], 'UTCTime minute');\n\t\tconst secondText = requireElement(match[6], 'UTCTime second');\n\t\tconst year = Number.parseInt(yearText, 10);\n\t\treturn buildStrictUtcDate(\n\t\t\tyear >= 50 ? 1900 + year : 2000 + year,\n\t\t\tmonthText,\n\t\t\tdayText,\n\t\t\thourText,\n\t\t\tminuteText,\n\t\t\tsecondText,\n\t\t\t'UTCTime',\n\t\t);\n\t}\n\tif (element.tag === 0x18) {\n\t\tconst value = decodeUtf8Text(element.value, 'GeneralizedTime');\n\t\tconst match = /^(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})Z$/.exec(value);\n\t\tif (match === null) {\n\t\t\tthrow new Error('Invalid GeneralizedTime');\n\t\t}\n\t\tconst yearText = requireElement(match[1], 'GeneralizedTime year');\n\t\tconst monthText = requireElement(match[2], 'GeneralizedTime month');\n\t\tconst dayText = requireElement(match[3], 'GeneralizedTime day');\n\t\tconst hourText = requireElement(match[4], 'GeneralizedTime hour');\n\t\tconst minuteText = requireElement(match[5], 'GeneralizedTime minute');\n\t\tconst secondText = requireElement(match[6], 'GeneralizedTime second');\n\t\treturn buildStrictUtcDate(\n\t\t\tNumber.parseInt(yearText, 10),\n\t\t\tmonthText,\n\t\t\tdayText,\n\t\t\thourText,\n\t\t\tminuteText,\n\t\t\tsecondText,\n\t\t\t'GeneralizedTime',\n\t\t);\n\t}\n\tthrow new Error(`Unsupported time tag: ${element.tag}`);\n}\n\n/**\n * Decodes a big-endian unsigned byte sequence into a JavaScript `number`.\n * Throws if the value exceeds 6 bytes (48 bits), which is the safe-integer\n * boundary for lossless arithmetic.\n */\nexport function decodeIntegerNumber(bytes: Uint8Array): number {\n\tconst first = bytes[0];\n\tif (first === undefined) {\n\t\tthrow new Error('INTEGER is empty');\n\t}\n\tif ((first & 0x80) !== 0) {\n\t\tthrow new Error('INTEGER must be non-negative');\n\t}\n\tif (bytes.length > 1 && first === 0 && ((bytes[1] ?? 0) & 0x80) === 0) {\n\t\tthrow new Error('INTEGER must use minimal encoding');\n\t}\n\tif (bytes.length > 6) {\n\t\tthrow new Error(`Integer too large for safe number (${bytes.length} bytes)`);\n\t}\n\tlet value = 0;\n\tfor (const byte of bytes) {\n\t\tvalue = value * 256 + byte;\n\t}\n\treturn value;\n}\n\n/**\n * Like {@linkcode decodeIntegerNumber}, but optionally rewrites thrown error\n * messages with a caller-specific field label.\n *\n * @param label Field name for error messages (defaults to `\"INTEGER\"`).\n */\nexport function decodeNonNegativeIntegerNumber(bytes: Uint8Array, label = 'INTEGER'): number {\n\tif (label === 'INTEGER') {\n\t\treturn decodeIntegerNumber(bytes);\n\t}\n\ttry {\n\t\treturn decodeIntegerNumber(bytes);\n\t} catch (error) {\n\t\tif (!(error instanceof Error)) {\n\t\t\tthrow error;\n\t\t}\n\t\tswitch (error.message) {\n\t\t\tcase 'INTEGER is empty':\n\t\t\t\tthrow new Error(`${label} is empty`);\n\t\t\tcase 'INTEGER must be non-negative':\n\t\t\t\tthrow new Error(`${label} must be non-negative`);\n\t\t\tcase 'INTEGER must use minimal encoding':\n\t\t\t\tthrow new Error(`${label} must use minimal encoding`);\n\t\t\tdefault:\n\t\t\t\tif (error.message.startsWith('Integer too large for safe number')) {\n\t\t\t\t\tthrow new Error(`${label} too large for safe number (${bytes.length} bytes)`);\n\t\t\t\t}\n\t\t\t\tthrow error;\n\t\t}\n\t}\n}\n\n/**\n * Converts a hex string (even or odd length) to a `Uint8Array`.\n * Odd-length strings are left-padded with a zero nibble.\n */\nexport function hexToBytes(value: string): Uint8Array {\n\tconst normalized = value.length % 2 === 0 ? value : `0${value}`;\n\tconst out = new Uint8Array(normalized.length / 2);\n\tfor (let index = 0; index < out.length; index += 1) {\n\t\tconst chunk = normalized.slice(index * 2, index * 2 + 2);\n\t\tif (!/^[0-9a-fA-F]{2}$/.test(chunk)) {\n\t\t\tthrow new Error(`Invalid hex byte: ${chunk}`);\n\t\t}\n\t\tconst parsed = Number.parseInt(chunk, 16);\n\t\tout[index] = parsed;\n\t}\n\treturn out;\n}\n\n/** Decodes a DER BOOLEAN value: any non-zero first byte is `true`. */\nexport function decodeBoolean(bytes: Uint8Array): boolean {\n\tif (bytes.length !== 1) {\n\t\tthrow new Error('BOOLEAN must contain exactly one octet');\n\t}\n\tconst value = bytes[0];\n\tif (value !== 0x00 && value !== 0xff) {\n\t\tthrow new Error('BOOLEAN must use DER encoding');\n\t}\n\treturn value === 0xff;\n}\n\nfunction buildStrictUtcDate(\n\tyear: number,\n\tmonthText: string,\n\tdayText: string,\n\thourText: string,\n\tminuteText: string,\n\tsecondText: string,\n\tlabel: 'UTCTime' | 'GeneralizedTime',\n): Date {\n\tconst month = Number.parseInt(monthText, 10);\n\tconst day = Number.parseInt(dayText, 10);\n\tconst hour = Number.parseInt(hourText, 10);\n\tconst minute = Number.parseInt(minuteText, 10);\n\tconst second = Number.parseInt(secondText, 10);\n\tconst date = new Date(Date.UTC(year, month - 1, day, hour, minute, second));\n\tif (\n\t\tNumber.isNaN(date.getTime()) ||\n\t\tdate.getUTCFullYear() !== year ||\n\t\tdate.getUTCMonth() !== month - 1 ||\n\t\tdate.getUTCDate() !== day ||\n\t\tdate.getUTCHours() !== hour ||\n\t\tdate.getUTCMinutes() !== minute ||\n\t\tdate.getUTCSeconds() !== second\n\t) {\n\t\tthrow new Error(`Invalid ${label}`);\n\t}\n\treturn date;\n}\n\nfunction decodeOidSubidentifier(\n\tbytes: Uint8Array,\n\tstart: number,\n): {\n\treadonly value: number;\n\treadonly nextOffset: number;\n} {\n\tconst first = bytes[start];\n\tif (first === undefined) {\n\t\tthrow new Error('Malformed OID');\n\t}\n\tif (first === 0x80) {\n\t\tthrow new Error('Malformed OID: non-minimal base-128 encoding');\n\t}\n\tlet value = 0;\n\tlet offset = start;\n\tfor (; offset < bytes.length; offset += 1) {\n\t\tconst next = bytes[offset];\n\t\tif (next === undefined) {\n\t\t\tthrow new Error('Malformed OID');\n\t\t}\n\t\tconst digit = next & 0x7f;\n\t\tif (value > Math.floor((Number.MAX_SAFE_INTEGER - digit) / 128)) {\n\t\t\tthrow new Error('Malformed OID: overflow/non-minimal or too-large subidentifier');\n\t\t}\n\t\tvalue = value * 128 + digit;\n\t\tif ((next & 0x80) === 0) {\n\t\t\treturn {\n\t\t\t\tvalue,\n\t\t\t\tnextOffset: offset + 1,\n\t\t\t};\n\t\t}\n\t}\n\tthrow new Error('Malformed OID: incomplete continuation');\n}\n\n/**\n * Decodes a DER string element by tag. Supports UTF8String (`0x0c`),\n * PrintableString (`0x13`), IA5String (`0x16`), UniversalString (`0x1c`),\n * and BMPString (`0x1e`).\n * Throws on unsupported string tags.\n */\nexport function decodeString(tag: number, bytes: Uint8Array): string {\n\tswitch (tag) {\n\t\tcase 0x0c:\n\t\t\treturn decodeUtf8Text(bytes, 'UTF8String');\n\t\tcase 0x13:\n\t\t\treturn decodePrintableString(bytes);\n\t\tcase 0x16:\n\t\t\treturn decodeIa5String(bytes);\n\t\tcase 0x14:\n\t\t\tthrow new Error('Unsupported string tag: 20 (TeletexString)');\n\t\tcase 0x1c:\n\t\t\treturn decodeUniversalString(bytes);\n\t\tcase 0x1e:\n\t\t\treturn decodeBmpString(bytes);\n\t\tdefault:\n\t\t\tthrow new Error(`Unsupported string tag: ${tag}`);\n\t}\n}\n\nfunction decodeUtf8Text(\n\tbytes: Uint8Array,\n\tlabel: 'GeneralizedTime' | 'UTCTime' | 'UTF8String',\n): string {\n\ttry {\n\t\treturn textDecoder.decode(bytes);\n\t} catch (error) {\n\t\tif (error instanceof Error) {\n\t\t\tthrow new Error(`Invalid ${label}: invalid UTF-8`, { cause: error });\n\t\t}\n\t\tthrow error;\n\t}\n}\n\nfunction decodeAsciiString(bytes: Uint8Array, label: 'IA5String' | 'PrintableString'): string {\n\tlet value = '';\n\tfor (const byte of bytes) {\n\t\tif (byte > 0x7f) {\n\t\t\tthrow new Error(`Invalid ${label}: contains non-ASCII bytes`);\n\t\t}\n\t\tvalue += String.fromCharCode(byte);\n\t}\n\treturn value;\n}\n\nfunction decodeIa5String(bytes: Uint8Array): string {\n\treturn decodeAsciiString(bytes, 'IA5String');\n}\n\nfunction decodePrintableString(bytes: Uint8Array): string {\n\tconst value = decodeAsciiString(bytes, 'PrintableString');\n\tif (!PRINTABLE_STRING_PATTERN.test(value)) {\n\t\tthrow new Error('Invalid PrintableString: contains characters outside the allowed set');\n\t}\n\treturn value;\n}\n\nfunction decodeBmpString(bytes: Uint8Array): string {\n\tif (bytes.length % 2 !== 0) {\n\t\tthrow new Error('Invalid BMPString length');\n\t}\n\tlet value = '';\n\tfor (let index = 0; index < bytes.length; index += 2) {\n\t\tconst left = bytes[index];\n\t\tconst right = bytes[index + 1];\n\t\tif (left === undefined || right === undefined) {\n\t\t\tthrow new Error('Invalid BMPString content');\n\t\t}\n\t\tconst codeUnit = (left << 8) | right;\n\t\tif (codeUnit >= 0xd800 && codeUnit <= 0xdfff) {\n\t\t\tthrow new Error('Invalid BMPString code point');\n\t\t}\n\t\tvalue += String.fromCharCode(codeUnit);\n\t}\n\treturn value;\n}\n\nfunction decodeUniversalString(bytes: Uint8Array): string {\n\tif (bytes.length % 4 !== 0) {\n\t\tthrow new Error('Invalid UniversalString length');\n\t}\n\tlet value = '';\n\tfor (let index = 0; index < bytes.length; index += 4) {\n\t\tconst first = bytes[index];\n\t\tconst second = bytes[index + 1];\n\t\tconst third = bytes[index + 2];\n\t\tconst fourth = bytes[index + 3];\n\t\tif (\n\t\t\tfirst === undefined ||\n\t\t\tsecond === undefined ||\n\t\t\tthird === undefined ||\n\t\t\tfourth === undefined\n\t\t) {\n\t\t\tthrow new Error('Invalid UniversalString content');\n\t\t}\n\t\tconst codePoint = ((first * 256 + second) * 256 + third) * 256 + fourth;\n\t\tif (codePoint > 0x10ffff || (codePoint >= 0xd800 && codePoint <= 0xdfff)) {\n\t\t\tthrow new Error('Invalid UniversalString code point');\n\t\t}\n\t\tvalue += String.fromCodePoint(codePoint);\n\t}\n\treturn value;\n}\n"],"mappings":";;;AAaA,MAAM,IAAc,IAAI,YAAY,SAAS,EAAE,OAAO,GAAK,CAAC,GAEtD,IAA2B;;;;;;;;AASjC,SAAgB,uBAAuB,GAA2B;CACjE,IAAI,EAAM,WAAW,GACpB,MAAU,MAAM,cAAc;CAE/B,IAAM,IAAqB,uBAAuB,GAAO,CAAC,GACtD,IAAS,EAAmB,YAC1B,IACL,EAAmB,QAAQ,KACxB,CAAC,GAAG,EAAmB,KAAK,IAC5B,EAAmB,QAAQ,KAC1B,CAAC,GAAG,EAAmB,QAAQ,EAAE,IACjC,CAAC,GAAG,EAAmB,QAAQ,EAAE;CACtC,OAAO,IAAS,EAAM,SAAQ;EAC7B,IAAM,IAAgB,uBAAuB,GAAO,CAAM;EAE1D,AADA,EAAO,KAAK,EAAc,KAAK,GAC/B,IAAS,EAAc;CACxB;CACA,OAAO,EAAO,KAAK,GAAG;AACvB;;AAGA,SAAgB,MAAM,GAA2B;CAChD,OAAO,MAAM,KAAK,IAAQ,MAAU,EAAM,SAAS,EAAE,CAAC,CAAC,SAAS,GAAG,GAAG,CAAC,CAAC,CAAC,KAAK,EAAE;AACjF;;;;;AAMA,SAAgB,cAAc,GAAgC;CAC7D,IAAM,IAAM,IAAI,YAAY,EAAM,MAAM;CAExC,OADA,IAAI,WAAW,CAAG,CAAC,CAAC,IAAI,CAAK,GACtB;AACR;;;;;;;;AASA,SAAgB,WAAW,GAAoB,GAAkC;CAChF,IAAM,IAAyB,CAAC,GAC5B,IAAS,EAAO;CACpB,OAAO,IAAS,EAAO,MAAK;EAC3B,IAAM,IAAQ,EAAY,GAAQ,CAAM;EACxC,IAAI,EAAM,MAAM,EAAO,KACtB,MAAU,MAAM,iCAAi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by the higher-level DER, parse, and crypto modules.\n *\n * These utilities stay intentionally small and strict so malformed input fails early and\n * higher-level modules can build on one consistent decoding layer.\n *\n * @module\n */\n\nimport type { DerElement } from '#micro509/internal/asn1/der';\nimport { readElement } from '#micro509/internal/asn1/der';\n\n/** Shared UTF-8 text decoder for ASN.1 string types. */\nconst textDecoder = new TextDecoder('utf-8', { fatal: true });\n\nconst PRINTABLE_STRING_PATTERN = /^[A-Za-z0-9 '()+,\\-./:=?]*$/u;\n\n/**\n * Decodes a DER-encoded OBJECT IDENTIFIER value into its dotted-decimal string form.\n *\n * @example \"1.2.840.113549.1.1.1\"\n * @throws on empty input, truncated multi-byte sub-identifiers, or incomplete continuation octets.\n */\nexport function decodeObjectIdentifier(bytes: Uint8Array): string {\n\tif (bytes.length === 0) {\n\t\tthrow new Error('OID is empty');\n\t}\n\tconst firstSubidentifier = decodeOidSubidentifier(bytes, 0);\n\tlet offset = firstSubidentifier.nextOffset;\n\tconst values =\n\t\tfirstSubidentifier.value < 40\n\t\t\t? [0, firstSubidentifier.value]\n\t\t\t: firstSubidentifier.value < 80\n\t\t\t\t? [1, firstSubidentifier.value - 40]\n\t\t\t\t: [2, firstSubidentifier.value - 80];\n\twhile (offset < bytes.length) {\n\t\tconst subidentifier = decodeOidSubidentifier(bytes, offset);\n\t\tvalues.push(subidentifier.value);\n\t\toffset = subidentifier.nextOffset;\n\t}\n\treturn values.join('.');\n}\n\n/** Converts raw bytes to a lowercase hex string with no separator. */\nexport function toHex(bytes: Uint8Array): string {\n\treturn Array.from(bytes, (value) => value.toString(16).padStart(2, '0')).join('');\n}\n\n/**\n * Copies a {@linkcode Uint8Array} into a standalone {@linkcode ArrayBuffer} suitable for\n * Web Crypto operations that require a non-shared backing buffer.\n */\nexport function toArrayBuffer(bytes: Uint8Array): ArrayBuffer {\n\tconst out = new ArrayBuffer(bytes.length);\n\tnew Uint8Array(out).set(bytes);\n\treturn out;\n}\n\n/**\n * Iterates through all direct child TLV elements inside a constructed {@linkcode DerElement}\n * (SEQUENCE, SET, or context-tagged container).\n *\n * @throws if any child overflows the parent boundary or if there is leftover data between the last child and the parent end offset.\n */\nexport function childrenOf(source: Uint8Array, parent: DerElement): DerElement[] {\n\tconst children: DerElement[] = [];\n\tlet offset = parent.start;\n\twhile (offset < parent.end) {\n\t\tconst child = readElement(source, offset);\n\t\tif (child.end > parent.end) {\n\t\t\tthrow new Error('DER child exceeds parent length');\n\t\t}\n\t\tchildren.push(child);\n\t\toffset = child.end;\n\t}\n\tif (offset !== parent.end) {\n\t\tthrow new Error('Malformed DER container');\n\t}\n\treturn children;\n}\n\n/**\n * Asserts that a value is not `undefined`, returning it narrowed.\\\n * Used to enforce the presence of mandatory ASN.1 fields during parsing.\n *\n * @param label Human-readable field name included in the error message on failure.\n */\nexport function requireElement<T>(value: T | undefined, label: string): T {\n\tif (value === undefined) {\n\t\tthrow new Error(`Missing ${label}`);\n\t}\n\treturn value;\n}\n\n/**\n * Strips the leading \"unused bits\" octet from a BIT STRING element, returning just the payload bytes.\n *\n * Used to extract signature values and public keys from their BIT STRING wrapper.\n *\n * @throws if the tag is not `0x03`.\n */\nexport function extractBitStringValue(element: DerElement): Uint8Array {\n\tif (element.tag !== 0x03) {\n\t\tthrow new Error('Expected BIT STRING');\n\t}\n\tconst unusedBits = element.value[0];\n\tif (unusedBits === undefined || unusedBits > 7) {\n\t\tthrow new Error('Invalid BIT STRING');\n\t}\n\tif (unusedBits !== 0) {\n\t\tthrow new Error('BIT STRING must have zero unused bits');\n\t}\n\treturn element.value.slice(1);\n}\n\n/** A BIT STRING payload and the number of unused trailing bits in its final byte. */\nexport interface DerBitString {\n\t/** Payload bytes, excluding the leading unused-bit count octet. */\n\treadonly bytes: Uint8Array;\n\t/** Unused trailing bits in the final byte, 0 through 7. */\n\treadonly unusedBits: number;\n\t/** `true` when the original encoding had non-zero padding bits (DER violation). */\n\treadonly nonZeroPadding: boolean;\n}\n\n/**\n * Decodes a BIT STRING element into its payload and unused-bit count.\n *\n * Accepts a non-zero unused-bit count, so it reads flag strings such as {@linkcode KeyUsage},\n * which {@linkcode extractBitStringValue} rejects.\n *\n * Padding bits that {@linkcode https://www.itu.int/rec/T-REC-X.690-202102-I/en | X.690 §11.2.1} requires to be zero are reported through\n * {@linkcode DerBitString.nonZeroPadding} rather than rejected, so a caller can decide.\n */\nexport function decodeBitString(element: DerElement): DerBitString {\n\tif (element.tag !== 0x03) {\n\t\tthrow new Error('Expected BIT STRING');\n\t}\n\tconst unusedBits = element.value[0];\n\tif (unusedBits === undefined || unusedBits > 7) {\n\t\tthrow new Error('Invalid BIT STRING');\n\t}\n\tconst bytes = element.value.slice(1);\n\tif (bytes.length === 0 && unusedBits !== 0) {\n\t\tthrow new Error('Invalid BIT STRING');\n\t}\n\tconst paddingMask = (1 << unusedBits) - 1;\n\tconst nonZeroPadding =\n\t\tunusedBits > 0 && bytes.length > 0 && ((bytes[bytes.length - 1] ?? 0) & paddingMask) !== 0;\n\treturn { bytes, unusedBits, nonZeroPadding };\n}\n\n/**\n * Parses a {@linkcode DerElement} with tag UTCTime (`0x17`) or GeneralizedTime (`0x18`) into a {@linkcode Date}.\n *\n * For UTCTime, the two-digit year is interpreted per RFC 5280:\n * - values >= 50 map to 19xx,\n * - values < 50 map to 20xx.\n *\n * @throws on unrecognized time tags.\n */\nexport function parseTime(element: DerElement): Date {\n\tif (element.tag === 0x17) {\n\t\tconst value = decodeUtf8Text(element.value, 'UTCTime');\n\t\tconst match = /^(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})Z$/.exec(value);\n\t\tif (match === null) {\n\t\t\tthrow new Error('Invalid UTCTime');\n\t\t}\n\t\tconst yearText = requireElement(match[1], 'UTCTime year');\n\t\tconst monthText = requireElement(match[2], 'UTCTime month');\n\t\tconst dayText = requireElement(match[3], 'UTCTime day');\n\t\tconst hourText = requireElement(match[4], 'UTCTime hour');\n\t\tconst minuteText = requireElement(match[5], 'UTCTime minute');\n\t\tconst secondText = requireElement(match[6], 'UTCTime second');\n\t\tconst year = Number.parseInt(yearText, 10);\n\t\treturn buildStrictUtcDate(\n\t\t\tyear >= 50 ? 1900 + year : 2000 + year,\n\t\t\tmonthText,\n\t\t\tdayText,\n\t\t\thourText,\n\t\t\tminuteText,\n\t\t\tsecondText,\n\t\t\t'UTCTime',\n\t\t);\n\t}\n\tif (element.tag === 0x18) {\n\t\tconst value = decodeUtf8Text(element.value, 'GeneralizedTime');\n\t\tconst match = /^(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})Z$/.exec(value);\n\t\tif (match === null) {\n\t\t\tthrow new Error('Invalid GeneralizedTime');\n\t\t}\n\t\tconst yearText = requireElement(match[1], 'GeneralizedTime year');\n\t\tconst monthText = requireElement(match[2], 'GeneralizedTime month');\n\t\tconst dayText = requireElement(match[3], 'GeneralizedTime day');\n\t\tconst hourText = requireElement(match[4], 'GeneralizedTime hour');\n\t\tconst minuteText = requireElement(match[5], 'GeneralizedTime minute');\n\t\tconst secondText = requireElement(match[6], 'GeneralizedTime second');\n\t\treturn buildStrictUtcDate(\n\t\t\tNumber.parseInt(yearText, 10),\n\t\t\tmonthText,\n\t\t\tdayText,\n\t\t\thourText,\n\t\t\tminuteText,\n\t\t\tsecondText,\n\t\t\t'GeneralizedTime',\n\t\t);\n\t}\n\tthrow new Error(`Unsupported time tag: ${element.tag}`);\n}\n\n/**\n * Decodes a big-endian unsigned byte sequence into a JavaScript `number`.\n *\n * @throws if the value exceeds {@linkcode Number.MAX_SAFE_INTEGER}, the boundary for lossless arithmetic.\n */\nexport function decodeIntegerNumber(bytes: Uint8Array): number {\n\tconst first = bytes[0];\n\tif (first === undefined) {\n\t\tthrow new Error('INTEGER is empty');\n\t}\n\tif ((first & 0x80) !== 0) {\n\t\tthrow new Error('INTEGER must be non-negative');\n\t}\n\tif (bytes.length > 1 && first === 0 && ((bytes[1] ?? 0) & 0x80) === 0) {\n\t\tthrow new Error('INTEGER must use minimal encoding');\n\t}\n\tlet value = 0;\n\tfor (const byte of bytes) {\n\t\tif (value > Math.floor((Number.MAX_SAFE_INTEGER - byte) / 256)) {\n\t\t\tthrow new Error(`Integer too large for safe number (${bytes.length} bytes)`);\n\t\t}\n\t\tvalue = value * 256 + byte;\n\t}\n\treturn value;\n}\n\n/**\n * Like {@linkcode decodeIntegerNumber}, but optionally rewrites thrown error messages with a caller-specific field label.\n *\n * @param bytes DER INTEGER content octets to decode.\n * @param label Field name for error messages (defaults to `\"INTEGER\"`).\n * @throws if the value is empty, negative, non-minimally encoded, or exceeds {@linkcode Number.MAX_SAFE_INTEGER}.\n */\nexport function decodeNonNegativeIntegerNumber(bytes: Uint8Array, label = 'INTEGER'): number {\n\tif (label === 'INTEGER') {\n\t\treturn decodeIntegerNumber(bytes);\n\t}\n\ttry {\n\t\treturn decodeIntegerNumber(bytes);\n\t} catch (error) {\n\t\tif (!(error instanceof Error)) {\n\t\t\tthrow error;\n\t\t}\n\t\tswitch (error.message) {\n\t\t\tcase 'INTEGER is empty':\n\t\t\t\tthrow new Error(`${label} is empty`);\n\t\t\tcase 'INTEGER must be non-negative':\n\t\t\t\tthrow new Error(`${label} must be non-negative`);\n\t\t\tcase 'INTEGER must use minimal encoding':\n\t\t\t\tthrow new Error(`${label} must use minimal encoding`);\n\t\t\tdefault:\n\t\t\t\tif (error.message.startsWith('Integer too large for safe number')) {\n\t\t\t\t\tthrow new Error(`${label} too large for safe number (${bytes.length} bytes)`);\n\t\t\t\t}\n\t\t\t\tthrow error;\n\t\t}\n\t}\n}\n\n/**\n * Converts a hex string (even or odd length) to a {@linkcode Uint8Array}.\n * Odd-length strings are left-padded with a zero nibble.\n */\nexport function hexToBytes(value: string): Uint8Array {\n\tconst normalized = value.length % 2 === 0 ? value : `0${value}`;\n\tconst out = new Uint8Array(normalized.length / 2);\n\tfor (let index = 0; index < out.length; index += 1) {\n\t\tconst chunk = normalized.slice(index * 2, index * 2 + 2);\n\t\tif (!/^[0-9a-fA-F]{2}$/.test(chunk)) {\n\t\t\tthrow new Error(`Invalid hex byte: ${chunk}`);\n\t\t}\n\t\tconst parsed = Number.parseInt(chunk, 16);\n\t\tout[index] = parsed;\n\t}\n\treturn out;\n}\n\n/** Decodes a DER BOOLEAN value: any non-zero first byte is `true`. */\nexport function decodeBoolean(bytes: Uint8Array): boolean {\n\tif (bytes.length !== 1) {\n\t\tthrow new Error('BOOLEAN must contain exactly one octet');\n\t}\n\tconst value = bytes[0];\n\tif (value !== 0x00 && value !== 0xff) {\n\t\tthrow new Error('BOOLEAN must use DER encoding');\n\t}\n\treturn value === 0xff;\n}\n\nfunction buildStrictUtcDate(\n\tyear: number,\n\tmonthText: string,\n\tdayText: string,\n\thourText: string,\n\tminuteText: string,\n\tsecondText: string,\n\tlabel: 'UTCTime' | 'GeneralizedTime',\n): Date {\n\tconst month = Number.parseInt(monthText, 10);\n\tconst day = Number.parseInt(dayText, 10);\n\tconst hour = Number.parseInt(hourText, 10);\n\tconst minute = Number.parseInt(minuteText, 10);\n\tconst second = Number.parseInt(secondText, 10);\n\tconst date = new Date(Date.UTC(year, month - 1, day, hour, minute, second));\n\tif (\n\t\tNumber.isNaN(date.getTime()) ||\n\t\tdate.getUTCFullYear() !== year ||\n\t\tdate.getUTCMonth() !== month - 1 ||\n\t\tdate.getUTCDate() !== day ||\n\t\tdate.getUTCHours() !== hour ||\n\t\tdate.getUTCMinutes() !== minute ||\n\t\tdate.getUTCSeconds() !== second\n\t) {\n\t\tthrow new Error(`Invalid ${label}`);\n\t}\n\treturn date;\n}\n\nfunction decodeOidSubidentifier(\n\tbytes: Uint8Array,\n\tstart: number,\n): {\n\treadonly value: number;\n\treadonly nextOffset: number;\n} {\n\tconst first = bytes[start];\n\tif (first === undefined) {\n\t\tthrow new Error('Malformed OID');\n\t}\n\tif (first === 0x80) {\n\t\tthrow new Error('Malformed OID: non-minimal base-128 encoding');\n\t}\n\tlet value = 0;\n\tlet offset = start;\n\tfor (; offset < bytes.length; offset += 1) {\n\t\tconst next = bytes[offset];\n\t\tif (next === undefined) {\n\t\t\tthrow new Error('Malformed OID');\n\t\t}\n\t\tconst digit = next & 0x7f;\n\t\tif (value > Math.floor((Number.MAX_SAFE_INTEGER - digit) / 128)) {\n\t\t\tthrow new Error('Malformed OID: overflow/non-minimal or too-large subidentifier');\n\t\t}\n\t\tvalue = value * 128 + digit;\n\t\tif ((next & 0x80) === 0) {\n\t\t\treturn {\n\t\t\t\tvalue,\n\t\t\t\tnextOffset: offset + 1,\n\t\t\t};\n\t\t}\n\t}\n\tthrow new Error('Malformed OID: incomplete continuation');\n}\n\n/**\n * Decodes a DER string element by tag.\n *\n * Supports:\n * - UTF8String (`0x0c`),\n * - PrintableString (`0x13`),\n * - IA5String (`0x16`),\n * - UniversalString (`0x1c`), and\n * - BMPString (`0x1e`).\n *\n * @throws on unsupported string tags.\n */\nexport function decodeString(tag: number, bytes: Uint8Array): string {\n\tswitch (tag) {\n\t\tcase 0x0c:\n\t\t\treturn decodeUtf8Text(bytes, 'UTF8String');\n\t\tcase 0x13:\n\t\t\treturn decodePrintableString(bytes);\n\t\tcase 0x16:\n\t\t\treturn decodeIa5String(bytes);\n\t\tcase 0x14:\n\t\t\tthrow new Error('Unsupported string tag: 20 (TeletexString)');\n\t\tcase 0x1c:\n\t\t\treturn decodeUniversalString(bytes);\n\t\tcase 0x1e:\n\t\t\treturn decodeBmpString(bytes);\n\t\tdefault:\n\t\t\tthrow new Error(`Unsupported string tag: ${tag}`);\n\t}\n}\n\nfunction decodeUtf8Text(\n\tbytes: Uint8Array,\n\tlabel: 'GeneralizedTime' | 'UTCTime' | 'UTF8String',\n): string {\n\ttry {\n\t\treturn textDecoder.decode(bytes);\n\t} catch (error) {\n\t\tif (error instanceof Error) {\n\t\t\tthrow new Error(`Invalid ${label}: invalid UTF-8`, { cause: error });\n\t\t}\n\t\tthrow error;\n\t}\n}\n\nfunction decodeAsciiString(bytes: Uint8Array, label: 'IA5String' | 'PrintableString'): string {\n\tlet value = '';\n\tfor (const byte of bytes) {\n\t\tif (byte > 0x7f) {\n\t\t\tthrow new Error(`Invalid ${label}: contains non-ASCII bytes`);\n\t\t}\n\t\tvalue += String.fromCharCode(byte);\n\t}\n\treturn value;\n}\n\nfunction decodeIa5String(bytes: Uint8Array): string {\n\treturn decodeAsciiString(bytes, 'IA5String');\n}\n\nfunction decodePrintableString(bytes: Uint8Array): string {\n\tconst value = decodeAsciiString(bytes, 'PrintableString');\n\tif (!PRINTABLE_STRING_PATTERN.test(value)) {\n\t\tthrow new Error('Invalid PrintableString: contains characters outside the allowed set');\n\t}\n\treturn value;\n}\n\nfunction decodeBmpString(bytes: Uint8Array): string {\n\tif (bytes.length % 2 !== 0) {\n\t\tthrow new Error('Invalid BMPString length');\n\t}\n\tlet value = '';\n\tfor (let index = 0; index < bytes.length; index += 2) {\n\t\tconst left = bytes[index];\n\t\tconst right = bytes[index + 1];\n\t\tif (left === undefined || right === undefined) {\n\t\t\tthrow new Error('Invalid BMPString content');\n\t\t}\n\t\tconst codeUnit = (left << 8) | right;\n\t\tif (codeUnit >= 0xd800 && codeUnit <= 0xdfff) {\n\t\t\tthrow new Error('Invalid BMPString code point');\n\t\t}\n\t\tvalue += String.fromCharCode(codeUnit);\n\t}\n\treturn value;\n}\n\nfunction decodeUniversalString(bytes: Uint8Array): string {\n\tif (bytes.length % 4 !== 0) {\n\t\tthrow new Error('Invalid UniversalString length');\n\t}\n\tlet value = '';\n\tfor (let index = 0; index < bytes.length; index += 4) {\n\t\tconst first = bytes[index];\n\t\tconst second = bytes[index + 1];\n\t\tconst third = bytes[index + 2];\n\t\tconst fourth = bytes[index + 3];\n\t\tif (\n\t\t\tfirst === undefined ||\n\t\t\tsecond === undefined ||\n\t\t\tthird === undefined ||\n\t\t\tfourth === undefined\n\t\t) {\n\t\t\tthrow new Error('Invalid UniversalString content');\n\t\t}\n\t\tconst codePoint = ((first * 256 + second) * 256 + third) * 256 + fourth;\n\t\tif (codePoint > 0x10ffff || (codePoint >= 0xd800 && codePoint <= 0xdfff)) {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AAQ,IACtB,IAAS,EAAM,IAAQ;EAC7B,IACC,MAAU,KAAA,KACV,MAAW,KAAA,KACX,MAAU,KAAA,KACV,MAAW,KAAA,GAEX,MAAU,MAAM,iCAAiC;EAElD,IAAM,MAAc,IAAQ,MAAM,KAAU,MAAM,KAAS,MAAM;EACjE,IAAI,IAAY,WAAa,KAAa,SAAU,KAAa,OAChE,MAAU,MAAM,oCAAoC;EAErD,KAAS,OAAO,cAAc,CAAS;CACxC;CACA,OAAO;AACR"}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
//#region src/internal/asn1/der.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Low-level DER encoding and reading helpers shared across the library.
|
|
4
|
+
*
|
|
5
|
+
* These utilities build and traverse ASN.1 TLV (tag-length-value) structures.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Maximum nesting depth allowed when recursively walking a DER structure.
|
|
11
|
+
*
|
|
12
|
+
* Guards against stack exhaustion from pathologically nested input.
|
|
13
|
+
*/
|
|
14
|
+
declare const DEFAULT_MAX_DER_DEPTH = 64;
|
|
15
|
+
/** Concatenates multiple byte arrays into a single {@linkcode Uint8Array}. */
|
|
16
|
+
declare function concatBytes(parts: readonly Uint8Array[]): Uint8Array;
|
|
17
|
+
/**
|
|
18
|
+
* Builds a complete DER TLV (tag-length-value) element:
|
|
19
|
+
*
|
|
20
|
+
* - one tag octet,
|
|
21
|
+
* - the DER-encoded length, then
|
|
22
|
+
* - the raw value bytes.
|
|
23
|
+
*/
|
|
24
|
+
declare function tlv(tag: number, value: Uint8Array): Uint8Array;
|
|
25
|
+
/** Wraps concatenated children in a SEQUENCE (tag `0x30`). */
|
|
26
|
+
declare function sequence(parts: readonly Uint8Array[]): Uint8Array;
|
|
27
|
+
/**
|
|
28
|
+
* Wraps children in a SET (tag `0x31`) after DER-sorting them lexicographically by encoded bytes,
|
|
29
|
+
* as required by {@linkcode https://www.itu.int/rec/T-REC-X.690-202102-I/en | X.690} DER.
|
|
30
|
+
*/
|
|
31
|
+
declare function setOf(parts: readonly Uint8Array[]): Uint8Array;
|
|
32
|
+
/**
|
|
33
|
+
* Wraps a value in an explicit context-specific constructed tag (`0xa0 + tag`).
|
|
34
|
+
*
|
|
35
|
+
* Used for optional SEQUENCE fields tagged with `[tag] EXPLICIT`.
|
|
36
|
+
*/
|
|
37
|
+
declare function explicitContext(tag: number, value: Uint8Array): Uint8Array;
|
|
38
|
+
/**
|
|
39
|
+
* Wraps a value in an implicit context-specific constructed tag (`0xa0 + tag`).
|
|
40
|
+
*
|
|
41
|
+
* Used for `[tag] IMPLICIT` fields whose underlying type is constructed (e.g. SEQUENCE).
|
|
42
|
+
*/
|
|
43
|
+
declare function implicitConstructedContext(tag: number, value: Uint8Array): Uint8Array;
|
|
44
|
+
/**
|
|
45
|
+
* Wraps a value in an implicit context-specific primitive tag (`0x80 + tag`).
|
|
46
|
+
*
|
|
47
|
+
* Used for `[tag] IMPLICIT` fields whose underlying type is primitive (e.g. OCTET STRING).
|
|
48
|
+
*/
|
|
49
|
+
declare function implicitPrimitiveContext(tag: number, value: Uint8Array): Uint8Array;
|
|
50
|
+
/**
|
|
51
|
+
* Encodes raw big-endian bytes as a DER INTEGER (tag `0x02`).
|
|
52
|
+
*
|
|
53
|
+
* Strips leading zero bytes for minimal encoding and prepends a zero byte when the high bit is set
|
|
54
|
+
* to keep the value non-negative.
|
|
55
|
+
*/
|
|
56
|
+
declare function integer(bytes: Uint8Array): Uint8Array;
|
|
57
|
+
/**
|
|
58
|
+
* Encodes a non-negative JavaScript `number` as a DER INTEGER.
|
|
59
|
+
*
|
|
60
|
+
* @throws if the value is not a non-negative safe integer.
|
|
61
|
+
*/
|
|
62
|
+
declare function integerFromNumber(value: number): Uint8Array;
|
|
63
|
+
/** Encodes a DER BOOLEAN (tag `0x01`): `true` → `0xff`, `false` → `0x00`. */
|
|
64
|
+
declare function bool(value: boolean): Uint8Array;
|
|
65
|
+
/** Produces a DER NULL element (tag `0x05`, zero-length value). */
|
|
66
|
+
declare function nullValue(): Uint8Array;
|
|
67
|
+
/** Wraps raw bytes in an OCTET STRING element (tag `0x04`). */
|
|
68
|
+
declare function octetString(value: Uint8Array): Uint8Array;
|
|
69
|
+
/**
|
|
70
|
+
* Encodes a DER BIT STRING (tag `0x03`).
|
|
71
|
+
*
|
|
72
|
+
* The value is prefixed with a single octet indicating how many trailing bits in the last byte are unused.
|
|
73
|
+
*
|
|
74
|
+
* @param unusedBits Number of unused trailing bits (0–7). Defaults to 0.
|
|
75
|
+
*/
|
|
76
|
+
declare function bitString(value: Uint8Array, unusedBits?: number): Uint8Array;
|
|
77
|
+
/** Encodes a DER UTF8String (tag `0x0c`). */
|
|
78
|
+
declare function utf8String(value: string): Uint8Array;
|
|
79
|
+
/**
|
|
80
|
+
* Encodes a DER PrintableString (tag `0x13`).
|
|
81
|
+
*
|
|
82
|
+
* @throws if the input contains characters outside the X.520 PrintableString set.
|
|
83
|
+
*/
|
|
84
|
+
declare function printableString(value: string): Uint8Array;
|
|
85
|
+
/**
|
|
86
|
+
* Encodes a DER IA5String (tag `0x16`).
|
|
87
|
+
*
|
|
88
|
+
* @throws if the input contains any non-ASCII character (code point > 0x7f).
|
|
89
|
+
*/
|
|
90
|
+
declare function ia5String(value: string): Uint8Array;
|
|
91
|
+
/**
|
|
92
|
+
* Encodes a DER BMPString (tag `0x1e`) as big-endian UTF-16.
|
|
93
|
+
*
|
|
94
|
+
* @throws on lone surrogates and on code points above the Basic Multilingual Plane.
|
|
95
|
+
*/
|
|
96
|
+
declare function bmpString(value: string): Uint8Array;
|
|
97
|
+
/**
|
|
98
|
+
* Encodes a DER UniversalString (tag `0x1c`) as big-endian UTF-32.
|
|
99
|
+
*
|
|
100
|
+
* @throws on lone surrogates.
|
|
101
|
+
*/
|
|
102
|
+
declare function universalString(value: string): Uint8Array;
|
|
103
|
+
/**
|
|
104
|
+
* Encodes a dotted-decimal OID string as a DER OBJECT IDENTIFIER (tag `0x06`).
|
|
105
|
+
*
|
|
106
|
+
* Validates arc constraints per X.660: first arc must be 0–2, second < 40 for arcs 0 and 1.\
|
|
107
|
+
* Sub-identifiers are encoded with base-128 continuation.
|
|
108
|
+
*/
|
|
109
|
+
declare function objectIdentifier(oid: string): Uint8Array;
|
|
110
|
+
/**
|
|
111
|
+
* Encodes a `Date` as a DER UTCTime (tag `0x17`), format `YYMMDDHHMMSSZ`.
|
|
112
|
+
*
|
|
113
|
+
* Only the two-digit year is stored; suitable for dates in 1950–2049.
|
|
114
|
+
*/
|
|
115
|
+
declare function utcTime(date: Date): Uint8Array;
|
|
116
|
+
/**
|
|
117
|
+
* Encodes a {@linkcode Date} as a DER GeneralizedTime (tag `0x18`), format `YYYYMMDDHHMMSSZ`.
|
|
118
|
+
*
|
|
119
|
+
* Uses a four-digit year; required for dates outside the 1950–2049 range.
|
|
120
|
+
*/
|
|
121
|
+
declare function generalizedTime(date: Date): Uint8Array;
|
|
122
|
+
/**
|
|
123
|
+
* Encodes a {@linkcode Date} as the appropriate DER time type per RFC 5280.
|
|
124
|
+
*
|
|
125
|
+
* - {@linkcode utcTime} for 1950–2049
|
|
126
|
+
* - {@linkcode generalizedTime} otherwise
|
|
127
|
+
*/
|
|
128
|
+
declare function time(date: Date): Uint8Array;
|
|
129
|
+
/** A single parsed ASN.1 TLV element with byte-range metadata. */
|
|
130
|
+
interface DerElement {
|
|
131
|
+
/** ASN.1 tag byte (e.g. `0x30` for SEQUENCE, `0x02` for INTEGER). */
|
|
132
|
+
readonly tag: number;
|
|
133
|
+
/** Number of bytes occupied by the tag + length octets. */
|
|
134
|
+
readonly headerLength: number;
|
|
135
|
+
/** Byte length of the value portion (excluding tag and length octets). */
|
|
136
|
+
readonly length: number;
|
|
137
|
+
/** Byte offset where the value portion begins in the source buffer. */
|
|
138
|
+
readonly start: number;
|
|
139
|
+
/** Byte offset one past the last value byte. Equals the next element's header offset. */
|
|
140
|
+
readonly end: number;
|
|
141
|
+
/** The raw value bytes (slice of the source buffer). */
|
|
142
|
+
readonly value: Uint8Array;
|
|
143
|
+
}
|
|
144
|
+
/** Options for {@linkcode readSequenceChildren}. */
|
|
145
|
+
interface ReadSequenceChildrenOptions {
|
|
146
|
+
/** Maximum nesting depth for the DER depth check. @default {@linkcode DEFAULT_MAX_DER_DEPTH}. */
|
|
147
|
+
readonly maxDepth?: number;
|
|
148
|
+
/** Constructed tags whose inner bytes may not parse as valid TLV children (e.g. opaque extension values). */
|
|
149
|
+
readonly allowOpaqueConstructedTags?: readonly number[];
|
|
150
|
+
}
|
|
151
|
+
/** Options for {@linkcode readRootElement}. */
|
|
152
|
+
interface ReadRootElementOptions {
|
|
153
|
+
/** Maximum nesting depth for the DER depth check. @default {@linkcode DEFAULT_MAX_DER_DEPTH}. */
|
|
154
|
+
readonly maxDepth?: number;
|
|
155
|
+
/** Constructed tags whose inner bytes may not parse as valid TLV children (e.g. opaque extension values). */
|
|
156
|
+
readonly allowOpaqueConstructedTags?: readonly number[];
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Walks the full DER tree rooted in {@linkcode bytes}.
|
|
160
|
+
*
|
|
161
|
+
* Constructed tags with content that cannot be parsed as valid children are tolerated when listed in {@linkcode options | allowOpaqueConstructedTags}.
|
|
162
|
+
*
|
|
163
|
+
* @throws if nesting exceeds {@linkcode maxDepth}.
|
|
164
|
+
*/
|
|
165
|
+
declare function assertDerMaxDepth(bytes: Uint8Array, maxDepth?: number, options?: {
|
|
166
|
+
/** Constructed tags whose inner bytes may not parse as valid TLV children. */
|
|
167
|
+
readonly allowOpaqueConstructedTags?: readonly number[];
|
|
168
|
+
}): void;
|
|
169
|
+
//#endregion
|
|
170
|
+
export { DEFAULT_MAX_DER_DEPTH, DerElement, ReadRootElementOptions, ReadSequenceChildrenOptions, assertDerMaxDepth, bitString, bmpString, bool, concatBytes, explicitContext, generalizedTime, ia5String, implicitConstructedContext, implicitPrimitiveContext, integer, integerFromNumber, nullValue, objectIdentifier, octetString, printableString, sequence, setOf, time, tlv, universalString, utcTime, utf8String };
|
|
171
|
+
//# sourceMappingURL=der.d.ts.map
|